inode.c 41 KB

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