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_intent_policy(struct ldlm_namespace *ns,
60 struct ldlm_lock **lockp, void *req_cookie,
61 ldlm_mode_t mode, int flags, void *data);
62 static int mds_postsetup(struct obd_device *obd);
63 static int mds_cleanup(struct obd_device *obd, int flags);
65 /* Assumes caller has already pushed into the kernel filesystem context */
66 static int mds_sendpage(struct ptlrpc_request *req, struct file *file,
67 loff_t offset, int count)
69 struct ptlrpc_bulk_desc *desc;
70 struct l_wait_info lwi;
72 int rc = 0, npages, i, tmpcount, tmpsize = 0;
75 LASSERT((offset & (PAGE_SIZE - 1)) == 0); /* I'm dubious about this */
77 npages = (count + PAGE_SIZE - 1) >> PAGE_SHIFT;
78 OBD_ALLOC(pages, sizeof(*pages) * npages);
80 GOTO(out, rc = -ENOMEM);
82 desc = ptlrpc_prep_bulk_exp(req, npages, BULK_PUT_SOURCE,
85 GOTO(out_free, rc = -ENOMEM);
87 for (i = 0, tmpcount = count; i < npages; i++, tmpcount -= tmpsize) {
88 tmpsize = tmpcount > PAGE_SIZE ? PAGE_SIZE : tmpcount;
90 pages[i] = alloc_pages(GFP_KERNEL, 0);
92 GOTO(cleanup_buf, rc = -ENOMEM);
94 ptlrpc_prep_bulk_page(desc, pages[i], 0, tmpsize);
97 for (i = 0, tmpcount = count; i < npages; i++, tmpcount -= tmpsize) {
98 tmpsize = tmpcount > PAGE_SIZE ? PAGE_SIZE : tmpcount;
99 CDEBUG(D_EXT2, "reading %u@%llu from dir %lu (size %llu)\n",
100 tmpsize, offset, file->f_dentry->d_inode->i_ino,
101 file->f_dentry->d_inode->i_size);
103 rc = fsfilt_readpage(req->rq_export->exp_obd, file,
104 kmap(pages[i]), tmpsize, &offset);
108 GOTO(cleanup_buf, rc = -EIO);
111 LASSERT(desc->bd_nob == count);
113 rc = ptlrpc_start_bulk_transfer(desc);
115 GOTO(cleanup_buf, rc);
117 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE)) {
118 CERROR("obd_fail_loc=%x, fail operation rc=%d\n",
119 OBD_FAIL_MDS_SENDPAGE, rc);
120 GOTO(abort_bulk, rc);
123 lwi = LWI_TIMEOUT(obd_timeout * HZ / 4, NULL, NULL);
124 rc = l_wait_event(desc->bd_waitq, !ptlrpc_bulk_active(desc), &lwi);
125 LASSERT (rc == 0 || rc == -ETIMEDOUT);
128 if (desc->bd_success &&
129 desc->bd_nob_transferred == count)
130 GOTO(cleanup_buf, rc);
132 rc = -ETIMEDOUT; /* XXX should this be a different errno? */
135 DEBUG_REQ(D_ERROR, req, "bulk failed: %s %d(%d), evicting %s@%s\n",
136 (rc == -ETIMEDOUT) ? "timeout" : "network error",
137 desc->bd_nob_transferred, count,
138 req->rq_export->exp_client_uuid.uuid,
139 req->rq_export->exp_connection->c_remote_uuid.uuid);
141 ptlrpc_fail_export(req->rq_export);
145 ptlrpc_abort_bulk (desc);
147 for (i = 0; i < npages; i++)
149 __free_pages(pages[i], 0);
151 ptlrpc_free_bulk(desc);
153 OBD_FREE(pages, sizeof(*pages) * npages);
158 int mds_lock_mode_for_dir(struct obd_device *obd,
159 struct dentry *dentry, int mode)
163 /* any dir access needs couple locks:
164 * 1) on part of dir we gonna lookup/modify in
165 * 2) on a whole dir to protect it from concurrent splitting
166 * and to flush client's cache for readdir()
167 * so, for a given mode and dentry this routine decides what
168 * lock mode to use for lock #2:
169 * 1) if caller's gonna lookup in dir then we need to protect
170 * dir from being splitted only - LCK_CR
171 * 2) if caller's gonna modify dir then we need to protect
172 * dir from being splitted and to flush cache - LCK_CW
173 * 3) if caller's gonna modify dir and that dir seems ready
174 * for splitting then we need to protect it from any
175 * type of access (lookup/modify/split) - LCK_EX -bzzz */
177 if (mode == LCK_PR) {
179 } else if (mode == LCK_PW) {
180 /* caller gonna modify directory.we use concurrent
181 write lock here to retract client's cache for readdir */
183 if (mds_splitting_expected(obd, dentry)) {
184 /* splitting possible. serialize any access */
185 CERROR("%s: gonna split %lu/%lu\n",
187 (unsigned long) dentry->d_inode->i_ino,
188 (unsigned long) dentry->d_inode->i_generation);
192 CWARN("unexpected lock mode %d\n", mode);
198 /* only valid locked dentries or errors should be returned */
199 struct dentry *mds_fid2locked_dentry(struct obd_device *obd, struct ll_fid *fid,
200 struct vfsmount **mnt, int lock_mode,
201 struct lustre_handle *lockh, int *mode,
202 char *name, int namelen, __u64 lockpart)
204 struct mds_obd *mds = &obd->u.mds;
205 struct dentry *de = mds_fid2dentry(mds, fid, mnt), *retval = de;
206 struct ldlm_res_id res_id = { .name = {0} };
208 ldlm_policy_data_t policy = { .l_inodebits = { lockpart } };
215 res_id.name[0] = de->d_inode->i_ino;
216 res_id.name[1] = de->d_inode->i_generation;
219 if (name && IS_PDIROPS(de->d_inode)) {
220 ldlm_policy_data_t cpolicy =
221 { .l_inodebits = { MDS_INODELOCK_UPDATE } };
222 LASSERT(mode != NULL);
223 *mode = mds_lock_mode_for_dir(obd, de, lock_mode);
224 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
226 &cpolicy, *mode, &flags,
228 ldlm_completion_ast, NULL, NULL,
229 NULL, 0, NULL, lockh + 1);
230 if (rc != ELDLM_OK) {
232 RETURN(ERR_PTR(-ENOLCK));
236 res_id.name[2] = full_name_hash(name, namelen);
238 CDEBUG(D_INFO, "take lock on %lu:%u:"LPX64"\n",
239 de->d_inode->i_ino, de->d_inode->i_generation,
243 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace, res_id,
244 LDLM_IBITS, &policy, lock_mode, &flags,
245 mds_blocking_ast, ldlm_completion_ast, NULL, NULL,
246 NULL, 0, NULL, lockh);
247 if (rc != ELDLM_OK) {
249 retval = ERR_PTR(-EIO); /* XXX translate ldlm code */
252 ldlm_lock_decref(lockh + 1, LCK_CW);
259 #ifndef DCACHE_DISCONNECTED
260 #define DCACHE_DISCONNECTED DCACHE_NFSD_DISCONNECTED
264 /* Look up an entry by inode number. */
265 /* this function ONLY returns valid dget'd dentries with an initialized inode
267 struct dentry *mds_fid2dentry(struct mds_obd *mds, struct ll_fid *fid,
268 struct vfsmount **mnt)
271 unsigned long ino = fid->id;
272 __u32 generation = fid->generation;
274 struct dentry *result;
277 RETURN(ERR_PTR(-ESTALE));
279 snprintf(fid_name, sizeof(fid_name), "0x%lx", ino);
281 CDEBUG(D_DENTRY, "--> mds_fid2dentry: ino/gen %lu/%u, sb %p\n",
282 ino, generation, mds->mds_sb);
284 /* under ext3 this is neither supposed to return bad inodes
286 result = ll_lookup_one_len(fid_name, mds->mds_fid_de, strlen(fid_name));
290 inode = result->d_inode;
292 RETURN(ERR_PTR(-ENOENT));
295 /* here we disabled generation check, as root inode i_generation
296 * of cache mds and real mds are different. */
297 if (generation && inode->i_generation != generation) {
298 /* we didn't find the right inode.. */
299 CERROR("bad inode %lu, link: %lu ct: %d or generation %u/%u\n",
300 inode->i_ino, (unsigned long)inode->i_nlink,
301 atomic_read(&inode->i_count), inode->i_generation,
304 RETURN(ERR_PTR(-ENOENT));
309 *mnt = mds->mds_vfsmnt;
317 /* Establish a connection to the MDS.
319 * This will set up an export structure for the client to hold state data
320 * about that client, like open files, the last operation number it did
321 * on the server, etc.
323 static int mds_connect(struct lustre_handle *conn, struct obd_device *obd,
324 struct obd_uuid *cluuid)
326 struct obd_export *exp;
327 struct mds_export_data *med; /* */
328 struct mds_client_data *mcd;
329 int rc, abort_recovery;
332 if (!conn || !obd || !cluuid)
335 /* Check for aborted recovery. */
336 spin_lock_bh(&obd->obd_processing_task_lock);
337 abort_recovery = obd->obd_abort_recovery;
338 spin_unlock_bh(&obd->obd_processing_task_lock);
340 target_abort_recovery(obd);
342 /* XXX There is a small race between checking the list and adding a
343 * new connection for the same UUID, but the real threat (list
344 * corruption when multiple different clients connect) is solved.
346 * There is a second race between adding the export to the list,
347 * and filling in the client data below. Hence skipping the case
348 * of NULL mcd above. We should already be controlling multiple
349 * connects at the client, and we can't hold the spinlock over
350 * memory allocations without risk of deadlocking.
352 rc = class_connect(conn, obd, cluuid);
355 exp = class_conn2export(conn);
357 med = &exp->exp_mds_data;
359 OBD_ALLOC(mcd, sizeof(*mcd));
361 CERROR("mds: out of memory for client data\n");
362 GOTO(out, rc = -ENOMEM);
365 memcpy(mcd->mcd_uuid, cluuid, sizeof(mcd->mcd_uuid));
368 rc = mds_client_add(obd, &obd->u.mds, med, -1);
373 OBD_FREE(mcd, sizeof(*mcd));
374 class_disconnect(exp, 0);
376 class_export_put(exp);
381 static int mds_init_export(struct obd_export *exp)
383 struct mds_export_data *med = &exp->exp_mds_data;
385 INIT_LIST_HEAD(&med->med_open_head);
386 spin_lock_init(&med->med_open_lock);
390 static int mds_destroy_export(struct obd_export *export)
392 struct mds_export_data *med;
393 struct obd_device *obd = export->exp_obd;
394 struct lvfs_run_ctxt saved;
398 med = &export->exp_mds_data;
399 target_destroy_export(export);
401 if (obd_uuid_equals(&export->exp_client_uuid, &obd->obd_uuid))
404 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
406 /* Close any open files (which may also cause orphan unlinking). */
407 spin_lock(&med->med_open_lock);
408 while (!list_empty(&med->med_open_head)) {
409 struct list_head *tmp = med->med_open_head.next;
410 struct mds_file_data *mfd =
411 list_entry(tmp, struct mds_file_data, mfd_list);
412 BDEVNAME_DECLARE_STORAGE(btmp);
414 /* bug 1579: fix force-closing for 2.5 */
415 struct dentry *dentry = mfd->mfd_dentry;
417 list_del(&mfd->mfd_list);
418 spin_unlock(&med->med_open_lock);
420 /* If you change this message, be sure to update
421 * replay_single:test_46 */
422 CERROR("force closing client file handle for %*s (%s:%lu)\n",
423 dentry->d_name.len, dentry->d_name.name,
424 ll_bdevname(dentry->d_inode->i_sb, btmp),
425 dentry->d_inode->i_ino);
426 rc = mds_mfd_close(NULL, obd, mfd,
427 !(export->exp_flags & OBD_OPT_FAILOVER));
430 CDEBUG(D_INODE, "Error closing file: %d\n", rc);
431 spin_lock(&med->med_open_lock);
433 spin_unlock(&med->med_open_lock);
434 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
437 mds_client_free(export, !(export->exp_flags & OBD_OPT_FAILOVER));
442 static int mds_disconnect(struct obd_export *exp, int flags)
444 struct obd_device *obd;
446 unsigned long irqflags;
451 class_export_get(exp);
453 obd = class_exp2obd(exp);
455 CDEBUG(D_IOCTL, "invalid client cookie "LPX64"\n",
456 exp->exp_handle.h_cookie);
461 if (!(exp->exp_flags & OBD_OPT_REAL_CLIENT)
462 && !atomic_read(&mds->mds_real_clients)) {
463 /* there was no client at all */
464 mds_lmv_disconnect(obd, flags);
467 if ((exp->exp_flags & OBD_OPT_REAL_CLIENT)
468 && atomic_dec_and_test(&mds->mds_real_clients)) {
469 /* time to drop LMV connections */
470 CDEBUG(D_OTHER, "%s: last real client %s disconnected. "
471 "Disconnnect from LMV now\n",
472 obd->obd_name, exp->exp_client_uuid.uuid);
473 mds_lmv_disconnect(obd, flags);
476 spin_lock_irqsave(&exp->exp_lock, irqflags);
477 exp->exp_flags = flags;
478 spin_unlock_irqrestore(&exp->exp_lock, irqflags);
480 /* Disconnect early so that clients can't keep using export */
481 rc = class_disconnect(exp, flags);
482 ldlm_cancel_locks_for_export(exp);
484 /* complete all outstanding replies */
485 spin_lock_irqsave(&exp->exp_lock, irqflags);
486 while (!list_empty(&exp->exp_outstanding_replies)) {
487 struct ptlrpc_reply_state *rs =
488 list_entry(exp->exp_outstanding_replies.next,
489 struct ptlrpc_reply_state, rs_exp_list);
490 struct ptlrpc_service *svc = rs->rs_srv_ni->sni_service;
492 spin_lock(&svc->srv_lock);
493 list_del_init(&rs->rs_exp_list);
494 ptlrpc_schedule_difficult_reply(rs);
495 spin_unlock(&svc->srv_lock);
497 spin_unlock_irqrestore(&exp->exp_lock, irqflags);
499 class_export_put(exp);
503 static int mds_getstatus(struct ptlrpc_request *req)
505 struct mds_obd *mds = mds_req2mds(req);
506 struct mds_body *body;
507 int rc, size = sizeof(*body);
510 rc = lustre_pack_reply(req, 1, &size, NULL);
511 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_GETSTATUS_PACK)) {
512 CERROR("mds: out of memory for message: size=%d\n", size);
513 req->rq_status = -ENOMEM; /* superfluous? */
517 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
518 memcpy(&body->fid1, &mds->mds_rootfid, sizeof(body->fid1));
520 /* the last_committed and last_xid fields are filled in for all
521 * replies already - no need to do so here also.
526 int mds_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
527 void *data, int flag)
532 if (flag == LDLM_CB_CANCELING) {
533 /* Don't need to do anything here. */
537 /* XXX layering violation! -phil */
538 l_lock(&lock->l_resource->lr_namespace->ns_lock);
539 /* Get this: if mds_blocking_ast is racing with mds_intent_policy,
540 * such that mds_blocking_ast is called just before l_i_p takes the
541 * ns_lock, then by the time we get the lock, we might not be the
542 * correct blocking function anymore. So check, and return early, if
544 if (lock->l_blocking_ast != mds_blocking_ast) {
545 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
549 lock->l_flags |= LDLM_FL_CBPENDING;
550 do_ast = (!lock->l_readers && !lock->l_writers);
551 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
554 struct lustre_handle lockh;
557 LDLM_DEBUG(lock, "already unused, calling ldlm_cli_cancel");
558 ldlm_lock2handle(lock, &lockh);
559 rc = ldlm_cli_cancel(&lockh);
561 CERROR("ldlm_cli_cancel: %d\n", rc);
563 LDLM_DEBUG(lock, "Lock still has references, will be "
569 int mds_get_md(struct obd_device *obd, struct inode *inode, void *md,
577 rc = fsfilt_get_md(obd, inode, md, *size);
582 CERROR("Error %d reading eadata for ino %lu\n",
587 if (S_ISREG(inode->i_mode))
588 rc = mds_convert_lov_ea(obd, inode, md, lmm_size);
602 /* Call with lock=1 if you want mds_pack_md to take the i_sem.
603 * Call with lock=0 if the caller has already taken the i_sem. */
604 int mds_pack_md(struct obd_device *obd, struct lustre_msg *msg, int offset,
605 struct mds_body *body, struct inode *inode, int lock)
607 struct mds_obd *mds = &obd->u.mds;
613 lmm = lustre_msg_buf(msg, offset, 0);
615 /* Some problem with getting eadata when I sized the reply
617 CDEBUG(D_INFO, "no space reserved for inode %lu MD\n",
621 lmm_size = msg->buflens[offset];
623 /* I don't really like this, but it is a sanity check on the client
624 * MD request. However, if the client doesn't know how much space
625 * to reserve for the MD, it shouldn't be bad to have too much space.
627 if (lmm_size > mds->mds_max_mdsize) {
628 CWARN("Reading MD for inode %lu of %d bytes > max %d\n",
629 inode->i_ino, lmm_size, mds->mds_max_mdsize);
633 rc = mds_get_md(obd, inode, lmm, &lmm_size, lock);
635 if (S_ISDIR(inode->i_mode))
636 body->valid |= OBD_MD_FLDIREA;
638 body->valid |= OBD_MD_FLEASIZE;
639 body->eadatasize = lmm_size;
646 static int mds_getattr_internal(struct obd_device *obd, struct dentry *dentry,
647 struct ptlrpc_request *req,
648 struct mds_body *reqbody, int reply_off)
650 struct mds_body *body;
651 struct inode *inode = dentry->d_inode;
655 if (inode == NULL && !(dentry->d_flags & DCACHE_CROSS_REF))
658 body = lustre_msg_buf(req->rq_repmsg, reply_off, sizeof(*body));
659 LASSERT(body != NULL); /* caller prepped reply */
661 if (dentry->d_flags & DCACHE_CROSS_REF) {
662 CDEBUG(D_OTHER, "cross reference: %lu/%lu/%lu\n",
663 (unsigned long) dentry->d_mdsnum,
664 (unsigned long) dentry->d_inum,
665 (unsigned long) dentry->d_generation);
666 body->valid |= OBD_MD_FLID | OBD_MD_MDS;
667 body->fid1.id = dentry->d_inum;
668 body->fid1.mds = dentry->d_mdsnum;
669 body->fid1.generation = dentry->d_generation;
672 mds_pack_inode2fid(obd, &body->fid1, inode);
673 mds_pack_inode2body(obd, body, inode);
675 if ((S_ISREG(inode->i_mode) && (reqbody->valid & OBD_MD_FLEASIZE)) ||
676 (S_ISDIR(inode->i_mode) && (reqbody->valid & OBD_MD_FLDIREA))) {
677 rc = mds_pack_md(obd, req->rq_repmsg, reply_off + 1, body,
680 /* If we have LOV EA data, the OST holds size, atime, mtime */
681 if (!(body->valid & OBD_MD_FLEASIZE) &&
682 !(body->valid & OBD_MD_FLDIREA))
683 body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
684 OBD_MD_FLATIME | OBD_MD_FLMTIME);
685 } else if (S_ISLNK(inode->i_mode) &&
686 (reqbody->valid & OBD_MD_LINKNAME) != 0) {
687 char *symname = lustre_msg_buf(req->rq_repmsg, reply_off + 1,0);
690 LASSERT (symname != NULL); /* caller prepped reply */
691 len = req->rq_repmsg->buflens[reply_off + 1];
693 rc = inode->i_op->readlink(dentry, symname, len);
695 CERROR("readlink failed: %d\n", rc);
696 } else if (rc != len - 1) {
697 CERROR ("Unexpected readlink rc %d: expecting %d\n",
701 CDEBUG(D_INODE, "read symlink dest %s\n", symname);
702 body->valid |= OBD_MD_LINKNAME;
703 body->eadatasize = rc + 1;
704 symname[rc] = 0; /* NULL terminate */
712 static int mds_getattr_pack_msg_cf(struct ptlrpc_request *req,
713 struct dentry *dentry,
716 int rc = 0, size[1] = {sizeof(struct mds_body)};
719 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK)) {
720 CERROR("failed MDS_GETATTR_PACK test\n");
721 req->rq_status = -ENOMEM;
722 GOTO(out, rc = -ENOMEM);
725 rc = lustre_pack_reply(req, 1, size, NULL);
727 CERROR("out of memory\n");
728 GOTO(out, req->rq_status = rc);
736 static int mds_getattr_pack_msg(struct ptlrpc_request *req, struct inode *inode,
739 struct mds_obd *mds = mds_req2mds(req);
740 struct mds_body *body;
741 int rc = 0, size[2] = {sizeof(*body)}, bufcount = 1;
744 body = lustre_msg_buf(req->rq_reqmsg, offset, sizeof (*body));
745 LASSERT(body != NULL); /* checked by caller */
746 LASSERT_REQSWABBED(req, offset); /* swabbed by caller */
748 if ((S_ISREG(inode->i_mode) && (body->valid & OBD_MD_FLEASIZE)) ||
749 (S_ISDIR(inode->i_mode) && (body->valid & OBD_MD_FLDIREA))) {
752 rc = fsfilt_get_md(req->rq_export->exp_obd, inode, NULL, 0);
754 CDEBUG(D_INODE, "got %d bytes MD data for inode %lu\n",
758 CERROR("error getting inode %lu MD: rc = %d\n",
761 } else if (rc > mds->mds_max_mdsize) {
763 CERROR("MD size %d larger than maximum possible %u\n",
764 rc, mds->mds_max_mdsize);
769 } else if (S_ISLNK(inode->i_mode) && (body->valid & OBD_MD_LINKNAME)) {
770 if (inode->i_size + 1 != body->eadatasize)
771 CERROR("symlink size: %Lu, reply space: %d\n",
772 inode->i_size + 1, body->eadatasize);
773 size[bufcount] = min_t(int, inode->i_size+1, body->eadatasize);
775 CDEBUG(D_INODE, "symlink size: %Lu, reply space: %d\n",
776 inode->i_size + 1, body->eadatasize);
779 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK)) {
780 CERROR("failed MDS_GETATTR_PACK test\n");
781 req->rq_status = -ENOMEM;
782 GOTO(out, rc = -ENOMEM);
785 rc = lustre_pack_reply(req, bufcount, size, NULL);
787 CERROR("out of memory\n");
788 GOTO(out, req->rq_status = rc);
796 static int mds_getattr_name(int offset, struct ptlrpc_request *req,
797 struct lustre_handle *child_lockh, int child_part)
799 struct obd_device *obd = req->rq_export->exp_obd;
800 struct ldlm_reply *rep = NULL;
801 struct lvfs_run_ctxt saved;
802 struct mds_body *body;
803 struct dentry *dparent = NULL, *dchild = NULL;
804 struct lvfs_ucred uc;
805 struct lustre_handle parent_lockh[2];
806 int namesize, update_mode;
807 int rc = 0, cleanup_phase = 0, resent_req = 0;
811 LASSERT(!strcmp(obd->obd_type->typ_name, "mds"));
813 /* Swab now, before anyone looks inside the request */
815 body = lustre_swab_reqbuf(req, offset, sizeof(*body),
816 lustre_swab_mds_body);
818 CERROR("Can't swab mds_body\n");
819 GOTO(cleanup, rc = -EFAULT);
822 LASSERT_REQSWAB(req, offset + 1);
823 name = lustre_msg_string(req->rq_reqmsg, offset + 1, 0);
825 CERROR("Can't unpack name\n");
826 GOTO(cleanup, rc = -EFAULT);
828 namesize = req->rq_reqmsg->buflens[offset + 1];
830 LASSERT (offset == 0 || offset == 2);
831 /* if requests were at offset 2, the getattr reply goes back at 1 */
833 rep = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*rep));
837 uc.luc_fsuid = body->fsuid;
838 uc.luc_fsgid = body->fsgid;
839 uc.luc_cap = body->capability;
840 uc.luc_suppgid1 = body->suppgid;
841 uc.luc_suppgid2 = -1;
842 push_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
843 cleanup_phase = 1; /* kernel context */
844 intent_set_disposition(rep, DISP_LOOKUP_EXECD);
846 LASSERT(namesize > 0);
848 /* we have no dentry here, drop LOOKUP bit */
849 child_part &= ~MDS_INODELOCK_LOOKUP;
850 CDEBUG(D_OTHER, "%s: request to retrieve attrs for %lu/%lu\n",
851 obd->obd_name, (unsigned long) body->fid1.id,
852 (unsigned long) body->fid1.generation);
853 dchild = mds_fid2locked_dentry(obd, &body->fid1, NULL, LCK_PR,
854 parent_lockh, &update_mode,
855 NULL, 0, child_part);
856 if (IS_ERR(dchild)) {
857 CERROR("can't find inode: %d\n", (int) PTR_ERR(dchild));
858 GOTO(cleanup, rc = PTR_ERR(dchild));
860 memcpy(child_lockh, parent_lockh, sizeof(parent_lockh[0]));
862 if (parent_lockh[1].cookie)
863 ldlm_lock_decref(parent_lockh + 1, update_mode);
869 /* FIXME: handle raw lookup */
871 if (body->valid == OBD_MD_FLID) {
872 struct mds_body *mds_reply;
873 int size = sizeof(*mds_reply);
875 // The user requested ONLY the inode number, so do a raw lookup
876 rc = lustre_pack_reply(req, 1, &size, NULL);
878 CERROR("out of memory\n");
882 rc = dir->i_op->lookup_raw(dir, name, namesize - 1, &inum);
884 mds_reply = lustre_msg_buf(req->rq_repmsg, offset,
886 mds_reply->fid1.id = inum;
887 mds_reply->valid = OBD_MD_FLID;
892 if (child_lockh->cookie != 0) {
893 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT);
897 if (resent_req == 0) {
898 rc = mds_get_parent_child_locked(obd, &obd->u.mds, &body->fid1,
899 parent_lockh, &dparent,
900 LCK_PR, MDS_INODELOCK_LOOKUP,
901 &update_mode, name, namesize,
902 child_lockh, &dchild, LCK_PR,
907 struct ldlm_lock *granted_lock;
908 struct ll_fid child_fid;
909 struct ldlm_resource *res;
910 DEBUG_REQ(D_DLMTRACE, req, "resent, not enqueuing new locks");
911 granted_lock = ldlm_handle2lock(child_lockh);
912 LASSERT(granted_lock);
914 res = granted_lock->l_resource;
915 child_fid.id = res->lr_name.name[0];
916 child_fid.generation = res->lr_name.name[1];
917 dchild = mds_fid2dentry(&obd->u.mds, &child_fid, NULL);
919 LDLM_LOCK_PUT(granted_lock);
922 cleanup_phase = 2; /* dchild, dparent, locks */
925 if (!DENTRY_VALID(dchild)) {
926 intent_set_disposition(rep, DISP_LOOKUP_NEG);
927 /* in the intent case, the policy clears this error:
928 the disposition is enough */
929 GOTO(cleanup, rc = -ENOENT);
931 intent_set_disposition(rep, DISP_LOOKUP_POS);
934 if (req->rq_repmsg == NULL) {
935 if (dchild->d_flags & DCACHE_CROSS_REF)
936 rc = mds_getattr_pack_msg_cf(req, dchild, offset);
938 rc = mds_getattr_pack_msg(req, dchild->d_inode, offset);
940 CERROR ("mds_getattr_pack_msg: %d\n", rc);
945 rc = mds_getattr_internal(obd, dchild, req, body, offset);
946 GOTO(cleanup, rc); /* returns the lock to the client */
949 switch (cleanup_phase) {
951 if (resent_req == 0) {
952 if (rc && DENTRY_VALID(dchild))
953 ldlm_lock_decref(child_lockh, LCK_PR);
955 ldlm_lock_decref(parent_lockh, LCK_PR);
957 if (parent_lockh[1].cookie != 0)
958 ldlm_lock_decref(parent_lockh + 1,
967 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
973 static int mds_getattr(int offset, struct ptlrpc_request *req)
975 struct mds_obd *mds = mds_req2mds(req);
976 struct obd_device *obd = req->rq_export->exp_obd;
977 struct lvfs_run_ctxt saved;
979 struct mds_body *body;
980 struct lvfs_ucred uc;
984 body = lustre_swab_reqbuf (req, offset, sizeof (*body),
985 lustre_swab_mds_body);
987 CERROR ("Can't unpack body\n");
991 uc.luc_fsuid = body->fsuid;
992 uc.luc_fsgid = body->fsgid;
993 uc.luc_cap = body->capability;
994 push_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
995 de = mds_fid2dentry(mds, &body->fid1, NULL);
997 rc = req->rq_status = -ENOENT;
998 GOTO(out_pop, PTR_ERR(de));
1001 rc = mds_getattr_pack_msg(req, de->d_inode, offset);
1003 CERROR ("mds_getattr_pack_msg: %d\n", rc);
1007 req->rq_status = mds_getattr_internal(obd, de, req, body, 0);
1012 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1017 static int mds_obd_statfs(struct obd_device *obd, struct obd_statfs *osfs,
1018 unsigned long max_age)
1022 spin_lock(&obd->obd_osfs_lock);
1023 rc = fsfilt_statfs(obd, obd->u.mds.mds_sb, max_age);
1025 memcpy(osfs, &obd->obd_osfs, sizeof(*osfs));
1026 spin_unlock(&obd->obd_osfs_lock);
1031 static int mds_statfs(struct ptlrpc_request *req)
1033 struct obd_device *obd = req->rq_export->exp_obd;
1034 int rc, size = sizeof(struct obd_statfs);
1037 rc = lustre_pack_reply(req, 1, &size, NULL);
1038 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_STATFS_PACK)) {
1039 CERROR("mds: statfs lustre_pack_reply failed: rc = %d\n", rc);
1043 /* We call this so that we can cache a bit - 1 jiffie worth */
1044 rc = mds_obd_statfs(obd, lustre_msg_buf(req->rq_repmsg, 0, size),
1047 CERROR("mds_obd_statfs failed: rc %d\n", rc);
1053 req->rq_status = rc;
1057 static int mds_sync(struct ptlrpc_request *req)
1059 struct obd_device *obd = req->rq_export->exp_obd;
1060 struct mds_obd *mds = &obd->u.mds;
1061 struct mds_body *body;
1062 int rc, size = sizeof(*body);
1065 body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof(*body));
1067 GOTO(out, rc = -EPROTO);
1069 rc = lustre_pack_reply(req, 1, &size, NULL);
1070 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK)) {
1071 CERROR("fsync lustre_pack_reply failed: rc = %d\n", rc);
1075 if (body->fid1.id == 0) {
1076 /* a fid of zero is taken to mean "sync whole filesystem" */
1077 rc = fsfilt_sync(obd, mds->mds_sb);
1081 /* just any file to grab fsync method - "file" arg unused */
1082 struct file *file = mds->mds_rcvd_filp;
1085 de = mds_fid2dentry(mds, &body->fid1, NULL);
1087 GOTO(out, rc = PTR_ERR(de));
1089 rc = file->f_op->fsync(NULL, de, 1);
1094 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
1095 mds_pack_inode2fid(obd, &body->fid1, de->d_inode);
1096 mds_pack_inode2body(obd, body, de->d_inode);
1099 req->rq_status = rc;
1103 /* mds_readpage does not take a DLM lock on the inode, because the client must
1104 * already have a PR lock.
1106 * If we were to take another one here, a deadlock will result, if another
1107 * thread is already waiting for a PW lock. */
1108 static int mds_readpage(struct ptlrpc_request *req)
1110 struct obd_device *obd = req->rq_export->exp_obd;
1111 struct vfsmount *mnt;
1114 struct mds_body *body, *repbody;
1115 struct lvfs_run_ctxt saved;
1116 int rc, size = sizeof(*repbody);
1117 struct lvfs_ucred uc;
1120 rc = lustre_pack_reply(req, 1, &size, NULL);
1121 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK)) {
1122 CERROR("mds: out of memory\n");
1123 GOTO(out, rc = -ENOMEM);
1126 body = lustre_swab_reqbuf(req, 0, sizeof(*body), lustre_swab_mds_body);
1128 GOTO (out, rc = -EFAULT);
1130 uc.luc_fsuid = body->fsuid;
1131 uc.luc_fsgid = body->fsgid;
1132 uc.luc_cap = body->capability;
1133 push_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1134 de = mds_fid2dentry(&obd->u.mds, &body->fid1, &mnt);
1136 GOTO(out_pop, rc = PTR_ERR(de));
1138 CDEBUG(D_INODE, "ino %lu\n", de->d_inode->i_ino);
1140 file = dentry_open(de, mnt, O_RDONLY | O_LARGEFILE);
1141 /* note: in case of an error, dentry_open puts dentry */
1143 GOTO(out_pop, rc = PTR_ERR(file));
1145 /* body->size is actually the offset -eeb */
1146 if ((body->size & (de->d_inode->i_blksize - 1)) != 0) {
1147 CERROR("offset "LPU64" not on a block boundary of %lu\n",
1148 body->size, de->d_inode->i_blksize);
1149 GOTO(out_file, rc = -EFAULT);
1152 /* body->nlink is actually the #bytes to read -eeb */
1153 if (body->nlink & (de->d_inode->i_blksize - 1)) {
1154 CERROR("size %u is not multiple of blocksize %lu\n",
1155 body->nlink, de->d_inode->i_blksize);
1156 GOTO(out_file, rc = -EFAULT);
1159 repbody = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*repbody));
1160 repbody->size = file->f_dentry->d_inode->i_size;
1161 repbody->valid = OBD_MD_FLSIZE;
1163 /* to make this asynchronous make sure that the handling function
1164 doesn't send a reply when this function completes. Instead a
1165 callback function would send the reply */
1166 /* body->size is actually the offset -eeb */
1167 rc = mds_sendpage(req, file, body->size, body->nlink);
1170 filp_close(file, 0);
1172 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1174 req->rq_status = rc;
1178 int mds_reint(struct ptlrpc_request *req, int offset,
1179 struct lustre_handle *lockh)
1181 struct mds_update_record *rec; /* 116 bytes on the stack? no sir! */
1185 OBD_ALLOC(rec, sizeof(*rec));
1189 rc = mds_update_unpack(req, offset, rec);
1190 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNPACK)) {
1191 CERROR("invalid record\n");
1192 GOTO(out, req->rq_status = -EINVAL);
1194 /* rc will be used to interrupt a for loop over multiple records */
1195 rc = mds_reint_rec(rec, offset, req, lockh);
1197 OBD_FREE(rec, sizeof(*rec));
1201 static int mds_filter_recovery_request(struct ptlrpc_request *req,
1202 struct obd_device *obd, int *process)
1204 switch (req->rq_reqmsg->opc) {
1205 case MDS_CONNECT: /* This will never get here, but for completeness. */
1206 case OST_CONNECT: /* This will never get here, but for completeness. */
1207 case MDS_DISCONNECT:
1208 case OST_DISCONNECT:
1213 case MDS_SYNC: /* used in unmounting */
1218 *process = target_queue_recovery_request(req, obd);
1222 DEBUG_REQ(D_ERROR, req, "not permitted during recovery");
1224 /* XXX what should we set rq_status to here? */
1225 req->rq_status = -EAGAIN;
1226 RETURN(ptlrpc_error(req));
1230 static char *reint_names[] = {
1231 [REINT_SETATTR] "setattr",
1232 [REINT_CREATE] "create",
1233 [REINT_LINK] "link",
1234 [REINT_UNLINK] "unlink",
1235 [REINT_RENAME] "rename",
1236 [REINT_OPEN] "open",
1239 #define FILTER_VALID_FLAGS (OBD_MD_FLTYPE | OBD_MD_FLMODE | OBD_MD_FLGENER |\
1240 OBD_MD_FLSIZE | OBD_MD_FLBLOCKS | OBD_MD_FLBLKSZ|\
1241 OBD_MD_FLATIME | OBD_MD_FLMTIME | OBD_MD_FLCTIME|\
1244 static void reconstruct_create(struct ptlrpc_request *req)
1246 struct mds_export_data *med = &req->rq_export->exp_mds_data;
1247 struct mds_client_data *mcd = med->med_mcd;
1248 struct ost_body *body;
1250 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
1252 /* copy rc, transno and disp; steal locks */
1253 mds_req_from_mcd(req, mcd);
1254 CERROR("reconstruct reply for x"LPU64"\n", req->rq_xid);
1257 static int mdt_obj_create(struct ptlrpc_request *req)
1259 struct obd_device *obd = req->rq_export->exp_obd;
1260 struct ldlm_res_id res_id = { .name = {0} };
1261 struct mds_obd *mds = &obd->u.mds;
1262 struct ost_body *body, *repbody;
1263 int rc, size = sizeof(*repbody);
1264 char fidname[LL_FID_NAMELEN];
1265 struct inode *parent_inode;
1266 struct lustre_handle lockh;
1267 struct lvfs_run_ctxt saved;
1268 ldlm_policy_data_t policy;
1269 struct dentry *new = NULL;
1270 struct dentry_params dp;
1271 int mealen, flags = 0;
1272 unsigned int tmpname;
1273 struct lvfs_ucred uc;
1278 DEBUG_REQ(D_HA, req, "create remote object");
1280 parent_inode = mds->mds_objects_dir->d_inode;
1282 body = lustre_swab_reqbuf(req, 0, sizeof(*body),
1283 lustre_swab_ost_body);
1287 MDS_CHECK_RESENT(req, reconstruct_create(req));
1289 uc.luc_fsuid = body->oa.o_uid;
1290 uc.luc_fsgid = body->oa.o_gid;
1292 push_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1294 rc = lustre_pack_reply(req, 1, &size, NULL);
1298 repbody = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*repbody));
1300 if (body->oa.o_flags & OBD_FL_RECREATE_OBJS) {
1301 /* this is re-create request from MDS holding directory name.
1302 * we have to lookup given ino/generation first. if it exists
1303 * (good case) then there is nothing to do. if it does not
1304 * then we have to recreate it */
1306 fid.id = body->oa.o_id;
1307 fid.generation = body->oa.o_generation;
1308 new = mds_fid2dentry(mds, &fid, NULL);
1309 if (!IS_ERR(new) && new->d_inode) {
1310 CWARN("mkdir() repairing is on its way: %lu/%lu\n",
1311 (unsigned long) fid.id,
1312 (unsigned long) fid.generation);
1313 obdo_from_inode(&repbody->oa, new->d_inode,
1314 FILTER_VALID_FLAGS);
1315 repbody->oa.o_id = new->d_inode->i_ino;
1316 repbody->oa.o_generation = new->d_inode->i_generation;
1317 repbody->oa.o_valid |= OBD_MD_FLID | OBD_MD_FLGENER;
1318 GOTO(cleanup2, rc = 0);
1320 CWARN("hmm. for some reason dir %lu/%lu (or reply) got lost\n",
1321 (unsigned long) fid.id, (unsigned long) fid.generation);
1322 LASSERT(new->d_inode == NULL ||
1323 new->d_inode->i_generation != fid.generation);
1327 down(&parent_inode->i_sem);
1328 handle = fsfilt_start(obd, parent_inode, FSFILT_OP_MKDIR, NULL);
1329 LASSERT(!IS_ERR(handle));
1332 tmpname = ll_insecure_random_int();
1333 rc = sprintf(fidname, "%u", tmpname);
1334 new = lookup_one_len(fidname, mds->mds_objects_dir, rc);
1336 CERROR("%s: can't lookup new inode (%s) for mkdir: %d\n",
1337 obd->obd_name, fidname, (int) PTR_ERR(new));
1338 fsfilt_commit(obd, new->d_inode, handle, 0);
1339 up(&parent_inode->i_sem);
1340 RETURN(PTR_ERR(new));
1341 } else if (new->d_inode) {
1342 CERROR("%s: name exists. repeat\n", obd->obd_name);
1346 new->d_fsdata = (void *) &dp;
1350 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
1351 DEBUG_REQ(D_HA, req, "replay create obj %lu/%lu",
1352 (unsigned long) body->oa.o_id,
1353 (unsigned long) body->oa.o_generation);
1354 dp.p_inum = body->oa.o_id;
1355 dp.p_generation = body->oa.o_generation;
1357 rc = vfs_mkdir(parent_inode, new, body->oa.o_mode);
1359 obdo_from_inode(&repbody->oa, new->d_inode, FILTER_VALID_FLAGS);
1360 repbody->oa.o_id = new->d_inode->i_ino;
1361 repbody->oa.o_generation = new->d_inode->i_generation;
1362 repbody->oa.o_valid |= OBD_MD_FLID | OBD_MD_FLGENER;
1364 rc = fsfilt_del_dir_entry(obd, new);
1365 up(&parent_inode->i_sem);
1368 CERROR("can't remove name for object: %d\n", rc);
1372 /* this lock should be taken to serialize MDS modifications
1373 * in failure case */
1374 res_id.name[0] = new->d_inode->i_ino;
1375 res_id.name[1] = new->d_inode->i_generation;
1376 policy.l_inodebits.bits = MDS_INODELOCK_UPDATE;
1377 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1378 res_id, LDLM_IBITS, &policy,
1379 LCK_EX, &flags, mds_blocking_ast,
1380 ldlm_completion_ast, NULL, NULL,
1381 NULL, 0, NULL, &lockh);
1385 CDEBUG(D_OTHER, "created dirobj: %lu/%lu mode %o\n",
1386 (unsigned long) new->d_inode->i_ino,
1387 (unsigned long) new->d_inode->i_generation,
1388 (unsigned) new->d_inode->i_mode);
1390 up(&parent_inode->i_sem);
1391 CERROR("%s: can't create dirobj: %d\n", obd->obd_name, rc);
1394 if (rc == 0 && body->oa.o_valid & OBD_MD_FLID) {
1395 /* this is new object for splitted dir. we have to
1396 * prevent recursive splitting on it -bzzz */
1397 mealen = obd_size_diskmd(mds->mds_lmv_exp, NULL);
1398 OBD_ALLOC(mea, mealen);
1400 GOTO(cleanup, rc = -ENOMEM);
1402 down(&new->d_inode->i_sem);
1403 rc = fsfilt_set_md(obd, new->d_inode, handle, mea, mealen);
1404 up(&new->d_inode->i_sem);
1405 OBD_FREE(mea, mealen);
1409 rc = mds_finish_transno(mds, parent_inode, handle, req, rc, 0);
1411 ptlrpc_save_lock(req, &lockh, LCK_EX);
1413 ldlm_lock_decref(&lockh, LCK_EX);
1416 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1420 static int mdt_get_info(struct ptlrpc_request *req)
1423 struct obd_export *exp = req->rq_export;
1424 int keylen, rc = 0, size = sizeof(obd_id);
1428 key = lustre_msg_buf(req->rq_reqmsg, 0, 1);
1430 DEBUG_REQ(D_HA, req, "no get_info key");
1433 keylen = req->rq_reqmsg->buflens[0];
1435 if (keylen < strlen("mdsize") || memcmp(key, "mdsize", 6) != 0)
1438 rc = lustre_pack_reply(req, 1, &size, NULL);
1442 reply = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*reply));
1443 rc = obd_get_info(exp, keylen, key, &size, reply);
1444 req->rq_repmsg->status = 0;
1448 static int mds_set_info(struct obd_export *exp, __u32 keylen,
1449 void *key, __u32 vallen, void *val)
1451 struct obd_device *obd;
1452 struct mds_obd *mds;
1456 obd = class_exp2obd(exp);
1458 CDEBUG(D_IOCTL, "invalid client cookie "LPX64"\n",
1459 exp->exp_handle.h_cookie);
1464 if (keylen == strlen("mds_num") &&
1465 memcmp(key, "mds_num", keylen) == 0) {
1468 CDEBUG(D_IOCTL, "set mds num %d\n", *(int*)val);
1469 mds->mds_num = *(int*)val;
1470 group = FILTER_GROUP_FIRST_MDS + mds->mds_num;
1471 valsize = sizeof(group);
1472 /*mds number has been changed, so the corresponding obdfilter exp
1473 *need to be changed too*/
1474 rc = obd_set_info(mds->mds_osc_exp, strlen("mds_conn"), "mds_conn",
1477 } else if (keylen == strlen("client") &&
1478 memcmp(key, "client", keylen) == 0) {
1479 if (!(exp->exp_flags & OBD_OPT_REAL_CLIENT)) {
1480 atomic_inc(&mds->mds_real_clients);
1481 CDEBUG(D_OTHER,"%s: peer from %s is real client (%d)\n",
1482 obd->obd_name, exp->exp_client_uuid.uuid,
1483 atomic_read(&mds->mds_real_clients));
1484 exp->exp_flags |= OBD_OPT_REAL_CLIENT;
1486 if (mds->mds_lmv_name) {
1487 rc = mds_lmv_connect(obd, mds->mds_lmv_name);
1492 CDEBUG(D_IOCTL, "invalid key\n");
1496 static int mdt_set_info(struct ptlrpc_request *req)
1499 struct obd_export *exp = req->rq_export;
1500 int keylen, rc = 0, vallen;
1503 key = lustre_msg_buf(req->rq_reqmsg, 0, 1);
1505 DEBUG_REQ(D_HA, req, "no set_info key");
1508 keylen = req->rq_reqmsg->buflens[0];
1510 if (keylen == strlen("mds_num") &&
1511 memcmp(key, "mds_num", keylen) == 0) {
1512 rc = lustre_pack_reply(req, 0, NULL, NULL);
1515 val = lustre_msg_buf(req->rq_reqmsg, 1, 0);
1517 vallen = req->rq_reqmsg->buflens[1];
1519 rc = obd_set_info(exp, keylen, key, vallen, val);
1520 req->rq_repmsg->status = 0;
1522 } else if (keylen == strlen("client") &&
1523 memcmp(key, "client", keylen) == 0) {
1524 rc = lustre_pack_reply(req, 0, NULL, NULL);
1527 rc = obd_set_info(exp, keylen, key, sizeof(obd_id), NULL);
1528 req->rq_repmsg->status = 0;
1531 CDEBUG(D_IOCTL, "invalid key\n");
1535 extern int ost_brw_write(struct ptlrpc_request *, struct obd_trans_info *);
1536 int mds_handle(struct ptlrpc_request *req)
1538 int should_process, fail = OBD_FAIL_MDS_ALL_REPLY_NET;
1540 struct mds_obd *mds = NULL; /* quell gcc overwarning */
1541 struct obd_device *obd = NULL;
1544 OBD_FAIL_RETURN(OBD_FAIL_MDS_ALL_REQUEST_NET | OBD_FAIL_ONCE, 0);
1546 LASSERT(current->journal_info == NULL);
1547 /* XXX identical to OST */
1548 if (req->rq_reqmsg->opc != MDS_CONNECT) {
1549 struct mds_export_data *med;
1550 int recovering, abort_recovery;
1552 if (req->rq_export == NULL) {
1553 CERROR("lustre_mds: operation %d on unconnected MDS\n",
1554 req->rq_reqmsg->opc);
1555 req->rq_status = -ENOTCONN;
1556 GOTO(out, rc = -ENOTCONN);
1559 med = &req->rq_export->exp_mds_data;
1560 obd = req->rq_export->exp_obd;
1563 /* sanity check: if the xid matches, the request must
1564 * be marked as a resent or replayed */
1565 if (req->rq_xid == med->med_mcd->mcd_last_xid)
1566 LASSERTF(lustre_msg_get_flags(req->rq_reqmsg) &
1567 (MSG_RESENT | MSG_REPLAY),
1568 "rq_xid "LPU64" matches last_xid, "
1569 "expected RESENT flag\n",
1571 /* else: note the opposite is not always true; a
1572 * RESENT req after a failover will usually not match
1573 * the last_xid, since it was likely never
1574 * committed. A REPLAYed request will almost never
1575 * match the last xid, however it could for a
1576 * committed, but still retained, open. */
1578 /* Check for aborted recovery. */
1579 spin_lock_bh(&obd->obd_processing_task_lock);
1580 abort_recovery = obd->obd_abort_recovery;
1581 recovering = obd->obd_recovering;
1582 spin_unlock_bh(&obd->obd_processing_task_lock);
1583 if (abort_recovery) {
1584 target_abort_recovery(obd);
1585 } else if (recovering) {
1586 rc = mds_filter_recovery_request(req, obd,
1588 if (rc || !should_process)
1593 switch (req->rq_reqmsg->opc) {
1595 DEBUG_REQ(D_INODE, req, "connect");
1596 OBD_FAIL_RETURN(OBD_FAIL_MDS_CONNECT_NET, 0);
1597 rc = target_handle_connect(req, mds_handle);
1599 /* Now that we have an export, set mds. */
1600 mds = mds_req2mds(req);
1603 case MDS_DISCONNECT:
1604 DEBUG_REQ(D_INODE, req, "disconnect");
1605 OBD_FAIL_RETURN(OBD_FAIL_MDS_DISCONNECT_NET, 0);
1606 rc = target_handle_disconnect(req);
1607 req->rq_status = rc; /* superfluous? */
1611 DEBUG_REQ(D_INODE, req, "getstatus");
1612 OBD_FAIL_RETURN(OBD_FAIL_MDS_GETSTATUS_NET, 0);
1613 rc = mds_getstatus(req);
1617 DEBUG_REQ(D_INODE, req, "getattr");
1618 OBD_FAIL_RETURN(OBD_FAIL_MDS_GETATTR_NET, 0);
1619 rc = mds_getattr(0, req);
1622 case MDS_GETATTR_NAME: {
1623 struct lustre_handle lockh;
1624 DEBUG_REQ(D_INODE, req, "getattr_name");
1625 OBD_FAIL_RETURN(OBD_FAIL_MDS_GETATTR_NAME_NET, 0);
1627 /* If this request gets a reconstructed reply, we won't be
1628 * acquiring any new locks in mds_getattr_name, so we don't
1632 rc = mds_getattr_name(0, req, &lockh, MDS_INODELOCK_UPDATE);
1633 /* this non-intent call (from an ioctl) is special */
1634 req->rq_status = rc;
1635 if (rc == 0 && lockh.cookie)
1636 ldlm_lock_decref(&lockh, LCK_PR);
1640 DEBUG_REQ(D_INODE, req, "statfs");
1641 OBD_FAIL_RETURN(OBD_FAIL_MDS_STATFS_NET, 0);
1642 rc = mds_statfs(req);
1646 DEBUG_REQ(D_INODE, req, "readpage");
1647 OBD_FAIL_RETURN(OBD_FAIL_MDS_READPAGE_NET, 0);
1648 rc = mds_readpage(req);
1650 if (OBD_FAIL_CHECK_ONCE(OBD_FAIL_MDS_SENDPAGE)) {
1651 if (req->rq_reply_state) {
1652 lustre_free_reply_state (req->rq_reply_state);
1653 req->rq_reply_state = NULL;
1661 __u32 *opcp = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*opcp));
1663 int size[3] = {sizeof(struct mds_body), mds->mds_max_mdsize,
1664 mds->mds_max_cookiesize};
1667 /* NB only peek inside req now; mds_reint() will swab it */
1669 CERROR ("Can't inspect opcode\n");
1674 if (lustre_msg_swabbed (req->rq_reqmsg))
1677 DEBUG_REQ(D_INODE, req, "reint %d (%s)", opc,
1678 (opc < sizeof(reint_names) / sizeof(reint_names[0]) ||
1679 reint_names[opc] == NULL) ? reint_names[opc] :
1682 OBD_FAIL_RETURN(OBD_FAIL_MDS_REINT_NET, 0);
1684 if (opc == REINT_UNLINK)
1686 else if (opc == REINT_OPEN || opc == REINT_RENAME)
1691 rc = lustre_pack_reply(req, bufcount, size, NULL);
1695 rc = mds_reint(req, 0, NULL);
1696 fail = OBD_FAIL_MDS_REINT_NET_REP;
1701 DEBUG_REQ(D_INODE, req, "close");
1702 OBD_FAIL_RETURN(OBD_FAIL_MDS_CLOSE_NET, 0);
1703 rc = mds_close(req);
1706 case MDS_DONE_WRITING:
1707 DEBUG_REQ(D_INODE, req, "done_writing");
1708 OBD_FAIL_RETURN(OBD_FAIL_MDS_DONE_WRITING_NET, 0);
1709 rc = mds_done_writing(req);
1713 DEBUG_REQ(D_INODE, req, "pin");
1714 OBD_FAIL_RETURN(OBD_FAIL_MDS_PIN_NET, 0);
1719 DEBUG_REQ(D_INODE, req, "sync");
1720 OBD_FAIL_RETURN(OBD_FAIL_MDS_SYNC_NET, 0);
1725 DEBUG_REQ(D_INODE, req, "ping");
1726 rc = target_handle_ping(req);
1729 case OBD_LOG_CANCEL:
1730 CDEBUG(D_INODE, "log cancel\n");
1731 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOG_CANCEL_NET, 0);
1732 rc = -ENOTSUPP; /* la la la */
1736 DEBUG_REQ(D_INODE, req, "enqueue");
1737 OBD_FAIL_RETURN(OBD_FAIL_LDLM_ENQUEUE, 0);
1738 rc = ldlm_handle_enqueue(req, ldlm_server_completion_ast,
1739 ldlm_server_blocking_ast, NULL);
1742 DEBUG_REQ(D_INODE, req, "convert");
1743 OBD_FAIL_RETURN(OBD_FAIL_LDLM_CONVERT, 0);
1744 rc = ldlm_handle_convert(req);
1746 case LDLM_BL_CALLBACK:
1747 case LDLM_CP_CALLBACK:
1748 DEBUG_REQ(D_INODE, req, "callback");
1749 CERROR("callbacks should not happen on MDS\n");
1751 OBD_FAIL_RETURN(OBD_FAIL_LDLM_BL_CALLBACK, 0);
1753 case LLOG_ORIGIN_HANDLE_CREATE:
1754 DEBUG_REQ(D_INODE, req, "llog_init");
1755 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
1756 rc = llog_origin_handle_create(req);
1758 case LLOG_ORIGIN_HANDLE_NEXT_BLOCK:
1759 DEBUG_REQ(D_INODE, req, "llog next block");
1760 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
1761 rc = llog_origin_handle_next_block(req);
1763 case LLOG_ORIGIN_HANDLE_PREV_BLOCK:
1764 DEBUG_REQ(D_INODE, req, "llog prev block");
1765 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
1766 rc = llog_origin_handle_prev_block(req);
1768 case LLOG_ORIGIN_HANDLE_READ_HEADER:
1769 DEBUG_REQ(D_INODE, req, "llog read header");
1770 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
1771 rc = llog_origin_handle_read_header(req);
1773 case LLOG_ORIGIN_HANDLE_CLOSE:
1774 DEBUG_REQ(D_INODE, req, "llog close");
1775 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
1776 rc = llog_origin_handle_close(req);
1779 DEBUG_REQ(D_INODE, req, "ost_create");
1780 rc = mdt_obj_create(req);
1783 DEBUG_REQ(D_INODE, req, "get_info");
1784 rc = mdt_get_info(req);
1787 DEBUG_REQ(D_INODE, req, "set_info");
1788 rc = mdt_set_info(req);
1791 CDEBUG(D_INODE, "write\n");
1792 OBD_FAIL_RETURN(OBD_FAIL_OST_BRW_NET, 0);
1793 rc = ost_brw_write(req, NULL);
1794 LASSERT(current->journal_info == NULL);
1795 /* mdt_brw sends its own replies */
1799 DEBUG_REQ(D_INODE, req, "llog catinfo");
1800 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
1801 rc = llog_catinfo(req);
1804 req->rq_status = -ENOTSUPP;
1805 rc = ptlrpc_error(req);
1809 LASSERT(current->journal_info == NULL);
1813 /* If we're DISCONNECTing, the mds_export_data is already freed */
1814 if (!rc && req->rq_reqmsg->opc != MDS_DISCONNECT) {
1815 struct mds_export_data *med = &req->rq_export->exp_mds_data;
1816 struct obd_device *obd = list_entry(mds, struct obd_device,
1818 req->rq_repmsg->last_xid =
1819 le64_to_cpu(med->med_mcd->mcd_last_xid);
1821 if (!obd->obd_no_transno) {
1822 req->rq_repmsg->last_committed =
1823 obd->obd_last_committed;
1825 DEBUG_REQ(D_IOCTL, req,
1826 "not sending last_committed update");
1828 CDEBUG(D_INFO, "last_transno "LPU64", last_committed "LPU64
1830 mds->mds_last_transno, obd->obd_last_committed,
1835 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_LAST_REPLAY) {
1836 if (obd && obd->obd_recovering) {
1837 DEBUG_REQ(D_HA, req, "LAST_REPLAY, queuing reply");
1838 return target_queue_final_reply(req, rc);
1840 /* Lost a race with recovery; let the error path DTRT. */
1841 rc = req->rq_status = -ENOTCONN;
1844 target_send_reply(req, rc, fail);
1848 /* Update the server data on disk. This stores the new mount_count and
1849 * also the last_rcvd value to disk. If we don't have a clean shutdown,
1850 * then the server last_rcvd value may be less than that of the clients.
1851 * This will alert us that we may need to do client recovery.
1853 * Also assumes for mds_last_transno that we are not modifying it (no locking).
1855 int mds_update_server_data(struct obd_device *obd, int force_sync)
1857 struct mds_obd *mds = &obd->u.mds;
1858 struct mds_server_data *msd = mds->mds_server_data;
1859 struct file *filp = mds->mds_rcvd_filp;
1860 struct lvfs_run_ctxt saved;
1865 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1866 msd->msd_last_transno = cpu_to_le64(mds->mds_last_transno);
1868 CDEBUG(D_SUPER, "MDS mount_count is "LPU64", last_transno is "LPU64"\n",
1869 mds->mds_mount_count, mds->mds_last_transno);
1870 rc = fsfilt_write_record(obd, filp, msd, sizeof(*msd), &off,force_sync);
1872 CERROR("error writing MDS server data: rc = %d\n", rc);
1873 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1878 /* mount the file system (secretly) */
1879 static int mds_setup(struct obd_device *obd, obd_count len, void *buf)
1881 struct lustre_cfg* lcfg = buf;
1882 struct mds_obd *mds = &obd->u.mds;
1883 char *options = NULL;
1884 struct vfsmount *mnt;
1889 dev_clear_rdonly(2);
1891 if (!lcfg->lcfg_inlbuf1 || !lcfg->lcfg_inlbuf2)
1892 RETURN(rc = -EINVAL);
1894 obd->obd_fsops = fsfilt_get_ops(lcfg->lcfg_inlbuf2);
1895 if (IS_ERR(obd->obd_fsops))
1896 RETURN(rc = PTR_ERR(obd->obd_fsops));
1898 mds->mds_max_mdsize = sizeof(struct lov_mds_md);
1900 page = __get_free_page(GFP_KERNEL);
1904 options = (char *)page;
1905 memset(options, 0, PAGE_SIZE);
1907 /* here we use "iopen_nopriv" hardcoded, because it affects MDS utility
1908 * and the rest of options are passed by mount options. Probably this
1909 * should be moved to somewhere else like startup scripts or lconf. */
1910 sprintf(options, "iopen_nopriv");
1912 if (lcfg->lcfg_inllen4 > 0 && lcfg->lcfg_inlbuf4)
1913 sprintf(options + strlen(options), ",%s",
1914 lcfg->lcfg_inlbuf4);
1916 /* we have to know mdsnum before touching underlying fs -bzzz */
1917 if (lcfg->lcfg_inllen5 > 0 && lcfg->lcfg_inlbuf5 &&
1918 strcmp(lcfg->lcfg_inlbuf5, "dumb")) {
1921 CDEBUG(D_OTHER, "MDS: %s is master for %s\n",
1922 obd->obd_name, lcfg->lcfg_inlbuf5);
1924 generate_random_uuid(uuid);
1925 class_uuid_unparse(uuid, &mds->mds_lmv_uuid);
1927 OBD_ALLOC(mds->mds_lmv_name, lcfg->lcfg_inllen5);
1928 if (mds->mds_lmv_name == NULL)
1929 RETURN(rc = -ENOMEM);
1931 memcpy(mds->mds_lmv_name, lcfg->lcfg_inlbuf5,
1932 lcfg->lcfg_inllen5);
1934 rc = mds_lmv_connect(obd, mds->mds_lmv_name);
1936 OBD_FREE(mds->mds_lmv_name, lcfg->lcfg_inllen5);
1941 /* FIXME-WANGDI: this should be reworked when we will use lmv along
1942 * with cobd, because correct mdsnum is set in mds_lmv_connect(). */
1943 if (lcfg->lcfg_inllen6 > 0 && lcfg->lcfg_inlbuf6 && !mds->mds_lmv_obd &&
1944 strcmp(lcfg->lcfg_inlbuf6, "dumb")) {
1945 if (!memcmp(lcfg->lcfg_inlbuf6, "master", strlen("master")) &&
1946 mds->mds_num == 0) {
1947 mds->mds_num = REAL_MDS_NUMBER;
1948 } else if (!memcmp(lcfg->lcfg_inlbuf6, "cache", strlen("cache")) &&
1949 mds->mds_num == 0) {
1950 mds->mds_num = CACHE_MDS_NUMBER;
1954 mnt = do_kern_mount(lcfg->lcfg_inlbuf2, 0,
1955 lcfg->lcfg_inlbuf1, options);
1961 CERROR("do_kern_mount failed: rc = %d\n", rc);
1965 CDEBUG(D_SUPER, "%s: mnt = %p\n", lcfg->lcfg_inlbuf1, mnt);
1967 sema_init(&mds->mds_orphan_recovery_sem, 1);
1968 sema_init(&mds->mds_epoch_sem, 1);
1969 spin_lock_init(&mds->mds_transno_lock);
1970 mds->mds_max_cookiesize = sizeof(struct llog_cookie);
1971 atomic_set(&mds->mds_open_count, 0);
1972 atomic_set(&mds->mds_real_clients, 0);
1974 obd->obd_namespace = ldlm_namespace_new(obd->obd_name,
1975 LDLM_NAMESPACE_SERVER);
1976 if (obd->obd_namespace == NULL) {
1977 mds_cleanup(obd, 0);
1978 GOTO(err_put, rc = -ENOMEM);
1980 ldlm_register_intent(obd->obd_namespace, mds_intent_policy);
1982 rc = mds_fs_setup(obd, mnt);
1984 CERROR("MDS filesystem method init failed: rc = %d\n", rc);
1988 rc = llog_start_commit_thread();
1992 if (lcfg->lcfg_inllen3 > 0 && lcfg->lcfg_inlbuf3) {
1995 generate_random_uuid(uuid);
1996 class_uuid_unparse(uuid, &mds->mds_lov_uuid);
1998 OBD_ALLOC(mds->mds_profile, lcfg->lcfg_inllen3);
1999 if (mds->mds_profile == NULL)
2000 GOTO(err_fs, rc = -ENOMEM);
2002 memcpy(mds->mds_profile, lcfg->lcfg_inlbuf3,
2003 lcfg->lcfg_inllen3);
2006 ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
2007 "mds_ldlm_client", &obd->obd_ldlm_client);
2008 obd->obd_replayable = 1;
2010 rc = mds_postsetup(obd);
2017 /* No extra cleanup needed for llog_init_commit_thread() */
2018 mds_fs_cleanup(obd, 0);
2020 ldlm_namespace_free(obd->obd_namespace, 0);
2021 obd->obd_namespace = NULL;
2024 mntput(mds->mds_vfsmnt);
2028 fsfilt_put_ops(obd->obd_fsops);
2032 static int mds_postsetup(struct obd_device *obd)
2034 struct mds_obd *mds = &obd->u.mds;
2038 rc = obd_llog_setup(obd, &obd->obd_llogs, LLOG_CONFIG_ORIG_CTXT,
2039 obd, 0, NULL, &llog_lvfs_ops);
2043 /* This check for @dumb string is needed to handle mounting MDS
2044 with smfs. Read lconf:MDSDEV.write_conf() for more detail
2046 if (mds->mds_profile && strcmp(mds->mds_profile, "dumb")) {
2047 struct lvfs_run_ctxt saved;
2048 struct lustre_profile *lprof;
2049 struct config_llog_instance cfg;
2051 cfg.cfg_instance = NULL;
2052 cfg.cfg_uuid = mds->mds_lov_uuid;
2053 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
2054 rc = class_config_parse_llog(llog_get_context(&obd->obd_llogs, LLOG_CONFIG_ORIG_CTXT),
2055 mds->mds_profile, &cfg);
2056 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
2060 lprof = class_get_profile(mds->mds_profile);
2061 if (lprof == NULL) {
2062 CERROR("No profile found: %s\n", mds->mds_profile);
2063 GOTO(err_cleanup, rc = -ENOENT);
2065 rc = mds_lov_connect(obd, lprof->lp_osc);
2067 GOTO(err_cleanup, rc);
2069 rc = mds_lmv_postsetup(obd);
2071 GOTO(err_cleanup, rc);
2079 obd_llog_cleanup(llog_get_context(&obd->obd_llogs, LLOG_CONFIG_ORIG_CTXT));
2083 static int mds_postrecov(struct obd_device *obd)
2086 struct llog_ctxt *ctxt;
2090 LASSERT(!obd->obd_recovering);
2091 ctxt = llog_get_context(&obd->obd_llogs, LLOG_UNLINK_ORIG_CTXT);
2092 LASSERT(ctxt != NULL);
2094 rc = llog_connect(ctxt, obd->u.mds.mds_lov_desc.ld_tgt_count,
2097 CERROR("faild at llog_origin_connect: %d\n", rc);
2100 rc = mds_cleanup_orphans(obd);
2102 rc2 = mds_lov_set_nextid(obd);
2108 int mds_lov_clean(struct obd_device *obd)
2110 struct mds_obd *mds = &obd->u.mds;
2112 if (mds->mds_profile) {
2114 struct config_llog_instance cfg;
2115 struct lvfs_run_ctxt saved;
2116 int len = strlen(mds->mds_profile) + sizeof("-clean") + 1;
2118 OBD_ALLOC(cln_prof, len);
2119 sprintf(cln_prof, "%s-clean", mds->mds_profile);
2121 cfg.cfg_instance = NULL;
2122 cfg.cfg_uuid = mds->mds_lov_uuid;
2124 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
2125 class_config_parse_llog(llog_get_context(&obd->obd_llogs, LLOG_CONFIG_ORIG_CTXT),
2127 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
2129 OBD_FREE(cln_prof, len);
2130 OBD_FREE(mds->mds_profile, strlen(mds->mds_profile) + 1);
2131 mds->mds_profile = NULL;
2136 int mds_lmv_clean(struct obd_device *obd)
2138 struct mds_obd *mds = &obd->u.mds;
2140 if (mds->mds_lmv_name) {
2141 OBD_FREE(mds->mds_lmv_name, strlen(mds->mds_lmv_name) + 1);
2142 mds->mds_lmv_name = NULL;
2147 static int mds_precleanup(struct obd_device *obd, int flags)
2153 mds_lov_disconnect(obd, flags);
2155 obd_llog_cleanup(llog_get_context(&obd->obd_llogs, LLOG_CONFIG_ORIG_CTXT));
2159 static int mds_cleanup(struct obd_device *obd, int flags)
2161 struct mds_obd *mds = &obd->u.mds;
2164 if (mds->mds_sb == NULL)
2167 mds_update_server_data(obd, 1);
2168 if (mds->mds_lov_objids != NULL) {
2169 OBD_FREE(mds->mds_lov_objids,
2170 mds->mds_lov_desc.ld_tgt_count * sizeof(obd_id));
2172 mds_fs_cleanup(obd, flags);
2176 /* 2 seems normal on mds, (may_umount() also expects 2
2177 fwiw), but we only see 1 at this point in obdfilter. */
2178 if (atomic_read(&obd->u.mds.mds_vfsmnt->mnt_count) > 2)
2179 CERROR("%s: mount busy, mnt_count %d != 2\n", obd->obd_name,
2180 atomic_read(&obd->u.mds.mds_vfsmnt->mnt_count));
2182 mntput(mds->mds_vfsmnt);
2186 ldlm_namespace_free(obd->obd_namespace, flags & OBD_OPT_FORCE);
2188 spin_lock_bh(&obd->obd_processing_task_lock);
2189 if (obd->obd_recovering) {
2190 target_cancel_recovery_timer(obd);
2191 obd->obd_recovering = 0;
2193 spin_unlock_bh(&obd->obd_processing_task_lock);
2196 dev_clear_rdonly(2);
2197 fsfilt_put_ops(obd->obd_fsops);
2202 static void fixup_handle_for_resent_req(struct ptlrpc_request *req,
2203 struct ldlm_lock *new_lock,
2204 struct lustre_handle *lockh)
2206 struct obd_export *exp = req->rq_export;
2207 struct obd_device *obd = exp->exp_obd;
2208 struct ldlm_request *dlmreq =
2209 lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*dlmreq));
2210 struct lustre_handle remote_hdl = dlmreq->lock_handle1;
2211 struct list_head *iter;
2213 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
2216 l_lock(&obd->obd_namespace->ns_lock);
2217 list_for_each(iter, &exp->exp_ldlm_data.led_held_locks) {
2218 struct ldlm_lock *lock;
2219 lock = list_entry(iter, struct ldlm_lock, l_export_chain);
2220 if (lock == new_lock)
2222 if (lock->l_remote_handle.cookie == remote_hdl.cookie) {
2223 lockh->cookie = lock->l_handle.h_cookie;
2224 DEBUG_REQ(D_HA, req, "restoring lock cookie "LPX64,
2226 l_unlock(&obd->obd_namespace->ns_lock);
2230 l_unlock(&obd->obd_namespace->ns_lock);
2232 /* If the xid matches, then we know this is a resent request,
2233 * and allow it. (It's probably an OPEN, for which we don't
2235 if (req->rq_xid == exp->exp_mds_data.med_mcd->mcd_last_xid)
2238 /* This remote handle isn't enqueued, so we never received or
2239 * processed this request. Clear MSG_RESENT, because it can
2240 * be handled like any normal request now. */
2242 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
2244 DEBUG_REQ(D_HA, req, "no existing lock with rhandle "LPX64,
2248 int intent_disposition(struct ldlm_reply *rep, int flag)
2252 return (rep->lock_policy_res1 & flag);
2255 void intent_set_disposition(struct ldlm_reply *rep, int flag)
2259 rep->lock_policy_res1 |= flag;
2262 static int mds_intent_policy(struct ldlm_namespace *ns,
2263 struct ldlm_lock **lockp, void *req_cookie,
2264 ldlm_mode_t mode, int flags, void *data)
2266 struct ptlrpc_request *req = req_cookie;
2267 struct ldlm_lock *lock = *lockp;
2268 struct ldlm_intent *it;
2269 struct mds_obd *mds = &req->rq_export->exp_obd->u.mds;
2270 struct ldlm_reply *rep;
2271 struct lustre_handle lockh = { 0 };
2272 struct ldlm_lock *new_lock;
2273 int getattr_part = MDS_INODELOCK_UPDATE;
2274 int rc, offset = 2, repsize[4] = {sizeof(struct ldlm_reply),
2275 sizeof(struct mds_body),
2276 mds->mds_max_mdsize,
2277 mds->mds_max_cookiesize};
2280 LASSERT(req != NULL);
2282 if (req->rq_reqmsg->bufcount <= 1) {
2283 /* No intent was provided */
2284 int size = sizeof(struct ldlm_reply);
2285 rc = lustre_pack_reply(req, 1, &size, NULL);
2290 it = lustre_swab_reqbuf(req, 1, sizeof(*it), lustre_swab_ldlm_intent);
2292 CERROR("Intent missing\n");
2293 RETURN(req->rq_status = -EFAULT);
2296 LDLM_DEBUG(lock, "intent policy, opc: %s", ldlm_it2str(it->opc));
2298 rc = lustre_pack_reply(req, it->opc == IT_UNLINK ? 4 : 3, repsize,
2301 RETURN(req->rq_status = rc);
2303 rep = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*rep));
2304 intent_set_disposition(rep, DISP_IT_EXECD);
2306 fixup_handle_for_resent_req(req, lock, &lockh);
2308 /* execute policy */
2309 switch ((long)it->opc) {
2311 case IT_CREAT|IT_OPEN:
2312 /* XXX swab here to assert that an mds_open reint
2313 * packet is following */
2314 rep->lock_policy_res2 = mds_reint(req, offset, &lockh);
2316 /* We abort the lock if the lookup was negative and
2317 * we did not make it to the OPEN portion */
2318 if (!intent_disposition(rep, DISP_LOOKUP_EXECD))
2319 RETURN(ELDLM_LOCK_ABORTED);
2320 if (intent_disposition(rep, DISP_LOOKUP_NEG) &&
2321 !intent_disposition(rep, DISP_OPEN_OPEN))
2323 if (rep->lock_policy_res2) {
2324 /* mds_open returns ENOLCK where it
2325 * should return zero, but it has no
2327 if (rep->lock_policy_res2 == ENOLCK)
2328 rep->lock_policy_res2 = 0;
2329 RETURN(ELDLM_LOCK_ABORTED);
2333 getattr_part = MDS_INODELOCK_LOOKUP;
2335 getattr_part |= MDS_INODELOCK_LOOKUP;
2337 rep->lock_policy_res2 = mds_getattr_name(offset, req, &lockh,
2339 /* FIXME: LDLM can set req->rq_status. MDS sets
2340 policy_res{1,2} with disposition and status.
2341 - replay: returns 0 & req->status is old status
2342 - otherwise: returns req->status */
2343 if (intent_disposition(rep, DISP_LOOKUP_NEG))
2344 rep->lock_policy_res2 = 0;
2345 if (!intent_disposition(rep, DISP_LOOKUP_POS) ||
2346 rep->lock_policy_res2)
2347 RETURN(ELDLM_LOCK_ABORTED);
2348 if (req->rq_status != 0) {
2350 rep->lock_policy_res2 = req->rq_status;
2351 RETURN(ELDLM_LOCK_ABORTED);
2355 CERROR("Unhandled intent "LPD64"\n", it->opc);
2359 /* By this point, whatever function we called above must have either
2360 * filled in 'lockh', been an intent replay, or returned an error. We
2361 * want to allow replayed RPCs to not get a lock, since we would just
2362 * drop it below anyways because lock replay is done separately by the
2363 * client afterwards. For regular RPCs we want to give the new lock to
2364 * the client instead of whatever lock it was about to get. */
2365 new_lock = ldlm_handle2lock(&lockh);
2366 if (new_lock == NULL && (flags & LDLM_FL_INTENT_ONLY))
2369 LASSERT(new_lock != NULL);
2371 /* If we've already given this lock to a client once, then we should
2372 * have no readers or writers. Otherwise, we should have one reader
2373 * _or_ writer ref (which will be zeroed below) before returning the
2374 * lock to a client. */
2375 if (new_lock->l_export == req->rq_export) {
2376 LASSERT(new_lock->l_readers + new_lock->l_writers == 0);
2378 LASSERT(new_lock->l_export == NULL);
2379 LASSERT(new_lock->l_readers + new_lock->l_writers == 1);
2384 if (new_lock->l_export == req->rq_export) {
2385 /* Already gave this to the client, which means that we
2386 * reconstructed a reply. */
2387 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
2389 RETURN(ELDLM_LOCK_REPLACED);
2392 /* Fixup the lock to be given to the client */
2393 l_lock(&new_lock->l_resource->lr_namespace->ns_lock);
2394 new_lock->l_readers = 0;
2395 new_lock->l_writers = 0;
2397 new_lock->l_export = class_export_get(req->rq_export);
2398 list_add(&new_lock->l_export_chain,
2399 &new_lock->l_export->exp_ldlm_data.led_held_locks);
2401 new_lock->l_blocking_ast = lock->l_blocking_ast;
2402 new_lock->l_completion_ast = lock->l_completion_ast;
2404 memcpy(&new_lock->l_remote_handle, &lock->l_remote_handle,
2405 sizeof(lock->l_remote_handle));
2407 new_lock->l_flags &= ~LDLM_FL_LOCAL;
2409 LDLM_LOCK_PUT(new_lock);
2410 l_unlock(&new_lock->l_resource->lr_namespace->ns_lock);
2412 RETURN(ELDLM_LOCK_REPLACED);
2415 int mds_attach(struct obd_device *dev, obd_count len, void *data)
2417 struct lprocfs_static_vars lvars;
2419 lprocfs_init_multi_vars(0, &lvars);
2420 return lprocfs_obd_attach(dev, lvars.obd_vars);
2423 int mds_detach(struct obd_device *dev)
2425 return lprocfs_obd_detach(dev);
2428 int mdt_attach(struct obd_device *dev, obd_count len, void *data)
2430 struct lprocfs_static_vars lvars;
2432 lprocfs_init_multi_vars(1, &lvars);
2433 return lprocfs_obd_attach(dev, lvars.obd_vars);
2436 int mdt_detach(struct obd_device *dev)
2438 return lprocfs_obd_detach(dev);
2441 static int mdt_setup(struct obd_device *obd, obd_count len, void *buf)
2443 struct mds_obd *mds = &obd->u.mds;
2448 ptlrpc_init_svc(MDS_NBUFS, MDS_BUFSIZE, MDS_MAXREQSIZE,
2449 MDS_REQUEST_PORTAL, MDC_REPLY_PORTAL,
2450 mds_handle, "mds", obd->obd_proc_entry);
2452 if (!mds->mds_service) {
2453 CERROR("failed to start service\n");
2457 rc = ptlrpc_start_n_threads(obd, mds->mds_service, MDT_NUM_THREADS,
2460 GOTO(err_thread, rc);
2462 mds->mds_setattr_service =
2463 ptlrpc_init_svc(MDS_NBUFS, MDS_BUFSIZE, MDS_MAXREQSIZE,
2464 MDS_SETATTR_PORTAL, MDC_REPLY_PORTAL,
2465 mds_handle, "mds_setattr",
2466 obd->obd_proc_entry);
2467 if (!mds->mds_setattr_service) {
2468 CERROR("failed to start getattr service\n");
2469 GOTO(err_thread, rc = -ENOMEM);
2472 rc = ptlrpc_start_n_threads(obd, mds->mds_setattr_service,
2473 MDT_NUM_THREADS, "ll_mdt_attr");
2475 GOTO(err_thread2, rc);
2477 mds->mds_readpage_service =
2478 ptlrpc_init_svc(MDS_NBUFS, MDS_BUFSIZE, MDS_MAXREQSIZE,
2479 MDS_READPAGE_PORTAL, MDC_REPLY_PORTAL,
2480 mds_handle, "mds_readpage",
2481 obd->obd_proc_entry);
2482 if (!mds->mds_readpage_service) {
2483 CERROR("failed to start readpage service\n");
2484 GOTO(err_thread2, rc = -ENOMEM);
2487 rc = ptlrpc_start_n_threads(obd, mds->mds_readpage_service,
2488 MDT_NUM_THREADS, "ll_mdt_rdpg");
2491 GOTO(err_thread3, rc);
2496 ptlrpc_unregister_service(mds->mds_readpage_service);
2498 ptlrpc_unregister_service(mds->mds_setattr_service);
2500 ptlrpc_unregister_service(mds->mds_service);
2504 static int mdt_cleanup(struct obd_device *obd, int flags)
2506 struct mds_obd *mds = &obd->u.mds;
2509 ptlrpc_stop_all_threads(mds->mds_readpage_service);
2510 ptlrpc_unregister_service(mds->mds_readpage_service);
2512 ptlrpc_stop_all_threads(mds->mds_setattr_service);
2513 ptlrpc_unregister_service(mds->mds_setattr_service);
2515 ptlrpc_stop_all_threads(mds->mds_service);
2516 ptlrpc_unregister_service(mds->mds_service);
2521 static struct dentry *mds_lvfs_fid2dentry(__u64 id, __u32 gen, __u64 gr,
2524 struct obd_device *obd = data;
2527 fid.generation = gen;
2528 return mds_fid2dentry(&obd->u.mds, &fid, NULL);
2531 static int mds_get_info(struct obd_export *exp, __u32 keylen,
2532 void *key, __u32 *vallen, void *val)
2534 struct obd_device *obd;
2535 struct mds_obd *mds;
2538 obd = class_exp2obd(exp);
2540 CDEBUG(D_IOCTL, "invalid client cookie "LPX64"\n",
2541 exp->exp_handle.h_cookie);
2545 if (keylen >= strlen("reint_log") && memcmp(key, "reint_log", 9) == 0) {
2546 /*Get log_context handle*/
2547 unsigned long *llh_handle = val;
2548 *vallen = sizeof(unsigned long);
2549 *llh_handle = (unsigned long)obd->obd_llog_ctxt[LLOG_REINT_ORIG_CTXT];
2552 if (keylen >= strlen("cache_sb") && memcmp(key, "cache_sb", 8) == 0) {
2553 /*Get log_context handle*/
2554 unsigned long *sb = val;
2555 *vallen = sizeof(unsigned long);
2556 *sb = (unsigned long)obd->u.mds.mds_sb;
2561 keylen == strlen("mdsize");
2562 if (keylen && memcmp(key, "mdsize", keylen) == 0) {
2563 __u32 *mdsize = val;
2564 *vallen = sizeof(*mdsize);
2565 *mdsize = mds->mds_max_mdsize;
2569 CDEBUG(D_IOCTL, "invalid key\n");
2573 struct lvfs_callback_ops mds_lvfs_ops = {
2574 l_fid2dentry: mds_lvfs_fid2dentry,
2577 int mds_preprw(int cmd, struct obd_export *exp, struct obdo *oa,
2578 int objcount, struct obd_ioobj *obj,
2579 int niocount, struct niobuf_remote *nb,
2580 struct niobuf_local *res,
2581 struct obd_trans_info *oti);
2582 int mds_commitrw(int cmd, struct obd_export *exp, struct obdo *oa,
2583 int objcount, struct obd_ioobj *obj, int niocount,
2584 struct niobuf_local *res, struct obd_trans_info *oti,
2587 /* use obd ops to offer management infrastructure */
2588 static struct obd_ops mds_obd_ops = {
2589 .o_owner = THIS_MODULE,
2590 .o_attach = mds_attach,
2591 .o_detach = mds_detach,
2592 .o_connect = mds_connect,
2593 .o_init_export = mds_init_export,
2594 .o_destroy_export = mds_destroy_export,
2595 .o_disconnect = mds_disconnect,
2596 .o_setup = mds_setup,
2597 .o_precleanup = mds_precleanup,
2598 .o_cleanup = mds_cleanup,
2599 .o_postrecov = mds_postrecov,
2600 .o_statfs = mds_obd_statfs,
2601 .o_iocontrol = mds_iocontrol,
2602 .o_create = mds_obd_create,
2603 .o_destroy = mds_obd_destroy,
2604 .o_llog_init = mds_llog_init,
2605 .o_llog_finish = mds_llog_finish,
2606 .o_notify = mds_notify,
2607 .o_get_info = mds_get_info,
2608 .o_set_info = mds_set_info,
2609 .o_preprw = mds_preprw,
2610 .o_commitrw = mds_commitrw,
2613 static struct obd_ops mdt_obd_ops = {
2614 .o_owner = THIS_MODULE,
2615 .o_attach = mdt_attach,
2616 .o_detach = mdt_detach,
2617 .o_setup = mdt_setup,
2618 .o_cleanup = mdt_cleanup,
2619 .o_attach = mdt_attach,
2620 .o_detach = mdt_detach,
2623 static int __init mds_init(void)
2625 struct lprocfs_static_vars lvars;
2627 lprocfs_init_multi_vars(0, &lvars);
2628 class_register_type(&mds_obd_ops, NULL, lvars.module_vars,
2630 lprocfs_init_multi_vars(1, &lvars);
2631 class_register_type(&mdt_obd_ops, NULL, lvars.module_vars,
2637 static void /*__exit*/ mds_exit(void)
2639 class_unregister_type(LUSTRE_MDS_NAME);
2640 class_unregister_type(LUSTRE_MDT_NAME);
2643 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
2644 MODULE_DESCRIPTION("Lustre Metadata Server (MDS)");
2645 MODULE_LICENSE("GPL");
2647 module_init(mds_init);
2648 module_exit(mds_exit);