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