Whamcloud - gitweb
landing smfs.
[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  *  Copyright (C) 2004 Cluster File Systems, Inc.
5  *
6  *   This file is part of Lustre, http://www.lustre.org.
7  *
8  *   Lustre is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Lustre is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with Lustre; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  *
21  */
22
23 #define DEBUG_SUBSYSTEM S_SM
24
25 #include <linux/kmod.h>
26 #include <linux/init.h>
27 #include <linux/fs.h>
28 #include <linux/slab.h>
29 #include <linux/string.h>
30 #include <linux/obd_class.h>
31 #include <linux/obd_support.h>
32 #include <linux/lustre_lib.h>
33 #include <linux/lustre_idl.h>
34 #include <linux/lustre_smfs.h>
35 #include "smfs_internal.h"
36
37 static void smfs_read_inode(struct inode *inode)
38 {
39         struct super_block *cache_sb;
40         struct inode *cache_inode;
41         ENTRY;
42
43         if (!inode)
44                 return;
45
46         CDEBUG(D_INODE, "read_inode ino %lu\n", inode->i_ino);
47         cache_sb = S2CSB(inode->i_sb);
48
49         cache_inode = iget(cache_sb, inode->i_ino);
50
51         SMFS_SET_INODE_REC(inode);
52         SMFS_SET_INODE_CACHE_HOOK(inode);
53         I2CI(inode) = cache_inode;
54
55         pre_smfs_inode(inode, cache_inode);
56         if (cache_sb && cache_sb->s_op->read_inode)
57                 cache_sb->s_op->read_inode(cache_inode);
58
59         post_smfs_inode(inode, cache_inode);
60         sm_set_inode_ops(cache_inode, inode);
61
62         CDEBUG(D_INODE, "read_inode ino %lu icount %d \n",
63                inode->i_ino, atomic_read(&inode->i_count));
64         EXIT;
65 }
66
67 /* Although some filesystem(such as ext3) do not have
68  * clear_inode method, but we need it to free the
69  * cache inode
70  */
71 static void smfs_clear_inode(struct inode *inode)
72 {
73         struct super_block *cache_sb;
74         struct inode *cache_inode;
75         ENTRY;
76
77         if (!inode)
78                 return;
79
80         cache_sb = S2CSB(inode->i_sb);
81         cache_inode = I2CI(inode);
82
83         /*FIXME: because i_count of cache_inode may not
84          * be 0 or 1 in before smfs_delete inode, So we
85          * need to dec it to 1 before we call delete_inode
86          * of the bellow cache filesystem Check again latter*/
87
88         if (atomic_read(&cache_inode->i_count) < 1)
89                 BUG();
90
91         while (atomic_read(&cache_inode->i_count) != 1)
92                 atomic_dec(&cache_inode->i_count);
93
94         iput(cache_inode);
95
96         SMFS_CLEAN_INODE_REC(inode);
97         I2CI(inode) = NULL;
98         EXIT;
99 }
100
101 static void smfs_delete_inode(struct inode *inode)
102 {
103         struct inode *cache_inode;
104         struct super_block *cache_sb;
105         ENTRY;
106
107         cache_inode = I2CI(inode);
108         cache_sb = S2CSB(inode->i_sb);
109
110         if (!cache_inode || !cache_sb)
111                 return;
112
113         /* FIXME-WANGDI: because i_count of cache_inode may not be 0 or 1 in
114          * before smfs_delete inode, So we need to dec it to 1 before we call
115          * delete_inode of the bellow cache filesystem Check again latter. */
116
117         if (atomic_read(&cache_inode->i_count) < 1)
118                 BUG();
119
120         while (atomic_read(&cache_inode->i_count) != 1)
121                 atomic_dec(&cache_inode->i_count);
122
123         pre_smfs_inode(inode, cache_inode);
124
125 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
126         list_del(&cache_inode->i_hash);
127         INIT_LIST_HEAD(&cache_inode->i_hash);
128 #else
129         hlist_del_init(&cache_inode->i_hash);
130 #endif
131         
132         list_del(&cache_inode->i_list);
133         INIT_LIST_HEAD(&cache_inode->i_list);
134
135         if (cache_inode->i_data.nrpages)
136                 truncate_inode_pages(&cache_inode->i_data, 0);
137
138         if (cache_sb->s_op->delete_inode)
139                 cache_sb->s_op->delete_inode(cache_inode);
140
141         post_smfs_inode(inode, cache_inode);
142
143         I2CI(inode) = NULL;
144         EXIT;
145 }
146
147 static void smfs_write_inode(struct inode *inode, int wait)
148 {
149         struct inode *cache_inode;
150         struct super_block *cache_sb;
151         ENTRY;
152
153         cache_inode = I2CI(inode);
154         cache_sb = S2CSB(inode->i_sb);
155
156         if (!cache_inode || !cache_sb)
157                 return;
158
159         pre_smfs_inode(inode, cache_inode);
160
161         if (cache_sb->s_op->write_inode)
162                 cache_sb->s_op->write_inode(cache_inode, wait);
163
164         post_smfs_inode(inode, cache_inode);
165         EXIT;
166 }
167
168 static void smfs_dirty_inode(struct inode *inode)
169 {
170         struct inode *cache_inode;
171         struct super_block *cache_sb;
172         ENTRY;
173
174         cache_inode = I2CI(inode);
175         cache_sb = S2CSB(inode->i_sb);
176
177         if (!cache_inode || !cache_sb)
178                 return;
179
180         pre_smfs_inode(inode, cache_inode);
181         if (cache_sb->s_op->dirty_inode)
182                 cache_sb->s_op->dirty_inode(cache_inode);
183
184         post_smfs_inode(inode, cache_inode);
185         EXIT;
186 }
187
188 static void smfs_put_inode(struct inode *inode)
189 {
190         struct inode *cache_inode;
191         struct super_block *cache_sb;
192         ENTRY;
193
194         cache_inode = I2CI(inode);
195         cache_sb = S2CSB(inode->i_sb);
196
197         if (!cache_inode || !cache_sb)
198                 return;
199         if (cache_sb->s_op->put_inode)
200                 cache_sb->s_op->put_inode(cache_inode);
201         EXIT;
202 }
203
204 static void smfs_write_super(struct super_block *sb)
205 {
206         struct super_block *cache_sb;
207         ENTRY;
208
209         cache_sb = S2CSB(sb);
210         if (!cache_sb)
211                 return;
212
213         if (cache_sb->s_op->write_super)
214                 cache_sb->s_op->write_super(cache_sb);
215
216         duplicate_sb(sb, cache_sb);
217         EXIT;
218 }
219
220 static void smfs_write_super_lockfs(struct super_block *sb)
221 {
222         struct super_block *cache_sb;
223         ENTRY;
224
225         cache_sb = S2CSB(sb);
226         if (!cache_sb)
227                 return;
228
229         if (cache_sb->s_op->write_super_lockfs)
230                 cache_sb->s_op->write_super_lockfs(cache_sb);
231
232         duplicate_sb(sb, cache_sb);
233         EXIT;
234 }
235
236 static void smfs_unlockfs(struct super_block *sb)
237 {
238         struct super_block *cache_sb;
239         ENTRY;
240
241         cache_sb = S2CSB(sb);
242         if (!cache_sb)
243                 return;
244
245         if (cache_sb->s_op->unlockfs)
246                 cache_sb->s_op->unlockfs(cache_sb);
247
248         duplicate_sb(sb, cache_sb);
249         EXIT;
250 }
251
252 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
253 static int smfs_statfs(struct super_block *sb, struct statfs *buf)
254 #else
255 static int smfs_statfs(struct super_block *sb, struct kstatfs *buf)
256 #endif
257 {
258         struct super_block *cache_sb;
259         int rc = 0;
260         ENTRY;
261
262         cache_sb = S2CSB(sb);
263         if (!cache_sb)
264                 RETURN(-EINVAL);
265
266         if (cache_sb->s_op->statfs)
267                 rc = cache_sb->s_op->statfs(cache_sb, buf);
268
269         duplicate_sb(sb, cache_sb);
270
271         RETURN(rc);
272 }
273
274 static int smfs_remount(struct super_block *sb, int *flags, char *data)
275 {
276         struct super_block *cache_sb;
277         int rc = 0;
278         ENTRY;
279
280         cache_sb = S2CSB(sb);
281
282         if (!cache_sb)
283                 RETURN(-EINVAL);
284
285         if (cache_sb->s_op->remount_fs)
286                 rc = cache_sb->s_op->remount_fs(cache_sb, flags, data);
287
288         duplicate_sb(sb, cache_sb);
289         RETURN(rc);
290 }
291
292 struct super_operations smfs_super_ops = {
293         read_inode:     smfs_read_inode,
294         clear_inode:    smfs_clear_inode,
295         put_super:      smfs_put_super,
296         delete_inode:   smfs_delete_inode,
297         write_inode:    smfs_write_inode,
298         dirty_inode:    smfs_dirty_inode,       /* BKL not held.  We take it */
299         put_inode:      smfs_put_inode,         /* BKL not held.  Don't need */
300
301         write_super:    smfs_write_super,       /* BKL held */
302         write_super_lockfs: smfs_write_super_lockfs, /* BKL not held. Take it */
303         unlockfs:       smfs_unlockfs,          /* BKL not held.  We take it */
304         statfs:         smfs_statfs,            /* BKL held */
305         remount_fs:     smfs_remount,           /* BKL held */
306 };