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