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-2005 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>
11 * Author: Mike Shaver <shaver@clusterfs.com>
13 * This file is part of Lustre, http://www.lustre.org.
15 * Lustre is free software; you can redistribute it and/or
16 * modify it under the terms of version 2 of the GNU General Public
17 * License as published by the Free Software Foundation.
19 * Lustre is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with Lustre; if not, write to the Free Software
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
30 # define EXPORT_SYMTAB
32 #define DEBUG_SUBSYSTEM S_MDS
34 #include <linux/module.h>
35 #include <linux/lustre_mds.h>
36 #include <linux/lustre_dlm.h>
37 #include <linux/init.h>
38 #include <linux/obd_class.h>
39 #include <linux/random.h>
41 #include <linux/jbd.h>
42 #include <linux/ext3_fs.h>
43 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
44 # include <linux/smp_lock.h>
45 # include <linux/buffer_head.h>
46 # include <linux/workqueue.h>
47 # include <linux/mount.h>
49 # include <linux/locks.h>
51 #include <linux/obd_lov.h>
52 #include <linux/lustre_mds.h>
53 #include <linux/lustre_fsfilt.h>
54 #include <linux/lprocfs_status.h>
55 #include <linux/lustre_commit_confd.h>
56 #include <linux/lustre_quota.h>
58 #include "mds_internal.h"
60 static int mds_intent_policy(struct ldlm_namespace *ns,
61 struct ldlm_lock **lockp, void *req_cookie,
62 ldlm_mode_t mode, int flags, void *data);
63 static int mds_postsetup(struct obd_device *obd);
64 static int mds_cleanup(struct obd_device *obd);
66 /* Assumes caller has already pushed into the kernel filesystem context */
67 static int mds_sendpage(struct ptlrpc_request *req, struct file *file,
68 loff_t offset, int count)
70 struct ptlrpc_bulk_desc *desc;
71 struct l_wait_info lwi;
73 int rc = 0, npages, i, tmpcount, tmpsize = 0;
76 LASSERT((offset & (PAGE_SIZE - 1)) == 0); /* I'm dubious about this */
78 npages = (count + PAGE_SIZE - 1) >> PAGE_SHIFT;
79 OBD_ALLOC(pages, sizeof(*pages) * npages);
81 GOTO(out, rc = -ENOMEM);
83 desc = ptlrpc_prep_bulk_exp(req, npages, BULK_PUT_SOURCE,
86 GOTO(out_free, rc = -ENOMEM);
88 for (i = 0, tmpcount = count; i < npages; i++, tmpcount -= tmpsize) {
89 tmpsize = tmpcount > PAGE_SIZE ? PAGE_SIZE : tmpcount;
91 pages[i] = alloc_pages(GFP_KERNEL, 0);
93 GOTO(cleanup_buf, rc = -ENOMEM);
95 ptlrpc_prep_bulk_page(desc, pages[i], 0, tmpsize);
98 for (i = 0, tmpcount = count; i < npages; i++, tmpcount -= tmpsize) {
99 tmpsize = tmpcount > PAGE_SIZE ? PAGE_SIZE : tmpcount;
100 CDEBUG(D_EXT2, "reading %u@%llu from dir %lu (size %llu)\n",
101 tmpsize, offset, file->f_dentry->d_inode->i_ino,
102 file->f_dentry->d_inode->i_size);
104 rc = fsfilt_readpage(req->rq_export->exp_obd, file,
105 kmap(pages[i]), tmpsize, &offset);
109 GOTO(cleanup_buf, rc = -EIO);
112 LASSERT(desc->bd_nob == count);
114 rc = ptlrpc_start_bulk_transfer(desc);
116 GOTO(cleanup_buf, rc);
118 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE)) {
119 CERROR("obd_fail_loc=%x, fail operation rc=%d\n",
120 OBD_FAIL_MDS_SENDPAGE, rc);
121 GOTO(abort_bulk, rc);
124 lwi = LWI_TIMEOUT(obd_timeout * HZ / 4, NULL, NULL);
125 rc = l_wait_event(desc->bd_waitq, !ptlrpc_bulk_active(desc), &lwi);
126 LASSERT (rc == 0 || rc == -ETIMEDOUT);
129 if (desc->bd_success &&
130 desc->bd_nob_transferred == count)
131 GOTO(cleanup_buf, rc);
133 rc = -ETIMEDOUT; /* XXX should this be a different errno? */
136 DEBUG_REQ(D_ERROR, req, "bulk failed: %s %d(%d), evicting %s@%s\n",
137 (rc == -ETIMEDOUT) ? "timeout" : "network error",
138 desc->bd_nob_transferred, count,
139 req->rq_export->exp_client_uuid.uuid,
140 req->rq_export->exp_connection->c_remote_uuid.uuid);
142 class_fail_export(req->rq_export);
146 ptlrpc_abort_bulk (desc);
148 for (i = 0; i < npages; i++)
150 __free_pages(pages[i], 0);
152 ptlrpc_free_bulk(desc);
154 OBD_FREE(pages, sizeof(*pages) * npages);
159 /* only valid locked dentries or errors should be returned */
160 struct dentry *mds_fid2locked_dentry(struct obd_device *obd, struct ll_fid *fid,
161 struct vfsmount **mnt, int lock_mode,
162 struct lustre_handle *lockh,
163 char *name, int namelen)
165 struct mds_obd *mds = &obd->u.mds;
166 struct dentry *de = mds_fid2dentry(mds, fid, mnt), *retval = de;
167 struct ldlm_res_id res_id = { .name = {0} };
174 res_id.name[0] = de->d_inode->i_ino;
175 res_id.name[1] = de->d_inode->i_generation;
176 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace, res_id,
177 LDLM_PLAIN, NULL, lock_mode, &flags,
178 mds_blocking_ast, ldlm_completion_ast, NULL, NULL,
179 NULL, 0, NULL, lockh);
180 if (rc != ELDLM_OK) {
182 retval = ERR_PTR(-EIO); /* XXX translate ldlm code */
188 /* Look up an entry by inode number. */
189 /* this function ONLY returns valid dget'd dentries with an initialized inode
191 struct dentry *mds_fid2dentry(struct mds_obd *mds, struct ll_fid *fid,
192 struct vfsmount **mnt)
195 unsigned long ino = fid->id;
196 __u32 generation = fid->generation;
198 struct dentry *result;
201 RETURN(ERR_PTR(-ESTALE));
203 snprintf(fid_name, sizeof(fid_name), "0x%lx", ino);
205 CDEBUG(D_DENTRY, "--> mds_fid2dentry: ino/gen %lu/%u, sb %p\n",
206 ino, generation, mds->mds_sb);
208 /* under ext3 this is neither supposed to return bad inodes
210 result = ll_lookup_one_len(fid_name, mds->mds_fid_de, strlen(fid_name));
214 inode = result->d_inode;
216 RETURN(ERR_PTR(-ENOENT));
218 if (generation && inode->i_generation != generation) {
219 /* we didn't find the right inode.. */
220 CERROR("bad inode %lu, link: %lu ct: %d or generation %u/%u\n",
221 inode->i_ino, (unsigned long)inode->i_nlink,
222 atomic_read(&inode->i_count), inode->i_generation,
225 RETURN(ERR_PTR(-ENOENT));
229 *mnt = mds->mds_vfsmnt;
237 /* Establish a connection to the MDS.
239 * This will set up an export structure for the client to hold state data
240 * about that client, like open files, the last operation number it did
241 * on the server, etc.
243 static int mds_connect(struct lustre_handle *conn, struct obd_device *obd,
244 struct obd_uuid *cluuid, struct obd_connect_data *data)
246 struct obd_export *exp;
247 struct mds_export_data *med;
248 struct mds_client_data *mcd;
249 int rc, abort_recovery;
252 if (!conn || !obd || !cluuid)
255 /* Check for aborted recovery. */
256 spin_lock_bh(&obd->obd_processing_task_lock);
257 abort_recovery = obd->obd_abort_recovery;
258 spin_unlock_bh(&obd->obd_processing_task_lock);
260 target_abort_recovery(obd);
262 /* XXX There is a small race between checking the list and adding a
263 * new connection for the same UUID, but the real threat (list
264 * corruption when multiple different clients connect) is solved.
266 * There is a second race between adding the export to the list,
267 * and filling in the client data below. Hence skipping the case
268 * of NULL mcd above. We should already be controlling multiple
269 * connects at the client, and we can't hold the spinlock over
270 * memory allocations without risk of deadlocking.
272 rc = class_connect(conn, obd, cluuid);
275 exp = class_conn2export(conn);
277 med = &exp->exp_mds_data;
280 data->ocd_connect_flags &= MDS_CONNECT_SUPPORTED;
281 exp->exp_connect_flags = data->ocd_connect_flags;
284 OBD_ALLOC(mcd, sizeof(*mcd));
286 CERROR("mds: out of memory for client data\n");
287 GOTO(out, rc = -ENOMEM);
290 memcpy(mcd->mcd_uuid, cluuid, sizeof(mcd->mcd_uuid));
293 rc = mds_client_add(obd, &obd->u.mds, med, -1);
299 OBD_FREE(mcd, sizeof(*mcd));
302 class_disconnect(exp);
304 class_export_put(exp);
310 static int mds_init_export(struct obd_export *exp)
312 struct mds_export_data *med = &exp->exp_mds_data;
314 INIT_LIST_HEAD(&med->med_open_head);
315 spin_lock_init(&med->med_open_lock);
319 static int mds_destroy_export(struct obd_export *export)
321 struct mds_export_data *med;
322 struct obd_device *obd = export->exp_obd;
323 struct lvfs_run_ctxt saved;
327 med = &export->exp_mds_data;
328 target_destroy_export(export);
330 if (obd_uuid_equals(&export->exp_client_uuid, &obd->obd_uuid))
333 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
334 /* Close any open files (which may also cause orphan unlinking). */
335 spin_lock(&med->med_open_lock);
336 while (!list_empty(&med->med_open_head)) {
337 struct list_head *tmp = med->med_open_head.next;
338 struct mds_file_data *mfd =
339 list_entry(tmp, struct mds_file_data, mfd_list);
340 struct dentry *dentry = mfd->mfd_dentry;
342 /* Remove mfd handle so it can't be found again.
343 * We are consuming the mfd_list reference here. */
344 mds_mfd_unlink(mfd, 0);
345 spin_unlock(&med->med_open_lock);
347 /* If you change this message, be sure to update
348 * replay_single:test_46 */
349 CDEBUG(D_INODE|D_IOCTL, "%s: force closing file handle for "
350 "%.*s (ino %lu)\n", obd->obd_name, dentry->d_name.len,
351 dentry->d_name.name, dentry->d_inode->i_ino);
352 /* child orphan sem protects orphan_dec_test and
353 * is_orphan race, mds_mfd_close drops it */
354 MDS_DOWN_WRITE_ORPHAN_SEM(dentry->d_inode);
355 rc = mds_mfd_close(NULL, obd, mfd,
356 !(export->exp_flags & OBD_OPT_FAILOVER));
359 CDEBUG(D_INODE|D_IOCTL, "Error closing file: %d\n", rc);
360 spin_lock(&med->med_open_lock);
362 spin_unlock(&med->med_open_lock);
363 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
365 mds_client_free(export);
370 static int mds_disconnect(struct obd_export *exp)
372 unsigned long irqflags;
377 class_export_get(exp);
379 /* Disconnect early so that clients can't keep using export */
380 rc = class_disconnect(exp);
381 ldlm_cancel_locks_for_export(exp);
383 /* complete all outstanding replies */
384 spin_lock_irqsave(&exp->exp_lock, irqflags);
385 while (!list_empty(&exp->exp_outstanding_replies)) {
386 struct ptlrpc_reply_state *rs =
387 list_entry(exp->exp_outstanding_replies.next,
388 struct ptlrpc_reply_state, rs_exp_list);
389 struct ptlrpc_service *svc = rs->rs_service;
391 spin_lock(&svc->srv_lock);
392 list_del_init(&rs->rs_exp_list);
393 ptlrpc_schedule_difficult_reply(rs);
394 spin_unlock(&svc->srv_lock);
396 spin_unlock_irqrestore(&exp->exp_lock, irqflags);
398 class_export_put(exp);
402 static int mds_getstatus(struct ptlrpc_request *req)
404 struct mds_obd *mds = mds_req2mds(req);
405 struct mds_body *body;
406 int rc, size = sizeof(*body);
409 rc = lustre_pack_reply(req, 1, &size, NULL);
410 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_GETSTATUS_PACK)) {
411 CERROR("mds: out of memory for message: size=%d\n", size);
412 req->rq_status = -ENOMEM; /* superfluous? */
416 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
417 memcpy(&body->fid1, &mds->mds_rootfid, sizeof(body->fid1));
419 /* the last_committed and last_xid fields are filled in for all
420 * replies already - no need to do so here also.
425 int mds_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
426 void *data, int flag)
431 if (flag == LDLM_CB_CANCELING) {
432 /* Don't need to do anything here. */
436 /* XXX layering violation! -phil */
437 l_lock(&lock->l_resource->lr_namespace->ns_lock);
438 /* Get this: if mds_blocking_ast is racing with mds_intent_policy,
439 * such that mds_blocking_ast is called just before l_i_p takes the
440 * ns_lock, then by the time we get the lock, we might not be the
441 * correct blocking function anymore. So check, and return early, if
443 if (lock->l_blocking_ast != mds_blocking_ast) {
444 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
448 lock->l_flags |= LDLM_FL_CBPENDING;
449 do_ast = (!lock->l_readers && !lock->l_writers);
450 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
453 struct lustre_handle lockh;
456 LDLM_DEBUG(lock, "already unused, calling ldlm_cli_cancel");
457 ldlm_lock2handle(lock, &lockh);
458 rc = ldlm_cli_cancel(&lockh);
460 CERROR("ldlm_cli_cancel: %d\n", rc);
462 LDLM_DEBUG(lock, "Lock still has references, will be "
468 int mds_get_md(struct obd_device *obd, struct inode *inode, void *md,
476 rc = fsfilt_get_md(obd, inode, md, *size);
479 CERROR("Error %d reading eadata for ino %lu\n",
483 rc = mds_convert_lov_ea(obd, inode, md, lmm_size);
499 /* Call with lock=1 if you want mds_pack_md to take the i_sem.
500 * Call with lock=0 if the caller has already taken the i_sem. */
501 int mds_pack_md(struct obd_device *obd, struct lustre_msg *msg, int offset,
502 struct mds_body *body, struct inode *inode, int lock)
504 struct mds_obd *mds = &obd->u.mds;
510 lmm = lustre_msg_buf(msg, offset, 0);
512 /* Some problem with getting eadata when I sized the reply
514 CDEBUG(D_INFO, "no space reserved for inode %lu MD\n",
518 lmm_size = msg->buflens[offset];
520 /* I don't really like this, but it is a sanity check on the client
521 * MD request. However, if the client doesn't know how much space
522 * to reserve for the MD, it shouldn't be bad to have too much space.
524 if (lmm_size > mds->mds_max_mdsize) {
525 CWARN("Reading MD for inode %lu of %d bytes > max %d\n",
526 inode->i_ino, lmm_size, mds->mds_max_mdsize);
530 rc = mds_get_md(obd, inode, lmm, &lmm_size, lock);
532 if (S_ISDIR(inode->i_mode))
533 body->valid |= OBD_MD_FLDIREA;
535 body->valid |= OBD_MD_FLEASIZE;
536 body->eadatasize = lmm_size;
543 static int mds_getattr_internal(struct obd_device *obd, struct dentry *dentry,
544 struct ptlrpc_request *req,
545 struct mds_body *reqbody, int reply_off)
547 struct mds_body *body;
548 struct inode *inode = dentry->d_inode;
555 body = lustre_msg_buf(req->rq_repmsg, reply_off, sizeof(*body));
556 LASSERT(body != NULL); /* caller prepped reply */
558 mds_pack_inode2fid(&body->fid1, inode);
559 mds_pack_inode2body(body, inode);
561 if ((S_ISREG(inode->i_mode) && (reqbody->valid & OBD_MD_FLEASIZE)) ||
562 (S_ISDIR(inode->i_mode) && (reqbody->valid & OBD_MD_FLDIREA))) {
563 rc = mds_pack_md(obd, req->rq_repmsg, reply_off + 1, body,
566 /* If we have LOV EA data, the OST holds size, atime, mtime */
567 if (!(body->valid & OBD_MD_FLEASIZE) &&
568 !(body->valid & OBD_MD_FLDIREA))
569 body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
570 OBD_MD_FLATIME | OBD_MD_FLMTIME);
571 } else if (S_ISLNK(inode->i_mode) &&
572 (reqbody->valid & OBD_MD_LINKNAME) != 0) {
573 char *symname = lustre_msg_buf(req->rq_repmsg, reply_off + 1,0);
576 LASSERT (symname != NULL); /* caller prepped reply */
577 len = req->rq_repmsg->buflens[reply_off + 1];
579 rc = inode->i_op->readlink(dentry, symname, len);
581 CERROR("readlink failed: %d\n", rc);
582 } else if (rc != len - 1) {
583 CERROR ("Unexpected readlink rc %d: expecting %d\n",
587 CDEBUG(D_INODE, "read symlink dest %s\n", symname);
588 body->valid |= OBD_MD_LINKNAME;
589 body->eadatasize = rc + 1;
590 symname[rc] = 0; /* NULL terminate */
598 static int mds_getattr_pack_msg(struct ptlrpc_request *req, struct inode *inode,
601 struct mds_obd *mds = mds_req2mds(req);
602 struct mds_body *body;
603 int rc = 0, size[2] = {sizeof(*body)}, bufcount = 1;
606 body = lustre_msg_buf(req->rq_reqmsg, offset, sizeof (*body));
607 LASSERT(body != NULL); /* checked by caller */
608 LASSERT_REQSWABBED(req, offset); /* swabbed by caller */
610 if ((S_ISREG(inode->i_mode) && (body->valid & OBD_MD_FLEASIZE)) ||
611 (S_ISDIR(inode->i_mode) && (body->valid & OBD_MD_FLDIREA))) {
614 ret = fsfilt_get_md(req->rq_export->exp_obd, inode, NULL, 0);
616 CDEBUG(D_INODE, "got %d bytes MD data for inode %lu\n",
619 if (ret != -ENODATA) {
620 CERROR("error getting inode %lu MD: rc = %d\n",
622 /* should we return ret in req->rq_status? */
625 } else if (ret > mds->mds_max_mdsize) {
627 CERROR("MD size %d larger than maximum possible %u\n",
628 ret, mds->mds_max_mdsize);
630 size[bufcount] = ret;
633 } else if (S_ISLNK(inode->i_mode) && (body->valid & OBD_MD_LINKNAME)) {
634 if (inode->i_size + 1 != body->eadatasize)
635 CERROR("symlink size: %Lu, reply space: %d\n",
636 inode->i_size + 1, body->eadatasize);
637 size[bufcount] = min_t(int, inode->i_size+1, body->eadatasize);
639 CDEBUG(D_INODE, "symlink size: %Lu, reply space: %d\n",
640 inode->i_size + 1, body->eadatasize);
643 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK)) {
644 CERROR("failed MDS_GETATTR_PACK test\n");
645 req->rq_status = -ENOMEM;
646 GOTO(out, rc = -ENOMEM);
649 rc = lustre_pack_reply(req, bufcount, size, NULL);
651 CERROR("lustre_pack_reply failed: rc %d\n", rc);
652 GOTO(out, req->rq_status = rc);
660 static int mds_getattr_name(int offset, struct ptlrpc_request *req,
661 struct lustre_handle *child_lockh)
663 struct obd_device *obd = req->rq_export->exp_obd;
664 struct ldlm_reply *rep = NULL;
665 struct lvfs_run_ctxt saved;
666 struct mds_body *body;
667 struct dentry *dparent = NULL, *dchild = NULL;
668 struct lvfs_ucred uc;
669 struct lustre_handle parent_lockh;
671 int rc = 0, cleanup_phase = 0, resent_req = 0;
675 LASSERT(!strcmp(obd->obd_type->typ_name, "mds"));
677 /* Swab now, before anyone looks inside the request */
679 body = lustre_swab_reqbuf(req, offset, sizeof(*body),
680 lustre_swab_mds_body);
682 CERROR("Can't swab mds_body\n");
683 GOTO(cleanup, rc = -EFAULT);
686 LASSERT_REQSWAB(req, offset + 1);
687 name = lustre_msg_string(req->rq_reqmsg, offset + 1, 0);
689 CERROR("Can't unpack name\n");
690 GOTO(cleanup, rc = -EFAULT);
692 namesize = req->rq_reqmsg->buflens[offset + 1];
694 LASSERT (offset == 0 || offset == 2);
695 /* if requests were at offset 2, the getattr reply goes back at 1 */
697 rep = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*rep));
702 uc.luc_fsuid = req->rq_uid;
704 uc.luc_fsuid = body->fsuid;
706 uc.luc_fsgid = body->fsgid;
707 uc.luc_cap = body->capability;
708 uc.luc_suppgid1 = body->suppgid;
709 uc.luc_suppgid2 = -1;
710 push_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
711 cleanup_phase = 1; /* kernel context */
712 intent_set_disposition(rep, DISP_LOOKUP_EXECD);
714 /* FIXME: handle raw lookup */
716 if (body->valid == OBD_MD_FLID) {
717 struct mds_body *mds_reply;
718 int size = sizeof(*mds_reply);
720 // The user requested ONLY the inode number, so do a raw lookup
721 rc = lustre_pack_reply(req, 1, &size, NULL);
723 CERROR("out of memory\n");
727 rc = dir->i_op->lookup_raw(dir, name, namesize - 1, &inum);
729 mds_reply = lustre_msg_buf(req->rq_repmsg, offset,
731 mds_reply->fid1.id = inum;
732 mds_reply->valid = OBD_MD_FLID;
737 if (child_lockh->cookie != 0) {
738 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT);
742 if (resent_req == 0) {
743 rc = mds_get_parent_child_locked(obd, &obd->u.mds, &body->fid1,
744 &parent_lockh, &dparent,
745 LCK_PR, name, namesize,
746 child_lockh, &dchild, LCK_PR);
750 struct ldlm_lock *granted_lock;
751 struct ll_fid child_fid;
752 struct ldlm_resource *res;
753 DEBUG_REQ(D_DLMTRACE, req, "resent, not enqueuing new locks");
754 granted_lock = ldlm_handle2lock(child_lockh);
755 LASSERTF(granted_lock != NULL, LPU64"/%u lockh "LPX64"\n",
756 body->fid1.id, body->fid1.generation,
757 child_lockh->cookie);
760 res = granted_lock->l_resource;
761 child_fid.id = res->lr_name.name[0];
762 child_fid.generation = res->lr_name.name[1];
763 dchild = mds_fid2dentry(&obd->u.mds, &child_fid, NULL);
764 LASSERT(!IS_ERR(dchild));
765 LDLM_LOCK_PUT(granted_lock);
768 cleanup_phase = 2; /* dchild, dparent, locks */
770 if (dchild->d_inode == NULL) {
771 intent_set_disposition(rep, DISP_LOOKUP_NEG);
772 /* in the intent case, the policy clears this error:
773 the disposition is enough */
774 GOTO(cleanup, rc = -ENOENT);
776 intent_set_disposition(rep, DISP_LOOKUP_POS);
779 if (req->rq_repmsg == NULL) {
780 rc = mds_getattr_pack_msg(req, dchild->d_inode, offset);
782 CERROR ("mds_getattr_pack_msg: %d\n", rc);
787 rc = mds_getattr_internal(obd, dchild, req, body, offset);
788 GOTO(cleanup, rc); /* returns the lock to the client */
791 switch (cleanup_phase) {
793 if (resent_req == 0) {
794 if (rc && dchild->d_inode)
795 ldlm_lock_decref(child_lockh, LCK_PR);
796 ldlm_lock_decref(&parent_lockh, LCK_PR);
801 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
807 static int mds_getattr(int offset, struct ptlrpc_request *req)
809 struct mds_obd *mds = mds_req2mds(req);
810 struct obd_device *obd = req->rq_export->exp_obd;
811 struct lvfs_run_ctxt saved;
813 struct mds_body *body;
814 struct lvfs_ucred uc;
818 body = lustre_swab_reqbuf(req, offset, sizeof(*body),
819 lustre_swab_mds_body);
821 CERROR("Can't unpack body\n");
826 uc.luc_fsuid = req->rq_uid;
828 uc.luc_fsuid = body->fsuid;
830 uc.luc_fsgid = body->fsgid;
831 uc.luc_cap = body->capability;
832 push_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
833 de = mds_fid2dentry(mds, &body->fid1, NULL);
835 rc = req->rq_status = PTR_ERR(de);
839 rc = mds_getattr_pack_msg(req, de->d_inode, offset);
841 CERROR("mds_getattr_pack_msg: %d\n", rc);
845 req->rq_status = mds_getattr_internal(obd, de, req, body, 0);
850 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
855 static int mds_obd_statfs(struct obd_device *obd, struct obd_statfs *osfs,
856 unsigned long max_age)
860 spin_lock(&obd->obd_osfs_lock);
861 rc = fsfilt_statfs(obd, obd->u.mds.mds_sb, max_age);
863 memcpy(osfs, &obd->obd_osfs, sizeof(*osfs));
864 spin_unlock(&obd->obd_osfs_lock);
869 static int mds_statfs(struct ptlrpc_request *req)
871 struct obd_device *obd = req->rq_export->exp_obd;
872 int rc, size = sizeof(struct obd_statfs);
875 /* This will trigger a watchdog timeout */
876 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_STATFS_LCW_SLEEP,
877 (MDS_SERVICE_WATCHDOG_TIMEOUT / 1000) + 1);
879 rc = lustre_pack_reply(req, 1, &size, NULL);
880 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_STATFS_PACK)) {
881 CERROR("mds: statfs lustre_pack_reply failed: rc = %d\n", rc);
885 /* We call this so that we can cache a bit - 1 jiffie worth */
886 rc = mds_obd_statfs(obd, lustre_msg_buf(req->rq_repmsg, 0, size),
889 CERROR("mds_obd_statfs failed: rc %d\n", rc);
899 static int mds_sync(struct ptlrpc_request *req)
901 struct obd_device *obd = req->rq_export->exp_obd;
902 struct mds_obd *mds = &obd->u.mds;
903 struct mds_body *body;
904 int rc, size = sizeof(*body);
907 body = lustre_swab_reqbuf(req, 0, sizeof(*body), lustre_swab_mds_body);
909 GOTO(out, rc = -EFAULT);
911 rc = lustre_pack_reply(req, 1, &size, NULL);
912 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK)) {
913 CERROR("fsync lustre_pack_reply failed: rc = %d\n", rc);
917 if (body->fid1.id == 0) {
918 /* a fid of zero is taken to mean "sync whole filesystem" */
919 rc = fsfilt_sync(obd, mds->mds_sb);
924 de = mds_fid2dentry(mds, &body->fid1, NULL);
926 GOTO(out, rc = PTR_ERR(de));
928 /* The file parameter isn't used for anything */
929 if (de->d_inode->i_fop && de->d_inode->i_fop->fsync)
930 rc = de->d_inode->i_fop->fsync(NULL, de, 1);
932 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
933 mds_pack_inode2fid(&body->fid1, de->d_inode);
934 mds_pack_inode2body(body, de->d_inode);
945 /* mds_readpage does not take a DLM lock on the inode, because the client must
946 * already have a PR lock.
948 * If we were to take another one here, a deadlock will result, if another
949 * thread is already waiting for a PW lock. */
950 static int mds_readpage(struct ptlrpc_request *req)
952 struct obd_device *obd = req->rq_export->exp_obd;
953 struct vfsmount *mnt;
956 struct mds_body *body, *repbody;
957 struct lvfs_run_ctxt saved;
958 int rc, size = sizeof(*repbody);
959 struct lvfs_ucred uc;
962 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK))
965 rc = lustre_pack_reply(req, 1, &size, NULL);
967 CERROR("mds: out of memory while packing readpage reply\n");
971 body = lustre_swab_reqbuf(req, 0, sizeof(*body), lustre_swab_mds_body);
973 GOTO (out, rc = -EFAULT);
976 uc.luc_fsuid = req->rq_uid;
978 uc.luc_fsuid = body->fsuid;
980 uc.luc_fsgid = body->fsgid;
981 uc.luc_cap = body->capability;
982 push_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
983 de = mds_fid2dentry(&obd->u.mds, &body->fid1, &mnt);
985 GOTO(out_pop, rc = PTR_ERR(de));
987 CDEBUG(D_INODE, "ino %lu\n", de->d_inode->i_ino);
989 file = dentry_open(de, mnt, O_RDONLY | O_LARGEFILE);
990 /* note: in case of an error, dentry_open puts dentry */
992 GOTO(out_pop, rc = PTR_ERR(file));
994 /* body->size is actually the offset -eeb */
995 if ((body->size & (de->d_inode->i_blksize - 1)) != 0) {
996 CERROR("offset "LPU64" not on a block boundary of %lu\n",
997 body->size, de->d_inode->i_blksize);
998 GOTO(out_file, rc = -EFAULT);
1001 /* body->nlink is actually the #bytes to read -eeb */
1002 if (body->nlink & (de->d_inode->i_blksize - 1)) {
1003 CERROR("size %u is not multiple of blocksize %lu\n",
1004 body->nlink, de->d_inode->i_blksize);
1005 GOTO(out_file, rc = -EFAULT);
1008 repbody = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*repbody));
1009 repbody->size = file->f_dentry->d_inode->i_size;
1010 repbody->valid = OBD_MD_FLSIZE;
1012 /* to make this asynchronous make sure that the handling function
1013 doesn't send a reply when this function completes. Instead a
1014 callback function would send the reply */
1015 /* body->size is actually the offset -eeb */
1016 rc = mds_sendpage(req, file, body->size, body->nlink);
1019 filp_close(file, 0);
1021 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1023 req->rq_status = rc;
1027 int mds_reint(struct ptlrpc_request *req, int offset,
1028 struct lustre_handle *lockh)
1030 struct mds_update_record *rec; /* 116 bytes on the stack? no sir! */
1033 OBD_ALLOC(rec, sizeof(*rec));
1037 rc = mds_update_unpack(req, offset, rec);
1038 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNPACK)) {
1039 CERROR("invalid record\n");
1040 GOTO(out, req->rq_status = -EINVAL);
1042 /* rc will be used to interrupt a for loop over multiple records */
1043 rc = mds_reint_rec(rec, offset, req, lockh);
1045 OBD_FREE(rec, sizeof(*rec));
1049 static int mds_filter_recovery_request(struct ptlrpc_request *req,
1050 struct obd_device *obd, int *process)
1052 switch (req->rq_reqmsg->opc) {
1053 case MDS_CONNECT: /* This will never get here, but for completeness. */
1054 case OST_CONNECT: /* This will never get here, but for completeness. */
1055 case MDS_DISCONNECT:
1056 case OST_DISCONNECT:
1061 case MDS_SYNC: /* used in unmounting */
1065 *process = target_queue_recovery_request(req, obd);
1069 DEBUG_REQ(D_ERROR, req, "not permitted during recovery");
1071 /* XXX what should we set rq_status to here? */
1072 req->rq_status = -EAGAIN;
1073 RETURN(ptlrpc_error(req));
1077 static char *reint_names[] = {
1078 [REINT_SETATTR] "setattr",
1079 [REINT_CREATE] "create",
1080 [REINT_LINK] "link",
1081 [REINT_UNLINK] "unlink",
1082 [REINT_RENAME] "rename",
1083 [REINT_OPEN] "open",
1086 static int mds_set_info(struct obd_export *exp, struct ptlrpc_request *req)
1093 key = lustre_msg_buf(req->rq_reqmsg, 0, 1);
1095 DEBUG_REQ(D_HA, req, "no set_info key");
1098 keylen = req->rq_reqmsg->buflens[0];
1100 val = lustre_msg_buf(req->rq_reqmsg, 1, sizeof(*val));
1102 DEBUG_REQ(D_HA, req, "no set_info val");
1106 rc = lustre_pack_reply(req, 0, NULL, NULL);
1109 req->rq_repmsg->status = 0;
1111 if (keylen < strlen("read-only") ||
1112 memcmp(key, "read-only", keylen) != 0)
1116 exp->exp_connect_flags |= OBD_CONNECT_RDONLY;
1118 exp->exp_connect_flags &= ~OBD_CONNECT_RDONLY;
1124 int mds_handle(struct ptlrpc_request *req)
1126 int should_process, fail = OBD_FAIL_MDS_ALL_REPLY_NET;
1128 struct mds_obd *mds = NULL; /* quell gcc overwarning */
1129 struct obd_device *obd = NULL;
1132 OBD_FAIL_RETURN(OBD_FAIL_MDS_ALL_REQUEST_NET | OBD_FAIL_ONCE, 0);
1134 LASSERT(current->journal_info == NULL);
1135 /* XXX identical to OST */
1136 if (req->rq_reqmsg->opc != MDS_CONNECT) {
1137 struct mds_export_data *med;
1138 int recovering, abort_recovery;
1140 if (req->rq_export == NULL) {
1141 CERROR("lustre_mds: operation %d on unconnected MDS\n",
1142 req->rq_reqmsg->opc);
1143 req->rq_status = -ENOTCONN;
1144 GOTO(out, rc = -ENOTCONN);
1147 med = &req->rq_export->exp_mds_data;
1148 obd = req->rq_export->exp_obd;
1151 /* sanity check: if the xid matches, the request must
1152 * be marked as a resent or replayed */
1153 if (req->rq_xid == med->med_mcd->mcd_last_xid)
1154 LASSERTF(lustre_msg_get_flags(req->rq_reqmsg) &
1155 (MSG_RESENT | MSG_REPLAY),
1156 "rq_xid "LPU64" matches last_xid, "
1157 "expected RESENT flag\n",
1159 /* else: note the opposite is not always true; a
1160 * RESENT req after a failover will usually not match
1161 * the last_xid, since it was likely never
1162 * committed. A REPLAYed request will almost never
1163 * match the last xid, however it could for a
1164 * committed, but still retained, open. */
1166 /* Check for aborted recovery. */
1167 spin_lock_bh(&obd->obd_processing_task_lock);
1168 abort_recovery = obd->obd_abort_recovery;
1169 recovering = obd->obd_recovering;
1170 spin_unlock_bh(&obd->obd_processing_task_lock);
1171 if (abort_recovery) {
1172 target_abort_recovery(obd);
1173 } else if (recovering) {
1174 rc = mds_filter_recovery_request(req, obd,
1176 if (rc || !should_process)
1181 switch (req->rq_reqmsg->opc) {
1183 DEBUG_REQ(D_INODE, req, "connect");
1184 OBD_FAIL_RETURN(OBD_FAIL_MDS_CONNECT_NET, 0);
1185 rc = target_handle_connect(req, mds_handle);
1187 /* Now that we have an export, set mds. */
1188 obd = req->rq_export->exp_obd;
1189 mds = mds_req2mds(req);
1193 case MDS_DISCONNECT:
1194 DEBUG_REQ(D_INODE, req, "disconnect");
1195 OBD_FAIL_RETURN(OBD_FAIL_MDS_DISCONNECT_NET, 0);
1196 rc = target_handle_disconnect(req);
1197 req->rq_status = rc; /* superfluous? */
1201 DEBUG_REQ(D_INODE, req, "getstatus");
1202 OBD_FAIL_RETURN(OBD_FAIL_MDS_GETSTATUS_NET, 0);
1203 rc = mds_getstatus(req);
1207 DEBUG_REQ(D_INODE, req, "getattr");
1208 OBD_FAIL_RETURN(OBD_FAIL_MDS_GETATTR_NET, 0);
1209 rc = mds_getattr(0, req);
1212 case MDS_GETATTR_NAME: {
1213 struct lustre_handle lockh;
1214 DEBUG_REQ(D_INODE, req, "getattr_name");
1215 OBD_FAIL_RETURN(OBD_FAIL_MDS_GETATTR_NAME_NET, 0);
1217 /* If this request gets a reconstructed reply, we won't be
1218 * acquiring any new locks in mds_getattr_name, so we don't
1222 rc = mds_getattr_name(0, req, &lockh);
1223 /* this non-intent call (from an ioctl) is special */
1224 req->rq_status = rc;
1225 if (rc == 0 && lockh.cookie)
1226 ldlm_lock_decref(&lockh, LCK_PR);
1230 DEBUG_REQ(D_INODE, req, "statfs");
1231 OBD_FAIL_RETURN(OBD_FAIL_MDS_STATFS_NET, 0);
1232 rc = mds_statfs(req);
1236 DEBUG_REQ(D_INODE, req, "readpage");
1237 OBD_FAIL_RETURN(OBD_FAIL_MDS_READPAGE_NET, 0);
1238 rc = mds_readpage(req);
1240 if (OBD_FAIL_CHECK_ONCE(OBD_FAIL_MDS_SENDPAGE)) {
1247 __u32 *opcp = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*opcp));
1249 int size[3] = {sizeof(struct mds_body), mds->mds_max_mdsize,
1250 mds->mds_max_cookiesize};
1253 /* NB only peek inside req now; mds_reint() will swab it */
1255 CERROR ("Can't inspect opcode\n");
1260 if (lustre_msg_swabbed (req->rq_reqmsg))
1263 DEBUG_REQ(D_INODE, req, "reint %d (%s)", opc,
1264 (opc < sizeof(reint_names) / sizeof(reint_names[0]) ||
1265 reint_names[opc] == NULL) ? reint_names[opc] :
1268 OBD_FAIL_RETURN(OBD_FAIL_MDS_REINT_NET, 0);
1270 if (opc == REINT_UNLINK || opc == REINT_RENAME)
1272 else if (opc == REINT_OPEN)
1277 rc = lustre_pack_reply(req, bufcount, size, NULL);
1281 rc = mds_reint(req, 0, NULL);
1282 fail = OBD_FAIL_MDS_REINT_NET_REP;
1287 DEBUG_REQ(D_INODE, req, "close");
1288 OBD_FAIL_RETURN(OBD_FAIL_MDS_CLOSE_NET, 0);
1289 rc = mds_close(req);
1292 case MDS_DONE_WRITING:
1293 DEBUG_REQ(D_INODE, req, "done_writing");
1294 OBD_FAIL_RETURN(OBD_FAIL_MDS_DONE_WRITING_NET, 0);
1295 rc = mds_done_writing(req);
1299 DEBUG_REQ(D_INODE, req, "pin");
1300 OBD_FAIL_RETURN(OBD_FAIL_MDS_PIN_NET, 0);
1305 DEBUG_REQ(D_INODE, req, "sync");
1306 OBD_FAIL_RETURN(OBD_FAIL_MDS_SYNC_NET, 0);
1311 DEBUG_REQ(D_INODE, req, "set_info");
1312 rc = mds_set_info(req->rq_export, req);
1315 case MDS_QUOTACHECK:
1316 DEBUG_REQ(D_INODE, req, "quotacheck");
1317 OBD_FAIL_RETURN(OBD_FAIL_MDS_QUOTACHECK_NET, 0);
1318 rc = mds_quotacheck(req);
1322 DEBUG_REQ(D_INODE, req, "quotactl");
1323 OBD_FAIL_RETURN(OBD_FAIL_MDS_QUOTACTL_NET, 0);
1324 rc = mds_quotactl(req);
1328 DEBUG_REQ(D_INODE, req, "ping");
1329 rc = target_handle_ping(req);
1332 case OBD_LOG_CANCEL:
1333 CDEBUG(D_INODE, "log cancel\n");
1334 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOG_CANCEL_NET, 0);
1335 rc = -ENOTSUPP; /* la la la */
1339 DEBUG_REQ(D_INODE, req, "enqueue");
1340 OBD_FAIL_RETURN(OBD_FAIL_LDLM_ENQUEUE, 0);
1341 rc = ldlm_handle_enqueue(req, ldlm_server_completion_ast,
1342 ldlm_server_blocking_ast, NULL);
1343 fail = OBD_FAIL_LDLM_REPLY;
1346 DEBUG_REQ(D_INODE, req, "convert");
1347 OBD_FAIL_RETURN(OBD_FAIL_LDLM_CONVERT, 0);
1348 rc = ldlm_handle_convert(req);
1350 case LDLM_BL_CALLBACK:
1351 case LDLM_CP_CALLBACK:
1352 DEBUG_REQ(D_INODE, req, "callback");
1353 CERROR("callbacks should not happen on MDS\n");
1355 OBD_FAIL_RETURN(OBD_FAIL_LDLM_BL_CALLBACK, 0);
1357 case LLOG_ORIGIN_HANDLE_CREATE:
1358 DEBUG_REQ(D_INODE, req, "llog_init");
1359 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
1360 rc = llog_origin_handle_create(req);
1362 case LLOG_ORIGIN_HANDLE_NEXT_BLOCK:
1363 DEBUG_REQ(D_INODE, req, "llog next block");
1364 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
1365 rc = llog_origin_handle_next_block(req);
1367 case LLOG_ORIGIN_HANDLE_READ_HEADER:
1368 DEBUG_REQ(D_INODE, req, "llog read header");
1369 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
1370 rc = llog_origin_handle_read_header(req);
1372 case LLOG_ORIGIN_HANDLE_CLOSE:
1373 DEBUG_REQ(D_INODE, req, "llog close");
1374 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
1375 rc = llog_origin_handle_close(req);
1378 DEBUG_REQ(D_INODE, req, "llog catinfo");
1379 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
1380 rc = llog_catinfo(req);
1383 req->rq_status = -ENOTSUPP;
1384 rc = ptlrpc_error(req);
1388 LASSERT(current->journal_info == NULL);
1390 /* If we're DISCONNECTing, the mds_export_data is already freed */
1391 if (!rc && req->rq_reqmsg->opc != MDS_DISCONNECT) {
1392 struct mds_export_data *med = &req->rq_export->exp_mds_data;
1393 req->rq_repmsg->last_xid =
1394 le64_to_cpu(med->med_mcd->mcd_last_xid);
1396 target_committed_to_req(req);
1402 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_LAST_REPLAY) {
1403 if (obd && obd->obd_recovering) {
1404 DEBUG_REQ(D_HA, req, "LAST_REPLAY, queuing reply");
1405 return target_queue_final_reply(req, rc);
1407 /* Lost a race with recovery; let the error path DTRT. */
1408 rc = req->rq_status = -ENOTCONN;
1411 target_send_reply(req, rc, fail);
1415 /* Update the server data on disk. This stores the new mount_count and
1416 * also the last_rcvd value to disk. If we don't have a clean shutdown,
1417 * then the server last_rcvd value may be less than that of the clients.
1418 * This will alert us that we may need to do client recovery.
1420 * Also assumes for mds_last_transno that we are not modifying it (no locking).
1422 int mds_update_server_data(struct obd_device *obd, int force_sync)
1424 struct mds_obd *mds = &obd->u.mds;
1425 struct mds_server_data *msd = mds->mds_server_data;
1426 struct file *filp = mds->mds_rcvd_filp;
1427 struct lvfs_run_ctxt saved;
1432 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1433 msd->msd_last_transno = cpu_to_le64(mds->mds_last_transno);
1435 CDEBUG(D_SUPER, "MDS mount_count is "LPU64", last_transno is "LPU64"\n",
1436 mds->mds_mount_count, mds->mds_last_transno);
1437 rc = fsfilt_write_record(obd, filp, msd, sizeof(*msd), &off,force_sync);
1439 CERROR("error writing MDS server data: rc = %d\n", rc);
1440 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1446 /* mount the file system (secretly) */
1447 static int mds_setup(struct obd_device *obd, obd_count len, void *buf)
1449 struct lprocfs_static_vars lvars;
1450 struct lustre_cfg* lcfg = buf;
1451 char *options = NULL;
1452 struct mds_obd *mds = &obd->u.mds;
1453 struct vfsmount *mnt;
1459 /* setup 1:/dev/loop/0 2:ext3 3:mdsA 4:errors=remount-ro,iopen_nopriv*/
1461 if (lcfg->lcfg_bufcount < 3)
1462 RETURN(rc = -EINVAL);
1464 if (LUSTRE_CFG_BUFLEN(lcfg, 1) == 0 || LUSTRE_CFG_BUFLEN(lcfg, 2) == 0)
1465 RETURN(rc = -EINVAL);
1467 obd->obd_fsops = fsfilt_get_ops(lustre_cfg_string(lcfg, 2));
1468 if (IS_ERR(obd->obd_fsops))
1469 RETURN(rc = PTR_ERR(obd->obd_fsops));
1471 page = __get_free_page(GFP_KERNEL);
1475 options = (char *)page;
1476 memset(options, 0, PAGE_SIZE);
1478 /* here we use "iopen_nopriv" hardcoded, because it affects MDS utility
1479 * and the rest of options are passed by mount options. Probably this
1480 * should be moved to somewhere else like startup scripts or lconf. */
1481 sprintf(options, "iopen_nopriv");
1483 if (LUSTRE_CFG_BUFLEN(lcfg, 4) > 0 && lustre_cfg_buf(lcfg, 4))
1484 sprintf(options + strlen(options), ",%s",
1485 lustre_cfg_string(lcfg, 4));
1487 //FIXME mount was already done in lustre_fill_super,
1488 //we just need to access it
1489 mnt = do_kern_mount(lustre_cfg_string(lcfg, 2), 0,
1490 lustre_cfg_string(lcfg, 1), (void *)options);
1494 CERROR("do_kern_mount failed: rc = %d\n", rc);
1498 CDEBUG(D_SUPER, "%s: mnt = %p\n", lustre_cfg_string(lcfg, 1), mnt);
1500 LASSERT(!lvfs_check_rdonly(lvfs_sbdev(mnt->mnt_sb)));
1502 sema_init(&mds->mds_orphan_recovery_sem, 1);
1503 sema_init(&mds->mds_epoch_sem, 1);
1504 spin_lock_init(&mds->mds_transno_lock);
1505 mds->mds_max_mdsize = sizeof(struct lov_mds_md);
1506 mds->mds_max_cookiesize = sizeof(struct llog_cookie);
1508 sprintf(ns_name, "mds-%s", obd->obd_uuid.uuid);
1509 obd->obd_namespace = ldlm_namespace_new(ns_name, LDLM_NAMESPACE_SERVER);
1510 if (obd->obd_namespace == NULL) {
1512 GOTO(err_put, rc = -ENOMEM);
1514 ldlm_register_intent(obd->obd_namespace, mds_intent_policy);
1516 rc = mds_fs_setup(obd, mnt);
1518 CERROR("%s: MDS filesystem method init failed: rc = %d\n",
1523 rc = llog_start_commit_thread();
1527 if (lcfg->lcfg_bufcount >= 4 && LUSTRE_CFG_BUFLEN(lcfg, 3) > 0) {
1530 generate_random_uuid(uuid);
1531 class_uuid_unparse(uuid, &mds->mds_lov_uuid);
1533 OBD_ALLOC(mds->mds_profile, LUSTRE_CFG_BUFLEN(lcfg, 3));
1534 if (mds->mds_profile == NULL)
1535 GOTO(err_fs, rc = -ENOMEM);
1537 strncpy(mds->mds_profile, lustre_cfg_string(lcfg, 3),
1538 LUSTRE_CFG_BUFLEN(lcfg, 3));
1542 ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
1543 "mds_ldlm_client", &obd->obd_ldlm_client);
1544 obd->obd_replayable = 1;
1546 mds_quota_setup(mds);
1548 rc = mds_postsetup(obd);
1552 lprocfs_init_vars(mds, &lvars);
1553 lprocfs_obd_setup(obd, lvars.obd_vars);
1555 if (obd->obd_recovering) {
1556 LCONSOLE_WARN("MDT %s now serving %s, but will be in recovery "
1557 "until %d %s reconnect, or if no clients "
1558 "reconnect for %d:%.02d; during that time new "
1559 "clients will not be allowed to connect. "
1560 "Recovery progress can be monitored by watching "
1561 "/proc/fs/lustre/mds/%s/recovery_status.\n",
1563 lustre_cfg_string(lcfg, 1),
1564 obd->obd_recoverable_clients,
1565 (obd->obd_recoverable_clients == 1)
1566 ? "client" : "clients",
1567 (int)(OBD_RECOVERY_TIMEOUT / HZ) / 60,
1568 (int)(OBD_RECOVERY_TIMEOUT / HZ) % 60,
1571 LCONSOLE_INFO("MDT %s now serving %s with recovery %s.\n",
1573 lustre_cfg_string(lcfg, 1),
1574 obd->obd_replayable ? "enabled" : "disabled");
1578 ping_evictor_start();
1583 /* No extra cleanup needed for llog_init_commit_thread() */
1584 mds_fs_cleanup(obd);
1586 ldlm_namespace_free(obd->obd_namespace, 0);
1587 obd->obd_namespace = NULL;
1590 mntput(mds->mds_vfsmnt);
1594 fsfilt_put_ops(obd->obd_fsops);
1598 static int mds_postsetup(struct obd_device *obd)
1600 struct mds_obd *mds = &obd->u.mds;
1604 rc = llog_setup(obd, LLOG_CONFIG_ORIG_CTXT, obd, 0, NULL,
1609 if (mds->mds_profile) {
1610 struct lvfs_run_ctxt saved;
1611 struct lustre_profile *lprof;
1612 struct config_llog_instance cfg;
1614 cfg.cfg_instance = NULL;
1615 cfg.cfg_uuid = mds->mds_lov_uuid;
1616 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1617 rc = class_config_parse_llog(llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT),
1618 mds->mds_profile, &cfg);
1619 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1624 LCONSOLE_ERROR("%s: the profile %s could not be read. "
1625 "If you recently installed a new "
1626 "version of Lustre, you may need to "
1627 "re-run 'lconf --write_conf "
1628 "<yourconfig>.xml' command line before "
1629 "restarting the MDS.\n",
1630 obd->obd_name, mds->mds_profile);
1637 lprof = class_get_profile(mds->mds_profile);
1638 if (lprof == NULL) {
1639 CERROR("No profile found: %s\n", mds->mds_profile);
1640 GOTO(err_cleanup, rc = -ENOENT);
1642 rc = mds_lov_connect(obd, lprof->lp_osc);
1644 GOTO(err_cleanup, rc);
1652 llog_cleanup(llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT));
1656 int mds_postrecov(struct obd_device *obd)
1658 struct mds_obd *mds = &obd->u.mds;
1665 LASSERT(!obd->obd_recovering);
1666 LASSERT(llog_get_context(obd, LLOG_MDS_OST_ORIG_CTXT) != NULL);
1668 /* set nextid first, so we are sure it happens */
1669 rc = mds_lov_set_nextid(obd);
1671 CERROR ("%s: mds_lov_set_nextid failed\n",
1676 /* clean PENDING dir */
1677 rc = mds_cleanup_orphans(obd);
1684 rc = obd_set_info(mds->mds_osc_exp, strlen("mds_conn"), "mds_conn",
1689 rc = llog_connect(llog_get_context(obd, LLOG_MDS_OST_ORIG_CTXT),
1690 obd->u.mds.mds_lov_desc.ld_tgt_count,
1693 CERROR("%s: failed at llog_origin_connect: %d\n",
1698 /* remove the orphaned precreated objects */
1699 rc = mds_lov_clearorphans(mds, NULL /* all OSTs */);
1705 RETURN(rc < 0 ? rc : item);
1708 /* cleanup all llogging subsystems */
1709 rc = obd_llog_finish(obd, mds->mds_lov_desc.ld_tgt_count);
1711 CERROR("%s: failed to cleanup llogging subsystems\n",
1716 int mds_lov_clean(struct obd_device *obd)
1718 struct mds_obd *mds = &obd->u.mds;
1720 if (mds->mds_profile) {
1722 struct config_llog_instance cfg;
1723 struct lvfs_run_ctxt saved;
1724 int len = strlen(mds->mds_profile) + sizeof("-clean") + 1;
1726 OBD_ALLOC(cln_prof, len);
1727 sprintf(cln_prof, "%s-clean", mds->mds_profile);
1729 cfg.cfg_instance = NULL;
1730 cfg.cfg_uuid = mds->mds_lov_uuid;
1732 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1733 class_config_parse_llog(llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT),
1735 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1737 OBD_FREE(cln_prof, len);
1738 OBD_FREE(mds->mds_profile, strlen(mds->mds_profile) + 1);
1739 mds->mds_profile = NULL;
1744 static int mds_precleanup(struct obd_device *obd, int stage)
1751 mds_lov_set_cleanup_flags(obd);
1752 target_cleanup_recovery(obd);
1755 mds_lov_disconnect(obd);
1757 llog_cleanup(llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT));
1758 rc = obd_llog_finish(obd, 0);
1763 static int mds_cleanup(struct obd_device *obd)
1765 struct mds_obd *mds = &obd->u.mds;
1766 lvfs_sbdev_type save_dev;
1767 int must_relock = 0;
1770 ping_evictor_stop();
1772 if (mds->mds_sb == NULL)
1774 save_dev = lvfs_sbdev(mds->mds_sb);
1776 if (mds->mds_osc_exp)
1777 /* lov export was disconnected by mds_lov_clean;
1778 we just need to drop our ref */
1779 class_export_put(mds->mds_osc_exp);
1781 lprocfs_obd_cleanup(obd);
1783 mds_quota_cleanup(mds);
1785 mds_update_server_data(obd, 1);
1786 if (mds->mds_lov_objids != NULL) {
1787 OBD_FREE(mds->mds_lov_objids,
1788 mds->mds_lov_desc.ld_tgt_count * sizeof(obd_id));
1790 mds_fs_cleanup(obd);
1792 /* 2 seems normal on mds, (may_umount() also expects 2
1793 fwiw), but we only see 1 at this point in obdfilter. */
1794 if (atomic_read(&obd->u.mds.mds_vfsmnt->mnt_count) > 2)
1795 CERROR("%s: mount busy, mnt_count %d != 2\n", obd->obd_name,
1796 atomic_read(&obd->u.mds.mds_vfsmnt->mnt_count));
1798 /* We can only unlock kernel if we are in the context of sys_ioctl,
1799 otherwise we never called lock_kernel */
1800 if (kernel_locked()) {
1805 mntput(mds->mds_vfsmnt);
1808 ldlm_namespace_free(obd->obd_namespace, obd->obd_force);
1810 spin_lock_bh(&obd->obd_processing_task_lock);
1811 if (obd->obd_recovering) {
1812 target_cancel_recovery_timer(obd);
1813 obd->obd_recovering = 0;
1815 spin_unlock_bh(&obd->obd_processing_task_lock);
1817 lvfs_clear_rdonly(save_dev);
1822 fsfilt_put_ops(obd->obd_fsops);
1824 LCONSOLE_INFO("MDT %s has stopped.\n", obd->obd_name);
1829 static void fixup_handle_for_resent_req(struct ptlrpc_request *req,
1830 struct ldlm_lock *new_lock,
1831 struct ldlm_lock **old_lock,
1832 struct lustre_handle *lockh)
1834 struct obd_export *exp = req->rq_export;
1835 struct obd_device *obd = exp->exp_obd;
1836 struct ldlm_request *dlmreq =
1837 lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*dlmreq));
1838 struct lustre_handle remote_hdl = dlmreq->lock_handle1;
1839 struct list_head *iter;
1841 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
1844 l_lock(&obd->obd_namespace->ns_lock);
1845 list_for_each(iter, &exp->exp_ldlm_data.led_held_locks) {
1846 struct ldlm_lock *lock;
1847 lock = list_entry(iter, struct ldlm_lock, l_export_chain);
1848 if (lock == new_lock)
1850 if (lock->l_remote_handle.cookie == remote_hdl.cookie) {
1851 lockh->cookie = lock->l_handle.h_cookie;
1852 LDLM_DEBUG(lock, "restoring lock cookie");
1853 DEBUG_REQ(D_HA, req, "restoring lock cookie "LPX64,
1856 *old_lock = LDLM_LOCK_GET(lock);
1857 l_unlock(&obd->obd_namespace->ns_lock);
1861 l_unlock(&obd->obd_namespace->ns_lock);
1863 /* If the xid matches, then we know this is a resent request,
1864 * and allow it. (It's probably an OPEN, for which we don't
1867 le64_to_cpu(exp->exp_mds_data.med_mcd->mcd_last_xid))
1870 /* This remote handle isn't enqueued, so we never received or
1871 * processed this request. Clear MSG_RESENT, because it can
1872 * be handled like any normal request now. */
1874 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
1876 DEBUG_REQ(D_HA, req, "no existing lock with rhandle "LPX64,
1880 int intent_disposition(struct ldlm_reply *rep, int flag)
1884 return (rep->lock_policy_res1 & flag);
1887 void intent_set_disposition(struct ldlm_reply *rep, int flag)
1891 rep->lock_policy_res1 |= flag;
1894 static int mds_intent_policy(struct ldlm_namespace *ns,
1895 struct ldlm_lock **lockp, void *req_cookie,
1896 ldlm_mode_t mode, int flags, void *data)
1898 struct ptlrpc_request *req = req_cookie;
1899 struct ldlm_lock *lock = *lockp;
1900 struct ldlm_intent *it;
1901 struct mds_obd *mds = &req->rq_export->exp_obd->u.mds;
1902 struct ldlm_reply *rep;
1903 struct lustre_handle lockh = { 0 };
1904 struct ldlm_lock *new_lock = NULL;
1905 int rc, offset = 2, repsize[4] = {sizeof(struct ldlm_reply),
1906 sizeof(struct mds_body),
1907 mds->mds_max_mdsize,
1908 mds->mds_max_cookiesize};
1911 LASSERT(req != NULL);
1913 if (req->rq_reqmsg->bufcount <= 1) {
1914 /* No intent was provided */
1915 int size = sizeof(struct ldlm_reply);
1916 rc = lustre_pack_reply(req, 1, &size, NULL);
1921 it = lustre_swab_reqbuf(req, 1, sizeof(*it), lustre_swab_ldlm_intent);
1923 CERROR("Intent missing\n");
1924 RETURN(req->rq_status = -EFAULT);
1927 LDLM_DEBUG(lock, "intent policy, opc: %s", ldlm_it2str(it->opc));
1929 rc = lustre_pack_reply(req, it->opc == IT_UNLINK ? 4 : 3, repsize,
1932 RETURN(req->rq_status = rc);
1934 rep = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*rep));
1935 intent_set_disposition(rep, DISP_IT_EXECD);
1938 /* execute policy */
1939 switch ((long)it->opc) {
1941 case IT_CREAT|IT_OPEN:
1942 fixup_handle_for_resent_req(req, lock, NULL, &lockh);
1943 /* XXX swab here to assert that an mds_open reint
1944 * packet is following */
1945 rep->lock_policy_res2 = mds_reint(req, offset, &lockh);
1947 /* We abort the lock if the lookup was negative and
1948 * we did not make it to the OPEN portion */
1949 if (!intent_disposition(rep, DISP_LOOKUP_EXECD))
1950 RETURN(ELDLM_LOCK_ABORTED);
1951 if (intent_disposition(rep, DISP_LOOKUP_NEG) &&
1952 !intent_disposition(rep, DISP_OPEN_OPEN))
1954 RETURN(ELDLM_LOCK_ABORTED);
1959 fixup_handle_for_resent_req(req, lock, &new_lock, &lockh);
1960 rep->lock_policy_res2 = mds_getattr_name(offset, req, &lockh);
1961 /* FIXME: LDLM can set req->rq_status. MDS sets
1962 policy_res{1,2} with disposition and status.
1963 - replay: returns 0 & req->status is old status
1964 - otherwise: returns req->status */
1965 if (intent_disposition(rep, DISP_LOOKUP_NEG))
1966 rep->lock_policy_res2 = 0;
1967 if (!intent_disposition(rep, DISP_LOOKUP_POS) ||
1968 rep->lock_policy_res2)
1969 RETURN(ELDLM_LOCK_ABORTED);
1970 if (req->rq_status != 0) {
1972 rep->lock_policy_res2 = req->rq_status;
1973 RETURN(ELDLM_LOCK_ABORTED);
1977 CERROR("Unhandled intent "LPD64"\n", it->opc);
1981 /* By this point, whatever function we called above must have either
1982 * filled in 'lockh', been an intent replay, or returned an error. We
1983 * want to allow replayed RPCs to not get a lock, since we would just
1984 * drop it below anyways because lock replay is done separately by the
1985 * client afterwards. For regular RPCs we want to give the new lock to
1986 * the client instead of whatever lock it was about to get. */
1987 if (new_lock == NULL)
1988 new_lock = ldlm_handle2lock(&lockh);
1989 if (new_lock == NULL && (flags & LDLM_FL_INTENT_ONLY))
1992 LASSERTF(new_lock != NULL, "op "LPX64" lockh "LPX64"\n",
1993 it->opc, lockh.cookie);
1995 /* If we've already given this lock to a client once, then we should
1996 * have no readers or writers. Otherwise, we should have one reader
1997 * _or_ writer ref (which will be zeroed below) before returning the
1998 * lock to a client. */
1999 if (new_lock->l_export == req->rq_export) {
2000 LASSERT(new_lock->l_readers + new_lock->l_writers == 0);
2002 LASSERT(new_lock->l_export == NULL);
2003 LASSERT(new_lock->l_readers + new_lock->l_writers == 1);
2008 if (new_lock->l_export == req->rq_export) {
2009 /* Already gave this to the client, which means that we
2010 * reconstructed a reply. */
2011 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
2013 RETURN(ELDLM_LOCK_REPLACED);
2016 /* Fixup the lock to be given to the client */
2017 l_lock(&new_lock->l_resource->lr_namespace->ns_lock);
2018 new_lock->l_readers = 0;
2019 new_lock->l_writers = 0;
2021 new_lock->l_export = class_export_get(req->rq_export);
2022 list_add(&new_lock->l_export_chain,
2023 &new_lock->l_export->exp_ldlm_data.led_held_locks);
2025 new_lock->l_blocking_ast = lock->l_blocking_ast;
2026 new_lock->l_completion_ast = lock->l_completion_ast;
2028 memcpy(&new_lock->l_remote_handle, &lock->l_remote_handle,
2029 sizeof(lock->l_remote_handle));
2031 new_lock->l_flags &= ~LDLM_FL_LOCAL;
2033 LDLM_LOCK_PUT(new_lock);
2034 l_unlock(&new_lock->l_resource->lr_namespace->ns_lock);
2036 RETURN(ELDLM_LOCK_REPLACED);
2039 static int mdt_setup(struct obd_device *obd, obd_count len, void *buf)
2041 struct mds_obd *mds = &obd->u.mds;
2042 struct lprocfs_static_vars lvars;
2046 lprocfs_init_vars(mdt, &lvars);
2047 lprocfs_obd_setup(obd, lvars.obd_vars);
2050 ptlrpc_init_svc(MDS_NBUFS, MDS_BUFSIZE, MDS_MAXREQSIZE,
2051 MDS_REQUEST_PORTAL, MDC_REPLY_PORTAL,
2052 MDS_SERVICE_WATCHDOG_TIMEOUT,
2053 mds_handle, "mds", obd->obd_proc_entry, NULL);
2055 if (!mds->mds_service) {
2056 CERROR("failed to start service\n");
2057 GOTO(err_lprocfs, rc = -ENOMEM);
2060 rc = ptlrpc_start_n_threads(obd, mds->mds_service, MDT_NUM_THREADS,
2063 GOTO(err_thread, rc);
2065 mds->mds_setattr_service =
2066 ptlrpc_init_svc(MDS_NBUFS, MDS_BUFSIZE, MDS_MAXREQSIZE,
2067 MDS_SETATTR_PORTAL, MDC_REPLY_PORTAL,
2068 MDS_SERVICE_WATCHDOG_TIMEOUT,
2069 mds_handle, "mds_setattr",
2070 obd->obd_proc_entry, NULL);
2071 if (!mds->mds_setattr_service) {
2072 CERROR("failed to start getattr service\n");
2073 GOTO(err_thread, rc = -ENOMEM);
2076 rc = ptlrpc_start_n_threads(obd, mds->mds_setattr_service,
2077 MDT_NUM_THREADS, "ll_mdt_attr");
2079 GOTO(err_thread2, rc);
2081 mds->mds_readpage_service =
2082 ptlrpc_init_svc(MDS_NBUFS, MDS_BUFSIZE, MDS_MAXREQSIZE,
2083 MDS_READPAGE_PORTAL, MDC_REPLY_PORTAL,
2084 MDS_SERVICE_WATCHDOG_TIMEOUT,
2085 mds_handle, "mds_readpage",
2086 obd->obd_proc_entry, NULL);
2087 if (!mds->mds_readpage_service) {
2088 CERROR("failed to start readpage service\n");
2089 GOTO(err_thread2, rc = -ENOMEM);
2092 rc = ptlrpc_start_n_threads(obd, mds->mds_readpage_service,
2093 MDT_NUM_THREADS, "ll_mdt_rdpg");
2096 GOTO(err_thread3, rc);
2101 ptlrpc_unregister_service(mds->mds_readpage_service);
2103 ptlrpc_unregister_service(mds->mds_setattr_service);
2105 ptlrpc_unregister_service(mds->mds_service);
2107 lprocfs_obd_cleanup(obd);
2111 static int mdt_cleanup(struct obd_device *obd)
2113 struct mds_obd *mds = &obd->u.mds;
2116 ptlrpc_unregister_service(mds->mds_readpage_service);
2117 ptlrpc_unregister_service(mds->mds_setattr_service);
2118 ptlrpc_unregister_service(mds->mds_service);
2120 lprocfs_obd_cleanup(obd);
2125 static struct dentry *mds_lvfs_fid2dentry(__u64 id, __u32 gen, __u64 gr,
2128 struct obd_device *obd = data;
2131 fid.generation = gen;
2132 return mds_fid2dentry(&obd->u.mds, &fid, NULL);
2135 struct lvfs_callback_ops mds_lvfs_ops = {
2136 l_fid2dentry: mds_lvfs_fid2dentry,
2139 /* use obd ops to offer management infrastructure */
2140 static struct obd_ops mds_obd_ops = {
2141 .o_owner = THIS_MODULE,
2142 .o_connect = mds_connect,
2143 .o_init_export = mds_init_export,
2144 .o_destroy_export = mds_destroy_export,
2145 .o_disconnect = mds_disconnect,
2146 .o_setup = mds_setup,
2147 .o_precleanup = mds_precleanup,
2148 .o_cleanup = mds_cleanup,
2149 .o_postrecov = mds_postrecov,
2150 .o_statfs = mds_obd_statfs,
2151 .o_iocontrol = mds_iocontrol,
2152 .o_create = mds_obd_create,
2153 .o_destroy = mds_obd_destroy,
2154 .o_llog_init = mds_llog_init,
2155 .o_llog_finish = mds_llog_finish,
2156 .o_notify = mds_notify,
2159 static struct obd_ops mdt_obd_ops = {
2160 .o_owner = THIS_MODULE,
2161 .o_setup = mdt_setup,
2162 .o_cleanup = mdt_cleanup,
2165 static int __init mds_init(void)
2168 struct lprocfs_static_vars lvars;
2170 rc = lustre_dquot_init();
2174 lprocfs_init_vars(mds, &lvars);
2175 class_register_type(&mds_obd_ops, lvars.module_vars, LUSTRE_MDS_NAME);
2176 lprocfs_init_vars(mdt, &lvars);
2177 class_register_type(&mdt_obd_ops, lvars.module_vars, LUSTRE_MDT_NAME);
2182 static void /*__exit*/ mds_exit(void)
2184 lustre_dquot_exit();
2186 class_unregister_type(LUSTRE_MDS_NAME);
2187 class_unregister_type(LUSTRE_MDT_NAME);
2190 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
2191 MODULE_DESCRIPTION("Lustre Metadata Server (MDS)");
2192 MODULE_LICENSE("GPL");
2194 module_init(mds_init);
2195 module_exit(mds_exit);