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 = sizeof(struct mds_body);
490 int rc, repsize[3] = {sizeof(struct mds_body),
491 obd->u.cli.cl_max_mds_easize,
492 obd->u.cli.cl_max_mds_cookiesize};
493 struct ptlrpc_request *req;
494 struct mdc_open_data *mod;
495 struct l_wait_info lwi;
498 req = ptlrpc_prep_req(class_exp2cliimp(exp), MDS_CLOSE, 1, &reqsize,
501 GOTO(out, rc = -ENOMEM);
503 /* Ensure that this close's handle is fixed up during replay. */
504 LASSERT(och != NULL);
506 if (likely(mod != NULL)) {
507 mod->mod_close_req = req;
508 LASSERT(mod->mod_open_req->rq_type != LI_POISON);
509 DEBUG_REQ(D_HA, mod->mod_open_req, "matched open req %p",
512 CDEBUG(D_HA, "couldn't find open req; expecting close error\n");
515 mdc_close_pack(req, 0, oa, oa->o_valid, och);
517 req->rq_replen = lustre_msg_size(3, repsize);
518 req->rq_commit_cb = mdc_commit_close;
519 LASSERT(req->rq_cb_data == NULL);
520 req->rq_cb_data = mod;
522 /* We hand a ref to the rpcd here, so we need another one of our own. */
523 ptlrpc_request_addref(req);
525 mdc_get_rpc_lock(obd->u.cli.cl_rpc_lock, NULL);
526 req->rq_interpret_reply = mdc_close_interpret;
527 req->rq_async_args.pointer_arg[0] = obd->u.cli.cl_rpc_lock;
528 req->rq_async_args.pointer_arg[1] = obd;
529 ptlrpcd_add_req(req);
530 lwi = LWI_TIMEOUT_INTR(MAX(req->rq_timeout * HZ, 1), go_back_to_sleep,
532 rc = l_wait_event(req->rq_reply_waitq, mdc_close_check_reply(req),
534 if (req->rq_repmsg == NULL) {
535 CDEBUG(D_HA, "request failed to send: %p, %d\n", req,
538 rc = req->rq_status ? req->rq_status : -EIO;
539 } else if (rc == 0) {
540 rc = req->rq_repmsg->status;
541 if (req->rq_repmsg->type == PTL_RPC_MSG_ERR) {
542 DEBUG_REQ(D_ERROR, req, "type == PTL_RPC_MSG_ERR, err "
546 } else if (mod == NULL) {
547 CERROR("Unexpected: can't find mdc_open_data, but the "
548 "close succeeded. Please tell CFS.\n");
550 if (!lustre_swab_repbuf(req, 0, sizeof(struct mds_body),
551 lustre_swab_mds_body)) {
552 CERROR("Error unpacking mds_body\n");
556 if (req->rq_async_args.pointer_arg[0] != NULL) {
557 CERROR("returned without dropping rpc_lock: rc %d\n", rc);
558 mdc_close_interpret(req, &req->rq_async_args, rc);
559 portals_debug_dumplog();
568 int mdc_done_writing(struct obd_export *exp, struct obdo *obdo)
570 struct ptlrpc_request *req;
571 struct mds_body *body;
572 int rc, size = sizeof(*body);
575 req = ptlrpc_prep_req(class_exp2cliimp(exp), MDS_DONE_WRITING, 1,
580 body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof(*body));
581 mdc_pack_fid(&body->fid1, obdo->o_id, 0, obdo->o_mode);
582 body->size = obdo->o_size;
583 body->blocks = obdo->o_blocks;
584 body->flags = obdo->o_flags;
585 body->valid = obdo->o_valid;
586 // memcpy(&body->handle, &och->och_fh, sizeof(body->handle));
588 req->rq_replen = lustre_msg_size(1, &size);
590 rc = ptlrpc_queue_wait(req);
591 ptlrpc_req_finished(req);
595 int mdc_readpage(struct obd_export *exp, struct ll_fid *mdc_fid,
596 __u64 offset, struct page *page,
597 struct ptlrpc_request **request)
599 struct obd_import *imp = class_exp2cliimp(exp);
600 struct ptlrpc_request *req = NULL;
601 struct ptlrpc_bulk_desc *desc = NULL;
602 struct mds_body *body;
603 int rc, size = sizeof(*body);
606 CDEBUG(D_INODE, "inode: %ld\n", (long)mdc_fid->id);
608 req = ptlrpc_prep_req(imp, MDS_READPAGE, 1, &size, NULL);
610 GOTO(out, rc = -ENOMEM);
611 /* XXX FIXME bug 249 */
612 req->rq_request_portal = MDS_READPAGE_PORTAL;
614 desc = ptlrpc_prep_bulk_imp(req, 1, BULK_PUT_SINK, MDS_BULK_PORTAL);
616 GOTO(out, rc = -ENOMEM);
617 /* NB req now owns desc and will free it when it gets freed */
619 ptlrpc_prep_bulk_page(desc, page, 0, PAGE_CACHE_SIZE);
621 mdc_readdir_pack(req, offset, PAGE_CACHE_SIZE, mdc_fid);
623 req->rq_replen = lustre_msg_size(1, &size);
624 rc = ptlrpc_queue_wait(req);
627 body = lustre_swab_repbuf(req, 0, sizeof (*body),
628 lustre_swab_mds_body);
630 CERROR("Can't unpack mds_body\n");
631 GOTO(out, rc = -EPROTO);
634 if (req->rq_bulk->bd_nob_transferred != PAGE_CACHE_SIZE) {
635 CERROR ("Unexpected # bytes transferred: %d"
637 req->rq_bulk->bd_nob_transferred,
639 GOTO (out, rc = -EPROTO);
649 static int mdc_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
650 void *karg, void *uarg)
652 struct obd_device *obd = exp->exp_obd;
653 struct obd_ioctl_data *data = karg;
654 struct obd_import *imp = obd->u.cli.cl_import;
655 struct llog_ctxt *ctxt;
659 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
662 if (!try_module_get(THIS_MODULE)) {
663 CERROR("Can't get module. Is it alive?");
668 case OBD_IOC_CLIENT_RECOVER:
669 rc = ptlrpc_recover_import(imp, data->ioc_inlbuf1);
673 case IOC_OSC_SET_ACTIVE:
674 rc = ptlrpc_set_import_active(imp, data->ioc_offset);
676 case OBD_IOC_PARSE: {
677 ctxt = llog_get_context(&exp->exp_obd->obd_llogs,
678 LLOG_CONFIG_REPL_CTXT);
679 rc = class_config_process_llog(ctxt, data->ioc_inlbuf1, NULL);
683 case OBD_IOC_LLOG_INFO:
684 case OBD_IOC_LLOG_PRINT: {
685 ctxt = llog_get_context(&obd->obd_llogs, LLOG_CONFIG_REPL_CTXT);
686 rc = llog_ioctl(ctxt, cmd, data);
692 CERROR("mdc_ioctl(): unrecognised ioctl %#x\n", cmd);
693 GOTO(out, rc = -ENOTTY);
696 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
699 module_put(THIS_MODULE);
705 int mdc_set_info(struct obd_export *exp, obd_count keylen,
706 void *key, obd_count vallen, void *val)
710 if (keylen == strlen("initial_recov") &&
711 memcmp(key, "initial_recov", strlen("initial_recov")) == 0) {
712 struct obd_import *imp = exp->exp_obd->u.cli.cl_import;
713 if (vallen != sizeof(int))
715 imp->imp_initial_recov = *(int *)val;
716 CDEBUG(D_HA, "%s: set imp_no_init_recov = %d\n",
717 exp->exp_obd->obd_name,
718 imp->imp_initial_recov);
720 } else if (keylen >= strlen("client") && strcmp(key, "client") == 0) {
721 struct ptlrpc_request *req;
722 char *bufs[1] = {key};
724 req = ptlrpc_prep_req(class_exp2cliimp(exp), OST_SET_INFO, 1,
729 req->rq_replen = lustre_msg_size(0, NULL);
730 rc = ptlrpc_queue_wait(req);
731 ptlrpc_req_finished(req);
733 } else if (keylen >= strlen("mds_num") && strcmp(key, "mds_num") == 0) {
734 struct ptlrpc_request *req;
735 int rc, size[2] = {keylen, vallen};
736 char *bufs[2] = {key, val};
738 req = ptlrpc_prep_req(class_exp2cliimp(exp), OST_SET_INFO, 2,
743 req->rq_replen = lustre_msg_size(0, NULL);
744 rc = ptlrpc_queue_wait(req);
745 ptlrpc_req_finished(req);
747 } else if (keylen >= strlen("inter_mds") && strcmp(key, "inter_mds") == 0) {
748 struct obd_import *imp = class_exp2cliimp(exp);
749 imp->imp_server_timeout = 1;
750 CDEBUG(D_OTHER, "%s: timeout / 2\n", exp->exp_obd->obd_name);
756 static int mdc_statfs(struct obd_device *obd, struct obd_statfs *osfs,
757 unsigned long max_age)
759 struct ptlrpc_request *req;
760 struct obd_statfs *msfs;
761 int rc, size = sizeof(*msfs);
764 /* We could possibly pass max_age in the request (as an absolute
765 * timestamp or a "seconds.usec ago") so the target can avoid doing
766 * extra calls into the filesystem if that isn't necessary (e.g.
767 * during mount that would help a bit). Having relative timestamps
768 * is not so great if request processing is slow, while absolute
769 * timestamps are not ideal because they need time synchronization. */
770 req = ptlrpc_prep_req(obd->u.cli.cl_import, MDS_STATFS, 0, NULL, NULL);
774 req->rq_replen = lustre_msg_size(1, &size);
776 mdc_get_rpc_lock(obd->u.cli.cl_rpc_lock, NULL);
777 rc = ptlrpc_queue_wait(req);
778 mdc_put_rpc_lock(obd->u.cli.cl_rpc_lock, NULL);
783 msfs = lustre_swab_repbuf(req, 0, sizeof(*msfs),lustre_swab_obd_statfs);
785 CERROR("Can't unpack obd_statfs\n");
786 GOTO(out, rc = -EPROTO);
789 memcpy(osfs, msfs, sizeof (*msfs));
792 ptlrpc_req_finished(req);
797 static int mdc_pin(struct obd_export *exp, obd_id ino, __u32 gen, int type,
798 struct obd_client_handle *handle, int flag)
800 struct ptlrpc_request *req;
801 struct mds_body *body;
802 int rc, size = sizeof(*body);
805 req = ptlrpc_prep_req(class_exp2cliimp(exp), MDS_PIN, 1, &size, NULL);
809 body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*body));
810 mdc_pack_fid(&body->fid1, ino, gen, type);
813 req->rq_replen = lustre_msg_size(1, &size);
815 mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
816 rc = ptlrpc_queue_wait(req);
817 mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
819 CERROR("pin failed: %d\n", rc);
820 ptlrpc_req_finished(req);
824 body = lustre_swab_repbuf(req, 0, sizeof(*body), lustre_swab_mds_body);
826 ptlrpc_req_finished(req);
830 memcpy(&handle->och_fh, &body->handle, sizeof(body->handle));
831 handle->och_magic = OBD_CLIENT_HANDLE_MAGIC;
833 OBD_ALLOC(handle->och_mod, sizeof(*handle->och_mod));
834 if (handle->och_mod == NULL) {
835 DEBUG_REQ(D_ERROR, req, "can't allocate mdc_open_data");
838 handle->och_mod->mod_open_req = req; /* will be dropped by unpin */
843 static int mdc_unpin(struct obd_export *exp,
844 struct obd_client_handle *handle, int flag)
846 struct ptlrpc_request *req;
847 struct mds_body *body;
848 int rc, size = sizeof(*body);
851 if (handle->och_magic != OBD_CLIENT_HANDLE_MAGIC)
854 req = ptlrpc_prep_req(class_exp2cliimp(exp), MDS_CLOSE, 1, &size, NULL);
858 body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof(*body));
859 memcpy(&body->handle, &handle->och_fh, sizeof(body->handle));
862 req->rq_replen = lustre_msg_size(0, NULL);
863 mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
864 rc = ptlrpc_queue_wait(req);
865 mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
868 CERROR("unpin failed: %d\n", rc);
870 ptlrpc_req_finished(req);
871 ptlrpc_req_finished(handle->och_mod->mod_open_req);
872 OBD_FREE(handle->och_mod, sizeof(*handle->och_mod));
876 int mdc_sync(struct obd_export *exp, struct ll_fid *fid,
877 struct ptlrpc_request **request)
879 struct ptlrpc_request *req;
880 struct mds_body *body;
881 int size = sizeof(*body);
885 req = ptlrpc_prep_req(class_exp2cliimp(exp), MDS_SYNC, 1,&size,NULL);
887 RETURN(rc = -ENOMEM);
890 body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*body));
891 memcpy(&body->fid1, fid, sizeof(*fid));
892 mdc_pack_req_body(req);
895 req->rq_replen = lustre_msg_size(1, &size);
897 rc = ptlrpc_queue_wait(req);
898 if (rc || request == NULL)
899 ptlrpc_req_finished(req);
906 static int mdc_import_event(struct obd_device *obd, struct obd_import *imp,
907 enum obd_import_event event)
911 LASSERT(imp->imp_obd == obd);
914 case IMP_EVENT_DISCON: {
917 case IMP_EVENT_INACTIVE: {
918 if (obd->obd_observer)
919 rc = obd_notify(obd->obd_observer, obd, 0);
922 case IMP_EVENT_INVALIDATE: {
923 struct ldlm_namespace *ns = obd->obd_namespace;
925 ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
929 case IMP_EVENT_ACTIVE: {
930 if (obd->obd_observer)
931 rc = obd_notify(obd->obd_observer, obd, 1);
935 CERROR("Unknown import event %d\n", event);
941 static int mdc_attach(struct obd_device *dev, obd_count len, void *data)
943 struct lprocfs_static_vars lvars;
945 lprocfs_init_vars(mdc, &lvars);
946 return lprocfs_obd_attach(dev, lvars.obd_vars);
949 static int mdc_detach(struct obd_device *dev)
951 return lprocfs_obd_detach(dev);
954 static int mdc_setup(struct obd_device *obd, obd_count len, void *buf)
956 struct client_obd *cli = &obd->u.cli;
960 OBD_ALLOC(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
961 if (!cli->cl_rpc_lock)
963 mdc_init_rpc_lock(cli->cl_rpc_lock);
967 OBD_ALLOC(cli->cl_setattr_lock, sizeof (*cli->cl_setattr_lock));
968 if (!cli->cl_setattr_lock)
969 GOTO(err_rpc_lock, rc = -ENOMEM);
970 mdc_init_rpc_lock(cli->cl_setattr_lock);
972 rc = client_obd_setup(obd, len, buf);
974 GOTO(err_setattr_lock, rc);
976 rc = obd_llog_init(obd, &obd->obd_llogs, obd, 0, NULL);
979 CERROR("failed to setup llogging subsystems\n");
985 OBD_FREE(cli->cl_setattr_lock, sizeof (*cli->cl_setattr_lock));
987 OBD_FREE(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
992 static int mdc_init_ea_size(struct obd_export *exp, int easize, int cookiesize)
994 struct obd_device *obd = exp->exp_obd;
995 struct client_obd *cli = &obd->u.cli;
998 if (cli->cl_max_mds_easize < easize)
999 cli->cl_max_mds_easize = easize;
1000 if (cli->cl_max_mds_cookiesize < cookiesize)
1001 cli->cl_max_mds_cookiesize = cookiesize;
1005 static int mdc_precleanup(struct obd_device *obd, int flags)
1009 rc = obd_llog_finish(obd, &obd->obd_llogs, 0);
1011 CERROR("failed to cleanup llogging subsystems\n");
1016 static int mdc_cleanup(struct obd_device *obd, int flags)
1018 struct client_obd *cli = &obd->u.cli;
1020 OBD_FREE(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
1021 OBD_FREE(cli->cl_setattr_lock, sizeof (*cli->cl_setattr_lock));
1025 return client_obd_cleanup(obd, flags);
1029 static int mdc_llog_init(struct obd_device *obd, struct obd_llogs *llogs,
1030 struct obd_device *tgt, int count,
1031 struct llog_catid *logid)
1033 struct llog_ctxt *ctxt;
1037 rc = obd_llog_setup(obd, llogs, LLOG_CONFIG_REPL_CTXT, tgt, 0, NULL,
1040 ctxt = llog_get_context(llogs, LLOG_CONFIG_REPL_CTXT);
1041 ctxt->loc_imp = obd->u.cli.cl_import;
1047 static int mdc_llog_finish(struct obd_device *obd,
1048 struct obd_llogs *llogs, int count)
1053 rc = obd_llog_cleanup(llog_get_context(llogs, LLOG_CONFIG_REPL_CTXT));
1056 static struct obd_device *mdc_get_real_obd(struct obd_export *exp,
1057 char *name, int len)
1060 RETURN(exp->exp_obd);
1063 static int mdc_get_info(struct obd_export *exp, obd_count keylen,
1064 void *key, __u32 *vallen, void *val)
1067 if (!vallen || !val)
1070 if (keylen >= strlen("mdsize") && strcmp(key, "mdsize") == 0) {
1071 struct ptlrpc_request *req;
1073 char *bufs[1] = {key};
1075 req = ptlrpc_prep_req(class_exp2cliimp(exp), OST_GET_INFO, 1,
1080 req->rq_replen = lustre_msg_size(1, vallen);
1081 rc = ptlrpc_queue_wait(req);
1085 /* FIXME: right swap routine here! */
1086 reply = lustre_swab_repbuf(req, 0, sizeof(*reply), NULL);
1087 if (reply == NULL) {
1088 CERROR("Can't unpack mdsize\n");
1089 GOTO(out, rc = -EPROTO);
1091 *((__u32 *)val) = *reply;
1093 ptlrpc_req_finished(req);
1099 int mdc_obj_create(struct obd_export *exp, struct obdo *oa,
1100 struct lov_stripe_md **ea, struct obd_trans_info *oti)
1102 struct ptlrpc_request *request;
1103 struct ost_body *body;
1104 int rc, size = sizeof(*body);
1109 request = ptlrpc_prep_req(class_exp2cliimp(exp), OST_CREATE,
1112 GOTO(out_req, rc = -ENOMEM);
1114 body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof (*body));
1115 memcpy(&body->oa, oa, sizeof(body->oa));
1117 request->rq_replen = lustre_msg_size(1, &size);
1118 rc = ptlrpc_queue_wait(request);
1122 body = lustre_swab_repbuf(request, 0, sizeof(*body),
1123 lustre_swab_ost_body);
1125 CERROR ("can't unpack ost_body\n");
1126 GOTO (out_req, rc = -EPROTO);
1129 memcpy(oa, &body->oa, sizeof(*oa));
1131 /* store ino/generation for recovery */
1132 body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof (*body));
1133 body->oa.o_id = oa->o_id;
1134 body->oa.o_generation = oa->o_generation;
1136 CDEBUG(D_HA, "transno: "LPD64"\n", request->rq_repmsg->transno);
1139 ptlrpc_req_finished(request);
1143 int mdc_brw(int rw, struct obd_export *exp, struct obdo *oa,
1144 struct lov_stripe_md *ea, obd_count oa_bufs,
1145 struct brw_page *pgarr, struct obd_trans_info *oti)
1147 struct ptlrpc_bulk_desc *desc;
1148 struct niobuf_remote *niobuf;
1149 struct ptlrpc_request *req;
1150 struct obd_ioobj *ioobj;
1151 struct ost_body *body;
1155 opc = ((rw & OBD_BRW_WRITE) != 0) ? OST_WRITE : OST_READ;
1157 size[0] = sizeof(*body);
1158 size[1] = sizeof(*ioobj);
1159 size[2] = oa_bufs * sizeof(*niobuf);
1161 req = ptlrpc_prep_req(class_exp2cliimp(exp), opc, 3, size, NULL);
1162 LASSERT(req != NULL);
1164 if (opc == OST_WRITE)
1165 desc = ptlrpc_prep_bulk_imp(req, oa_bufs, BULK_GET_SOURCE,
1168 desc = ptlrpc_prep_bulk_imp(req, oa_bufs, BULK_PUT_SINK,
1170 LASSERT(desc != NULL);
1172 body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof(*body));
1173 ioobj = lustre_msg_buf(req->rq_reqmsg, 1, sizeof(*ioobj));
1174 niobuf = lustre_msg_buf(req->rq_reqmsg, 2, oa_bufs * sizeof(*niobuf));
1176 memcpy(&body->oa, oa, sizeof(*oa));
1177 obdo_to_ioobj(oa, ioobj);
1178 ioobj->ioo_bufcnt = oa_bufs;
1180 for (i = 0; i < oa_bufs; i++, niobuf++) {
1181 struct brw_page *pg = &pgarr[i];
1183 LASSERT(pg->count > 0);
1184 LASSERT((pg->off & ~PAGE_MASK) + pg->count <= PAGE_SIZE);
1186 ptlrpc_prep_bulk_page(desc, pg->pg, pg->off & ~PAGE_MASK,
1189 niobuf->offset = pg->off;
1190 niobuf->len = pg->count;
1191 niobuf->flags = pg->flag;
1194 /* size[0] still sizeof (*body) */
1195 if (opc == OST_WRITE) {
1196 /* 1 RC per niobuf */
1197 size[1] = sizeof(__u32) * oa_bufs;
1198 req->rq_replen = lustre_msg_size(2, size);
1200 /* 1 RC for the whole I/O */
1201 req->rq_replen = lustre_msg_size(1, size);
1203 err = ptlrpc_queue_wait(req);
1206 ptlrpc_req_finished(req);
1210 static int mdc_valid_attrs(struct obd_export *exp, struct ll_fid *fid)
1212 struct ldlm_res_id res_id = { .name = {0} };
1213 struct obd_device *obd = exp->exp_obd;
1214 struct lustre_handle lockh;
1215 ldlm_policy_data_t policy;
1219 res_id.name[0] = fid->id;
1220 res_id.name[1] = fid->generation;
1221 policy.l_inodebits.bits = MDS_INODELOCK_UPDATE;
1223 CDEBUG(D_INFO, "trying to match res "LPU64"\n", res_id.name[0]);
1225 /* FIXME use LDLM_FL_TEST_LOCK instead */
1226 flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_CBPENDING;
1227 if (ldlm_lock_match(obd->obd_namespace, flags, &res_id,
1228 LDLM_IBITS, &policy, LCK_PR, &lockh)) {
1229 ldlm_lock_decref(&lockh, LCK_PR);
1233 if (ldlm_lock_match(obd->obd_namespace, flags, &res_id,
1234 LDLM_IBITS, &policy, LCK_PW, &lockh)) {
1235 ldlm_lock_decref(&lockh, LCK_PW);
1240 static int mdc_change_cbdata_name(struct obd_export *exp, struct ll_fid *pfid,
1241 char *name, int len, struct ll_fid *cfid,
1242 ldlm_iterator_t it, void *data)
1245 rc = mdc_change_cbdata(exp, cfid, it, data);
1249 struct obd_ops mdc_obd_ops = {
1250 .o_owner = THIS_MODULE,
1251 .o_attach = mdc_attach,
1252 .o_detach = mdc_detach,
1253 .o_setup = mdc_setup,
1254 .o_precleanup = mdc_precleanup,
1255 .o_cleanup = mdc_cleanup,
1256 .o_connect = client_connect_import,
1257 .o_disconnect = client_disconnect_export,
1258 .o_iocontrol = mdc_iocontrol,
1259 .o_set_info = mdc_set_info,
1260 .o_statfs = mdc_statfs,
1262 .o_unpin = mdc_unpin,
1263 .o_import_event = mdc_import_event,
1264 .o_llog_init = mdc_llog_init,
1265 .o_llog_finish = mdc_llog_finish,
1266 .o_create = mdc_obj_create,
1267 .o_set_info = mdc_set_info,
1268 .o_get_info = mdc_get_info,
1270 .o_init_ea_size = mdc_init_ea_size,
1273 struct md_ops mdc_md_ops = {
1274 .m_getstatus = mdc_getstatus,
1275 .m_getattr = mdc_getattr,
1276 .m_change_cbdata = mdc_change_cbdata,
1277 .m_close = mdc_close,
1278 .m_create = mdc_create,
1279 .m_done_writing = mdc_done_writing,
1280 .m_enqueue = mdc_enqueue,
1281 .m_getattr_name = mdc_getattr_name,
1282 .m_intent_lock = mdc_intent_lock,
1284 .m_rename = mdc_rename,
1285 .m_setattr = mdc_setattr,
1287 .m_readpage = mdc_readpage,
1288 .m_unlink = mdc_unlink,
1289 .m_valid_attrs = mdc_valid_attrs,
1290 .m_req2lustre_md = mdc_req2lustre_md,
1291 .m_set_open_replay_data = mdc_set_open_replay_data,
1292 .m_clear_open_replay_data = mdc_clear_open_replay_data,
1293 .m_store_inode_generation = mdc_store_inode_generation,
1294 .m_set_lock_data = mdc_set_lock_data,
1295 .m_get_real_obd = mdc_get_real_obd,
1296 .m_change_cbdata_name = mdc_change_cbdata_name,
1297 .m_change_cbdata = mdc_change_cbdata,
1300 int __init mdc_init(void)
1302 struct lprocfs_static_vars lvars;
1303 lprocfs_init_vars(mdc, &lvars);
1304 return class_register_type(&mdc_obd_ops, &mdc_md_ops, lvars.module_vars,
1309 static void /*__exit*/ mdc_exit(void)
1311 class_unregister_type(LUSTRE_MDC_NAME);
1314 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
1315 MODULE_DESCRIPTION("Lustre Metadata Client");
1316 MODULE_LICENSE("GPL");
1318 EXPORT_SYMBOL(mdc_req2lustre_md);
1319 EXPORT_SYMBOL(mdc_change_cbdata);
1320 EXPORT_SYMBOL(mdc_getstatus);
1321 EXPORT_SYMBOL(mdc_getattr);
1322 EXPORT_SYMBOL(mdc_getattr_name);
1323 EXPORT_SYMBOL(mdc_create);
1324 EXPORT_SYMBOL(mdc_unlink);
1325 EXPORT_SYMBOL(mdc_rename);
1326 EXPORT_SYMBOL(mdc_link);
1327 EXPORT_SYMBOL(mdc_readpage);
1328 EXPORT_SYMBOL(mdc_setattr);
1329 EXPORT_SYMBOL(mdc_close);
1330 EXPORT_SYMBOL(mdc_done_writing);
1331 EXPORT_SYMBOL(mdc_sync);
1332 EXPORT_SYMBOL(mdc_set_open_replay_data);
1333 EXPORT_SYMBOL(mdc_clear_open_replay_data);
1334 EXPORT_SYMBOL(mdc_store_inode_generation);
1336 module_init(mdc_init);
1337 module_exit(mdc_exit);