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 <libcfs/kp30.h>
53 #include <linux/lustre_net.h>
54 #include <linux/lustre_sec.h>
55 #include <lustre/lustre_user.h>
56 #include <linux/obd_ost.h>
57 #include <linux/obd_lov.h>
63 #include <linux/lustre_ha.h>
64 #include <linux/lprocfs_status.h>
65 #include <linux/lustre_log.h>
66 #include <linux/lustre_lite.h>
67 #include <linux/lustre_audit.h>
68 #include <linux/lustre_gs.h>
70 #include "osc_internal.h"
72 /* Pack OSC object metadata for disk storage (LE byte order). */
73 static int osc_packmd(struct obd_export *exp, struct lov_mds_md **lmmp,
74 struct lov_stripe_md *lsm)
79 lmm_size = sizeof(**lmmp);
84 OBD_FREE(*lmmp, lmm_size);
90 OBD_ALLOC(*lmmp, lmm_size);
96 LASSERT(lsm->lsm_object_id);
97 LASSERT(lsm->lsm_object_gr);
98 (*lmmp)->lmm_object_id = cpu_to_le64(lsm->lsm_object_id);
99 (*lmmp)->lmm_object_gr = cpu_to_le64(lsm->lsm_object_gr);
105 /* Unpack OSC object metadata from disk storage (LE byte order). */
106 static int osc_unpackmd(struct obd_export *exp, struct lov_stripe_md **lsmp,
107 struct lov_mds_md *lmm, int lmm_bytes)
113 if (lmm_bytes < sizeof (*lmm)) {
114 CERROR("lov_mds_md too small: %d, need %d\n",
115 lmm_bytes, (int)sizeof(*lmm));
118 /* XXX LOV_MAGIC etc check? */
120 if (lmm->lmm_object_id == 0) {
121 CERROR("lov_mds_md: zero lmm_object_id\n");
126 lsm_size = lov_stripe_md_size(1);
130 if (*lsmp != NULL && lmm == NULL) {
131 OBD_FREE(*lsmp, lsm_size);
137 OBD_ALLOC(*lsmp, lsm_size);
140 loi_init((*lsmp)->lsm_oinfo);
144 /* XXX zero *lsmp? */
145 (*lsmp)->lsm_object_id = le64_to_cpu (lmm->lmm_object_id);
146 (*lsmp)->lsm_object_gr = le64_to_cpu (lmm->lmm_object_gr);
147 LASSERT((*lsmp)->lsm_object_id);
148 LASSERT((*lsmp)->lsm_object_gr);
151 (*lsmp)->lsm_maxbytes = LUSTRE_STRIPE_MAXBYTES;
156 static int osc_getattr_interpret(struct ptlrpc_request *req,
157 struct osc_getattr_async_args *aa, int rc)
159 struct ost_body *body;
165 body = lustre_swab_repbuf(req, 0, sizeof(*body), lustre_swab_ost_body);
167 CDEBUG(D_INODE, "mode: %o\n", body->oa.o_mode);
168 memcpy(aa->aa_oa, &body->oa, sizeof(*aa->aa_oa));
170 /* This should really be sent by the OST */
171 aa->aa_oa->o_blksize = PTLRPC_MAX_BRW_SIZE;
172 aa->aa_oa->o_valid |= OBD_MD_FLBLKSZ;
174 CERROR("can't unpack ost_body\n");
176 aa->aa_oa->o_valid = 0;
182 static int osc_getattr_async(struct obd_export *exp, struct obdo *oa,
183 struct lov_stripe_md *md,
184 struct ptlrpc_request_set *set)
186 struct ptlrpc_request *request;
187 struct ost_body *body;
188 int size = sizeof(*body);
189 struct osc_getattr_async_args *aa;
192 request = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_OBD_VERSION,
193 OST_GETATTR, 1, &size, NULL);
197 body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof (*body));
198 memcpy(&body->oa, oa, sizeof(*oa));
200 request->rq_replen = lustre_msg_size(1, &size);
201 request->rq_interpret_reply = osc_getattr_interpret;
203 LASSERT (sizeof (*aa) <= sizeof (request->rq_async_args));
204 aa = (struct osc_getattr_async_args *)&request->rq_async_args;
207 ptlrpc_set_add_req (set, request);
211 static int osc_getattr(struct obd_export *exp, struct obdo *oa,
212 struct lov_stripe_md *md)
214 struct ptlrpc_request *request;
215 struct ost_body *body;
216 int rc, size = sizeof(*body);
219 request = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_OBD_VERSION,
220 OST_GETATTR, 1, &size, NULL);
224 body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof (*body));
225 memcpy(&body->oa, oa, sizeof(*oa));
227 request->rq_replen = lustre_msg_size(1, &size);
229 rc = ptlrpc_queue_wait(request);
231 CERROR("%s failed: rc = %d\n", __FUNCTION__, rc);
235 body = lustre_swab_repbuf(request, 0, sizeof (*body),
236 lustre_swab_ost_body);
238 CERROR ("can't unpack ost_body\n");
239 GOTO (out, rc = -EPROTO);
242 CDEBUG(D_INODE, "mode: %o\n", body->oa.o_mode);
243 memcpy(oa, &body->oa, sizeof(*oa));
245 /* This should really be sent by the OST */
246 oa->o_blksize = PTLRPC_MAX_BRW_SIZE;
247 oa->o_valid |= OBD_MD_FLBLKSZ;
251 ptlrpc_req_finished(request);
255 static int osc_setattr(struct obd_export *exp, struct obdo *oa,
256 struct lov_stripe_md *md, struct obd_trans_info *oti,
257 struct lustre_capa *capa)
259 struct ptlrpc_request *request;
260 struct ost_body *body;
261 int rc, size = sizeof(*body);
264 LASSERT(!(oa->o_valid & OBD_MD_FLGROUP) || oa->o_gr > 0);
266 request = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_OBD_VERSION,
267 OST_SETATTR, 1, &size, NULL);
271 body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof(*body));
272 memcpy(&body->oa, oa, sizeof(*oa));
274 request->rq_replen = lustre_msg_size(1, &size);
276 if (oti != NULL && (oti->oti_flags & OBD_MODE_ASYNC)) {
277 ptlrpcd_add_req(request);
280 rc = ptlrpc_queue_wait(request);
284 body = lustre_swab_repbuf(request, 0, sizeof(*body),
285 lustre_swab_ost_body);
287 GOTO(out, rc = -EPROTO);
289 memcpy(oa, &body->oa, sizeof(*oa));
293 ptlrpc_req_finished(request);
297 int osc_real_create(struct obd_export *exp, struct obdo *oa,
298 struct lov_stripe_md **ea, struct obd_trans_info *oti)
300 struct osc_creator *oscc = &exp->exp_obd->u.cli.cl_oscc;
301 struct ptlrpc_request *request;
302 struct ost_body *body;
303 struct lov_stripe_md *lsm;
304 int rc, size = sizeof(*body);
312 rc = obd_alloc_memmd(exp, &lsm);
317 request = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_OBD_VERSION,
318 OST_CREATE, 1, &size, NULL);
320 GOTO(out, rc = -ENOMEM);
322 body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof (*body));
323 memcpy(&body->oa, oa, sizeof(body->oa));
325 request->rq_replen = lustre_msg_size(1, &size);
326 if (oa->o_valid & OBD_MD_FLINLINE) {
327 LASSERT((oa->o_valid & OBD_MD_FLFLAGS) &&
328 oa->o_flags == OBD_FL_DELORPHAN);
329 DEBUG_REQ(D_HA, request,
330 "delorphan from OST integration");
331 /* Don't resend the delorphan request */
332 request->rq_no_resend = request->rq_no_delay = 1;
335 rc = ptlrpc_queue_wait(request);
339 body = lustre_swab_repbuf(request, 0, sizeof(*body),
340 lustre_swab_ost_body);
342 CERROR ("can't unpack ost_body\n");
343 GOTO (out_req, rc = -EPROTO);
346 if ((oa->o_valid & OBD_MD_FLFLAGS) && oa->o_flags == OBD_FL_DELORPHAN) {
347 struct obd_import *imp = class_exp2cliimp(exp);
348 /* MDS declares last known object, OSS responses
349 * with next possible object -bzzz */
350 spin_lock(&oscc->oscc_lock);
351 oscc->oscc_next_id = body->oa.o_id;
352 spin_unlock(&oscc->oscc_lock);
353 CDEBUG(D_HA, "%s: set nextid "LPD64" after recovery\n",
354 imp->imp_target_uuid.uuid, oa->o_id);
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;
367 lsm->lsm_object_gr = oa->o_gr;
371 oti->oti_transno = request->rq_repmsg->transno;
373 if (oa->o_valid & OBD_MD_FLCOOKIE) {
374 if (!oti->oti_logcookies)
375 oti_alloc_cookies(oti, 1);
376 memcpy(oti->oti_logcookies, obdo_logcookie(oa),
377 sizeof(oti->oti_onecookie));
381 CDEBUG(D_HA, "transno: "LPD64"\n", request->rq_repmsg->transno);
384 ptlrpc_req_finished(request);
387 obd_free_memmd(exp, &lsm);
391 static int osc_punch(struct obd_export *exp, struct obdo *oa,
392 struct lov_stripe_md *md, obd_size start,
393 obd_size end, struct obd_trans_info *oti,
394 struct lustre_capa *capa)
396 struct ptlrpc_request *request;
397 struct ost_body *body;
398 struct lustre_capa *req_capa;
400 int rc, size[2] = { sizeof(*body), sizeof(*capa) };
408 request = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_OBD_VERSION,
409 OST_PUNCH, capa ? 2 : 1, size, NULL);
413 body = lustre_msg_buf(request->rq_reqmsg, bufcnt++, sizeof (*body));
415 memcpy(&body->oa, oa, sizeof(*oa));
417 /* overload the size and blocks fields in the oa with start/end */
418 body->oa.o_size = start;
419 body->oa.o_blocks = end;
420 body->oa.o_valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS);
423 req_capa = lustre_msg_buf(request->rq_reqmsg, bufcnt++,
425 capa_dup2(req_capa, capa);
426 body->oa.o_valid |= OBD_MD_CAPA;
429 request->rq_replen = lustre_msg_size(1, size);
431 rc = ptlrpc_queue_wait(request);
435 body = lustre_swab_repbuf (request, 0, sizeof (*body),
436 lustre_swab_ost_body);
438 CERROR ("can't unpack ost_body\n");
439 GOTO (out, rc = -EPROTO);
442 memcpy(oa, &body->oa, sizeof(*oa));
446 ptlrpc_req_finished(request);
450 static int osc_sync(struct obd_export *exp, struct obdo *oa,
451 struct lov_stripe_md *md, obd_size start,
454 struct ptlrpc_request *request;
455 struct ost_body *body;
456 int rc, size = sizeof(*body);
464 request = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_OBD_VERSION,
465 OST_SYNC, 1, &size, NULL);
469 body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof (*body));
470 memcpy(&body->oa, oa, sizeof(*oa));
472 /* overload the size and blocks fields in the oa with start/end */
473 body->oa.o_size = start;
474 body->oa.o_blocks = end;
475 body->oa.o_valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS);
477 request->rq_replen = lustre_msg_size(1, &size);
479 rc = ptlrpc_queue_wait(request);
483 body = lustre_swab_repbuf(request, 0, sizeof(*body),
484 lustre_swab_ost_body);
486 CERROR ("can't unpack ost_body\n");
487 GOTO (out, rc = -EPROTO);
490 memcpy(oa, &body->oa, sizeof(*oa));
494 ptlrpc_req_finished(request);
498 static int osc_destroy(struct obd_export *exp, struct obdo *oa,
499 struct lov_stripe_md *ea, struct obd_trans_info *oti)
501 struct ptlrpc_request *request;
502 struct ost_body *body;
503 int rc, size = sizeof(*body);
511 request = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_OBD_VERSION,
512 OST_DESTROY, 1, &size, NULL);
515 request->rq_request_portal = OST_DESTROY_PORTAL;
517 body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof (*body));
519 if (oti != NULL && oa->o_valid & OBD_MD_FLCOOKIE) {
520 memcpy(obdo_logcookie(oa), oti->oti_logcookies,
521 sizeof(*oti->oti_logcookies));
522 oti->oti_logcookies++;
525 memcpy(&body->oa, oa, sizeof(*oa));
526 request->rq_replen = lustre_msg_size(1, &size);
528 if (oti != NULL && (oti->oti_flags & OBD_MODE_ASYNC)) {
529 ptlrpcd_add_req(request);
532 rc = ptlrpc_queue_wait(request);
538 ptlrpc_req_finished(request);
542 body = lustre_swab_repbuf(request, 0, sizeof(*body),
543 lustre_swab_ost_body);
545 CERROR ("Can't unpack body\n");
546 ptlrpc_req_finished(request);
550 memcpy(oa, &body->oa, sizeof(*oa));
551 ptlrpc_req_finished(request);
556 static void osc_announce_cached(struct client_obd *cli, struct obdo *oa,
559 obd_valid bits = OBD_MD_FLBLOCKS|OBD_MD_FLGRANT;
561 LASSERT(!(oa->o_valid & bits));
564 spin_lock(&cli->cl_loi_list_lock);
565 oa->o_dirty = cli->cl_dirty;
566 oa->o_undirty = cli->cl_dirty_max - oa->o_dirty;
567 oa->o_grant = cli->cl_avail_grant;
568 oa->o_dropped = cli->cl_lost_grant;
569 cli->cl_lost_grant = 0;
570 spin_unlock(&cli->cl_loi_list_lock);
571 CDEBUG(D_CACHE,"dirty: "LPU64" undirty: %u dropped %u grant: "LPU64"\n",
572 oa->o_dirty, oa->o_undirty, oa->o_dropped, oa->o_grant);
575 /* caller must hold loi_list_lock */
576 static void osc_consume_write_grant(struct client_obd *cli,
577 struct osc_async_page *oap)
579 cli->cl_dirty += PAGE_SIZE;
580 cli->cl_avail_grant -= PAGE_SIZE;
581 oap->oap_brw_flags |= OBD_BRW_FROM_GRANT;
582 CDEBUG(D_CACHE, "using %lu grant credits for oap %p\n", PAGE_SIZE, oap);
583 LASSERT(cli->cl_avail_grant >= 0);
586 static unsigned long rpcs_in_flight(struct client_obd *cli)
588 return cli->cl_r_in_flight + cli->cl_w_in_flight;
591 /* caller must hold loi_list_lock */
592 void osc_wake_cache_waiters(struct client_obd *cli)
594 struct list_head *l, *tmp;
595 struct osc_cache_waiter *ocw;
597 list_for_each_safe(l, tmp, &cli->cl_cache_waiters) {
598 /* if we can't dirty more, we must wait until some is written */
599 if (cli->cl_dirty + PAGE_SIZE > cli->cl_dirty_max) {
600 CDEBUG(D_CACHE, "no dirty room: dirty: %ld max %ld\n",
601 cli->cl_dirty, cli->cl_dirty_max);
605 /* if still dirty cache but no grant wait for pending RPCs that
606 * may yet return us some grant before doing sync writes */
607 if (cli->cl_w_in_flight && cli->cl_avail_grant < PAGE_SIZE) {
608 CDEBUG(D_CACHE, "%u BRW writes in flight, no grant\n",
609 cli->cl_w_in_flight);
611 ocw = list_entry(l, struct osc_cache_waiter, ocw_entry);
612 list_del_init(&ocw->ocw_entry);
613 if (cli->cl_avail_grant < PAGE_SIZE) {
614 /* no more RPCs in flight to return grant, do sync IO */
615 ocw->ocw_rc = -EDQUOT;
616 CDEBUG(D_INODE, "wake oap %p for sync\n", ocw->ocw_oap);
618 osc_consume_write_grant(cli, ocw->ocw_oap);
621 wake_up(&ocw->ocw_waitq);
627 static void osc_update_grant(struct client_obd *cli, struct ost_body *body)
629 spin_lock(&cli->cl_loi_list_lock);
630 CDEBUG(D_CACHE, "got "LPU64" extra grant\n", body->oa.o_grant);
631 cli->cl_avail_grant += body->oa.o_grant;
632 /* waiters are woken in brw_interpret_oap */
633 spin_unlock(&cli->cl_loi_list_lock);
636 /* We assume that the reason this OSC got a short read is because it read
637 * beyond the end of a stripe file; i.e. lustre is reading a sparse file
638 * via the LOV, and it _knows_ it's reading inside the file, it's just that
639 * this stripe never got written at or beyond this stripe offset yet. */
640 static void handle_short_read(int nob_read, obd_count page_count,
641 struct brw_page *pga)
645 /* skip bytes read OK */
646 while (nob_read > 0) {
647 LASSERT (page_count > 0);
649 if (pga->count > nob_read) {
650 /* EOF inside this page */
651 ptr = kmap(pga->pg) + (pga->page_offset & ~PAGE_MASK);
652 memset(ptr + nob_read, 0, pga->count - nob_read);
659 nob_read -= pga->count;
664 /* zero remaining pages */
665 while (page_count-- > 0) {
666 ptr = kmap(pga->pg) + (pga->page_offset & ~PAGE_MASK);
667 memset(ptr, 0, pga->count);
673 static int check_write_rcs(struct ptlrpc_request *request,
674 int requested_nob, int niocount,
675 obd_count page_count, struct brw_page *pga)
679 /* return error if any niobuf was in error */
680 remote_rcs = lustre_swab_repbuf(request, 1,
681 sizeof(*remote_rcs) * niocount, NULL);
682 if (remote_rcs == NULL) {
683 CERROR("Missing/short RC vector on BRW_WRITE reply\n");
686 if (lustre_msg_swabbed(request->rq_repmsg))
687 for (i = 0; i < niocount; i++)
688 __swab32s((__u32 *)&remote_rcs[i]);
690 for (i = 0; i < niocount; i++) {
691 if (remote_rcs[i] < 0)
692 return(remote_rcs[i]);
694 if (remote_rcs[i] != 0) {
695 CERROR("rc[%d] invalid (%d) req %p\n",
696 i, remote_rcs[i], request);
701 if (request->rq_bulk->bd_nob_transferred != requested_nob) {
702 CERROR("Unexpected # bytes transferred: %d (requested %d)\n",
703 requested_nob, request->rq_bulk->bd_nob_transferred);
710 static inline int can_merge_pages(struct brw_page *p1, struct brw_page *p2)
712 if (p1->flag != p2->flag) {
713 unsigned mask = ~OBD_BRW_FROM_GRANT;
715 /* warn if we try to combine flags that we don't know to be
717 if ((p1->flag & mask) != (p2->flag & mask))
718 CERROR("is it ok to have flags 0x%x and 0x%x in the "
719 "same brw?\n", p1->flag, p2->flag);
723 return (p1->disk_offset + p1->count == p2->disk_offset);
727 static obd_count cksum_pages(int nob, obd_count page_count,
728 struct brw_page *pga)
734 LASSERT (page_count > 0);
737 ost_checksum(&cksum, ptr + (pga->off & (PAGE_SIZE - 1)),
738 pga->count > nob ? nob : pga->count);
750 #define osc_encrypt_page(page, off, count) \
751 osc_crypt_page(page, off, count, ENCRYPT_DATA)
752 #define osc_decrypt_page(page, off, count) \
753 osc_crypt_page(page, off, count, DECRYPT_DATA)
754 /*Put a global call back var here is Ugly, but put it to client_obd
755 *also seems not a good idea, WangDi*/
756 crypt_cb_t osc_crypt_cb = NULL;
758 static int osc_crypt_page(struct page *page, obd_off page_off, obd_off count,
764 if (osc_crypt_cb != NULL)
765 rc = osc_crypt_cb(page, page_off, count, flags);
767 CERROR("crypt page error %d \n", rc);
771 static int osc_decrypt_pages(struct brw_page *pga, int page_count)
776 for (i = 0; i < page_count; i++) {
777 struct brw_page *pg = &pga[i];
778 osc_decrypt_page(pg->pg, pg->page_offset, pg->count);
783 static int osc_brw_prep_request(int cmd, struct obd_import *imp,struct obdo *oa,
784 struct lov_stripe_md *lsm, obd_count page_count,
785 struct brw_page *pga, int *requested_nobp,
786 int *niocountp, struct ptlrpc_request **reqp)
788 struct ptlrpc_request *req;
789 struct ptlrpc_bulk_desc *desc;
790 struct client_obd *cli = &imp->imp_obd->u.cli;
791 struct ost_body *body;
792 struct lustre_id *raw_id = obdo_id(oa);
793 struct obd_capa *ocapa = NULL;
794 struct lustre_capa *capa = NULL;
795 struct obd_ioobj *ioobj;
796 struct niobuf_remote *niobuf;
805 opc = ((cmd & OBD_BRW_WRITE) != 0) ? OST_WRITE : OST_READ;
807 for (niocount = i = 1; i < page_count; i++)
808 if (!can_merge_pages(&pga[i - 1], &pga[i]))
811 /* TODO: this could be optimized: thie capability can be
812 * found from ll_inode_info->lli_capas. */
813 /* partial write might cause read, both CAPA_READ and CAPA_WRITE
814 * capability could be used here */
815 capa_op = (opc == OST_WRITE) ? CAPA_WRITE : CAPA_READ | CAPA_WRITE;
816 ocapa = capa_get(oa->o_fsuid, capa_op, raw_id->li_fid.lf_group,
817 raw_id->li_stc.u.e3s.l3s_ino, CLIENT_CAPA);
819 size[bufcnt++] = sizeof(*body);
820 size[bufcnt++] = sizeof(*ioobj);
822 size[bufcnt++] = sizeof(*capa);
823 size[bufcnt++] = niocount * sizeof(*niobuf);
825 req = ptlrpc_prep_req(imp, LUSTRE_OBD_VERSION, opc, bufcnt, size, NULL);
829 if (opc == OST_WRITE)
830 desc = ptlrpc_prep_bulk_imp (req, page_count,
831 BULK_GET_SOURCE, OST_BULK_PORTAL);
833 desc = ptlrpc_prep_bulk_imp (req, page_count,
834 BULK_PUT_SINK, OST_BULK_PORTAL);
836 GOTO(out, rc = -ENOMEM);
837 /* NB request now owns desc and will free it when it gets freed */
840 body = lustre_msg_buf(req->rq_reqmsg, bufcnt++, sizeof(*body));
841 memcpy(&body->oa, oa, sizeof(*oa));
842 ioobj = lustre_msg_buf(req->rq_reqmsg, bufcnt++, sizeof(*ioobj));
844 capa = lustre_msg_buf(req->rq_reqmsg, bufcnt++, sizeof(*capa));
845 capa_dup(capa, ocapa);
846 body->oa.o_valid |= OBD_MD_CAPA;
848 niobuf = lustre_msg_buf(req->rq_reqmsg, bufcnt++,
849 niocount * sizeof(*niobuf));
851 obdo_to_ioobj(oa, ioobj);
852 ioobj->ioo_bufcnt = niocount;
854 LASSERT (page_count > 0);
856 for (requested_nob = i = 0; i < page_count; i++, niobuf++) {
857 struct brw_page *pg = &pga[i];
858 struct brw_page *pg_prev = pg - 1;
860 LASSERT(pg->count > 0);
861 LASSERTF((pg->page_offset & ~PAGE_MASK)+ pg->count <= PAGE_SIZE,
862 "i: %d pg: %p pg_off: "LPU64", count: %u\n", i, pg,
863 pg->page_offset, pg->count);
864 LASSERTF(i == 0 || pg->disk_offset > pg_prev->disk_offset,
865 "i %d p_c %u pg %p [pri %lu ind %lu] off "LPU64
866 " prev_pg %p [pri %lu ind %lu] off "LPU64"\n",
868 pg->pg, pg->pg->private, pg->pg->index, pg->disk_offset,
869 pg_prev->pg, pg_prev->pg->private, pg_prev->pg->index,
870 pg_prev->disk_offset);
872 if (opc == OST_WRITE) {
873 rc = osc_encrypt_page(pg->pg, pg->page_offset, pg->count);
878 ptlrpc_prep_bulk_page(desc, pg->pg,
879 pg->page_offset & ~PAGE_MASK, pg->count);
880 requested_nob += pg->count;
882 if (i > 0 && can_merge_pages(pg_prev, pg)) {
884 niobuf->len += pg->count;
886 niobuf->offset = pg->disk_offset;
887 niobuf->len = pg->count;
888 niobuf->flags = pg->flag;
892 LASSERT((void *)(niobuf - niocount) ==
893 lustre_msg_buf(req->rq_reqmsg, bufcnt - 1,
894 niocount * sizeof(*niobuf)));
895 osc_announce_cached(cli, &body->oa, opc == OST_WRITE ? requested_nob:0);
897 /* size[0] still sizeof (*body) */
898 if (opc == OST_WRITE) {
900 body->oa.o_valid |= OBD_MD_FLCKSUM;
901 body->oa.o_cksum = cksum_pages(requested_nob, page_count, pga);
903 /* 1 RC per niobuf */
904 size[1] = sizeof(__u32) * niocount;
905 req->rq_replen = lustre_msg_size(2, size);
907 /* 1 RC for the whole I/O */
908 req->rq_replen = lustre_msg_size(1, size);
911 *niocountp = niocount;
912 *requested_nobp = requested_nob;
917 ptlrpc_req_finished (req);
921 static int osc_brw_fini_request(struct ptlrpc_request *req, struct obdo *oa,
922 int requested_nob, int niocount,
923 obd_count page_count, struct brw_page *pga,
926 struct client_obd *cli = &req->rq_import->imp_obd->u.cli;
927 struct ost_body *body;
933 body = lustre_swab_repbuf(req, 0, sizeof(*body), lustre_swab_ost_body);
935 CERROR ("Can't unpack body\n");
939 osc_update_grant(cli, body);
940 memcpy(oa, &body->oa, sizeof(*oa));
942 if (req->rq_reqmsg->opc == OST_WRITE) {
944 CERROR ("Unexpected +ve rc %d\n", rc);
947 LASSERT (req->rq_bulk->bd_nob == requested_nob);
948 osc_decrypt_pages(pga, page_count);
949 RETURN(check_write_rcs(req, requested_nob, niocount,
953 if (rc > requested_nob) {
954 CERROR("Unexpected rc %d (%d requested)\n", rc, requested_nob);
958 if (rc != req->rq_bulk->bd_nob_transferred) {
959 CERROR ("Unexpected rc %d (%d transferred)\n",
960 rc, req->rq_bulk->bd_nob_transferred);
964 if (rc < requested_nob)
965 handle_short_read(rc, page_count, pga);
968 if (oa->o_valid & OBD_MD_FLCKSUM) {
969 const struct ptlrpc_peer *peer =
970 &req->rq_import->imp_connection->c_peer;
971 static int cksum_counter;
972 obd_count server_cksum = oa->o_cksum;
973 obd_count cksum = cksum_pages(rc, page_count, pga);
974 char str[PTL_NALFMT_SIZE];
976 ptlrpc_peernid2str(peer, str);
979 if (server_cksum != cksum) {
980 CERROR("Bad checksum: server %x, client %x, server NID "
981 LPX64" (%s)\n", server_cksum, cksum,
982 peer->peer_id.nid, str);
985 } else if ((cksum_counter & (-cksum_counter)) == cksum_counter){
986 CWARN("Checksum %u from "LPX64" (%s) OK: %x\n",
987 cksum_counter, peer->peer_id.nid, str, cksum);
990 static int cksum_missed;
993 if ((cksum_missed & (-cksum_missed)) == cksum_missed)
994 CERROR("Request checksum %u from "LPX64", no reply\n",
996 req->rq_import->imp_connection->c_peer.peer_id.nid);
999 osc_decrypt_pages(pga, page_count);
1003 static int osc_brw_internal(int cmd, struct obd_export *exp,struct obdo *oa,
1004 struct lov_stripe_md *lsm,
1005 obd_count page_count, struct brw_page *pga)
1009 struct ptlrpc_request *request;
1014 rc = osc_brw_prep_request(cmd, class_exp2cliimp(exp), oa, lsm,
1015 page_count, pga, &requested_nob, &niocount,
1020 rc = ptlrpc_queue_wait(request);
1022 if (rc == -ETIMEDOUT && request->rq_resend) {
1023 DEBUG_REQ(D_HA, request, "BULK TIMEOUT");
1024 ptlrpc_req_finished(request);
1028 rc = osc_brw_fini_request(request, oa, requested_nob, niocount,
1029 page_count, pga, rc);
1031 ptlrpc_req_finished(request);
1035 static int brw_interpret(struct ptlrpc_request *request,
1036 struct osc_brw_async_args *aa, int rc)
1038 struct obdo *oa = aa->aa_oa;
1039 int requested_nob = aa->aa_requested_nob;
1040 int niocount = aa->aa_nio_count;
1041 obd_count page_count = aa->aa_page_count;
1042 struct brw_page *pga = aa->aa_pga;
1045 rc = osc_brw_fini_request(request, oa, requested_nob, niocount,
1046 page_count, pga, rc);
1050 static int async_internal(int cmd, struct obd_export *exp, struct obdo *oa,
1051 struct lov_stripe_md *lsm, obd_count page_count,
1052 struct brw_page *pga, struct ptlrpc_request_set *set)
1054 struct ptlrpc_request *request;
1057 struct osc_brw_async_args *aa;
1061 rc = osc_brw_prep_request(cmd, class_exp2cliimp(exp), oa, lsm,
1062 page_count, pga, &requested_nob, &nio_count,
1065 LASSERT(sizeof(*aa) <= sizeof(request->rq_async_args));
1066 aa = (struct osc_brw_async_args *)&request->rq_async_args;
1068 aa->aa_requested_nob = requested_nob;
1069 aa->aa_nio_count = nio_count;
1070 aa->aa_page_count = page_count;
1073 request->rq_interpret_reply = brw_interpret;
1074 ptlrpc_set_add_req(set, request);
1080 #define min_t(type,x,y) \
1081 ({ type __x = (x); type __y = (y); __x < __y ? __x: __y; })
1085 * ugh, we want disk allocation on the target to happen in offset order. we'll
1086 * follow sedgewicks advice and stick to the dead simple shellsort -- it'll do
1087 * fine for our small page arrays and doesn't require allocation. its an
1088 * insertion sort that swaps elements that are strides apart, shrinking the
1089 * stride down until its '1' and the array is sorted.
1091 static void sort_brw_pages(struct brw_page *array, int num)
1094 struct brw_page tmp;
1098 for (stride = 1; stride < num ; stride = (stride * 3) + 1)
1103 for (i = stride ; i < num ; i++) {
1106 while (j >= stride && array[j - stride].disk_offset >
1108 array[j] = array[j - stride];
1113 } while (stride > 1);
1116 /* make sure we the regions we're passing to elan don't violate its '4
1117 * fragments' constraint. portal headers are a fragment, all full
1118 * PAGE_SIZE long pages count as 1 fragment, and each partial page
1119 * counts as a fragment. I think. see bug 934. */
1120 static obd_count check_elan_limit(struct brw_page *pg, obd_count pages)
1123 int saw_whole_frag = 0;
1126 for (i = 0 ; frags_left && i < pages ; pg++, i++) {
1127 if (pg->count == PAGE_SIZE) {
1128 if (!saw_whole_frag) {
1139 static int osc_brw(int cmd, struct obd_export *exp, struct obdo *oa,
1140 struct lov_stripe_md *lsm, obd_count page_count,
1141 struct brw_page *pga, struct obd_trans_info *oti)
1145 if (cmd == OBD_BRW_CHECK) {
1146 /* The caller just wants to know if there's a chance that this
1147 * I/O can succeed */
1148 struct obd_import *imp = class_exp2cliimp(exp);
1150 if (imp == NULL || imp->imp_invalid)
1155 while (page_count) {
1156 obd_count pages_per_brw;
1159 if (page_count > PTLRPC_MAX_BRW_PAGES)
1160 pages_per_brw = PTLRPC_MAX_BRW_PAGES;
1162 pages_per_brw = page_count;
1164 sort_brw_pages(pga, pages_per_brw);
1165 pages_per_brw = check_elan_limit(pga, pages_per_brw);
1167 rc = osc_brw_internal(cmd, exp, oa, lsm, pages_per_brw, pga);
1172 page_count -= pages_per_brw;
1173 pga += pages_per_brw;
1178 static int osc_brw_async(int cmd, struct obd_export *exp, struct obdo *oa,
1179 struct lov_stripe_md *lsm, obd_count page_count,
1180 struct brw_page *pga, struct ptlrpc_request_set *set,
1181 struct obd_trans_info *oti)
1185 if (cmd == OBD_BRW_CHECK) {
1186 /* The caller just wants to know if there's a chance that this
1187 * I/O can succeed */
1188 struct obd_import *imp = class_exp2cliimp(exp);
1190 if (imp == NULL || imp->imp_invalid)
1195 while (page_count) {
1196 obd_count pages_per_brw;
1199 if (page_count > PTLRPC_MAX_BRW_PAGES)
1200 pages_per_brw = PTLRPC_MAX_BRW_PAGES;
1202 pages_per_brw = page_count;
1204 sort_brw_pages(pga, pages_per_brw);
1205 pages_per_brw = check_elan_limit(pga, pages_per_brw);
1207 rc = async_internal(cmd, exp, oa, lsm, pages_per_brw, pga, set);
1212 page_count -= pages_per_brw;
1213 pga += pages_per_brw;
1218 static void osc_check_rpcs(struct client_obd *cli);
1219 static void osc_exit_cache(struct client_obd *cli, struct osc_async_page *oap,
1221 static void loi_list_maint(struct client_obd *cli, struct lov_oinfo *loi);
1222 static void lop_update_pending(struct client_obd *cli,
1223 struct loi_oap_pages *lop, int cmd, int delta);
1225 /* this is called when a sync waiter receives an interruption. Its job is to
1226 * get the caller woken as soon as possible. If its page hasn't been put in an
1227 * rpc yet it can dequeue immediately. Otherwise it has to mark the rpc as
1228 * desiring interruption which will forcefully complete the rpc once the rpc
1230 static void osc_occ_interrupted(struct oig_callback_context *occ)
1232 struct osc_async_page *oap;
1233 struct loi_oap_pages *lop;
1234 struct lov_oinfo *loi;
1237 /* XXX member_of() */
1238 oap = list_entry(occ, struct osc_async_page, oap_occ);
1240 spin_lock(&oap->oap_cli->cl_loi_list_lock);
1242 oap->oap_interrupted = 1;
1244 /* ok, it's been put in an rpc. */
1245 if (oap->oap_request != NULL) {
1246 ptlrpc_mark_interrupted(oap->oap_request);
1247 ptlrpcd_wake(oap->oap_request);
1251 /* we don't get interruption callbacks until osc_trigger_sync_io()
1252 * has been called and put the sync oaps in the pending/urgent lists.*/
1253 if (!list_empty(&oap->oap_pending_item)) {
1254 list_del_init(&oap->oap_pending_item);
1255 if (oap->oap_async_flags & ASYNC_URGENT)
1256 list_del_init(&oap->oap_urgent_item);
1259 lop = (oap->oap_cmd == OBD_BRW_WRITE) ?
1260 &loi->loi_write_lop : &loi->loi_read_lop;
1261 lop_update_pending(oap->oap_cli, lop, oap->oap_cmd, -1);
1262 loi_list_maint(oap->oap_cli, oap->oap_loi);
1264 oig_complete_one(oap->oap_oig, &oap->oap_occ, 0);
1265 oap->oap_oig = NULL;
1269 spin_unlock(&oap->oap_cli->cl_loi_list_lock);
1272 /* this must be called holding the loi list lock to give coverage to exit_cache,
1273 * async_flag maintenance, and oap_request */
1274 static void osc_ap_completion(struct client_obd *cli, struct obdo *oa,
1275 struct osc_async_page *oap, int sent, int rc)
1277 osc_exit_cache(cli, oap, sent);
1278 oap->oap_async_flags = 0;
1279 oap->oap_interrupted = 0;
1281 if (oap->oap_request != NULL) {
1282 ptlrpc_req_finished(oap->oap_request);
1283 oap->oap_request = NULL;
1286 if (rc == 0 && oa != NULL)
1287 oap->oap_loi->loi_blocks = oa->o_blocks;
1290 oig_complete_one(oap->oap_oig, &oap->oap_occ, rc);
1291 oap->oap_oig = NULL;
1296 oap->oap_caller_ops->ap_completion(oap->oap_caller_data, oap->oap_cmd,
1300 static int brw_interpret_oap(struct ptlrpc_request *request,
1301 struct osc_brw_async_args *aa, int rc)
1303 struct osc_async_page *oap;
1304 struct client_obd *cli;
1305 struct list_head *pos, *n;
1309 do_gettimeofday(&now);
1310 rc = osc_brw_fini_request(request, aa->aa_oa, aa->aa_requested_nob,
1311 aa->aa_nio_count, aa->aa_page_count,
1314 CDEBUG(D_INODE, "request %p aa %p rc %d\n", request, aa, rc);
1318 /* in failout recovery we ignore writeback failure and want
1319 * to just tell llite to unlock the page and continue */
1320 if (request->rq_reqmsg->opc == OST_WRITE &&
1321 (cli->cl_import == NULL || cli->cl_import->imp_invalid)) {
1322 CDEBUG(D_INODE, "flipping to rc 0 imp %p inv %d\n",
1324 cli->cl_import ? cli->cl_import->imp_invalid : -1);
1328 spin_lock(&cli->cl_loi_list_lock);
1330 if (request->rq_reqmsg->opc == OST_WRITE)
1331 lprocfs_stime_record(&cli->cl_write_stime, &now,
1332 &request->rq_rpcd_start);
1334 lprocfs_stime_record(&cli->cl_read_stime, &now,
1335 &request->rq_rpcd_start);
1337 /* We need to decrement before osc_ap_completion->osc_wake_cache_waiters
1338 * is called so we know whether to go to sync BRWs or wait for more
1339 * RPCs to complete */
1340 if (request->rq_reqmsg->opc == OST_WRITE)
1341 cli->cl_w_in_flight--;
1343 cli->cl_r_in_flight--;
1345 /* the caller may re-use the oap after the completion call so
1346 * we need to clean it up a little */
1347 list_for_each_safe(pos, n, &aa->aa_oaps) {
1348 oap = list_entry(pos, struct osc_async_page, oap_rpc_item);
1350 //CDEBUG(D_INODE, "page %p index %lu oap %p\n",
1351 //oap->oap_page, oap->oap_page->index, oap);
1353 list_del_init(&oap->oap_rpc_item);
1354 osc_ap_completion(cli, aa->aa_oa, oap, 1, rc);
1357 /* no write RPCs in flight, reset the time */
1358 if (request->rq_reqmsg->opc == OST_WRITE && cli->cl_w_in_flight == 0)
1359 do_gettimeofday(&cli->cl_last_write_time);
1361 osc_wake_cache_waiters(cli);
1362 osc_check_rpcs(cli);
1363 spin_unlock(&cli->cl_loi_list_lock);
1365 obdo_free(aa->aa_oa);
1366 OBD_FREE(aa->aa_pga, aa->aa_page_count * sizeof(struct brw_page));
1371 static struct ptlrpc_request *osc_build_req(struct client_obd *cli,
1372 struct list_head *rpc_list,
1373 int page_count, int cmd)
1375 struct ptlrpc_request *req;
1376 struct brw_page *pga = NULL;
1377 int requested_nob, nio_count;
1378 struct osc_brw_async_args *aa;
1379 struct obdo *oa = NULL;
1380 struct obd_async_page_ops *ops = NULL;
1381 void *caller_data = NULL;
1382 struct list_head *pos;
1385 LASSERT(!list_empty(rpc_list));
1387 OBD_ALLOC(pga, sizeof(*pga) * page_count);
1389 RETURN(ERR_PTR(-ENOMEM));
1393 GOTO(out, req = ERR_PTR(-ENOMEM));
1396 list_for_each(pos, rpc_list) {
1397 struct osc_async_page *oap;
1399 oap = list_entry(pos, struct osc_async_page, oap_rpc_item);
1401 ops = oap->oap_caller_ops;
1402 caller_data = oap->oap_caller_data;
1404 pga[i].disk_offset = oap->oap_obj_off + oap->oap_page_off;
1405 pga[i].page_offset = pga[i].disk_offset;
1406 pga[i].pg = oap->oap_page;
1407 pga[i].count = oap->oap_count;
1408 pga[i].flag = oap->oap_brw_flags;
1409 CDEBUG(0, "put page %p index %lu oap %p flg %x to pga\n",
1410 pga[i].pg, oap->oap_page->index, oap, pga[i].flag);
1414 /* always get the data for the obdo for the rpc */
1415 LASSERT(ops != NULL);
1416 ops->ap_fill_obdo(caller_data, cmd, oa);
1418 sort_brw_pages(pga, page_count);
1419 rc = osc_brw_prep_request(cmd, cli->cl_import, oa, NULL, page_count,
1420 pga, &requested_nob, &nio_count, &req);
1422 CERROR("prep_req failed: %d\n", rc);
1423 GOTO(out, req = ERR_PTR(rc));
1426 LASSERT(sizeof(*aa) <= sizeof(req->rq_async_args));
1427 aa = (struct osc_brw_async_args *)&req->rq_async_args;
1429 aa->aa_requested_nob = requested_nob;
1430 aa->aa_nio_count = nio_count;
1431 aa->aa_page_count = page_count;
1440 OBD_FREE(pga, sizeof(*pga) * page_count);
1445 /* strange write gap too long (15s) */
1446 #define CLI_ODD_WRITE_GAP 15000000
1448 static void lop_update_pending(struct client_obd *cli,
1449 struct loi_oap_pages *lop, int cmd, int delta)
1451 lop->lop_num_pending += delta;
1452 if (cmd == OBD_BRW_WRITE)
1453 cli->cl_pending_w_pages += delta;
1455 cli->cl_pending_r_pages += delta;
1458 static long timeval_sub(struct timeval *large, struct timeval *small)
1460 return (large->tv_sec - small->tv_sec) * 1000000 +
1461 (large->tv_usec - small->tv_usec);
1464 /* the loi lock is held across this function but it's allowed to release
1465 * and reacquire it during its work */
1466 static int osc_send_oap_rpc(struct client_obd *cli, struct lov_oinfo *loi,
1467 int cmd, struct loi_oap_pages *lop)
1469 struct ptlrpc_request *request;
1470 obd_count page_count = 0;
1471 struct list_head *tmp, *pos;
1472 struct osc_async_page *oap = NULL;
1473 struct osc_brw_async_args *aa;
1474 struct obd_async_page_ops *ops;
1475 LIST_HEAD(rpc_list);
1478 LASSERT(lop != LP_POISON);
1479 LASSERT(lop->lop_pending.next != LP_POISON);
1481 /* first we find the pages we're allowed to work with */
1482 list_for_each_safe(pos, tmp, &lop->lop_pending) {
1483 oap = list_entry(pos, struct osc_async_page, oap_pending_item);
1484 ops = oap->oap_caller_ops;
1486 LASSERT(oap->oap_magic == OAP_MAGIC);
1488 /* in llite being 'ready' equates to the page being locked
1489 * until completion unlocks it. commit_write submits a page
1490 * as not ready because its unlock will happen unconditionally
1491 * as the call returns. if we race with commit_write giving
1492 * us that page we dont' want to create a hole in the page
1493 * stream, so we stop and leave the rpc to be fired by
1494 * another dirtier or kupdated interval (the not ready page
1495 * will still be on the dirty list). we could call in
1496 * at the end of ll_file_write to process the queue again. */
1497 if (!(oap->oap_async_flags & ASYNC_READY)) {
1498 int rc = ops->ap_make_ready(oap->oap_caller_data, cmd);
1500 CDEBUG(D_INODE, "oap %p page %p returned %d "
1501 "instead of ready\n", oap,
1505 /* llite is telling us that the page is still
1506 * in commit_write and that we should try
1507 * and put it in an rpc again later. we
1508 * break out of the loop so we don't create
1509 * a hole in the sequence of pages in the rpc
1514 /* the io isn't needed.. tell the checks
1515 * below to complete the rpc with EINTR */
1516 oap->oap_async_flags |= ASYNC_COUNT_STABLE;
1517 oap->oap_count = -EINTR;
1520 oap->oap_async_flags |= ASYNC_READY;
1523 LASSERTF(0, "oap %p page %p returned %d "
1524 "from make_ready\n", oap,
1532 * Page submitted for IO has to be locked. Either by
1533 * ->ap_make_ready() or by higher layers.
1535 * XXX nikita: this assertion should be adjusted when lustre
1536 * starts using PG_writeback for pages being written out.
1538 LASSERT(PageLocked(oap->oap_page));
1540 /* take the page out of our book-keeping */
1541 list_del_init(&oap->oap_pending_item);
1542 lop_update_pending(cli, lop, cmd, -1);
1543 list_del_init(&oap->oap_urgent_item);
1545 /* ask the caller for the size of the io as the rpc leaves. */
1546 if (!(oap->oap_async_flags & ASYNC_COUNT_STABLE))
1548 ops->ap_refresh_count(oap->oap_caller_data,cmd);
1549 if (oap->oap_count <= 0) {
1550 CDEBUG(D_CACHE, "oap %p count %d, completing\n", oap,
1552 osc_ap_completion(cli, NULL, oap, 0, oap->oap_count);
1556 /* now put the page back in our accounting */
1557 list_add_tail(&oap->oap_rpc_item, &rpc_list);
1558 if (++page_count >= cli->cl_max_pages_per_rpc)
1562 osc_wake_cache_waiters(cli);
1564 if (page_count == 0)
1567 loi_list_maint(cli, loi);
1568 spin_unlock(&cli->cl_loi_list_lock);
1570 request = osc_build_req(cli, &rpc_list, page_count, cmd);
1571 if (IS_ERR(request)) {
1572 /* this should happen rarely and is pretty bad, it makes the
1573 * pending list not follow the dirty order */
1574 spin_lock(&cli->cl_loi_list_lock);
1575 list_for_each_safe(pos, tmp, &rpc_list) {
1576 oap = list_entry(pos, struct osc_async_page,
1578 list_del_init(&oap->oap_rpc_item);
1580 /* queued sync pages can be torn down while the pages
1581 * were between the pending list and the rpc */
1582 if (oap->oap_interrupted) {
1583 CDEBUG(D_INODE, "oap %p interrupted\n", oap);
1584 osc_ap_completion(cli, NULL, oap, 0,
1589 /* put the page back in the loi/lop lists */
1590 list_add_tail(&oap->oap_pending_item,
1592 lop_update_pending(cli, lop, cmd, 1);
1593 if (oap->oap_async_flags & ASYNC_URGENT)
1594 list_add(&oap->oap_urgent_item,
1597 loi_list_maint(cli, loi);
1598 RETURN(PTR_ERR(request));
1601 LASSERT(sizeof(*aa) <= sizeof(request->rq_async_args));
1602 aa = (struct osc_brw_async_args *)&request->rq_async_args;
1603 INIT_LIST_HEAD(&aa->aa_oaps);
1604 list_splice(&rpc_list, &aa->aa_oaps);
1605 INIT_LIST_HEAD(&rpc_list);
1608 if (cmd == OBD_BRW_READ) {
1609 lprocfs_oh_tally_log2(&cli->cl_read_page_hist, page_count);
1610 lprocfs_oh_tally(&cli->cl_read_rpc_hist, cli->cl_r_in_flight);
1612 lprocfs_oh_tally_log2(&cli->cl_write_page_hist, page_count);
1613 lprocfs_oh_tally(&cli->cl_write_rpc_hist,
1614 cli->cl_w_in_flight);
1618 spin_lock(&cli->cl_loi_list_lock);
1620 /* collect write gaps and sum of them */
1621 if (cmd == OBD_BRW_WRITE && cli->cl_w_in_flight == 0) {
1625 do_gettimeofday(&now);
1627 if (cli->cl_last_write_time.tv_sec) {
1628 diff = timeval_sub(&now, &cli->cl_last_write_time);
1629 if (diff < CLI_ODD_WRITE_GAP) {
1630 cli->cl_write_gap_sum += diff;
1631 cli->cl_write_gaps++;
1636 if (cmd == OBD_BRW_READ) {
1637 cli->cl_r_in_flight++;
1640 cli->cl_w_in_flight++;
1641 cli->cl_write_num++;
1644 /* queued sync pages can be torn down while the pages
1645 * were between the pending list and the rpc */
1646 list_for_each(pos, &aa->aa_oaps) {
1647 oap = list_entry(pos, struct osc_async_page, oap_rpc_item);
1648 if (oap->oap_interrupted) {
1649 CDEBUG(D_INODE, "oap %p in req %p interrupted\n",
1651 ptlrpc_mark_interrupted(request);
1656 CDEBUG(D_INODE, "req %p: %d pages, aa %p. now %dr/%dw in flight\n",
1657 request, page_count, aa, cli->cl_r_in_flight,
1658 cli->cl_w_in_flight);
1660 oap->oap_request = ptlrpc_request_addref(request);
1661 request->rq_interpret_reply = brw_interpret_oap;
1663 ptlrpcd_add_req(request);
1667 static int lop_makes_rpc(struct client_obd *cli, struct loi_oap_pages *lop,
1673 if (lop->lop_num_pending == 0)
1676 /* if we have an invalid import we want to drain the queued pages
1677 * by forcing them through rpcs that immediately fail and complete
1678 * the pages. recovery relies on this to empty the queued pages
1679 * before canceling the locks and evicting down the llite pages */
1680 if (cli->cl_import == NULL || cli->cl_import->imp_invalid)
1683 /* stream rpcs in queue order as long as as there is an urgent page
1684 * queued. this is our cheap solution for good batching in the case
1685 * where writepage marks some random page in the middle of the file as
1686 * urgent because of, say, memory pressure */
1687 if (!list_empty(&lop->lop_urgent))
1690 /* fire off rpcs when we have 'optimal' rpcs as tuned for the wire. */
1691 optimal = cli->cl_max_pages_per_rpc;
1692 if (cmd == OBD_BRW_WRITE) {
1693 /* trigger a write rpc stream as long as there are dirtiers
1694 * waiting for space. as they're waiting, they're not going to
1695 * create more pages to coallesce with what's waiting.. */
1696 if (!list_empty(&cli->cl_cache_waiters))
1699 /* *2 to avoid triggering rpcs that would want to include pages
1700 * that are being queued but which can't be made ready until
1701 * the queuer finishes with the page. this is a wart for
1702 * llite::commit_write() */
1705 if (lop->lop_num_pending >= optimal)
1711 static void on_list(struct list_head *item, struct list_head *list,
1714 if (list_empty(item) && should_be_on)
1715 list_add_tail(item, list);
1716 else if (!list_empty(item) && !should_be_on)
1717 list_del_init(item);
1720 /* maintain the loi's cli list membership invariants so that osc_send_oap_rpc
1721 * can find pages to build into rpcs quickly */
1722 static void loi_list_maint(struct client_obd *cli, struct lov_oinfo *loi)
1724 on_list(&loi->loi_cli_item, &cli->cl_loi_ready_list,
1725 lop_makes_rpc(cli, &loi->loi_write_lop, OBD_BRW_WRITE) ||
1726 lop_makes_rpc(cli, &loi->loi_read_lop, OBD_BRW_READ));
1728 on_list(&loi->loi_write_item, &cli->cl_loi_write_list,
1729 loi->loi_write_lop.lop_num_pending);
1731 on_list(&loi->loi_read_item, &cli->cl_loi_read_list,
1732 loi->loi_read_lop.lop_num_pending);
1735 #define LOI_DEBUG(LOI, STR, args...) \
1736 CDEBUG(D_INODE, "loi ready %d wr %d:%d rd %d:%d " STR, \
1737 !list_empty(&(LOI)->loi_cli_item), \
1738 (LOI)->loi_write_lop.lop_num_pending, \
1739 !list_empty(&(LOI)->loi_write_lop.lop_urgent), \
1740 (LOI)->loi_read_lop.lop_num_pending, \
1741 !list_empty(&(LOI)->loi_read_lop.lop_urgent), \
1744 struct lov_oinfo *osc_next_loi(struct client_obd *cli)
1747 /* first return all objects which we already know to have
1748 * pages ready to be stuffed into rpcs */
1749 if (!list_empty(&cli->cl_loi_ready_list))
1750 RETURN(list_entry(cli->cl_loi_ready_list.next,
1751 struct lov_oinfo, loi_cli_item));
1753 /* then if we have cache waiters, return all objects with queued
1754 * writes. This is especially important when many small files
1755 * have filled up the cache and not been fired into rpcs because
1756 * they don't pass the nr_pending/object threshhold */
1757 if (!list_empty(&cli->cl_cache_waiters) &&
1758 !list_empty(&cli->cl_loi_write_list))
1759 RETURN(list_entry(cli->cl_loi_write_list.next,
1760 struct lov_oinfo, loi_write_item));
1762 /* then return all queued objects when we have an invalid import
1763 * so that they get flushed */
1764 if (cli->cl_import == NULL || cli->cl_import->imp_invalid) {
1765 if (!list_empty(&cli->cl_loi_write_list))
1766 RETURN(list_entry(cli->cl_loi_write_list.next,
1767 struct lov_oinfo, loi_write_item));
1768 if (!list_empty(&cli->cl_loi_read_list))
1769 RETURN(list_entry(cli->cl_loi_read_list.next,
1770 struct lov_oinfo, loi_read_item));
1775 /* called with the loi list lock held */
1776 static void osc_check_rpcs(struct client_obd *cli)
1778 struct lov_oinfo *loi;
1779 int rc = 0, race_counter = 0;
1782 while ((loi = osc_next_loi(cli)) != NULL) {
1783 LOI_DEBUG(loi, "%lu in flight\n", rpcs_in_flight(cli));
1784 LASSERT(loi->loi_ost_idx != LL_POISON);
1786 if (rpcs_in_flight(cli) >= cli->cl_max_rpcs_in_flight)
1789 /* attempt some read/write balancing by alternating between
1790 * reads and writes in an object. The makes_rpc checks here
1791 * would be redundant if we were getting read/write work items
1792 * instead of objects. we don't want send_oap_rpc to drain a
1793 * partial read pending queue when we're given this object to
1794 * do io on writes while there are cache waiters */
1795 if (lop_makes_rpc(cli, &loi->loi_write_lop, OBD_BRW_WRITE)) {
1796 rc = osc_send_oap_rpc(cli, loi, OBD_BRW_WRITE,
1797 &loi->loi_write_lop);
1805 if (lop_makes_rpc(cli, &loi->loi_read_lop, OBD_BRW_READ)) {
1806 rc = osc_send_oap_rpc(cli, loi, OBD_BRW_READ,
1807 &loi->loi_read_lop);
1816 /* attempt some inter-object balancing by issueing rpcs
1817 * for each object in turn */
1818 if (!list_empty(&loi->loi_cli_item))
1819 list_del_init(&loi->loi_cli_item);
1820 if (!list_empty(&loi->loi_write_item))
1821 list_del_init(&loi->loi_write_item);
1822 if (!list_empty(&loi->loi_read_item))
1823 list_del_init(&loi->loi_read_item);
1825 loi_list_maint(cli, loi);
1827 /* send_oap_rpc fails with 0 when make_ready tells it to
1828 * back off. llite's make_ready does this when it tries
1829 * to lock a page queued for write that is already locked.
1830 * we want to try sending rpcs from many objects, but we
1831 * don't want to spin failing with 0. */
1832 if (race_counter == 10)
1838 /* we're trying to queue a page in the osc so we're subject to the
1839 * 'cl_dirty_max' limit on the number of pages that can be queued in the osc.
1840 * If the osc's queued pages are already at that limit, then we want to sleep
1841 * until there is space in the osc's queue for us. We also may be waiting for
1842 * write credits from the OST if there are RPCs in flight that may return some
1843 * before we fall back to sync writes.
1845 * We need this know our allocation was granted in the presence of signals */
1846 static int ocw_granted(struct client_obd *cli, struct osc_cache_waiter *ocw)
1850 spin_lock(&cli->cl_loi_list_lock);
1851 rc = list_empty(&ocw->ocw_entry) || rpcs_in_flight(cli) == 0;
1852 spin_unlock(&cli->cl_loi_list_lock);
1856 /* Caller must hold loi_list_lock - we drop/regain it if we need to wait for
1857 * grant or cache space. */
1858 static int osc_enter_cache(struct client_obd *cli, struct lov_oinfo *loi,
1859 struct osc_async_page *oap)
1861 struct osc_cache_waiter ocw;
1862 struct l_wait_info lwi = { 0 };
1863 struct timeval start, stop;
1866 CDEBUG(D_CACHE, "dirty: %ld dirty_max: %ld dropped: %lu grant: %lu\n",
1867 cli->cl_dirty, cli->cl_dirty_max, cli->cl_lost_grant,
1868 cli->cl_avail_grant);
1870 if (cli->cl_dirty_max < PAGE_SIZE)
1873 if (~0ul - cli->cl_dirty_sum <= cli->cl_dirty) {
1874 cli->cl_dirty_av = (cli->cl_dirty_av +
1875 (cli->cl_dirty_sum / cli->cl_dirty_num)) / 2;
1876 cli->cl_dirty_num = 0;
1877 cli->cl_dirty_sum = 0;
1879 if (cli->cl_dirty_num)
1880 cli->cl_dirty_av = (cli->cl_dirty_sum / cli->cl_dirty_num);
1883 cli->cl_dirty_num++;
1884 cli->cl_dirty_sum += cli->cl_dirty;
1886 if (cli->cl_dirty > cli->cl_dirty_dmax)
1887 cli->cl_dirty_dmax = cli->cl_dirty;
1888 if (cli->cl_dirty < cli->cl_dirty_dmin || !cli->cl_dirty_dmin)
1889 cli->cl_dirty_dmin = cli->cl_dirty;
1891 /* Hopefully normal case - cache space and write credits available */
1892 if (cli->cl_dirty + PAGE_SIZE <= cli->cl_dirty_max &&
1893 cli->cl_avail_grant >= PAGE_SIZE) {
1894 /* account for ourselves */
1895 osc_consume_write_grant(cli, oap);
1899 /* Make sure that there are write rpcs in flight to wait for. This
1900 * is a little silly as this object may not have any pending but
1901 * other objects sure might. */
1902 if (cli->cl_w_in_flight) {
1903 list_add_tail(&ocw.ocw_entry, &cli->cl_cache_waiters);
1904 init_waitqueue_head(&ocw.ocw_waitq);
1908 loi_list_maint(cli, loi);
1909 osc_check_rpcs(cli);
1910 spin_unlock(&cli->cl_loi_list_lock);
1912 CDEBUG(0, "sleeping for cache space\n");
1913 do_gettimeofday(&start);
1914 l_wait_event(ocw.ocw_waitq, ocw_granted(cli, &ocw), &lwi);
1915 do_gettimeofday(&stop);
1917 cli->cl_cache_wait_num++;
1918 cli->cl_cache_wait_sum += timeval_sub(&stop, &start);
1920 spin_lock(&cli->cl_loi_list_lock);
1921 lprocfs_stime_record(&cli->cl_enter_stime, &stop, &start);
1922 if (!list_empty(&ocw.ocw_entry)) {
1923 list_del(&ocw.ocw_entry);
1931 cli->cl_sync_rpcs++;
1935 /* the companion to enter_cache, called when an oap is no longer part of the
1936 * dirty accounting.. so writeback completes or truncate happens before writing
1937 * starts. must be called with the loi lock held. */
1938 static void osc_exit_cache(struct client_obd *cli, struct osc_async_page *oap,
1943 if (!(oap->oap_brw_flags & OBD_BRW_FROM_GRANT)) {
1948 if (~0ul - cli->cl_dirty_sum <= cli->cl_dirty) {
1949 cli->cl_dirty_av = (cli->cl_dirty_av +
1950 (cli->cl_dirty_sum / cli->cl_dirty_num)) / 2;
1951 cli->cl_dirty_num = 0;
1952 cli->cl_dirty_sum = 0;
1954 if (cli->cl_dirty_num)
1955 cli->cl_dirty_av = (cli->cl_dirty_sum / cli->cl_dirty_num);
1958 cli->cl_dirty_num++;
1959 cli->cl_dirty_sum += cli->cl_dirty;
1961 if (cli->cl_dirty > cli->cl_dirty_dmax)
1962 cli->cl_dirty_dmax = cli->cl_dirty;
1963 if (cli->cl_dirty < cli->cl_dirty_dmin || !cli->cl_dirty_dmin)
1964 cli->cl_dirty_dmin = cli->cl_dirty;
1966 oap->oap_brw_flags &= ~OBD_BRW_FROM_GRANT;
1967 cli->cl_dirty -= PAGE_SIZE;
1969 cli->cl_lost_grant += PAGE_SIZE;
1970 CDEBUG(D_CACHE, "lost grant: %lu avail grant: %lu dirty: %lu\n",
1971 cli->cl_lost_grant, cli->cl_avail_grant, cli->cl_dirty);
1977 int osc_prep_async_page(struct obd_export *exp, struct lov_stripe_md *lsm,
1978 struct lov_oinfo *loi, struct page *page,
1979 obd_off offset, struct obd_async_page_ops *ops,
1980 void *data, void **res)
1982 struct osc_async_page *oap;
1985 OBD_ALLOC(oap, sizeof(*oap));
1989 oap->oap_magic = OAP_MAGIC;
1990 oap->oap_cli = &exp->exp_obd->u.cli;
1993 oap->oap_caller_ops = ops;
1994 oap->oap_caller_data = data;
1996 oap->oap_page = page;
1997 oap->oap_obj_off = offset;
1999 INIT_LIST_HEAD(&oap->oap_pending_item);
2000 INIT_LIST_HEAD(&oap->oap_urgent_item);
2001 INIT_LIST_HEAD(&oap->oap_rpc_item);
2003 oap->oap_occ.occ_interrupted = osc_occ_interrupted;
2005 CDEBUG(D_CACHE, "oap %p page %p obj off "LPU64"\n", oap, page, offset);
2010 static int osc_queue_async_io(struct obd_export *exp, struct lov_stripe_md *lsm,
2011 struct lov_oinfo *loi, void *cookie,
2012 int cmd, obd_off off, int count,
2013 obd_flags brw_flags, enum async_flags async_flags)
2015 struct client_obd *cli = &exp->exp_obd->u.cli;
2016 struct osc_async_page *oap;
2017 struct loi_oap_pages *lop;
2021 oap = OAP_FROM_COOKIE(cookie);
2023 if (cli->cl_import == NULL || cli->cl_import->imp_invalid)
2026 if (!list_empty(&oap->oap_pending_item) ||
2027 !list_empty(&oap->oap_urgent_item) ||
2028 !list_empty(&oap->oap_rpc_item))
2032 loi = &lsm->lsm_oinfo[0];
2034 spin_lock(&cli->cl_loi_list_lock);
2037 oap->oap_async_flags = async_flags;
2038 oap->oap_page_off = off;
2039 oap->oap_count = count;
2040 oap->oap_brw_flags = brw_flags;
2042 if (cmd == OBD_BRW_WRITE) {
2043 rc = osc_enter_cache(cli, loi, oap);
2045 spin_unlock(&cli->cl_loi_list_lock);
2048 lop = &loi->loi_write_lop;
2050 lop = &loi->loi_read_lop;
2053 if (oap->oap_async_flags & ASYNC_URGENT)
2054 list_add(&oap->oap_urgent_item, &lop->lop_urgent);
2055 list_add_tail(&oap->oap_pending_item, &lop->lop_pending);
2056 lop_update_pending(cli, lop, cmd, 1);
2058 loi_list_maint(cli, loi);
2060 LOI_DEBUG(loi, "oap %p page %p added for cmd %d\n", oap, oap->oap_page,
2063 osc_check_rpcs(cli);
2064 spin_unlock(&cli->cl_loi_list_lock);
2069 /* aka (~was & now & flag), but this is more clear :) */
2070 #define SETTING(was, now, flag) (!(was & flag) && (now & flag))
2072 static int osc_set_async_flags(struct obd_export *exp,
2073 struct lov_stripe_md *lsm,
2074 struct lov_oinfo *loi, void *cookie,
2075 obd_flags async_flags)
2077 struct client_obd *cli = &exp->exp_obd->u.cli;
2078 struct loi_oap_pages *lop;
2079 struct osc_async_page *oap;
2083 oap = OAP_FROM_COOKIE(cookie);
2085 if (cli->cl_import == NULL || cli->cl_import->imp_invalid)
2089 loi = &lsm->lsm_oinfo[0];
2091 if (oap->oap_cmd == OBD_BRW_WRITE) {
2092 lop = &loi->loi_write_lop;
2094 lop = &loi->loi_read_lop;
2097 spin_lock(&cli->cl_loi_list_lock);
2099 if (list_empty(&oap->oap_pending_item))
2100 GOTO(out, rc = -EINVAL);
2102 if ((oap->oap_async_flags & async_flags) == async_flags)
2105 if (SETTING(oap->oap_async_flags, async_flags, ASYNC_READY))
2106 oap->oap_async_flags |= ASYNC_READY;
2108 if (SETTING(oap->oap_async_flags, async_flags, ASYNC_URGENT)) {
2109 if (list_empty(&oap->oap_rpc_item)) {
2110 list_add(&oap->oap_urgent_item, &lop->lop_urgent);
2111 loi_list_maint(cli, loi);
2115 LOI_DEBUG(loi, "oap %p page %p has flags %x\n", oap, oap->oap_page,
2116 oap->oap_async_flags);
2118 osc_check_rpcs(cli);
2119 spin_unlock(&cli->cl_loi_list_lock);
2123 static int osc_queue_group_io(struct obd_export *exp, struct lov_stripe_md *lsm,
2124 struct lov_oinfo *loi,
2125 struct obd_io_group *oig, void *cookie,
2126 int cmd, obd_off off, int count,
2127 obd_flags brw_flags,
2128 obd_flags async_flags)
2130 struct client_obd *cli = &exp->exp_obd->u.cli;
2131 struct osc_async_page *oap;
2132 struct loi_oap_pages *lop;
2135 oap = OAP_FROM_COOKIE(cookie);
2137 if (cli->cl_import == NULL || cli->cl_import->imp_invalid)
2140 if (!list_empty(&oap->oap_pending_item) ||
2141 !list_empty(&oap->oap_urgent_item) ||
2142 !list_empty(&oap->oap_rpc_item))
2146 loi = &lsm->lsm_oinfo[0];
2148 spin_lock(&cli->cl_loi_list_lock);
2151 oap->oap_page_off = off;
2152 oap->oap_count = count;
2153 oap->oap_brw_flags = brw_flags;
2154 oap->oap_async_flags = async_flags;
2156 if (cmd == OBD_BRW_WRITE)
2157 lop = &loi->loi_write_lop;
2159 lop = &loi->loi_read_lop;
2161 list_add_tail(&oap->oap_pending_item, &lop->lop_pending_group);
2162 if (oap->oap_async_flags & ASYNC_GROUP_SYNC) {
2164 oig_add_one(oig, &oap->oap_occ);
2167 LOI_DEBUG(loi, "oap %p page %p on group pending\n", oap, oap->oap_page);
2169 spin_unlock(&cli->cl_loi_list_lock);
2174 static void osc_group_to_pending(struct client_obd *cli, struct lov_oinfo *loi,
2175 struct loi_oap_pages *lop, int cmd)
2177 struct list_head *pos, *tmp;
2178 struct osc_async_page *oap;
2180 list_for_each_safe(pos, tmp, &lop->lop_pending_group) {
2181 oap = list_entry(pos, struct osc_async_page, oap_pending_item);
2182 list_del(&oap->oap_pending_item);
2183 list_add_tail(&oap->oap_pending_item, &lop->lop_pending);
2184 list_add(&oap->oap_urgent_item, &lop->lop_urgent);
2185 lop_update_pending(cli, lop, cmd, 1);
2187 loi_list_maint(cli, loi);
2190 static int osc_trigger_group_io(struct obd_export *exp,
2191 struct lov_stripe_md *lsm,
2192 struct lov_oinfo *loi,
2193 struct obd_io_group *oig)
2195 struct client_obd *cli = &exp->exp_obd->u.cli;
2199 loi = &lsm->lsm_oinfo[0];
2201 spin_lock(&cli->cl_loi_list_lock);
2203 osc_group_to_pending(cli, loi, &loi->loi_write_lop, OBD_BRW_WRITE);
2204 osc_group_to_pending(cli, loi, &loi->loi_read_lop, OBD_BRW_READ);
2206 osc_check_rpcs(cli);
2207 spin_unlock(&cli->cl_loi_list_lock);
2212 static int osc_teardown_async_page(struct obd_export *exp,
2213 struct lov_stripe_md *lsm,
2214 struct lov_oinfo *loi, void *cookie)
2216 struct client_obd *cli = &exp->exp_obd->u.cli;
2217 struct loi_oap_pages *lop;
2218 struct osc_async_page *oap;
2222 oap = OAP_FROM_COOKIE(cookie);
2225 loi = &lsm->lsm_oinfo[0];
2227 if (oap->oap_cmd == OBD_BRW_WRITE) {
2228 lop = &loi->loi_write_lop;
2230 lop = &loi->loi_read_lop;
2233 spin_lock(&cli->cl_loi_list_lock);
2235 if (!list_empty(&oap->oap_rpc_item))
2236 GOTO(out, rc = -EBUSY);
2238 osc_exit_cache(cli, oap, 0);
2239 osc_wake_cache_waiters(cli);
2241 if (!list_empty(&oap->oap_urgent_item)) {
2242 list_del_init(&oap->oap_urgent_item);
2243 oap->oap_async_flags &= ~ASYNC_URGENT;
2245 if (!list_empty(&oap->oap_pending_item)) {
2246 list_del_init(&oap->oap_pending_item);
2247 lop_update_pending(cli, lop, oap->oap_cmd, -1);
2249 loi_list_maint(cli, loi);
2251 LOI_DEBUG(loi, "oap %p page %p torn down\n", oap, oap->oap_page);
2253 spin_unlock(&cli->cl_loi_list_lock);
2255 OBD_FREE(oap, sizeof(*oap));
2260 /* Note: caller will lock/unlock, and set uptodate on the pages */
2261 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
2262 static int sanosc_brw_read(struct obd_export *exp, struct obdo *oa,
2263 struct lov_stripe_md *lsm, obd_count page_count,
2264 struct brw_page *pga)
2266 struct ptlrpc_request *request = NULL;
2267 struct ost_body *body;
2268 struct niobuf_remote *nioptr;
2269 struct obd_ioobj *iooptr;
2270 int rc, size[3] = {sizeof(*body)}, mapped = 0;
2274 /* XXX does not handle 'new' brw protocol */
2276 size[1] = sizeof(struct obd_ioobj);
2277 size[2] = page_count * sizeof(*nioptr);
2279 request = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_OBD_VERSION,
2280 OST_SAN_READ, 3, size, NULL);
2284 body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof(*body));
2285 iooptr = lustre_msg_buf(request->rq_reqmsg, 1, sizeof(*iooptr));
2286 nioptr = lustre_msg_buf(request->rq_reqmsg, 2,
2287 sizeof(*nioptr) * page_count);
2289 memcpy(&body->oa, oa, sizeof(body->oa));
2291 obdo_to_ioobj(oa, iooptr);
2292 iooptr->ioo_bufcnt = page_count;
2294 for (mapped = 0; mapped < page_count; mapped++, nioptr++) {
2295 LASSERT(PageLocked(pga[mapped].pg));
2296 LASSERT(mapped == 0 ||
2297 pga[mapped].disk_offset > pga[mapped - 1].disk_offset);
2299 nioptr->offset = pga[mapped].disk_offset;
2300 nioptr->len = pga[mapped].count;
2301 nioptr->flags = pga[mapped].flag;
2304 size[1] = page_count * sizeof(*nioptr);
2305 request->rq_replen = lustre_msg_size(2, size);
2307 rc = ptlrpc_queue_wait(request);
2311 body = lustre_swab_repbuf(request, 0, sizeof(*body),
2312 lustre_swab_ost_body);
2314 CERROR("Can't unpack body\n");
2315 GOTO(out_req, rc = -EPROTO);
2318 memcpy(oa, &body->oa, sizeof(*oa));
2320 swab = lustre_msg_swabbed(request->rq_repmsg);
2321 LASSERT_REPSWAB(request, 1);
2322 nioptr = lustre_msg_buf(request->rq_repmsg, 1, size[1]);
2324 /* nioptr missing or short */
2325 GOTO(out_req, rc = -EPROTO);
2329 for (mapped = 0; mapped < page_count; mapped++, nioptr++) {
2330 struct page *page = pga[mapped].pg;
2331 struct buffer_head *bh;
2335 lustre_swab_niobuf_remote (nioptr);
2337 /* got san device associated */
2338 LASSERT(exp->exp_obd != NULL);
2339 dev = exp->exp_obd->u.cli.cl_sandev;
2342 if (!nioptr->offset) {
2343 CDEBUG(D_PAGE, "hole at ino %lu; index %ld\n",
2344 page->mapping->host->i_ino,
2346 memset(page_address(page), 0, PAGE_SIZE);
2350 if (!page->buffers) {
2351 create_empty_buffers(page, dev, PAGE_SIZE);
2354 clear_bit(BH_New, &bh->b_state);
2355 set_bit(BH_Mapped, &bh->b_state);
2356 bh->b_blocknr = (unsigned long)nioptr->offset;
2358 clear_bit(BH_Uptodate, &bh->b_state);
2360 ll_rw_block(READ, 1, &bh);
2364 /* if buffer already existed, it must be the
2365 * one we mapped before, check it */
2366 LASSERT(!test_bit(BH_New, &bh->b_state));
2367 LASSERT(test_bit(BH_Mapped, &bh->b_state));
2368 LASSERT(bh->b_blocknr == (unsigned long)nioptr->offset);
2370 /* wait it's io completion */
2371 if (test_bit(BH_Lock, &bh->b_state))
2374 if (!test_bit(BH_Uptodate, &bh->b_state))
2375 ll_rw_block(READ, 1, &bh);
2379 /* must do syncronous write here */
2381 if (!buffer_uptodate(bh)) {
2389 ptlrpc_req_finished(request);
2393 static int sanosc_brw_write(struct obd_export *exp, struct obdo *oa,
2394 struct lov_stripe_md *lsm, obd_count page_count,
2395 struct brw_page *pga)
2397 struct ptlrpc_request *request = NULL;
2398 struct ost_body *body;
2399 struct niobuf_remote *nioptr;
2400 struct obd_ioobj *iooptr;
2401 int rc, size[3] = {sizeof(*body)}, mapped = 0;
2405 size[1] = sizeof(struct obd_ioobj);
2406 size[2] = page_count * sizeof(*nioptr);
2408 request = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_OBD_VERSION,
2409 OST_SAN_WRITE, 3, size, NULL);
2413 body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof (*body));
2414 iooptr = lustre_msg_buf(request->rq_reqmsg, 1, sizeof (*iooptr));
2415 nioptr = lustre_msg_buf(request->rq_reqmsg, 2,
2416 sizeof (*nioptr) * page_count);
2418 memcpy(&body->oa, oa, sizeof(body->oa));
2420 obdo_to_ioobj(oa, iooptr);
2421 iooptr->ioo_bufcnt = page_count;
2424 for (mapped = 0; mapped < page_count; mapped++, nioptr++) {
2425 LASSERT(PageLocked(pga[mapped].pg));
2426 LASSERT(mapped == 0 ||
2427 pga[mapped].disk_offset > pga[mapped - 1].disk_offset);
2429 nioptr->offset = pga[mapped].disk_offset;
2430 nioptr->len = pga[mapped].count;
2431 nioptr->flags = pga[mapped].flag;
2434 size[1] = page_count * sizeof(*nioptr);
2435 request->rq_replen = lustre_msg_size(2, size);
2437 rc = ptlrpc_queue_wait(request);
2441 swab = lustre_msg_swabbed (request->rq_repmsg);
2442 LASSERT_REPSWAB (request, 1);
2443 nioptr = lustre_msg_buf(request->rq_repmsg, 1, size[1]);
2445 CERROR("absent/short niobuf array\n");
2446 GOTO(out_req, rc = -EPROTO);
2450 for (mapped = 0; mapped < page_count; mapped++, nioptr++) {
2451 struct page *page = pga[mapped].pg;
2452 struct buffer_head *bh;
2456 lustre_swab_niobuf_remote (nioptr);
2458 /* got san device associated */
2459 LASSERT(exp->exp_obd != NULL);
2460 dev = exp->exp_obd->u.cli.cl_sandev;
2462 if (!page->buffers) {
2463 create_empty_buffers(page, dev, PAGE_SIZE);
2466 LASSERT(!test_bit(BH_New, &page->buffers->b_state));
2467 LASSERT(test_bit(BH_Mapped, &page->buffers->b_state));
2468 LASSERT(page->buffers->b_blocknr ==
2469 (unsigned long)nioptr->offset);
2475 /* if buffer locked, wait it's io completion */
2476 if (test_bit(BH_Lock, &bh->b_state))
2479 clear_bit(BH_New, &bh->b_state);
2480 set_bit(BH_Mapped, &bh->b_state);
2482 /* override the block nr */
2483 bh->b_blocknr = (unsigned long)nioptr->offset;
2485 /* we are about to write it, so set it
2487 * page lock should garentee no race condition here */
2488 set_bit(BH_Uptodate, &bh->b_state);
2489 set_bit(BH_Dirty, &bh->b_state);
2491 ll_rw_block(WRITE, 1, &bh);
2493 /* must do syncronous write here */
2495 if (!buffer_uptodate(bh) || test_bit(BH_Dirty, &bh->b_state)) {
2503 ptlrpc_req_finished(request);
2507 static int sanosc_brw(int cmd, struct obd_export *exp, struct obdo *oa,
2508 struct lov_stripe_md *lsm, obd_count page_count,
2509 struct brw_page *pga, struct obd_trans_info *oti)
2513 while (page_count) {
2514 obd_count pages_per_brw;
2517 if (page_count > PTLRPC_MAX_BRW_PAGES)
2518 pages_per_brw = PTLRPC_MAX_BRW_PAGES;
2520 pages_per_brw = page_count;
2522 if (cmd & OBD_BRW_WRITE)
2523 rc = sanosc_brw_write(exp, oa, lsm, pages_per_brw,pga);
2525 rc = sanosc_brw_read(exp, oa, lsm, pages_per_brw, pga);
2530 page_count -= pages_per_brw;
2531 pga += pages_per_brw;
2538 static void osc_set_data_with_check(struct lustre_handle *lockh, void *data)
2540 struct ldlm_lock *lock = ldlm_handle2lock(lockh);
2543 CERROR("lockh %p, data %p - client evicted?\n", lockh, data);
2547 lock_res_and_lock(lock);
2549 if (lock->l_ast_data && lock->l_ast_data != data) {
2550 struct inode *new_inode = data;
2551 struct inode *old_inode = lock->l_ast_data;
2552 if (!(old_inode->i_state & I_FREEING))
2553 LDLM_ERROR(lock, "inconsistent l_ast_data found");
2554 LASSERTF(old_inode->i_state & I_FREEING,
2555 "Found existing inode %p/%lu/%u state %lu in lock: "
2556 "setting data to %p/%lu/%u\n", old_inode,
2557 old_inode->i_ino, old_inode->i_generation,
2559 new_inode, new_inode->i_ino, new_inode->i_generation);
2562 lock->l_ast_data = data;
2563 unlock_res_and_lock(lock);
2564 LDLM_LOCK_PUT(lock);
2567 static int osc_change_cbdata(struct obd_export *exp, struct lov_stripe_md *lsm,
2568 ldlm_iterator_t replace, void *data)
2570 struct ldlm_res_id res_id = { .name = {0} };
2571 struct obd_device *obd = class_exp2obd(exp);
2573 res_id.name[0] = lsm->lsm_object_id;
2574 res_id.name[2] = lsm->lsm_object_gr;
2575 ldlm_change_cbdata(obd->obd_namespace, &res_id, replace, data);
2579 static int osc_enqueue(struct obd_export *exp, struct lov_stripe_md *lsm,
2580 __u32 type, ldlm_policy_data_t *policy, __u32 mode,
2581 int *flags, void *bl_cb, void *cp_cb, void *gl_cb,
2582 void *data, __u32 lvb_len, void *lvb_swabber,
2583 struct lustre_handle *lockh)
2585 struct obd_device *obd = exp->exp_obd;
2586 struct ldlm_res_id res_id = { .name = {0} };
2588 struct ldlm_reply *rep;
2589 struct ptlrpc_request *req = NULL;
2593 res_id.name[0] = lsm->lsm_object_id;
2594 res_id.name[2] = lsm->lsm_object_gr;
2596 /* Filesystem lock extents are extended to page boundaries so that
2597 * dealing with the page cache is a little smoother. */
2598 policy->l_extent.start -= policy->l_extent.start & ~PAGE_MASK;
2599 policy->l_extent.end |= ~PAGE_MASK;
2601 if (lsm->lsm_oinfo->loi_kms_valid == 0)
2604 /* Next, search for already existing extent locks that will cover us */
2605 rc = ldlm_lock_match(obd->obd_namespace, 0, &res_id, type, policy, mode,
2608 if (ptlrpcs_check_cred(obd->u.cli.cl_import)) {
2609 /* return immediately if no credential held */
2610 ldlm_lock_decref(lockh, mode);
2614 osc_set_data_with_check(lockh, data);
2615 if (*flags & LDLM_FL_HAS_INTENT) {
2616 /* I would like to be able to ASSERT here that rss <=
2617 * kms, but I can't, for reasons which are explained in
2620 /* We already have a lock, and it's referenced */
2624 /* If we're trying to read, we also search for an existing PW lock. The
2625 * VFS and page cache already protect us locally, so lots of readers/
2626 * writers can share a single PW lock.
2628 * There are problems with conversion deadlocks, so instead of
2629 * converting a read lock to a write lock, we'll just enqueue a new
2632 * At some point we should cancel the read lock instead of making them
2633 * send us a blocking callback, but there are problems with canceling
2634 * locks out from other users right now, too. */
2636 if (mode == LCK_PR) {
2637 rc = ldlm_lock_match(obd->obd_namespace, 0, &res_id, type,
2638 policy, LCK_PW, lockh);
2640 if (ptlrpcs_check_cred(obd->u.cli.cl_import)) {
2641 /* return immediately if no credential held */
2642 ldlm_lock_decref(lockh, LCK_PW);
2646 /* FIXME: This is not incredibly elegant, but it might
2647 * be more elegant than adding another parameter to
2648 * lock_match. I want a second opinion. */
2649 ldlm_lock_addref(lockh, LCK_PR);
2650 ldlm_lock_decref(lockh, LCK_PW);
2651 osc_set_data_with_check(lockh, data);
2655 if (mode == LCK_PW) {
2656 rc = ldlm_lock_match(obd->obd_namespace, 0, &res_id, type,
2657 policy, LCK_PR, lockh);
2659 rc = ldlm_cli_convert(lockh, mode, flags);
2661 /* Update readers/writers accounting */
2662 ldlm_lock_addref(lockh, LCK_PW);
2663 ldlm_lock_decref(lockh, LCK_PR);
2664 osc_set_data_with_check(lockh, data);
2667 /* If the conversion failed, we need to drop refcount
2668 on matched lock before we get new one */
2669 /* XXX Won't it save us some efforts if we cancel PR
2670 lock here? We are going to take PW lock anyway and it
2671 will invalidate PR lock */
2672 ldlm_lock_decref(lockh, LCK_PR);
2673 if (rc != EDEADLOCK) {
2680 if (*flags & LDLM_FL_HAS_INTENT) {
2681 int size[2] = {0, sizeof(struct ldlm_request)};
2683 req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_DLM_VERSION,
2684 LDLM_ENQUEUE, 2, size, NULL);
2688 size[0] = sizeof(*rep);
2689 size[1] = sizeof(lvb);
2690 req->rq_replen = lustre_msg_size(2, size);
2692 rc = ldlm_cli_enqueue(exp, req, obd->obd_namespace, res_id, type,
2693 policy, mode, flags, bl_cb, cp_cb, gl_cb, data,
2694 &lvb, sizeof(lvb), lustre_swab_ost_lvb, lockh);
2696 if (rc == ELDLM_LOCK_ABORTED) {
2697 /* swabbed by ldlm_cli_enqueue() */
2698 LASSERT_REPSWABBED(req, 0);
2699 rep = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*rep));
2700 LASSERT(rep != NULL);
2701 if (rep->lock_policy_res1)
2702 rc = rep->lock_policy_res1;
2704 ptlrpc_req_finished(req);
2707 if ((*flags & LDLM_FL_HAS_INTENT && rc == ELDLM_LOCK_ABORTED) || !rc) {
2708 CDEBUG(D_INODE, "received kms == "LPU64", blocks == "LPU64"\n",
2709 lvb.lvb_size, lvb.lvb_blocks);
2710 lsm->lsm_oinfo->loi_rss = lvb.lvb_size;
2711 lsm->lsm_oinfo->loi_blocks = lvb.lvb_blocks;
2717 static int osc_match(struct obd_export *exp, struct lov_stripe_md *lsm,
2718 __u32 type, ldlm_policy_data_t *policy, __u32 mode,
2719 int *flags, void *data, struct lustre_handle *lockh)
2721 struct ldlm_res_id res_id = { .name = {0} };
2722 struct obd_device *obd = exp->exp_obd;
2726 res_id.name[0] = lsm->lsm_object_id;
2727 res_id.name[2] = lsm->lsm_object_gr;
2729 OBD_FAIL_RETURN(OBD_FAIL_OSC_MATCH, -EIO);
2731 /* Filesystem lock extents are extended to page boundaries so that
2732 * dealing with the page cache is a little smoother */
2733 policy->l_extent.start -= policy->l_extent.start & ~PAGE_MASK;
2734 policy->l_extent.end |= ~PAGE_MASK;
2736 /* Next, search for already existing extent locks that will cover us */
2737 rc = ldlm_lock_match(obd->obd_namespace, *flags, &res_id, type,
2738 policy, mode, lockh);
2740 // if (!(*flags & LDLM_FL_TEST_LOCK))
2741 osc_set_data_with_check(lockh, data);
2744 /* If we're trying to read, we also search for an existing PW lock. The
2745 * VFS and page cache already protect us locally, so lots of readers/
2746 * writers can share a single PW lock. */
2747 if (mode == LCK_PR) {
2748 rc = ldlm_lock_match(obd->obd_namespace, *flags, &res_id, type,
2749 policy, LCK_PW, lockh);
2750 if (rc == 1 && !(*flags & LDLM_FL_TEST_LOCK)) {
2751 /* FIXME: This is not incredibly elegant, but it might
2752 * be more elegant than adding another parameter to
2753 * lock_match. I want a second opinion. */
2754 osc_set_data_with_check(lockh, data);
2755 ldlm_lock_addref(lockh, LCK_PR);
2756 ldlm_lock_decref(lockh, LCK_PW);
2762 static int osc_cancel(struct obd_export *exp, struct lov_stripe_md *md,
2763 __u32 mode, struct lustre_handle *lockh)
2767 if (mode == LCK_GROUP)
2768 ldlm_lock_decref_and_cancel(lockh, mode);
2770 ldlm_lock_decref(lockh, mode);
2775 static int osc_cancel_unused(struct obd_export *exp,
2776 struct lov_stripe_md *lsm,
2777 int flags, void *opaque)
2779 struct obd_device *obd = class_exp2obd(exp);
2780 struct ldlm_res_id res_id = { .name = {0} }, *resp = NULL;
2783 res_id.name[0] = lsm->lsm_object_id;
2784 res_id.name[2] = lsm->lsm_object_gr;
2788 return ldlm_cli_cancel_unused(obd->obd_namespace, resp, flags, opaque);
2791 static int osc_statfs(struct obd_device *obd, struct obd_statfs *osfs,
2792 unsigned long max_age)
2794 struct obd_statfs *msfs;
2795 struct ptlrpc_request *request;
2796 int rc, size = sizeof(*osfs);
2799 /* We could possibly pass max_age in the request (as an absolute
2800 * timestamp or a "seconds.usec ago") so the target can avoid doing
2801 * extra calls into the filesystem if that isn't necessary (e.g.
2802 * during mount that would help a bit). Having relative timestamps
2803 * is not so great if request processing is slow, while absolute
2804 * timestamps are not ideal because they need time synchronization. */
2805 request = ptlrpc_prep_req(obd->u.cli.cl_import, LUSTRE_OBD_VERSION,
2806 OST_STATFS, 0, NULL, NULL);
2810 request->rq_replen = lustre_msg_size(1, &size);
2811 request->rq_request_portal = OST_CREATE_PORTAL; //XXX FIXME bug 249
2813 rc = ptlrpc_queue_wait(request);
2817 msfs = lustre_swab_repbuf(request, 0, sizeof(*msfs),
2818 lustre_swab_obd_statfs);
2820 CERROR("Can't unpack obd_statfs\n");
2821 GOTO(out, rc = -EPROTO);
2824 memcpy(osfs, msfs, sizeof(*osfs));
2828 ptlrpc_req_finished(request);
2832 /* Retrieve object striping information.
2834 * @lmmu is a pointer to an in-core struct with lmm_ost_count indicating
2835 * the maximum number of OST indices which will fit in the user buffer.
2836 * lmm_magic must be LOV_MAGIC (we only use 1 slot here).
2838 static int osc_getstripe(struct lov_stripe_md *lsm, struct lov_user_md *lump)
2840 struct lov_user_md lum, *lumk;
2847 rc = copy_from_user(&lum, lump, sizeof(lum));
2851 if (lum.lmm_magic != LOV_USER_MAGIC)
2854 if (lum.lmm_stripe_count > 0) {
2855 lum_size = sizeof(lum) + sizeof(lum.lmm_objects[0]);
2856 OBD_ALLOC(lumk, lum_size);
2860 lumk->lmm_objects[0].l_object_id = lsm->lsm_object_id;
2861 lumk->lmm_objects[0].l_object_gr = lsm->lsm_object_gr;
2863 lum_size = sizeof(lum);
2867 lumk->lmm_object_id = lsm->lsm_object_id;
2868 lumk->lmm_object_gr = lsm->lsm_object_gr;
2869 lumk->lmm_stripe_count = 1;
2871 if (copy_to_user(lump, lumk, lum_size))
2875 OBD_FREE(lumk, lum_size);
2880 static int osc_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
2881 void *karg, void *uarg)
2883 struct obd_device *obd = exp->exp_obd;
2884 struct obd_ioctl_data *data = karg;
2888 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
2891 if (!try_module_get(THIS_MODULE)) {
2892 CERROR("Can't get module. Is it alive?");
2897 case OBD_IOC_LOV_GET_CONFIG: {
2899 struct lov_desc *desc;
2900 struct obd_uuid uuid;
2904 if (obd_ioctl_getdata(&buf, &len, (void *)uarg))
2905 GOTO(out, err = -EINVAL);
2907 data = (struct obd_ioctl_data *)buf;
2909 if (sizeof(*desc) > data->ioc_inllen1) {
2911 GOTO(out, err = -EINVAL);
2914 if (data->ioc_inllen2 < sizeof(uuid)) {
2916 GOTO(out, err = -EINVAL);
2919 if (data->ioc_inllen3 < sizeof(__u32)) {
2921 GOTO(out, err = -EINVAL);
2924 desc = (struct lov_desc *)data->ioc_inlbuf1;
2925 desc->ld_tgt_count = 1;
2926 desc->ld_active_tgt_count = 1;
2927 desc->ld_default_stripe_count = 1;
2928 desc->ld_default_stripe_size = 0;
2929 desc->ld_default_stripe_offset = 0;
2930 desc->ld_pattern = 0;
2931 memcpy(&desc->ld_uuid, &obd->obd_uuid, sizeof(uuid));
2932 memcpy(data->ioc_inlbuf2, &obd->obd_uuid, sizeof(uuid));
2933 *((__u32 *)data->ioc_inlbuf3) = 1;
2935 err = copy_to_user((void *)uarg, buf, len);
2938 obd_ioctl_freedata(buf, len);
2941 case LL_IOC_LOV_SETSTRIPE:
2942 err = obd_alloc_memmd(exp, karg);
2946 case LL_IOC_LOV_GETSTRIPE:
2947 err = osc_getstripe(karg, uarg);
2949 case OBD_IOC_CLIENT_RECOVER:
2950 err = ptlrpc_recover_import(obd->u.cli.cl_import,
2955 case IOC_OSC_SET_ACTIVE:
2956 err = ptlrpc_set_import_active(obd->u.cli.cl_import,
2959 case IOC_OSC_CTL_RECOVERY:
2960 err = ptlrpc_import_control_recovery(obd->u.cli.cl_import,
2964 CDEBUG(D_INODE, "unrecognised ioctl %#x by %s\n", cmd, current->comm);
2965 GOTO(out, err = -ENOTTY);
2968 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
2971 module_put(THIS_MODULE);
2976 static int osc_get_info(struct obd_export *exp, __u32 keylen,
2977 void *key, __u32 *vallen, void *val)
2980 if (!vallen || !val)
2983 if (keylen > strlen("lock_to_stripe") &&
2984 strcmp(key, "lock_to_stripe") == 0) {
2985 __u32 *stripe = val;
2986 *vallen = sizeof(*stripe);
2989 } else if (keylen >= strlen("last_id") && strcmp(key, "last_id") == 0) {
2990 struct ptlrpc_request *req;
2992 char *bufs[1] = {key};
2994 req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_OBD_VERSION,
2995 OST_GET_INFO, 1, (int *)&keylen, bufs);
2999 req->rq_replen = lustre_msg_size(1, (int *)vallen);
3000 rc = ptlrpc_queue_wait(req);
3004 reply = lustre_swab_repbuf(req, 0, sizeof(*reply),
3005 lustre_swab_ost_last_id);
3006 if (reply == NULL) {
3007 CERROR("Can't unpack OST last ID\n");
3008 GOTO(out, rc = -EPROTO);
3010 *((obd_id *)val) = *reply;
3012 ptlrpc_req_finished(req);
3014 } else if (keylen == 10 && strcmp(key, "client_nid") == 0) {
3015 struct ptlrpc_connection * conn;
3016 ptl_nid_t * nid = val;
3017 ptl_process_id_t id;
3020 *vallen = sizeof(*nid);
3021 conn = class_exp2cliimp(exp)->imp_connection;
3023 if (!conn || !conn->c_peer.peer_ni)
3026 rc = PtlGetId(conn->c_peer.peer_ni->pni_ni_h, &id);
3035 static int osc_set_info(struct obd_export *exp, obd_count keylen,
3036 void *key, obd_count vallen, void *val)
3038 struct obd_device *obd = exp->exp_obd;
3039 struct obd_import *imp = class_exp2cliimp(exp);
3040 struct llog_ctxt *ctxt;
3044 if (keylen == strlen("unlinked") &&
3045 memcmp(key, "unlinked", keylen) == 0) {
3046 struct osc_creator *oscc = &obd->u.cli.cl_oscc;
3047 spin_lock(&oscc->oscc_lock);
3048 oscc->oscc_flags &= ~OSCC_FLAG_NOSPC;
3049 spin_unlock(&oscc->oscc_lock);
3053 if (keylen == strlen("unrecovery") &&
3054 memcmp(key, "unrecovery", keylen) == 0) {
3055 struct osc_creator *oscc = &obd->u.cli.cl_oscc;
3056 spin_lock(&oscc->oscc_lock);
3057 oscc->oscc_flags &= ~OSCC_FLAG_RECOVERING;
3058 spin_unlock(&oscc->oscc_lock);
3062 if (keylen == strlen("initial_recov") &&
3063 memcmp(key, "initial_recov", strlen("initial_recov")) == 0) {
3064 struct obd_import *imp = class_exp2cliimp(exp);
3065 if (vallen != sizeof(int))
3067 imp->imp_initial_recov = *(int *)val;
3068 CDEBUG(D_HA, "%s: set imp_no_init_recov = %d\n",
3069 exp->exp_obd->obd_name,
3070 imp->imp_initial_recov);
3074 if (keylen == strlen("async") &&
3075 memcmp(key, "async", keylen) == 0) {
3076 struct client_obd *cl = &obd->u.cli;
3077 if (vallen != sizeof(int))
3079 cl->cl_async = *(int *)val;
3080 CDEBUG(D_HA, "%s: set async = %d\n",
3081 obd->obd_name, cl->cl_async);
3085 if (keylen == 5 && strcmp(key, "audit") == 0) {
3086 struct ptlrpc_request *req;
3087 char *bufs[2] = {key, val};
3088 int size[2] = {keylen, vallen};
3090 req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_OBD_VERSION,
3091 OST_SET_INFO, 2, size, bufs);
3095 req->rq_replen = lustre_msg_size(0, size);
3096 lustre_swab_reqbuf(req, 1, sizeof(struct audit_attr_msg),
3097 lustre_swab_audit_attr);
3098 rc = ptlrpc_queue_wait(req);
3100 ptlrpc_req_finished(req);
3104 if (keylen == 9 && strcmp(key, "audit_obj") == 0) {
3105 struct ptlrpc_request *req;
3106 char *bufs[2] = {key, val};
3107 int size[2] = {keylen, vallen};
3109 req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_OBD_VERSION,
3110 OST_SET_INFO, 2, size, bufs);
3114 req->rq_replen = lustre_msg_size(0, size);
3115 lustre_swab_reqbuf(req, 1, sizeof(struct obdo),
3117 rc = ptlrpc_queue_wait(req);
3119 ptlrpc_req_finished(req);
3123 if (keylen == 8 && memcmp(key, "auditlog", 8) == 0) {
3124 struct ptlrpc_request *req;
3125 char *bufs[2] = {key, val};
3126 int size[2] = {keylen, vallen};
3128 req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_OBD_VERSION,
3129 OST_SET_INFO, 2, size, bufs);
3133 req->rq_replen = lustre_msg_size(0, size);
3134 lustre_swab_reqbuf(req, 1, sizeof(struct audit_msg),
3135 lustre_swab_audit_msg);
3136 rc = ptlrpc_queue_wait(req);
3138 ptlrpc_req_finished(req);
3142 if (keylen == strlen("sec") && memcmp(key, "sec", keylen) == 0) {
3143 struct client_obd *cli = &exp->exp_obd->u.cli;
3145 cli->cl_sec_flavor = ptlrpcs_name2flavor(val);
3146 if (cli->cl_sec_flavor == PTLRPCS_FLVR_INVALID) {
3147 CERROR("unrecognized security flavor %s\n", (char*) val);
3154 if (keylen == strlen("sec_flags") &&
3155 memcmp(key, "sec_flags", keylen) == 0) {
3156 struct client_obd *cli = &exp->exp_obd->u.cli;
3158 cli->cl_sec_flags = *((unsigned long *) val);
3162 if (keylen == strlen("flush_cred") &&
3163 memcmp(key, "flush_cred", keylen) == 0) {
3164 struct client_obd *cli = &exp->exp_obd->u.cli;
3167 ptlrpcs_import_flush_current_creds(cli->cl_import);
3170 if (keylen == strlen("crypto_cb") &&
3171 memcmp(key, "crypto_cb", keylen) == 0) {
3172 LASSERT(vallen == sizeof(crypt_cb_t));
3173 osc_crypt_cb = (crypt_cb_t)val;
3177 if (keylen == 8 && memcmp(key, "capa_key", 8) == 0) {
3178 struct ptlrpc_request *req;
3179 char *bufs[2] = {key, val};
3180 unsigned long irqflags;
3181 int rc, size[2] = {keylen, vallen};
3183 LASSERT(vallen == sizeof(struct lustre_capa_key));
3185 req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_OBD_VERSION,
3186 OST_SET_INFO, 2, size, bufs);
3190 spin_lock_irqsave (&req->rq_lock, irqflags);
3192 spin_unlock_irqrestore (&req->rq_lock, irqflags);
3194 req->rq_replen = lustre_msg_size(0, NULL);
3195 rc = ptlrpc_queue_wait(req);
3196 ptlrpc_req_finished(req);
3200 if (keylen == strlen("setext") &&
3201 memcmp(key, "setext", keylen) == 0) {
3202 struct client_obd *cli = &exp->exp_obd->u.cli;
3203 struct osc_creator *oscc = &cli->cl_oscc;
3204 struct fid_extent *ext = val;
3206 oscc->oscc_next_id = (obd_id)ext->fe_start;
3210 if (keylen < strlen("mds_conn") ||
3211 memcmp(key, "mds_conn", keylen) != 0)
3214 ctxt = llog_get_context(&exp->exp_obd->obd_llogs,
3215 LLOG_UNLINK_ORIG_CTXT);
3218 rc = llog_initiator_connect(ctxt);
3220 CERROR("cannot establish the connect for "
3221 "ctxt %p: %d\n", ctxt, rc);
3224 imp->imp_server_timeout = 1;
3225 CDEBUG(D_HA, "pinging OST %s\n", imp->imp_target_uuid.uuid);
3226 imp->imp_pingable = 1;
3232 static struct llog_operations osc_size_repl_logops = {
3233 lop_cancel: llog_obd_repl_cancel
3236 static struct llog_operations osc_unlink_orig_logops;
3238 static int osc_llog_init(struct obd_device *obd, struct obd_llogs *llogs,
3239 struct obd_device *tgt, int count,
3240 struct llog_catid *catid)
3245 osc_unlink_orig_logops = llog_lvfs_ops;
3246 osc_unlink_orig_logops.lop_setup = llog_obd_origin_setup;
3247 osc_unlink_orig_logops.lop_cleanup = llog_catalog_cleanup;
3248 osc_unlink_orig_logops.lop_add = llog_catalog_add;
3249 osc_unlink_orig_logops.lop_connect = llog_origin_connect;
3251 rc = obd_llog_setup(obd, llogs, LLOG_UNLINK_ORIG_CTXT, tgt, count,
3252 &catid->lci_logid, &osc_unlink_orig_logops);
3256 rc = obd_llog_setup(obd, llogs, LLOG_SIZE_REPL_CTXT, tgt, count, NULL,
3257 &osc_size_repl_logops);
3261 static int osc_llog_finish(struct obd_device *obd,
3262 struct obd_llogs *llogs, int count)
3267 rc = obd_llog_cleanup(llog_get_context(llogs, LLOG_UNLINK_ORIG_CTXT));
3271 rc = obd_llog_cleanup(llog_get_context(llogs, LLOG_SIZE_REPL_CTXT));
3275 static int osc_connect(struct lustre_handle *exph,
3276 struct obd_device *obd, struct obd_uuid *cluuid,
3277 struct obd_connect_data *data,
3278 unsigned long connect_flags)
3282 rc = client_connect_import(exph, obd, cluuid, data, connect_flags);
3286 static int osc_disconnect(struct obd_export *exp, unsigned long flags)
3288 struct obd_device *obd = class_exp2obd(exp);
3289 struct llog_ctxt *ctxt;
3293 ctxt = llog_get_context(&obd->obd_llogs, LLOG_SIZE_REPL_CTXT);
3294 if (obd->u.cli.cl_conn_count == 1)
3295 /* flush any remaining cancel messages out to the target */
3296 llog_sync(ctxt, exp);
3298 rc = client_disconnect_export(exp, flags);
3302 static int osc_import_event(struct obd_device *obd,
3303 struct obd_import *imp,
3304 enum obd_import_event event)
3306 struct client_obd *cli;
3310 LASSERT(imp->imp_obd == obd);
3313 case IMP_EVENT_DISCON: {
3314 /* Only do this on the MDS OSC's */
3315 if (imp->imp_server_timeout) {
3316 struct osc_creator *oscc = &obd->u.cli.cl_oscc;
3318 spin_lock(&oscc->oscc_lock);
3319 oscc->oscc_flags |= OSCC_FLAG_RECOVERING;
3320 spin_unlock(&oscc->oscc_lock);
3324 case IMP_EVENT_INACTIVE: {
3325 if (obd->obd_observer)
3326 rc = obd_notify(obd->obd_observer, obd, 0, 0);
3329 case IMP_EVENT_INVALIDATE: {
3330 struct ldlm_namespace *ns = obd->obd_namespace;
3334 spin_lock(&cli->cl_loi_list_lock);
3335 cli->cl_avail_grant = 0;
3336 cli->cl_lost_grant = 0;
3337 /* all pages go to failing rpcs due to the invalid import */
3338 osc_check_rpcs(cli);
3339 spin_unlock(&cli->cl_loi_list_lock);
3341 ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
3345 case IMP_EVENT_ACTIVE: {
3346 /* Only do this on the MDS OSC's */
3347 if (imp->imp_server_timeout) {
3348 struct osc_creator *oscc = &obd->u.cli.cl_oscc;
3350 spin_lock(&oscc->oscc_lock);
3351 oscc->oscc_flags &= ~OSCC_FLAG_NOSPC;
3352 spin_unlock(&oscc->oscc_lock);
3355 if (obd->obd_observer)
3356 rc = obd_notify(obd->obd_observer, obd, 1, 0);
3360 CERROR("Unknown import event %d\n", event);
3366 static int osc_attach(struct obd_device *dev, obd_count len, void *data)
3368 struct lprocfs_static_vars lvars;
3372 lprocfs_init_vars(osc,&lvars);
3373 rc = lprocfs_obd_attach(dev, lvars.obd_vars);
3377 rc = lproc_osc_attach_seqstat(dev);
3379 lprocfs_obd_detach(dev);
3383 ptlrpc_lprocfs_register_obd(dev);
3387 static int osc_detach(struct obd_device *dev)
3389 ptlrpc_lprocfs_unregister_obd(dev);
3390 return lprocfs_obd_detach(dev);
3393 static int osc_setup(struct obd_device *obd, obd_count len, void *buf)
3397 rc = ptlrpcd_addref();
3401 rc = client_obd_setup(obd, len, buf);
3410 static int osc_cleanup(struct obd_device *obd, int flags)
3412 struct osc_creator *oscc = &obd->u.cli.cl_oscc;
3415 rc = ldlm_cli_cancel_unused(obd->obd_namespace, NULL,
3416 LDLM_FL_CONFIG_CHANGE, NULL);
3420 spin_lock(&oscc->oscc_lock);
3421 oscc->oscc_flags &= ~OSCC_FLAG_RECOVERING;
3422 oscc->oscc_flags |= OSCC_FLAG_EXITING;
3423 spin_unlock(&oscc->oscc_lock);
3425 rc = client_obd_cleanup(obd, flags);
3431 struct obd_ops osc_obd_ops = {
3432 .o_owner = THIS_MODULE,
3433 .o_attach = osc_attach,
3434 .o_detach = osc_detach,
3435 .o_setup = osc_setup,
3436 .o_cleanup = osc_cleanup,
3437 .o_add_conn = client_import_add_conn,
3438 .o_del_conn = client_import_del_conn,
3439 .o_connect = osc_connect,
3440 .o_disconnect = osc_disconnect,
3441 .o_statfs = osc_statfs,
3442 .o_packmd = osc_packmd,
3443 .o_unpackmd = osc_unpackmd,
3444 .o_create = osc_create,
3445 .o_destroy = osc_destroy,
3446 .o_getattr = osc_getattr,
3447 .o_getattr_async = osc_getattr_async,
3448 .o_setattr = osc_setattr,
3450 .o_brw_async = osc_brw_async,
3451 .o_prep_async_page = osc_prep_async_page,
3452 .o_queue_async_io = osc_queue_async_io,
3453 .o_set_async_flags = osc_set_async_flags,
3454 .o_queue_group_io = osc_queue_group_io,
3455 .o_trigger_group_io = osc_trigger_group_io,
3456 .o_teardown_async_page = osc_teardown_async_page,
3457 .o_punch = osc_punch,
3459 .o_enqueue = osc_enqueue,
3460 .o_match = osc_match,
3461 .o_change_cbdata = osc_change_cbdata,
3462 .o_cancel = osc_cancel,
3463 .o_cancel_unused = osc_cancel_unused,
3464 .o_iocontrol = osc_iocontrol,
3465 .o_get_info = osc_get_info,
3466 .o_set_info = osc_set_info,
3467 .o_import_event = osc_import_event,
3468 .o_llog_init = osc_llog_init,
3469 .o_llog_finish = osc_llog_finish,
3472 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
3473 struct obd_ops sanosc_obd_ops = {
3474 .o_owner = THIS_MODULE,
3475 .o_attach = osc_attach,
3476 .o_detach = osc_detach,
3477 .o_cleanup = client_obd_cleanup,
3478 .o_add_conn = client_import_add_conn,
3479 .o_del_conn = client_import_del_conn,
3480 .o_connect = osc_connect,
3481 .o_disconnect = client_disconnect_export,
3482 .o_statfs = osc_statfs,
3483 .o_packmd = osc_packmd,
3484 .o_unpackmd = osc_unpackmd,
3485 .o_create = osc_real_create,
3486 .o_destroy = osc_destroy,
3487 .o_getattr = osc_getattr,
3488 .o_getattr_async = osc_getattr_async,
3489 .o_setattr = osc_setattr,
3490 .o_setup = client_sanobd_setup,
3491 .o_brw = sanosc_brw,
3492 .o_punch = osc_punch,
3494 .o_enqueue = osc_enqueue,
3495 .o_match = osc_match,
3496 .o_change_cbdata = osc_change_cbdata,
3497 .o_cancel = osc_cancel,
3498 .o_cancel_unused = osc_cancel_unused,
3499 .o_iocontrol = osc_iocontrol,
3500 .o_import_event = osc_import_event,
3501 .o_llog_init = osc_llog_init,
3502 .o_llog_finish = osc_llog_finish,
3506 int __init osc_init(void)
3508 struct lprocfs_static_vars lvars;
3509 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
3510 struct lprocfs_static_vars sanlvars;
3515 lprocfs_init_vars(osc, &lvars);
3516 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
3517 lprocfs_init_vars(osc, &sanlvars);
3520 rc = class_register_type(&osc_obd_ops, NULL, lvars.module_vars,
3521 OBD_OSC_DEVICENAME);
3525 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
3526 rc = class_register_type(&sanosc_obd_ops, NULL, sanlvars.module_vars,
3527 OBD_SANOSC_DEVICENAME);
3529 class_unregister_type(OBD_OSC_DEVICENAME);
3536 static void /*__exit*/ osc_exit(void)
3538 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
3539 class_unregister_type(OBD_SANOSC_DEVICENAME);
3541 class_unregister_type(OBD_OSC_DEVICENAME);
3544 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
3545 MODULE_DESCRIPTION("Lustre Object Storage Client (OSC)");
3546 MODULE_LICENSE("GPL");
3548 module_init(osc_init);
3549 module_exit(osc_exit);