inode.c 43 KB

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