Whamcloud - gitweb
b=2252
[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                         int rc = ops->ap_make_ready(oap->oap_caller_data, cmd);
1270                         if (rc < 0)
1271                                 CDEBUG(D_INODE, "oap %p page %p returned %d "
1272                                                 "instead of ready\n", oap, 
1273                                                 oap->oap_page, rc);
1274                         switch (rc) {
1275                         case -EAGAIN:
1276                                 /* llite is telling us that the page is still
1277                                  * in commit_write and that we should try
1278                                  * and put it in an rpc again later.  we 
1279                                  * break out of the loop so we don't create
1280                                  * a whole in the sequence of pages in 
1281                                  * the rpc stream.*/
1282                                 pos = NULL;
1283                                 break;
1284                         case -EINTR:
1285                                 /* the io isn't needed.. tell the checks
1286                                  * below to complete the rpc with EINTR */
1287                                 oap->oap_async_flags |= ASYNC_COUNT_STABLE;
1288                                 oap->oap_count = -EINTR;
1289                                 break;
1290                         case 0:
1291                                 oap->oap_async_flags |= ASYNC_READY;
1292                                 break;
1293                         default:
1294                                 LASSERTF(0, "oap %p page %p returned %d "
1295                                             "from make_ready\n", oap, 
1296                                             oap->oap_page, rc);
1297                                 break;
1298                         }
1299                 }
1300                 if (pos == NULL)
1301                         break;
1302
1303                 /* take the page out of our book-keeping */
1304                 list_del_init(&oap->oap_pending_item);
1305                 lop_update_pending(cli, lop, cmd, -1);
1306                 if (!list_empty(&oap->oap_urgent_item))
1307                         list_del_init(&oap->oap_urgent_item);
1308
1309                 /* ask the caller for the size of the io as the rpc leaves. */
1310                 if (!(oap->oap_async_flags & ASYNC_COUNT_STABLE))
1311                         oap->oap_count = ops->ap_refresh_count(
1312                                                         oap->oap_caller_data,
1313                                                         cmd);
1314                 if (oap->oap_count <= 0) {
1315                         CDEBUG(D_INODE, "oap %p count %d, completing\n", oap,
1316                                oap->oap_count);
1317                         osc_complete_oap(cli, oap, oap->oap_count);
1318                         continue;
1319                 }
1320
1321                 /* now put the page back in our accounting */
1322                 list_add_tail(&oap->oap_rpc_item, &rpc_list);
1323                 if (++page_count >= cli->cl_max_pages_per_rpc)
1324                         break;
1325         }
1326
1327         if (page_count == 0)
1328                 RETURN(0);
1329
1330         spin_unlock(&cli->cl_loi_list_lock);
1331
1332         request = osc_build_req(cli, &rpc_list, page_count, cmd);
1333         if (IS_ERR(request)) {
1334                 /* this should happen rarely and is pretty bad, it makes the
1335                  * pending list not follow the dirty order */
1336                 spin_lock(&cli->cl_loi_list_lock);
1337                 list_for_each_safe(pos, tmp, &rpc_list) {
1338                         oap = list_entry(pos, struct osc_async_page,
1339                                          oap_rpc_item);
1340                         list_del_init(&oap->oap_rpc_item);
1341                         list_add_tail(&oap->oap_pending_item,
1342                                       &lop->lop_pending);
1343                         lop_update_pending(cli, lop, cmd, 1);
1344                         if (oap->oap_async_flags & ASYNC_URGENT)
1345                                 list_add(&oap->oap_urgent_item,
1346                                          &lop->lop_urgent);
1347                 }
1348                 RETURN(PTR_ERR(request));
1349         }
1350
1351         LASSERT(sizeof(*aa) <= sizeof(request->rq_async_args));
1352         aa = (struct osc_brw_async_args *)&request->rq_async_args;
1353         INIT_LIST_HEAD(&aa->aa_oaps);
1354         list_splice(&rpc_list, &aa->aa_oaps);
1355         INIT_LIST_HEAD(&rpc_list);
1356
1357         if (cmd == OBD_BRW_READ)
1358                 lprocfs_oh_tally_log2(&cli->cl_read_page_hist, page_count);
1359         else 
1360                 lprocfs_oh_tally_log2(&cli->cl_write_page_hist, page_count);
1361
1362         spin_lock(&cli->cl_loi_list_lock);
1363         if (cmd == OBD_BRW_READ)
1364                 lprocfs_oh_tally(&cli->cl_read_rpc_hist, cli->cl_brw_in_flight);
1365         else 
1366                 lprocfs_oh_tally(&cli->cl_write_rpc_hist, 
1367                                  cli->cl_brw_in_flight);
1368
1369         cli->cl_brw_in_flight++;
1370         CDEBUG(D_INODE, "req %p: %d pages, aa %p.  now %d in flight\n", request,
1371                page_count, aa, cli->cl_brw_in_flight);
1372
1373         request->rq_interpret_reply = brw_interpret_oap;
1374         ptlrpcd_add_req(request);
1375         RETURN(1);
1376 }
1377
1378 static int lop_makes_rpc(struct client_obd *cli, struct loi_oap_pages *lop,
1379                          int cmd)
1380 {
1381         int optimal;
1382         ENTRY;
1383
1384         /* stream rpcs until we complete the urgent pages in the object */
1385         if (!list_empty(&lop->lop_urgent))
1386                 RETURN(1);
1387
1388         /* fire off rpcs when we have 'optimal' rpcs as tuned for the wire. */
1389         optimal = cli->cl_max_pages_per_rpc;
1390         /* *2 to avoid triggering rpcs that would want to include pages that
1391          * are being queued but which can't be made ready until the queuer
1392          * finishes with the page. this is a wart for llite::commit_write() */
1393         if (cmd == OBD_BRW_WRITE)
1394                 optimal *= 2;
1395         if (lop->lop_num_pending >= optimal)
1396                 RETURN(1);
1397
1398         /* trigger a write rpc stream as long as there are dirtiers waiting
1399          * for space.  as they're waiting, they're not going to create more
1400          * pages to coallesce with what's waiting.. */
1401         if (!list_empty(&cli->cl_cache_waiters))
1402                 RETURN(1);
1403
1404         RETURN(0);
1405 }
1406
1407 static int loi_makes_rpc(struct client_obd *cli, struct lov_oinfo *loi)
1408 {
1409         return lop_makes_rpc(cli, &loi->loi_write_lop, OBD_BRW_WRITE) ||
1410                lop_makes_rpc(cli, &loi->loi_read_lop, OBD_BRW_READ);
1411 }
1412
1413 static void loi_onto_ready_list(struct client_obd *cli, struct lov_oinfo *loi)
1414 {
1415         if (list_empty(&loi->loi_cli_item) && loi_makes_rpc(cli, loi))
1416                 list_add_tail(&loi->loi_cli_item, &cli->cl_loi_ready_list);
1417 }
1418
1419 #define LOI_DEBUG(LOI, STR, args...) \
1420         CDEBUG(D_INODE, "loi rdy %d [%p,%p] wr %d:%d rd %d:%d " STR, \
1421                !list_empty(&(LOI)->loi_cli_item),                  \
1422                (LOI)->loi_cli_item.next,                  \
1423                (LOI)->loi_cli_item.prev,                  \
1424                (LOI)->loi_write_lop.lop_num_pending,                     \
1425                !list_empty(&(LOI)->loi_write_lop.lop_urgent),         \
1426                (LOI)->loi_read_lop.lop_num_pending,                      \
1427                !list_empty(&(LOI)->loi_read_lop.lop_urgent),         \
1428                args)                       \
1429
1430 /* called with the loi list lock held */
1431 static void osc_check_rpcs(struct client_obd *cli)
1432 {
1433         struct lov_oinfo *loi;
1434         int rc = 0, making_progress;
1435         ENTRY;
1436
1437         if (list_empty(&cli->cl_loi_ready_list)) {
1438                 CDEBUG(D_INODE, "no lois ready\n");
1439                 EXIT;
1440                 return;
1441         }
1442
1443         while (!list_empty(&cli->cl_loi_ready_list)) {
1444                 loi = list_entry(cli->cl_loi_ready_list.next, struct lov_oinfo,
1445                                  loi_cli_item);
1446
1447                 if (cli->cl_brw_in_flight >= cli->cl_max_rpcs_in_flight)
1448                         break;
1449
1450                 making_progress = 0;
1451
1452                 /* hmm, it occurs to me that having rpc preparation fail
1453                  * with num_pending == num_urgent means that there won't
1454                  * be any more calls into here unless other traffic comes
1455                  * in.  hmm. */
1456
1457                 /* attempt some read/write balancing by alternating between
1458                  * reads and writes in an object */
1459                 if (lop_makes_rpc(cli, &loi->loi_write_lop, OBD_BRW_WRITE)) {
1460                         rc = osc_send_oap_rpc(cli, OBD_BRW_WRITE,
1461                                               &loi->loi_write_lop);
1462                         if (rc < 0)
1463                                 break;
1464                         if (rc > 0)
1465                                 making_progress++;
1466                 }
1467                 if (lop_makes_rpc(cli, &loi->loi_read_lop, OBD_BRW_READ)) {
1468                         rc = osc_send_oap_rpc(cli, OBD_BRW_READ,
1469                                               &loi->loi_read_lop);
1470                         if (rc < 0)
1471                                 break;
1472                         if (rc > 0)
1473                                 making_progress++;
1474                 }
1475
1476                 /* attempt some inter-object balancing by issueing rpcs
1477                  * for each object in turn */
1478                 if (!list_empty(&loi->loi_cli_item))
1479                         list_del_init(&loi->loi_cli_item);
1480
1481                 loi_onto_ready_list(cli, loi);
1482
1483                 LOI_DEBUG(loi, "mp %d\n", making_progress);
1484
1485                 /* could be smarter, !making_progress can happen in theory
1486                  * if all the pages can not be locked in set_io_ready */
1487                 if (!making_progress)
1488                         break;
1489         }
1490         EXIT;
1491 }
1492
1493 /* we're trying to queue a page in the osc so we're subject to the
1494  * 'cl_dirty_max' limit on the number of pages that can be queued in the osc.
1495  * If the osc's queued pages are already at that limit, then we want to sleep
1496  * until there is space in the osc's queue for us.  we need this goofy
1497  * little struct to really tell that our allocation was fulfilled in
1498  * the presence of pending signals */
1499 struct osc_cache_waiter {
1500         struct list_head        ocw_entry;
1501         wait_queue_head_t       ocw_waitq;
1502         int                     ocw_rc;
1503 };
1504 static int ocw_granted(struct client_obd *cli, struct osc_cache_waiter *ocw)
1505 {
1506         int rc;
1507         ENTRY;
1508         spin_lock(&cli->cl_loi_list_lock);
1509         rc = list_empty(&ocw->ocw_entry);
1510         spin_unlock(&cli->cl_loi_list_lock);
1511         RETURN(rc);
1512 };
1513
1514 static inline obd_size osc_cache_cap(struct client_obd *cli)
1515 {
1516         if (cli->cl_ost_can_grant)
1517                 return min(cli->cl_dirty_granted, cli->cl_dirty_max);
1518
1519         return cli->cl_dirty_max;
1520 }
1521 void osc_adjust_cache(struct client_obd *cli)
1522 {
1523         struct list_head *l, *tmp;
1524         struct osc_cache_waiter *ocw;
1525         obd_size cache_cap = osc_cache_cap(cli);
1526
1527         ENTRY;
1528
1529         list_for_each_safe(l, tmp, &cli->cl_cache_waiters) {
1530                 if (cli->cl_dirty + PAGE_SIZE > cache_cap &&
1531                     cache_cap >= PAGE_SIZE)
1532                         break;
1533
1534                 ocw = list_entry(l, struct osc_cache_waiter, ocw_entry);
1535                 list_del_init(&ocw->ocw_entry);
1536                 if (cache_cap < PAGE_SIZE) {
1537                 /* "They" said we are starting synchronous operations, so
1538                    wakeup everybody waiting for pages in cache and make them
1539                    go away unsatisfied. */
1540                         ocw->ocw_rc = -EDQUOT;
1541                 } else {
1542                         cli->cl_dirty += PAGE_SIZE;
1543                 }
1544                 wake_up(&ocw->ocw_waitq);
1545         }
1546
1547         EXIT;
1548 }
1549 static int osc_enter_cache(struct client_obd *cli, struct lov_oinfo *loi,
1550                            struct osc_async_page *oap)
1551 {
1552         struct osc_cache_waiter ocw;
1553         struct l_wait_info lwi = {0};
1554         int rc = 0;
1555         ENTRY;
1556
1557         if (osc_cache_cap(cli) < PAGE_SIZE)
1558                 RETURN(-EDQUOT);
1559
1560         /* if we fail this test then cl_dirty contains at least one page
1561          * that will have to be completed after we release the lock */
1562         if (cli->cl_dirty + PAGE_SIZE <= osc_cache_cap(cli)) {
1563                 /* account for ourselves */
1564                 cli->cl_dirty += PAGE_SIZE;
1565                 GOTO(out, rc = 0);
1566         }
1567
1568         init_waitqueue_head(&ocw.ocw_waitq);
1569         ocw.ocw_rc = 0;
1570         list_add_tail(&ocw.ocw_entry, &cli->cl_cache_waiters);
1571
1572         /* make sure that there are write rpcs in flight to wait for. this
1573          * is a little silly as this object may not have any pending
1574          * but other objects sure might. this should probably be cleaned. */
1575         loi_onto_ready_list(cli, loi);
1576         osc_check_rpcs(cli);
1577         spin_unlock(&cli->cl_loi_list_lock);
1578
1579         CDEBUG(D_INODE, "sleeping for cache space\n");
1580         l_wait_event(ocw.ocw_waitq, ocw_granted(cli, &ocw), &lwi);
1581
1582         spin_lock(&cli->cl_loi_list_lock);
1583         rc = ocw.ocw_rc;
1584         if (!list_empty(&ocw.ocw_entry)) {
1585                 rc = -EINTR;
1586                 list_del(&ocw.ocw_entry);
1587         }
1588         GOTO(out, rc);
1589 out:
1590         if (rc == 0)
1591                 oap->oap_brw_flags |= OBD_BRW_FROM_GRANT;
1592         return rc;
1593 }
1594
1595 /* the companion to enter_cache, called when an oap is now longer part of the
1596  * dirty accounting.. so writeback completes or truncate happens before writing
1597  * starts.  must be called with the loi lock held. */
1598 static void osc_exit_cache(struct client_obd *cli, struct osc_async_page *oap)
1599 {
1600         struct osc_cache_waiter *ocw;
1601         ENTRY;
1602
1603         if (!(oap->oap_brw_flags & OBD_BRW_FROM_GRANT)) {
1604                 EXIT;
1605                 return;
1606         }
1607
1608         /* If nobody waits for cache space or if we need to shrink it */
1609         if (list_empty(&cli->cl_cache_waiters) ||
1610             (cli->cl_dirty > osc_cache_cap(cli))) {
1611                 cli->cl_dirty -= PAGE_SIZE;
1612         } else {
1613                 ocw = list_entry(cli->cl_cache_waiters.next,
1614                                  struct osc_cache_waiter, ocw_entry);
1615                 list_del_init(&ocw->ocw_entry);
1616                 wake_up(&ocw->ocw_waitq);
1617         }
1618
1619         oap->oap_brw_flags &= ~OBD_BRW_FROM_GRANT;
1620         EXIT;
1621 }
1622
1623 int osc_prep_async_page(struct obd_export *exp, struct lov_stripe_md *lsm,
1624                         struct lov_oinfo *loi, struct page *page,
1625                         obd_off offset, struct obd_async_page_ops *ops,
1626                         void *data, void **res)
1627 {
1628         struct osc_async_page *oap;
1629         ENTRY;
1630
1631         OBD_ALLOC(oap, sizeof(*oap));
1632         if (oap == NULL)
1633                 return -ENOMEM;
1634
1635         oap->oap_magic = OAP_MAGIC;
1636         oap->oap_caller_ops = ops;
1637         oap->oap_caller_data = data;
1638
1639         oap->oap_page = page;
1640         oap->oap_obj_off = offset;
1641
1642         INIT_LIST_HEAD(&oap->oap_pending_item);
1643         INIT_LIST_HEAD(&oap->oap_urgent_item);
1644         INIT_LIST_HEAD(&oap->oap_rpc_item);
1645
1646         CDEBUG(D_CACHE, "oap %p page %p obj off "LPU64"\n", oap, page, offset);
1647         *res = oap;
1648         RETURN(0);
1649 }
1650
1651 struct osc_async_page *oap_from_cookie(void *cookie)
1652 {
1653         struct osc_async_page *oap = cookie;
1654         if (oap->oap_magic != OAP_MAGIC)
1655                 return ERR_PTR(-EINVAL);
1656         return oap;
1657 };
1658
1659 static int osc_queue_async_io(struct obd_export *exp, struct lov_stripe_md *lsm,
1660                               struct lov_oinfo *loi, void *cookie,
1661                               int cmd, obd_off off, int count,
1662                               obd_flag brw_flags, enum async_flags async_flags)
1663 {
1664         struct client_obd *cli = &exp->exp_obd->u.cli;
1665         struct osc_async_page *oap;
1666         struct loi_oap_pages *lop;
1667         int rc;
1668         ENTRY;
1669
1670         oap = oap_from_cookie(cookie);
1671         if (IS_ERR(oap))
1672                 RETURN(PTR_ERR(oap));
1673
1674         if (cli->cl_import == NULL || cli->cl_import->imp_invalid)
1675                 RETURN(-EIO);
1676
1677         if (!list_empty(&oap->oap_pending_item) ||
1678             !list_empty(&oap->oap_urgent_item) ||
1679             !list_empty(&oap->oap_rpc_item))
1680                 RETURN(-EBUSY);
1681
1682         if (loi == NULL)
1683                 loi = &lsm->lsm_oinfo[0];
1684
1685         spin_lock(&cli->cl_loi_list_lock);
1686
1687         oap->oap_cmd = cmd;
1688         oap->oap_async_flags = async_flags;
1689         oap->oap_page_off = off;
1690         oap->oap_count = count;
1691         oap->oap_brw_flags = brw_flags;
1692
1693         if (cmd == OBD_BRW_WRITE) {
1694                 rc = osc_enter_cache(cli, loi, oap);
1695                 if (rc) {
1696                         spin_unlock(&cli->cl_loi_list_lock);
1697                         RETURN(rc);
1698                 }
1699                 lop = &loi->loi_write_lop;
1700         } else {
1701                 lop = &loi->loi_read_lop;
1702         }
1703
1704         if (oap->oap_async_flags & ASYNC_URGENT)
1705                 list_add(&oap->oap_urgent_item, &lop->lop_urgent);
1706         list_add_tail(&oap->oap_pending_item, &lop->lop_pending);
1707         lop_update_pending(cli, lop, cmd, 1);
1708
1709         loi_onto_ready_list(cli, loi);
1710
1711         LOI_DEBUG(loi, "oap %p page %p added for cmd %d\n", oap, oap->oap_page,
1712                   cmd);
1713
1714         osc_check_rpcs(cli);
1715         spin_unlock(&cli->cl_loi_list_lock);
1716
1717         RETURN(0);
1718 }
1719
1720 /* aka (~was & now & flag), but this is more clear :) */
1721 #define SETTING(was, now, flag) (!(was & flag) && (now & flag))
1722
1723 static int osc_set_async_flags(struct obd_export *exp,
1724                                struct lov_stripe_md *lsm,
1725                                struct lov_oinfo *loi, void *cookie,
1726                                obd_flag async_flags)
1727 {
1728         struct client_obd *cli = &exp->exp_obd->u.cli;
1729         struct loi_oap_pages *lop;
1730         struct osc_async_page *oap;
1731         int rc = 0;
1732         ENTRY;
1733
1734         oap = oap_from_cookie(cookie);
1735         if (IS_ERR(oap))
1736                 RETURN(PTR_ERR(oap));
1737
1738         if (cli->cl_import == NULL || cli->cl_import->imp_invalid)
1739                 RETURN(-EIO);
1740
1741         if (loi == NULL)
1742                 loi = &lsm->lsm_oinfo[0];
1743
1744         if (oap->oap_cmd == OBD_BRW_WRITE) {
1745                 lop = &loi->loi_write_lop;
1746         } else {
1747                 lop = &loi->loi_read_lop;
1748         }
1749
1750         spin_lock(&cli->cl_loi_list_lock);
1751
1752         if (list_empty(&oap->oap_pending_item))
1753                 GOTO(out, rc = -EINVAL);
1754
1755         if ((oap->oap_async_flags & async_flags) == async_flags)
1756                 GOTO(out, rc = 0);
1757
1758         if (SETTING(oap->oap_async_flags, async_flags, ASYNC_READY))
1759                 oap->oap_async_flags |= ASYNC_READY;
1760
1761         if (SETTING(oap->oap_async_flags, async_flags, ASYNC_URGENT)) {
1762                 if (list_empty(&oap->oap_rpc_item)) {
1763                         list_add(&oap->oap_urgent_item, &lop->lop_urgent);
1764                         loi_onto_ready_list(cli, loi);
1765                 }
1766         }
1767
1768         LOI_DEBUG(loi, "oap %p page %p has flags %x\n", oap, oap->oap_page,
1769                         oap->oap_async_flags);
1770 out:
1771         osc_check_rpcs(cli);
1772         spin_unlock(&cli->cl_loi_list_lock);
1773         RETURN(rc);
1774 }
1775
1776 static int osc_queue_sync_io(struct obd_export *exp, struct lov_stripe_md *lsm,
1777                              struct lov_oinfo *loi,
1778                              struct obd_sync_io_container *osic, void *cookie,
1779                              int cmd, obd_off off, int count,
1780                              obd_flag brw_flags)
1781 {
1782         struct client_obd *cli = &exp->exp_obd->u.cli;
1783         struct osc_async_page *oap;
1784         struct loi_oap_pages *lop;
1785         ENTRY;
1786
1787         oap = oap_from_cookie(cookie);
1788         if (IS_ERR(oap))
1789                 RETURN(PTR_ERR(oap));
1790
1791         if (cli->cl_import == NULL || cli->cl_import->imp_invalid)
1792                 RETURN(-EIO);
1793
1794         if (!list_empty(&oap->oap_pending_item) ||
1795             !list_empty(&oap->oap_urgent_item) ||
1796             !list_empty(&oap->oap_rpc_item))
1797                 RETURN(-EBUSY);
1798
1799         if (loi == NULL)
1800                 loi = &lsm->lsm_oinfo[0];
1801
1802         spin_lock(&cli->cl_loi_list_lock);
1803
1804         oap->oap_cmd = cmd;
1805         oap->oap_page_off = off;
1806         oap->oap_count = count;
1807         oap->oap_brw_flags = brw_flags;
1808
1809         if (cmd == OBD_BRW_WRITE)
1810                 lop = &loi->loi_write_lop;
1811         else
1812                 lop = &loi->loi_read_lop;
1813
1814         list_add_tail(&oap->oap_pending_item, &lop->lop_pending_sync);
1815         oap->oap_osic = osic;
1816         osic_add_one(osic);
1817
1818         LOI_DEBUG(loi, "oap %p page %p on sync pending\n", oap, oap->oap_page);
1819
1820         spin_unlock(&cli->cl_loi_list_lock);
1821
1822         RETURN(0);
1823 }
1824
1825 static void osc_sync_to_pending(struct client_obd *cli, struct lov_oinfo *loi,
1826                                 struct loi_oap_pages *lop, int cmd)
1827 {
1828         struct list_head *pos, *tmp;
1829         struct osc_async_page *oap;
1830
1831         list_for_each_safe(pos, tmp, &lop->lop_pending_sync) {
1832                 oap = list_entry(pos, struct osc_async_page, oap_pending_item);
1833                 list_del(&oap->oap_pending_item);
1834                 oap->oap_async_flags |= ASYNC_READY | ASYNC_URGENT |
1835                                         ASYNC_COUNT_STABLE;
1836                 list_add_tail(&oap->oap_pending_item, &lop->lop_pending);
1837                 list_add(&oap->oap_urgent_item, &lop->lop_urgent);
1838                 lop_update_pending(cli, lop, cmd, 1);
1839         }
1840         loi_onto_ready_list(cli, loi);
1841 }
1842
1843 static int osc_trigger_sync_io(struct obd_export *exp,
1844                                struct lov_stripe_md *lsm,
1845                                struct lov_oinfo *loi,
1846                                struct obd_sync_io_container *osic)
1847 {
1848         struct client_obd *cli = &exp->exp_obd->u.cli;
1849         ENTRY;
1850
1851         if (cli->cl_import == NULL || cli->cl_import->imp_invalid)
1852                 RETURN(-EIO);
1853
1854         if (loi == NULL)
1855                 loi = &lsm->lsm_oinfo[0];
1856
1857         spin_lock(&cli->cl_loi_list_lock);
1858
1859         osc_sync_to_pending(cli, loi, &loi->loi_write_lop, OBD_BRW_WRITE);
1860         osc_sync_to_pending(cli, loi, &loi->loi_read_lop, OBD_BRW_READ);
1861
1862         osc_check_rpcs(cli);
1863         spin_unlock(&cli->cl_loi_list_lock);
1864
1865         RETURN(0);
1866 }
1867
1868 static int osc_teardown_async_page(struct obd_export *exp,
1869                                    struct lov_stripe_md *lsm,
1870                                    struct lov_oinfo *loi, void *cookie)
1871 {
1872         struct client_obd *cli = &exp->exp_obd->u.cli;
1873         struct loi_oap_pages *lop;
1874         struct osc_async_page *oap;
1875         int rc = 0;
1876         ENTRY;
1877
1878         oap = oap_from_cookie(cookie);
1879         if (IS_ERR(oap))
1880                 RETURN(PTR_ERR(oap));
1881
1882         if (loi == NULL)
1883                 loi = &lsm->lsm_oinfo[0];
1884
1885         if (oap->oap_cmd == OBD_BRW_WRITE) {
1886                 lop = &loi->loi_write_lop;
1887         } else {
1888                 lop = &loi->loi_read_lop;
1889         }
1890
1891         spin_lock(&cli->cl_loi_list_lock);
1892
1893         osc_exit_cache(cli, oap);
1894
1895         if (!list_empty(&oap->oap_rpc_item))
1896                 GOTO(out, rc = -EBUSY);
1897
1898         if (!list_empty(&oap->oap_urgent_item)) {
1899                 list_del_init(&oap->oap_urgent_item);
1900                 oap->oap_async_flags &= ~ASYNC_URGENT;
1901         }
1902         if (!list_empty(&oap->oap_pending_item)) {
1903                 list_del_init(&oap->oap_pending_item);
1904                 lop_update_pending(cli, lop, oap->oap_cmd, -1);
1905         }
1906         if (!list_empty(&loi->loi_cli_item) && !loi_makes_rpc(cli, loi))
1907                 list_del_init(&loi->loi_cli_item);
1908
1909         LOI_DEBUG(loi, "oap %p page %p torn down\n", oap, oap->oap_page);
1910 out:
1911         spin_unlock(&cli->cl_loi_list_lock);
1912         OBD_FREE(oap, sizeof(*oap));
1913         RETURN(rc);
1914 }
1915
1916 #ifdef __KERNEL__
1917 /* Note: caller will lock/unlock, and set uptodate on the pages */
1918 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
1919 static int sanosc_brw_read(struct obd_export *exp, struct obdo *oa,
1920                            struct lov_stripe_md *lsm, obd_count page_count,
1921                            struct brw_page *pga)
1922 {
1923         struct ptlrpc_request *request = NULL;
1924         struct ost_body *body;
1925         struct niobuf_remote *nioptr;
1926         struct obd_ioobj *iooptr;
1927         int rc, size[3] = {sizeof(*body)}, mapped = 0;
1928         int swab;
1929         ENTRY;
1930
1931         /* XXX does not handle 'new' brw protocol */
1932
1933         size[1] = sizeof(struct obd_ioobj);
1934         size[2] = page_count * sizeof(*nioptr);
1935
1936         request = ptlrpc_prep_req(class_exp2cliimp(exp), OST_SAN_READ, 3,
1937                                   size, NULL);
1938         if (!request)
1939                 RETURN(-ENOMEM);
1940
1941         body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof(*body));
1942         iooptr = lustre_msg_buf(request->rq_reqmsg, 1, sizeof(*iooptr));
1943         nioptr = lustre_msg_buf(request->rq_reqmsg, 2,
1944                                 sizeof(*nioptr) * page_count);
1945
1946         memcpy(&body->oa, oa, sizeof(body->oa));
1947
1948         obdo_to_ioobj(oa, iooptr);
1949         iooptr->ioo_bufcnt = page_count;
1950
1951         for (mapped = 0; mapped < page_count; mapped++, nioptr++) {
1952                 LASSERT(PageLocked(pga[mapped].pg));
1953                 LASSERT(mapped == 0 || pga[mapped].off > pga[mapped - 1].off);
1954
1955                 nioptr->offset = pga[mapped].off;
1956                 nioptr->len    = pga[mapped].count;
1957                 nioptr->flags  = pga[mapped].flag;
1958         }
1959
1960         size[1] = page_count * sizeof(*nioptr);
1961         request->rq_replen = lustre_msg_size(2, size);
1962
1963         rc = ptlrpc_queue_wait(request);
1964         if (rc)
1965                 GOTO(out_req, rc);
1966
1967         body = lustre_swab_repbuf(request, 0, sizeof(*body),
1968                                   lustre_swab_ost_body);
1969         if (body == NULL) {
1970                 CERROR("Can't unpack body\n");
1971                 GOTO(out_req, rc = -EPROTO);
1972         }
1973
1974         memcpy(oa, &body->oa, sizeof(*oa));
1975
1976         swab = lustre_msg_swabbed(request->rq_repmsg);
1977         LASSERT_REPSWAB(request, 1);
1978         nioptr = lustre_msg_buf(request->rq_repmsg, 1, size[1]);
1979         if (!nioptr) {
1980                 /* nioptr missing or short */
1981                 GOTO(out_req, rc = -EPROTO);
1982         }
1983
1984         /* actual read */
1985         for (mapped = 0; mapped < page_count; mapped++, nioptr++) {
1986                 struct page *page = pga[mapped].pg;
1987                 struct buffer_head *bh;
1988                 kdev_t dev;
1989
1990                 if (swab)
1991                         lustre_swab_niobuf_remote (nioptr);
1992
1993                 /* got san device associated */
1994                 LASSERT(exp->exp_obd != NULL);
1995                 dev = exp->exp_obd->u.cli.cl_sandev;
1996
1997                 /* hole */
1998                 if (!nioptr->offset) {
1999                         CDEBUG(D_PAGE, "hole at ino %lu; index %ld\n",
2000                                         page->mapping->host->i_ino,
2001                                         page->index);
2002                         memset(page_address(page), 0, PAGE_SIZE);
2003                         continue;
2004                 }
2005
2006                 if (!page->buffers) {
2007                         create_empty_buffers(page, dev, PAGE_SIZE);
2008                         bh = page->buffers;
2009
2010                         clear_bit(BH_New, &bh->b_state);
2011                         set_bit(BH_Mapped, &bh->b_state);
2012                         bh->b_blocknr = (unsigned long)nioptr->offset;
2013
2014                         clear_bit(BH_Uptodate, &bh->b_state);
2015
2016                         ll_rw_block(READ, 1, &bh);
2017                 } else {
2018                         bh = page->buffers;
2019
2020                         /* if buffer already existed, it must be the
2021                          * one we mapped before, check it */
2022                         LASSERT(!test_bit(BH_New, &bh->b_state));
2023                         LASSERT(test_bit(BH_Mapped, &bh->b_state));
2024                         LASSERT(bh->b_blocknr == (unsigned long)nioptr->offset);
2025
2026                         /* wait it's io completion */
2027                         if (test_bit(BH_Lock, &bh->b_state))
2028                                 wait_on_buffer(bh);
2029
2030                         if (!test_bit(BH_Uptodate, &bh->b_state))
2031                                 ll_rw_block(READ, 1, &bh);
2032                 }
2033
2034
2035                 /* must do syncronous write here */
2036                 wait_on_buffer(bh);
2037                 if (!buffer_uptodate(bh)) {
2038                         /* I/O error */
2039                         rc = -EIO;
2040                         goto out_req;
2041                 }
2042         }
2043
2044 out_req:
2045         ptlrpc_req_finished(request);
2046         RETURN(rc);
2047 }
2048
2049 static int sanosc_brw_write(struct obd_export *exp, struct obdo *oa,
2050                             struct lov_stripe_md *lsm, obd_count page_count,
2051                             struct brw_page *pga)
2052 {
2053         struct ptlrpc_request *request = NULL;
2054         struct ost_body *body;
2055         struct niobuf_remote *nioptr;
2056         struct obd_ioobj *iooptr;
2057         int rc, size[3] = {sizeof(*body)}, mapped = 0;
2058         int swab;
2059         ENTRY;
2060
2061         size[1] = sizeof(struct obd_ioobj);
2062         size[2] = page_count * sizeof(*nioptr);
2063
2064         request = ptlrpc_prep_req(class_exp2cliimp(exp), OST_SAN_WRITE,
2065                                   3, size, NULL);
2066         if (!request)
2067                 RETURN(-ENOMEM);
2068
2069         body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof (*body));
2070         iooptr = lustre_msg_buf(request->rq_reqmsg, 1, sizeof (*iooptr));
2071         nioptr = lustre_msg_buf(request->rq_reqmsg, 2,
2072                                 sizeof (*nioptr) * page_count);
2073
2074         memcpy(&body->oa, oa, sizeof(body->oa));
2075
2076         obdo_to_ioobj(oa, iooptr);
2077         iooptr->ioo_bufcnt = page_count;
2078
2079         /* pack request */
2080         for (mapped = 0; mapped < page_count; mapped++, nioptr++) {
2081                 LASSERT(PageLocked(pga[mapped].pg));
2082                 LASSERT(mapped == 0 || pga[mapped].off > pga[mapped - 1].off);
2083
2084                 nioptr->offset = pga[mapped].off;
2085                 nioptr->len    = pga[mapped].count;
2086                 nioptr->flags  = pga[mapped].flag;
2087         }
2088
2089         size[1] = page_count * sizeof(*nioptr);
2090         request->rq_replen = lustre_msg_size(2, size);
2091
2092         rc = ptlrpc_queue_wait(request);
2093         if (rc)
2094                 GOTO(out_req, rc);
2095
2096         swab = lustre_msg_swabbed (request->rq_repmsg);
2097         LASSERT_REPSWAB (request, 1);
2098         nioptr = lustre_msg_buf(request->rq_repmsg, 1, size[1]);
2099         if (!nioptr) {
2100                 CERROR("absent/short niobuf array\n");
2101                 GOTO(out_req, rc = -EPROTO);
2102         }
2103
2104         /* actual write */
2105         for (mapped = 0; mapped < page_count; mapped++, nioptr++) {
2106                 struct page *page = pga[mapped].pg;
2107                 struct buffer_head *bh;
2108                 kdev_t dev;
2109
2110                 if (swab)
2111                         lustre_swab_niobuf_remote (nioptr);
2112
2113                 /* got san device associated */
2114                 LASSERT(exp->exp_obd != NULL);
2115                 dev = exp->exp_obd->u.cli.cl_sandev;
2116
2117                 if (!page->buffers) {
2118                         create_empty_buffers(page, dev, PAGE_SIZE);
2119                 } else {
2120                         /* checking */
2121                         LASSERT(!test_bit(BH_New, &page->buffers->b_state));
2122                         LASSERT(test_bit(BH_Mapped, &page->buffers->b_state));
2123                         LASSERT(page->buffers->b_blocknr ==
2124                                 (unsigned long)nioptr->offset);
2125                 }
2126                 bh = page->buffers;
2127
2128                 LASSERT(bh);
2129
2130                 /* if buffer locked, wait it's io completion */
2131                 if (test_bit(BH_Lock, &bh->b_state))
2132                         wait_on_buffer(bh);
2133
2134                 clear_bit(BH_New, &bh->b_state);
2135                 set_bit(BH_Mapped, &bh->b_state);
2136
2137                 /* override the block nr */
2138                 bh->b_blocknr = (unsigned long)nioptr->offset;
2139
2140                 /* we are about to write it, so set it
2141                  * uptodate/dirty
2142                  * page lock should garentee no race condition here */
2143                 set_bit(BH_Uptodate, &bh->b_state);
2144                 set_bit(BH_Dirty, &bh->b_state);
2145
2146                 ll_rw_block(WRITE, 1, &bh);
2147
2148                 /* must do syncronous write here */
2149                 wait_on_buffer(bh);
2150                 if (!buffer_uptodate(bh) || test_bit(BH_Dirty, &bh->b_state)) {
2151                         /* I/O error */
2152                         rc = -EIO;
2153                         goto out_req;
2154                 }
2155         }
2156
2157 out_req:
2158         ptlrpc_req_finished(request);
2159         RETURN(rc);
2160 }
2161
2162 static int sanosc_brw(int cmd, struct obd_export *exp, struct obdo *oa,
2163                       struct lov_stripe_md *lsm, obd_count page_count,
2164                       struct brw_page *pga, struct obd_trans_info *oti)
2165 {
2166         ENTRY;
2167
2168         while (page_count) {
2169                 obd_count pages_per_brw;
2170                 int rc;
2171
2172                 if (page_count > OSC_BRW_MAX_IOV)
2173                         pages_per_brw = OSC_BRW_MAX_IOV;
2174                 else
2175                         pages_per_brw = page_count;
2176
2177                 if (cmd & OBD_BRW_WRITE)
2178                         rc = sanosc_brw_write(exp, oa, lsm, pages_per_brw,pga);
2179                 else
2180                         rc = sanosc_brw_read(exp, oa, lsm, pages_per_brw, pga);
2181
2182                 if (rc != 0)
2183                         RETURN(rc);
2184
2185                 page_count -= pages_per_brw;
2186                 pga += pages_per_brw;
2187         }
2188         RETURN(0);
2189 }
2190 #endif
2191 #endif
2192
2193 static void osc_set_data_with_check(struct lustre_handle *lockh, void *data)
2194 {
2195         struct ldlm_lock *lock = ldlm_handle2lock(lockh);
2196
2197         LASSERT(lock != NULL);
2198         l_lock(&lock->l_resource->lr_namespace->ns_lock);
2199 #ifdef __KERNEL__
2200         if (lock->l_ast_data && lock->l_ast_data != data) {
2201                 struct inode *new_inode = data;
2202                 struct inode *old_inode = lock->l_ast_data;
2203                 unsigned long state = old_inode->i_state & I_FREEING;
2204                 CERROR("Found existing inode %p/%lu/%u state %lu in lock: "
2205                        "setting data to %p/%lu/%u\n", old_inode,
2206                        old_inode->i_ino, old_inode->i_generation, state,
2207                        new_inode, new_inode->i_ino, new_inode->i_generation);
2208                 LASSERT(state);
2209         }
2210 #endif
2211         lock->l_ast_data = data;
2212         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
2213         LDLM_LOCK_PUT(lock);
2214 }
2215
2216 static int osc_change_cbdata(struct obd_export *exp, struct lov_stripe_md *lsm,
2217                              ldlm_iterator_t replace, void *data)
2218 {
2219         struct ldlm_res_id res_id = { .name = {lsm->lsm_object_id} };
2220         struct obd_device *obd = class_exp2obd(exp);
2221
2222         ldlm_change_cbdata(obd->obd_namespace, &res_id, replace, data);
2223         return 0;
2224 }
2225
2226 static int osc_enqueue(struct obd_export *exp, struct lov_stripe_md *lsm,
2227                        struct lustre_handle *parent_lock,
2228                        __u32 type, void *extentp, int extent_len, __u32 mode,
2229                        int *flags, void *callback, void *data,
2230                        struct lustre_handle *lockh)
2231 {
2232         struct ldlm_res_id res_id = { .name = {lsm->lsm_object_id} };
2233         struct obd_device *obd = exp->exp_obd;
2234         struct ldlm_extent *extent = extentp;
2235         int rc;
2236         ENTRY;
2237
2238         /* Filesystem lock extents are extended to page boundaries so that
2239          * dealing with the page cache is a little smoother.  */
2240         extent->start -= extent->start & ~PAGE_MASK;
2241         extent->end |= ~PAGE_MASK;
2242
2243         /* Next, search for already existing extent locks that will cover us */
2244         rc = ldlm_lock_match(obd->obd_namespace, 0, &res_id,
2245                              type, extent, sizeof(*extent), mode, lockh);
2246         if (rc == 1) {
2247                 osc_set_data_with_check(lockh, data);
2248                 /* We already have a lock, and it's referenced */
2249                 RETURN(ELDLM_OK);
2250         }
2251
2252         /* If we're trying to read, we also search for an existing PW lock.  The
2253          * VFS and page cache already protect us locally, so lots of readers/
2254          * writers can share a single PW lock.
2255          *
2256          * There are problems with conversion deadlocks, so instead of
2257          * converting a read lock to a write lock, we'll just enqueue a new
2258          * one.
2259          *
2260          * At some point we should cancel the read lock instead of making them
2261          * send us a blocking callback, but there are problems with canceling
2262          * locks out from other users right now, too. */
2263
2264         if (mode == LCK_PR) {
2265                 rc = ldlm_lock_match(obd->obd_namespace, 0, &res_id, type,
2266                                      extent, sizeof(*extent), LCK_PW, lockh);
2267                 if (rc == 1) {
2268                         /* FIXME: This is not incredibly elegant, but it might
2269                          * be more elegant than adding another parameter to
2270                          * lock_match.  I want a second opinion. */
2271                         ldlm_lock_addref(lockh, LCK_PR);
2272                         ldlm_lock_decref(lockh, LCK_PW);
2273                         osc_set_data_with_check(lockh, data);
2274                         RETURN(ELDLM_OK);
2275                 }
2276         }
2277
2278         rc = ldlm_cli_enqueue(exp, NULL, obd->obd_namespace, parent_lock,
2279                               res_id, type, extent, sizeof(*extent), mode,
2280                               flags,ldlm_completion_ast, callback, data, lockh);
2281         RETURN(rc);
2282 }
2283
2284 static int osc_match(struct obd_export *exp, struct lov_stripe_md *lsm,
2285                      __u32 type, void *extentp, int extent_len, __u32 mode,
2286                      int *flags, void *data, struct lustre_handle *lockh)
2287 {
2288         struct ldlm_res_id res_id = { .name = {lsm->lsm_object_id} };
2289         struct obd_device *obd = exp->exp_obd;
2290         struct ldlm_extent *extent = extentp;
2291         int rc;
2292         ENTRY;
2293
2294         OBD_FAIL_RETURN(OBD_FAIL_OSC_MATCH, -EIO);
2295
2296         /* Filesystem lock extents are extended to page boundaries so that
2297          * dealing with the page cache is a little smoother */
2298         extent->start -= extent->start & ~PAGE_MASK;
2299         extent->end |= ~PAGE_MASK;
2300
2301         /* Next, search for already existing extent locks that will cover us */
2302         rc = ldlm_lock_match(obd->obd_namespace, *flags, &res_id, type,
2303                              extent, sizeof(*extent), mode, lockh);
2304         if (rc) {
2305                 osc_set_data_with_check(lockh, data);
2306                 RETURN(rc);
2307         }
2308         /* If we're trying to read, we also search for an existing PW lock.  The
2309          * VFS and page cache already protect us locally, so lots of readers/
2310          * writers can share a single PW lock. */
2311         if (mode == LCK_PR) {
2312                 rc = ldlm_lock_match(obd->obd_namespace, *flags, &res_id, type,
2313                                      extent, sizeof(*extent), LCK_PW, lockh);
2314                 if (rc == 1) {
2315                         /* FIXME: This is not incredibly elegant, but it might
2316                          * be more elegant than adding another parameter to
2317                          * lock_match.  I want a second opinion. */
2318                         osc_set_data_with_check(lockh, data);
2319                         ldlm_lock_addref(lockh, LCK_PR);
2320                         ldlm_lock_decref(lockh, LCK_PW);
2321                 }
2322         }
2323         RETURN(rc);
2324 }
2325
2326 static int osc_cancel(struct obd_export *exp, struct lov_stripe_md *md,
2327                       __u32 mode, struct lustre_handle *lockh)
2328 {
2329         ENTRY;
2330
2331         ldlm_lock_decref(lockh, mode);
2332
2333         RETURN(0);
2334 }
2335
2336 static int osc_cancel_unused(struct obd_export *exp,
2337                              struct lov_stripe_md *lsm, int flags, void *opaque)
2338 {
2339         struct obd_device *obd = class_exp2obd(exp);
2340         struct ldlm_res_id res_id = { .name = {lsm->lsm_object_id} };
2341
2342         return ldlm_cli_cancel_unused(obd->obd_namespace, &res_id, flags,
2343                                       opaque);
2344 }
2345
2346 static int osc_statfs(struct obd_device *obd, struct obd_statfs *osfs,
2347                       unsigned long max_age)
2348 {
2349         struct obd_statfs *msfs;
2350         struct ptlrpc_request *request;
2351         int rc, size = sizeof(*osfs);
2352         ENTRY;
2353
2354         /* We could possibly pass max_age in the request (as an absolute
2355          * timestamp or a "seconds.usec ago") so the target can avoid doing
2356          * extra calls into the filesystem if that isn't necessary (e.g.
2357          * during mount that would help a bit).  Having relative timestamps
2358          * is not so great if request processing is slow, while absolute
2359          * timestamps are not ideal because they need time synchronization. */
2360         request = ptlrpc_prep_req(obd->u.cli.cl_import, OST_STATFS,0,NULL,NULL);
2361         if (!request)
2362                 RETURN(-ENOMEM);
2363
2364         request->rq_replen = lustre_msg_size(1, &size);
2365         request->rq_request_portal = OST_CREATE_PORTAL; //XXX FIXME bug 249
2366
2367         rc = ptlrpc_queue_wait(request);
2368         if (rc)
2369                 GOTO(out, rc);
2370
2371         msfs = lustre_swab_repbuf(request, 0, sizeof(*msfs),
2372                                   lustre_swab_obd_statfs);
2373         if (msfs == NULL) {
2374                 CERROR("Can't unpack obd_statfs\n");
2375                 GOTO(out, rc = -EPROTO);
2376         }
2377
2378         memcpy(osfs, msfs, sizeof(*osfs));
2379
2380         EXIT;
2381  out:
2382         ptlrpc_req_finished(request);
2383         return rc;
2384 }
2385
2386 /* Retrieve object striping information.
2387  *
2388  * @lmmu is a pointer to an in-core struct with lmm_ost_count indicating
2389  * the maximum number of OST indices which will fit in the user buffer.
2390  * lmm_magic must be LOV_MAGIC (we only use 1 slot here).
2391  */
2392 static int osc_getstripe(struct lov_stripe_md *lsm, struct lov_user_md *lump)
2393 {
2394         struct lov_user_md lum;
2395         struct lov_mds_md *lmmk;
2396         int rc, lmm_size;
2397         ENTRY;
2398
2399         if (!lsm)
2400                 RETURN(-ENODATA);
2401
2402         rc = copy_from_user(&lum, lump, sizeof(lum));
2403         if (rc)
2404                 RETURN(-EFAULT);
2405
2406         if (lum.lmm_magic != LOV_USER_MAGIC)
2407                 RETURN(-EINVAL);
2408
2409         if (lum.lmm_stripe_count < 1)
2410                 RETURN(-EOVERFLOW);
2411
2412         lmm_size = sizeof(lum) + sizeof(lum.lmm_objects[0]);
2413         OBD_ALLOC(lmmk, lmm_size);
2414         if (!lmmk)
2415                 RETURN(-ENOMEM);
2416
2417         lmmk->lmm_stripe_count = 1;
2418         lmmk->lmm_object_id = lsm->lsm_object_id;
2419         lmmk->lmm_objects[0].l_object_id = lsm->lsm_object_id;
2420
2421         if (copy_to_user(lump, lmmk, lmm_size))
2422                 rc = -EFAULT;
2423
2424         OBD_FREE(lmmk, lmm_size);
2425
2426         RETURN(rc);
2427 }
2428
2429 static int osc_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
2430                          void *karg, void *uarg)
2431 {
2432         struct obd_device *obd = exp->exp_obd;
2433         struct obd_ioctl_data *data = karg;
2434         int err = 0;
2435         ENTRY;
2436
2437         switch (cmd) {
2438         case OBD_IOC_LOV_GET_CONFIG: {
2439                 char *buf;
2440                 struct lov_desc *desc;
2441                 struct obd_uuid uuid;
2442
2443                 buf = NULL;
2444                 len = 0;
2445                 if (obd_ioctl_getdata(&buf, &len, (void *)uarg))
2446                         GOTO(out, err = -EINVAL);
2447
2448                 data = (struct obd_ioctl_data *)buf;
2449
2450                 if (sizeof(*desc) > data->ioc_inllen1) {
2451                         OBD_FREE(buf, len);
2452                         GOTO(out, err = -EINVAL);
2453                 }
2454
2455                 if (data->ioc_inllen2 < sizeof(uuid)) {
2456                         OBD_FREE(buf, len);
2457                         GOTO(out, err = -EINVAL);
2458                 }
2459
2460                 desc = (struct lov_desc *)data->ioc_inlbuf1;
2461                 desc->ld_tgt_count = 1;
2462                 desc->ld_active_tgt_count = 1;
2463                 desc->ld_default_stripe_count = 1;
2464                 desc->ld_default_stripe_size = 0;
2465                 desc->ld_default_stripe_offset = 0;
2466                 desc->ld_pattern = 0;
2467                 memcpy(&desc->ld_uuid, &obd->obd_uuid, sizeof(uuid));
2468
2469                 memcpy(data->ioc_inlbuf2, &obd->obd_uuid, sizeof(uuid));
2470
2471                 err = copy_to_user((void *)uarg, buf, len);
2472                 if (err)
2473                         err = -EFAULT;
2474                 obd_ioctl_freedata(buf, len);
2475                 GOTO(out, err);
2476         }
2477         case LL_IOC_LOV_SETSTRIPE:
2478                 err = obd_alloc_memmd(exp, karg);
2479                 if (err > 0)
2480                         err = 0;
2481                 GOTO(out, err);
2482         case LL_IOC_LOV_GETSTRIPE:
2483                 err = osc_getstripe(karg, uarg);
2484                 GOTO(out, err);
2485         case OBD_IOC_CLIENT_RECOVER:
2486                 err = ptlrpc_recover_import(obd->u.cli.cl_import,
2487                                             data->ioc_inlbuf1);
2488                 if (err > 0)
2489                         err = 0;
2490                 GOTO(out, err);
2491         case IOC_OSC_SET_ACTIVE:
2492                 err = ptlrpc_set_import_active(obd->u.cli.cl_import,
2493                                                data->ioc_offset);
2494                 GOTO(out, err);
2495         default:
2496                 CDEBUG(D_INODE, "unrecognised ioctl %#x by %s\n", cmd, current->comm);
2497                 GOTO(out, err = -ENOTTY);
2498         }
2499 out:
2500         return err;
2501 }
2502
2503 static int osc_get_info(struct obd_export *exp, obd_count keylen,
2504                         void *key, __u32 *vallen, void *val)
2505 {
2506         ENTRY;
2507         if (!vallen || !val)
2508                 RETURN(-EFAULT);
2509
2510         if (keylen > strlen("lock_to_stripe") &&
2511             strcmp(key, "lock_to_stripe") == 0) {
2512                 __u32 *stripe = val;
2513                 *vallen = sizeof(*stripe);
2514                 *stripe = 0;
2515                 RETURN(0);
2516         } else if (keylen >= strlen("last_id") && strcmp(key, "last_id") == 0) {
2517                 struct ptlrpc_request *req;
2518                 obd_id *reply;
2519                 char *bufs[1] = {key};
2520                 int rc;
2521                 req = ptlrpc_prep_req(class_exp2cliimp(exp), OST_GET_INFO, 1,
2522                                       &keylen, bufs);
2523                 if (req == NULL)
2524                         RETURN(-ENOMEM);
2525
2526                 req->rq_replen = lustre_msg_size(1, vallen);
2527                 rc = ptlrpc_queue_wait(req);
2528                 if (rc)
2529                         GOTO(out, rc);
2530
2531                 reply = lustre_swab_repbuf(req, 0, sizeof(*reply),
2532                                            lustre_swab_ost_last_id);
2533                 if (reply == NULL) {
2534                         CERROR("Can't unpack OST last ID\n");
2535                         GOTO(out, rc = -EPROTO);
2536                 }
2537                 *((obd_id *)val) = *reply;
2538         out:
2539                 ptlrpc_req_finished(req);
2540                 RETURN(rc);
2541         }
2542         RETURN(-EINVAL);
2543 }
2544
2545 static int osc_set_info(struct obd_export *exp, obd_count keylen,
2546                         void *key, obd_count vallen, void *val)
2547 {
2548         struct ptlrpc_request *req;
2549         struct obd_import *imp = class_exp2cliimp(exp);
2550         struct llog_ctxt *ctxt;
2551         int rc, size = keylen;
2552         char *bufs[1] = {key};
2553         ENTRY;
2554
2555         if (keylen == strlen("next_id") &&
2556             memcmp(key, "next_id", strlen("next_id")) == 0) {
2557                 if (vallen != sizeof(obd_id))
2558                         RETURN(-EINVAL);
2559                 exp->u.eu_osc_data.oed_oscc.oscc_next_id = *((obd_id*)val) + 1;
2560                 CDEBUG(D_INODE, "%s: set oscc_next_id = "LPU64"\n",
2561                        exp->exp_obd->obd_name,
2562                        exp->u.eu_osc_data.oed_oscc.oscc_next_id);
2563
2564                 RETURN(0);
2565         }
2566
2567         if (keylen == strlen("growth_count") &&
2568             memcmp(key, "growth_count", strlen("growth_count")) == 0) {
2569                 if (vallen != sizeof(int))
2570                         RETURN(-EINVAL);
2571                 exp->u.eu_osc_data.oed_oscc.oscc_grow_count = *((int*)val);
2572                 RETURN(0);
2573         }
2574
2575         if (keylen == strlen("unlinked") &&
2576             memcmp(key, "unlinked", keylen) == 0) {
2577                 struct osc_creator *oscc = &exp->u.eu_osc_data.oed_oscc;
2578                 spin_lock(&oscc->oscc_lock);
2579                 oscc->oscc_flags &= ~OSCC_FLAG_NOSPC;
2580                 spin_unlock(&oscc->oscc_lock);
2581                 RETURN(0);
2582         }
2583
2584         if (keylen < strlen("mds_conn") ||
2585             memcmp(key, "mds_conn", strlen("mds_conn")) != 0)
2586                 RETURN(-EINVAL);
2587
2588
2589         req = ptlrpc_prep_req(imp, OST_SET_INFO, 1, &size, bufs);
2590         if (req == NULL)
2591                 RETURN(-ENOMEM);
2592
2593         req->rq_replen = lustre_msg_size(0, NULL);
2594         rc = ptlrpc_queue_wait(req);
2595         ptlrpc_req_finished(req);
2596
2597         ctxt = llog_get_context(exp->exp_obd, LLOG_UNLINK_ORIG_CTXT);
2598         if (ctxt) {
2599                 rc = llog_initiator_connect(ctxt);
2600                 if (rc)
2601                         RETURN(rc);
2602         }
2603
2604         imp->imp_server_timeout = 1;
2605         CDEBUG(D_HA, "pinging OST %s\n", imp->imp_target_uuid.uuid);
2606         ptlrpc_pinger_add_import(imp);
2607
2608         RETURN(rc);
2609 }
2610
2611
2612 static struct llog_operations osc_size_repl_logops = {
2613         lop_cancel: llog_obd_repl_cancel
2614 };
2615
2616 static struct llog_operations osc_unlink_orig_logops;
2617 static int osc_llog_init(struct obd_device *obd, struct obd_device *tgt,
2618                         int count, struct llog_logid *logid)
2619 {
2620         int rc;
2621         ENTRY;
2622
2623         osc_unlink_orig_logops = llog_lvfs_ops;
2624         osc_unlink_orig_logops.lop_setup = llog_obd_origin_setup;
2625         osc_unlink_orig_logops.lop_cleanup = llog_obd_origin_cleanup;
2626         osc_unlink_orig_logops.lop_add = llog_obd_origin_add;
2627         osc_unlink_orig_logops.lop_connect = llog_origin_connect;
2628
2629         rc = llog_setup(obd, LLOG_UNLINK_ORIG_CTXT, tgt, count, logid,
2630                         &osc_unlink_orig_logops);
2631         if (rc)
2632                 RETURN(rc);
2633
2634         rc = llog_setup(obd, LLOG_SIZE_REPL_CTXT, tgt, count, NULL,
2635                         &osc_size_repl_logops);
2636         RETURN(rc);
2637 }
2638
2639 static int osc_llog_finish(struct obd_device *obd, int count)
2640 {
2641         int rc;
2642         ENTRY;
2643
2644         rc = llog_cleanup(llog_get_context(obd, LLOG_UNLINK_ORIG_CTXT));
2645         if (rc)
2646                 RETURN(rc);
2647
2648         rc = llog_cleanup(llog_get_context(obd, LLOG_SIZE_REPL_CTXT));
2649         RETURN(rc);
2650 }
2651
2652
2653 static int osc_connect(struct lustre_handle *exph,
2654                        struct obd_device *obd, struct obd_uuid *cluuid)
2655 {
2656         int rc;
2657         struct obd_export *exp;
2658
2659         rc = client_connect_import(exph, obd, cluuid);
2660
2661         if (obd->u.cli.cl_conn_count == 1) {
2662                 exp = class_conn2export(exph);
2663                 oscc_init(exp);
2664         }
2665
2666         return rc;
2667 }
2668
2669 static int osc_disconnect(struct obd_export *exp, int flags)
2670 {
2671         struct obd_device *obd = class_exp2obd(exp);
2672         struct llog_ctxt *ctxt = llog_get_context(obd, LLOG_SIZE_REPL_CTXT);
2673         int rc;
2674
2675         if (obd->u.cli.cl_conn_count == 1) {
2676                 /* flush any remaining cancel messages out to the target */
2677                 llog_sync(ctxt, exp);
2678                 
2679                 /* balance the conn2export for oscc in osc_connect */
2680                 class_export_put(exp);
2681         }
2682
2683         rc = client_disconnect_export(exp, flags);
2684         return rc;
2685 }
2686
2687 static int osc_lock_contains(struct obd_export *exp, struct lov_stripe_md *lsm,
2688                              struct ldlm_lock *lock, obd_off offset)
2689 {
2690         ENTRY;
2691         if (exp == NULL)
2692                 RETURN(-ENODEV);
2693
2694         if (lock->l_policy_data.l_extent.start <= offset &&
2695             lock->l_policy_data.l_extent.end >= offset)
2696                 RETURN(1);
2697         RETURN(0);
2698 }
2699
2700 static int osc_invalidate_import(struct obd_device *obd,
2701                                  struct obd_import *imp)
2702 {
2703         struct client_obd *cli;
2704         LASSERT(imp->imp_obd == obd);
2705         /* this used to try and tear down queued pages, but it was
2706          * not correctly implemented.  We'll have to do it again once
2707          * we call obd_invalidate_import() agian */
2708         /* XXX And we still need to do this */
2709
2710         /* Reset grants, too */
2711         cli = &obd->u.cli;
2712         spin_lock(&cli->cl_loi_list_lock);
2713         cli->cl_ost_can_grant = cli->cl_dirty_granted = 0;
2714         spin_unlock(&cli->cl_loi_list_lock);
2715         
2716         RETURN(0);
2717 }
2718
2719 int osc_setup(struct obd_device *obd, obd_count len, void *buf)
2720 {
2721         int rc;
2722         
2723         rc = ptlrpcd_addref();
2724         if (rc)
2725                 return rc;
2726
2727         rc = client_obd_setup(obd, len, buf);
2728         if (rc)
2729                 ptlrpcd_decref();
2730         RETURN(rc);
2731 }
2732
2733 int osc_cleanup(struct obd_device *obd, int flags)
2734 {
2735         int rc;
2736
2737         rc = client_obd_cleanup(obd, flags);
2738         ptlrpcd_decref();
2739         RETURN(rc);
2740 }
2741
2742
2743 struct obd_ops osc_obd_ops = {
2744         o_owner:        THIS_MODULE,
2745         o_attach:       osc_attach,
2746         o_detach:       osc_detach,
2747         o_setup:        osc_setup,
2748         o_cleanup:      osc_cleanup,
2749         o_connect:      osc_connect,
2750         o_disconnect:   osc_disconnect,
2751         o_statfs:       osc_statfs,
2752         o_packmd:       osc_packmd,
2753         o_unpackmd:     osc_unpackmd,
2754         o_create:       osc_create,
2755         o_destroy:      osc_destroy,
2756         o_getattr:      osc_getattr,
2757         o_getattr_async:osc_getattr_async,
2758         o_setattr:      osc_setattr,
2759         o_brw:          osc_brw,
2760         o_brw_async:    osc_brw_async,
2761         .o_prep_async_page =            osc_prep_async_page,
2762         .o_queue_async_io =             osc_queue_async_io,
2763         .o_set_async_flags =            osc_set_async_flags,
2764         .o_queue_sync_io =              osc_queue_sync_io,
2765         .o_trigger_sync_io =            osc_trigger_sync_io,
2766         .o_teardown_async_page =        osc_teardown_async_page,
2767         o_punch:        osc_punch,
2768         o_sync:         osc_sync,
2769         o_enqueue:      osc_enqueue,
2770         o_match:        osc_match,
2771         o_change_cbdata:osc_change_cbdata,
2772         o_cancel:       osc_cancel,
2773         o_cancel_unused:osc_cancel_unused,
2774         o_iocontrol:    osc_iocontrol,
2775         o_get_info:     osc_get_info,
2776         o_set_info:     osc_set_info,
2777         o_lock_contains:osc_lock_contains,
2778         o_invalidate_import: osc_invalidate_import,
2779         o_llog_init:    osc_llog_init,
2780         o_llog_finish:  osc_llog_finish,
2781 };
2782
2783 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
2784 struct obd_ops sanosc_obd_ops = {
2785         o_owner:        THIS_MODULE,
2786         o_attach:       osc_attach,
2787         o_detach:       osc_detach,
2788         o_cleanup:      client_obd_cleanup,
2789         o_connect:      osc_connect,
2790         o_disconnect:   client_disconnect_export,
2791         o_statfs:       osc_statfs,
2792         o_packmd:       osc_packmd,
2793         o_unpackmd:     osc_unpackmd,
2794         o_create:       osc_real_create,
2795         o_destroy:      osc_destroy,
2796         o_getattr:      osc_getattr,
2797         o_getattr_async:osc_getattr_async,
2798         o_setattr:      osc_setattr,
2799         o_setup:        client_sanobd_setup,
2800         o_brw:          sanosc_brw,
2801         o_punch:        osc_punch,
2802         o_sync:         osc_sync,
2803         o_enqueue:      osc_enqueue,
2804         o_match:        osc_match,
2805         o_change_cbdata:osc_change_cbdata,
2806         o_cancel:       osc_cancel,
2807         o_cancel_unused:osc_cancel_unused,
2808         o_iocontrol:    osc_iocontrol,
2809         o_lock_contains:osc_lock_contains,
2810         o_invalidate_import: osc_invalidate_import,
2811         o_llog_init:    osc_llog_init,
2812         o_llog_finish:  osc_llog_finish,
2813 };
2814 #endif
2815
2816 int __init osc_init(void)
2817 {
2818         struct lprocfs_static_vars lvars, sanlvars;
2819         int rc;
2820         ENTRY;
2821
2822         lprocfs_init_vars(osc, &lvars);
2823         lprocfs_init_vars(osc, &sanlvars);
2824
2825         rc = class_register_type(&osc_obd_ops, lvars.module_vars,
2826                                  LUSTRE_OSC_NAME);
2827         if (rc)
2828                 RETURN(rc);
2829
2830 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
2831         rc = class_register_type(&sanosc_obd_ops, sanlvars.module_vars,
2832                                  LUSTRE_SANOSC_NAME);
2833         if (rc)
2834                 class_unregister_type(LUSTRE_OSC_NAME);
2835 #endif
2836
2837         RETURN(rc);
2838 }
2839
2840 static void /*__exit*/ osc_exit(void)
2841 {
2842 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
2843         class_unregister_type(LUSTRE_SANOSC_NAME);
2844 #endif
2845         class_unregister_type(LUSTRE_OSC_NAME);
2846 }
2847
2848 #ifdef __KERNEL__
2849 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
2850 MODULE_DESCRIPTION("Lustre Object Storage Client (OSC)");
2851 MODULE_LICENSE("GPL");
2852
2853 module_init(osc_init);
2854 module_exit(osc_exit);
2855 #endif