inode.c 41 KB

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