inode.c 41 KB

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