Whamcloud - gitweb
1)cleanup smfs for build in 2.6
[fs/lustre-release.git] / lustre / smfs / inode.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  lustre/smfs/inode.c
5  *  Lustre filesystem abstraction routines
6  *
7  *  Copyright (C) 2004 Cluster File Systems, Inc.
8  *
9  *   This file is part of Lustre, http://www.lustre.org.
10  *
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.
14  *
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.
19  *
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.
23  */
24
25 #define DEBUG_SUBSYSTEM S_SM
26
27 #include <linux/kmod.h>
28 #include <linux/init.h>
29 #include <linux/fs.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)
40 {
41         struct smfs_inode_info *sm_info = (struct smfs_inode_info *)opaque;
42         struct inode *cache_inode = NULL;
43  
44         cache_inode = iget(S2CSB(inode->i_sb), inode->i_ino);
45         
46         if (sm_info) 
47                 memcpy(I2SMI(inode), sm_info, sizeof(struct smfs_inode_info)); 
48         
49         I2CI(inode) = cache_inode;
50         post_smfs_inode(inode, cache_inode);
51         sm_set_inode_ops(cache_inode, inode);
52 #if CONFIG_SNAPFS
53         if (sm_info) {
54                 smfs_init_snap_inode_info(inode, &sm_info->sm_sninfo);
55         }
56 #endif
57 }
58
59 static void smfs_read_inode2(struct inode *inode, void *opaque)
60 {
61         ENTRY;
62         if (!inode)
63                 return;
64
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));
69         EXIT;
70         return;
71 }
72
73 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
74 static int smfs_test_inode(struct inode *inode, unsigned long ino, 
75                                   void *opaque)
76 #else
77 static int smfs_test_inode(struct inode *inode, void *opaque)
78 #endif
79 {
80         struct snap_inode_info *sn_info = &I2SMI(inode)->sm_sninfo;
81         struct snap_inode_info *test_info = (struct snap_inode_info *)opaque;
82        
83         if (!sn_info || !test_info)
84                 return 0;
85         if (sn_info->sn_index != test_info->sn_index)
86                 return 0;
87         if (sn_info->sn_gen != test_info->sn_gen)
88                 return 0;
89 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
90         smfs_init_cache_inode(inode, opaque);
91 #endif
92         return 1;
93 }
94
95 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
96 int smfs_set_inode(struct inode *inode, void *opaque)
97 {
98         smfs_read_inode2(inode, opaque);
99         return 0;
100 }
101
102 struct inode *smfs_iget(struct super_block *sb, ino_t hash,
103                         struct smfs_inode_info *si_info)
104 {
105         struct inode *inode;
106
107         LASSERT(hash != 0);
108         inode = iget5_locked(sb, hash, smfs_test_inode, smfs_set_inode, 
109                              si_info);
110
111         if (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);
116         }
117         return inode;
118 }
119 #else
120 struct inode *smfs_iget(struct super_block *sb, ino_t hash,
121                         struct smfs_inode_info *si_info)
122 {
123         struct inode *inode;
124         LASSERT(hash != 0);
125
126         inode = iget4(sb, hash, smfs_test_inode, si_info);
127
128         if (inode)
129                 CDEBUG(D_VFSTRACE, "inode: %lu/%u(%p)\n", inode->i_ino,
130                        inode->i_generation, inode);
131         return inode;
132 }
133 #endif
134 struct inode *smfs_get_inode (struct super_block *sb, ino_t hash,
135                               struct inode *dir, int index)
136 {
137         struct smfs_inode_info sm_info;
138         struct inode *inode = NULL;       
139         ENTRY;
140         
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);
146
147         RETURN(inode);
148
149 EXPORT_SYMBOL(smfs_get_inode);
150 static void smfs_delete_inode(struct inode *inode)
151 {
152         struct inode *cache_inode;
153
154         ENTRY;
155         cache_inode = I2CI(inode);
156
157         if (!cache_inode || !S2CSB(inode->i_sb))
158                 return;
159
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. */
163
164         if (atomic_read(&cache_inode->i_count) < 1)
165                 BUG();
166
167         while (atomic_read(&cache_inode->i_count) != 1)
168                 atomic_dec(&cache_inode->i_count);
169
170         pre_smfs_inode(inode, cache_inode);
171
172         if (atomic_read(&cache_inode->i_count) < 1)
173                 LBUG();
174
175         while (atomic_read(&cache_inode->i_count) != 1) {
176                 atomic_dec(&cache_inode->i_count);
177         }
178
179         pre_smfs_inode(inode, cache_inode);
180
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);
184 #else
185         hlist_del_init(&cache_inode->i_hash);
186 #endif
187
188         list_del(&cache_inode->i_list);
189         INIT_LIST_HEAD(&cache_inode->i_list);
190
191         if (cache_inode->i_data.nrpages)
192                 truncate_inode_pages(&cache_inode->i_data, 0);
193
194         if (S2CSB(inode->i_sb)->s_op->delete_inode)
195                 S2CSB(inode->i_sb)->s_op->delete_inode(cache_inode);
196
197         post_smfs_inode(inode, cache_inode);
198
199         I2CI(inode) = NULL;
200         return;
201 }
202
203 static void smfs_write_inode(struct inode *inode, int wait)
204 {
205         struct inode *cache_inode;
206
207         ENTRY;
208         cache_inode = I2CI(inode);
209
210         if (!cache_inode) {
211                 CWARN("cache inode null\n");
212                 return;
213         }
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);
217         
218         post_smfs_inode(inode, cache_inode);
219         EXIT;
220 }
221
222 static void smfs_dirty_inode(struct inode *inode)
223 {
224         struct inode *cache_inode;
225
226         ENTRY;
227         cache_inode = I2CI(inode);
228
229         if (!cache_inode || !S2CSB(inode->i_sb))
230                 return;
231
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);
235
236         post_smfs_inode(inode, cache_inode);
237         EXIT;
238 }
239
240 static void smfs_put_inode(struct inode *inode)
241 {
242         struct inode *cache_inode;
243
244         ENTRY;
245         cache_inode = I2CI(inode);
246
247         if (!cache_inode) {
248                 CWARN("cache inode null\n");
249                 return;
250         }
251         
252         if (atomic_read(&cache_inode->i_count) > 1 && 
253             cache_inode != cache_inode->i_sb->s_root->d_inode)
254                 iput(cache_inode);
255         
256         if (S2CSB(inode->i_sb)->s_op->put_inode)
257                 S2CSB(inode->i_sb)->s_op->put_inode(cache_inode);
258         
259         EXIT;
260 }
261
262 static void smfs_write_super(struct super_block *sb)
263 {
264         ENTRY;
265
266         if (!S2CSB(sb))
267                 return;
268
269         if (S2CSB(sb)->s_op->write_super)
270                 S2CSB(sb)->s_op->write_super(S2CSB(sb));
271         duplicate_sb(sb, S2CSB(sb));
272         EXIT;
273         return;
274 }
275
276 static void smfs_clear_inode(struct inode *inode)
277 {
278         struct inode *cache_inode;
279
280         ENTRY;
281
282         if (!inode) return;
283
284         cache_inode = I2CI(inode);
285
286         if (cache_inode != cache_inode->i_sb->s_root->d_inode) {
287                 iput(cache_inode);
288                 SMFS_CLEAN_INODE_REC(inode);
289                 I2CI(inode) = NULL;
290         }
291         EXIT;
292         return;
293 }
294
295 static void smfs_write_super_lockfs(struct super_block *sb)
296 {
297         struct super_block *cache_sb;
298         ENTRY;
299
300         cache_sb = S2CSB(sb);
301         if (!cache_sb)
302                 return;
303
304         if (cache_sb->s_op->write_super_lockfs)
305                 cache_sb->s_op->write_super_lockfs(cache_sb);
306
307         duplicate_sb(sb, cache_sb);
308         EXIT;
309 }
310
311 static void smfs_unlockfs(struct super_block *sb)
312 {
313         struct super_block *cache_sb;
314         ENTRY;
315
316         cache_sb = S2CSB(sb);
317         if (!cache_sb)
318                 return;
319
320         if (cache_sb->s_op->unlockfs)
321                 cache_sb->s_op->unlockfs(cache_sb);
322
323         duplicate_sb(sb, cache_sb);
324         EXIT;
325 }
326
327 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
328 static int smfs_statfs(struct super_block *sb, struct statfs *buf)
329 #else
330 static int smfs_statfs(struct super_block *sb, struct kstatfs *buf)
331 #endif
332 {
333         struct super_block *cache_sb;
334         int rc = 0;
335         ENTRY;
336
337         cache_sb = S2CSB(sb);
338         if (!cache_sb)
339                 RETURN(-EINVAL);
340
341         if (cache_sb->s_op->statfs)
342                 rc = cache_sb->s_op->statfs(cache_sb, buf);
343
344         duplicate_sb(sb, cache_sb);
345
346         RETURN(rc);
347 }
348 static int smfs_remount(struct super_block *sb, int *flags, char *data)
349 {
350         struct super_block *cache_sb;
351         int rc = 0;
352         ENTRY;
353
354         cache_sb = S2CSB(sb);
355
356         if (!cache_sb)
357                 RETURN(-EINVAL);
358
359         if (cache_sb->s_op->remount_fs)
360                 rc = cache_sb->s_op->remount_fs(cache_sb, flags, data);
361
362         duplicate_sb(sb, cache_sb);
363         RETURN(rc);
364 }
365 struct super_operations smfs_super_ops = {
366 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
367         .read_inode2        = smfs_read_inode2,
368 #endif 
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 */
380 };
381
382 int is_smfs_sb(struct super_block *sb)
383 {
384         return (sb->s_op->put_super == smfs_super_ops.put_super);
385 }
386 EXPORT_SYMBOL(is_smfs_sb);