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> &
14 * Andreas Dilger <braam@clusterfs.com>
19 #define DEBUG_SUBSYSTEM S_MDS
21 #include <linux/module.h>
22 #include <linux/lustre_mds.h>
23 #include <linux/lustre_dlm.h>
24 #include <linux/init.h>
26 extern int mds_get_lovtgts(struct obd_device *obd, int tgt_count,
28 extern int mds_get_lovdesc(struct obd_device *obd, struct lov_desc *desc);
29 extern int mds_update_last_rcvd(struct mds_obd *mds, void *handle,
30 struct ptlrpc_request *req);
31 static int mds_cleanup(struct obd_device * obddev);
33 inline struct mds_obd *mds_req2mds(struct ptlrpc_request *req)
35 return &req->rq_export->exp_obd->u.mds;
39 /* Assumes caller has already pushed into the kernel filesystem context */
40 static int mds_sendpage(struct ptlrpc_request *req, struct file *file,
44 struct mds_obd *mds = mds_req2mds(req);
45 struct ptlrpc_bulk_desc *desc;
46 struct ptlrpc_bulk_page *bulk;
50 desc = ptlrpc_prep_bulk(req->rq_connection);
52 GOTO(out, rc = -ENOMEM);
54 bulk = ptlrpc_prep_bulk_page(desc);
56 GOTO(cleanup_bulk, rc = -ENOMEM);
58 OBD_ALLOC(buf, PAGE_SIZE);
60 GOTO(cleanup_bulk, rc = -ENOMEM);
62 rc = mds_fs_readpage(mds, file, buf, PAGE_SIZE, (loff_t *)&offset);
65 GOTO(cleanup_buf, rc = -EIO);
67 bulk->b_xid = req->rq_xid;
69 bulk->b_buflen = PAGE_SIZE;
70 desc->b_portal = MDS_BULK_PORTAL;
72 rc = ptlrpc_send_bulk(desc);
74 GOTO(cleanup_buf, rc);
76 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE)) {
77 CERROR("obd_fail_loc=%x, fail operation rc=%d\n",
78 OBD_FAIL_MDS_SENDPAGE, rc);
79 ptlrpc_abort_bulk(desc);
80 GOTO(cleanup_buf, rc);
83 wait_event(desc->b_waitq, ptlrpc_check_bulk_sent(desc));
84 if (desc->b_flags & PTL_RPC_FL_INTR)
85 GOTO(cleanup_buf, rc = -EINTR);
89 OBD_FREE(buf, PAGE_SIZE);
91 ptlrpc_free_bulk(desc);
96 /* 'dir' is a inode for which a lock has already been taken */
97 struct dentry *mds_name2locked_dentry(struct obd_device *obd, struct dentry *dir,
98 struct vfsmount **mnt, char *name,
99 int namelen, int lock_mode,
100 struct lustre_handle *lockh,
103 struct dentry *dchild;
105 __u64 res_id[3] = {0};
108 down(&dir->d_inode->i_sem);
109 dchild = lookup_one_len(name, dir, namelen);
110 if (IS_ERR(dchild)) {
111 CERROR("child lookup error %ld\n", PTR_ERR(dchild));
112 up(&dir->d_inode->i_sem);
116 if (dir_lock_mode != LCK_EX && dir_lock_mode != LCK_PW) {
117 up(&dir->d_inode->i_sem);
118 ldlm_lock_decref(lockh, dir_lock_mode);
121 if (lock_mode == 0 || !dchild->d_inode)
124 res_id[0] = dchild->d_inode->i_ino;
125 rc = ldlm_match_or_enqueue(NULL, NULL, obd->obd_namespace, NULL,
126 res_id, LDLM_PLAIN, NULL, 0, lock_mode,
127 &flags, ldlm_completion_ast, (void *)mds_lock_callback, NULL,
129 if (rc != ELDLM_OK) {
131 up(&dir->d_inode->i_sem);
132 RETURN(ERR_PTR(-ENOLCK)); /* XXX translate ldlm code */
138 struct dentry *mds_fid2locked_dentry(struct obd_device *obd, struct ll_fid *fid,
139 struct vfsmount **mnt, int lock_mode,
140 struct lustre_handle *lockh)
142 struct mds_obd *mds = &obd->u.mds;
143 struct dentry *de = mds_fid2dentry(mds, fid, mnt), *retval = de;
145 __u64 res_id[3] = {0};
151 res_id[0] = de->d_inode->i_ino;
152 rc = ldlm_match_or_enqueue(NULL, NULL, obd->obd_namespace, NULL,
153 res_id, LDLM_PLAIN, NULL, 0, lock_mode,
154 &flags, ldlm_completion_ast, (void *)mds_lock_callback, NULL,
156 if (rc != ELDLM_OK) {
158 retval = ERR_PTR(-ENOLCK); /* XXX translate ldlm code */
164 struct dentry *mds_fid2dentry(struct mds_obd *mds, struct ll_fid *fid,
165 struct vfsmount **mnt)
167 /* stolen from NFS */
168 struct super_block *sb = mds->mds_sb;
169 unsigned long ino = fid->id;
170 __u32 generation = fid->generation;
172 struct list_head *lp;
173 struct dentry *result;
176 RETURN(ERR_PTR(-ESTALE));
178 inode = iget(sb, ino);
180 RETURN(ERR_PTR(-ENOMEM));
182 CDEBUG(D_DENTRY, "--> mds_fid2dentry: sb %p\n", inode->i_sb);
184 if (is_bad_inode(inode) ||
185 (generation && inode->i_generation != generation)) {
186 /* we didn't find the right inode.. */
187 CERROR("bad inode %lu, link: %d ct: %d or version %u/%u\n",
188 inode->i_ino, inode->i_nlink,
189 atomic_read(&inode->i_count), inode->i_generation,
193 RETURN(ERR_PTR(-ESTALE));
196 /* now to find a dentry. If possible, get a well-connected one */
198 *mnt = mds->mds_vfsmnt;
199 spin_lock(&dcache_lock);
200 for (lp = inode->i_dentry.next; lp != &inode->i_dentry ; lp=lp->next) {
201 result = list_entry(lp,struct dentry, d_alias);
202 if (! (result->d_flags & DCACHE_NFSD_DISCONNECTED)) {
204 result->d_vfs_flags |= DCACHE_REFERENCED;
205 spin_unlock(&dcache_lock);
212 spin_unlock(&dcache_lock);
213 result = d_alloc_root(inode);
214 if (result == NULL) {
216 return ERR_PTR(-ENOMEM);
220 result->d_flags |= DCACHE_NFSD_DISCONNECTED;
224 static int mds_connect(struct lustre_handle *conn, struct obd_device *obd)
229 rc = class_connect(conn, obd);
237 static int mds_disconnect(struct lustre_handle *conn)
241 rc = class_disconnect(conn);
248 /* FIXME: the error cases need fixing to avoid leaks */
249 static int mds_getstatus(struct ptlrpc_request *req)
251 struct mds_obd *mds = mds_req2mds(req);
252 struct mds_body *body;
253 struct mds_client_info *mci;
254 struct mds_client_data *mcd;
255 int rc, size = sizeof(*body);
258 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
259 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_GETSTATUS_PACK)) {
260 CERROR("mds: out of memory for message: size=%d\n", size);
261 req->rq_status = -ENOMEM;
265 body = lustre_msg_buf(req->rq_reqmsg, 0);
266 mds_unpack_body(body);
268 /* Anything we need to do here with the client's trans no or so? */
269 body = lustre_msg_buf(req->rq_repmsg, 0);
270 memcpy(&body->fid1, &mds->mds_rootfid, sizeof(body->fid1));
272 mci = mds_uuid_to_mci(mds, ptlrpc_req_to_uuid(req));
274 /* We don't have any old connection data for this client */
277 CDEBUG(D_INFO, "allocating new client data for UUID '%s'",
278 ptlrpc_req_to_uuid(req));
280 OBD_ALLOC(mcd, sizeof(*mcd));
282 CERROR("mds: out of memory for client data\n");
283 req->rq_status = -ENOMEM;
286 memcpy(mcd->mcd_uuid, ptlrpc_req_to_uuid(req),
287 sizeof(mcd->mcd_uuid));
288 rc = mds_client_add(mds, mcd, -1);
294 /* We have old connection data for this client... */
296 CDEBUG(D_INFO, "found existing data for UUID '%s' at #%d\n",
297 mcd->mcd_uuid, mci->mci_off);
299 /* mcd_last_xid is is stored in little endian on the disk and
300 mds_pack_rep_body converts it to network order */
301 body->last_xid = le32_to_cpu(mcd->mcd_last_xid);
302 mds_pack_rep_body(req);
306 static int mds_getlovinfo(struct ptlrpc_request *req)
308 struct mds_obd *mds = mds_req2mds(req);
309 struct mds_status_req *streq;
310 struct lov_desc *desc;
312 int rc, size[2] = {sizeof(*desc)};
315 streq = lustre_msg_buf(req->rq_reqmsg, 0);
316 streq->flags = NTOH__u32(streq->flags);
317 streq->repbuf = NTOH__u32(streq->repbuf);
318 size[1] = streq->repbuf;
320 rc = lustre_pack_msg(2, size, NULL, &req->rq_replen, &req->rq_repmsg);
322 CERROR("mds: out of memory for message: size=%d\n", size[1]);
323 req->rq_status = -ENOMEM;
327 desc = lustre_msg_buf(req->rq_repmsg, 0);
328 rc = mds_get_lovdesc(req->rq_obd, desc);
330 CERROR("mds_get_lovdesc error %d", rc);
335 tgt_count = NTOH__u32(desc->ld_tgt_count);
336 if (tgt_count * sizeof(uuid_t) > streq->repbuf) {
337 CERROR("too many targets, enlarge client buffers\n");
338 req->rq_status = -ENOSPC;
342 mds->mds_max_mdsize = sizeof(desc) + tgt_count * sizeof(uuid_t);
343 rc = mds_get_lovtgts(req->rq_obd, tgt_count,
344 lustre_msg_buf(req->rq_repmsg, 1));
346 CERROR("get_lovtgts error %d\n", rc);
353 int mds_lock_callback(struct lustre_handle *lockh, struct ldlm_lock_desc *desc,
354 void *data, int data_len, struct ptlrpc_request **reqp)
359 /* Completion AST. Do nothing */
363 if (ldlm_cli_cancel(lockh) < 0)
368 static int mds_getattr_name(int offset, struct ptlrpc_request *req)
370 struct mds_obd *mds = mds_req2mds(req);
371 struct obd_device *obd = req->rq_export->exp_obd;
372 struct obd_run_ctxt saved;
373 struct mds_body *body;
374 struct dentry *de = NULL, *dchild = NULL;
376 struct lustre_handle lockh;
378 int namelen, flags, lock_mode, rc = 0;
379 __u64 res_id[3] = {0, 0, 0};
382 if (strcmp(req->rq_export->exp_obd->obd_type->typ_name, "mds") != 0)
385 if (req->rq_reqmsg->bufcount <= offset + 1) {
387 GOTO(out_pre_de, rc = -EINVAL);
390 body = lustre_msg_buf(req->rq_reqmsg, offset);
391 name = lustre_msg_buf(req->rq_reqmsg, offset + 1);
392 namelen = req->rq_reqmsg->buflens[offset + 1];
393 /* requests were at offset 2, replies go back at 1 */
397 push_ctxt(&saved, &mds->mds_ctxt);
398 de = mds_fid2dentry(mds, &body->fid1, NULL);
401 GOTO(out_pre_de, rc = -ESTALE);
405 CDEBUG(D_INODE, "parent ino %ld\n", dir->i_ino);
407 lock_mode = (req->rq_reqmsg->opc == MDS_REINT) ? LCK_CW : LCK_PW;
408 res_id[0] = dir->i_ino;
410 rc = ldlm_lock_match(obd->obd_namespace, res_id, LDLM_PLAIN,
411 NULL, 0, lock_mode, &lockh);
413 LDLM_DEBUG_NOLOCK("enqueue res %Lu", res_id[0]);
414 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace, NULL,
415 res_id, LDLM_PLAIN, NULL, 0, lock_mode,
416 &flags, ldlm_completion_ast, (void *)mds_lock_callback,
418 if (rc != ELDLM_OK) {
419 CERROR("lock enqueue: err: %d\n", rc);
420 GOTO(out_create_de, rc = -EIO);
423 ldlm_lock_dump((void *)(unsigned long)lockh.addr);
426 dchild = lookup_one_len(name, de, namelen - 1);
427 if (IS_ERR(dchild)) {
428 CDEBUG(D_INODE, "child lookup error %ld\n", PTR_ERR(dchild));
431 GOTO(out_create_dchild, rc = -ESTALE);
434 if (dchild->d_inode) {
435 struct mds_body *body;
436 struct inode *inode = dchild->d_inode;
437 CDEBUG(D_INODE, "child exists (dir %ld, name %s, ino %ld)\n",
438 dir->i_ino, name, dchild->d_inode->i_ino);
440 body = lustre_msg_buf(req->rq_repmsg, offset);
441 mds_pack_inode2fid(&body->fid1, inode);
442 mds_pack_inode2body(body, inode);
443 if (S_ISREG(inode->i_mode)) {
444 struct lov_stripe_md *md;
445 md = lustre_msg_buf(req->rq_repmsg, offset + 1);
446 md->lmd_easize = mds->mds_max_mdsize;
447 mds_fs_get_md(mds, inode, md);
449 /* now a normal case for intent locking */
458 ldlm_lock_decref(&lockh, lock_mode);
468 static int mds_getattr(int offset, struct ptlrpc_request *req)
470 struct mds_obd *mds = mds_req2mds(req);
471 struct obd_run_ctxt saved;
474 struct mds_body *body;
475 int rc, size[2] = {sizeof(*body)}, bufcount = 1;
478 body = lustre_msg_buf(req->rq_reqmsg, offset);
479 push_ctxt(&saved, &mds->mds_ctxt);
480 de = mds_fid2dentry(mds, &body->fid1, NULL);
482 GOTO(out_pop, rc = -ENOENT);
486 if (S_ISREG(body->fid1.f_type)) {
488 size[1] = mds->mds_max_mdsize;
489 } else if (body->valid & OBD_MD_LINKNAME) {
491 size[1] = inode->i_size;
494 rc = lustre_pack_msg(bufcount, size, NULL, &req->rq_replen,
496 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK)) {
497 CERROR("mds: out of memory\n");
501 if (body->valid & OBD_MD_LINKNAME) {
502 char *tmp = lustre_msg_buf(req->rq_repmsg, 1);
504 rc = inode->i_op->readlink(de, tmp, size[1]);
507 CERROR("readlink failed: %d\n", rc);
512 body = lustre_msg_buf(req->rq_repmsg, 0);
513 body->ino = inode->i_ino;
514 body->generation = inode->i_generation;
515 body->atime = inode->i_atime;
516 body->ctime = inode->i_ctime;
517 body->mtime = inode->i_mtime;
518 body->uid = inode->i_uid;
519 body->gid = inode->i_gid;
520 body->size = inode->i_size;
521 body->mode = inode->i_mode;
522 body->nlink = inode->i_nlink;
523 body->valid = (OBD_MD_FLID | OBD_MD_FLATIME | OBD_MD_FLCTIME |
524 OBD_MD_FLMTIME | OBD_MD_FLSIZE | OBD_MD_FLUID |
525 OBD_MD_FLGID | OBD_MD_FLNLINK | OBD_MD_FLGENER |
528 if (S_ISREG(inode->i_mode)) {
529 rc = mds_fs_get_md(mds, inode,
530 lustre_msg_buf(req->rq_repmsg, 1));
532 CERROR("mds_fs_get_md failed: %d\n", rc);
535 body->valid |= OBD_MD_FLEASIZE;
536 } else if (S_ISLNK(inode->i_mode))
537 body->valid |= OBD_MD_LINKNAME;
547 static int mds_statfs(struct ptlrpc_request *req)
549 struct mds_obd *mds = mds_req2mds(req);
550 struct obd_statfs *osfs;
552 int rc, size = sizeof(*osfs);
555 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen,
557 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_STATFS_PACK)) {
558 CERROR("mds: statfs lustre_pack_msg failed: rc = %d\n", rc);
562 rc = vfs_statfs(mds->mds_sb, &sfs);
564 CERROR("mds: statfs failed: rc %d\n", rc);
567 osfs = lustre_msg_buf(req->rq_repmsg, 0);
568 memset(osfs, 0, size);
569 obd_statfs_pack(osfs, &sfs);
576 static int mds_open(struct ptlrpc_request *req)
579 struct mds_body *body;
581 struct vfsmount *mnt;
582 struct mds_obd *mds = mds_req2mds(req);
583 struct mds_client_info *mci;
585 struct list_head *tmp;
586 struct mds_file_data *mfd;
587 int rc, size = sizeof(*body);
590 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
591 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_OPEN_PACK)) {
592 CERROR("mds: out of memory\n");
593 req->rq_status = -ENOMEM;
597 mci = mds_uuid_to_mci(mds, ptlrpc_req_to_uuid(req));
599 CERROR("mds: no mci!\n");
600 req->rq_status = -ENOTCONN;
604 body = lustre_msg_buf(req->rq_reqmsg, 0);
606 /* was this animal open already? */
607 /* XXX we should only check on re-open, or do a refcount... */
608 list_for_each(tmp, &mci->mci_open_head) {
609 struct mds_file_data *fd;
610 fd = list_entry(tmp, struct mds_file_data, mfd_list);
611 if (body->extra == fd->mfd_clientfd &&
612 body->fid1.id == fd->mfd_file->f_dentry->d_inode->i_ino) {
613 CERROR("Re opening %Ld\n", body->fid1.id);
618 OBD_ALLOC(mfd, sizeof(*mfd));
620 CERROR("mds: out of memory\n");
621 req->rq_status = -ENOMEM;
625 de = mds_fid2dentry(mds, &body->fid1, &mnt);
627 req->rq_status = -ENOENT;
631 /* check if this inode has seen a delayed object creation */
632 if (req->rq_reqmsg->bufcount > 1) {
634 struct lov_stripe_md *md;
635 struct inode *inode = de->d_inode;
638 md = lustre_msg_buf(req->rq_reqmsg, 1);
640 handle = mds_fs_start(mds, de->d_inode, MDS_FSOP_SETATTR);
642 req->rq_status = -ENOMEM;
646 /* XXX error handling */
647 rc = mds_fs_set_md(mds, inode, handle, md);
649 struct obd_run_ctxt saved;
650 push_ctxt(&saved, &mds->mds_ctxt);
651 rc = mds_update_last_rcvd(mds, handle, req);
657 /* FIXME: need to return last_rcvd, last_committed */
659 /* FIXME: keep rc intact */
660 rc = mds_fs_commit(mds, de->d_inode, handle);
668 file = dentry_open(de, mnt, flags & ~O_DIRECT);
669 if (!file || IS_ERR(file)) {
670 req->rq_status = -EINVAL;
671 OBD_FREE(mfd, sizeof(*mfd));
675 file->private_data = mfd;
676 mfd->mfd_file = file;
677 mfd->mfd_clientfd = body->extra;
678 list_add(&mfd->mfd_list, &mci->mci_open_head);
680 body = lustre_msg_buf(req->rq_repmsg, 0);
681 body->extra = (__u64) (unsigned long)file;
685 static int mds_close(struct ptlrpc_request *req)
688 struct mds_body *body;
690 struct mds_obd *mds = mds_req2mds(req);
691 struct vfsmount *mnt;
692 struct mds_file_data *mfd;
696 rc = lustre_pack_msg(0, NULL, NULL, &req->rq_replen, &req->rq_repmsg);
697 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_CLOSE_PACK)) {
698 CERROR("mds: out of memory\n");
699 req->rq_status = -ENOMEM;
703 body = lustre_msg_buf(req->rq_reqmsg, 0);
704 de = mds_fid2dentry(mds, &body->fid1, &mnt);
706 req->rq_status = -ENOENT;
710 file = (struct file *)(unsigned long)body->extra;
713 mfd = (struct mds_file_data *)file->private_data;
714 list_del(&mfd->mfd_list);
715 OBD_FREE(mfd, sizeof(*mfd));
717 req->rq_status = filp_close(file, 0);
724 static int mds_readpage(struct ptlrpc_request *req)
726 struct mds_obd *mds = mds_req2mds(req);
727 struct vfsmount *mnt;
730 struct mds_body *body;
731 struct obd_run_ctxt saved;
732 int rc, size = sizeof(*body);
735 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
736 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK)) {
737 CERROR("mds: out of memory\n");
738 GOTO(out, rc = -ENOMEM);
741 body = lustre_msg_buf(req->rq_reqmsg, 0);
742 push_ctxt(&saved, &mds->mds_ctxt);
743 de = mds_fid2dentry(mds, &body->fid1, &mnt);
745 GOTO(out_pop, rc = PTR_ERR(de));
747 CDEBUG(D_INODE, "ino %ld\n", de->d_inode->i_ino);
749 file = dentry_open(de, mnt, O_RDONLY | O_LARGEFILE);
750 /* note: in case of an error, dentry_open puts dentry */
752 GOTO(out_pop, rc = PTR_ERR(file));
754 /* to make this asynchronous make sure that the handling function
755 doesn't send a reply when this function completes. Instead a
756 callback function would send the reply */
757 rc = mds_sendpage(req, file, body->size);
767 int mds_reint(int offset, struct ptlrpc_request *req)
770 struct mds_update_record rec;
772 rc = mds_update_unpack(req, offset, &rec);
773 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNPACK)) {
774 CERROR("invalid record\n");
775 req->rq_status = -EINVAL;
778 /* rc will be used to interrupt a for loop over multiple records */
779 rc = mds_reint_rec(&rec, offset, req);
783 int mds_handle(struct ptlrpc_request *req)
788 rc = lustre_unpack_msg(req->rq_reqmsg, req->rq_reqlen);
789 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_HANDLE_UNPACK)) {
790 CERROR("lustre_mds: Invalid request\n");
794 if (req->rq_reqmsg->type != PTL_RPC_MSG_REQUEST) {
795 CERROR("lustre_mds: wrong packet type sent %d\n",
796 req->rq_reqmsg->type);
797 GOTO(out, rc = -EINVAL);
800 if (req->rq_reqmsg->opc != MDS_CONNECT && req->rq_export == NULL)
801 GOTO(out, rc = -ENOTCONN);
803 if (strcmp(req->rq_obd->obd_type->typ_name, "mds") != 0)
804 GOTO(out, rc = -EINVAL);
806 switch (req->rq_reqmsg->opc) {
808 CDEBUG(D_INODE, "connect\n");
809 OBD_FAIL_RETURN(OBD_FAIL_MDS_CONNECT_NET, 0);
810 rc = target_handle_connect(req);
814 CDEBUG(D_INODE, "disconnect\n");
815 OBD_FAIL_RETURN(OBD_FAIL_MDS_DISCONNECT_NET, 0);
816 rc = target_handle_disconnect(req);
820 CDEBUG(D_INODE, "getstatus\n");
821 OBD_FAIL_RETURN(OBD_FAIL_MDS_GETSTATUS_NET, 0);
822 rc = mds_getstatus(req);
826 CDEBUG(D_INODE, "getlovinfo\n");
827 rc = mds_getlovinfo(req);
831 CDEBUG(D_INODE, "getattr\n");
832 OBD_FAIL_RETURN(OBD_FAIL_MDS_GETATTR_NET, 0);
833 rc = mds_getattr(0, req);
837 CDEBUG(D_INODE, "statfs\n");
838 OBD_FAIL_RETURN(OBD_FAIL_MDS_STATFS_NET, 0);
839 rc = mds_statfs(req);
843 CDEBUG(D_INODE, "readpage\n");
844 OBD_FAIL_RETURN(OBD_FAIL_MDS_READPAGE_NET, 0);
845 rc = mds_readpage(req);
847 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
852 int size = sizeof(struct mds_body);
853 CDEBUG(D_INODE, "reint\n");
854 OBD_FAIL_RETURN(OBD_FAIL_MDS_REINT_NET, 0);
856 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen,
859 rc = req->rq_status = -ENOMEM;
862 rc = mds_reint(0, req);
863 OBD_FAIL_RETURN(OBD_FAIL_MDS_REINT_NET_REP, 0);
868 CDEBUG(D_INODE, "open\n");
869 OBD_FAIL_RETURN(OBD_FAIL_MDS_OPEN_NET, 0);
874 CDEBUG(D_INODE, "close\n");
875 OBD_FAIL_RETURN(OBD_FAIL_MDS_CLOSE_NET, 0);
880 CDEBUG(D_INODE, "enqueue\n");
881 OBD_FAIL_RETURN(OBD_FAIL_LDLM_ENQUEUE, 0);
882 rc = ldlm_handle_enqueue(req);
888 CDEBUG(D_INODE, "convert\n");
889 OBD_FAIL_RETURN(OBD_FAIL_LDLM_CONVERT, 0);
890 rc = ldlm_handle_convert(req);
896 CDEBUG(D_INODE, "cancel\n");
897 OBD_FAIL_RETURN(OBD_FAIL_LDLM_CANCEL, 0);
898 rc = ldlm_handle_cancel(req);
903 CDEBUG(D_INODE, "callback\n");
904 CERROR("callbacks should not happen on MDS\n");
906 OBD_FAIL_RETURN(OBD_FAIL_LDLM_CALLBACK, 0);
911 rc = ptlrpc_error(req->rq_svc, req);
918 struct mds_obd *mds = mds_req2mds(req);
919 req->rq_repmsg->last_rcvd = HTON__u64(mds->mds_last_rcvd);
920 req->rq_repmsg->last_committed =
921 HTON__u64(mds->mds_last_committed);
922 CDEBUG(D_INFO, "last_rcvd %Lu, last_committed %Lu, xid %d\n",
923 (unsigned long long)mds->mds_last_rcvd,
924 (unsigned long long)mds->mds_last_committed,
925 cpu_to_le32(req->rq_xid));
928 /* Still not 100% sure whether we should reply with the server
929 * last_rcvd or that of this client. I'm not sure it even makes
930 * a difference on a per-client basis, because last_rcvd is global
931 * and we are not supposed to allow transactions while in recovery.
934 CERROR("mds: processing error %d\n", rc);
935 ptlrpc_error(req->rq_svc, req);
937 CDEBUG(D_NET, "sending reply\n");
938 ptlrpc_reply(req->rq_svc, req);
943 /* Update the server data on disk. This stores the new mount_count and
944 * also the last_rcvd value to disk. If we don't have a clean shutdown,
945 * then the server last_rcvd value may be less than that of the clients.
946 * This will alert us that we may need to do client recovery.
949 int mds_update_server_data(struct mds_obd *mds)
951 struct obd_run_ctxt saved;
952 struct mds_server_data *msd = mds->mds_server_data;
953 struct file *filp = mds->mds_rcvd_filp;
957 msd->msd_last_rcvd = cpu_to_le64(mds->mds_last_rcvd);
958 msd->msd_mount_count = cpu_to_le64(mds->mds_mount_count);
960 CDEBUG(D_SUPER, "MDS mount_count is %Lu, last_rcvd is %Lu\n",
961 (unsigned long long)mds->mds_mount_count,
962 (unsigned long long)mds->mds_last_rcvd);
963 push_ctxt(&saved, &mds->mds_ctxt);
964 rc = lustre_fwrite(filp, (char *)msd, sizeof(*msd), &off);
965 if (rc != sizeof(*msd)) {
966 CERROR("error writing MDS server data: rc = %d\n", rc);
971 rc = fsync_dev(filp->f_dentry->d_inode->i_rdev);
974 CERROR("error flushing MDS server data: rc = %d\n", rc);
979 /* Do recovery actions for the MDS */
980 static int mds_recover(struct obd_device *obddev)
982 struct mds_obd *mds = &obddev->u.mds;
985 /* This happens at the end when recovery is complete */
986 ++mds->mds_mount_count;
987 rc = mds_update_server_data(mds);
992 #define MDS_NUM_THREADS 8
993 /* mount the file system (secretly) */
994 static int mds_setup(struct obd_device *obddev, obd_count len, void *buf)
997 struct obd_ioctl_data* data = buf;
998 struct mds_obd *mds = &obddev->u.mds;
999 struct vfsmount *mnt;
1004 #ifdef CONFIG_DEV_RDONLY
1005 dev_clear_rdonly(2);
1007 if (!data->ioc_inlbuf1 || !data->ioc_inlbuf2)
1008 GOTO(err_dec, rc = -EINVAL);
1010 mds->mds_fstype = strdup(data->ioc_inlbuf2);
1012 mnt = do_kern_mount(mds->mds_fstype, 0, data->ioc_inlbuf1, NULL);
1015 CERROR("do_kern_mount failed: rc = %d\n", rc);
1016 GOTO(err_kfree, rc);
1019 mds->mds_sb = mnt->mnt_root->d_inode->i_sb;
1021 GOTO(err_put, rc = -ENODEV);
1023 mds->mds_max_mdsize = sizeof(struct lov_stripe_md);
1024 rc = mds_fs_setup(mds, mnt);
1026 CERROR("MDS filesystem method init failed: rc = %d\n", rc);
1030 mds->mds_service = ptlrpc_init_svc(64 * 1024, MDS_REQUEST_PORTAL,
1031 MDC_REPLY_PORTAL, "self",mds_handle);
1032 if (!mds->mds_service) {
1033 CERROR("failed to start service\n");
1034 GOTO(err_fs, rc = -EINVAL);
1038 obddev->obd_namespace =
1039 ldlm_namespace_new("mds_server", LDLM_NAMESPACE_SERVER);
1040 if (obddev->obd_namespace == NULL) {
1042 mds_cleanup(obddev);
1046 for (i = 0; i < MDS_NUM_THREADS; i++) {
1048 sprintf(name, "lustre_MDS_%2d", i);
1049 rc = ptlrpc_start_thread(obddev, mds->mds_service, name);
1051 CERROR("cannot start MDS thread #%d: rc %d\n", i, rc);
1053 GOTO(err_thread, rc);
1057 rc = mds_recover(obddev);
1059 GOTO(err_thread, rc);
1066 ptlrpc_stop_all_threads(mds->mds_service);
1068 ptlrpc_unregister_service(mds->mds_service);
1070 mds_fs_cleanup(mds);
1073 mntput(mds->mds_vfsmnt);
1077 kfree(mds->mds_fstype);
1083 static int mds_cleanup(struct obd_device * obddev)
1085 struct super_block *sb;
1086 struct mds_obd *mds = &obddev->u.mds;
1090 if ( !list_empty(&obddev->obd_exports) ) {
1091 CERROR("still has exports!\n");
1095 ptlrpc_stop_all_threads(mds->mds_service);
1096 ptlrpc_unregister_service(mds->mds_service);
1102 mds_update_server_data(mds);
1104 if (mds->mds_rcvd_filp) {
1105 int rc = filp_close(mds->mds_rcvd_filp, 0);
1106 mds->mds_rcvd_filp = NULL;
1109 CERROR("last_rcvd file won't close, rc=%d\n", rc);
1113 mntput(mds->mds_vfsmnt);
1115 kfree(mds->mds_fstype);
1117 ldlm_namespace_free(obddev->obd_namespace);
1120 #ifdef CONFIG_DEV_RDONLY
1121 dev_clear_rdonly(2);
1123 mds_fs_cleanup(mds);
1129 static int ldlm_intent_policy(struct ldlm_lock *lock, void *req_cookie,
1130 ldlm_mode_t mode, void *data)
1132 struct ptlrpc_request *req = req_cookie;
1139 if (req->rq_reqmsg->bufcount > 1) {
1140 /* an intent needs to be considered */
1141 struct ldlm_intent *it = lustre_msg_buf(req->rq_reqmsg, 1);
1142 struct mds_obd *mds= &req->rq_export->exp_obd->u.mds;
1143 struct mds_body *mds_rep;
1144 struct ldlm_reply *rep;
1145 __u64 new_resid[3] = {0, 0, 0}, old_res;
1146 int bufcount = -1, rc, size[3] = {sizeof(struct ldlm_reply),
1147 sizeof(struct mds_body),
1148 mds->mds_max_mdsize};
1150 it->opc = NTOH__u64(it->opc);
1152 LDLM_DEBUG(lock, "intent policy, opc: %s", ldlm_it2str(it->opc));
1155 switch((long)it->opc) {
1157 /* Note that in the negative case you may be returning
1158 * a file and its obdo */
1160 case IT_CREAT|IT_OPEN:
1181 rc = lustre_pack_msg(bufcount, size, NULL, &req->rq_replen,
1184 rc = req->rq_status = -ENOMEM;
1188 rep = lustre_msg_buf(req->rq_repmsg, 0);
1189 rep->lock_policy_res1 = 1;
1191 /* execute policy */
1192 switch ((long)it->opc) {
1194 case IT_CREAT|IT_OPEN:
1202 rc = mds_reint(2, req);
1203 if (rc || req->rq_status != 0) {
1204 rep->lock_policy_res2 = req->rq_status;
1205 RETURN(ELDLM_LOCK_ABORTED);
1215 rc = mds_getattr_name(2, req);
1216 /* FIXME: we need to sit down and decide on who should
1217 * set req->rq_status, who should return negative and
1218 * positive return values, and what they all mean. */
1219 if (rc || req->rq_status != 0) {
1220 rep->lock_policy_res2 = req->rq_status;
1221 RETURN(ELDLM_LOCK_ABORTED);
1224 case IT_READDIR|IT_OPEN:
1228 CERROR("Unhandled intent\n");
1232 if (it->opc == IT_UNLINK || it->opc == IT_RMDIR ||
1233 it->opc == IT_RENAME || it->opc == IT_RENAME2)
1234 RETURN(ELDLM_LOCK_ABORTED);
1236 rep->lock_policy_res2 = req->rq_status;
1237 mds_rep = lustre_msg_buf(req->rq_repmsg, 1);
1238 new_resid[0] = NTOH__u32(mds_rep->ino);
1239 if (new_resid[0] == 0)
1241 old_res = lock->l_resource->lr_name[0];
1243 CDEBUG(D_INFO, "remote intent: locking %d instead of"
1244 "%ld\n", mds_rep->ino, (long)old_res);
1246 ldlm_lock_change_resource(lock, new_resid);
1247 if (lock->l_resource == NULL) {
1251 LDLM_DEBUG(lock, "intent policy, old res %ld",
1253 RETURN(ELDLM_LOCK_CHANGED);
1255 int size = sizeof(struct ldlm_reply);
1256 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen,
1259 CERROR("out of memory\n");
1268 extern int mds_iocontrol(long cmd, struct lustre_handle *conn,
1269 int len, void *karg, void *uarg);
1271 /* use obd ops to offer management infrastructure */
1272 static struct obd_ops mds_obd_ops = {
1273 o_connect: mds_connect,
1274 o_disconnect: mds_disconnect,
1276 o_cleanup: mds_cleanup,
1277 o_iocontrol: mds_iocontrol
1280 static int __init mds_init(void)
1282 class_register_type(&mds_obd_ops, LUSTRE_MDS_NAME);
1283 ldlm_register_intent(ldlm_intent_policy);
1287 static void __exit mds_exit(void)
1289 ldlm_unregister_intent();
1290 class_unregister_type(LUSTRE_MDS_NAME);
1293 MODULE_AUTHOR("Cluster File Systems <info@clusterfs.com>");
1294 MODULE_DESCRIPTION("Lustre Metadata Server (MDS) v0.01");
1295 MODULE_LICENSE("GPL");
1297 module_init(mds_init);
1298 module_exit(mds_exit);