Whamcloud - gitweb
- removed redundant check and lock release. If name is not passed to mds_id2locked_de...
[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         if (!I2SMI(inode)) {
43                 struct inode *cache_inode = NULL;
44                 struct smfs_iget_args *sargs;
45                 unsigned long ino;
46
47                 sargs = (struct smfs_iget_args *)opaque;
48                 
49                 /* getting backing fs inode. */
50                 ino = sargs ? sargs->s_ino : inode->i_ino;
51                 cache_inode = iget(S2CSB(inode->i_sb), ino); 
52
53                 OBD_ALLOC(inode->u.generic_ip,
54                           sizeof(struct smfs_inode_info));
55         
56                 LASSERT(inode->u.generic_ip);
57                 I2CI(inode) = cache_inode;
58         
59                 CDEBUG(D_INODE, "cache_inode i_count ino %lu i_count %d\n",
60                        cache_inode->i_ino, atomic_read(&cache_inode->i_count));
61         
62                 post_smfs_inode(inode, cache_inode);
63                 sm_set_inode_ops(cache_inode, inode);
64         
65                 if (sargs) { 
66                         struct inode *dir = sargs->s_inode; 
67                         if (dir)
68                                 I2SMI(inode)->smi_flags = I2SMI(dir)->smi_flags;
69                 }
70         }
71 }
72
73 static void smfs_clear_inode_info(struct inode *inode)
74 {
75         if (I2SMI(inode)) {
76                 struct inode *cache_inode = I2CI(inode);
77
78                 LASSERTF(((atomic_read(&cache_inode->i_count) == 1) || 
79                           cache_inode == cache_inode->i_sb->s_root->d_inode),
80                          "inode %p cache inode %p %lu i_count %d != 0 \n", 
81                          inode, cache_inode, cache_inode->i_ino, 
82                          atomic_read(&cache_inode->i_count));
83
84                 if (cache_inode != cache_inode->i_sb->s_root->d_inode)
85                         iput(cache_inode);
86                 
87                 OBD_FREE(inode->u.generic_ip,
88                          sizeof(struct smfs_inode_info));
89                 inode->u.generic_ip = NULL;
90         }
91 }
92
93 static void smfs_read_inode2(struct inode *inode, void *opaque)
94 {
95         ENTRY;
96
97         if (!inode) {
98                 EXIT;
99                 return;
100         }
101         
102         CDEBUG(D_INODE, "read_inode ino %lu\n", inode->i_ino);
103         smfs_init_inode_info(inode, opaque);
104         CDEBUG(D_INODE, "read_inode ino %lu icount %d \n",
105                inode->i_ino, atomic_read(&inode->i_count));
106         EXIT;
107 }
108
109 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
110 static int smfs_test_inode(struct inode *inode, unsigned long ino, 
111                            void *opaque)
112 #else
113 static int smfs_test_inode(struct inode *inode, void *opaque)
114 #endif
115 {
116         struct smfs_iget_args *sargs = (struct smfs_iget_args*)opaque;
117
118         LASSERT(sargs);
119
120         if (!sargs)
121                 return 1;
122
123         if (inode->i_ino != sargs->s_ino)
124                 return 0;
125         
126 #ifdef CONFIG_SNAPFS
127         if (SMFS_DO_COW(S2SMI(inode->i_sb)) && 
128             !smfs_snap_test_inode(inode, opaque))
129                 return 0;  
130 #endif
131         if (I2SMI(inode)) {
132                 struct inode *cache_inode = I2CI(inode);
133
134                 /* this is needed to make symatry between smfs_put_inode(). */
135                 LASSERT(cache_inode != NULL);
136                 igrab(cache_inode);
137                 
138                 LASSERTF(cache_inode->i_ino == inode->i_ino, 
139                          "inode ino %lu != cache ino %lu",
140                          cache_inode->i_ino, inode->i_ino); 
141         } else {
142                 smfs_init_inode_info(inode, opaque);
143         }
144         
145         return 1;
146 }
147
148 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
149 int smfs_set_inode(struct inode *inode, void *opaque)
150 {
151         smfs_read_inode2(inode, opaque);
152         return 0;
153 }
154
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 = iget5_locked(sb, hash, smfs_test_inode,
162                              smfs_set_inode, sargs);
163         if (inode) {
164                 if (inode->i_state & I_NEW)
165                         unlock_new_inode(inode);
166                 CDEBUG(D_VFSTRACE, "inode: %lu/%u(%p) index %d "
167                        "ino %d\n", inode->i_ino, inode->i_generation,
168                        inode, sargs->s_index, sargs->s_ino);
169                 inode->i_ino = hash;
170         }
171         return inode;
172 }
173 #else
174 struct inode *smfs_iget(struct super_block *sb, ino_t hash,
175                         struct smfs_iget_args *sargs)
176 {
177         struct inode *inode;
178         LASSERT(hash != 0);
179
180         inode = iget4(sb, hash, smfs_test_inode, sargs);
181         if (inode) {
182                 struct inode *cache_inode = I2CI(inode);
183                 LASSERTF((inode->i_ino == cache_inode->i_ino), 
184                          "inode %p ino %lu != cache inode %p ino %lu",
185                           inode, inode->i_ino, cache_inode, cache_inode->i_ino); 
186                 CDEBUG(D_VFSTRACE, "inode: %lu/%u(%p)\n", inode->i_ino,
187                        inode->i_generation, inode);
188         }
189         return inode;
190 }
191 #endif
192
193 struct inode *smfs_get_inode(struct super_block *sb, ino_t hash,
194                              struct inode *dir, int index)
195 {
196         struct smfs_iget_args sargs;
197         struct inode *inode;
198         ENTRY;
199        
200         sargs.s_ino = hash; 
201         sargs.s_inode = dir; 
202         sargs.s_index = index;
203         inode = smfs_iget(sb, hash, &sargs);
204
205         RETURN(inode);
206 }
207  
208 static void smfs_delete_inode(struct inode *inode)
209 {
210         ENTRY;
211         clear_inode(inode);
212         EXIT;
213 }
214
215 static void smfs_write_inode(struct inode *inode, int wait)
216 {
217         struct inode *cache_inode;
218         ENTRY;
219
220         cache_inode = I2CI(inode);
221         LASSERT(cache_inode != NULL);
222
223         pre_smfs_inode(inode, cache_inode);
224         if (S2CSB(inode->i_sb)->s_op->write_inode)
225                 S2CSB(inode->i_sb)->s_op->write_inode(cache_inode, wait);
226         
227         post_smfs_inode(inode, cache_inode);
228         EXIT;
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         if (!cache_inode || !S2CSB(inode->i_sb)) {
238                 EXIT;
239                 return;
240         }
241
242         pre_smfs_inode(inode, cache_inode);
243         if (S2CSB(inode->i_sb)->s_op->dirty_inode)
244                 S2CSB(inode->i_sb)->s_op->dirty_inode(cache_inode);
245
246         post_smfs_inode(inode, cache_inode);
247         EXIT;
248 }
249
250 static void smfs_put_inode(struct inode *inode)
251 {
252         struct inode *cache_inode;
253         ENTRY;
254
255         cache_inode = I2CI(inode);
256         LASSERT(cache_inode != NULL);
257
258         if (atomic_read(&cache_inode->i_count) > 1)
259                 iput(cache_inode);
260
261         /*
262          * check if we have to release backend inode. As iput() calls fs
263          * specific ->put_inode() first and than decrements inode rerfcount, we
264          * should check here ->i_count for 1, not for 0.
265
266          * In principle we could rely on ->clear_inode(), but it will be called
267          * later, and thus we will have iputing of @inode and @cache_inode in
268          * different control paths, this is why we do it here. --umka
269          */
270         if (atomic_read(&inode->i_count) == 1 &&
271             cache_inode->i_sb->s_root->d_inode != cache_inode)
272                 smfs_clear_inode_info(inode);
273         
274         EXIT;
275 }
276
277 static void smfs_clear_inode(struct inode *inode)
278 {
279         ENTRY;
280         smfs_clear_inode_info(inode);
281         EXIT;
282 }
283
284 static void smfs_write_super(struct super_block *sb)
285 {
286         ENTRY;
287
288         if (!S2CSB(sb)) {
289                 EXIT;
290                 return;
291         }
292
293         if (S2CSB(sb)->s_op->write_super)
294                 S2CSB(sb)->s_op->write_super(S2CSB(sb));
295         duplicate_sb(sb, S2CSB(sb));
296         EXIT;
297 }
298
299 static void smfs_write_super_lockfs(struct super_block *sb)
300 {
301         struct super_block *cache_sb;
302         ENTRY;
303
304         cache_sb = S2CSB(sb);
305         if (!cache_sb) {
306                 EXIT;
307                 return;
308         }
309
310         if (cache_sb->s_op->write_super_lockfs)
311                 cache_sb->s_op->write_super_lockfs(cache_sb);
312
313         duplicate_sb(sb, cache_sb);
314         EXIT;
315 }
316
317 static void smfs_unlockfs(struct super_block *sb)
318 {
319         struct super_block *cache_sb;
320         ENTRY;
321
322         cache_sb = S2CSB(sb);
323         if (!cache_sb) {
324                 EXIT;
325                 return;
326         }
327
328         if (cache_sb->s_op->unlockfs)
329                 cache_sb->s_op->unlockfs(cache_sb);
330
331         duplicate_sb(sb, cache_sb);
332         EXIT;
333 }
334
335 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
336 static int smfs_statfs(struct super_block *sb, struct statfs *buf)
337 #else
338 static int smfs_statfs(struct super_block *sb, struct kstatfs *buf)
339 #endif
340 {
341         struct super_block *cache_sb;
342         int rc = 0;
343         ENTRY;
344
345         cache_sb = S2CSB(sb);
346         if (!cache_sb)
347                 RETURN(-EINVAL);
348
349         if (cache_sb->s_op->statfs)
350                 rc = cache_sb->s_op->statfs(cache_sb, buf);
351
352         duplicate_sb(sb, cache_sb);
353         RETURN(rc);
354 }
355 static int smfs_remount(struct super_block *sb, int *flags, char *data)
356 {
357         struct super_block *cache_sb;
358         int rc = 0;
359         ENTRY;
360
361         cache_sb = S2CSB(sb);
362
363         if (!cache_sb)
364                 RETURN(-EINVAL);
365
366         if (cache_sb->s_op->remount_fs)
367                 rc = cache_sb->s_op->remount_fs(cache_sb, flags, data);
368
369         duplicate_sb(sb, cache_sb);
370         RETURN(rc);
371 }
372
373 struct super_operations smfs_super_ops = {
374 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
375         .read_inode2        = smfs_read_inode2,
376 #endif 
377         .clear_inode        = smfs_clear_inode,
378         .put_super          = smfs_put_super,
379         .delete_inode       = smfs_delete_inode,
380         .write_inode        = smfs_write_inode,
381         .dirty_inode        = smfs_dirty_inode, /* BKL not held. */
382         .put_inode          = smfs_put_inode,   /* BKL not held. */
383         .write_super        = smfs_write_super, /* BKL held */
384         .write_super_lockfs = smfs_write_super_lockfs, /* BKL not held. */
385         .unlockfs           = smfs_unlockfs,    /* BKL not held. */
386         .statfs             = smfs_statfs,      /* BKL held */
387         .remount_fs         = smfs_remount,     /* BKL held */
388 };
389
390 int is_smfs_sb(struct super_block *sb)
391 {
392         return (sb->s_op->put_super == smfs_super_ops.put_super);
393 }
394 EXPORT_SYMBOL(is_smfs_sb);