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,
98 struct dentry *dir, struct vfsmount **mnt,
99 char *name, 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,
128 mds_blocking_ast, NULL, 0, lockh);
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,
155 mds_blocking_ast, NULL, 0, lockh);
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,
228 struct list_head *p, *n;
232 list_for_each_safe(p, n, &obd->obd_exports) {
233 struct obd_export *exp;
234 struct mds_client_data *mcd;
236 exp = list_entry(p, struct obd_export, exp_chain);
237 mcd = exp->exp_mds_data.med_mcd;
238 if (mcd && !memcmp(cluuid, mcd->mcd_uuid,
239 sizeof(mcd->mcd_uuid))) {
241 "existing export for UUID '%s' at %p\n",
243 if (exp->exp_obd != obd)
245 exp->exp_rconnh.addr = conn->addr;
246 exp->exp_rconnh.cookie = conn->cookie;
247 conn->addr = (__u64) (unsigned long)exp;
248 conn->cookie = exp->exp_cookie;
249 CDEBUG(D_IOCTL, "connect: addr %Lx cookie %Lx\n",
250 (long long)conn->addr,
251 (long long)conn->cookie);
256 rc = class_connect(conn, obd, NULL);
264 static int mds_disconnect(struct lustre_handle *conn)
268 rc = class_disconnect(conn);
275 /* FIXME: the error cases need fixing to avoid leaks */
276 static int mds_getstatus(struct ptlrpc_request *req)
278 struct mds_obd *mds = mds_req2mds(req);
279 struct mds_body *body;
280 struct mds_client_data *mcd;
281 struct mds_export_data *med = &req->rq_export->exp_mds_data;
282 int rc, size = sizeof(*body);
285 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
286 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_GETSTATUS_PACK)) {
287 CERROR("mds: out of memory for message: size=%d\n", size);
288 req->rq_status = -ENOMEM;
292 body = lustre_msg_buf(req->rq_reqmsg, 0);
293 mds_unpack_body(body);
295 /* Anything we need to do here with the client's trans no or so? */
296 body = lustre_msg_buf(req->rq_repmsg, 0);
297 memcpy(&body->fid1, &mds->mds_rootfid, sizeof(body->fid1));
303 CDEBUG(D_INFO, "allocating new client data for UUID '%s'\n",
304 ptlrpc_req_to_uuid(req));
305 OBD_ALLOC(mcd, sizeof(*mcd));
307 CERROR("mds: out of memory for client data\n");
308 req->rq_status = -ENOMEM;
311 memcpy(mcd->mcd_uuid, ptlrpc_req_to_uuid(req),
312 sizeof(mcd->mcd_uuid));
313 rc = mds_client_add(mds, med, -1);
316 OBD_FREE(mcd, sizeof(*mcd));
321 CDEBUG(D_INFO, "found existing data for UUID '%s' at #%d\n",
322 mcd->mcd_uuid, med->med_off);
324 /* mcd_last_xid is is stored in little endian on the disk and
325 mds_pack_rep_body converts it to network order */
326 body->last_xid = le32_to_cpu(mcd->mcd_last_xid);
327 mds_pack_rep_body(req);
331 static int mds_getlovinfo(struct ptlrpc_request *req)
333 struct mds_obd *mds = mds_req2mds(req);
334 struct mds_status_req *streq;
335 struct lov_desc *desc;
337 int rc, size[2] = {sizeof(*desc)};
340 streq = lustre_msg_buf(req->rq_reqmsg, 0);
341 streq->flags = NTOH__u32(streq->flags);
342 streq->repbuf = NTOH__u32(streq->repbuf);
343 size[1] = streq->repbuf;
345 rc = lustre_pack_msg(2, size, NULL, &req->rq_replen, &req->rq_repmsg);
347 CERROR("mds: out of memory for message: size=%d\n", size[1]);
348 req->rq_status = -ENOMEM;
352 desc = lustre_msg_buf(req->rq_repmsg, 0);
353 rc = mds_get_lovdesc(req->rq_obd, desc);
355 CERROR("mds_get_lovdesc error %d", rc);
360 tgt_count = NTOH__u32(desc->ld_tgt_count);
361 if (tgt_count * sizeof(uuid_t) > streq->repbuf) {
362 CERROR("too many targets, enlarge client buffers\n");
363 req->rq_status = -ENOSPC;
367 mds->mds_max_mdsize = sizeof(desc) + tgt_count * sizeof(uuid_t);
368 rc = mds_get_lovtgts(req->rq_obd, tgt_count,
369 lustre_msg_buf(req->rq_repmsg, 1));
371 CERROR("get_lovtgts error %d\n", rc);
378 int mds_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
379 void *data, __u32 data_len)
381 struct lustre_handle lockh;
384 ldlm_lock2handle(lock, &lockh);
385 if (ldlm_cli_cancel(&lockh) < 0)
390 static int mds_getattr_name(int offset, struct ptlrpc_request *req)
392 struct mds_obd *mds = mds_req2mds(req);
393 struct obd_device *obd = req->rq_export->exp_obd;
394 struct obd_run_ctxt saved;
395 struct mds_body *body;
396 struct dentry *de = NULL, *dchild = NULL;
398 struct lustre_handle lockh;
400 int namelen, flags, lock_mode, rc = 0;
401 __u64 res_id[3] = {0, 0, 0};
404 if (strcmp(req->rq_export->exp_obd->obd_type->typ_name, "mds") != 0)
407 if (req->rq_reqmsg->bufcount <= offset + 1) {
409 GOTO(out_pre_de, rc = -EINVAL);
412 body = lustre_msg_buf(req->rq_reqmsg, offset);
413 name = lustre_msg_buf(req->rq_reqmsg, offset + 1);
414 namelen = req->rq_reqmsg->buflens[offset + 1];
415 /* requests were at offset 2, replies go back at 1 */
419 push_ctxt(&saved, &mds->mds_ctxt);
420 de = mds_fid2dentry(mds, &body->fid1, NULL);
423 GOTO(out_pre_de, rc = -ESTALE);
427 CDEBUG(D_INODE, "parent ino %ld\n", dir->i_ino);
429 lock_mode = (req->rq_reqmsg->opc == MDS_REINT) ? LCK_CW : LCK_PW;
430 res_id[0] = dir->i_ino;
432 rc = ldlm_lock_match(obd->obd_namespace, res_id, LDLM_PLAIN,
433 NULL, 0, lock_mode, &lockh);
435 LDLM_DEBUG_NOLOCK("enqueue res %Lu", res_id[0]);
436 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace, NULL,
437 res_id, LDLM_PLAIN, NULL, 0, lock_mode,
438 &flags, ldlm_completion_ast,
439 mds_blocking_ast, NULL, 0, &lockh);
440 if (rc != ELDLM_OK) {
441 CERROR("lock enqueue: err: %d\n", rc);
442 GOTO(out_create_de, rc = -EIO);
445 ldlm_lock_dump((void *)(unsigned long)lockh.addr);
448 dchild = lookup_one_len(name, de, namelen - 1);
449 if (IS_ERR(dchild)) {
450 CDEBUG(D_INODE, "child lookup error %ld\n", PTR_ERR(dchild));
453 GOTO(out_create_dchild, rc = -ESTALE);
456 if (dchild->d_inode) {
457 struct mds_body *body;
458 struct inode *inode = dchild->d_inode;
459 CDEBUG(D_INODE, "child exists (dir %ld, name %s, ino %ld)\n",
460 dir->i_ino, name, dchild->d_inode->i_ino);
462 body = lustre_msg_buf(req->rq_repmsg, offset);
463 mds_pack_inode2fid(&body->fid1, inode);
464 mds_pack_inode2body(body, inode);
465 if (S_ISREG(inode->i_mode)) {
466 struct lov_stripe_md *md;
467 md = lustre_msg_buf(req->rq_repmsg, offset + 1);
468 md->lmd_easize = mds->mds_max_mdsize;
469 mds_fs_get_md(mds, inode, md);
471 /* now a normal case for intent locking */
480 ldlm_lock_decref(&lockh, lock_mode);
490 static int mds_getattr(int offset, struct ptlrpc_request *req)
492 struct mds_obd *mds = mds_req2mds(req);
493 struct obd_run_ctxt saved;
496 struct mds_body *body;
497 int rc, size[2] = {sizeof(*body)}, bufcount = 1;
500 body = lustre_msg_buf(req->rq_reqmsg, offset);
501 push_ctxt(&saved, &mds->mds_ctxt);
502 de = mds_fid2dentry(mds, &body->fid1, NULL);
504 GOTO(out_pop, rc = -ENOENT);
508 if (S_ISREG(body->fid1.f_type)) {
510 size[1] = mds->mds_max_mdsize;
511 } else if (body->valid & OBD_MD_LINKNAME) {
513 size[1] = inode->i_size;
516 rc = lustre_pack_msg(bufcount, size, NULL, &req->rq_replen,
518 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK)) {
519 CERROR("mds: out of memory\n");
523 if (body->valid & OBD_MD_LINKNAME) {
524 char *tmp = lustre_msg_buf(req->rq_repmsg, 1);
526 rc = inode->i_op->readlink(de, tmp, size[1]);
529 CERROR("readlink failed: %d\n", rc);
534 body = lustre_msg_buf(req->rq_repmsg, 0);
535 body->ino = inode->i_ino;
536 body->generation = inode->i_generation;
537 body->atime = inode->i_atime;
538 body->ctime = inode->i_ctime;
539 body->mtime = inode->i_mtime;
540 body->uid = inode->i_uid;
541 body->gid = inode->i_gid;
542 body->size = inode->i_size;
543 body->mode = inode->i_mode;
544 body->nlink = inode->i_nlink;
545 body->valid = (OBD_MD_FLID | OBD_MD_FLATIME | OBD_MD_FLCTIME |
546 OBD_MD_FLMTIME | OBD_MD_FLSIZE | OBD_MD_FLUID |
547 OBD_MD_FLGID | OBD_MD_FLNLINK | OBD_MD_FLGENER |
550 if (S_ISREG(inode->i_mode)) {
551 rc = mds_fs_get_md(mds, inode,
552 lustre_msg_buf(req->rq_repmsg, 1));
554 CERROR("mds_fs_get_md failed: %d\n", rc);
557 body->valid |= OBD_MD_FLEASIZE;
558 } else if (S_ISLNK(inode->i_mode))
559 body->valid |= OBD_MD_LINKNAME;
569 static int mds_statfs(struct ptlrpc_request *req)
571 struct mds_obd *mds = mds_req2mds(req);
572 struct obd_statfs *osfs;
574 int rc, size = sizeof(*osfs);
577 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen,
579 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_STATFS_PACK)) {
580 CERROR("mds: statfs lustre_pack_msg failed: rc = %d\n", rc);
584 rc = vfs_statfs(mds->mds_sb, &sfs);
586 CERROR("mds: statfs failed: rc %d\n", rc);
589 osfs = lustre_msg_buf(req->rq_repmsg, 0);
590 memset(osfs, 0, size);
591 obd_statfs_pack(osfs, &sfs);
598 static int mds_open(struct ptlrpc_request *req)
601 struct mds_body *body;
603 struct vfsmount *mnt;
604 struct mds_obd *mds = mds_req2mds(req);
605 struct mds_export_data *med;
607 struct list_head *tmp;
608 struct mds_file_data *mfd;
609 int rc, size = sizeof(*body);
612 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
613 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_OPEN_PACK)) {
614 CERROR("mds: out of memory\n");
615 req->rq_status = -ENOMEM;
619 body = lustre_msg_buf(req->rq_reqmsg, 0);
621 /* was this animal open already? */
622 /* XXX we should only check on re-open, or do a refcount... */
623 med = &req->rq_export->exp_mds_data;
624 list_for_each(tmp, &med->med_open_head) {
625 struct mds_file_data *fd;
626 fd = list_entry(tmp, struct mds_file_data, mfd_list);
627 if (body->extra == fd->mfd_clientfd &&
628 body->fid1.id == fd->mfd_file->f_dentry->d_inode->i_ino) {
629 CERROR("Re opening %Ld\n", body->fid1.id);
634 OBD_ALLOC(mfd, sizeof(*mfd));
636 CERROR("mds: out of memory\n");
637 req->rq_status = -ENOMEM;
641 de = mds_fid2dentry(mds, &body->fid1, &mnt);
643 req->rq_status = -ENOENT;
647 /* check if this inode has seen a delayed object creation */
648 if (req->rq_reqmsg->bufcount > 1) {
650 struct lov_stripe_md *md;
651 struct inode *inode = de->d_inode;
654 md = lustre_msg_buf(req->rq_reqmsg, 1);
656 handle = mds_fs_start(mds, de->d_inode, MDS_FSOP_SETATTR);
658 req->rq_status = -ENOMEM;
662 /* XXX error handling */
663 rc = mds_fs_set_md(mds, inode, handle, md);
665 struct obd_run_ctxt saved;
666 push_ctxt(&saved, &mds->mds_ctxt);
667 rc = mds_update_last_rcvd(mds, handle, req);
673 /* FIXME: need to return last_rcvd, last_committed */
675 /* FIXME: keep rc intact */
676 rc = mds_fs_commit(mds, de->d_inode, handle);
684 file = dentry_open(de, mnt, flags & ~O_DIRECT);
685 if (!file || IS_ERR(file)) {
686 req->rq_status = -EINVAL;
687 OBD_FREE(mfd, sizeof(*mfd));
691 file->private_data = mfd;
692 mfd->mfd_file = file;
693 mfd->mfd_clientfd = body->extra;
694 list_add(&mfd->mfd_list, &med->med_open_head);
696 body = lustre_msg_buf(req->rq_repmsg, 0);
697 body->extra = (__u64) (unsigned long)file;
701 static int mds_close(struct ptlrpc_request *req)
704 struct mds_body *body;
706 struct mds_obd *mds = mds_req2mds(req);
707 struct vfsmount *mnt;
708 struct mds_file_data *mfd;
712 rc = lustre_pack_msg(0, NULL, NULL, &req->rq_replen, &req->rq_repmsg);
713 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_CLOSE_PACK)) {
714 CERROR("mds: out of memory\n");
715 req->rq_status = -ENOMEM;
719 body = lustre_msg_buf(req->rq_reqmsg, 0);
720 de = mds_fid2dentry(mds, &body->fid1, &mnt);
722 req->rq_status = -ENOENT;
726 file = (struct file *)(unsigned long)body->extra;
729 mfd = (struct mds_file_data *)file->private_data;
730 list_del(&mfd->mfd_list);
731 OBD_FREE(mfd, sizeof(*mfd));
733 req->rq_status = filp_close(file, 0);
740 static int mds_readpage(struct ptlrpc_request *req)
742 struct mds_obd *mds = mds_req2mds(req);
743 struct vfsmount *mnt;
746 struct mds_body *body;
747 struct obd_run_ctxt saved;
748 int rc, size = sizeof(*body);
751 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
752 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK)) {
753 CERROR("mds: out of memory\n");
754 GOTO(out, rc = -ENOMEM);
757 body = lustre_msg_buf(req->rq_reqmsg, 0);
758 push_ctxt(&saved, &mds->mds_ctxt);
759 de = mds_fid2dentry(mds, &body->fid1, &mnt);
761 GOTO(out_pop, rc = PTR_ERR(de));
763 CDEBUG(D_INODE, "ino %ld\n", de->d_inode->i_ino);
765 file = dentry_open(de, mnt, O_RDONLY | O_LARGEFILE);
766 /* note: in case of an error, dentry_open puts dentry */
768 GOTO(out_pop, rc = PTR_ERR(file));
770 /* to make this asynchronous make sure that the handling function
771 doesn't send a reply when this function completes. Instead a
772 callback function would send the reply */
773 rc = mds_sendpage(req, file, body->size);
783 int mds_reint(int offset, struct ptlrpc_request *req)
786 struct mds_update_record rec;
788 rc = mds_update_unpack(req, offset, &rec);
789 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNPACK)) {
790 CERROR("invalid record\n");
791 req->rq_status = -EINVAL;
794 /* rc will be used to interrupt a for loop over multiple records */
795 rc = mds_reint_rec(&rec, offset, req);
799 int mds_handle(struct ptlrpc_request *req)
804 rc = lustre_unpack_msg(req->rq_reqmsg, req->rq_reqlen);
805 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_HANDLE_UNPACK)) {
806 CERROR("lustre_mds: Invalid request\n");
810 if (req->rq_reqmsg->type != PTL_RPC_MSG_REQUEST) {
811 CERROR("lustre_mds: wrong packet type sent %d\n",
812 req->rq_reqmsg->type);
813 GOTO(out, rc = -EINVAL);
816 if (req->rq_reqmsg->opc != MDS_CONNECT && req->rq_export == NULL)
817 GOTO(out, rc = -ENOTCONN);
819 if (strcmp(req->rq_obd->obd_type->typ_name, "mds") != 0)
820 GOTO(out, rc = -EINVAL);
822 switch (req->rq_reqmsg->opc) {
824 CDEBUG(D_INODE, "connect\n");
825 OBD_FAIL_RETURN(OBD_FAIL_MDS_CONNECT_NET, 0);
826 rc = target_handle_connect(req);
830 CDEBUG(D_INODE, "disconnect\n");
831 OBD_FAIL_RETURN(OBD_FAIL_MDS_DISCONNECT_NET, 0);
832 rc = target_handle_disconnect(req);
836 CDEBUG(D_INODE, "getstatus\n");
837 OBD_FAIL_RETURN(OBD_FAIL_MDS_GETSTATUS_NET, 0);
838 rc = mds_getstatus(req);
842 CDEBUG(D_INODE, "getlovinfo\n");
843 rc = mds_getlovinfo(req);
847 CDEBUG(D_INODE, "getattr\n");
848 OBD_FAIL_RETURN(OBD_FAIL_MDS_GETATTR_NET, 0);
849 rc = mds_getattr(0, req);
853 CDEBUG(D_INODE, "statfs\n");
854 OBD_FAIL_RETURN(OBD_FAIL_MDS_STATFS_NET, 0);
855 rc = mds_statfs(req);
859 CDEBUG(D_INODE, "readpage\n");
860 OBD_FAIL_RETURN(OBD_FAIL_MDS_READPAGE_NET, 0);
861 rc = mds_readpage(req);
863 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
868 int size = sizeof(struct mds_body);
869 CDEBUG(D_INODE, "reint\n");
870 OBD_FAIL_RETURN(OBD_FAIL_MDS_REINT_NET, 0);
872 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen,
875 rc = req->rq_status = -ENOMEM;
878 rc = mds_reint(0, req);
879 OBD_FAIL_RETURN(OBD_FAIL_MDS_REINT_NET_REP, 0);
884 CDEBUG(D_INODE, "open\n");
885 OBD_FAIL_RETURN(OBD_FAIL_MDS_OPEN_NET, 0);
890 CDEBUG(D_INODE, "close\n");
891 OBD_FAIL_RETURN(OBD_FAIL_MDS_CLOSE_NET, 0);
896 CDEBUG(D_INODE, "enqueue\n");
897 OBD_FAIL_RETURN(OBD_FAIL_LDLM_ENQUEUE, 0);
898 rc = ldlm_handle_enqueue(req);
904 CDEBUG(D_INODE, "convert\n");
905 OBD_FAIL_RETURN(OBD_FAIL_LDLM_CONVERT, 0);
906 rc = ldlm_handle_convert(req);
912 CDEBUG(D_INODE, "cancel\n");
913 OBD_FAIL_RETURN(OBD_FAIL_LDLM_CANCEL, 0);
914 rc = ldlm_handle_cancel(req);
918 case LDLM_BL_CALLBACK:
919 case LDLM_CP_CALLBACK:
920 CDEBUG(D_INODE, "callback\n");
921 CERROR("callbacks should not happen on MDS\n");
923 OBD_FAIL_RETURN(OBD_FAIL_LDLM_BL_CALLBACK, 0);
926 rc = ptlrpc_error(req->rq_svc, req);
933 struct mds_obd *mds = mds_req2mds(req);
934 req->rq_repmsg->last_rcvd = HTON__u64(mds->mds_last_rcvd);
935 req->rq_repmsg->last_committed =
936 HTON__u64(mds->mds_last_committed);
937 CDEBUG(D_INFO, "last_rcvd %Lu, last_committed %Lu, xid %d\n",
938 (unsigned long long)mds->mds_last_rcvd,
939 (unsigned long long)mds->mds_last_committed,
940 cpu_to_le32(req->rq_xid));
943 /* Still not 100% sure whether we should reply with the server
944 * last_rcvd or that of this client. I'm not sure it even makes
945 * a difference on a per-client basis, because last_rcvd is global
946 * and we are not supposed to allow transactions while in recovery.
949 CERROR("mds: processing error %d\n", rc);
950 ptlrpc_error(req->rq_svc, req);
952 CDEBUG(D_NET, "sending reply\n");
953 ptlrpc_reply(req->rq_svc, req);
958 /* Update the server data on disk. This stores the new mount_count and
959 * also the last_rcvd value to disk. If we don't have a clean shutdown,
960 * then the server last_rcvd value may be less than that of the clients.
961 * This will alert us that we may need to do client recovery.
964 int mds_update_server_data(struct mds_obd *mds)
966 struct obd_run_ctxt saved;
967 struct mds_server_data *msd = mds->mds_server_data;
968 struct file *filp = mds->mds_rcvd_filp;
972 msd->msd_last_rcvd = cpu_to_le64(mds->mds_last_rcvd);
973 msd->msd_mount_count = cpu_to_le64(mds->mds_mount_count);
975 CDEBUG(D_SUPER, "MDS mount_count is %Lu, last_rcvd is %Lu\n",
976 (unsigned long long)mds->mds_mount_count,
977 (unsigned long long)mds->mds_last_rcvd);
978 push_ctxt(&saved, &mds->mds_ctxt);
979 rc = lustre_fwrite(filp, (char *)msd, sizeof(*msd), &off);
980 if (rc != sizeof(*msd)) {
981 CERROR("error writing MDS server data: rc = %d\n", rc);
986 rc = fsync_dev(filp->f_dentry->d_inode->i_rdev);
989 CERROR("error flushing MDS server data: rc = %d\n", rc);
994 /* Do recovery actions for the MDS */
995 static int mds_recover(struct obd_device *obddev)
997 struct mds_obd *mds = &obddev->u.mds;
1000 /* This happens at the end when recovery is complete */
1001 ++mds->mds_mount_count;
1002 rc = mds_update_server_data(mds);
1007 #define MDS_NUM_THREADS 8
1008 /* mount the file system (secretly) */
1009 static int mds_setup(struct obd_device *obddev, obd_count len, void *buf)
1012 struct obd_ioctl_data* data = buf;
1013 struct mds_obd *mds = &obddev->u.mds;
1014 struct vfsmount *mnt;
1019 #ifdef CONFIG_DEV_RDONLY
1020 dev_clear_rdonly(2);
1022 if (!data->ioc_inlbuf1 || !data->ioc_inlbuf2)
1023 GOTO(err_dec, rc = -EINVAL);
1025 mds->mds_fstype = strdup(data->ioc_inlbuf2);
1027 mnt = do_kern_mount(mds->mds_fstype, 0, data->ioc_inlbuf1, NULL);
1030 CERROR("do_kern_mount failed: rc = %d\n", rc);
1031 GOTO(err_kfree, rc);
1034 mds->mds_sb = mnt->mnt_root->d_inode->i_sb;
1036 GOTO(err_put, rc = -ENODEV);
1038 mds->mds_max_mdsize = sizeof(struct lov_stripe_md);
1039 rc = mds_fs_setup(obddev, mnt);
1041 CERROR("MDS filesystem method init failed: rc = %d\n", rc);
1045 mds->mds_service = ptlrpc_init_svc(64 * 1024, MDS_REQUEST_PORTAL,
1046 MDC_REPLY_PORTAL, "self",mds_handle);
1047 if (!mds->mds_service) {
1048 CERROR("failed to start service\n");
1049 GOTO(err_fs, rc = -EINVAL);
1053 obddev->obd_namespace =
1054 ldlm_namespace_new("mds_server", LDLM_NAMESPACE_SERVER);
1055 if (obddev->obd_namespace == NULL) {
1057 mds_cleanup(obddev);
1061 for (i = 0; i < MDS_NUM_THREADS; i++) {
1063 sprintf(name, "lustre_MDS_%02d", i);
1064 rc = ptlrpc_start_thread(obddev, mds->mds_service, name);
1066 CERROR("cannot start MDS thread #%d: rc %d\n", i, rc);
1068 GOTO(err_thread, rc);
1072 rc = mds_recover(obddev);
1074 GOTO(err_thread, rc);
1079 ptlrpc_stop_all_threads(mds->mds_service);
1081 ptlrpc_unregister_service(mds->mds_service);
1083 mds_fs_cleanup(obddev);
1086 mntput(mds->mds_vfsmnt);
1090 kfree(mds->mds_fstype);
1096 static int mds_cleanup(struct obd_device *obddev)
1098 struct super_block *sb;
1099 struct mds_obd *mds = &obddev->u.mds;
1103 if (!list_empty(&obddev->obd_exports)) {
1104 CERROR("still has exports; forcing cleanup\n");
1105 class_disconnect_all(obddev);
1106 if (!list_empty(&obddev->obd_exports)) {
1107 CERROR("still has exports after forced cleanup?\n");
1112 ptlrpc_stop_all_threads(mds->mds_service);
1113 ptlrpc_unregister_service(mds->mds_service);
1119 mds_update_server_data(mds);
1121 if (mds->mds_rcvd_filp) {
1122 int rc = filp_close(mds->mds_rcvd_filp, 0);
1123 mds->mds_rcvd_filp = NULL;
1126 CERROR("last_rcvd file won't close, rc=%d\n", rc);
1130 mntput(mds->mds_vfsmnt);
1132 kfree(mds->mds_fstype);
1134 ldlm_namespace_free(obddev->obd_namespace);
1137 #ifdef CONFIG_DEV_RDONLY
1138 dev_clear_rdonly(2);
1140 mds_fs_cleanup(obddev);
1146 static int ldlm_intent_policy(struct ldlm_lock *lock, void *req_cookie,
1147 ldlm_mode_t mode, void *data)
1149 struct ptlrpc_request *req = req_cookie;
1156 if (req->rq_reqmsg->bufcount > 1) {
1157 /* an intent needs to be considered */
1158 struct ldlm_intent *it = lustre_msg_buf(req->rq_reqmsg, 1);
1159 struct mds_obd *mds= &req->rq_export->exp_obd->u.mds;
1160 struct mds_body *mds_rep;
1161 struct ldlm_reply *rep;
1162 __u64 new_resid[3] = {0, 0, 0}, old_res;
1163 int bufcount = -1, rc, size[3] = {sizeof(struct ldlm_reply),
1164 sizeof(struct mds_body),
1165 mds->mds_max_mdsize};
1167 it->opc = NTOH__u64(it->opc);
1169 LDLM_DEBUG(lock, "intent policy, opc: %s",
1170 ldlm_it2str(it->opc));
1173 switch((long)it->opc) {
1175 /* Note that in the negative case you may be returning
1176 * a file and its obdo */
1178 case IT_CREAT|IT_OPEN:
1199 rc = lustre_pack_msg(bufcount, size, NULL, &req->rq_replen,
1202 rc = req->rq_status = -ENOMEM;
1206 rep = lustre_msg_buf(req->rq_repmsg, 0);
1207 rep->lock_policy_res1 = 1;
1209 /* execute policy */
1210 switch ((long)it->opc) {
1211 case IT_CREAT|IT_OPEN:
1212 rc = mds_reint(2, req);
1213 if (rc || (req->rq_status != 0 &&
1214 req->rq_status != -EEXIST)) {
1215 rep->lock_policy_res2 = req->rq_status;
1216 RETURN(ELDLM_LOCK_ABORTED);
1227 rc = mds_reint(2, req);
1228 if (rc || req->rq_status != 0) {
1229 rep->lock_policy_res2 = req->rq_status;
1230 RETURN(ELDLM_LOCK_ABORTED);
1240 rc = mds_getattr_name(2, req);
1241 /* FIXME: we need to sit down and decide on who should
1242 * set req->rq_status, who should return negative and
1243 * positive return values, and what they all mean. */
1244 if (rc || req->rq_status != 0) {
1245 rep->lock_policy_res2 = req->rq_status;
1246 RETURN(ELDLM_LOCK_ABORTED);
1249 case IT_READDIR|IT_OPEN:
1253 CERROR("Unhandled intent\n");
1257 if (it->opc == IT_UNLINK || it->opc == IT_RMDIR ||
1258 it->opc == IT_RENAME || it->opc == IT_RENAME2)
1259 RETURN(ELDLM_LOCK_ABORTED);
1261 rep->lock_policy_res2 = req->rq_status;
1262 mds_rep = lustre_msg_buf(req->rq_repmsg, 1);
1263 new_resid[0] = NTOH__u32(mds_rep->ino);
1264 if (new_resid[0] == 0)
1266 old_res = lock->l_resource->lr_name[0];
1268 CDEBUG(D_INFO, "remote intent: locking %d instead of"
1269 "%ld\n", mds_rep->ino, (long)old_res);
1271 ldlm_lock_change_resource(lock, new_resid);
1272 if (lock->l_resource == NULL) {
1276 LDLM_DEBUG(lock, "intent policy, old res %ld",
1278 RETURN(ELDLM_LOCK_CHANGED);
1280 int size = sizeof(struct ldlm_reply);
1281 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen,
1284 CERROR("out of memory\n");
1293 extern int mds_iocontrol(long cmd, struct lustre_handle *conn,
1294 int len, void *karg, void *uarg);
1296 /* use obd ops to offer management infrastructure */
1297 static struct obd_ops mds_obd_ops = {
1298 o_connect: mds_connect,
1299 o_disconnect: mds_disconnect,
1301 o_cleanup: mds_cleanup,
1302 o_iocontrol: mds_iocontrol
1305 static int __init mds_init(void)
1307 class_register_type(&mds_obd_ops, LUSTRE_MDS_NAME);
1308 ldlm_register_intent(ldlm_intent_policy);
1312 static void __exit mds_exit(void)
1314 ldlm_unregister_intent();
1315 class_unregister_type(LUSTRE_MDS_NAME);
1318 MODULE_AUTHOR("Cluster File Systems <info@clusterfs.com>");
1319 MODULE_DESCRIPTION("Lustre Metadata Server (MDS) v0.01");
1320 MODULE_LICENSE("GPL");
1322 module_init(mds_init);
1323 module_exit(mds_exit);