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)
48 int ngroups = current_ngroups;
50 if (ngroups > LUSTRE_MAX_GROUPS)
51 ngroups = LUSTRE_MAX_GROUPS;
52 return sizeof(struct mds_req_sec_desc) +
53 sizeof(__u32) * ngroups;
57 * because group info might have changed since last time
58 * we call get_secdesc_size(), so here we did more sanity check
59 * to prevent garbage gids
61 void mdc_pack_secdesc(struct ptlrpc_request *req, int size)
63 struct mds_req_sec_desc *rsd;
64 struct group_info *ginfo;
66 rsd = lustre_msg_buf(req->rq_reqmsg, MDS_REQ_SECDESC_OFF, size);
67 rsd->rsd_uid = current->uid;
68 rsd->rsd_gid = current->gid;
69 rsd->rsd_fsuid = current->fsuid;
70 rsd->rsd_fsgid = current->fsgid;
71 rsd->rsd_cap = current->cap_effective;
72 rsd->rsd_ngroups = (size - sizeof(*rsd)) / sizeof(__u32);
73 LASSERT(rsd->rsd_ngroups <= LUSTRE_MAX_GROUPS);
75 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,4)
77 get_group_info(current->group_info);
78 ginfo = current->group_info;
80 if (rsd->rsd_ngroups > ginfo->ngropus)
81 rsd->rsd_ngroups = ginfo->ngroups;
82 memcpy(rsd->rsd_groups, ginfo->blocks[0],
83 rsd->rsd_ngroups * sizeof(__u32));
85 LASSERT(rsd->rsd_ngroups <= NGROUPS);
86 if (rsd->rsd_ngroups > current->ngroups)
87 rsd->rsd_ngroups = current->ngroups;
88 memcpy(rsd->rsd_groups, current->groups,
89 rsd->rsd_ngroups * sizeof(__u32));
93 extern int mds_queue_req(struct ptlrpc_request *);
94 /* Helper that implements most of mdc_getstatus and signal_completed_replay. */
95 /* XXX this should become mdc_get_info("key"), sending MDS_GET_INFO RPC */
96 static int send_getstatus(struct obd_import *imp, struct ll_fid *rootfid,
97 int level, int msg_flags)
99 struct ptlrpc_request *req;
100 struct mds_body *body;
101 int rc, size[2] = {0, sizeof(*body)};
104 //size[0] = mdc_get_secdesc_size();
106 req = ptlrpc_prep_req(imp, LUSTRE_MDS_VERSION, MDS_GETSTATUS,
109 GOTO(out, rc = -ENOMEM);
111 //mdc_pack_secdesc(req, size[0]);
113 body = lustre_msg_buf(req->rq_reqmsg, MDS_REQ_REC_OFF, sizeof (*body));
114 req->rq_send_state = level;
115 req->rq_replen = lustre_msg_size(1, &size[1]);
117 req->rq_reqmsg->flags |= msg_flags;
118 rc = ptlrpc_queue_wait(req);
121 body = lustre_swab_repbuf (req, 0, sizeof (*body),
122 lustre_swab_mds_body);
124 CERROR ("Can't extract mds_body\n");
125 GOTO (out, rc = -EPROTO);
128 memcpy(rootfid, &body->fid1, sizeof(*rootfid));
130 CDEBUG(D_NET, "root ino="LPU64", last_committed="LPU64
131 ", last_xid="LPU64"\n",
132 rootfid->id, req->rq_repmsg->last_committed,
133 req->rq_repmsg->last_xid);
138 ptlrpc_req_finished(req);
142 /* This should be mdc_get_info("rootfid") */
143 int mdc_getstatus(struct obd_export *exp, struct ll_fid *rootfid)
145 return send_getstatus(class_exp2cliimp(exp), rootfid, LUSTRE_IMP_FULL,
149 int mdc_getattr_common(struct obd_export *exp, unsigned int ea_size,
150 struct ptlrpc_request *req)
152 struct mds_body *body;
155 int repsize[2] = {sizeof(*body), 0};
159 /* request message already built */
162 repsize[bufcount++] = ea_size;
163 CDEBUG(D_INODE, "reserved %u bytes for MD/symlink in packet\n",
166 req->rq_replen = lustre_msg_size(bufcount, repsize);
168 mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
169 rc = ptlrpc_queue_wait(req);
170 mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
174 body = lustre_swab_repbuf (req, 0, sizeof (*body),
175 lustre_swab_mds_body);
177 CERROR ("Can't unpack mds_body\n");
181 CDEBUG(D_NET, "mode: %o\n", body->mode);
183 LASSERT_REPSWAB (req, 1);
184 if (body->eadatasize != 0) {
185 /* reply indicates presence of eadata; check it's there... */
186 eadata = lustre_msg_buf (req->rq_repmsg, 1, body->eadatasize);
187 if (eadata == NULL) {
188 CERROR ("Missing/short eadata\n");
196 int mdc_getattr(struct obd_export *exp, struct ll_fid *fid,
197 unsigned long valid, unsigned int ea_size,
198 struct ptlrpc_request **request)
200 struct ptlrpc_request *req;
201 struct mds_body *body;
202 int size[2] = {0, sizeof(*body)};
206 /* XXX do we need to make another request here? We just did a getattr
207 * to do the lookup in the first place.
209 size[0] = mdc_get_secdesc_size();
211 req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
212 MDS_GETATTR, 2, size, NULL);
214 GOTO(out, rc = -ENOMEM);
216 mdc_pack_secdesc(req, size[0]);
218 body = lustre_msg_buf(req->rq_reqmsg, MDS_REQ_REC_OFF, sizeof (*body));
219 memcpy(&body->fid1, fid, sizeof(*fid));
221 body->eadatasize = ea_size;
223 rc = mdc_getattr_common(exp, ea_size, req);
225 ptlrpc_req_finished (req);
233 int mdc_getattr_name(struct obd_export *exp, struct ll_fid *fid,
234 char *filename, int namelen, unsigned long valid,
235 unsigned int ea_size, struct ptlrpc_request **request)
237 struct ptlrpc_request *req;
238 struct mds_body *body;
239 int rc, size[3] = {0, sizeof(*body), namelen};
242 size[0] = mdc_get_secdesc_size();
244 req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
245 MDS_GETATTR_NAME, 3, size, NULL);
247 GOTO(out, rc = -ENOMEM);
249 mdc_pack_secdesc(req, size[0]);
251 body = lustre_msg_buf(req->rq_reqmsg, MDS_REQ_REC_OFF, sizeof (*body));
252 memcpy(&body->fid1, fid, sizeof(*fid));
254 body->eadatasize = ea_size;
256 if (filename != NULL) {
257 LASSERT (strnlen (filename, namelen) == namelen - 1);
258 memcpy(lustre_msg_buf(req->rq_reqmsg, 2, namelen),
261 LASSERT(namelen == 1);
264 rc = mdc_getattr_common(exp, ea_size, req);
266 ptlrpc_req_finished (req);
274 /* This should be called with both the request and the reply still packed. */
275 int mdc_store_inode_generation(struct obd_export *exp,
276 struct ptlrpc_request *req,
277 int reqoff, int repoff)
279 struct mds_rec_create *rec =
280 lustre_msg_buf(req->rq_reqmsg, reqoff, sizeof(*rec));
281 struct mds_body *body =
282 lustre_msg_buf(req->rq_repmsg, repoff, sizeof(*body));
284 LASSERT (rec != NULL);
285 LASSERT (body != NULL);
287 memcpy(&rec->cr_replayfid, &body->fid1, sizeof rec->cr_replayfid);
288 DEBUG_REQ(D_HA, req, "storing generation %u for ino "LPU64,
289 rec->cr_replayfid.generation, rec->cr_replayfid.id);
293 int mdc_req2lustre_md(struct obd_export *exp_mdc, struct ptlrpc_request *req,
294 unsigned int offset, struct obd_export *exp_osc,
295 struct lustre_md *md)
301 memset(md, 0, sizeof(*md));
303 md->body = lustre_msg_buf(req->rq_repmsg, offset, sizeof (*md->body));
304 LASSERT (md->body != NULL);
305 LASSERT_REPSWABBED (req, offset);
307 if (!(md->body->valid & OBD_MD_FLEASIZE) &&
308 !(md->body->valid & OBD_MD_FLDIREA))
311 /* ea is presented in reply, parse it */
312 if (S_ISREG(md->body->mode)) {
314 struct lov_mds_md *lmm;
316 if (md->body->eadatasize == 0) {
317 CERROR ("OBD_MD_FLEASIZE set, but eadatasize 0\n");
320 lmmsize = md->body->eadatasize;
321 lmm = lustre_msg_buf(req->rq_repmsg, offset + 1, lmmsize);
322 LASSERT (lmm != NULL);
323 LASSERT_REPSWABBED (req, offset + 1);
325 rc = obd_unpackmd(exp_osc, &md->lsm, lmm, lmmsize);
327 LASSERT (rc >= sizeof (*md->lsm));
330 } else if (S_ISDIR(md->body->mode)) {
333 LASSERT(exp_mdc != NULL);
334 /* dir can be non-splitted */
335 if (md->body->eadatasize == 0)
338 mdsize = md->body->eadatasize;
339 mea = lustre_msg_buf(req->rq_repmsg, offset + 1, mdsize);
340 LASSERT(mea != NULL);
342 rc = obd_unpackmd(exp_mdc, (void *) &md->mea,
343 (void *) mea, mdsize);
345 LASSERT (rc >= sizeof (*md->mea));
354 static void mdc_commit_open(struct ptlrpc_request *req)
356 struct mdc_open_data *mod = req->rq_cb_data;
360 if (mod->mod_close_req != NULL)
361 mod->mod_close_req->rq_cb_data = NULL;
363 if (mod->mod_och != NULL)
364 mod->mod_och->och_mod = NULL;
366 OBD_FREE(mod, sizeof(*mod));
367 req->rq_cb_data = NULL;
370 static void mdc_replay_open(struct ptlrpc_request *req)
372 struct mdc_open_data *mod = req->rq_cb_data;
373 struct obd_client_handle *och;
374 struct ptlrpc_request *close_req;
375 struct lustre_handle old;
376 struct mds_body *body;
379 body = lustre_swab_repbuf(req, 1, sizeof(*body), lustre_swab_mds_body);
380 LASSERT (body != NULL);
383 DEBUG_REQ(D_ERROR, req,
384 "can't properly replay without open data");
391 struct lustre_handle *file_fh;
392 LASSERT(och->och_magic == OBD_CLIENT_HANDLE_MAGIC);
393 file_fh = &och->och_fh;
394 CDEBUG(D_HA, "updating handle from "LPX64" to "LPX64"\n",
395 file_fh->cookie, body->handle.cookie);
396 memcpy(&old, file_fh, sizeof(old));
397 memcpy(file_fh, &body->handle, sizeof(*file_fh));
400 close_req = mod->mod_close_req;
401 if (close_req != NULL) {
402 struct mds_body *close_body;
403 LASSERT(close_req->rq_reqmsg->opc == MDS_CLOSE);
404 close_body = lustre_msg_buf(close_req->rq_reqmsg,
406 sizeof(*close_body));
408 LASSERT(!memcmp(&old, &close_body->handle, sizeof old));
409 DEBUG_REQ(D_HA, close_req, "updating close body with new fh");
410 memcpy(&close_body->handle, &body->handle,
411 sizeof(close_body->handle));
417 int mdc_set_open_replay_data(struct obd_export *exp,
418 struct obd_client_handle *och,
419 struct ptlrpc_request *open_req)
421 struct mdc_open_data *mod;
422 struct mds_rec_create *rec;
423 struct mds_body *body;
425 rec = lustre_msg_buf(open_req->rq_reqmsg, MDS_REQ_INTENT_REC_OFF,
427 body = lustre_msg_buf(open_req->rq_repmsg, 1, sizeof(*body));
429 LASSERT(rec != NULL);
430 /* outgoing messages always in my byte order */
431 LASSERT(body != NULL);
432 /* incoming message in my byte order (it's been swabbed) */
433 LASSERT_REPSWABBED(open_req, 1);
435 OBD_ALLOC(mod, sizeof(*mod));
437 DEBUG_REQ(D_ERROR, open_req, "can't allocate mdc_open_data");
443 mod->mod_open_req = open_req;
445 memcpy(&rec->cr_replayfid, &body->fid1, sizeof rec->cr_replayfid);
446 open_req->rq_replay_cb = mdc_replay_open;
447 open_req->rq_commit_cb = mdc_commit_open;
448 open_req->rq_cb_data = mod;
449 DEBUG_REQ(D_HA, open_req, "set up replay data");
453 int mdc_clear_open_replay_data(struct obd_export *exp,
454 struct obd_client_handle *och)
456 struct mdc_open_data *mod = och->och_mod;
458 /* Don't free the structure now (it happens in mdc_commit_open, after
459 * we're sure we won't need to fix up the close request in the future),
460 * but make sure that replay doesn't poke at the och, which is about to
462 LASSERT(mod != LP_POISON);
469 static void mdc_commit_close(struct ptlrpc_request *req)
471 struct mdc_open_data *mod = req->rq_cb_data;
472 struct ptlrpc_request *open_req;
473 struct obd_import *imp = req->rq_import;
475 DEBUG_REQ(D_HA, req, "close req committed");
479 mod->mod_close_req = NULL;
480 req->rq_cb_data = NULL;
481 req->rq_commit_cb = NULL;
483 open_req = mod->mod_open_req;
484 LASSERT(open_req != NULL);
485 LASSERT(open_req != LP_POISON);
486 LASSERT(open_req->rq_type != LI_POISON);
488 DEBUG_REQ(D_HA, open_req, "open req balanced");
489 if (open_req->rq_transno == 0) {
490 DEBUG_REQ(D_ERROR, open_req, "BUG 3892 open");
491 DEBUG_REQ(D_ERROR, req, "BUG 3892 close");
492 LASSERTF(open_req->rq_transno != 0, "BUG 3892");
494 LASSERT(open_req->rq_import == imp);
496 /* We no longer want to preserve this for transno-unconditional
498 spin_lock(&open_req->rq_lock);
499 open_req->rq_replay = 0;
500 spin_unlock(&open_req->rq_lock);
503 static int mdc_close_interpret(struct ptlrpc_request *req, void *data, int rc)
505 union ptlrpc_async_args *aa = data;
506 struct mdc_rpc_lock *rpc_lock;
507 struct obd_device *obd = aa->pointer_arg[1];
510 spin_lock_irqsave(&req->rq_lock, flags);
511 rpc_lock = aa->pointer_arg[0];
512 aa->pointer_arg[0] = NULL;
513 spin_unlock_irqrestore (&req->rq_lock, flags);
515 if (rpc_lock == NULL) {
516 CERROR("called with NULL rpc_lock\n");
518 mdc_put_rpc_lock(rpc_lock, NULL);
519 LASSERTF(rpc_lock == obd->u.cli.cl_rpc_lock, "%p != %p\n",
520 rpc_lock, obd->u.cli.cl_rpc_lock);
522 wake_up(&req->rq_reply_waitq);
526 /* We can't use ptlrpc_check_reply, because we don't want to wake up for
527 * anything but a reply or an error. */
528 static int mdc_close_check_reply(struct ptlrpc_request *req)
533 spin_lock_irqsave(&req->rq_lock, flags);
534 if (req->rq_async_args.pointer_arg[0] == NULL)
536 spin_unlock_irqrestore (&req->rq_lock, flags);
540 static int go_back_to_sleep(void *unused)
545 int mdc_close(struct obd_export *exp, struct obdo *oa,
546 struct obd_client_handle *och, struct ptlrpc_request **request)
548 struct obd_device *obd = class_exp2obd(exp);
549 int reqsize[3] = {0, sizeof(struct mds_body),
550 obd->u.cli.cl_max_mds_cookiesize};
551 int rc, repsize[3] = {sizeof(struct mds_body),
552 obd->u.cli.cl_max_mds_easize,
553 obd->u.cli.cl_max_mds_cookiesize};
554 struct ptlrpc_request *req;
555 struct mdc_open_data *mod;
556 struct l_wait_info lwi;
559 //reqsize[0] = mdc_get_secdesc_size();
561 req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
562 MDS_CLOSE, 3, reqsize, NULL);
564 GOTO(out, rc = -ENOMEM);
566 //mdc_pack_secdesc(req, reqsize[0]);
568 /* Ensure that this close's handle is fixed up during replay. */
569 LASSERT(och != NULL);
571 if (likely(mod != NULL)) {
572 mod->mod_close_req = req;
573 LASSERT(mod->mod_open_req->rq_type != LI_POISON);
574 DEBUG_REQ(D_HA, mod->mod_open_req, "matched open req %p",
577 CDEBUG(D_HA, "couldn't find open req; expecting close error\n");
580 mdc_close_pack(req, 1, oa, oa->o_valid, och);
582 req->rq_replen = lustre_msg_size(3, repsize);
583 req->rq_commit_cb = mdc_commit_close;
584 LASSERT(req->rq_cb_data == NULL);
585 req->rq_cb_data = mod;
587 /* We hand a ref to the rpcd here, so we need another one of our own. */
588 ptlrpc_request_addref(req);
590 mdc_get_rpc_lock(obd->u.cli.cl_rpc_lock, NULL);
591 req->rq_interpret_reply = mdc_close_interpret;
592 req->rq_async_args.pointer_arg[0] = obd->u.cli.cl_rpc_lock;
593 req->rq_async_args.pointer_arg[1] = obd;
594 ptlrpcd_add_req(req);
595 lwi = LWI_TIMEOUT_INTR(MAX(req->rq_timeout * HZ, 1), go_back_to_sleep,
597 rc = l_wait_event(req->rq_reply_waitq, mdc_close_check_reply(req),
599 if (req->rq_repmsg == NULL) {
600 CDEBUG(D_HA, "request failed to send: %p, %d\n", req,
603 rc = req->rq_status ? req->rq_status : -EIO;
604 } else if (rc == 0) {
605 rc = req->rq_repmsg->status;
606 if (req->rq_repmsg->type == PTL_RPC_MSG_ERR) {
607 DEBUG_REQ(D_ERROR, req, "type == PTL_RPC_MSG_ERR, err "
611 } else if (mod == NULL) {
612 CERROR("Unexpected: can't find mdc_open_data, but the "
613 "close succeeded. Please tell CFS.\n");
615 if (!lustre_swab_repbuf(req, 0, sizeof(struct mds_body),
616 lustre_swab_mds_body)) {
617 CERROR("Error unpacking mds_body\n");
621 if (req->rq_async_args.pointer_arg[0] != NULL) {
622 CERROR("returned without dropping rpc_lock: rc %d\n", rc);
623 mdc_close_interpret(req, &req->rq_async_args, rc);
624 portals_debug_dumplog();
633 int mdc_done_writing(struct obd_export *exp, struct obdo *obdo)
635 struct ptlrpc_request *req;
636 struct mds_body *body;
637 int rc, size[2] = {0, sizeof(*body)};
640 size[0] = mdc_get_secdesc_size();
642 req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
643 MDS_DONE_WRITING, 2, size, NULL);
647 mdc_pack_secdesc(req, size[0]);
649 body = lustre_msg_buf(req->rq_reqmsg, MDS_REQ_REC_OFF, sizeof(*body));
650 mdc_pack_fid(&body->fid1, obdo->o_id, 0, obdo->o_mode);
651 body->size = obdo->o_size;
652 body->blocks = obdo->o_blocks;
653 body->flags = obdo->o_flags;
654 body->valid = obdo->o_valid;
655 // memcpy(&body->handle, &och->och_fh, sizeof(body->handle));
657 req->rq_replen = lustre_msg_size(1, &size[1]);
659 rc = ptlrpc_queue_wait(req);
660 ptlrpc_req_finished(req);
664 int mdc_readpage(struct obd_export *exp, struct ll_fid *mdc_fid,
665 __u64 offset, struct page *page,
666 struct ptlrpc_request **request)
668 struct obd_import *imp = class_exp2cliimp(exp);
669 struct ptlrpc_request *req = NULL;
670 struct ptlrpc_bulk_desc *desc = NULL;
671 struct mds_body *body;
672 int rc, size[2] = {0, sizeof(*body)};
675 CDEBUG(D_INODE, "inode: %ld\n", (long)mdc_fid->id);
677 size[0] = mdc_get_secdesc_size();
679 req = ptlrpc_prep_req(imp, LUSTRE_MDS_VERSION, MDS_READPAGE,
682 GOTO(out, rc = -ENOMEM);
683 /* XXX FIXME bug 249 */
684 req->rq_request_portal = MDS_READPAGE_PORTAL;
686 mdc_pack_secdesc(req, size[0]);
688 desc = ptlrpc_prep_bulk_imp(req, 1, BULK_PUT_SINK, MDS_BULK_PORTAL);
690 GOTO(out, rc = -ENOMEM);
691 /* NB req now owns desc and will free it when it gets freed */
693 ptlrpc_prep_bulk_page(desc, page, 0, PAGE_CACHE_SIZE);
695 mdc_readdir_pack(req, 1, offset, PAGE_CACHE_SIZE, mdc_fid);
697 req->rq_replen = lustre_msg_size(1, &size[1]);
698 rc = ptlrpc_queue_wait(req);
701 body = lustre_swab_repbuf(req, 0, sizeof (*body),
702 lustre_swab_mds_body);
704 CERROR("Can't unpack mds_body\n");
705 GOTO(out, rc = -EPROTO);
708 if (req->rq_bulk->bd_nob_transferred != PAGE_CACHE_SIZE) {
709 CERROR ("Unexpected # bytes transferred: %d"
711 req->rq_bulk->bd_nob_transferred,
713 GOTO (out, rc = -EPROTO);
723 static int mdc_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
724 void *karg, void *uarg)
726 struct obd_device *obd = exp->exp_obd;
727 struct obd_ioctl_data *data = karg;
728 struct obd_import *imp = obd->u.cli.cl_import;
729 struct llog_ctxt *ctxt;
733 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
736 if (!try_module_get(THIS_MODULE)) {
737 CERROR("Can't get module. Is it alive?");
742 case OBD_IOC_CLIENT_RECOVER:
743 rc = ptlrpc_recover_import(imp, data->ioc_inlbuf1);
747 case IOC_OSC_SET_ACTIVE:
748 rc = ptlrpc_set_import_active(imp, data->ioc_offset);
750 case OBD_IOC_PARSE: {
751 ctxt = llog_get_context(&exp->exp_obd->obd_llogs,
752 LLOG_CONFIG_REPL_CTXT);
753 rc = class_config_process_llog(ctxt, data->ioc_inlbuf1, NULL);
757 case OBD_IOC_LLOG_INFO:
758 case OBD_IOC_LLOG_PRINT: {
759 ctxt = llog_get_context(&obd->obd_llogs, LLOG_CONFIG_REPL_CTXT);
760 rc = llog_ioctl(ctxt, cmd, data);
766 CERROR("mdc_ioctl(): unrecognised ioctl %#x\n", cmd);
767 GOTO(out, rc = -ENOTTY);
770 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
773 module_put(THIS_MODULE);
779 int mdc_set_info(struct obd_export *exp, obd_count keylen,
780 void *key, obd_count vallen, void *val)
784 if (keylen == strlen("initial_recov") &&
785 memcmp(key, "initial_recov", strlen("initial_recov")) == 0) {
786 struct obd_import *imp = exp->exp_obd->u.cli.cl_import;
787 if (vallen != sizeof(int))
789 imp->imp_initial_recov = *(int *)val;
790 CDEBUG(D_HA, "%s: set imp_no_init_recov = %d\n",
791 exp->exp_obd->obd_name,
792 imp->imp_initial_recov);
794 } else if (keylen >= strlen("mds_num") && strcmp(key, "mds_num") == 0) {
795 struct ptlrpc_request *req;
796 int rc, size[2] = {keylen, vallen};
797 char *bufs[2] = {key, val};
799 req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_OBD_VERSION,
800 OST_SET_INFO, 2, size, bufs);
804 req->rq_replen = lustre_msg_size(0, NULL);
805 rc = ptlrpc_queue_wait(req);
806 ptlrpc_req_finished(req);
808 } else if (keylen >= strlen("inter_mds") && strcmp(key, "inter_mds") == 0) {
809 struct obd_import *imp = class_exp2cliimp(exp);
810 imp->imp_server_timeout = 1;
811 CDEBUG(D_OTHER, "%s: timeout / 2\n", exp->exp_obd->obd_name);
817 static int mdc_statfs(struct obd_device *obd, struct obd_statfs *osfs,
818 unsigned long max_age)
820 struct ptlrpc_request *req;
821 struct obd_statfs *msfs;
822 int rc, size = sizeof(*msfs);
825 /* We could possibly pass max_age in the request (as an absolute
826 * timestamp or a "seconds.usec ago") so the target can avoid doing
827 * extra calls into the filesystem if that isn't necessary (e.g.
828 * during mount that would help a bit). Having relative timestamps
829 * is not so great if request processing is slow, while absolute
830 * timestamps are not ideal because they need time synchronization. */
831 req = ptlrpc_prep_req(obd->u.cli.cl_import, LUSTRE_MDS_VERSION,
832 MDS_STATFS, 0, NULL, NULL);
836 req->rq_replen = lustre_msg_size(1, &size);
838 mdc_get_rpc_lock(obd->u.cli.cl_rpc_lock, NULL);
839 rc = ptlrpc_queue_wait(req);
840 mdc_put_rpc_lock(obd->u.cli.cl_rpc_lock, NULL);
845 msfs = lustre_swab_repbuf(req, 0, sizeof(*msfs),lustre_swab_obd_statfs);
847 CERROR("Can't unpack obd_statfs\n");
848 GOTO(out, rc = -EPROTO);
851 memcpy(osfs, msfs, sizeof (*msfs));
854 ptlrpc_req_finished(req);
859 static int mdc_pin(struct obd_export *exp, obd_id ino, __u32 gen, int type,
860 struct obd_client_handle *handle, int flag)
862 struct ptlrpc_request *req;
863 struct mds_body *body;
864 int rc, size[2] = {0, sizeof(*body)};
867 //size[0] = mdc_get_secdesc_size();
869 req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
870 MDS_PIN, 2, size, NULL);
874 //mdc_pack_secdesc(req, size[0]);
876 body = lustre_msg_buf(req->rq_reqmsg, MDS_REQ_REC_OFF, sizeof(*body));
877 mdc_pack_fid(&body->fid1, ino, gen, type);
880 req->rq_replen = lustre_msg_size(1, &size[1]);
882 mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
883 rc = ptlrpc_queue_wait(req);
884 mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
886 CERROR("pin failed: %d\n", rc);
887 ptlrpc_req_finished(req);
891 body = lustre_swab_repbuf(req, 0, sizeof(*body), lustre_swab_mds_body);
893 ptlrpc_req_finished(req);
897 memcpy(&handle->och_fh, &body->handle, sizeof(body->handle));
898 handle->och_magic = OBD_CLIENT_HANDLE_MAGIC;
900 OBD_ALLOC(handle->och_mod, sizeof(*handle->och_mod));
901 if (handle->och_mod == NULL) {
902 DEBUG_REQ(D_ERROR, req, "can't allocate mdc_open_data");
905 handle->och_mod->mod_open_req = req; /* will be dropped by unpin */
910 static int mdc_unpin(struct obd_export *exp,
911 struct obd_client_handle *handle, int flag)
913 struct ptlrpc_request *req;
914 struct mds_body *body;
915 int rc, size[2] = {0, sizeof(*body)};
918 if (handle->och_magic != OBD_CLIENT_HANDLE_MAGIC)
921 //size[0] = mdc_get_secdesc_size();
923 req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
924 MDS_CLOSE, 2, size, NULL);
928 //mdc_pack_secdesc(req, size[0]);
930 body = lustre_msg_buf(req->rq_reqmsg, MDS_REQ_REC_OFF, sizeof(*body));
931 memcpy(&body->handle, &handle->och_fh, sizeof(body->handle));
934 req->rq_replen = lustre_msg_size(0, NULL);
935 mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
936 rc = ptlrpc_queue_wait(req);
937 mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
940 CERROR("unpin failed: %d\n", rc);
942 ptlrpc_req_finished(req);
943 ptlrpc_req_finished(handle->och_mod->mod_open_req);
944 OBD_FREE(handle->och_mod, sizeof(*handle->och_mod));
948 int mdc_sync(struct obd_export *exp, struct ll_fid *fid,
949 struct ptlrpc_request **request)
951 struct ptlrpc_request *req;
952 struct mds_body *body;
953 int size[2] = {0, sizeof(*body)};
957 //size[0] = mdc_get_secdesc_size();
959 req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
960 MDS_SYNC, 2, size, NULL);
962 RETURN(rc = -ENOMEM);
964 //mdc_pack_secdesc(req, size[0]);
967 body = lustre_msg_buf(req->rq_reqmsg, MDS_REQ_REC_OFF,
969 memcpy(&body->fid1, fid, sizeof(*fid));
972 req->rq_replen = lustre_msg_size(1, &size[1]);
974 rc = ptlrpc_queue_wait(req);
975 if (rc || request == NULL)
976 ptlrpc_req_finished(req);
983 static int mdc_import_event(struct obd_device *obd, struct obd_import *imp,
984 enum obd_import_event event)
988 LASSERT(imp->imp_obd == obd);
991 case IMP_EVENT_DISCON: {
994 case IMP_EVENT_INACTIVE: {
995 if (obd->obd_observer)
996 rc = obd_notify(obd->obd_observer, obd, 0, 0);
999 case IMP_EVENT_INVALIDATE: {
1000 struct ldlm_namespace *ns = obd->obd_namespace;
1002 ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
1006 case IMP_EVENT_ACTIVE: {
1007 if (obd->obd_observer)
1008 rc = obd_notify(obd->obd_observer, obd, 1, 0);
1012 CERROR("Unknown import event %d\n", event);
1018 static int mdc_attach(struct obd_device *dev, obd_count len, void *data)
1020 struct lprocfs_static_vars lvars;
1022 lprocfs_init_vars(mdc, &lvars);
1023 return lprocfs_obd_attach(dev, lvars.obd_vars);
1026 static int mdc_detach(struct obd_device *dev)
1028 return lprocfs_obd_detach(dev);
1031 static int mdc_setup(struct obd_device *obd, obd_count len, void *buf)
1033 struct client_obd *cli = &obd->u.cli;
1037 OBD_ALLOC(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
1038 if (!cli->cl_rpc_lock)
1040 mdc_init_rpc_lock(cli->cl_rpc_lock);
1044 OBD_ALLOC(cli->cl_setattr_lock, sizeof (*cli->cl_setattr_lock));
1045 if (!cli->cl_setattr_lock)
1046 GOTO(err_rpc_lock, rc = -ENOMEM);
1047 mdc_init_rpc_lock(cli->cl_setattr_lock);
1049 rc = client_obd_setup(obd, len, buf);
1051 GOTO(err_setattr_lock, rc);
1053 rc = obd_llog_init(obd, &obd->obd_llogs, obd, 0, NULL);
1055 mdc_cleanup(obd, 0);
1056 CERROR("failed to setup llogging subsystems\n");
1062 OBD_FREE(cli->cl_setattr_lock, sizeof (*cli->cl_setattr_lock));
1064 OBD_FREE(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
1069 static int mdc_init_ea_size(struct obd_export *exp, int easize, int cookiesize)
1071 struct obd_device *obd = exp->exp_obd;
1072 struct client_obd *cli = &obd->u.cli;
1075 if (cli->cl_max_mds_easize < easize)
1076 cli->cl_max_mds_easize = easize;
1077 if (cli->cl_max_mds_cookiesize < cookiesize)
1078 cli->cl_max_mds_cookiesize = cookiesize;
1082 static int mdc_precleanup(struct obd_device *obd, int flags)
1086 rc = obd_llog_finish(obd, &obd->obd_llogs, 0);
1088 CERROR("failed to cleanup llogging subsystems\n");
1093 static int mdc_cleanup(struct obd_device *obd, int flags)
1095 struct client_obd *cli = &obd->u.cli;
1097 OBD_FREE(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
1098 OBD_FREE(cli->cl_setattr_lock, sizeof (*cli->cl_setattr_lock));
1102 return client_obd_cleanup(obd, flags);
1106 static int mdc_llog_init(struct obd_device *obd, struct obd_llogs *llogs,
1107 struct obd_device *tgt, int count,
1108 struct llog_catid *logid)
1110 struct llog_ctxt *ctxt;
1114 rc = obd_llog_setup(obd, llogs, LLOG_CONFIG_REPL_CTXT, tgt, 0, NULL,
1117 ctxt = llog_get_context(llogs, LLOG_CONFIG_REPL_CTXT);
1118 ctxt->loc_imp = obd->u.cli.cl_import;
1124 static int mdc_llog_finish(struct obd_device *obd,
1125 struct obd_llogs *llogs, int count)
1130 rc = obd_llog_cleanup(llog_get_context(llogs, LLOG_CONFIG_REPL_CTXT));
1133 static struct obd_device *mdc_get_real_obd(struct obd_export *exp,
1134 char *name, int len)
1137 RETURN(exp->exp_obd);
1140 static int mdc_get_info(struct obd_export *exp, obd_count keylen,
1141 void *key, __u32 *vallen, void *val)
1144 if (!vallen || !val)
1147 if (keylen >= strlen("mdsize") && strcmp(key, "mdsize") == 0) {
1148 struct ptlrpc_request *req;
1150 char *bufs[1] = {key};
1152 req = ptlrpc_prep_req(class_exp2cliimp(exp),
1153 LUSTRE_OBD_VERSION, OST_GET_INFO,
1158 req->rq_replen = lustre_msg_size(1, vallen);
1159 rc = ptlrpc_queue_wait(req);
1163 /* FIXME: right swap routine here! */
1164 reply = lustre_swab_repbuf(req, 0, sizeof(*reply), NULL);
1165 if (reply == NULL) {
1166 CERROR("Can't unpack mdsize\n");
1167 GOTO(out, rc = -EPROTO);
1169 *((__u32 *)val) = *reply;
1171 ptlrpc_req_finished(req);
1177 int mdc_obj_create(struct obd_export *exp, struct obdo *oa,
1178 struct lov_stripe_md **ea, struct obd_trans_info *oti)
1180 struct ptlrpc_request *request;
1181 struct ost_body *body;
1182 int rc, size = sizeof(*body);
1187 request = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_OBD_VERSION,
1188 OST_CREATE, 1, &size, NULL);
1190 GOTO(out_req, rc = -ENOMEM);
1192 body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof (*body));
1193 memcpy(&body->oa, oa, sizeof(body->oa));
1195 request->rq_replen = lustre_msg_size(1, &size);
1196 rc = ptlrpc_queue_wait(request);
1200 body = lustre_swab_repbuf(request, 0, sizeof(*body),
1201 lustre_swab_ost_body);
1203 CERROR ("can't unpack ost_body\n");
1204 GOTO (out_req, rc = -EPROTO);
1207 memcpy(oa, &body->oa, sizeof(*oa));
1209 /* store ino/generation for recovery */
1210 body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof (*body));
1211 body->oa.o_id = oa->o_id;
1212 body->oa.o_generation = oa->o_generation;
1214 CDEBUG(D_HA, "transno: "LPD64"\n", request->rq_repmsg->transno);
1217 ptlrpc_req_finished(request);
1221 int mdc_brw(int rw, struct obd_export *exp, struct obdo *oa,
1222 struct lov_stripe_md *ea, obd_count oa_bufs,
1223 struct brw_page *pgarr, struct obd_trans_info *oti)
1225 struct ptlrpc_bulk_desc *desc;
1226 struct niobuf_remote *niobuf;
1227 struct ptlrpc_request *req;
1228 struct obd_ioobj *ioobj;
1229 struct ost_body *body;
1233 opc = ((rw & OBD_BRW_WRITE) != 0) ? OST_WRITE : OST_READ;
1235 size[0] = sizeof(*body);
1236 size[1] = sizeof(*ioobj);
1237 size[2] = oa_bufs * sizeof(*niobuf);
1239 req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_OBD_VERSION, opc,
1241 LASSERT(req != NULL);
1243 if (opc == OST_WRITE)
1244 desc = ptlrpc_prep_bulk_imp(req, oa_bufs, BULK_GET_SOURCE,
1247 desc = ptlrpc_prep_bulk_imp(req, oa_bufs, BULK_PUT_SINK,
1249 LASSERT(desc != NULL);
1251 body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof(*body));
1252 ioobj = lustre_msg_buf(req->rq_reqmsg, 1, sizeof(*ioobj));
1253 niobuf = lustre_msg_buf(req->rq_reqmsg, 2, oa_bufs * sizeof(*niobuf));
1255 memcpy(&body->oa, oa, sizeof(*oa));
1256 obdo_to_ioobj(oa, ioobj);
1257 ioobj->ioo_bufcnt = oa_bufs;
1259 for (i = 0; i < oa_bufs; i++, niobuf++) {
1260 struct brw_page *pg = &pgarr[i];
1262 LASSERT(pg->count > 0);
1263 LASSERT((pg->off & ~PAGE_MASK) + pg->count <= PAGE_SIZE);
1265 ptlrpc_prep_bulk_page(desc, pg->pg, pg->off & ~PAGE_MASK,
1268 niobuf->offset = pg->off;
1269 niobuf->len = pg->count;
1270 niobuf->flags = pg->flag;
1273 /* size[0] still sizeof (*body) */
1274 if (opc == OST_WRITE) {
1275 /* 1 RC per niobuf */
1276 size[1] = sizeof(__u32) * oa_bufs;
1277 req->rq_replen = lustre_msg_size(2, size);
1279 /* 1 RC for the whole I/O */
1280 req->rq_replen = lustre_msg_size(1, size);
1282 err = ptlrpc_queue_wait(req);
1285 ptlrpc_req_finished(req);
1289 static int mdc_valid_attrs(struct obd_export *exp, struct ll_fid *fid)
1291 struct ldlm_res_id res_id = { .name = {0} };
1292 struct obd_device *obd = exp->exp_obd;
1293 struct lustre_handle lockh;
1294 ldlm_policy_data_t policy;
1298 res_id.name[0] = fid->id;
1299 res_id.name[1] = fid->generation;
1300 policy.l_inodebits.bits = MDS_INODELOCK_UPDATE;
1302 CDEBUG(D_INFO, "trying to match res "LPU64"\n", res_id.name[0]);
1304 /* FIXME use LDLM_FL_TEST_LOCK instead */
1305 flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_CBPENDING;
1306 if (ldlm_lock_match(obd->obd_namespace, flags, &res_id,
1307 LDLM_IBITS, &policy, LCK_PR, &lockh)) {
1308 ldlm_lock_decref(&lockh, LCK_PR);
1312 if (ldlm_lock_match(obd->obd_namespace, flags, &res_id,
1313 LDLM_IBITS, &policy, LCK_PW, &lockh)) {
1314 ldlm_lock_decref(&lockh, LCK_PW);
1319 static int mdc_change_cbdata_name(struct obd_export *exp, struct ll_fid *pfid,
1320 char *name, int len, struct ll_fid *cfid,
1321 ldlm_iterator_t it, void *data)
1324 rc = mdc_change_cbdata(exp, cfid, it, data);
1328 struct obd_ops mdc_obd_ops = {
1329 .o_owner = THIS_MODULE,
1330 .o_attach = mdc_attach,
1331 .o_detach = mdc_detach,
1332 .o_setup = mdc_setup,
1333 .o_precleanup = mdc_precleanup,
1334 .o_cleanup = mdc_cleanup,
1335 .o_connect = client_connect_import,
1336 .o_disconnect = client_disconnect_export,
1337 .o_iocontrol = mdc_iocontrol,
1338 .o_statfs = mdc_statfs,
1340 .o_unpin = mdc_unpin,
1341 .o_import_event = mdc_import_event,
1342 .o_llog_init = mdc_llog_init,
1343 .o_llog_finish = mdc_llog_finish,
1344 .o_create = mdc_obj_create,
1345 .o_set_info = mdc_set_info,
1346 .o_get_info = mdc_get_info,
1348 .o_init_ea_size = mdc_init_ea_size,
1351 struct md_ops mdc_md_ops = {
1352 .m_getstatus = mdc_getstatus,
1353 .m_getattr = mdc_getattr,
1354 .m_change_cbdata = mdc_change_cbdata,
1355 .m_close = mdc_close,
1356 .m_create = mdc_create,
1357 .m_done_writing = mdc_done_writing,
1358 .m_enqueue = mdc_enqueue,
1359 .m_getattr_name = mdc_getattr_name,
1360 .m_intent_lock = mdc_intent_lock,
1362 .m_rename = mdc_rename,
1363 .m_setattr = mdc_setattr,
1365 .m_readpage = mdc_readpage,
1366 .m_unlink = mdc_unlink,
1367 .m_valid_attrs = mdc_valid_attrs,
1368 .m_req2lustre_md = mdc_req2lustre_md,
1369 .m_set_open_replay_data = mdc_set_open_replay_data,
1370 .m_clear_open_replay_data = mdc_clear_open_replay_data,
1371 .m_store_inode_generation = mdc_store_inode_generation,
1372 .m_set_lock_data = mdc_set_lock_data,
1373 .m_get_real_obd = mdc_get_real_obd,
1374 .m_change_cbdata_name = mdc_change_cbdata_name,
1375 .m_change_cbdata = mdc_change_cbdata,
1378 int __init mdc_init(void)
1380 struct lprocfs_static_vars lvars;
1381 lprocfs_init_vars(mdc, &lvars);
1382 return class_register_type(&mdc_obd_ops, &mdc_md_ops, lvars.module_vars,
1387 static void /*__exit*/ mdc_exit(void)
1389 class_unregister_type(LUSTRE_MDC_NAME);
1392 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
1393 MODULE_DESCRIPTION("Lustre Metadata Client");
1394 MODULE_LICENSE("GPL");
1396 EXPORT_SYMBOL(mdc_req2lustre_md);
1397 EXPORT_SYMBOL(mdc_change_cbdata);
1398 EXPORT_SYMBOL(mdc_getstatus);
1399 EXPORT_SYMBOL(mdc_getattr);
1400 EXPORT_SYMBOL(mdc_getattr_name);
1401 EXPORT_SYMBOL(mdc_create);
1402 EXPORT_SYMBOL(mdc_unlink);
1403 EXPORT_SYMBOL(mdc_rename);
1404 EXPORT_SYMBOL(mdc_link);
1405 EXPORT_SYMBOL(mdc_readpage);
1406 EXPORT_SYMBOL(mdc_setattr);
1407 EXPORT_SYMBOL(mdc_close);
1408 EXPORT_SYMBOL(mdc_done_writing);
1409 EXPORT_SYMBOL(mdc_sync);
1410 EXPORT_SYMBOL(mdc_set_open_replay_data);
1411 EXPORT_SYMBOL(mdc_clear_open_replay_data);
1412 EXPORT_SYMBOL(mdc_store_inode_generation);
1414 module_init(mdc_init);
1415 module_exit(mdc_exit);