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->ngroups)
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_LOCK, 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);
632 int mdc_done_writing(struct obd_export *exp, struct obdo *obdo)
634 struct ptlrpc_request *req;
635 struct mds_body *body;
636 int rc, size[2] = {0, sizeof(*body)};
639 size[0] = mdc_get_secdesc_size();
641 req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
642 MDS_DONE_WRITING, 2, size, NULL);
646 mdc_pack_secdesc(req, size[0]);
648 body = lustre_msg_buf(req->rq_reqmsg, MDS_REQ_REC_OFF, sizeof(*body));
649 mdc_pack_fid(&body->fid1, obdo->o_id, 0, obdo->o_mode);
650 body->size = obdo->o_size;
651 body->blocks = obdo->o_blocks;
652 body->flags = obdo->o_flags;
653 body->valid = obdo->o_valid;
654 // memcpy(&body->handle, &och->och_fh, sizeof(body->handle));
656 req->rq_replen = lustre_msg_size(1, &size[1]);
658 rc = ptlrpc_queue_wait(req);
659 ptlrpc_req_finished(req);
663 int mdc_readpage(struct obd_export *exp, struct ll_fid *mdc_fid,
664 __u64 offset, struct page *page,
665 struct ptlrpc_request **request)
667 struct obd_import *imp = class_exp2cliimp(exp);
668 struct ptlrpc_request *req = NULL;
669 struct ptlrpc_bulk_desc *desc = NULL;
670 struct mds_body *body;
671 int rc, size[2] = {0, sizeof(*body)};
674 CDEBUG(D_INODE, "inode: %ld\n", (long)mdc_fid->id);
676 size[0] = mdc_get_secdesc_size();
678 req = ptlrpc_prep_req(imp, LUSTRE_MDS_VERSION, MDS_READPAGE,
681 GOTO(out, rc = -ENOMEM);
682 /* XXX FIXME bug 249 */
683 req->rq_request_portal = MDS_READPAGE_PORTAL;
685 mdc_pack_secdesc(req, size[0]);
687 desc = ptlrpc_prep_bulk_imp(req, 1, BULK_PUT_SINK, MDS_BULK_PORTAL);
689 GOTO(out, rc = -ENOMEM);
690 /* NB req now owns desc and will free it when it gets freed */
692 ptlrpc_prep_bulk_page(desc, page, 0, PAGE_CACHE_SIZE);
694 mdc_readdir_pack(req, 1, offset, PAGE_CACHE_SIZE, mdc_fid);
696 req->rq_replen = lustre_msg_size(1, &size[1]);
697 rc = ptlrpc_queue_wait(req);
700 body = lustre_swab_repbuf(req, 0, sizeof (*body),
701 lustre_swab_mds_body);
703 CERROR("Can't unpack mds_body\n");
704 GOTO(out, rc = -EPROTO);
707 if (req->rq_bulk->bd_nob_transferred != PAGE_CACHE_SIZE) {
708 CERROR ("Unexpected # bytes transferred: %d"
710 req->rq_bulk->bd_nob_transferred,
712 GOTO (out, rc = -EPROTO);
722 static int mdc_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
723 void *karg, void *uarg)
725 struct obd_device *obd = exp->exp_obd;
726 struct obd_ioctl_data *data = karg;
727 struct obd_import *imp = obd->u.cli.cl_import;
728 struct llog_ctxt *ctxt;
732 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
735 if (!try_module_get(THIS_MODULE)) {
736 CERROR("Can't get module. Is it alive?");
741 case OBD_IOC_CLIENT_RECOVER:
742 rc = ptlrpc_recover_import(imp, data->ioc_inlbuf1);
746 case IOC_OSC_SET_ACTIVE:
747 rc = ptlrpc_set_import_active(imp, data->ioc_offset);
749 case OBD_IOC_PARSE: {
750 ctxt = llog_get_context(&exp->exp_obd->obd_llogs,
751 LLOG_CONFIG_REPL_CTXT);
752 rc = class_config_process_llog(ctxt, data->ioc_inlbuf1, NULL);
756 case OBD_IOC_LLOG_INFO:
757 case OBD_IOC_LLOG_PRINT: {
758 ctxt = llog_get_context(&obd->obd_llogs, LLOG_CONFIG_REPL_CTXT);
759 rc = llog_ioctl(ctxt, cmd, data);
765 CERROR("mdc_ioctl(): unrecognised ioctl %#x\n", cmd);
766 GOTO(out, rc = -ENOTTY);
769 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
772 module_put(THIS_MODULE);
778 int mdc_set_info(struct obd_export *exp, obd_count keylen,
779 void *key, obd_count vallen, void *val)
783 if (keylen == strlen("initial_recov") &&
784 memcmp(key, "initial_recov", strlen("initial_recov")) == 0) {
785 struct obd_import *imp = exp->exp_obd->u.cli.cl_import;
786 if (vallen != sizeof(int))
788 imp->imp_initial_recov = *(int *)val;
789 CDEBUG(D_HA, "%s: set imp_no_init_recov = %d\n",
790 exp->exp_obd->obd_name,
791 imp->imp_initial_recov);
793 } else if (keylen >= strlen("mds_num") && strcmp(key, "mds_num") == 0) {
794 struct ptlrpc_request *req;
795 int rc, size[2] = {keylen, vallen};
796 char *bufs[2] = {key, val};
798 req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_OBD_VERSION,
799 OST_SET_INFO, 2, size, bufs);
803 req->rq_replen = lustre_msg_size(0, NULL);
804 rc = ptlrpc_queue_wait(req);
805 ptlrpc_req_finished(req);
807 } else if (keylen >= strlen("inter_mds") && strcmp(key, "inter_mds") == 0) {
808 struct obd_import *imp = class_exp2cliimp(exp);
809 imp->imp_server_timeout = 1;
810 CDEBUG(D_OTHER, "%s: timeout / 2\n", exp->exp_obd->obd_name);
816 static int mdc_statfs(struct obd_device *obd, struct obd_statfs *osfs,
817 unsigned long max_age)
819 struct ptlrpc_request *req;
820 struct obd_statfs *msfs;
821 int rc, size = sizeof(*msfs);
824 /* We could possibly pass max_age in the request (as an absolute
825 * timestamp or a "seconds.usec ago") so the target can avoid doing
826 * extra calls into the filesystem if that isn't necessary (e.g.
827 * during mount that would help a bit). Having relative timestamps
828 * is not so great if request processing is slow, while absolute
829 * timestamps are not ideal because they need time synchronization. */
830 req = ptlrpc_prep_req(obd->u.cli.cl_import, LUSTRE_MDS_VERSION,
831 MDS_STATFS, 0, NULL, NULL);
835 req->rq_replen = lustre_msg_size(1, &size);
837 mdc_get_rpc_lock(obd->u.cli.cl_rpc_lock, NULL);
838 rc = ptlrpc_queue_wait(req);
839 mdc_put_rpc_lock(obd->u.cli.cl_rpc_lock, NULL);
844 msfs = lustre_swab_repbuf(req, 0, sizeof(*msfs),lustre_swab_obd_statfs);
846 CERROR("Can't unpack obd_statfs\n");
847 GOTO(out, rc = -EPROTO);
850 memcpy(osfs, msfs, sizeof (*msfs));
853 ptlrpc_req_finished(req);
858 static int mdc_pin(struct obd_export *exp, obd_id ino, __u32 gen, int type,
859 struct obd_client_handle *handle, int flag)
861 struct ptlrpc_request *req;
862 struct mds_body *body;
863 int rc, size[2] = {0, sizeof(*body)};
866 //size[0] = mdc_get_secdesc_size();
868 req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
869 MDS_PIN, 2, size, NULL);
873 //mdc_pack_secdesc(req, size[0]);
875 body = lustre_msg_buf(req->rq_reqmsg, MDS_REQ_REC_OFF, sizeof(*body));
876 mdc_pack_fid(&body->fid1, ino, gen, type);
879 req->rq_replen = lustre_msg_size(1, &size[1]);
881 mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
882 rc = ptlrpc_queue_wait(req);
883 mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
885 CERROR("pin failed: %d\n", rc);
886 ptlrpc_req_finished(req);
890 body = lustre_swab_repbuf(req, 0, sizeof(*body), lustre_swab_mds_body);
892 ptlrpc_req_finished(req);
896 memcpy(&handle->och_fh, &body->handle, sizeof(body->handle));
897 handle->och_magic = OBD_CLIENT_HANDLE_MAGIC;
899 OBD_ALLOC(handle->och_mod, sizeof(*handle->och_mod));
900 if (handle->och_mod == NULL) {
901 DEBUG_REQ(D_ERROR, req, "can't allocate mdc_open_data");
904 handle->och_mod->mod_open_req = req; /* will be dropped by unpin */
909 static int mdc_unpin(struct obd_export *exp,
910 struct obd_client_handle *handle, int flag)
912 struct ptlrpc_request *req;
913 struct mds_body *body;
914 int rc, size[2] = {0, sizeof(*body)};
917 if (handle->och_magic != OBD_CLIENT_HANDLE_MAGIC)
920 //size[0] = mdc_get_secdesc_size();
922 req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
923 MDS_CLOSE, 2, size, NULL);
927 //mdc_pack_secdesc(req, size[0]);
929 body = lustre_msg_buf(req->rq_reqmsg, MDS_REQ_REC_OFF, sizeof(*body));
930 memcpy(&body->handle, &handle->och_fh, sizeof(body->handle));
933 req->rq_replen = lustre_msg_size(0, NULL);
934 mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
935 rc = ptlrpc_queue_wait(req);
936 mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
939 CERROR("unpin failed: %d\n", rc);
941 ptlrpc_req_finished(req);
942 ptlrpc_req_finished(handle->och_mod->mod_open_req);
943 OBD_FREE(handle->och_mod, sizeof(*handle->och_mod));
947 int mdc_sync(struct obd_export *exp, struct ll_fid *fid,
948 struct ptlrpc_request **request)
950 struct ptlrpc_request *req;
951 struct mds_body *body;
952 int size[2] = {0, sizeof(*body)};
956 //size[0] = mdc_get_secdesc_size();
958 req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
959 MDS_SYNC, 2, size, NULL);
961 RETURN(rc = -ENOMEM);
963 //mdc_pack_secdesc(req, size[0]);
966 body = lustre_msg_buf(req->rq_reqmsg, MDS_REQ_REC_OFF,
968 memcpy(&body->fid1, fid, sizeof(*fid));
971 req->rq_replen = lustre_msg_size(1, &size[1]);
973 rc = ptlrpc_queue_wait(req);
974 if (rc || request == NULL)
975 ptlrpc_req_finished(req);
982 static int mdc_import_event(struct obd_device *obd, struct obd_import *imp,
983 enum obd_import_event event)
987 LASSERT(imp->imp_obd == obd);
990 case IMP_EVENT_DISCON: {
993 case IMP_EVENT_INACTIVE: {
994 if (obd->obd_observer)
995 rc = obd_notify(obd->obd_observer, obd, 0, 0);
998 case IMP_EVENT_INVALIDATE: {
999 struct ldlm_namespace *ns = obd->obd_namespace;
1001 ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
1005 case IMP_EVENT_ACTIVE: {
1006 if (obd->obd_observer)
1007 rc = obd_notify(obd->obd_observer, obd, 1, 0);
1011 CERROR("Unknown import event %d\n", event);
1017 static int mdc_attach(struct obd_device *dev, obd_count len, void *data)
1019 struct lprocfs_static_vars lvars;
1021 lprocfs_init_vars(mdc, &lvars);
1022 return lprocfs_obd_attach(dev, lvars.obd_vars);
1025 static int mdc_detach(struct obd_device *dev)
1027 return lprocfs_obd_detach(dev);
1030 static int mdc_setup(struct obd_device *obd, obd_count len, void *buf)
1032 struct client_obd *cli = &obd->u.cli;
1036 OBD_ALLOC(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
1037 if (!cli->cl_rpc_lock)
1039 mdc_init_rpc_lock(cli->cl_rpc_lock);
1043 OBD_ALLOC(cli->cl_setattr_lock, sizeof (*cli->cl_setattr_lock));
1044 if (!cli->cl_setattr_lock)
1045 GOTO(err_rpc_lock, rc = -ENOMEM);
1046 mdc_init_rpc_lock(cli->cl_setattr_lock);
1048 rc = client_obd_setup(obd, len, buf);
1050 GOTO(err_setattr_lock, rc);
1052 rc = obd_llog_init(obd, &obd->obd_llogs, obd, 0, NULL);
1054 mdc_cleanup(obd, 0);
1055 CERROR("failed to setup llogging subsystems\n");
1061 OBD_FREE(cli->cl_setattr_lock, sizeof (*cli->cl_setattr_lock));
1063 OBD_FREE(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
1068 static int mdc_init_ea_size(struct obd_export *exp, int easize, int cookiesize)
1070 struct obd_device *obd = exp->exp_obd;
1071 struct client_obd *cli = &obd->u.cli;
1074 if (cli->cl_max_mds_easize < easize)
1075 cli->cl_max_mds_easize = easize;
1076 if (cli->cl_max_mds_cookiesize < cookiesize)
1077 cli->cl_max_mds_cookiesize = cookiesize;
1081 static int mdc_precleanup(struct obd_device *obd, int flags)
1085 rc = obd_llog_finish(obd, &obd->obd_llogs, 0);
1087 CERROR("failed to cleanup llogging subsystems\n");
1092 static int mdc_cleanup(struct obd_device *obd, int flags)
1094 struct client_obd *cli = &obd->u.cli;
1096 OBD_FREE(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
1097 OBD_FREE(cli->cl_setattr_lock, sizeof (*cli->cl_setattr_lock));
1101 return client_obd_cleanup(obd, flags);
1105 static int mdc_llog_init(struct obd_device *obd, struct obd_llogs *llogs,
1106 struct obd_device *tgt, int count,
1107 struct llog_catid *logid)
1109 struct llog_ctxt *ctxt;
1113 rc = obd_llog_setup(obd, llogs, LLOG_CONFIG_REPL_CTXT, tgt, 0, NULL,
1116 ctxt = llog_get_context(llogs, LLOG_CONFIG_REPL_CTXT);
1117 ctxt->loc_imp = obd->u.cli.cl_import;
1123 static int mdc_llog_finish(struct obd_device *obd,
1124 struct obd_llogs *llogs, int count)
1129 rc = obd_llog_cleanup(llog_get_context(llogs, LLOG_CONFIG_REPL_CTXT));
1132 static struct obd_device *mdc_get_real_obd(struct obd_export *exp,
1133 char *name, int len)
1136 RETURN(exp->exp_obd);
1139 static int mdc_get_info(struct obd_export *exp, obd_count keylen,
1140 void *key, __u32 *vallen, void *val)
1143 if (!vallen || !val)
1146 if (keylen >= strlen("mdsize") && strcmp(key, "mdsize") == 0) {
1147 struct ptlrpc_request *req;
1149 char *bufs[1] = {key};
1151 req = ptlrpc_prep_req(class_exp2cliimp(exp),
1152 LUSTRE_OBD_VERSION, OST_GET_INFO,
1157 req->rq_replen = lustre_msg_size(1, vallen);
1158 rc = ptlrpc_queue_wait(req);
1162 /* FIXME: right swap routine here! */
1163 reply = lustre_swab_repbuf(req, 0, sizeof(*reply), NULL);
1164 if (reply == NULL) {
1165 CERROR("Can't unpack mdsize\n");
1166 GOTO(out, rc = -EPROTO);
1168 *((__u32 *)val) = *reply;
1170 ptlrpc_req_finished(req);
1176 int mdc_obj_create(struct obd_export *exp, struct obdo *oa,
1177 struct lov_stripe_md **ea, struct obd_trans_info *oti)
1179 struct ptlrpc_request *request;
1180 struct ost_body *body;
1181 int rc, size = sizeof(*body);
1186 request = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_OBD_VERSION,
1187 OST_CREATE, 1, &size, NULL);
1189 GOTO(out_req, rc = -ENOMEM);
1191 body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof (*body));
1192 memcpy(&body->oa, oa, sizeof(body->oa));
1194 request->rq_replen = lustre_msg_size(1, &size);
1195 rc = ptlrpc_queue_wait(request);
1199 body = lustre_swab_repbuf(request, 0, sizeof(*body),
1200 lustre_swab_ost_body);
1202 CERROR ("can't unpack ost_body\n");
1203 GOTO (out_req, rc = -EPROTO);
1206 memcpy(oa, &body->oa, sizeof(*oa));
1208 /* store ino/generation for recovery */
1209 body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof (*body));
1210 body->oa.o_id = oa->o_id;
1211 body->oa.o_generation = oa->o_generation;
1213 CDEBUG(D_HA, "transno: "LPD64"\n", request->rq_repmsg->transno);
1216 ptlrpc_req_finished(request);
1220 int mdc_brw(int rw, struct obd_export *exp, struct obdo *oa,
1221 struct lov_stripe_md *ea, obd_count oa_bufs,
1222 struct brw_page *pgarr, struct obd_trans_info *oti)
1224 struct ptlrpc_bulk_desc *desc;
1225 struct niobuf_remote *niobuf;
1226 struct ptlrpc_request *req;
1227 struct obd_ioobj *ioobj;
1228 struct ost_body *body;
1232 opc = ((rw & OBD_BRW_WRITE) != 0) ? OST_WRITE : OST_READ;
1234 size[0] = sizeof(*body);
1235 size[1] = sizeof(*ioobj);
1236 size[2] = oa_bufs * sizeof(*niobuf);
1238 req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_OBD_VERSION, opc,
1240 LASSERT(req != NULL);
1242 if (opc == OST_WRITE)
1243 desc = ptlrpc_prep_bulk_imp(req, oa_bufs, BULK_GET_SOURCE,
1246 desc = ptlrpc_prep_bulk_imp(req, oa_bufs, BULK_PUT_SINK,
1248 LASSERT(desc != NULL);
1250 body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof(*body));
1251 ioobj = lustre_msg_buf(req->rq_reqmsg, 1, sizeof(*ioobj));
1252 niobuf = lustre_msg_buf(req->rq_reqmsg, 2, oa_bufs * sizeof(*niobuf));
1254 memcpy(&body->oa, oa, sizeof(*oa));
1255 obdo_to_ioobj(oa, ioobj);
1256 ioobj->ioo_bufcnt = oa_bufs;
1258 for (i = 0; i < oa_bufs; i++, niobuf++) {
1259 struct brw_page *pg = &pgarr[i];
1261 LASSERT(pg->count > 0);
1262 LASSERT((pg->disk_offset & ~PAGE_MASK) + pg->count <= PAGE_SIZE);
1264 ptlrpc_prep_bulk_page(desc, pg->pg, pg->disk_offset & ~PAGE_MASK,
1267 niobuf->offset = pg->disk_offset;
1268 niobuf->len = pg->count;
1269 niobuf->flags = pg->flag;
1272 /* size[0] still sizeof (*body) */
1273 if (opc == OST_WRITE) {
1274 /* 1 RC per niobuf */
1275 size[1] = sizeof(__u32) * oa_bufs;
1276 req->rq_replen = lustre_msg_size(2, size);
1278 /* 1 RC for the whole I/O */
1279 req->rq_replen = lustre_msg_size(1, size);
1281 err = ptlrpc_queue_wait(req);
1284 ptlrpc_req_finished(req);
1288 static int mdc_valid_attrs(struct obd_export *exp, struct ll_fid *fid)
1290 struct ldlm_res_id res_id = { .name = {0} };
1291 struct obd_device *obd = exp->exp_obd;
1292 struct lustre_handle lockh;
1293 ldlm_policy_data_t policy;
1297 res_id.name[0] = fid->id;
1298 res_id.name[1] = fid->generation;
1299 policy.l_inodebits.bits = MDS_INODELOCK_UPDATE;
1301 CDEBUG(D_INFO, "trying to match res "LPU64"\n", res_id.name[0]);
1303 /* FIXME use LDLM_FL_TEST_LOCK instead */
1304 flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_CBPENDING;
1305 if (ldlm_lock_match(obd->obd_namespace, flags, &res_id,
1306 LDLM_IBITS, &policy, LCK_PR, &lockh)) {
1307 ldlm_lock_decref(&lockh, LCK_PR);
1311 if (ldlm_lock_match(obd->obd_namespace, flags, &res_id,
1312 LDLM_IBITS, &policy, LCK_PW, &lockh)) {
1313 ldlm_lock_decref(&lockh, LCK_PW);
1318 static int mdc_change_cbdata_name(struct obd_export *exp, struct ll_fid *pfid,
1319 char *name, int len, struct ll_fid *cfid,
1320 ldlm_iterator_t it, void *data)
1323 rc = mdc_change_cbdata(exp, cfid, it, data);
1327 struct obd_ops mdc_obd_ops = {
1328 .o_owner = THIS_MODULE,
1329 .o_attach = mdc_attach,
1330 .o_detach = mdc_detach,
1331 .o_setup = mdc_setup,
1332 .o_precleanup = mdc_precleanup,
1333 .o_cleanup = mdc_cleanup,
1334 .o_add_conn = client_import_add_conn,
1335 .o_del_conn = client_import_del_conn,
1336 .o_connect = client_connect_import,
1337 .o_disconnect = client_disconnect_export,
1338 .o_iocontrol = mdc_iocontrol,
1339 .o_statfs = mdc_statfs,
1341 .o_unpin = mdc_unpin,
1342 .o_import_event = mdc_import_event,
1343 .o_llog_init = mdc_llog_init,
1344 .o_llog_finish = mdc_llog_finish,
1345 .o_create = mdc_obj_create,
1346 .o_set_info = mdc_set_info,
1347 .o_get_info = mdc_get_info,
1349 .o_init_ea_size = mdc_init_ea_size,
1352 struct md_ops mdc_md_ops = {
1353 .m_getstatus = mdc_getstatus,
1354 .m_getattr = mdc_getattr,
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);