inode.c 11 KB

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