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