inode.c 40 KB

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