inode.c 69 KB

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