fs: remove inode_lock from iput_final and prune_icache
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / logfs / inode.c
blobedfea7a3a747fa2c984a5817d82909fdccc7d0ea
1 /*
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>
7 */
8 #include "logfs.h"
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 * 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.
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) {
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();
81 static struct inode *__logfs_iget(struct super_block *sb, ino_t ino)
83 struct inode *inode = iget_locked(sb, ino);
84 int err;
86 if (!inode)
87 return ERR_PTR(-ENOMEM);
88 if (!(inode->i_state & I_NEW))
89 return inode;
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 */
96 inode->i_nlink = 0;
97 logfs_inode(inode)->li_flags |= LOGFS_IF_ZOMBIE;
98 iget_failed(inode);
99 if (!err)
100 err = -ENOENT;
101 return ERR_PTR(err);
104 logfs_inode_setops(inode);
105 unlock_new_inode(inode);
106 return inode;
109 struct inode *logfs_iget(struct super_block *sb, ino_t ino)
111 BUG_ON(ino == LOGFS_INO_MASTER);
112 BUG_ON(ino == LOGFS_INO_SEGFILE);
113 return __logfs_iget(sb, ino);
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
120 struct inode *logfs_safe_iget(struct super_block *sb, ino_t ino, int *is_cached)
122 struct logfs_super *super = logfs_super(sb);
123 struct logfs_inode *li;
125 if (ino == LOGFS_INO_MASTER)
126 return super->s_master_inode;
127 if (ino == LOGFS_INO_SEGFILE)
128 return super->s_segfile_inode;
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;
138 spin_unlock(&logfs_inode_lock);
140 *is_cached = 0;
141 return __logfs_iget(sb, ino);
144 static void logfs_i_callback(struct rcu_head *head)
146 struct inode *inode = container_of(head, struct inode, i_rcu);
147 INIT_LIST_HEAD(&inode->i_dentry);
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_inode(struct inode *inode)
162 struct logfs_inode *li = logfs_inode(inode);
164 BUG_ON(list_empty(&li->li_freeing_list));
165 spin_lock(&logfs_inode_lock);
166 li->li_refcount--;
167 if (li->li_refcount == 0)
168 __logfs_destroy_inode(inode);
169 spin_unlock(&logfs_inode_lock);
172 void logfs_safe_iput(struct inode *inode, int is_cached)
174 if (inode->i_ino == LOGFS_INO_MASTER)
175 return;
176 if (inode->i_ino == LOGFS_INO_SEGFILE)
177 return;
179 if (is_cached) {
180 logfs_destroy_inode(inode);
181 return;
184 iput(inode);
187 static void logfs_init_inode(struct super_block *sb, struct inode *inode)
189 struct logfs_inode *li = logfs_inode(inode);
190 int i;
192 li->li_flags = 0;
193 li->li_height = 0;
194 li->li_used_bytes = 0;
195 li->li_block = NULL;
196 inode->i_uid = 0;
197 inode->i_gid = 0;
198 inode->i_size = 0;
199 inode->i_blocks = 0;
200 inode->i_ctime = CURRENT_TIME;
201 inode->i_mtime = CURRENT_TIME;
202 inode->i_nlink = 1;
203 li->li_refcount = 1;
204 INIT_LIST_HEAD(&li->li_freeing_list);
206 for (i = 0; i < LOGFS_EMBEDDED_FIELDS; i++)
207 li->li_data[i] = 0;
209 return;
212 static struct inode *logfs_alloc_inode(struct super_block *sb)
214 struct logfs_inode *li;
216 li = kmem_cache_alloc(logfs_inode_cache, GFP_NOFS);
217 if (!li)
218 return NULL;
219 logfs_init_inode(sb, &li->vfs_inode);
220 return &li->vfs_inode;
224 * In logfs inodes are written to an inode file. The inode file, like any
225 * other file, is managed with a inode. The inode file's inode, aka master
226 * inode, requires special handling in several respects. First, it cannot be
227 * written to the inode file, so it is stored in the journal instead.
229 * Secondly, this inode cannot be written back and destroyed before all other
230 * inodes have been written. The ordering is important. Linux' VFS is happily
231 * unaware of the ordering constraint and would ordinarily destroy the master
232 * inode at umount time while other inodes are still in use and dirty. Not
233 * good.
235 * So logfs makes sure the master inode is not written until all other inodes
236 * have been destroyed. Sadly, this method has another side-effect. The VFS
237 * will notice one remaining inode and print a frightening warning message.
238 * Worse, it is impossible to judge whether such a warning was caused by the
239 * master inode or any other inodes have leaked as well.
241 * Our attempt of solving this is with logfs_new_meta_inode() below. Its
242 * purpose is to create a new inode that will not trigger the warning if such
243 * an inode is still in use. An ugly hack, no doubt. Suggections for
244 * improvement are welcome.
246 * AV: that's what ->put_super() is for...
248 struct inode *logfs_new_meta_inode(struct super_block *sb, u64 ino)
250 struct inode *inode;
252 inode = new_inode(sb);
253 if (!inode)
254 return ERR_PTR(-ENOMEM);
256 inode->i_mode = S_IFREG;
257 inode->i_ino = ino;
258 inode->i_data.a_ops = &logfs_reg_aops;
259 mapping_set_gfp_mask(&inode->i_data, GFP_NOFS);
261 return inode;
264 struct inode *logfs_read_meta_inode(struct super_block *sb, u64 ino)
266 struct inode *inode;
267 int err;
269 inode = logfs_new_meta_inode(sb, ino);
270 if (IS_ERR(inode))
271 return inode;
273 err = logfs_read_inode(inode);
274 if (err) {
275 iput(inode);
276 return ERR_PTR(err);
278 logfs_inode_setops(inode);
279 return inode;
282 static int logfs_write_inode(struct inode *inode, struct writeback_control *wbc)
284 int ret;
285 long flags = WF_LOCK;
287 /* Can only happen if creat() failed. Safe to skip. */
288 if (logfs_inode(inode)->li_flags & LOGFS_IF_STILLBORN)
289 return 0;
291 ret = __logfs_write_inode(inode, flags);
292 LOGFS_BUG_ON(ret, inode->i_sb);
293 return ret;
296 /* called with inode->i_lock held */
297 static int logfs_drop_inode(struct inode *inode)
299 struct logfs_super *super = logfs_super(inode->i_sb);
300 struct logfs_inode *li = logfs_inode(inode);
302 spin_lock(&logfs_inode_lock);
303 list_move(&li->li_freeing_list, &super->s_freeing_list);
304 spin_unlock(&logfs_inode_lock);
305 return generic_drop_inode(inode);
308 static void logfs_set_ino_generation(struct super_block *sb,
309 struct inode *inode)
311 struct logfs_super *super = logfs_super(sb);
312 u64 ino;
314 mutex_lock(&super->s_journal_mutex);
315 ino = logfs_seek_hole(super->s_master_inode, super->s_last_ino + 1);
316 super->s_last_ino = ino;
317 super->s_inos_till_wrap--;
318 if (super->s_inos_till_wrap < 0) {
319 super->s_last_ino = LOGFS_RESERVED_INOS;
320 super->s_generation++;
321 super->s_inos_till_wrap = INOS_PER_WRAP;
323 inode->i_ino = ino;
324 inode->i_generation = super->s_generation;
325 mutex_unlock(&super->s_journal_mutex);
328 struct inode *logfs_new_inode(struct inode *dir, int mode)
330 struct super_block *sb = dir->i_sb;
331 struct inode *inode;
333 inode = new_inode(sb);
334 if (!inode)
335 return ERR_PTR(-ENOMEM);
337 logfs_init_inode(sb, inode);
339 /* inherit parent flags */
340 logfs_inode(inode)->li_flags |=
341 logfs_inode(dir)->li_flags & LOGFS_FL_INHERITED;
343 inode->i_mode = mode;
344 logfs_set_ino_generation(sb, inode);
346 inode_init_owner(inode, dir, mode);
347 logfs_inode_setops(inode);
348 insert_inode_hash(inode);
350 return inode;
353 static void logfs_init_once(void *_li)
355 struct logfs_inode *li = _li;
356 int i;
358 li->li_flags = 0;
359 li->li_used_bytes = 0;
360 li->li_refcount = 1;
361 for (i = 0; i < LOGFS_EMBEDDED_FIELDS; i++)
362 li->li_data[i] = 0;
363 inode_init_once(&li->vfs_inode);
366 static int logfs_sync_fs(struct super_block *sb, int wait)
368 logfs_write_anchor(sb);
369 return 0;
372 static void logfs_put_super(struct super_block *sb)
374 struct logfs_super *super = logfs_super(sb);
375 /* kill the meta-inodes */
376 iput(super->s_master_inode);
377 iput(super->s_segfile_inode);
378 iput(super->s_mapping_inode);
381 const struct super_operations logfs_super_operations = {
382 .alloc_inode = logfs_alloc_inode,
383 .destroy_inode = logfs_destroy_inode,
384 .evict_inode = logfs_evict_inode,
385 .drop_inode = logfs_drop_inode,
386 .put_super = logfs_put_super,
387 .write_inode = logfs_write_inode,
388 .statfs = logfs_statfs,
389 .sync_fs = logfs_sync_fs,
392 int logfs_init_inode_cache(void)
394 logfs_inode_cache = kmem_cache_create("logfs_inode_cache",
395 sizeof(struct logfs_inode), 0, SLAB_RECLAIM_ACCOUNT,
396 logfs_init_once);
397 if (!logfs_inode_cache)
398 return -ENOMEM;
399 return 0;
402 void logfs_destroy_inode_cache(void)
404 kmem_cache_destroy(logfs_inode_cache);