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 int rc = ops->ap_make_ready(oap->oap_caller_data, cmd);
1271 CDEBUG(D_INODE, "oap %p page %p returned %d "
1272 "instead of ready\n", oap,
1276 /* llite is telling us that the page is still
1277 * in commit_write and that we should try
1278 * and put it in an rpc again later. we
1279 * break out of the loop so we don't create
1280 * a whole in the sequence of pages in
1285 /* the io isn't needed.. tell the checks
1286 * below to complete the rpc with EINTR */
1287 oap->oap_async_flags |= ASYNC_COUNT_STABLE;
1288 oap->oap_count = -EINTR;
1291 oap->oap_async_flags |= ASYNC_READY;
1294 LASSERTF(0, "oap %p page %p returned %d "
1295 "from make_ready\n", oap,
1303 /* take the page out of our book-keeping */
1304 list_del_init(&oap->oap_pending_item);
1305 lop_update_pending(cli, lop, cmd, -1);
1306 if (!list_empty(&oap->oap_urgent_item))
1307 list_del_init(&oap->oap_urgent_item);
1309 /* ask the caller for the size of the io as the rpc leaves. */
1310 if (!(oap->oap_async_flags & ASYNC_COUNT_STABLE))
1311 oap->oap_count = ops->ap_refresh_count(
1312 oap->oap_caller_data,
1314 if (oap->oap_count <= 0) {
1315 CDEBUG(D_INODE, "oap %p count %d, completing\n", oap,
1317 osc_complete_oap(cli, oap, oap->oap_count);
1321 /* now put the page back in our accounting */
1322 list_add_tail(&oap->oap_rpc_item, &rpc_list);
1323 if (++page_count >= cli->cl_max_pages_per_rpc)
1327 if (page_count == 0)
1330 spin_unlock(&cli->cl_loi_list_lock);
1332 request = osc_build_req(cli, &rpc_list, page_count, cmd);
1333 if (IS_ERR(request)) {
1334 /* this should happen rarely and is pretty bad, it makes the
1335 * pending list not follow the dirty order */
1336 spin_lock(&cli->cl_loi_list_lock);
1337 list_for_each_safe(pos, tmp, &rpc_list) {
1338 oap = list_entry(pos, struct osc_async_page,
1340 list_del_init(&oap->oap_rpc_item);
1341 list_add_tail(&oap->oap_pending_item,
1343 lop_update_pending(cli, lop, cmd, 1);
1344 if (oap->oap_async_flags & ASYNC_URGENT)
1345 list_add(&oap->oap_urgent_item,
1348 RETURN(PTR_ERR(request));
1351 LASSERT(sizeof(*aa) <= sizeof(request->rq_async_args));
1352 aa = (struct osc_brw_async_args *)&request->rq_async_args;
1353 INIT_LIST_HEAD(&aa->aa_oaps);
1354 list_splice(&rpc_list, &aa->aa_oaps);
1355 INIT_LIST_HEAD(&rpc_list);
1357 if (cmd == OBD_BRW_READ)
1358 lprocfs_oh_tally_log2(&cli->cl_read_page_hist, page_count);
1360 lprocfs_oh_tally_log2(&cli->cl_write_page_hist, page_count);
1362 spin_lock(&cli->cl_loi_list_lock);
1363 if (cmd == OBD_BRW_READ)
1364 lprocfs_oh_tally(&cli->cl_read_rpc_hist, cli->cl_brw_in_flight);
1366 lprocfs_oh_tally(&cli->cl_write_rpc_hist,
1367 cli->cl_brw_in_flight);
1369 cli->cl_brw_in_flight++;
1370 CDEBUG(D_INODE, "req %p: %d pages, aa %p. now %d in flight\n", request,
1371 page_count, aa, cli->cl_brw_in_flight);
1373 request->rq_interpret_reply = brw_interpret_oap;
1374 ptlrpcd_add_req(request);
1378 static int lop_makes_rpc(struct client_obd *cli, struct loi_oap_pages *lop,
1384 /* stream rpcs until we complete the urgent pages in the object */
1385 if (!list_empty(&lop->lop_urgent))
1388 /* fire off rpcs when we have 'optimal' rpcs as tuned for the wire. */
1389 optimal = cli->cl_max_pages_per_rpc;
1390 /* *2 to avoid triggering rpcs that would want to include pages that
1391 * are being queued but which can't be made ready until the queuer
1392 * finishes with the page. this is a wart for llite::commit_write() */
1393 if (cmd == OBD_BRW_WRITE)
1395 if (lop->lop_num_pending >= optimal)
1398 /* trigger a write rpc stream as long as there are dirtiers waiting
1399 * for space. as they're waiting, they're not going to create more
1400 * pages to coallesce with what's waiting.. */
1401 if (!list_empty(&cli->cl_cache_waiters))
1407 static int loi_makes_rpc(struct client_obd *cli, struct lov_oinfo *loi)
1409 return lop_makes_rpc(cli, &loi->loi_write_lop, OBD_BRW_WRITE) ||
1410 lop_makes_rpc(cli, &loi->loi_read_lop, OBD_BRW_READ);
1413 static void loi_onto_ready_list(struct client_obd *cli, struct lov_oinfo *loi)
1415 if (list_empty(&loi->loi_cli_item) && loi_makes_rpc(cli, loi))
1416 list_add_tail(&loi->loi_cli_item, &cli->cl_loi_ready_list);
1419 #define LOI_DEBUG(LOI, STR, args...) \
1420 CDEBUG(D_INODE, "loi rdy %d [%p,%p] wr %d:%d rd %d:%d " STR, \
1421 !list_empty(&(LOI)->loi_cli_item), \
1422 (LOI)->loi_cli_item.next, \
1423 (LOI)->loi_cli_item.prev, \
1424 (LOI)->loi_write_lop.lop_num_pending, \
1425 !list_empty(&(LOI)->loi_write_lop.lop_urgent), \
1426 (LOI)->loi_read_lop.lop_num_pending, \
1427 !list_empty(&(LOI)->loi_read_lop.lop_urgent), \
1430 /* called with the loi list lock held */
1431 static void osc_check_rpcs(struct client_obd *cli)
1433 struct lov_oinfo *loi;
1434 int rc = 0, making_progress;
1437 if (list_empty(&cli->cl_loi_ready_list)) {
1438 CDEBUG(D_INODE, "no lois ready\n");
1443 while (!list_empty(&cli->cl_loi_ready_list)) {
1444 loi = list_entry(cli->cl_loi_ready_list.next, struct lov_oinfo,
1447 if (cli->cl_brw_in_flight >= cli->cl_max_rpcs_in_flight)
1450 making_progress = 0;
1452 /* hmm, it occurs to me that having rpc preparation fail
1453 * with num_pending == num_urgent means that there won't
1454 * be any more calls into here unless other traffic comes
1457 /* attempt some read/write balancing by alternating between
1458 * reads and writes in an object */
1459 if (lop_makes_rpc(cli, &loi->loi_write_lop, OBD_BRW_WRITE)) {
1460 rc = osc_send_oap_rpc(cli, OBD_BRW_WRITE,
1461 &loi->loi_write_lop);
1467 if (lop_makes_rpc(cli, &loi->loi_read_lop, OBD_BRW_READ)) {
1468 rc = osc_send_oap_rpc(cli, OBD_BRW_READ,
1469 &loi->loi_read_lop);
1476 /* attempt some inter-object balancing by issueing rpcs
1477 * for each object in turn */
1478 if (!list_empty(&loi->loi_cli_item))
1479 list_del_init(&loi->loi_cli_item);
1481 loi_onto_ready_list(cli, loi);
1483 LOI_DEBUG(loi, "mp %d\n", making_progress);
1485 /* could be smarter, !making_progress can happen in theory
1486 * if all the pages can not be locked in set_io_ready */
1487 if (!making_progress)
1493 /* we're trying to queue a page in the osc so we're subject to the
1494 * 'cl_dirty_max' limit on the number of pages that can be queued in the osc.
1495 * If the osc's queued pages are already at that limit, then we want to sleep
1496 * until there is space in the osc's queue for us. we need this goofy
1497 * little struct to really tell that our allocation was fulfilled in
1498 * the presence of pending signals */
1499 struct osc_cache_waiter {
1500 struct list_head ocw_entry;
1501 wait_queue_head_t ocw_waitq;
1504 static int ocw_granted(struct client_obd *cli, struct osc_cache_waiter *ocw)
1508 spin_lock(&cli->cl_loi_list_lock);
1509 rc = list_empty(&ocw->ocw_entry);
1510 spin_unlock(&cli->cl_loi_list_lock);
1514 static inline obd_size osc_cache_cap(struct client_obd *cli)
1516 if (cli->cl_ost_can_grant)
1517 return min(cli->cl_dirty_granted, cli->cl_dirty_max);
1519 return cli->cl_dirty_max;
1521 void osc_adjust_cache(struct client_obd *cli)
1523 struct list_head *l, *tmp;
1524 struct osc_cache_waiter *ocw;
1525 obd_size cache_cap = osc_cache_cap(cli);
1529 list_for_each_safe(l, tmp, &cli->cl_cache_waiters) {
1530 if (cli->cl_dirty + PAGE_SIZE > cache_cap &&
1531 cache_cap >= PAGE_SIZE)
1534 ocw = list_entry(l, struct osc_cache_waiter, ocw_entry);
1535 list_del_init(&ocw->ocw_entry);
1536 if (cache_cap < PAGE_SIZE) {
1537 /* "They" said we are starting synchronous operations, so
1538 wakeup everybody waiting for pages in cache and make them
1539 go away unsatisfied. */
1540 ocw->ocw_rc = -EDQUOT;
1542 cli->cl_dirty += PAGE_SIZE;
1544 wake_up(&ocw->ocw_waitq);
1549 static int osc_enter_cache(struct client_obd *cli, struct lov_oinfo *loi,
1550 struct osc_async_page *oap)
1552 struct osc_cache_waiter ocw;
1553 struct l_wait_info lwi = {0};
1557 if (osc_cache_cap(cli) < PAGE_SIZE)
1560 /* if we fail this test then cl_dirty contains at least one page
1561 * that will have to be completed after we release the lock */
1562 if (cli->cl_dirty + PAGE_SIZE <= osc_cache_cap(cli)) {
1563 /* account for ourselves */
1564 cli->cl_dirty += PAGE_SIZE;
1568 init_waitqueue_head(&ocw.ocw_waitq);
1570 list_add_tail(&ocw.ocw_entry, &cli->cl_cache_waiters);
1572 /* make sure that there are write rpcs in flight to wait for. this
1573 * is a little silly as this object may not have any pending
1574 * but other objects sure might. this should probably be cleaned. */
1575 loi_onto_ready_list(cli, loi);
1576 osc_check_rpcs(cli);
1577 spin_unlock(&cli->cl_loi_list_lock);
1579 CDEBUG(D_INODE, "sleeping for cache space\n");
1580 l_wait_event(ocw.ocw_waitq, ocw_granted(cli, &ocw), &lwi);
1582 spin_lock(&cli->cl_loi_list_lock);
1584 if (!list_empty(&ocw.ocw_entry)) {
1586 list_del(&ocw.ocw_entry);
1591 oap->oap_brw_flags |= OBD_BRW_FROM_GRANT;
1595 /* the companion to enter_cache, called when an oap is now longer part of the
1596 * dirty accounting.. so writeback completes or truncate happens before writing
1597 * starts. must be called with the loi lock held. */
1598 static void osc_exit_cache(struct client_obd *cli, struct osc_async_page *oap)
1600 struct osc_cache_waiter *ocw;
1603 if (!(oap->oap_brw_flags & OBD_BRW_FROM_GRANT)) {
1608 /* If nobody waits for cache space or if we need to shrink it */
1609 if (list_empty(&cli->cl_cache_waiters) ||
1610 (cli->cl_dirty > osc_cache_cap(cli))) {
1611 cli->cl_dirty -= PAGE_SIZE;
1613 ocw = list_entry(cli->cl_cache_waiters.next,
1614 struct osc_cache_waiter, ocw_entry);
1615 list_del_init(&ocw->ocw_entry);
1616 wake_up(&ocw->ocw_waitq);
1619 oap->oap_brw_flags &= ~OBD_BRW_FROM_GRANT;
1623 int osc_prep_async_page(struct obd_export *exp, struct lov_stripe_md *lsm,
1624 struct lov_oinfo *loi, struct page *page,
1625 obd_off offset, struct obd_async_page_ops *ops,
1626 void *data, void **res)
1628 struct osc_async_page *oap;
1631 OBD_ALLOC(oap, sizeof(*oap));
1635 oap->oap_magic = OAP_MAGIC;
1636 oap->oap_caller_ops = ops;
1637 oap->oap_caller_data = data;
1639 oap->oap_page = page;
1640 oap->oap_obj_off = offset;
1642 INIT_LIST_HEAD(&oap->oap_pending_item);
1643 INIT_LIST_HEAD(&oap->oap_urgent_item);
1644 INIT_LIST_HEAD(&oap->oap_rpc_item);
1646 CDEBUG(D_CACHE, "oap %p page %p obj off "LPU64"\n", oap, page, offset);
1651 struct osc_async_page *oap_from_cookie(void *cookie)
1653 struct osc_async_page *oap = cookie;
1654 if (oap->oap_magic != OAP_MAGIC)
1655 return ERR_PTR(-EINVAL);
1659 static int osc_queue_async_io(struct obd_export *exp, struct lov_stripe_md *lsm,
1660 struct lov_oinfo *loi, void *cookie,
1661 int cmd, obd_off off, int count,
1662 obd_flag brw_flags, enum async_flags async_flags)
1664 struct client_obd *cli = &exp->exp_obd->u.cli;
1665 struct osc_async_page *oap;
1666 struct loi_oap_pages *lop;
1670 oap = oap_from_cookie(cookie);
1672 RETURN(PTR_ERR(oap));
1674 if (cli->cl_import == NULL || cli->cl_import->imp_invalid)
1677 if (!list_empty(&oap->oap_pending_item) ||
1678 !list_empty(&oap->oap_urgent_item) ||
1679 !list_empty(&oap->oap_rpc_item))
1683 loi = &lsm->lsm_oinfo[0];
1685 spin_lock(&cli->cl_loi_list_lock);
1688 oap->oap_async_flags = async_flags;
1689 oap->oap_page_off = off;
1690 oap->oap_count = count;
1691 oap->oap_brw_flags = brw_flags;
1693 if (cmd == OBD_BRW_WRITE) {
1694 rc = osc_enter_cache(cli, loi, oap);
1696 spin_unlock(&cli->cl_loi_list_lock);
1699 lop = &loi->loi_write_lop;
1701 lop = &loi->loi_read_lop;
1704 if (oap->oap_async_flags & ASYNC_URGENT)
1705 list_add(&oap->oap_urgent_item, &lop->lop_urgent);
1706 list_add_tail(&oap->oap_pending_item, &lop->lop_pending);
1707 lop_update_pending(cli, lop, cmd, 1);
1709 loi_onto_ready_list(cli, loi);
1711 LOI_DEBUG(loi, "oap %p page %p added for cmd %d\n", oap, oap->oap_page,
1714 osc_check_rpcs(cli);
1715 spin_unlock(&cli->cl_loi_list_lock);
1720 /* aka (~was & now & flag), but this is more clear :) */
1721 #define SETTING(was, now, flag) (!(was & flag) && (now & flag))
1723 static int osc_set_async_flags(struct obd_export *exp,
1724 struct lov_stripe_md *lsm,
1725 struct lov_oinfo *loi, void *cookie,
1726 obd_flag async_flags)
1728 struct client_obd *cli = &exp->exp_obd->u.cli;
1729 struct loi_oap_pages *lop;
1730 struct osc_async_page *oap;
1734 oap = oap_from_cookie(cookie);
1736 RETURN(PTR_ERR(oap));
1738 if (cli->cl_import == NULL || cli->cl_import->imp_invalid)
1742 loi = &lsm->lsm_oinfo[0];
1744 if (oap->oap_cmd == OBD_BRW_WRITE) {
1745 lop = &loi->loi_write_lop;
1747 lop = &loi->loi_read_lop;
1750 spin_lock(&cli->cl_loi_list_lock);
1752 if (list_empty(&oap->oap_pending_item))
1753 GOTO(out, rc = -EINVAL);
1755 if ((oap->oap_async_flags & async_flags) == async_flags)
1758 if (SETTING(oap->oap_async_flags, async_flags, ASYNC_READY))
1759 oap->oap_async_flags |= ASYNC_READY;
1761 if (SETTING(oap->oap_async_flags, async_flags, ASYNC_URGENT)) {
1762 if (list_empty(&oap->oap_rpc_item)) {
1763 list_add(&oap->oap_urgent_item, &lop->lop_urgent);
1764 loi_onto_ready_list(cli, loi);
1768 LOI_DEBUG(loi, "oap %p page %p has flags %x\n", oap, oap->oap_page,
1769 oap->oap_async_flags);
1771 osc_check_rpcs(cli);
1772 spin_unlock(&cli->cl_loi_list_lock);
1776 static int osc_queue_sync_io(struct obd_export *exp, struct lov_stripe_md *lsm,
1777 struct lov_oinfo *loi,
1778 struct obd_sync_io_container *osic, void *cookie,
1779 int cmd, obd_off off, int count,
1782 struct client_obd *cli = &exp->exp_obd->u.cli;
1783 struct osc_async_page *oap;
1784 struct loi_oap_pages *lop;
1787 oap = oap_from_cookie(cookie);
1789 RETURN(PTR_ERR(oap));
1791 if (cli->cl_import == NULL || cli->cl_import->imp_invalid)
1794 if (!list_empty(&oap->oap_pending_item) ||
1795 !list_empty(&oap->oap_urgent_item) ||
1796 !list_empty(&oap->oap_rpc_item))
1800 loi = &lsm->lsm_oinfo[0];
1802 spin_lock(&cli->cl_loi_list_lock);
1805 oap->oap_page_off = off;
1806 oap->oap_count = count;
1807 oap->oap_brw_flags = brw_flags;
1809 if (cmd == OBD_BRW_WRITE)
1810 lop = &loi->loi_write_lop;
1812 lop = &loi->loi_read_lop;
1814 list_add_tail(&oap->oap_pending_item, &lop->lop_pending_sync);
1815 oap->oap_osic = osic;
1818 LOI_DEBUG(loi, "oap %p page %p on sync pending\n", oap, oap->oap_page);
1820 spin_unlock(&cli->cl_loi_list_lock);
1825 static void osc_sync_to_pending(struct client_obd *cli, struct lov_oinfo *loi,
1826 struct loi_oap_pages *lop, int cmd)
1828 struct list_head *pos, *tmp;
1829 struct osc_async_page *oap;
1831 list_for_each_safe(pos, tmp, &lop->lop_pending_sync) {
1832 oap = list_entry(pos, struct osc_async_page, oap_pending_item);
1833 list_del(&oap->oap_pending_item);
1834 oap->oap_async_flags |= ASYNC_READY | ASYNC_URGENT |
1836 list_add_tail(&oap->oap_pending_item, &lop->lop_pending);
1837 list_add(&oap->oap_urgent_item, &lop->lop_urgent);
1838 lop_update_pending(cli, lop, cmd, 1);
1840 loi_onto_ready_list(cli, loi);
1843 static int osc_trigger_sync_io(struct obd_export *exp,
1844 struct lov_stripe_md *lsm,
1845 struct lov_oinfo *loi,
1846 struct obd_sync_io_container *osic)
1848 struct client_obd *cli = &exp->exp_obd->u.cli;
1851 if (cli->cl_import == NULL || cli->cl_import->imp_invalid)
1855 loi = &lsm->lsm_oinfo[0];
1857 spin_lock(&cli->cl_loi_list_lock);
1859 osc_sync_to_pending(cli, loi, &loi->loi_write_lop, OBD_BRW_WRITE);
1860 osc_sync_to_pending(cli, loi, &loi->loi_read_lop, OBD_BRW_READ);
1862 osc_check_rpcs(cli);
1863 spin_unlock(&cli->cl_loi_list_lock);
1868 static int osc_teardown_async_page(struct obd_export *exp,
1869 struct lov_stripe_md *lsm,
1870 struct lov_oinfo *loi, void *cookie)
1872 struct client_obd *cli = &exp->exp_obd->u.cli;
1873 struct loi_oap_pages *lop;
1874 struct osc_async_page *oap;
1878 oap = oap_from_cookie(cookie);
1880 RETURN(PTR_ERR(oap));
1883 loi = &lsm->lsm_oinfo[0];
1885 if (oap->oap_cmd == OBD_BRW_WRITE) {
1886 lop = &loi->loi_write_lop;
1888 lop = &loi->loi_read_lop;
1891 spin_lock(&cli->cl_loi_list_lock);
1893 osc_exit_cache(cli, oap);
1895 if (!list_empty(&oap->oap_rpc_item))
1896 GOTO(out, rc = -EBUSY);
1898 if (!list_empty(&oap->oap_urgent_item)) {
1899 list_del_init(&oap->oap_urgent_item);
1900 oap->oap_async_flags &= ~ASYNC_URGENT;
1902 if (!list_empty(&oap->oap_pending_item)) {
1903 list_del_init(&oap->oap_pending_item);
1904 lop_update_pending(cli, lop, oap->oap_cmd, -1);
1906 if (!list_empty(&loi->loi_cli_item) && !loi_makes_rpc(cli, loi))
1907 list_del_init(&loi->loi_cli_item);
1909 LOI_DEBUG(loi, "oap %p page %p torn down\n", oap, oap->oap_page);
1911 spin_unlock(&cli->cl_loi_list_lock);
1912 OBD_FREE(oap, sizeof(*oap));
1917 /* Note: caller will lock/unlock, and set uptodate on the pages */
1918 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
1919 static int sanosc_brw_read(struct obd_export *exp, struct obdo *oa,
1920 struct lov_stripe_md *lsm, obd_count page_count,
1921 struct brw_page *pga)
1923 struct ptlrpc_request *request = NULL;
1924 struct ost_body *body;
1925 struct niobuf_remote *nioptr;
1926 struct obd_ioobj *iooptr;
1927 int rc, size[3] = {sizeof(*body)}, mapped = 0;
1931 /* XXX does not handle 'new' brw protocol */
1933 size[1] = sizeof(struct obd_ioobj);
1934 size[2] = page_count * sizeof(*nioptr);
1936 request = ptlrpc_prep_req(class_exp2cliimp(exp), OST_SAN_READ, 3,
1941 body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof(*body));
1942 iooptr = lustre_msg_buf(request->rq_reqmsg, 1, sizeof(*iooptr));
1943 nioptr = lustre_msg_buf(request->rq_reqmsg, 2,
1944 sizeof(*nioptr) * page_count);
1946 memcpy(&body->oa, oa, sizeof(body->oa));
1948 obdo_to_ioobj(oa, iooptr);
1949 iooptr->ioo_bufcnt = page_count;
1951 for (mapped = 0; mapped < page_count; mapped++, nioptr++) {
1952 LASSERT(PageLocked(pga[mapped].pg));
1953 LASSERT(mapped == 0 || pga[mapped].off > pga[mapped - 1].off);
1955 nioptr->offset = pga[mapped].off;
1956 nioptr->len = pga[mapped].count;
1957 nioptr->flags = pga[mapped].flag;
1960 size[1] = page_count * sizeof(*nioptr);
1961 request->rq_replen = lustre_msg_size(2, size);
1963 rc = ptlrpc_queue_wait(request);
1967 body = lustre_swab_repbuf(request, 0, sizeof(*body),
1968 lustre_swab_ost_body);
1970 CERROR("Can't unpack body\n");
1971 GOTO(out_req, rc = -EPROTO);
1974 memcpy(oa, &body->oa, sizeof(*oa));
1976 swab = lustre_msg_swabbed(request->rq_repmsg);
1977 LASSERT_REPSWAB(request, 1);
1978 nioptr = lustre_msg_buf(request->rq_repmsg, 1, size[1]);
1980 /* nioptr missing or short */
1981 GOTO(out_req, rc = -EPROTO);
1985 for (mapped = 0; mapped < page_count; mapped++, nioptr++) {
1986 struct page *page = pga[mapped].pg;
1987 struct buffer_head *bh;
1991 lustre_swab_niobuf_remote (nioptr);
1993 /* got san device associated */
1994 LASSERT(exp->exp_obd != NULL);
1995 dev = exp->exp_obd->u.cli.cl_sandev;
1998 if (!nioptr->offset) {
1999 CDEBUG(D_PAGE, "hole at ino %lu; index %ld\n",
2000 page->mapping->host->i_ino,
2002 memset(page_address(page), 0, PAGE_SIZE);
2006 if (!page->buffers) {
2007 create_empty_buffers(page, dev, PAGE_SIZE);
2010 clear_bit(BH_New, &bh->b_state);
2011 set_bit(BH_Mapped, &bh->b_state);
2012 bh->b_blocknr = (unsigned long)nioptr->offset;
2014 clear_bit(BH_Uptodate, &bh->b_state);
2016 ll_rw_block(READ, 1, &bh);
2020 /* if buffer already existed, it must be the
2021 * one we mapped before, check it */
2022 LASSERT(!test_bit(BH_New, &bh->b_state));
2023 LASSERT(test_bit(BH_Mapped, &bh->b_state));
2024 LASSERT(bh->b_blocknr == (unsigned long)nioptr->offset);
2026 /* wait it's io completion */
2027 if (test_bit(BH_Lock, &bh->b_state))
2030 if (!test_bit(BH_Uptodate, &bh->b_state))
2031 ll_rw_block(READ, 1, &bh);
2035 /* must do syncronous write here */
2037 if (!buffer_uptodate(bh)) {
2045 ptlrpc_req_finished(request);
2049 static int sanosc_brw_write(struct obd_export *exp, struct obdo *oa,
2050 struct lov_stripe_md *lsm, obd_count page_count,
2051 struct brw_page *pga)
2053 struct ptlrpc_request *request = NULL;
2054 struct ost_body *body;
2055 struct niobuf_remote *nioptr;
2056 struct obd_ioobj *iooptr;
2057 int rc, size[3] = {sizeof(*body)}, mapped = 0;
2061 size[1] = sizeof(struct obd_ioobj);
2062 size[2] = page_count * sizeof(*nioptr);
2064 request = ptlrpc_prep_req(class_exp2cliimp(exp), OST_SAN_WRITE,
2069 body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof (*body));
2070 iooptr = lustre_msg_buf(request->rq_reqmsg, 1, sizeof (*iooptr));
2071 nioptr = lustre_msg_buf(request->rq_reqmsg, 2,
2072 sizeof (*nioptr) * page_count);
2074 memcpy(&body->oa, oa, sizeof(body->oa));
2076 obdo_to_ioobj(oa, iooptr);
2077 iooptr->ioo_bufcnt = page_count;
2080 for (mapped = 0; mapped < page_count; mapped++, nioptr++) {
2081 LASSERT(PageLocked(pga[mapped].pg));
2082 LASSERT(mapped == 0 || pga[mapped].off > pga[mapped - 1].off);
2084 nioptr->offset = pga[mapped].off;
2085 nioptr->len = pga[mapped].count;
2086 nioptr->flags = pga[mapped].flag;
2089 size[1] = page_count * sizeof(*nioptr);
2090 request->rq_replen = lustre_msg_size(2, size);
2092 rc = ptlrpc_queue_wait(request);
2096 swab = lustre_msg_swabbed (request->rq_repmsg);
2097 LASSERT_REPSWAB (request, 1);
2098 nioptr = lustre_msg_buf(request->rq_repmsg, 1, size[1]);
2100 CERROR("absent/short niobuf array\n");
2101 GOTO(out_req, rc = -EPROTO);
2105 for (mapped = 0; mapped < page_count; mapped++, nioptr++) {
2106 struct page *page = pga[mapped].pg;
2107 struct buffer_head *bh;
2111 lustre_swab_niobuf_remote (nioptr);
2113 /* got san device associated */
2114 LASSERT(exp->exp_obd != NULL);
2115 dev = exp->exp_obd->u.cli.cl_sandev;
2117 if (!page->buffers) {
2118 create_empty_buffers(page, dev, PAGE_SIZE);
2121 LASSERT(!test_bit(BH_New, &page->buffers->b_state));
2122 LASSERT(test_bit(BH_Mapped, &page->buffers->b_state));
2123 LASSERT(page->buffers->b_blocknr ==
2124 (unsigned long)nioptr->offset);
2130 /* if buffer locked, wait it's io completion */
2131 if (test_bit(BH_Lock, &bh->b_state))
2134 clear_bit(BH_New, &bh->b_state);
2135 set_bit(BH_Mapped, &bh->b_state);
2137 /* override the block nr */
2138 bh->b_blocknr = (unsigned long)nioptr->offset;
2140 /* we are about to write it, so set it
2142 * page lock should garentee no race condition here */
2143 set_bit(BH_Uptodate, &bh->b_state);
2144 set_bit(BH_Dirty, &bh->b_state);
2146 ll_rw_block(WRITE, 1, &bh);
2148 /* must do syncronous write here */
2150 if (!buffer_uptodate(bh) || test_bit(BH_Dirty, &bh->b_state)) {
2158 ptlrpc_req_finished(request);
2162 static int sanosc_brw(int cmd, struct obd_export *exp, struct obdo *oa,
2163 struct lov_stripe_md *lsm, obd_count page_count,
2164 struct brw_page *pga, struct obd_trans_info *oti)
2168 while (page_count) {
2169 obd_count pages_per_brw;
2172 if (page_count > OSC_BRW_MAX_IOV)
2173 pages_per_brw = OSC_BRW_MAX_IOV;
2175 pages_per_brw = page_count;
2177 if (cmd & OBD_BRW_WRITE)
2178 rc = sanosc_brw_write(exp, oa, lsm, pages_per_brw,pga);
2180 rc = sanosc_brw_read(exp, oa, lsm, pages_per_brw, pga);
2185 page_count -= pages_per_brw;
2186 pga += pages_per_brw;
2193 static void osc_set_data_with_check(struct lustre_handle *lockh, void *data)
2195 struct ldlm_lock *lock = ldlm_handle2lock(lockh);
2197 LASSERT(lock != NULL);
2198 l_lock(&lock->l_resource->lr_namespace->ns_lock);
2200 if (lock->l_ast_data && lock->l_ast_data != data) {
2201 struct inode *new_inode = data;
2202 struct inode *old_inode = lock->l_ast_data;
2203 unsigned long state = old_inode->i_state & I_FREEING;
2204 CERROR("Found existing inode %p/%lu/%u state %lu in lock: "
2205 "setting data to %p/%lu/%u\n", old_inode,
2206 old_inode->i_ino, old_inode->i_generation, state,
2207 new_inode, new_inode->i_ino, new_inode->i_generation);
2211 lock->l_ast_data = data;
2212 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
2213 LDLM_LOCK_PUT(lock);
2216 static int osc_change_cbdata(struct obd_export *exp, struct lov_stripe_md *lsm,
2217 ldlm_iterator_t replace, void *data)
2219 struct ldlm_res_id res_id = { .name = {lsm->lsm_object_id} };
2220 struct obd_device *obd = class_exp2obd(exp);
2222 ldlm_change_cbdata(obd->obd_namespace, &res_id, replace, data);
2226 static int osc_enqueue(struct obd_export *exp, struct lov_stripe_md *lsm,
2227 struct lustre_handle *parent_lock,
2228 __u32 type, void *extentp, int extent_len, __u32 mode,
2229 int *flags, void *callback, void *data,
2230 struct lustre_handle *lockh)
2232 struct ldlm_res_id res_id = { .name = {lsm->lsm_object_id} };
2233 struct obd_device *obd = exp->exp_obd;
2234 struct ldlm_extent *extent = extentp;
2238 /* Filesystem lock extents are extended to page boundaries so that
2239 * dealing with the page cache is a little smoother. */
2240 extent->start -= extent->start & ~PAGE_MASK;
2241 extent->end |= ~PAGE_MASK;
2243 /* Next, search for already existing extent locks that will cover us */
2244 rc = ldlm_lock_match(obd->obd_namespace, 0, &res_id,
2245 type, extent, sizeof(*extent), mode, lockh);
2247 osc_set_data_with_check(lockh, data);
2248 /* We already have a lock, and it's referenced */
2252 /* If we're trying to read, we also search for an existing PW lock. The
2253 * VFS and page cache already protect us locally, so lots of readers/
2254 * writers can share a single PW lock.
2256 * There are problems with conversion deadlocks, so instead of
2257 * converting a read lock to a write lock, we'll just enqueue a new
2260 * At some point we should cancel the read lock instead of making them
2261 * send us a blocking callback, but there are problems with canceling
2262 * locks out from other users right now, too. */
2264 if (mode == LCK_PR) {
2265 rc = ldlm_lock_match(obd->obd_namespace, 0, &res_id, type,
2266 extent, sizeof(*extent), LCK_PW, lockh);
2268 /* FIXME: This is not incredibly elegant, but it might
2269 * be more elegant than adding another parameter to
2270 * lock_match. I want a second opinion. */
2271 ldlm_lock_addref(lockh, LCK_PR);
2272 ldlm_lock_decref(lockh, LCK_PW);
2273 osc_set_data_with_check(lockh, data);
2278 rc = ldlm_cli_enqueue(exp, NULL, obd->obd_namespace, parent_lock,
2279 res_id, type, extent, sizeof(*extent), mode,
2280 flags,ldlm_completion_ast, callback, data, lockh);
2284 static int osc_match(struct obd_export *exp, struct lov_stripe_md *lsm,
2285 __u32 type, void *extentp, int extent_len, __u32 mode,
2286 int *flags, void *data, struct lustre_handle *lockh)
2288 struct ldlm_res_id res_id = { .name = {lsm->lsm_object_id} };
2289 struct obd_device *obd = exp->exp_obd;
2290 struct ldlm_extent *extent = extentp;
2294 OBD_FAIL_RETURN(OBD_FAIL_OSC_MATCH, -EIO);
2296 /* Filesystem lock extents are extended to page boundaries so that
2297 * dealing with the page cache is a little smoother */
2298 extent->start -= extent->start & ~PAGE_MASK;
2299 extent->end |= ~PAGE_MASK;
2301 /* Next, search for already existing extent locks that will cover us */
2302 rc = ldlm_lock_match(obd->obd_namespace, *flags, &res_id, type,
2303 extent, sizeof(*extent), mode, lockh);
2305 osc_set_data_with_check(lockh, data);
2308 /* If we're trying to read, we also search for an existing PW lock. The
2309 * VFS and page cache already protect us locally, so lots of readers/
2310 * writers can share a single PW lock. */
2311 if (mode == LCK_PR) {
2312 rc = ldlm_lock_match(obd->obd_namespace, *flags, &res_id, type,
2313 extent, sizeof(*extent), LCK_PW, lockh);
2315 /* FIXME: This is not incredibly elegant, but it might
2316 * be more elegant than adding another parameter to
2317 * lock_match. I want a second opinion. */
2318 osc_set_data_with_check(lockh, data);
2319 ldlm_lock_addref(lockh, LCK_PR);
2320 ldlm_lock_decref(lockh, LCK_PW);
2326 static int osc_cancel(struct obd_export *exp, struct lov_stripe_md *md,
2327 __u32 mode, struct lustre_handle *lockh)
2331 ldlm_lock_decref(lockh, mode);
2336 static int osc_cancel_unused(struct obd_export *exp,
2337 struct lov_stripe_md *lsm, int flags, void *opaque)
2339 struct obd_device *obd = class_exp2obd(exp);
2340 struct ldlm_res_id res_id = { .name = {lsm->lsm_object_id} };
2342 return ldlm_cli_cancel_unused(obd->obd_namespace, &res_id, flags,
2346 static int osc_statfs(struct obd_device *obd, struct obd_statfs *osfs,
2347 unsigned long max_age)
2349 struct obd_statfs *msfs;
2350 struct ptlrpc_request *request;
2351 int rc, size = sizeof(*osfs);
2354 /* We could possibly pass max_age in the request (as an absolute
2355 * timestamp or a "seconds.usec ago") so the target can avoid doing
2356 * extra calls into the filesystem if that isn't necessary (e.g.
2357 * during mount that would help a bit). Having relative timestamps
2358 * is not so great if request processing is slow, while absolute
2359 * timestamps are not ideal because they need time synchronization. */
2360 request = ptlrpc_prep_req(obd->u.cli.cl_import, OST_STATFS,0,NULL,NULL);
2364 request->rq_replen = lustre_msg_size(1, &size);
2365 request->rq_request_portal = OST_CREATE_PORTAL; //XXX FIXME bug 249
2367 rc = ptlrpc_queue_wait(request);
2371 msfs = lustre_swab_repbuf(request, 0, sizeof(*msfs),
2372 lustre_swab_obd_statfs);
2374 CERROR("Can't unpack obd_statfs\n");
2375 GOTO(out, rc = -EPROTO);
2378 memcpy(osfs, msfs, sizeof(*osfs));
2382 ptlrpc_req_finished(request);
2386 /* Retrieve object striping information.
2388 * @lmmu is a pointer to an in-core struct with lmm_ost_count indicating
2389 * the maximum number of OST indices which will fit in the user buffer.
2390 * lmm_magic must be LOV_MAGIC (we only use 1 slot here).
2392 static int osc_getstripe(struct lov_stripe_md *lsm, struct lov_user_md *lump)
2394 struct lov_user_md lum;
2395 struct lov_mds_md *lmmk;
2402 rc = copy_from_user(&lum, lump, sizeof(lum));
2406 if (lum.lmm_magic != LOV_USER_MAGIC)
2409 if (lum.lmm_stripe_count < 1)
2412 lmm_size = sizeof(lum) + sizeof(lum.lmm_objects[0]);
2413 OBD_ALLOC(lmmk, lmm_size);
2417 lmmk->lmm_stripe_count = 1;
2418 lmmk->lmm_object_id = lsm->lsm_object_id;
2419 lmmk->lmm_objects[0].l_object_id = lsm->lsm_object_id;
2421 if (copy_to_user(lump, lmmk, lmm_size))
2424 OBD_FREE(lmmk, lmm_size);
2429 static int osc_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
2430 void *karg, void *uarg)
2432 struct obd_device *obd = exp->exp_obd;
2433 struct obd_ioctl_data *data = karg;
2438 case OBD_IOC_LOV_GET_CONFIG: {
2440 struct lov_desc *desc;
2441 struct obd_uuid uuid;
2445 if (obd_ioctl_getdata(&buf, &len, (void *)uarg))
2446 GOTO(out, err = -EINVAL);
2448 data = (struct obd_ioctl_data *)buf;
2450 if (sizeof(*desc) > data->ioc_inllen1) {
2452 GOTO(out, err = -EINVAL);
2455 if (data->ioc_inllen2 < sizeof(uuid)) {
2457 GOTO(out, err = -EINVAL);
2460 desc = (struct lov_desc *)data->ioc_inlbuf1;
2461 desc->ld_tgt_count = 1;
2462 desc->ld_active_tgt_count = 1;
2463 desc->ld_default_stripe_count = 1;
2464 desc->ld_default_stripe_size = 0;
2465 desc->ld_default_stripe_offset = 0;
2466 desc->ld_pattern = 0;
2467 memcpy(&desc->ld_uuid, &obd->obd_uuid, sizeof(uuid));
2469 memcpy(data->ioc_inlbuf2, &obd->obd_uuid, sizeof(uuid));
2471 err = copy_to_user((void *)uarg, buf, len);
2474 obd_ioctl_freedata(buf, len);
2477 case LL_IOC_LOV_SETSTRIPE:
2478 err = obd_alloc_memmd(exp, karg);
2482 case LL_IOC_LOV_GETSTRIPE:
2483 err = osc_getstripe(karg, uarg);
2485 case OBD_IOC_CLIENT_RECOVER:
2486 err = ptlrpc_recover_import(obd->u.cli.cl_import,
2491 case IOC_OSC_SET_ACTIVE:
2492 err = ptlrpc_set_import_active(obd->u.cli.cl_import,
2496 CDEBUG(D_INODE, "unrecognised ioctl %#x by %s\n", cmd, current->comm);
2497 GOTO(out, err = -ENOTTY);
2503 static int osc_get_info(struct obd_export *exp, obd_count keylen,
2504 void *key, __u32 *vallen, void *val)
2507 if (!vallen || !val)
2510 if (keylen > strlen("lock_to_stripe") &&
2511 strcmp(key, "lock_to_stripe") == 0) {
2512 __u32 *stripe = val;
2513 *vallen = sizeof(*stripe);
2516 } else if (keylen >= strlen("last_id") && strcmp(key, "last_id") == 0) {
2517 struct ptlrpc_request *req;
2519 char *bufs[1] = {key};
2521 req = ptlrpc_prep_req(class_exp2cliimp(exp), OST_GET_INFO, 1,
2526 req->rq_replen = lustre_msg_size(1, vallen);
2527 rc = ptlrpc_queue_wait(req);
2531 reply = lustre_swab_repbuf(req, 0, sizeof(*reply),
2532 lustre_swab_ost_last_id);
2533 if (reply == NULL) {
2534 CERROR("Can't unpack OST last ID\n");
2535 GOTO(out, rc = -EPROTO);
2537 *((obd_id *)val) = *reply;
2539 ptlrpc_req_finished(req);
2545 static int osc_set_info(struct obd_export *exp, obd_count keylen,
2546 void *key, obd_count vallen, void *val)
2548 struct ptlrpc_request *req;
2549 struct obd_import *imp = class_exp2cliimp(exp);
2550 struct llog_ctxt *ctxt;
2551 int rc, size = keylen;
2552 char *bufs[1] = {key};
2555 if (keylen == strlen("next_id") &&
2556 memcmp(key, "next_id", strlen("next_id")) == 0) {
2557 if (vallen != sizeof(obd_id))
2559 exp->u.eu_osc_data.oed_oscc.oscc_next_id = *((obd_id*)val) + 1;
2560 CDEBUG(D_INODE, "%s: set oscc_next_id = "LPU64"\n",
2561 exp->exp_obd->obd_name,
2562 exp->u.eu_osc_data.oed_oscc.oscc_next_id);
2567 if (keylen == strlen("growth_count") &&
2568 memcmp(key, "growth_count", strlen("growth_count")) == 0) {
2569 if (vallen != sizeof(int))
2571 exp->u.eu_osc_data.oed_oscc.oscc_grow_count = *((int*)val);
2575 if (keylen == strlen("unlinked") &&
2576 memcmp(key, "unlinked", keylen) == 0) {
2577 struct osc_creator *oscc = &exp->u.eu_osc_data.oed_oscc;
2578 spin_lock(&oscc->oscc_lock);
2579 oscc->oscc_flags &= ~OSCC_FLAG_NOSPC;
2580 spin_unlock(&oscc->oscc_lock);
2584 if (keylen < strlen("mds_conn") ||
2585 memcmp(key, "mds_conn", strlen("mds_conn")) != 0)
2589 req = ptlrpc_prep_req(imp, OST_SET_INFO, 1, &size, bufs);
2593 req->rq_replen = lustre_msg_size(0, NULL);
2594 rc = ptlrpc_queue_wait(req);
2595 ptlrpc_req_finished(req);
2597 ctxt = llog_get_context(exp->exp_obd, LLOG_UNLINK_ORIG_CTXT);
2599 rc = llog_initiator_connect(ctxt);
2604 imp->imp_server_timeout = 1;
2605 CDEBUG(D_HA, "pinging OST %s\n", imp->imp_target_uuid.uuid);
2606 ptlrpc_pinger_add_import(imp);
2612 static struct llog_operations osc_size_repl_logops = {
2613 lop_cancel: llog_obd_repl_cancel
2616 static struct llog_operations osc_unlink_orig_logops;
2617 static int osc_llog_init(struct obd_device *obd, struct obd_device *tgt,
2618 int count, struct llog_logid *logid)
2623 osc_unlink_orig_logops = llog_lvfs_ops;
2624 osc_unlink_orig_logops.lop_setup = llog_obd_origin_setup;
2625 osc_unlink_orig_logops.lop_cleanup = llog_obd_origin_cleanup;
2626 osc_unlink_orig_logops.lop_add = llog_obd_origin_add;
2627 osc_unlink_orig_logops.lop_connect = llog_origin_connect;
2629 rc = llog_setup(obd, LLOG_UNLINK_ORIG_CTXT, tgt, count, logid,
2630 &osc_unlink_orig_logops);
2634 rc = llog_setup(obd, LLOG_SIZE_REPL_CTXT, tgt, count, NULL,
2635 &osc_size_repl_logops);
2639 static int osc_llog_finish(struct obd_device *obd, int count)
2644 rc = llog_cleanup(llog_get_context(obd, LLOG_UNLINK_ORIG_CTXT));
2648 rc = llog_cleanup(llog_get_context(obd, LLOG_SIZE_REPL_CTXT));
2653 static int osc_connect(struct lustre_handle *exph,
2654 struct obd_device *obd, struct obd_uuid *cluuid)
2657 struct obd_export *exp;
2659 rc = client_connect_import(exph, obd, cluuid);
2661 if (obd->u.cli.cl_conn_count == 1) {
2662 exp = class_conn2export(exph);
2669 static int osc_disconnect(struct obd_export *exp, int flags)
2671 struct obd_device *obd = class_exp2obd(exp);
2672 struct llog_ctxt *ctxt = llog_get_context(obd, LLOG_SIZE_REPL_CTXT);
2675 if (obd->u.cli.cl_conn_count == 1) {
2676 /* flush any remaining cancel messages out to the target */
2677 llog_sync(ctxt, exp);
2679 /* balance the conn2export for oscc in osc_connect */
2680 class_export_put(exp);
2683 rc = client_disconnect_export(exp, flags);
2687 static int osc_lock_contains(struct obd_export *exp, struct lov_stripe_md *lsm,
2688 struct ldlm_lock *lock, obd_off offset)
2694 if (lock->l_policy_data.l_extent.start <= offset &&
2695 lock->l_policy_data.l_extent.end >= offset)
2700 static int osc_invalidate_import(struct obd_device *obd,
2701 struct obd_import *imp)
2703 struct client_obd *cli;
2704 LASSERT(imp->imp_obd == obd);
2705 /* this used to try and tear down queued pages, but it was
2706 * not correctly implemented. We'll have to do it again once
2707 * we call obd_invalidate_import() agian */
2708 /* XXX And we still need to do this */
2710 /* Reset grants, too */
2712 spin_lock(&cli->cl_loi_list_lock);
2713 cli->cl_ost_can_grant = cli->cl_dirty_granted = 0;
2714 spin_unlock(&cli->cl_loi_list_lock);
2719 int osc_setup(struct obd_device *obd, obd_count len, void *buf)
2723 rc = ptlrpcd_addref();
2727 rc = client_obd_setup(obd, len, buf);
2733 int osc_cleanup(struct obd_device *obd, int flags)
2737 rc = client_obd_cleanup(obd, flags);
2743 struct obd_ops osc_obd_ops = {
2744 o_owner: THIS_MODULE,
2745 o_attach: osc_attach,
2746 o_detach: osc_detach,
2748 o_cleanup: osc_cleanup,
2749 o_connect: osc_connect,
2750 o_disconnect: osc_disconnect,
2751 o_statfs: osc_statfs,
2752 o_packmd: osc_packmd,
2753 o_unpackmd: osc_unpackmd,
2754 o_create: osc_create,
2755 o_destroy: osc_destroy,
2756 o_getattr: osc_getattr,
2757 o_getattr_async:osc_getattr_async,
2758 o_setattr: osc_setattr,
2760 o_brw_async: osc_brw_async,
2761 .o_prep_async_page = osc_prep_async_page,
2762 .o_queue_async_io = osc_queue_async_io,
2763 .o_set_async_flags = osc_set_async_flags,
2764 .o_queue_sync_io = osc_queue_sync_io,
2765 .o_trigger_sync_io = osc_trigger_sync_io,
2766 .o_teardown_async_page = osc_teardown_async_page,
2769 o_enqueue: osc_enqueue,
2771 o_change_cbdata:osc_change_cbdata,
2772 o_cancel: osc_cancel,
2773 o_cancel_unused:osc_cancel_unused,
2774 o_iocontrol: osc_iocontrol,
2775 o_get_info: osc_get_info,
2776 o_set_info: osc_set_info,
2777 o_lock_contains:osc_lock_contains,
2778 o_invalidate_import: osc_invalidate_import,
2779 o_llog_init: osc_llog_init,
2780 o_llog_finish: osc_llog_finish,
2783 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
2784 struct obd_ops sanosc_obd_ops = {
2785 o_owner: THIS_MODULE,
2786 o_attach: osc_attach,
2787 o_detach: osc_detach,
2788 o_cleanup: client_obd_cleanup,
2789 o_connect: osc_connect,
2790 o_disconnect: client_disconnect_export,
2791 o_statfs: osc_statfs,
2792 o_packmd: osc_packmd,
2793 o_unpackmd: osc_unpackmd,
2794 o_create: osc_real_create,
2795 o_destroy: osc_destroy,
2796 o_getattr: osc_getattr,
2797 o_getattr_async:osc_getattr_async,
2798 o_setattr: osc_setattr,
2799 o_setup: client_sanobd_setup,
2803 o_enqueue: osc_enqueue,
2805 o_change_cbdata:osc_change_cbdata,
2806 o_cancel: osc_cancel,
2807 o_cancel_unused:osc_cancel_unused,
2808 o_iocontrol: osc_iocontrol,
2809 o_lock_contains:osc_lock_contains,
2810 o_invalidate_import: osc_invalidate_import,
2811 o_llog_init: osc_llog_init,
2812 o_llog_finish: osc_llog_finish,
2816 int __init osc_init(void)
2818 struct lprocfs_static_vars lvars, sanlvars;
2822 lprocfs_init_vars(osc, &lvars);
2823 lprocfs_init_vars(osc, &sanlvars);
2825 rc = class_register_type(&osc_obd_ops, lvars.module_vars,
2830 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
2831 rc = class_register_type(&sanosc_obd_ops, sanlvars.module_vars,
2832 LUSTRE_SANOSC_NAME);
2834 class_unregister_type(LUSTRE_OSC_NAME);
2840 static void /*__exit*/ osc_exit(void)
2842 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
2843 class_unregister_type(LUSTRE_SANOSC_NAME);
2845 class_unregister_type(LUSTRE_OSC_NAME);
2849 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
2850 MODULE_DESCRIPTION("Lustre Object Storage Client (OSC)");
2851 MODULE_LICENSE("GPL");
2853 module_init(osc_init);
2854 module_exit(osc_exit);