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 LASSERT (strnlen (filename, namelen) == namelen - 1);
200 memcpy(lustre_msg_buf(req->rq_reqmsg, 1, namelen), filename, namelen);
202 rc = mdc_getattr_common(exp, ea_size, req);
204 ptlrpc_req_finished (req);
212 /* This should be called with both the request and the reply still packed. */
213 void mdc_store_inode_generation(struct ptlrpc_request *req, int reqoff,
216 struct mds_rec_create *rec =
217 lustre_msg_buf(req->rq_reqmsg, reqoff, sizeof(*rec));
218 struct mds_body *body =
219 lustre_msg_buf(req->rq_repmsg, repoff, sizeof(*body));
221 LASSERT (rec != NULL);
222 LASSERT (body != NULL);
224 memcpy(&rec->cr_replayfid, &body->fid1, sizeof rec->cr_replayfid);
225 DEBUG_REQ(D_HA, req, "storing generation %u for ino "LPU64,
226 rec->cr_replayfid.generation, rec->cr_replayfid.id);
229 int mdc_req2lustre_md(struct ptlrpc_request *req, int offset,
230 struct obd_export *exp_osc,
231 struct obd_export *exp_mdc,
232 struct lustre_md *md)
238 memset(md, 0, sizeof(*md));
240 md->body = lustre_msg_buf(req->rq_repmsg, offset, sizeof (*md->body));
241 LASSERT (md->body != NULL);
242 LASSERT_REPSWABBED (req, offset);
244 if (!(md->body->valid & OBD_MD_FLEASIZE))
247 /* ea is presented in reply, parse it */
248 if (S_ISREG(md->body->mode)) {
250 struct lov_mds_md *lmm;
252 if (md->body->eadatasize == 0) {
253 CERROR ("OBD_MD_FLEASIZE set, but eadatasize 0\n");
256 lmmsize = md->body->eadatasize;
257 lmm = lustre_msg_buf(req->rq_repmsg, offset + 1, lmmsize);
258 LASSERT (lmm != NULL);
259 LASSERT_REPSWABBED (req, offset + 1);
261 rc = obd_unpackmd(exp_osc, &md->lsm, lmm, lmmsize);
263 LASSERT (rc >= sizeof (*md->lsm));
266 } else if (S_ISDIR(md->body->mode)) {
269 LASSERT(exp_mdc != NULL);
270 /* dir can be non-splitted */
271 if (md->body->eadatasize == 0)
274 mdsize = md->body->eadatasize;
275 mea = lustre_msg_buf(req->rq_repmsg, offset + 1, mdsize);
276 LASSERT(mea != NULL);
278 rc = obd_unpackmd(exp_mdc, (void *) &md->mea,
279 (void *) mea, mdsize);
281 LASSERT (rc >= sizeof (*md->mea));
290 static void mdc_commit_open(struct ptlrpc_request *req)
292 struct mdc_open_data *mod = req->rq_cb_data;
296 if (mod->mod_close_req != NULL)
297 mod->mod_close_req->rq_cb_data = NULL;
299 if (mod->mod_och != NULL)
300 mod->mod_och->och_mod = NULL;
302 OBD_FREE(mod, sizeof(*mod));
303 req->rq_cb_data = NULL;
306 static void mdc_replay_open(struct ptlrpc_request *req)
308 struct mdc_open_data *mod = req->rq_cb_data;
309 struct obd_client_handle *och;
310 struct ptlrpc_request *close_req;
311 struct lustre_handle old;
312 struct mds_body *body;
315 body = lustre_swab_repbuf(req, 1, sizeof(*body), lustre_swab_mds_body);
316 LASSERT (body != NULL);
319 DEBUG_REQ(D_ERROR, req,
320 "can't properly replay without open data");
327 struct lustre_handle *file_fh;
328 LASSERT(och->och_magic == OBD_CLIENT_HANDLE_MAGIC);
329 file_fh = &och->och_fh;
330 CDEBUG(D_HA, "updating handle from "LPX64" to "LPX64"\n",
331 file_fh->cookie, body->handle.cookie);
332 memcpy(&old, file_fh, sizeof(old));
333 memcpy(file_fh, &body->handle, sizeof(*file_fh));
336 close_req = mod->mod_close_req;
337 if (close_req != NULL) {
338 struct mds_body *close_body;
339 LASSERT(close_req->rq_reqmsg->opc == MDS_CLOSE);
340 close_body = lustre_msg_buf(close_req->rq_reqmsg, 0,
341 sizeof(*close_body));
343 LASSERT(!memcmp(&old, &close_body->handle, sizeof old));
344 DEBUG_REQ(D_HA, close_req, "updating close body with new fh");
345 memcpy(&close_body->handle, &body->handle,
346 sizeof(close_body->handle));
352 void mdc_set_open_replay_data(struct obd_client_handle *och,
353 struct ptlrpc_request *open_req)
355 struct mdc_open_data *mod;
356 struct mds_rec_create *rec =
357 lustre_msg_buf(open_req->rq_reqmsg, 2, sizeof(*rec));
358 struct mds_body *body =
359 lustre_msg_buf(open_req->rq_repmsg, 1, sizeof(*body));
361 LASSERT(rec != NULL);
362 /* outgoing messages always in my byte order */
363 LASSERT(body != NULL);
364 /* incoming message in my byte order (it's been swabbed) */
365 LASSERT_REPSWABBED(open_req, 1);
367 OBD_ALLOC(mod, sizeof(*mod));
369 DEBUG_REQ(D_ERROR, open_req, "can't allocate mdc_open_data");
375 mod->mod_open_req = open_req;
377 memcpy(&rec->cr_replayfid, &body->fid1, sizeof rec->cr_replayfid);
378 open_req->rq_replay_cb = mdc_replay_open;
379 open_req->rq_commit_cb = mdc_commit_open;
380 open_req->rq_cb_data = mod;
381 DEBUG_REQ(D_HA, open_req, "set up replay data");
384 void mdc_clear_open_replay_data(struct obd_client_handle *och)
386 struct mdc_open_data *mod = och->och_mod;
388 /* Don't free the structure now (it happens in mdc_commit_open, after
389 * we're sure we won't need to fix up the close request in the future),
390 * but make sure that replay doesn't poke at the och, which is about to
392 LASSERT(mod != (void *)0x5a5a5a5a);
398 static void mdc_commit_close(struct ptlrpc_request *req)
400 struct mdc_open_data *mod = req->rq_cb_data;
401 struct ptlrpc_request *open_req;
402 struct obd_import *imp = req->rq_import;
404 DEBUG_REQ(D_HA, req, "close req committed");
408 mod->mod_close_req = NULL;
409 req->rq_cb_data = NULL;
410 req->rq_commit_cb = NULL;
412 open_req = mod->mod_open_req;
413 LASSERT(open_req != NULL);
414 LASSERT(open_req != (void *)0x5a5a5a5a);
416 DEBUG_REQ(D_HA, open_req, "open req balanced");
417 LASSERT(open_req->rq_transno != 0);
418 LASSERT(open_req->rq_import == imp);
420 /* We no longer want to preserve this for transno-unconditional
422 spin_lock(&open_req->rq_lock);
423 open_req->rq_replay = 0;
424 spin_unlock(&open_req->rq_lock);
427 static int mdc_close_interpret(struct ptlrpc_request *req, void *data, int rc)
429 union ptlrpc_async_args *aa = data;
430 struct mdc_rpc_lock *rpc_lock = aa->pointer_arg[0];
431 struct obd_device *obd = aa->pointer_arg[1];
433 if (rpc_lock == NULL) {
434 CERROR("called with NULL rpc_lock\n");
436 mdc_put_rpc_lock(rpc_lock, NULL);
437 LASSERTF(req->rq_async_args.pointer_arg[0] ==
438 obd->u.cli.cl_rpc_lock, "%p != %p\n",
439 req->rq_async_args.pointer_arg[0],
440 obd->u.cli.cl_rpc_lock);
441 aa->pointer_arg[0] = NULL;
443 wake_up(&req->rq_reply_waitq);
447 /* We can't use ptlrpc_check_reply, because we don't want to wake up for
448 * anything but a reply or an error. */
449 static int mdc_close_check_reply(struct ptlrpc_request *req)
454 spin_lock_irqsave(&req->rq_lock, flags);
455 if (PTLRPC_REQUEST_COMPLETE(req)) {
458 spin_unlock_irqrestore (&req->rq_lock, flags);
462 static int go_back_to_sleep(void *unused)
467 int mdc_close(struct obd_export *exp, struct obdo *obdo,
468 struct obd_client_handle *och, struct ptlrpc_request **request)
470 struct mds_body *body;
471 struct obd_device *obd = class_exp2obd(exp);
472 int reqsize = sizeof(*body);
473 int rc, repsize[3] = {sizeof(*body),
474 obd->u.cli.cl_max_mds_easize,
475 obd->u.cli.cl_max_mds_cookiesize};
476 struct ptlrpc_request *req;
477 struct mdc_open_data *mod;
478 struct l_wait_info lwi;
481 req = ptlrpc_prep_req(class_exp2cliimp(exp), MDS_CLOSE, 1, &reqsize,
484 GOTO(out, rc = -ENOMEM);
486 /* Ensure that this close's handle is fixed up during replay. */
487 LASSERT(och != NULL);
489 if (likely(mod != NULL)) {
490 mod->mod_close_req = req;
491 DEBUG_REQ(D_HA, mod->mod_open_req, "matched open req %p",
494 CDEBUG(D_HA, "couldn't find open req; expecting close error\n");
497 body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof(*body));
498 mdc_pack_fid(&body->fid1, obdo->o_id, 0, obdo->o_mode);
499 memcpy(&body->handle, &och->och_fh, sizeof(body->handle));
500 body->size = obdo->o_size;
501 body->blocks = obdo->o_blocks;
502 body->flags = obdo->o_flags;
503 body->valid = obdo->o_valid;
505 req->rq_replen = lustre_msg_size(3, repsize);
506 req->rq_commit_cb = mdc_commit_close;
507 LASSERT(req->rq_cb_data == NULL);
508 req->rq_cb_data = mod;
510 /* We hand a ref to the rpcd here, so we need another one of our own. */
511 ptlrpc_request_addref(req);
513 mdc_get_rpc_lock(obd->u.cli.cl_rpc_lock, NULL);
514 req->rq_interpret_reply = mdc_close_interpret;
515 req->rq_async_args.pointer_arg[0] = obd->u.cli.cl_rpc_lock;
516 req->rq_async_args.pointer_arg[1] = obd;
517 ptlrpcd_add_req(req);
518 lwi = LWI_TIMEOUT_INTR(MAX(req->rq_timeout * HZ, 1), go_back_to_sleep,
520 rc = l_wait_event(req->rq_reply_waitq, mdc_close_check_reply(req),
523 rc = req->rq_repmsg->status;
524 if (req->rq_repmsg->type == PTL_RPC_MSG_ERR) {
525 DEBUG_REQ(D_ERROR, req, "type == PTL_RPC_MSG_ERR, err "
529 } else if (mod == NULL) {
530 CERROR("Unexpected: can't find mdc_open_data, but the "
531 "close succeeded. Please tell CFS.\n");
533 if (!lustre_swab_repbuf(req, 0, sizeof(struct mds_body),
534 lustre_swab_mds_body)) {
535 CERROR("Error unpacking mds_body\n");
539 if (req->rq_async_args.pointer_arg[0] != NULL) {
540 CERROR("returned without dropping rpc_lock: rc %d\n", rc);
541 mdc_close_interpret(req, &req->rq_async_args, rc);
542 portals_debug_dumplog();
551 int mdc_done_writing(struct obd_export *exp, struct obdo *obdo)
553 struct ptlrpc_request *req;
554 struct mds_body *body;
555 int rc, size = sizeof(*body);
558 req = ptlrpc_prep_req(class_exp2cliimp(exp), MDS_DONE_WRITING, 1,
563 body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof(*body));
564 mdc_pack_fid(&body->fid1, obdo->o_id, 0, obdo->o_mode);
565 body->size = obdo->o_size;
566 body->blocks = obdo->o_blocks;
567 body->flags = obdo->o_flags;
568 body->valid = obdo->o_valid;
569 // memcpy(&body->handle, &och->och_fh, sizeof(body->handle));
571 req->rq_replen = lustre_msg_size(1, &size);
573 rc = ptlrpc_queue_wait(req);
574 ptlrpc_req_finished(req);
578 int mdc_readpage(struct obd_export *exp, struct ll_fid *mdc_fid,
579 __u64 offset, struct page *page,
580 struct ptlrpc_request **request)
582 struct obd_import *imp = class_exp2cliimp(exp);
583 struct ptlrpc_request *req = NULL;
584 struct ptlrpc_bulk_desc *desc = NULL;
585 struct mds_body *body;
586 int rc, size = sizeof(*body);
589 CDEBUG(D_INODE, "inode: %ld\n", (long)mdc_fid->id);
591 req = ptlrpc_prep_req(imp, MDS_READPAGE, 1, &size, NULL);
593 GOTO(out, rc = -ENOMEM);
594 /* XXX FIXME bug 249 */
595 req->rq_request_portal = MDS_READPAGE_PORTAL;
597 desc = ptlrpc_prep_bulk_imp(req, 1, BULK_PUT_SINK, MDS_BULK_PORTAL);
599 GOTO(out, rc = -ENOMEM);
600 /* NB req now owns desc and will free it when it gets freed */
602 ptlrpc_prep_bulk_page(desc, page, 0, PAGE_CACHE_SIZE);
604 mdc_readdir_pack(req, offset, PAGE_CACHE_SIZE, mdc_fid);
606 req->rq_replen = lustre_msg_size(1, &size);
607 rc = ptlrpc_queue_wait(req);
610 body = lustre_swab_repbuf(req, 0, sizeof (*body),
611 lustre_swab_mds_body);
613 CERROR("Can't unpack mds_body\n");
614 GOTO(out, rc = -EPROTO);
617 if (req->rq_bulk->bd_nob_transferred != PAGE_CACHE_SIZE) {
618 CERROR ("Unexpected # bytes transferred: %d"
620 req->rq_bulk->bd_nob_transferred,
622 GOTO (out, rc = -EPROTO);
632 static int mdc_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
633 void *karg, void *uarg)
635 struct obd_device *obd = exp->exp_obd;
636 struct obd_ioctl_data *data = karg;
637 struct obd_import *imp = obd->u.cli.cl_import;
638 struct llog_ctxt *ctxt;
645 case OBD_IOC_CLIENT_RECOVER:
646 rc = ptlrpc_recover_import(imp, data->ioc_inlbuf1);
650 case IOC_OSC_SET_ACTIVE:
651 rc = ptlrpc_set_import_active(imp, data->ioc_offset);
653 case OBD_IOC_PARSE: {
654 ctxt = llog_get_context(&exp->exp_obd->obd_llogs,
655 LLOG_CONFIG_REPL_CTXT);
656 rc = class_config_parse_llog(ctxt, data->ioc_inlbuf1, NULL);
660 case OBD_IOC_LLOG_INFO:
661 case OBD_IOC_LLOG_PRINT: {
662 ctxt = llog_get_context(&obd->obd_llogs, LLOG_CONFIG_REPL_CTXT);
663 rc = llog_ioctl(ctxt, cmd, data);
669 CERROR("osc_ioctl(): unrecognised ioctl %#x\n", cmd);
670 GOTO(out, rc = -ENOTTY);
677 int mdc_set_info(struct obd_export *exp, obd_count keylen,
678 void *key, obd_count vallen, void *val)
682 if (keylen == strlen("initial_recov") &&
683 memcmp(key, "initial_recov", strlen("initial_recov")) == 0) {
684 struct obd_import *imp = exp->exp_obd->u.cli.cl_import;
685 if (vallen != sizeof(int))
687 imp->imp_initial_recov = *(int *)val;
688 CDEBUG(D_HA, "%s: set imp_no_init_recov = %d\n",
689 exp->exp_obd->obd_name,
690 imp->imp_initial_recov);
692 } else if (keylen >= strlen("client") && strcmp(key, "client") == 0) {
693 struct ptlrpc_request *req;
694 char *bufs[1] = {key};
696 req = ptlrpc_prep_req(class_exp2cliimp(exp), OST_SET_INFO, 1,
701 req->rq_replen = lustre_msg_size(0, NULL);
702 rc = ptlrpc_queue_wait(req);
703 ptlrpc_req_finished(req);
710 static int mdc_statfs(struct obd_device *obd, struct obd_statfs *osfs,
711 unsigned long max_age)
713 struct ptlrpc_request *req;
714 struct obd_statfs *msfs;
715 int rc, size = sizeof(*msfs);
718 /* We could possibly pass max_age in the request (as an absolute
719 * timestamp or a "seconds.usec ago") so the target can avoid doing
720 * extra calls into the filesystem if that isn't necessary (e.g.
721 * during mount that would help a bit). Having relative timestamps
722 * is not so great if request processing is slow, while absolute
723 * timestamps are not ideal because they need time synchronization. */
724 req = ptlrpc_prep_req(obd->u.cli.cl_import, MDS_STATFS, 0, NULL, NULL);
728 req->rq_replen = lustre_msg_size(1, &size);
730 mdc_get_rpc_lock(obd->u.cli.cl_rpc_lock, NULL);
731 rc = ptlrpc_queue_wait(req);
732 mdc_put_rpc_lock(obd->u.cli.cl_rpc_lock, NULL);
737 msfs = lustre_swab_repbuf(req, 0, sizeof(*msfs),lustre_swab_obd_statfs);
739 CERROR("Can't unpack obd_statfs\n");
740 GOTO(out, rc = -EPROTO);
743 memcpy(osfs, msfs, sizeof (*msfs));
746 ptlrpc_req_finished(req);
751 static int mdc_pin(struct obd_export *exp, obd_id ino, __u32 gen, int type,
752 struct obd_client_handle *handle, int flag)
754 struct ptlrpc_request *req;
755 struct mds_body *body;
756 int rc, size = sizeof(*body);
759 req = ptlrpc_prep_req(class_exp2cliimp(exp), MDS_PIN, 1, &size, NULL);
763 body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*body));
764 mdc_pack_fid(&body->fid1, ino, gen, type);
767 req->rq_replen = lustre_msg_size(1, &size);
769 mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
770 rc = ptlrpc_queue_wait(req);
771 mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
773 CERROR("pin failed: %d\n", rc);
774 ptlrpc_req_finished(req);
778 body = lustre_swab_repbuf(req, 0, sizeof(*body), lustre_swab_mds_body);
780 ptlrpc_req_finished(req);
784 memcpy(&handle->och_fh, &body->handle, sizeof(body->handle));
785 handle->och_magic = OBD_CLIENT_HANDLE_MAGIC;
787 OBD_ALLOC(handle->och_mod, sizeof(*handle->och_mod));
788 if (handle->och_mod == NULL) {
789 DEBUG_REQ(D_ERROR, req, "can't allocate mdc_open_data");
792 handle->och_mod->mod_open_req = req; /* will be dropped by unpin */
797 static int mdc_unpin(struct obd_export *exp,
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 if (handle->och_magic != OBD_CLIENT_HANDLE_MAGIC)
808 req = ptlrpc_prep_req(class_exp2cliimp(exp), MDS_CLOSE, 1, &size, NULL);
812 body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof(*body));
813 memcpy(&body->handle, &handle->och_fh, sizeof(body->handle));
816 req->rq_replen = lustre_msg_size(0, NULL);
817 mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
818 rc = ptlrpc_queue_wait(req);
819 mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
822 CERROR("unpin failed: %d\n", rc);
824 ptlrpc_req_finished(req);
825 ptlrpc_req_finished(handle->och_mod->mod_open_req);
826 OBD_FREE(handle->och_mod, sizeof(*handle->och_mod));
830 int mdc_sync(struct obd_export *exp, struct ll_fid *fid,
831 struct ptlrpc_request **request)
833 struct ptlrpc_request *req;
834 struct mds_body *body;
835 int size = sizeof(*body);
839 req = ptlrpc_prep_req(class_exp2cliimp(exp), MDS_SYNC, 1,&size,NULL);
841 RETURN(rc = -ENOMEM);
844 body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*body));
845 memcpy(&body->fid1, fid, sizeof(*fid));
846 mdc_pack_req_body(req);
849 req->rq_replen = lustre_msg_size(1, &size);
851 rc = ptlrpc_queue_wait(req);
852 if (rc || request == NULL)
853 ptlrpc_req_finished(req);
860 static int mdc_attach(struct obd_device *dev, obd_count len, void *data)
862 struct lprocfs_static_vars lvars;
864 lprocfs_init_vars(mdc, &lvars);
865 return lprocfs_obd_attach(dev, lvars.obd_vars);
868 static int mdc_detach(struct obd_device *dev)
870 return lprocfs_obd_detach(dev);
873 static int mdc_import_event(struct obd_device *obd,
874 struct obd_import *imp,
875 enum obd_import_event event)
879 LASSERT(imp->imp_obd == obd);
882 case IMP_EVENT_DISCON: {
885 case IMP_EVENT_INACTIVE: {
886 if (obd->obd_observer)
887 rc = obd_notify(obd->obd_observer, obd, 0);
890 case IMP_EVENT_INVALIDATE: {
891 struct ldlm_namespace *ns = obd->obd_namespace;
893 ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
897 case IMP_EVENT_ACTIVE: {
898 if (obd->obd_observer)
899 rc = obd_notify(obd->obd_observer, obd, 1);
903 CERROR("Unknown import event %d\n", event);
909 static int mdc_setup(struct obd_device *obd, obd_count len, void *buf)
911 struct client_obd *cli = &obd->u.cli;
915 OBD_ALLOC(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
916 if (!cli->cl_rpc_lock)
918 mdc_init_rpc_lock(cli->cl_rpc_lock);
922 OBD_ALLOC(cli->cl_setattr_lock, sizeof (*cli->cl_setattr_lock));
923 if (!cli->cl_setattr_lock)
924 GOTO(out_free_rpc, rc = -ENOMEM);
925 mdc_init_rpc_lock(cli->cl_setattr_lock);
927 rc = client_obd_setup(obd, len, buf);
929 OBD_FREE(cli->cl_setattr_lock, sizeof (*cli->cl_setattr_lock));
931 OBD_FREE(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
934 rc = obd_llog_init(obd, &obd->obd_llogs, obd, 0, NULL);
937 CERROR("failed to setup llogging subsystems\n");
943 int mdc_init_ea_size(struct obd_export *exp, int easize, int cookiesize)
945 struct obd_device *obd = exp->exp_obd;
946 struct client_obd *cli = &obd->u.cli;
949 if (cli->cl_max_mds_easize < easize)
950 cli->cl_max_mds_easize = easize;
951 if (cli->cl_max_mds_cookiesize < cookiesize)
952 cli->cl_max_mds_cookiesize = cookiesize;
956 static int mdc_precleanup(struct obd_device *obd, int flags)
960 rc = obd_llog_finish(obd, &obd->obd_llogs, 0);
962 CERROR("failed to cleanup llogging subsystems\n");
967 static int mdc_cleanup(struct obd_device *obd, int flags)
969 struct client_obd *cli = &obd->u.cli;
971 OBD_FREE(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
972 OBD_FREE(cli->cl_setattr_lock, sizeof (*cli->cl_setattr_lock));
976 return client_obd_cleanup(obd, flags);
980 static int mdc_llog_init(struct obd_device *obd, struct obd_llogs *llogs,
981 struct obd_device *tgt, int count,
982 struct llog_catid *logid)
984 struct llog_ctxt *ctxt;
988 rc = llog_setup(obd, llogs, LLOG_CONFIG_REPL_CTXT, tgt, 0, NULL,
991 ctxt = llog_get_context(llogs, LLOG_CONFIG_REPL_CTXT);
992 ctxt->loc_imp = obd->u.cli.cl_import;
998 static int mdc_llog_finish(struct obd_device *obd,
999 struct obd_llogs *llogs, int count)
1004 rc = llog_cleanup(llog_get_context(llogs, LLOG_CONFIG_REPL_CTXT));
1008 int mdc_obj_create(struct obd_export *exp, struct obdo *oa,
1009 struct lov_stripe_md **ea, struct obd_trans_info *oti)
1011 struct ptlrpc_request *request;
1012 struct ost_body *body;
1013 int rc, size = sizeof(*body);
1018 request = ptlrpc_prep_req(class_exp2cliimp(exp), OST_CREATE,
1021 GOTO(out_req, rc = -ENOMEM);
1023 body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof (*body));
1024 memcpy(&body->oa, oa, sizeof(body->oa));
1026 request->rq_replen = lustre_msg_size(1, &size);
1027 rc = ptlrpc_queue_wait(request);
1031 body = lustre_swab_repbuf(request, 0, sizeof(*body),
1032 lustre_swab_ost_body);
1034 CERROR ("can't unpack ost_body\n");
1035 GOTO (out_req, rc = -EPROTO);
1038 memcpy(oa, &body->oa, sizeof(*oa));
1040 CDEBUG(D_HA, "transno: "LPD64"\n", request->rq_repmsg->transno);
1043 ptlrpc_req_finished(request);
1047 static int mdc_get_info(struct obd_export *exp, obd_count keylen,
1048 void *key, __u32 *vallen, void *val)
1051 if (!vallen || !val)
1054 if (keylen >= strlen("mdsize") && strcmp(key, "mdsize") == 0) {
1055 struct ptlrpc_request *req;
1057 char *bufs[1] = {key};
1059 req = ptlrpc_prep_req(class_exp2cliimp(exp), OST_GET_INFO, 1,
1064 req->rq_replen = lustre_msg_size(1, vallen);
1065 rc = ptlrpc_queue_wait(req);
1069 /* FIXME: right swap routine here! */
1070 reply = lustre_swab_repbuf(req, 0, sizeof(*reply), NULL);
1071 if (reply == NULL) {
1072 CERROR("Can't unpack mdsize\n");
1073 GOTO(out, rc = -EPROTO);
1075 *((__u32 *)val) = *reply;
1077 ptlrpc_req_finished(req);
1083 int mdc_brw(int rw, struct obd_export *exp, struct obdo *oa,
1084 struct lov_stripe_md *ea, obd_count oa_bufs,
1085 struct brw_page *pgarr, struct obd_trans_info *oti)
1087 struct ptlrpc_bulk_desc *desc;
1088 struct niobuf_remote *niobuf;
1089 struct ptlrpc_request *req;
1090 struct obd_ioobj *ioobj;
1091 struct ost_body *body;
1095 opc = ((rw & OBD_BRW_WRITE) != 0) ? OST_WRITE : OST_READ;
1097 size[0] = sizeof(*body);
1098 size[1] = sizeof(*ioobj);
1099 size[2] = oa_bufs * sizeof(*niobuf);
1101 req = ptlrpc_prep_req(class_exp2cliimp(exp), opc, 3, size, NULL);
1102 LASSERT(req != NULL);
1104 if (opc == OST_WRITE)
1105 desc = ptlrpc_prep_bulk_imp(req, oa_bufs, BULK_GET_SOURCE,
1108 desc = ptlrpc_prep_bulk_imp(req, oa_bufs, BULK_PUT_SINK,
1110 LASSERT(desc != NULL);
1112 body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof(*body));
1113 ioobj = lustre_msg_buf(req->rq_reqmsg, 1, sizeof(*ioobj));
1114 niobuf = lustre_msg_buf(req->rq_reqmsg, 2, oa_bufs * sizeof(*niobuf));
1116 memcpy(&body->oa, oa, sizeof(*oa));
1117 obdo_to_ioobj(oa, ioobj);
1118 ioobj->ioo_bufcnt = oa_bufs;
1120 for (i = 0; i < oa_bufs; i++, niobuf++) {
1121 struct brw_page *pg = &pgarr[i];
1123 LASSERT(pg->count > 0);
1124 LASSERT((pg->off & ~PAGE_MASK) + pg->count <= PAGE_SIZE);
1126 ptlrpc_prep_bulk_page(desc, pg->pg, pg->off & ~PAGE_MASK,
1129 niobuf->offset = pg->off;
1130 niobuf->len = pg->count;
1131 niobuf->flags = pg->flag;
1134 /* size[0] still sizeof (*body) */
1135 if (opc == OST_WRITE) {
1136 /* 1 RC per niobuf */
1137 size[1] = sizeof(__u32) * oa_bufs;
1138 req->rq_replen = lustre_msg_size(2, size);
1140 /* 1 RC for the whole I/O */
1141 req->rq_replen = lustre_msg_size(1, size);
1143 err = ptlrpc_queue_wait(req);
1146 ptlrpc_req_finished(req);
1150 static int mdc_valid_attrs(struct obd_export *exp, struct ll_fid *fid)
1152 struct ldlm_res_id res_id = { .name = {0} };
1153 struct obd_device *obd = exp->exp_obd;
1154 struct lustre_handle lockh;
1155 ldlm_policy_data_t policy;
1159 res_id.name[0] = fid->id;
1160 res_id.name[1] = fid->generation;
1161 policy.l_inodebits.bits = MDS_INODELOCK_UPDATE;
1163 CDEBUG(D_INFO, "trying to match res "LPU64"\n", res_id.name[0]);
1165 /* FIXME use LDLM_FL_TEST_LOCK instead */
1166 flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_CBPENDING;
1167 if (ldlm_lock_match(obd->obd_namespace, flags, &res_id,
1168 LDLM_IBITS, &policy, LCK_PR, &lockh)) {
1169 ldlm_lock_decref(&lockh, LCK_PR);
1173 if (ldlm_lock_match(obd->obd_namespace, flags, &res_id,
1174 LDLM_IBITS, &policy, LCK_PW, &lockh)) {
1175 ldlm_lock_decref(&lockh, LCK_PW);
1181 struct obd_ops mdc_obd_ops = {
1182 o_owner: THIS_MODULE,
1183 o_attach: mdc_attach,
1184 o_detach: mdc_detach,
1186 o_precleanup: mdc_precleanup,
1187 o_cleanup: mdc_cleanup,
1188 o_connect: client_connect_import,
1189 o_disconnect: client_disconnect_export,
1190 o_iocontrol: mdc_iocontrol,
1191 o_set_info: mdc_set_info,
1192 o_statfs: mdc_statfs,
1195 o_llog_init: mdc_llog_init,
1196 o_llog_finish: mdc_llog_finish,
1197 o_create: mdc_obj_create,
1198 o_get_info: mdc_get_info,
1200 o_init_ea_size: mdc_init_ea_size,
1203 struct md_ops mdc_md_ops = {
1204 m_getstatus: mdc_getstatus,
1205 m_getattr: mdc_getattr,
1206 m_change_cbdata: mdc_change_cbdata,
1208 m_create: mdc_create,
1209 m_done_writing: mdc_done_writing,
1210 m_enqueue: mdc_enqueue,
1211 m_getattr_name: mdc_getattr_name,
1212 m_intent_lock: mdc_intent_lock,
1214 m_rename: mdc_rename,
1215 m_setattr: mdc_setattr,
1217 m_readpage: mdc_readpage,
1218 m_unlink: mdc_unlink,
1219 m_valid_attrs: mdc_valid_attrs,
1222 int __init mdc_init(void)
1224 struct lprocfs_static_vars lvars;
1225 lprocfs_init_vars(mdc, &lvars);
1226 return class_register_type(&mdc_obd_ops, &mdc_md_ops, lvars.module_vars,
1230 static void /*__exit*/ mdc_exit(void)
1232 class_unregister_type(LUSTRE_MDC_NAME);
1236 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
1237 MODULE_DESCRIPTION("Lustre Metadata Client");
1238 MODULE_LICENSE("GPL");
1240 EXPORT_SYMBOL(mdc_req2lustre_md);
1241 EXPORT_SYMBOL(mdc_change_cbdata);
1242 EXPORT_SYMBOL(mdc_getstatus);
1243 EXPORT_SYMBOL(mdc_getattr);
1244 EXPORT_SYMBOL(mdc_getattr_name);
1245 EXPORT_SYMBOL(mdc_create);
1246 EXPORT_SYMBOL(mdc_unlink);
1247 EXPORT_SYMBOL(mdc_rename);
1248 EXPORT_SYMBOL(mdc_link);
1249 EXPORT_SYMBOL(mdc_readpage);
1250 EXPORT_SYMBOL(mdc_setattr);
1251 EXPORT_SYMBOL(mdc_close);
1252 EXPORT_SYMBOL(mdc_done_writing);
1253 EXPORT_SYMBOL(mdc_sync);
1254 EXPORT_SYMBOL(mdc_set_open_replay_data);
1255 EXPORT_SYMBOL(mdc_clear_open_replay_data);
1256 EXPORT_SYMBOL(mdc_store_inode_generation);
1258 module_init(mdc_init);
1259 module_exit(mdc_exit);