inode.c 52 KB

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