inode.c 36 KB

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