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);
295 } else if (de->d_inode && de->d_inode->i_nlink == 0) {
296 /* as sometimes we lookup inode by ino/generation through
297 iopen mechanism, it's possible to find already unlinked
298 inode with nlink == 0. let's interpretate the case as
300 CWARN("found already unlinked inode %lu/%u\n",
301 de->d_inode->i_ino, de->d_inode->i_generation);
303 retval = ERR_PTR(-ENOENT);
304 ldlm_lock_decref(lockh, lock_mode);
307 ldlm_lock_decref(lockh + 1, *mode);
314 #ifndef DCACHE_DISCONNECTED
315 #define DCACHE_DISCONNECTED DCACHE_NFSD_DISCONNECTED
318 /* Look up an entry by inode number. This function ONLY returns valid dget'd
319 * dentries with an initialized inode or errors */
320 struct dentry *mds_id2dentry(struct obd_device *obd, struct lustre_id *id,
321 struct vfsmount **mnt)
323 unsigned long ino = (unsigned long)id_ino(id);
324 __u32 generation = (__u32)id_gen(id);
325 struct mds_obd *mds = &obd->u.mds;
326 struct dentry *result;
331 RETURN(ERR_PTR(-ESTALE));
333 snprintf(idname, sizeof(idname), "0x%lx", ino);
335 CDEBUG(D_DENTRY, "--> mds_id2dentry: ino/gen %lu/%u, sb %p\n",
336 ino, generation, mds->mds_sb);
338 /* under ext3 this is neither supposed to return bad inodes nor NULL
340 result = ll_lookup_one_len(idname, mds->mds_id_de,
345 inode = result->d_inode;
347 RETURN(ERR_PTR(-ENOENT));
349 if (is_bad_inode(inode)) {
350 CERROR("bad inode returned %lu/%u\n",
351 inode->i_ino, inode->i_generation);
353 RETURN(ERR_PTR(-ENOENT));
356 /* here we disabled generation check, as root inode i_generation
357 * of cache mds and real mds are different. */
358 if (inode->i_ino != id_ino(&mds->mds_rootid) && generation &&
359 inode->i_generation != generation) {
360 /* we didn't find the right inode.. */
361 if (id_group(id) != mds->mds_num) {
362 CERROR("bad inode %lu found, link: %lu, ct: %d, generation "
363 "%u != %u, mds %u != %u, request to wrong MDS?\n",
364 inode->i_ino, (unsigned long)inode->i_nlink,
365 atomic_read(&inode->i_count), inode->i_generation,
366 generation, mds->mds_num, (unsigned)id_group(id));
368 CERROR("bad inode %lu found, link: %lu, ct: %d, generation "
369 "%u != %u, inode is recreated while request handled?\n",
370 inode->i_ino, (unsigned long)inode->i_nlink,
371 atomic_read(&inode->i_count), inode->i_generation,
375 RETURN(ERR_PTR(-ENOENT));
379 *mnt = mds->mds_vfsmnt;
387 int mds_req_add_idmapping(struct ptlrpc_request *req,
388 struct mds_export_data *med)
390 struct mds_req_sec_desc *rsd;
391 struct lustre_sec_desc *lsd;
394 if (!med->med_remote)
397 /* maybe we should do it more completely: invalidate the gss ctxt? */
398 if (req->rq_mapped_uid == MDS_IDMAP_NOTFOUND) {
399 CWARN("didn't find mapped uid\n");
403 rsd = lustre_swab_mds_secdesc(req, MDS_REQ_SECDESC_OFF);
405 CERROR("Can't unpack security desc\n");
409 lsd = mds_get_lsd(req->rq_mapped_uid);
411 CERROR("can't get LSD(%u), no mapping added\n",
416 rc = mds_idmap_add(med->med_idmap, rsd->rsd_uid, lsd->lsd_uid,
417 rsd->rsd_gid, lsd->lsd_gid);
423 int mds_req_del_idmapping(struct ptlrpc_request *req,
424 struct mds_export_data *med)
426 struct mds_req_sec_desc *rsd;
427 struct lustre_sec_desc *lsd;
430 if (!med->med_remote)
433 rsd = lustre_swab_mds_secdesc(req, MDS_REQ_SECDESC_OFF);
435 CERROR("Can't unpack security desc\n");
439 LASSERT(req->rq_mapped_uid != -1);
440 lsd = mds_get_lsd(req->rq_mapped_uid);
442 CERROR("can't get LSD(%u), no idmapping deleted\n",
447 rc = mds_idmap_del(med->med_idmap, rsd->rsd_uid, lsd->lsd_uid,
448 rsd->rsd_gid, lsd->lsd_gid);
453 static int mds_init_export_data(struct ptlrpc_request *req,
454 struct mds_export_data *med)
456 struct obd_connect_data *data, *reply;
457 int ask_remote, ask_local;
460 data = lustre_msg_buf(req->rq_reqmsg, 5, sizeof(*data));
461 reply = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*data));
462 LASSERT(data && reply);
464 if (med->med_initialized) {
465 CDEBUG(D_SEC, "med already initialized, reconnect?\n");
469 ask_remote = data->ocd_connect_flags & OBD_CONNECT_REMOTE;
470 ask_local = data->ocd_connect_flags & OBD_CONNECT_LOCAL;
472 /* currently the policy is simple: satisfy client as possible
475 if (req->rq_auth_uid == -1) {
477 CWARN("null sec is used, force to be local\n");
481 if (!req->rq_remote_realm)
482 CWARN("local realm asked to be remote\n");
484 } else if (ask_local) {
485 if (req->rq_remote_realm)
486 CWARN("remote realm asked to be local\n");
489 med->med_remote = (req->rq_remote_realm != 0);
492 med->med_nllu = data->ocd_nllu[0];
493 med->med_nllg = data->ocd_nllu[1];
495 med->med_initialized = 1;
497 reply->ocd_connect_flags &= ~(OBD_CONNECT_REMOTE | OBD_CONNECT_LOCAL);
498 if (med->med_remote) {
500 med->med_idmap = mds_idmap_alloc();
503 CERROR("Failed to alloc idmap, following request from "
504 "this client will be refused\n");
506 reply->ocd_connect_flags |= OBD_CONNECT_REMOTE;
507 CDEBUG(D_SEC, "set client as remote\n");
509 reply->ocd_connect_flags |= OBD_CONNECT_LOCAL;
510 CDEBUG(D_SEC, "set client as local\n");
516 static void mds_free_export_data(struct mds_export_data *med)
521 LASSERT(med->med_remote);
522 mds_idmap_free(med->med_idmap);
523 med->med_idmap = NULL;
526 /* Establish a connection to the MDS.
528 * This will set up an export structure for the client to hold state data about
529 * that client, like open files, the last operation number it did on the server,
532 static int mds_connect(struct lustre_handle *conn, struct obd_device *obd,
533 struct obd_uuid *cluuid, struct obd_connect_data *data,
536 struct mds_export_data *med;
537 struct mds_client_data *mcd;
538 struct obd_export *exp;
542 if (!conn || !obd || !cluuid)
545 /* XXX There is a small race between checking the list and adding a new
546 * connection for the same UUID, but the real threat (list corruption
547 * when multiple different clients connect) is solved.
549 * There is a second race between adding the export to the list, and
550 * filling in the client data below. Hence skipping the case of NULL
551 * mcd above. We should already be controlling multiple connects at the
552 * client, and we can't hold the spinlock over memory allocations
553 * without risk of deadlocking.
555 rc = class_connect(conn, obd, cluuid);
558 exp = class_conn2export(conn);
560 LASSERT(exp != NULL);
561 med = &exp->exp_mds_data;
563 OBD_ALLOC(mcd, sizeof(*mcd));
565 CERROR("%s: out of memory for client data.\n",
567 GOTO(out, rc = -ENOMEM);
570 memcpy(mcd->mcd_uuid, cluuid, sizeof(mcd->mcd_uuid));
573 rc = mds_client_add(obd, &obd->u.mds, med, -1);
581 OBD_FREE(mcd, sizeof(*mcd));
582 class_disconnect(exp, 0);
584 class_export_put(exp);
589 static int mds_connect_post(struct obd_export *exp, unsigned initial,
592 struct obd_device *obd = exp->exp_obd;
593 struct mds_obd *mds = &obd->u.mds;
594 struct mds_export_data *med;
595 struct mds_client_data *mcd;
599 med = &exp->exp_mds_data;
603 /* some one reconnect initially, we have to reset
604 * data existing export can have. bug 6102 */
605 if (mcd->mcd_last_xid != 0)
606 CDEBUG(D_HA, "initial reconnect to existing export\n");
607 mcd->mcd_last_transno = 0;
608 mcd->mcd_last_xid = 0;
609 mcd->mcd_last_result = 0;
610 mcd->mcd_last_data = 0;
613 if (!(flags & OBD_OPT_MDS_CONNECTION)) {
614 if (!(exp->exp_flags & OBD_OPT_REAL_CLIENT)) {
615 atomic_inc(&mds->mds_real_clients);
616 CDEBUG(D_OTHER,"%s: peer from %s is real client (%d)\n",
617 obd->obd_name, exp->exp_client_uuid.uuid,
618 atomic_read(&mds->mds_real_clients));
619 exp->exp_flags |= OBD_OPT_REAL_CLIENT;
621 if (mds->mds_md_name)
622 rc = mds_md_connect(obd, mds->mds_md_name);
627 static int mds_init_export(struct obd_export *exp)
629 struct mds_export_data *med = &exp->exp_mds_data;
631 INIT_LIST_HEAD(&med->med_open_head);
632 spin_lock_init(&med->med_open_lock);
636 static int mds_destroy_export(struct obd_export *export)
638 struct obd_device *obd = export->exp_obd;
639 struct mds_export_data *med = &export->exp_mds_data;
640 struct lvfs_run_ctxt saved;
644 mds_free_export_data(med);
645 target_destroy_export(export);
647 if (obd_uuid_equals(&export->exp_client_uuid, &obd->obd_uuid))
650 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
652 /* Close any open files (which may also cause orphan unlinking). */
653 spin_lock(&med->med_open_lock);
654 while (!list_empty(&med->med_open_head)) {
655 struct list_head *tmp = med->med_open_head.next;
656 struct mds_file_data *mfd =
657 list_entry(tmp, struct mds_file_data, mfd_list);
658 struct lustre_id sid;
660 BDEVNAME_DECLARE_STORAGE(btmp);
662 /* bug 1579: fix force-closing for 2.5 */
663 struct dentry *dentry = mfd->mfd_dentry;
665 list_del(&mfd->mfd_list);
666 spin_unlock(&med->med_open_lock);
668 down(&dentry->d_inode->i_sem);
669 rc = mds_read_inode_sid(obd, dentry->d_inode, &sid);
670 up(&dentry->d_inode->i_sem);
672 CERROR("Can't read inode self id, inode %lu, "
673 "rc %d\n", dentry->d_inode->i_ino, rc);
674 memset(&sid, 0, sizeof(sid));
677 /* If you change this message, be sure to update
678 * replay_single:test_46 */
679 CERROR("force closing client file handle for %.*s (%s:"
680 DLID4")\n", dentry->d_name.len, dentry->d_name.name,
681 ll_bdevname(dentry->d_inode->i_sb, btmp),
684 /* child inode->i_alloc_sem protects orphan_dec_test and
685 * is_orphan race, mds_mfd_close drops it */
686 DOWN_WRITE_I_ALLOC_SEM(dentry->d_inode);
687 rc = mds_mfd_close(NULL, 0, obd, mfd,
688 !(export->exp_flags & OBD_OPT_FAILOVER));
690 CDEBUG(D_INODE, "Error closing file: %d\n", rc);
691 spin_lock(&med->med_open_lock);
693 spin_unlock(&med->med_open_lock);
694 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
698 mds_client_free(export, !(export->exp_flags & OBD_OPT_FAILOVER));
702 static int mds_disconnect(struct obd_export *exp, unsigned long flags)
704 unsigned long irqflags;
705 struct obd_device *obd;
710 LASSERT(exp != NULL);
711 obd = class_exp2obd(exp);
713 CDEBUG(D_IOCTL, "invalid client cookie "LPX64"\n",
714 exp->exp_handle.h_cookie);
720 * suppress any inter-mds requests durring disconnecting lmv if this is
721 * detected --force mode. This is needed to avoid endless recovery.
723 if (atomic_read(&mds->mds_real_clients) > 0 &&
724 !(exp->exp_flags & OBD_OPT_REAL_CLIENT))
725 flags |= OBD_OPT_FORCE;
727 if (!(exp->exp_flags & OBD_OPT_REAL_CLIENT)
728 && !atomic_read(&mds->mds_real_clients)) {
729 /* there was no client at all */
730 mds_md_disconnect(obd, flags);
733 if ((exp->exp_flags & OBD_OPT_REAL_CLIENT)
734 && atomic_dec_and_test(&mds->mds_real_clients)) {
735 /* time to drop LMV connections */
736 CDEBUG(D_OTHER, "%s: last real client %s disconnected. "
737 "Disconnnect from LMV now\n",
738 obd->obd_name, exp->exp_client_uuid.uuid);
739 mds_md_disconnect(obd, flags);
742 spin_lock_irqsave(&exp->exp_lock, irqflags);
743 exp->exp_flags = flags;
744 spin_unlock_irqrestore(&exp->exp_lock, irqflags);
746 /* disconnect early so that clients can't keep using export */
747 rc = class_disconnect(exp, flags);
748 ldlm_cancel_locks_for_export(exp);
750 /* complete all outstanding replies */
751 spin_lock_irqsave(&exp->exp_lock, irqflags);
752 while (!list_empty(&exp->exp_outstanding_replies)) {
753 struct ptlrpc_reply_state *rs =
754 list_entry(exp->exp_outstanding_replies.next,
755 struct ptlrpc_reply_state, rs_exp_list);
756 struct ptlrpc_service *svc = rs->rs_srv_ni->sni_service;
758 spin_lock(&svc->srv_lock);
759 list_del_init(&rs->rs_exp_list);
760 ptlrpc_schedule_difficult_reply(rs);
761 spin_unlock(&svc->srv_lock);
763 spin_unlock_irqrestore(&exp->exp_lock, irqflags);
767 static int mds_getstatus(struct ptlrpc_request *req)
769 struct mds_obd *mds = mds_req2mds(req);
770 struct mds_body *body;
774 size = sizeof(*body);
776 rc = lustre_pack_reply(req, 1, &size, NULL);
777 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_GETSTATUS_PACK)) {
778 CERROR("mds: out of memory for message: size=%d\n", size);
779 req->rq_status = -ENOMEM; /* superfluous? */
783 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
784 body->valid |= OBD_MD_FID;
786 memcpy(&body->id1, &mds->mds_rootid, sizeof(body->id1));
789 * the last_committed and last_xid fields are filled in for all replies
790 * already - no need to do so here also.
795 int mds_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
796 void *data, int flag)
801 if (flag == LDLM_CB_CANCELING) {
802 /* Don't need to do anything here. */
806 /* XXX layering violation! -phil */
807 l_lock(&lock->l_resource->lr_namespace->ns_lock);
810 * get this: if mds_blocking_ast is racing with mds_intent_policy, such
811 * that mds_blocking_ast is called just before l_i_p takes the ns_lock,
812 * then by the time we get the lock, we might not be the correct
813 * blocking function anymore. So check, and return early, if so.
815 if (lock->l_blocking_ast != mds_blocking_ast) {
816 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
820 lock->l_flags |= LDLM_FL_CBPENDING;
821 do_ast = (!lock->l_readers && !lock->l_writers);
822 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
825 struct lustre_handle lockh;
828 LDLM_DEBUG(lock, "already unused, calling ldlm_cli_cancel");
829 ldlm_lock2handle(lock, &lockh);
830 rc = ldlm_cli_cancel(&lockh);
832 CERROR("ldlm_cli_cancel: %d\n", rc);
834 LDLM_DEBUG(lock, "Lock still has references, will be "
840 static int mds_convert_md(struct obd_device *obd, struct inode *inode,
841 void *md, int size, int mea)
845 if (S_ISREG(inode->i_mode)) {
846 rc = mds_convert_lov_ea(obd, inode, md, size);
847 } else if (S_ISDIR(inode->i_mode)) {
849 rc = mds_convert_mea_ea(obd, inode, md, size);
851 rc = mds_convert_lov_ea(obd, inode, md, size);
854 CERROR("Invalid EA format (nor LOV or MEA) "
855 "is detected. Inode %lu/%u\n",
856 inode->i_ino, inode->i_generation);
862 int mds_get_md(struct obd_device *obd, struct inode *inode,
863 void *md, int *size, int lock, int mea)
872 rc = fsfilt_get_md(obd, inode, md, *size,
873 (mea ? EA_MEA : EA_LOV));
875 CERROR("Error %d reading eadata for ino %lu\n",
879 rc = mds_convert_md(obd, inode, md,
894 /* Call with lock=1 if you want mds_pack_md to take the i_sem.
895 * Call with lock=0 if the caller has already taken the i_sem. */
896 int mds_pack_md(struct obd_device *obd, struct lustre_msg *msg, int offset,
897 struct mds_body *body, struct inode *inode, int lock, int mea)
899 struct mds_obd *mds = &obd->u.mds;
904 lmm = lustre_msg_buf(msg, offset, 0);
906 /* Some problem with getting eadata when I sized the reply
908 CDEBUG(D_INFO, "no space reserved for inode %lu MD\n",
912 lmm_size = msg->buflens[offset];
914 /* I don't really like this, but it is a sanity check on the client
915 * MD request. However, if the client doesn't know how much space
916 * to reserve for the MD, it shouldn't be bad to have too much space.
918 if (lmm_size > mds->mds_max_mdsize) {
919 CWARN("Reading MD for inode %lu of %d bytes > max %d\n",
920 inode->i_ino, lmm_size, mds->mds_max_mdsize);
924 rc = mds_get_md(obd, inode, lmm, &lmm_size, lock, mea);
926 body->valid |= S_ISDIR(inode->i_mode) ?
927 OBD_MD_FLDIREA : OBD_MD_FLEASIZE;
930 body->valid |= OBD_MD_MEA;
932 body->eadatasize = lmm_size;
939 int mds_pack_link(struct dentry *dentry, struct ptlrpc_request *req,
940 struct mds_body *repbody, int reply_off)
942 struct inode *inode = dentry->d_inode;
947 symname = lustre_msg_buf(req->rq_repmsg, reply_off + 1,0);
948 LASSERT(symname != NULL);
949 len = req->rq_repmsg->buflens[reply_off + 1];
951 rc = inode->i_op->readlink(dentry, symname, len);
953 CERROR("readlink failed: %d\n", rc);
954 } else if (rc != len - 1) {
955 CERROR ("Unexpected readlink rc %d: expecting %d\n",
959 CDEBUG(D_INODE, "read symlink dest %s\n", symname);
960 repbody->valid |= OBD_MD_LINKNAME;
961 repbody->eadatasize = rc + 1;
962 symname[rc] = 0; /* NULL terminate */
969 int mds_pack_ea(struct dentry *dentry, struct ptlrpc_request *req,
970 struct mds_body *repbody, int req_off, int reply_off)
972 struct inode *inode = dentry->d_inode;
978 ea_name = lustre_msg_string(req->rq_reqmsg, req_off + 1, 0);
979 len = req->rq_repmsg->buflens[reply_off + 1];
981 value = lustre_msg_buf(req->rq_repmsg, reply_off + 1, len);
984 if (inode->i_op && inode->i_op->getxattr)
985 rc = inode->i_op->getxattr(dentry, ea_name, value, len);
988 if (rc != -ENODATA && rc != -EOPNOTSUPP)
989 CERROR("getxattr failed: %d", rc);
991 repbody->valid |= OBD_MD_FLEA;
992 repbody->eadatasize = rc;
999 int mds_pack_ealist(struct dentry *dentry, struct ptlrpc_request *req,
1000 struct mds_body *repbody, int reply_off)
1002 struct inode *inode = dentry->d_inode;
1007 len = req->rq_repmsg->buflens[reply_off + 1];
1009 value = lustre_msg_buf(req->rq_repmsg, reply_off + 1, len);
1012 if (inode->i_op && inode->i_op->getxattr)
1013 rc = inode->i_op->listxattr(dentry, value, len);
1016 CERROR("listxattr failed: %d", rc);
1018 repbody->valid |= OBD_MD_FLEALIST;
1019 repbody->eadatasize = rc;
1025 int mds_pack_acl(struct obd_device *obd, struct lustre_msg *repmsg, int offset,
1026 struct mds_body *body, struct inode *inode)
1028 struct dentry de = { .d_inode = inode };
1029 __u32 buflen, *sizep;
1034 if (!inode->i_op->getxattr)
1037 buflen = repmsg->buflens[offset + 1];
1038 buf = lustre_msg_buf(repmsg, offset + 1, buflen);
1040 size = inode->i_op->getxattr(&de, XATTR_NAME_ACL_ACCESS, buf, buflen);
1041 if (size == -ENODATA || size == -EOPNOTSUPP)
1047 sizep = lustre_msg_buf(repmsg, offset, 4);
1049 CERROR("can't locate returned acl size buf\n");
1053 *sizep = cpu_to_le32(size);
1054 body->valid |= OBD_MD_FLACL_ACCESS;
1060 * here we take simple rule: once uid/fsuid is root, we also squash
1061 * the gid/fsgid, don't care setuid/setgid attributes.
1063 int mds_squash_root(struct mds_obd *mds, struct mds_req_sec_desc *rsd,
1066 if (!mds->mds_squash_uid || *peernid == mds->mds_nosquash_nid)
1069 if (rsd->rsd_uid && rsd->rsd_fsuid)
1072 CDEBUG(D_SEC, "squash req from "LPX64":"
1073 "(%u:%u-%u:%u/%x)=>(%u:%u-%u:%u/%x)\n", *peernid,
1074 rsd->rsd_uid, rsd->rsd_gid,
1075 rsd->rsd_fsuid, rsd->rsd_fsgid, rsd->rsd_cap,
1076 rsd->rsd_uid ? rsd->rsd_uid : mds->mds_squash_uid,
1077 rsd->rsd_uid ? rsd->rsd_gid : mds->mds_squash_gid,
1078 rsd->rsd_fsuid ? rsd->rsd_fsuid : mds->mds_squash_uid,
1079 rsd->rsd_fsuid ? rsd->rsd_fsgid : mds->mds_squash_gid,
1080 rsd->rsd_cap & ~CAP_FS_MASK);
1082 if (rsd->rsd_uid == 0) {
1083 rsd->rsd_uid = mds->mds_squash_uid;
1084 rsd->rsd_gid = mds->mds_squash_gid;
1086 if (rsd->rsd_fsuid == 0) {
1087 rsd->rsd_fsuid = mds->mds_squash_uid;
1088 rsd->rsd_fsgid = mds->mds_squash_gid;
1090 rsd->rsd_cap &= ~CAP_FS_MASK;
1095 static int mds_getattr_internal(struct obd_device *obd, struct dentry *dentry,
1096 struct ptlrpc_request *req, int req_off,
1097 struct mds_body *reqbody, int reply_off)
1099 struct mds_export_data *med = &req->rq_export->u.eu_mds_data;
1100 struct inode *inode = dentry->d_inode;
1101 struct mds_body *body;
1105 if (inode == NULL && !(dentry->d_flags & DCACHE_CROSS_REF))
1108 body = lustre_msg_buf(req->rq_repmsg, reply_off, sizeof(*body));
1109 LASSERT(body != NULL); /* caller prepped reply */
1111 if (dentry->d_flags & DCACHE_CROSS_REF) {
1112 mds_pack_dentry2body(obd, body, dentry,
1113 (reqbody->valid & OBD_MD_FID) ? 1 : 0);
1114 CDEBUG(D_OTHER, "cross reference: "DLID4"\n",
1119 mds_pack_inode2body(obd, body, inode,
1120 (reqbody->valid & OBD_MD_FID) ? 1 : 0);
1122 if ((S_ISREG(inode->i_mode) && (reqbody->valid & OBD_MD_FLEASIZE)) ||
1123 (S_ISDIR(inode->i_mode) && (reqbody->valid & OBD_MD_FLDIREA))) {
1125 /* guessing what kind og attribute do we need. */
1126 int is_mea = (S_ISDIR(inode->i_mode) &&
1127 (reqbody->valid & OBD_MD_MEA) != 0);
1129 rc = mds_pack_md(obd, req->rq_repmsg, reply_off + 1,
1130 body, inode, 1, is_mea);
1132 /* if we have LOV EA data, the OST holds size, atime, mtime. */
1133 if (!(body->valid & OBD_MD_FLEASIZE) &&
1134 !(body->valid & OBD_MD_FLDIREA))
1135 body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
1136 OBD_MD_FLATIME | OBD_MD_FLMTIME);
1137 } else if (S_ISLNK(inode->i_mode) &&
1138 (reqbody->valid & OBD_MD_LINKNAME) != 0) {
1139 rc = mds_pack_link(dentry, req, body, reply_off);
1140 } else if (reqbody->valid & OBD_MD_FLEA) {
1141 rc = mds_pack_ea(dentry, req, body, req_off, reply_off);
1142 } else if (reqbody->valid & OBD_MD_FLEALIST) {
1143 rc = mds_pack_ealist(dentry, req, body, reply_off);
1146 if (reqbody->valid & OBD_MD_FLACL_ACCESS) {
1147 int inc = (reqbody->valid & OBD_MD_FLEASIZE) ? 2 : 1;
1148 rc = mds_pack_acl(obd, req->rq_repmsg, reply_off + inc,
1153 mds_body_do_reverse_map(med, body);
1158 static int mds_getattr_pack_msg_cf(struct ptlrpc_request *req,
1159 struct dentry *dentry,
1162 int rc = 0, size[1] = {sizeof(struct mds_body)};
1165 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK)) {
1166 CERROR("failed MDS_GETATTR_PACK test\n");
1167 req->rq_status = -ENOMEM;
1171 rc = lustre_pack_reply(req, 1, size, NULL);
1173 CERROR("lustre_pack_reply failed: rc %d\n", rc);
1174 GOTO(out, req->rq_status = rc);
1182 static int mds_getattr_pack_msg(struct ptlrpc_request *req, struct dentry *de,
1185 struct inode *inode = de->d_inode;
1186 struct mds_obd *mds = mds_req2mds(req);
1187 struct mds_body *body;
1188 int rc = 0, size[4] = {sizeof(*body)}, bufcount = 1;
1191 body = lustre_msg_buf(req->rq_reqmsg, offset, sizeof(*body));
1192 LASSERT(body != NULL); /* checked by caller */
1193 LASSERT_REQSWABBED(req, offset); /* swabbed by caller */
1195 if ((S_ISREG(inode->i_mode) && (body->valid & OBD_MD_FLEASIZE)) ||
1196 (S_ISDIR(inode->i_mode) && (body->valid & OBD_MD_FLDIREA))) {
1199 down(&inode->i_sem);
1200 rc = fsfilt_get_md(req->rq_export->exp_obd, inode, NULL, 0,
1201 ((body->valid & OBD_MD_MEA) ? EA_MEA : EA_LOV));
1204 if (rc != -ENODATA && rc != -EOPNOTSUPP)
1205 CERROR("error getting inode %lu MD: rc = %d\n",
1208 } else if (rc > mds->mds_max_mdsize) {
1210 CERROR("MD size %d larger than maximum possible %u\n",
1211 rc, mds->mds_max_mdsize);
1213 size[bufcount] = rc;
1216 } else if (S_ISLNK(inode->i_mode) && (body->valid & OBD_MD_LINKNAME)) {
1217 if (inode->i_size + 1 != body->eadatasize)
1218 CERROR("symlink size: %Lu, reply space: %d\n",
1219 inode->i_size + 1, body->eadatasize);
1220 size[bufcount] = min_t(int, inode->i_size+1, body->eadatasize);
1222 CDEBUG(D_INODE, "symlink size: %Lu, reply space: %d\n",
1223 inode->i_size + 1, body->eadatasize);
1224 } else if ((body->valid & OBD_MD_FLEA)) {
1225 char *ea_name = lustre_msg_string(req->rq_reqmsg,
1228 if (inode->i_op && inode->i_op->getxattr)
1229 rc = inode->i_op->getxattr(de, ea_name, NULL, 0);
1232 if (rc != -ENODATA && rc != -EOPNOTSUPP)
1233 CERROR("error getting inode %lu EA: rc = %d\n",
1237 size[bufcount] = min_t(int, body->eadatasize, rc);
1240 } else if (body->valid & OBD_MD_FLEALIST) {
1242 if (inode->i_op && inode->i_op->getxattr)
1243 rc = inode->i_op->listxattr(de, NULL, 0);
1246 if (rc != -ENODATA && rc != -EOPNOTSUPP)
1247 CERROR("error getting inode %lu EA: rc = %d\n",
1251 size[bufcount] = min_t(int, body->eadatasize, rc);
1256 /* may co-exist with OBD_MD_FLEASIZE */
1257 if (body->valid & OBD_MD_FLACL_ACCESS) {
1258 size[bufcount++] = 4;
1259 size[bufcount++] = xattr_acl_size(LL_ACL_MAX_ENTRIES);
1262 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK)) {
1263 CERROR("failed MDS_GETATTR_PACK test\n");
1264 req->rq_status = -ENOMEM;
1265 GOTO(out, rc = -ENOMEM);
1268 rc = lustre_pack_reply(req, bufcount, size, NULL);
1270 CERROR("out of memory\n");
1271 GOTO(out, req->rq_status = rc);
1279 int mds_check_mds_num(struct obd_device *obd, struct inode *inode,
1280 char *name, int namelen)
1282 struct mea *mea = NULL;
1283 int mea_size, rc = 0;
1286 rc = mds_md_get_attr(obd, inode, &mea, &mea_size);
1291 * dir is already splitted, check if requested filename should
1292 * live at this MDS or at another one.
1294 int i = mea_name2idx(mea, name, namelen - 1);
1295 if (mea->mea_master != id_group(&mea->mea_ids[i])) {
1297 "inapropriate MDS(%d) for %s. should be "
1298 "%lu(%d)\n", mea->mea_master, name,
1299 (unsigned long)id_group(&mea->mea_ids[i]), i);
1305 OBD_FREE(mea, mea_size);
1309 int mds_getattr_size(struct obd_device *obd, struct dentry *dentry,
1310 struct ptlrpc_request *req, struct mds_body *body)
1312 struct inode *inode = dentry->d_inode;
1315 LASSERT(body != NULL);
1317 if (dentry->d_inode == NULL || !S_ISREG(inode->i_mode))
1320 if (obd->obd_recovering) {
1321 CDEBUG(D_ERROR, "size for "DLID4" is unknown yet (recovering)\n",
1326 if (atomic_read(&inode->i_writecount)) {
1327 /* some one has opened the file for write.
1328 * mds doesn't know actual size */
1329 CDEBUG(D_OTHER, "MDS doesn't know actual size for "DLID4"\n",
1333 CDEBUG(D_OTHER, "MDS returns "LPD64"/"LPD64" for"DLID4"\n",
1334 body->size, body->blocks, OLID4(&body->id1));
1335 body->valid |= OBD_MD_FLSIZE;
1339 static int mds_getattr_lock(struct ptlrpc_request *req, int offset,
1340 struct lustre_handle *child_lockh, int child_part)
1342 struct obd_device *obd = req->rq_export->exp_obd;
1343 struct mds_obd *mds = &obd->u.mds;
1344 struct ldlm_reply *rep = NULL;
1345 struct lvfs_run_ctxt saved;
1346 struct mds_req_sec_desc *rsd;
1347 struct mds_body *body;
1348 struct dentry *dparent = NULL, *dchild = NULL;
1349 struct lvfs_ucred uc = {NULL, NULL,};
1350 struct lustre_handle parent_lockh[2] = {{0}, {0}};
1351 unsigned int namesize;
1352 int rc = 0, cleanup_phase = 0, resent_req = 0, update_mode, reply_offset;
1356 LASSERT(!strcmp(obd->obd_type->typ_name, LUSTRE_MDS_NAME));
1357 MD_COUNTER_INCREMENT(obd, getattr_lock);
1359 rsd = lustre_swab_mds_secdesc(req, MDS_REQ_SECDESC_OFF);
1361 CERROR("Can't unpack security desc\n");
1365 /* swab now, before anyone looks inside the request. */
1366 body = lustre_swab_reqbuf(req, offset, sizeof(*body),
1367 lustre_swab_mds_body);
1369 CERROR("Can't swab mds_body\n");
1370 GOTO(cleanup, rc = -EFAULT);
1373 LASSERT_REQSWAB(req, offset + 1);
1374 name = lustre_msg_string(req->rq_reqmsg, offset + 1, 0);
1376 CERROR("Can't unpack name\n");
1377 GOTO(cleanup, rc = -EFAULT);
1379 namesize = req->rq_reqmsg->buflens[offset + 1];
1381 /* namesize less than 2 means we have empty name, probably came from
1382 revalidate by cfid, so no point in having name to be set */
1386 LASSERT (offset == 1 || offset == 3);
1388 rep = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*rep));
1394 rc = mds_init_ucred(&uc, req, rsd);
1399 push_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1400 cleanup_phase = 1; /* kernel context */
1401 intent_set_disposition(rep, DISP_LOOKUP_EXECD);
1403 LASSERT(namesize > 0);
1404 if (child_lockh->cookie != 0) {
1405 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT);
1409 if (body->valid == OBD_MD_FLID) {
1410 struct mds_body *mds_reply;
1411 int size = sizeof(*mds_reply);
1415 dparent = mds_id2dentry(obd, &body->id1, NULL);
1416 if (IS_ERR(dparent)) {
1417 rc = PTR_ERR(dparent);
1421 * the user requested ONLY the inode number, so do a raw lookup.
1423 rc = lustre_pack_reply(req, 1, &size, NULL);
1425 CERROR("out of memory\n");
1429 dir = dparent->d_inode;
1430 LASSERT(dir->i_op->lookup_raw != NULL);
1431 rc = dir->i_op->lookup_raw(dir, name, namesize - 1, &inum);
1433 mds_reply = lustre_msg_buf(req->rq_repmsg, 0,
1434 sizeof(*mds_reply));
1436 id_ino(&mds_reply->id1) = inum;
1437 mds_reply->valid = OBD_MD_FLID;
1441 if (resent_req == 0) {
1442 LASSERT(id_fid(&body->id1) != 0);
1444 rc = mds_get_parent_child_locked(obd, mds, &body->id1,
1445 parent_lockh, &dparent,
1447 MDS_INODELOCK_UPDATE,
1450 child_lockh, &dchild,
1451 LCK_PR, child_part);
1455 cleanup_phase = 2; /* dchild, dparent, locks */
1458 * let's make sure this name should leave on this mds
1461 rc = mds_check_mds_num(obd, dparent->d_inode, name, namesize);
1465 /* we have no dentry here, drop LOOKUP bit */
1466 /* FIXME: we need MDS_INODELOCK_LOOKUP or not. */
1467 child_part &= ~MDS_INODELOCK_LOOKUP;
1468 CDEBUG(D_OTHER, "%s: retrieve attrs for "DLID4"\n",
1469 obd->obd_name, OLID4(&body->id1));
1471 dchild = mds_id2locked_dentry(obd, &body->id1, NULL,
1472 LCK_PR, parent_lockh,
1475 MDS_INODELOCK_UPDATE);
1476 if (IS_ERR(dchild)) {
1477 CERROR("can't find inode with id "DLID4", err = %d\n",
1478 OLID4(&body->id1), (int)PTR_ERR(dchild));
1479 GOTO(cleanup, rc = PTR_ERR(dchild));
1481 memcpy(child_lockh, parent_lockh, sizeof(parent_lockh[0]));
1484 struct ldlm_lock *granted_lock;
1486 DEBUG_REQ(D_DLMTRACE, req, "resent, not enqueuing new locks");
1487 granted_lock = ldlm_handle2lock(child_lockh);
1489 LASSERTF(granted_lock != NULL, LPU64"/%lu lockh "LPX64"\n",
1490 id_fid(&body->id1), (unsigned long)id_group(&body->id1),
1491 child_lockh->cookie);
1494 /* usual named request */
1495 dparent = mds_id2dentry(obd, &body->id1, NULL);
1496 LASSERT(!IS_ERR(dparent));
1497 dchild = ll_lookup_one_len(name, dparent, namesize - 1);
1498 LASSERT(!IS_ERR(dchild));
1500 /* client wants to get attr. by id */
1501 dchild = mds_id2dentry(obd, &body->id1, NULL);
1502 LASSERT(!IS_ERR(dchild));
1504 LDLM_LOCK_PUT(granted_lock);
1507 cleanup_phase = 2; /* dchild, dparent, locks */
1509 if (!DENTRY_VALID(dchild)) {
1510 intent_set_disposition(rep, DISP_LOOKUP_NEG);
1512 * in the intent case, the policy clears this error: the
1513 * disposition is enough.
1518 intent_set_disposition(rep, DISP_LOOKUP_POS);
1521 if (req->rq_repmsg == NULL) {
1522 if (dchild->d_flags & DCACHE_CROSS_REF)
1523 rc = mds_getattr_pack_msg_cf(req, dchild, offset);
1525 rc = mds_getattr_pack_msg(req, dchild, offset);
1527 CERROR ("mds_getattr_pack_msg: %d\n", rc);
1532 rc = mds_getattr_internal(obd, dchild, req, offset, body, reply_offset);
1534 GOTO(cleanup, rc); /* returns the lock to the client */
1536 /* probably MDS knows actual size? */
1537 body = lustre_msg_buf(req->rq_repmsg, reply_offset, sizeof(*body));
1538 LASSERT(body != NULL);
1539 mds_getattr_size(obd, dchild, req, body);
1544 switch (cleanup_phase) {
1546 if (resent_req == 0) {
1547 if (rc && DENTRY_VALID(dchild))
1548 ldlm_lock_decref(child_lockh, LCK_PR);
1550 ldlm_lock_decref(parent_lockh, LCK_PR);
1552 if (parent_lockh[1].cookie != 0)
1553 ldlm_lock_decref(parent_lockh + 1, update_mode);
1560 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1562 mds_exit_ucred(&uc);
1567 static int mds_getattr(struct ptlrpc_request *req, int offset)
1569 struct obd_device *obd = req->rq_export->exp_obd;
1570 struct lvfs_run_ctxt saved;
1572 struct mds_req_sec_desc *rsd;
1573 struct mds_body *body;
1574 struct lvfs_ucred uc = {NULL, NULL,};
1578 MD_COUNTER_INCREMENT(obd, getattr);
1580 rsd = lustre_swab_mds_secdesc(req, MDS_REQ_SECDESC_OFF);
1582 CERROR("Can't unpack security desc\n");
1586 body = lustre_swab_reqbuf(req, offset, sizeof(*body),
1587 lustre_swab_mds_body);
1589 CERROR ("Can't unpack body\n");
1593 rc = mds_init_ucred(&uc, req, rsd);
1595 mds_exit_ucred(&uc);
1599 push_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1600 de = mds_id2dentry(obd, &body->id1, NULL);
1602 rc = req->rq_status = PTR_ERR(de);
1606 rc = mds_getattr_pack_msg(req, de, offset);
1608 CERROR("mds_getattr_pack_msg: %d\n", rc);
1612 req->rq_status = mds_getattr_internal(obd, de, req, offset, body, 0);
1617 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1618 mds_exit_ucred(&uc);
1622 static int mds_obd_statfs(struct obd_device *obd, struct obd_statfs *osfs,
1623 unsigned long max_age)
1628 spin_lock(&obd->obd_osfs_lock);
1629 rc = fsfilt_statfs(obd, obd->u.mds.mds_sb, max_age);
1631 memcpy(osfs, &obd->obd_osfs, sizeof(*osfs));
1632 spin_unlock(&obd->obd_osfs_lock);
1637 static int mds_statfs(struct ptlrpc_request *req)
1639 struct obd_device *obd = req->rq_export->exp_obd;
1640 int rc, size = sizeof(struct obd_statfs);
1643 /* This will trigger a watchdog timeout */
1644 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_STATFS_LCW_SLEEP,
1645 (MDS_SERVICE_WATCHDOG_TIMEOUT / 1000) + 1);
1647 rc = lustre_pack_reply(req, 1, &size, NULL);
1648 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_STATFS_PACK)) {
1649 CERROR("mds: statfs lustre_pack_reply failed: rc = %d\n", rc);
1653 OBD_COUNTER_INCREMENT(obd, statfs);
1655 /* We call this so that we can cache a bit - 1 jiffie worth */
1656 rc = mds_obd_statfs(obd, lustre_msg_buf(req->rq_repmsg, 0, size),
1659 CERROR("mds_obd_statfs failed: rc %d\n", rc);
1665 req->rq_status = rc;
1669 static int mds_sync(struct ptlrpc_request *req, int offset)
1671 struct obd_device *obd = req->rq_export->exp_obd;
1672 struct mds_obd *mds = &obd->u.mds;
1673 struct mds_body *body;
1674 int rc, size = sizeof(*body);
1677 body = lustre_swab_reqbuf(req, offset, sizeof(*body),
1678 lustre_swab_mds_body);
1680 GOTO(out, rc = -EPROTO);
1682 rc = lustre_pack_reply(req, 1, &size, NULL);
1683 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK)) {
1684 CERROR("fsync lustre_pack_reply failed: rc = %d\n", rc);
1688 if (id_ino(&body->id1) == 0) {
1689 /* an id of zero is taken to mean "sync whole filesystem" */
1690 rc = fsfilt_sync(obd, mds->mds_sb);
1694 /* just any file to grab fsync method - "file" arg unused */
1695 struct file *file = mds->mds_rcvd_filp;
1696 struct mds_body *rep_body;
1699 de = mds_id2dentry(obd, &body->id1, NULL);
1701 GOTO(out, rc = PTR_ERR(de));
1703 rc = file->f_op->fsync(NULL, de, 1);
1707 rep_body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*rep_body));
1708 mds_pack_inode2body(obd, rep_body, de->d_inode,
1709 (body->valid & OBD_MD_FID) ? 1 : 0);
1715 req->rq_status = rc;
1719 /* mds_readpage does not take a DLM lock on the inode, because the client must
1720 * already have a PR lock.
1722 * If we were to take another one here, a deadlock will result, if another
1723 * thread is already waiting for a PW lock. */
1724 static int mds_readpage(struct ptlrpc_request *req, int offset)
1726 struct obd_device *obd = req->rq_export->exp_obd;
1727 struct vfsmount *mnt;
1730 struct mds_req_sec_desc *rsd;
1731 struct mds_body *body, *repbody;
1732 struct lvfs_run_ctxt saved;
1733 int rc, size = sizeof(*repbody);
1734 struct lvfs_ucred uc = {NULL, NULL,};
1737 rc = lustre_pack_reply(req, 1, &size, NULL);
1738 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK)) {
1739 CERROR("mds: out of memory\n");
1740 GOTO(out, rc = -ENOMEM);
1743 rsd = lustre_swab_mds_secdesc(req, MDS_REQ_SECDESC_OFF);
1745 CERROR("Can't unpack security desc\n");
1746 GOTO (out, rc = -EFAULT);
1749 body = lustre_swab_reqbuf(req, offset, sizeof(*body),
1750 lustre_swab_mds_body);
1752 CERROR("Can't unpack body\n");
1753 GOTO (out, rc = -EFAULT);
1756 rc = mds_init_ucred(&uc, req, rsd);
1761 push_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1762 de = mds_id2dentry(obd, &body->id1, &mnt);
1764 GOTO(out_pop, rc = PTR_ERR(de));
1766 CDEBUG(D_INODE, "ino %lu\n", de->d_inode->i_ino);
1768 file = dentry_open(de, mnt, O_RDONLY | O_LARGEFILE);
1769 /* note: in case of an error, dentry_open puts dentry */
1771 GOTO(out_pop, rc = PTR_ERR(file));
1773 /* body->size is actually the offset -eeb */
1774 if ((body->size & (de->d_inode->i_blksize - 1)) != 0) {
1775 CERROR("offset "LPU64" not on a block boundary of %lu\n",
1776 body->size, de->d_inode->i_blksize);
1777 GOTO(out_file, rc = -EFAULT);
1780 /* body->nlink is actually the #bytes to read -eeb */
1781 if (body->nlink & (de->d_inode->i_blksize - 1)) {
1782 CERROR("size %u is not multiple of blocksize %lu\n",
1783 body->nlink, de->d_inode->i_blksize);
1784 GOTO(out_file, rc = -EFAULT);
1787 repbody = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*repbody));
1788 repbody->size = file->f_dentry->d_inode->i_size;
1789 repbody->valid = OBD_MD_FLSIZE;
1791 /* to make this asynchronous make sure that the handling function
1792 doesn't send a reply when this function completes. Instead a
1793 callback function would send the reply */
1794 /* body->size is actually the offset -eeb */
1795 rc = mds_sendpage(req, file, body->size, body->nlink);
1799 filp_close(file, 0);
1801 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1803 mds_exit_ucred(&uc);
1804 req->rq_status = rc;
1808 /* update master MDS ID, which is stored in local inode EA. */
1809 int mds_update_mid(struct obd_device *obd, struct lustre_id *id,
1810 void *data, int data_len)
1812 struct mds_obd *mds = &obd->u.mds;
1813 struct dentry *dentry;
1821 dentry = mds_id2dentry(obd, id, NULL);
1823 GOTO(out, rc = PTR_ERR(dentry));
1825 if (!dentry->d_inode) {
1826 CERROR("Can't find object "DLID4".\n",
1828 GOTO(out_dentry, rc = -EINVAL);
1831 handle = fsfilt_start(obd, dentry->d_inode,
1832 FSFILT_OP_SETATTR, NULL);
1834 GOTO(out_dentry, rc = PTR_ERR(handle));
1836 rc = mds_update_inode_mid(obd, dentry->d_inode, handle,
1837 (struct lustre_id *)data);
1839 CERROR("Can't update inode "DLID4" master id, "
1840 "error = %d.\n", OLID4(id), rc);
1841 GOTO(out_commit, rc);
1846 fsfilt_commit(obd, mds->mds_sb, dentry->d_inode,
1853 EXPORT_SYMBOL(mds_update_mid);
1855 /* read master MDS ID, which is stored in local inode EA. */
1856 int mds_read_mid(struct obd_device *obd, struct lustre_id *id,
1857 void *data, int data_len)
1859 struct dentry *dentry;
1866 dentry = mds_id2dentry(obd, id, NULL);
1868 GOTO(out, rc = PTR_ERR(dentry));
1870 if (!dentry->d_inode) {
1871 CERROR("Can't find object "DLID4".\n",
1873 GOTO(out_dentry, rc = -EINVAL);
1876 down(&dentry->d_inode->i_sem);
1877 rc = mds_read_inode_mid(obd, dentry->d_inode,
1878 (struct lustre_id *)data);
1879 up(&dentry->d_inode->i_sem);
1881 CERROR("Can't read inode "DLID4" master id, "
1882 "error = %d.\n", OLID4(id), rc);
1883 GOTO(out_dentry, rc);
1892 EXPORT_SYMBOL(mds_read_mid);
1894 int mds_read_md(struct obd_device *obd, struct lustre_id *id,
1895 char **data, int *datalen)
1897 struct dentry *dentry;
1898 struct mds_obd *mds = &obd->u.mds;
1899 int rc = 0, mea = 0;
1906 dentry = mds_id2dentry(obd, id, NULL);
1908 GOTO(out, rc = PTR_ERR(dentry));
1910 if (!dentry->d_inode) {
1911 CERROR("Can't find object "DLID4".\n",
1913 GOTO(out_dentry, rc = -EINVAL);
1915 if (S_ISDIR(dentry->d_inode->i_mode)) {
1916 *datalen = obd_packmd(mds->mds_md_exp, NULL, NULL);
1919 *datalen = obd_packmd(mds->mds_dt_exp, NULL, NULL);
1922 OBD_ALLOC(ea, *datalen);
1925 GOTO(out_dentry, rc = PTR_ERR(dentry));
1928 down(&dentry->d_inode->i_sem);
1929 rc = fsfilt_get_md(obd, dentry->d_inode, *data, *datalen,
1930 (mea ? EA_MEA : EA_LOV));
1931 up(&dentry->d_inode->i_sem);
1934 CERROR("Error %d reading eadata for ino %lu\n",
1935 rc, dentry->d_inode->i_ino);
1941 EXPORT_SYMBOL(mds_read_md);
1943 int mds_reint(struct ptlrpc_request *req, int offset,
1944 struct lustre_handle *lockh)
1946 struct mds_update_record *rec;
1947 struct mds_req_sec_desc *rsd;
1951 OBD_ALLOC(rec, sizeof(*rec));
1955 rsd = lustre_swab_mds_secdesc(req, MDS_REQ_SECDESC_OFF);
1957 CERROR("Can't unpack security desc\n");
1958 GOTO(out, rc = -EFAULT);
1961 rc = mds_update_unpack(req, offset, rec);
1962 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNPACK)) {
1963 CERROR("invalid record\n");
1964 GOTO(out, req->rq_status = -EINVAL);
1967 rc = mds_init_ucred(&rec->ur_uc, req, rsd);
1972 /* rc will be used to interrupt a for loop over multiple records */
1973 rc = mds_reint_rec(rec, offset, req, lockh);
1976 mds_exit_ucred(&rec->ur_uc);
1977 OBD_FREE(rec, sizeof(*rec));
1981 static int mds_filter_recovery_request(struct ptlrpc_request *req,
1982 struct obd_device *obd, int *process)
1984 switch (req->rq_reqmsg->opc) {
1985 case MDS_CONNECT: /* This will never get here, but for completeness. */
1986 case OST_CONNECT: /* This will never get here, but for completeness. */
1987 case MDS_DISCONNECT:
1988 case OST_DISCONNECT:
1993 case MDS_SYNC: /* used in unmounting */
1998 *process = target_queue_recovery_request(req, obd);
2002 DEBUG_REQ(D_ERROR, req, "not permitted during recovery");
2004 /* XXX what should we set rq_status to here? */
2005 req->rq_status = -EAGAIN;
2006 RETURN(ptlrpc_error(req));
2010 static char *reint_names[] = {
2011 [REINT_SETATTR] "setattr",
2012 [REINT_CREATE] "create",
2013 [REINT_LINK] "link",
2014 [REINT_UNLINK] "unlink",
2015 [REINT_RENAME] "rename",
2016 [REINT_OPEN] "open",
2019 #define FILTER_VALID_FLAGS (OBD_MD_FLTYPE | OBD_MD_FLMODE | OBD_MD_FLGENER | \
2020 OBD_MD_FLSIZE | OBD_MD_FLBLOCKS | OBD_MD_FLBLKSZ| \
2021 OBD_MD_FLATIME | OBD_MD_FLMTIME | OBD_MD_FLCTIME| \
2024 static void reconstruct_create(struct ptlrpc_request *req)
2026 struct mds_export_data *med = &req->rq_export->exp_mds_data;
2027 struct mds_client_data *mcd = med->med_mcd;
2028 struct dentry *dentry;
2029 struct ost_body *body;
2030 struct lustre_id id;
2034 /* copy rc, transno and disp; steal locks */
2035 mds_req_from_mcd(req, mcd);
2036 if (req->rq_status) {
2044 id_ino(&id) = mcd->mcd_last_data;
2045 LASSERT(id_ino(&id) != 0);
2047 dentry = mds_id2dentry(req2obd(req), &id, NULL);
2048 if (IS_ERR(dentry)) {
2049 CERROR("can't find inode "LPU64"\n", id_ino(&id));
2050 req->rq_status = PTR_ERR(dentry);
2055 CWARN("reconstruct reply for x"LPU64" (remote ino) "LPU64" -> %lu/%u\n",
2056 req->rq_xid, id_ino(&id), dentry->d_inode->i_ino,
2057 dentry->d_inode->i_generation);
2059 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
2060 obdo_from_inode(&body->oa, dentry->d_inode, FILTER_VALID_FLAGS);
2061 body->oa.o_id = dentry->d_inode->i_ino;
2062 body->oa.o_generation = dentry->d_inode->i_generation;
2063 body->oa.o_valid |= OBD_MD_FLID | OBD_MD_FLGENER;
2065 down(&dentry->d_inode->i_sem);
2066 rc = mds_read_inode_sid(req2obd(req), dentry->d_inode, &id);
2067 up(&dentry->d_inode->i_sem);
2069 CERROR("Can't read inode self id, inode %lu, "
2070 "rc %d\n", dentry->d_inode->i_ino, rc);
2074 body->oa.o_fid = id_fid(&id);
2075 body->oa.o_mds = id_group(&id);
2081 static int mds_inode_init_acl(struct obd_device *obd, void *handle,
2082 struct dentry *de, void *xattr, int xattr_size)
2084 struct inode *inode = de->d_inode;
2085 struct posix_acl *acl;
2092 LASSERT(xattr_size > 0);
2094 if (!inode->i_op->getxattr || !inode->i_op->setxattr) {
2095 CERROR("backend fs dosen't support xattr\n");
2099 /* set default acl */
2100 if (S_ISDIR(inode->i_mode)) {
2101 rc = inode->i_op->setxattr(de, XATTR_NAME_ACL_DEFAULT,
2102 xattr, xattr_size, 0);
2104 CERROR("set default acl err: %d\n", rc);
2109 /* set access acl */
2110 acl = posix_acl_from_xattr(xattr, xattr_size);
2111 if (acl == NULL || IS_ERR(acl)) {
2112 CERROR("insane attr data\n");
2113 return PTR_ERR(acl);
2116 if (posix_acl_valid(acl)) {
2117 CERROR("default acl not valid: %d\n", rc);
2122 mode = inode->i_mode;
2123 rc = posix_acl_create_masq(acl, &mode);
2125 CERROR("create masq err %d\n", rc);
2129 if (inode->i_mode != mode) {
2130 struct iattr iattr = { .ia_valid = ATTR_MODE,
2134 rc2 = fsfilt_setattr(obd, de, handle, &iattr, 0);
2136 CERROR("setattr mode err: %d\n", rc2);
2143 /* we didn't change acl except mode bits of some
2144 * entries, so should be fit into original size.
2146 rc = posix_acl_to_xattr(acl, xattr, xattr_size);
2149 rc = inode->i_op->setxattr(de, XATTR_NAME_ACL_ACCESS,
2150 xattr, xattr_size, 0);
2152 CERROR("set access acl err: %d\n", rc);
2155 posix_acl_release(acl);
2159 static int mdt_obj_create(struct ptlrpc_request *req)
2161 struct obd_device *obd = req->rq_export->exp_obd;
2162 struct mds_obd *mds = &obd->u.mds;
2163 struct ost_body *body, *repbody;
2166 char idname[LL_ID_NAMELEN];
2167 int size = sizeof(*repbody);
2168 struct inode *parent_inode;
2169 struct lvfs_run_ctxt saved;
2170 int rc, cleanup_phase = 0;
2171 struct dentry *new = NULL;
2172 struct dentry_params dp;
2173 int mealen, flags = 0;
2174 struct lvfs_ucred uc;
2175 struct lustre_id id;
2177 void *handle = NULL;
2178 unsigned long cr_inum = 0;
2181 DEBUG_REQ(D_HA, req, "create remote object");
2182 parent_inode = mds->mds_unnamed_dir->d_inode;
2184 body = lustre_swab_reqbuf(req, 0, sizeof(*body),
2185 lustre_swab_ost_body);
2189 /* acl data is packed transparently, no swab here */
2190 LASSERT(req->rq_reqmsg->bufcount >= 2);
2191 acl_size = req->rq_reqmsg->buflens[1];
2193 acl = lustre_msg_buf(req->rq_reqmsg, 1, acl_size);
2195 CERROR("No default acl buf?\n");
2200 rc = lustre_pack_reply(req, 1, &size, NULL);
2204 MDS_CHECK_RESENT(req, reconstruct_create(req));
2207 uc.luc_ginfo = NULL;
2208 uc.luc_uid = body->oa.o_uid;
2209 uc.luc_gid = body->oa.o_gid;
2210 uc.luc_fsuid = body->oa.o_uid;
2211 uc.luc_fsgid = body->oa.o_gid;
2213 push_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
2214 repbody = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*repbody));
2216 /* in REPLAY case inum should be given (client or other MDS fills it) */
2217 if (body->oa.o_id && ((body->oa.o_flags & OBD_FL_RECREATE_OBJS) ||
2218 (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY))) {
2220 * this is re-create request from MDS holding directory name.
2221 * we have to lookup given ino/gen first. if it exists (good
2222 * case) then there is nothing to do. if it does not then we
2223 * have to recreate it.
2225 id_ino(&id) = body->oa.o_id;
2226 id_gen(&id) = body->oa.o_generation;
2228 new = mds_id2dentry(obd, &id, NULL);
2229 if (!IS_ERR(new) && new->d_inode) {
2230 struct lustre_id sid;
2232 CWARN("mkdir() repairing is on its way: %lu/%lu\n",
2233 (unsigned long)id_ino(&id), (unsigned long)id_gen(&id));
2235 obdo_from_inode(&repbody->oa, new->d_inode,
2236 FILTER_VALID_FLAGS);
2238 repbody->oa.o_id = new->d_inode->i_ino;
2239 repbody->oa.o_generation = new->d_inode->i_generation;
2240 repbody->oa.o_valid |= OBD_MD_FLID | OBD_MD_FLGENER;
2243 down(&new->d_inode->i_sem);
2244 rc = mds_read_inode_sid(obd, new->d_inode, &sid);
2245 up(&new->d_inode->i_sem);
2247 CERROR("Can't read inode self id "
2248 "inode %lu, rc %d.\n",
2249 new->d_inode->i_ino, rc);
2253 repbody->oa.o_fid = id_fid(&sid);
2254 repbody->oa.o_mds = id_group(&sid);
2255 LASSERT(id_fid(&sid) != 0);
2258 * here we could use fid passed in body->oa.o_fid and
2259 * thus avoid mds_read_inode_sid().
2261 cr_inum = new->d_inode->i_ino;
2262 GOTO(cleanup, rc = 0);
2266 down(&parent_inode->i_sem);
2267 handle = fsfilt_start(obd, parent_inode, FSFILT_OP_MKDIR, NULL);
2268 if (IS_ERR(handle)) {
2269 up(&parent_inode->i_sem);
2270 CERROR("fsfilt_start() failed, rc = %d\n",
2271 (int)PTR_ERR(handle));
2272 GOTO(cleanup, rc = PTR_ERR(handle));
2274 cleanup_phase = 1; /* transaction */
2277 rc = sprintf(idname, "%u.%u", ll_insecure_random_int(), current->pid);
2278 new = lookup_one_len(idname, mds->mds_unnamed_dir, rc);
2280 CERROR("%s: can't lookup new inode (%s) for mkdir: %d\n",
2281 obd->obd_name, idname, (int) PTR_ERR(new));
2282 fsfilt_commit(obd, mds->mds_sb, new->d_inode, handle, 0);
2283 up(&parent_inode->i_sem);
2284 RETURN(PTR_ERR(new));
2285 } else if (new->d_inode) {
2286 CERROR("%s: name exists. repeat\n", obd->obd_name);
2290 new->d_fsdata = (void *)&dp;
2294 if ((lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) ||
2295 (body->oa.o_flags & OBD_FL_RECREATE_OBJS)) {
2296 LASSERT(body->oa.o_id != 0);
2297 dp.p_inum = body->oa.o_id;
2298 DEBUG_REQ(D_HA, req, "replay create obj %lu/%lu",
2299 (unsigned long)body->oa.o_id,
2300 (unsigned long)body->oa.o_generation);
2303 rc = vfs_mkdir(parent_inode, new, body->oa.o_mode);
2306 rc = mds_inode_init_acl(obd, handle, new,
2309 up(&parent_inode->i_sem);
2313 if ((body->oa.o_flags & OBD_FL_RECREATE_OBJS) ||
2314 lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
2315 new->d_inode->i_generation = body->oa.o_generation;
2316 mark_inode_dirty(new->d_inode);
2319 * avoiding asserts in cache flush case, as
2320 * @body->oa.o_id should be zero.
2322 if (body->oa.o_id) {
2323 LASSERTF(body->oa.o_id == new->d_inode->i_ino,
2324 "BUG 3550: failed to recreate obj "
2325 LPU64" -> %lu\n", body->oa.o_id,
2326 new->d_inode->i_ino);
2328 LASSERTF(body->oa.o_generation ==
2329 new->d_inode->i_generation,
2330 "BUG 3550: failed to recreate obj/gen "
2331 LPU64"/%u -> %lu/%u\n", body->oa.o_id,
2332 body->oa.o_generation,
2333 new->d_inode->i_ino,
2334 new->d_inode->i_generation);
2338 obdo_from_inode(&repbody->oa, new->d_inode, FILTER_VALID_FLAGS);
2339 repbody->oa.o_id = new->d_inode->i_ino;
2340 repbody->oa.o_generation = new->d_inode->i_generation;
2341 repbody->oa.o_valid |= OBD_MD_FLID | OBD_MD_FLGENER | OBD_MD_FID;
2343 if ((body->oa.o_flags & OBD_FL_RECREATE_OBJS) ||
2344 lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
2345 id_group(&id) = mds->mds_num;
2347 LASSERT(body->oa.o_fid != 0);
2348 id_fid(&id) = body->oa.o_fid;
2350 LASSERT(body->oa.o_id != 0);
2351 id_ino(&id) = repbody->oa.o_id;
2352 id_gen(&id) = repbody->oa.o_generation;
2354 down(&new->d_inode->i_sem);
2355 rc = mds_update_inode_sid(obd, new->d_inode, handle, &id);
2356 up(&new->d_inode->i_sem);
2359 * make sure, that fid is up-to-date.
2361 mds_set_last_fid(obd, id_fid(&id));
2364 * allocate new sid, as object is created from scratch
2365 * and this is not replay.
2367 down(&new->d_inode->i_sem);
2368 rc = mds_alloc_inode_sid(obd, new->d_inode, handle, &id);
2369 up(&new->d_inode->i_sem);
2372 CERROR("Can't update lustre ID for inode %lu, "
2373 "error = %d\n", new->d_inode->i_ino, rc);
2377 /* initializing o_fid after it is allocated. */
2378 repbody->oa.o_fid = id_fid(&id);
2379 repbody->oa.o_mds = id_group(&id);
2381 rc = fsfilt_del_dir_entry(obd, new);
2382 up(&parent_inode->i_sem);
2384 CERROR("can't remove name for object: %d\n", rc);
2388 cleanup_phase = 2; /* created directory object */
2390 CDEBUG(D_OTHER, "created dirobj: %lu/%lu mode %o\n",
2391 (unsigned long)new->d_inode->i_ino,
2392 (unsigned long)new->d_inode->i_generation,
2393 (unsigned)new->d_inode->i_mode);
2394 cr_inum = new->d_inode->i_ino;
2396 up(&parent_inode->i_sem);
2397 CERROR("%s: can't create dirobj: %d\n", obd->obd_name, rc);
2401 if (body->oa.o_valid & OBD_MD_FLID) {
2402 /* this is new object for splitted dir. We have to prevent
2403 * recursive splitting on it -bzzz */
2404 mealen = obd_size_diskmd(mds->mds_md_exp, NULL);
2406 OBD_ALLOC(mea, mealen);
2408 GOTO(cleanup, rc = -ENOMEM);
2410 mea->mea_magic = MEA_MAGIC_ALL_CHARS;
2411 mea->mea_master = 0;
2414 down(&new->d_inode->i_sem);
2415 rc = fsfilt_set_md(obd, new->d_inode, handle,
2416 mea, mealen, EA_MEA);
2417 up(&new->d_inode->i_sem);
2419 CERROR("fsfilt_set_md() failed, "
2422 OBD_FREE(mea, mealen);
2424 CDEBUG(D_OTHER, "%s: mark non-splittable %lu/%u - %d\n",
2425 obd->obd_name, new->d_inode->i_ino,
2426 new->d_inode->i_generation, flags);
2427 } else if (body->oa.o_easize) {
2428 /* we pass LCK_EX to split routine to signal that we have
2429 * exclusive access to the directory. simple because nobody
2430 * knows it already exists -bzzz */
2431 rc = mds_try_to_split_dir(obd, new, NULL,
2432 body->oa.o_easize, LCK_EX);
2434 CERROR("Can't split directory %lu, error = %d.\n",
2435 new->d_inode->i_ino, rc);
2443 switch (cleanup_phase) {
2444 case 2: /* object has been created, but we'll may want to replay it later */
2446 ptlrpc_require_repack(req);
2447 case 1: /* transaction */
2448 rc = mds_finish_transno(mds, parent_inode, handle,
2453 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
2457 static int mdt_get_info(struct ptlrpc_request *req)
2459 struct obd_export *exp = req->rq_export;
2464 key = lustre_msg_buf(req->rq_reqmsg, 0, 1);
2466 DEBUG_REQ(D_HA, req, "no get_info key");
2469 keylen = req->rq_reqmsg->buflens[0];
2471 if ((keylen < strlen("mdsize") || strcmp(key, "mdsize") != 0) &&
2472 (keylen < strlen("mdsnum") || strcmp(key, "mdsnum") != 0) &&
2473 (keylen < strlen("rootid") || strcmp(key, "rootid") != 0))
2476 if (keylen >= strlen("rootid") && !strcmp(key, "rootid")) {
2477 struct lustre_id *reply;
2478 int size = sizeof(*reply);
2480 rc = lustre_pack_reply(req, 1, &size, NULL);
2484 reply = lustre_msg_buf(req->rq_repmsg, 0, size);
2485 rc = obd_get_info(exp, keylen, key, (__u32 *)&size, reply);
2488 int size = sizeof(*reply);
2490 rc = lustre_pack_reply(req, 1, &size, NULL);
2494 reply = lustre_msg_buf(req->rq_repmsg, 0, size);
2495 rc = obd_get_info(exp, keylen, key, (__u32 *)&size, reply);
2498 req->rq_repmsg->status = 0;
2502 static int mds_set_info(struct obd_export *exp, __u32 keylen,
2503 void *key, __u32 vallen, void *val)
2505 struct obd_device *obd;
2506 struct mds_obd *mds;
2510 obd = class_exp2obd(exp);
2512 CDEBUG(D_IOCTL, "invalid client cookie "LPX64"\n",
2513 exp->exp_handle.h_cookie);
2518 if (keylen >= strlen("mds_type") &&
2519 memcmp(key, "mds_type", keylen) == 0) {
2523 CDEBUG(D_IOCTL, "set mds type to %x\n", *(int*)val);
2525 mds->mds_obd_type = *(int*)val;
2526 group = FILTER_GROUP_FIRST_MDS + mds->mds_obd_type;
2527 valsize = sizeof(group);
2529 /* mds number has been changed, so the corresponding obdfilter
2530 * exp need to be changed too. */
2531 rc = obd_set_info(mds->mds_dt_exp, strlen("mds_conn"),
2532 "mds_conn", valsize, &group);
2535 CDEBUG(D_IOCTL, "invalid key\n");
2539 static int mdt_set_info(struct ptlrpc_request *req)
2542 struct obd_export *exp = req->rq_export;
2543 int keylen, rc = 0, vallen;
2546 key = lustre_msg_buf(req->rq_reqmsg, 0, 1);
2548 DEBUG_REQ(D_HA, req, "no set_info key");
2551 keylen = req->rq_reqmsg->buflens[0];
2553 if (keylen == strlen("mds_type") &&
2554 memcmp(key, "mds_type", keylen) == 0) {
2555 rc = lustre_pack_reply(req, 0, NULL, NULL);
2559 val = lustre_msg_buf(req->rq_reqmsg, 1, 0);
2560 vallen = req->rq_reqmsg->buflens[1];
2562 rc = obd_set_info(exp, keylen, key, vallen, val);
2563 req->rq_repmsg->status = 0;
2566 CDEBUG(D_IOCTL, "invalid key\n");
2570 static void mds_revoke_export_locks(struct obd_export *exp)
2572 struct ldlm_namespace *ns = exp->exp_obd->obd_namespace;
2573 struct list_head *locklist = &exp->exp_ldlm_data.led_held_locks;
2574 struct ldlm_lock *lock, *next;
2575 struct ldlm_lock_desc desc;
2577 if (!exp->u.eu_mds_data.med_remote)
2581 l_lock(&ns->ns_lock);
2582 list_for_each_entry_safe(lock, next, locklist, l_export_chain) {
2583 if (lock->l_req_mode != lock->l_granted_mode)
2586 LASSERT(lock->l_resource);
2587 if (lock->l_resource->lr_type != LDLM_IBITS &&
2588 lock->l_resource->lr_type != LDLM_PLAIN)
2591 if (lock->l_flags & LDLM_FL_AST_SENT)
2594 lock->l_flags |= LDLM_FL_AST_SENT;
2596 /* the desc just pretend to exclusive */
2597 ldlm_lock2desc(lock, &desc);
2598 desc.l_req_mode = LCK_EX;
2599 desc.l_granted_mode = 0;
2601 lock->l_blocking_ast(lock, &desc, NULL, LDLM_CB_BLOCKING);
2603 l_unlock(&ns->ns_lock);
2607 static int mds_msg_check_version(struct lustre_msg *msg)
2613 case MDS_DISCONNECT:
2615 rc = lustre_msg_check_version(msg, LUSTRE_OBD_VERSION);
2617 CERROR("bad opc %u version %08x, expecting %08x\n",
2618 msg->opc, msg->version, LUSTRE_OBD_VERSION);
2623 case MDS_GETATTR_LOCK:
2627 case MDS_DONE_WRITING:
2630 rc = lustre_msg_check_version(msg, LUSTRE_MDS_VERSION);
2632 CERROR("bad opc %u version %08x, expecting %08x\n",
2633 msg->opc, msg->version, LUSTRE_MDS_VERSION);
2637 case LDLM_BL_CALLBACK:
2638 case LDLM_CP_CALLBACK:
2639 rc = lustre_msg_check_version(msg, LUSTRE_DLM_VERSION);
2641 CERROR("bad opc %u version %08x, expecting %08x\n",
2642 msg->opc, msg->version, LUSTRE_DLM_VERSION);
2644 case OBD_LOG_CANCEL:
2645 case LLOG_ORIGIN_HANDLE_OPEN:
2646 case LLOG_ORIGIN_HANDLE_NEXT_BLOCK:
2647 case LLOG_ORIGIN_HANDLE_PREV_BLOCK:
2648 case LLOG_ORIGIN_HANDLE_READ_HEADER:
2649 case LLOG_ORIGIN_HANDLE_CLOSE:
2651 rc = lustre_msg_check_version(msg, LUSTRE_LOG_VERSION);
2653 CERROR("bad opc %u version %08x, expecting %08x\n",
2654 msg->opc, msg->version, LUSTRE_LOG_VERSION);
2660 rc = lustre_msg_check_version(msg, LUSTRE_OBD_VERSION);
2662 CERROR("bad opc %u version %08x, expecting %08x\n",
2663 msg->opc, msg->version, LUSTRE_OBD_VERSION);
2666 case SEC_INIT_CONTINUE:
2671 CERROR("MDS unknown opcode %d\n", msg->opc);
2679 int mds_handle(struct ptlrpc_request *req)
2681 int should_process, fail = OBD_FAIL_MDS_ALL_REPLY_NET;
2682 struct obd_device *obd = NULL;
2683 struct mds_obd *mds = NULL; /* quell gcc overwarning */
2687 OBD_FAIL_RETURN(OBD_FAIL_MDS_ALL_REQUEST_NET | OBD_FAIL_ONCE, 0);
2689 rc = mds_msg_check_version(req->rq_reqmsg);
2691 CERROR("MDS drop mal-formed request\n");
2695 /* Security opc should NOT trigger any recovery events */
2696 if (req->rq_reqmsg->opc == SEC_INIT ||
2697 req->rq_reqmsg->opc == SEC_INIT_CONTINUE) {
2698 if (req->rq_export) {
2699 mds_req_add_idmapping(req,
2700 &req->rq_export->exp_mds_data);
2701 mds_revoke_export_locks(req->rq_export);
2704 } else if (req->rq_reqmsg->opc == SEC_FINI) {
2705 if (req->rq_export) {
2706 mds_req_del_idmapping(req,
2707 &req->rq_export->exp_mds_data);
2708 mds_revoke_export_locks(req->rq_export);
2713 LASSERT(current->journal_info == NULL);
2714 /* XXX identical to OST */
2715 if (req->rq_reqmsg->opc != MDS_CONNECT) {
2716 struct mds_export_data *med;
2719 if (req->rq_export == NULL) {
2720 CERROR("operation %d on unconnected MDS from %s\n",
2721 req->rq_reqmsg->opc,
2723 req->rq_status = -ENOTCONN;
2724 GOTO(out, rc = -ENOTCONN);
2727 med = &req->rq_export->exp_mds_data;
2728 obd = req->rq_export->exp_obd;
2731 /* sanity check: if the xid matches, the request must
2732 * be marked as a resent or replayed */
2733 if (req->rq_xid == le64_to_cpu(med->med_mcd->mcd_last_xid) ||
2734 req->rq_xid == le64_to_cpu(med->med_mcd->mcd_last_close_xid)) {
2735 LASSERTF(lustre_msg_get_flags(req->rq_reqmsg) &
2736 (MSG_RESENT | MSG_REPLAY),
2737 "rq_xid "LPU64" matches last_xid, "
2738 "expected RESENT flag\n",
2741 /* else: note the opposite is not always true; a
2742 * RESENT req after a failover will usually not match
2743 * the last_xid, since it was likely never
2744 * committed. A REPLAYed request will almost never
2745 * match the last xid, however it could for a
2746 * committed, but still retained, open. */
2748 spin_lock_bh(&obd->obd_processing_task_lock);
2749 recovering = obd->obd_recovering;
2750 spin_unlock_bh(&obd->obd_processing_task_lock);
2752 rc = mds_filter_recovery_request(req, obd,
2754 if (rc || should_process == 0) {
2756 } else if (should_process < 0) {
2757 req->rq_status = should_process;
2758 rc = ptlrpc_error(req);
2764 switch (req->rq_reqmsg->opc) {
2766 DEBUG_REQ(D_INODE, req, "connect");
2767 OBD_FAIL_RETURN(OBD_FAIL_MDS_CONNECT_NET, 0);
2768 rc = target_handle_connect(req);
2770 struct mds_export_data *med;
2772 LASSERT(req->rq_export);
2773 med = &req->rq_export->u.eu_mds_data;
2774 mds_init_export_data(req, med);
2775 mds_req_add_idmapping(req, med);
2777 /* Now that we have an export, set mds. */
2778 obd = req->rq_export->exp_obd;
2779 mds = mds_req2mds(req);
2783 case MDS_DISCONNECT:
2784 DEBUG_REQ(D_INODE, req, "disconnect");
2785 OBD_FAIL_RETURN(OBD_FAIL_MDS_DISCONNECT_NET, 0);
2786 rc = target_handle_disconnect(req);
2787 req->rq_status = rc; /* superfluous? */
2791 DEBUG_REQ(D_INODE, req, "getstatus");
2792 OBD_FAIL_RETURN(OBD_FAIL_MDS_GETSTATUS_NET, 0);
2793 rc = mds_getstatus(req);
2797 DEBUG_REQ(D_INODE, req, "getattr");
2798 OBD_FAIL_RETURN(OBD_FAIL_MDS_GETATTR_NET, 0);
2799 rc = mds_getattr(req, MDS_REQ_REC_OFF);
2802 case MDS_GETATTR_LOCK: {
2803 struct lustre_handle lockh;
2804 DEBUG_REQ(D_INODE, req, "getattr_lock");
2805 OBD_FAIL_RETURN(OBD_FAIL_MDS_GETATTR_LOCK_NET, 0);
2807 /* If this request gets a reconstructed reply, we won't be
2808 * acquiring any new locks in mds_getattr_lock, so we don't
2812 rc = mds_getattr_lock(req, MDS_REQ_REC_OFF, &lockh,
2813 MDS_INODELOCK_UPDATE);
2814 /* this non-intent call (from an ioctl) is special */
2815 req->rq_status = rc;
2816 if (rc == 0 && lockh.cookie)
2817 ldlm_lock_decref(&lockh, LCK_PR);
2821 DEBUG_REQ(D_INODE, req, "statfs");
2822 OBD_FAIL_RETURN(OBD_FAIL_MDS_STATFS_NET, 0);
2823 rc = mds_statfs(req);
2827 DEBUG_REQ(D_INODE, req, "readpage");
2828 OBD_FAIL_RETURN(OBD_FAIL_MDS_READPAGE_NET, 0);
2829 rc = mds_readpage(req, MDS_REQ_REC_OFF);
2831 if (OBD_FAIL_CHECK_ONCE(OBD_FAIL_MDS_SENDPAGE)) {
2832 if (req->rq_reply_state) {
2833 lustre_free_reply_state (req->rq_reply_state);
2834 req->rq_reply_state = NULL;
2841 __u32 *opcp = lustre_msg_buf(req->rq_reqmsg, MDS_REQ_REC_OFF,
2844 int size[3] = {sizeof(struct mds_body), mds->mds_max_mdsize,
2845 mds->mds_max_cookiesize};
2848 /* NB only peek inside req now; mds_reint() will swab it */
2850 CERROR ("Can't inspect opcode\n");
2855 if (lustre_msg_swabbed (req->rq_reqmsg))
2858 DEBUG_REQ(D_INODE, req, "reint %d (%s)", opc,
2859 (opc < sizeof(reint_names) / sizeof(reint_names[0]) ||
2860 reint_names[opc] == NULL) ? reint_names[opc] :
2863 OBD_FAIL_RETURN(OBD_FAIL_MDS_REINT_NET, 0);
2865 if (opc == REINT_UNLINK || opc == REINT_RENAME)
2867 else if (opc == REINT_OPEN)
2872 rc = lustre_pack_reply(req, bufcount, size, NULL);
2876 rc = mds_reint(req, MDS_REQ_REC_OFF, NULL);
2877 fail = OBD_FAIL_MDS_REINT_NET_REP;
2882 DEBUG_REQ(D_INODE, req, "close");
2883 OBD_FAIL_RETURN(OBD_FAIL_MDS_CLOSE_NET, 0);
2884 rc = mds_close(req, MDS_REQ_REC_OFF);
2887 case MDS_DONE_WRITING:
2888 DEBUG_REQ(D_INODE, req, "done_writing");
2889 OBD_FAIL_RETURN(OBD_FAIL_MDS_DONE_WRITING_NET, 0);
2890 rc = mds_done_writing(req, MDS_REQ_REC_OFF);
2894 DEBUG_REQ(D_INODE, req, "pin");
2895 OBD_FAIL_RETURN(OBD_FAIL_MDS_PIN_NET, 0);
2896 rc = mds_pin(req, MDS_REQ_REC_OFF);
2900 DEBUG_REQ(D_INODE, req, "sync");
2901 OBD_FAIL_RETURN(OBD_FAIL_MDS_SYNC_NET, 0);
2902 rc = mds_sync(req, MDS_REQ_REC_OFF);
2906 DEBUG_REQ(D_INODE, req, "ping");
2907 rc = target_handle_ping(req);
2910 case OBD_LOG_CANCEL:
2911 CDEBUG(D_INODE, "log cancel\n");
2912 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOG_CANCEL_NET, 0);
2913 rc = -ENOTSUPP; /* la la la */
2917 DEBUG_REQ(D_INODE, req, "enqueue");
2918 OBD_FAIL_RETURN(OBD_FAIL_LDLM_ENQUEUE, 0);
2919 rc = ldlm_handle_enqueue(req, ldlm_server_completion_ast,
2920 ldlm_server_blocking_ast, NULL);
2921 fail = OBD_FAIL_LDLM_REPLY;
2924 DEBUG_REQ(D_INODE, req, "convert");
2925 OBD_FAIL_RETURN(OBD_FAIL_LDLM_CONVERT, 0);
2926 rc = ldlm_handle_convert(req);
2928 case LDLM_BL_CALLBACK:
2929 case LDLM_CP_CALLBACK:
2930 DEBUG_REQ(D_INODE, req, "callback");
2931 CERROR("callbacks should not happen on MDS\n");
2933 OBD_FAIL_RETURN(OBD_FAIL_LDLM_BL_CALLBACK, 0);
2935 case LLOG_ORIGIN_HANDLE_OPEN:
2936 DEBUG_REQ(D_INODE, req, "llog_init");
2937 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
2938 rc = llog_origin_handle_open(req);
2940 case LLOG_ORIGIN_HANDLE_NEXT_BLOCK:
2941 DEBUG_REQ(D_INODE, req, "llog next block");
2942 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
2943 rc = llog_origin_handle_next_block(req);
2945 case LLOG_ORIGIN_HANDLE_PREV_BLOCK:
2946 DEBUG_REQ(D_INODE, req, "llog prev block");
2947 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
2948 rc = llog_origin_handle_prev_block(req);
2950 case LLOG_ORIGIN_HANDLE_READ_HEADER:
2951 DEBUG_REQ(D_INODE, req, "llog read header");
2952 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
2953 rc = llog_origin_handle_read_header(req);
2955 case LLOG_ORIGIN_HANDLE_CLOSE:
2956 DEBUG_REQ(D_INODE, req, "llog close");
2957 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
2958 rc = llog_origin_handle_close(req);
2961 DEBUG_REQ(D_INODE, req, "ost_create");
2962 rc = mdt_obj_create(req);
2965 DEBUG_REQ(D_INODE, req, "get_info");
2966 rc = mdt_get_info(req);
2969 DEBUG_REQ(D_INODE, req, "set_info");
2970 rc = mdt_set_info(req);
2973 CDEBUG(D_INODE, "write\n");
2974 OBD_FAIL_RETURN(OBD_FAIL_OST_BRW_NET, 0);
2975 rc = ost_brw_write(req, NULL);
2976 LASSERT(current->journal_info == NULL);
2977 /* mdt_brw sends its own replies */
2981 DEBUG_REQ(D_INODE, req, "llog catinfo");
2982 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
2983 rc = llog_catinfo(req);
2986 req->rq_status = -ENOTSUPP;
2987 rc = ptlrpc_error(req);
2991 LASSERT(current->journal_info == NULL);
2995 /* If we're DISCONNECTing, the mds_export_data is already freed */
2996 if (!rc && req->rq_reqmsg->opc != MDS_DISCONNECT) {
2997 struct mds_export_data *med = &req->rq_export->exp_mds_data;
2998 struct obd_device *obd = list_entry(mds, struct obd_device,
3000 req->rq_repmsg->last_xid =
3001 le64_to_cpu(med->med_mcd->mcd_last_xid);
3003 if (!obd->obd_no_transno) {
3004 req->rq_repmsg->last_committed =
3005 obd->obd_last_committed;
3007 DEBUG_REQ(D_IOCTL, req,
3008 "not sending last_committed update");
3010 CDEBUG(D_INFO, "last_transno "LPU64", last_committed "LPU64
3012 mds->mds_last_transno, obd->obd_last_committed,
3018 target_send_reply(req, rc, fail);
3022 /* Update the server data on disk. This stores the new mount_count and also the
3023 * last_rcvd value to disk. If we don't have a clean shutdown, then the server
3024 * last_rcvd value may be less than that of the clients. This will alert us
3025 * that we may need to do client recovery.
3027 * Also assumes for mds_last_transno that we are not modifying it (no locking).
3029 int mds_update_server_data(struct obd_device *obd, int force_sync)
3031 struct mds_obd *mds = &obd->u.mds;
3032 struct mds_server_data *msd = mds->mds_server_data;
3033 struct file *filp = mds->mds_rcvd_filp;
3034 struct lvfs_run_ctxt saved;
3039 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
3040 msd->msd_last_transno = cpu_to_le64(mds->mds_last_transno);
3042 CDEBUG(D_SUPER, "MDS mount_count is "LPU64", last_transno is "LPU64"\n",
3043 mds->mds_mount_count, mds->mds_last_transno);
3044 rc = fsfilt_write_record(obd, filp, msd, sizeof(*msd), &off, force_sync);
3046 CERROR("error writing MDS server data: rc = %d\n", rc);
3047 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
3052 /* saves last allocated fid counter to file. */
3053 int mds_update_last_fid(struct obd_device *obd, void *handle,
3056 struct mds_obd *mds = &obd->u.mds;
3057 struct file *filp = mds->mds_fid_filp;
3058 struct lvfs_run_ctxt saved;
3064 spin_lock(&mds->mds_last_fid_lock);
3065 last_fid = mds->mds_last_fid;
3066 spin_unlock(&mds->mds_last_fid_lock);
3068 CDEBUG(D_SUPER, "MDS last_fid is #"LPU64"\n",
3072 fsfilt_add_journal_cb(obd, mds->mds_sb, last_fid,
3073 handle, mds_commit_last_fid_cb,
3077 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
3078 rc = fsfilt_write_record(obd, filp, &last_fid, sizeof(last_fid),
3080 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
3083 CERROR("error writing MDS last_fid #"LPU64
3084 ", err = %d\n", last_fid, rc);
3088 CDEBUG(D_SUPER, "wrote fid #"LPU64" at idx "
3089 "%llu: err = %d\n", last_fid, off, rc);
3094 void mds_set_last_fid(struct obd_device *obd, __u64 fid)
3096 struct mds_obd *mds = &obd->u.mds;
3098 spin_lock(&mds->mds_last_fid_lock);
3099 if (fid > mds->mds_last_fid)
3100 mds->mds_last_fid = fid;
3101 spin_unlock(&mds->mds_last_fid_lock);
3104 void mds_commit_last_transno_cb(struct obd_device *obd,
3105 __u64 transno, void *data,
3108 obd_transno_commit_cb(obd, transno, error);
3111 void mds_commit_last_fid_cb(struct obd_device *obd,
3112 __u64 fid, void *data,
3116 CERROR("%s: fid "LPD64" commit error: %d\n",
3117 obd->obd_name, fid, error);
3121 CDEBUG(D_HA, "%s: fid "LPD64" committed\n",
3122 obd->obd_name, fid);
3125 __u64 mds_alloc_fid(struct obd_device *obd)
3127 struct mds_obd *mds = &obd->u.mds;
3130 spin_lock(&mds->mds_last_fid_lock);
3131 fid = ++mds->mds_last_fid;
3132 spin_unlock(&mds->mds_last_fid_lock);
3138 * allocates new lustre_id on passed @inode and saves it to inode EA.
3140 int mds_alloc_inode_sid(struct obd_device *obd, struct inode *inode,
3141 void *handle, struct lustre_id *id)
3143 struct mds_obd *mds = &obd->u.mds;
3144 int alloc = 0, rc = 0;
3147 LASSERT(obd != NULL);
3148 LASSERT(inode != NULL);
3151 OBD_ALLOC(id, sizeof(*id));
3157 id_group(id) = mds->mds_num;
3158 id_fid(id) = mds_alloc_fid(obd);
3159 id_ino(id) = inode->i_ino;
3160 id_gen(id) = inode->i_generation;
3161 id_type(id) = (S_IFMT & inode->i_mode);
3163 rc = mds_update_inode_sid(obd, inode, handle, id);
3165 CERROR("Can't update inode FID EA, "
3170 OBD_FREE(id, sizeof(*id));
3175 * reads inode self id from inode EA. Probably later this should be replaced by
3176 * caching inode self id to avoid raeding it every time it is needed.
3178 int mds_read_inode_sid(struct obd_device *obd, struct inode *inode,
3179 struct lustre_id *id)
3184 LASSERT(id != NULL);
3185 LASSERT(obd != NULL);
3186 LASSERT(inode != NULL);
3188 rc = fsfilt_get_md(obd, inode, &id->li_fid,
3189 sizeof(id->li_fid), EA_SID);
3191 CERROR("fsfilt_get_md() failed, "
3204 /* updates inode self id in EA. */
3205 int mds_update_inode_sid(struct obd_device *obd, struct inode *inode,
3206 void *handle, struct lustre_id *id)
3211 LASSERT(id != NULL);
3212 LASSERT(obd != NULL);
3213 LASSERT(inode != NULL);
3215 rc = fsfilt_set_md(obd, inode, handle, &id->li_fid,
3216 sizeof(id->li_fid), EA_SID);
3218 CERROR("fsfilt_set_md() failed, rc = %d\n", rc);
3226 * reads inode id on master MDS. This is usualy done by CMOBD to update requests
3227 * to master MDS by correct store cookie, needed to find inode on master MDS
3230 int mds_read_inode_mid(struct obd_device *obd, struct inode *inode,
3231 struct lustre_id *id)
3236 LASSERT(id != NULL);
3237 LASSERT(obd != NULL);
3238 LASSERT(inode != NULL);
3240 rc = fsfilt_get_md(obd, inode, id, sizeof(*id), EA_MID);
3242 CERROR("fsfilt_get_md() failed, rc = %d\n", rc);
3255 * updates master inode id. Usualy this is done by CMOBD after an inode is
3256 * created and relationship between cache MDS and master one should be
3259 int mds_update_inode_mid(struct obd_device *obd, struct inode *inode,
3260 void *handle, struct lustre_id *id)
3265 LASSERT(id != NULL);
3266 LASSERT(obd != NULL);
3267 LASSERT(inode != NULL);
3269 rc = fsfilt_set_md(obd, inode, handle, id,
3270 sizeof(*id), EA_MID);
3272 CERROR("fsfilt_set_md() failed, "
3280 /* mount the file system (secretly) */
3281 static int mds_setup(struct obd_device *obd, obd_count len, void *buf)
3283 struct lustre_cfg* lcfg = buf;
3284 struct mds_obd *mds = &obd->u.mds;
3285 struct lvfs_obd_ctxt *lvfs_ctxt = NULL;
3286 char *options = NULL;
3287 struct vfsmount *mnt;
3293 if (lcfg->lcfg_bufcount < 3)
3294 RETURN(rc = -EINVAL);
3296 if (LUSTRE_CFG_BUFLEN(lcfg, 1) == 0 || LUSTRE_CFG_BUFLEN(lcfg, 2) == 0)
3297 RETURN(rc = -EINVAL);
3299 obd->obd_fsops = fsfilt_get_ops(lustre_cfg_string(lcfg, 2));
3300 if (IS_ERR(obd->obd_fsops))
3301 RETURN(rc = PTR_ERR(obd->obd_fsops));
3303 mds->mds_max_mdsize = sizeof(struct lov_mds_md);
3305 page = get_zeroed_page(GFP_KERNEL);
3309 options = (char *)page;
3312 * here we use "iopen_nopriv" hardcoded, because it affects MDS utility
3313 * and the rest of options are passed by mount options. Probably this
3314 * should be moved to somewhere else like startup scripts or lconf. */
3315 sprintf(options, "iopen_nopriv");
3317 if (LUSTRE_CFG_BUFLEN(lcfg, 4) > 0 && lustre_cfg_buf(lcfg, 4))
3318 sprintf(options + strlen(options), ",%s",
3319 lustre_cfg_string(lcfg, 4));
3321 /* we have to know mdsnum before touching underlying fs -bzzz */
3322 atomic_set(&mds->mds_open_count, 0);
3323 sema_init(&mds->mds_md_sem, 1);
3324 sema_init(&mds->mds_create_sem, 1);
3325 mds->mds_md_connected = 0;
3326 mds->mds_md_name = NULL;
3328 if (LUSTRE_CFG_BUFLEN(lcfg, 5) > 0 && lustre_cfg_buf(lcfg, 5) &&
3329 strncmp(lustre_cfg_string(lcfg, 5), "dumb", LUSTRE_CFG_BUFLEN(lcfg, 5))) {
3332 generate_random_uuid(uuid);
3333 class_uuid_unparse(uuid, &mds->mds_md_uuid);
3335 OBD_ALLOC(mds->mds_md_name, LUSTRE_CFG_BUFLEN(lcfg, 5));
3336 if (mds->mds_md_name == NULL)
3337 RETURN(rc = -ENOMEM);
3339 memcpy(mds->mds_md_name, lustre_cfg_buf(lcfg, 5),
3340 LUSTRE_CFG_BUFLEN(lcfg, 5));
3342 CDEBUG(D_OTHER, "MDS: %s is master for %s\n",
3343 obd->obd_name, mds->mds_md_name);
3345 rc = mds_md_connect(obd, mds->mds_md_name);
3347 OBD_FREE(mds->mds_md_name, LUSTRE_CFG_BUFLEN(lcfg, 5));
3352 mds->mds_obd_type = MDS_MASTER_OBD;
3354 if (LUSTRE_CFG_BUFLEN(lcfg, 6) > 0 && lustre_cfg_buf(lcfg, 6) &&
3355 strncmp(lustre_cfg_string(lcfg, 6), "dumb",
3356 LUSTRE_CFG_BUFLEN(lcfg, 6))) {
3357 if (!memcmp(lustre_cfg_string(lcfg, 6), "master",
3358 strlen("master"))) {
3359 mds->mds_obd_type = MDS_MASTER_OBD;
3360 } else if (!memcmp(lustre_cfg_string(lcfg, 6), "cache",
3362 mds->mds_obd_type = MDS_CACHE_OBD;
3366 rc = lvfs_mount_fs(lustre_cfg_string(lcfg, 1),
3367 lustre_cfg_string(lcfg, 2),
3368 options, 0, &lvfs_ctxt);
3372 if (rc || !lvfs_ctxt) {
3373 CERROR("lvfs_mount_fs failed: rc = %d\n", rc);
3377 mnt = lvfs_ctxt->loc_mnt;
3378 mds->mds_lvfs_ctxt = lvfs_ctxt;
3379 ll_clear_rdonly(ll_sbdev(mnt->mnt_sb));
3381 CDEBUG(D_SUPER, "%s: mnt = %p\n", lustre_cfg_string(lcfg, 1), mnt);
3383 sema_init(&mds->mds_epoch_sem, 1);
3384 atomic_set(&mds->mds_real_clients, 0);
3385 spin_lock_init(&mds->mds_transno_lock);
3386 spin_lock_init(&mds->mds_last_fid_lock);
3387 sema_init(&mds->mds_orphan_recovery_sem, 1);
3388 mds->mds_max_cookiesize = sizeof(struct llog_cookie);
3390 sprintf(ns_name, "mds-%s", obd->obd_uuid.uuid);
3391 obd->obd_namespace = ldlm_namespace_new(ns_name, LDLM_NAMESPACE_SERVER);
3393 if (obd->obd_namespace == NULL) {
3394 mds_cleanup(obd, 0);
3395 GOTO(err_put, rc = -ENOMEM);
3397 ldlm_register_intent(obd->obd_namespace, mds_intent_policy);
3399 rc = mds_fs_setup(obd, mnt);
3401 CERROR("%s: MDS filesystem method init failed: rc = %d\n",
3406 rc = llog_start_commit_thread();
3412 if (LUSTRE_CFG_BUFLEN(lcfg, 3) > 0 && lustre_cfg_buf(lcfg, 3) &&
3413 strncmp(lustre_cfg_string(lcfg, 3), "dumb",
3414 LUSTRE_CFG_BUFLEN(lcfg, 3))) {
3417 generate_random_uuid(uuid);
3418 class_uuid_unparse(uuid, &mds->mds_dt_uuid);
3420 OBD_ALLOC(mds->mds_profile, LUSTRE_CFG_BUFLEN(lcfg, 3));
3421 if (mds->mds_profile == NULL)
3422 GOTO(err_fs, rc = -ENOMEM);
3424 strncpy(mds->mds_profile, lustre_cfg_string(lcfg, 3),
3425 LUSTRE_CFG_BUFLEN(lcfg, 3));
3429 * setup root dir and files ID dir if lmv already connected, or there is
3432 if (mds->mds_md_exp || (LUSTRE_CFG_BUFLEN(lcfg, 3) > 0 &&
3433 lustre_cfg_buf(lcfg, 3) &&
3434 strncmp(lustre_cfg_string(lcfg, 3), "dumb",
3435 LUSTRE_CFG_BUFLEN(lcfg, 3)))) {
3436 rc = mds_fs_setup_rootid(obd);
3440 rc = mds_fs_setup_virtid(obd);
3445 ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
3446 "mds_ldlm_client", &obd->obd_ldlm_client);
3447 obd->obd_replayable = 1;
3449 rc = mds_postsetup(obd);
3456 /* No extra cleanup needed for llog_init_commit_thread() */
3457 mds_fs_cleanup(obd, 0);
3459 ldlm_namespace_free(obd->obd_namespace, 0);
3460 obd->obd_namespace = NULL;
3463 lvfs_umount_fs(mds->mds_lvfs_ctxt);
3467 fsfilt_put_ops(obd->obd_fsops);
3471 static int mds_postsetup(struct obd_device *obd)
3473 struct mds_obd *mds = &obd->u.mds;
3477 rc = obd_llog_setup(obd, &obd->obd_llogs, LLOG_CONFIG_ORIG_CTXT,
3478 obd, 0, NULL, &llog_lvfs_ops);
3482 if (mds->mds_profile) {
3483 struct llog_ctxt *lgctxt;
3484 struct lvfs_run_ctxt saved;
3485 struct lustre_profile *lprof;
3486 struct config_llog_instance cfg;
3488 cfg.cfg_instance = NULL;
3489 cfg.cfg_uuid = mds->mds_dt_uuid;
3490 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
3492 lgctxt = llog_get_context(&obd->obd_llogs, LLOG_CONFIG_ORIG_CTXT);
3494 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
3495 GOTO(err_llog, rc = -EINVAL);
3498 rc = class_config_process_llog(lgctxt, mds->mds_profile, &cfg);
3499 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
3504 lprof = class_get_profile(mds->mds_profile);
3505 if (lprof == NULL) {
3506 CERROR("No profile found: %s\n", mds->mds_profile);
3507 GOTO(err_cleanup, rc = -ENOENT);
3509 rc = mds_dt_connect(obd, lprof->lp_lov);
3511 GOTO(err_cleanup, rc);
3513 rc = mds_md_postsetup(obd);
3515 GOTO(err_cleanup, rc);
3522 obd_llog_cleanup(llog_get_context(&obd->obd_llogs,
3523 LLOG_CONFIG_ORIG_CTXT));
3527 int mds_postrecov_common(struct obd_device *obd)
3529 struct mds_obd *mds = &obd->u.mds;
3530 struct llog_ctxt *ctxt;
3531 int rc, item = 0, valsize;
3535 LASSERT(!obd->obd_recovering);
3536 ctxt = llog_get_context(&obd->obd_llogs, LLOG_UNLINK_ORIG_CTXT);
3537 LASSERT(ctxt != NULL);
3539 /* clean PENDING dir */
3540 rc = mds_cleanup_orphans(obd);
3545 group = FILTER_GROUP_FIRST_MDS + mds->mds_num;
3546 valsize = sizeof(group);
3547 rc = obd_set_info(mds->mds_dt_exp, strlen("mds_conn"),
3548 "mds_conn", valsize, &group);
3552 rc = llog_connect(ctxt, obd->u.mds.mds_dt_desc.ld_tgt_count,
3555 CERROR("%s: failed at llog_origin_connect: %d\n",
3560 /* remove the orphaned precreated objects */
3561 rc = mds_dt_clear_orphans(mds, NULL /* all OSTs */);
3566 RETURN(rc < 0 ? rc : item);
3569 /* cleanup all llogging subsystems */
3570 rc = obd_llog_finish(obd, &obd->obd_llogs,
3571 mds->mds_dt_desc.ld_tgt_count);
3573 CERROR("%s: failed to cleanup llogging subsystems\n",
3578 int mds_postrecov(struct obd_device *obd)
3582 rc = mds_postrecov_common(obd);
3584 rc = mds_md_reconnect(obd);
3588 int mds_dt_clean(struct obd_device *obd)
3590 struct mds_obd *mds = &obd->u.mds;
3593 if (mds->mds_profile) {
3595 struct llog_ctxt *llctx;
3596 struct lvfs_run_ctxt saved;
3597 struct config_llog_instance cfg;
3598 int len = strlen(mds->mds_profile) + sizeof("-clean") + 1;
3600 OBD_ALLOC(cln_prof, len);
3601 sprintf(cln_prof, "%s-clean", mds->mds_profile);
3603 cfg.cfg_instance = NULL;
3604 cfg.cfg_uuid = mds->mds_dt_uuid;
3606 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
3607 llctx = llog_get_context(&obd->obd_llogs,
3608 LLOG_CONFIG_ORIG_CTXT);
3609 class_config_process_llog(llctx, cln_prof, &cfg);
3610 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
3612 OBD_FREE(cln_prof, len);
3613 OBD_FREE(mds->mds_profile, strlen(mds->mds_profile) + 1);
3614 mds->mds_profile = NULL;
3619 int mds_md_clean(struct obd_device *obd)
3621 struct mds_obd *mds = &obd->u.mds;
3624 if (mds->mds_md_name) {
3625 OBD_FREE(mds->mds_md_name, strlen(mds->mds_md_name) + 1);
3626 mds->mds_md_name = NULL;
3631 static int mds_precleanup(struct obd_device *obd, int flags)
3637 mds_dt_disconnect(obd, flags);
3639 obd_llog_cleanup(llog_get_context(&obd->obd_llogs, LLOG_CONFIG_ORIG_CTXT));
3643 extern void lgss_svc_cache_purge_all(void);
3644 static int mds_cleanup(struct obd_device *obd, int flags)
3646 struct mds_obd *mds = &obd->u.mds;
3649 if (mds->mds_sb == NULL)
3652 mds_update_server_data(obd, 1);
3653 mds_update_last_fid(obd, NULL, 1);
3655 if (mds->mds_dt_objids != NULL) {
3656 int size = mds->mds_dt_desc.ld_tgt_count *
3658 OBD_FREE(mds->mds_dt_objids, size);
3660 mds_fs_cleanup(obd, flags);
3664 /* 2 seems normal on mds, (may_umount() also expects 2
3665 fwiw), but we only see 1 at this point in obdfilter. */
3666 lvfs_umount_fs(mds->mds_lvfs_ctxt);
3670 ldlm_namespace_free(obd->obd_namespace, flags & OBD_OPT_FORCE);
3672 spin_lock_bh(&obd->obd_processing_task_lock);
3673 if (obd->obd_recovering) {
3674 target_cancel_recovery_timer(obd);
3675 obd->obd_recovering = 0;
3677 spin_unlock_bh(&obd->obd_processing_task_lock);
3680 fsfilt_put_ops(obd->obd_fsops);
3684 lgss_svc_cache_purge_all();
3687 spin_lock(&mds->mds_denylist_lock);
3688 while (!list_empty( &mds->mds_denylist ) ) {
3689 deny_sec_t *p_deny_sec = list_entry(mds->mds_denylist.next,
3691 list_del(&p_deny_sec->list);
3692 OBD_FREE(p_deny_sec, sizeof(*p_deny_sec));
3694 spin_unlock(&mds->mds_denylist_lock);
3699 static int set_security(const char *value, char **sec)
3701 if (!strcmp(value, "null"))
3703 else if (!strcmp(value, "krb5i"))
3705 else if (!strcmp(value, "krb5p"))
3708 CERROR("Unrecognized security flavor %s\n", value);
3715 static int mds_process_config(struct obd_device *obd, obd_count len, void *buf)
3717 struct lustre_cfg *lcfg = buf;
3718 struct mds_obd *mds = &obd->u.mds;
3722 switch(lcfg->lcfg_command) {
3723 case LCFG_SET_SECURITY: {
3724 if ((LUSTRE_CFG_BUFLEN(lcfg, 1) == 0) ||
3725 (LUSTRE_CFG_BUFLEN(lcfg, 2) == 0))
3726 GOTO(out, rc = -EINVAL);
3728 if (!strcmp(lustre_cfg_string(lcfg, 1), "mds_sec"))
3729 rc = set_security(lustre_cfg_string(lcfg, 2),
3731 else if (!strcmp(lustre_cfg_string(lcfg, 1), "oss_sec"))
3732 rc = set_security(lustre_cfg_string(lcfg, 2),
3734 else if (!strcmp(lustre_cfg_string(lcfg, 1), "deny_sec")){
3735 spin_lock(&mds->mds_denylist_lock);
3736 rc = add_deny_security(lustre_cfg_string(lcfg, 2),
3737 &mds->mds_denylist);
3738 spin_unlock(&mds->mds_denylist_lock);
3740 CERROR("Unrecognized key\n");
3746 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
3747 GOTO(out, rc = -EINVAL);
3753 static void fixup_handle_for_resent_req(struct ptlrpc_request *req,
3755 struct ldlm_lock *new_lock,
3756 struct ldlm_lock **old_lock,
3757 struct lustre_handle *lockh)
3759 struct obd_export *exp = req->rq_export;
3760 struct obd_device *obd = exp->exp_obd;
3761 struct ldlm_request *dlmreq =
3762 lustre_msg_buf(req->rq_reqmsg, offset, sizeof (*dlmreq));
3763 struct lustre_handle remote_hdl = dlmreq->lock_handle1;
3764 struct list_head *iter;
3766 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
3769 l_lock(&obd->obd_namespace->ns_lock);
3770 list_for_each(iter, &exp->exp_ldlm_data.led_held_locks) {
3771 struct ldlm_lock *lock;
3772 lock = list_entry(iter, struct ldlm_lock, l_export_chain);
3773 if (lock == new_lock)
3775 if (lock->l_remote_handle.cookie == remote_hdl.cookie) {
3776 lockh->cookie = lock->l_handle.h_cookie;
3777 LDLM_DEBUG(lock, "restoring lock cookie");
3778 DEBUG_REQ(D_HA, req, "restoring lock cookie "LPX64,
3781 *old_lock = LDLM_LOCK_GET(lock);
3782 l_unlock(&obd->obd_namespace->ns_lock);
3786 l_unlock(&obd->obd_namespace->ns_lock);
3788 /* If the xid matches, then we know this is a resent request,
3789 * and allow it. (It's probably an OPEN, for which we don't
3792 le64_to_cpu(exp->exp_mds_data.med_mcd->mcd_last_xid))
3796 le64_to_cpu(exp->exp_mds_data.med_mcd->mcd_last_close_xid))
3799 /* This remote handle isn't enqueued, so we never received or
3800 * processed this request. Clear MSG_RESENT, because it can
3801 * be handled like any normal request now. */
3803 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
3805 DEBUG_REQ(D_HA, req, "no existing lock with rhandle "LPX64,
3809 int intent_disposition(struct ldlm_reply *rep, int flag)
3813 return (rep->lock_policy_res1 & flag);
3816 void intent_set_disposition(struct ldlm_reply *rep, int flag)
3820 rep->lock_policy_res1 |= flag;
3823 static int mds_intent_policy(struct ldlm_namespace *ns,
3824 struct ldlm_lock **lockp, void *req_cookie,
3825 ldlm_mode_t mode, int flags, void *data)
3827 struct ptlrpc_request *req = req_cookie;
3828 struct ldlm_lock *lock = *lockp;
3829 struct ldlm_intent *it;
3830 struct mds_obd *mds = &req->rq_export->exp_obd->u.mds;
3831 struct ldlm_reply *rep;
3832 struct lustre_handle lockh[2] = {{0}, {0}};
3833 struct ldlm_lock *new_lock = NULL;
3834 int getattr_part = MDS_INODELOCK_UPDATE;
3835 int rc, reply_buffers;
3836 int repsize[5] = {sizeof(struct ldlm_reply),
3837 sizeof(struct mds_body),
3838 mds->mds_max_mdsize};
3840 int offset = MDS_REQ_INTENT_REC_OFF;
3843 LASSERT(req != NULL);
3844 MD_COUNTER_INCREMENT(req->rq_export->exp_obd, intent_lock);
3846 if (req->rq_reqmsg->bufcount <= MDS_REQ_INTENT_IT_OFF) {
3847 /* No intent was provided */
3848 int size = sizeof(struct ldlm_reply);
3849 rc = lustre_pack_reply(req, 1, &size, NULL);
3854 it = lustre_swab_reqbuf(req, MDS_REQ_INTENT_IT_OFF, sizeof(*it),
3855 lustre_swab_ldlm_intent);
3857 CERROR("Intent missing\n");
3858 RETURN(req->rq_status = -EFAULT);
3861 LDLM_DEBUG(lock, "intent policy, opc: %s", ldlm_it2str(it->opc));
3864 if (it->opc & ( IT_OPEN | IT_GETATTR | IT_LOOKUP | IT_CHDIR )) {
3867 repsize[4] = xattr_acl_size(LL_ACL_MAX_ENTRIES);
3870 rc = lustre_pack_reply(req, reply_buffers, repsize, NULL);
3872 RETURN(req->rq_status = rc);
3874 rep = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*rep));
3875 LASSERT(rep != NULL);
3877 intent_set_disposition(rep, DISP_IT_EXECD);
3879 /* execute policy */
3880 switch ((long)it->opc) {
3882 case IT_CREAT|IT_OPEN:
3883 fixup_handle_for_resent_req(req, MDS_REQ_INTENT_LOCKREQ_OFF,
3885 /* XXX swab here to assert that an mds_open reint
3886 * packet is following */
3887 fixup_handle_for_resent_req(req, MDS_REQ_INTENT_LOCKREQ_OFF,
3889 rep->lock_policy_res2 = mds_reint(req, offset, lockh);
3891 if (rep->lock_policy_res2) {
3893 * mds_open() returns ENOLCK where it should return
3894 * zero, but it has no lock to return.
3896 if (rep->lock_policy_res2 == ENOLCK)
3897 rep->lock_policy_res2 = 0;
3899 RETURN(ELDLM_LOCK_ABORTED);
3903 * IT_OPEN may return lock on cross-node dentry that we want to
3904 * hold during attr retrival -bzzz
3906 if (lockh[0].cookie == 0)
3907 RETURN(ELDLM_LOCK_ABORTED);
3911 getattr_part = MDS_INODELOCK_LOOKUP;
3914 getattr_part |= MDS_INODELOCK_LOOKUP;
3916 fixup_handle_for_resent_req(req, MDS_REQ_INTENT_LOCKREQ_OFF,
3917 lock, &new_lock, lockh);
3918 rep->lock_policy_res2 = mds_getattr_lock(req, offset, lockh,
3920 /* FIXME: LDLM can set req->rq_status. MDS sets
3921 policy_res{1,2} with disposition and status.
3922 - replay: returns 0 & req->status is old status
3923 - otherwise: returns req->status */
3924 if (intent_disposition(rep, DISP_LOOKUP_NEG))
3925 rep->lock_policy_res2 = 0;
3926 if (!intent_disposition(rep, DISP_LOOKUP_POS) ||
3927 rep->lock_policy_res2)
3928 RETURN(ELDLM_LOCK_ABORTED);
3929 if (req->rq_status != 0) {
3931 rep->lock_policy_res2 = req->rq_status;
3932 RETURN(ELDLM_LOCK_ABORTED);
3936 rc = mds_lock_and_check_slave(offset, req, lockh);
3937 if ((rep->lock_policy_res2 = rc)) {
3939 rep->lock_policy_res2 = 0;
3940 RETURN(ELDLM_LOCK_ABORTED);
3944 CERROR("Unhandled intent "LPD64"\n", it->opc);
3948 /* By this point, whatever function we called above must have either
3949 * filled in 'lockh', been an intent replay, or returned an error. We
3950 * want to allow replayed RPCs to not get a lock, since we would just
3951 * drop it below anyways because lock replay is done separately by the
3952 * client afterwards. For regular RPCs we want to give the new lock to
3953 * the client instead of whatever lock it was about to get. */
3954 if (new_lock == NULL)
3955 new_lock = ldlm_handle2lock(&lockh[0]);
3956 if (new_lock == NULL && (flags & LDLM_FL_INTENT_ONLY))
3959 LASSERTF(new_lock != NULL, "op "LPX64" lockh "LPX64"\n",
3960 it->opc, lockh[0].cookie);
3962 /* If we've already given this lock to a client once, then we should
3963 * have no readers or writers. Otherwise, we should have one reader
3964 * _or_ writer ref (which will be zeroed below) before returning the
3965 * lock to a client. */
3966 if (new_lock->l_export == req->rq_export) {
3967 LASSERT(new_lock->l_readers + new_lock->l_writers == 0);
3969 LASSERT(new_lock->l_export == NULL);
3970 LASSERT(new_lock->l_readers + new_lock->l_writers == 1);
3975 if (new_lock->l_export == req->rq_export) {
3976 /* Already gave this to the client, which means that we
3977 * reconstructed a reply. */
3978 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
3980 RETURN(ELDLM_LOCK_REPLACED);
3983 /* Fixup the lock to be given to the client */
3984 l_lock(&new_lock->l_resource->lr_namespace->ns_lock);
3985 new_lock->l_readers = 0;
3986 new_lock->l_writers = 0;
3988 new_lock->l_export = class_export_get(req->rq_export);
3989 list_add(&new_lock->l_export_chain,
3990 &new_lock->l_export->exp_ldlm_data.led_held_locks);
3992 new_lock->l_blocking_ast = lock->l_blocking_ast;
3993 new_lock->l_completion_ast = lock->l_completion_ast;
3995 memcpy(&new_lock->l_remote_handle, &lock->l_remote_handle,
3996 sizeof(lock->l_remote_handle));
3998 new_lock->l_flags &= ~LDLM_FL_LOCAL;
4000 LDLM_LOCK_PUT(new_lock);
4001 l_unlock(&new_lock->l_resource->lr_namespace->ns_lock);
4003 RETURN(ELDLM_LOCK_REPLACED);
4006 int mds_attach(struct obd_device *dev, obd_count len, void *data)
4008 struct lprocfs_static_vars lvars;
4010 struct mds_obd *mds = &dev->u.mds;
4012 spin_lock_init(&mds->mds_denylist_lock);
4013 INIT_LIST_HEAD(&mds->mds_denylist);
4015 lprocfs_init_multi_vars(0, &lvars);
4017 rc = lprocfs_obd_attach(dev, lvars.obd_vars);
4021 return lprocfs_alloc_md_stats(dev, 0);
4024 int mds_detach(struct obd_device *dev)
4026 lprocfs_free_md_stats(dev);
4027 return lprocfs_obd_detach(dev);
4030 int mdt_attach(struct obd_device *dev, obd_count len, void *data)
4032 struct lprocfs_static_vars lvars;
4034 lprocfs_init_multi_vars(1, &lvars);
4035 return lprocfs_obd_attach(dev, lvars.obd_vars);
4038 int mdt_detach(struct obd_device *dev)
4040 return lprocfs_obd_detach(dev);
4043 static int mdt_setup(struct obd_device *obd, obd_count len, void *buf)
4045 struct mds_obd *mds = &obd->u.mds;
4050 ptlrpc_init_svc(MDS_NBUFS, MDS_BUFSIZE, MDS_MAXREQSIZE,
4051 MDS_REQUEST_PORTAL, MDC_REPLY_PORTAL,
4052 MDS_SERVICE_WATCHDOG_TIMEOUT,
4053 mds_handle, "mds", obd->obd_proc_entry);
4055 if (!mds->mds_service) {
4056 CERROR("failed to start service\n");
4060 rc = ptlrpc_start_n_threads(obd, mds->mds_service, MDT_NUM_THREADS,
4063 GOTO(err_thread, rc);
4065 mds->mds_setattr_service =
4066 ptlrpc_init_svc(MDS_NBUFS, MDS_BUFSIZE, MDS_MAXREQSIZE,
4067 MDS_SETATTR_PORTAL, MDC_REPLY_PORTAL,
4068 MDS_SERVICE_WATCHDOG_TIMEOUT,
4069 mds_handle, "mds_setattr",
4070 obd->obd_proc_entry);
4071 if (!mds->mds_setattr_service) {
4072 CERROR("failed to start getattr service\n");
4073 GOTO(err_thread, rc = -ENOMEM);
4076 rc = ptlrpc_start_n_threads(obd, mds->mds_setattr_service,
4077 MDT_NUM_THREADS, "ll_mdt_attr");
4079 GOTO(err_thread2, rc);
4081 mds->mds_readpage_service =
4082 ptlrpc_init_svc(MDS_NBUFS, MDS_BUFSIZE, MDS_MAXREQSIZE,
4083 MDS_READPAGE_PORTAL, MDC_REPLY_PORTAL,
4084 MDS_SERVICE_WATCHDOG_TIMEOUT,
4085 mds_handle, "mds_readpage",
4086 obd->obd_proc_entry);
4087 if (!mds->mds_readpage_service) {
4088 CERROR("failed to start readpage service\n");
4089 GOTO(err_thread2, rc = -ENOMEM);
4092 rc = ptlrpc_start_n_threads(obd, mds->mds_readpage_service,
4093 MDT_NUM_THREADS, "ll_mdt_rdpg");
4096 GOTO(err_thread3, rc);
4101 ptlrpc_unregister_service(mds->mds_readpage_service);
4103 ptlrpc_unregister_service(mds->mds_setattr_service);
4105 ptlrpc_unregister_service(mds->mds_service);
4109 static int mdt_cleanup(struct obd_device *obd, int flags)
4111 struct mds_obd *mds = &obd->u.mds;
4114 ptlrpc_stop_all_threads(mds->mds_readpage_service);
4115 ptlrpc_unregister_service(mds->mds_readpage_service);
4117 ptlrpc_stop_all_threads(mds->mds_setattr_service);
4118 ptlrpc_unregister_service(mds->mds_setattr_service);
4120 ptlrpc_stop_all_threads(mds->mds_service);
4121 ptlrpc_unregister_service(mds->mds_service);
4126 static struct dentry *mds_lvfs_id2dentry(__u64 ino, __u32 gen,
4127 __u64 gr, void *data)
4129 struct lustre_id id;
4130 struct obd_device *obd = data;
4134 return mds_id2dentry(obd, &id, NULL);
4137 static int mds_get_info(struct obd_export *exp, __u32 keylen,
4138 void *key, __u32 *valsize, void *val)
4140 struct obd_device *obd;
4141 struct mds_obd *mds;
4144 obd = class_exp2obd(exp);
4148 CDEBUG(D_IOCTL, "invalid client cookie "LPX64"\n",
4149 exp->exp_handle.h_cookie);
4153 if (keylen >= strlen("reint_log") && memcmp(key, "reint_log", 9) == 0) {
4154 /* get log_context handle. */
4155 unsigned long *llh_handle = val;
4156 *valsize = sizeof(unsigned long);
4157 *llh_handle = (unsigned long)obd->obd_llog_ctxt[LLOG_REINT_ORIG_CTXT];
4160 if (keylen >= strlen("cache_sb") && memcmp(key, "cache_sb", 8) == 0) {
4161 /* get log_context handle. */
4162 unsigned long *sb = val;
4163 *valsize = sizeof(unsigned long);
4164 *sb = (unsigned long)obd->u.mds.mds_sb;
4168 if (keylen >= strlen("mdsize") && memcmp(key, "mdsize", keylen) == 0) {
4169 __u32 *mdsize = val;
4170 *valsize = sizeof(*mdsize);
4171 *mdsize = mds->mds_max_mdsize;
4175 if (keylen >= strlen("mdsnum") && strcmp(key, "mdsnum") == 0) {
4176 __u32 *mdsnum = val;
4177 *valsize = sizeof(*mdsnum);
4178 *mdsnum = mds->mds_num;
4182 if (keylen >= strlen("rootid") && strcmp(key, "rootid") == 0) {
4183 struct lustre_id *rootid = val;
4184 *valsize = sizeof(struct lustre_id);
4185 *rootid = mds->mds_rootid;
4189 CDEBUG(D_IOCTL, "invalid key\n");
4193 struct lvfs_callback_ops mds_lvfs_ops = {
4194 l_id2dentry: mds_lvfs_id2dentry,
4197 int mds_preprw(int cmd, struct obd_export *exp, struct obdo *oa,
4198 int objcount, struct obd_ioobj *obj,
4199 int niocount, struct niobuf_remote *nb,
4200 struct niobuf_local *res,
4201 struct obd_trans_info *oti);
4203 int mds_commitrw(int cmd, struct obd_export *exp, struct obdo *oa,
4204 int objcount, struct obd_ioobj *obj, int niocount,
4205 struct niobuf_local *res, struct obd_trans_info *oti,
4208 /* use obd ops to offer management infrastructure */
4209 static struct obd_ops mds_obd_ops = {
4210 .o_owner = THIS_MODULE,
4211 .o_attach = mds_attach,
4212 .o_detach = mds_detach,
4213 .o_connect = mds_connect,
4214 .o_connect_post = mds_connect_post,
4215 .o_init_export = mds_init_export,
4216 .o_destroy_export = mds_destroy_export,
4217 .o_disconnect = mds_disconnect,
4218 .o_setup = mds_setup,
4219 .o_precleanup = mds_precleanup,
4220 .o_cleanup = mds_cleanup,
4221 .o_process_config = mds_process_config,
4222 .o_postrecov = mds_postrecov,
4223 .o_statfs = mds_obd_statfs,
4224 .o_iocontrol = mds_iocontrol,
4225 .o_create = mds_obd_create,
4226 .o_destroy = mds_obd_destroy,
4227 .o_llog_init = mds_llog_init,
4228 .o_llog_finish = mds_llog_finish,
4229 .o_notify = mds_notify,
4230 .o_get_info = mds_get_info,
4231 .o_set_info = mds_set_info,
4232 .o_preprw = mds_preprw,
4233 .o_commitrw = mds_commitrw,
4236 static struct obd_ops mdt_obd_ops = {
4237 .o_owner = THIS_MODULE,
4238 .o_attach = mdt_attach,
4239 .o_detach = mdt_detach,
4240 .o_setup = mdt_setup,
4241 .o_cleanup = mdt_cleanup,
4244 static int __init mds_init(void)
4246 struct lprocfs_static_vars lvars;
4248 mds_init_lsd_cache();
4250 lprocfs_init_multi_vars(0, &lvars);
4251 class_register_type(&mds_obd_ops, NULL, lvars.module_vars,
4253 lprocfs_init_multi_vars(1, &lvars);
4254 class_register_type(&mdt_obd_ops, NULL, lvars.module_vars,
4260 static void /*__exit*/ mds_exit(void)
4262 mds_cleanup_lsd_cache();
4264 class_unregister_type(LUSTRE_MDS_NAME);
4265 class_unregister_type(LUSTRE_MDT_NAME);
4268 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
4269 MODULE_DESCRIPTION("Lustre Metadata Server (MDS)");
4270 MODULE_LICENSE("GPL");
4272 module_init(mds_init);
4273 module_exit(mds_exit);