1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * Lustre Metadata Server (mds) journal abstraction routines
8 * Copyright (C) 2002 Cluster File Systems, Inc.
9 * author: Andreas Dilger <adilger@clusterfs.com>
11 * This code is issued under the GNU General Public License.
12 * See the file COPYING in this distribution
16 #define DEBUG_SUBSYSTEM S_MDS
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>
27 static struct mds_fs_operations mds_extN_fs_ops;
28 static kmem_cache_t *jcb_cache;
29 static int jcb_cache_count;
32 struct journal_callback cb_jcb;
33 struct mds_obd *cb_mds;
37 #define EXTN_XATTR_INDEX_LUSTRE 5
38 #define XATTR_LUSTRE_MDS_OBJID "system.lustre_mds_objid"
40 #define XATTR_MDS_MO_MAGIC 0x4711
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
48 static void *mds_extN_start(struct inode *inode, int op)
50 /* For updates to the last recieved file */
51 int nblocks = EXTN_DATA_TRANS_BLOCKS;
56 nblocks += EXTN_DELETE_TRANS_BLOCKS;
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 */
67 /* New inode + block bitmap + GDT for new file */
70 /* Change parent directory */
71 nblocks += EXTN_INDEX_EXTRA_TRANS_BLOCKS+EXTN_DATA_TRANS_BLOCKS;
73 case MDS_FSOP_SETATTR:
74 /* Setattr on inode */
77 default: CERROR("unknown transaction start op %d\n", op);
81 return journal_start(EXTN_JOURNAL(inode), nblocks);
84 static int mds_extN_commit(struct inode *inode, void *handle)
86 return journal_stop((handle_t *)handle);
89 static int mds_extN_setattr(struct dentry *dentry, void *handle,
92 struct inode *inode = dentry->d_inode;
94 if (inode->i_op->setattr)
95 return inode->i_op->setattr(dentry, iattr);
97 return inode_setattr(inode, iattr);
100 static int mds_extN_set_md(struct inode *inode, void *handle,
101 struct lov_stripe_md *md)
110 rc = extN_xattr_set(handle, inode, EXTN_XATTR_INDEX_LUSTRE,
111 XATTR_LUSTRE_MDS_OBJID, NULL, 0, 0);
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);
122 CERROR("error adding objectid %Ld to inode %ld\n",
123 (unsigned long long)md->lmd_object_id, inode->i_ino);
127 static int mds_extN_get_md(struct inode *inode, struct lov_stripe_md *md)
130 int size = md->lmd_size;
134 rc = extN_xattr_get(inode, EXTN_XATTR_INDEX_LUSTRE,
135 XATTR_LUSTRE_MDS_OBJID, md, size);
141 CDEBUG(D_INFO, "error getting EA %s from MDS inode %ld: "
142 "rc = %d\n", XATTR_LUSTRE_MDS_OBJID, inode->i_ino, rc);
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",
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);
157 static ssize_t mds_extN_readpage(struct file *file, char *buf, size_t count,
160 struct inode *inode = file->f_dentry->d_inode;
163 if (S_ISREG(inode->i_mode))
164 rc = file->f_op->read(file, buf, count, offset);
166 struct buffer_head *bh;
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,
175 memcpy(buf, bh->b_data, inode->i_blksize);
177 rc = inode->i_blksize;
184 static void mds_extN_delete_inode(struct inode *inode)
186 if (S_ISREG(inode->i_mode)) {
187 void *handle = mds_extN_start(inode, MDS_FSOP_UNLINK);
189 if (IS_ERR(handle)) {
190 CERROR("unable to start transaction");
194 if (mds_extN_set_md(inode, handle, NULL))
195 CERROR("error clearing obdo on %ld\n", inode->i_ino);
197 if (mds_extN_fs_ops.cl_delete_inode)
198 mds_extN_fs_ops.cl_delete_inode(inode);
200 if (mds_extN_commit(inode, handle))
201 CERROR("error closing handle on %ld\n", inode->i_ino);
203 mds_extN_fs_ops.cl_delete_inode(inode);
206 static void mds_extN_callback_status(void *jcb, int error)
208 struct mds_cb_data *mcb = (struct mds_cb_data *)jcb;
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;
215 kmem_cache_free(jcb_cache, jcb);
219 #ifdef HAVE_JOURNAL_CALLBACK
220 static void mds_extN_callback_func(void *cb_data)
222 mds_extN_callback_status(cb_data, 0);
226 static int mds_extN_set_last_rcvd(struct mds_obd *mds, void *handle)
228 struct mds_cb_data *mcb;
230 mcb = kmem_cache_alloc(jcb_cache, GFP_NOFS);
236 mcb->cb_last_rcvd = mds->mds_last_rcvd;
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,
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);
250 #warning "no journal callback kernel patch, faking it..."
252 static long next = 0;
254 if (time_after(jiffies, next)) {
255 CERROR("no journal callback kernel patch, faking it...\n");
256 next = jiffies + 300 * HZ;
259 mds_extN_callback_status((struct journal_callback *)mcb, 0);
265 static int mds_extN_journal_data(struct file *filp)
267 struct inode *inode = filp->f_dentry->d_inode;
269 EXTN_I(inode)->i_flags |= EXTN_JOURNAL_DATA_FL;
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.
279 * This can be removed when the extN EA code is fixed.
281 static int mds_extN_statfs(struct super_block *sb, struct statfs *sfs)
283 int rc = vfs_statfs(sb, sfs);
285 if (!rc && sfs->f_bfree < sfs->f_ffree)
286 sfs->f_ffree = sfs->f_bfree;
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,
306 static int __init mds_extN_init(void)
310 //rc = extN_xattr_register();
311 jcb_cache = kmem_cache_create("mds_extN_jcb",
312 sizeof(struct mds_cb_data), 0,
315 CERROR("error allocating MDS journal callback cache\n");
316 GOTO(out, rc = -ENOMEM);
318 rc = mds_register_fs_type(&mds_extN_fs_ops, "extN");
321 kmem_cache_destroy(jcb_cache);
326 static void __exit mds_extN_exit(void)
330 mds_unregister_fs_type("extN");
331 rc = kmem_cache_destroy(jcb_cache);
333 if (rc || jcb_cache_count) {
334 CERROR("can't free MDS callback cache: count %d, rc = %d\n",
335 jcb_cache_count, rc);
338 //rc = extN_xattr_unregister();
341 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
342 MODULE_DESCRIPTION("Lustre MDS extN Filesystem Helper v0.1");
343 MODULE_LICENSE("GPL");
345 module_init(mds_extN_init);
346 module_exit(mds_extN_exit);