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/ctype.h>
40 # include <linux/init.h>
41 # if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
42 # include <linux/workqueue.h>
43 # include <linux/smp_lock.h>
45 # include <linux/locks.h>
47 #else /* __KERNEL__ */
48 # include <liblustre.h>
51 # include <linux/lustre_dlm.h>
52 #include <linux/kp30.h>
53 #include <linux/lustre_net.h>
54 #include <linux/lustre_user.h>
55 #include <linux/obd_ost.h>
56 #include <linux/obd_lov.h>
62 #include <linux/lustre_ha.h>
63 #include <linux/lprocfs_status.h>
64 #include <linux/lustre_log.h>
65 #include "osc_internal.h"
68 static int osc_attach(struct obd_device *dev, obd_count len, void *data)
70 struct lprocfs_static_vars lvars;
74 lprocfs_init_vars(osc,&lvars);
75 rc = lprocfs_obd_attach(dev, lvars.obd_vars);
79 rc = lproc_osc_attach_seqstat(dev);
81 lprocfs_obd_detach(dev);
85 ptlrpc_lprocfs_register_obd(dev);
89 static int osc_detach(struct obd_device *dev)
91 ptlrpc_lprocfs_unregister_obd(dev);
92 return lprocfs_obd_detach(dev);
96 /* Pack OSC object metadata for disk storage (LE byte order). */
97 static int osc_packmd(struct obd_export *exp, struct lov_mds_md **lmmp,
98 struct lov_stripe_md *lsm)
103 lmm_size = sizeof(**lmmp);
108 OBD_FREE(*lmmp, lmm_size);
114 OBD_ALLOC(*lmmp, lmm_size);
120 LASSERT(lsm->lsm_object_id);
121 LASSERT(lsm->lsm_object_gr);
122 (*lmmp)->lmm_object_id = cpu_to_le64(lsm->lsm_object_id);
123 (*lmmp)->lmm_object_gr = cpu_to_le64(lsm->lsm_object_gr);
129 /* Unpack OSC object metadata from disk storage (LE byte order). */
130 static int osc_unpackmd(struct obd_export *exp, struct lov_stripe_md **lsmp,
131 struct lov_mds_md *lmm, int lmm_bytes)
137 if (lmm_bytes < sizeof (*lmm)) {
138 CERROR("lov_mds_md too small: %d, need %d\n",
139 lmm_bytes, (int)sizeof(*lmm));
142 /* XXX LOV_MAGIC etc check? */
144 if (lmm->lmm_object_id == 0) {
145 CERROR("lov_mds_md: zero lmm_object_id\n");
150 lsm_size = lov_stripe_md_size(1);
154 if (*lsmp != NULL && lmm == NULL) {
155 OBD_FREE(*lsmp, lsm_size);
161 OBD_ALLOC(*lsmp, lsm_size);
164 loi_init((*lsmp)->lsm_oinfo);
168 /* XXX zero *lsmp? */
169 (*lsmp)->lsm_object_id = le64_to_cpu (lmm->lmm_object_id);
170 (*lsmp)->lsm_object_gr = le64_to_cpu (lmm->lmm_object_gr);
171 LASSERT((*lsmp)->lsm_object_id);
172 LASSERT((*lsmp)->lsm_object_gr);
175 (*lsmp)->lsm_maxbytes = LUSTRE_STRIPE_MAXBYTES;
180 static int osc_getattr_interpret(struct ptlrpc_request *req,
181 struct osc_getattr_async_args *aa, int rc)
183 struct ost_body *body;
189 body = lustre_swab_repbuf(req, 0, sizeof(*body), lustre_swab_ost_body);
191 CDEBUG(D_INODE, "mode: %o\n", body->oa.o_mode);
192 memcpy(aa->aa_oa, &body->oa, sizeof(*aa->aa_oa));
194 /* This should really be sent by the OST */
195 aa->aa_oa->o_blksize = PTLRPC_MAX_BRW_SIZE;
196 aa->aa_oa->o_valid |= OBD_MD_FLBLKSZ;
198 CERROR("can't unpack ost_body\n");
200 aa->aa_oa->o_valid = 0;
206 static int osc_getattr_async(struct obd_export *exp, struct obdo *oa,
207 struct lov_stripe_md *md,
208 struct ptlrpc_request_set *set)
210 struct ptlrpc_request *request;
211 struct ost_body *body;
212 int size = sizeof(*body);
213 struct osc_getattr_async_args *aa;
216 request = ptlrpc_prep_req(class_exp2cliimp(exp), OST_GETATTR, 1,
221 body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof (*body));
222 memcpy(&body->oa, oa, sizeof(*oa));
224 request->rq_replen = lustre_msg_size(1, &size);
225 request->rq_interpret_reply = osc_getattr_interpret;
227 LASSERT (sizeof (*aa) <= sizeof (request->rq_async_args));
228 aa = (struct osc_getattr_async_args *)&request->rq_async_args;
231 ptlrpc_set_add_req (set, request);
235 static int osc_getattr(struct obd_export *exp, struct obdo *oa,
236 struct lov_stripe_md *md)
238 struct ptlrpc_request *request;
239 struct ost_body *body;
240 int rc, size = sizeof(*body);
243 request = ptlrpc_prep_req(class_exp2cliimp(exp), OST_GETATTR, 1,
248 body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof (*body));
249 memcpy(&body->oa, oa, sizeof(*oa));
251 request->rq_replen = lustre_msg_size(1, &size);
253 rc = ptlrpc_queue_wait(request);
255 CERROR("%s failed: rc = %d\n", __FUNCTION__, rc);
259 body = lustre_swab_repbuf(request, 0, sizeof (*body),
260 lustre_swab_ost_body);
262 CERROR ("can't unpack ost_body\n");
263 GOTO (out, rc = -EPROTO);
266 CDEBUG(D_INODE, "mode: %o\n", body->oa.o_mode);
267 memcpy(oa, &body->oa, sizeof(*oa));
269 /* This should really be sent by the OST */
270 oa->o_blksize = PTLRPC_MAX_BRW_SIZE;
271 oa->o_valid |= OBD_MD_FLBLKSZ;
275 ptlrpc_req_finished(request);
279 static int osc_setattr(struct obd_export *exp, struct obdo *oa,
280 struct lov_stripe_md *md, struct obd_trans_info *oti)
282 struct ptlrpc_request *request;
283 struct ost_body *body;
284 int rc, size = sizeof(*body);
287 LASSERT(!(oa->o_valid & OBD_MD_FLGROUP) || oa->o_gr > 0);
289 request = ptlrpc_prep_req(class_exp2cliimp(exp), OST_SETATTR, 1, &size,
294 body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof(*body));
295 memcpy(&body->oa, oa, sizeof(*oa));
297 request->rq_replen = lustre_msg_size(1, &size);
299 rc = ptlrpc_queue_wait(request);
303 body = lustre_swab_repbuf(request, 0, sizeof(*body),
304 lustre_swab_ost_body);
306 GOTO(out, rc = -EPROTO);
308 memcpy(oa, &body->oa, sizeof(*oa));
312 ptlrpc_req_finished(request);
316 int osc_real_create(struct obd_export *exp, struct obdo *oa,
317 struct lov_stripe_md **ea, struct obd_trans_info *oti)
319 struct ptlrpc_request *request;
320 struct ost_body *body;
321 struct lov_stripe_md *lsm;
322 int rc, size = sizeof(*body);
330 rc = obd_alloc_memmd(exp, &lsm);
335 request = ptlrpc_prep_req(class_exp2cliimp(exp), OST_CREATE, 1, &size,
338 GOTO(out, rc = -ENOMEM);
340 body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof (*body));
341 memcpy(&body->oa, oa, sizeof(body->oa));
343 request->rq_replen = lustre_msg_size(1, &size);
344 if (oa->o_valid & OBD_MD_FLINLINE) {
345 LASSERT((oa->o_valid & OBD_MD_FLFLAGS) &&
346 oa->o_flags == OBD_FL_DELORPHAN);
347 DEBUG_REQ(D_HA, request,
348 "delorphan from OST integration");
351 rc = ptlrpc_queue_wait(request);
355 body = lustre_swab_repbuf(request, 0, sizeof(*body),
356 lustre_swab_ost_body);
358 CERROR ("can't unpack ost_body\n");
359 GOTO (out_req, rc = -EPROTO);
362 memcpy(oa, &body->oa, sizeof(*oa));
364 /* This should really be sent by the OST */
365 oa->o_blksize = PTLRPC_MAX_BRW_SIZE;
366 oa->o_valid |= OBD_MD_FLBLKSZ;
368 /* XXX LOV STACKING: the lsm that is passed to us from LOV does not
369 * have valid lsm_oinfo data structs, so don't go touching that.
370 * This needs to be fixed in a big way.
372 lsm->lsm_object_id = oa->o_id;
373 lsm->lsm_object_gr = oa->o_gr;
377 oti->oti_transno = request->rq_repmsg->transno;
379 if (oa->o_valid & OBD_MD_FLCOOKIE) {
380 if (!oti->oti_logcookies)
381 oti_alloc_cookies(oti, 1);
382 memcpy(oti->oti_logcookies, obdo_logcookie(oa),
383 sizeof(oti->oti_onecookie));
387 CDEBUG(D_HA, "transno: "LPD64"\n", request->rq_repmsg->transno);
390 ptlrpc_req_finished(request);
393 obd_free_memmd(exp, &lsm);
397 static int osc_punch(struct obd_export *exp, struct obdo *oa,
398 struct lov_stripe_md *md, obd_size start,
399 obd_size end, struct obd_trans_info *oti)
401 struct ptlrpc_request *request;
402 struct ost_body *body;
403 int rc, size = sizeof(*body);
411 request = ptlrpc_prep_req(class_exp2cliimp(exp), OST_PUNCH, 1, &size,
416 body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof (*body));
417 memcpy(&body->oa, oa, sizeof(*oa));
419 /* overload the size and blocks fields in the oa with start/end */
420 body->oa.o_size = start;
421 body->oa.o_blocks = end;
422 body->oa.o_valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS);
424 request->rq_replen = lustre_msg_size(1, &size);
426 rc = ptlrpc_queue_wait(request);
430 body = lustre_swab_repbuf (request, 0, sizeof (*body),
431 lustre_swab_ost_body);
433 CERROR ("can't unpack ost_body\n");
434 GOTO (out, rc = -EPROTO);
437 memcpy(oa, &body->oa, sizeof(*oa));
441 ptlrpc_req_finished(request);
445 static int osc_sync(struct obd_export *exp, struct obdo *oa,
446 struct lov_stripe_md *md, obd_size start, obd_size end)
448 struct ptlrpc_request *request;
449 struct ost_body *body;
450 int rc, size = sizeof(*body);
458 request = ptlrpc_prep_req(class_exp2cliimp(exp), OST_SYNC, 1, &size,
463 body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof (*body));
464 memcpy(&body->oa, oa, sizeof(*oa));
466 /* overload the size and blocks fields in the oa with start/end */
467 body->oa.o_size = start;
468 body->oa.o_blocks = end;
469 body->oa.o_valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS);
471 request->rq_replen = lustre_msg_size(1, &size);
473 rc = ptlrpc_queue_wait(request);
477 body = lustre_swab_repbuf(request, 0, sizeof(*body),
478 lustre_swab_ost_body);
480 CERROR ("can't unpack ost_body\n");
481 GOTO (out, rc = -EPROTO);
484 memcpy(oa, &body->oa, sizeof(*oa));
488 ptlrpc_req_finished(request);
492 static int osc_destroy(struct obd_export *exp, struct obdo *oa,
493 struct lov_stripe_md *ea, struct obd_trans_info *oti)
495 struct ptlrpc_request *request;
496 struct ost_body *body;
497 int rc, size = sizeof(*body);
505 request = ptlrpc_prep_req(class_exp2cliimp(exp), OST_DESTROY, 1,
510 body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof (*body));
512 if (oti != NULL && oa->o_valid & OBD_MD_FLCOOKIE) {
513 memcpy(obdo_logcookie(oa), oti->oti_logcookies,
514 sizeof(*oti->oti_logcookies));
515 oti->oti_logcookies++;
518 memcpy(&body->oa, oa, sizeof(*oa));
519 request->rq_replen = lustre_msg_size(1, &size);
521 rc = ptlrpc_queue_wait(request);
525 body = lustre_swab_repbuf(request, 0, sizeof(*body),
526 lustre_swab_ost_body);
528 CERROR ("Can't unpack body\n");
529 GOTO (out, rc = -EPROTO);
532 memcpy(oa, &body->oa, sizeof(*oa));
536 ptlrpc_req_finished(request);
540 static void osc_announce_cached(struct client_obd *cli, struct obdo *oa,
543 obd_flag bits = OBD_MD_FLBLOCKS|OBD_MD_FLGRANT;
545 LASSERT(!(oa->o_valid & bits));
548 spin_lock(&cli->cl_loi_list_lock);
549 oa->o_dirty = cli->cl_dirty;
550 oa->o_undirty = cli->cl_dirty_max - oa->o_dirty;
551 oa->o_grant = cli->cl_avail_grant;
552 oa->o_dropped = cli->cl_lost_grant;
553 cli->cl_lost_grant = 0;
554 spin_unlock(&cli->cl_loi_list_lock);
555 CDEBUG(D_CACHE,"dirty: "LPU64" undirty: %u dropped %u grant: "LPU64"\n",
556 oa->o_dirty, oa->o_undirty, oa->o_dropped, oa->o_grant);
559 /* caller must hold loi_list_lock */
560 static void osc_consume_write_grant(struct client_obd *cli,
561 struct osc_async_page *oap)
563 cli->cl_dirty += PAGE_SIZE;
564 cli->cl_avail_grant -= PAGE_SIZE;
565 oap->oap_brw_flags |= OBD_BRW_FROM_GRANT;
566 CDEBUG(D_CACHE, "using %lu grant credits for oap %p\n", PAGE_SIZE, oap);
567 LASSERT(cli->cl_avail_grant >= 0);
570 /* caller must hold loi_list_lock */
571 void osc_wake_cache_waiters(struct client_obd *cli)
573 struct list_head *l, *tmp;
574 struct osc_cache_waiter *ocw;
576 list_for_each_safe(l, tmp, &cli->cl_cache_waiters) {
577 /* if we can't dirty more, we must wait until some is written */
578 if (cli->cl_dirty + PAGE_SIZE > cli->cl_dirty_max) {
579 CDEBUG(D_CACHE, "no dirty room: dirty: %ld max %ld\n",
580 cli->cl_dirty, cli->cl_dirty_max);
584 /* if still dirty cache but no grant wait for pending RPCs that
585 * may yet return us some grant before doing sync writes */
586 if (cli->cl_brw_in_flight && cli->cl_avail_grant < PAGE_SIZE) {
587 CDEBUG(D_CACHE, "%d BRWs in flight, no grant\n",
588 cli->cl_brw_in_flight);
592 ocw = list_entry(l, struct osc_cache_waiter, ocw_entry);
593 list_del_init(&ocw->ocw_entry);
594 if (cli->cl_avail_grant < PAGE_SIZE) {
595 /* no more RPCs in flight to return grant, do sync IO */
596 ocw->ocw_rc = -EDQUOT;
597 CDEBUG(D_INODE, "wake oap %p for sync\n", ocw->ocw_oap);
599 osc_consume_write_grant(cli, ocw->ocw_oap);
602 wake_up(&ocw->ocw_waitq);
608 static void osc_update_grant(struct client_obd *cli, struct ost_body *body)
610 spin_lock(&cli->cl_loi_list_lock);
611 CDEBUG(D_CACHE, "got "LPU64" extra grant\n", body->oa.o_grant);
612 cli->cl_avail_grant += body->oa.o_grant;
613 /* waiters are woken in brw_interpret_oap */
614 spin_unlock(&cli->cl_loi_list_lock);
617 /* We assume that the reason this OSC got a short read is because it read
618 * beyond the end of a stripe file; i.e. lustre is reading a sparse file
619 * via the LOV, and it _knows_ it's reading inside the file, it's just that
620 * this stripe never got written at or beyond this stripe offset yet. */
621 static void handle_short_read(int nob_read, obd_count page_count,
622 struct brw_page *pga)
626 /* skip bytes read OK */
627 while (nob_read > 0) {
628 LASSERT (page_count > 0);
630 if (pga->count > nob_read) {
631 /* EOF inside this page */
632 ptr = kmap(pga->pg) + (pga->off & ~PAGE_MASK);
633 memset(ptr + nob_read, 0, pga->count - nob_read);
640 nob_read -= pga->count;
645 /* zero remaining pages */
646 while (page_count-- > 0) {
647 ptr = kmap(pga->pg) + (pga->off & ~PAGE_MASK);
648 memset(ptr, 0, pga->count);
654 static int check_write_rcs(struct ptlrpc_request *request,
655 int requested_nob, int niocount,
656 obd_count page_count, struct brw_page *pga)
660 /* return error if any niobuf was in error */
661 remote_rcs = lustre_swab_repbuf(request, 1,
662 sizeof(*remote_rcs) * niocount, NULL);
663 if (remote_rcs == NULL) {
664 CERROR("Missing/short RC vector on BRW_WRITE reply\n");
667 if (lustre_msg_swabbed(request->rq_repmsg))
668 for (i = 0; i < niocount; i++)
669 __swab32s(&remote_rcs[i]);
671 for (i = 0; i < niocount; i++) {
672 if (remote_rcs[i] < 0)
673 return(remote_rcs[i]);
675 if (remote_rcs[i] != 0) {
676 CERROR("rc[%d] invalid (%d) req %p\n",
677 i, remote_rcs[i], request);
682 if (request->rq_bulk->bd_nob_transferred != requested_nob) {
683 CERROR("Unexpected # bytes transferred: %d (requested %d)\n",
684 requested_nob, request->rq_bulk->bd_nob_transferred);
691 static inline int can_merge_pages(struct brw_page *p1, struct brw_page *p2)
693 if (p1->flag != p2->flag) {
694 unsigned mask = ~OBD_BRW_FROM_GRANT;
696 /* warn if we try to combine flags that we don't know to be
698 if ((p1->flag & mask) != (p2->flag & mask))
699 CERROR("is it ok to have flags 0x%x and 0x%x in the "
700 "same brw?\n", p1->flag, p2->flag);
704 return (p1->off + p1->count == p2->off);
708 static obd_count cksum_pages(int nob, obd_count page_count,
709 struct brw_page *pga)
715 LASSERT (page_count > 0);
718 ost_checksum(&cksum, ptr + (pga->off & (PAGE_SIZE - 1)),
719 pga->count > nob ? nob : pga->count);
731 static int osc_brw_prep_request(int cmd, struct obd_import *imp,struct obdo *oa,
732 struct lov_stripe_md *lsm, obd_count page_count,
733 struct brw_page *pga, int *requested_nobp,
734 int *niocountp, struct ptlrpc_request **reqp)
736 struct ptlrpc_request *req;
737 struct ptlrpc_bulk_desc *desc;
738 struct client_obd *cli = &imp->imp_obd->u.cli;
739 struct ost_body *body;
740 struct obd_ioobj *ioobj;
741 struct niobuf_remote *niobuf;
750 opc = ((cmd & OBD_BRW_WRITE) != 0) ? OST_WRITE : OST_READ;
752 for (niocount = i = 1; i < page_count; i++)
753 if (!can_merge_pages(&pga[i - 1], &pga[i]))
756 size[0] = sizeof(*body);
757 size[1] = sizeof(*ioobj);
758 size[2] = niocount * sizeof(*niobuf);
760 req = ptlrpc_prep_req(imp, opc, 3, size, NULL);
764 if (opc == OST_WRITE)
765 desc = ptlrpc_prep_bulk_imp (req, page_count,
766 BULK_GET_SOURCE, OST_BULK_PORTAL);
768 desc = ptlrpc_prep_bulk_imp (req, page_count,
769 BULK_PUT_SINK, OST_BULK_PORTAL);
771 GOTO(out, rc = -ENOMEM);
772 /* NB request now owns desc and will free it when it gets freed */
774 body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof(*body));
775 ioobj = lustre_msg_buf(req->rq_reqmsg, 1, sizeof(*ioobj));
776 niobuf = lustre_msg_buf(req->rq_reqmsg, 2, niocount * sizeof(*niobuf));
778 memcpy(&body->oa, oa, sizeof(*oa));
780 obdo_to_ioobj(oa, ioobj);
781 ioobj->ioo_bufcnt = niocount;
783 LASSERT (page_count > 0);
784 for (requested_nob = i = 0; i < page_count; i++, niobuf++) {
785 struct brw_page *pg = &pga[i];
786 struct brw_page *pg_prev = pg - 1;
788 LASSERT(pg->count > 0);
789 LASSERT((pg->off & ~PAGE_MASK) + pg->count <= PAGE_SIZE);
790 LASSERTF(i == 0 || pg->off > pg_prev->off,
791 "i %d p_c %u pg %p [pri %lu ind %lu] off "LPU64
792 " prev_pg %p [pri %lu ind %lu] off "LPU64"\n",
794 pg->pg, pg->pg->private, pg->pg->index, pg->off,
795 pg_prev->pg, pg_prev->pg->private, pg_prev->pg->index,
798 ptlrpc_prep_bulk_page(desc, pg->pg, pg->off & ~PAGE_MASK,
800 requested_nob += pg->count;
802 if (i > 0 && can_merge_pages(pg_prev, pg)) {
804 niobuf->len += pg->count;
806 niobuf->offset = pg->off;
807 niobuf->len = pg->count;
808 niobuf->flags = pg->flag;
812 LASSERT((void *)(niobuf - niocount) ==
813 lustre_msg_buf(req->rq_reqmsg, 2, niocount * sizeof(*niobuf)));
814 osc_announce_cached(cli, &body->oa, opc == OST_WRITE ? requested_nob:0);
815 spin_lock_irqsave(&req->rq_lock, flags);
816 req->rq_no_resend = 1;
817 spin_unlock_irqrestore(&req->rq_lock, flags);
819 /* size[0] still sizeof (*body) */
820 if (opc == OST_WRITE) {
822 body->oa.o_valid |= OBD_MD_FLCKSUM;
823 body->oa.o_cksum = cksum_pages(requested_nob, page_count, pga);
825 /* 1 RC per niobuf */
826 size[1] = sizeof(__u32) * niocount;
827 req->rq_replen = lustre_msg_size(2, size);
829 /* 1 RC for the whole I/O */
830 req->rq_replen = lustre_msg_size(1, size);
833 *niocountp = niocount;
834 *requested_nobp = requested_nob;
839 ptlrpc_req_finished (req);
843 static int osc_brw_fini_request(struct ptlrpc_request *req, struct obdo *oa,
844 int requested_nob, int niocount,
845 obd_count page_count, struct brw_page *pga,
848 struct client_obd *cli = &req->rq_import->imp_obd->u.cli;
849 struct ost_body *body;
855 body = lustre_swab_repbuf(req, 0, sizeof(*body), lustre_swab_ost_body);
857 CERROR ("Can't unpack body\n");
861 osc_update_grant(cli, body);
863 if (req->rq_reqmsg->opc == OST_WRITE) {
865 CERROR ("Unexpected +ve rc %d\n", rc);
868 LASSERT (req->rq_bulk->bd_nob == requested_nob);
870 RETURN(check_write_rcs(req, requested_nob, niocount,
874 if (rc > requested_nob) {
875 CERROR("Unexpected rc %d (%d requested)\n", rc, requested_nob);
879 if (rc != req->rq_bulk->bd_nob_transferred) {
880 CERROR ("Unexpected rc %d (%d transferred)\n",
881 rc, req->rq_bulk->bd_nob_transferred);
885 if (rc < requested_nob)
886 handle_short_read(rc, page_count, pga);
888 memcpy(oa, &body->oa, sizeof(*oa));
891 if (oa->o_valid & OBD_MD_FLCKSUM) {
892 const struct ptlrpc_peer *peer =
893 &req->rq_import->imp_connection->c_peer;
894 static int cksum_counter;
895 obd_count server_cksum = oa->o_cksum;
896 obd_count cksum = cksum_pages(rc, page_count, pga);
897 char str[PTL_NALFMT_SIZE];
899 portals_nid2str(peer->peer_ni->pni_number, peer->peer_nid, str);
902 if (server_cksum != cksum) {
903 CERROR("Bad checksum: server %x, client %x, server NID "
904 LPX64" (%s)\n", server_cksum, cksum,
905 peer->peer_nid, str);
908 } else if ((cksum_counter & (-cksum_counter)) == cksum_counter){
909 CWARN("Checksum %u from "LPX64" (%s) OK: %x\n",
910 cksum_counter, peer->peer_nid, str, cksum);
913 static int cksum_missed;
916 if ((cksum_missed & (-cksum_missed)) == cksum_missed)
917 CERROR("Request checksum %u from "LPX64", no reply\n",
919 req->rq_import->imp_connection->c_peer.peer_nid);
925 static int osc_brw_internal(int cmd, struct obd_export *exp,struct obdo *oa,
926 struct lov_stripe_md *lsm,
927 obd_count page_count, struct brw_page *pga)
931 struct ptlrpc_request *request;
936 rc = osc_brw_prep_request(cmd, class_exp2cliimp(exp), oa, lsm,
937 page_count, pga, &requested_nob, &niocount,
939 /* NB ^ sets rq_no_resend */
944 rc = ptlrpc_queue_wait(request);
946 if (rc == -ETIMEDOUT && request->rq_resend) {
947 DEBUG_REQ(D_HA, request, "BULK TIMEOUT");
948 ptlrpc_req_finished(request);
952 rc = osc_brw_fini_request(request, oa, requested_nob, niocount,
953 page_count, pga, rc);
955 ptlrpc_req_finished(request);
959 static int brw_interpret(struct ptlrpc_request *request,
960 struct osc_brw_async_args *aa, int rc)
962 struct obdo *oa = aa->aa_oa;
963 int requested_nob = aa->aa_requested_nob;
964 int niocount = aa->aa_nio_count;
965 obd_count page_count = aa->aa_page_count;
966 struct brw_page *pga = aa->aa_pga;
969 /* XXX bug 937 here */
970 if (rc == -ETIMEDOUT && request->rq_resend) {
971 DEBUG_REQ(D_HA, request, "BULK TIMEOUT");
972 LBUG(); /* re-send. later. */
976 rc = osc_brw_fini_request(request, oa, requested_nob, niocount,
977 page_count, pga, rc);
981 static int async_internal(int cmd, struct obd_export *exp, struct obdo *oa,
982 struct lov_stripe_md *lsm, obd_count page_count,
983 struct brw_page *pga, struct ptlrpc_request_set *set)
985 struct ptlrpc_request *request;
988 struct osc_brw_async_args *aa;
992 rc = osc_brw_prep_request(cmd, class_exp2cliimp(exp), oa, lsm,
993 page_count, pga, &requested_nob, &nio_count,
995 /* NB ^ sets rq_no_resend */
998 LASSERT(sizeof(*aa) <= sizeof(request->rq_async_args));
999 aa = (struct osc_brw_async_args *)&request->rq_async_args;
1001 aa->aa_requested_nob = requested_nob;
1002 aa->aa_nio_count = nio_count;
1003 aa->aa_page_count = page_count;
1006 request->rq_interpret_reply = brw_interpret;
1007 ptlrpc_set_add_req(set, request);
1013 #define min_t(type,x,y) \
1014 ({ type __x = (x); type __y = (y); __x < __y ? __x: __y; })
1018 * ugh, we want disk allocation on the target to happen in offset order. we'll
1019 * follow sedgewicks advice and stick to the dead simple shellsort -- it'll do
1020 * fine for our small page arrays and doesn't require allocation. its an
1021 * insertion sort that swaps elements that are strides apart, shrinking the
1022 * stride down until its '1' and the array is sorted.
1024 static void sort_brw_pages(struct brw_page *array, int num)
1027 struct brw_page tmp;
1031 for (stride = 1; stride < num ; stride = (stride * 3) + 1)
1036 for (i = stride ; i < num ; i++) {
1039 while (j >= stride && array[j - stride].off > tmp.off) {
1040 array[j] = array[j - stride];
1045 } while (stride > 1);
1048 /* make sure we the regions we're passing to elan don't violate its '4
1049 * fragments' constraint. portal headers are a fragment, all full
1050 * PAGE_SIZE long pages count as 1 fragment, and each partial page
1051 * counts as a fragment. I think. see bug 934. */
1052 static obd_count check_elan_limit(struct brw_page *pg, obd_count pages)
1055 int saw_whole_frag = 0;
1058 for (i = 0 ; frags_left && i < pages ; pg++, i++) {
1059 if (pg->count == PAGE_SIZE) {
1060 if (!saw_whole_frag) {
1071 static int osc_brw(int cmd, struct obd_export *exp, struct obdo *oa,
1072 struct lov_stripe_md *md, obd_count page_count,
1073 struct brw_page *pga, struct obd_trans_info *oti)
1077 if (cmd == OBD_BRW_CHECK) {
1078 /* The caller just wants to know if there's a chance that this
1079 * I/O can succeed */
1080 struct obd_import *imp = class_exp2cliimp(exp);
1082 if (imp == NULL || imp->imp_invalid)
1087 while (page_count) {
1088 obd_count pages_per_brw;
1091 if (page_count > PTLRPC_MAX_BRW_PAGES)
1092 pages_per_brw = PTLRPC_MAX_BRW_PAGES;
1094 pages_per_brw = page_count;
1096 sort_brw_pages(pga, pages_per_brw);
1097 pages_per_brw = check_elan_limit(pga, pages_per_brw);
1099 rc = osc_brw_internal(cmd, exp, oa, md, pages_per_brw, pga);
1104 page_count -= pages_per_brw;
1105 pga += pages_per_brw;
1110 static int osc_brw_async(int cmd, struct obd_export *exp, struct obdo *oa,
1111 struct lov_stripe_md *md, obd_count page_count,
1112 struct brw_page *pga, struct ptlrpc_request_set *set,
1113 struct obd_trans_info *oti)
1117 if (cmd == OBD_BRW_CHECK) {
1118 /* The caller just wants to know if there's a chance that this
1119 * I/O can succeed */
1120 struct obd_import *imp = class_exp2cliimp(exp);
1122 if (imp == NULL || imp->imp_invalid)
1127 while (page_count) {
1128 obd_count pages_per_brw;
1131 if (page_count > PTLRPC_MAX_BRW_PAGES)
1132 pages_per_brw = PTLRPC_MAX_BRW_PAGES;
1134 pages_per_brw = page_count;
1136 sort_brw_pages(pga, pages_per_brw);
1137 pages_per_brw = check_elan_limit(pga, pages_per_brw);
1139 rc = async_internal(cmd, exp, oa, md, pages_per_brw, pga, set);
1144 page_count -= pages_per_brw;
1145 pga += pages_per_brw;
1150 static void osc_check_rpcs(struct client_obd *cli);
1151 static void osc_exit_cache(struct client_obd *cli, struct osc_async_page *oap,
1153 static void loi_list_maint(struct client_obd *cli, struct lov_oinfo *loi);
1154 static void lop_update_pending(struct client_obd *cli,
1155 struct loi_oap_pages *lop, int cmd, int delta);
1157 /* this is called when a sync waiter receives an interruption. Its job is to
1158 * get the caller woken as soon as possible. If its page hasn't been put in an
1159 * rpc yet it can dequeue immediately. Otherwise it has to mark the rpc as
1160 * desiring interruption which will forcefully complete the rpc once the rpc
1162 static void osc_occ_interrupted(struct oig_callback_context *occ)
1164 struct osc_async_page *oap;
1165 struct loi_oap_pages *lop;
1166 struct lov_oinfo *loi;
1169 /* XXX member_of() */
1170 oap = list_entry(occ, struct osc_async_page, oap_occ);
1172 spin_lock(&oap->oap_cli->cl_loi_list_lock);
1174 oap->oap_interrupted = 1;
1176 /* ok, it's been put in an rpc. */
1177 if (oap->oap_request != NULL) {
1178 ptlrpc_mark_interrupted(oap->oap_request);
1179 ptlrpcd_wake(oap->oap_request);
1183 /* we don't get interruption callbacks until osc_trigger_sync_io()
1184 * has been called and put the sync oaps in the pending/urgent lists.*/
1185 if (!list_empty(&oap->oap_pending_item)) {
1186 list_del_init(&oap->oap_pending_item);
1187 if (oap->oap_async_flags & ASYNC_URGENT)
1188 list_del_init(&oap->oap_urgent_item);
1191 lop = (oap->oap_cmd == OBD_BRW_WRITE) ?
1192 &loi->loi_write_lop : &loi->loi_read_lop;
1193 lop_update_pending(oap->oap_cli, lop, oap->oap_cmd, -1);
1194 loi_list_maint(oap->oap_cli, oap->oap_loi);
1196 oig_complete_one(oap->oap_oig, &oap->oap_occ, 0);
1197 oap->oap_oig = NULL;
1201 spin_unlock(&oap->oap_cli->cl_loi_list_lock);
1204 /* this must be called holding the loi list lock to give coverage to exit_cache,
1205 * async_flag maintenance, and oap_request */
1206 static void osc_complete_oap(struct client_obd *cli,
1207 struct osc_async_page *oap, int sent, int rc)
1209 osc_exit_cache(cli, oap, sent);
1210 oap->oap_async_flags = 0;
1211 oap->oap_interrupted = 0;
1213 if (oap->oap_request != NULL) {
1214 ptlrpc_req_finished(oap->oap_request);
1215 oap->oap_request = NULL;
1219 oig_complete_one(oap->oap_oig, &oap->oap_occ, rc);
1220 oap->oap_oig = NULL;
1225 oap->oap_caller_ops->ap_completion(oap->oap_caller_data, oap->oap_cmd,
1229 static int brw_interpret_oap(struct ptlrpc_request *request,
1230 struct osc_brw_async_args *aa, int rc)
1232 struct osc_async_page *oap;
1233 struct client_obd *cli;
1234 struct list_head *pos, *n;
1238 rc = osc_brw_fini_request(request, aa->aa_oa, aa->aa_requested_nob,
1239 aa->aa_nio_count, aa->aa_page_count,
1242 CDEBUG(D_INODE, "request %p aa %p rc %d\n", request, aa, rc);
1245 /* in failout recovery we ignore writeback failure and want
1246 * to just tell llite to unlock the page and continue */
1247 if (request->rq_reqmsg->opc == OST_WRITE &&
1248 (cli->cl_import == NULL || cli->cl_import->imp_invalid)) {
1249 CDEBUG(D_INODE, "flipping to rc 0 imp %p inv %d\n",
1251 cli->cl_import ? cli->cl_import->imp_invalid : -1);
1255 spin_lock(&cli->cl_loi_list_lock);
1257 /* We need to decrement before osc_complete_oap->osc_wake_cache_waiters
1258 * is called so we know whether to go to sync BRWs or wait for more
1259 * RPCs to complete */
1260 cli->cl_brw_in_flight--;
1262 /* the caller may re-use the oap after the completion call so
1263 * we need to clean it up a little */
1264 list_for_each_safe(pos, n, &aa->aa_oaps) {
1265 oap = list_entry(pos, struct osc_async_page, oap_rpc_item);
1267 //CDEBUG(D_INODE, "page %p index %lu oap %p\n",
1268 //oap->oap_page, oap->oap_page->index, oap);
1270 list_del_init(&oap->oap_rpc_item);
1271 osc_complete_oap(cli, oap, 1, rc);
1274 osc_wake_cache_waiters(cli);
1275 osc_check_rpcs(cli);
1277 spin_unlock(&cli->cl_loi_list_lock);
1279 obdo_free(aa->aa_oa);
1280 OBD_FREE(aa->aa_pga, aa->aa_page_count * sizeof(struct brw_page));
1285 static struct ptlrpc_request *osc_build_req(struct client_obd *cli,
1286 struct list_head *rpc_list,
1287 int page_count, int cmd)
1289 struct ptlrpc_request *req;
1290 struct brw_page *pga = NULL;
1291 int requested_nob, nio_count;
1292 struct osc_brw_async_args *aa;
1293 struct obdo *oa = NULL;
1294 struct obd_async_page_ops *ops = NULL;
1295 void *caller_data = NULL;
1296 struct list_head *pos;
1299 LASSERT(!list_empty(rpc_list));
1301 OBD_ALLOC(pga, sizeof(*pga) * page_count);
1303 RETURN(ERR_PTR(-ENOMEM));
1307 GOTO(out, req = ERR_PTR(-ENOMEM));
1310 list_for_each(pos, rpc_list) {
1311 struct osc_async_page *oap;
1313 oap = list_entry(pos, struct osc_async_page, oap_rpc_item);
1315 ops = oap->oap_caller_ops;
1316 caller_data = oap->oap_caller_data;
1318 pga[i].off = oap->oap_obj_off + oap->oap_page_off;
1319 pga[i].pg = oap->oap_page;
1320 pga[i].count = oap->oap_count;
1321 pga[i].flag = oap->oap_brw_flags;
1322 CDEBUG(0, "put page %p index %lu oap %p flg %x to pga\n",
1323 pga[i].pg, oap->oap_page->index, oap, pga[i].flag);
1327 /* always get the data for the obdo for the rpc */
1328 LASSERT(ops != NULL);
1329 ops->ap_fill_obdo(caller_data, cmd, oa);
1331 sort_brw_pages(pga, page_count);
1332 rc = osc_brw_prep_request(cmd, cli->cl_import, oa, NULL, page_count,
1333 pga, &requested_nob, &nio_count, &req);
1335 CERROR("prep_req failed: %d\n", rc);
1336 GOTO(out, req = ERR_PTR(rc));
1339 LASSERT(sizeof(*aa) <= sizeof(req->rq_async_args));
1340 aa = (struct osc_brw_async_args *)&req->rq_async_args;
1342 aa->aa_requested_nob = requested_nob;
1343 aa->aa_nio_count = nio_count;
1344 aa->aa_page_count = page_count;
1353 OBD_FREE(pga, sizeof(*pga) * page_count);
1358 static void lop_update_pending(struct client_obd *cli,
1359 struct loi_oap_pages *lop, int cmd, int delta)
1361 lop->lop_num_pending += delta;
1362 if (cmd == OBD_BRW_WRITE)
1363 cli->cl_pending_w_pages += delta;
1365 cli->cl_pending_r_pages += delta;
1368 /* the loi lock is held across this function but it's allowed to release
1369 * and reacquire it during its work */
1370 static int osc_send_oap_rpc(struct client_obd *cli, struct lov_oinfo *loi,
1371 int cmd, struct loi_oap_pages *lop)
1373 struct ptlrpc_request *request;
1374 obd_count page_count = 0;
1375 struct list_head *tmp, *pos;
1376 struct osc_async_page *oap = NULL;
1377 struct osc_brw_async_args *aa;
1378 struct obd_async_page_ops *ops;
1379 LIST_HEAD(rpc_list);
1382 /* first we find the pages we're allowed to work with */
1383 list_for_each_safe(pos, tmp, &lop->lop_pending) {
1384 oap = list_entry(pos, struct osc_async_page, oap_pending_item);
1385 ops = oap->oap_caller_ops;
1387 LASSERT(oap->oap_magic == OAP_MAGIC);
1389 /* in llite being 'ready' equates to the page being locked
1390 * until completion unlocks it. commit_write submits a page
1391 * as not ready because its unlock will happen unconditionally
1392 * as the call returns. if we race with commit_write giving
1393 * us that page we dont' want to create a hole in the page
1394 * stream, so we stop and leave the rpc to be fired by
1395 * another dirtier or kupdated interval (the not ready page
1396 * will still be on the dirty list). we could call in
1397 * at the end of ll_file_write to process the queue again. */
1398 if (!(oap->oap_async_flags & ASYNC_READY)) {
1399 int rc = ops->ap_make_ready(oap->oap_caller_data, cmd);
1401 CDEBUG(D_INODE, "oap %p page %p returned %d "
1402 "instead of ready\n", oap,
1406 /* llite is telling us that the page is still
1407 * in commit_write and that we should try
1408 * and put it in an rpc again later. we
1409 * break out of the loop so we don't create
1410 * a hole in the sequence of pages in the rpc
1415 /* the io isn't needed.. tell the checks
1416 * below to complete the rpc with EINTR */
1417 oap->oap_async_flags |= ASYNC_COUNT_STABLE;
1418 oap->oap_count = -EINTR;
1421 oap->oap_async_flags |= ASYNC_READY;
1424 LASSERTF(0, "oap %p page %p returned %d "
1425 "from make_ready\n", oap,
1433 /* take the page out of our book-keeping */
1434 list_del_init(&oap->oap_pending_item);
1435 lop_update_pending(cli, lop, cmd, -1);
1436 if (!list_empty(&oap->oap_urgent_item))
1437 list_del_init(&oap->oap_urgent_item);
1439 /* ask the caller for the size of the io as the rpc leaves. */
1440 if (!(oap->oap_async_flags & ASYNC_COUNT_STABLE))
1442 ops->ap_refresh_count(oap->oap_caller_data,cmd);
1443 if (oap->oap_count <= 0) {
1444 CDEBUG(D_CACHE, "oap %p count %d, completing\n", oap,
1446 osc_complete_oap(cli, oap, 0, oap->oap_count);
1450 /* now put the page back in our accounting */
1451 list_add_tail(&oap->oap_rpc_item, &rpc_list);
1452 if (++page_count >= cli->cl_max_pages_per_rpc)
1456 osc_wake_cache_waiters(cli);
1458 if (page_count == 0)
1461 loi_list_maint(cli, loi);
1462 spin_unlock(&cli->cl_loi_list_lock);
1464 request = osc_build_req(cli, &rpc_list, page_count, cmd);
1465 if (IS_ERR(request)) {
1466 /* this should happen rarely and is pretty bad, it makes the
1467 * pending list not follow the dirty order */
1468 spin_lock(&cli->cl_loi_list_lock);
1469 list_for_each_safe(pos, tmp, &rpc_list) {
1470 oap = list_entry(pos, struct osc_async_page,
1472 list_del_init(&oap->oap_rpc_item);
1474 /* queued sync pages can be torn down while the pages
1475 * were between the pending list and the rpc */
1476 if (oap->oap_interrupted) {
1477 CDEBUG(D_INODE, "oap %p interrupted\n", oap);
1478 osc_complete_oap(cli, oap, 0, oap->oap_count);
1482 /* put the page back in the loi/lop lists */
1483 list_add_tail(&oap->oap_pending_item,
1485 lop_update_pending(cli, lop, cmd, 1);
1486 if (oap->oap_async_flags & ASYNC_URGENT)
1487 list_add(&oap->oap_urgent_item,
1490 loi_list_maint(cli, loi);
1491 RETURN(PTR_ERR(request));
1494 LASSERT(sizeof(*aa) <= sizeof(request->rq_async_args));
1495 aa = (struct osc_brw_async_args *)&request->rq_async_args;
1496 INIT_LIST_HEAD(&aa->aa_oaps);
1497 list_splice(&rpc_list, &aa->aa_oaps);
1498 INIT_LIST_HEAD(&rpc_list);
1501 if (cmd == OBD_BRW_READ) {
1502 lprocfs_oh_tally_log2(&cli->cl_read_page_hist, page_count);
1503 lprocfs_oh_tally(&cli->cl_read_rpc_hist, cli->cl_brw_in_flight);
1505 lprocfs_oh_tally_log2(&cli->cl_write_page_hist, page_count);
1506 lprocfs_oh_tally(&cli->cl_write_rpc_hist,
1507 cli->cl_brw_in_flight);
1511 spin_lock(&cli->cl_loi_list_lock);
1513 cli->cl_brw_in_flight++;
1514 /* queued sync pages can be torn down while the pages
1515 * were between the pending list and the rpc */
1516 list_for_each(pos, &aa->aa_oaps) {
1517 oap = list_entry(pos, struct osc_async_page, oap_rpc_item);
1518 if (oap->oap_interrupted) {
1519 CDEBUG(D_INODE, "oap %p in req %p interrupted\n",
1521 ptlrpc_mark_interrupted(request);
1526 CDEBUG(D_INODE, "req %p: %d pages, aa %p. now %d in flight\n", request,
1527 page_count, aa, cli->cl_brw_in_flight);
1529 oap->oap_request = ptlrpc_request_addref(request);
1530 request->rq_interpret_reply = brw_interpret_oap;
1531 ptlrpcd_add_req(request);
1535 static int lop_makes_rpc(struct client_obd *cli, struct loi_oap_pages *lop,
1541 if (lop->lop_num_pending == 0)
1544 /* if we have an invalid import we want to drain the queued pages
1545 * by forcing them through rpcs that immediately fail and complete
1546 * the pages. recovery relies on this to empty the queued pages
1547 * before canceling the locks and evicting down the llite pages */
1548 if (cli->cl_import == NULL || cli->cl_import->imp_invalid)
1551 /* stream rpcs in queue order as long as as there is an urgent page
1552 * queued. this is our cheap solution for good batching in the case
1553 * where writepage marks some random page in the middle of the file as
1554 * urgent because of, say, memory pressure */
1555 if (!list_empty(&lop->lop_urgent))
1558 /* fire off rpcs when we have 'optimal' rpcs as tuned for the wire. */
1559 optimal = cli->cl_max_pages_per_rpc;
1560 if (cmd == OBD_BRW_WRITE) {
1561 /* trigger a write rpc stream as long as there are dirtiers
1562 * waiting for space. as they're waiting, they're not going to
1563 * create more pages to coallesce with what's waiting.. */
1564 if (!list_empty(&cli->cl_cache_waiters))
1567 /* *2 to avoid triggering rpcs that would want to include pages
1568 * that are being queued but which can't be made ready until
1569 * the queuer finishes with the page. this is a wart for
1570 * llite::commit_write() */
1573 if (lop->lop_num_pending >= optimal)
1579 static void on_list(struct list_head *item, struct list_head *list,
1582 if (list_empty(item) && should_be_on)
1583 list_add_tail(item, list);
1584 else if (!list_empty(item) && !should_be_on)
1585 list_del_init(item);
1588 /* maintain the loi's cli list membership invariants so that osc_send_oap_rpc
1589 * can find pages to build into rpcs quickly */
1590 static void loi_list_maint(struct client_obd *cli, struct lov_oinfo *loi)
1592 on_list(&loi->loi_cli_item, &cli->cl_loi_ready_list,
1593 lop_makes_rpc(cli, &loi->loi_write_lop, OBD_BRW_WRITE) ||
1594 lop_makes_rpc(cli, &loi->loi_read_lop, OBD_BRW_READ));
1596 on_list(&loi->loi_write_item, &cli->cl_loi_write_list,
1597 loi->loi_write_lop.lop_num_pending);
1599 on_list(&loi->loi_read_item, &cli->cl_loi_read_list,
1600 loi->loi_read_lop.lop_num_pending);
1603 #define LOI_DEBUG(LOI, STR, args...) \
1604 CDEBUG(D_INODE, "loi ready %d wr %d:%d rd %d:%d " STR, \
1605 !list_empty(&(LOI)->loi_cli_item), \
1606 (LOI)->loi_write_lop.lop_num_pending, \
1607 !list_empty(&(LOI)->loi_write_lop.lop_urgent), \
1608 (LOI)->loi_read_lop.lop_num_pending, \
1609 !list_empty(&(LOI)->loi_read_lop.lop_urgent), \
1612 struct lov_oinfo *osc_next_loi(struct client_obd *cli)
1615 /* first return all objects which we already know to have
1616 * pages ready to be stuffed into rpcs */
1617 if (!list_empty(&cli->cl_loi_ready_list))
1618 RETURN(list_entry(cli->cl_loi_ready_list.next,
1619 struct lov_oinfo, loi_cli_item));
1621 /* then if we have cache waiters, return all objects with queued
1622 * writes. This is especially important when many small files
1623 * have filled up the cache and not been fired into rpcs because
1624 * they don't pass the nr_pending/object threshhold */
1625 if (!list_empty(&cli->cl_cache_waiters) &&
1626 !list_empty(&cli->cl_loi_write_list))
1627 RETURN(list_entry(cli->cl_loi_write_list.next,
1628 struct lov_oinfo, loi_write_item));
1630 /* then return all queued objects when we have an invalid import
1631 * so that they get flushed */
1632 if (cli->cl_import == NULL || cli->cl_import->imp_invalid) {
1633 if (!list_empty(&cli->cl_loi_write_list))
1634 RETURN(list_entry(cli->cl_loi_write_list.next,
1635 struct lov_oinfo, loi_write_item));
1636 if (!list_empty(&cli->cl_loi_read_list))
1637 RETURN(list_entry(cli->cl_loi_read_list.next,
1638 struct lov_oinfo, loi_read_item));
1643 /* called with the loi list lock held */
1644 static void osc_check_rpcs(struct client_obd *cli)
1646 struct lov_oinfo *loi;
1647 int rc = 0, race_counter = 0;
1650 while ((loi = osc_next_loi(cli)) != NULL) {
1651 LOI_DEBUG(loi, "%d in flight\n", cli->cl_brw_in_flight);
1653 if (cli->cl_brw_in_flight >= cli->cl_max_rpcs_in_flight)
1656 /* attempt some read/write balancing by alternating between
1657 * reads and writes in an object. The makes_rpc checks here
1658 * would be redundant if we were getting read/write work items
1659 * instead of objects. we don't want send_oap_rpc to drain a
1660 * partial read pending queue when we're given this object to
1661 * do io on writes while there are cache waiters */
1662 if (lop_makes_rpc(cli, &loi->loi_write_lop, OBD_BRW_WRITE)) {
1663 rc = osc_send_oap_rpc(cli, loi, OBD_BRW_WRITE,
1664 &loi->loi_write_lop);
1672 if (lop_makes_rpc(cli, &loi->loi_read_lop, OBD_BRW_READ)) {
1673 rc = osc_send_oap_rpc(cli, loi, OBD_BRW_READ,
1674 &loi->loi_read_lop);
1683 /* attempt some inter-object balancing by issueing rpcs
1684 * for each object in turn */
1685 if (!list_empty(&loi->loi_cli_item))
1686 list_del_init(&loi->loi_cli_item);
1687 if (!list_empty(&loi->loi_write_item))
1688 list_del_init(&loi->loi_write_item);
1689 if (!list_empty(&loi->loi_read_item))
1690 list_del_init(&loi->loi_read_item);
1692 loi_list_maint(cli, loi);
1694 /* send_oap_rpc fails with 0 when make_ready tells it to
1695 * back off. llite's make_ready does this when it tries
1696 * to lock a page queued for write that is already locked.
1697 * we want to try sending rpcs from many objects, but we
1698 * don't want to spin failing with 0. */
1699 if (race_counter == 10)
1705 /* we're trying to queue a page in the osc so we're subject to the
1706 * 'cl_dirty_max' limit on the number of pages that can be queued in the osc.
1707 * If the osc's queued pages are already at that limit, then we want to sleep
1708 * until there is space in the osc's queue for us. We also may be waiting for
1709 * write credits from the OST if there are RPCs in flight that may return some
1710 * before we fall back to sync writes.
1712 * We need this know our allocation was granted in the presence of signals */
1713 static int ocw_granted(struct client_obd *cli, struct osc_cache_waiter *ocw)
1717 spin_lock(&cli->cl_loi_list_lock);
1718 rc = list_empty(&ocw->ocw_entry) || cli->cl_brw_in_flight == 0;
1719 spin_unlock(&cli->cl_loi_list_lock);
1723 /* Caller must hold loi_list_lock - we drop/regain it if we need to wait for
1724 * grant or cache space. */
1725 static int osc_enter_cache(struct client_obd *cli, struct lov_oinfo *loi,
1726 struct osc_async_page *oap)
1728 struct osc_cache_waiter ocw;
1729 struct l_wait_info lwi = { 0 };
1731 CDEBUG(D_CACHE, "dirty: %ld dirty_max: %ld dropped: %lu grant: %lu\n",
1732 cli->cl_dirty, cli->cl_dirty_max, cli->cl_lost_grant,
1733 cli->cl_avail_grant);
1735 if (cli->cl_dirty_max < PAGE_SIZE)
1738 /* Hopefully normal case - cache space and write credits available */
1739 if (cli->cl_dirty + PAGE_SIZE <= cli->cl_dirty_max &&
1740 cli->cl_avail_grant >= PAGE_SIZE) {
1741 /* account for ourselves */
1742 osc_consume_write_grant(cli, oap);
1746 /* Make sure that there are write rpcs in flight to wait for. This
1747 * is a little silly as this object may not have any pending but
1748 * other objects sure might. */
1749 if (cli->cl_brw_in_flight) {
1750 list_add_tail(&ocw.ocw_entry, &cli->cl_cache_waiters);
1751 init_waitqueue_head(&ocw.ocw_waitq);
1755 loi_list_maint(cli, loi);
1756 osc_check_rpcs(cli);
1757 spin_unlock(&cli->cl_loi_list_lock);
1759 CDEBUG(0, "sleeping for cache space\n");
1760 l_wait_event(ocw.ocw_waitq, ocw_granted(cli, &ocw), &lwi);
1762 spin_lock(&cli->cl_loi_list_lock);
1763 if (!list_empty(&ocw.ocw_entry)) {
1764 list_del(&ocw.ocw_entry);
1773 /* the companion to enter_cache, called when an oap is no longer part of the
1774 * dirty accounting.. so writeback completes or truncate happens before writing
1775 * starts. must be called with the loi lock held. */
1776 static void osc_exit_cache(struct client_obd *cli, struct osc_async_page *oap,
1781 if (!(oap->oap_brw_flags & OBD_BRW_FROM_GRANT)) {
1786 oap->oap_brw_flags &= ~OBD_BRW_FROM_GRANT;
1787 cli->cl_dirty -= PAGE_SIZE;
1789 cli->cl_lost_grant += PAGE_SIZE;
1790 CDEBUG(D_CACHE, "lost grant: %lu avail grant: %lu dirty: %lu\n",
1791 cli->cl_lost_grant, cli->cl_avail_grant, cli->cl_dirty);
1797 int osc_prep_async_page(struct obd_export *exp, struct lov_stripe_md *lsm,
1798 struct lov_oinfo *loi, struct page *page,
1799 obd_off offset, struct obd_async_page_ops *ops,
1800 void *data, void **res)
1802 struct osc_async_page *oap;
1805 OBD_ALLOC(oap, sizeof(*oap));
1809 oap->oap_magic = OAP_MAGIC;
1810 oap->oap_cli = &exp->exp_obd->u.cli;
1813 oap->oap_caller_ops = ops;
1814 oap->oap_caller_data = data;
1816 oap->oap_page = page;
1817 oap->oap_obj_off = offset;
1819 INIT_LIST_HEAD(&oap->oap_pending_item);
1820 INIT_LIST_HEAD(&oap->oap_urgent_item);
1821 INIT_LIST_HEAD(&oap->oap_rpc_item);
1823 oap->oap_occ.occ_interrupted = osc_occ_interrupted;
1825 CDEBUG(D_CACHE, "oap %p page %p obj off "LPU64"\n", oap, page, offset);
1830 struct osc_async_page *oap_from_cookie(void *cookie)
1832 struct osc_async_page *oap = cookie;
1833 if (oap->oap_magic != OAP_MAGIC)
1834 return ERR_PTR(-EINVAL);
1838 static int osc_queue_async_io(struct obd_export *exp, struct lov_stripe_md *lsm,
1839 struct lov_oinfo *loi, void *cookie,
1840 int cmd, obd_off off, int count,
1841 obd_flag brw_flags, enum async_flags async_flags)
1843 struct client_obd *cli = &exp->exp_obd->u.cli;
1844 struct osc_async_page *oap;
1845 struct loi_oap_pages *lop;
1849 oap = oap_from_cookie(cookie);
1851 RETURN(PTR_ERR(oap));
1853 if (cli->cl_import == NULL || cli->cl_import->imp_invalid)
1856 if (!list_empty(&oap->oap_pending_item) ||
1857 !list_empty(&oap->oap_urgent_item) ||
1858 !list_empty(&oap->oap_rpc_item))
1862 loi = &lsm->lsm_oinfo[0];
1864 spin_lock(&cli->cl_loi_list_lock);
1867 oap->oap_async_flags = async_flags;
1868 oap->oap_page_off = off;
1869 oap->oap_count = count;
1870 oap->oap_brw_flags = brw_flags;
1872 if (cmd == OBD_BRW_WRITE) {
1873 rc = osc_enter_cache(cli, loi, oap);
1875 spin_unlock(&cli->cl_loi_list_lock);
1878 lop = &loi->loi_write_lop;
1880 lop = &loi->loi_read_lop;
1883 if (oap->oap_async_flags & ASYNC_URGENT)
1884 list_add(&oap->oap_urgent_item, &lop->lop_urgent);
1885 list_add_tail(&oap->oap_pending_item, &lop->lop_pending);
1886 lop_update_pending(cli, lop, cmd, 1);
1888 loi_list_maint(cli, loi);
1890 LOI_DEBUG(loi, "oap %p page %p added for cmd %d\n", oap, oap->oap_page,
1893 osc_check_rpcs(cli);
1894 spin_unlock(&cli->cl_loi_list_lock);
1899 /* aka (~was & now & flag), but this is more clear :) */
1900 #define SETTING(was, now, flag) (!(was & flag) && (now & flag))
1902 static int osc_set_async_flags(struct obd_export *exp,
1903 struct lov_stripe_md *lsm,
1904 struct lov_oinfo *loi, void *cookie,
1905 obd_flag async_flags)
1907 struct client_obd *cli = &exp->exp_obd->u.cli;
1908 struct loi_oap_pages *lop;
1909 struct osc_async_page *oap;
1913 oap = oap_from_cookie(cookie);
1915 RETURN(PTR_ERR(oap));
1917 if (cli->cl_import == NULL || cli->cl_import->imp_invalid)
1921 loi = &lsm->lsm_oinfo[0];
1923 if (oap->oap_cmd == OBD_BRW_WRITE) {
1924 lop = &loi->loi_write_lop;
1926 lop = &loi->loi_read_lop;
1929 spin_lock(&cli->cl_loi_list_lock);
1931 if (list_empty(&oap->oap_pending_item))
1932 GOTO(out, rc = -EINVAL);
1934 if ((oap->oap_async_flags & async_flags) == async_flags)
1937 if (SETTING(oap->oap_async_flags, async_flags, ASYNC_READY))
1938 oap->oap_async_flags |= ASYNC_READY;
1940 if (SETTING(oap->oap_async_flags, async_flags, ASYNC_URGENT)) {
1941 if (list_empty(&oap->oap_rpc_item)) {
1942 list_add(&oap->oap_urgent_item, &lop->lop_urgent);
1943 loi_list_maint(cli, loi);
1947 LOI_DEBUG(loi, "oap %p page %p has flags %x\n", oap, oap->oap_page,
1948 oap->oap_async_flags);
1950 osc_check_rpcs(cli);
1951 spin_unlock(&cli->cl_loi_list_lock);
1955 static int osc_queue_group_io(struct obd_export *exp, struct lov_stripe_md *lsm,
1956 struct lov_oinfo *loi,
1957 struct obd_io_group *oig, void *cookie,
1958 int cmd, obd_off off, int count,
1960 obd_flag async_flags)
1962 struct client_obd *cli = &exp->exp_obd->u.cli;
1963 struct osc_async_page *oap;
1964 struct loi_oap_pages *lop;
1967 oap = oap_from_cookie(cookie);
1969 RETURN(PTR_ERR(oap));
1971 if (cli->cl_import == NULL || cli->cl_import->imp_invalid)
1974 if (!list_empty(&oap->oap_pending_item) ||
1975 !list_empty(&oap->oap_urgent_item) ||
1976 !list_empty(&oap->oap_rpc_item))
1980 loi = &lsm->lsm_oinfo[0];
1982 spin_lock(&cli->cl_loi_list_lock);
1985 oap->oap_page_off = off;
1986 oap->oap_count = count;
1987 oap->oap_brw_flags = brw_flags;
1988 oap->oap_async_flags = async_flags;
1990 if (cmd == OBD_BRW_WRITE)
1991 lop = &loi->loi_write_lop;
1993 lop = &loi->loi_read_lop;
1995 list_add_tail(&oap->oap_pending_item, &lop->lop_pending_group);
1996 if (oap->oap_async_flags & ASYNC_GROUP_SYNC) {
1998 oig_add_one(oig, &oap->oap_occ);
2001 LOI_DEBUG(loi, "oap %p page %p on group pending\n", oap, oap->oap_page);
2003 spin_unlock(&cli->cl_loi_list_lock);
2008 static void osc_group_to_pending(struct client_obd *cli, struct lov_oinfo *loi,
2009 struct loi_oap_pages *lop, int cmd)
2011 struct list_head *pos, *tmp;
2012 struct osc_async_page *oap;
2014 list_for_each_safe(pos, tmp, &lop->lop_pending_group) {
2015 oap = list_entry(pos, struct osc_async_page, oap_pending_item);
2016 list_del(&oap->oap_pending_item);
2017 list_add_tail(&oap->oap_pending_item, &lop->lop_pending);
2018 list_add(&oap->oap_urgent_item, &lop->lop_urgent);
2019 lop_update_pending(cli, lop, cmd, 1);
2021 loi_list_maint(cli, loi);
2024 static int osc_trigger_group_io(struct obd_export *exp,
2025 struct lov_stripe_md *lsm,
2026 struct lov_oinfo *loi,
2027 struct obd_io_group *oig)
2029 struct client_obd *cli = &exp->exp_obd->u.cli;
2033 loi = &lsm->lsm_oinfo[0];
2035 spin_lock(&cli->cl_loi_list_lock);
2037 osc_group_to_pending(cli, loi, &loi->loi_write_lop, OBD_BRW_WRITE);
2038 osc_group_to_pending(cli, loi, &loi->loi_read_lop, OBD_BRW_READ);
2040 osc_check_rpcs(cli);
2041 spin_unlock(&cli->cl_loi_list_lock);
2046 static int osc_teardown_async_page(struct obd_export *exp,
2047 struct lov_stripe_md *lsm,
2048 struct lov_oinfo *loi, void *cookie)
2050 struct client_obd *cli = &exp->exp_obd->u.cli;
2051 struct loi_oap_pages *lop;
2052 struct osc_async_page *oap;
2056 oap = oap_from_cookie(cookie);
2058 RETURN(PTR_ERR(oap));
2061 loi = &lsm->lsm_oinfo[0];
2063 if (oap->oap_cmd == OBD_BRW_WRITE) {
2064 lop = &loi->loi_write_lop;
2066 lop = &loi->loi_read_lop;
2069 spin_lock(&cli->cl_loi_list_lock);
2071 if (!list_empty(&oap->oap_rpc_item))
2072 GOTO(out, rc = -EBUSY);
2074 osc_exit_cache(cli, oap, 0);
2075 osc_wake_cache_waiters(cli);
2077 if (!list_empty(&oap->oap_urgent_item)) {
2078 list_del_init(&oap->oap_urgent_item);
2079 oap->oap_async_flags &= ~ASYNC_URGENT;
2081 if (!list_empty(&oap->oap_pending_item)) {
2082 list_del_init(&oap->oap_pending_item);
2083 lop_update_pending(cli, lop, oap->oap_cmd, -1);
2085 loi_list_maint(cli, loi);
2087 LOI_DEBUG(loi, "oap %p page %p torn down\n", oap, oap->oap_page);
2089 spin_unlock(&cli->cl_loi_list_lock);
2091 OBD_FREE(oap, sizeof(*oap));
2096 /* Note: caller will lock/unlock, and set uptodate on the pages */
2097 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
2098 static int sanosc_brw_read(struct obd_export *exp, struct obdo *oa,
2099 struct lov_stripe_md *lsm, obd_count page_count,
2100 struct brw_page *pga)
2102 struct ptlrpc_request *request = NULL;
2103 struct ost_body *body;
2104 struct niobuf_remote *nioptr;
2105 struct obd_ioobj *iooptr;
2106 int rc, size[3] = {sizeof(*body)}, mapped = 0;
2110 /* XXX does not handle 'new' brw protocol */
2112 size[1] = sizeof(struct obd_ioobj);
2113 size[2] = page_count * sizeof(*nioptr);
2115 request = ptlrpc_prep_req(class_exp2cliimp(exp), OST_SAN_READ, 3,
2120 body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof(*body));
2121 iooptr = lustre_msg_buf(request->rq_reqmsg, 1, sizeof(*iooptr));
2122 nioptr = lustre_msg_buf(request->rq_reqmsg, 2,
2123 sizeof(*nioptr) * page_count);
2125 memcpy(&body->oa, oa, sizeof(body->oa));
2127 obdo_to_ioobj(oa, iooptr);
2128 iooptr->ioo_bufcnt = page_count;
2130 for (mapped = 0; mapped < page_count; mapped++, nioptr++) {
2131 LASSERT(PageLocked(pga[mapped].pg));
2132 LASSERT(mapped == 0 || pga[mapped].off > pga[mapped - 1].off);
2134 nioptr->offset = pga[mapped].off;
2135 nioptr->len = pga[mapped].count;
2136 nioptr->flags = pga[mapped].flag;
2139 size[1] = page_count * sizeof(*nioptr);
2140 request->rq_replen = lustre_msg_size(2, size);
2142 rc = ptlrpc_queue_wait(request);
2146 body = lustre_swab_repbuf(request, 0, sizeof(*body),
2147 lustre_swab_ost_body);
2149 CERROR("Can't unpack body\n");
2150 GOTO(out_req, rc = -EPROTO);
2153 memcpy(oa, &body->oa, sizeof(*oa));
2155 swab = lustre_msg_swabbed(request->rq_repmsg);
2156 LASSERT_REPSWAB(request, 1);
2157 nioptr = lustre_msg_buf(request->rq_repmsg, 1, size[1]);
2159 /* nioptr missing or short */
2160 GOTO(out_req, rc = -EPROTO);
2164 for (mapped = 0; mapped < page_count; mapped++, nioptr++) {
2165 struct page *page = pga[mapped].pg;
2166 struct buffer_head *bh;
2170 lustre_swab_niobuf_remote (nioptr);
2172 /* got san device associated */
2173 LASSERT(exp->exp_obd != NULL);
2174 dev = exp->exp_obd->u.cli.cl_sandev;
2177 if (!nioptr->offset) {
2178 CDEBUG(D_PAGE, "hole at ino %lu; index %ld\n",
2179 page->mapping->host->i_ino,
2181 memset(page_address(page), 0, PAGE_SIZE);
2185 if (!page->buffers) {
2186 create_empty_buffers(page, dev, PAGE_SIZE);
2189 clear_bit(BH_New, &bh->b_state);
2190 set_bit(BH_Mapped, &bh->b_state);
2191 bh->b_blocknr = (unsigned long)nioptr->offset;
2193 clear_bit(BH_Uptodate, &bh->b_state);
2195 ll_rw_block(READ, 1, &bh);
2199 /* if buffer already existed, it must be the
2200 * one we mapped before, check it */
2201 LASSERT(!test_bit(BH_New, &bh->b_state));
2202 LASSERT(test_bit(BH_Mapped, &bh->b_state));
2203 LASSERT(bh->b_blocknr == (unsigned long)nioptr->offset);
2205 /* wait it's io completion */
2206 if (test_bit(BH_Lock, &bh->b_state))
2209 if (!test_bit(BH_Uptodate, &bh->b_state))
2210 ll_rw_block(READ, 1, &bh);
2214 /* must do syncronous write here */
2216 if (!buffer_uptodate(bh)) {
2224 ptlrpc_req_finished(request);
2228 static int sanosc_brw_write(struct obd_export *exp, struct obdo *oa,
2229 struct lov_stripe_md *lsm, obd_count page_count,
2230 struct brw_page *pga)
2232 struct ptlrpc_request *request = NULL;
2233 struct ost_body *body;
2234 struct niobuf_remote *nioptr;
2235 struct obd_ioobj *iooptr;
2236 int rc, size[3] = {sizeof(*body)}, mapped = 0;
2240 size[1] = sizeof(struct obd_ioobj);
2241 size[2] = page_count * sizeof(*nioptr);
2243 request = ptlrpc_prep_req(class_exp2cliimp(exp), OST_SAN_WRITE,
2248 body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof (*body));
2249 iooptr = lustre_msg_buf(request->rq_reqmsg, 1, sizeof (*iooptr));
2250 nioptr = lustre_msg_buf(request->rq_reqmsg, 2,
2251 sizeof (*nioptr) * page_count);
2253 memcpy(&body->oa, oa, sizeof(body->oa));
2255 obdo_to_ioobj(oa, iooptr);
2256 iooptr->ioo_bufcnt = page_count;
2259 for (mapped = 0; mapped < page_count; mapped++, nioptr++) {
2260 LASSERT(PageLocked(pga[mapped].pg));
2261 LASSERT(mapped == 0 || pga[mapped].off > pga[mapped - 1].off);
2263 nioptr->offset = pga[mapped].off;
2264 nioptr->len = pga[mapped].count;
2265 nioptr->flags = pga[mapped].flag;
2268 size[1] = page_count * sizeof(*nioptr);
2269 request->rq_replen = lustre_msg_size(2, size);
2271 rc = ptlrpc_queue_wait(request);
2275 swab = lustre_msg_swabbed (request->rq_repmsg);
2276 LASSERT_REPSWAB (request, 1);
2277 nioptr = lustre_msg_buf(request->rq_repmsg, 1, size[1]);
2279 CERROR("absent/short niobuf array\n");
2280 GOTO(out_req, rc = -EPROTO);
2284 for (mapped = 0; mapped < page_count; mapped++, nioptr++) {
2285 struct page *page = pga[mapped].pg;
2286 struct buffer_head *bh;
2290 lustre_swab_niobuf_remote (nioptr);
2292 /* got san device associated */
2293 LASSERT(exp->exp_obd != NULL);
2294 dev = exp->exp_obd->u.cli.cl_sandev;
2296 if (!page->buffers) {
2297 create_empty_buffers(page, dev, PAGE_SIZE);
2300 LASSERT(!test_bit(BH_New, &page->buffers->b_state));
2301 LASSERT(test_bit(BH_Mapped, &page->buffers->b_state));
2302 LASSERT(page->buffers->b_blocknr ==
2303 (unsigned long)nioptr->offset);
2309 /* if buffer locked, wait it's io completion */
2310 if (test_bit(BH_Lock, &bh->b_state))
2313 clear_bit(BH_New, &bh->b_state);
2314 set_bit(BH_Mapped, &bh->b_state);
2316 /* override the block nr */
2317 bh->b_blocknr = (unsigned long)nioptr->offset;
2319 /* we are about to write it, so set it
2321 * page lock should garentee no race condition here */
2322 set_bit(BH_Uptodate, &bh->b_state);
2323 set_bit(BH_Dirty, &bh->b_state);
2325 ll_rw_block(WRITE, 1, &bh);
2327 /* must do syncronous write here */
2329 if (!buffer_uptodate(bh) || test_bit(BH_Dirty, &bh->b_state)) {
2337 ptlrpc_req_finished(request);
2341 static int sanosc_brw(int cmd, struct obd_export *exp, struct obdo *oa,
2342 struct lov_stripe_md *lsm, obd_count page_count,
2343 struct brw_page *pga, struct obd_trans_info *oti)
2347 while (page_count) {
2348 obd_count pages_per_brw;
2351 if (page_count > PTLRPC_MAX_BRW_PAGES)
2352 pages_per_brw = PTLRPC_MAX_BRW_PAGES;
2354 pages_per_brw = page_count;
2356 if (cmd & OBD_BRW_WRITE)
2357 rc = sanosc_brw_write(exp, oa, lsm, pages_per_brw,pga);
2359 rc = sanosc_brw_read(exp, oa, lsm, pages_per_brw, pga);
2364 page_count -= pages_per_brw;
2365 pga += pages_per_brw;
2372 static void osc_set_data_with_check(struct lustre_handle *lockh, void *data)
2374 struct ldlm_lock *lock = ldlm_handle2lock(lockh);
2376 LASSERT(lock != NULL);
2377 l_lock(&lock->l_resource->lr_namespace->ns_lock);
2379 if (lock->l_ast_data && lock->l_ast_data != data) {
2380 struct inode *new_inode = data;
2381 struct inode *old_inode = lock->l_ast_data;
2382 LASSERTF(old_inode->i_state & I_FREEING,
2383 "Found existing inode %p/%lu/%u state %lu in lock: "
2384 "setting data to %p/%lu/%u\n", old_inode,
2385 old_inode->i_ino, old_inode->i_generation,
2387 new_inode, new_inode->i_ino, new_inode->i_generation);
2390 lock->l_ast_data = data;
2391 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
2392 LDLM_LOCK_PUT(lock);
2395 static int osc_change_cbdata(struct obd_export *exp, struct lov_stripe_md *lsm,
2396 ldlm_iterator_t replace, void *data)
2398 struct ldlm_res_id res_id = { .name = {0} };
2399 struct obd_device *obd = class_exp2obd(exp);
2401 res_id.name[0] = lsm->lsm_object_id;
2402 res_id.name[2] = lsm->lsm_object_gr;
2403 ldlm_change_cbdata(obd->obd_namespace, &res_id, replace, data);
2407 static int osc_enqueue(struct obd_export *exp, struct lov_stripe_md *lsm,
2408 __u32 type, ldlm_policy_data_t *policy, __u32 mode,
2409 int *flags, void *bl_cb, void *cp_cb, void *gl_cb,
2410 void *data, __u32 lvb_len, void *lvb_swabber,
2411 struct lustre_handle *lockh)
2413 struct obd_device *obd = exp->exp_obd;
2414 struct ldlm_res_id res_id = { .name = {0} };
2419 res_id.name[0] = lsm->lsm_object_id;
2420 res_id.name[2] = lsm->lsm_object_gr;
2422 /* Filesystem lock extents are extended to page boundaries so that
2423 * dealing with the page cache is a little smoother. */
2424 policy->l_extent.start -= policy->l_extent.start & ~PAGE_MASK;
2425 policy->l_extent.end |= ~PAGE_MASK;
2427 if (lsm->lsm_oinfo->loi_kms_valid == 0)
2430 /* Next, search for already existing extent locks that will cover us */
2431 rc = ldlm_lock_match(obd->obd_namespace, 0, &res_id, type, policy, mode,
2434 osc_set_data_with_check(lockh, data);
2435 if (*flags & LDLM_FL_HAS_INTENT) {
2436 /* I would like to be able to ASSERT here that rss <=
2437 * kms, but I can't, for reasons which are explained in
2440 /* We already have a lock, and it's referenced */
2444 /* If we're trying to read, we also search for an existing PW lock. The
2445 * VFS and page cache already protect us locally, so lots of readers/
2446 * writers can share a single PW lock.
2448 * There are problems with conversion deadlocks, so instead of
2449 * converting a read lock to a write lock, we'll just enqueue a new
2452 * At some point we should cancel the read lock instead of making them
2453 * send us a blocking callback, but there are problems with canceling
2454 * locks out from other users right now, too. */
2456 if (mode == LCK_PR) {
2457 rc = ldlm_lock_match(obd->obd_namespace, 0, &res_id, type,
2458 policy, LCK_PW, lockh);
2460 /* FIXME: This is not incredibly elegant, but it might
2461 * be more elegant than adding another parameter to
2462 * lock_match. I want a second opinion. */
2463 ldlm_lock_addref(lockh, LCK_PR);
2464 ldlm_lock_decref(lockh, LCK_PW);
2465 osc_set_data_with_check(lockh, data);
2471 rc = ldlm_cli_enqueue(exp, NULL, obd->obd_namespace, res_id, type,
2472 policy, mode, flags, bl_cb, cp_cb, gl_cb, data,
2473 &lvb, sizeof(lvb), lustre_swab_ost_lvb, lockh);
2475 if ((*flags & LDLM_FL_HAS_INTENT && rc == ELDLM_LOCK_ABORTED) || !rc) {
2476 CDEBUG(D_INODE, "received kms == "LPU64"\n", lvb.lvb_size);
2477 lsm->lsm_oinfo->loi_rss = lvb.lvb_size;
2483 static int osc_match(struct obd_export *exp, struct lov_stripe_md *lsm,
2484 __u32 type, ldlm_policy_data_t *policy, __u32 mode,
2485 int *flags, void *data, struct lustre_handle *lockh)
2487 struct ldlm_res_id res_id = { .name = {0} };
2488 struct obd_device *obd = exp->exp_obd;
2492 res_id.name[0] = lsm->lsm_object_id;
2493 res_id.name[2] = lsm->lsm_object_gr;
2495 OBD_FAIL_RETURN(OBD_FAIL_OSC_MATCH, -EIO);
2497 /* Filesystem lock extents are extended to page boundaries so that
2498 * dealing with the page cache is a little smoother */
2499 policy->l_extent.start -= policy->l_extent.start & ~PAGE_MASK;
2500 policy->l_extent.end |= ~PAGE_MASK;
2502 /* Next, search for already existing extent locks that will cover us */
2503 rc = ldlm_lock_match(obd->obd_namespace, *flags, &res_id, type,
2504 policy, mode, lockh);
2506 osc_set_data_with_check(lockh, data);
2509 /* If we're trying to read, we also search for an existing PW lock. The
2510 * VFS and page cache already protect us locally, so lots of readers/
2511 * writers can share a single PW lock. */
2512 if (mode == LCK_PR) {
2513 rc = ldlm_lock_match(obd->obd_namespace, *flags, &res_id, type,
2514 policy, LCK_PW, lockh);
2515 if (rc == 1 && !(*flags & LDLM_FL_TEST_LOCK)) {
2516 /* FIXME: This is not incredibly elegant, but it might
2517 * be more elegant than adding another parameter to
2518 * lock_match. I want a second opinion. */
2519 osc_set_data_with_check(lockh, data);
2520 ldlm_lock_addref(lockh, LCK_PR);
2521 ldlm_lock_decref(lockh, LCK_PW);
2527 static int osc_cancel(struct obd_export *exp, struct lov_stripe_md *md,
2528 __u32 mode, struct lustre_handle *lockh)
2533 ldlm_lock_decref_and_cancel(lockh, mode);
2535 ldlm_lock_decref(lockh, mode);
2540 static int osc_cancel_unused(struct obd_export *exp,
2541 struct lov_stripe_md *lsm, int flags, void *opaque)
2543 struct obd_device *obd = class_exp2obd(exp);
2544 struct ldlm_res_id res_id = { .name = {0} };
2546 res_id.name[0] = lsm->lsm_object_id;
2547 res_id.name[2] = lsm->lsm_object_gr;
2549 return ldlm_cli_cancel_unused(obd->obd_namespace, &res_id, flags,
2553 static int osc_statfs(struct obd_device *obd, struct obd_statfs *osfs,
2554 unsigned long max_age)
2556 struct obd_statfs *msfs;
2557 struct ptlrpc_request *request;
2558 int rc, size = sizeof(*osfs);
2561 /* We could possibly pass max_age in the request (as an absolute
2562 * timestamp or a "seconds.usec ago") so the target can avoid doing
2563 * extra calls into the filesystem if that isn't necessary (e.g.
2564 * during mount that would help a bit). Having relative timestamps
2565 * is not so great if request processing is slow, while absolute
2566 * timestamps are not ideal because they need time synchronization. */
2567 request = ptlrpc_prep_req(obd->u.cli.cl_import, OST_STATFS,0,NULL,NULL);
2571 request->rq_replen = lustre_msg_size(1, &size);
2572 request->rq_request_portal = OST_CREATE_PORTAL; //XXX FIXME bug 249
2574 rc = ptlrpc_queue_wait(request);
2578 msfs = lustre_swab_repbuf(request, 0, sizeof(*msfs),
2579 lustre_swab_obd_statfs);
2581 CERROR("Can't unpack obd_statfs\n");
2582 GOTO(out, rc = -EPROTO);
2585 memcpy(osfs, msfs, sizeof(*osfs));
2589 ptlrpc_req_finished(request);
2593 /* Retrieve object striping information.
2595 * @lmmu is a pointer to an in-core struct with lmm_ost_count indicating
2596 * the maximum number of OST indices which will fit in the user buffer.
2597 * lmm_magic must be LOV_MAGIC (we only use 1 slot here).
2599 static int osc_getstripe(struct lov_stripe_md *lsm, struct lov_user_md *lump)
2601 struct lov_user_md lum, *lumk;
2608 rc = copy_from_user(&lum, lump, sizeof(lum));
2612 if (lum.lmm_magic != LOV_USER_MAGIC)
2615 if (lum.lmm_stripe_count > 0) {
2616 lum_size = sizeof(lum) + sizeof(lum.lmm_objects[0]);
2617 OBD_ALLOC(lumk, lum_size);
2621 lumk->lmm_objects[0].l_object_id = lsm->lsm_object_id;
2622 lumk->lmm_objects[0].l_object_gr = lsm->lsm_object_gr;
2624 lum_size = sizeof(lum);
2628 lumk->lmm_object_id = lsm->lsm_object_id;
2629 lumk->lmm_object_gr = lsm->lsm_object_gr;
2630 lumk->lmm_stripe_count = 1;
2632 if (copy_to_user(lump, lumk, lum_size))
2636 OBD_FREE(lumk, lum_size);
2641 static int osc_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
2642 void *karg, void *uarg)
2644 struct obd_device *obd = exp->exp_obd;
2645 struct obd_ioctl_data *data = karg;
2652 case OBD_IOC_LOV_GET_CONFIG: {
2654 struct lov_desc *desc;
2655 struct obd_uuid uuid;
2659 if (obd_ioctl_getdata(&buf, &len, (void *)uarg))
2660 GOTO(out, err = -EINVAL);
2662 data = (struct obd_ioctl_data *)buf;
2664 if (sizeof(*desc) > data->ioc_inllen1) {
2666 GOTO(out, err = -EINVAL);
2669 if (data->ioc_inllen2 < sizeof(uuid)) {
2671 GOTO(out, err = -EINVAL);
2674 desc = (struct lov_desc *)data->ioc_inlbuf1;
2675 desc->ld_tgt_count = 1;
2676 desc->ld_active_tgt_count = 1;
2677 desc->ld_default_stripe_count = 1;
2678 desc->ld_default_stripe_size = 0;
2679 desc->ld_default_stripe_offset = 0;
2680 desc->ld_pattern = 0;
2681 memcpy(&desc->ld_uuid, &obd->obd_uuid, sizeof(uuid));
2683 memcpy(data->ioc_inlbuf2, &obd->obd_uuid, sizeof(uuid));
2685 err = copy_to_user((void *)uarg, buf, len);
2688 obd_ioctl_freedata(buf, len);
2691 case LL_IOC_LOV_SETSTRIPE:
2692 err = obd_alloc_memmd(exp, karg);
2696 case LL_IOC_LOV_GETSTRIPE:
2697 err = osc_getstripe(karg, uarg);
2699 case OBD_IOC_CLIENT_RECOVER:
2700 err = ptlrpc_recover_import(obd->u.cli.cl_import,
2705 case IOC_OSC_SET_ACTIVE:
2706 err = ptlrpc_set_import_active(obd->u.cli.cl_import,
2710 CDEBUG(D_INODE, "unrecognised ioctl %#x by %s\n", cmd, current->comm);
2711 GOTO(out, err = -ENOTTY);
2718 static int osc_get_info(struct obd_export *exp, obd_count keylen,
2719 void *key, __u32 *vallen, void *val)
2722 if (!vallen || !val)
2725 if (keylen > strlen("lock_to_stripe") &&
2726 strcmp(key, "lock_to_stripe") == 0) {
2727 __u32 *stripe = val;
2728 *vallen = sizeof(*stripe);
2731 } else if (keylen >= strlen("last_id") && strcmp(key, "last_id") == 0) {
2732 struct ptlrpc_request *req;
2734 char *bufs[1] = {key};
2736 req = ptlrpc_prep_req(class_exp2cliimp(exp), OST_GET_INFO, 1,
2741 req->rq_replen = lustre_msg_size(1, vallen);
2742 rc = ptlrpc_queue_wait(req);
2746 reply = lustre_swab_repbuf(req, 0, sizeof(*reply),
2747 lustre_swab_ost_last_id);
2748 if (reply == NULL) {
2749 CERROR("Can't unpack OST last ID\n");
2750 GOTO(out, rc = -EPROTO);
2752 *((obd_id *)val) = *reply;
2754 ptlrpc_req_finished(req);
2760 static int osc_set_info(struct obd_export *exp, obd_count keylen,
2761 void *key, obd_count vallen, void *val)
2763 struct ptlrpc_request *req;
2764 struct obd_device *obd = exp->exp_obd;
2765 struct obd_import *imp = class_exp2cliimp(exp);
2766 struct llog_ctxt *ctxt;
2767 int rc, size[2] = {keylen, vallen};
2768 char *bufs[2] = {key, val};
2771 if (keylen == strlen("next_id") &&
2772 memcmp(key, "next_id", strlen("next_id")) == 0) {
2773 if (vallen != sizeof(obd_id))
2775 obd->u.cli.cl_oscc.oscc_next_id = *((obd_id*)val) + 1;
2776 CDEBUG(D_INODE, "%s: set oscc_next_id = "LPU64"\n",
2777 exp->exp_obd->obd_name,
2778 obd->u.cli.cl_oscc.oscc_next_id);
2783 if (keylen == strlen("growth_count") &&
2784 memcmp(key, "growth_count", strlen("growth_count")) == 0) {
2785 if (vallen != sizeof(int))
2787 obd->u.cli.cl_oscc.oscc_grow_count = *((int*)val);
2791 if (keylen == strlen("unlinked") &&
2792 memcmp(key, "unlinked", keylen) == 0) {
2793 struct osc_creator *oscc = &obd->u.cli.cl_oscc;
2794 spin_lock(&oscc->oscc_lock);
2795 oscc->oscc_flags &= ~OSCC_FLAG_NOSPC;
2796 spin_unlock(&oscc->oscc_lock);
2801 if (keylen == strlen("initial_recov") &&
2802 memcmp(key, "initial_recov", strlen("initial_recov")) == 0) {
2803 struct obd_import *imp = exp->exp_obd->u.cli.cl_import;
2804 if (vallen != sizeof(int))
2806 imp->imp_initial_recov = *(int *)val;
2807 CDEBUG(D_HA, "%s: set imp_no_init_recov = %d\n",
2808 exp->exp_obd->obd_name,
2809 imp->imp_initial_recov);
2813 if (keylen < strlen("mds_conn") ||
2814 memcmp(key, "mds_conn", strlen("mds_conn")) != 0)
2817 req = ptlrpc_prep_req(imp, OST_SET_INFO, 2, size, bufs);
2821 req->rq_replen = lustre_msg_size(0, NULL);
2822 rc = ptlrpc_queue_wait(req);
2823 ptlrpc_req_finished(req);
2825 ctxt = llog_get_context(&exp->exp_obd->obd_llogs, LLOG_UNLINK_ORIG_CTXT);
2827 rc = llog_initiator_connect(ctxt);
2832 imp->imp_server_timeout = 1;
2833 CDEBUG(D_HA, "pinging OST %s\n", imp->imp_target_uuid.uuid);
2834 imp->imp_pingable = 1;
2840 static struct llog_operations osc_size_repl_logops = {
2841 lop_cancel: llog_obd_repl_cancel
2844 static struct llog_operations osc_unlink_orig_logops;
2845 static int osc_llog_init(struct obd_device *obd, struct obd_llogs *llogs,
2846 struct obd_device *tgt, int count,
2847 struct llog_catid *catid)
2852 osc_unlink_orig_logops = llog_lvfs_ops;
2853 osc_unlink_orig_logops.lop_setup = llog_obd_origin_setup;
2854 osc_unlink_orig_logops.lop_cleanup = llog_obd_origin_cleanup;
2855 osc_unlink_orig_logops.lop_add = llog_obd_origin_add;
2856 osc_unlink_orig_logops.lop_connect = llog_origin_connect;
2858 rc = llog_setup(obd, llogs, LLOG_UNLINK_ORIG_CTXT, tgt, count,
2859 &catid->lci_logid, &osc_unlink_orig_logops);
2863 rc = llog_setup(obd, llogs, LLOG_SIZE_REPL_CTXT, tgt, count, NULL,
2864 &osc_size_repl_logops);
2868 static int osc_llog_finish(struct obd_device *obd,
2869 struct obd_llogs *llogs, int count)
2874 rc = llog_cleanup(llog_get_context(llogs, LLOG_UNLINK_ORIG_CTXT));
2878 rc = llog_cleanup(llog_get_context(llogs, LLOG_SIZE_REPL_CTXT));
2883 static int osc_connect(struct lustre_handle *exph,
2884 struct obd_device *obd, struct obd_uuid *cluuid)
2888 rc = client_connect_import(exph, obd, cluuid);
2893 static int osc_disconnect(struct obd_export *exp, int flags)
2895 struct obd_device *obd = class_exp2obd(exp);
2896 struct llog_ctxt *ctxt;
2899 ctxt = llog_get_context(&obd->obd_llogs, LLOG_SIZE_REPL_CTXT);
2900 if (obd->u.cli.cl_conn_count == 1)
2901 /* flush any remaining cancel messages out to the target */
2902 llog_sync(ctxt, exp);
2904 rc = client_disconnect_export(exp, flags);
2908 static int osc_import_event(struct obd_device *obd,
2909 struct obd_import *imp,
2910 enum obd_import_event event)
2912 struct client_obd *cli;
2915 LASSERT(imp->imp_obd == obd);
2918 case IMP_EVENT_DISCON: {
2919 /* Only do this on the MDS OSC's */
2920 if (imp->imp_server_timeout) {
2921 struct osc_creator *oscc = &obd->u.cli.cl_oscc;
2923 spin_lock(&oscc->oscc_lock);
2924 oscc->oscc_flags |= OSCC_FLAG_RECOVERING;
2925 spin_unlock(&oscc->oscc_lock);
2929 case IMP_EVENT_INACTIVE: {
2930 if (obd->obd_observer)
2931 rc = obd_notify(obd->obd_observer, obd, 0);
2934 case IMP_EVENT_INVALIDATE: {
2935 struct ldlm_namespace *ns = obd->obd_namespace;
2939 spin_lock(&cli->cl_loi_list_lock);
2940 cli->cl_avail_grant = 0;
2941 cli->cl_lost_grant = 0;
2942 /* all pages go to failing rpcs due to the invalid import */
2943 osc_check_rpcs(cli);
2944 spin_unlock(&cli->cl_loi_list_lock);
2946 ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
2950 case IMP_EVENT_ACTIVE: {
2951 if (obd->obd_observer)
2952 rc = obd_notify(obd->obd_observer, obd, 1);
2956 CERROR("Unknown import event %d\n", event);
2962 int osc_setup(struct obd_device *obd, obd_count len, void *buf)
2966 rc = ptlrpcd_addref();
2970 rc = client_obd_setup(obd, len, buf);
2979 int osc_cleanup(struct obd_device *obd, int flags)
2983 rc = client_obd_cleanup(obd, flags);
2989 struct obd_ops osc_obd_ops = {
2990 o_owner: THIS_MODULE,
2991 o_attach: osc_attach,
2992 o_detach: osc_detach,
2994 o_cleanup: osc_cleanup,
2995 o_connect: osc_connect,
2996 o_disconnect: osc_disconnect,
2997 o_statfs: osc_statfs,
2998 o_packmd: osc_packmd,
2999 o_unpackmd: osc_unpackmd,
3000 o_create: osc_create,
3001 o_destroy: osc_destroy,
3002 o_getattr: osc_getattr,
3003 o_getattr_async:osc_getattr_async,
3004 o_setattr: osc_setattr,
3006 o_brw_async: osc_brw_async,
3007 .o_prep_async_page = osc_prep_async_page,
3008 .o_queue_async_io = osc_queue_async_io,
3009 .o_set_async_flags = osc_set_async_flags,
3010 .o_queue_group_io = osc_queue_group_io,
3011 .o_trigger_group_io = osc_trigger_group_io,
3012 .o_teardown_async_page = osc_teardown_async_page,
3015 o_enqueue: osc_enqueue,
3017 o_change_cbdata:osc_change_cbdata,
3018 o_cancel: osc_cancel,
3019 o_cancel_unused:osc_cancel_unused,
3020 o_iocontrol: osc_iocontrol,
3021 o_get_info: osc_get_info,
3022 o_set_info: osc_set_info,
3023 o_import_event: osc_import_event,
3024 o_llog_init: osc_llog_init,
3025 o_llog_finish: osc_llog_finish,
3028 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
3029 struct obd_ops sanosc_obd_ops = {
3030 o_owner: THIS_MODULE,
3031 o_attach: osc_attach,
3032 o_detach: osc_detach,
3033 o_cleanup: client_obd_cleanup,
3034 o_connect: osc_connect,
3035 o_disconnect: client_disconnect_export,
3036 o_statfs: osc_statfs,
3037 o_packmd: osc_packmd,
3038 o_unpackmd: osc_unpackmd,
3039 o_create: osc_real_create,
3040 o_destroy: osc_destroy,
3041 o_getattr: osc_getattr,
3042 o_getattr_async:osc_getattr_async,
3043 o_setattr: osc_setattr,
3044 o_setup: client_sanobd_setup,
3048 o_enqueue: osc_enqueue,
3050 o_change_cbdata:osc_change_cbdata,
3051 o_cancel: osc_cancel,
3052 o_cancel_unused:osc_cancel_unused,
3053 o_iocontrol: osc_iocontrol,
3054 o_import_event: osc_import_event,
3055 o_llog_init: osc_llog_init,
3056 o_llog_finish: osc_llog_finish,
3060 int __init osc_init(void)
3062 struct lprocfs_static_vars lvars, sanlvars;
3066 lprocfs_init_vars(osc, &lvars);
3067 lprocfs_init_vars(osc, &sanlvars);
3069 rc = class_register_type(&osc_obd_ops, NULL, lvars.module_vars,
3074 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
3075 rc = class_register_type(&sanosc_obd_ops, NULL, sanlvars.module_vars,
3076 LUSTRE_SANOSC_NAME);
3078 class_unregister_type(LUSTRE_OSC_NAME);
3084 static void /*__exit*/ osc_exit(void)
3086 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
3087 class_unregister_type(LUSTRE_SANOSC_NAME);
3089 class_unregister_type(LUSTRE_OSC_NAME);
3093 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
3094 MODULE_DESCRIPTION("Lustre Object Storage Client (OSC)");
3095 MODULE_LICENSE("GPL");
3097 module_init(osc_init);
3098 module_exit(osc_exit);