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,
101 struct dentry *dchild;
103 __u64 res_id[3] = {0};
106 down(&dir->d_inode->i_sem);
107 dchild = lookup_one_len(name, dir, namelen);
108 if (IS_ERR(dchild)) {
109 CERROR("child lookup error %ld\n", PTR_ERR(dchild));
110 up(&dir->d_inode->i_sem);
113 if (dir_lock_mode != LCK_EX && dir_lock_mode != LCK_PW) {
114 up(&dir->d_inode->i_sem);
115 ldlm_lock_decref(lockh, dir_lock_mode);
118 if (lock_mode == 0 || !dchild->d_inode)
121 res_id[0] = dchild->d_inode->i_ino;
122 rc = ldlm_match_or_enqueue(mds->mds_ldlm_client, mds->mds_ldlm_conn,
123 (struct lustre_handle *)&mds->mds_connh,
124 NULL, mds->mds_local_namespace, NULL,
125 res_id, LDLM_PLAIN, NULL, 0, lock_mode,
126 &flags, (void *)mds_lock_callback, NULL,
128 if (rc != ELDLM_OK) {
136 struct dentry *mds_fid2locked_dentry(struct mds_obd *mds, struct ll_fid *fid,
137 struct vfsmount **mnt, int lock_mode,
138 struct lustre_handle *lockh)
140 struct dentry *de = mds_fid2dentry(mds, fid, mnt), *retval = de;
142 __u64 res_id[3] = {0};
148 res_id[0] = de->d_inode->i_ino;
149 rc = ldlm_match_or_enqueue(mds->mds_ldlm_client, mds->mds_ldlm_conn,
150 (struct lustre_handle *)&mds->mds_connh,
151 NULL, mds->mds_local_namespace, NULL,
152 res_id, LDLM_PLAIN, NULL, 0, lock_mode,
153 &flags, (void *)mds_lock_callback, NULL,
155 if (rc != ELDLM_OK) {
163 struct dentry *mds_fid2dentry(struct mds_obd *mds, struct ll_fid *fid,
164 struct vfsmount **mnt)
166 /* stolen from NFS */
167 struct super_block *sb = mds->mds_sb;
168 unsigned long ino = fid->id;
169 __u32 generation = fid->generation;
171 struct list_head *lp;
172 struct dentry *result;
175 RETURN(ERR_PTR(-ESTALE));
177 inode = iget(sb, ino);
179 RETURN(ERR_PTR(-ENOMEM));
181 CDEBUG(D_DENTRY, "--> mds_fid2dentry: sb %p\n", inode->i_sb);
183 if (is_bad_inode(inode) ||
184 (generation && inode->i_generation != generation)) {
185 /* we didn't find the right inode.. */
186 CERROR("bad inode %lu, link: %d ct: %d or version %u/%u\n",
187 inode->i_ino, inode->i_nlink,
188 atomic_read(&inode->i_count), inode->i_generation,
192 RETURN(ERR_PTR(-ESTALE));
195 /* now to find a dentry.
196 * If possible, get a well-connected one
199 *mnt = mds->mds_vfsmnt;
200 spin_lock(&dcache_lock);
201 for (lp = inode->i_dentry.next; lp != &inode->i_dentry ; lp=lp->next) {
202 result = list_entry(lp,struct dentry, d_alias);
203 if (! (result->d_flags & DCACHE_NFSD_DISCONNECTED)) {
205 result->d_vfs_flags |= DCACHE_REFERENCED;
206 spin_unlock(&dcache_lock);
213 spin_unlock(&dcache_lock);
214 result = d_alloc_root(inode);
215 if (result == NULL) {
217 return ERR_PTR(-ENOMEM);
221 result->d_flags |= DCACHE_NFSD_DISCONNECTED;
225 static int mds_connect(struct lustre_handle *conn, struct obd_device *obd)
230 rc = class_connect(conn, obd);
238 static int mds_disconnect(struct lustre_handle *conn)
242 rc = class_disconnect(conn);
249 /* FIXME: the error cases need fixing to avoid leaks */
250 static int mds_getstatus(struct ptlrpc_request *req)
252 struct mds_obd *mds = mds_req2mds(req);
253 struct mds_body *body;
254 struct mds_client_info *mci;
255 struct mds_client_data *mcd;
256 int rc, size = sizeof(*body);
259 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
260 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_GETSTATUS_PACK)) {
261 CERROR("mds: out of memory for message: size=%d\n", size);
262 req->rq_status = -ENOMEM;
266 body = lustre_msg_buf(req->rq_reqmsg, 0);
267 mds_unpack_body(body);
269 /* Anything we need to do here with the client's trans no or so? */
270 body = lustre_msg_buf(req->rq_repmsg, 0);
271 memcpy(&body->fid1, &mds->mds_rootfid, sizeof(body->fid1));
273 mci = mds_uuid_to_mci(mds, ptlrpc_req_to_uuid(req));
275 /* We don't have any old connection data for this client */
278 CDEBUG(D_INFO, "allocating new client data for UUID '%s'",
279 ptlrpc_req_to_uuid(req));
281 OBD_ALLOC(mcd, sizeof(*mcd));
283 CERROR("mds: out of memory for client data\n");
284 req->rq_status = -ENOMEM;
287 memcpy(mcd->mcd_uuid, ptlrpc_req_to_uuid(req),
288 sizeof(mcd->mcd_uuid));
289 rc = mds_client_add(mds, mcd, -1);
295 /* We have old connection data for this client... */
297 CDEBUG(D_INFO, "found existing data for UUID '%s' at #%d\n",
298 mcd->mcd_uuid, mci->mci_off);
300 /* mcd_last_xid is is stored in little endian on the disk and
301 mds_pack_rep_body converts it to network order */
302 body->last_xid = le32_to_cpu(mcd->mcd_last_xid);
303 mds_pack_rep_body(req);
307 static int mds_lovinfo(struct ptlrpc_request *req)
309 struct mds_obd *mds = mds_req2mds(req);
310 struct mds_status_req *streq;
311 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("get_lovdesc error %d", rc);
335 if (desc->ld_tgt_count * sizeof(uuid_t) > streq->repbuf) {
336 CERROR("too many targets, enlarge client buffers\n");
337 req->rq_status = -ENOSPC;
341 mds->mds_max_mdsize = sizeof(desc) +
342 desc->ld_tgt_count * sizeof(uuid_t);
343 rc = mds_get_lovtgts(req->rq_obd, lustre_msg_buf(req->rq_repmsg, 1));
345 CERROR("get_lovtgts error %d", rc);
352 int mds_lock_callback(struct lustre_handle *lockh, struct ldlm_lock_desc *desc,
353 void *data, int data_len, struct ptlrpc_request **reqp)
358 /* Completion AST. Do nothing */
362 if (ldlm_cli_cancel(lockh) < 0)
367 static int mds_getattr_name(int offset, struct ptlrpc_request *req)
369 struct mds_obd *mds = mds_req2mds(req);
370 struct obd_run_ctxt saved;
371 struct mds_body *body;
372 struct dentry *de = NULL, *dchild = NULL;
374 struct lustre_handle lockh;
376 int namelen, flags, lock_mode, rc = 0;
377 __u64 res_id[3] = {0, 0, 0};
380 if (strcmp(req->rq_export->export_obd->obd_type->typ_name, "mds") != 0)
383 if (req->rq_reqmsg->bufcount <= offset + 1) {
385 GOTO(out_pre_de, rc = -EINVAL);
388 body = lustre_msg_buf(req->rq_reqmsg, offset);
389 name = lustre_msg_buf(req->rq_reqmsg, offset + 1);
390 namelen = req->rq_reqmsg->buflens[offset + 1];
391 /* requests were at offset 2, replies go back at 1 */
395 push_ctxt(&saved, &mds->mds_ctxt);
396 de = mds_fid2dentry(mds, &body->fid1, NULL);
399 GOTO(out_pre_de, rc = -ESTALE);
403 CDEBUG(D_INODE, "parent ino %ld\n", dir->i_ino);
405 lock_mode = (req->rq_reqmsg->opc == MDS_REINT) ? LCK_CW : LCK_PW;
406 res_id[0] = dir->i_ino;
408 rc = ldlm_lock_match(mds->mds_local_namespace, res_id, LDLM_PLAIN,
409 NULL, 0, lock_mode, &lockh);
411 LDLM_DEBUG_NOLOCK("enqueue res %Lu", res_id[0]);
412 rc = ldlm_cli_enqueue(mds->mds_ldlm_client, mds->mds_ldlm_conn,
413 (struct lustre_handle *)&mds->mds_connh,
414 NULL, mds->mds_local_namespace, NULL,
415 res_id, LDLM_PLAIN, NULL, 0, lock_mode,
416 &flags, (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_size = 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;
630 //struct iattr iattr;
633 md = lustre_msg_buf(req->rq_reqmsg, 1);
634 //iattr.ia_mode = inode->i_mode;
636 handle = mds_fs_start(mds, de->d_inode, MDS_FSOP_SETATTR);
638 req->rq_status = -ENOMEM;
642 /* XXX error handling */
643 rc = mds_fs_set_md(mds, inode, handle, md);
644 // rc = mds_fs_setattr(mds, de, handle, &iattr);
646 struct obd_run_ctxt saved;
647 push_ctxt(&saved, &mds->mds_ctxt);
648 rc = mds_update_last_rcvd(mds, handle, req);
654 /* FIXME: need to return last_rcvd, last_committed */
656 /* FIXME: keep rc intact */
657 rc = mds_fs_commit(mds, de->d_inode, handle);
665 file = dentry_open(de, mnt, flags & ~O_DIRECT);
666 if (!file || IS_ERR(file)) {
667 req->rq_status = -EINVAL;
668 OBD_FREE(mfd, sizeof(*mfd));
672 file->private_data = mfd;
673 mfd->mfd_file = file;
674 mfd->mfd_clientfd = body->extra;
675 list_add(&mfd->mfd_list, &mci->mci_open_head);
677 body = lustre_msg_buf(req->rq_repmsg, 0);
678 body->extra = (__u64) (unsigned long)file;
682 static int mds_close(struct ptlrpc_request *req)
685 struct mds_body *body;
687 struct mds_obd *mds = mds_req2mds(req);
688 struct vfsmount *mnt;
689 struct mds_file_data *mfd;
693 rc = lustre_pack_msg(0, NULL, NULL, &req->rq_replen, &req->rq_repmsg);
694 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_CLOSE_PACK)) {
695 CERROR("mds: out of memory\n");
696 req->rq_status = -ENOMEM;
700 body = lustre_msg_buf(req->rq_reqmsg, 0);
701 de = mds_fid2dentry(mds, &body->fid1, &mnt);
703 req->rq_status = -ENOENT;
707 file = (struct file *)(unsigned long)body->extra;
710 mfd = (struct mds_file_data *)file->private_data;
711 list_del(&mfd->mfd_list);
712 OBD_FREE(mfd, sizeof(*mfd));
714 req->rq_status = filp_close(file, 0);
721 static int mds_readpage(struct ptlrpc_request *req)
723 struct mds_obd *mds = mds_req2mds(req);
724 struct vfsmount *mnt;
727 struct mds_body *body;
728 struct obd_run_ctxt saved;
729 int rc, size = sizeof(*body);
732 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
733 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK)) {
734 CERROR("mds: out of memory\n");
735 GOTO(out, rc = -ENOMEM);
738 body = lustre_msg_buf(req->rq_reqmsg, 0);
739 push_ctxt(&saved, &mds->mds_ctxt);
740 de = mds_fid2dentry(mds, &body->fid1, &mnt);
742 GOTO(out_pop, rc = PTR_ERR(de));
744 CDEBUG(D_INODE, "ino %ld\n", de->d_inode->i_ino);
746 file = dentry_open(de, mnt, O_RDONLY | O_LARGEFILE);
747 /* note: in case of an error, dentry_open puts dentry */
749 GOTO(out_pop, rc = PTR_ERR(file));
751 /* to make this asynchronous make sure that the handling function
752 doesn't send a reply when this function completes. Instead a
753 callback function would send the reply */
754 rc = mds_sendpage(req, file, body->size);
764 int mds_reint(int offset, struct ptlrpc_request *req)
767 struct mds_update_record rec;
769 rc = mds_update_unpack(req, offset, &rec);
770 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNPACK)) {
771 CERROR("invalid record\n");
772 req->rq_status = -EINVAL;
775 /* rc will be used to interrupt a for loop over multiple records */
776 rc = mds_reint_rec(&rec, offset, req);
780 int mds_handle(struct ptlrpc_request *req)
785 rc = lustre_unpack_msg(req->rq_reqmsg, req->rq_reqlen);
786 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_HANDLE_UNPACK)) {
787 CERROR("lustre_mds: Invalid request\n");
791 if (req->rq_reqmsg->type != PTL_RPC_MSG_REQUEST) {
792 CERROR("lustre_mds: wrong packet type sent %d\n",
793 req->rq_reqmsg->type);
794 GOTO(out, rc = -EINVAL);
797 if (req->rq_reqmsg->opc != MDS_CONNECT &&
798 req->rq_export == NULL)
799 GOTO(out, rc = -ENOTCONN);
801 if (strcmp(req->rq_obd->obd_type->typ_name, "mds") != 0)
802 GOTO(out, rc = -EINVAL);
804 switch (req->rq_reqmsg->opc) {
806 CDEBUG(D_INODE, "connect\n");
807 OBD_FAIL_RETURN(OBD_FAIL_MDS_CONNECT_NET, 0);
808 rc = target_handle_connect(req);
812 CDEBUG(D_INODE, "disconnect\n");
813 OBD_FAIL_RETURN(OBD_FAIL_MDS_DISCONNECT_NET, 0);
814 rc = target_handle_disconnect(req);
818 CDEBUG(D_INODE, "getstatus\n");
819 OBD_FAIL_RETURN(OBD_FAIL_MDS_GETSTATUS_NET, 0);
820 rc = mds_getstatus(req);
824 CDEBUG(D_INODE, "lovinfo\n");
825 rc = mds_lovinfo(req);
829 CDEBUG(D_INODE, "getattr\n");
830 OBD_FAIL_RETURN(OBD_FAIL_MDS_GETATTR_NET, 0);
831 rc = mds_getattr(0, req);
835 CDEBUG(D_INODE, "statfs\n");
836 OBD_FAIL_RETURN(OBD_FAIL_MDS_STATFS_NET, 0);
837 rc = mds_statfs(req);
841 CDEBUG(D_INODE, "readpage\n");
842 OBD_FAIL_RETURN(OBD_FAIL_MDS_READPAGE_NET, 0);
843 rc = mds_readpage(req);
845 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
850 int size = sizeof(struct mds_body);
851 CDEBUG(D_INODE, "reint\n");
852 OBD_FAIL_RETURN(OBD_FAIL_MDS_REINT_NET, 0);
854 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen,
857 rc = req->rq_status = -ENOMEM;
860 rc = mds_reint(0, req);
861 OBD_FAIL_RETURN(OBD_FAIL_MDS_REINT_NET_REP, 0);
866 CDEBUG(D_INODE, "open\n");
867 OBD_FAIL_RETURN(OBD_FAIL_MDS_OPEN_NET, 0);
872 CDEBUG(D_INODE, "close\n");
873 OBD_FAIL_RETURN(OBD_FAIL_MDS_CLOSE_NET, 0);
878 rc = ptlrpc_error(req->rq_svc, req);
885 struct mds_obd *mds = mds_req2mds(req);
886 req->rq_repmsg->last_rcvd = HTON__u64(mds->mds_last_rcvd);
887 req->rq_repmsg->last_committed =
888 HTON__u64(mds->mds_last_committed);
889 CDEBUG(D_INFO, "last_rcvd %Lu, last_committed %Lu, xid %d\n",
890 (unsigned long long)mds->mds_last_rcvd,
891 (unsigned long long)mds->mds_last_committed,
892 cpu_to_le32(req->rq_xid));
895 /* Still not 100% sure whether we should reply with the server
896 * last_rcvd or that of this client. I'm not sure it even makes
897 * a difference on a per-client basis, because last_rcvd is global
898 * and we are not supposed to allow transactions while in recovery.
901 CERROR("mds: processing error %d\n", rc);
902 ptlrpc_error(req->rq_svc, req);
904 CDEBUG(D_NET, "sending reply\n");
905 ptlrpc_reply(req->rq_svc, req);
910 /* Update the server data on disk. This stores the new mount_count and
911 * also the last_rcvd value to disk. If we don't have a clean shutdown,
912 * then the server last_rcvd value may be less than that of the clients.
913 * This will alert us that we may need to do client recovery.
916 int mds_update_server_data(struct mds_obd *mds)
918 struct obd_run_ctxt saved;
919 struct mds_server_data *msd = mds->mds_server_data;
920 struct file *filp = mds->mds_rcvd_filp;
924 msd->msd_last_rcvd = cpu_to_le64(mds->mds_last_rcvd);
925 msd->msd_mount_count = cpu_to_le64(mds->mds_mount_count);
927 CDEBUG(D_SUPER, "MDS mount_count is %Lu, last_rcvd is %Lu\n",
928 (unsigned long long)mds->mds_mount_count,
929 (unsigned long long)mds->mds_last_rcvd);
930 push_ctxt(&saved, &mds->mds_ctxt);
931 rc = lustre_fwrite(filp, (char *)msd, sizeof(*msd), &off);
932 if (rc != sizeof(*msd)) {
933 CERROR("error writing MDS server data: rc = %d\n", rc);
938 rc = fsync_dev(filp->f_dentry->d_inode->i_rdev);
941 CERROR("error flushing MDS server data: rc = %d\n", rc);
946 /* Do recovery actions for the MDS */
947 static int mds_recover(struct obd_device *obddev)
949 struct mds_obd *mds = &obddev->u.mds;
952 /* This happens at the end when recovery is complete */
953 ++mds->mds_mount_count;
954 rc = mds_update_server_data(mds);
960 /* mount the file system (secretly) */
961 static int mds_setup(struct obd_device *obddev, obd_count len, void *buf)
963 struct obd_ioctl_data* data = buf;
964 struct obd_export *export;
965 struct mds_obd *mds = &obddev->u.mds;
966 struct vfsmount *mnt;
971 #ifdef CONFIG_DEV_RDONLY
974 if (!data->ioc_inlbuf1 || !data->ioc_inlbuf2)
975 GOTO(err_dec, rc = -EINVAL);
977 mds->mds_fstype = strdup(data->ioc_inlbuf2);
979 mnt = do_kern_mount(mds->mds_fstype, 0, data->ioc_inlbuf1, NULL);
982 CERROR("do_kern_mount failed: rc = %d\n", rc);
986 mds->mds_sb = mnt->mnt_root->d_inode->i_sb;
988 GOTO(err_put, rc = -ENODEV);
990 mds->mds_max_mdsize = sizeof(struct lov_stripe_md);
991 rc = mds_fs_setup(mds, mnt);
993 CERROR("MDS filesystem method init failed: rc = %d\n", rc);
997 mds->mds_service = ptlrpc_init_svc(64 * 1024, MDS_REQUEST_PORTAL,
998 MDC_REPLY_PORTAL, "self",mds_handle);
999 if (!mds->mds_service) {
1000 CERROR("failed to start service\n");
1001 GOTO(err_fs, rc = -EINVAL);
1004 rc = ptlrpc_start_thread(obddev, mds->mds_service, "lustre_mds");
1006 CERROR("cannot start thread: rc = %d\n", rc);
1011 mds->mds_ldlm_conn = ptlrpc_uuid_to_connection("self");
1012 if (!mds->mds_ldlm_conn) {
1013 mds_cleanup(obddev);
1014 GOTO(err_thread, rc);
1017 obddev->obd_namespace =
1018 ldlm_namespace_new("mds_server", LDLM_NAMESPACE_SERVER);
1019 if (obddev->obd_namespace == NULL) {
1021 mds_cleanup(obddev);
1022 GOTO(err_thread, rc);
1025 mds->mds_local_namespace =
1026 ldlm_namespace_new("mds_client", LDLM_NAMESPACE_CLIENT);
1027 if (mds->mds_local_namespace == NULL) {
1029 mds_cleanup(obddev);
1030 GOTO(err_thread, rc);
1033 OBD_ALLOC(mds->mds_ldlm_client, sizeof(*mds->mds_ldlm_client));
1034 if (mds->mds_ldlm_client == NULL) {
1036 mds_cleanup(obddev);
1037 GOTO(err_thread, rc);
1039 ptlrpc_init_client(NULL, NULL, LDLM_REQUEST_PORTAL, LDLM_REPLY_PORTAL,
1040 mds->mds_ldlm_client);
1041 mds->mds_ldlm_client->cli_target_devno = obddev->obd_minor;
1042 mds->mds_ldlm_client->cli_name = "mds ldlm";
1044 rc = mds_recover(obddev);
1046 GOTO(err_thread, rc);
1048 rc = class_connect(&mds->mds_connh, obddev);
1050 GOTO(err_thread, rc);
1051 export = class_conn2export(&mds->mds_connh);
1054 export->export_connection = mds->mds_ldlm_conn;
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;
1084 class_disconnect(&mds->mds_connh);
1087 if ( !list_empty(&obddev->obd_exports) ) {
1088 CERROR("still has exports!\n");
1092 ptlrpc_stop_all_threads(mds->mds_service);
1093 ptlrpc_unregister_service(mds->mds_service);
1099 mds_update_server_data(mds);
1101 if (mds->mds_rcvd_filp) {
1102 int rc = filp_close(mds->mds_rcvd_filp, 0);
1103 mds->mds_rcvd_filp = NULL;
1106 CERROR("last_rcvd file won't close, rc=%d\n", rc);
1110 mntput(mds->mds_vfsmnt);
1112 kfree(mds->mds_fstype);
1114 ldlm_namespace_free(mds->mds_local_namespace);
1115 ldlm_namespace_free(obddev->obd_namespace);
1117 if (mds->mds_ldlm_conn != NULL)
1118 ptlrpc_put_connection(mds->mds_ldlm_conn);
1120 OBD_FREE(mds->mds_ldlm_client, sizeof(*mds->mds_ldlm_client));
1123 #ifdef CONFIG_DEV_RDONLY
1124 dev_clear_rdonly(2);
1126 mds_fs_cleanup(mds);
1132 extern int mds_iocontrol(long cmd, struct lustre_handle *conn,
1133 int len, void *karg, void *uarg);
1135 /* use obd ops to offer management infrastructure */
1136 static struct obd_ops mds_obd_ops = {
1137 o_connect: mds_connect,
1138 o_disconnect: mds_disconnect,
1140 o_cleanup: mds_cleanup,
1141 o_iocontrol: mds_iocontrol
1144 static int __init mds_init(void)
1146 inter_module_register("mds_reint", THIS_MODULE, &mds_reint);
1147 inter_module_register("mds_getattr_name", THIS_MODULE,
1149 class_register_type(&mds_obd_ops, LUSTRE_MDS_NAME);
1153 static void __exit mds_exit(void)
1155 inter_module_unregister("mds_reint");
1156 inter_module_unregister("mds_getattr_name");
1157 class_unregister_type(LUSTRE_MDS_NAME);
1160 MODULE_AUTHOR("Cluster File Systems <info@clusterfs.com>");
1161 MODULE_DESCRIPTION("Lustre Metadata Server (MDS) v0.01");
1162 MODULE_LICENSE("GPL");
1164 module_init(mds_init);
1165 module_exit(mds_exit);