Whamcloud - gitweb
b=6285
[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         
43         struct inode *cache_inode = NULL;
44         struct smfs_iget_args *sargs = opaque;
45         
46         LASSERTF((!I2SMI(inode)), "Inode %lu already has smfs_inode_info %p \n",
47                  inode->i_ino, I2SMI(inode));
48         
49         /* getting backing fs inode. */
50         LASSERT(sargs);
51         
52         cache_inode = igrab(sargs->s_inode); 
53         LASSERT(cache_inode);
54         
55         OBD_ALLOC(inode->u.generic_ip, sizeof(struct smfs_inode_info));
56
57         LASSERT(inode->u.generic_ip);
58               
59         I2CI(inode) = cache_inode;
60         CDEBUG(D_INODE,"Init inode info #%lu (%p) icount %u\n", inode->i_ino, inode, 
61                         atomic_read(&cache_inode->i_count));
62         
63         post_smfs_inode(inode, cache_inode);
64         sm_set_inode_ops(inode);
65
66         //inherit parent inode flags
67         if (sargs->s_info) { 
68                 I2SMI(inode)->smi_flags = sargs->s_info->smi_flags;
69                 CDEBUG(D_INODE, "set inode %lu flags 0x%.8x\n", inode->i_ino,
70                       I2SMI(inode)->smi_flags);
71         } 
72 }
73
74 static void smfs_clear_inode_info(struct inode *inode)
75 {
76         struct inode *cache_inode = I2CI(inode);
77         struct smfs_inode_info * info = I2SMI(inode);
78         
79         CDEBUG(D_INODE, "Clear_info: inode %lu (%p)\n", inode->i_ino, inode);
80
81         inode->u.generic_ip = NULL;      
82         iput(cache_inode);
83         OBD_FREE(info, sizeof(*info));
84 }
85
86 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
87 static void smfs_read_inode2(struct inode *inode, void *opaque)
88 {
89         ENTRY;
90
91         if (!inode) {
92                 EXIT;
93                 return;
94         }
95         
96         smfs_init_inode_info(inode, opaque);
97         CDEBUG(D_INODE, "read_inode ino %lu icount %d \n",
98                inode->i_ino, atomic_read(&inode->i_count));
99         EXIT;
100 }
101
102 static int smfs_test_inode(struct inode *inode, unsigned long ino, 
103                            void *opaque)
104 #else
105 static int smfs_test_inode(struct inode *inode, void *opaque)
106 #endif
107 {
108         struct smfs_iget_args *sargs = (struct smfs_iget_args*)opaque;
109
110         if (!sargs || (inode->i_ino != sargs->s_inode->i_ino))
111                 return 0;
112         
113 #ifdef CONFIG_SNAPFS
114         if (SMFS_DO_COW(S2SMI(inode->i_sb)) && 
115             !smfs_snap_test_inode(inode, opaque))
116                 return 0;  
117 #endif
118                 
119         return 1;
120 }
121
122 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
123 int smfs_set_inode(struct inode *inode, void *opaque)
124 {
125         struct smfs_iget_args *sargs = opaque;
126         
127         inode->i_ino = sargs->s_inode->i_ino;
128         
129         return 0;
130 }
131
132 static struct inode *smfs_iget(struct super_block *sb, ino_t hash,
133                         struct smfs_iget_args *sargs)
134 {
135         struct inode *inode;
136         LASSERT(hash != 0);
137
138         inode = iget5_locked(sb, hash, smfs_test_inode,
139                              smfs_set_inode, sargs);
140         if (inode) {
141                 if (inode->i_state & I_NEW) {
142                         smfs_init_inode_info(inode, (void*)sargs);
143                         unlock_new_inode(inode);
144                 }
145                 
146                 CDEBUG(D_INODE, "inode: %lu/%u(%p) index %d\n",
147                                 inode->i_ino, inode->i_generation,
148                                 inode, sargs->s_index);
149                 
150         }
151         return inode;
152 }
153 #else
154 struct inode *smfs_iget(struct super_block *sb, ino_t hash,
155                         struct smfs_iget_args *sargs)
156 {
157         struct inode *inode;
158         LASSERT(hash != 0);
159
160         inode = iget4(sb, hash, smfs_test_inode, sargs);
161         if (inode) {
162                 struct inode *cache_inode = I2CI(inode);
163                 
164                 CDEBUG(D_INODE, "new inode: %lu/%u(%p)\n", inode->i_ino,
165                        inode->i_generation, inode);
166         }
167         return inode;
168 }
169 #endif
170
171 struct inode *smfs_get_inode(struct super_block *sb, struct inode * cache_inode,
172                              struct smfs_inode_info * dir_info, int index)
173 {
174         struct smfs_iget_args sargs;
175         struct inode *inode;
176         ENTRY;
177        
178         sargs.s_inode = cache_inode; 
179         sargs.s_info = dir_info; 
180         sargs.s_index = index;
181         
182         inode = smfs_iget(sb, cache_inode->i_ino, &sargs);
183         LASSERT(inode);
184         RETURN(inode);
185 }
186 #ifdef FC3_KERNEL 
187 static int smfs_write_inode(struct inode *inode, int wait)
188 #else
189 static void smfs_write_inode(struct inode *inode, int wait)
190 #endif
191 {
192         struct inode *cache_inode = I2CI(inode);
193 #ifdef FC3_KERNEL
194         int rc = 0;
195 #endif        
196         ENTRY;
197
198         LASSERT(cache_inode);
199         
200         CDEBUG(D_INODE,"Write inode %lu\n",inode->i_ino);
201
202         pre_smfs_inode(inode, cache_inode);
203         
204 #ifdef FC3_KERNEL
205         rc = cache_inode->i_sb->s_op->write_inode(cache_inode, wait);
206 #else
207         cache_inode->i_sb->s_op->write_inode(cache_inode, wait);
208 #endif
209         post_smfs_inode(inode, cache_inode);
210         
211 #ifdef FC3_KERNEL
212         RETURN(rc);
213 #else
214         EXIT;
215 #endif
216 }
217
218 static void smfs_dirty_inode(struct inode *inode)
219 {
220         struct inode *cache_inode;
221         ENTRY;
222
223         cache_inode = I2CI(inode);
224         LASSERT(cache_inode);
225         
226         pre_smfs_inode(inode, cache_inode);
227     
228         S2CSB(inode->i_sb)->s_op->dirty_inode(cache_inode);
229
230         post_smfs_inode(inode, cache_inode);
231         EXIT;
232 }
233
234 static void smfs_delete_inode(struct inode *inode)
235 {
236         //struct inode * cache_inode = I2CI(inode);
237
238         //smfs_clear_inode_info(inode);
239         clear_inode(inode);
240 }
241
242 static void smfs_clear_inode(struct inode *inode)
243 {
244         smfs_clear_inode_info(inode);
245 }
246
247 static void smfs_write_super(struct super_block *sb)
248 {
249         ENTRY;
250
251         LASSERT(S2CSB(sb));
252
253         S2CSB(sb)->s_op->write_super(S2CSB(sb));
254         duplicate_sb(sb, S2CSB(sb));
255         EXIT;
256 }
257
258 static void smfs_write_super_lockfs(struct super_block *sb)
259 {
260         struct super_block * cache_sb = S2CSB(sb);
261         ENTRY;
262
263         LASSERT(cache_sb);
264
265         cache_sb->s_op->write_super_lockfs(cache_sb);
266         duplicate_sb(sb, cache_sb);
267         EXIT;
268 }
269
270 static void smfs_unlockfs(struct super_block *sb)
271 {
272         struct super_block * cache_sb = S2CSB(sb);
273         ENTRY;
274
275         LASSERT(cache_sb);
276         
277         cache_sb->s_op->unlockfs(cache_sb);
278
279         duplicate_sb(sb, cache_sb);
280         EXIT;
281 }
282
283 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
284 static int smfs_statfs(struct super_block *sb, struct statfs *buf)
285 #else
286 static int smfs_statfs(struct super_block *sb, struct kstatfs *buf)
287 #endif
288 {
289         struct super_block *cache_sb = S2CSB(sb);
290         int rc = 0;
291         ENTRY;
292
293         LASSERT(cache_sb);
294
295         rc = cache_sb->s_op->statfs(cache_sb, buf);
296         duplicate_sb(sb, cache_sb);
297
298         RETURN(rc);
299 }
300
301 static int smfs_remount(struct super_block *sb, int *flags, char *data)
302 {
303         struct super_block *cache_sb = S2CSB(sb);
304         int rc = 0;
305         ENTRY;
306
307         LASSERT(cache_sb);
308
309         rc = cache_sb->s_op->remount_fs(cache_sb, flags, data);
310         duplicate_sb(sb, cache_sb);
311
312         RETURN(rc);
313 }
314
315 struct super_operations smfs_super_ops = {
316 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
317         .read_inode2        = smfs_read_inode2,
318 #endif 
319         .clear_inode        = smfs_clear_inode,
320         .put_super          = smfs_put_super,
321         .delete_inode       = smfs_delete_inode,
322         .write_inode        = smfs_write_inode,
323         .dirty_inode        = smfs_dirty_inode, /* BKL not held. */
324         .write_super        = smfs_write_super, /* BKL held */
325         .write_super_lockfs = smfs_write_super_lockfs, /* BKL not held. */
326         .unlockfs           = smfs_unlockfs,    /* BKL not held. */
327         .statfs             = smfs_statfs,      /* BKL held */
328         .remount_fs         = smfs_remount,     /* BKL held */
329 };
330
331