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