inode.c 43 KB

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