inode.c 42 KB

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