inode.c 40 KB

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