inode.c 42 KB

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