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