2 * fs/logfs/inode.c - inode handling code
4 * As should be obvious for Linux kernel code, license is GPLv2
6 * Copyright (c) 2005-2008 Joern Engel <joern@logfs.org>
9 #include <linux/slab.h>
10 #include <linux/writeback.h>
11 #include <linux/backing-dev.h>
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...
26 * NFS sucks, as everyone already knows.
28 #define INOS_PER_WRAP (0x10000)
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.
35 * So we cannot just call iget() or some variant of it, but first have to check
36 * whether 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.
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.
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
49 static struct kmem_cache
*logfs_inode_cache
;
51 static DEFINE_SPINLOCK(logfs_inode_lock
);
53 static void logfs_inode_setops(struct inode
*inode
)
55 switch (inode
->i_mode
& S_IFMT
) {
57 inode
->i_op
= &logfs_dir_iops
;
58 inode
->i_fop
= &logfs_dir_fops
;
59 inode
->i_mapping
->a_ops
= &logfs_reg_aops
;
62 inode
->i_op
= &logfs_reg_iops
;
63 inode
->i_fop
= &logfs_reg_fops
;
64 inode
->i_mapping
->a_ops
= &logfs_reg_aops
;
67 inode
->i_op
= &page_symlink_inode_operations
;
68 inode_nohighmem(inode
);
69 inode
->i_mapping
->a_ops
= &logfs_reg_aops
;
71 case S_IFSOCK
: /* fall through */
72 case S_IFBLK
: /* fall through */
73 case S_IFCHR
: /* fall through */
75 init_special_inode(inode
, inode
->i_mode
, inode
->i_rdev
);
82 static struct inode
*__logfs_iget(struct super_block
*sb
, ino_t ino
)
84 struct inode
*inode
= iget_locked(sb
, ino
);
88 return ERR_PTR(-ENOMEM
);
89 if (!(inode
->i_state
& I_NEW
))
92 err
= logfs_read_inode(inode
);
93 if (err
|| inode
->i_nlink
== 0) {
94 /* inode->i_nlink == 0 can be true when called from
96 /* set i_nlink to 0 to prevent caching */
98 logfs_inode(inode
)->li_flags
|= LOGFS_IF_ZOMBIE
;
105 logfs_inode_setops(inode
);
106 unlock_new_inode(inode
);
110 struct inode
*logfs_iget(struct super_block
*sb
, ino_t ino
)
112 BUG_ON(ino
== LOGFS_INO_MASTER
);
113 BUG_ON(ino
== LOGFS_INO_SEGFILE
);
114 return __logfs_iget(sb
, ino
);
118 * is_cached is set to 1 if we hand out a cached inode, 0 otherwise.
119 * this allows logfs_iput to do the right thing later
121 struct inode
*logfs_safe_iget(struct super_block
*sb
, ino_t ino
, int *is_cached
)
123 struct logfs_super
*super
= logfs_super(sb
);
124 struct logfs_inode
*li
;
126 if (ino
== LOGFS_INO_MASTER
)
127 return super
->s_master_inode
;
128 if (ino
== LOGFS_INO_SEGFILE
)
129 return super
->s_segfile_inode
;
131 spin_lock(&logfs_inode_lock
);
132 list_for_each_entry(li
, &super
->s_freeing_list
, li_freeing_list
)
133 if (li
->vfs_inode
.i_ino
== ino
) {
135 spin_unlock(&logfs_inode_lock
);
137 return &li
->vfs_inode
;
139 spin_unlock(&logfs_inode_lock
);
142 return __logfs_iget(sb
, ino
);
145 static void logfs_i_callback(struct rcu_head
*head
)
147 struct inode
*inode
= container_of(head
, struct inode
, i_rcu
);
148 kmem_cache_free(logfs_inode_cache
, logfs_inode(inode
));
151 static void __logfs_destroy_inode(struct inode
*inode
)
153 struct logfs_inode
*li
= logfs_inode(inode
);
155 BUG_ON(li
->li_block
);
156 list_del(&li
->li_freeing_list
);
157 call_rcu(&inode
->i_rcu
, logfs_i_callback
);
160 static void __logfs_destroy_meta_inode(struct inode
*inode
)
162 struct logfs_inode
*li
= logfs_inode(inode
);
163 BUG_ON(li
->li_block
);
164 call_rcu(&inode
->i_rcu
, logfs_i_callback
);
167 static void logfs_destroy_inode(struct inode
*inode
)
169 struct logfs_inode
*li
= logfs_inode(inode
);
171 if (inode
->i_ino
< LOGFS_RESERVED_INOS
) {
173 * The reserved inodes are never destroyed unless we are in
176 __logfs_destroy_meta_inode(inode
);
180 BUG_ON(list_empty(&li
->li_freeing_list
));
181 spin_lock(&logfs_inode_lock
);
183 if (li
->li_refcount
== 0)
184 __logfs_destroy_inode(inode
);
185 spin_unlock(&logfs_inode_lock
);
188 void logfs_safe_iput(struct inode
*inode
, int is_cached
)
190 if (inode
->i_ino
== LOGFS_INO_MASTER
)
192 if (inode
->i_ino
== LOGFS_INO_SEGFILE
)
196 logfs_destroy_inode(inode
);
203 static void logfs_init_inode(struct super_block
*sb
, struct inode
*inode
)
205 struct logfs_inode
*li
= logfs_inode(inode
);
210 li
->li_used_bytes
= 0;
212 i_uid_write(inode
, 0);
213 i_gid_write(inode
, 0);
216 inode
->i_ctime
= current_time(inode
);
217 inode
->i_mtime
= current_time(inode
);
219 INIT_LIST_HEAD(&li
->li_freeing_list
);
221 for (i
= 0; i
< LOGFS_EMBEDDED_FIELDS
; i
++)
227 static struct inode
*logfs_alloc_inode(struct super_block
*sb
)
229 struct logfs_inode
*li
;
231 li
= kmem_cache_alloc(logfs_inode_cache
, GFP_NOFS
);
234 logfs_init_inode(sb
, &li
->vfs_inode
);
235 return &li
->vfs_inode
;
239 * In logfs inodes are written to an inode file. The inode file, like any
240 * other file, is managed with a inode. The inode file's inode, aka master
241 * inode, requires special handling in several respects. First, it cannot be
242 * written to the inode file, so it is stored in the journal instead.
244 * Secondly, this inode cannot be written back and destroyed before all other
245 * inodes have been written. The ordering is important. Linux' VFS is happily
246 * unaware of the ordering constraint and would ordinarily destroy the master
247 * inode at umount time while other inodes are still in use and dirty. Not
250 * So logfs makes sure the master inode is not written until all other inodes
251 * have been destroyed. Sadly, this method has another side-effect. The VFS
252 * will notice one remaining inode and print a frightening warning message.
253 * Worse, it is impossible to judge whether such a warning was caused by the
254 * master inode or any other inodes have leaked as well.
256 * Our attempt of solving this is with logfs_new_meta_inode() below. Its
257 * purpose is to create a new inode that will not trigger the warning if such
258 * an inode is still in use. An ugly hack, no doubt. Suggections for
259 * improvement are welcome.
261 * AV: that's what ->put_super() is for...
263 struct inode
*logfs_new_meta_inode(struct super_block
*sb
, u64 ino
)
267 inode
= new_inode(sb
);
269 return ERR_PTR(-ENOMEM
);
271 inode
->i_mode
= S_IFREG
;
273 inode
->i_data
.a_ops
= &logfs_reg_aops
;
274 mapping_set_gfp_mask(&inode
->i_data
, GFP_NOFS
);
279 struct inode
*logfs_read_meta_inode(struct super_block
*sb
, u64 ino
)
284 inode
= logfs_new_meta_inode(sb
, ino
);
288 err
= logfs_read_inode(inode
);
293 logfs_inode_setops(inode
);
297 static int logfs_write_inode(struct inode
*inode
, struct writeback_control
*wbc
)
300 long flags
= WF_LOCK
;
302 /* Can only happen if creat() failed. Safe to skip. */
303 if (logfs_inode(inode
)->li_flags
& LOGFS_IF_STILLBORN
)
306 ret
= __logfs_write_inode(inode
, NULL
, flags
);
307 LOGFS_BUG_ON(ret
, inode
->i_sb
);
311 /* called with inode->i_lock held */
312 static int logfs_drop_inode(struct inode
*inode
)
314 struct logfs_super
*super
= logfs_super(inode
->i_sb
);
315 struct logfs_inode
*li
= logfs_inode(inode
);
317 spin_lock(&logfs_inode_lock
);
318 list_move(&li
->li_freeing_list
, &super
->s_freeing_list
);
319 spin_unlock(&logfs_inode_lock
);
320 return generic_drop_inode(inode
);
323 static void logfs_set_ino_generation(struct super_block
*sb
,
326 struct logfs_super
*super
= logfs_super(sb
);
329 mutex_lock(&super
->s_journal_mutex
);
330 ino
= logfs_seek_hole(super
->s_master_inode
, super
->s_last_ino
+ 1);
331 super
->s_last_ino
= ino
;
332 super
->s_inos_till_wrap
--;
333 if (super
->s_inos_till_wrap
< 0) {
334 super
->s_last_ino
= LOGFS_RESERVED_INOS
;
335 super
->s_generation
++;
336 super
->s_inos_till_wrap
= INOS_PER_WRAP
;
339 inode
->i_generation
= super
->s_generation
;
340 mutex_unlock(&super
->s_journal_mutex
);
343 struct inode
*logfs_new_inode(struct inode
*dir
, umode_t mode
)
345 struct super_block
*sb
= dir
->i_sb
;
348 inode
= new_inode(sb
);
350 return ERR_PTR(-ENOMEM
);
352 logfs_init_inode(sb
, inode
);
354 /* inherit parent flags */
355 logfs_inode(inode
)->li_flags
|=
356 logfs_inode(dir
)->li_flags
& LOGFS_FL_INHERITED
;
358 inode
->i_mode
= mode
;
359 logfs_set_ino_generation(sb
, inode
);
361 inode_init_owner(inode
, dir
, mode
);
362 logfs_inode_setops(inode
);
363 insert_inode_hash(inode
);
368 static void logfs_init_once(void *_li
)
370 struct logfs_inode
*li
= _li
;
374 li
->li_used_bytes
= 0;
376 for (i
= 0; i
< LOGFS_EMBEDDED_FIELDS
; i
++)
378 inode_init_once(&li
->vfs_inode
);
381 static int logfs_sync_fs(struct super_block
*sb
, int wait
)
383 logfs_get_wblocks(sb
, NULL
, WF_LOCK
);
384 logfs_write_anchor(sb
);
385 logfs_put_wblocks(sb
, NULL
, WF_LOCK
);
389 static void logfs_put_super(struct super_block
*sb
)
391 struct logfs_super
*super
= logfs_super(sb
);
392 /* kill the meta-inodes */
393 iput(super
->s_segfile_inode
);
394 iput(super
->s_master_inode
);
395 iput(super
->s_mapping_inode
);
398 const struct super_operations logfs_super_operations
= {
399 .alloc_inode
= logfs_alloc_inode
,
400 .destroy_inode
= logfs_destroy_inode
,
401 .evict_inode
= logfs_evict_inode
,
402 .drop_inode
= logfs_drop_inode
,
403 .put_super
= logfs_put_super
,
404 .write_inode
= logfs_write_inode
,
405 .statfs
= logfs_statfs
,
406 .sync_fs
= logfs_sync_fs
,
409 int logfs_init_inode_cache(void)
411 logfs_inode_cache
= kmem_cache_create("logfs_inode_cache",
412 sizeof(struct logfs_inode
), 0,
413 SLAB_RECLAIM_ACCOUNT
|SLAB_ACCOUNT
,
415 if (!logfs_inode_cache
)
420 void logfs_destroy_inode_cache(void)
423 * Make sure all delayed rcu free inodes are flushed before we
427 kmem_cache_destroy(logfs_inode_cache
);