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