1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
5 * Lustre Metadata Server (mds) request handler
7 * Copyright (c) 2001, 2002 Cluster File Systems, Inc.
8 * Author: Peter Braam <braam@clusterfs.com>
9 * Author: Andreas Dilger <adilger@clusterfs.com>
10 * Author: Phil Schwan <phil@clusterfs.com>
12 * This file is part of Lustre, http://www.lustre.org.
14 * Lustre is free software; you can redistribute it and/or
15 * modify it under the terms of version 2 of the GNU General Public
16 * License as published by the Free Software Foundation.
18 * Lustre is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with Lustre; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 #define DEBUG_SUBSYSTEM S_MDS
31 #include <linux/module.h>
32 #include <linux/lustre_mds.h>
33 #include <linux/lustre_dlm.h>
34 #include <linux/init.h>
35 #include <linux/obd_class.h>
37 extern int mds_get_lovtgts(struct obd_device *obd, int tgt_count,
39 extern int mds_get_lovdesc(struct obd_device *obd, struct lov_desc *desc);
40 extern int mds_update_last_rcvd(struct mds_obd *mds, void *handle,
41 struct ptlrpc_request *req);
42 static int mds_cleanup(struct obd_device * obddev);
44 inline struct mds_obd *mds_req2mds(struct ptlrpc_request *req)
46 return &req->rq_export->exp_obd->u.mds;
49 static int mds_bulk_timeout(void *data)
51 struct ptlrpc_bulk_desc *desc = data;
54 CERROR("(not yet) starting recovery of client %p\n", desc->b_client);
58 /* Assumes caller has already pushed into the kernel filesystem context */
59 static int mds_sendpage(struct ptlrpc_request *req, struct file *file,
63 struct mds_obd *mds = mds_req2mds(req);
64 struct ptlrpc_bulk_desc *desc;
65 struct ptlrpc_bulk_page *bulk;
66 struct l_wait_info lwi;
70 desc = ptlrpc_prep_bulk(req->rq_connection);
72 GOTO(out, rc = -ENOMEM);
74 bulk = ptlrpc_prep_bulk_page(desc);
76 GOTO(cleanup_bulk, rc = -ENOMEM);
78 OBD_ALLOC(buf, PAGE_SIZE);
80 GOTO(cleanup_bulk, rc = -ENOMEM);
82 rc = mds_fs_readpage(mds, file, buf, PAGE_SIZE, (loff_t *)&offset);
85 GOTO(cleanup_buf, rc = -EIO);
87 bulk->b_xid = req->rq_xid;
89 bulk->b_buflen = PAGE_SIZE;
90 desc->b_portal = MDS_BULK_PORTAL;
92 rc = ptlrpc_send_bulk(desc);
94 GOTO(cleanup_buf, rc);
96 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE)) {
97 CERROR("obd_fail_loc=%x, fail operation rc=%d\n",
98 OBD_FAIL_MDS_SENDPAGE, rc);
99 ptlrpc_abort_bulk(desc);
100 GOTO(cleanup_buf, rc);
103 lwi = LWI_TIMEOUT(obd_timeout * HZ, mds_bulk_timeout, desc);
104 rc = l_wait_event(desc->b_waitq, desc->b_flags & PTL_BULK_FL_SENT, &lwi);
106 if (rc != -ETIMEDOUT)
108 GOTO(cleanup_buf, rc);
113 OBD_FREE(buf, PAGE_SIZE);
115 ptlrpc_free_bulk(desc);
121 * Look up a named entry in a directory, and get an LDLM lock on it.
122 * 'dir' is a inode for which an LDLM lock has already been taken.
124 * If we do not need an exclusive or write lock on this entry (e.g.
125 * a read lock for attribute lookup only) then we do not hold the
126 * directory on return. It is up to the caller to know what type
127 * of lock it is getting, and clean up appropriately.
129 struct dentry *mds_name2locked_dentry(struct obd_device *obd,
130 struct dentry *dir, struct vfsmount **mnt,
131 char *name, int namelen, int lock_mode,
132 struct lustre_handle *lockh,
135 struct dentry *dchild;
137 __u64 res_id[3] = {0};
140 down(&dir->d_inode->i_sem);
141 dchild = lookup_one_len(name, dir, namelen);
142 if (IS_ERR(dchild)) {
143 CERROR("child lookup error %ld\n", PTR_ERR(dchild));
144 up(&dir->d_inode->i_sem);
148 if (dir_lock_mode != LCK_EX && dir_lock_mode != LCK_PW) {
149 up(&dir->d_inode->i_sem);
150 ldlm_lock_decref(lockh, dir_lock_mode);
153 if (lock_mode == 0 || !dchild->d_inode)
156 res_id[0] = dchild->d_inode->i_ino;
157 rc = ldlm_match_or_enqueue(NULL, NULL, obd->obd_namespace, NULL,
158 res_id, LDLM_PLAIN, NULL, 0, lock_mode,
159 &flags, ldlm_completion_ast,
160 mds_blocking_ast, NULL, 0, lockh);
161 if (rc != ELDLM_OK) {
163 up(&dir->d_inode->i_sem);
164 RETURN(ERR_PTR(-ENOLCK)); /* XXX translate ldlm code */
170 struct dentry *mds_fid2locked_dentry(struct obd_device *obd, struct ll_fid *fid,
171 struct vfsmount **mnt, int lock_mode,
172 struct lustre_handle *lockh)
174 struct mds_obd *mds = &obd->u.mds;
175 struct dentry *de = mds_fid2dentry(mds, fid, mnt), *retval = de;
177 __u64 res_id[3] = {0};
183 res_id[0] = de->d_inode->i_ino;
184 rc = ldlm_match_or_enqueue(NULL, NULL, obd->obd_namespace, NULL,
185 res_id, LDLM_PLAIN, NULL, 0, lock_mode,
186 &flags, ldlm_completion_ast,
187 mds_blocking_ast, NULL, 0, lockh);
188 if (rc != ELDLM_OK) {
190 retval = ERR_PTR(-ENOLCK); /* XXX translate ldlm code */
196 /* Look up an entry by inode number. */
197 struct dentry *mds_fid2dentry(struct mds_obd *mds, struct ll_fid *fid,
198 struct vfsmount **mnt)
200 /* stolen from NFS */
201 struct super_block *sb = mds->mds_sb;
202 unsigned long ino = fid->id;
203 __u32 generation = fid->generation;
205 struct list_head *lp;
206 struct dentry *result;
209 RETURN(ERR_PTR(-ESTALE));
211 inode = iget(sb, ino);
213 RETURN(ERR_PTR(-ENOMEM));
215 CDEBUG(D_DENTRY, "--> mds_fid2dentry: sb %p\n", inode->i_sb);
217 if (is_bad_inode(inode) ||
218 (generation && inode->i_generation != generation)) {
219 /* we didn't find the right inode.. */
220 CERROR("bad inode %lu, link: %d ct: %d or version %u/%u\n",
221 inode->i_ino, inode->i_nlink,
222 atomic_read(&inode->i_count), inode->i_generation,
226 RETURN(ERR_PTR(-ESTALE));
229 /* now to find a dentry. If possible, get a well-connected one */
231 *mnt = mds->mds_vfsmnt;
232 spin_lock(&dcache_lock);
233 list_for_each(lp, &inode->i_dentry) {
234 result = list_entry(lp, struct dentry, d_alias);
235 if (!(result->d_flags & DCACHE_NFSD_DISCONNECTED)) {
237 result->d_vfs_flags |= DCACHE_REFERENCED;
238 spin_unlock(&dcache_lock);
245 spin_unlock(&dcache_lock);
246 result = d_alloc_root(inode);
247 if (result == NULL) {
249 return ERR_PTR(-ENOMEM);
253 result->d_flags |= DCACHE_NFSD_DISCONNECTED;
257 /* Establish a connection to the MDS.
259 * This will set up an export structure for the client to hold state data
260 * about that client, like open files, the last operation number it did
261 * on the server, etc.
263 static int mds_connect(struct lustre_handle *conn, struct obd_device *obd,
266 struct obd_export *exp;
267 struct mds_client_data *mcd;
272 if (!conn || !obd || !cluuid)
277 list_for_each(p, &obd->obd_exports) {
278 exp = list_entry(p, struct obd_export, exp_chain);
279 mcd = exp->exp_mds_data.med_mcd;
280 if (!memcmp(cluuid, mcd->mcd_uuid, sizeof(mcd->mcd_uuid))) {
281 CDEBUG(D_INFO, "existing export for UUID '%s' at %p\n",
283 LASSERT(exp->exp_obd == obd);
285 exp->exp_rconnh.addr = conn->addr;
286 exp->exp_rconnh.cookie = conn->cookie;
287 conn->addr = (__u64) (unsigned long)exp;
288 conn->cookie = exp->exp_cookie;
289 CDEBUG(D_IOCTL,"connect: addr %Lx cookie %Lx\n",
290 (long long)conn->addr, (long long)conn->cookie);
294 rc = class_connect(conn, obd, cluuid);
297 exp = class_conn2export(conn);
300 OBD_ALLOC(mcd, sizeof(*mcd));
302 CERROR("mds: out of memory for client data\n");
303 GOTO(out_export, rc = -ENOMEM);
305 memcpy(mcd->mcd_uuid, cluuid, sizeof(mcd->mcd_uuid));
306 exp->exp_mds_data.med_mcd = mcd;
307 rc = mds_client_add(&exp->exp_mds_data, -1);
314 OBD_FREE(mcd, sizeof(*mcd));
316 class_disconnect(conn);
323 static int mds_disconnect(struct lustre_handle *conn)
325 struct obd_export *exp;
328 exp = class_conn2export(conn);
332 rc = class_disconnect(conn);
339 static int mds_getstatus(struct ptlrpc_request *req)
341 struct mds_obd *mds = mds_req2mds(req);
342 struct mds_body *body;
343 struct mds_export_data *med = &req->rq_export->exp_mds_data;
344 int rc, size = sizeof(*body);
347 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
348 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_GETSTATUS_PACK)) {
349 CERROR("mds: out of memory for message: size=%d\n", size);
350 req->rq_status = -ENOMEM;
354 body = lustre_msg_buf(req->rq_reqmsg, 0);
355 mds_unpack_body(body);
357 /* Anything we need to do here with the client's trans no or so? */
358 body = lustre_msg_buf(req->rq_repmsg, 0);
359 memcpy(&body->fid1, &mds->mds_rootfid, sizeof(body->fid1));
361 LASSERT(med->med_mcd);
363 /* mcd_last_xid is is stored in little endian on the disk and
364 mds_pack_rep_body converts it to network order */
365 req->rq_repmsg->last_xid = le32_to_cpu(med->med_mcd->mcd_last_xid);
366 mds_pack_rep_body(req);
370 static int mds_getlovinfo(struct ptlrpc_request *req)
372 struct mds_obd *mds = mds_req2mds(req);
373 struct mds_status_req *streq;
374 struct lov_desc *desc;
376 int rc, size[2] = {sizeof(*desc)};
379 streq = lustre_msg_buf(req->rq_reqmsg, 0);
380 streq->flags = NTOH__u32(streq->flags);
381 streq->repbuf = NTOH__u32(streq->repbuf);
382 size[1] = streq->repbuf;
384 rc = lustre_pack_msg(2, size, NULL, &req->rq_replen, &req->rq_repmsg);
386 CERROR("mds: out of memory for message: size=%d\n", size[1]);
387 req->rq_status = -ENOMEM;
391 desc = lustre_msg_buf(req->rq_repmsg, 0);
392 rc = mds_get_lovdesc(req->rq_obd, desc);
394 CERROR("mds_get_lovdesc error %d", rc);
399 tgt_count = NTOH__u32(desc->ld_tgt_count);
400 if (tgt_count * sizeof(uuid_t) > streq->repbuf) {
401 CERROR("too many targets, enlarge client buffers\n");
402 req->rq_status = -ENOSPC;
406 mds->mds_max_mdsize = sizeof(struct lov_mds_md) +
407 tgt_count * sizeof(struct lov_object_id);
408 rc = mds_get_lovtgts(req->rq_obd, tgt_count,
409 lustre_msg_buf(req->rq_repmsg, 1));
411 CERROR("get_lovtgts error %d\n", rc);
418 int mds_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
419 void *data, __u32 data_len)
424 l_lock(&lock->l_resource->lr_namespace->ns_lock);
425 lock->l_flags |= LDLM_FL_CBPENDING;
426 do_ast = (!lock->l_readers && !lock->l_writers);
427 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
430 struct lustre_handle lockh;
433 LDLM_DEBUG(lock, "already unused, calling ldlm_cli_cancel");
434 ldlm_lock2handle(lock, &lockh);
435 rc = ldlm_cli_cancel(&lockh);
437 CERROR("ldlm_cli_cancel: %d\n", rc);
439 LDLM_DEBUG(lock, "Lock still has references, will be"
444 static int mds_getattr_internal(struct mds_obd *mds, struct dentry *dentry,
445 struct ptlrpc_request *req,
446 int request_off, int reply_off)
448 struct mds_body *request_body, *body;
449 struct inode *inode = dentry->d_inode;
456 /* Did the client request the link name? */
457 request_body = lustre_msg_buf(req->rq_reqmsg, request_off);
458 body = lustre_msg_buf(req->rq_repmsg, reply_off);
459 if ((body->valid & OBD_MD_LINKNAME) && S_ISLNK(inode->i_mode)) {
460 char *tmp = lustre_msg_buf(req->rq_repmsg, reply_off + 1);
462 rc = inode->i_op->readlink(dentry, tmp, req->rq_repmsg->
463 buflens[reply_off + 1]);
465 CERROR("readlink failed: %d\n", rc);
469 body->valid |= OBD_MD_LINKNAME;
472 mds_pack_inode2fid(&body->fid1, inode);
473 mds_pack_inode2body(body, inode);
474 if (S_ISREG(inode->i_mode)) {
475 struct lov_mds_md *md;
477 md = lustre_msg_buf(req->rq_repmsg, reply_off + 1);
478 md->lmd_easize = mds->mds_max_mdsize;
479 rc = mds_fs_get_md(mds, inode, md);
484 CERROR("mds_fs_get_md failed: %d\n", rc);
487 body->valid |= OBD_MD_FLEASIZE;
492 static int mds_getattr_name(int offset, struct ptlrpc_request *req)
494 struct mds_obd *mds = mds_req2mds(req);
495 struct obd_device *obd = req->rq_export->exp_obd;
496 struct obd_run_ctxt saved;
497 struct mds_body *body;
498 struct dentry *de = NULL, *dchild = NULL;
500 struct lustre_handle lockh;
502 int namelen, flags, lock_mode, rc = 0, old_offset = offset;
503 __u64 res_id[3] = {0, 0, 0};
506 LASSERT(!strcmp(req->rq_export->exp_obd->obd_type->typ_name, "mds"));
508 if (req->rq_reqmsg->bufcount <= offset + 1) {
510 GOTO(out_pre_de, rc = -EINVAL);
513 body = lustre_msg_buf(req->rq_reqmsg, offset);
514 name = lustre_msg_buf(req->rq_reqmsg, offset + 1);
515 namelen = req->rq_reqmsg->buflens[offset + 1];
516 /* requests were at offset 2, replies go back at 1 */
520 push_ctxt(&saved, &mds->mds_ctxt);
521 de = mds_fid2dentry(mds, &body->fid1, NULL);
524 GOTO(out_pre_de, rc = -ESTALE);
528 CDEBUG(D_INODE, "parent ino %ld, name %*s\n", dir->i_ino,namelen,name);
530 lock_mode = (req->rq_reqmsg->opc == MDS_REINT) ? LCK_CW : LCK_PW;
531 res_id[0] = dir->i_ino;
533 rc = ldlm_lock_match(obd->obd_namespace, res_id, LDLM_PLAIN,
534 NULL, 0, lock_mode, &lockh);
536 LDLM_DEBUG_NOLOCK("enqueue res %Lu", res_id[0]);
537 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace, NULL,
538 res_id, LDLM_PLAIN, NULL, 0, lock_mode,
539 &flags, ldlm_completion_ast,
540 mds_blocking_ast, NULL, 0, &lockh);
541 if (rc != ELDLM_OK) {
542 CERROR("lock enqueue: err: %d\n", rc);
543 GOTO(out_create_de, rc = -EIO);
546 ldlm_lock_dump((void *)(unsigned long)lockh.addr);
549 dchild = lookup_one_len(name, de, namelen - 1);
550 if (IS_ERR(dchild)) {
551 CDEBUG(D_INODE, "child lookup error %ld\n", PTR_ERR(dchild));
554 GOTO(out_create_dchild, rc = -ESTALE);
557 rc = mds_getattr_internal(mds, dchild, req, old_offset, offset);
563 ldlm_lock_decref(&lockh, lock_mode);
572 static int mds_getattr(int offset, struct ptlrpc_request *req)
574 struct mds_obd *mds = mds_req2mds(req);
575 struct obd_run_ctxt saved;
578 struct mds_body *body;
579 int rc = 0, size[2] = {sizeof(*body)}, bufcount = 1;
582 body = lustre_msg_buf(req->rq_reqmsg, offset);
583 push_ctxt(&saved, &mds->mds_ctxt);
584 de = mds_fid2dentry(mds, &body->fid1, NULL);
586 req->rq_status = -ENOENT;
587 GOTO(out_pop, rc = -ENOENT);
591 if (S_ISREG(body->fid1.f_type)) {
593 size[1] = mds->mds_max_mdsize;
594 } else if (body->valid & OBD_MD_LINKNAME) {
596 size[1] = inode->i_size;
599 rc = lustre_pack_msg(bufcount, size, NULL, &req->rq_replen,
601 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK)) {
602 CERROR("out of memory or FAIL_MDS_GETATTR_PACK\n");
607 req->rq_status = mds_getattr_internal(mds, de, req, offset, 0);
616 static int mds_statfs(struct ptlrpc_request *req)
618 struct mds_obd *mds = mds_req2mds(req);
619 struct obd_statfs *osfs;
621 int rc, size = sizeof(*osfs);
624 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen,
626 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_STATFS_PACK)) {
627 CERROR("mds: statfs lustre_pack_msg failed: rc = %d\n", rc);
631 rc = vfs_statfs(mds->mds_sb, &sfs);
633 CERROR("mds: statfs failed: rc %d\n", rc);
636 osfs = lustre_msg_buf(req->rq_repmsg, 0);
637 memset(osfs, 0, size);
638 obd_statfs_pack(osfs, &sfs);
645 static int mds_open(struct ptlrpc_request *req)
648 struct mds_body *body;
650 struct vfsmount *mnt;
651 struct mds_obd *mds = mds_req2mds(req);
652 struct mds_export_data *med;
654 struct list_head *tmp;
655 struct mds_file_data *mfd;
656 int rc, size = sizeof(*body);
659 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
660 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_OPEN_PACK)) {
661 CERROR("mds: out of memory\n");
662 req->rq_status = -ENOMEM;
666 body = lustre_msg_buf(req->rq_reqmsg, 0);
668 /* was this animal open already? */
669 /* XXX we should only check on re-open, or do a refcount... */
670 med = &req->rq_export->exp_mds_data;
671 list_for_each(tmp, &med->med_open_head) {
672 struct mds_file_data *fd;
673 fd = list_entry(tmp, struct mds_file_data, mfd_list);
674 if (body->extra == fd->mfd_clientfd &&
675 body->fid1.id == fd->mfd_file->f_dentry->d_inode->i_ino) {
676 CERROR("Re opening %Ld\n", body->fid1.id);
681 OBD_ALLOC(mfd, sizeof(*mfd));
683 CERROR("mds: out of memory\n");
684 req->rq_status = -ENOMEM;
688 de = mds_fid2dentry(mds, &body->fid1, &mnt);
690 req->rq_status = -ENOENT;
694 /* check if this inode has seen a delayed object creation */
695 if (req->rq_reqmsg->bufcount > 1) {
697 struct lov_mds_md *md;
698 struct inode *inode = de->d_inode;
701 md = lustre_msg_buf(req->rq_reqmsg, 1);
703 handle = mds_fs_start(mds, de->d_inode, MDS_FSOP_SETATTR);
705 req->rq_status = -ENOMEM;
709 /* XXX error handling */
710 rc = mds_fs_set_md(mds, inode, handle, md);
712 struct obd_run_ctxt saved;
713 push_ctxt(&saved, &mds->mds_ctxt);
714 rc = mds_update_last_rcvd(mds, handle, req);
720 /* FIXME: need to return last_rcvd, last_committed */
722 /* FIXME: keep rc intact */
723 rc = mds_fs_commit(mds, de->d_inode, handle);
731 file = dentry_open(de, mnt, flags & ~O_DIRECT);
732 if (!file || IS_ERR(file)) {
733 req->rq_status = -EINVAL;
734 OBD_FREE(mfd, sizeof(*mfd));
738 file->private_data = mfd;
739 mfd->mfd_file = file;
740 mfd->mfd_clientfd = body->extra;
741 list_add(&mfd->mfd_list, &med->med_open_head);
743 body = lustre_msg_buf(req->rq_repmsg, 0);
744 /* FIXME: need to have cookies involved here */
745 body->extra = (__u64) (unsigned long)file;
749 static int mds_close(struct ptlrpc_request *req)
752 struct mds_body *body;
754 struct mds_obd *mds = mds_req2mds(req);
755 struct vfsmount *mnt;
756 struct mds_file_data *mfd;
760 rc = lustre_pack_msg(0, NULL, NULL, &req->rq_replen, &req->rq_repmsg);
761 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_CLOSE_PACK)) {
762 CERROR("mds: out of memory\n");
763 req->rq_status = -ENOMEM;
767 body = lustre_msg_buf(req->rq_reqmsg, 0);
768 de = mds_fid2dentry(mds, &body->fid1, &mnt);
770 req->rq_status = -ENOENT;
774 /* FIXME: need to have cookies involved here */
775 file = (struct file *)(unsigned long)body->extra;
778 mfd = (struct mds_file_data *)file->private_data;
779 list_del(&mfd->mfd_list);
780 OBD_FREE(mfd, sizeof(*mfd));
782 req->rq_status = filp_close(file, 0);
789 static int mds_readpage(struct ptlrpc_request *req)
791 struct mds_obd *mds = mds_req2mds(req);
792 struct vfsmount *mnt;
795 struct mds_body *body;
796 struct obd_run_ctxt saved;
797 int rc, size = sizeof(*body);
800 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
801 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK)) {
802 CERROR("mds: out of memory\n");
803 GOTO(out, rc = -ENOMEM);
806 body = lustre_msg_buf(req->rq_reqmsg, 0);
807 push_ctxt(&saved, &mds->mds_ctxt);
808 de = mds_fid2dentry(mds, &body->fid1, &mnt);
810 GOTO(out_pop, rc = PTR_ERR(de));
812 CDEBUG(D_INODE, "ino %ld\n", de->d_inode->i_ino);
814 file = dentry_open(de, mnt, O_RDONLY | O_LARGEFILE);
815 /* note: in case of an error, dentry_open puts dentry */
817 GOTO(out_pop, rc = PTR_ERR(file));
819 /* to make this asynchronous make sure that the handling function
820 doesn't send a reply when this function completes. Instead a
821 callback function would send the reply */
822 rc = mds_sendpage(req, file, body->size);
832 int mds_reint(int offset, struct ptlrpc_request *req)
835 struct mds_update_record rec;
837 rc = mds_update_unpack(req, offset, &rec);
838 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNPACK)) {
839 CERROR("invalid record\n");
840 req->rq_status = -EINVAL;
843 /* rc will be used to interrupt a for loop over multiple records */
844 rc = mds_reint_rec(&rec, offset, req);
848 int mds_handle(struct ptlrpc_request *req)
853 rc = lustre_unpack_msg(req->rq_reqmsg, req->rq_reqlen);
854 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_HANDLE_UNPACK)) {
855 CERROR("lustre_mds: Invalid request\n");
859 if (req->rq_reqmsg->type != PTL_RPC_MSG_REQUEST) {
860 CERROR("lustre_mds: wrong packet type sent %d\n",
861 req->rq_reqmsg->type);
862 GOTO(out, rc = -EINVAL);
865 if (req->rq_reqmsg->opc != MDS_CONNECT && req->rq_export == NULL)
866 GOTO(out, rc = -ENOTCONN);
868 if (strcmp(req->rq_obd->obd_type->typ_name, "mds") != 0)
869 GOTO(out, rc = -EINVAL);
871 switch (req->rq_reqmsg->opc) {
873 CDEBUG(D_INODE, "connect\n");
874 OBD_FAIL_RETURN(OBD_FAIL_MDS_CONNECT_NET, 0);
875 rc = target_handle_connect(req);
879 CDEBUG(D_INODE, "disconnect\n");
880 OBD_FAIL_RETURN(OBD_FAIL_MDS_DISCONNECT_NET, 0);
881 rc = target_handle_disconnect(req);
885 CDEBUG(D_INODE, "getstatus\n");
886 OBD_FAIL_RETURN(OBD_FAIL_MDS_GETSTATUS_NET, 0);
887 rc = mds_getstatus(req);
891 CDEBUG(D_INODE, "getlovinfo\n");
892 rc = mds_getlovinfo(req);
896 CDEBUG(D_INODE, "getattr\n");
897 OBD_FAIL_RETURN(OBD_FAIL_MDS_GETATTR_NET, 0);
898 rc = mds_getattr(0, req);
902 CDEBUG(D_INODE, "statfs\n");
903 OBD_FAIL_RETURN(OBD_FAIL_MDS_STATFS_NET, 0);
904 rc = mds_statfs(req);
908 CDEBUG(D_INODE, "readpage\n");
909 OBD_FAIL_RETURN(OBD_FAIL_MDS_READPAGE_NET, 0);
910 rc = mds_readpage(req);
912 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
917 int size = sizeof(struct mds_body);
918 CDEBUG(D_INODE, "reint\n");
919 OBD_FAIL_RETURN(OBD_FAIL_MDS_REINT_NET, 0);
921 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen,
927 rc = mds_reint(0, req);
928 OBD_FAIL_RETURN(OBD_FAIL_MDS_REINT_NET_REP, 0);
933 CDEBUG(D_INODE, "open\n");
934 OBD_FAIL_RETURN(OBD_FAIL_MDS_OPEN_NET, 0);
939 CDEBUG(D_INODE, "close\n");
940 OBD_FAIL_RETURN(OBD_FAIL_MDS_CLOSE_NET, 0);
945 CDEBUG(D_INODE, "enqueue\n");
946 OBD_FAIL_RETURN(OBD_FAIL_LDLM_ENQUEUE, 0);
947 rc = ldlm_handle_enqueue(req);
953 CDEBUG(D_INODE, "convert\n");
954 OBD_FAIL_RETURN(OBD_FAIL_LDLM_CONVERT, 0);
955 rc = ldlm_handle_convert(req);
961 CDEBUG(D_INODE, "cancel\n");
962 OBD_FAIL_RETURN(OBD_FAIL_LDLM_CANCEL, 0);
963 rc = ldlm_handle_cancel(req);
967 case LDLM_BL_CALLBACK:
968 case LDLM_CP_CALLBACK:
969 CDEBUG(D_INODE, "callback\n");
970 CERROR("callbacks should not happen on MDS\n");
972 OBD_FAIL_RETURN(OBD_FAIL_LDLM_BL_CALLBACK, 0);
975 rc = ptlrpc_error(req->rq_svc, req);
982 struct mds_obd *mds = mds_req2mds(req);
983 req->rq_repmsg->last_xid = HTON__u64(mds->mds_last_rcvd);
984 req->rq_repmsg->last_committed =
985 HTON__u64(mds->mds_last_committed);
986 CDEBUG(D_INFO, "last_rcvd %Lu, last_committed %Lu, xid %d\n",
987 (unsigned long long)mds->mds_last_rcvd,
988 (unsigned long long)mds->mds_last_committed,
989 cpu_to_le32(req->rq_xid));
992 /* Still not 100% sure whether we should reply with the server
993 * last_rcvd or that of this client. I'm not sure it even makes
994 * a difference on a per-client basis, because last_rcvd is global
995 * and we are not supposed to allow transactions while in recovery.
998 CERROR("mds: processing error %d\n", rc);
999 ptlrpc_error(req->rq_svc, req);
1001 CDEBUG(D_NET, "sending reply\n");
1002 ptlrpc_reply(req->rq_svc, req);
1007 /* Update the server data on disk. This stores the new mount_count and
1008 * also the last_rcvd value to disk. If we don't have a clean shutdown,
1009 * then the server last_rcvd value may be less than that of the clients.
1010 * This will alert us that we may need to do client recovery.
1012 * Assumes we are already in the server filesystem context.
1015 int mds_update_server_data(struct mds_obd *mds)
1017 struct mds_server_data *msd = mds->mds_server_data;
1018 struct file *filp = mds->mds_rcvd_filp;
1022 msd->msd_last_rcvd = cpu_to_le64(mds->mds_last_rcvd);
1023 msd->msd_mount_count = cpu_to_le64(mds->mds_mount_count);
1025 CDEBUG(D_SUPER, "MDS mount_count is %Lu, last_rcvd is %Lu\n",
1026 (unsigned long long)mds->mds_mount_count,
1027 (unsigned long long)mds->mds_last_rcvd);
1028 rc = lustre_fwrite(filp, (char *)msd, sizeof(*msd), &off);
1029 if (rc != sizeof(*msd)) {
1030 CERROR("error writing MDS server data: rc = %d\n", rc);
1035 rc = fsync_dev(filp->f_dentry->d_inode->i_rdev);
1037 CERROR("error flushing MDS server data: rc = %d\n", rc);
1042 /* Do recovery actions for the MDS */
1043 static int mds_recover(struct obd_device *obddev)
1045 struct mds_obd *mds = &obddev->u.mds;
1046 struct obd_run_ctxt saved;
1049 /* This happens at the end when recovery is complete */
1050 ++mds->mds_mount_count;
1051 push_ctxt(&saved, &mds->mds_ctxt);
1052 rc = mds_update_server_data(mds);
1058 #define MDS_NUM_THREADS 8
1059 /* mount the file system (secretly) */
1060 static int mds_setup(struct obd_device *obddev, obd_count len, void *buf)
1063 struct obd_ioctl_data* data = buf;
1064 struct mds_obd *mds = &obddev->u.mds;
1065 struct vfsmount *mnt;
1070 #ifdef CONFIG_DEV_RDONLY
1071 dev_clear_rdonly(2);
1073 if (!data->ioc_inlbuf1 || !data->ioc_inlbuf2)
1074 GOTO(err_dec, rc = -EINVAL);
1076 mds->mds_fstype = strdup(data->ioc_inlbuf2);
1078 mnt = do_kern_mount(mds->mds_fstype, 0, data->ioc_inlbuf1, NULL);
1081 CERROR("do_kern_mount failed: rc = %d\n", rc);
1082 GOTO(err_kfree, rc);
1085 mds->mds_sb = mnt->mnt_root->d_inode->i_sb;
1087 GOTO(err_put, rc = -ENODEV);
1089 mds->mds_max_mdsize = sizeof(struct lov_mds_md);
1090 rc = mds_fs_setup(obddev, mnt);
1092 CERROR("MDS filesystem method init failed: rc = %d\n", rc);
1096 mds->mds_service = ptlrpc_init_svc(64 * 1024, MDS_REQUEST_PORTAL,
1097 MDC_REPLY_PORTAL, "self",mds_handle,
1099 if (!mds->mds_service) {
1100 CERROR("failed to start service\n");
1101 GOTO(err_fs, rc = -EINVAL);
1104 obddev->obd_namespace =
1105 ldlm_namespace_new("mds_server", LDLM_NAMESPACE_SERVER);
1106 if (obddev->obd_namespace == NULL) {
1107 mds_cleanup(obddev);
1108 GOTO(err_svc, rc = -ENOMEM);
1111 for (i = 0; i < MDS_NUM_THREADS; i++) {
1113 sprintf(name, "lustre_MDS_%02d", i);
1114 rc = ptlrpc_start_thread(obddev, mds->mds_service, name);
1116 CERROR("cannot start MDS thread #%d: rc %d\n", i, rc);
1117 GOTO(err_thread, rc);
1121 rc = mds_recover(obddev);
1123 GOTO(err_thread, rc);
1125 mds_destroy_export = mds_client_free;
1130 ptlrpc_stop_all_threads(mds->mds_service);
1132 ptlrpc_unregister_service(mds->mds_service);
1134 mds_fs_cleanup(obddev);
1137 mntput(mds->mds_vfsmnt);
1141 kfree(mds->mds_fstype);
1147 static int mds_cleanup(struct obd_device *obddev)
1149 struct super_block *sb;
1150 struct mds_obd *mds = &obddev->u.mds;
1151 struct obd_run_ctxt saved;
1154 ptlrpc_stop_all_threads(mds->mds_service);
1155 ptlrpc_unregister_service(mds->mds_service);
1161 push_ctxt(&saved, &mds->mds_ctxt);
1162 mds_update_server_data(mds);
1164 if (mds->mds_rcvd_filp) {
1165 int rc = filp_close(mds->mds_rcvd_filp, 0);
1166 mds->mds_rcvd_filp = NULL;
1169 CERROR("last_rcvd file won't close, rc=%d\n", rc);
1174 mntput(mds->mds_vfsmnt);
1176 kfree(mds->mds_fstype);
1178 ldlm_namespace_free(obddev->obd_namespace);
1181 #ifdef CONFIG_DEV_RDONLY
1182 dev_clear_rdonly(2);
1184 mds_fs_cleanup(obddev);
1190 static int ldlm_intent_policy(struct ldlm_lock *lock, void *req_cookie,
1191 ldlm_mode_t mode, void *data)
1193 struct ptlrpc_request *req = req_cookie;
1200 if (req->rq_reqmsg->bufcount > 1) {
1201 /* an intent needs to be considered */
1202 struct ldlm_intent *it = lustre_msg_buf(req->rq_reqmsg, 1);
1203 struct mds_obd *mds= &req->rq_export->exp_obd->u.mds;
1204 struct mds_body *mds_rep;
1205 struct ldlm_reply *rep;
1206 __u64 new_resid[3] = {0, 0, 0}, old_res;
1207 int rc, size[3] = {sizeof(struct ldlm_reply),
1208 sizeof(struct mds_body),
1209 mds->mds_max_mdsize};
1211 it->opc = NTOH__u64(it->opc);
1213 LDLM_DEBUG(lock, "intent policy, opc: %s",
1214 ldlm_it2str(it->opc));
1216 rc = lustre_pack_msg(3, size, NULL, &req->rq_replen,
1219 rc = req->rq_status = -ENOMEM;
1223 rep = lustre_msg_buf(req->rq_repmsg, 0);
1224 rep->lock_policy_res1 = 1;
1226 /* execute policy */
1227 switch ((long)it->opc) {
1228 case IT_CREAT|IT_OPEN:
1229 rc = mds_reint(2, req);
1230 if (rc || (req->rq_status != 0 &&
1231 req->rq_status != -EEXIST)) {
1232 rep->lock_policy_res2 = req->rq_status;
1233 RETURN(ELDLM_LOCK_ABORTED);
1244 rc = mds_reint(2, req);
1245 if (rc || (req->rq_status != 0 &&
1246 req->rq_status != -EISDIR &&
1247 req->rq_status != -ENOTDIR)) {
1248 rep->lock_policy_res2 = req->rq_status;
1249 RETURN(ELDLM_LOCK_ABORTED);
1259 rc = mds_getattr_name(2, req);
1260 /* FIXME: we need to sit down and decide on who should
1261 * set req->rq_status, who should return negative and
1262 * positive return values, and what they all mean. */
1263 if (rc || req->rq_status != 0) {
1264 rep->lock_policy_res2 = req->rq_status;
1265 RETURN(ELDLM_LOCK_ABORTED);
1268 case IT_READDIR|IT_OPEN:
1272 CERROR("Unhandled intent\n");
1276 if (it->opc == IT_UNLINK || it->opc == IT_RMDIR ||
1277 it->opc == IT_RENAME || it->opc == IT_RENAME2)
1278 RETURN(ELDLM_LOCK_ABORTED);
1280 rep->lock_policy_res2 = req->rq_status;
1281 mds_rep = lustre_msg_buf(req->rq_repmsg, 1);
1283 /* If the client is about to open a file that doesn't have an MD
1284 * stripe record, it's going to need a write lock. */
1285 if (it->opc & IT_OPEN) {
1286 struct lov_mds_md *md =
1287 lustre_msg_buf(req->rq_repmsg, 2);
1288 if (md->lmd_easize == 0) {
1289 LDLM_DEBUG(lock, "open with no EA; returning PW"
1291 lock->l_req_mode = LCK_PW;
1295 /* Give the client a lock on the child object, instead of the
1296 * parent that it requested. */
1297 new_resid[0] = NTOH__u32(mds_rep->ino);
1298 if (new_resid[0] == 0)
1300 old_res = lock->l_resource->lr_name[0];
1302 ldlm_lock_change_resource(lock, new_resid);
1303 if (lock->l_resource == NULL) {
1307 LDLM_DEBUG(lock, "intent policy, old res %ld",
1309 RETURN(ELDLM_LOCK_CHANGED);
1311 int size = sizeof(struct ldlm_reply);
1312 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen,
1323 extern int mds_iocontrol(long cmd, struct lustre_handle *conn,
1324 int len, void *karg, void *uarg);
1326 /* use obd ops to offer management infrastructure */
1327 static struct obd_ops mds_obd_ops = {
1328 o_connect: mds_connect,
1329 o_disconnect: mds_disconnect,
1331 o_cleanup: mds_cleanup,
1332 o_iocontrol: mds_iocontrol
1335 static int __init mds_init(void)
1337 class_register_type(&mds_obd_ops, LUSTRE_MDS_NAME);
1338 ldlm_register_intent(ldlm_intent_policy);
1342 static void __exit mds_exit(void)
1344 ldlm_unregister_intent();
1345 class_unregister_type(LUSTRE_MDS_NAME);
1348 MODULE_AUTHOR("Cluster File Systems <info@clusterfs.com>");
1349 MODULE_DESCRIPTION("Lustre Metadata Server (MDS) v0.01");
1350 MODULE_LICENSE("GPL");
1352 module_init(mds_init);
1353 module_exit(mds_exit);