1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2001-2003 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 void mdc_store_inode_generation(struct ptlrpc_request *req, int reqoff,
221 struct mds_rec_create *rec =
222 lustre_msg_buf(req->rq_reqmsg, reqoff, sizeof(*rec));
223 struct mds_body *body =
224 lustre_msg_buf(req->rq_repmsg, repoff, sizeof(*body));
226 LASSERT (rec != NULL);
227 LASSERT (body != NULL);
229 memcpy(&rec->cr_replayfid, &body->fid1, sizeof rec->cr_replayfid);
230 DEBUG_REQ(D_HA, req, "storing generation %u for ino "LPU64,
231 rec->cr_replayfid.generation, rec->cr_replayfid.id);
234 int mdc_req2lustre_md(struct ptlrpc_request *req, int offset,
235 struct obd_export *exp_osc,
236 struct obd_export *exp_mdc,
237 struct lustre_md *md)
243 memset(md, 0, sizeof(*md));
245 md->body = lustre_msg_buf(req->rq_repmsg, offset, sizeof (*md->body));
246 LASSERT (md->body != NULL);
247 LASSERT_REPSWABBED (req, offset);
249 if (!(md->body->valid & OBD_MD_FLEASIZE))
252 /* ea is presented in reply, parse it */
253 if (S_ISREG(md->body->mode)) {
255 struct lov_mds_md *lmm;
257 if (md->body->eadatasize == 0) {
258 CERROR ("OBD_MD_FLEASIZE set, but eadatasize 0\n");
261 lmmsize = md->body->eadatasize;
262 lmm = lustre_msg_buf(req->rq_repmsg, offset + 1, lmmsize);
263 LASSERT (lmm != NULL);
264 LASSERT_REPSWABBED (req, offset + 1);
266 rc = obd_unpackmd(exp_osc, &md->lsm, lmm, lmmsize);
268 LASSERT (rc >= sizeof (*md->lsm));
271 } else if (S_ISDIR(md->body->mode)) {
274 LASSERT(exp_mdc != NULL);
275 /* dir can be non-splitted */
276 if (md->body->eadatasize == 0)
279 mdsize = md->body->eadatasize;
280 mea = lustre_msg_buf(req->rq_repmsg, offset + 1, mdsize);
281 LASSERT(mea != NULL);
283 rc = obd_unpackmd(exp_mdc, (void *) &md->mea,
284 (void *) mea, mdsize);
286 LASSERT (rc >= sizeof (*md->mea));
295 static void mdc_commit_open(struct ptlrpc_request *req)
297 struct mdc_open_data *mod = req->rq_cb_data;
301 if (mod->mod_close_req != NULL)
302 mod->mod_close_req->rq_cb_data = NULL;
304 if (mod->mod_och != NULL)
305 mod->mod_och->och_mod = NULL;
307 OBD_FREE(mod, sizeof(*mod));
308 req->rq_cb_data = NULL;
311 static void mdc_replay_open(struct ptlrpc_request *req)
313 struct mdc_open_data *mod = req->rq_cb_data;
314 struct obd_client_handle *och;
315 struct ptlrpc_request *close_req;
316 struct lustre_handle old;
317 struct mds_body *body;
320 body = lustre_swab_repbuf(req, 1, sizeof(*body), lustre_swab_mds_body);
321 LASSERT (body != NULL);
324 DEBUG_REQ(D_ERROR, req,
325 "can't properly replay without open data");
332 struct lustre_handle *file_fh;
333 LASSERT(och->och_magic == OBD_CLIENT_HANDLE_MAGIC);
334 file_fh = &och->och_fh;
335 CDEBUG(D_HA, "updating handle from "LPX64" to "LPX64"\n",
336 file_fh->cookie, body->handle.cookie);
337 memcpy(&old, file_fh, sizeof(old));
338 memcpy(file_fh, &body->handle, sizeof(*file_fh));
341 close_req = mod->mod_close_req;
342 if (close_req != NULL) {
343 struct mds_body *close_body;
344 LASSERT(close_req->rq_reqmsg->opc == MDS_CLOSE);
345 close_body = lustre_msg_buf(close_req->rq_reqmsg, 0,
346 sizeof(*close_body));
348 LASSERT(!memcmp(&old, &close_body->handle, sizeof old));
349 DEBUG_REQ(D_HA, close_req, "updating close body with new fh");
350 memcpy(&close_body->handle, &body->handle,
351 sizeof(close_body->handle));
357 void mdc_set_open_replay_data(struct obd_client_handle *och,
358 struct ptlrpc_request *open_req)
360 struct mdc_open_data *mod;
361 struct mds_rec_create *rec =
362 lustre_msg_buf(open_req->rq_reqmsg, 2, sizeof(*rec));
363 struct mds_body *body =
364 lustre_msg_buf(open_req->rq_repmsg, 1, sizeof(*body));
366 LASSERT(rec != NULL);
367 /* outgoing messages always in my byte order */
368 LASSERT(body != NULL);
369 /* incoming message in my byte order (it's been swabbed) */
370 LASSERT_REPSWABBED(open_req, 1);
372 OBD_ALLOC(mod, sizeof(*mod));
374 DEBUG_REQ(D_ERROR, open_req, "can't allocate mdc_open_data");
380 mod->mod_open_req = open_req;
382 memcpy(&rec->cr_replayfid, &body->fid1, sizeof rec->cr_replayfid);
383 open_req->rq_replay_cb = mdc_replay_open;
384 open_req->rq_commit_cb = mdc_commit_open;
385 open_req->rq_cb_data = mod;
386 DEBUG_REQ(D_HA, open_req, "set up replay data");
389 void mdc_clear_open_replay_data(struct obd_client_handle *och)
391 struct mdc_open_data *mod = och->och_mod;
393 /* Don't free the structure now (it happens in mdc_commit_open, after
394 * we're sure we won't need to fix up the close request in the future),
395 * but make sure that replay doesn't poke at the och, which is about to
397 LASSERT(mod != (void *)0x5a5a5a5a);
403 static void mdc_commit_close(struct ptlrpc_request *req)
405 struct mdc_open_data *mod = req->rq_cb_data;
406 struct ptlrpc_request *open_req;
407 struct obd_import *imp = req->rq_import;
409 DEBUG_REQ(D_HA, req, "close req committed");
413 mod->mod_close_req = NULL;
414 req->rq_cb_data = NULL;
415 req->rq_commit_cb = NULL;
417 open_req = mod->mod_open_req;
418 LASSERT(open_req != NULL);
419 LASSERT(open_req != (void *)0x5a5a5a5a);
421 DEBUG_REQ(D_HA, open_req, "open req balanced");
422 LASSERT(open_req->rq_transno != 0);
423 LASSERT(open_req->rq_import == imp);
425 /* We no longer want to preserve this for transno-unconditional
427 spin_lock(&open_req->rq_lock);
428 open_req->rq_replay = 0;
429 spin_unlock(&open_req->rq_lock);
432 static int mdc_close_interpret(struct ptlrpc_request *req, void *data, int rc)
434 union ptlrpc_async_args *aa = data;
435 struct mdc_rpc_lock *rpc_lock = aa->pointer_arg[0];
436 struct obd_device *obd = aa->pointer_arg[1];
438 if (rpc_lock == NULL) {
439 CERROR("called with NULL rpc_lock\n");
441 mdc_put_rpc_lock(rpc_lock, NULL);
442 LASSERTF(req->rq_async_args.pointer_arg[0] ==
443 obd->u.cli.cl_rpc_lock, "%p != %p\n",
444 req->rq_async_args.pointer_arg[0],
445 obd->u.cli.cl_rpc_lock);
446 aa->pointer_arg[0] = NULL;
448 wake_up(&req->rq_reply_waitq);
452 /* We can't use ptlrpc_check_reply, because we don't want to wake up for
453 * anything but a reply or an error. */
454 static int mdc_close_check_reply(struct ptlrpc_request *req)
459 spin_lock_irqsave(&req->rq_lock, flags);
460 if (PTLRPC_REQUEST_COMPLETE(req)) {
463 spin_unlock_irqrestore (&req->rq_lock, flags);
467 static int go_back_to_sleep(void *unused)
472 int mdc_close(struct obd_export *exp, struct obdo *obdo,
473 struct obd_client_handle *och, struct ptlrpc_request **request)
475 struct mds_body *body;
476 struct obd_device *obd = class_exp2obd(exp);
477 int reqsize = sizeof(*body);
478 int rc, repsize[3] = {sizeof(*body),
479 obd->u.cli.cl_max_mds_easize,
480 obd->u.cli.cl_max_mds_cookiesize};
481 struct ptlrpc_request *req;
482 struct mdc_open_data *mod;
483 struct l_wait_info lwi;
486 req = ptlrpc_prep_req(class_exp2cliimp(exp), MDS_CLOSE, 1, &reqsize,
489 GOTO(out, rc = -ENOMEM);
491 /* Ensure that this close's handle is fixed up during replay. */
492 LASSERT(och != NULL);
494 if (likely(mod != NULL)) {
495 mod->mod_close_req = req;
496 DEBUG_REQ(D_HA, mod->mod_open_req, "matched open req %p",
499 CDEBUG(D_HA, "couldn't find open req; expecting close error\n");
502 body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof(*body));
503 mdc_pack_fid(&body->fid1, obdo->o_id, 0, obdo->o_mode);
504 memcpy(&body->handle, &och->och_fh, sizeof(body->handle));
505 body->size = obdo->o_size;
506 body->blocks = obdo->o_blocks;
507 body->flags = obdo->o_flags;
508 body->valid = obdo->o_valid;
510 req->rq_replen = lustre_msg_size(3, repsize);
511 req->rq_commit_cb = mdc_commit_close;
512 LASSERT(req->rq_cb_data == NULL);
513 req->rq_cb_data = mod;
515 /* We hand a ref to the rpcd here, so we need another one of our own. */
516 ptlrpc_request_addref(req);
518 mdc_get_rpc_lock(obd->u.cli.cl_rpc_lock, NULL);
519 req->rq_interpret_reply = mdc_close_interpret;
520 req->rq_async_args.pointer_arg[0] = obd->u.cli.cl_rpc_lock;
521 req->rq_async_args.pointer_arg[1] = obd;
522 ptlrpcd_add_req(req);
523 lwi = LWI_TIMEOUT_INTR(MAX(req->rq_timeout * HZ, 1), go_back_to_sleep,
525 rc = l_wait_event(req->rq_reply_waitq, mdc_close_check_reply(req),
528 rc = req->rq_repmsg->status;
529 if (req->rq_repmsg->type == PTL_RPC_MSG_ERR) {
530 DEBUG_REQ(D_ERROR, req, "type == PTL_RPC_MSG_ERR, err "
534 } else if (mod == NULL) {
535 CERROR("Unexpected: can't find mdc_open_data, but the "
536 "close succeeded. Please tell CFS.\n");
538 if (!lustre_swab_repbuf(req, 0, sizeof(struct mds_body),
539 lustre_swab_mds_body)) {
540 CERROR("Error unpacking mds_body\n");
544 if (req->rq_async_args.pointer_arg[0] != NULL) {
545 CERROR("returned without dropping rpc_lock: rc %d\n", rc);
546 mdc_close_interpret(req, &req->rq_async_args, rc);
547 portals_debug_dumplog();
556 int mdc_done_writing(struct obd_export *exp, struct obdo *obdo)
558 struct ptlrpc_request *req;
559 struct mds_body *body;
560 int rc, size = sizeof(*body);
563 req = ptlrpc_prep_req(class_exp2cliimp(exp), MDS_DONE_WRITING, 1,
568 body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof(*body));
569 mdc_pack_fid(&body->fid1, obdo->o_id, 0, obdo->o_mode);
570 body->size = obdo->o_size;
571 body->blocks = obdo->o_blocks;
572 body->flags = obdo->o_flags;
573 body->valid = obdo->o_valid;
574 // memcpy(&body->handle, &och->och_fh, sizeof(body->handle));
576 req->rq_replen = lustre_msg_size(1, &size);
578 rc = ptlrpc_queue_wait(req);
579 ptlrpc_req_finished(req);
583 int mdc_readpage(struct obd_export *exp, struct ll_fid *mdc_fid,
584 __u64 offset, struct page *page,
585 struct ptlrpc_request **request)
587 struct obd_import *imp = class_exp2cliimp(exp);
588 struct ptlrpc_request *req = NULL;
589 struct ptlrpc_bulk_desc *desc = NULL;
590 struct mds_body *body;
591 int rc, size = sizeof(*body);
594 CDEBUG(D_INODE, "inode: %ld\n", (long)mdc_fid->id);
596 req = ptlrpc_prep_req(imp, MDS_READPAGE, 1, &size, NULL);
598 GOTO(out, rc = -ENOMEM);
599 /* XXX FIXME bug 249 */
600 req->rq_request_portal = MDS_READPAGE_PORTAL;
602 desc = ptlrpc_prep_bulk_imp(req, 1, BULK_PUT_SINK, MDS_BULK_PORTAL);
604 GOTO(out, rc = -ENOMEM);
605 /* NB req now owns desc and will free it when it gets freed */
607 ptlrpc_prep_bulk_page(desc, page, 0, PAGE_CACHE_SIZE);
609 mdc_readdir_pack(req, offset, PAGE_CACHE_SIZE, mdc_fid);
611 req->rq_replen = lustre_msg_size(1, &size);
612 rc = ptlrpc_queue_wait(req);
615 body = lustre_swab_repbuf(req, 0, sizeof (*body),
616 lustre_swab_mds_body);
618 CERROR("Can't unpack mds_body\n");
619 GOTO(out, rc = -EPROTO);
622 if (req->rq_bulk->bd_nob_transferred != PAGE_CACHE_SIZE) {
623 CERROR ("Unexpected # bytes transferred: %d"
625 req->rq_bulk->bd_nob_transferred,
627 GOTO (out, rc = -EPROTO);
637 static int mdc_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
638 void *karg, void *uarg)
640 struct obd_device *obd = exp->exp_obd;
641 struct obd_ioctl_data *data = karg;
642 struct obd_import *imp = obd->u.cli.cl_import;
643 struct llog_ctxt *ctxt;
650 case OBD_IOC_CLIENT_RECOVER:
651 rc = ptlrpc_recover_import(imp, data->ioc_inlbuf1);
655 case IOC_OSC_SET_ACTIVE:
656 rc = ptlrpc_set_import_active(imp, data->ioc_offset);
658 case OBD_IOC_PARSE: {
659 ctxt = llog_get_context(&exp->exp_obd->obd_llogs,
660 LLOG_CONFIG_REPL_CTXT);
661 rc = class_config_parse_llog(ctxt, data->ioc_inlbuf1, NULL);
665 case OBD_IOC_LLOG_INFO:
666 case OBD_IOC_LLOG_PRINT: {
667 ctxt = llog_get_context(&obd->obd_llogs, LLOG_CONFIG_REPL_CTXT);
668 rc = llog_ioctl(ctxt, cmd, data);
674 CERROR("osc_ioctl(): unrecognised ioctl %#x\n", cmd);
675 GOTO(out, rc = -ENOTTY);
682 int mdc_set_info(struct obd_export *exp, obd_count keylen,
683 void *key, obd_count vallen, void *val)
687 if (keylen == strlen("initial_recov") &&
688 memcmp(key, "initial_recov", strlen("initial_recov")) == 0) {
689 struct obd_import *imp = exp->exp_obd->u.cli.cl_import;
690 if (vallen != sizeof(int))
692 imp->imp_initial_recov = *(int *)val;
693 CDEBUG(D_HA, "%s: set imp_no_init_recov = %d\n",
694 exp->exp_obd->obd_name,
695 imp->imp_initial_recov);
697 } else if (keylen >= strlen("client") && strcmp(key, "client") == 0) {
698 struct ptlrpc_request *req;
699 char *bufs[1] = {key};
701 req = ptlrpc_prep_req(class_exp2cliimp(exp), OST_SET_INFO, 1,
706 req->rq_replen = lustre_msg_size(0, NULL);
707 rc = ptlrpc_queue_wait(req);
708 ptlrpc_req_finished(req);
715 static int mdc_statfs(struct obd_device *obd, struct obd_statfs *osfs,
716 unsigned long max_age)
718 struct ptlrpc_request *req;
719 struct obd_statfs *msfs;
720 int rc, size = sizeof(*msfs);
723 /* We could possibly pass max_age in the request (as an absolute
724 * timestamp or a "seconds.usec ago") so the target can avoid doing
725 * extra calls into the filesystem if that isn't necessary (e.g.
726 * during mount that would help a bit). Having relative timestamps
727 * is not so great if request processing is slow, while absolute
728 * timestamps are not ideal because they need time synchronization. */
729 req = ptlrpc_prep_req(obd->u.cli.cl_import, MDS_STATFS, 0, NULL, NULL);
733 req->rq_replen = lustre_msg_size(1, &size);
735 mdc_get_rpc_lock(obd->u.cli.cl_rpc_lock, NULL);
736 rc = ptlrpc_queue_wait(req);
737 mdc_put_rpc_lock(obd->u.cli.cl_rpc_lock, NULL);
742 msfs = lustre_swab_repbuf(req, 0, sizeof(*msfs),lustre_swab_obd_statfs);
744 CERROR("Can't unpack obd_statfs\n");
745 GOTO(out, rc = -EPROTO);
748 memcpy(osfs, msfs, sizeof (*msfs));
751 ptlrpc_req_finished(req);
756 static int mdc_pin(struct obd_export *exp, obd_id ino, __u32 gen, int type,
757 struct obd_client_handle *handle, int flag)
759 struct ptlrpc_request *req;
760 struct mds_body *body;
761 int rc, size = sizeof(*body);
764 req = ptlrpc_prep_req(class_exp2cliimp(exp), MDS_PIN, 1, &size, NULL);
768 body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*body));
769 mdc_pack_fid(&body->fid1, ino, gen, type);
772 req->rq_replen = lustre_msg_size(1, &size);
774 mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
775 rc = ptlrpc_queue_wait(req);
776 mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
778 CERROR("pin failed: %d\n", rc);
779 ptlrpc_req_finished(req);
783 body = lustre_swab_repbuf(req, 0, sizeof(*body), lustre_swab_mds_body);
785 ptlrpc_req_finished(req);
789 memcpy(&handle->och_fh, &body->handle, sizeof(body->handle));
790 handle->och_magic = OBD_CLIENT_HANDLE_MAGIC;
792 OBD_ALLOC(handle->och_mod, sizeof(*handle->och_mod));
793 if (handle->och_mod == NULL) {
794 DEBUG_REQ(D_ERROR, req, "can't allocate mdc_open_data");
797 handle->och_mod->mod_open_req = req; /* will be dropped by unpin */
802 static int mdc_unpin(struct obd_export *exp,
803 struct obd_client_handle *handle, int flag)
805 struct ptlrpc_request *req;
806 struct mds_body *body;
807 int rc, size = sizeof(*body);
810 if (handle->och_magic != OBD_CLIENT_HANDLE_MAGIC)
813 req = ptlrpc_prep_req(class_exp2cliimp(exp), MDS_CLOSE, 1, &size, NULL);
817 body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof(*body));
818 memcpy(&body->handle, &handle->och_fh, sizeof(body->handle));
821 req->rq_replen = lustre_msg_size(0, NULL);
822 mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
823 rc = ptlrpc_queue_wait(req);
824 mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
827 CERROR("unpin failed: %d\n", rc);
829 ptlrpc_req_finished(req);
830 ptlrpc_req_finished(handle->och_mod->mod_open_req);
831 OBD_FREE(handle->och_mod, sizeof(*handle->och_mod));
835 int mdc_sync(struct obd_export *exp, struct ll_fid *fid,
836 struct ptlrpc_request **request)
838 struct ptlrpc_request *req;
839 struct mds_body *body;
840 int size = sizeof(*body);
844 req = ptlrpc_prep_req(class_exp2cliimp(exp), MDS_SYNC, 1,&size,NULL);
846 RETURN(rc = -ENOMEM);
849 body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*body));
850 memcpy(&body->fid1, fid, sizeof(*fid));
851 mdc_pack_req_body(req);
854 req->rq_replen = lustre_msg_size(1, &size);
856 rc = ptlrpc_queue_wait(req);
857 if (rc || request == NULL)
858 ptlrpc_req_finished(req);
865 static int mdc_attach(struct obd_device *dev, obd_count len, void *data)
867 struct lprocfs_static_vars lvars;
869 lprocfs_init_vars(mdc, &lvars);
870 return lprocfs_obd_attach(dev, lvars.obd_vars);
873 static int mdc_detach(struct obd_device *dev)
875 return lprocfs_obd_detach(dev);
878 static int mdc_import_event(struct obd_device *obd,
879 struct obd_import *imp,
880 enum obd_import_event event)
884 LASSERT(imp->imp_obd == obd);
887 case IMP_EVENT_DISCON: {
890 case IMP_EVENT_INACTIVE: {
891 if (obd->obd_observer)
892 rc = obd_notify(obd->obd_observer, obd, 0);
895 case IMP_EVENT_INVALIDATE: {
896 struct ldlm_namespace *ns = obd->obd_namespace;
898 ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
902 case IMP_EVENT_ACTIVE: {
903 if (obd->obd_observer)
904 rc = obd_notify(obd->obd_observer, obd, 1);
908 CERROR("Unknown import event %d\n", event);
914 static int mdc_setup(struct obd_device *obd, obd_count len, void *buf)
916 struct client_obd *cli = &obd->u.cli;
920 OBD_ALLOC(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
921 if (!cli->cl_rpc_lock)
923 mdc_init_rpc_lock(cli->cl_rpc_lock);
927 OBD_ALLOC(cli->cl_setattr_lock, sizeof (*cli->cl_setattr_lock));
928 if (!cli->cl_setattr_lock)
929 GOTO(out_free_rpc, rc = -ENOMEM);
930 mdc_init_rpc_lock(cli->cl_setattr_lock);
932 rc = client_obd_setup(obd, len, buf);
934 OBD_FREE(cli->cl_setattr_lock, sizeof (*cli->cl_setattr_lock));
936 OBD_FREE(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
939 rc = obd_llog_init(obd, &obd->obd_llogs, obd, 0, NULL);
942 CERROR("failed to setup llogging subsystems\n");
948 int mdc_init_ea_size(struct obd_export *exp, int easize, int cookiesize)
950 struct obd_device *obd = exp->exp_obd;
951 struct client_obd *cli = &obd->u.cli;
954 if (cli->cl_max_mds_easize < easize)
955 cli->cl_max_mds_easize = easize;
956 if (cli->cl_max_mds_cookiesize < cookiesize)
957 cli->cl_max_mds_cookiesize = cookiesize;
961 static int mdc_precleanup(struct obd_device *obd, int flags)
965 rc = obd_llog_finish(obd, &obd->obd_llogs, 0);
967 CERROR("failed to cleanup llogging subsystems\n");
972 static int mdc_cleanup(struct obd_device *obd, int flags)
974 struct client_obd *cli = &obd->u.cli;
976 OBD_FREE(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
977 OBD_FREE(cli->cl_setattr_lock, sizeof (*cli->cl_setattr_lock));
981 return client_obd_cleanup(obd, flags);
985 static int mdc_llog_init(struct obd_device *obd, struct obd_llogs *llogs,
986 struct obd_device *tgt, int count,
987 struct llog_catid *logid)
989 struct llog_ctxt *ctxt;
993 rc = llog_setup(obd, llogs, LLOG_CONFIG_REPL_CTXT, tgt, 0, NULL,
996 ctxt = llog_get_context(llogs, LLOG_CONFIG_REPL_CTXT);
997 ctxt->loc_imp = obd->u.cli.cl_import;
1003 static int mdc_llog_finish(struct obd_device *obd,
1004 struct obd_llogs *llogs, int count)
1009 rc = llog_cleanup(llog_get_context(llogs, LLOG_CONFIG_REPL_CTXT));
1013 int mdc_obj_create(struct obd_export *exp, struct obdo *oa,
1014 struct lov_stripe_md **ea, struct obd_trans_info *oti)
1016 struct ptlrpc_request *request;
1017 struct ost_body *body;
1018 int rc, size = sizeof(*body);
1023 request = ptlrpc_prep_req(class_exp2cliimp(exp), OST_CREATE,
1026 GOTO(out_req, rc = -ENOMEM);
1028 body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof (*body));
1029 memcpy(&body->oa, oa, sizeof(body->oa));
1031 request->rq_replen = lustre_msg_size(1, &size);
1032 rc = ptlrpc_queue_wait(request);
1036 body = lustre_swab_repbuf(request, 0, sizeof(*body),
1037 lustre_swab_ost_body);
1039 CERROR ("can't unpack ost_body\n");
1040 GOTO (out_req, rc = -EPROTO);
1043 memcpy(oa, &body->oa, sizeof(*oa));
1045 /* store ino/generation for recovery */
1046 body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof (*body));
1047 body->oa.o_id = oa->o_id;
1048 body->oa.o_generation = oa->o_generation;
1050 CDEBUG(D_HA, "transno: "LPD64"\n", request->rq_repmsg->transno);
1053 ptlrpc_req_finished(request);
1057 static int mdc_get_info(struct obd_export *exp, obd_count keylen,
1058 void *key, __u32 *vallen, void *val)
1061 if (!vallen || !val)
1064 if (keylen >= strlen("mdsize") && strcmp(key, "mdsize") == 0) {
1065 struct ptlrpc_request *req;
1067 char *bufs[1] = {key};
1069 req = ptlrpc_prep_req(class_exp2cliimp(exp), OST_GET_INFO, 1,
1074 req->rq_replen = lustre_msg_size(1, vallen);
1075 rc = ptlrpc_queue_wait(req);
1079 /* FIXME: right swap routine here! */
1080 reply = lustre_swab_repbuf(req, 0, sizeof(*reply), NULL);
1081 if (reply == NULL) {
1082 CERROR("Can't unpack mdsize\n");
1083 GOTO(out, rc = -EPROTO);
1085 *((__u32 *)val) = *reply;
1087 ptlrpc_req_finished(req);
1093 int mdc_brw(int rw, struct obd_export *exp, struct obdo *oa,
1094 struct lov_stripe_md *ea, obd_count oa_bufs,
1095 struct brw_page *pgarr, struct obd_trans_info *oti)
1097 struct ptlrpc_bulk_desc *desc;
1098 struct niobuf_remote *niobuf;
1099 struct ptlrpc_request *req;
1100 struct obd_ioobj *ioobj;
1101 struct ost_body *body;
1105 opc = ((rw & OBD_BRW_WRITE) != 0) ? OST_WRITE : OST_READ;
1107 size[0] = sizeof(*body);
1108 size[1] = sizeof(*ioobj);
1109 size[2] = oa_bufs * sizeof(*niobuf);
1111 req = ptlrpc_prep_req(class_exp2cliimp(exp), opc, 3, size, NULL);
1112 LASSERT(req != NULL);
1114 if (opc == OST_WRITE)
1115 desc = ptlrpc_prep_bulk_imp(req, oa_bufs, BULK_GET_SOURCE,
1118 desc = ptlrpc_prep_bulk_imp(req, oa_bufs, BULK_PUT_SINK,
1120 LASSERT(desc != NULL);
1122 body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof(*body));
1123 ioobj = lustre_msg_buf(req->rq_reqmsg, 1, sizeof(*ioobj));
1124 niobuf = lustre_msg_buf(req->rq_reqmsg, 2, oa_bufs * sizeof(*niobuf));
1126 memcpy(&body->oa, oa, sizeof(*oa));
1127 obdo_to_ioobj(oa, ioobj);
1128 ioobj->ioo_bufcnt = oa_bufs;
1130 for (i = 0; i < oa_bufs; i++, niobuf++) {
1131 struct brw_page *pg = &pgarr[i];
1133 LASSERT(pg->count > 0);
1134 LASSERT((pg->off & ~PAGE_MASK) + pg->count <= PAGE_SIZE);
1136 ptlrpc_prep_bulk_page(desc, pg->pg, pg->off & ~PAGE_MASK,
1139 niobuf->offset = pg->off;
1140 niobuf->len = pg->count;
1141 niobuf->flags = pg->flag;
1144 /* size[0] still sizeof (*body) */
1145 if (opc == OST_WRITE) {
1146 /* 1 RC per niobuf */
1147 size[1] = sizeof(__u32) * oa_bufs;
1148 req->rq_replen = lustre_msg_size(2, size);
1150 /* 1 RC for the whole I/O */
1151 req->rq_replen = lustre_msg_size(1, size);
1153 err = ptlrpc_queue_wait(req);
1156 ptlrpc_req_finished(req);
1160 static int mdc_valid_attrs(struct obd_export *exp, struct ll_fid *fid)
1162 struct ldlm_res_id res_id = { .name = {0} };
1163 struct obd_device *obd = exp->exp_obd;
1164 struct lustre_handle lockh;
1165 ldlm_policy_data_t policy;
1169 res_id.name[0] = fid->id;
1170 res_id.name[1] = fid->generation;
1171 policy.l_inodebits.bits = MDS_INODELOCK_UPDATE;
1173 CDEBUG(D_INFO, "trying to match res "LPU64"\n", res_id.name[0]);
1175 /* FIXME use LDLM_FL_TEST_LOCK instead */
1176 flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_CBPENDING;
1177 if (ldlm_lock_match(obd->obd_namespace, flags, &res_id,
1178 LDLM_IBITS, &policy, LCK_PR, &lockh)) {
1179 ldlm_lock_decref(&lockh, LCK_PR);
1183 if (ldlm_lock_match(obd->obd_namespace, flags, &res_id,
1184 LDLM_IBITS, &policy, LCK_PW, &lockh)) {
1185 ldlm_lock_decref(&lockh, LCK_PW);
1191 struct obd_ops mdc_obd_ops = {
1192 o_owner: THIS_MODULE,
1193 o_attach: mdc_attach,
1194 o_detach: mdc_detach,
1196 o_precleanup: mdc_precleanup,
1197 o_cleanup: mdc_cleanup,
1198 o_connect: client_connect_import,
1199 o_disconnect: client_disconnect_export,
1200 o_iocontrol: mdc_iocontrol,
1201 o_set_info: mdc_set_info,
1202 o_statfs: mdc_statfs,
1205 o_llog_init: mdc_llog_init,
1206 o_llog_finish: mdc_llog_finish,
1207 o_create: mdc_obj_create,
1208 o_get_info: mdc_get_info,
1210 o_init_ea_size: mdc_init_ea_size,
1213 struct md_ops mdc_md_ops = {
1214 m_getstatus: mdc_getstatus,
1215 m_getattr: mdc_getattr,
1216 m_change_cbdata: mdc_change_cbdata,
1218 m_create: mdc_create,
1219 m_done_writing: mdc_done_writing,
1220 m_enqueue: mdc_enqueue,
1221 m_getattr_name: mdc_getattr_name,
1222 m_intent_lock: mdc_intent_lock,
1224 m_rename: mdc_rename,
1225 m_setattr: mdc_setattr,
1227 m_readpage: mdc_readpage,
1228 m_unlink: mdc_unlink,
1229 m_valid_attrs: mdc_valid_attrs,
1232 int __init mdc_init(void)
1234 struct lprocfs_static_vars lvars;
1235 lprocfs_init_vars(mdc, &lvars);
1236 return class_register_type(&mdc_obd_ops, &mdc_md_ops, lvars.module_vars,
1240 static void /*__exit*/ mdc_exit(void)
1242 class_unregister_type(LUSTRE_MDC_NAME);
1246 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
1247 MODULE_DESCRIPTION("Lustre Metadata Client");
1248 MODULE_LICENSE("GPL");
1250 EXPORT_SYMBOL(mdc_req2lustre_md);
1251 EXPORT_SYMBOL(mdc_change_cbdata);
1252 EXPORT_SYMBOL(mdc_getstatus);
1253 EXPORT_SYMBOL(mdc_getattr);
1254 EXPORT_SYMBOL(mdc_getattr_name);
1255 EXPORT_SYMBOL(mdc_create);
1256 EXPORT_SYMBOL(mdc_unlink);
1257 EXPORT_SYMBOL(mdc_rename);
1258 EXPORT_SYMBOL(mdc_link);
1259 EXPORT_SYMBOL(mdc_readpage);
1260 EXPORT_SYMBOL(mdc_setattr);
1261 EXPORT_SYMBOL(mdc_close);
1262 EXPORT_SYMBOL(mdc_done_writing);
1263 EXPORT_SYMBOL(mdc_sync);
1264 EXPORT_SYMBOL(mdc_set_open_replay_data);
1265 EXPORT_SYMBOL(mdc_clear_open_replay_data);
1266 EXPORT_SYMBOL(mdc_store_inode_generation);
1268 module_init(mdc_init);
1269 module_exit(mdc_exit);