inode.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. /*
  2. * fs/logfs/inode.c - inode handling code
  3. *
  4. * As should be obvious for Linux kernel code, license is GPLv2
  5. *
  6. * Copyright (c) 2005-2008 Joern Engel <joern@logfs.org>
  7. */
  8. #include "logfs.h"
  9. #include <linux/slab.h>
  10. #include <linux/writeback.h>
  11. #include <linux/backing-dev.h>
  12. /*
  13. * How soon to reuse old inode numbers? LogFS doesn't store deleted inodes
  14. * on the medium. It therefore also lacks a method to store the previous
  15. * generation number for deleted inodes. Instead a single generation number
  16. * is stored which will be used for new inodes. Being just a 32bit counter,
  17. * this can obvious wrap relatively quickly. So we only reuse inodes if we
  18. * know that a fair number of inodes can be created before we have to increment
  19. * the generation again - effectively adding some bits to the counter.
  20. * But being too aggressive here means we keep a very large and very sparse
  21. * inode file, wasting space on indirect blocks.
  22. * So what is a good value? Beats me. 64k seems moderately bad on both
  23. * fronts, so let's use that for now...
  24. *
  25. * NFS sucks, as everyone already knows.
  26. */
  27. #define INOS_PER_WRAP (0x10000)
  28. /*
  29. * Logfs' requirement to read inodes for garbage collection makes life a bit
  30. * harder. GC may have to read inodes that are in I_FREEING state, when they
  31. * are being written out - and waiting for GC to make progress, naturally.
  32. *
  33. * So we cannot just call iget() or some variant of it, but first have to check
  34. * whether the inode in question might be in I_FREEING state. Therefore we
  35. * maintain our own per-sb list of "almost deleted" inodes and check against
  36. * that list first. Normally this should be at most 1-2 entries long.
  37. *
  38. * Also, inodes have logfs-specific reference counting on top of what the vfs
  39. * does. When .destroy_inode is called, normally the reference count will drop
  40. * to zero and the inode gets deleted. But if GC accessed the inode, its
  41. * refcount will remain nonzero and final deletion will have to wait.
  42. *
  43. * As a result we have two sets of functions to get/put inodes:
  44. * logfs_safe_iget/logfs_safe_iput - safe to call from GC context
  45. * logfs_iget/iput - normal version
  46. */
  47. static struct kmem_cache *logfs_inode_cache;
  48. static DEFINE_SPINLOCK(logfs_inode_lock);
  49. static void logfs_inode_setops(struct inode *inode)
  50. {
  51. switch (inode->i_mode & S_IFMT) {
  52. case S_IFDIR:
  53. inode->i_op = &logfs_dir_iops;
  54. inode->i_fop = &logfs_dir_fops;
  55. inode->i_mapping->a_ops = &logfs_reg_aops;
  56. break;
  57. case S_IFREG:
  58. inode->i_op = &logfs_reg_iops;
  59. inode->i_fop = &logfs_reg_fops;
  60. inode->i_mapping->a_ops = &logfs_reg_aops;
  61. break;
  62. case S_IFLNK:
  63. inode->i_op = &page_symlink_inode_operations;
  64. inode_nohighmem(inode);
  65. inode->i_mapping->a_ops = &logfs_reg_aops;
  66. break;
  67. case S_IFSOCK: /* fall through */
  68. case S_IFBLK: /* fall through */
  69. case S_IFCHR: /* fall through */
  70. case S_IFIFO:
  71. init_special_inode(inode, inode->i_mode, inode->i_rdev);
  72. break;
  73. default:
  74. BUG();
  75. }
  76. }
  77. static struct inode *__logfs_iget(struct super_block *sb, ino_t ino)
  78. {
  79. struct inode *inode = iget_locked(sb, ino);
  80. int err;
  81. if (!inode)
  82. return ERR_PTR(-ENOMEM);
  83. if (!(inode->i_state & I_NEW))
  84. return inode;
  85. err = logfs_read_inode(inode);
  86. if (err || inode->i_nlink == 0) {
  87. /* inode->i_nlink == 0 can be true when called from
  88. * block validator */
  89. /* set i_nlink to 0 to prevent caching */
  90. clear_nlink(inode);
  91. logfs_inode(inode)->li_flags |= LOGFS_IF_ZOMBIE;
  92. iget_failed(inode);
  93. if (!err)
  94. err = -ENOENT;
  95. return ERR_PTR(err);
  96. }
  97. logfs_inode_setops(inode);
  98. unlock_new_inode(inode);
  99. return inode;
  100. }
  101. struct inode *logfs_iget(struct super_block *sb, ino_t ino)
  102. {
  103. BUG_ON(ino == LOGFS_INO_MASTER);
  104. BUG_ON(ino == LOGFS_INO_SEGFILE);
  105. return __logfs_iget(sb, ino);
  106. }
  107. /*
  108. * is_cached is set to 1 if we hand out a cached inode, 0 otherwise.
  109. * this allows logfs_iput to do the right thing later
  110. */
  111. struct inode *logfs_safe_iget(struct super_block *sb, ino_t ino, int *is_cached)
  112. {
  113. struct logfs_super *super = logfs_super(sb);
  114. struct logfs_inode *li;
  115. if (ino == LOGFS_INO_MASTER)
  116. return super->s_master_inode;
  117. if (ino == LOGFS_INO_SEGFILE)
  118. return super->s_segfile_inode;
  119. spin_lock(&logfs_inode_lock);
  120. list_for_each_entry(li, &super->s_freeing_list, li_freeing_list)
  121. if (li->vfs_inode.i_ino == ino) {
  122. li->li_refcount++;
  123. spin_unlock(&logfs_inode_lock);
  124. *is_cached = 1;
  125. return &li->vfs_inode;
  126. }
  127. spin_unlock(&logfs_inode_lock);
  128. *is_cached = 0;
  129. return __logfs_iget(sb, ino);
  130. }
  131. static void logfs_i_callback(struct rcu_head *head)
  132. {
  133. struct inode *inode = container_of(head, struct inode, i_rcu);
  134. kmem_cache_free(logfs_inode_cache, logfs_inode(inode));
  135. }
  136. static void __logfs_destroy_inode(struct inode *inode)
  137. {
  138. struct logfs_inode *li = logfs_inode(inode);
  139. BUG_ON(li->li_block);
  140. list_del(&li->li_freeing_list);
  141. call_rcu(&inode->i_rcu, logfs_i_callback);
  142. }
  143. static void __logfs_destroy_meta_inode(struct inode *inode)
  144. {
  145. struct logfs_inode *li = logfs_inode(inode);
  146. BUG_ON(li->li_block);
  147. call_rcu(&inode->i_rcu, logfs_i_callback);
  148. }
  149. static void logfs_destroy_inode(struct inode *inode)
  150. {
  151. struct logfs_inode *li = logfs_inode(inode);
  152. if (inode->i_ino < LOGFS_RESERVED_INOS) {
  153. /*
  154. * The reserved inodes are never destroyed unless we are in
  155. * unmont path.
  156. */
  157. __logfs_destroy_meta_inode(inode);
  158. return;
  159. }
  160. BUG_ON(list_empty(&li->li_freeing_list));
  161. spin_lock(&logfs_inode_lock);
  162. li->li_refcount--;
  163. if (li->li_refcount == 0)
  164. __logfs_destroy_inode(inode);
  165. spin_unlock(&logfs_inode_lock);
  166. }
  167. void logfs_safe_iput(struct inode *inode, int is_cached)
  168. {
  169. if (inode->i_ino == LOGFS_INO_MASTER)
  170. return;
  171. if (inode->i_ino == LOGFS_INO_SEGFILE)
  172. return;
  173. if (is_cached) {
  174. logfs_destroy_inode(inode);
  175. return;
  176. }
  177. iput(inode);
  178. }
  179. static void logfs_init_inode(struct super_block *sb, struct inode *inode)
  180. {
  181. struct logfs_inode *li = logfs_inode(inode);
  182. int i;
  183. li->li_flags = 0;
  184. li->li_height = 0;
  185. li->li_used_bytes = 0;
  186. li->li_block = NULL;
  187. i_uid_write(inode, 0);
  188. i_gid_write(inode, 0);
  189. inode->i_size = 0;
  190. inode->i_blocks = 0;
  191. inode->i_ctime = current_time(inode);
  192. inode->i_mtime = current_time(inode);
  193. li->li_refcount = 1;
  194. INIT_LIST_HEAD(&li->li_freeing_list);
  195. for (i = 0; i < LOGFS_EMBEDDED_FIELDS; i++)
  196. li->li_data[i] = 0;
  197. return;
  198. }
  199. static struct inode *logfs_alloc_inode(struct super_block *sb)
  200. {
  201. struct logfs_inode *li;
  202. li = kmem_cache_alloc(logfs_inode_cache, GFP_NOFS);
  203. if (!li)
  204. return NULL;
  205. logfs_init_inode(sb, &li->vfs_inode);
  206. return &li->vfs_inode;
  207. }
  208. /*
  209. * In logfs inodes are written to an inode file. The inode file, like any
  210. * other file, is managed with a inode. The inode file's inode, aka master
  211. * inode, requires special handling in several respects. First, it cannot be
  212. * written to the inode file, so it is stored in the journal instead.
  213. *
  214. * Secondly, this inode cannot be written back and destroyed before all other
  215. * inodes have been written. The ordering is important. Linux' VFS is happily
  216. * unaware of the ordering constraint and would ordinarily destroy the master
  217. * inode at umount time while other inodes are still in use and dirty. Not
  218. * good.
  219. *
  220. * So logfs makes sure the master inode is not written until all other inodes
  221. * have been destroyed. Sadly, this method has another side-effect. The VFS
  222. * will notice one remaining inode and print a frightening warning message.
  223. * Worse, it is impossible to judge whether such a warning was caused by the
  224. * master inode or any other inodes have leaked as well.
  225. *
  226. * Our attempt of solving this is with logfs_new_meta_inode() below. Its
  227. * purpose is to create a new inode that will not trigger the warning if such
  228. * an inode is still in use. An ugly hack, no doubt. Suggections for
  229. * improvement are welcome.
  230. *
  231. * AV: that's what ->put_super() is for...
  232. */
  233. struct inode *logfs_new_meta_inode(struct super_block *sb, u64 ino)
  234. {
  235. struct inode *inode;
  236. inode = new_inode(sb);
  237. if (!inode)
  238. return ERR_PTR(-ENOMEM);
  239. inode->i_mode = S_IFREG;
  240. inode->i_ino = ino;
  241. inode->i_data.a_ops = &logfs_reg_aops;
  242. mapping_set_gfp_mask(&inode->i_data, GFP_NOFS);
  243. return inode;
  244. }
  245. struct inode *logfs_read_meta_inode(struct super_block *sb, u64 ino)
  246. {
  247. struct inode *inode;
  248. int err;
  249. inode = logfs_new_meta_inode(sb, ino);
  250. if (IS_ERR(inode))
  251. return inode;
  252. err = logfs_read_inode(inode);
  253. if (err) {
  254. iput(inode);
  255. return ERR_PTR(err);
  256. }
  257. logfs_inode_setops(inode);
  258. return inode;
  259. }
  260. static int logfs_write_inode(struct inode *inode, struct writeback_control *wbc)
  261. {
  262. int ret;
  263. long flags = WF_LOCK;
  264. /* Can only happen if creat() failed. Safe to skip. */
  265. if (logfs_inode(inode)->li_flags & LOGFS_IF_STILLBORN)
  266. return 0;
  267. ret = __logfs_write_inode(inode, NULL, flags);
  268. LOGFS_BUG_ON(ret, inode->i_sb);
  269. return ret;
  270. }
  271. /* called with inode->i_lock held */
  272. static int logfs_drop_inode(struct inode *inode)
  273. {
  274. struct logfs_super *super = logfs_super(inode->i_sb);
  275. struct logfs_inode *li = logfs_inode(inode);
  276. spin_lock(&logfs_inode_lock);
  277. list_move(&li->li_freeing_list, &super->s_freeing_list);
  278. spin_unlock(&logfs_inode_lock);
  279. return generic_drop_inode(inode);
  280. }
  281. static void logfs_set_ino_generation(struct super_block *sb,
  282. struct inode *inode)
  283. {
  284. struct logfs_super *super = logfs_super(sb);
  285. u64 ino;
  286. mutex_lock(&super->s_journal_mutex);
  287. ino = logfs_seek_hole(super->s_master_inode, super->s_last_ino + 1);
  288. super->s_last_ino = ino;
  289. super->s_inos_till_wrap--;
  290. if (super->s_inos_till_wrap < 0) {
  291. super->s_last_ino = LOGFS_RESERVED_INOS;
  292. super->s_generation++;
  293. super->s_inos_till_wrap = INOS_PER_WRAP;
  294. }
  295. inode->i_ino = ino;
  296. inode->i_generation = super->s_generation;
  297. mutex_unlock(&super->s_journal_mutex);
  298. }
  299. struct inode *logfs_new_inode(struct inode *dir, umode_t mode)
  300. {
  301. struct super_block *sb = dir->i_sb;
  302. struct inode *inode;
  303. inode = new_inode(sb);
  304. if (!inode)
  305. return ERR_PTR(-ENOMEM);
  306. logfs_init_inode(sb, inode);
  307. /* inherit parent flags */
  308. logfs_inode(inode)->li_flags |=
  309. logfs_inode(dir)->li_flags & LOGFS_FL_INHERITED;
  310. inode->i_mode = mode;
  311. logfs_set_ino_generation(sb, inode);
  312. inode_init_owner(inode, dir, mode);
  313. logfs_inode_setops(inode);
  314. insert_inode_hash(inode);
  315. return inode;
  316. }
  317. static void logfs_init_once(void *_li)
  318. {
  319. struct logfs_inode *li = _li;
  320. int i;
  321. li->li_flags = 0;
  322. li->li_used_bytes = 0;
  323. li->li_refcount = 1;
  324. for (i = 0; i < LOGFS_EMBEDDED_FIELDS; i++)
  325. li->li_data[i] = 0;
  326. inode_init_once(&li->vfs_inode);
  327. }
  328. static int logfs_sync_fs(struct super_block *sb, int wait)
  329. {
  330. logfs_get_wblocks(sb, NULL, WF_LOCK);
  331. logfs_write_anchor(sb);
  332. logfs_put_wblocks(sb, NULL, WF_LOCK);
  333. return 0;
  334. }
  335. static void logfs_put_super(struct super_block *sb)
  336. {
  337. struct logfs_super *super = logfs_super(sb);
  338. /* kill the meta-inodes */
  339. iput(super->s_segfile_inode);
  340. iput(super->s_master_inode);
  341. iput(super->s_mapping_inode);
  342. }
  343. const struct super_operations logfs_super_operations = {
  344. .alloc_inode = logfs_alloc_inode,
  345. .destroy_inode = logfs_destroy_inode,
  346. .evict_inode = logfs_evict_inode,
  347. .drop_inode = logfs_drop_inode,
  348. .put_super = logfs_put_super,
  349. .write_inode = logfs_write_inode,
  350. .statfs = logfs_statfs,
  351. .sync_fs = logfs_sync_fs,
  352. };
  353. int logfs_init_inode_cache(void)
  354. {
  355. logfs_inode_cache = kmem_cache_create("logfs_inode_cache",
  356. sizeof(struct logfs_inode), 0,
  357. SLAB_RECLAIM_ACCOUNT|SLAB_ACCOUNT,
  358. logfs_init_once);
  359. if (!logfs_inode_cache)
  360. return -ENOMEM;
  361. return 0;
  362. }
  363. void logfs_destroy_inode_cache(void)
  364. {
  365. /*
  366. * Make sure all delayed rcu free inodes are flushed before we
  367. * destroy cache.
  368. */
  369. rcu_barrier();
  370. kmem_cache_destroy(logfs_inode_cache);
  371. }