inode.c 49 KB

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