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->bd_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->bp_xid = req->rq_xid;
89 bulk->bp_buflen = PAGE_SIZE;
90 desc->bd_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->bd_waitq, desc->bd_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 spin_lock(&obd->obd_dev_lock);
278 list_for_each(p, &obd->obd_exports) {
279 exp = list_entry(p, struct obd_export, exp_obd_chain);
280 mcd = exp->exp_mds_data.med_mcd;
281 if (!memcmp(cluuid, mcd->mcd_uuid, sizeof(mcd->mcd_uuid))) {
282 LASSERT(exp->exp_obd == obd);
284 LASSERT(list_empty(&exp->exp_conn_chain));
285 conn->addr = (__u64) (unsigned long)exp;
286 conn->cookie = exp->exp_cookie;
287 spin_unlock(&obd->obd_dev_lock);
288 CDEBUG(D_INFO, "existing export for UUID '%s' at %p\n",
290 CDEBUG(D_IOCTL,"connect: addr %Lx cookie %Lx\n",
291 (long long)conn->addr, (long long)conn->cookie);
295 spin_unlock(&obd->obd_dev_lock);
296 /* XXX There is a small race between checking the list and adding a
297 * new connection for the same UUID, but the real threat (list
298 * corruption when multiple different clients connect) is solved.
300 rc = class_connect(conn, obd, cluuid);
303 exp = class_conn2export(conn);
306 OBD_ALLOC(mcd, sizeof(*mcd));
308 CERROR("mds: out of memory for client data\n");
309 GOTO(out_export, rc = -ENOMEM);
311 memcpy(mcd->mcd_uuid, cluuid, sizeof(mcd->mcd_uuid));
312 exp->exp_mds_data.med_mcd = mcd;
313 rc = mds_client_add(&exp->exp_mds_data, -1);
320 OBD_FREE(mcd, sizeof(*mcd));
322 class_disconnect(conn);
329 static int mds_disconnect(struct lustre_handle *conn)
333 rc = class_disconnect(conn);
340 static int mds_getstatus(struct ptlrpc_request *req)
342 struct mds_obd *mds = mds_req2mds(req);
343 struct mds_body *body;
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 /* Anything we need to do here with the client's trans no or so? */
355 body = lustre_msg_buf(req->rq_repmsg, 0);
356 memcpy(&body->fid1, &mds->mds_rootfid, sizeof(body->fid1));
358 /* the last_committed and last_xid fields are filled in for all
359 * replies already - no need to do so here also.
364 static int mds_getlovinfo(struct ptlrpc_request *req)
366 struct mds_obd *mds = mds_req2mds(req);
367 struct mds_status_req *streq;
368 struct lov_desc *desc;
370 int rc, size[2] = {sizeof(*desc)};
373 streq = lustre_msg_buf(req->rq_reqmsg, 0);
374 streq->flags = NTOH__u32(streq->flags);
375 streq->repbuf = NTOH__u32(streq->repbuf);
376 size[1] = streq->repbuf;
378 rc = lustre_pack_msg(2, size, NULL, &req->rq_replen, &req->rq_repmsg);
380 CERROR("mds: out of memory for message: size=%d\n", size[1]);
381 req->rq_status = -ENOMEM;
385 desc = lustre_msg_buf(req->rq_repmsg, 0);
386 rc = mds_get_lovdesc(req->rq_obd, desc);
388 CERROR("mds_get_lovdesc error %d", rc);
393 tgt_count = NTOH__u32(desc->ld_tgt_count);
394 if (tgt_count * sizeof(uuid_t) > streq->repbuf) {
395 CERROR("too many targets, enlarge client buffers\n");
396 req->rq_status = -ENOSPC;
400 mds->mds_max_mdsize = sizeof(struct lov_mds_md) +
401 tgt_count * sizeof(struct lov_object_id);
402 rc = mds_get_lovtgts(req->rq_obd, tgt_count,
403 lustre_msg_buf(req->rq_repmsg, 1));
405 CERROR("get_lovtgts error %d\n", rc);
412 int mds_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
413 void *data, __u32 data_len)
418 l_lock(&lock->l_resource->lr_namespace->ns_lock);
419 lock->l_flags |= LDLM_FL_CBPENDING;
420 do_ast = (!lock->l_readers && !lock->l_writers);
421 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
424 struct lustre_handle lockh;
427 LDLM_DEBUG(lock, "already unused, calling ldlm_cli_cancel");
428 ldlm_lock2handle(lock, &lockh);
429 rc = ldlm_cli_cancel(&lockh);
431 CERROR("ldlm_cli_cancel: %d\n", rc);
433 LDLM_DEBUG(lock, "Lock still has references, will be"
438 static int mds_getattr_internal(struct mds_obd *mds, struct dentry *dentry,
439 struct ptlrpc_request *req,
440 struct mds_body *reqbody, int reply_off)
442 struct mds_body *body;
443 struct inode *inode = dentry->d_inode;
450 body = lustre_msg_buf(req->rq_repmsg, reply_off);
452 mds_pack_inode2fid(&body->fid1, inode);
453 mds_pack_inode2body(body, inode);
455 if (S_ISREG(inode->i_mode)) {
456 struct lov_mds_md *md;
458 md = lustre_msg_buf(req->rq_repmsg, reply_off + 1);
459 md->lmd_easize = mds->mds_max_mdsize;
460 rc = mds_fs_get_md(mds, inode, md);
465 CERROR("mds_fs_get_md failed: %d\n", rc);
468 body->valid |= OBD_MD_FLEASIZE;
469 } else if (S_ISLNK(inode->i_mode) && reqbody->valid & OBD_MD_LINKNAME) {
470 char *symname = lustre_msg_buf(req->rq_repmsg, reply_off + 1);
471 int len = req->rq_repmsg->buflens[reply_off + 1];
473 rc = inode->i_op->readlink(dentry, symname, len);
475 CERROR("readlink failed: %d\n", rc);
478 CDEBUG(D_INODE, "read symlink dest %s\n", symname);
480 body->valid |= OBD_MD_LINKNAME;
485 static int mds_getattr_name(int offset, struct ptlrpc_request *req)
487 struct mds_obd *mds = mds_req2mds(req);
488 struct obd_device *obd = req->rq_export->exp_obd;
489 struct obd_run_ctxt saved;
490 struct mds_body *body;
491 struct dentry *de = NULL, *dchild = NULL;
493 struct lustre_handle lockh;
495 int namelen, flags, lock_mode, rc = 0;
496 __u64 res_id[3] = {0, 0, 0};
499 LASSERT(!strcmp(req->rq_export->exp_obd->obd_type->typ_name, "mds"));
501 if (req->rq_reqmsg->bufcount <= offset + 1) {
503 GOTO(out_pre_de, rc = -EINVAL);
506 body = lustre_msg_buf(req->rq_reqmsg, offset);
507 name = lustre_msg_buf(req->rq_reqmsg, offset + 1);
508 namelen = req->rq_reqmsg->buflens[offset + 1];
509 /* requests were at offset 2, replies go back at 1 */
513 push_ctxt(&saved, &mds->mds_ctxt);
514 de = mds_fid2dentry(mds, &body->fid1, NULL);
517 GOTO(out_pre_de, rc = -ESTALE);
521 CDEBUG(D_INODE, "parent ino %ld, name %*s\n", dir->i_ino,namelen,name);
523 lock_mode = (req->rq_reqmsg->opc == MDS_REINT) ? LCK_CW : LCK_PW;
524 res_id[0] = dir->i_ino;
526 rc = ldlm_lock_match(obd->obd_namespace, res_id, LDLM_PLAIN,
527 NULL, 0, lock_mode, &lockh);
529 LDLM_DEBUG_NOLOCK("enqueue res "LPU64, res_id[0]);
530 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace, NULL,
531 res_id, LDLM_PLAIN, NULL, 0, lock_mode,
532 &flags, ldlm_completion_ast,
533 mds_blocking_ast, NULL, 0, &lockh);
534 if (rc != ELDLM_OK) {
535 CERROR("lock enqueue: err: %d\n", rc);
536 GOTO(out_create_de, rc = -EIO);
539 ldlm_lock_dump((void *)(unsigned long)lockh.addr);
542 dchild = lookup_one_len(name, de, namelen - 1);
543 if (IS_ERR(dchild)) {
544 CDEBUG(D_INODE, "child lookup error %ld\n", PTR_ERR(dchild));
547 GOTO(out_create_dchild, rc = -ESTALE);
550 rc = mds_getattr_internal(mds, dchild, req, body, offset);
556 ldlm_lock_decref(&lockh, lock_mode);
565 static int mds_getattr(int offset, struct ptlrpc_request *req)
567 struct mds_obd *mds = mds_req2mds(req);
568 struct obd_run_ctxt saved;
571 struct mds_body *body;
572 int rc = 0, size[2] = {sizeof(*body)}, bufcount = 1;
575 body = lustre_msg_buf(req->rq_reqmsg, offset);
576 push_ctxt(&saved, &mds->mds_ctxt);
577 de = mds_fid2dentry(mds, &body->fid1, NULL);
579 req->rq_status = -ENOENT;
580 GOTO(out_pop, rc = -ENOENT);
584 if (S_ISREG(body->fid1.f_type)) {
586 size[1] = mds->mds_max_mdsize;
587 } else if (body->valid & OBD_MD_LINKNAME) {
589 size[1] = inode->i_size + 1;
592 rc = lustre_pack_msg(bufcount, size, NULL, &req->rq_replen,
594 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK)) {
595 CERROR("out of memory or FAIL_MDS_GETATTR_PACK\n");
600 req->rq_status = mds_getattr_internal(mds, de, req, body, 0);
609 static int mds_statfs(struct ptlrpc_request *req)
611 struct mds_obd *mds = mds_req2mds(req);
612 struct obd_statfs *osfs;
614 int rc, size = sizeof(*osfs);
617 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen,
619 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_STATFS_PACK)) {
620 CERROR("mds: statfs lustre_pack_msg failed: rc = %d\n", rc);
624 rc = vfs_statfs(mds->mds_sb, &sfs);
626 CERROR("mds: statfs failed: rc %d\n", rc);
629 osfs = lustre_msg_buf(req->rq_repmsg, 0);
630 memset(osfs, 0, size);
631 statfs_pack(osfs, &sfs);
632 obd_statfs_pack(osfs, osfs);
639 static int mds_open(struct ptlrpc_request *req)
642 struct mds_body *body;
644 struct vfsmount *mnt;
645 struct mds_obd *mds = mds_req2mds(req);
646 struct mds_export_data *med;
648 struct list_head *tmp;
649 struct mds_file_data *mfd;
650 int rc, size = sizeof(*body);
653 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
654 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_OPEN_PACK)) {
655 CERROR("mds: out of memory\n");
656 req->rq_status = -ENOMEM;
660 body = lustre_msg_buf(req->rq_reqmsg, 0);
662 /* was this animal open already? */
663 /* XXX we should only check on re-open, or do a refcount... */
664 med = &req->rq_export->exp_mds_data;
665 list_for_each(tmp, &med->med_open_head) {
666 struct mds_file_data *fd;
667 fd = list_entry(tmp, struct mds_file_data, mfd_list);
668 if (body->extra == fd->mfd_clientfd &&
669 body->fid1.id == fd->mfd_file->f_dentry->d_inode->i_ino) {
670 CERROR("Re opening "LPD64"\n", body->fid1.id);
675 OBD_ALLOC(mfd, sizeof(*mfd));
677 CERROR("mds: out of memory\n");
678 req->rq_status = -ENOMEM;
682 de = mds_fid2dentry(mds, &body->fid1, &mnt);
684 req->rq_status = -ENOENT;
688 /* check if this inode has seen a delayed object creation */
689 if (req->rq_reqmsg->bufcount > 1) {
691 struct lov_mds_md *md;
692 struct inode *inode = de->d_inode;
695 md = lustre_msg_buf(req->rq_reqmsg, 1);
697 handle = mds_fs_start(mds, de->d_inode, MDS_FSOP_SETATTR);
699 req->rq_status = -ENOMEM;
703 /* XXX error handling */
704 rc = mds_fs_set_md(mds, inode, handle, md);
706 struct obd_run_ctxt saved;
707 push_ctxt(&saved, &mds->mds_ctxt);
708 rc = mds_update_last_rcvd(mds, handle, req);
714 /* FIXME: need to return last_rcvd, last_committed */
716 /* FIXME: keep rc intact */
717 rc = mds_fs_commit(mds, de->d_inode, handle);
725 file = dentry_open(de, mnt, flags & ~O_DIRECT);
726 if (!file || IS_ERR(file)) {
727 req->rq_status = -EINVAL;
728 OBD_FREE(mfd, sizeof(*mfd));
732 file->private_data = mfd;
733 mfd->mfd_file = file;
734 mfd->mfd_clientfd = body->extra;
735 list_add(&mfd->mfd_list, &med->med_open_head);
737 body = lustre_msg_buf(req->rq_repmsg, 0);
738 /* FIXME: need to have cookies involved here */
739 body->extra = (__u64) (unsigned long)file;
743 static int mds_close(struct ptlrpc_request *req)
746 struct mds_body *body;
748 struct mds_obd *mds = mds_req2mds(req);
749 struct vfsmount *mnt;
750 struct mds_file_data *mfd;
754 rc = lustre_pack_msg(0, NULL, NULL, &req->rq_replen, &req->rq_repmsg);
755 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_CLOSE_PACK)) {
756 CERROR("mds: out of memory\n");
757 req->rq_status = -ENOMEM;
761 body = lustre_msg_buf(req->rq_reqmsg, 0);
762 de = mds_fid2dentry(mds, &body->fid1, &mnt);
764 req->rq_status = -ENOENT;
768 /* FIXME: need to have cookies involved here */
769 file = (struct file *)(unsigned long)body->extra;
772 mfd = (struct mds_file_data *)file->private_data;
773 list_del(&mfd->mfd_list);
774 OBD_FREE(mfd, sizeof(*mfd));
776 req->rq_status = filp_close(file, 0);
783 static int mds_readpage(struct ptlrpc_request *req)
785 struct mds_obd *mds = mds_req2mds(req);
786 struct vfsmount *mnt;
789 struct mds_body *body;
790 struct obd_run_ctxt saved;
791 int rc, size = sizeof(*body);
794 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
795 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK)) {
796 CERROR("mds: out of memory\n");
797 GOTO(out, rc = -ENOMEM);
800 body = lustre_msg_buf(req->rq_reqmsg, 0);
801 push_ctxt(&saved, &mds->mds_ctxt);
802 de = mds_fid2dentry(mds, &body->fid1, &mnt);
804 GOTO(out_pop, rc = PTR_ERR(de));
806 CDEBUG(D_INODE, "ino %ld\n", de->d_inode->i_ino);
808 file = dentry_open(de, mnt, O_RDONLY | O_LARGEFILE);
809 /* note: in case of an error, dentry_open puts dentry */
811 GOTO(out_pop, rc = PTR_ERR(file));
813 /* to make this asynchronous make sure that the handling function
814 doesn't send a reply when this function completes. Instead a
815 callback function would send the reply */
816 rc = mds_sendpage(req, file, body->size);
826 int mds_reint(int offset, struct ptlrpc_request *req)
829 struct mds_update_record rec;
831 rc = mds_update_unpack(req, offset, &rec);
832 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNPACK)) {
833 CERROR("invalid record\n");
834 req->rq_status = -EINVAL;
837 /* rc will be used to interrupt a for loop over multiple records */
838 rc = mds_reint_rec(&rec, offset, req);
842 int mds_handle(struct ptlrpc_request *req)
847 rc = lustre_unpack_msg(req->rq_reqmsg, req->rq_reqlen);
848 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_HANDLE_UNPACK)) {
849 CERROR("lustre_mds: Invalid request\n");
853 if (req->rq_reqmsg->opc != MDS_CONNECT && req->rq_export == NULL)
854 GOTO(out, rc = -ENOTCONN);
856 if (strcmp(req->rq_obd->obd_type->typ_name, "mds") != 0)
857 GOTO(out, rc = -EINVAL);
859 switch (req->rq_reqmsg->opc) {
861 CDEBUG(D_INODE, "connect\n");
862 OBD_FAIL_RETURN(OBD_FAIL_MDS_CONNECT_NET, 0);
863 rc = target_handle_connect(req);
867 CDEBUG(D_INODE, "disconnect\n");
868 OBD_FAIL_RETURN(OBD_FAIL_MDS_DISCONNECT_NET, 0);
869 rc = target_handle_disconnect(req);
873 CDEBUG(D_INODE, "getstatus\n");
874 OBD_FAIL_RETURN(OBD_FAIL_MDS_GETSTATUS_NET, 0);
875 rc = mds_getstatus(req);
879 CDEBUG(D_INODE, "getlovinfo\n");
880 rc = mds_getlovinfo(req);
884 CDEBUG(D_INODE, "getattr\n");
885 OBD_FAIL_RETURN(OBD_FAIL_MDS_GETATTR_NET, 0);
886 rc = mds_getattr(0, req);
890 CDEBUG(D_INODE, "statfs\n");
891 OBD_FAIL_RETURN(OBD_FAIL_MDS_STATFS_NET, 0);
892 rc = mds_statfs(req);
896 CDEBUG(D_INODE, "readpage\n");
897 OBD_FAIL_RETURN(OBD_FAIL_MDS_READPAGE_NET, 0);
898 rc = mds_readpage(req);
900 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
905 int size = sizeof(struct mds_body);
906 CDEBUG(D_INODE, "reint\n");
907 OBD_FAIL_RETURN(OBD_FAIL_MDS_REINT_NET, 0);
909 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen,
915 rc = mds_reint(0, req);
916 OBD_FAIL_RETURN(OBD_FAIL_MDS_REINT_NET_REP, 0);
921 CDEBUG(D_INODE, "open\n");
922 OBD_FAIL_RETURN(OBD_FAIL_MDS_OPEN_NET, 0);
927 CDEBUG(D_INODE, "close\n");
928 OBD_FAIL_RETURN(OBD_FAIL_MDS_CLOSE_NET, 0);
933 CDEBUG(D_INODE, "enqueue\n");
934 OBD_FAIL_RETURN(OBD_FAIL_LDLM_ENQUEUE, 0);
935 rc = ldlm_handle_enqueue(req);
941 CDEBUG(D_INODE, "convert\n");
942 OBD_FAIL_RETURN(OBD_FAIL_LDLM_CONVERT, 0);
943 rc = ldlm_handle_convert(req);
949 CDEBUG(D_INODE, "cancel\n");
950 OBD_FAIL_RETURN(OBD_FAIL_LDLM_CANCEL, 0);
951 rc = ldlm_handle_cancel(req);
955 case LDLM_BL_CALLBACK:
956 case LDLM_CP_CALLBACK:
957 CDEBUG(D_INODE, "callback\n");
958 CERROR("callbacks should not happen on MDS\n");
960 OBD_FAIL_RETURN(OBD_FAIL_LDLM_BL_CALLBACK, 0);
963 rc = ptlrpc_error(req->rq_svc, req);
970 struct mds_export_data *med = &req->rq_export->exp_mds_data;
971 struct mds_obd *mds = mds_req2mds(req);
973 req->rq_repmsg->last_xid =
974 HTON__u64(le64_to_cpu(med->med_mcd->mcd_last_xid));
975 req->rq_repmsg->last_committed =
976 HTON__u64(mds->mds_last_committed);
977 CDEBUG(D_INFO, "last_rcvd ~%Lu, last_committed %Lu, xid %d\n",
978 (unsigned long long)mds->mds_last_rcvd,
979 (unsigned long long)mds->mds_last_committed,
980 cpu_to_le32(req->rq_xid));
984 CERROR("mds: processing error %d\n", rc);
985 ptlrpc_error(req->rq_svc, req);
987 CDEBUG(D_NET, "sending reply\n");
988 ptlrpc_reply(req->rq_svc, req);
993 /* Update the server data on disk. This stores the new mount_count and
994 * also the last_rcvd value to disk. If we don't have a clean shutdown,
995 * then the server last_rcvd value may be less than that of the clients.
996 * This will alert us that we may need to do client recovery.
998 * Assumes we are already in the server filesystem context.
1000 * Also assumes for mds_last_rcvd that we are not modifying it (no locking).
1003 int mds_update_server_data(struct mds_obd *mds)
1005 struct mds_server_data *msd = mds->mds_server_data;
1006 struct file *filp = mds->mds_rcvd_filp;
1010 msd->msd_last_rcvd = cpu_to_le64(mds->mds_last_rcvd);
1011 msd->msd_mount_count = cpu_to_le64(mds->mds_mount_count);
1013 CDEBUG(D_SUPER, "MDS mount_count is %Lu, last_rcvd is %Lu\n",
1014 (unsigned long long)mds->mds_mount_count,
1015 (unsigned long long)mds->mds_last_rcvd);
1016 rc = lustre_fwrite(filp, (char *)msd, sizeof(*msd), &off);
1017 if (rc != sizeof(*msd)) {
1018 CERROR("error writing MDS server data: rc = %d\n", rc);
1023 rc = fsync_dev(filp->f_dentry->d_inode->i_rdev);
1025 CERROR("error flushing MDS server data: rc = %d\n", rc);
1030 /* Do recovery actions for the MDS */
1031 static int mds_recover(struct obd_device *obddev)
1033 struct mds_obd *mds = &obddev->u.mds;
1034 struct obd_run_ctxt saved;
1037 /* This happens at the end when recovery is complete */
1038 ++mds->mds_mount_count;
1039 push_ctxt(&saved, &mds->mds_ctxt);
1040 rc = mds_update_server_data(mds);
1046 #define MDS_NUM_THREADS 8
1047 /* mount the file system (secretly) */
1048 static int mds_setup(struct obd_device *obddev, obd_count len, void *buf)
1051 struct obd_ioctl_data* data = buf;
1052 struct mds_obd *mds = &obddev->u.mds;
1053 struct vfsmount *mnt;
1058 #ifdef CONFIG_DEV_RDONLY
1059 dev_clear_rdonly(2);
1061 if (!data->ioc_inlbuf1 || !data->ioc_inlbuf2)
1062 GOTO(err_dec, rc = -EINVAL);
1064 mds->mds_fstype = strdup(data->ioc_inlbuf2);
1066 mnt = do_kern_mount(mds->mds_fstype, 0, data->ioc_inlbuf1, NULL);
1069 CERROR("do_kern_mount failed: rc = %d\n", rc);
1070 GOTO(err_kfree, rc);
1073 mds->mds_sb = mnt->mnt_root->d_inode->i_sb;
1075 GOTO(err_put, rc = -ENODEV);
1077 spin_lock_init(&mds->mds_last_lock);
1078 mds->mds_max_mdsize = sizeof(struct lov_mds_md);
1079 rc = mds_fs_setup(obddev, mnt);
1081 CERROR("MDS filesystem method init failed: rc = %d\n", rc);
1085 mds->mds_service = ptlrpc_init_svc(1024, 640, MDS_REQUEST_PORTAL,
1086 MDC_REPLY_PORTAL, "self",mds_handle,
1088 if (!mds->mds_service) {
1089 CERROR("failed to start service\n");
1090 GOTO(err_fs, rc = -EINVAL);
1093 obddev->obd_namespace =
1094 ldlm_namespace_new("mds_server", LDLM_NAMESPACE_SERVER);
1095 if (obddev->obd_namespace == NULL) {
1096 mds_cleanup(obddev);
1097 GOTO(err_svc, rc = -ENOMEM);
1100 for (i = 0; i < MDS_NUM_THREADS; i++) {
1102 sprintf(name, "lustre_MDS_%02d", i);
1103 rc = ptlrpc_start_thread(obddev, mds->mds_service, name);
1105 CERROR("cannot start MDS thread #%d: rc %d\n", i, rc);
1106 GOTO(err_thread, rc);
1110 rc = mds_recover(obddev);
1112 GOTO(err_thread, rc);
1114 mds_destroy_export = mds_client_free;
1116 ptlrpc_init_client(LDLM_REQUEST_PORTAL, LDLM_REPLY_PORTAL,
1117 "mds_ldlm_client", &obddev->obd_ldlm_client);
1122 ptlrpc_stop_all_threads(mds->mds_service);
1124 ptlrpc_unregister_service(mds->mds_service);
1126 mds_fs_cleanup(obddev);
1129 mntput(mds->mds_vfsmnt);
1133 kfree(mds->mds_fstype);
1139 static int mds_cleanup(struct obd_device *obddev)
1141 struct super_block *sb;
1142 struct mds_obd *mds = &obddev->u.mds;
1143 struct obd_run_ctxt saved;
1146 ptlrpc_stop_all_threads(mds->mds_service);
1147 ptlrpc_unregister_service(mds->mds_service);
1153 push_ctxt(&saved, &mds->mds_ctxt);
1154 mds_update_server_data(mds);
1156 if (mds->mds_rcvd_filp) {
1157 int rc = filp_close(mds->mds_rcvd_filp, 0);
1158 mds->mds_rcvd_filp = NULL;
1161 CERROR("last_rcvd file won't close, rc=%d\n", rc);
1166 mntput(mds->mds_vfsmnt);
1168 kfree(mds->mds_fstype);
1170 ldlm_namespace_free(obddev->obd_namespace);
1173 #ifdef CONFIG_DEV_RDONLY
1174 dev_clear_rdonly(2);
1176 mds_fs_cleanup(obddev);
1182 static int ldlm_intent_policy(struct ldlm_lock *lock, void *req_cookie,
1183 ldlm_mode_t mode, void *data)
1185 struct ptlrpc_request *req = req_cookie;
1192 if (req->rq_reqmsg->bufcount > 1) {
1193 /* an intent needs to be considered */
1194 struct ldlm_intent *it = lustre_msg_buf(req->rq_reqmsg, 1);
1195 struct mds_obd *mds= &req->rq_export->exp_obd->u.mds;
1196 struct mds_body *mds_rep;
1197 struct ldlm_reply *rep;
1198 __u64 new_resid[3] = {0, 0, 0}, old_res;
1199 int rc, size[3] = {sizeof(struct ldlm_reply),
1200 sizeof(struct mds_body),
1201 mds->mds_max_mdsize};
1203 it->opc = NTOH__u64(it->opc);
1205 LDLM_DEBUG(lock, "intent policy, opc: %s",
1206 ldlm_it2str(it->opc));
1208 rc = lustre_pack_msg(3, size, NULL, &req->rq_replen,
1211 rc = req->rq_status = -ENOMEM;
1215 rep = lustre_msg_buf(req->rq_repmsg, 0);
1216 rep->lock_policy_res1 = 1;
1218 /* execute policy */
1219 switch ((long)it->opc) {
1220 case IT_CREAT|IT_OPEN:
1221 rc = mds_reint(2, req);
1222 if (rc || (req->rq_status != 0 &&
1223 req->rq_status != -EEXIST)) {
1224 rep->lock_policy_res2 = req->rq_status;
1225 RETURN(ELDLM_LOCK_ABORTED);
1236 rc = mds_reint(2, req);
1237 if (rc || (req->rq_status != 0 &&
1238 req->rq_status != -EISDIR &&
1239 req->rq_status != -ENOTDIR)) {
1240 rep->lock_policy_res2 = req->rq_status;
1241 RETURN(ELDLM_LOCK_ABORTED);
1251 rc = mds_getattr_name(2, req);
1252 /* FIXME: we need to sit down and decide on who should
1253 * set req->rq_status, who should return negative and
1254 * positive return values, and what they all mean. */
1255 if (rc || req->rq_status != 0) {
1256 rep->lock_policy_res2 = req->rq_status;
1257 RETURN(ELDLM_LOCK_ABORTED);
1260 case IT_READDIR|IT_OPEN:
1264 CERROR("Unhandled intent\n");
1268 if (it->opc == IT_UNLINK || it->opc == IT_RMDIR ||
1269 it->opc == IT_RENAME || it->opc == IT_RENAME2)
1270 RETURN(ELDLM_LOCK_ABORTED);
1272 rep->lock_policy_res2 = req->rq_status;
1273 mds_rep = lustre_msg_buf(req->rq_repmsg, 1);
1275 /* If the client is about to open a file that doesn't have an MD
1276 * stripe record, it's going to need a write lock. */
1277 if (it->opc & IT_OPEN) {
1278 struct lov_mds_md *md =
1279 lustre_msg_buf(req->rq_repmsg, 2);
1280 if (md->lmd_easize == 0) {
1281 LDLM_DEBUG(lock, "open with no EA; returning PW"
1283 lock->l_req_mode = LCK_PW;
1287 /* Give the client a lock on the child object, instead of the
1288 * parent that it requested. */
1289 new_resid[0] = NTOH__u32(mds_rep->ino);
1290 if (new_resid[0] == 0)
1292 old_res = lock->l_resource->lr_name[0];
1294 ldlm_lock_change_resource(lock, new_resid);
1295 if (lock->l_resource == NULL) {
1299 LDLM_DEBUG(lock, "intent policy, old res %ld",
1301 RETURN(ELDLM_LOCK_CHANGED);
1303 int size = sizeof(struct ldlm_reply);
1304 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen,
1315 extern int mds_iocontrol(long cmd, struct lustre_handle *conn,
1316 int len, void *karg, void *uarg);
1318 /* use obd ops to offer management infrastructure */
1319 static struct obd_ops mds_obd_ops = {
1320 o_connect: mds_connect,
1321 o_disconnect: mds_disconnect,
1323 o_cleanup: mds_cleanup,
1324 o_iocontrol: mds_iocontrol
1327 static int __init mds_init(void)
1329 class_register_type(&mds_obd_ops, LUSTRE_MDS_NAME);
1330 ldlm_register_intent(ldlm_intent_policy);
1334 static void __exit mds_exit(void)
1336 ldlm_unregister_intent();
1337 class_unregister_type(LUSTRE_MDS_NAME);
1340 MODULE_AUTHOR("Cluster File Systems <info@clusterfs.com>");
1341 MODULE_DESCRIPTION("Lustre Metadata Server (MDS) v0.01");
1342 MODULE_LICENSE("GPL");
1344 module_init(mds_init);
1345 module_exit(mds_exit);