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