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.
5 * Author Peter Braam <braam@clusterfs.com>
7 * This file is part of Lustre, http://www.lustre.org.
9 * Lustre is free software; you can redistribute it and/or
10 * modify it under the terms of version 2 of the GNU General Public
11 * License as published by the Free Software Foundation.
13 * Lustre is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with Lustre; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 * For testing and management it is treated as an obd_device,
23 * although * it does not export a full OBD method table (the
24 * requests are coming * in over the wire, so object target modules
25 * do not have a full * method table.)
30 # define EXPORT_SYMTAB
32 #define DEBUG_SUBSYSTEM S_OSC
35 # include <linux/version.h>
36 # include <linux/module.h>
37 # include <linux/mm.h>
38 # include <linux/highmem.h>
39 # include <linux/lustre_dlm.h>
40 # if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
41 # include <linux/workqueue.h>
42 # include <linux/smp_lock.h>
44 # include <linux/locks.h>
46 #else /* __KERNEL__ */
47 # include <liblustre.h>
50 #include <linux/kp30.h>
51 #include <linux/lustre_net.h>
52 #include <linux/lustre_user.h>
53 #include <linux/obd_ost.h>
54 #include <linux/obd_lov.h>
57 # include <linux/ctype.h>
58 # include <linux/init.h>
63 #include <linux/lustre_ha.h>
64 #include <linux/lprocfs_status.h>
65 #include <linux/lustre_log.h>
66 #include "osc_internal.h"
69 static int osc_attach(struct obd_device *dev, obd_count len, void *data)
71 struct lprocfs_static_vars lvars;
75 lprocfs_init_vars(osc,&lvars);
76 rc = lprocfs_obd_attach(dev, lvars.obd_vars);
80 rc = lproc_osc_attach_seqstat(dev);
82 lprocfs_obd_detach(dev);
86 ptlrpc_lprocfs_register_obd(dev);
90 static int osc_detach(struct obd_device *dev)
92 ptlrpc_lprocfs_unregister_obd(dev);
93 return lprocfs_obd_detach(dev);
97 /* Pack OSC object metadata for disk storage (LE byte order). */
98 static int osc_packmd(struct obd_export *exp, struct lov_mds_md **lmmp,
99 struct lov_stripe_md *lsm)
104 lmm_size = sizeof(**lmmp);
109 OBD_FREE(*lmmp, lmm_size);
115 OBD_ALLOC(*lmmp, lmm_size);
121 LASSERT(lsm->lsm_object_id);
122 (*lmmp)->lmm_object_id = cpu_to_le64(lsm->lsm_object_id);
128 /* Unpack OSC object metadata from disk storage (LE byte order). */
129 static int osc_unpackmd(struct obd_export *exp, struct lov_stripe_md **lsmp,
130 struct lov_mds_md *lmm, int lmm_bytes)
136 if (lmm_bytes < sizeof (*lmm)) {
137 CERROR("lov_mds_md too small: %d, need %d\n",
138 lmm_bytes, (int)sizeof(*lmm));
141 /* XXX LOV_MAGIC etc check? */
143 if (lmm->lmm_object_id == 0) {
144 CERROR("lov_mds_md: zero lmm_object_id\n");
149 lsm_size = lov_stripe_md_size(1);
153 if (*lsmp != NULL && lmm == NULL) {
154 OBD_FREE(*lsmp, lsm_size);
160 OBD_ALLOC(*lsmp, lsm_size);
163 loi_init((*lsmp)->lsm_oinfo);
167 /* XXX zero *lsmp? */
168 (*lsmp)->lsm_object_id = le64_to_cpu (lmm->lmm_object_id);
169 LASSERT((*lsmp)->lsm_object_id);
172 (*lsmp)->lsm_maxbytes = LUSTRE_STRIPE_MAXBYTES;
177 static int osc_getattr_interpret(struct ptlrpc_request *req,
178 struct osc_getattr_async_args *aa, int rc)
180 struct ost_body *body;
186 body = lustre_swab_repbuf(req, 0, sizeof(*body), lustre_swab_ost_body);
188 CDEBUG(D_INODE, "mode: %o\n", body->oa.o_mode);
189 memcpy(aa->aa_oa, &body->oa, sizeof(*aa->aa_oa));
191 /* This should really be sent by the OST */
192 aa->aa_oa->o_blksize = OSC_BRW_MAX_SIZE;
193 aa->aa_oa->o_valid |= OBD_MD_FLBLKSZ;
195 CERROR("can't unpack ost_body\n");
197 aa->aa_oa->o_valid = 0;
203 static int osc_getattr_async(struct obd_export *exp, struct obdo *oa,
204 struct lov_stripe_md *md,
205 struct ptlrpc_request_set *set)
207 struct ptlrpc_request *request;
208 struct ost_body *body;
209 int size = sizeof(*body);
210 struct osc_getattr_async_args *aa;
213 request = ptlrpc_prep_req(class_exp2cliimp(exp), OST_GETATTR, 1,
218 body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof (*body));
219 memcpy(&body->oa, oa, sizeof(*oa));
221 request->rq_replen = lustre_msg_size(1, &size);
222 request->rq_interpret_reply = osc_getattr_interpret;
224 LASSERT (sizeof (*aa) <= sizeof (request->rq_async_args));
225 aa = (struct osc_getattr_async_args *)&request->rq_async_args;
228 ptlrpc_set_add_req (set, request);
232 static int osc_getattr(struct obd_export *exp, struct obdo *oa,
233 struct lov_stripe_md *md)
235 struct ptlrpc_request *request;
236 struct ost_body *body;
237 int rc, size = sizeof(*body);
240 request = ptlrpc_prep_req(class_exp2cliimp(exp), OST_GETATTR, 1,
245 body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof (*body));
246 memcpy(&body->oa, oa, sizeof(*oa));
248 request->rq_replen = lustre_msg_size(1, &size);
250 rc = ptlrpc_queue_wait(request);
252 CERROR("%s failed: rc = %d\n", __FUNCTION__, rc);
256 body = lustre_swab_repbuf(request, 0, sizeof (*body),
257 lustre_swab_ost_body);
259 CERROR ("can't unpack ost_body\n");
260 GOTO (out, rc = -EPROTO);
263 CDEBUG(D_INODE, "mode: %o\n", body->oa.o_mode);
264 memcpy(oa, &body->oa, sizeof(*oa));
266 /* This should really be sent by the OST */
267 oa->o_blksize = OSC_BRW_MAX_SIZE;
268 oa->o_valid |= OBD_MD_FLBLKSZ;
272 ptlrpc_req_finished(request);
276 static int osc_setattr(struct obd_export *exp, struct obdo *oa,
277 struct lov_stripe_md *md, struct obd_trans_info *oti)
279 struct ptlrpc_request *request;
280 struct ost_body *body;
281 int rc, size = sizeof(*body);
284 request = ptlrpc_prep_req(class_exp2cliimp(exp), OST_SETATTR, 1, &size,
289 body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof(*body));
290 memcpy(&body->oa, oa, sizeof(*oa));
292 request->rq_replen = lustre_msg_size(1, &size);
294 rc = ptlrpc_queue_wait(request);
298 body = lustre_swab_repbuf(request, 0, sizeof(*body),
299 lustre_swab_ost_body);
301 GOTO(out, rc = -EPROTO);
303 memcpy(oa, &body->oa, sizeof(*oa));
307 ptlrpc_req_finished(request);
311 int osc_real_create(struct obd_export *exp, struct obdo *oa,
312 struct lov_stripe_md **ea, struct obd_trans_info *oti)
314 struct ptlrpc_request *request;
315 struct ost_body *body;
316 struct lov_stripe_md *lsm;
317 int rc, size = sizeof(*body);
325 rc = obd_alloc_memmd(exp, &lsm);
330 request = ptlrpc_prep_req(class_exp2cliimp(exp), OST_CREATE, 1, &size,
333 GOTO(out, rc = -ENOMEM);
335 body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof (*body));
336 memcpy(&body->oa, oa, sizeof(body->oa));
338 request->rq_replen = lustre_msg_size(1, &size);
339 if (oa->o_valid & OBD_MD_FLINLINE) {
340 LASSERT((oa->o_valid & OBD_MD_FLFLAGS) &&
341 oa->o_flags == OBD_FL_DELORPHAN);
342 DEBUG_REQ(D_HA, request,
343 "delorphan from OST integration; level == RECOVER");
344 request->rq_send_state = LUSTRE_IMP_RECOVER;
347 rc = ptlrpc_queue_wait(request);
351 body = lustre_swab_repbuf(request, 0, sizeof(*body),
352 lustre_swab_ost_body);
354 CERROR ("can't unpack ost_body\n");
355 GOTO (out_req, rc = -EPROTO);
358 memcpy(oa, &body->oa, sizeof(*oa));
360 /* This should really be sent by the OST */
361 oa->o_blksize = OSC_BRW_MAX_SIZE;
362 oa->o_valid |= OBD_MD_FLBLKSZ;
364 /* XXX LOV STACKING: the lsm that is passed to us from LOV does not
365 * have valid lsm_oinfo data structs, so don't go touching that.
366 * This needs to be fixed in a big way.
368 lsm->lsm_object_id = oa->o_id;
372 oti->oti_transno = request->rq_repmsg->transno;
374 if (oa->o_valid & OBD_MD_FLCOOKIE) {
375 if (!oti->oti_logcookies)
376 oti_alloc_cookies(oti, 1);
377 memcpy(oti->oti_logcookies, obdo_logcookie(oa),
378 sizeof(oti->oti_onecookie));
382 CDEBUG(D_HA, "transno: "LPD64"\n", request->rq_repmsg->transno);
385 ptlrpc_req_finished(request);
388 obd_free_memmd(exp, &lsm);
392 static int osc_punch(struct obd_export *exp, struct obdo *oa,
393 struct lov_stripe_md *md, obd_size start,
394 obd_size end, struct obd_trans_info *oti)
396 struct ptlrpc_request *request;
397 struct ost_body *body;
398 int rc, size = sizeof(*body);
406 request = ptlrpc_prep_req(class_exp2cliimp(exp), OST_PUNCH, 1, &size,
411 body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof (*body));
412 memcpy(&body->oa, oa, sizeof(*oa));
414 /* overload the size and blocks fields in the oa with start/end */
415 body->oa.o_size = start;
416 body->oa.o_blocks = end;
417 body->oa.o_valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS);
419 request->rq_replen = lustre_msg_size(1, &size);
421 rc = ptlrpc_queue_wait(request);
425 body = lustre_swab_repbuf (request, 0, sizeof (*body),
426 lustre_swab_ost_body);
428 CERROR ("can't unpack ost_body\n");
429 GOTO (out, rc = -EPROTO);
432 memcpy(oa, &body->oa, sizeof(*oa));
436 ptlrpc_req_finished(request);
440 static int osc_sync(struct obd_export *exp, struct obdo *oa,
441 struct lov_stripe_md *md, obd_size start, obd_size end)
443 struct ptlrpc_request *request;
444 struct ost_body *body;
445 int rc, size = sizeof(*body);
453 request = ptlrpc_prep_req(class_exp2cliimp(exp), OST_SYNC, 1, &size,
458 body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof (*body));
459 memcpy(&body->oa, oa, sizeof(*oa));
461 /* overload the size and blocks fields in the oa with start/end */
462 body->oa.o_size = start;
463 body->oa.o_blocks = end;
464 body->oa.o_valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS);
466 request->rq_replen = lustre_msg_size(1, &size);
468 rc = ptlrpc_queue_wait(request);
472 body = lustre_swab_repbuf(request, 0, sizeof(*body),
473 lustre_swab_ost_body);
475 CERROR ("can't unpack ost_body\n");
476 GOTO (out, rc = -EPROTO);
479 memcpy(oa, &body->oa, sizeof(*oa));
483 ptlrpc_req_finished(request);
487 static int osc_destroy(struct obd_export *exp, struct obdo *oa,
488 struct lov_stripe_md *ea, struct obd_trans_info *oti)
490 struct ptlrpc_request *request;
491 struct ost_body *body;
492 int rc, size = sizeof(*body);
500 request = ptlrpc_prep_req(class_exp2cliimp(exp), OST_DESTROY, 1,
505 body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof (*body));
507 if (oti != NULL && oa->o_valid & OBD_MD_FLCOOKIE) {
508 memcpy(obdo_logcookie(oa), oti->oti_logcookies,
509 sizeof(*oti->oti_logcookies));
510 oti->oti_logcookies++;
513 memcpy(&body->oa, oa, sizeof(*oa));
514 request->rq_replen = lustre_msg_size(1, &size);
516 rc = ptlrpc_queue_wait(request);
520 body = lustre_swab_repbuf(request, 0, sizeof(*body),
521 lustre_swab_ost_body);
523 CERROR ("Can't unpack body\n");
524 GOTO (out, rc = -EPROTO);
527 memcpy(oa, &body->oa, sizeof(*oa));
531 ptlrpc_req_finished(request);
535 static void osc_announce_cached(struct client_obd *cli, struct ost_body *body)
537 obd_flag bits = OBD_MD_FLBLOCKS|OBD_MD_FLRDEV;
539 LASSERT(!(body->oa.o_valid & bits));
541 body->oa.o_valid |= bits;
542 down(&cli->cl_dirty_sem);
543 body->oa.o_blocks = cli->cl_dirty;
544 body->oa.o_rdev = cli->cl_dirty_granted;
545 up(&cli->cl_dirty_sem);
546 CDEBUG(D_INODE, "announcing "LPU64" dirty "LPU64" granted\n",
547 cli->cl_dirty, cli->cl_dirty_granted);
550 static void osc_update_grant(struct client_obd *cli, struct ost_body *body)
552 if(!(body->oa.o_valid & OBD_MD_FLRDEV)) {
553 if (cli->cl_ost_can_grant) {
554 CDEBUG(D_INODE, "%s can't grant\n",
555 cli->cl_import->imp_target_uuid.uuid);
557 cli->cl_ost_can_grant = 0;
561 CDEBUG(D_ERROR, "got "LPU64" grant\n", body->oa.o_rdev);
562 down(&cli->cl_dirty_sem);
563 cli->cl_dirty_granted = body->oa.o_rdev;
564 /* XXX check for over-run and wake up the io thread that
565 * doesn't exist yet */
566 up(&cli->cl_dirty_sem);
569 /* We assume that the reason this OSC got a short read is because it read
570 * beyond the end of a stripe file; i.e. lustre is reading a sparse file
571 * via the LOV, and it _knows_ it's reading inside the file, it's just that
572 * this stripe never got written at or beyond this stripe offset yet. */
573 static void handle_short_read(int nob_read, obd_count page_count,
574 struct brw_page *pga)
578 /* skip bytes read OK */
579 while (nob_read > 0) {
580 LASSERT (page_count > 0);
582 if (pga->count > nob_read) {
583 /* EOF inside this page */
584 ptr = kmap(pga->pg) + (pga->off & ~PAGE_MASK);
585 memset(ptr + nob_read, 0, pga->count - nob_read);
592 nob_read -= pga->count;
597 /* zero remaining pages */
598 while (page_count-- > 0) {
599 ptr = kmap(pga->pg) + (pga->off & ~PAGE_MASK);
600 memset(ptr, 0, pga->count);
606 static int check_write_rcs(struct ptlrpc_request *request, int niocount,
607 obd_count page_count, struct brw_page *pga)
612 /* return error if any niobuf was in error */
613 remote_rcs = lustre_swab_repbuf(request, 1,
614 sizeof(*remote_rcs) * niocount, NULL);
615 if (remote_rcs == NULL) {
616 CERROR ("Missing/short RC vector on BRW_WRITE reply\n");
619 if (lustre_msg_swabbed (request->rq_repmsg))
620 for (i = 0; i < niocount; i++)
621 __swab32s (&remote_rcs[i]);
623 for (i = 0; i < niocount; i++) {
624 if (remote_rcs[i] < 0)
625 return (remote_rcs[i]);
627 if (remote_rcs[i] != 0) {
628 CERROR ("rc[%d] invalid (%d) req %p\n",
629 i, remote_rcs[i], request);
637 static inline int can_merge_pages(struct brw_page *p1, struct brw_page *p2)
639 if (p1->flag != p2->flag) {
640 unsigned mask = ~(OBD_BRW_CREATE|OBD_BRW_FROM_GRANT);
642 /* warn if we try to combine flags that we don't know to be
644 if ((p1->flag & mask) != (p2->flag & mask))
645 CERROR("is it ok to have flags 0x%x and 0x%x in the "
646 "same brw?\n", p1->flag, p2->flag);
650 return (p1->off + p1->count == p2->off);
654 static obd_count cksum_pages(int nob, obd_count page_count,
655 struct brw_page *pga)
661 LASSERT (page_count > 0);
664 ost_checksum(&cksum, ptr + (pga->off & (PAGE_SIZE - 1)),
665 pga->count > nob ? nob : pga->count);
677 static int osc_brw_prep_request(int cmd, struct obd_import *imp,struct obdo *oa,
678 struct lov_stripe_md *lsm, obd_count page_count,
679 struct brw_page *pga, int *requested_nobp,
680 int *niocountp, struct ptlrpc_request **reqp)
682 struct ptlrpc_request *req;
683 struct ptlrpc_bulk_desc *desc;
684 struct client_obd *cli = &imp->imp_obd->u.cli;
685 struct ost_body *body;
686 struct obd_ioobj *ioobj;
687 struct niobuf_remote *niobuf;
696 opc = ((cmd & OBD_BRW_WRITE) != 0) ? OST_WRITE : OST_READ;
698 for (niocount = i = 1; i < page_count; i++)
699 if (!can_merge_pages (&pga[i - 1], &pga[i]))
702 size[0] = sizeof(*body);
703 size[1] = sizeof(*ioobj);
704 size[2] = niocount * sizeof(*niobuf);
706 req = ptlrpc_prep_req(imp, opc, 3, size, NULL);
710 if (opc == OST_WRITE)
711 desc = ptlrpc_prep_bulk_imp(req, BULK_GET_SOURCE,
714 desc = ptlrpc_prep_bulk_imp(req, BULK_PUT_SINK,
717 GOTO(out, rc = -ENOMEM);
718 /* NB request now owns desc and will free it when it gets freed */
720 body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof(*body));
721 ioobj = lustre_msg_buf(req->rq_reqmsg, 1, sizeof(*ioobj));
722 niobuf = lustre_msg_buf(req->rq_reqmsg, 2, niocount * sizeof(*niobuf));
724 memcpy(&body->oa, oa, sizeof(*oa));
726 obdo_to_ioobj(oa, ioobj);
727 ioobj->ioo_bufcnt = niocount;
729 LASSERT (page_count > 0);
730 for (requested_nob = i = 0; i < page_count; i++, niobuf++) {
731 struct brw_page *pg = &pga[i];
732 struct brw_page *pg_prev = pg - 1;
734 LASSERT(pg->count > 0);
735 LASSERT((pg->off & ~PAGE_MASK) + pg->count <= PAGE_SIZE);
736 LASSERTF(i == 0 || pg->off > pg_prev->off,
737 "i %d p_c %u pg %p [pri %lu ind %lu] off "LPU64
738 " prev_pg %p [pri %lu ind %lu] off "LPU64,
740 pg->pg, pg->pg->private, pg->pg->index, pg->off,
741 pg_prev->pg, pg_prev->pg->private, pg_prev->pg->index,
744 rc = ptlrpc_prep_bulk_page(desc, pg->pg, pg->off & ~PAGE_MASK,
749 requested_nob += pg->count;
751 if (i > 0 && can_merge_pages(pg_prev, pg)) {
753 niobuf->len += pg->count;
755 niobuf->offset = pg->off;
756 niobuf->len = pg->count;
757 niobuf->flags = pg->flag;
761 LASSERT((void *)(niobuf - niocount) ==
762 lustre_msg_buf(req->rq_reqmsg, 2, niocount * sizeof(*niobuf)));
763 osc_announce_cached(cli, body);
764 spin_lock_irqsave(&req->rq_lock, flags);
765 req->rq_no_resend = 1;
766 spin_unlock_irqrestore(&req->rq_lock, flags);
768 /* size[0] still sizeof (*body) */
769 if (opc == OST_WRITE) {
771 body->oa.o_valid |= OBD_MD_FLCKSUM;
772 body->oa.o_nlink = cksum_pages(requested_nob, page_count, pga);
774 /* 1 RC per niobuf */
775 size[1] = sizeof(__u32) * niocount;
776 req->rq_replen = lustre_msg_size(2, size);
778 /* 1 RC for the whole I/O */
779 req->rq_replen = lustre_msg_size(1, size);
782 *niocountp = niocount;
783 *requested_nobp = requested_nob;
788 ptlrpc_req_finished (req);
792 static int osc_brw_fini_request(struct ptlrpc_request *req, struct obdo *oa,
793 int requested_nob, int niocount,
794 obd_count page_count, struct brw_page *pga,
797 struct client_obd *cli = &req->rq_import->imp_obd->u.cli;
798 struct ost_body *body;
803 body = lustre_swab_repbuf(req, 0, sizeof(*body), lustre_swab_ost_body);
805 CERROR ("Can't unpack body\n");
809 osc_update_grant(cli, body);
811 if (req->rq_reqmsg->opc == OST_WRITE) {
813 CERROR ("Unexpected +ve rc %d\n", rc);
817 return(check_write_rcs(req, niocount, page_count, pga));
820 if (rc > requested_nob) {
821 CERROR("Unexpected rc %d (%d requested)\n", rc, requested_nob);
825 if (rc < requested_nob)
826 handle_short_read(rc, page_count, pga);
828 memcpy(oa, &body->oa, sizeof(*oa));
831 if (oa->o_valid & OBD_MD_FLCKSUM) {
832 const struct ptlrpc_peer *peer =
833 &req->rq_import->imp_connection->c_peer;
834 static int cksum_counter;
835 obd_count server_cksum = oa->o_nlink;
836 obd_count cksum = cksum_pages(rc, page_count, pga);
837 char str[PTL_NALFMT_SIZE];
839 portals_nid2str(peer->peer_ni->pni_number, peer->peer_nid, str);
842 if (server_cksum != cksum) {
843 CERROR("Bad checksum: server %x, client %x, server NID "
844 LPX64" (%s)\n", server_cksum, cksum,
845 peer->peer_nid, str);
848 } else if ((cksum_counter & (-cksum_counter)) == cksum_counter){
849 CWARN("Checksum %u from "LPX64" (%s) OK: %x\n",
850 cksum_counter, peer->peer_nid, str, cksum);
853 static int cksum_missed;
856 if ((cksum_missed & (-cksum_missed)) == cksum_missed)
857 CERROR("Request checksum %u from "LPX64", no reply\n",
859 req->rq_import->imp_connection->c_peer.peer_nid);
865 static int osc_brw_internal(int cmd, struct obd_export *exp,struct obdo *oa,
866 struct lov_stripe_md *lsm,
867 obd_count page_count, struct brw_page *pga)
871 struct ptlrpc_request *request;
876 rc = osc_brw_prep_request(cmd, class_exp2cliimp(exp), oa, lsm,
877 page_count, pga, &requested_nob, &niocount,
879 /* NB ^ sets rq_no_resend */
884 rc = ptlrpc_queue_wait(request);
886 if (rc == -ETIMEDOUT && request->rq_resend) {
887 DEBUG_REQ(D_HA, request, "BULK TIMEOUT");
888 ptlrpc_req_finished(request);
892 rc = osc_brw_fini_request(request, oa, requested_nob, niocount,
893 page_count, pga, rc);
895 ptlrpc_req_finished(request);
899 static int brw_interpret(struct ptlrpc_request *request,
900 struct osc_brw_async_args *aa, int rc)
902 struct obdo *oa = aa->aa_oa;
903 int requested_nob = aa->aa_requested_nob;
904 int niocount = aa->aa_nio_count;
905 obd_count page_count = aa->aa_page_count;
906 struct brw_page *pga = aa->aa_pga;
909 /* XXX bug 937 here */
910 if (rc == -ETIMEDOUT && request->rq_resend) {
911 DEBUG_REQ(D_HA, request, "BULK TIMEOUT");
912 LBUG(); /* re-send. later. */
916 rc = osc_brw_fini_request(request, oa, requested_nob, niocount,
917 page_count, pga, rc);
921 static int async_internal(int cmd, struct obd_export *exp, struct obdo *oa,
922 struct lov_stripe_md *lsm, obd_count page_count,
923 struct brw_page *pga, struct ptlrpc_request_set *set)
925 struct ptlrpc_request *request;
928 struct osc_brw_async_args *aa;
932 rc = osc_brw_prep_request(cmd, class_exp2cliimp(exp), oa, lsm,
933 page_count, pga, &requested_nob, &nio_count,
935 /* NB ^ sets rq_no_resend */
938 LASSERT(sizeof(*aa) <= sizeof(request->rq_async_args));
939 aa = (struct osc_brw_async_args *)&request->rq_async_args;
941 aa->aa_requested_nob = requested_nob;
942 aa->aa_nio_count = nio_count;
943 aa->aa_page_count = page_count;
946 request->rq_interpret_reply = brw_interpret;
947 ptlrpc_set_add_req(set, request);
953 #define min_t(type,x,y) \
954 ({ type __x = (x); type __y = (y); __x < __y ? __x: __y; })
958 * ugh, we want disk allocation on the target to happen in offset order. we'll
959 * follow sedgewicks advice and stick to the dead simple shellsort -- it'll do
960 * fine for our small page arrays and doesn't require allocation. its an
961 * insertion sort that swaps elements that are strides apart, shrinking the
962 * stride down until its '1' and the array is sorted.
964 static void sort_brw_pages(struct brw_page *array, int num)
971 for (stride = 1; stride < num ; stride = (stride * 3) + 1)
976 for (i = stride ; i < num ; i++) {
979 while (j >= stride && array[j - stride].off > tmp.off) {
980 array[j] = array[j - stride];
985 } while (stride > 1);
988 /* make sure we the regions we're passing to elan don't violate its '4
989 * fragments' constraint. portal headers are a fragment, all full
990 * PAGE_SIZE long pages count as 1 fragment, and each partial page
991 * counts as a fragment. I think. see bug 934. */
992 static obd_count check_elan_limit(struct brw_page *pg, obd_count pages)
995 int saw_whole_frag = 0;
998 for (i = 0 ; frags_left && i < pages ; pg++, i++) {
999 if (pg->count == PAGE_SIZE) {
1000 if (!saw_whole_frag) {
1011 static int osc_brw(int cmd, struct obd_export *exp, struct obdo *oa,
1012 struct lov_stripe_md *md, obd_count page_count,
1013 struct brw_page *pga, struct obd_trans_info *oti)
1017 if (cmd == OBD_BRW_CHECK) {
1018 /* The caller just wants to know if there's a chance that this
1019 * I/O can succeed */
1020 struct obd_import *imp = class_exp2cliimp(exp);
1022 if (imp == NULL || imp->imp_invalid)
1027 while (page_count) {
1028 obd_count pages_per_brw;
1031 if (page_count > OSC_BRW_MAX_IOV)
1032 pages_per_brw = OSC_BRW_MAX_IOV;
1034 pages_per_brw = page_count;
1036 sort_brw_pages(pga, pages_per_brw);
1037 pages_per_brw = check_elan_limit(pga, pages_per_brw);
1039 rc = osc_brw_internal(cmd, exp, oa, md, pages_per_brw, pga);
1044 page_count -= pages_per_brw;
1045 pga += pages_per_brw;
1050 static int osc_brw_async(int cmd, struct obd_export *exp, struct obdo *oa,
1051 struct lov_stripe_md *md, obd_count page_count,
1052 struct brw_page *pga, struct ptlrpc_request_set *set,
1053 struct obd_trans_info *oti)
1057 if (cmd == OBD_BRW_CHECK) {
1058 /* The caller just wants to know if there's a chance that this
1059 * I/O can succeed */
1060 struct obd_import *imp = class_exp2cliimp(exp);
1062 if (imp == NULL || imp->imp_invalid)
1067 while (page_count) {
1068 obd_count pages_per_brw;
1071 if (page_count > OSC_BRW_MAX_IOV)
1072 pages_per_brw = OSC_BRW_MAX_IOV;
1074 pages_per_brw = page_count;
1076 sort_brw_pages(pga, pages_per_brw);
1077 pages_per_brw = check_elan_limit(pga, pages_per_brw);
1079 rc = async_internal(cmd, exp, oa, md, pages_per_brw, pga, set);
1084 page_count -= pages_per_brw;
1085 pga += pages_per_brw;
1090 static void osc_check_rpcs(struct client_obd *cli);
1091 static void osc_exit_cache(struct client_obd *cli, struct osc_async_page *oap);
1093 static void osc_complete_oap(struct client_obd *cli,
1094 struct osc_async_page *oap, int rc)
1097 osc_exit_cache(cli, oap);
1098 oap->oap_async_flags = 0;
1099 if (oap->oap_osic) {
1100 osic_complete_one(oap->oap_osic, rc);
1101 oap->oap_osic = NULL;
1106 oap->oap_caller_ops->ap_completion(oap->oap_caller_data, oap->oap_cmd,
1111 static int brw_interpret_oap(struct ptlrpc_request *request,
1112 struct osc_brw_async_args *aa, int rc)
1114 struct osc_async_page *oap;
1115 struct client_obd *cli;
1116 struct list_head *pos, *n;
1119 CDEBUG(D_INODE, "request %p aa %p\n", request, aa);
1121 rc = osc_brw_fini_request(request, aa->aa_oa, aa->aa_requested_nob,
1122 aa->aa_nio_count, aa->aa_page_count,
1126 /* in failout recovery we ignore writeback failure and want
1127 * to just tell llite to unlock the page and continue */
1128 if (cli->cl_import == NULL || cli->cl_import->imp_invalid)
1131 spin_lock(&cli->cl_loi_list_lock);
1133 /* the caller may re-use the oap after the completion call so
1134 * we need to clean it up a little */
1135 list_for_each_safe(pos, n, &aa->aa_oaps) {
1136 oap = list_entry(pos, struct osc_async_page, oap_rpc_item);
1138 //CDEBUG(D_INODE, "page %p index %lu oap %p\n",
1139 //oap->oap_page, oap->oap_page->index, oap);
1141 list_del_init(&oap->oap_rpc_item);
1142 osc_complete_oap(cli, oap, rc);
1145 cli->cl_brw_in_flight--;
1146 osc_check_rpcs(cli);
1148 spin_unlock(&cli->cl_loi_list_lock);
1150 obdo_free(aa->aa_oa);
1151 OBD_FREE(aa->aa_pga, aa->aa_page_count * sizeof(struct brw_page));
1156 static struct ptlrpc_request *osc_build_req(struct client_obd *cli,
1157 struct list_head *rpc_list,
1158 int page_count, int cmd)
1160 struct ptlrpc_request *req;
1161 struct brw_page *pga = NULL;
1162 int requested_nob, nio_count;
1163 struct osc_brw_async_args *aa;
1164 struct obdo *oa = NULL;
1165 struct obd_async_page_ops *ops = NULL;
1166 void *caller_data = NULL;
1167 struct list_head *pos;
1170 LASSERT(!list_empty(rpc_list));
1172 OBD_ALLOC(pga, sizeof(*pga) * page_count);
1174 RETURN(ERR_PTR(-ENOMEM));
1178 GOTO(out, req = ERR_PTR(-ENOMEM));
1181 list_for_each(pos, rpc_list) {
1182 struct osc_async_page *oap;
1184 oap = list_entry(pos, struct osc_async_page, oap_rpc_item);
1186 ops = oap->oap_caller_ops;
1187 caller_data = oap->oap_caller_data;
1189 pga[i].off = oap->oap_obj_off + oap->oap_page_off;
1190 pga[i].pg = oap->oap_page;
1191 pga[i].count = oap->oap_count;
1192 pga[i].flag = oap->oap_brw_flags;
1193 //CDEBUG(D_INODE, "putting page %p index %lu oap %p into pga\n",
1194 //pga[i].pg, oap->oap_page->index, oap);
1198 /* always get the data for the obdo for the rpc */
1199 LASSERT(ops != NULL);
1200 ops->ap_fill_obdo(caller_data, cmd, oa);
1202 sort_brw_pages(pga, page_count);
1203 rc = osc_brw_prep_request(cmd, cli->cl_import, oa, NULL, page_count,
1204 pga, &requested_nob, &nio_count, &req);
1206 CERROR("prep_req failed: %d\n", rc);
1207 GOTO(out, req = ERR_PTR(rc));
1210 LASSERT(sizeof(*aa) <= sizeof(req->rq_async_args));
1211 aa = (struct osc_brw_async_args *)&req->rq_async_args;
1213 aa->aa_requested_nob = requested_nob;
1214 aa->aa_nio_count = nio_count;
1215 aa->aa_page_count = page_count;
1224 OBD_FREE(pga, sizeof(*pga) * page_count);
1229 static void lop_update_pending(struct client_obd *cli,
1230 struct loi_oap_pages *lop, int cmd, int delta)
1232 lop->lop_num_pending += delta;
1233 if (cmd == OBD_BRW_WRITE)
1234 cli->cl_pending_w_pages += delta;
1236 cli->cl_pending_r_pages += delta;
1239 /* the loi lock is held across this function but it's allowed to release
1240 * and reacquire it during its work */
1241 static int osc_send_oap_rpc(struct client_obd *cli, int cmd,
1242 struct loi_oap_pages *lop)
1244 struct ptlrpc_request *request;
1245 obd_count page_count = 0;
1246 struct list_head *tmp, *pos;
1247 struct osc_async_page *oap = NULL;
1248 struct osc_brw_async_args *aa;
1249 struct obd_async_page_ops *ops;
1250 LIST_HEAD(rpc_list);
1253 /* first we find the pages we're allowed to work with */
1254 list_for_each_safe(pos, tmp, &lop->lop_pending) {
1255 oap = list_entry(pos, struct osc_async_page, oap_pending_item);
1256 ops = oap->oap_caller_ops;
1258 /* in llite being 'ready' equates to the page being locked
1259 * until completion unlocks it. commit_write submits a page
1260 * as not ready because its unlock will happen unconditionally
1261 * as the call returns. if we race with commit_write giving
1262 * us that page we dont' want to create a hole in the page
1263 * stream, so we stop and leave the rpc to be fired by
1264 * another dirtier or kupdated interval (the not ready page
1265 * will still be on the dirty list). we could call in
1266 * at the end of ll_file_write to process the queue again. */
1267 if (!(oap->oap_async_flags & ASYNC_READY)) {
1268 if (ops->ap_make_ready(oap->oap_caller_data, cmd)) {
1269 CDEBUG(D_INODE, "oap at page_count %d not "
1270 "ready\n", page_count);
1273 oap->oap_async_flags |= ASYNC_READY;
1276 /* take the page out of our book-keeping */
1277 list_del_init(&oap->oap_pending_item);
1278 lop_update_pending(cli, lop, cmd, -1);
1279 if (!list_empty(&oap->oap_urgent_item))
1280 list_del_init(&oap->oap_urgent_item);
1282 /* ask the caller for the size of the io as the rpc leaves. */
1283 if (!(oap->oap_async_flags & ASYNC_COUNT_STABLE))
1284 oap->oap_count = ops->ap_refresh_count(
1285 oap->oap_caller_data,
1287 if (oap->oap_count <= 0) {
1288 CDEBUG(D_INODE, "oap %p count %d, completing\n", oap,
1290 osc_complete_oap(cli, oap, oap->oap_count);
1294 /* now put the page back in our accounting */
1295 list_add_tail(&oap->oap_rpc_item, &rpc_list);
1296 if (++page_count >= cli->cl_max_pages_per_rpc)
1300 if (page_count == 0)
1303 spin_unlock(&cli->cl_loi_list_lock);
1305 request = osc_build_req(cli, &rpc_list, page_count, cmd);
1306 if (IS_ERR(request)) {
1307 /* this should happen rarely and is pretty bad, it makes the
1308 * pending list not follow the dirty order */
1309 spin_lock(&cli->cl_loi_list_lock);
1310 list_for_each_safe(pos, tmp, &rpc_list) {
1311 oap = list_entry(pos, struct osc_async_page,
1313 list_del_init(&oap->oap_rpc_item);
1314 list_add_tail(&oap->oap_pending_item,
1316 lop_update_pending(cli, lop, cmd, 1);
1317 if (oap->oap_async_flags & ASYNC_URGENT)
1318 list_add(&oap->oap_urgent_item,
1321 RETURN(PTR_ERR(request));
1324 LASSERT(sizeof(*aa) <= sizeof(request->rq_async_args));
1325 aa = (struct osc_brw_async_args *)&request->rq_async_args;
1326 INIT_LIST_HEAD(&aa->aa_oaps);
1327 list_splice(&rpc_list, &aa->aa_oaps);
1328 INIT_LIST_HEAD(&rpc_list);
1330 if (cmd == OBD_BRW_READ)
1331 lproc_osc_hist_pow2(&cli->cl_read_page_hist, page_count);
1333 lproc_osc_hist_pow2(&cli->cl_write_page_hist, page_count);
1335 spin_lock(&cli->cl_loi_list_lock);
1336 if (cmd == OBD_BRW_READ)
1337 lproc_osc_hist(&cli->cl_read_rpc_hist, cli->cl_brw_in_flight);
1339 lproc_osc_hist(&cli->cl_write_rpc_hist, cli->cl_brw_in_flight);
1341 cli->cl_brw_in_flight++;
1342 CDEBUG(D_INODE, "req %p: %d pages, aa %p. now %d in flight\n", request,
1343 page_count, aa, cli->cl_brw_in_flight);
1345 request->rq_interpret_reply = brw_interpret_oap;
1346 ptlrpcd_add_req(request);
1350 static int lop_makes_rpc(struct client_obd *cli, struct loi_oap_pages *lop,
1356 /* stream rpcs until we complete the urgent pages in the object */
1357 if (!list_empty(&lop->lop_urgent))
1360 /* fire off rpcs when we have 'optimal' rpcs as tuned for the wire. */
1361 optimal = cli->cl_max_pages_per_rpc;
1362 /* *2 to avoid triggering rpcs that would want to include pages that
1363 * are being queued but which can't be made ready until the queuer
1364 * finishes with the page. this is a wart for llite::commit_write() */
1365 if (cmd == OBD_BRW_WRITE)
1367 if (lop->lop_num_pending >= optimal)
1370 /* trigger a write rpc stream as long as there are dirtiers waiting
1371 * for space. as they're waiting, they're not going to create more
1372 * pages to coallesce with what's waiting.. */
1373 if (!list_empty(&cli->cl_cache_waiters))
1379 static int loi_makes_rpc(struct client_obd *cli, struct lov_oinfo *loi)
1381 return lop_makes_rpc(cli, &loi->loi_write_lop, OBD_BRW_WRITE) ||
1382 lop_makes_rpc(cli, &loi->loi_read_lop, OBD_BRW_READ);
1385 static void loi_onto_ready_list(struct client_obd *cli, struct lov_oinfo *loi)
1387 if (list_empty(&loi->loi_cli_item) && loi_makes_rpc(cli, loi))
1388 list_add_tail(&loi->loi_cli_item, &cli->cl_loi_ready_list);
1391 #define LOI_DEBUG(LOI, STR, args...) \
1392 CDEBUG(D_INODE, "loi rdy %d [%p,%p] wr %d:%d rd %d:%d " STR, \
1393 !list_empty(&(LOI)->loi_cli_item), \
1394 (LOI)->loi_cli_item.next, \
1395 (LOI)->loi_cli_item.prev, \
1396 (LOI)->loi_write_lop.lop_num_pending, \
1397 !list_empty(&(LOI)->loi_write_lop.lop_urgent), \
1398 (LOI)->loi_read_lop.lop_num_pending, \
1399 !list_empty(&(LOI)->loi_read_lop.lop_urgent), \
1402 /* called with the loi list lock held */
1403 static void osc_check_rpcs(struct client_obd *cli)
1405 struct lov_oinfo *loi;
1406 int rc = 0, making_progress;
1409 if (list_empty(&cli->cl_loi_ready_list)) {
1410 CDEBUG(D_INODE, "no lois ready\n");
1415 while (!list_empty(&cli->cl_loi_ready_list)) {
1416 loi = list_entry(cli->cl_loi_ready_list.next, struct lov_oinfo,
1419 if (cli->cl_brw_in_flight >= cli->cl_max_rpcs_in_flight)
1422 making_progress = 0;
1424 /* hmm, it occurs to me that having rpc preparation fail
1425 * with num_pending == num_urgent means that there won't
1426 * be any more calls into here unless other traffic comes
1429 /* attempt some read/write balancing by alternating between
1430 * reads and writes in an object */
1431 if (lop_makes_rpc(cli, &loi->loi_write_lop, OBD_BRW_WRITE)) {
1432 rc = osc_send_oap_rpc(cli, OBD_BRW_WRITE,
1433 &loi->loi_write_lop);
1439 if (lop_makes_rpc(cli, &loi->loi_read_lop, OBD_BRW_READ)) {
1440 rc = osc_send_oap_rpc(cli, OBD_BRW_READ,
1441 &loi->loi_read_lop);
1448 /* attempt some inter-object balancing by issueing rpcs
1449 * for each object in turn */
1450 if (!list_empty(&loi->loi_cli_item))
1451 list_del_init(&loi->loi_cli_item);
1453 loi_onto_ready_list(cli, loi);
1455 LOI_DEBUG(loi, "mp %d\n", making_progress);
1457 /* could be smarter, !making_progress can happen in theory
1458 * if all the pages can not be locked in set_io_ready */
1459 if (!making_progress)
1465 /* we're trying to queue a page in the osc so we're subject to the
1466 * 'cl_dirty_max' limit on the number of pages that can be queued in the osc.
1467 * If the osc's queued pages are already at that limit, then we want to sleep
1468 * until there is space in the osc's queue for us. we need this goofy
1469 * little struct to really tell that our allocation was fulfilled in
1470 * the presence of pending signals */
1471 struct osc_cache_waiter {
1472 struct list_head ocw_entry;
1473 wait_queue_head_t ocw_waitq;
1475 static int ocw_granted(struct client_obd *cli, struct osc_cache_waiter *ocw)
1479 spin_lock(&cli->cl_loi_list_lock);
1480 rc = list_empty(&ocw->ocw_entry);
1481 spin_unlock(&cli->cl_loi_list_lock);
1484 static int osc_enter_cache(struct client_obd *cli, struct lov_oinfo *loi,
1485 struct osc_async_page *oap)
1487 struct osc_cache_waiter ocw;
1488 struct l_wait_info lwi = {0};
1492 /* XXX check for ost grants here as well.. for now we ignore them. */
1493 if (cli->cl_dirty_max < PAGE_SIZE)
1496 /* if we fail this test then cl_dirty contains at least one page
1497 * that will have to be completed after we release the lock */
1498 if (cli->cl_dirty + PAGE_SIZE <= cli->cl_dirty_max) {
1499 /* account for ourselves */
1500 cli->cl_dirty += PAGE_SIZE;
1504 init_waitqueue_head(&ocw.ocw_waitq);
1505 list_add_tail(&ocw.ocw_entry, &cli->cl_cache_waiters);
1507 /* make sure that there are write rpcs in flight to wait for. this
1508 * is a little silly as this object may not have any pending
1509 * but other objects sure might. this should probably be cleaned. */
1510 loi_onto_ready_list(cli, loi);
1511 osc_check_rpcs(cli);
1512 spin_unlock(&cli->cl_loi_list_lock);
1514 CDEBUG(D_INODE, "sleeping for cache space\n");
1515 l_wait_event(ocw.ocw_waitq, ocw_granted(cli, &ocw), &lwi);
1517 spin_lock(&cli->cl_loi_list_lock);
1518 if (!list_empty(&ocw.ocw_entry)) {
1520 list_del(&ocw.ocw_entry);
1525 oap->oap_brw_flags |= OBD_BRW_FROM_GRANT;
1529 /* the companion to enter_cache, called when an oap is now longer part of the
1530 * dirty accounting.. so writeback completes or truncate happens before writing
1531 * starts. must be called with the loi lock held. */
1532 static void osc_exit_cache(struct client_obd *cli, struct osc_async_page *oap)
1534 struct osc_cache_waiter *ocw;
1537 if (!(oap->oap_brw_flags & OBD_BRW_FROM_GRANT)) {
1542 if (list_empty(&cli->cl_cache_waiters)) {
1543 cli->cl_dirty -= PAGE_SIZE;
1545 ocw = list_entry(cli->cl_cache_waiters.next,
1546 struct osc_cache_waiter, ocw_entry);
1547 list_del_init(&ocw->ocw_entry);
1548 wake_up(&ocw->ocw_waitq);
1551 oap->oap_brw_flags &= ~OBD_BRW_FROM_GRANT;
1555 int osc_prep_async_page(struct obd_export *exp, struct lov_stripe_md *lsm,
1556 struct lov_oinfo *loi, struct page *page,
1557 obd_off offset, struct obd_async_page_ops *ops,
1558 void *data, void **res)
1560 struct osc_async_page *oap;
1563 OBD_ALLOC(oap, sizeof(*oap));
1567 oap->oap_magic = OAP_MAGIC;
1568 oap->oap_caller_ops = ops;
1569 oap->oap_caller_data = data;
1571 oap->oap_page = page;
1572 oap->oap_obj_off = offset;
1574 INIT_LIST_HEAD(&oap->oap_pending_item);
1575 INIT_LIST_HEAD(&oap->oap_urgent_item);
1576 INIT_LIST_HEAD(&oap->oap_rpc_item);
1578 CDEBUG(D_CACHE, "oap %p page %p obj off "LPU64"\n", oap, page, offset);
1583 struct osc_async_page *oap_from_cookie(void *cookie)
1585 struct osc_async_page *oap = cookie;
1586 if (oap->oap_magic != OAP_MAGIC)
1587 return ERR_PTR(-EINVAL);
1591 static int osc_queue_async_io(struct obd_export *exp, struct lov_stripe_md *lsm,
1592 struct lov_oinfo *loi, void *cookie,
1593 int cmd, obd_off off, int count,
1594 obd_flag brw_flags, enum async_flags async_flags)
1596 struct client_obd *cli = &exp->exp_obd->u.cli;
1597 struct osc_async_page *oap;
1598 struct loi_oap_pages *lop;
1602 oap = oap_from_cookie(cookie);
1604 RETURN(PTR_ERR(oap));
1606 if (cli->cl_import == NULL || cli->cl_import->imp_invalid)
1609 if (!list_empty(&oap->oap_pending_item) ||
1610 !list_empty(&oap->oap_urgent_item) ||
1611 !list_empty(&oap->oap_rpc_item))
1615 loi = &lsm->lsm_oinfo[0];
1617 spin_lock(&cli->cl_loi_list_lock);
1620 oap->oap_async_flags = async_flags;
1621 oap->oap_page_off = off;
1622 oap->oap_count = count;
1623 oap->oap_brw_flags = brw_flags;
1625 if (cmd == OBD_BRW_WRITE) {
1626 rc = osc_enter_cache(cli, loi, oap);
1628 spin_unlock(&cli->cl_loi_list_lock);
1631 lop = &loi->loi_write_lop;
1633 lop = &loi->loi_read_lop;
1636 if (oap->oap_async_flags & ASYNC_URGENT)
1637 list_add(&oap->oap_urgent_item, &lop->lop_urgent);
1638 list_add_tail(&oap->oap_pending_item, &lop->lop_pending);
1639 lop_update_pending(cli, lop, cmd, 1);
1641 loi_onto_ready_list(cli, loi);
1643 LOI_DEBUG(loi, "oap %p page %p added for cmd %d\n", oap, oap->oap_page,
1646 osc_check_rpcs(cli);
1647 spin_unlock(&cli->cl_loi_list_lock);
1652 /* aka (~was & now & flag), but this is more clear :) */
1653 #define SETTING(was, now, flag) (!(was & flag) && (now & flag))
1655 static int osc_set_async_flags(struct obd_export *exp,
1656 struct lov_stripe_md *lsm,
1657 struct lov_oinfo *loi, void *cookie,
1658 obd_flag async_flags)
1660 struct client_obd *cli = &exp->exp_obd->u.cli;
1661 struct loi_oap_pages *lop;
1662 struct osc_async_page *oap;
1666 oap = oap_from_cookie(cookie);
1668 RETURN(PTR_ERR(oap));
1670 if (cli->cl_import == NULL || cli->cl_import->imp_invalid)
1674 loi = &lsm->lsm_oinfo[0];
1676 if (oap->oap_cmd == OBD_BRW_WRITE) {
1677 lop = &loi->loi_write_lop;
1679 lop = &loi->loi_read_lop;
1682 spin_lock(&cli->cl_loi_list_lock);
1684 if (oap->oap_async_flags == async_flags)
1687 if (SETTING(oap->oap_async_flags, async_flags, ASYNC_READY))
1688 oap->oap_async_flags |= ASYNC_READY;
1690 if (SETTING(oap->oap_async_flags, async_flags, ASYNC_URGENT)) {
1691 if (list_empty(&oap->oap_rpc_item)) {
1692 list_add(&oap->oap_urgent_item, &lop->lop_urgent);
1693 loi_onto_ready_list(cli, loi);
1697 LOI_DEBUG(loi, "oap %p page %p has flags %x\n", oap, oap->oap_page,
1698 oap->oap_async_flags);
1700 osc_check_rpcs(cli);
1701 spin_unlock(&cli->cl_loi_list_lock);
1705 static int osc_queue_sync_io(struct obd_export *exp, struct lov_stripe_md *lsm,
1706 struct lov_oinfo *loi,
1707 struct obd_sync_io_container *osic, void *cookie,
1708 int cmd, obd_off off, int count,
1711 struct client_obd *cli = &exp->exp_obd->u.cli;
1712 struct osc_async_page *oap;
1713 struct loi_oap_pages *lop;
1716 oap = oap_from_cookie(cookie);
1718 RETURN(PTR_ERR(oap));
1720 if (cli->cl_import == NULL || cli->cl_import->imp_invalid)
1723 if (!list_empty(&oap->oap_pending_item) ||
1724 !list_empty(&oap->oap_urgent_item) ||
1725 !list_empty(&oap->oap_rpc_item))
1729 loi = &lsm->lsm_oinfo[0];
1731 spin_lock(&cli->cl_loi_list_lock);
1734 oap->oap_page_off = off;
1735 oap->oap_count = count;
1736 oap->oap_brw_flags = brw_flags;
1738 if (cmd == OBD_BRW_WRITE)
1739 lop = &loi->loi_write_lop;
1741 lop = &loi->loi_read_lop;
1743 list_add_tail(&oap->oap_pending_item, &lop->lop_pending_sync);
1744 oap->oap_osic = osic;
1747 LOI_DEBUG(loi, "oap %p page %p on sync pending\n", oap, oap->oap_page);
1749 spin_unlock(&cli->cl_loi_list_lock);
1754 static void osc_sync_to_pending(struct client_obd *cli, struct lov_oinfo *loi,
1755 struct loi_oap_pages *lop, int cmd)
1757 struct list_head *pos, *tmp;
1758 struct osc_async_page *oap;
1760 list_for_each_safe(pos, tmp, &lop->lop_pending_sync) {
1761 oap = list_entry(pos, struct osc_async_page, oap_pending_item);
1762 list_del(&oap->oap_pending_item);
1763 oap->oap_async_flags |= ASYNC_READY | ASYNC_URGENT |
1765 list_add_tail(&oap->oap_pending_item, &lop->lop_pending);
1766 list_add(&oap->oap_urgent_item, &lop->lop_urgent);
1767 lop_update_pending(cli, lop, cmd, 1);
1769 loi_onto_ready_list(cli, loi);
1772 static int osc_trigger_sync_io(struct obd_export *exp,
1773 struct lov_stripe_md *lsm,
1774 struct lov_oinfo *loi,
1775 struct obd_sync_io_container *osic)
1777 struct client_obd *cli = &exp->exp_obd->u.cli;
1780 if (cli->cl_import == NULL || cli->cl_import->imp_invalid)
1784 loi = &lsm->lsm_oinfo[0];
1786 spin_lock(&cli->cl_loi_list_lock);
1788 osc_sync_to_pending(cli, loi, &loi->loi_write_lop, OBD_BRW_WRITE);
1789 osc_sync_to_pending(cli, loi, &loi->loi_read_lop, OBD_BRW_READ);
1791 osc_check_rpcs(cli);
1792 spin_unlock(&cli->cl_loi_list_lock);
1797 static int osc_teardown_async_page(struct obd_export *exp,
1798 struct lov_stripe_md *lsm,
1799 struct lov_oinfo *loi, void *cookie)
1801 struct client_obd *cli = &exp->exp_obd->u.cli;
1802 struct loi_oap_pages *lop;
1803 struct osc_async_page *oap;
1807 oap = oap_from_cookie(cookie);
1809 RETURN(PTR_ERR(oap));
1812 loi = &lsm->lsm_oinfo[0];
1814 if (oap->oap_cmd == OBD_BRW_WRITE) {
1815 lop = &loi->loi_write_lop;
1817 lop = &loi->loi_read_lop;
1820 spin_lock(&cli->cl_loi_list_lock);
1822 osc_exit_cache(cli, oap);
1824 if (!list_empty(&oap->oap_rpc_item))
1825 GOTO(out, rc = -EBUSY);
1827 if (!list_empty(&oap->oap_urgent_item)) {
1828 list_del_init(&oap->oap_urgent_item);
1829 oap->oap_async_flags &= ~ASYNC_URGENT;
1831 if (!list_empty(&oap->oap_pending_item)) {
1832 list_del_init(&oap->oap_pending_item);
1833 lop_update_pending(cli, lop, oap->oap_cmd, -1);
1835 if (!list_empty(&loi->loi_cli_item) && !loi_makes_rpc(cli, loi))
1836 list_del_init(&loi->loi_cli_item);
1838 LOI_DEBUG(loi, "oap %p page %p torn down\n", oap, oap->oap_page);
1840 spin_unlock(&cli->cl_loi_list_lock);
1841 OBD_FREE(oap, sizeof(*oap));
1846 /* Note: caller will lock/unlock, and set uptodate on the pages */
1847 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
1848 static int sanosc_brw_read(struct obd_export *exp, struct obdo *oa,
1849 struct lov_stripe_md *lsm, obd_count page_count,
1850 struct brw_page *pga)
1852 struct ptlrpc_request *request = NULL;
1853 struct ost_body *body;
1854 struct niobuf_remote *nioptr;
1855 struct obd_ioobj *iooptr;
1856 int rc, size[3] = {sizeof(*body)}, mapped = 0;
1860 /* XXX does not handle 'new' brw protocol */
1862 size[1] = sizeof(struct obd_ioobj);
1863 size[2] = page_count * sizeof(*nioptr);
1865 request = ptlrpc_prep_req(class_exp2cliimp(exp), OST_SAN_READ, 3,
1870 body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof(*body));
1871 iooptr = lustre_msg_buf(request->rq_reqmsg, 1, sizeof(*iooptr));
1872 nioptr = lustre_msg_buf(request->rq_reqmsg, 2,
1873 sizeof(*nioptr) * page_count);
1875 memcpy(&body->oa, oa, sizeof(body->oa));
1877 obdo_to_ioobj(oa, iooptr);
1878 iooptr->ioo_bufcnt = page_count;
1880 for (mapped = 0; mapped < page_count; mapped++, nioptr++) {
1881 LASSERT(PageLocked(pga[mapped].pg));
1882 LASSERT(mapped == 0 || pga[mapped].off > pga[mapped - 1].off);
1884 nioptr->offset = pga[mapped].off;
1885 nioptr->len = pga[mapped].count;
1886 nioptr->flags = pga[mapped].flag;
1889 size[1] = page_count * sizeof(*nioptr);
1890 request->rq_replen = lustre_msg_size(2, size);
1892 rc = ptlrpc_queue_wait(request);
1896 body = lustre_swab_repbuf(request, 0, sizeof(*body),
1897 lustre_swab_ost_body);
1899 CERROR("Can't unpack body\n");
1900 GOTO(out_req, rc = -EPROTO);
1903 memcpy(oa, &body->oa, sizeof(*oa));
1905 swab = lustre_msg_swabbed(request->rq_repmsg);
1906 LASSERT_REPSWAB(request, 1);
1907 nioptr = lustre_msg_buf(request->rq_repmsg, 1, size[1]);
1909 /* nioptr missing or short */
1910 GOTO(out_req, rc = -EPROTO);
1914 for (mapped = 0; mapped < page_count; mapped++, nioptr++) {
1915 struct page *page = pga[mapped].pg;
1916 struct buffer_head *bh;
1920 lustre_swab_niobuf_remote (nioptr);
1922 /* got san device associated */
1923 LASSERT(exp->exp_obd != NULL);
1924 dev = exp->exp_obd->u.cli.cl_sandev;
1927 if (!nioptr->offset) {
1928 CDEBUG(D_PAGE, "hole at ino %lu; index %ld\n",
1929 page->mapping->host->i_ino,
1931 memset(page_address(page), 0, PAGE_SIZE);
1935 if (!page->buffers) {
1936 create_empty_buffers(page, dev, PAGE_SIZE);
1939 clear_bit(BH_New, &bh->b_state);
1940 set_bit(BH_Mapped, &bh->b_state);
1941 bh->b_blocknr = (unsigned long)nioptr->offset;
1943 clear_bit(BH_Uptodate, &bh->b_state);
1945 ll_rw_block(READ, 1, &bh);
1949 /* if buffer already existed, it must be the
1950 * one we mapped before, check it */
1951 LASSERT(!test_bit(BH_New, &bh->b_state));
1952 LASSERT(test_bit(BH_Mapped, &bh->b_state));
1953 LASSERT(bh->b_blocknr == (unsigned long)nioptr->offset);
1955 /* wait it's io completion */
1956 if (test_bit(BH_Lock, &bh->b_state))
1959 if (!test_bit(BH_Uptodate, &bh->b_state))
1960 ll_rw_block(READ, 1, &bh);
1964 /* must do syncronous write here */
1966 if (!buffer_uptodate(bh)) {
1974 ptlrpc_req_finished(request);
1978 static int sanosc_brw_write(struct obd_export *exp, struct obdo *oa,
1979 struct lov_stripe_md *lsm, obd_count page_count,
1980 struct brw_page *pga)
1982 struct ptlrpc_request *request = NULL;
1983 struct ost_body *body;
1984 struct niobuf_remote *nioptr;
1985 struct obd_ioobj *iooptr;
1986 int rc, size[3] = {sizeof(*body)}, mapped = 0;
1990 size[1] = sizeof(struct obd_ioobj);
1991 size[2] = page_count * sizeof(*nioptr);
1993 request = ptlrpc_prep_req(class_exp2cliimp(exp), OST_SAN_WRITE,
1998 body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof (*body));
1999 iooptr = lustre_msg_buf(request->rq_reqmsg, 1, sizeof (*iooptr));
2000 nioptr = lustre_msg_buf(request->rq_reqmsg, 2,
2001 sizeof (*nioptr) * page_count);
2003 memcpy(&body->oa, oa, sizeof(body->oa));
2005 obdo_to_ioobj(oa, iooptr);
2006 iooptr->ioo_bufcnt = page_count;
2009 for (mapped = 0; mapped < page_count; mapped++, nioptr++) {
2010 LASSERT(PageLocked(pga[mapped].pg));
2011 LASSERT(mapped == 0 || pga[mapped].off > pga[mapped - 1].off);
2013 nioptr->offset = pga[mapped].off;
2014 nioptr->len = pga[mapped].count;
2015 nioptr->flags = pga[mapped].flag;
2018 size[1] = page_count * sizeof(*nioptr);
2019 request->rq_replen = lustre_msg_size(2, size);
2021 rc = ptlrpc_queue_wait(request);
2025 swab = lustre_msg_swabbed (request->rq_repmsg);
2026 LASSERT_REPSWAB (request, 1);
2027 nioptr = lustre_msg_buf(request->rq_repmsg, 1, size[1]);
2029 CERROR("absent/short niobuf array\n");
2030 GOTO(out_req, rc = -EPROTO);
2034 for (mapped = 0; mapped < page_count; mapped++, nioptr++) {
2035 struct page *page = pga[mapped].pg;
2036 struct buffer_head *bh;
2040 lustre_swab_niobuf_remote (nioptr);
2042 /* got san device associated */
2043 LASSERT(exp->exp_obd != NULL);
2044 dev = exp->exp_obd->u.cli.cl_sandev;
2046 if (!page->buffers) {
2047 create_empty_buffers(page, dev, PAGE_SIZE);
2050 LASSERT(!test_bit(BH_New, &page->buffers->b_state));
2051 LASSERT(test_bit(BH_Mapped, &page->buffers->b_state));
2052 LASSERT(page->buffers->b_blocknr ==
2053 (unsigned long)nioptr->offset);
2059 /* if buffer locked, wait it's io completion */
2060 if (test_bit(BH_Lock, &bh->b_state))
2063 clear_bit(BH_New, &bh->b_state);
2064 set_bit(BH_Mapped, &bh->b_state);
2066 /* override the block nr */
2067 bh->b_blocknr = (unsigned long)nioptr->offset;
2069 /* we are about to write it, so set it
2071 * page lock should garentee no race condition here */
2072 set_bit(BH_Uptodate, &bh->b_state);
2073 set_bit(BH_Dirty, &bh->b_state);
2075 ll_rw_block(WRITE, 1, &bh);
2077 /* must do syncronous write here */
2079 if (!buffer_uptodate(bh) || test_bit(BH_Dirty, &bh->b_state)) {
2087 ptlrpc_req_finished(request);
2091 static int sanosc_brw(int cmd, struct obd_export *exp, struct obdo *oa,
2092 struct lov_stripe_md *lsm, obd_count page_count,
2093 struct brw_page *pga, struct obd_trans_info *oti)
2097 while (page_count) {
2098 obd_count pages_per_brw;
2101 if (page_count > OSC_BRW_MAX_IOV)
2102 pages_per_brw = OSC_BRW_MAX_IOV;
2104 pages_per_brw = page_count;
2106 if (cmd & OBD_BRW_WRITE)
2107 rc = sanosc_brw_write(exp, oa, lsm, pages_per_brw,pga);
2109 rc = sanosc_brw_read(exp, oa, lsm, pages_per_brw, pga);
2114 page_count -= pages_per_brw;
2115 pga += pages_per_brw;
2122 static void osc_set_data_with_check(struct lustre_handle *lockh, void *data)
2124 struct ldlm_lock *lock = ldlm_handle2lock(lockh);
2126 LASSERT(lock != NULL);
2127 l_lock(&lock->l_resource->lr_namespace->ns_lock);
2129 if (lock->l_ast_data && lock->l_ast_data != data) {
2130 struct inode *new_inode = data;
2131 struct inode *old_inode = lock->l_ast_data;
2132 unsigned long state = old_inode->i_state & I_FREEING;
2133 CERROR("Found existing inode %p/%lu/%u state %lu in lock: "
2134 "setting data to %p/%lu/%u\n", old_inode,
2135 old_inode->i_ino, old_inode->i_generation, state,
2136 new_inode, new_inode->i_ino, new_inode->i_generation);
2140 lock->l_ast_data = data;
2141 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
2142 LDLM_LOCK_PUT(lock);
2145 static int osc_change_cbdata(struct obd_export *exp, struct lov_stripe_md *lsm,
2146 ldlm_iterator_t replace, void *data)
2148 struct ldlm_res_id res_id = { .name = {lsm->lsm_object_id} };
2149 struct obd_device *obd = class_exp2obd(exp);
2151 ldlm_change_cbdata(obd->obd_namespace, &res_id, replace, data);
2155 static int osc_enqueue(struct obd_export *exp, struct lov_stripe_md *lsm,
2156 struct lustre_handle *parent_lock,
2157 __u32 type, void *extentp, int extent_len, __u32 mode,
2158 int *flags, void *callback, void *data,
2159 struct lustre_handle *lockh)
2161 struct ldlm_res_id res_id = { .name = {lsm->lsm_object_id} };
2162 struct obd_device *obd = exp->exp_obd;
2163 struct ldlm_extent *extent = extentp;
2167 /* Filesystem lock extents are extended to page boundaries so that
2168 * dealing with the page cache is a little smoother. */
2169 extent->start -= extent->start & ~PAGE_MASK;
2170 extent->end |= ~PAGE_MASK;
2172 /* Next, search for already existing extent locks that will cover us */
2173 rc = ldlm_lock_match(obd->obd_namespace, 0, &res_id,
2174 type, extent, sizeof(*extent), mode, lockh);
2176 osc_set_data_with_check(lockh, data);
2177 /* We already have a lock, and it's referenced */
2181 /* If we're trying to read, we also search for an existing PW lock. The
2182 * VFS and page cache already protect us locally, so lots of readers/
2183 * writers can share a single PW lock.
2185 * There are problems with conversion deadlocks, so instead of
2186 * converting a read lock to a write lock, we'll just enqueue a new
2189 * At some point we should cancel the read lock instead of making them
2190 * send us a blocking callback, but there are problems with canceling
2191 * locks out from other users right now, too. */
2193 if (mode == LCK_PR) {
2194 rc = ldlm_lock_match(obd->obd_namespace, 0, &res_id, type,
2195 extent, sizeof(*extent), LCK_PW, lockh);
2197 /* FIXME: This is not incredibly elegant, but it might
2198 * be more elegant than adding another parameter to
2199 * lock_match. I want a second opinion. */
2200 ldlm_lock_addref(lockh, LCK_PR);
2201 ldlm_lock_decref(lockh, LCK_PW);
2202 osc_set_data_with_check(lockh, data);
2207 rc = ldlm_cli_enqueue(exp, NULL, obd->obd_namespace, parent_lock,
2208 res_id, type, extent, sizeof(*extent), mode,
2209 flags,ldlm_completion_ast, callback, data, lockh);
2213 static int osc_match(struct obd_export *exp, struct lov_stripe_md *lsm,
2214 __u32 type, void *extentp, int extent_len, __u32 mode,
2215 int *flags, void *data, struct lustre_handle *lockh)
2217 struct ldlm_res_id res_id = { .name = {lsm->lsm_object_id} };
2218 struct obd_device *obd = exp->exp_obd;
2219 struct ldlm_extent *extent = extentp;
2223 OBD_FAIL_RETURN(OBD_FAIL_OSC_MATCH, -EIO);
2225 /* Filesystem lock extents are extended to page boundaries so that
2226 * dealing with the page cache is a little smoother */
2227 extent->start -= extent->start & ~PAGE_MASK;
2228 extent->end |= ~PAGE_MASK;
2230 /* Next, search for already existing extent locks that will cover us */
2231 rc = ldlm_lock_match(obd->obd_namespace, *flags, &res_id, type,
2232 extent, sizeof(*extent), mode, lockh);
2234 osc_set_data_with_check(lockh, data);
2237 /* If we're trying to read, we also search for an existing PW lock. The
2238 * VFS and page cache already protect us locally, so lots of readers/
2239 * writers can share a single PW lock. */
2240 if (mode == LCK_PR) {
2241 rc = ldlm_lock_match(obd->obd_namespace, *flags, &res_id, type,
2242 extent, sizeof(*extent), LCK_PW, lockh);
2244 /* FIXME: This is not incredibly elegant, but it might
2245 * be more elegant than adding another parameter to
2246 * lock_match. I want a second opinion. */
2247 osc_set_data_with_check(lockh, data);
2248 ldlm_lock_addref(lockh, LCK_PR);
2249 ldlm_lock_decref(lockh, LCK_PW);
2255 static int osc_cancel(struct obd_export *exp, struct lov_stripe_md *md,
2256 __u32 mode, struct lustre_handle *lockh)
2260 ldlm_lock_decref(lockh, mode);
2265 static int osc_cancel_unused(struct obd_export *exp,
2266 struct lov_stripe_md *lsm, int flags, void *opaque)
2268 struct obd_device *obd = class_exp2obd(exp);
2269 struct ldlm_res_id res_id = { .name = {lsm->lsm_object_id} };
2271 return ldlm_cli_cancel_unused(obd->obd_namespace, &res_id, flags,
2275 static int osc_statfs(struct obd_device *obd, struct obd_statfs *osfs,
2276 unsigned long max_age)
2278 struct obd_statfs *msfs;
2279 struct ptlrpc_request *request;
2280 int rc, size = sizeof(*osfs);
2283 /* We could possibly pass max_age in the request (as an absolute
2284 * timestamp or a "seconds.usec ago") so the target can avoid doing
2285 * extra calls into the filesystem if that isn't necessary (e.g.
2286 * during mount that would help a bit). Having relative timestamps
2287 * is not so great if request processing is slow, while absolute
2288 * timestamps are not ideal because they need time synchronization. */
2289 request = ptlrpc_prep_req(obd->u.cli.cl_import, OST_STATFS,0,NULL,NULL);
2293 request->rq_replen = lustre_msg_size(1, &size);
2294 request->rq_request_portal = OST_CREATE_PORTAL; //XXX FIXME bug 249
2296 rc = ptlrpc_queue_wait(request);
2300 msfs = lustre_swab_repbuf(request, 0, sizeof(*msfs),
2301 lustre_swab_obd_statfs);
2303 CERROR("Can't unpack obd_statfs\n");
2304 GOTO(out, rc = -EPROTO);
2307 memcpy(osfs, msfs, sizeof(*osfs));
2311 ptlrpc_req_finished(request);
2315 /* Retrieve object striping information.
2317 * @lmmu is a pointer to an in-core struct with lmm_ost_count indicating
2318 * the maximum number of OST indices which will fit in the user buffer.
2319 * lmm_magic must be LOV_MAGIC (we only use 1 slot here).
2321 static int osc_getstripe(struct lov_stripe_md *lsm, struct lov_user_md *lump)
2323 struct lov_user_md lum;
2324 struct lov_mds_md *lmmk;
2331 rc = copy_from_user(&lum, lump, sizeof(lum));
2335 if (lum.lmm_magic != LOV_USER_MAGIC)
2338 if (lum.lmm_stripe_count < 1)
2341 lmm_size = sizeof(lum) + sizeof(lum.lmm_objects[0]);
2342 OBD_ALLOC(lmmk, lmm_size);
2346 lmmk->lmm_stripe_count = 1;
2347 lmmk->lmm_object_id = lsm->lsm_object_id;
2348 lmmk->lmm_objects[0].l_object_id = lsm->lsm_object_id;
2350 if (copy_to_user(lump, lmmk, lmm_size))
2353 OBD_FREE(lmmk, lmm_size);
2358 static int osc_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
2359 void *karg, void *uarg)
2361 struct obd_device *obd = exp->exp_obd;
2362 struct obd_ioctl_data *data = karg;
2367 case OBD_IOC_LOV_GET_CONFIG: {
2369 struct lov_desc *desc;
2370 struct obd_uuid uuid;
2374 if (obd_ioctl_getdata(&buf, &len, (void *)uarg))
2375 GOTO(out, err = -EINVAL);
2377 data = (struct obd_ioctl_data *)buf;
2379 if (sizeof(*desc) > data->ioc_inllen1) {
2381 GOTO(out, err = -EINVAL);
2384 if (data->ioc_inllen2 < sizeof(uuid)) {
2386 GOTO(out, err = -EINVAL);
2389 desc = (struct lov_desc *)data->ioc_inlbuf1;
2390 desc->ld_tgt_count = 1;
2391 desc->ld_active_tgt_count = 1;
2392 desc->ld_default_stripe_count = 1;
2393 desc->ld_default_stripe_size = 0;
2394 desc->ld_default_stripe_offset = 0;
2395 desc->ld_pattern = 0;
2396 memcpy(&desc->ld_uuid, &obd->obd_uuid, sizeof(uuid));
2398 memcpy(data->ioc_inlbuf2, &obd->obd_uuid, sizeof(uuid));
2400 err = copy_to_user((void *)uarg, buf, len);
2403 obd_ioctl_freedata(buf, len);
2406 case LL_IOC_LOV_SETSTRIPE:
2407 err = obd_alloc_memmd(exp, karg);
2411 case LL_IOC_LOV_GETSTRIPE:
2412 err = osc_getstripe(karg, uarg);
2414 case OBD_IOC_CLIENT_RECOVER:
2415 err = ptlrpc_recover_import(obd->u.cli.cl_import,
2420 case IOC_OSC_SET_ACTIVE:
2421 err = ptlrpc_set_import_active(obd->u.cli.cl_import,
2425 CDEBUG(D_INODE, "unrecognised ioctl %#x by %s\n", cmd, current->comm);
2426 GOTO(out, err = -ENOTTY);
2432 static int osc_get_info(struct obd_export *exp, obd_count keylen,
2433 void *key, __u32 *vallen, void *val)
2436 if (!vallen || !val)
2439 if (keylen > strlen("lock_to_stripe") &&
2440 strcmp(key, "lock_to_stripe") == 0) {
2441 __u32 *stripe = val;
2442 *vallen = sizeof(*stripe);
2445 } else if (keylen >= strlen("last_id") && strcmp(key, "last_id") == 0) {
2446 struct ptlrpc_request *req;
2448 char *bufs[1] = {key};
2450 req = ptlrpc_prep_req(class_exp2cliimp(exp), OST_GET_INFO, 1,
2455 req->rq_replen = lustre_msg_size(1, vallen);
2456 rc = ptlrpc_queue_wait(req);
2460 reply = lustre_swab_repbuf(req, 0, sizeof(*reply),
2461 lustre_swab_ost_last_id);
2462 if (reply == NULL) {
2463 CERROR("Can't unpack OST last ID\n");
2464 GOTO(out, rc = -EPROTO);
2466 *((obd_id *)val) = *reply;
2468 ptlrpc_req_finished(req);
2474 static int osc_set_info(struct obd_export *exp, obd_count keylen,
2475 void *key, obd_count vallen, void *val)
2477 struct ptlrpc_request *req;
2478 struct obd_import *imp = class_exp2cliimp(exp);
2479 struct llog_ctxt *ctxt;
2480 int rc, size = keylen;
2481 char *bufs[1] = {key};
2484 if (keylen == strlen("next_id") &&
2485 memcmp(key, "next_id", strlen("next_id")) == 0) {
2486 if (vallen != sizeof(obd_id))
2488 exp->u.eu_osc_data.oed_oscc.oscc_next_id = *((obd_id*)val) + 1;
2489 CDEBUG(D_INODE, "%s: set oscc_next_id = "LPU64"\n",
2490 exp->exp_obd->obd_name,
2491 exp->u.eu_osc_data.oed_oscc.oscc_next_id);
2496 if (keylen == strlen("growth_count") &&
2497 memcmp(key, "growth_count", strlen("growth_count")) == 0) {
2498 if (vallen != sizeof(int))
2500 exp->u.eu_osc_data.oed_oscc.oscc_grow_count = *((int*)val);
2504 if (keylen == strlen("unlinked") &&
2505 memcmp(key, "unlinked", keylen) == 0) {
2506 struct osc_creator *oscc = &exp->u.eu_osc_data.oed_oscc;
2507 spin_lock(&oscc->oscc_lock);
2508 oscc->oscc_flags &= ~OSCC_FLAG_NOSPC;
2509 spin_unlock(&oscc->oscc_lock);
2513 if (keylen < strlen("mds_conn") ||
2514 memcmp(key, "mds_conn", strlen("mds_conn")) != 0)
2518 req = ptlrpc_prep_req(imp, OST_SET_INFO, 1, &size, bufs);
2522 req->rq_replen = lustre_msg_size(0, NULL);
2523 rc = ptlrpc_queue_wait(req);
2524 ptlrpc_req_finished(req);
2526 ctxt = llog_get_context(exp->exp_obd, LLOG_UNLINK_ORIG_CTXT);
2528 rc = llog_initiator_connect(ctxt);
2533 imp->imp_server_timeout = 1;
2534 CDEBUG(D_HA, "pinging OST %s\n", imp->imp_target_uuid.uuid);
2535 ptlrpc_pinger_add_import(imp);
2541 static struct llog_operations osc_size_repl_logops = {
2542 lop_cancel: llog_obd_repl_cancel
2545 static struct llog_operations osc_unlink_orig_logops;
2546 static int osc_llog_init(struct obd_device *obd, struct obd_device *tgt,
2547 int count, struct llog_logid *logid)
2552 osc_unlink_orig_logops = llog_lvfs_ops;
2553 osc_unlink_orig_logops.lop_setup = llog_obd_origin_setup;
2554 osc_unlink_orig_logops.lop_cleanup = llog_obd_origin_cleanup;
2555 osc_unlink_orig_logops.lop_add = llog_obd_origin_add;
2556 osc_unlink_orig_logops.lop_connect = llog_origin_connect;
2558 rc = llog_setup(obd, LLOG_UNLINK_ORIG_CTXT, tgt, count, logid,
2559 &osc_unlink_orig_logops);
2563 rc = llog_setup(obd, LLOG_SIZE_REPL_CTXT, tgt, count, NULL,
2564 &osc_size_repl_logops);
2568 static int osc_llog_finish(struct obd_device *obd, int count)
2573 rc = llog_cleanup(llog_get_context(obd, LLOG_UNLINK_ORIG_CTXT));
2577 rc = llog_cleanup(llog_get_context(obd, LLOG_SIZE_REPL_CTXT));
2582 static int osc_connect(struct lustre_handle *exph,
2583 struct obd_device *obd, struct obd_uuid *cluuid)
2586 struct obd_export *exp;
2588 rc = client_connect_import(exph, obd, cluuid);
2590 if (obd->u.cli.cl_conn_count == 1) {
2591 exp = class_conn2export(exph);
2598 static int osc_disconnect(struct obd_export *exp, int flags)
2600 struct obd_device *obd = class_exp2obd(exp);
2601 struct llog_ctxt *ctxt = llog_get_context(obd, LLOG_SIZE_REPL_CTXT);
2604 if (obd->u.cli.cl_conn_count == 1) {
2605 /* flush any remaining cancel messages out to the target */
2606 llog_sync(ctxt, exp);
2608 /* balance the conn2export for oscc in osc_connect */
2609 class_export_put(exp);
2612 rc = client_disconnect_export(exp, flags);
2616 static int osc_lock_contains(struct obd_export *exp, struct lov_stripe_md *lsm,
2617 struct ldlm_lock *lock, obd_off offset)
2623 if (lock->l_policy_data.l_extent.start <= offset &&
2624 lock->l_policy_data.l_extent.end >= offset)
2629 static int osc_invalidate_import(struct obd_device *obd,
2630 struct obd_import *imp)
2632 LASSERT(imp->imp_obd == obd);
2633 /* this used to try and tear down queued pages, but it was
2634 * not correctly implemented. We'll have to do it again once
2635 * we call obd_invalidate_import() agian */
2640 int osc_setup(struct obd_device *obd, obd_count len, void *buf)
2644 rc = ptlrpcd_addref();
2648 rc = client_obd_setup(obd, len, buf);
2654 int osc_cleanup(struct obd_device *obd, int flags)
2658 rc = client_obd_cleanup(obd, flags);
2664 struct obd_ops osc_obd_ops = {
2665 o_owner: THIS_MODULE,
2666 o_attach: osc_attach,
2667 o_detach: osc_detach,
2669 o_cleanup: osc_cleanup,
2670 o_connect: osc_connect,
2671 o_disconnect: osc_disconnect,
2672 o_statfs: osc_statfs,
2673 o_packmd: osc_packmd,
2674 o_unpackmd: osc_unpackmd,
2675 o_create: osc_create,
2676 o_destroy: osc_destroy,
2677 o_getattr: osc_getattr,
2678 o_getattr_async:osc_getattr_async,
2679 o_setattr: osc_setattr,
2681 o_brw_async: osc_brw_async,
2682 .o_prep_async_page = osc_prep_async_page,
2683 .o_queue_async_io = osc_queue_async_io,
2684 .o_set_async_flags = osc_set_async_flags,
2685 .o_queue_sync_io = osc_queue_sync_io,
2686 .o_trigger_sync_io = osc_trigger_sync_io,
2687 .o_teardown_async_page = osc_teardown_async_page,
2690 o_enqueue: osc_enqueue,
2692 o_change_cbdata:osc_change_cbdata,
2693 o_cancel: osc_cancel,
2694 o_cancel_unused:osc_cancel_unused,
2695 o_iocontrol: osc_iocontrol,
2696 o_get_info: osc_get_info,
2697 o_set_info: osc_set_info,
2698 o_lock_contains:osc_lock_contains,
2699 o_invalidate_import: osc_invalidate_import,
2700 o_llog_init: osc_llog_init,
2701 o_llog_finish: osc_llog_finish,
2704 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
2705 struct obd_ops sanosc_obd_ops = {
2706 o_owner: THIS_MODULE,
2707 o_attach: osc_attach,
2708 o_detach: osc_detach,
2709 o_cleanup: client_obd_cleanup,
2710 o_connect: osc_connect,
2711 o_disconnect: client_disconnect_export,
2712 o_statfs: osc_statfs,
2713 o_packmd: osc_packmd,
2714 o_unpackmd: osc_unpackmd,
2715 o_create: osc_real_create,
2716 o_destroy: osc_destroy,
2717 o_getattr: osc_getattr,
2718 o_getattr_async:osc_getattr_async,
2719 o_setattr: osc_setattr,
2720 o_setup: client_sanobd_setup,
2724 o_enqueue: osc_enqueue,
2726 o_change_cbdata:osc_change_cbdata,
2727 o_cancel: osc_cancel,
2728 o_cancel_unused:osc_cancel_unused,
2729 o_iocontrol: osc_iocontrol,
2730 o_lock_contains:osc_lock_contains,
2731 o_invalidate_import: osc_invalidate_import,
2732 o_llog_init: osc_llog_init,
2733 o_llog_finish: osc_llog_finish,
2737 int __init osc_init(void)
2739 struct lprocfs_static_vars lvars, sanlvars;
2743 lprocfs_init_vars(osc, &lvars);
2744 lprocfs_init_vars(osc, &sanlvars);
2746 rc = class_register_type(&osc_obd_ops, lvars.module_vars,
2751 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
2752 rc = class_register_type(&sanosc_obd_ops, sanlvars.module_vars,
2753 LUSTRE_SANOSC_NAME);
2755 class_unregister_type(LUSTRE_OSC_NAME);
2761 static void /*__exit*/ osc_exit(void)
2763 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
2764 class_unregister_type(LUSTRE_SANOSC_NAME);
2766 class_unregister_type(LUSTRE_OSC_NAME);
2770 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
2771 MODULE_DESCRIPTION("Lustre Object Storage Client (OSC)");
2772 MODULE_LICENSE("GPL");
2774 module_init(osc_init);
2775 module_exit(osc_exit);