inode.c 42 KB

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