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