inode.c 50 KB

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