1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
5 * Lustre filesystem abstraction routines
7 * Copyright (C) 2004 Cluster File Systems, Inc.
9 * This file is part of Lustre, http://www.lustre.org.
11 * Lustre is free software; you can redistribute it and/or
12 * modify it under the terms of version 2 of the GNU General Public
13 * License as published by the Free Software Foundation.
15 * Lustre is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with Lustre; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #define DEBUG_SUBSYSTEM S_SM
27 #include <linux/kmod.h>
28 #include <linux/init.h>
30 #include <linux/slab.h>
31 #include <linux/string.h>
32 #include <linux/obd_class.h>
33 #include <linux/obd_support.h>
34 #include <linux/lustre_lib.h>
35 #include <linux/lustre_idl.h>
36 #include <linux/lustre_fsfilt.h>
37 #include <linux/lustre_smfs.h>
38 #include "smfs_internal.h"
39 static void smfs_init_cache_inode (struct inode *inode, void *opaque)
41 struct smfs_inode_info *sm_info = (struct smfs_inode_info *)opaque;
42 struct inode *cache_inode = NULL;
44 cache_inode = iget(S2CSB(inode->i_sb), inode->i_ino);
47 memcpy(I2SMI(inode), sm_info, sizeof(struct smfs_inode_info));
49 I2CI(inode) = cache_inode;
50 post_smfs_inode(inode, cache_inode);
51 sm_set_inode_ops(cache_inode, inode);
54 smfs_init_snap_inode_info(inode, &sm_info->sm_sninfo);
59 static void smfs_read_inode2(struct inode *inode, void *opaque)
65 CDEBUG(D_INODE, "read_inode ino %lu\n", inode->i_ino);
66 smfs_init_cache_inode(inode, opaque);
67 CDEBUG(D_INODE, "read_inode ino %lu icount %d \n",
68 inode->i_ino, atomic_read(&inode->i_count));
73 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
74 static int smfs_test_inode(struct inode *inode, unsigned long ino,
77 static int smfs_test_inode(struct inode *inode, void *opaque)
80 struct snap_inode_info *sn_info = &I2SMI(inode)->sm_sninfo;
81 struct snap_inode_info *test_info = (struct snap_inode_info *)opaque;
83 if (!sn_info || !test_info)
85 if (sn_info->sn_index != test_info->sn_index)
87 if (sn_info->sn_gen != test_info->sn_gen)
89 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
90 smfs_init_cache_inode(inode, opaque);
95 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
96 int smfs_set_inode(struct inode *inode, void *opaque)
98 smfs_read_inode2(inode, opaque);
102 struct inode *smfs_iget(struct super_block *sb, ino_t hash,
103 struct smfs_inode_info *si_info)
108 inode = iget5_locked(sb, hash, smfs_test_inode, smfs_set_inode,
112 if (inode->i_state & I_NEW)
113 unlock_new_inode(inode);
114 CDEBUG(D_VFSTRACE, "inode: %lu/%u(%p)\n", inode->i_ino,
115 inode->i_generation, inode);
120 struct inode *smfs_iget(struct super_block *sb, ino_t hash,
121 struct smfs_inode_info *si_info)
126 inode = iget4(sb, hash, smfs_test_inode, si_info);
129 CDEBUG(D_VFSTRACE, "inode: %lu/%u(%p)\n", inode->i_ino,
130 inode->i_generation, inode);
134 struct inode *smfs_get_inode (struct super_block *sb, ino_t hash,
135 struct inode *dir, int index)
137 struct smfs_inode_info sm_info;
138 struct inode *inode = NULL;
141 sm_info.smi_flags = I2SMI(dir)->smi_flags;
142 sm_info.sm_sninfo.sn_flags = I2SMI(dir)->sm_sninfo.sn_flags;
143 sm_info.sm_sninfo.sn_index = index;
144 sm_info.sm_sninfo.sn_gen = I2SMI(dir)->sm_sninfo.sn_gen;
145 inode = smfs_iget(sb, hash, &sm_info);
149 EXPORT_SYMBOL(smfs_get_inode);
150 static void smfs_delete_inode(struct inode *inode)
152 struct inode *cache_inode;
155 cache_inode = I2CI(inode);
157 if (!cache_inode || !S2CSB(inode->i_sb))
160 /* FIXME-WANGDI: because i_count of cache_inode may not be 0 or 1 in
161 * before smfs_delete inode, So we need to dec it to 1 before we call
162 * delete_inode of the bellow cache filesystem Check again latter. */
164 if (atomic_read(&cache_inode->i_count) < 1)
167 while (atomic_read(&cache_inode->i_count) != 1)
168 atomic_dec(&cache_inode->i_count);
170 pre_smfs_inode(inode, cache_inode);
172 if (atomic_read(&cache_inode->i_count) < 1)
175 while (atomic_read(&cache_inode->i_count) != 1) {
176 atomic_dec(&cache_inode->i_count);
179 pre_smfs_inode(inode, cache_inode);
181 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
182 list_del(&cache_inode->i_hash);
183 INIT_LIST_HEAD(&cache_inode->i_hash);
185 hlist_del_init(&cache_inode->i_hash);
188 list_del(&cache_inode->i_list);
189 INIT_LIST_HEAD(&cache_inode->i_list);
191 if (cache_inode->i_data.nrpages)
192 truncate_inode_pages(&cache_inode->i_data, 0);
194 if (S2CSB(inode->i_sb)->s_op->delete_inode)
195 S2CSB(inode->i_sb)->s_op->delete_inode(cache_inode);
197 post_smfs_inode(inode, cache_inode);
203 static void smfs_write_inode(struct inode *inode, int wait)
205 struct inode *cache_inode;
208 cache_inode = I2CI(inode);
211 CWARN("cache inode null\n");
214 pre_smfs_inode(inode, cache_inode);
215 if (S2CSB(inode->i_sb)->s_op->write_inode)
216 S2CSB(inode->i_sb)->s_op->write_inode(cache_inode, wait);
218 post_smfs_inode(inode, cache_inode);
222 static void smfs_dirty_inode(struct inode *inode)
224 struct inode *cache_inode;
227 cache_inode = I2CI(inode);
229 if (!cache_inode || !S2CSB(inode->i_sb))
232 pre_smfs_inode(inode, cache_inode);
233 if (S2CSB(inode->i_sb)->s_op->dirty_inode)
234 S2CSB(inode->i_sb)->s_op->dirty_inode(cache_inode);
236 post_smfs_inode(inode, cache_inode);
240 static void smfs_put_inode(struct inode *inode)
242 struct inode *cache_inode;
245 cache_inode = I2CI(inode);
248 CWARN("cache inode null\n");
252 if (atomic_read(&cache_inode->i_count) > 1 &&
253 cache_inode != cache_inode->i_sb->s_root->d_inode)
256 if (S2CSB(inode->i_sb)->s_op->put_inode)
257 S2CSB(inode->i_sb)->s_op->put_inode(cache_inode);
262 static void smfs_write_super(struct super_block *sb)
269 if (S2CSB(sb)->s_op->write_super)
270 S2CSB(sb)->s_op->write_super(S2CSB(sb));
271 duplicate_sb(sb, S2CSB(sb));
276 static void smfs_clear_inode(struct inode *inode)
278 struct inode *cache_inode;
284 cache_inode = I2CI(inode);
286 if (cache_inode != cache_inode->i_sb->s_root->d_inode) {
288 SMFS_CLEAN_INODE_REC(inode);
295 static void smfs_write_super_lockfs(struct super_block *sb)
297 struct super_block *cache_sb;
300 cache_sb = S2CSB(sb);
304 if (cache_sb->s_op->write_super_lockfs)
305 cache_sb->s_op->write_super_lockfs(cache_sb);
307 duplicate_sb(sb, cache_sb);
311 static void smfs_unlockfs(struct super_block *sb)
313 struct super_block *cache_sb;
316 cache_sb = S2CSB(sb);
320 if (cache_sb->s_op->unlockfs)
321 cache_sb->s_op->unlockfs(cache_sb);
323 duplicate_sb(sb, cache_sb);
327 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
328 static int smfs_statfs(struct super_block *sb, struct statfs *buf)
330 static int smfs_statfs(struct super_block *sb, struct kstatfs *buf)
333 struct super_block *cache_sb;
337 cache_sb = S2CSB(sb);
341 if (cache_sb->s_op->statfs)
342 rc = cache_sb->s_op->statfs(cache_sb, buf);
344 duplicate_sb(sb, cache_sb);
348 static int smfs_remount(struct super_block *sb, int *flags, char *data)
350 struct super_block *cache_sb;
354 cache_sb = S2CSB(sb);
359 if (cache_sb->s_op->remount_fs)
360 rc = cache_sb->s_op->remount_fs(cache_sb, flags, data);
362 duplicate_sb(sb, cache_sb);
365 struct super_operations smfs_super_ops = {
366 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
367 .read_inode2 = smfs_read_inode2,
369 .clear_inode = smfs_clear_inode,
370 .put_super = smfs_put_super,
371 .delete_inode = smfs_delete_inode,
372 .write_inode = smfs_write_inode,
373 .dirty_inode = smfs_dirty_inode, /* BKL not held. */
374 .put_inode = smfs_put_inode, /* BKL not held. */
375 .write_super = smfs_write_super, /* BKL held */
376 .write_super_lockfs = smfs_write_super_lockfs, /* BKL not held. */
377 .unlockfs = smfs_unlockfs, /* BKL not held. */
378 .statfs = smfs_statfs, /* BKL held */
379 .remount_fs = smfs_remount, /* BKL held */
382 int is_smfs_sb(struct super_block *sb)
384 return (sb->s_op->put_super == smfs_super_ops.put_super);
386 EXPORT_SYMBOL(is_smfs_sb);