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