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>
36 extern int mds_get_lovtgts(struct obd_device *obd, int tgt_count,
38 extern int mds_get_lovdesc(struct obd_device *obd, struct lov_desc *desc);
39 extern int mds_update_last_rcvd(struct mds_obd *mds, void *handle,
40 struct ptlrpc_request *req);
41 static int mds_cleanup(struct obd_device * obddev);
43 inline struct mds_obd *mds_req2mds(struct ptlrpc_request *req)
45 return &req->rq_export->exp_obd->u.mds;
49 /* Assumes caller has already pushed into the kernel filesystem context */
50 static int mds_sendpage(struct ptlrpc_request *req, struct file *file,
54 struct mds_obd *mds = mds_req2mds(req);
55 struct ptlrpc_bulk_desc *desc;
56 struct ptlrpc_bulk_page *bulk;
60 desc = ptlrpc_prep_bulk(req->rq_connection);
62 GOTO(out, rc = -ENOMEM);
64 bulk = ptlrpc_prep_bulk_page(desc);
66 GOTO(cleanup_bulk, rc = -ENOMEM);
68 OBD_ALLOC(buf, PAGE_SIZE);
70 GOTO(cleanup_bulk, rc = -ENOMEM);
72 rc = mds_fs_readpage(mds, file, buf, PAGE_SIZE, (loff_t *)&offset);
75 GOTO(cleanup_buf, rc = -EIO);
77 bulk->b_xid = req->rq_xid;
79 bulk->b_buflen = PAGE_SIZE;
80 desc->b_portal = MDS_BULK_PORTAL;
82 rc = ptlrpc_send_bulk(desc);
84 GOTO(cleanup_buf, rc);
86 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE)) {
87 CERROR("obd_fail_loc=%x, fail operation rc=%d\n",
88 OBD_FAIL_MDS_SENDPAGE, rc);
89 ptlrpc_abort_bulk(desc);
90 GOTO(cleanup_buf, rc);
93 wait_event(desc->b_waitq, ptlrpc_check_bulk_sent(desc));
94 if (desc->b_flags & PTL_RPC_FL_INTR)
95 GOTO(cleanup_buf, rc = -EINTR);
99 OBD_FREE(buf, PAGE_SIZE);
101 ptlrpc_free_bulk(desc);
107 * Look up a named entry in a directory, and get an LDLM lock on it.
108 * 'dir' is a inode for which an LDLM lock has already been taken.
110 * If we do not need an exclusive or write lock on this entry (e.g.
111 * a read lock for attribute lookup only) then we do not hold the
112 * directory on return. It is up to the caller to know what type
113 * of lock it is getting, and clean up appropriately.
115 struct dentry *mds_name2locked_dentry(struct obd_device *obd,
116 struct dentry *dir, struct vfsmount **mnt,
117 char *name, int namelen, int lock_mode,
118 struct lustre_handle *lockh,
121 struct dentry *dchild;
123 __u64 res_id[3] = {0};
126 down(&dir->d_inode->i_sem);
127 dchild = lookup_one_len(name, dir, namelen);
128 if (IS_ERR(dchild)) {
129 CERROR("child lookup error %ld\n", PTR_ERR(dchild));
130 up(&dir->d_inode->i_sem);
134 if (dir_lock_mode != LCK_EX && dir_lock_mode != LCK_PW) {
135 up(&dir->d_inode->i_sem);
136 ldlm_lock_decref(lockh, dir_lock_mode);
139 if (lock_mode == 0 || !dchild->d_inode)
142 res_id[0] = dchild->d_inode->i_ino;
143 rc = ldlm_match_or_enqueue(NULL, NULL, obd->obd_namespace, NULL,
144 res_id, LDLM_PLAIN, NULL, 0, lock_mode,
145 &flags, ldlm_completion_ast,
146 mds_blocking_ast, NULL, 0, lockh);
147 if (rc != ELDLM_OK) {
149 up(&dir->d_inode->i_sem);
150 RETURN(ERR_PTR(-ENOLCK)); /* XXX translate ldlm code */
156 struct dentry *mds_fid2locked_dentry(struct obd_device *obd, struct ll_fid *fid,
157 struct vfsmount **mnt, int lock_mode,
158 struct lustre_handle *lockh)
160 struct mds_obd *mds = &obd->u.mds;
161 struct dentry *de = mds_fid2dentry(mds, fid, mnt), *retval = de;
163 __u64 res_id[3] = {0};
169 res_id[0] = de->d_inode->i_ino;
170 rc = ldlm_match_or_enqueue(NULL, NULL, obd->obd_namespace, NULL,
171 res_id, LDLM_PLAIN, NULL, 0, lock_mode,
172 &flags, ldlm_completion_ast,
173 mds_blocking_ast, NULL, 0, lockh);
174 if (rc != ELDLM_OK) {
176 retval = ERR_PTR(-ENOLCK); /* XXX translate ldlm code */
182 /* Look up an entry by inode number. */
183 struct dentry *mds_fid2dentry(struct mds_obd *mds, struct ll_fid *fid,
184 struct vfsmount **mnt)
186 /* stolen from NFS */
187 struct super_block *sb = mds->mds_sb;
188 unsigned long ino = fid->id;
189 __u32 generation = fid->generation;
191 struct list_head *lp;
192 struct dentry *result;
195 RETURN(ERR_PTR(-ESTALE));
197 inode = iget(sb, ino);
199 RETURN(ERR_PTR(-ENOMEM));
201 CDEBUG(D_DENTRY, "--> mds_fid2dentry: sb %p\n", inode->i_sb);
203 if (is_bad_inode(inode) ||
204 (generation && inode->i_generation != generation)) {
205 /* we didn't find the right inode.. */
206 CERROR("bad inode %lu, link: %d ct: %d or version %u/%u\n",
207 inode->i_ino, inode->i_nlink,
208 atomic_read(&inode->i_count), inode->i_generation,
212 RETURN(ERR_PTR(-ESTALE));
215 /* now to find a dentry. If possible, get a well-connected one */
217 *mnt = mds->mds_vfsmnt;
218 spin_lock(&dcache_lock);
219 for (lp = inode->i_dentry.next; lp != &inode->i_dentry ; lp=lp->next) {
220 result = list_entry(lp,struct dentry, d_alias);
221 if (! (result->d_flags & DCACHE_NFSD_DISCONNECTED)) {
223 result->d_vfs_flags |= DCACHE_REFERENCED;
224 spin_unlock(&dcache_lock);
231 spin_unlock(&dcache_lock);
232 result = d_alloc_root(inode);
233 if (result == NULL) {
235 return ERR_PTR(-ENOMEM);
239 result->d_flags |= DCACHE_NFSD_DISCONNECTED;
243 /* Establish a connection to the MDS.
245 * This will set up an export structure for the client to hold state data
246 * about that client, like open files, the last operation number it did
247 * on the server, etc.
249 static int mds_connect(struct lustre_handle *conn, struct obd_device *obd,
252 struct obd_export *exp;
253 struct mds_client_data *mcd;
262 list_for_each(p, &obd->obd_exports) {
263 exp = list_entry(p, struct obd_export, exp_chain);
264 mcd = exp->exp_mds_data.med_mcd;
265 if (mcd && !memcmp(cluuid, mcd->mcd_uuid,
266 sizeof(mcd->mcd_uuid))) {
267 CDEBUG(D_INFO, "existing export for UUID '%s' at %p\n",
269 LASSERT(exp->exp_obd == obd);
270 exp->exp_rconnh.addr = conn->addr;
271 exp->exp_rconnh.cookie = conn->cookie;
272 conn->addr = (__u64) (unsigned long)exp;
273 conn->cookie = exp->exp_cookie;
274 CDEBUG(D_IOCTL,"connect: addr %Lx cookie %Lx\n",
275 (long long)conn->addr, (long long)conn->cookie);
279 #warning shaver: we might need a real cluuid here
280 rc = class_connect(conn, obd, NULL);
283 exp = class_conn2export(conn);
286 OBD_ALLOC(mcd, sizeof(*mcd));
288 CERROR("mds: out of memory for client data\n");
289 GOTO(out_export, rc = -ENOMEM);
291 memcpy(mcd->mcd_uuid, cluuid, sizeof(mcd->mcd_uuid));
292 exp->exp_mds_data.med_mcd = mcd;
293 rc = mds_client_add(&exp->exp_mds_data, -1);
300 OBD_FREE(mcd, sizeof(*mcd));
302 class_disconnect(conn);
309 static int mds_disconnect(struct lustre_handle *conn)
311 struct obd_export *exp;
314 exp = class_conn2export(conn);
318 rc = mds_client_free(&exp->exp_mds_data);
320 CERROR("error freeing client data: rc = %d\n", rc);
322 rc = class_disconnect(conn);
329 static int mds_getstatus(struct ptlrpc_request *req)
331 struct mds_obd *mds = mds_req2mds(req);
332 struct mds_body *body;
333 struct mds_export_data *med = &req->rq_export->exp_mds_data;
334 int rc, size = sizeof(*body);
337 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
338 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_GETSTATUS_PACK)) {
339 CERROR("mds: out of memory for message: size=%d\n", size);
340 req->rq_status = -ENOMEM;
344 body = lustre_msg_buf(req->rq_reqmsg, 0);
345 mds_unpack_body(body);
347 /* Anything we need to do here with the client's trans no or so? */
348 body = lustre_msg_buf(req->rq_repmsg, 0);
349 memcpy(&body->fid1, &mds->mds_rootfid, sizeof(body->fid1));
351 LASSERT(med->med_mcd);
353 /* mcd_last_xid is is stored in little endian on the disk and
354 mds_pack_rep_body converts it to network order */
355 req->rq_repmsg->last_xid = le32_to_cpu(med->med_mcd->mcd_last_xid);
356 mds_pack_rep_body(req);
360 static int mds_getlovinfo(struct ptlrpc_request *req)
362 struct mds_obd *mds = mds_req2mds(req);
363 struct mds_status_req *streq;
364 struct lov_desc *desc;
366 int rc, size[2] = {sizeof(*desc)};
369 streq = lustre_msg_buf(req->rq_reqmsg, 0);
370 streq->flags = NTOH__u32(streq->flags);
371 streq->repbuf = NTOH__u32(streq->repbuf);
372 size[1] = streq->repbuf;
374 rc = lustre_pack_msg(2, size, NULL, &req->rq_replen, &req->rq_repmsg);
376 CERROR("mds: out of memory for message: size=%d\n", size[1]);
377 req->rq_status = -ENOMEM;
381 desc = lustre_msg_buf(req->rq_repmsg, 0);
382 rc = mds_get_lovdesc(req->rq_obd, desc);
384 CERROR("mds_get_lovdesc error %d", rc);
389 tgt_count = NTOH__u32(desc->ld_tgt_count);
390 if (tgt_count * sizeof(uuid_t) > streq->repbuf) {
391 CERROR("too many targets, enlarge client buffers\n");
392 req->rq_status = -ENOSPC;
396 mds->mds_max_mdsize = sizeof(desc) + tgt_count * sizeof(uuid_t);
397 rc = mds_get_lovtgts(req->rq_obd, tgt_count,
398 lustre_msg_buf(req->rq_repmsg, 1));
400 CERROR("get_lovtgts error %d\n", rc);
407 int mds_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
408 void *data, __u32 data_len)
410 struct lustre_handle lockh;
413 ldlm_lock2handle(lock, &lockh);
414 if (ldlm_cli_cancel(&lockh) < 0)
419 static int mds_getattr_name(int offset, struct ptlrpc_request *req)
421 struct mds_obd *mds = mds_req2mds(req);
422 struct obd_device *obd = req->rq_export->exp_obd;
423 struct obd_run_ctxt saved;
424 struct mds_body *body;
425 struct dentry *de = NULL, *dchild = NULL;
427 struct lustre_handle lockh;
429 int namelen, flags, lock_mode, rc = 0;
430 __u64 res_id[3] = {0, 0, 0};
433 LASSERT(!strcmp(req->rq_export->exp_obd->obd_type->typ_name, "mds"));
435 if (req->rq_reqmsg->bufcount <= offset + 1) {
437 GOTO(out_pre_de, rc = -EINVAL);
440 body = lustre_msg_buf(req->rq_reqmsg, offset);
441 name = lustre_msg_buf(req->rq_reqmsg, offset + 1);
442 namelen = req->rq_reqmsg->buflens[offset + 1];
443 /* requests were at offset 2, replies go back at 1 */
447 push_ctxt(&saved, &mds->mds_ctxt);
448 de = mds_fid2dentry(mds, &body->fid1, NULL);
451 GOTO(out_pre_de, rc = -ESTALE);
455 CDEBUG(D_INODE, "parent ino %ld, name %*s\n", dir->i_ino,namelen,name);
457 lock_mode = (req->rq_reqmsg->opc == MDS_REINT) ? LCK_CW : LCK_PW;
458 res_id[0] = dir->i_ino;
460 rc = ldlm_lock_match(obd->obd_namespace, res_id, LDLM_PLAIN,
461 NULL, 0, lock_mode, &lockh);
463 LDLM_DEBUG_NOLOCK("enqueue res %Lu", res_id[0]);
464 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace, NULL,
465 res_id, LDLM_PLAIN, NULL, 0, lock_mode,
466 &flags, ldlm_completion_ast,
467 mds_blocking_ast, NULL, 0, &lockh);
468 if (rc != ELDLM_OK) {
469 CERROR("lock enqueue: err: %d\n", rc);
470 GOTO(out_create_de, rc = -EIO);
473 ldlm_lock_dump((void *)(unsigned long)lockh.addr);
476 dchild = lookup_one_len(name, de, namelen - 1);
477 if (IS_ERR(dchild)) {
478 CDEBUG(D_INODE, "child lookup error %ld\n", PTR_ERR(dchild));
481 GOTO(out_create_dchild, rc = -ESTALE);
484 if (dchild->d_inode) {
485 struct mds_body *body;
486 struct inode *inode = dchild->d_inode;
487 CDEBUG(D_INODE, "child exists (dir %ld, name %s, ino %ld)\n",
488 dir->i_ino, name, dchild->d_inode->i_ino);
490 body = lustre_msg_buf(req->rq_repmsg, offset);
491 mds_pack_inode2fid(&body->fid1, inode);
492 mds_pack_inode2body(body, inode);
493 if (S_ISREG(inode->i_mode)) {
494 struct lov_stripe_md *md;
495 md = lustre_msg_buf(req->rq_repmsg, offset + 1);
496 md->lmd_easize = mds->mds_max_mdsize;
497 /* FIXME: why don't we check (or use) rc of get_md? */
498 mds_fs_get_md(mds, inode, md);
500 /* now a normal case for intent locking */
509 ldlm_lock_decref(&lockh, lock_mode);
519 static int mds_getattr(int offset, struct ptlrpc_request *req)
521 struct mds_obd *mds = mds_req2mds(req);
522 struct obd_run_ctxt saved;
525 struct mds_body *body;
526 int rc, size[2] = {sizeof(*body)}, bufcount = 1;
529 body = lustre_msg_buf(req->rq_reqmsg, offset);
530 push_ctxt(&saved, &mds->mds_ctxt);
531 de = mds_fid2dentry(mds, &body->fid1, NULL);
533 GOTO(out_pop, rc = -ENOENT);
537 if (S_ISREG(body->fid1.f_type)) {
539 size[1] = mds->mds_max_mdsize;
540 } else if (body->valid & OBD_MD_LINKNAME) {
542 size[1] = inode->i_size;
545 rc = lustre_pack_msg(bufcount, size, NULL, &req->rq_replen,
547 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK)) {
548 CERROR("mds: out of memory\n");
552 if (body->valid & OBD_MD_LINKNAME) {
553 char *tmp = lustre_msg_buf(req->rq_repmsg, 1);
555 rc = inode->i_op->readlink(de, tmp, size[1]);
558 CERROR("readlink failed: %d\n", rc);
563 body = lustre_msg_buf(req->rq_repmsg, 0);
564 body->ino = inode->i_ino;
565 body->generation = inode->i_generation;
566 body->atime = inode->i_atime;
567 body->ctime = inode->i_ctime;
568 body->mtime = inode->i_mtime;
569 body->uid = inode->i_uid;
570 body->gid = inode->i_gid;
571 body->size = inode->i_size;
572 body->mode = inode->i_mode;
573 body->nlink = inode->i_nlink;
574 body->valid = (OBD_MD_FLID | OBD_MD_FLATIME | OBD_MD_FLCTIME |
575 OBD_MD_FLMTIME | OBD_MD_FLSIZE | OBD_MD_FLUID |
576 OBD_MD_FLGID | OBD_MD_FLNLINK | OBD_MD_FLGENER |
579 if (S_ISREG(inode->i_mode)) {
580 rc = mds_fs_get_md(mds, inode,
581 lustre_msg_buf(req->rq_repmsg, 1));
583 CERROR("mds_fs_get_md failed: %d\n", rc);
586 body->valid |= OBD_MD_FLEASIZE;
587 } else if (S_ISLNK(inode->i_mode))
588 body->valid |= OBD_MD_LINKNAME;
598 static int mds_statfs(struct ptlrpc_request *req)
600 struct mds_obd *mds = mds_req2mds(req);
601 struct obd_statfs *osfs;
603 int rc, size = sizeof(*osfs);
606 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen,
608 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_STATFS_PACK)) {
609 CERROR("mds: statfs lustre_pack_msg failed: rc = %d\n", rc);
613 rc = vfs_statfs(mds->mds_sb, &sfs);
615 CERROR("mds: statfs failed: rc %d\n", rc);
618 osfs = lustre_msg_buf(req->rq_repmsg, 0);
619 memset(osfs, 0, size);
620 obd_statfs_pack(osfs, &sfs);
627 static int mds_open(struct ptlrpc_request *req)
630 struct mds_body *body;
632 struct vfsmount *mnt;
633 struct mds_obd *mds = mds_req2mds(req);
634 struct mds_export_data *med;
636 struct list_head *tmp;
637 struct mds_file_data *mfd;
638 int rc, size = sizeof(*body);
641 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
642 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_OPEN_PACK)) {
643 CERROR("mds: out of memory\n");
644 req->rq_status = -ENOMEM;
648 body = lustre_msg_buf(req->rq_reqmsg, 0);
650 /* was this animal open already? */
651 /* XXX we should only check on re-open, or do a refcount... */
652 med = &req->rq_export->exp_mds_data;
653 list_for_each(tmp, &med->med_open_head) {
654 struct mds_file_data *fd;
655 fd = list_entry(tmp, struct mds_file_data, mfd_list);
656 if (body->extra == fd->mfd_clientfd &&
657 body->fid1.id == fd->mfd_file->f_dentry->d_inode->i_ino) {
658 CERROR("Re opening %Ld\n", body->fid1.id);
663 OBD_ALLOC(mfd, sizeof(*mfd));
665 CERROR("mds: out of memory\n");
666 req->rq_status = -ENOMEM;
670 de = mds_fid2dentry(mds, &body->fid1, &mnt);
672 req->rq_status = -ENOENT;
676 /* check if this inode has seen a delayed object creation */
677 if (req->rq_reqmsg->bufcount > 1) {
679 struct lov_stripe_md *md;
680 struct inode *inode = de->d_inode;
683 md = lustre_msg_buf(req->rq_reqmsg, 1);
685 handle = mds_fs_start(mds, de->d_inode, MDS_FSOP_SETATTR);
687 req->rq_status = -ENOMEM;
691 /* XXX error handling */
692 rc = mds_fs_set_md(mds, inode, handle, md);
694 struct obd_run_ctxt saved;
695 push_ctxt(&saved, &mds->mds_ctxt);
696 rc = mds_update_last_rcvd(mds, handle, req);
702 /* FIXME: need to return last_rcvd, last_committed */
704 /* FIXME: keep rc intact */
705 rc = mds_fs_commit(mds, de->d_inode, handle);
713 file = dentry_open(de, mnt, flags & ~O_DIRECT);
714 if (!file || IS_ERR(file)) {
715 req->rq_status = -EINVAL;
716 OBD_FREE(mfd, sizeof(*mfd));
720 file->private_data = mfd;
721 mfd->mfd_file = file;
722 mfd->mfd_clientfd = body->extra;
723 list_add(&mfd->mfd_list, &med->med_open_head);
725 body = lustre_msg_buf(req->rq_repmsg, 0);
726 /* FIXME: need to have cookies involved here */
727 body->extra = (__u64) (unsigned long)file;
731 static int mds_close(struct ptlrpc_request *req)
734 struct mds_body *body;
736 struct mds_obd *mds = mds_req2mds(req);
737 struct vfsmount *mnt;
738 struct mds_file_data *mfd;
742 rc = lustre_pack_msg(0, NULL, NULL, &req->rq_replen, &req->rq_repmsg);
743 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_CLOSE_PACK)) {
744 CERROR("mds: out of memory\n");
745 req->rq_status = -ENOMEM;
749 body = lustre_msg_buf(req->rq_reqmsg, 0);
750 de = mds_fid2dentry(mds, &body->fid1, &mnt);
752 req->rq_status = -ENOENT;
756 /* FIXME: need to have cookies involved here */
757 file = (struct file *)(unsigned long)body->extra;
760 mfd = (struct mds_file_data *)file->private_data;
761 list_del(&mfd->mfd_list);
762 OBD_FREE(mfd, sizeof(*mfd));
764 req->rq_status = filp_close(file, 0);
771 static int mds_readpage(struct ptlrpc_request *req)
773 struct mds_obd *mds = mds_req2mds(req);
774 struct vfsmount *mnt;
777 struct mds_body *body;
778 struct obd_run_ctxt saved;
779 int rc, size = sizeof(*body);
782 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
783 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK)) {
784 CERROR("mds: out of memory\n");
785 GOTO(out, rc = -ENOMEM);
788 body = lustre_msg_buf(req->rq_reqmsg, 0);
789 push_ctxt(&saved, &mds->mds_ctxt);
790 de = mds_fid2dentry(mds, &body->fid1, &mnt);
792 GOTO(out_pop, rc = PTR_ERR(de));
794 CDEBUG(D_INODE, "ino %ld\n", de->d_inode->i_ino);
796 file = dentry_open(de, mnt, O_RDONLY | O_LARGEFILE);
797 /* note: in case of an error, dentry_open puts dentry */
799 GOTO(out_pop, rc = PTR_ERR(file));
801 /* to make this asynchronous make sure that the handling function
802 doesn't send a reply when this function completes. Instead a
803 callback function would send the reply */
804 rc = mds_sendpage(req, file, body->size);
814 int mds_reint(int offset, struct ptlrpc_request *req)
817 struct mds_update_record rec;
819 rc = mds_update_unpack(req, offset, &rec);
820 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNPACK)) {
821 CERROR("invalid record\n");
822 req->rq_status = -EINVAL;
825 /* rc will be used to interrupt a for loop over multiple records */
826 rc = mds_reint_rec(&rec, offset, req);
830 int mds_handle(struct ptlrpc_request *req)
835 rc = lustre_unpack_msg(req->rq_reqmsg, req->rq_reqlen);
836 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_HANDLE_UNPACK)) {
837 CERROR("lustre_mds: Invalid request\n");
841 if (req->rq_reqmsg->type != PTL_RPC_MSG_REQUEST) {
842 CERROR("lustre_mds: wrong packet type sent %d\n",
843 req->rq_reqmsg->type);
844 GOTO(out, rc = -EINVAL);
847 if (req->rq_reqmsg->opc != MDS_CONNECT && req->rq_export == NULL)
848 GOTO(out, rc = -ENOTCONN);
850 if (strcmp(req->rq_obd->obd_type->typ_name, "mds") != 0)
851 GOTO(out, rc = -EINVAL);
853 switch (req->rq_reqmsg->opc) {
855 CDEBUG(D_INODE, "connect\n");
856 OBD_FAIL_RETURN(OBD_FAIL_MDS_CONNECT_NET, 0);
857 rc = target_handle_connect(req);
861 CDEBUG(D_INODE, "disconnect\n");
862 OBD_FAIL_RETURN(OBD_FAIL_MDS_DISCONNECT_NET, 0);
863 rc = target_handle_disconnect(req);
867 CDEBUG(D_INODE, "getstatus\n");
868 OBD_FAIL_RETURN(OBD_FAIL_MDS_GETSTATUS_NET, 0);
869 rc = mds_getstatus(req);
873 CDEBUG(D_INODE, "getlovinfo\n");
874 rc = mds_getlovinfo(req);
878 CDEBUG(D_INODE, "getattr\n");
879 OBD_FAIL_RETURN(OBD_FAIL_MDS_GETATTR_NET, 0);
880 rc = mds_getattr(0, req);
884 CDEBUG(D_INODE, "statfs\n");
885 OBD_FAIL_RETURN(OBD_FAIL_MDS_STATFS_NET, 0);
886 rc = mds_statfs(req);
890 CDEBUG(D_INODE, "readpage\n");
891 OBD_FAIL_RETURN(OBD_FAIL_MDS_READPAGE_NET, 0);
892 rc = mds_readpage(req);
894 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
899 int size = sizeof(struct mds_body);
900 CDEBUG(D_INODE, "reint\n");
901 OBD_FAIL_RETURN(OBD_FAIL_MDS_REINT_NET, 0);
903 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen,
909 rc = mds_reint(0, req);
910 OBD_FAIL_RETURN(OBD_FAIL_MDS_REINT_NET_REP, 0);
915 CDEBUG(D_INODE, "open\n");
916 OBD_FAIL_RETURN(OBD_FAIL_MDS_OPEN_NET, 0);
921 CDEBUG(D_INODE, "close\n");
922 OBD_FAIL_RETURN(OBD_FAIL_MDS_CLOSE_NET, 0);
927 CDEBUG(D_INODE, "enqueue\n");
928 OBD_FAIL_RETURN(OBD_FAIL_LDLM_ENQUEUE, 0);
929 rc = ldlm_handle_enqueue(req);
935 CDEBUG(D_INODE, "convert\n");
936 OBD_FAIL_RETURN(OBD_FAIL_LDLM_CONVERT, 0);
937 rc = ldlm_handle_convert(req);
943 CDEBUG(D_INODE, "cancel\n");
944 OBD_FAIL_RETURN(OBD_FAIL_LDLM_CANCEL, 0);
945 rc = ldlm_handle_cancel(req);
949 case LDLM_BL_CALLBACK:
950 case LDLM_CP_CALLBACK:
951 CDEBUG(D_INODE, "callback\n");
952 CERROR("callbacks should not happen on MDS\n");
954 OBD_FAIL_RETURN(OBD_FAIL_LDLM_BL_CALLBACK, 0);
957 rc = ptlrpc_error(req->rq_svc, req);
964 struct mds_obd *mds = mds_req2mds(req);
965 req->rq_repmsg->last_xid = HTON__u64(mds->mds_last_rcvd);
966 req->rq_repmsg->last_committed =
967 HTON__u64(mds->mds_last_committed);
968 CDEBUG(D_INFO, "last_rcvd %Lu, last_committed %Lu, xid %d\n",
969 (unsigned long long)mds->mds_last_rcvd,
970 (unsigned long long)mds->mds_last_committed,
971 cpu_to_le32(req->rq_xid));
974 /* Still not 100% sure whether we should reply with the server
975 * last_rcvd or that of this client. I'm not sure it even makes
976 * a difference on a per-client basis, because last_rcvd is global
977 * and we are not supposed to allow transactions while in recovery.
980 CERROR("mds: processing error %d\n", rc);
981 ptlrpc_error(req->rq_svc, req);
983 CDEBUG(D_NET, "sending reply\n");
984 ptlrpc_reply(req->rq_svc, req);
989 /* Update the server data on disk. This stores the new mount_count and
990 * also the last_rcvd value to disk. If we don't have a clean shutdown,
991 * then the server last_rcvd value may be less than that of the clients.
992 * This will alert us that we may need to do client recovery.
994 * Assumes we are already in the server filesystem context.
997 int mds_update_server_data(struct mds_obd *mds)
999 struct mds_server_data *msd = mds->mds_server_data;
1000 struct file *filp = mds->mds_rcvd_filp;
1004 msd->msd_last_rcvd = cpu_to_le64(mds->mds_last_rcvd);
1005 msd->msd_mount_count = cpu_to_le64(mds->mds_mount_count);
1007 CDEBUG(D_SUPER, "MDS mount_count is %Lu, last_rcvd is %Lu\n",
1008 (unsigned long long)mds->mds_mount_count,
1009 (unsigned long long)mds->mds_last_rcvd);
1010 rc = lustre_fwrite(filp, (char *)msd, sizeof(*msd), &off);
1011 if (rc != sizeof(*msd)) {
1012 CERROR("error writing MDS server data: rc = %d\n", rc);
1017 rc = fsync_dev(filp->f_dentry->d_inode->i_rdev);
1019 CERROR("error flushing MDS server data: rc = %d\n", rc);
1024 /* Do recovery actions for the MDS */
1025 static int mds_recover(struct obd_device *obddev)
1027 struct mds_obd *mds = &obddev->u.mds;
1028 struct obd_run_ctxt saved;
1031 /* This happens at the end when recovery is complete */
1032 ++mds->mds_mount_count;
1033 push_ctxt(&saved, &mds->mds_ctxt);
1034 rc = mds_update_server_data(mds);
1040 #define MDS_NUM_THREADS 8
1041 /* mount the file system (secretly) */
1042 static int mds_setup(struct obd_device *obddev, obd_count len, void *buf)
1045 struct obd_ioctl_data* data = buf;
1046 struct mds_obd *mds = &obddev->u.mds;
1047 struct vfsmount *mnt;
1052 #ifdef CONFIG_DEV_RDONLY
1053 dev_clear_rdonly(2);
1055 if (!data->ioc_inlbuf1 || !data->ioc_inlbuf2)
1056 GOTO(err_dec, rc = -EINVAL);
1058 mds->mds_fstype = strdup(data->ioc_inlbuf2);
1060 mnt = do_kern_mount(mds->mds_fstype, 0, data->ioc_inlbuf1, NULL);
1063 CERROR("do_kern_mount failed: rc = %d\n", rc);
1064 GOTO(err_kfree, rc);
1067 mds->mds_sb = mnt->mnt_root->d_inode->i_sb;
1069 GOTO(err_put, rc = -ENODEV);
1071 mds->mds_max_mdsize = sizeof(struct lov_stripe_md);
1072 rc = mds_fs_setup(obddev, mnt);
1074 CERROR("MDS filesystem method init failed: rc = %d\n", rc);
1078 mds->mds_service = ptlrpc_init_svc(64 * 1024, MDS_REQUEST_PORTAL,
1079 MDC_REPLY_PORTAL, "self",mds_handle);
1080 if (!mds->mds_service) {
1081 CERROR("failed to start service\n");
1082 GOTO(err_fs, rc = -EINVAL);
1085 obddev->obd_namespace =
1086 ldlm_namespace_new("mds_server", LDLM_NAMESPACE_SERVER);
1087 if (obddev->obd_namespace == NULL) {
1088 mds_cleanup(obddev);
1089 GOTO(err_svc, rc = -ENOMEM);
1092 for (i = 0; i < MDS_NUM_THREADS; i++) {
1094 sprintf(name, "lustre_MDS_%02d", i);
1095 rc = ptlrpc_start_thread(obddev, mds->mds_service, name);
1097 CERROR("cannot start MDS thread #%d: rc %d\n", i, rc);
1098 GOTO(err_thread, rc);
1102 rc = mds_recover(obddev);
1104 GOTO(err_thread, rc);
1109 ptlrpc_stop_all_threads(mds->mds_service);
1111 ptlrpc_unregister_service(mds->mds_service);
1113 mds_fs_cleanup(obddev);
1116 mntput(mds->mds_vfsmnt);
1120 kfree(mds->mds_fstype);
1126 static int mds_cleanup(struct obd_device *obddev)
1128 struct super_block *sb;
1129 struct mds_obd *mds = &obddev->u.mds;
1130 struct obd_run_ctxt saved;
1133 ptlrpc_stop_all_threads(mds->mds_service);
1134 ptlrpc_unregister_service(mds->mds_service);
1140 push_ctxt(&saved, &mds->mds_ctxt);
1141 mds_update_server_data(mds);
1143 if (mds->mds_rcvd_filp) {
1144 int rc = filp_close(mds->mds_rcvd_filp, 0);
1145 mds->mds_rcvd_filp = NULL;
1148 CERROR("last_rcvd file won't close, rc=%d\n", rc);
1153 mntput(mds->mds_vfsmnt);
1155 kfree(mds->mds_fstype);
1157 ldlm_namespace_free(obddev->obd_namespace);
1160 #ifdef CONFIG_DEV_RDONLY
1161 dev_clear_rdonly(2);
1163 mds_fs_cleanup(obddev);
1169 static int ldlm_intent_policy(struct ldlm_lock *lock, void *req_cookie,
1170 ldlm_mode_t mode, void *data)
1172 struct ptlrpc_request *req = req_cookie;
1179 if (req->rq_reqmsg->bufcount > 1) {
1180 /* an intent needs to be considered */
1181 struct ldlm_intent *it = lustre_msg_buf(req->rq_reqmsg, 1);
1182 struct mds_obd *mds= &req->rq_export->exp_obd->u.mds;
1183 struct mds_body *mds_rep;
1184 struct ldlm_reply *rep;
1185 __u64 new_resid[3] = {0, 0, 0}, old_res;
1186 int bufcount = -1, rc, size[3] = {sizeof(struct ldlm_reply),
1187 sizeof(struct mds_body),
1188 mds->mds_max_mdsize};
1190 it->opc = NTOH__u64(it->opc);
1192 LDLM_DEBUG(lock, "intent policy, opc: %s",
1193 ldlm_it2str(it->opc));
1196 switch((long)it->opc) {
1198 /* Note that in the negative case you may be returning
1199 * a file and its obdo */
1201 case IT_CREAT|IT_OPEN:
1222 rc = lustre_pack_msg(bufcount, size, NULL, &req->rq_replen,
1225 rc = req->rq_status = -ENOMEM;
1229 rep = lustre_msg_buf(req->rq_repmsg, 0);
1230 rep->lock_policy_res1 = 1;
1232 /* execute policy */
1233 switch ((long)it->opc) {
1234 case IT_CREAT|IT_OPEN:
1235 rc = mds_reint(2, req);
1236 if (rc || (req->rq_status != 0 &&
1237 req->rq_status != -EEXIST)) {
1238 rep->lock_policy_res2 = req->rq_status;
1239 RETURN(ELDLM_LOCK_ABORTED);
1250 rc = mds_reint(2, req);
1251 if (rc || (req->rq_status != 0 &&
1252 req->rq_status != -EISDIR &&
1253 req->rq_status != -ENOTDIR)) {
1254 rep->lock_policy_res2 = req->rq_status;
1255 RETURN(ELDLM_LOCK_ABORTED);
1265 rc = mds_getattr_name(2, req);
1266 /* FIXME: we need to sit down and decide on who should
1267 * set req->rq_status, who should return negative and
1268 * positive return values, and what they all mean. */
1269 if (rc || req->rq_status != 0) {
1270 rep->lock_policy_res2 = req->rq_status;
1271 RETURN(ELDLM_LOCK_ABORTED);
1274 case IT_READDIR|IT_OPEN:
1278 CERROR("Unhandled intent\n");
1282 if (it->opc == IT_UNLINK || it->opc == IT_RMDIR ||
1283 it->opc == IT_RENAME || it->opc == IT_RENAME2)
1284 RETURN(ELDLM_LOCK_ABORTED);
1286 rep->lock_policy_res2 = req->rq_status;
1287 mds_rep = lustre_msg_buf(req->rq_repmsg, 1);
1289 /* If the client is about to open a file that doesn't have an MD
1290 * stripe record, it's going to need a write lock. */
1291 if (it->opc & IT_OPEN) {
1292 struct lov_stripe_md *md =
1293 lustre_msg_buf(req->rq_repmsg, 2);
1294 if (md->lmd_easize == 0) {
1295 LDLM_DEBUG(lock, "open with no EA; returning PW"
1297 lock->l_req_mode = LCK_PW;
1301 /* Give the client a lock on the child object, instead of the
1302 * parent that it requested. */
1303 new_resid[0] = NTOH__u32(mds_rep->ino);
1304 if (new_resid[0] == 0)
1306 old_res = lock->l_resource->lr_name[0];
1308 ldlm_lock_change_resource(lock, new_resid);
1309 if (lock->l_resource == NULL) {
1313 LDLM_DEBUG(lock, "intent policy, old res %ld",
1315 RETURN(ELDLM_LOCK_CHANGED);
1317 int size = sizeof(struct ldlm_reply);
1318 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen,
1329 extern int mds_iocontrol(long cmd, struct lustre_handle *conn,
1330 int len, void *karg, void *uarg);
1332 /* use obd ops to offer management infrastructure */
1333 static struct obd_ops mds_obd_ops = {
1334 o_connect: mds_connect,
1335 o_disconnect: mds_disconnect,
1337 o_cleanup: mds_cleanup,
1338 o_iocontrol: mds_iocontrol
1341 static int __init mds_init(void)
1343 class_register_type(&mds_obd_ops, LUSTRE_MDS_NAME);
1344 ldlm_register_intent(ldlm_intent_policy);
1348 static void __exit mds_exit(void)
1350 ldlm_unregister_intent();
1351 class_unregister_type(LUSTRE_MDS_NAME);
1354 MODULE_AUTHOR("Cluster File Systems <info@clusterfs.com>");
1355 MODULE_DESCRIPTION("Lustre Metadata Server (MDS) v0.01");
1356 MODULE_LICENSE("GPL");
1358 module_init(mds_init);
1359 module_exit(mds_exit);