inode.c 57 KB

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