inode.c 36 KB

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