inode.c 44 KB

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