1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2001-2003 Cluster File Systems, Inc.
5 * Author Peter Braam <braam@clusterfs.com>
7 * This file is part of Lustre, http://www.lustre.org.
9 * Lustre is free software; you can redistribute it and/or
10 * modify it under the terms of version 2 of the GNU General Public
11 * License as published by the Free Software Foundation.
13 * Lustre is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with Lustre; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 * For testing and management it is treated as an obd_device,
23 * although * it does not export a full OBD method table (the
24 * requests are coming * in over the wire, so object target modules
25 * do not have a full * method table.)
30 # define EXPORT_SYMTAB
32 #define DEBUG_SUBSYSTEM S_OSC
35 # include <linux/version.h>
36 # include <linux/module.h>
37 # include <linux/mm.h>
38 # include <linux/highmem.h>
39 # include <linux/lustre_dlm.h>
40 # if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
41 # include <linux/workqueue.h>
42 # include <linux/smp_lock.h>
44 # include <linux/locks.h>
46 #else /* __KERNEL__ */
47 # include <liblustre.h>
50 #include <linux/kp30.h>
51 #include <linux/lustre_net.h>
52 #include <linux/lustre_user.h>
53 #include <linux/obd_ost.h>
54 #include <linux/obd_lov.h>
57 # include <linux/ctype.h>
58 # include <linux/init.h>
63 #include <linux/lustre_ha.h>
64 #include <linux/lprocfs_status.h>
65 #include <linux/lustre_log.h>
66 #include "osc_internal.h"
69 static int osc_attach(struct obd_device *dev, obd_count len, void *data)
71 struct lprocfs_static_vars lvars;
75 lprocfs_init_vars(osc,&lvars);
76 rc = lprocfs_obd_attach(dev, lvars.obd_vars);
80 rc = lproc_osc_attach_seqstat(dev);
82 lprocfs_obd_detach(dev);
86 ptlrpc_lprocfs_register_obd(dev);
90 static int osc_detach(struct obd_device *dev)
92 ptlrpc_lprocfs_unregister_obd(dev);
93 return lprocfs_obd_detach(dev);
97 /* Pack OSC object metadata for disk storage (LE byte order). */
98 static int osc_packmd(struct obd_export *exp, struct lov_mds_md **lmmp,
99 struct lov_stripe_md *lsm)
104 lmm_size = sizeof(**lmmp);
109 OBD_FREE(*lmmp, lmm_size);
115 OBD_ALLOC(*lmmp, lmm_size);
121 LASSERT(lsm->lsm_object_id);
122 (*lmmp)->lmm_object_id = cpu_to_le64(lsm->lsm_object_id);
128 /* Unpack OSC object metadata from disk storage (LE byte order). */
129 static int osc_unpackmd(struct obd_export *exp, struct lov_stripe_md **lsmp,
130 struct lov_mds_md *lmm, int lmm_bytes)
136 if (lmm_bytes < sizeof (*lmm)) {
137 CERROR("lov_mds_md too small: %d, need %d\n",
138 lmm_bytes, (int)sizeof(*lmm));
141 /* XXX LOV_MAGIC etc check? */
143 if (lmm->lmm_object_id == 0) {
144 CERROR("lov_mds_md: zero lmm_object_id\n");
149 lsm_size = lov_stripe_md_size(1);
153 if (*lsmp != NULL && lmm == NULL) {
154 OBD_FREE(*lsmp, lsm_size);
160 OBD_ALLOC(*lsmp, lsm_size);
163 loi_init((*lsmp)->lsm_oinfo);
167 /* XXX zero *lsmp? */
168 (*lsmp)->lsm_object_id = le64_to_cpu (lmm->lmm_object_id);
169 LASSERT((*lsmp)->lsm_object_id);
172 (*lsmp)->lsm_maxbytes = LUSTRE_STRIPE_MAXBYTES;
177 static int osc_getattr_interpret(struct ptlrpc_request *req,
178 struct osc_getattr_async_args *aa, int rc)
180 struct ost_body *body;
186 body = lustre_swab_repbuf(req, 0, sizeof(*body), lustre_swab_ost_body);
188 CDEBUG(D_INODE, "mode: %o\n", body->oa.o_mode);
189 memcpy(aa->aa_oa, &body->oa, sizeof(*aa->aa_oa));
191 /* This should really be sent by the OST */
192 aa->aa_oa->o_blksize = OSC_BRW_MAX_SIZE;
193 aa->aa_oa->o_valid |= OBD_MD_FLBLKSZ;
195 CERROR("can't unpack ost_body\n");
197 aa->aa_oa->o_valid = 0;
203 static int osc_getattr_async(struct obd_export *exp, struct obdo *oa,
204 struct lov_stripe_md *md,
205 struct ptlrpc_request_set *set)
207 struct ptlrpc_request *request;
208 struct ost_body *body;
209 int size = sizeof(*body);
210 struct osc_getattr_async_args *aa;
213 request = ptlrpc_prep_req(class_exp2cliimp(exp), OST_GETATTR, 1,
218 body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof (*body));
219 memcpy(&body->oa, oa, sizeof(*oa));
221 request->rq_replen = lustre_msg_size(1, &size);
222 request->rq_interpret_reply = osc_getattr_interpret;
224 LASSERT (sizeof (*aa) <= sizeof (request->rq_async_args));
225 aa = (struct osc_getattr_async_args *)&request->rq_async_args;
228 ptlrpc_set_add_req (set, request);
232 static int osc_getattr(struct obd_export *exp, struct obdo *oa,
233 struct lov_stripe_md *md)
235 struct ptlrpc_request *request;
236 struct ost_body *body;
237 int rc, size = sizeof(*body);
240 request = ptlrpc_prep_req(class_exp2cliimp(exp), OST_GETATTR, 1,
245 body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof (*body));
246 memcpy(&body->oa, oa, sizeof(*oa));
248 request->rq_replen = lustre_msg_size(1, &size);
250 rc = ptlrpc_queue_wait(request);
252 CERROR("%s failed: rc = %d\n", __FUNCTION__, rc);
256 body = lustre_swab_repbuf(request, 0, sizeof (*body),
257 lustre_swab_ost_body);
259 CERROR ("can't unpack ost_body\n");
260 GOTO (out, rc = -EPROTO);
263 CDEBUG(D_INODE, "mode: %o\n", body->oa.o_mode);
264 memcpy(oa, &body->oa, sizeof(*oa));
266 /* This should really be sent by the OST */
267 oa->o_blksize = OSC_BRW_MAX_SIZE;
268 oa->o_valid |= OBD_MD_FLBLKSZ;
272 ptlrpc_req_finished(request);
276 static int osc_setattr(struct obd_export *exp, struct obdo *oa,
277 struct lov_stripe_md *md, struct obd_trans_info *oti)
279 struct ptlrpc_request *request;
280 struct ost_body *body;
281 int rc, size = sizeof(*body);
284 request = ptlrpc_prep_req(class_exp2cliimp(exp), OST_SETATTR, 1, &size,
289 body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof(*body));
290 memcpy(&body->oa, oa, sizeof(*oa));
292 request->rq_replen = lustre_msg_size(1, &size);
294 rc = ptlrpc_queue_wait(request);
298 body = lustre_swab_repbuf(request, 0, sizeof(*body),
299 lustre_swab_ost_body);
301 GOTO(out, rc = -EPROTO);
303 memcpy(oa, &body->oa, sizeof(*oa));
307 ptlrpc_req_finished(request);
311 int osc_real_create(struct obd_export *exp, struct obdo *oa,
312 struct lov_stripe_md **ea, struct obd_trans_info *oti)
314 struct ptlrpc_request *request;
315 struct ost_body *body;
316 struct lov_stripe_md *lsm;
317 int rc, size = sizeof(*body);
325 rc = obd_alloc_memmd(exp, &lsm);
330 request = ptlrpc_prep_req(class_exp2cliimp(exp), OST_CREATE, 1, &size,
333 GOTO(out, rc = -ENOMEM);
335 body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof (*body));
336 memcpy(&body->oa, oa, sizeof(body->oa));
338 request->rq_replen = lustre_msg_size(1, &size);
339 if (oa->o_valid & OBD_MD_FLINLINE) {
340 LASSERT((oa->o_valid & OBD_MD_FLFLAGS) &&
341 oa->o_flags == OBD_FL_DELORPHAN);
342 DEBUG_REQ(D_HA, request,
343 "delorphan from OST integration; level == RECOVER");
344 request->rq_send_state = LUSTRE_IMP_RECOVER;
347 rc = ptlrpc_queue_wait(request);
351 body = lustre_swab_repbuf(request, 0, sizeof(*body),
352 lustre_swab_ost_body);
354 CERROR ("can't unpack ost_body\n");
355 GOTO (out_req, rc = -EPROTO);
358 memcpy(oa, &body->oa, sizeof(*oa));
360 /* This should really be sent by the OST */
361 oa->o_blksize = OSC_BRW_MAX_SIZE;
362 oa->o_valid |= OBD_MD_FLBLKSZ;
364 /* XXX LOV STACKING: the lsm that is passed to us from LOV does not
365 * have valid lsm_oinfo data structs, so don't go touching that.
366 * This needs to be fixed in a big way.
368 lsm->lsm_object_id = oa->o_id;
372 oti->oti_transno = request->rq_repmsg->transno;
374 if (oa->o_valid & OBD_MD_FLCOOKIE) {
375 if (!oti->oti_logcookies)
376 oti_alloc_cookies(oti, 1);
377 memcpy(oti->oti_logcookies, obdo_logcookie(oa),
378 sizeof(oti->oti_onecookie));
382 CDEBUG(D_HA, "transno: "LPD64"\n", request->rq_repmsg->transno);
385 ptlrpc_req_finished(request);
388 obd_free_memmd(exp, &lsm);
392 static int osc_punch(struct obd_export *exp, struct obdo *oa,
393 struct lov_stripe_md *md, obd_size start,
394 obd_size end, struct obd_trans_info *oti)
396 struct ptlrpc_request *request;
397 struct ost_body *body;
398 int rc, size = sizeof(*body);
406 request = ptlrpc_prep_req(class_exp2cliimp(exp), OST_PUNCH, 1, &size,
411 body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof (*body));
412 memcpy(&body->oa, oa, sizeof(*oa));
414 /* overload the size and blocks fields in the oa with start/end */
415 body->oa.o_size = start;
416 body->oa.o_blocks = end;
417 body->oa.o_valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS);
419 request->rq_replen = lustre_msg_size(1, &size);
421 rc = ptlrpc_queue_wait(request);
425 body = lustre_swab_repbuf (request, 0, sizeof (*body),
426 lustre_swab_ost_body);
428 CERROR ("can't unpack ost_body\n");
429 GOTO (out, rc = -EPROTO);
432 memcpy(oa, &body->oa, sizeof(*oa));
436 ptlrpc_req_finished(request);
440 static int osc_sync(struct obd_export *exp, struct obdo *oa,
441 struct lov_stripe_md *md, obd_size start, obd_size end)
443 struct ptlrpc_request *request;
444 struct ost_body *body;
445 int rc, size = sizeof(*body);
453 request = ptlrpc_prep_req(class_exp2cliimp(exp), OST_SYNC, 1, &size,
458 body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof (*body));
459 memcpy(&body->oa, oa, sizeof(*oa));
461 /* overload the size and blocks fields in the oa with start/end */
462 body->oa.o_size = start;
463 body->oa.o_blocks = end;
464 body->oa.o_valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS);
466 request->rq_replen = lustre_msg_size(1, &size);
468 rc = ptlrpc_queue_wait(request);
472 body = lustre_swab_repbuf(request, 0, sizeof(*body),
473 lustre_swab_ost_body);
475 CERROR ("can't unpack ost_body\n");
476 GOTO (out, rc = -EPROTO);
479 memcpy(oa, &body->oa, sizeof(*oa));
483 ptlrpc_req_finished(request);
487 static int osc_destroy(struct obd_export *exp, struct obdo *oa,
488 struct lov_stripe_md *ea, struct obd_trans_info *oti)
490 struct ptlrpc_request *request;
491 struct ost_body *body;
492 int rc, size = sizeof(*body);
500 request = ptlrpc_prep_req(class_exp2cliimp(exp), OST_DESTROY, 1,
505 body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof (*body));
507 if (oti != NULL && oa->o_valid & OBD_MD_FLCOOKIE) {
508 memcpy(obdo_logcookie(oa), oti->oti_logcookies,
509 sizeof(*oti->oti_logcookies));
510 oti->oti_logcookies++;
513 memcpy(&body->oa, oa, sizeof(*oa));
514 request->rq_replen = lustre_msg_size(1, &size);
516 rc = ptlrpc_queue_wait(request);
520 body = lustre_swab_repbuf(request, 0, sizeof(*body),
521 lustre_swab_ost_body);
523 CERROR ("Can't unpack body\n");
524 GOTO (out, rc = -EPROTO);
527 memcpy(oa, &body->oa, sizeof(*oa));
531 ptlrpc_req_finished(request);
535 static void osc_announce_cached(struct client_obd *cli, struct ost_body *body)
537 obd_flag bits = OBD_MD_FLBLOCKS|OBD_MD_FLRDEV;
539 LASSERT(!(body->oa.o_valid & bits));
541 body->oa.o_valid |= bits;
542 down(&cli->cl_dirty_sem);
543 body->oa.o_blocks = cli->cl_dirty;
544 body->oa.o_rdev = cli->cl_dirty_granted;
545 up(&cli->cl_dirty_sem);
546 CDEBUG(D_INODE, "announcing "LPU64" dirty "LPU64" granted\n",
547 cli->cl_dirty, cli->cl_dirty_granted);
550 static void osc_update_grant(struct client_obd *cli, struct ost_body *body)
552 if(!(body->oa.o_valid & OBD_MD_FLRDEV)) {
553 if (cli->cl_ost_can_grant) {
554 CDEBUG(D_INODE, "%s can't grant\n",
555 cli->cl_import->imp_target_uuid.uuid);
557 cli->cl_ost_can_grant = 0;
561 CDEBUG(D_ERROR, "got "LPU64" grant\n", body->oa.o_rdev);
562 down(&cli->cl_dirty_sem);
563 cli->cl_dirty_granted = body->oa.o_rdev;
564 /* XXX check for over-run and wake up the io thread that
565 * doesn't exist yet */
566 up(&cli->cl_dirty_sem);
569 /* We assume that the reason this OSC got a short read is because it read
570 * beyond the end of a stripe file; i.e. lustre is reading a sparse file
571 * via the LOV, and it _knows_ it's reading inside the file, it's just that
572 * this stripe never got written at or beyond this stripe offset yet. */
573 static void handle_short_read(int nob_read, obd_count page_count,
574 struct brw_page *pga)
578 /* skip bytes read OK */
579 while (nob_read > 0) {
580 LASSERT (page_count > 0);
582 if (pga->count > nob_read) {
583 /* EOF inside this page */
584 ptr = kmap(pga->pg) + (pga->off & ~PAGE_MASK);
585 memset(ptr + nob_read, 0, pga->count - nob_read);
592 nob_read -= pga->count;
597 /* zero remaining pages */
598 while (page_count-- > 0) {
599 ptr = kmap(pga->pg) + (pga->off & ~PAGE_MASK);
600 memset(ptr, 0, pga->count);
606 static int check_write_rcs(struct ptlrpc_request *request, int niocount,
607 obd_count page_count, struct brw_page *pga)
612 /* return error if any niobuf was in error */
613 remote_rcs = lustre_swab_repbuf(request, 1,
614 sizeof(*remote_rcs) * niocount, NULL);
615 if (remote_rcs == NULL) {
616 CERROR ("Missing/short RC vector on BRW_WRITE reply\n");
619 if (lustre_msg_swabbed (request->rq_repmsg))
620 for (i = 0; i < niocount; i++)
621 __swab32s (&remote_rcs[i]);
623 for (i = 0; i < niocount; i++) {
624 if (remote_rcs[i] < 0)
625 return (remote_rcs[i]);
627 if (remote_rcs[i] != 0) {
628 CERROR ("rc[%d] invalid (%d) req %p\n",
629 i, remote_rcs[i], request);
637 static inline int can_merge_pages(struct brw_page *p1, struct brw_page *p2)
639 if (p1->flag != p2->flag) {
640 unsigned mask = ~(OBD_BRW_CREATE|OBD_BRW_FROM_GRANT);
642 /* warn if we try to combine flags that we don't know to be
644 if ((p1->flag & mask) != (p2->flag & mask))
645 CERROR("is it ok to have flags 0x%x and 0x%x in the "
646 "same brw?\n", p1->flag, p2->flag);
650 return (p1->off + p1->count == p2->off);
654 static obd_count cksum_pages(int nob, obd_count page_count,
655 struct brw_page *pga)
661 LASSERT (page_count > 0);
664 ost_checksum(&cksum, ptr + (pga->off & (PAGE_SIZE - 1)),
665 pga->count > nob ? nob : pga->count);
677 static int osc_brw_prep_request(int cmd, struct obd_import *imp,struct obdo *oa,
678 struct lov_stripe_md *lsm, obd_count page_count,
679 struct brw_page *pga, int *requested_nobp,
680 int *niocountp, struct ptlrpc_request **reqp)
682 struct ptlrpc_request *req;
683 struct ptlrpc_bulk_desc *desc;
684 struct client_obd *cli = &imp->imp_obd->u.cli;
685 struct ost_body *body;
686 struct obd_ioobj *ioobj;
687 struct niobuf_remote *niobuf;
696 opc = ((cmd & OBD_BRW_WRITE) != 0) ? OST_WRITE : OST_READ;
698 for (niocount = i = 1; i < page_count; i++)
699 if (!can_merge_pages (&pga[i - 1], &pga[i]))
702 size[0] = sizeof(*body);
703 size[1] = sizeof(*ioobj);
704 size[2] = niocount * sizeof(*niobuf);
706 req = ptlrpc_prep_req(imp, opc, 3, size, NULL);
710 if (opc == OST_WRITE)
711 desc = ptlrpc_prep_bulk_imp(req, BULK_GET_SOURCE,
714 desc = ptlrpc_prep_bulk_imp(req, BULK_PUT_SINK,
717 GOTO(out, rc = -ENOMEM);
718 /* NB request now owns desc and will free it when it gets freed */
720 body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof(*body));
721 ioobj = lustre_msg_buf(req->rq_reqmsg, 1, sizeof(*ioobj));
722 niobuf = lustre_msg_buf(req->rq_reqmsg, 2, niocount * sizeof(*niobuf));
724 memcpy(&body->oa, oa, sizeof(*oa));
726 obdo_to_ioobj(oa, ioobj);
727 ioobj->ioo_bufcnt = niocount;
729 LASSERT (page_count > 0);
730 for (requested_nob = i = 0; i < page_count; i++, niobuf++) {
731 struct brw_page *pg = &pga[i];
732 struct brw_page *pg_prev = pg - 1;
734 LASSERT(pg->count > 0);
735 LASSERT((pg->off & ~PAGE_MASK) + pg->count <= PAGE_SIZE);
736 LASSERTF(i == 0 || pg->off > pg_prev->off,
737 "i %d p_c %u pg %p [pri %lu ind %lu] off "LPU64
738 " prev_pg %p [pri %lu ind %lu] off "LPU64,
740 pg->pg, pg->pg->private, pg->pg->index, pg->off,
741 pg_prev->pg, pg_prev->pg->private, pg_prev->pg->index,
744 rc = ptlrpc_prep_bulk_page(desc, pg->pg, pg->off & ~PAGE_MASK,
749 requested_nob += pg->count;
751 if (i > 0 && can_merge_pages(pg_prev, pg)) {
753 niobuf->len += pg->count;
755 niobuf->offset = pg->off;
756 niobuf->len = pg->count;
757 niobuf->flags = pg->flag;
761 LASSERT((void *)(niobuf - niocount) ==
762 lustre_msg_buf(req->rq_reqmsg, 2, niocount * sizeof(*niobuf)));
763 osc_announce_cached(cli, body);
764 spin_lock_irqsave(&req->rq_lock, flags);
765 req->rq_no_resend = 1;
766 spin_unlock_irqrestore(&req->rq_lock, flags);
768 /* size[0] still sizeof (*body) */
769 if (opc == OST_WRITE) {
771 body->oa.o_valid |= OBD_MD_FLCKSUM;
772 body->oa.o_nlink = cksum_pages(requested_nob, page_count, pga);
774 /* 1 RC per niobuf */
775 size[1] = sizeof(__u32) * niocount;
776 req->rq_replen = lustre_msg_size(2, size);
778 /* 1 RC for the whole I/O */
779 req->rq_replen = lustre_msg_size(1, size);
782 *niocountp = niocount;
783 *requested_nobp = requested_nob;
788 ptlrpc_req_finished (req);
792 static int osc_brw_fini_request(struct ptlrpc_request *req, struct obdo *oa,
793 int requested_nob, int niocount,
794 obd_count page_count, struct brw_page *pga,
797 struct client_obd *cli = &req->rq_import->imp_obd->u.cli;
798 struct ost_body *body;
803 body = lustre_swab_repbuf(req, 0, sizeof(*body), lustre_swab_ost_body);
805 CERROR ("Can't unpack body\n");
809 osc_update_grant(cli, body);
811 if (req->rq_reqmsg->opc == OST_WRITE) {
813 CERROR ("Unexpected +ve rc %d\n", rc);
817 return(check_write_rcs(req, niocount, page_count, pga));
820 if (rc > requested_nob) {
821 CERROR("Unexpected rc %d (%d requested)\n", rc, requested_nob);
825 if (rc < requested_nob)
826 handle_short_read(rc, page_count, pga);
828 memcpy(oa, &body->oa, sizeof(*oa));
831 if (oa->o_valid & OBD_MD_FLCKSUM) {
832 const struct ptlrpc_peer *peer =
833 &req->rq_import->imp_connection->c_peer;
834 static int cksum_counter;
835 obd_count server_cksum = oa->o_nlink;
836 obd_count cksum = cksum_pages(rc, page_count, pga);
837 char str[PTL_NALFMT_SIZE];
839 portals_nid2str(peer->peer_ni->pni_number, peer->peer_nid, str);
842 if (server_cksum != cksum) {
843 CERROR("Bad checksum: server %x, client %x, server NID "
844 LPX64" (%s)\n", server_cksum, cksum,
845 peer->peer_nid, str);
848 } else if ((cksum_counter & (-cksum_counter)) == cksum_counter){
849 CWARN("Checksum %u from "LPX64" (%s) OK: %x\n",
850 cksum_counter, peer->peer_nid, str, cksum);
853 static int cksum_missed;
856 if ((cksum_missed & (-cksum_missed)) == cksum_missed)
857 CERROR("Request checksum %u from "LPX64", no reply\n",
859 req->rq_import->imp_connection->c_peer.peer_nid);
865 static int osc_brw_internal(int cmd, struct obd_export *exp,struct obdo *oa,
866 struct lov_stripe_md *lsm,
867 obd_count page_count, struct brw_page *pga)
871 struct ptlrpc_request *request;
876 rc = osc_brw_prep_request(cmd, class_exp2cliimp(exp), oa, lsm,
877 page_count, pga, &requested_nob, &niocount,
879 /* NB ^ sets rq_no_resend */
884 rc = ptlrpc_queue_wait(request);
886 if (rc == -ETIMEDOUT && request->rq_resend) {
887 DEBUG_REQ(D_HA, request, "BULK TIMEOUT");
888 ptlrpc_req_finished(request);
892 rc = osc_brw_fini_request(request, oa, requested_nob, niocount,
893 page_count, pga, rc);
895 ptlrpc_req_finished(request);
899 static int brw_interpret(struct ptlrpc_request *request,
900 struct osc_brw_async_args *aa, int rc)
902 struct obdo *oa = aa->aa_oa;
903 int requested_nob = aa->aa_requested_nob;
904 int niocount = aa->aa_nio_count;
905 obd_count page_count = aa->aa_page_count;
906 struct brw_page *pga = aa->aa_pga;
909 /* XXX bug 937 here */
910 if (rc == -ETIMEDOUT && request->rq_resend) {
911 DEBUG_REQ(D_HA, request, "BULK TIMEOUT");
912 LBUG(); /* re-send. later. */
916 rc = osc_brw_fini_request(request, oa, requested_nob, niocount,
917 page_count, pga, rc);
921 static int async_internal(int cmd, struct obd_export *exp, struct obdo *oa,
922 struct lov_stripe_md *lsm, obd_count page_count,
923 struct brw_page *pga, struct ptlrpc_request_set *set)
925 struct ptlrpc_request *request;
928 struct osc_brw_async_args *aa;
932 rc = osc_brw_prep_request(cmd, class_exp2cliimp(exp), oa, lsm,
933 page_count, pga, &requested_nob, &nio_count,
935 /* NB ^ sets rq_no_resend */
938 LASSERT(sizeof(*aa) <= sizeof(request->rq_async_args));
939 aa = (struct osc_brw_async_args *)&request->rq_async_args;
941 aa->aa_requested_nob = requested_nob;
942 aa->aa_nio_count = nio_count;
943 aa->aa_page_count = page_count;
946 request->rq_interpret_reply = brw_interpret;
947 ptlrpc_set_add_req(set, request);
953 #define min_t(type,x,y) \
954 ({ type __x = (x); type __y = (y); __x < __y ? __x: __y; })
958 * ugh, we want disk allocation on the target to happen in offset order. we'll
959 * follow sedgewicks advice and stick to the dead simple shellsort -- it'll do
960 * fine for our small page arrays and doesn't require allocation. its an
961 * insertion sort that swaps elements that are strides apart, shrinking the
962 * stride down until its '1' and the array is sorted.
964 static void sort_brw_pages(struct brw_page *array, int num)
971 for (stride = 1; stride < num ; stride = (stride * 3) + 1)
976 for (i = stride ; i < num ; i++) {
979 while (j >= stride && array[j - stride].off > tmp.off) {
980 array[j] = array[j - stride];
985 } while (stride > 1);
988 /* make sure we the regions we're passing to elan don't violate its '4
989 * fragments' constraint. portal headers are a fragment, all full
990 * PAGE_SIZE long pages count as 1 fragment, and each partial page
991 * counts as a fragment. I think. see bug 934. */
992 static obd_count check_elan_limit(struct brw_page *pg, obd_count pages)
995 int saw_whole_frag = 0;
998 for (i = 0 ; frags_left && i < pages ; pg++, i++) {
999 if (pg->count == PAGE_SIZE) {
1000 if (!saw_whole_frag) {
1011 static int osc_brw(int cmd, struct obd_export *exp, struct obdo *oa,
1012 struct lov_stripe_md *md, obd_count page_count,
1013 struct brw_page *pga, struct obd_trans_info *oti)
1017 if (cmd == OBD_BRW_CHECK) {
1018 /* The caller just wants to know if there's a chance that this
1019 * I/O can succeed */
1020 struct obd_import *imp = class_exp2cliimp(exp);
1022 if (imp == NULL || imp->imp_invalid)
1027 while (page_count) {
1028 obd_count pages_per_brw;
1031 if (page_count > OSC_BRW_MAX_IOV)
1032 pages_per_brw = OSC_BRW_MAX_IOV;
1034 pages_per_brw = page_count;
1036 sort_brw_pages(pga, pages_per_brw);
1037 pages_per_brw = check_elan_limit(pga, pages_per_brw);
1039 rc = osc_brw_internal(cmd, exp, oa, md, pages_per_brw, pga);
1044 page_count -= pages_per_brw;
1045 pga += pages_per_brw;
1050 static int osc_brw_async(int cmd, struct obd_export *exp, struct obdo *oa,
1051 struct lov_stripe_md *md, obd_count page_count,
1052 struct brw_page *pga, struct ptlrpc_request_set *set,
1053 struct obd_trans_info *oti)
1057 if (cmd == OBD_BRW_CHECK) {
1058 /* The caller just wants to know if there's a chance that this
1059 * I/O can succeed */
1060 struct obd_import *imp = class_exp2cliimp(exp);
1062 if (imp == NULL || imp->imp_invalid)
1067 while (page_count) {
1068 obd_count pages_per_brw;
1071 if (page_count > OSC_BRW_MAX_IOV)
1072 pages_per_brw = OSC_BRW_MAX_IOV;
1074 pages_per_brw = page_count;
1076 sort_brw_pages(pga, pages_per_brw);
1077 pages_per_brw = check_elan_limit(pga, pages_per_brw);
1079 rc = async_internal(cmd, exp, oa, md, pages_per_brw, pga, set);
1084 page_count -= pages_per_brw;
1085 pga += pages_per_brw;
1090 static void osc_check_rpcs(struct client_obd *cli);
1091 static void osc_exit_cache(struct client_obd *cli, struct osc_async_page *oap);
1093 static void osc_complete_oap(struct client_obd *cli,
1094 struct osc_async_page *oap, int rc)
1097 osc_exit_cache(cli, oap);
1098 oap->oap_async_flags = 0;
1099 if (oap->oap_osic) {
1100 osic_complete_one(oap->oap_osic, rc);
1101 oap->oap_osic = NULL;
1106 oap->oap_caller_ops->ap_completion(oap->oap_caller_data, oap->oap_cmd,
1111 static int brw_interpret_oap(struct ptlrpc_request *request,
1112 struct osc_brw_async_args *aa, int rc)
1114 struct osc_async_page *oap;
1115 struct client_obd *cli;
1116 struct list_head *pos, *n;
1119 CDEBUG(D_INODE, "request %p aa %p\n", request, aa);
1121 rc = osc_brw_fini_request(request, aa->aa_oa, aa->aa_requested_nob,
1122 aa->aa_nio_count, aa->aa_page_count,
1126 /* in failout recovery we ignore writeback failure and want
1127 * to just tell llite to unlock the page and continue */
1128 if (cli->cl_import == NULL || cli->cl_import->imp_invalid)
1131 spin_lock(&cli->cl_loi_list_lock);
1133 /* the caller may re-use the oap after the completion call so
1134 * we need to clean it up a little */
1135 list_for_each_safe(pos, n, &aa->aa_oaps) {
1136 oap = list_entry(pos, struct osc_async_page, oap_rpc_item);
1138 //CDEBUG(D_INODE, "page %p index %lu oap %p\n",
1139 //oap->oap_page, oap->oap_page->index, oap);
1141 list_del_init(&oap->oap_rpc_item);
1142 osc_complete_oap(cli, oap, rc);
1145 cli->cl_brw_in_flight--;
1146 osc_check_rpcs(cli);
1148 spin_unlock(&cli->cl_loi_list_lock);
1150 obdo_free(aa->aa_oa);
1151 OBD_FREE(aa->aa_pga, aa->aa_page_count * sizeof(struct brw_page));
1156 static struct ptlrpc_request *osc_build_req(struct client_obd *cli,
1157 struct list_head *rpc_list,
1158 int page_count, int cmd)
1160 struct ptlrpc_request *req;
1161 struct brw_page *pga = NULL;
1162 int requested_nob, nio_count;
1163 struct osc_brw_async_args *aa;
1164 struct obdo *oa = NULL;
1165 struct obd_async_page_ops *ops = NULL;
1166 void *caller_data = NULL;
1167 struct list_head *pos;
1170 LASSERT(!list_empty(rpc_list));
1172 OBD_ALLOC(pga, sizeof(*pga) * page_count);
1174 RETURN(ERR_PTR(-ENOMEM));
1178 GOTO(out, req = ERR_PTR(-ENOMEM));
1181 list_for_each(pos, rpc_list) {
1182 struct osc_async_page *oap;
1184 oap = list_entry(pos, struct osc_async_page, oap_rpc_item);
1186 ops = oap->oap_caller_ops;
1187 caller_data = oap->oap_caller_data;
1189 pga[i].off = oap->oap_obj_off + oap->oap_page_off;
1190 pga[i].pg = oap->oap_page;
1191 pga[i].count = oap->oap_count;
1192 pga[i].flag = oap->oap_brw_flags;
1193 //CDEBUG(D_INODE, "putting page %p index %lu oap %p into pga\n",
1194 //pga[i].pg, oap->oap_page->index, oap);
1198 /* always get the data for the obdo for the rpc */
1199 LASSERT(ops != NULL);
1200 ops->ap_fill_obdo(caller_data, cmd, oa);
1202 sort_brw_pages(pga, page_count);
1203 rc = osc_brw_prep_request(cmd, cli->cl_import, oa, NULL, page_count,
1204 pga, &requested_nob, &nio_count, &req);
1206 CERROR("prep_req failed: %d\n", rc);
1207 GOTO(out, req = ERR_PTR(rc));
1210 LASSERT(sizeof(*aa) <= sizeof(req->rq_async_args));
1211 aa = (struct osc_brw_async_args *)&req->rq_async_args;
1213 aa->aa_requested_nob = requested_nob;
1214 aa->aa_nio_count = nio_count;
1215 aa->aa_page_count = page_count;
1224 OBD_FREE(pga, sizeof(*pga) * page_count);
1229 static void lop_update_pending(struct client_obd *cli,
1230 struct loi_oap_pages *lop, int cmd, int delta)
1232 lop->lop_num_pending += delta;
1233 if (cmd == OBD_BRW_WRITE)
1234 cli->cl_pending_w_pages += delta;
1236 cli->cl_pending_r_pages += delta;
1239 /* the loi lock is held across this function but it's allowed to release
1240 * and reacquire it during its work */
1241 static int osc_send_oap_rpc(struct client_obd *cli, int cmd,
1242 struct loi_oap_pages *lop)
1244 struct ptlrpc_request *request;
1245 obd_count page_count = 0;
1246 struct list_head *tmp, *pos;
1247 struct osc_async_page *oap = NULL;
1248 struct osc_brw_async_args *aa;
1249 struct obd_async_page_ops *ops;
1250 LIST_HEAD(rpc_list);
1253 /* first we find the pages we're allowed to work with */
1254 list_for_each_safe(pos, tmp, &lop->lop_pending) {
1255 oap = list_entry(pos, struct osc_async_page, oap_pending_item);
1256 ops = oap->oap_caller_ops;
1258 /* in llite being 'ready' equates to the page being locked
1259 * until completion unlocks it. commit_write submits a page
1260 * as not ready because its unlock will happen unconditionally
1261 * as the call returns. if we race with commit_write giving
1262 * us that page we dont' want to create a hole in the page
1263 * stream, so we stop and leave the rpc to be fired by
1264 * another dirtier or kupdated interval (the not ready page
1265 * will still be on the dirty list). we could call in
1266 * at the end of ll_file_write to process the queue again. */
1267 if (!(oap->oap_async_flags & ASYNC_READY)) {
1268 int rc = ops->ap_make_ready(oap->oap_caller_data, cmd);
1270 CDEBUG(D_INODE, "oap %p page %p returned %d "
1271 "instead of ready\n", oap,
1275 /* llite is telling us that the page is still
1276 * in commit_write and that we should try
1277 * and put it in an rpc again later. we
1278 * break out of the loop so we don't create
1279 * a whole in the sequence of pages in
1284 /* the io isn't needed.. tell the checks
1285 * below to complete the rpc with EINTR */
1286 oap->oap_async_flags |= ASYNC_COUNT_STABLE;
1287 oap->oap_count = -EINTR;
1290 oap->oap_async_flags |= ASYNC_READY;
1293 LASSERTF(0, "oap %p page %p returned %d "
1294 "from make_ready\n", oap,
1302 /* take the page out of our book-keeping */
1303 list_del_init(&oap->oap_pending_item);
1304 lop_update_pending(cli, lop, cmd, -1);
1305 if (!list_empty(&oap->oap_urgent_item))
1306 list_del_init(&oap->oap_urgent_item);
1308 /* ask the caller for the size of the io as the rpc leaves. */
1309 if (!(oap->oap_async_flags & ASYNC_COUNT_STABLE))
1310 oap->oap_count = ops->ap_refresh_count(
1311 oap->oap_caller_data,
1313 if (oap->oap_count <= 0) {
1314 CDEBUG(D_INODE, "oap %p count %d, completing\n", oap,
1316 osc_complete_oap(cli, oap, oap->oap_count);
1320 /* now put the page back in our accounting */
1321 list_add_tail(&oap->oap_rpc_item, &rpc_list);
1322 if (++page_count >= cli->cl_max_pages_per_rpc)
1326 if (page_count == 0)
1329 spin_unlock(&cli->cl_loi_list_lock);
1331 request = osc_build_req(cli, &rpc_list, page_count, cmd);
1332 if (IS_ERR(request)) {
1333 /* this should happen rarely and is pretty bad, it makes the
1334 * pending list not follow the dirty order */
1335 spin_lock(&cli->cl_loi_list_lock);
1336 list_for_each_safe(pos, tmp, &rpc_list) {
1337 oap = list_entry(pos, struct osc_async_page,
1339 list_del_init(&oap->oap_rpc_item);
1340 list_add_tail(&oap->oap_pending_item,
1342 lop_update_pending(cli, lop, cmd, 1);
1343 if (oap->oap_async_flags & ASYNC_URGENT)
1344 list_add(&oap->oap_urgent_item,
1347 RETURN(PTR_ERR(request));
1350 LASSERT(sizeof(*aa) <= sizeof(request->rq_async_args));
1351 aa = (struct osc_brw_async_args *)&request->rq_async_args;
1352 INIT_LIST_HEAD(&aa->aa_oaps);
1353 list_splice(&rpc_list, &aa->aa_oaps);
1354 INIT_LIST_HEAD(&rpc_list);
1356 if (cmd == OBD_BRW_READ)
1357 lprocfs_oh_tally_log2(&cli->cl_read_page_hist, page_count);
1359 lprocfs_oh_tally_log2(&cli->cl_write_page_hist, page_count);
1361 spin_lock(&cli->cl_loi_list_lock);
1362 if (cmd == OBD_BRW_READ)
1363 lprocfs_oh_tally(&cli->cl_read_rpc_hist, cli->cl_brw_in_flight);
1365 lprocfs_oh_tally(&cli->cl_write_rpc_hist,
1366 cli->cl_brw_in_flight);
1368 cli->cl_brw_in_flight++;
1369 CDEBUG(D_INODE, "req %p: %d pages, aa %p. now %d in flight\n", request,
1370 page_count, aa, cli->cl_brw_in_flight);
1372 request->rq_interpret_reply = brw_interpret_oap;
1373 ptlrpcd_add_req(request);
1377 static int lop_makes_rpc(struct client_obd *cli, struct loi_oap_pages *lop,
1383 /* stream rpcs until we complete the urgent pages in the object */
1384 if (!list_empty(&lop->lop_urgent))
1387 /* fire off rpcs when we have 'optimal' rpcs as tuned for the wire. */
1388 optimal = cli->cl_max_pages_per_rpc;
1389 /* *2 to avoid triggering rpcs that would want to include pages that
1390 * are being queued but which can't be made ready until the queuer
1391 * finishes with the page. this is a wart for llite::commit_write() */
1392 if (cmd == OBD_BRW_WRITE)
1394 if (lop->lop_num_pending >= optimal)
1397 /* trigger a write rpc stream as long as there are dirtiers waiting
1398 * for space. as they're waiting, they're not going to create more
1399 * pages to coallesce with what's waiting.. */
1400 if (!list_empty(&cli->cl_cache_waiters))
1406 static int loi_makes_rpc(struct client_obd *cli, struct lov_oinfo *loi)
1408 return lop_makes_rpc(cli, &loi->loi_write_lop, OBD_BRW_WRITE) ||
1409 lop_makes_rpc(cli, &loi->loi_read_lop, OBD_BRW_READ);
1412 static void loi_onto_ready_list(struct client_obd *cli, struct lov_oinfo *loi)
1414 if (list_empty(&loi->loi_cli_item) && loi_makes_rpc(cli, loi))
1415 list_add_tail(&loi->loi_cli_item, &cli->cl_loi_ready_list);
1418 #define LOI_DEBUG(LOI, STR, args...) \
1419 CDEBUG(D_INODE, "loi rdy %d [%p,%p] wr %d:%d rd %d:%d " STR, \
1420 !list_empty(&(LOI)->loi_cli_item), \
1421 (LOI)->loi_cli_item.next, \
1422 (LOI)->loi_cli_item.prev, \
1423 (LOI)->loi_write_lop.lop_num_pending, \
1424 !list_empty(&(LOI)->loi_write_lop.lop_urgent), \
1425 (LOI)->loi_read_lop.lop_num_pending, \
1426 !list_empty(&(LOI)->loi_read_lop.lop_urgent), \
1429 /* called with the loi list lock held */
1430 static void osc_check_rpcs(struct client_obd *cli)
1432 struct lov_oinfo *loi;
1433 int rc = 0, making_progress;
1436 if (list_empty(&cli->cl_loi_ready_list)) {
1437 CDEBUG(D_INODE, "no lois ready\n");
1442 while (!list_empty(&cli->cl_loi_ready_list)) {
1443 loi = list_entry(cli->cl_loi_ready_list.next, struct lov_oinfo,
1446 if (cli->cl_brw_in_flight >= cli->cl_max_rpcs_in_flight)
1449 making_progress = 0;
1451 /* hmm, it occurs to me that having rpc preparation fail
1452 * with num_pending == num_urgent means that there won't
1453 * be any more calls into here unless other traffic comes
1456 /* attempt some read/write balancing by alternating between
1457 * reads and writes in an object */
1458 if (lop_makes_rpc(cli, &loi->loi_write_lop, OBD_BRW_WRITE)) {
1459 rc = osc_send_oap_rpc(cli, OBD_BRW_WRITE,
1460 &loi->loi_write_lop);
1466 if (lop_makes_rpc(cli, &loi->loi_read_lop, OBD_BRW_READ)) {
1467 rc = osc_send_oap_rpc(cli, OBD_BRW_READ,
1468 &loi->loi_read_lop);
1475 /* attempt some inter-object balancing by issueing rpcs
1476 * for each object in turn */
1477 if (!list_empty(&loi->loi_cli_item))
1478 list_del_init(&loi->loi_cli_item);
1480 loi_onto_ready_list(cli, loi);
1482 LOI_DEBUG(loi, "mp %d\n", making_progress);
1484 /* could be smarter, !making_progress can happen in theory
1485 * if all the pages can not be locked in set_io_ready */
1486 if (!making_progress)
1492 /* we're trying to queue a page in the osc so we're subject to the
1493 * 'cl_dirty_max' limit on the number of pages that can be queued in the osc.
1494 * If the osc's queued pages are already at that limit, then we want to sleep
1495 * until there is space in the osc's queue for us. we need this goofy
1496 * little struct to really tell that our allocation was fulfilled in
1497 * the presence of pending signals */
1498 struct osc_cache_waiter {
1499 struct list_head ocw_entry;
1500 wait_queue_head_t ocw_waitq;
1502 static int ocw_granted(struct client_obd *cli, struct osc_cache_waiter *ocw)
1506 spin_lock(&cli->cl_loi_list_lock);
1507 rc = list_empty(&ocw->ocw_entry);
1508 spin_unlock(&cli->cl_loi_list_lock);
1511 static int osc_enter_cache(struct client_obd *cli, struct lov_oinfo *loi,
1512 struct osc_async_page *oap)
1514 struct osc_cache_waiter ocw;
1515 struct l_wait_info lwi = {0};
1519 /* XXX check for ost grants here as well.. for now we ignore them. */
1520 if (cli->cl_dirty_max < PAGE_SIZE)
1523 /* if we fail this test then cl_dirty contains at least one page
1524 * that will have to be completed after we release the lock */
1525 if (cli->cl_dirty + PAGE_SIZE <= cli->cl_dirty_max) {
1526 /* account for ourselves */
1527 cli->cl_dirty += PAGE_SIZE;
1531 init_waitqueue_head(&ocw.ocw_waitq);
1532 list_add_tail(&ocw.ocw_entry, &cli->cl_cache_waiters);
1534 /* make sure that there are write rpcs in flight to wait for. this
1535 * is a little silly as this object may not have any pending
1536 * but other objects sure might. this should probably be cleaned. */
1537 loi_onto_ready_list(cli, loi);
1538 osc_check_rpcs(cli);
1539 spin_unlock(&cli->cl_loi_list_lock);
1541 CDEBUG(D_INODE, "sleeping for cache space\n");
1542 l_wait_event(ocw.ocw_waitq, ocw_granted(cli, &ocw), &lwi);
1544 spin_lock(&cli->cl_loi_list_lock);
1545 if (!list_empty(&ocw.ocw_entry)) {
1547 list_del(&ocw.ocw_entry);
1552 oap->oap_brw_flags |= OBD_BRW_FROM_GRANT;
1556 /* the companion to enter_cache, called when an oap is now longer part of the
1557 * dirty accounting.. so writeback completes or truncate happens before writing
1558 * starts. must be called with the loi lock held. */
1559 static void osc_exit_cache(struct client_obd *cli, struct osc_async_page *oap)
1561 struct osc_cache_waiter *ocw;
1564 if (!(oap->oap_brw_flags & OBD_BRW_FROM_GRANT)) {
1569 if (list_empty(&cli->cl_cache_waiters)) {
1570 cli->cl_dirty -= PAGE_SIZE;
1572 ocw = list_entry(cli->cl_cache_waiters.next,
1573 struct osc_cache_waiter, ocw_entry);
1574 list_del_init(&ocw->ocw_entry);
1575 wake_up(&ocw->ocw_waitq);
1578 oap->oap_brw_flags &= ~OBD_BRW_FROM_GRANT;
1582 int osc_prep_async_page(struct obd_export *exp, struct lov_stripe_md *lsm,
1583 struct lov_oinfo *loi, struct page *page,
1584 obd_off offset, struct obd_async_page_ops *ops,
1585 void *data, void **res)
1587 struct osc_async_page *oap;
1590 OBD_ALLOC(oap, sizeof(*oap));
1594 oap->oap_magic = OAP_MAGIC;
1595 oap->oap_caller_ops = ops;
1596 oap->oap_caller_data = data;
1598 oap->oap_page = page;
1599 oap->oap_obj_off = offset;
1601 INIT_LIST_HEAD(&oap->oap_pending_item);
1602 INIT_LIST_HEAD(&oap->oap_urgent_item);
1603 INIT_LIST_HEAD(&oap->oap_rpc_item);
1605 CDEBUG(D_CACHE, "oap %p page %p obj off "LPU64"\n", oap, page, offset);
1610 struct osc_async_page *oap_from_cookie(void *cookie)
1612 struct osc_async_page *oap = cookie;
1613 if (oap->oap_magic != OAP_MAGIC)
1614 return ERR_PTR(-EINVAL);
1618 static int osc_queue_async_io(struct obd_export *exp, struct lov_stripe_md *lsm,
1619 struct lov_oinfo *loi, void *cookie,
1620 int cmd, obd_off off, int count,
1621 obd_flag brw_flags, enum async_flags async_flags)
1623 struct client_obd *cli = &exp->exp_obd->u.cli;
1624 struct osc_async_page *oap;
1625 struct loi_oap_pages *lop;
1629 oap = oap_from_cookie(cookie);
1631 RETURN(PTR_ERR(oap));
1633 if (cli->cl_import == NULL || cli->cl_import->imp_invalid)
1636 if (!list_empty(&oap->oap_pending_item) ||
1637 !list_empty(&oap->oap_urgent_item) ||
1638 !list_empty(&oap->oap_rpc_item))
1642 loi = &lsm->lsm_oinfo[0];
1644 spin_lock(&cli->cl_loi_list_lock);
1647 oap->oap_async_flags = async_flags;
1648 oap->oap_page_off = off;
1649 oap->oap_count = count;
1650 oap->oap_brw_flags = brw_flags;
1652 if (cmd == OBD_BRW_WRITE) {
1653 rc = osc_enter_cache(cli, loi, oap);
1655 spin_unlock(&cli->cl_loi_list_lock);
1658 lop = &loi->loi_write_lop;
1660 lop = &loi->loi_read_lop;
1663 if (oap->oap_async_flags & ASYNC_URGENT)
1664 list_add(&oap->oap_urgent_item, &lop->lop_urgent);
1665 list_add_tail(&oap->oap_pending_item, &lop->lop_pending);
1666 lop_update_pending(cli, lop, cmd, 1);
1668 loi_onto_ready_list(cli, loi);
1670 LOI_DEBUG(loi, "oap %p page %p added for cmd %d\n", oap, oap->oap_page,
1673 osc_check_rpcs(cli);
1674 spin_unlock(&cli->cl_loi_list_lock);
1679 /* aka (~was & now & flag), but this is more clear :) */
1680 #define SETTING(was, now, flag) (!(was & flag) && (now & flag))
1682 static int osc_set_async_flags(struct obd_export *exp,
1683 struct lov_stripe_md *lsm,
1684 struct lov_oinfo *loi, void *cookie,
1685 obd_flag async_flags)
1687 struct client_obd *cli = &exp->exp_obd->u.cli;
1688 struct loi_oap_pages *lop;
1689 struct osc_async_page *oap;
1693 oap = oap_from_cookie(cookie);
1695 RETURN(PTR_ERR(oap));
1697 if (cli->cl_import == NULL || cli->cl_import->imp_invalid)
1701 loi = &lsm->lsm_oinfo[0];
1703 if (oap->oap_cmd == OBD_BRW_WRITE) {
1704 lop = &loi->loi_write_lop;
1706 lop = &loi->loi_read_lop;
1709 spin_lock(&cli->cl_loi_list_lock);
1711 if (list_empty(&oap->oap_pending_item))
1712 GOTO(out, rc = -EINVAL);
1714 if ((oap->oap_async_flags & async_flags) == async_flags)
1717 if (SETTING(oap->oap_async_flags, async_flags, ASYNC_READY))
1718 oap->oap_async_flags |= ASYNC_READY;
1720 if (SETTING(oap->oap_async_flags, async_flags, ASYNC_URGENT)) {
1721 if (list_empty(&oap->oap_rpc_item)) {
1722 list_add(&oap->oap_urgent_item, &lop->lop_urgent);
1723 loi_onto_ready_list(cli, loi);
1727 LOI_DEBUG(loi, "oap %p page %p has flags %x\n", oap, oap->oap_page,
1728 oap->oap_async_flags);
1730 osc_check_rpcs(cli);
1731 spin_unlock(&cli->cl_loi_list_lock);
1735 static int osc_queue_sync_io(struct obd_export *exp, struct lov_stripe_md *lsm,
1736 struct lov_oinfo *loi,
1737 struct obd_sync_io_container *osic, void *cookie,
1738 int cmd, obd_off off, int count,
1741 struct client_obd *cli = &exp->exp_obd->u.cli;
1742 struct osc_async_page *oap;
1743 struct loi_oap_pages *lop;
1746 oap = oap_from_cookie(cookie);
1748 RETURN(PTR_ERR(oap));
1750 if (cli->cl_import == NULL || cli->cl_import->imp_invalid)
1753 if (!list_empty(&oap->oap_pending_item) ||
1754 !list_empty(&oap->oap_urgent_item) ||
1755 !list_empty(&oap->oap_rpc_item))
1759 loi = &lsm->lsm_oinfo[0];
1761 spin_lock(&cli->cl_loi_list_lock);
1764 oap->oap_page_off = off;
1765 oap->oap_count = count;
1766 oap->oap_brw_flags = brw_flags;
1768 if (cmd == OBD_BRW_WRITE)
1769 lop = &loi->loi_write_lop;
1771 lop = &loi->loi_read_lop;
1773 list_add_tail(&oap->oap_pending_item, &lop->lop_pending_sync);
1774 oap->oap_osic = osic;
1777 LOI_DEBUG(loi, "oap %p page %p on sync pending\n", oap, oap->oap_page);
1779 spin_unlock(&cli->cl_loi_list_lock);
1784 static void osc_sync_to_pending(struct client_obd *cli, struct lov_oinfo *loi,
1785 struct loi_oap_pages *lop, int cmd)
1787 struct list_head *pos, *tmp;
1788 struct osc_async_page *oap;
1790 list_for_each_safe(pos, tmp, &lop->lop_pending_sync) {
1791 oap = list_entry(pos, struct osc_async_page, oap_pending_item);
1792 list_del(&oap->oap_pending_item);
1793 oap->oap_async_flags |= ASYNC_READY | ASYNC_URGENT |
1795 list_add_tail(&oap->oap_pending_item, &lop->lop_pending);
1796 list_add(&oap->oap_urgent_item, &lop->lop_urgent);
1797 lop_update_pending(cli, lop, cmd, 1);
1799 loi_onto_ready_list(cli, loi);
1802 static int osc_trigger_sync_io(struct obd_export *exp,
1803 struct lov_stripe_md *lsm,
1804 struct lov_oinfo *loi,
1805 struct obd_sync_io_container *osic)
1807 struct client_obd *cli = &exp->exp_obd->u.cli;
1810 if (cli->cl_import == NULL || cli->cl_import->imp_invalid)
1814 loi = &lsm->lsm_oinfo[0];
1816 spin_lock(&cli->cl_loi_list_lock);
1818 osc_sync_to_pending(cli, loi, &loi->loi_write_lop, OBD_BRW_WRITE);
1819 osc_sync_to_pending(cli, loi, &loi->loi_read_lop, OBD_BRW_READ);
1821 osc_check_rpcs(cli);
1822 spin_unlock(&cli->cl_loi_list_lock);
1827 static int osc_teardown_async_page(struct obd_export *exp,
1828 struct lov_stripe_md *lsm,
1829 struct lov_oinfo *loi, void *cookie)
1831 struct client_obd *cli = &exp->exp_obd->u.cli;
1832 struct loi_oap_pages *lop;
1833 struct osc_async_page *oap;
1837 oap = oap_from_cookie(cookie);
1839 RETURN(PTR_ERR(oap));
1842 loi = &lsm->lsm_oinfo[0];
1844 if (oap->oap_cmd == OBD_BRW_WRITE) {
1845 lop = &loi->loi_write_lop;
1847 lop = &loi->loi_read_lop;
1850 spin_lock(&cli->cl_loi_list_lock);
1852 osc_exit_cache(cli, oap);
1854 if (!list_empty(&oap->oap_rpc_item))
1855 GOTO(out, rc = -EBUSY);
1857 if (!list_empty(&oap->oap_urgent_item)) {
1858 list_del_init(&oap->oap_urgent_item);
1859 oap->oap_async_flags &= ~ASYNC_URGENT;
1861 if (!list_empty(&oap->oap_pending_item)) {
1862 list_del_init(&oap->oap_pending_item);
1863 lop_update_pending(cli, lop, oap->oap_cmd, -1);
1865 if (!list_empty(&loi->loi_cli_item) && !loi_makes_rpc(cli, loi))
1866 list_del_init(&loi->loi_cli_item);
1868 LOI_DEBUG(loi, "oap %p page %p torn down\n", oap, oap->oap_page);
1870 spin_unlock(&cli->cl_loi_list_lock);
1871 OBD_FREE(oap, sizeof(*oap));
1876 /* Note: caller will lock/unlock, and set uptodate on the pages */
1877 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
1878 static int sanosc_brw_read(struct obd_export *exp, struct obdo *oa,
1879 struct lov_stripe_md *lsm, obd_count page_count,
1880 struct brw_page *pga)
1882 struct ptlrpc_request *request = NULL;
1883 struct ost_body *body;
1884 struct niobuf_remote *nioptr;
1885 struct obd_ioobj *iooptr;
1886 int rc, size[3] = {sizeof(*body)}, mapped = 0;
1890 /* XXX does not handle 'new' brw protocol */
1892 size[1] = sizeof(struct obd_ioobj);
1893 size[2] = page_count * sizeof(*nioptr);
1895 request = ptlrpc_prep_req(class_exp2cliimp(exp), OST_SAN_READ, 3,
1900 body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof(*body));
1901 iooptr = lustre_msg_buf(request->rq_reqmsg, 1, sizeof(*iooptr));
1902 nioptr = lustre_msg_buf(request->rq_reqmsg, 2,
1903 sizeof(*nioptr) * page_count);
1905 memcpy(&body->oa, oa, sizeof(body->oa));
1907 obdo_to_ioobj(oa, iooptr);
1908 iooptr->ioo_bufcnt = page_count;
1910 for (mapped = 0; mapped < page_count; mapped++, nioptr++) {
1911 LASSERT(PageLocked(pga[mapped].pg));
1912 LASSERT(mapped == 0 || pga[mapped].off > pga[mapped - 1].off);
1914 nioptr->offset = pga[mapped].off;
1915 nioptr->len = pga[mapped].count;
1916 nioptr->flags = pga[mapped].flag;
1919 size[1] = page_count * sizeof(*nioptr);
1920 request->rq_replen = lustre_msg_size(2, size);
1922 rc = ptlrpc_queue_wait(request);
1926 body = lustre_swab_repbuf(request, 0, sizeof(*body),
1927 lustre_swab_ost_body);
1929 CERROR("Can't unpack body\n");
1930 GOTO(out_req, rc = -EPROTO);
1933 memcpy(oa, &body->oa, sizeof(*oa));
1935 swab = lustre_msg_swabbed(request->rq_repmsg);
1936 LASSERT_REPSWAB(request, 1);
1937 nioptr = lustre_msg_buf(request->rq_repmsg, 1, size[1]);
1939 /* nioptr missing or short */
1940 GOTO(out_req, rc = -EPROTO);
1944 for (mapped = 0; mapped < page_count; mapped++, nioptr++) {
1945 struct page *page = pga[mapped].pg;
1946 struct buffer_head *bh;
1950 lustre_swab_niobuf_remote (nioptr);
1952 /* got san device associated */
1953 LASSERT(exp->exp_obd != NULL);
1954 dev = exp->exp_obd->u.cli.cl_sandev;
1957 if (!nioptr->offset) {
1958 CDEBUG(D_PAGE, "hole at ino %lu; index %ld\n",
1959 page->mapping->host->i_ino,
1961 memset(page_address(page), 0, PAGE_SIZE);
1965 if (!page->buffers) {
1966 create_empty_buffers(page, dev, PAGE_SIZE);
1969 clear_bit(BH_New, &bh->b_state);
1970 set_bit(BH_Mapped, &bh->b_state);
1971 bh->b_blocknr = (unsigned long)nioptr->offset;
1973 clear_bit(BH_Uptodate, &bh->b_state);
1975 ll_rw_block(READ, 1, &bh);
1979 /* if buffer already existed, it must be the
1980 * one we mapped before, check it */
1981 LASSERT(!test_bit(BH_New, &bh->b_state));
1982 LASSERT(test_bit(BH_Mapped, &bh->b_state));
1983 LASSERT(bh->b_blocknr == (unsigned long)nioptr->offset);
1985 /* wait it's io completion */
1986 if (test_bit(BH_Lock, &bh->b_state))
1989 if (!test_bit(BH_Uptodate, &bh->b_state))
1990 ll_rw_block(READ, 1, &bh);
1994 /* must do syncronous write here */
1996 if (!buffer_uptodate(bh)) {
2004 ptlrpc_req_finished(request);
2008 static int sanosc_brw_write(struct obd_export *exp, struct obdo *oa,
2009 struct lov_stripe_md *lsm, obd_count page_count,
2010 struct brw_page *pga)
2012 struct ptlrpc_request *request = NULL;
2013 struct ost_body *body;
2014 struct niobuf_remote *nioptr;
2015 struct obd_ioobj *iooptr;
2016 int rc, size[3] = {sizeof(*body)}, mapped = 0;
2020 size[1] = sizeof(struct obd_ioobj);
2021 size[2] = page_count * sizeof(*nioptr);
2023 request = ptlrpc_prep_req(class_exp2cliimp(exp), OST_SAN_WRITE,
2028 body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof (*body));
2029 iooptr = lustre_msg_buf(request->rq_reqmsg, 1, sizeof (*iooptr));
2030 nioptr = lustre_msg_buf(request->rq_reqmsg, 2,
2031 sizeof (*nioptr) * page_count);
2033 memcpy(&body->oa, oa, sizeof(body->oa));
2035 obdo_to_ioobj(oa, iooptr);
2036 iooptr->ioo_bufcnt = page_count;
2039 for (mapped = 0; mapped < page_count; mapped++, nioptr++) {
2040 LASSERT(PageLocked(pga[mapped].pg));
2041 LASSERT(mapped == 0 || pga[mapped].off > pga[mapped - 1].off);
2043 nioptr->offset = pga[mapped].off;
2044 nioptr->len = pga[mapped].count;
2045 nioptr->flags = pga[mapped].flag;
2048 size[1] = page_count * sizeof(*nioptr);
2049 request->rq_replen = lustre_msg_size(2, size);
2051 rc = ptlrpc_queue_wait(request);
2055 swab = lustre_msg_swabbed (request->rq_repmsg);
2056 LASSERT_REPSWAB (request, 1);
2057 nioptr = lustre_msg_buf(request->rq_repmsg, 1, size[1]);
2059 CERROR("absent/short niobuf array\n");
2060 GOTO(out_req, rc = -EPROTO);
2064 for (mapped = 0; mapped < page_count; mapped++, nioptr++) {
2065 struct page *page = pga[mapped].pg;
2066 struct buffer_head *bh;
2070 lustre_swab_niobuf_remote (nioptr);
2072 /* got san device associated */
2073 LASSERT(exp->exp_obd != NULL);
2074 dev = exp->exp_obd->u.cli.cl_sandev;
2076 if (!page->buffers) {
2077 create_empty_buffers(page, dev, PAGE_SIZE);
2080 LASSERT(!test_bit(BH_New, &page->buffers->b_state));
2081 LASSERT(test_bit(BH_Mapped, &page->buffers->b_state));
2082 LASSERT(page->buffers->b_blocknr ==
2083 (unsigned long)nioptr->offset);
2089 /* if buffer locked, wait it's io completion */
2090 if (test_bit(BH_Lock, &bh->b_state))
2093 clear_bit(BH_New, &bh->b_state);
2094 set_bit(BH_Mapped, &bh->b_state);
2096 /* override the block nr */
2097 bh->b_blocknr = (unsigned long)nioptr->offset;
2099 /* we are about to write it, so set it
2101 * page lock should garentee no race condition here */
2102 set_bit(BH_Uptodate, &bh->b_state);
2103 set_bit(BH_Dirty, &bh->b_state);
2105 ll_rw_block(WRITE, 1, &bh);
2107 /* must do syncronous write here */
2109 if (!buffer_uptodate(bh) || test_bit(BH_Dirty, &bh->b_state)) {
2117 ptlrpc_req_finished(request);
2121 static int sanosc_brw(int cmd, struct obd_export *exp, struct obdo *oa,
2122 struct lov_stripe_md *lsm, obd_count page_count,
2123 struct brw_page *pga, struct obd_trans_info *oti)
2127 while (page_count) {
2128 obd_count pages_per_brw;
2131 if (page_count > OSC_BRW_MAX_IOV)
2132 pages_per_brw = OSC_BRW_MAX_IOV;
2134 pages_per_brw = page_count;
2136 if (cmd & OBD_BRW_WRITE)
2137 rc = sanosc_brw_write(exp, oa, lsm, pages_per_brw,pga);
2139 rc = sanosc_brw_read(exp, oa, lsm, pages_per_brw, pga);
2144 page_count -= pages_per_brw;
2145 pga += pages_per_brw;
2152 static void osc_set_data_with_check(struct lustre_handle *lockh, void *data)
2154 struct ldlm_lock *lock = ldlm_handle2lock(lockh);
2156 LASSERT(lock != NULL);
2157 l_lock(&lock->l_resource->lr_namespace->ns_lock);
2159 if (lock->l_ast_data && lock->l_ast_data != data) {
2160 struct inode *new_inode = data;
2161 struct inode *old_inode = lock->l_ast_data;
2162 unsigned long state = old_inode->i_state & I_FREEING;
2163 CERROR("Found existing inode %p/%lu/%u state %lu in lock: "
2164 "setting data to %p/%lu/%u\n", old_inode,
2165 old_inode->i_ino, old_inode->i_generation, state,
2166 new_inode, new_inode->i_ino, new_inode->i_generation);
2170 lock->l_ast_data = data;
2171 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
2172 LDLM_LOCK_PUT(lock);
2175 static int osc_change_cbdata(struct obd_export *exp, struct lov_stripe_md *lsm,
2176 ldlm_iterator_t replace, void *data)
2178 struct ldlm_res_id res_id = { .name = {lsm->lsm_object_id} };
2179 struct obd_device *obd = class_exp2obd(exp);
2181 ldlm_change_cbdata(obd->obd_namespace, &res_id, replace, data);
2185 static int osc_enqueue(struct obd_export *exp, struct lov_stripe_md *lsm,
2186 struct lustre_handle *parent_lock,
2187 __u32 type, void *extentp, int extent_len, __u32 mode,
2188 int *flags, void *callback, void *data,
2189 struct lustre_handle *lockh)
2191 struct ldlm_res_id res_id = { .name = {lsm->lsm_object_id} };
2192 struct obd_device *obd = exp->exp_obd;
2193 struct ldlm_extent *extent = extentp;
2197 /* Filesystem lock extents are extended to page boundaries so that
2198 * dealing with the page cache is a little smoother. */
2199 extent->start -= extent->start & ~PAGE_MASK;
2200 extent->end |= ~PAGE_MASK;
2202 /* Next, search for already existing extent locks that will cover us */
2203 rc = ldlm_lock_match(obd->obd_namespace, 0, &res_id,
2204 type, extent, sizeof(*extent), mode, lockh);
2206 osc_set_data_with_check(lockh, data);
2207 /* We already have a lock, and it's referenced */
2211 /* If we're trying to read, we also search for an existing PW lock. The
2212 * VFS and page cache already protect us locally, so lots of readers/
2213 * writers can share a single PW lock.
2215 * There are problems with conversion deadlocks, so instead of
2216 * converting a read lock to a write lock, we'll just enqueue a new
2219 * At some point we should cancel the read lock instead of making them
2220 * send us a blocking callback, but there are problems with canceling
2221 * locks out from other users right now, too. */
2223 if (mode == LCK_PR) {
2224 rc = ldlm_lock_match(obd->obd_namespace, 0, &res_id, type,
2225 extent, sizeof(*extent), LCK_PW, lockh);
2227 /* FIXME: This is not incredibly elegant, but it might
2228 * be more elegant than adding another parameter to
2229 * lock_match. I want a second opinion. */
2230 ldlm_lock_addref(lockh, LCK_PR);
2231 ldlm_lock_decref(lockh, LCK_PW);
2232 osc_set_data_with_check(lockh, data);
2237 rc = ldlm_cli_enqueue(exp, NULL, obd->obd_namespace, parent_lock,
2238 res_id, type, extent, sizeof(*extent), mode,
2239 flags,ldlm_completion_ast, callback, data, lockh);
2243 static int osc_match(struct obd_export *exp, struct lov_stripe_md *lsm,
2244 __u32 type, void *extentp, int extent_len, __u32 mode,
2245 int *flags, void *data, struct lustre_handle *lockh)
2247 struct ldlm_res_id res_id = { .name = {lsm->lsm_object_id} };
2248 struct obd_device *obd = exp->exp_obd;
2249 struct ldlm_extent *extent = extentp;
2253 OBD_FAIL_RETURN(OBD_FAIL_OSC_MATCH, -EIO);
2255 /* Filesystem lock extents are extended to page boundaries so that
2256 * dealing with the page cache is a little smoother */
2257 extent->start -= extent->start & ~PAGE_MASK;
2258 extent->end |= ~PAGE_MASK;
2260 /* Next, search for already existing extent locks that will cover us */
2261 rc = ldlm_lock_match(obd->obd_namespace, *flags, &res_id, type,
2262 extent, sizeof(*extent), mode, lockh);
2264 osc_set_data_with_check(lockh, data);
2267 /* If we're trying to read, we also search for an existing PW lock. The
2268 * VFS and page cache already protect us locally, so lots of readers/
2269 * writers can share a single PW lock. */
2270 if (mode == LCK_PR) {
2271 rc = ldlm_lock_match(obd->obd_namespace, *flags, &res_id, type,
2272 extent, sizeof(*extent), LCK_PW, lockh);
2274 /* FIXME: This is not incredibly elegant, but it might
2275 * be more elegant than adding another parameter to
2276 * lock_match. I want a second opinion. */
2277 osc_set_data_with_check(lockh, data);
2278 ldlm_lock_addref(lockh, LCK_PR);
2279 ldlm_lock_decref(lockh, LCK_PW);
2285 static int osc_cancel(struct obd_export *exp, struct lov_stripe_md *md,
2286 __u32 mode, struct lustre_handle *lockh)
2290 ldlm_lock_decref(lockh, mode);
2295 static int osc_cancel_unused(struct obd_export *exp,
2296 struct lov_stripe_md *lsm, int flags, void *opaque)
2298 struct obd_device *obd = class_exp2obd(exp);
2299 struct ldlm_res_id res_id = { .name = {lsm->lsm_object_id} };
2301 return ldlm_cli_cancel_unused(obd->obd_namespace, &res_id, flags,
2305 static int osc_statfs(struct obd_device *obd, struct obd_statfs *osfs,
2306 unsigned long max_age)
2308 struct obd_statfs *msfs;
2309 struct ptlrpc_request *request;
2310 int rc, size = sizeof(*osfs);
2313 /* We could possibly pass max_age in the request (as an absolute
2314 * timestamp or a "seconds.usec ago") so the target can avoid doing
2315 * extra calls into the filesystem if that isn't necessary (e.g.
2316 * during mount that would help a bit). Having relative timestamps
2317 * is not so great if request processing is slow, while absolute
2318 * timestamps are not ideal because they need time synchronization. */
2319 request = ptlrpc_prep_req(obd->u.cli.cl_import, OST_STATFS,0,NULL,NULL);
2323 request->rq_replen = lustre_msg_size(1, &size);
2324 request->rq_request_portal = OST_CREATE_PORTAL; //XXX FIXME bug 249
2326 rc = ptlrpc_queue_wait(request);
2330 msfs = lustre_swab_repbuf(request, 0, sizeof(*msfs),
2331 lustre_swab_obd_statfs);
2333 CERROR("Can't unpack obd_statfs\n");
2334 GOTO(out, rc = -EPROTO);
2337 memcpy(osfs, msfs, sizeof(*osfs));
2341 ptlrpc_req_finished(request);
2345 /* Retrieve object striping information.
2347 * @lmmu is a pointer to an in-core struct with lmm_ost_count indicating
2348 * the maximum number of OST indices which will fit in the user buffer.
2349 * lmm_magic must be LOV_MAGIC (we only use 1 slot here).
2351 static int osc_getstripe(struct lov_stripe_md *lsm, struct lov_user_md *lump)
2353 struct lov_user_md lum;
2354 struct lov_mds_md *lmmk;
2361 rc = copy_from_user(&lum, lump, sizeof(lum));
2365 if (lum.lmm_magic != LOV_USER_MAGIC)
2368 if (lum.lmm_stripe_count < 1)
2371 lmm_size = sizeof(lum) + sizeof(lum.lmm_objects[0]);
2372 OBD_ALLOC(lmmk, lmm_size);
2376 lmmk->lmm_stripe_count = 1;
2377 lmmk->lmm_object_id = lsm->lsm_object_id;
2378 lmmk->lmm_objects[0].l_object_id = lsm->lsm_object_id;
2380 if (copy_to_user(lump, lmmk, lmm_size))
2383 OBD_FREE(lmmk, lmm_size);
2388 static int osc_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
2389 void *karg, void *uarg)
2391 struct obd_device *obd = exp->exp_obd;
2392 struct obd_ioctl_data *data = karg;
2397 case OBD_IOC_LOV_GET_CONFIG: {
2399 struct lov_desc *desc;
2400 struct obd_uuid uuid;
2404 if (obd_ioctl_getdata(&buf, &len, (void *)uarg))
2405 GOTO(out, err = -EINVAL);
2407 data = (struct obd_ioctl_data *)buf;
2409 if (sizeof(*desc) > data->ioc_inllen1) {
2411 GOTO(out, err = -EINVAL);
2414 if (data->ioc_inllen2 < sizeof(uuid)) {
2416 GOTO(out, err = -EINVAL);
2419 desc = (struct lov_desc *)data->ioc_inlbuf1;
2420 desc->ld_tgt_count = 1;
2421 desc->ld_active_tgt_count = 1;
2422 desc->ld_default_stripe_count = 1;
2423 desc->ld_default_stripe_size = 0;
2424 desc->ld_default_stripe_offset = 0;
2425 desc->ld_pattern = 0;
2426 memcpy(&desc->ld_uuid, &obd->obd_uuid, sizeof(uuid));
2428 memcpy(data->ioc_inlbuf2, &obd->obd_uuid, sizeof(uuid));
2430 err = copy_to_user((void *)uarg, buf, len);
2433 obd_ioctl_freedata(buf, len);
2436 case LL_IOC_LOV_SETSTRIPE:
2437 err = obd_alloc_memmd(exp, karg);
2441 case LL_IOC_LOV_GETSTRIPE:
2442 err = osc_getstripe(karg, uarg);
2444 case OBD_IOC_CLIENT_RECOVER:
2445 err = ptlrpc_recover_import(obd->u.cli.cl_import,
2450 case IOC_OSC_SET_ACTIVE:
2451 err = ptlrpc_set_import_active(obd->u.cli.cl_import,
2455 CDEBUG(D_INODE, "unrecognised ioctl %#x by %s\n", cmd, current->comm);
2456 GOTO(out, err = -ENOTTY);
2462 static int osc_get_info(struct obd_export *exp, obd_count keylen,
2463 void *key, __u32 *vallen, void *val)
2466 if (!vallen || !val)
2469 if (keylen > strlen("lock_to_stripe") &&
2470 strcmp(key, "lock_to_stripe") == 0) {
2471 __u32 *stripe = val;
2472 *vallen = sizeof(*stripe);
2475 } else if (keylen >= strlen("last_id") && strcmp(key, "last_id") == 0) {
2476 struct ptlrpc_request *req;
2478 char *bufs[1] = {key};
2480 req = ptlrpc_prep_req(class_exp2cliimp(exp), OST_GET_INFO, 1,
2485 req->rq_replen = lustre_msg_size(1, vallen);
2486 rc = ptlrpc_queue_wait(req);
2490 reply = lustre_swab_repbuf(req, 0, sizeof(*reply),
2491 lustre_swab_ost_last_id);
2492 if (reply == NULL) {
2493 CERROR("Can't unpack OST last ID\n");
2494 GOTO(out, rc = -EPROTO);
2496 *((obd_id *)val) = *reply;
2498 ptlrpc_req_finished(req);
2504 static int osc_set_info(struct obd_export *exp, obd_count keylen,
2505 void *key, obd_count vallen, void *val)
2507 struct ptlrpc_request *req;
2508 struct obd_import *imp = class_exp2cliimp(exp);
2509 struct llog_ctxt *ctxt;
2510 int rc, size = keylen;
2511 char *bufs[1] = {key};
2514 if (keylen == strlen("next_id") &&
2515 memcmp(key, "next_id", strlen("next_id")) == 0) {
2516 if (vallen != sizeof(obd_id))
2518 exp->u.eu_osc_data.oed_oscc.oscc_next_id = *((obd_id*)val) + 1;
2519 CDEBUG(D_INODE, "%s: set oscc_next_id = "LPU64"\n",
2520 exp->exp_obd->obd_name,
2521 exp->u.eu_osc_data.oed_oscc.oscc_next_id);
2526 if (keylen == strlen("growth_count") &&
2527 memcmp(key, "growth_count", strlen("growth_count")) == 0) {
2528 if (vallen != sizeof(int))
2530 exp->u.eu_osc_data.oed_oscc.oscc_grow_count = *((int*)val);
2534 if (keylen == strlen("unlinked") &&
2535 memcmp(key, "unlinked", keylen) == 0) {
2536 struct osc_creator *oscc = &exp->u.eu_osc_data.oed_oscc;
2537 spin_lock(&oscc->oscc_lock);
2538 oscc->oscc_flags &= ~OSCC_FLAG_NOSPC;
2539 spin_unlock(&oscc->oscc_lock);
2543 if (keylen < strlen("mds_conn") ||
2544 memcmp(key, "mds_conn", strlen("mds_conn")) != 0)
2548 req = ptlrpc_prep_req(imp, OST_SET_INFO, 1, &size, bufs);
2552 req->rq_replen = lustre_msg_size(0, NULL);
2553 rc = ptlrpc_queue_wait(req);
2554 ptlrpc_req_finished(req);
2556 ctxt = llog_get_context(exp->exp_obd, LLOG_UNLINK_ORIG_CTXT);
2558 rc = llog_initiator_connect(ctxt);
2563 imp->imp_server_timeout = 1;
2564 CDEBUG(D_HA, "pinging OST %s\n", imp->imp_target_uuid.uuid);
2565 ptlrpc_pinger_add_import(imp);
2571 static struct llog_operations osc_size_repl_logops = {
2572 lop_cancel: llog_obd_repl_cancel
2575 static struct llog_operations osc_unlink_orig_logops;
2576 static int osc_llog_init(struct obd_device *obd, struct obd_device *tgt,
2577 int count, struct llog_logid *logid)
2582 osc_unlink_orig_logops = llog_lvfs_ops;
2583 osc_unlink_orig_logops.lop_setup = llog_obd_origin_setup;
2584 osc_unlink_orig_logops.lop_cleanup = llog_obd_origin_cleanup;
2585 osc_unlink_orig_logops.lop_add = llog_obd_origin_add;
2586 osc_unlink_orig_logops.lop_connect = llog_origin_connect;
2588 rc = llog_setup(obd, LLOG_UNLINK_ORIG_CTXT, tgt, count, logid,
2589 &osc_unlink_orig_logops);
2593 rc = llog_setup(obd, LLOG_SIZE_REPL_CTXT, tgt, count, NULL,
2594 &osc_size_repl_logops);
2598 static int osc_llog_finish(struct obd_device *obd, int count)
2603 rc = llog_cleanup(llog_get_context(obd, LLOG_UNLINK_ORIG_CTXT));
2607 rc = llog_cleanup(llog_get_context(obd, LLOG_SIZE_REPL_CTXT));
2612 static int osc_connect(struct lustre_handle *exph,
2613 struct obd_device *obd, struct obd_uuid *cluuid)
2616 struct obd_export *exp;
2618 rc = client_connect_import(exph, obd, cluuid);
2620 if (obd->u.cli.cl_conn_count == 1) {
2621 exp = class_conn2export(exph);
2628 static int osc_disconnect(struct obd_export *exp, int flags)
2630 struct obd_device *obd = class_exp2obd(exp);
2631 struct llog_ctxt *ctxt = llog_get_context(obd, LLOG_SIZE_REPL_CTXT);
2634 if (obd->u.cli.cl_conn_count == 1) {
2635 /* flush any remaining cancel messages out to the target */
2636 llog_sync(ctxt, exp);
2638 /* balance the conn2export for oscc in osc_connect */
2639 class_export_put(exp);
2642 rc = client_disconnect_export(exp, flags);
2646 static int osc_lock_contains(struct obd_export *exp, struct lov_stripe_md *lsm,
2647 struct ldlm_lock *lock, obd_off offset)
2653 if (lock->l_policy_data.l_extent.start <= offset &&
2654 lock->l_policy_data.l_extent.end >= offset)
2659 static int osc_invalidate_import(struct obd_device *obd,
2660 struct obd_import *imp)
2662 LASSERT(imp->imp_obd == obd);
2663 /* this used to try and tear down queued pages, but it was
2664 * not correctly implemented. We'll have to do it again once
2665 * we call obd_invalidate_import() agian */
2670 int osc_setup(struct obd_device *obd, obd_count len, void *buf)
2674 rc = ptlrpcd_addref();
2678 rc = client_obd_setup(obd, len, buf);
2684 int osc_cleanup(struct obd_device *obd, int flags)
2688 rc = client_obd_cleanup(obd, flags);
2694 struct obd_ops osc_obd_ops = {
2695 o_owner: THIS_MODULE,
2696 o_attach: osc_attach,
2697 o_detach: osc_detach,
2699 o_cleanup: osc_cleanup,
2700 o_connect: osc_connect,
2701 o_disconnect: osc_disconnect,
2702 o_statfs: osc_statfs,
2703 o_packmd: osc_packmd,
2704 o_unpackmd: osc_unpackmd,
2705 o_create: osc_create,
2706 o_destroy: osc_destroy,
2707 o_getattr: osc_getattr,
2708 o_getattr_async:osc_getattr_async,
2709 o_setattr: osc_setattr,
2711 o_brw_async: osc_brw_async,
2712 .o_prep_async_page = osc_prep_async_page,
2713 .o_queue_async_io = osc_queue_async_io,
2714 .o_set_async_flags = osc_set_async_flags,
2715 .o_queue_sync_io = osc_queue_sync_io,
2716 .o_trigger_sync_io = osc_trigger_sync_io,
2717 .o_teardown_async_page = osc_teardown_async_page,
2720 o_enqueue: osc_enqueue,
2722 o_change_cbdata:osc_change_cbdata,
2723 o_cancel: osc_cancel,
2724 o_cancel_unused:osc_cancel_unused,
2725 o_iocontrol: osc_iocontrol,
2726 o_get_info: osc_get_info,
2727 o_set_info: osc_set_info,
2728 o_lock_contains:osc_lock_contains,
2729 o_invalidate_import: osc_invalidate_import,
2730 o_llog_init: osc_llog_init,
2731 o_llog_finish: osc_llog_finish,
2734 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
2735 struct obd_ops sanosc_obd_ops = {
2736 o_owner: THIS_MODULE,
2737 o_attach: osc_attach,
2738 o_detach: osc_detach,
2739 o_cleanup: client_obd_cleanup,
2740 o_connect: osc_connect,
2741 o_disconnect: client_disconnect_export,
2742 o_statfs: osc_statfs,
2743 o_packmd: osc_packmd,
2744 o_unpackmd: osc_unpackmd,
2745 o_create: osc_real_create,
2746 o_destroy: osc_destroy,
2747 o_getattr: osc_getattr,
2748 o_getattr_async:osc_getattr_async,
2749 o_setattr: osc_setattr,
2750 o_setup: client_sanobd_setup,
2754 o_enqueue: osc_enqueue,
2756 o_change_cbdata:osc_change_cbdata,
2757 o_cancel: osc_cancel,
2758 o_cancel_unused:osc_cancel_unused,
2759 o_iocontrol: osc_iocontrol,
2760 o_lock_contains:osc_lock_contains,
2761 o_invalidate_import: osc_invalidate_import,
2762 o_llog_init: osc_llog_init,
2763 o_llog_finish: osc_llog_finish,
2767 int __init osc_init(void)
2769 struct lprocfs_static_vars lvars, sanlvars;
2773 lprocfs_init_vars(osc, &lvars);
2774 lprocfs_init_vars(osc, &sanlvars);
2776 rc = class_register_type(&osc_obd_ops, lvars.module_vars,
2781 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
2782 rc = class_register_type(&sanosc_obd_ops, sanlvars.module_vars,
2783 LUSTRE_SANOSC_NAME);
2785 class_unregister_type(LUSTRE_OSC_NAME);
2791 static void /*__exit*/ osc_exit(void)
2793 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
2794 class_unregister_type(LUSTRE_SANOSC_NAME);
2796 class_unregister_type(LUSTRE_OSC_NAME);
2800 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
2801 MODULE_DESCRIPTION("Lustre Object Storage Client (OSC)");
2802 MODULE_LICENSE("GPL");
2804 module_init(osc_init);
2805 module_exit(osc_exit);