inode.c 43 KB

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