Whamcloud - gitweb
fcf9faf5d0aa7710ace92ecdd5eff286c1a01739
[fs/lustre-release.git] / lustre / mds / mds_extN.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  linux/mds/mds_extN.c
5  *
6  *  Lustre Metadata Server (mds) journal abstraction routines
7  *
8  *  Copyright (C) 2002  Cluster File Systems, Inc.
9  *  author: Andreas Dilger <adilger@clusterfs.com>
10  *
11  *  This code is issued under the GNU General Public License.
12  *  See the file COPYING in this distribution
13  *
14  */
15
16 #define DEBUG_SUBSYSTEM S_MDS
17
18 #include <linux/fs.h>
19 #include <linux/jbd.h>
20 #include <linux/extN_fs.h>
21 #include <linux/extN_jbd.h>
22 #include <linux/extN_xattr.h>
23 #include <linux/lustre_mds.h>
24 #include <linux/module.h>
25 #include <linux/obd_lov.h>
26
27 static struct mds_fs_operations mds_extN_fs_ops;
28 static kmem_cache_t *jcb_cache;
29 static int jcb_cache_count;
30
31 struct mds_cb_data {
32         struct journal_callback cb_jcb;
33         struct mds_obd *cb_mds;
34         __u64 cb_last_rcvd;
35 };
36
37 #define EXTN_XATTR_INDEX_LUSTRE         5
38 #define XATTR_LUSTRE_MDS_OBJID          "system.lustre_mds_objid"
39
40 #define XATTR_MDS_MO_MAGIC              0x4711
41
42 /*
43  * We don't currently need any additional blocks for rmdir and
44  * unlink transactions because we are storing the OST oa_id inside
45  * the inode (which we will be changing anyways as part of this
46  * transaction).
47  */
48 static void *mds_extN_start(struct inode *inode, int op)
49 {
50         /* For updates to the last recieved file */
51         int nblocks = EXTN_DATA_TRANS_BLOCKS;
52
53         switch(op) {
54         case MDS_FSOP_RMDIR:
55         case MDS_FSOP_UNLINK:
56                 nblocks += EXTN_DELETE_TRANS_BLOCKS;
57                 break;
58         case MDS_FSOP_RENAME:
59                 /* We may be modifying two directories */
60                 nblocks += EXTN_DATA_TRANS_BLOCKS;
61         case MDS_FSOP_SYMLINK:
62                 /* Possible new block + block bitmap + GDT for long symlink */
63                 nblocks += 3;
64         case MDS_FSOP_CREATE:
65         case MDS_FSOP_MKDIR:
66         case MDS_FSOP_MKNOD:
67                 /* New inode + block bitmap + GDT for new file */
68                 nblocks += 3;
69         case MDS_FSOP_LINK:
70                 /* Change parent directory */
71                 nblocks += EXTN_INDEX_EXTRA_TRANS_BLOCKS+EXTN_DATA_TRANS_BLOCKS;
72                 break;
73         case MDS_FSOP_SETATTR:
74                 /* Setattr on inode */
75                 nblocks += 1;
76                 break;
77         default: CERROR("unknown transaction start op %d\n", op);
78                  LBUG();
79         }
80
81         return journal_start(EXTN_JOURNAL(inode), nblocks);
82 }
83
84 static int mds_extN_commit(struct inode *inode, void *handle)
85 {
86         return journal_stop((handle_t *)handle);
87 }
88
89 static int mds_extN_setattr(struct dentry *dentry, void *handle,
90                             struct iattr *iattr)
91 {
92         struct inode *inode = dentry->d_inode;
93
94         if (inode->i_op->setattr)
95                 return inode->i_op->setattr(dentry, iattr);
96         else
97                 return inode_setattr(inode, iattr);
98 }
99
100 static int mds_extN_set_md(struct inode *inode, void *handle,
101                              struct lov_stripe_md *md)
102 {
103         int rc;
104
105
106
107         lock_kernel();
108         down(&inode->i_sem);
109         if (md == NULL)
110                 rc = extN_xattr_set(handle, inode, EXTN_XATTR_INDEX_LUSTRE,
111                                     XATTR_LUSTRE_MDS_OBJID, NULL, 0, 0);
112         else { 
113                 md->lmd_magic = cpu_to_le32(XATTR_MDS_MO_MAGIC);
114                 rc = extN_xattr_set(handle, inode, EXTN_XATTR_INDEX_LUSTRE,
115                                     XATTR_LUSTRE_MDS_OBJID, md, 
116                                     md->lmd_size, XATTR_CREATE);
117         }
118         up(&inode->i_sem);
119         unlock_kernel();
120
121         if (rc)
122                 CERROR("error adding objectid %Ld to inode %ld\n",
123                        (unsigned long long)md->lmd_object_id, inode->i_ino);
124         return rc;
125 }
126
127 static int mds_extN_get_md(struct inode *inode, struct lov_stripe_md *md)
128 {
129         int rc;
130         int size = md->lmd_size;
131
132         lock_kernel();
133         down(&inode->i_sem);
134         rc = extN_xattr_get(inode, EXTN_XATTR_INDEX_LUSTRE,
135                             XATTR_LUSTRE_MDS_OBJID, md, size);
136
137         up(&inode->i_sem);
138         unlock_kernel();
139
140         if (rc < 0) {
141                 CDEBUG(D_INFO, "error getting EA %s from MDS inode %ld: "
142                        "rc = %d\n", XATTR_LUSTRE_MDS_OBJID, inode->i_ino, rc);
143                 memset(md, 0, size); 
144         } else if (md->lmd_magic != cpu_to_le32(XATTR_MDS_MO_MAGIC)) {
145                 CERROR("MDS striping md for ino %ld has bad magic\n",
146                        inode->i_ino);
147                 rc = -EINVAL;
148         } else {
149                 /* This field is byteswapped because it appears in the
150                  * catalogue.  All others are opaque to the MDS */
151                 md->lmd_object_id = le64_to_cpu(md->lmd_object_id);
152         }
153
154         return rc;
155 }
156
157 static ssize_t mds_extN_readpage(struct file *file, char *buf, size_t count,
158                                  loff_t *offset)
159 {
160         struct inode *inode = file->f_dentry->d_inode;
161         int rc = 0;
162
163         if (S_ISREG(inode->i_mode))
164                 rc = file->f_op->read(file, buf, count, offset);
165         else {
166                 struct buffer_head *bh;
167
168                 /* FIXME: this assumes the blocksize == count, but the calling
169                  *        function will detect this as an error for now */
170                 bh = extN_bread(NULL, inode,
171                                 *offset >> inode->i_sb->s_blocksize_bits,
172                                 0, &rc);
173
174                 if (bh) {
175                         memcpy(buf, bh->b_data, inode->i_blksize);
176                         brelse(bh);
177                         rc = inode->i_blksize;
178                 }
179         }
180
181         return rc;
182 }
183
184 static void mds_extN_delete_inode(struct inode *inode)
185 {
186         if (S_ISREG(inode->i_mode)) {
187                 void *handle = mds_extN_start(inode, MDS_FSOP_UNLINK);
188
189                 if (IS_ERR(handle)) {
190                         CERROR("unable to start transaction");
191                         EXIT;
192                         return;
193                 }
194                 if (mds_extN_set_md(inode, handle, NULL))
195                         CERROR("error clearing obdo on %ld\n", inode->i_ino);
196
197                 if (mds_extN_fs_ops.cl_delete_inode)
198                         mds_extN_fs_ops.cl_delete_inode(inode);
199
200                 if (mds_extN_commit(inode, handle))
201                         CERROR("error closing handle on %ld\n", inode->i_ino);
202         } else
203                 mds_extN_fs_ops.cl_delete_inode(inode);
204 }
205
206 static void mds_extN_callback_status(void *jcb, int error)
207 {
208         struct mds_cb_data *mcb = (struct mds_cb_data *)jcb;
209
210         CDEBUG(D_EXT2, "got callback for last_rcvd %Ld: rc = %d\n",
211                mcb->cb_last_rcvd, error);
212         if (!error && mcb->cb_last_rcvd > mcb->cb_mds->mds_last_committed)
213                 mcb->cb_mds->mds_last_committed = mcb->cb_last_rcvd;
214
215         kmem_cache_free(jcb_cache, jcb);
216         --jcb_cache_count;
217 }
218
219 #ifdef HAVE_JOURNAL_CALLBACK
220 static void mds_extN_callback_func(void *cb_data)
221 {
222         mds_extN_callback_status(cb_data, 0);
223 }
224 #endif
225
226 static int mds_extN_set_last_rcvd(struct mds_obd *mds, void *handle)
227 {
228         struct mds_cb_data *mcb;
229
230         mcb = kmem_cache_alloc(jcb_cache, GFP_NOFS);
231         if (!mcb)
232                 RETURN(-ENOMEM);
233
234         ++jcb_cache_count;
235         mcb->cb_mds = mds;
236         mcb->cb_last_rcvd = mds->mds_last_rcvd;
237
238 #ifdef HAVE_JOURNAL_CALLBACK_STATUS
239         CDEBUG(D_EXT2, "set callback for last_rcvd: %Ld\n",
240                (unsigned long long)mcb->cb_last_rcvd);
241         journal_callback_set(handle, mds_extN_callback_status,
242                              (void *)mcb);
243 #elif defined(HAVE_JOURNAL_CALLBACK)
244         /* XXX original patch version - remove soon */
245 #warning "using old journal callback kernel patch, please update"
246         CDEBUG(D_EXT2, "set callback for last_rcvd: %Ld\n",
247                (unsigned long long)mcb->cb_last_rcvd);
248         journal_callback_set(handle, mds_extN_callback_func, mcb);
249 #else
250 #warning "no journal callback kernel patch, faking it..."
251         {
252         static long next = 0;
253
254         if (time_after(jiffies, next)) {
255                 CERROR("no journal callback kernel patch, faking it...\n");
256                 next = jiffies + 300 * HZ;
257         }
258         }
259         mds_extN_callback_status((struct journal_callback *)mcb, 0);
260 #endif
261
262         return 0;
263 }
264
265 static int mds_extN_journal_data(struct file *filp)
266 {
267         struct inode *inode = filp->f_dentry->d_inode;
268
269         EXTN_I(inode)->i_flags |= EXTN_JOURNAL_DATA_FL;
270
271         return 0;
272 }
273
274 /*
275  * We need to hack the return value for the free inode counts because
276  * the current EA code requires one filesystem block per inode with EAs,
277  * so it is possible to run out of blocks before we run out of inodes.
278  *
279  * This can be removed when the extN EA code is fixed.
280  */
281 static int mds_extN_statfs(struct super_block *sb, struct statfs *sfs)
282 {
283         int rc = vfs_statfs(sb, sfs);
284
285         if (!rc && sfs->f_bfree < sfs->f_ffree)
286                 sfs->f_ffree = sfs->f_bfree;
287
288         return rc;
289 }
290
291 static struct mds_fs_operations mds_extN_fs_ops = {
292         fs_owner:               THIS_MODULE,
293         fs_start:               mds_extN_start,
294         fs_commit:              mds_extN_commit,
295         fs_setattr:             mds_extN_setattr,
296         fs_set_md:              mds_extN_set_md,
297         fs_get_md:              mds_extN_get_md,
298         fs_readpage:            mds_extN_readpage,
299         fs_delete_inode:        mds_extN_delete_inode,
300         cl_delete_inode:        clear_inode,
301         fs_journal_data:        mds_extN_journal_data,
302         fs_set_last_rcvd:       mds_extN_set_last_rcvd,
303         fs_statfs:              mds_extN_statfs,
304 };
305
306 static int __init mds_extN_init(void)
307 {
308         int rc;
309
310         //rc = extN_xattr_register();
311         jcb_cache = kmem_cache_create("mds_extN_jcb",
312                                       sizeof(struct mds_cb_data), 0,
313                                       0, NULL, NULL);
314         if (!jcb_cache) {
315                 CERROR("error allocating MDS journal callback cache\n");
316                 GOTO(out, rc = -ENOMEM);
317         }
318         rc = mds_register_fs_type(&mds_extN_fs_ops, "extN");
319
320         if (rc)
321                 kmem_cache_destroy(jcb_cache);
322 out:
323         return rc;
324 }
325
326 static void __exit mds_extN_exit(void)
327 {
328         int rc;
329
330         mds_unregister_fs_type("extN");
331         rc = kmem_cache_destroy(jcb_cache);
332
333         if (rc || jcb_cache_count) {
334                 CERROR("can't free MDS callback cache: count %d, rc = %d\n",
335                        jcb_cache_count, rc);
336         }
337
338         //rc = extN_xattr_unregister();
339 }
340
341 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
342 MODULE_DESCRIPTION("Lustre MDS extN Filesystem Helper v0.1");
343 MODULE_LICENSE("GPL");
344
345 module_init(mds_extN_init);
346 module_exit(mds_extN_exit);