inode.c 36 KB

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