1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2001-2004 Cluster File Systems, Inc.
6 * This file is part of Lustre, http://www.sf.net/projects/lustre/
8 * Lustre is free software; you can redistribute it and/or
9 * modify it under the terms of version 2 of the GNU General Public
10 * License as published by the Free Software Foundation.
12 * Lustre is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Lustre; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 # define EXPORT_SYMTAB
25 #define DEBUG_SUBSYSTEM S_MDC
28 # include <linux/module.h>
29 # include <linux/pagemap.h>
30 # include <linux/miscdevice.h>
31 # include <linux/init.h>
33 # include <liblustre.h>
36 #include <linux/obd_class.h>
37 #include <linux/lustre_mds.h>
38 #include <linux/lustre_dlm.h>
39 #include <linux/lprocfs_status.h>
40 #include "mdc_internal.h"
42 #define REQUEST_MINOR 244
44 static int mdc_cleanup(struct obd_device *obd, int flags);
46 int mdc_get_secdesc_size(void)
49 int ngroups = current_ngroups;
51 if (ngroups > LUSTRE_MAX_GROUPS)
52 ngroups = LUSTRE_MAX_GROUPS;
54 return sizeof(struct mds_req_sec_desc) +
55 sizeof(__u32) * ngroups;
62 * because group info might have changed since last time we call
63 * get_secdesc_size(), so here we did more sanity check to prevent garbage gids
65 void mdc_pack_secdesc(struct ptlrpc_request *req, int size)
68 struct mds_req_sec_desc *rsd;
70 rsd = lustre_msg_buf(req->rq_reqmsg,
71 MDS_REQ_SECDESC_OFF, size);
73 rsd->rsd_uid = current->uid;
74 rsd->rsd_gid = current->gid;
75 rsd->rsd_fsuid = current->fsuid;
76 rsd->rsd_fsgid = current->fsgid;
77 rsd->rsd_cap = current->cap_effective;
78 rsd->rsd_ngroups = (size - sizeof(*rsd)) / sizeof(__u32);
79 LASSERT(rsd->rsd_ngroups <= LUSTRE_MAX_GROUPS);
81 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,4)
83 get_group_info(current->group_info);
84 ginfo = current->group_info;
86 if (rsd->rsd_ngroups > ginfo->ngroups)
87 rsd->rsd_ngroups = ginfo->ngroups;
88 memcpy(rsd->rsd_groups, ginfo->blocks[0],
89 rsd->rsd_ngroups * sizeof(__u32));
91 LASSERT(rsd->rsd_ngroups <= NGROUPS);
92 if (rsd->rsd_ngroups > current->ngroups)
93 rsd->rsd_ngroups = current->ngroups;
94 memcpy(rsd->rsd_groups, current->groups,
95 rsd->rsd_ngroups * sizeof(__u32));
100 extern int mds_queue_req(struct ptlrpc_request *);
101 /* Helper that implements most of mdc_getstatus and signal_completed_replay. */
102 /* XXX this should become mdc_get_info("key"), sending MDS_GET_INFO RPC */
103 static int send_getstatus(struct obd_import *imp, struct lustre_id *rootid,
104 int level, int msg_flags)
106 struct ptlrpc_request *req;
107 struct mds_body *body;
108 int rc, size[2] = {0, sizeof(*body)};
111 //size[0] = mdc_get_secdesc_size();
113 req = ptlrpc_prep_req(imp, LUSTRE_MDS_VERSION, MDS_GETSTATUS,
116 GOTO(out, rc = -ENOMEM);
118 //mdc_pack_secdesc(req, size[0]);
120 body = lustre_msg_buf(req->rq_reqmsg, MDS_REQ_REC_OFF, sizeof (*body));
121 req->rq_send_state = level;
122 req->rq_replen = lustre_msg_size(1, &size[1]);
124 req->rq_reqmsg->flags |= msg_flags;
125 rc = ptlrpc_queue_wait(req);
128 body = lustre_swab_repbuf (req, 0, sizeof (*body),
129 lustre_swab_mds_body);
131 CERROR ("Can't extract mds_body\n");
132 GOTO (out, rc = -EPROTO);
135 memcpy(rootid, &body->id1, sizeof(*rootid));
137 CDEBUG(D_NET, "root ino="LPU64", last_committed="LPU64
138 ", last_xid="LPU64"\n", rootid->li_stc.u.e3s.l3s_ino,
139 req->rq_repmsg->last_committed, req->rq_repmsg->last_xid);
144 ptlrpc_req_finished(req);
148 /* This should be mdc_get_info("rootid") */
149 int mdc_getstatus(struct obd_export *exp, struct lustre_id *rootid)
151 return send_getstatus(class_exp2cliimp(exp), rootid,
155 int mdc_getattr_common(struct obd_export *exp, unsigned int ea_size,
156 struct ptlrpc_request *req)
158 struct mds_body *body;
161 int repsize[2] = {sizeof(*body), 0};
165 /* request message already built */
168 repsize[bufcount++] = ea_size;
169 CDEBUG(D_INODE, "reserved %u bytes for MD/symlink in packet\n",
172 req->rq_replen = lustre_msg_size(bufcount, repsize);
174 mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
175 rc = ptlrpc_queue_wait(req);
176 mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
180 body = lustre_swab_repbuf (req, 0, sizeof (*body),
181 lustre_swab_mds_body);
183 CERROR ("Can't unpack mds_body\n");
187 CDEBUG(D_NET, "mode: %o\n", body->mode);
189 LASSERT_REPSWAB (req, 1);
190 if (body->eadatasize != 0) {
191 /* reply indicates presence of eadata; check it's there... */
192 eadata = lustre_msg_buf (req->rq_repmsg, 1, body->eadatasize);
193 if (eadata == NULL) {
194 CERROR ("Missing/short eadata\n");
202 int mdc_getattr(struct obd_export *exp, struct lustre_id *id,
203 unsigned long valid, unsigned int ea_size,
204 struct ptlrpc_request **request)
206 struct ptlrpc_request *req;
207 struct mds_body *body;
208 int size[2] = {0, sizeof(*body)};
212 /* XXX do we need to make another request here? We just did a getattr
213 * to do the lookup in the first place.
215 size[0] = mdc_get_secdesc_size();
217 req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
218 MDS_GETATTR, 2, size, NULL);
220 GOTO(out, rc = -ENOMEM);
222 mdc_pack_secdesc(req, size[0]);
224 body = lustre_msg_buf(req->rq_reqmsg, MDS_REQ_REC_OFF, sizeof (*body));
225 memcpy(&body->id1, id, sizeof(*id));
227 body->eadatasize = ea_size;
229 rc = mdc_getattr_common(exp, ea_size, req);
231 ptlrpc_req_finished (req);
239 int mdc_getattr_name(struct obd_export *exp, struct lustre_id *id,
240 char *filename, int namelen, unsigned long valid,
241 unsigned int ea_size, struct ptlrpc_request **request)
243 struct ptlrpc_request *req;
244 struct mds_body *body;
245 int rc, size[3] = {0, sizeof(*body), namelen};
248 size[0] = mdc_get_secdesc_size();
250 req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
251 MDS_GETATTR_LOCK, 3, size, NULL);
253 GOTO(out, rc = -ENOMEM);
255 mdc_pack_secdesc(req, size[0]);
257 body = lustre_msg_buf(req->rq_reqmsg, MDS_REQ_REC_OFF, sizeof (*body));
258 memcpy(&body->id1, id, sizeof(*id));
260 body->eadatasize = ea_size;
262 if (filename != NULL) {
263 LASSERT (strnlen (filename, namelen) == namelen - 1);
264 memcpy(lustre_msg_buf(req->rq_reqmsg, 2, namelen),
267 LASSERT(namelen == 1);
270 rc = mdc_getattr_common(exp, ea_size, req);
272 ptlrpc_req_finished (req);
280 /* This should be called with both the request and the reply still packed. */
281 int mdc_store_inode_generation(struct obd_export *exp,
282 struct ptlrpc_request *req,
283 int reqoff, int repoff)
285 struct mds_rec_create *rec =
286 lustre_msg_buf(req->rq_reqmsg, reqoff, sizeof(*rec));
287 struct mds_body *body =
288 lustre_msg_buf(req->rq_repmsg, repoff, sizeof(*body));
290 LASSERT (rec != NULL);
291 LASSERT (body != NULL);
293 memcpy(&rec->cr_replayid, &body->id1, sizeof(rec->cr_replayid));
294 DEBUG_REQ(D_HA, req, "storing generation for ino "DLID4,
295 OLID4(&rec->cr_replayid));
299 int mdc_req2lustre_md(struct obd_export *exp_mdc, struct ptlrpc_request *req,
300 unsigned int offset, struct obd_export *exp_osc,
301 struct lustre_md *md)
307 memset(md, 0, sizeof(*md));
309 md->body = lustre_msg_buf(req->rq_repmsg, offset, sizeof (*md->body));
310 LASSERT (md->body != NULL);
311 LASSERT_REPSWABBED (req, offset);
313 if (!(md->body->valid & OBD_MD_FLEASIZE) &&
314 !(md->body->valid & OBD_MD_FLDIREA))
317 /* ea is presented in reply, parse it */
318 if (S_ISREG(md->body->mode)) {
320 struct lov_mds_md *lmm;
322 if (md->body->eadatasize == 0) {
323 CERROR ("OBD_MD_FLEASIZE set, but eadatasize 0\n");
326 lmmsize = md->body->eadatasize;
327 lmm = lustre_msg_buf(req->rq_repmsg, offset + 1, lmmsize);
328 LASSERT (lmm != NULL);
329 LASSERT_REPSWABBED (req, offset + 1);
331 rc = obd_unpackmd(exp_osc, &md->lsm, lmm, lmmsize);
333 LASSERT (rc >= sizeof (*md->lsm));
336 } else if (S_ISDIR(md->body->mode)) {
339 LASSERT(exp_mdc != NULL);
341 /* dir can be non-splitted */
342 if (md->body->eadatasize == 0)
345 mdsize = md->body->eadatasize;
346 mea = lustre_msg_buf(req->rq_repmsg, offset + 1, mdsize);
347 LASSERT(mea != NULL);
350 * check mea for validness, as there is possible that old tests
351 * will try to set lov EA to dir object and thus confuse this
354 if (mea->mea_magic != MEA_MAGIC_LAST_CHAR &&
355 mea->mea_magic != MEA_MAGIC_ALL_CHARS)
356 GOTO(out_invalid_mea, rc = -EINVAL);
358 if (mea->mea_count > 256 || mea->mea_master > 256 ||
359 mea->mea_master > mea->mea_count)
360 GOTO(out_invalid_mea, rc = -EINVAL);
362 LASSERT(id_fid(&mea->mea_ids[0]));
364 rc = obd_unpackmd(exp_mdc, (void *)&md->mea,
365 (void *)mea, mdsize);
367 LASSERT (rc >= sizeof (*md->mea));
374 CERROR("Detected invalid mea, which does not "
375 "support neither old either new format.\n");
382 static void mdc_commit_open(struct ptlrpc_request *req)
384 struct mdc_open_data *mod = req->rq_cb_data;
388 if (mod->mod_close_req != NULL)
389 mod->mod_close_req->rq_cb_data = NULL;
391 if (mod->mod_och != NULL)
392 mod->mod_och->och_mod = NULL;
394 OBD_FREE(mod, sizeof(*mod));
395 req->rq_cb_data = NULL;
398 static void mdc_replay_open(struct ptlrpc_request *req)
400 struct mdc_open_data *mod = req->rq_cb_data;
401 struct obd_client_handle *och;
402 struct ptlrpc_request *close_req;
403 struct lustre_handle old;
404 struct mds_body *body;
407 body = lustre_swab_repbuf(req, 1, sizeof(*body), lustre_swab_mds_body);
408 LASSERT (body != NULL);
411 DEBUG_REQ(D_ERROR, req,
412 "can't properly replay without open data");
419 struct lustre_handle *file_fh;
420 LASSERT(och->och_magic == OBD_CLIENT_HANDLE_MAGIC);
421 file_fh = &och->och_fh;
422 CDEBUG(D_HA, "updating handle from "LPX64" to "LPX64"\n",
423 file_fh->cookie, body->handle.cookie);
424 memcpy(&old, file_fh, sizeof(old));
425 memcpy(file_fh, &body->handle, sizeof(*file_fh));
428 close_req = mod->mod_close_req;
429 if (close_req != NULL) {
430 struct mds_body *close_body;
431 LASSERT(close_req->rq_reqmsg->opc == MDS_CLOSE);
432 close_body = lustre_msg_buf(close_req->rq_reqmsg,
434 sizeof(*close_body));
436 LASSERT(!memcmp(&old, &close_body->handle, sizeof old));
437 DEBUG_REQ(D_HA, close_req, "updating close body with new fh");
438 memcpy(&close_body->handle, &body->handle,
439 sizeof(close_body->handle));
445 int mdc_set_open_replay_data(struct obd_export *exp,
446 struct obd_client_handle *och,
447 struct ptlrpc_request *open_req)
449 struct mdc_open_data *mod;
450 struct mds_rec_create *rec;
451 struct mds_body *body;
453 rec = lustre_msg_buf(open_req->rq_reqmsg, MDS_REQ_INTENT_REC_OFF,
455 body = lustre_msg_buf(open_req->rq_repmsg, 1, sizeof(*body));
457 LASSERT(rec != NULL);
458 /* outgoing messages always in my byte order */
459 LASSERT(body != NULL);
460 /* incoming message in my byte order (it's been swabbed) */
461 LASSERT_REPSWABBED(open_req, 1);
463 OBD_ALLOC(mod, sizeof(*mod));
465 DEBUG_REQ(D_ERROR, open_req, "can't allocate mdc_open_data");
471 mod->mod_open_req = open_req;
473 memcpy(&rec->cr_replayid, &body->id1, sizeof rec->cr_replayid);
474 open_req->rq_replay_cb = mdc_replay_open;
475 open_req->rq_commit_cb = mdc_commit_open;
476 open_req->rq_cb_data = mod;
477 DEBUG_REQ(D_HA, open_req, "set up replay data");
481 int mdc_clear_open_replay_data(struct obd_export *exp,
482 struct obd_client_handle *och)
484 struct mdc_open_data *mod = och->och_mod;
486 /* Don't free the structure now (it happens in mdc_commit_open, after
487 * we're sure we won't need to fix up the close request in the future),
488 * but make sure that replay doesn't poke at the och, which is about to
490 LASSERT(mod != LP_POISON);
497 static void mdc_commit_close(struct ptlrpc_request *req)
499 struct mdc_open_data *mod = req->rq_cb_data;
500 struct ptlrpc_request *open_req;
501 struct obd_import *imp = req->rq_import;
503 DEBUG_REQ(D_HA, req, "close req committed");
507 mod->mod_close_req = NULL;
508 req->rq_cb_data = NULL;
509 req->rq_commit_cb = NULL;
511 open_req = mod->mod_open_req;
512 LASSERT(open_req != NULL);
513 LASSERT(open_req != LP_POISON);
514 LASSERT(open_req->rq_type != LI_POISON);
516 DEBUG_REQ(D_HA, open_req, "open req balanced");
517 if (open_req->rq_transno == 0) {
518 DEBUG_REQ(D_ERROR, open_req, "BUG 3892 open");
519 DEBUG_REQ(D_ERROR, req, "BUG 3892 close");
520 LASSERTF(open_req->rq_transno != 0, "BUG 3892");
522 LASSERT(open_req->rq_import == imp);
524 /* We no longer want to preserve this for transno-unconditional
526 spin_lock(&open_req->rq_lock);
527 open_req->rq_replay = 0;
528 spin_unlock(&open_req->rq_lock);
531 static int mdc_close_interpret(struct ptlrpc_request *req, void *data, int rc)
533 union ptlrpc_async_args *aa = data;
534 struct mdc_rpc_lock *rpc_lock;
535 struct obd_device *obd = aa->pointer_arg[1];
538 spin_lock_irqsave(&req->rq_lock, flags);
539 rpc_lock = aa->pointer_arg[0];
540 aa->pointer_arg[0] = NULL;
541 spin_unlock_irqrestore (&req->rq_lock, flags);
543 if (rpc_lock == NULL) {
544 CERROR("called with NULL rpc_lock\n");
546 mdc_put_rpc_lock(rpc_lock, NULL);
547 LASSERTF(rpc_lock == obd->u.cli.cl_rpc_lock, "%p != %p\n",
548 rpc_lock, obd->u.cli.cl_rpc_lock);
550 wake_up(&req->rq_reply_waitq);
554 /* We can't use ptlrpc_check_reply, because we don't want to wake up for
555 * anything but a reply or an error. */
556 static int mdc_close_check_reply(struct ptlrpc_request *req)
561 spin_lock_irqsave(&req->rq_lock, flags);
562 if (req->rq_async_args.pointer_arg[0] == NULL)
564 spin_unlock_irqrestore (&req->rq_lock, flags);
568 static int go_back_to_sleep(void *unused)
573 int mdc_close(struct obd_export *exp, struct obdo *oa,
574 struct obd_client_handle *och, struct ptlrpc_request **request)
576 struct obd_device *obd = class_exp2obd(exp);
577 int reqsize[3] = {0, sizeof(struct mds_body),
578 obd->u.cli.cl_max_mds_cookiesize};
579 int rc, repsize[3] = {sizeof(struct mds_body),
580 obd->u.cli.cl_max_mds_easize,
581 obd->u.cli.cl_max_mds_cookiesize};
582 struct ptlrpc_request *req;
583 struct mdc_open_data *mod;
584 struct l_wait_info lwi;
587 //reqsize[0] = mdc_get_secdesc_size();
589 req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
590 MDS_CLOSE, 3, reqsize, NULL);
592 GOTO(out, rc = -ENOMEM);
594 //mdc_pack_secdesc(req, reqsize[0]);
596 /* Ensure that this close's handle is fixed up during replay. */
597 LASSERT(och != NULL);
599 if (likely(mod != NULL)) {
600 mod->mod_close_req = req;
601 LASSERT(mod->mod_open_req->rq_type != LI_POISON);
602 DEBUG_REQ(D_HA, mod->mod_open_req, "matched open req %p",
605 CDEBUG(D_HA, "couldn't find open req; expecting close error\n");
608 mdc_close_pack(req, 1, oa, oa->o_valid, och);
610 req->rq_replen = lustre_msg_size(3, repsize);
611 req->rq_commit_cb = mdc_commit_close;
612 LASSERT(req->rq_cb_data == NULL);
613 req->rq_cb_data = mod;
615 /* We hand a ref to the rpcd here, so we need another one of our own. */
616 ptlrpc_request_addref(req);
618 mdc_get_rpc_lock(obd->u.cli.cl_rpc_lock, NULL);
619 req->rq_interpret_reply = mdc_close_interpret;
620 req->rq_async_args.pointer_arg[0] = obd->u.cli.cl_rpc_lock;
621 req->rq_async_args.pointer_arg[1] = obd;
622 ptlrpcd_add_req(req);
623 lwi = LWI_TIMEOUT_INTR(MAX(req->rq_timeout * HZ, 1), go_back_to_sleep,
625 rc = l_wait_event(req->rq_reply_waitq, mdc_close_check_reply(req),
627 if (req->rq_repmsg == NULL) {
628 CDEBUG(D_HA, "request failed to send: %p, %d\n", req,
631 rc = req->rq_status ? req->rq_status : -EIO;
632 } else if (rc == 0) {
633 rc = req->rq_repmsg->status;
634 if (req->rq_repmsg->type == PTL_RPC_MSG_ERR) {
635 DEBUG_REQ(D_ERROR, req, "type == PTL_RPC_MSG_ERR, err "
639 } else if (mod == NULL) {
640 CERROR("Unexpected: can't find mdc_open_data, but the "
641 "close succeeded. Please tell CFS.\n");
643 if (!lustre_swab_repbuf(req, 0, sizeof(struct mds_body),
644 lustre_swab_mds_body)) {
645 CERROR("Error unpacking mds_body\n");
649 if (req->rq_async_args.pointer_arg[0] != NULL) {
650 CERROR("returned without dropping rpc_lock: rc %d\n", rc);
651 mdc_close_interpret(req, &req->rq_async_args, rc);
660 int mdc_done_writing(struct obd_export *exp, struct obdo *obdo)
662 struct ptlrpc_request *req;
663 struct mds_body *body;
664 int rc, size[2] = {0, sizeof(*body)};
667 size[0] = mdc_get_secdesc_size();
669 req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
670 MDS_DONE_WRITING, 2, size, NULL);
674 mdc_pack_secdesc(req, size[0]);
676 body = lustre_msg_buf(req->rq_reqmsg, MDS_REQ_REC_OFF,
679 mdc_pack_id(&body->id1, obdo->o_id, 0, obdo->o_mode,
680 obdo->o_mds, obdo->o_fid);
682 body->size = obdo->o_size;
683 body->blocks = obdo->o_blocks;
684 body->flags = obdo->o_flags;
685 body->valid = obdo->o_valid;
687 req->rq_replen = lustre_msg_size(1, &size[1]);
689 rc = ptlrpc_queue_wait(req);
690 ptlrpc_req_finished(req);
694 int mdc_readpage(struct obd_export *exp,
695 struct lustre_id *id,
696 __u64 offset, struct page *page,
697 struct ptlrpc_request **request)
699 struct obd_import *imp = class_exp2cliimp(exp);
700 struct ptlrpc_request *req = NULL;
701 struct ptlrpc_bulk_desc *desc = NULL;
702 struct mds_body *body;
703 int rc, size[2] = {0, sizeof(*body)};
706 CDEBUG(D_INODE, "inode: %ld\n", (long)id->li_stc.u.e3s.l3s_ino);
708 size[0] = mdc_get_secdesc_size();
710 req = ptlrpc_prep_req(imp, LUSTRE_MDS_VERSION, MDS_READPAGE,
713 GOTO(out, rc = -ENOMEM);
714 /* XXX FIXME bug 249 */
715 req->rq_request_portal = MDS_READPAGE_PORTAL;
717 mdc_pack_secdesc(req, size[0]);
719 desc = ptlrpc_prep_bulk_imp(req, 1, BULK_PUT_SINK, MDS_BULK_PORTAL);
721 GOTO(out, rc = -ENOMEM);
722 /* NB req now owns desc and will free it when it gets freed */
724 ptlrpc_prep_bulk_page(desc, page, 0, PAGE_CACHE_SIZE);
725 mdc_readdir_pack(req, 1, offset, PAGE_CACHE_SIZE, id);
727 req->rq_replen = lustre_msg_size(1, &size[1]);
728 rc = ptlrpc_queue_wait(req);
731 body = lustre_swab_repbuf(req, 0, sizeof (*body),
732 lustre_swab_mds_body);
734 CERROR("Can't unpack mds_body\n");
735 GOTO(out, rc = -EPROTO);
738 if (req->rq_bulk->bd_nob_transferred != PAGE_CACHE_SIZE) {
739 CERROR ("Unexpected # bytes transferred: %d"
741 req->rq_bulk->bd_nob_transferred,
743 GOTO (out, rc = -EPROTO);
753 static int mdc_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
754 void *karg, void *uarg)
756 struct obd_device *obd = exp->exp_obd;
757 struct obd_ioctl_data *data = karg;
758 struct obd_import *imp = obd->u.cli.cl_import;
759 struct llog_ctxt *ctxt;
763 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
766 if (!try_module_get(THIS_MODULE)) {
767 CERROR("Can't get module. Is it alive?");
772 case OBD_IOC_CMOBD_SYNC:
775 case OBD_IOC_COBD_CON:
778 case OBD_IOC_COBD_COFF:
781 case OBD_IOC_CLIENT_RECOVER:
782 rc = ptlrpc_recover_import(imp, data->ioc_inlbuf1);
786 case IOC_OSC_SET_ACTIVE:
787 rc = ptlrpc_set_import_active(imp, data->ioc_offset);
789 case OBD_IOC_PARSE: {
790 ctxt = llog_get_context(&exp->exp_obd->obd_llogs,
791 LLOG_CONFIG_REPL_CTXT);
792 rc = class_config_process_llog(ctxt, data->ioc_inlbuf1, NULL);
796 case OBD_IOC_LLOG_INFO:
797 case OBD_IOC_LLOG_PRINT: {
798 ctxt = llog_get_context(&obd->obd_llogs, LLOG_CONFIG_REPL_CTXT);
799 rc = llog_ioctl(ctxt, cmd, data);
805 CERROR("mdc_ioctl(): unrecognised ioctl %#x\n", cmd);
806 GOTO(out, rc = -ENOTTY);
809 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
812 module_put(THIS_MODULE);
818 int mdc_set_info(struct obd_export *exp, obd_count keylen,
819 void *key, obd_count vallen, void *val)
823 if (keylen == strlen("initial_recov") &&
824 memcmp(key, "initial_recov", strlen("initial_recov")) == 0) {
825 struct obd_import *imp = exp->exp_obd->u.cli.cl_import;
826 if (vallen != sizeof(int))
828 imp->imp_initial_recov = *(int *)val;
829 CDEBUG(D_HA, "%s: set imp_no_init_recov = %d\n",
830 exp->exp_obd->obd_name,
831 imp->imp_initial_recov);
833 } else if (keylen >= strlen("mds_type") && strcmp(key, "mds_type") == 0) {
834 struct ptlrpc_request *req;
835 char *bufs[2] = {key, val};
836 int rc, size[2] = {keylen, vallen};
838 req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_OBD_VERSION,
839 OST_SET_INFO, 2, size, bufs);
843 req->rq_replen = lustre_msg_size(0, NULL);
844 rc = ptlrpc_queue_wait(req);
845 ptlrpc_req_finished(req);
847 } else if (keylen >= strlen("inter_mds") && strcmp(key, "inter_mds") == 0) {
848 struct obd_import *imp = class_exp2cliimp(exp);
849 imp->imp_server_timeout = 1;
850 CDEBUG(D_OTHER, "%s: timeout / 2\n", exp->exp_obd->obd_name);
856 static int mdc_statfs(struct obd_device *obd, struct obd_statfs *osfs,
857 unsigned long max_age)
859 struct ptlrpc_request *req;
860 struct obd_statfs *msfs;
861 int rc, size = sizeof(*msfs);
864 /* We could possibly pass max_age in the request (as an absolute
865 * timestamp or a "seconds.usec ago") so the target can avoid doing
866 * extra calls into the filesystem if that isn't necessary (e.g.
867 * during mount that would help a bit). Having relative timestamps
868 * is not so great if request processing is slow, while absolute
869 * timestamps are not ideal because they need time synchronization. */
870 req = ptlrpc_prep_req(obd->u.cli.cl_import, LUSTRE_MDS_VERSION,
871 MDS_STATFS, 0, NULL, NULL);
875 req->rq_replen = lustre_msg_size(1, &size);
877 mdc_get_rpc_lock(obd->u.cli.cl_rpc_lock, NULL);
878 rc = ptlrpc_queue_wait(req);
879 mdc_put_rpc_lock(obd->u.cli.cl_rpc_lock, NULL);
884 msfs = lustre_swab_repbuf(req, 0, sizeof(*msfs),lustre_swab_obd_statfs);
886 CERROR("Can't unpack obd_statfs\n");
887 GOTO(out, rc = -EPROTO);
890 memcpy(osfs, msfs, sizeof (*msfs));
893 ptlrpc_req_finished(req);
898 static int mdc_pin(struct obd_export *exp, obd_id ino, __u32 gen, int type,
899 struct obd_client_handle *handle, int flag)
901 struct ptlrpc_request *req;
902 struct mds_body *body;
903 int rc, size[2] = {0, sizeof(*body)};
906 //size[0] = mdc_get_secdesc_size();
908 req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
909 MDS_PIN, 2, size, NULL);
913 //mdc_pack_secdesc(req, size[0]);
915 body = lustre_msg_buf(req->rq_reqmsg,
916 MDS_REQ_REC_OFF, sizeof(*body));
918 /* FIXME-UMKA: here should be also mdsnum and fid. */
919 mdc_pack_id(&body->id1, ino, gen, type, 0, 0);
922 req->rq_replen = lustre_msg_size(1, &size[1]);
924 mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
925 rc = ptlrpc_queue_wait(req);
926 mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
928 CERROR("pin failed: %d\n", rc);
929 ptlrpc_req_finished(req);
933 body = lustre_swab_repbuf(req, 0, sizeof(*body), lustre_swab_mds_body);
935 ptlrpc_req_finished(req);
939 memcpy(&handle->och_fh, &body->handle, sizeof(body->handle));
940 handle->och_magic = OBD_CLIENT_HANDLE_MAGIC;
942 OBD_ALLOC(handle->och_mod, sizeof(*handle->och_mod));
943 if (handle->och_mod == NULL) {
944 DEBUG_REQ(D_ERROR, req, "can't allocate mdc_open_data");
947 handle->och_mod->mod_open_req = req; /* will be dropped by unpin */
952 static int mdc_unpin(struct obd_export *exp,
953 struct obd_client_handle *handle, int flag)
955 struct ptlrpc_request *req;
956 struct mds_body *body;
957 int rc, size[2] = {0, sizeof(*body)};
960 if (handle->och_magic != OBD_CLIENT_HANDLE_MAGIC)
963 //size[0] = mdc_get_secdesc_size();
965 req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
966 MDS_CLOSE, 2, size, NULL);
970 //mdc_pack_secdesc(req, size[0]);
972 body = lustre_msg_buf(req->rq_reqmsg, MDS_REQ_REC_OFF, sizeof(*body));
973 memcpy(&body->handle, &handle->och_fh, sizeof(body->handle));
976 req->rq_replen = lustre_msg_size(0, NULL);
977 mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
978 rc = ptlrpc_queue_wait(req);
979 mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
982 CERROR("unpin failed: %d\n", rc);
984 ptlrpc_req_finished(req);
985 ptlrpc_req_finished(handle->och_mod->mod_open_req);
986 OBD_FREE(handle->och_mod, sizeof(*handle->och_mod));
990 int mdc_sync(struct obd_export *exp, struct lustre_id *id,
991 struct ptlrpc_request **request)
993 struct ptlrpc_request *req;
994 struct mds_body *body;
995 int size[2] = {0, sizeof(*body)};
999 //size[0] = mdc_get_secdesc_size();
1001 req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
1002 MDS_SYNC, 2, size, NULL);
1004 RETURN(rc = -ENOMEM);
1006 //mdc_pack_secdesc(req, size[0]);
1009 body = lustre_msg_buf(req->rq_reqmsg, MDS_REQ_REC_OFF,
1011 memcpy(&body->id1, id, sizeof(*id));
1014 req->rq_replen = lustre_msg_size(1, &size[1]);
1016 rc = ptlrpc_queue_wait(req);
1017 if (rc || request == NULL)
1018 ptlrpc_req_finished(req);
1025 static int mdc_import_event(struct obd_device *obd, struct obd_import *imp,
1026 enum obd_import_event event)
1030 LASSERT(imp->imp_obd == obd);
1033 case IMP_EVENT_DISCON: {
1036 case IMP_EVENT_INACTIVE: {
1037 if (obd->obd_observer)
1038 rc = obd_notify(obd->obd_observer, obd, 0, 0);
1041 case IMP_EVENT_INVALIDATE: {
1042 struct ldlm_namespace *ns = obd->obd_namespace;
1044 ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
1048 case IMP_EVENT_ACTIVE: {
1049 if (obd->obd_observer)
1050 rc = obd_notify(obd->obd_observer, obd, 1, 0);
1054 CERROR("Unknown import event %d\n", event);
1060 static int mdc_attach(struct obd_device *dev, obd_count len, void *data)
1062 struct lprocfs_static_vars lvars;
1064 lprocfs_init_vars(mdc, &lvars);
1065 return lprocfs_obd_attach(dev, lvars.obd_vars);
1068 static int mdc_detach(struct obd_device *dev)
1070 return lprocfs_obd_detach(dev);
1073 static int mdc_setup(struct obd_device *obd, obd_count len, void *buf)
1075 struct client_obd *cli = &obd->u.cli;
1079 OBD_ALLOC(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
1080 if (!cli->cl_rpc_lock)
1082 mdc_init_rpc_lock(cli->cl_rpc_lock);
1086 OBD_ALLOC(cli->cl_setattr_lock, sizeof (*cli->cl_setattr_lock));
1087 if (!cli->cl_setattr_lock)
1088 GOTO(err_rpc_lock, rc = -ENOMEM);
1089 mdc_init_rpc_lock(cli->cl_setattr_lock);
1091 rc = client_obd_setup(obd, len, buf);
1093 GOTO(err_setattr_lock, rc);
1095 rc = obd_llog_init(obd, &obd->obd_llogs, obd, 0, NULL);
1097 mdc_cleanup(obd, 0);
1098 CERROR("failed to setup llogging subsystems\n");
1104 OBD_FREE(cli->cl_setattr_lock, sizeof (*cli->cl_setattr_lock));
1106 OBD_FREE(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
1111 static int mdc_init_ea_size(struct obd_export *exp, int easize, int cookiesize)
1113 struct obd_device *obd = exp->exp_obd;
1114 struct client_obd *cli = &obd->u.cli;
1117 if (cli->cl_max_mds_easize < easize)
1118 cli->cl_max_mds_easize = easize;
1119 if (cli->cl_max_mds_cookiesize < cookiesize)
1120 cli->cl_max_mds_cookiesize = cookiesize;
1124 static int mdc_precleanup(struct obd_device *obd, int flags)
1128 rc = obd_llog_finish(obd, &obd->obd_llogs, 0);
1130 CERROR("failed to cleanup llogging subsystems\n");
1135 static int mdc_cleanup(struct obd_device *obd, int flags)
1137 struct client_obd *cli = &obd->u.cli;
1139 OBD_FREE(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
1140 OBD_FREE(cli->cl_setattr_lock, sizeof (*cli->cl_setattr_lock));
1144 return client_obd_cleanup(obd, flags);
1148 static int mdc_llog_init(struct obd_device *obd, struct obd_llogs *llogs,
1149 struct obd_device *tgt, int count,
1150 struct llog_catid *logid)
1152 struct llog_ctxt *ctxt;
1156 rc = obd_llog_setup(obd, llogs, LLOG_CONFIG_REPL_CTXT, tgt, 0, NULL,
1159 ctxt = llog_get_context(llogs, LLOG_CONFIG_REPL_CTXT);
1160 ctxt->loc_imp = obd->u.cli.cl_import;
1166 static int mdc_llog_finish(struct obd_device *obd,
1167 struct obd_llogs *llogs, int count)
1172 rc = obd_llog_cleanup(llog_get_context(llogs, LLOG_CONFIG_REPL_CTXT));
1175 static struct obd_device *mdc_get_real_obd(struct obd_export *exp,
1176 char *name, int len)
1179 RETURN(exp->exp_obd);
1182 static int mdc_get_info(struct obd_export *exp, obd_count keylen,
1183 void *key, __u32 *valsize, void *val)
1185 struct ptlrpc_request *req;
1186 char *bufs[1] = {key};
1190 if (!valsize || !val)
1193 if ((keylen < strlen("mdsize") || strcmp(key, "mdsize") != 0) &&
1194 (keylen < strlen("mdsnum") || strcmp(key, "mdsnum") != 0) &&
1195 (keylen < strlen("rootid") || strcmp(key, "rootid") != 0))
1198 req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_OBD_VERSION,
1199 OST_GET_INFO, 1, &keylen, bufs);
1203 req->rq_replen = lustre_msg_size(1, valsize);
1204 rc = ptlrpc_queue_wait(req);
1208 if (keylen >= strlen("rootid") && !strcmp(key, "rootid")) {
1209 struct lustre_id *reply;
1211 reply = lustre_swab_repbuf(req, 0, sizeof(*reply),
1212 lustre_swab_lustre_id);
1213 if (reply == NULL) {
1214 CERROR("Can't unpack %s\n", (char *)key);
1215 GOTO(out_req, rc = -EPROTO);
1218 *(struct lustre_id *)val = *reply;
1222 reply = lustre_swab_repbuf(req, 0, sizeof(*reply),
1223 lustre_swab_generic_32s);
1224 if (reply == NULL) {
1225 CERROR("Can't unpack %s\n", (char *)key);
1226 GOTO(out_req, rc = -EPROTO);
1228 *((__u32 *)val) = *reply;
1231 ptlrpc_req_finished(req);
1235 int mdc_obj_create(struct obd_export *exp, struct obdo *oa,
1236 struct lov_stripe_md **ea, struct obd_trans_info *oti)
1238 struct ptlrpc_request *request;
1239 struct ost_body *body;
1240 int rc, size = sizeof(*body);
1245 request = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_OBD_VERSION,
1246 OST_CREATE, 1, &size, NULL);
1248 GOTO(out_req, rc = -ENOMEM);
1250 body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof (*body));
1251 memcpy(&body->oa, oa, sizeof(body->oa));
1253 request->rq_replen = lustre_msg_size(1, &size);
1254 rc = ptlrpc_queue_wait(request);
1258 body = lustre_swab_repbuf(request, 0, sizeof(*body),
1259 lustre_swab_ost_body);
1261 CERROR ("can't unpack ost_body\n");
1262 GOTO (out_req, rc = -EPROTO);
1265 memcpy(oa, &body->oa, sizeof(*oa));
1267 /* store ino/generation for recovery */
1268 body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof (*body));
1269 body->oa.o_id = oa->o_id;
1270 body->oa.o_generation = oa->o_generation;
1272 CDEBUG(D_HA, "transno: "LPD64"\n", request->rq_repmsg->transno);
1275 ptlrpc_req_finished(request);
1279 int mdc_brw(int rw, struct obd_export *exp, struct obdo *oa,
1280 struct lov_stripe_md *ea, obd_count oa_bufs,
1281 struct brw_page *pgarr, struct obd_trans_info *oti)
1283 struct ptlrpc_bulk_desc *desc;
1284 struct niobuf_remote *niobuf;
1285 struct ptlrpc_request *req;
1286 struct obd_ioobj *ioobj;
1287 struct ost_body *body;
1291 opc = ((rw & OBD_BRW_WRITE) != 0) ? OST_WRITE : OST_READ;
1293 size[0] = sizeof(*body);
1294 size[1] = sizeof(*ioobj);
1295 size[2] = oa_bufs * sizeof(*niobuf);
1297 req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_OBD_VERSION, opc,
1299 LASSERT(req != NULL);
1301 if (opc == OST_WRITE)
1302 desc = ptlrpc_prep_bulk_imp(req, oa_bufs, BULK_GET_SOURCE,
1305 desc = ptlrpc_prep_bulk_imp(req, oa_bufs, BULK_PUT_SINK,
1307 LASSERT(desc != NULL);
1309 body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof(*body));
1310 ioobj = lustre_msg_buf(req->rq_reqmsg, 1, sizeof(*ioobj));
1311 niobuf = lustre_msg_buf(req->rq_reqmsg, 2, oa_bufs * sizeof(*niobuf));
1313 memcpy(&body->oa, oa, sizeof(*oa));
1314 obdo_to_ioobj(oa, ioobj);
1315 ioobj->ioo_bufcnt = oa_bufs;
1317 for (i = 0; i < oa_bufs; i++, niobuf++) {
1318 struct brw_page *pg = &pgarr[i];
1320 LASSERT(pg->count > 0);
1321 LASSERT((pg->disk_offset & ~PAGE_MASK) + pg->count <= PAGE_SIZE);
1323 ptlrpc_prep_bulk_page(desc, pg->pg, pg->disk_offset & ~PAGE_MASK,
1326 niobuf->offset = pg->disk_offset;
1327 niobuf->len = pg->count;
1328 niobuf->flags = pg->flag;
1331 /* size[0] still sizeof (*body) */
1332 if (opc == OST_WRITE) {
1333 /* 1 RC per niobuf */
1334 size[1] = sizeof(__u32) * oa_bufs;
1335 req->rq_replen = lustre_msg_size(2, size);
1337 /* 1 RC for the whole I/O */
1338 req->rq_replen = lustre_msg_size(1, size);
1340 err = ptlrpc_queue_wait(req);
1343 ptlrpc_req_finished(req);
1347 static int mdc_valid_attrs(struct obd_export *exp,
1348 struct lustre_id *id)
1350 struct ldlm_res_id res_id = { .name = {0} };
1351 struct obd_device *obd = exp->exp_obd;
1352 struct lustre_handle lockh;
1353 ldlm_policy_data_t policy;
1357 res_id.name[0] = id_fid(id);
1358 res_id.name[1] = id_group(id);
1359 policy.l_inodebits.bits = MDS_INODELOCK_UPDATE;
1361 CDEBUG(D_INFO, "trying to match res "LPU64"\n",
1364 /* FIXME use LDLM_FL_TEST_LOCK instead */
1365 flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_CBPENDING;
1366 if (ldlm_lock_match(obd->obd_namespace, flags, &res_id,
1367 LDLM_IBITS, &policy, LCK_PR, &lockh)) {
1368 ldlm_lock_decref(&lockh, LCK_PR);
1372 if (ldlm_lock_match(obd->obd_namespace, flags, &res_id,
1373 LDLM_IBITS, &policy, LCK_PW, &lockh)) {
1374 ldlm_lock_decref(&lockh, LCK_PW);
1380 static int mdc_change_cbdata_name(struct obd_export *exp,
1381 struct lustre_id *pid,
1382 char *name, int len,
1383 struct lustre_id *cid,
1384 ldlm_iterator_t it, void *data)
1387 rc = mdc_change_cbdata(exp, cid, it, data);
1391 struct obd_ops mdc_obd_ops = {
1392 .o_owner = THIS_MODULE,
1393 .o_attach = mdc_attach,
1394 .o_detach = mdc_detach,
1395 .o_setup = mdc_setup,
1396 .o_precleanup = mdc_precleanup,
1397 .o_cleanup = mdc_cleanup,
1398 .o_add_conn = client_import_add_conn,
1399 .o_del_conn = client_import_del_conn,
1400 .o_connect = client_connect_import,
1401 .o_disconnect = client_disconnect_export,
1402 .o_iocontrol = mdc_iocontrol,
1403 .o_statfs = mdc_statfs,
1405 .o_unpin = mdc_unpin,
1406 .o_import_event = mdc_import_event,
1407 .o_llog_init = mdc_llog_init,
1408 .o_llog_finish = mdc_llog_finish,
1409 .o_create = mdc_obj_create,
1410 .o_set_info = mdc_set_info,
1411 .o_get_info = mdc_get_info,
1413 .o_init_ea_size = mdc_init_ea_size,
1416 struct md_ops mdc_md_ops = {
1417 .m_getstatus = mdc_getstatus,
1418 .m_getattr = mdc_getattr,
1419 .m_close = mdc_close,
1420 .m_create = mdc_create,
1421 .m_done_writing = mdc_done_writing,
1422 .m_enqueue = mdc_enqueue,
1423 .m_getattr_name = mdc_getattr_name,
1424 .m_intent_lock = mdc_intent_lock,
1426 .m_rename = mdc_rename,
1427 .m_setattr = mdc_setattr,
1429 .m_readpage = mdc_readpage,
1430 .m_unlink = mdc_unlink,
1431 .m_valid_attrs = mdc_valid_attrs,
1432 .m_req2lustre_md = mdc_req2lustre_md,
1433 .m_set_open_replay_data = mdc_set_open_replay_data,
1434 .m_clear_open_replay_data = mdc_clear_open_replay_data,
1435 .m_store_inode_generation = mdc_store_inode_generation,
1436 .m_set_lock_data = mdc_set_lock_data,
1437 .m_get_real_obd = mdc_get_real_obd,
1438 .m_change_cbdata_name = mdc_change_cbdata_name,
1439 .m_change_cbdata = mdc_change_cbdata,
1442 int __init mdc_init(void)
1444 struct lprocfs_static_vars lvars;
1445 lprocfs_init_vars(mdc, &lvars);
1446 return class_register_type(&mdc_obd_ops, &mdc_md_ops, lvars.module_vars,
1451 static void /*__exit*/ mdc_exit(void)
1453 class_unregister_type(LUSTRE_MDC_NAME);
1456 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
1457 MODULE_DESCRIPTION("Lustre Metadata Client");
1458 MODULE_LICENSE("GPL");
1460 EXPORT_SYMBOL(mdc_req2lustre_md);
1461 EXPORT_SYMBOL(mdc_change_cbdata);
1462 EXPORT_SYMBOL(mdc_getstatus);
1463 EXPORT_SYMBOL(mdc_getattr);
1464 EXPORT_SYMBOL(mdc_getattr_name);
1465 EXPORT_SYMBOL(mdc_create);
1466 EXPORT_SYMBOL(mdc_unlink);
1467 EXPORT_SYMBOL(mdc_rename);
1468 EXPORT_SYMBOL(mdc_link);
1469 EXPORT_SYMBOL(mdc_readpage);
1470 EXPORT_SYMBOL(mdc_setattr);
1471 EXPORT_SYMBOL(mdc_close);
1472 EXPORT_SYMBOL(mdc_done_writing);
1473 EXPORT_SYMBOL(mdc_sync);
1474 EXPORT_SYMBOL(mdc_set_open_replay_data);
1475 EXPORT_SYMBOL(mdc_clear_open_replay_data);
1476 EXPORT_SYMBOL(mdc_store_inode_generation);
1478 module_init(mdc_init);
1479 module_exit(mdc_exit);