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, 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;
38 /* Assumes caller has already pushed into the kernel filesystem context */
39 static int mds_sendpage(struct ptlrpc_request *req, struct file *file,
43 struct mds_obd *mds = mds_req2mds(req);
44 struct ptlrpc_bulk_desc *desc;
45 struct ptlrpc_bulk_page *bulk;
49 desc = ptlrpc_prep_bulk(req->rq_connection);
51 GOTO(out, rc = -ENOMEM);
53 bulk = ptlrpc_prep_bulk_page(desc);
55 GOTO(cleanup_bulk, rc = -ENOMEM);
57 OBD_ALLOC(buf, PAGE_SIZE);
59 GOTO(cleanup_bulk, rc = -ENOMEM);
61 rc = mds_fs_readpage(mds, file, buf, PAGE_SIZE, (loff_t *)&offset);
64 GOTO(cleanup_buf, rc = -EIO);
66 bulk->b_xid = req->rq_xid;
68 bulk->b_buflen = PAGE_SIZE;
69 desc->b_portal = MDS_BULK_PORTAL;
71 rc = ptlrpc_send_bulk(desc);
73 GOTO(cleanup_buf, rc);
75 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE)) {
76 CERROR("obd_fail_loc=%x, fail operation rc=%d\n",
77 OBD_FAIL_MDS_SENDPAGE, rc);
78 ptlrpc_abort_bulk(desc);
79 GOTO(cleanup_buf, rc);
82 wait_event(desc->b_waitq, ptlrpc_check_bulk_sent(desc));
83 if (desc->b_flags & PTL_RPC_FL_INTR)
84 GOTO(cleanup_buf, rc = -EINTR);
88 OBD_FREE(buf, PAGE_SIZE);
90 ptlrpc_free_bulk(desc);
95 /* 'dir' is a inode for which a lock has already been taken */
96 struct dentry *mds_name2locked_dentry(struct mds_obd *mds, struct dentry *dir,
97 struct vfsmount **mnt, char *name,
98 int namelen, int lock_mode,
99 struct lustre_handle *lockh,
102 struct dentry *dchild;
104 __u64 res_id[3] = {0};
107 down(&dir->d_inode->i_sem);
108 dchild = lookup_one_len(name, dir, namelen);
109 if (IS_ERR(dchild)) {
110 CERROR("child lookup error %ld\n", PTR_ERR(dchild));
111 up(&dir->d_inode->i_sem);
114 if (dir_lock_mode != LCK_EX && dir_lock_mode != LCK_PW) {
115 up(&dir->d_inode->i_sem);
116 ldlm_lock_decref(lockh, dir_lock_mode);
119 if (lock_mode == 0 || !dchild->d_inode)
122 res_id[0] = dchild->d_inode->i_ino;
123 rc = ldlm_match_or_enqueue(mds->mds_ldlm_client, mds->mds_ldlm_conn,
124 (struct lustre_handle *)&mds->mds_connh,
125 NULL, mds->mds_local_namespace, NULL,
126 res_id, LDLM_PLAIN, NULL, 0, lock_mode,
127 &flags, (void *)mds_lock_callback, NULL,
129 if (rc != ELDLM_OK) {
137 struct dentry *mds_fid2locked_dentry(struct mds_obd *mds, struct ll_fid *fid,
138 struct vfsmount **mnt, int lock_mode,
139 struct lustre_handle *lockh)
141 struct dentry *de = mds_fid2dentry(mds, fid, mnt), *retval = de;
143 __u64 res_id[3] = {0};
149 res_id[0] = de->d_inode->i_ino;
150 rc = ldlm_match_or_enqueue(mds->mds_ldlm_client, mds->mds_ldlm_conn,
151 (struct lustre_handle *)&mds->mds_connh,
152 NULL, mds->mds_local_namespace, NULL,
153 res_id, LDLM_PLAIN, NULL, 0, lock_mode,
154 &flags, (void *)mds_lock_callback, NULL,
156 if (rc != ELDLM_OK) {
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.
197 * If possible, get a well-connected one
200 *mnt = mds->mds_vfsmnt;
201 spin_lock(&dcache_lock);
202 for (lp = inode->i_dentry.next; lp != &inode->i_dentry ; lp=lp->next) {
203 result = list_entry(lp,struct dentry, d_alias);
204 if (! (result->d_flags & DCACHE_NFSD_DISCONNECTED)) {
206 result->d_vfs_flags |= DCACHE_REFERENCED;
207 spin_unlock(&dcache_lock);
214 spin_unlock(&dcache_lock);
215 result = d_alloc_root(inode);
216 if (result == NULL) {
218 return ERR_PTR(-ENOMEM);
222 result->d_flags |= DCACHE_NFSD_DISCONNECTED;
226 static int mds_connect(struct lustre_handle *conn, struct obd_device *obd)
231 rc = class_connect(conn, obd);
239 static int mds_disconnect(struct lustre_handle *conn)
243 rc = class_disconnect(conn);
250 /* FIXME: the error cases need fixing to avoid leaks */
251 static int mds_getstatus(struct ptlrpc_request *req)
253 struct mds_obd *mds = mds_req2mds(req);
254 struct mds_body *body;
255 struct mds_client_info *mci;
256 struct mds_client_data *mcd;
257 int rc, size = sizeof(*body);
260 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
261 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_GETSTATUS_PACK)) {
262 CERROR("mds: out of memory for message: size=%d\n", size);
263 req->rq_status = -ENOMEM;
267 body = lustre_msg_buf(req->rq_reqmsg, 0);
268 mds_unpack_body(body);
270 /* Anything we need to do here with the client's trans no or so? */
271 body = lustre_msg_buf(req->rq_repmsg, 0);
272 memcpy(&body->fid1, &mds->mds_rootfid, sizeof(body->fid1));
274 mci = mds_uuid_to_mci(mds, ptlrpc_req_to_uuid(req));
276 /* We don't have any old connection data for this client */
279 CDEBUG(D_INFO, "allocating new client data for UUID '%s'",
280 ptlrpc_req_to_uuid(req));
282 OBD_ALLOC(mcd, sizeof(*mcd));
284 CERROR("mds: out of memory for client data\n");
285 req->rq_status = -ENOMEM;
288 memcpy(mcd->mcd_uuid, ptlrpc_req_to_uuid(req),
289 sizeof(mcd->mcd_uuid));
290 rc = mds_client_add(mds, mcd, -1);
296 /* We have old connection data for this client... */
298 CDEBUG(D_INFO, "found existing data for UUID '%s' at #%d\n",
299 mcd->mcd_uuid, mci->mci_off);
301 /* mcd_last_xid is is stored in little endian on the disk and
302 mds_pack_rep_body converts it to network order */
303 body->last_xid = le32_to_cpu(mcd->mcd_last_xid);
304 mds_pack_rep_body(req);
308 static int mds_getlovinfo(struct ptlrpc_request *req)
310 struct mds_obd *mds = mds_req2mds(req);
311 struct mds_status_req *streq;
312 struct lov_desc *desc;
314 int rc, size[2] = {sizeof(*desc)};
317 streq = lustre_msg_buf(req->rq_reqmsg, 0);
318 streq->flags = NTOH__u32(streq->flags);
319 streq->repbuf = NTOH__u32(streq->repbuf);
320 size[1] = streq->repbuf;
322 rc = lustre_pack_msg(2, size, NULL, &req->rq_replen, &req->rq_repmsg);
324 CERROR("mds: out of memory for message: size=%d\n", size[1]);
325 req->rq_status = -ENOMEM;
329 desc = lustre_msg_buf(req->rq_repmsg, 0);
330 rc = mds_get_lovdesc(req->rq_obd, desc);
332 CERROR("mds_get_lovdesc error %d", rc);
337 tgt_count = NTOH__u32(desc->ld_tgt_count);
338 if (tgt_count * sizeof(uuid_t) > streq->repbuf) {
339 CERROR("too many targets, enlarge client buffers\n");
340 req->rq_status = -ENOSPC;
344 mds->mds_max_mdsize = sizeof(desc) + tgt_count * sizeof(uuid_t);
345 rc = mds_get_lovtgts(req->rq_obd, tgt_count,
346 lustre_msg_buf(req->rq_repmsg, 1));
348 CERROR("get_lovtgts error %d", rc);
355 int mds_lock_callback(struct lustre_handle *lockh, struct ldlm_lock_desc *desc,
356 void *data, int data_len, struct ptlrpc_request **reqp)
361 /* Completion AST. Do nothing */
365 if (ldlm_cli_cancel(lockh) < 0)
370 static int mds_getattr_name(int offset, struct ptlrpc_request *req)
372 struct mds_obd *mds = mds_req2mds(req);
373 struct obd_run_ctxt saved;
374 struct mds_body *body;
375 struct dentry *de = NULL, *dchild = NULL;
377 struct lustre_handle lockh;
379 int namelen, flags, lock_mode, rc = 0;
380 __u64 res_id[3] = {0, 0, 0};
383 if (strcmp(req->rq_export->exp_obd->obd_type->typ_name, "mds") != 0)
386 if (req->rq_reqmsg->bufcount <= offset + 1) {
388 GOTO(out_pre_de, rc = -EINVAL);
391 body = lustre_msg_buf(req->rq_reqmsg, offset);
392 name = lustre_msg_buf(req->rq_reqmsg, offset + 1);
393 namelen = req->rq_reqmsg->buflens[offset + 1];
394 /* requests were at offset 2, replies go back at 1 */
398 push_ctxt(&saved, &mds->mds_ctxt);
399 de = mds_fid2dentry(mds, &body->fid1, NULL);
402 GOTO(out_pre_de, rc = -ESTALE);
406 CDEBUG(D_INODE, "parent ino %ld\n", dir->i_ino);
408 lock_mode = (req->rq_reqmsg->opc == MDS_REINT) ? LCK_CW : LCK_PW;
409 res_id[0] = dir->i_ino;
411 rc = ldlm_lock_match(mds->mds_local_namespace, res_id, LDLM_PLAIN,
412 NULL, 0, lock_mode, &lockh);
414 LDLM_DEBUG_NOLOCK("enqueue res %Lu", res_id[0]);
415 rc = ldlm_cli_enqueue(mds->mds_ldlm_client, mds->mds_ldlm_conn,
416 (struct lustre_handle *)&mds->mds_connh,
417 NULL, mds->mds_local_namespace, NULL,
418 res_id, LDLM_PLAIN, NULL, 0, lock_mode,
419 &flags, (void *)mds_lock_callback,
421 if (rc != ELDLM_OK) {
422 CERROR("lock enqueue: err: %d\n", rc);
423 GOTO(out_create_de, rc = -EIO);
426 ldlm_lock_dump((void *)(unsigned long)lockh.addr);
429 dchild = lookup_one_len(name, de, namelen - 1);
430 if (IS_ERR(dchild)) {
431 CDEBUG(D_INODE, "child lookup error %ld\n", PTR_ERR(dchild));
434 GOTO(out_create_dchild, rc = -ESTALE);
437 if (dchild->d_inode) {
438 struct mds_body *body;
439 struct inode *inode = dchild->d_inode;
440 CDEBUG(D_INODE, "child exists (dir %ld, name %s, ino %ld)\n",
441 dir->i_ino, name, dchild->d_inode->i_ino);
443 body = lustre_msg_buf(req->rq_repmsg, offset);
444 mds_pack_inode2fid(&body->fid1, inode);
445 mds_pack_inode2body(body, inode);
446 if (S_ISREG(inode->i_mode)) {
447 struct lov_stripe_md *md;
448 md = lustre_msg_buf(req->rq_repmsg, offset + 1);
449 md->lmd_size = mds->mds_max_mdsize;
450 mds_fs_get_md(mds, inode, md);
452 /* now a normal case for intent locking */
461 ldlm_lock_decref(&lockh, lock_mode);
471 static int mds_getattr(int offset, struct ptlrpc_request *req)
473 struct mds_obd *mds = mds_req2mds(req);
474 struct obd_run_ctxt saved;
477 struct mds_body *body;
478 int rc, size[2] = {sizeof(*body)}, bufcount = 1;
481 body = lustre_msg_buf(req->rq_reqmsg, offset);
482 push_ctxt(&saved, &mds->mds_ctxt);
483 de = mds_fid2dentry(mds, &body->fid1, NULL);
485 GOTO(out_pop, rc = -ENOENT);
489 if (S_ISREG(body->fid1.f_type)) {
491 size[1] = mds->mds_max_mdsize;
492 } else if (body->valid & OBD_MD_LINKNAME) {
494 size[1] = inode->i_size;
497 rc = lustre_pack_msg(bufcount, size, NULL, &req->rq_replen,
499 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK)) {
500 CERROR("mds: out of memory\n");
504 if (body->valid & OBD_MD_LINKNAME) {
505 char *tmp = lustre_msg_buf(req->rq_repmsg, 1);
507 rc = inode->i_op->readlink(de, tmp, size[1]);
510 CERROR("readlink failed: %d\n", rc);
515 body = lustre_msg_buf(req->rq_repmsg, 0);
516 body->ino = inode->i_ino;
517 body->generation = inode->i_generation;
518 body->atime = inode->i_atime;
519 body->ctime = inode->i_ctime;
520 body->mtime = inode->i_mtime;
521 body->uid = inode->i_uid;
522 body->gid = inode->i_gid;
523 body->size = inode->i_size;
524 body->mode = inode->i_mode;
525 body->nlink = inode->i_nlink;
526 body->valid = ~0; /* FIXME: should be more selective */
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);
544 static int mds_statfs(struct ptlrpc_request *req)
546 struct mds_obd *mds = mds_req2mds(req);
547 struct obd_statfs *osfs;
549 int rc, size = sizeof(*osfs);
552 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen,
554 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_STATFS_PACK)) {
555 CERROR("mds: statfs lustre_pack_msg failed: rc = %d\n", rc);
559 rc = vfs_statfs(mds->mds_sb, &sfs);
561 CERROR("mds: statfs failed: rc %d\n", rc);
564 osfs = lustre_msg_buf(req->rq_repmsg, 0);
565 memset(osfs, 0, size);
566 obd_statfs_pack(osfs, &sfs);
573 static int mds_open(struct ptlrpc_request *req)
576 struct mds_body *body;
578 struct vfsmount *mnt;
579 struct mds_obd *mds = mds_req2mds(req);
580 struct mds_client_info *mci;
582 struct list_head *tmp;
583 struct mds_file_data *mfd;
584 int rc, size = sizeof(*body);
587 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
588 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_OPEN_PACK)) {
589 CERROR("mds: out of memory\n");
590 req->rq_status = -ENOMEM;
594 mci = mds_uuid_to_mci(mds, ptlrpc_req_to_uuid(req));
596 CERROR("mds: no mci!\n");
597 req->rq_status = -ENOTCONN;
601 body = lustre_msg_buf(req->rq_reqmsg, 0);
603 /* was this animal open already? */
604 /* XXX we should only check on re-open, or do a refcount... */
605 list_for_each(tmp, &mci->mci_open_head) {
606 struct mds_file_data *fd;
607 fd = list_entry(tmp, struct mds_file_data, mfd_list);
608 if (body->extra == fd->mfd_clientfd &&
609 body->fid1.id == fd->mfd_file->f_dentry->d_inode->i_ino) {
610 CERROR("Re opening %Ld\n", body->fid1.id);
615 OBD_ALLOC(mfd, sizeof(*mfd));
617 CERROR("mds: out of memory\n");
618 req->rq_status = -ENOMEM;
622 de = mds_fid2dentry(mds, &body->fid1, &mnt);
624 req->rq_status = -ENOENT;
628 /* check if this inode has seen a delayed object creation */
629 if (req->rq_reqmsg->bufcount > 1) {
631 struct lov_stripe_md *md;
632 struct inode *inode = de->d_inode;
633 //struct iattr iattr;
636 md = lustre_msg_buf(req->rq_reqmsg, 1);
637 //iattr.ia_mode = inode->i_mode;
639 handle = mds_fs_start(mds, de->d_inode, MDS_FSOP_SETATTR);
641 req->rq_status = -ENOMEM;
645 /* XXX error handling */
646 rc = mds_fs_set_md(mds, inode, handle, md);
647 // rc = mds_fs_setattr(mds, de, handle, &iattr);
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 &&
801 req->rq_export == NULL)
802 GOTO(out, rc = -ENOTCONN);
804 if (strcmp(req->rq_obd->obd_type->typ_name, "mds") != 0)
805 GOTO(out, rc = -EINVAL);
807 switch (req->rq_reqmsg->opc) {
809 CDEBUG(D_INODE, "connect\n");
810 OBD_FAIL_RETURN(OBD_FAIL_MDS_CONNECT_NET, 0);
811 rc = target_handle_connect(req);
815 CDEBUG(D_INODE, "disconnect\n");
816 OBD_FAIL_RETURN(OBD_FAIL_MDS_DISCONNECT_NET, 0);
817 rc = target_handle_disconnect(req);
821 CDEBUG(D_INODE, "getstatus\n");
822 OBD_FAIL_RETURN(OBD_FAIL_MDS_GETSTATUS_NET, 0);
823 rc = mds_getstatus(req);
827 CDEBUG(D_INODE, "getlovinfo\n");
828 rc = mds_getlovinfo(req);
832 CDEBUG(D_INODE, "getattr\n");
833 OBD_FAIL_RETURN(OBD_FAIL_MDS_GETATTR_NET, 0);
834 rc = mds_getattr(0, req);
838 CDEBUG(D_INODE, "statfs\n");
839 OBD_FAIL_RETURN(OBD_FAIL_MDS_STATFS_NET, 0);
840 rc = mds_statfs(req);
844 CDEBUG(D_INODE, "readpage\n");
845 OBD_FAIL_RETURN(OBD_FAIL_MDS_READPAGE_NET, 0);
846 rc = mds_readpage(req);
848 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
853 int size = sizeof(struct mds_body);
854 CDEBUG(D_INODE, "reint\n");
855 OBD_FAIL_RETURN(OBD_FAIL_MDS_REINT_NET, 0);
857 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen,
860 rc = req->rq_status = -ENOMEM;
863 rc = mds_reint(0, req);
864 OBD_FAIL_RETURN(OBD_FAIL_MDS_REINT_NET_REP, 0);
869 CDEBUG(D_INODE, "open\n");
870 OBD_FAIL_RETURN(OBD_FAIL_MDS_OPEN_NET, 0);
875 CDEBUG(D_INODE, "close\n");
876 OBD_FAIL_RETURN(OBD_FAIL_MDS_CLOSE_NET, 0);
881 rc = ptlrpc_error(req->rq_svc, req);
888 struct mds_obd *mds = mds_req2mds(req);
889 req->rq_repmsg->last_rcvd = HTON__u64(mds->mds_last_rcvd);
890 req->rq_repmsg->last_committed =
891 HTON__u64(mds->mds_last_committed);
892 CDEBUG(D_INFO, "last_rcvd %Lu, last_committed %Lu, xid %d\n",
893 (unsigned long long)mds->mds_last_rcvd,
894 (unsigned long long)mds->mds_last_committed,
895 cpu_to_le32(req->rq_xid));
898 /* Still not 100% sure whether we should reply with the server
899 * last_rcvd or that of this client. I'm not sure it even makes
900 * a difference on a per-client basis, because last_rcvd is global
901 * and we are not supposed to allow transactions while in recovery.
904 CERROR("mds: processing error %d\n", rc);
905 ptlrpc_error(req->rq_svc, req);
907 CDEBUG(D_NET, "sending reply\n");
908 ptlrpc_reply(req->rq_svc, req);
913 /* Update the server data on disk. This stores the new mount_count and
914 * also the last_rcvd value to disk. If we don't have a clean shutdown,
915 * then the server last_rcvd value may be less than that of the clients.
916 * This will alert us that we may need to do client recovery.
919 int mds_update_server_data(struct mds_obd *mds)
921 struct obd_run_ctxt saved;
922 struct mds_server_data *msd = mds->mds_server_data;
923 struct file *filp = mds->mds_rcvd_filp;
927 msd->msd_last_rcvd = cpu_to_le64(mds->mds_last_rcvd);
928 msd->msd_mount_count = cpu_to_le64(mds->mds_mount_count);
930 CDEBUG(D_SUPER, "MDS mount_count is %Lu, last_rcvd is %Lu\n",
931 (unsigned long long)mds->mds_mount_count,
932 (unsigned long long)mds->mds_last_rcvd);
933 push_ctxt(&saved, &mds->mds_ctxt);
934 rc = lustre_fwrite(filp, (char *)msd, sizeof(*msd), &off);
935 if (rc != sizeof(*msd)) {
936 CERROR("error writing MDS server data: rc = %d\n", rc);
941 rc = fsync_dev(filp->f_dentry->d_inode->i_rdev);
944 CERROR("error flushing MDS server data: rc = %d\n", rc);
949 /* Do recovery actions for the MDS */
950 static int mds_recover(struct obd_device *obddev)
952 struct mds_obd *mds = &obddev->u.mds;
955 /* This happens at the end when recovery is complete */
956 ++mds->mds_mount_count;
957 rc = mds_update_server_data(mds);
963 /* mount the file system (secretly) */
964 static int mds_setup(struct obd_device *obddev, obd_count len, void *buf)
966 struct obd_ioctl_data* data = buf;
967 struct obd_export *export;
968 struct mds_obd *mds = &obddev->u.mds;
969 struct vfsmount *mnt;
974 #ifdef CONFIG_DEV_RDONLY
977 if (!data->ioc_inlbuf1 || !data->ioc_inlbuf2)
978 GOTO(err_dec, rc = -EINVAL);
980 mds->mds_fstype = strdup(data->ioc_inlbuf2);
982 mnt = do_kern_mount(mds->mds_fstype, 0, data->ioc_inlbuf1, NULL);
985 CERROR("do_kern_mount failed: rc = %d\n", rc);
989 mds->mds_sb = mnt->mnt_root->d_inode->i_sb;
991 GOTO(err_put, rc = -ENODEV);
993 mds->mds_max_mdsize = sizeof(struct lov_stripe_md);
994 rc = mds_fs_setup(mds, mnt);
996 CERROR("MDS filesystem method init failed: rc = %d\n", rc);
1000 mds->mds_service = ptlrpc_init_svc(64 * 1024, MDS_REQUEST_PORTAL,
1001 MDC_REPLY_PORTAL, "self",mds_handle);
1002 if (!mds->mds_service) {
1003 CERROR("failed to start service\n");
1004 GOTO(err_fs, rc = -EINVAL);
1007 rc = ptlrpc_start_thread(obddev, mds->mds_service, "lustre_mds");
1009 CERROR("cannot start thread: rc = %d\n", rc);
1014 mds->mds_ldlm_conn = ptlrpc_uuid_to_connection("self");
1015 if (!mds->mds_ldlm_conn) {
1016 mds_cleanup(obddev);
1017 GOTO(err_thread, rc);
1020 obddev->obd_namespace =
1021 ldlm_namespace_new("mds_server", LDLM_NAMESPACE_SERVER);
1022 if (obddev->obd_namespace == NULL) {
1024 mds_cleanup(obddev);
1025 GOTO(err_thread, rc);
1028 mds->mds_local_namespace =
1029 ldlm_namespace_new("mds_client", LDLM_NAMESPACE_CLIENT);
1030 if (mds->mds_local_namespace == NULL) {
1032 mds_cleanup(obddev);
1033 GOTO(err_thread, rc);
1036 OBD_ALLOC(mds->mds_ldlm_client, sizeof(*mds->mds_ldlm_client));
1037 if (mds->mds_ldlm_client == NULL) {
1039 mds_cleanup(obddev);
1040 GOTO(err_thread, rc);
1042 ptlrpc_init_client(NULL, NULL, LDLM_REQUEST_PORTAL, LDLM_REPLY_PORTAL,
1043 mds->mds_ldlm_client);
1044 mds->mds_ldlm_client->cli_target_devno = obddev->obd_minor;
1045 mds->mds_ldlm_client->cli_name = "mds ldlm";
1047 rc = mds_recover(obddev);
1049 GOTO(err_thread, rc);
1051 rc = class_connect(&mds->mds_connh, obddev);
1053 GOTO(err_thread, rc);
1054 export = class_conn2export(&mds->mds_connh);
1057 export->exp_connection = mds->mds_ldlm_conn;
1064 ptlrpc_stop_all_threads(mds->mds_service);
1066 ptlrpc_unregister_service(mds->mds_service);
1068 mds_fs_cleanup(mds);
1071 mntput(mds->mds_vfsmnt);
1075 kfree(mds->mds_fstype);
1081 static int mds_cleanup(struct obd_device * obddev)
1083 struct super_block *sb;
1084 struct mds_obd *mds = &obddev->u.mds;
1087 class_disconnect(&mds->mds_connh);
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(mds->mds_local_namespace);
1118 ldlm_namespace_free(obddev->obd_namespace);
1120 if (mds->mds_ldlm_conn != NULL)
1121 ptlrpc_put_connection(mds->mds_ldlm_conn);
1123 OBD_FREE(mds->mds_ldlm_client, sizeof(*mds->mds_ldlm_client));
1126 #ifdef CONFIG_DEV_RDONLY
1127 dev_clear_rdonly(2);
1129 mds_fs_cleanup(mds);
1135 extern int mds_iocontrol(long cmd, struct lustre_handle *conn,
1136 int len, void *karg, void *uarg);
1138 /* use obd ops to offer management infrastructure */
1139 static struct obd_ops mds_obd_ops = {
1140 o_connect: mds_connect,
1141 o_disconnect: mds_disconnect,
1143 o_cleanup: mds_cleanup,
1144 o_iocontrol: mds_iocontrol
1147 static int __init mds_init(void)
1149 inter_module_register("mds_reint", THIS_MODULE, &mds_reint);
1150 inter_module_register("mds_getattr_name", THIS_MODULE,
1152 class_register_type(&mds_obd_ops, LUSTRE_MDS_NAME);
1156 static void __exit mds_exit(void)
1158 inter_module_unregister("mds_reint");
1159 inter_module_unregister("mds_getattr_name");
1160 class_unregister_type(LUSTRE_MDS_NAME);
1163 MODULE_AUTHOR("Cluster File Systems <info@clusterfs.com>");
1164 MODULE_DESCRIPTION("Lustre Metadata Server (MDS) v0.01");
1165 MODULE_LICENSE("GPL");
1167 module_init(mds_init);
1168 module_exit(mds_exit);