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 <lustre/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 (*lmmp)->lmm_object_id = cpu_to_le64(lsm->lsm_object_id);
127 /* Unpack OSC object metadata from disk storage (LE byte order). */
128 static int osc_unpackmd(struct obd_export *exp, struct lov_stripe_md **lsmp,
129 struct lov_mds_md *lmm, int lmm_bytes)
135 if (lmm_bytes < sizeof (*lmm)) {
136 CERROR("lov_mds_md too small: %d, need %d\n",
137 lmm_bytes, (int)sizeof(*lmm));
140 /* XXX LOV_MAGIC etc check? */
142 if (lmm->lmm_object_id == 0) {
143 CERROR("lov_mds_md: zero lmm_object_id\n");
148 lsm_size = lov_stripe_md_size(1);
152 if (*lsmp != NULL && lmm == NULL) {
153 OBD_FREE(*lsmp, lsm_size);
159 OBD_ALLOC(*lsmp, lsm_size);
162 loi_init((*lsmp)->lsm_oinfo);
166 /* XXX zero *lsmp? */
167 (*lsmp)->lsm_object_id = le64_to_cpu (lmm->lmm_object_id);
168 LASSERT((*lsmp)->lsm_object_id);
171 (*lsmp)->lsm_maxbytes = LUSTRE_STRIPE_MAXBYTES;
176 static int osc_getattr_interpret(struct ptlrpc_request *req,
177 struct osc_getattr_async_args *aa, int rc)
179 struct ost_body *body;
185 body = lustre_swab_repbuf(req, 0, sizeof(*body), lustre_swab_ost_body);
187 CDEBUG(D_INODE, "mode: %o\n", body->oa.o_mode);
188 memcpy(aa->aa_oa, &body->oa, sizeof(*aa->aa_oa));
190 /* This should really be sent by the OST */
191 aa->aa_oa->o_blksize = PTLRPC_MAX_BRW_SIZE;
192 aa->aa_oa->o_valid |= OBD_MD_FLBLKSZ;
194 CERROR("can't unpack ost_body\n");
196 aa->aa_oa->o_valid = 0;
202 static int osc_getattr_async(struct obd_export *exp, struct obdo *oa,
203 struct lov_stripe_md *md,
204 struct ptlrpc_request_set *set)
206 struct ptlrpc_request *request;
207 struct ost_body *body;
208 int size = sizeof(*body);
209 struct osc_getattr_async_args *aa;
212 request = ptlrpc_prep_req(class_exp2cliimp(exp), OST_GETATTR, 1,
217 body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof (*body));
218 memcpy(&body->oa, oa, sizeof(*oa));
220 request->rq_replen = lustre_msg_size(1, &size);
221 request->rq_interpret_reply = osc_getattr_interpret;
223 LASSERT (sizeof (*aa) <= sizeof (request->rq_async_args));
224 aa = (struct osc_getattr_async_args *)&request->rq_async_args;
227 ptlrpc_set_add_req (set, request);
231 static int osc_getattr(struct obd_export *exp, struct obdo *oa,
232 struct lov_stripe_md *md)
234 struct ptlrpc_request *request;
235 struct ost_body *body;
236 int rc, size = sizeof(*body);
239 request = ptlrpc_prep_req(class_exp2cliimp(exp), OST_GETATTR, 1,
244 body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof (*body));
245 memcpy(&body->oa, oa, sizeof(*oa));
247 request->rq_replen = lustre_msg_size(1, &size);
249 rc = ptlrpc_queue_wait(request);
251 CERROR("%s failed: rc = %d\n", __FUNCTION__, rc);
255 body = lustre_swab_repbuf(request, 0, sizeof (*body),
256 lustre_swab_ost_body);
258 CERROR ("can't unpack ost_body\n");
259 GOTO (out, rc = -EPROTO);
262 CDEBUG(D_INODE, "mode: %o\n", body->oa.o_mode);
263 memcpy(oa, &body->oa, sizeof(*oa));
265 /* This should really be sent by the OST */
266 oa->o_blksize = PTLRPC_MAX_BRW_SIZE;
267 oa->o_valid |= OBD_MD_FLBLKSZ;
271 ptlrpc_req_finished(request);
275 static int osc_setattr(struct obd_export *exp, struct obdo *oa,
276 struct lov_stripe_md *md, struct obd_trans_info *oti)
278 struct ptlrpc_request *request;
279 struct ost_body *body;
280 int rc, size = sizeof(*body);
283 request = ptlrpc_prep_req(class_exp2cliimp(exp), OST_SETATTR, 1, &size,
288 body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof(*body));
289 memcpy(&body->oa, oa, sizeof(*oa));
291 request->rq_replen = lustre_msg_size(1, &size);
293 rc = ptlrpc_queue_wait(request);
297 body = lustre_swab_repbuf(request, 0, sizeof(*body),
298 lustre_swab_ost_body);
300 GOTO(out, rc = -EPROTO);
302 memcpy(oa, &body->oa, sizeof(*oa));
306 ptlrpc_req_finished(request);
310 int osc_real_create(struct obd_export *exp, struct obdo *oa,
311 struct lov_stripe_md **ea, struct obd_trans_info *oti)
313 struct ptlrpc_request *request;
314 struct ost_body *body;
315 struct lov_stripe_md *lsm;
316 int rc, size = sizeof(*body);
324 rc = obd_alloc_memmd(exp, &lsm);
329 request = ptlrpc_prep_req(class_exp2cliimp(exp), OST_CREATE, 1, &size,
332 GOTO(out, rc = -ENOMEM);
334 body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof (*body));
335 memcpy(&body->oa, oa, sizeof(body->oa));
337 request->rq_replen = lustre_msg_size(1, &size);
338 if (oa->o_valid & OBD_MD_FLINLINE) {
339 LASSERT((oa->o_valid & OBD_MD_FLFLAGS) &&
340 oa->o_flags == OBD_FL_DELORPHAN);
341 DEBUG_REQ(D_HA, request,
342 "delorphan from OST integration");
345 rc = ptlrpc_queue_wait(request);
349 body = lustre_swab_repbuf(request, 0, sizeof(*body),
350 lustre_swab_ost_body);
352 CERROR ("can't unpack ost_body\n");
353 GOTO (out_req, rc = -EPROTO);
356 memcpy(oa, &body->oa, sizeof(*oa));
358 /* This should really be sent by the OST */
359 oa->o_blksize = PTLRPC_MAX_BRW_SIZE;
360 oa->o_valid |= OBD_MD_FLBLKSZ;
362 /* XXX LOV STACKING: the lsm that is passed to us from LOV does not
363 * have valid lsm_oinfo data structs, so don't go touching that.
364 * This needs to be fixed in a big way.
366 lsm->lsm_object_id = oa->o_id;
370 oti->oti_transno = request->rq_repmsg->transno;
372 if (oa->o_valid & OBD_MD_FLCOOKIE) {
373 if (!oti->oti_logcookies)
374 oti_alloc_cookies(oti, 1);
375 memcpy(oti->oti_logcookies, obdo_logcookie(oa),
376 sizeof(oti->oti_onecookie));
380 CDEBUG(D_HA, "transno: "LPD64"\n", request->rq_repmsg->transno);
383 ptlrpc_req_finished(request);
386 obd_free_memmd(exp, &lsm);
390 static int osc_punch(struct obd_export *exp, struct obdo *oa,
391 struct lov_stripe_md *md, obd_size start,
392 obd_size end, struct obd_trans_info *oti)
394 struct ptlrpc_request *request;
395 struct ost_body *body;
396 int rc, size = sizeof(*body);
404 request = ptlrpc_prep_req(class_exp2cliimp(exp), OST_PUNCH, 1, &size,
409 body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof (*body));
410 memcpy(&body->oa, oa, sizeof(*oa));
412 /* overload the size and blocks fields in the oa with start/end */
413 body->oa.o_size = start;
414 body->oa.o_blocks = end;
415 body->oa.o_valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS);
417 request->rq_replen = lustre_msg_size(1, &size);
419 rc = ptlrpc_queue_wait(request);
423 body = lustre_swab_repbuf (request, 0, sizeof (*body),
424 lustre_swab_ost_body);
426 CERROR ("can't unpack ost_body\n");
427 GOTO (out, rc = -EPROTO);
430 memcpy(oa, &body->oa, sizeof(*oa));
434 ptlrpc_req_finished(request);
438 static int osc_sync(struct obd_export *exp, struct obdo *oa,
439 struct lov_stripe_md *md, obd_size start, obd_size end)
441 struct ptlrpc_request *request;
442 struct ost_body *body;
443 int rc, size = sizeof(*body);
451 request = ptlrpc_prep_req(class_exp2cliimp(exp), OST_SYNC, 1, &size,
456 body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof (*body));
457 memcpy(&body->oa, oa, sizeof(*oa));
459 /* overload the size and blocks fields in the oa with start/end */
460 body->oa.o_size = start;
461 body->oa.o_blocks = end;
462 body->oa.o_valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS);
464 request->rq_replen = lustre_msg_size(1, &size);
466 rc = ptlrpc_queue_wait(request);
470 body = lustre_swab_repbuf(request, 0, sizeof(*body),
471 lustre_swab_ost_body);
473 CERROR ("can't unpack ost_body\n");
474 GOTO (out, rc = -EPROTO);
477 memcpy(oa, &body->oa, sizeof(*oa));
481 ptlrpc_req_finished(request);
485 static int osc_destroy(struct obd_export *exp, struct obdo *oa,
486 struct lov_stripe_md *ea, struct obd_trans_info *oti)
488 struct ptlrpc_request *request;
489 struct ost_body *body;
490 int rc, size = sizeof(*body);
498 request = ptlrpc_prep_req(class_exp2cliimp(exp), OST_DESTROY, 1,
503 body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof (*body));
505 if (oti != NULL && oa->o_valid & OBD_MD_FLCOOKIE) {
506 memcpy(obdo_logcookie(oa), oti->oti_logcookies,
507 sizeof(*oti->oti_logcookies));
508 oti->oti_logcookies++;
511 memcpy(&body->oa, oa, sizeof(*oa));
512 request->rq_replen = lustre_msg_size(1, &size);
514 rc = ptlrpc_queue_wait(request);
518 body = lustre_swab_repbuf(request, 0, sizeof(*body),
519 lustre_swab_ost_body);
521 CERROR ("Can't unpack body\n");
522 GOTO (out, rc = -EPROTO);
525 memcpy(oa, &body->oa, sizeof(*oa));
529 ptlrpc_req_finished(request);
533 static void osc_announce_cached(struct client_obd *cli, struct obdo *oa,
536 obd_flag bits = OBD_MD_FLBLOCKS|OBD_MD_FLGRANT;
538 LASSERT(!(oa->o_valid & bits));
541 spin_lock(&cli->cl_loi_list_lock);
542 oa->o_dirty = cli->cl_dirty;
543 oa->o_undirty = cli->cl_dirty_max - oa->o_dirty;
544 oa->o_grant = cli->cl_avail_grant;
545 oa->o_dropped = cli->cl_lost_grant;
546 cli->cl_lost_grant = 0;
547 spin_unlock(&cli->cl_loi_list_lock);
548 CDEBUG(D_CACHE,"dirty: "LPU64" undirty: %u dropped %u grant: "LPU64"\n",
549 oa->o_dirty, oa->o_undirty, oa->o_dropped, oa->o_grant);
552 /* caller must hold loi_list_lock */
553 static void osc_consume_write_grant(struct client_obd *cli,
554 struct osc_async_page *oap)
556 cli->cl_dirty += PAGE_SIZE;
557 cli->cl_avail_grant -= PAGE_SIZE;
558 oap->oap_brw_flags |= OBD_BRW_FROM_GRANT;
559 CDEBUG(D_CACHE, "using %lu grant credits for oap %p\n", PAGE_SIZE, oap);
560 LASSERT(cli->cl_avail_grant >= 0);
563 /* caller must hold loi_list_lock */
564 void osc_wake_cache_waiters(struct client_obd *cli)
566 struct list_head *l, *tmp;
567 struct osc_cache_waiter *ocw;
569 list_for_each_safe(l, tmp, &cli->cl_cache_waiters) {
570 /* if we can't dirty more, we must wait until some is written */
571 if (cli->cl_dirty + PAGE_SIZE > cli->cl_dirty_max) {
572 CDEBUG(D_CACHE, "no dirty room: dirty: %ld max %ld\n",
573 cli->cl_dirty, cli->cl_dirty_max);
577 /* if still dirty cache but no grant wait for pending RPCs that
578 * may yet return us some grant before doing sync writes */
579 if (cli->cl_brw_in_flight && cli->cl_avail_grant < PAGE_SIZE) {
580 CDEBUG(D_CACHE, "%d BRWs in flight, no grant\n",
581 cli->cl_brw_in_flight);
585 ocw = list_entry(l, struct osc_cache_waiter, ocw_entry);
586 list_del_init(&ocw->ocw_entry);
587 if (cli->cl_avail_grant < PAGE_SIZE) {
588 /* no more RPCs in flight to return grant, do sync IO */
589 ocw->ocw_rc = -EDQUOT;
590 CDEBUG(D_INODE, "wake oap %p for sync\n", ocw->ocw_oap);
592 osc_consume_write_grant(cli, ocw->ocw_oap);
595 wake_up(&ocw->ocw_waitq);
601 static void osc_update_grant(struct client_obd *cli, struct ost_body *body)
603 spin_lock(&cli->cl_loi_list_lock);
604 CDEBUG(D_CACHE, "got "LPU64" extra grant\n", body->oa.o_grant);
605 cli->cl_avail_grant += body->oa.o_grant;
606 /* waiters are woken in brw_interpret_oap */
607 spin_unlock(&cli->cl_loi_list_lock);
610 /* We assume that the reason this OSC got a short read is because it read
611 * beyond the end of a stripe file; i.e. lustre is reading a sparse file
612 * via the LOV, and it _knows_ it's reading inside the file, it's just that
613 * this stripe never got written at or beyond this stripe offset yet. */
614 static void handle_short_read(int nob_read, obd_count page_count,
615 struct brw_page *pga)
619 /* skip bytes read OK */
620 while (nob_read > 0) {
621 LASSERT (page_count > 0);
623 if (pga->count > nob_read) {
624 /* EOF inside this page */
625 ptr = kmap(pga->pg) + (pga->off & ~PAGE_MASK);
626 memset(ptr + nob_read, 0, pga->count - nob_read);
633 nob_read -= pga->count;
638 /* zero remaining pages */
639 while (page_count-- > 0) {
640 ptr = kmap(pga->pg) + (pga->off & ~PAGE_MASK);
641 memset(ptr, 0, pga->count);
647 static int check_write_rcs(struct ptlrpc_request *request,
648 int requested_nob, int niocount,
649 obd_count page_count, struct brw_page *pga)
653 /* return error if any niobuf was in error */
654 remote_rcs = lustre_swab_repbuf(request, 1,
655 sizeof(*remote_rcs) * niocount, NULL);
656 if (remote_rcs == NULL) {
657 CERROR("Missing/short RC vector on BRW_WRITE reply\n");
660 if (lustre_msg_swabbed(request->rq_repmsg))
661 for (i = 0; i < niocount; i++)
662 __swab32s(&remote_rcs[i]);
664 for (i = 0; i < niocount; i++) {
665 if (remote_rcs[i] < 0)
666 return(remote_rcs[i]);
668 if (remote_rcs[i] != 0) {
669 CERROR("rc[%d] invalid (%d) req %p\n",
670 i, remote_rcs[i], request);
675 if (request->rq_bulk->bd_nob_transferred != requested_nob) {
676 CERROR("Unexpected # bytes transferred: %d (requested %d)\n",
677 requested_nob, request->rq_bulk->bd_nob_transferred);
684 static inline int can_merge_pages(struct brw_page *p1, struct brw_page *p2)
686 if (p1->flag != p2->flag) {
687 unsigned mask = ~OBD_BRW_FROM_GRANT;
689 /* warn if we try to combine flags that we don't know to be
691 if ((p1->flag & mask) != (p2->flag & mask))
692 CERROR("is it ok to have flags 0x%x and 0x%x in the "
693 "same brw?\n", p1->flag, p2->flag);
697 return (p1->off + p1->count == p2->off);
701 static obd_count cksum_pages(int nob, obd_count page_count,
702 struct brw_page *pga)
708 LASSERT (page_count > 0);
711 ost_checksum(&cksum, ptr + (pga->off & (PAGE_SIZE - 1)),
712 pga->count > nob ? nob : pga->count);
724 static int osc_brw_prep_request(int cmd, struct obd_import *imp,struct obdo *oa,
725 struct lov_stripe_md *lsm, obd_count page_count,
726 struct brw_page *pga, int *requested_nobp,
727 int *niocountp, struct ptlrpc_request **reqp)
729 struct ptlrpc_request *req;
730 struct ptlrpc_bulk_desc *desc;
731 struct client_obd *cli = &imp->imp_obd->u.cli;
732 struct ost_body *body;
733 struct obd_ioobj *ioobj;
734 struct niobuf_remote *niobuf;
743 opc = ((cmd & OBD_BRW_WRITE) != 0) ? OST_WRITE : OST_READ;
745 for (niocount = i = 1; i < page_count; i++)
746 if (!can_merge_pages(&pga[i - 1], &pga[i]))
749 size[0] = sizeof(*body);
750 size[1] = sizeof(*ioobj);
751 size[2] = niocount * sizeof(*niobuf);
753 req = ptlrpc_prep_req(imp, opc, 3, size, NULL);
757 if (opc == OST_WRITE)
758 desc = ptlrpc_prep_bulk_imp (req, page_count,
759 BULK_GET_SOURCE, OST_BULK_PORTAL);
761 desc = ptlrpc_prep_bulk_imp (req, page_count,
762 BULK_PUT_SINK, OST_BULK_PORTAL);
764 GOTO(out, rc = -ENOMEM);
765 /* NB request now owns desc and will free it when it gets freed */
767 body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof(*body));
768 ioobj = lustre_msg_buf(req->rq_reqmsg, 1, sizeof(*ioobj));
769 niobuf = lustre_msg_buf(req->rq_reqmsg, 2, niocount * sizeof(*niobuf));
771 memcpy(&body->oa, oa, sizeof(*oa));
773 obdo_to_ioobj(oa, ioobj);
774 ioobj->ioo_bufcnt = niocount;
776 LASSERT (page_count > 0);
777 for (requested_nob = i = 0; i < page_count; i++, niobuf++) {
778 struct brw_page *pg = &pga[i];
779 struct brw_page *pg_prev = pg - 1;
781 LASSERT(pg->count > 0);
782 LASSERT((pg->off & ~PAGE_MASK) + pg->count <= PAGE_SIZE);
783 LASSERTF(i == 0 || pg->off > pg_prev->off,
784 "i %d p_c %u pg %p [pri %lu ind %lu] off "LPU64
785 " prev_pg %p [pri %lu ind %lu] off "LPU64"\n",
787 pg->pg, pg->pg->private, pg->pg->index, pg->off,
788 pg_prev->pg, pg_prev->pg->private, pg_prev->pg->index,
791 ptlrpc_prep_bulk_page(desc, pg->pg, pg->off & ~PAGE_MASK,
793 requested_nob += pg->count;
795 if (i > 0 && can_merge_pages(pg_prev, pg)) {
797 niobuf->len += pg->count;
799 niobuf->offset = pg->off;
800 niobuf->len = pg->count;
801 niobuf->flags = pg->flag;
805 LASSERT((void *)(niobuf - niocount) ==
806 lustre_msg_buf(req->rq_reqmsg, 2, niocount * sizeof(*niobuf)));
807 osc_announce_cached(cli, &body->oa, opc == OST_WRITE ? requested_nob:0);
808 spin_lock_irqsave(&req->rq_lock, flags);
809 req->rq_no_resend = 1;
810 spin_unlock_irqrestore(&req->rq_lock, flags);
812 /* size[0] still sizeof (*body) */
813 if (opc == OST_WRITE) {
815 body->oa.o_valid |= OBD_MD_FLCKSUM;
816 body->oa.o_cksum = cksum_pages(requested_nob, page_count, pga);
818 /* 1 RC per niobuf */
819 size[1] = sizeof(__u32) * niocount;
820 req->rq_replen = lustre_msg_size(2, size);
822 /* 1 RC for the whole I/O */
823 req->rq_replen = lustre_msg_size(1, size);
826 *niocountp = niocount;
827 *requested_nobp = requested_nob;
832 ptlrpc_req_finished (req);
836 static int osc_brw_fini_request(struct ptlrpc_request *req, struct obdo *oa,
837 int requested_nob, int niocount,
838 obd_count page_count, struct brw_page *pga,
841 struct client_obd *cli = &req->rq_import->imp_obd->u.cli;
842 struct ost_body *body;
848 body = lustre_swab_repbuf(req, 0, sizeof(*body), lustre_swab_ost_body);
850 CERROR ("Can't unpack body\n");
854 osc_update_grant(cli, body);
855 memcpy(oa, &body->oa, sizeof(*oa));
857 if (req->rq_reqmsg->opc == OST_WRITE) {
859 CERROR ("Unexpected +ve rc %d\n", rc);
862 LASSERT (req->rq_bulk->bd_nob == requested_nob);
864 RETURN(check_write_rcs(req, requested_nob, niocount,
868 if (rc > requested_nob) {
869 CERROR("Unexpected rc %d (%d requested)\n", rc, requested_nob);
873 if (rc != req->rq_bulk->bd_nob_transferred) {
874 CERROR ("Unexpected rc %d (%d transferred)\n",
875 rc, req->rq_bulk->bd_nob_transferred);
879 if (rc < requested_nob)
880 handle_short_read(rc, page_count, pga);
883 if (oa->o_valid & OBD_MD_FLCKSUM) {
884 const struct ptlrpc_peer *peer =
885 &req->rq_import->imp_connection->c_peer;
886 static int cksum_counter;
887 obd_count server_cksum = oa->o_cksum;
888 obd_count cksum = cksum_pages(rc, page_count, pga);
889 char str[PTL_NALFMT_SIZE];
891 portals_nid2str(peer->peer_ni->pni_number, peer->peer_nid, str);
894 if (server_cksum != cksum) {
895 CERROR("Bad checksum: server %x, client %x, server NID "
896 LPX64" (%s)\n", server_cksum, cksum,
897 peer->peer_nid, str);
900 } else if ((cksum_counter & (-cksum_counter)) == cksum_counter){
901 CWARN("Checksum %u from "LPX64" (%s) OK: %x\n",
902 cksum_counter, peer->peer_nid, str, cksum);
905 static int cksum_missed;
908 if ((cksum_missed & (-cksum_missed)) == cksum_missed)
909 CERROR("Request checksum %u from "LPX64", no reply\n",
911 req->rq_import->imp_connection->c_peer.peer_nid);
917 static int osc_brw_internal(int cmd, struct obd_export *exp,struct obdo *oa,
918 struct lov_stripe_md *lsm,
919 obd_count page_count, struct brw_page *pga)
923 struct ptlrpc_request *request;
928 rc = osc_brw_prep_request(cmd, class_exp2cliimp(exp), oa, lsm,
929 page_count, pga, &requested_nob, &niocount,
931 /* NB ^ sets rq_no_resend */
936 rc = ptlrpc_queue_wait(request);
938 if (rc == -ETIMEDOUT && request->rq_resend) {
939 DEBUG_REQ(D_HA, request, "BULK TIMEOUT");
940 ptlrpc_req_finished(request);
944 rc = osc_brw_fini_request(request, oa, requested_nob, niocount,
945 page_count, pga, rc);
947 ptlrpc_req_finished(request);
951 static int brw_interpret(struct ptlrpc_request *request,
952 struct osc_brw_async_args *aa, int rc)
954 struct obdo *oa = aa->aa_oa;
955 int requested_nob = aa->aa_requested_nob;
956 int niocount = aa->aa_nio_count;
957 obd_count page_count = aa->aa_page_count;
958 struct brw_page *pga = aa->aa_pga;
961 /* XXX bug 937 here */
962 if (rc == -ETIMEDOUT && request->rq_resend) {
963 DEBUG_REQ(D_HA, request, "BULK TIMEOUT");
964 LBUG(); /* re-send. later. */
968 rc = osc_brw_fini_request(request, oa, requested_nob, niocount,
969 page_count, pga, rc);
973 static int async_internal(int cmd, struct obd_export *exp, struct obdo *oa,
974 struct lov_stripe_md *lsm, obd_count page_count,
975 struct brw_page *pga, struct ptlrpc_request_set *set)
977 struct ptlrpc_request *request;
980 struct osc_brw_async_args *aa;
984 rc = osc_brw_prep_request(cmd, class_exp2cliimp(exp), oa, lsm,
985 page_count, pga, &requested_nob, &nio_count,
987 /* NB ^ sets rq_no_resend */
990 LASSERT(sizeof(*aa) <= sizeof(request->rq_async_args));
991 aa = (struct osc_brw_async_args *)&request->rq_async_args;
993 aa->aa_requested_nob = requested_nob;
994 aa->aa_nio_count = nio_count;
995 aa->aa_page_count = page_count;
998 request->rq_interpret_reply = brw_interpret;
999 ptlrpc_set_add_req(set, request);
1005 #define min_t(type,x,y) \
1006 ({ type __x = (x); type __y = (y); __x < __y ? __x: __y; })
1010 * ugh, we want disk allocation on the target to happen in offset order. we'll
1011 * follow sedgewicks advice and stick to the dead simple shellsort -- it'll do
1012 * fine for our small page arrays and doesn't require allocation. its an
1013 * insertion sort that swaps elements that are strides apart, shrinking the
1014 * stride down until its '1' and the array is sorted.
1016 static void sort_brw_pages(struct brw_page *array, int num)
1019 struct brw_page tmp;
1023 for (stride = 1; stride < num ; stride = (stride * 3) + 1)
1028 for (i = stride ; i < num ; i++) {
1031 while (j >= stride && array[j - stride].off > tmp.off) {
1032 array[j] = array[j - stride];
1037 } while (stride > 1);
1040 /* make sure we the regions we're passing to elan don't violate its '4
1041 * fragments' constraint. portal headers are a fragment, all full
1042 * PAGE_SIZE long pages count as 1 fragment, and each partial page
1043 * counts as a fragment. I think. see bug 934. */
1044 static obd_count check_elan_limit(struct brw_page *pg, obd_count pages)
1047 int saw_whole_frag = 0;
1050 for (i = 0 ; frags_left && i < pages ; pg++, i++) {
1051 if (pg->count == PAGE_SIZE) {
1052 if (!saw_whole_frag) {
1063 static int osc_brw(int cmd, struct obd_export *exp, struct obdo *oa,
1064 struct lov_stripe_md *md, obd_count page_count,
1065 struct brw_page *pga, struct obd_trans_info *oti)
1069 if (cmd == OBD_BRW_CHECK) {
1070 /* The caller just wants to know if there's a chance that this
1071 * I/O can succeed */
1072 struct obd_import *imp = class_exp2cliimp(exp);
1074 if (imp == NULL || imp->imp_invalid)
1079 while (page_count) {
1080 obd_count pages_per_brw;
1083 if (page_count > PTLRPC_MAX_BRW_PAGES)
1084 pages_per_brw = PTLRPC_MAX_BRW_PAGES;
1086 pages_per_brw = page_count;
1088 sort_brw_pages(pga, pages_per_brw);
1089 pages_per_brw = check_elan_limit(pga, pages_per_brw);
1091 rc = osc_brw_internal(cmd, exp, oa, md, pages_per_brw, pga);
1096 page_count -= pages_per_brw;
1097 pga += pages_per_brw;
1102 static int osc_brw_async(int cmd, struct obd_export *exp, struct obdo *oa,
1103 struct lov_stripe_md *md, obd_count page_count,
1104 struct brw_page *pga, struct ptlrpc_request_set *set,
1105 struct obd_trans_info *oti)
1109 if (cmd == OBD_BRW_CHECK) {
1110 /* The caller just wants to know if there's a chance that this
1111 * I/O can succeed */
1112 struct obd_import *imp = class_exp2cliimp(exp);
1114 if (imp == NULL || imp->imp_invalid)
1119 while (page_count) {
1120 obd_count pages_per_brw;
1123 if (page_count > PTLRPC_MAX_BRW_PAGES)
1124 pages_per_brw = PTLRPC_MAX_BRW_PAGES;
1126 pages_per_brw = page_count;
1128 sort_brw_pages(pga, pages_per_brw);
1129 pages_per_brw = check_elan_limit(pga, pages_per_brw);
1131 rc = async_internal(cmd, exp, oa, md, pages_per_brw, pga, set);
1136 page_count -= pages_per_brw;
1137 pga += pages_per_brw;
1142 static void osc_check_rpcs(struct client_obd *cli);
1143 static void osc_exit_cache(struct client_obd *cli, struct osc_async_page *oap,
1145 static void loi_list_maint(struct client_obd *cli, struct lov_oinfo *loi);
1146 static void lop_update_pending(struct client_obd *cli,
1147 struct loi_oap_pages *lop, int cmd, int delta);
1149 /* this is called when a sync waiter receives an interruption. Its job is to
1150 * get the caller woken as soon as possible. If its page hasn't been put in an
1151 * rpc yet it can dequeue immediately. Otherwise it has to mark the rpc as
1152 * desiring interruption which will forcefully complete the rpc once the rpc
1154 static void osc_occ_interrupted(struct oig_callback_context *occ)
1156 struct osc_async_page *oap;
1157 struct loi_oap_pages *lop;
1158 struct lov_oinfo *loi;
1161 /* XXX member_of() */
1162 oap = list_entry(occ, struct osc_async_page, oap_occ);
1164 spin_lock(&oap->oap_cli->cl_loi_list_lock);
1166 oap->oap_interrupted = 1;
1168 /* ok, it's been put in an rpc. */
1169 if (oap->oap_request != NULL) {
1170 ptlrpc_mark_interrupted(oap->oap_request);
1171 ptlrpcd_wake(oap->oap_request);
1175 /* we don't get interruption callbacks until osc_trigger_sync_io()
1176 * has been called and put the sync oaps in the pending/urgent lists.*/
1177 if (!list_empty(&oap->oap_pending_item)) {
1178 list_del_init(&oap->oap_pending_item);
1179 if (oap->oap_async_flags & ASYNC_URGENT)
1180 list_del_init(&oap->oap_urgent_item);
1183 lop = (oap->oap_cmd == OBD_BRW_WRITE) ?
1184 &loi->loi_write_lop : &loi->loi_read_lop;
1185 lop_update_pending(oap->oap_cli, lop, oap->oap_cmd, -1);
1186 loi_list_maint(oap->oap_cli, oap->oap_loi);
1188 oig_complete_one(oap->oap_oig, &oap->oap_occ, 0);
1189 oap->oap_oig = NULL;
1193 spin_unlock(&oap->oap_cli->cl_loi_list_lock);
1196 /* this must be called holding the loi list lock to give coverage to exit_cache,
1197 * async_flag maintenance, and oap_request */
1198 static void osc_ap_completion(struct client_obd *cli, struct obdo *oa,
1199 struct osc_async_page *oap, int sent, int rc)
1201 osc_exit_cache(cli, oap, sent);
1202 oap->oap_async_flags = 0;
1203 oap->oap_interrupted = 0;
1205 if (oap->oap_request != NULL) {
1206 ptlrpc_req_finished(oap->oap_request);
1207 oap->oap_request = NULL;
1210 if (rc == 0 && oa != NULL)
1211 oap->oap_loi->loi_blocks = oa->o_blocks;
1214 oig_complete_one(oap->oap_oig, &oap->oap_occ, rc);
1215 oap->oap_oig = NULL;
1220 oap->oap_caller_ops->ap_completion(oap->oap_caller_data, oap->oap_cmd,
1224 static int brw_interpret_oap(struct ptlrpc_request *request,
1225 struct osc_brw_async_args *aa, int rc)
1227 struct osc_async_page *oap;
1228 struct client_obd *cli;
1229 struct list_head *pos, *n;
1233 rc = osc_brw_fini_request(request, aa->aa_oa, aa->aa_requested_nob,
1234 aa->aa_nio_count, aa->aa_page_count,
1237 CDEBUG(D_INODE, "request %p aa %p rc %d\n", request, aa, rc);
1240 /* in failout recovery we ignore writeback failure and want
1241 * to just tell llite to unlock the page and continue */
1242 if (request->rq_reqmsg->opc == OST_WRITE &&
1243 (cli->cl_import == NULL || cli->cl_import->imp_invalid)) {
1244 CDEBUG(D_INODE, "flipping to rc 0 imp %p inv %d\n",
1246 cli->cl_import ? cli->cl_import->imp_invalid : -1);
1250 spin_lock(&cli->cl_loi_list_lock);
1252 /* We need to decrement before osc_ap_completion->osc_wake_cache_waiters
1253 * is called so we know whether to go to sync BRWs or wait for more
1254 * RPCs to complete */
1255 cli->cl_brw_in_flight--;
1257 /* the caller may re-use the oap after the completion call so
1258 * we need to clean it up a little */
1259 list_for_each_safe(pos, n, &aa->aa_oaps) {
1260 oap = list_entry(pos, struct osc_async_page, oap_rpc_item);
1262 //CDEBUG(D_INODE, "page %p index %lu oap %p\n",
1263 //oap->oap_page, oap->oap_page->index, oap);
1265 list_del_init(&oap->oap_rpc_item);
1266 osc_ap_completion(cli, aa->aa_oa, oap, 1, rc);
1269 osc_wake_cache_waiters(cli);
1270 osc_check_rpcs(cli);
1272 spin_unlock(&cli->cl_loi_list_lock);
1274 obdo_free(aa->aa_oa);
1275 OBD_FREE(aa->aa_pga, aa->aa_page_count * sizeof(struct brw_page));
1280 static struct ptlrpc_request *osc_build_req(struct client_obd *cli,
1281 struct list_head *rpc_list,
1282 int page_count, int cmd)
1284 struct ptlrpc_request *req;
1285 struct brw_page *pga = NULL;
1286 int requested_nob, nio_count;
1287 struct osc_brw_async_args *aa;
1288 struct obdo *oa = NULL;
1289 struct obd_async_page_ops *ops = NULL;
1290 void *caller_data = NULL;
1291 struct list_head *pos;
1294 LASSERT(!list_empty(rpc_list));
1296 OBD_ALLOC(pga, sizeof(*pga) * page_count);
1298 RETURN(ERR_PTR(-ENOMEM));
1302 GOTO(out, req = ERR_PTR(-ENOMEM));
1305 list_for_each(pos, rpc_list) {
1306 struct osc_async_page *oap;
1308 oap = list_entry(pos, struct osc_async_page, oap_rpc_item);
1310 ops = oap->oap_caller_ops;
1311 caller_data = oap->oap_caller_data;
1313 pga[i].off = oap->oap_obj_off + oap->oap_page_off;
1314 pga[i].pg = oap->oap_page;
1315 pga[i].count = oap->oap_count;
1316 pga[i].flag = oap->oap_brw_flags;
1317 CDEBUG(0, "put page %p index %lu oap %p flg %x to pga\n",
1318 pga[i].pg, oap->oap_page->index, oap, pga[i].flag);
1322 /* always get the data for the obdo for the rpc */
1323 LASSERT(ops != NULL);
1324 ops->ap_fill_obdo(caller_data, cmd, oa);
1326 sort_brw_pages(pga, page_count);
1327 rc = osc_brw_prep_request(cmd, cli->cl_import, oa, NULL, page_count,
1328 pga, &requested_nob, &nio_count, &req);
1330 CERROR("prep_req failed: %d\n", rc);
1331 GOTO(out, req = ERR_PTR(rc));
1334 LASSERT(sizeof(*aa) <= sizeof(req->rq_async_args));
1335 aa = (struct osc_brw_async_args *)&req->rq_async_args;
1337 aa->aa_requested_nob = requested_nob;
1338 aa->aa_nio_count = nio_count;
1339 aa->aa_page_count = page_count;
1348 OBD_FREE(pga, sizeof(*pga) * page_count);
1353 static void lop_update_pending(struct client_obd *cli,
1354 struct loi_oap_pages *lop, int cmd, int delta)
1356 lop->lop_num_pending += delta;
1357 if (cmd == OBD_BRW_WRITE)
1358 cli->cl_pending_w_pages += delta;
1360 cli->cl_pending_r_pages += delta;
1363 /* the loi lock is held across this function but it's allowed to release
1364 * and reacquire it during its work */
1365 static int osc_send_oap_rpc(struct client_obd *cli, struct lov_oinfo *loi,
1366 int cmd, struct loi_oap_pages *lop)
1368 struct ptlrpc_request *request;
1369 obd_count page_count = 0;
1370 struct list_head *tmp, *pos;
1371 struct osc_async_page *oap = NULL;
1372 struct osc_brw_async_args *aa;
1373 struct obd_async_page_ops *ops;
1374 LIST_HEAD(rpc_list);
1377 /* first we find the pages we're allowed to work with */
1378 list_for_each_safe(pos, tmp, &lop->lop_pending) {
1379 oap = list_entry(pos, struct osc_async_page, oap_pending_item);
1380 ops = oap->oap_caller_ops;
1382 LASSERT(oap->oap_magic == OAP_MAGIC);
1384 /* in llite being 'ready' equates to the page being locked
1385 * until completion unlocks it. commit_write submits a page
1386 * as not ready because its unlock will happen unconditionally
1387 * as the call returns. if we race with commit_write giving
1388 * us that page we dont' want to create a hole in the page
1389 * stream, so we stop and leave the rpc to be fired by
1390 * another dirtier or kupdated interval (the not ready page
1391 * will still be on the dirty list). we could call in
1392 * at the end of ll_file_write to process the queue again. */
1393 if (!(oap->oap_async_flags & ASYNC_READY)) {
1394 int rc = ops->ap_make_ready(oap->oap_caller_data, cmd);
1396 CDEBUG(D_INODE, "oap %p page %p returned %d "
1397 "instead of ready\n", oap,
1401 /* llite is telling us that the page is still
1402 * in commit_write and that we should try
1403 * and put it in an rpc again later. we
1404 * break out of the loop so we don't create
1405 * a hole in the sequence of pages in the rpc
1410 /* the io isn't needed.. tell the checks
1411 * below to complete the rpc with EINTR */
1412 oap->oap_async_flags |= ASYNC_COUNT_STABLE;
1413 oap->oap_count = -EINTR;
1416 oap->oap_async_flags |= ASYNC_READY;
1419 LASSERTF(0, "oap %p page %p returned %d "
1420 "from make_ready\n", oap,
1428 /* take the page out of our book-keeping */
1429 list_del_init(&oap->oap_pending_item);
1430 lop_update_pending(cli, lop, cmd, -1);
1431 if (!list_empty(&oap->oap_urgent_item))
1432 list_del_init(&oap->oap_urgent_item);
1434 /* ask the caller for the size of the io as the rpc leaves. */
1435 if (!(oap->oap_async_flags & ASYNC_COUNT_STABLE))
1437 ops->ap_refresh_count(oap->oap_caller_data,cmd);
1438 if (oap->oap_count <= 0) {
1439 CDEBUG(D_CACHE, "oap %p count %d, completing\n", oap,
1441 osc_ap_completion(cli, NULL, oap, 0, oap->oap_count);
1445 /* now put the page back in our accounting */
1446 list_add_tail(&oap->oap_rpc_item, &rpc_list);
1447 if (++page_count >= cli->cl_max_pages_per_rpc)
1451 osc_wake_cache_waiters(cli);
1453 if (page_count == 0)
1456 loi_list_maint(cli, loi);
1457 spin_unlock(&cli->cl_loi_list_lock);
1459 request = osc_build_req(cli, &rpc_list, page_count, cmd);
1460 if (IS_ERR(request)) {
1461 /* this should happen rarely and is pretty bad, it makes the
1462 * pending list not follow the dirty order */
1463 spin_lock(&cli->cl_loi_list_lock);
1464 list_for_each_safe(pos, tmp, &rpc_list) {
1465 oap = list_entry(pos, struct osc_async_page,
1467 list_del_init(&oap->oap_rpc_item);
1469 /* queued sync pages can be torn down while the pages
1470 * were between the pending list and the rpc */
1471 if (oap->oap_interrupted) {
1472 CDEBUG(D_INODE, "oap %p interrupted\n", oap);
1473 osc_ap_completion(cli, NULL, oap, 0,
1478 /* put the page back in the loi/lop lists */
1479 list_add_tail(&oap->oap_pending_item,
1481 lop_update_pending(cli, lop, cmd, 1);
1482 if (oap->oap_async_flags & ASYNC_URGENT)
1483 list_add(&oap->oap_urgent_item,
1486 loi_list_maint(cli, loi);
1487 RETURN(PTR_ERR(request));
1490 LASSERT(sizeof(*aa) <= sizeof(request->rq_async_args));
1491 aa = (struct osc_brw_async_args *)&request->rq_async_args;
1492 INIT_LIST_HEAD(&aa->aa_oaps);
1493 list_splice(&rpc_list, &aa->aa_oaps);
1494 INIT_LIST_HEAD(&rpc_list);
1497 if (cmd == OBD_BRW_READ) {
1498 lprocfs_oh_tally_log2(&cli->cl_read_page_hist, page_count);
1499 lprocfs_oh_tally(&cli->cl_read_rpc_hist, cli->cl_brw_in_flight);
1501 lprocfs_oh_tally_log2(&cli->cl_write_page_hist, page_count);
1502 lprocfs_oh_tally(&cli->cl_write_rpc_hist,
1503 cli->cl_brw_in_flight);
1507 spin_lock(&cli->cl_loi_list_lock);
1509 cli->cl_brw_in_flight++;
1510 /* queued sync pages can be torn down while the pages
1511 * were between the pending list and the rpc */
1512 list_for_each(pos, &aa->aa_oaps) {
1513 oap = list_entry(pos, struct osc_async_page, oap_rpc_item);
1514 if (oap->oap_interrupted) {
1515 CDEBUG(D_INODE, "oap %p in req %p interrupted\n",
1517 ptlrpc_mark_interrupted(request);
1522 CDEBUG(D_INODE, "req %p: %d pages, aa %p. now %d in flight\n", request,
1523 page_count, aa, cli->cl_brw_in_flight);
1525 oap->oap_request = ptlrpc_request_addref(request);
1526 request->rq_interpret_reply = brw_interpret_oap;
1527 ptlrpcd_add_req(request);
1531 static int lop_makes_rpc(struct client_obd *cli, struct loi_oap_pages *lop,
1537 if (lop->lop_num_pending == 0)
1540 /* if we have an invalid import we want to drain the queued pages
1541 * by forcing them through rpcs that immediately fail and complete
1542 * the pages. recovery relies on this to empty the queued pages
1543 * before canceling the locks and evicting down the llite pages */
1544 if (cli->cl_import == NULL || cli->cl_import->imp_invalid)
1547 /* stream rpcs in queue order as long as as there is an urgent page
1548 * queued. this is our cheap solution for good batching in the case
1549 * where writepage marks some random page in the middle of the file as
1550 * urgent because of, say, memory pressure */
1551 if (!list_empty(&lop->lop_urgent))
1554 /* fire off rpcs when we have 'optimal' rpcs as tuned for the wire. */
1555 optimal = cli->cl_max_pages_per_rpc;
1556 if (cmd == OBD_BRW_WRITE) {
1557 /* trigger a write rpc stream as long as there are dirtiers
1558 * waiting for space. as they're waiting, they're not going to
1559 * create more pages to coallesce with what's waiting.. */
1560 if (!list_empty(&cli->cl_cache_waiters))
1563 /* *2 to avoid triggering rpcs that would want to include pages
1564 * that are being queued but which can't be made ready until
1565 * the queuer finishes with the page. this is a wart for
1566 * llite::commit_write() */
1569 if (lop->lop_num_pending >= optimal)
1575 static void on_list(struct list_head *item, struct list_head *list,
1578 if (list_empty(item) && should_be_on)
1579 list_add_tail(item, list);
1580 else if (!list_empty(item) && !should_be_on)
1581 list_del_init(item);
1584 /* maintain the loi's cli list membership invariants so that osc_send_oap_rpc
1585 * can find pages to build into rpcs quickly */
1586 static void loi_list_maint(struct client_obd *cli, struct lov_oinfo *loi)
1588 on_list(&loi->loi_cli_item, &cli->cl_loi_ready_list,
1589 lop_makes_rpc(cli, &loi->loi_write_lop, OBD_BRW_WRITE) ||
1590 lop_makes_rpc(cli, &loi->loi_read_lop, OBD_BRW_READ));
1592 on_list(&loi->loi_write_item, &cli->cl_loi_write_list,
1593 loi->loi_write_lop.lop_num_pending);
1595 on_list(&loi->loi_read_item, &cli->cl_loi_read_list,
1596 loi->loi_read_lop.lop_num_pending);
1599 #define LOI_DEBUG(LOI, STR, args...) \
1600 CDEBUG(D_INODE, "loi ready %d wr %d:%d rd %d:%d " STR, \
1601 !list_empty(&(LOI)->loi_cli_item), \
1602 (LOI)->loi_write_lop.lop_num_pending, \
1603 !list_empty(&(LOI)->loi_write_lop.lop_urgent), \
1604 (LOI)->loi_read_lop.lop_num_pending, \
1605 !list_empty(&(LOI)->loi_read_lop.lop_urgent), \
1608 struct lov_oinfo *osc_next_loi(struct client_obd *cli)
1611 /* first return all objects which we already know to have
1612 * pages ready to be stuffed into rpcs */
1613 if (!list_empty(&cli->cl_loi_ready_list))
1614 RETURN(list_entry(cli->cl_loi_ready_list.next,
1615 struct lov_oinfo, loi_cli_item));
1617 /* then if we have cache waiters, return all objects with queued
1618 * writes. This is especially important when many small files
1619 * have filled up the cache and not been fired into rpcs because
1620 * they don't pass the nr_pending/object threshhold */
1621 if (!list_empty(&cli->cl_cache_waiters) &&
1622 !list_empty(&cli->cl_loi_write_list))
1623 RETURN(list_entry(cli->cl_loi_write_list.next,
1624 struct lov_oinfo, loi_write_item));
1626 /* then return all queued objects when we have an invalid import
1627 * so that they get flushed */
1628 if (cli->cl_import == NULL || cli->cl_import->imp_invalid) {
1629 if (!list_empty(&cli->cl_loi_write_list))
1630 RETURN(list_entry(cli->cl_loi_write_list.next,
1631 struct lov_oinfo, loi_write_item));
1632 if (!list_empty(&cli->cl_loi_read_list))
1633 RETURN(list_entry(cli->cl_loi_read_list.next,
1634 struct lov_oinfo, loi_read_item));
1639 /* called with the loi list lock held */
1640 static void osc_check_rpcs(struct client_obd *cli)
1642 struct lov_oinfo *loi;
1643 int rc = 0, race_counter = 0;
1646 while ((loi = osc_next_loi(cli)) != NULL) {
1647 LOI_DEBUG(loi, "%d in flight\n", cli->cl_brw_in_flight);
1649 if (cli->cl_brw_in_flight >= cli->cl_max_rpcs_in_flight)
1652 /* attempt some read/write balancing by alternating between
1653 * reads and writes in an object. The makes_rpc checks here
1654 * would be redundant if we were getting read/write work items
1655 * instead of objects. we don't want send_oap_rpc to drain a
1656 * partial read pending queue when we're given this object to
1657 * do io on writes while there are cache waiters */
1658 if (lop_makes_rpc(cli, &loi->loi_write_lop, OBD_BRW_WRITE)) {
1659 rc = osc_send_oap_rpc(cli, loi, OBD_BRW_WRITE,
1660 &loi->loi_write_lop);
1668 if (lop_makes_rpc(cli, &loi->loi_read_lop, OBD_BRW_READ)) {
1669 rc = osc_send_oap_rpc(cli, loi, OBD_BRW_READ,
1670 &loi->loi_read_lop);
1679 /* attempt some inter-object balancing by issueing rpcs
1680 * for each object in turn */
1681 if (!list_empty(&loi->loi_cli_item))
1682 list_del_init(&loi->loi_cli_item);
1683 if (!list_empty(&loi->loi_write_item))
1684 list_del_init(&loi->loi_write_item);
1685 if (!list_empty(&loi->loi_read_item))
1686 list_del_init(&loi->loi_read_item);
1688 loi_list_maint(cli, loi);
1690 /* send_oap_rpc fails with 0 when make_ready tells it to
1691 * back off. llite's make_ready does this when it tries
1692 * to lock a page queued for write that is already locked.
1693 * we want to try sending rpcs from many objects, but we
1694 * don't want to spin failing with 0. */
1695 if (race_counter == 10)
1701 /* we're trying to queue a page in the osc so we're subject to the
1702 * 'cl_dirty_max' limit on the number of pages that can be queued in the osc.
1703 * If the osc's queued pages are already at that limit, then we want to sleep
1704 * until there is space in the osc's queue for us. We also may be waiting for
1705 * write credits from the OST if there are RPCs in flight that may return some
1706 * before we fall back to sync writes.
1708 * We need this know our allocation was granted in the presence of signals */
1709 static int ocw_granted(struct client_obd *cli, struct osc_cache_waiter *ocw)
1713 spin_lock(&cli->cl_loi_list_lock);
1714 rc = list_empty(&ocw->ocw_entry) || cli->cl_brw_in_flight == 0;
1715 spin_unlock(&cli->cl_loi_list_lock);
1719 /* Caller must hold loi_list_lock - we drop/regain it if we need to wait for
1720 * grant or cache space. */
1721 static int osc_enter_cache(struct client_obd *cli, struct lov_oinfo *loi,
1722 struct osc_async_page *oap)
1724 struct osc_cache_waiter ocw;
1725 struct l_wait_info lwi = { 0 };
1727 CDEBUG(D_CACHE, "dirty: %ld dirty_max: %ld dropped: %lu grant: %lu\n",
1728 cli->cl_dirty, cli->cl_dirty_max, cli->cl_lost_grant,
1729 cli->cl_avail_grant);
1731 if (cli->cl_dirty_max < PAGE_SIZE)
1734 /* Hopefully normal case - cache space and write credits available */
1735 if (cli->cl_dirty + PAGE_SIZE <= cli->cl_dirty_max &&
1736 cli->cl_avail_grant >= PAGE_SIZE) {
1737 /* account for ourselves */
1738 osc_consume_write_grant(cli, oap);
1742 /* Make sure that there are write rpcs in flight to wait for. This
1743 * is a little silly as this object may not have any pending but
1744 * other objects sure might. */
1745 if (cli->cl_brw_in_flight) {
1746 list_add_tail(&ocw.ocw_entry, &cli->cl_cache_waiters);
1747 init_waitqueue_head(&ocw.ocw_waitq);
1751 loi_list_maint(cli, loi);
1752 osc_check_rpcs(cli);
1753 spin_unlock(&cli->cl_loi_list_lock);
1755 CDEBUG(0, "sleeping for cache space\n");
1756 l_wait_event(ocw.ocw_waitq, ocw_granted(cli, &ocw), &lwi);
1758 spin_lock(&cli->cl_loi_list_lock);
1759 if (!list_empty(&ocw.ocw_entry)) {
1760 list_del(&ocw.ocw_entry);
1769 /* the companion to enter_cache, called when an oap is no longer part of the
1770 * dirty accounting.. so writeback completes or truncate happens before writing
1771 * starts. must be called with the loi lock held. */
1772 static void osc_exit_cache(struct client_obd *cli, struct osc_async_page *oap,
1777 if (!(oap->oap_brw_flags & OBD_BRW_FROM_GRANT)) {
1782 oap->oap_brw_flags &= ~OBD_BRW_FROM_GRANT;
1783 cli->cl_dirty -= PAGE_SIZE;
1785 cli->cl_lost_grant += PAGE_SIZE;
1786 CDEBUG(D_CACHE, "lost grant: %lu avail grant: %lu dirty: %lu\n",
1787 cli->cl_lost_grant, cli->cl_avail_grant, cli->cl_dirty);
1793 int osc_prep_async_page(struct obd_export *exp, struct lov_stripe_md *lsm,
1794 struct lov_oinfo *loi, struct page *page,
1795 obd_off offset, struct obd_async_page_ops *ops,
1796 void *data, void **res)
1798 struct osc_async_page *oap;
1801 OBD_ALLOC(oap, sizeof(*oap));
1805 oap->oap_magic = OAP_MAGIC;
1806 oap->oap_cli = &exp->exp_obd->u.cli;
1809 oap->oap_caller_ops = ops;
1810 oap->oap_caller_data = data;
1812 oap->oap_page = page;
1813 oap->oap_obj_off = offset;
1815 INIT_LIST_HEAD(&oap->oap_pending_item);
1816 INIT_LIST_HEAD(&oap->oap_urgent_item);
1817 INIT_LIST_HEAD(&oap->oap_rpc_item);
1819 oap->oap_occ.occ_interrupted = osc_occ_interrupted;
1821 CDEBUG(D_CACHE, "oap %p page %p obj off "LPU64"\n", oap, page, offset);
1826 struct osc_async_page *oap_from_cookie(void *cookie)
1828 struct osc_async_page *oap = cookie;
1829 if (oap->oap_magic != OAP_MAGIC)
1830 return ERR_PTR(-EINVAL);
1834 static int osc_queue_async_io(struct obd_export *exp, struct lov_stripe_md *lsm,
1835 struct lov_oinfo *loi, void *cookie,
1836 int cmd, obd_off off, int count,
1837 obd_flag brw_flags, enum async_flags async_flags)
1839 struct client_obd *cli = &exp->exp_obd->u.cli;
1840 struct osc_async_page *oap;
1841 struct loi_oap_pages *lop;
1845 oap = oap_from_cookie(cookie);
1847 RETURN(PTR_ERR(oap));
1849 if (cli->cl_import == NULL || cli->cl_import->imp_invalid)
1852 if (!list_empty(&oap->oap_pending_item) ||
1853 !list_empty(&oap->oap_urgent_item) ||
1854 !list_empty(&oap->oap_rpc_item))
1858 loi = &lsm->lsm_oinfo[0];
1860 spin_lock(&cli->cl_loi_list_lock);
1863 oap->oap_async_flags = async_flags;
1864 oap->oap_page_off = off;
1865 oap->oap_count = count;
1866 oap->oap_brw_flags = brw_flags;
1868 if (cmd == OBD_BRW_WRITE) {
1869 rc = osc_enter_cache(cli, loi, oap);
1871 spin_unlock(&cli->cl_loi_list_lock);
1874 lop = &loi->loi_write_lop;
1876 lop = &loi->loi_read_lop;
1879 if (oap->oap_async_flags & ASYNC_URGENT)
1880 list_add(&oap->oap_urgent_item, &lop->lop_urgent);
1881 list_add_tail(&oap->oap_pending_item, &lop->lop_pending);
1882 lop_update_pending(cli, lop, cmd, 1);
1884 loi_list_maint(cli, loi);
1886 LOI_DEBUG(loi, "oap %p page %p added for cmd %d\n", oap, oap->oap_page,
1889 osc_check_rpcs(cli);
1890 spin_unlock(&cli->cl_loi_list_lock);
1895 /* aka (~was & now & flag), but this is more clear :) */
1896 #define SETTING(was, now, flag) (!(was & flag) && (now & flag))
1898 static int osc_set_async_flags(struct obd_export *exp,
1899 struct lov_stripe_md *lsm,
1900 struct lov_oinfo *loi, void *cookie,
1901 obd_flag async_flags)
1903 struct client_obd *cli = &exp->exp_obd->u.cli;
1904 struct loi_oap_pages *lop;
1905 struct osc_async_page *oap;
1909 oap = oap_from_cookie(cookie);
1911 RETURN(PTR_ERR(oap));
1913 if (cli->cl_import == NULL || cli->cl_import->imp_invalid)
1917 loi = &lsm->lsm_oinfo[0];
1919 if (oap->oap_cmd == OBD_BRW_WRITE) {
1920 lop = &loi->loi_write_lop;
1922 lop = &loi->loi_read_lop;
1925 spin_lock(&cli->cl_loi_list_lock);
1927 if (list_empty(&oap->oap_pending_item))
1928 GOTO(out, rc = -EINVAL);
1930 if ((oap->oap_async_flags & async_flags) == async_flags)
1933 if (SETTING(oap->oap_async_flags, async_flags, ASYNC_READY))
1934 oap->oap_async_flags |= ASYNC_READY;
1936 if (SETTING(oap->oap_async_flags, async_flags, ASYNC_URGENT)) {
1937 if (list_empty(&oap->oap_rpc_item)) {
1938 list_add(&oap->oap_urgent_item, &lop->lop_urgent);
1939 loi_list_maint(cli, loi);
1943 LOI_DEBUG(loi, "oap %p page %p has flags %x\n", oap, oap->oap_page,
1944 oap->oap_async_flags);
1946 osc_check_rpcs(cli);
1947 spin_unlock(&cli->cl_loi_list_lock);
1951 static int osc_queue_group_io(struct obd_export *exp, struct lov_stripe_md *lsm,
1952 struct lov_oinfo *loi,
1953 struct obd_io_group *oig, void *cookie,
1954 int cmd, obd_off off, int count,
1956 obd_flag async_flags)
1958 struct client_obd *cli = &exp->exp_obd->u.cli;
1959 struct osc_async_page *oap;
1960 struct loi_oap_pages *lop;
1963 oap = oap_from_cookie(cookie);
1965 RETURN(PTR_ERR(oap));
1967 if (cli->cl_import == NULL || cli->cl_import->imp_invalid)
1970 if (!list_empty(&oap->oap_pending_item) ||
1971 !list_empty(&oap->oap_urgent_item) ||
1972 !list_empty(&oap->oap_rpc_item))
1976 loi = &lsm->lsm_oinfo[0];
1978 spin_lock(&cli->cl_loi_list_lock);
1981 oap->oap_page_off = off;
1982 oap->oap_count = count;
1983 oap->oap_brw_flags = brw_flags;
1984 oap->oap_async_flags = async_flags;
1986 if (cmd == OBD_BRW_WRITE)
1987 lop = &loi->loi_write_lop;
1989 lop = &loi->loi_read_lop;
1991 list_add_tail(&oap->oap_pending_item, &lop->lop_pending_group);
1992 if (oap->oap_async_flags & ASYNC_GROUP_SYNC) {
1994 oig_add_one(oig, &oap->oap_occ);
1997 LOI_DEBUG(loi, "oap %p page %p on group pending\n", oap, oap->oap_page);
1999 spin_unlock(&cli->cl_loi_list_lock);
2004 static void osc_group_to_pending(struct client_obd *cli, struct lov_oinfo *loi,
2005 struct loi_oap_pages *lop, int cmd)
2007 struct list_head *pos, *tmp;
2008 struct osc_async_page *oap;
2010 list_for_each_safe(pos, tmp, &lop->lop_pending_group) {
2011 oap = list_entry(pos, struct osc_async_page, oap_pending_item);
2012 list_del(&oap->oap_pending_item);
2013 list_add_tail(&oap->oap_pending_item, &lop->lop_pending);
2014 list_add(&oap->oap_urgent_item, &lop->lop_urgent);
2015 lop_update_pending(cli, lop, cmd, 1);
2017 loi_list_maint(cli, loi);
2020 static int osc_trigger_group_io(struct obd_export *exp,
2021 struct lov_stripe_md *lsm,
2022 struct lov_oinfo *loi,
2023 struct obd_io_group *oig)
2025 struct client_obd *cli = &exp->exp_obd->u.cli;
2029 loi = &lsm->lsm_oinfo[0];
2031 spin_lock(&cli->cl_loi_list_lock);
2033 osc_group_to_pending(cli, loi, &loi->loi_write_lop, OBD_BRW_WRITE);
2034 osc_group_to_pending(cli, loi, &loi->loi_read_lop, OBD_BRW_READ);
2036 osc_check_rpcs(cli);
2037 spin_unlock(&cli->cl_loi_list_lock);
2042 static int osc_teardown_async_page(struct obd_export *exp,
2043 struct lov_stripe_md *lsm,
2044 struct lov_oinfo *loi, void *cookie)
2046 struct client_obd *cli = &exp->exp_obd->u.cli;
2047 struct loi_oap_pages *lop;
2048 struct osc_async_page *oap;
2052 oap = oap_from_cookie(cookie);
2054 RETURN(PTR_ERR(oap));
2057 loi = &lsm->lsm_oinfo[0];
2059 if (oap->oap_cmd == OBD_BRW_WRITE) {
2060 lop = &loi->loi_write_lop;
2062 lop = &loi->loi_read_lop;
2065 spin_lock(&cli->cl_loi_list_lock);
2067 if (!list_empty(&oap->oap_rpc_item))
2068 GOTO(out, rc = -EBUSY);
2070 osc_exit_cache(cli, oap, 0);
2071 osc_wake_cache_waiters(cli);
2073 if (!list_empty(&oap->oap_urgent_item)) {
2074 list_del_init(&oap->oap_urgent_item);
2075 oap->oap_async_flags &= ~ASYNC_URGENT;
2077 if (!list_empty(&oap->oap_pending_item)) {
2078 list_del_init(&oap->oap_pending_item);
2079 lop_update_pending(cli, lop, oap->oap_cmd, -1);
2081 loi_list_maint(cli, loi);
2083 LOI_DEBUG(loi, "oap %p page %p torn down\n", oap, oap->oap_page);
2085 spin_unlock(&cli->cl_loi_list_lock);
2087 OBD_FREE(oap, sizeof(*oap));
2092 /* Note: caller will lock/unlock, and set uptodate on the pages */
2093 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
2094 static int sanosc_brw_read(struct obd_export *exp, struct obdo *oa,
2095 struct lov_stripe_md *lsm, obd_count page_count,
2096 struct brw_page *pga)
2098 struct ptlrpc_request *request = NULL;
2099 struct ost_body *body;
2100 struct niobuf_remote *nioptr;
2101 struct obd_ioobj *iooptr;
2102 int rc, size[3] = {sizeof(*body)}, mapped = 0;
2106 /* XXX does not handle 'new' brw protocol */
2108 size[1] = sizeof(struct obd_ioobj);
2109 size[2] = page_count * sizeof(*nioptr);
2111 request = ptlrpc_prep_req(class_exp2cliimp(exp), OST_SAN_READ, 3,
2116 body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof(*body));
2117 iooptr = lustre_msg_buf(request->rq_reqmsg, 1, sizeof(*iooptr));
2118 nioptr = lustre_msg_buf(request->rq_reqmsg, 2,
2119 sizeof(*nioptr) * page_count);
2121 memcpy(&body->oa, oa, sizeof(body->oa));
2123 obdo_to_ioobj(oa, iooptr);
2124 iooptr->ioo_bufcnt = page_count;
2126 for (mapped = 0; mapped < page_count; mapped++, nioptr++) {
2127 LASSERT(PageLocked(pga[mapped].pg));
2128 LASSERT(mapped == 0 || pga[mapped].off > pga[mapped - 1].off);
2130 nioptr->offset = pga[mapped].off;
2131 nioptr->len = pga[mapped].count;
2132 nioptr->flags = pga[mapped].flag;
2135 size[1] = page_count * sizeof(*nioptr);
2136 request->rq_replen = lustre_msg_size(2, size);
2138 rc = ptlrpc_queue_wait(request);
2142 body = lustre_swab_repbuf(request, 0, sizeof(*body),
2143 lustre_swab_ost_body);
2145 CERROR("Can't unpack body\n");
2146 GOTO(out_req, rc = -EPROTO);
2149 memcpy(oa, &body->oa, sizeof(*oa));
2151 swab = lustre_msg_swabbed(request->rq_repmsg);
2152 LASSERT_REPSWAB(request, 1);
2153 nioptr = lustre_msg_buf(request->rq_repmsg, 1, size[1]);
2155 /* nioptr missing or short */
2156 GOTO(out_req, rc = -EPROTO);
2160 for (mapped = 0; mapped < page_count; mapped++, nioptr++) {
2161 struct page *page = pga[mapped].pg;
2162 struct buffer_head *bh;
2166 lustre_swab_niobuf_remote (nioptr);
2168 /* got san device associated */
2169 LASSERT(exp->exp_obd != NULL);
2170 dev = exp->exp_obd->u.cli.cl_sandev;
2173 if (!nioptr->offset) {
2174 CDEBUG(D_PAGE, "hole at ino %lu; index %ld\n",
2175 page->mapping->host->i_ino,
2177 memset(page_address(page), 0, PAGE_SIZE);
2181 if (!page->buffers) {
2182 create_empty_buffers(page, dev, PAGE_SIZE);
2185 clear_bit(BH_New, &bh->b_state);
2186 set_bit(BH_Mapped, &bh->b_state);
2187 bh->b_blocknr = (unsigned long)nioptr->offset;
2189 clear_bit(BH_Uptodate, &bh->b_state);
2191 ll_rw_block(READ, 1, &bh);
2195 /* if buffer already existed, it must be the
2196 * one we mapped before, check it */
2197 LASSERT(!test_bit(BH_New, &bh->b_state));
2198 LASSERT(test_bit(BH_Mapped, &bh->b_state));
2199 LASSERT(bh->b_blocknr == (unsigned long)nioptr->offset);
2201 /* wait it's io completion */
2202 if (test_bit(BH_Lock, &bh->b_state))
2205 if (!test_bit(BH_Uptodate, &bh->b_state))
2206 ll_rw_block(READ, 1, &bh);
2210 /* must do syncronous write here */
2212 if (!buffer_uptodate(bh)) {
2220 ptlrpc_req_finished(request);
2224 static int sanosc_brw_write(struct obd_export *exp, struct obdo *oa,
2225 struct lov_stripe_md *lsm, obd_count page_count,
2226 struct brw_page *pga)
2228 struct ptlrpc_request *request = NULL;
2229 struct ost_body *body;
2230 struct niobuf_remote *nioptr;
2231 struct obd_ioobj *iooptr;
2232 int rc, size[3] = {sizeof(*body)}, mapped = 0;
2236 size[1] = sizeof(struct obd_ioobj);
2237 size[2] = page_count * sizeof(*nioptr);
2239 request = ptlrpc_prep_req(class_exp2cliimp(exp), OST_SAN_WRITE,
2244 body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof (*body));
2245 iooptr = lustre_msg_buf(request->rq_reqmsg, 1, sizeof (*iooptr));
2246 nioptr = lustre_msg_buf(request->rq_reqmsg, 2,
2247 sizeof (*nioptr) * page_count);
2249 memcpy(&body->oa, oa, sizeof(body->oa));
2251 obdo_to_ioobj(oa, iooptr);
2252 iooptr->ioo_bufcnt = page_count;
2255 for (mapped = 0; mapped < page_count; mapped++, nioptr++) {
2256 LASSERT(PageLocked(pga[mapped].pg));
2257 LASSERT(mapped == 0 || pga[mapped].off > pga[mapped - 1].off);
2259 nioptr->offset = pga[mapped].off;
2260 nioptr->len = pga[mapped].count;
2261 nioptr->flags = pga[mapped].flag;
2264 size[1] = page_count * sizeof(*nioptr);
2265 request->rq_replen = lustre_msg_size(2, size);
2267 rc = ptlrpc_queue_wait(request);
2271 swab = lustre_msg_swabbed (request->rq_repmsg);
2272 LASSERT_REPSWAB (request, 1);
2273 nioptr = lustre_msg_buf(request->rq_repmsg, 1, size[1]);
2275 CERROR("absent/short niobuf array\n");
2276 GOTO(out_req, rc = -EPROTO);
2280 for (mapped = 0; mapped < page_count; mapped++, nioptr++) {
2281 struct page *page = pga[mapped].pg;
2282 struct buffer_head *bh;
2286 lustre_swab_niobuf_remote (nioptr);
2288 /* got san device associated */
2289 LASSERT(exp->exp_obd != NULL);
2290 dev = exp->exp_obd->u.cli.cl_sandev;
2292 if (!page->buffers) {
2293 create_empty_buffers(page, dev, PAGE_SIZE);
2296 LASSERT(!test_bit(BH_New, &page->buffers->b_state));
2297 LASSERT(test_bit(BH_Mapped, &page->buffers->b_state));
2298 LASSERT(page->buffers->b_blocknr ==
2299 (unsigned long)nioptr->offset);
2305 /* if buffer locked, wait it's io completion */
2306 if (test_bit(BH_Lock, &bh->b_state))
2309 clear_bit(BH_New, &bh->b_state);
2310 set_bit(BH_Mapped, &bh->b_state);
2312 /* override the block nr */
2313 bh->b_blocknr = (unsigned long)nioptr->offset;
2315 /* we are about to write it, so set it
2317 * page lock should garentee no race condition here */
2318 set_bit(BH_Uptodate, &bh->b_state);
2319 set_bit(BH_Dirty, &bh->b_state);
2321 ll_rw_block(WRITE, 1, &bh);
2323 /* must do syncronous write here */
2325 if (!buffer_uptodate(bh) || test_bit(BH_Dirty, &bh->b_state)) {
2333 ptlrpc_req_finished(request);
2337 static int sanosc_brw(int cmd, struct obd_export *exp, struct obdo *oa,
2338 struct lov_stripe_md *lsm, obd_count page_count,
2339 struct brw_page *pga, struct obd_trans_info *oti)
2343 while (page_count) {
2344 obd_count pages_per_brw;
2347 if (page_count > PTLRPC_MAX_BRW_PAGES)
2348 pages_per_brw = PTLRPC_MAX_BRW_PAGES;
2350 pages_per_brw = page_count;
2352 if (cmd & OBD_BRW_WRITE)
2353 rc = sanosc_brw_write(exp, oa, lsm, pages_per_brw,pga);
2355 rc = sanosc_brw_read(exp, oa, lsm, pages_per_brw, pga);
2360 page_count -= pages_per_brw;
2361 pga += pages_per_brw;
2368 static void osc_set_data_with_check(struct lustre_handle *lockh, void *data)
2370 struct ldlm_lock *lock = ldlm_handle2lock(lockh);
2372 LASSERT(lock != NULL);
2373 l_lock(&lock->l_resource->lr_namespace->ns_lock);
2375 if (lock->l_ast_data && lock->l_ast_data != data) {
2376 struct inode *new_inode = data;
2377 struct inode *old_inode = lock->l_ast_data;
2378 LASSERTF(old_inode->i_state & I_FREEING,
2379 "Found existing inode %p/%lu/%u state %lu in lock: "
2380 "setting data to %p/%lu/%u\n", old_inode,
2381 old_inode->i_ino, old_inode->i_generation,
2383 new_inode, new_inode->i_ino, new_inode->i_generation);
2386 lock->l_ast_data = data;
2387 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
2388 LDLM_LOCK_PUT(lock);
2391 static int osc_change_cbdata(struct obd_export *exp, struct lov_stripe_md *lsm,
2392 ldlm_iterator_t replace, void *data)
2394 struct ldlm_res_id res_id = { .name = {lsm->lsm_object_id} };
2395 struct obd_device *obd = class_exp2obd(exp);
2397 ldlm_change_cbdata(obd->obd_namespace, &res_id, replace, data);
2401 static int osc_enqueue(struct obd_export *exp, struct lov_stripe_md *lsm,
2402 __u32 type, ldlm_policy_data_t *policy, __u32 mode,
2403 int *flags, void *bl_cb, void *cp_cb, void *gl_cb,
2404 void *data, __u32 lvb_len, void *lvb_swabber,
2405 struct lustre_handle *lockh)
2407 struct ldlm_res_id res_id = { .name = {lsm->lsm_object_id} };
2408 struct obd_device *obd = exp->exp_obd;
2413 /* Filesystem lock extents are extended to page boundaries so that
2414 * dealing with the page cache is a little smoother. */
2415 policy->l_extent.start -= policy->l_extent.start & ~PAGE_MASK;
2416 policy->l_extent.end |= ~PAGE_MASK;
2418 if (lsm->lsm_oinfo->loi_kms_valid == 0)
2421 /* Next, search for already existing extent locks that will cover us */
2422 rc = ldlm_lock_match(obd->obd_namespace, 0, &res_id, type, policy, mode,
2425 osc_set_data_with_check(lockh, data);
2426 if (*flags & LDLM_FL_HAS_INTENT) {
2427 /* I would like to be able to ASSERT here that rss <=
2428 * kms, but I can't, for reasons which are explained in
2431 /* We already have a lock, and it's referenced */
2435 /* If we're trying to read, we also search for an existing PW lock. The
2436 * VFS and page cache already protect us locally, so lots of readers/
2437 * writers can share a single PW lock.
2439 * There are problems with conversion deadlocks, so instead of
2440 * converting a read lock to a write lock, we'll just enqueue a new
2443 * At some point we should cancel the read lock instead of making them
2444 * send us a blocking callback, but there are problems with canceling
2445 * locks out from other users right now, too. */
2447 if (mode == LCK_PR) {
2448 rc = ldlm_lock_match(obd->obd_namespace, 0, &res_id, type,
2449 policy, LCK_PW, lockh);
2451 /* FIXME: This is not incredibly elegant, but it might
2452 * be more elegant than adding another parameter to
2453 * lock_match. I want a second opinion. */
2454 ldlm_lock_addref(lockh, LCK_PR);
2455 ldlm_lock_decref(lockh, LCK_PW);
2456 osc_set_data_with_check(lockh, data);
2462 rc = ldlm_cli_enqueue(exp, NULL, obd->obd_namespace, res_id, type,
2463 policy, mode, flags, bl_cb, cp_cb, gl_cb, data,
2464 &lvb, sizeof(lvb), lustre_swab_ost_lvb, lockh);
2466 if ((*flags & LDLM_FL_HAS_INTENT && rc == ELDLM_LOCK_ABORTED) || !rc) {
2467 CDEBUG(D_INODE, "received kms == "LPU64", blocks == "LPU64"\n",
2468 lvb.lvb_size, lvb.lvb_blocks);
2469 lsm->lsm_oinfo->loi_rss = lvb.lvb_size;
2470 lsm->lsm_oinfo->loi_blocks = lvb.lvb_blocks;
2476 static int osc_match(struct obd_export *exp, struct lov_stripe_md *lsm,
2477 __u32 type, ldlm_policy_data_t *policy, __u32 mode,
2478 int *flags, void *data, struct lustre_handle *lockh)
2480 struct ldlm_res_id res_id = { .name = {lsm->lsm_object_id} };
2481 struct obd_device *obd = exp->exp_obd;
2485 OBD_FAIL_RETURN(OBD_FAIL_OSC_MATCH, -EIO);
2487 /* Filesystem lock extents are extended to page boundaries so that
2488 * dealing with the page cache is a little smoother */
2489 policy->l_extent.start -= policy->l_extent.start & ~PAGE_MASK;
2490 policy->l_extent.end |= ~PAGE_MASK;
2492 /* Next, search for already existing extent locks that will cover us */
2493 rc = ldlm_lock_match(obd->obd_namespace, *flags, &res_id, type,
2494 policy, mode, lockh);
2496 osc_set_data_with_check(lockh, data);
2499 /* If we're trying to read, we also search for an existing PW lock. The
2500 * VFS and page cache already protect us locally, so lots of readers/
2501 * writers can share a single PW lock. */
2502 if (mode == LCK_PR) {
2503 rc = ldlm_lock_match(obd->obd_namespace, *flags, &res_id, type,
2504 policy, LCK_PW, lockh);
2505 if (rc == 1 && !(*flags & LDLM_FL_TEST_LOCK)) {
2506 /* FIXME: This is not incredibly elegant, but it might
2507 * be more elegant than adding another parameter to
2508 * lock_match. I want a second opinion. */
2509 osc_set_data_with_check(lockh, data);
2510 ldlm_lock_addref(lockh, LCK_PR);
2511 ldlm_lock_decref(lockh, LCK_PW);
2517 static int osc_cancel(struct obd_export *exp, struct lov_stripe_md *md,
2518 __u32 mode, struct lustre_handle *lockh)
2522 if (mode == LCK_GROUP)
2523 ldlm_lock_decref_and_cancel(lockh, mode);
2525 ldlm_lock_decref(lockh, mode);
2530 static int osc_cancel_unused(struct obd_export *exp,
2531 struct lov_stripe_md *lsm, int flags, void *opaque)
2533 struct obd_device *obd = class_exp2obd(exp);
2534 struct ldlm_res_id res_id = { .name = {lsm->lsm_object_id} };
2536 return ldlm_cli_cancel_unused(obd->obd_namespace, &res_id, flags,
2540 static int osc_statfs(struct obd_device *obd, struct obd_statfs *osfs,
2541 unsigned long max_age)
2543 struct obd_statfs *msfs;
2544 struct ptlrpc_request *request;
2545 int rc, size = sizeof(*osfs);
2548 /* We could possibly pass max_age in the request (as an absolute
2549 * timestamp or a "seconds.usec ago") so the target can avoid doing
2550 * extra calls into the filesystem if that isn't necessary (e.g.
2551 * during mount that would help a bit). Having relative timestamps
2552 * is not so great if request processing is slow, while absolute
2553 * timestamps are not ideal because they need time synchronization. */
2554 request = ptlrpc_prep_req(obd->u.cli.cl_import, OST_STATFS,0,NULL,NULL);
2558 request->rq_replen = lustre_msg_size(1, &size);
2559 request->rq_request_portal = OST_CREATE_PORTAL; //XXX FIXME bug 249
2561 rc = ptlrpc_queue_wait(request);
2565 msfs = lustre_swab_repbuf(request, 0, sizeof(*msfs),
2566 lustre_swab_obd_statfs);
2568 CERROR("Can't unpack obd_statfs\n");
2569 GOTO(out, rc = -EPROTO);
2572 memcpy(osfs, msfs, sizeof(*osfs));
2576 ptlrpc_req_finished(request);
2580 /* Retrieve object striping information.
2582 * @lmmu is a pointer to an in-core struct with lmm_ost_count indicating
2583 * the maximum number of OST indices which will fit in the user buffer.
2584 * lmm_magic must be LOV_MAGIC (we only use 1 slot here).
2586 static int osc_getstripe(struct lov_stripe_md *lsm, struct lov_user_md *lump)
2588 struct lov_user_md lum, *lumk;
2595 rc = copy_from_user(&lum, lump, sizeof(lum));
2599 if (lum.lmm_magic != LOV_USER_MAGIC)
2602 if (lum.lmm_stripe_count > 0) {
2603 lum_size = sizeof(lum) + sizeof(lum.lmm_objects[0]);
2604 OBD_ALLOC(lumk, lum_size);
2608 lumk->lmm_objects[0].l_object_id = lsm->lsm_object_id;
2610 lum_size = sizeof(lum);
2614 lumk->lmm_object_id = lsm->lsm_object_id;
2615 lumk->lmm_stripe_count = 1;
2617 if (copy_to_user(lump, lumk, lum_size))
2621 OBD_FREE(lumk, lum_size);
2626 static int osc_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
2627 void *karg, void *uarg)
2629 struct obd_device *obd = exp->exp_obd;
2630 struct obd_ioctl_data *data = karg;
2637 case OBD_IOC_LOV_GET_CONFIG: {
2639 struct lov_desc *desc;
2640 struct obd_uuid uuid;
2644 if (obd_ioctl_getdata(&buf, &len, (void *)uarg))
2645 GOTO(out, err = -EINVAL);
2647 data = (struct obd_ioctl_data *)buf;
2649 if (sizeof(*desc) > data->ioc_inllen1) {
2651 GOTO(out, err = -EINVAL);
2654 if (data->ioc_inllen2 < sizeof(uuid)) {
2656 GOTO(out, err = -EINVAL);
2659 desc = (struct lov_desc *)data->ioc_inlbuf1;
2660 desc->ld_tgt_count = 1;
2661 desc->ld_active_tgt_count = 1;
2662 desc->ld_default_stripe_count = 1;
2663 desc->ld_default_stripe_size = 0;
2664 desc->ld_default_stripe_offset = 0;
2665 desc->ld_pattern = 0;
2666 memcpy(&desc->ld_uuid, &obd->obd_uuid, sizeof(uuid));
2668 memcpy(data->ioc_inlbuf2, &obd->obd_uuid, sizeof(uuid));
2670 err = copy_to_user((void *)uarg, buf, len);
2673 obd_ioctl_freedata(buf, len);
2676 case LL_IOC_LOV_SETSTRIPE:
2677 err = obd_alloc_memmd(exp, karg);
2681 case LL_IOC_LOV_GETSTRIPE:
2682 err = osc_getstripe(karg, uarg);
2684 case OBD_IOC_CLIENT_RECOVER:
2685 err = ptlrpc_recover_import(obd->u.cli.cl_import,
2690 case IOC_OSC_SET_ACTIVE:
2691 err = ptlrpc_set_import_active(obd->u.cli.cl_import,
2695 CDEBUG(D_INODE, "unrecognised ioctl %#x by %s\n", cmd, current->comm);
2696 GOTO(out, err = -ENOTTY);
2703 static int osc_get_info(struct obd_export *exp, obd_count keylen,
2704 void *key, __u32 *vallen, void *val)
2707 if (!vallen || !val)
2710 if (keylen > strlen("lock_to_stripe") &&
2711 strcmp(key, "lock_to_stripe") == 0) {
2712 __u32 *stripe = val;
2713 *vallen = sizeof(*stripe);
2716 } else if (keylen >= strlen("last_id") && strcmp(key, "last_id") == 0) {
2717 struct ptlrpc_request *req;
2719 char *bufs[1] = {key};
2721 req = ptlrpc_prep_req(class_exp2cliimp(exp), OST_GET_INFO, 1,
2726 req->rq_replen = lustre_msg_size(1, vallen);
2727 rc = ptlrpc_queue_wait(req);
2731 reply = lustre_swab_repbuf(req, 0, sizeof(*reply),
2732 lustre_swab_ost_last_id);
2733 if (reply == NULL) {
2734 CERROR("Can't unpack OST last ID\n");
2735 GOTO(out, rc = -EPROTO);
2737 *((obd_id *)val) = *reply;
2739 ptlrpc_req_finished(req);
2745 static int osc_set_info(struct obd_export *exp, obd_count keylen,
2746 void *key, obd_count vallen, void *val)
2748 struct ptlrpc_request *req;
2749 struct obd_device *obd = exp->exp_obd;
2750 struct obd_import *imp = class_exp2cliimp(exp);
2751 struct llog_ctxt *ctxt;
2752 int rc, size = keylen;
2753 char *bufs[1] = {key};
2756 if (keylen == strlen("next_id") &&
2757 memcmp(key, "next_id", strlen("next_id")) == 0) {
2758 if (vallen != sizeof(obd_id))
2760 obd->u.cli.cl_oscc.oscc_next_id = *((obd_id*)val) + 1;
2761 CDEBUG(D_INODE, "%s: set oscc_next_id = "LPU64"\n",
2762 exp->exp_obd->obd_name,
2763 obd->u.cli.cl_oscc.oscc_next_id);
2768 if (keylen == strlen("growth_count") &&
2769 memcmp(key, "growth_count", strlen("growth_count")) == 0) {
2770 if (vallen != sizeof(int))
2772 obd->u.cli.cl_oscc.oscc_grow_count = *((int*)val);
2776 if (keylen == strlen("unlinked") &&
2777 memcmp(key, "unlinked", keylen) == 0) {
2778 struct osc_creator *oscc = &obd->u.cli.cl_oscc;
2779 spin_lock(&oscc->oscc_lock);
2780 oscc->oscc_flags &= ~OSCC_FLAG_NOSPC;
2781 spin_unlock(&oscc->oscc_lock);
2786 if (keylen == strlen("initial_recov") &&
2787 memcmp(key, "initial_recov", strlen("initial_recov")) == 0) {
2788 struct obd_import *imp = exp->exp_obd->u.cli.cl_import;
2789 if (vallen != sizeof(int))
2791 imp->imp_initial_recov = *(int *)val;
2792 CDEBUG(D_HA, "%s: set imp_no_init_recov = %d\n",
2793 exp->exp_obd->obd_name,
2794 imp->imp_initial_recov);
2798 if (keylen < strlen("mds_conn") ||
2799 memcmp(key, "mds_conn", strlen("mds_conn")) != 0)
2803 req = ptlrpc_prep_req(imp, OST_SET_INFO, 1, &size, bufs);
2807 req->rq_replen = lustre_msg_size(0, NULL);
2808 rc = ptlrpc_queue_wait(req);
2809 ptlrpc_req_finished(req);
2811 ctxt = llog_get_context(exp->exp_obd, LLOG_UNLINK_ORIG_CTXT);
2813 rc = llog_initiator_connect(ctxt);
2818 imp->imp_server_timeout = 1;
2819 CDEBUG(D_HA, "pinging OST %s\n", imp->imp_target_uuid.uuid);
2820 imp->imp_pingable = 1;
2826 static struct llog_operations osc_size_repl_logops = {
2827 lop_cancel: llog_obd_repl_cancel
2830 static struct llog_operations osc_unlink_orig_logops;
2831 static int osc_llog_init(struct obd_device *obd, struct obd_device *tgt,
2832 int count, struct llog_catid *catid)
2837 osc_unlink_orig_logops = llog_lvfs_ops;
2838 osc_unlink_orig_logops.lop_setup = llog_obd_origin_setup;
2839 osc_unlink_orig_logops.lop_cleanup = llog_obd_origin_cleanup;
2840 osc_unlink_orig_logops.lop_add = llog_obd_origin_add;
2841 osc_unlink_orig_logops.lop_connect = llog_origin_connect;
2843 rc = llog_setup(obd, LLOG_UNLINK_ORIG_CTXT, tgt, count,
2844 &catid->lci_logid, &osc_unlink_orig_logops);
2848 rc = llog_setup(obd, LLOG_SIZE_REPL_CTXT, tgt, count, NULL,
2849 &osc_size_repl_logops);
2853 static int osc_llog_finish(struct obd_device *obd, int count)
2858 rc = llog_cleanup(llog_get_context(obd, LLOG_UNLINK_ORIG_CTXT));
2862 rc = llog_cleanup(llog_get_context(obd, LLOG_SIZE_REPL_CTXT));
2867 static int osc_connect(struct lustre_handle *exph,
2868 struct obd_device *obd, struct obd_uuid *cluuid)
2872 rc = client_connect_import(exph, obd, cluuid);
2877 static int osc_disconnect(struct obd_export *exp, int flags)
2879 struct obd_device *obd = class_exp2obd(exp);
2880 struct llog_ctxt *ctxt = llog_get_context(obd, LLOG_SIZE_REPL_CTXT);
2883 if (obd->u.cli.cl_conn_count == 1)
2884 /* flush any remaining cancel messages out to the target */
2885 llog_sync(ctxt, exp);
2887 rc = client_disconnect_export(exp, flags);
2891 static int osc_import_event(struct obd_device *obd,
2892 struct obd_import *imp,
2893 enum obd_import_event event)
2895 struct client_obd *cli;
2898 LASSERT(imp->imp_obd == obd);
2901 case IMP_EVENT_DISCON: {
2902 /* Only do this on the MDS OSC's */
2903 if (imp->imp_server_timeout) {
2904 struct osc_creator *oscc = &obd->u.cli.cl_oscc;
2906 spin_lock(&oscc->oscc_lock);
2907 oscc->oscc_flags |= OSCC_FLAG_RECOVERING;
2908 spin_unlock(&oscc->oscc_lock);
2912 case IMP_EVENT_INACTIVE: {
2913 if (obd->obd_observer)
2914 rc = obd_notify(obd->obd_observer, obd, 0);
2917 case IMP_EVENT_INVALIDATE: {
2918 struct ldlm_namespace *ns = obd->obd_namespace;
2922 spin_lock(&cli->cl_loi_list_lock);
2923 cli->cl_avail_grant = 0;
2924 cli->cl_lost_grant = 0;
2925 /* all pages go to failing rpcs due to the invalid import */
2926 osc_check_rpcs(cli);
2927 spin_unlock(&cli->cl_loi_list_lock);
2929 ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
2933 case IMP_EVENT_ACTIVE: {
2934 if (obd->obd_observer)
2935 rc = obd_notify(obd->obd_observer, obd, 1);
2939 CERROR("Unknown import event %d\n", event);
2945 int osc_setup(struct obd_device *obd, obd_count len, void *buf)
2949 rc = ptlrpcd_addref();
2953 rc = client_obd_setup(obd, len, buf);
2962 int osc_cleanup(struct obd_device *obd, int flags)
2966 rc = client_obd_cleanup(obd, flags);
2972 struct obd_ops osc_obd_ops = {
2973 o_owner: THIS_MODULE,
2974 o_attach: osc_attach,
2975 o_detach: osc_detach,
2977 o_cleanup: osc_cleanup,
2978 o_connect: osc_connect,
2979 o_disconnect: osc_disconnect,
2980 o_statfs: osc_statfs,
2981 o_packmd: osc_packmd,
2982 o_unpackmd: osc_unpackmd,
2983 o_create: osc_create,
2984 o_destroy: osc_destroy,
2985 o_getattr: osc_getattr,
2986 o_getattr_async:osc_getattr_async,
2987 o_setattr: osc_setattr,
2989 o_brw_async: osc_brw_async,
2990 .o_prep_async_page = osc_prep_async_page,
2991 .o_queue_async_io = osc_queue_async_io,
2992 .o_set_async_flags = osc_set_async_flags,
2993 .o_queue_group_io = osc_queue_group_io,
2994 .o_trigger_group_io = osc_trigger_group_io,
2995 .o_teardown_async_page = osc_teardown_async_page,
2998 o_enqueue: osc_enqueue,
3000 o_change_cbdata:osc_change_cbdata,
3001 o_cancel: osc_cancel,
3002 o_cancel_unused:osc_cancel_unused,
3003 o_iocontrol: osc_iocontrol,
3004 o_get_info: osc_get_info,
3005 o_set_info: osc_set_info,
3006 o_import_event: osc_import_event,
3007 o_llog_init: osc_llog_init,
3008 o_llog_finish: osc_llog_finish,
3011 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
3012 struct obd_ops sanosc_obd_ops = {
3013 o_owner: THIS_MODULE,
3014 o_attach: osc_attach,
3015 o_detach: osc_detach,
3016 o_cleanup: client_obd_cleanup,
3017 o_connect: osc_connect,
3018 o_disconnect: client_disconnect_export,
3019 o_statfs: osc_statfs,
3020 o_packmd: osc_packmd,
3021 o_unpackmd: osc_unpackmd,
3022 o_create: osc_real_create,
3023 o_destroy: osc_destroy,
3024 o_getattr: osc_getattr,
3025 o_getattr_async:osc_getattr_async,
3026 o_setattr: osc_setattr,
3027 o_setup: client_sanobd_setup,
3031 o_enqueue: osc_enqueue,
3033 o_change_cbdata:osc_change_cbdata,
3034 o_cancel: osc_cancel,
3035 o_cancel_unused:osc_cancel_unused,
3036 o_iocontrol: osc_iocontrol,
3037 o_import_event: osc_import_event,
3038 o_llog_init: osc_llog_init,
3039 o_llog_finish: osc_llog_finish,
3043 int __init osc_init(void)
3045 struct lprocfs_static_vars lvars;
3046 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
3047 struct lprocfs_static_vars sanlvars;
3052 lprocfs_init_vars(osc, &lvars);
3053 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
3054 lprocfs_init_vars(osc, &sanlvars);
3057 rc = class_register_type(&osc_obd_ops, lvars.module_vars,
3062 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
3063 rc = class_register_type(&sanosc_obd_ops, sanlvars.module_vars,
3064 LUSTRE_SANOSC_NAME);
3066 class_unregister_type(LUSTRE_OSC_NAME);
3072 static void /*__exit*/ osc_exit(void)
3074 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
3075 class_unregister_type(LUSTRE_SANOSC_NAME);
3077 class_unregister_type(LUSTRE_OSC_NAME);
3081 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
3082 MODULE_DESCRIPTION("Lustre Object Storage Client (OSC)");
3083 MODULE_LICENSE("GPL");
3085 module_init(osc_init);
3086 module_exit(osc_exit);