1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * Lustre Metadata Server (mds) request handler
8 * Copyright (C) 2001, 2002 Cluster File Systems, Inc.
10 * This code is issued under the GNU General Public License.
11 * See the file COPYING in this distribution
13 * by Peter Braam <braam@clusterfs.com>
15 * This server is single threaded at present (but can easily be multi threaded)
21 #include <linux/version.h>
22 #include <linux/module.h>
24 #include <linux/stat.h>
25 #include <linux/locks.h>
26 #include <linux/quotaops.h>
27 #include <asm/unistd.h>
28 #include <asm/uaccess.h>
30 #define DEBUG_SUBSYSTEM S_MDS
32 #include <linux/lustre_mds.h>
33 #include <linux/lustre_lib.h>
34 #include <linux/lustre_net.h>
36 int mds_sendpage(struct ptlrpc_request *req, struct file *file,
37 __u64 offset, struct niobuf *dst)
40 mm_segment_t oldfs = get_fs();
41 struct ptlrpc_bulk_desc *bulk;
44 bulk = ptlrpc_prep_bulk(req->rq_connection);
50 bulk->b_xid = req->rq_reqmsg->xid;
52 OBD_ALLOC(buf, PAGE_SIZE);
55 GOTO(cleanup_bulk, rc);
59 rc = mds_fs_readpage(&req->rq_obd->u.mds, file, buf, PAGE_SIZE,
63 if (rc != PAGE_SIZE) {
65 GOTO(cleanup_buf, rc);
69 bulk->b_buflen = PAGE_SIZE;
71 rc = ptlrpc_send_bulk(bulk, MDS_BULK_PORTAL);
72 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE)) {
73 CERROR("obd_fail_loc=%x, fail operation rc=%d\n",
74 OBD_FAIL_MDS_SENDPAGE, rc);
75 PtlMDUnlink(bulk->b_md_h);
76 GOTO(cleanup_buf, rc);
78 wait_event_interruptible(bulk->b_waitq,
79 ptlrpc_check_bulk_sent(bulk));
81 if (bulk->b_flags == PTL_RPC_INTR) {
83 GOTO(cleanup_buf, rc);
88 OBD_FREE(buf, PAGE_SIZE);
90 OBD_FREE(bulk, sizeof(*bulk));
95 struct dentry *mds_fid2dentry(struct mds_obd *mds, struct ll_fid *fid,
96 struct vfsmount **mnt)
99 struct super_block *sb = mds->mds_sb;
100 unsigned long ino = fid->id;
101 __u32 generation = fid->generation;
103 struct list_head *lp;
104 struct dentry *result;
107 return ERR_PTR(-ESTALE);
109 inode = iget(sb, ino);
111 return ERR_PTR(-ENOMEM);
113 CDEBUG(D_DENTRY, "--> mds_fid2dentry: sb %p\n", inode->i_sb);
115 if (is_bad_inode(inode) ||
116 (generation && inode->i_generation != generation)) {
117 /* we didn't find the right inode.. */
118 CERROR("bad inode %lu, link: %d ct: %d or version %u/%u\n",
120 inode->i_nlink, atomic_read(&inode->i_count),
125 return ERR_PTR(-ESTALE);
128 /* now to find a dentry.
129 * If possible, get a well-connected one
132 *mnt = mds->mds_vfsmnt;
133 spin_lock(&dcache_lock);
134 for (lp = inode->i_dentry.next; lp != &inode->i_dentry ; lp=lp->next) {
135 result = list_entry(lp,struct dentry, d_alias);
136 if (! (result->d_flags & DCACHE_NFSD_DISCONNECTED)) {
138 result->d_vfs_flags |= DCACHE_REFERENCED;
139 spin_unlock(&dcache_lock);
146 spin_unlock(&dcache_lock);
147 result = d_alloc_root(inode);
148 if (result == NULL) {
150 return ERR_PTR(-ENOMEM);
154 result->d_flags |= DCACHE_NFSD_DISCONNECTED;
158 int mds_getattr(struct ptlrpc_request *req)
162 struct mds_body *body;
163 struct mds_obd *mds = &req->rq_obd->u.mds;
164 int rc, size = sizeof(*body);
167 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
168 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK)) {
169 CERROR("mds: out of memory\n");
170 req->rq_status = -ENOMEM;
174 body = lustre_msg_buf(req->rq_reqmsg, 0);
175 de = mds_fid2dentry(mds, &body->fid1, NULL);
177 req->rq_status = -ENOENT;
181 body = lustre_msg_buf(req->rq_repmsg, 0);
183 body->ino = inode->i_ino;
184 body->generation = inode->i_generation;
185 body->atime = inode->i_atime;
186 body->ctime = inode->i_ctime;
187 body->mtime = inode->i_mtime;
188 body->uid = inode->i_uid;
189 body->gid = inode->i_gid;
190 body->size = inode->i_size;
191 body->mode = inode->i_mode;
192 body->nlink = inode->i_nlink;
194 mds_fs_get_objid(mds, inode, &body->objid);
199 int mds_open(struct ptlrpc_request *req)
202 struct mds_body *body;
204 struct vfsmount *mnt;
206 int rc, size = sizeof(*body);
209 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
210 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_OPEN_PACK)) {
211 CERROR("mds: out of memory\n");
212 req->rq_status = -ENOMEM;
216 body = lustre_msg_buf(req->rq_reqmsg, 0);
217 de = mds_fid2dentry(&req->rq_obd->u.mds, &body->fid1, &mnt);
219 req->rq_status = -ENOENT;
223 file = dentry_open(de, mnt, flags);
224 if (!file || IS_ERR(file)) {
225 req->rq_status = -EINVAL;
229 body = lustre_msg_buf(req->rq_repmsg, 0);
230 body->objid = (__u64) (unsigned long)file;
234 int mds_close(struct ptlrpc_request *req)
237 struct mds_body *body;
239 struct vfsmount *mnt;
243 rc = lustre_pack_msg(0, NULL, NULL, &req->rq_replen, &req->rq_repmsg);
244 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_CLOSE_PACK)) {
245 CERROR("mds: out of memory\n");
246 req->rq_status = -ENOMEM;
250 body = lustre_msg_buf(req->rq_reqmsg, 0);
251 de = mds_fid2dentry(&req->rq_obd->u.mds, &body->fid1, &mnt);
253 req->rq_status = -ENOENT;
257 file = (struct file *)(unsigned long)body->objid;
258 req->rq_status = filp_close(file, 0);
265 int mds_readpage(struct ptlrpc_request *req)
267 struct vfsmount *mnt;
270 struct niobuf *niobuf;
271 struct mds_body *body;
272 int rc, size = sizeof(*body);
275 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
276 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK)) {
277 CERROR("mds: out of memory\n");
278 req->rq_status = -ENOMEM;
282 body = lustre_msg_buf(req->rq_reqmsg, 0);
283 de = mds_fid2dentry(&req->rq_obd->u.mds, &body->fid1, &mnt);
285 req->rq_status = PTR_ERR(de);
289 CDEBUG(D_INODE, "ino %ld\n", de->d_inode->i_ino);
291 file = dentry_open(de, mnt, O_RDONLY | O_LARGEFILE);
292 /* note: in case of an error, dentry_open puts dentry */
294 req->rq_status = PTR_ERR(file);
298 niobuf = lustre_msg_buf(req->rq_reqmsg, 1);
300 req->rq_status = -EINVAL;
305 /* to make this asynchronous make sure that the handling function
306 doesn't send a reply when this function completes. Instead a
307 callback function would send the reply */
308 rc = mds_sendpage(req, file, body->size, niobuf);
315 int mds_reint(struct ptlrpc_request *req)
318 struct mds_update_record rec;
320 rc = mds_update_unpack(req, &rec);
321 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNPACK)) {
322 CERROR("invalid record\n");
323 req->rq_status = -EINVAL;
326 /* rc will be used to interrupt a for loop over multiple records */
327 rc = mds_reint_rec(&rec, req);
331 int mds_handle(struct obd_device *dev, struct ptlrpc_service *svc,
332 struct ptlrpc_request *req)
337 rc = lustre_unpack_msg(req->rq_reqmsg, req->rq_reqlen);
338 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_HANDLE_UNPACK)) {
339 CERROR("lustre_mds: Invalid request\n");
343 if (req->rq_reqmsg->type != PTL_RPC_REQUEST) {
344 CERROR("lustre_mds: wrong packet type sent %d\n",
345 req->rq_reqmsg->type);
346 GOTO(out, rc = -EINVAL);
349 switch (req->rq_reqmsg->opc) {
351 CDEBUG(D_INODE, "getattr\n");
352 OBD_FAIL_RETURN(OBD_FAIL_MDS_GETATTR_NET, 0);
353 rc = mds_getattr(req);
357 CDEBUG(D_INODE, "readpage\n");
358 OBD_FAIL_RETURN(OBD_FAIL_MDS_READPAGE_NET, 0);
359 rc = mds_readpage(req);
361 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
366 CDEBUG(D_INODE, "reint\n");
367 OBD_FAIL_RETURN(OBD_FAIL_MDS_REINT_NET, 0);
372 CDEBUG(D_INODE, "open\n");
373 OBD_FAIL_RETURN(OBD_FAIL_MDS_OPEN_NET, 0);
378 CDEBUG(D_INODE, "close\n");
379 OBD_FAIL_RETURN(OBD_FAIL_MDS_CLOSE_NET, 0);
384 rc = ptlrpc_error(svc, req);
391 ptlrpc_error(svc, req);
393 CDEBUG(D_NET, "sending reply\n");
394 ptlrpc_reply(svc, req);
400 static int mds_prep(struct obd_device *obddev)
402 struct obd_run_ctxt saved;
403 struct mds_obd *mds = &obddev->u.mds;
404 struct super_operations *s_ops;
410 mds->mds_service = ptlrpc_init_svc(128 * 1024,
411 MDS_REQUEST_PORTAL, MDC_REPLY_PORTAL,
414 if (!mds->mds_service) {
415 CERROR("failed to start service\n");
419 rc = ptlrpc_start_thread(obddev, mds->mds_service, "lustre_mds");
421 CERROR("cannot start thread\n");
425 push_ctxt(&saved, &mds->mds_ctxt);
426 rc = simple_mkdir(current->fs->pwd, "ROOT", 0700);
427 if (rc && rc != -EEXIST) {
428 CERROR("cannot create ROOT directory\n");
431 rc = simple_mkdir(current->fs->pwd, "FH", 0700);
432 if (rc && rc != -EEXIST) {
433 CERROR("cannot create FH directory\n");
437 f = filp_open("mount_count", O_RDWR | O_CREAT, 0644);
439 CERROR("cannot open/create mount_count file, rc = %ld\n",
441 GOTO(err_svc, rc = PTR_ERR(f));
443 rc = lustre_fread(f, (char *)&mount_count, sizeof(mount_count), &off);
445 CERROR("empty MDS mount_count, new MDS?\n");
446 /* XXX maybe this should just be a random number? */
447 mds->mds_mount_count = 0;
448 } else if (rc != sizeof(mount_count)) {
449 CERROR("error reading mount_count: rc = %d\n", rc);
450 /* XXX maybe this should just be a random number? */
451 mds->mds_mount_count = 0;
453 mds->mds_mount_count = le64_to_cpu(mount_count);
456 mds->mds_mount_count++;
457 CDEBUG(D_SUPER, "MDS mount_count is %Ld\n", mds->mds_mount_count);
459 mount_count = cpu_to_le64(mds->mds_mount_count);
460 rc = lustre_fwrite(f, (char *)&mount_count, sizeof(mount_count), &off);
461 if (rc != sizeof(mount_count))
462 CERROR("error writing mount_count: rc = %d\n", rc);
463 rc = filp_close(f, 0);
465 CERROR("error closing mount_count: rc = %d\n", rc);
467 f = filp_open("last_rcvd", O_RDWR | O_CREAT, 0644);
469 CERROR("cannot open/create last_rcvd file\n");
470 GOTO(err_svc, rc = PTR_ERR(f));
472 mds->mds_last_rcvd = f;
476 * Replace the client filesystem delete_inode method with our own,
477 * so that we can clear the object ID before the inode is deleted.
478 * The fs_delete_inode method will call cl_delete_inode for us.
480 * We need to do this for the MDS superblock only, hence we install
481 * a modified copy of the original superblock method table.
483 * We still assume that there is only a single MDS client filesystem
484 * type, as we don't have access to the mds struct in * delete_inode.
486 OBD_ALLOC(s_ops, sizeof(*s_ops));
487 memcpy(s_ops, mds->mds_sb->s_op, sizeof(*s_ops));
488 mds->mds_fsops->cl_delete_inode = s_ops->delete_inode;
489 s_ops->delete_inode = mds->mds_fsops->fs_delete_inode;
490 mds->mds_sb->s_op = s_ops;
495 rpc_unregister_service(mds->mds_service);
496 OBD_FREE(mds->mds_service, sizeof(*mds->mds_service));
501 /* mount the file system (secretly) */
502 static int mds_setup(struct obd_device *obddev, obd_count len, void *buf)
504 struct obd_ioctl_data* data = buf;
505 struct mds_obd *mds = &obddev->u.mds;
506 struct vfsmount *mnt;
510 #ifdef CONFIG_DEV_RDONLY
513 if (!data->ioc_inlbuf1 || !data->ioc_inlbuf2)
516 mds->mds_fstype = strdup(data->ioc_inlbuf2);
518 if (!strcmp(mds->mds_fstype, "ext3"))
519 mds->mds_fsops = &mds_ext3_fs_ops;
520 else if (!strcmp(mds->mds_fstype, "ext2"))
521 mds->mds_fsops = &mds_ext2_fs_ops;
523 CERROR("unsupported MDS filesystem type %s\n", mds->mds_fstype);
524 GOTO(err_kfree, (err = -EPERM));
528 mnt = do_kern_mount(mds->mds_fstype, 0, data->ioc_inlbuf1, NULL);
530 CERROR("do_kern_mount failed: %d\n", err);
531 GOTO(err_dec, err = PTR_ERR(mnt));
534 mds->mds_sb = mnt->mnt_root->d_inode->i_sb;
536 GOTO(err_put, (err = -ENODEV));
538 mds->mds_vfsmnt = mnt;
539 mds->mds_ctxt.pwdmnt = mnt;
540 mds->mds_ctxt.pwd = mnt->mnt_root;
541 mds->mds_ctxt.fs = KERNEL_DS;
543 err = mds_prep(obddev);
551 mntput(mds->mds_vfsmnt);
557 kfree(mds->mds_fstype);
561 static int mds_cleanup(struct obd_device * obddev)
563 struct super_operations *s_ops = NULL;
564 struct super_block *sb;
565 struct mds_obd *mds = &obddev->u.mds;
569 if ( !list_empty(&obddev->obd_gen_clients) ) {
570 CERROR("still has clients!\n");
574 ptlrpc_stop_thread(mds->mds_service);
575 rpc_unregister_service(mds->mds_service);
576 if (!list_empty(&mds->mds_service->srv_reqs)) {
577 // XXX reply with errors and clean up
578 CERROR("Request list not empty!\n");
580 OBD_FREE(mds->mds_service, sizeof(*mds->mds_service));
586 if (mds->mds_last_rcvd) {
587 int rc = filp_close(mds->mds_last_rcvd, 0);
588 mds->mds_last_rcvd = NULL;
591 CERROR("last_rcvd file won't close, rc=%d\n", rc);
596 mntput(mds->mds_vfsmnt);
598 kfree(mds->mds_fstype);
600 #ifdef CONFIG_DEV_RDONLY
603 OBD_FREE(s_ops, sizeof(*s_ops));
609 /* use obd ops to offer management infrastructure */
610 static struct obd_ops mds_obd_ops = {
612 o_cleanup: mds_cleanup,
615 static int __init mds_init(void)
617 obd_register_type(&mds_obd_ops, LUSTRE_MDS_NAME);
621 static void __exit mds_exit(void)
623 obd_unregister_type(LUSTRE_MDS_NAME);
626 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
627 MODULE_DESCRIPTION("Lustre Metadata Server (MDS) v0.01");
628 MODULE_LICENSE("GPL");
630 module_init(mds_init);
631 module_exit(mds_exit);