Whamcloud - gitweb
8360b18bb5756c8b58226c8f3d5ddabd52272f20
[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_read_inode(struct inode *inode)
41 {
42         struct inode *cache_inode;
43         ENTRY;
44
45         if (!inode)
46                 return;
47
48         CDEBUG(D_INODE, "read_inode ino %lu\n", inode->i_ino);
49
50         cache_inode = iget(S2CSB(inode->i_sb), inode->i_ino);
51
52         I2CI(inode) = cache_inode;
53         
54         post_smfs_inode(inode, cache_inode);
55         sm_set_inode_ops(cache_inode, inode);
56
57         CDEBUG(D_INODE, "read_inode ino %lu icount %d \n",
58                inode->i_ino, atomic_read(&inode->i_count));
59         return;
60 }
61
62 static void smfs_read_inode2(struct inode *inode, void *opaque)
63 {
64         struct inode *cache_inode;
65         ENTRY;
66
67         if (!inode)
68                 return;
69
70         CDEBUG(D_INODE, "read_inode ino %lu\n", inode->i_ino);
71
72         cache_inode = iget(S2CSB(inode->i_sb), inode->i_ino);
73
74         if (opaque)
75                 I2SMI(inode)->smi_flags = *((int *)opaque);
76
77         I2CI(inode) = cache_inode;
78
79         post_smfs_inode(inode, cache_inode);
80         sm_set_inode_ops(cache_inode, inode);
81 #if CONFIG_SNAPFS
82         if (opaque)
83                 smfs_init_snap_inode_info(inode, *((int *)opaque));
84 #endif
85         CDEBUG(D_INODE, "read_inode ino %lu icount %d \n",
86                inode->i_ino, atomic_read(&inode->i_count));
87         return;
88 }
89
90 /* Although some filesystem(such as ext3) do not have
91  * clear_inode method, but we need it to free the
92  * cache inode
93  */
94 static void smfs_clear_inode(struct inode *inode)
95 {
96         struct inode *cache_inode;
97
98         ENTRY;
99
100         if (!inode) return;
101
102         cache_inode = I2CI(inode);
103
104         /* FIXME: because i_count of cache_inode may not
105          * be 0 or 1 in before smfs_delete inode, So we
106          * need to dec it to 1 before we call delete_inode
107          * of the bellow cache filesystem Check again latter. */
108         if (cache_inode != cache_inode->i_sb->s_root->d_inode) {
109                 struct list_head *lp;
110                 struct dentry *tmp;
111                 while (!list_empty(&cache_inode->i_dentry)) {
112                         lp = cache_inode->i_dentry.next;
113                         tmp = list_entry(lp, struct dentry, d_alias);
114                         if (atomic_read(&tmp->d_count) >= 1)
115                                 post_smfs_dentry(tmp);
116                 }
117                 if (atomic_read(&cache_inode->i_count) < 1)
118                         LBUG();
119                                                                                                                                                                                                      
120                 while (atomic_read(&cache_inode->i_count) != 1) {
121                         atomic_dec(&cache_inode->i_count);
122                 }
123                 iput(cache_inode);
124                 SMFS_CLEAN_INODE_REC(inode);
125                 I2CI(inode) = NULL;
126         }
127         return;
128 }
129
130 static void smfs_delete_inode(struct inode *inode)
131 {
132         struct inode *cache_inode;
133
134         ENTRY;
135         cache_inode = I2CI(inode);
136
137         if (!cache_inode || !S2CSB(inode->i_sb))
138                 return;
139
140         /* FIXME-WANGDI: because i_count of cache_inode may not be 0 or 1 in
141          * before smfs_delete inode, So we need to dec it to 1 before we call
142          * delete_inode of the bellow cache filesystem Check again latter. */
143
144         if (atomic_read(&cache_inode->i_count) < 1)
145                 BUG();
146
147         while (atomic_read(&cache_inode->i_count) != 1)
148                 atomic_dec(&cache_inode->i_count);
149
150         pre_smfs_inode(inode, cache_inode);
151
152         if (atomic_read(&cache_inode->i_count) < 1)
153                 LBUG();
154
155         while (atomic_read(&cache_inode->i_count) != 1) {
156                 atomic_dec(&cache_inode->i_count);
157         }
158
159         pre_smfs_inode(inode, cache_inode);
160
161 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
162         list_del(&cache_inode->i_hash);
163         INIT_LIST_HEAD(&cache_inode->i_hash);
164 #else
165         hlist_del_init(&cache_inode->i_hash);
166 #endif
167
168         list_del(&cache_inode->i_list);
169         INIT_LIST_HEAD(&cache_inode->i_list);
170
171         if (cache_inode->i_data.nrpages)
172                 truncate_inode_pages(&cache_inode->i_data, 0);
173
174         if (S2CSB(inode->i_sb)->s_op->delete_inode)
175                 S2CSB(inode->i_sb)->s_op->delete_inode(cache_inode);
176
177         post_smfs_inode(inode, cache_inode);
178
179         I2CI(inode) = NULL;
180         return;
181 }
182
183 static void smfs_write_inode(struct inode *inode, int wait)
184 {
185         struct inode *cache_inode;
186
187         ENTRY;
188         cache_inode = I2CI(inode);
189
190         if (!cache_inode) {
191                 CWARN("cache inode null\n");
192                 return;
193         }
194         pre_smfs_inode(inode, cache_inode);
195         if (S2CSB(inode->i_sb)->s_op->write_inode)
196                 S2CSB(inode->i_sb)->s_op->write_inode(cache_inode, wait);
197         
198         post_smfs_inode(inode, cache_inode);
199         EXIT;
200 }
201
202 static void smfs_dirty_inode(struct inode *inode)
203 {
204         struct inode *cache_inode;
205
206         ENTRY;
207         cache_inode = I2CI(inode);
208
209         if (!cache_inode || !S2CSB(inode->i_sb))
210                 return;
211
212         pre_smfs_inode(inode, cache_inode);
213         if (S2CSB(inode->i_sb)->s_op->dirty_inode)
214                 S2CSB(inode->i_sb)->s_op->dirty_inode(cache_inode);
215
216         post_smfs_inode(inode, cache_inode);
217         EXIT;
218 }
219
220 static void smfs_put_inode(struct inode *inode)
221 {
222         struct inode *cache_inode;
223
224         ENTRY;
225         cache_inode = I2CI(inode);
226
227         if (!cache_inode) {
228                 CWARN("cache inode null\n");
229                 return;
230         }
231         if (S2CSB(inode->i_sb)->s_op->put_inode)
232                 S2CSB(inode->i_sb)->s_op->put_inode(cache_inode);
233
234         EXIT;
235 }
236
237 static void smfs_write_super(struct super_block *sb)
238 {
239         ENTRY;
240
241         if (!S2CSB(sb))
242                 return;
243
244         if (S2CSB(sb)->s_op->write_super)
245                 S2CSB(sb)->s_op->write_super(S2CSB(sb));
246         duplicate_sb(sb, S2CSB(sb));
247         EXIT;
248 }
249
250 static void smfs_write_super_lockfs(struct super_block *sb)
251 {
252         struct super_block *cache_sb;
253         ENTRY;
254
255         cache_sb = S2CSB(sb);
256         if (!cache_sb)
257                 return;
258
259         if (cache_sb->s_op->write_super_lockfs)
260                 cache_sb->s_op->write_super_lockfs(cache_sb);
261
262         duplicate_sb(sb, cache_sb);
263         EXIT;
264 }
265
266 static void smfs_unlockfs(struct super_block *sb)
267 {
268         struct super_block *cache_sb;
269         ENTRY;
270
271         cache_sb = S2CSB(sb);
272         if (!cache_sb)
273                 return;
274
275         if (cache_sb->s_op->unlockfs)
276                 cache_sb->s_op->unlockfs(cache_sb);
277
278         duplicate_sb(sb, cache_sb);
279         EXIT;
280 }
281
282 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
283 static int smfs_statfs(struct super_block *sb, struct statfs *buf)
284 #else
285 static int smfs_statfs(struct super_block *sb, struct kstatfs *buf)
286 #endif
287 {
288         struct super_block *cache_sb;
289         int rc = 0;
290         ENTRY;
291
292         cache_sb = S2CSB(sb);
293         if (!cache_sb)
294                 RETURN(-EINVAL);
295
296         if (cache_sb->s_op->statfs)
297                 rc = cache_sb->s_op->statfs(cache_sb, buf);
298
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;
307         int rc = 0;
308         ENTRY;
309
310         cache_sb = S2CSB(sb);
311
312         if (!cache_sb)
313                 RETURN(-EINVAL);
314
315         if (cache_sb->s_op->remount_fs)
316                 rc = cache_sb->s_op->remount_fs(cache_sb, flags, data);
317
318         duplicate_sb(sb, cache_sb);
319         RETURN(rc);
320 }
321
322 struct super_operations smfs_super_ops = {
323         .read_inode         = smfs_read_inode,
324         .read_inode2        = smfs_read_inode2,
325         .clear_inode        = smfs_clear_inode,
326         .put_super          = smfs_put_super,
327         .delete_inode       = smfs_delete_inode,
328         .write_inode        = smfs_write_inode,
329         .dirty_inode        = smfs_dirty_inode, /* BKL not held. */
330         .put_inode          = smfs_put_inode,   /* BKL not held. */
331         .write_super        = smfs_write_super, /* BKL held */
332         .write_super_lockfs = smfs_write_super_lockfs, /* BKL not held. */
333         .unlockfs           = smfs_unlockfs,    /* BKL not held. */
334         .statfs             = smfs_statfs,      /* BKL held */
335         .remount_fs         = smfs_remount,     /* BKL held */
336 };