inode.c 49 KB

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