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