inode.c 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * (C) 1997 Linus Torvalds
  4. * (C) 1999 Andrea Arcangeli <andrea@suse.de> (dynamic inode allocation)
  5. */
  6. #include <linux/export.h>
  7. #include <linux/fs.h>
  8. #include <linux/filelock.h>
  9. #include <linux/mm.h>
  10. #include <linux/backing-dev.h>
  11. #include <linux/hash.h>
  12. #include <linux/swap.h>
  13. #include <linux/security.h>
  14. #include <linux/cdev.h>
  15. #include <linux/memblock.h>
  16. #include <linux/fsnotify.h>
  17. #include <linux/mount.h>
  18. #include <linux/posix_acl.h>
  19. #include <linux/buffer_head.h> /* for inode_has_buffers */
  20. #include <linux/ratelimit.h>
  21. #include <linux/list_lru.h>
  22. #include <linux/iversion.h>
  23. #include <linux/rw_hint.h>
  24. #include <trace/events/writeback.h>
  25. #include "internal.h"
  26. /*
  27. * Inode locking rules:
  28. *
  29. * inode->i_lock protects:
  30. * inode->i_state, inode->i_hash, __iget(), inode->i_io_list
  31. * Inode LRU list locks protect:
  32. * inode->i_sb->s_inode_lru, inode->i_lru
  33. * inode->i_sb->s_inode_list_lock protects:
  34. * inode->i_sb->s_inodes, inode->i_sb_list
  35. * bdi->wb.list_lock protects:
  36. * bdi->wb.b_{dirty,io,more_io,dirty_time}, inode->i_io_list
  37. * inode_hash_lock protects:
  38. * inode_hashtable, inode->i_hash
  39. *
  40. * Lock ordering:
  41. *
  42. * inode->i_sb->s_inode_list_lock
  43. * inode->i_lock
  44. * Inode LRU list locks
  45. *
  46. * bdi->wb.list_lock
  47. * inode->i_lock
  48. *
  49. * inode_hash_lock
  50. * inode->i_sb->s_inode_list_lock
  51. * inode->i_lock
  52. *
  53. * iunique_lock
  54. * inode_hash_lock
  55. */
  56. static unsigned int i_hash_mask __ro_after_init;
  57. static unsigned int i_hash_shift __ro_after_init;
  58. static struct hlist_head *inode_hashtable __ro_after_init;
  59. static __cacheline_aligned_in_smp DEFINE_SPINLOCK(inode_hash_lock);
  60. /*
  61. * Empty aops. Can be used for the cases where the user does not
  62. * define any of the address_space operations.
  63. */
  64. const struct address_space_operations empty_aops = {
  65. };
  66. EXPORT_SYMBOL(empty_aops);
  67. static DEFINE_PER_CPU(unsigned long, nr_inodes);
  68. static DEFINE_PER_CPU(unsigned long, nr_unused);
  69. static struct kmem_cache *inode_cachep __ro_after_init;
  70. static long get_nr_inodes(void)
  71. {
  72. int i;
  73. long sum = 0;
  74. for_each_possible_cpu(i)
  75. sum += per_cpu(nr_inodes, i);
  76. return sum < 0 ? 0 : sum;
  77. }
  78. static inline long get_nr_inodes_unused(void)
  79. {
  80. int i;
  81. long sum = 0;
  82. for_each_possible_cpu(i)
  83. sum += per_cpu(nr_unused, i);
  84. return sum < 0 ? 0 : sum;
  85. }
  86. long get_nr_dirty_inodes(void)
  87. {
  88. /* not actually dirty inodes, but a wild approximation */
  89. long nr_dirty = get_nr_inodes() - get_nr_inodes_unused();
  90. return nr_dirty > 0 ? nr_dirty : 0;
  91. }
  92. /*
  93. * Handle nr_inode sysctl
  94. */
  95. #ifdef CONFIG_SYSCTL
  96. /*
  97. * Statistics gathering..
  98. */
  99. static struct inodes_stat_t inodes_stat;
  100. static int proc_nr_inodes(const struct ctl_table *table, int write, void *buffer,
  101. size_t *lenp, loff_t *ppos)
  102. {
  103. inodes_stat.nr_inodes = get_nr_inodes();
  104. inodes_stat.nr_unused = get_nr_inodes_unused();
  105. return proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
  106. }
  107. static struct ctl_table inodes_sysctls[] = {
  108. {
  109. .procname = "inode-nr",
  110. .data = &inodes_stat,
  111. .maxlen = 2*sizeof(long),
  112. .mode = 0444,
  113. .proc_handler = proc_nr_inodes,
  114. },
  115. {
  116. .procname = "inode-state",
  117. .data = &inodes_stat,
  118. .maxlen = 7*sizeof(long),
  119. .mode = 0444,
  120. .proc_handler = proc_nr_inodes,
  121. },
  122. };
  123. static int __init init_fs_inode_sysctls(void)
  124. {
  125. register_sysctl_init("fs", inodes_sysctls);
  126. return 0;
  127. }
  128. early_initcall(init_fs_inode_sysctls);
  129. #endif
  130. static int no_open(struct inode *inode, struct file *file)
  131. {
  132. return -ENXIO;
  133. }
  134. /**
  135. * inode_init_always_gfp - perform inode structure initialisation
  136. * @sb: superblock inode belongs to
  137. * @inode: inode to initialise
  138. * @gfp: allocation flags
  139. *
  140. * These are initializations that need to be done on every inode
  141. * allocation as the fields are not initialised by slab allocation.
  142. * If there are additional allocations required @gfp is used.
  143. */
  144. int inode_init_always_gfp(struct super_block *sb, struct inode *inode, gfp_t gfp)
  145. {
  146. static const struct inode_operations empty_iops;
  147. static const struct file_operations no_open_fops = {.open = no_open};
  148. struct address_space *const mapping = &inode->i_data;
  149. inode->i_sb = sb;
  150. inode->i_blkbits = sb->s_blocksize_bits;
  151. inode->i_flags = 0;
  152. inode->i_state = 0;
  153. atomic64_set(&inode->i_sequence, 0);
  154. atomic_set(&inode->i_count, 1);
  155. inode->i_op = &empty_iops;
  156. inode->i_fop = &no_open_fops;
  157. inode->i_ino = 0;
  158. inode->__i_nlink = 1;
  159. inode->i_opflags = 0;
  160. if (sb->s_xattr)
  161. inode->i_opflags |= IOP_XATTR;
  162. i_uid_write(inode, 0);
  163. i_gid_write(inode, 0);
  164. atomic_set(&inode->i_writecount, 0);
  165. inode->i_size = 0;
  166. inode->i_write_hint = WRITE_LIFE_NOT_SET;
  167. inode->i_blocks = 0;
  168. inode->i_bytes = 0;
  169. inode->i_generation = 0;
  170. inode->i_pipe = NULL;
  171. inode->i_cdev = NULL;
  172. inode->i_link = NULL;
  173. inode->i_dir_seq = 0;
  174. inode->i_rdev = 0;
  175. inode->dirtied_when = 0;
  176. #ifdef CONFIG_CGROUP_WRITEBACK
  177. inode->i_wb_frn_winner = 0;
  178. inode->i_wb_frn_avg_time = 0;
  179. inode->i_wb_frn_history = 0;
  180. #endif
  181. spin_lock_init(&inode->i_lock);
  182. lockdep_set_class(&inode->i_lock, &sb->s_type->i_lock_key);
  183. init_rwsem(&inode->i_rwsem);
  184. lockdep_set_class(&inode->i_rwsem, &sb->s_type->i_mutex_key);
  185. atomic_set(&inode->i_dio_count, 0);
  186. mapping->a_ops = &empty_aops;
  187. mapping->host = inode;
  188. mapping->flags = 0;
  189. mapping->wb_err = 0;
  190. atomic_set(&mapping->i_mmap_writable, 0);
  191. #ifdef CONFIG_READ_ONLY_THP_FOR_FS
  192. atomic_set(&mapping->nr_thps, 0);
  193. #endif
  194. mapping_set_gfp_mask(mapping, GFP_HIGHUSER_MOVABLE);
  195. mapping->i_private_data = NULL;
  196. mapping->writeback_index = 0;
  197. init_rwsem(&mapping->invalidate_lock);
  198. lockdep_set_class_and_name(&mapping->invalidate_lock,
  199. &sb->s_type->invalidate_lock_key,
  200. "mapping.invalidate_lock");
  201. if (sb->s_iflags & SB_I_STABLE_WRITES)
  202. mapping_set_stable_writes(mapping);
  203. inode->i_private = NULL;
  204. inode->i_mapping = mapping;
  205. INIT_HLIST_HEAD(&inode->i_dentry); /* buggered by rcu freeing */
  206. #ifdef CONFIG_FS_POSIX_ACL
  207. inode->i_acl = inode->i_default_acl = ACL_NOT_CACHED;
  208. #endif
  209. #ifdef CONFIG_FSNOTIFY
  210. inode->i_fsnotify_mask = 0;
  211. #endif
  212. inode->i_flctx = NULL;
  213. if (unlikely(security_inode_alloc(inode, gfp)))
  214. return -ENOMEM;
  215. this_cpu_inc(nr_inodes);
  216. return 0;
  217. }
  218. EXPORT_SYMBOL(inode_init_always_gfp);
  219. void free_inode_nonrcu(struct inode *inode)
  220. {
  221. kmem_cache_free(inode_cachep, inode);
  222. }
  223. EXPORT_SYMBOL(free_inode_nonrcu);
  224. static void i_callback(struct rcu_head *head)
  225. {
  226. struct inode *inode = container_of(head, struct inode, i_rcu);
  227. if (inode->free_inode)
  228. inode->free_inode(inode);
  229. else
  230. free_inode_nonrcu(inode);
  231. }
  232. static struct inode *alloc_inode(struct super_block *sb)
  233. {
  234. const struct super_operations *ops = sb->s_op;
  235. struct inode *inode;
  236. if (ops->alloc_inode)
  237. inode = ops->alloc_inode(sb);
  238. else
  239. inode = alloc_inode_sb(sb, inode_cachep, GFP_KERNEL);
  240. if (!inode)
  241. return NULL;
  242. if (unlikely(inode_init_always(sb, inode))) {
  243. if (ops->destroy_inode) {
  244. ops->destroy_inode(inode);
  245. if (!ops->free_inode)
  246. return NULL;
  247. }
  248. inode->free_inode = ops->free_inode;
  249. i_callback(&inode->i_rcu);
  250. return NULL;
  251. }
  252. return inode;
  253. }
  254. void __destroy_inode(struct inode *inode)
  255. {
  256. BUG_ON(inode_has_buffers(inode));
  257. inode_detach_wb(inode);
  258. security_inode_free(inode);
  259. fsnotify_inode_delete(inode);
  260. locks_free_lock_context(inode);
  261. if (!inode->i_nlink) {
  262. WARN_ON(atomic_long_read(&inode->i_sb->s_remove_count) == 0);
  263. atomic_long_dec(&inode->i_sb->s_remove_count);
  264. }
  265. #ifdef CONFIG_FS_POSIX_ACL
  266. if (inode->i_acl && !is_uncached_acl(inode->i_acl))
  267. posix_acl_release(inode->i_acl);
  268. if (inode->i_default_acl && !is_uncached_acl(inode->i_default_acl))
  269. posix_acl_release(inode->i_default_acl);
  270. #endif
  271. this_cpu_dec(nr_inodes);
  272. }
  273. EXPORT_SYMBOL(__destroy_inode);
  274. static void destroy_inode(struct inode *inode)
  275. {
  276. const struct super_operations *ops = inode->i_sb->s_op;
  277. BUG_ON(!list_empty(&inode->i_lru));
  278. __destroy_inode(inode);
  279. if (ops->destroy_inode) {
  280. ops->destroy_inode(inode);
  281. if (!ops->free_inode)
  282. return;
  283. }
  284. inode->free_inode = ops->free_inode;
  285. call_rcu(&inode->i_rcu, i_callback);
  286. }
  287. /**
  288. * drop_nlink - directly drop an inode's link count
  289. * @inode: inode
  290. *
  291. * This is a low-level filesystem helper to replace any
  292. * direct filesystem manipulation of i_nlink. In cases
  293. * where we are attempting to track writes to the
  294. * filesystem, a decrement to zero means an imminent
  295. * write when the file is truncated and actually unlinked
  296. * on the filesystem.
  297. */
  298. void drop_nlink(struct inode *inode)
  299. {
  300. WARN_ON(inode->i_nlink == 0);
  301. inode->__i_nlink--;
  302. if (!inode->i_nlink)
  303. atomic_long_inc(&inode->i_sb->s_remove_count);
  304. }
  305. EXPORT_SYMBOL(drop_nlink);
  306. /**
  307. * clear_nlink - directly zero an inode's link count
  308. * @inode: inode
  309. *
  310. * This is a low-level filesystem helper to replace any
  311. * direct filesystem manipulation of i_nlink. See
  312. * drop_nlink() for why we care about i_nlink hitting zero.
  313. */
  314. void clear_nlink(struct inode *inode)
  315. {
  316. if (inode->i_nlink) {
  317. inode->__i_nlink = 0;
  318. atomic_long_inc(&inode->i_sb->s_remove_count);
  319. }
  320. }
  321. EXPORT_SYMBOL(clear_nlink);
  322. /**
  323. * set_nlink - directly set an inode's link count
  324. * @inode: inode
  325. * @nlink: new nlink (should be non-zero)
  326. *
  327. * This is a low-level filesystem helper to replace any
  328. * direct filesystem manipulation of i_nlink.
  329. */
  330. void set_nlink(struct inode *inode, unsigned int nlink)
  331. {
  332. if (!nlink) {
  333. clear_nlink(inode);
  334. } else {
  335. /* Yes, some filesystems do change nlink from zero to one */
  336. if (inode->i_nlink == 0)
  337. atomic_long_dec(&inode->i_sb->s_remove_count);
  338. inode->__i_nlink = nlink;
  339. }
  340. }
  341. EXPORT_SYMBOL(set_nlink);
  342. /**
  343. * inc_nlink - directly increment an inode's link count
  344. * @inode: inode
  345. *
  346. * This is a low-level filesystem helper to replace any
  347. * direct filesystem manipulation of i_nlink. Currently,
  348. * it is only here for parity with dec_nlink().
  349. */
  350. void inc_nlink(struct inode *inode)
  351. {
  352. if (unlikely(inode->i_nlink == 0)) {
  353. WARN_ON(!(inode->i_state & I_LINKABLE));
  354. atomic_long_dec(&inode->i_sb->s_remove_count);
  355. }
  356. inode->__i_nlink++;
  357. }
  358. EXPORT_SYMBOL(inc_nlink);
  359. static void __address_space_init_once(struct address_space *mapping)
  360. {
  361. xa_init_flags(&mapping->i_pages, XA_FLAGS_LOCK_IRQ | XA_FLAGS_ACCOUNT);
  362. init_rwsem(&mapping->i_mmap_rwsem);
  363. INIT_LIST_HEAD(&mapping->i_private_list);
  364. spin_lock_init(&mapping->i_private_lock);
  365. mapping->i_mmap = RB_ROOT_CACHED;
  366. }
  367. void address_space_init_once(struct address_space *mapping)
  368. {
  369. memset(mapping, 0, sizeof(*mapping));
  370. __address_space_init_once(mapping);
  371. }
  372. EXPORT_SYMBOL(address_space_init_once);
  373. /*
  374. * These are initializations that only need to be done
  375. * once, because the fields are idempotent across use
  376. * of the inode, so let the slab aware of that.
  377. */
  378. void inode_init_once(struct inode *inode)
  379. {
  380. memset(inode, 0, sizeof(*inode));
  381. INIT_HLIST_NODE(&inode->i_hash);
  382. INIT_LIST_HEAD(&inode->i_devices);
  383. INIT_LIST_HEAD(&inode->i_io_list);
  384. INIT_LIST_HEAD(&inode->i_wb_list);
  385. INIT_LIST_HEAD(&inode->i_lru);
  386. INIT_LIST_HEAD(&inode->i_sb_list);
  387. __address_space_init_once(&inode->i_data);
  388. i_size_ordered_init(inode);
  389. }
  390. EXPORT_SYMBOL(inode_init_once);
  391. static void init_once(void *foo)
  392. {
  393. struct inode *inode = (struct inode *) foo;
  394. inode_init_once(inode);
  395. }
  396. /*
  397. * get additional reference to inode; caller must already hold one.
  398. */
  399. void ihold(struct inode *inode)
  400. {
  401. WARN_ON(atomic_inc_return(&inode->i_count) < 2);
  402. }
  403. EXPORT_SYMBOL(ihold);
  404. static void __inode_add_lru(struct inode *inode, bool rotate)
  405. {
  406. if (inode->i_state & (I_DIRTY_ALL | I_SYNC | I_FREEING | I_WILL_FREE))
  407. return;
  408. if (atomic_read(&inode->i_count))
  409. return;
  410. if (!(inode->i_sb->s_flags & SB_ACTIVE))
  411. return;
  412. if (!mapping_shrinkable(&inode->i_data))
  413. return;
  414. if (list_lru_add_obj(&inode->i_sb->s_inode_lru, &inode->i_lru))
  415. this_cpu_inc(nr_unused);
  416. else if (rotate)
  417. inode->i_state |= I_REFERENCED;
  418. }
  419. struct wait_queue_head *inode_bit_waitqueue(struct wait_bit_queue_entry *wqe,
  420. struct inode *inode, u32 bit)
  421. {
  422. void *bit_address;
  423. bit_address = inode_state_wait_address(inode, bit);
  424. init_wait_var_entry(wqe, bit_address, 0);
  425. return __var_waitqueue(bit_address);
  426. }
  427. EXPORT_SYMBOL(inode_bit_waitqueue);
  428. /*
  429. * Add inode to LRU if needed (inode is unused and clean).
  430. *
  431. * Needs inode->i_lock held.
  432. */
  433. void inode_add_lru(struct inode *inode)
  434. {
  435. __inode_add_lru(inode, false);
  436. }
  437. static void inode_lru_list_del(struct inode *inode)
  438. {
  439. if (list_lru_del_obj(&inode->i_sb->s_inode_lru, &inode->i_lru))
  440. this_cpu_dec(nr_unused);
  441. }
  442. static void inode_pin_lru_isolating(struct inode *inode)
  443. {
  444. lockdep_assert_held(&inode->i_lock);
  445. WARN_ON(inode->i_state & (I_LRU_ISOLATING | I_FREEING | I_WILL_FREE));
  446. inode->i_state |= I_LRU_ISOLATING;
  447. }
  448. static void inode_unpin_lru_isolating(struct inode *inode)
  449. {
  450. spin_lock(&inode->i_lock);
  451. WARN_ON(!(inode->i_state & I_LRU_ISOLATING));
  452. inode->i_state &= ~I_LRU_ISOLATING;
  453. /* Called with inode->i_lock which ensures memory ordering. */
  454. inode_wake_up_bit(inode, __I_LRU_ISOLATING);
  455. spin_unlock(&inode->i_lock);
  456. }
  457. static void inode_wait_for_lru_isolating(struct inode *inode)
  458. {
  459. struct wait_bit_queue_entry wqe;
  460. struct wait_queue_head *wq_head;
  461. lockdep_assert_held(&inode->i_lock);
  462. if (!(inode->i_state & I_LRU_ISOLATING))
  463. return;
  464. wq_head = inode_bit_waitqueue(&wqe, inode, __I_LRU_ISOLATING);
  465. for (;;) {
  466. prepare_to_wait_event(wq_head, &wqe.wq_entry, TASK_UNINTERRUPTIBLE);
  467. /*
  468. * Checking I_LRU_ISOLATING with inode->i_lock guarantees
  469. * memory ordering.
  470. */
  471. if (!(inode->i_state & I_LRU_ISOLATING))
  472. break;
  473. spin_unlock(&inode->i_lock);
  474. schedule();
  475. spin_lock(&inode->i_lock);
  476. }
  477. finish_wait(wq_head, &wqe.wq_entry);
  478. WARN_ON(inode->i_state & I_LRU_ISOLATING);
  479. }
  480. /**
  481. * inode_sb_list_add - add inode to the superblock list of inodes
  482. * @inode: inode to add
  483. */
  484. void inode_sb_list_add(struct inode *inode)
  485. {
  486. spin_lock(&inode->i_sb->s_inode_list_lock);
  487. list_add(&inode->i_sb_list, &inode->i_sb->s_inodes);
  488. spin_unlock(&inode->i_sb->s_inode_list_lock);
  489. }
  490. EXPORT_SYMBOL_GPL(inode_sb_list_add);
  491. static inline void inode_sb_list_del(struct inode *inode)
  492. {
  493. if (!list_empty(&inode->i_sb_list)) {
  494. spin_lock(&inode->i_sb->s_inode_list_lock);
  495. list_del_init(&inode->i_sb_list);
  496. spin_unlock(&inode->i_sb->s_inode_list_lock);
  497. }
  498. }
  499. static unsigned long hash(struct super_block *sb, unsigned long hashval)
  500. {
  501. unsigned long tmp;
  502. tmp = (hashval * (unsigned long)sb) ^ (GOLDEN_RATIO_PRIME + hashval) /
  503. L1_CACHE_BYTES;
  504. tmp = tmp ^ ((tmp ^ GOLDEN_RATIO_PRIME) >> i_hash_shift);
  505. return tmp & i_hash_mask;
  506. }
  507. /**
  508. * __insert_inode_hash - hash an inode
  509. * @inode: unhashed inode
  510. * @hashval: unsigned long value used to locate this object in the
  511. * inode_hashtable.
  512. *
  513. * Add an inode to the inode hash for this superblock.
  514. */
  515. void __insert_inode_hash(struct inode *inode, unsigned long hashval)
  516. {
  517. struct hlist_head *b = inode_hashtable + hash(inode->i_sb, hashval);
  518. spin_lock(&inode_hash_lock);
  519. spin_lock(&inode->i_lock);
  520. hlist_add_head_rcu(&inode->i_hash, b);
  521. spin_unlock(&inode->i_lock);
  522. spin_unlock(&inode_hash_lock);
  523. }
  524. EXPORT_SYMBOL(__insert_inode_hash);
  525. /**
  526. * __remove_inode_hash - remove an inode from the hash
  527. * @inode: inode to unhash
  528. *
  529. * Remove an inode from the superblock.
  530. */
  531. void __remove_inode_hash(struct inode *inode)
  532. {
  533. spin_lock(&inode_hash_lock);
  534. spin_lock(&inode->i_lock);
  535. hlist_del_init_rcu(&inode->i_hash);
  536. spin_unlock(&inode->i_lock);
  537. spin_unlock(&inode_hash_lock);
  538. }
  539. EXPORT_SYMBOL(__remove_inode_hash);
  540. void dump_mapping(const struct address_space *mapping)
  541. {
  542. struct inode *host;
  543. const struct address_space_operations *a_ops;
  544. struct hlist_node *dentry_first;
  545. struct dentry *dentry_ptr;
  546. struct dentry dentry;
  547. char fname[64] = {};
  548. unsigned long ino;
  549. /*
  550. * If mapping is an invalid pointer, we don't want to crash
  551. * accessing it, so probe everything depending on it carefully.
  552. */
  553. if (get_kernel_nofault(host, &mapping->host) ||
  554. get_kernel_nofault(a_ops, &mapping->a_ops)) {
  555. pr_warn("invalid mapping:%px\n", mapping);
  556. return;
  557. }
  558. if (!host) {
  559. pr_warn("aops:%ps\n", a_ops);
  560. return;
  561. }
  562. if (get_kernel_nofault(dentry_first, &host->i_dentry.first) ||
  563. get_kernel_nofault(ino, &host->i_ino)) {
  564. pr_warn("aops:%ps invalid inode:%px\n", a_ops, host);
  565. return;
  566. }
  567. if (!dentry_first) {
  568. pr_warn("aops:%ps ino:%lx\n", a_ops, ino);
  569. return;
  570. }
  571. dentry_ptr = container_of(dentry_first, struct dentry, d_u.d_alias);
  572. if (get_kernel_nofault(dentry, dentry_ptr) ||
  573. !dentry.d_parent || !dentry.d_name.name) {
  574. pr_warn("aops:%ps ino:%lx invalid dentry:%px\n",
  575. a_ops, ino, dentry_ptr);
  576. return;
  577. }
  578. if (strncpy_from_kernel_nofault(fname, dentry.d_name.name, 63) < 0)
  579. strscpy(fname, "<invalid>");
  580. /*
  581. * Even if strncpy_from_kernel_nofault() succeeded,
  582. * the fname could be unreliable
  583. */
  584. pr_warn("aops:%ps ino:%lx dentry name(?):\"%s\"\n",
  585. a_ops, ino, fname);
  586. }
  587. void clear_inode(struct inode *inode)
  588. {
  589. /*
  590. * We have to cycle the i_pages lock here because reclaim can be in the
  591. * process of removing the last page (in __filemap_remove_folio())
  592. * and we must not free the mapping under it.
  593. */
  594. xa_lock_irq(&inode->i_data.i_pages);
  595. BUG_ON(inode->i_data.nrpages);
  596. /*
  597. * Almost always, mapping_empty(&inode->i_data) here; but there are
  598. * two known and long-standing ways in which nodes may get left behind
  599. * (when deep radix-tree node allocation failed partway; or when THP
  600. * collapse_file() failed). Until those two known cases are cleaned up,
  601. * or a cleanup function is called here, do not BUG_ON(!mapping_empty),
  602. * nor even WARN_ON(!mapping_empty).
  603. */
  604. xa_unlock_irq(&inode->i_data.i_pages);
  605. BUG_ON(!list_empty(&inode->i_data.i_private_list));
  606. BUG_ON(!(inode->i_state & I_FREEING));
  607. BUG_ON(inode->i_state & I_CLEAR);
  608. BUG_ON(!list_empty(&inode->i_wb_list));
  609. /* don't need i_lock here, no concurrent mods to i_state */
  610. inode->i_state = I_FREEING | I_CLEAR;
  611. }
  612. EXPORT_SYMBOL(clear_inode);
  613. /*
  614. * Free the inode passed in, removing it from the lists it is still connected
  615. * to. We remove any pages still attached to the inode and wait for any IO that
  616. * is still in progress before finally destroying the inode.
  617. *
  618. * An inode must already be marked I_FREEING so that we avoid the inode being
  619. * moved back onto lists if we race with other code that manipulates the lists
  620. * (e.g. writeback_single_inode). The caller is responsible for setting this.
  621. *
  622. * An inode must already be removed from the LRU list before being evicted from
  623. * the cache. This should occur atomically with setting the I_FREEING state
  624. * flag, so no inodes here should ever be on the LRU when being evicted.
  625. */
  626. static void evict(struct inode *inode)
  627. {
  628. const struct super_operations *op = inode->i_sb->s_op;
  629. BUG_ON(!(inode->i_state & I_FREEING));
  630. BUG_ON(!list_empty(&inode->i_lru));
  631. if (!list_empty(&inode->i_io_list))
  632. inode_io_list_del(inode);
  633. inode_sb_list_del(inode);
  634. spin_lock(&inode->i_lock);
  635. inode_wait_for_lru_isolating(inode);
  636. /*
  637. * Wait for flusher thread to be done with the inode so that filesystem
  638. * does not start destroying it while writeback is still running. Since
  639. * the inode has I_FREEING set, flusher thread won't start new work on
  640. * the inode. We just have to wait for running writeback to finish.
  641. */
  642. inode_wait_for_writeback(inode);
  643. spin_unlock(&inode->i_lock);
  644. if (op->evict_inode) {
  645. op->evict_inode(inode);
  646. } else {
  647. truncate_inode_pages_final(&inode->i_data);
  648. clear_inode(inode);
  649. }
  650. if (S_ISCHR(inode->i_mode) && inode->i_cdev)
  651. cd_forget(inode);
  652. remove_inode_hash(inode);
  653. /*
  654. * Wake up waiters in __wait_on_freeing_inode().
  655. *
  656. * Lockless hash lookup may end up finding the inode before we removed
  657. * it above, but only lock it *after* we are done with the wakeup below.
  658. * In this case the potential waiter cannot safely block.
  659. *
  660. * The inode being unhashed after the call to remove_inode_hash() is
  661. * used as an indicator whether blocking on it is safe.
  662. */
  663. spin_lock(&inode->i_lock);
  664. /*
  665. * Pairs with the barrier in prepare_to_wait_event() to make sure
  666. * ___wait_var_event() either sees the bit cleared or
  667. * waitqueue_active() check in wake_up_var() sees the waiter.
  668. */
  669. smp_mb();
  670. inode_wake_up_bit(inode, __I_NEW);
  671. BUG_ON(inode->i_state != (I_FREEING | I_CLEAR));
  672. spin_unlock(&inode->i_lock);
  673. destroy_inode(inode);
  674. }
  675. /*
  676. * dispose_list - dispose of the contents of a local list
  677. * @head: the head of the list to free
  678. *
  679. * Dispose-list gets a local list with local inodes in it, so it doesn't
  680. * need to worry about list corruption and SMP locks.
  681. */
  682. static void dispose_list(struct list_head *head)
  683. {
  684. while (!list_empty(head)) {
  685. struct inode *inode;
  686. inode = list_first_entry(head, struct inode, i_lru);
  687. list_del_init(&inode->i_lru);
  688. evict(inode);
  689. cond_resched();
  690. }
  691. }
  692. /**
  693. * evict_inodes - evict all evictable inodes for a superblock
  694. * @sb: superblock to operate on
  695. *
  696. * Make sure that no inodes with zero refcount are retained. This is
  697. * called by superblock shutdown after having SB_ACTIVE flag removed,
  698. * so any inode reaching zero refcount during or after that call will
  699. * be immediately evicted.
  700. */
  701. void evict_inodes(struct super_block *sb)
  702. {
  703. struct inode *inode, *next;
  704. LIST_HEAD(dispose);
  705. again:
  706. spin_lock(&sb->s_inode_list_lock);
  707. list_for_each_entry_safe(inode, next, &sb->s_inodes, i_sb_list) {
  708. if (atomic_read(&inode->i_count))
  709. continue;
  710. spin_lock(&inode->i_lock);
  711. if (atomic_read(&inode->i_count)) {
  712. spin_unlock(&inode->i_lock);
  713. continue;
  714. }
  715. if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) {
  716. spin_unlock(&inode->i_lock);
  717. continue;
  718. }
  719. inode->i_state |= I_FREEING;
  720. inode_lru_list_del(inode);
  721. spin_unlock(&inode->i_lock);
  722. list_add(&inode->i_lru, &dispose);
  723. /*
  724. * We can have a ton of inodes to evict at unmount time given
  725. * enough memory, check to see if we need to go to sleep for a
  726. * bit so we don't livelock.
  727. */
  728. if (need_resched()) {
  729. spin_unlock(&sb->s_inode_list_lock);
  730. cond_resched();
  731. dispose_list(&dispose);
  732. goto again;
  733. }
  734. }
  735. spin_unlock(&sb->s_inode_list_lock);
  736. dispose_list(&dispose);
  737. }
  738. EXPORT_SYMBOL_GPL(evict_inodes);
  739. /**
  740. * invalidate_inodes - attempt to free all inodes on a superblock
  741. * @sb: superblock to operate on
  742. *
  743. * Attempts to free all inodes (including dirty inodes) for a given superblock.
  744. */
  745. void invalidate_inodes(struct super_block *sb)
  746. {
  747. struct inode *inode, *next;
  748. LIST_HEAD(dispose);
  749. again:
  750. spin_lock(&sb->s_inode_list_lock);
  751. list_for_each_entry_safe(inode, next, &sb->s_inodes, i_sb_list) {
  752. spin_lock(&inode->i_lock);
  753. if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) {
  754. spin_unlock(&inode->i_lock);
  755. continue;
  756. }
  757. if (atomic_read(&inode->i_count)) {
  758. spin_unlock(&inode->i_lock);
  759. continue;
  760. }
  761. inode->i_state |= I_FREEING;
  762. inode_lru_list_del(inode);
  763. spin_unlock(&inode->i_lock);
  764. list_add(&inode->i_lru, &dispose);
  765. if (need_resched()) {
  766. spin_unlock(&sb->s_inode_list_lock);
  767. cond_resched();
  768. dispose_list(&dispose);
  769. goto again;
  770. }
  771. }
  772. spin_unlock(&sb->s_inode_list_lock);
  773. dispose_list(&dispose);
  774. }
  775. /*
  776. * Isolate the inode from the LRU in preparation for freeing it.
  777. *
  778. * If the inode has the I_REFERENCED flag set, then it means that it has been
  779. * used recently - the flag is set in iput_final(). When we encounter such an
  780. * inode, clear the flag and move it to the back of the LRU so it gets another
  781. * pass through the LRU before it gets reclaimed. This is necessary because of
  782. * the fact we are doing lazy LRU updates to minimise lock contention so the
  783. * LRU does not have strict ordering. Hence we don't want to reclaim inodes
  784. * with this flag set because they are the inodes that are out of order.
  785. */
  786. static enum lru_status inode_lru_isolate(struct list_head *item,
  787. struct list_lru_one *lru, spinlock_t *lru_lock, void *arg)
  788. {
  789. struct list_head *freeable = arg;
  790. struct inode *inode = container_of(item, struct inode, i_lru);
  791. /*
  792. * We are inverting the lru lock/inode->i_lock here, so use a
  793. * trylock. If we fail to get the lock, just skip it.
  794. */
  795. if (!spin_trylock(&inode->i_lock))
  796. return LRU_SKIP;
  797. /*
  798. * Inodes can get referenced, redirtied, or repopulated while
  799. * they're already on the LRU, and this can make them
  800. * unreclaimable for a while. Remove them lazily here; iput,
  801. * sync, or the last page cache deletion will requeue them.
  802. */
  803. if (atomic_read(&inode->i_count) ||
  804. (inode->i_state & ~I_REFERENCED) ||
  805. !mapping_shrinkable(&inode->i_data)) {
  806. list_lru_isolate(lru, &inode->i_lru);
  807. spin_unlock(&inode->i_lock);
  808. this_cpu_dec(nr_unused);
  809. return LRU_REMOVED;
  810. }
  811. /* Recently referenced inodes get one more pass */
  812. if (inode->i_state & I_REFERENCED) {
  813. inode->i_state &= ~I_REFERENCED;
  814. spin_unlock(&inode->i_lock);
  815. return LRU_ROTATE;
  816. }
  817. /*
  818. * On highmem systems, mapping_shrinkable() permits dropping
  819. * page cache in order to free up struct inodes: lowmem might
  820. * be under pressure before the cache inside the highmem zone.
  821. */
  822. if (inode_has_buffers(inode) || !mapping_empty(&inode->i_data)) {
  823. inode_pin_lru_isolating(inode);
  824. spin_unlock(&inode->i_lock);
  825. spin_unlock(lru_lock);
  826. if (remove_inode_buffers(inode)) {
  827. unsigned long reap;
  828. reap = invalidate_mapping_pages(&inode->i_data, 0, -1);
  829. if (current_is_kswapd())
  830. __count_vm_events(KSWAPD_INODESTEAL, reap);
  831. else
  832. __count_vm_events(PGINODESTEAL, reap);
  833. mm_account_reclaimed_pages(reap);
  834. }
  835. inode_unpin_lru_isolating(inode);
  836. spin_lock(lru_lock);
  837. return LRU_RETRY;
  838. }
  839. WARN_ON(inode->i_state & I_NEW);
  840. inode->i_state |= I_FREEING;
  841. list_lru_isolate_move(lru, &inode->i_lru, freeable);
  842. spin_unlock(&inode->i_lock);
  843. this_cpu_dec(nr_unused);
  844. return LRU_REMOVED;
  845. }
  846. /*
  847. * Walk the superblock inode LRU for freeable inodes and attempt to free them.
  848. * This is called from the superblock shrinker function with a number of inodes
  849. * to trim from the LRU. Inodes to be freed are moved to a temporary list and
  850. * then are freed outside inode_lock by dispose_list().
  851. */
  852. long prune_icache_sb(struct super_block *sb, struct shrink_control *sc)
  853. {
  854. LIST_HEAD(freeable);
  855. long freed;
  856. freed = list_lru_shrink_walk(&sb->s_inode_lru, sc,
  857. inode_lru_isolate, &freeable);
  858. dispose_list(&freeable);
  859. return freed;
  860. }
  861. static void __wait_on_freeing_inode(struct inode *inode, bool is_inode_hash_locked);
  862. /*
  863. * Called with the inode lock held.
  864. */
  865. static struct inode *find_inode(struct super_block *sb,
  866. struct hlist_head *head,
  867. int (*test)(struct inode *, void *),
  868. void *data, bool is_inode_hash_locked)
  869. {
  870. struct inode *inode = NULL;
  871. if (is_inode_hash_locked)
  872. lockdep_assert_held(&inode_hash_lock);
  873. else
  874. lockdep_assert_not_held(&inode_hash_lock);
  875. rcu_read_lock();
  876. repeat:
  877. hlist_for_each_entry_rcu(inode, head, i_hash) {
  878. if (inode->i_sb != sb)
  879. continue;
  880. if (!test(inode, data))
  881. continue;
  882. spin_lock(&inode->i_lock);
  883. if (inode->i_state & (I_FREEING|I_WILL_FREE)) {
  884. __wait_on_freeing_inode(inode, is_inode_hash_locked);
  885. goto repeat;
  886. }
  887. if (unlikely(inode->i_state & I_CREATING)) {
  888. spin_unlock(&inode->i_lock);
  889. rcu_read_unlock();
  890. return ERR_PTR(-ESTALE);
  891. }
  892. __iget(inode);
  893. spin_unlock(&inode->i_lock);
  894. rcu_read_unlock();
  895. return inode;
  896. }
  897. rcu_read_unlock();
  898. return NULL;
  899. }
  900. /*
  901. * find_inode_fast is the fast path version of find_inode, see the comment at
  902. * iget_locked for details.
  903. */
  904. static struct inode *find_inode_fast(struct super_block *sb,
  905. struct hlist_head *head, unsigned long ino,
  906. bool is_inode_hash_locked)
  907. {
  908. struct inode *inode = NULL;
  909. if (is_inode_hash_locked)
  910. lockdep_assert_held(&inode_hash_lock);
  911. else
  912. lockdep_assert_not_held(&inode_hash_lock);
  913. rcu_read_lock();
  914. repeat:
  915. hlist_for_each_entry_rcu(inode, head, i_hash) {
  916. if (inode->i_ino != ino)
  917. continue;
  918. if (inode->i_sb != sb)
  919. continue;
  920. spin_lock(&inode->i_lock);
  921. if (inode->i_state & (I_FREEING|I_WILL_FREE)) {
  922. __wait_on_freeing_inode(inode, is_inode_hash_locked);
  923. goto repeat;
  924. }
  925. if (unlikely(inode->i_state & I_CREATING)) {
  926. spin_unlock(&inode->i_lock);
  927. rcu_read_unlock();
  928. return ERR_PTR(-ESTALE);
  929. }
  930. __iget(inode);
  931. spin_unlock(&inode->i_lock);
  932. rcu_read_unlock();
  933. return inode;
  934. }
  935. rcu_read_unlock();
  936. return NULL;
  937. }
  938. /*
  939. * Each cpu owns a range of LAST_INO_BATCH numbers.
  940. * 'shared_last_ino' is dirtied only once out of LAST_INO_BATCH allocations,
  941. * to renew the exhausted range.
  942. *
  943. * This does not significantly increase overflow rate because every CPU can
  944. * consume at most LAST_INO_BATCH-1 unused inode numbers. So there is
  945. * NR_CPUS*(LAST_INO_BATCH-1) wastage. At 4096 and 1024, this is ~0.1% of the
  946. * 2^32 range, and is a worst-case. Even a 50% wastage would only increase
  947. * overflow rate by 2x, which does not seem too significant.
  948. *
  949. * On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW
  950. * error if st_ino won't fit in target struct field. Use 32bit counter
  951. * here to attempt to avoid that.
  952. */
  953. #define LAST_INO_BATCH 1024
  954. static DEFINE_PER_CPU(unsigned int, last_ino);
  955. unsigned int get_next_ino(void)
  956. {
  957. unsigned int *p = &get_cpu_var(last_ino);
  958. unsigned int res = *p;
  959. #ifdef CONFIG_SMP
  960. if (unlikely((res & (LAST_INO_BATCH-1)) == 0)) {
  961. static atomic_t shared_last_ino;
  962. int next = atomic_add_return(LAST_INO_BATCH, &shared_last_ino);
  963. res = next - LAST_INO_BATCH;
  964. }
  965. #endif
  966. res++;
  967. /* get_next_ino should not provide a 0 inode number */
  968. if (unlikely(!res))
  969. res++;
  970. *p = res;
  971. put_cpu_var(last_ino);
  972. return res;
  973. }
  974. EXPORT_SYMBOL(get_next_ino);
  975. /**
  976. * new_inode_pseudo - obtain an inode
  977. * @sb: superblock
  978. *
  979. * Allocates a new inode for given superblock.
  980. * Inode wont be chained in superblock s_inodes list
  981. * This means :
  982. * - fs can't be unmount
  983. * - quotas, fsnotify, writeback can't work
  984. */
  985. struct inode *new_inode_pseudo(struct super_block *sb)
  986. {
  987. return alloc_inode(sb);
  988. }
  989. /**
  990. * new_inode - obtain an inode
  991. * @sb: superblock
  992. *
  993. * Allocates a new inode for given superblock. The default gfp_mask
  994. * for allocations related to inode->i_mapping is GFP_HIGHUSER_MOVABLE.
  995. * If HIGHMEM pages are unsuitable or it is known that pages allocated
  996. * for the page cache are not reclaimable or migratable,
  997. * mapping_set_gfp_mask() must be called with suitable flags on the
  998. * newly created inode's mapping
  999. *
  1000. */
  1001. struct inode *new_inode(struct super_block *sb)
  1002. {
  1003. struct inode *inode;
  1004. inode = new_inode_pseudo(sb);
  1005. if (inode)
  1006. inode_sb_list_add(inode);
  1007. return inode;
  1008. }
  1009. EXPORT_SYMBOL(new_inode);
  1010. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  1011. void lockdep_annotate_inode_mutex_key(struct inode *inode)
  1012. {
  1013. if (S_ISDIR(inode->i_mode)) {
  1014. struct file_system_type *type = inode->i_sb->s_type;
  1015. /* Set new key only if filesystem hasn't already changed it */
  1016. if (lockdep_match_class(&inode->i_rwsem, &type->i_mutex_key)) {
  1017. /*
  1018. * ensure nobody is actually holding i_mutex
  1019. */
  1020. // mutex_destroy(&inode->i_mutex);
  1021. init_rwsem(&inode->i_rwsem);
  1022. lockdep_set_class(&inode->i_rwsem,
  1023. &type->i_mutex_dir_key);
  1024. }
  1025. }
  1026. }
  1027. EXPORT_SYMBOL(lockdep_annotate_inode_mutex_key);
  1028. #endif
  1029. /**
  1030. * unlock_new_inode - clear the I_NEW state and wake up any waiters
  1031. * @inode: new inode to unlock
  1032. *
  1033. * Called when the inode is fully initialised to clear the new state of the
  1034. * inode and wake up anyone waiting for the inode to finish initialisation.
  1035. */
  1036. void unlock_new_inode(struct inode *inode)
  1037. {
  1038. lockdep_annotate_inode_mutex_key(inode);
  1039. spin_lock(&inode->i_lock);
  1040. WARN_ON(!(inode->i_state & I_NEW));
  1041. inode->i_state &= ~I_NEW & ~I_CREATING;
  1042. /*
  1043. * Pairs with the barrier in prepare_to_wait_event() to make sure
  1044. * ___wait_var_event() either sees the bit cleared or
  1045. * waitqueue_active() check in wake_up_var() sees the waiter.
  1046. */
  1047. smp_mb();
  1048. inode_wake_up_bit(inode, __I_NEW);
  1049. spin_unlock(&inode->i_lock);
  1050. }
  1051. EXPORT_SYMBOL(unlock_new_inode);
  1052. void discard_new_inode(struct inode *inode)
  1053. {
  1054. lockdep_annotate_inode_mutex_key(inode);
  1055. spin_lock(&inode->i_lock);
  1056. WARN_ON(!(inode->i_state & I_NEW));
  1057. inode->i_state &= ~I_NEW;
  1058. /*
  1059. * Pairs with the barrier in prepare_to_wait_event() to make sure
  1060. * ___wait_var_event() either sees the bit cleared or
  1061. * waitqueue_active() check in wake_up_var() sees the waiter.
  1062. */
  1063. smp_mb();
  1064. inode_wake_up_bit(inode, __I_NEW);
  1065. spin_unlock(&inode->i_lock);
  1066. iput(inode);
  1067. }
  1068. EXPORT_SYMBOL(discard_new_inode);
  1069. /**
  1070. * lock_two_nondirectories - take two i_mutexes on non-directory objects
  1071. *
  1072. * Lock any non-NULL argument. Passed objects must not be directories.
  1073. * Zero, one or two objects may be locked by this function.
  1074. *
  1075. * @inode1: first inode to lock
  1076. * @inode2: second inode to lock
  1077. */
  1078. void lock_two_nondirectories(struct inode *inode1, struct inode *inode2)
  1079. {
  1080. if (inode1)
  1081. WARN_ON_ONCE(S_ISDIR(inode1->i_mode));
  1082. if (inode2)
  1083. WARN_ON_ONCE(S_ISDIR(inode2->i_mode));
  1084. if (inode1 > inode2)
  1085. swap(inode1, inode2);
  1086. if (inode1)
  1087. inode_lock(inode1);
  1088. if (inode2 && inode2 != inode1)
  1089. inode_lock_nested(inode2, I_MUTEX_NONDIR2);
  1090. }
  1091. EXPORT_SYMBOL(lock_two_nondirectories);
  1092. /**
  1093. * unlock_two_nondirectories - release locks from lock_two_nondirectories()
  1094. * @inode1: first inode to unlock
  1095. * @inode2: second inode to unlock
  1096. */
  1097. void unlock_two_nondirectories(struct inode *inode1, struct inode *inode2)
  1098. {
  1099. if (inode1) {
  1100. WARN_ON_ONCE(S_ISDIR(inode1->i_mode));
  1101. inode_unlock(inode1);
  1102. }
  1103. if (inode2 && inode2 != inode1) {
  1104. WARN_ON_ONCE(S_ISDIR(inode2->i_mode));
  1105. inode_unlock(inode2);
  1106. }
  1107. }
  1108. EXPORT_SYMBOL(unlock_two_nondirectories);
  1109. /**
  1110. * inode_insert5 - obtain an inode from a mounted file system
  1111. * @inode: pre-allocated inode to use for insert to cache
  1112. * @hashval: hash value (usually inode number) to get
  1113. * @test: callback used for comparisons between inodes
  1114. * @set: callback used to initialize a new struct inode
  1115. * @data: opaque data pointer to pass to @test and @set
  1116. *
  1117. * Search for the inode specified by @hashval and @data in the inode cache,
  1118. * and if present it is return it with an increased reference count. This is
  1119. * a variant of iget5_locked() for callers that don't want to fail on memory
  1120. * allocation of inode.
  1121. *
  1122. * If the inode is not in cache, insert the pre-allocated inode to cache and
  1123. * return it locked, hashed, and with the I_NEW flag set. The file system gets
  1124. * to fill it in before unlocking it via unlock_new_inode().
  1125. *
  1126. * Note both @test and @set are called with the inode_hash_lock held, so can't
  1127. * sleep.
  1128. */
  1129. struct inode *inode_insert5(struct inode *inode, unsigned long hashval,
  1130. int (*test)(struct inode *, void *),
  1131. int (*set)(struct inode *, void *), void *data)
  1132. {
  1133. struct hlist_head *head = inode_hashtable + hash(inode->i_sb, hashval);
  1134. struct inode *old;
  1135. again:
  1136. spin_lock(&inode_hash_lock);
  1137. old = find_inode(inode->i_sb, head, test, data, true);
  1138. if (unlikely(old)) {
  1139. /*
  1140. * Uhhuh, somebody else created the same inode under us.
  1141. * Use the old inode instead of the preallocated one.
  1142. */
  1143. spin_unlock(&inode_hash_lock);
  1144. if (IS_ERR(old))
  1145. return NULL;
  1146. wait_on_inode(old);
  1147. if (unlikely(inode_unhashed(old))) {
  1148. iput(old);
  1149. goto again;
  1150. }
  1151. return old;
  1152. }
  1153. if (set && unlikely(set(inode, data))) {
  1154. inode = NULL;
  1155. goto unlock;
  1156. }
  1157. /*
  1158. * Return the locked inode with I_NEW set, the
  1159. * caller is responsible for filling in the contents
  1160. */
  1161. spin_lock(&inode->i_lock);
  1162. inode->i_state |= I_NEW;
  1163. hlist_add_head_rcu(&inode->i_hash, head);
  1164. spin_unlock(&inode->i_lock);
  1165. /*
  1166. * Add inode to the sb list if it's not already. It has I_NEW at this
  1167. * point, so it should be safe to test i_sb_list locklessly.
  1168. */
  1169. if (list_empty(&inode->i_sb_list))
  1170. inode_sb_list_add(inode);
  1171. unlock:
  1172. spin_unlock(&inode_hash_lock);
  1173. return inode;
  1174. }
  1175. EXPORT_SYMBOL(inode_insert5);
  1176. /**
  1177. * iget5_locked - obtain an inode from a mounted file system
  1178. * @sb: super block of file system
  1179. * @hashval: hash value (usually inode number) to get
  1180. * @test: callback used for comparisons between inodes
  1181. * @set: callback used to initialize a new struct inode
  1182. * @data: opaque data pointer to pass to @test and @set
  1183. *
  1184. * Search for the inode specified by @hashval and @data in the inode cache,
  1185. * and if present it is return it with an increased reference count. This is
  1186. * a generalized version of iget_locked() for file systems where the inode
  1187. * number is not sufficient for unique identification of an inode.
  1188. *
  1189. * If the inode is not in cache, allocate a new inode and return it locked,
  1190. * hashed, and with the I_NEW flag set. The file system gets to fill it in
  1191. * before unlocking it via unlock_new_inode().
  1192. *
  1193. * Note both @test and @set are called with the inode_hash_lock held, so can't
  1194. * sleep.
  1195. */
  1196. struct inode *iget5_locked(struct super_block *sb, unsigned long hashval,
  1197. int (*test)(struct inode *, void *),
  1198. int (*set)(struct inode *, void *), void *data)
  1199. {
  1200. struct inode *inode = ilookup5(sb, hashval, test, data);
  1201. if (!inode) {
  1202. struct inode *new = alloc_inode(sb);
  1203. if (new) {
  1204. inode = inode_insert5(new, hashval, test, set, data);
  1205. if (unlikely(inode != new))
  1206. destroy_inode(new);
  1207. }
  1208. }
  1209. return inode;
  1210. }
  1211. EXPORT_SYMBOL(iget5_locked);
  1212. /**
  1213. * iget5_locked_rcu - obtain an inode from a mounted file system
  1214. * @sb: super block of file system
  1215. * @hashval: hash value (usually inode number) to get
  1216. * @test: callback used for comparisons between inodes
  1217. * @set: callback used to initialize a new struct inode
  1218. * @data: opaque data pointer to pass to @test and @set
  1219. *
  1220. * This is equivalent to iget5_locked, except the @test callback must
  1221. * tolerate the inode not being stable, including being mid-teardown.
  1222. */
  1223. struct inode *iget5_locked_rcu(struct super_block *sb, unsigned long hashval,
  1224. int (*test)(struct inode *, void *),
  1225. int (*set)(struct inode *, void *), void *data)
  1226. {
  1227. struct hlist_head *head = inode_hashtable + hash(sb, hashval);
  1228. struct inode *inode, *new;
  1229. again:
  1230. inode = find_inode(sb, head, test, data, false);
  1231. if (inode) {
  1232. if (IS_ERR(inode))
  1233. return NULL;
  1234. wait_on_inode(inode);
  1235. if (unlikely(inode_unhashed(inode))) {
  1236. iput(inode);
  1237. goto again;
  1238. }
  1239. return inode;
  1240. }
  1241. new = alloc_inode(sb);
  1242. if (new) {
  1243. inode = inode_insert5(new, hashval, test, set, data);
  1244. if (unlikely(inode != new))
  1245. destroy_inode(new);
  1246. }
  1247. return inode;
  1248. }
  1249. EXPORT_SYMBOL_GPL(iget5_locked_rcu);
  1250. /**
  1251. * iget_locked - obtain an inode from a mounted file system
  1252. * @sb: super block of file system
  1253. * @ino: inode number to get
  1254. *
  1255. * Search for the inode specified by @ino in the inode cache and if present
  1256. * return it with an increased reference count. This is for file systems
  1257. * where the inode number is sufficient for unique identification of an inode.
  1258. *
  1259. * If the inode is not in cache, allocate a new inode and return it locked,
  1260. * hashed, and with the I_NEW flag set. The file system gets to fill it in
  1261. * before unlocking it via unlock_new_inode().
  1262. */
  1263. struct inode *iget_locked(struct super_block *sb, unsigned long ino)
  1264. {
  1265. struct hlist_head *head = inode_hashtable + hash(sb, ino);
  1266. struct inode *inode;
  1267. again:
  1268. inode = find_inode_fast(sb, head, ino, false);
  1269. if (inode) {
  1270. if (IS_ERR(inode))
  1271. return NULL;
  1272. wait_on_inode(inode);
  1273. if (unlikely(inode_unhashed(inode))) {
  1274. iput(inode);
  1275. goto again;
  1276. }
  1277. return inode;
  1278. }
  1279. inode = alloc_inode(sb);
  1280. if (inode) {
  1281. struct inode *old;
  1282. spin_lock(&inode_hash_lock);
  1283. /* We released the lock, so.. */
  1284. old = find_inode_fast(sb, head, ino, true);
  1285. if (!old) {
  1286. inode->i_ino = ino;
  1287. spin_lock(&inode->i_lock);
  1288. inode->i_state = I_NEW;
  1289. hlist_add_head_rcu(&inode->i_hash, head);
  1290. spin_unlock(&inode->i_lock);
  1291. inode_sb_list_add(inode);
  1292. spin_unlock(&inode_hash_lock);
  1293. /* Return the locked inode with I_NEW set, the
  1294. * caller is responsible for filling in the contents
  1295. */
  1296. return inode;
  1297. }
  1298. /*
  1299. * Uhhuh, somebody else created the same inode under
  1300. * us. Use the old inode instead of the one we just
  1301. * allocated.
  1302. */
  1303. spin_unlock(&inode_hash_lock);
  1304. destroy_inode(inode);
  1305. if (IS_ERR(old))
  1306. return NULL;
  1307. inode = old;
  1308. wait_on_inode(inode);
  1309. if (unlikely(inode_unhashed(inode))) {
  1310. iput(inode);
  1311. goto again;
  1312. }
  1313. }
  1314. return inode;
  1315. }
  1316. EXPORT_SYMBOL(iget_locked);
  1317. /*
  1318. * search the inode cache for a matching inode number.
  1319. * If we find one, then the inode number we are trying to
  1320. * allocate is not unique and so we should not use it.
  1321. *
  1322. * Returns 1 if the inode number is unique, 0 if it is not.
  1323. */
  1324. static int test_inode_iunique(struct super_block *sb, unsigned long ino)
  1325. {
  1326. struct hlist_head *b = inode_hashtable + hash(sb, ino);
  1327. struct inode *inode;
  1328. hlist_for_each_entry_rcu(inode, b, i_hash) {
  1329. if (inode->i_ino == ino && inode->i_sb == sb)
  1330. return 0;
  1331. }
  1332. return 1;
  1333. }
  1334. /**
  1335. * iunique - get a unique inode number
  1336. * @sb: superblock
  1337. * @max_reserved: highest reserved inode number
  1338. *
  1339. * Obtain an inode number that is unique on the system for a given
  1340. * superblock. This is used by file systems that have no natural
  1341. * permanent inode numbering system. An inode number is returned that
  1342. * is higher than the reserved limit but unique.
  1343. *
  1344. * BUGS:
  1345. * With a large number of inodes live on the file system this function
  1346. * currently becomes quite slow.
  1347. */
  1348. ino_t iunique(struct super_block *sb, ino_t max_reserved)
  1349. {
  1350. /*
  1351. * On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW
  1352. * error if st_ino won't fit in target struct field. Use 32bit counter
  1353. * here to attempt to avoid that.
  1354. */
  1355. static DEFINE_SPINLOCK(iunique_lock);
  1356. static unsigned int counter;
  1357. ino_t res;
  1358. rcu_read_lock();
  1359. spin_lock(&iunique_lock);
  1360. do {
  1361. if (counter <= max_reserved)
  1362. counter = max_reserved + 1;
  1363. res = counter++;
  1364. } while (!test_inode_iunique(sb, res));
  1365. spin_unlock(&iunique_lock);
  1366. rcu_read_unlock();
  1367. return res;
  1368. }
  1369. EXPORT_SYMBOL(iunique);
  1370. struct inode *igrab(struct inode *inode)
  1371. {
  1372. spin_lock(&inode->i_lock);
  1373. if (!(inode->i_state & (I_FREEING|I_WILL_FREE))) {
  1374. __iget(inode);
  1375. spin_unlock(&inode->i_lock);
  1376. } else {
  1377. spin_unlock(&inode->i_lock);
  1378. /*
  1379. * Handle the case where s_op->clear_inode is not been
  1380. * called yet, and somebody is calling igrab
  1381. * while the inode is getting freed.
  1382. */
  1383. inode = NULL;
  1384. }
  1385. return inode;
  1386. }
  1387. EXPORT_SYMBOL(igrab);
  1388. /**
  1389. * ilookup5_nowait - search for an inode in the inode cache
  1390. * @sb: super block of file system to search
  1391. * @hashval: hash value (usually inode number) to search for
  1392. * @test: callback used for comparisons between inodes
  1393. * @data: opaque data pointer to pass to @test
  1394. *
  1395. * Search for the inode specified by @hashval and @data in the inode cache.
  1396. * If the inode is in the cache, the inode is returned with an incremented
  1397. * reference count.
  1398. *
  1399. * Note: I_NEW is not waited upon so you have to be very careful what you do
  1400. * with the returned inode. You probably should be using ilookup5() instead.
  1401. *
  1402. * Note2: @test is called with the inode_hash_lock held, so can't sleep.
  1403. */
  1404. struct inode *ilookup5_nowait(struct super_block *sb, unsigned long hashval,
  1405. int (*test)(struct inode *, void *), void *data)
  1406. {
  1407. struct hlist_head *head = inode_hashtable + hash(sb, hashval);
  1408. struct inode *inode;
  1409. spin_lock(&inode_hash_lock);
  1410. inode = find_inode(sb, head, test, data, true);
  1411. spin_unlock(&inode_hash_lock);
  1412. return IS_ERR(inode) ? NULL : inode;
  1413. }
  1414. EXPORT_SYMBOL(ilookup5_nowait);
  1415. /**
  1416. * ilookup5 - search for an inode in the inode cache
  1417. * @sb: super block of file system to search
  1418. * @hashval: hash value (usually inode number) to search for
  1419. * @test: callback used for comparisons between inodes
  1420. * @data: opaque data pointer to pass to @test
  1421. *
  1422. * Search for the inode specified by @hashval and @data in the inode cache,
  1423. * and if the inode is in the cache, return the inode with an incremented
  1424. * reference count. Waits on I_NEW before returning the inode.
  1425. * returned with an incremented reference count.
  1426. *
  1427. * This is a generalized version of ilookup() for file systems where the
  1428. * inode number is not sufficient for unique identification of an inode.
  1429. *
  1430. * Note: @test is called with the inode_hash_lock held, so can't sleep.
  1431. */
  1432. struct inode *ilookup5(struct super_block *sb, unsigned long hashval,
  1433. int (*test)(struct inode *, void *), void *data)
  1434. {
  1435. struct inode *inode;
  1436. again:
  1437. inode = ilookup5_nowait(sb, hashval, test, data);
  1438. if (inode) {
  1439. wait_on_inode(inode);
  1440. if (unlikely(inode_unhashed(inode))) {
  1441. iput(inode);
  1442. goto again;
  1443. }
  1444. }
  1445. return inode;
  1446. }
  1447. EXPORT_SYMBOL(ilookup5);
  1448. /**
  1449. * ilookup - search for an inode in the inode cache
  1450. * @sb: super block of file system to search
  1451. * @ino: inode number to search for
  1452. *
  1453. * Search for the inode @ino in the inode cache, and if the inode is in the
  1454. * cache, the inode is returned with an incremented reference count.
  1455. */
  1456. struct inode *ilookup(struct super_block *sb, unsigned long ino)
  1457. {
  1458. struct hlist_head *head = inode_hashtable + hash(sb, ino);
  1459. struct inode *inode;
  1460. again:
  1461. inode = find_inode_fast(sb, head, ino, false);
  1462. if (inode) {
  1463. if (IS_ERR(inode))
  1464. return NULL;
  1465. wait_on_inode(inode);
  1466. if (unlikely(inode_unhashed(inode))) {
  1467. iput(inode);
  1468. goto again;
  1469. }
  1470. }
  1471. return inode;
  1472. }
  1473. EXPORT_SYMBOL(ilookup);
  1474. /**
  1475. * find_inode_nowait - find an inode in the inode cache
  1476. * @sb: super block of file system to search
  1477. * @hashval: hash value (usually inode number) to search for
  1478. * @match: callback used for comparisons between inodes
  1479. * @data: opaque data pointer to pass to @match
  1480. *
  1481. * Search for the inode specified by @hashval and @data in the inode
  1482. * cache, where the helper function @match will return 0 if the inode
  1483. * does not match, 1 if the inode does match, and -1 if the search
  1484. * should be stopped. The @match function must be responsible for
  1485. * taking the i_lock spin_lock and checking i_state for an inode being
  1486. * freed or being initialized, and incrementing the reference count
  1487. * before returning 1. It also must not sleep, since it is called with
  1488. * the inode_hash_lock spinlock held.
  1489. *
  1490. * This is a even more generalized version of ilookup5() when the
  1491. * function must never block --- find_inode() can block in
  1492. * __wait_on_freeing_inode() --- or when the caller can not increment
  1493. * the reference count because the resulting iput() might cause an
  1494. * inode eviction. The tradeoff is that the @match funtion must be
  1495. * very carefully implemented.
  1496. */
  1497. struct inode *find_inode_nowait(struct super_block *sb,
  1498. unsigned long hashval,
  1499. int (*match)(struct inode *, unsigned long,
  1500. void *),
  1501. void *data)
  1502. {
  1503. struct hlist_head *head = inode_hashtable + hash(sb, hashval);
  1504. struct inode *inode, *ret_inode = NULL;
  1505. int mval;
  1506. spin_lock(&inode_hash_lock);
  1507. hlist_for_each_entry(inode, head, i_hash) {
  1508. if (inode->i_sb != sb)
  1509. continue;
  1510. mval = match(inode, hashval, data);
  1511. if (mval == 0)
  1512. continue;
  1513. if (mval == 1)
  1514. ret_inode = inode;
  1515. goto out;
  1516. }
  1517. out:
  1518. spin_unlock(&inode_hash_lock);
  1519. return ret_inode;
  1520. }
  1521. EXPORT_SYMBOL(find_inode_nowait);
  1522. /**
  1523. * find_inode_rcu - find an inode in the inode cache
  1524. * @sb: Super block of file system to search
  1525. * @hashval: Key to hash
  1526. * @test: Function to test match on an inode
  1527. * @data: Data for test function
  1528. *
  1529. * Search for the inode specified by @hashval and @data in the inode cache,
  1530. * where the helper function @test will return 0 if the inode does not match
  1531. * and 1 if it does. The @test function must be responsible for taking the
  1532. * i_lock spin_lock and checking i_state for an inode being freed or being
  1533. * initialized.
  1534. *
  1535. * If successful, this will return the inode for which the @test function
  1536. * returned 1 and NULL otherwise.
  1537. *
  1538. * The @test function is not permitted to take a ref on any inode presented.
  1539. * It is also not permitted to sleep.
  1540. *
  1541. * The caller must hold the RCU read lock.
  1542. */
  1543. struct inode *find_inode_rcu(struct super_block *sb, unsigned long hashval,
  1544. int (*test)(struct inode *, void *), void *data)
  1545. {
  1546. struct hlist_head *head = inode_hashtable + hash(sb, hashval);
  1547. struct inode *inode;
  1548. RCU_LOCKDEP_WARN(!rcu_read_lock_held(),
  1549. "suspicious find_inode_rcu() usage");
  1550. hlist_for_each_entry_rcu(inode, head, i_hash) {
  1551. if (inode->i_sb == sb &&
  1552. !(READ_ONCE(inode->i_state) & (I_FREEING | I_WILL_FREE)) &&
  1553. test(inode, data))
  1554. return inode;
  1555. }
  1556. return NULL;
  1557. }
  1558. EXPORT_SYMBOL(find_inode_rcu);
  1559. /**
  1560. * find_inode_by_ino_rcu - Find an inode in the inode cache
  1561. * @sb: Super block of file system to search
  1562. * @ino: The inode number to match
  1563. *
  1564. * Search for the inode specified by @hashval and @data in the inode cache,
  1565. * where the helper function @test will return 0 if the inode does not match
  1566. * and 1 if it does. The @test function must be responsible for taking the
  1567. * i_lock spin_lock and checking i_state for an inode being freed or being
  1568. * initialized.
  1569. *
  1570. * If successful, this will return the inode for which the @test function
  1571. * returned 1 and NULL otherwise.
  1572. *
  1573. * The @test function is not permitted to take a ref on any inode presented.
  1574. * It is also not permitted to sleep.
  1575. *
  1576. * The caller must hold the RCU read lock.
  1577. */
  1578. struct inode *find_inode_by_ino_rcu(struct super_block *sb,
  1579. unsigned long ino)
  1580. {
  1581. struct hlist_head *head = inode_hashtable + hash(sb, ino);
  1582. struct inode *inode;
  1583. RCU_LOCKDEP_WARN(!rcu_read_lock_held(),
  1584. "suspicious find_inode_by_ino_rcu() usage");
  1585. hlist_for_each_entry_rcu(inode, head, i_hash) {
  1586. if (inode->i_ino == ino &&
  1587. inode->i_sb == sb &&
  1588. !(READ_ONCE(inode->i_state) & (I_FREEING | I_WILL_FREE)))
  1589. return inode;
  1590. }
  1591. return NULL;
  1592. }
  1593. EXPORT_SYMBOL(find_inode_by_ino_rcu);
  1594. int insert_inode_locked(struct inode *inode)
  1595. {
  1596. struct super_block *sb = inode->i_sb;
  1597. ino_t ino = inode->i_ino;
  1598. struct hlist_head *head = inode_hashtable + hash(sb, ino);
  1599. while (1) {
  1600. struct inode *old = NULL;
  1601. spin_lock(&inode_hash_lock);
  1602. hlist_for_each_entry(old, head, i_hash) {
  1603. if (old->i_ino != ino)
  1604. continue;
  1605. if (old->i_sb != sb)
  1606. continue;
  1607. spin_lock(&old->i_lock);
  1608. if (old->i_state & (I_FREEING|I_WILL_FREE)) {
  1609. spin_unlock(&old->i_lock);
  1610. continue;
  1611. }
  1612. break;
  1613. }
  1614. if (likely(!old)) {
  1615. spin_lock(&inode->i_lock);
  1616. inode->i_state |= I_NEW | I_CREATING;
  1617. hlist_add_head_rcu(&inode->i_hash, head);
  1618. spin_unlock(&inode->i_lock);
  1619. spin_unlock(&inode_hash_lock);
  1620. return 0;
  1621. }
  1622. if (unlikely(old->i_state & I_CREATING)) {
  1623. spin_unlock(&old->i_lock);
  1624. spin_unlock(&inode_hash_lock);
  1625. return -EBUSY;
  1626. }
  1627. __iget(old);
  1628. spin_unlock(&old->i_lock);
  1629. spin_unlock(&inode_hash_lock);
  1630. wait_on_inode(old);
  1631. if (unlikely(!inode_unhashed(old))) {
  1632. iput(old);
  1633. return -EBUSY;
  1634. }
  1635. iput(old);
  1636. }
  1637. }
  1638. EXPORT_SYMBOL(insert_inode_locked);
  1639. int insert_inode_locked4(struct inode *inode, unsigned long hashval,
  1640. int (*test)(struct inode *, void *), void *data)
  1641. {
  1642. struct inode *old;
  1643. inode->i_state |= I_CREATING;
  1644. old = inode_insert5(inode, hashval, test, NULL, data);
  1645. if (old != inode) {
  1646. iput(old);
  1647. return -EBUSY;
  1648. }
  1649. return 0;
  1650. }
  1651. EXPORT_SYMBOL(insert_inode_locked4);
  1652. int generic_delete_inode(struct inode *inode)
  1653. {
  1654. return 1;
  1655. }
  1656. EXPORT_SYMBOL(generic_delete_inode);
  1657. /*
  1658. * Called when we're dropping the last reference
  1659. * to an inode.
  1660. *
  1661. * Call the FS "drop_inode()" function, defaulting to
  1662. * the legacy UNIX filesystem behaviour. If it tells
  1663. * us to evict inode, do so. Otherwise, retain inode
  1664. * in cache if fs is alive, sync and evict if fs is
  1665. * shutting down.
  1666. */
  1667. static void iput_final(struct inode *inode)
  1668. {
  1669. struct super_block *sb = inode->i_sb;
  1670. const struct super_operations *op = inode->i_sb->s_op;
  1671. unsigned long state;
  1672. int drop;
  1673. WARN_ON(inode->i_state & I_NEW);
  1674. if (op->drop_inode)
  1675. drop = op->drop_inode(inode);
  1676. else
  1677. drop = generic_drop_inode(inode);
  1678. if (!drop &&
  1679. !(inode->i_state & I_DONTCACHE) &&
  1680. (sb->s_flags & SB_ACTIVE)) {
  1681. __inode_add_lru(inode, true);
  1682. spin_unlock(&inode->i_lock);
  1683. return;
  1684. }
  1685. state = inode->i_state;
  1686. if (!drop) {
  1687. WRITE_ONCE(inode->i_state, state | I_WILL_FREE);
  1688. spin_unlock(&inode->i_lock);
  1689. write_inode_now(inode, 1);
  1690. spin_lock(&inode->i_lock);
  1691. state = inode->i_state;
  1692. WARN_ON(state & I_NEW);
  1693. state &= ~I_WILL_FREE;
  1694. }
  1695. WRITE_ONCE(inode->i_state, state | I_FREEING);
  1696. if (!list_empty(&inode->i_lru))
  1697. inode_lru_list_del(inode);
  1698. spin_unlock(&inode->i_lock);
  1699. evict(inode);
  1700. }
  1701. /**
  1702. * iput - put an inode
  1703. * @inode: inode to put
  1704. *
  1705. * Puts an inode, dropping its usage count. If the inode use count hits
  1706. * zero, the inode is then freed and may also be destroyed.
  1707. *
  1708. * Consequently, iput() can sleep.
  1709. */
  1710. void iput(struct inode *inode)
  1711. {
  1712. if (!inode)
  1713. return;
  1714. BUG_ON(inode->i_state & I_CLEAR);
  1715. retry:
  1716. if (atomic_dec_and_lock(&inode->i_count, &inode->i_lock)) {
  1717. if (inode->i_nlink && (inode->i_state & I_DIRTY_TIME)) {
  1718. atomic_inc(&inode->i_count);
  1719. spin_unlock(&inode->i_lock);
  1720. trace_writeback_lazytime_iput(inode);
  1721. mark_inode_dirty_sync(inode);
  1722. goto retry;
  1723. }
  1724. iput_final(inode);
  1725. }
  1726. }
  1727. EXPORT_SYMBOL(iput);
  1728. #ifdef CONFIG_BLOCK
  1729. /**
  1730. * bmap - find a block number in a file
  1731. * @inode: inode owning the block number being requested
  1732. * @block: pointer containing the block to find
  1733. *
  1734. * Replaces the value in ``*block`` with the block number on the device holding
  1735. * corresponding to the requested block number in the file.
  1736. * That is, asked for block 4 of inode 1 the function will replace the
  1737. * 4 in ``*block``, with disk block relative to the disk start that holds that
  1738. * block of the file.
  1739. *
  1740. * Returns -EINVAL in case of error, 0 otherwise. If mapping falls into a
  1741. * hole, returns 0 and ``*block`` is also set to 0.
  1742. */
  1743. int bmap(struct inode *inode, sector_t *block)
  1744. {
  1745. if (!inode->i_mapping->a_ops->bmap)
  1746. return -EINVAL;
  1747. *block = inode->i_mapping->a_ops->bmap(inode->i_mapping, *block);
  1748. return 0;
  1749. }
  1750. EXPORT_SYMBOL(bmap);
  1751. #endif
  1752. /*
  1753. * With relative atime, only update atime if the previous atime is
  1754. * earlier than or equal to either the ctime or mtime,
  1755. * or if at least a day has passed since the last atime update.
  1756. */
  1757. static bool relatime_need_update(struct vfsmount *mnt, struct inode *inode,
  1758. struct timespec64 now)
  1759. {
  1760. struct timespec64 atime, mtime, ctime;
  1761. if (!(mnt->mnt_flags & MNT_RELATIME))
  1762. return true;
  1763. /*
  1764. * Is mtime younger than or equal to atime? If yes, update atime:
  1765. */
  1766. atime = inode_get_atime(inode);
  1767. mtime = inode_get_mtime(inode);
  1768. if (timespec64_compare(&mtime, &atime) >= 0)
  1769. return true;
  1770. /*
  1771. * Is ctime younger than or equal to atime? If yes, update atime:
  1772. */
  1773. ctime = inode_get_ctime(inode);
  1774. if (timespec64_compare(&ctime, &atime) >= 0)
  1775. return true;
  1776. /*
  1777. * Is the previous atime value older than a day? If yes,
  1778. * update atime:
  1779. */
  1780. if ((long)(now.tv_sec - atime.tv_sec) >= 24*60*60)
  1781. return true;
  1782. /*
  1783. * Good, we can skip the atime update:
  1784. */
  1785. return false;
  1786. }
  1787. /**
  1788. * inode_update_timestamps - update the timestamps on the inode
  1789. * @inode: inode to be updated
  1790. * @flags: S_* flags that needed to be updated
  1791. *
  1792. * The update_time function is called when an inode's timestamps need to be
  1793. * updated for a read or write operation. This function handles updating the
  1794. * actual timestamps. It's up to the caller to ensure that the inode is marked
  1795. * dirty appropriately.
  1796. *
  1797. * In the case where any of S_MTIME, S_CTIME, or S_VERSION need to be updated,
  1798. * attempt to update all three of them. S_ATIME updates can be handled
  1799. * independently of the rest.
  1800. *
  1801. * Returns a set of S_* flags indicating which values changed.
  1802. */
  1803. int inode_update_timestamps(struct inode *inode, int flags)
  1804. {
  1805. int updated = 0;
  1806. struct timespec64 now;
  1807. if (flags & (S_MTIME|S_CTIME|S_VERSION)) {
  1808. struct timespec64 ctime = inode_get_ctime(inode);
  1809. struct timespec64 mtime = inode_get_mtime(inode);
  1810. now = inode_set_ctime_current(inode);
  1811. if (!timespec64_equal(&now, &ctime))
  1812. updated |= S_CTIME;
  1813. if (!timespec64_equal(&now, &mtime)) {
  1814. inode_set_mtime_to_ts(inode, now);
  1815. updated |= S_MTIME;
  1816. }
  1817. if (IS_I_VERSION(inode) && inode_maybe_inc_iversion(inode, updated))
  1818. updated |= S_VERSION;
  1819. } else {
  1820. now = current_time(inode);
  1821. }
  1822. if (flags & S_ATIME) {
  1823. struct timespec64 atime = inode_get_atime(inode);
  1824. if (!timespec64_equal(&now, &atime)) {
  1825. inode_set_atime_to_ts(inode, now);
  1826. updated |= S_ATIME;
  1827. }
  1828. }
  1829. return updated;
  1830. }
  1831. EXPORT_SYMBOL(inode_update_timestamps);
  1832. /**
  1833. * generic_update_time - update the timestamps on the inode
  1834. * @inode: inode to be updated
  1835. * @flags: S_* flags that needed to be updated
  1836. *
  1837. * The update_time function is called when an inode's timestamps need to be
  1838. * updated for a read or write operation. In the case where any of S_MTIME, S_CTIME,
  1839. * or S_VERSION need to be updated we attempt to update all three of them. S_ATIME
  1840. * updates can be handled done independently of the rest.
  1841. *
  1842. * Returns a S_* mask indicating which fields were updated.
  1843. */
  1844. int generic_update_time(struct inode *inode, int flags)
  1845. {
  1846. int updated = inode_update_timestamps(inode, flags);
  1847. int dirty_flags = 0;
  1848. if (updated & (S_ATIME|S_MTIME|S_CTIME))
  1849. dirty_flags = inode->i_sb->s_flags & SB_LAZYTIME ? I_DIRTY_TIME : I_DIRTY_SYNC;
  1850. if (updated & S_VERSION)
  1851. dirty_flags |= I_DIRTY_SYNC;
  1852. __mark_inode_dirty(inode, dirty_flags);
  1853. return updated;
  1854. }
  1855. EXPORT_SYMBOL(generic_update_time);
  1856. /*
  1857. * This does the actual work of updating an inodes time or version. Must have
  1858. * had called mnt_want_write() before calling this.
  1859. */
  1860. int inode_update_time(struct inode *inode, int flags)
  1861. {
  1862. if (inode->i_op->update_time)
  1863. return inode->i_op->update_time(inode, flags);
  1864. generic_update_time(inode, flags);
  1865. return 0;
  1866. }
  1867. EXPORT_SYMBOL(inode_update_time);
  1868. /**
  1869. * atime_needs_update - update the access time
  1870. * @path: the &struct path to update
  1871. * @inode: inode to update
  1872. *
  1873. * Update the accessed time on an inode and mark it for writeback.
  1874. * This function automatically handles read only file systems and media,
  1875. * as well as the "noatime" flag and inode specific "noatime" markers.
  1876. */
  1877. bool atime_needs_update(const struct path *path, struct inode *inode)
  1878. {
  1879. struct vfsmount *mnt = path->mnt;
  1880. struct timespec64 now, atime;
  1881. if (inode->i_flags & S_NOATIME)
  1882. return false;
  1883. /* Atime updates will likely cause i_uid and i_gid to be written
  1884. * back improprely if their true value is unknown to the vfs.
  1885. */
  1886. if (HAS_UNMAPPED_ID(mnt_idmap(mnt), inode))
  1887. return false;
  1888. if (IS_NOATIME(inode))
  1889. return false;
  1890. if ((inode->i_sb->s_flags & SB_NODIRATIME) && S_ISDIR(inode->i_mode))
  1891. return false;
  1892. if (mnt->mnt_flags & MNT_NOATIME)
  1893. return false;
  1894. if ((mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode))
  1895. return false;
  1896. now = current_time(inode);
  1897. if (!relatime_need_update(mnt, inode, now))
  1898. return false;
  1899. atime = inode_get_atime(inode);
  1900. if (timespec64_equal(&atime, &now))
  1901. return false;
  1902. return true;
  1903. }
  1904. void touch_atime(const struct path *path)
  1905. {
  1906. struct vfsmount *mnt = path->mnt;
  1907. struct inode *inode = d_inode(path->dentry);
  1908. if (!atime_needs_update(path, inode))
  1909. return;
  1910. if (!sb_start_write_trylock(inode->i_sb))
  1911. return;
  1912. if (mnt_get_write_access(mnt) != 0)
  1913. goto skip_update;
  1914. /*
  1915. * File systems can error out when updating inodes if they need to
  1916. * allocate new space to modify an inode (such is the case for
  1917. * Btrfs), but since we touch atime while walking down the path we
  1918. * really don't care if we failed to update the atime of the file,
  1919. * so just ignore the return value.
  1920. * We may also fail on filesystems that have the ability to make parts
  1921. * of the fs read only, e.g. subvolumes in Btrfs.
  1922. */
  1923. inode_update_time(inode, S_ATIME);
  1924. mnt_put_write_access(mnt);
  1925. skip_update:
  1926. sb_end_write(inode->i_sb);
  1927. }
  1928. EXPORT_SYMBOL(touch_atime);
  1929. /*
  1930. * Return mask of changes for notify_change() that need to be done as a
  1931. * response to write or truncate. Return 0 if nothing has to be changed.
  1932. * Negative value on error (change should be denied).
  1933. */
  1934. int dentry_needs_remove_privs(struct mnt_idmap *idmap,
  1935. struct dentry *dentry)
  1936. {
  1937. struct inode *inode = d_inode(dentry);
  1938. int mask = 0;
  1939. int ret;
  1940. if (IS_NOSEC(inode))
  1941. return 0;
  1942. mask = setattr_should_drop_suidgid(idmap, inode);
  1943. ret = security_inode_need_killpriv(dentry);
  1944. if (ret < 0)
  1945. return ret;
  1946. if (ret)
  1947. mask |= ATTR_KILL_PRIV;
  1948. return mask;
  1949. }
  1950. static int __remove_privs(struct mnt_idmap *idmap,
  1951. struct dentry *dentry, int kill)
  1952. {
  1953. struct iattr newattrs;
  1954. newattrs.ia_valid = ATTR_FORCE | kill;
  1955. /*
  1956. * Note we call this on write, so notify_change will not
  1957. * encounter any conflicting delegations:
  1958. */
  1959. return notify_change(idmap, dentry, &newattrs, NULL);
  1960. }
  1961. int file_remove_privs_flags(struct file *file, unsigned int flags)
  1962. {
  1963. struct dentry *dentry = file_dentry(file);
  1964. struct inode *inode = file_inode(file);
  1965. int error = 0;
  1966. int kill;
  1967. if (IS_NOSEC(inode) || !S_ISREG(inode->i_mode))
  1968. return 0;
  1969. kill = dentry_needs_remove_privs(file_mnt_idmap(file), dentry);
  1970. if (kill < 0)
  1971. return kill;
  1972. if (kill) {
  1973. if (flags & IOCB_NOWAIT)
  1974. return -EAGAIN;
  1975. error = __remove_privs(file_mnt_idmap(file), dentry, kill);
  1976. }
  1977. if (!error)
  1978. inode_has_no_xattr(inode);
  1979. return error;
  1980. }
  1981. EXPORT_SYMBOL_GPL(file_remove_privs_flags);
  1982. /**
  1983. * file_remove_privs - remove special file privileges (suid, capabilities)
  1984. * @file: file to remove privileges from
  1985. *
  1986. * When file is modified by a write or truncation ensure that special
  1987. * file privileges are removed.
  1988. *
  1989. * Return: 0 on success, negative errno on failure.
  1990. */
  1991. int file_remove_privs(struct file *file)
  1992. {
  1993. return file_remove_privs_flags(file, 0);
  1994. }
  1995. EXPORT_SYMBOL(file_remove_privs);
  1996. static int inode_needs_update_time(struct inode *inode)
  1997. {
  1998. int sync_it = 0;
  1999. struct timespec64 now = current_time(inode);
  2000. struct timespec64 ts;
  2001. /* First try to exhaust all avenues to not sync */
  2002. if (IS_NOCMTIME(inode))
  2003. return 0;
  2004. ts = inode_get_mtime(inode);
  2005. if (!timespec64_equal(&ts, &now))
  2006. sync_it = S_MTIME;
  2007. ts = inode_get_ctime(inode);
  2008. if (!timespec64_equal(&ts, &now))
  2009. sync_it |= S_CTIME;
  2010. if (IS_I_VERSION(inode) && inode_iversion_need_inc(inode))
  2011. sync_it |= S_VERSION;
  2012. return sync_it;
  2013. }
  2014. static int __file_update_time(struct file *file, int sync_mode)
  2015. {
  2016. int ret = 0;
  2017. struct inode *inode = file_inode(file);
  2018. /* try to update time settings */
  2019. if (!mnt_get_write_access_file(file)) {
  2020. ret = inode_update_time(inode, sync_mode);
  2021. mnt_put_write_access_file(file);
  2022. }
  2023. return ret;
  2024. }
  2025. /**
  2026. * file_update_time - update mtime and ctime time
  2027. * @file: file accessed
  2028. *
  2029. * Update the mtime and ctime members of an inode and mark the inode for
  2030. * writeback. Note that this function is meant exclusively for usage in
  2031. * the file write path of filesystems, and filesystems may choose to
  2032. * explicitly ignore updates via this function with the _NOCMTIME inode
  2033. * flag, e.g. for network filesystem where these imestamps are handled
  2034. * by the server. This can return an error for file systems who need to
  2035. * allocate space in order to update an inode.
  2036. *
  2037. * Return: 0 on success, negative errno on failure.
  2038. */
  2039. int file_update_time(struct file *file)
  2040. {
  2041. int ret;
  2042. struct inode *inode = file_inode(file);
  2043. ret = inode_needs_update_time(inode);
  2044. if (ret <= 0)
  2045. return ret;
  2046. return __file_update_time(file, ret);
  2047. }
  2048. EXPORT_SYMBOL(file_update_time);
  2049. /**
  2050. * file_modified_flags - handle mandated vfs changes when modifying a file
  2051. * @file: file that was modified
  2052. * @flags: kiocb flags
  2053. *
  2054. * When file has been modified ensure that special
  2055. * file privileges are removed and time settings are updated.
  2056. *
  2057. * If IOCB_NOWAIT is set, special file privileges will not be removed and
  2058. * time settings will not be updated. It will return -EAGAIN.
  2059. *
  2060. * Context: Caller must hold the file's inode lock.
  2061. *
  2062. * Return: 0 on success, negative errno on failure.
  2063. */
  2064. static int file_modified_flags(struct file *file, int flags)
  2065. {
  2066. int ret;
  2067. struct inode *inode = file_inode(file);
  2068. /*
  2069. * Clear the security bits if the process is not being run by root.
  2070. * This keeps people from modifying setuid and setgid binaries.
  2071. */
  2072. ret = file_remove_privs_flags(file, flags);
  2073. if (ret)
  2074. return ret;
  2075. if (unlikely(file->f_mode & FMODE_NOCMTIME))
  2076. return 0;
  2077. ret = inode_needs_update_time(inode);
  2078. if (ret <= 0)
  2079. return ret;
  2080. if (flags & IOCB_NOWAIT)
  2081. return -EAGAIN;
  2082. return __file_update_time(file, ret);
  2083. }
  2084. /**
  2085. * file_modified - handle mandated vfs changes when modifying a file
  2086. * @file: file that was modified
  2087. *
  2088. * When file has been modified ensure that special
  2089. * file privileges are removed and time settings are updated.
  2090. *
  2091. * Context: Caller must hold the file's inode lock.
  2092. *
  2093. * Return: 0 on success, negative errno on failure.
  2094. */
  2095. int file_modified(struct file *file)
  2096. {
  2097. return file_modified_flags(file, 0);
  2098. }
  2099. EXPORT_SYMBOL(file_modified);
  2100. /**
  2101. * kiocb_modified - handle mandated vfs changes when modifying a file
  2102. * @iocb: iocb that was modified
  2103. *
  2104. * When file has been modified ensure that special
  2105. * file privileges are removed and time settings are updated.
  2106. *
  2107. * Context: Caller must hold the file's inode lock.
  2108. *
  2109. * Return: 0 on success, negative errno on failure.
  2110. */
  2111. int kiocb_modified(struct kiocb *iocb)
  2112. {
  2113. return file_modified_flags(iocb->ki_filp, iocb->ki_flags);
  2114. }
  2115. EXPORT_SYMBOL_GPL(kiocb_modified);
  2116. int inode_needs_sync(struct inode *inode)
  2117. {
  2118. if (IS_SYNC(inode))
  2119. return 1;
  2120. if (S_ISDIR(inode->i_mode) && IS_DIRSYNC(inode))
  2121. return 1;
  2122. return 0;
  2123. }
  2124. EXPORT_SYMBOL(inode_needs_sync);
  2125. /*
  2126. * If we try to find an inode in the inode hash while it is being
  2127. * deleted, we have to wait until the filesystem completes its
  2128. * deletion before reporting that it isn't found. This function waits
  2129. * until the deletion _might_ have completed. Callers are responsible
  2130. * to recheck inode state.
  2131. *
  2132. * It doesn't matter if I_NEW is not set initially, a call to
  2133. * wake_up_bit(&inode->i_state, __I_NEW) after removing from the hash list
  2134. * will DTRT.
  2135. */
  2136. static void __wait_on_freeing_inode(struct inode *inode, bool is_inode_hash_locked)
  2137. {
  2138. struct wait_bit_queue_entry wqe;
  2139. struct wait_queue_head *wq_head;
  2140. /*
  2141. * Handle racing against evict(), see that routine for more details.
  2142. */
  2143. if (unlikely(inode_unhashed(inode))) {
  2144. WARN_ON(is_inode_hash_locked);
  2145. spin_unlock(&inode->i_lock);
  2146. return;
  2147. }
  2148. wq_head = inode_bit_waitqueue(&wqe, inode, __I_NEW);
  2149. prepare_to_wait_event(wq_head, &wqe.wq_entry, TASK_UNINTERRUPTIBLE);
  2150. spin_unlock(&inode->i_lock);
  2151. rcu_read_unlock();
  2152. if (is_inode_hash_locked)
  2153. spin_unlock(&inode_hash_lock);
  2154. schedule();
  2155. finish_wait(wq_head, &wqe.wq_entry);
  2156. if (is_inode_hash_locked)
  2157. spin_lock(&inode_hash_lock);
  2158. rcu_read_lock();
  2159. }
  2160. static __initdata unsigned long ihash_entries;
  2161. static int __init set_ihash_entries(char *str)
  2162. {
  2163. if (!str)
  2164. return 0;
  2165. ihash_entries = simple_strtoul(str, &str, 0);
  2166. return 1;
  2167. }
  2168. __setup("ihash_entries=", set_ihash_entries);
  2169. /*
  2170. * Initialize the waitqueues and inode hash table.
  2171. */
  2172. void __init inode_init_early(void)
  2173. {
  2174. /* If hashes are distributed across NUMA nodes, defer
  2175. * hash allocation until vmalloc space is available.
  2176. */
  2177. if (hashdist)
  2178. return;
  2179. inode_hashtable =
  2180. alloc_large_system_hash("Inode-cache",
  2181. sizeof(struct hlist_head),
  2182. ihash_entries,
  2183. 14,
  2184. HASH_EARLY | HASH_ZERO,
  2185. &i_hash_shift,
  2186. &i_hash_mask,
  2187. 0,
  2188. 0);
  2189. }
  2190. void __init inode_init(void)
  2191. {
  2192. /* inode slab cache */
  2193. inode_cachep = kmem_cache_create("inode_cache",
  2194. sizeof(struct inode),
  2195. 0,
  2196. (SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|
  2197. SLAB_ACCOUNT),
  2198. init_once);
  2199. /* Hash may have been set up in inode_init_early */
  2200. if (!hashdist)
  2201. return;
  2202. inode_hashtable =
  2203. alloc_large_system_hash("Inode-cache",
  2204. sizeof(struct hlist_head),
  2205. ihash_entries,
  2206. 14,
  2207. HASH_ZERO,
  2208. &i_hash_shift,
  2209. &i_hash_mask,
  2210. 0,
  2211. 0);
  2212. }
  2213. void init_special_inode(struct inode *inode, umode_t mode, dev_t rdev)
  2214. {
  2215. inode->i_mode = mode;
  2216. if (S_ISCHR(mode)) {
  2217. inode->i_fop = &def_chr_fops;
  2218. inode->i_rdev = rdev;
  2219. } else if (S_ISBLK(mode)) {
  2220. if (IS_ENABLED(CONFIG_BLOCK))
  2221. inode->i_fop = &def_blk_fops;
  2222. inode->i_rdev = rdev;
  2223. } else if (S_ISFIFO(mode))
  2224. inode->i_fop = &pipefifo_fops;
  2225. else if (S_ISSOCK(mode))
  2226. ; /* leave it no_open_fops */
  2227. else
  2228. printk(KERN_DEBUG "init_special_inode: bogus i_mode (%o) for"
  2229. " inode %s:%lu\n", mode, inode->i_sb->s_id,
  2230. inode->i_ino);
  2231. }
  2232. EXPORT_SYMBOL(init_special_inode);
  2233. /**
  2234. * inode_init_owner - Init uid,gid,mode for new inode according to posix standards
  2235. * @idmap: idmap of the mount the inode was created from
  2236. * @inode: New inode
  2237. * @dir: Directory inode
  2238. * @mode: mode of the new inode
  2239. *
  2240. * If the inode has been created through an idmapped mount the idmap of
  2241. * the vfsmount must be passed through @idmap. This function will then take
  2242. * care to map the inode according to @idmap before checking permissions
  2243. * and initializing i_uid and i_gid. On non-idmapped mounts or if permission
  2244. * checking is to be performed on the raw inode simply pass @nop_mnt_idmap.
  2245. */
  2246. void inode_init_owner(struct mnt_idmap *idmap, struct inode *inode,
  2247. const struct inode *dir, umode_t mode)
  2248. {
  2249. inode_fsuid_set(inode, idmap);
  2250. if (dir && dir->i_mode & S_ISGID) {
  2251. inode->i_gid = dir->i_gid;
  2252. /* Directories are special, and always inherit S_ISGID */
  2253. if (S_ISDIR(mode))
  2254. mode |= S_ISGID;
  2255. } else
  2256. inode_fsgid_set(inode, idmap);
  2257. inode->i_mode = mode;
  2258. }
  2259. EXPORT_SYMBOL(inode_init_owner);
  2260. /**
  2261. * inode_owner_or_capable - check current task permissions to inode
  2262. * @idmap: idmap of the mount the inode was found from
  2263. * @inode: inode being checked
  2264. *
  2265. * Return true if current either has CAP_FOWNER in a namespace with the
  2266. * inode owner uid mapped, or owns the file.
  2267. *
  2268. * If the inode has been found through an idmapped mount the idmap of
  2269. * the vfsmount must be passed through @idmap. This function will then take
  2270. * care to map the inode according to @idmap before checking permissions.
  2271. * On non-idmapped mounts or if permission checking is to be performed on the
  2272. * raw inode simply pass @nop_mnt_idmap.
  2273. */
  2274. bool inode_owner_or_capable(struct mnt_idmap *idmap,
  2275. const struct inode *inode)
  2276. {
  2277. vfsuid_t vfsuid;
  2278. struct user_namespace *ns;
  2279. vfsuid = i_uid_into_vfsuid(idmap, inode);
  2280. if (vfsuid_eq_kuid(vfsuid, current_fsuid()))
  2281. return true;
  2282. ns = current_user_ns();
  2283. if (vfsuid_has_mapping(ns, vfsuid) && ns_capable(ns, CAP_FOWNER))
  2284. return true;
  2285. return false;
  2286. }
  2287. EXPORT_SYMBOL(inode_owner_or_capable);
  2288. /*
  2289. * Direct i/o helper functions
  2290. */
  2291. bool inode_dio_finished(const struct inode *inode)
  2292. {
  2293. return atomic_read(&inode->i_dio_count) == 0;
  2294. }
  2295. EXPORT_SYMBOL(inode_dio_finished);
  2296. /**
  2297. * inode_dio_wait - wait for outstanding DIO requests to finish
  2298. * @inode: inode to wait for
  2299. *
  2300. * Waits for all pending direct I/O requests to finish so that we can
  2301. * proceed with a truncate or equivalent operation.
  2302. *
  2303. * Must be called under a lock that serializes taking new references
  2304. * to i_dio_count, usually by inode->i_mutex.
  2305. */
  2306. void inode_dio_wait(struct inode *inode)
  2307. {
  2308. wait_var_event(&inode->i_dio_count, inode_dio_finished(inode));
  2309. }
  2310. EXPORT_SYMBOL(inode_dio_wait);
  2311. void inode_dio_wait_interruptible(struct inode *inode)
  2312. {
  2313. wait_var_event_interruptible(&inode->i_dio_count,
  2314. inode_dio_finished(inode));
  2315. }
  2316. EXPORT_SYMBOL(inode_dio_wait_interruptible);
  2317. /*
  2318. * inode_set_flags - atomically set some inode flags
  2319. *
  2320. * Note: the caller should be holding i_mutex, or else be sure that
  2321. * they have exclusive access to the inode structure (i.e., while the
  2322. * inode is being instantiated). The reason for the cmpxchg() loop
  2323. * --- which wouldn't be necessary if all code paths which modify
  2324. * i_flags actually followed this rule, is that there is at least one
  2325. * code path which doesn't today so we use cmpxchg() out of an abundance
  2326. * of caution.
  2327. *
  2328. * In the long run, i_mutex is overkill, and we should probably look
  2329. * at using the i_lock spinlock to protect i_flags, and then make sure
  2330. * it is so documented in include/linux/fs.h and that all code follows
  2331. * the locking convention!!
  2332. */
  2333. void inode_set_flags(struct inode *inode, unsigned int flags,
  2334. unsigned int mask)
  2335. {
  2336. WARN_ON_ONCE(flags & ~mask);
  2337. set_mask_bits(&inode->i_flags, mask, flags);
  2338. }
  2339. EXPORT_SYMBOL(inode_set_flags);
  2340. void inode_nohighmem(struct inode *inode)
  2341. {
  2342. mapping_set_gfp_mask(inode->i_mapping, GFP_USER);
  2343. }
  2344. EXPORT_SYMBOL(inode_nohighmem);
  2345. /**
  2346. * timestamp_truncate - Truncate timespec to a granularity
  2347. * @t: Timespec
  2348. * @inode: inode being updated
  2349. *
  2350. * Truncate a timespec to the granularity supported by the fs
  2351. * containing the inode. Always rounds down. gran must
  2352. * not be 0 nor greater than a second (NSEC_PER_SEC, or 10^9 ns).
  2353. */
  2354. struct timespec64 timestamp_truncate(struct timespec64 t, struct inode *inode)
  2355. {
  2356. struct super_block *sb = inode->i_sb;
  2357. unsigned int gran = sb->s_time_gran;
  2358. t.tv_sec = clamp(t.tv_sec, sb->s_time_min, sb->s_time_max);
  2359. if (unlikely(t.tv_sec == sb->s_time_max || t.tv_sec == sb->s_time_min))
  2360. t.tv_nsec = 0;
  2361. /* Avoid division in the common cases 1 ns and 1 s. */
  2362. if (gran == 1)
  2363. ; /* nothing */
  2364. else if (gran == NSEC_PER_SEC)
  2365. t.tv_nsec = 0;
  2366. else if (gran > 1 && gran < NSEC_PER_SEC)
  2367. t.tv_nsec -= t.tv_nsec % gran;
  2368. else
  2369. WARN(1, "invalid file time granularity: %u", gran);
  2370. return t;
  2371. }
  2372. EXPORT_SYMBOL(timestamp_truncate);
  2373. /**
  2374. * current_time - Return FS time
  2375. * @inode: inode.
  2376. *
  2377. * Return the current time truncated to the time granularity supported by
  2378. * the fs.
  2379. *
  2380. * Note that inode and inode->sb cannot be NULL.
  2381. * Otherwise, the function warns and returns time without truncation.
  2382. */
  2383. struct timespec64 current_time(struct inode *inode)
  2384. {
  2385. struct timespec64 now;
  2386. ktime_get_coarse_real_ts64(&now);
  2387. return timestamp_truncate(now, inode);
  2388. }
  2389. EXPORT_SYMBOL(current_time);
  2390. /**
  2391. * inode_set_ctime_current - set the ctime to current_time
  2392. * @inode: inode
  2393. *
  2394. * Set the inode->i_ctime to the current value for the inode. Returns
  2395. * the current value that was assigned to i_ctime.
  2396. */
  2397. struct timespec64 inode_set_ctime_current(struct inode *inode)
  2398. {
  2399. struct timespec64 now = current_time(inode);
  2400. inode_set_ctime_to_ts(inode, now);
  2401. return now;
  2402. }
  2403. EXPORT_SYMBOL(inode_set_ctime_current);
  2404. /**
  2405. * in_group_or_capable - check whether caller is CAP_FSETID privileged
  2406. * @idmap: idmap of the mount @inode was found from
  2407. * @inode: inode to check
  2408. * @vfsgid: the new/current vfsgid of @inode
  2409. *
  2410. * Check wether @vfsgid is in the caller's group list or if the caller is
  2411. * privileged with CAP_FSETID over @inode. This can be used to determine
  2412. * whether the setgid bit can be kept or must be dropped.
  2413. *
  2414. * Return: true if the caller is sufficiently privileged, false if not.
  2415. */
  2416. bool in_group_or_capable(struct mnt_idmap *idmap,
  2417. const struct inode *inode, vfsgid_t vfsgid)
  2418. {
  2419. if (vfsgid_in_group_p(vfsgid))
  2420. return true;
  2421. if (capable_wrt_inode_uidgid(idmap, inode, CAP_FSETID))
  2422. return true;
  2423. return false;
  2424. }
  2425. EXPORT_SYMBOL(in_group_or_capable);
  2426. /**
  2427. * mode_strip_sgid - handle the sgid bit for non-directories
  2428. * @idmap: idmap of the mount the inode was created from
  2429. * @dir: parent directory inode
  2430. * @mode: mode of the file to be created in @dir
  2431. *
  2432. * If the @mode of the new file has both the S_ISGID and S_IXGRP bit
  2433. * raised and @dir has the S_ISGID bit raised ensure that the caller is
  2434. * either in the group of the parent directory or they have CAP_FSETID
  2435. * in their user namespace and are privileged over the parent directory.
  2436. * In all other cases, strip the S_ISGID bit from @mode.
  2437. *
  2438. * Return: the new mode to use for the file
  2439. */
  2440. umode_t mode_strip_sgid(struct mnt_idmap *idmap,
  2441. const struct inode *dir, umode_t mode)
  2442. {
  2443. if ((mode & (S_ISGID | S_IXGRP)) != (S_ISGID | S_IXGRP))
  2444. return mode;
  2445. if (S_ISDIR(mode) || !dir || !(dir->i_mode & S_ISGID))
  2446. return mode;
  2447. if (in_group_or_capable(idmap, dir, i_gid_into_vfsgid(idmap, dir)))
  2448. return mode;
  2449. return mode & ~S_ISGID;
  2450. }
  2451. EXPORT_SYMBOL(mode_strip_sgid);