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