inode.c 38 KB

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