inode.c 39 KB

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