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