inode.c 48 KB

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