inode.c 43 KB

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