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 extern int mds_queue_req(struct ptlrpc_request *);
47 /* Helper that implements most of mdc_getstatus and signal_completed_replay. */
48 /* XXX this should become mdc_get_info("key"), sending MDS_GET_INFO RPC */
49 static int send_getstatus(struct obd_import *imp, struct ll_fid *rootfid,
50 int level, int msg_flags)
52 struct ptlrpc_request *req;
53 struct mds_body *body;
54 int rc, size = sizeof(*body);
57 req = ptlrpc_prep_req(imp, MDS_GETSTATUS, 1, &size, NULL);
59 GOTO(out, rc = -ENOMEM);
61 body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*body));
62 req->rq_send_state = level;
63 req->rq_replen = lustre_msg_size(1, &size);
65 mdc_pack_req_body(req);
66 req->rq_reqmsg->flags |= msg_flags;
67 rc = ptlrpc_queue_wait(req);
70 body = lustre_swab_repbuf (req, 0, sizeof (*body),
71 lustre_swab_mds_body);
73 CERROR ("Can't extract mds_body\n");
74 GOTO (out, rc = -EPROTO);
77 memcpy(rootfid, &body->fid1, sizeof(*rootfid));
79 CDEBUG(D_NET, "root ino="LPU64", last_committed="LPU64
80 ", last_xid="LPU64"\n",
81 rootfid->id, req->rq_repmsg->last_committed,
82 req->rq_repmsg->last_xid);
87 ptlrpc_req_finished(req);
91 /* This should be mdc_get_info("rootfid") */
92 int mdc_getstatus(struct obd_export *exp, struct ll_fid *rootfid)
94 return send_getstatus(class_exp2cliimp(exp), rootfid, LUSTRE_IMP_FULL,
98 int mdc_getattr_common(struct obd_export *exp, unsigned int ea_size,
99 struct ptlrpc_request *req)
101 struct mds_body *body;
104 int size[2] = {sizeof(*body), 0};
108 /* request message already built */
111 size[bufcount++] = ea_size;
112 CDEBUG(D_INODE, "reserved %u bytes for MD/symlink in packet\n",
115 req->rq_replen = lustre_msg_size(bufcount, size);
117 mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
118 rc = ptlrpc_queue_wait(req);
119 mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
123 body = lustre_swab_repbuf (req, 0, sizeof (*body),
124 lustre_swab_mds_body);
126 CERROR ("Can't unpack mds_body\n");
130 CDEBUG(D_NET, "mode: %o\n", body->mode);
132 LASSERT_REPSWAB (req, 1);
133 if (body->eadatasize != 0) {
134 /* reply indicates presence of eadata; check it's there... */
135 eadata = lustre_msg_buf (req->rq_repmsg, 1, body->eadatasize);
136 if (eadata == NULL) {
137 CERROR ("Missing/short eadata\n");
145 int mdc_getattr(struct obd_export *exp, struct ll_fid *fid,
146 unsigned long valid, unsigned int ea_size,
147 struct ptlrpc_request **request)
149 struct ptlrpc_request *req;
150 struct mds_body *body;
151 int size = sizeof(*body);
155 /* XXX do we need to make another request here? We just did a getattr
156 * to do the lookup in the first place.
158 req = ptlrpc_prep_req(class_exp2cliimp(exp), MDS_GETATTR, 1, &size,
161 GOTO(out, rc = -ENOMEM);
163 body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*body));
164 memcpy(&body->fid1, fid, sizeof(*fid));
166 body->eadatasize = ea_size;
167 mdc_pack_req_body(req);
169 rc = mdc_getattr_common(exp, ea_size, req);
171 ptlrpc_req_finished (req);
179 int mdc_getattr_name(struct obd_export *exp, struct ll_fid *fid,
180 char *filename, int namelen, unsigned long valid,
181 unsigned int ea_size, struct ptlrpc_request **request)
183 struct ptlrpc_request *req;
184 struct mds_body *body;
185 int rc, size[2] = {sizeof(*body), namelen};
188 req = ptlrpc_prep_req(class_exp2cliimp(exp), MDS_GETATTR_NAME, 2,
191 GOTO(out, rc = -ENOMEM);
193 body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*body));
194 memcpy(&body->fid1, fid, sizeof(*fid));
196 body->eadatasize = ea_size;
197 mdc_pack_req_body(req);
199 if (filename != NULL) {
200 LASSERT (strnlen (filename, namelen) == namelen - 1);
201 memcpy(lustre_msg_buf(req->rq_reqmsg, 1, namelen),
204 LASSERT(namelen == 1);
207 rc = mdc_getattr_common(exp, ea_size, req);
209 ptlrpc_req_finished (req);
217 /* This should be called with both the request and the reply still packed. */
218 int mdc_store_inode_generation(struct obd_export *exp,
219 struct ptlrpc_request *req, int reqoff,
222 struct mds_rec_create *rec =
223 lustre_msg_buf(req->rq_reqmsg, reqoff, sizeof(*rec));
224 struct mds_body *body =
225 lustre_msg_buf(req->rq_repmsg, repoff, sizeof(*body));
227 LASSERT (rec != NULL);
228 LASSERT (body != NULL);
230 memcpy(&rec->cr_replayfid, &body->fid1, sizeof rec->cr_replayfid);
231 DEBUG_REQ(D_HA, req, "storing generation %u for ino "LPU64,
232 rec->cr_replayfid.generation, rec->cr_replayfid.id);
236 int mdc_req2lustre_md(struct obd_export *exp_mdc, struct ptlrpc_request *req,
237 unsigned int offset, struct obd_export *exp_osc,
238 struct lustre_md *md)
244 memset(md, 0, sizeof(*md));
246 md->body = lustre_msg_buf(req->rq_repmsg, offset, sizeof (*md->body));
247 LASSERT (md->body != NULL);
248 LASSERT_REPSWABBED (req, offset);
250 if (!(md->body->valid & OBD_MD_FLEASIZE) &&
251 !(md->body->valid & OBD_MD_FLDIREA))
254 /* ea is presented in reply, parse it */
255 if (S_ISREG(md->body->mode)) {
257 struct lov_mds_md *lmm;
259 if (md->body->eadatasize == 0) {
260 CERROR ("OBD_MD_FLEASIZE set, but eadatasize 0\n");
263 lmmsize = md->body->eadatasize;
264 lmm = lustre_msg_buf(req->rq_repmsg, offset + 1, lmmsize);
265 LASSERT (lmm != NULL);
266 LASSERT_REPSWABBED (req, offset + 1);
268 rc = obd_unpackmd(exp_osc, &md->lsm, lmm, lmmsize);
270 LASSERT (rc >= sizeof (*md->lsm));
273 } else if (S_ISDIR(md->body->mode)) {
276 LASSERT(exp_mdc != NULL);
277 /* dir can be non-splitted */
278 if (md->body->eadatasize == 0)
281 mdsize = md->body->eadatasize;
282 mea = lustre_msg_buf(req->rq_repmsg, offset + 1, mdsize);
283 LASSERT(mea != NULL);
285 rc = obd_unpackmd(exp_mdc, (void *) &md->mea,
286 (void *) mea, mdsize);
288 LASSERT (rc >= sizeof (*md->mea));
297 static void mdc_commit_open(struct ptlrpc_request *req)
299 struct mdc_open_data *mod = req->rq_cb_data;
303 if (mod->mod_close_req != NULL)
304 mod->mod_close_req->rq_cb_data = NULL;
306 if (mod->mod_och != NULL)
307 mod->mod_och->och_mod = NULL;
309 OBD_FREE(mod, sizeof(*mod));
310 req->rq_cb_data = NULL;
313 static void mdc_replay_open(struct ptlrpc_request *req)
315 struct mdc_open_data *mod = req->rq_cb_data;
316 struct obd_client_handle *och;
317 struct ptlrpc_request *close_req;
318 struct lustre_handle old;
319 struct mds_body *body;
322 body = lustre_swab_repbuf(req, 1, sizeof(*body), lustre_swab_mds_body);
323 LASSERT (body != NULL);
326 DEBUG_REQ(D_ERROR, req,
327 "can't properly replay without open data");
334 struct lustre_handle *file_fh;
335 LASSERT(och->och_magic == OBD_CLIENT_HANDLE_MAGIC);
336 file_fh = &och->och_fh;
337 CDEBUG(D_HA, "updating handle from "LPX64" to "LPX64"\n",
338 file_fh->cookie, body->handle.cookie);
339 memcpy(&old, file_fh, sizeof(old));
340 memcpy(file_fh, &body->handle, sizeof(*file_fh));
343 close_req = mod->mod_close_req;
344 if (close_req != NULL) {
345 struct mds_body *close_body;
346 LASSERT(close_req->rq_reqmsg->opc == MDS_CLOSE);
347 close_body = lustre_msg_buf(close_req->rq_reqmsg, 0,
348 sizeof(*close_body));
350 LASSERT(!memcmp(&old, &close_body->handle, sizeof old));
351 DEBUG_REQ(D_HA, close_req, "updating close body with new fh");
352 memcpy(&close_body->handle, &body->handle,
353 sizeof(close_body->handle));
359 int mdc_set_open_replay_data(struct obd_export *exp,
360 struct obd_client_handle *och,
361 struct ptlrpc_request *open_req)
363 struct mdc_open_data *mod;
364 struct mds_rec_create *rec =
365 lustre_msg_buf(open_req->rq_reqmsg, 2, sizeof(*rec));
366 struct mds_body *body =
367 lustre_msg_buf(open_req->rq_repmsg, 1, sizeof(*body));
369 LASSERT(rec != NULL);
370 /* outgoing messages always in my byte order */
371 LASSERT(body != NULL);
372 /* incoming message in my byte order (it's been swabbed) */
373 LASSERT_REPSWABBED(open_req, 1);
375 OBD_ALLOC(mod, sizeof(*mod));
377 DEBUG_REQ(D_ERROR, open_req, "can't allocate mdc_open_data");
383 mod->mod_open_req = open_req;
385 memcpy(&rec->cr_replayfid, &body->fid1, sizeof rec->cr_replayfid);
386 open_req->rq_replay_cb = mdc_replay_open;
387 open_req->rq_commit_cb = mdc_commit_open;
388 open_req->rq_cb_data = mod;
389 DEBUG_REQ(D_HA, open_req, "set up replay data");
393 int mdc_clear_open_replay_data(struct obd_export *exp,
394 struct obd_client_handle *och)
396 struct mdc_open_data *mod = och->och_mod;
398 /* Don't free the structure now (it happens in mdc_commit_open, after
399 * we're sure we won't need to fix up the close request in the future),
400 * but make sure that replay doesn't poke at the och, which is about to
402 LASSERT(mod != LP_POISON);
409 static void mdc_commit_close(struct ptlrpc_request *req)
411 struct mdc_open_data *mod = req->rq_cb_data;
412 struct ptlrpc_request *open_req;
413 struct obd_import *imp = req->rq_import;
415 DEBUG_REQ(D_HA, req, "close req committed");
419 mod->mod_close_req = NULL;
420 req->rq_cb_data = NULL;
421 req->rq_commit_cb = NULL;
423 open_req = mod->mod_open_req;
424 LASSERT(open_req != NULL);
425 LASSERT(open_req != LP_POISON);
426 LASSERT(open_req->rq_type != LI_POISON);
428 DEBUG_REQ(D_HA, open_req, "open req balanced");
429 if (open_req->rq_transno == 0) {
430 DEBUG_REQ(D_ERROR, open_req, "BUG 3892 open");
431 DEBUG_REQ(D_ERROR, req, "BUG 3892 close");
432 LASSERTF(open_req->rq_transno != 0, "BUG 3892");
434 LASSERT(open_req->rq_import == imp);
436 /* We no longer want to preserve this for transno-unconditional
438 spin_lock(&open_req->rq_lock);
439 open_req->rq_replay = 0;
440 spin_unlock(&open_req->rq_lock);
443 static int mdc_close_interpret(struct ptlrpc_request *req, void *data, int rc)
445 union ptlrpc_async_args *aa = data;
446 struct mdc_rpc_lock *rpc_lock;
447 struct obd_device *obd = aa->pointer_arg[1];
450 spin_lock_irqsave(&req->rq_lock, flags);
451 rpc_lock = aa->pointer_arg[0];
452 aa->pointer_arg[0] = NULL;
453 spin_unlock_irqrestore (&req->rq_lock, flags);
455 if (rpc_lock == NULL) {
456 CERROR("called with NULL rpc_lock\n");
458 mdc_put_rpc_lock(rpc_lock, NULL);
459 LASSERTF(rpc_lock == obd->u.cli.cl_rpc_lock, "%p != %p\n",
460 rpc_lock, obd->u.cli.cl_rpc_lock);
462 wake_up(&req->rq_reply_waitq);
466 /* We can't use ptlrpc_check_reply, because we don't want to wake up for
467 * anything but a reply or an error. */
468 static int mdc_close_check_reply(struct ptlrpc_request *req)
473 spin_lock_irqsave(&req->rq_lock, flags);
474 if (req->rq_async_args.pointer_arg[0] == NULL)
476 spin_unlock_irqrestore (&req->rq_lock, flags);
480 static int go_back_to_sleep(void *unused)
485 int mdc_close(struct obd_export *exp, struct obdo *oa,
486 struct obd_client_handle *och, struct ptlrpc_request **request)
488 struct obd_device *obd = class_exp2obd(exp);
489 int reqsize[2] = {sizeof(struct mds_body),
490 obd->u.cli.cl_max_mds_cookiesize};
491 int rc, repsize[3] = {sizeof(struct mds_body),
492 obd->u.cli.cl_max_mds_easize,
493 obd->u.cli.cl_max_mds_cookiesize};
494 struct ptlrpc_request *req;
495 struct mdc_open_data *mod;
496 struct l_wait_info lwi;
499 req = ptlrpc_prep_req(class_exp2cliimp(exp), MDS_CLOSE, 2, reqsize,
502 GOTO(out, rc = -ENOMEM);
504 /* Ensure that this close's handle is fixed up during replay. */
505 LASSERT(och != NULL);
507 if (likely(mod != NULL)) {
508 mod->mod_close_req = req;
509 LASSERT(mod->mod_open_req->rq_type != LI_POISON);
510 DEBUG_REQ(D_HA, mod->mod_open_req, "matched open req %p",
513 CDEBUG(D_HA, "couldn't find open req; expecting close error\n");
516 mdc_close_pack(req, 0, oa, oa->o_valid, och);
518 req->rq_replen = lustre_msg_size(3, repsize);
519 req->rq_commit_cb = mdc_commit_close;
520 LASSERT(req->rq_cb_data == NULL);
521 req->rq_cb_data = mod;
523 /* We hand a ref to the rpcd here, so we need another one of our own. */
524 ptlrpc_request_addref(req);
526 mdc_get_rpc_lock(obd->u.cli.cl_rpc_lock, NULL);
527 req->rq_interpret_reply = mdc_close_interpret;
528 req->rq_async_args.pointer_arg[0] = obd->u.cli.cl_rpc_lock;
529 req->rq_async_args.pointer_arg[1] = obd;
530 ptlrpcd_add_req(req);
531 lwi = LWI_TIMEOUT_INTR(MAX(req->rq_timeout * HZ, 1), go_back_to_sleep,
533 rc = l_wait_event(req->rq_reply_waitq, mdc_close_check_reply(req),
535 if (req->rq_repmsg == NULL) {
536 CDEBUG(D_HA, "request failed to send: %p, %d\n", req,
539 rc = req->rq_status ? req->rq_status : -EIO;
540 } else if (rc == 0) {
541 rc = req->rq_repmsg->status;
542 if (req->rq_repmsg->type == PTL_RPC_MSG_ERR) {
543 DEBUG_REQ(D_ERROR, req, "type == PTL_RPC_MSG_ERR, err "
547 } else if (mod == NULL) {
548 CERROR("Unexpected: can't find mdc_open_data, but the "
549 "close succeeded. Please tell CFS.\n");
551 if (!lustre_swab_repbuf(req, 0, sizeof(struct mds_body),
552 lustre_swab_mds_body)) {
553 CERROR("Error unpacking mds_body\n");
557 if (req->rq_async_args.pointer_arg[0] != NULL) {
558 CERROR("returned without dropping rpc_lock: rc %d\n", rc);
559 mdc_close_interpret(req, &req->rq_async_args, rc);
560 portals_debug_dumplog();
569 int mdc_done_writing(struct obd_export *exp, struct obdo *obdo)
571 struct ptlrpc_request *req;
572 struct mds_body *body;
573 int rc, size = sizeof(*body);
576 req = ptlrpc_prep_req(class_exp2cliimp(exp), MDS_DONE_WRITING, 1,
581 body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof(*body));
582 mdc_pack_fid(&body->fid1, obdo->o_id, 0, obdo->o_mode);
583 body->size = obdo->o_size;
584 body->blocks = obdo->o_blocks;
585 body->flags = obdo->o_flags;
586 body->valid = obdo->o_valid;
587 // memcpy(&body->handle, &och->och_fh, sizeof(body->handle));
589 req->rq_replen = lustre_msg_size(1, &size);
591 rc = ptlrpc_queue_wait(req);
592 ptlrpc_req_finished(req);
596 int mdc_readpage(struct obd_export *exp, struct ll_fid *mdc_fid,
597 __u64 offset, struct page *page,
598 struct ptlrpc_request **request)
600 struct obd_import *imp = class_exp2cliimp(exp);
601 struct ptlrpc_request *req = NULL;
602 struct ptlrpc_bulk_desc *desc = NULL;
603 struct mds_body *body;
604 int rc, size = sizeof(*body);
607 CDEBUG(D_INODE, "inode: %ld\n", (long)mdc_fid->id);
609 req = ptlrpc_prep_req(imp, MDS_READPAGE, 1, &size, NULL);
611 GOTO(out, rc = -ENOMEM);
612 /* XXX FIXME bug 249 */
613 req->rq_request_portal = MDS_READPAGE_PORTAL;
615 desc = ptlrpc_prep_bulk_imp(req, 1, BULK_PUT_SINK, MDS_BULK_PORTAL);
617 GOTO(out, rc = -ENOMEM);
618 /* NB req now owns desc and will free it when it gets freed */
620 ptlrpc_prep_bulk_page(desc, page, 0, PAGE_CACHE_SIZE);
622 mdc_readdir_pack(req, offset, PAGE_CACHE_SIZE, mdc_fid);
624 req->rq_replen = lustre_msg_size(1, &size);
625 rc = ptlrpc_queue_wait(req);
628 body = lustre_swab_repbuf(req, 0, sizeof (*body),
629 lustre_swab_mds_body);
631 CERROR("Can't unpack mds_body\n");
632 GOTO(out, rc = -EPROTO);
635 if (req->rq_bulk->bd_nob_transferred != PAGE_CACHE_SIZE) {
636 CERROR ("Unexpected # bytes transferred: %d"
638 req->rq_bulk->bd_nob_transferred,
640 GOTO (out, rc = -EPROTO);
650 static int mdc_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
651 void *karg, void *uarg)
653 struct obd_device *obd = exp->exp_obd;
654 struct obd_ioctl_data *data = karg;
655 struct obd_import *imp = obd->u.cli.cl_import;
656 struct llog_ctxt *ctxt;
660 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
663 if (!try_module_get(THIS_MODULE)) {
664 CERROR("Can't get module. Is it alive?");
669 case OBD_IOC_CLIENT_RECOVER:
670 rc = ptlrpc_recover_import(imp, data->ioc_inlbuf1);
674 case IOC_OSC_SET_ACTIVE:
675 rc = ptlrpc_set_import_active(imp, data->ioc_offset);
677 case OBD_IOC_PARSE: {
678 ctxt = llog_get_context(&exp->exp_obd->obd_llogs,
679 LLOG_CONFIG_REPL_CTXT);
680 rc = class_config_process_llog(ctxt, data->ioc_inlbuf1, NULL);
684 case OBD_IOC_LLOG_INFO:
685 case OBD_IOC_LLOG_PRINT: {
686 ctxt = llog_get_context(&obd->obd_llogs, LLOG_CONFIG_REPL_CTXT);
687 rc = llog_ioctl(ctxt, cmd, data);
693 CERROR("mdc_ioctl(): unrecognised ioctl %#x\n", cmd);
694 GOTO(out, rc = -ENOTTY);
697 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
700 module_put(THIS_MODULE);
706 int mdc_set_info(struct obd_export *exp, obd_count keylen,
707 void *key, obd_count vallen, void *val)
711 if (keylen == strlen("initial_recov") &&
712 memcmp(key, "initial_recov", strlen("initial_recov")) == 0) {
713 struct obd_import *imp = exp->exp_obd->u.cli.cl_import;
714 if (vallen != sizeof(int))
716 imp->imp_initial_recov = *(int *)val;
717 CDEBUG(D_HA, "%s: set imp_no_init_recov = %d\n",
718 exp->exp_obd->obd_name,
719 imp->imp_initial_recov);
721 } else if (keylen >= strlen("client") && strcmp(key, "client") == 0) {
722 struct ptlrpc_request *req;
723 char *bufs[1] = {key};
725 req = ptlrpc_prep_req(class_exp2cliimp(exp), OST_SET_INFO, 1,
730 req->rq_replen = lustre_msg_size(0, NULL);
731 rc = ptlrpc_queue_wait(req);
732 ptlrpc_req_finished(req);
734 } else if (keylen >= strlen("mds_num") && strcmp(key, "mds_num") == 0) {
735 struct ptlrpc_request *req;
736 int rc, size[2] = {keylen, vallen};
737 char *bufs[2] = {key, val};
739 req = ptlrpc_prep_req(class_exp2cliimp(exp), OST_SET_INFO, 2,
744 req->rq_replen = lustre_msg_size(0, NULL);
745 rc = ptlrpc_queue_wait(req);
746 ptlrpc_req_finished(req);
748 } else if (keylen >= strlen("inter_mds") && strcmp(key, "inter_mds") == 0) {
749 struct obd_import *imp = class_exp2cliimp(exp);
750 imp->imp_server_timeout = 1;
751 CDEBUG(D_OTHER, "%s: timeout / 2\n", exp->exp_obd->obd_name);
757 static int mdc_statfs(struct obd_device *obd, struct obd_statfs *osfs,
758 unsigned long max_age)
760 struct ptlrpc_request *req;
761 struct obd_statfs *msfs;
762 int rc, size = sizeof(*msfs);
765 /* We could possibly pass max_age in the request (as an absolute
766 * timestamp or a "seconds.usec ago") so the target can avoid doing
767 * extra calls into the filesystem if that isn't necessary (e.g.
768 * during mount that would help a bit). Having relative timestamps
769 * is not so great if request processing is slow, while absolute
770 * timestamps are not ideal because they need time synchronization. */
771 req = ptlrpc_prep_req(obd->u.cli.cl_import, MDS_STATFS, 0, NULL, NULL);
775 req->rq_replen = lustre_msg_size(1, &size);
777 mdc_get_rpc_lock(obd->u.cli.cl_rpc_lock, NULL);
778 rc = ptlrpc_queue_wait(req);
779 mdc_put_rpc_lock(obd->u.cli.cl_rpc_lock, NULL);
784 msfs = lustre_swab_repbuf(req, 0, sizeof(*msfs),lustre_swab_obd_statfs);
786 CERROR("Can't unpack obd_statfs\n");
787 GOTO(out, rc = -EPROTO);
790 memcpy(osfs, msfs, sizeof (*msfs));
793 ptlrpc_req_finished(req);
798 static int mdc_pin(struct obd_export *exp, obd_id ino, __u32 gen, int type,
799 struct obd_client_handle *handle, int flag)
801 struct ptlrpc_request *req;
802 struct mds_body *body;
803 int rc, size = sizeof(*body);
806 req = ptlrpc_prep_req(class_exp2cliimp(exp), MDS_PIN, 1, &size, NULL);
810 body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*body));
811 mdc_pack_fid(&body->fid1, ino, gen, type);
814 req->rq_replen = lustre_msg_size(1, &size);
816 mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
817 rc = ptlrpc_queue_wait(req);
818 mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
820 CERROR("pin failed: %d\n", rc);
821 ptlrpc_req_finished(req);
825 body = lustre_swab_repbuf(req, 0, sizeof(*body), lustre_swab_mds_body);
827 ptlrpc_req_finished(req);
831 memcpy(&handle->och_fh, &body->handle, sizeof(body->handle));
832 handle->och_magic = OBD_CLIENT_HANDLE_MAGIC;
834 OBD_ALLOC(handle->och_mod, sizeof(*handle->och_mod));
835 if (handle->och_mod == NULL) {
836 DEBUG_REQ(D_ERROR, req, "can't allocate mdc_open_data");
839 handle->och_mod->mod_open_req = req; /* will be dropped by unpin */
844 static int mdc_unpin(struct obd_export *exp,
845 struct obd_client_handle *handle, int flag)
847 struct ptlrpc_request *req;
848 struct mds_body *body;
849 int rc, size = sizeof(*body);
852 if (handle->och_magic != OBD_CLIENT_HANDLE_MAGIC)
855 req = ptlrpc_prep_req(class_exp2cliimp(exp), MDS_CLOSE, 1, &size, NULL);
859 body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof(*body));
860 memcpy(&body->handle, &handle->och_fh, sizeof(body->handle));
863 req->rq_replen = lustre_msg_size(0, NULL);
864 mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
865 rc = ptlrpc_queue_wait(req);
866 mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
869 CERROR("unpin failed: %d\n", rc);
871 ptlrpc_req_finished(req);
872 ptlrpc_req_finished(handle->och_mod->mod_open_req);
873 OBD_FREE(handle->och_mod, sizeof(*handle->och_mod));
877 int mdc_sync(struct obd_export *exp, struct ll_fid *fid,
878 struct ptlrpc_request **request)
880 struct ptlrpc_request *req;
881 struct mds_body *body;
882 int size = sizeof(*body);
886 req = ptlrpc_prep_req(class_exp2cliimp(exp), MDS_SYNC, 1,&size,NULL);
888 RETURN(rc = -ENOMEM);
891 body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*body));
892 memcpy(&body->fid1, fid, sizeof(*fid));
893 mdc_pack_req_body(req);
896 req->rq_replen = lustre_msg_size(1, &size);
898 rc = ptlrpc_queue_wait(req);
899 if (rc || request == NULL)
900 ptlrpc_req_finished(req);
907 static int mdc_import_event(struct obd_device *obd, struct obd_import *imp,
908 enum obd_import_event event)
912 LASSERT(imp->imp_obd == obd);
915 case IMP_EVENT_DISCON: {
918 case IMP_EVENT_INACTIVE: {
919 if (obd->obd_observer)
920 rc = obd_notify(obd->obd_observer, obd, 0);
923 case IMP_EVENT_INVALIDATE: {
924 struct ldlm_namespace *ns = obd->obd_namespace;
926 ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
930 case IMP_EVENT_ACTIVE: {
931 if (obd->obd_observer)
932 rc = obd_notify(obd->obd_observer, obd, 1);
936 CERROR("Unknown import event %d\n", event);
942 static int mdc_attach(struct obd_device *dev, obd_count len, void *data)
944 struct lprocfs_static_vars lvars;
946 lprocfs_init_vars(mdc, &lvars);
947 return lprocfs_obd_attach(dev, lvars.obd_vars);
950 static int mdc_detach(struct obd_device *dev)
952 return lprocfs_obd_detach(dev);
955 static int mdc_setup(struct obd_device *obd, obd_count len, void *buf)
957 struct client_obd *cli = &obd->u.cli;
961 OBD_ALLOC(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
962 if (!cli->cl_rpc_lock)
964 mdc_init_rpc_lock(cli->cl_rpc_lock);
968 OBD_ALLOC(cli->cl_setattr_lock, sizeof (*cli->cl_setattr_lock));
969 if (!cli->cl_setattr_lock)
970 GOTO(err_rpc_lock, rc = -ENOMEM);
971 mdc_init_rpc_lock(cli->cl_setattr_lock);
973 rc = client_obd_setup(obd, len, buf);
975 GOTO(err_setattr_lock, rc);
977 rc = obd_llog_init(obd, &obd->obd_llogs, obd, 0, NULL);
980 CERROR("failed to setup llogging subsystems\n");
986 OBD_FREE(cli->cl_setattr_lock, sizeof (*cli->cl_setattr_lock));
988 OBD_FREE(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
993 static int mdc_init_ea_size(struct obd_export *exp, int easize, int cookiesize)
995 struct obd_device *obd = exp->exp_obd;
996 struct client_obd *cli = &obd->u.cli;
999 if (cli->cl_max_mds_easize < easize)
1000 cli->cl_max_mds_easize = easize;
1001 if (cli->cl_max_mds_cookiesize < cookiesize)
1002 cli->cl_max_mds_cookiesize = cookiesize;
1006 static int mdc_precleanup(struct obd_device *obd, int flags)
1010 rc = obd_llog_finish(obd, &obd->obd_llogs, 0);
1012 CERROR("failed to cleanup llogging subsystems\n");
1017 static int mdc_cleanup(struct obd_device *obd, int flags)
1019 struct client_obd *cli = &obd->u.cli;
1021 OBD_FREE(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
1022 OBD_FREE(cli->cl_setattr_lock, sizeof (*cli->cl_setattr_lock));
1026 return client_obd_cleanup(obd, flags);
1030 static int mdc_llog_init(struct obd_device *obd, struct obd_llogs *llogs,
1031 struct obd_device *tgt, int count,
1032 struct llog_catid *logid)
1034 struct llog_ctxt *ctxt;
1038 rc = obd_llog_setup(obd, llogs, LLOG_CONFIG_REPL_CTXT, tgt, 0, NULL,
1041 ctxt = llog_get_context(llogs, LLOG_CONFIG_REPL_CTXT);
1042 ctxt->loc_imp = obd->u.cli.cl_import;
1048 static int mdc_llog_finish(struct obd_device *obd,
1049 struct obd_llogs *llogs, int count)
1054 rc = obd_llog_cleanup(llog_get_context(llogs, LLOG_CONFIG_REPL_CTXT));
1057 static struct obd_device *mdc_get_real_obd(struct obd_export *exp,
1058 char *name, int len)
1061 RETURN(exp->exp_obd);
1064 static int mdc_get_info(struct obd_export *exp, obd_count keylen,
1065 void *key, __u32 *vallen, void *val)
1068 if (!vallen || !val)
1071 if (keylen >= strlen("mdsize") && strcmp(key, "mdsize") == 0) {
1072 struct ptlrpc_request *req;
1074 char *bufs[1] = {key};
1076 req = ptlrpc_prep_req(class_exp2cliimp(exp), OST_GET_INFO, 1,
1081 req->rq_replen = lustre_msg_size(1, vallen);
1082 rc = ptlrpc_queue_wait(req);
1086 /* FIXME: right swap routine here! */
1087 reply = lustre_swab_repbuf(req, 0, sizeof(*reply), NULL);
1088 if (reply == NULL) {
1089 CERROR("Can't unpack mdsize\n");
1090 GOTO(out, rc = -EPROTO);
1092 *((__u32 *)val) = *reply;
1094 ptlrpc_req_finished(req);
1100 int mdc_obj_create(struct obd_export *exp, struct obdo *oa,
1101 struct lov_stripe_md **ea, struct obd_trans_info *oti)
1103 struct ptlrpc_request *request;
1104 struct ost_body *body;
1105 int rc, size = sizeof(*body);
1110 request = ptlrpc_prep_req(class_exp2cliimp(exp), OST_CREATE,
1113 GOTO(out_req, rc = -ENOMEM);
1115 body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof (*body));
1116 memcpy(&body->oa, oa, sizeof(body->oa));
1118 request->rq_replen = lustre_msg_size(1, &size);
1119 rc = ptlrpc_queue_wait(request);
1123 body = lustre_swab_repbuf(request, 0, sizeof(*body),
1124 lustre_swab_ost_body);
1126 CERROR ("can't unpack ost_body\n");
1127 GOTO (out_req, rc = -EPROTO);
1130 memcpy(oa, &body->oa, sizeof(*oa));
1132 /* store ino/generation for recovery */
1133 body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof (*body));
1134 body->oa.o_id = oa->o_id;
1135 body->oa.o_generation = oa->o_generation;
1137 CDEBUG(D_HA, "transno: "LPD64"\n", request->rq_repmsg->transno);
1140 ptlrpc_req_finished(request);
1144 int mdc_brw(int rw, struct obd_export *exp, struct obdo *oa,
1145 struct lov_stripe_md *ea, obd_count oa_bufs,
1146 struct brw_page *pgarr, struct obd_trans_info *oti)
1148 struct ptlrpc_bulk_desc *desc;
1149 struct niobuf_remote *niobuf;
1150 struct ptlrpc_request *req;
1151 struct obd_ioobj *ioobj;
1152 struct ost_body *body;
1156 opc = ((rw & OBD_BRW_WRITE) != 0) ? OST_WRITE : OST_READ;
1158 size[0] = sizeof(*body);
1159 size[1] = sizeof(*ioobj);
1160 size[2] = oa_bufs * sizeof(*niobuf);
1162 req = ptlrpc_prep_req(class_exp2cliimp(exp), opc, 3, size, NULL);
1163 LASSERT(req != NULL);
1165 if (opc == OST_WRITE)
1166 desc = ptlrpc_prep_bulk_imp(req, oa_bufs, BULK_GET_SOURCE,
1169 desc = ptlrpc_prep_bulk_imp(req, oa_bufs, BULK_PUT_SINK,
1171 LASSERT(desc != NULL);
1173 body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof(*body));
1174 ioobj = lustre_msg_buf(req->rq_reqmsg, 1, sizeof(*ioobj));
1175 niobuf = lustre_msg_buf(req->rq_reqmsg, 2, oa_bufs * sizeof(*niobuf));
1177 memcpy(&body->oa, oa, sizeof(*oa));
1178 obdo_to_ioobj(oa, ioobj);
1179 ioobj->ioo_bufcnt = oa_bufs;
1181 for (i = 0; i < oa_bufs; i++, niobuf++) {
1182 struct brw_page *pg = &pgarr[i];
1184 LASSERT(pg->count > 0);
1185 LASSERT((pg->off & ~PAGE_MASK) + pg->count <= PAGE_SIZE);
1187 ptlrpc_prep_bulk_page(desc, pg->pg, pg->off & ~PAGE_MASK,
1190 niobuf->offset = pg->off;
1191 niobuf->len = pg->count;
1192 niobuf->flags = pg->flag;
1195 /* size[0] still sizeof (*body) */
1196 if (opc == OST_WRITE) {
1197 /* 1 RC per niobuf */
1198 size[1] = sizeof(__u32) * oa_bufs;
1199 req->rq_replen = lustre_msg_size(2, size);
1201 /* 1 RC for the whole I/O */
1202 req->rq_replen = lustre_msg_size(1, size);
1204 err = ptlrpc_queue_wait(req);
1207 ptlrpc_req_finished(req);
1211 static int mdc_valid_attrs(struct obd_export *exp, struct ll_fid *fid)
1213 struct ldlm_res_id res_id = { .name = {0} };
1214 struct obd_device *obd = exp->exp_obd;
1215 struct lustre_handle lockh;
1216 ldlm_policy_data_t policy;
1220 res_id.name[0] = fid->id;
1221 res_id.name[1] = fid->generation;
1222 policy.l_inodebits.bits = MDS_INODELOCK_UPDATE;
1224 CDEBUG(D_INFO, "trying to match res "LPU64"\n", res_id.name[0]);
1226 /* FIXME use LDLM_FL_TEST_LOCK instead */
1227 flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_CBPENDING;
1228 if (ldlm_lock_match(obd->obd_namespace, flags, &res_id,
1229 LDLM_IBITS, &policy, LCK_PR, &lockh)) {
1230 ldlm_lock_decref(&lockh, LCK_PR);
1234 if (ldlm_lock_match(obd->obd_namespace, flags, &res_id,
1235 LDLM_IBITS, &policy, LCK_PW, &lockh)) {
1236 ldlm_lock_decref(&lockh, LCK_PW);
1241 static int mdc_change_cbdata_name(struct obd_export *exp, struct ll_fid *pfid,
1242 char *name, int len, struct ll_fid *cfid,
1243 ldlm_iterator_t it, void *data)
1246 rc = mdc_change_cbdata(exp, cfid, it, data);
1250 struct obd_ops mdc_obd_ops = {
1251 .o_owner = THIS_MODULE,
1252 .o_attach = mdc_attach,
1253 .o_detach = mdc_detach,
1254 .o_setup = mdc_setup,
1255 .o_precleanup = mdc_precleanup,
1256 .o_cleanup = mdc_cleanup,
1257 .o_connect = client_connect_import,
1258 .o_disconnect = client_disconnect_export,
1259 .o_iocontrol = mdc_iocontrol,
1260 .o_set_info = mdc_set_info,
1261 .o_statfs = mdc_statfs,
1263 .o_unpin = mdc_unpin,
1264 .o_import_event = mdc_import_event,
1265 .o_llog_init = mdc_llog_init,
1266 .o_llog_finish = mdc_llog_finish,
1267 .o_create = mdc_obj_create,
1268 .o_set_info = mdc_set_info,
1269 .o_get_info = mdc_get_info,
1271 .o_init_ea_size = mdc_init_ea_size,
1274 struct md_ops mdc_md_ops = {
1275 .m_getstatus = mdc_getstatus,
1276 .m_getattr = mdc_getattr,
1277 .m_change_cbdata = mdc_change_cbdata,
1278 .m_close = mdc_close,
1279 .m_create = mdc_create,
1280 .m_done_writing = mdc_done_writing,
1281 .m_enqueue = mdc_enqueue,
1282 .m_getattr_name = mdc_getattr_name,
1283 .m_intent_lock = mdc_intent_lock,
1285 .m_rename = mdc_rename,
1286 .m_setattr = mdc_setattr,
1288 .m_readpage = mdc_readpage,
1289 .m_unlink = mdc_unlink,
1290 .m_valid_attrs = mdc_valid_attrs,
1291 .m_req2lustre_md = mdc_req2lustre_md,
1292 .m_set_open_replay_data = mdc_set_open_replay_data,
1293 .m_clear_open_replay_data = mdc_clear_open_replay_data,
1294 .m_store_inode_generation = mdc_store_inode_generation,
1295 .m_set_lock_data = mdc_set_lock_data,
1296 .m_get_real_obd = mdc_get_real_obd,
1297 .m_change_cbdata_name = mdc_change_cbdata_name,
1298 .m_change_cbdata = mdc_change_cbdata,
1301 int __init mdc_init(void)
1303 struct lprocfs_static_vars lvars;
1304 lprocfs_init_vars(mdc, &lvars);
1305 return class_register_type(&mdc_obd_ops, &mdc_md_ops, lvars.module_vars,
1310 static void /*__exit*/ mdc_exit(void)
1312 class_unregister_type(LUSTRE_MDC_NAME);
1315 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
1316 MODULE_DESCRIPTION("Lustre Metadata Client");
1317 MODULE_LICENSE("GPL");
1319 EXPORT_SYMBOL(mdc_req2lustre_md);
1320 EXPORT_SYMBOL(mdc_change_cbdata);
1321 EXPORT_SYMBOL(mdc_getstatus);
1322 EXPORT_SYMBOL(mdc_getattr);
1323 EXPORT_SYMBOL(mdc_getattr_name);
1324 EXPORT_SYMBOL(mdc_create);
1325 EXPORT_SYMBOL(mdc_unlink);
1326 EXPORT_SYMBOL(mdc_rename);
1327 EXPORT_SYMBOL(mdc_link);
1328 EXPORT_SYMBOL(mdc_readpage);
1329 EXPORT_SYMBOL(mdc_setattr);
1330 EXPORT_SYMBOL(mdc_close);
1331 EXPORT_SYMBOL(mdc_done_writing);
1332 EXPORT_SYMBOL(mdc_sync);
1333 EXPORT_SYMBOL(mdc_set_open_replay_data);
1334 EXPORT_SYMBOL(mdc_clear_open_replay_data);
1335 EXPORT_SYMBOL(mdc_store_inode_generation);
1337 module_init(mdc_init);
1338 module_exit(mdc_exit);