inode.c 37 KB

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