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