inode.c 41 KB

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