inode.c 36 KB

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