Whamcloud - gitweb
Land b_smallfix onto HEAD (20040330_2312)
[fs/lustre-release.git] / lustre / lov / lov_obd.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Copyright (C) 2002, 2003 Cluster File Systems, Inc.
5  * Author: Phil Schwan <phil@clusterfs.com>
6  *         Peter Braam <braam@clusterfs.com>
7  *         Mike Shaver <shaver@clusterfs.com>
8  *
9  *   This file is part of Lustre, http://www.lustre.org.
10  *
11  *   Lustre is free software; you can redistribute it and/or
12  *   modify it under the terms of version 2 of the GNU General Public
13  *   License as published by the Free Software Foundation.
14  *
15  *   Lustre is distributed in the hope that it will be useful,
16  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *   GNU General Public License for more details.
19  *
20  *   You should have received a copy of the GNU General Public License
21  *   along with Lustre; if not, write to the Free Software
22  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  */
24
25 #ifndef EXPORT_SYMTAB
26 # define EXPORT_SYMTAB
27 #endif
28 #define DEBUG_SUBSYSTEM S_LOV
29 #ifdef __KERNEL__
30 #include <linux/slab.h>
31 #include <linux/module.h>
32 #include <linux/init.h>
33 #include <linux/slab.h>
34 #include <linux/pagemap.h>
35 #include <linux/seq_file.h>
36 #include <asm/div64.h>
37 #else
38 #include <liblustre.h>
39 #endif
40
41 #include <linux/obd_support.h>
42 #include <linux/lustre_lib.h>
43 #include <linux/lustre_net.h>
44 #include <linux/lustre_idl.h>
45 #include <linux/lustre_dlm.h>
46 #include <linux/lustre_mds.h>
47 #include <linux/obd_class.h>
48 #include <linux/obd_lov.h>
49 #include <linux/obd_ost.h>
50 #include <linux/lprocfs_status.h>
51
52 #include "lov_internal.h"
53
54 static int lov_stripe_offset(struct lov_stripe_md *lsm, obd_off lov_off,
55                              int stripeno, obd_off *obd_off);
56
57 struct lov_lock_handles {
58         struct portals_handle llh_handle;
59         atomic_t llh_refcount;
60         int llh_stripe_count;
61         struct lustre_handle llh_handles[0];
62 };
63
64 static void lov_llh_addref(void *llhp)
65 {
66         struct lov_lock_handles *llh = llhp;
67
68         atomic_inc(&llh->llh_refcount);
69         CDEBUG(D_INFO, "GETting llh %p : new refcount %d\n", llh,
70                atomic_read(&llh->llh_refcount));
71 }
72
73 static struct lov_lock_handles *lov_llh_new(struct lov_stripe_md *lsm)
74 {
75         struct lov_lock_handles *llh;
76
77         OBD_ALLOC(llh, sizeof *llh +
78                   sizeof(*llh->llh_handles) * lsm->lsm_stripe_count);
79         if (llh == NULL) {
80                 CERROR("out of memory\n");
81                 return NULL;
82         }
83         atomic_set(&llh->llh_refcount, 2);
84         llh->llh_stripe_count = lsm->lsm_stripe_count;
85         INIT_LIST_HEAD(&llh->llh_handle.h_link);
86         class_handle_hash(&llh->llh_handle, lov_llh_addref);
87         return llh;
88 }
89
90 static struct lov_lock_handles *lov_handle2llh(struct lustre_handle *handle)
91 {
92         ENTRY;
93         LASSERT(handle != NULL);
94         RETURN(class_handle2object(handle->cookie));
95 }
96
97 static void lov_llh_put(struct lov_lock_handles *llh)
98 {
99         CDEBUG(D_INFO, "PUTting llh %p : new refcount %d\n", llh,
100                atomic_read(&llh->llh_refcount) - 1);
101         LASSERT(atomic_read(&llh->llh_refcount) > 0 &&
102                 atomic_read(&llh->llh_refcount) < 0x5a5a);
103         if (atomic_dec_and_test(&llh->llh_refcount)) {
104                 LASSERT(list_empty(&llh->llh_handle.h_link));
105                 OBD_FREE(llh, sizeof *llh +
106                          sizeof(*llh->llh_handles) * llh->llh_stripe_count);
107         }
108 }
109
110 static void lov_llh_destroy(struct lov_lock_handles *llh)
111 {
112         class_handle_unhash(&llh->llh_handle);
113         lov_llh_put(llh);
114 }
115
116 /* obd methods */
117 int lov_attach(struct obd_device *dev, obd_count len, void *data)
118 {
119         struct lprocfs_static_vars lvars;
120         int rc;
121
122         lprocfs_init_vars(lov, &lvars);
123         rc = lprocfs_obd_attach(dev, lvars.obd_vars);
124         if (rc == 0) {
125 #ifdef __KERNEL__
126                 struct proc_dir_entry *entry;
127
128                 entry = create_proc_entry("target_obd", 0444, 
129                                           dev->obd_proc_entry);
130                 if (entry == NULL) {
131                         rc = -ENOMEM;
132                 } else {
133                         entry->proc_fops = &lov_proc_target_fops;
134                         entry->data = dev;
135                 }
136 #endif
137         }
138         return rc;
139 }
140
141 int lov_detach(struct obd_device *dev)
142 {
143         return lprocfs_obd_detach(dev);
144 }
145
146 static int lov_connect(struct lustre_handle *conn, struct obd_device *obd,
147                        struct obd_uuid *cluuid)
148 {
149         struct ptlrpc_request *req = NULL;
150         struct lov_obd *lov = &obd->u.lov;
151         struct lov_desc *desc = &lov->desc;
152         struct lov_tgt_desc *tgts;
153         struct obd_export *exp;
154         int rc, rc2, i;
155         ENTRY;
156
157         rc = class_connect(conn, obd, cluuid);
158         if (rc)
159                 RETURN(rc);
160
161         exp = class_conn2export(conn);
162
163         /* We don't want to actually do the underlying connections more than
164          * once, so keep track. */
165         lov->refcount++;
166         if (lov->refcount > 1) {
167                 class_export_put(exp);
168                 RETURN(0);
169         }
170
171         for (i = 0, tgts = lov->tgts; i < desc->ld_tgt_count; i++, tgts++) {
172                 struct obd_uuid *tgt_uuid = &tgts->uuid;
173                 struct obd_device *tgt_obd;
174                 struct obd_uuid lov_osc_uuid = { "LOV_OSC_UUID" };
175                 struct lustre_handle conn = {0, };
176
177                 LASSERT( tgt_uuid != NULL);
178
179                 tgt_obd = class_find_client_obd(tgt_uuid, LUSTRE_OSC_NAME, 
180                                                 &obd->obd_uuid);
181
182                 if (!tgt_obd) {
183                         CERROR("Target %s not attached\n", tgt_uuid->uuid);
184                         GOTO(out_disc, rc = -EINVAL);
185                 }
186
187                 if (!tgt_obd->obd_set_up) {
188                         CERROR("Target %s not set up\n", tgt_uuid->uuid);
189                         GOTO(out_disc, rc = -EINVAL);
190                 }
191
192                 if (tgt_obd->u.cli.cl_import->imp_invalid) {
193                         CERROR("not connecting OSC %s; administratively "
194                                "disabled\n", tgt_uuid->uuid);
195                         rc = obd_register_observer(tgt_obd, obd);
196                         if (rc) {
197                                 CERROR("Target %s register_observer error %d; "
198                                        "will not be able to reactivate\n",
199                                        tgt_uuid->uuid, rc);
200                         }
201                         continue;
202                 }
203
204                 rc = obd_connect(&conn, tgt_obd, &lov_osc_uuid);
205                 if (rc) {
206                         CERROR("Target %s connect error %d\n", tgt_uuid->uuid,
207                                rc);
208                         GOTO(out_disc, rc);
209                 }
210                 tgts->ltd_exp = class_conn2export(&conn);
211
212                 rc = obd_register_observer(tgt_obd, obd);
213                 if (rc) {
214                         CERROR("Target %s register_observer error %d\n",
215                                tgt_uuid->uuid, rc);
216                         obd_disconnect(tgts->ltd_exp, 0);
217                         GOTO(out_disc, rc);
218                 }
219
220                 desc->ld_active_tgt_count++;
221                 tgts->active = 1;
222         }
223
224         ptlrpc_req_finished(req);
225         class_export_put(exp);
226         RETURN (0);
227
228  out_disc:
229         while (i-- > 0) {
230                 struct obd_uuid uuid;
231                 --tgts;
232                 --desc->ld_active_tgt_count;
233                 tgts->active = 0;
234                 /* save for CERROR below; (we know it's terminated) */
235                 uuid = tgts->uuid;
236                 rc2 = obd_disconnect(tgts->ltd_exp, 0);
237                 if (rc2)
238                         CERROR("error: LOV target %s disconnect on OST idx %d: "
239                                "rc = %d\n", uuid.uuid, i, rc2);
240         }
241         class_disconnect(exp, 0);
242         RETURN (rc);
243 }
244
245 static int lov_disconnect(struct obd_export *exp, int flags)
246 {
247         struct obd_device *obd = class_exp2obd(exp);
248         struct lov_obd *lov = &obd->u.lov;
249         int rc, i;
250         ENTRY;
251
252         if (!lov->tgts)
253                 goto out_local;
254
255         /* Only disconnect the underlying layers on the final disconnect. */
256         lov->refcount--;
257         if (lov->refcount != 0)
258                 goto out_local;
259
260         for (i = 0; i < lov->desc.ld_tgt_count; i++) {
261                 if (lov->tgts[i].ltd_exp == NULL)
262                         continue;
263
264                 if (obd->obd_no_recov) {
265                         /* Pass it on to our clients.
266                          * XXX This should be an argument to disconnect,
267                          * XXX not a back-door flag on the OBD.  Ah well.
268                          */
269                         struct obd_device *osc_obd;
270                         osc_obd = class_exp2obd(lov->tgts[i].ltd_exp);
271                         if (osc_obd)
272                                 osc_obd->obd_no_recov = 1;
273                 }
274
275                 obd_register_observer(lov->tgts[i].ltd_exp->exp_obd, NULL);
276
277                 rc = obd_disconnect(lov->tgts[i].ltd_exp, flags);
278                 if (rc) {
279                         if (lov->tgts[i].active) {
280                                 CERROR("Target %s disconnect error %d\n",
281                                        lov->tgts[i].uuid.uuid, rc);
282                         }
283                         rc = 0;
284                 }
285                 if (lov->tgts[i].active) {
286                         lov->desc.ld_active_tgt_count--;
287                         lov->tgts[i].active = 0;
288                 }
289                 lov->tgts[i].ltd_exp = NULL;
290         }
291
292  out_local:
293         rc = class_disconnect(exp, 0);
294         RETURN(rc);
295 }
296
297 /* Error codes:
298  *
299  *  -EINVAL  : UUID can't be found in the LOV's target list
300  *  -ENOTCONN: The UUID is found, but the target connection is bad (!)
301  *  -EBADF   : The UUID is found, but the OBD is the wrong type (!)
302  */
303 static int lov_set_osc_active(struct lov_obd *lov, struct obd_uuid *uuid,
304                               int activate)
305 {
306         struct obd_device *obd;
307         struct lov_tgt_desc *tgt;
308         int i, rc = 0;
309         ENTRY;
310
311         CDEBUG(D_INFO, "Searching in lov %p for uuid %s (activate=%d)\n",
312                lov, uuid->uuid, activate);
313
314         spin_lock(&lov->lov_lock);
315         for (i = 0, tgt = lov->tgts; i < lov->desc.ld_tgt_count; i++, tgt++) {
316                 CDEBUG(D_INFO, "lov idx %d is %s conn "LPX64"\n",
317                        i, tgt->uuid.uuid, tgt->ltd_exp->exp_handle.h_cookie);
318                 if (strncmp(uuid->uuid, tgt->uuid.uuid, sizeof uuid->uuid) == 0)
319                         break;
320         }
321
322         if (i == lov->desc.ld_tgt_count)
323                 GOTO(out, rc = -EINVAL);
324
325         obd = class_exp2obd(tgt->ltd_exp);
326         if (obd == NULL) {
327                 /* This can happen if OST failure races with node shutdown */
328                 GOTO(out, rc = -ENOTCONN);
329         }
330
331         CDEBUG(D_INFO, "Found OBD %s=%s device %d (%p) type %s at LOV idx %d\n",
332                obd->obd_name, obd->obd_uuid.uuid, obd->obd_minor, obd,
333                obd->obd_type->typ_name, i);
334         LASSERT(strcmp(obd->obd_type->typ_name, "osc") == 0);
335
336         if (tgt->active == activate) {
337                 CDEBUG(D_INFO, "OBD %p already %sactive!\n", obd,
338                        activate ? "" : "in");
339                 GOTO(out, rc);
340         }
341
342         CDEBUG(D_INFO, "Marking OBD %p %sactive\n", obd, activate ? "" : "in");
343
344         tgt->active = activate;
345         if (activate)
346                 lov->desc.ld_active_tgt_count++;
347         else
348                 lov->desc.ld_active_tgt_count--;
349
350         EXIT;
351  out:
352         spin_unlock(&lov->lov_lock);
353         return rc;
354 }
355
356 static int lov_notify(struct obd_device *obd, struct obd_device *watched,
357                        int active)
358 {
359         int rc;
360         struct obd_uuid *uuid;
361
362         if (strcmp(watched->obd_type->typ_name, "osc")) {
363                 CERROR("unexpected notification of %s %s!\n",
364                        watched->obd_type->typ_name,
365                        watched->obd_name);
366                 return -EINVAL;
367         }
368         uuid = &watched->u.cli.cl_import->imp_target_uuid;
369
370         /*
371          * Must notify (MDS) before we mark the OSC as active, so that
372          * the orphan deletion happens without interference from racing
373          * creates.
374          */
375         if (obd->obd_observer) {
376                 /* Pass the notification up the chain. */
377                 rc = obd_notify(obd->obd_observer, watched, active);
378                 if (rc)
379                         RETURN(rc);
380         }
381
382         rc = lov_set_osc_active(&obd->u.lov, uuid, active);
383
384         if (rc) {
385                 CERROR("%sactivation of %s failed: %d\n",
386                        active ? "" : "de", uuid->uuid, rc);
387         }
388         RETURN(rc);
389 }
390
391 static int lov_setup(struct obd_device *obd, obd_count len, void *buf)
392 {
393         struct lustre_cfg *lcfg = buf;
394         struct lov_desc *desc;
395         struct lov_obd *lov = &obd->u.lov;
396         struct obd_uuid *uuids;
397         struct lov_tgt_desc *tgts;
398         int i;
399         int count;
400         int rc = 0;
401         ENTRY;
402
403         if (lcfg->lcfg_inllen1 < 1) {
404                 CERROR("LOV setup requires a descriptor\n");
405                 RETURN(-EINVAL);
406         }
407
408         if (lcfg->lcfg_inllen2 < 1) {
409                 CERROR("LOV setup requires an OST UUID list\n");
410                 RETURN(-EINVAL);
411         }
412
413         desc = (struct lov_desc *)lcfg->lcfg_inlbuf1;
414         if (sizeof(*desc) > lcfg->lcfg_inllen1) {
415                 CERROR("descriptor size wrong: %d > %d\n",
416                        (int)sizeof(*desc), lcfg->lcfg_inllen1);
417                 RETURN(-EINVAL);
418         }
419
420         count = desc->ld_tgt_count;
421         uuids = (struct obd_uuid *)lcfg->lcfg_inlbuf2;
422         if (sizeof(*uuids) * count != lcfg->lcfg_inllen2) {
423                 CERROR("UUID array size wrong: %u * %u != %u\n",
424                        (int)sizeof(*uuids), count, lcfg->lcfg_inllen2);
425                 RETURN(-EINVAL);
426         }
427
428         /* Because of 64-bit divide/mod operations only work with a 32-bit
429          * divisor in a 32-bit kernel, we cannot support a stripe width
430          * of 4GB or larger on 32-bit CPUs.
431          */
432         if ((desc->ld_default_stripe_count ?
433              desc->ld_default_stripe_count : desc->ld_tgt_count) *
434              desc->ld_default_stripe_size > ~0UL) {
435                 CERROR("LOV: stripe width "LPU64"x%u > %lu on 32-bit system\n",
436                        desc->ld_default_stripe_size,
437                        desc->ld_default_stripe_count ?
438                        desc->ld_default_stripe_count : desc->ld_tgt_count,~0UL);
439                 RETURN(-EINVAL);
440         }
441
442         lov->bufsize = sizeof(struct lov_tgt_desc) * count;
443         OBD_ALLOC(lov->tgts, lov->bufsize);
444         if (lov->tgts == NULL) {
445                 CERROR("Out of memory\n");
446                 RETURN(-EINVAL);
447         }
448
449         lov->desc = *desc;
450         spin_lock_init(&lov->lov_lock);
451
452         for (i = 0, tgts = lov->tgts; i < desc->ld_tgt_count; i++, tgts++) {
453                 struct obd_uuid *uuid = &tgts->uuid;
454
455                 /* NULL termination already checked */
456                 *uuid = uuids[i];
457         }
458
459
460         RETURN(rc);
461 }
462
463 static int lov_cleanup(struct obd_device *obd, int flags) 
464 {
465         struct lov_obd *lov = &obd->u.lov;
466
467         OBD_FREE(lov->tgts, lov->bufsize);
468         RETURN(0);
469 }
470
471
472 /* compute object size given "stripeno" and the ost size */
473 static obd_size lov_stripe_size(struct lov_stripe_md *lsm, obd_size ost_size,
474                                 int stripeno)
475 {
476         unsigned long ssize  = lsm->lsm_stripe_size;
477         unsigned long swidth = ssize * lsm->lsm_stripe_count;
478         unsigned long stripe_size;
479         obd_size lov_size;
480
481         if (ost_size == 0)
482                 return 0;
483
484         /* do_div(a, b) returns a % b, and a = a / b */
485         stripe_size = do_div(ost_size, ssize);
486
487         if (stripe_size)
488                 lov_size = ost_size * swidth + stripeno * ssize + stripe_size;
489         else
490                 lov_size = (ost_size - 1) * swidth + (stripeno + 1) * ssize;
491
492         return lov_size;
493 }
494
495 static void lov_merge_attrs(struct obdo *tgt, struct obdo *src, obd_flag valid,
496                             struct lov_stripe_md *lsm, int stripeno, int *set)
497 {
498         valid &= src->o_valid;
499
500         if (*set) {
501                 if (valid & OBD_MD_FLSIZE) {
502                         /* this handles sparse files properly */
503                         obd_size lov_size;
504
505                         lov_size = lov_stripe_size(lsm, src->o_size, stripeno);
506                         if (lov_size > tgt->o_size)
507                                 tgt->o_size = lov_size;
508                 }
509                 if (valid & OBD_MD_FLBLOCKS)
510                         tgt->o_blocks += src->o_blocks;
511                 if (valid & OBD_MD_FLBLKSZ)
512                         tgt->o_blksize += src->o_blksize;
513                 if (valid & OBD_MD_FLCTIME && tgt->o_ctime < src->o_ctime)
514                         tgt->o_ctime = src->o_ctime;
515                 if (valid & OBD_MD_FLMTIME && tgt->o_mtime < src->o_mtime)
516                         tgt->o_mtime = src->o_mtime;
517         } else {
518                 memcpy(tgt, src, sizeof(*tgt));
519                 tgt->o_id = lsm->lsm_object_id;
520                 if (valid & OBD_MD_FLSIZE)
521                         tgt->o_size = lov_stripe_size(lsm,src->o_size,stripeno);
522                 *set = 1;
523         }
524 }
525
526 #ifndef log2
527 #define log2(n) ffz(~(n))
528 #endif
529
530 static int lov_clear_orphans(struct obd_export *export, struct obdo *src_oa,
531                              struct lov_stripe_md **ea,
532                              struct obd_trans_info *oti)
533 {
534         struct lov_obd *lov;
535         struct obdo *tmp_oa;
536         struct obd_uuid *ost_uuid = NULL;
537         int rc = 0, i;
538         ENTRY;
539
540         LASSERT(src_oa->o_valid & OBD_MD_FLFLAGS &&
541                 src_oa->o_flags == OBD_FL_DELORPHAN);
542
543         lov = &export->exp_obd->u.lov;
544
545         tmp_oa = obdo_alloc();
546         if (tmp_oa == NULL)
547                 RETURN(-ENOMEM);
548
549         if (src_oa->o_valid & OBD_MD_FLINLINE) {
550                 ost_uuid = (struct obd_uuid *)src_oa->o_inline;
551                 CDEBUG(D_HA, "clearing orphans only for %s\n",
552                        ost_uuid->uuid);
553         }
554
555         for (i = 0; i < lov->desc.ld_tgt_count; i++) {
556                 struct lov_stripe_md obj_md;
557                 struct lov_stripe_md *obj_mdp = &obj_md;
558                 int err;
559
560                 /* if called for a specific target, we don't 
561                    care if it is not active. */
562                 if (lov->tgts[i].active == 0 && ost_uuid == NULL) {
563                         CDEBUG(D_HA, "lov idx %d inactive\n", i);
564                         continue;
565                 }
566
567                 if (ost_uuid && !obd_uuid_equals(ost_uuid, &lov->tgts[i].uuid))
568                         continue;
569
570                 memcpy(tmp_oa, src_oa, sizeof(*tmp_oa));
571                 
572                 /* XXX: LOV STACKING: use real "obj_mdp" sub-data */
573                 err = obd_create(lov->tgts[i].ltd_exp, tmp_oa, &obj_mdp, oti);
574                 if (err)
575                         /* This export will be disabled until it is recovered,
576                            and then orphan recovery will be completed. */
577                         CERROR("error in orphan recovery on OST idx %d/%d: "
578                                "rc = %d\n", i, lov->desc.ld_tgt_count, err);
579
580                 if (ost_uuid)
581                         break;
582         }
583         obdo_free(tmp_oa);
584         RETURN(rc);
585 }
586
587 #define LOV_CREATE_RESEED_INTERVAL 1000
588
589 /* the LOV expects oa->o_id to be set to the LOV object id */
590 static int lov_create(struct obd_export *exp, struct obdo *src_oa,
591                       struct lov_stripe_md **ea, struct obd_trans_info *oti)
592 {
593         static int ost_start_idx, ost_start_count;
594         struct lov_obd *lov;
595         struct lov_stripe_md *lsm;
596         struct lov_oinfo *loi = NULL;
597         struct obdo *tmp_oa, *ret_oa;
598         struct llog_cookie *cookies = NULL;
599         unsigned ost_count, ost_idx;
600         int set = 0, obj_alloc = 0, cookie_sent = 0, rc = 0, i;
601         ENTRY;
602
603         LASSERT(ea != NULL);
604
605         if ((src_oa->o_valid & OBD_MD_FLFLAGS) &&
606             src_oa->o_flags == OBD_FL_DELORPHAN) {
607                 rc = lov_clear_orphans(exp, src_oa, ea, oti);
608                 RETURN(rc);
609         }
610
611         if (exp == NULL)
612                 RETURN(-EINVAL);
613
614         lov = &exp->exp_obd->u.lov;
615
616         if (!lov->desc.ld_active_tgt_count)
617                 RETURN(-EIO);
618
619         /* Recreate a specific object id at the given OST index */
620         if (src_oa->o_valid & OBD_MD_FLFLAGS && src_oa->o_flags &
621                                                 OBD_FL_RECREATE_OBJS) {
622                  struct lov_stripe_md obj_md;
623                  struct lov_stripe_md *obj_mdp = &obj_md;
624
625                  ost_idx = src_oa->o_nlink;
626                  lsm = *ea;
627                  if (lsm == NULL)
628                         RETURN(-EINVAL);
629                  if (ost_idx >= lov->desc.ld_tgt_count)
630                          RETURN(-EINVAL);
631                  for (i = 0; i < lsm->lsm_stripe_count; i++) {
632                          if (lsm->lsm_oinfo[i].loi_ost_idx == ost_idx) {
633                                  if (lsm->lsm_oinfo[i].loi_id != src_oa->o_id)
634                                          RETURN(-EINVAL);
635                                  break;
636                          }
637                  }
638                  if (i == lsm->lsm_stripe_count)
639                          RETURN(-EINVAL);
640
641                  rc = obd_create(lov->tgts[ost_idx].ltd_exp, src_oa,
642                                  &obj_mdp, oti);
643                  RETURN(rc);
644         }
645
646         ret_oa = obdo_alloc();
647         if (!ret_oa)
648                 RETURN(-ENOMEM);
649
650         tmp_oa = obdo_alloc();
651         if (!tmp_oa)
652                 GOTO(out_oa, rc = -ENOMEM);
653
654         lsm = *ea;
655         if (lsm == NULL) {
656                 int stripes;
657                 ost_count = lov_get_stripecnt(lov, 0);
658
659                 /* If the MDS file was truncated up to some size, stripe over
660                  * enough OSTs to allow the file to be created at that size. */
661                 if (src_oa->o_valid & OBD_MD_FLSIZE) {
662                         stripes=((src_oa->o_size+LUSTRE_STRIPE_MAXBYTES)>>12)-1;
663                         do_div(stripes, (__u32)(LUSTRE_STRIPE_MAXBYTES >> 12));
664
665                         if (stripes > lov->desc.ld_active_tgt_count)
666                                 RETURN(-EFBIG);
667                         if (stripes > ost_count)
668                                 stripes = ost_count;
669                 } else {
670                         stripes = ost_count;
671                 }
672
673                 rc = lov_alloc_memmd(&lsm, stripes, lov->desc.ld_pattern ?
674                                      lov->desc.ld_pattern : LOV_PATTERN_RAID0);
675                 if (rc < 0)
676                         GOTO(out_tmp, rc);
677
678                 rc = 0;
679         }
680
681         ost_count = lov->desc.ld_tgt_count;
682
683         LASSERT(src_oa->o_valid & OBD_MD_FLID);
684         lsm->lsm_object_id = src_oa->o_id;
685         if (!lsm->lsm_stripe_size)
686                 lsm->lsm_stripe_size = lov->desc.ld_default_stripe_size;
687         if (!lsm->lsm_pattern) {
688                 lsm->lsm_pattern = lov->desc.ld_pattern ?
689                         lov->desc.ld_pattern : LOV_PATTERN_RAID0;
690         }
691
692         if (*ea == NULL || lsm->lsm_oinfo[0].loi_ost_idx >= ost_count) {
693                 if (--ost_start_count <= 0) {
694                         ost_start_idx = ll_insecure_random_int();
695                         ost_start_count = LOV_CREATE_RESEED_INTERVAL;
696                 } else if (lsm->lsm_stripe_count >=
697                            lov->desc.ld_active_tgt_count) {
698                         /* If we allocate from all of the stripes, make the
699                          * next file start on the next OST. */
700                         ++ost_start_idx;
701                 }
702                 ost_idx = ost_start_idx % ost_count;
703         } else {
704                 ost_idx = lsm->lsm_oinfo[0].loi_ost_idx;
705         }
706
707         CDEBUG(D_INODE, "allocating %d subobjs for objid "LPX64" at idx %d\n",
708                lsm->lsm_stripe_count, lsm->lsm_object_id, ost_idx);
709
710         /* XXX LOV STACKING: need to figure out how many real OSCs */
711         if (oti && (src_oa->o_valid & OBD_MD_FLCOOKIE)) {
712                 oti_alloc_cookies(oti, lsm->lsm_stripe_count);
713                 if (!oti->oti_logcookies)
714                         GOTO(out_cleanup, rc = -ENOMEM);
715                 cookies = oti->oti_logcookies;
716         }
717
718         loi = lsm->lsm_oinfo;
719         for (i = 0; i < ost_count; i++, ost_idx = (ost_idx + 1) % ost_count) {
720                 struct lov_stripe_md obj_md;
721                 struct lov_stripe_md *obj_mdp = &obj_md;
722                 int err;
723
724                 ++ost_start_idx;
725                 if (lov->tgts[ost_idx].active == 0) {
726                         CDEBUG(D_HA, "lov idx %d inactive\n", ost_idx);
727                         continue;
728                 }
729
730                 /* create data objects with "parent" OA */
731                 memcpy(tmp_oa, src_oa, sizeof(*tmp_oa));
732
733                 /* XXX When we start creating objects on demand, we need to
734                  *     make sure that we always create the object on the
735                  *     stripe which holds the existing file size.
736                  */
737                 if (src_oa->o_valid & OBD_MD_FLSIZE) {
738                         if (lov_stripe_offset(lsm, src_oa->o_size, i,
739                                               &tmp_oa->o_size) < 0 &&
740                             tmp_oa->o_size)
741                                 tmp_oa->o_size--;
742
743                         CDEBUG(D_INODE, "stripe %d has size "LPU64"/"LPU64"\n",
744                                i, tmp_oa->o_size, src_oa->o_size);
745                 }
746
747
748                 /* XXX: LOV STACKING: use real "obj_mdp" sub-data */
749                 err = obd_create(lov->tgts[ost_idx].ltd_exp, tmp_oa, &obj_mdp,
750                                  oti);
751                 if (err) {
752                         if (lov->tgts[ost_idx].active) {
753                                 CERROR("error creating objid "LPX64" sub-object"
754                                        " on OST idx %d/%d: rc = %d\n",
755                                        src_oa->o_id, ost_idx,
756                                        lsm->lsm_stripe_count, err);
757                                 if (err > 0) {
758                                         CERROR("obd_create returned invalid "
759                                                "err %d\n", err);
760                                         err = -EIO;
761                                 }
762                         }
763                         if (!rc)
764                                 rc = err;
765                         continue;
766                 }
767                 if (oti->oti_objid)
768                         oti->oti_objid[ost_idx] = tmp_oa->o_id;
769                 loi->loi_id = tmp_oa->o_id;
770                 loi->loi_ost_idx = ost_idx;
771                 CDEBUG(D_INODE, "objid "LPX64" has subobj "LPX64" at idx %d\n",
772                        lsm->lsm_object_id, loi->loi_id, ost_idx);
773
774                 lov_merge_attrs(ret_oa, tmp_oa, tmp_oa->o_valid, lsm,
775                                 obj_alloc, &set);
776                 loi_init(loi);
777
778                 if (cookies)
779                         ++oti->oti_logcookies;
780                 if (tmp_oa->o_valid & OBD_MD_FLCOOKIE)
781                         ++cookie_sent;
782                 ++obj_alloc;
783                 ++loi;
784
785                 /* If we have allocated enough objects, we are OK */
786                 if (obj_alloc == lsm->lsm_stripe_count)
787                         GOTO(out_done, rc = 0);
788         }
789
790         if (obj_alloc == 0) {
791                 if (rc == 0)
792                         rc = -EIO;
793                 GOTO(out_cleanup, rc);
794         }
795
796         /* If we were passed specific striping params, then a failure to
797          * meet those requirements is an error, since we can't reallocate
798          * that memory (it might be part of a larger array or something).
799          *
800          * We can only get here if lsm_stripe_count was originally > 1.
801          */
802         if (*ea != NULL) {
803                 CERROR("can't lstripe objid "LPX64": have %u want %u, rc %d\n",
804                        lsm->lsm_object_id, obj_alloc, lsm->lsm_stripe_count,rc);
805                 if (rc == 0)
806                         rc = -EFBIG;
807                 GOTO(out_cleanup, rc);
808         } else {
809                 struct lov_stripe_md *lsm_new;
810                 /* XXX LOV STACKING call into osc for sizes */
811                 unsigned oldsize, newsize;
812
813                 if (oti && cookies && cookie_sent) {
814                         oldsize = lsm->lsm_stripe_count * sizeof(*cookies);
815                         newsize = obj_alloc * sizeof(*cookies);
816
817                         oti_alloc_cookies(oti, obj_alloc);
818                         if (oti->oti_logcookies) {
819                                 memcpy(oti->oti_logcookies, cookies, newsize);
820                                 OBD_FREE(cookies, oldsize);
821                                 cookies = oti->oti_logcookies;
822                         } else {
823                                 CWARN("'leaking' %d bytes\n", oldsize-newsize);
824                         }
825                 }
826
827                 CWARN("using fewer stripes for object "LPX64": old %u new %u\n",
828                       lsm->lsm_object_id, lsm->lsm_stripe_count, obj_alloc);
829                 oldsize = lov_stripe_md_size(lsm->lsm_stripe_count);
830                 newsize = lov_stripe_md_size(obj_alloc);
831                 OBD_ALLOC(lsm_new, newsize);
832                 if (lsm_new != NULL) {
833                         memcpy(lsm_new, lsm, newsize);
834                         lsm_new->lsm_stripe_count = obj_alloc;
835                         OBD_FREE(lsm, oldsize);
836                         lsm = lsm_new;
837                 } else {
838                         CWARN("'leaking' %d bytes\n", oldsize - newsize);
839                 }
840                 rc = 0;
841         }
842         EXIT;
843  out_done:
844         *ea = lsm;
845         if (src_oa->o_valid & OBD_MD_FLSIZE &&
846             ret_oa->o_size != src_oa->o_size) {
847                 CERROR("original size "LPU64" isn't new object size "LPU64"\n",
848                        src_oa->o_size, ret_oa->o_size);
849                 LBUG();
850         }
851         ret_oa->o_id = src_oa->o_id;
852         memcpy(src_oa, ret_oa, sizeof(*src_oa));
853
854  out_tmp:
855         obdo_free(tmp_oa);
856  out_oa:
857         obdo_free(ret_oa);
858         if (oti && cookies) {
859                 oti->oti_logcookies = cookies;
860                 if (!cookie_sent) {
861                         oti_free_cookies(oti);
862                         src_oa->o_valid &= ~OBD_MD_FLCOOKIE;
863                 } else {
864                         src_oa->o_valid |= OBD_MD_FLCOOKIE;
865                 }
866         }
867         RETURN(rc);
868
869  out_cleanup:
870         while (obj_alloc-- > 0) {
871                 struct obd_export *sub_exp;
872                 int err;
873
874                 --loi;
875                 sub_exp = lov->tgts[loi->loi_ost_idx].ltd_exp;
876                 /* destroy already created objects here */
877                 memcpy(tmp_oa, src_oa, sizeof(*tmp_oa));
878                 tmp_oa->o_id = loi->loi_id;
879
880                 err = obd_destroy(sub_exp, tmp_oa, NULL, oti);
881                 if (err)
882                         CERROR("Failed to uncreate objid "LPX64" subobj "LPX64
883                                " on OST idx %d: rc = %d\n", src_oa->o_id,
884                                loi->loi_id, loi->loi_ost_idx, err);
885         }
886         if (*ea == NULL)
887                 obd_free_memmd(exp, &lsm);
888         goto out_tmp;
889 }
890
891 #define lsm_bad_magic(LSMP)                                     \
892 ({                                                              \
893         struct lov_stripe_md *_lsm__ = (LSMP);                  \
894         int _ret__ = 0;                                         \
895         if (!_lsm__) {                                          \
896                 CERROR("LOV requires striping ea\n");           \
897                 _ret__ = 1;                                     \
898         } else if (_lsm__->lsm_magic != LOV_MAGIC) {            \
899                 CERROR("LOV striping magic bad %#x != %#x\n",   \
900                        _lsm__->lsm_magic, LOV_MAGIC);           \
901                 _ret__ = 1;                                     \
902         }                                                       \
903         _ret__;                                                 \
904 })
905
906 static int lov_destroy(struct obd_export *exp, struct obdo *oa,
907                        struct lov_stripe_md *lsm, struct obd_trans_info *oti)
908 {
909         struct obdo tmp;
910         struct lov_obd *lov;
911         struct lov_oinfo *loi;
912         int rc = 0, i;
913         ENTRY;
914
915         if (lsm_bad_magic(lsm))
916                 RETURN(-EINVAL);
917
918         if (!exp || !exp->exp_obd)
919                 RETURN(-ENODEV);
920
921         lov = &exp->exp_obd->u.lov;
922         for (i = 0,loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++,loi++) {
923                 int err;
924                 if (lov->tgts[loi->loi_ost_idx].active == 0) {
925                         CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
926                         /* Orphan clean up will (someday) fix this up. */
927                         if (oti != NULL && oa->o_valid & OBD_MD_FLCOOKIE)
928                                 oti->oti_logcookies++;
929                         continue;
930                 }
931
932                 memcpy(&tmp, oa, sizeof(tmp));
933                 tmp.o_id = loi->loi_id;
934                 err = obd_destroy(lov->tgts[loi->loi_ost_idx].ltd_exp, &tmp,
935                                   NULL, oti);
936                 if (err && lov->tgts[loi->loi_ost_idx].active) {
937                         CERROR("error: destroying objid "LPX64" subobj "
938                                LPX64" on OST idx %d: rc = %d\n",
939                                oa->o_id, loi->loi_id, loi->loi_ost_idx, err);
940                         if (!rc)
941                                 rc = err;
942                 }
943         }
944         RETURN(rc);
945 }
946
947 static int lov_getattr(struct obd_export *exp, struct obdo *oa,
948                        struct lov_stripe_md *lsm)
949 {
950         struct obdo tmp;
951         struct lov_obd *lov;
952         struct lov_oinfo *loi;
953         int i, rc = 0, set = 0;
954         ENTRY;
955
956         if (lsm_bad_magic(lsm))
957                 RETURN(-EINVAL);
958
959         if (!exp || !exp->exp_obd)
960                 RETURN(-ENODEV);
961
962         lov = &exp->exp_obd->u.lov;
963
964         CDEBUG(D_INFO, "objid "LPX64": %ux%u byte stripes\n",
965                lsm->lsm_object_id, lsm->lsm_stripe_count, lsm->lsm_stripe_size);
966         for (i = 0,loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++,loi++) {
967                 int err;
968
969                 if (lov->tgts[loi->loi_ost_idx].active == 0) {
970                         CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
971                         continue;
972                 }
973
974                 CDEBUG(D_INFO, "objid "LPX64"[%d] has subobj "LPX64" at idx "
975                        "%u\n", oa->o_id, i, loi->loi_id, loi->loi_ost_idx);
976                 /* create data objects with "parent" OA */
977                 memcpy(&tmp, oa, sizeof(tmp));
978                 tmp.o_id = loi->loi_id;
979
980                 err = obd_getattr(lov->tgts[loi->loi_ost_idx].ltd_exp, &tmp,
981                                   NULL);
982                 if (err) {
983                         if (lov->tgts[loi->loi_ost_idx].active) {
984                                 CERROR("error: getattr objid "LPX64" subobj "
985                                        LPX64" on OST idx %d: rc = %d\n",
986                                        oa->o_id, loi->loi_id, loi->loi_ost_idx,
987                                        err);
988                                 RETURN(err);
989                         }
990                 } else {
991                         lov_merge_attrs(oa, &tmp, tmp.o_valid, lsm, i, &set);
992                 }
993         }
994         if (!set)
995                 rc = -EIO;
996         RETURN(rc);
997 }
998
999 static int lov_getattr_interpret(struct ptlrpc_request_set *rqset, void *data, 
1000                                  int rc)
1001 {
1002         struct lov_getattr_async_args *aa = data;
1003         struct lov_stripe_md *lsm = aa->aa_lsm;
1004         struct obdo          *oa = aa->aa_oa;
1005         struct obdo          *obdos = aa->aa_obdos;
1006         struct lov_oinfo     *loi;
1007         int                   i;
1008         int                   set = 0;
1009         ENTRY;
1010
1011         if (rc == 0) {
1012                 /* NB all stripe requests succeeded to get here */
1013
1014                 for (i = 0, loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count;
1015                      i++, loi++) {
1016                         if (obdos[i].o_valid == 0)      /* inactive stripe */
1017                                 continue;
1018
1019                         lov_merge_attrs(oa, &obdos[i], obdos[i].o_valid, lsm,
1020                                         i, &set);
1021                 }
1022
1023                 if (!set) {
1024                         CERROR ("No stripes had valid attrs\n");
1025                         rc = -EIO;
1026                 }
1027         }
1028
1029         OBD_FREE (obdos, lsm->lsm_stripe_count * sizeof (*obdos));
1030         RETURN (rc);
1031 }
1032
1033 static int lov_getattr_async(struct obd_export *exp, struct obdo *oa,
1034                               struct lov_stripe_md *lsm,
1035                               struct ptlrpc_request_set *rqset)
1036 {
1037         struct obdo *obdos;
1038         struct lov_obd *lov;
1039         struct lov_oinfo *loi;
1040         struct lov_getattr_async_args *aa;
1041         int i, rc = 0, set = 0;
1042         ENTRY;
1043
1044         if (lsm_bad_magic(lsm))
1045                 RETURN(-EINVAL);
1046
1047         if (!exp || !exp->exp_obd)
1048                 RETURN(-ENODEV);
1049
1050         lov = &exp->exp_obd->u.lov;
1051
1052         OBD_ALLOC (obdos, lsm->lsm_stripe_count * sizeof (*obdos));
1053         if (obdos == NULL)
1054                 RETURN(-ENOMEM);
1055
1056         CDEBUG(D_INFO, "objid "LPX64": %ux%u byte stripes\n",
1057                lsm->lsm_object_id, lsm->lsm_stripe_count, lsm->lsm_stripe_size);
1058         for (i = 0,loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++,loi++) {
1059                 int err;
1060
1061                 if (lov->tgts[loi->loi_ost_idx].active == 0) {
1062                         CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
1063                         /* leaves obdos[i].obd_valid unset */
1064                         continue;
1065                 }
1066
1067                 CDEBUG(D_INFO, "objid "LPX64"[%d] has subobj "LPX64" at idx "
1068                        "%u\n", oa->o_id, i, loi->loi_id, loi->loi_ost_idx);
1069                 /* create data objects with "parent" OA */
1070                 memcpy(&obdos[i], oa, sizeof(obdos[i]));
1071                 obdos[i].o_id = loi->loi_id;
1072
1073                 err = obd_getattr_async(lov->tgts[loi->loi_ost_idx].ltd_exp,
1074                                          &obdos[i], NULL, rqset);
1075                 if (err) {
1076                         CERROR("error: getattr objid "LPX64" subobj "
1077                                LPX64" on OST idx %d: rc = %d\n",
1078                                oa->o_id, loi->loi_id, loi->loi_ost_idx,
1079                                err);
1080                         GOTO(out_obdos, rc = err);
1081                 }
1082                 set = 1;
1083         }
1084         if (!set)
1085                 GOTO (out_obdos, rc = -EIO);
1086
1087         LASSERT (rqset->set_interpret == NULL);
1088         rqset->set_interpret = lov_getattr_interpret;
1089         LASSERT (sizeof (rqset->set_args) >= sizeof (*aa));
1090         aa = (struct lov_getattr_async_args *)&rqset->set_args;
1091         aa->aa_lsm = lsm;
1092         aa->aa_oa = oa;
1093         aa->aa_obdos = obdos;
1094         aa->aa_lov = lov;
1095         GOTO(out, rc = 0);
1096
1097 out_obdos:
1098         OBD_FREE (obdos, lsm->lsm_stripe_count * sizeof (*obdos));
1099 out:
1100         RETURN(rc);
1101 }
1102
1103
1104 static int lov_setattr(struct obd_export *exp, struct obdo *src_oa,
1105                        struct lov_stripe_md *lsm, struct obd_trans_info *oti)
1106 {
1107         struct obdo *tmp_oa, *ret_oa;
1108         struct lov_obd *lov;
1109         struct lov_oinfo *loi;
1110         int rc = 0, i, set = 0;
1111         ENTRY;
1112
1113         if (lsm_bad_magic(lsm))
1114                 RETURN(-EINVAL);
1115
1116         if (!exp || !exp->exp_obd)
1117                 RETURN(-ENODEV);
1118
1119         /* for now, we only expect time updates here */
1120         LASSERT(!(src_oa->o_valid & ~(OBD_MD_FLID|OBD_MD_FLTYPE | OBD_MD_FLMODE|
1121                                       OBD_MD_FLATIME | OBD_MD_FLMTIME |
1122                                       OBD_MD_FLCTIME | OBD_MD_FLFLAGS |
1123                                       OBD_MD_FLSIZE)));
1124         ret_oa = obdo_alloc();
1125         if (!ret_oa)
1126                 RETURN(-ENOMEM);
1127
1128         tmp_oa = obdo_alloc();
1129         if (!tmp_oa)
1130                 GOTO(out_oa, rc = -ENOMEM);
1131
1132         lov = &exp->exp_obd->u.lov;
1133         for (i = 0,loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++,loi++) {
1134                 int err;
1135
1136                 if (lov->tgts[loi->loi_ost_idx].active == 0) {
1137                         CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
1138                         continue;
1139                 }
1140
1141                 memcpy(tmp_oa, src_oa, sizeof(*tmp_oa));
1142                 tmp_oa->o_id = loi->loi_id;
1143
1144                 if (src_oa->o_valid & OBD_MD_FLSIZE) {
1145                         if (lov_stripe_offset(lsm, src_oa->o_size, i,
1146                                               &tmp_oa->o_size) < 0 &&
1147                             tmp_oa->o_size)
1148                                 tmp_oa->o_size--;
1149
1150                         CDEBUG(D_INODE, "stripe %d has size "LPU64"/"LPU64"\n",
1151                                i, tmp_oa->o_size, src_oa->o_size);
1152                 }
1153
1154                 err = obd_setattr(lov->tgts[loi->loi_ost_idx].ltd_exp, tmp_oa,
1155                                   NULL, NULL);
1156                 if (err) {
1157                         if (lov->tgts[loi->loi_ost_idx].active) {
1158                                 CERROR("error: setattr objid "LPX64" subobj "
1159                                        LPX64" on OST idx %d: rc = %d\n",
1160                                        src_oa->o_id, loi->loi_id,
1161                                        loi->loi_ost_idx, err);
1162                                 if (!rc)
1163                                         rc = err;
1164                         }
1165                         continue;
1166                 }
1167                 lov_merge_attrs(ret_oa, tmp_oa, tmp_oa->o_valid, lsm, i, &set);
1168         }
1169         if (!set && !rc)
1170                 rc = -EIO;
1171
1172         ret_oa->o_id = src_oa->o_id;
1173         memcpy(src_oa, ret_oa, sizeof(*src_oa));
1174         GOTO(out_tmp, rc);
1175 out_tmp:
1176         obdo_free(tmp_oa);
1177 out_oa:
1178         obdo_free(ret_oa);
1179         return rc;
1180 }
1181
1182 /* we have an offset in file backed by an lov and want to find out where
1183  * that offset lands in our given stripe of the file.  for the easy
1184  * case where the offset is within the stripe, we just have to scale the
1185  * offset down to make it relative to the stripe instead of the lov.
1186  *
1187  * the harder case is what to do when the offset doesn't intersect the
1188  * stripe.  callers will want start offsets clamped ahead to the start
1189  * of the nearest stripe in the file.  end offsets similarly clamped to the
1190  * nearest ending byte of a stripe in the file:
1191  *
1192  * all this function does is move offsets to the nearest region of the
1193  * stripe, and it does its work "mod" the full length of all the stripes.
1194  * consider a file with 3 stripes:
1195  *
1196  *             S                                              E
1197  * ---------------------------------------------------------------------
1198  * |    0    |     1     |     2     |    0    |     1     |     2     |
1199  * ---------------------------------------------------------------------
1200  *
1201  * to find stripe 1's offsets for S and E, it divides by the full stripe
1202  * width and does its math in the context of a single set of stripes:
1203  *
1204  *             S         E
1205  * -----------------------------------
1206  * |    0    |     1     |     2     |
1207  * -----------------------------------
1208  *
1209  * it'll notice that E is outside stripe 1 and clamp it to the end of the
1210  * stripe, then multiply it back out by lov_off to give the real offsets in
1211  * the stripe:
1212  *
1213  *   S                   E
1214  * ---------------------------------------------------------------------
1215  * |    1    |     1     |     1     |    1    |     1     |     1     |
1216  * ---------------------------------------------------------------------
1217  *
1218  * it would have done similarly and pulled S forward to the start of a 1
1219  * stripe if, say, S had landed in a 0 stripe.
1220  *
1221  * this rounding isn't always correct.  consider an E lov offset that lands
1222  * on a 0 stripe, the "mod stripe width" math will pull it forward to the
1223  * start of a 1 stripe, when in fact it wanted to be rounded back to the end
1224  * of a previous 1 stripe.  this logic is handled by callers and this is why:
1225  *
1226  * this function returns < 0 when the offset was "before" the stripe and
1227  * was moved forward to the start of the stripe in question;  0 when it
1228  * falls in the stripe and no shifting was done; > 0 when the offset
1229  * was outside the stripe and was pulled back to its final byte. */
1230 static int lov_stripe_offset(struct lov_stripe_md *lsm, obd_off lov_off,
1231                              int stripeno, obd_off *obd_off)
1232 {
1233         unsigned long ssize  = lsm->lsm_stripe_size;
1234         unsigned long swidth = ssize * lsm->lsm_stripe_count;
1235         unsigned long stripe_off, this_stripe;
1236         int ret = 0;
1237
1238         if (lov_off == OBD_OBJECT_EOF) {
1239                 *obd_off = OBD_OBJECT_EOF;
1240                 return 0;
1241         }
1242
1243         /* do_div(a, b) returns a % b, and a = a / b */
1244         stripe_off = do_div(lov_off, swidth);
1245
1246         this_stripe = stripeno * ssize;
1247         if (stripe_off < this_stripe) {
1248                 stripe_off = 0;
1249                 ret = -1;
1250         } else {
1251                 stripe_off -= this_stripe;
1252
1253                 if (stripe_off >= ssize) {
1254                         stripe_off = ssize;
1255                         ret = 1;
1256                 }
1257         }
1258
1259         *obd_off = lov_off * ssize + stripe_off;
1260         return ret;
1261 }
1262
1263 /* Given a whole-file size and a stripe number, give the file size which
1264  * corresponds to the individual object of that stripe.
1265  *
1266  * This behaves basically in the same was as lov_stripe_offset, except that
1267  * file sizes falling before the beginning of a stripe are clamped to the end
1268  * of the previous stripe, not the beginning of the next:
1269  *
1270  *                                               S
1271  * ---------------------------------------------------------------------
1272  * |    0    |     1     |     2     |    0    |     1     |     2     |
1273  * ---------------------------------------------------------------------
1274  *
1275  * if clamped to stripe 2 becomes:
1276  *
1277  *                                   S
1278  * ---------------------------------------------------------------------
1279  * |    0    |     1     |     2     |    0    |     1     |     2     |
1280  * ---------------------------------------------------------------------
1281  */
1282 static obd_off lov_size_to_stripe(struct lov_stripe_md *lsm, obd_off file_size,
1283                                   int stripeno)
1284 {
1285         unsigned long ssize  = lsm->lsm_stripe_size;
1286         unsigned long swidth = ssize * lsm->lsm_stripe_count;
1287         unsigned long stripe_off, this_stripe;
1288
1289         if (file_size == OBD_OBJECT_EOF)
1290                 return OBD_OBJECT_EOF;
1291
1292         /* do_div(a, b) returns a % b, and a = a / b */
1293         stripe_off = do_div(file_size, swidth);
1294
1295         this_stripe = stripeno * ssize;
1296         if (stripe_off < this_stripe) {
1297                 /* Move to end of previous stripe, or zero */
1298                 if (file_size > 0) {
1299                         file_size--;
1300                         stripe_off = ssize;
1301                 } else {
1302                         stripe_off = 0;
1303                 }
1304         } else {
1305                 stripe_off -= this_stripe;
1306
1307                 if (stripe_off >= ssize) {
1308                         /* Clamp to end of this stripe */
1309                         stripe_off = ssize;
1310                 }
1311         }
1312
1313         return (file_size * ssize + stripe_off);
1314 }
1315
1316 /* given an extent in an lov and a stripe, calculate the extent of the stripe
1317  * that is contained within the lov extent.  this returns true if the given
1318  * stripe does intersect with the lov extent. */
1319 static int lov_stripe_intersects(struct lov_stripe_md *lsm, int stripeno,
1320                                  obd_off start, obd_off end,
1321                                  obd_off *obd_start, obd_off *obd_end)
1322 {
1323         int start_side, end_side;
1324
1325         start_side = lov_stripe_offset(lsm, start, stripeno, obd_start);
1326         end_side = lov_stripe_offset(lsm, end, stripeno, obd_end);
1327
1328         CDEBUG(D_INODE, "["LPU64"->"LPU64"] -> [(%d) "LPU64"->"LPU64" (%d)]\n",
1329                start, end, start_side, *obd_start, *obd_end, end_side);
1330
1331         /* this stripe doesn't intersect the file extent when neither
1332          * start or the end intersected the stripe and obd_start and
1333          * obd_end got rounded up to the save value. */
1334         if (start_side != 0 && end_side != 0 && *obd_start == *obd_end)
1335                 return 0;
1336
1337         /* as mentioned in the lov_stripe_offset commentary, end
1338          * might have been shifted in the wrong direction.  This
1339          * happens when an end offset is before the stripe when viewed
1340          * through the "mod stripe size" math. we detect it being shifted
1341          * in the wrong direction and touch it up.
1342          * interestingly, this can't underflow since end must be > start
1343          * if we passed through the previous check.
1344          * (should we assert for that somewhere?) */
1345         if (end_side != 0)
1346                 (*obd_end)--;
1347
1348         return 1;
1349 }
1350
1351 /* compute which stripe number "lov_off" will be written into */
1352 static int lov_stripe_number(struct lov_stripe_md *lsm, obd_off lov_off)
1353 {
1354         unsigned long ssize  = lsm->lsm_stripe_size;
1355         unsigned long swidth = ssize * lsm->lsm_stripe_count;
1356         unsigned long stripe_off;
1357
1358         stripe_off = do_div(lov_off, swidth);
1359
1360         return stripe_off / ssize;
1361 }
1362
1363 /* FIXME: maybe we'll just make one node the authoritative attribute node, then
1364  * we can send this 'punch' to just the authoritative node and the nodes
1365  * that the punch will affect. */
1366 static int lov_punch(struct obd_export *exp, struct obdo *oa,
1367                      struct lov_stripe_md *lsm,
1368                      obd_off start, obd_off end, struct obd_trans_info *oti)
1369 {
1370         struct obdo tmp;
1371         struct lov_obd *lov;
1372         struct lov_oinfo *loi;
1373         int rc = 0, i;
1374         ENTRY;
1375
1376         if (lsm_bad_magic(lsm))
1377                 RETURN(-EINVAL);
1378
1379         if (!exp || !exp->exp_obd)
1380                 RETURN(-ENODEV);
1381
1382         lov = &exp->exp_obd->u.lov;
1383         for (i = 0,loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++,loi++) {
1384                 obd_off starti, endi;
1385                 int err;
1386
1387                 if (lov->tgts[loi->loi_ost_idx].active == 0) {
1388                         CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
1389                         continue;
1390                 }
1391
1392                 if (!lov_stripe_intersects(lsm, i, start, end, &starti, &endi))
1393                         continue;
1394
1395                 /* create data objects with "parent" OA */
1396                 memcpy(&tmp, oa, sizeof(tmp));
1397                 tmp.o_id = loi->loi_id;
1398
1399                 err = obd_punch(lov->tgts[loi->loi_ost_idx].ltd_exp, &tmp, NULL,
1400                                 starti, endi, NULL);
1401                 if (err) {
1402                         if (lov->tgts[loi->loi_ost_idx].active) {
1403                                 CERROR("error: punch objid "LPX64" subobj "LPX64
1404                                        " on OST idx %d: rc = %d\n", oa->o_id,
1405                                        loi->loi_id, loi->loi_ost_idx, err);
1406                         }
1407                         if (!rc)
1408                                 rc = err;
1409                 } else {
1410                         loi->loi_kms = loi->loi_rss = starti;
1411                 }
1412         }
1413         RETURN(rc);
1414 }
1415
1416 static int lov_sync(struct obd_export *exp, struct obdo *oa,
1417                     struct lov_stripe_md *lsm, obd_off start, obd_off end)
1418 {
1419         struct obdo *tmp;
1420         struct lov_obd *lov;
1421         struct lov_oinfo *loi;
1422         int rc = 0, i;
1423         ENTRY;
1424
1425         if (lsm_bad_magic(lsm))
1426                 RETURN(-EINVAL);
1427
1428         if (!exp->exp_obd)
1429                 RETURN(-ENODEV);
1430
1431         tmp = obdo_alloc();
1432         if (!tmp)
1433                 RETURN(-ENOMEM);
1434
1435         lov = &exp->exp_obd->u.lov;
1436         for (i = 0,loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++,loi++) {
1437                 obd_off starti, endi;
1438                 int err;
1439
1440                 if (lov->tgts[loi->loi_ost_idx].active == 0) {
1441                         CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
1442                         continue;
1443                 }
1444
1445                 if (!lov_stripe_intersects(lsm, i, start, end, &starti, &endi))
1446                         continue;
1447
1448                 memcpy(tmp, oa, sizeof(*tmp));
1449                 tmp->o_id = loi->loi_id;
1450
1451                 err = obd_sync(lov->tgts[loi->loi_ost_idx].ltd_exp, tmp, NULL,
1452                                starti, endi);
1453                 if (err) {
1454                         if (lov->tgts[loi->loi_ost_idx].active) {
1455                                 CERROR("error: fsync objid "LPX64" subobj "LPX64
1456                                        " on OST idx %d: rc = %d\n", oa->o_id,
1457                                        loi->loi_id, loi->loi_ost_idx, err);
1458                         }
1459                         if (!rc)
1460                                 rc = err;
1461                 }
1462         }
1463
1464         obdo_free(tmp);
1465         RETURN(rc);
1466 }
1467
1468 static int lov_brw_check(struct lov_obd *lov, struct obdo *oa,
1469                          struct lov_stripe_md *lsm,
1470                          obd_count oa_bufs, struct brw_page *pga)
1471 {
1472         int i, rc = 0;
1473
1474         /* The caller just wants to know if there's a chance that this
1475          * I/O can succeed */
1476         for (i = 0; i < oa_bufs; i++) {
1477                 int stripe = lov_stripe_number(lsm, pga[i].off);
1478                 int ost = lsm->lsm_oinfo[stripe].loi_ost_idx;
1479                 struct ldlm_extent ext, subext;
1480                 ext.start = pga[i].off;
1481                 ext.end = pga[i].off + pga[i].count;
1482
1483                 if (!lov_stripe_intersects(lsm, i, ext.start, ext.end,
1484                                            &subext.start, &subext.end))
1485                         continue;
1486
1487                 if (lov->tgts[ost].active == 0) {
1488                         CDEBUG(D_HA, "lov idx %d inactive\n", ost);
1489                         return -EIO;
1490                 }
1491                 rc = obd_brw(OBD_BRW_CHECK, lov->tgts[stripe].ltd_exp, oa,
1492                              NULL, 1, &pga[i], NULL);
1493                 if (rc)
1494                         break;
1495         }
1496         return rc;
1497 }
1498
1499 static int lov_brw(int cmd, struct obd_export *exp, struct obdo *src_oa,
1500                    struct lov_stripe_md *lsm, obd_count oa_bufs,
1501                    struct brw_page *pga, struct obd_trans_info *oti)
1502 {
1503         struct {
1504                 int bufct;
1505                 int index;
1506                 int subcount;
1507                 struct lov_stripe_md lsm;
1508                 int ost_idx;
1509         } *stripeinfo, *si, *si_last;
1510         struct obdo *ret_oa = NULL, *tmp_oa = NULL;
1511         struct lov_obd *lov;
1512         struct brw_page *ioarr;
1513         struct lov_oinfo *loi;
1514         int rc = 0, i, *where, stripe_count = lsm->lsm_stripe_count, set = 0;
1515         ENTRY;
1516
1517         if (lsm_bad_magic(lsm))
1518                 RETURN(-EINVAL);
1519
1520         lov = &exp->exp_obd->u.lov;
1521
1522         if (cmd == OBD_BRW_CHECK) {
1523                 rc = lov_brw_check(lov, src_oa, lsm, oa_bufs, pga);
1524                 RETURN(rc);
1525         }
1526
1527         OBD_ALLOC(stripeinfo, stripe_count * sizeof(*stripeinfo));
1528         if (!stripeinfo)
1529                 RETURN(-ENOMEM);
1530
1531         OBD_ALLOC(where, sizeof(*where) * oa_bufs);
1532         if (!where)
1533                 GOTO(out_sinfo, rc = -ENOMEM);
1534
1535         OBD_ALLOC(ioarr, sizeof(*ioarr) * oa_bufs);
1536         if (!ioarr)
1537                 GOTO(out_where, rc = -ENOMEM);
1538
1539         if (src_oa) {
1540                 ret_oa = obdo_alloc();
1541                 if (!ret_oa)
1542                         GOTO(out_ioarr, rc = -ENOMEM);
1543
1544                 tmp_oa = obdo_alloc();
1545                 if (!tmp_oa)
1546                         GOTO(out_oa, rc = -ENOMEM);
1547         }
1548
1549         for (i = 0; i < oa_bufs; i++) {
1550                 where[i] = lov_stripe_number(lsm, pga[i].off);
1551                 stripeinfo[where[i]].bufct++;
1552         }
1553
1554         for (i = 0, loi = lsm->lsm_oinfo, si_last = si = stripeinfo;
1555              i < stripe_count; i++, loi++, si_last = si, si++) {
1556                 if (i > 0)
1557                         si->index = si_last->index + si_last->bufct;
1558                 si->lsm.lsm_object_id = loi->loi_id;
1559                 si->ost_idx = loi->loi_ost_idx;
1560         }
1561
1562         for (i = 0; i < oa_bufs; i++) {
1563                 int which = where[i];
1564                 int shift;
1565
1566                 shift = stripeinfo[which].index + stripeinfo[which].subcount;
1567                 LASSERT(shift < oa_bufs);
1568                 ioarr[shift] = pga[i];
1569                 lov_stripe_offset(lsm, pga[i].off, which, &ioarr[shift].off);
1570                 stripeinfo[which].subcount++;
1571         }
1572
1573         for (i = 0, si = stripeinfo; i < stripe_count; i++, si++) {
1574                 int shift = si->index;
1575
1576                 if (lov->tgts[si->ost_idx].active == 0) {
1577                         CDEBUG(D_HA, "lov idx %d inactive\n", si->ost_idx);
1578                         GOTO(out_oa, rc = -EIO);
1579                 }
1580
1581                 if (si->bufct) {
1582                         LASSERT(shift < oa_bufs);
1583                         if (src_oa)
1584                                 memcpy(tmp_oa, src_oa, sizeof(*tmp_oa));
1585
1586                         tmp_oa->o_id = si->lsm.lsm_object_id;
1587                         rc = obd_brw(cmd, lov->tgts[si->ost_idx].ltd_exp, 
1588                                      tmp_oa, &si->lsm, si->bufct, 
1589                                      &ioarr[shift], oti);
1590                         if (rc)
1591                                 GOTO(out_ioarr, rc);
1592
1593                         lov_merge_attrs(ret_oa, tmp_oa, tmp_oa->o_valid, lsm,
1594                                         i, &set);
1595                 }
1596         }
1597
1598         ret_oa->o_id = src_oa->o_id;
1599         memcpy(src_oa, ret_oa, sizeof(*src_oa));
1600
1601         GOTO(out_oa, rc);
1602  out_oa:
1603         if (tmp_oa)
1604                 obdo_free(tmp_oa);
1605         if (ret_oa)
1606                 obdo_free(ret_oa);
1607  out_ioarr:
1608         OBD_FREE(ioarr, sizeof(*ioarr) * oa_bufs);
1609  out_where:
1610         OBD_FREE(where, sizeof(*where) * oa_bufs);
1611  out_sinfo:
1612         OBD_FREE(stripeinfo, stripe_count * sizeof(*stripeinfo));
1613         return rc;
1614 }
1615
1616 static int lov_brw_interpret(struct ptlrpc_request_set *reqset, void *data,
1617                              int rc)
1618 {
1619         struct lov_brw_async_args *aa = data;
1620         struct lov_stripe_md *lsm = aa->aa_lsm;
1621         obd_count             oa_bufs = aa->aa_oa_bufs;
1622         struct obdo          *oa = aa->aa_oa;
1623         struct obdo          *obdos = aa->aa_obdos;
1624         struct brw_page      *ioarr = aa->aa_ioarr;
1625         struct lov_oinfo     *loi;
1626         int i, set = 0;
1627         ENTRY;
1628
1629         if (rc == 0) {
1630                 /* NB all stripe requests succeeded to get here */
1631
1632                 for (i = 0, loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count;
1633                      i++, loi++) {
1634                         if (obdos[i].o_valid == 0)      /* inactive stripe */
1635                                 continue;
1636
1637                         lov_merge_attrs(oa, &obdos[i], obdos[i].o_valid, lsm,
1638                                         i, &set);
1639                 }
1640
1641                 if (!set) {
1642                         CERROR("No stripes had valid attrs\n");
1643                         rc = -EIO;
1644                 }
1645         }
1646         oa->o_id = lsm->lsm_object_id;
1647
1648         OBD_FREE(obdos, lsm->lsm_stripe_count * sizeof(*obdos));
1649         OBD_FREE(ioarr, sizeof(*ioarr) * oa_bufs);
1650         RETURN(rc);
1651 }
1652
1653 static int lov_brw_async(int cmd, struct obd_export *exp, struct obdo *oa,
1654                          struct lov_stripe_md *lsm, obd_count oa_bufs,
1655                          struct brw_page *pga, struct ptlrpc_request_set *set,
1656                          struct obd_trans_info *oti)
1657 {
1658         struct {
1659                 int bufct;
1660                 int index;
1661                 int subcount;
1662                 struct lov_stripe_md lsm;
1663                 int ost_idx;
1664         } *stripeinfo, *si, *si_last;
1665         struct lov_obd *lov;
1666         struct brw_page *ioarr;
1667         struct obdo *obdos = NULL;
1668         struct lov_oinfo *loi;
1669         struct lov_brw_async_args *aa;
1670         int rc = 0, i, *where, stripe_count = lsm->lsm_stripe_count;
1671         ENTRY;
1672
1673         if (lsm_bad_magic(lsm))
1674                 RETURN(-EINVAL);
1675
1676         lov = &exp->exp_obd->u.lov;
1677
1678         if (cmd == OBD_BRW_CHECK) {
1679                 rc = lov_brw_check(lov, oa, lsm, oa_bufs, pga);
1680                 RETURN(rc);
1681         }
1682
1683         OBD_ALLOC(stripeinfo, stripe_count * sizeof(*stripeinfo));
1684         if (!stripeinfo)
1685                 RETURN(-ENOMEM);
1686
1687         OBD_ALLOC(where, sizeof(*where) * oa_bufs);
1688         if (!where)
1689                 GOTO(out_sinfo, rc = -ENOMEM);
1690
1691         if (oa) {
1692                 OBD_ALLOC(obdos, sizeof(*obdos) * stripe_count);
1693                 if (!obdos)
1694                         GOTO(out_where, rc = -ENOMEM);
1695         }
1696
1697         OBD_ALLOC(ioarr, sizeof(*ioarr) * oa_bufs);
1698         if (!ioarr)
1699                 GOTO(out_obdos, rc = -ENOMEM);
1700
1701         for (i = 0; i < oa_bufs; i++) {
1702                 where[i] = lov_stripe_number(lsm, pga[i].off);
1703                 stripeinfo[where[i]].bufct++;
1704         }
1705
1706         for (i = 0, loi = lsm->lsm_oinfo, si_last = si = stripeinfo;
1707              i < stripe_count; i++, loi++, si_last = si, si++) {
1708                 if (i > 0)
1709                         si->index = si_last->index + si_last->bufct;
1710                 si->lsm.lsm_object_id = loi->loi_id;
1711                 si->ost_idx = loi->loi_ost_idx;
1712
1713                 if (oa) {
1714                         memcpy(&obdos[i], oa, sizeof(*obdos));
1715                         obdos[i].o_id = si->lsm.lsm_object_id;
1716                 }
1717         }
1718
1719         for (i = 0; i < oa_bufs; i++) {
1720                 int which = where[i];
1721                 int shift;
1722
1723                 shift = stripeinfo[which].index + stripeinfo[which].subcount;
1724                 LASSERT(shift < oa_bufs);
1725                 ioarr[shift] = pga[i];
1726                 lov_stripe_offset(lsm, pga[i].off, which, &ioarr[shift].off);
1727                 stripeinfo[which].subcount++;
1728         }
1729
1730         for (i = 0, si = stripeinfo; i < stripe_count; i++, si++) {
1731                 int shift = si->index;
1732
1733                 if (si->bufct == 0)
1734                         continue;
1735
1736                 if (lov->tgts[si->ost_idx].active == 0) {
1737                         CDEBUG(D_HA, "lov idx %d inactive\n", si->ost_idx);
1738                         GOTO(out_ioarr, rc = -EIO);
1739                 }
1740
1741                 LASSERT(shift < oa_bufs);
1742
1743                 rc = obd_brw_async(cmd, lov->tgts[si->ost_idx].ltd_exp,
1744                                    &obdos[i], &si->lsm, si->bufct,
1745                                    &ioarr[shift], set, oti);
1746                 if (rc)
1747                         GOTO(out_ioarr, rc);
1748         }
1749         LASSERT(rc == 0);
1750         LASSERT(set->set_interpret == NULL);
1751         set->set_interpret = (set_interpreter_func)lov_brw_interpret;
1752         LASSERT(sizeof(set->set_args) >= sizeof(struct lov_brw_async_args));
1753         aa = (struct lov_brw_async_args *)&set->set_args;
1754         aa->aa_lsm = lsm;
1755         aa->aa_obdos = obdos;
1756         aa->aa_oa = oa;
1757         aa->aa_ioarr = ioarr;
1758         aa->aa_oa_bufs = oa_bufs;
1759
1760         /* Don't free ioarr or obdos - that's done in lov_brw_interpret */
1761         GOTO(out_where, rc);
1762
1763  out_ioarr:
1764         OBD_FREE(ioarr, sizeof(*ioarr) * oa_bufs);
1765  out_obdos:
1766         OBD_FREE(obdos, stripe_count * sizeof(*obdos));
1767  out_where:
1768         OBD_FREE(where, sizeof(*where) * oa_bufs);
1769  out_sinfo:
1770         OBD_FREE(stripeinfo, stripe_count * sizeof(*stripeinfo));
1771         return rc;
1772 }
1773
1774 struct lov_async_page *lap_from_cookie(void *cookie)
1775 {
1776         struct lov_async_page *lap = cookie;
1777         if (lap->lap_magic != LAP_MAGIC)
1778                 return ERR_PTR(-EINVAL);
1779         return lap;
1780 };
1781
1782 static int lov_ap_make_ready(void *data, int cmd)
1783 {
1784         struct lov_async_page *lap = lap_from_cookie(data);
1785         /* XXX should these assert? */
1786         if (IS_ERR(lap))
1787                 return -EINVAL;
1788
1789         return lap->lap_caller_ops->ap_make_ready(lap->lap_caller_data, cmd);
1790 }
1791 static int lov_ap_refresh_count(void *data, int cmd)
1792 {
1793         struct lov_async_page *lap = lap_from_cookie(data);
1794         if (IS_ERR(lap))
1795                 return -EINVAL;
1796
1797         return lap->lap_caller_ops->ap_refresh_count(lap->lap_caller_data, 
1798                                                      cmd);
1799 }
1800 static void lov_ap_fill_obdo(void *data, int cmd, struct obdo *oa)
1801 {
1802         struct lov_async_page *lap = lap_from_cookie(data);
1803         /* XXX should these assert? */
1804         if (IS_ERR(lap))
1805                 return;
1806
1807         lap->lap_caller_ops->ap_fill_obdo(lap->lap_caller_data, cmd, oa);
1808         /* XXX woah, shouldn't we be altering more here?  size? */
1809         oa->o_id = lap->lap_loi_id;
1810 }
1811
1812 static void lov_ap_completion(void *data, int cmd, struct obdo *oa, int rc)
1813 {
1814         struct lov_async_page *lap = lap_from_cookie(data);
1815         if (IS_ERR(lap))
1816                 return;
1817
1818         /* in a raid1 regime this would down a count of many ios
1819          * in flight, onl calling the caller_ops completion when all
1820          * the raid1 ios are complete */
1821         lap->lap_caller_ops->ap_completion(lap->lap_caller_data, cmd, oa, rc);
1822 }
1823
1824 static struct obd_async_page_ops lov_async_page_ops = {
1825         .ap_make_ready =        lov_ap_make_ready,
1826         .ap_refresh_count =     lov_ap_refresh_count,
1827         .ap_fill_obdo =         lov_ap_fill_obdo,
1828         .ap_completion =        lov_ap_completion,
1829 };
1830
1831 int lov_prep_async_page(struct obd_export *exp, struct lov_stripe_md *lsm,
1832                            struct lov_oinfo *loi, struct page *page,
1833                            obd_off offset, struct obd_async_page_ops *ops, 
1834                            void *data, void **res)
1835 {
1836         struct lov_obd *lov = &exp->exp_obd->u.lov;
1837         struct lov_async_page *lap;
1838         int rc;
1839         ENTRY;
1840
1841         if (lsm_bad_magic(lsm))
1842                 RETURN(-EINVAL);
1843         LASSERT(loi == NULL);
1844
1845         OBD_ALLOC(lap, sizeof(*lap));
1846         if (lap == NULL)
1847                 RETURN(-ENOMEM);
1848
1849         lap->lap_magic = LAP_MAGIC;
1850         lap->lap_caller_ops = ops;
1851         lap->lap_caller_data = data;
1852
1853         /* for now only raid 0 which passes through */
1854         lap->lap_stripe = lov_stripe_number(lsm, offset);
1855         lov_stripe_offset(lsm, offset, lap->lap_stripe, &lap->lap_sub_offset);
1856         loi = &lsm->lsm_oinfo[lap->lap_stripe];
1857
1858         /* so the callback doesn't need the lsm */ 
1859         lap->lap_loi_id = loi->loi_id;
1860
1861         rc = obd_prep_async_page(lov->tgts[loi->loi_ost_idx].ltd_exp,
1862                                  lsm, loi, page, lap->lap_sub_offset,
1863                                  &lov_async_page_ops, lap,
1864                                  &lap->lap_sub_cookie);
1865         if (rc) {
1866                 OBD_FREE(lap, sizeof(*lap));
1867                 RETURN(rc);
1868         }
1869         CDEBUG(D_CACHE, "lap %p page %p cookie %p off "LPU64"\n", lap, page,
1870                lap->lap_sub_cookie, offset);
1871         *res = lap;
1872         RETURN(0);
1873 }
1874
1875 static int lov_queue_async_io(struct obd_export *exp,
1876                               struct lov_stripe_md *lsm,
1877                               struct lov_oinfo *loi, void *cookie,
1878                               int cmd, obd_off off, int count,
1879                               obd_flag brw_flags, obd_flag async_flags)
1880 {
1881         struct lov_obd *lov = &exp->exp_obd->u.lov;
1882         struct lov_async_page *lap;
1883         int rc;
1884
1885         LASSERT(loi == NULL);
1886
1887         if (lsm_bad_magic(lsm))
1888                 RETURN(-EINVAL);
1889
1890         lap = lap_from_cookie(cookie);
1891         if (IS_ERR(lap))
1892                 RETURN(PTR_ERR(lap));
1893
1894         loi = &lsm->lsm_oinfo[lap->lap_stripe];
1895         rc = obd_queue_async_io(lov->tgts[loi->loi_ost_idx].ltd_exp, lsm,
1896                                 loi, lap->lap_sub_cookie, cmd, off, count,
1897                                 brw_flags, async_flags);
1898         RETURN(rc);
1899 }
1900
1901 static int lov_set_async_flags(struct obd_export *exp,
1902                                struct lov_stripe_md *lsm,
1903                                struct lov_oinfo *loi, void *cookie,
1904                                obd_flag async_flags)
1905 {
1906         struct lov_obd *lov = &exp->exp_obd->u.lov;
1907         struct lov_async_page *lap;
1908         int rc;
1909
1910         LASSERT(loi == NULL);
1911
1912         if (lsm_bad_magic(lsm))
1913                 RETURN(-EINVAL);
1914
1915         lap = lap_from_cookie(cookie);
1916         if (IS_ERR(lap))
1917                 RETURN(PTR_ERR(lap));
1918
1919         loi = &lsm->lsm_oinfo[lap->lap_stripe];
1920         rc = obd_set_async_flags(lov->tgts[loi->loi_ost_idx].ltd_exp,
1921                                  lsm, loi, lap->lap_sub_cookie, async_flags);
1922         RETURN(rc);
1923 }
1924
1925 static int lov_queue_group_io(struct obd_export *exp,
1926                               struct lov_stripe_md *lsm,
1927                               struct lov_oinfo *loi,
1928                               struct obd_io_group *oig, void *cookie,
1929                               int cmd, obd_off off, int count,
1930                               obd_flag brw_flags, obd_flag async_flags)
1931 {
1932         struct lov_obd *lov = &exp->exp_obd->u.lov;
1933         struct lov_async_page *lap;
1934         int rc;
1935
1936         LASSERT(loi == NULL);
1937
1938         if (lsm_bad_magic(lsm))
1939                 RETURN(-EINVAL);
1940
1941         lap = lap_from_cookie(cookie);
1942         if (IS_ERR(lap))
1943                 RETURN(PTR_ERR(lap));
1944
1945         loi = &lsm->lsm_oinfo[lap->lap_stripe];
1946         rc = obd_queue_group_io(lov->tgts[loi->loi_ost_idx].ltd_exp, lsm, loi,
1947                                 oig, lap->lap_sub_cookie, cmd, off, count,
1948                                 brw_flags, async_flags);
1949         RETURN(rc);
1950 }
1951
1952 /* this isn't exactly optimal.  we may have queued sync io in oscs on
1953  * all stripes, but we don't record that fact at queue time.  so we
1954  * trigger sync io on all stripes. */
1955 static int lov_trigger_group_io(struct obd_export *exp,
1956                                 struct lov_stripe_md *lsm,
1957                                 struct lov_oinfo *loi,
1958                                 struct obd_io_group *oig)
1959 {
1960         struct lov_obd *lov = &exp->exp_obd->u.lov;
1961         int rc = 0, i, err;
1962
1963         LASSERT(loi == NULL);
1964
1965         if (lsm_bad_magic(lsm))
1966                 RETURN(-EINVAL);
1967
1968         for (i = 0, loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count;
1969              i++, loi++) {
1970                 err = obd_trigger_group_io(lov->tgts[loi->loi_ost_idx].ltd_exp, 
1971                                            lsm, loi, oig);
1972                 if (rc == 0 && err != 0)
1973                         rc = err;
1974         };
1975         RETURN(rc);
1976 }
1977
1978 static int lov_teardown_async_page(struct obd_export *exp,
1979                                    struct lov_stripe_md *lsm,
1980                                    struct lov_oinfo *loi, void *cookie)
1981 {
1982         struct lov_obd *lov = &exp->exp_obd->u.lov;
1983         struct lov_async_page *lap;
1984         int rc;
1985
1986         LASSERT(loi == NULL);
1987
1988         if (lsm_bad_magic(lsm))
1989                 RETURN(-EINVAL);
1990
1991         lap = lap_from_cookie(cookie);
1992         if (IS_ERR(lap))
1993                 RETURN(PTR_ERR(lap));
1994
1995         loi = &lsm->lsm_oinfo[lap->lap_stripe];
1996         rc = obd_teardown_async_page(lov->tgts[loi->loi_ost_idx].ltd_exp, 
1997                                      lsm, loi, lap->lap_sub_cookie);
1998         if (rc) {
1999                 CERROR("unable to teardown sub cookie %p: %d\n", 
2000                        lap->lap_sub_cookie, rc);
2001                 RETURN(rc);
2002         }
2003         OBD_FREE(lap, sizeof(*lap));
2004         RETURN(rc);
2005 }
2006
2007 static int lov_enqueue(struct obd_export *exp, struct lov_stripe_md *lsm,
2008                        __u32 type, ldlm_policy_data_t *policy, __u32 mode,
2009                        int *flags, void *bl_cb, void *cp_cb, void *gl_cb,
2010                        void *data,__u32 lvb_len, void *lvb_swabber,
2011                        struct lustre_handle *lockh)
2012 {
2013         struct lov_lock_handles *lov_lockh = NULL;
2014         struct lustre_handle *lov_lockhp;
2015         struct lov_obd *lov;
2016         struct lov_oinfo *loi;
2017         char submd_buf[sizeof(struct lov_stripe_md) + sizeof(struct lov_oinfo)];
2018         struct lov_stripe_md *submd = (void *)submd_buf;
2019         ldlm_error_t rc;
2020         int i, save_flags = *flags;
2021         ENTRY;
2022
2023         if (lsm_bad_magic(lsm))
2024                 RETURN(-EINVAL);
2025
2026         /* we should never be asked to replay a lock this way. */
2027         LASSERT((*flags & LDLM_FL_REPLAY) == 0);
2028
2029         if (!exp || !exp->exp_obd)
2030                 RETURN(-ENODEV);
2031
2032         if (lsm->lsm_stripe_count > 1) {
2033                 lov_lockh = lov_llh_new(lsm);
2034                 if (lov_lockh == NULL)
2035                         RETURN(-ENOMEM);
2036
2037                 lockh->cookie = lov_lockh->llh_handle.h_cookie;
2038                 lov_lockhp = lov_lockh->llh_handles;
2039         } else {
2040                 lov_lockhp = lockh;
2041         }
2042
2043         lov = &exp->exp_obd->u.lov;
2044         for (i = 0, loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count;
2045              i++, loi++, lov_lockhp++) {
2046                 ldlm_policy_data_t sub_ext;
2047
2048                 if (!lov_stripe_intersects(lsm, i, policy->l_extent.start,
2049                                            policy->l_extent.end,
2050                                            &sub_ext.l_extent.start,
2051                                            &sub_ext.l_extent.end))
2052                         continue;
2053
2054                 sub_ext.l_extent.gid = policy->l_extent.gid;
2055
2056                 if (lov->tgts[loi->loi_ost_idx].active == 0) {
2057                         CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
2058                         continue;
2059                 }
2060
2061                 /* XXX LOV STACKING: submd should be from the subobj */
2062                 submd->lsm_object_id = loi->loi_id;
2063                 submd->lsm_stripe_count = 0;
2064                 submd->lsm_oinfo->loi_kms_valid = loi->loi_kms_valid;
2065                 submd->lsm_oinfo->loi_rss = loi->loi_rss;
2066                 submd->lsm_oinfo->loi_kms = loi->loi_kms;
2067                 submd->lsm_oinfo->loi_blocks = loi->loi_blocks;
2068                 loi->loi_mtime = submd->lsm_oinfo->loi_mtime;
2069                 /* XXX submd is not fully initialized here */
2070                 *flags = save_flags;
2071                 rc = obd_enqueue(lov->tgts[loi->loi_ost_idx].ltd_exp, submd,
2072                                  type, &sub_ext, mode, flags, bl_cb, cp_cb,
2073                                  gl_cb, data, lvb_len, lvb_swabber, lov_lockhp);
2074
2075                 /* XXX FIXME: This unpleasantness doesn't belong here at *all*.
2076                  * It belongs in the OSC, except that the OSC doesn't have
2077                  * access to the real LOI -- it gets a copy, that we created
2078                  * above, and that copy can be arbitrarily out of date.
2079                  *
2080                  * The LOV API is due for a serious rewriting anyways, and this
2081                  * can be addressed then. */
2082                 if (rc == ELDLM_OK) {
2083                         struct ldlm_lock *lock = ldlm_handle2lock(lov_lockhp);
2084                         __u64 tmp = submd->lsm_oinfo->loi_rss;
2085
2086                         LASSERT(lock != NULL);
2087                         loi->loi_rss = tmp;
2088                         loi->loi_blocks = submd->lsm_oinfo->loi_blocks;
2089                         /* Extend KMS up to the end of this lock and no further
2090                          * A lock on [x,y] means a KMS of up to y + 1 bytes! */
2091                         if (tmp > lock->l_policy_data.l_extent.end)
2092                                 tmp = lock->l_policy_data.l_extent.end + 1;
2093                         if (tmp >= loi->loi_kms) {
2094                                 CDEBUG(D_INODE, "lock acquired, setting rss="
2095                                        LPU64", kms="LPU64"\n", loi->loi_rss,
2096                                        tmp);
2097                                 loi->loi_kms = tmp;
2098                                 loi->loi_kms_valid = 1;
2099                         } else {
2100                                 CDEBUG(D_INODE, "lock acquired, setting rss="
2101                                        LPU64"; leaving kms="LPU64", end="LPU64
2102                                        "\n", loi->loi_rss, loi->loi_kms,
2103                                        lock->l_policy_data.l_extent.end);
2104                         }
2105                         ldlm_lock_allow_match(lock);
2106                         LDLM_LOCK_PUT(lock);
2107                 } else if (rc == ELDLM_LOCK_ABORTED &&
2108                            save_flags & LDLM_FL_HAS_INTENT) {
2109                         memset(lov_lockhp, 0, sizeof(*lov_lockhp));
2110                         loi->loi_rss = submd->lsm_oinfo->loi_rss;
2111                         loi->loi_blocks = submd->lsm_oinfo->loi_blocks;
2112                         CDEBUG(D_INODE, "glimpsed, setting rss="LPU64"; leaving"
2113                                " kms="LPU64"\n", loi->loi_rss, loi->loi_kms);
2114                 } else {
2115                         memset(lov_lockhp, 0, sizeof(*lov_lockhp));
2116                         if (lov->tgts[loi->loi_ost_idx].active) {
2117                                 CERROR("error: enqueue objid "LPX64" subobj "
2118                                        LPX64" on OST idx %d: rc = %d\n",
2119                                        lsm->lsm_object_id, loi->loi_id,
2120                                        loi->loi_ost_idx, rc);
2121                                 GOTO(out_locks, rc);
2122                         }
2123                 }
2124         }
2125         if (lsm->lsm_stripe_count > 1)
2126                 lov_llh_put(lov_lockh);
2127         RETURN(ELDLM_OK);
2128
2129  out_locks:
2130         while (loi--, lov_lockhp--, i-- > 0) {
2131                 struct lov_stripe_md submd;
2132                 int err;
2133
2134                 if (lov_lockhp->cookie == 0)
2135                         continue;
2136
2137                 /* XXX LOV STACKING: submd should be from the subobj */
2138                 submd.lsm_object_id = loi->loi_id;
2139                 submd.lsm_stripe_count = 0;
2140                 err = obd_cancel(lov->tgts[loi->loi_ost_idx].ltd_exp, &submd,
2141                                  mode, lov_lockhp);
2142                 if (err && lov->tgts[loi->loi_ost_idx].active) {
2143                         CERROR("error: cancelling objid "LPX64" on OST "
2144                                "idx %d after enqueue error: rc = %d\n",
2145                                loi->loi_id, loi->loi_ost_idx, err);
2146                 }
2147         }
2148
2149         if (lsm->lsm_stripe_count > 1) {
2150                 lov_llh_destroy(lov_lockh);
2151                 lov_llh_put(lov_lockh);
2152         }
2153         return rc;
2154 }
2155
2156 static int lov_match(struct obd_export *exp, struct lov_stripe_md *lsm,
2157                      __u32 type, ldlm_policy_data_t *policy, __u32 mode,
2158                      int *flags, void *data, struct lustre_handle *lockh)
2159 {
2160         struct lov_lock_handles *lov_lockh = NULL;
2161         struct lustre_handle *lov_lockhp;
2162         struct lov_obd *lov;
2163         struct lov_oinfo *loi;
2164         struct lov_stripe_md submd;
2165         ldlm_error_t rc = 0;
2166         int i;
2167         ENTRY;
2168
2169         if (lsm_bad_magic(lsm))
2170                 RETURN(-EINVAL);
2171
2172         if (!exp || !exp->exp_obd)
2173                 RETURN(-ENODEV);
2174
2175         if (lsm->lsm_stripe_count > 1) {
2176                 lov_lockh = lov_llh_new(lsm);
2177                 if (lov_lockh == NULL)
2178                         RETURN(-ENOMEM);
2179
2180                 lockh->cookie = lov_lockh->llh_handle.h_cookie;
2181                 lov_lockhp = lov_lockh->llh_handles;
2182         } else {
2183                 lov_lockhp = lockh;
2184         }
2185
2186         lov = &exp->exp_obd->u.lov;
2187         for (i = 0, loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count;
2188              i++, loi++, lov_lockhp++) {
2189                 ldlm_policy_data_t sub_ext;
2190                 int lov_flags;
2191
2192                 if (!lov_stripe_intersects(lsm, i, policy->l_extent.start,
2193                                            policy->l_extent.end,
2194                                            &sub_ext.l_extent.start,
2195                                            &sub_ext.l_extent.end))
2196                         continue;
2197
2198                 if (lov->tgts[loi->loi_ost_idx].active == 0) {
2199                         CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
2200                         rc = -EIO;
2201                         break;
2202                 }
2203
2204                 /* XXX LOV STACKING: submd should be from the subobj */
2205                 submd.lsm_object_id = loi->loi_id;
2206                 submd.lsm_stripe_count = 0;
2207                 lov_flags = *flags;
2208                 /* XXX submd is not fully initialized here */
2209                 rc = obd_match(lov->tgts[loi->loi_ost_idx].ltd_exp, &submd,
2210                                type, &sub_ext, mode, &lov_flags, data,
2211                                lov_lockhp);
2212                 if (rc != 1)
2213                         break;
2214         }
2215         if (rc == 1) {
2216                 if (lsm->lsm_stripe_count > 1) {
2217                         if (*flags & LDLM_FL_TEST_LOCK)
2218                                 lov_llh_destroy(lov_lockh);
2219                         lov_llh_put(lov_lockh);
2220                 }
2221                 RETURN(1);
2222         }
2223
2224         while (loi--, lov_lockhp--, i-- > 0) {
2225                 struct lov_stripe_md submd;
2226                 int err;
2227
2228                 if (lov_lockhp->cookie == 0)
2229                         continue;
2230
2231                 /* XXX LOV STACKING: submd should be from the subobj */
2232                 submd.lsm_object_id = loi->loi_id;
2233                 submd.lsm_stripe_count = 0;
2234                 err = obd_cancel(lov->tgts[loi->loi_ost_idx].ltd_exp, &submd,
2235                                  mode, lov_lockhp);
2236                 if (err && lov->tgts[loi->loi_ost_idx].active) {
2237                         CERROR("error: cancelling objid "LPX64" on OST "
2238                                "idx %d after match failure: rc = %d\n",
2239                                loi->loi_id, loi->loi_ost_idx, err);
2240                 }
2241         }
2242
2243         if (lsm->lsm_stripe_count > 1) {
2244                 lov_llh_destroy(lov_lockh);
2245                 lov_llh_put(lov_lockh);
2246         }
2247         RETURN(rc);
2248 }
2249
2250 static int lov_change_cbdata(struct obd_export *exp,
2251                              struct lov_stripe_md *lsm, ldlm_iterator_t it,
2252                              void *data)
2253 {
2254         struct lov_obd *lov;
2255         struct lov_oinfo *loi;
2256         int rc = 0, i;
2257         ENTRY;
2258
2259         if (lsm_bad_magic(lsm))
2260                 RETURN(-EINVAL);
2261
2262         if (!exp || !exp->exp_obd)
2263                 RETURN(-ENODEV);
2264
2265         lov = &exp->exp_obd->u.lov;
2266         for (i = 0,loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++,loi++) {
2267                 struct lov_stripe_md submd;
2268                 if (lov->tgts[loi->loi_ost_idx].active == 0)
2269                         CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
2270
2271                 submd.lsm_object_id = loi->loi_id;
2272                 submd.lsm_stripe_count = 0;
2273                 rc = obd_change_cbdata(lov->tgts[loi->loi_ost_idx].ltd_exp,
2274                                        &submd, it, data);
2275         }
2276         RETURN(rc);
2277 }
2278
2279 static int lov_cancel(struct obd_export *exp, struct lov_stripe_md *lsm,
2280                       __u32 mode, struct lustre_handle *lockh)
2281 {
2282         struct lov_lock_handles *lov_lockh = NULL;
2283         struct lustre_handle *lov_lockhp;
2284         struct lov_obd *lov;
2285         struct lov_oinfo *loi;
2286         int rc = 0, i;
2287         ENTRY;
2288
2289         if (lsm_bad_magic(lsm))
2290                 RETURN(-EINVAL);
2291
2292         if (!exp || !exp->exp_obd)
2293                 RETURN(-ENODEV);
2294
2295         LASSERT(lockh);
2296         if (lsm->lsm_stripe_count > 1) {
2297                 lov_lockh = lov_handle2llh(lockh);
2298                 if (!lov_lockh) {
2299                         CERROR("LOV: invalid lov lock handle %p\n", lockh);
2300                         RETURN(-EINVAL);
2301                 }
2302
2303                 lov_lockhp = lov_lockh->llh_handles;
2304         } else {
2305                 lov_lockhp = lockh;
2306         }
2307
2308         lov = &exp->exp_obd->u.lov;
2309         for (i = 0, loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count;
2310              i++, loi++, lov_lockhp++) {
2311                 struct lov_stripe_md submd;
2312                 int err;
2313
2314                 if (lov_lockhp->cookie == 0) {
2315                         CDEBUG(D_HA, "lov idx %d subobj "LPX64" no lock?\n",
2316                                loi->loi_ost_idx, loi->loi_id);
2317                         continue;
2318                 }
2319
2320                 /* XXX LOV STACKING: submd should be from the subobj */
2321                 submd.lsm_object_id = loi->loi_id;
2322                 submd.lsm_stripe_count = 0;
2323                 err = obd_cancel(lov->tgts[loi->loi_ost_idx].ltd_exp, &submd,
2324                                  mode, lov_lockhp);
2325                 if (err) {
2326                         if (lov->tgts[loi->loi_ost_idx].active) {
2327                                 CERROR("error: cancel objid "LPX64" subobj "
2328                                        LPX64" on OST idx %d: rc = %d\n",
2329                                        lsm->lsm_object_id,
2330                                        loi->loi_id, loi->loi_ost_idx, err);
2331                                 if (!rc)
2332                                         rc = err;
2333                         }
2334                 }
2335         }
2336
2337         if (lsm->lsm_stripe_count > 1)
2338                 lov_llh_destroy(lov_lockh);
2339         if (lov_lockh != NULL)
2340                 lov_llh_put(lov_lockh);
2341         RETURN(rc);
2342 }
2343
2344 static int lov_cancel_unused(struct obd_export *exp,
2345                              struct lov_stripe_md *lsm, int flags, void *opaque)
2346 {
2347         struct lov_obd *lov;
2348         struct lov_oinfo *loi;
2349         int rc = 0, i;
2350         ENTRY;
2351
2352         if (lsm_bad_magic(lsm))
2353                 RETURN(-EINVAL);
2354
2355         if (!exp || !exp->exp_obd)
2356                 RETURN(-ENODEV);
2357
2358         lov = &exp->exp_obd->u.lov;
2359         for (i = 0,loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++,loi++) {
2360                 struct lov_stripe_md submd;
2361                 int err;
2362
2363                 if (lov->tgts[loi->loi_ost_idx].active == 0)
2364                         CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
2365
2366                 submd.lsm_object_id = loi->loi_id;
2367                 submd.lsm_stripe_count = 0;
2368                 err = obd_cancel_unused(lov->tgts[loi->loi_ost_idx].ltd_exp,
2369                                         &submd, flags, opaque);
2370                 if (err && lov->tgts[loi->loi_ost_idx].active) {
2371                         CERROR("error: cancel unused objid "LPX64" subobj "LPX64
2372                                " on OST idx %d: rc = %d\n", lsm->lsm_object_id,
2373                                loi->loi_id, loi->loi_ost_idx, err);
2374                         if (!rc)
2375                                 rc = err;
2376                 }
2377         }
2378         RETURN(rc);
2379 }
2380
2381 #define LOV_U64_MAX ((__u64)~0ULL)
2382 #define LOV_SUM_MAX(tot, add)                                           \
2383         do {                                                            \
2384                 if ((tot) + (add) < (tot))                              \
2385                         (tot) = LOV_U64_MAX;                            \
2386                 else                                                    \
2387                         (tot) += (add);                                 \
2388         } while(0)
2389
2390 static int lov_statfs(struct obd_device *obd, struct obd_statfs *osfs,
2391                       unsigned long max_age)
2392 {
2393         struct lov_obd *lov = &obd->u.lov;
2394         struct obd_statfs lov_sfs;
2395         int set = 0;
2396         int rc = 0;
2397         int i;
2398         ENTRY;
2399
2400
2401         /* We only get block data from the OBD */
2402         for (i = 0; i < lov->desc.ld_tgt_count; i++) {
2403                 int err;
2404
2405                 if (!lov->tgts[i].active) {
2406                         CDEBUG(D_HA, "lov idx %d inactive\n", i);
2407                         continue;
2408                 }
2409
2410                 err = obd_statfs(class_exp2obd(lov->tgts[i].ltd_exp), &lov_sfs,
2411                                  max_age);
2412                 if (err) {
2413                         if (lov->tgts[i].active && !rc)
2414                                 rc = err;
2415                         continue;
2416                 }
2417
2418                 if (!set) {
2419                         memcpy(osfs, &lov_sfs, sizeof(lov_sfs));
2420                         set = 1;
2421                 } else {
2422                         osfs->os_bfree += lov_sfs.os_bfree;
2423                         osfs->os_bavail += lov_sfs.os_bavail;
2424                         osfs->os_blocks += lov_sfs.os_blocks;
2425                         /* XXX not sure about this one - depends on policy.
2426                          *   - could be minimum if we always stripe on all OBDs
2427                          *     (but that would be wrong for any other policy,
2428                          *     if one of the OBDs has no more objects left)
2429                          *   - could be sum if we stripe whole objects
2430                          *   - could be average, just to give a nice number
2431                          *
2432                          * To give a "reasonable" (if not wholly accurate)
2433                          * number, we divide the total number of free objects
2434                          * by expected stripe count (watch out for overflow).
2435                          */
2436                         LOV_SUM_MAX(osfs->os_files, lov_sfs.os_files);
2437                         LOV_SUM_MAX(osfs->os_ffree, lov_sfs.os_ffree);
2438                 }
2439         }
2440
2441         if (set) {
2442                 __u32 expected_stripes = lov->desc.ld_default_stripe_count ?
2443                                          lov->desc.ld_default_stripe_count :
2444                                          lov->desc.ld_active_tgt_count;
2445
2446                 if (osfs->os_files != LOV_U64_MAX)
2447                         do_div(osfs->os_files, expected_stripes);
2448                 if (osfs->os_ffree != LOV_U64_MAX)
2449                         do_div(osfs->os_ffree, expected_stripes);
2450         } else if (!rc)
2451                 rc = -EIO;
2452
2453         RETURN(rc);
2454 }
2455
2456 static int lov_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
2457                          void *karg, void *uarg)
2458 {
2459         struct obd_device *obddev = class_exp2obd(exp);
2460         struct lov_obd *lov = &obddev->u.lov;
2461         int i, count = lov->desc.ld_tgt_count;
2462         struct obd_uuid *uuidp;
2463         int rc;
2464
2465         ENTRY;
2466
2467         switch (cmd) {
2468         case OBD_IOC_LOV_GET_CONFIG: {
2469                 struct obd_ioctl_data *data = karg;
2470                 struct lov_tgt_desc *tgtdesc;
2471                 struct lov_desc *desc;
2472                 char *buf = NULL;
2473
2474                 buf = NULL;
2475                 len = 0;
2476                 if (obd_ioctl_getdata(&buf, &len, (void *)uarg))
2477                         RETURN(-EINVAL);
2478
2479                 data = (struct obd_ioctl_data *)buf;
2480
2481                 if (sizeof(*desc) > data->ioc_inllen1) {
2482                         OBD_FREE(buf, len);
2483                         RETURN(-EINVAL);
2484                 }
2485
2486                 if (sizeof(uuidp->uuid) * count > data->ioc_inllen2) {
2487                         OBD_FREE(buf, len);
2488                         RETURN(-EINVAL);
2489                 }
2490
2491                 desc = (struct lov_desc *)data->ioc_inlbuf1;
2492                 memcpy(desc, &(lov->desc), sizeof(*desc));
2493
2494                 uuidp = (struct obd_uuid *)data->ioc_inlbuf2;
2495                 tgtdesc = lov->tgts;
2496                 for (i = 0; i < count; i++, uuidp++, tgtdesc++)
2497                         obd_str2uuid(uuidp, tgtdesc->uuid.uuid);
2498
2499                 rc = copy_to_user((void *)uarg, buf, len);
2500                 if (rc)
2501                         rc = -EFAULT;
2502                 obd_ioctl_freedata(buf, len);
2503                 break;
2504         }
2505         case LL_IOC_LOV_SETSTRIPE:
2506                 rc = lov_setstripe(exp, karg, uarg);
2507                 break;
2508         case LL_IOC_LOV_GETSTRIPE:
2509                 rc = lov_getstripe(exp, karg, uarg);
2510                 break;
2511         case LL_IOC_LOV_SETEA:
2512                 rc = lov_setea(exp, karg, uarg);
2513                 break;
2514         default: {
2515                 int set = 0;
2516                 if (count == 0)
2517                         RETURN(-ENOTTY);
2518                 rc = 0;
2519                 for (i = 0; i < count; i++) {
2520                         int err;
2521
2522                         err = obd_iocontrol(cmd, lov->tgts[i].ltd_exp,
2523                                             len, karg, uarg);
2524                         if (err) {
2525                                 if (lov->tgts[i].active) {
2526                                         CERROR("error: iocontrol OSC %s on OST"
2527                                                "idx %d: err = %d\n",
2528                                                lov->tgts[i].uuid.uuid, i, err);
2529                                         if (!rc)
2530                                                 rc = err;
2531                                 }
2532                         } else
2533                                 set = 1;
2534                 }
2535                 if (!set && !rc)
2536                         rc = -EIO;
2537         }
2538         }
2539
2540         RETURN(rc);
2541 }
2542
2543 static int lov_get_info(struct obd_export *exp, __u32 keylen,
2544                         void *key, __u32 *vallen, void *val)
2545 {
2546         struct obd_device *obddev = class_exp2obd(exp);
2547         struct lov_obd *lov = &obddev->u.lov;
2548         int i;
2549         ENTRY;
2550
2551         if (!vallen || !val)
2552                 RETURN(-EFAULT);
2553
2554         if (keylen > strlen("lock_to_stripe") &&
2555             strcmp(key, "lock_to_stripe") == 0) {
2556                 struct {
2557                         char name[16];
2558                         struct ldlm_lock *lock;
2559                         struct lov_stripe_md *lsm;
2560                 } *data = key;
2561                 struct lov_oinfo *loi;
2562                 __u32 *stripe = val;
2563
2564                 if (*vallen < sizeof(*stripe))
2565                         RETURN(-EFAULT);
2566                 *vallen = sizeof(*stripe);
2567
2568                 /* XXX This is another one of those bits that will need to
2569                  * change if we ever actually support nested LOVs.  It uses
2570                  * the lock's export to find out which stripe it is. */
2571                 for (i = 0, loi = data->lsm->lsm_oinfo;
2572                      i < data->lsm->lsm_stripe_count;
2573                      i++, loi++) {
2574                         if (lov->tgts[loi->loi_ost_idx].ltd_exp == 
2575                             data->lock->l_conn_export) {
2576                                 *stripe = i;
2577                                 RETURN(0);
2578                         }
2579                 }
2580                 RETURN(-ENXIO);
2581         } else if (keylen >= strlen("size_to_stripe") &&
2582                    strcmp(key, "size_to_stripe") == 0) {
2583                 struct {
2584                         int stripe_number;
2585                         __u64 size;
2586                         struct lov_stripe_md *lsm;
2587                 } *data = val;
2588
2589                 if (*vallen < sizeof(*data))
2590                         RETURN(-EFAULT);
2591
2592                 data->size = lov_size_to_stripe(data->lsm, data->size,
2593                                                 data->stripe_number);
2594                 RETURN(0);
2595         } else if (keylen >= strlen("last_id") && strcmp(key, "last_id") == 0) {
2596                 obd_id *ids = val;
2597                 int rc, size = sizeof(obd_id);
2598                 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
2599                         if (!lov->tgts[i].active)
2600                                 continue;
2601                         rc = obd_get_info(lov->tgts[i].ltd_exp, keylen, key,
2602                                           &size, &(ids[i]));
2603                         if (rc != 0)
2604                                 RETURN(rc);
2605                 }
2606                 RETURN(0);
2607         } else if (keylen >= strlen("lovdesc") && strcmp(key, "lovdesc") == 0) {
2608                 struct lov_desc *desc_ret = val;
2609                 *desc_ret = lov->desc;
2610                 
2611                 RETURN(0);
2612         }
2613
2614         RETURN(-EINVAL);
2615 }
2616
2617 static int lov_set_info(struct obd_export *exp, obd_count keylen,
2618                         void *key, obd_count vallen, void *val)
2619 {
2620         struct obd_device *obddev = class_exp2obd(exp);
2621         struct lov_obd *lov = &obddev->u.lov;
2622         int i, rc = 0;
2623         ENTRY;
2624
2625 #define KEY_IS(str) \
2626         (keylen == strlen(str) && memcmp(key, str, keylen) == 0)
2627
2628         if (KEY_IS("next_id")) {
2629                 if (vallen != lov->desc.ld_tgt_count)
2630                         RETURN(-EINVAL);
2631                 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
2632                         int er;
2633
2634                         /* initialize all OSCs, even inactive ones */
2635
2636                         er = obd_set_info(lov->tgts[i].ltd_exp, keylen, key,
2637                                           sizeof(obd_id), ((obd_id*)val) + i);
2638                         if (!rc)
2639                                 rc = er;
2640                 }
2641                 RETURN(rc);
2642         }
2643
2644         if (KEY_IS("growth_count")) {
2645                 if (vallen != sizeof(int))
2646                         RETURN(-EINVAL);
2647         } else if (KEY_IS("mds_conn") || KEY_IS("unlinked")) {
2648                 if (vallen != 0)
2649                         RETURN(-EINVAL);
2650         } else {
2651                 RETURN(-EINVAL);
2652         }
2653
2654         for (i = 0; i < lov->desc.ld_tgt_count; i++) {
2655                 int er;
2656
2657                 if (val && !obd_uuid_equals(val, &lov->tgts[i].uuid)) 
2658                         continue;
2659
2660                 if (!val && !lov->tgts[i].active)
2661                         continue;
2662
2663                 er = obd_set_info(lov->tgts[i].ltd_exp, keylen, key, vallen,
2664                                    val);
2665                 if (!rc)
2666                         rc = er;
2667         }
2668         RETURN(rc);
2669 #undef KEY_IS
2670
2671 }
2672
2673 /* Merge rss if kms == 0
2674  *
2675  * Even when merging RSS, we will take the KMS value if it's larger.
2676  * This prevents getattr from stomping on dirty cached pages which
2677  * extend the file size. */
2678 __u64 lov_merge_size(struct lov_stripe_md *lsm, int kms)
2679 {
2680         struct lov_oinfo *loi;
2681         __u64 size = 0;
2682         int i;
2683
2684         for (i = 0, loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count;
2685              i++, loi++) {
2686                 obd_size lov_size, tmpsize;
2687
2688                 tmpsize = loi->loi_kms;
2689                 if (kms == 0 && loi->loi_rss > tmpsize)
2690                         tmpsize = loi->loi_rss;
2691
2692                 lov_size = lov_stripe_size(lsm, tmpsize, i);
2693                 if (lov_size > size)
2694                         size = lov_size;
2695         }
2696         return size;
2697 }
2698 EXPORT_SYMBOL(lov_merge_size);
2699
2700 /* Merge blocks */
2701 __u64 lov_merge_blocks(struct lov_stripe_md *lsm)
2702 {
2703         struct lov_oinfo *loi;
2704         __u64 blocks = 0;
2705         int i;
2706
2707         for (i = 0, loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count;
2708              i++, loi++) {
2709                 blocks += loi->loi_blocks;
2710         }
2711         return blocks;
2712 }
2713 EXPORT_SYMBOL(lov_merge_blocks);
2714
2715 __u64 lov_merge_mtime(struct lov_stripe_md *lsm, __u64 current_time)
2716 {
2717         struct lov_oinfo *loi;
2718         int i;
2719
2720         for (i = 0, loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count;
2721              i++, loi++) {
2722                 if (loi->loi_mtime > current_time)
2723                         current_time = loi->loi_mtime;
2724         }
2725         return current_time;
2726 }
2727 EXPORT_SYMBOL(lov_merge_mtime);
2728
2729 #if 0
2730 struct lov_multi_wait {
2731         struct ldlm_lock *lock;
2732         wait_queue_t      wait;
2733         int               completed;
2734         int               generation;
2735 };
2736
2737 int lov_complete_many(struct obd_export *exp, struct lov_stripe_md *lsm,
2738                       struct lustre_handle *lockh)
2739 {
2740         struct lov_lock_handles *lov_lockh = NULL;
2741         struct lustre_handle *lov_lockhp;
2742         struct lov_obd *lov;
2743         struct lov_oinfo *loi;
2744         struct lov_multi_wait *queues;
2745         int rc = 0, i;
2746         ENTRY;
2747
2748         if (lsm_bad_magic(lsm))
2749                 RETURN(-EINVAL);
2750
2751         if (!exp || !exp->exp_obd)
2752                 RETURN(-ENODEV);
2753
2754         LASSERT(lockh != NULL);
2755         if (lsm->lsm_stripe_count > 1) {
2756                 lov_lockh = lov_handle2llh(lockh);
2757                 if (lov_lockh == NULL) {
2758                         CERROR("LOV: invalid lov lock handle %p\n", lockh);
2759                         RETURN(-EINVAL);
2760                 }
2761
2762                 lov_lockhp = lov_lockh->llh_handles;
2763         } else {
2764                 lov_lockhp = lockh;
2765         }
2766
2767         OBD_ALLOC(queues, lsm->lsm_stripe_count * sizeof(*queues));
2768         if (queues == NULL)
2769                 GOTO(out, rc = -ENOMEM);
2770
2771         lov = &exp->exp_obd->u.lov;
2772         for (i = 0, loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count;
2773              i++, loi++, lov_lockhp++) {
2774                 struct ldlm_lock *lock;
2775                 struct obd_device *obd;
2776                 unsigned long irqflags;
2777
2778                 lock = ldlm_handle2lock(lov_lockhp);
2779                 if (lock == NULL) {
2780                         CDEBUG(D_HA, "lov idx %d subobj "LPX64" no lock?\n",
2781                                loi->loi_ost_idx, loi->loi_id);
2782                         queues[i].completed = 1;
2783                         continue;
2784                 }
2785
2786                 queues[i].lock = lock;
2787                 init_waitqueue_entry(&(queues[i].wait), current);
2788                 add_wait_queue(lock->l_waitq, &(queues[i].wait));
2789
2790                 obd = class_exp2obd(lock->l_conn_export);
2791                 if (obd != NULL)
2792                         imp = obd->u.cli.cl_import;
2793                 if (imp != NULL) {
2794                         spin_lock_irqsave(&imp->imp_lock, irqflags);
2795                         queues[i].generation = imp->imp_generation;
2796                         spin_unlock_irqrestore(&imp->imp_lock, irqflags);
2797                 }
2798         }
2799
2800         lwi = LWI_TIMEOUT_INTR(obd_timeout * HZ, ldlm_expired_completion_wait,
2801                                interrupted_completion_wait, &lwd);
2802         rc = l_wait_event_added(check_multi_complete(queues, lsm), &lwi);
2803
2804         for (i = 0; i < lsm->lsm_stripe_count; i++)
2805                 remove_wait_queue(lock->l_waitq, &(queues[i].wait));
2806
2807         if (rc == -EINTR || rc == -ETIMEDOUT) {
2808
2809
2810         }
2811
2812  out:
2813         if (lov_lockh != NULL)
2814                 lov_llh_put(lov_lockh);
2815         RETURN(rc);
2816 }
2817 #endif
2818
2819 void lov_increase_kms(struct obd_export *exp, struct lov_stripe_md *lsm,
2820                       obd_off size)
2821 {
2822         struct lov_oinfo *loi;
2823         int stripe = 0;
2824         __u64 kms;
2825         ENTRY;
2826
2827         if (size > 0)
2828                 stripe = lov_stripe_number(lsm, size - 1);
2829         kms = lov_size_to_stripe(lsm, size, stripe);
2830         loi = &(lsm->lsm_oinfo[stripe]);
2831
2832         CDEBUG(D_INODE, "stripe %d KMS %sincreasing "LPU64"->"LPU64"\n",
2833                stripe, kms > loi->loi_kms ? "" : "not ", loi->loi_kms, kms);
2834         if (kms > loi->loi_kms)
2835                 loi->loi_kms = kms;
2836         EXIT;
2837 }
2838 EXPORT_SYMBOL(lov_increase_kms);
2839
2840 struct obd_ops lov_obd_ops = {
2841         o_owner:       THIS_MODULE,
2842         o_attach:      lov_attach,
2843         o_detach:      lov_detach,
2844         o_setup:       lov_setup,
2845         o_cleanup:     lov_cleanup,
2846         o_connect:     lov_connect,
2847         o_disconnect:  lov_disconnect,
2848         o_statfs:      lov_statfs,
2849         o_packmd:      lov_packmd,
2850         o_unpackmd:    lov_unpackmd,
2851         o_create:      lov_create,
2852         o_destroy:     lov_destroy,
2853         o_getattr:     lov_getattr,
2854         o_getattr_async: lov_getattr_async,
2855         o_setattr:     lov_setattr,
2856         o_brw:         lov_brw,
2857         o_brw_async:   lov_brw_async,
2858         .o_prep_async_page =    lov_prep_async_page,
2859         .o_queue_async_io =     lov_queue_async_io,
2860         .o_set_async_flags =    lov_set_async_flags,
2861         .o_queue_group_io =     lov_queue_group_io,
2862         .o_trigger_group_io =   lov_trigger_group_io,
2863         .o_teardown_async_page  lov_teardown_async_page,
2864         o_punch:       lov_punch,
2865         o_sync:        lov_sync,
2866         o_enqueue:     lov_enqueue,
2867         o_match:       lov_match,
2868         o_change_cbdata: lov_change_cbdata,
2869         o_cancel:      lov_cancel,
2870         o_cancel_unused: lov_cancel_unused,
2871         o_iocontrol:   lov_iocontrol,
2872         o_get_info:    lov_get_info,
2873         o_set_info:    lov_set_info,
2874         o_llog_init:   lov_llog_init,
2875         o_llog_finish: lov_llog_finish,
2876         o_notify: lov_notify,
2877 };
2878
2879 int __init lov_init(void)
2880 {
2881         struct lprocfs_static_vars lvars;
2882         int rc;
2883
2884         lprocfs_init_vars(lov, &lvars);
2885         rc = class_register_type(&lov_obd_ops, lvars.module_vars,
2886                                  OBD_LOV_DEVICENAME);
2887         RETURN(rc);
2888 }
2889
2890 #ifdef __KERNEL__
2891 static void /*__exit*/ lov_exit(void)
2892 {
2893         class_unregister_type(OBD_LOV_DEVICENAME);
2894 }
2895
2896 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
2897 MODULE_DESCRIPTION("Lustre Logical Object Volume OBD driver");
2898 MODULE_LICENSE("GPL");
2899
2900 module_init(lov_init);
2901 module_exit(lov_exit);
2902 #endif