inode.c 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171
  1. /*
  2. * (C) 1997 Linus Torvalds
  3. * (C) 1999 Andrea Arcangeli <andrea@suse.de> (dynamic inode allocation)
  4. */
  5. #include <linux/export.h>
  6. #include <linux/fs.h>
  7. #include <linux/mm.h>
  8. #include <linux/backing-dev.h>
  9. #include <linux/hash.h>
  10. #include <linux/swap.h>
  11. #include <linux/security.h>
  12. #include <linux/cdev.h>
  13. #include <linux/bootmem.h>
  14. #include <linux/fsnotify.h>
  15. #include <linux/mount.h>
  16. #include <linux/posix_acl.h>
  17. #include <linux/prefetch.h>
  18. #include <linux/buffer_head.h> /* for inode_has_buffers */
  19. #include <linux/ratelimit.h>
  20. #include <linux/list_lru.h>
  21. #include <linux/iversion.h>
  22. #include <trace/events/writeback.h>
  23. #include "internal.h"
  24. /*
  25. * Inode locking rules:
  26. *
  27. * inode->i_lock protects:
  28. * inode->i_state, inode->i_hash, __iget()
  29. * Inode LRU list locks protect:
  30. * inode->i_sb->s_inode_lru, inode->i_lru
  31. * inode->i_sb->s_inode_list_lock protects:
  32. * inode->i_sb->s_inodes, inode->i_sb_list
  33. * bdi->wb.list_lock protects:
  34. * bdi->wb.b_{dirty,io,more_io,dirty_time}, inode->i_io_list
  35. * inode_hash_lock protects:
  36. * inode_hashtable, inode->i_hash
  37. *
  38. * Lock ordering:
  39. *
  40. * inode->i_sb->s_inode_list_lock
  41. * inode->i_lock
  42. * Inode LRU list locks
  43. *
  44. * bdi->wb.list_lock
  45. * inode->i_lock
  46. *
  47. * inode_hash_lock
  48. * inode->i_sb->s_inode_list_lock
  49. * inode->i_lock
  50. *
  51. * iunique_lock
  52. * inode_hash_lock
  53. */
  54. static unsigned int i_hash_mask __read_mostly;
  55. static unsigned int i_hash_shift __read_mostly;
  56. static struct hlist_head *inode_hashtable __read_mostly;
  57. static __cacheline_aligned_in_smp DEFINE_SPINLOCK(inode_hash_lock);
  58. /*
  59. * Empty aops. Can be used for the cases where the user does not
  60. * define any of the address_space operations.
  61. */
  62. const struct address_space_operations empty_aops = {
  63. };
  64. EXPORT_SYMBOL(empty_aops);
  65. /*
  66. * Statistics gathering..
  67. */
  68. struct inodes_stat_t inodes_stat;
  69. static DEFINE_PER_CPU(unsigned long, nr_inodes);
  70. static DEFINE_PER_CPU(unsigned long, nr_unused);
  71. static struct kmem_cache *inode_cachep __read_mostly;
  72. static long get_nr_inodes(void)
  73. {
  74. int i;
  75. long sum = 0;
  76. for_each_possible_cpu(i)
  77. sum += per_cpu(nr_inodes, i);
  78. return sum < 0 ? 0 : sum;
  79. }
  80. static inline long get_nr_inodes_unused(void)
  81. {
  82. int i;
  83. long sum = 0;
  84. for_each_possible_cpu(i)
  85. sum += per_cpu(nr_unused, i);
  86. return sum < 0 ? 0 : sum;
  87. }
  88. long get_nr_dirty_inodes(void)
  89. {
  90. /* not actually dirty inodes, but a wild approximation */
  91. long nr_dirty = get_nr_inodes() - get_nr_inodes_unused();
  92. return nr_dirty > 0 ? nr_dirty : 0;
  93. }
  94. /*
  95. * Handle nr_inode sysctl
  96. */
  97. #ifdef CONFIG_SYSCTL
  98. int proc_nr_inodes(struct ctl_table *table, int write,
  99. void __user *buffer, size_t *lenp, loff_t *ppos)
  100. {
  101. inodes_stat.nr_inodes = get_nr_inodes();
  102. inodes_stat.nr_unused = get_nr_inodes_unused();
  103. return proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
  104. }
  105. #endif
  106. static int no_open(struct inode *inode, struct file *file)
  107. {
  108. return -ENXIO;
  109. }
  110. /**
  111. * inode_init_always - perform inode structure initialisation
  112. * @sb: superblock inode belongs to
  113. * @inode: inode to initialise
  114. *
  115. * These are initializations that need to be done on every inode
  116. * allocation as the fields are not initialised by slab allocation.
  117. */
  118. int inode_init_always(struct super_block *sb, struct inode *inode)
  119. {
  120. static const struct inode_operations empty_iops;
  121. static const struct file_operations no_open_fops = {.open = no_open};
  122. struct address_space *const mapping = &inode->i_data;
  123. inode->i_sb = sb;
  124. inode->i_blkbits = sb->s_blocksize_bits;
  125. inode->i_flags = 0;
  126. atomic64_set(&inode->i_sequence, 0);
  127. atomic_set(&inode->i_count, 1);
  128. inode->i_op = &empty_iops;
  129. inode->i_fop = &no_open_fops;
  130. inode->__i_nlink = 1;
  131. inode->i_opflags = 0;
  132. if (sb->s_xattr)
  133. inode->i_opflags |= IOP_XATTR;
  134. i_uid_write(inode, 0);
  135. i_gid_write(inode, 0);
  136. atomic_set(&inode->i_writecount, 0);
  137. inode->i_size = 0;
  138. inode->i_write_hint = WRITE_LIFE_NOT_SET;
  139. inode->i_blocks = 0;
  140. inode->i_bytes = 0;
  141. inode->i_generation = 0;
  142. inode->i_pipe = NULL;
  143. inode->i_bdev = NULL;
  144. inode->i_cdev = NULL;
  145. inode->i_link = NULL;
  146. inode->i_dir_seq = 0;
  147. inode->i_rdev = 0;
  148. inode->dirtied_when = 0;
  149. #ifdef CONFIG_CGROUP_WRITEBACK
  150. inode->i_wb_frn_winner = 0;
  151. inode->i_wb_frn_avg_time = 0;
  152. inode->i_wb_frn_history = 0;
  153. #endif
  154. if (security_inode_alloc(inode))
  155. goto out;
  156. spin_lock_init(&inode->i_lock);
  157. lockdep_set_class(&inode->i_lock, &sb->s_type->i_lock_key);
  158. init_rwsem(&inode->i_rwsem);
  159. lockdep_set_class(&inode->i_rwsem, &sb->s_type->i_mutex_key);
  160. atomic_set(&inode->i_dio_count, 0);
  161. mapping->a_ops = &empty_aops;
  162. mapping->host = inode;
  163. mapping->flags = 0;
  164. mapping->wb_err = 0;
  165. atomic_set(&mapping->i_mmap_writable, 0);
  166. mapping_set_gfp_mask(mapping, GFP_HIGHUSER_MOVABLE);
  167. mapping->private_data = NULL;
  168. mapping->writeback_index = 0;
  169. inode->i_private = NULL;
  170. inode->i_mapping = mapping;
  171. INIT_HLIST_HEAD(&inode->i_dentry); /* buggered by rcu freeing */
  172. #ifdef CONFIG_FS_POSIX_ACL
  173. inode->i_acl = inode->i_default_acl = ACL_NOT_CACHED;
  174. #endif
  175. #ifdef CONFIG_FSNOTIFY
  176. inode->i_fsnotify_mask = 0;
  177. #endif
  178. inode->i_flctx = NULL;
  179. this_cpu_inc(nr_inodes);
  180. return 0;
  181. out:
  182. return -ENOMEM;
  183. }
  184. EXPORT_SYMBOL(inode_init_always);
  185. static struct inode *alloc_inode(struct super_block *sb)
  186. {
  187. struct inode *inode;
  188. if (sb->s_op->alloc_inode)
  189. inode = sb->s_op->alloc_inode(sb);
  190. else
  191. inode = kmem_cache_alloc(inode_cachep, GFP_KERNEL);
  192. if (!inode)
  193. return NULL;
  194. if (unlikely(inode_init_always(sb, inode))) {
  195. if (inode->i_sb->s_op->destroy_inode)
  196. inode->i_sb->s_op->destroy_inode(inode);
  197. else
  198. kmem_cache_free(inode_cachep, inode);
  199. return NULL;
  200. }
  201. return inode;
  202. }
  203. void free_inode_nonrcu(struct inode *inode)
  204. {
  205. kmem_cache_free(inode_cachep, inode);
  206. }
  207. EXPORT_SYMBOL(free_inode_nonrcu);
  208. void __destroy_inode(struct inode *inode)
  209. {
  210. BUG_ON(inode_has_buffers(inode));
  211. inode_detach_wb(inode);
  212. security_inode_free(inode);
  213. fsnotify_inode_delete(inode);
  214. locks_free_lock_context(inode);
  215. if (!inode->i_nlink) {
  216. WARN_ON(atomic_long_read(&inode->i_sb->s_remove_count) == 0);
  217. atomic_long_dec(&inode->i_sb->s_remove_count);
  218. }
  219. #ifdef CONFIG_FS_POSIX_ACL
  220. if (inode->i_acl && !is_uncached_acl(inode->i_acl))
  221. posix_acl_release(inode->i_acl);
  222. if (inode->i_default_acl && !is_uncached_acl(inode->i_default_acl))
  223. posix_acl_release(inode->i_default_acl);
  224. #endif
  225. this_cpu_dec(nr_inodes);
  226. }
  227. EXPORT_SYMBOL(__destroy_inode);
  228. static void i_callback(struct rcu_head *head)
  229. {
  230. struct inode *inode = container_of(head, struct inode, i_rcu);
  231. kmem_cache_free(inode_cachep, inode);
  232. }
  233. static void destroy_inode(struct inode *inode)
  234. {
  235. BUG_ON(!list_empty(&inode->i_lru));
  236. __destroy_inode(inode);
  237. if (inode->i_sb->s_op->destroy_inode)
  238. inode->i_sb->s_op->destroy_inode(inode);
  239. else
  240. call_rcu(&inode->i_rcu, i_callback);
  241. }
  242. /**
  243. * drop_nlink - directly drop an inode's link count
  244. * @inode: inode
  245. *
  246. * This is a low-level filesystem helper to replace any
  247. * direct filesystem manipulation of i_nlink. In cases
  248. * where we are attempting to track writes to the
  249. * filesystem, a decrement to zero means an imminent
  250. * write when the file is truncated and actually unlinked
  251. * on the filesystem.
  252. */
  253. void drop_nlink(struct inode *inode)
  254. {
  255. WARN_ON(inode->i_nlink == 0);
  256. inode->__i_nlink--;
  257. if (!inode->i_nlink)
  258. atomic_long_inc(&inode->i_sb->s_remove_count);
  259. }
  260. EXPORT_SYMBOL(drop_nlink);
  261. /**
  262. * clear_nlink - directly zero an inode's link count
  263. * @inode: inode
  264. *
  265. * This is a low-level filesystem helper to replace any
  266. * direct filesystem manipulation of i_nlink. See
  267. * drop_nlink() for why we care about i_nlink hitting zero.
  268. */
  269. void clear_nlink(struct inode *inode)
  270. {
  271. if (inode->i_nlink) {
  272. inode->__i_nlink = 0;
  273. atomic_long_inc(&inode->i_sb->s_remove_count);
  274. }
  275. }
  276. EXPORT_SYMBOL(clear_nlink);
  277. /**
  278. * set_nlink - directly set an inode's link count
  279. * @inode: inode
  280. * @nlink: new nlink (should be non-zero)
  281. *
  282. * This is a low-level filesystem helper to replace any
  283. * direct filesystem manipulation of i_nlink.
  284. */
  285. void set_nlink(struct inode *inode, unsigned int nlink)
  286. {
  287. if (!nlink) {
  288. clear_nlink(inode);
  289. } else {
  290. /* Yes, some filesystems do change nlink from zero to one */
  291. if (inode->i_nlink == 0)
  292. atomic_long_dec(&inode->i_sb->s_remove_count);
  293. inode->__i_nlink = nlink;
  294. }
  295. }
  296. EXPORT_SYMBOL(set_nlink);
  297. /**
  298. * inc_nlink - directly increment an inode's link count
  299. * @inode: inode
  300. *
  301. * This is a low-level filesystem helper to replace any
  302. * direct filesystem manipulation of i_nlink. Currently,
  303. * it is only here for parity with dec_nlink().
  304. */
  305. void inc_nlink(struct inode *inode)
  306. {
  307. if (unlikely(inode->i_nlink == 0)) {
  308. WARN_ON(!(inode->i_state & I_LINKABLE));
  309. atomic_long_dec(&inode->i_sb->s_remove_count);
  310. }
  311. inode->__i_nlink++;
  312. }
  313. EXPORT_SYMBOL(inc_nlink);
  314. static void __address_space_init_once(struct address_space *mapping)
  315. {
  316. INIT_RADIX_TREE(&mapping->i_pages, GFP_ATOMIC | __GFP_ACCOUNT);
  317. init_rwsem(&mapping->i_mmap_rwsem);
  318. INIT_LIST_HEAD(&mapping->private_list);
  319. spin_lock_init(&mapping->private_lock);
  320. mapping->i_mmap = RB_ROOT_CACHED;
  321. }
  322. void address_space_init_once(struct address_space *mapping)
  323. {
  324. memset(mapping, 0, sizeof(*mapping));
  325. __address_space_init_once(mapping);
  326. }
  327. EXPORT_SYMBOL(address_space_init_once);
  328. /*
  329. * These are initializations that only need to be done
  330. * once, because the fields are idempotent across use
  331. * of the inode, so let the slab aware of that.
  332. */
  333. void inode_init_once(struct inode *inode)
  334. {
  335. memset(inode, 0, sizeof(*inode));
  336. INIT_HLIST_NODE(&inode->i_hash);
  337. INIT_LIST_HEAD(&inode->i_devices);
  338. INIT_LIST_HEAD(&inode->i_io_list);
  339. INIT_LIST_HEAD(&inode->i_wb_list);
  340. INIT_LIST_HEAD(&inode->i_lru);
  341. __address_space_init_once(&inode->i_data);
  342. i_size_ordered_init(inode);
  343. }
  344. EXPORT_SYMBOL(inode_init_once);
  345. static void init_once(void *foo)
  346. {
  347. struct inode *inode = (struct inode *) foo;
  348. inode_init_once(inode);
  349. }
  350. /*
  351. * inode->i_lock must be held
  352. */
  353. void __iget(struct inode *inode)
  354. {
  355. atomic_inc(&inode->i_count);
  356. }
  357. /*
  358. * get additional reference to inode; caller must already hold one.
  359. */
  360. void ihold(struct inode *inode)
  361. {
  362. WARN_ON(atomic_inc_return(&inode->i_count) < 2);
  363. }
  364. EXPORT_SYMBOL(ihold);
  365. static void inode_lru_list_add(struct inode *inode)
  366. {
  367. if (list_lru_add(&inode->i_sb->s_inode_lru, &inode->i_lru))
  368. this_cpu_inc(nr_unused);
  369. else
  370. inode->i_state |= I_REFERENCED;
  371. }
  372. /*
  373. * Add inode to LRU if needed (inode is unused and clean).
  374. *
  375. * Needs inode->i_lock held.
  376. */
  377. void inode_add_lru(struct inode *inode)
  378. {
  379. if (!(inode->i_state & (I_DIRTY_ALL | I_SYNC |
  380. I_FREEING | I_WILL_FREE)) &&
  381. !atomic_read(&inode->i_count) && inode->i_sb->s_flags & SB_ACTIVE)
  382. inode_lru_list_add(inode);
  383. }
  384. static void inode_lru_list_del(struct inode *inode)
  385. {
  386. if (list_lru_del(&inode->i_sb->s_inode_lru, &inode->i_lru))
  387. this_cpu_dec(nr_unused);
  388. }
  389. /**
  390. * inode_sb_list_add - add inode to the superblock list of inodes
  391. * @inode: inode to add
  392. */
  393. void inode_sb_list_add(struct inode *inode)
  394. {
  395. spin_lock(&inode->i_sb->s_inode_list_lock);
  396. list_add(&inode->i_sb_list, &inode->i_sb->s_inodes);
  397. spin_unlock(&inode->i_sb->s_inode_list_lock);
  398. }
  399. EXPORT_SYMBOL_GPL(inode_sb_list_add);
  400. static inline void inode_sb_list_del(struct inode *inode)
  401. {
  402. if (!list_empty(&inode->i_sb_list)) {
  403. spin_lock(&inode->i_sb->s_inode_list_lock);
  404. list_del_init(&inode->i_sb_list);
  405. spin_unlock(&inode->i_sb->s_inode_list_lock);
  406. }
  407. }
  408. static unsigned long hash(struct super_block *sb, unsigned long hashval)
  409. {
  410. unsigned long tmp;
  411. tmp = (hashval * (unsigned long)sb) ^ (GOLDEN_RATIO_PRIME + hashval) /
  412. L1_CACHE_BYTES;
  413. tmp = tmp ^ ((tmp ^ GOLDEN_RATIO_PRIME) >> i_hash_shift);
  414. return tmp & i_hash_mask;
  415. }
  416. /**
  417. * __insert_inode_hash - hash an inode
  418. * @inode: unhashed inode
  419. * @hashval: unsigned long value used to locate this object in the
  420. * inode_hashtable.
  421. *
  422. * Add an inode to the inode hash for this superblock.
  423. */
  424. void __insert_inode_hash(struct inode *inode, unsigned long hashval)
  425. {
  426. struct hlist_head *b = inode_hashtable + hash(inode->i_sb, hashval);
  427. spin_lock(&inode_hash_lock);
  428. spin_lock(&inode->i_lock);
  429. hlist_add_head(&inode->i_hash, b);
  430. spin_unlock(&inode->i_lock);
  431. spin_unlock(&inode_hash_lock);
  432. }
  433. EXPORT_SYMBOL(__insert_inode_hash);
  434. /**
  435. * __remove_inode_hash - remove an inode from the hash
  436. * @inode: inode to unhash
  437. *
  438. * Remove an inode from the superblock.
  439. */
  440. void __remove_inode_hash(struct inode *inode)
  441. {
  442. spin_lock(&inode_hash_lock);
  443. spin_lock(&inode->i_lock);
  444. hlist_del_init(&inode->i_hash);
  445. spin_unlock(&inode->i_lock);
  446. spin_unlock(&inode_hash_lock);
  447. }
  448. EXPORT_SYMBOL(__remove_inode_hash);
  449. void clear_inode(struct inode *inode)
  450. {
  451. /*
  452. * We have to cycle the i_pages lock here because reclaim can be in the
  453. * process of removing the last page (in __delete_from_page_cache())
  454. * and we must not free the mapping under it.
  455. */
  456. xa_lock_irq(&inode->i_data.i_pages);
  457. BUG_ON(inode->i_data.nrpages);
  458. BUG_ON(inode->i_data.nrexceptional);
  459. xa_unlock_irq(&inode->i_data.i_pages);
  460. BUG_ON(!list_empty(&inode->i_data.private_list));
  461. BUG_ON(!(inode->i_state & I_FREEING));
  462. BUG_ON(inode->i_state & I_CLEAR);
  463. BUG_ON(!list_empty(&inode->i_wb_list));
  464. /* don't need i_lock here, no concurrent mods to i_state */
  465. inode->i_state = I_FREEING | I_CLEAR;
  466. }
  467. EXPORT_SYMBOL(clear_inode);
  468. /*
  469. * Free the inode passed in, removing it from the lists it is still connected
  470. * to. We remove any pages still attached to the inode and wait for any IO that
  471. * is still in progress before finally destroying the inode.
  472. *
  473. * An inode must already be marked I_FREEING so that we avoid the inode being
  474. * moved back onto lists if we race with other code that manipulates the lists
  475. * (e.g. writeback_single_inode). The caller is responsible for setting this.
  476. *
  477. * An inode must already be removed from the LRU list before being evicted from
  478. * the cache. This should occur atomically with setting the I_FREEING state
  479. * flag, so no inodes here should ever be on the LRU when being evicted.
  480. */
  481. static void evict(struct inode *inode)
  482. {
  483. const struct super_operations *op = inode->i_sb->s_op;
  484. BUG_ON(!(inode->i_state & I_FREEING));
  485. BUG_ON(!list_empty(&inode->i_lru));
  486. if (!list_empty(&inode->i_io_list))
  487. inode_io_list_del(inode);
  488. inode_sb_list_del(inode);
  489. /*
  490. * Wait for flusher thread to be done with the inode so that filesystem
  491. * does not start destroying it while writeback is still running. Since
  492. * the inode has I_FREEING set, flusher thread won't start new work on
  493. * the inode. We just have to wait for running writeback to finish.
  494. */
  495. inode_wait_for_writeback(inode);
  496. if (op->evict_inode) {
  497. op->evict_inode(inode);
  498. } else {
  499. truncate_inode_pages_final(&inode->i_data);
  500. clear_inode(inode);
  501. }
  502. if (S_ISBLK(inode->i_mode) && inode->i_bdev)
  503. bd_forget(inode);
  504. if (S_ISCHR(inode->i_mode) && inode->i_cdev)
  505. cd_forget(inode);
  506. remove_inode_hash(inode);
  507. spin_lock(&inode->i_lock);
  508. wake_up_bit(&inode->i_state, __I_NEW);
  509. BUG_ON(inode->i_state != (I_FREEING | I_CLEAR));
  510. spin_unlock(&inode->i_lock);
  511. destroy_inode(inode);
  512. }
  513. /*
  514. * dispose_list - dispose of the contents of a local list
  515. * @head: the head of the list to free
  516. *
  517. * Dispose-list gets a local list with local inodes in it, so it doesn't
  518. * need to worry about list corruption and SMP locks.
  519. */
  520. static void dispose_list(struct list_head *head)
  521. {
  522. while (!list_empty(head)) {
  523. struct inode *inode;
  524. inode = list_first_entry(head, struct inode, i_lru);
  525. list_del_init(&inode->i_lru);
  526. evict(inode);
  527. cond_resched();
  528. }
  529. }
  530. /**
  531. * evict_inodes - evict all evictable inodes for a superblock
  532. * @sb: superblock to operate on
  533. *
  534. * Make sure that no inodes with zero refcount are retained. This is
  535. * called by superblock shutdown after having SB_ACTIVE flag removed,
  536. * so any inode reaching zero refcount during or after that call will
  537. * be immediately evicted.
  538. */
  539. void evict_inodes(struct super_block *sb)
  540. {
  541. struct inode *inode, *next;
  542. LIST_HEAD(dispose);
  543. again:
  544. spin_lock(&sb->s_inode_list_lock);
  545. list_for_each_entry_safe(inode, next, &sb->s_inodes, i_sb_list) {
  546. if (atomic_read(&inode->i_count))
  547. continue;
  548. spin_lock(&inode->i_lock);
  549. if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) {
  550. spin_unlock(&inode->i_lock);
  551. continue;
  552. }
  553. inode->i_state |= I_FREEING;
  554. inode_lru_list_del(inode);
  555. spin_unlock(&inode->i_lock);
  556. list_add(&inode->i_lru, &dispose);
  557. /*
  558. * We can have a ton of inodes to evict at unmount time given
  559. * enough memory, check to see if we need to go to sleep for a
  560. * bit so we don't livelock.
  561. */
  562. if (need_resched()) {
  563. spin_unlock(&sb->s_inode_list_lock);
  564. cond_resched();
  565. dispose_list(&dispose);
  566. goto again;
  567. }
  568. }
  569. spin_unlock(&sb->s_inode_list_lock);
  570. dispose_list(&dispose);
  571. }
  572. EXPORT_SYMBOL_GPL(evict_inodes);
  573. /**
  574. * invalidate_inodes - attempt to free all inodes on a superblock
  575. * @sb: superblock to operate on
  576. * @kill_dirty: flag to guide handling of dirty inodes
  577. *
  578. * Attempts to free all inodes for a given superblock. If there were any
  579. * busy inodes return a non-zero value, else zero.
  580. * If @kill_dirty is set, discard dirty inodes too, otherwise treat
  581. * them as busy.
  582. */
  583. int invalidate_inodes(struct super_block *sb, bool kill_dirty)
  584. {
  585. int busy = 0;
  586. struct inode *inode, *next;
  587. LIST_HEAD(dispose);
  588. again:
  589. spin_lock(&sb->s_inode_list_lock);
  590. list_for_each_entry_safe(inode, next, &sb->s_inodes, i_sb_list) {
  591. spin_lock(&inode->i_lock);
  592. if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) {
  593. spin_unlock(&inode->i_lock);
  594. continue;
  595. }
  596. if (inode->i_state & I_DIRTY_ALL && !kill_dirty) {
  597. spin_unlock(&inode->i_lock);
  598. busy = 1;
  599. continue;
  600. }
  601. if (atomic_read(&inode->i_count)) {
  602. spin_unlock(&inode->i_lock);
  603. busy = 1;
  604. continue;
  605. }
  606. inode->i_state |= I_FREEING;
  607. inode_lru_list_del(inode);
  608. spin_unlock(&inode->i_lock);
  609. list_add(&inode->i_lru, &dispose);
  610. if (need_resched()) {
  611. spin_unlock(&sb->s_inode_list_lock);
  612. cond_resched();
  613. dispose_list(&dispose);
  614. goto again;
  615. }
  616. }
  617. spin_unlock(&sb->s_inode_list_lock);
  618. dispose_list(&dispose);
  619. return busy;
  620. }
  621. /*
  622. * Isolate the inode from the LRU in preparation for freeing it.
  623. *
  624. * Any inodes which are pinned purely because of attached pagecache have their
  625. * pagecache removed. If the inode has metadata buffers attached to
  626. * mapping->private_list then try to remove them.
  627. *
  628. * If the inode has the I_REFERENCED flag set, then it means that it has been
  629. * used recently - the flag is set in iput_final(). When we encounter such an
  630. * inode, clear the flag and move it to the back of the LRU so it gets another
  631. * pass through the LRU before it gets reclaimed. This is necessary because of
  632. * the fact we are doing lazy LRU updates to minimise lock contention so the
  633. * LRU does not have strict ordering. Hence we don't want to reclaim inodes
  634. * with this flag set because they are the inodes that are out of order.
  635. */
  636. static enum lru_status inode_lru_isolate(struct list_head *item,
  637. struct list_lru_one *lru, spinlock_t *lru_lock, void *arg)
  638. {
  639. struct list_head *freeable = arg;
  640. struct inode *inode = container_of(item, struct inode, i_lru);
  641. /*
  642. * we are inverting the lru lock/inode->i_lock here, so use a trylock.
  643. * If we fail to get the lock, just skip it.
  644. */
  645. if (!spin_trylock(&inode->i_lock))
  646. return LRU_SKIP;
  647. /*
  648. * Referenced or dirty inodes are still in use. Give them another pass
  649. * through the LRU as we canot reclaim them now.
  650. */
  651. if (atomic_read(&inode->i_count) ||
  652. (inode->i_state & ~I_REFERENCED)) {
  653. list_lru_isolate(lru, &inode->i_lru);
  654. spin_unlock(&inode->i_lock);
  655. this_cpu_dec(nr_unused);
  656. return LRU_REMOVED;
  657. }
  658. /* recently referenced inodes get one more pass */
  659. if (inode->i_state & I_REFERENCED) {
  660. inode->i_state &= ~I_REFERENCED;
  661. spin_unlock(&inode->i_lock);
  662. return LRU_ROTATE;
  663. }
  664. if (inode_has_buffers(inode) || inode->i_data.nrpages) {
  665. __iget(inode);
  666. spin_unlock(&inode->i_lock);
  667. spin_unlock(lru_lock);
  668. if (remove_inode_buffers(inode)) {
  669. unsigned long reap;
  670. reap = invalidate_mapping_pages(&inode->i_data, 0, -1);
  671. if (current_is_kswapd())
  672. __count_vm_events(KSWAPD_INODESTEAL, reap);
  673. else
  674. __count_vm_events(PGINODESTEAL, reap);
  675. if (current->reclaim_state)
  676. current->reclaim_state->reclaimed_slab += reap;
  677. }
  678. iput(inode);
  679. spin_lock(lru_lock);
  680. return LRU_RETRY;
  681. }
  682. WARN_ON(inode->i_state & I_NEW);
  683. inode->i_state |= I_FREEING;
  684. list_lru_isolate_move(lru, &inode->i_lru, freeable);
  685. spin_unlock(&inode->i_lock);
  686. this_cpu_dec(nr_unused);
  687. return LRU_REMOVED;
  688. }
  689. /*
  690. * Walk the superblock inode LRU for freeable inodes and attempt to free them.
  691. * This is called from the superblock shrinker function with a number of inodes
  692. * to trim from the LRU. Inodes to be freed are moved to a temporary list and
  693. * then are freed outside inode_lock by dispose_list().
  694. */
  695. long prune_icache_sb(struct super_block *sb, struct shrink_control *sc)
  696. {
  697. LIST_HEAD(freeable);
  698. long freed;
  699. freed = list_lru_shrink_walk(&sb->s_inode_lru, sc,
  700. inode_lru_isolate, &freeable);
  701. dispose_list(&freeable);
  702. return freed;
  703. }
  704. static void __wait_on_freeing_inode(struct inode *inode);
  705. /*
  706. * Called with the inode lock held.
  707. */
  708. static struct inode *find_inode(struct super_block *sb,
  709. struct hlist_head *head,
  710. int (*test)(struct inode *, void *),
  711. void *data)
  712. {
  713. struct inode *inode = NULL;
  714. repeat:
  715. hlist_for_each_entry(inode, head, i_hash) {
  716. if (inode->i_sb != sb)
  717. continue;
  718. if (!test(inode, data))
  719. continue;
  720. spin_lock(&inode->i_lock);
  721. if (inode->i_state & (I_FREEING|I_WILL_FREE)) {
  722. __wait_on_freeing_inode(inode);
  723. goto repeat;
  724. }
  725. if (unlikely(inode->i_state & I_CREATING)) {
  726. spin_unlock(&inode->i_lock);
  727. return ERR_PTR(-ESTALE);
  728. }
  729. __iget(inode);
  730. spin_unlock(&inode->i_lock);
  731. return inode;
  732. }
  733. return NULL;
  734. }
  735. /*
  736. * find_inode_fast is the fast path version of find_inode, see the comment at
  737. * iget_locked for details.
  738. */
  739. static struct inode *find_inode_fast(struct super_block *sb,
  740. struct hlist_head *head, unsigned long ino)
  741. {
  742. struct inode *inode = NULL;
  743. repeat:
  744. hlist_for_each_entry(inode, head, i_hash) {
  745. if (inode->i_ino != ino)
  746. continue;
  747. if (inode->i_sb != sb)
  748. continue;
  749. spin_lock(&inode->i_lock);
  750. if (inode->i_state & (I_FREEING|I_WILL_FREE)) {
  751. __wait_on_freeing_inode(inode);
  752. goto repeat;
  753. }
  754. if (unlikely(inode->i_state & I_CREATING)) {
  755. spin_unlock(&inode->i_lock);
  756. return ERR_PTR(-ESTALE);
  757. }
  758. __iget(inode);
  759. spin_unlock(&inode->i_lock);
  760. return inode;
  761. }
  762. return NULL;
  763. }
  764. /*
  765. * Each cpu owns a range of LAST_INO_BATCH numbers.
  766. * 'shared_last_ino' is dirtied only once out of LAST_INO_BATCH allocations,
  767. * to renew the exhausted range.
  768. *
  769. * This does not significantly increase overflow rate because every CPU can
  770. * consume at most LAST_INO_BATCH-1 unused inode numbers. So there is
  771. * NR_CPUS*(LAST_INO_BATCH-1) wastage. At 4096 and 1024, this is ~0.1% of the
  772. * 2^32 range, and is a worst-case. Even a 50% wastage would only increase
  773. * overflow rate by 2x, which does not seem too significant.
  774. *
  775. * On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW
  776. * error if st_ino won't fit in target struct field. Use 32bit counter
  777. * here to attempt to avoid that.
  778. */
  779. #define LAST_INO_BATCH 1024
  780. static DEFINE_PER_CPU(unsigned int, last_ino);
  781. unsigned int get_next_ino(void)
  782. {
  783. unsigned int *p = &get_cpu_var(last_ino);
  784. unsigned int res = *p;
  785. #ifdef CONFIG_SMP
  786. if (unlikely((res & (LAST_INO_BATCH-1)) == 0)) {
  787. static atomic_t shared_last_ino;
  788. int next = atomic_add_return(LAST_INO_BATCH, &shared_last_ino);
  789. res = next - LAST_INO_BATCH;
  790. }
  791. #endif
  792. res++;
  793. /* get_next_ino should not provide a 0 inode number */
  794. if (unlikely(!res))
  795. res++;
  796. *p = res;
  797. put_cpu_var(last_ino);
  798. return res;
  799. }
  800. EXPORT_SYMBOL(get_next_ino);
  801. /**
  802. * new_inode_pseudo - obtain an inode
  803. * @sb: superblock
  804. *
  805. * Allocates a new inode for given superblock.
  806. * Inode wont be chained in superblock s_inodes list
  807. * This means :
  808. * - fs can't be unmount
  809. * - quotas, fsnotify, writeback can't work
  810. */
  811. struct inode *new_inode_pseudo(struct super_block *sb)
  812. {
  813. struct inode *inode = alloc_inode(sb);
  814. if (inode) {
  815. spin_lock(&inode->i_lock);
  816. inode->i_state = 0;
  817. spin_unlock(&inode->i_lock);
  818. INIT_LIST_HEAD(&inode->i_sb_list);
  819. }
  820. return inode;
  821. }
  822. /**
  823. * new_inode - obtain an inode
  824. * @sb: superblock
  825. *
  826. * Allocates a new inode for given superblock. The default gfp_mask
  827. * for allocations related to inode->i_mapping is GFP_HIGHUSER_MOVABLE.
  828. * If HIGHMEM pages are unsuitable or it is known that pages allocated
  829. * for the page cache are not reclaimable or migratable,
  830. * mapping_set_gfp_mask() must be called with suitable flags on the
  831. * newly created inode's mapping
  832. *
  833. */
  834. struct inode *new_inode(struct super_block *sb)
  835. {
  836. struct inode *inode;
  837. spin_lock_prefetch(&sb->s_inode_list_lock);
  838. inode = new_inode_pseudo(sb);
  839. if (inode)
  840. inode_sb_list_add(inode);
  841. return inode;
  842. }
  843. EXPORT_SYMBOL(new_inode);
  844. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  845. void lockdep_annotate_inode_mutex_key(struct inode *inode)
  846. {
  847. if (S_ISDIR(inode->i_mode)) {
  848. struct file_system_type *type = inode->i_sb->s_type;
  849. /* Set new key only if filesystem hasn't already changed it */
  850. if (lockdep_match_class(&inode->i_rwsem, &type->i_mutex_key)) {
  851. /*
  852. * ensure nobody is actually holding i_mutex
  853. */
  854. // mutex_destroy(&inode->i_mutex);
  855. init_rwsem(&inode->i_rwsem);
  856. lockdep_set_class(&inode->i_rwsem,
  857. &type->i_mutex_dir_key);
  858. }
  859. }
  860. }
  861. EXPORT_SYMBOL(lockdep_annotate_inode_mutex_key);
  862. #endif
  863. /**
  864. * unlock_new_inode - clear the I_NEW state and wake up any waiters
  865. * @inode: new inode to unlock
  866. *
  867. * Called when the inode is fully initialised to clear the new state of the
  868. * inode and wake up anyone waiting for the inode to finish initialisation.
  869. */
  870. void unlock_new_inode(struct inode *inode)
  871. {
  872. lockdep_annotate_inode_mutex_key(inode);
  873. spin_lock(&inode->i_lock);
  874. WARN_ON(!(inode->i_state & I_NEW));
  875. inode->i_state &= ~I_NEW & ~I_CREATING;
  876. smp_mb();
  877. wake_up_bit(&inode->i_state, __I_NEW);
  878. spin_unlock(&inode->i_lock);
  879. }
  880. EXPORT_SYMBOL(unlock_new_inode);
  881. void discard_new_inode(struct inode *inode)
  882. {
  883. lockdep_annotate_inode_mutex_key(inode);
  884. spin_lock(&inode->i_lock);
  885. WARN_ON(!(inode->i_state & I_NEW));
  886. inode->i_state &= ~I_NEW;
  887. smp_mb();
  888. wake_up_bit(&inode->i_state, __I_NEW);
  889. spin_unlock(&inode->i_lock);
  890. iput(inode);
  891. }
  892. EXPORT_SYMBOL(discard_new_inode);
  893. /**
  894. * lock_two_nondirectories - take two i_mutexes on non-directory objects
  895. *
  896. * Lock any non-NULL argument that is not a directory.
  897. * Zero, one or two objects may be locked by this function.
  898. *
  899. * @inode1: first inode to lock
  900. * @inode2: second inode to lock
  901. */
  902. void lock_two_nondirectories(struct inode *inode1, struct inode *inode2)
  903. {
  904. if (inode1 > inode2)
  905. swap(inode1, inode2);
  906. if (inode1 && !S_ISDIR(inode1->i_mode))
  907. inode_lock(inode1);
  908. if (inode2 && !S_ISDIR(inode2->i_mode) && inode2 != inode1)
  909. inode_lock_nested(inode2, I_MUTEX_NONDIR2);
  910. }
  911. EXPORT_SYMBOL(lock_two_nondirectories);
  912. /**
  913. * unlock_two_nondirectories - release locks from lock_two_nondirectories()
  914. * @inode1: first inode to unlock
  915. * @inode2: second inode to unlock
  916. */
  917. void unlock_two_nondirectories(struct inode *inode1, struct inode *inode2)
  918. {
  919. if (inode1 && !S_ISDIR(inode1->i_mode))
  920. inode_unlock(inode1);
  921. if (inode2 && !S_ISDIR(inode2->i_mode) && inode2 != inode1)
  922. inode_unlock(inode2);
  923. }
  924. EXPORT_SYMBOL(unlock_two_nondirectories);
  925. /**
  926. * inode_insert5 - obtain an inode from a mounted file system
  927. * @inode: pre-allocated inode to use for insert to cache
  928. * @hashval: hash value (usually inode number) to get
  929. * @test: callback used for comparisons between inodes
  930. * @set: callback used to initialize a new struct inode
  931. * @data: opaque data pointer to pass to @test and @set
  932. *
  933. * Search for the inode specified by @hashval and @data in the inode cache,
  934. * and if present it is return it with an increased reference count. This is
  935. * a variant of iget5_locked() for callers that don't want to fail on memory
  936. * allocation of inode.
  937. *
  938. * If the inode is not in cache, insert the pre-allocated inode to cache and
  939. * return it locked, hashed, and with the I_NEW flag set. The file system gets
  940. * to fill it in before unlocking it via unlock_new_inode().
  941. *
  942. * Note both @test and @set are called with the inode_hash_lock held, so can't
  943. * sleep.
  944. */
  945. struct inode *inode_insert5(struct inode *inode, unsigned long hashval,
  946. int (*test)(struct inode *, void *),
  947. int (*set)(struct inode *, void *), void *data)
  948. {
  949. struct hlist_head *head = inode_hashtable + hash(inode->i_sb, hashval);
  950. struct inode *old;
  951. bool creating = inode->i_state & I_CREATING;
  952. again:
  953. spin_lock(&inode_hash_lock);
  954. old = find_inode(inode->i_sb, head, test, data);
  955. if (unlikely(old)) {
  956. /*
  957. * Uhhuh, somebody else created the same inode under us.
  958. * Use the old inode instead of the preallocated one.
  959. */
  960. spin_unlock(&inode_hash_lock);
  961. if (IS_ERR(old))
  962. return NULL;
  963. wait_on_inode(old);
  964. if (unlikely(inode_unhashed(old))) {
  965. iput(old);
  966. goto again;
  967. }
  968. return old;
  969. }
  970. if (set && unlikely(set(inode, data))) {
  971. inode = NULL;
  972. goto unlock;
  973. }
  974. /*
  975. * Return the locked inode with I_NEW set, the
  976. * caller is responsible for filling in the contents
  977. */
  978. spin_lock(&inode->i_lock);
  979. inode->i_state |= I_NEW;
  980. hlist_add_head(&inode->i_hash, head);
  981. spin_unlock(&inode->i_lock);
  982. if (!creating)
  983. inode_sb_list_add(inode);
  984. unlock:
  985. spin_unlock(&inode_hash_lock);
  986. return inode;
  987. }
  988. EXPORT_SYMBOL(inode_insert5);
  989. /**
  990. * iget5_locked - obtain an inode from a mounted file system
  991. * @sb: super block of file system
  992. * @hashval: hash value (usually inode number) to get
  993. * @test: callback used for comparisons between inodes
  994. * @set: callback used to initialize a new struct inode
  995. * @data: opaque data pointer to pass to @test and @set
  996. *
  997. * Search for the inode specified by @hashval and @data in the inode cache,
  998. * and if present it is return it with an increased reference count. This is
  999. * a generalized version of iget_locked() for file systems where the inode
  1000. * number is not sufficient for unique identification of an inode.
  1001. *
  1002. * If the inode is not in cache, allocate a new inode and return it locked,
  1003. * hashed, and with the I_NEW flag set. The file system gets to fill it in
  1004. * before unlocking it via unlock_new_inode().
  1005. *
  1006. * Note both @test and @set are called with the inode_hash_lock held, so can't
  1007. * sleep.
  1008. */
  1009. struct inode *iget5_locked(struct super_block *sb, unsigned long hashval,
  1010. int (*test)(struct inode *, void *),
  1011. int (*set)(struct inode *, void *), void *data)
  1012. {
  1013. struct inode *inode = ilookup5(sb, hashval, test, data);
  1014. if (!inode) {
  1015. struct inode *new = alloc_inode(sb);
  1016. if (new) {
  1017. new->i_state = 0;
  1018. inode = inode_insert5(new, hashval, test, set, data);
  1019. if (unlikely(inode != new))
  1020. destroy_inode(new);
  1021. }
  1022. }
  1023. return inode;
  1024. }
  1025. EXPORT_SYMBOL(iget5_locked);
  1026. /**
  1027. * iget_locked - obtain an inode from a mounted file system
  1028. * @sb: super block of file system
  1029. * @ino: inode number to get
  1030. *
  1031. * Search for the inode specified by @ino in the inode cache and if present
  1032. * return it with an increased reference count. This is for file systems
  1033. * where the inode number is sufficient for unique identification of an inode.
  1034. *
  1035. * If the inode is not in cache, allocate a new inode and return it locked,
  1036. * hashed, and with the I_NEW flag set. The file system gets to fill it in
  1037. * before unlocking it via unlock_new_inode().
  1038. */
  1039. struct inode *iget_locked(struct super_block *sb, unsigned long ino)
  1040. {
  1041. struct hlist_head *head = inode_hashtable + hash(sb, ino);
  1042. struct inode *inode;
  1043. again:
  1044. spin_lock(&inode_hash_lock);
  1045. inode = find_inode_fast(sb, head, ino);
  1046. spin_unlock(&inode_hash_lock);
  1047. if (inode) {
  1048. if (IS_ERR(inode))
  1049. return NULL;
  1050. wait_on_inode(inode);
  1051. if (unlikely(inode_unhashed(inode))) {
  1052. iput(inode);
  1053. goto again;
  1054. }
  1055. return inode;
  1056. }
  1057. inode = alloc_inode(sb);
  1058. if (inode) {
  1059. struct inode *old;
  1060. spin_lock(&inode_hash_lock);
  1061. /* We released the lock, so.. */
  1062. old = find_inode_fast(sb, head, ino);
  1063. if (!old) {
  1064. inode->i_ino = ino;
  1065. spin_lock(&inode->i_lock);
  1066. inode->i_state = I_NEW;
  1067. hlist_add_head(&inode->i_hash, head);
  1068. spin_unlock(&inode->i_lock);
  1069. inode_sb_list_add(inode);
  1070. spin_unlock(&inode_hash_lock);
  1071. /* Return the locked inode with I_NEW set, the
  1072. * caller is responsible for filling in the contents
  1073. */
  1074. return inode;
  1075. }
  1076. /*
  1077. * Uhhuh, somebody else created the same inode under
  1078. * us. Use the old inode instead of the one we just
  1079. * allocated.
  1080. */
  1081. spin_unlock(&inode_hash_lock);
  1082. destroy_inode(inode);
  1083. if (IS_ERR(old))
  1084. return NULL;
  1085. inode = old;
  1086. wait_on_inode(inode);
  1087. if (unlikely(inode_unhashed(inode))) {
  1088. iput(inode);
  1089. goto again;
  1090. }
  1091. }
  1092. return inode;
  1093. }
  1094. EXPORT_SYMBOL(iget_locked);
  1095. /*
  1096. * search the inode cache for a matching inode number.
  1097. * If we find one, then the inode number we are trying to
  1098. * allocate is not unique and so we should not use it.
  1099. *
  1100. * Returns 1 if the inode number is unique, 0 if it is not.
  1101. */
  1102. static int test_inode_iunique(struct super_block *sb, unsigned long ino)
  1103. {
  1104. struct hlist_head *b = inode_hashtable + hash(sb, ino);
  1105. struct inode *inode;
  1106. spin_lock(&inode_hash_lock);
  1107. hlist_for_each_entry(inode, b, i_hash) {
  1108. if (inode->i_ino == ino && inode->i_sb == sb) {
  1109. spin_unlock(&inode_hash_lock);
  1110. return 0;
  1111. }
  1112. }
  1113. spin_unlock(&inode_hash_lock);
  1114. return 1;
  1115. }
  1116. /**
  1117. * iunique - get a unique inode number
  1118. * @sb: superblock
  1119. * @max_reserved: highest reserved inode number
  1120. *
  1121. * Obtain an inode number that is unique on the system for a given
  1122. * superblock. This is used by file systems that have no natural
  1123. * permanent inode numbering system. An inode number is returned that
  1124. * is higher than the reserved limit but unique.
  1125. *
  1126. * BUGS:
  1127. * With a large number of inodes live on the file system this function
  1128. * currently becomes quite slow.
  1129. */
  1130. ino_t iunique(struct super_block *sb, ino_t max_reserved)
  1131. {
  1132. /*
  1133. * On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW
  1134. * error if st_ino won't fit in target struct field. Use 32bit counter
  1135. * here to attempt to avoid that.
  1136. */
  1137. static DEFINE_SPINLOCK(iunique_lock);
  1138. static unsigned int counter;
  1139. ino_t res;
  1140. spin_lock(&iunique_lock);
  1141. do {
  1142. if (counter <= max_reserved)
  1143. counter = max_reserved + 1;
  1144. res = counter++;
  1145. } while (!test_inode_iunique(sb, res));
  1146. spin_unlock(&iunique_lock);
  1147. return res;
  1148. }
  1149. EXPORT_SYMBOL(iunique);
  1150. struct inode *igrab(struct inode *inode)
  1151. {
  1152. spin_lock(&inode->i_lock);
  1153. if (!(inode->i_state & (I_FREEING|I_WILL_FREE))) {
  1154. __iget(inode);
  1155. spin_unlock(&inode->i_lock);
  1156. } else {
  1157. spin_unlock(&inode->i_lock);
  1158. /*
  1159. * Handle the case where s_op->clear_inode is not been
  1160. * called yet, and somebody is calling igrab
  1161. * while the inode is getting freed.
  1162. */
  1163. inode = NULL;
  1164. }
  1165. return inode;
  1166. }
  1167. EXPORT_SYMBOL(igrab);
  1168. /**
  1169. * ilookup5_nowait - search for an inode in the inode cache
  1170. * @sb: super block of file system to search
  1171. * @hashval: hash value (usually inode number) to search for
  1172. * @test: callback used for comparisons between inodes
  1173. * @data: opaque data pointer to pass to @test
  1174. *
  1175. * Search for the inode specified by @hashval and @data in the inode cache.
  1176. * If the inode is in the cache, the inode is returned with an incremented
  1177. * reference count.
  1178. *
  1179. * Note: I_NEW is not waited upon so you have to be very careful what you do
  1180. * with the returned inode. You probably should be using ilookup5() instead.
  1181. *
  1182. * Note2: @test is called with the inode_hash_lock held, so can't sleep.
  1183. */
  1184. struct inode *ilookup5_nowait(struct super_block *sb, unsigned long hashval,
  1185. int (*test)(struct inode *, void *), void *data)
  1186. {
  1187. struct hlist_head *head = inode_hashtable + hash(sb, hashval);
  1188. struct inode *inode;
  1189. spin_lock(&inode_hash_lock);
  1190. inode = find_inode(sb, head, test, data);
  1191. spin_unlock(&inode_hash_lock);
  1192. return IS_ERR(inode) ? NULL : inode;
  1193. }
  1194. EXPORT_SYMBOL(ilookup5_nowait);
  1195. /**
  1196. * ilookup5 - search for an inode in the inode cache
  1197. * @sb: super block of file system to search
  1198. * @hashval: hash value (usually inode number) to search for
  1199. * @test: callback used for comparisons between inodes
  1200. * @data: opaque data pointer to pass to @test
  1201. *
  1202. * Search for the inode specified by @hashval and @data in the inode cache,
  1203. * and if the inode is in the cache, return the inode with an incremented
  1204. * reference count. Waits on I_NEW before returning the inode.
  1205. * returned with an incremented reference count.
  1206. *
  1207. * This is a generalized version of ilookup() for file systems where the
  1208. * inode number is not sufficient for unique identification of an inode.
  1209. *
  1210. * Note: @test is called with the inode_hash_lock held, so can't sleep.
  1211. */
  1212. struct inode *ilookup5(struct super_block *sb, unsigned long hashval,
  1213. int (*test)(struct inode *, void *), void *data)
  1214. {
  1215. struct inode *inode;
  1216. again:
  1217. inode = ilookup5_nowait(sb, hashval, test, data);
  1218. if (inode) {
  1219. wait_on_inode(inode);
  1220. if (unlikely(inode_unhashed(inode))) {
  1221. iput(inode);
  1222. goto again;
  1223. }
  1224. }
  1225. return inode;
  1226. }
  1227. EXPORT_SYMBOL(ilookup5);
  1228. /**
  1229. * ilookup - search for an inode in the inode cache
  1230. * @sb: super block of file system to search
  1231. * @ino: inode number to search for
  1232. *
  1233. * Search for the inode @ino in the inode cache, and if the inode is in the
  1234. * cache, the inode is returned with an incremented reference count.
  1235. */
  1236. struct inode *ilookup(struct super_block *sb, unsigned long ino)
  1237. {
  1238. struct hlist_head *head = inode_hashtable + hash(sb, ino);
  1239. struct inode *inode;
  1240. again:
  1241. spin_lock(&inode_hash_lock);
  1242. inode = find_inode_fast(sb, head, ino);
  1243. spin_unlock(&inode_hash_lock);
  1244. if (inode) {
  1245. if (IS_ERR(inode))
  1246. return NULL;
  1247. wait_on_inode(inode);
  1248. if (unlikely(inode_unhashed(inode))) {
  1249. iput(inode);
  1250. goto again;
  1251. }
  1252. }
  1253. return inode;
  1254. }
  1255. EXPORT_SYMBOL(ilookup);
  1256. /**
  1257. * find_inode_nowait - find an inode in the inode cache
  1258. * @sb: super block of file system to search
  1259. * @hashval: hash value (usually inode number) to search for
  1260. * @match: callback used for comparisons between inodes
  1261. * @data: opaque data pointer to pass to @match
  1262. *
  1263. * Search for the inode specified by @hashval and @data in the inode
  1264. * cache, where the helper function @match will return 0 if the inode
  1265. * does not match, 1 if the inode does match, and -1 if the search
  1266. * should be stopped. The @match function must be responsible for
  1267. * taking the i_lock spin_lock and checking i_state for an inode being
  1268. * freed or being initialized, and incrementing the reference count
  1269. * before returning 1. It also must not sleep, since it is called with
  1270. * the inode_hash_lock spinlock held.
  1271. *
  1272. * This is a even more generalized version of ilookup5() when the
  1273. * function must never block --- find_inode() can block in
  1274. * __wait_on_freeing_inode() --- or when the caller can not increment
  1275. * the reference count because the resulting iput() might cause an
  1276. * inode eviction. The tradeoff is that the @match funtion must be
  1277. * very carefully implemented.
  1278. */
  1279. struct inode *find_inode_nowait(struct super_block *sb,
  1280. unsigned long hashval,
  1281. int (*match)(struct inode *, unsigned long,
  1282. void *),
  1283. void *data)
  1284. {
  1285. struct hlist_head *head = inode_hashtable + hash(sb, hashval);
  1286. struct inode *inode, *ret_inode = NULL;
  1287. int mval;
  1288. spin_lock(&inode_hash_lock);
  1289. hlist_for_each_entry(inode, head, i_hash) {
  1290. if (inode->i_sb != sb)
  1291. continue;
  1292. mval = match(inode, hashval, data);
  1293. if (mval == 0)
  1294. continue;
  1295. if (mval == 1)
  1296. ret_inode = inode;
  1297. goto out;
  1298. }
  1299. out:
  1300. spin_unlock(&inode_hash_lock);
  1301. return ret_inode;
  1302. }
  1303. EXPORT_SYMBOL(find_inode_nowait);
  1304. int insert_inode_locked(struct inode *inode)
  1305. {
  1306. struct super_block *sb = inode->i_sb;
  1307. ino_t ino = inode->i_ino;
  1308. struct hlist_head *head = inode_hashtable + hash(sb, ino);
  1309. while (1) {
  1310. struct inode *old = NULL;
  1311. spin_lock(&inode_hash_lock);
  1312. hlist_for_each_entry(old, head, i_hash) {
  1313. if (old->i_ino != ino)
  1314. continue;
  1315. if (old->i_sb != sb)
  1316. continue;
  1317. spin_lock(&old->i_lock);
  1318. if (old->i_state & (I_FREEING|I_WILL_FREE)) {
  1319. spin_unlock(&old->i_lock);
  1320. continue;
  1321. }
  1322. break;
  1323. }
  1324. if (likely(!old)) {
  1325. spin_lock(&inode->i_lock);
  1326. inode->i_state |= I_NEW | I_CREATING;
  1327. hlist_add_head(&inode->i_hash, head);
  1328. spin_unlock(&inode->i_lock);
  1329. spin_unlock(&inode_hash_lock);
  1330. return 0;
  1331. }
  1332. if (unlikely(old->i_state & I_CREATING)) {
  1333. spin_unlock(&old->i_lock);
  1334. spin_unlock(&inode_hash_lock);
  1335. return -EBUSY;
  1336. }
  1337. __iget(old);
  1338. spin_unlock(&old->i_lock);
  1339. spin_unlock(&inode_hash_lock);
  1340. wait_on_inode(old);
  1341. if (unlikely(!inode_unhashed(old))) {
  1342. iput(old);
  1343. return -EBUSY;
  1344. }
  1345. iput(old);
  1346. }
  1347. }
  1348. EXPORT_SYMBOL(insert_inode_locked);
  1349. int insert_inode_locked4(struct inode *inode, unsigned long hashval,
  1350. int (*test)(struct inode *, void *), void *data)
  1351. {
  1352. struct inode *old;
  1353. inode->i_state |= I_CREATING;
  1354. old = inode_insert5(inode, hashval, test, NULL, data);
  1355. if (old != inode) {
  1356. iput(old);
  1357. return -EBUSY;
  1358. }
  1359. return 0;
  1360. }
  1361. EXPORT_SYMBOL(insert_inode_locked4);
  1362. int generic_delete_inode(struct inode *inode)
  1363. {
  1364. return 1;
  1365. }
  1366. EXPORT_SYMBOL(generic_delete_inode);
  1367. /*
  1368. * Called when we're dropping the last reference
  1369. * to an inode.
  1370. *
  1371. * Call the FS "drop_inode()" function, defaulting to
  1372. * the legacy UNIX filesystem behaviour. If it tells
  1373. * us to evict inode, do so. Otherwise, retain inode
  1374. * in cache if fs is alive, sync and evict if fs is
  1375. * shutting down.
  1376. */
  1377. static void iput_final(struct inode *inode)
  1378. {
  1379. struct super_block *sb = inode->i_sb;
  1380. const struct super_operations *op = inode->i_sb->s_op;
  1381. int drop;
  1382. WARN_ON(inode->i_state & I_NEW);
  1383. if (op->drop_inode)
  1384. drop = op->drop_inode(inode);
  1385. else
  1386. drop = generic_drop_inode(inode);
  1387. if (!drop && (sb->s_flags & SB_ACTIVE)) {
  1388. inode_add_lru(inode);
  1389. spin_unlock(&inode->i_lock);
  1390. return;
  1391. }
  1392. if (!drop) {
  1393. inode->i_state |= I_WILL_FREE;
  1394. spin_unlock(&inode->i_lock);
  1395. write_inode_now(inode, 1);
  1396. spin_lock(&inode->i_lock);
  1397. WARN_ON(inode->i_state & I_NEW);
  1398. inode->i_state &= ~I_WILL_FREE;
  1399. }
  1400. inode->i_state |= I_FREEING;
  1401. if (!list_empty(&inode->i_lru))
  1402. inode_lru_list_del(inode);
  1403. spin_unlock(&inode->i_lock);
  1404. evict(inode);
  1405. }
  1406. /**
  1407. * iput - put an inode
  1408. * @inode: inode to put
  1409. *
  1410. * Puts an inode, dropping its usage count. If the inode use count hits
  1411. * zero, the inode is then freed and may also be destroyed.
  1412. *
  1413. * Consequently, iput() can sleep.
  1414. */
  1415. void iput(struct inode *inode)
  1416. {
  1417. if (!inode)
  1418. return;
  1419. BUG_ON(inode->i_state & I_CLEAR);
  1420. retry:
  1421. if (atomic_dec_and_lock(&inode->i_count, &inode->i_lock)) {
  1422. if (inode->i_nlink && (inode->i_state & I_DIRTY_TIME)) {
  1423. atomic_inc(&inode->i_count);
  1424. spin_unlock(&inode->i_lock);
  1425. trace_writeback_lazytime_iput(inode);
  1426. mark_inode_dirty_sync(inode);
  1427. goto retry;
  1428. }
  1429. iput_final(inode);
  1430. }
  1431. }
  1432. EXPORT_SYMBOL(iput);
  1433. /**
  1434. * bmap - find a block number in a file
  1435. * @inode: inode of file
  1436. * @block: block to find
  1437. *
  1438. * Returns the block number on the device holding the inode that
  1439. * is the disk block number for the block of the file requested.
  1440. * That is, asked for block 4 of inode 1 the function will return the
  1441. * disk block relative to the disk start that holds that block of the
  1442. * file.
  1443. */
  1444. sector_t bmap(struct inode *inode, sector_t block)
  1445. {
  1446. sector_t res = 0;
  1447. if (inode->i_mapping->a_ops->bmap)
  1448. res = inode->i_mapping->a_ops->bmap(inode->i_mapping, block);
  1449. return res;
  1450. }
  1451. EXPORT_SYMBOL(bmap);
  1452. /*
  1453. * With relative atime, only update atime if the previous atime is
  1454. * earlier than either the ctime or mtime or if at least a day has
  1455. * passed since the last atime update.
  1456. */
  1457. static int relatime_need_update(struct vfsmount *mnt, struct inode *inode,
  1458. struct timespec now)
  1459. {
  1460. if (!(mnt->mnt_flags & MNT_RELATIME))
  1461. return 1;
  1462. /*
  1463. * Is mtime younger than atime? If yes, update atime:
  1464. */
  1465. if (timespec64_compare(&inode->i_mtime, &inode->i_atime) >= 0)
  1466. return 1;
  1467. /*
  1468. * Is ctime younger than atime? If yes, update atime:
  1469. */
  1470. if (timespec64_compare(&inode->i_ctime, &inode->i_atime) >= 0)
  1471. return 1;
  1472. /*
  1473. * Is the previous atime value older than a day? If yes,
  1474. * update atime:
  1475. */
  1476. if ((long)(now.tv_sec - inode->i_atime.tv_sec) >= 24*60*60)
  1477. return 1;
  1478. /*
  1479. * Good, we can skip the atime update:
  1480. */
  1481. return 0;
  1482. }
  1483. int generic_update_time(struct inode *inode, struct timespec64 *time, int flags)
  1484. {
  1485. int iflags = I_DIRTY_TIME;
  1486. bool dirty = false;
  1487. if (flags & S_ATIME)
  1488. inode->i_atime = *time;
  1489. if (flags & S_VERSION)
  1490. dirty = inode_maybe_inc_iversion(inode, false);
  1491. if (flags & S_CTIME)
  1492. inode->i_ctime = *time;
  1493. if (flags & S_MTIME)
  1494. inode->i_mtime = *time;
  1495. if ((flags & (S_ATIME | S_CTIME | S_MTIME)) &&
  1496. !(inode->i_sb->s_flags & SB_LAZYTIME))
  1497. dirty = true;
  1498. if (dirty)
  1499. iflags |= I_DIRTY_SYNC;
  1500. __mark_inode_dirty(inode, iflags);
  1501. return 0;
  1502. }
  1503. EXPORT_SYMBOL(generic_update_time);
  1504. /*
  1505. * This does the actual work of updating an inodes time or version. Must have
  1506. * had called mnt_want_write() before calling this.
  1507. */
  1508. static int update_time(struct inode *inode, struct timespec64 *time, int flags)
  1509. {
  1510. int (*update_time)(struct inode *, struct timespec64 *, int);
  1511. update_time = inode->i_op->update_time ? inode->i_op->update_time :
  1512. generic_update_time;
  1513. return update_time(inode, time, flags);
  1514. }
  1515. /**
  1516. * touch_atime - update the access time
  1517. * @path: the &struct path to update
  1518. * @inode: inode to update
  1519. *
  1520. * Update the accessed time on an inode and mark it for writeback.
  1521. * This function automatically handles read only file systems and media,
  1522. * as well as the "noatime" flag and inode specific "noatime" markers.
  1523. */
  1524. bool atime_needs_update(const struct path *path, struct inode *inode)
  1525. {
  1526. struct vfsmount *mnt = path->mnt;
  1527. struct timespec64 now;
  1528. if (inode->i_flags & S_NOATIME)
  1529. return false;
  1530. /* Atime updates will likely cause i_uid and i_gid to be written
  1531. * back improprely if their true value is unknown to the vfs.
  1532. */
  1533. if (HAS_UNMAPPED_ID(inode))
  1534. return false;
  1535. if (IS_NOATIME(inode))
  1536. return false;
  1537. if ((inode->i_sb->s_flags & SB_NODIRATIME) && S_ISDIR(inode->i_mode))
  1538. return false;
  1539. if (mnt->mnt_flags & MNT_NOATIME)
  1540. return false;
  1541. if ((mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode))
  1542. return false;
  1543. now = current_time(inode);
  1544. if (!relatime_need_update(mnt, inode, timespec64_to_timespec(now)))
  1545. return false;
  1546. if (timespec64_equal(&inode->i_atime, &now))
  1547. return false;
  1548. return true;
  1549. }
  1550. void touch_atime(const struct path *path)
  1551. {
  1552. struct vfsmount *mnt = path->mnt;
  1553. struct inode *inode = d_inode(path->dentry);
  1554. struct timespec64 now;
  1555. if (!atime_needs_update(path, inode))
  1556. return;
  1557. if (!sb_start_write_trylock(inode->i_sb))
  1558. return;
  1559. if (__mnt_want_write(mnt) != 0)
  1560. goto skip_update;
  1561. /*
  1562. * File systems can error out when updating inodes if they need to
  1563. * allocate new space to modify an inode (such is the case for
  1564. * Btrfs), but since we touch atime while walking down the path we
  1565. * really don't care if we failed to update the atime of the file,
  1566. * so just ignore the return value.
  1567. * We may also fail on filesystems that have the ability to make parts
  1568. * of the fs read only, e.g. subvolumes in Btrfs.
  1569. */
  1570. now = current_time(inode);
  1571. update_time(inode, &now, S_ATIME);
  1572. __mnt_drop_write(mnt);
  1573. skip_update:
  1574. sb_end_write(inode->i_sb);
  1575. }
  1576. EXPORT_SYMBOL(touch_atime);
  1577. /*
  1578. * The logic we want is
  1579. *
  1580. * if suid or (sgid and xgrp)
  1581. * remove privs
  1582. */
  1583. int should_remove_suid(struct dentry *dentry)
  1584. {
  1585. umode_t mode = d_inode(dentry)->i_mode;
  1586. int kill = 0;
  1587. /* suid always must be killed */
  1588. if (unlikely(mode & S_ISUID))
  1589. kill = ATTR_KILL_SUID;
  1590. /*
  1591. * sgid without any exec bits is just a mandatory locking mark; leave
  1592. * it alone. If some exec bits are set, it's a real sgid; kill it.
  1593. */
  1594. if (unlikely((mode & S_ISGID) && (mode & S_IXGRP)))
  1595. kill |= ATTR_KILL_SGID;
  1596. if (unlikely(kill && !capable(CAP_FSETID) && S_ISREG(mode)))
  1597. return kill;
  1598. return 0;
  1599. }
  1600. EXPORT_SYMBOL(should_remove_suid);
  1601. /*
  1602. * Return mask of changes for notify_change() that need to be done as a
  1603. * response to write or truncate. Return 0 if nothing has to be changed.
  1604. * Negative value on error (change should be denied).
  1605. */
  1606. int dentry_needs_remove_privs(struct dentry *dentry)
  1607. {
  1608. struct inode *inode = d_inode(dentry);
  1609. int mask = 0;
  1610. int ret;
  1611. if (IS_NOSEC(inode))
  1612. return 0;
  1613. mask = should_remove_suid(dentry);
  1614. ret = security_inode_need_killpriv(dentry);
  1615. if (ret < 0)
  1616. return ret;
  1617. if (ret)
  1618. mask |= ATTR_KILL_PRIV;
  1619. return mask;
  1620. }
  1621. static int __remove_privs(struct dentry *dentry, int kill)
  1622. {
  1623. struct iattr newattrs;
  1624. newattrs.ia_valid = ATTR_FORCE | kill;
  1625. /*
  1626. * Note we call this on write, so notify_change will not
  1627. * encounter any conflicting delegations:
  1628. */
  1629. return notify_change(dentry, &newattrs, NULL);
  1630. }
  1631. /*
  1632. * Remove special file priviledges (suid, capabilities) when file is written
  1633. * to or truncated.
  1634. */
  1635. int file_remove_privs(struct file *file)
  1636. {
  1637. struct dentry *dentry = file_dentry(file);
  1638. struct inode *inode = file_inode(file);
  1639. int kill;
  1640. int error = 0;
  1641. /*
  1642. * Fast path for nothing security related.
  1643. * As well for non-regular files, e.g. blkdev inodes.
  1644. * For example, blkdev_write_iter() might get here
  1645. * trying to remove privs which it is not allowed to.
  1646. */
  1647. if (IS_NOSEC(inode) || !S_ISREG(inode->i_mode))
  1648. return 0;
  1649. kill = dentry_needs_remove_privs(dentry);
  1650. if (kill < 0)
  1651. return kill;
  1652. if (kill)
  1653. error = __remove_privs(dentry, kill);
  1654. if (!error)
  1655. inode_has_no_xattr(inode);
  1656. return error;
  1657. }
  1658. EXPORT_SYMBOL(file_remove_privs);
  1659. /**
  1660. * file_update_time - update mtime and ctime time
  1661. * @file: file accessed
  1662. *
  1663. * Update the mtime and ctime members of an inode and mark the inode
  1664. * for writeback. Note that this function is meant exclusively for
  1665. * usage in the file write path of filesystems, and filesystems may
  1666. * choose to explicitly ignore update via this function with the
  1667. * S_NOCMTIME inode flag, e.g. for network filesystem where these
  1668. * timestamps are handled by the server. This can return an error for
  1669. * file systems who need to allocate space in order to update an inode.
  1670. */
  1671. int file_update_time(struct file *file)
  1672. {
  1673. struct inode *inode = file_inode(file);
  1674. struct timespec64 now;
  1675. int sync_it = 0;
  1676. int ret;
  1677. /* First try to exhaust all avenues to not sync */
  1678. if (IS_NOCMTIME(inode))
  1679. return 0;
  1680. now = current_time(inode);
  1681. if (!timespec64_equal(&inode->i_mtime, &now))
  1682. sync_it = S_MTIME;
  1683. if (!timespec64_equal(&inode->i_ctime, &now))
  1684. sync_it |= S_CTIME;
  1685. if (IS_I_VERSION(inode) && inode_iversion_need_inc(inode))
  1686. sync_it |= S_VERSION;
  1687. if (!sync_it)
  1688. return 0;
  1689. /* Finally allowed to write? Takes lock. */
  1690. if (__mnt_want_write_file(file))
  1691. return 0;
  1692. ret = update_time(inode, &now, sync_it);
  1693. __mnt_drop_write_file(file);
  1694. return ret;
  1695. }
  1696. EXPORT_SYMBOL(file_update_time);
  1697. int inode_needs_sync(struct inode *inode)
  1698. {
  1699. if (IS_SYNC(inode))
  1700. return 1;
  1701. if (S_ISDIR(inode->i_mode) && IS_DIRSYNC(inode))
  1702. return 1;
  1703. return 0;
  1704. }
  1705. EXPORT_SYMBOL(inode_needs_sync);
  1706. /*
  1707. * If we try to find an inode in the inode hash while it is being
  1708. * deleted, we have to wait until the filesystem completes its
  1709. * deletion before reporting that it isn't found. This function waits
  1710. * until the deletion _might_ have completed. Callers are responsible
  1711. * to recheck inode state.
  1712. *
  1713. * It doesn't matter if I_NEW is not set initially, a call to
  1714. * wake_up_bit(&inode->i_state, __I_NEW) after removing from the hash list
  1715. * will DTRT.
  1716. */
  1717. static void __wait_on_freeing_inode(struct inode *inode)
  1718. {
  1719. wait_queue_head_t *wq;
  1720. DEFINE_WAIT_BIT(wait, &inode->i_state, __I_NEW);
  1721. wq = bit_waitqueue(&inode->i_state, __I_NEW);
  1722. prepare_to_wait(wq, &wait.wq_entry, TASK_UNINTERRUPTIBLE);
  1723. spin_unlock(&inode->i_lock);
  1724. spin_unlock(&inode_hash_lock);
  1725. schedule();
  1726. finish_wait(wq, &wait.wq_entry);
  1727. spin_lock(&inode_hash_lock);
  1728. }
  1729. static __initdata unsigned long ihash_entries;
  1730. static int __init set_ihash_entries(char *str)
  1731. {
  1732. if (!str)
  1733. return 0;
  1734. ihash_entries = simple_strtoul(str, &str, 0);
  1735. return 1;
  1736. }
  1737. __setup("ihash_entries=", set_ihash_entries);
  1738. /*
  1739. * Initialize the waitqueues and inode hash table.
  1740. */
  1741. void __init inode_init_early(void)
  1742. {
  1743. /* If hashes are distributed across NUMA nodes, defer
  1744. * hash allocation until vmalloc space is available.
  1745. */
  1746. if (hashdist)
  1747. return;
  1748. inode_hashtable =
  1749. alloc_large_system_hash("Inode-cache",
  1750. sizeof(struct hlist_head),
  1751. ihash_entries,
  1752. 14,
  1753. HASH_EARLY | HASH_ZERO,
  1754. &i_hash_shift,
  1755. &i_hash_mask,
  1756. 0,
  1757. 0);
  1758. }
  1759. void __init inode_init(void)
  1760. {
  1761. /* inode slab cache */
  1762. inode_cachep = kmem_cache_create("inode_cache",
  1763. sizeof(struct inode),
  1764. 0,
  1765. (SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|
  1766. SLAB_MEM_SPREAD|SLAB_ACCOUNT),
  1767. init_once);
  1768. /* Hash may have been set up in inode_init_early */
  1769. if (!hashdist)
  1770. return;
  1771. inode_hashtable =
  1772. alloc_large_system_hash("Inode-cache",
  1773. sizeof(struct hlist_head),
  1774. ihash_entries,
  1775. 14,
  1776. HASH_ZERO,
  1777. &i_hash_shift,
  1778. &i_hash_mask,
  1779. 0,
  1780. 0);
  1781. }
  1782. void init_special_inode(struct inode *inode, umode_t mode, dev_t rdev)
  1783. {
  1784. inode->i_mode = mode;
  1785. if (S_ISCHR(mode)) {
  1786. inode->i_fop = &def_chr_fops;
  1787. inode->i_rdev = rdev;
  1788. } else if (S_ISBLK(mode)) {
  1789. inode->i_fop = &def_blk_fops;
  1790. inode->i_rdev = rdev;
  1791. } else if (S_ISFIFO(mode))
  1792. inode->i_fop = &pipefifo_fops;
  1793. else if (S_ISSOCK(mode))
  1794. ; /* leave it no_open_fops */
  1795. else
  1796. printk(KERN_DEBUG "init_special_inode: bogus i_mode (%o) for"
  1797. " inode %s:%lu\n", mode, inode->i_sb->s_id,
  1798. inode->i_ino);
  1799. }
  1800. EXPORT_SYMBOL(init_special_inode);
  1801. /**
  1802. * inode_init_owner - Init uid,gid,mode for new inode according to posix standards
  1803. * @inode: New inode
  1804. * @dir: Directory inode
  1805. * @mode: mode of the new inode
  1806. */
  1807. void inode_init_owner(struct inode *inode, const struct inode *dir,
  1808. umode_t mode)
  1809. {
  1810. inode->i_uid = current_fsuid();
  1811. if (dir && dir->i_mode & S_ISGID) {
  1812. inode->i_gid = dir->i_gid;
  1813. /* Directories are special, and always inherit S_ISGID */
  1814. if (S_ISDIR(mode))
  1815. mode |= S_ISGID;
  1816. else if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP) &&
  1817. !in_group_p(inode->i_gid) &&
  1818. !capable_wrt_inode_uidgid(dir, CAP_FSETID))
  1819. mode &= ~S_ISGID;
  1820. } else
  1821. inode->i_gid = current_fsgid();
  1822. inode->i_mode = mode;
  1823. }
  1824. EXPORT_SYMBOL(inode_init_owner);
  1825. /**
  1826. * inode_owner_or_capable - check current task permissions to inode
  1827. * @inode: inode being checked
  1828. *
  1829. * Return true if current either has CAP_FOWNER in a namespace with the
  1830. * inode owner uid mapped, or owns the file.
  1831. */
  1832. bool inode_owner_or_capable(const struct inode *inode)
  1833. {
  1834. struct user_namespace *ns;
  1835. if (uid_eq(current_fsuid(), inode->i_uid))
  1836. return true;
  1837. ns = current_user_ns();
  1838. if (kuid_has_mapping(ns, inode->i_uid) && ns_capable(ns, CAP_FOWNER))
  1839. return true;
  1840. return false;
  1841. }
  1842. EXPORT_SYMBOL(inode_owner_or_capable);
  1843. /*
  1844. * Direct i/o helper functions
  1845. */
  1846. static void __inode_dio_wait(struct inode *inode)
  1847. {
  1848. wait_queue_head_t *wq = bit_waitqueue(&inode->i_state, __I_DIO_WAKEUP);
  1849. DEFINE_WAIT_BIT(q, &inode->i_state, __I_DIO_WAKEUP);
  1850. do {
  1851. prepare_to_wait(wq, &q.wq_entry, TASK_UNINTERRUPTIBLE);
  1852. if (atomic_read(&inode->i_dio_count))
  1853. schedule();
  1854. } while (atomic_read(&inode->i_dio_count));
  1855. finish_wait(wq, &q.wq_entry);
  1856. }
  1857. /**
  1858. * inode_dio_wait - wait for outstanding DIO requests to finish
  1859. * @inode: inode to wait for
  1860. *
  1861. * Waits for all pending direct I/O requests to finish so that we can
  1862. * proceed with a truncate or equivalent operation.
  1863. *
  1864. * Must be called under a lock that serializes taking new references
  1865. * to i_dio_count, usually by inode->i_mutex.
  1866. */
  1867. void inode_dio_wait(struct inode *inode)
  1868. {
  1869. if (atomic_read(&inode->i_dio_count))
  1870. __inode_dio_wait(inode);
  1871. }
  1872. EXPORT_SYMBOL(inode_dio_wait);
  1873. /*
  1874. * inode_set_flags - atomically set some inode flags
  1875. *
  1876. * Note: the caller should be holding i_mutex, or else be sure that
  1877. * they have exclusive access to the inode structure (i.e., while the
  1878. * inode is being instantiated). The reason for the cmpxchg() loop
  1879. * --- which wouldn't be necessary if all code paths which modify
  1880. * i_flags actually followed this rule, is that there is at least one
  1881. * code path which doesn't today so we use cmpxchg() out of an abundance
  1882. * of caution.
  1883. *
  1884. * In the long run, i_mutex is overkill, and we should probably look
  1885. * at using the i_lock spinlock to protect i_flags, and then make sure
  1886. * it is so documented in include/linux/fs.h and that all code follows
  1887. * the locking convention!!
  1888. */
  1889. void inode_set_flags(struct inode *inode, unsigned int flags,
  1890. unsigned int mask)
  1891. {
  1892. unsigned int old_flags, new_flags;
  1893. WARN_ON_ONCE(flags & ~mask);
  1894. do {
  1895. old_flags = READ_ONCE(inode->i_flags);
  1896. new_flags = (old_flags & ~mask) | flags;
  1897. } while (unlikely(cmpxchg(&inode->i_flags, old_flags,
  1898. new_flags) != old_flags));
  1899. }
  1900. EXPORT_SYMBOL(inode_set_flags);
  1901. void inode_nohighmem(struct inode *inode)
  1902. {
  1903. mapping_set_gfp_mask(inode->i_mapping, GFP_USER);
  1904. }
  1905. EXPORT_SYMBOL(inode_nohighmem);
  1906. /**
  1907. * timespec64_trunc - Truncate timespec64 to a granularity
  1908. * @t: Timespec64
  1909. * @gran: Granularity in ns.
  1910. *
  1911. * Truncate a timespec64 to a granularity. Always rounds down. gran must
  1912. * not be 0 nor greater than a second (NSEC_PER_SEC, or 10^9 ns).
  1913. */
  1914. struct timespec64 timespec64_trunc(struct timespec64 t, unsigned gran)
  1915. {
  1916. /* Avoid division in the common cases 1 ns and 1 s. */
  1917. if (gran == 1) {
  1918. /* nothing */
  1919. } else if (gran == NSEC_PER_SEC) {
  1920. t.tv_nsec = 0;
  1921. } else if (gran > 1 && gran < NSEC_PER_SEC) {
  1922. t.tv_nsec -= t.tv_nsec % gran;
  1923. } else {
  1924. WARN(1, "illegal file time granularity: %u", gran);
  1925. }
  1926. return t;
  1927. }
  1928. EXPORT_SYMBOL(timespec64_trunc);
  1929. /**
  1930. * current_time - Return FS time
  1931. * @inode: inode.
  1932. *
  1933. * Return the current time truncated to the time granularity supported by
  1934. * the fs.
  1935. *
  1936. * Note that inode and inode->sb cannot be NULL.
  1937. * Otherwise, the function warns and returns time without truncation.
  1938. */
  1939. struct timespec64 current_time(struct inode *inode)
  1940. {
  1941. struct timespec64 now = current_kernel_time64();
  1942. if (unlikely(!inode->i_sb)) {
  1943. WARN(1, "current_time() called with uninitialized super_block in the inode");
  1944. return now;
  1945. }
  1946. return timespec64_trunc(now, inode->i_sb->s_time_gran);
  1947. }
  1948. EXPORT_SYMBOL(current_time);