inode.c 39 KB

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