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