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>
16 * This server is single threaded at present (but can easily be multi threaded)
21 #define DEBUG_SUBSYSTEM S_MDS
23 #include <linux/module.h>
24 #include <linux/lustre_mds.h>
25 #include <linux/lustre_dlm.h>
26 extern int mds_get_lovtgts(struct obd_device *obd, uuid_t *uuidarray);
27 extern int mds_get_lovdesc(struct obd_device *obd, struct lov_desc *desc);
28 extern int mds_update_last_rcvd(struct mds_obd *mds, void *handle,
29 struct ptlrpc_request *req);
30 static int mds_cleanup(struct obd_device * obddev);
32 inline struct mds_obd *mds_req2mds(struct ptlrpc_request *req)
34 return &req->rq_export->export_obd->u.mds;
37 /* Assumes caller has already pushed into the kernel filesystem context */
38 static int mds_sendpage(struct ptlrpc_request *req, struct file *file,
42 struct mds_obd *mds = mds_req2mds(req);
43 struct ptlrpc_bulk_desc *desc;
44 struct ptlrpc_bulk_page *bulk;
48 desc = ptlrpc_prep_bulk(req->rq_connection);
50 GOTO(out, rc = -ENOMEM);
52 bulk = ptlrpc_prep_bulk_page(desc);
54 GOTO(cleanup_bulk, rc = -ENOMEM);
56 OBD_ALLOC(buf, PAGE_SIZE);
58 GOTO(cleanup_bulk, rc = -ENOMEM);
60 rc = mds_fs_readpage(mds, file, buf, PAGE_SIZE, (loff_t *)&offset);
63 GOTO(cleanup_buf, rc = -EIO);
65 bulk->b_xid = req->rq_xid;
67 bulk->b_buflen = PAGE_SIZE;
68 desc->b_portal = MDS_BULK_PORTAL;
70 rc = ptlrpc_send_bulk(desc);
72 GOTO(cleanup_buf, rc);
74 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE)) {
75 CERROR("obd_fail_loc=%x, fail operation rc=%d\n",
76 OBD_FAIL_MDS_SENDPAGE, rc);
77 ptlrpc_abort_bulk(desc);
78 GOTO(cleanup_buf, rc);
81 wait_event(desc->b_waitq, ptlrpc_check_bulk_sent(desc));
82 if (desc->b_flags & PTL_RPC_FL_INTR)
83 GOTO(cleanup_buf, rc = -EINTR);
87 OBD_FREE(buf, PAGE_SIZE);
89 ptlrpc_free_bulk(desc);
94 /* 'dir' is a inode for which a lock has already been taken */
95 struct dentry *mds_name2locked_dentry(struct mds_obd *mds, struct dentry *dir,
96 struct vfsmount **mnt, char *name,
97 int namelen, int lock_mode,
98 struct lustre_handle *lockh)
100 struct dentry *dchild;
102 __u64 res_id[3] = {0};
105 down(&dir->d_inode->i_sem);
106 dchild = lookup_one_len(name, dir, namelen);
107 if (IS_ERR(dchild)) {
108 CERROR("child lookup error %ld\n", PTR_ERR(dchild));
109 up(&dir->d_inode->i_sem);
112 up(&dir->d_inode->i_sem);
117 res_id[0] = dchild->d_inode->i_ino;
118 rc = ldlm_match_or_enqueue(mds->mds_ldlm_client, mds->mds_ldlm_conn,
119 (struct lustre_handle *)&mds->mds_connh,
120 NULL, mds->mds_local_namespace, NULL,
121 res_id, LDLM_PLAIN, NULL, 0, lock_mode,
122 &flags, (void *)mds_lock_callback, NULL,
124 if (rc != ELDLM_OK) {
132 struct dentry *mds_fid2locked_dentry(struct mds_obd *mds, struct ll_fid *fid,
133 struct vfsmount **mnt, int lock_mode,
134 struct lustre_handle *lockh)
136 struct dentry *de = mds_fid2dentry(mds, fid, mnt), *retval = de;
138 __u64 res_id[3] = {0};
144 res_id[0] = de->d_inode->i_ino;
145 rc = ldlm_match_or_enqueue(mds->mds_ldlm_client, mds->mds_ldlm_conn,
146 (struct lustre_handle *)&mds->mds_connh,
147 NULL, mds->mds_local_namespace, NULL,
148 res_id, LDLM_PLAIN, NULL, 0, lock_mode,
149 &flags, (void *)mds_lock_callback, NULL,
151 if (rc != ELDLM_OK) {
159 struct dentry *mds_fid2dentry(struct mds_obd *mds, struct ll_fid *fid,
160 struct vfsmount **mnt)
162 /* stolen from NFS */
163 struct super_block *sb = mds->mds_sb;
164 unsigned long ino = fid->id;
165 __u32 generation = fid->generation;
167 struct list_head *lp;
168 struct dentry *result;
171 RETURN(ERR_PTR(-ESTALE));
173 inode = iget(sb, ino);
175 RETURN(ERR_PTR(-ENOMEM));
177 CDEBUG(D_DENTRY, "--> mds_fid2dentry: sb %p\n", inode->i_sb);
179 if (is_bad_inode(inode) ||
180 (generation && inode->i_generation != generation)) {
181 /* we didn't find the right inode.. */
182 CERROR("bad inode %lu, link: %d ct: %d or version %u/%u\n",
183 inode->i_ino, inode->i_nlink,
184 atomic_read(&inode->i_count), inode->i_generation,
188 RETURN(ERR_PTR(-ESTALE));
191 /* now to find a dentry.
192 * If possible, get a well-connected one
195 *mnt = mds->mds_vfsmnt;
196 spin_lock(&dcache_lock);
197 for (lp = inode->i_dentry.next; lp != &inode->i_dentry ; lp=lp->next) {
198 result = list_entry(lp,struct dentry, d_alias);
199 if (! (result->d_flags & DCACHE_NFSD_DISCONNECTED)) {
201 result->d_vfs_flags |= DCACHE_REFERENCED;
202 spin_unlock(&dcache_lock);
209 spin_unlock(&dcache_lock);
210 result = d_alloc_root(inode);
211 if (result == NULL) {
213 return ERR_PTR(-ENOMEM);
217 result->d_flags |= DCACHE_NFSD_DISCONNECTED;
221 static int mds_connect(struct lustre_handle *conn, struct obd_device *obd)
226 rc = class_connect(conn, obd);
234 static int mds_disconnect(struct lustre_handle *conn)
238 rc = class_disconnect(conn);
245 /* FIXME: the error cases need fixing to avoid leaks */
246 static int mds_getstatus(struct ptlrpc_request *req)
248 struct mds_obd *mds = mds_req2mds(req);
249 struct mds_body *body;
250 struct mds_client_info *mci;
251 struct mds_client_data *mcd;
252 int rc, size = sizeof(*body);
255 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
256 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_GETSTATUS_PACK)) {
257 CERROR("mds: out of memory for message: size=%d\n", size);
258 req->rq_status = -ENOMEM;
262 body = lustre_msg_buf(req->rq_reqmsg, 0);
263 mds_unpack_body(body);
265 /* Anything we need to do here with the client's trans no or so? */
266 body = lustre_msg_buf(req->rq_repmsg, 0);
267 memcpy(&body->fid1, &mds->mds_rootfid, sizeof(body->fid1));
269 mci = mds_uuid_to_mci(mds, ptlrpc_req_to_uuid(req));
271 /* We don't have any old connection data for this client */
274 CDEBUG(D_INFO, "allocating new client data for UUID '%s'",
275 ptlrpc_req_to_uuid(req));
277 OBD_ALLOC(mcd, sizeof(*mcd));
279 CERROR("mds: out of memory for client data\n");
280 req->rq_status = -ENOMEM;
283 memcpy(mcd->mcd_uuid, ptlrpc_req_to_uuid(req),
284 sizeof(mcd->mcd_uuid));
285 rc = mds_client_add(mds, mcd, -1);
291 /* We have old connection data for this client... */
293 CDEBUG(D_INFO, "found existing data for UUID '%s' at #%d\n",
294 mcd->mcd_uuid, mci->mci_off);
296 /* mcd_last_xid is is stored in little endian on the disk and
297 mds_pack_rep_body converts it to network order */
298 body->last_xid = le32_to_cpu(mcd->mcd_last_xid);
299 mds_pack_rep_body(req);
303 static int mds_lovinfo(struct ptlrpc_request *req)
305 struct mds_status_req *streq;
306 struct lov_desc *desc;
307 int rc, size[2] = {sizeof(*desc)};
310 streq = lustre_msg_buf(req->rq_reqmsg, 0);
311 streq->flags = NTOH__u32(streq->flags);
312 streq->repbuf = NTOH__u32(streq->repbuf);
313 size[1] = streq->repbuf;
315 rc = lustre_pack_msg(2, size, NULL, &req->rq_replen, &req->rq_repmsg);
317 CERROR("mds: out of memory for message: size=%d\n", size[1]);
318 req->rq_status = -ENOMEM;
322 desc = lustre_msg_buf(req->rq_repmsg, 0);
323 rc = mds_get_lovdesc(req->rq_obd, desc);
325 CERROR("get_lovdesc error %d", rc);
330 if (desc->ld_tgt_count * sizeof(uuid_t) > streq->repbuf) {
331 CERROR("too many targets, enlarge client buffers\n");
332 req->rq_status = -ENOSPC;
336 rc = mds_get_lovtgts(req->rq_obd, lustre_msg_buf(req->rq_repmsg, 1));
338 CERROR("get_lovtgts error %d", rc);
345 int mds_lock_callback(struct lustre_handle *lockh, struct ldlm_lock_desc *desc,
346 void *data, int data_len, struct ptlrpc_request **reqp)
351 /* Completion AST. Do nothing */
355 if (ldlm_cli_cancel(lockh) < 0)
360 static int mds_getattr_name(int offset, struct ptlrpc_request *req)
362 struct mds_obd *mds = mds_req2mds(req);
363 struct obd_run_ctxt saved;
364 struct mds_body *body;
365 struct dentry *de = NULL, *dchild = NULL;
367 struct lustre_handle lockh;
369 int namelen, flags, lock_mode, rc = 0;
370 __u64 res_id[3] = {0, 0, 0};
373 if (strcmp(req->rq_export->export_obd->obd_type->typ_name, "mds") != 0)
376 if (req->rq_reqmsg->bufcount <= offset + 1) {
378 GOTO(out_pre_de, rc = -EINVAL);
381 body = lustre_msg_buf(req->rq_reqmsg, offset);
382 name = lustre_msg_buf(req->rq_reqmsg, offset + 1);
383 namelen = req->rq_reqmsg->buflens[offset + 1];
384 /* requests were at offset 2, replies go back at 1 */
388 push_ctxt(&saved, &mds->mds_ctxt);
389 de = mds_fid2dentry(mds, &body->fid1, NULL);
392 GOTO(out_pre_de, rc = -ESTALE);
396 CDEBUG(D_INODE, "parent ino %ld\n", dir->i_ino);
398 lock_mode = (req->rq_reqmsg->opc == MDS_REINT) ? LCK_CW : LCK_PW;
399 res_id[0] = dir->i_ino;
401 rc = ldlm_lock_match(mds->mds_local_namespace, res_id, LDLM_PLAIN,
402 NULL, 0, lock_mode, &lockh);
404 LDLM_DEBUG_NOLOCK("enqueue res %Lu", res_id[0]);
405 rc = ldlm_cli_enqueue(mds->mds_ldlm_client, mds->mds_ldlm_conn,
406 (struct lustre_handle *)&mds->mds_connh,
407 NULL, mds->mds_local_namespace, NULL,
408 res_id, LDLM_PLAIN, NULL, 0, lock_mode,
409 &flags, (void *)mds_lock_callback,
411 if (rc != ELDLM_OK) {
412 CERROR("lock enqueue: err: %d\n", rc);
413 GOTO(out_create_de, rc = -EIO);
416 ldlm_lock_dump((void *)(unsigned long)lockh.addr);
419 dchild = lookup_one_len(name, de, namelen - 1);
420 if (IS_ERR(dchild)) {
421 CERROR("child lookup error %ld\n", PTR_ERR(dchild));
424 GOTO(out_create_dchild, rc = -ESTALE);
427 if (dchild->d_inode) {
428 struct mds_body *body;
430 struct inode *inode = dchild->d_inode;
431 CERROR("child exists (dir %ld, name %s, ino %ld)\n",
432 dir->i_ino, name, dchild->d_inode->i_ino);
434 body = lustre_msg_buf(req->rq_repmsg, offset);
435 mds_pack_inode2fid(&body->fid1, inode);
436 mds_pack_inode2body(body, inode);
437 if (S_ISREG(inode->i_mode)) {
438 obdo = lustre_msg_buf(req->rq_repmsg, offset + 1);
439 mds_fs_get_obdo(mds, inode, obdo);
441 /* now a normal case for intent locking */
450 ldlm_lock_decref(&lockh, lock_mode);
460 static int mds_getattr(int offset, struct ptlrpc_request *req)
462 struct mds_obd *mds = mds_req2mds(req);
463 struct obd_run_ctxt saved;
466 struct mds_body *body;
467 int rc, size[2] = {sizeof(*body)}, bufcount = 1;
470 body = lustre_msg_buf(req->rq_reqmsg, offset);
471 push_ctxt(&saved, &mds->mds_ctxt);
472 de = mds_fid2dentry(mds, &body->fid1, NULL);
474 GOTO(out_pop, rc = -ENOENT);
478 if (S_ISREG(body->fid1.f_type)) {
480 size[1] = sizeof(struct obdo);
481 } else if (body->valid & OBD_MD_LINKNAME) {
483 size[1] = inode->i_size;
486 rc = lustre_pack_msg(bufcount, size, NULL, &req->rq_replen,
488 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK)) {
489 CERROR("mds: out of memory\n");
493 if (body->valid & OBD_MD_LINKNAME) {
494 char *tmp = lustre_msg_buf(req->rq_repmsg, 1);
496 rc = inode->i_op->readlink(de, tmp, size[1]);
499 CERROR("readlink failed: %d\n", rc);
504 body = lustre_msg_buf(req->rq_repmsg, 0);
505 body->ino = inode->i_ino;
506 body->generation = inode->i_generation;
507 body->atime = inode->i_atime;
508 body->ctime = inode->i_ctime;
509 body->mtime = inode->i_mtime;
510 body->uid = inode->i_uid;
511 body->gid = inode->i_gid;
512 body->size = inode->i_size;
513 body->mode = inode->i_mode;
514 body->nlink = inode->i_nlink;
515 body->valid = ~0; /* FIXME: should be more selective */
517 if (S_ISREG(inode->i_mode)) {
518 rc = mds_fs_get_obdo(mds, inode,
519 lustre_msg_buf(req->rq_repmsg, 1));
521 CERROR("mds_fs_get_obdo failed: %d\n", rc);
533 static int mds_statfs(struct ptlrpc_request *req)
535 struct mds_obd *mds = mds_req2mds(req);
536 struct obd_statfs *osfs;
538 int rc, size = sizeof(*osfs);
541 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen,
543 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_STATFS_PACK)) {
544 CERROR("mds: statfs lustre_pack_msg failed: rc = %d\n", rc);
548 rc = vfs_statfs(mds->mds_sb, &sfs);
550 CERROR("mds: statfs failed: rc %d\n", rc);
553 osfs = lustre_msg_buf(req->rq_repmsg, 0);
554 memset(osfs, 0, size);
555 obd_statfs_pack(osfs, &sfs);
562 static int mds_open(struct ptlrpc_request *req)
565 struct mds_body *body;
567 struct vfsmount *mnt;
568 struct mds_obd *mds = mds_req2mds(req);
569 struct mds_client_info *mci;
571 struct list_head *tmp;
572 struct mds_file_data *mfd;
573 int rc, size = sizeof(*body);
576 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
577 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_OPEN_PACK)) {
578 CERROR("mds: out of memory\n");
579 req->rq_status = -ENOMEM;
583 mci = mds_uuid_to_mci(mds, ptlrpc_req_to_uuid(req));
585 CERROR("mds: no mci!\n");
586 req->rq_status = -ENOTCONN;
590 body = lustre_msg_buf(req->rq_reqmsg, 0);
592 /* was this animal open already? */
593 /* XXX we should only check on re-open, or do a refcount... */
594 list_for_each(tmp, &mci->mci_open_head) {
595 struct mds_file_data *fd;
596 fd = list_entry(tmp, struct mds_file_data, mfd_list);
597 if (body->extra == fd->mfd_clientfd &&
598 body->fid1.id == fd->mfd_file->f_dentry->d_inode->i_ino) {
599 CERROR("Re opening %Ld\n", body->fid1.id);
604 OBD_ALLOC(mfd, sizeof(*mfd));
606 CERROR("mds: out of memory\n");
607 req->rq_status = -ENOMEM;
611 de = mds_fid2dentry(mds, &body->fid1, &mnt);
613 req->rq_status = -ENOENT;
617 /* check if this inode has seen a delayed object creation */
618 if (req->rq_reqmsg->bufcount > 1) {
620 struct inode *inode = de->d_inode;
621 //struct iattr iattr;
625 obdo = lustre_msg_buf(req->rq_reqmsg, 1);
626 //iattr.ia_valid = ATTR_MODE;
627 //iattr.ia_mode = inode->i_mode;
629 handle = mds_fs_start(mds, de->d_inode, MDS_FSOP_SETATTR);
631 req->rq_status = -ENOMEM;
635 /* XXX error handling */
636 rc = mds_fs_set_obdo(mds, inode, handle, obdo);
637 // rc = mds_fs_setattr(mds, de, handle, &iattr);
639 struct obd_run_ctxt saved;
640 push_ctxt(&saved, &mds->mds_ctxt);
641 rc = mds_update_last_rcvd(mds, handle, req);
647 /* FIXME: need to return last_rcvd, last_committed */
649 /* FIXME: keep rc intact */
650 rc = mds_fs_commit(mds, de->d_inode, handle);
658 file = dentry_open(de, mnt, flags & ~O_DIRECT);
659 if (!file || IS_ERR(file)) {
660 req->rq_status = -EINVAL;
661 OBD_FREE(mfd, sizeof(*mfd));
665 file->private_data = mfd;
666 mfd->mfd_file = file;
667 mfd->mfd_clientfd = body->extra;
668 list_add(&mfd->mfd_list, &mci->mci_open_head);
670 body = lustre_msg_buf(req->rq_repmsg, 0);
671 body->extra = (__u64) (unsigned long)file;
675 static int mds_close(struct ptlrpc_request *req)
678 struct mds_body *body;
680 struct mds_obd *mds = mds_req2mds(req);
681 struct vfsmount *mnt;
682 struct mds_file_data *mfd;
686 rc = lustre_pack_msg(0, NULL, NULL, &req->rq_replen, &req->rq_repmsg);
687 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_CLOSE_PACK)) {
688 CERROR("mds: out of memory\n");
689 req->rq_status = -ENOMEM;
693 body = lustre_msg_buf(req->rq_reqmsg, 0);
694 de = mds_fid2dentry(mds, &body->fid1, &mnt);
696 req->rq_status = -ENOENT;
700 file = (struct file *)(unsigned long)body->extra;
703 mfd = (struct mds_file_data *)file->private_data;
704 list_del(&mfd->mfd_list);
705 OBD_FREE(mfd, sizeof(*mfd));
707 req->rq_status = filp_close(file, 0);
714 static int mds_readpage(struct ptlrpc_request *req)
716 struct mds_obd *mds = mds_req2mds(req);
717 struct vfsmount *mnt;
720 struct mds_body *body;
721 struct obd_run_ctxt saved;
722 int rc, size = sizeof(*body);
725 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
726 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK)) {
727 CERROR("mds: out of memory\n");
728 GOTO(out, rc = -ENOMEM);
731 body = lustre_msg_buf(req->rq_reqmsg, 0);
732 push_ctxt(&saved, &mds->mds_ctxt);
733 de = mds_fid2dentry(mds, &body->fid1, &mnt);
735 GOTO(out_pop, rc = PTR_ERR(de));
737 CDEBUG(D_INODE, "ino %ld\n", de->d_inode->i_ino);
739 file = dentry_open(de, mnt, O_RDONLY | O_LARGEFILE);
740 /* note: in case of an error, dentry_open puts dentry */
742 GOTO(out_pop, rc = PTR_ERR(file));
744 /* to make this asynchronous make sure that the handling function
745 doesn't send a reply when this function completes. Instead a
746 callback function would send the reply */
747 rc = mds_sendpage(req, file, body->size);
757 int mds_reint(int offset, struct ptlrpc_request *req)
760 struct mds_update_record rec;
762 rc = mds_update_unpack(req, offset, &rec);
763 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNPACK)) {
764 CERROR("invalid record\n");
765 req->rq_status = -EINVAL;
768 /* rc will be used to interrupt a for loop over multiple records */
769 rc = mds_reint_rec(&rec, offset, req);
773 int mds_handle(struct ptlrpc_request *req)
778 rc = lustre_unpack_msg(req->rq_reqmsg, req->rq_reqlen);
779 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_HANDLE_UNPACK)) {
780 CERROR("lustre_mds: Invalid request\n");
784 if (req->rq_reqmsg->type != PTL_RPC_MSG_REQUEST) {
785 CERROR("lustre_mds: wrong packet type sent %d\n",
786 req->rq_reqmsg->type);
787 GOTO(out, rc = -EINVAL);
790 if (req->rq_reqmsg->opc != MDS_CONNECT &&
791 req->rq_export == NULL)
792 GOTO(out, rc = -ENOTCONN);
794 if (strcmp(req->rq_obd->obd_type->typ_name, "mds") != 0)
795 GOTO(out, rc = -EINVAL);
797 switch (req->rq_reqmsg->opc) {
799 CDEBUG(D_INODE, "connect\n");
800 OBD_FAIL_RETURN(OBD_FAIL_MDS_CONNECT_NET, 0);
801 rc = target_handle_connect(req);
805 CDEBUG(D_INODE, "disconnect\n");
806 OBD_FAIL_RETURN(OBD_FAIL_MDS_DISCONNECT_NET, 0);
807 rc = target_handle_disconnect(req);
811 CDEBUG(D_INODE, "getstatus\n");
812 OBD_FAIL_RETURN(OBD_FAIL_MDS_GETSTATUS_NET, 0);
813 rc = mds_getstatus(req);
817 CDEBUG(D_INODE, "lovinfo\n");
818 rc = mds_lovinfo(req);
822 CDEBUG(D_INODE, "getattr\n");
823 OBD_FAIL_RETURN(OBD_FAIL_MDS_GETATTR_NET, 0);
824 rc = mds_getattr(0, req);
828 CDEBUG(D_INODE, "statfs\n");
829 OBD_FAIL_RETURN(OBD_FAIL_MDS_STATFS_NET, 0);
830 rc = mds_statfs(req);
834 CDEBUG(D_INODE, "readpage\n");
835 OBD_FAIL_RETURN(OBD_FAIL_MDS_READPAGE_NET, 0);
836 rc = mds_readpage(req);
838 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
843 int size = sizeof(struct mds_body);
844 CDEBUG(D_INODE, "reint\n");
845 OBD_FAIL_RETURN(OBD_FAIL_MDS_REINT_NET, 0);
847 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen,
850 rc = req->rq_status = -ENOMEM;
853 rc = mds_reint(0, req);
854 OBD_FAIL_RETURN(OBD_FAIL_MDS_REINT_NET_REP, 0);
859 CDEBUG(D_INODE, "open\n");
860 OBD_FAIL_RETURN(OBD_FAIL_MDS_OPEN_NET, 0);
865 CDEBUG(D_INODE, "close\n");
866 OBD_FAIL_RETURN(OBD_FAIL_MDS_CLOSE_NET, 0);
871 rc = ptlrpc_error(req->rq_svc, req);
878 struct mds_obd *mds = mds_req2mds(req);
879 req->rq_repmsg->last_rcvd = HTON__u64(mds->mds_last_rcvd);
880 req->rq_repmsg->last_committed =
881 HTON__u64(mds->mds_last_committed);
882 CDEBUG(D_INFO, "last_rcvd %Lu, last_committed %Lu, xid %d\n",
883 (unsigned long long)mds->mds_last_rcvd,
884 (unsigned long long)mds->mds_last_committed,
885 cpu_to_le32(req->rq_xid));
888 /* Still not 100% sure whether we should reply with the server
889 * last_rcvd or that of this client. I'm not sure it even makes
890 * a difference on a per-client basis, because last_rcvd is global
891 * and we are not supposed to allow transactions while in recovery.
894 CERROR("mds: processing error %d\n", rc);
895 ptlrpc_error(req->rq_svc, req);
897 CDEBUG(D_NET, "sending reply\n");
898 ptlrpc_reply(req->rq_svc, req);
903 /* Update the server data on disk. This stores the new mount_count and
904 * also the last_rcvd value to disk. If we don't have a clean shutdown,
905 * then the server last_rcvd value may be less than that of the clients.
906 * This will alert us that we may need to do client recovery.
909 int mds_update_server_data(struct mds_obd *mds)
911 struct obd_run_ctxt saved;
912 struct mds_server_data *msd = mds->mds_server_data;
913 struct file *filp = mds->mds_rcvd_filp;
917 msd->msd_last_rcvd = cpu_to_le64(mds->mds_last_rcvd);
918 msd->msd_mount_count = cpu_to_le64(mds->mds_mount_count);
920 CDEBUG(D_SUPER, "MDS mount_count is %Lu, last_rcvd is %Lu\n",
921 (unsigned long long)mds->mds_mount_count,
922 (unsigned long long)mds->mds_last_rcvd);
923 push_ctxt(&saved, &mds->mds_ctxt);
924 rc = lustre_fwrite(filp, (char *)msd, sizeof(*msd), &off);
925 if (rc != sizeof(*msd)) {
926 CERROR("error writing MDS server data: rc = %d\n", rc);
931 rc = fsync_dev(filp->f_dentry->d_inode->i_rdev);
934 CERROR("error flushing MDS server data: rc = %d\n", rc);
939 /* Do recovery actions for the MDS */
940 static int mds_recover(struct obd_device *obddev)
942 struct mds_obd *mds = &obddev->u.mds;
945 /* This happens at the end when recovery is complete */
946 ++mds->mds_mount_count;
947 rc = mds_update_server_data(mds);
953 /* mount the file system (secretly) */
954 static int mds_setup(struct obd_device *obddev, obd_count len, void *buf)
956 struct obd_ioctl_data* data = buf;
957 struct obd_export *export;
958 struct mds_obd *mds = &obddev->u.mds;
959 struct vfsmount *mnt;
964 #ifdef CONFIG_DEV_RDONLY
967 if (!data->ioc_inlbuf1 || !data->ioc_inlbuf2)
968 GOTO(err_dec, rc = -EINVAL);
970 mds->mds_fstype = strdup(data->ioc_inlbuf2);
972 mnt = do_kern_mount(mds->mds_fstype, 0, data->ioc_inlbuf1, NULL);
975 CERROR("do_kern_mount failed: rc = %d\n", rc);
979 mds->mds_sb = mnt->mnt_root->d_inode->i_sb;
981 GOTO(err_put, rc = -ENODEV);
983 rc = mds_fs_setup(mds, mnt);
985 CERROR("MDS filesystem method init failed: rc = %d\n", rc);
989 mds->mds_service = ptlrpc_init_svc(64 * 1024, MDS_REQUEST_PORTAL,
990 MDC_REPLY_PORTAL, "self",mds_handle);
991 if (!mds->mds_service) {
992 CERROR("failed to start service\n");
993 GOTO(err_fs, rc = -EINVAL);
996 rc = ptlrpc_start_thread(obddev, mds->mds_service, "lustre_mds");
998 CERROR("cannot start thread: rc = %d\n", rc);
1003 mds->mds_ldlm_conn = ptlrpc_uuid_to_connection("self");
1004 if (!mds->mds_ldlm_conn) {
1005 mds_cleanup(obddev);
1006 GOTO(err_thread, rc);
1009 obddev->obd_namespace =
1010 ldlm_namespace_new("mds_server", LDLM_NAMESPACE_SERVER);
1011 if (obddev->obd_namespace == NULL) {
1013 mds_cleanup(obddev);
1014 GOTO(err_thread, rc);
1017 mds->mds_local_namespace =
1018 ldlm_namespace_new("mds_client", LDLM_NAMESPACE_CLIENT);
1019 if (mds->mds_local_namespace == NULL) {
1021 mds_cleanup(obddev);
1022 GOTO(err_thread, rc);
1025 OBD_ALLOC(mds->mds_ldlm_client, sizeof(*mds->mds_ldlm_client));
1026 if (mds->mds_ldlm_client == NULL) {
1028 mds_cleanup(obddev);
1029 GOTO(err_thread, rc);
1031 ptlrpc_init_client(NULL, NULL, LDLM_REQUEST_PORTAL, LDLM_REPLY_PORTAL,
1032 mds->mds_ldlm_client);
1033 mds->mds_ldlm_client->cli_target_devno = obddev->obd_minor;
1034 mds->mds_ldlm_client->cli_name = "mds ldlm";
1036 rc = mds_recover(obddev);
1038 GOTO(err_thread, rc);
1040 rc = class_connect(&mds->mds_connh, obddev);
1042 GOTO(err_thread, rc);
1043 export = class_conn2export(&mds->mds_connh);
1046 export->export_connection = mds->mds_ldlm_conn;
1053 ptlrpc_stop_all_threads(mds->mds_service);
1055 ptlrpc_unregister_service(mds->mds_service);
1057 mds_fs_cleanup(mds);
1060 mntput(mds->mds_vfsmnt);
1064 kfree(mds->mds_fstype);
1070 static int mds_cleanup(struct obd_device * obddev)
1072 struct super_block *sb;
1073 struct mds_obd *mds = &obddev->u.mds;
1076 class_disconnect(&mds->mds_connh);
1079 if ( !list_empty(&obddev->obd_exports) ) {
1080 CERROR("still has exports!\n");
1084 ptlrpc_stop_all_threads(mds->mds_service);
1085 ptlrpc_unregister_service(mds->mds_service);
1091 mds_update_server_data(mds);
1093 if (mds->mds_rcvd_filp) {
1094 int rc = filp_close(mds->mds_rcvd_filp, 0);
1095 mds->mds_rcvd_filp = NULL;
1098 CERROR("last_rcvd file won't close, rc=%d\n", rc);
1102 mntput(mds->mds_vfsmnt);
1104 kfree(mds->mds_fstype);
1106 ldlm_namespace_free(mds->mds_local_namespace);
1107 ldlm_namespace_free(obddev->obd_namespace);
1109 if (mds->mds_ldlm_conn != NULL)
1110 ptlrpc_put_connection(mds->mds_ldlm_conn);
1112 OBD_FREE(mds->mds_ldlm_client, sizeof(*mds->mds_ldlm_client));
1115 #ifdef CONFIG_DEV_RDONLY
1116 dev_clear_rdonly(2);
1118 mds_fs_cleanup(mds);
1124 extern int mds_iocontrol(long cmd, struct lustre_handle *conn,
1125 int len, void *karg, void *uarg);
1127 /* use obd ops to offer management infrastructure */
1128 static struct obd_ops mds_obd_ops = {
1129 o_connect: mds_connect,
1130 o_disconnect: mds_disconnect,
1132 o_cleanup: mds_cleanup,
1133 o_iocontrol: mds_iocontrol
1136 static int __init mds_init(void)
1138 inter_module_register("mds_reint", THIS_MODULE, &mds_reint);
1139 inter_module_register("mds_getattr_name", THIS_MODULE,
1141 class_register_type(&mds_obd_ops, LUSTRE_MDS_NAME);
1145 static void __exit mds_exit(void)
1147 inter_module_unregister("mds_reint");
1148 inter_module_unregister("mds_getattr_name");
1149 class_unregister_type(LUSTRE_MDS_NAME);
1152 MODULE_AUTHOR("Cluster File Systems <info@clusterfs.com>");
1153 MODULE_DESCRIPTION("Lustre Metadata Server (MDS) v0.01");
1154 MODULE_LICENSE("GPL");
1156 module_init(mds_init);
1157 module_exit(mds_exit);