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 obdo *oa)
537 obd_flag bits = OBD_MD_FLBLOCKS|OBD_MD_FLGRANT;
539 LASSERT(!(oa->o_valid & bits));
542 spin_lock(&cli->cl_loi_list_lock);
543 oa->o_blocks = cli->cl_dirty;
544 oa->o_grant = cli->cl_dirty_granted;
545 spin_unlock(&cli->cl_loi_list_lock);
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_FLGRANT)) {
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_SUPER, "got "LPU64" grant\n", body->oa.o_grant);
562 spin_lock(&cli->cl_loi_list_lock);
563 if (cli->cl_dirty_granted != body->oa.o_grant) {
564 cli->cl_dirty_granted = body->oa.o_grant;
565 osc_adjust_cache(cli);
567 spin_unlock(&cli->cl_loi_list_lock);
570 /* We assume that the reason this OSC got a short read is because it read
571 * beyond the end of a stripe file; i.e. lustre is reading a sparse file
572 * via the LOV, and it _knows_ it's reading inside the file, it's just that
573 * this stripe never got written at or beyond this stripe offset yet. */
574 static void handle_short_read(int nob_read, obd_count page_count,
575 struct brw_page *pga)
579 /* skip bytes read OK */
580 while (nob_read > 0) {
581 LASSERT (page_count > 0);
583 if (pga->count > nob_read) {
584 /* EOF inside this page */
585 ptr = kmap(pga->pg) + (pga->off & ~PAGE_MASK);
586 memset(ptr + nob_read, 0, pga->count - nob_read);
593 nob_read -= pga->count;
598 /* zero remaining pages */
599 while (page_count-- > 0) {
600 ptr = kmap(pga->pg) + (pga->off & ~PAGE_MASK);
601 memset(ptr, 0, pga->count);
607 static int check_write_rcs(struct ptlrpc_request *request, int niocount,
608 obd_count page_count, struct brw_page *pga)
613 /* return error if any niobuf was in error */
614 remote_rcs = lustre_swab_repbuf(request, 1,
615 sizeof(*remote_rcs) * niocount, NULL);
616 if (remote_rcs == NULL) {
617 CERROR ("Missing/short RC vector on BRW_WRITE reply\n");
620 if (lustre_msg_swabbed (request->rq_repmsg))
621 for (i = 0; i < niocount; i++)
622 __swab32s (&remote_rcs[i]);
624 for (i = 0; i < niocount; i++) {
625 if (remote_rcs[i] < 0)
626 return (remote_rcs[i]);
628 if (remote_rcs[i] != 0) {
629 CERROR ("rc[%d] invalid (%d) req %p\n",
630 i, remote_rcs[i], request);
638 static inline int can_merge_pages(struct brw_page *p1, struct brw_page *p2)
640 if (p1->flag != p2->flag) {
641 unsigned mask = ~(OBD_BRW_CREATE|OBD_BRW_FROM_GRANT);
643 /* warn if we try to combine flags that we don't know to be
645 if ((p1->flag & mask) != (p2->flag & mask))
646 CERROR("is it ok to have flags 0x%x and 0x%x in the "
647 "same brw?\n", p1->flag, p2->flag);
651 return (p1->off + p1->count == p2->off);
655 static obd_count cksum_pages(int nob, obd_count page_count,
656 struct brw_page *pga)
662 LASSERT (page_count > 0);
665 ost_checksum(&cksum, ptr + (pga->off & (PAGE_SIZE - 1)),
666 pga->count > nob ? nob : pga->count);
678 static int osc_brw_prep_request(int cmd, struct obd_import *imp,struct obdo *oa,
679 struct lov_stripe_md *lsm, obd_count page_count,
680 struct brw_page *pga, int *requested_nobp,
681 int *niocountp, struct ptlrpc_request **reqp)
683 struct ptlrpc_request *req;
684 struct ptlrpc_bulk_desc *desc;
685 struct client_obd *cli = &imp->imp_obd->u.cli;
686 struct ost_body *body;
687 struct obd_ioobj *ioobj;
688 struct niobuf_remote *niobuf;
697 opc = ((cmd & OBD_BRW_WRITE) != 0) ? OST_WRITE : OST_READ;
699 for (niocount = i = 1; i < page_count; i++)
700 if (!can_merge_pages (&pga[i - 1], &pga[i]))
703 size[0] = sizeof(*body);
704 size[1] = sizeof(*ioobj);
705 size[2] = niocount * sizeof(*niobuf);
707 req = ptlrpc_prep_req(imp, opc, 3, size, NULL);
711 if (opc == OST_WRITE)
712 desc = ptlrpc_prep_bulk_imp(req, BULK_GET_SOURCE,
715 desc = ptlrpc_prep_bulk_imp(req, BULK_PUT_SINK,
718 GOTO(out, rc = -ENOMEM);
719 /* NB request now owns desc and will free it when it gets freed */
721 body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof(*body));
722 ioobj = lustre_msg_buf(req->rq_reqmsg, 1, sizeof(*ioobj));
723 niobuf = lustre_msg_buf(req->rq_reqmsg, 2, niocount * sizeof(*niobuf));
725 memcpy(&body->oa, oa, sizeof(*oa));
727 obdo_to_ioobj(oa, ioobj);
728 ioobj->ioo_bufcnt = niocount;
730 LASSERT (page_count > 0);
731 for (requested_nob = i = 0; i < page_count; i++, niobuf++) {
732 struct brw_page *pg = &pga[i];
733 struct brw_page *pg_prev = pg - 1;
735 LASSERT(pg->count > 0);
736 LASSERT((pg->off & ~PAGE_MASK) + pg->count <= PAGE_SIZE);
737 LASSERTF(i == 0 || pg->off > pg_prev->off,
738 "i %d p_c %u pg %p [pri %lu ind %lu] off "LPU64
739 " prev_pg %p [pri %lu ind %lu] off "LPU64,
741 pg->pg, pg->pg->private, pg->pg->index, pg->off,
742 pg_prev->pg, pg_prev->pg->private, pg_prev->pg->index,
745 rc = ptlrpc_prep_bulk_page(desc, pg->pg, pg->off & ~PAGE_MASK,
750 requested_nob += pg->count;
752 if (i > 0 && can_merge_pages(pg_prev, pg)) {
754 niobuf->len += pg->count;
756 niobuf->offset = pg->off;
757 niobuf->len = pg->count;
758 niobuf->flags = pg->flag;
762 LASSERT((void *)(niobuf - niocount) ==
763 lustre_msg_buf(req->rq_reqmsg, 2, niocount * sizeof(*niobuf)));
764 osc_announce_cached(cli, &body->oa);
765 spin_lock_irqsave(&req->rq_lock, flags);
766 req->rq_no_resend = 1;
767 spin_unlock_irqrestore(&req->rq_lock, flags);
769 /* size[0] still sizeof (*body) */
770 if (opc == OST_WRITE) {
772 body->oa.o_valid |= OBD_MD_FLCKSUM;
773 body->oa.o_nlink = cksum_pages(requested_nob, page_count, pga);
775 /* 1 RC per niobuf */
776 size[1] = sizeof(__u32) * niocount;
777 req->rq_replen = lustre_msg_size(2, size);
779 /* 1 RC for the whole I/O */
780 req->rq_replen = lustre_msg_size(1, size);
783 *niocountp = niocount;
784 *requested_nobp = requested_nob;
789 ptlrpc_req_finished (req);
793 static int osc_brw_fini_request(struct ptlrpc_request *req, struct obdo *oa,
794 int requested_nob, int niocount,
795 obd_count page_count, struct brw_page *pga,
798 struct client_obd *cli = &req->rq_import->imp_obd->u.cli;
799 struct ost_body *body;
804 body = lustre_swab_repbuf(req, 0, sizeof(*body), lustre_swab_ost_body);
806 CERROR ("Can't unpack body\n");
810 osc_update_grant(cli, body);
812 if (req->rq_reqmsg->opc == OST_WRITE) {
814 CERROR ("Unexpected +ve rc %d\n", rc);
818 return(check_write_rcs(req, niocount, page_count, pga));
821 if (rc > requested_nob) {
822 CERROR("Unexpected rc %d (%d requested)\n", rc, requested_nob);
826 if (rc < requested_nob)
827 handle_short_read(rc, page_count, pga);
829 memcpy(oa, &body->oa, sizeof(*oa));
832 if (oa->o_valid & OBD_MD_FLCKSUM) {
833 const struct ptlrpc_peer *peer =
834 &req->rq_import->imp_connection->c_peer;
835 static int cksum_counter;
836 obd_count server_cksum = oa->o_nlink;
837 obd_count cksum = cksum_pages(rc, page_count, pga);
838 char str[PTL_NALFMT_SIZE];
840 portals_nid2str(peer->peer_ni->pni_number, peer->peer_nid, str);
843 if (server_cksum != cksum) {
844 CERROR("Bad checksum: server %x, client %x, server NID "
845 LPX64" (%s)\n", server_cksum, cksum,
846 peer->peer_nid, str);
849 } else if ((cksum_counter & (-cksum_counter)) == cksum_counter){
850 CWARN("Checksum %u from "LPX64" (%s) OK: %x\n",
851 cksum_counter, peer->peer_nid, str, cksum);
854 static int cksum_missed;
857 if ((cksum_missed & (-cksum_missed)) == cksum_missed)
858 CERROR("Request checksum %u from "LPX64", no reply\n",
860 req->rq_import->imp_connection->c_peer.peer_nid);
866 static int osc_brw_internal(int cmd, struct obd_export *exp,struct obdo *oa,
867 struct lov_stripe_md *lsm,
868 obd_count page_count, struct brw_page *pga)
872 struct ptlrpc_request *request;
877 rc = osc_brw_prep_request(cmd, class_exp2cliimp(exp), oa, lsm,
878 page_count, pga, &requested_nob, &niocount,
880 /* NB ^ sets rq_no_resend */
885 rc = ptlrpc_queue_wait(request);
887 if (rc == -ETIMEDOUT && request->rq_resend) {
888 DEBUG_REQ(D_HA, request, "BULK TIMEOUT");
889 ptlrpc_req_finished(request);
893 rc = osc_brw_fini_request(request, oa, requested_nob, niocount,
894 page_count, pga, rc);
896 ptlrpc_req_finished(request);
900 static int brw_interpret(struct ptlrpc_request *request,
901 struct osc_brw_async_args *aa, int rc)
903 struct obdo *oa = aa->aa_oa;
904 int requested_nob = aa->aa_requested_nob;
905 int niocount = aa->aa_nio_count;
906 obd_count page_count = aa->aa_page_count;
907 struct brw_page *pga = aa->aa_pga;
910 /* XXX bug 937 here */
911 if (rc == -ETIMEDOUT && request->rq_resend) {
912 DEBUG_REQ(D_HA, request, "BULK TIMEOUT");
913 LBUG(); /* re-send. later. */
917 rc = osc_brw_fini_request(request, oa, requested_nob, niocount,
918 page_count, pga, rc);
922 static int async_internal(int cmd, struct obd_export *exp, struct obdo *oa,
923 struct lov_stripe_md *lsm, obd_count page_count,
924 struct brw_page *pga, struct ptlrpc_request_set *set)
926 struct ptlrpc_request *request;
929 struct osc_brw_async_args *aa;
933 rc = osc_brw_prep_request(cmd, class_exp2cliimp(exp), oa, lsm,
934 page_count, pga, &requested_nob, &nio_count,
936 /* NB ^ sets rq_no_resend */
939 LASSERT(sizeof(*aa) <= sizeof(request->rq_async_args));
940 aa = (struct osc_brw_async_args *)&request->rq_async_args;
942 aa->aa_requested_nob = requested_nob;
943 aa->aa_nio_count = nio_count;
944 aa->aa_page_count = page_count;
947 request->rq_interpret_reply = brw_interpret;
948 ptlrpc_set_add_req(set, request);
954 #define min_t(type,x,y) \
955 ({ type __x = (x); type __y = (y); __x < __y ? __x: __y; })
959 * ugh, we want disk allocation on the target to happen in offset order. we'll
960 * follow sedgewicks advice and stick to the dead simple shellsort -- it'll do
961 * fine for our small page arrays and doesn't require allocation. its an
962 * insertion sort that swaps elements that are strides apart, shrinking the
963 * stride down until its '1' and the array is sorted.
965 static void sort_brw_pages(struct brw_page *array, int num)
972 for (stride = 1; stride < num ; stride = (stride * 3) + 1)
977 for (i = stride ; i < num ; i++) {
980 while (j >= stride && array[j - stride].off > tmp.off) {
981 array[j] = array[j - stride];
986 } while (stride > 1);
989 /* make sure we the regions we're passing to elan don't violate its '4
990 * fragments' constraint. portal headers are a fragment, all full
991 * PAGE_SIZE long pages count as 1 fragment, and each partial page
992 * counts as a fragment. I think. see bug 934. */
993 static obd_count check_elan_limit(struct brw_page *pg, obd_count pages)
996 int saw_whole_frag = 0;
999 for (i = 0 ; frags_left && i < pages ; pg++, i++) {
1000 if (pg->count == PAGE_SIZE) {
1001 if (!saw_whole_frag) {
1012 static int osc_brw(int cmd, struct obd_export *exp, struct obdo *oa,
1013 struct lov_stripe_md *md, obd_count page_count,
1014 struct brw_page *pga, struct obd_trans_info *oti)
1018 if (cmd == OBD_BRW_CHECK) {
1019 /* The caller just wants to know if there's a chance that this
1020 * I/O can succeed */
1021 struct obd_import *imp = class_exp2cliimp(exp);
1023 if (imp == NULL || imp->imp_invalid)
1028 while (page_count) {
1029 obd_count pages_per_brw;
1032 if (page_count > OSC_BRW_MAX_IOV)
1033 pages_per_brw = OSC_BRW_MAX_IOV;
1035 pages_per_brw = page_count;
1037 sort_brw_pages(pga, pages_per_brw);
1038 pages_per_brw = check_elan_limit(pga, pages_per_brw);
1040 rc = osc_brw_internal(cmd, exp, oa, md, pages_per_brw, pga);
1045 page_count -= pages_per_brw;
1046 pga += pages_per_brw;
1051 static int osc_brw_async(int cmd, struct obd_export *exp, struct obdo *oa,
1052 struct lov_stripe_md *md, obd_count page_count,
1053 struct brw_page *pga, struct ptlrpc_request_set *set,
1054 struct obd_trans_info *oti)
1058 if (cmd == OBD_BRW_CHECK) {
1059 /* The caller just wants to know if there's a chance that this
1060 * I/O can succeed */
1061 struct obd_import *imp = class_exp2cliimp(exp);
1063 if (imp == NULL || imp->imp_invalid)
1068 while (page_count) {
1069 obd_count pages_per_brw;
1072 if (page_count > OSC_BRW_MAX_IOV)
1073 pages_per_brw = OSC_BRW_MAX_IOV;
1075 pages_per_brw = page_count;
1077 sort_brw_pages(pga, pages_per_brw);
1078 pages_per_brw = check_elan_limit(pga, pages_per_brw);
1080 rc = async_internal(cmd, exp, oa, md, pages_per_brw, pga, set);
1085 page_count -= pages_per_brw;
1086 pga += pages_per_brw;
1091 static void osc_check_rpcs(struct client_obd *cli);
1092 static void osc_exit_cache(struct client_obd *cli, struct osc_async_page *oap);
1094 static void osc_complete_oap(struct client_obd *cli,
1095 struct osc_async_page *oap, int rc)
1098 osc_exit_cache(cli, oap);
1099 oap->oap_async_flags = 0;
1100 if (oap->oap_osic) {
1101 osic_complete_one(oap->oap_osic, rc);
1102 oap->oap_osic = NULL;
1107 oap->oap_caller_ops->ap_completion(oap->oap_caller_data, oap->oap_cmd,
1112 static int brw_interpret_oap(struct ptlrpc_request *request,
1113 struct osc_brw_async_args *aa, int rc)
1115 struct osc_async_page *oap;
1116 struct client_obd *cli;
1117 struct list_head *pos, *n;
1120 CDEBUG(D_INODE, "request %p aa %p\n", request, aa);
1122 rc = osc_brw_fini_request(request, aa->aa_oa, aa->aa_requested_nob,
1123 aa->aa_nio_count, aa->aa_page_count,
1127 /* in failout recovery we ignore writeback failure and want
1128 * to just tell llite to unlock the page and continue */
1129 if (cli->cl_import == NULL || cli->cl_import->imp_invalid)
1132 spin_lock(&cli->cl_loi_list_lock);
1134 /* the caller may re-use the oap after the completion call so
1135 * we need to clean it up a little */
1136 list_for_each_safe(pos, n, &aa->aa_oaps) {
1137 oap = list_entry(pos, struct osc_async_page, oap_rpc_item);
1139 //CDEBUG(D_INODE, "page %p index %lu oap %p\n",
1140 //oap->oap_page, oap->oap_page->index, oap);
1142 list_del_init(&oap->oap_rpc_item);
1143 osc_complete_oap(cli, oap, rc);
1146 cli->cl_brw_in_flight--;
1147 osc_check_rpcs(cli);
1149 spin_unlock(&cli->cl_loi_list_lock);
1151 obdo_free(aa->aa_oa);
1152 OBD_FREE(aa->aa_pga, aa->aa_page_count * sizeof(struct brw_page));
1157 static struct ptlrpc_request *osc_build_req(struct client_obd *cli,
1158 struct list_head *rpc_list,
1159 int page_count, int cmd)
1161 struct ptlrpc_request *req;
1162 struct brw_page *pga = NULL;
1163 int requested_nob, nio_count;
1164 struct osc_brw_async_args *aa;
1165 struct obdo *oa = NULL;
1166 struct obd_async_page_ops *ops = NULL;
1167 void *caller_data = NULL;
1168 struct list_head *pos;
1171 LASSERT(!list_empty(rpc_list));
1173 OBD_ALLOC(pga, sizeof(*pga) * page_count);
1175 RETURN(ERR_PTR(-ENOMEM));
1179 GOTO(out, req = ERR_PTR(-ENOMEM));
1182 list_for_each(pos, rpc_list) {
1183 struct osc_async_page *oap;
1185 oap = list_entry(pos, struct osc_async_page, oap_rpc_item);
1187 ops = oap->oap_caller_ops;
1188 caller_data = oap->oap_caller_data;
1190 pga[i].off = oap->oap_obj_off + oap->oap_page_off;
1191 pga[i].pg = oap->oap_page;
1192 pga[i].count = oap->oap_count;
1193 pga[i].flag = oap->oap_brw_flags;
1194 //CDEBUG(D_INODE, "putting page %p index %lu oap %p into pga\n",
1195 //pga[i].pg, oap->oap_page->index, oap);
1199 /* always get the data for the obdo for the rpc */
1200 LASSERT(ops != NULL);
1201 ops->ap_fill_obdo(caller_data, cmd, oa);
1203 sort_brw_pages(pga, page_count);
1204 rc = osc_brw_prep_request(cmd, cli->cl_import, oa, NULL, page_count,
1205 pga, &requested_nob, &nio_count, &req);
1207 CERROR("prep_req failed: %d\n", rc);
1208 GOTO(out, req = ERR_PTR(rc));
1211 LASSERT(sizeof(*aa) <= sizeof(req->rq_async_args));
1212 aa = (struct osc_brw_async_args *)&req->rq_async_args;
1214 aa->aa_requested_nob = requested_nob;
1215 aa->aa_nio_count = nio_count;
1216 aa->aa_page_count = page_count;
1225 OBD_FREE(pga, sizeof(*pga) * page_count);
1230 static void lop_update_pending(struct client_obd *cli,
1231 struct loi_oap_pages *lop, int cmd, int delta)
1233 lop->lop_num_pending += delta;
1234 if (cmd == OBD_BRW_WRITE)
1235 cli->cl_pending_w_pages += delta;
1237 cli->cl_pending_r_pages += delta;
1240 /* the loi lock is held across this function but it's allowed to release
1241 * and reacquire it during its work */
1242 static int osc_send_oap_rpc(struct client_obd *cli, int cmd,
1243 struct loi_oap_pages *lop)
1245 struct ptlrpc_request *request;
1246 obd_count page_count = 0;
1247 struct list_head *tmp, *pos;
1248 struct osc_async_page *oap = NULL;
1249 struct osc_brw_async_args *aa;
1250 struct obd_async_page_ops *ops;
1251 LIST_HEAD(rpc_list);
1254 /* first we find the pages we're allowed to work with */
1255 list_for_each_safe(pos, tmp, &lop->lop_pending) {
1256 oap = list_entry(pos, struct osc_async_page, oap_pending_item);
1257 ops = oap->oap_caller_ops;
1259 /* in llite being 'ready' equates to the page being locked
1260 * until completion unlocks it. commit_write submits a page
1261 * as not ready because its unlock will happen unconditionally
1262 * as the call returns. if we race with commit_write giving
1263 * us that page we dont' want to create a hole in the page
1264 * stream, so we stop and leave the rpc to be fired by
1265 * another dirtier or kupdated interval (the not ready page
1266 * will still be on the dirty list). we could call in
1267 * at the end of ll_file_write to process the queue again. */
1268 if (!(oap->oap_async_flags & ASYNC_READY)) {
1269 if (ops->ap_make_ready(oap->oap_caller_data, cmd)) {
1270 CDEBUG(D_INODE, "oap at page_count %d not "
1271 "ready\n", page_count);
1274 oap->oap_async_flags |= ASYNC_READY;
1277 /* take the page out of our book-keeping */
1278 list_del_init(&oap->oap_pending_item);
1279 lop_update_pending(cli, lop, cmd, -1);
1280 if (!list_empty(&oap->oap_urgent_item))
1281 list_del_init(&oap->oap_urgent_item);
1283 /* ask the caller for the size of the io as the rpc leaves. */
1284 if (!(oap->oap_async_flags & ASYNC_COUNT_STABLE))
1285 oap->oap_count = ops->ap_refresh_count(
1286 oap->oap_caller_data,
1288 if (oap->oap_count <= 0) {
1289 CDEBUG(D_INODE, "oap %p count %d, completing\n", oap,
1291 osc_complete_oap(cli, oap, oap->oap_count);
1295 /* now put the page back in our accounting */
1296 list_add_tail(&oap->oap_rpc_item, &rpc_list);
1297 if (++page_count >= cli->cl_max_pages_per_rpc)
1301 if (page_count == 0)
1304 spin_unlock(&cli->cl_loi_list_lock);
1306 request = osc_build_req(cli, &rpc_list, page_count, cmd);
1307 if (IS_ERR(request)) {
1308 /* this should happen rarely and is pretty bad, it makes the
1309 * pending list not follow the dirty order */
1310 spin_lock(&cli->cl_loi_list_lock);
1311 list_for_each_safe(pos, tmp, &rpc_list) {
1312 oap = list_entry(pos, struct osc_async_page,
1314 list_del_init(&oap->oap_rpc_item);
1315 list_add_tail(&oap->oap_pending_item,
1317 lop_update_pending(cli, lop, cmd, 1);
1318 if (oap->oap_async_flags & ASYNC_URGENT)
1319 list_add(&oap->oap_urgent_item,
1322 RETURN(PTR_ERR(request));
1325 LASSERT(sizeof(*aa) <= sizeof(request->rq_async_args));
1326 aa = (struct osc_brw_async_args *)&request->rq_async_args;
1327 INIT_LIST_HEAD(&aa->aa_oaps);
1328 list_splice(&rpc_list, &aa->aa_oaps);
1329 INIT_LIST_HEAD(&rpc_list);
1331 if (cmd == OBD_BRW_READ)
1332 lproc_osc_hist_pow2(&cli->cl_read_page_hist, page_count);
1334 lproc_osc_hist_pow2(&cli->cl_write_page_hist, page_count);
1336 spin_lock(&cli->cl_loi_list_lock);
1337 if (cmd == OBD_BRW_READ)
1338 lproc_osc_hist(&cli->cl_read_rpc_hist, cli->cl_brw_in_flight);
1340 lproc_osc_hist(&cli->cl_write_rpc_hist, cli->cl_brw_in_flight);
1342 cli->cl_brw_in_flight++;
1343 CDEBUG(D_INODE, "req %p: %d pages, aa %p. now %d in flight\n", request,
1344 page_count, aa, cli->cl_brw_in_flight);
1346 request->rq_interpret_reply = brw_interpret_oap;
1347 ptlrpcd_add_req(request);
1351 static int lop_makes_rpc(struct client_obd *cli, struct loi_oap_pages *lop,
1357 /* stream rpcs until we complete the urgent pages in the object */
1358 if (!list_empty(&lop->lop_urgent))
1361 /* fire off rpcs when we have 'optimal' rpcs as tuned for the wire. */
1362 optimal = cli->cl_max_pages_per_rpc;
1363 /* *2 to avoid triggering rpcs that would want to include pages that
1364 * are being queued but which can't be made ready until the queuer
1365 * finishes with the page. this is a wart for llite::commit_write() */
1366 if (cmd == OBD_BRW_WRITE)
1368 if (lop->lop_num_pending >= optimal)
1371 /* trigger a write rpc stream as long as there are dirtiers waiting
1372 * for space. as they're waiting, they're not going to create more
1373 * pages to coallesce with what's waiting.. */
1374 if (!list_empty(&cli->cl_cache_waiters))
1380 static int loi_makes_rpc(struct client_obd *cli, struct lov_oinfo *loi)
1382 return lop_makes_rpc(cli, &loi->loi_write_lop, OBD_BRW_WRITE) ||
1383 lop_makes_rpc(cli, &loi->loi_read_lop, OBD_BRW_READ);
1386 static void loi_onto_ready_list(struct client_obd *cli, struct lov_oinfo *loi)
1388 if (list_empty(&loi->loi_cli_item) && loi_makes_rpc(cli, loi))
1389 list_add_tail(&loi->loi_cli_item, &cli->cl_loi_ready_list);
1392 #define LOI_DEBUG(LOI, STR, args...) \
1393 CDEBUG(D_INODE, "loi rdy %d [%p,%p] wr %d:%d rd %d:%d " STR, \
1394 !list_empty(&(LOI)->loi_cli_item), \
1395 (LOI)->loi_cli_item.next, \
1396 (LOI)->loi_cli_item.prev, \
1397 (LOI)->loi_write_lop.lop_num_pending, \
1398 !list_empty(&(LOI)->loi_write_lop.lop_urgent), \
1399 (LOI)->loi_read_lop.lop_num_pending, \
1400 !list_empty(&(LOI)->loi_read_lop.lop_urgent), \
1403 /* called with the loi list lock held */
1404 static void osc_check_rpcs(struct client_obd *cli)
1406 struct lov_oinfo *loi;
1407 int rc = 0, making_progress;
1410 if (list_empty(&cli->cl_loi_ready_list)) {
1411 CDEBUG(D_INODE, "no lois ready\n");
1416 while (!list_empty(&cli->cl_loi_ready_list)) {
1417 loi = list_entry(cli->cl_loi_ready_list.next, struct lov_oinfo,
1420 if (cli->cl_brw_in_flight >= cli->cl_max_rpcs_in_flight)
1423 making_progress = 0;
1425 /* hmm, it occurs to me that having rpc preparation fail
1426 * with num_pending == num_urgent means that there won't
1427 * be any more calls into here unless other traffic comes
1430 /* attempt some read/write balancing by alternating between
1431 * reads and writes in an object */
1432 if (lop_makes_rpc(cli, &loi->loi_write_lop, OBD_BRW_WRITE)) {
1433 rc = osc_send_oap_rpc(cli, OBD_BRW_WRITE,
1434 &loi->loi_write_lop);
1440 if (lop_makes_rpc(cli, &loi->loi_read_lop, OBD_BRW_READ)) {
1441 rc = osc_send_oap_rpc(cli, OBD_BRW_READ,
1442 &loi->loi_read_lop);
1449 /* attempt some inter-object balancing by issueing rpcs
1450 * for each object in turn */
1451 if (!list_empty(&loi->loi_cli_item))
1452 list_del_init(&loi->loi_cli_item);
1454 loi_onto_ready_list(cli, loi);
1456 LOI_DEBUG(loi, "mp %d\n", making_progress);
1458 /* could be smarter, !making_progress can happen in theory
1459 * if all the pages can not be locked in set_io_ready */
1460 if (!making_progress)
1466 /* we're trying to queue a page in the osc so we're subject to the
1467 * 'cl_dirty_max' limit on the number of pages that can be queued in the osc.
1468 * If the osc's queued pages are already at that limit, then we want to sleep
1469 * until there is space in the osc's queue for us. we need this goofy
1470 * little struct to really tell that our allocation was fulfilled in
1471 * the presence of pending signals */
1472 struct osc_cache_waiter {
1473 struct list_head ocw_entry;
1474 wait_queue_head_t ocw_waitq;
1477 static int ocw_granted(struct client_obd *cli, struct osc_cache_waiter *ocw)
1481 spin_lock(&cli->cl_loi_list_lock);
1482 rc = list_empty(&ocw->ocw_entry);
1483 spin_unlock(&cli->cl_loi_list_lock);
1487 static inline obd_size osc_cache_cap(struct client_obd *cli)
1489 if (cli->cl_ost_can_grant)
1490 return min(cli->cl_dirty_granted, cli->cl_dirty_max);
1492 return cli->cl_dirty_max;
1494 void osc_adjust_cache(struct client_obd *cli)
1496 struct list_head *l, *tmp;
1497 struct osc_cache_waiter *ocw;
1498 obd_size cache_cap = osc_cache_cap(cli);
1502 list_for_each_safe(l, tmp, &cli->cl_cache_waiters) {
1503 if (cli->cl_dirty + PAGE_SIZE > cache_cap &&
1504 cache_cap >= PAGE_SIZE)
1507 ocw = list_entry(l, struct osc_cache_waiter, ocw_entry);
1508 list_del_init(&ocw->ocw_entry);
1509 if (cache_cap < PAGE_SIZE) {
1510 /* "They" said we are starting synchronous operations, so
1511 wakeup everybody waiting for pages in cache and make them
1512 go away unsatisfied. */
1513 ocw->ocw_rc = -EDQUOT;
1515 cli->cl_dirty += PAGE_SIZE;
1517 wake_up(&ocw->ocw_waitq);
1522 static int osc_enter_cache(struct client_obd *cli, struct lov_oinfo *loi,
1523 struct osc_async_page *oap)
1525 struct osc_cache_waiter ocw;
1526 struct l_wait_info lwi = {0};
1530 if (osc_cache_cap(cli) < PAGE_SIZE)
1533 /* if we fail this test then cl_dirty contains at least one page
1534 * that will have to be completed after we release the lock */
1535 if (cli->cl_dirty + PAGE_SIZE <= osc_cache_cap(cli)) {
1536 /* account for ourselves */
1537 cli->cl_dirty += PAGE_SIZE;
1541 init_waitqueue_head(&ocw.ocw_waitq);
1543 list_add_tail(&ocw.ocw_entry, &cli->cl_cache_waiters);
1545 /* make sure that there are write rpcs in flight to wait for. this
1546 * is a little silly as this object may not have any pending
1547 * but other objects sure might. this should probably be cleaned. */
1548 loi_onto_ready_list(cli, loi);
1549 osc_check_rpcs(cli);
1550 spin_unlock(&cli->cl_loi_list_lock);
1552 CDEBUG(D_INODE, "sleeping for cache space\n");
1553 l_wait_event(ocw.ocw_waitq, ocw_granted(cli, &ocw), &lwi);
1555 spin_lock(&cli->cl_loi_list_lock);
1557 if (!list_empty(&ocw.ocw_entry)) {
1559 list_del(&ocw.ocw_entry);
1564 oap->oap_brw_flags |= OBD_BRW_FROM_GRANT;
1568 /* the companion to enter_cache, called when an oap is now longer part of the
1569 * dirty accounting.. so writeback completes or truncate happens before writing
1570 * starts. must be called with the loi lock held. */
1571 static void osc_exit_cache(struct client_obd *cli, struct osc_async_page *oap)
1573 struct osc_cache_waiter *ocw;
1576 if (!(oap->oap_brw_flags & OBD_BRW_FROM_GRANT)) {
1581 /* If nobody waits for cache space or if we need to shrink it */
1582 if (list_empty(&cli->cl_cache_waiters) ||
1583 (cli->cl_dirty > osc_cache_cap(cli))) {
1584 cli->cl_dirty -= PAGE_SIZE;
1586 ocw = list_entry(cli->cl_cache_waiters.next,
1587 struct osc_cache_waiter, ocw_entry);
1588 list_del_init(&ocw->ocw_entry);
1589 wake_up(&ocw->ocw_waitq);
1592 oap->oap_brw_flags &= ~OBD_BRW_FROM_GRANT;
1596 int osc_prep_async_page(struct obd_export *exp, struct lov_stripe_md *lsm,
1597 struct lov_oinfo *loi, struct page *page,
1598 obd_off offset, struct obd_async_page_ops *ops,
1599 void *data, void **res)
1601 struct osc_async_page *oap;
1604 OBD_ALLOC(oap, sizeof(*oap));
1608 oap->oap_magic = OAP_MAGIC;
1609 oap->oap_caller_ops = ops;
1610 oap->oap_caller_data = data;
1612 oap->oap_page = page;
1613 oap->oap_obj_off = offset;
1615 INIT_LIST_HEAD(&oap->oap_pending_item);
1616 INIT_LIST_HEAD(&oap->oap_urgent_item);
1617 INIT_LIST_HEAD(&oap->oap_rpc_item);
1619 CDEBUG(D_CACHE, "oap %p page %p obj off "LPU64"\n", oap, page, offset);
1624 struct osc_async_page *oap_from_cookie(void *cookie)
1626 struct osc_async_page *oap = cookie;
1627 if (oap->oap_magic != OAP_MAGIC)
1628 return ERR_PTR(-EINVAL);
1632 static int osc_queue_async_io(struct obd_export *exp, struct lov_stripe_md *lsm,
1633 struct lov_oinfo *loi, void *cookie,
1634 int cmd, obd_off off, int count,
1635 obd_flag brw_flags, enum async_flags async_flags)
1637 struct client_obd *cli = &exp->exp_obd->u.cli;
1638 struct osc_async_page *oap;
1639 struct loi_oap_pages *lop;
1643 oap = oap_from_cookie(cookie);
1645 RETURN(PTR_ERR(oap));
1647 if (cli->cl_import == NULL || cli->cl_import->imp_invalid)
1650 if (!list_empty(&oap->oap_pending_item) ||
1651 !list_empty(&oap->oap_urgent_item) ||
1652 !list_empty(&oap->oap_rpc_item))
1656 loi = &lsm->lsm_oinfo[0];
1658 spin_lock(&cli->cl_loi_list_lock);
1661 oap->oap_async_flags = async_flags;
1662 oap->oap_page_off = off;
1663 oap->oap_count = count;
1664 oap->oap_brw_flags = brw_flags;
1666 if (cmd == OBD_BRW_WRITE) {
1667 rc = osc_enter_cache(cli, loi, oap);
1669 spin_unlock(&cli->cl_loi_list_lock);
1672 lop = &loi->loi_write_lop;
1674 lop = &loi->loi_read_lop;
1677 if (oap->oap_async_flags & ASYNC_URGENT)
1678 list_add(&oap->oap_urgent_item, &lop->lop_urgent);
1679 list_add_tail(&oap->oap_pending_item, &lop->lop_pending);
1680 lop_update_pending(cli, lop, cmd, 1);
1682 loi_onto_ready_list(cli, loi);
1684 LOI_DEBUG(loi, "oap %p page %p added for cmd %d\n", oap, oap->oap_page,
1687 osc_check_rpcs(cli);
1688 spin_unlock(&cli->cl_loi_list_lock);
1693 /* aka (~was & now & flag), but this is more clear :) */
1694 #define SETTING(was, now, flag) (!(was & flag) && (now & flag))
1696 static int osc_set_async_flags(struct obd_export *exp,
1697 struct lov_stripe_md *lsm,
1698 struct lov_oinfo *loi, void *cookie,
1699 obd_flag async_flags)
1701 struct client_obd *cli = &exp->exp_obd->u.cli;
1702 struct loi_oap_pages *lop;
1703 struct osc_async_page *oap;
1707 oap = oap_from_cookie(cookie);
1709 RETURN(PTR_ERR(oap));
1711 if (cli->cl_import == NULL || cli->cl_import->imp_invalid)
1715 loi = &lsm->lsm_oinfo[0];
1717 if (oap->oap_cmd == OBD_BRW_WRITE) {
1718 lop = &loi->loi_write_lop;
1720 lop = &loi->loi_read_lop;
1723 spin_lock(&cli->cl_loi_list_lock);
1725 if (oap->oap_async_flags == async_flags)
1728 if (SETTING(oap->oap_async_flags, async_flags, ASYNC_READY))
1729 oap->oap_async_flags |= ASYNC_READY;
1731 if (SETTING(oap->oap_async_flags, async_flags, ASYNC_URGENT)) {
1732 if (list_empty(&oap->oap_rpc_item)) {
1733 list_add(&oap->oap_urgent_item, &lop->lop_urgent);
1734 loi_onto_ready_list(cli, loi);
1738 LOI_DEBUG(loi, "oap %p page %p has flags %x\n", oap, oap->oap_page,
1739 oap->oap_async_flags);
1741 osc_check_rpcs(cli);
1742 spin_unlock(&cli->cl_loi_list_lock);
1746 static int osc_queue_sync_io(struct obd_export *exp, struct lov_stripe_md *lsm,
1747 struct lov_oinfo *loi,
1748 struct obd_sync_io_container *osic, void *cookie,
1749 int cmd, obd_off off, int count,
1752 struct client_obd *cli = &exp->exp_obd->u.cli;
1753 struct osc_async_page *oap;
1754 struct loi_oap_pages *lop;
1757 oap = oap_from_cookie(cookie);
1759 RETURN(PTR_ERR(oap));
1761 if (cli->cl_import == NULL || cli->cl_import->imp_invalid)
1764 if (!list_empty(&oap->oap_pending_item) ||
1765 !list_empty(&oap->oap_urgent_item) ||
1766 !list_empty(&oap->oap_rpc_item))
1770 loi = &lsm->lsm_oinfo[0];
1772 spin_lock(&cli->cl_loi_list_lock);
1775 oap->oap_page_off = off;
1776 oap->oap_count = count;
1777 oap->oap_brw_flags = brw_flags;
1779 if (cmd == OBD_BRW_WRITE)
1780 lop = &loi->loi_write_lop;
1782 lop = &loi->loi_read_lop;
1784 list_add_tail(&oap->oap_pending_item, &lop->lop_pending_sync);
1785 oap->oap_osic = osic;
1788 LOI_DEBUG(loi, "oap %p page %p on sync pending\n", oap, oap->oap_page);
1790 spin_unlock(&cli->cl_loi_list_lock);
1795 static void osc_sync_to_pending(struct client_obd *cli, struct lov_oinfo *loi,
1796 struct loi_oap_pages *lop, int cmd)
1798 struct list_head *pos, *tmp;
1799 struct osc_async_page *oap;
1801 list_for_each_safe(pos, tmp, &lop->lop_pending_sync) {
1802 oap = list_entry(pos, struct osc_async_page, oap_pending_item);
1803 list_del(&oap->oap_pending_item);
1804 oap->oap_async_flags |= ASYNC_READY | ASYNC_URGENT |
1806 list_add_tail(&oap->oap_pending_item, &lop->lop_pending);
1807 list_add(&oap->oap_urgent_item, &lop->lop_urgent);
1808 lop_update_pending(cli, lop, cmd, 1);
1810 loi_onto_ready_list(cli, loi);
1813 static int osc_trigger_sync_io(struct obd_export *exp,
1814 struct lov_stripe_md *lsm,
1815 struct lov_oinfo *loi,
1816 struct obd_sync_io_container *osic)
1818 struct client_obd *cli = &exp->exp_obd->u.cli;
1821 if (cli->cl_import == NULL || cli->cl_import->imp_invalid)
1825 loi = &lsm->lsm_oinfo[0];
1827 spin_lock(&cli->cl_loi_list_lock);
1829 osc_sync_to_pending(cli, loi, &loi->loi_write_lop, OBD_BRW_WRITE);
1830 osc_sync_to_pending(cli, loi, &loi->loi_read_lop, OBD_BRW_READ);
1832 osc_check_rpcs(cli);
1833 spin_unlock(&cli->cl_loi_list_lock);
1838 static int osc_teardown_async_page(struct obd_export *exp,
1839 struct lov_stripe_md *lsm,
1840 struct lov_oinfo *loi, void *cookie)
1842 struct client_obd *cli = &exp->exp_obd->u.cli;
1843 struct loi_oap_pages *lop;
1844 struct osc_async_page *oap;
1848 oap = oap_from_cookie(cookie);
1850 RETURN(PTR_ERR(oap));
1853 loi = &lsm->lsm_oinfo[0];
1855 if (oap->oap_cmd == OBD_BRW_WRITE) {
1856 lop = &loi->loi_write_lop;
1858 lop = &loi->loi_read_lop;
1861 spin_lock(&cli->cl_loi_list_lock);
1863 osc_exit_cache(cli, oap);
1865 if (!list_empty(&oap->oap_rpc_item))
1866 GOTO(out, rc = -EBUSY);
1868 if (!list_empty(&oap->oap_urgent_item)) {
1869 list_del_init(&oap->oap_urgent_item);
1870 oap->oap_async_flags &= ~ASYNC_URGENT;
1872 if (!list_empty(&oap->oap_pending_item)) {
1873 list_del_init(&oap->oap_pending_item);
1874 lop_update_pending(cli, lop, oap->oap_cmd, -1);
1876 if (!list_empty(&loi->loi_cli_item) && !loi_makes_rpc(cli, loi))
1877 list_del_init(&loi->loi_cli_item);
1879 LOI_DEBUG(loi, "oap %p page %p torn down\n", oap, oap->oap_page);
1881 spin_unlock(&cli->cl_loi_list_lock);
1882 OBD_FREE(oap, sizeof(*oap));
1887 /* Note: caller will lock/unlock, and set uptodate on the pages */
1888 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
1889 static int sanosc_brw_read(struct obd_export *exp, struct obdo *oa,
1890 struct lov_stripe_md *lsm, obd_count page_count,
1891 struct brw_page *pga)
1893 struct ptlrpc_request *request = NULL;
1894 struct ost_body *body;
1895 struct niobuf_remote *nioptr;
1896 struct obd_ioobj *iooptr;
1897 int rc, size[3] = {sizeof(*body)}, mapped = 0;
1901 /* XXX does not handle 'new' brw protocol */
1903 size[1] = sizeof(struct obd_ioobj);
1904 size[2] = page_count * sizeof(*nioptr);
1906 request = ptlrpc_prep_req(class_exp2cliimp(exp), OST_SAN_READ, 3,
1911 body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof(*body));
1912 iooptr = lustre_msg_buf(request->rq_reqmsg, 1, sizeof(*iooptr));
1913 nioptr = lustre_msg_buf(request->rq_reqmsg, 2,
1914 sizeof(*nioptr) * page_count);
1916 memcpy(&body->oa, oa, sizeof(body->oa));
1918 obdo_to_ioobj(oa, iooptr);
1919 iooptr->ioo_bufcnt = page_count;
1921 for (mapped = 0; mapped < page_count; mapped++, nioptr++) {
1922 LASSERT(PageLocked(pga[mapped].pg));
1923 LASSERT(mapped == 0 || pga[mapped].off > pga[mapped - 1].off);
1925 nioptr->offset = pga[mapped].off;
1926 nioptr->len = pga[mapped].count;
1927 nioptr->flags = pga[mapped].flag;
1930 size[1] = page_count * sizeof(*nioptr);
1931 request->rq_replen = lustre_msg_size(2, size);
1933 rc = ptlrpc_queue_wait(request);
1937 body = lustre_swab_repbuf(request, 0, sizeof(*body),
1938 lustre_swab_ost_body);
1940 CERROR("Can't unpack body\n");
1941 GOTO(out_req, rc = -EPROTO);
1944 memcpy(oa, &body->oa, sizeof(*oa));
1946 swab = lustre_msg_swabbed(request->rq_repmsg);
1947 LASSERT_REPSWAB(request, 1);
1948 nioptr = lustre_msg_buf(request->rq_repmsg, 1, size[1]);
1950 /* nioptr missing or short */
1951 GOTO(out_req, rc = -EPROTO);
1955 for (mapped = 0; mapped < page_count; mapped++, nioptr++) {
1956 struct page *page = pga[mapped].pg;
1957 struct buffer_head *bh;
1961 lustre_swab_niobuf_remote (nioptr);
1963 /* got san device associated */
1964 LASSERT(exp->exp_obd != NULL);
1965 dev = exp->exp_obd->u.cli.cl_sandev;
1968 if (!nioptr->offset) {
1969 CDEBUG(D_PAGE, "hole at ino %lu; index %ld\n",
1970 page->mapping->host->i_ino,
1972 memset(page_address(page), 0, PAGE_SIZE);
1976 if (!page->buffers) {
1977 create_empty_buffers(page, dev, PAGE_SIZE);
1980 clear_bit(BH_New, &bh->b_state);
1981 set_bit(BH_Mapped, &bh->b_state);
1982 bh->b_blocknr = (unsigned long)nioptr->offset;
1984 clear_bit(BH_Uptodate, &bh->b_state);
1986 ll_rw_block(READ, 1, &bh);
1990 /* if buffer already existed, it must be the
1991 * one we mapped before, check it */
1992 LASSERT(!test_bit(BH_New, &bh->b_state));
1993 LASSERT(test_bit(BH_Mapped, &bh->b_state));
1994 LASSERT(bh->b_blocknr == (unsigned long)nioptr->offset);
1996 /* wait it's io completion */
1997 if (test_bit(BH_Lock, &bh->b_state))
2000 if (!test_bit(BH_Uptodate, &bh->b_state))
2001 ll_rw_block(READ, 1, &bh);
2005 /* must do syncronous write here */
2007 if (!buffer_uptodate(bh)) {
2015 ptlrpc_req_finished(request);
2019 static int sanosc_brw_write(struct obd_export *exp, struct obdo *oa,
2020 struct lov_stripe_md *lsm, obd_count page_count,
2021 struct brw_page *pga)
2023 struct ptlrpc_request *request = NULL;
2024 struct ost_body *body;
2025 struct niobuf_remote *nioptr;
2026 struct obd_ioobj *iooptr;
2027 int rc, size[3] = {sizeof(*body)}, mapped = 0;
2031 size[1] = sizeof(struct obd_ioobj);
2032 size[2] = page_count * sizeof(*nioptr);
2034 request = ptlrpc_prep_req(class_exp2cliimp(exp), OST_SAN_WRITE,
2039 body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof (*body));
2040 iooptr = lustre_msg_buf(request->rq_reqmsg, 1, sizeof (*iooptr));
2041 nioptr = lustre_msg_buf(request->rq_reqmsg, 2,
2042 sizeof (*nioptr) * page_count);
2044 memcpy(&body->oa, oa, sizeof(body->oa));
2046 obdo_to_ioobj(oa, iooptr);
2047 iooptr->ioo_bufcnt = page_count;
2050 for (mapped = 0; mapped < page_count; mapped++, nioptr++) {
2051 LASSERT(PageLocked(pga[mapped].pg));
2052 LASSERT(mapped == 0 || pga[mapped].off > pga[mapped - 1].off);
2054 nioptr->offset = pga[mapped].off;
2055 nioptr->len = pga[mapped].count;
2056 nioptr->flags = pga[mapped].flag;
2059 size[1] = page_count * sizeof(*nioptr);
2060 request->rq_replen = lustre_msg_size(2, size);
2062 rc = ptlrpc_queue_wait(request);
2066 swab = lustre_msg_swabbed (request->rq_repmsg);
2067 LASSERT_REPSWAB (request, 1);
2068 nioptr = lustre_msg_buf(request->rq_repmsg, 1, size[1]);
2070 CERROR("absent/short niobuf array\n");
2071 GOTO(out_req, rc = -EPROTO);
2075 for (mapped = 0; mapped < page_count; mapped++, nioptr++) {
2076 struct page *page = pga[mapped].pg;
2077 struct buffer_head *bh;
2081 lustre_swab_niobuf_remote (nioptr);
2083 /* got san device associated */
2084 LASSERT(exp->exp_obd != NULL);
2085 dev = exp->exp_obd->u.cli.cl_sandev;
2087 if (!page->buffers) {
2088 create_empty_buffers(page, dev, PAGE_SIZE);
2091 LASSERT(!test_bit(BH_New, &page->buffers->b_state));
2092 LASSERT(test_bit(BH_Mapped, &page->buffers->b_state));
2093 LASSERT(page->buffers->b_blocknr ==
2094 (unsigned long)nioptr->offset);
2100 /* if buffer locked, wait it's io completion */
2101 if (test_bit(BH_Lock, &bh->b_state))
2104 clear_bit(BH_New, &bh->b_state);
2105 set_bit(BH_Mapped, &bh->b_state);
2107 /* override the block nr */
2108 bh->b_blocknr = (unsigned long)nioptr->offset;
2110 /* we are about to write it, so set it
2112 * page lock should garentee no race condition here */
2113 set_bit(BH_Uptodate, &bh->b_state);
2114 set_bit(BH_Dirty, &bh->b_state);
2116 ll_rw_block(WRITE, 1, &bh);
2118 /* must do syncronous write here */
2120 if (!buffer_uptodate(bh) || test_bit(BH_Dirty, &bh->b_state)) {
2128 ptlrpc_req_finished(request);
2132 static int sanosc_brw(int cmd, struct obd_export *exp, struct obdo *oa,
2133 struct lov_stripe_md *lsm, obd_count page_count,
2134 struct brw_page *pga, struct obd_trans_info *oti)
2138 while (page_count) {
2139 obd_count pages_per_brw;
2142 if (page_count > OSC_BRW_MAX_IOV)
2143 pages_per_brw = OSC_BRW_MAX_IOV;
2145 pages_per_brw = page_count;
2147 if (cmd & OBD_BRW_WRITE)
2148 rc = sanosc_brw_write(exp, oa, lsm, pages_per_brw,pga);
2150 rc = sanosc_brw_read(exp, oa, lsm, pages_per_brw, pga);
2155 page_count -= pages_per_brw;
2156 pga += pages_per_brw;
2163 static void osc_set_data_with_check(struct lustre_handle *lockh, void *data)
2165 struct ldlm_lock *lock = ldlm_handle2lock(lockh);
2167 LASSERT(lock != NULL);
2168 l_lock(&lock->l_resource->lr_namespace->ns_lock);
2170 if (lock->l_ast_data && lock->l_ast_data != data) {
2171 struct inode *new_inode = data;
2172 struct inode *old_inode = lock->l_ast_data;
2173 unsigned long state = old_inode->i_state & I_FREEING;
2174 CERROR("Found existing inode %p/%lu/%u state %lu in lock: "
2175 "setting data to %p/%lu/%u\n", old_inode,
2176 old_inode->i_ino, old_inode->i_generation, state,
2177 new_inode, new_inode->i_ino, new_inode->i_generation);
2181 lock->l_ast_data = data;
2182 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
2183 LDLM_LOCK_PUT(lock);
2186 static int osc_change_cbdata(struct obd_export *exp, struct lov_stripe_md *lsm,
2187 ldlm_iterator_t replace, void *data)
2189 struct ldlm_res_id res_id = { .name = {lsm->lsm_object_id} };
2190 struct obd_device *obd = class_exp2obd(exp);
2192 ldlm_change_cbdata(obd->obd_namespace, &res_id, replace, data);
2196 static int osc_enqueue(struct obd_export *exp, struct lov_stripe_md *lsm,
2197 struct lustre_handle *parent_lock,
2198 __u32 type, void *extentp, int extent_len, __u32 mode,
2199 int *flags, void *callback, void *data,
2200 struct lustre_handle *lockh)
2202 struct ldlm_res_id res_id = { .name = {lsm->lsm_object_id} };
2203 struct obd_device *obd = exp->exp_obd;
2204 struct ldlm_extent *extent = extentp;
2208 /* Filesystem lock extents are extended to page boundaries so that
2209 * dealing with the page cache is a little smoother. */
2210 extent->start -= extent->start & ~PAGE_MASK;
2211 extent->end |= ~PAGE_MASK;
2213 /* Next, search for already existing extent locks that will cover us */
2214 rc = ldlm_lock_match(obd->obd_namespace, 0, &res_id,
2215 type, extent, sizeof(*extent), mode, lockh);
2217 osc_set_data_with_check(lockh, data);
2218 /* We already have a lock, and it's referenced */
2222 /* If we're trying to read, we also search for an existing PW lock. The
2223 * VFS and page cache already protect us locally, so lots of readers/
2224 * writers can share a single PW lock.
2226 * There are problems with conversion deadlocks, so instead of
2227 * converting a read lock to a write lock, we'll just enqueue a new
2230 * At some point we should cancel the read lock instead of making them
2231 * send us a blocking callback, but there are problems with canceling
2232 * locks out from other users right now, too. */
2234 if (mode == LCK_PR) {
2235 rc = ldlm_lock_match(obd->obd_namespace, 0, &res_id, type,
2236 extent, sizeof(*extent), LCK_PW, lockh);
2238 /* FIXME: This is not incredibly elegant, but it might
2239 * be more elegant than adding another parameter to
2240 * lock_match. I want a second opinion. */
2241 ldlm_lock_addref(lockh, LCK_PR);
2242 ldlm_lock_decref(lockh, LCK_PW);
2243 osc_set_data_with_check(lockh, data);
2248 rc = ldlm_cli_enqueue(exp, NULL, obd->obd_namespace, parent_lock,
2249 res_id, type, extent, sizeof(*extent), mode,
2250 flags,ldlm_completion_ast, callback, data, lockh);
2254 static int osc_match(struct obd_export *exp, struct lov_stripe_md *lsm,
2255 __u32 type, void *extentp, int extent_len, __u32 mode,
2256 int *flags, void *data, struct lustre_handle *lockh)
2258 struct ldlm_res_id res_id = { .name = {lsm->lsm_object_id} };
2259 struct obd_device *obd = exp->exp_obd;
2260 struct ldlm_extent *extent = extentp;
2264 OBD_FAIL_RETURN(OBD_FAIL_OSC_MATCH, -EIO);
2266 /* Filesystem lock extents are extended to page boundaries so that
2267 * dealing with the page cache is a little smoother */
2268 extent->start -= extent->start & ~PAGE_MASK;
2269 extent->end |= ~PAGE_MASK;
2271 /* Next, search for already existing extent locks that will cover us */
2272 rc = ldlm_lock_match(obd->obd_namespace, *flags, &res_id, type,
2273 extent, sizeof(*extent), mode, lockh);
2275 osc_set_data_with_check(lockh, data);
2278 /* If we're trying to read, we also search for an existing PW lock. The
2279 * VFS and page cache already protect us locally, so lots of readers/
2280 * writers can share a single PW lock. */
2281 if (mode == LCK_PR) {
2282 rc = ldlm_lock_match(obd->obd_namespace, *flags, &res_id, type,
2283 extent, sizeof(*extent), LCK_PW, lockh);
2285 /* FIXME: This is not incredibly elegant, but it might
2286 * be more elegant than adding another parameter to
2287 * lock_match. I want a second opinion. */
2288 osc_set_data_with_check(lockh, data);
2289 ldlm_lock_addref(lockh, LCK_PR);
2290 ldlm_lock_decref(lockh, LCK_PW);
2296 static int osc_cancel(struct obd_export *exp, struct lov_stripe_md *md,
2297 __u32 mode, struct lustre_handle *lockh)
2301 ldlm_lock_decref(lockh, mode);
2306 static int osc_cancel_unused(struct obd_export *exp,
2307 struct lov_stripe_md *lsm, int flags, void *opaque)
2309 struct obd_device *obd = class_exp2obd(exp);
2310 struct ldlm_res_id res_id = { .name = {lsm->lsm_object_id} };
2312 return ldlm_cli_cancel_unused(obd->obd_namespace, &res_id, flags,
2316 static int osc_statfs(struct obd_device *obd, struct obd_statfs *osfs,
2317 unsigned long max_age)
2319 struct obd_statfs *msfs;
2320 struct ptlrpc_request *request;
2321 int rc, size = sizeof(*osfs);
2324 /* We could possibly pass max_age in the request (as an absolute
2325 * timestamp or a "seconds.usec ago") so the target can avoid doing
2326 * extra calls into the filesystem if that isn't necessary (e.g.
2327 * during mount that would help a bit). Having relative timestamps
2328 * is not so great if request processing is slow, while absolute
2329 * timestamps are not ideal because they need time synchronization. */
2330 request = ptlrpc_prep_req(obd->u.cli.cl_import, OST_STATFS,0,NULL,NULL);
2334 request->rq_replen = lustre_msg_size(1, &size);
2335 request->rq_request_portal = OST_CREATE_PORTAL; //XXX FIXME bug 249
2337 rc = ptlrpc_queue_wait(request);
2341 msfs = lustre_swab_repbuf(request, 0, sizeof(*msfs),
2342 lustre_swab_obd_statfs);
2344 CERROR("Can't unpack obd_statfs\n");
2345 GOTO(out, rc = -EPROTO);
2348 memcpy(osfs, msfs, sizeof(*osfs));
2352 ptlrpc_req_finished(request);
2356 /* Retrieve object striping information.
2358 * @lmmu is a pointer to an in-core struct with lmm_ost_count indicating
2359 * the maximum number of OST indices which will fit in the user buffer.
2360 * lmm_magic must be LOV_MAGIC (we only use 1 slot here).
2362 static int osc_getstripe(struct lov_stripe_md *lsm, struct lov_user_md *lump)
2364 struct lov_user_md lum;
2365 struct lov_mds_md *lmmk;
2372 rc = copy_from_user(&lum, lump, sizeof(lum));
2376 if (lum.lmm_magic != LOV_USER_MAGIC)
2379 if (lum.lmm_stripe_count < 1)
2382 lmm_size = sizeof(lum) + sizeof(lum.lmm_objects[0]);
2383 OBD_ALLOC(lmmk, lmm_size);
2387 lmmk->lmm_stripe_count = 1;
2388 lmmk->lmm_object_id = lsm->lsm_object_id;
2389 lmmk->lmm_objects[0].l_object_id = lsm->lsm_object_id;
2391 if (copy_to_user(lump, lmmk, lmm_size))
2394 OBD_FREE(lmmk, lmm_size);
2399 static int osc_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
2400 void *karg, void *uarg)
2402 struct obd_device *obd = exp->exp_obd;
2403 struct obd_ioctl_data *data = karg;
2408 case OBD_IOC_LOV_GET_CONFIG: {
2410 struct lov_desc *desc;
2411 struct obd_uuid uuid;
2415 if (obd_ioctl_getdata(&buf, &len, (void *)uarg))
2416 GOTO(out, err = -EINVAL);
2418 data = (struct obd_ioctl_data *)buf;
2420 if (sizeof(*desc) > data->ioc_inllen1) {
2422 GOTO(out, err = -EINVAL);
2425 if (data->ioc_inllen2 < sizeof(uuid)) {
2427 GOTO(out, err = -EINVAL);
2430 desc = (struct lov_desc *)data->ioc_inlbuf1;
2431 desc->ld_tgt_count = 1;
2432 desc->ld_active_tgt_count = 1;
2433 desc->ld_default_stripe_count = 1;
2434 desc->ld_default_stripe_size = 0;
2435 desc->ld_default_stripe_offset = 0;
2436 desc->ld_pattern = 0;
2437 memcpy(&desc->ld_uuid, &obd->obd_uuid, sizeof(uuid));
2439 memcpy(data->ioc_inlbuf2, &obd->obd_uuid, sizeof(uuid));
2441 err = copy_to_user((void *)uarg, buf, len);
2444 obd_ioctl_freedata(buf, len);
2447 case LL_IOC_LOV_SETSTRIPE:
2448 err = obd_alloc_memmd(exp, karg);
2452 case LL_IOC_LOV_GETSTRIPE:
2453 err = osc_getstripe(karg, uarg);
2455 case OBD_IOC_CLIENT_RECOVER:
2456 err = ptlrpc_recover_import(obd->u.cli.cl_import,
2461 case IOC_OSC_SET_ACTIVE:
2462 err = ptlrpc_set_import_active(obd->u.cli.cl_import,
2466 CDEBUG(D_INODE, "unrecognised ioctl %#x by %s\n", cmd, current->comm);
2467 GOTO(out, err = -ENOTTY);
2473 static int osc_get_info(struct obd_export *exp, obd_count keylen,
2474 void *key, __u32 *vallen, void *val)
2477 if (!vallen || !val)
2480 if (keylen > strlen("lock_to_stripe") &&
2481 strcmp(key, "lock_to_stripe") == 0) {
2482 __u32 *stripe = val;
2483 *vallen = sizeof(*stripe);
2486 } else if (keylen >= strlen("last_id") && strcmp(key, "last_id") == 0) {
2487 struct ptlrpc_request *req;
2489 char *bufs[1] = {key};
2491 req = ptlrpc_prep_req(class_exp2cliimp(exp), OST_GET_INFO, 1,
2496 req->rq_replen = lustre_msg_size(1, vallen);
2497 rc = ptlrpc_queue_wait(req);
2501 reply = lustre_swab_repbuf(req, 0, sizeof(*reply),
2502 lustre_swab_ost_last_id);
2503 if (reply == NULL) {
2504 CERROR("Can't unpack OST last ID\n");
2505 GOTO(out, rc = -EPROTO);
2507 *((obd_id *)val) = *reply;
2509 ptlrpc_req_finished(req);
2515 static int osc_set_info(struct obd_export *exp, obd_count keylen,
2516 void *key, obd_count vallen, void *val)
2518 struct ptlrpc_request *req;
2519 struct obd_import *imp = class_exp2cliimp(exp);
2520 struct llog_ctxt *ctxt;
2521 int rc, size = keylen;
2522 char *bufs[1] = {key};
2525 if (keylen == strlen("next_id") &&
2526 memcmp(key, "next_id", strlen("next_id")) == 0) {
2527 if (vallen != sizeof(obd_id))
2529 exp->u.eu_osc_data.oed_oscc.oscc_next_id = *((obd_id*)val) + 1;
2530 CDEBUG(D_INODE, "%s: set oscc_next_id = "LPU64"\n",
2531 exp->exp_obd->obd_name,
2532 exp->u.eu_osc_data.oed_oscc.oscc_next_id);
2537 if (keylen == strlen("growth_count") &&
2538 memcmp(key, "growth_count", strlen("growth_count")) == 0) {
2539 if (vallen != sizeof(int))
2541 exp->u.eu_osc_data.oed_oscc.oscc_grow_count = *((int*)val);
2545 if (keylen == strlen("unlinked") &&
2546 memcmp(key, "unlinked", keylen) == 0) {
2547 struct osc_creator *oscc = &exp->u.eu_osc_data.oed_oscc;
2548 spin_lock(&oscc->oscc_lock);
2549 oscc->oscc_flags &= ~OSCC_FLAG_NOSPC;
2550 spin_unlock(&oscc->oscc_lock);
2554 if (keylen < strlen("mds_conn") ||
2555 memcmp(key, "mds_conn", strlen("mds_conn")) != 0)
2559 req = ptlrpc_prep_req(imp, OST_SET_INFO, 1, &size, bufs);
2563 req->rq_replen = lustre_msg_size(0, NULL);
2564 rc = ptlrpc_queue_wait(req);
2565 ptlrpc_req_finished(req);
2567 ctxt = llog_get_context(exp->exp_obd, LLOG_UNLINK_ORIG_CTXT);
2569 rc = llog_initiator_connect(ctxt);
2574 imp->imp_server_timeout = 1;
2575 CDEBUG(D_HA, "pinging OST %s\n", imp->imp_target_uuid.uuid);
2576 ptlrpc_pinger_add_import(imp);
2582 static struct llog_operations osc_size_repl_logops = {
2583 lop_cancel: llog_obd_repl_cancel
2586 static struct llog_operations osc_unlink_orig_logops;
2587 static int osc_llog_init(struct obd_device *obd, struct obd_device *tgt,
2588 int count, struct llog_logid *logid)
2593 osc_unlink_orig_logops = llog_lvfs_ops;
2594 osc_unlink_orig_logops.lop_setup = llog_obd_origin_setup;
2595 osc_unlink_orig_logops.lop_cleanup = llog_obd_origin_cleanup;
2596 osc_unlink_orig_logops.lop_add = llog_obd_origin_add;
2597 osc_unlink_orig_logops.lop_connect = llog_origin_connect;
2599 rc = llog_setup(obd, LLOG_UNLINK_ORIG_CTXT, tgt, count, logid,
2600 &osc_unlink_orig_logops);
2604 rc = llog_setup(obd, LLOG_SIZE_REPL_CTXT, tgt, count, NULL,
2605 &osc_size_repl_logops);
2609 static int osc_llog_finish(struct obd_device *obd, int count)
2614 rc = llog_cleanup(llog_get_context(obd, LLOG_UNLINK_ORIG_CTXT));
2618 rc = llog_cleanup(llog_get_context(obd, LLOG_SIZE_REPL_CTXT));
2623 static int osc_connect(struct lustre_handle *exph,
2624 struct obd_device *obd, struct obd_uuid *cluuid)
2627 struct obd_export *exp;
2629 rc = client_connect_import(exph, obd, cluuid);
2631 if (obd->u.cli.cl_conn_count == 1) {
2632 exp = class_conn2export(exph);
2639 static int osc_disconnect(struct obd_export *exp, int flags)
2641 struct obd_device *obd = class_exp2obd(exp);
2642 struct llog_ctxt *ctxt = llog_get_context(obd, LLOG_SIZE_REPL_CTXT);
2645 if (obd->u.cli.cl_conn_count == 1) {
2646 /* flush any remaining cancel messages out to the target */
2647 llog_sync(ctxt, exp);
2649 /* balance the conn2export for oscc in osc_connect */
2650 class_export_put(exp);
2653 rc = client_disconnect_export(exp, flags);
2657 static int osc_lock_contains(struct obd_export *exp, struct lov_stripe_md *lsm,
2658 struct ldlm_lock *lock, obd_off offset)
2664 if (lock->l_policy_data.l_extent.start <= offset &&
2665 lock->l_policy_data.l_extent.end >= offset)
2670 static int osc_invalidate_import(struct obd_device *obd,
2671 struct obd_import *imp)
2673 struct client_obd *cli;
2674 LASSERT(imp->imp_obd == obd);
2675 /* this used to try and tear down queued pages, but it was
2676 * not correctly implemented. We'll have to do it again once
2677 * we call obd_invalidate_import() agian */
2678 /* XXX And we still need to do this */
2680 /* Reset grants, too */
2682 spin_lock(&cli->cl_loi_list_lock);
2683 cli->cl_ost_can_grant = cli->cl_dirty_granted = 0;
2684 spin_unlock(&cli->cl_loi_list_lock);
2689 int osc_setup(struct obd_device *obd, obd_count len, void *buf)
2693 rc = ptlrpcd_addref();
2697 rc = client_obd_setup(obd, len, buf);
2703 int osc_cleanup(struct obd_device *obd, int flags)
2707 rc = client_obd_cleanup(obd, flags);
2713 struct obd_ops osc_obd_ops = {
2714 o_owner: THIS_MODULE,
2715 o_attach: osc_attach,
2716 o_detach: osc_detach,
2718 o_cleanup: osc_cleanup,
2719 o_connect: osc_connect,
2720 o_disconnect: osc_disconnect,
2721 o_statfs: osc_statfs,
2722 o_packmd: osc_packmd,
2723 o_unpackmd: osc_unpackmd,
2724 o_create: osc_create,
2725 o_destroy: osc_destroy,
2726 o_getattr: osc_getattr,
2727 o_getattr_async:osc_getattr_async,
2728 o_setattr: osc_setattr,
2730 o_brw_async: osc_brw_async,
2731 .o_prep_async_page = osc_prep_async_page,
2732 .o_queue_async_io = osc_queue_async_io,
2733 .o_set_async_flags = osc_set_async_flags,
2734 .o_queue_sync_io = osc_queue_sync_io,
2735 .o_trigger_sync_io = osc_trigger_sync_io,
2736 .o_teardown_async_page = osc_teardown_async_page,
2739 o_enqueue: osc_enqueue,
2741 o_change_cbdata:osc_change_cbdata,
2742 o_cancel: osc_cancel,
2743 o_cancel_unused:osc_cancel_unused,
2744 o_iocontrol: osc_iocontrol,
2745 o_get_info: osc_get_info,
2746 o_set_info: osc_set_info,
2747 o_lock_contains:osc_lock_contains,
2748 o_invalidate_import: osc_invalidate_import,
2749 o_llog_init: osc_llog_init,
2750 o_llog_finish: osc_llog_finish,
2753 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
2754 struct obd_ops sanosc_obd_ops = {
2755 o_owner: THIS_MODULE,
2756 o_attach: osc_attach,
2757 o_detach: osc_detach,
2758 o_cleanup: client_obd_cleanup,
2759 o_connect: osc_connect,
2760 o_disconnect: client_disconnect_export,
2761 o_statfs: osc_statfs,
2762 o_packmd: osc_packmd,
2763 o_unpackmd: osc_unpackmd,
2764 o_create: osc_real_create,
2765 o_destroy: osc_destroy,
2766 o_getattr: osc_getattr,
2767 o_getattr_async:osc_getattr_async,
2768 o_setattr: osc_setattr,
2769 o_setup: client_sanobd_setup,
2773 o_enqueue: osc_enqueue,
2775 o_change_cbdata:osc_change_cbdata,
2776 o_cancel: osc_cancel,
2777 o_cancel_unused:osc_cancel_unused,
2778 o_iocontrol: osc_iocontrol,
2779 o_lock_contains:osc_lock_contains,
2780 o_invalidate_import: osc_invalidate_import,
2781 o_llog_init: osc_llog_init,
2782 o_llog_finish: osc_llog_finish,
2786 int __init osc_init(void)
2788 struct lprocfs_static_vars lvars, sanlvars;
2792 lprocfs_init_vars(osc, &lvars);
2793 lprocfs_init_vars(osc, &sanlvars);
2795 rc = class_register_type(&osc_obd_ops, lvars.module_vars,
2800 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
2801 rc = class_register_type(&sanosc_obd_ops, sanlvars.module_vars,
2802 LUSTRE_SANOSC_NAME);
2804 class_unregister_type(LUSTRE_OSC_NAME);
2810 static void /*__exit*/ osc_exit(void)
2812 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
2813 class_unregister_type(LUSTRE_SANOSC_NAME);
2815 class_unregister_type(LUSTRE_OSC_NAME);
2819 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
2820 MODULE_DESCRIPTION("Lustre Object Storage Client (OSC)");
2821 MODULE_LICENSE("GPL");
2823 module_init(osc_init);
2824 module_exit(osc_exit);