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);
856 if (req->rq_reqmsg->opc == OST_WRITE) {
858 CERROR ("Unexpected +ve rc %d\n", rc);
861 LASSERT (req->rq_bulk->bd_nob == requested_nob);
863 RETURN(check_write_rcs(req, requested_nob, niocount,
867 if (rc > requested_nob) {
868 CERROR("Unexpected rc %d (%d requested)\n", rc, requested_nob);
872 if (rc != req->rq_bulk->bd_nob_transferred) {
873 CERROR ("Unexpected rc %d (%d transferred)\n",
874 rc, req->rq_bulk->bd_nob_transferred);
878 if (rc < requested_nob)
879 handle_short_read(rc, page_count, pga);
881 memcpy(oa, &body->oa, sizeof(*oa));
884 if (oa->o_valid & OBD_MD_FLCKSUM) {
885 const struct ptlrpc_peer *peer =
886 &req->rq_import->imp_connection->c_peer;
887 static int cksum_counter;
888 obd_count server_cksum = oa->o_cksum;
889 obd_count cksum = cksum_pages(rc, page_count, pga);
890 char str[PTL_NALFMT_SIZE];
892 portals_nid2str(peer->peer_ni->pni_number, peer->peer_nid, str);
895 if (server_cksum != cksum) {
896 CERROR("Bad checksum: server %x, client %x, server NID "
897 LPX64" (%s)\n", server_cksum, cksum,
898 peer->peer_nid, str);
901 } else if ((cksum_counter & (-cksum_counter)) == cksum_counter){
902 CWARN("Checksum %u from "LPX64" (%s) OK: %x\n",
903 cksum_counter, peer->peer_nid, str, cksum);
906 static int cksum_missed;
909 if ((cksum_missed & (-cksum_missed)) == cksum_missed)
910 CERROR("Request checksum %u from "LPX64", no reply\n",
912 req->rq_import->imp_connection->c_peer.peer_nid);
918 static int osc_brw_internal(int cmd, struct obd_export *exp,struct obdo *oa,
919 struct lov_stripe_md *lsm,
920 obd_count page_count, struct brw_page *pga)
924 struct ptlrpc_request *request;
929 rc = osc_brw_prep_request(cmd, class_exp2cliimp(exp), oa, lsm,
930 page_count, pga, &requested_nob, &niocount,
932 /* NB ^ sets rq_no_resend */
937 rc = ptlrpc_queue_wait(request);
939 if (rc == -ETIMEDOUT && request->rq_resend) {
940 DEBUG_REQ(D_HA, request, "BULK TIMEOUT");
941 ptlrpc_req_finished(request);
945 rc = osc_brw_fini_request(request, oa, requested_nob, niocount,
946 page_count, pga, rc);
948 ptlrpc_req_finished(request);
952 static int brw_interpret(struct ptlrpc_request *request,
953 struct osc_brw_async_args *aa, int rc)
955 struct obdo *oa = aa->aa_oa;
956 int requested_nob = aa->aa_requested_nob;
957 int niocount = aa->aa_nio_count;
958 obd_count page_count = aa->aa_page_count;
959 struct brw_page *pga = aa->aa_pga;
962 /* XXX bug 937 here */
963 if (rc == -ETIMEDOUT && request->rq_resend) {
964 DEBUG_REQ(D_HA, request, "BULK TIMEOUT");
965 LBUG(); /* re-send. later. */
969 rc = osc_brw_fini_request(request, oa, requested_nob, niocount,
970 page_count, pga, rc);
974 static int async_internal(int cmd, struct obd_export *exp, struct obdo *oa,
975 struct lov_stripe_md *lsm, obd_count page_count,
976 struct brw_page *pga, struct ptlrpc_request_set *set)
978 struct ptlrpc_request *request;
981 struct osc_brw_async_args *aa;
985 rc = osc_brw_prep_request(cmd, class_exp2cliimp(exp), oa, lsm,
986 page_count, pga, &requested_nob, &nio_count,
988 /* NB ^ sets rq_no_resend */
991 LASSERT(sizeof(*aa) <= sizeof(request->rq_async_args));
992 aa = (struct osc_brw_async_args *)&request->rq_async_args;
994 aa->aa_requested_nob = requested_nob;
995 aa->aa_nio_count = nio_count;
996 aa->aa_page_count = page_count;
999 request->rq_interpret_reply = brw_interpret;
1000 ptlrpc_set_add_req(set, request);
1006 #define min_t(type,x,y) \
1007 ({ type __x = (x); type __y = (y); __x < __y ? __x: __y; })
1011 * ugh, we want disk allocation on the target to happen in offset order. we'll
1012 * follow sedgewicks advice and stick to the dead simple shellsort -- it'll do
1013 * fine for our small page arrays and doesn't require allocation. its an
1014 * insertion sort that swaps elements that are strides apart, shrinking the
1015 * stride down until its '1' and the array is sorted.
1017 static void sort_brw_pages(struct brw_page *array, int num)
1020 struct brw_page tmp;
1024 for (stride = 1; stride < num ; stride = (stride * 3) + 1)
1029 for (i = stride ; i < num ; i++) {
1032 while (j >= stride && array[j - stride].off > tmp.off) {
1033 array[j] = array[j - stride];
1038 } while (stride > 1);
1041 /* make sure we the regions we're passing to elan don't violate its '4
1042 * fragments' constraint. portal headers are a fragment, all full
1043 * PAGE_SIZE long pages count as 1 fragment, and each partial page
1044 * counts as a fragment. I think. see bug 934. */
1045 static obd_count check_elan_limit(struct brw_page *pg, obd_count pages)
1048 int saw_whole_frag = 0;
1051 for (i = 0 ; frags_left && i < pages ; pg++, i++) {
1052 if (pg->count == PAGE_SIZE) {
1053 if (!saw_whole_frag) {
1064 static int osc_brw(int cmd, struct obd_export *exp, struct obdo *oa,
1065 struct lov_stripe_md *md, obd_count page_count,
1066 struct brw_page *pga, struct obd_trans_info *oti)
1070 if (cmd == OBD_BRW_CHECK) {
1071 /* The caller just wants to know if there's a chance that this
1072 * I/O can succeed */
1073 struct obd_import *imp = class_exp2cliimp(exp);
1075 if (imp == NULL || imp->imp_invalid)
1080 while (page_count) {
1081 obd_count pages_per_brw;
1084 if (page_count > PTLRPC_MAX_BRW_PAGES)
1085 pages_per_brw = PTLRPC_MAX_BRW_PAGES;
1087 pages_per_brw = page_count;
1089 sort_brw_pages(pga, pages_per_brw);
1090 pages_per_brw = check_elan_limit(pga, pages_per_brw);
1092 rc = osc_brw_internal(cmd, exp, oa, md, pages_per_brw, pga);
1097 page_count -= pages_per_brw;
1098 pga += pages_per_brw;
1103 static int osc_brw_async(int cmd, struct obd_export *exp, struct obdo *oa,
1104 struct lov_stripe_md *md, obd_count page_count,
1105 struct brw_page *pga, struct ptlrpc_request_set *set,
1106 struct obd_trans_info *oti)
1110 if (cmd == OBD_BRW_CHECK) {
1111 /* The caller just wants to know if there's a chance that this
1112 * I/O can succeed */
1113 struct obd_import *imp = class_exp2cliimp(exp);
1115 if (imp == NULL || imp->imp_invalid)
1120 while (page_count) {
1121 obd_count pages_per_brw;
1124 if (page_count > PTLRPC_MAX_BRW_PAGES)
1125 pages_per_brw = PTLRPC_MAX_BRW_PAGES;
1127 pages_per_brw = page_count;
1129 sort_brw_pages(pga, pages_per_brw);
1130 pages_per_brw = check_elan_limit(pga, pages_per_brw);
1132 rc = async_internal(cmd, exp, oa, md, pages_per_brw, pga, set);
1137 page_count -= pages_per_brw;
1138 pga += pages_per_brw;
1143 static void osc_check_rpcs(struct client_obd *cli);
1144 static void osc_exit_cache(struct client_obd *cli, struct osc_async_page *oap,
1146 static void loi_list_maint(struct client_obd *cli, struct lov_oinfo *loi);
1147 static void lop_update_pending(struct client_obd *cli,
1148 struct loi_oap_pages *lop, int cmd, int delta);
1150 /* this is called when a sync waiter receives an interruption. Its job is to
1151 * get the caller woken as soon as possible. If its page hasn't been put in an
1152 * rpc yet it can dequeue immediately. Otherwise it has to mark the rpc as
1153 * desiring interruption which will forcefully complete the rpc once the rpc
1155 static void osc_occ_interrupted(struct oig_callback_context *occ)
1157 struct osc_async_page *oap;
1158 struct loi_oap_pages *lop;
1159 struct lov_oinfo *loi;
1162 /* XXX member_of() */
1163 oap = list_entry(occ, struct osc_async_page, oap_occ);
1165 spin_lock(&oap->oap_cli->cl_loi_list_lock);
1167 oap->oap_interrupted = 1;
1169 /* ok, it's been put in an rpc. */
1170 if (oap->oap_request != NULL) {
1171 ptlrpc_mark_interrupted(oap->oap_request);
1172 ptlrpcd_wake(oap->oap_request);
1176 /* we don't get interruption callbacks until osc_trigger_sync_io()
1177 * has been called and put the sync oaps in the pending/urgent lists.*/
1178 if (!list_empty(&oap->oap_pending_item)) {
1179 list_del_init(&oap->oap_pending_item);
1180 if (oap->oap_async_flags & ASYNC_URGENT)
1181 list_del_init(&oap->oap_urgent_item);
1184 lop = (oap->oap_cmd == OBD_BRW_WRITE) ?
1185 &loi->loi_write_lop : &loi->loi_read_lop;
1186 lop_update_pending(oap->oap_cli, lop, oap->oap_cmd, -1);
1187 loi_list_maint(oap->oap_cli, oap->oap_loi);
1189 oig_complete_one(oap->oap_oig, &oap->oap_occ, 0);
1190 oap->oap_oig = NULL;
1194 spin_unlock(&oap->oap_cli->cl_loi_list_lock);
1197 /* this must be called holding the loi list lock to give coverage to exit_cache,
1198 * async_flag maintenance, and oap_request */
1199 static void osc_complete_oap(struct client_obd *cli,
1200 struct osc_async_page *oap, int sent, int rc)
1202 osc_exit_cache(cli, oap, sent);
1203 oap->oap_async_flags = 0;
1204 oap->oap_interrupted = 0;
1206 if (oap->oap_request != NULL) {
1207 ptlrpc_req_finished(oap->oap_request);
1208 oap->oap_request = NULL;
1212 oig_complete_one(oap->oap_oig, &oap->oap_occ, rc);
1213 oap->oap_oig = NULL;
1218 oap->oap_caller_ops->ap_completion(oap->oap_caller_data, oap->oap_cmd,
1222 static int brw_interpret_oap(struct ptlrpc_request *request,
1223 struct osc_brw_async_args *aa, int rc)
1225 struct osc_async_page *oap;
1226 struct client_obd *cli;
1227 struct list_head *pos, *n;
1231 rc = osc_brw_fini_request(request, aa->aa_oa, aa->aa_requested_nob,
1232 aa->aa_nio_count, aa->aa_page_count,
1235 CDEBUG(D_INODE, "request %p aa %p rc %d\n", request, aa, rc);
1238 /* in failout recovery we ignore writeback failure and want
1239 * to just tell llite to unlock the page and continue */
1240 if (request->rq_reqmsg->opc == OST_WRITE &&
1241 (cli->cl_import == NULL || cli->cl_import->imp_invalid)) {
1242 CDEBUG(D_INODE, "flipping to rc 0 imp %p inv %d\n",
1244 cli->cl_import ? cli->cl_import->imp_invalid : -1);
1248 spin_lock(&cli->cl_loi_list_lock);
1250 /* We need to decrement before osc_complete_oap->osc_wake_cache_waiters
1251 * is called so we know whether to go to sync BRWs or wait for more
1252 * RPCs to complete */
1253 cli->cl_brw_in_flight--;
1255 /* the caller may re-use the oap after the completion call so
1256 * we need to clean it up a little */
1257 list_for_each_safe(pos, n, &aa->aa_oaps) {
1258 oap = list_entry(pos, struct osc_async_page, oap_rpc_item);
1260 //CDEBUG(D_INODE, "page %p index %lu oap %p\n",
1261 //oap->oap_page, oap->oap_page->index, oap);
1263 list_del_init(&oap->oap_rpc_item);
1264 osc_complete_oap(cli, oap, 1, rc);
1267 osc_wake_cache_waiters(cli);
1268 osc_check_rpcs(cli);
1270 spin_unlock(&cli->cl_loi_list_lock);
1272 obdo_free(aa->aa_oa);
1273 OBD_FREE(aa->aa_pga, aa->aa_page_count * sizeof(struct brw_page));
1278 static struct ptlrpc_request *osc_build_req(struct client_obd *cli,
1279 struct list_head *rpc_list,
1280 int page_count, int cmd)
1282 struct ptlrpc_request *req;
1283 struct brw_page *pga = NULL;
1284 int requested_nob, nio_count;
1285 struct osc_brw_async_args *aa;
1286 struct obdo *oa = NULL;
1287 struct obd_async_page_ops *ops = NULL;
1288 void *caller_data = NULL;
1289 struct list_head *pos;
1292 LASSERT(!list_empty(rpc_list));
1294 OBD_ALLOC(pga, sizeof(*pga) * page_count);
1296 RETURN(ERR_PTR(-ENOMEM));
1300 GOTO(out, req = ERR_PTR(-ENOMEM));
1303 list_for_each(pos, rpc_list) {
1304 struct osc_async_page *oap;
1306 oap = list_entry(pos, struct osc_async_page, oap_rpc_item);
1308 ops = oap->oap_caller_ops;
1309 caller_data = oap->oap_caller_data;
1311 pga[i].off = oap->oap_obj_off + oap->oap_page_off;
1312 pga[i].pg = oap->oap_page;
1313 pga[i].count = oap->oap_count;
1314 pga[i].flag = oap->oap_brw_flags;
1315 CDEBUG(0, "put page %p index %lu oap %p flg %x to pga\n",
1316 pga[i].pg, oap->oap_page->index, oap, pga[i].flag);
1320 /* always get the data for the obdo for the rpc */
1321 LASSERT(ops != NULL);
1322 ops->ap_fill_obdo(caller_data, cmd, oa);
1324 sort_brw_pages(pga, page_count);
1325 rc = osc_brw_prep_request(cmd, cli->cl_import, oa, NULL, page_count,
1326 pga, &requested_nob, &nio_count, &req);
1328 CERROR("prep_req failed: %d\n", rc);
1329 GOTO(out, req = ERR_PTR(rc));
1332 LASSERT(sizeof(*aa) <= sizeof(req->rq_async_args));
1333 aa = (struct osc_brw_async_args *)&req->rq_async_args;
1335 aa->aa_requested_nob = requested_nob;
1336 aa->aa_nio_count = nio_count;
1337 aa->aa_page_count = page_count;
1346 OBD_FREE(pga, sizeof(*pga) * page_count);
1351 static void lop_update_pending(struct client_obd *cli,
1352 struct loi_oap_pages *lop, int cmd, int delta)
1354 lop->lop_num_pending += delta;
1355 if (cmd == OBD_BRW_WRITE)
1356 cli->cl_pending_w_pages += delta;
1358 cli->cl_pending_r_pages += delta;
1361 /* the loi lock is held across this function but it's allowed to release
1362 * and reacquire it during its work */
1363 static int osc_send_oap_rpc(struct client_obd *cli, struct lov_oinfo *loi,
1364 int cmd, struct loi_oap_pages *lop)
1366 struct ptlrpc_request *request;
1367 obd_count page_count = 0;
1368 struct list_head *tmp, *pos;
1369 struct osc_async_page *oap = NULL;
1370 struct osc_brw_async_args *aa;
1371 struct obd_async_page_ops *ops;
1372 LIST_HEAD(rpc_list);
1375 /* first we find the pages we're allowed to work with */
1376 list_for_each_safe(pos, tmp, &lop->lop_pending) {
1377 oap = list_entry(pos, struct osc_async_page, oap_pending_item);
1378 ops = oap->oap_caller_ops;
1380 LASSERT(oap->oap_magic == OAP_MAGIC);
1382 /* in llite being 'ready' equates to the page being locked
1383 * until completion unlocks it. commit_write submits a page
1384 * as not ready because its unlock will happen unconditionally
1385 * as the call returns. if we race with commit_write giving
1386 * us that page we dont' want to create a hole in the page
1387 * stream, so we stop and leave the rpc to be fired by
1388 * another dirtier or kupdated interval (the not ready page
1389 * will still be on the dirty list). we could call in
1390 * at the end of ll_file_write to process the queue again. */
1391 if (!(oap->oap_async_flags & ASYNC_READY)) {
1392 int rc = ops->ap_make_ready(oap->oap_caller_data, cmd);
1394 CDEBUG(D_INODE, "oap %p page %p returned %d "
1395 "instead of ready\n", oap,
1399 /* llite is telling us that the page is still
1400 * in commit_write and that we should try
1401 * and put it in an rpc again later. we
1402 * break out of the loop so we don't create
1403 * a hole in the sequence of pages in the rpc
1408 /* the io isn't needed.. tell the checks
1409 * below to complete the rpc with EINTR */
1410 oap->oap_async_flags |= ASYNC_COUNT_STABLE;
1411 oap->oap_count = -EINTR;
1414 oap->oap_async_flags |= ASYNC_READY;
1417 LASSERTF(0, "oap %p page %p returned %d "
1418 "from make_ready\n", oap,
1426 /* take the page out of our book-keeping */
1427 list_del_init(&oap->oap_pending_item);
1428 lop_update_pending(cli, lop, cmd, -1);
1429 if (!list_empty(&oap->oap_urgent_item))
1430 list_del_init(&oap->oap_urgent_item);
1432 /* ask the caller for the size of the io as the rpc leaves. */
1433 if (!(oap->oap_async_flags & ASYNC_COUNT_STABLE))
1435 ops->ap_refresh_count(oap->oap_caller_data,cmd);
1436 if (oap->oap_count <= 0) {
1437 CDEBUG(D_CACHE, "oap %p count %d, completing\n", oap,
1439 osc_complete_oap(cli, oap, 0, oap->oap_count);
1443 /* now put the page back in our accounting */
1444 list_add_tail(&oap->oap_rpc_item, &rpc_list);
1445 if (++page_count >= cli->cl_max_pages_per_rpc)
1449 osc_wake_cache_waiters(cli);
1451 if (page_count == 0)
1454 loi_list_maint(cli, loi);
1455 spin_unlock(&cli->cl_loi_list_lock);
1457 request = osc_build_req(cli, &rpc_list, page_count, cmd);
1458 if (IS_ERR(request)) {
1459 /* this should happen rarely and is pretty bad, it makes the
1460 * pending list not follow the dirty order */
1461 spin_lock(&cli->cl_loi_list_lock);
1462 list_for_each_safe(pos, tmp, &rpc_list) {
1463 oap = list_entry(pos, struct osc_async_page,
1465 list_del_init(&oap->oap_rpc_item);
1467 /* queued sync pages can be torn down while the pages
1468 * were between the pending list and the rpc */
1469 if (oap->oap_interrupted) {
1470 CDEBUG(D_INODE, "oap %p interrupted\n", oap);
1471 osc_complete_oap(cli, oap, 0, oap->oap_count);
1475 /* put the page back in the loi/lop lists */
1476 list_add_tail(&oap->oap_pending_item,
1478 lop_update_pending(cli, lop, cmd, 1);
1479 if (oap->oap_async_flags & ASYNC_URGENT)
1480 list_add(&oap->oap_urgent_item,
1483 loi_list_maint(cli, loi);
1484 RETURN(PTR_ERR(request));
1487 LASSERT(sizeof(*aa) <= sizeof(request->rq_async_args));
1488 aa = (struct osc_brw_async_args *)&request->rq_async_args;
1489 INIT_LIST_HEAD(&aa->aa_oaps);
1490 list_splice(&rpc_list, &aa->aa_oaps);
1491 INIT_LIST_HEAD(&rpc_list);
1494 if (cmd == OBD_BRW_READ) {
1495 lprocfs_oh_tally_log2(&cli->cl_read_page_hist, page_count);
1496 lprocfs_oh_tally(&cli->cl_read_rpc_hist, cli->cl_brw_in_flight);
1498 lprocfs_oh_tally_log2(&cli->cl_write_page_hist, page_count);
1499 lprocfs_oh_tally(&cli->cl_write_rpc_hist,
1500 cli->cl_brw_in_flight);
1504 spin_lock(&cli->cl_loi_list_lock);
1506 cli->cl_brw_in_flight++;
1507 /* queued sync pages can be torn down while the pages
1508 * were between the pending list and the rpc */
1509 list_for_each(pos, &aa->aa_oaps) {
1510 oap = list_entry(pos, struct osc_async_page, oap_rpc_item);
1511 if (oap->oap_interrupted) {
1512 CDEBUG(D_INODE, "oap %p in req %p interrupted\n",
1514 ptlrpc_mark_interrupted(request);
1519 CDEBUG(D_INODE, "req %p: %d pages, aa %p. now %d in flight\n", request,
1520 page_count, aa, cli->cl_brw_in_flight);
1522 oap->oap_request = ptlrpc_request_addref(request);
1523 request->rq_interpret_reply = brw_interpret_oap;
1524 ptlrpcd_add_req(request);
1528 static int lop_makes_rpc(struct client_obd *cli, struct loi_oap_pages *lop,
1534 if (lop->lop_num_pending == 0)
1537 /* if we have an invalid import we want to drain the queued pages
1538 * by forcing them through rpcs that immediately fail and complete
1539 * the pages. recovery relies on this to empty the queued pages
1540 * before canceling the locks and evicting down the llite pages */
1541 if (cli->cl_import == NULL || cli->cl_import->imp_invalid)
1544 /* stream rpcs in queue order as long as as there is an urgent page
1545 * queued. this is our cheap solution for good batching in the case
1546 * where writepage marks some random page in the middle of the file as
1547 * urgent because of, say, memory pressure */
1548 if (!list_empty(&lop->lop_urgent))
1551 /* fire off rpcs when we have 'optimal' rpcs as tuned for the wire. */
1552 optimal = cli->cl_max_pages_per_rpc;
1553 if (cmd == OBD_BRW_WRITE) {
1554 /* trigger a write rpc stream as long as there are dirtiers
1555 * waiting for space. as they're waiting, they're not going to
1556 * create more pages to coallesce with what's waiting.. */
1557 if (!list_empty(&cli->cl_cache_waiters))
1560 /* *2 to avoid triggering rpcs that would want to include pages
1561 * that are being queued but which can't be made ready until
1562 * the queuer finishes with the page. this is a wart for
1563 * llite::commit_write() */
1566 if (lop->lop_num_pending >= optimal)
1572 static void on_list(struct list_head *item, struct list_head *list,
1575 if (list_empty(item) && should_be_on)
1576 list_add_tail(item, list);
1577 else if (!list_empty(item) && !should_be_on)
1578 list_del_init(item);
1581 /* maintain the loi's cli list membership invariants so that osc_send_oap_rpc
1582 * can find pages to build into rpcs quickly */
1583 static void loi_list_maint(struct client_obd *cli, struct lov_oinfo *loi)
1585 on_list(&loi->loi_cli_item, &cli->cl_loi_ready_list,
1586 lop_makes_rpc(cli, &loi->loi_write_lop, OBD_BRW_WRITE) ||
1587 lop_makes_rpc(cli, &loi->loi_read_lop, OBD_BRW_READ));
1589 on_list(&loi->loi_write_item, &cli->cl_loi_write_list,
1590 loi->loi_write_lop.lop_num_pending);
1592 on_list(&loi->loi_read_item, &cli->cl_loi_read_list,
1593 loi->loi_read_lop.lop_num_pending);
1596 #define LOI_DEBUG(LOI, STR, args...) \
1597 CDEBUG(D_INODE, "loi ready %d wr %d:%d rd %d:%d " STR, \
1598 !list_empty(&(LOI)->loi_cli_item), \
1599 (LOI)->loi_write_lop.lop_num_pending, \
1600 !list_empty(&(LOI)->loi_write_lop.lop_urgent), \
1601 (LOI)->loi_read_lop.lop_num_pending, \
1602 !list_empty(&(LOI)->loi_read_lop.lop_urgent), \
1605 struct lov_oinfo *osc_next_loi(struct client_obd *cli)
1608 /* first return all objects which we already know to have
1609 * pages ready to be stuffed into rpcs */
1610 if (!list_empty(&cli->cl_loi_ready_list))
1611 RETURN(list_entry(cli->cl_loi_ready_list.next,
1612 struct lov_oinfo, loi_cli_item));
1614 /* then if we have cache waiters, return all objects with queued
1615 * writes. This is especially important when many small files
1616 * have filled up the cache and not been fired into rpcs because
1617 * they don't pass the nr_pending/object threshhold */
1618 if (!list_empty(&cli->cl_cache_waiters) &&
1619 !list_empty(&cli->cl_loi_write_list))
1620 RETURN(list_entry(cli->cl_loi_write_list.next,
1621 struct lov_oinfo, loi_write_item));
1623 /* then return all queued objects when we have an invalid import
1624 * so that they get flushed */
1625 if (cli->cl_import == NULL || cli->cl_import->imp_invalid) {
1626 if (!list_empty(&cli->cl_loi_write_list))
1627 RETURN(list_entry(cli->cl_loi_write_list.next,
1628 struct lov_oinfo, loi_write_item));
1629 if (!list_empty(&cli->cl_loi_read_list))
1630 RETURN(list_entry(cli->cl_loi_read_list.next,
1631 struct lov_oinfo, loi_read_item));
1636 /* called with the loi list lock held */
1637 static void osc_check_rpcs(struct client_obd *cli)
1639 struct lov_oinfo *loi;
1640 int rc = 0, race_counter = 0;
1643 while ((loi = osc_next_loi(cli)) != NULL) {
1644 LOI_DEBUG(loi, "%d in flight\n", cli->cl_brw_in_flight);
1646 if (cli->cl_brw_in_flight >= cli->cl_max_rpcs_in_flight)
1649 /* attempt some read/write balancing by alternating between
1650 * reads and writes in an object. The makes_rpc checks here
1651 * would be redundant if we were getting read/write work items
1652 * instead of objects. we don't want send_oap_rpc to drain a
1653 * partial read pending queue when we're given this object to
1654 * do io on writes while there are cache waiters */
1655 if (lop_makes_rpc(cli, &loi->loi_write_lop, OBD_BRW_WRITE)) {
1656 rc = osc_send_oap_rpc(cli, loi, OBD_BRW_WRITE,
1657 &loi->loi_write_lop);
1665 if (lop_makes_rpc(cli, &loi->loi_read_lop, OBD_BRW_READ)) {
1666 rc = osc_send_oap_rpc(cli, loi, OBD_BRW_READ,
1667 &loi->loi_read_lop);
1676 /* attempt some inter-object balancing by issueing rpcs
1677 * for each object in turn */
1678 if (!list_empty(&loi->loi_cli_item))
1679 list_del_init(&loi->loi_cli_item);
1680 if (!list_empty(&loi->loi_write_item))
1681 list_del_init(&loi->loi_write_item);
1682 if (!list_empty(&loi->loi_read_item))
1683 list_del_init(&loi->loi_read_item);
1685 loi_list_maint(cli, loi);
1687 /* send_oap_rpc fails with 0 when make_ready tells it to
1688 * back off. llite's make_ready does this when it tries
1689 * to lock a page queued for write that is already locked.
1690 * we want to try sending rpcs from many objects, but we
1691 * don't want to spin failing with 0. */
1692 if (race_counter == 10)
1698 /* we're trying to queue a page in the osc so we're subject to the
1699 * 'cl_dirty_max' limit on the number of pages that can be queued in the osc.
1700 * If the osc's queued pages are already at that limit, then we want to sleep
1701 * until there is space in the osc's queue for us. We also may be waiting for
1702 * write credits from the OST if there are RPCs in flight that may return some
1703 * before we fall back to sync writes.
1705 * We need this know our allocation was granted in the presence of signals */
1706 static int ocw_granted(struct client_obd *cli, struct osc_cache_waiter *ocw)
1710 spin_lock(&cli->cl_loi_list_lock);
1711 rc = list_empty(&ocw->ocw_entry) || cli->cl_brw_in_flight == 0;
1712 spin_unlock(&cli->cl_loi_list_lock);
1716 /* Caller must hold loi_list_lock - we drop/regain it if we need to wait for
1717 * grant or cache space. */
1718 static int osc_enter_cache(struct client_obd *cli, struct lov_oinfo *loi,
1719 struct osc_async_page *oap)
1721 struct osc_cache_waiter ocw;
1722 struct l_wait_info lwi = { 0 };
1724 CDEBUG(D_CACHE, "dirty: %ld dirty_max: %ld dropped: %lu grant: %lu\n",
1725 cli->cl_dirty, cli->cl_dirty_max, cli->cl_lost_grant,
1726 cli->cl_avail_grant);
1728 if (cli->cl_dirty_max < PAGE_SIZE)
1731 /* Hopefully normal case - cache space and write credits available */
1732 if (cli->cl_dirty + PAGE_SIZE <= cli->cl_dirty_max &&
1733 cli->cl_avail_grant >= PAGE_SIZE) {
1734 /* account for ourselves */
1735 osc_consume_write_grant(cli, oap);
1739 /* Make sure that there are write rpcs in flight to wait for. This
1740 * is a little silly as this object may not have any pending but
1741 * other objects sure might. */
1742 if (cli->cl_brw_in_flight) {
1743 list_add_tail(&ocw.ocw_entry, &cli->cl_cache_waiters);
1744 init_waitqueue_head(&ocw.ocw_waitq);
1748 loi_list_maint(cli, loi);
1749 osc_check_rpcs(cli);
1750 spin_unlock(&cli->cl_loi_list_lock);
1752 CDEBUG(0, "sleeping for cache space\n");
1753 l_wait_event(ocw.ocw_waitq, ocw_granted(cli, &ocw), &lwi);
1755 spin_lock(&cli->cl_loi_list_lock);
1756 if (!list_empty(&ocw.ocw_entry)) {
1757 list_del(&ocw.ocw_entry);
1766 /* the companion to enter_cache, called when an oap is no longer part of the
1767 * dirty accounting.. so writeback completes or truncate happens before writing
1768 * starts. must be called with the loi lock held. */
1769 static void osc_exit_cache(struct client_obd *cli, struct osc_async_page *oap,
1774 if (!(oap->oap_brw_flags & OBD_BRW_FROM_GRANT)) {
1779 oap->oap_brw_flags &= ~OBD_BRW_FROM_GRANT;
1780 cli->cl_dirty -= PAGE_SIZE;
1782 cli->cl_lost_grant += PAGE_SIZE;
1783 CDEBUG(D_CACHE, "lost grant: %lu avail grant: %lu dirty: %lu\n",
1784 cli->cl_lost_grant, cli->cl_avail_grant, cli->cl_dirty);
1790 int osc_prep_async_page(struct obd_export *exp, struct lov_stripe_md *lsm,
1791 struct lov_oinfo *loi, struct page *page,
1792 obd_off offset, struct obd_async_page_ops *ops,
1793 void *data, void **res)
1795 struct osc_async_page *oap;
1798 OBD_ALLOC(oap, sizeof(*oap));
1802 oap->oap_magic = OAP_MAGIC;
1803 oap->oap_cli = &exp->exp_obd->u.cli;
1806 oap->oap_caller_ops = ops;
1807 oap->oap_caller_data = data;
1809 oap->oap_page = page;
1810 oap->oap_obj_off = offset;
1812 INIT_LIST_HEAD(&oap->oap_pending_item);
1813 INIT_LIST_HEAD(&oap->oap_urgent_item);
1814 INIT_LIST_HEAD(&oap->oap_rpc_item);
1816 oap->oap_occ.occ_interrupted = osc_occ_interrupted;
1818 CDEBUG(D_CACHE, "oap %p page %p obj off "LPU64"\n", oap, page, offset);
1823 struct osc_async_page *oap_from_cookie(void *cookie)
1825 struct osc_async_page *oap = cookie;
1826 if (oap->oap_magic != OAP_MAGIC)
1827 return ERR_PTR(-EINVAL);
1831 static int osc_queue_async_io(struct obd_export *exp, struct lov_stripe_md *lsm,
1832 struct lov_oinfo *loi, void *cookie,
1833 int cmd, obd_off off, int count,
1834 obd_flag brw_flags, enum async_flags async_flags)
1836 struct client_obd *cli = &exp->exp_obd->u.cli;
1837 struct osc_async_page *oap;
1838 struct loi_oap_pages *lop;
1842 oap = oap_from_cookie(cookie);
1844 RETURN(PTR_ERR(oap));
1846 if (cli->cl_import == NULL || cli->cl_import->imp_invalid)
1849 if (!list_empty(&oap->oap_pending_item) ||
1850 !list_empty(&oap->oap_urgent_item) ||
1851 !list_empty(&oap->oap_rpc_item))
1855 loi = &lsm->lsm_oinfo[0];
1857 spin_lock(&cli->cl_loi_list_lock);
1860 oap->oap_async_flags = async_flags;
1861 oap->oap_page_off = off;
1862 oap->oap_count = count;
1863 oap->oap_brw_flags = brw_flags;
1865 if (cmd == OBD_BRW_WRITE) {
1866 rc = osc_enter_cache(cli, loi, oap);
1868 spin_unlock(&cli->cl_loi_list_lock);
1871 lop = &loi->loi_write_lop;
1873 lop = &loi->loi_read_lop;
1876 if (oap->oap_async_flags & ASYNC_URGENT)
1877 list_add(&oap->oap_urgent_item, &lop->lop_urgent);
1878 list_add_tail(&oap->oap_pending_item, &lop->lop_pending);
1879 lop_update_pending(cli, lop, cmd, 1);
1881 loi_list_maint(cli, loi);
1883 LOI_DEBUG(loi, "oap %p page %p added for cmd %d\n", oap, oap->oap_page,
1886 osc_check_rpcs(cli);
1887 spin_unlock(&cli->cl_loi_list_lock);
1892 /* aka (~was & now & flag), but this is more clear :) */
1893 #define SETTING(was, now, flag) (!(was & flag) && (now & flag))
1895 static int osc_set_async_flags(struct obd_export *exp,
1896 struct lov_stripe_md *lsm,
1897 struct lov_oinfo *loi, void *cookie,
1898 obd_flag async_flags)
1900 struct client_obd *cli = &exp->exp_obd->u.cli;
1901 struct loi_oap_pages *lop;
1902 struct osc_async_page *oap;
1906 oap = oap_from_cookie(cookie);
1908 RETURN(PTR_ERR(oap));
1910 if (cli->cl_import == NULL || cli->cl_import->imp_invalid)
1914 loi = &lsm->lsm_oinfo[0];
1916 if (oap->oap_cmd == OBD_BRW_WRITE) {
1917 lop = &loi->loi_write_lop;
1919 lop = &loi->loi_read_lop;
1922 spin_lock(&cli->cl_loi_list_lock);
1924 if (list_empty(&oap->oap_pending_item))
1925 GOTO(out, rc = -EINVAL);
1927 if ((oap->oap_async_flags & async_flags) == async_flags)
1930 if (SETTING(oap->oap_async_flags, async_flags, ASYNC_READY))
1931 oap->oap_async_flags |= ASYNC_READY;
1933 if (SETTING(oap->oap_async_flags, async_flags, ASYNC_URGENT)) {
1934 if (list_empty(&oap->oap_rpc_item)) {
1935 list_add(&oap->oap_urgent_item, &lop->lop_urgent);
1936 loi_list_maint(cli, loi);
1940 LOI_DEBUG(loi, "oap %p page %p has flags %x\n", oap, oap->oap_page,
1941 oap->oap_async_flags);
1943 osc_check_rpcs(cli);
1944 spin_unlock(&cli->cl_loi_list_lock);
1948 static int osc_queue_group_io(struct obd_export *exp, struct lov_stripe_md *lsm,
1949 struct lov_oinfo *loi,
1950 struct obd_io_group *oig, void *cookie,
1951 int cmd, obd_off off, int count,
1953 obd_flag async_flags)
1955 struct client_obd *cli = &exp->exp_obd->u.cli;
1956 struct osc_async_page *oap;
1957 struct loi_oap_pages *lop;
1960 oap = oap_from_cookie(cookie);
1962 RETURN(PTR_ERR(oap));
1964 if (cli->cl_import == NULL || cli->cl_import->imp_invalid)
1967 if (!list_empty(&oap->oap_pending_item) ||
1968 !list_empty(&oap->oap_urgent_item) ||
1969 !list_empty(&oap->oap_rpc_item))
1973 loi = &lsm->lsm_oinfo[0];
1975 spin_lock(&cli->cl_loi_list_lock);
1978 oap->oap_page_off = off;
1979 oap->oap_count = count;
1980 oap->oap_brw_flags = brw_flags;
1981 oap->oap_async_flags = async_flags;
1983 if (cmd == OBD_BRW_WRITE)
1984 lop = &loi->loi_write_lop;
1986 lop = &loi->loi_read_lop;
1988 list_add_tail(&oap->oap_pending_item, &lop->lop_pending_group);
1989 if (oap->oap_async_flags & ASYNC_GROUP_SYNC) {
1991 oig_add_one(oig, &oap->oap_occ);
1994 LOI_DEBUG(loi, "oap %p page %p on group pending\n", oap, oap->oap_page);
1996 spin_unlock(&cli->cl_loi_list_lock);
2001 static void osc_group_to_pending(struct client_obd *cli, struct lov_oinfo *loi,
2002 struct loi_oap_pages *lop, int cmd)
2004 struct list_head *pos, *tmp;
2005 struct osc_async_page *oap;
2007 list_for_each_safe(pos, tmp, &lop->lop_pending_group) {
2008 oap = list_entry(pos, struct osc_async_page, oap_pending_item);
2009 list_del(&oap->oap_pending_item);
2010 list_add_tail(&oap->oap_pending_item, &lop->lop_pending);
2011 list_add(&oap->oap_urgent_item, &lop->lop_urgent);
2012 lop_update_pending(cli, lop, cmd, 1);
2014 loi_list_maint(cli, loi);
2017 static int osc_trigger_group_io(struct obd_export *exp,
2018 struct lov_stripe_md *lsm,
2019 struct lov_oinfo *loi,
2020 struct obd_io_group *oig)
2022 struct client_obd *cli = &exp->exp_obd->u.cli;
2026 loi = &lsm->lsm_oinfo[0];
2028 spin_lock(&cli->cl_loi_list_lock);
2030 osc_group_to_pending(cli, loi, &loi->loi_write_lop, OBD_BRW_WRITE);
2031 osc_group_to_pending(cli, loi, &loi->loi_read_lop, OBD_BRW_READ);
2033 osc_check_rpcs(cli);
2034 spin_unlock(&cli->cl_loi_list_lock);
2039 static int osc_teardown_async_page(struct obd_export *exp,
2040 struct lov_stripe_md *lsm,
2041 struct lov_oinfo *loi, void *cookie)
2043 struct client_obd *cli = &exp->exp_obd->u.cli;
2044 struct loi_oap_pages *lop;
2045 struct osc_async_page *oap;
2049 oap = oap_from_cookie(cookie);
2051 RETURN(PTR_ERR(oap));
2054 loi = &lsm->lsm_oinfo[0];
2056 if (oap->oap_cmd == OBD_BRW_WRITE) {
2057 lop = &loi->loi_write_lop;
2059 lop = &loi->loi_read_lop;
2062 spin_lock(&cli->cl_loi_list_lock);
2064 if (!list_empty(&oap->oap_rpc_item))
2065 GOTO(out, rc = -EBUSY);
2067 osc_exit_cache(cli, oap, 0);
2068 osc_wake_cache_waiters(cli);
2070 if (!list_empty(&oap->oap_urgent_item)) {
2071 list_del_init(&oap->oap_urgent_item);
2072 oap->oap_async_flags &= ~ASYNC_URGENT;
2074 if (!list_empty(&oap->oap_pending_item)) {
2075 list_del_init(&oap->oap_pending_item);
2076 lop_update_pending(cli, lop, oap->oap_cmd, -1);
2078 loi_list_maint(cli, loi);
2080 LOI_DEBUG(loi, "oap %p page %p torn down\n", oap, oap->oap_page);
2082 spin_unlock(&cli->cl_loi_list_lock);
2084 OBD_FREE(oap, sizeof(*oap));
2089 /* Note: caller will lock/unlock, and set uptodate on the pages */
2090 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
2091 static int sanosc_brw_read(struct obd_export *exp, struct obdo *oa,
2092 struct lov_stripe_md *lsm, obd_count page_count,
2093 struct brw_page *pga)
2095 struct ptlrpc_request *request = NULL;
2096 struct ost_body *body;
2097 struct niobuf_remote *nioptr;
2098 struct obd_ioobj *iooptr;
2099 int rc, size[3] = {sizeof(*body)}, mapped = 0;
2103 /* XXX does not handle 'new' brw protocol */
2105 size[1] = sizeof(struct obd_ioobj);
2106 size[2] = page_count * sizeof(*nioptr);
2108 request = ptlrpc_prep_req(class_exp2cliimp(exp), OST_SAN_READ, 3,
2113 body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof(*body));
2114 iooptr = lustre_msg_buf(request->rq_reqmsg, 1, sizeof(*iooptr));
2115 nioptr = lustre_msg_buf(request->rq_reqmsg, 2,
2116 sizeof(*nioptr) * page_count);
2118 memcpy(&body->oa, oa, sizeof(body->oa));
2120 obdo_to_ioobj(oa, iooptr);
2121 iooptr->ioo_bufcnt = page_count;
2123 for (mapped = 0; mapped < page_count; mapped++, nioptr++) {
2124 LASSERT(PageLocked(pga[mapped].pg));
2125 LASSERT(mapped == 0 || pga[mapped].off > pga[mapped - 1].off);
2127 nioptr->offset = pga[mapped].off;
2128 nioptr->len = pga[mapped].count;
2129 nioptr->flags = pga[mapped].flag;
2132 size[1] = page_count * sizeof(*nioptr);
2133 request->rq_replen = lustre_msg_size(2, size);
2135 rc = ptlrpc_queue_wait(request);
2139 body = lustre_swab_repbuf(request, 0, sizeof(*body),
2140 lustre_swab_ost_body);
2142 CERROR("Can't unpack body\n");
2143 GOTO(out_req, rc = -EPROTO);
2146 memcpy(oa, &body->oa, sizeof(*oa));
2148 swab = lustre_msg_swabbed(request->rq_repmsg);
2149 LASSERT_REPSWAB(request, 1);
2150 nioptr = lustre_msg_buf(request->rq_repmsg, 1, size[1]);
2152 /* nioptr missing or short */
2153 GOTO(out_req, rc = -EPROTO);
2157 for (mapped = 0; mapped < page_count; mapped++, nioptr++) {
2158 struct page *page = pga[mapped].pg;
2159 struct buffer_head *bh;
2163 lustre_swab_niobuf_remote (nioptr);
2165 /* got san device associated */
2166 LASSERT(exp->exp_obd != NULL);
2167 dev = exp->exp_obd->u.cli.cl_sandev;
2170 if (!nioptr->offset) {
2171 CDEBUG(D_PAGE, "hole at ino %lu; index %ld\n",
2172 page->mapping->host->i_ino,
2174 memset(page_address(page), 0, PAGE_SIZE);
2178 if (!page->buffers) {
2179 create_empty_buffers(page, dev, PAGE_SIZE);
2182 clear_bit(BH_New, &bh->b_state);
2183 set_bit(BH_Mapped, &bh->b_state);
2184 bh->b_blocknr = (unsigned long)nioptr->offset;
2186 clear_bit(BH_Uptodate, &bh->b_state);
2188 ll_rw_block(READ, 1, &bh);
2192 /* if buffer already existed, it must be the
2193 * one we mapped before, check it */
2194 LASSERT(!test_bit(BH_New, &bh->b_state));
2195 LASSERT(test_bit(BH_Mapped, &bh->b_state));
2196 LASSERT(bh->b_blocknr == (unsigned long)nioptr->offset);
2198 /* wait it's io completion */
2199 if (test_bit(BH_Lock, &bh->b_state))
2202 if (!test_bit(BH_Uptodate, &bh->b_state))
2203 ll_rw_block(READ, 1, &bh);
2207 /* must do syncronous write here */
2209 if (!buffer_uptodate(bh)) {
2217 ptlrpc_req_finished(request);
2221 static int sanosc_brw_write(struct obd_export *exp, struct obdo *oa,
2222 struct lov_stripe_md *lsm, obd_count page_count,
2223 struct brw_page *pga)
2225 struct ptlrpc_request *request = NULL;
2226 struct ost_body *body;
2227 struct niobuf_remote *nioptr;
2228 struct obd_ioobj *iooptr;
2229 int rc, size[3] = {sizeof(*body)}, mapped = 0;
2233 size[1] = sizeof(struct obd_ioobj);
2234 size[2] = page_count * sizeof(*nioptr);
2236 request = ptlrpc_prep_req(class_exp2cliimp(exp), OST_SAN_WRITE,
2241 body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof (*body));
2242 iooptr = lustre_msg_buf(request->rq_reqmsg, 1, sizeof (*iooptr));
2243 nioptr = lustre_msg_buf(request->rq_reqmsg, 2,
2244 sizeof (*nioptr) * page_count);
2246 memcpy(&body->oa, oa, sizeof(body->oa));
2248 obdo_to_ioobj(oa, iooptr);
2249 iooptr->ioo_bufcnt = page_count;
2252 for (mapped = 0; mapped < page_count; mapped++, nioptr++) {
2253 LASSERT(PageLocked(pga[mapped].pg));
2254 LASSERT(mapped == 0 || pga[mapped].off > pga[mapped - 1].off);
2256 nioptr->offset = pga[mapped].off;
2257 nioptr->len = pga[mapped].count;
2258 nioptr->flags = pga[mapped].flag;
2261 size[1] = page_count * sizeof(*nioptr);
2262 request->rq_replen = lustre_msg_size(2, size);
2264 rc = ptlrpc_queue_wait(request);
2268 swab = lustre_msg_swabbed (request->rq_repmsg);
2269 LASSERT_REPSWAB (request, 1);
2270 nioptr = lustre_msg_buf(request->rq_repmsg, 1, size[1]);
2272 CERROR("absent/short niobuf array\n");
2273 GOTO(out_req, rc = -EPROTO);
2277 for (mapped = 0; mapped < page_count; mapped++, nioptr++) {
2278 struct page *page = pga[mapped].pg;
2279 struct buffer_head *bh;
2283 lustre_swab_niobuf_remote (nioptr);
2285 /* got san device associated */
2286 LASSERT(exp->exp_obd != NULL);
2287 dev = exp->exp_obd->u.cli.cl_sandev;
2289 if (!page->buffers) {
2290 create_empty_buffers(page, dev, PAGE_SIZE);
2293 LASSERT(!test_bit(BH_New, &page->buffers->b_state));
2294 LASSERT(test_bit(BH_Mapped, &page->buffers->b_state));
2295 LASSERT(page->buffers->b_blocknr ==
2296 (unsigned long)nioptr->offset);
2302 /* if buffer locked, wait it's io completion */
2303 if (test_bit(BH_Lock, &bh->b_state))
2306 clear_bit(BH_New, &bh->b_state);
2307 set_bit(BH_Mapped, &bh->b_state);
2309 /* override the block nr */
2310 bh->b_blocknr = (unsigned long)nioptr->offset;
2312 /* we are about to write it, so set it
2314 * page lock should garentee no race condition here */
2315 set_bit(BH_Uptodate, &bh->b_state);
2316 set_bit(BH_Dirty, &bh->b_state);
2318 ll_rw_block(WRITE, 1, &bh);
2320 /* must do syncronous write here */
2322 if (!buffer_uptodate(bh) || test_bit(BH_Dirty, &bh->b_state)) {
2330 ptlrpc_req_finished(request);
2334 static int sanosc_brw(int cmd, struct obd_export *exp, struct obdo *oa,
2335 struct lov_stripe_md *lsm, obd_count page_count,
2336 struct brw_page *pga, struct obd_trans_info *oti)
2340 while (page_count) {
2341 obd_count pages_per_brw;
2344 if (page_count > PTLRPC_MAX_BRW_PAGES)
2345 pages_per_brw = PTLRPC_MAX_BRW_PAGES;
2347 pages_per_brw = page_count;
2349 if (cmd & OBD_BRW_WRITE)
2350 rc = sanosc_brw_write(exp, oa, lsm, pages_per_brw,pga);
2352 rc = sanosc_brw_read(exp, oa, lsm, pages_per_brw, pga);
2357 page_count -= pages_per_brw;
2358 pga += pages_per_brw;
2365 static void osc_set_data_with_check(struct lustre_handle *lockh, void *data)
2367 struct ldlm_lock *lock = ldlm_handle2lock(lockh);
2369 LASSERT(lock != NULL);
2370 l_lock(&lock->l_resource->lr_namespace->ns_lock);
2372 if (lock->l_ast_data && lock->l_ast_data != data) {
2373 struct inode *new_inode = data;
2374 struct inode *old_inode = lock->l_ast_data;
2375 LASSERTF(old_inode->i_state & I_FREEING,
2376 "Found existing inode %p/%lu/%u state %lu in lock: "
2377 "setting data to %p/%lu/%u\n", old_inode,
2378 old_inode->i_ino, old_inode->i_generation,
2380 new_inode, new_inode->i_ino, new_inode->i_generation);
2383 lock->l_ast_data = data;
2384 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
2385 LDLM_LOCK_PUT(lock);
2388 static int osc_change_cbdata(struct obd_export *exp, struct lov_stripe_md *lsm,
2389 ldlm_iterator_t replace, void *data)
2391 struct ldlm_res_id res_id = { .name = {lsm->lsm_object_id} };
2392 struct obd_device *obd = class_exp2obd(exp);
2394 ldlm_change_cbdata(obd->obd_namespace, &res_id, replace, data);
2398 static int osc_enqueue(struct obd_export *exp, struct lov_stripe_md *lsm,
2399 __u32 type, ldlm_policy_data_t *policy, __u32 mode,
2400 int *flags, void *bl_cb, void *cp_cb, void *gl_cb,
2401 void *data, __u32 lvb_len, void *lvb_swabber,
2402 struct lustre_handle *lockh)
2404 struct ldlm_res_id res_id = { .name = {lsm->lsm_object_id} };
2405 struct obd_device *obd = exp->exp_obd;
2410 /* Filesystem lock extents are extended to page boundaries so that
2411 * dealing with the page cache is a little smoother. */
2412 policy->l_extent.start -= policy->l_extent.start & ~PAGE_MASK;
2413 policy->l_extent.end |= ~PAGE_MASK;
2415 if (lsm->lsm_oinfo->loi_kms_valid == 0)
2418 /* Next, search for already existing extent locks that will cover us */
2419 rc = ldlm_lock_match(obd->obd_namespace, 0, &res_id, type, policy, mode,
2422 osc_set_data_with_check(lockh, data);
2423 if (*flags & LDLM_FL_HAS_INTENT) {
2424 /* I would like to be able to ASSERT here that rss <=
2425 * kms, but I can't, for reasons which are explained in
2428 /* We already have a lock, and it's referenced */
2432 /* If we're trying to read, we also search for an existing PW lock. The
2433 * VFS and page cache already protect us locally, so lots of readers/
2434 * writers can share a single PW lock.
2436 * There are problems with conversion deadlocks, so instead of
2437 * converting a read lock to a write lock, we'll just enqueue a new
2440 * At some point we should cancel the read lock instead of making them
2441 * send us a blocking callback, but there are problems with canceling
2442 * locks out from other users right now, too. */
2444 if (mode == LCK_PR) {
2445 rc = ldlm_lock_match(obd->obd_namespace, 0, &res_id, type,
2446 policy, LCK_PW, lockh);
2448 /* FIXME: This is not incredibly elegant, but it might
2449 * be more elegant than adding another parameter to
2450 * lock_match. I want a second opinion. */
2451 ldlm_lock_addref(lockh, LCK_PR);
2452 ldlm_lock_decref(lockh, LCK_PW);
2453 osc_set_data_with_check(lockh, data);
2459 rc = ldlm_cli_enqueue(exp, NULL, obd->obd_namespace, res_id, type,
2460 policy, mode, flags, bl_cb, cp_cb, gl_cb, data,
2461 &lvb, sizeof(lvb), lustre_swab_ost_lvb, lockh);
2463 if ((*flags & LDLM_FL_HAS_INTENT && rc == ELDLM_LOCK_ABORTED) || !rc) {
2464 CDEBUG(D_INODE, "received kms == "LPU64"\n", lvb.lvb_size);
2465 lsm->lsm_oinfo->loi_rss = lvb.lvb_size;
2471 static int osc_match(struct obd_export *exp, struct lov_stripe_md *lsm,
2472 __u32 type, ldlm_policy_data_t *policy, __u32 mode,
2473 int *flags, void *data, struct lustre_handle *lockh)
2475 struct ldlm_res_id res_id = { .name = {lsm->lsm_object_id} };
2476 struct obd_device *obd = exp->exp_obd;
2480 OBD_FAIL_RETURN(OBD_FAIL_OSC_MATCH, -EIO);
2482 /* Filesystem lock extents are extended to page boundaries so that
2483 * dealing with the page cache is a little smoother */
2484 policy->l_extent.start -= policy->l_extent.start & ~PAGE_MASK;
2485 policy->l_extent.end |= ~PAGE_MASK;
2487 /* Next, search for already existing extent locks that will cover us */
2488 rc = ldlm_lock_match(obd->obd_namespace, *flags, &res_id, type,
2489 policy, mode, lockh);
2491 osc_set_data_with_check(lockh, data);
2494 /* If we're trying to read, we also search for an existing PW lock. The
2495 * VFS and page cache already protect us locally, so lots of readers/
2496 * writers can share a single PW lock. */
2497 if (mode == LCK_PR) {
2498 rc = ldlm_lock_match(obd->obd_namespace, *flags, &res_id, type,
2499 policy, LCK_PW, lockh);
2500 if (rc == 1 && !(*flags & LDLM_FL_TEST_LOCK)) {
2501 /* FIXME: This is not incredibly elegant, but it might
2502 * be more elegant than adding another parameter to
2503 * lock_match. I want a second opinion. */
2504 osc_set_data_with_check(lockh, data);
2505 ldlm_lock_addref(lockh, LCK_PR);
2506 ldlm_lock_decref(lockh, LCK_PW);
2512 static int osc_cancel(struct obd_export *exp, struct lov_stripe_md *md,
2513 __u32 mode, struct lustre_handle *lockh)
2517 if (mode == LCK_GROUP)
2518 ldlm_lock_decref_and_cancel(lockh, mode);
2520 ldlm_lock_decref(lockh, mode);
2525 static int osc_cancel_unused(struct obd_export *exp,
2526 struct lov_stripe_md *lsm, int flags, void *opaque)
2528 struct obd_device *obd = class_exp2obd(exp);
2529 struct ldlm_res_id res_id = { .name = {lsm->lsm_object_id} };
2531 return ldlm_cli_cancel_unused(obd->obd_namespace, &res_id, flags,
2535 static int osc_statfs(struct obd_device *obd, struct obd_statfs *osfs,
2536 unsigned long max_age)
2538 struct obd_statfs *msfs;
2539 struct ptlrpc_request *request;
2540 int rc, size = sizeof(*osfs);
2543 /* We could possibly pass max_age in the request (as an absolute
2544 * timestamp or a "seconds.usec ago") so the target can avoid doing
2545 * extra calls into the filesystem if that isn't necessary (e.g.
2546 * during mount that would help a bit). Having relative timestamps
2547 * is not so great if request processing is slow, while absolute
2548 * timestamps are not ideal because they need time synchronization. */
2549 request = ptlrpc_prep_req(obd->u.cli.cl_import, OST_STATFS,0,NULL,NULL);
2553 request->rq_replen = lustre_msg_size(1, &size);
2554 request->rq_request_portal = OST_CREATE_PORTAL; //XXX FIXME bug 249
2556 rc = ptlrpc_queue_wait(request);
2560 msfs = lustre_swab_repbuf(request, 0, sizeof(*msfs),
2561 lustre_swab_obd_statfs);
2563 CERROR("Can't unpack obd_statfs\n");
2564 GOTO(out, rc = -EPROTO);
2567 memcpy(osfs, msfs, sizeof(*osfs));
2571 ptlrpc_req_finished(request);
2575 /* Retrieve object striping information.
2577 * @lmmu is a pointer to an in-core struct with lmm_ost_count indicating
2578 * the maximum number of OST indices which will fit in the user buffer.
2579 * lmm_magic must be LOV_MAGIC (we only use 1 slot here).
2581 static int osc_getstripe(struct lov_stripe_md *lsm, struct lov_user_md *lump)
2583 struct lov_user_md lum, *lumk;
2590 rc = copy_from_user(&lum, lump, sizeof(lum));
2594 if (lum.lmm_magic != LOV_USER_MAGIC)
2597 if (lum.lmm_stripe_count > 0) {
2598 lum_size = sizeof(lum) + sizeof(lum.lmm_objects[0]);
2599 OBD_ALLOC(lumk, lum_size);
2603 lumk->lmm_objects[0].l_object_id = lsm->lsm_object_id;
2605 lum_size = sizeof(lum);
2609 lumk->lmm_object_id = lsm->lsm_object_id;
2610 lumk->lmm_stripe_count = 1;
2612 if (copy_to_user(lump, lumk, lum_size))
2616 OBD_FREE(lumk, lum_size);
2621 static int osc_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
2622 void *karg, void *uarg)
2624 struct obd_device *obd = exp->exp_obd;
2625 struct obd_ioctl_data *data = karg;
2632 case OBD_IOC_LOV_GET_CONFIG: {
2634 struct lov_desc *desc;
2635 struct obd_uuid uuid;
2639 if (obd_ioctl_getdata(&buf, &len, (void *)uarg))
2640 GOTO(out, err = -EINVAL);
2642 data = (struct obd_ioctl_data *)buf;
2644 if (sizeof(*desc) > data->ioc_inllen1) {
2646 GOTO(out, err = -EINVAL);
2649 if (data->ioc_inllen2 < sizeof(uuid)) {
2651 GOTO(out, err = -EINVAL);
2654 desc = (struct lov_desc *)data->ioc_inlbuf1;
2655 desc->ld_tgt_count = 1;
2656 desc->ld_active_tgt_count = 1;
2657 desc->ld_default_stripe_count = 1;
2658 desc->ld_default_stripe_size = 0;
2659 desc->ld_default_stripe_offset = 0;
2660 desc->ld_pattern = 0;
2661 memcpy(&desc->ld_uuid, &obd->obd_uuid, sizeof(uuid));
2663 memcpy(data->ioc_inlbuf2, &obd->obd_uuid, sizeof(uuid));
2665 err = copy_to_user((void *)uarg, buf, len);
2668 obd_ioctl_freedata(buf, len);
2671 case LL_IOC_LOV_SETSTRIPE:
2672 err = obd_alloc_memmd(exp, karg);
2676 case LL_IOC_LOV_GETSTRIPE:
2677 err = osc_getstripe(karg, uarg);
2679 case OBD_IOC_CLIENT_RECOVER:
2680 err = ptlrpc_recover_import(obd->u.cli.cl_import,
2685 case IOC_OSC_SET_ACTIVE:
2686 err = ptlrpc_set_import_active(obd->u.cli.cl_import,
2690 CDEBUG(D_INODE, "unrecognised ioctl %#x by %s\n", cmd, current->comm);
2691 GOTO(out, err = -ENOTTY);
2698 static int osc_get_info(struct obd_export *exp, obd_count keylen,
2699 void *key, __u32 *vallen, void *val)
2702 if (!vallen || !val)
2705 if (keylen > strlen("lock_to_stripe") &&
2706 strcmp(key, "lock_to_stripe") == 0) {
2707 __u32 *stripe = val;
2708 *vallen = sizeof(*stripe);
2711 } else if (keylen >= strlen("last_id") && strcmp(key, "last_id") == 0) {
2712 struct ptlrpc_request *req;
2714 char *bufs[1] = {key};
2716 req = ptlrpc_prep_req(class_exp2cliimp(exp), OST_GET_INFO, 1,
2721 req->rq_replen = lustre_msg_size(1, vallen);
2722 rc = ptlrpc_queue_wait(req);
2726 reply = lustre_swab_repbuf(req, 0, sizeof(*reply),
2727 lustre_swab_ost_last_id);
2728 if (reply == NULL) {
2729 CERROR("Can't unpack OST last ID\n");
2730 GOTO(out, rc = -EPROTO);
2732 *((obd_id *)val) = *reply;
2734 ptlrpc_req_finished(req);
2740 static int osc_set_info(struct obd_export *exp, obd_count keylen,
2741 void *key, obd_count vallen, void *val)
2743 struct ptlrpc_request *req;
2744 struct obd_device *obd = exp->exp_obd;
2745 struct obd_import *imp = class_exp2cliimp(exp);
2746 struct llog_ctxt *ctxt;
2747 int rc, size = keylen;
2748 char *bufs[1] = {key};
2751 if (keylen == strlen("next_id") &&
2752 memcmp(key, "next_id", strlen("next_id")) == 0) {
2753 if (vallen != sizeof(obd_id))
2755 obd->u.cli.cl_oscc.oscc_next_id = *((obd_id*)val) + 1;
2756 CDEBUG(D_INODE, "%s: set oscc_next_id = "LPU64"\n",
2757 exp->exp_obd->obd_name,
2758 obd->u.cli.cl_oscc.oscc_next_id);
2763 if (keylen == strlen("growth_count") &&
2764 memcmp(key, "growth_count", strlen("growth_count")) == 0) {
2765 if (vallen != sizeof(int))
2767 obd->u.cli.cl_oscc.oscc_grow_count = *((int*)val);
2771 if (keylen == strlen("unlinked") &&
2772 memcmp(key, "unlinked", keylen) == 0) {
2773 struct osc_creator *oscc = &obd->u.cli.cl_oscc;
2774 spin_lock(&oscc->oscc_lock);
2775 oscc->oscc_flags &= ~OSCC_FLAG_NOSPC;
2776 spin_unlock(&oscc->oscc_lock);
2781 if (keylen == strlen("initial_recov") &&
2782 memcmp(key, "initial_recov", strlen("initial_recov")) == 0) {
2783 struct obd_import *imp = exp->exp_obd->u.cli.cl_import;
2784 if (vallen != sizeof(int))
2786 imp->imp_initial_recov = *(int *)val;
2787 CDEBUG(D_HA, "%s: set imp_no_init_recov = %d\n",
2788 exp->exp_obd->obd_name,
2789 imp->imp_initial_recov);
2793 if (keylen < strlen("mds_conn") ||
2794 memcmp(key, "mds_conn", strlen("mds_conn")) != 0)
2798 req = ptlrpc_prep_req(imp, OST_SET_INFO, 1, &size, bufs);
2802 req->rq_replen = lustre_msg_size(0, NULL);
2803 rc = ptlrpc_queue_wait(req);
2804 ptlrpc_req_finished(req);
2806 ctxt = llog_get_context(exp->exp_obd, LLOG_UNLINK_ORIG_CTXT);
2808 rc = llog_initiator_connect(ctxt);
2813 imp->imp_server_timeout = 1;
2814 CDEBUG(D_HA, "pinging OST %s\n", imp->imp_target_uuid.uuid);
2815 imp->imp_pingable = 1;
2821 static struct llog_operations osc_size_repl_logops = {
2822 lop_cancel: llog_obd_repl_cancel
2825 static struct llog_operations osc_unlink_orig_logops;
2826 static int osc_llog_init(struct obd_device *obd, struct obd_device *tgt,
2827 int count, struct llog_catid *catid)
2832 osc_unlink_orig_logops = llog_lvfs_ops;
2833 osc_unlink_orig_logops.lop_setup = llog_obd_origin_setup;
2834 osc_unlink_orig_logops.lop_cleanup = llog_obd_origin_cleanup;
2835 osc_unlink_orig_logops.lop_add = llog_obd_origin_add;
2836 osc_unlink_orig_logops.lop_connect = llog_origin_connect;
2838 rc = llog_setup(obd, LLOG_UNLINK_ORIG_CTXT, tgt, count,
2839 &catid->lci_logid, &osc_unlink_orig_logops);
2843 rc = llog_setup(obd, LLOG_SIZE_REPL_CTXT, tgt, count, NULL,
2844 &osc_size_repl_logops);
2848 static int osc_llog_finish(struct obd_device *obd, int count)
2853 rc = llog_cleanup(llog_get_context(obd, LLOG_UNLINK_ORIG_CTXT));
2857 rc = llog_cleanup(llog_get_context(obd, LLOG_SIZE_REPL_CTXT));
2862 static int osc_connect(struct lustre_handle *exph,
2863 struct obd_device *obd, struct obd_uuid *cluuid)
2867 rc = client_connect_import(exph, obd, cluuid);
2872 static int osc_disconnect(struct obd_export *exp, int flags)
2874 struct obd_device *obd = class_exp2obd(exp);
2875 struct llog_ctxt *ctxt = llog_get_context(obd, LLOG_SIZE_REPL_CTXT);
2878 if (obd->u.cli.cl_conn_count == 1)
2879 /* flush any remaining cancel messages out to the target */
2880 llog_sync(ctxt, exp);
2882 rc = client_disconnect_export(exp, flags);
2886 static int osc_import_event(struct obd_device *obd,
2887 struct obd_import *imp,
2888 enum obd_import_event event)
2890 struct client_obd *cli;
2893 LASSERT(imp->imp_obd == obd);
2896 case IMP_EVENT_DISCON: {
2897 /* Only do this on the MDS OSC's */
2898 if (imp->imp_server_timeout) {
2899 struct osc_creator *oscc = &obd->u.cli.cl_oscc;
2901 spin_lock(&oscc->oscc_lock);
2902 oscc->oscc_flags |= OSCC_FLAG_RECOVERING;
2903 spin_unlock(&oscc->oscc_lock);
2907 case IMP_EVENT_INACTIVE: {
2908 if (obd->obd_observer)
2909 rc = obd_notify(obd->obd_observer, obd, 0);
2912 case IMP_EVENT_INVALIDATE: {
2913 struct ldlm_namespace *ns = obd->obd_namespace;
2917 spin_lock(&cli->cl_loi_list_lock);
2918 cli->cl_avail_grant = 0;
2919 cli->cl_lost_grant = 0;
2920 /* all pages go to failing rpcs due to the invalid import */
2921 osc_check_rpcs(cli);
2922 spin_unlock(&cli->cl_loi_list_lock);
2924 ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
2928 case IMP_EVENT_ACTIVE: {
2929 if (obd->obd_observer)
2930 rc = obd_notify(obd->obd_observer, obd, 1);
2934 CERROR("Unknown import event %d\n", event);
2940 int osc_setup(struct obd_device *obd, obd_count len, void *buf)
2944 rc = ptlrpcd_addref();
2948 rc = client_obd_setup(obd, len, buf);
2957 int osc_cleanup(struct obd_device *obd, int flags)
2961 rc = client_obd_cleanup(obd, flags);
2967 struct obd_ops osc_obd_ops = {
2968 o_owner: THIS_MODULE,
2969 o_attach: osc_attach,
2970 o_detach: osc_detach,
2972 o_cleanup: osc_cleanup,
2973 o_connect: osc_connect,
2974 o_disconnect: osc_disconnect,
2975 o_statfs: osc_statfs,
2976 o_packmd: osc_packmd,
2977 o_unpackmd: osc_unpackmd,
2978 o_create: osc_create,
2979 o_destroy: osc_destroy,
2980 o_getattr: osc_getattr,
2981 o_getattr_async:osc_getattr_async,
2982 o_setattr: osc_setattr,
2984 o_brw_async: osc_brw_async,
2985 .o_prep_async_page = osc_prep_async_page,
2986 .o_queue_async_io = osc_queue_async_io,
2987 .o_set_async_flags = osc_set_async_flags,
2988 .o_queue_group_io = osc_queue_group_io,
2989 .o_trigger_group_io = osc_trigger_group_io,
2990 .o_teardown_async_page = osc_teardown_async_page,
2993 o_enqueue: osc_enqueue,
2995 o_change_cbdata:osc_change_cbdata,
2996 o_cancel: osc_cancel,
2997 o_cancel_unused:osc_cancel_unused,
2998 o_iocontrol: osc_iocontrol,
2999 o_get_info: osc_get_info,
3000 o_set_info: osc_set_info,
3001 o_import_event: osc_import_event,
3002 o_llog_init: osc_llog_init,
3003 o_llog_finish: osc_llog_finish,
3006 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
3007 struct obd_ops sanosc_obd_ops = {
3008 o_owner: THIS_MODULE,
3009 o_attach: osc_attach,
3010 o_detach: osc_detach,
3011 o_cleanup: client_obd_cleanup,
3012 o_connect: osc_connect,
3013 o_disconnect: client_disconnect_export,
3014 o_statfs: osc_statfs,
3015 o_packmd: osc_packmd,
3016 o_unpackmd: osc_unpackmd,
3017 o_create: osc_real_create,
3018 o_destroy: osc_destroy,
3019 o_getattr: osc_getattr,
3020 o_getattr_async:osc_getattr_async,
3021 o_setattr: osc_setattr,
3022 o_setup: client_sanobd_setup,
3026 o_enqueue: osc_enqueue,
3028 o_change_cbdata:osc_change_cbdata,
3029 o_cancel: osc_cancel,
3030 o_cancel_unused:osc_cancel_unused,
3031 o_iocontrol: osc_iocontrol,
3032 o_import_event: osc_import_event,
3033 o_llog_init: osc_llog_init,
3034 o_llog_finish: osc_llog_finish,
3038 int __init osc_init(void)
3040 struct lprocfs_static_vars lvars, sanlvars;
3044 lprocfs_init_vars(osc, &lvars);
3045 lprocfs_init_vars(osc, &sanlvars);
3047 rc = class_register_type(&osc_obd_ops, lvars.module_vars,
3052 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
3053 rc = class_register_type(&sanosc_obd_ops, sanlvars.module_vars,
3054 LUSTRE_SANOSC_NAME);
3056 class_unregister_type(LUSTRE_OSC_NAME);
3062 static void /*__exit*/ osc_exit(void)
3064 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
3065 class_unregister_type(LUSTRE_SANOSC_NAME);
3067 class_unregister_type(LUSTRE_OSC_NAME);
3071 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
3072 MODULE_DESCRIPTION("Lustre Object Storage Client (OSC)");
3073 MODULE_LICENSE("GPL");
3075 module_init(osc_init);
3076 module_exit(osc_exit);