inode.c 44 KB

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