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("mds_num") && strcmp(key, "mds_num") == 0) {
722 struct ptlrpc_request *req;
723 int rc, size[2] = {keylen, vallen};
724 char *bufs[2] = {key, val};
726 req = ptlrpc_prep_req(class_exp2cliimp(exp), OST_SET_INFO, 2,
731 req->rq_replen = lustre_msg_size(0, NULL);
732 rc = ptlrpc_queue_wait(req);
733 ptlrpc_req_finished(req);
735 } else if (keylen >= strlen("inter_mds") && strcmp(key, "inter_mds") == 0) {
736 struct obd_import *imp = class_exp2cliimp(exp);
737 imp->imp_server_timeout = 1;
738 CDEBUG(D_OTHER, "%s: timeout / 2\n", exp->exp_obd->obd_name);
744 static int mdc_statfs(struct obd_device *obd, struct obd_statfs *osfs,
745 unsigned long max_age)
747 struct ptlrpc_request *req;
748 struct obd_statfs *msfs;
749 int rc, size = sizeof(*msfs);
752 /* We could possibly pass max_age in the request (as an absolute
753 * timestamp or a "seconds.usec ago") so the target can avoid doing
754 * extra calls into the filesystem if that isn't necessary (e.g.
755 * during mount that would help a bit). Having relative timestamps
756 * is not so great if request processing is slow, while absolute
757 * timestamps are not ideal because they need time synchronization. */
758 req = ptlrpc_prep_req(obd->u.cli.cl_import, MDS_STATFS, 0, NULL, NULL);
762 req->rq_replen = lustre_msg_size(1, &size);
764 mdc_get_rpc_lock(obd->u.cli.cl_rpc_lock, NULL);
765 rc = ptlrpc_queue_wait(req);
766 mdc_put_rpc_lock(obd->u.cli.cl_rpc_lock, NULL);
771 msfs = lustre_swab_repbuf(req, 0, sizeof(*msfs),lustre_swab_obd_statfs);
773 CERROR("Can't unpack obd_statfs\n");
774 GOTO(out, rc = -EPROTO);
777 memcpy(osfs, msfs, sizeof (*msfs));
780 ptlrpc_req_finished(req);
785 static int mdc_pin(struct obd_export *exp, obd_id ino, __u32 gen, int type,
786 struct obd_client_handle *handle, int flag)
788 struct ptlrpc_request *req;
789 struct mds_body *body;
790 int rc, size = sizeof(*body);
793 req = ptlrpc_prep_req(class_exp2cliimp(exp), MDS_PIN, 1, &size, NULL);
797 body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*body));
798 mdc_pack_fid(&body->fid1, ino, gen, type);
801 req->rq_replen = lustre_msg_size(1, &size);
803 mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
804 rc = ptlrpc_queue_wait(req);
805 mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
807 CERROR("pin failed: %d\n", rc);
808 ptlrpc_req_finished(req);
812 body = lustre_swab_repbuf(req, 0, sizeof(*body), lustre_swab_mds_body);
814 ptlrpc_req_finished(req);
818 memcpy(&handle->och_fh, &body->handle, sizeof(body->handle));
819 handle->och_magic = OBD_CLIENT_HANDLE_MAGIC;
821 OBD_ALLOC(handle->och_mod, sizeof(*handle->och_mod));
822 if (handle->och_mod == NULL) {
823 DEBUG_REQ(D_ERROR, req, "can't allocate mdc_open_data");
826 handle->och_mod->mod_open_req = req; /* will be dropped by unpin */
831 static int mdc_unpin(struct obd_export *exp,
832 struct obd_client_handle *handle, int flag)
834 struct ptlrpc_request *req;
835 struct mds_body *body;
836 int rc, size = sizeof(*body);
839 if (handle->och_magic != OBD_CLIENT_HANDLE_MAGIC)
842 req = ptlrpc_prep_req(class_exp2cliimp(exp), MDS_CLOSE, 1, &size, NULL);
846 body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof(*body));
847 memcpy(&body->handle, &handle->och_fh, sizeof(body->handle));
850 req->rq_replen = lustre_msg_size(0, NULL);
851 mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
852 rc = ptlrpc_queue_wait(req);
853 mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
856 CERROR("unpin failed: %d\n", rc);
858 ptlrpc_req_finished(req);
859 ptlrpc_req_finished(handle->och_mod->mod_open_req);
860 OBD_FREE(handle->och_mod, sizeof(*handle->och_mod));
864 int mdc_sync(struct obd_export *exp, struct ll_fid *fid,
865 struct ptlrpc_request **request)
867 struct ptlrpc_request *req;
868 struct mds_body *body;
869 int size = sizeof(*body);
873 req = ptlrpc_prep_req(class_exp2cliimp(exp), MDS_SYNC, 1,&size,NULL);
875 RETURN(rc = -ENOMEM);
878 body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*body));
879 memcpy(&body->fid1, fid, sizeof(*fid));
880 mdc_pack_req_body(req);
883 req->rq_replen = lustre_msg_size(1, &size);
885 rc = ptlrpc_queue_wait(req);
886 if (rc || request == NULL)
887 ptlrpc_req_finished(req);
894 static int mdc_import_event(struct obd_device *obd, struct obd_import *imp,
895 enum obd_import_event event)
899 LASSERT(imp->imp_obd == obd);
902 case IMP_EVENT_DISCON: {
905 case IMP_EVENT_INACTIVE: {
906 if (obd->obd_observer)
907 rc = obd_notify(obd->obd_observer, obd, 0, 0);
910 case IMP_EVENT_INVALIDATE: {
911 struct ldlm_namespace *ns = obd->obd_namespace;
913 ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
917 case IMP_EVENT_ACTIVE: {
918 if (obd->obd_observer)
919 rc = obd_notify(obd->obd_observer, obd, 1, 0);
923 CERROR("Unknown import event %d\n", event);
929 static int mdc_attach(struct obd_device *dev, obd_count len, void *data)
931 struct lprocfs_static_vars lvars;
933 lprocfs_init_vars(mdc, &lvars);
934 return lprocfs_obd_attach(dev, lvars.obd_vars);
937 static int mdc_detach(struct obd_device *dev)
939 return lprocfs_obd_detach(dev);
942 static int mdc_setup(struct obd_device *obd, obd_count len, void *buf)
944 struct client_obd *cli = &obd->u.cli;
948 OBD_ALLOC(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
949 if (!cli->cl_rpc_lock)
951 mdc_init_rpc_lock(cli->cl_rpc_lock);
955 OBD_ALLOC(cli->cl_setattr_lock, sizeof (*cli->cl_setattr_lock));
956 if (!cli->cl_setattr_lock)
957 GOTO(err_rpc_lock, rc = -ENOMEM);
958 mdc_init_rpc_lock(cli->cl_setattr_lock);
960 rc = client_obd_setup(obd, len, buf);
962 GOTO(err_setattr_lock, rc);
964 rc = obd_llog_init(obd, &obd->obd_llogs, obd, 0, NULL);
967 CERROR("failed to setup llogging subsystems\n");
973 OBD_FREE(cli->cl_setattr_lock, sizeof (*cli->cl_setattr_lock));
975 OBD_FREE(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
980 static int mdc_init_ea_size(struct obd_export *exp, int easize, int cookiesize)
982 struct obd_device *obd = exp->exp_obd;
983 struct client_obd *cli = &obd->u.cli;
986 if (cli->cl_max_mds_easize < easize)
987 cli->cl_max_mds_easize = easize;
988 if (cli->cl_max_mds_cookiesize < cookiesize)
989 cli->cl_max_mds_cookiesize = cookiesize;
993 static int mdc_precleanup(struct obd_device *obd, int flags)
997 rc = obd_llog_finish(obd, &obd->obd_llogs, 0);
999 CERROR("failed to cleanup llogging subsystems\n");
1004 static int mdc_cleanup(struct obd_device *obd, int flags)
1006 struct client_obd *cli = &obd->u.cli;
1008 OBD_FREE(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
1009 OBD_FREE(cli->cl_setattr_lock, sizeof (*cli->cl_setattr_lock));
1013 return client_obd_cleanup(obd, flags);
1017 static int mdc_llog_init(struct obd_device *obd, struct obd_llogs *llogs,
1018 struct obd_device *tgt, int count,
1019 struct llog_catid *logid)
1021 struct llog_ctxt *ctxt;
1025 rc = obd_llog_setup(obd, llogs, LLOG_CONFIG_REPL_CTXT, tgt, 0, NULL,
1028 ctxt = llog_get_context(llogs, LLOG_CONFIG_REPL_CTXT);
1029 ctxt->loc_imp = obd->u.cli.cl_import;
1035 static int mdc_llog_finish(struct obd_device *obd,
1036 struct obd_llogs *llogs, int count)
1041 rc = obd_llog_cleanup(llog_get_context(llogs, LLOG_CONFIG_REPL_CTXT));
1044 static struct obd_device *mdc_get_real_obd(struct obd_export *exp,
1045 char *name, int len)
1048 RETURN(exp->exp_obd);
1051 static int mdc_get_info(struct obd_export *exp, obd_count keylen,
1052 void *key, __u32 *vallen, void *val)
1055 if (!vallen || !val)
1058 if (keylen >= strlen("mdsize") && strcmp(key, "mdsize") == 0) {
1059 struct ptlrpc_request *req;
1061 char *bufs[1] = {key};
1063 req = ptlrpc_prep_req(class_exp2cliimp(exp), OST_GET_INFO, 1,
1068 req->rq_replen = lustre_msg_size(1, vallen);
1069 rc = ptlrpc_queue_wait(req);
1073 /* FIXME: right swap routine here! */
1074 reply = lustre_swab_repbuf(req, 0, sizeof(*reply), NULL);
1075 if (reply == NULL) {
1076 CERROR("Can't unpack mdsize\n");
1077 GOTO(out, rc = -EPROTO);
1079 *((__u32 *)val) = *reply;
1081 ptlrpc_req_finished(req);
1087 int mdc_obj_create(struct obd_export *exp, struct obdo *oa,
1088 struct lov_stripe_md **ea, struct obd_trans_info *oti)
1090 struct ptlrpc_request *request;
1091 struct ost_body *body;
1092 int rc, size = sizeof(*body);
1097 request = ptlrpc_prep_req(class_exp2cliimp(exp), OST_CREATE,
1100 GOTO(out_req, rc = -ENOMEM);
1102 body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof (*body));
1103 memcpy(&body->oa, oa, sizeof(body->oa));
1105 request->rq_replen = lustre_msg_size(1, &size);
1106 rc = ptlrpc_queue_wait(request);
1110 body = lustre_swab_repbuf(request, 0, sizeof(*body),
1111 lustre_swab_ost_body);
1113 CERROR ("can't unpack ost_body\n");
1114 GOTO (out_req, rc = -EPROTO);
1117 memcpy(oa, &body->oa, sizeof(*oa));
1119 /* store ino/generation for recovery */
1120 body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof (*body));
1121 body->oa.o_id = oa->o_id;
1122 body->oa.o_generation = oa->o_generation;
1124 CDEBUG(D_HA, "transno: "LPD64"\n", request->rq_repmsg->transno);
1127 ptlrpc_req_finished(request);
1131 int mdc_brw(int rw, struct obd_export *exp, struct obdo *oa,
1132 struct lov_stripe_md *ea, obd_count oa_bufs,
1133 struct brw_page *pgarr, struct obd_trans_info *oti)
1135 struct ptlrpc_bulk_desc *desc;
1136 struct niobuf_remote *niobuf;
1137 struct ptlrpc_request *req;
1138 struct obd_ioobj *ioobj;
1139 struct ost_body *body;
1143 opc = ((rw & OBD_BRW_WRITE) != 0) ? OST_WRITE : OST_READ;
1145 size[0] = sizeof(*body);
1146 size[1] = sizeof(*ioobj);
1147 size[2] = oa_bufs * sizeof(*niobuf);
1149 req = ptlrpc_prep_req(class_exp2cliimp(exp), opc, 3, size, NULL);
1150 LASSERT(req != NULL);
1152 if (opc == OST_WRITE)
1153 desc = ptlrpc_prep_bulk_imp(req, oa_bufs, BULK_GET_SOURCE,
1156 desc = ptlrpc_prep_bulk_imp(req, oa_bufs, BULK_PUT_SINK,
1158 LASSERT(desc != NULL);
1160 body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof(*body));
1161 ioobj = lustre_msg_buf(req->rq_reqmsg, 1, sizeof(*ioobj));
1162 niobuf = lustre_msg_buf(req->rq_reqmsg, 2, oa_bufs * sizeof(*niobuf));
1164 memcpy(&body->oa, oa, sizeof(*oa));
1165 obdo_to_ioobj(oa, ioobj);
1166 ioobj->ioo_bufcnt = oa_bufs;
1168 for (i = 0; i < oa_bufs; i++, niobuf++) {
1169 struct brw_page *pg = &pgarr[i];
1171 LASSERT(pg->count > 0);
1172 LASSERT((pg->off & ~PAGE_MASK) + pg->count <= PAGE_SIZE);
1174 ptlrpc_prep_bulk_page(desc, pg->pg, pg->off & ~PAGE_MASK,
1177 niobuf->offset = pg->off;
1178 niobuf->len = pg->count;
1179 niobuf->flags = pg->flag;
1182 /* size[0] still sizeof (*body) */
1183 if (opc == OST_WRITE) {
1184 /* 1 RC per niobuf */
1185 size[1] = sizeof(__u32) * oa_bufs;
1186 req->rq_replen = lustre_msg_size(2, size);
1188 /* 1 RC for the whole I/O */
1189 req->rq_replen = lustre_msg_size(1, size);
1191 err = ptlrpc_queue_wait(req);
1194 ptlrpc_req_finished(req);
1198 static int mdc_valid_attrs(struct obd_export *exp, struct ll_fid *fid)
1200 struct ldlm_res_id res_id = { .name = {0} };
1201 struct obd_device *obd = exp->exp_obd;
1202 struct lustre_handle lockh;
1203 ldlm_policy_data_t policy;
1207 res_id.name[0] = fid->id;
1208 res_id.name[1] = fid->generation;
1209 policy.l_inodebits.bits = MDS_INODELOCK_UPDATE;
1211 CDEBUG(D_INFO, "trying to match res "LPU64"\n", res_id.name[0]);
1213 /* FIXME use LDLM_FL_TEST_LOCK instead */
1214 flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_CBPENDING;
1215 if (ldlm_lock_match(obd->obd_namespace, flags, &res_id,
1216 LDLM_IBITS, &policy, LCK_PR, &lockh)) {
1217 ldlm_lock_decref(&lockh, LCK_PR);
1221 if (ldlm_lock_match(obd->obd_namespace, flags, &res_id,
1222 LDLM_IBITS, &policy, LCK_PW, &lockh)) {
1223 ldlm_lock_decref(&lockh, LCK_PW);
1228 static int mdc_change_cbdata_name(struct obd_export *exp, struct ll_fid *pfid,
1229 char *name, int len, struct ll_fid *cfid,
1230 ldlm_iterator_t it, void *data)
1233 rc = mdc_change_cbdata(exp, cfid, it, data);
1237 struct obd_ops mdc_obd_ops = {
1238 .o_owner = THIS_MODULE,
1239 .o_attach = mdc_attach,
1240 .o_detach = mdc_detach,
1241 .o_setup = mdc_setup,
1242 .o_precleanup = mdc_precleanup,
1243 .o_cleanup = mdc_cleanup,
1244 .o_connect = client_connect_import,
1245 .o_disconnect = client_disconnect_export,
1246 .o_iocontrol = mdc_iocontrol,
1247 .o_statfs = mdc_statfs,
1249 .o_unpin = mdc_unpin,
1250 .o_import_event = mdc_import_event,
1251 .o_llog_init = mdc_llog_init,
1252 .o_llog_finish = mdc_llog_finish,
1253 .o_create = mdc_obj_create,
1254 .o_set_info = mdc_set_info,
1255 .o_get_info = mdc_get_info,
1257 .o_init_ea_size = mdc_init_ea_size,
1260 struct md_ops mdc_md_ops = {
1261 .m_getstatus = mdc_getstatus,
1262 .m_getattr = mdc_getattr,
1263 .m_change_cbdata = mdc_change_cbdata,
1264 .m_close = mdc_close,
1265 .m_create = mdc_create,
1266 .m_done_writing = mdc_done_writing,
1267 .m_enqueue = mdc_enqueue,
1268 .m_getattr_name = mdc_getattr_name,
1269 .m_intent_lock = mdc_intent_lock,
1271 .m_rename = mdc_rename,
1272 .m_setattr = mdc_setattr,
1274 .m_readpage = mdc_readpage,
1275 .m_unlink = mdc_unlink,
1276 .m_valid_attrs = mdc_valid_attrs,
1277 .m_req2lustre_md = mdc_req2lustre_md,
1278 .m_set_open_replay_data = mdc_set_open_replay_data,
1279 .m_clear_open_replay_data = mdc_clear_open_replay_data,
1280 .m_store_inode_generation = mdc_store_inode_generation,
1281 .m_set_lock_data = mdc_set_lock_data,
1282 .m_get_real_obd = mdc_get_real_obd,
1283 .m_change_cbdata_name = mdc_change_cbdata_name,
1284 .m_change_cbdata = mdc_change_cbdata,
1287 int __init mdc_init(void)
1289 struct lprocfs_static_vars lvars;
1290 lprocfs_init_vars(mdc, &lvars);
1291 return class_register_type(&mdc_obd_ops, &mdc_md_ops, lvars.module_vars,
1296 static void /*__exit*/ mdc_exit(void)
1298 class_unregister_type(LUSTRE_MDC_NAME);
1301 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
1302 MODULE_DESCRIPTION("Lustre Metadata Client");
1303 MODULE_LICENSE("GPL");
1305 EXPORT_SYMBOL(mdc_req2lustre_md);
1306 EXPORT_SYMBOL(mdc_change_cbdata);
1307 EXPORT_SYMBOL(mdc_getstatus);
1308 EXPORT_SYMBOL(mdc_getattr);
1309 EXPORT_SYMBOL(mdc_getattr_name);
1310 EXPORT_SYMBOL(mdc_create);
1311 EXPORT_SYMBOL(mdc_unlink);
1312 EXPORT_SYMBOL(mdc_rename);
1313 EXPORT_SYMBOL(mdc_link);
1314 EXPORT_SYMBOL(mdc_readpage);
1315 EXPORT_SYMBOL(mdc_setattr);
1316 EXPORT_SYMBOL(mdc_close);
1317 EXPORT_SYMBOL(mdc_done_writing);
1318 EXPORT_SYMBOL(mdc_sync);
1319 EXPORT_SYMBOL(mdc_set_open_replay_data);
1320 EXPORT_SYMBOL(mdc_clear_open_replay_data);
1321 EXPORT_SYMBOL(mdc_store_inode_generation);
1323 module_init(mdc_init);
1324 module_exit(mdc_exit);