inode.c 50 KB

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