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