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