inode.c 56 KB

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