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