inode.c 44 KB

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