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/obd_ost.h>
53 #include <linux/lustre_mds.h>
54 #include <linux/lustre_fsfilt.h>
55 #include <linux/lprocfs_status.h>
56 #include <linux/lustre_commit_confd.h>
58 #include "mds_internal.h"
60 static int mds_intent_policy(struct ldlm_namespace *ns,
61 struct ldlm_lock **lockp, void *req_cookie,
62 ldlm_mode_t mode, int flags, void *data);
63 static int mds_postsetup(struct obd_device *obd);
64 static int mds_cleanup(struct obd_device *obd, int flags);
67 /* Assumes caller has already pushed into the kernel filesystem context */
68 static int mds_sendpage(struct ptlrpc_request *req, struct file *file,
69 loff_t offset, int count)
71 struct ptlrpc_bulk_desc *desc;
72 struct l_wait_info lwi;
74 int rc = 0, npages, i, tmpcount, tmpsize = 0;
77 LASSERT((offset & (PAGE_SIZE - 1)) == 0); /* I'm dubious about this */
79 npages = (count + PAGE_SIZE - 1) >> PAGE_SHIFT;
80 OBD_ALLOC(pages, sizeof(*pages) * npages);
82 GOTO(out, rc = -ENOMEM);
84 desc = ptlrpc_prep_bulk_exp(req, npages, BULK_PUT_SOURCE,
87 GOTO(out_free, rc = -ENOMEM);
89 for (i = 0, tmpcount = count; i < npages; i++, tmpcount -= tmpsize) {
90 tmpsize = tmpcount > PAGE_SIZE ? PAGE_SIZE : tmpcount;
92 pages[i] = alloc_pages(GFP_KERNEL, 0);
94 GOTO(cleanup_buf, rc = -ENOMEM);
96 ptlrpc_prep_bulk_page(desc, pages[i], 0, tmpsize);
99 for (i = 0, tmpcount = count; i < npages; i++, tmpcount -= tmpsize) {
100 tmpsize = tmpcount > PAGE_SIZE ? PAGE_SIZE : tmpcount;
101 CDEBUG(D_EXT2, "reading %u@%llu from dir %lu (size %llu)\n",
102 tmpsize, offset, file->f_dentry->d_inode->i_ino,
103 file->f_dentry->d_inode->i_size);
105 rc = fsfilt_readpage(req->rq_export->exp_obd, file,
106 kmap(pages[i]), tmpsize, &offset);
110 GOTO(cleanup_buf, rc = -EIO);
113 LASSERT(desc->bd_nob == count);
115 rc = ptlrpc_start_bulk_transfer(desc);
117 GOTO(cleanup_buf, rc);
119 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE)) {
120 CERROR("obd_fail_loc=%x, fail operation rc=%d\n",
121 OBD_FAIL_MDS_SENDPAGE, rc = -EIO);
122 GOTO(abort_bulk, rc);
125 lwi = LWI_TIMEOUT(obd_timeout * HZ / 4, NULL, NULL);
126 rc = l_wait_event(desc->bd_waitq, !ptlrpc_bulk_active(desc), &lwi);
127 LASSERT (rc == 0 || rc == -ETIMEDOUT);
130 if (desc->bd_success &&
131 desc->bd_nob_transferred == count)
132 GOTO(cleanup_buf, rc);
134 rc = -ETIMEDOUT; /* XXX should this be a different errno? */
137 DEBUG_REQ(D_ERROR, req, "bulk failed: %s %d(%d), evicting %s@%s\n",
138 (rc == -ETIMEDOUT) ? "timeout" : "network error",
139 desc->bd_nob_transferred, count,
140 req->rq_export->exp_client_uuid.uuid,
141 req->rq_export->exp_connection->c_remote_uuid.uuid);
143 ptlrpc_fail_export(req->rq_export);
147 ptlrpc_abort_bulk (desc);
149 for (i = 0; i < npages; i++)
151 __free_pages(pages[i], 0);
153 ptlrpc_free_bulk(desc);
155 OBD_FREE(pages, sizeof(*pages) * npages);
160 int mds_lock_mode_for_dir(struct obd_device *obd,
161 struct dentry *dentry, int mode)
165 /* any dir access needs couple locks:
166 * 1) on part of dir we gonna lookup/modify in
167 * 2) on a whole dir to protect it from concurrent splitting
168 * and to flush client's cache for readdir()
169 * so, for a given mode and dentry this routine decides what
170 * lock mode to use for lock #2:
171 * 1) if caller's gonna lookup in dir then we need to protect
172 * dir from being splitted only - LCK_CR
173 * 2) if caller's gonna modify dir then we need to protect
174 * dir from being splitted and to flush cache - LCK_CW
175 * 3) if caller's gonna modify dir and that dir seems ready
176 * for splitting then we need to protect it from any
177 * type of access (lookup/modify/split) - LCK_EX -bzzz */
179 split = mds_splitting_expected(obd, dentry);
182 * it is important to check here only for MDS_NO_SPLITTABLE. The reason
183 * is that MDS_NO_SPLITTABLE means dir is not splittable in principle
184 * and another thread will not split it on the quiet. But if we have
185 * MDS_NO_SPLIT_EXPECTED, this means, that dir may be splitted anytime,
186 * but not now (for current thread) and we should consider that it can
187 * happen soon and go that branch which can yield LCK_EX to protect from
188 * possible splitting.
190 if (split == MDS_NO_SPLITTABLE) {
192 * this inode won't be splitted. so we need not to protect from
193 * just flush client's cache on modification.
202 if (mode == LCK_PR) {
204 } else if (mode == LCK_PW) {
206 * caller gonna modify directory.we use concurrent write
207 * lock here to retract client's cache for readdir.
210 if (split == MDS_EXPECT_SPLIT) {
212 * splitting possible. serialize any access the
213 * idea is that first one seen dir is splittable
214 * is given exclusive lock and split
215 * directory. caller passes lock mode to
216 * mds_try_to_split_dir() and splitting would be
217 * done with exclusive lock only -bzzz.
219 CDEBUG(D_OTHER, "%s: gonna split %lu/%lu\n",
221 (unsigned long)dentry->d_inode->i_ino,
222 (unsigned long)dentry->d_inode->i_generation);
230 /* only valid locked dentries or errors should be returned */
231 struct dentry *mds_id2locked_dentry(struct obd_device *obd, struct lustre_id *id,
232 struct vfsmount **mnt, int lock_mode,
233 struct lustre_handle *lockh, int *mode,
234 char *name, int namelen, __u64 lockpart)
236 struct dentry *de = mds_id2dentry(obd, id, mnt), *retval = de;
237 ldlm_policy_data_t policy = { .l_inodebits = { lockpart } };
238 struct ldlm_res_id res_id = { .name = {0} };
246 res_id.name[0] = id_fid(id);
247 res_id.name[1] = id_group(id);
250 if (name && IS_PDIROPS(de->d_inode)) {
251 ldlm_policy_data_t cpolicy =
252 { .l_inodebits = { MDS_INODELOCK_UPDATE } };
253 LASSERT(mode != NULL);
254 *mode = mds_lock_mode_for_dir(obd, de, lock_mode);
256 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
258 &cpolicy, *mode, &flags,
260 ldlm_completion_ast, NULL, NULL,
261 NULL, 0, NULL, lockh + 1);
262 if (rc != ELDLM_OK) {
264 RETURN(ERR_PTR(-ENOLCK));
269 res_id.name[2] = full_name_hash(name, namelen);
271 CDEBUG(D_INFO, "take lock on "DLID4":"LPX64"\n",
272 OLID4(id), res_id.name[2]);
275 #warning "No PDIROPS support in the kernel"
277 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace, res_id,
278 LDLM_IBITS, &policy, lock_mode, &flags,
279 mds_blocking_ast, ldlm_completion_ast,
280 NULL, NULL, NULL, 0, NULL, lockh);
281 if (rc != ELDLM_OK) {
283 retval = ERR_PTR(-EIO); /* XXX translate ldlm code */
286 ldlm_lock_decref(lockh + 1, LCK_CW);
293 #ifndef DCACHE_DISCONNECTED
294 #define DCACHE_DISCONNECTED DCACHE_NFSD_DISCONNECTED
298 /* Look up an entry by inode number. This function ONLY returns valid dget'd
299 * dentries with an initialized inode or errors */
300 struct dentry *mds_id2dentry(struct obd_device *obd, struct lustre_id *id,
301 struct vfsmount **mnt)
303 unsigned long ino = (unsigned long)id_ino(id);
304 __u32 generation = (__u32)id_gen(id);
305 struct mds_obd *mds = &obd->u.mds;
306 struct dentry *result;
311 RETURN(ERR_PTR(-ESTALE));
313 snprintf(idname, sizeof(idname), "0x%lx", ino);
315 CDEBUG(D_DENTRY, "--> mds_id2dentry: ino/gen %lu/%u, sb %p\n",
316 ino, generation, mds->mds_sb);
318 /* under ext3 this is neither supposed to return bad inodes nor NULL
320 result = ll_lookup_one_len(idname, mds->mds_id_de,
325 inode = result->d_inode;
327 RETURN(ERR_PTR(-ENOENT));
329 if (is_bad_inode(inode)) {
330 CERROR("bad inode returned %lu/%u\n",
331 inode->i_ino, inode->i_generation);
333 RETURN(ERR_PTR(-ENOENT));
336 /* here we disabled generation check, as root inode i_generation
337 * of cache mds and real mds are different. */
338 if (inode->i_ino != id_ino(&mds->mds_rootid) && generation &&
339 inode->i_generation != generation) {
340 /* we didn't find the right inode.. */
341 CERROR("bad inode %lu, link: %lu, ct: %d, generation %u/%u\n",
342 inode->i_ino, (unsigned long)inode->i_nlink,
343 atomic_read(&inode->i_count), inode->i_generation,
346 RETURN(ERR_PTR(-ENOENT));
350 *mnt = mds->mds_vfsmnt;
358 /* Establish a connection to the MDS.
360 * This will set up an export structure for the client to hold state data about
361 * that client, like open files, the last operation number it did on the server,
364 static int mds_connect(struct lustre_handle *conn, struct obd_device *obd,
365 struct obd_uuid *cluuid, unsigned long flags)
367 struct mds_export_data *med;
368 struct mds_client_data *mcd;
369 struct obd_export *exp;
373 if (!conn || !obd || !cluuid)
376 /* XXX There is a small race between checking the list and adding a new
377 * connection for the same UUID, but the real threat (list corruption
378 * when multiple different clients connect) is solved.
380 * There is a second race between adding the export to the list, and
381 * filling in the client data below. Hence skipping the case of NULL
382 * mcd above. We should already be controlling multiple connects at the
383 * client, and we can't hold the spinlock over memory allocations
384 * without risk of deadlocking.
386 rc = class_connect(conn, obd, cluuid);
389 exp = class_conn2export(conn);
391 LASSERT(exp != NULL);
392 med = &exp->exp_mds_data;
394 OBD_ALLOC(mcd, sizeof(*mcd));
396 CERROR("%s: out of memory for client data.\n",
398 GOTO(out, rc = -ENOMEM);
401 memcpy(mcd->mcd_uuid, cluuid, sizeof(mcd->mcd_uuid));
404 rc = mds_client_add(obd, &obd->u.mds, med, -1);
411 OBD_FREE(mcd, sizeof(*mcd));
412 class_disconnect(exp, 0);
414 class_export_put(exp);
419 static int mds_connect_post(struct obd_export *exp, unsigned long connect_flags)
421 struct obd_device *obd = exp->exp_obd;
422 struct mds_obd *mds = &obd->u.mds;
426 if (!(connect_flags & OBD_OPT_MDS_CONNECTION)) {
427 if (!(exp->exp_flags & OBD_OPT_REAL_CLIENT)) {
428 atomic_inc(&mds->mds_real_clients);
429 CDEBUG(D_OTHER,"%s: peer from %s is real client (%d)\n",
430 obd->obd_name, exp->exp_client_uuid.uuid,
431 atomic_read(&mds->mds_real_clients));
432 exp->exp_flags |= OBD_OPT_REAL_CLIENT;
434 if (mds->mds_lmv_name)
435 rc = mds_lmv_connect(obd, mds->mds_lmv_name);
439 static int mds_init_export(struct obd_export *exp)
441 struct mds_export_data *med = &exp->exp_mds_data;
443 INIT_LIST_HEAD(&med->med_open_head);
444 spin_lock_init(&med->med_open_lock);
448 static int mds_destroy_export(struct obd_export *export)
450 struct mds_export_data *med;
451 struct obd_device *obd = export->exp_obd;
452 struct lvfs_run_ctxt saved;
456 med = &export->exp_mds_data;
457 target_destroy_export(export);
459 if (obd_uuid_equals(&export->exp_client_uuid, &obd->obd_uuid))
462 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
464 /* Close any open files (which may also cause orphan unlinking). */
465 spin_lock(&med->med_open_lock);
466 while (!list_empty(&med->med_open_head)) {
467 struct list_head *tmp = med->med_open_head.next;
468 struct mds_file_data *mfd =
469 list_entry(tmp, struct mds_file_data, mfd_list);
470 BDEVNAME_DECLARE_STORAGE(btmp);
472 /* bug 1579: fix force-closing for 2.5 */
473 struct dentry *dentry = mfd->mfd_dentry;
475 list_del(&mfd->mfd_list);
476 spin_unlock(&med->med_open_lock);
478 /* If you change this message, be sure to update
479 * replay_single:test_46 */
480 CERROR("force closing client file handle for %*s (%s:%lu)\n",
481 dentry->d_name.len, dentry->d_name.name,
482 ll_bdevname(dentry->d_inode->i_sb, btmp),
483 dentry->d_inode->i_ino);
484 /* child inode->i_alloc_sem protects orphan_dec_test and
485 * is_orphan race, mds_mfd_close drops it */
486 DOWN_WRITE_I_ALLOC_SEM(dentry->d_inode);
487 rc = mds_mfd_close(NULL, 0, obd, mfd,
488 !(export->exp_flags & OBD_OPT_FAILOVER));
490 CDEBUG(D_INODE, "Error closing file: %d\n", rc);
491 spin_lock(&med->med_open_lock);
493 spin_unlock(&med->med_open_lock);
494 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
497 mds_client_free(export, !(export->exp_flags & OBD_OPT_FAILOVER));
502 static int mds_disconnect(struct obd_export *exp, int flags)
504 unsigned long irqflags;
505 struct obd_device *obd;
510 LASSERT(exp != NULL);
511 obd = class_exp2obd(exp);
513 CDEBUG(D_IOCTL, "invalid client cookie "LPX64"\n",
514 exp->exp_handle.h_cookie);
520 * suppress any inter-mds requests durring disconnecting lmv if this is
521 * detected --force mode. This is needed to avoid endless recovery.
523 if (atomic_read(&mds->mds_real_clients) > 0 &&
524 !(exp->exp_flags & OBD_OPT_REAL_CLIENT))
525 flags |= OBD_OPT_FORCE;
527 if (!(exp->exp_flags & OBD_OPT_REAL_CLIENT)
528 && !atomic_read(&mds->mds_real_clients)) {
529 /* there was no client at all */
530 mds_lmv_disconnect(obd, flags);
533 if ((exp->exp_flags & OBD_OPT_REAL_CLIENT)
534 && atomic_dec_and_test(&mds->mds_real_clients)) {
535 /* time to drop LMV connections */
536 CDEBUG(D_OTHER, "%s: last real client %s disconnected. "
537 "Disconnnect from LMV now\n",
538 obd->obd_name, exp->exp_client_uuid.uuid);
539 mds_lmv_disconnect(obd, flags);
542 spin_lock_irqsave(&exp->exp_lock, irqflags);
543 exp->exp_flags = flags;
544 spin_unlock_irqrestore(&exp->exp_lock, irqflags);
546 /* disconnect early so that clients can't keep using export */
547 rc = class_disconnect(exp, flags);
548 ldlm_cancel_locks_for_export(exp);
550 /* complete all outstanding replies */
551 spin_lock_irqsave(&exp->exp_lock, irqflags);
552 while (!list_empty(&exp->exp_outstanding_replies)) {
553 struct ptlrpc_reply_state *rs =
554 list_entry(exp->exp_outstanding_replies.next,
555 struct ptlrpc_reply_state, rs_exp_list);
556 struct ptlrpc_service *svc = rs->rs_srv_ni->sni_service;
558 spin_lock(&svc->srv_lock);
559 list_del_init(&rs->rs_exp_list);
560 ptlrpc_schedule_difficult_reply(rs);
561 spin_unlock(&svc->srv_lock);
563 spin_unlock_irqrestore(&exp->exp_lock, irqflags);
567 static int mds_getstatus(struct ptlrpc_request *req)
569 struct mds_obd *mds = mds_req2mds(req);
570 struct mds_body *body;
574 size = sizeof(*body);
576 rc = lustre_pack_reply(req, 1, &size, NULL);
577 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_GETSTATUS_PACK)) {
578 CERROR("mds: out of memory for message: size=%d\n", size);
579 req->rq_status = -ENOMEM; /* superfluous? */
583 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
585 body->valid |= OBD_MD_FID;
586 memcpy(&body->id1, &mds->mds_rootid, sizeof(body->id1));
589 * the last_committed and last_xid fields are filled in for all replies
590 * already - no need to do so here also.
595 int mds_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
596 void *data, int flag)
601 if (flag == LDLM_CB_CANCELING) {
602 /* Don't need to do anything here. */
606 /* XXX layering violation! -phil */
607 l_lock(&lock->l_resource->lr_namespace->ns_lock);
610 * get this: if mds_blocking_ast is racing with mds_intent_policy, such
611 * that mds_blocking_ast is called just before l_i_p takes the ns_lock,
612 * then by the time we get the lock, we might not be the correct
613 * blocking function anymore. So check, and return early, if so.
615 if (lock->l_blocking_ast != mds_blocking_ast) {
616 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
620 lock->l_flags |= LDLM_FL_CBPENDING;
621 do_ast = (!lock->l_readers && !lock->l_writers);
622 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
625 struct lustre_handle lockh;
628 LDLM_DEBUG(lock, "already unused, calling ldlm_cli_cancel");
629 ldlm_lock2handle(lock, &lockh);
630 rc = ldlm_cli_cancel(&lockh);
632 CERROR("ldlm_cli_cancel: %d\n", rc);
634 LDLM_DEBUG(lock, "Lock still has references, will be "
640 int mds_get_md(struct obd_device *obd, struct inode *inode, void *md,
648 rc = fsfilt_get_md(obd, inode, md, *size);
653 CERROR("Error %d reading eadata for ino %lu\n",
658 if (S_ISREG(inode->i_mode))
659 rc = mds_convert_lov_ea(obd, inode, md, lmm_size);
660 if (S_ISDIR(inode->i_mode))
661 rc = mds_convert_mea_ea(obd, inode, md, lmm_size);
675 /* Call with lock=1 if you want mds_pack_md to take the i_sem.
676 * Call with lock=0 if the caller has already taken the i_sem. */
677 int mds_pack_md(struct obd_device *obd, struct lustre_msg *msg, int offset,
678 struct mds_body *body, struct inode *inode, int lock)
680 struct mds_obd *mds = &obd->u.mds;
686 lmm = lustre_msg_buf(msg, offset, 0);
688 /* Some problem with getting eadata when I sized the reply
690 CDEBUG(D_INFO, "no space reserved for inode %lu MD\n",
694 lmm_size = msg->buflens[offset];
696 /* I don't really like this, but it is a sanity check on the client
697 * MD request. However, if the client doesn't know how much space
698 * to reserve for the MD, it shouldn't be bad to have too much space.
700 if (lmm_size > mds->mds_max_mdsize) {
701 CWARN("Reading MD for inode %lu of %d bytes > max %d\n",
702 inode->i_ino, lmm_size, mds->mds_max_mdsize);
706 rc = mds_get_md(obd, inode, lmm, &lmm_size, lock);
708 if (S_ISDIR(inode->i_mode))
709 body->valid |= OBD_MD_FLDIREA;
711 body->valid |= OBD_MD_FLEASIZE;
712 body->eadatasize = lmm_size;
719 void mds_squash_root(struct mds_obd *mds, struct mds_req_sec_desc *rsd,
722 if (!mds->mds_squash_uid ||
723 (rsd->rsd_uid && rsd->rsd_fsuid))
726 if (*peernid == mds->mds_nosquash_nid)
729 CDEBUG(D_OTHER, "squash req from 0x%llx, (%d:%d/%x)=>(%d:%d/%x)\n",
730 *peernid, rsd->rsd_fsuid, rsd->rsd_fsgid, rsd->rsd_cap,
731 mds->mds_squash_uid, mds->mds_squash_gid,
732 (rsd->rsd_cap & ~CAP_FS_MASK));
734 rsd->rsd_uid = mds->mds_squash_uid;
735 rsd->rsd_fsuid = mds->mds_squash_uid;
736 rsd->rsd_fsgid = mds->mds_squash_gid;
738 /* XXX should we remove all capabilities? */
739 rsd->rsd_cap &= ~CAP_FS_MASK;
742 static int mds_getattr_internal(struct obd_device *obd, struct dentry *dentry,
743 struct ptlrpc_request *req, struct mds_body *reqbody,
746 struct inode *inode = dentry->d_inode;
747 struct mds_body *body;
751 if (inode == NULL && !(dentry->d_flags & DCACHE_CROSS_REF))
754 body = lustre_msg_buf(req->rq_repmsg, reply_off, sizeof(*body));
755 LASSERT(body != NULL); /* caller prepped reply */
757 if (dentry->d_flags & DCACHE_CROSS_REF) {
758 mds_pack_dentry2body(obd, body, dentry,
759 (reqbody->valid & OBD_MD_FID));
760 CDEBUG(D_OTHER, "cross reference: "DLID4"\n",
765 mds_pack_inode2body(obd, body, inode, (reqbody->valid & OBD_MD_FID));
767 if ((S_ISREG(inode->i_mode) && (reqbody->valid & OBD_MD_FLEASIZE)) ||
768 (S_ISDIR(inode->i_mode) && (reqbody->valid & OBD_MD_FLDIREA))) {
769 rc = mds_pack_md(obd, req->rq_repmsg, reply_off + 1, body,
772 /* if we have LOV EA data, the OST holds size, atime, mtime. */
773 if (!(body->valid & OBD_MD_FLEASIZE) &&
774 !(body->valid & OBD_MD_FLDIREA))
775 body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
776 OBD_MD_FLATIME | OBD_MD_FLMTIME);
777 } else if (S_ISLNK(inode->i_mode) &&
778 (reqbody->valid & OBD_MD_LINKNAME) != 0) {
779 int len = req->rq_repmsg->buflens[reply_off + 1];
780 char *symname = lustre_msg_buf(req->rq_repmsg, reply_off + 1, 0);
782 LASSERT(symname != NULL); /* caller prepped reply */
784 if (!inode->i_op->readlink) {
787 rc = inode->i_op->readlink(dentry, symname, len);
789 CERROR("readlink failed: %d\n", rc);
790 } else if (rc != len - 1) {
791 CERROR("Unexpected readlink rc %d: expecting %d\n",
795 CDEBUG(D_INODE, "read symlink dest %s\n", symname);
796 body->valid |= OBD_MD_LINKNAME;
797 body->eadatasize = rc + 1;
807 static int mds_getattr_pack_msg_cf(struct ptlrpc_request *req,
808 struct dentry *dentry,
811 int rc = 0, size[1] = {sizeof(struct mds_body)};
814 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK)) {
815 CERROR("failed MDS_GETATTR_PACK test\n");
816 req->rq_status = -ENOMEM;
817 GOTO(out, rc = -ENOMEM);
820 rc = lustre_pack_reply(req, 1, size, NULL);
822 CERROR("lustre_pack_reply failed: rc %d\n", rc);
823 GOTO(out, req->rq_status = rc);
830 static int mds_getattr_pack_msg(struct ptlrpc_request *req, struct inode *inode,
833 struct mds_obd *mds = mds_req2mds(req);
834 struct mds_body *body;
835 int rc = 0, size[2] = {sizeof(*body)}, bufcount = 1;
838 body = lustre_msg_buf(req->rq_reqmsg, offset, sizeof (*body));
839 LASSERT(body != NULL); /* checked by caller */
840 LASSERT_REQSWABBED(req, offset); /* swabbed by caller */
842 if ((S_ISREG(inode->i_mode) && (body->valid & OBD_MD_FLEASIZE)) ||
843 (S_ISDIR(inode->i_mode) && (body->valid & OBD_MD_FLDIREA))) {
846 rc = fsfilt_get_md(req->rq_export->exp_obd, inode, NULL, 0);
848 CDEBUG(D_INODE, "got %d bytes MD data for inode %lu\n",
852 CERROR("error getting inode %lu MD: rc = %d\n",
855 } else if (rc > mds->mds_max_mdsize) {
857 CERROR("MD size %d larger than maximum possible %u\n",
858 rc, mds->mds_max_mdsize);
863 } else if (S_ISLNK(inode->i_mode) && (body->valid & OBD_MD_LINKNAME)) {
864 if (inode->i_size + 1 != body->eadatasize)
865 CERROR("symlink size: %Lu, reply space: %d\n",
866 inode->i_size + 1, body->eadatasize);
867 size[bufcount] = min_t(int, inode->i_size+1, body->eadatasize);
869 CDEBUG(D_INODE, "symlink size: %Lu, reply space: %d\n",
870 inode->i_size + 1, body->eadatasize);
873 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK)) {
874 CERROR("failed MDS_GETATTR_PACK test\n");
875 req->rq_status = -ENOMEM;
876 GOTO(out, rc = -ENOMEM);
879 rc = lustre_pack_reply(req, bufcount, size, NULL);
881 CERROR("out of memory\n");
882 GOTO(out, req->rq_status = rc);
890 int mds_check_mds_num(struct obd_device *obd, struct inode *inode,
891 char *name, int namelen)
893 struct mea *mea = NULL;
894 int mea_size, rc = 0;
897 rc = mds_get_lmv_attr(obd, inode, &mea, &mea_size);
902 * dir is already splitted, check if requested filename should
903 * live at this MDS or at another one.
905 int i = mea_name2idx(mea, name, namelen - 1);
906 if (mea->mea_master != id_group(&mea->mea_ids[i])) {
908 "inapropriate MDS(%d) for %s. should be "
909 "%lu(%d)\n", mea->mea_master, name,
910 (unsigned long)id_group(&mea->mea_ids[i]), i);
916 OBD_FREE(mea, mea_size);
920 static int mds_getattr_lock(struct ptlrpc_request *req, int offset,
921 struct lustre_handle *child_lockh, int child_part)
923 struct obd_device *obd = req->rq_export->exp_obd;
924 struct mds_obd *mds = &obd->u.mds;
925 struct ldlm_reply *rep = NULL;
926 struct lvfs_run_ctxt saved;
927 struct mds_req_sec_desc *rsd;
928 struct mds_body *body;
929 struct dentry *dparent = NULL, *dchild = NULL;
930 struct lvfs_ucred uc;
931 struct lustre_handle parent_lockh[2] = {{0}, {0}};
932 unsigned int namesize;
933 int rc = 0, cleanup_phase = 0, resent_req = 0, update_mode, reply_offset;
937 LASSERT(!strcmp(obd->obd_type->typ_name, LUSTRE_MDS_NAME));
938 MDS_UPDATE_COUNTER((&obd->u.mds), MDS_GETATTR_NAME_COUNT);
940 rsd = lustre_swab_mds_secdesc(req, MDS_REQ_SECDESC_OFF);
942 CERROR("Can't unpack security desc\n");
945 mds_squash_root(mds, rsd, &req->rq_peer.peer_id.nid);
947 /* swab now, before anyone looks inside the request. */
948 body = lustre_swab_reqbuf(req, offset, sizeof(*body),
949 lustre_swab_mds_body);
951 CERROR("Can't swab mds_body\n");
952 GOTO(cleanup, rc = -EFAULT);
955 LASSERT_REQSWAB(req, offset + 1);
956 name = lustre_msg_string(req->rq_reqmsg, offset + 1, 0);
958 CERROR("Can't unpack name\n");
959 GOTO(cleanup, rc = -EFAULT);
961 namesize = req->rq_reqmsg->buflens[offset + 1];
963 /* namesize less than 2 means we have empty name, probably came from
964 revalidate by cfid, so no point in having name to be set */
968 LASSERT (offset == 1 || offset == 3);
970 rep = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*rep));
976 rc = mds_init_ucred(&uc, rsd);
978 CERROR("can't init ucred\n");
982 push_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
983 cleanup_phase = 1; /* kernel context */
984 intent_set_disposition(rep, DISP_LOOKUP_EXECD);
986 LASSERT(namesize > 0);
987 if (child_lockh->cookie != 0) {
988 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT);
993 if (body->valid == OBD_MD_FLID) {
994 struct mds_body *mds_reply;
995 int size = sizeof(*mds_reply);
999 dparent = mds_id2dentry(obd, &body->id1, NULL);
1000 if (IS_ERR(dparent)) {
1001 rc = PTR_ERR(dparent);
1006 * the user requested ONLY the inode number, so do a raw lookup.
1008 rc = lustre_pack_reply(req, 1, &size, NULL);
1010 CERROR("out of memory\n");
1014 dir = dparent->d_inode;
1015 LASSERT(dir->i_op->lookup_raw != NULL);
1016 rc = dir->i_op->lookup_raw(dir, name, namesize - 1, &inum);
1018 mds_reply = lustre_msg_buf(req->rq_repmsg, 0,
1019 sizeof(*mds_reply));
1021 id_ino(&mds_reply->id1) = inum;
1022 mds_reply->valid = OBD_MD_FLID;
1027 if (resent_req == 0) {
1028 LASSERT(id_fid(&body->id1) != 0);
1030 rc = mds_get_parent_child_locked(obd, mds, &body->id1,
1031 parent_lockh, &dparent,
1033 MDS_INODELOCK_UPDATE,
1036 child_lockh, &dchild,
1037 LCK_PR, child_part);
1042 * let's make sure this name should leave on this mds
1045 rc = mds_check_mds_num(obd, dparent->d_inode, name, namesize);
1049 /* we have no dentry here, drop LOOKUP bit */
1050 /* FIXME: we need MDS_INODELOCK_LOOKUP or not. */
1051 child_part &= ~MDS_INODELOCK_LOOKUP;
1052 CDEBUG(D_OTHER, "%s: retrieve attrs for "DLID4"\n",
1053 obd->obd_name, OLID4(&body->id1));
1055 dchild = mds_id2locked_dentry(obd, &body->id1, NULL,
1056 LCK_PR, parent_lockh,
1059 MDS_INODELOCK_UPDATE);
1060 if (IS_ERR(dchild)) {
1061 CERROR("can't find inode with id "DLID4", err = %d\n",
1062 OLID4(&body->id1), (int)PTR_ERR(dchild));
1063 GOTO(cleanup, rc = PTR_ERR(dchild));
1065 memcpy(child_lockh, parent_lockh, sizeof(parent_lockh[0]));
1067 if (parent_lockh[1].cookie)
1068 ldlm_lock_decref(parent_lockh + 1, update_mode);
1072 struct ldlm_lock *granted_lock;
1074 DEBUG_REQ(D_DLMTRACE, req, "resent, not enqueuing new locks");
1075 granted_lock = ldlm_handle2lock(child_lockh);
1077 LASSERTF(granted_lock != NULL, LPU64"/%lu lockh "LPX64"\n",
1078 id_fid(&body->id1), (unsigned long)id_group(&body->id1),
1079 child_lockh->cookie);
1081 dparent = mds_id2dentry(obd, &body->id1, NULL);
1084 dchild = ll_lookup_one_len(name, dparent, namesize - 1);
1086 LDLM_LOCK_PUT(granted_lock);
1089 cleanup_phase = 2; /* dchild, dparent, locks */
1091 if (!DENTRY_VALID(dchild)) {
1092 intent_set_disposition(rep, DISP_LOOKUP_NEG);
1094 * in the intent case, the policy clears this error: the
1095 * disposition is enough.
1100 intent_set_disposition(rep, DISP_LOOKUP_POS);
1103 if (req->rq_repmsg == NULL) {
1104 if (dchild->d_flags & DCACHE_CROSS_REF)
1105 rc = mds_getattr_pack_msg_cf(req, dchild, offset);
1107 rc = mds_getattr_pack_msg(req, dchild->d_inode, offset);
1109 CERROR ("mds_getattr_pack_msg: %d\n", rc);
1114 rc = mds_getattr_internal(obd, dchild, req, body, reply_offset);
1115 GOTO(cleanup, rc); /* returns the lock to the client */
1118 switch (cleanup_phase) {
1120 if (resent_req == 0) {
1121 if (rc && DENTRY_VALID(dchild))
1122 ldlm_lock_decref(child_lockh, LCK_PR);
1124 ldlm_lock_decref(parent_lockh, LCK_PR);
1126 if (parent_lockh[1].cookie != 0)
1127 ldlm_lock_decref(parent_lockh + 1, update_mode);
1134 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1135 mds_exit_ucred(&uc);
1140 static int mds_getattr(struct ptlrpc_request *req, int offset)
1142 struct obd_device *obd = req->rq_export->exp_obd;
1143 struct mds_obd *mds = &obd->u.mds;
1144 struct lvfs_run_ctxt saved;
1146 struct mds_req_sec_desc *rsd;
1147 struct mds_body *body;
1148 struct lvfs_ucred uc;
1152 rsd = lustre_swab_mds_secdesc(req, MDS_REQ_SECDESC_OFF);
1154 CERROR("Can't unpack security desc\n");
1158 body = lustre_swab_reqbuf(req, offset, sizeof(*body),
1159 lustre_swab_mds_body);
1161 CERROR ("Can't unpack body\n");
1165 MDS_UPDATE_COUNTER(mds, MDS_GETATTR_COUNT);
1167 rc = mds_init_ucred(&uc, rsd);
1169 CERROR("can't init ucred\n");
1173 push_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1174 de = mds_id2dentry(obd, &body->id1, NULL);
1176 rc = req->rq_status = PTR_ERR(de);
1180 rc = mds_getattr_pack_msg(req, de->d_inode, offset);
1182 CERROR("mds_getattr_pack_msg: %d\n", rc);
1186 req->rq_status = mds_getattr_internal(obd, de, req, body, 0);
1191 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1192 mds_exit_ucred(&uc);
1196 static int mds_obd_statfs(struct obd_device *obd, struct obd_statfs *osfs,
1197 unsigned long max_age)
1202 spin_lock(&obd->obd_osfs_lock);
1203 rc = fsfilt_statfs(obd, obd->u.mds.mds_sb, max_age);
1205 memcpy(osfs, &obd->obd_osfs, sizeof(*osfs));
1206 spin_unlock(&obd->obd_osfs_lock);
1211 static int mds_statfs(struct ptlrpc_request *req)
1213 struct obd_device *obd = req->rq_export->exp_obd;
1214 int rc, size = sizeof(struct obd_statfs);
1217 rc = lustre_pack_reply(req, 1, &size, NULL);
1218 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_STATFS_PACK)) {
1219 CERROR("mds: statfs lustre_pack_reply failed: rc = %d\n", rc);
1223 MDS_UPDATE_COUNTER((&obd->u.mds), MDS_STATFS_COUNT);
1225 /* We call this so that we can cache a bit - 1 jiffie worth */
1226 rc = mds_obd_statfs(obd, lustre_msg_buf(req->rq_repmsg, 0, size),
1229 CERROR("mds_obd_statfs failed: rc %d\n", rc);
1234 req->rq_status = rc;
1238 static int mds_sync(struct ptlrpc_request *req, int offset)
1240 struct obd_device *obd = req->rq_export->exp_obd;
1241 struct mds_obd *mds = &obd->u.mds;
1242 struct mds_body *body;
1243 int rc, size = sizeof(*body);
1246 body = lustre_msg_buf(req->rq_reqmsg, offset, sizeof(*body));
1248 GOTO(out, rc = -EPROTO);
1250 rc = lustre_pack_reply(req, 1, &size, NULL);
1251 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK)) {
1252 CERROR("fsync lustre_pack_reply failed: rc = %d\n", rc);
1256 if (id_ino(&body->id1) == 0) {
1257 /* an id of zero is taken to mean "sync whole filesystem" */
1258 rc = fsfilt_sync(obd, mds->mds_sb);
1262 /* just any file to grab fsync method - "file" arg unused */
1263 struct file *file = mds->mds_rcvd_filp;
1266 de = mds_id2dentry(obd, &body->id1, NULL);
1268 GOTO(out, rc = PTR_ERR(de));
1270 rc = file->f_op->fsync(NULL, de, 1);
1274 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
1275 mds_pack_inode2body(obd, body, de->d_inode, 0);
1279 req->rq_status = rc;
1283 /* mds_readpage does not take a DLM lock on the inode, because the client must
1284 * already have a PR lock.
1286 * If we were to take another one here, a deadlock will result, if another
1287 * thread is already waiting for a PW lock. */
1288 static int mds_readpage(struct ptlrpc_request *req, int offset)
1290 struct obd_device *obd = req->rq_export->exp_obd;
1291 struct mds_obd *mds = &obd->u.mds;
1292 struct vfsmount *mnt;
1295 struct mds_req_sec_desc *rsd;
1296 struct mds_body *body, *repbody;
1297 struct lvfs_run_ctxt saved;
1298 int rc, size = sizeof(*repbody);
1299 struct lvfs_ucred uc;
1302 rc = lustre_pack_reply(req, 1, &size, NULL);
1303 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK)) {
1304 CERROR("mds: out of memory\n");
1305 GOTO(out, rc = -ENOMEM);
1308 rsd = lustre_swab_mds_secdesc(req, MDS_REQ_SECDESC_OFF);
1310 CERROR("Can't unpack security desc\n");
1311 GOTO (out, rc = -EFAULT);
1313 mds_squash_root(mds, rsd, &req->rq_peer.peer_id.nid);
1315 body = lustre_swab_reqbuf(req, offset, sizeof(*body),
1316 lustre_swab_mds_body);
1318 CERROR("Can't unpack body\n");
1319 GOTO (out, rc = -EFAULT);
1322 rc = mds_init_ucred(&uc, rsd);
1324 CERROR("can't init ucred\n");
1328 push_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1329 de = mds_id2dentry(obd, &body->id1, &mnt);
1331 GOTO(out_pop, rc = PTR_ERR(de));
1333 CDEBUG(D_INODE, "ino %lu\n", de->d_inode->i_ino);
1335 file = dentry_open(de, mnt, O_RDONLY | O_LARGEFILE);
1336 /* note: in case of an error, dentry_open puts dentry */
1338 GOTO(out_pop, rc = PTR_ERR(file));
1340 /* body->size is actually the offset -eeb */
1341 if ((body->size & (de->d_inode->i_blksize - 1)) != 0) {
1342 CERROR("offset "LPU64" not on a block boundary of %lu\n",
1343 body->size, de->d_inode->i_blksize);
1344 GOTO(out_file, rc = -EFAULT);
1347 /* body->nlink is actually the #bytes to read -eeb */
1348 if (body->nlink & (de->d_inode->i_blksize - 1)) {
1349 CERROR("size %u is not multiple of blocksize %lu\n",
1350 body->nlink, de->d_inode->i_blksize);
1351 GOTO(out_file, rc = -EFAULT);
1354 repbody = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*repbody));
1355 repbody->size = file->f_dentry->d_inode->i_size;
1356 repbody->valid = OBD_MD_FLSIZE;
1358 /* to make this asynchronous make sure that the handling function
1359 doesn't send a reply when this function completes. Instead a
1360 callback function would send the reply */
1361 /* body->size is actually the offset -eeb */
1362 rc = mds_sendpage(req, file, body->size, body->nlink);
1365 filp_close(file, 0);
1367 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1368 mds_exit_ucred(&uc);
1370 req->rq_status = rc;
1374 /* update master MDS ID, which is stored in local inode EA. */
1375 int mds_update_mid(struct obd_device *obd, struct lustre_id *id,
1376 void *data, int data_len)
1378 struct mds_obd *mds = &obd->u.mds;
1379 struct dentry *dentry;
1387 dentry = mds_id2dentry(obd, id, NULL);
1389 GOTO(out, rc = PTR_ERR(dentry));
1391 if (!dentry->d_inode) {
1392 CERROR("Can't find object "DLID4".\n",
1394 GOTO(out_dentry, rc = -EINVAL);
1397 handle = fsfilt_start(obd, dentry->d_inode,
1398 FSFILT_OP_SETATTR, NULL);
1400 GOTO(out_dentry, rc = PTR_ERR(handle));
1402 rc = mds_update_inode_mid(obd, dentry->d_inode, handle,
1403 (struct lustre_id *)data);
1405 CERROR("Can't update inode "DLID4" master id, "
1406 "error = %d.\n", OLID4(id), rc);
1407 GOTO(out_commit, rc);
1411 fsfilt_commit(obd, mds->mds_sb, dentry->d_inode,
1418 EXPORT_SYMBOL(mds_update_mid);
1420 /* read master MDS ID, which is stored in local inode EA. */
1421 int mds_read_mid(struct obd_device *obd, struct lustre_id *id,
1422 void *data, int data_len)
1424 struct dentry *dentry;
1431 dentry = mds_id2dentry(obd, id, NULL);
1433 GOTO(out, rc = PTR_ERR(dentry));
1435 if (!dentry->d_inode) {
1436 CERROR("Can't find object "DLID4".\n",
1438 GOTO(out_dentry, rc = -EINVAL);
1441 down(&dentry->d_inode->i_sem);
1442 rc = mds_read_inode_mid(obd, dentry->d_inode,
1443 (struct lustre_id *)data);
1444 up(&dentry->d_inode->i_sem);
1446 CERROR("Can't read inode "DLID4" master id, "
1447 "error = %d.\n", OLID4(id), rc);
1448 GOTO(out_dentry, rc);
1456 EXPORT_SYMBOL(mds_read_mid);
1458 int mds_reint(struct ptlrpc_request *req, int offset,
1459 struct lustre_handle *lockh)
1461 struct mds_obd *mds = &req->rq_export->exp_obd->u.mds;
1462 struct mds_update_record *rec;
1463 struct mds_req_sec_desc *rsd;
1467 OBD_ALLOC(rec, sizeof(*rec));
1471 rsd = lustre_swab_mds_secdesc(req, MDS_REQ_SECDESC_OFF);
1473 CERROR("Can't unpack security desc\n");
1474 GOTO(out, rc = -EFAULT);
1476 mds_squash_root(mds, rsd, &req->rq_peer.peer_id.nid);
1478 rc = mds_update_unpack(req, offset, rec);
1479 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNPACK)) {
1480 CERROR("invalid record\n");
1481 GOTO(out, req->rq_status = -EINVAL);
1484 rc = mds_init_ucred(&rec->ur_uc, rsd);
1486 CERROR("can't init ucred\n");
1490 /* rc will be used to interrupt a for loop over multiple records */
1491 rc = mds_reint_rec(rec, offset, req, lockh);
1492 mds_exit_ucred(&rec->ur_uc);
1494 OBD_FREE(rec, sizeof(*rec));
1498 static int mds_filter_recovery_request(struct ptlrpc_request *req,
1499 struct obd_device *obd, int *process)
1501 switch (req->rq_reqmsg->opc) {
1502 case MDS_CONNECT: /* This will never get here, but for completeness. */
1503 case OST_CONNECT: /* This will never get here, but for completeness. */
1504 case MDS_DISCONNECT:
1505 case OST_DISCONNECT:
1510 case MDS_SYNC: /* used in unmounting */
1515 *process = target_queue_recovery_request(req, obd);
1519 DEBUG_REQ(D_ERROR, req, "not permitted during recovery");
1521 /* XXX what should we set rq_status to here? */
1522 req->rq_status = -EAGAIN;
1523 RETURN(ptlrpc_error(req));
1527 static char *reint_names[] = {
1528 [REINT_SETATTR] "setattr",
1529 [REINT_CREATE] "create",
1530 [REINT_LINK] "link",
1531 [REINT_UNLINK] "unlink",
1532 [REINT_RENAME] "rename",
1533 [REINT_OPEN] "open",
1536 #define FILTER_VALID_FLAGS (OBD_MD_FLTYPE | OBD_MD_FLMODE | OBD_MD_FLGENER | \
1537 OBD_MD_FLSIZE | OBD_MD_FLBLOCKS | OBD_MD_FLBLKSZ| \
1538 OBD_MD_FLATIME | OBD_MD_FLMTIME | OBD_MD_FLCTIME| \
1541 static void reconstruct_create(struct ptlrpc_request *req)
1543 struct mds_export_data *med = &req->rq_export->exp_mds_data;
1544 struct mds_client_data *mcd = med->med_mcd;
1545 struct dentry *dentry;
1546 struct ost_body *body;
1547 struct lustre_id id;
1551 /* copy rc, transno and disp; steal locks */
1552 mds_req_from_mcd(req, mcd);
1553 if (req->rq_status) {
1561 id_ino(&id) = mcd->mcd_last_data;
1562 LASSERT(id_ino(&id) != 0);
1564 dentry = mds_id2dentry(req2obd(req), &id, NULL);
1565 if (IS_ERR(dentry)) {
1566 CERROR("can't find inode "LPU64"\n", id_ino(&id));
1567 req->rq_status = PTR_ERR(dentry);
1572 CWARN("reconstruct reply for x"LPU64" (remote ino) "LPU64" -> %lu/%u\n",
1573 req->rq_xid, id_ino(&id), dentry->d_inode->i_ino,
1574 dentry->d_inode->i_generation);
1576 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
1577 obdo_from_inode(&body->oa, dentry->d_inode, FILTER_VALID_FLAGS);
1578 body->oa.o_id = dentry->d_inode->i_ino;
1579 body->oa.o_generation = dentry->d_inode->i_generation;
1580 body->oa.o_valid |= OBD_MD_FLID | OBD_MD_FLGENER;
1582 down(&dentry->d_inode->i_sem);
1583 rc = mds_read_inode_sid(req2obd(req), dentry->d_inode, &id);
1584 up(&dentry->d_inode->i_sem);
1586 CERROR("Can't read inode self id, inode %lu, "
1587 "rc %d\n", dentry->d_inode->i_ino, rc);
1591 body->oa.o_fid = id_fid(&id);
1592 body->oa.o_mds = id_group(&id);
1598 static int mdt_obj_create(struct ptlrpc_request *req)
1600 struct obd_device *obd = req->rq_export->exp_obd;
1601 struct mds_obd *mds = &obd->u.mds;
1602 struct ost_body *body, *repbody;
1603 char idname[LL_ID_NAMELEN];
1604 int size = sizeof(*repbody);
1605 struct inode *parent_inode;
1606 struct lvfs_run_ctxt saved;
1607 int rc, cleanup_phase = 0;
1608 struct dentry *new = NULL;
1609 struct dentry_params dp;
1610 int mealen, flags = 0;
1611 struct lvfs_ucred uc;
1612 struct lustre_id id;
1614 void *handle = NULL;
1615 unsigned long cr_inum = 0;
1618 DEBUG_REQ(D_HA, req, "create remote object");
1620 parent_inode = mds->mds_unnamed_dir->d_inode;
1622 body = lustre_swab_reqbuf(req, 0, sizeof(*body),
1623 lustre_swab_ost_body);
1627 rc = lustre_pack_reply(req, 1, &size, NULL);
1631 MDS_CHECK_RESENT(req, reconstruct_create(req));
1634 * this only serve to inter-mds request, don't need check group database
1637 uc.luc_ghash = NULL;
1638 uc.luc_ginfo = NULL;
1639 uc.luc_uid = body->oa.o_uid;
1640 uc.luc_fsuid = body->oa.o_uid;
1641 uc.luc_fsgid = body->oa.o_gid;
1642 push_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1644 repbody = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*repbody));
1646 /* in REPLAY case inum should be given (client or other MDS fills it) */
1647 if (body->oa.o_id && ((body->oa.o_flags & OBD_FL_RECREATE_OBJS) ||
1648 (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY))) {
1649 /* this is re-create request from MDS holding directory name.
1650 * we have to lookup given ino/gen first. if it exists
1651 * (good case) then there is nothing to do. if it does not then
1652 * we have to recreate it */
1653 id_ino(&id) = body->oa.o_id;
1654 id_gen(&id) = body->oa.o_generation;
1656 new = mds_id2dentry(obd, &id, NULL);
1657 if (!IS_ERR(new) && new->d_inode) {
1658 struct lustre_id sid;
1660 CWARN("mkdir() repairing is on its way: %lu/%lu\n",
1661 (unsigned long)id_ino(&id), (unsigned long)id_gen(&id));
1663 obdo_from_inode(&repbody->oa, new->d_inode,
1664 FILTER_VALID_FLAGS);
1666 repbody->oa.o_id = new->d_inode->i_ino;
1667 repbody->oa.o_generation = new->d_inode->i_generation;
1668 repbody->oa.o_valid |= OBD_MD_FLID | OBD_MD_FLGENER;
1671 down(&new->d_inode->i_sem);
1672 rc = mds_read_inode_sid(obd, new->d_inode, &sid);
1673 up(&new->d_inode->i_sem);
1675 CERROR("Can't read inode self id "
1676 "inode %lu, rc %d.\n",
1677 new->d_inode->i_ino, rc);
1681 repbody->oa.o_fid = id_fid(&sid);
1682 repbody->oa.o_mds = id_group(&sid);
1683 cr_inum = new->d_inode->i_ino;
1684 GOTO(cleanup, rc = 0);
1688 down(&parent_inode->i_sem);
1689 handle = fsfilt_start(obd, parent_inode, FSFILT_OP_MKDIR, NULL);
1690 if (IS_ERR(handle)) {
1691 up(&parent_inode->i_sem);
1692 CERROR("fsfilt_start() failed, rc = %d\n",
1693 (int)PTR_ERR(handle));
1694 GOTO(cleanup, rc = PTR_ERR(handle));
1696 cleanup_phase = 1; /* transaction */
1699 rc = sprintf(idname, "%u.%u", ll_insecure_random_int(), current->pid);
1700 new = lookup_one_len(idname, mds->mds_unnamed_dir, rc);
1702 CERROR("%s: can't lookup new inode (%s) for mkdir: %d\n",
1703 obd->obd_name, idname, (int) PTR_ERR(new));
1704 fsfilt_commit(obd, mds->mds_sb, new->d_inode, handle, 0);
1705 up(&parent_inode->i_sem);
1706 RETURN(PTR_ERR(new));
1707 } else if (new->d_inode) {
1708 CERROR("%s: name exists. repeat\n", obd->obd_name);
1712 new->d_fsdata = (void *)&dp;
1716 if ((lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) ||
1717 (body->oa.o_flags & OBD_FL_RECREATE_OBJS)) {
1718 LASSERT(body->oa.o_id != 0);
1719 DEBUG_REQ(D_HA, req, "replay create obj %lu/%lu",
1720 (unsigned long)body->oa.o_id,
1721 (unsigned long)body->oa.o_generation);
1722 dp.p_inum = body->oa.o_id;
1725 rc = vfs_mkdir(parent_inode, new, body->oa.o_mode);
1727 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
1728 new->d_inode->i_generation = body->oa.o_generation;
1729 mark_inode_dirty(new->d_inode);
1732 * avoiding asserts in cache flush case, as
1733 * @body->oa.o_id should be zero.
1735 if (body->oa.o_id) {
1736 LASSERTF(body->oa.o_id == new->d_inode->i_ino,
1737 "BUG 3550: failed to recreate obj "
1738 LPU64" -> %lu\n", body->oa.o_id,
1739 new->d_inode->i_ino);
1741 LASSERTF(body->oa.o_generation ==
1742 new->d_inode->i_generation,
1743 "BUG 3550: failed to recreate obj/gen "
1744 LPU64"/%u -> %lu/%u\n", body->oa.o_id,
1745 body->oa.o_generation,
1746 new->d_inode->i_ino,
1747 new->d_inode->i_generation);
1751 obdo_from_inode(&repbody->oa, new->d_inode, FILTER_VALID_FLAGS);
1752 repbody->oa.o_id = new->d_inode->i_ino;
1753 repbody->oa.o_generation = new->d_inode->i_generation;
1754 repbody->oa.o_valid |= OBD_MD_FLID | OBD_MD_FLGENER | OBD_MD_FID;
1756 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
1757 /* building lustre_id on passed @oa. */
1758 id_group(&id) = mds->mds_num;
1760 LASSERT(body->oa.o_fid != 0);
1761 id_fid(&id) = body->oa.o_fid;
1763 id_ino(&id) = repbody->oa.o_id;
1764 id_gen(&id) = repbody->oa.o_generation;
1766 down(&new->d_inode->i_sem);
1767 rc = mds_update_inode_sid(obd, new->d_inode, handle, &id);
1768 up(&new->d_inode->i_sem);
1771 * make sure, that fid is up-to-date.
1773 mds_set_last_fid(obd, id_fid(&id));
1775 down(&new->d_inode->i_sem);
1776 rc = mds_alloc_inode_sid(obd, new->d_inode, handle, &id);
1777 up(&new->d_inode->i_sem);
1780 CERROR("Can't update lustre ID for inode %lu, "
1781 "error = %d\n", new->d_inode->i_ino, rc);
1785 /* initializing o_fid after it is allocated. */
1786 repbody->oa.o_fid = id_fid(&id);
1787 repbody->oa.o_mds = id_group(&id);
1789 rc = fsfilt_del_dir_entry(obd, new);
1790 up(&parent_inode->i_sem);
1792 CERROR("can't remove name for object: %d\n", rc);
1796 cleanup_phase = 2; /* created directory object */
1798 CDEBUG(D_OTHER, "created dirobj: %lu/%lu mode %o\n",
1799 (unsigned long)new->d_inode->i_ino,
1800 (unsigned long)new->d_inode->i_generation,
1801 (unsigned)new->d_inode->i_mode);
1802 cr_inum = new->d_inode->i_ino;
1804 up(&parent_inode->i_sem);
1805 CERROR("%s: can't create dirobj: %d\n", obd->obd_name, rc);
1809 if (body->oa.o_valid & OBD_MD_FLID) {
1810 /* this is new object for splitted dir. We have to prevent
1811 * recursive splitting on it -bzzz */
1812 mealen = obd_size_diskmd(mds->mds_lmv_exp, NULL);
1814 OBD_ALLOC(mea, mealen);
1816 GOTO(cleanup, rc = -ENOMEM);
1818 mea->mea_magic = MEA_MAGIC_ALL_CHARS;
1819 mea->mea_master = 0;
1822 down(&new->d_inode->i_sem);
1823 rc = fsfilt_set_md(obd, new->d_inode, handle, mea, mealen);
1824 up(&new->d_inode->i_sem);
1826 CERROR("fsfilt_set_md() failed, rc = %d\n", rc);
1828 OBD_FREE(mea, mealen);
1829 CDEBUG(D_OTHER, "%s: mark non-splittable %lu/%u - %d\n",
1830 obd->obd_name, new->d_inode->i_ino,
1831 new->d_inode->i_generation, flags);
1832 } else if (body->oa.o_easize) {
1833 /* we pass LCK_EX to split routine to signal that we have
1834 * exclusive access to the directory. simple because nobody
1835 * knows it already exists -bzzz */
1836 rc = mds_try_to_split_dir(obd, new, NULL,
1837 body->oa.o_easize, LCK_EX);
1839 CERROR("Can't split directory %lu, error = %d.\n",
1840 new->d_inode->i_ino, rc);
1847 switch (cleanup_phase) {
1848 case 2: /* object has been created, but we'll may want to replay it later */
1850 ptlrpc_require_repack(req);
1851 case 1: /* transaction */
1852 rc = mds_finish_transno(mds, parent_inode, handle,
1857 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1858 mds_put_group_entry(mds, uc.luc_ghash);
1862 static int mdt_get_info(struct ptlrpc_request *req)
1864 struct obd_export *exp = req->rq_export;
1869 key = lustre_msg_buf(req->rq_reqmsg, 0, 1);
1871 DEBUG_REQ(D_HA, req, "no get_info key");
1874 keylen = req->rq_reqmsg->buflens[0];
1876 if ((keylen < strlen("mdsize") || strcmp(key, "mdsize") != 0) &&
1877 (keylen < strlen("mdsnum") || strcmp(key, "mdsnum") != 0) &&
1878 (keylen < strlen("rootid") || strcmp(key, "rootid") != 0))
1881 if (keylen >= strlen("rootid") && !strcmp(key, "rootid")) {
1882 struct lustre_id *reply;
1883 int size = sizeof(*reply);
1885 rc = lustre_pack_reply(req, 1, &size, NULL);
1889 reply = lustre_msg_buf(req->rq_repmsg, 0, size);
1890 rc = obd_get_info(exp, keylen, key, &size, reply);
1893 int size = sizeof(*reply);
1895 rc = lustre_pack_reply(req, 1, &size, NULL);
1899 reply = lustre_msg_buf(req->rq_repmsg, 0, size);
1900 rc = obd_get_info(exp, keylen, key, &size, reply);
1903 req->rq_repmsg->status = 0;
1907 static int mds_set_info(struct obd_export *exp, __u32 keylen,
1908 void *key, __u32 vallen, void *val)
1910 struct obd_device *obd;
1911 struct mds_obd *mds;
1915 obd = class_exp2obd(exp);
1917 CDEBUG(D_IOCTL, "invalid client cookie "LPX64"\n",
1918 exp->exp_handle.h_cookie);
1923 if (keylen >= strlen("mds_type") &&
1924 memcmp(key, "mds_type", keylen) == 0) {
1928 CDEBUG(D_IOCTL, "set mds type to %x\n", *(int*)val);
1930 mds->mds_obd_type = *(int*)val;
1931 group = FILTER_GROUP_FIRST_MDS + mds->mds_obd_type;
1932 valsize = sizeof(group);
1934 /* mds number has been changed, so the corresponding obdfilter
1935 * exp need to be changed too. */
1936 rc = obd_set_info(mds->mds_lov_exp, strlen("mds_conn"),
1937 "mds_conn", valsize, &group);
1940 CDEBUG(D_IOCTL, "invalid key\n");
1944 static int mdt_set_info(struct ptlrpc_request *req)
1947 struct obd_export *exp = req->rq_export;
1948 int keylen, rc = 0, vallen;
1951 key = lustre_msg_buf(req->rq_reqmsg, 0, 1);
1953 DEBUG_REQ(D_HA, req, "no set_info key");
1956 keylen = req->rq_reqmsg->buflens[0];
1958 if (keylen == strlen("mds_type") &&
1959 memcmp(key, "mds_type", keylen) == 0) {
1960 rc = lustre_pack_reply(req, 0, NULL, NULL);
1964 val = lustre_msg_buf(req->rq_reqmsg, 1, 0);
1965 vallen = req->rq_reqmsg->buflens[1];
1967 rc = obd_set_info(exp, keylen, key, vallen, val);
1968 req->rq_repmsg->status = 0;
1971 CDEBUG(D_IOCTL, "invalid key\n");
1975 static int mds_msg_check_version(struct lustre_msg *msg)
1981 case MDS_DISCONNECT:
1983 rc = lustre_msg_check_version(msg, LUSTRE_OBD_VERSION);
1985 CERROR("bad opc %u version %08x, expecting %08x\n",
1986 msg->opc, msg->version, LUSTRE_OBD_VERSION);
1991 case MDS_GETATTR_LOCK:
1995 case MDS_DONE_WRITING:
1998 rc = lustre_msg_check_version(msg, LUSTRE_MDS_VERSION);
2000 CERROR("bad opc %u version %08x, expecting %08x\n",
2001 msg->opc, msg->version, LUSTRE_MDS_VERSION);
2005 case LDLM_BL_CALLBACK:
2006 case LDLM_CP_CALLBACK:
2007 rc = lustre_msg_check_version(msg, LUSTRE_DLM_VERSION);
2009 CERROR("bad opc %u version %08x, expecting %08x\n",
2010 msg->opc, msg->version, LUSTRE_DLM_VERSION);
2012 case OBD_LOG_CANCEL:
2013 case LLOG_ORIGIN_HANDLE_OPEN:
2014 case LLOG_ORIGIN_HANDLE_NEXT_BLOCK:
2015 case LLOG_ORIGIN_HANDLE_PREV_BLOCK:
2016 case LLOG_ORIGIN_HANDLE_READ_HEADER:
2017 case LLOG_ORIGIN_HANDLE_CLOSE:
2019 rc = lustre_msg_check_version(msg, LUSTRE_LOG_VERSION);
2021 CERROR("bad opc %u version %08x, expecting %08x\n",
2022 msg->opc, msg->version, LUSTRE_LOG_VERSION);
2028 rc = lustre_msg_check_version(msg, LUSTRE_OBD_VERSION);
2030 CERROR("bad opc %u version %08x, expecting %08x\n",
2031 msg->opc, msg->version, LUSTRE_OBD_VERSION);
2034 CERROR("MDS unknown opcode %d\n", msg->opc);
2042 static char str[PTL_NALFMT_SIZE];
2043 int mds_handle(struct ptlrpc_request *req)
2045 int should_process, fail = OBD_FAIL_MDS_ALL_REPLY_NET;
2047 struct mds_obd *mds = NULL; /* quell gcc overwarning */
2048 struct obd_device *obd = NULL;
2051 OBD_FAIL_RETURN(OBD_FAIL_MDS_ALL_REQUEST_NET | OBD_FAIL_ONCE, 0);
2053 rc = mds_msg_check_version(req->rq_reqmsg);
2055 CERROR("MDS drop mal-formed request\n");
2059 LASSERT(current->journal_info == NULL);
2060 /* XXX identical to OST */
2061 if (req->rq_reqmsg->opc != MDS_CONNECT) {
2062 struct mds_export_data *med;
2065 if (req->rq_export == NULL) {
2066 CERROR("operation %d on unconnected MDS from NID %s\n",
2067 req->rq_reqmsg->opc,
2068 ptlrpc_peernid2str(&req->rq_peer, str));
2069 req->rq_status = -ENOTCONN;
2070 GOTO(out, rc = -ENOTCONN);
2073 med = &req->rq_export->exp_mds_data;
2074 obd = req->rq_export->exp_obd;
2077 /* sanity check: if the xid matches, the request must
2078 * be marked as a resent or replayed */
2079 if (req->rq_xid == med->med_mcd->mcd_last_xid) {
2080 LASSERTF(lustre_msg_get_flags(req->rq_reqmsg) &
2081 (MSG_RESENT | MSG_REPLAY),
2082 "rq_xid "LPU64" matches last_xid, "
2083 "expected RESENT flag\n",
2086 /* else: note the opposite is not always true; a
2087 * RESENT req after a failover will usually not match
2088 * the last_xid, since it was likely never
2089 * committed. A REPLAYed request will almost never
2090 * match the last xid, however it could for a
2091 * committed, but still retained, open. */
2093 spin_lock_bh(&obd->obd_processing_task_lock);
2094 recovering = obd->obd_recovering;
2095 spin_unlock_bh(&obd->obd_processing_task_lock);
2097 rc = mds_filter_recovery_request(req, obd,
2099 if (rc || should_process == 0) {
2101 } else if (should_process < 0) {
2102 req->rq_status = should_process;
2103 rc = ptlrpc_error(req);
2109 switch (req->rq_reqmsg->opc) {
2111 DEBUG_REQ(D_INODE, req, "connect");
2112 OBD_FAIL_RETURN(OBD_FAIL_MDS_CONNECT_NET, 0);
2113 rc = target_handle_connect(req);
2115 /* Now that we have an export, set mds. */
2116 mds = mds_req2mds(req);
2119 case MDS_DISCONNECT:
2120 DEBUG_REQ(D_INODE, req, "disconnect");
2121 OBD_FAIL_RETURN(OBD_FAIL_MDS_DISCONNECT_NET, 0);
2122 rc = target_handle_disconnect(req);
2123 req->rq_status = rc; /* superfluous? */
2127 DEBUG_REQ(D_INODE, req, "getstatus");
2128 OBD_FAIL_RETURN(OBD_FAIL_MDS_GETSTATUS_NET, 0);
2129 rc = mds_getstatus(req);
2133 DEBUG_REQ(D_INODE, req, "getattr");
2134 OBD_FAIL_RETURN(OBD_FAIL_MDS_GETATTR_NET, 0);
2135 rc = mds_getattr(req, MDS_REQ_REC_OFF);
2138 case MDS_GETATTR_LOCK: {
2139 struct lustre_handle lockh;
2140 DEBUG_REQ(D_INODE, req, "getattr_lock");
2141 OBD_FAIL_RETURN(OBD_FAIL_MDS_GETATTR_LOCK_NET, 0);
2143 /* If this request gets a reconstructed reply, we won't be
2144 * acquiring any new locks in mds_getattr_lock, so we don't
2148 rc = mds_getattr_lock(req, MDS_REQ_REC_OFF, &lockh,
2149 MDS_INODELOCK_UPDATE);
2150 /* this non-intent call (from an ioctl) is special */
2151 req->rq_status = rc;
2152 if (rc == 0 && lockh.cookie)
2153 ldlm_lock_decref(&lockh, LCK_PR);
2157 DEBUG_REQ(D_INODE, req, "statfs");
2158 OBD_FAIL_RETURN(OBD_FAIL_MDS_STATFS_NET, 0);
2159 rc = mds_statfs(req);
2163 DEBUG_REQ(D_INODE, req, "readpage");
2164 OBD_FAIL_RETURN(OBD_FAIL_MDS_READPAGE_NET, 0);
2165 rc = mds_readpage(req, MDS_REQ_REC_OFF);
2167 if (OBD_FAIL_CHECK_ONCE(OBD_FAIL_MDS_SENDPAGE)) {
2168 if (req->rq_reply_state) {
2169 lustre_free_reply_state (req->rq_reply_state);
2170 req->rq_reply_state = NULL;
2177 __u32 *opcp = lustre_msg_buf(req->rq_reqmsg, MDS_REQ_REC_OFF,
2180 int size[3] = {sizeof(struct mds_body), mds->mds_max_mdsize,
2181 mds->mds_max_cookiesize};
2184 /* NB only peek inside req now; mds_reint() will swab it */
2186 CERROR ("Can't inspect opcode\n");
2191 if (lustre_msg_swabbed (req->rq_reqmsg))
2194 DEBUG_REQ(D_INODE, req, "reint %d (%s)", opc,
2195 (opc < sizeof(reint_names) / sizeof(reint_names[0]) ||
2196 reint_names[opc] == NULL) ? reint_names[opc] :
2199 OBD_FAIL_RETURN(OBD_FAIL_MDS_REINT_NET, 0);
2201 if (opc == REINT_UNLINK || opc == REINT_RENAME)
2203 else if (opc == REINT_OPEN)
2208 rc = lustre_pack_reply(req, bufcount, size, NULL);
2212 rc = mds_reint(req, MDS_REQ_REC_OFF, NULL);
2213 fail = OBD_FAIL_MDS_REINT_NET_REP;
2218 DEBUG_REQ(D_INODE, req, "close");
2219 OBD_FAIL_RETURN(OBD_FAIL_MDS_CLOSE_NET, 0);
2220 rc = mds_close(req, MDS_REQ_REC_OFF);
2223 case MDS_DONE_WRITING:
2224 DEBUG_REQ(D_INODE, req, "done_writing");
2225 OBD_FAIL_RETURN(OBD_FAIL_MDS_DONE_WRITING_NET, 0);
2226 rc = mds_done_writing(req, MDS_REQ_REC_OFF);
2230 DEBUG_REQ(D_INODE, req, "pin");
2231 OBD_FAIL_RETURN(OBD_FAIL_MDS_PIN_NET, 0);
2232 rc = mds_pin(req, MDS_REQ_REC_OFF);
2236 DEBUG_REQ(D_INODE, req, "sync");
2237 OBD_FAIL_RETURN(OBD_FAIL_MDS_SYNC_NET, 0);
2238 rc = mds_sync(req, MDS_REQ_REC_OFF);
2242 DEBUG_REQ(D_INODE, req, "ping");
2243 rc = target_handle_ping(req);
2246 case OBD_LOG_CANCEL:
2247 CDEBUG(D_INODE, "log cancel\n");
2248 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOG_CANCEL_NET, 0);
2249 rc = -ENOTSUPP; /* la la la */
2253 DEBUG_REQ(D_INODE, req, "enqueue");
2254 OBD_FAIL_RETURN(OBD_FAIL_LDLM_ENQUEUE, 0);
2255 rc = ldlm_handle_enqueue(req, ldlm_server_completion_ast,
2256 ldlm_server_blocking_ast, NULL);
2257 fail = OBD_FAIL_LDLM_REPLY;
2260 DEBUG_REQ(D_INODE, req, "convert");
2261 OBD_FAIL_RETURN(OBD_FAIL_LDLM_CONVERT, 0);
2262 rc = ldlm_handle_convert(req);
2264 case LDLM_BL_CALLBACK:
2265 case LDLM_CP_CALLBACK:
2266 DEBUG_REQ(D_INODE, req, "callback");
2267 CERROR("callbacks should not happen on MDS\n");
2269 OBD_FAIL_RETURN(OBD_FAIL_LDLM_BL_CALLBACK, 0);
2271 case LLOG_ORIGIN_HANDLE_OPEN:
2272 DEBUG_REQ(D_INODE, req, "llog_init");
2273 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
2274 rc = llog_origin_handle_open(req);
2276 case LLOG_ORIGIN_HANDLE_NEXT_BLOCK:
2277 DEBUG_REQ(D_INODE, req, "llog next block");
2278 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
2279 rc = llog_origin_handle_next_block(req);
2281 case LLOG_ORIGIN_HANDLE_PREV_BLOCK:
2282 DEBUG_REQ(D_INODE, req, "llog prev block");
2283 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
2284 rc = llog_origin_handle_prev_block(req);
2286 case LLOG_ORIGIN_HANDLE_READ_HEADER:
2287 DEBUG_REQ(D_INODE, req, "llog read header");
2288 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
2289 rc = llog_origin_handle_read_header(req);
2291 case LLOG_ORIGIN_HANDLE_CLOSE:
2292 DEBUG_REQ(D_INODE, req, "llog close");
2293 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
2294 rc = llog_origin_handle_close(req);
2297 DEBUG_REQ(D_INODE, req, "ost_create");
2298 rc = mdt_obj_create(req);
2301 DEBUG_REQ(D_INODE, req, "get_info");
2302 rc = mdt_get_info(req);
2305 DEBUG_REQ(D_INODE, req, "set_info");
2306 rc = mdt_set_info(req);
2309 CDEBUG(D_INODE, "write\n");
2310 OBD_FAIL_RETURN(OBD_FAIL_OST_BRW_NET, 0);
2311 rc = ost_brw_write(req, NULL);
2312 LASSERT(current->journal_info == NULL);
2313 /* mdt_brw sends its own replies */
2317 DEBUG_REQ(D_INODE, req, "llog catinfo");
2318 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
2319 rc = llog_catinfo(req);
2322 req->rq_status = -ENOTSUPP;
2323 rc = ptlrpc_error(req);
2327 LASSERT(current->journal_info == NULL);
2331 /* If we're DISCONNECTing, the mds_export_data is already freed */
2332 if (!rc && req->rq_reqmsg->opc != MDS_DISCONNECT) {
2333 struct mds_export_data *med = &req->rq_export->exp_mds_data;
2334 struct obd_device *obd = list_entry(mds, struct obd_device,
2336 req->rq_repmsg->last_xid =
2337 le64_to_cpu(med->med_mcd->mcd_last_xid);
2339 if (!obd->obd_no_transno) {
2340 req->rq_repmsg->last_committed =
2341 obd->obd_last_committed;
2343 DEBUG_REQ(D_IOCTL, req,
2344 "not sending last_committed update");
2346 CDEBUG(D_INFO, "last_transno "LPU64", last_committed "LPU64
2348 mds->mds_last_transno, obd->obd_last_committed,
2353 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_LAST_REPLAY) {
2354 if (obd && obd->obd_recovering) {
2355 DEBUG_REQ(D_HA, req, "LAST_REPLAY, queuing reply");
2356 return target_queue_final_reply(req, rc);
2358 /* Lost a race with recovery; let the error path DTRT. */
2359 rc = req->rq_status = -ENOTCONN;
2362 target_send_reply(req, rc, fail);
2366 /* Update the server data on disk. This stores the new mount_count and also the
2367 * last_rcvd value to disk. If we don't have a clean shutdown, then the server
2368 * last_rcvd value may be less than that of the clients. This will alert us
2369 * that we may need to do client recovery.
2371 * Also assumes for mds_last_transno that we are not modifying it (no locking).
2373 int mds_update_server_data(struct obd_device *obd, int force_sync)
2375 struct mds_obd *mds = &obd->u.mds;
2376 struct mds_server_data *msd = mds->mds_server_data;
2377 struct file *filp = mds->mds_rcvd_filp;
2378 struct lvfs_run_ctxt saved;
2383 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
2384 msd->msd_last_transno = cpu_to_le64(mds->mds_last_transno);
2386 CDEBUG(D_SUPER, "MDS mount_count is "LPU64", last_transno is "LPU64"\n",
2387 mds->mds_mount_count, mds->mds_last_transno);
2388 rc = fsfilt_write_record(obd, filp, msd, sizeof(*msd), &off, force_sync);
2390 CERROR("error writing MDS server data: rc = %d\n", rc);
2391 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
2396 /* saves last allocated fid counter to file. */
2397 int mds_update_last_fid(struct obd_device *obd, void *handle,
2400 struct mds_obd *mds = &obd->u.mds;
2401 struct file *filp = mds->mds_fid_filp;
2402 struct lvfs_run_ctxt saved;
2407 down(&mds->mds_last_fid_sem);
2408 if (mds->mds_last_fid_changed) {
2409 CDEBUG(D_SUPER, "MDS last_fid is #"LPU64"\n",
2413 fsfilt_add_journal_cb(obd, mds->mds_sb,
2414 mds->mds_last_fid, handle,
2415 mds_commit_last_fid_cb, NULL);
2418 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
2419 rc = fsfilt_write_record(obd, filp, &mds->mds_last_fid,
2420 sizeof(mds->mds_last_fid),
2423 CERROR("error writing MDS last_fid #"LPU64
2424 ", err = %d\n", mds->mds_last_fid, rc);
2426 mds->mds_last_fid_changed = 0;
2429 CDEBUG(D_SUPER, "wrote fid #"LPU64" at idx "
2430 "%llu: err = %d\n", mds->mds_last_fid,
2432 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
2434 up(&mds->mds_last_fid_sem);
2439 void mds_set_last_fid(struct obd_device *obd, __u64 fid)
2441 struct mds_obd *mds = &obd->u.mds;
2443 down(&mds->mds_last_fid_sem);
2444 if (fid > mds->mds_last_fid) {
2445 mds->mds_last_fid = fid;
2446 mds->mds_last_fid_changed = 1;
2448 up(&mds->mds_last_fid_sem);
2451 void mds_commit_last_transno_cb(struct obd_device *obd,
2452 __u64 transno, void *data,
2455 obd_transno_commit_cb(obd, transno, error);
2458 void mds_commit_last_fid_cb(struct obd_device *obd,
2459 __u64 fid, void *data,
2463 CERROR("%s: fid "LPD64" commit error: %d\n",
2464 obd->obd_name, fid, error);
2468 CDEBUG(D_HA, "%s: fid "LPD64" committed\n",
2469 obd->obd_name, fid);
2473 * allocates new lustre_id on passed @inode and saves it to inode EA.
2475 int mds_alloc_inode_sid(struct obd_device *obd, struct inode *inode,
2476 void *handle, struct lustre_id *id)
2478 struct mds_obd *mds = &obd->u.mds;
2482 LASSERT(id != NULL);
2483 LASSERT(obd != NULL);
2485 id_group(id) = mds->mds_num;
2487 down(&mds->mds_last_fid_sem);
2488 mds->mds_last_fid_changed = 1;
2489 id_fid(id) = ++mds->mds_last_fid;
2490 up(&mds->mds_last_fid_sem);
2492 id_ino(id) = inode->i_ino;
2493 id_gen(id) = inode->i_generation;
2494 id_type(id) = (S_IFMT & inode->i_mode);
2496 rc = mds_update_inode_sid(obd, inode, handle, id);
2498 CERROR("Can't update inode FID EA, "
2506 * reads inode self id from inode EA. Probably later this should be replaced by
2507 * caching inode self id to avoid raeding it every time it is needed.
2509 int mds_read_inode_sid(struct obd_device *obd, struct inode *inode,
2510 struct lustre_id *id)
2515 LASSERT(id != NULL);
2516 LASSERT(obd != NULL);
2517 LASSERT(inode != NULL);
2519 rc = fsfilt_get_sid(obd, inode, &id->li_fid,
2520 sizeof(id->li_fid));
2522 CERROR("fsfilt_get_sid() failed, "
2535 /* updates inode self id in EA. */
2536 int mds_update_inode_sid(struct obd_device *obd, struct inode *inode,
2537 void *handle, struct lustre_id *id)
2542 LASSERT(id != NULL);
2543 LASSERT(obd != NULL);
2544 LASSERT(inode != NULL);
2546 rc = fsfilt_set_sid(obd, inode, handle, &id->li_fid,
2547 sizeof(id->li_fid));
2549 CERROR("fsfilt_set_sid() failed, rc = %d\n", rc);
2557 * reads inode id on master MDS. This is usualy done by CMOBD to update requests
2558 * to master MDS by correct store cookie, needed to find inode on master MDS
2561 int mds_read_inode_mid(struct obd_device *obd, struct inode *inode,
2562 struct lustre_id *id)
2567 LASSERT(id != NULL);
2568 LASSERT(obd != NULL);
2569 LASSERT(inode != NULL);
2571 rc = fsfilt_get_mid(obd, inode, id, sizeof(*id));
2573 CERROR("fsfilt_get_mid() failed, "
2587 * updates master inode id. Usualy this is done by CMOBD after an inode is
2588 * created and relationship between cache MDS and master one should be
2591 int mds_update_inode_mid(struct obd_device *obd, struct inode *inode,
2592 void *handle, struct lustre_id *id)
2597 LASSERT(id != NULL);
2598 LASSERT(obd != NULL);
2599 LASSERT(inode != NULL);
2601 rc = fsfilt_set_mid(obd, inode, handle, id, sizeof(*id));
2603 CERROR("fsfilt_set_mid() failed, rc = %d\n", rc);
2610 /* mount the file system (secretly) */
2611 static int mds_setup(struct obd_device *obd, obd_count len, void *buf)
2613 struct lustre_cfg* lcfg = buf;
2614 struct mds_obd *mds = &obd->u.mds;
2615 char *options = NULL;
2616 struct vfsmount *mnt;
2622 dev_clear_rdonly(2);
2624 if (!lcfg->lcfg_inlbuf1 || !lcfg->lcfg_inlbuf2)
2625 RETURN(rc = -EINVAL);
2627 obd->obd_fsops = fsfilt_get_ops(lcfg->lcfg_inlbuf2);
2628 if (IS_ERR(obd->obd_fsops))
2629 RETURN(rc = PTR_ERR(obd->obd_fsops));
2631 mds->mds_max_mdsize = sizeof(struct lov_mds_md);
2633 page = __get_free_page(GFP_KERNEL);
2637 options = (char *)page;
2638 memset(options, 0, PAGE_SIZE);
2641 * here we use "iopen_nopriv" hardcoded, because it affects MDS utility
2642 * and the rest of options are passed by mount options. Probably this
2643 * should be moved to somewhere else like startup scripts or lconf.
2645 sprintf(options, "iopen_nopriv");
2647 if (lcfg->lcfg_inllen4 > 0 && lcfg->lcfg_inlbuf4)
2648 sprintf(options + strlen(options), ",%s",
2649 lcfg->lcfg_inlbuf4);
2651 /* we have to know mdsnum before touching underlying fs -bzzz */
2652 sema_init(&mds->mds_lmv_sem, 1);
2653 mds->mds_lmv_connected = 0;
2654 if (lcfg->lcfg_inllen5 > 0 && lcfg->lcfg_inlbuf5 &&
2655 strcmp(lcfg->lcfg_inlbuf5, "dumb")) {
2658 CDEBUG(D_OTHER, "MDS: %s is master for %s\n",
2659 obd->obd_name, lcfg->lcfg_inlbuf5);
2661 generate_random_uuid(uuid);
2662 class_uuid_unparse(uuid, &mds->mds_lmv_uuid);
2664 OBD_ALLOC(mds->mds_lmv_name, lcfg->lcfg_inllen5);
2665 if (mds->mds_lmv_name == NULL)
2666 RETURN(rc = -ENOMEM);
2668 memcpy(mds->mds_lmv_name, lcfg->lcfg_inlbuf5,
2669 lcfg->lcfg_inllen5);
2671 rc = mds_lmv_connect(obd, mds->mds_lmv_name);
2673 OBD_FREE(mds->mds_lmv_name, lcfg->lcfg_inllen5);
2678 mds->mds_obd_type = MDS_MASTER_OBD;
2680 if (lcfg->lcfg_inllen6 > 0 && lcfg->lcfg_inlbuf6 &&
2681 strcmp(lcfg->lcfg_inlbuf6, "dumb")) {
2682 if (!memcmp(lcfg->lcfg_inlbuf6, "master", strlen("master"))) {
2683 mds->mds_obd_type = MDS_MASTER_OBD;
2684 } else if (!memcmp(lcfg->lcfg_inlbuf6, "cache", strlen("cache"))) {
2685 mds->mds_obd_type = MDS_CACHE_OBD;
2689 mnt = do_kern_mount(lcfg->lcfg_inlbuf2, 0, lcfg->lcfg_inlbuf1, options);
2694 CERROR("do_kern_mount failed: rc = %d\n", rc);
2698 CDEBUG(D_SUPER, "%s: mnt = %p\n", lcfg->lcfg_inlbuf1, mnt);
2700 mds->mds_last_fid_changed = 0;
2701 sema_init(&mds->mds_epoch_sem, 1);
2702 sema_init(&mds->mds_last_fid_sem, 1);
2703 atomic_set(&mds->mds_real_clients, 0);
2704 spin_lock_init(&mds->mds_transno_lock);
2705 sema_init(&mds->mds_orphan_recovery_sem, 1);
2706 mds->mds_max_cookiesize = sizeof(struct llog_cookie);
2708 sprintf(ns_name, "mds-%s", obd->obd_uuid.uuid);
2709 obd->obd_namespace = ldlm_namespace_new(ns_name, LDLM_NAMESPACE_SERVER);
2711 if (obd->obd_namespace == NULL) {
2712 mds_cleanup(obd, 0);
2713 GOTO(err_put, rc = -ENOMEM);
2715 ldlm_register_intent(obd->obd_namespace, mds_intent_policy);
2717 rc = mds_fs_setup(obd, mnt);
2719 CERROR("%s: MDS filesystem method init failed: rc = %d\n",
2724 rc = llog_start_commit_thread();
2729 * this check for @dumb string is needed to handle mounting MDS with
2730 * smfs. Read lconf:MDSDEV.write_conf() for more details.
2732 if (lcfg->lcfg_inllen3 > 0 && lcfg->lcfg_inlbuf3 &&
2733 strcmp(lcfg->lcfg_inlbuf3, "dumb")) {
2736 generate_random_uuid(uuid);
2737 class_uuid_unparse(uuid, &mds->mds_lov_uuid);
2739 OBD_ALLOC(mds->mds_profile, lcfg->lcfg_inllen3);
2740 if (mds->mds_profile == NULL)
2741 GOTO(err_fs, rc = -ENOMEM);
2743 memcpy(mds->mds_profile, lcfg->lcfg_inlbuf3,
2744 lcfg->lcfg_inllen3);
2747 * setup root id in the case this is not clients write
2748 * setup. This is important, as in the case of LMV we need
2749 * mds->mds_num to be already assigned to form correct root fid.
2751 rc = mds_fs_setup_rootid(obd);
2755 /* setup lustre id for ID directory. */
2756 rc = mds_fs_setup_virtid(obd);
2761 ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
2762 "mds_ldlm_client", &obd->obd_ldlm_client);
2763 obd->obd_replayable = 1;
2765 mds->mds_counters = lprocfs_alloc_mds_counters();
2767 rc = mds_postsetup(obd);
2774 /* No extra cleanup needed for llog_init_commit_thread() */
2775 mds_fs_cleanup(obd, 0);
2777 ldlm_namespace_free(obd->obd_namespace, 0);
2778 obd->obd_namespace = NULL;
2781 mntput(mds->mds_vfsmnt);
2785 fsfilt_put_ops(obd->obd_fsops);
2789 static int mds_postsetup(struct obd_device *obd)
2791 struct mds_obd *mds = &obd->u.mds;
2795 rc = obd_llog_setup(obd, &obd->obd_llogs, LLOG_CONFIG_ORIG_CTXT,
2796 obd, 0, NULL, &llog_lvfs_ops);
2800 if (mds->mds_profile) {
2801 struct llog_ctxt *lgctxt;
2802 struct lvfs_run_ctxt saved;
2803 struct lustre_profile *lprof;
2804 struct config_llog_instance cfg;
2806 cfg.cfg_instance = NULL;
2807 cfg.cfg_uuid = mds->mds_lov_uuid;
2808 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
2810 lgctxt = llog_get_context(&obd->obd_llogs, LLOG_CONFIG_ORIG_CTXT);
2812 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
2813 GOTO(err_llog, rc = -EINVAL);
2816 rc = class_config_process_llog(lgctxt, mds->mds_profile, &cfg);
2817 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
2822 lprof = class_get_profile(mds->mds_profile);
2823 if (lprof == NULL) {
2824 CERROR("No profile found: %s\n", mds->mds_profile);
2825 GOTO(err_cleanup, rc = -ENOENT);
2827 rc = mds_lov_connect(obd, lprof->lp_lov);
2829 GOTO(err_cleanup, rc);
2831 rc = mds_lmv_postsetup(obd);
2833 GOTO(err_cleanup, rc);
2841 obd_llog_cleanup(llog_get_context(&obd->obd_llogs,
2842 LLOG_CONFIG_ORIG_CTXT));
2846 int mds_postrecov(struct obd_device *obd)
2848 struct mds_obd *mds = &obd->u.mds;
2849 struct llog_ctxt *ctxt;
2850 int rc, item = 0, valsize;
2854 LASSERT(!obd->obd_recovering);
2855 ctxt = llog_get_context(&obd->obd_llogs, LLOG_UNLINK_ORIG_CTXT);
2856 LASSERT(ctxt != NULL);
2858 /* set nextid first, so we are sure it happens */
2859 rc = mds_lov_set_nextid(obd);
2861 CERROR("%s: mds_lov_set_nextid failed\n", obd->obd_name);
2865 /* clean PENDING dir */
2866 rc = mds_cleanup_orphans(obd);
2871 group = FILTER_GROUP_FIRST_MDS + mds->mds_num;
2872 valsize = sizeof(group);
2873 rc = obd_set_info(mds->mds_lov_exp, strlen("mds_conn"), "mds_conn",
2878 rc = llog_connect(ctxt, obd->u.mds.mds_lov_desc.ld_tgt_count,
2881 CERROR("%s: failed at llog_origin_connect: %d\n",
2886 /* remove the orphaned precreated objects */
2887 rc = mds_lov_clearorphans(mds, NULL /* all OSTs */);
2892 RETURN(rc < 0 ? rc : item);
2895 /* cleanup all llogging subsystems */
2896 rc = obd_llog_finish(obd, &obd->obd_llogs,
2897 mds->mds_lov_desc.ld_tgt_count);
2899 CERROR("%s: failed to cleanup llogging subsystems\n",
2904 int mds_lov_clean(struct obd_device *obd)
2906 struct mds_obd *mds = &obd->u.mds;
2908 if (mds->mds_profile) {
2910 struct llog_ctxt *llctx;
2911 struct lvfs_run_ctxt saved;
2912 struct config_llog_instance cfg;
2913 int len = strlen(mds->mds_profile) + sizeof("-clean") + 1;
2915 OBD_ALLOC(cln_prof, len);
2916 sprintf(cln_prof, "%s-clean", mds->mds_profile);
2918 cfg.cfg_instance = NULL;
2919 cfg.cfg_uuid = mds->mds_lov_uuid;
2921 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
2922 llctx = llog_get_context(&obd->obd_llogs,
2923 LLOG_CONFIG_ORIG_CTXT);
2924 class_config_process_llog(llctx, cln_prof, &cfg);
2925 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
2927 OBD_FREE(cln_prof, len);
2928 OBD_FREE(mds->mds_profile, strlen(mds->mds_profile) + 1);
2929 mds->mds_profile = NULL;
2934 int mds_lmv_clean(struct obd_device *obd)
2936 struct mds_obd *mds = &obd->u.mds;
2938 if (mds->mds_lmv_name) {
2939 OBD_FREE(mds->mds_lmv_name, strlen(mds->mds_lmv_name) + 1);
2940 mds->mds_lmv_name = NULL;
2945 static int mds_precleanup(struct obd_device *obd, int flags)
2951 mds_lov_disconnect(obd, flags);
2953 obd_llog_cleanup(llog_get_context(&obd->obd_llogs, LLOG_CONFIG_ORIG_CTXT));
2957 static int mds_cleanup(struct obd_device *obd, int flags)
2959 struct mds_obd *mds = &obd->u.mds;
2962 if (mds->mds_sb == NULL)
2965 mds_update_server_data(obd, 1);
2966 mds_update_last_fid(obd, NULL, 1);
2968 if (mds->mds_lov_objids != NULL) {
2969 int size = mds->mds_lov_desc.ld_tgt_count *
2971 OBD_FREE(mds->mds_lov_objids, size);
2973 mds_fs_cleanup(obd, flags);
2978 * 2 seems normal on mds, (may_umount() also expects 2 fwiw), but we
2979 * only see 1 at this point in obdfilter.
2981 if (atomic_read(&obd->u.mds.mds_vfsmnt->mnt_count) > 2)
2982 CERROR("%s: mount busy, mnt_count %d != 2\n", obd->obd_name,
2983 atomic_read(&obd->u.mds.mds_vfsmnt->mnt_count));
2985 mntput(mds->mds_vfsmnt);
2988 ldlm_namespace_free(obd->obd_namespace, flags & OBD_OPT_FORCE);
2990 if (mds->mds_counters) {
2991 lprocfs_free_mds_counters(mds->mds_counters);
2994 spin_lock_bh(&obd->obd_processing_task_lock);
2995 if (obd->obd_recovering) {
2996 target_cancel_recovery_timer(obd);
2997 obd->obd_recovering = 0;
2999 spin_unlock_bh(&obd->obd_processing_task_lock);
3002 dev_clear_rdonly(2);
3003 fsfilt_put_ops(obd->obd_fsops);
3008 static void fixup_handle_for_resent_req(struct ptlrpc_request *req,
3010 struct ldlm_lock *new_lock,
3011 struct ldlm_lock **old_lock,
3012 struct lustre_handle *lockh)
3014 struct obd_export *exp = req->rq_export;
3015 struct obd_device *obd = exp->exp_obd;
3016 struct ldlm_request *dlmreq =
3017 lustre_msg_buf(req->rq_reqmsg, offset, sizeof (*dlmreq));
3018 struct lustre_handle remote_hdl = dlmreq->lock_handle1;
3019 struct list_head *iter;
3021 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
3024 l_lock(&obd->obd_namespace->ns_lock);
3025 list_for_each(iter, &exp->exp_ldlm_data.led_held_locks) {
3026 struct ldlm_lock *lock;
3027 lock = list_entry(iter, struct ldlm_lock, l_export_chain);
3028 if (lock == new_lock)
3030 if (lock->l_remote_handle.cookie == remote_hdl.cookie) {
3031 lockh->cookie = lock->l_handle.h_cookie;
3032 LDLM_DEBUG(lock, "restoring lock cookie");
3033 DEBUG_REQ(D_HA, req, "restoring lock cookie "LPX64,
3036 *old_lock = LDLM_LOCK_GET(lock);
3037 l_unlock(&obd->obd_namespace->ns_lock);
3041 l_unlock(&obd->obd_namespace->ns_lock);
3043 /* If the xid matches, then we know this is a resent request,
3044 * and allow it. (It's probably an OPEN, for which we don't
3046 if (req->rq_xid == exp->exp_mds_data.med_mcd->mcd_last_xid)
3049 /* This remote handle isn't enqueued, so we never received or
3050 * processed this request. Clear MSG_RESENT, because it can
3051 * be handled like any normal request now. */
3053 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
3055 DEBUG_REQ(D_HA, req, "no existing lock with rhandle "LPX64,
3059 int intent_disposition(struct ldlm_reply *rep, int flag)
3063 return (rep->lock_policy_res1 & flag);
3066 void intent_set_disposition(struct ldlm_reply *rep, int flag)
3070 rep->lock_policy_res1 |= flag;
3073 static int mds_intent_policy(struct ldlm_namespace *ns,
3074 struct ldlm_lock **lockp, void *req_cookie,
3075 ldlm_mode_t mode, int flags, void *data)
3077 struct ptlrpc_request *req = req_cookie;
3078 struct ldlm_lock *lock = *lockp;
3079 struct ldlm_intent *it;
3080 struct mds_obd *mds = &req->rq_export->exp_obd->u.mds;
3081 struct ldlm_reply *rep;
3082 struct lustre_handle lockh[2] = {{0}, {0}};
3083 struct ldlm_lock *new_lock = NULL;
3084 int getattr_part = MDS_INODELOCK_UPDATE;
3085 int rc, repsize[4] = { sizeof(struct ldlm_reply),
3086 sizeof(struct mds_body),
3087 mds->mds_max_mdsize,
3088 mds->mds_max_cookiesize };
3089 int offset = MDS_REQ_INTENT_REC_OFF;
3092 LASSERT(req != NULL);
3094 if (req->rq_reqmsg->bufcount <= MDS_REQ_INTENT_IT_OFF) {
3095 /* No intent was provided */
3096 int size = sizeof(struct ldlm_reply);
3097 rc = lustre_pack_reply(req, 1, &size, NULL);
3102 it = lustre_swab_reqbuf(req, MDS_REQ_INTENT_IT_OFF, sizeof(*it),
3103 lustre_swab_ldlm_intent);
3105 CERROR("Intent missing\n");
3106 RETURN(req->rq_status = -EFAULT);
3109 LDLM_DEBUG(lock, "intent policy, opc: %s", ldlm_it2str(it->opc));
3111 rc = lustre_pack_reply(req, 3, repsize, NULL);
3113 RETURN(req->rq_status = rc);
3115 rep = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*rep));
3116 LASSERT(rep != NULL);
3118 intent_set_disposition(rep, DISP_IT_EXECD);
3120 /* execute policy */
3121 switch ((long)it->opc) {
3123 case IT_CREAT|IT_OPEN:
3124 /* XXX swab here to assert that an mds_open reint
3125 * packet is following */
3126 fixup_handle_for_resent_req(req, MDS_REQ_INTENT_LOCKREQ_OFF,
3128 rep->lock_policy_res2 = mds_reint(req, offset, lockh);
3130 /* We abort the lock if the lookup was negative and
3131 * we did not make it to the OPEN portion */
3132 if (!intent_disposition(rep, DISP_LOOKUP_EXECD))
3133 RETURN(ELDLM_LOCK_ABORTED);
3134 if (intent_disposition(rep, DISP_LOOKUP_NEG) &&
3135 !intent_disposition(rep, DISP_OPEN_OPEN))
3137 /* IT_OPEN may return lock on cross-node dentry
3138 * that we want to hold during attr retrival -bzzz */
3139 if (rc != 0 || lockh[0].cookie == 0)
3140 RETURN(ELDLM_LOCK_ABORTED);
3143 getattr_part = MDS_INODELOCK_LOOKUP;
3146 getattr_part |= MDS_INODELOCK_LOOKUP;
3148 fixup_handle_for_resent_req(req, MDS_REQ_INTENT_LOCKREQ_OFF,
3149 lock, &new_lock, lockh);
3150 rep->lock_policy_res2 = mds_getattr_lock(req, offset, lockh,
3152 /* FIXME: LDLM can set req->rq_status. MDS sets
3153 policy_res{1,2} with disposition and status.
3154 - replay: returns 0 & req->status is old status
3155 - otherwise: returns req->status */
3156 if (intent_disposition(rep, DISP_LOOKUP_NEG))
3157 rep->lock_policy_res2 = 0;
3158 if (!intent_disposition(rep, DISP_LOOKUP_POS) ||
3159 rep->lock_policy_res2)
3160 RETURN(ELDLM_LOCK_ABORTED);
3161 if (req->rq_status != 0) {
3163 rep->lock_policy_res2 = req->rq_status;
3164 RETURN(ELDLM_LOCK_ABORTED);
3168 rc = mds_lock_and_check_slave(offset, req, lockh);
3169 if ((rep->lock_policy_res2 = rc)) {
3171 rep->lock_policy_res2 = 0;
3172 RETURN(ELDLM_LOCK_ABORTED);
3176 CERROR("Unhandled intent "LPD64"\n", it->opc);
3180 /* By this point, whatever function we called above must have either
3181 * filled in 'lockh', been an intent replay, or returned an error. We
3182 * want to allow replayed RPCs to not get a lock, since we would just
3183 * drop it below anyways because lock replay is done separately by the
3184 * client afterwards. For regular RPCs we want to give the new lock to
3185 * the client instead of whatever lock it was about to get. */
3186 if (new_lock == NULL)
3187 new_lock = ldlm_handle2lock(&lockh[0]);
3188 if (new_lock == NULL && (flags & LDLM_FL_INTENT_ONLY))
3191 LASSERTF(new_lock != NULL, "op "LPX64" lockh "LPX64"\n",
3192 it->opc, lockh[0].cookie);
3194 /* If we've already given this lock to a client once, then we should
3195 * have no readers or writers. Otherwise, we should have one reader
3196 * _or_ writer ref (which will be zeroed below) before returning the
3197 * lock to a client. */
3198 if (new_lock->l_export == req->rq_export) {
3199 LASSERT(new_lock->l_readers + new_lock->l_writers == 0);
3201 LASSERT(new_lock->l_export == NULL);
3202 LASSERT(new_lock->l_readers + new_lock->l_writers == 1);
3207 if (new_lock->l_export == req->rq_export) {
3208 /* Already gave this to the client, which means that we
3209 * reconstructed a reply. */
3210 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
3212 RETURN(ELDLM_LOCK_REPLACED);
3215 /* Fixup the lock to be given to the client */
3216 l_lock(&new_lock->l_resource->lr_namespace->ns_lock);
3217 new_lock->l_readers = 0;
3218 new_lock->l_writers = 0;
3220 new_lock->l_export = class_export_get(req->rq_export);
3221 list_add(&new_lock->l_export_chain,
3222 &new_lock->l_export->exp_ldlm_data.led_held_locks);
3224 new_lock->l_blocking_ast = lock->l_blocking_ast;
3225 new_lock->l_completion_ast = lock->l_completion_ast;
3227 memcpy(&new_lock->l_remote_handle, &lock->l_remote_handle,
3228 sizeof(lock->l_remote_handle));
3230 new_lock->l_flags &= ~LDLM_FL_LOCAL;
3232 LDLM_LOCK_PUT(new_lock);
3233 l_unlock(&new_lock->l_resource->lr_namespace->ns_lock);
3235 RETURN(ELDLM_LOCK_REPLACED);
3238 int mds_attach(struct obd_device *dev, obd_count len, void *data)
3240 struct lprocfs_static_vars lvars;
3242 lprocfs_init_multi_vars(0, &lvars);
3243 return lprocfs_obd_attach(dev, lvars.obd_vars);
3246 int mds_detach(struct obd_device *dev)
3248 return lprocfs_obd_detach(dev);
3251 int mdt_attach(struct obd_device *dev, obd_count len, void *data)
3253 struct lprocfs_static_vars lvars;
3255 lprocfs_init_multi_vars(1, &lvars);
3256 return lprocfs_obd_attach(dev, lvars.obd_vars);
3259 int mdt_detach(struct obd_device *dev)
3261 return lprocfs_obd_detach(dev);
3264 static int mdt_setup(struct obd_device *obd, obd_count len, void *buf)
3266 struct mds_obd *mds = &obd->u.mds;
3271 ptlrpc_init_svc(MDS_NBUFS, MDS_BUFSIZE, MDS_MAXREQSIZE,
3272 MDS_REQUEST_PORTAL, MDC_REPLY_PORTAL,
3273 mds_handle, "mds", obd->obd_proc_entry);
3275 if (!mds->mds_service) {
3276 CERROR("failed to start service\n");
3280 rc = ptlrpc_start_n_threads(obd, mds->mds_service, MDT_NUM_THREADS,
3283 GOTO(err_thread, rc);
3285 mds->mds_setattr_service =
3286 ptlrpc_init_svc(MDS_NBUFS, MDS_BUFSIZE, MDS_MAXREQSIZE,
3287 MDS_SETATTR_PORTAL, MDC_REPLY_PORTAL,
3288 mds_handle, "mds_setattr",
3289 obd->obd_proc_entry);
3290 if (!mds->mds_setattr_service) {
3291 CERROR("failed to start getattr service\n");
3292 GOTO(err_thread, rc = -ENOMEM);
3295 rc = ptlrpc_start_n_threads(obd, mds->mds_setattr_service,
3296 MDT_NUM_THREADS, "ll_mdt_attr");
3298 GOTO(err_thread2, rc);
3300 mds->mds_readpage_service =
3301 ptlrpc_init_svc(MDS_NBUFS, MDS_BUFSIZE, MDS_MAXREQSIZE,
3302 MDS_READPAGE_PORTAL, MDC_REPLY_PORTAL,
3303 mds_handle, "mds_readpage",
3304 obd->obd_proc_entry);
3305 if (!mds->mds_readpage_service) {
3306 CERROR("failed to start readpage service\n");
3307 GOTO(err_thread2, rc = -ENOMEM);
3310 rc = ptlrpc_start_n_threads(obd, mds->mds_readpage_service,
3311 MDT_NUM_THREADS, "ll_mdt_rdpg");
3314 GOTO(err_thread3, rc);
3319 ptlrpc_unregister_service(mds->mds_readpage_service);
3321 ptlrpc_unregister_service(mds->mds_setattr_service);
3323 ptlrpc_unregister_service(mds->mds_service);
3327 static int mdt_cleanup(struct obd_device *obd, int flags)
3329 struct mds_obd *mds = &obd->u.mds;
3332 ptlrpc_stop_all_threads(mds->mds_readpage_service);
3333 ptlrpc_unregister_service(mds->mds_readpage_service);
3335 ptlrpc_stop_all_threads(mds->mds_setattr_service);
3336 ptlrpc_unregister_service(mds->mds_setattr_service);
3338 ptlrpc_stop_all_threads(mds->mds_service);
3339 ptlrpc_unregister_service(mds->mds_service);
3344 static struct dentry *mds_lvfs_id2dentry(__u64 ino, __u32 gen,
3345 __u64 gr, void *data)
3347 struct lustre_id id;
3348 struct obd_device *obd = data;
3352 return mds_id2dentry(obd, &id, NULL);
3355 static int mds_get_info(struct obd_export *exp, __u32 keylen,
3356 void *key, __u32 *valsize, void *val)
3358 struct obd_device *obd;
3359 struct mds_obd *mds;
3362 obd = class_exp2obd(exp);
3366 CDEBUG(D_IOCTL, "invalid client cookie "LPX64"\n",
3367 exp->exp_handle.h_cookie);
3371 if (keylen >= strlen("reint_log") && memcmp(key, "reint_log", 9) == 0) {
3372 /* get log_context handle. */
3373 unsigned long *llh_handle = val;
3374 *valsize = sizeof(unsigned long);
3375 *llh_handle = (unsigned long)obd->obd_llog_ctxt[LLOG_REINT_ORIG_CTXT];
3378 if (keylen >= strlen("cache_sb") && memcmp(key, "cache_sb", 8) == 0) {
3379 /* get log_context handle. */
3380 unsigned long *sb = val;
3381 *valsize = sizeof(unsigned long);
3382 *sb = (unsigned long)obd->u.mds.mds_sb;
3386 if (keylen >= strlen("mdsize") && memcmp(key, "mdsize", keylen) == 0) {
3387 __u32 *mdsize = val;
3388 *valsize = sizeof(*mdsize);
3389 *mdsize = mds->mds_max_mdsize;
3393 if (keylen >= strlen("mdsnum") && strcmp(key, "mdsnum") == 0) {
3394 __u32 *mdsnum = val;
3395 *valsize = sizeof(*mdsnum);
3396 *mdsnum = mds->mds_num;
3400 if (keylen >= strlen("rootid") && strcmp(key, "rootid") == 0) {
3401 struct lustre_id *rootid = val;
3402 *valsize = sizeof(struct lustre_id);
3403 *rootid = mds->mds_rootid;
3407 CDEBUG(D_IOCTL, "invalid key\n");
3411 struct lvfs_callback_ops mds_lvfs_ops = {
3412 l_id2dentry: mds_lvfs_id2dentry,
3415 int mds_preprw(int cmd, struct obd_export *exp, struct obdo *oa,
3416 int objcount, struct obd_ioobj *obj,
3417 int niocount, struct niobuf_remote *nb,
3418 struct niobuf_local *res,
3419 struct obd_trans_info *oti);
3421 int mds_commitrw(int cmd, struct obd_export *exp, struct obdo *oa,
3422 int objcount, struct obd_ioobj *obj, int niocount,
3423 struct niobuf_local *res, struct obd_trans_info *oti,
3426 /* use obd ops to offer management infrastructure */
3427 static struct obd_ops mds_obd_ops = {
3428 .o_owner = THIS_MODULE,
3429 .o_attach = mds_attach,
3430 .o_detach = mds_detach,
3431 .o_connect = mds_connect,
3432 .o_connect_post = mds_connect_post,
3433 .o_init_export = mds_init_export,
3434 .o_destroy_export = mds_destroy_export,
3435 .o_disconnect = mds_disconnect,
3436 .o_setup = mds_setup,
3437 .o_precleanup = mds_precleanup,
3438 .o_cleanup = mds_cleanup,
3439 .o_postrecov = mds_postrecov,
3440 .o_statfs = mds_obd_statfs,
3441 .o_iocontrol = mds_iocontrol,
3442 .o_create = mds_obd_create,
3443 .o_destroy = mds_obd_destroy,
3444 .o_llog_init = mds_llog_init,
3445 .o_llog_finish = mds_llog_finish,
3446 .o_notify = mds_notify,
3447 .o_get_info = mds_get_info,
3448 .o_set_info = mds_set_info,
3449 .o_preprw = mds_preprw,
3450 .o_commitrw = mds_commitrw,
3453 static struct obd_ops mdt_obd_ops = {
3454 .o_owner = THIS_MODULE,
3455 .o_attach = mdt_attach,
3456 .o_detach = mdt_detach,
3457 .o_setup = mdt_setup,
3458 .o_cleanup = mdt_cleanup,
3461 static int __init mds_init(void)
3463 struct lprocfs_static_vars lvars;
3465 mds_group_hash_init();
3467 lprocfs_init_multi_vars(0, &lvars);
3468 class_register_type(&mds_obd_ops, NULL, lvars.module_vars,
3470 lprocfs_init_multi_vars(1, &lvars);
3471 class_register_type(&mdt_obd_ops, NULL, lvars.module_vars,
3477 static void /*__exit*/ mds_exit(void)
3479 mds_group_hash_cleanup();
3481 class_unregister_type(LUSTRE_MDS_NAME);
3482 class_unregister_type(LUSTRE_MDT_NAME);
3485 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
3486 MODULE_DESCRIPTION("Lustre Metadata Server (MDS)");
3487 MODULE_LICENSE("GPL");
3489 module_init(mds_init);
3490 module_exit(mds_exit);