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-2003 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>
57 #include "mds_internal.h"
59 static int mds_cleanup(struct obd_device *obd, int flags);
61 static int mds_bulk_timeout(void *data)
63 struct ptlrpc_bulk_desc *desc = data;
64 struct obd_export *exp = desc->bd_export;
66 DEBUG_REQ(D_ERROR, desc->bd_req,"bulk send timed out: evicting %s@%s\n",
67 exp->exp_client_uuid.uuid,
68 exp->exp_connection->c_remote_uuid.uuid);
69 ptlrpc_fail_export(exp);
70 ptlrpc_abort_bulk (desc);
74 /* Assumes caller has already pushed into the kernel filesystem context */
75 static int mds_sendpage(struct ptlrpc_request *req, struct file *file,
76 loff_t offset, int count)
78 struct ptlrpc_bulk_desc *desc;
79 struct l_wait_info lwi;
81 int rc = 0, npages, i, tmpcount, tmpsize = 0;
84 LASSERT((offset & (PAGE_SIZE - 1)) == 0); /* I'm dubious about this */
86 npages = (count + PAGE_SIZE - 1) >> PAGE_SHIFT;
87 OBD_ALLOC(pages, sizeof(*pages) * npages);
89 GOTO(out, rc = -ENOMEM);
91 desc = ptlrpc_prep_bulk_exp (req, BULK_PUT_SOURCE, MDS_BULK_PORTAL);
93 GOTO(out_free, rc = -ENOMEM);
95 for (i = 0, tmpcount = count; i < npages; i++, tmpcount -= tmpsize) {
96 tmpsize = tmpcount > PAGE_SIZE ? PAGE_SIZE : tmpcount;
98 pages[i] = alloc_pages(GFP_KERNEL, 0);
100 GOTO(cleanup_buf, rc = -ENOMEM);
102 rc = ptlrpc_prep_bulk_page(desc, pages[i], 0, tmpsize);
104 GOTO(cleanup_buf, rc);
107 for (i = 0, tmpcount = count; i < npages; i++, tmpcount -= tmpsize) {
108 tmpsize = tmpcount > PAGE_SIZE ? PAGE_SIZE : tmpcount;
109 CDEBUG(D_EXT2, "reading %u@%llu from dir %lu (size %llu)\n",
110 tmpsize, offset, file->f_dentry->d_inode->i_ino,
111 file->f_dentry->d_inode->i_size);
113 rc = fsfilt_readpage(req->rq_export->exp_obd, file,
114 page_address(pages[i]), tmpsize, &offset);
117 GOTO(cleanup_buf, rc = -EIO);
120 rc = ptlrpc_bulk_put(desc);
122 GOTO(cleanup_buf, rc);
124 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE)) {
125 CERROR("obd_fail_loc=%x, fail operation rc=%d\n",
126 OBD_FAIL_MDS_SENDPAGE, rc);
127 ptlrpc_abort_bulk(desc);
128 GOTO(cleanup_buf, rc);
131 lwi = LWI_TIMEOUT(obd_timeout * HZ / 4, mds_bulk_timeout, desc);
132 rc = l_wait_event(desc->bd_waitq, ptlrpc_bulk_complete (desc), &lwi);
134 LASSERT (rc == -ETIMEDOUT);
135 GOTO(cleanup_buf, rc);
140 for (i = 0; i < npages; i++)
142 __free_pages(pages[i], 0);
144 ptlrpc_free_bulk(desc);
146 OBD_FREE(pages, sizeof(*pages) * npages);
151 /* only valid locked dentries or errors should be returned */
152 struct dentry *mds_fid2locked_dentry(struct obd_device *obd, struct ll_fid *fid,
153 struct vfsmount **mnt, int lock_mode,
154 struct lustre_handle *lockh,
155 char *name, int namelen)
157 struct mds_obd *mds = &obd->u.mds;
158 struct dentry *de = mds_fid2dentry(mds, fid, mnt), *retval = de;
159 struct ldlm_res_id res_id = { .name = {0} };
166 res_id.name[0] = de->d_inode->i_ino;
167 res_id.name[1] = de->d_inode->i_generation;
168 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace, NULL,
169 res_id, LDLM_PLAIN, NULL, 0, lock_mode,
170 &flags, ldlm_completion_ast,
171 mds_blocking_ast, NULL, lockh);
172 if (rc != ELDLM_OK) {
174 retval = ERR_PTR(-EIO); /* XXX translate ldlm code */
180 #ifndef DCACHE_DISCONNECTED
181 #define DCACHE_DISCONNECTED DCACHE_NFSD_DISCONNECTED
185 /* Look up an entry by inode number. */
186 /* this function ONLY returns valid dget'd dentries with an initialized inode
188 struct dentry *mds_fid2dentry(struct mds_obd *mds, struct ll_fid *fid,
189 struct vfsmount **mnt)
192 unsigned long ino = fid->id;
193 __u32 generation = fid->generation;
195 struct dentry *result;
198 RETURN(ERR_PTR(-ESTALE));
200 snprintf(fid_name, sizeof(fid_name), "0x%lx", ino);
202 CDEBUG(D_DENTRY, "--> mds_fid2dentry: ino %lu, gen %u, sb %p\n",
203 ino, generation, mds->mds_sb);
205 /* under ext3 this is neither supposed to return bad inodes
207 result = ll_lookup_one_len(fid_name, mds->mds_fid_de, strlen(fid_name));
211 inode = result->d_inode;
213 RETURN(ERR_PTR(-ENOENT));
215 if (generation && inode->i_generation != generation) {
216 /* we didn't find the right inode.. */
217 CERROR("bad inode %lu, link: %lu ct: %d or generation %u/%u\n",
218 inode->i_ino, (unsigned long)inode->i_nlink,
219 atomic_read(&inode->i_count), inode->i_generation,
222 RETURN(ERR_PTR(-ENOENT));
226 *mnt = mds->mds_vfsmnt;
234 /* Establish a connection to the MDS.
236 * This will set up an export structure for the client to hold state data
237 * about that client, like open files, the last operation number it did
238 * on the server, etc.
240 static int mds_connect(struct lustre_handle *conn, struct obd_device *obd,
241 struct obd_uuid *cluuid)
243 struct obd_export *exp;
244 struct mds_export_data *med; /* */
245 struct mds_client_data *mcd;
246 int rc, abort_recovery;
249 if (!conn || !obd || !cluuid)
252 /* Check for aborted recovery. */
253 spin_lock_bh(&obd->obd_processing_task_lock);
254 abort_recovery = obd->obd_abort_recovery;
255 spin_unlock_bh(&obd->obd_processing_task_lock);
257 target_abort_recovery(obd);
259 /* XXX There is a small race between checking the list and adding a
260 * new connection for the same UUID, but the real threat (list
261 * corruption when multiple different clients connect) is solved.
263 * There is a second race between adding the export to the list,
264 * and filling in the client data below. Hence skipping the case
265 * of NULL mcd above. We should already be controlling multiple
266 * connects at the client, and we can't hold the spinlock over
267 * memory allocations without risk of deadlocking.
269 rc = class_connect(conn, obd, cluuid);
272 exp = class_conn2export(conn);
274 med = &exp->exp_mds_data;
276 OBD_ALLOC(mcd, sizeof(*mcd));
278 CERROR("mds: out of memory for client data\n");
279 GOTO(out, rc = -ENOMEM);
282 memcpy(mcd->mcd_uuid, cluuid, sizeof(mcd->mcd_uuid));
285 rc = mds_client_add(obd, &obd->u.mds, med, -1);
290 OBD_FREE(mcd, sizeof(*mcd));
291 class_disconnect(exp, 0);
293 class_export_put(exp);
298 static int mds_init_export(struct obd_export *exp)
300 struct mds_export_data *med = &exp->exp_mds_data;
302 INIT_LIST_HEAD(&med->med_open_head);
303 spin_lock_init(&med->med_open_lock);
307 static int mds_destroy_export(struct obd_export *export)
309 struct mds_export_data *med;
310 struct obd_device *obd = export->exp_obd;
311 struct obd_run_ctxt saved;
315 med = &export->exp_mds_data;
316 target_destroy_export(export);
318 push_ctxt(&saved, &obd->obd_ctxt, NULL);
319 /* Close any open files (which may also cause orphan unlinking). */
320 spin_lock(&med->med_open_lock);
321 while (!list_empty(&med->med_open_head)) {
322 struct list_head *tmp = med->med_open_head.next;
323 struct mds_file_data *mfd =
324 list_entry(tmp, struct mds_file_data, mfd_list);
325 BDEVNAME_DECLARE_STORAGE(btmp);
327 /* bug 1579: fix force-closing for 2.5 */
328 struct dentry *dentry = mfd->mfd_dentry;
330 list_del(&mfd->mfd_list);
331 spin_unlock(&med->med_open_lock);
333 CERROR("force closing client file handle for %*s (%s:%lu)\n",
334 dentry->d_name.len, dentry->d_name.name,
335 ll_bdevname(dentry->d_inode->i_sb, btmp),
336 dentry->d_inode->i_ino);
337 rc = mds_mfd_close(NULL, obd, mfd,
338 !(export->exp_flags & OBD_OPT_FAILOVER));
341 CDEBUG(D_INODE, "Error closing file: %d\n", rc);
342 spin_lock(&med->med_open_lock);
344 spin_unlock(&med->med_open_lock);
345 pop_ctxt(&saved, &obd->obd_ctxt, NULL);
347 mds_client_free(export, !(export->exp_flags & OBD_OPT_FAILOVER));
352 static int mds_disconnect(struct obd_export *export, int flags)
354 unsigned long irqflags;
358 ldlm_cancel_locks_for_export(export);
360 spin_lock_irqsave(&export->exp_lock, irqflags);
361 export->exp_flags = flags;
362 spin_unlock_irqrestore(&export->exp_lock, irqflags);
364 rc = class_disconnect(export, flags);
368 static int mds_getstatus(struct ptlrpc_request *req)
370 struct mds_obd *mds = mds_req2mds(req);
371 struct mds_body *body;
372 int rc, size = sizeof(*body);
375 rc = lustre_pack_reply(req, 1, &size, NULL);
376 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_GETSTATUS_PACK)) {
377 CERROR("mds: out of memory for message: size=%d\n", size);
378 req->rq_status = -ENOMEM; /* superfluous? */
382 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
383 memcpy(&body->fid1, &mds->mds_rootfid, sizeof(body->fid1));
385 /* the last_committed and last_xid fields are filled in for all
386 * replies already - no need to do so here also.
391 int mds_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
392 void *data, int flag)
397 if (flag == LDLM_CB_CANCELING) {
398 /* Don't need to do anything here. */
402 /* XXX layering violation! -phil */
403 l_lock(&lock->l_resource->lr_namespace->ns_lock);
404 /* Get this: if mds_blocking_ast is racing with ldlm_intent_policy,
405 * such that mds_blocking_ast is called just before l_i_p takes the
406 * ns_lock, then by the time we get the lock, we might not be the
407 * correct blocking function anymore. So check, and return early, if
409 if (lock->l_blocking_ast != mds_blocking_ast) {
410 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
414 lock->l_flags |= LDLM_FL_CBPENDING;
415 do_ast = (!lock->l_readers && !lock->l_writers);
416 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
419 struct lustre_handle lockh;
422 LDLM_DEBUG(lock, "already unused, calling ldlm_cli_cancel");
423 ldlm_lock2handle(lock, &lockh);
424 rc = ldlm_cli_cancel(&lockh);
426 CERROR("ldlm_cli_cancel: %d\n", rc);
428 LDLM_DEBUG(lock, "Lock still has references, will be "
434 /* Call with lock=1 if you want mds_pack_md to take the i_sem.
435 * Call with lock=0 if the caller has already taken the i_sem. */
436 int mds_pack_md(struct obd_device *obd, struct lustre_msg *msg, int offset,
437 struct mds_body *body, struct inode *inode, int lock)
439 struct mds_obd *mds = &obd->u.mds;
445 lmm = lustre_msg_buf(msg, offset, 0);
447 /* Some problem with getting eadata when I sized the reply
449 CDEBUG(D_INFO, "no space reserved for inode %lu MD\n",
453 lmm_size = msg->buflens[offset];
455 /* I don't really like this, but it is a sanity check on the client
456 * MD request. However, if the client doesn't know how much space
457 * to reserve for the MD, it shouldn't be bad to have too much space.
459 if (lmm_size > mds->mds_max_mdsize) {
460 CWARN("Reading MD for inode %lu of %d bytes > max %d\n",
461 inode->i_ino, lmm_size, mds->mds_max_mdsize);
467 rc = fsfilt_get_md(obd, inode, lmm, lmm_size);
471 CERROR("Error %d reading eadata for ino %lu\n",
475 rc = mds_convert_lov_ea(obd, inode, lmm, lmm_size);
479 body->valid |= OBD_MD_FLEASIZE;
480 body->eadatasize = lmm_size;
487 static int mds_getattr_internal(struct obd_device *obd, struct dentry *dentry,
488 struct ptlrpc_request *req,
489 struct mds_body *reqbody, int reply_off)
491 struct mds_body *body;
492 struct inode *inode = dentry->d_inode;
499 body = lustre_msg_buf(req->rq_repmsg, reply_off, sizeof(*body));
500 LASSERT(body != NULL); /* caller prepped reply */
502 mds_pack_inode2fid(&body->fid1, inode);
503 mds_pack_inode2body(body, inode);
505 if (S_ISREG(inode->i_mode) && (reqbody->valid & OBD_MD_FLEASIZE) != 0) {
506 rc = mds_pack_md(obd, req->rq_repmsg, reply_off + 1, body,
509 /* If we have LOV EA data, the OST holds size, atime, mtime */
510 if (!(body->valid & OBD_MD_FLEASIZE))
511 body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
512 OBD_MD_FLATIME | OBD_MD_FLMTIME);
513 } else if (S_ISLNK(inode->i_mode) &&
514 (reqbody->valid & OBD_MD_LINKNAME) != 0) {
515 char *symname = lustre_msg_buf(req->rq_repmsg, reply_off + 1,0);
518 LASSERT (symname != NULL); /* caller prepped reply */
519 len = req->rq_repmsg->buflens[reply_off + 1];
521 rc = inode->i_op->readlink(dentry, symname, len);
523 CERROR("readlink failed: %d\n", rc);
524 } else if (rc != len - 1) {
525 CERROR ("Unexpected readlink rc %d: expecting %d\n",
529 CDEBUG(D_INODE, "read symlink dest %s\n", symname);
530 body->valid |= OBD_MD_LINKNAME;
531 body->eadatasize = rc + 1;
532 symname[rc] = 0; /* NULL terminate */
540 static int mds_getattr_pack_msg(struct ptlrpc_request *req, struct inode *inode,
543 struct mds_obd *mds = mds_req2mds(req);
544 struct mds_body *body;
545 int rc = 0, size[2] = {sizeof(*body)}, bufcount = 1;
548 body = lustre_msg_buf(req->rq_reqmsg, offset, sizeof (*body));
549 LASSERT(body != NULL); /* checked by caller */
550 LASSERT_REQSWABBED(req, offset); /* swabbed by caller */
552 if (S_ISREG(inode->i_mode) && (body->valid & OBD_MD_FLEASIZE)) {
555 rc = fsfilt_get_md(req->rq_export->exp_obd, inode, NULL, 0);
557 CDEBUG(D_INODE, "got %d bytes MD data for inode %lu\n",
561 CERROR("error getting inode %lu MD: rc = %d\n",
564 } else if (rc > mds->mds_max_mdsize) {
566 CERROR("MD size %d larger than maximum possible %u\n",
567 rc, mds->mds_max_mdsize);
572 } else if (S_ISLNK(inode->i_mode) && (body->valid & OBD_MD_LINKNAME)) {
573 if (inode->i_size + 1 != body->eadatasize)
574 CERROR("symlink size: %Lu, reply space: %d\n",
575 inode->i_size + 1, body->eadatasize);
576 size[bufcount] = MIN(inode->i_size + 1, body->eadatasize);
578 CDEBUG(D_INODE, "symlink size: %Lu, reply space: %d\n",
579 inode->i_size + 1, body->eadatasize);
582 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK)) {
583 CERROR("failed MDS_GETATTR_PACK test\n");
584 req->rq_status = -ENOMEM;
585 GOTO(out, rc = -ENOMEM);
588 rc = lustre_pack_reply(req, bufcount, size, NULL);
590 CERROR("out of memory\n");
591 GOTO(out, req->rq_status = rc);
599 static int mds_getattr_name(int offset, struct ptlrpc_request *req,
600 struct lustre_handle *child_lockh)
602 struct obd_device *obd = req->rq_export->exp_obd;
603 struct ldlm_reply *rep = NULL;
604 struct obd_run_ctxt saved;
605 struct mds_body *body;
606 struct dentry *de = NULL, *dchild = NULL;
609 struct ldlm_res_id child_res_id = { .name = {0} };
610 struct lustre_handle parent_lockh;
612 int flags = 0, rc = 0, cleanup_phase = 0;
616 LASSERT(!strcmp(obd->obd_type->typ_name, "mds"));
618 /* Swab now, before anyone looks inside the request */
620 body = lustre_swab_reqbuf(req, offset, sizeof(*body),
621 lustre_swab_mds_body);
623 CERROR("Can't swab mds_body\n");
624 GOTO(cleanup, rc = -EFAULT);
627 LASSERT_REQSWAB(req, offset + 1);
628 name = lustre_msg_string(req->rq_reqmsg, offset + 1, 0);
630 CERROR("Can't unpack name\n");
631 GOTO(cleanup, rc = -EFAULT);
633 namesize = req->rq_reqmsg->buflens[offset + 1];
635 LASSERT (offset == 0 || offset == 2);
636 /* if requests were at offset 2, the getattr reply goes back at 1 */
638 rep = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*rep));
642 uc.ouc_fsuid = body->fsuid;
643 uc.ouc_fsgid = body->fsgid;
644 uc.ouc_cap = body->capability;
645 uc.ouc_suppgid1 = body->suppgid;
646 uc.ouc_suppgid2 = -1;
647 push_ctxt(&saved, &obd->obd_ctxt, &uc);
648 /* Step 1: Lookup/lock parent */
649 intent_set_disposition(rep, DISP_LOOKUP_EXECD);
650 de = mds_fid2locked_dentry(obd, &body->fid1, NULL, LCK_PR,
651 &parent_lockh, name, namesize - 1);
653 GOTO(cleanup, rc = PTR_ERR(de));
657 cleanup_phase = 1; /* parent dentry and lock */
659 CDEBUG(D_INODE, "parent ino %lu, name %s\n", dir->i_ino, name);
661 /* Step 2: Lookup child */
663 if (body->valid == OBD_MD_FLID) {
664 struct mds_body *mds_reply;
665 int size = sizeof(*mds_reply);
667 // The user requested ONLY the inode number, so do a raw lookup
668 rc = lustre_pack_reply(req, 1, &size, NULL);
670 CERROR("out of memory\n");
674 rc = dir->i_op->lookup_raw(dir, name, namesize - 1, &inum);
676 mds_reply = lustre_msg_buf(req->rq_repmsg, offset,
678 mds_reply->fid1.id = inum;
679 mds_reply->valid = OBD_MD_FLID;
684 dchild = ll_lookup_one_len(name, de, namesize - 1);
685 if (IS_ERR(dchild)) {
686 CDEBUG(D_INODE, "child lookup error %ld\n", PTR_ERR(dchild));
687 GOTO(cleanup, rc = PTR_ERR(dchild));
689 cleanup_phase = 2; /* child dentry */
691 if (dchild->d_inode == NULL) {
692 intent_set_disposition(rep, DISP_LOOKUP_NEG);
693 /* in the intent case, the policy clears this error:
694 the disposition is enough */
695 GOTO(cleanup, rc = -ENOENT);
697 intent_set_disposition(rep, DISP_LOOKUP_POS);
700 /* Step 3: Lock child */
701 /* fixup_handle_for_resent_req might have set the child_lockh for us, if
702 * the lock was already granted for this request on the last
704 if (child_lockh->cookie == 0) {
705 child_res_id.name[0] = dchild->d_inode->i_ino;
706 child_res_id.name[1] = dchild->d_inode->i_generation;
707 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace, NULL,
708 child_res_id, LDLM_PLAIN, NULL, 0, LCK_PR,
709 &flags, ldlm_completion_ast,
710 mds_blocking_ast, NULL, child_lockh);
711 if (rc != ELDLM_OK) {
712 CERROR("ldlm_cli_enqueue: %d\n", rc);
713 GOTO(cleanup, rc = -EIO);
717 cleanup_phase = 3; /* child lock */
719 if (req->rq_repmsg == NULL) {
720 rc = mds_getattr_pack_msg(req, dchild->d_inode, offset);
722 CERROR ("mds_getattr_pack_msg: %d\n", rc);
727 rc = mds_getattr_internal(obd, dchild, req, body, offset);
728 GOTO(cleanup, rc); /* returns the lock to the client */
731 switch (cleanup_phase) {
734 ldlm_lock_decref(child_lockh, LCK_PR);
740 ldlm_lock_decref(&parent_lockh, LCK_PR);
742 ldlm_put_lock_into_req(req, &parent_lockh, LCK_PR);
747 pop_ctxt(&saved, &obd->obd_ctxt, &uc);
751 static int mds_getattr(int offset, struct ptlrpc_request *req)
753 struct mds_obd *mds = mds_req2mds(req);
754 struct obd_device *obd = req->rq_export->exp_obd;
755 struct obd_run_ctxt saved;
757 struct mds_body *body;
762 body = lustre_swab_reqbuf (req, offset, sizeof (*body),
763 lustre_swab_mds_body);
765 CERROR ("Can't unpack body\n");
769 uc.ouc_fsuid = body->fsuid;
770 uc.ouc_fsgid = body->fsgid;
771 uc.ouc_cap = body->capability;
772 push_ctxt(&saved, &obd->obd_ctxt, &uc);
773 de = mds_fid2dentry(mds, &body->fid1, NULL);
775 rc = req->rq_status = -ENOENT;
776 GOTO(out_pop, PTR_ERR(de));
779 rc = mds_getattr_pack_msg(req, de->d_inode, offset);
781 CERROR ("mds_getattr_pack_msg: %d\n", rc);
785 req->rq_status = mds_getattr_internal(obd, de, req, body, 0);
790 pop_ctxt(&saved, &obd->obd_ctxt, &uc);
795 static int mds_obd_statfs(struct obd_device *obd, struct obd_statfs *osfs,
796 unsigned long max_age)
798 return fsfilt_statfs(obd, obd->u.mds.mds_sb, osfs);
801 static int mds_statfs(struct ptlrpc_request *req)
803 struct obd_device *obd = req->rq_export->exp_obd;
804 int rc, size = sizeof(struct obd_statfs);
807 rc = lustre_pack_reply(req, 1, &size, NULL);
808 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_STATFS_PACK)) {
809 CERROR("mds: statfs lustre_pack_reply failed: rc = %d\n", rc);
813 /* We call this so that we can cache a bit - 1 jiffie worth */
814 rc = obd_statfs(obd, lustre_msg_buf(req->rq_repmsg,0,size),jiffies-HZ);
816 CERROR("mds_obd_statfs failed: rc %d\n", rc);
826 static int mds_sync(struct ptlrpc_request *req)
828 struct obd_device *obd = req->rq_export->exp_obd;
829 struct mds_obd *mds = &obd->u.mds;
830 struct mds_body *body;
831 int rc, size = sizeof(*body);
834 body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof(*body));
836 GOTO(out, rc = -EPROTO);
838 rc = lustre_pack_reply(req, 1, &size, NULL);
839 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK)) {
840 CERROR("fsync lustre_pack_reply failed: rc = %d\n", rc);
844 if (body->fid1.id == 0) {
845 /* a fid of zero is taken to mean "sync whole filesystem" */
846 rc = fsfilt_sync(obd, mds->mds_sb);
850 /* just any file to grab fsync method - "file" arg unused */
851 struct file *file = mds->mds_rcvd_filp;
854 de = mds_fid2dentry(mds, &body->fid1, NULL);
856 GOTO(out, rc = PTR_ERR(de));
858 rc = file->f_op->fsync(NULL, de, 1);
863 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
864 mds_pack_inode2fid(&body->fid1, de->d_inode);
865 mds_pack_inode2body(body, de->d_inode);
872 static int mds_readpage(struct ptlrpc_request *req)
874 struct obd_device *obd = req->rq_export->exp_obd;
875 struct vfsmount *mnt;
878 struct mds_body *body, *repbody;
879 struct lustre_handle lockh;
880 struct obd_run_ctxt saved;
881 int rc, size = sizeof(*repbody);
885 rc = lustre_pack_reply(req, 1, &size, NULL);
886 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK)) {
887 CERROR("mds: out of memory\n");
888 GOTO(out, rc = -ENOMEM);
891 body = lustre_swab_reqbuf (req, 0, sizeof (*body),
892 lustre_swab_mds_body);
894 GOTO (out, rc = -EFAULT);
896 uc.ouc_fsuid = body->fsuid;
897 uc.ouc_fsgid = body->fsgid;
898 uc.ouc_cap = body->capability;
899 push_ctxt(&saved, &obd->obd_ctxt, &uc);
900 de = mds_fid2locked_dentry(obd, &body->fid1, &mnt, LCK_PR,
903 GOTO(out_pop, rc = PTR_ERR(de));
905 CDEBUG(D_INODE, "ino %lu\n", de->d_inode->i_ino);
907 file = dentry_open(de, mnt, O_RDONLY | O_LARGEFILE);
908 /* note: in case of an error, dentry_open puts dentry */
910 GOTO(out_lock, rc = PTR_ERR(file));
912 /* body->size is actually the offset -eeb */
913 if ((body->size & (de->d_inode->i_blksize - 1)) != 0) {
914 CERROR("offset "LPU64" not on a block boundary of %lu\n",
915 body->size, de->d_inode->i_blksize);
916 GOTO(out_file, rc = -EFAULT);
919 /* body->nlink is actually the #bytes to read -eeb */
920 if (body->nlink & (de->d_inode->i_blksize - 1)) {
921 CERROR("size %u is not multiple of blocksize %lu\n",
922 body->nlink, de->d_inode->i_blksize);
923 GOTO(out_file, rc = -EFAULT);
926 repbody = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*repbody));
927 repbody->size = file->f_dentry->d_inode->i_size;
928 repbody->valid = OBD_MD_FLSIZE;
930 /* to make this asynchronous make sure that the handling function
931 doesn't send a reply when this function completes. Instead a
932 callback function would send the reply */
933 /* body->size is actually the offset -eeb */
934 rc = mds_sendpage(req, file, body->size, body->nlink);
939 ldlm_lock_decref(&lockh, LCK_PR);
941 pop_ctxt(&saved, &obd->obd_ctxt, &uc);
947 int mds_reint(struct ptlrpc_request *req, int offset,
948 struct lustre_handle *lockh)
950 struct mds_update_record *rec; /* 116 bytes on the stack? no sir! */
953 OBD_ALLOC(rec, sizeof(*rec));
957 rc = mds_update_unpack(req, offset, rec);
958 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNPACK)) {
959 CERROR("invalid record\n");
960 GOTO(out, req->rq_status = -EINVAL);
962 /* rc will be used to interrupt a for loop over multiple records */
963 rc = mds_reint_rec(rec, offset, req, lockh);
965 OBD_FREE(rec, sizeof(*rec));
969 static int mds_filter_recovery_request(struct ptlrpc_request *req,
970 struct obd_device *obd, int *process)
972 switch (req->rq_reqmsg->opc) {
973 case MDS_CONNECT: /* This will never get here, but for completeness. */
974 case OST_CONNECT: /* This will never get here, but for completeness. */
981 case MDS_SYNC: /* used in unmounting */
985 *process = target_queue_recovery_request(req, obd);
989 DEBUG_REQ(D_ERROR, req, "not permitted during recovery");
991 /* XXX what should we set rq_status to here? */
992 req->rq_status = -EAGAIN;
993 RETURN(ptlrpc_error(req));
997 static char *reint_names[] = {
998 [REINT_SETATTR] "setattr",
999 [REINT_CREATE] "create",
1000 [REINT_LINK] "link",
1001 [REINT_UNLINK] "unlink",
1002 [REINT_RENAME] "rename",
1003 [REINT_OPEN] "open",
1006 void mds_steal_ack_locks(struct obd_export *exp,
1007 struct ptlrpc_request *req)
1009 unsigned long flags;
1010 struct ptlrpc_request *oldrep = exp->exp_outstanding_reply;
1014 memcpy(req->rq_ack_locks, oldrep->rq_ack_locks,
1015 sizeof req->rq_ack_locks);
1016 spin_lock_irqsave (&req->rq_lock, flags);
1017 oldrep->rq_resent = 1;
1018 wake_up(&oldrep->rq_reply_waitq);
1019 spin_unlock_irqrestore (&req->rq_lock, flags);
1020 DEBUG_REQ(D_HA, oldrep, "stole locks from");
1021 DEBUG_REQ(D_HA, req, "stole locks for");
1024 int mds_handle(struct ptlrpc_request *req)
1028 struct mds_obd *mds = NULL; /* quell gcc overwarning */
1029 struct obd_device *obd = NULL;
1032 OBD_FAIL_RETURN(OBD_FAIL_MDS_ALL_REQUEST_NET | OBD_FAIL_ONCE, 0);
1034 LASSERT(current->journal_info == NULL);
1035 /* XXX identical to OST */
1036 if (req->rq_reqmsg->opc != MDS_CONNECT) {
1037 struct mds_export_data *med;
1038 int recovering, abort_recovery;
1040 if (req->rq_export == NULL) {
1041 CERROR("lustre_mds: operation %d on unconnected MDS\n",
1042 req->rq_reqmsg->opc);
1043 req->rq_status = -ENOTCONN;
1044 GOTO(out, rc = -ENOTCONN);
1047 med = &req->rq_export->exp_mds_data;
1048 obd = req->rq_export->exp_obd;
1051 /* Check for aborted recovery. */
1052 spin_lock_bh(&obd->obd_processing_task_lock);
1053 abort_recovery = obd->obd_abort_recovery;
1054 recovering = obd->obd_recovering;
1055 spin_unlock_bh(&obd->obd_processing_task_lock);
1056 if (abort_recovery) {
1057 target_abort_recovery(obd);
1058 } else if (recovering) {
1059 rc = mds_filter_recovery_request(req, obd,
1061 if (rc || !should_process)
1066 switch (req->rq_reqmsg->opc) {
1068 DEBUG_REQ(D_INODE, req, "connect");
1069 OBD_FAIL_RETURN(OBD_FAIL_MDS_CONNECT_NET, 0);
1070 rc = target_handle_connect(req, mds_handle);
1072 /* Now that we have an export, set mds. */
1073 mds = mds_req2mds(req);
1076 case MDS_DISCONNECT:
1077 DEBUG_REQ(D_INODE, req, "disconnect");
1078 OBD_FAIL_RETURN(OBD_FAIL_MDS_DISCONNECT_NET, 0);
1079 rc = target_handle_disconnect(req);
1080 req->rq_status = rc; /* superfluous? */
1084 DEBUG_REQ(D_INODE, req, "getstatus");
1085 OBD_FAIL_RETURN(OBD_FAIL_MDS_GETSTATUS_NET, 0);
1086 rc = mds_getstatus(req);
1090 DEBUG_REQ(D_INODE, req, "getattr");
1091 OBD_FAIL_RETURN(OBD_FAIL_MDS_GETATTR_NET, 0);
1092 rc = mds_getattr(0, req);
1095 case MDS_GETATTR_NAME: {
1096 struct lustre_handle lockh;
1097 DEBUG_REQ(D_INODE, req, "getattr_name");
1098 OBD_FAIL_RETURN(OBD_FAIL_MDS_GETATTR_NAME_NET, 0);
1100 /* If this request gets a reconstructed reply, we won't be
1101 * acquiring any new locks in mds_getattr_name, so we don't
1105 rc = mds_getattr_name(0, req, &lockh);
1106 /* this non-intent call (from an ioctl) is special */
1107 req->rq_status = rc;
1108 if (rc == 0 && lockh.cookie)
1109 ldlm_lock_decref(&lockh, LCK_PR);
1113 DEBUG_REQ(D_INODE, req, "statfs");
1114 OBD_FAIL_RETURN(OBD_FAIL_MDS_STATFS_NET, 0);
1115 rc = mds_statfs(req);
1119 DEBUG_REQ(D_INODE, req, "readpage");
1120 OBD_FAIL_RETURN(OBD_FAIL_MDS_READPAGE_NET, 0);
1121 rc = mds_readpage(req);
1123 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
1128 __u32 *opcp = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*opcp));
1130 int size[3] = {sizeof(struct mds_body), mds->mds_max_mdsize,
1131 mds->mds_max_cookiesize};
1134 /* NB only peek inside req now; mds_reint() will swab it */
1136 CERROR ("Can't inspect opcode\n");
1141 if (lustre_msg_swabbed (req->rq_reqmsg))
1144 DEBUG_REQ(D_INODE, req, "reint %d (%s)", opc,
1145 (opc < sizeof(reint_names) / sizeof(reint_names[0]) ||
1146 reint_names[opc] == NULL) ? reint_names[opc] :
1149 OBD_FAIL_RETURN(OBD_FAIL_MDS_REINT_NET, 0);
1151 if (opc == REINT_UNLINK)
1153 else if (opc == REINT_OPEN || opc == REINT_RENAME)
1158 rc = lustre_pack_reply(req, bufcount, size, NULL);
1162 rc = mds_reint(req, 0, NULL);
1163 OBD_FAIL_RETURN(OBD_FAIL_MDS_REINT_NET_REP, 0);
1168 DEBUG_REQ(D_INODE, req, "close");
1169 OBD_FAIL_RETURN(OBD_FAIL_MDS_CLOSE_NET, 0);
1170 rc = mds_close(req);
1173 case MDS_DONE_WRITING:
1174 DEBUG_REQ(D_INODE, req, "done_writing");
1175 OBD_FAIL_RETURN(OBD_FAIL_MDS_DONE_WRITING_NET, 0);
1176 rc = mds_done_writing(req);
1180 DEBUG_REQ(D_INODE, req, "pin");
1181 OBD_FAIL_RETURN(OBD_FAIL_MDS_PIN_NET, 0);
1186 DEBUG_REQ(D_INODE, req, "sync");
1187 OBD_FAIL_RETURN(OBD_FAIL_MDS_SYNC_NET, 0);
1192 DEBUG_REQ(D_INODE, req, "ping");
1193 rc = target_handle_ping(req);
1196 case OBD_LOG_CANCEL:
1197 CDEBUG(D_INODE, "log cancel\n");
1198 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOG_CANCEL_NET, 0);
1199 rc = -ENOTSUPP; /* la la la */
1203 DEBUG_REQ(D_INODE, req, "enqueue");
1204 OBD_FAIL_RETURN(OBD_FAIL_LDLM_ENQUEUE, 0);
1205 rc = ldlm_handle_enqueue(req, ldlm_server_completion_ast,
1206 ldlm_server_blocking_ast);
1209 DEBUG_REQ(D_INODE, req, "convert");
1210 OBD_FAIL_RETURN(OBD_FAIL_LDLM_CONVERT, 0);
1211 rc = ldlm_handle_convert(req);
1213 case LDLM_BL_CALLBACK:
1214 case LDLM_CP_CALLBACK:
1215 DEBUG_REQ(D_INODE, req, "callback");
1216 CERROR("callbacks should not happen on MDS\n");
1218 OBD_FAIL_RETURN(OBD_FAIL_LDLM_BL_CALLBACK, 0);
1220 case LLOG_ORIGIN_HANDLE_CREATE:
1221 DEBUG_REQ(D_INODE, req, "llog_init");
1222 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
1223 rc = llog_origin_handle_create(req);
1225 case LLOG_ORIGIN_HANDLE_NEXT_BLOCK:
1226 DEBUG_REQ(D_INODE, req, "llog next block");
1227 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
1228 rc = llog_origin_handle_next_block(req);
1230 case LLOG_ORIGIN_HANDLE_READ_HEADER:
1231 DEBUG_REQ(D_INODE, req, "llog read header");
1232 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
1233 rc = llog_origin_handle_read_header(req);
1235 case LLOG_ORIGIN_HANDLE_CLOSE:
1236 DEBUG_REQ(D_INODE, req, "llog close");
1237 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
1238 rc = llog_origin_handle_close(req);
1241 req->rq_status = -ENOTSUPP;
1242 rc = ptlrpc_error(req);
1246 LASSERT(current->journal_info == NULL);
1250 /* If we're DISCONNECTing, the mds_export_data is already freed */
1251 if (!rc && req->rq_reqmsg->opc != MDS_DISCONNECT) {
1252 struct mds_export_data *med = &req->rq_export->exp_mds_data;
1253 struct obd_device *obd = list_entry(mds, struct obd_device,
1255 req->rq_repmsg->last_xid =
1256 le64_to_cpu(med->med_mcd->mcd_last_xid);
1258 if (!obd->obd_no_transno) {
1259 req->rq_repmsg->last_committed =
1260 obd->obd_last_committed;
1262 DEBUG_REQ(D_IOCTL, req,
1263 "not sending last_committed update");
1265 CDEBUG(D_INFO, "last_transno "LPU64", last_committed "LPU64
1267 mds->mds_last_transno, obd->obd_last_committed,
1272 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_LAST_REPLAY) {
1273 if (obd && obd->obd_recovering) {
1274 DEBUG_REQ(D_HA, req, "LAST_REPLAY, queuing reply");
1275 return target_queue_final_reply(req, rc);
1277 /* Lost a race with recovery; let the error path DTRT. */
1278 rc = req->rq_status = -ENOTCONN;
1281 target_send_reply(req, rc, OBD_FAIL_MDS_ALL_REPLY_NET);
1285 /* Update the server data on disk. This stores the new mount_count and
1286 * also the last_rcvd value to disk. If we don't have a clean shutdown,
1287 * then the server last_rcvd value may be less than that of the clients.
1288 * This will alert us that we may need to do client recovery.
1290 * Also assumes for mds_last_transno that we are not modifying it (no locking).
1292 int mds_update_server_data(struct obd_device *obd, int force_sync)
1294 struct mds_obd *mds = &obd->u.mds;
1295 struct mds_server_data *msd = mds->mds_server_data;
1296 struct file *filp = mds->mds_rcvd_filp;
1297 struct obd_run_ctxt saved;
1302 push_ctxt(&saved, &obd->obd_ctxt, NULL);
1303 msd->msd_last_transno = cpu_to_le64(mds->mds_last_transno);
1304 msd->msd_mount_count = cpu_to_le64(mds->mds_mount_count);
1306 CDEBUG(D_SUPER, "MDS mount_count is "LPU64", last_transno is "LPU64"\n",
1307 mds->mds_mount_count, mds->mds_last_transno);
1308 rc = fsfilt_write_record(obd, filp, msd, sizeof(*msd), &off,force_sync);
1310 CERROR("error writing MDS server data: rc = %d\n", rc);
1311 pop_ctxt(&saved, &obd->obd_ctxt, NULL);
1316 /* mount the file system (secretly) */
1317 static int mds_setup(struct obd_device *obd, obd_count len, void *buf)
1319 struct lustre_cfg* lcfg = buf;
1320 struct mds_obd *mds = &obd->u.mds;
1321 struct vfsmount *mnt;
1326 dev_clear_rdonly(2);
1328 if (!lcfg->lcfg_inlbuf1 || !lcfg->lcfg_inlbuf2)
1329 RETURN(rc = -EINVAL);
1331 obd->obd_fsops = fsfilt_get_ops(lcfg->lcfg_inlbuf2);
1332 if (IS_ERR(obd->obd_fsops))
1333 RETURN(rc = PTR_ERR(obd->obd_fsops));
1335 if (!(page = __get_free_page(GFP_KERNEL)))
1338 memset((void *)page, 0, PAGE_SIZE);
1339 sprintf((char *)page, "iopen_nopriv");
1341 mnt = do_kern_mount(lcfg->lcfg_inlbuf2, 0,
1342 lcfg->lcfg_inlbuf1, (void *)page);
1346 CERROR("do_kern_mount failed: rc = %d\n", rc);
1350 CDEBUG(D_SUPER, "%s: mnt = %p\n", lcfg->lcfg_inlbuf1, mnt);
1352 sema_init(&mds->mds_orphan_recovery_sem, 1);
1353 sema_init(&mds->mds_epoch_sem, 1);
1354 spin_lock_init(&mds->mds_transno_lock);
1355 mds->mds_max_mdsize = sizeof(struct lov_mds_md);
1356 mds->mds_max_cookiesize = sizeof(struct llog_cookie);
1358 obd->obd_namespace = ldlm_namespace_new("mds_server",
1359 LDLM_NAMESPACE_SERVER);
1360 if (obd->obd_namespace == NULL) {
1361 mds_cleanup(obd, 0);
1362 GOTO(err_put, rc = -ENOMEM);
1365 rc = mds_fs_setup(obd, mnt);
1367 CERROR("MDS filesystem method init failed: rc = %d\n", rc);
1371 rc = llog_start_commit_thread();
1376 if (lcfg->lcfg_inllen3 > 0 && lcfg->lcfg_inlbuf3) {
1379 generate_random_uuid(uuid);
1380 class_uuid_unparse(uuid, &mds->mds_lov_uuid);
1382 OBD_ALLOC(mds->mds_profile, lcfg->lcfg_inllen3);
1383 if (mds->mds_profile == NULL)
1384 GOTO(err_fs, rc = -ENOMEM);
1386 memcpy(mds->mds_profile, lcfg->lcfg_inlbuf3,
1387 lcfg->lcfg_inllen3);
1391 ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
1392 "mds_ldlm_client", &obd->obd_ldlm_client);
1393 obd->obd_replayable = 1;
1398 /* No extra cleanup needed for llog_init_commit_thread() */
1399 mds_fs_cleanup(obd, 0);
1401 ldlm_namespace_free(obd->obd_namespace, 0);
1402 obd->obd_namespace = NULL;
1405 mntput(mds->mds_vfsmnt);
1409 fsfilt_put_ops(obd->obd_fsops);
1413 static int mds_postsetup(struct obd_device *obd)
1415 struct mds_obd *mds = &obd->u.mds;
1420 rc = llog_setup(obd, LLOG_CONFIG_ORIG_CTXT, obd, 0, NULL,
1425 if (mds->mds_profile) {
1426 struct obd_run_ctxt saved;
1427 struct lustre_profile *lprof;
1428 struct config_llog_instance cfg;
1430 cfg.cfg_instance = NULL;
1431 cfg.cfg_uuid = mds->mds_lov_uuid;
1432 push_ctxt(&saved, &obd->obd_ctxt, NULL);
1433 rc = class_config_parse_llog(llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT),
1434 mds->mds_profile, &cfg);
1435 pop_ctxt(&saved, &obd->obd_ctxt, NULL);
1439 lprof = class_get_profile(mds->mds_profile);
1440 if (lprof == NULL) {
1441 CERROR("No profile found: %s\n", mds->mds_profile);
1442 GOTO(err_cleanup, rc = -ENOENT);
1444 rc = mds_lov_connect(obd, lprof->lp_osc);
1446 GOTO(err_cleanup, rc);
1454 llog_cleanup(llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT));
1458 static int mds_postrecov(struct obd_device *obd)
1463 LASSERT(!obd->obd_recovering);
1465 #ifdef ENABLE_ORPHANS
1466 rc = llog_connect(llog_get_context(obd, LLOG_UNLINK_ORIG_CTXT),
1467 obd->u.mds.mds_lov_desc.ld_tgt_count, NULL, NULL);
1469 CERROR("faild at llog_origin_connect: %d\n", rc);
1472 rc = mds_cleanup_orphans(obd);
1474 rc2 = mds_lov_set_nextid(obd);
1480 int mds_lov_clean(struct obd_device *obd)
1482 struct mds_obd *mds = &obd->u.mds;
1484 if (mds->mds_profile) {
1486 struct config_llog_instance cfg;
1487 struct obd_run_ctxt saved;
1488 int len = strlen(mds->mds_profile) + sizeof("-clean") + 1;
1490 OBD_ALLOC(cln_prof, len);
1491 sprintf(cln_prof, "%s-clean", mds->mds_profile);
1493 cfg.cfg_instance = NULL;
1494 cfg.cfg_uuid = mds->mds_lov_uuid;
1496 push_ctxt(&saved, &obd->obd_ctxt, NULL);
1497 class_config_parse_llog(llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT),
1499 pop_ctxt(&saved, &obd->obd_ctxt, NULL);
1501 OBD_FREE(cln_prof, len);
1502 OBD_FREE(mds->mds_profile, strlen(mds->mds_profile) + 1);
1503 mds->mds_profile = NULL;
1508 static int mds_precleanup(struct obd_device *obd, int flags)
1513 mds_lov_disconnect(obd, flags);
1515 llog_cleanup(llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT));
1519 static int mds_cleanup(struct obd_device *obd, int flags)
1521 struct mds_obd *mds = &obd->u.mds;
1524 if (mds->mds_sb == NULL)
1527 mds_update_server_data(obd, 1);
1528 if (mds->mds_lov_objids != NULL) {
1529 OBD_FREE(mds->mds_lov_objids,
1530 mds->mds_lov_desc.ld_tgt_count * sizeof(obd_id));
1532 mds_fs_cleanup(obd, flags);
1536 /* 2 seems normal on mds, (may_umount() also expects 2
1537 fwiw), but we only see 1 at this point in obdfilter. */
1538 if (atomic_read(&obd->u.mds.mds_vfsmnt->mnt_count) > 2)
1539 CERROR("%s: mount busy, mnt_count %d != 2\n", obd->obd_name,
1540 atomic_read(&obd->u.mds.mds_vfsmnt->mnt_count));
1542 mntput(mds->mds_vfsmnt);
1546 ldlm_namespace_free(obd->obd_namespace, flags & OBD_OPT_FORCE);
1548 if (obd->obd_recovering)
1549 target_cancel_recovery_timer(obd);
1551 dev_clear_rdonly(2);
1552 fsfilt_put_ops(obd->obd_fsops);
1557 static void fixup_handle_for_resent_req(struct ptlrpc_request *req,
1558 struct ldlm_lock *new_lock,
1559 struct lustre_handle *lockh)
1561 struct obd_export *exp = req->rq_export;
1562 struct obd_device *obd = exp->exp_obd;
1563 struct ldlm_request *dlmreq =
1564 lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*dlmreq));
1565 struct lustre_handle remote_hdl = dlmreq->lock_handle1;
1566 struct list_head *iter;
1568 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
1571 l_lock(&obd->obd_namespace->ns_lock);
1572 list_for_each(iter, &exp->exp_ldlm_data.led_held_locks) {
1573 struct ldlm_lock *lock;
1574 lock = list_entry(iter, struct ldlm_lock, l_export_chain);
1575 if (lock == new_lock)
1577 if (lock->l_remote_handle.cookie == remote_hdl.cookie) {
1578 lockh->cookie = lock->l_handle.h_cookie;
1579 DEBUG_REQ(D_HA, req, "restoring lock cookie "LPX64,
1581 l_unlock(&obd->obd_namespace->ns_lock);
1585 l_unlock(&obd->obd_namespace->ns_lock);
1587 /* This remote handle isn't enqueued, so we never received or
1588 * processed this request. Clear MSG_RESENT, because it can
1589 * be handled like any normal request now. */
1591 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
1593 DEBUG_REQ(D_HA, req, "no existing lock with rhandle "LPX64,
1597 int intent_disposition(struct ldlm_reply *rep, int flag)
1601 return (rep->lock_policy_res1 & flag);
1604 void intent_set_disposition(struct ldlm_reply *rep, int flag)
1608 rep->lock_policy_res1 |= flag;
1611 static int ldlm_intent_policy(struct ldlm_namespace *ns,
1612 struct ldlm_lock **lockp, void *req_cookie,
1613 ldlm_mode_t mode, int flags, void *data)
1615 struct ptlrpc_request *req = req_cookie;
1616 struct ldlm_lock *lock = *lockp;
1623 if (req->rq_reqmsg->bufcount > 1) {
1624 /* an intent needs to be considered */
1625 struct ldlm_intent *it;
1626 struct mds_obd *mds = &req->rq_export->exp_obd->u.mds;
1627 struct ldlm_reply *rep;
1628 struct lustre_handle lockh = { 0 };
1629 struct ldlm_lock *new_lock;
1630 int offset = 2, repsize[4] = {sizeof(struct ldlm_reply),
1631 sizeof(struct mds_body),
1632 mds->mds_max_mdsize,
1633 mds->mds_max_cookiesize};
1635 it = lustre_swab_reqbuf(req, 1, sizeof (*it),
1636 lustre_swab_ldlm_intent);
1638 CERROR ("Intent missing\n");
1639 req->rq_status = -EFAULT;
1640 RETURN(req->rq_status);
1643 LDLM_DEBUG(lock, "intent policy, opc: %s",
1644 ldlm_it2str(it->opc));
1646 rc = lustre_pack_reply(req, it->opc == IT_UNLINK ? 4 : 3,
1649 RETURN(req->rq_status = rc);
1651 rep = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*rep));
1652 intent_set_disposition(rep, DISP_IT_EXECD);
1654 fixup_handle_for_resent_req(req, lock, &lockh);
1656 /* execute policy */
1657 switch ((long)it->opc) {
1659 case IT_CREAT|IT_OPEN:
1660 /* XXX swab here to assert that an mds_open reint
1661 * packet is following */
1662 rep->lock_policy_res2 = mds_reint(req, offset, &lockh);
1664 /* We abort the lock if the lookup was negative and
1665 * we did not make it to the OPEN portion */
1666 if (!intent_disposition(rep, DISP_LOOKUP_EXECD))
1667 RETURN(ELDLM_LOCK_ABORTED);
1668 if (intent_disposition(rep, DISP_LOOKUP_NEG) &&
1669 !intent_disposition(rep, DISP_OPEN_OPEN))
1671 RETURN(ELDLM_LOCK_ABORTED);
1676 rep->lock_policy_res2 = mds_getattr_name(offset, req,
1678 /* FIXME: LDLM can set req->rq_status. MDS sets
1679 policy_res{1,2} with disposition and status.
1680 - replay: returns 0 & req->status is old status
1681 - otherwise: returns req->status */
1682 if (intent_disposition(rep, DISP_LOOKUP_NEG))
1683 rep->lock_policy_res2 = 0;
1684 if (!intent_disposition(rep, DISP_LOOKUP_POS) ||
1685 rep->lock_policy_res2)
1686 RETURN(ELDLM_LOCK_ABORTED);
1687 if (req->rq_status != 0) {
1689 rep->lock_policy_res2 = req->rq_status;
1690 RETURN(ELDLM_LOCK_ABORTED);
1694 CERROR("Unhandled intent "LPD64"\n", it->opc);
1698 /* By this point, whatever function we called above must have
1699 * either filled in 'lockh', been an intent replay, or returned
1700 * an error. We want to allow replayed RPCs to not get a lock,
1701 * since we would just drop it below anyways because lock replay
1702 * is done separately by the client afterwards. For regular
1703 * RPCs we want to give the new lock to the client instead of
1704 * whatever lock it was about to get.
1706 new_lock = ldlm_handle2lock(&lockh);
1707 if (new_lock == NULL && (flags & LDLM_FL_INTENT_ONLY))
1710 LASSERT(new_lock != NULL);
1712 /* If we've already given this lock to a client once, then we
1713 * should have no readers or writers. Otherwise, we should
1714 * have one reader _or_ writer ref (which will be zeroed below)
1715 * before returning the lock to a client.
1717 if (new_lock->l_export == req->rq_export) {
1718 LASSERT(new_lock->l_readers + new_lock->l_writers == 0);
1720 LASSERT(new_lock->l_export == NULL);
1721 LASSERT(new_lock->l_readers + new_lock->l_writers == 1);
1726 if (new_lock->l_export == req->rq_export) {
1727 /* Already gave this to the client, which means that we
1728 * reconstructed a reply. */
1729 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
1731 RETURN(ELDLM_LOCK_REPLACED);
1734 /* Fixup the lock to be given to the client */
1735 l_lock(&new_lock->l_resource->lr_namespace->ns_lock);
1736 new_lock->l_readers = 0;
1737 new_lock->l_writers = 0;
1739 new_lock->l_export = class_export_get(req->rq_export);
1740 list_add(&new_lock->l_export_chain,
1741 &new_lock->l_export->exp_ldlm_data.led_held_locks);
1743 new_lock->l_blocking_ast = lock->l_blocking_ast;
1744 new_lock->l_completion_ast = lock->l_completion_ast;
1746 memcpy(&new_lock->l_remote_handle, &lock->l_remote_handle,
1747 sizeof(lock->l_remote_handle));
1749 new_lock->l_flags &= ~LDLM_FL_LOCAL;
1751 LDLM_LOCK_PUT(new_lock);
1752 l_unlock(&new_lock->l_resource->lr_namespace->ns_lock);
1754 RETURN(ELDLM_LOCK_REPLACED);
1756 int size = sizeof(struct ldlm_reply);
1757 rc = lustre_pack_reply(req, 1, &size, NULL);
1766 int mds_attach(struct obd_device *dev, obd_count len, void *data)
1768 struct lprocfs_static_vars lvars;
1770 lprocfs_init_multi_vars(0, &lvars);
1771 return lprocfs_obd_attach(dev, lvars.obd_vars);
1774 int mds_detach(struct obd_device *dev)
1776 return lprocfs_obd_detach(dev);
1779 int mdt_attach(struct obd_device *dev, obd_count len, void *data)
1781 struct lprocfs_static_vars lvars;
1783 lprocfs_init_multi_vars(1, &lvars);
1784 return lprocfs_obd_attach(dev, lvars.obd_vars);
1787 int mdt_detach(struct obd_device *dev)
1789 return lprocfs_obd_detach(dev);
1792 static int mdt_setup(struct obd_device *obddev, obd_count len, void *buf)
1794 struct mds_obd *mds = &obddev->u.mds;
1798 mds->mds_service = ptlrpc_init_svc(MDS_NEVENTS, MDS_NBUFS,
1799 MDS_BUFSIZE, MDS_MAXREQSIZE,
1800 MDS_REQUEST_PORTAL, MDC_REPLY_PORTAL,
1802 obddev->obd_proc_entry);
1804 if (!mds->mds_service) {
1805 CERROR("failed to start service\n");
1806 RETURN(rc = -ENOMEM);
1809 rc = ptlrpc_start_n_threads(obddev, mds->mds_service, MDT_NUM_THREADS,
1812 GOTO(err_thread, rc);
1814 mds->mds_setattr_service =
1815 ptlrpc_init_svc(MDS_NEVENTS, MDS_NBUFS,
1816 MDS_BUFSIZE, MDS_MAXREQSIZE,
1817 MDS_SETATTR_PORTAL, MDC_REPLY_PORTAL,
1818 mds_handle, "mds_setattr",
1819 obddev->obd_proc_entry);
1820 if (!mds->mds_setattr_service) {
1821 CERROR("failed to start getattr service\n");
1822 GOTO(err_thread, rc = -ENOMEM);
1825 rc = ptlrpc_start_n_threads(obddev, mds->mds_setattr_service,
1826 MDT_NUM_THREADS, "ll_mdt_attr");
1828 GOTO(err_thread2, rc);
1830 mds->mds_readpage_service =
1831 ptlrpc_init_svc(MDS_NEVENTS, MDS_NBUFS,
1832 MDS_BUFSIZE, MDS_MAXREQSIZE,
1833 MDS_READPAGE_PORTAL, MDC_REPLY_PORTAL,
1834 mds_handle, "mds_readpage",
1835 obddev->obd_proc_entry);
1836 if (!mds->mds_readpage_service) {
1837 CERROR("failed to start readpage service\n");
1838 GOTO(err_thread2, rc = -ENOMEM);
1841 rc = ptlrpc_start_n_threads(obddev, mds->mds_readpage_service,
1842 MDT_NUM_THREADS, "ll_mdt_rdpg");
1845 GOTO(err_thread3, rc);
1850 ptlrpc_unregister_service(mds->mds_readpage_service);
1852 ptlrpc_unregister_service(mds->mds_setattr_service);
1854 ptlrpc_unregister_service(mds->mds_service);
1859 static int mdt_cleanup(struct obd_device *obddev, int flags)
1861 struct mds_obd *mds = &obddev->u.mds;
1864 ptlrpc_stop_all_threads(mds->mds_readpage_service);
1865 ptlrpc_unregister_service(mds->mds_readpage_service);
1867 ptlrpc_stop_all_threads(mds->mds_setattr_service);
1868 ptlrpc_unregister_service(mds->mds_setattr_service);
1870 ptlrpc_stop_all_threads(mds->mds_service);
1871 ptlrpc_unregister_service(mds->mds_service);
1876 static struct dentry *mds_lvfs_fid2dentry(__u64 id, __u32 gen, __u64 gr, void *data)
1878 struct obd_device *obd = data;
1881 fid.generation = gen;
1882 return mds_fid2dentry(&obd->u.mds, &fid, NULL);
1885 struct lvfs_callback_ops mds_lvfs_ops = {
1886 l_fid2dentry: mds_lvfs_fid2dentry,
1889 /* use obd ops to offer management infrastructure */
1890 static struct obd_ops mds_obd_ops = {
1891 o_owner: THIS_MODULE,
1892 o_attach: mds_attach,
1893 o_detach: mds_detach,
1894 o_connect: mds_connect,
1895 o_init_export: mds_init_export,
1896 o_destroy_export: mds_destroy_export,
1897 o_disconnect: mds_disconnect,
1899 o_postsetup: mds_postsetup,
1900 o_precleanup: mds_precleanup,
1901 o_cleanup: mds_cleanup,
1902 o_postrecov: mds_postrecov,
1903 o_statfs: mds_obd_statfs,
1904 o_iocontrol: mds_iocontrol,
1905 o_create: mds_obd_create,
1906 o_destroy: mds_obd_destroy,
1907 o_llog_init: mds_llog_init,
1908 o_llog_finish: mds_llog_finish,
1909 o_notify: mds_notify,
1912 static struct obd_ops mdt_obd_ops = {
1913 o_owner: THIS_MODULE,
1914 o_attach: mdt_attach,
1915 o_detach: mdt_detach,
1917 o_cleanup: mdt_cleanup,
1920 static int __init mds_init(void)
1922 struct lprocfs_static_vars lvars;
1924 lprocfs_init_multi_vars(0, &lvars);
1925 class_register_type(&mds_obd_ops, lvars.module_vars, LUSTRE_MDS_NAME);
1926 lprocfs_init_multi_vars(1, &lvars);
1927 class_register_type(&mdt_obd_ops, lvars.module_vars, LUSTRE_MDT_NAME);
1928 ldlm_register_intent(ldlm_intent_policy);
1933 static void /*__exit*/ mds_exit(void)
1935 ldlm_unregister_intent();
1936 class_unregister_type(LUSTRE_MDS_NAME);
1937 class_unregister_type(LUSTRE_MDT_NAME);
1940 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
1941 MODULE_DESCRIPTION("Lustre Metadata Server (MDS)");
1942 MODULE_LICENSE("GPL");
1944 module_init(mds_init);
1945 module_exit(mds_exit);