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