inode.c 56 KB

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