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