inode.c 40 KB

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