Whamcloud - gitweb
merge b_devel into HEAD, which will become 0.7.3
[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 #define EXPORT_SYMTAB
30 #define DEBUG_SUBSYSTEM S_OSC
31
32 #ifdef __KERNEL__
33 # include <linux/version.h>
34 # include <linux/module.h>
35 # include <linux/mm.h>
36 # include <linux/highmem.h>
37 # include <linux/lustre_dlm.h>
38 # if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
39 #  include <linux/workqueue.h>
40 #  include <linux/smp_lock.h>
41 # else
42 #  include <linux/locks.h>
43 # endif
44 #else /* __KERNEL__ */
45 # include <liblustre.h>
46 #endif
47
48 #include <linux/kp30.h>
49 #include <linux/lustre_mds.h> /* for mds_objid */
50 #include <linux/lustre_otree.h>
51 #include <linux/obd_ost.h>
52 #include <linux/lustre_commit_confd.h>
53 #include <linux/obd_lov.h>
54
55 #ifndef  __CYGWIN__
56 # include <linux/ctype.h>
57 # include <linux/init.h>
58 #else
59 # include <ctype.h>
60 #endif
61
62 #include <linux/lustre_ha.h>
63 #include <linux/obd_support.h> /* for OBD_FAIL_CHECK */
64 #include <linux/lustre_lite.h> /* for ll_i2info */
65 #include <portals/lib-types.h> /* for PTL_MD_MAX_IOV */
66 #include <linux/lprocfs_status.h>
67
68 static struct llog_cookie zero_cookie = { { 0 } };
69
70 static int osc_attach(struct obd_device *dev, obd_count len, void *data)
71 {
72         struct lprocfs_static_vars lvars;
73
74         lprocfs_init_vars(osc,&lvars);
75         return lprocfs_obd_attach(dev, lvars.obd_vars);
76 }
77
78 static int osc_detach(struct obd_device *dev)
79 {
80         return lprocfs_obd_detach(dev);
81 }
82
83 /* Pack OSC object metadata for disk storage (LE byte order). */
84 static int osc_packmd(struct lustre_handle *conn, struct lov_mds_md **lmmp,
85                       struct lov_stripe_md *lsm)
86 {
87         int lmm_size;
88         ENTRY;
89
90         lmm_size = sizeof(**lmmp);
91         if (!lmmp)
92                 RETURN(lmm_size);
93
94         if (*lmmp && !lsm) {
95                 OBD_FREE(*lmmp, lmm_size);
96                 *lmmp = NULL;
97                 RETURN(0);
98         }
99
100         if (!*lmmp) {
101                 OBD_ALLOC(*lmmp, lmm_size);
102                 if (!*lmmp)
103                         RETURN(-ENOMEM);
104         }
105
106         if (lsm) {
107                 LASSERT(lsm->lsm_object_id);
108                 (*lmmp)->lmm_object_id = cpu_to_le64 (lsm->lsm_object_id);
109         }
110
111         RETURN(lmm_size);
112 }
113
114 /* Unpack OSC object metadata from disk storage (LE byte order). */
115 static int osc_unpackmd(struct lustre_handle *conn, struct lov_stripe_md **lsmp,
116                         struct lov_mds_md *lmm, int lmm_bytes)
117 {
118         int lsm_size;
119         ENTRY;
120
121         if (lmm != NULL) {
122                 if (lmm_bytes < sizeof (*lmm)) {
123                         CERROR("lov_mds_md too small: %d, need %d\n",
124                                lmm_bytes, (int)sizeof(*lmm));
125                         RETURN(-EINVAL);
126                 }
127                 /* XXX LOV_MAGIC etc check? */
128
129                 if (lmm->lmm_object_id == cpu_to_le64(0)) {
130                         CERROR("lov_mds_md: zero lmm_object_id\n");
131                         RETURN(-EINVAL);
132                 }
133         }
134
135         lsm_size = lov_stripe_md_size(1);
136         if (lsmp == NULL)
137                 RETURN(lsm_size);
138
139         if (*lsmp != NULL && lmm == NULL) {
140                 OBD_FREE(*lsmp, lsm_size);
141                 *lsmp = NULL;
142                 RETURN(0);
143         }
144
145         if (*lsmp == NULL) {
146                 OBD_ALLOC(*lsmp, lsm_size);
147                 if (*lsmp == NULL)
148                         RETURN(-ENOMEM);
149
150                 (*lsmp)->lsm_oinfo[0].loi_dirty_ot =
151                         &(*lsmp)->lsm_oinfo[0].loi_dirty_ot_inline;
152                 ot_init((*lsmp)->lsm_oinfo[0].loi_dirty_ot);
153         }
154
155         if (lmm != NULL) {
156                 /* XXX zero *lsmp? */
157                 (*lsmp)->lsm_object_id = le64_to_cpu (lmm->lmm_object_id);
158                 LASSERT((*lsmp)->lsm_object_id);
159         }
160
161         (*lsmp)->lsm_maxbytes = LUSTRE_STRIPE_MAXBYTES;
162
163         RETURN(lsm_size);
164 }
165
166 #warning "FIXME: make this be sent from OST"
167 #define OSC_BRW_MAX_SIZE 65536
168 #define OSC_BRW_MAX_IOV min_t(int, PTL_MD_MAX_IOV, OSC_BRW_MAX_SIZE/PAGE_SIZE)
169
170 static int osc_getattr_interpret(struct ptlrpc_request *req,
171                                  struct osc_getattr_async_args *aa, int rc)
172 {
173         struct ost_body *body;
174         ENTRY;
175
176         if (rc != 0)
177                 RETURN(rc);
178
179         body = lustre_swab_repbuf(req, 0, sizeof(*body), lustre_swab_ost_body);
180         if (body) {
181                 CDEBUG(D_INODE, "mode: %o\n", body->oa.o_mode);
182                 memcpy(aa->aa_oa, &body->oa, sizeof(*aa->aa_oa));
183
184                 /* This should really be sent by the OST */
185                 aa->aa_oa->o_blksize = OSC_BRW_MAX_SIZE;
186                 aa->aa_oa->o_valid |= OBD_MD_FLBLKSZ;
187         } else {
188                 CERROR("can't unpack ost_body\n");
189                 rc = -EPROTO;
190                 aa->aa_oa->o_valid = 0;
191         }
192
193         RETURN(rc);
194 }
195
196 static int osc_getattr_async(struct lustre_handle *conn, struct obdo *oa,
197                              struct lov_stripe_md *md,
198                              struct ptlrpc_request_set *set)
199 {
200         struct ptlrpc_request *request;
201         struct ost_body *body;
202         int size = sizeof(*body);
203         struct osc_getattr_async_args *aa;
204         ENTRY;
205
206         request = ptlrpc_prep_req(class_conn2cliimp(conn), OST_GETATTR, 1,
207                                   &size, NULL);
208         if (!request)
209                 RETURN(-ENOMEM);
210
211         body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof (*body));
212         memcpy(&body->oa, oa, sizeof(*oa));
213
214         request->rq_replen = lustre_msg_size(1, &size);
215         request->rq_interpret_reply = osc_getattr_interpret;
216
217         LASSERT (sizeof (*aa) <= sizeof (request->rq_async_args));
218         aa = (struct osc_getattr_async_args *)&request->rq_async_args;
219         aa->aa_oa = oa;
220
221         ptlrpc_set_add_req (set, request);
222         RETURN (0);
223 }
224
225 static int osc_getattr(struct lustre_handle *conn, struct obdo *oa,
226                        struct lov_stripe_md *md)
227 {
228         struct ptlrpc_request *request;
229         struct ost_body *body;
230         int rc, size = sizeof(*body);
231         ENTRY;
232
233         request = ptlrpc_prep_req(class_conn2cliimp(conn), OST_GETATTR, 1,
234                                   &size, NULL);
235         if (!request)
236                 RETURN(-ENOMEM);
237
238         body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof (*body));
239         memcpy(&body->oa, oa, sizeof(*oa));
240
241         request->rq_replen = lustre_msg_size(1, &size);
242
243         rc = ptlrpc_queue_wait(request);
244         if (rc) {
245                 CERROR("%s failed: rc = %d\n", __FUNCTION__, rc);
246                 GOTO(out, rc);
247         }
248
249         body = lustre_swab_repbuf(request, 0, sizeof (*body),
250                                   lustre_swab_ost_body);
251         if (body == NULL) {
252                 CERROR ("can't unpack ost_body\n");
253                 GOTO (out, rc = -EPROTO);
254         }
255
256         CDEBUG(D_INODE, "mode: %o\n", body->oa.o_mode);
257         memcpy(oa, &body->oa, sizeof(*oa));
258
259         /* This should really be sent by the OST */
260         oa->o_blksize = OSC_BRW_MAX_SIZE;
261         oa->o_valid |= OBD_MD_FLBLKSZ;
262
263         EXIT;
264  out:
265         ptlrpc_req_finished(request);
266         return rc;
267 }
268
269 /* The import lock must already be held. */
270 static inline void osc_update_body_handle(struct list_head *head,
271                                           struct lustre_handle *old,
272                                           struct lustre_handle *new, int op)
273 {
274         struct list_head *tmp;
275         struct ost_body *body;
276         struct ptlrpc_request *req;
277         struct ptlrpc_request *last_req = NULL; /* temporary fire escape */
278
279         list_for_each(tmp, head) {
280                 req = list_entry(tmp, struct ptlrpc_request, rq_list);
281
282                 /* XXX ok to remove when bug 1303 resolved - rread 05/27/03  */
283                 LASSERT (req != last_req);
284                 last_req = req;
285
286                 if (req->rq_reqmsg->opc != op)
287                         continue;
288                 body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*body));
289                 if (memcmp(obdo_handle(&body->oa), old, sizeof(*old)))
290                         continue;
291
292                 DEBUG_REQ(D_HA, req, "updating close body with new fh");
293                 memcpy(obdo_handle(&body->oa), new, sizeof(*new));
294         }
295 }
296
297 static void osc_replay_open(struct ptlrpc_request *req)
298 {
299         struct lustre_handle old;
300         struct ost_body *body;
301         struct obd_client_handle *och = req->rq_replay_data;
302         struct lustre_handle *oa_handle;
303         ENTRY;
304
305         body = lustre_swab_repbuf (req, 0, sizeof (*body),
306                                    lustre_swab_ost_body);
307         LASSERT (body != NULL);
308
309         oa_handle = obdo_handle(&body->oa);
310
311         memcpy(&old, &och->och_fh, sizeof(old));
312         CDEBUG(D_HA, "updating cookie from "LPD64" to "LPD64"\n",
313                och->och_fh.cookie, oa_handle->cookie);
314         memcpy(&och->och_fh, oa_handle, sizeof(och->och_fh));
315
316         /* A few frames up, ptlrpc_replay holds the lock, so this is safe. */
317         osc_update_body_handle(&req->rq_import->imp_sending_list, &old,
318                               &och->och_fh, OST_CLOSE);
319         osc_update_body_handle(&req->rq_import->imp_delayed_list, &old,
320                               &och->och_fh, OST_CLOSE);
321         EXIT;
322 }
323
324
325 static int osc_open(struct lustre_handle *conn, struct obdo *oa,
326                     struct lov_stripe_md *md, struct obd_trans_info *oti,
327                     struct obd_client_handle *och)
328 {
329         struct ptlrpc_request *request;
330         struct ost_body *body;
331         unsigned long flags;
332         int rc, size = sizeof(*body);
333         ENTRY;
334         LASSERT(och != NULL);
335
336         request = ptlrpc_prep_req(class_conn2cliimp(conn), OST_OPEN, 1, &size,
337                                   NULL);
338         if (!request)
339                 RETURN(-ENOMEM);
340
341         spin_lock_irqsave (&request->rq_lock, flags);
342         request->rq_replay = 1;
343         spin_unlock_irqrestore (&request->rq_lock, flags);
344
345         body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof (*body));
346         memcpy(&body->oa, oa, sizeof(*oa));
347
348         request->rq_replen = lustre_msg_size(1, &size);
349
350         rc = ptlrpc_queue_wait(request);
351         if (rc)
352                 GOTO(out, rc);
353
354         body = lustre_swab_repbuf (request, 0, sizeof (*body),
355                                    lustre_swab_ost_body);
356         if (body == NULL) {
357                 CERROR ("Can't unpack ost_body\n");
358                 GOTO (out, rc = -EPROTO);
359         }
360
361         memcpy(oa, &body->oa, sizeof(*oa));
362
363         /* If the open succeeded, we better have a handle */
364         /* BlueArc OSTs don't send back (o_valid | FLHANDLE).  sigh.
365          * Temporary workaround until fixed. -phil 24 Feb 03 */
366         // if ((oa->o_valid & OBD_MD_FLHANDLE) == 0) {
367         //         CERROR ("No file handle\n");
368         //         GOTO (out, rc = -EPROTO);
369         // }
370         oa->o_valid |= OBD_MD_FLHANDLE;
371
372         /* This should really be sent by the OST */
373         oa->o_blksize = OSC_BRW_MAX_SIZE;
374         oa->o_valid |= OBD_MD_FLBLKSZ;
375
376         memcpy(&och->och_fh, obdo_handle(oa), sizeof(och->och_fh));
377         request->rq_replay_cb = osc_replay_open;
378         request->rq_replay_data = och;
379         och->och_req = ptlrpc_request_addref(request);
380         och->och_magic = OBD_CLIENT_HANDLE_MAGIC;
381
382         EXIT;
383  out:
384         ptlrpc_req_finished(request);
385         return rc;
386 }
387
388 static int osc_close(struct lustre_handle *conn, struct obdo *oa,
389                      struct lov_stripe_md *md, struct obd_trans_info *oti)
390 {
391         struct obd_import *import = class_conn2cliimp(conn);
392         struct ptlrpc_request *request;
393         struct ost_body *body;
394         struct obd_client_handle *och;
395         unsigned long flags;
396         int rc, size = sizeof(*body);
397         ENTRY;
398
399         LASSERT(oa != NULL);
400         och = (struct obd_client_handle *)&oa->o_inline;
401         if (och->och_magic == 0) {
402                 /* Zero magic means that this file was never opened on this
403                  * OST--almost certainly because the OST was inactive at
404                  * open-time */
405                 RETURN(0);
406         }
407         LASSERT(och->och_magic == OBD_CLIENT_HANDLE_MAGIC);
408
409         request = ptlrpc_prep_req(import, OST_CLOSE, 1, &size, NULL);
410         if (!request)
411                 RETURN(-ENOMEM);
412
413         body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof (*body));
414         memcpy(&body->oa, oa, sizeof(*oa));
415
416         request->rq_replen = lustre_msg_size(1, &size);
417
418         rc = ptlrpc_queue_wait(request);
419         if (rc)
420                 CDEBUG(D_HA, "Suppressing close error %d\n", rc); // bug 1036
421
422         /* och_req == NULL can't happen any more, right? --phik */
423         if (och->och_req != NULL) {
424                 spin_lock_irqsave(&import->imp_lock, flags);
425                 spin_lock (&och->och_req->rq_lock);
426                 och->och_req->rq_replay = 0;
427                 spin_unlock (&och->och_req->rq_lock);
428                 /* see comments in llite/file.c:ll_mdc_close() */
429                 if (och->och_req->rq_transno) {
430                         /* this can't happen yet, because the OSTs don't yet
431                          * issue transnos for OPEN requests -phik 21 Apr 2003 */
432                         LBUG();
433                         if (!request->rq_transno && import->imp_replayable) {
434                                 request->rq_transno = och->och_req->rq_transno;
435                                 ptlrpc_retain_replayable_request(request,
436                                                                  import);
437                         }
438                         spin_unlock_irqrestore(&import->imp_lock, flags);
439                 } else {
440                         spin_unlock_irqrestore(&import->imp_lock, flags);
441                 }
442
443                 ptlrpc_req_finished(och->och_req);
444         }
445
446         if (!rc) {
447                 body = lustre_swab_repbuf (request, 0, sizeof (*body),
448                                            lustre_swab_ost_body);
449                 if (body == NULL) {
450                         rc = -EPROTO;
451                         CDEBUG(D_HA, "Suppressing close error %d\n", rc); // bug 1036
452                 } else
453                         memcpy(oa, &body->oa, sizeof(*oa));
454         }
455
456         ptlrpc_req_finished(request);
457         RETURN(0);
458 }
459
460 static int osc_setattr(struct lustre_handle *conn, struct obdo *oa,
461                        struct lov_stripe_md *md, struct obd_trans_info *oti)
462 {
463         struct ptlrpc_request *request;
464         struct ost_body *body;
465         int rc, size = sizeof(*body);
466         ENTRY;
467
468         request = ptlrpc_prep_req(class_conn2cliimp(conn), OST_SETATTR, 1,
469                                   &size, NULL);
470         if (!request)
471                 RETURN(-ENOMEM);
472
473         body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof (*body));
474         memcpy(&body->oa, oa, sizeof(*oa));
475
476         request->rq_replen = lustre_msg_size(1, &size);
477
478         rc = ptlrpc_queue_wait(request);
479
480         ptlrpc_req_finished(request);
481         return rc;
482 }
483
484 static int osc_create(struct lustre_handle *conn, struct obdo *oa,
485                       struct lov_stripe_md **ea, struct obd_trans_info *oti)
486 {
487         struct ptlrpc_request *request;
488         struct ost_body *body;
489         struct lov_stripe_md *lsm;
490         int rc, size = sizeof(*body);
491         ENTRY;
492
493         LASSERT(oa);
494         LASSERT(ea);
495
496         lsm = *ea;
497         if (!lsm) {
498                 rc = obd_alloc_memmd(conn, &lsm);
499                 if (rc < 0)
500                         RETURN(rc);
501         }
502
503         request = ptlrpc_prep_req(class_conn2cliimp(conn), OST_CREATE, 1, &size,
504                                   NULL);
505         if (!request)
506                 GOTO(out, rc = -ENOMEM);
507
508         body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof (*body));
509         memcpy(&body->oa, oa, sizeof(body->oa));
510
511         request->rq_replen = lustre_msg_size(1, &size);
512
513         rc = ptlrpc_queue_wait(request);
514         if (rc)
515                 GOTO(out_req, rc);
516
517         body = lustre_swab_repbuf(request, 0, sizeof(*body),
518                                   lustre_swab_ost_body);
519         if (body == NULL) {
520                 CERROR ("can't unpack ost_body\n");
521                 GOTO (out_req, rc = -EPROTO);
522         }
523
524         memcpy(oa, &body->oa, sizeof(*oa));
525
526         /* This should really be sent by the OST */
527         oa->o_blksize = OSC_BRW_MAX_SIZE;
528         oa->o_valid |= OBD_MD_FLBLKSZ;
529
530         /* XXX LOV STACKING: the lsm that is passed to us from LOV does not
531          * have valid lsm_oinfo data structs, so don't go touching that.
532          * This needs to be fixed in a big way.
533          */
534         lsm->lsm_object_id = oa->o_id;
535         *ea = lsm;
536
537         if (oti != NULL) {
538                 oti->oti_transno = request->rq_repmsg->transno;
539
540                 if (oa->o_valid & OBD_MD_FLCOOKIE) {
541                         if (!oti->oti_logcookies)
542                                 oti_alloc_cookies(oti, 1);
543                         memcpy(oti->oti_logcookies, obdo_logcookie(oa),
544                                sizeof(oti->oti_onecookie));
545                 }
546         }
547
548         CDEBUG(D_HA, "transno: "LPD64"\n", request->rq_repmsg->transno);
549         EXIT;
550 out_req:
551         ptlrpc_req_finished(request);
552 out:
553         if (rc && !*ea)
554                 obd_free_memmd(conn, &lsm);
555         return rc;
556 }
557
558 static int osc_punch(struct lustre_handle *conn, struct obdo *oa,
559                      struct lov_stripe_md *md, obd_size start,
560                      obd_size end, struct obd_trans_info *oti)
561 {
562         struct ptlrpc_request *request;
563         struct ost_body *body;
564         int rc, size = sizeof(*body);
565         ENTRY;
566
567         if (!oa) {
568                 CERROR("oa NULL\n");
569                 RETURN(-EINVAL);
570         }
571
572         request = ptlrpc_prep_req(class_conn2cliimp(conn), OST_PUNCH, 1, &size,
573                                   NULL);
574         if (!request)
575                 RETURN(-ENOMEM);
576
577         body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof (*body));
578         memcpy(&body->oa, oa, sizeof(*oa));
579
580         /* overload the size and blocks fields in the oa with start/end */
581         body->oa.o_size = start;
582         body->oa.o_blocks = end;
583         body->oa.o_valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS);
584
585         request->rq_replen = lustre_msg_size(1, &size);
586
587         rc = ptlrpc_queue_wait(request);
588         if (rc)
589                 GOTO(out, rc);
590
591         body = lustre_swab_repbuf (request, 0, sizeof (*body),
592                                    lustre_swab_ost_body);
593         if (body == NULL) {
594                 CERROR ("can't unpack ost_body\n");
595                 GOTO (out, rc = -EPROTO);
596         }
597
598         memcpy(oa, &body->oa, sizeof(*oa));
599
600         EXIT;
601  out:
602         ptlrpc_req_finished(request);
603         return rc;
604 }
605
606 static int osc_destroy(struct lustre_handle *conn, struct obdo *oa,
607                        struct lov_stripe_md *ea, struct obd_trans_info *oti)
608 {
609         struct ptlrpc_request *request;
610         struct ost_body *body;
611         int rc, size = sizeof(*body);
612         ENTRY;
613
614         if (!oa) {
615                 CERROR("oa NULL\n");
616                 RETURN(-EINVAL);
617         }
618         request = ptlrpc_prep_req(class_conn2cliimp(conn), OST_DESTROY, 1,
619                                   &size, NULL);
620         if (!request)
621                 RETURN(-ENOMEM);
622
623         body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof (*body));
624         memcpy(&body->oa, oa, sizeof(*oa));
625
626         if (oti && oa->o_valid & OBD_MD_FLCOOKIE) {
627                 memcpy(obdo_logcookie(oa), oti->oti_logcookies,
628                        sizeof(*oti->oti_logcookies));
629                 oti->oti_logcookies++;
630         }
631
632         request->rq_replen = lustre_msg_size(1, &size);
633
634         rc = ptlrpc_queue_wait(request);
635         if (rc)
636                 GOTO(out, rc);
637
638         body = lustre_swab_repbuf(request, 0, sizeof(*body),
639                                   lustre_swab_ost_body);
640         if (body == NULL) {
641                 CERROR ("Can't unpack body\n");
642                 GOTO (out, rc = -EPROTO);
643         }
644
645         memcpy(oa, &body->oa, sizeof(*oa));
646
647         EXIT;
648  out:
649         ptlrpc_req_finished(request);
650         return rc;
651 }
652
653 static void osc_announce_cached(struct client_obd *cli, struct ost_body *body)
654 {
655         obd_flag bits = OBD_MD_FLBLOCKS|OBD_MD_FLRDEV;
656
657         LASSERT(!(body->oa.o_valid & bits));
658
659         body->oa.o_valid |= bits;
660         down(&cli->cl_dirty_sem);
661         body->oa.o_blocks = cli->cl_dirty;
662         body->oa.o_rdev = cli->cl_dirty_granted;
663         up(&cli->cl_dirty_sem);
664         CDEBUG(D_INODE, "announcing "LPU64" dirty "LPU64" granted\n",
665                cli->cl_dirty, cli->cl_dirty_granted);
666 }
667
668 static void osc_update_grant(struct client_obd *cli, struct ost_body *body)
669 {
670         if(!(body->oa.o_valid & OBD_MD_FLRDEV)) {
671                 if (cli->cl_ost_can_grant) {
672                         CDEBUG(D_INODE, "%s can't grant\n",
673                                cli->cl_import->imp_target_uuid.uuid);
674                 }
675                 cli->cl_ost_can_grant = 0;
676                 return;
677         }
678
679         CDEBUG(D_ERROR, "got "LPU64" grant\n", body->oa.o_rdev);
680         down(&cli->cl_dirty_sem);
681         cli->cl_dirty_granted = body->oa.o_rdev;
682         /* XXX check for over-run and wake up the io thread that
683          * doesn't exist yet */
684         up(&cli->cl_dirty_sem);
685 }
686
687 /* We assume that the reason this OSC got a short read is because it read
688  * beyond the end of a stripe file; i.e. lustre is reading a sparse file
689  * via the LOV, and it _knows_ it's reading inside the file, it's just that
690  * this stripe never got written at or beyond this stripe offset yet. */
691 static void handle_short_read(int nob_read, obd_count page_count,
692                               struct brw_page *pga)
693 {
694         char *ptr;
695
696         /* skip bytes read OK */
697         while (nob_read > 0) {
698                 LASSERT (page_count > 0);
699
700                 if (pga->count > nob_read) {
701                         /* EOF inside this page */
702                         ptr = kmap(pga->pg) + (pga->off & ~PAGE_MASK);
703                         memset(ptr + nob_read, 0, pga->count - nob_read);
704                         kunmap(pga->pg);
705                         page_count--;
706                         pga++;
707                         break;
708                 }
709
710                 nob_read -= pga->count;
711                 page_count--;
712                 pga++;
713         }
714
715         /* zero remaining pages */
716         while (page_count-- > 0) {
717                 ptr = kmap(pga->pg) + (pga->off & ~PAGE_MASK);
718                 memset(ptr, 0, pga->count);
719                 kunmap(pga->pg);
720                 pga++;
721         }
722 }
723
724 static int check_write_rcs(struct ptlrpc_request *request, int niocount,
725                            obd_count page_count, struct brw_page *pga)
726 {
727         int    i;
728         __u32 *remote_rcs;
729
730         /* return error if any niobuf was in error */
731         remote_rcs = lustre_swab_repbuf(request, 1,
732                                         sizeof(*remote_rcs) * niocount, NULL);
733         if (remote_rcs == NULL) {
734                 CERROR ("Missing/short RC vector on BRW_WRITE reply\n");
735                 return (-EPROTO);
736         }
737         if (lustre_msg_swabbed (request->rq_repmsg))
738                 for (i = 0; i < niocount; i++)
739                         __swab32s (&remote_rcs[i]);
740
741         for (i = 0; i < niocount; i++) {
742                 if (remote_rcs[i] < 0)
743                         return (remote_rcs[i]);
744
745                 if (remote_rcs[i] != 0) {
746                         CERROR ("rc[%d] invalid (%d) req %p\n",
747                                 i, remote_rcs[i], request);
748                         return (-EPROTO);
749                 }
750         }
751
752         return (0);
753 }
754
755 static inline int can_merge_pages (struct brw_page *p1, struct brw_page *p2)
756 {
757         if (p1->flag != p2->flag) {
758                 /* XXX we don't make much use of 'flag' right now
759                  * but this will warn about usage when we do */
760                 CERROR ("different flags set %d, %d\n",
761                         p1->flag, p2->flag);
762                 return (0);
763         }
764
765         return (p1->off + p1->count == p2->off);
766 }
767
768 #if CHECKSUM_BULK
769 static obd_count cksum_pages(int nob, obd_count page_count,
770                              struct brw_page *pga)
771 {
772         obd_count cksum = 0;
773         char *ptr;
774         int   i;
775
776         while (nob > 0) {
777                 LASSERT (page_count > 0);
778
779                 ptr = kmap (pga->pg);
780                 ost_checksum (&cksum, ptr + (pga->off & (PAGE_SIZE - 1)),
781                               pga->count > nob ? nob : pga->count);
782                 kunmap (pga->pg);
783
784                 nob -= pga->count;
785                 page_count--;
786                 pga++;
787         }
788
789         return (cksum);
790 }
791 #endif
792
793 static int osc_brw_prep_request(int cmd, struct obd_import *imp,struct obdo *oa,
794                                 struct lov_stripe_md *lsm, obd_count page_count,
795                                 struct brw_page *pga, int *requested_nobp,
796                                 int *niocountp, struct ptlrpc_request **reqp)
797 {
798         struct ptlrpc_request   *req;
799         struct ptlrpc_bulk_desc *desc;
800         struct client_obd       *cli = &imp->imp_obd->u.cli;
801         struct ost_body         *body;
802         struct obd_ioobj        *ioobj;
803         struct niobuf_remote    *niobuf;
804         unsigned long            flags;
805         int                      niocount;
806         int                      size[3];
807         int                      i;
808         int                      requested_nob;
809         int                      opc;
810         int                      rc;
811
812         opc = ((cmd & OBD_BRW_WRITE) != 0) ? OST_WRITE : OST_READ;
813
814         for (niocount = i = 1; i < page_count; i++)
815                 if (!can_merge_pages (&pga[i - 1], &pga[i]))
816                         niocount++;
817
818         size[0] = sizeof(*body);
819         size[1] = sizeof(*ioobj);
820         size[2] = niocount * sizeof(*niobuf);
821
822         req = ptlrpc_prep_req(imp, opc, 3, size, NULL);
823         if (req == NULL)
824                 return (-ENOMEM);
825
826         if (opc == OST_WRITE)
827                 desc = ptlrpc_prep_bulk_imp(req, BULK_GET_SOURCE,
828                                             OST_BULK_PORTAL);
829         else
830                 desc = ptlrpc_prep_bulk_imp(req, BULK_PUT_SINK,
831                                             OST_BULK_PORTAL);
832         if (desc == NULL)
833                 GOTO(out, rc = -ENOMEM);
834         /* NB request now owns desc and will free it when it gets freed */
835
836         body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof(*body));
837         ioobj = lustre_msg_buf(req->rq_reqmsg, 1, sizeof(*ioobj));
838         niobuf = lustre_msg_buf(req->rq_reqmsg, 2, niocount * sizeof(*niobuf));
839
840         memcpy(&body->oa, oa, sizeof(*oa));
841
842         ioobj->ioo_id = oa->o_id;
843         ioobj->ioo_gr = oa->o_valid & 0 ? oa->o_gr : 0;
844         ioobj->ioo_type = oa->o_mode;
845         ioobj->ioo_bufcnt = niocount;
846
847         LASSERT (page_count > 0);
848         for (requested_nob = i = 0; i < page_count; i++, niobuf++) {
849                 struct brw_page *pg = &pga[i];
850                 struct brw_page *pg_prev = pg - 1;
851
852                 LASSERT(pg->count > 0);
853                 LASSERT((pg->off & ~PAGE_MASK) + pg->count <= PAGE_SIZE);
854                 LASSERT(i == 0 || pg->off > pg_prev->off);
855
856                 rc = ptlrpc_prep_bulk_page(desc, pg->pg, pg->off & ~PAGE_MASK,
857                                            pg->count);
858                 if (rc != 0)
859                         GOTO(out, rc);
860
861                 requested_nob += pg->count;
862
863                 if (i > 0 && can_merge_pages(pg_prev, pg)) {
864                         niobuf--;
865                         niobuf->len += pg->count;
866                 } else {
867                         niobuf->offset = pg->off;
868                         niobuf->len    = pg->count;
869                         niobuf->flags  = pg->flag;
870                 }
871         }
872
873         LASSERT((void *)(niobuf - niocount) ==
874                 lustre_msg_buf(req->rq_reqmsg, 2, niocount * sizeof(*niobuf)));
875 #if CHECKSUM_BULK
876         body->oa.o_valid |= OBD_MD_FLCKSUM;
877         if (opc == OST_BRW_WRITE)
878                 body->oa.o_nlink = cksum_pages(requested_nob, page_count, pga);
879 #endif
880         osc_announce_cached(cli, body);
881         spin_lock_irqsave(&req->rq_lock, flags);
882         req->rq_no_resend = 1;
883         spin_unlock_irqrestore(&req->rq_lock, flags);
884
885         /* size[0] still sizeof (*body) */
886         if (opc == OST_WRITE) {
887                 /* 1 RC per niobuf */
888                 size[1] = sizeof(__u32) * niocount;
889                 req->rq_replen = lustre_msg_size(2, size);
890         } else {
891                 /* 1 RC for the whole I/O */
892                 req->rq_replen = lustre_msg_size(1, size);
893         }
894
895         *niocountp = niocount;
896         *requested_nobp = requested_nob;
897         *reqp = req;
898         return (0);
899
900  out:
901         ptlrpc_req_finished (req);
902         return (rc);
903 }
904
905 static int osc_brw_fini_request(struct ptlrpc_request *req, struct obdo *oa,
906                                 int requested_nob, int niocount,
907                                 obd_count page_count, struct brw_page *pga,
908                                 int rc)
909 {
910         struct client_obd *cli = &req->rq_import->imp_obd->u.cli;
911         struct ost_body *body;
912
913         if (rc < 0)
914                 return (rc);
915
916         body = lustre_swab_repbuf(req, 0, sizeof(*body), lustre_swab_ost_body);
917         if (body == NULL) {
918                 CERROR ("Can't unpack body\n");
919                 return (-EPROTO);
920         }
921
922         osc_update_grant(cli, body);
923
924         if (req->rq_reqmsg->opc == OST_WRITE) {
925                 if (rc > 0) {
926                         CERROR ("Unexpected +ve rc %d\n", rc);
927                         return (-EPROTO);
928                 }
929
930                 return(check_write_rcs(req, niocount, page_count, pga));
931         }
932
933         if (rc > requested_nob) {
934                 CERROR("Unexpected rc %d (%d requested)\n", rc, requested_nob);
935                 return (-EPROTO);
936         }
937
938         if (rc < requested_nob)
939                 handle_short_read(rc, page_count, pga);
940
941         memcpy(oa, &body->oa, sizeof(*oa));
942
943 #if CHECKSUM_BULK
944         if (oa->o_valid & OBD_MD_FLCKSUM) {
945                 static int cksum_counter;
946                 obd_count server_cksum = oa->o_nlink;
947                 obd_count cksum = cksum_pages(rc, page_count, pga);
948
949                 cksum_counter++;
950                 if (server_cksum != cksum) {
951                         CERROR("Bad checksum: server "LPX64", client "LPX64
952                                ", server NID "LPX64"\n", server_cksum, cksum,
953                                imp->imp_connection->c_peer.peer_nid);
954                         cksum_counter = 0;
955                         oa->o_rdev = cksum;
956                 } else if ((cksum_counter & (-cksum_counter)) == cksum_counter)
957                         CERROR("Checksum %u from "LPX64" OK: %x\n",
958                                cksum_counter,
959                                imp->imp_connection->c_peer.peer_nid, cksum);
960         } else {
961                 static int cksum_missed;
962                 cksum_missed++;
963                 if ((cksum_missed & (-cksum_missed)) == cksum_missed)
964                         CERROR("Request checksum %u from "LPX64", no reply\n",
965                                cksum_missed,
966                                imp->imp_connection->c_peer.peer_nid);
967         }
968 #endif
969         return (0);
970 }
971
972 static int osc_brw_internal(int cmd, struct lustre_handle *conn,struct obdo *oa,
973                             struct lov_stripe_md *lsm,
974                             obd_count page_count, struct brw_page *pga)
975 {
976         int                    requested_nob;
977         int                    niocount;
978         struct ptlrpc_request *request;
979         int                    rc;
980         ENTRY;
981
982 restart_bulk:
983         rc = osc_brw_prep_request(cmd, class_conn2cliimp(conn), oa, lsm,
984                                   page_count, pga, &requested_nob, &niocount,
985                                   &request);
986         /* NB ^ sets rq_no_resend */
987
988         if (rc != 0)
989                 return (rc);
990
991         rc = ptlrpc_queue_wait(request);
992
993         if (rc == -ETIMEDOUT && request->rq_resend) {
994                 DEBUG_REQ(D_HA, request,  "BULK TIMEOUT");
995                 ptlrpc_req_finished(request);
996                 goto restart_bulk;
997         }
998
999         rc = osc_brw_fini_request(request, oa, requested_nob, niocount,
1000                                   page_count, pga, rc);
1001
1002         ptlrpc_req_finished(request);
1003         RETURN (rc);
1004 }
1005
1006 static int brw_interpret(struct ptlrpc_request *request,
1007                          struct osc_brw_async_args *aa, int rc)
1008 {
1009         struct obdo *oa      = aa->aa_oa;
1010         int requested_nob    = aa->aa_requested_nob;
1011         int niocount         = aa->aa_nio_count;
1012         obd_count page_count = aa->aa_page_count;
1013         struct brw_page *pga = aa->aa_pga;
1014         ENTRY;
1015
1016         /* XXX bug 937 here */
1017         if (rc == -ETIMEDOUT && request->rq_resend) {
1018                 DEBUG_REQ(D_HA, request,  "BULK TIMEOUT");
1019                 LBUG(); /* re-send.  later. */
1020                 //goto restart_bulk;
1021         }
1022
1023         rc = osc_brw_fini_request(request, oa, requested_nob, niocount,
1024                                   page_count, pga, rc);
1025         RETURN (rc);
1026 }
1027
1028 static int async_internal(int cmd, struct lustre_handle *conn, struct obdo *oa,
1029                           struct lov_stripe_md *lsm, obd_count page_count,
1030                           struct brw_page *pga, struct ptlrpc_request_set *set)
1031 {
1032         struct ptlrpc_request     *request;
1033         int                        requested_nob;
1034         int                        nio_count;
1035         struct osc_brw_async_args *aa;
1036         int                        rc;
1037         ENTRY;
1038
1039         rc = osc_brw_prep_request(cmd, class_conn2cliimp(conn), oa, lsm,
1040                                   page_count, pga, &requested_nob, &nio_count,
1041                                   &request);
1042         /* NB ^ sets rq_no_resend */
1043
1044         if (rc == 0) {
1045                 LASSERT(sizeof(*aa) <= sizeof(request->rq_async_args));
1046                 aa = (struct osc_brw_async_args *)&request->rq_async_args;
1047                 aa->aa_oa = oa;
1048                 aa->aa_requested_nob = requested_nob;
1049                 aa->aa_nio_count = nio_count;
1050                 aa->aa_page_count = page_count;
1051                 aa->aa_pga = pga;
1052
1053                 request->rq_interpret_reply = brw_interpret;
1054                 ptlrpc_set_add_req(set, request);
1055         }
1056         RETURN (rc);
1057 }
1058
1059 #ifndef min_t
1060 #define min_t(type,x,y) \
1061         ({ type __x = (x); type __y = (y); __x < __y ? __x: __y; })
1062 #endif
1063
1064 /*
1065  * ugh, we want disk allocation on the target to happen in offset order.  we'll
1066  * follow sedgewicks advice and stick to the dead simple shellsort -- it'll do
1067  * fine for our small page arrays and doesn't require allocation.  its an
1068  * insertion sort that swaps elements that are strides apart, shrinking the
1069  * stride down until its '1' and the array is sorted.
1070  */
1071 static void sort_brw_pages(struct brw_page *array, int num)
1072 {
1073         int stride, i, j;
1074         struct brw_page tmp;
1075
1076         if (num == 1)
1077                 return;
1078         for (stride = 1; stride < num ; stride = (stride * 3) + 1)
1079                 ;
1080
1081         do {
1082                 stride /= 3;
1083                 for (i = stride ; i < num ; i++) {
1084                         tmp = array[i];
1085                         j = i;
1086                         while (j >= stride && array[j - stride].off > tmp.off) {
1087                                 array[j] = array[j - stride];
1088                                 j -= stride;
1089                         }
1090                         array[j] = tmp;
1091                 }
1092         } while (stride > 1);
1093 }
1094
1095 /* make sure we the regions we're passing to elan don't violate its '4
1096  * fragments' constraint.  portal headers are a fragment, all full
1097  * PAGE_SIZE long pages count as 1 fragment, and each partial page
1098  * counts as a fragment.  I think.  see bug 934. */
1099 static obd_count check_elan_limit(struct brw_page *pg, obd_count pages)
1100 {
1101         int frags_left = 3;
1102         int saw_whole_frag = 0;
1103         int i;
1104
1105         for (i = 0 ; frags_left && i < pages ; pg++, i++) {
1106                 if (pg->count == PAGE_SIZE) {
1107                         if (!saw_whole_frag) {
1108                                 saw_whole_frag = 1;
1109                                 frags_left--;
1110                         }
1111                 } else {
1112                         frags_left--;
1113                 }
1114         }
1115         return i;
1116 }
1117
1118 static int osc_brw(int cmd, struct lustre_handle *conn, struct obdo *oa,
1119                    struct lov_stripe_md *md, obd_count page_count,
1120                    struct brw_page *pga, struct obd_trans_info *oti)
1121 {
1122         ENTRY;
1123
1124         if (cmd == OBD_BRW_CHECK) {
1125                 /* The caller just wants to know if there's a chance that this
1126                  * I/O can succeed */
1127                 struct obd_import *imp = class_conn2cliimp(conn);
1128
1129                 if (imp == NULL || imp->imp_invalid)
1130                         RETURN(-EIO);
1131                 RETURN(0);
1132         }
1133
1134         while (page_count) {
1135                 obd_count pages_per_brw;
1136                 int rc;
1137
1138                 if (page_count > OSC_BRW_MAX_IOV)
1139                         pages_per_brw = OSC_BRW_MAX_IOV;
1140                 else
1141                         pages_per_brw = page_count;
1142
1143                 sort_brw_pages(pga, pages_per_brw);
1144                 pages_per_brw = check_elan_limit(pga, pages_per_brw);
1145
1146                 rc = osc_brw_internal(cmd, conn, oa, md, pages_per_brw, pga);
1147
1148                 if (rc != 0)
1149                         RETURN(rc);
1150
1151                 page_count -= pages_per_brw;
1152                 pga += pages_per_brw;
1153         }
1154         RETURN(0);
1155 }
1156
1157 static int osc_brw_async(int cmd, struct lustre_handle *conn, struct obdo *oa,
1158                          struct lov_stripe_md *md, obd_count page_count,
1159                          struct brw_page *pga, struct ptlrpc_request_set *set,
1160                          struct obd_trans_info *oti)
1161 {
1162         ENTRY;
1163
1164         if (cmd == OBD_BRW_CHECK) {
1165                 /* The caller just wants to know if there's a chance that this
1166                  * I/O can succeed */
1167                 struct obd_import *imp = class_conn2cliimp(conn);
1168
1169                 if (imp == NULL || imp->imp_invalid)
1170                         RETURN(-EIO);
1171                 RETURN(0);
1172         }
1173
1174         while (page_count) {
1175                 obd_count pages_per_brw;
1176                 int rc;
1177
1178                 if (page_count > OSC_BRW_MAX_IOV)
1179                         pages_per_brw = OSC_BRW_MAX_IOV;
1180                 else
1181                         pages_per_brw = page_count;
1182
1183                 sort_brw_pages(pga, pages_per_brw);
1184                 pages_per_brw = check_elan_limit(pga, pages_per_brw);
1185
1186                 rc = async_internal(cmd, conn, oa, md, pages_per_brw, pga, set);
1187
1188                 if (rc != 0)
1189                         RETURN(rc);
1190
1191                 page_count -= pages_per_brw;
1192                 pga += pages_per_brw;
1193         }
1194         RETURN(0);
1195 }
1196
1197 #ifdef __KERNEL__
1198 /* Note: caller will lock/unlock, and set uptodate on the pages */
1199 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
1200 static int sanosc_brw_read(struct lustre_handle *conn, struct obdo *oa,
1201                            struct lov_stripe_md *lsm, obd_count page_count,
1202                            struct brw_page *pga)
1203 {
1204         struct ptlrpc_request *request = NULL;
1205         struct ost_body *body;
1206         struct niobuf_remote *nioptr;
1207         struct obd_ioobj *iooptr;
1208         int rc, size[3] = {sizeof(*body)}, mapped = 0;
1209         int swab;
1210         ENTRY;
1211
1212         /* XXX does not handle 'new' brw protocol */
1213
1214         size[1] = sizeof(struct obd_ioobj);
1215         size[2] = page_count * sizeof(*nioptr);
1216
1217         request = ptlrpc_prep_req(class_conn2cliimp(conn), OST_SAN_READ, 3,
1218                                   size, NULL);
1219         if (!request)
1220                 RETURN(-ENOMEM);
1221
1222         body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof(*body));
1223         iooptr = lustre_msg_buf(request->rq_reqmsg, 1, sizeof(*iooptr));
1224         nioptr = lustre_msg_buf(request->rq_reqmsg, 2,
1225                                 sizeof(*nioptr) * page_count);
1226
1227         memcpy(&body->oa, oa, sizeof(body->oa));
1228
1229         iooptr->ioo_id = oa->o_id;
1230         iooptr->ioo_gr = oa->o_valid & 0 ? oa->o_gr : 0;
1231         iooptr->ioo_type = oa->o_mode;
1232         iooptr->ioo_bufcnt = page_count;
1233
1234         for (mapped = 0; mapped < page_count; mapped++, nioptr++) {
1235                 LASSERT(PageLocked(pga[mapped].pg));
1236                 LASSERT(mapped == 0 || pga[mapped].off > pga[mapped - 1].off);
1237
1238                 nioptr->offset = pga[mapped].off;
1239                 nioptr->len    = pga[mapped].count;
1240                 nioptr->flags  = pga[mapped].flag;
1241         }
1242
1243         size[1] = page_count * sizeof(*nioptr);
1244         request->rq_replen = lustre_msg_size(2, size);
1245
1246         rc = ptlrpc_queue_wait(request);
1247         if (rc)
1248                 GOTO(out_req, rc);
1249
1250         body = lustre_swab_repbuf(request, 0, sizeof(*body),
1251                                   lustre_swab_ost_body);
1252         if (body == NULL) {
1253                 CERROR("Can't unpack body\n");
1254                 GOTO(out_req, rc = -EPROTO);
1255         }
1256
1257         memcpy(oa, &body->oa, sizeof(*oa));
1258
1259         swab = lustre_msg_swabbed(request->rq_repmsg);
1260         LASSERT_REPSWAB(request, 1);
1261         nioptr = lustre_msg_buf(request->rq_repmsg, 1, size[1]);
1262         if (!nioptr) {
1263                 /* nioptr missing or short */
1264                 GOTO(out_req, rc = -EPROTO);
1265         }
1266
1267         /* actual read */
1268         for (mapped = 0; mapped < page_count; mapped++, nioptr++) {
1269                 struct page *page = pga[mapped].pg;
1270                 struct buffer_head *bh;
1271                 kdev_t dev;
1272
1273                 if (swab)
1274                         lustre_swab_niobuf_remote (nioptr);
1275
1276                 /* got san device associated */
1277                 LASSERT(class_conn2obd(conn));
1278                 dev = class_conn2obd(conn)->u.cli.cl_sandev;
1279
1280                 /* hole */
1281                 if (!nioptr->offset) {
1282                         CDEBUG(D_PAGE, "hole at ino %lu; index %ld\n",
1283                                         page->mapping->host->i_ino,
1284                                         page->index);
1285                         memset(page_address(page), 0, PAGE_SIZE);
1286                         continue;
1287                 }
1288
1289                 if (!page->buffers) {
1290                         create_empty_buffers(page, dev, PAGE_SIZE);
1291                         bh = page->buffers;
1292
1293                         clear_bit(BH_New, &bh->b_state);
1294                         set_bit(BH_Mapped, &bh->b_state);
1295                         bh->b_blocknr = (unsigned long)nioptr->offset;
1296
1297                         clear_bit(BH_Uptodate, &bh->b_state);
1298
1299                         ll_rw_block(READ, 1, &bh);
1300                 } else {
1301                         bh = page->buffers;
1302
1303                         /* if buffer already existed, it must be the
1304                          * one we mapped before, check it */
1305                         LASSERT(!test_bit(BH_New, &bh->b_state));
1306                         LASSERT(test_bit(BH_Mapped, &bh->b_state));
1307                         LASSERT(bh->b_blocknr == (unsigned long)nioptr->offset);
1308
1309                         /* wait it's io completion */
1310                         if (test_bit(BH_Lock, &bh->b_state))
1311                                 wait_on_buffer(bh);
1312
1313                         if (!test_bit(BH_Uptodate, &bh->b_state))
1314                                 ll_rw_block(READ, 1, &bh);
1315                 }
1316
1317
1318                 /* must do syncronous write here */
1319                 wait_on_buffer(bh);
1320                 if (!buffer_uptodate(bh)) {
1321                         /* I/O error */
1322                         rc = -EIO;
1323                         goto out_req;
1324                 }
1325         }
1326
1327 out_req:
1328         ptlrpc_req_finished(request);
1329         RETURN(rc);
1330 }
1331
1332 static int sanosc_brw_write(struct lustre_handle *conn, struct obdo *oa,
1333                             struct lov_stripe_md *lsm, obd_count page_count,
1334                             struct brw_page *pga)
1335 {
1336         struct ptlrpc_request *request = NULL;
1337         struct ost_body *body;
1338         struct niobuf_remote *nioptr;
1339         struct obd_ioobj *iooptr;
1340         int rc, size[3] = {sizeof(*body)}, mapped = 0;
1341         int swab;
1342         ENTRY;
1343
1344         size[1] = sizeof(struct obd_ioobj);
1345         size[2] = page_count * sizeof(*nioptr);
1346
1347         request = ptlrpc_prep_req(class_conn2cliimp(conn), OST_SAN_WRITE,
1348                                   3, size, NULL);
1349         if (!request)
1350                 RETURN(-ENOMEM);
1351
1352         body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof (*body));
1353         iooptr = lustre_msg_buf(request->rq_reqmsg, 1, sizeof (*iooptr));
1354         nioptr = lustre_msg_buf(request->rq_reqmsg, 2,
1355                                 sizeof (*nioptr) * page_count);
1356
1357         memcpy(&body->oa, oa, sizeof(body->oa));
1358
1359         iooptr->ioo_id = oa->o_id;
1360         iooptr->ioo_gr = oa->o_valid & 0 ? oa->o_gr : 0;
1361         iooptr->ioo_type = oa->o_mode;
1362         iooptr->ioo_bufcnt = page_count;
1363
1364         /* pack request */
1365         for (mapped = 0; mapped < page_count; mapped++, nioptr++) {
1366                 LASSERT(PageLocked(pga[mapped].pg));
1367                 LASSERT(mapped == 0 || pga[mapped].off > pga[mapped - 1].off);
1368
1369                 nioptr->offset = pga[mapped].off;
1370                 nioptr->len    = pga[mapped].count;
1371                 nioptr->flags  = pga[mapped].flag;
1372         }
1373
1374         size[1] = page_count * sizeof(*nioptr);
1375         request->rq_replen = lustre_msg_size(2, size);
1376
1377         rc = ptlrpc_queue_wait(request);
1378         if (rc)
1379                 GOTO(out_req, rc);
1380
1381         swab = lustre_msg_swabbed (request->rq_repmsg);
1382         LASSERT_REPSWAB (request, 1);
1383         nioptr = lustre_msg_buf(request->rq_repmsg, 1, size[1]);
1384         if (!nioptr) {
1385                 CERROR("absent/short niobuf array\n");
1386                 GOTO(out_req, rc = -EPROTO);
1387         }
1388
1389         /* actual write */
1390         for (mapped = 0; mapped < page_count; mapped++, nioptr++) {
1391                 struct page *page = pga[mapped].pg;
1392                 struct buffer_head *bh;
1393                 kdev_t dev;
1394
1395                 if (swab)
1396                         lustre_swab_niobuf_remote (nioptr);
1397
1398                 /* got san device associated */
1399                 LASSERT(class_conn2obd(conn));
1400                 dev = class_conn2obd(conn)->u.cli.cl_sandev;
1401
1402                 if (!page->buffers) {
1403                         create_empty_buffers(page, dev, PAGE_SIZE);
1404                 } else {
1405                         /* checking */
1406                         LASSERT(!test_bit(BH_New, &page->buffers->b_state));
1407                         LASSERT(test_bit(BH_Mapped, &page->buffers->b_state));
1408                         LASSERT(page->buffers->b_blocknr ==
1409                                 (unsigned long)nioptr->offset);
1410                 }
1411                 bh = page->buffers;
1412
1413                 LASSERT(bh);
1414
1415                 /* if buffer locked, wait it's io completion */
1416                 if (test_bit(BH_Lock, &bh->b_state))
1417                         wait_on_buffer(bh);
1418
1419                 clear_bit(BH_New, &bh->b_state);
1420                 set_bit(BH_Mapped, &bh->b_state);
1421
1422                 /* override the block nr */
1423                 bh->b_blocknr = (unsigned long)nioptr->offset;
1424
1425                 /* we are about to write it, so set it
1426                  * uptodate/dirty
1427                  * page lock should garentee no race condition here */
1428                 set_bit(BH_Uptodate, &bh->b_state);
1429                 set_bit(BH_Dirty, &bh->b_state);
1430
1431                 ll_rw_block(WRITE, 1, &bh);
1432
1433                 /* must do syncronous write here */
1434                 wait_on_buffer(bh);
1435                 if (!buffer_uptodate(bh) || test_bit(BH_Dirty, &bh->b_state)) {
1436                         /* I/O error */
1437                         rc = -EIO;
1438                         goto out_req;
1439                 }
1440         }
1441
1442 out_req:
1443         ptlrpc_req_finished(request);
1444         RETURN(rc);
1445 }
1446
1447 static int sanosc_brw(int cmd, struct lustre_handle *conn, struct obdo *oa,
1448                       struct lov_stripe_md *lsm, obd_count page_count,
1449                       struct brw_page *pga, struct obd_trans_info *oti)
1450 {
1451         ENTRY;
1452
1453         while (page_count) {
1454                 obd_count pages_per_brw;
1455                 int rc;
1456
1457                 if (page_count > OSC_BRW_MAX_IOV)
1458                         pages_per_brw = OSC_BRW_MAX_IOV;
1459                 else
1460                         pages_per_brw = page_count;
1461
1462                 if (cmd & OBD_BRW_WRITE)
1463                         rc = sanosc_brw_write(conn, oa, lsm, pages_per_brw,pga);
1464                 else
1465                         rc = sanosc_brw_read(conn, oa, lsm, pages_per_brw, pga);
1466
1467                 if (rc != 0)
1468                         RETURN(rc);
1469
1470                 page_count -= pages_per_brw;
1471                 pga += pages_per_brw;
1472         }
1473         RETURN(0);
1474 }
1475 #endif
1476 #endif
1477
1478 static int osc_mark_page_dirty(struct lustre_handle *conn,
1479                                struct lov_stripe_md *lsm, unsigned long offset)
1480 {
1481         struct client_obd *cli = &class_conn2obd(conn)->u.cli;
1482         struct otree *dirty_ot = lsm->lsm_oinfo[0].loi_dirty_ot;
1483         int rc;
1484         ENTRY;
1485
1486         down(&cli->cl_dirty_sem);
1487
1488 #if 0
1489         if (cli->cl_ost_can_grant &&
1490             (cli->cl_dirty + PAGE_CACHE_SIZE >= cli->cl_dirty_granted)) {
1491                 CDEBUG(D_INODE, "granted "LPU64" < "LPU64"\n",
1492                        cli->cl_dirty_granted, cli->cl_dirty + PAGE_CACHE_SIZE);
1493                 GOTO(out, rc = -EDQUOT);
1494         }
1495 #endif
1496
1497         rc = ot_mark_offset(dirty_ot, offset);
1498         if (rc)
1499                 GOTO(out, rc);
1500
1501         cli->cl_dirty += PAGE_CACHE_SIZE;
1502         CDEBUG(D_INODE, "dirtied off %lu, now "LPU64" bytes dirty\n",
1503                         offset, cli->cl_dirty);
1504 out:
1505         up(&cli->cl_dirty_sem);
1506         RETURN(rc);
1507 }
1508
1509 static int osc_clear_dirty_pages(struct lustre_handle *conn,
1510                                  struct lov_stripe_md *lsm,
1511                                  unsigned long start, unsigned long end,
1512                                  unsigned long *cleared)
1513 {
1514         struct client_obd *cli = &class_conn2obd(conn)->u.cli;
1515         struct otree *dirty_ot = lsm->lsm_oinfo[0].loi_dirty_ot;
1516         unsigned long old_marked, new_marked;
1517         int rc;
1518         ENTRY;
1519
1520         down(&cli->cl_dirty_sem);
1521
1522         old_marked = ot_num_marked(dirty_ot);
1523
1524         rc = ot_clear_extent(dirty_ot, start, end);
1525         if (rc)
1526                 GOTO(out, rc);
1527
1528         new_marked = ot_num_marked(dirty_ot);
1529
1530         LASSERT(new_marked <= old_marked);
1531         LASSERT(old_marked * PAGE_CACHE_SIZE <= cli->cl_dirty);
1532         *cleared = old_marked - new_marked;
1533         cli->cl_dirty -= (__u64)*cleared << PAGE_CACHE_SHIFT;
1534         CDEBUG(D_INODE, "cleared [%lu,%lu], now "LPU64" bytes dirty\n",
1535                         start, end, cli->cl_dirty);
1536
1537 out:
1538         up(&cli->cl_dirty_sem);
1539         RETURN(rc);
1540 }
1541
1542 static int osc_last_dirty_offset(struct lustre_handle *conn,
1543                                  struct lov_stripe_md *lsm,
1544                                  unsigned long *offset)
1545 {
1546         struct otree *dirty_ot = lsm->lsm_oinfo[0].loi_dirty_ot;
1547         int rc;
1548         ENTRY;
1549
1550         rc = ot_last_marked(dirty_ot, offset);
1551         RETURN(rc);
1552 }
1553
1554 static int osc_enqueue(struct lustre_handle *connh, struct lov_stripe_md *lsm,
1555                        struct lustre_handle *parent_lock,
1556                        __u32 type, void *extentp, int extent_len, __u32 mode,
1557                        int *flags, void *callback, void *data,
1558                        struct lustre_handle *lockh)
1559 {
1560         struct ldlm_res_id res_id = { .name = {lsm->lsm_object_id} };
1561         struct obd_device *obd = class_conn2obd(connh);
1562         struct ldlm_extent *extent = extentp;
1563         int rc;
1564         ENTRY;
1565
1566         /* Filesystem lock extents are extended to page boundaries so that
1567          * dealing with the page cache is a little smoother.  */
1568         extent->start -= extent->start & ~PAGE_MASK;
1569         extent->end |= ~PAGE_MASK;
1570
1571         /* Next, search for already existing extent locks that will cover us */
1572         rc = ldlm_lock_match(obd->obd_namespace, LDLM_FL_MATCH_DATA, &res_id,
1573                              type, extent, sizeof(extent), mode, data, lockh);
1574         if (rc == 1)
1575                 /* We already have a lock, and it's referenced */
1576                 RETURN(ELDLM_OK);
1577
1578         /* If we're trying to read, we also search for an existing PW lock.  The
1579          * VFS and page cache already protect us locally, so lots of readers/
1580          * writers can share a single PW lock.
1581          *
1582          * There are problems with conversion deadlocks, so instead of
1583          * converting a read lock to a write lock, we'll just enqueue a new
1584          * one.
1585          *
1586          * At some point we should cancel the read lock instead of making them
1587          * send us a blocking callback, but there are problems with canceling
1588          * locks out from other users right now, too. */
1589
1590         if (mode == LCK_PR) {
1591                 rc = ldlm_lock_match(obd->obd_namespace, LDLM_FL_MATCH_DATA,
1592                                      &res_id, type, extent, sizeof(extent),
1593                                      LCK_PW, data, lockh);
1594                 if (rc == 1) {
1595                         /* FIXME: This is not incredibly elegant, but it might
1596                          * be more elegant than adding another parameter to
1597                          * lock_match.  I want a second opinion. */
1598                         ldlm_lock_addref(lockh, LCK_PR);
1599                         ldlm_lock_decref(lockh, LCK_PW);
1600
1601                         RETURN(ELDLM_OK);
1602                 }
1603         }
1604
1605         rc = ldlm_cli_enqueue(connh, NULL, obd->obd_namespace, parent_lock,
1606                               res_id, type, extent, sizeof(extent), mode, flags,
1607                               ldlm_completion_ast, callback, data, lockh);
1608         RETURN(rc);
1609 }
1610
1611 static int osc_match(struct lustre_handle *connh, struct lov_stripe_md *lsm,
1612                        __u32 type, void *extentp, int extent_len, __u32 mode,
1613                        int *flags, void *data, struct lustre_handle *lockh)
1614 {
1615         struct ldlm_res_id res_id = { .name = {lsm->lsm_object_id} };
1616         struct obd_device *obd = class_conn2obd(connh);
1617         struct ldlm_extent *extent = extentp;
1618         int rc;
1619         ENTRY;
1620
1621         /* Filesystem lock extents are extended to page boundaries so that
1622          * dealing with the page cache is a little smoother */
1623         extent->start -= extent->start & ~PAGE_MASK;
1624         extent->end |= ~PAGE_MASK;
1625
1626         /* Next, search for already existing extent locks that will cover us */
1627         rc = ldlm_lock_match(obd->obd_namespace, *flags, &res_id, type,
1628                              extent, sizeof(extent), mode, data, lockh);
1629         if (rc)
1630                 RETURN(rc);
1631
1632         /* If we're trying to read, we also search for an existing PW lock.  The
1633          * VFS and page cache already protect us locally, so lots of readers/
1634          * writers can share a single PW lock. */
1635         if (mode == LCK_PR) {
1636                 rc = ldlm_lock_match(obd->obd_namespace, *flags, &res_id,
1637                                      type, extent, sizeof(extent), LCK_PW,
1638                                      data, lockh);
1639                 if (rc == 1) {
1640                         /* FIXME: This is not incredibly elegant, but it might
1641                          * be more elegant than adding another parameter to
1642                          * lock_match.  I want a second opinion. */
1643                         ldlm_lock_addref(lockh, LCK_PR);
1644                         ldlm_lock_decref(lockh, LCK_PW);
1645                 }
1646         }
1647         RETURN(rc);
1648 }
1649
1650 static int osc_cancel(struct lustre_handle *oconn, struct lov_stripe_md *md,
1651                       __u32 mode, struct lustre_handle *lockh)
1652 {
1653         ENTRY;
1654
1655         ldlm_lock_decref(lockh, mode);
1656
1657         RETURN(0);
1658 }
1659
1660 static int osc_cancel_unused(struct lustre_handle *connh,
1661                              struct lov_stripe_md *lsm, int flags, void *opaque)
1662 {
1663         struct obd_device *obd = class_conn2obd(connh);
1664         struct ldlm_res_id res_id = { .name = {lsm->lsm_object_id} };
1665
1666         return ldlm_cli_cancel_unused(obd->obd_namespace, &res_id, flags,
1667                                       opaque);
1668 }
1669
1670 static int osc_statfs(struct obd_device *obd, struct obd_statfs *osfs,
1671                       unsigned long max_age)
1672 {
1673         struct obd_statfs *msfs;
1674         struct ptlrpc_request *request;
1675         int rc, size = sizeof(*osfs);
1676         ENTRY;
1677
1678         /* We could possibly pass max_age in the request (as an absolute
1679          * timestamp or a "seconds.usec ago") so the target can avoid doing
1680          * extra calls into the filesystem if that isn't necessary (e.g.
1681          * during mount that would help a bit).  Having relative timestamps
1682          * is not so great if request processing is slow, while absolute
1683          * timestamps are not ideal because they need time synchronization. */
1684         request = ptlrpc_prep_req(obd->u.cli.cl_import, OST_STATFS,0,NULL,NULL);
1685         if (!request)
1686                 RETURN(-ENOMEM);
1687
1688         request->rq_replen = lustre_msg_size(1, &size);
1689
1690         rc = ptlrpc_queue_wait(request);
1691         if (rc) {
1692                 CERROR("%s failed: rc = %d\n", __FUNCTION__, rc);
1693                 GOTO(out, rc);
1694         }
1695
1696         msfs = lustre_swab_repbuf(request, 0, sizeof(*msfs),
1697                                   lustre_swab_obd_statfs);
1698         if (msfs == NULL) {
1699                 CERROR("Can't unpack obd_statfs\n");
1700                 GOTO(out, rc = -EPROTO);
1701         }
1702
1703         memcpy(osfs, msfs, sizeof(*osfs));
1704
1705         EXIT;
1706  out:
1707         ptlrpc_req_finished(request);
1708         return rc;
1709 }
1710
1711 /* Retrieve object striping information.
1712  *
1713  * @lmmu is a pointer to an in-core struct with lmm_ost_count indicating
1714  * the maximum number of OST indices which will fit in the user buffer.
1715  * lmm_magic must be LOV_MAGIC (we only use 1 slot here).
1716  */
1717 static int osc_getstripe(struct lustre_handle *conn, struct lov_stripe_md *lsm,
1718                          struct lov_mds_md *lmmu)
1719 {
1720         struct lov_mds_md lmm, *lmmk;
1721         int rc, lmm_size;
1722         ENTRY;
1723
1724         if (!lsm)
1725                 RETURN(-ENODATA);
1726
1727         rc = copy_from_user(&lmm, lmmu, sizeof(lmm));
1728         if (rc)
1729                 RETURN(-EFAULT);
1730
1731         if (lmm.lmm_magic != LOV_MAGIC)
1732                 RETURN(-EINVAL);
1733
1734         if (lmm.lmm_ost_count < 1)
1735                 RETURN(-EOVERFLOW);
1736
1737         lmm_size = sizeof(lmm) + sizeof(lmm.lmm_objects[0]);
1738         OBD_ALLOC(lmmk, lmm_size);
1739         if (rc < 0)
1740                 RETURN(rc);
1741
1742         lmmk->lmm_stripe_count = 1;
1743         lmmk->lmm_ost_count = 1;
1744         lmmk->lmm_object_id = lsm->lsm_object_id;
1745         lmmk->lmm_objects[0].l_object_id = lsm->lsm_object_id;
1746
1747         if (copy_to_user(lmmu, lmmk, lmm_size))
1748                 rc = -EFAULT;
1749
1750         OBD_FREE(lmmk, lmm_size);
1751
1752         RETURN(rc);
1753 }
1754
1755 static int osc_iocontrol(unsigned int cmd, struct lustre_handle *conn, int len,
1756                          void *karg, void *uarg)
1757 {
1758         struct obd_device *obd = class_conn2obd(conn);
1759         struct obd_ioctl_data *data = karg;
1760         int err = 0;
1761         ENTRY;
1762
1763         switch (cmd) {
1764         case IOC_OSC_REGISTER_LOV: {
1765                 if (obd->u.cli.cl_containing_lov)
1766                         GOTO(out, err = -EALREADY);
1767                 obd->u.cli.cl_containing_lov = (struct obd_device *)karg;
1768                 GOTO(out, err);
1769         }
1770         case OBD_IOC_LOV_GET_CONFIG: {
1771                 char *buf;
1772                 struct lov_desc *desc;
1773                 struct obd_uuid uuid;
1774
1775                 buf = NULL;
1776                 len = 0;
1777                 if (obd_ioctl_getdata(&buf, &len, (void *)uarg))
1778                         GOTO(out, err = -EINVAL);
1779
1780                 data = (struct obd_ioctl_data *)buf;
1781
1782                 if (sizeof(*desc) > data->ioc_inllen1) {
1783                         OBD_FREE(buf, len);
1784                         GOTO(out, err = -EINVAL);
1785                 }
1786
1787                 if (data->ioc_inllen2 < sizeof(uuid)) {
1788                         OBD_FREE(buf, len);
1789                         GOTO(out, err = -EINVAL);
1790                 }
1791
1792                 desc = (struct lov_desc *)data->ioc_inlbuf1;
1793                 desc->ld_tgt_count = 1;
1794                 desc->ld_active_tgt_count = 1;
1795                 desc->ld_default_stripe_count = 1;
1796                 desc->ld_default_stripe_size = 0;
1797                 desc->ld_default_stripe_offset = 0;
1798                 desc->ld_pattern = 0;
1799                 memcpy(&desc->ld_uuid, &obd->obd_uuid, sizeof(uuid));
1800
1801                 memcpy(data->ioc_inlbuf2, &obd->obd_uuid, sizeof(uuid));
1802
1803                 err = copy_to_user((void *)uarg, buf, len);
1804                 if (err)
1805                         err = -EFAULT;
1806                 obd_ioctl_freedata(buf, len);
1807                 GOTO(out, err);
1808         }
1809         case LL_IOC_LOV_SETSTRIPE:
1810                 err = obd_alloc_memmd(conn, karg);
1811                 if (err > 0)
1812                         err = 0;
1813                 GOTO(out, err);
1814         case LL_IOC_LOV_GETSTRIPE:
1815                 err = osc_getstripe(conn, karg, uarg);
1816                 GOTO(out, err);
1817         case OBD_IOC_CLIENT_RECOVER:
1818                 err = ptlrpc_recover_import(obd->u.cli.cl_import,
1819                                             data->ioc_inlbuf1);
1820                 GOTO(out, err);
1821         case IOC_OSC_SET_ACTIVE:
1822                 err = ptlrpc_set_import_active(obd->u.cli.cl_import,
1823                                                data->ioc_offset);
1824                 GOTO(out, err);
1825         default:
1826                 CERROR("unrecognised ioctl %#x by %s\n", cmd, current->comm);
1827                 GOTO(out, err = -ENOTTY);
1828         }
1829 out:
1830         return err;
1831 }
1832
1833 static int osc_get_info(struct lustre_handle *conn, obd_count keylen,
1834                         void *key, __u32 *vallen, void *val)
1835 {
1836         ENTRY;
1837         if (!vallen || !val)
1838                 RETURN(-EFAULT);
1839
1840         if (keylen > strlen("lock_to_stripe") &&
1841             strcmp(key, "lock_to_stripe") == 0) {
1842                 __u32 *stripe = val;
1843                 *vallen = sizeof(*stripe);
1844                 *stripe = 0;
1845                 RETURN(0);
1846         }
1847         RETURN(-EINVAL);
1848 }
1849
1850 static int osc_set_info(struct lustre_handle *conn, obd_count keylen,
1851                         void *key, obd_count vallen, void *val)
1852 {
1853         struct ptlrpc_request *req;
1854         int rc, size = keylen;
1855         char *bufs[1] = {key};
1856         ENTRY;
1857
1858         if (keylen < strlen("mds_conn") ||
1859             memcmp(key, "mds_conn", strlen("mds_conn")) != 0)
1860                 RETURN(-EINVAL);
1861
1862         req = ptlrpc_prep_req(class_conn2cliimp(conn), OST_SET_INFO, 1,
1863                               &size, bufs);
1864         if (req == NULL)
1865                 RETURN(-ENOMEM);
1866
1867         req->rq_replen = lustre_msg_size(0, NULL);
1868         rc = ptlrpc_queue_wait(req);
1869         ptlrpc_req_finished(req);
1870         RETURN(rc);
1871 }
1872
1873 static int osc_log_cancel(struct lustre_handle *conn, struct lov_stripe_md *lsm,
1874                           int count, struct llog_cookie *cookies, int flags)
1875 {
1876         struct obd_device *obd = class_conn2obd(conn);
1877         struct llog_commit_data *llcd;
1878         struct client_obd *cli;
1879         int rc = 0;
1880         ENTRY;
1881
1882         cli = &obd->u.cli;
1883         if ((count == 0 || cookies == NULL ||
1884              memcmp(cookies, &zero_cookie, sizeof(*cookies)) == 0)) {
1885                 down(&cli->cl_sem);
1886                 if (cli->cl_llcd == NULL || !(flags & OBD_LLOG_FL_SENDNOW))
1887                         GOTO(out, rc);
1888
1889                 llcd = cli->cl_llcd;
1890                 GOTO(send_now, rc);
1891         }
1892
1893         down(&cli->cl_sem);
1894         llcd = cli->cl_llcd;
1895         if (llcd == NULL) {
1896                 llcd = llcd_grab();
1897                 if (llcd == NULL) {
1898                         CERROR("couldn't get an llcd - dropped "LPX64":%x+%u\n",
1899                                cookies->lgc_lgl.lgl_oid,
1900                                cookies->lgc_lgl.lgl_ogen, cookies->lgc_index);
1901                         GOTO(out, rc = -ENOMEM);
1902                 }
1903                 llcd->llcd_import = cli->cl_import;
1904                 cli->cl_llcd = llcd;
1905         }
1906
1907         memcpy(llcd->llcd_cookies + llcd->llcd_cookiebytes, cookies,
1908                sizeof(*cookies));
1909         llcd->llcd_cookiebytes += sizeof(*cookies);
1910
1911         /* If we can't fit any more cookies into the page, we need to send it */
1912 send_now:
1913         if ((PAGE_SIZE - llcd->llcd_cookiebytes < sizeof(*cookies) ||
1914              flags & OBD_LLOG_FL_SENDNOW)) {
1915                 cli->cl_llcd = NULL;
1916                 llcd_send(llcd);
1917         }
1918 out:
1919         up(&cli->cl_sem);
1920
1921         return rc;
1922 }
1923
1924 static int osc_disconnect(struct lustre_handle *conn, int flags)
1925 {
1926         struct obd_device *obd = class_conn2obd(conn);
1927
1928         /* flush any remaining cancel messages out to the target */
1929         if (obd->u.cli.cl_llcd)
1930                 osc_log_cancel(conn, NULL, 0, NULL, OBD_LLOG_FL_SENDNOW);
1931
1932         return client_import_disconnect(conn, flags);
1933 }
1934
1935 static int osc_log_add(struct lustre_handle *conn,
1936                        struct llog_handle *cathandle,
1937                        struct llog_trans_hdr *rec, struct lov_stripe_md *lsm,
1938                        struct llog_cookie *logcookies, int numcookies)
1939 {
1940         ENTRY;
1941         LASSERT(logcookies && numcookies > 0);
1942
1943         llog_add_record(cathandle, rec, logcookies);
1944
1945         RETURN(1);
1946 }
1947
1948 struct obd_ops osc_obd_ops = {
1949         o_owner:        THIS_MODULE,
1950         o_attach:       osc_attach,
1951         o_detach:       osc_detach,
1952         o_setup:        client_obd_setup,
1953         o_cleanup:      client_obd_cleanup,
1954         o_connect:      client_import_connect,
1955         o_disconnect:   osc_disconnect,
1956         o_statfs:       osc_statfs,
1957         o_packmd:       osc_packmd,
1958         o_unpackmd:     osc_unpackmd,
1959         o_create:       osc_create,
1960         o_destroy:      osc_destroy,
1961         o_getattr:      osc_getattr,
1962         o_getattr_async:osc_getattr_async,
1963         o_setattr:      osc_setattr,
1964         o_open:         osc_open,
1965         o_close:        osc_close,
1966         o_brw:          osc_brw,
1967         o_brw_async:    osc_brw_async,
1968         o_punch:        osc_punch,
1969         o_enqueue:      osc_enqueue,
1970         o_match:        osc_match,
1971         o_cancel:       osc_cancel,
1972         o_cancel_unused:osc_cancel_unused,
1973         o_iocontrol:    osc_iocontrol,
1974         o_get_info:     osc_get_info,
1975         o_set_info:     osc_set_info,
1976         o_log_cancel:   osc_log_cancel,
1977         o_log_add:      osc_log_add,
1978         o_mark_page_dirty:    osc_mark_page_dirty,
1979         o_clear_dirty_pages:  osc_clear_dirty_pages,
1980         o_last_dirty_offset:  osc_last_dirty_offset,
1981 };
1982
1983 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
1984 struct obd_ops sanosc_obd_ops = {
1985         o_owner:        THIS_MODULE,
1986         o_attach:       osc_attach,
1987         o_detach:       osc_detach,
1988         o_cleanup:      client_obd_cleanup,
1989         o_connect:      client_import_connect,
1990         o_disconnect:   client_import_disconnect,
1991         o_statfs:       osc_statfs,
1992         o_packmd:       osc_packmd,
1993         o_unpackmd:     osc_unpackmd,
1994         o_create:       osc_create,
1995         o_destroy:      osc_destroy,
1996         o_getattr:      osc_getattr,
1997         o_getattr_async: osc_getattr_async,
1998         o_setattr:      osc_setattr,
1999         o_open:         osc_open,
2000         o_close:        osc_close,
2001         o_setup:        client_sanobd_setup,
2002         o_brw:          sanosc_brw,
2003         o_punch:        osc_punch,
2004         o_enqueue:      osc_enqueue,
2005         o_match:        osc_match,
2006         o_cancel:       osc_cancel,
2007         o_cancel_unused: osc_cancel_unused,
2008         o_iocontrol:    osc_iocontrol,
2009         o_log_cancel:   osc_log_cancel,
2010         o_log_add:      osc_log_add,
2011         o_mark_page_dirty:   osc_mark_page_dirty,
2012         o_clear_dirty_pages: osc_clear_dirty_pages,
2013         o_last_dirty_offset: osc_last_dirty_offset,
2014 };
2015 #endif
2016
2017 int __init osc_init(void)
2018 {
2019         struct lprocfs_static_vars lvars, sanlvars;
2020         int rc;
2021         ENTRY;
2022
2023         LASSERT(sizeof(struct obd_client_handle) <= FD_OSTDATA_SIZE);
2024         LASSERT(sizeof(struct obd_client_handle) <= OBD_INLINESZ);
2025
2026         lprocfs_init_vars(osc,&lvars);
2027         lprocfs_init_vars(osc,&sanlvars);
2028
2029         rc = class_register_type(&osc_obd_ops, lvars.module_vars,
2030                                  LUSTRE_OSC_NAME);
2031         if (rc)
2032                 RETURN(rc);
2033
2034 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
2035         rc = class_register_type(&sanosc_obd_ops, sanlvars.module_vars,
2036                                  LUSTRE_SANOSC_NAME);
2037         if (rc)
2038                 class_unregister_type(LUSTRE_OSC_NAME);
2039 #endif
2040
2041         RETURN(rc);
2042 }
2043
2044 static void /*__exit*/ osc_exit(void)
2045 {
2046 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
2047         class_unregister_type(LUSTRE_SANOSC_NAME);
2048 #endif
2049         class_unregister_type(LUSTRE_OSC_NAME);
2050 }
2051
2052 #ifdef __KERNEL__
2053 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
2054 MODULE_DESCRIPTION("Lustre Object Storage Client (OSC)");
2055 MODULE_LICENSE("GPL");
2056
2057 module_init(osc_init);
2058 module_exit(osc_exit);
2059 #endif