inode.c 50 KB

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