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)
258 struct ptlrpc_request *request;
259 struct ost_body *body;
260 int rc, size = sizeof(*body);
263 LASSERT(!(oa->o_valid & OBD_MD_FLGROUP) || oa->o_gr > 0);
265 request = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_OBD_VERSION,
266 OST_SETATTR, 1, &size, NULL);
270 body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof(*body));
271 memcpy(&body->oa, oa, sizeof(*oa));
273 request->rq_replen = lustre_msg_size(1, &size);
275 if (oti != NULL && (oti->oti_flags & OBD_MODE_ASYNC)) {
276 ptlrpcd_add_req(request);
279 rc = ptlrpc_queue_wait(request);
283 body = lustre_swab_repbuf(request, 0, sizeof(*body),
284 lustre_swab_ost_body);
286 GOTO(out, rc = -EPROTO);
288 memcpy(oa, &body->oa, sizeof(*oa));
292 ptlrpc_req_finished(request);
296 int osc_real_create(struct obd_export *exp, struct obdo *oa,
297 struct lov_stripe_md **ea, struct obd_trans_info *oti)
299 struct osc_creator *oscc = &exp->exp_obd->u.cli.cl_oscc;
300 struct ptlrpc_request *request;
301 struct ost_body *body;
302 struct lov_stripe_md *lsm;
303 int rc, size = sizeof(*body);
311 rc = obd_alloc_memmd(exp, &lsm);
316 request = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_OBD_VERSION,
317 OST_CREATE, 1, &size, NULL);
319 GOTO(out, rc = -ENOMEM);
321 body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof (*body));
322 memcpy(&body->oa, oa, sizeof(body->oa));
324 request->rq_replen = lustre_msg_size(1, &size);
325 if (oa->o_valid & OBD_MD_FLINLINE) {
326 LASSERT((oa->o_valid & OBD_MD_FLFLAGS) &&
327 oa->o_flags == OBD_FL_DELORPHAN);
328 DEBUG_REQ(D_HA, request,
329 "delorphan from OST integration");
330 /* Don't resend the delorphan request */
331 request->rq_no_resend = request->rq_no_delay = 1;
334 rc = ptlrpc_queue_wait(request);
338 body = lustre_swab_repbuf(request, 0, sizeof(*body),
339 lustre_swab_ost_body);
341 CERROR ("can't unpack ost_body\n");
342 GOTO (out_req, rc = -EPROTO);
345 if ((oa->o_valid & OBD_MD_FLFLAGS) && oa->o_flags == OBD_FL_DELORPHAN) {
346 struct obd_import *imp = class_exp2cliimp(exp);
347 /* MDS declares last known object, OSS responses
348 * with next possible object -bzzz */
349 spin_lock(&oscc->oscc_lock);
350 oscc->oscc_next_id = body->oa.o_id;
351 spin_unlock(&oscc->oscc_lock);
352 CDEBUG(D_HA, "%s: set nextid "LPD64" after recovery\n",
353 imp->imp_target_uuid.uuid, oa->o_id);
355 memcpy(oa, &body->oa, sizeof(*oa));
357 /* This should really be sent by the OST */
358 oa->o_blksize = PTLRPC_MAX_BRW_SIZE;
359 oa->o_valid |= OBD_MD_FLBLKSZ;
361 /* XXX LOV STACKING: the lsm that is passed to us from LOV does not
362 * have valid lsm_oinfo data structs, so don't go touching that.
363 * This needs to be fixed in a big way.
365 lsm->lsm_object_id = oa->o_id;
366 lsm->lsm_object_gr = oa->o_gr;
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,
393 struct lustre_capa *capa)
395 struct ptlrpc_request *request;
396 struct ost_body *body;
397 struct lustre_capa *req_capa;
399 int rc, size[2] = { sizeof(*body), sizeof(*capa) };
407 request = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_OBD_VERSION,
408 OST_PUNCH, capa ? 2 : 1, size, NULL);
412 body = lustre_msg_buf(request->rq_reqmsg, bufcnt++, sizeof (*body));
414 memcpy(&body->oa, oa, sizeof(*oa));
416 /* overload the size and blocks fields in the oa with start/end */
417 body->oa.o_size = start;
418 body->oa.o_blocks = end;
419 body->oa.o_valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS);
422 req_capa = lustre_msg_buf(request->rq_reqmsg, bufcnt++,
424 capa_dup2(req_capa, capa);
425 body->oa.o_valid |= OBD_MD_CAPA;
428 request->rq_replen = lustre_msg_size(1, size);
430 rc = ptlrpc_queue_wait(request);
434 body = lustre_swab_repbuf (request, 0, sizeof (*body),
435 lustre_swab_ost_body);
437 CERROR ("can't unpack ost_body\n");
438 GOTO (out, rc = -EPROTO);
441 memcpy(oa, &body->oa, sizeof(*oa));
445 ptlrpc_req_finished(request);
449 static int osc_sync(struct obd_export *exp, struct obdo *oa,
450 struct lov_stripe_md *md, obd_size start,
453 struct ptlrpc_request *request;
454 struct ost_body *body;
455 int rc, size = sizeof(*body);
463 request = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_OBD_VERSION,
464 OST_SYNC, 1, &size, NULL);
468 body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof (*body));
469 memcpy(&body->oa, oa, sizeof(*oa));
471 /* overload the size and blocks fields in the oa with start/end */
472 body->oa.o_size = start;
473 body->oa.o_blocks = end;
474 body->oa.o_valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS);
476 request->rq_replen = lustre_msg_size(1, &size);
478 rc = ptlrpc_queue_wait(request);
482 body = lustre_swab_repbuf(request, 0, sizeof(*body),
483 lustre_swab_ost_body);
485 CERROR ("can't unpack ost_body\n");
486 GOTO (out, rc = -EPROTO);
489 memcpy(oa, &body->oa, sizeof(*oa));
493 ptlrpc_req_finished(request);
497 static int osc_destroy(struct obd_export *exp, struct obdo *oa,
498 struct lov_stripe_md *ea, struct obd_trans_info *oti)
500 struct ptlrpc_request *request;
501 struct ost_body *body;
502 int rc, size = sizeof(*body);
510 request = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_OBD_VERSION,
511 OST_DESTROY, 1, &size, NULL);
514 request->rq_request_portal = OST_DESTROY_PORTAL;
516 body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof (*body));
518 if (oti != NULL && oa->o_valid & OBD_MD_FLCOOKIE) {
519 memcpy(obdo_logcookie(oa), oti->oti_logcookies,
520 sizeof(*oti->oti_logcookies));
521 oti->oti_logcookies++;
524 memcpy(&body->oa, oa, sizeof(*oa));
525 request->rq_replen = lustre_msg_size(1, &size);
527 if (oti != NULL && (oti->oti_flags & OBD_MODE_ASYNC)) {
528 ptlrpcd_add_req(request);
531 rc = ptlrpc_queue_wait(request);
537 ptlrpc_req_finished(request);
541 body = lustre_swab_repbuf(request, 0, sizeof(*body),
542 lustre_swab_ost_body);
544 CERROR ("Can't unpack body\n");
545 ptlrpc_req_finished(request);
549 memcpy(oa, &body->oa, sizeof(*oa));
550 ptlrpc_req_finished(request);
555 static void osc_announce_cached(struct client_obd *cli, struct obdo *oa,
558 obd_valid bits = OBD_MD_FLBLOCKS|OBD_MD_FLGRANT;
560 LASSERT(!(oa->o_valid & bits));
563 spin_lock(&cli->cl_loi_list_lock);
564 oa->o_dirty = cli->cl_dirty;
565 oa->o_undirty = cli->cl_dirty_max - oa->o_dirty;
566 oa->o_grant = cli->cl_avail_grant;
567 oa->o_dropped = cli->cl_lost_grant;
568 cli->cl_lost_grant = 0;
569 spin_unlock(&cli->cl_loi_list_lock);
570 CDEBUG(D_CACHE,"dirty: "LPU64" undirty: %u dropped %u grant: "LPU64"\n",
571 oa->o_dirty, oa->o_undirty, oa->o_dropped, oa->o_grant);
574 /* caller must hold loi_list_lock */
575 static void osc_consume_write_grant(struct client_obd *cli,
576 struct osc_async_page *oap)
578 cli->cl_dirty += PAGE_SIZE;
579 cli->cl_avail_grant -= PAGE_SIZE;
580 oap->oap_brw_flags |= OBD_BRW_FROM_GRANT;
581 CDEBUG(D_CACHE, "using %lu grant credits for oap %p\n", PAGE_SIZE, oap);
582 LASSERT(cli->cl_avail_grant >= 0);
585 static unsigned long rpcs_in_flight(struct client_obd *cli)
587 return cli->cl_r_in_flight + cli->cl_w_in_flight;
590 /* caller must hold loi_list_lock */
591 void osc_wake_cache_waiters(struct client_obd *cli)
593 struct list_head *l, *tmp;
594 struct osc_cache_waiter *ocw;
596 list_for_each_safe(l, tmp, &cli->cl_cache_waiters) {
597 /* if we can't dirty more, we must wait until some is written */
598 if (cli->cl_dirty + PAGE_SIZE > cli->cl_dirty_max) {
599 CDEBUG(D_CACHE, "no dirty room: dirty: %ld max %ld\n",
600 cli->cl_dirty, cli->cl_dirty_max);
604 /* if still dirty cache but no grant wait for pending RPCs that
605 * may yet return us some grant before doing sync writes */
606 if (cli->cl_w_in_flight && cli->cl_avail_grant < PAGE_SIZE) {
607 CDEBUG(D_CACHE, "%u BRW writes in flight, no grant\n",
608 cli->cl_w_in_flight);
610 ocw = list_entry(l, struct osc_cache_waiter, ocw_entry);
611 list_del_init(&ocw->ocw_entry);
612 if (cli->cl_avail_grant < PAGE_SIZE) {
613 /* no more RPCs in flight to return grant, do sync IO */
614 ocw->ocw_rc = -EDQUOT;
615 CDEBUG(D_INODE, "wake oap %p for sync\n", ocw->ocw_oap);
617 osc_consume_write_grant(cli, ocw->ocw_oap);
620 wake_up(&ocw->ocw_waitq);
626 static void osc_update_grant(struct client_obd *cli, struct ost_body *body)
628 spin_lock(&cli->cl_loi_list_lock);
629 CDEBUG(D_CACHE, "got "LPU64" extra grant\n", body->oa.o_grant);
630 cli->cl_avail_grant += body->oa.o_grant;
631 /* waiters are woken in brw_interpret_oap */
632 spin_unlock(&cli->cl_loi_list_lock);
635 /* We assume that the reason this OSC got a short read is because it read
636 * beyond the end of a stripe file; i.e. lustre is reading a sparse file
637 * via the LOV, and it _knows_ it's reading inside the file, it's just that
638 * this stripe never got written at or beyond this stripe offset yet. */
639 static void handle_short_read(int nob_read, obd_count page_count,
640 struct brw_page *pga)
644 /* skip bytes read OK */
645 while (nob_read > 0) {
646 LASSERT (page_count > 0);
648 if (pga->count > nob_read) {
649 /* EOF inside this page */
650 ptr = kmap(pga->pg) + (pga->page_offset & ~PAGE_MASK);
651 memset(ptr + nob_read, 0, pga->count - nob_read);
658 nob_read -= pga->count;
663 /* zero remaining pages */
664 while (page_count-- > 0) {
665 ptr = kmap(pga->pg) + (pga->page_offset & ~PAGE_MASK);
666 memset(ptr, 0, pga->count);
672 static int check_write_rcs(struct ptlrpc_request *request,
673 int requested_nob, int niocount,
674 obd_count page_count, struct brw_page *pga)
678 /* return error if any niobuf was in error */
679 remote_rcs = lustre_swab_repbuf(request, 1,
680 sizeof(*remote_rcs) * niocount, NULL);
681 if (remote_rcs == NULL) {
682 CERROR("Missing/short RC vector on BRW_WRITE reply\n");
685 if (lustre_msg_swabbed(request->rq_repmsg))
686 for (i = 0; i < niocount; i++)
687 __swab32s((__u32 *)&remote_rcs[i]);
689 for (i = 0; i < niocount; i++) {
690 if (remote_rcs[i] < 0)
691 return(remote_rcs[i]);
693 if (remote_rcs[i] != 0) {
694 CERROR("rc[%d] invalid (%d) req %p\n",
695 i, remote_rcs[i], request);
700 if (request->rq_bulk->bd_nob_transferred != requested_nob) {
701 CERROR("Unexpected # bytes transferred: %d (requested %d)\n",
702 requested_nob, request->rq_bulk->bd_nob_transferred);
709 static inline int can_merge_pages(struct brw_page *p1, struct brw_page *p2)
711 if (p1->flag != p2->flag) {
712 unsigned mask = ~OBD_BRW_FROM_GRANT;
714 /* warn if we try to combine flags that we don't know to be
716 if ((p1->flag & mask) != (p2->flag & mask))
717 CERROR("is it ok to have flags 0x%x and 0x%x in the "
718 "same brw?\n", p1->flag, p2->flag);
722 return (p1->disk_offset + p1->count == p2->disk_offset);
726 static obd_count cksum_pages(int nob, obd_count page_count,
727 struct brw_page *pga)
733 LASSERT (page_count > 0);
736 ost_checksum(&cksum, ptr + (pga->off & (PAGE_SIZE - 1)),
737 pga->count > nob ? nob : pga->count);
749 #define osc_encrypt_page(page, off, count) \
750 osc_crypt_page(page, off, count, ENCRYPT_DATA)
751 #define osc_decrypt_page(page, off, count) \
752 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_brw_prep_request(int cmd, struct obd_import *imp,struct obdo *oa,
772 struct lov_stripe_md *lsm, obd_count page_count,
773 struct brw_page *pga, int *requested_nobp,
774 int *niocountp, struct ptlrpc_request **reqp)
776 struct ptlrpc_request *req;
777 struct ptlrpc_bulk_desc *desc;
778 struct client_obd *cli = &imp->imp_obd->u.cli;
779 struct ost_body *body;
780 struct lustre_id *raw_id = obdo_id(oa);
781 struct obd_capa *ocapa = NULL;
782 struct lustre_capa *capa = NULL;
783 struct obd_ioobj *ioobj;
784 struct niobuf_remote *niobuf;
793 opc = ((cmd & OBD_BRW_WRITE) != 0) ? OST_WRITE : OST_READ;
795 for (niocount = i = 1; i < page_count; i++)
796 if (!can_merge_pages(&pga[i - 1], &pga[i]))
799 capa_op = (opc == OST_WRITE) ? MAY_WRITE : MAY_READ;
801 ocapa = capa_get(oa->o_fsuid, capa_op, raw_id->li_fid.lf_group,
802 raw_id->li_stc.u.e3s.l3s_ino, CLIENT_CAPA,
805 if (opc == OST_READ && capa_op == MAY_READ) {
806 /* partial write might cause read, MAY_WRITE capability
807 * should be used here */
813 size[bufcnt++] = sizeof(*body);
814 size[bufcnt++] = sizeof(*ioobj);
816 size[bufcnt++] = sizeof(*capa);
817 size[bufcnt++] = niocount * sizeof(*niobuf);
819 req = ptlrpc_prep_req(imp, LUSTRE_OBD_VERSION, opc, bufcnt, size, NULL);
823 if (opc == OST_WRITE)
824 desc = ptlrpc_prep_bulk_imp (req, page_count,
825 BULK_GET_SOURCE, OST_BULK_PORTAL);
827 desc = ptlrpc_prep_bulk_imp (req, page_count,
828 BULK_PUT_SINK, OST_BULK_PORTAL);
830 GOTO(out, rc = -ENOMEM);
831 /* NB request now owns desc and will free it when it gets freed */
834 body = lustre_msg_buf(req->rq_reqmsg, bufcnt++, sizeof(*body));
835 ioobj = lustre_msg_buf(req->rq_reqmsg, bufcnt++, sizeof(*ioobj));
837 capa = lustre_msg_buf(req->rq_reqmsg, bufcnt++, sizeof(*capa));
838 niobuf = lustre_msg_buf(req->rq_reqmsg, bufcnt++,
839 niocount * sizeof(*niobuf));
841 memcpy(&body->oa, oa, sizeof(*oa));
843 obdo_to_ioobj(oa, ioobj);
844 ioobj->ioo_bufcnt = niocount;
847 capa_dup(capa, ocapa);
848 body->oa.o_valid |= OBD_MD_CAPA;
849 capa_put(ocapa, CLIENT_CAPA);
852 LASSERT (page_count > 0);
854 for (requested_nob = i = 0; i < page_count; i++, niobuf++) {
855 struct brw_page *pg = &pga[i];
856 struct brw_page *pg_prev = pg - 1;
858 LASSERT(pg->count > 0);
859 LASSERTF((pg->page_offset & ~PAGE_MASK)+ pg->count <= PAGE_SIZE,
860 "i: %d pg: %p pg_off: "LPU64", count: %u\n", i, pg,
861 pg->page_offset, pg->count);
862 LASSERTF(i == 0 || pg->disk_offset > pg_prev->disk_offset,
863 "i %d p_c %u pg %p [pri %lu ind %lu] off "LPU64
864 " prev_pg %p [pri %lu ind %lu] off "LPU64"\n",
866 pg->pg, pg->pg->private, pg->pg->index, pg->disk_offset,
867 pg_prev->pg, pg_prev->pg->private, pg_prev->pg->index,
868 pg_prev->disk_offset);
870 if (opc == OST_WRITE) {
871 rc = osc_encrypt_page(pg->pg, pg->page_offset, pg->count);
876 ptlrpc_prep_bulk_page(desc, pg->pg,
877 pg->page_offset & ~PAGE_MASK, pg->count);
878 requested_nob += pg->count;
880 if (i > 0 && can_merge_pages(pg_prev, pg)) {
882 niobuf->len += pg->count;
884 niobuf->offset = pg->disk_offset;
885 niobuf->len = pg->count;
886 niobuf->flags = pg->flag;
890 LASSERT((void *)(niobuf - niocount) ==
891 lustre_msg_buf(req->rq_reqmsg, bufcnt - 1,
892 niocount * sizeof(*niobuf)));
893 osc_announce_cached(cli, &body->oa, opc == OST_WRITE ? requested_nob:0);
895 /* size[0] still sizeof (*body) */
896 if (opc == OST_WRITE) {
898 body->oa.o_valid |= OBD_MD_FLCKSUM;
899 body->oa.o_cksum = cksum_pages(requested_nob, page_count, pga);
901 /* 1 RC per niobuf */
902 size[1] = sizeof(__u32) * niocount;
903 req->rq_replen = lustre_msg_size(2, size);
905 /* 1 RC for the whole I/O */
906 req->rq_replen = lustre_msg_size(1, size);
909 *niocountp = niocount;
910 *requested_nobp = requested_nob;
915 ptlrpc_req_finished (req);
919 static int osc_brw_fini_request(struct ptlrpc_request *req, struct obdo *oa,
920 int requested_nob, int niocount,
921 obd_count page_count, struct brw_page *pga,
924 struct client_obd *cli = &req->rq_import->imp_obd->u.cli;
925 struct ost_body *body;
931 body = lustre_swab_repbuf(req, 0, sizeof(*body), lustre_swab_ost_body);
933 CERROR ("Can't unpack body\n");
937 osc_update_grant(cli, body);
938 memcpy(oa, &body->oa, sizeof(*oa));
940 if (req->rq_reqmsg->opc == OST_WRITE) {
942 CERROR ("Unexpected +ve rc %d\n", rc);
945 LASSERT (req->rq_bulk->bd_nob == requested_nob);
946 osc_decrypt_page(pga->pg, pga->page_offset,
948 RETURN(check_write_rcs(req, requested_nob, niocount,
952 if (rc > requested_nob) {
953 CERROR("Unexpected rc %d (%d requested)\n", rc, requested_nob);
957 if (rc != req->rq_bulk->bd_nob_transferred) {
958 CERROR ("Unexpected rc %d (%d transferred)\n",
959 rc, req->rq_bulk->bd_nob_transferred);
963 if (rc < requested_nob)
964 handle_short_read(rc, page_count, pga);
967 if (oa->o_valid & OBD_MD_FLCKSUM) {
968 const struct ptlrpc_peer *peer =
969 &req->rq_import->imp_connection->c_peer;
970 static int cksum_counter;
971 obd_count server_cksum = oa->o_cksum;
972 obd_count cksum = cksum_pages(rc, page_count, pga);
973 char str[PTL_NALFMT_SIZE];
975 ptlrpc_peernid2str(peer, str);
978 if (server_cksum != cksum) {
979 CERROR("Bad checksum: server %x, client %x, server NID "
980 LPX64" (%s)\n", server_cksum, cksum,
981 peer->peer_id.nid, str);
984 } else if ((cksum_counter & (-cksum_counter)) == cksum_counter){
985 CWARN("Checksum %u from "LPX64" (%s) OK: %x\n",
986 cksum_counter, peer->peer_id.nid, str, cksum);
989 static int cksum_missed;
992 if ((cksum_missed & (-cksum_missed)) == cksum_missed)
993 CERROR("Request checksum %u from "LPX64", no reply\n",
995 req->rq_import->imp_connection->c_peer.peer_id.nid);
998 osc_decrypt_page(pga->pg, pga->page_offset, pga->count);
1002 static int osc_brw_internal(int cmd, struct obd_export *exp,struct obdo *oa,
1003 struct lov_stripe_md *lsm,
1004 obd_count page_count, struct brw_page *pga)
1008 struct ptlrpc_request *request;
1013 rc = osc_brw_prep_request(cmd, class_exp2cliimp(exp), oa, lsm,
1014 page_count, pga, &requested_nob, &niocount,
1019 rc = ptlrpc_queue_wait(request);
1021 if (rc == -ETIMEDOUT && request->rq_resend) {
1022 DEBUG_REQ(D_HA, request, "BULK TIMEOUT");
1023 ptlrpc_req_finished(request);
1027 rc = osc_brw_fini_request(request, oa, requested_nob, niocount,
1028 page_count, pga, rc);
1030 ptlrpc_req_finished(request);
1034 static int brw_interpret(struct ptlrpc_request *request,
1035 struct osc_brw_async_args *aa, int rc)
1037 struct obdo *oa = aa->aa_oa;
1038 int requested_nob = aa->aa_requested_nob;
1039 int niocount = aa->aa_nio_count;
1040 obd_count page_count = aa->aa_page_count;
1041 struct brw_page *pga = aa->aa_pga;
1044 rc = osc_brw_fini_request(request, oa, requested_nob, niocount,
1045 page_count, pga, rc);
1049 static int async_internal(int cmd, struct obd_export *exp, struct obdo *oa,
1050 struct lov_stripe_md *lsm, obd_count page_count,
1051 struct brw_page *pga, struct ptlrpc_request_set *set)
1053 struct ptlrpc_request *request;
1056 struct osc_brw_async_args *aa;
1060 rc = osc_brw_prep_request(cmd, class_exp2cliimp(exp), oa, lsm,
1061 page_count, pga, &requested_nob, &nio_count,
1064 LASSERT(sizeof(*aa) <= sizeof(request->rq_async_args));
1065 aa = (struct osc_brw_async_args *)&request->rq_async_args;
1067 aa->aa_requested_nob = requested_nob;
1068 aa->aa_nio_count = nio_count;
1069 aa->aa_page_count = page_count;
1072 request->rq_interpret_reply = brw_interpret;
1073 ptlrpc_set_add_req(set, request);
1079 #define min_t(type,x,y) \
1080 ({ type __x = (x); type __y = (y); __x < __y ? __x: __y; })
1084 * ugh, we want disk allocation on the target to happen in offset order. we'll
1085 * follow sedgewicks advice and stick to the dead simple shellsort -- it'll do
1086 * fine for our small page arrays and doesn't require allocation. its an
1087 * insertion sort that swaps elements that are strides apart, shrinking the
1088 * stride down until its '1' and the array is sorted.
1090 static void sort_brw_pages(struct brw_page *array, int num)
1093 struct brw_page tmp;
1097 for (stride = 1; stride < num ; stride = (stride * 3) + 1)
1102 for (i = stride ; i < num ; i++) {
1105 while (j >= stride && array[j - stride].disk_offset >
1107 array[j] = array[j - stride];
1112 } while (stride > 1);
1115 /* make sure we the regions we're passing to elan don't violate its '4
1116 * fragments' constraint. portal headers are a fragment, all full
1117 * PAGE_SIZE long pages count as 1 fragment, and each partial page
1118 * counts as a fragment. I think. see bug 934. */
1119 static obd_count check_elan_limit(struct brw_page *pg, obd_count pages)
1122 int saw_whole_frag = 0;
1125 for (i = 0 ; frags_left && i < pages ; pg++, i++) {
1126 if (pg->count == PAGE_SIZE) {
1127 if (!saw_whole_frag) {
1138 static int osc_brw(int cmd, struct obd_export *exp, struct obdo *oa,
1139 struct lov_stripe_md *lsm, obd_count page_count,
1140 struct brw_page *pga, struct obd_trans_info *oti)
1144 if (cmd == OBD_BRW_CHECK) {
1145 /* The caller just wants to know if there's a chance that this
1146 * I/O can succeed */
1147 struct obd_import *imp = class_exp2cliimp(exp);
1149 if (imp == NULL || imp->imp_invalid)
1154 while (page_count) {
1155 obd_count pages_per_brw;
1158 if (page_count > PTLRPC_MAX_BRW_PAGES)
1159 pages_per_brw = PTLRPC_MAX_BRW_PAGES;
1161 pages_per_brw = page_count;
1163 sort_brw_pages(pga, pages_per_brw);
1164 pages_per_brw = check_elan_limit(pga, pages_per_brw);
1166 rc = osc_brw_internal(cmd, exp, oa, lsm, pages_per_brw, pga);
1171 page_count -= pages_per_brw;
1172 pga += pages_per_brw;
1177 static int osc_brw_async(int cmd, struct obd_export *exp, struct obdo *oa,
1178 struct lov_stripe_md *lsm, obd_count page_count,
1179 struct brw_page *pga, struct ptlrpc_request_set *set,
1180 struct obd_trans_info *oti)
1184 if (cmd == OBD_BRW_CHECK) {
1185 /* The caller just wants to know if there's a chance that this
1186 * I/O can succeed */
1187 struct obd_import *imp = class_exp2cliimp(exp);
1189 if (imp == NULL || imp->imp_invalid)
1194 while (page_count) {
1195 obd_count pages_per_brw;
1198 if (page_count > PTLRPC_MAX_BRW_PAGES)
1199 pages_per_brw = PTLRPC_MAX_BRW_PAGES;
1201 pages_per_brw = page_count;
1203 sort_brw_pages(pga, pages_per_brw);
1204 pages_per_brw = check_elan_limit(pga, pages_per_brw);
1206 rc = async_internal(cmd, exp, oa, lsm, pages_per_brw, pga, set);
1211 page_count -= pages_per_brw;
1212 pga += pages_per_brw;
1217 static void osc_check_rpcs(struct client_obd *cli);
1218 static void osc_exit_cache(struct client_obd *cli, struct osc_async_page *oap,
1220 static void loi_list_maint(struct client_obd *cli, struct lov_oinfo *loi);
1221 static void lop_update_pending(struct client_obd *cli,
1222 struct loi_oap_pages *lop, int cmd, int delta);
1224 /* this is called when a sync waiter receives an interruption. Its job is to
1225 * get the caller woken as soon as possible. If its page hasn't been put in an
1226 * rpc yet it can dequeue immediately. Otherwise it has to mark the rpc as
1227 * desiring interruption which will forcefully complete the rpc once the rpc
1229 static void osc_occ_interrupted(struct oig_callback_context *occ)
1231 struct osc_async_page *oap;
1232 struct loi_oap_pages *lop;
1233 struct lov_oinfo *loi;
1236 /* XXX member_of() */
1237 oap = list_entry(occ, struct osc_async_page, oap_occ);
1239 spin_lock(&oap->oap_cli->cl_loi_list_lock);
1241 oap->oap_interrupted = 1;
1243 /* ok, it's been put in an rpc. */
1244 if (oap->oap_request != NULL) {
1245 ptlrpc_mark_interrupted(oap->oap_request);
1246 ptlrpcd_wake(oap->oap_request);
1250 /* we don't get interruption callbacks until osc_trigger_sync_io()
1251 * has been called and put the sync oaps in the pending/urgent lists.*/
1252 if (!list_empty(&oap->oap_pending_item)) {
1253 list_del_init(&oap->oap_pending_item);
1254 if (oap->oap_async_flags & ASYNC_URGENT)
1255 list_del_init(&oap->oap_urgent_item);
1258 lop = (oap->oap_cmd == OBD_BRW_WRITE) ?
1259 &loi->loi_write_lop : &loi->loi_read_lop;
1260 lop_update_pending(oap->oap_cli, lop, oap->oap_cmd, -1);
1261 loi_list_maint(oap->oap_cli, oap->oap_loi);
1263 oig_complete_one(oap->oap_oig, &oap->oap_occ, 0);
1264 oap->oap_oig = NULL;
1268 spin_unlock(&oap->oap_cli->cl_loi_list_lock);
1271 /* this must be called holding the loi list lock to give coverage to exit_cache,
1272 * async_flag maintenance, and oap_request */
1273 static void osc_ap_completion(struct client_obd *cli, struct obdo *oa,
1274 struct osc_async_page *oap, int sent, int rc)
1276 osc_exit_cache(cli, oap, sent);
1277 oap->oap_async_flags = 0;
1278 oap->oap_interrupted = 0;
1280 if (oap->oap_request != NULL) {
1281 ptlrpc_req_finished(oap->oap_request);
1282 oap->oap_request = NULL;
1285 if (rc == 0 && oa != NULL)
1286 oap->oap_loi->loi_blocks = oa->o_blocks;
1289 oig_complete_one(oap->oap_oig, &oap->oap_occ, rc);
1290 oap->oap_oig = NULL;
1295 oap->oap_caller_ops->ap_completion(oap->oap_caller_data, oap->oap_cmd,
1299 static int brw_interpret_oap(struct ptlrpc_request *request,
1300 struct osc_brw_async_args *aa, int rc)
1302 struct osc_async_page *oap;
1303 struct client_obd *cli;
1304 struct list_head *pos, *n;
1308 do_gettimeofday(&now);
1309 rc = osc_brw_fini_request(request, aa->aa_oa, aa->aa_requested_nob,
1310 aa->aa_nio_count, aa->aa_page_count,
1313 CDEBUG(D_INODE, "request %p aa %p rc %d\n", request, aa, rc);
1316 /* in failout recovery we ignore writeback failure and want
1317 * to just tell llite to unlock the page and continue */
1318 if (request->rq_reqmsg->opc == OST_WRITE &&
1319 (cli->cl_import == NULL || cli->cl_import->imp_invalid)) {
1320 CDEBUG(D_INODE, "flipping to rc 0 imp %p inv %d\n",
1322 cli->cl_import ? cli->cl_import->imp_invalid : -1);
1326 spin_lock(&cli->cl_loi_list_lock);
1328 if (request->rq_reqmsg->opc == OST_WRITE)
1329 lprocfs_stime_record(&cli->cl_write_stime, &now,
1330 &request->rq_rpcd_start);
1332 lprocfs_stime_record(&cli->cl_read_stime, &now,
1333 &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 osc_wake_cache_waiters(cli);
1358 osc_check_rpcs(cli);
1359 spin_unlock(&cli->cl_loi_list_lock);
1361 obdo_free(aa->aa_oa);
1362 OBD_FREE(aa->aa_pga, aa->aa_page_count * sizeof(struct brw_page));
1367 static struct ptlrpc_request *osc_build_req(struct client_obd *cli,
1368 struct list_head *rpc_list,
1369 int page_count, int cmd)
1371 struct ptlrpc_request *req;
1372 struct brw_page *pga = NULL;
1373 int requested_nob, nio_count;
1374 struct osc_brw_async_args *aa;
1375 struct obdo *oa = NULL;
1376 struct obd_async_page_ops *ops = NULL;
1377 void *caller_data = NULL;
1378 struct list_head *pos;
1381 LASSERT(!list_empty(rpc_list));
1383 OBD_ALLOC(pga, sizeof(*pga) * page_count);
1385 RETURN(ERR_PTR(-ENOMEM));
1389 GOTO(out, req = ERR_PTR(-ENOMEM));
1392 list_for_each(pos, rpc_list) {
1393 struct osc_async_page *oap;
1395 oap = list_entry(pos, struct osc_async_page, oap_rpc_item);
1397 ops = oap->oap_caller_ops;
1398 caller_data = oap->oap_caller_data;
1400 pga[i].disk_offset = oap->oap_obj_off + oap->oap_page_off;
1401 pga[i].page_offset = pga[i].disk_offset;
1402 pga[i].pg = oap->oap_page;
1403 pga[i].count = oap->oap_count;
1404 pga[i].flag = oap->oap_brw_flags;
1405 CDEBUG(0, "put page %p index %lu oap %p flg %x to pga\n",
1406 pga[i].pg, oap->oap_page->index, oap, pga[i].flag);
1410 /* always get the data for the obdo for the rpc */
1411 LASSERT(ops != NULL);
1412 ops->ap_fill_obdo(caller_data, cmd, oa);
1414 sort_brw_pages(pga, page_count);
1415 rc = osc_brw_prep_request(cmd, cli->cl_import, oa, NULL, page_count,
1416 pga, &requested_nob, &nio_count, &req);
1418 CERROR("prep_req failed: %d\n", rc);
1419 GOTO(out, req = ERR_PTR(rc));
1422 LASSERT(sizeof(*aa) <= sizeof(req->rq_async_args));
1423 aa = (struct osc_brw_async_args *)&req->rq_async_args;
1425 aa->aa_requested_nob = requested_nob;
1426 aa->aa_nio_count = nio_count;
1427 aa->aa_page_count = page_count;
1436 OBD_FREE(pga, sizeof(*pga) * page_count);
1441 static void lop_update_pending(struct client_obd *cli,
1442 struct loi_oap_pages *lop, int cmd, int delta)
1444 lop->lop_num_pending += delta;
1445 if (cmd == OBD_BRW_WRITE)
1446 cli->cl_pending_w_pages += delta;
1448 cli->cl_pending_r_pages += delta;
1451 /* the loi lock is held across this function but it's allowed to release
1452 * and reacquire it during its work */
1453 static int osc_send_oap_rpc(struct client_obd *cli, struct lov_oinfo *loi,
1454 int cmd, struct loi_oap_pages *lop)
1456 struct ptlrpc_request *request;
1457 obd_count page_count = 0;
1458 struct list_head *tmp, *pos;
1459 struct osc_async_page *oap = NULL;
1460 struct osc_brw_async_args *aa;
1461 struct obd_async_page_ops *ops;
1462 LIST_HEAD(rpc_list);
1465 /* first we find the pages we're allowed to work with */
1466 list_for_each_safe(pos, tmp, &lop->lop_pending) {
1467 oap = list_entry(pos, struct osc_async_page, oap_pending_item);
1468 ops = oap->oap_caller_ops;
1470 LASSERT(oap->oap_magic == OAP_MAGIC);
1472 /* in llite being 'ready' equates to the page being locked
1473 * until completion unlocks it. commit_write submits a page
1474 * as not ready because its unlock will happen unconditionally
1475 * as the call returns. if we race with commit_write giving
1476 * us that page we dont' want to create a hole in the page
1477 * stream, so we stop and leave the rpc to be fired by
1478 * another dirtier or kupdated interval (the not ready page
1479 * will still be on the dirty list). we could call in
1480 * at the end of ll_file_write to process the queue again. */
1481 if (!(oap->oap_async_flags & ASYNC_READY)) {
1482 int rc = ops->ap_make_ready(oap->oap_caller_data, cmd);
1484 CDEBUG(D_INODE, "oap %p page %p returned %d "
1485 "instead of ready\n", oap,
1489 /* llite is telling us that the page is still
1490 * in commit_write and that we should try
1491 * and put it in an rpc again later. we
1492 * break out of the loop so we don't create
1493 * a hole in the sequence of pages in the rpc
1498 /* the io isn't needed.. tell the checks
1499 * below to complete the rpc with EINTR */
1500 oap->oap_async_flags |= ASYNC_COUNT_STABLE;
1501 oap->oap_count = -EINTR;
1504 oap->oap_async_flags |= ASYNC_READY;
1507 LASSERTF(0, "oap %p page %p returned %d "
1508 "from make_ready\n", oap,
1516 * Page submitted for IO has to be locked. Either by
1517 * ->ap_make_ready() or by higher layers.
1519 * XXX nikita: this assertion should be adjusted when lustre
1520 * starts using PG_writeback for pages being written out.
1522 LASSERT(PageLocked(oap->oap_page));
1524 /* take the page out of our book-keeping */
1525 list_del_init(&oap->oap_pending_item);
1526 lop_update_pending(cli, lop, cmd, -1);
1527 list_del_init(&oap->oap_urgent_item);
1529 /* ask the caller for the size of the io as the rpc leaves. */
1530 if (!(oap->oap_async_flags & ASYNC_COUNT_STABLE))
1532 ops->ap_refresh_count(oap->oap_caller_data,cmd);
1533 if (oap->oap_count <= 0) {
1534 CDEBUG(D_CACHE, "oap %p count %d, completing\n", oap,
1536 osc_ap_completion(cli, NULL, oap, 0, oap->oap_count);
1540 /* now put the page back in our accounting */
1541 list_add_tail(&oap->oap_rpc_item, &rpc_list);
1542 if (++page_count >= cli->cl_max_pages_per_rpc)
1546 osc_wake_cache_waiters(cli);
1548 if (page_count == 0)
1551 loi_list_maint(cli, loi);
1552 spin_unlock(&cli->cl_loi_list_lock);
1554 request = osc_build_req(cli, &rpc_list, page_count, cmd);
1555 if (IS_ERR(request)) {
1556 /* this should happen rarely and is pretty bad, it makes the
1557 * pending list not follow the dirty order */
1558 spin_lock(&cli->cl_loi_list_lock);
1559 list_for_each_safe(pos, tmp, &rpc_list) {
1560 oap = list_entry(pos, struct osc_async_page,
1562 list_del_init(&oap->oap_rpc_item);
1564 /* queued sync pages can be torn down while the pages
1565 * were between the pending list and the rpc */
1566 if (oap->oap_interrupted) {
1567 CDEBUG(D_INODE, "oap %p interrupted\n", oap);
1568 osc_ap_completion(cli, NULL, oap, 0,
1573 /* put the page back in the loi/lop lists */
1574 list_add_tail(&oap->oap_pending_item,
1576 lop_update_pending(cli, lop, cmd, 1);
1577 if (oap->oap_async_flags & ASYNC_URGENT)
1578 list_add(&oap->oap_urgent_item,
1581 loi_list_maint(cli, loi);
1582 RETURN(PTR_ERR(request));
1585 LASSERT(sizeof(*aa) <= sizeof(request->rq_async_args));
1586 aa = (struct osc_brw_async_args *)&request->rq_async_args;
1587 INIT_LIST_HEAD(&aa->aa_oaps);
1588 list_splice(&rpc_list, &aa->aa_oaps);
1589 INIT_LIST_HEAD(&rpc_list);
1592 if (cmd == OBD_BRW_READ) {
1593 lprocfs_oh_tally_log2(&cli->cl_read_page_hist, page_count);
1594 lprocfs_oh_tally(&cli->cl_read_rpc_hist, cli->cl_r_in_flight);
1596 lprocfs_oh_tally_log2(&cli->cl_write_page_hist, page_count);
1597 lprocfs_oh_tally(&cli->cl_write_rpc_hist,
1598 cli->cl_w_in_flight);
1602 spin_lock(&cli->cl_loi_list_lock);
1604 if (cmd == OBD_BRW_READ)
1605 cli->cl_r_in_flight++;
1607 cli->cl_w_in_flight++;
1608 /* queued sync pages can be torn down while the pages
1609 * were between the pending list and the rpc */
1610 list_for_each(pos, &aa->aa_oaps) {
1611 oap = list_entry(pos, struct osc_async_page, oap_rpc_item);
1612 if (oap->oap_interrupted) {
1613 CDEBUG(D_INODE, "oap %p in req %p interrupted\n",
1615 ptlrpc_mark_interrupted(request);
1620 CDEBUG(D_INODE, "req %p: %d pages, aa %p. now %dr/%dw in flight\n",
1621 request, page_count, aa, cli->cl_r_in_flight,
1622 cli->cl_w_in_flight);
1624 oap->oap_request = ptlrpc_request_addref(request);
1625 request->rq_interpret_reply = brw_interpret_oap;
1626 ptlrpcd_add_req(request);
1630 static int lop_makes_rpc(struct client_obd *cli, struct loi_oap_pages *lop,
1636 if (lop->lop_num_pending == 0)
1639 /* if we have an invalid import we want to drain the queued pages
1640 * by forcing them through rpcs that immediately fail and complete
1641 * the pages. recovery relies on this to empty the queued pages
1642 * before canceling the locks and evicting down the llite pages */
1643 if (cli->cl_import == NULL || cli->cl_import->imp_invalid)
1646 /* stream rpcs in queue order as long as as there is an urgent page
1647 * queued. this is our cheap solution for good batching in the case
1648 * where writepage marks some random page in the middle of the file as
1649 * urgent because of, say, memory pressure */
1650 if (!list_empty(&lop->lop_urgent))
1653 /* fire off rpcs when we have 'optimal' rpcs as tuned for the wire. */
1654 optimal = cli->cl_max_pages_per_rpc;
1655 if (cmd == OBD_BRW_WRITE) {
1656 /* trigger a write rpc stream as long as there are dirtiers
1657 * waiting for space. as they're waiting, they're not going to
1658 * create more pages to coallesce with what's waiting.. */
1659 if (!list_empty(&cli->cl_cache_waiters))
1662 /* *2 to avoid triggering rpcs that would want to include pages
1663 * that are being queued but which can't be made ready until
1664 * the queuer finishes with the page. this is a wart for
1665 * llite::commit_write() */
1668 if (lop->lop_num_pending >= optimal)
1674 static void on_list(struct list_head *item, struct list_head *list,
1677 if (list_empty(item) && should_be_on)
1678 list_add_tail(item, list);
1679 else if (!list_empty(item) && !should_be_on)
1680 list_del_init(item);
1683 /* maintain the loi's cli list membership invariants so that osc_send_oap_rpc
1684 * can find pages to build into rpcs quickly */
1685 static void loi_list_maint(struct client_obd *cli, struct lov_oinfo *loi)
1687 on_list(&loi->loi_cli_item, &cli->cl_loi_ready_list,
1688 lop_makes_rpc(cli, &loi->loi_write_lop, OBD_BRW_WRITE) ||
1689 lop_makes_rpc(cli, &loi->loi_read_lop, OBD_BRW_READ));
1691 on_list(&loi->loi_write_item, &cli->cl_loi_write_list,
1692 loi->loi_write_lop.lop_num_pending);
1694 on_list(&loi->loi_read_item, &cli->cl_loi_read_list,
1695 loi->loi_read_lop.lop_num_pending);
1698 #define LOI_DEBUG(LOI, STR, args...) \
1699 CDEBUG(D_INODE, "loi ready %d wr %d:%d rd %d:%d " STR, \
1700 !list_empty(&(LOI)->loi_cli_item), \
1701 (LOI)->loi_write_lop.lop_num_pending, \
1702 !list_empty(&(LOI)->loi_write_lop.lop_urgent), \
1703 (LOI)->loi_read_lop.lop_num_pending, \
1704 !list_empty(&(LOI)->loi_read_lop.lop_urgent), \
1707 struct lov_oinfo *osc_next_loi(struct client_obd *cli)
1710 /* first return all objects which we already know to have
1711 * pages ready to be stuffed into rpcs */
1712 if (!list_empty(&cli->cl_loi_ready_list))
1713 RETURN(list_entry(cli->cl_loi_ready_list.next,
1714 struct lov_oinfo, loi_cli_item));
1716 /* then if we have cache waiters, return all objects with queued
1717 * writes. This is especially important when many small files
1718 * have filled up the cache and not been fired into rpcs because
1719 * they don't pass the nr_pending/object threshhold */
1720 if (!list_empty(&cli->cl_cache_waiters) &&
1721 !list_empty(&cli->cl_loi_write_list))
1722 RETURN(list_entry(cli->cl_loi_write_list.next,
1723 struct lov_oinfo, loi_write_item));
1725 /* then return all queued objects when we have an invalid import
1726 * so that they get flushed */
1727 if (cli->cl_import == NULL || cli->cl_import->imp_invalid) {
1728 if (!list_empty(&cli->cl_loi_write_list))
1729 RETURN(list_entry(cli->cl_loi_write_list.next,
1730 struct lov_oinfo, loi_write_item));
1731 if (!list_empty(&cli->cl_loi_read_list))
1732 RETURN(list_entry(cli->cl_loi_read_list.next,
1733 struct lov_oinfo, loi_read_item));
1738 /* called with the loi list lock held */
1739 static void osc_check_rpcs(struct client_obd *cli)
1741 struct lov_oinfo *loi;
1742 int rc = 0, race_counter = 0;
1745 while ((loi = osc_next_loi(cli)) != NULL) {
1746 LOI_DEBUG(loi, "%lu in flight\n", rpcs_in_flight(cli));
1748 if (rpcs_in_flight(cli) >= cli->cl_max_rpcs_in_flight)
1751 /* attempt some read/write balancing by alternating between
1752 * reads and writes in an object. The makes_rpc checks here
1753 * would be redundant if we were getting read/write work items
1754 * instead of objects. we don't want send_oap_rpc to drain a
1755 * partial read pending queue when we're given this object to
1756 * do io on writes while there are cache waiters */
1757 if (lop_makes_rpc(cli, &loi->loi_write_lop, OBD_BRW_WRITE)) {
1758 rc = osc_send_oap_rpc(cli, loi, OBD_BRW_WRITE,
1759 &loi->loi_write_lop);
1767 if (lop_makes_rpc(cli, &loi->loi_read_lop, OBD_BRW_READ)) {
1768 rc = osc_send_oap_rpc(cli, loi, OBD_BRW_READ,
1769 &loi->loi_read_lop);
1778 /* attempt some inter-object balancing by issueing rpcs
1779 * for each object in turn */
1780 if (!list_empty(&loi->loi_cli_item))
1781 list_del_init(&loi->loi_cli_item);
1782 if (!list_empty(&loi->loi_write_item))
1783 list_del_init(&loi->loi_write_item);
1784 if (!list_empty(&loi->loi_read_item))
1785 list_del_init(&loi->loi_read_item);
1787 loi_list_maint(cli, loi);
1789 /* send_oap_rpc fails with 0 when make_ready tells it to
1790 * back off. llite's make_ready does this when it tries
1791 * to lock a page queued for write that is already locked.
1792 * we want to try sending rpcs from many objects, but we
1793 * don't want to spin failing with 0. */
1794 if (race_counter == 10)
1800 /* we're trying to queue a page in the osc so we're subject to the
1801 * 'cl_dirty_max' limit on the number of pages that can be queued in the osc.
1802 * If the osc's queued pages are already at that limit, then we want to sleep
1803 * until there is space in the osc's queue for us. We also may be waiting for
1804 * write credits from the OST if there are RPCs in flight that may return some
1805 * before we fall back to sync writes.
1807 * We need this know our allocation was granted in the presence of signals */
1808 static int ocw_granted(struct client_obd *cli, struct osc_cache_waiter *ocw)
1812 spin_lock(&cli->cl_loi_list_lock);
1813 rc = list_empty(&ocw->ocw_entry) || rpcs_in_flight(cli) == 0;
1814 spin_unlock(&cli->cl_loi_list_lock);
1818 /* Caller must hold loi_list_lock - we drop/regain it if we need to wait for
1819 * grant or cache space. */
1820 static int osc_enter_cache(struct client_obd *cli, struct lov_oinfo *loi,
1821 struct osc_async_page *oap)
1823 struct osc_cache_waiter ocw;
1824 struct l_wait_info lwi = { 0 };
1825 struct timeval start, stop;
1827 CDEBUG(D_CACHE, "dirty: %ld dirty_max: %ld dropped: %lu grant: %lu\n",
1828 cli->cl_dirty, cli->cl_dirty_max, cli->cl_lost_grant,
1829 cli->cl_avail_grant);
1831 if (cli->cl_dirty_max < PAGE_SIZE)
1834 /* Hopefully normal case - cache space and write credits available */
1835 if (cli->cl_dirty + PAGE_SIZE <= cli->cl_dirty_max &&
1836 cli->cl_avail_grant >= PAGE_SIZE) {
1837 /* account for ourselves */
1838 osc_consume_write_grant(cli, oap);
1842 /* Make sure that there are write rpcs in flight to wait for. This
1843 * is a little silly as this object may not have any pending but
1844 * other objects sure might. */
1845 if (cli->cl_w_in_flight) {
1846 list_add_tail(&ocw.ocw_entry, &cli->cl_cache_waiters);
1847 init_waitqueue_head(&ocw.ocw_waitq);
1851 loi_list_maint(cli, loi);
1852 osc_check_rpcs(cli);
1853 spin_unlock(&cli->cl_loi_list_lock);
1855 CDEBUG(0, "sleeping for cache space\n");
1856 do_gettimeofday(&start);
1857 l_wait_event(ocw.ocw_waitq, ocw_granted(cli, &ocw), &lwi);
1858 do_gettimeofday(&stop);
1859 spin_lock(&cli->cl_loi_list_lock);
1860 lprocfs_stime_record(&cli->cl_enter_stime, &stop, &start);
1861 if (!list_empty(&ocw.ocw_entry)) {
1862 list_del(&ocw.ocw_entry);
1871 /* the companion to enter_cache, called when an oap is no longer part of the
1872 * dirty accounting.. so writeback completes or truncate happens before writing
1873 * starts. must be called with the loi lock held. */
1874 static void osc_exit_cache(struct client_obd *cli, struct osc_async_page *oap,
1879 if (!(oap->oap_brw_flags & OBD_BRW_FROM_GRANT)) {
1884 oap->oap_brw_flags &= ~OBD_BRW_FROM_GRANT;
1885 cli->cl_dirty -= PAGE_SIZE;
1887 cli->cl_lost_grant += PAGE_SIZE;
1888 CDEBUG(D_CACHE, "lost grant: %lu avail grant: %lu dirty: %lu\n",
1889 cli->cl_lost_grant, cli->cl_avail_grant, cli->cl_dirty);
1895 int osc_prep_async_page(struct obd_export *exp, struct lov_stripe_md *lsm,
1896 struct lov_oinfo *loi, struct page *page,
1897 obd_off offset, struct obd_async_page_ops *ops,
1898 void *data, void **res)
1900 struct osc_async_page *oap;
1903 OBD_ALLOC(oap, sizeof(*oap));
1907 oap->oap_magic = OAP_MAGIC;
1908 oap->oap_cli = &exp->exp_obd->u.cli;
1911 oap->oap_caller_ops = ops;
1912 oap->oap_caller_data = data;
1914 oap->oap_page = page;
1915 oap->oap_obj_off = offset;
1917 INIT_LIST_HEAD(&oap->oap_pending_item);
1918 INIT_LIST_HEAD(&oap->oap_urgent_item);
1919 INIT_LIST_HEAD(&oap->oap_rpc_item);
1921 oap->oap_occ.occ_interrupted = osc_occ_interrupted;
1923 CDEBUG(D_CACHE, "oap %p page %p obj off "LPU64"\n", oap, page, offset);
1928 static int osc_queue_async_io(struct obd_export *exp, struct lov_stripe_md *lsm,
1929 struct lov_oinfo *loi, void *cookie,
1930 int cmd, obd_off off, int count,
1931 obd_flags brw_flags, enum async_flags async_flags)
1933 struct client_obd *cli = &exp->exp_obd->u.cli;
1934 struct osc_async_page *oap;
1935 struct loi_oap_pages *lop;
1939 oap = OAP_FROM_COOKIE(cookie);
1941 if (cli->cl_import == NULL || cli->cl_import->imp_invalid)
1944 if (!list_empty(&oap->oap_pending_item) ||
1945 !list_empty(&oap->oap_urgent_item) ||
1946 !list_empty(&oap->oap_rpc_item))
1950 loi = &lsm->lsm_oinfo[0];
1952 spin_lock(&cli->cl_loi_list_lock);
1955 oap->oap_async_flags = async_flags;
1956 oap->oap_page_off = off;
1957 oap->oap_count = count;
1958 oap->oap_brw_flags = brw_flags;
1960 if (cmd == OBD_BRW_WRITE) {
1961 rc = osc_enter_cache(cli, loi, oap);
1963 spin_unlock(&cli->cl_loi_list_lock);
1966 lop = &loi->loi_write_lop;
1968 lop = &loi->loi_read_lop;
1971 if (oap->oap_async_flags & ASYNC_URGENT)
1972 list_add(&oap->oap_urgent_item, &lop->lop_urgent);
1973 list_add_tail(&oap->oap_pending_item, &lop->lop_pending);
1974 lop_update_pending(cli, lop, cmd, 1);
1976 loi_list_maint(cli, loi);
1978 LOI_DEBUG(loi, "oap %p page %p added for cmd %d\n", oap, oap->oap_page,
1981 osc_check_rpcs(cli);
1982 spin_unlock(&cli->cl_loi_list_lock);
1987 /* aka (~was & now & flag), but this is more clear :) */
1988 #define SETTING(was, now, flag) (!(was & flag) && (now & flag))
1990 static int osc_set_async_flags(struct obd_export *exp,
1991 struct lov_stripe_md *lsm,
1992 struct lov_oinfo *loi, void *cookie,
1993 obd_flags async_flags)
1995 struct client_obd *cli = &exp->exp_obd->u.cli;
1996 struct loi_oap_pages *lop;
1997 struct osc_async_page *oap;
2001 oap = OAP_FROM_COOKIE(cookie);
2003 if (cli->cl_import == NULL || cli->cl_import->imp_invalid)
2007 loi = &lsm->lsm_oinfo[0];
2009 if (oap->oap_cmd == OBD_BRW_WRITE) {
2010 lop = &loi->loi_write_lop;
2012 lop = &loi->loi_read_lop;
2015 spin_lock(&cli->cl_loi_list_lock);
2017 if (list_empty(&oap->oap_pending_item))
2018 GOTO(out, rc = -EINVAL);
2020 if ((oap->oap_async_flags & async_flags) == async_flags)
2023 if (SETTING(oap->oap_async_flags, async_flags, ASYNC_READY))
2024 oap->oap_async_flags |= ASYNC_READY;
2026 if (SETTING(oap->oap_async_flags, async_flags, ASYNC_URGENT)) {
2027 if (list_empty(&oap->oap_rpc_item)) {
2028 list_add(&oap->oap_urgent_item, &lop->lop_urgent);
2029 loi_list_maint(cli, loi);
2033 LOI_DEBUG(loi, "oap %p page %p has flags %x\n", oap, oap->oap_page,
2034 oap->oap_async_flags);
2036 osc_check_rpcs(cli);
2037 spin_unlock(&cli->cl_loi_list_lock);
2041 static int osc_queue_group_io(struct obd_export *exp, struct lov_stripe_md *lsm,
2042 struct lov_oinfo *loi,
2043 struct obd_io_group *oig, void *cookie,
2044 int cmd, obd_off off, int count,
2045 obd_flags brw_flags,
2046 obd_flags async_flags)
2048 struct client_obd *cli = &exp->exp_obd->u.cli;
2049 struct osc_async_page *oap;
2050 struct loi_oap_pages *lop;
2053 oap = OAP_FROM_COOKIE(cookie);
2055 if (cli->cl_import == NULL || cli->cl_import->imp_invalid)
2058 if (!list_empty(&oap->oap_pending_item) ||
2059 !list_empty(&oap->oap_urgent_item) ||
2060 !list_empty(&oap->oap_rpc_item))
2064 loi = &lsm->lsm_oinfo[0];
2066 spin_lock(&cli->cl_loi_list_lock);
2069 oap->oap_page_off = off;
2070 oap->oap_count = count;
2071 oap->oap_brw_flags = brw_flags;
2072 oap->oap_async_flags = async_flags;
2074 if (cmd == OBD_BRW_WRITE)
2075 lop = &loi->loi_write_lop;
2077 lop = &loi->loi_read_lop;
2079 list_add_tail(&oap->oap_pending_item, &lop->lop_pending_group);
2080 if (oap->oap_async_flags & ASYNC_GROUP_SYNC) {
2082 oig_add_one(oig, &oap->oap_occ);
2085 LOI_DEBUG(loi, "oap %p page %p on group pending\n", oap, oap->oap_page);
2087 spin_unlock(&cli->cl_loi_list_lock);
2092 static void osc_group_to_pending(struct client_obd *cli, struct lov_oinfo *loi,
2093 struct loi_oap_pages *lop, int cmd)
2095 struct list_head *pos, *tmp;
2096 struct osc_async_page *oap;
2098 list_for_each_safe(pos, tmp, &lop->lop_pending_group) {
2099 oap = list_entry(pos, struct osc_async_page, oap_pending_item);
2100 list_del(&oap->oap_pending_item);
2101 list_add_tail(&oap->oap_pending_item, &lop->lop_pending);
2102 list_add(&oap->oap_urgent_item, &lop->lop_urgent);
2103 lop_update_pending(cli, lop, cmd, 1);
2105 loi_list_maint(cli, loi);
2108 static int osc_trigger_group_io(struct obd_export *exp,
2109 struct lov_stripe_md *lsm,
2110 struct lov_oinfo *loi,
2111 struct obd_io_group *oig)
2113 struct client_obd *cli = &exp->exp_obd->u.cli;
2117 loi = &lsm->lsm_oinfo[0];
2119 spin_lock(&cli->cl_loi_list_lock);
2121 osc_group_to_pending(cli, loi, &loi->loi_write_lop, OBD_BRW_WRITE);
2122 osc_group_to_pending(cli, loi, &loi->loi_read_lop, OBD_BRW_READ);
2124 osc_check_rpcs(cli);
2125 spin_unlock(&cli->cl_loi_list_lock);
2130 static int osc_teardown_async_page(struct obd_export *exp,
2131 struct lov_stripe_md *lsm,
2132 struct lov_oinfo *loi, void *cookie)
2134 struct client_obd *cli = &exp->exp_obd->u.cli;
2135 struct loi_oap_pages *lop;
2136 struct osc_async_page *oap;
2140 oap = OAP_FROM_COOKIE(cookie);
2143 loi = &lsm->lsm_oinfo[0];
2145 if (oap->oap_cmd == OBD_BRW_WRITE) {
2146 lop = &loi->loi_write_lop;
2148 lop = &loi->loi_read_lop;
2151 spin_lock(&cli->cl_loi_list_lock);
2153 if (!list_empty(&oap->oap_rpc_item))
2154 GOTO(out, rc = -EBUSY);
2156 osc_exit_cache(cli, oap, 0);
2157 osc_wake_cache_waiters(cli);
2159 if (!list_empty(&oap->oap_urgent_item)) {
2160 list_del_init(&oap->oap_urgent_item);
2161 oap->oap_async_flags &= ~ASYNC_URGENT;
2163 if (!list_empty(&oap->oap_pending_item)) {
2164 list_del_init(&oap->oap_pending_item);
2165 lop_update_pending(cli, lop, oap->oap_cmd, -1);
2167 loi_list_maint(cli, loi);
2169 LOI_DEBUG(loi, "oap %p page %p torn down\n", oap, oap->oap_page);
2171 spin_unlock(&cli->cl_loi_list_lock);
2173 OBD_FREE(oap, sizeof(*oap));
2178 /* Note: caller will lock/unlock, and set uptodate on the pages */
2179 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
2180 static int sanosc_brw_read(struct obd_export *exp, struct obdo *oa,
2181 struct lov_stripe_md *lsm, obd_count page_count,
2182 struct brw_page *pga)
2184 struct ptlrpc_request *request = NULL;
2185 struct ost_body *body;
2186 struct niobuf_remote *nioptr;
2187 struct obd_ioobj *iooptr;
2188 int rc, size[3] = {sizeof(*body)}, mapped = 0;
2192 /* XXX does not handle 'new' brw protocol */
2194 size[1] = sizeof(struct obd_ioobj);
2195 size[2] = page_count * sizeof(*nioptr);
2197 request = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_OBD_VERSION,
2198 OST_SAN_READ, 3, size, NULL);
2202 body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof(*body));
2203 iooptr = lustre_msg_buf(request->rq_reqmsg, 1, sizeof(*iooptr));
2204 nioptr = lustre_msg_buf(request->rq_reqmsg, 2,
2205 sizeof(*nioptr) * page_count);
2207 memcpy(&body->oa, oa, sizeof(body->oa));
2209 obdo_to_ioobj(oa, iooptr);
2210 iooptr->ioo_bufcnt = page_count;
2212 for (mapped = 0; mapped < page_count; mapped++, nioptr++) {
2213 LASSERT(PageLocked(pga[mapped].pg));
2214 LASSERT(mapped == 0 ||
2215 pga[mapped].disk_offset > pga[mapped - 1].disk_offset);
2217 nioptr->offset = pga[mapped].disk_offset;
2218 nioptr->len = pga[mapped].count;
2219 nioptr->flags = pga[mapped].flag;
2222 size[1] = page_count * sizeof(*nioptr);
2223 request->rq_replen = lustre_msg_size(2, size);
2225 rc = ptlrpc_queue_wait(request);
2229 body = lustre_swab_repbuf(request, 0, sizeof(*body),
2230 lustre_swab_ost_body);
2232 CERROR("Can't unpack body\n");
2233 GOTO(out_req, rc = -EPROTO);
2236 memcpy(oa, &body->oa, sizeof(*oa));
2238 swab = lustre_msg_swabbed(request->rq_repmsg);
2239 LASSERT_REPSWAB(request, 1);
2240 nioptr = lustre_msg_buf(request->rq_repmsg, 1, size[1]);
2242 /* nioptr missing or short */
2243 GOTO(out_req, rc = -EPROTO);
2247 for (mapped = 0; mapped < page_count; mapped++, nioptr++) {
2248 struct page *page = pga[mapped].pg;
2249 struct buffer_head *bh;
2253 lustre_swab_niobuf_remote (nioptr);
2255 /* got san device associated */
2256 LASSERT(exp->exp_obd != NULL);
2257 dev = exp->exp_obd->u.cli.cl_sandev;
2260 if (!nioptr->offset) {
2261 CDEBUG(D_PAGE, "hole at ino %lu; index %ld\n",
2262 page->mapping->host->i_ino,
2264 memset(page_address(page), 0, PAGE_SIZE);
2268 if (!page->buffers) {
2269 create_empty_buffers(page, dev, PAGE_SIZE);
2272 clear_bit(BH_New, &bh->b_state);
2273 set_bit(BH_Mapped, &bh->b_state);
2274 bh->b_blocknr = (unsigned long)nioptr->offset;
2276 clear_bit(BH_Uptodate, &bh->b_state);
2278 ll_rw_block(READ, 1, &bh);
2282 /* if buffer already existed, it must be the
2283 * one we mapped before, check it */
2284 LASSERT(!test_bit(BH_New, &bh->b_state));
2285 LASSERT(test_bit(BH_Mapped, &bh->b_state));
2286 LASSERT(bh->b_blocknr == (unsigned long)nioptr->offset);
2288 /* wait it's io completion */
2289 if (test_bit(BH_Lock, &bh->b_state))
2292 if (!test_bit(BH_Uptodate, &bh->b_state))
2293 ll_rw_block(READ, 1, &bh);
2297 /* must do syncronous write here */
2299 if (!buffer_uptodate(bh)) {
2307 ptlrpc_req_finished(request);
2311 static int sanosc_brw_write(struct obd_export *exp, struct obdo *oa,
2312 struct lov_stripe_md *lsm, obd_count page_count,
2313 struct brw_page *pga)
2315 struct ptlrpc_request *request = NULL;
2316 struct ost_body *body;
2317 struct niobuf_remote *nioptr;
2318 struct obd_ioobj *iooptr;
2319 int rc, size[3] = {sizeof(*body)}, mapped = 0;
2323 size[1] = sizeof(struct obd_ioobj);
2324 size[2] = page_count * sizeof(*nioptr);
2326 request = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_OBD_VERSION,
2327 OST_SAN_WRITE, 3, size, NULL);
2331 body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof (*body));
2332 iooptr = lustre_msg_buf(request->rq_reqmsg, 1, sizeof (*iooptr));
2333 nioptr = lustre_msg_buf(request->rq_reqmsg, 2,
2334 sizeof (*nioptr) * page_count);
2336 memcpy(&body->oa, oa, sizeof(body->oa));
2338 obdo_to_ioobj(oa, iooptr);
2339 iooptr->ioo_bufcnt = page_count;
2342 for (mapped = 0; mapped < page_count; mapped++, nioptr++) {
2343 LASSERT(PageLocked(pga[mapped].pg));
2344 LASSERT(mapped == 0 ||
2345 pga[mapped].disk_offset > pga[mapped - 1].disk_offset);
2347 nioptr->offset = pga[mapped].disk_offset;
2348 nioptr->len = pga[mapped].count;
2349 nioptr->flags = pga[mapped].flag;
2352 size[1] = page_count * sizeof(*nioptr);
2353 request->rq_replen = lustre_msg_size(2, size);
2355 rc = ptlrpc_queue_wait(request);
2359 swab = lustre_msg_swabbed (request->rq_repmsg);
2360 LASSERT_REPSWAB (request, 1);
2361 nioptr = lustre_msg_buf(request->rq_repmsg, 1, size[1]);
2363 CERROR("absent/short niobuf array\n");
2364 GOTO(out_req, rc = -EPROTO);
2368 for (mapped = 0; mapped < page_count; mapped++, nioptr++) {
2369 struct page *page = pga[mapped].pg;
2370 struct buffer_head *bh;
2374 lustre_swab_niobuf_remote (nioptr);
2376 /* got san device associated */
2377 LASSERT(exp->exp_obd != NULL);
2378 dev = exp->exp_obd->u.cli.cl_sandev;
2380 if (!page->buffers) {
2381 create_empty_buffers(page, dev, PAGE_SIZE);
2384 LASSERT(!test_bit(BH_New, &page->buffers->b_state));
2385 LASSERT(test_bit(BH_Mapped, &page->buffers->b_state));
2386 LASSERT(page->buffers->b_blocknr ==
2387 (unsigned long)nioptr->offset);
2393 /* if buffer locked, wait it's io completion */
2394 if (test_bit(BH_Lock, &bh->b_state))
2397 clear_bit(BH_New, &bh->b_state);
2398 set_bit(BH_Mapped, &bh->b_state);
2400 /* override the block nr */
2401 bh->b_blocknr = (unsigned long)nioptr->offset;
2403 /* we are about to write it, so set it
2405 * page lock should garentee no race condition here */
2406 set_bit(BH_Uptodate, &bh->b_state);
2407 set_bit(BH_Dirty, &bh->b_state);
2409 ll_rw_block(WRITE, 1, &bh);
2411 /* must do syncronous write here */
2413 if (!buffer_uptodate(bh) || test_bit(BH_Dirty, &bh->b_state)) {
2421 ptlrpc_req_finished(request);
2425 static int sanosc_brw(int cmd, struct obd_export *exp, struct obdo *oa,
2426 struct lov_stripe_md *lsm, obd_count page_count,
2427 struct brw_page *pga, struct obd_trans_info *oti)
2431 while (page_count) {
2432 obd_count pages_per_brw;
2435 if (page_count > PTLRPC_MAX_BRW_PAGES)
2436 pages_per_brw = PTLRPC_MAX_BRW_PAGES;
2438 pages_per_brw = page_count;
2440 if (cmd & OBD_BRW_WRITE)
2441 rc = sanosc_brw_write(exp, oa, lsm, pages_per_brw,pga);
2443 rc = sanosc_brw_read(exp, oa, lsm, pages_per_brw, pga);
2448 page_count -= pages_per_brw;
2449 pga += pages_per_brw;
2456 static void osc_set_data_with_check(struct lustre_handle *lockh, void *data)
2458 struct ldlm_lock *lock = ldlm_handle2lock(lockh);
2461 CERROR("lockh %p, data %p - client evicted?\n", lockh, data);
2465 lock_res_and_lock(lock);
2467 if (lock->l_ast_data && lock->l_ast_data != data) {
2468 struct inode *new_inode = data;
2469 struct inode *old_inode = lock->l_ast_data;
2470 if (!(old_inode->i_state & I_FREEING))
2471 LDLM_ERROR(lock, "inconsistent l_ast_data found");
2472 LASSERTF(old_inode->i_state & I_FREEING,
2473 "Found existing inode %p/%lu/%u state %lu in lock: "
2474 "setting data to %p/%lu/%u\n", old_inode,
2475 old_inode->i_ino, old_inode->i_generation,
2477 new_inode, new_inode->i_ino, new_inode->i_generation);
2480 lock->l_ast_data = data;
2481 unlock_res_and_lock(lock);
2482 LDLM_LOCK_PUT(lock);
2485 static int osc_change_cbdata(struct obd_export *exp, struct lov_stripe_md *lsm,
2486 ldlm_iterator_t replace, void *data)
2488 struct ldlm_res_id res_id = { .name = {0} };
2489 struct obd_device *obd = class_exp2obd(exp);
2491 res_id.name[0] = lsm->lsm_object_id;
2492 res_id.name[2] = lsm->lsm_object_gr;
2493 ldlm_change_cbdata(obd->obd_namespace, &res_id, replace, data);
2497 static int osc_enqueue(struct obd_export *exp, struct lov_stripe_md *lsm,
2498 __u32 type, ldlm_policy_data_t *policy, __u32 mode,
2499 int *flags, void *bl_cb, void *cp_cb, void *gl_cb,
2500 void *data, __u32 lvb_len, void *lvb_swabber,
2501 struct lustre_handle *lockh)
2503 struct obd_device *obd = exp->exp_obd;
2504 struct ldlm_res_id res_id = { .name = {0} };
2506 struct ldlm_reply *rep;
2507 struct ptlrpc_request *req = NULL;
2511 res_id.name[0] = lsm->lsm_object_id;
2512 res_id.name[2] = lsm->lsm_object_gr;
2514 /* Filesystem lock extents are extended to page boundaries so that
2515 * dealing with the page cache is a little smoother. */
2516 policy->l_extent.start -= policy->l_extent.start & ~PAGE_MASK;
2517 policy->l_extent.end |= ~PAGE_MASK;
2519 if (lsm->lsm_oinfo->loi_kms_valid == 0)
2522 /* Next, search for already existing extent locks that will cover us */
2523 rc = ldlm_lock_match(obd->obd_namespace, 0, &res_id, type, policy, mode,
2526 if (ptlrpcs_check_cred(obd->u.cli.cl_import)) {
2527 /* return immediately if no credential held */
2528 ldlm_lock_decref(lockh, mode);
2532 osc_set_data_with_check(lockh, data);
2533 if (*flags & LDLM_FL_HAS_INTENT) {
2534 /* I would like to be able to ASSERT here that rss <=
2535 * kms, but I can't, for reasons which are explained in
2538 /* We already have a lock, and it's referenced */
2542 /* If we're trying to read, we also search for an existing PW lock. The
2543 * VFS and page cache already protect us locally, so lots of readers/
2544 * writers can share a single PW lock.
2546 * There are problems with conversion deadlocks, so instead of
2547 * converting a read lock to a write lock, we'll just enqueue a new
2550 * At some point we should cancel the read lock instead of making them
2551 * send us a blocking callback, but there are problems with canceling
2552 * locks out from other users right now, too. */
2554 if (mode == LCK_PR) {
2555 rc = ldlm_lock_match(obd->obd_namespace, 0, &res_id, type,
2556 policy, LCK_PW, lockh);
2558 if (ptlrpcs_check_cred(obd->u.cli.cl_import)) {
2559 /* return immediately if no credential held */
2560 ldlm_lock_decref(lockh, LCK_PW);
2564 /* FIXME: This is not incredibly elegant, but it might
2565 * be more elegant than adding another parameter to
2566 * lock_match. I want a second opinion. */
2567 ldlm_lock_addref(lockh, LCK_PR);
2568 ldlm_lock_decref(lockh, LCK_PW);
2569 osc_set_data_with_check(lockh, data);
2573 if (mode == LCK_PW) {
2574 rc = ldlm_lock_match(obd->obd_namespace, 0, &res_id, type,
2575 policy, LCK_PR, lockh);
2577 rc = ldlm_cli_convert(lockh, mode, flags);
2579 /* Update readers/writers accounting */
2580 ldlm_lock_addref(lockh, LCK_PW);
2581 ldlm_lock_decref(lockh, LCK_PR);
2582 osc_set_data_with_check(lockh, data);
2585 /* If the conversion failed, we need to drop refcount
2586 on matched lock before we get new one */
2587 /* XXX Won't it save us some efforts if we cancel PR
2588 lock here? We are going to take PW lock anyway and it
2589 will invalidate PR lock */
2590 ldlm_lock_decref(lockh, LCK_PR);
2591 if (rc != EDEADLOCK) {
2598 if (*flags & LDLM_FL_HAS_INTENT) {
2599 int size[2] = {0, sizeof(struct ldlm_request)};
2601 req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_DLM_VERSION,
2602 LDLM_ENQUEUE, 2, size, NULL);
2606 size[0] = sizeof(*rep);
2607 size[1] = sizeof(lvb);
2608 req->rq_replen = lustre_msg_size(2, size);
2610 rc = ldlm_cli_enqueue(exp, req, obd->obd_namespace, res_id, type,
2611 policy, mode, flags, bl_cb, cp_cb, gl_cb, data,
2612 &lvb, sizeof(lvb), lustre_swab_ost_lvb, lockh);
2614 if (rc == ELDLM_LOCK_ABORTED) {
2615 /* swabbed by ldlm_cli_enqueue() */
2616 LASSERT_REPSWABBED(req, 0);
2617 rep = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*rep));
2618 LASSERT(rep != NULL);
2619 if (rep->lock_policy_res1)
2620 rc = rep->lock_policy_res1;
2622 ptlrpc_req_finished(req);
2625 if ((*flags & LDLM_FL_HAS_INTENT && rc == ELDLM_LOCK_ABORTED) || !rc) {
2626 CDEBUG(D_INODE, "received kms == "LPU64", blocks == "LPU64"\n",
2627 lvb.lvb_size, lvb.lvb_blocks);
2628 lsm->lsm_oinfo->loi_rss = lvb.lvb_size;
2629 lsm->lsm_oinfo->loi_blocks = lvb.lvb_blocks;
2635 static int osc_match(struct obd_export *exp, struct lov_stripe_md *lsm,
2636 __u32 type, ldlm_policy_data_t *policy, __u32 mode,
2637 int *flags, void *data, struct lustre_handle *lockh)
2639 struct ldlm_res_id res_id = { .name = {0} };
2640 struct obd_device *obd = exp->exp_obd;
2644 res_id.name[0] = lsm->lsm_object_id;
2645 res_id.name[2] = lsm->lsm_object_gr;
2647 OBD_FAIL_RETURN(OBD_FAIL_OSC_MATCH, -EIO);
2649 /* Filesystem lock extents are extended to page boundaries so that
2650 * dealing with the page cache is a little smoother */
2651 policy->l_extent.start -= policy->l_extent.start & ~PAGE_MASK;
2652 policy->l_extent.end |= ~PAGE_MASK;
2654 /* Next, search for already existing extent locks that will cover us */
2655 rc = ldlm_lock_match(obd->obd_namespace, *flags, &res_id, type,
2656 policy, mode, lockh);
2658 // if (!(*flags & LDLM_FL_TEST_LOCK))
2659 osc_set_data_with_check(lockh, data);
2662 /* If we're trying to read, we also search for an existing PW lock. The
2663 * VFS and page cache already protect us locally, so lots of readers/
2664 * writers can share a single PW lock. */
2665 if (mode == LCK_PR) {
2666 rc = ldlm_lock_match(obd->obd_namespace, *flags, &res_id, type,
2667 policy, LCK_PW, lockh);
2668 if (rc == 1 && !(*flags & LDLM_FL_TEST_LOCK)) {
2669 /* FIXME: This is not incredibly elegant, but it might
2670 * be more elegant than adding another parameter to
2671 * lock_match. I want a second opinion. */
2672 osc_set_data_with_check(lockh, data);
2673 ldlm_lock_addref(lockh, LCK_PR);
2674 ldlm_lock_decref(lockh, LCK_PW);
2680 static int osc_cancel(struct obd_export *exp, struct lov_stripe_md *md,
2681 __u32 mode, struct lustre_handle *lockh)
2685 if (mode == LCK_GROUP)
2686 ldlm_lock_decref_and_cancel(lockh, mode);
2688 ldlm_lock_decref(lockh, mode);
2693 static int osc_cancel_unused(struct obd_export *exp,
2694 struct lov_stripe_md *lsm,
2695 int flags, void *opaque)
2697 struct obd_device *obd = class_exp2obd(exp);
2698 struct ldlm_res_id res_id = { .name = {0} }, *resp = NULL;
2701 res_id.name[0] = lsm->lsm_object_id;
2702 res_id.name[2] = lsm->lsm_object_gr;
2706 return ldlm_cli_cancel_unused(obd->obd_namespace, resp, flags, opaque);
2709 static int osc_statfs(struct obd_device *obd, struct obd_statfs *osfs,
2710 unsigned long max_age)
2712 struct obd_statfs *msfs;
2713 struct ptlrpc_request *request;
2714 int rc, size = sizeof(*osfs);
2717 /* We could possibly pass max_age in the request (as an absolute
2718 * timestamp or a "seconds.usec ago") so the target can avoid doing
2719 * extra calls into the filesystem if that isn't necessary (e.g.
2720 * during mount that would help a bit). Having relative timestamps
2721 * is not so great if request processing is slow, while absolute
2722 * timestamps are not ideal because they need time synchronization. */
2723 request = ptlrpc_prep_req(obd->u.cli.cl_import, LUSTRE_OBD_VERSION,
2724 OST_STATFS, 0, NULL, NULL);
2728 request->rq_replen = lustre_msg_size(1, &size);
2729 request->rq_request_portal = OST_CREATE_PORTAL; //XXX FIXME bug 249
2731 rc = ptlrpc_queue_wait(request);
2735 msfs = lustre_swab_repbuf(request, 0, sizeof(*msfs),
2736 lustre_swab_obd_statfs);
2738 CERROR("Can't unpack obd_statfs\n");
2739 GOTO(out, rc = -EPROTO);
2742 memcpy(osfs, msfs, sizeof(*osfs));
2746 ptlrpc_req_finished(request);
2750 /* Retrieve object striping information.
2752 * @lmmu is a pointer to an in-core struct with lmm_ost_count indicating
2753 * the maximum number of OST indices which will fit in the user buffer.
2754 * lmm_magic must be LOV_MAGIC (we only use 1 slot here).
2756 static int osc_getstripe(struct lov_stripe_md *lsm, struct lov_user_md *lump)
2758 struct lov_user_md lum, *lumk;
2765 rc = copy_from_user(&lum, lump, sizeof(lum));
2769 if (lum.lmm_magic != LOV_USER_MAGIC)
2772 if (lum.lmm_stripe_count > 0) {
2773 lum_size = sizeof(lum) + sizeof(lum.lmm_objects[0]);
2774 OBD_ALLOC(lumk, lum_size);
2778 lumk->lmm_objects[0].l_object_id = lsm->lsm_object_id;
2779 lumk->lmm_objects[0].l_object_gr = lsm->lsm_object_gr;
2781 lum_size = sizeof(lum);
2785 lumk->lmm_object_id = lsm->lsm_object_id;
2786 lumk->lmm_object_gr = lsm->lsm_object_gr;
2787 lumk->lmm_stripe_count = 1;
2789 if (copy_to_user(lump, lumk, lum_size))
2793 OBD_FREE(lumk, lum_size);
2798 static int osc_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
2799 void *karg, void *uarg)
2801 struct obd_device *obd = exp->exp_obd;
2802 struct obd_ioctl_data *data = karg;
2806 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
2809 if (!try_module_get(THIS_MODULE)) {
2810 CERROR("Can't get module. Is it alive?");
2815 case OBD_IOC_LOV_GET_CONFIG: {
2817 struct lov_desc *desc;
2818 struct obd_uuid uuid;
2822 if (obd_ioctl_getdata(&buf, &len, (void *)uarg))
2823 GOTO(out, err = -EINVAL);
2825 data = (struct obd_ioctl_data *)buf;
2827 if (sizeof(*desc) > data->ioc_inllen1) {
2829 GOTO(out, err = -EINVAL);
2832 if (data->ioc_inllen2 < sizeof(uuid)) {
2834 GOTO(out, err = -EINVAL);
2837 if (data->ioc_inllen3 < sizeof(__u32)) {
2839 GOTO(out, err = -EINVAL);
2842 desc = (struct lov_desc *)data->ioc_inlbuf1;
2843 desc->ld_tgt_count = 1;
2844 desc->ld_active_tgt_count = 1;
2845 desc->ld_default_stripe_count = 1;
2846 desc->ld_default_stripe_size = 0;
2847 desc->ld_default_stripe_offset = 0;
2848 desc->ld_pattern = 0;
2849 memcpy(&desc->ld_uuid, &obd->obd_uuid, sizeof(uuid));
2850 memcpy(data->ioc_inlbuf2, &obd->obd_uuid, sizeof(uuid));
2851 *((__u32 *)data->ioc_inlbuf3) = 1;
2853 err = copy_to_user((void *)uarg, buf, len);
2856 obd_ioctl_freedata(buf, len);
2859 case LL_IOC_LOV_SETSTRIPE:
2860 err = obd_alloc_memmd(exp, karg);
2864 case LL_IOC_LOV_GETSTRIPE:
2865 err = osc_getstripe(karg, uarg);
2867 case OBD_IOC_CLIENT_RECOVER:
2868 err = ptlrpc_recover_import(obd->u.cli.cl_import,
2873 case IOC_OSC_SET_ACTIVE:
2874 err = ptlrpc_set_import_active(obd->u.cli.cl_import,
2877 case IOC_OSC_CTL_RECOVERY:
2878 err = ptlrpc_import_control_recovery(obd->u.cli.cl_import,
2882 CDEBUG(D_INODE, "unrecognised ioctl %#x by %s\n", cmd, current->comm);
2883 GOTO(out, err = -ENOTTY);
2886 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
2889 module_put(THIS_MODULE);
2894 static int osc_get_info(struct obd_export *exp, __u32 keylen,
2895 void *key, __u32 *vallen, void *val)
2898 if (!vallen || !val)
2901 if (keylen > strlen("lock_to_stripe") &&
2902 strcmp(key, "lock_to_stripe") == 0) {
2903 __u32 *stripe = val;
2904 *vallen = sizeof(*stripe);
2907 } else if (keylen >= strlen("last_id") && strcmp(key, "last_id") == 0) {
2908 struct ptlrpc_request *req;
2910 char *bufs[1] = {key};
2912 req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_OBD_VERSION,
2913 OST_GET_INFO, 1, (int *)&keylen, bufs);
2917 req->rq_replen = lustre_msg_size(1, (int *)vallen);
2918 rc = ptlrpc_queue_wait(req);
2922 reply = lustre_swab_repbuf(req, 0, sizeof(*reply),
2923 lustre_swab_ost_last_id);
2924 if (reply == NULL) {
2925 CERROR("Can't unpack OST last ID\n");
2926 GOTO(out, rc = -EPROTO);
2928 *((obd_id *)val) = *reply;
2930 ptlrpc_req_finished(req);
2932 } else if (keylen >= strlen("client_nid") &&
2933 strcmp(key, "client_nid") == 0) {
2934 struct ptlrpc_connection * conn;
2935 ptl_nid_t * nid = val;
2936 *vallen = sizeof(*nid);
2938 conn = class_exp2cliimp(exp)->imp_connection;
2942 nid = &conn->c_peer.peer_id.nid;
2949 static int osc_set_info(struct obd_export *exp, obd_count keylen,
2950 void *key, obd_count vallen, void *val)
2952 struct obd_device *obd = exp->exp_obd;
2953 struct obd_import *imp = class_exp2cliimp(exp);
2954 struct llog_ctxt *ctxt;
2958 if (keylen == strlen("unlinked") &&
2959 memcmp(key, "unlinked", keylen) == 0) {
2960 struct osc_creator *oscc = &obd->u.cli.cl_oscc;
2961 spin_lock(&oscc->oscc_lock);
2962 oscc->oscc_flags &= ~OSCC_FLAG_NOSPC;
2963 spin_unlock(&oscc->oscc_lock);
2967 if (keylen == strlen("unrecovery") &&
2968 memcmp(key, "unrecovery", keylen) == 0) {
2969 struct osc_creator *oscc = &obd->u.cli.cl_oscc;
2970 spin_lock(&oscc->oscc_lock);
2971 oscc->oscc_flags &= ~OSCC_FLAG_RECOVERING;
2972 spin_unlock(&oscc->oscc_lock);
2976 if (keylen == strlen("initial_recov") &&
2977 memcmp(key, "initial_recov", strlen("initial_recov")) == 0) {
2978 struct obd_import *imp = class_exp2cliimp(exp);
2979 if (vallen != sizeof(int))
2981 imp->imp_initial_recov = *(int *)val;
2982 CDEBUG(D_HA, "%s: set imp_no_init_recov = %d\n",
2983 exp->exp_obd->obd_name,
2984 imp->imp_initial_recov);
2988 if (keylen == strlen("async") &&
2989 memcmp(key, "async", keylen) == 0) {
2990 struct client_obd *cl = &obd->u.cli;
2991 if (vallen != sizeof(int))
2993 cl->cl_async = *(int *)val;
2994 CDEBUG(D_HA, "%s: set async = %d\n",
2995 obd->obd_name, cl->cl_async);
2999 if (keylen == 5 && strcmp(key, "audit") == 0) {
3000 struct ptlrpc_request *req;
3001 char *bufs[2] = {key, val};
3002 int size[2] = {keylen, vallen};
3004 req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_OBD_VERSION,
3005 OST_SET_INFO, 2, size, bufs);
3009 req->rq_replen = lustre_msg_size(0, size);
3010 lustre_swab_reqbuf(req, 1, sizeof(struct audit_attr_msg),
3011 lustre_swab_audit_attr);
3012 rc = ptlrpc_queue_wait(req);
3014 ptlrpc_req_finished(req);
3018 if (keylen == 9 && strcmp(key, "audit_obj") == 0) {
3019 struct ptlrpc_request *req;
3020 char *bufs[2] = {key, val};
3021 int size[2] = {keylen, vallen};
3023 req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_OBD_VERSION,
3024 OST_SET_INFO, 2, size, bufs);
3028 req->rq_replen = lustre_msg_size(0, size);
3029 lustre_swab_reqbuf(req, 1, sizeof(struct obdo),
3031 rc = ptlrpc_queue_wait(req);
3033 ptlrpc_req_finished(req);
3037 if (keylen == 8 && memcmp(key, "auditlog", 8) == 0) {
3038 struct ptlrpc_request *req;
3039 char *bufs[2] = {key, val};
3040 int size[2] = {keylen, vallen};
3042 req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_OBD_VERSION,
3043 OST_SET_INFO, 2, size, bufs);
3047 req->rq_replen = lustre_msg_size(0, size);
3048 lustre_swab_reqbuf(req, 1, sizeof(struct audit_msg),
3049 lustre_swab_audit_msg);
3050 rc = ptlrpc_queue_wait(req);
3052 ptlrpc_req_finished(req);
3056 if (keylen == strlen("sec") && memcmp(key, "sec", keylen) == 0) {
3057 struct client_obd *cli = &exp->exp_obd->u.cli;
3059 cli->cl_sec_flavor = ptlrpcs_name2flavor(val);
3060 if (cli->cl_sec_flavor == PTLRPCS_FLVR_INVALID) {
3061 CERROR("unrecognized security flavor %s\n", (char*) val);
3068 if (keylen == strlen("sec_flags") &&
3069 memcmp(key, "sec_flags", keylen) == 0) {
3070 struct client_obd *cli = &exp->exp_obd->u.cli;
3072 cli->cl_sec_flags = *((unsigned long *) val);
3076 if (keylen == strlen("flush_cred") &&
3077 memcmp(key, "flush_cred", keylen) == 0) {
3078 struct client_obd *cli = &exp->exp_obd->u.cli;
3081 ptlrpcs_import_flush_current_creds(cli->cl_import);
3084 if (keylen == strlen("crypto_cb") &&
3085 memcmp(key, "crypto_cb", keylen) == 0) {
3086 LASSERT(vallen == sizeof(crypt_cb_t));
3087 osc_crypt_cb = (crypt_cb_t)val;
3091 if (keylen == 8 && memcmp(key, "capa_key", 8) == 0) {
3092 struct ptlrpc_request *req;
3093 char *bufs[2] = {key, val};
3094 unsigned long irqflags;
3095 int rc, size[2] = {keylen, vallen};
3097 LASSERT(vallen == sizeof(struct lustre_capa_key));
3099 req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_OBD_VERSION,
3100 OST_SET_INFO, 2, size, bufs);
3104 spin_lock_irqsave (&req->rq_lock, irqflags);
3106 spin_unlock_irqrestore (&req->rq_lock, irqflags);
3108 req->rq_replen = lustre_msg_size(0, NULL);
3109 rc = ptlrpc_queue_wait(req);
3110 ptlrpc_req_finished(req);
3114 if (keylen == strlen("setext") &&
3115 memcmp(key, "setext", keylen) == 0) {
3116 struct client_obd *cli = &exp->exp_obd->u.cli;
3117 struct osc_creator *oscc = &cli->cl_oscc;
3118 struct fid_extent *ext = val;
3120 oscc->oscc_next_id = (obd_id)ext->fe_start;
3124 if (keylen < strlen("mds_conn") ||
3125 memcmp(key, "mds_conn", keylen) != 0)
3128 ctxt = llog_get_context(&exp->exp_obd->obd_llogs,
3129 LLOG_UNLINK_ORIG_CTXT);
3132 rc = llog_initiator_connect(ctxt);
3134 CERROR("cannot establish the connect for "
3135 "ctxt %p: %d\n", ctxt, rc);
3138 imp->imp_server_timeout = 1;
3139 CDEBUG(D_HA, "pinging OST %s\n", imp->imp_target_uuid.uuid);
3140 imp->imp_pingable = 1;
3146 static struct llog_operations osc_size_repl_logops = {
3147 lop_cancel: llog_obd_repl_cancel
3150 static struct llog_operations osc_unlink_orig_logops;
3152 static int osc_llog_init(struct obd_device *obd, struct obd_llogs *llogs,
3153 struct obd_device *tgt, int count,
3154 struct llog_catid *catid)
3159 osc_unlink_orig_logops = llog_lvfs_ops;
3160 osc_unlink_orig_logops.lop_setup = llog_obd_origin_setup;
3161 osc_unlink_orig_logops.lop_cleanup = llog_catalog_cleanup;
3162 osc_unlink_orig_logops.lop_add = llog_catalog_add;
3163 osc_unlink_orig_logops.lop_connect = llog_origin_connect;
3165 rc = obd_llog_setup(obd, llogs, LLOG_UNLINK_ORIG_CTXT, tgt, count,
3166 &catid->lci_logid, &osc_unlink_orig_logops);
3170 rc = obd_llog_setup(obd, llogs, LLOG_SIZE_REPL_CTXT, tgt, count, NULL,
3171 &osc_size_repl_logops);
3175 static int osc_llog_finish(struct obd_device *obd,
3176 struct obd_llogs *llogs, int count)
3181 rc = obd_llog_cleanup(llog_get_context(llogs, LLOG_UNLINK_ORIG_CTXT));
3185 rc = obd_llog_cleanup(llog_get_context(llogs, LLOG_SIZE_REPL_CTXT));
3189 static int osc_connect(struct lustre_handle *exph,
3190 struct obd_device *obd, struct obd_uuid *cluuid,
3191 struct obd_connect_data *data,
3192 unsigned long connect_flags)
3196 rc = client_connect_import(exph, obd, cluuid, data, connect_flags);
3200 static int osc_disconnect(struct obd_export *exp, unsigned long flags)
3202 struct obd_device *obd = class_exp2obd(exp);
3203 struct llog_ctxt *ctxt;
3207 ctxt = llog_get_context(&obd->obd_llogs, LLOG_SIZE_REPL_CTXT);
3208 if (obd->u.cli.cl_conn_count == 1)
3209 /* flush any remaining cancel messages out to the target */
3210 llog_sync(ctxt, exp);
3212 rc = client_disconnect_export(exp, flags);
3216 static int osc_import_event(struct obd_device *obd,
3217 struct obd_import *imp,
3218 enum obd_import_event event)
3220 struct client_obd *cli;
3223 LASSERT(imp->imp_obd == obd);
3226 case IMP_EVENT_DISCON: {
3227 /* Only do this on the MDS OSC's */
3228 if (imp->imp_server_timeout) {
3229 struct osc_creator *oscc = &obd->u.cli.cl_oscc;
3231 spin_lock(&oscc->oscc_lock);
3232 oscc->oscc_flags |= OSCC_FLAG_RECOVERING;
3233 spin_unlock(&oscc->oscc_lock);
3237 case IMP_EVENT_INACTIVE: {
3238 if (obd->obd_observer)
3239 rc = obd_notify(obd->obd_observer, obd, 0, 0);
3242 case IMP_EVENT_INVALIDATE: {
3243 struct ldlm_namespace *ns = obd->obd_namespace;
3247 spin_lock(&cli->cl_loi_list_lock);
3248 cli->cl_avail_grant = 0;
3249 cli->cl_lost_grant = 0;
3250 /* all pages go to failing rpcs due to the invalid import */
3251 osc_check_rpcs(cli);
3252 spin_unlock(&cli->cl_loi_list_lock);
3254 ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
3258 case IMP_EVENT_ACTIVE: {
3259 /* Only do this on the MDS OSC's */
3260 if (imp->imp_server_timeout) {
3261 struct osc_creator *oscc = &obd->u.cli.cl_oscc;
3263 spin_lock(&oscc->oscc_lock);
3264 oscc->oscc_flags &= ~OSCC_FLAG_NOSPC;
3265 spin_unlock(&oscc->oscc_lock);
3268 if (obd->obd_observer)
3269 rc = obd_notify(obd->obd_observer, obd, 1, 0);
3273 CERROR("Unknown import event %d\n", event);
3279 static int osc_attach(struct obd_device *dev, obd_count len, void *data)
3281 struct lprocfs_static_vars lvars;
3285 lprocfs_init_vars(osc,&lvars);
3286 rc = lprocfs_obd_attach(dev, lvars.obd_vars);
3290 rc = lproc_osc_attach_seqstat(dev);
3292 lprocfs_obd_detach(dev);
3296 ptlrpc_lprocfs_register_obd(dev);
3300 static int osc_detach(struct obd_device *dev)
3302 ptlrpc_lprocfs_unregister_obd(dev);
3303 return lprocfs_obd_detach(dev);
3306 static int osc_setup(struct obd_device *obd, obd_count len, void *buf)
3310 rc = ptlrpcd_addref();
3314 rc = client_obd_setup(obd, len, buf);
3323 static int osc_cleanup(struct obd_device *obd, int flags)
3325 struct osc_creator *oscc = &obd->u.cli.cl_oscc;
3328 rc = ldlm_cli_cancel_unused(obd->obd_namespace, NULL,
3329 LDLM_FL_CONFIG_CHANGE, NULL);
3333 spin_lock(&oscc->oscc_lock);
3334 oscc->oscc_flags &= ~OSCC_FLAG_RECOVERING;
3335 oscc->oscc_flags |= OSCC_FLAG_EXITING;
3336 spin_unlock(&oscc->oscc_lock);
3338 rc = client_obd_cleanup(obd, flags);
3344 struct obd_ops osc_obd_ops = {
3345 .o_owner = THIS_MODULE,
3346 .o_attach = osc_attach,
3347 .o_detach = osc_detach,
3348 .o_setup = osc_setup,
3349 .o_cleanup = osc_cleanup,
3350 .o_add_conn = client_import_add_conn,
3351 .o_del_conn = client_import_del_conn,
3352 .o_connect = osc_connect,
3353 .o_disconnect = osc_disconnect,
3354 .o_statfs = osc_statfs,
3355 .o_packmd = osc_packmd,
3356 .o_unpackmd = osc_unpackmd,
3357 .o_create = osc_create,
3358 .o_destroy = osc_destroy,
3359 .o_getattr = osc_getattr,
3360 .o_getattr_async = osc_getattr_async,
3361 .o_setattr = osc_setattr,
3363 .o_brw_async = osc_brw_async,
3364 .o_prep_async_page = osc_prep_async_page,
3365 .o_queue_async_io = osc_queue_async_io,
3366 .o_set_async_flags = osc_set_async_flags,
3367 .o_queue_group_io = osc_queue_group_io,
3368 .o_trigger_group_io = osc_trigger_group_io,
3369 .o_teardown_async_page = osc_teardown_async_page,
3370 .o_punch = osc_punch,
3372 .o_enqueue = osc_enqueue,
3373 .o_match = osc_match,
3374 .o_change_cbdata = osc_change_cbdata,
3375 .o_cancel = osc_cancel,
3376 .o_cancel_unused = osc_cancel_unused,
3377 .o_iocontrol = osc_iocontrol,
3378 .o_get_info = osc_get_info,
3379 .o_set_info = osc_set_info,
3380 .o_import_event = osc_import_event,
3381 .o_llog_init = osc_llog_init,
3382 .o_llog_finish = osc_llog_finish,
3385 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
3386 struct obd_ops sanosc_obd_ops = {
3387 .o_owner = THIS_MODULE,
3388 .o_attach = osc_attach,
3389 .o_detach = osc_detach,
3390 .o_cleanup = client_obd_cleanup,
3391 .o_add_conn = client_import_add_conn,
3392 .o_del_conn = client_import_del_conn,
3393 .o_connect = osc_connect,
3394 .o_disconnect = client_disconnect_export,
3395 .o_statfs = osc_statfs,
3396 .o_packmd = osc_packmd,
3397 .o_unpackmd = osc_unpackmd,
3398 .o_create = osc_real_create,
3399 .o_destroy = osc_destroy,
3400 .o_getattr = osc_getattr,
3401 .o_getattr_async = osc_getattr_async,
3402 .o_setattr = osc_setattr,
3403 .o_setup = client_sanobd_setup,
3404 .o_brw = sanosc_brw,
3405 .o_punch = osc_punch,
3407 .o_enqueue = osc_enqueue,
3408 .o_match = osc_match,
3409 .o_change_cbdata = osc_change_cbdata,
3410 .o_cancel = osc_cancel,
3411 .o_cancel_unused = osc_cancel_unused,
3412 .o_iocontrol = osc_iocontrol,
3413 .o_import_event = osc_import_event,
3414 .o_llog_init = osc_llog_init,
3415 .o_llog_finish = osc_llog_finish,
3419 int __init osc_init(void)
3421 struct lprocfs_static_vars lvars;
3422 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
3423 struct lprocfs_static_vars sanlvars;
3428 lprocfs_init_vars(osc, &lvars);
3429 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
3430 lprocfs_init_vars(osc, &sanlvars);
3433 rc = class_register_type(&osc_obd_ops, NULL, lvars.module_vars,
3434 OBD_OSC_DEVICENAME);
3438 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
3439 rc = class_register_type(&sanosc_obd_ops, NULL, sanlvars.module_vars,
3440 OBD_SANOSC_DEVICENAME);
3442 class_unregister_type(OBD_OSC_DEVICENAME);
3449 static void /*__exit*/ osc_exit(void)
3451 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
3452 class_unregister_type(OBD_SANOSC_DEVICENAME);
3454 class_unregister_type(OBD_OSC_DEVICENAME);
3457 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
3458 MODULE_DESCRIPTION("Lustre Object Storage Client (OSC)");
3459 MODULE_LICENSE("GPL");
3461 module_init(osc_init);
3462 module_exit(osc_exit);