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/namei.h>
43 #include <linux/ext3_fs.h>
44 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
45 # include <linux/smp_lock.h>
46 # include <linux/buffer_head.h>
47 # include <linux/workqueue.h>
48 # include <linux/mount.h>
50 # include <linux/locks.h>
52 #include <linux/obd_lov.h>
53 #include <linux/obd_ost.h>
54 #include <linux/lustre_mds.h>
55 #include <linux/lustre_fsfilt.h>
56 #include <linux/lprocfs_status.h>
57 #include <linux/lustre_commit_confd.h>
58 #include <linux/lustre_acl.h>
59 #include "mds_internal.h"
60 #include <linux/lustre_sec.h>
62 static int mds_intent_policy(struct ldlm_namespace *ns,
63 struct ldlm_lock **lockp, void *req_cookie,
64 ldlm_mode_t mode, int flags, void *data);
65 static int mds_postsetup(struct obd_device *obd);
66 static int mds_cleanup(struct obd_device *obd, int flags);
69 /* Assumes caller has already pushed into the kernel filesystem context */
70 static int mds_sendpage(struct ptlrpc_request *req, struct file *file,
71 loff_t offset, int count)
73 struct ptlrpc_bulk_desc *desc;
74 struct l_wait_info lwi;
76 int rc = 0, npages, i, tmpcount, tmpsize = 0;
79 LASSERT((offset & (PAGE_SIZE - 1)) == 0); /* I'm dubious about this */
81 npages = (count + PAGE_SIZE - 1) >> PAGE_SHIFT;
82 OBD_ALLOC(pages, sizeof(*pages) * npages);
84 GOTO(out, rc = -ENOMEM);
86 desc = ptlrpc_prep_bulk_exp(req, npages, BULK_PUT_SOURCE,
89 GOTO(out_free, rc = -ENOMEM);
91 for (i = 0, tmpcount = count; i < npages; i++, tmpcount -= tmpsize) {
92 tmpsize = tmpcount > PAGE_SIZE ? PAGE_SIZE : tmpcount;
94 pages[i] = alloc_pages(GFP_KERNEL, 0);
96 GOTO(cleanup_buf, rc = -ENOMEM);
98 ptlrpc_prep_bulk_page(desc, pages[i], 0, tmpsize);
101 for (i = 0, tmpcount = count; i < npages; i++, tmpcount -= tmpsize) {
102 tmpsize = tmpcount > PAGE_SIZE ? PAGE_SIZE : tmpcount;
103 CDEBUG(D_EXT2, "reading %u@%llu from dir %lu (size %llu)\n",
104 tmpsize, offset, file->f_dentry->d_inode->i_ino,
105 file->f_dentry->d_inode->i_size);
107 rc = fsfilt_readpage(req->rq_export->exp_obd, file,
108 kmap(pages[i]), tmpsize, &offset);
112 GOTO(cleanup_buf, rc = -EIO);
115 LASSERT(desc->bd_nob == count);
117 rc = ptlrpc_start_bulk_transfer(desc);
119 GOTO(cleanup_buf, rc);
121 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE)) {
122 CERROR("obd_fail_loc=%x, fail operation rc=%d\n",
123 OBD_FAIL_MDS_SENDPAGE, rc = -EIO);
124 GOTO(abort_bulk, rc);
127 lwi = LWI_TIMEOUT(obd_timeout * HZ / 4, NULL, NULL);
128 rc = l_wait_event(desc->bd_waitq, !ptlrpc_bulk_active(desc), &lwi);
129 LASSERT (rc == 0 || rc == -ETIMEDOUT);
132 if (desc->bd_success &&
133 desc->bd_nob_transferred == count)
134 GOTO(cleanup_buf, rc);
136 rc = -ETIMEDOUT; /* XXX should this be a different errno? */
139 DEBUG_REQ(D_ERROR, req, "bulk failed: %s %d(%d), evicting %s@%s\n",
140 (rc == -ETIMEDOUT) ? "timeout" : "network error",
141 desc->bd_nob_transferred, count,
142 req->rq_export->exp_client_uuid.uuid,
143 req->rq_export->exp_connection->c_remote_uuid.uuid);
145 ptlrpc_fail_export(req->rq_export);
149 ptlrpc_abort_bulk (desc);
151 for (i = 0; i < npages; i++)
153 __free_pages(pages[i], 0);
155 ptlrpc_free_bulk(desc);
157 OBD_FREE(pages, sizeof(*pages) * npages);
162 extern char *ldlm_lockname[];
164 int mds_lock_mode_for_dir(struct obd_device *obd,
165 struct dentry *dentry, int mode)
167 int ret_mode = 0, split;
169 /* any dir access needs couple locks:
170 * 1) on part of dir we gonna lookup/modify in
171 * 2) on a whole dir to protect it from concurrent splitting
172 * and to flush client's cache for readdir()
173 * so, for a given mode and dentry this routine decides what
174 * lock mode to use for lock #2:
175 * 1) if caller's gonna lookup in dir then we need to protect
176 * dir from being splitted only - LCK_CR
177 * 2) if caller's gonna modify dir then we need to protect
178 * dir from being splitted and to flush cache - LCK_CW
179 * 3) if caller's gonna modify dir and that dir seems ready
180 * for splitting then we need to protect it from any
181 * type of access (lookup/modify/split) - LCK_EX -bzzz */
183 split = mds_splitting_expected(obd, dentry);
186 * it is important to check here only for MDS_NO_SPLITTABLE. The reason
187 * is that MDS_NO_SPLITTABLE means dir is not splittable in principle
188 * and another thread will not split it on the quiet. But if we have
189 * MDS_NO_SPLIT_EXPECTED, this means, that dir may be splitted anytime,
190 * but not now (for current thread) and we should consider that it can
191 * happen soon and go that branch which can yield LCK_EX to protect from
192 * possible splitting.
194 if (split == MDS_NO_SPLITTABLE) {
196 * this inode won't be splitted. so we need not to protect from
197 * just flush client's cache on modification.
204 if (mode == LCK_EX) {
206 } else if (mode == LCK_PR) {
208 } else if (mode == LCK_PW) {
210 * caller gonna modify directory. We use concurrent
211 * write lock here to retract client's cache for
214 if (split == MDS_EXPECT_SPLIT) {
216 * splitting possible. serialize any access the
217 * idea is that first one seen dir is splittable
218 * is given exclusive lock and split
219 * directory. caller passes lock mode to
220 * mds_try_to_split_dir() and splitting would be
221 * done with exclusive lock only -bzzz.
223 CDEBUG(D_OTHER, "%s: gonna split %lu/%lu\n",
225 (unsigned long)dentry->d_inode->i_ino,
226 (unsigned long)dentry->d_inode->i_generation);
237 /* only valid locked dentries or errors should be returned */
238 struct dentry *mds_id2locked_dentry(struct obd_device *obd, struct lustre_id *id,
239 struct vfsmount **mnt, int lock_mode,
240 struct lustre_handle *lockh, int *mode,
241 char *name, int namelen, __u64 lockpart)
243 struct dentry *de = mds_id2dentry(obd, id, mnt), *retval = de;
244 ldlm_policy_data_t policy = { .l_inodebits = { lockpart } };
245 struct ldlm_res_id res_id = { .name = {0} };
253 res_id.name[0] = id_fid(id);
254 res_id.name[1] = id_group(id);
257 if (name && IS_PDIROPS(de->d_inode)) {
258 ldlm_policy_data_t cpolicy =
259 { .l_inodebits = { MDS_INODELOCK_UPDATE } };
260 LASSERT(mode != NULL);
261 *mode = mds_lock_mode_for_dir(obd, de, lock_mode);
263 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
265 &cpolicy, *mode, &flags,
267 ldlm_completion_ast, NULL, NULL,
268 NULL, 0, NULL, lockh + 1);
269 if (rc != ELDLM_OK) {
271 RETURN(ERR_PTR(-ENOLCK));
276 res_id.name[2] = full_name_hash((unsigned char *)name, namelen);
278 CDEBUG(D_INFO, "take lock on "DLID4":"LPX64"\n",
279 OLID4(id), res_id.name[2]);
282 #warning "No PDIROPS support in the kernel"
284 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace, res_id,
285 LDLM_IBITS, &policy, lock_mode, &flags,
286 mds_blocking_ast, ldlm_completion_ast,
287 NULL, NULL, NULL, 0, NULL, lockh);
288 if (rc != ELDLM_OK) {
290 retval = ERR_PTR(-EIO); /* XXX translate ldlm code */
293 ldlm_lock_decref(lockh + 1, *mode);
300 #ifndef DCACHE_DISCONNECTED
301 #define DCACHE_DISCONNECTED DCACHE_NFSD_DISCONNECTED
304 /* Look up an entry by inode number. This function ONLY returns valid dget'd
305 * dentries with an initialized inode or errors */
306 struct dentry *mds_id2dentry(struct obd_device *obd, struct lustre_id *id,
307 struct vfsmount **mnt)
309 unsigned long ino = (unsigned long)id_ino(id);
310 __u32 generation = (__u32)id_gen(id);
311 struct mds_obd *mds = &obd->u.mds;
312 struct dentry *result;
317 RETURN(ERR_PTR(-ESTALE));
319 snprintf(idname, sizeof(idname), "0x%lx", ino);
321 CDEBUG(D_DENTRY, "--> mds_id2dentry: ino/gen %lu/%u, sb %p\n",
322 ino, generation, mds->mds_sb);
324 /* under ext3 this is neither supposed to return bad inodes nor NULL
326 result = ll_lookup_one_len(idname, mds->mds_id_de,
331 inode = result->d_inode;
333 RETURN(ERR_PTR(-ENOENT));
335 if (is_bad_inode(inode)) {
336 CERROR("bad inode returned %lu/%u\n",
337 inode->i_ino, inode->i_generation);
339 RETURN(ERR_PTR(-ENOENT));
342 /* here we disabled generation check, as root inode i_generation
343 * of cache mds and real mds are different. */
344 if (inode->i_ino != id_ino(&mds->mds_rootid) && generation &&
345 inode->i_generation != generation) {
346 /* we didn't find the right inode.. */
347 CERROR("bad inode %lu, link: %lu, ct: %d, generation %u/%u\n",
348 inode->i_ino, (unsigned long)inode->i_nlink,
349 atomic_read(&inode->i_count), inode->i_generation,
352 RETURN(ERR_PTR(-ENOENT));
356 *mnt = mds->mds_vfsmnt;
364 int mds_req_add_idmapping(struct ptlrpc_request *req,
365 struct mds_export_data *med)
367 struct mds_req_sec_desc *rsd;
368 struct lustre_sec_desc *lsd;
371 if (!med->med_remote)
374 /* maybe we should do it more completely: invalidate the gss ctxt? */
375 if (req->rq_mapped_uid == MDS_IDMAP_NOTFOUND) {
376 CWARN("didn't find mapped uid\n");
380 rsd = lustre_swab_mds_secdesc(req, MDS_REQ_SECDESC_OFF);
382 CERROR("Can't unpack security desc\n");
386 lsd = mds_get_lsd(req->rq_mapped_uid);
388 CERROR("can't get LSD(%u), no mapping added\n",
393 rc = mds_idmap_add(med->med_idmap, rsd->rsd_uid, lsd->lsd_uid,
394 rsd->rsd_gid, lsd->lsd_gid);
400 int mds_req_del_idmapping(struct ptlrpc_request *req,
401 struct mds_export_data *med)
403 struct mds_req_sec_desc *rsd;
404 struct lustre_sec_desc *lsd;
407 if (!med->med_remote)
410 rsd = lustre_swab_mds_secdesc(req, MDS_REQ_SECDESC_OFF);
412 CERROR("Can't unpack security desc\n");
416 LASSERT(req->rq_mapped_uid != -1);
417 lsd = mds_get_lsd(req->rq_mapped_uid);
419 CERROR("can't get LSD(%u), no idmapping deleted\n",
424 rc = mds_idmap_del(med->med_idmap, rsd->rsd_uid, lsd->lsd_uid,
425 rsd->rsd_gid, lsd->lsd_gid);
430 static int mds_init_export_data(struct ptlrpc_request *req,
431 struct mds_export_data *med)
433 struct obd_connect_data *data, *reply;
434 int ask_remote, ask_local;
437 data = lustre_msg_buf(req->rq_reqmsg, 5, sizeof(*data));
438 reply = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*data));
439 LASSERT(data && reply);
441 if (med->med_initialized) {
442 CDEBUG(D_SEC, "med already initialized, reconnect?\n");
446 ask_remote = data->ocd_connect_flags & OBD_CONNECT_REMOTE;
447 ask_local = data->ocd_connect_flags & OBD_CONNECT_LOCAL;
449 /* currently the policy is simple: satisfy client as possible
452 if (req->rq_auth_uid == -1) {
454 CWARN("null sec is used, force to be local\n");
458 if (!req->rq_remote_realm)
459 CWARN("local realm asked to be remote\n");
461 } else if (ask_local) {
462 if (req->rq_remote_realm)
463 CWARN("remote realm asked to be local\n");
466 med->med_remote = (req->rq_remote_realm != 0);
469 med->med_nllu = data->ocd_nllu[0];
470 med->med_nllg = data->ocd_nllu[1];
472 med->med_initialized = 1;
474 reply->ocd_connect_flags &= ~(OBD_CONNECT_REMOTE | OBD_CONNECT_LOCAL);
475 if (med->med_remote) {
477 med->med_idmap = mds_idmap_alloc();
480 CERROR("Failed to alloc idmap, following request from "
481 "this client will be refused\n");
483 reply->ocd_connect_flags |= OBD_CONNECT_REMOTE;
484 CDEBUG(D_SEC, "set client as remote\n");
486 reply->ocd_connect_flags |= OBD_CONNECT_LOCAL;
487 CDEBUG(D_SEC, "set client as local\n");
493 static void mds_free_export_data(struct mds_export_data *med)
498 LASSERT(med->med_remote);
499 mds_idmap_free(med->med_idmap);
500 med->med_idmap = NULL;
503 /* Establish a connection to the MDS.
505 * This will set up an export structure for the client to hold state data about
506 * that client, like open files, the last operation number it did on the server,
509 static int mds_connect(struct lustre_handle *conn, struct obd_device *obd,
510 struct obd_uuid *cluuid, struct obd_connect_data *data,
513 struct mds_export_data *med;
514 struct mds_client_data *mcd;
515 struct obd_export *exp;
519 if (!conn || !obd || !cluuid)
522 /* XXX There is a small race between checking the list and adding a new
523 * connection for the same UUID, but the real threat (list corruption
524 * when multiple different clients connect) is solved.
526 * There is a second race between adding the export to the list, and
527 * filling in the client data below. Hence skipping the case of NULL
528 * mcd above. We should already be controlling multiple connects at the
529 * client, and we can't hold the spinlock over memory allocations
530 * without risk of deadlocking.
532 rc = class_connect(conn, obd, cluuid);
535 exp = class_conn2export(conn);
537 LASSERT(exp != NULL);
538 med = &exp->exp_mds_data;
540 OBD_ALLOC(mcd, sizeof(*mcd));
542 CERROR("%s: out of memory for client data.\n",
544 GOTO(out, rc = -ENOMEM);
547 memcpy(mcd->mcd_uuid, cluuid, sizeof(mcd->mcd_uuid));
550 rc = mds_client_add(obd, &obd->u.mds, med, -1);
558 OBD_FREE(mcd, sizeof(*mcd));
559 class_disconnect(exp, 0);
561 class_export_put(exp);
566 static int mds_connect_post(struct obd_export *exp, unsigned initial,
569 struct obd_device *obd = exp->exp_obd;
570 struct mds_obd *mds = &obd->u.mds;
571 struct mds_export_data *med;
572 struct mds_client_data *mcd;
576 med = &exp->exp_mds_data;
580 /* some one reconnect initially, we have to reset
581 * data existing export can have. bug 6102 */
582 if (mcd->mcd_last_xid != 0)
583 CDEBUG(D_HA, "initial reconnect to existing export\n");
584 mcd->mcd_last_transno = 0;
585 mcd->mcd_last_xid = 0;
586 mcd->mcd_last_result = 0;
587 mcd->mcd_last_data = 0;
590 if (!(flags & OBD_OPT_MDS_CONNECTION)) {
591 if (!(exp->exp_flags & OBD_OPT_REAL_CLIENT)) {
592 atomic_inc(&mds->mds_real_clients);
593 CDEBUG(D_OTHER,"%s: peer from %s is real client (%d)\n",
594 obd->obd_name, exp->exp_client_uuid.uuid,
595 atomic_read(&mds->mds_real_clients));
596 exp->exp_flags |= OBD_OPT_REAL_CLIENT;
598 if (mds->mds_md_name)
599 rc = mds_md_connect(obd, mds->mds_md_name);
604 static int mds_init_export(struct obd_export *exp)
606 struct mds_export_data *med = &exp->exp_mds_data;
608 INIT_LIST_HEAD(&med->med_open_head);
609 spin_lock_init(&med->med_open_lock);
613 static int mds_destroy_export(struct obd_export *export)
615 struct obd_device *obd = export->exp_obd;
616 struct mds_export_data *med = &export->exp_mds_data;
617 struct lvfs_run_ctxt saved;
621 mds_free_export_data(med);
622 target_destroy_export(export);
624 if (obd_uuid_equals(&export->exp_client_uuid, &obd->obd_uuid))
627 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
629 /* Close any open files (which may also cause orphan unlinking). */
630 spin_lock(&med->med_open_lock);
631 while (!list_empty(&med->med_open_head)) {
632 struct list_head *tmp = med->med_open_head.next;
633 struct mds_file_data *mfd =
634 list_entry(tmp, struct mds_file_data, mfd_list);
635 struct lustre_id sid;
637 BDEVNAME_DECLARE_STORAGE(btmp);
639 /* bug 1579: fix force-closing for 2.5 */
640 struct dentry *dentry = mfd->mfd_dentry;
642 list_del(&mfd->mfd_list);
643 spin_unlock(&med->med_open_lock);
645 down(&dentry->d_inode->i_sem);
646 rc = mds_read_inode_sid(obd, dentry->d_inode, &sid);
647 up(&dentry->d_inode->i_sem);
649 CERROR("Can't read inode self id, inode %lu, "
650 "rc %d\n", dentry->d_inode->i_ino, rc);
651 memset(&sid, 0, sizeof(sid));
654 /* If you change this message, be sure to update
655 * replay_single:test_46 */
656 CERROR("force closing client file handle for %.*s (%s:"
657 DLID4")\n", dentry->d_name.len, dentry->d_name.name,
658 ll_bdevname(dentry->d_inode->i_sb, btmp),
661 /* child inode->i_alloc_sem protects orphan_dec_test and
662 * is_orphan race, mds_mfd_close drops it */
663 DOWN_WRITE_I_ALLOC_SEM(dentry->d_inode);
664 rc = mds_mfd_close(NULL, 0, obd, mfd,
665 !(export->exp_flags & OBD_OPT_FAILOVER));
667 CDEBUG(D_INODE, "Error closing file: %d\n", rc);
668 spin_lock(&med->med_open_lock);
670 spin_unlock(&med->med_open_lock);
671 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
675 mds_client_free(export, !(export->exp_flags & OBD_OPT_FAILOVER));
679 static int mds_disconnect(struct obd_export *exp, unsigned long flags)
681 unsigned long irqflags;
682 struct obd_device *obd;
687 LASSERT(exp != NULL);
688 obd = class_exp2obd(exp);
690 CDEBUG(D_IOCTL, "invalid client cookie "LPX64"\n",
691 exp->exp_handle.h_cookie);
697 * suppress any inter-mds requests durring disconnecting lmv if this is
698 * detected --force mode. This is needed to avoid endless recovery.
700 if (atomic_read(&mds->mds_real_clients) > 0 &&
701 !(exp->exp_flags & OBD_OPT_REAL_CLIENT))
702 flags |= OBD_OPT_FORCE;
704 if (!(exp->exp_flags & OBD_OPT_REAL_CLIENT)
705 && !atomic_read(&mds->mds_real_clients)) {
706 /* there was no client at all */
707 mds_md_disconnect(obd, flags);
710 if ((exp->exp_flags & OBD_OPT_REAL_CLIENT)
711 && atomic_dec_and_test(&mds->mds_real_clients)) {
712 /* time to drop LMV connections */
713 CDEBUG(D_OTHER, "%s: last real client %s disconnected. "
714 "Disconnnect from LMV now\n",
715 obd->obd_name, exp->exp_client_uuid.uuid);
716 mds_md_disconnect(obd, flags);
719 spin_lock_irqsave(&exp->exp_lock, irqflags);
720 exp->exp_flags = flags;
721 spin_unlock_irqrestore(&exp->exp_lock, irqflags);
723 /* disconnect early so that clients can't keep using export */
724 rc = class_disconnect(exp, flags);
725 ldlm_cancel_locks_for_export(exp);
727 /* complete all outstanding replies */
728 spin_lock_irqsave(&exp->exp_lock, irqflags);
729 while (!list_empty(&exp->exp_outstanding_replies)) {
730 struct ptlrpc_reply_state *rs =
731 list_entry(exp->exp_outstanding_replies.next,
732 struct ptlrpc_reply_state, rs_exp_list);
733 struct ptlrpc_service *svc = rs->rs_srv_ni->sni_service;
735 spin_lock(&svc->srv_lock);
736 list_del_init(&rs->rs_exp_list);
737 ptlrpc_schedule_difficult_reply(rs);
738 spin_unlock(&svc->srv_lock);
740 spin_unlock_irqrestore(&exp->exp_lock, irqflags);
744 static int mds_getstatus(struct ptlrpc_request *req)
746 struct mds_obd *mds = mds_req2mds(req);
747 struct mds_body *body;
751 size = sizeof(*body);
753 rc = lustre_pack_reply(req, 1, &size, NULL);
754 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_GETSTATUS_PACK)) {
755 CERROR("mds: out of memory for message: size=%d\n", size);
756 req->rq_status = -ENOMEM; /* superfluous? */
760 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
761 body->valid |= OBD_MD_FID;
763 memcpy(&body->id1, &mds->mds_rootid, sizeof(body->id1));
766 * the last_committed and last_xid fields are filled in for all replies
767 * already - no need to do so here also.
772 int mds_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
773 void *data, int flag)
778 if (flag == LDLM_CB_CANCELING) {
779 /* Don't need to do anything here. */
783 /* XXX layering violation! -phil */
784 l_lock(&lock->l_resource->lr_namespace->ns_lock);
787 * get this: if mds_blocking_ast is racing with mds_intent_policy, such
788 * that mds_blocking_ast is called just before l_i_p takes the ns_lock,
789 * then by the time we get the lock, we might not be the correct
790 * blocking function anymore. So check, and return early, if so.
792 if (lock->l_blocking_ast != mds_blocking_ast) {
793 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
797 lock->l_flags |= LDLM_FL_CBPENDING;
798 do_ast = (!lock->l_readers && !lock->l_writers);
799 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
802 struct lustre_handle lockh;
805 LDLM_DEBUG(lock, "already unused, calling ldlm_cli_cancel");
806 ldlm_lock2handle(lock, &lockh);
807 rc = ldlm_cli_cancel(&lockh);
809 CERROR("ldlm_cli_cancel: %d\n", rc);
811 LDLM_DEBUG(lock, "Lock still has references, will be "
817 static int mds_convert_md(struct obd_device *obd, struct inode *inode,
818 void *md, int size, int mea)
822 if (S_ISREG(inode->i_mode)) {
823 rc = mds_convert_lov_ea(obd, inode, md, size);
824 } else if (S_ISDIR(inode->i_mode)) {
826 rc = mds_convert_mea_ea(obd, inode, md, size);
828 rc = mds_convert_lov_ea(obd, inode, md, size);
831 CERROR("Invalid EA format (nor LOV or MEA) "
832 "is detected. Inode %lu/%u\n",
833 inode->i_ino, inode->i_generation);
839 int mds_get_md(struct obd_device *obd, struct inode *inode,
840 void *md, int *size, int lock, int mea)
849 rc = fsfilt_get_md(obd, inode, md, *size,
850 (mea ? EA_MEA : EA_LOV));
852 CERROR("Error %d reading eadata for ino %lu\n",
856 rc = mds_convert_md(obd, inode, md,
872 /* Call with lock=1 if you want mds_pack_md to take the i_sem.
873 * Call with lock=0 if the caller has already taken the i_sem. */
874 int mds_pack_md(struct obd_device *obd, struct lustre_msg *msg, int offset,
875 struct mds_body *body, struct inode *inode, int lock, int mea)
877 struct mds_obd *mds = &obd->u.mds;
883 lmm = lustre_msg_buf(msg, offset, 0);
885 /* Some problem with getting eadata when I sized the reply
887 CDEBUG(D_INFO, "no space reserved for inode %lu MD\n",
891 lmm_size = msg->buflens[offset];
893 /* I don't really like this, but it is a sanity check on the client
894 * MD request. However, if the client doesn't know how much space
895 * to reserve for the MD, it shouldn't be bad to have too much space.
897 if (lmm_size > mds->mds_max_mdsize) {
898 CWARN("Reading MD for inode %lu of %d bytes > max %d\n",
899 inode->i_ino, lmm_size, mds->mds_max_mdsize);
903 rc = mds_get_md(obd, inode, lmm, &lmm_size, lock, mea);
905 if (S_ISDIR(inode->i_mode))
906 body->valid |= OBD_MD_FLDIREA;
908 body->valid |= OBD_MD_FLEASIZE;
911 body->valid |= OBD_MD_MEA;
913 body->eadatasize = lmm_size;
919 int mds_pack_link(struct dentry *dentry, struct ptlrpc_request *req,
920 struct mds_body *repbody, int reply_off)
922 struct inode *inode = dentry->d_inode;
927 symname = lustre_msg_buf(req->rq_repmsg, reply_off + 1,0);
928 LASSERT(symname != NULL);
929 len = req->rq_repmsg->buflens[reply_off + 1];
931 rc = inode->i_op->readlink(dentry, symname, len);
933 CERROR("readlink failed: %d\n", rc);
934 } else if (rc != len - 1) {
935 CERROR ("Unexpected readlink rc %d: expecting %d\n",
939 CDEBUG(D_INODE, "read symlink dest %s\n", symname);
940 repbody->valid |= OBD_MD_LINKNAME;
941 repbody->eadatasize = rc + 1;
942 symname[rc] = 0; /* NULL terminate */
949 int mds_pack_ea(struct dentry *dentry, struct ptlrpc_request *req,
950 struct mds_body *repbody, int req_off, int reply_off)
952 struct inode *inode = dentry->d_inode;
958 ea_name = lustre_msg_string(req->rq_reqmsg, req_off + 1, 0);
959 len = req->rq_repmsg->buflens[reply_off + 1];
961 value = lustre_msg_buf(req->rq_repmsg, reply_off + 1, len);
964 if (inode->i_op && inode->i_op->getxattr)
965 rc = inode->i_op->getxattr(dentry, ea_name, value, len);
968 if (rc != -ENODATA && rc != -EOPNOTSUPP)
969 CERROR("getxattr failed: %d", rc);
971 repbody->valid |= OBD_MD_FLEA;
972 repbody->eadatasize = rc;
979 int mds_pack_ealist(struct dentry *dentry, struct ptlrpc_request *req,
980 struct mds_body *repbody, int reply_off)
982 struct inode *inode = dentry->d_inode;
987 len = req->rq_repmsg->buflens[reply_off + 1];
989 value = lustre_msg_buf(req->rq_repmsg, reply_off + 1, len);
992 if (inode->i_op && inode->i_op->getxattr)
993 rc = inode->i_op->listxattr(dentry, value, len);
996 CERROR("listxattr failed: %d", rc);
998 repbody->valid |= OBD_MD_FLEALIST;
999 repbody->eadatasize = rc;
1005 int mds_pack_acl(struct obd_device *obd, struct lustre_msg *repmsg, int offset,
1006 struct mds_body *body, struct inode *inode)
1008 struct dentry de = { .d_inode = inode };
1009 __u32 buflen, *sizep;
1014 if (!inode->i_op->getxattr)
1017 buflen = repmsg->buflens[offset + 1];
1018 buf = lustre_msg_buf(repmsg, offset + 1, buflen);
1020 size = inode->i_op->getxattr(&de, XATTR_NAME_ACL_ACCESS, buf, buflen);
1021 if (size == -ENODATA || size == -EOPNOTSUPP)
1027 sizep = lustre_msg_buf(repmsg, offset, 4);
1029 CERROR("can't locate returned acl size buf\n");
1033 *sizep = cpu_to_le32(size);
1034 body->valid |= OBD_MD_FLACL_ACCESS;
1040 * here we take simple rule: once uid/fsuid is root, we also squash
1041 * the gid/fsgid, don't care setuid/setgid attributes.
1043 int mds_squash_root(struct mds_obd *mds, struct mds_req_sec_desc *rsd,
1046 if (!mds->mds_squash_uid || *peernid == mds->mds_nosquash_nid)
1049 if (rsd->rsd_uid && rsd->rsd_fsuid)
1052 CDEBUG(D_SEC, "squash req from "LPX64":"
1053 "(%u:%u-%u:%u/%x)=>(%u:%u-%u:%u/%x)\n", *peernid,
1054 rsd->rsd_uid, rsd->rsd_gid,
1055 rsd->rsd_fsuid, rsd->rsd_fsgid, rsd->rsd_cap,
1056 rsd->rsd_uid ? rsd->rsd_uid : mds->mds_squash_uid,
1057 rsd->rsd_uid ? rsd->rsd_gid : mds->mds_squash_gid,
1058 rsd->rsd_fsuid ? rsd->rsd_fsuid : mds->mds_squash_uid,
1059 rsd->rsd_fsuid ? rsd->rsd_fsgid : mds->mds_squash_gid,
1060 rsd->rsd_cap & ~CAP_FS_MASK);
1062 if (rsd->rsd_uid == 0) {
1063 rsd->rsd_uid = mds->mds_squash_uid;
1064 rsd->rsd_gid = mds->mds_squash_gid;
1066 if (rsd->rsd_fsuid == 0) {
1067 rsd->rsd_fsuid = mds->mds_squash_uid;
1068 rsd->rsd_fsgid = mds->mds_squash_gid;
1070 rsd->rsd_cap &= ~CAP_FS_MASK;
1075 static int mds_getattr_internal(struct obd_device *obd, struct dentry *dentry,
1076 struct ptlrpc_request *req, int req_off,
1077 struct mds_body *reqbody, int reply_off)
1079 struct mds_export_data *med = &req->rq_export->u.eu_mds_data;
1080 struct inode *inode = dentry->d_inode;
1081 struct mds_body *body;
1085 if (inode == NULL && !(dentry->d_flags & DCACHE_CROSS_REF))
1088 body = lustre_msg_buf(req->rq_repmsg, reply_off, sizeof(*body));
1089 LASSERT(body != NULL); /* caller prepped reply */
1091 if (dentry->d_flags & DCACHE_CROSS_REF) {
1092 mds_pack_dentry2body(obd, body, dentry,
1093 (reqbody->valid & OBD_MD_FID) ? 1 : 0);
1094 CDEBUG(D_OTHER, "cross reference: "DLID4"\n",
1099 mds_pack_inode2body(obd, body, inode,
1100 (reqbody->valid & OBD_MD_FID) ? 1 : 0);
1102 if ((S_ISREG(inode->i_mode) && (reqbody->valid & OBD_MD_FLEASIZE)) ||
1103 (S_ISDIR(inode->i_mode) && (reqbody->valid & OBD_MD_FLDIREA))) {
1105 /* guessing what kind og attribute do we need. */
1106 int is_mea = (S_ISDIR(inode->i_mode) &&
1107 (reqbody->valid & OBD_MD_MEA) != 0);
1109 rc = mds_pack_md(obd, req->rq_repmsg, reply_off + 1,
1110 body, inode, 1, is_mea);
1112 /* if we have LOV EA data, the OST holds size, atime, mtime. */
1113 if (!(body->valid & OBD_MD_FLEASIZE) &&
1114 !(body->valid & OBD_MD_FLDIREA))
1115 body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
1116 OBD_MD_FLATIME | OBD_MD_FLMTIME);
1117 } else if (S_ISLNK(inode->i_mode) &&
1118 (reqbody->valid & OBD_MD_LINKNAME) != 0) {
1119 rc = mds_pack_link(dentry, req, body, reply_off);
1120 } else if (reqbody->valid & OBD_MD_FLEA) {
1121 rc = mds_pack_ea(dentry, req, body, req_off, reply_off);
1122 } else if (reqbody->valid & OBD_MD_FLEALIST) {
1123 rc = mds_pack_ealist(dentry, req, body, reply_off);
1126 if (reqbody->valid & OBD_MD_FLACL_ACCESS) {
1127 int inc = (reqbody->valid & OBD_MD_FLEASIZE) ? 2 : 1;
1128 rc = mds_pack_acl(obd, req->rq_repmsg, reply_off + inc,
1133 mds_body_do_reverse_map(med, body);
1138 static int mds_getattr_pack_msg_cf(struct ptlrpc_request *req,
1139 struct dentry *dentry,
1142 int rc = 0, size[1] = {sizeof(struct mds_body)};
1145 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK)) {
1146 CERROR("failed MDS_GETATTR_PACK test\n");
1147 req->rq_status = -ENOMEM;
1151 rc = lustre_pack_reply(req, 1, size, NULL);
1153 CERROR("lustre_pack_reply failed: rc %d\n", rc);
1154 GOTO(out, req->rq_status = rc);
1162 static int mds_getattr_pack_msg(struct ptlrpc_request *req, struct dentry *de,
1165 struct inode *inode = de->d_inode;
1166 struct mds_obd *mds = mds_req2mds(req);
1167 struct mds_body *body;
1168 int rc = 0, size[4] = {sizeof(*body)}, bufcount = 1;
1171 body = lustre_msg_buf(req->rq_reqmsg, offset, sizeof(*body));
1172 LASSERT(body != NULL); /* checked by caller */
1173 LASSERT_REQSWABBED(req, offset); /* swabbed by caller */
1175 if ((S_ISREG(inode->i_mode) && (body->valid & OBD_MD_FLEASIZE)) ||
1176 (S_ISDIR(inode->i_mode) && (body->valid & OBD_MD_FLDIREA))) {
1179 down(&inode->i_sem);
1180 rc = fsfilt_get_md(req->rq_export->exp_obd, inode, NULL, 0,
1181 ((body->valid & OBD_MD_MEA) ? EA_MEA : EA_LOV));
1184 if (rc != -ENODATA && rc != -EOPNOTSUPP)
1185 CERROR("error getting inode %lu MD: rc = %d\n",
1188 } else if (rc > mds->mds_max_mdsize) {
1190 CERROR("MD size %d larger than maximum possible %u\n",
1191 rc, mds->mds_max_mdsize);
1193 size[bufcount] = rc;
1196 } else if (S_ISLNK(inode->i_mode) && (body->valid & OBD_MD_LINKNAME)) {
1197 if (inode->i_size + 1 != body->eadatasize)
1198 CERROR("symlink size: %Lu, reply space: %d\n",
1199 inode->i_size + 1, body->eadatasize);
1200 size[bufcount] = min_t(int, inode->i_size+1, body->eadatasize);
1202 CDEBUG(D_INODE, "symlink size: %Lu, reply space: %d\n",
1203 inode->i_size + 1, body->eadatasize);
1204 } else if ((body->valid & OBD_MD_FLEA)) {
1205 char *ea_name = lustre_msg_string(req->rq_reqmsg,
1208 if (inode->i_op && inode->i_op->getxattr)
1209 rc = inode->i_op->getxattr(de, ea_name, NULL, 0);
1212 if (rc != -ENODATA && rc != -EOPNOTSUPP)
1213 CERROR("error getting inode %lu EA: rc = %d\n",
1217 size[bufcount] = min_t(int, body->eadatasize, rc);
1220 } else if (body->valid & OBD_MD_FLEALIST) {
1222 if (inode->i_op && inode->i_op->getxattr)
1223 rc = inode->i_op->listxattr(de, NULL, 0);
1226 if (rc != -ENODATA && rc != -EOPNOTSUPP)
1227 CERROR("error getting inode %lu EA: rc = %d\n",
1231 size[bufcount] = min_t(int, body->eadatasize, rc);
1236 /* may co-exist with OBD_MD_FLEASIZE */
1237 if (body->valid & OBD_MD_FLACL_ACCESS) {
1238 size[bufcount++] = 4;
1239 size[bufcount++] = xattr_acl_size(LL_ACL_MAX_ENTRIES);
1242 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK)) {
1243 CERROR("failed MDS_GETATTR_PACK test\n");
1244 req->rq_status = -ENOMEM;
1245 GOTO(out, rc = -ENOMEM);
1248 rc = lustre_pack_reply(req, bufcount, size, NULL);
1250 CERROR("out of memory\n");
1251 GOTO(out, req->rq_status = rc);
1259 int mds_check_mds_num(struct obd_device *obd, struct inode *inode,
1260 char *name, int namelen)
1262 struct mea *mea = NULL;
1263 int mea_size, rc = 0;
1266 rc = mds_md_get_attr(obd, inode, &mea, &mea_size);
1271 * dir is already splitted, check if requested filename should
1272 * live at this MDS or at another one.
1274 int i = mea_name2idx(mea, name, namelen - 1);
1275 if (mea->mea_master != id_group(&mea->mea_ids[i])) {
1277 "inapropriate MDS(%d) for %s. should be "
1278 "%lu(%d)\n", mea->mea_master, name,
1279 (unsigned long)id_group(&mea->mea_ids[i]), i);
1285 OBD_FREE(mea, mea_size);
1289 static int mds_getattr_lock(struct ptlrpc_request *req, int offset,
1290 struct lustre_handle *child_lockh, int child_part)
1292 struct obd_device *obd = req->rq_export->exp_obd;
1293 struct mds_obd *mds = &obd->u.mds;
1294 struct ldlm_reply *rep = NULL;
1295 struct lvfs_run_ctxt saved;
1296 struct mds_req_sec_desc *rsd;
1297 struct mds_body *body;
1298 struct dentry *dparent = NULL, *dchild = NULL;
1299 struct lvfs_ucred uc = {NULL, NULL,};
1300 struct lustre_handle parent_lockh[2] = {{0}, {0}};
1301 unsigned int namesize;
1302 int rc = 0, cleanup_phase = 0, resent_req = 0, update_mode, reply_offset;
1306 LASSERT(!strcmp(obd->obd_type->typ_name, LUSTRE_MDS_NAME));
1307 MD_COUNTER_INCREMENT(obd, getattr_lock);
1309 rsd = lustre_swab_mds_secdesc(req, MDS_REQ_SECDESC_OFF);
1311 CERROR("Can't unpack security desc\n");
1315 /* swab now, before anyone looks inside the request. */
1316 body = lustre_swab_reqbuf(req, offset, sizeof(*body),
1317 lustre_swab_mds_body);
1319 CERROR("Can't swab mds_body\n");
1320 GOTO(cleanup, rc = -EFAULT);
1323 LASSERT_REQSWAB(req, offset + 1);
1324 name = lustre_msg_string(req->rq_reqmsg, offset + 1, 0);
1326 CERROR("Can't unpack name\n");
1327 GOTO(cleanup, rc = -EFAULT);
1329 namesize = req->rq_reqmsg->buflens[offset + 1];
1331 /* namesize less than 2 means we have empty name, probably came from
1332 revalidate by cfid, so no point in having name to be set */
1336 LASSERT (offset == 1 || offset == 3);
1338 rep = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*rep));
1344 rc = mds_init_ucred(&uc, req, rsd);
1349 push_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1350 cleanup_phase = 1; /* kernel context */
1351 intent_set_disposition(rep, DISP_LOOKUP_EXECD);
1353 LASSERT(namesize > 0);
1354 if (child_lockh->cookie != 0) {
1355 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT);
1359 if (body->valid == OBD_MD_FLID) {
1360 struct mds_body *mds_reply;
1361 int size = sizeof(*mds_reply);
1365 dparent = mds_id2dentry(obd, &body->id1, NULL);
1366 if (IS_ERR(dparent)) {
1367 rc = PTR_ERR(dparent);
1371 * the user requested ONLY the inode number, so do a raw lookup.
1373 rc = lustre_pack_reply(req, 1, &size, NULL);
1375 CERROR("out of memory\n");
1379 dir = dparent->d_inode;
1380 LASSERT(dir->i_op->lookup_raw != NULL);
1381 rc = dir->i_op->lookup_raw(dir, name, namesize - 1, &inum);
1383 mds_reply = lustre_msg_buf(req->rq_repmsg, 0,
1384 sizeof(*mds_reply));
1386 id_ino(&mds_reply->id1) = inum;
1387 mds_reply->valid = OBD_MD_FLID;
1391 if (resent_req == 0) {
1392 LASSERT(id_fid(&body->id1) != 0);
1394 rc = mds_get_parent_child_locked(obd, mds, &body->id1,
1395 parent_lockh, &dparent,
1397 MDS_INODELOCK_UPDATE,
1400 child_lockh, &dchild,
1401 LCK_PR, child_part);
1405 cleanup_phase = 2; /* dchild, dparent, locks */
1408 * let's make sure this name should leave on this mds
1411 rc = mds_check_mds_num(obd, dparent->d_inode, name, namesize);
1415 /* we have no dentry here, drop LOOKUP bit */
1416 /* FIXME: we need MDS_INODELOCK_LOOKUP or not. */
1417 child_part &= ~MDS_INODELOCK_LOOKUP;
1418 CDEBUG(D_OTHER, "%s: retrieve attrs for "DLID4"\n",
1419 obd->obd_name, OLID4(&body->id1));
1421 dchild = mds_id2locked_dentry(obd, &body->id1, NULL,
1422 LCK_PR, parent_lockh,
1425 MDS_INODELOCK_UPDATE);
1426 if (IS_ERR(dchild)) {
1427 CERROR("can't find inode with id "DLID4", err = %d\n",
1428 OLID4(&body->id1), (int)PTR_ERR(dchild));
1429 GOTO(cleanup, rc = PTR_ERR(dchild));
1431 memcpy(child_lockh, parent_lockh, sizeof(parent_lockh[0]));
1434 struct ldlm_lock *granted_lock;
1436 DEBUG_REQ(D_DLMTRACE, req, "resent, not enqueuing new locks");
1437 granted_lock = ldlm_handle2lock(child_lockh);
1439 LASSERTF(granted_lock != NULL, LPU64"/%lu lockh "LPX64"\n",
1440 id_fid(&body->id1), (unsigned long)id_group(&body->id1),
1441 child_lockh->cookie);
1444 /* usual named request */
1445 dparent = mds_id2dentry(obd, &body->id1, NULL);
1446 LASSERT(!IS_ERR(dparent));
1447 dchild = ll_lookup_one_len(name, dparent, namesize - 1);
1448 LASSERT(!IS_ERR(dchild));
1450 /* client wants to get attr. by id */
1451 dchild = mds_id2dentry(obd, &body->id1, NULL);
1452 LASSERT(!IS_ERR(dchild));
1454 LDLM_LOCK_PUT(granted_lock);
1457 cleanup_phase = 2; /* dchild, dparent, locks */
1459 if (!DENTRY_VALID(dchild)) {
1460 intent_set_disposition(rep, DISP_LOOKUP_NEG);
1462 * in the intent case, the policy clears this error: the
1463 * disposition is enough.
1468 intent_set_disposition(rep, DISP_LOOKUP_POS);
1471 if (req->rq_repmsg == NULL) {
1472 if (dchild->d_flags & DCACHE_CROSS_REF)
1473 rc = mds_getattr_pack_msg_cf(req, dchild, offset);
1475 rc = mds_getattr_pack_msg(req, dchild, offset);
1477 CERROR ("mds_getattr_pack_msg: %d\n", rc);
1482 rc = mds_getattr_internal(obd, dchild, req, offset, body, reply_offset);
1483 GOTO(cleanup, rc); /* returns the lock to the client */
1486 switch (cleanup_phase) {
1488 if (resent_req == 0) {
1489 if (rc && DENTRY_VALID(dchild))
1490 ldlm_lock_decref(child_lockh, LCK_PR);
1492 ldlm_lock_decref(parent_lockh, LCK_PR);
1494 if (parent_lockh[1].cookie != 0)
1495 ldlm_lock_decref(parent_lockh + 1, update_mode);
1502 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1504 mds_exit_ucred(&uc);
1509 static int mds_getattr(struct ptlrpc_request *req, int offset)
1511 struct obd_device *obd = req->rq_export->exp_obd;
1512 struct lvfs_run_ctxt saved;
1514 struct mds_req_sec_desc *rsd;
1515 struct mds_body *body;
1516 struct lvfs_ucred uc = {NULL, NULL,};
1520 MD_COUNTER_INCREMENT(obd, getattr);
1522 rsd = lustre_swab_mds_secdesc(req, MDS_REQ_SECDESC_OFF);
1524 CERROR("Can't unpack security desc\n");
1528 body = lustre_swab_reqbuf(req, offset, sizeof(*body),
1529 lustre_swab_mds_body);
1531 CERROR ("Can't unpack body\n");
1535 rc = mds_init_ucred(&uc, req, rsd);
1537 mds_exit_ucred(&uc);
1541 push_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1542 de = mds_id2dentry(obd, &body->id1, NULL);
1544 rc = req->rq_status = PTR_ERR(de);
1548 rc = mds_getattr_pack_msg(req, de, offset);
1550 CERROR("mds_getattr_pack_msg: %d\n", rc);
1554 req->rq_status = mds_getattr_internal(obd, de, req, offset, body, 0);
1559 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1560 mds_exit_ucred(&uc);
1564 static int mds_obd_statfs(struct obd_device *obd, struct obd_statfs *osfs,
1565 unsigned long max_age)
1570 spin_lock(&obd->obd_osfs_lock);
1571 rc = fsfilt_statfs(obd, obd->u.mds.mds_sb, max_age);
1573 memcpy(osfs, &obd->obd_osfs, sizeof(*osfs));
1574 spin_unlock(&obd->obd_osfs_lock);
1579 static int mds_statfs(struct ptlrpc_request *req)
1581 struct obd_device *obd = req->rq_export->exp_obd;
1582 int rc, size = sizeof(struct obd_statfs);
1585 /* This will trigger a watchdog timeout */
1586 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_STATFS_LCW_SLEEP,
1587 (MDS_SERVICE_WATCHDOG_TIMEOUT / 1000) + 1);
1589 rc = lustre_pack_reply(req, 1, &size, NULL);
1590 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_STATFS_PACK)) {
1591 CERROR("mds: statfs lustre_pack_reply failed: rc = %d\n", rc);
1595 OBD_COUNTER_INCREMENT(obd, statfs);
1597 /* We call this so that we can cache a bit - 1 jiffie worth */
1598 rc = mds_obd_statfs(obd, lustre_msg_buf(req->rq_repmsg, 0, size),
1601 CERROR("mds_obd_statfs failed: rc %d\n", rc);
1607 req->rq_status = rc;
1611 static int mds_sync(struct ptlrpc_request *req, int offset)
1613 struct obd_device *obd = req->rq_export->exp_obd;
1614 struct mds_obd *mds = &obd->u.mds;
1615 struct mds_body *body;
1616 int rc, size = sizeof(*body);
1619 body = lustre_swab_reqbuf(req, offset, sizeof(*body),
1620 lustre_swab_mds_body);
1622 GOTO(out, rc = -EPROTO);
1624 rc = lustre_pack_reply(req, 1, &size, NULL);
1625 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK)) {
1626 CERROR("fsync lustre_pack_reply failed: rc = %d\n", rc);
1630 if (id_ino(&body->id1) == 0) {
1631 /* an id of zero is taken to mean "sync whole filesystem" */
1632 rc = fsfilt_sync(obd, mds->mds_sb);
1636 /* just any file to grab fsync method - "file" arg unused */
1637 struct file *file = mds->mds_rcvd_filp;
1638 struct mds_body *rep_body;
1641 de = mds_id2dentry(obd, &body->id1, NULL);
1643 GOTO(out, rc = PTR_ERR(de));
1645 rc = file->f_op->fsync(NULL, de, 1);
1649 rep_body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*rep_body));
1650 mds_pack_inode2body(obd, rep_body, de->d_inode,
1651 (body->valid & OBD_MD_FID) ? 1 : 0);
1657 req->rq_status = rc;
1661 /* mds_readpage does not take a DLM lock on the inode, because the client must
1662 * already have a PR lock.
1664 * If we were to take another one here, a deadlock will result, if another
1665 * thread is already waiting for a PW lock. */
1666 static int mds_readpage(struct ptlrpc_request *req, int offset)
1668 struct obd_device *obd = req->rq_export->exp_obd;
1669 struct vfsmount *mnt;
1672 struct mds_req_sec_desc *rsd;
1673 struct mds_body *body, *repbody;
1674 struct lvfs_run_ctxt saved;
1675 int rc, size = sizeof(*repbody);
1676 struct lvfs_ucred uc = {NULL, NULL,};
1679 rc = lustre_pack_reply(req, 1, &size, NULL);
1680 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK)) {
1681 CERROR("mds: out of memory\n");
1682 GOTO(out, rc = -ENOMEM);
1685 rsd = lustre_swab_mds_secdesc(req, MDS_REQ_SECDESC_OFF);
1687 CERROR("Can't unpack security desc\n");
1688 GOTO (out, rc = -EFAULT);
1691 body = lustre_swab_reqbuf(req, offset, sizeof(*body),
1692 lustre_swab_mds_body);
1694 CERROR("Can't unpack body\n");
1695 GOTO (out, rc = -EFAULT);
1698 rc = mds_init_ucred(&uc, req, rsd);
1703 push_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1704 de = mds_id2dentry(obd, &body->id1, &mnt);
1706 GOTO(out_pop, rc = PTR_ERR(de));
1708 CDEBUG(D_INODE, "ino %lu\n", de->d_inode->i_ino);
1710 file = dentry_open(de, mnt, O_RDONLY | O_LARGEFILE);
1711 /* note: in case of an error, dentry_open puts dentry */
1713 GOTO(out_pop, rc = PTR_ERR(file));
1715 /* body->size is actually the offset -eeb */
1716 if ((body->size & (de->d_inode->i_blksize - 1)) != 0) {
1717 CERROR("offset "LPU64" not on a block boundary of %lu\n",
1718 body->size, de->d_inode->i_blksize);
1719 GOTO(out_file, rc = -EFAULT);
1722 /* body->nlink is actually the #bytes to read -eeb */
1723 if (body->nlink & (de->d_inode->i_blksize - 1)) {
1724 CERROR("size %u is not multiple of blocksize %lu\n",
1725 body->nlink, de->d_inode->i_blksize);
1726 GOTO(out_file, rc = -EFAULT);
1729 repbody = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*repbody));
1730 repbody->size = file->f_dentry->d_inode->i_size;
1731 repbody->valid = OBD_MD_FLSIZE;
1733 /* to make this asynchronous make sure that the handling function
1734 doesn't send a reply when this function completes. Instead a
1735 callback function would send the reply */
1736 /* body->size is actually the offset -eeb */
1737 rc = mds_sendpage(req, file, body->size, body->nlink);
1741 filp_close(file, 0);
1743 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1745 mds_exit_ucred(&uc);
1746 req->rq_status = rc;
1750 /* update master MDS ID, which is stored in local inode EA. */
1751 int mds_update_mid(struct obd_device *obd, struct lustre_id *id,
1752 void *data, int data_len)
1754 struct mds_obd *mds = &obd->u.mds;
1755 struct dentry *dentry;
1763 dentry = mds_id2dentry(obd, id, NULL);
1765 GOTO(out, rc = PTR_ERR(dentry));
1767 if (!dentry->d_inode) {
1768 CERROR("Can't find object "DLID4".\n",
1770 GOTO(out_dentry, rc = -EINVAL);
1773 handle = fsfilt_start(obd, dentry->d_inode,
1774 FSFILT_OP_SETATTR, NULL);
1776 GOTO(out_dentry, rc = PTR_ERR(handle));
1778 rc = mds_update_inode_mid(obd, dentry->d_inode, handle,
1779 (struct lustre_id *)data);
1781 CERROR("Can't update inode "DLID4" master id, "
1782 "error = %d.\n", OLID4(id), rc);
1783 GOTO(out_commit, rc);
1788 fsfilt_commit(obd, mds->mds_sb, dentry->d_inode,
1795 EXPORT_SYMBOL(mds_update_mid);
1797 /* read master MDS ID, which is stored in local inode EA. */
1798 int mds_read_mid(struct obd_device *obd, struct lustre_id *id,
1799 void *data, int data_len)
1801 struct dentry *dentry;
1808 dentry = mds_id2dentry(obd, id, NULL);
1810 GOTO(out, rc = PTR_ERR(dentry));
1812 if (!dentry->d_inode) {
1813 CERROR("Can't find object "DLID4".\n",
1815 GOTO(out_dentry, rc = -EINVAL);
1818 down(&dentry->d_inode->i_sem);
1819 rc = mds_read_inode_mid(obd, dentry->d_inode,
1820 (struct lustre_id *)data);
1821 up(&dentry->d_inode->i_sem);
1823 CERROR("Can't read inode "DLID4" master id, "
1824 "error = %d.\n", OLID4(id), rc);
1825 GOTO(out_dentry, rc);
1834 EXPORT_SYMBOL(mds_read_mid);
1836 int mds_read_md(struct obd_device *obd, struct lustre_id *id,
1837 char **data, int *datalen)
1839 struct dentry *dentry;
1840 struct mds_obd *mds = &obd->u.mds;
1841 int rc = 0, mea = 0;
1848 dentry = mds_id2dentry(obd, id, NULL);
1850 GOTO(out, rc = PTR_ERR(dentry));
1852 if (!dentry->d_inode) {
1853 CERROR("Can't find object "DLID4".\n",
1855 GOTO(out_dentry, rc = -EINVAL);
1857 if (S_ISDIR(dentry->d_inode->i_mode)) {
1858 *datalen = obd_packmd(mds->mds_md_exp, NULL, NULL);
1861 *datalen = obd_packmd(mds->mds_dt_exp, NULL, NULL);
1864 OBD_ALLOC(ea, *datalen);
1867 GOTO(out_dentry, rc = PTR_ERR(dentry));
1870 down(&dentry->d_inode->i_sem);
1871 rc = fsfilt_get_md(obd, dentry->d_inode, *data, *datalen,
1872 (mea ? EA_MEA : EA_LOV));
1873 up(&dentry->d_inode->i_sem);
1876 CERROR("Error %d reading eadata for ino %lu\n",
1877 rc, dentry->d_inode->i_ino);
1883 EXPORT_SYMBOL(mds_read_md);
1885 int mds_reint(struct ptlrpc_request *req, int offset,
1886 struct lustre_handle *lockh)
1888 struct mds_update_record *rec;
1889 struct mds_req_sec_desc *rsd;
1893 OBD_ALLOC(rec, sizeof(*rec));
1897 rsd = lustre_swab_mds_secdesc(req, MDS_REQ_SECDESC_OFF);
1899 CERROR("Can't unpack security desc\n");
1900 GOTO(out, rc = -EFAULT);
1903 rc = mds_update_unpack(req, offset, rec);
1904 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNPACK)) {
1905 CERROR("invalid record\n");
1906 GOTO(out, req->rq_status = -EINVAL);
1909 rc = mds_init_ucred(&rec->ur_uc, req, rsd);
1914 /* rc will be used to interrupt a for loop over multiple records */
1915 rc = mds_reint_rec(rec, offset, req, lockh);
1918 mds_exit_ucred(&rec->ur_uc);
1919 OBD_FREE(rec, sizeof(*rec));
1923 static int mds_filter_recovery_request(struct ptlrpc_request *req,
1924 struct obd_device *obd, int *process)
1926 switch (req->rq_reqmsg->opc) {
1927 case MDS_CONNECT: /* This will never get here, but for completeness. */
1928 case OST_CONNECT: /* This will never get here, but for completeness. */
1929 case MDS_DISCONNECT:
1930 case OST_DISCONNECT:
1935 case MDS_SYNC: /* used in unmounting */
1940 *process = target_queue_recovery_request(req, obd);
1944 DEBUG_REQ(D_ERROR, req, "not permitted during recovery");
1946 /* XXX what should we set rq_status to here? */
1947 req->rq_status = -EAGAIN;
1948 RETURN(ptlrpc_error(req));
1952 static char *reint_names[] = {
1953 [REINT_SETATTR] "setattr",
1954 [REINT_CREATE] "create",
1955 [REINT_LINK] "link",
1956 [REINT_UNLINK] "unlink",
1957 [REINT_RENAME] "rename",
1958 [REINT_OPEN] "open",
1961 #define FILTER_VALID_FLAGS (OBD_MD_FLTYPE | OBD_MD_FLMODE | OBD_MD_FLGENER | \
1962 OBD_MD_FLSIZE | OBD_MD_FLBLOCKS | OBD_MD_FLBLKSZ| \
1963 OBD_MD_FLATIME | OBD_MD_FLMTIME | OBD_MD_FLCTIME| \
1966 static void reconstruct_create(struct ptlrpc_request *req)
1968 struct mds_export_data *med = &req->rq_export->exp_mds_data;
1969 struct mds_client_data *mcd = med->med_mcd;
1970 struct dentry *dentry;
1971 struct ost_body *body;
1972 struct lustre_id id;
1976 /* copy rc, transno and disp; steal locks */
1977 mds_req_from_mcd(req, mcd);
1978 if (req->rq_status) {
1986 id_ino(&id) = mcd->mcd_last_data;
1987 LASSERT(id_ino(&id) != 0);
1989 dentry = mds_id2dentry(req2obd(req), &id, NULL);
1990 if (IS_ERR(dentry)) {
1991 CERROR("can't find inode "LPU64"\n", id_ino(&id));
1992 req->rq_status = PTR_ERR(dentry);
1997 CWARN("reconstruct reply for x"LPU64" (remote ino) "LPU64" -> %lu/%u\n",
1998 req->rq_xid, id_ino(&id), dentry->d_inode->i_ino,
1999 dentry->d_inode->i_generation);
2001 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
2002 obdo_from_inode(&body->oa, dentry->d_inode, FILTER_VALID_FLAGS);
2003 body->oa.o_id = dentry->d_inode->i_ino;
2004 body->oa.o_generation = dentry->d_inode->i_generation;
2005 body->oa.o_valid |= OBD_MD_FLID | OBD_MD_FLGENER;
2007 down(&dentry->d_inode->i_sem);
2008 rc = mds_read_inode_sid(req2obd(req), dentry->d_inode, &id);
2009 up(&dentry->d_inode->i_sem);
2011 CERROR("Can't read inode self id, inode %lu, "
2012 "rc %d\n", dentry->d_inode->i_ino, rc);
2016 body->oa.o_fid = id_fid(&id);
2017 body->oa.o_mds = id_group(&id);
2023 static int mds_inode_init_acl(struct obd_device *obd, void *handle,
2024 struct dentry *de, void *xattr, int xattr_size)
2026 struct inode *inode = de->d_inode;
2027 struct posix_acl *acl;
2034 LASSERT(xattr_size > 0);
2036 if (!inode->i_op->getxattr || !inode->i_op->setxattr) {
2037 CERROR("backend fs dosen't support xattr\n");
2041 /* set default acl */
2042 if (S_ISDIR(inode->i_mode)) {
2043 rc = inode->i_op->setxattr(de, XATTR_NAME_ACL_DEFAULT,
2044 xattr, xattr_size, 0);
2046 CERROR("set default acl err: %d\n", rc);
2051 /* set access acl */
2052 acl = posix_acl_from_xattr(xattr, xattr_size);
2053 if (acl == NULL || IS_ERR(acl)) {
2054 CERROR("insane attr data\n");
2055 return PTR_ERR(acl);
2058 if (posix_acl_valid(acl)) {
2059 CERROR("default acl not valid: %d\n", rc);
2064 mode = inode->i_mode;
2065 rc = posix_acl_create_masq(acl, &mode);
2067 CERROR("create masq err %d\n", rc);
2071 if (inode->i_mode != mode) {
2072 struct iattr iattr = { .ia_valid = ATTR_MODE,
2076 rc2 = fsfilt_setattr(obd, de, handle, &iattr, 0);
2078 CERROR("setattr mode err: %d\n", rc2);
2085 /* we didn't change acl except mode bits of some
2086 * entries, so should be fit into original size.
2088 rc = posix_acl_to_xattr(acl, xattr, xattr_size);
2091 rc = inode->i_op->setxattr(de, XATTR_NAME_ACL_ACCESS,
2092 xattr, xattr_size, 0);
2094 CERROR("set access acl err: %d\n", rc);
2097 posix_acl_release(acl);
2101 static int mdt_obj_create(struct ptlrpc_request *req)
2103 struct obd_device *obd = req->rq_export->exp_obd;
2104 struct mds_obd *mds = &obd->u.mds;
2105 struct ost_body *body, *repbody;
2108 char idname[LL_ID_NAMELEN];
2109 int size = sizeof(*repbody);
2110 struct inode *parent_inode;
2111 struct lvfs_run_ctxt saved;
2112 int rc, cleanup_phase = 0;
2113 struct dentry *new = NULL;
2114 struct dentry_params dp;
2115 int mealen, flags = 0;
2116 struct lvfs_ucred uc;
2117 struct lustre_id id;
2119 void *handle = NULL;
2120 unsigned long cr_inum = 0;
2123 DEBUG_REQ(D_HA, req, "create remote object");
2124 parent_inode = mds->mds_unnamed_dir->d_inode;
2126 body = lustre_swab_reqbuf(req, 0, sizeof(*body),
2127 lustre_swab_ost_body);
2131 /* acl data is packed transparently, no swab here */
2132 LASSERT(req->rq_reqmsg->bufcount >= 2);
2133 acl_size = req->rq_reqmsg->buflens[1];
2135 acl = lustre_msg_buf(req->rq_reqmsg, 1, acl_size);
2137 CERROR("No default acl buf?\n");
2142 rc = lustre_pack_reply(req, 1, &size, NULL);
2146 MDS_CHECK_RESENT(req, reconstruct_create(req));
2149 uc.luc_ginfo = NULL;
2150 uc.luc_uid = body->oa.o_uid;
2151 uc.luc_gid = body->oa.o_gid;
2152 uc.luc_fsuid = body->oa.o_uid;
2153 uc.luc_fsgid = body->oa.o_gid;
2155 push_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
2156 repbody = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*repbody));
2158 /* in REPLAY case inum should be given (client or other MDS fills it) */
2159 if (body->oa.o_id && ((body->oa.o_flags & OBD_FL_RECREATE_OBJS) ||
2160 (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY))) {
2162 * this is re-create request from MDS holding directory name.
2163 * we have to lookup given ino/gen first. if it exists (good
2164 * case) then there is nothing to do. if it does not then we
2165 * have to recreate it.
2167 id_ino(&id) = body->oa.o_id;
2168 id_gen(&id) = body->oa.o_generation;
2170 new = mds_id2dentry(obd, &id, NULL);
2171 if (!IS_ERR(new) && new->d_inode) {
2172 struct lustre_id sid;
2174 CWARN("mkdir() repairing is on its way: %lu/%lu\n",
2175 (unsigned long)id_ino(&id), (unsigned long)id_gen(&id));
2177 obdo_from_inode(&repbody->oa, new->d_inode,
2178 FILTER_VALID_FLAGS);
2180 repbody->oa.o_id = new->d_inode->i_ino;
2181 repbody->oa.o_generation = new->d_inode->i_generation;
2182 repbody->oa.o_valid |= OBD_MD_FLID | OBD_MD_FLGENER;
2185 down(&new->d_inode->i_sem);
2186 rc = mds_read_inode_sid(obd, new->d_inode, &sid);
2187 up(&new->d_inode->i_sem);
2189 CERROR("Can't read inode self id "
2190 "inode %lu, rc %d.\n",
2191 new->d_inode->i_ino, rc);
2195 repbody->oa.o_fid = id_fid(&sid);
2196 repbody->oa.o_mds = id_group(&sid);
2197 LASSERT(id_fid(&sid) != 0);
2200 * here we could use fid passed in body->oa.o_fid and
2201 * thus avoid mds_read_inode_sid().
2203 cr_inum = new->d_inode->i_ino;
2204 GOTO(cleanup, rc = 0);
2208 down(&parent_inode->i_sem);
2209 handle = fsfilt_start(obd, parent_inode, FSFILT_OP_MKDIR, NULL);
2210 if (IS_ERR(handle)) {
2211 up(&parent_inode->i_sem);
2212 CERROR("fsfilt_start() failed, rc = %d\n",
2213 (int)PTR_ERR(handle));
2214 GOTO(cleanup, rc = PTR_ERR(handle));
2216 cleanup_phase = 1; /* transaction */
2219 rc = sprintf(idname, "%u.%u", ll_insecure_random_int(), current->pid);
2220 new = lookup_one_len(idname, mds->mds_unnamed_dir, rc);
2222 CERROR("%s: can't lookup new inode (%s) for mkdir: %d\n",
2223 obd->obd_name, idname, (int) PTR_ERR(new));
2224 fsfilt_commit(obd, mds->mds_sb, new->d_inode, handle, 0);
2225 up(&parent_inode->i_sem);
2226 RETURN(PTR_ERR(new));
2227 } else if (new->d_inode) {
2228 CERROR("%s: name exists. repeat\n", obd->obd_name);
2232 new->d_fsdata = (void *)&dp;
2236 if ((lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) ||
2237 (body->oa.o_flags & OBD_FL_RECREATE_OBJS)) {
2238 LASSERT(body->oa.o_id != 0);
2239 dp.p_inum = body->oa.o_id;
2240 DEBUG_REQ(D_HA, req, "replay create obj %lu/%lu",
2241 (unsigned long)body->oa.o_id,
2242 (unsigned long)body->oa.o_generation);
2245 rc = vfs_mkdir(parent_inode, new, body->oa.o_mode);
2248 rc = mds_inode_init_acl(obd, handle, new,
2251 up(&parent_inode->i_sem);
2255 if ((body->oa.o_flags & OBD_FL_RECREATE_OBJS) ||
2256 lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
2257 new->d_inode->i_generation = body->oa.o_generation;
2258 mark_inode_dirty(new->d_inode);
2261 * avoiding asserts in cache flush case, as
2262 * @body->oa.o_id should be zero.
2264 if (body->oa.o_id) {
2265 LASSERTF(body->oa.o_id == new->d_inode->i_ino,
2266 "BUG 3550: failed to recreate obj "
2267 LPU64" -> %lu\n", body->oa.o_id,
2268 new->d_inode->i_ino);
2270 LASSERTF(body->oa.o_generation ==
2271 new->d_inode->i_generation,
2272 "BUG 3550: failed to recreate obj/gen "
2273 LPU64"/%u -> %lu/%u\n", body->oa.o_id,
2274 body->oa.o_generation,
2275 new->d_inode->i_ino,
2276 new->d_inode->i_generation);
2280 obdo_from_inode(&repbody->oa, new->d_inode, FILTER_VALID_FLAGS);
2281 repbody->oa.o_id = new->d_inode->i_ino;
2282 repbody->oa.o_generation = new->d_inode->i_generation;
2283 repbody->oa.o_valid |= OBD_MD_FLID | OBD_MD_FLGENER | OBD_MD_FID;
2285 if ((body->oa.o_flags & OBD_FL_RECREATE_OBJS) ||
2286 lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
2287 id_group(&id) = mds->mds_num;
2289 LASSERT(body->oa.o_fid != 0);
2290 id_fid(&id) = body->oa.o_fid;
2292 LASSERT(body->oa.o_id != 0);
2293 id_ino(&id) = repbody->oa.o_id;
2294 id_gen(&id) = repbody->oa.o_generation;
2296 down(&new->d_inode->i_sem);
2297 rc = mds_update_inode_sid(obd, new->d_inode, handle, &id);
2298 up(&new->d_inode->i_sem);
2301 * make sure, that fid is up-to-date.
2303 mds_set_last_fid(obd, id_fid(&id));
2306 * allocate new sid, as object is created from scratch
2307 * and this is not replay.
2309 down(&new->d_inode->i_sem);
2310 rc = mds_alloc_inode_sid(obd, new->d_inode, handle, &id);
2311 up(&new->d_inode->i_sem);
2314 CERROR("Can't update lustre ID for inode %lu, "
2315 "error = %d\n", new->d_inode->i_ino, rc);
2319 /* initializing o_fid after it is allocated. */
2320 repbody->oa.o_fid = id_fid(&id);
2321 repbody->oa.o_mds = id_group(&id);
2323 rc = fsfilt_del_dir_entry(obd, new);
2324 up(&parent_inode->i_sem);
2326 CERROR("can't remove name for object: %d\n", rc);
2330 cleanup_phase = 2; /* created directory object */
2332 CDEBUG(D_OTHER, "created dirobj: %lu/%lu mode %o\n",
2333 (unsigned long)new->d_inode->i_ino,
2334 (unsigned long)new->d_inode->i_generation,
2335 (unsigned)new->d_inode->i_mode);
2336 cr_inum = new->d_inode->i_ino;
2338 up(&parent_inode->i_sem);
2339 CERROR("%s: can't create dirobj: %d\n", obd->obd_name, rc);
2343 if (body->oa.o_valid & OBD_MD_FLID) {
2344 /* this is new object for splitted dir. We have to prevent
2345 * recursive splitting on it -bzzz */
2346 mealen = obd_size_diskmd(mds->mds_md_exp, NULL);
2348 OBD_ALLOC(mea, mealen);
2350 GOTO(cleanup, rc = -ENOMEM);
2352 mea->mea_magic = MEA_MAGIC_ALL_CHARS;
2353 mea->mea_master = 0;
2356 down(&new->d_inode->i_sem);
2357 rc = fsfilt_set_md(obd, new->d_inode, handle,
2358 mea, mealen, EA_MEA);
2359 up(&new->d_inode->i_sem);
2361 CERROR("fsfilt_set_md() failed, "
2364 OBD_FREE(mea, mealen);
2366 CDEBUG(D_OTHER, "%s: mark non-splittable %lu/%u - %d\n",
2367 obd->obd_name, new->d_inode->i_ino,
2368 new->d_inode->i_generation, flags);
2369 } else if (body->oa.o_easize) {
2370 /* we pass LCK_EX to split routine to signal that we have
2371 * exclusive access to the directory. simple because nobody
2372 * knows it already exists -bzzz */
2373 rc = mds_try_to_split_dir(obd, new, NULL,
2374 body->oa.o_easize, LCK_EX);
2376 CERROR("Can't split directory %lu, error = %d.\n",
2377 new->d_inode->i_ino, rc);
2385 switch (cleanup_phase) {
2386 case 2: /* object has been created, but we'll may want to replay it later */
2388 ptlrpc_require_repack(req);
2389 case 1: /* transaction */
2390 rc = mds_finish_transno(mds, parent_inode, handle,
2395 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
2399 static int mdt_get_info(struct ptlrpc_request *req)
2401 struct obd_export *exp = req->rq_export;
2406 key = lustre_msg_buf(req->rq_reqmsg, 0, 1);
2408 DEBUG_REQ(D_HA, req, "no get_info key");
2411 keylen = req->rq_reqmsg->buflens[0];
2413 if ((keylen < strlen("mdsize") || strcmp(key, "mdsize") != 0) &&
2414 (keylen < strlen("mdsnum") || strcmp(key, "mdsnum") != 0) &&
2415 (keylen < strlen("rootid") || strcmp(key, "rootid") != 0))
2418 if (keylen >= strlen("rootid") && !strcmp(key, "rootid")) {
2419 struct lustre_id *reply;
2420 int size = sizeof(*reply);
2422 rc = lustre_pack_reply(req, 1, &size, NULL);
2426 reply = lustre_msg_buf(req->rq_repmsg, 0, size);
2427 rc = obd_get_info(exp, keylen, key, (__u32 *)&size, reply);
2430 int size = sizeof(*reply);
2432 rc = lustre_pack_reply(req, 1, &size, NULL);
2436 reply = lustre_msg_buf(req->rq_repmsg, 0, size);
2437 rc = obd_get_info(exp, keylen, key, (__u32 *)&size, reply);
2440 req->rq_repmsg->status = 0;
2444 static int mds_set_info(struct obd_export *exp, __u32 keylen,
2445 void *key, __u32 vallen, void *val)
2447 struct obd_device *obd;
2448 struct mds_obd *mds;
2452 obd = class_exp2obd(exp);
2454 CDEBUG(D_IOCTL, "invalid client cookie "LPX64"\n",
2455 exp->exp_handle.h_cookie);
2460 if (keylen >= strlen("mds_type") &&
2461 memcmp(key, "mds_type", keylen) == 0) {
2465 CDEBUG(D_IOCTL, "set mds type to %x\n", *(int*)val);
2467 mds->mds_obd_type = *(int*)val;
2468 group = FILTER_GROUP_FIRST_MDS + mds->mds_obd_type;
2469 valsize = sizeof(group);
2471 /* mds number has been changed, so the corresponding obdfilter
2472 * exp need to be changed too. */
2473 rc = obd_set_info(mds->mds_dt_exp, strlen("mds_conn"),
2474 "mds_conn", valsize, &group);
2477 CDEBUG(D_IOCTL, "invalid key\n");
2481 static int mdt_set_info(struct ptlrpc_request *req)
2484 struct obd_export *exp = req->rq_export;
2485 int keylen, rc = 0, vallen;
2488 key = lustre_msg_buf(req->rq_reqmsg, 0, 1);
2490 DEBUG_REQ(D_HA, req, "no set_info key");
2493 keylen = req->rq_reqmsg->buflens[0];
2495 if (keylen == strlen("mds_type") &&
2496 memcmp(key, "mds_type", keylen) == 0) {
2497 rc = lustre_pack_reply(req, 0, NULL, NULL);
2501 val = lustre_msg_buf(req->rq_reqmsg, 1, 0);
2502 vallen = req->rq_reqmsg->buflens[1];
2504 rc = obd_set_info(exp, keylen, key, vallen, val);
2505 req->rq_repmsg->status = 0;
2508 CDEBUG(D_IOCTL, "invalid key\n");
2512 static void mds_revoke_export_locks(struct obd_export *exp)
2514 struct ldlm_namespace *ns = exp->exp_obd->obd_namespace;
2515 struct list_head *locklist = &exp->exp_ldlm_data.led_held_locks;
2516 struct ldlm_lock *lock, *next;
2517 struct ldlm_lock_desc desc;
2519 if (!exp->u.eu_mds_data.med_remote)
2523 l_lock(&ns->ns_lock);
2524 list_for_each_entry_safe(lock, next, locklist, l_export_chain) {
2525 if (lock->l_req_mode != lock->l_granted_mode)
2528 LASSERT(lock->l_resource);
2529 if (lock->l_resource->lr_type != LDLM_IBITS &&
2530 lock->l_resource->lr_type != LDLM_PLAIN)
2533 if (lock->l_flags & LDLM_FL_AST_SENT)
2536 lock->l_flags |= LDLM_FL_AST_SENT;
2538 /* the desc just pretend to exclusive */
2539 ldlm_lock2desc(lock, &desc);
2540 desc.l_req_mode = LCK_EX;
2541 desc.l_granted_mode = 0;
2543 lock->l_blocking_ast(lock, &desc, NULL, LDLM_CB_BLOCKING);
2545 l_unlock(&ns->ns_lock);
2549 static int mds_msg_check_version(struct lustre_msg *msg)
2555 case MDS_DISCONNECT:
2557 rc = lustre_msg_check_version(msg, LUSTRE_OBD_VERSION);
2559 CERROR("bad opc %u version %08x, expecting %08x\n",
2560 msg->opc, msg->version, LUSTRE_OBD_VERSION);
2565 case MDS_GETATTR_LOCK:
2569 case MDS_DONE_WRITING:
2572 rc = lustre_msg_check_version(msg, LUSTRE_MDS_VERSION);
2574 CERROR("bad opc %u version %08x, expecting %08x\n",
2575 msg->opc, msg->version, LUSTRE_MDS_VERSION);
2579 case LDLM_BL_CALLBACK:
2580 case LDLM_CP_CALLBACK:
2581 rc = lustre_msg_check_version(msg, LUSTRE_DLM_VERSION);
2583 CERROR("bad opc %u version %08x, expecting %08x\n",
2584 msg->opc, msg->version, LUSTRE_DLM_VERSION);
2586 case OBD_LOG_CANCEL:
2587 case LLOG_ORIGIN_HANDLE_OPEN:
2588 case LLOG_ORIGIN_HANDLE_NEXT_BLOCK:
2589 case LLOG_ORIGIN_HANDLE_PREV_BLOCK:
2590 case LLOG_ORIGIN_HANDLE_READ_HEADER:
2591 case LLOG_ORIGIN_HANDLE_CLOSE:
2593 rc = lustre_msg_check_version(msg, LUSTRE_LOG_VERSION);
2595 CERROR("bad opc %u version %08x, expecting %08x\n",
2596 msg->opc, msg->version, LUSTRE_LOG_VERSION);
2602 rc = lustre_msg_check_version(msg, LUSTRE_OBD_VERSION);
2604 CERROR("bad opc %u version %08x, expecting %08x\n",
2605 msg->opc, msg->version, LUSTRE_OBD_VERSION);
2608 case SEC_INIT_CONTINUE:
2613 CERROR("MDS unknown opcode %d\n", msg->opc);
2621 int mds_handle(struct ptlrpc_request *req)
2623 int should_process, fail = OBD_FAIL_MDS_ALL_REPLY_NET;
2624 struct obd_device *obd = NULL;
2625 struct mds_obd *mds = NULL; /* quell gcc overwarning */
2629 OBD_FAIL_RETURN(OBD_FAIL_MDS_ALL_REQUEST_NET | OBD_FAIL_ONCE, 0);
2631 rc = mds_msg_check_version(req->rq_reqmsg);
2633 CERROR("MDS drop mal-formed request\n");
2637 /* Security opc should NOT trigger any recovery events */
2638 if (req->rq_reqmsg->opc == SEC_INIT ||
2639 req->rq_reqmsg->opc == SEC_INIT_CONTINUE) {
2640 if (req->rq_export) {
2641 mds_req_add_idmapping(req,
2642 &req->rq_export->exp_mds_data);
2643 mds_revoke_export_locks(req->rq_export);
2646 } else if (req->rq_reqmsg->opc == SEC_FINI) {
2647 if (req->rq_export) {
2648 mds_req_del_idmapping(req,
2649 &req->rq_export->exp_mds_data);
2650 mds_revoke_export_locks(req->rq_export);
2655 LASSERT(current->journal_info == NULL);
2656 /* XXX identical to OST */
2657 if (req->rq_reqmsg->opc != MDS_CONNECT) {
2658 struct mds_export_data *med;
2661 if (req->rq_export == NULL) {
2662 CERROR("operation %d on unconnected MDS from %s\n",
2663 req->rq_reqmsg->opc,
2665 req->rq_status = -ENOTCONN;
2666 GOTO(out, rc = -ENOTCONN);
2669 med = &req->rq_export->exp_mds_data;
2670 obd = req->rq_export->exp_obd;
2673 /* sanity check: if the xid matches, the request must
2674 * be marked as a resent or replayed */
2675 if (req->rq_xid == le64_to_cpu(med->med_mcd->mcd_last_xid) ||
2676 req->rq_xid == le64_to_cpu(med->med_mcd->mcd_last_close_xid)) {
2677 LASSERTF(lustre_msg_get_flags(req->rq_reqmsg) &
2678 (MSG_RESENT | MSG_REPLAY),
2679 "rq_xid "LPU64" matches last_xid, "
2680 "expected RESENT flag\n",
2683 /* else: note the opposite is not always true; a
2684 * RESENT req after a failover will usually not match
2685 * the last_xid, since it was likely never
2686 * committed. A REPLAYed request will almost never
2687 * match the last xid, however it could for a
2688 * committed, but still retained, open. */
2690 spin_lock_bh(&obd->obd_processing_task_lock);
2691 recovering = obd->obd_recovering;
2692 spin_unlock_bh(&obd->obd_processing_task_lock);
2694 rc = mds_filter_recovery_request(req, obd,
2696 if (rc || should_process == 0) {
2698 } else if (should_process < 0) {
2699 req->rq_status = should_process;
2700 rc = ptlrpc_error(req);
2706 switch (req->rq_reqmsg->opc) {
2708 DEBUG_REQ(D_INODE, req, "connect");
2709 OBD_FAIL_RETURN(OBD_FAIL_MDS_CONNECT_NET, 0);
2710 rc = target_handle_connect(req);
2712 struct mds_export_data *med;
2714 LASSERT(req->rq_export);
2715 med = &req->rq_export->u.eu_mds_data;
2716 mds_init_export_data(req, med);
2717 mds_req_add_idmapping(req, med);
2719 /* Now that we have an export, set mds. */
2720 obd = req->rq_export->exp_obd;
2721 mds = mds_req2mds(req);
2725 case MDS_DISCONNECT:
2726 DEBUG_REQ(D_INODE, req, "disconnect");
2727 OBD_FAIL_RETURN(OBD_FAIL_MDS_DISCONNECT_NET, 0);
2728 rc = target_handle_disconnect(req);
2729 req->rq_status = rc; /* superfluous? */
2733 DEBUG_REQ(D_INODE, req, "getstatus");
2734 OBD_FAIL_RETURN(OBD_FAIL_MDS_GETSTATUS_NET, 0);
2735 rc = mds_getstatus(req);
2739 DEBUG_REQ(D_INODE, req, "getattr");
2740 OBD_FAIL_RETURN(OBD_FAIL_MDS_GETATTR_NET, 0);
2741 rc = mds_getattr(req, MDS_REQ_REC_OFF);
2744 case MDS_GETATTR_LOCK: {
2745 struct lustre_handle lockh;
2746 DEBUG_REQ(D_INODE, req, "getattr_lock");
2747 OBD_FAIL_RETURN(OBD_FAIL_MDS_GETATTR_LOCK_NET, 0);
2749 /* If this request gets a reconstructed reply, we won't be
2750 * acquiring any new locks in mds_getattr_lock, so we don't
2754 rc = mds_getattr_lock(req, MDS_REQ_REC_OFF, &lockh,
2755 MDS_INODELOCK_UPDATE);
2756 /* this non-intent call (from an ioctl) is special */
2757 req->rq_status = rc;
2758 if (rc == 0 && lockh.cookie)
2759 ldlm_lock_decref(&lockh, LCK_PR);
2763 DEBUG_REQ(D_INODE, req, "statfs");
2764 OBD_FAIL_RETURN(OBD_FAIL_MDS_STATFS_NET, 0);
2765 rc = mds_statfs(req);
2769 DEBUG_REQ(D_INODE, req, "readpage");
2770 OBD_FAIL_RETURN(OBD_FAIL_MDS_READPAGE_NET, 0);
2771 rc = mds_readpage(req, MDS_REQ_REC_OFF);
2773 if (OBD_FAIL_CHECK_ONCE(OBD_FAIL_MDS_SENDPAGE)) {
2774 if (req->rq_reply_state) {
2775 lustre_free_reply_state (req->rq_reply_state);
2776 req->rq_reply_state = NULL;
2783 __u32 *opcp = lustre_msg_buf(req->rq_reqmsg, MDS_REQ_REC_OFF,
2786 int size[3] = {sizeof(struct mds_body), mds->mds_max_mdsize,
2787 mds->mds_max_cookiesize};
2790 /* NB only peek inside req now; mds_reint() will swab it */
2792 CERROR ("Can't inspect opcode\n");
2797 if (lustre_msg_swabbed (req->rq_reqmsg))
2800 DEBUG_REQ(D_INODE, req, "reint %d (%s)", opc,
2801 (opc < sizeof(reint_names) / sizeof(reint_names[0]) ||
2802 reint_names[opc] == NULL) ? reint_names[opc] :
2805 OBD_FAIL_RETURN(OBD_FAIL_MDS_REINT_NET, 0);
2807 if (opc == REINT_UNLINK || opc == REINT_RENAME)
2809 else if (opc == REINT_OPEN)
2814 rc = lustre_pack_reply(req, bufcount, size, NULL);
2818 rc = mds_reint(req, MDS_REQ_REC_OFF, NULL);
2819 fail = OBD_FAIL_MDS_REINT_NET_REP;
2824 DEBUG_REQ(D_INODE, req, "close");
2825 OBD_FAIL_RETURN(OBD_FAIL_MDS_CLOSE_NET, 0);
2826 rc = mds_close(req, MDS_REQ_REC_OFF);
2829 case MDS_DONE_WRITING:
2830 DEBUG_REQ(D_INODE, req, "done_writing");
2831 OBD_FAIL_RETURN(OBD_FAIL_MDS_DONE_WRITING_NET, 0);
2832 rc = mds_done_writing(req, MDS_REQ_REC_OFF);
2836 DEBUG_REQ(D_INODE, req, "pin");
2837 OBD_FAIL_RETURN(OBD_FAIL_MDS_PIN_NET, 0);
2838 rc = mds_pin(req, MDS_REQ_REC_OFF);
2842 DEBUG_REQ(D_INODE, req, "sync");
2843 OBD_FAIL_RETURN(OBD_FAIL_MDS_SYNC_NET, 0);
2844 rc = mds_sync(req, MDS_REQ_REC_OFF);
2848 DEBUG_REQ(D_INODE, req, "ping");
2849 rc = target_handle_ping(req);
2852 case OBD_LOG_CANCEL:
2853 CDEBUG(D_INODE, "log cancel\n");
2854 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOG_CANCEL_NET, 0);
2855 rc = -ENOTSUPP; /* la la la */
2859 DEBUG_REQ(D_INODE, req, "enqueue");
2860 OBD_FAIL_RETURN(OBD_FAIL_LDLM_ENQUEUE, 0);
2861 rc = ldlm_handle_enqueue(req, ldlm_server_completion_ast,
2862 ldlm_server_blocking_ast, NULL);
2863 fail = OBD_FAIL_LDLM_REPLY;
2866 DEBUG_REQ(D_INODE, req, "convert");
2867 OBD_FAIL_RETURN(OBD_FAIL_LDLM_CONVERT, 0);
2868 rc = ldlm_handle_convert(req);
2870 case LDLM_BL_CALLBACK:
2871 case LDLM_CP_CALLBACK:
2872 DEBUG_REQ(D_INODE, req, "callback");
2873 CERROR("callbacks should not happen on MDS\n");
2875 OBD_FAIL_RETURN(OBD_FAIL_LDLM_BL_CALLBACK, 0);
2877 case LLOG_ORIGIN_HANDLE_OPEN:
2878 DEBUG_REQ(D_INODE, req, "llog_init");
2879 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
2880 rc = llog_origin_handle_open(req);
2882 case LLOG_ORIGIN_HANDLE_NEXT_BLOCK:
2883 DEBUG_REQ(D_INODE, req, "llog next block");
2884 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
2885 rc = llog_origin_handle_next_block(req);
2887 case LLOG_ORIGIN_HANDLE_PREV_BLOCK:
2888 DEBUG_REQ(D_INODE, req, "llog prev block");
2889 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
2890 rc = llog_origin_handle_prev_block(req);
2892 case LLOG_ORIGIN_HANDLE_READ_HEADER:
2893 DEBUG_REQ(D_INODE, req, "llog read header");
2894 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
2895 rc = llog_origin_handle_read_header(req);
2897 case LLOG_ORIGIN_HANDLE_CLOSE:
2898 DEBUG_REQ(D_INODE, req, "llog close");
2899 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
2900 rc = llog_origin_handle_close(req);
2903 DEBUG_REQ(D_INODE, req, "ost_create");
2904 rc = mdt_obj_create(req);
2907 DEBUG_REQ(D_INODE, req, "get_info");
2908 rc = mdt_get_info(req);
2911 DEBUG_REQ(D_INODE, req, "set_info");
2912 rc = mdt_set_info(req);
2915 CDEBUG(D_INODE, "write\n");
2916 OBD_FAIL_RETURN(OBD_FAIL_OST_BRW_NET, 0);
2917 rc = ost_brw_write(req, NULL);
2918 LASSERT(current->journal_info == NULL);
2919 /* mdt_brw sends its own replies */
2923 DEBUG_REQ(D_INODE, req, "llog catinfo");
2924 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
2925 rc = llog_catinfo(req);
2928 req->rq_status = -ENOTSUPP;
2929 rc = ptlrpc_error(req);
2933 LASSERT(current->journal_info == NULL);
2937 /* If we're DISCONNECTing, the mds_export_data is already freed */
2938 if (!rc && req->rq_reqmsg->opc != MDS_DISCONNECT) {
2939 struct mds_export_data *med = &req->rq_export->exp_mds_data;
2940 struct obd_device *obd = list_entry(mds, struct obd_device,
2942 req->rq_repmsg->last_xid =
2943 le64_to_cpu(med->med_mcd->mcd_last_xid);
2945 if (!obd->obd_no_transno) {
2946 req->rq_repmsg->last_committed =
2947 obd->obd_last_committed;
2949 DEBUG_REQ(D_IOCTL, req,
2950 "not sending last_committed update");
2952 CDEBUG(D_INFO, "last_transno "LPU64", last_committed "LPU64
2954 mds->mds_last_transno, obd->obd_last_committed,
2960 target_send_reply(req, rc, fail);
2964 /* Update the server data on disk. This stores the new mount_count and also the
2965 * last_rcvd value to disk. If we don't have a clean shutdown, then the server
2966 * last_rcvd value may be less than that of the clients. This will alert us
2967 * that we may need to do client recovery.
2969 * Also assumes for mds_last_transno that we are not modifying it (no locking).
2971 int mds_update_server_data(struct obd_device *obd, int force_sync)
2973 struct mds_obd *mds = &obd->u.mds;
2974 struct mds_server_data *msd = mds->mds_server_data;
2975 struct file *filp = mds->mds_rcvd_filp;
2976 struct lvfs_run_ctxt saved;
2981 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
2982 msd->msd_last_transno = cpu_to_le64(mds->mds_last_transno);
2984 CDEBUG(D_SUPER, "MDS mount_count is "LPU64", last_transno is "LPU64"\n",
2985 mds->mds_mount_count, mds->mds_last_transno);
2986 rc = fsfilt_write_record(obd, filp, msd, sizeof(*msd), &off, force_sync);
2988 CERROR("error writing MDS server data: rc = %d\n", rc);
2989 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
2994 /* saves last allocated fid counter to file. */
2995 int mds_update_last_fid(struct obd_device *obd, void *handle,
2998 struct mds_obd *mds = &obd->u.mds;
2999 struct file *filp = mds->mds_fid_filp;
3000 struct lvfs_run_ctxt saved;
3006 spin_lock(&mds->mds_last_fid_lock);
3007 last_fid = mds->mds_last_fid;
3008 spin_unlock(&mds->mds_last_fid_lock);
3010 CDEBUG(D_SUPER, "MDS last_fid is #"LPU64"\n",
3014 fsfilt_add_journal_cb(obd, mds->mds_sb, last_fid,
3015 handle, mds_commit_last_fid_cb,
3019 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
3020 rc = fsfilt_write_record(obd, filp, &last_fid, sizeof(last_fid),
3022 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
3025 CERROR("error writing MDS last_fid #"LPU64
3026 ", err = %d\n", last_fid, rc);
3030 CDEBUG(D_SUPER, "wrote fid #"LPU64" at idx "
3031 "%llu: err = %d\n", last_fid, off, rc);
3036 void mds_set_last_fid(struct obd_device *obd, __u64 fid)
3038 struct mds_obd *mds = &obd->u.mds;
3040 spin_lock(&mds->mds_last_fid_lock);
3041 if (fid > mds->mds_last_fid)
3042 mds->mds_last_fid = fid;
3043 spin_unlock(&mds->mds_last_fid_lock);
3046 void mds_commit_last_transno_cb(struct obd_device *obd,
3047 __u64 transno, void *data,
3050 obd_transno_commit_cb(obd, transno, error);
3053 void mds_commit_last_fid_cb(struct obd_device *obd,
3054 __u64 fid, void *data,
3058 CERROR("%s: fid "LPD64" commit error: %d\n",
3059 obd->obd_name, fid, error);
3063 CDEBUG(D_HA, "%s: fid "LPD64" committed\n",
3064 obd->obd_name, fid);
3067 __u64 mds_alloc_fid(struct obd_device *obd)
3069 struct mds_obd *mds = &obd->u.mds;
3072 spin_lock(&mds->mds_last_fid_lock);
3073 fid = ++mds->mds_last_fid;
3074 spin_unlock(&mds->mds_last_fid_lock);
3080 * allocates new lustre_id on passed @inode and saves it to inode EA.
3082 int mds_alloc_inode_sid(struct obd_device *obd, struct inode *inode,
3083 void *handle, struct lustre_id *id)
3085 struct mds_obd *mds = &obd->u.mds;
3086 int alloc = 0, rc = 0;
3089 LASSERT(obd != NULL);
3090 LASSERT(inode != NULL);
3093 OBD_ALLOC(id, sizeof(*id));
3099 id_group(id) = mds->mds_num;
3100 id_fid(id) = mds_alloc_fid(obd);
3101 id_ino(id) = inode->i_ino;
3102 id_gen(id) = inode->i_generation;
3103 id_type(id) = (S_IFMT & inode->i_mode);
3105 rc = mds_update_inode_sid(obd, inode, handle, id);
3107 CERROR("Can't update inode FID EA, "
3112 OBD_FREE(id, sizeof(*id));
3117 * reads inode self id from inode EA. Probably later this should be replaced by
3118 * caching inode self id to avoid raeding it every time it is needed.
3120 int mds_read_inode_sid(struct obd_device *obd, struct inode *inode,
3121 struct lustre_id *id)
3126 LASSERT(id != NULL);
3127 LASSERT(obd != NULL);
3128 LASSERT(inode != NULL);
3130 rc = fsfilt_get_md(obd, inode, &id->li_fid,
3131 sizeof(id->li_fid), EA_SID);
3133 CERROR("fsfilt_get_md() failed, "
3146 /* updates inode self id in EA. */
3147 int mds_update_inode_sid(struct obd_device *obd, struct inode *inode,
3148 void *handle, struct lustre_id *id)
3153 LASSERT(id != NULL);
3154 LASSERT(obd != NULL);
3155 LASSERT(inode != NULL);
3157 rc = fsfilt_set_md(obd, inode, handle, &id->li_fid,
3158 sizeof(id->li_fid), EA_SID);
3160 CERROR("fsfilt_set_md() failed, rc = %d\n", rc);
3168 * reads inode id on master MDS. This is usualy done by CMOBD to update requests
3169 * to master MDS by correct store cookie, needed to find inode on master MDS
3172 int mds_read_inode_mid(struct obd_device *obd, struct inode *inode,
3173 struct lustre_id *id)
3178 LASSERT(id != NULL);
3179 LASSERT(obd != NULL);
3180 LASSERT(inode != NULL);
3182 rc = fsfilt_get_md(obd, inode, id, sizeof(*id), EA_MID);
3184 CERROR("fsfilt_get_md() failed, rc = %d\n", rc);
3197 * updates master inode id. Usualy this is done by CMOBD after an inode is
3198 * created and relationship between cache MDS and master one should be
3201 int mds_update_inode_mid(struct obd_device *obd, struct inode *inode,
3202 void *handle, struct lustre_id *id)
3207 LASSERT(id != NULL);
3208 LASSERT(obd != NULL);
3209 LASSERT(inode != NULL);
3211 rc = fsfilt_set_md(obd, inode, handle, id,
3212 sizeof(*id), EA_MID);
3214 CERROR("fsfilt_set_md() failed, "
3222 /* mount the file system (secretly) */
3223 static int mds_setup(struct obd_device *obd, obd_count len, void *buf)
3225 struct lustre_cfg* lcfg = buf;
3226 struct mds_obd *mds = &obd->u.mds;
3227 struct lvfs_obd_ctxt *lvfs_ctxt = NULL;
3228 char *options = NULL;
3229 struct vfsmount *mnt;
3235 if (lcfg->lcfg_bufcount < 3)
3236 RETURN(rc = -EINVAL);
3238 if (LUSTRE_CFG_BUFLEN(lcfg, 1) == 0 || LUSTRE_CFG_BUFLEN(lcfg, 2) == 0)
3239 RETURN(rc = -EINVAL);
3241 obd->obd_fsops = fsfilt_get_ops(lustre_cfg_string(lcfg, 2));
3242 if (IS_ERR(obd->obd_fsops))
3243 RETURN(rc = PTR_ERR(obd->obd_fsops));
3245 mds->mds_max_mdsize = sizeof(struct lov_mds_md);
3247 page = __get_free_page(GFP_KERNEL);
3251 options = (char *)page;
3252 memset(options, 0, PAGE_SIZE);
3255 * here we use "iopen_nopriv" hardcoded, because it affects MDS utility
3256 * and the rest of options are passed by mount options. Probably this
3257 * should be moved to somewhere else like startup scripts or lconf. */
3258 sprintf(options, "iopen_nopriv");
3260 if (LUSTRE_CFG_BUFLEN(lcfg, 4) > 0 && lustre_cfg_buf(lcfg, 4))
3261 sprintf(options + strlen(options), ",%s",
3262 lustre_cfg_string(lcfg, 4));
3264 /* we have to know mdsnum before touching underlying fs -bzzz */
3265 atomic_set(&mds->mds_open_count, 0);
3266 sema_init(&mds->mds_md_sem, 1);
3267 mds->mds_md_connected = 0;
3268 mds->mds_md_name = NULL;
3270 if (LUSTRE_CFG_BUFLEN(lcfg, 5) > 0 && lustre_cfg_buf(lcfg, 5) &&
3271 strncmp(lustre_cfg_string(lcfg, 5), "dumb", LUSTRE_CFG_BUFLEN(lcfg, 5))) {
3274 generate_random_uuid(uuid);
3275 class_uuid_unparse(uuid, &mds->mds_md_uuid);
3277 OBD_ALLOC(mds->mds_md_name, LUSTRE_CFG_BUFLEN(lcfg, 5));
3278 if (mds->mds_md_name == NULL)
3279 RETURN(rc = -ENOMEM);
3281 memcpy(mds->mds_md_name, lustre_cfg_buf(lcfg, 5),
3282 LUSTRE_CFG_BUFLEN(lcfg, 5));
3284 CDEBUG(D_OTHER, "MDS: %s is master for %s\n",
3285 obd->obd_name, mds->mds_md_name);
3287 rc = mds_md_connect(obd, mds->mds_md_name);
3289 OBD_FREE(mds->mds_md_name, LUSTRE_CFG_BUFLEN(lcfg, 5));
3294 mds->mds_obd_type = MDS_MASTER_OBD;
3296 if (LUSTRE_CFG_BUFLEN(lcfg, 6) > 0 && lustre_cfg_buf(lcfg, 6) &&
3297 strncmp(lustre_cfg_string(lcfg, 6), "dumb",
3298 LUSTRE_CFG_BUFLEN(lcfg, 6))) {
3299 if (!memcmp(lustre_cfg_string(lcfg, 6), "master",
3300 strlen("master"))) {
3301 mds->mds_obd_type = MDS_MASTER_OBD;
3302 } else if (!memcmp(lustre_cfg_string(lcfg, 6), "cache",
3304 mds->mds_obd_type = MDS_CACHE_OBD;
3308 rc = lvfs_mount_fs(lustre_cfg_string(lcfg, 1),
3309 lustre_cfg_string(lcfg, 2),
3310 options, 0, &lvfs_ctxt);
3314 if (rc || !lvfs_ctxt) {
3315 CERROR("lvfs_mount_fs failed: rc = %d\n", rc);
3319 mnt = lvfs_ctxt->loc_mnt;
3320 mds->mds_lvfs_ctxt = lvfs_ctxt;
3321 ll_clear_rdonly(ll_sbdev(mnt->mnt_sb));
3323 CDEBUG(D_SUPER, "%s: mnt = %p\n", lustre_cfg_string(lcfg, 1), mnt);
3325 sema_init(&mds->mds_epoch_sem, 1);
3326 atomic_set(&mds->mds_real_clients, 0);
3327 spin_lock_init(&mds->mds_transno_lock);
3328 spin_lock_init(&mds->mds_last_fid_lock);
3329 sema_init(&mds->mds_orphan_recovery_sem, 1);
3330 mds->mds_max_cookiesize = sizeof(struct llog_cookie);
3332 sprintf(ns_name, "mds-%s", obd->obd_uuid.uuid);
3333 obd->obd_namespace = ldlm_namespace_new(ns_name, LDLM_NAMESPACE_SERVER);
3335 if (obd->obd_namespace == NULL) {
3336 mds_cleanup(obd, 0);
3337 GOTO(err_put, rc = -ENOMEM);
3339 ldlm_register_intent(obd->obd_namespace, mds_intent_policy);
3341 rc = mds_fs_setup(obd, mnt);
3343 CERROR("%s: MDS filesystem method init failed: rc = %d\n",
3348 rc = llog_start_commit_thread();
3354 if (LUSTRE_CFG_BUFLEN(lcfg, 3) > 0 && lustre_cfg_buf(lcfg, 3) &&
3355 strncmp(lustre_cfg_string(lcfg, 3), "dumb",
3356 LUSTRE_CFG_BUFLEN(lcfg, 3))) {
3359 generate_random_uuid(uuid);
3360 class_uuid_unparse(uuid, &mds->mds_dt_uuid);
3362 OBD_ALLOC(mds->mds_profile, LUSTRE_CFG_BUFLEN(lcfg, 3));
3363 if (mds->mds_profile == NULL)
3364 GOTO(err_fs, rc = -ENOMEM);
3366 strncpy(mds->mds_profile, lustre_cfg_string(lcfg, 3),
3367 LUSTRE_CFG_BUFLEN(lcfg, 3));
3371 * setup root dir and files ID dir if lmv already connected, or there is
3374 if (mds->mds_md_exp || (LUSTRE_CFG_BUFLEN(lcfg, 3) > 0 &&
3375 lustre_cfg_buf(lcfg, 3) &&
3376 strncmp(lustre_cfg_string(lcfg, 3), "dumb",
3377 LUSTRE_CFG_BUFLEN(lcfg, 3)))) {
3378 rc = mds_fs_setup_rootid(obd);
3382 rc = mds_fs_setup_virtid(obd);
3387 ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
3388 "mds_ldlm_client", &obd->obd_ldlm_client);
3389 obd->obd_replayable = 1;
3391 rc = mds_postsetup(obd);
3398 /* No extra cleanup needed for llog_init_commit_thread() */
3399 mds_fs_cleanup(obd, 0);
3401 ldlm_namespace_free(obd->obd_namespace, 0);
3402 obd->obd_namespace = NULL;
3405 lvfs_umount_fs(mds->mds_lvfs_ctxt);
3409 fsfilt_put_ops(obd->obd_fsops);
3413 static int mds_postsetup(struct obd_device *obd)
3415 struct mds_obd *mds = &obd->u.mds;
3419 rc = obd_llog_setup(obd, &obd->obd_llogs, LLOG_CONFIG_ORIG_CTXT,
3420 obd, 0, NULL, &llog_lvfs_ops);
3424 if (mds->mds_profile) {
3425 struct llog_ctxt *lgctxt;
3426 struct lvfs_run_ctxt saved;
3427 struct lustre_profile *lprof;
3428 struct config_llog_instance cfg;
3430 cfg.cfg_instance = NULL;
3431 cfg.cfg_uuid = mds->mds_dt_uuid;
3432 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
3434 lgctxt = llog_get_context(&obd->obd_llogs, LLOG_CONFIG_ORIG_CTXT);
3436 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
3437 GOTO(err_llog, rc = -EINVAL);
3440 rc = class_config_process_llog(lgctxt, mds->mds_profile, &cfg);
3441 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
3446 lprof = class_get_profile(mds->mds_profile);
3447 if (lprof == NULL) {
3448 CERROR("No profile found: %s\n", mds->mds_profile);
3449 GOTO(err_cleanup, rc = -ENOENT);
3451 rc = mds_dt_connect(obd, lprof->lp_lov);
3453 GOTO(err_cleanup, rc);
3455 rc = mds_md_postsetup(obd);
3457 GOTO(err_cleanup, rc);
3464 obd_llog_cleanup(llog_get_context(&obd->obd_llogs,
3465 LLOG_CONFIG_ORIG_CTXT));
3469 int mds_postrecov_common(struct obd_device *obd)
3471 struct mds_obd *mds = &obd->u.mds;
3472 struct llog_ctxt *ctxt;
3473 int rc, item = 0, valsize;
3477 LASSERT(!obd->obd_recovering);
3478 ctxt = llog_get_context(&obd->obd_llogs, LLOG_UNLINK_ORIG_CTXT);
3479 LASSERT(ctxt != NULL);
3481 /* set nextid first, so we are sure it happens */
3482 rc = mds_dt_set_nextid(obd);
3484 CERROR("%s: mds_dt_set_nextid() failed\n", obd->obd_name);
3488 /* clean PENDING dir */
3489 rc = mds_cleanup_orphans(obd);
3494 group = FILTER_GROUP_FIRST_MDS + mds->mds_num;
3495 valsize = sizeof(group);
3496 rc = obd_set_info(mds->mds_dt_exp, strlen("mds_conn"), "mds_conn",
3501 rc = llog_connect(ctxt, obd->u.mds.mds_dt_desc.ld_tgt_count,
3504 CERROR("%s: failed at llog_origin_connect: %d\n",
3509 /* remove the orphaned precreated objects */
3510 rc = mds_dt_clearorphans(mds, NULL /* all OSTs */);
3515 RETURN(rc < 0 ? rc : item);
3518 /* cleanup all llogging subsystems */
3519 rc = obd_llog_finish(obd, &obd->obd_llogs,
3520 mds->mds_dt_desc.ld_tgt_count);
3522 CERROR("%s: failed to cleanup llogging subsystems\n",
3527 int mds_postrecov(struct obd_device *obd)
3531 rc = mds_postrecov_common(obd);
3533 rc = mds_md_reconnect(obd);
3537 int mds_dt_clean(struct obd_device *obd)
3539 struct mds_obd *mds = &obd->u.mds;
3542 if (mds->mds_profile) {
3544 struct llog_ctxt *llctx;
3545 struct lvfs_run_ctxt saved;
3546 struct config_llog_instance cfg;
3547 int len = strlen(mds->mds_profile) + sizeof("-clean") + 1;
3549 OBD_ALLOC(cln_prof, len);
3550 sprintf(cln_prof, "%s-clean", mds->mds_profile);
3552 cfg.cfg_instance = NULL;
3553 cfg.cfg_uuid = mds->mds_dt_uuid;
3555 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
3556 llctx = llog_get_context(&obd->obd_llogs,
3557 LLOG_CONFIG_ORIG_CTXT);
3558 class_config_process_llog(llctx, cln_prof, &cfg);
3559 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
3561 OBD_FREE(cln_prof, len);
3562 OBD_FREE(mds->mds_profile, strlen(mds->mds_profile) + 1);
3563 mds->mds_profile = NULL;
3568 int mds_md_clean(struct obd_device *obd)
3570 struct mds_obd *mds = &obd->u.mds;
3573 if (mds->mds_md_name) {
3574 OBD_FREE(mds->mds_md_name, strlen(mds->mds_md_name) + 1);
3575 mds->mds_md_name = NULL;
3580 static int mds_precleanup(struct obd_device *obd, int flags)
3586 mds_dt_disconnect(obd, flags);
3588 obd_llog_cleanup(llog_get_context(&obd->obd_llogs, LLOG_CONFIG_ORIG_CTXT));
3592 extern void lgss_svc_cache_purge_all(void);
3593 static int mds_cleanup(struct obd_device *obd, int flags)
3595 struct mds_obd *mds = &obd->u.mds;
3598 if (mds->mds_sb == NULL)
3601 mds_update_server_data(obd, 1);
3602 mds_update_last_fid(obd, NULL, 1);
3604 if (mds->mds_dt_objids != NULL) {
3605 int size = mds->mds_dt_desc.ld_tgt_count *
3607 OBD_FREE(mds->mds_dt_objids, size);
3609 mds_fs_cleanup(obd, flags);
3613 /* 2 seems normal on mds, (may_umount() also expects 2
3614 fwiw), but we only see 1 at this point in obdfilter. */
3615 lvfs_umount_fs(mds->mds_lvfs_ctxt);
3619 ldlm_namespace_free(obd->obd_namespace, flags & OBD_OPT_FORCE);
3621 spin_lock_bh(&obd->obd_processing_task_lock);
3622 if (obd->obd_recovering) {
3623 target_cancel_recovery_timer(obd);
3624 obd->obd_recovering = 0;
3626 spin_unlock_bh(&obd->obd_processing_task_lock);
3629 fsfilt_put_ops(obd->obd_fsops);
3633 lgss_svc_cache_purge_all();
3636 spin_lock(&mds->mds_denylist_lock);
3637 while (!list_empty( &mds->mds_denylist ) ) {
3638 deny_sec_t *p_deny_sec = list_entry(mds->mds_denylist.next,
3640 list_del(&p_deny_sec->list);
3641 OBD_FREE(p_deny_sec, sizeof(*p_deny_sec));
3643 spin_unlock(&mds->mds_denylist_lock);
3644 if(mds->mds_mds_sec)
3645 OBD_FREE(mds->mds_mds_sec, strlen(mds->mds_mds_sec) + 1);
3646 if(mds->mds_ost_sec)
3647 OBD_FREE(mds->mds_ost_sec, strlen(mds->mds_ost_sec) + 1);
3652 static int set_security(const char *value, char **sec)
3656 if (!strcmp(value, "null") ||
3657 !strcmp(value, "krb5i") ||
3658 !strcmp(value, "krb5p")) {
3659 OBD_ALLOC(*sec, strlen(value) + 1);
3662 memcpy(*sec, value, strlen(value) + 1);
3664 CERROR("Unrecognized value, force use NULL\n");
3671 static int mds_process_config(struct obd_device *obd, obd_count len, void *buf)
3673 struct lustre_cfg *lcfg = buf;
3674 struct mds_obd *mds = &obd->u.mds;
3678 switch(lcfg->lcfg_command) {
3679 case LCFG_SET_SECURITY: {
3680 if ((LUSTRE_CFG_BUFLEN(lcfg, 1) == 0) ||
3681 (LUSTRE_CFG_BUFLEN(lcfg, 2) == 0))
3682 GOTO(out, rc = -EINVAL);
3684 if (!strcmp(lustre_cfg_string(lcfg, 1), "mds_sec"))
3685 rc = set_security(lustre_cfg_string(lcfg, 2),
3687 else if (!strcmp(lustre_cfg_string(lcfg, 1), "oss_sec"))
3688 rc = set_security(lustre_cfg_string(lcfg, 2),
3690 else if (!strcmp(lustre_cfg_string(lcfg, 1), "deny_sec")){
3691 spin_lock(&mds->mds_denylist_lock);
3692 rc = add_deny_security(lustre_cfg_string(lcfg, 2),
3693 &mds->mds_denylist);
3694 spin_unlock(&mds->mds_denylist_lock);
3696 CERROR("Unrecognized key\n");
3702 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
3703 GOTO(out, rc = -EINVAL);
3711 static void fixup_handle_for_resent_req(struct ptlrpc_request *req,
3713 struct ldlm_lock *new_lock,
3714 struct ldlm_lock **old_lock,
3715 struct lustre_handle *lockh)
3717 struct obd_export *exp = req->rq_export;
3718 struct obd_device *obd = exp->exp_obd;
3719 struct ldlm_request *dlmreq =
3720 lustre_msg_buf(req->rq_reqmsg, offset, sizeof (*dlmreq));
3721 struct lustre_handle remote_hdl = dlmreq->lock_handle1;
3722 struct list_head *iter;
3724 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
3727 l_lock(&obd->obd_namespace->ns_lock);
3728 list_for_each(iter, &exp->exp_ldlm_data.led_held_locks) {
3729 struct ldlm_lock *lock;
3730 lock = list_entry(iter, struct ldlm_lock, l_export_chain);
3731 if (lock == new_lock)
3733 if (lock->l_remote_handle.cookie == remote_hdl.cookie) {
3734 lockh->cookie = lock->l_handle.h_cookie;
3735 LDLM_DEBUG(lock, "restoring lock cookie");
3736 DEBUG_REQ(D_HA, req, "restoring lock cookie "LPX64,
3739 *old_lock = LDLM_LOCK_GET(lock);
3740 l_unlock(&obd->obd_namespace->ns_lock);
3744 l_unlock(&obd->obd_namespace->ns_lock);
3746 /* If the xid matches, then we know this is a resent request,
3747 * and allow it. (It's probably an OPEN, for which we don't
3750 le64_to_cpu(exp->exp_mds_data.med_mcd->mcd_last_xid))
3754 le64_to_cpu(exp->exp_mds_data.med_mcd->mcd_last_close_xid))
3757 /* This remote handle isn't enqueued, so we never received or
3758 * processed this request. Clear MSG_RESENT, because it can
3759 * be handled like any normal request now. */
3761 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
3763 DEBUG_REQ(D_HA, req, "no existing lock with rhandle "LPX64,
3767 int intent_disposition(struct ldlm_reply *rep, int flag)
3771 return (rep->lock_policy_res1 & flag);
3774 void intent_set_disposition(struct ldlm_reply *rep, int flag)
3778 rep->lock_policy_res1 |= flag;
3781 static int mds_intent_policy(struct ldlm_namespace *ns,
3782 struct ldlm_lock **lockp, void *req_cookie,
3783 ldlm_mode_t mode, int flags, void *data)
3785 struct ptlrpc_request *req = req_cookie;
3786 struct ldlm_lock *lock = *lockp;
3787 struct ldlm_intent *it;
3788 struct mds_obd *mds = &req->rq_export->exp_obd->u.mds;
3789 struct ldlm_reply *rep;
3790 struct lustre_handle lockh[2] = {{0}, {0}};
3791 struct ldlm_lock *new_lock = NULL;
3792 int getattr_part = MDS_INODELOCK_UPDATE;
3793 int rc, reply_buffers;
3794 int repsize[5] = {sizeof(struct ldlm_reply),
3795 sizeof(struct mds_body),
3796 mds->mds_max_mdsize};
3798 int offset = MDS_REQ_INTENT_REC_OFF;
3801 LASSERT(req != NULL);
3802 MD_COUNTER_INCREMENT(req->rq_export->exp_obd, intent_lock);
3804 if (req->rq_reqmsg->bufcount <= MDS_REQ_INTENT_IT_OFF) {
3805 /* No intent was provided */
3806 int size = sizeof(struct ldlm_reply);
3807 rc = lustre_pack_reply(req, 1, &size, NULL);
3812 it = lustre_swab_reqbuf(req, MDS_REQ_INTENT_IT_OFF, sizeof(*it),
3813 lustre_swab_ldlm_intent);
3815 CERROR("Intent missing\n");
3816 RETURN(req->rq_status = -EFAULT);
3819 LDLM_DEBUG(lock, "intent policy, opc: %s", ldlm_it2str(it->opc));
3822 if (it->opc & ( IT_OPEN | IT_GETATTR | IT_LOOKUP | IT_CHDIR )) {
3825 repsize[4] = xattr_acl_size(LL_ACL_MAX_ENTRIES);
3828 rc = lustre_pack_reply(req, reply_buffers, repsize, NULL);
3830 RETURN(req->rq_status = rc);
3832 rep = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*rep));
3833 LASSERT(rep != NULL);
3835 intent_set_disposition(rep, DISP_IT_EXECD);
3837 /* execute policy */
3838 switch ((long)it->opc) {
3840 case IT_CREAT|IT_OPEN:
3841 fixup_handle_for_resent_req(req, MDS_REQ_INTENT_LOCKREQ_OFF,
3843 /* XXX swab here to assert that an mds_open reint
3844 * packet is following */
3845 fixup_handle_for_resent_req(req, MDS_REQ_INTENT_LOCKREQ_OFF,
3847 rep->lock_policy_res2 = mds_reint(req, offset, lockh);
3849 if (rep->lock_policy_res2) {
3851 * mds_open() returns ENOLCK where it should return
3852 * zero, but it has no lock to return.
3854 if (rep->lock_policy_res2 == ENOLCK)
3855 rep->lock_policy_res2 = 0;
3857 RETURN(ELDLM_LOCK_ABORTED);
3861 * IT_OPEN may return lock on cross-node dentry that we want to
3862 * hold during attr retrival -bzzz
3864 if (lockh[0].cookie == 0)
3865 RETURN(ELDLM_LOCK_ABORTED);
3869 getattr_part = MDS_INODELOCK_LOOKUP;
3872 getattr_part |= MDS_INODELOCK_LOOKUP;
3874 fixup_handle_for_resent_req(req, MDS_REQ_INTENT_LOCKREQ_OFF,
3875 lock, &new_lock, lockh);
3876 rep->lock_policy_res2 = mds_getattr_lock(req, offset, lockh,
3878 /* FIXME: LDLM can set req->rq_status. MDS sets
3879 policy_res{1,2} with disposition and status.
3880 - replay: returns 0 & req->status is old status
3881 - otherwise: returns req->status */
3882 if (intent_disposition(rep, DISP_LOOKUP_NEG))
3883 rep->lock_policy_res2 = 0;
3884 if (!intent_disposition(rep, DISP_LOOKUP_POS) ||
3885 rep->lock_policy_res2)
3886 RETURN(ELDLM_LOCK_ABORTED);
3887 if (req->rq_status != 0) {
3889 rep->lock_policy_res2 = req->rq_status;
3890 RETURN(ELDLM_LOCK_ABORTED);
3894 rc = mds_lock_and_check_slave(offset, req, lockh);
3895 if ((rep->lock_policy_res2 = rc)) {
3897 rep->lock_policy_res2 = 0;
3898 RETURN(ELDLM_LOCK_ABORTED);
3902 CERROR("Unhandled intent "LPD64"\n", it->opc);
3906 /* By this point, whatever function we called above must have either
3907 * filled in 'lockh', been an intent replay, or returned an error. We
3908 * want to allow replayed RPCs to not get a lock, since we would just
3909 * drop it below anyways because lock replay is done separately by the
3910 * client afterwards. For regular RPCs we want to give the new lock to
3911 * the client instead of whatever lock it was about to get. */
3912 if (new_lock == NULL)
3913 new_lock = ldlm_handle2lock(&lockh[0]);
3914 if (new_lock == NULL && (flags & LDLM_FL_INTENT_ONLY))
3917 LASSERTF(new_lock != NULL, "op "LPX64" lockh "LPX64"\n",
3918 it->opc, lockh[0].cookie);
3920 /* If we've already given this lock to a client once, then we should
3921 * have no readers or writers. Otherwise, we should have one reader
3922 * _or_ writer ref (which will be zeroed below) before returning the
3923 * lock to a client. */
3924 if (new_lock->l_export == req->rq_export) {
3925 LASSERT(new_lock->l_readers + new_lock->l_writers == 0);
3927 LASSERT(new_lock->l_export == NULL);
3928 LASSERT(new_lock->l_readers + new_lock->l_writers == 1);
3933 if (new_lock->l_export == req->rq_export) {
3934 /* Already gave this to the client, which means that we
3935 * reconstructed a reply. */
3936 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
3938 RETURN(ELDLM_LOCK_REPLACED);
3941 /* Fixup the lock to be given to the client */
3942 l_lock(&new_lock->l_resource->lr_namespace->ns_lock);
3943 new_lock->l_readers = 0;
3944 new_lock->l_writers = 0;
3946 new_lock->l_export = class_export_get(req->rq_export);
3947 list_add(&new_lock->l_export_chain,
3948 &new_lock->l_export->exp_ldlm_data.led_held_locks);
3950 new_lock->l_blocking_ast = lock->l_blocking_ast;
3951 new_lock->l_completion_ast = lock->l_completion_ast;
3953 memcpy(&new_lock->l_remote_handle, &lock->l_remote_handle,
3954 sizeof(lock->l_remote_handle));
3956 new_lock->l_flags &= ~LDLM_FL_LOCAL;
3958 LDLM_LOCK_PUT(new_lock);
3959 l_unlock(&new_lock->l_resource->lr_namespace->ns_lock);
3961 RETURN(ELDLM_LOCK_REPLACED);
3964 int mds_attach(struct obd_device *dev, obd_count len, void *data)
3966 struct lprocfs_static_vars lvars;
3968 struct mds_obd *mds = &dev->u.mds;
3970 spin_lock_init(&mds->mds_denylist_lock);
3971 INIT_LIST_HEAD(&mds->mds_denylist);
3973 lprocfs_init_multi_vars(0, &lvars);
3975 rc = lprocfs_obd_attach(dev, lvars.obd_vars);
3979 return lprocfs_alloc_md_stats(dev, 0);
3982 int mds_detach(struct obd_device *dev)
3984 lprocfs_free_md_stats(dev);
3985 return lprocfs_obd_detach(dev);
3988 int mdt_attach(struct obd_device *dev, obd_count len, void *data)
3990 struct lprocfs_static_vars lvars;
3992 lprocfs_init_multi_vars(1, &lvars);
3993 return lprocfs_obd_attach(dev, lvars.obd_vars);
3996 int mdt_detach(struct obd_device *dev)
3998 return lprocfs_obd_detach(dev);
4001 static int mdt_setup(struct obd_device *obd, obd_count len, void *buf)
4003 struct mds_obd *mds = &obd->u.mds;
4008 ptlrpc_init_svc(MDS_NBUFS, MDS_BUFSIZE, MDS_MAXREQSIZE,
4009 MDS_REQUEST_PORTAL, MDC_REPLY_PORTAL,
4010 MDS_SERVICE_WATCHDOG_TIMEOUT,
4011 mds_handle, "mds", obd->obd_proc_entry);
4013 if (!mds->mds_service) {
4014 CERROR("failed to start service\n");
4018 rc = ptlrpc_start_n_threads(obd, mds->mds_service, MDT_NUM_THREADS,
4021 GOTO(err_thread, rc);
4023 mds->mds_setattr_service =
4024 ptlrpc_init_svc(MDS_NBUFS, MDS_BUFSIZE, MDS_MAXREQSIZE,
4025 MDS_SETATTR_PORTAL, MDC_REPLY_PORTAL,
4026 MDS_SERVICE_WATCHDOG_TIMEOUT,
4027 mds_handle, "mds_setattr",
4028 obd->obd_proc_entry);
4029 if (!mds->mds_setattr_service) {
4030 CERROR("failed to start getattr service\n");
4031 GOTO(err_thread, rc = -ENOMEM);
4034 rc = ptlrpc_start_n_threads(obd, mds->mds_setattr_service,
4035 MDT_NUM_THREADS, "ll_mdt_attr");
4037 GOTO(err_thread2, rc);
4039 mds->mds_readpage_service =
4040 ptlrpc_init_svc(MDS_NBUFS, MDS_BUFSIZE, MDS_MAXREQSIZE,
4041 MDS_READPAGE_PORTAL, MDC_REPLY_PORTAL,
4042 MDS_SERVICE_WATCHDOG_TIMEOUT,
4043 mds_handle, "mds_readpage",
4044 obd->obd_proc_entry);
4045 if (!mds->mds_readpage_service) {
4046 CERROR("failed to start readpage service\n");
4047 GOTO(err_thread2, rc = -ENOMEM);
4050 rc = ptlrpc_start_n_threads(obd, mds->mds_readpage_service,
4051 MDT_NUM_THREADS, "ll_mdt_rdpg");
4054 GOTO(err_thread3, rc);
4059 ptlrpc_unregister_service(mds->mds_readpage_service);
4061 ptlrpc_unregister_service(mds->mds_setattr_service);
4063 ptlrpc_unregister_service(mds->mds_service);
4067 static int mdt_cleanup(struct obd_device *obd, int flags)
4069 struct mds_obd *mds = &obd->u.mds;
4072 ptlrpc_stop_all_threads(mds->mds_readpage_service);
4073 ptlrpc_unregister_service(mds->mds_readpage_service);
4075 ptlrpc_stop_all_threads(mds->mds_setattr_service);
4076 ptlrpc_unregister_service(mds->mds_setattr_service);
4078 ptlrpc_stop_all_threads(mds->mds_service);
4079 ptlrpc_unregister_service(mds->mds_service);
4084 static struct dentry *mds_lvfs_id2dentry(__u64 ino, __u32 gen,
4085 __u64 gr, void *data)
4087 struct lustre_id id;
4088 struct obd_device *obd = data;
4092 return mds_id2dentry(obd, &id, NULL);
4095 static int mds_get_info(struct obd_export *exp, __u32 keylen,
4096 void *key, __u32 *valsize, void *val)
4098 struct obd_device *obd;
4099 struct mds_obd *mds;
4102 obd = class_exp2obd(exp);
4106 CDEBUG(D_IOCTL, "invalid client cookie "LPX64"\n",
4107 exp->exp_handle.h_cookie);
4111 if (keylen >= strlen("reint_log") && memcmp(key, "reint_log", 9) == 0) {
4112 /* get log_context handle. */
4113 unsigned long *llh_handle = val;
4114 *valsize = sizeof(unsigned long);
4115 *llh_handle = (unsigned long)obd->obd_llog_ctxt[LLOG_REINT_ORIG_CTXT];
4118 if (keylen >= strlen("cache_sb") && memcmp(key, "cache_sb", 8) == 0) {
4119 /* get log_context handle. */
4120 unsigned long *sb = val;
4121 *valsize = sizeof(unsigned long);
4122 *sb = (unsigned long)obd->u.mds.mds_sb;
4126 if (keylen >= strlen("mdsize") && memcmp(key, "mdsize", keylen) == 0) {
4127 __u32 *mdsize = val;
4128 *valsize = sizeof(*mdsize);
4129 *mdsize = mds->mds_max_mdsize;
4133 if (keylen >= strlen("mdsnum") && strcmp(key, "mdsnum") == 0) {
4134 __u32 *mdsnum = val;
4135 *valsize = sizeof(*mdsnum);
4136 *mdsnum = mds->mds_num;
4140 if (keylen >= strlen("rootid") && strcmp(key, "rootid") == 0) {
4141 struct lustre_id *rootid = val;
4142 *valsize = sizeof(struct lustre_id);
4143 *rootid = mds->mds_rootid;
4147 CDEBUG(D_IOCTL, "invalid key\n");
4151 struct lvfs_callback_ops mds_lvfs_ops = {
4152 l_id2dentry: mds_lvfs_id2dentry,
4155 int mds_preprw(int cmd, struct obd_export *exp, struct obdo *oa,
4156 int objcount, struct obd_ioobj *obj,
4157 int niocount, struct niobuf_remote *nb,
4158 struct niobuf_local *res,
4159 struct obd_trans_info *oti);
4161 int mds_commitrw(int cmd, struct obd_export *exp, struct obdo *oa,
4162 int objcount, struct obd_ioobj *obj, int niocount,
4163 struct niobuf_local *res, struct obd_trans_info *oti,
4166 /* use obd ops to offer management infrastructure */
4167 static struct obd_ops mds_obd_ops = {
4168 .o_owner = THIS_MODULE,
4169 .o_attach = mds_attach,
4170 .o_detach = mds_detach,
4171 .o_connect = mds_connect,
4172 .o_connect_post = mds_connect_post,
4173 .o_init_export = mds_init_export,
4174 .o_destroy_export = mds_destroy_export,
4175 .o_disconnect = mds_disconnect,
4176 .o_setup = mds_setup,
4177 .o_precleanup = mds_precleanup,
4178 .o_cleanup = mds_cleanup,
4179 .o_process_config = mds_process_config,
4180 .o_postrecov = mds_postrecov,
4181 .o_statfs = mds_obd_statfs,
4182 .o_iocontrol = mds_iocontrol,
4183 .o_create = mds_obd_create,
4184 .o_destroy = mds_obd_destroy,
4185 .o_llog_init = mds_llog_init,
4186 .o_llog_finish = mds_llog_finish,
4187 .o_notify = mds_notify,
4188 .o_get_info = mds_get_info,
4189 .o_set_info = mds_set_info,
4190 .o_preprw = mds_preprw,
4191 .o_commitrw = mds_commitrw,
4194 static struct obd_ops mdt_obd_ops = {
4195 .o_owner = THIS_MODULE,
4196 .o_attach = mdt_attach,
4197 .o_detach = mdt_detach,
4198 .o_setup = mdt_setup,
4199 .o_cleanup = mdt_cleanup,
4202 static int __init mds_init(void)
4204 struct lprocfs_static_vars lvars;
4206 mds_init_lsd_cache();
4208 lprocfs_init_multi_vars(0, &lvars);
4209 class_register_type(&mds_obd_ops, NULL, lvars.module_vars,
4211 lprocfs_init_multi_vars(1, &lvars);
4212 class_register_type(&mdt_obd_ops, NULL, lvars.module_vars,
4218 static void /*__exit*/ mds_exit(void)
4220 mds_cleanup_lsd_cache();
4222 class_unregister_type(LUSTRE_MDS_NAME);
4223 class_unregister_type(LUSTRE_MDT_NAME);
4226 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
4227 MODULE_DESCRIPTION("Lustre Metadata Server (MDS)");
4228 MODULE_LICENSE("GPL");
4230 module_init(mds_init);
4231 module_exit(mds_exit);