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 = ~0; /* FIXME: should be more selective */
525 if (S_ISREG(inode->i_mode)) {
526 rc = mds_fs_get_md(mds, inode,
527 lustre_msg_buf(req->rq_repmsg, 1));
529 CERROR("mds_fs_get_md failed: %d\n", rc);
541 static int mds_statfs(struct ptlrpc_request *req)
543 struct mds_obd *mds = mds_req2mds(req);
544 struct obd_statfs *osfs;
546 int rc, size = sizeof(*osfs);
549 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen,
551 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_STATFS_PACK)) {
552 CERROR("mds: statfs lustre_pack_msg failed: rc = %d\n", rc);
556 rc = vfs_statfs(mds->mds_sb, &sfs);
558 CERROR("mds: statfs failed: rc %d\n", rc);
561 osfs = lustre_msg_buf(req->rq_repmsg, 0);
562 memset(osfs, 0, size);
563 obd_statfs_pack(osfs, &sfs);
570 static int mds_open(struct ptlrpc_request *req)
573 struct mds_body *body;
575 struct vfsmount *mnt;
576 struct mds_obd *mds = mds_req2mds(req);
577 struct mds_client_info *mci;
579 struct list_head *tmp;
580 struct mds_file_data *mfd;
581 int rc, size = sizeof(*body);
584 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
585 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_OPEN_PACK)) {
586 CERROR("mds: out of memory\n");
587 req->rq_status = -ENOMEM;
591 mci = mds_uuid_to_mci(mds, ptlrpc_req_to_uuid(req));
593 CERROR("mds: no mci!\n");
594 req->rq_status = -ENOTCONN;
598 body = lustre_msg_buf(req->rq_reqmsg, 0);
600 /* was this animal open already? */
601 /* XXX we should only check on re-open, or do a refcount... */
602 list_for_each(tmp, &mci->mci_open_head) {
603 struct mds_file_data *fd;
604 fd = list_entry(tmp, struct mds_file_data, mfd_list);
605 if (body->extra == fd->mfd_clientfd &&
606 body->fid1.id == fd->mfd_file->f_dentry->d_inode->i_ino) {
607 CERROR("Re opening %Ld\n", body->fid1.id);
612 OBD_ALLOC(mfd, sizeof(*mfd));
614 CERROR("mds: out of memory\n");
615 req->rq_status = -ENOMEM;
619 de = mds_fid2dentry(mds, &body->fid1, &mnt);
621 req->rq_status = -ENOENT;
625 /* check if this inode has seen a delayed object creation */
626 if (req->rq_reqmsg->bufcount > 1) {
628 struct lov_stripe_md *md;
629 struct inode *inode = de->d_inode;
632 md = lustre_msg_buf(req->rq_reqmsg, 1);
634 handle = mds_fs_start(mds, de->d_inode, MDS_FSOP_SETATTR);
636 req->rq_status = -ENOMEM;
640 /* XXX error handling */
641 rc = mds_fs_set_md(mds, inode, handle, md);
643 struct obd_run_ctxt saved;
644 push_ctxt(&saved, &mds->mds_ctxt);
645 rc = mds_update_last_rcvd(mds, handle, req);
651 /* FIXME: need to return last_rcvd, last_committed */
653 /* FIXME: keep rc intact */
654 rc = mds_fs_commit(mds, de->d_inode, handle);
662 file = dentry_open(de, mnt, flags & ~O_DIRECT);
663 if (!file || IS_ERR(file)) {
664 req->rq_status = -EINVAL;
665 OBD_FREE(mfd, sizeof(*mfd));
669 file->private_data = mfd;
670 mfd->mfd_file = file;
671 mfd->mfd_clientfd = body->extra;
672 list_add(&mfd->mfd_list, &mci->mci_open_head);
674 body = lustre_msg_buf(req->rq_repmsg, 0);
675 body->extra = (__u64) (unsigned long)file;
679 static int mds_close(struct ptlrpc_request *req)
682 struct mds_body *body;
684 struct mds_obd *mds = mds_req2mds(req);
685 struct vfsmount *mnt;
686 struct mds_file_data *mfd;
690 rc = lustre_pack_msg(0, NULL, NULL, &req->rq_replen, &req->rq_repmsg);
691 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_CLOSE_PACK)) {
692 CERROR("mds: out of memory\n");
693 req->rq_status = -ENOMEM;
697 body = lustre_msg_buf(req->rq_reqmsg, 0);
698 de = mds_fid2dentry(mds, &body->fid1, &mnt);
700 req->rq_status = -ENOENT;
704 file = (struct file *)(unsigned long)body->extra;
707 mfd = (struct mds_file_data *)file->private_data;
708 list_del(&mfd->mfd_list);
709 OBD_FREE(mfd, sizeof(*mfd));
711 req->rq_status = filp_close(file, 0);
718 static int mds_readpage(struct ptlrpc_request *req)
720 struct mds_obd *mds = mds_req2mds(req);
721 struct vfsmount *mnt;
724 struct mds_body *body;
725 struct obd_run_ctxt saved;
726 int rc, size = sizeof(*body);
729 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
730 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK)) {
731 CERROR("mds: out of memory\n");
732 GOTO(out, rc = -ENOMEM);
735 body = lustre_msg_buf(req->rq_reqmsg, 0);
736 push_ctxt(&saved, &mds->mds_ctxt);
737 de = mds_fid2dentry(mds, &body->fid1, &mnt);
739 GOTO(out_pop, rc = PTR_ERR(de));
741 CDEBUG(D_INODE, "ino %ld\n", de->d_inode->i_ino);
743 file = dentry_open(de, mnt, O_RDONLY | O_LARGEFILE);
744 /* note: in case of an error, dentry_open puts dentry */
746 GOTO(out_pop, rc = PTR_ERR(file));
748 /* to make this asynchronous make sure that the handling function
749 doesn't send a reply when this function completes. Instead a
750 callback function would send the reply */
751 rc = mds_sendpage(req, file, body->size);
761 int mds_reint(int offset, struct ptlrpc_request *req)
764 struct mds_update_record rec;
766 rc = mds_update_unpack(req, offset, &rec);
767 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNPACK)) {
768 CERROR("invalid record\n");
769 req->rq_status = -EINVAL;
772 /* rc will be used to interrupt a for loop over multiple records */
773 rc = mds_reint_rec(&rec, offset, req);
777 int mds_handle(struct ptlrpc_request *req)
782 rc = lustre_unpack_msg(req->rq_reqmsg, req->rq_reqlen);
783 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_HANDLE_UNPACK)) {
784 CERROR("lustre_mds: Invalid request\n");
788 if (req->rq_reqmsg->type != PTL_RPC_MSG_REQUEST) {
789 CERROR("lustre_mds: wrong packet type sent %d\n",
790 req->rq_reqmsg->type);
791 GOTO(out, rc = -EINVAL);
794 if (req->rq_reqmsg->opc != MDS_CONNECT &&
795 req->rq_export == NULL)
796 GOTO(out, rc = -ENOTCONN);
798 if (strcmp(req->rq_obd->obd_type->typ_name, "mds") != 0)
799 GOTO(out, rc = -EINVAL);
801 switch (req->rq_reqmsg->opc) {
803 CDEBUG(D_INODE, "connect\n");
804 OBD_FAIL_RETURN(OBD_FAIL_MDS_CONNECT_NET, 0);
805 rc = target_handle_connect(req);
809 CDEBUG(D_INODE, "disconnect\n");
810 OBD_FAIL_RETURN(OBD_FAIL_MDS_DISCONNECT_NET, 0);
811 rc = target_handle_disconnect(req);
815 CDEBUG(D_INODE, "getstatus\n");
816 OBD_FAIL_RETURN(OBD_FAIL_MDS_GETSTATUS_NET, 0);
817 rc = mds_getstatus(req);
821 CDEBUG(D_INODE, "getlovinfo\n");
822 rc = mds_getlovinfo(req);
826 CDEBUG(D_INODE, "getattr\n");
827 OBD_FAIL_RETURN(OBD_FAIL_MDS_GETATTR_NET, 0);
828 rc = mds_getattr(0, req);
832 CDEBUG(D_INODE, "statfs\n");
833 OBD_FAIL_RETURN(OBD_FAIL_MDS_STATFS_NET, 0);
834 rc = mds_statfs(req);
838 CDEBUG(D_INODE, "readpage\n");
839 OBD_FAIL_RETURN(OBD_FAIL_MDS_READPAGE_NET, 0);
840 rc = mds_readpage(req);
842 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
847 int size = sizeof(struct mds_body);
848 CDEBUG(D_INODE, "reint\n");
849 OBD_FAIL_RETURN(OBD_FAIL_MDS_REINT_NET, 0);
851 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen,
854 rc = req->rq_status = -ENOMEM;
857 rc = mds_reint(0, req);
858 OBD_FAIL_RETURN(OBD_FAIL_MDS_REINT_NET_REP, 0);
863 CDEBUG(D_INODE, "open\n");
864 OBD_FAIL_RETURN(OBD_FAIL_MDS_OPEN_NET, 0);
869 CDEBUG(D_INODE, "close\n");
870 OBD_FAIL_RETURN(OBD_FAIL_MDS_CLOSE_NET, 0);
875 CDEBUG(D_INODE, "enqueue\n");
876 OBD_FAIL_RETURN(OBD_FAIL_LDLM_ENQUEUE, 0);
877 rc = ldlm_handle_enqueue(req);
883 CDEBUG(D_INODE, "convert\n");
884 OBD_FAIL_RETURN(OBD_FAIL_LDLM_CONVERT, 0);
885 rc = ldlm_handle_convert(req);
891 CDEBUG(D_INODE, "cancel\n");
892 OBD_FAIL_RETURN(OBD_FAIL_LDLM_CANCEL, 0);
893 rc = ldlm_handle_cancel(req);
898 CDEBUG(D_INODE, "callback\n");
899 CERROR("callbacks should not happen on MDS\n");
901 OBD_FAIL_RETURN(OBD_FAIL_LDLM_CALLBACK, 0);
906 rc = ptlrpc_error(req->rq_svc, req);
913 struct mds_obd *mds = mds_req2mds(req);
914 req->rq_repmsg->last_rcvd = HTON__u64(mds->mds_last_rcvd);
915 req->rq_repmsg->last_committed =
916 HTON__u64(mds->mds_last_committed);
917 CDEBUG(D_INFO, "last_rcvd %Lu, last_committed %Lu, xid %d\n",
918 (unsigned long long)mds->mds_last_rcvd,
919 (unsigned long long)mds->mds_last_committed,
920 cpu_to_le32(req->rq_xid));
923 /* Still not 100% sure whether we should reply with the server
924 * last_rcvd or that of this client. I'm not sure it even makes
925 * a difference on a per-client basis, because last_rcvd is global
926 * and we are not supposed to allow transactions while in recovery.
929 CERROR("mds: processing error %d\n", rc);
930 ptlrpc_error(req->rq_svc, req);
932 CDEBUG(D_NET, "sending reply\n");
933 ptlrpc_reply(req->rq_svc, req);
938 /* Update the server data on disk. This stores the new mount_count and
939 * also the last_rcvd value to disk. If we don't have a clean shutdown,
940 * then the server last_rcvd value may be less than that of the clients.
941 * This will alert us that we may need to do client recovery.
944 int mds_update_server_data(struct mds_obd *mds)
946 struct obd_run_ctxt saved;
947 struct mds_server_data *msd = mds->mds_server_data;
948 struct file *filp = mds->mds_rcvd_filp;
952 msd->msd_last_rcvd = cpu_to_le64(mds->mds_last_rcvd);
953 msd->msd_mount_count = cpu_to_le64(mds->mds_mount_count);
955 CDEBUG(D_SUPER, "MDS mount_count is %Lu, last_rcvd is %Lu\n",
956 (unsigned long long)mds->mds_mount_count,
957 (unsigned long long)mds->mds_last_rcvd);
958 push_ctxt(&saved, &mds->mds_ctxt);
959 rc = lustre_fwrite(filp, (char *)msd, sizeof(*msd), &off);
960 if (rc != sizeof(*msd)) {
961 CERROR("error writing MDS server data: rc = %d\n", rc);
966 rc = fsync_dev(filp->f_dentry->d_inode->i_rdev);
969 CERROR("error flushing MDS server data: rc = %d\n", rc);
974 /* Do recovery actions for the MDS */
975 static int mds_recover(struct obd_device *obddev)
977 struct mds_obd *mds = &obddev->u.mds;
980 /* This happens at the end when recovery is complete */
981 ++mds->mds_mount_count;
982 rc = mds_update_server_data(mds);
987 #define MDS_NUM_THREADS 8
988 /* mount the file system (secretly) */
989 static int mds_setup(struct obd_device *obddev, obd_count len, void *buf)
992 struct obd_ioctl_data* data = buf;
993 struct mds_obd *mds = &obddev->u.mds;
994 struct vfsmount *mnt;
999 #ifdef CONFIG_DEV_RDONLY
1000 dev_clear_rdonly(2);
1002 if (!data->ioc_inlbuf1 || !data->ioc_inlbuf2)
1003 GOTO(err_dec, rc = -EINVAL);
1005 mds->mds_fstype = strdup(data->ioc_inlbuf2);
1007 mnt = do_kern_mount(mds->mds_fstype, 0, data->ioc_inlbuf1, NULL);
1010 CERROR("do_kern_mount failed: rc = %d\n", rc);
1011 GOTO(err_kfree, rc);
1014 mds->mds_sb = mnt->mnt_root->d_inode->i_sb;
1016 GOTO(err_put, rc = -ENODEV);
1018 mds->mds_max_mdsize = sizeof(struct lov_stripe_md);
1019 rc = mds_fs_setup(mds, mnt);
1021 CERROR("MDS filesystem method init failed: rc = %d\n", rc);
1025 mds->mds_service = ptlrpc_init_svc(64 * 1024, MDS_REQUEST_PORTAL,
1026 MDC_REPLY_PORTAL, "self",mds_handle);
1027 if (!mds->mds_service) {
1028 CERROR("failed to start service\n");
1029 GOTO(err_fs, rc = -EINVAL);
1033 obddev->obd_namespace =
1034 ldlm_namespace_new("mds_server", LDLM_NAMESPACE_SERVER);
1035 if (obddev->obd_namespace == NULL) {
1037 mds_cleanup(obddev);
1041 for (i = 0; i < MDS_NUM_THREADS; i++) {
1043 sprintf(name, "lustre_MDS_%2d", i);
1044 rc = ptlrpc_start_thread(obddev, mds->mds_service, name);
1046 CERROR("cannot start MDS thread #%d: rc %d\n", i, rc);
1048 GOTO(err_thread, rc);
1052 rc = mds_recover(obddev);
1054 GOTO(err_thread, rc);
1061 ptlrpc_stop_all_threads(mds->mds_service);
1063 ptlrpc_unregister_service(mds->mds_service);
1065 mds_fs_cleanup(mds);
1068 mntput(mds->mds_vfsmnt);
1072 kfree(mds->mds_fstype);
1078 static int mds_cleanup(struct obd_device * obddev)
1080 struct super_block *sb;
1081 struct mds_obd *mds = &obddev->u.mds;
1085 if ( !list_empty(&obddev->obd_exports) ) {
1086 CERROR("still has exports!\n");
1090 ptlrpc_stop_all_threads(mds->mds_service);
1091 ptlrpc_unregister_service(mds->mds_service);
1097 mds_update_server_data(mds);
1099 if (mds->mds_rcvd_filp) {
1100 int rc = filp_close(mds->mds_rcvd_filp, 0);
1101 mds->mds_rcvd_filp = NULL;
1104 CERROR("last_rcvd file won't close, rc=%d\n", rc);
1108 mntput(mds->mds_vfsmnt);
1110 kfree(mds->mds_fstype);
1112 ldlm_namespace_free(obddev->obd_namespace);
1115 #ifdef CONFIG_DEV_RDONLY
1116 dev_clear_rdonly(2);
1118 mds_fs_cleanup(mds);
1124 static int ldlm_intent_policy(struct ldlm_lock *lock, void *req_cookie,
1125 ldlm_mode_t mode, void *data)
1127 struct ptlrpc_request *req = req_cookie;
1134 if (req->rq_reqmsg->bufcount > 1) {
1135 /* an intent needs to be considered */
1136 struct ldlm_intent *it = lustre_msg_buf(req->rq_reqmsg, 1);
1137 struct mds_obd *mds= &req->rq_export->exp_obd->u.mds;
1138 struct mds_body *mds_rep;
1139 struct ldlm_reply *rep;
1140 __u64 new_resid[3] = {0, 0, 0}, old_res;
1141 int bufcount = -1, rc, size[3] = {sizeof(struct ldlm_reply),
1142 sizeof(struct mds_body),
1143 mds->mds_max_mdsize};
1145 it->opc = NTOH__u64(it->opc);
1147 LDLM_DEBUG(lock, "intent policy, opc: %s", ldlm_it2str(it->opc));
1150 switch((long)it->opc) {
1152 /* Note that in the negative case you may be returning
1153 * a file and its obdo */
1155 case IT_CREAT|IT_OPEN:
1176 rc = lustre_pack_msg(bufcount, size, NULL, &req->rq_replen,
1179 rc = req->rq_status = -ENOMEM;
1183 rep = lustre_msg_buf(req->rq_repmsg, 0);
1184 rep->lock_policy_res1 = 1;
1186 /* execute policy */
1187 switch ((long)it->opc) {
1189 case IT_CREAT|IT_OPEN:
1197 rc = mds_reint(2, req);
1198 if (rc || req->rq_status != 0) {
1199 rep->lock_policy_res2 = req->rq_status;
1200 RETURN(ELDLM_LOCK_ABORTED);
1210 rc = mds_getattr_name(2, req);
1211 /* FIXME: we need to sit down and decide on who should
1212 * set req->rq_status, who should return negative and
1213 * positive return values, and what they all mean. */
1214 if (rc || req->rq_status != 0) {
1215 rep->lock_policy_res2 = req->rq_status;
1216 RETURN(ELDLM_LOCK_ABORTED);
1219 case IT_READDIR|IT_OPEN:
1223 CERROR("Unhandled intent\n");
1227 if (it->opc == IT_UNLINK || it->opc == IT_RMDIR ||
1228 it->opc == IT_RENAME || it->opc == IT_RENAME2)
1229 RETURN(ELDLM_LOCK_ABORTED);
1231 rep->lock_policy_res2 = req->rq_status;
1232 mds_rep = lustre_msg_buf(req->rq_repmsg, 1);
1233 new_resid[0] = NTOH__u32(mds_rep->ino);
1234 if (new_resid[0] == 0)
1236 old_res = lock->l_resource->lr_name[0];
1238 CDEBUG(D_INFO, "remote intent: locking %d instead of"
1239 "%ld\n", mds_rep->ino, (long)old_res);
1241 ldlm_lock_change_resource(lock, new_resid);
1242 if (lock->l_resource == NULL) {
1246 LDLM_DEBUG(lock, "intent policy, old res %ld",
1248 RETURN(ELDLM_LOCK_CHANGED);
1250 int size = sizeof(struct ldlm_reply);
1251 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen,
1254 CERROR("out of memory\n");
1263 extern int mds_iocontrol(long cmd, struct lustre_handle *conn,
1264 int len, void *karg, void *uarg);
1266 /* use obd ops to offer management infrastructure */
1267 static struct obd_ops mds_obd_ops = {
1268 o_connect: mds_connect,
1269 o_disconnect: mds_disconnect,
1271 o_cleanup: mds_cleanup,
1272 o_iocontrol: mds_iocontrol
1275 static int __init mds_init(void)
1277 class_register_type(&mds_obd_ops, LUSTRE_MDS_NAME);
1278 ldlm_register_intent(ldlm_intent_policy);
1282 static void __exit mds_exit(void)
1284 ldlm_unregister_intent();
1285 class_unregister_type(LUSTRE_MDS_NAME);
1288 MODULE_AUTHOR("Cluster File Systems <info@clusterfs.com>");
1289 MODULE_DESCRIPTION("Lustre Metadata Server (MDS) v0.01");
1290 MODULE_LICENSE("GPL");
1292 module_init(mds_init);
1293 module_exit(mds_exit);