inode.c 64 KB

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