inode.c 41 KB

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