inode.c 84 KB

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