inode.c 42 KB

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