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