Whamcloud - gitweb
update .snap on smfs
[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
40 static void smfs_init_inode_info (struct inode *inode, void *opaque)
41 {
42         struct smfs_iget_args *sargs = (struct smfs_iget_args*)opaque;
43         struct inode *cache_inode = NULL;
44         
45         if (sargs)
46                 cache_inode = iget(S2CSB(inode->i_sb), sargs->s_ino);
47         else 
48                 cache_inode = iget(S2CSB(inode->i_sb), inode->i_ino); 
49                  
50         OBD_ALLOC(I2SMI(inode), sizeof(struct smfs_inode_info));
51         LASSERT(I2SMI(inode));
52         I2CI(inode) = cache_inode;
53         CDEBUG(D_INODE, "cache_inode i_count ino %lu i_count %d\n",
54                cache_inode->i_ino, atomic_read(&cache_inode->i_count));
55         post_smfs_inode(inode, cache_inode);
56         sm_set_inode_ops(cache_inode, inode);
57         if (sargs) { 
58                 struct inode *dir = sargs->s_inode; 
59                 if (dir)
60                         I2SMI(inode)->smi_flags = I2SMI(dir)->smi_flags;
61         }
62 }
63
64 static void smfs_clear_inode_info(struct inode *inode)
65 {
66         if (I2SMI(inode)) {
67                 struct inode *cache_inode = I2CI(inode);
68                 LASSERTF(((atomic_read(&cache_inode->i_count) == 0) || 
69                            cache_inode == cache_inode->i_sb->s_root->d_inode),  
70                          "inode %p cache inode %p %lu i_count %d != 0 \n", 
71                           inode, cache_inode, cache_inode->i_ino, 
72                           atomic_read(&cache_inode->i_count));
73
74                 OBD_FREE(I2SMI(inode), sizeof(struct smfs_inode_info));
75                 I2SMI(inode) = NULL;
76         }
77 }
78
79 static void smfs_read_inode2(struct inode *inode, void *opaque)
80 {
81         ENTRY;
82
83         if (!inode)
84                 return;
85         
86         CDEBUG(D_INODE, "read_inode ino %lu\n", inode->i_ino);
87         smfs_init_inode_info(inode, opaque);
88         CDEBUG(D_INODE, "read_inode ino %lu icount %d \n",
89                inode->i_ino, atomic_read(&inode->i_count));
90         EXIT;
91         return;
92 }
93
94 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
95 static int smfs_test_inode(struct inode *inode, unsigned long ino, 
96                                   void *opaque)
97 #else
98 static int smfs_test_inode(struct inode *inode, void *opaque)
99 #endif
100 {
101         struct smfs_iget_args *sargs = (struct smfs_iget_args*)opaque;
102
103         LASSERT(sargs);
104         if (!sargs)
105                 return 1;
106 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
107         if (inode->i_ino != sargs->s_ino)
108                 return 0; 
109 #endif       
110 #ifdef CONFIG_SNAPFS        
111         if (SMFS_DO_COW(S2SMI(inode->i_sb)) && 
112             !smfs_snap_test_inode(inode, opaque))
113                 return 0;  
114 #endif        
115         return 1;
116 }
117
118 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
119 int smfs_set_inode(struct inode *inode, void *opaque)
120 {
121         smfs_read_inode2(inode, opaque);
122         return 0;
123 }
124
125 struct inode *smfs_iget(struct super_block *sb, ino_t hash,
126                         struct smfs_iget_args *sargs)
127 {
128         struct inode *inode;
129
130         LASSERT(hash != 0);
131
132         inode = iget5_locked(sb, hash, smfs_test_inode, smfs_set_inode, sargs);
133         if (inode) {
134                 if (inode->i_state & I_NEW)
135                         unlock_new_inode(inode);
136                 CDEBUG(D_VFSTRACE, "inode: %lu/%u(%p) index %d ino %lu \n", 
137                        inode->i_ino, inode->i_generation, inode, sargs->s_index, 
138                        sargs->s_ino);
139                 inode->i_ino = hash;
140         }
141         return inode;
142 }
143 #else
144 struct inode *smfs_iget(struct super_block *sb, ino_t hash,
145                         struct smfs_iget_args *sargs)
146 {
147         struct inode *inode;
148         LASSERT(hash != 0);
149
150         inode = iget4(sb, hash, smfs_test_inode, sargs);
151
152         if (inode)
153                 CDEBUG(D_VFSTRACE, "inode: %lu/%u(%p)\n", inode->i_ino,
154                        inode->i_generation, inode);
155         return inode;
156 }
157 #endif
158 struct inode *smfs_get_inode (struct super_block *sb, ino_t hash,
159                               struct inode *dir, int index)
160 {
161         struct inode *inode;
162         struct smfs_iget_args sargs; 
163         ENTRY;
164        
165         sargs.s_index = index;
166         sargs.s_inode = dir; 
167         sargs.s_ino = hash; 
168         inode = smfs_iget(sb, hash, &sargs);
169         
170         RETURN(inode);
171 }
172  
173 static void smfs_delete_inode(struct inode *inode)
174 {
175         struct inode *cache_inode;
176
177         ENTRY;
178         cache_inode = I2CI(inode);
179
180         if (!cache_inode || !S2CSB(inode->i_sb))
181                 return;
182
183         /* FIXME-WANGDI: because i_count of cache_inode may not be 0 or 1 in
184          * before smfs_delete inode, So we need to dec it to 1 before we call
185          * delete_inode of the bellow cache filesystem Check again latter. */
186
187         if (atomic_read(&cache_inode->i_count) < 1)
188                 BUG();
189
190         while (atomic_read(&cache_inode->i_count) != 1)
191                 atomic_dec(&cache_inode->i_count);
192
193         pre_smfs_inode(inode, cache_inode);
194
195         if (atomic_read(&cache_inode->i_count) < 1)
196                 LBUG();
197
198         while (atomic_read(&cache_inode->i_count) != 1) {
199                 atomic_dec(&cache_inode->i_count);
200         }
201
202         pre_smfs_inode(inode, cache_inode);
203
204 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
205         list_del(&cache_inode->i_hash);
206         INIT_LIST_HEAD(&cache_inode->i_hash);
207 #else
208         hlist_del_init(&cache_inode->i_hash);
209 #endif
210         list_del(&cache_inode->i_list);
211         INIT_LIST_HEAD(&cache_inode->i_list);
212
213         if (cache_inode->i_data.nrpages)
214                 truncate_inode_pages(&cache_inode->i_data, 0);
215
216         if (S2CSB(inode->i_sb)->s_op->delete_inode)
217                 S2CSB(inode->i_sb)->s_op->delete_inode(cache_inode);
218
219         post_smfs_inode(inode, cache_inode);
220
221         I2CI(inode) = NULL;
222         return;
223 }
224
225 static void smfs_write_inode(struct inode *inode, int wait)
226 {
227         struct inode *cache_inode;
228
229         ENTRY;
230         cache_inode = I2CI(inode);
231
232         if (!cache_inode) {
233                 CWARN("cache inode null\n");
234                 return;
235         }
236         pre_smfs_inode(inode, cache_inode);
237         if (S2CSB(inode->i_sb)->s_op->write_inode)
238                 S2CSB(inode->i_sb)->s_op->write_inode(cache_inode, wait);
239         
240         post_smfs_inode(inode, cache_inode);
241         EXIT;
242 }
243
244 static void smfs_dirty_inode(struct inode *inode)
245 {
246         struct inode *cache_inode;
247
248         ENTRY;
249         cache_inode = I2CI(inode);
250
251         if (!cache_inode || !S2CSB(inode->i_sb))
252                 return;
253
254         pre_smfs_inode(inode, cache_inode);
255         if (S2CSB(inode->i_sb)->s_op->dirty_inode)
256                 S2CSB(inode->i_sb)->s_op->dirty_inode(cache_inode);
257
258         post_smfs_inode(inode, cache_inode);
259         EXIT;
260 }
261
262 static void smfs_put_inode(struct inode *inode)
263 {
264         struct inode *cache_inode;
265
266         ENTRY;
267         cache_inode = I2CI(inode);
268
269         if (!cache_inode) {
270                 CWARN("cache inode null\n");
271                 return;
272         }
273         
274         if (atomic_read(&cache_inode->i_count) > 0) {
275                 iput(cache_inode);
276         }
277         
278         if (S2CSB(inode->i_sb)->s_op->put_inode)
279                 S2CSB(inode->i_sb)->s_op->put_inode(cache_inode);
280         
281         EXIT;
282 }
283
284 static void smfs_write_super(struct super_block *sb)
285 {
286         ENTRY;
287
288         if (!S2CSB(sb))
289                 return;
290
291         if (S2CSB(sb)->s_op->write_super)
292                 S2CSB(sb)->s_op->write_super(S2CSB(sb));
293         duplicate_sb(sb, S2CSB(sb));
294         EXIT;
295         return;
296 }
297
298 static void smfs_clear_inode(struct inode *inode)
299 {
300         struct inode *cache_inode;
301         
302         ENTRY;
303
304         if (!inode) return;
305         
306         cache_inode = I2CI(inode);
307
308         smfs_clear_inode_info(inode);
309         
310         EXIT;
311         return;
312 }
313
314 static void smfs_write_super_lockfs(struct super_block *sb)
315 {
316         struct super_block *cache_sb;
317         ENTRY;
318
319         cache_sb = S2CSB(sb);
320         if (!cache_sb)
321                 return;
322
323         if (cache_sb->s_op->write_super_lockfs)
324                 cache_sb->s_op->write_super_lockfs(cache_sb);
325
326         duplicate_sb(sb, cache_sb);
327         EXIT;
328 }
329
330 static void smfs_unlockfs(struct super_block *sb)
331 {
332         struct super_block *cache_sb;
333         ENTRY;
334
335         cache_sb = S2CSB(sb);
336         if (!cache_sb)
337                 return;
338
339         if (cache_sb->s_op->unlockfs)
340                 cache_sb->s_op->unlockfs(cache_sb);
341
342         duplicate_sb(sb, cache_sb);
343         EXIT;
344 }
345
346 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
347 static int smfs_statfs(struct super_block *sb, struct statfs *buf)
348 #else
349 static int smfs_statfs(struct super_block *sb, struct kstatfs *buf)
350 #endif
351 {
352         struct super_block *cache_sb;
353         int rc = 0;
354         ENTRY;
355
356         cache_sb = S2CSB(sb);
357         if (!cache_sb)
358                 RETURN(-EINVAL);
359
360         if (cache_sb->s_op->statfs)
361                 rc = cache_sb->s_op->statfs(cache_sb, buf);
362
363         duplicate_sb(sb, cache_sb);
364
365         RETURN(rc);
366 }
367 static int smfs_remount(struct super_block *sb, int *flags, char *data)
368 {
369         struct super_block *cache_sb;
370         int rc = 0;
371         ENTRY;
372
373         cache_sb = S2CSB(sb);
374
375         if (!cache_sb)
376                 RETURN(-EINVAL);
377
378         if (cache_sb->s_op->remount_fs)
379                 rc = cache_sb->s_op->remount_fs(cache_sb, flags, data);
380
381         duplicate_sb(sb, cache_sb);
382         RETURN(rc);
383 }
384 struct super_operations smfs_super_ops = {
385 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
386         .read_inode2        = smfs_read_inode2,
387 #endif 
388         .clear_inode        = smfs_clear_inode,
389         .put_super          = smfs_put_super,
390         .delete_inode       = smfs_delete_inode,
391         .write_inode        = smfs_write_inode,
392         .dirty_inode        = smfs_dirty_inode, /* BKL not held. */
393         .put_inode          = smfs_put_inode,   /* BKL not held. */
394         .write_super        = smfs_write_super, /* BKL held */
395         .write_super_lockfs = smfs_write_super_lockfs, /* BKL not held. */
396         .unlockfs           = smfs_unlockfs,    /* BKL not held. */
397         .statfs             = smfs_statfs,      /* BKL held */
398         .remount_fs         = smfs_remount,     /* BKL held */
399 };
400
401 int is_smfs_sb(struct super_block *sb)
402 {
403         return (sb->s_op->put_super == smfs_super_ops.put_super);
404 }
405 EXPORT_SYMBOL(is_smfs_sb);