Whamcloud - gitweb
r=zab,phil
[fs/lustre-release.git] / lustre / osc / osc_request.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (C) 2001-2003 Cluster File Systems, Inc.
5  *   Author Peter Braam <braam@clusterfs.com>
6  *
7  *   This file is part of Lustre, http://www.lustre.org.
8  *
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.
12  *
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.
17  *
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.
21  *
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.)
26  *
27  */
28
29 #ifndef EXPORT_SYMTAB
30 # define EXPORT_SYMTAB
31 #endif
32 #define DEBUG_SUBSYSTEM S_OSC
33
34 #ifdef __KERNEL__
35 # include <linux/version.h>
36 # include <linux/module.h>
37 # include <linux/mm.h>
38 # include <linux/highmem.h>
39 # include <linux/lustre_dlm.h>
40 # if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
41 #  include <linux/workqueue.h>
42 #  include <linux/smp_lock.h>
43 # else
44 #  include <linux/locks.h>
45 # endif
46 #else /* __KERNEL__ */
47 # include <liblustre.h>
48 #endif
49
50 #include <linux/kp30.h>
51 #include <linux/lustre_net.h>
52 #include <linux/lustre_user.h>
53 #include <linux/obd_ost.h>
54 #include <linux/obd_lov.h>
55
56 #ifndef  __CYGWIN__
57 # include <linux/ctype.h>
58 # include <linux/init.h>
59 #else
60 # include <ctype.h>
61 #endif
62
63 #include <linux/lustre_ha.h>
64 #include <linux/lprocfs_status.h>
65 #include <linux/lustre_log.h>
66 #include "osc_internal.h"
67
68
69 static int osc_attach(struct obd_device *dev, obd_count len, void *data)
70 {
71         struct lprocfs_static_vars lvars;
72         int rc;
73         ENTRY;
74
75         lprocfs_init_vars(osc,&lvars);
76         rc = lprocfs_obd_attach(dev, lvars.obd_vars);
77         if (rc < 0)
78                 RETURN(rc);
79
80         rc = lproc_osc_attach_seqstat(dev);
81         if (rc < 0) {
82                 lprocfs_obd_detach(dev);
83                 RETURN(rc);
84         }
85
86         ptlrpc_lprocfs_register_obd(dev);
87         RETURN(0);
88 }
89
90 static int osc_detach(struct obd_device *dev)
91 {
92         ptlrpc_lprocfs_unregister_obd(dev);
93         return lprocfs_obd_detach(dev);
94 }
95
96
97 /* Pack OSC object metadata for disk storage (LE byte order). */
98 static int osc_packmd(struct obd_export *exp, struct lov_mds_md **lmmp,
99                       struct lov_stripe_md *lsm)
100 {
101         int lmm_size;
102         ENTRY;
103
104         lmm_size = sizeof(**lmmp);
105         if (!lmmp)
106                 RETURN(lmm_size);
107
108         if (*lmmp && !lsm) {
109                 OBD_FREE(*lmmp, lmm_size);
110                 *lmmp = NULL;
111                 RETURN(0);
112         }
113
114         if (!*lmmp) {
115                 OBD_ALLOC(*lmmp, lmm_size);
116                 if (!*lmmp)
117                         RETURN(-ENOMEM);
118         }
119
120         if (lsm) {
121                 LASSERT(lsm->lsm_object_id);
122                 (*lmmp)->lmm_object_id = cpu_to_le64(lsm->lsm_object_id);
123         }
124
125         RETURN(lmm_size);
126 }
127
128 /* Unpack OSC object metadata from disk storage (LE byte order). */
129 static int osc_unpackmd(struct obd_export *exp, struct lov_stripe_md **lsmp,
130                         struct lov_mds_md *lmm, int lmm_bytes)
131 {
132         int lsm_size;
133         ENTRY;
134
135         if (lmm != NULL) {
136                 if (lmm_bytes < sizeof (*lmm)) {
137                         CERROR("lov_mds_md too small: %d, need %d\n",
138                                lmm_bytes, (int)sizeof(*lmm));
139                         RETURN(-EINVAL);
140                 }
141                 /* XXX LOV_MAGIC etc check? */
142
143                 if (lmm->lmm_object_id == 0) {
144                         CERROR("lov_mds_md: zero lmm_object_id\n");
145                         RETURN(-EINVAL);
146                 }
147         }
148
149         lsm_size = lov_stripe_md_size(1);
150         if (lsmp == NULL)
151                 RETURN(lsm_size);
152
153         if (*lsmp != NULL && lmm == NULL) {
154                 OBD_FREE(*lsmp, lsm_size);
155                 *lsmp = NULL;
156                 RETURN(0);
157         }
158
159         if (*lsmp == NULL) {
160                 OBD_ALLOC(*lsmp, lsm_size);
161                 if (*lsmp == NULL)
162                         RETURN(-ENOMEM);
163                 loi_init((*lsmp)->lsm_oinfo);
164         }
165
166         if (lmm != NULL) {
167                 /* XXX zero *lsmp? */
168                 (*lsmp)->lsm_object_id = le64_to_cpu (lmm->lmm_object_id);
169                 LASSERT((*lsmp)->lsm_object_id);
170         }
171
172         (*lsmp)->lsm_maxbytes = LUSTRE_STRIPE_MAXBYTES;
173
174         RETURN(lsm_size);
175 }
176
177 static int osc_getattr_interpret(struct ptlrpc_request *req,
178                                  struct osc_getattr_async_args *aa, int rc)
179 {
180         struct ost_body *body;
181         ENTRY;
182
183         if (rc != 0)
184                 RETURN(rc);
185
186         body = lustre_swab_repbuf(req, 0, sizeof(*body), lustre_swab_ost_body);
187         if (body) {
188                 CDEBUG(D_INODE, "mode: %o\n", body->oa.o_mode);
189                 memcpy(aa->aa_oa, &body->oa, sizeof(*aa->aa_oa));
190
191                 /* This should really be sent by the OST */
192                 aa->aa_oa->o_blksize = OSC_BRW_MAX_SIZE;
193                 aa->aa_oa->o_valid |= OBD_MD_FLBLKSZ;
194         } else {
195                 CERROR("can't unpack ost_body\n");
196                 rc = -EPROTO;
197                 aa->aa_oa->o_valid = 0;
198         }
199
200         RETURN(rc);
201 }
202
203 static int osc_getattr_async(struct obd_export *exp, struct obdo *oa,
204                              struct lov_stripe_md *md,
205                              struct ptlrpc_request_set *set)
206 {
207         struct ptlrpc_request *request;
208         struct ost_body *body;
209         int size = sizeof(*body);
210         struct osc_getattr_async_args *aa;
211         ENTRY;
212
213         request = ptlrpc_prep_req(class_exp2cliimp(exp), OST_GETATTR, 1,
214                                   &size, NULL);
215         if (!request)
216                 RETURN(-ENOMEM);
217
218         body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof (*body));
219         memcpy(&body->oa, oa, sizeof(*oa));
220
221         request->rq_replen = lustre_msg_size(1, &size);
222         request->rq_interpret_reply = osc_getattr_interpret;
223
224         LASSERT (sizeof (*aa) <= sizeof (request->rq_async_args));
225         aa = (struct osc_getattr_async_args *)&request->rq_async_args;
226         aa->aa_oa = oa;
227
228         ptlrpc_set_add_req (set, request);
229         RETURN (0);
230 }
231
232 static int osc_getattr(struct obd_export *exp, struct obdo *oa,
233                        struct lov_stripe_md *md)
234 {
235         struct ptlrpc_request *request;
236         struct ost_body *body;
237         int rc, size = sizeof(*body);
238         ENTRY;
239
240         request = ptlrpc_prep_req(class_exp2cliimp(exp), OST_GETATTR, 1,
241                                   &size, NULL);
242         if (!request)
243                 RETURN(-ENOMEM);
244
245         body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof (*body));
246         memcpy(&body->oa, oa, sizeof(*oa));
247
248         request->rq_replen = lustre_msg_size(1, &size);
249
250         rc = ptlrpc_queue_wait(request);
251         if (rc) {
252                 CERROR("%s failed: rc = %d\n", __FUNCTION__, rc);
253                 GOTO(out, rc);
254         }
255
256         body = lustre_swab_repbuf(request, 0, sizeof (*body),
257                                   lustre_swab_ost_body);
258         if (body == NULL) {
259                 CERROR ("can't unpack ost_body\n");
260                 GOTO (out, rc = -EPROTO);
261         }
262
263         CDEBUG(D_INODE, "mode: %o\n", body->oa.o_mode);
264         memcpy(oa, &body->oa, sizeof(*oa));
265
266         /* This should really be sent by the OST */
267         oa->o_blksize = OSC_BRW_MAX_SIZE;
268         oa->o_valid |= OBD_MD_FLBLKSZ;
269
270         EXIT;
271  out:
272         ptlrpc_req_finished(request);
273         return rc;
274 }
275
276 static int osc_setattr(struct obd_export *exp, struct obdo *oa,
277                        struct lov_stripe_md *md, struct obd_trans_info *oti)
278 {
279         struct ptlrpc_request *request;
280         struct ost_body *body;
281         int rc, size = sizeof(*body);
282         ENTRY;
283
284         request = ptlrpc_prep_req(class_exp2cliimp(exp), OST_SETATTR, 1, &size,
285                                   NULL);
286         if (!request)
287                 RETURN(-ENOMEM);
288
289         body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof(*body));
290         memcpy(&body->oa, oa, sizeof(*oa));
291
292         request->rq_replen = lustre_msg_size(1, &size);
293
294         rc = ptlrpc_queue_wait(request);
295         if (rc)
296                 GOTO(out, rc);
297
298         body = lustre_swab_repbuf(request, 0, sizeof(*body),
299                                   lustre_swab_ost_body);
300         if (body == NULL)
301                 GOTO(out, rc = -EPROTO);
302
303         memcpy(oa, &body->oa, sizeof(*oa));
304
305         EXIT;
306 out:
307         ptlrpc_req_finished(request);
308         RETURN(0);
309 }
310
311 int osc_real_create(struct obd_export *exp, struct obdo *oa,
312                     struct lov_stripe_md **ea, struct obd_trans_info *oti)
313 {
314         struct ptlrpc_request *request;
315         struct ost_body *body;
316         struct lov_stripe_md *lsm;
317         int rc, size = sizeof(*body);
318         ENTRY;
319
320         LASSERT(oa);
321         LASSERT(ea);
322
323         lsm = *ea;
324         if (!lsm) {
325                 rc = obd_alloc_memmd(exp, &lsm);
326                 if (rc < 0)
327                         RETURN(rc);
328         }
329
330         request = ptlrpc_prep_req(class_exp2cliimp(exp), OST_CREATE, 1, &size,
331                                   NULL);
332         if (!request)
333                 GOTO(out, rc = -ENOMEM);
334
335         body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof (*body));
336         memcpy(&body->oa, oa, sizeof(body->oa));
337
338         request->rq_replen = lustre_msg_size(1, &size);
339         if (oa->o_valid & OBD_MD_FLINLINE) {
340                 LASSERT((oa->o_valid & OBD_MD_FLFLAGS) &&
341                         oa->o_flags == OBD_FL_DELORPHAN);
342                 DEBUG_REQ(D_HA, request,
343                           "delorphan from OST integration; level == RECOVER");
344                 request->rq_send_state = LUSTRE_IMP_RECOVER;
345         }
346
347         rc = ptlrpc_queue_wait(request);
348         if (rc)
349                 GOTO(out_req, rc);
350
351         body = lustre_swab_repbuf(request, 0, sizeof(*body),
352                                   lustre_swab_ost_body);
353         if (body == NULL) {
354                 CERROR ("can't unpack ost_body\n");
355                 GOTO (out_req, rc = -EPROTO);
356         }
357
358         memcpy(oa, &body->oa, sizeof(*oa));
359
360         /* This should really be sent by the OST */
361         oa->o_blksize = OSC_BRW_MAX_SIZE;
362         oa->o_valid |= OBD_MD_FLBLKSZ;
363
364         /* XXX LOV STACKING: the lsm that is passed to us from LOV does not
365          * have valid lsm_oinfo data structs, so don't go touching that.
366          * This needs to be fixed in a big way.
367          */
368         lsm->lsm_object_id = oa->o_id;
369         *ea = lsm;
370
371         if (oti != NULL) {
372                 oti->oti_transno = request->rq_repmsg->transno;
373
374                 if (oa->o_valid & OBD_MD_FLCOOKIE) {
375                         if (!oti->oti_logcookies)
376                                 oti_alloc_cookies(oti, 1);
377                         memcpy(oti->oti_logcookies, obdo_logcookie(oa),
378                                sizeof(oti->oti_onecookie));
379                 }
380         }
381
382         CDEBUG(D_HA, "transno: "LPD64"\n", request->rq_repmsg->transno);
383         EXIT;
384 out_req:
385         ptlrpc_req_finished(request);
386 out:
387         if (rc && !*ea)
388                 obd_free_memmd(exp, &lsm);
389         return rc;
390 }
391
392 static int osc_punch(struct obd_export *exp, struct obdo *oa,
393                      struct lov_stripe_md *md, obd_size start,
394                      obd_size end, struct obd_trans_info *oti)
395 {
396         struct ptlrpc_request *request;
397         struct ost_body *body;
398         int rc, size = sizeof(*body);
399         ENTRY;
400
401         if (!oa) {
402                 CERROR("oa NULL\n");
403                 RETURN(-EINVAL);
404         }
405
406         request = ptlrpc_prep_req(class_exp2cliimp(exp), OST_PUNCH, 1, &size,
407                                   NULL);
408         if (!request)
409                 RETURN(-ENOMEM);
410
411         body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof (*body));
412         memcpy(&body->oa, oa, sizeof(*oa));
413
414         /* overload the size and blocks fields in the oa with start/end */
415         body->oa.o_size = start;
416         body->oa.o_blocks = end;
417         body->oa.o_valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS);
418
419         request->rq_replen = lustre_msg_size(1, &size);
420
421         rc = ptlrpc_queue_wait(request);
422         if (rc)
423                 GOTO(out, rc);
424
425         body = lustre_swab_repbuf (request, 0, sizeof (*body),
426                                    lustre_swab_ost_body);
427         if (body == NULL) {
428                 CERROR ("can't unpack ost_body\n");
429                 GOTO (out, rc = -EPROTO);
430         }
431
432         memcpy(oa, &body->oa, sizeof(*oa));
433
434         EXIT;
435  out:
436         ptlrpc_req_finished(request);
437         return rc;
438 }
439
440 static int osc_sync(struct obd_export *exp, struct obdo *oa,
441                     struct lov_stripe_md *md, obd_size start, obd_size end)
442 {
443         struct ptlrpc_request *request;
444         struct ost_body *body;
445         int rc, size = sizeof(*body);
446         ENTRY;
447
448         if (!oa) {
449                 CERROR("oa NULL\n");
450                 RETURN(-EINVAL);
451         }
452
453         request = ptlrpc_prep_req(class_exp2cliimp(exp), OST_SYNC, 1, &size,
454                                   NULL);
455         if (!request)
456                 RETURN(-ENOMEM);
457
458         body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof (*body));
459         memcpy(&body->oa, oa, sizeof(*oa));
460
461         /* overload the size and blocks fields in the oa with start/end */
462         body->oa.o_size = start;
463         body->oa.o_blocks = end;
464         body->oa.o_valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS);
465
466         request->rq_replen = lustre_msg_size(1, &size);
467
468         rc = ptlrpc_queue_wait(request);
469         if (rc)
470                 GOTO(out, rc);
471
472         body = lustre_swab_repbuf(request, 0, sizeof(*body),
473                                   lustre_swab_ost_body);
474         if (body == NULL) {
475                 CERROR ("can't unpack ost_body\n");
476                 GOTO (out, rc = -EPROTO);
477         }
478
479         memcpy(oa, &body->oa, sizeof(*oa));
480
481         EXIT;
482  out:
483         ptlrpc_req_finished(request);
484         return rc;
485 }
486
487 static int osc_destroy(struct obd_export *exp, struct obdo *oa,
488                        struct lov_stripe_md *ea, struct obd_trans_info *oti)
489 {
490         struct ptlrpc_request *request;
491         struct ost_body *body;
492         int rc, size = sizeof(*body);
493         ENTRY;
494
495         if (!oa) {
496                 CERROR("oa NULL\n");
497                 RETURN(-EINVAL);
498         }
499
500         request = ptlrpc_prep_req(class_exp2cliimp(exp), OST_DESTROY, 1,
501                                   &size, NULL);
502         if (!request)
503                 RETURN(-ENOMEM);
504
505         body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof (*body));
506
507         if (oti != NULL && oa->o_valid & OBD_MD_FLCOOKIE) {
508                 memcpy(obdo_logcookie(oa), oti->oti_logcookies,
509                        sizeof(*oti->oti_logcookies));
510                 oti->oti_logcookies++;
511         }
512
513         memcpy(&body->oa, oa, sizeof(*oa));
514         request->rq_replen = lustre_msg_size(1, &size);
515
516         rc = ptlrpc_queue_wait(request);
517         if (rc)
518                 GOTO(out, rc);
519
520         body = lustre_swab_repbuf(request, 0, sizeof(*body),
521                                   lustre_swab_ost_body);
522         if (body == NULL) {
523                 CERROR ("Can't unpack body\n");
524                 GOTO (out, rc = -EPROTO);
525         }
526
527         memcpy(oa, &body->oa, sizeof(*oa));
528
529         EXIT;
530  out:
531         ptlrpc_req_finished(request);
532         return rc;
533 }
534
535 static void osc_announce_cached(struct client_obd *cli, struct obdo *oa)
536 {
537         obd_flag bits = OBD_MD_FLBLOCKS|OBD_MD_FLGRANT;
538
539         LASSERT(!(oa->o_valid & bits));
540
541         oa->o_valid |= bits;
542         spin_lock(&cli->cl_loi_list_lock);
543         oa->o_blocks = cli->cl_dirty;
544         oa->o_grant = cli->cl_dirty_granted;
545         spin_unlock(&cli->cl_loi_list_lock);
546         CDEBUG(D_INODE, "announcing "LPU64" dirty "LPU64" granted\n",
547                cli->cl_dirty, cli->cl_dirty_granted);
548 }
549
550 static void osc_update_grant(struct client_obd *cli, struct ost_body *body)
551 {
552         if(!(body->oa.o_valid & OBD_MD_FLGRANT)) {
553                 if (cli->cl_ost_can_grant) {
554                         CDEBUG(D_INODE, "%s can't grant\n",
555                                cli->cl_import->imp_target_uuid.uuid);
556                 }
557                 cli->cl_ost_can_grant = 0;
558                 return;
559         }
560
561         CDEBUG(D_SUPER, "got "LPU64" grant\n", body->oa.o_grant);
562         spin_lock(&cli->cl_loi_list_lock);
563         if (cli->cl_dirty_granted != body->oa.o_grant) {
564                 cli->cl_dirty_granted = body->oa.o_grant;
565                 osc_adjust_cache(cli);
566         }
567         spin_unlock(&cli->cl_loi_list_lock);
568 }
569
570 /* We assume that the reason this OSC got a short read is because it read
571  * beyond the end of a stripe file; i.e. lustre is reading a sparse file
572  * via the LOV, and it _knows_ it's reading inside the file, it's just that
573  * this stripe never got written at or beyond this stripe offset yet. */
574 static void handle_short_read(int nob_read, obd_count page_count,
575                               struct brw_page *pga)
576 {
577         char *ptr;
578
579         /* skip bytes read OK */
580         while (nob_read > 0) {
581                 LASSERT (page_count > 0);
582
583                 if (pga->count > nob_read) {
584                         /* EOF inside this page */
585                         ptr = kmap(pga->pg) + (pga->off & ~PAGE_MASK);
586                         memset(ptr + nob_read, 0, pga->count - nob_read);
587                         kunmap(pga->pg);
588                         page_count--;
589                         pga++;
590                         break;
591                 }
592
593                 nob_read -= pga->count;
594                 page_count--;
595                 pga++;
596         }
597
598         /* zero remaining pages */
599         while (page_count-- > 0) {
600                 ptr = kmap(pga->pg) + (pga->off & ~PAGE_MASK);
601                 memset(ptr, 0, pga->count);
602                 kunmap(pga->pg);
603                 pga++;
604         }
605 }
606
607 static int check_write_rcs(struct ptlrpc_request *request, int niocount,
608                            obd_count page_count, struct brw_page *pga)
609 {
610         int    i;
611         int    *remote_rcs;
612
613         /* return error if any niobuf was in error */
614         remote_rcs = lustre_swab_repbuf(request, 1,
615                                         sizeof(*remote_rcs) * niocount, NULL);
616         if (remote_rcs == NULL) {
617                 CERROR ("Missing/short RC vector on BRW_WRITE reply\n");
618                 return (-EPROTO);
619         }
620         if (lustre_msg_swabbed (request->rq_repmsg))
621                 for (i = 0; i < niocount; i++)
622                         __swab32s (&remote_rcs[i]);
623
624         for (i = 0; i < niocount; i++) {
625                 if (remote_rcs[i] < 0)
626                         return (remote_rcs[i]);
627
628                 if (remote_rcs[i] != 0) {
629                         CERROR ("rc[%d] invalid (%d) req %p\n",
630                                 i, remote_rcs[i], request);
631                         return (-EPROTO);
632                 }
633         }
634
635         return (0);
636 }
637
638 static inline int can_merge_pages(struct brw_page *p1, struct brw_page *p2)
639 {
640         if (p1->flag != p2->flag) {
641                 unsigned mask = ~(OBD_BRW_CREATE|OBD_BRW_FROM_GRANT);
642
643                 /* warn if we try to combine flags that we don't know to be
644                  * safe to combine */
645                 if ((p1->flag & mask) != (p2->flag & mask))
646                         CERROR("is it ok to have flags 0x%x and 0x%x in the "
647                                "same brw?\n", p1->flag, p2->flag);
648                 return 0;
649         }
650
651         return (p1->off + p1->count == p2->off);
652 }
653
654 #if CHECKSUM_BULK
655 static obd_count cksum_pages(int nob, obd_count page_count,
656                              struct brw_page *pga)
657 {
658         obd_count cksum = 0;
659         char *ptr;
660
661         while (nob > 0) {
662                 LASSERT (page_count > 0);
663
664                 ptr = kmap(pga->pg);
665                 ost_checksum(&cksum, ptr + (pga->off & (PAGE_SIZE - 1)),
666                              pga->count > nob ? nob : pga->count);
667                 kunmap(pga->pg);
668
669                 nob -= pga->count;
670                 page_count--;
671                 pga++;
672         }
673
674         return (cksum);
675 }
676 #endif
677
678 static int osc_brw_prep_request(int cmd, struct obd_import *imp,struct obdo *oa,
679                                 struct lov_stripe_md *lsm, obd_count page_count,
680                                 struct brw_page *pga, int *requested_nobp,
681                                 int *niocountp, struct ptlrpc_request **reqp)
682 {
683         struct ptlrpc_request   *req;
684         struct ptlrpc_bulk_desc *desc;
685         struct client_obd       *cli = &imp->imp_obd->u.cli;
686         struct ost_body         *body;
687         struct obd_ioobj        *ioobj;
688         struct niobuf_remote    *niobuf;
689         unsigned long            flags;
690         int                      niocount;
691         int                      size[3];
692         int                      i;
693         int                      requested_nob;
694         int                      opc;
695         int                      rc;
696
697         opc = ((cmd & OBD_BRW_WRITE) != 0) ? OST_WRITE : OST_READ;
698
699         for (niocount = i = 1; i < page_count; i++)
700                 if (!can_merge_pages (&pga[i - 1], &pga[i]))
701                         niocount++;
702
703         size[0] = sizeof(*body);
704         size[1] = sizeof(*ioobj);
705         size[2] = niocount * sizeof(*niobuf);
706
707         req = ptlrpc_prep_req(imp, opc, 3, size, NULL);
708         if (req == NULL)
709                 return (-ENOMEM);
710
711         if (opc == OST_WRITE)
712                 desc = ptlrpc_prep_bulk_imp(req, BULK_GET_SOURCE,
713                                             OST_BULK_PORTAL);
714         else
715                 desc = ptlrpc_prep_bulk_imp(req, BULK_PUT_SINK,
716                                             OST_BULK_PORTAL);
717         if (desc == NULL)
718                 GOTO(out, rc = -ENOMEM);
719         /* NB request now owns desc and will free it when it gets freed */
720
721         body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof(*body));
722         ioobj = lustre_msg_buf(req->rq_reqmsg, 1, sizeof(*ioobj));
723         niobuf = lustre_msg_buf(req->rq_reqmsg, 2, niocount * sizeof(*niobuf));
724
725         memcpy(&body->oa, oa, sizeof(*oa));
726
727         obdo_to_ioobj(oa, ioobj);
728         ioobj->ioo_bufcnt = niocount;
729
730         LASSERT (page_count > 0);
731         for (requested_nob = i = 0; i < page_count; i++, niobuf++) {
732                 struct brw_page *pg = &pga[i];
733                 struct brw_page *pg_prev = pg - 1;
734
735                 LASSERT(pg->count > 0);
736                 LASSERT((pg->off & ~PAGE_MASK) + pg->count <= PAGE_SIZE);
737                 LASSERTF(i == 0 || pg->off > pg_prev->off,
738                          "i %d p_c %u pg %p [pri %lu ind %lu] off "LPU64
739                          " prev_pg %p [pri %lu ind %lu] off "LPU64,
740                          i, page_count,
741                          pg->pg, pg->pg->private, pg->pg->index, pg->off,
742                          pg_prev->pg, pg_prev->pg->private, pg_prev->pg->index,
743                                  pg_prev->off);
744
745                 rc = ptlrpc_prep_bulk_page(desc, pg->pg, pg->off & ~PAGE_MASK,
746                                            pg->count);
747                 if (rc != 0)
748                         GOTO(out, rc);
749
750                 requested_nob += pg->count;
751
752                 if (i > 0 && can_merge_pages(pg_prev, pg)) {
753                         niobuf--;
754                         niobuf->len += pg->count;
755                 } else {
756                         niobuf->offset = pg->off;
757                         niobuf->len    = pg->count;
758                         niobuf->flags  = pg->flag;
759                 }
760         }
761
762         LASSERT((void *)(niobuf - niocount) ==
763                 lustre_msg_buf(req->rq_reqmsg, 2, niocount * sizeof(*niobuf)));
764         osc_announce_cached(cli, &body->oa);
765         spin_lock_irqsave(&req->rq_lock, flags);
766         req->rq_no_resend = 1;
767         spin_unlock_irqrestore(&req->rq_lock, flags);
768
769         /* size[0] still sizeof (*body) */
770         if (opc == OST_WRITE) {
771 #if CHECKSUM_BULK
772                 body->oa.o_valid |= OBD_MD_FLCKSUM;
773                 body->oa.o_nlink = cksum_pages(requested_nob, page_count, pga);
774 #endif
775                 /* 1 RC per niobuf */
776                 size[1] = sizeof(__u32) * niocount;
777                 req->rq_replen = lustre_msg_size(2, size);
778         } else {
779                 /* 1 RC for the whole I/O */
780                 req->rq_replen = lustre_msg_size(1, size);
781         }
782
783         *niocountp = niocount;
784         *requested_nobp = requested_nob;
785         *reqp = req;
786         return (0);
787
788  out:
789         ptlrpc_req_finished (req);
790         return (rc);
791 }
792
793 static int osc_brw_fini_request(struct ptlrpc_request *req, struct obdo *oa,
794                                 int requested_nob, int niocount,
795                                 obd_count page_count, struct brw_page *pga,
796                                 int rc)
797 {
798         struct client_obd *cli = &req->rq_import->imp_obd->u.cli;
799         struct ost_body *body;
800
801         if (rc < 0)
802                 return (rc);
803
804         body = lustre_swab_repbuf(req, 0, sizeof(*body), lustre_swab_ost_body);
805         if (body == NULL) {
806                 CERROR ("Can't unpack body\n");
807                 return (-EPROTO);
808         }
809
810         osc_update_grant(cli, body);
811
812         if (req->rq_reqmsg->opc == OST_WRITE) {
813                 if (rc > 0) {
814                         CERROR ("Unexpected +ve rc %d\n", rc);
815                         return (-EPROTO);
816                 }
817
818                 return(check_write_rcs(req, niocount, page_count, pga));
819         }
820
821         if (rc > requested_nob) {
822                 CERROR("Unexpected rc %d (%d requested)\n", rc, requested_nob);
823                 return (-EPROTO);
824         }
825
826         if (rc < requested_nob)
827                 handle_short_read(rc, page_count, pga);
828
829         memcpy(oa, &body->oa, sizeof(*oa));
830
831 #if CHECKSUM_BULK
832         if (oa->o_valid & OBD_MD_FLCKSUM) {
833                 const struct ptlrpc_peer *peer =
834                         &req->rq_import->imp_connection->c_peer;
835                 static int cksum_counter;
836                 obd_count server_cksum = oa->o_nlink;
837                 obd_count cksum = cksum_pages(rc, page_count, pga);
838                 char str[PTL_NALFMT_SIZE];
839
840                 portals_nid2str(peer->peer_ni->pni_number, peer->peer_nid, str);
841
842                 cksum_counter++;
843                 if (server_cksum != cksum) {
844                         CERROR("Bad checksum: server %x, client %x, server NID "
845                                LPX64" (%s)\n", server_cksum, cksum,
846                                peer->peer_nid, str);
847                         cksum_counter = 0;
848                         oa->o_nlink = cksum;
849                 } else if ((cksum_counter & (-cksum_counter)) == cksum_counter){
850                         CWARN("Checksum %u from "LPX64" (%s) OK: %x\n",
851                               cksum_counter, peer->peer_nid, str, cksum);
852                 }
853         } else {
854                 static int cksum_missed;
855
856                 cksum_missed++;
857                 if ((cksum_missed & (-cksum_missed)) == cksum_missed)
858                         CERROR("Request checksum %u from "LPX64", no reply\n",
859                                cksum_missed,
860                                req->rq_import->imp_connection->c_peer.peer_nid);
861         }
862 #endif
863         return (0);
864 }
865
866 static int osc_brw_internal(int cmd, struct obd_export *exp,struct obdo *oa,
867                             struct lov_stripe_md *lsm,
868                             obd_count page_count, struct brw_page *pga)
869 {
870         int                    requested_nob;
871         int                    niocount;
872         struct ptlrpc_request *request;
873         int                    rc;
874         ENTRY;
875
876 restart_bulk:
877         rc = osc_brw_prep_request(cmd, class_exp2cliimp(exp), oa, lsm,
878                                   page_count, pga, &requested_nob, &niocount,
879                                   &request);
880         /* NB ^ sets rq_no_resend */
881
882         if (rc != 0)
883                 return (rc);
884
885         rc = ptlrpc_queue_wait(request);
886
887         if (rc == -ETIMEDOUT && request->rq_resend) {
888                 DEBUG_REQ(D_HA, request,  "BULK TIMEOUT");
889                 ptlrpc_req_finished(request);
890                 goto restart_bulk;
891         }
892
893         rc = osc_brw_fini_request(request, oa, requested_nob, niocount,
894                                   page_count, pga, rc);
895
896         ptlrpc_req_finished(request);
897         RETURN (rc);
898 }
899
900 static int brw_interpret(struct ptlrpc_request *request,
901                          struct osc_brw_async_args *aa, int rc)
902 {
903         struct obdo *oa      = aa->aa_oa;
904         int requested_nob    = aa->aa_requested_nob;
905         int niocount         = aa->aa_nio_count;
906         obd_count page_count = aa->aa_page_count;
907         struct brw_page *pga = aa->aa_pga;
908         ENTRY;
909
910         /* XXX bug 937 here */
911         if (rc == -ETIMEDOUT && request->rq_resend) {
912                 DEBUG_REQ(D_HA, request,  "BULK TIMEOUT");
913                 LBUG(); /* re-send.  later. */
914                 //goto restart_bulk;
915         }
916
917         rc = osc_brw_fini_request(request, oa, requested_nob, niocount,
918                                   page_count, pga, rc);
919         RETURN (rc);
920 }
921
922 static int async_internal(int cmd, struct obd_export *exp, struct obdo *oa,
923                           struct lov_stripe_md *lsm, obd_count page_count,
924                           struct brw_page *pga, struct ptlrpc_request_set *set)
925 {
926         struct ptlrpc_request     *request;
927         int                        requested_nob;
928         int                        nio_count;
929         struct osc_brw_async_args *aa;
930         int                        rc;
931         ENTRY;
932
933         rc = osc_brw_prep_request(cmd, class_exp2cliimp(exp), oa, lsm,
934                                   page_count, pga, &requested_nob, &nio_count,
935                                   &request);
936         /* NB ^ sets rq_no_resend */
937
938         if (rc == 0) {
939                 LASSERT(sizeof(*aa) <= sizeof(request->rq_async_args));
940                 aa = (struct osc_brw_async_args *)&request->rq_async_args;
941                 aa->aa_oa = oa;
942                 aa->aa_requested_nob = requested_nob;
943                 aa->aa_nio_count = nio_count;
944                 aa->aa_page_count = page_count;
945                 aa->aa_pga = pga;
946
947                 request->rq_interpret_reply = brw_interpret;
948                 ptlrpc_set_add_req(set, request);
949         }
950         RETURN (rc);
951 }
952
953 #ifndef min_t
954 #define min_t(type,x,y) \
955         ({ type __x = (x); type __y = (y); __x < __y ? __x: __y; })
956 #endif
957
958 /*
959  * ugh, we want disk allocation on the target to happen in offset order.  we'll
960  * follow sedgewicks advice and stick to the dead simple shellsort -- it'll do
961  * fine for our small page arrays and doesn't require allocation.  its an
962  * insertion sort that swaps elements that are strides apart, shrinking the
963  * stride down until its '1' and the array is sorted.
964  */
965 static void sort_brw_pages(struct brw_page *array, int num)
966 {
967         int stride, i, j;
968         struct brw_page tmp;
969
970         if (num == 1)
971                 return;
972         for (stride = 1; stride < num ; stride = (stride * 3) + 1)
973                 ;
974
975         do {
976                 stride /= 3;
977                 for (i = stride ; i < num ; i++) {
978                         tmp = array[i];
979                         j = i;
980                         while (j >= stride && array[j - stride].off > tmp.off) {
981                                 array[j] = array[j - stride];
982                                 j -= stride;
983                         }
984                         array[j] = tmp;
985                 }
986         } while (stride > 1);
987 }
988
989 /* make sure we the regions we're passing to elan don't violate its '4
990  * fragments' constraint.  portal headers are a fragment, all full
991  * PAGE_SIZE long pages count as 1 fragment, and each partial page
992  * counts as a fragment.  I think.  see bug 934. */
993 static obd_count check_elan_limit(struct brw_page *pg, obd_count pages)
994 {
995         int frags_left = 3;
996         int saw_whole_frag = 0;
997         int i;
998
999         for (i = 0 ; frags_left && i < pages ; pg++, i++) {
1000                 if (pg->count == PAGE_SIZE) {
1001                         if (!saw_whole_frag) {
1002                                 saw_whole_frag = 1;
1003                                 frags_left--;
1004                         }
1005                 } else {
1006                         frags_left--;
1007                 }
1008         }
1009         return i;
1010 }
1011
1012 static int osc_brw(int cmd, struct obd_export *exp, struct obdo *oa,
1013                    struct lov_stripe_md *md, obd_count page_count,
1014                    struct brw_page *pga, struct obd_trans_info *oti)
1015 {
1016         ENTRY;
1017
1018         if (cmd == OBD_BRW_CHECK) {
1019                 /* The caller just wants to know if there's a chance that this
1020                  * I/O can succeed */
1021                 struct obd_import *imp = class_exp2cliimp(exp);
1022
1023                 if (imp == NULL || imp->imp_invalid)
1024                         RETURN(-EIO);
1025                 RETURN(0);
1026         }
1027
1028         while (page_count) {
1029                 obd_count pages_per_brw;
1030                 int rc;
1031
1032                 if (page_count > OSC_BRW_MAX_IOV)
1033                         pages_per_brw = OSC_BRW_MAX_IOV;
1034                 else
1035                         pages_per_brw = page_count;
1036
1037                 sort_brw_pages(pga, pages_per_brw);
1038                 pages_per_brw = check_elan_limit(pga, pages_per_brw);
1039
1040                 rc = osc_brw_internal(cmd, exp, oa, md, pages_per_brw, pga);
1041
1042                 if (rc != 0)
1043                         RETURN(rc);
1044
1045                 page_count -= pages_per_brw;
1046                 pga += pages_per_brw;
1047         }
1048         RETURN(0);
1049 }
1050
1051 static int osc_brw_async(int cmd, struct obd_export *exp, struct obdo *oa,
1052                          struct lov_stripe_md *md, obd_count page_count,
1053                          struct brw_page *pga, struct ptlrpc_request_set *set,
1054                          struct obd_trans_info *oti)
1055 {
1056         ENTRY;
1057
1058         if (cmd == OBD_BRW_CHECK) {
1059                 /* The caller just wants to know if there's a chance that this
1060                  * I/O can succeed */
1061                 struct obd_import *imp = class_exp2cliimp(exp);
1062
1063                 if (imp == NULL || imp->imp_invalid)
1064                         RETURN(-EIO);
1065                 RETURN(0);
1066         }
1067
1068         while (page_count) {
1069                 obd_count pages_per_brw;
1070                 int rc;
1071
1072                 if (page_count > OSC_BRW_MAX_IOV)
1073                         pages_per_brw = OSC_BRW_MAX_IOV;
1074                 else
1075                         pages_per_brw = page_count;
1076
1077                 sort_brw_pages(pga, pages_per_brw);
1078                 pages_per_brw = check_elan_limit(pga, pages_per_brw);
1079
1080                 rc = async_internal(cmd, exp, oa, md, pages_per_brw, pga, set);
1081
1082                 if (rc != 0)
1083                         RETURN(rc);
1084
1085                 page_count -= pages_per_brw;
1086                 pga += pages_per_brw;
1087         }
1088         RETURN(0);
1089 }
1090
1091 static void osc_check_rpcs(struct client_obd *cli);
1092 static void osc_exit_cache(struct client_obd *cli, struct osc_async_page *oap);
1093
1094 static void osc_complete_oap(struct client_obd *cli,
1095                              struct osc_async_page *oap, int rc)
1096 {
1097         ENTRY;
1098         osc_exit_cache(cli, oap);
1099         oap->oap_async_flags = 0;
1100         if (oap->oap_osic) {
1101                 osic_complete_one(oap->oap_osic, rc);
1102                 oap->oap_osic = NULL;
1103                 EXIT;
1104                 return;
1105         }
1106
1107         oap->oap_caller_ops->ap_completion(oap->oap_caller_data, oap->oap_cmd,
1108                                            rc);
1109         EXIT;
1110 }
1111
1112 static int brw_interpret_oap(struct ptlrpc_request *request,
1113                              struct osc_brw_async_args *aa, int rc)
1114 {
1115         struct osc_async_page *oap;
1116         struct client_obd *cli;
1117         struct list_head *pos, *n;
1118         ENTRY;
1119
1120         CDEBUG(D_INODE, "request %p aa %p\n", request, aa);
1121
1122         rc = osc_brw_fini_request(request, aa->aa_oa, aa->aa_requested_nob,
1123                                   aa->aa_nio_count, aa->aa_page_count,
1124                                   aa->aa_pga, rc);
1125
1126         cli = aa->aa_cli;
1127         /* in failout recovery we ignore writeback failure and want
1128          * to just tell llite to unlock the page and continue */
1129         if (cli->cl_import == NULL || cli->cl_import->imp_invalid)
1130                 rc = 0;
1131
1132         spin_lock(&cli->cl_loi_list_lock);
1133
1134         /* the caller may re-use the oap after the completion call so
1135          * we need to clean it up a little */
1136         list_for_each_safe(pos, n, &aa->aa_oaps) {
1137                 oap = list_entry(pos, struct osc_async_page, oap_rpc_item);
1138
1139                 //CDEBUG(D_INODE, "page %p index %lu oap %p\n",
1140                        //oap->oap_page, oap->oap_page->index, oap);
1141
1142                 list_del_init(&oap->oap_rpc_item);
1143                 osc_complete_oap(cli, oap, rc);
1144         }
1145
1146         cli->cl_brw_in_flight--;
1147         osc_check_rpcs(cli);
1148
1149         spin_unlock(&cli->cl_loi_list_lock);
1150
1151         obdo_free(aa->aa_oa);
1152         OBD_FREE(aa->aa_pga, aa->aa_page_count * sizeof(struct brw_page));
1153
1154         RETURN(0);
1155 }
1156
1157 static struct ptlrpc_request *osc_build_req(struct client_obd *cli,
1158                                             struct list_head *rpc_list,
1159                                             int page_count, int cmd)
1160 {
1161         struct ptlrpc_request *req;
1162         struct brw_page *pga = NULL;
1163         int requested_nob, nio_count;
1164         struct osc_brw_async_args *aa;
1165         struct obdo *oa = NULL;
1166         struct obd_async_page_ops *ops = NULL;
1167         void *caller_data = NULL;
1168         struct list_head *pos;
1169         int i, rc;
1170
1171         LASSERT(!list_empty(rpc_list));
1172
1173         OBD_ALLOC(pga, sizeof(*pga) * page_count);
1174         if (pga == NULL)
1175                 RETURN(ERR_PTR(-ENOMEM));
1176
1177         oa = obdo_alloc();
1178         if (oa == NULL)
1179                 GOTO(out, req = ERR_PTR(-ENOMEM));
1180
1181         i = 0;
1182         list_for_each(pos, rpc_list) {
1183                 struct osc_async_page *oap;
1184
1185                 oap = list_entry(pos, struct osc_async_page, oap_rpc_item);
1186                 if (ops == NULL) {
1187                         ops = oap->oap_caller_ops;
1188                         caller_data = oap->oap_caller_data;
1189                 }
1190                 pga[i].off = oap->oap_obj_off + oap->oap_page_off;
1191                 pga[i].pg = oap->oap_page;
1192                 pga[i].count = oap->oap_count;
1193                 pga[i].flag = oap->oap_brw_flags;
1194                 //CDEBUG(D_INODE, "putting page %p index %lu oap %p into pga\n",
1195                        //pga[i].pg, oap->oap_page->index, oap);
1196                 i++;
1197         }
1198
1199         /* always get the data for the obdo for the rpc */
1200         LASSERT(ops != NULL);
1201         ops->ap_fill_obdo(caller_data, cmd, oa);
1202
1203         sort_brw_pages(pga, page_count);
1204         rc = osc_brw_prep_request(cmd, cli->cl_import, oa, NULL, page_count,
1205                                   pga, &requested_nob, &nio_count, &req);
1206         if (rc != 0) {
1207                 CERROR("prep_req failed: %d\n", rc);
1208                 GOTO(out, req = ERR_PTR(rc));
1209         }
1210
1211         LASSERT(sizeof(*aa) <= sizeof(req->rq_async_args));
1212         aa = (struct osc_brw_async_args *)&req->rq_async_args;
1213         aa->aa_oa = oa;
1214         aa->aa_requested_nob = requested_nob;
1215         aa->aa_nio_count = nio_count;
1216         aa->aa_page_count = page_count;
1217         aa->aa_pga = pga;
1218         aa->aa_cli = cli;
1219
1220 out:
1221         if (IS_ERR(req)) {
1222                 if (oa)
1223                         obdo_free(oa);
1224                 if (pga)
1225                         OBD_FREE(pga, sizeof(*pga) * page_count);
1226         }
1227         RETURN(req);
1228 }
1229
1230 static void lop_update_pending(struct client_obd *cli,
1231                                struct loi_oap_pages *lop, int cmd, int delta)
1232 {
1233         lop->lop_num_pending += delta;
1234         if (cmd == OBD_BRW_WRITE)
1235                 cli->cl_pending_w_pages += delta;
1236         else
1237                 cli->cl_pending_r_pages += delta;
1238 }
1239
1240 /* the loi lock is held across this function but it's allowed to release
1241  * and reacquire it during its work */
1242 static int osc_send_oap_rpc(struct client_obd *cli, int cmd,
1243                             struct loi_oap_pages *lop)
1244 {
1245         struct ptlrpc_request *request;
1246         obd_count page_count = 0;
1247         struct list_head *tmp, *pos;
1248         struct osc_async_page *oap = NULL;
1249         struct osc_brw_async_args *aa;
1250         struct obd_async_page_ops *ops;
1251         LIST_HEAD(rpc_list);
1252         ENTRY;
1253
1254         /* first we find the pages we're allowed to work with */
1255         list_for_each_safe(pos, tmp, &lop->lop_pending) {
1256                 oap = list_entry(pos, struct osc_async_page, oap_pending_item);
1257                 ops = oap->oap_caller_ops;
1258
1259                 /* in llite being 'ready' equates to the page being locked
1260                  * until completion unlocks it.  commit_write submits a page
1261                  * as not ready because its unlock will happen unconditionally
1262                  * as the call returns.  if we race with commit_write giving
1263                  * us that page we dont' want to create a hole in the page
1264                  * stream, so we stop and leave the rpc to be fired by
1265                  * another dirtier or kupdated interval (the not ready page
1266                  * will still be on the dirty list).  we could call in
1267                  * at the end of ll_file_write to process the queue again. */
1268                 if (!(oap->oap_async_flags & ASYNC_READY)) {
1269                         if (ops->ap_make_ready(oap->oap_caller_data, cmd)) {
1270                                 CDEBUG(D_INODE, "oap at page_count %d not "
1271                                                 "ready\n", page_count);
1272                                 break;
1273                         }
1274                         oap->oap_async_flags |= ASYNC_READY;
1275                 }
1276
1277                 /* take the page out of our book-keeping */
1278                 list_del_init(&oap->oap_pending_item);
1279                 lop_update_pending(cli, lop, cmd, -1);
1280                 if (!list_empty(&oap->oap_urgent_item))
1281                         list_del_init(&oap->oap_urgent_item);
1282
1283                 /* ask the caller for the size of the io as the rpc leaves. */
1284                 if (!(oap->oap_async_flags & ASYNC_COUNT_STABLE))
1285                         oap->oap_count = ops->ap_refresh_count(
1286                                                         oap->oap_caller_data,
1287                                                         cmd);
1288                 if (oap->oap_count <= 0) {
1289                         CDEBUG(D_INODE, "oap %p count %d, completing\n", oap,
1290                                oap->oap_count);
1291                         osc_complete_oap(cli, oap, oap->oap_count);
1292                         continue;
1293                 }
1294
1295                 /* now put the page back in our accounting */
1296                 list_add_tail(&oap->oap_rpc_item, &rpc_list);
1297                 if (++page_count >= cli->cl_max_pages_per_rpc)
1298                         break;
1299         }
1300
1301         if (page_count == 0)
1302                 RETURN(0);
1303
1304         spin_unlock(&cli->cl_loi_list_lock);
1305
1306         request = osc_build_req(cli, &rpc_list, page_count, cmd);
1307         if (IS_ERR(request)) {
1308                 /* this should happen rarely and is pretty bad, it makes the
1309                  * pending list not follow the dirty order */
1310                 spin_lock(&cli->cl_loi_list_lock);
1311                 list_for_each_safe(pos, tmp, &rpc_list) {
1312                         oap = list_entry(pos, struct osc_async_page,
1313                                          oap_rpc_item);
1314                         list_del_init(&oap->oap_rpc_item);
1315                         list_add_tail(&oap->oap_pending_item,
1316                                       &lop->lop_pending);
1317                         lop_update_pending(cli, lop, cmd, 1);
1318                         if (oap->oap_async_flags & ASYNC_URGENT)
1319                                 list_add(&oap->oap_urgent_item,
1320                                          &lop->lop_urgent);
1321                 }
1322                 RETURN(PTR_ERR(request));
1323         }
1324
1325         LASSERT(sizeof(*aa) <= sizeof(request->rq_async_args));
1326         aa = (struct osc_brw_async_args *)&request->rq_async_args;
1327         INIT_LIST_HEAD(&aa->aa_oaps);
1328         list_splice(&rpc_list, &aa->aa_oaps);
1329         INIT_LIST_HEAD(&rpc_list);
1330
1331         if (cmd == OBD_BRW_READ)
1332                 lproc_osc_hist_pow2(&cli->cl_read_page_hist, page_count);
1333         else 
1334                 lproc_osc_hist_pow2(&cli->cl_write_page_hist, page_count);
1335
1336         spin_lock(&cli->cl_loi_list_lock);
1337         if (cmd == OBD_BRW_READ)
1338                 lproc_osc_hist(&cli->cl_read_rpc_hist, cli->cl_brw_in_flight);
1339         else 
1340                 lproc_osc_hist(&cli->cl_write_rpc_hist, cli->cl_brw_in_flight);
1341
1342         cli->cl_brw_in_flight++;
1343         CDEBUG(D_INODE, "req %p: %d pages, aa %p.  now %d in flight\n", request,
1344                page_count, aa, cli->cl_brw_in_flight);
1345
1346         request->rq_interpret_reply = brw_interpret_oap;
1347         ptlrpcd_add_req(request);
1348         RETURN(1);
1349 }
1350
1351 static int lop_makes_rpc(struct client_obd *cli, struct loi_oap_pages *lop,
1352                          int cmd)
1353 {
1354         int optimal;
1355         ENTRY;
1356
1357         /* stream rpcs until we complete the urgent pages in the object */
1358         if (!list_empty(&lop->lop_urgent))
1359                 RETURN(1);
1360
1361         /* fire off rpcs when we have 'optimal' rpcs as tuned for the wire. */
1362         optimal = cli->cl_max_pages_per_rpc;
1363         /* *2 to avoid triggering rpcs that would want to include pages that
1364          * are being queued but which can't be made ready until the queuer
1365          * finishes with the page. this is a wart for llite::commit_write() */
1366         if (cmd == OBD_BRW_WRITE)
1367                 optimal *= 2;
1368         if (lop->lop_num_pending >= optimal)
1369                 RETURN(1);
1370
1371         /* trigger a write rpc stream as long as there are dirtiers waiting
1372          * for space.  as they're waiting, they're not going to create more
1373          * pages to coallesce with what's waiting.. */
1374         if (!list_empty(&cli->cl_cache_waiters))
1375                 RETURN(1);
1376
1377         RETURN(0);
1378 }
1379
1380 static int loi_makes_rpc(struct client_obd *cli, struct lov_oinfo *loi)
1381 {
1382         return lop_makes_rpc(cli, &loi->loi_write_lop, OBD_BRW_WRITE) ||
1383                lop_makes_rpc(cli, &loi->loi_read_lop, OBD_BRW_READ);
1384 }
1385
1386 static void loi_onto_ready_list(struct client_obd *cli, struct lov_oinfo *loi)
1387 {
1388         if (list_empty(&loi->loi_cli_item) && loi_makes_rpc(cli, loi))
1389                 list_add_tail(&loi->loi_cli_item, &cli->cl_loi_ready_list);
1390 }
1391
1392 #define LOI_DEBUG(LOI, STR, args...) \
1393         CDEBUG(D_INODE, "loi rdy %d [%p,%p] wr %d:%d rd %d:%d " STR, \
1394                !list_empty(&(LOI)->loi_cli_item),                  \
1395                (LOI)->loi_cli_item.next,                  \
1396                (LOI)->loi_cli_item.prev,                  \
1397                (LOI)->loi_write_lop.lop_num_pending,                     \
1398                !list_empty(&(LOI)->loi_write_lop.lop_urgent),         \
1399                (LOI)->loi_read_lop.lop_num_pending,                      \
1400                !list_empty(&(LOI)->loi_read_lop.lop_urgent),         \
1401                args)                       \
1402
1403 /* called with the loi list lock held */
1404 static void osc_check_rpcs(struct client_obd *cli)
1405 {
1406         struct lov_oinfo *loi;
1407         int rc = 0, making_progress;
1408         ENTRY;
1409
1410         if (list_empty(&cli->cl_loi_ready_list)) {
1411                 CDEBUG(D_INODE, "no lois ready\n");
1412                 EXIT;
1413                 return;
1414         }
1415
1416         while (!list_empty(&cli->cl_loi_ready_list)) {
1417                 loi = list_entry(cli->cl_loi_ready_list.next, struct lov_oinfo,
1418                                  loi_cli_item);
1419
1420                 if (cli->cl_brw_in_flight >= cli->cl_max_rpcs_in_flight)
1421                         break;
1422
1423                 making_progress = 0;
1424
1425                 /* hmm, it occurs to me that having rpc preparation fail
1426                  * with num_pending == num_urgent means that there won't
1427                  * be any more calls into here unless other traffic comes
1428                  * in.  hmm. */
1429
1430                 /* attempt some read/write balancing by alternating between
1431                  * reads and writes in an object */
1432                 if (lop_makes_rpc(cli, &loi->loi_write_lop, OBD_BRW_WRITE)) {
1433                         rc = osc_send_oap_rpc(cli, OBD_BRW_WRITE,
1434                                               &loi->loi_write_lop);
1435                         if (rc < 0)
1436                                 break;
1437                         if (rc > 0)
1438                                 making_progress++;
1439                 }
1440                 if (lop_makes_rpc(cli, &loi->loi_read_lop, OBD_BRW_READ)) {
1441                         rc = osc_send_oap_rpc(cli, OBD_BRW_READ,
1442                                               &loi->loi_read_lop);
1443                         if (rc < 0)
1444                                 break;
1445                         if (rc > 0)
1446                                 making_progress++;
1447                 }
1448
1449                 /* attempt some inter-object balancing by issueing rpcs
1450                  * for each object in turn */
1451                 if (!list_empty(&loi->loi_cli_item))
1452                         list_del_init(&loi->loi_cli_item);
1453
1454                 loi_onto_ready_list(cli, loi);
1455
1456                 LOI_DEBUG(loi, "mp %d\n", making_progress);
1457
1458                 /* could be smarter, !making_progress can happen in theory
1459                  * if all the pages can not be locked in set_io_ready */
1460                 if (!making_progress)
1461                         break;
1462         }
1463         EXIT;
1464 }
1465
1466 /* we're trying to queue a page in the osc so we're subject to the
1467  * 'cl_dirty_max' limit on the number of pages that can be queued in the osc.
1468  * If the osc's queued pages are already at that limit, then we want to sleep
1469  * until there is space in the osc's queue for us.  we need this goofy
1470  * little struct to really tell that our allocation was fulfilled in
1471  * the presence of pending signals */
1472 struct osc_cache_waiter {
1473         struct list_head        ocw_entry;
1474         wait_queue_head_t       ocw_waitq;
1475         int                     ocw_rc;
1476 };
1477 static int ocw_granted(struct client_obd *cli, struct osc_cache_waiter *ocw)
1478 {
1479         int rc;
1480         ENTRY;
1481         spin_lock(&cli->cl_loi_list_lock);
1482         rc = list_empty(&ocw->ocw_entry);
1483         spin_unlock(&cli->cl_loi_list_lock);
1484         RETURN(rc);
1485 };
1486
1487 static inline obd_size osc_cache_cap(struct client_obd *cli)
1488 {
1489         if (cli->cl_ost_can_grant)
1490                 return min(cli->cl_dirty_granted, cli->cl_dirty_max);
1491
1492         return cli->cl_dirty_max;
1493 }
1494 void osc_adjust_cache(struct client_obd *cli)
1495 {
1496         struct list_head *l, *tmp;
1497         struct osc_cache_waiter *ocw;
1498         obd_size cache_cap = osc_cache_cap(cli);
1499
1500         ENTRY;
1501
1502         list_for_each_safe(l, tmp, &cli->cl_cache_waiters) {
1503                 if (cli->cl_dirty + PAGE_SIZE > cache_cap &&
1504                     cache_cap >= PAGE_SIZE)
1505                         break;
1506
1507                 ocw = list_entry(l, struct osc_cache_waiter, ocw_entry);
1508                 list_del_init(&ocw->ocw_entry);
1509                 if (cache_cap < PAGE_SIZE) {
1510                 /* "They" said we are starting synchronous operations, so
1511                    wakeup everybody waiting for pages in cache and make them
1512                    go away unsatisfied. */
1513                         ocw->ocw_rc = -EDQUOT;
1514                 } else {
1515                         cli->cl_dirty += PAGE_SIZE;
1516                 }
1517                 wake_up(&ocw->ocw_waitq);
1518         }
1519
1520         EXIT;
1521 }
1522 static int osc_enter_cache(struct client_obd *cli, struct lov_oinfo *loi,
1523                            struct osc_async_page *oap)
1524 {
1525         struct osc_cache_waiter ocw;
1526         struct l_wait_info lwi = {0};
1527         int rc = 0;
1528         ENTRY;
1529
1530         if (osc_cache_cap(cli) < PAGE_SIZE)
1531                 RETURN(-EDQUOT);
1532
1533         /* if we fail this test then cl_dirty contains at least one page
1534          * that will have to be completed after we release the lock */
1535         if (cli->cl_dirty + PAGE_SIZE <= osc_cache_cap(cli)) {
1536                 /* account for ourselves */
1537                 cli->cl_dirty += PAGE_SIZE;
1538                 GOTO(out, rc = 0);
1539         }
1540
1541         init_waitqueue_head(&ocw.ocw_waitq);
1542         ocw.ocw_rc = 0;
1543         list_add_tail(&ocw.ocw_entry, &cli->cl_cache_waiters);
1544
1545         /* make sure that there are write rpcs in flight to wait for. this
1546          * is a little silly as this object may not have any pending
1547          * but other objects sure might. this should probably be cleaned. */
1548         loi_onto_ready_list(cli, loi);
1549         osc_check_rpcs(cli);
1550         spin_unlock(&cli->cl_loi_list_lock);
1551
1552         CDEBUG(D_INODE, "sleeping for cache space\n");
1553         l_wait_event(ocw.ocw_waitq, ocw_granted(cli, &ocw), &lwi);
1554
1555         spin_lock(&cli->cl_loi_list_lock);
1556         rc = ocw.ocw_rc;
1557         if (!list_empty(&ocw.ocw_entry)) {
1558                 rc = -EINTR;
1559                 list_del(&ocw.ocw_entry);
1560         }
1561         GOTO(out, rc);
1562 out:
1563         if (rc == 0)
1564                 oap->oap_brw_flags |= OBD_BRW_FROM_GRANT;
1565         return rc;
1566 }
1567
1568 /* the companion to enter_cache, called when an oap is now longer part of the
1569  * dirty accounting.. so writeback completes or truncate happens before writing
1570  * starts.  must be called with the loi lock held. */
1571 static void osc_exit_cache(struct client_obd *cli, struct osc_async_page *oap)
1572 {
1573         struct osc_cache_waiter *ocw;
1574         ENTRY;
1575
1576         if (!(oap->oap_brw_flags & OBD_BRW_FROM_GRANT)) {
1577                 EXIT;
1578                 return;
1579         }
1580
1581         /* If nobody waits for cache space or if we need to shrink it */
1582         if (list_empty(&cli->cl_cache_waiters) ||
1583             (cli->cl_dirty > osc_cache_cap(cli))) {
1584                 cli->cl_dirty -= PAGE_SIZE;
1585         } else {
1586                 ocw = list_entry(cli->cl_cache_waiters.next,
1587                                  struct osc_cache_waiter, ocw_entry);
1588                 list_del_init(&ocw->ocw_entry);
1589                 wake_up(&ocw->ocw_waitq);
1590         }
1591
1592         oap->oap_brw_flags &= ~OBD_BRW_FROM_GRANT;
1593         EXIT;
1594 }
1595
1596 int osc_prep_async_page(struct obd_export *exp, struct lov_stripe_md *lsm,
1597                         struct lov_oinfo *loi, struct page *page,
1598                         obd_off offset, struct obd_async_page_ops *ops,
1599                         void *data, void **res)
1600 {
1601         struct osc_async_page *oap;
1602         ENTRY;
1603
1604         OBD_ALLOC(oap, sizeof(*oap));
1605         if (oap == NULL)
1606                 return -ENOMEM;
1607
1608         oap->oap_magic = OAP_MAGIC;
1609         oap->oap_caller_ops = ops;
1610         oap->oap_caller_data = data;
1611
1612         oap->oap_page = page;
1613         oap->oap_obj_off = offset;
1614
1615         INIT_LIST_HEAD(&oap->oap_pending_item);
1616         INIT_LIST_HEAD(&oap->oap_urgent_item);
1617         INIT_LIST_HEAD(&oap->oap_rpc_item);
1618
1619         CDEBUG(D_CACHE, "oap %p page %p obj off "LPU64"\n", oap, page, offset);
1620         *res = oap;
1621         RETURN(0);
1622 }
1623
1624 struct osc_async_page *oap_from_cookie(void *cookie)
1625 {
1626         struct osc_async_page *oap = cookie;
1627         if (oap->oap_magic != OAP_MAGIC)
1628                 return ERR_PTR(-EINVAL);
1629         return oap;
1630 };
1631
1632 static int osc_queue_async_io(struct obd_export *exp, struct lov_stripe_md *lsm,
1633                               struct lov_oinfo *loi, void *cookie,
1634                               int cmd, obd_off off, int count,
1635                               obd_flag brw_flags, enum async_flags async_flags)
1636 {
1637         struct client_obd *cli = &exp->exp_obd->u.cli;
1638         struct osc_async_page *oap;
1639         struct loi_oap_pages *lop;
1640         int rc;
1641         ENTRY;
1642
1643         oap = oap_from_cookie(cookie);
1644         if (IS_ERR(oap))
1645                 RETURN(PTR_ERR(oap));
1646
1647         if (cli->cl_import == NULL || cli->cl_import->imp_invalid)
1648                 RETURN(-EIO);
1649
1650         if (!list_empty(&oap->oap_pending_item) ||
1651             !list_empty(&oap->oap_urgent_item) ||
1652             !list_empty(&oap->oap_rpc_item))
1653                 RETURN(-EBUSY);
1654
1655         if (loi == NULL)
1656                 loi = &lsm->lsm_oinfo[0];
1657
1658         spin_lock(&cli->cl_loi_list_lock);
1659
1660         oap->oap_cmd = cmd;
1661         oap->oap_async_flags = async_flags;
1662         oap->oap_page_off = off;
1663         oap->oap_count = count;
1664         oap->oap_brw_flags = brw_flags;
1665
1666         if (cmd == OBD_BRW_WRITE) {
1667                 rc = osc_enter_cache(cli, loi, oap);
1668                 if (rc) {
1669                         spin_unlock(&cli->cl_loi_list_lock);
1670                         RETURN(rc);
1671                 }
1672                 lop = &loi->loi_write_lop;
1673         } else {
1674                 lop = &loi->loi_read_lop;
1675         }
1676
1677         if (oap->oap_async_flags & ASYNC_URGENT)
1678                 list_add(&oap->oap_urgent_item, &lop->lop_urgent);
1679         list_add_tail(&oap->oap_pending_item, &lop->lop_pending);
1680         lop_update_pending(cli, lop, cmd, 1);
1681
1682         loi_onto_ready_list(cli, loi);
1683
1684         LOI_DEBUG(loi, "oap %p page %p added for cmd %d\n", oap, oap->oap_page,
1685                   cmd);
1686
1687         osc_check_rpcs(cli);
1688         spin_unlock(&cli->cl_loi_list_lock);
1689
1690         RETURN(0);
1691 }
1692
1693 /* aka (~was & now & flag), but this is more clear :) */
1694 #define SETTING(was, now, flag) (!(was & flag) && (now & flag))
1695
1696 static int osc_set_async_flags(struct obd_export *exp,
1697                                struct lov_stripe_md *lsm,
1698                                struct lov_oinfo *loi, void *cookie,
1699                                obd_flag async_flags)
1700 {
1701         struct client_obd *cli = &exp->exp_obd->u.cli;
1702         struct loi_oap_pages *lop;
1703         struct osc_async_page *oap;
1704         int rc = 0;
1705         ENTRY;
1706
1707         oap = oap_from_cookie(cookie);
1708         if (IS_ERR(oap))
1709                 RETURN(PTR_ERR(oap));
1710
1711         if (cli->cl_import == NULL || cli->cl_import->imp_invalid)
1712                 RETURN(-EIO);
1713
1714         if (loi == NULL)
1715                 loi = &lsm->lsm_oinfo[0];
1716
1717         if (oap->oap_cmd == OBD_BRW_WRITE) {
1718                 lop = &loi->loi_write_lop;
1719         } else {
1720                 lop = &loi->loi_read_lop;
1721         }
1722
1723         spin_lock(&cli->cl_loi_list_lock);
1724
1725         if (oap->oap_async_flags == async_flags)
1726                 GOTO(out, rc = 0);
1727
1728         if (SETTING(oap->oap_async_flags, async_flags, ASYNC_READY))
1729                 oap->oap_async_flags |= ASYNC_READY;
1730
1731         if (SETTING(oap->oap_async_flags, async_flags, ASYNC_URGENT)) {
1732                 if (list_empty(&oap->oap_rpc_item)) {
1733                         list_add(&oap->oap_urgent_item, &lop->lop_urgent);
1734                         loi_onto_ready_list(cli, loi);
1735                 }
1736         }
1737
1738         LOI_DEBUG(loi, "oap %p page %p has flags %x\n", oap, oap->oap_page,
1739                         oap->oap_async_flags);
1740 out:
1741         osc_check_rpcs(cli);
1742         spin_unlock(&cli->cl_loi_list_lock);
1743         RETURN(rc);
1744 }
1745
1746 static int osc_queue_sync_io(struct obd_export *exp, struct lov_stripe_md *lsm,
1747                              struct lov_oinfo *loi,
1748                              struct obd_sync_io_container *osic, void *cookie,
1749                              int cmd, obd_off off, int count,
1750                              obd_flag brw_flags)
1751 {
1752         struct client_obd *cli = &exp->exp_obd->u.cli;
1753         struct osc_async_page *oap;
1754         struct loi_oap_pages *lop;
1755         ENTRY;
1756
1757         oap = oap_from_cookie(cookie);
1758         if (IS_ERR(oap))
1759                 RETURN(PTR_ERR(oap));
1760
1761         if (cli->cl_import == NULL || cli->cl_import->imp_invalid)
1762                 RETURN(-EIO);
1763
1764         if (!list_empty(&oap->oap_pending_item) ||
1765             !list_empty(&oap->oap_urgent_item) ||
1766             !list_empty(&oap->oap_rpc_item))
1767                 RETURN(-EBUSY);
1768
1769         if (loi == NULL)
1770                 loi = &lsm->lsm_oinfo[0];
1771
1772         spin_lock(&cli->cl_loi_list_lock);
1773
1774         oap->oap_cmd = cmd;
1775         oap->oap_page_off = off;
1776         oap->oap_count = count;
1777         oap->oap_brw_flags = brw_flags;
1778
1779         if (cmd == OBD_BRW_WRITE)
1780                 lop = &loi->loi_write_lop;
1781         else
1782                 lop = &loi->loi_read_lop;
1783
1784         list_add_tail(&oap->oap_pending_item, &lop->lop_pending_sync);
1785         oap->oap_osic = osic;
1786         osic_add_one(osic);
1787
1788         LOI_DEBUG(loi, "oap %p page %p on sync pending\n", oap, oap->oap_page);
1789
1790         spin_unlock(&cli->cl_loi_list_lock);
1791
1792         RETURN(0);
1793 }
1794
1795 static void osc_sync_to_pending(struct client_obd *cli, struct lov_oinfo *loi,
1796                                 struct loi_oap_pages *lop, int cmd)
1797 {
1798         struct list_head *pos, *tmp;
1799         struct osc_async_page *oap;
1800
1801         list_for_each_safe(pos, tmp, &lop->lop_pending_sync) {
1802                 oap = list_entry(pos, struct osc_async_page, oap_pending_item);
1803                 list_del(&oap->oap_pending_item);
1804                 oap->oap_async_flags |= ASYNC_READY | ASYNC_URGENT |
1805                                         ASYNC_COUNT_STABLE;
1806                 list_add_tail(&oap->oap_pending_item, &lop->lop_pending);
1807                 list_add(&oap->oap_urgent_item, &lop->lop_urgent);
1808                 lop_update_pending(cli, lop, cmd, 1);
1809         }
1810         loi_onto_ready_list(cli, loi);
1811 }
1812
1813 static int osc_trigger_sync_io(struct obd_export *exp,
1814                                struct lov_stripe_md *lsm,
1815                                struct lov_oinfo *loi,
1816                                struct obd_sync_io_container *osic)
1817 {
1818         struct client_obd *cli = &exp->exp_obd->u.cli;
1819         ENTRY;
1820
1821         if (cli->cl_import == NULL || cli->cl_import->imp_invalid)
1822                 RETURN(-EIO);
1823
1824         if (loi == NULL)
1825                 loi = &lsm->lsm_oinfo[0];
1826
1827         spin_lock(&cli->cl_loi_list_lock);
1828
1829         osc_sync_to_pending(cli, loi, &loi->loi_write_lop, OBD_BRW_WRITE);
1830         osc_sync_to_pending(cli, loi, &loi->loi_read_lop, OBD_BRW_READ);
1831
1832         osc_check_rpcs(cli);
1833         spin_unlock(&cli->cl_loi_list_lock);
1834
1835         RETURN(0);
1836 }
1837
1838 static int osc_teardown_async_page(struct obd_export *exp,
1839                                    struct lov_stripe_md *lsm,
1840                                    struct lov_oinfo *loi, void *cookie)
1841 {
1842         struct client_obd *cli = &exp->exp_obd->u.cli;
1843         struct loi_oap_pages *lop;
1844         struct osc_async_page *oap;
1845         int rc = 0;
1846         ENTRY;
1847
1848         oap = oap_from_cookie(cookie);
1849         if (IS_ERR(oap))
1850                 RETURN(PTR_ERR(oap));
1851
1852         if (loi == NULL)
1853                 loi = &lsm->lsm_oinfo[0];
1854
1855         if (oap->oap_cmd == OBD_BRW_WRITE) {
1856                 lop = &loi->loi_write_lop;
1857         } else {
1858                 lop = &loi->loi_read_lop;
1859         }
1860
1861         spin_lock(&cli->cl_loi_list_lock);
1862
1863         osc_exit_cache(cli, oap);
1864
1865         if (!list_empty(&oap->oap_rpc_item))
1866                 GOTO(out, rc = -EBUSY);
1867
1868         if (!list_empty(&oap->oap_urgent_item)) {
1869                 list_del_init(&oap->oap_urgent_item);
1870                 oap->oap_async_flags &= ~ASYNC_URGENT;
1871         }
1872         if (!list_empty(&oap->oap_pending_item)) {
1873                 list_del_init(&oap->oap_pending_item);
1874                 lop_update_pending(cli, lop, oap->oap_cmd, -1);
1875         }
1876         if (!list_empty(&loi->loi_cli_item) && !loi_makes_rpc(cli, loi))
1877                 list_del_init(&loi->loi_cli_item);
1878
1879         LOI_DEBUG(loi, "oap %p page %p torn down\n", oap, oap->oap_page);
1880 out:
1881         spin_unlock(&cli->cl_loi_list_lock);
1882         OBD_FREE(oap, sizeof(*oap));
1883         RETURN(rc);
1884 }
1885
1886 #ifdef __KERNEL__
1887 /* Note: caller will lock/unlock, and set uptodate on the pages */
1888 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
1889 static int sanosc_brw_read(struct obd_export *exp, struct obdo *oa,
1890                            struct lov_stripe_md *lsm, obd_count page_count,
1891                            struct brw_page *pga)
1892 {
1893         struct ptlrpc_request *request = NULL;
1894         struct ost_body *body;
1895         struct niobuf_remote *nioptr;
1896         struct obd_ioobj *iooptr;
1897         int rc, size[3] = {sizeof(*body)}, mapped = 0;
1898         int swab;
1899         ENTRY;
1900
1901         /* XXX does not handle 'new' brw protocol */
1902
1903         size[1] = sizeof(struct obd_ioobj);
1904         size[2] = page_count * sizeof(*nioptr);
1905
1906         request = ptlrpc_prep_req(class_exp2cliimp(exp), OST_SAN_READ, 3,
1907                                   size, NULL);
1908         if (!request)
1909                 RETURN(-ENOMEM);
1910
1911         body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof(*body));
1912         iooptr = lustre_msg_buf(request->rq_reqmsg, 1, sizeof(*iooptr));
1913         nioptr = lustre_msg_buf(request->rq_reqmsg, 2,
1914                                 sizeof(*nioptr) * page_count);
1915
1916         memcpy(&body->oa, oa, sizeof(body->oa));
1917
1918         obdo_to_ioobj(oa, iooptr);
1919         iooptr->ioo_bufcnt = page_count;
1920
1921         for (mapped = 0; mapped < page_count; mapped++, nioptr++) {
1922                 LASSERT(PageLocked(pga[mapped].pg));
1923                 LASSERT(mapped == 0 || pga[mapped].off > pga[mapped - 1].off);
1924
1925                 nioptr->offset = pga[mapped].off;
1926                 nioptr->len    = pga[mapped].count;
1927                 nioptr->flags  = pga[mapped].flag;
1928         }
1929
1930         size[1] = page_count * sizeof(*nioptr);
1931         request->rq_replen = lustre_msg_size(2, size);
1932
1933         rc = ptlrpc_queue_wait(request);
1934         if (rc)
1935                 GOTO(out_req, rc);
1936
1937         body = lustre_swab_repbuf(request, 0, sizeof(*body),
1938                                   lustre_swab_ost_body);
1939         if (body == NULL) {
1940                 CERROR("Can't unpack body\n");
1941                 GOTO(out_req, rc = -EPROTO);
1942         }
1943
1944         memcpy(oa, &body->oa, sizeof(*oa));
1945
1946         swab = lustre_msg_swabbed(request->rq_repmsg);
1947         LASSERT_REPSWAB(request, 1);
1948         nioptr = lustre_msg_buf(request->rq_repmsg, 1, size[1]);
1949         if (!nioptr) {
1950                 /* nioptr missing or short */
1951                 GOTO(out_req, rc = -EPROTO);
1952         }
1953
1954         /* actual read */
1955         for (mapped = 0; mapped < page_count; mapped++, nioptr++) {
1956                 struct page *page = pga[mapped].pg;
1957                 struct buffer_head *bh;
1958                 kdev_t dev;
1959
1960                 if (swab)
1961                         lustre_swab_niobuf_remote (nioptr);
1962
1963                 /* got san device associated */
1964                 LASSERT(exp->exp_obd != NULL);
1965                 dev = exp->exp_obd->u.cli.cl_sandev;
1966
1967                 /* hole */
1968                 if (!nioptr->offset) {
1969                         CDEBUG(D_PAGE, "hole at ino %lu; index %ld\n",
1970                                         page->mapping->host->i_ino,
1971                                         page->index);
1972                         memset(page_address(page), 0, PAGE_SIZE);
1973                         continue;
1974                 }
1975
1976                 if (!page->buffers) {
1977                         create_empty_buffers(page, dev, PAGE_SIZE);
1978                         bh = page->buffers;
1979
1980                         clear_bit(BH_New, &bh->b_state);
1981                         set_bit(BH_Mapped, &bh->b_state);
1982                         bh->b_blocknr = (unsigned long)nioptr->offset;
1983
1984                         clear_bit(BH_Uptodate, &bh->b_state);
1985
1986                         ll_rw_block(READ, 1, &bh);
1987                 } else {
1988                         bh = page->buffers;
1989
1990                         /* if buffer already existed, it must be the
1991                          * one we mapped before, check it */
1992                         LASSERT(!test_bit(BH_New, &bh->b_state));
1993                         LASSERT(test_bit(BH_Mapped, &bh->b_state));
1994                         LASSERT(bh->b_blocknr == (unsigned long)nioptr->offset);
1995
1996                         /* wait it's io completion */
1997                         if (test_bit(BH_Lock, &bh->b_state))
1998                                 wait_on_buffer(bh);
1999
2000                         if (!test_bit(BH_Uptodate, &bh->b_state))
2001                                 ll_rw_block(READ, 1, &bh);
2002                 }
2003
2004
2005                 /* must do syncronous write here */
2006                 wait_on_buffer(bh);
2007                 if (!buffer_uptodate(bh)) {
2008                         /* I/O error */
2009                         rc = -EIO;
2010                         goto out_req;
2011                 }
2012         }
2013
2014 out_req:
2015         ptlrpc_req_finished(request);
2016         RETURN(rc);
2017 }
2018
2019 static int sanosc_brw_write(struct obd_export *exp, struct obdo *oa,
2020                             struct lov_stripe_md *lsm, obd_count page_count,
2021                             struct brw_page *pga)
2022 {
2023         struct ptlrpc_request *request = NULL;
2024         struct ost_body *body;
2025         struct niobuf_remote *nioptr;
2026         struct obd_ioobj *iooptr;
2027         int rc, size[3] = {sizeof(*body)}, mapped = 0;
2028         int swab;
2029         ENTRY;
2030
2031         size[1] = sizeof(struct obd_ioobj);
2032         size[2] = page_count * sizeof(*nioptr);
2033
2034         request = ptlrpc_prep_req(class_exp2cliimp(exp), OST_SAN_WRITE,
2035                                   3, size, NULL);
2036         if (!request)
2037                 RETURN(-ENOMEM);
2038
2039         body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof (*body));
2040         iooptr = lustre_msg_buf(request->rq_reqmsg, 1, sizeof (*iooptr));
2041         nioptr = lustre_msg_buf(request->rq_reqmsg, 2,
2042                                 sizeof (*nioptr) * page_count);
2043
2044         memcpy(&body->oa, oa, sizeof(body->oa));
2045
2046         obdo_to_ioobj(oa, iooptr);
2047         iooptr->ioo_bufcnt = page_count;
2048
2049         /* pack request */
2050         for (mapped = 0; mapped < page_count; mapped++, nioptr++) {
2051                 LASSERT(PageLocked(pga[mapped].pg));
2052                 LASSERT(mapped == 0 || pga[mapped].off > pga[mapped - 1].off);
2053
2054                 nioptr->offset = pga[mapped].off;
2055                 nioptr->len    = pga[mapped].count;
2056                 nioptr->flags  = pga[mapped].flag;
2057         }
2058
2059         size[1] = page_count * sizeof(*nioptr);
2060         request->rq_replen = lustre_msg_size(2, size);
2061
2062         rc = ptlrpc_queue_wait(request);
2063         if (rc)
2064                 GOTO(out_req, rc);
2065
2066         swab = lustre_msg_swabbed (request->rq_repmsg);
2067         LASSERT_REPSWAB (request, 1);
2068         nioptr = lustre_msg_buf(request->rq_repmsg, 1, size[1]);
2069         if (!nioptr) {
2070                 CERROR("absent/short niobuf array\n");
2071                 GOTO(out_req, rc = -EPROTO);
2072         }
2073
2074         /* actual write */
2075         for (mapped = 0; mapped < page_count; mapped++, nioptr++) {
2076                 struct page *page = pga[mapped].pg;
2077                 struct buffer_head *bh;
2078                 kdev_t dev;
2079
2080                 if (swab)
2081                         lustre_swab_niobuf_remote (nioptr);
2082
2083                 /* got san device associated */
2084                 LASSERT(exp->exp_obd != NULL);
2085                 dev = exp->exp_obd->u.cli.cl_sandev;
2086
2087                 if (!page->buffers) {
2088                         create_empty_buffers(page, dev, PAGE_SIZE);
2089                 } else {
2090                         /* checking */
2091                         LASSERT(!test_bit(BH_New, &page->buffers->b_state));
2092                         LASSERT(test_bit(BH_Mapped, &page->buffers->b_state));
2093                         LASSERT(page->buffers->b_blocknr ==
2094                                 (unsigned long)nioptr->offset);
2095                 }
2096                 bh = page->buffers;
2097
2098                 LASSERT(bh);
2099
2100                 /* if buffer locked, wait it's io completion */
2101                 if (test_bit(BH_Lock, &bh->b_state))
2102                         wait_on_buffer(bh);
2103
2104                 clear_bit(BH_New, &bh->b_state);
2105                 set_bit(BH_Mapped, &bh->b_state);
2106
2107                 /* override the block nr */
2108                 bh->b_blocknr = (unsigned long)nioptr->offset;
2109
2110                 /* we are about to write it, so set it
2111                  * uptodate/dirty
2112                  * page lock should garentee no race condition here */
2113                 set_bit(BH_Uptodate, &bh->b_state);
2114                 set_bit(BH_Dirty, &bh->b_state);
2115
2116                 ll_rw_block(WRITE, 1, &bh);
2117
2118                 /* must do syncronous write here */
2119                 wait_on_buffer(bh);
2120                 if (!buffer_uptodate(bh) || test_bit(BH_Dirty, &bh->b_state)) {
2121                         /* I/O error */
2122                         rc = -EIO;
2123                         goto out_req;
2124                 }
2125         }
2126
2127 out_req:
2128         ptlrpc_req_finished(request);
2129         RETURN(rc);
2130 }
2131
2132 static int sanosc_brw(int cmd, struct obd_export *exp, struct obdo *oa,
2133                       struct lov_stripe_md *lsm, obd_count page_count,
2134                       struct brw_page *pga, struct obd_trans_info *oti)
2135 {
2136         ENTRY;
2137
2138         while (page_count) {
2139                 obd_count pages_per_brw;
2140                 int rc;
2141
2142                 if (page_count > OSC_BRW_MAX_IOV)
2143                         pages_per_brw = OSC_BRW_MAX_IOV;
2144                 else
2145                         pages_per_brw = page_count;
2146
2147                 if (cmd & OBD_BRW_WRITE)
2148                         rc = sanosc_brw_write(exp, oa, lsm, pages_per_brw,pga);
2149                 else
2150                         rc = sanosc_brw_read(exp, oa, lsm, pages_per_brw, pga);
2151
2152                 if (rc != 0)
2153                         RETURN(rc);
2154
2155                 page_count -= pages_per_brw;
2156                 pga += pages_per_brw;
2157         }
2158         RETURN(0);
2159 }
2160 #endif
2161 #endif
2162
2163 static void osc_set_data_with_check(struct lustre_handle *lockh, void *data)
2164 {
2165         struct ldlm_lock *lock = ldlm_handle2lock(lockh);
2166
2167         LASSERT(lock != NULL);
2168         l_lock(&lock->l_resource->lr_namespace->ns_lock);
2169 #ifdef __KERNEL__
2170         if (lock->l_ast_data && lock->l_ast_data != data) {
2171                 struct inode *new_inode = data;
2172                 struct inode *old_inode = lock->l_ast_data;
2173                 unsigned long state = old_inode->i_state & I_FREEING;
2174                 CERROR("Found existing inode %p/%lu/%u state %lu in lock: "
2175                        "setting data to %p/%lu/%u\n", old_inode,
2176                        old_inode->i_ino, old_inode->i_generation, state,
2177                        new_inode, new_inode->i_ino, new_inode->i_generation);
2178                 LASSERT(state);
2179         }
2180 #endif
2181         lock->l_ast_data = data;
2182         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
2183         LDLM_LOCK_PUT(lock);
2184 }
2185
2186 static int osc_change_cbdata(struct obd_export *exp, struct lov_stripe_md *lsm,
2187                              ldlm_iterator_t replace, void *data)
2188 {
2189         struct ldlm_res_id res_id = { .name = {lsm->lsm_object_id} };
2190         struct obd_device *obd = class_exp2obd(exp);
2191
2192         ldlm_change_cbdata(obd->obd_namespace, &res_id, replace, data);
2193         return 0;
2194 }
2195
2196 static int osc_enqueue(struct obd_export *exp, struct lov_stripe_md *lsm,
2197                        struct lustre_handle *parent_lock,
2198                        __u32 type, void *extentp, int extent_len, __u32 mode,
2199                        int *flags, void *callback, void *data,
2200                        struct lustre_handle *lockh)
2201 {
2202         struct ldlm_res_id res_id = { .name = {lsm->lsm_object_id} };
2203         struct obd_device *obd = exp->exp_obd;
2204         struct ldlm_extent *extent = extentp;
2205         int rc;
2206         ENTRY;
2207
2208         /* Filesystem lock extents are extended to page boundaries so that
2209          * dealing with the page cache is a little smoother.  */
2210         extent->start -= extent->start & ~PAGE_MASK;
2211         extent->end |= ~PAGE_MASK;
2212
2213         /* Next, search for already existing extent locks that will cover us */
2214         rc = ldlm_lock_match(obd->obd_namespace, 0, &res_id,
2215                              type, extent, sizeof(*extent), mode, lockh);
2216         if (rc == 1) {
2217                 osc_set_data_with_check(lockh, data);
2218                 /* We already have a lock, and it's referenced */
2219                 RETURN(ELDLM_OK);
2220         }
2221
2222         /* If we're trying to read, we also search for an existing PW lock.  The
2223          * VFS and page cache already protect us locally, so lots of readers/
2224          * writers can share a single PW lock.
2225          *
2226          * There are problems with conversion deadlocks, so instead of
2227          * converting a read lock to a write lock, we'll just enqueue a new
2228          * one.
2229          *
2230          * At some point we should cancel the read lock instead of making them
2231          * send us a blocking callback, but there are problems with canceling
2232          * locks out from other users right now, too. */
2233
2234         if (mode == LCK_PR) {
2235                 rc = ldlm_lock_match(obd->obd_namespace, 0, &res_id, type,
2236                                      extent, sizeof(*extent), LCK_PW, lockh);
2237                 if (rc == 1) {
2238                         /* FIXME: This is not incredibly elegant, but it might
2239                          * be more elegant than adding another parameter to
2240                          * lock_match.  I want a second opinion. */
2241                         ldlm_lock_addref(lockh, LCK_PR);
2242                         ldlm_lock_decref(lockh, LCK_PW);
2243                         osc_set_data_with_check(lockh, data);
2244                         RETURN(ELDLM_OK);
2245                 }
2246         }
2247
2248         rc = ldlm_cli_enqueue(exp, NULL, obd->obd_namespace, parent_lock,
2249                               res_id, type, extent, sizeof(*extent), mode,
2250                               flags,ldlm_completion_ast, callback, data, lockh);
2251         RETURN(rc);
2252 }
2253
2254 static int osc_match(struct obd_export *exp, struct lov_stripe_md *lsm,
2255                      __u32 type, void *extentp, int extent_len, __u32 mode,
2256                      int *flags, void *data, struct lustre_handle *lockh)
2257 {
2258         struct ldlm_res_id res_id = { .name = {lsm->lsm_object_id} };
2259         struct obd_device *obd = exp->exp_obd;
2260         struct ldlm_extent *extent = extentp;
2261         int rc;
2262         ENTRY;
2263
2264         OBD_FAIL_RETURN(OBD_FAIL_OSC_MATCH, -EIO);
2265
2266         /* Filesystem lock extents are extended to page boundaries so that
2267          * dealing with the page cache is a little smoother */
2268         extent->start -= extent->start & ~PAGE_MASK;
2269         extent->end |= ~PAGE_MASK;
2270
2271         /* Next, search for already existing extent locks that will cover us */
2272         rc = ldlm_lock_match(obd->obd_namespace, *flags, &res_id, type,
2273                              extent, sizeof(*extent), mode, lockh);
2274         if (rc) {
2275                 osc_set_data_with_check(lockh, data);
2276                 RETURN(rc);
2277         }
2278         /* If we're trying to read, we also search for an existing PW lock.  The
2279          * VFS and page cache already protect us locally, so lots of readers/
2280          * writers can share a single PW lock. */
2281         if (mode == LCK_PR) {
2282                 rc = ldlm_lock_match(obd->obd_namespace, *flags, &res_id, type,
2283                                      extent, sizeof(*extent), LCK_PW, lockh);
2284                 if (rc == 1) {
2285                         /* FIXME: This is not incredibly elegant, but it might
2286                          * be more elegant than adding another parameter to
2287                          * lock_match.  I want a second opinion. */
2288                         osc_set_data_with_check(lockh, data);
2289                         ldlm_lock_addref(lockh, LCK_PR);
2290                         ldlm_lock_decref(lockh, LCK_PW);
2291                 }
2292         }
2293         RETURN(rc);
2294 }
2295
2296 static int osc_cancel(struct obd_export *exp, struct lov_stripe_md *md,
2297                       __u32 mode, struct lustre_handle *lockh)
2298 {
2299         ENTRY;
2300
2301         ldlm_lock_decref(lockh, mode);
2302
2303         RETURN(0);
2304 }
2305
2306 static int osc_cancel_unused(struct obd_export *exp,
2307                              struct lov_stripe_md *lsm, int flags, void *opaque)
2308 {
2309         struct obd_device *obd = class_exp2obd(exp);
2310         struct ldlm_res_id res_id = { .name = {lsm->lsm_object_id} };
2311
2312         return ldlm_cli_cancel_unused(obd->obd_namespace, &res_id, flags,
2313                                       opaque);
2314 }
2315
2316 static int osc_statfs(struct obd_device *obd, struct obd_statfs *osfs,
2317                       unsigned long max_age)
2318 {
2319         struct obd_statfs *msfs;
2320         struct ptlrpc_request *request;
2321         int rc, size = sizeof(*osfs);
2322         ENTRY;
2323
2324         /* We could possibly pass max_age in the request (as an absolute
2325          * timestamp or a "seconds.usec ago") so the target can avoid doing
2326          * extra calls into the filesystem if that isn't necessary (e.g.
2327          * during mount that would help a bit).  Having relative timestamps
2328          * is not so great if request processing is slow, while absolute
2329          * timestamps are not ideal because they need time synchronization. */
2330         request = ptlrpc_prep_req(obd->u.cli.cl_import, OST_STATFS,0,NULL,NULL);
2331         if (!request)
2332                 RETURN(-ENOMEM);
2333
2334         request->rq_replen = lustre_msg_size(1, &size);
2335         request->rq_request_portal = OST_CREATE_PORTAL; //XXX FIXME bug 249
2336
2337         rc = ptlrpc_queue_wait(request);
2338         if (rc)
2339                 GOTO(out, rc);
2340
2341         msfs = lustre_swab_repbuf(request, 0, sizeof(*msfs),
2342                                   lustre_swab_obd_statfs);
2343         if (msfs == NULL) {
2344                 CERROR("Can't unpack obd_statfs\n");
2345                 GOTO(out, rc = -EPROTO);
2346         }
2347
2348         memcpy(osfs, msfs, sizeof(*osfs));
2349
2350         EXIT;
2351  out:
2352         ptlrpc_req_finished(request);
2353         return rc;
2354 }
2355
2356 /* Retrieve object striping information.
2357  *
2358  * @lmmu is a pointer to an in-core struct with lmm_ost_count indicating
2359  * the maximum number of OST indices which will fit in the user buffer.
2360  * lmm_magic must be LOV_MAGIC (we only use 1 slot here).
2361  */
2362 static int osc_getstripe(struct lov_stripe_md *lsm, struct lov_user_md *lump)
2363 {
2364         struct lov_user_md lum;
2365         struct lov_mds_md *lmmk;
2366         int rc, lmm_size;
2367         ENTRY;
2368
2369         if (!lsm)
2370                 RETURN(-ENODATA);
2371
2372         rc = copy_from_user(&lum, lump, sizeof(lum));
2373         if (rc)
2374                 RETURN(-EFAULT);
2375
2376         if (lum.lmm_magic != LOV_USER_MAGIC)
2377                 RETURN(-EINVAL);
2378
2379         if (lum.lmm_stripe_count < 1)
2380                 RETURN(-EOVERFLOW);
2381
2382         lmm_size = sizeof(lum) + sizeof(lum.lmm_objects[0]);
2383         OBD_ALLOC(lmmk, lmm_size);
2384         if (!lmmk)
2385                 RETURN(-ENOMEM);
2386
2387         lmmk->lmm_stripe_count = 1;
2388         lmmk->lmm_object_id = lsm->lsm_object_id;
2389         lmmk->lmm_objects[0].l_object_id = lsm->lsm_object_id;
2390
2391         if (copy_to_user(lump, lmmk, lmm_size))
2392                 rc = -EFAULT;
2393
2394         OBD_FREE(lmmk, lmm_size);
2395
2396         RETURN(rc);
2397 }
2398
2399 static int osc_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
2400                          void *karg, void *uarg)
2401 {
2402         struct obd_device *obd = exp->exp_obd;
2403         struct obd_ioctl_data *data = karg;
2404         int err = 0;
2405         ENTRY;
2406
2407         switch (cmd) {
2408         case OBD_IOC_LOV_GET_CONFIG: {
2409                 char *buf;
2410                 struct lov_desc *desc;
2411                 struct obd_uuid uuid;
2412
2413                 buf = NULL;
2414                 len = 0;
2415                 if (obd_ioctl_getdata(&buf, &len, (void *)uarg))
2416                         GOTO(out, err = -EINVAL);
2417
2418                 data = (struct obd_ioctl_data *)buf;
2419
2420                 if (sizeof(*desc) > data->ioc_inllen1) {
2421                         OBD_FREE(buf, len);
2422                         GOTO(out, err = -EINVAL);
2423                 }
2424
2425                 if (data->ioc_inllen2 < sizeof(uuid)) {
2426                         OBD_FREE(buf, len);
2427                         GOTO(out, err = -EINVAL);
2428                 }
2429
2430                 desc = (struct lov_desc *)data->ioc_inlbuf1;
2431                 desc->ld_tgt_count = 1;
2432                 desc->ld_active_tgt_count = 1;
2433                 desc->ld_default_stripe_count = 1;
2434                 desc->ld_default_stripe_size = 0;
2435                 desc->ld_default_stripe_offset = 0;
2436                 desc->ld_pattern = 0;
2437                 memcpy(&desc->ld_uuid, &obd->obd_uuid, sizeof(uuid));
2438
2439                 memcpy(data->ioc_inlbuf2, &obd->obd_uuid, sizeof(uuid));
2440
2441                 err = copy_to_user((void *)uarg, buf, len);
2442                 if (err)
2443                         err = -EFAULT;
2444                 obd_ioctl_freedata(buf, len);
2445                 GOTO(out, err);
2446         }
2447         case LL_IOC_LOV_SETSTRIPE:
2448                 err = obd_alloc_memmd(exp, karg);
2449                 if (err > 0)
2450                         err = 0;
2451                 GOTO(out, err);
2452         case LL_IOC_LOV_GETSTRIPE:
2453                 err = osc_getstripe(karg, uarg);
2454                 GOTO(out, err);
2455         case OBD_IOC_CLIENT_RECOVER:
2456                 err = ptlrpc_recover_import(obd->u.cli.cl_import,
2457                                             data->ioc_inlbuf1);
2458                 if (err > 0)
2459                         err = 0;
2460                 GOTO(out, err);
2461         case IOC_OSC_SET_ACTIVE:
2462                 err = ptlrpc_set_import_active(obd->u.cli.cl_import,
2463                                                data->ioc_offset);
2464                 GOTO(out, err);
2465         default:
2466                 CDEBUG(D_INODE, "unrecognised ioctl %#x by %s\n", cmd, current->comm);
2467                 GOTO(out, err = -ENOTTY);
2468         }
2469 out:
2470         return err;
2471 }
2472
2473 static int osc_get_info(struct obd_export *exp, obd_count keylen,
2474                         void *key, __u32 *vallen, void *val)
2475 {
2476         ENTRY;
2477         if (!vallen || !val)
2478                 RETURN(-EFAULT);
2479
2480         if (keylen > strlen("lock_to_stripe") &&
2481             strcmp(key, "lock_to_stripe") == 0) {
2482                 __u32 *stripe = val;
2483                 *vallen = sizeof(*stripe);
2484                 *stripe = 0;
2485                 RETURN(0);
2486         } else if (keylen >= strlen("last_id") && strcmp(key, "last_id") == 0) {
2487                 struct ptlrpc_request *req;
2488                 obd_id *reply;
2489                 char *bufs[1] = {key};
2490                 int rc;
2491                 req = ptlrpc_prep_req(class_exp2cliimp(exp), OST_GET_INFO, 1,
2492                                       &keylen, bufs);
2493                 if (req == NULL)
2494                         RETURN(-ENOMEM);
2495
2496                 req->rq_replen = lustre_msg_size(1, vallen);
2497                 rc = ptlrpc_queue_wait(req);
2498                 if (rc)
2499                         GOTO(out, rc);
2500
2501                 reply = lustre_swab_repbuf(req, 0, sizeof(*reply),
2502                                            lustre_swab_ost_last_id);
2503                 if (reply == NULL) {
2504                         CERROR("Can't unpack OST last ID\n");
2505                         GOTO(out, rc = -EPROTO);
2506                 }
2507                 *((obd_id *)val) = *reply;
2508         out:
2509                 ptlrpc_req_finished(req);
2510                 RETURN(rc);
2511         }
2512         RETURN(-EINVAL);
2513 }
2514
2515 static int osc_set_info(struct obd_export *exp, obd_count keylen,
2516                         void *key, obd_count vallen, void *val)
2517 {
2518         struct ptlrpc_request *req;
2519         struct obd_import *imp = class_exp2cliimp(exp);
2520         struct llog_ctxt *ctxt;
2521         int rc, size = keylen;
2522         char *bufs[1] = {key};
2523         ENTRY;
2524
2525         if (keylen == strlen("next_id") &&
2526             memcmp(key, "next_id", strlen("next_id")) == 0) {
2527                 if (vallen != sizeof(obd_id))
2528                         RETURN(-EINVAL);
2529                 exp->u.eu_osc_data.oed_oscc.oscc_next_id = *((obd_id*)val) + 1;
2530                 CDEBUG(D_INODE, "%s: set oscc_next_id = "LPU64"\n",
2531                        exp->exp_obd->obd_name,
2532                        exp->u.eu_osc_data.oed_oscc.oscc_next_id);
2533
2534                 RETURN(0);
2535         }
2536
2537         if (keylen == strlen("growth_count") &&
2538             memcmp(key, "growth_count", strlen("growth_count")) == 0) {
2539                 if (vallen != sizeof(int))
2540                         RETURN(-EINVAL);
2541                 exp->u.eu_osc_data.oed_oscc.oscc_grow_count = *((int*)val);
2542                 RETURN(0);
2543         }
2544
2545         if (keylen == strlen("unlinked") &&
2546             memcmp(key, "unlinked", keylen) == 0) {
2547                 struct osc_creator *oscc = &exp->u.eu_osc_data.oed_oscc;
2548                 spin_lock(&oscc->oscc_lock);
2549                 oscc->oscc_flags &= ~OSCC_FLAG_NOSPC;
2550                 spin_unlock(&oscc->oscc_lock);
2551                 RETURN(0);
2552         }
2553
2554         if (keylen < strlen("mds_conn") ||
2555             memcmp(key, "mds_conn", strlen("mds_conn")) != 0)
2556                 RETURN(-EINVAL);
2557
2558
2559         req = ptlrpc_prep_req(imp, OST_SET_INFO, 1, &size, bufs);
2560         if (req == NULL)
2561                 RETURN(-ENOMEM);
2562
2563         req->rq_replen = lustre_msg_size(0, NULL);
2564         rc = ptlrpc_queue_wait(req);
2565         ptlrpc_req_finished(req);
2566
2567         ctxt = llog_get_context(exp->exp_obd, LLOG_UNLINK_ORIG_CTXT);
2568         if (ctxt) {
2569                 rc = llog_initiator_connect(ctxt);
2570                 if (rc)
2571                         RETURN(rc);
2572         }
2573
2574         imp->imp_server_timeout = 1;
2575         CDEBUG(D_HA, "pinging OST %s\n", imp->imp_target_uuid.uuid);
2576         ptlrpc_pinger_add_import(imp);
2577
2578         RETURN(rc);
2579 }
2580
2581
2582 static struct llog_operations osc_size_repl_logops = {
2583         lop_cancel: llog_obd_repl_cancel
2584 };
2585
2586 static struct llog_operations osc_unlink_orig_logops;
2587 static int osc_llog_init(struct obd_device *obd, struct obd_device *tgt,
2588                         int count, struct llog_logid *logid)
2589 {
2590         int rc;
2591         ENTRY;
2592
2593         osc_unlink_orig_logops = llog_lvfs_ops;
2594         osc_unlink_orig_logops.lop_setup = llog_obd_origin_setup;
2595         osc_unlink_orig_logops.lop_cleanup = llog_obd_origin_cleanup;
2596         osc_unlink_orig_logops.lop_add = llog_obd_origin_add;
2597         osc_unlink_orig_logops.lop_connect = llog_origin_connect;
2598
2599         rc = llog_setup(obd, LLOG_UNLINK_ORIG_CTXT, tgt, count, logid,
2600                         &osc_unlink_orig_logops);
2601         if (rc)
2602                 RETURN(rc);
2603
2604         rc = llog_setup(obd, LLOG_SIZE_REPL_CTXT, tgt, count, NULL,
2605                         &osc_size_repl_logops);
2606         RETURN(rc);
2607 }
2608
2609 static int osc_llog_finish(struct obd_device *obd, int count)
2610 {
2611         int rc;
2612         ENTRY;
2613
2614         rc = llog_cleanup(llog_get_context(obd, LLOG_UNLINK_ORIG_CTXT));
2615         if (rc)
2616                 RETURN(rc);
2617
2618         rc = llog_cleanup(llog_get_context(obd, LLOG_SIZE_REPL_CTXT));
2619         RETURN(rc);
2620 }
2621
2622
2623 static int osc_connect(struct lustre_handle *exph,
2624                        struct obd_device *obd, struct obd_uuid *cluuid)
2625 {
2626         int rc;
2627         struct obd_export *exp;
2628
2629         rc = client_connect_import(exph, obd, cluuid);
2630
2631         if (obd->u.cli.cl_conn_count == 1) {
2632                 exp = class_conn2export(exph);
2633                 oscc_init(exp);
2634         }
2635
2636         return rc;
2637 }
2638
2639 static int osc_disconnect(struct obd_export *exp, int flags)
2640 {
2641         struct obd_device *obd = class_exp2obd(exp);
2642         struct llog_ctxt *ctxt = llog_get_context(obd, LLOG_SIZE_REPL_CTXT);
2643         int rc;
2644
2645         if (obd->u.cli.cl_conn_count == 1) {
2646                 /* flush any remaining cancel messages out to the target */
2647                 llog_sync(ctxt, exp);
2648                 
2649                 /* balance the conn2export for oscc in osc_connect */
2650                 class_export_put(exp);
2651         }
2652
2653         rc = client_disconnect_export(exp, flags);
2654         return rc;
2655 }
2656
2657 static int osc_lock_contains(struct obd_export *exp, struct lov_stripe_md *lsm,
2658                              struct ldlm_lock *lock, obd_off offset)
2659 {
2660         ENTRY;
2661         if (exp == NULL)
2662                 RETURN(-ENODEV);
2663
2664         if (lock->l_policy_data.l_extent.start <= offset &&
2665             lock->l_policy_data.l_extent.end >= offset)
2666                 RETURN(1);
2667         RETURN(0);
2668 }
2669
2670 static int osc_invalidate_import(struct obd_device *obd,
2671                                  struct obd_import *imp)
2672 {
2673         struct client_obd *cli;
2674         LASSERT(imp->imp_obd == obd);
2675         /* this used to try and tear down queued pages, but it was
2676          * not correctly implemented.  We'll have to do it again once
2677          * we call obd_invalidate_import() agian */
2678         /* XXX And we still need to do this */
2679
2680         /* Reset grants, too */
2681         cli = &obd->u.cli;
2682         spin_lock(&cli->cl_loi_list_lock);
2683         cli->cl_ost_can_grant = cli->cl_dirty_granted = 0;
2684         spin_unlock(&cli->cl_loi_list_lock);
2685         
2686         RETURN(0);
2687 }
2688
2689 int osc_setup(struct obd_device *obd, obd_count len, void *buf)
2690 {
2691         int rc;
2692         
2693         rc = ptlrpcd_addref();
2694         if (rc)
2695                 return rc;
2696
2697         rc = client_obd_setup(obd, len, buf);
2698         if (rc)
2699                 ptlrpcd_decref();
2700         RETURN(rc);
2701 }
2702
2703 int osc_cleanup(struct obd_device *obd, int flags)
2704 {
2705         int rc;
2706
2707         rc = client_obd_cleanup(obd, flags);
2708         ptlrpcd_decref();
2709         RETURN(rc);
2710 }
2711
2712
2713 struct obd_ops osc_obd_ops = {
2714         o_owner:        THIS_MODULE,
2715         o_attach:       osc_attach,
2716         o_detach:       osc_detach,
2717         o_setup:        osc_setup,
2718         o_cleanup:      osc_cleanup,
2719         o_connect:      osc_connect,
2720         o_disconnect:   osc_disconnect,
2721         o_statfs:       osc_statfs,
2722         o_packmd:       osc_packmd,
2723         o_unpackmd:     osc_unpackmd,
2724         o_create:       osc_create,
2725         o_destroy:      osc_destroy,
2726         o_getattr:      osc_getattr,
2727         o_getattr_async:osc_getattr_async,
2728         o_setattr:      osc_setattr,
2729         o_brw:          osc_brw,
2730         o_brw_async:    osc_brw_async,
2731         .o_prep_async_page =            osc_prep_async_page,
2732         .o_queue_async_io =             osc_queue_async_io,
2733         .o_set_async_flags =            osc_set_async_flags,
2734         .o_queue_sync_io =              osc_queue_sync_io,
2735         .o_trigger_sync_io =            osc_trigger_sync_io,
2736         .o_teardown_async_page =        osc_teardown_async_page,
2737         o_punch:        osc_punch,
2738         o_sync:         osc_sync,
2739         o_enqueue:      osc_enqueue,
2740         o_match:        osc_match,
2741         o_change_cbdata:osc_change_cbdata,
2742         o_cancel:       osc_cancel,
2743         o_cancel_unused:osc_cancel_unused,
2744         o_iocontrol:    osc_iocontrol,
2745         o_get_info:     osc_get_info,
2746         o_set_info:     osc_set_info,
2747         o_lock_contains:osc_lock_contains,
2748         o_invalidate_import: osc_invalidate_import,
2749         o_llog_init:    osc_llog_init,
2750         o_llog_finish:  osc_llog_finish,
2751 };
2752
2753 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
2754 struct obd_ops sanosc_obd_ops = {
2755         o_owner:        THIS_MODULE,
2756         o_attach:       osc_attach,
2757         o_detach:       osc_detach,
2758         o_cleanup:      client_obd_cleanup,
2759         o_connect:      osc_connect,
2760         o_disconnect:   client_disconnect_export,
2761         o_statfs:       osc_statfs,
2762         o_packmd:       osc_packmd,
2763         o_unpackmd:     osc_unpackmd,
2764         o_create:       osc_real_create,
2765         o_destroy:      osc_destroy,
2766         o_getattr:      osc_getattr,
2767         o_getattr_async:osc_getattr_async,
2768         o_setattr:      osc_setattr,
2769         o_setup:        client_sanobd_setup,
2770         o_brw:          sanosc_brw,
2771         o_punch:        osc_punch,
2772         o_sync:         osc_sync,
2773         o_enqueue:      osc_enqueue,
2774         o_match:        osc_match,
2775         o_change_cbdata:osc_change_cbdata,
2776         o_cancel:       osc_cancel,
2777         o_cancel_unused:osc_cancel_unused,
2778         o_iocontrol:    osc_iocontrol,
2779         o_lock_contains:osc_lock_contains,
2780         o_invalidate_import: osc_invalidate_import,
2781         o_llog_init:    osc_llog_init,
2782         o_llog_finish:  osc_llog_finish,
2783 };
2784 #endif
2785
2786 int __init osc_init(void)
2787 {
2788         struct lprocfs_static_vars lvars, sanlvars;
2789         int rc;
2790         ENTRY;
2791
2792         lprocfs_init_vars(osc, &lvars);
2793         lprocfs_init_vars(osc, &sanlvars);
2794
2795         rc = class_register_type(&osc_obd_ops, lvars.module_vars,
2796                                  LUSTRE_OSC_NAME);
2797         if (rc)
2798                 RETURN(rc);
2799
2800 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
2801         rc = class_register_type(&sanosc_obd_ops, sanlvars.module_vars,
2802                                  LUSTRE_SANOSC_NAME);
2803         if (rc)
2804                 class_unregister_type(LUSTRE_OSC_NAME);
2805 #endif
2806
2807         RETURN(rc);
2808 }
2809
2810 static void /*__exit*/ osc_exit(void)
2811 {
2812 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
2813         class_unregister_type(LUSTRE_SANOSC_NAME);
2814 #endif
2815         class_unregister_type(LUSTRE_OSC_NAME);
2816 }
2817
2818 #ifdef __KERNEL__
2819 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
2820 MODULE_DESCRIPTION("Lustre Object Storage Client (OSC)");
2821 MODULE_LICENSE("GPL");
2822
2823 module_init(osc_init);
2824 module_exit(osc_exit);
2825 #endif