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