Whamcloud - gitweb
Set loi_kms_valid even if we get a lock back that has rss == kms. This
[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                         CERROR("error in orphan recovery on OST idx %d/%d: "
576                                "rc = %d\n", i, lov->desc.ld_tgt_count, err);
577                         if (!rc)
578                                 rc = err;
579                 }
580
581                 if (ost_uuid)
582                         break;
583         }
584         obdo_free(tmp_oa);
585         RETURN(rc);
586 }
587
588 #define LOV_CREATE_RESEED_INTERVAL 1000
589
590 /* the LOV expects oa->o_id to be set to the LOV object id */
591 static int lov_create(struct obd_export *exp, struct obdo *src_oa,
592                       struct lov_stripe_md **ea, struct obd_trans_info *oti)
593 {
594         static int ost_start_idx, ost_start_count;
595         struct lov_obd *lov;
596         struct lov_stripe_md *lsm;
597         struct lov_oinfo *loi = NULL;
598         struct obdo *tmp_oa, *ret_oa;
599         struct llog_cookie *cookies = NULL;
600         unsigned ost_count, ost_idx;
601         int set = 0, obj_alloc = 0, cookie_sent = 0, rc = 0, i;
602         ENTRY;
603
604         LASSERT(ea != NULL);
605
606         if ((src_oa->o_valid & OBD_MD_FLFLAGS) &&
607             src_oa->o_flags == OBD_FL_DELORPHAN) {
608                 rc = lov_clear_orphans(exp, src_oa, ea, oti);
609                 RETURN(rc);
610         }
611
612         if (exp == NULL)
613                 RETURN(-EINVAL);
614
615         lov = &exp->exp_obd->u.lov;
616
617         if (!lov->desc.ld_active_tgt_count)
618                 RETURN(-EIO);
619
620         /* Recreate a specific object id at the given OST index */
621         if (src_oa->o_valid & OBD_MD_FLFLAGS && src_oa->o_flags &
622                                                 OBD_FL_RECREATE_OBJS) {
623                  struct lov_stripe_md obj_md;
624                  struct lov_stripe_md *obj_mdp = &obj_md;
625
626                  ost_idx = src_oa->o_nlink;
627                  lsm = *ea;
628                  if (lsm == NULL)
629                         RETURN(-EINVAL);
630                  if (ost_idx >= lov->desc.ld_tgt_count)
631                          RETURN(-EINVAL);
632                  for (i = 0; i < lsm->lsm_stripe_count; i++) {
633                          if (lsm->lsm_oinfo[i].loi_ost_idx == ost_idx) {
634                                  if (lsm->lsm_oinfo[i].loi_id != src_oa->o_id)
635                                          RETURN(-EINVAL);
636                                  break;
637                          }
638                  }
639                  if (i == lsm->lsm_stripe_count)
640                          RETURN(-EINVAL);
641
642                  rc = obd_create(lov->tgts[ost_idx].ltd_exp, src_oa,
643                                  &obj_mdp, oti);
644                  RETURN(rc);
645         }
646
647         ret_oa = obdo_alloc();
648         if (!ret_oa)
649                 RETURN(-ENOMEM);
650
651         tmp_oa = obdo_alloc();
652         if (!tmp_oa)
653                 GOTO(out_oa, rc = -ENOMEM);
654
655         lsm = *ea;
656         if (lsm == NULL) {
657                 int stripes;
658                 ost_count = lov_get_stripecnt(lov, 0);
659
660                 /* If the MDS file was truncated up to some size, stripe over
661                  * enough OSTs to allow the file to be created at that size. */
662                 if (src_oa->o_valid & OBD_MD_FLSIZE) {
663                         stripes=((src_oa->o_size+LUSTRE_STRIPE_MAXBYTES)>>12)-1;
664                         do_div(stripes, (__u32)(LUSTRE_STRIPE_MAXBYTES >> 12));
665
666                         if (stripes > lov->desc.ld_active_tgt_count)
667                                 RETURN(-EFBIG);
668                         if (stripes > ost_count)
669                                 stripes = ost_count;
670                 } else {
671                         stripes = ost_count;
672                 }
673
674                 rc = lov_alloc_memmd(&lsm, stripes, lov->desc.ld_pattern ?
675                                      lov->desc.ld_pattern : LOV_PATTERN_RAID0);
676                 if (rc < 0)
677                         GOTO(out_tmp, rc);
678
679                 rc = 0;
680         }
681
682         ost_count = lov->desc.ld_tgt_count;
683
684         LASSERT(src_oa->o_valid & OBD_MD_FLID);
685         lsm->lsm_object_id = src_oa->o_id;
686         if (!lsm->lsm_stripe_size)
687                 lsm->lsm_stripe_size = lov->desc.ld_default_stripe_size;
688         if (!lsm->lsm_pattern) {
689                 lsm->lsm_pattern = lov->desc.ld_pattern ?
690                         lov->desc.ld_pattern : LOV_PATTERN_RAID0;
691         }
692
693         if (*ea == NULL || lsm->lsm_oinfo[0].loi_ost_idx >= ost_count) {
694                 if (--ost_start_count <= 0) {
695                         ost_start_idx = ll_insecure_random_int();
696                         ost_start_count = LOV_CREATE_RESEED_INTERVAL;
697                 } else if (lsm->lsm_stripe_count >=
698                            lov->desc.ld_active_tgt_count) {
699                         /* If we allocate from all of the stripes, make the
700                          * next file start on the next OST. */
701                         ++ost_start_idx;
702                 }
703                 ost_idx = ost_start_idx % ost_count;
704         } else {
705                 ost_idx = lsm->lsm_oinfo[0].loi_ost_idx;
706         }
707
708         CDEBUG(D_INODE, "allocating %d subobjs for objid "LPX64" at idx %d\n",
709                lsm->lsm_stripe_count, lsm->lsm_object_id, ost_idx);
710
711         /* XXX LOV STACKING: need to figure out how many real OSCs */
712         if (oti && (src_oa->o_valid & OBD_MD_FLCOOKIE)) {
713                 oti_alloc_cookies(oti, lsm->lsm_stripe_count);
714                 if (!oti->oti_logcookies)
715                         GOTO(out_cleanup, rc = -ENOMEM);
716                 cookies = oti->oti_logcookies;
717         }
718
719         loi = lsm->lsm_oinfo;
720         for (i = 0; i < ost_count; i++, ost_idx = (ost_idx + 1) % ost_count) {
721                 struct lov_stripe_md obj_md;
722                 struct lov_stripe_md *obj_mdp = &obj_md;
723                 int err;
724
725                 ++ost_start_idx;
726                 if (lov->tgts[ost_idx].active == 0) {
727                         CDEBUG(D_HA, "lov idx %d inactive\n", ost_idx);
728                         continue;
729                 }
730
731                 /* create data objects with "parent" OA */
732                 memcpy(tmp_oa, src_oa, sizeof(*tmp_oa));
733
734                 /* XXX When we start creating objects on demand, we need to
735                  *     make sure that we always create the object on the
736                  *     stripe which holds the existing file size.
737                  */
738                 if (src_oa->o_valid & OBD_MD_FLSIZE) {
739                         if (lov_stripe_offset(lsm, src_oa->o_size, i,
740                                               &tmp_oa->o_size) < 0 &&
741                             tmp_oa->o_size)
742                                 tmp_oa->o_size--;
743
744                         CDEBUG(D_INODE, "stripe %d has size "LPU64"/"LPU64"\n",
745                                i, tmp_oa->o_size, src_oa->o_size);
746                 }
747
748
749                 /* XXX: LOV STACKING: use real "obj_mdp" sub-data */
750                 err = obd_create(lov->tgts[ost_idx].ltd_exp, tmp_oa, &obj_mdp,
751                                  oti);
752                 if (err) {
753                         if (lov->tgts[ost_idx].active) {
754                                 CERROR("error creating objid "LPX64" sub-object"
755                                        " on OST idx %d/%d: rc = %d\n",
756                                        src_oa->o_id, ost_idx,
757                                        lsm->lsm_stripe_count, err);
758                                 if (err > 0) {
759                                         CERROR("obd_create returned invalid "
760                                                "err %d\n", err);
761                                         err = -EIO;
762                                 }
763                         }
764                         if (!rc)
765                                 rc = err;
766                         continue;
767                 }
768                 if (oti->oti_objid)
769                         oti->oti_objid[ost_idx] = tmp_oa->o_id;
770                 loi->loi_id = tmp_oa->o_id;
771                 loi->loi_ost_idx = ost_idx;
772                 CDEBUG(D_INODE, "objid "LPX64" has subobj "LPX64" at idx %d\n",
773                        lsm->lsm_object_id, loi->loi_id, ost_idx);
774
775                 lov_merge_attrs(ret_oa, tmp_oa, tmp_oa->o_valid, lsm,
776                                 obj_alloc, &set);
777                 loi_init(loi);
778
779                 if (cookies)
780                         ++oti->oti_logcookies;
781                 if (tmp_oa->o_valid & OBD_MD_FLCOOKIE)
782                         ++cookie_sent;
783                 ++obj_alloc;
784                 ++loi;
785
786                 /* If we have allocated enough objects, we are OK */
787                 if (obj_alloc == lsm->lsm_stripe_count)
788                         GOTO(out_done, rc = 0);
789         }
790
791         if (obj_alloc == 0) {
792                 if (rc == 0)
793                         rc = -EIO;
794                 GOTO(out_cleanup, rc);
795         }
796
797         /* If we were passed specific striping params, then a failure to
798          * meet those requirements is an error, since we can't reallocate
799          * that memory (it might be part of a larger array or something).
800          *
801          * We can only get here if lsm_stripe_count was originally > 1.
802          */
803         if (*ea != NULL) {
804                 CERROR("can't lstripe objid "LPX64": have %u want %u, rc %d\n",
805                        lsm->lsm_object_id, obj_alloc, lsm->lsm_stripe_count,rc);
806                 if (rc == 0)
807                         rc = -EFBIG;
808                 GOTO(out_cleanup, rc);
809         } else {
810                 struct lov_stripe_md *lsm_new;
811                 /* XXX LOV STACKING call into osc for sizes */
812                 unsigned oldsize, newsize;
813
814                 if (oti && cookies && cookie_sent) {
815                         oldsize = lsm->lsm_stripe_count * sizeof(*cookies);
816                         newsize = obj_alloc * sizeof(*cookies);
817
818                         oti_alloc_cookies(oti, obj_alloc);
819                         if (oti->oti_logcookies) {
820                                 memcpy(oti->oti_logcookies, cookies, newsize);
821                                 OBD_FREE(cookies, oldsize);
822                                 cookies = oti->oti_logcookies;
823                         } else {
824                                 CWARN("'leaking' %d bytes\n", oldsize-newsize);
825                         }
826                 }
827
828                 CWARN("using fewer stripes for object "LPX64": old %u new %u\n",
829                       lsm->lsm_object_id, lsm->lsm_stripe_count, obj_alloc);
830                 oldsize = lov_stripe_md_size(lsm->lsm_stripe_count);
831                 newsize = lov_stripe_md_size(obj_alloc);
832                 OBD_ALLOC(lsm_new, newsize);
833                 if (lsm_new != NULL) {
834                         memcpy(lsm_new, lsm, newsize);
835                         lsm_new->lsm_stripe_count = obj_alloc;
836                         OBD_FREE(lsm, newsize);
837                         lsm = lsm_new;
838                 } else {
839                         CWARN("'leaking' %d bytes\n", oldsize - newsize);
840                 }
841                 rc = 0;
842         }
843         EXIT;
844  out_done:
845         *ea = lsm;
846         if (src_oa->o_valid & OBD_MD_FLSIZE &&
847             ret_oa->o_size != src_oa->o_size) {
848                 CERROR("original size "LPU64" isn't new object size "LPU64"\n",
849                        src_oa->o_size, ret_oa->o_size);
850                 LBUG();
851         }
852         ret_oa->o_id = src_oa->o_id;
853         memcpy(src_oa, ret_oa, sizeof(*src_oa));
854
855  out_tmp:
856         obdo_free(tmp_oa);
857  out_oa:
858         obdo_free(ret_oa);
859         if (oti && cookies) {
860                 oti->oti_logcookies = cookies;
861                 if (!cookie_sent) {
862                         oti_free_cookies(oti);
863                         src_oa->o_valid &= ~OBD_MD_FLCOOKIE;
864                 } else {
865                         src_oa->o_valid |= OBD_MD_FLCOOKIE;
866                 }
867         }
868         RETURN(rc);
869
870  out_cleanup:
871         while (obj_alloc-- > 0) {
872                 struct obd_export *sub_exp;
873                 int err;
874
875                 --loi;
876                 sub_exp = lov->tgts[loi->loi_ost_idx].ltd_exp;
877                 /* destroy already created objects here */
878                 memcpy(tmp_oa, src_oa, sizeof(*tmp_oa));
879                 tmp_oa->o_id = loi->loi_id;
880
881                 err = obd_destroy(sub_exp, tmp_oa, NULL, oti);
882                 if (err)
883                         CERROR("Failed to uncreate objid "LPX64" subobj "LPX64
884                                " on OST idx %d: rc = %d\n", src_oa->o_id,
885                                loi->loi_id, loi->loi_ost_idx, err);
886         }
887         if (*ea == NULL)
888                 obd_free_memmd(exp, &lsm);
889         goto out_tmp;
890 }
891
892 #define lsm_bad_magic(LSMP)                                     \
893 ({                                                              \
894         struct lov_stripe_md *_lsm__ = (LSMP);                  \
895         int _ret__ = 0;                                         \
896         if (!_lsm__) {                                          \
897                 CERROR("LOV requires striping ea\n");           \
898                 _ret__ = 1;                                     \
899         } else if (_lsm__->lsm_magic != LOV_MAGIC) {            \
900                 CERROR("LOV striping magic bad %#x != %#x\n",   \
901                        _lsm__->lsm_magic, LOV_MAGIC);           \
902                 _ret__ = 1;                                     \
903         }                                                       \
904         _ret__;                                                 \
905 })
906
907 static int lov_destroy(struct obd_export *exp, struct obdo *oa,
908                        struct lov_stripe_md *lsm, struct obd_trans_info *oti)
909 {
910         struct obdo tmp;
911         struct lov_obd *lov;
912         struct lov_oinfo *loi;
913         int rc = 0, i;
914         ENTRY;
915
916         if (lsm_bad_magic(lsm))
917                 RETURN(-EINVAL);
918
919         if (!exp || !exp->exp_obd)
920                 RETURN(-ENODEV);
921
922         lov = &exp->exp_obd->u.lov;
923         for (i = 0,loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++,loi++) {
924                 int err;
925                 if (lov->tgts[loi->loi_ost_idx].active == 0) {
926                         CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
927                         /* Orphan clean up will (someday) fix this up. */
928                         continue;
929                 }
930
931                 memcpy(&tmp, oa, sizeof(tmp));
932                 tmp.o_id = loi->loi_id;
933                 err = obd_destroy(lov->tgts[loi->loi_ost_idx].ltd_exp, &tmp,
934                                   NULL, oti);
935                 if (err && lov->tgts[loi->loi_ost_idx].active) {
936                         CERROR("error: destroying objid "LPX64" subobj "
937                                LPX64" on OST idx %d: rc = %d\n",
938                                oa->o_id, loi->loi_id, loi->loi_ost_idx, err);
939                         if (!rc)
940                                 rc = err;
941                 }
942         }
943         RETURN(rc);
944 }
945
946 static int lov_getattr(struct obd_export *exp, struct obdo *oa,
947                        struct lov_stripe_md *lsm)
948 {
949         struct obdo tmp;
950         struct lov_obd *lov;
951         struct lov_oinfo *loi;
952         int i, rc = 0, set = 0;
953         ENTRY;
954
955         if (lsm_bad_magic(lsm))
956                 RETURN(-EINVAL);
957
958         if (!exp || !exp->exp_obd)
959                 RETURN(-ENODEV);
960
961         lov = &exp->exp_obd->u.lov;
962
963         CDEBUG(D_INFO, "objid "LPX64": %ux%u byte stripes\n",
964                lsm->lsm_object_id, lsm->lsm_stripe_count, lsm->lsm_stripe_size);
965         for (i = 0,loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++,loi++) {
966                 int err;
967
968                 if (lov->tgts[loi->loi_ost_idx].active == 0) {
969                         CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
970                         continue;
971                 }
972
973                 CDEBUG(D_INFO, "objid "LPX64"[%d] has subobj "LPX64" at idx "
974                        "%u\n", oa->o_id, i, loi->loi_id, loi->loi_ost_idx);
975                 /* create data objects with "parent" OA */
976                 memcpy(&tmp, oa, sizeof(tmp));
977                 tmp.o_id = loi->loi_id;
978
979                 err = obd_getattr(lov->tgts[loi->loi_ost_idx].ltd_exp, &tmp,
980                                   NULL);
981                 if (err) {
982                         if (lov->tgts[loi->loi_ost_idx].active) {
983                                 CERROR("error: getattr objid "LPX64" subobj "
984                                        LPX64" on OST idx %d: rc = %d\n",
985                                        oa->o_id, loi->loi_id, loi->loi_ost_idx,
986                                        err);
987                                 RETURN(err);
988                         }
989                 } else {
990                         lov_merge_attrs(oa, &tmp, tmp.o_valid, lsm, i, &set);
991                 }
992         }
993         if (!set)
994                 rc = -EIO;
995         RETURN(rc);
996 }
997
998 static int lov_getattr_interpret(struct ptlrpc_request_set *rqset, void *data, 
999                                  int rc)
1000 {
1001         struct lov_getattr_async_args *aa = data;
1002         struct lov_stripe_md *lsm = aa->aa_lsm;
1003         struct obdo          *oa = aa->aa_oa;
1004         struct obdo          *obdos = aa->aa_obdos;
1005         struct lov_oinfo     *loi;
1006         int                   i;
1007         int                   set = 0;
1008         ENTRY;
1009
1010         if (rc == 0) {
1011                 /* NB all stripe requests succeeded to get here */
1012
1013                 for (i = 0, loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count;
1014                      i++, loi++) {
1015                         if (obdos[i].o_valid == 0)      /* inactive stripe */
1016                                 continue;
1017
1018                         lov_merge_attrs(oa, &obdos[i], obdos[i].o_valid, lsm,
1019                                         i, &set);
1020                 }
1021
1022                 if (!set) {
1023                         CERROR ("No stripes had valid attrs\n");
1024                         rc = -EIO;
1025                 }
1026         }
1027
1028         OBD_FREE (obdos, lsm->lsm_stripe_count * sizeof (*obdos));
1029         RETURN (rc);
1030 }
1031
1032 static int lov_getattr_async(struct obd_export *exp, struct obdo *oa,
1033                               struct lov_stripe_md *lsm,
1034                               struct ptlrpc_request_set *rqset)
1035 {
1036         struct obdo *obdos;
1037         struct lov_obd *lov;
1038         struct lov_oinfo *loi;
1039         struct lov_getattr_async_args *aa;
1040         int i, rc = 0, set = 0;
1041         ENTRY;
1042
1043         if (lsm_bad_magic(lsm))
1044                 RETURN(-EINVAL);
1045
1046         if (!exp || !exp->exp_obd)
1047                 RETURN(-ENODEV);
1048
1049         lov = &exp->exp_obd->u.lov;
1050
1051         OBD_ALLOC (obdos, lsm->lsm_stripe_count * sizeof (*obdos));
1052         if (obdos == NULL)
1053                 RETURN(-ENOMEM);
1054
1055         CDEBUG(D_INFO, "objid "LPX64": %ux%u byte stripes\n",
1056                lsm->lsm_object_id, lsm->lsm_stripe_count, lsm->lsm_stripe_size);
1057         for (i = 0,loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++,loi++) {
1058                 int err;
1059
1060                 if (lov->tgts[loi->loi_ost_idx].active == 0) {
1061                         CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
1062                         /* leaves obdos[i].obd_valid unset */
1063                         continue;
1064                 }
1065
1066                 CDEBUG(D_INFO, "objid "LPX64"[%d] has subobj "LPX64" at idx "
1067                        "%u\n", oa->o_id, i, loi->loi_id, loi->loi_ost_idx);
1068                 /* create data objects with "parent" OA */
1069                 memcpy(&obdos[i], oa, sizeof(obdos[i]));
1070                 obdos[i].o_id = loi->loi_id;
1071
1072                 err = obd_getattr_async(lov->tgts[loi->loi_ost_idx].ltd_exp,
1073                                          &obdos[i], NULL, rqset);
1074                 if (err) {
1075                         CERROR("error: getattr objid "LPX64" subobj "
1076                                LPX64" on OST idx %d: rc = %d\n",
1077                                oa->o_id, loi->loi_id, loi->loi_ost_idx,
1078                                err);
1079                         GOTO(out_obdos, rc = err);
1080                 }
1081                 set = 1;
1082         }
1083         if (!set)
1084                 GOTO (out_obdos, rc = -EIO);
1085
1086         LASSERT (rqset->set_interpret == NULL);
1087         rqset->set_interpret = lov_getattr_interpret;
1088         LASSERT (sizeof (rqset->set_args) >= sizeof (*aa));
1089         aa = (struct lov_getattr_async_args *)&rqset->set_args;
1090         aa->aa_lsm = lsm;
1091         aa->aa_oa = oa;
1092         aa->aa_obdos = obdos;
1093         aa->aa_lov = lov;
1094         GOTO(out, rc = 0);
1095
1096 out_obdos:
1097         OBD_FREE (obdos, lsm->lsm_stripe_count * sizeof (*obdos));
1098 out:
1099         RETURN(rc);
1100 }
1101
1102
1103 static int lov_setattr(struct obd_export *exp, struct obdo *src_oa,
1104                        struct lov_stripe_md *lsm, struct obd_trans_info *oti)
1105 {
1106         struct obdo *tmp_oa, *ret_oa;
1107         struct lov_obd *lov;
1108         struct lov_oinfo *loi;
1109         int rc = 0, i, set = 0;
1110         ENTRY;
1111
1112         if (lsm_bad_magic(lsm))
1113                 RETURN(-EINVAL);
1114
1115         if (!exp || !exp->exp_obd)
1116                 RETURN(-ENODEV);
1117
1118         /* for now, we only expect time updates here */
1119         LASSERT(!(src_oa->o_valid & ~(OBD_MD_FLID|OBD_MD_FLTYPE | OBD_MD_FLMODE|
1120                                       OBD_MD_FLATIME | OBD_MD_FLMTIME |
1121                                       OBD_MD_FLCTIME | OBD_MD_FLFLAGS |
1122                                       OBD_MD_FLSIZE)));
1123         ret_oa = obdo_alloc();
1124         if (!ret_oa)
1125                 RETURN(-ENOMEM);
1126
1127         tmp_oa = obdo_alloc();
1128         if (!tmp_oa)
1129                 GOTO(out_oa, rc = -ENOMEM);
1130
1131         lov = &exp->exp_obd->u.lov;
1132         for (i = 0,loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++,loi++) {
1133                 int err;
1134
1135                 if (lov->tgts[loi->loi_ost_idx].active == 0) {
1136                         CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
1137                         continue;
1138                 }
1139
1140                 memcpy(tmp_oa, src_oa, sizeof(*tmp_oa));
1141                 tmp_oa->o_id = loi->loi_id;
1142
1143                 if (src_oa->o_valid & OBD_MD_FLSIZE) {
1144                         if (lov_stripe_offset(lsm, src_oa->o_size, i,
1145                                               &tmp_oa->o_size) < 0 &&
1146                             tmp_oa->o_size)
1147                                 tmp_oa->o_size--;
1148
1149                         CDEBUG(D_INODE, "stripe %d has size "LPU64"/"LPU64"\n",
1150                                i, tmp_oa->o_size, src_oa->o_size);
1151                 }
1152
1153                 err = obd_setattr(lov->tgts[loi->loi_ost_idx].ltd_exp, tmp_oa,
1154                                   NULL, NULL);
1155                 if (err) {
1156                         if (lov->tgts[loi->loi_ost_idx].active) {
1157                                 CERROR("error: setattr objid "LPX64" subobj "
1158                                        LPX64" on OST idx %d: rc = %d\n",
1159                                        src_oa->o_id, loi->loi_id,
1160                                        loi->loi_ost_idx, err);
1161                                 if (!rc)
1162                                         rc = err;
1163                         }
1164                         continue;
1165                 }
1166                 lov_merge_attrs(ret_oa, tmp_oa, tmp_oa->o_valid, lsm, i, &set);
1167         }
1168         if (!set && !rc)
1169                 rc = -EIO;
1170
1171         ret_oa->o_id = src_oa->o_id;
1172         memcpy(src_oa, ret_oa, sizeof(*src_oa));
1173         GOTO(out_tmp, rc);
1174 out_tmp:
1175         obdo_free(tmp_oa);
1176 out_oa:
1177         obdo_free(ret_oa);
1178         return rc;
1179 }
1180
1181 /* we have an offset in file backed by an lov and want to find out where
1182  * that offset lands in our given stripe of the file.  for the easy
1183  * case where the offset is within the stripe, we just have to scale the
1184  * offset down to make it relative to the stripe instead of the lov.
1185  *
1186  * the harder case is what to do when the offset doesn't intersect the
1187  * stripe.  callers will want start offsets clamped ahead to the start
1188  * of the nearest stripe in the file.  end offsets similarly clamped to the
1189  * nearest ending byte of a stripe in the file:
1190  *
1191  * all this function does is move offsets to the nearest region of the
1192  * stripe, and it does its work "mod" the full length of all the stripes.
1193  * consider a file with 3 stripes:
1194  *
1195  *             S                                              E
1196  * ---------------------------------------------------------------------
1197  * |    0    |     1     |     2     |    0    |     1     |     2     |
1198  * ---------------------------------------------------------------------
1199  *
1200  * to find stripe 1's offsets for S and E, it divides by the full stripe
1201  * width and does its math in the context of a single set of stripes:
1202  *
1203  *             S         E
1204  * -----------------------------------
1205  * |    0    |     1     |     2     |
1206  * -----------------------------------
1207  *
1208  * it'll notice that E is outside stripe 1 and clamp it to the end of the
1209  * stripe, then multiply it back out by lov_off to give the real offsets in
1210  * the stripe:
1211  *
1212  *   S                   E
1213  * ---------------------------------------------------------------------
1214  * |    1    |     1     |     1     |    1    |     1     |     1     |
1215  * ---------------------------------------------------------------------
1216  *
1217  * it would have done similarly and pulled S forward to the start of a 1
1218  * stripe if, say, S had landed in a 0 stripe.
1219  *
1220  * this rounding isn't always correct.  consider an E lov offset that lands
1221  * on a 0 stripe, the "mod stripe width" math will pull it forward to the
1222  * start of a 1 stripe, when in fact it wanted to be rounded back to the end
1223  * of a previous 1 stripe.  this logic is handled by callers and this is why:
1224  *
1225  * this function returns < 0 when the offset was "before" the stripe and
1226  * was moved forward to the start of the stripe in question;  0 when it
1227  * falls in the stripe and no shifting was done; > 0 when the offset
1228  * was outside the stripe and was pulled back to its final byte. */
1229 static int lov_stripe_offset(struct lov_stripe_md *lsm, obd_off lov_off,
1230                              int stripeno, obd_off *obd_off)
1231 {
1232         unsigned long ssize  = lsm->lsm_stripe_size;
1233         unsigned long swidth = ssize * lsm->lsm_stripe_count;
1234         unsigned long stripe_off, this_stripe;
1235         int ret = 0;
1236
1237         if (lov_off == OBD_OBJECT_EOF) {
1238                 *obd_off = OBD_OBJECT_EOF;
1239                 return 0;
1240         }
1241
1242         /* do_div(a, b) returns a % b, and a = a / b */
1243         stripe_off = do_div(lov_off, swidth);
1244
1245         this_stripe = stripeno * ssize;
1246         if (stripe_off < this_stripe) {
1247                 stripe_off = 0;
1248                 ret = -1;
1249         } else {
1250                 stripe_off -= this_stripe;
1251
1252                 if (stripe_off >= ssize) {
1253                         stripe_off = ssize;
1254                         ret = 1;
1255                 }
1256         }
1257
1258         *obd_off = lov_off * ssize + stripe_off;
1259         return ret;
1260 }
1261
1262 /* Given a whole-file size and a stripe number, give the file size which
1263  * corresponds to the individual object of that stripe.
1264  *
1265  * This behaves basically in the same was as lov_stripe_offset, except that
1266  * file sizes falling before the beginning of a stripe are clamped to the end
1267  * of the previous stripe, not the beginning of the next:
1268  *
1269  *                                               S
1270  * ---------------------------------------------------------------------
1271  * |    0    |     1     |     2     |    0    |     1     |     2     |
1272  * ---------------------------------------------------------------------
1273  *
1274  * if clamped to stripe 2 becomes:
1275  *
1276  *                                   S
1277  * ---------------------------------------------------------------------
1278  * |    0    |     1     |     2     |    0    |     1     |     2     |
1279  * ---------------------------------------------------------------------
1280  */
1281 static obd_off lov_size_to_stripe(struct lov_stripe_md *lsm, obd_off file_size,
1282                                   int stripeno)
1283 {
1284         unsigned long ssize  = lsm->lsm_stripe_size;
1285         unsigned long swidth = ssize * lsm->lsm_stripe_count;
1286         unsigned long stripe_off, this_stripe;
1287
1288         if (file_size == OBD_OBJECT_EOF)
1289                 return OBD_OBJECT_EOF;
1290
1291         /* do_div(a, b) returns a % b, and a = a / b */
1292         stripe_off = do_div(file_size, swidth);
1293
1294         this_stripe = stripeno * ssize;
1295         if (stripe_off < this_stripe) {
1296                 /* Move to end of previous stripe, or zero */
1297                 if (file_size > 0) {
1298                         file_size--;
1299                         stripe_off = ssize;
1300                 } else {
1301                         stripe_off = 0;
1302                 }
1303         } else {
1304                 stripe_off -= this_stripe;
1305
1306                 if (stripe_off >= ssize) {
1307                         /* Clamp to end of this stripe */
1308                         stripe_off = ssize;
1309                 }
1310         }
1311
1312         return (file_size * ssize + stripe_off);
1313 }
1314
1315 /* given an extent in an lov and a stripe, calculate the extent of the stripe
1316  * that is contained within the lov extent.  this returns true if the given
1317  * stripe does intersect with the lov extent. */
1318 static int lov_stripe_intersects(struct lov_stripe_md *lsm, int stripeno,
1319                                  obd_off start, obd_off end,
1320                                  obd_off *obd_start, obd_off *obd_end)
1321 {
1322         int start_side, end_side;
1323
1324         start_side = lov_stripe_offset(lsm, start, stripeno, obd_start);
1325         end_side = lov_stripe_offset(lsm, end, stripeno, obd_end);
1326
1327         CDEBUG(D_INODE, "["LPU64"->"LPU64"] -> [(%d) "LPU64"->"LPU64" (%d)]\n",
1328                start, end, start_side, *obd_start, *obd_end, end_side);
1329
1330         /* this stripe doesn't intersect the file extent when neither
1331          * start or the end intersected the stripe and obd_start and
1332          * obd_end got rounded up to the save value. */
1333         if (start_side != 0 && end_side != 0 && *obd_start == *obd_end)
1334                 return 0;
1335
1336         /* as mentioned in the lov_stripe_offset commentary, end
1337          * might have been shifted in the wrong direction.  This
1338          * happens when an end offset is before the stripe when viewed
1339          * through the "mod stripe size" math. we detect it being shifted
1340          * in the wrong direction and touch it up.
1341          * interestingly, this can't underflow since end must be > start
1342          * if we passed through the previous check.
1343          * (should we assert for that somewhere?) */
1344         if (end_side != 0)
1345                 (*obd_end)--;
1346
1347         return 1;
1348 }
1349
1350 /* compute which stripe number "lov_off" will be written into */
1351 static int lov_stripe_number(struct lov_stripe_md *lsm, obd_off lov_off)
1352 {
1353         unsigned long ssize  = lsm->lsm_stripe_size;
1354         unsigned long swidth = ssize * lsm->lsm_stripe_count;
1355         unsigned long stripe_off;
1356
1357         stripe_off = do_div(lov_off, swidth);
1358
1359         return stripe_off / ssize;
1360 }
1361
1362 /* FIXME: maybe we'll just make one node the authoritative attribute node, then
1363  * we can send this 'punch' to just the authoritative node and the nodes
1364  * that the punch will affect. */
1365 static int lov_punch(struct obd_export *exp, struct obdo *oa,
1366                      struct lov_stripe_md *lsm,
1367                      obd_off start, obd_off end, struct obd_trans_info *oti)
1368 {
1369         struct obdo tmp;
1370         struct lov_obd *lov;
1371         struct lov_oinfo *loi;
1372         int rc = 0, i;
1373         ENTRY;
1374
1375         if (lsm_bad_magic(lsm))
1376                 RETURN(-EINVAL);
1377
1378         if (!exp || !exp->exp_obd)
1379                 RETURN(-ENODEV);
1380
1381         lov = &exp->exp_obd->u.lov;
1382         for (i = 0,loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++,loi++) {
1383                 obd_off starti, endi;
1384                 int err;
1385
1386                 if (lov->tgts[loi->loi_ost_idx].active == 0) {
1387                         CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
1388                         continue;
1389                 }
1390
1391                 if (!lov_stripe_intersects(lsm, i, start, end, &starti, &endi))
1392                         continue;
1393
1394                 /* create data objects with "parent" OA */
1395                 memcpy(&tmp, oa, sizeof(tmp));
1396                 tmp.o_id = loi->loi_id;
1397
1398                 err = obd_punch(lov->tgts[loi->loi_ost_idx].ltd_exp, &tmp, NULL,
1399                                 starti, endi, NULL);
1400                 if (err) {
1401                         if (lov->tgts[loi->loi_ost_idx].active) {
1402                                 CERROR("error: punch objid "LPX64" subobj "LPX64
1403                                        " on OST idx %d: rc = %d\n", oa->o_id,
1404                                        loi->loi_id, loi->loi_ost_idx, err);
1405                         }
1406                         if (!rc)
1407                                 rc = err;
1408                 } else {
1409                         loi->loi_kms = loi->loi_rss = starti;
1410                 }
1411         }
1412         RETURN(rc);
1413 }
1414
1415 static int lov_sync(struct obd_export *exp, struct obdo *oa,
1416                     struct lov_stripe_md *lsm, obd_off start, obd_off end)
1417 {
1418         struct obdo *tmp;
1419         struct lov_obd *lov;
1420         struct lov_oinfo *loi;
1421         int rc = 0, i;
1422         ENTRY;
1423
1424         if (lsm_bad_magic(lsm))
1425                 RETURN(-EINVAL);
1426
1427         if (!exp->exp_obd)
1428                 RETURN(-ENODEV);
1429
1430         tmp = obdo_alloc();
1431         if (!tmp)
1432                 RETURN(-ENOMEM);
1433
1434         lov = &exp->exp_obd->u.lov;
1435         for (i = 0,loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++,loi++) {
1436                 obd_off starti, endi;
1437                 int err;
1438
1439                 if (lov->tgts[loi->loi_ost_idx].active == 0) {
1440                         CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
1441                         continue;
1442                 }
1443
1444                 if (!lov_stripe_intersects(lsm, i, start, end, &starti, &endi))
1445                         continue;
1446
1447                 memcpy(tmp, oa, sizeof(*tmp));
1448                 tmp->o_id = loi->loi_id;
1449
1450                 err = obd_sync(lov->tgts[loi->loi_ost_idx].ltd_exp, tmp, NULL,
1451                                starti, endi);
1452                 if (err) {
1453                         if (lov->tgts[loi->loi_ost_idx].active) {
1454                                 CERROR("error: fsync objid "LPX64" subobj "LPX64
1455                                        " on OST idx %d: rc = %d\n", oa->o_id,
1456                                        loi->loi_id, loi->loi_ost_idx, err);
1457                         }
1458                         if (!rc)
1459                                 rc = err;
1460                 }
1461         }
1462
1463         obdo_free(tmp);
1464         RETURN(rc);
1465 }
1466
1467 static int lov_brw_check(struct lov_obd *lov, struct obdo *oa,
1468                          struct lov_stripe_md *lsm,
1469                          obd_count oa_bufs, struct brw_page *pga)
1470 {
1471         int i, rc = 0;
1472
1473         /* The caller just wants to know if there's a chance that this
1474          * I/O can succeed */
1475         for (i = 0; i < oa_bufs; i++) {
1476                 int stripe = lov_stripe_number(lsm, pga[i].off);
1477                 int ost = lsm->lsm_oinfo[stripe].loi_ost_idx;
1478                 struct ldlm_extent ext, subext;
1479                 ext.start = pga[i].off;
1480                 ext.end = pga[i].off + pga[i].count;
1481
1482                 if (!lov_stripe_intersects(lsm, i, ext.start, ext.end,
1483                                            &subext.start, &subext.end))
1484                         continue;
1485
1486                 if (lov->tgts[ost].active == 0) {
1487                         CDEBUG(D_HA, "lov idx %d inactive\n", ost);
1488                         return -EIO;
1489                 }
1490                 rc = obd_brw(OBD_BRW_CHECK, lov->tgts[stripe].ltd_exp, oa,
1491                              NULL, 1, &pga[i], NULL);
1492                 if (rc)
1493                         break;
1494         }
1495         return rc;
1496 }
1497
1498 static int lov_brw(int cmd, struct obd_export *exp, struct obdo *src_oa,
1499                    struct lov_stripe_md *lsm, obd_count oa_bufs,
1500                    struct brw_page *pga, struct obd_trans_info *oti)
1501 {
1502         struct {
1503                 int bufct;
1504                 int index;
1505                 int subcount;
1506                 struct lov_stripe_md lsm;
1507                 int ost_idx;
1508         } *stripeinfo, *si, *si_last;
1509         struct obdo *ret_oa = NULL, *tmp_oa = NULL;
1510         struct lov_obd *lov;
1511         struct brw_page *ioarr;
1512         struct lov_oinfo *loi;
1513         int rc = 0, i, *where, stripe_count = lsm->lsm_stripe_count, set = 0;
1514         ENTRY;
1515
1516         if (lsm_bad_magic(lsm))
1517                 RETURN(-EINVAL);
1518
1519         lov = &exp->exp_obd->u.lov;
1520
1521         if (cmd == OBD_BRW_CHECK) {
1522                 rc = lov_brw_check(lov, src_oa, lsm, oa_bufs, pga);
1523                 RETURN(rc);
1524         }
1525
1526         OBD_ALLOC(stripeinfo, stripe_count * sizeof(*stripeinfo));
1527         if (!stripeinfo)
1528                 RETURN(-ENOMEM);
1529
1530         OBD_ALLOC(where, sizeof(*where) * oa_bufs);
1531         if (!where)
1532                 GOTO(out_sinfo, rc = -ENOMEM);
1533
1534         OBD_ALLOC(ioarr, sizeof(*ioarr) * oa_bufs);
1535         if (!ioarr)
1536                 GOTO(out_where, rc = -ENOMEM);
1537
1538         if (src_oa) {
1539                 ret_oa = obdo_alloc();
1540                 if (!ret_oa)
1541                         GOTO(out_ioarr, rc = -ENOMEM);
1542
1543                 tmp_oa = obdo_alloc();
1544                 if (!tmp_oa)
1545                         GOTO(out_oa, rc = -ENOMEM);
1546         }
1547
1548         for (i = 0; i < oa_bufs; i++) {
1549                 where[i] = lov_stripe_number(lsm, pga[i].off);
1550                 stripeinfo[where[i]].bufct++;
1551         }
1552
1553         for (i = 0, loi = lsm->lsm_oinfo, si_last = si = stripeinfo;
1554              i < stripe_count; i++, loi++, si_last = si, si++) {
1555                 if (i > 0)
1556                         si->index = si_last->index + si_last->bufct;
1557                 si->lsm.lsm_object_id = loi->loi_id;
1558                 si->ost_idx = loi->loi_ost_idx;
1559         }
1560
1561         for (i = 0; i < oa_bufs; i++) {
1562                 int which = where[i];
1563                 int shift;
1564
1565                 shift = stripeinfo[which].index + stripeinfo[which].subcount;
1566                 LASSERT(shift < oa_bufs);
1567                 ioarr[shift] = pga[i];
1568                 lov_stripe_offset(lsm, pga[i].off, which, &ioarr[shift].off);
1569                 stripeinfo[which].subcount++;
1570         }
1571
1572         for (i = 0, si = stripeinfo; i < stripe_count; i++, si++) {
1573                 int shift = si->index;
1574
1575                 if (lov->tgts[si->ost_idx].active == 0) {
1576                         CDEBUG(D_HA, "lov idx %d inactive\n", si->ost_idx);
1577                         GOTO(out_oa, rc = -EIO);
1578                 }
1579
1580                 if (si->bufct) {
1581                         LASSERT(shift < oa_bufs);
1582                         if (src_oa)
1583                                 memcpy(tmp_oa, src_oa, sizeof(*tmp_oa));
1584
1585                         tmp_oa->o_id = si->lsm.lsm_object_id;
1586                         rc = obd_brw(cmd, lov->tgts[si->ost_idx].ltd_exp, 
1587                                      tmp_oa, &si->lsm, si->bufct, 
1588                                      &ioarr[shift], oti);
1589                         if (rc)
1590                                 GOTO(out_ioarr, rc);
1591
1592                         lov_merge_attrs(ret_oa, tmp_oa, tmp_oa->o_valid, lsm,
1593                                         i, &set);
1594                 }
1595         }
1596
1597         ret_oa->o_id = src_oa->o_id;
1598         memcpy(src_oa, ret_oa, sizeof(*src_oa));
1599
1600         GOTO(out_oa, rc);
1601  out_oa:
1602         if (tmp_oa)
1603                 obdo_free(tmp_oa);
1604         if (ret_oa)
1605                 obdo_free(ret_oa);
1606  out_ioarr:
1607         OBD_FREE(ioarr, sizeof(*ioarr) * oa_bufs);
1608  out_where:
1609         OBD_FREE(where, sizeof(*where) * oa_bufs);
1610  out_sinfo:
1611         OBD_FREE(stripeinfo, stripe_count * sizeof(*stripeinfo));
1612         return rc;
1613 }
1614
1615 static int lov_brw_interpret(struct ptlrpc_request_set *reqset, void *data,
1616                              int rc)
1617 {
1618         struct lov_brw_async_args *aa = data;
1619         struct lov_stripe_md *lsm = aa->aa_lsm;
1620         obd_count             oa_bufs = aa->aa_oa_bufs;
1621         struct obdo          *oa = aa->aa_oa;
1622         struct obdo          *obdos = aa->aa_obdos;
1623         struct brw_page      *ioarr = aa->aa_ioarr;
1624         struct lov_oinfo     *loi;
1625         int i, set = 0;
1626         ENTRY;
1627
1628         if (rc == 0) {
1629                 /* NB all stripe requests succeeded to get here */
1630
1631                 for (i = 0, loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count;
1632                      i++, loi++) {
1633                         if (obdos[i].o_valid == 0)      /* inactive stripe */
1634                                 continue;
1635
1636                         lov_merge_attrs(oa, &obdos[i], obdos[i].o_valid, lsm,
1637                                         i, &set);
1638                 }
1639
1640                 if (!set) {
1641                         CERROR("No stripes had valid attrs\n");
1642                         rc = -EIO;
1643                 }
1644         }
1645         oa->o_id = lsm->lsm_object_id;
1646
1647         OBD_FREE(obdos, lsm->lsm_stripe_count * sizeof(*obdos));
1648         OBD_FREE(ioarr, sizeof(*ioarr) * oa_bufs);
1649         RETURN(rc);
1650 }
1651
1652 static int lov_brw_async(int cmd, struct obd_export *exp, struct obdo *oa,
1653                          struct lov_stripe_md *lsm, obd_count oa_bufs,
1654                          struct brw_page *pga, struct ptlrpc_request_set *set,
1655                          struct obd_trans_info *oti)
1656 {
1657         struct {
1658                 int bufct;
1659                 int index;
1660                 int subcount;
1661                 struct lov_stripe_md lsm;
1662                 int ost_idx;
1663         } *stripeinfo, *si, *si_last;
1664         struct lov_obd *lov;
1665         struct brw_page *ioarr;
1666         struct obdo *obdos = NULL;
1667         struct lov_oinfo *loi;
1668         struct lov_brw_async_args *aa;
1669         int rc = 0, i, *where, stripe_count = lsm->lsm_stripe_count;
1670         ENTRY;
1671
1672         if (lsm_bad_magic(lsm))
1673                 RETURN(-EINVAL);
1674
1675         lov = &exp->exp_obd->u.lov;
1676
1677         if (cmd == OBD_BRW_CHECK) {
1678                 rc = lov_brw_check(lov, oa, lsm, oa_bufs, pga);
1679                 RETURN(rc);
1680         }
1681
1682         OBD_ALLOC(stripeinfo, stripe_count * sizeof(*stripeinfo));
1683         if (!stripeinfo)
1684                 RETURN(-ENOMEM);
1685
1686         OBD_ALLOC(where, sizeof(*where) * oa_bufs);
1687         if (!where)
1688                 GOTO(out_sinfo, rc = -ENOMEM);
1689
1690         if (oa) {
1691                 OBD_ALLOC(obdos, sizeof(*obdos) * stripe_count);
1692                 if (!obdos)
1693                         GOTO(out_where, rc = -ENOMEM);
1694         }
1695
1696         OBD_ALLOC(ioarr, sizeof(*ioarr) * oa_bufs);
1697         if (!ioarr)
1698                 GOTO(out_obdos, rc = -ENOMEM);
1699
1700         for (i = 0; i < oa_bufs; i++) {
1701                 where[i] = lov_stripe_number(lsm, pga[i].off);
1702                 stripeinfo[where[i]].bufct++;
1703         }
1704
1705         for (i = 0, loi = lsm->lsm_oinfo, si_last = si = stripeinfo;
1706              i < stripe_count; i++, loi++, si_last = si, si++) {
1707                 if (i > 0)
1708                         si->index = si_last->index + si_last->bufct;
1709                 si->lsm.lsm_object_id = loi->loi_id;
1710                 si->ost_idx = loi->loi_ost_idx;
1711
1712                 if (oa) {
1713                         memcpy(&obdos[i], oa, sizeof(*obdos));
1714                         obdos[i].o_id = si->lsm.lsm_object_id;
1715                 }
1716         }
1717
1718         for (i = 0; i < oa_bufs; i++) {
1719                 int which = where[i];
1720                 int shift;
1721
1722                 shift = stripeinfo[which].index + stripeinfo[which].subcount;
1723                 LASSERT(shift < oa_bufs);
1724                 ioarr[shift] = pga[i];
1725                 lov_stripe_offset(lsm, pga[i].off, which, &ioarr[shift].off);
1726                 stripeinfo[which].subcount++;
1727         }
1728
1729         for (i = 0, si = stripeinfo; i < stripe_count; i++, si++) {
1730                 int shift = si->index;
1731
1732                 if (si->bufct == 0)
1733                         continue;
1734
1735                 if (lov->tgts[si->ost_idx].active == 0) {
1736                         CDEBUG(D_HA, "lov idx %d inactive\n", si->ost_idx);
1737                         GOTO(out_ioarr, rc = -EIO);
1738                 }
1739
1740                 LASSERT(shift < oa_bufs);
1741
1742                 rc = obd_brw_async(cmd, lov->tgts[si->ost_idx].ltd_exp,
1743                                    &obdos[i], &si->lsm, si->bufct,
1744                                    &ioarr[shift], set, oti);
1745                 if (rc)
1746                         GOTO(out_ioarr, rc);
1747         }
1748         LASSERT(rc == 0);
1749         LASSERT(set->set_interpret == NULL);
1750         set->set_interpret = (set_interpreter_func)lov_brw_interpret;
1751         LASSERT(sizeof(set->set_args) >= sizeof(struct lov_brw_async_args));
1752         aa = (struct lov_brw_async_args *)&set->set_args;
1753         aa->aa_lsm = lsm;
1754         aa->aa_obdos = obdos;
1755         aa->aa_oa = oa;
1756         aa->aa_ioarr = ioarr;
1757         aa->aa_oa_bufs = oa_bufs;
1758
1759         /* Don't free ioarr or obdos - that's done in lov_brw_interpret */
1760         GOTO(out_where, rc);
1761
1762  out_ioarr:
1763         OBD_FREE(ioarr, sizeof(*ioarr) * oa_bufs);
1764  out_obdos:
1765         OBD_FREE(obdos, stripe_count * sizeof(*obdos));
1766  out_where:
1767         OBD_FREE(where, sizeof(*where) * oa_bufs);
1768  out_sinfo:
1769         OBD_FREE(stripeinfo, stripe_count * sizeof(*stripeinfo));
1770         return rc;
1771 }
1772
1773 struct lov_async_page *lap_from_cookie(void *cookie)
1774 {
1775         struct lov_async_page *lap = cookie;
1776         if (lap->lap_magic != LAP_MAGIC)
1777                 return ERR_PTR(-EINVAL);
1778         return lap;
1779 };
1780
1781 static int lov_ap_make_ready(void *data, int cmd)
1782 {
1783         struct lov_async_page *lap = lap_from_cookie(data);
1784         /* XXX should these assert? */
1785         if (IS_ERR(lap))
1786                 return -EINVAL;
1787
1788         return lap->lap_caller_ops->ap_make_ready(lap->lap_caller_data, cmd);
1789 }
1790 static int lov_ap_refresh_count(void *data, int cmd)
1791 {
1792         struct lov_async_page *lap = lap_from_cookie(data);
1793         if (IS_ERR(lap))
1794                 return -EINVAL;
1795
1796         return lap->lap_caller_ops->ap_refresh_count(lap->lap_caller_data, 
1797                                                      cmd);
1798 }
1799 static void lov_ap_fill_obdo(void *data, int cmd, struct obdo *oa)
1800 {
1801         struct lov_async_page *lap = lap_from_cookie(data);
1802         /* XXX should these assert? */
1803         if (IS_ERR(lap))
1804                 return;
1805
1806         lap->lap_caller_ops->ap_fill_obdo(lap->lap_caller_data, cmd, oa);
1807         /* XXX woah, shouldn't we be altering more here?  size? */
1808         oa->o_id = lap->lap_loi_id;
1809 }
1810 static void lov_ap_completion(void *data, int cmd, int rc)
1811 {
1812         struct lov_async_page *lap = lap_from_cookie(data);
1813         if (IS_ERR(lap))
1814                 return;
1815
1816         /* in a raid1 regime this would down a count of many ios
1817          * in flight, onl calling the caller_ops completion when all
1818          * the raid1 ios are complete */
1819         lap->lap_caller_ops->ap_completion(lap->lap_caller_data, cmd, rc);
1820 }
1821
1822 static struct obd_async_page_ops lov_async_page_ops = {
1823         .ap_make_ready =        lov_ap_make_ready,
1824         .ap_refresh_count =     lov_ap_refresh_count,
1825         .ap_fill_obdo =         lov_ap_fill_obdo,
1826         .ap_completion =        lov_ap_completion,
1827 };
1828
1829 int lov_prep_async_page(struct obd_export *exp, struct lov_stripe_md *lsm,
1830                            struct lov_oinfo *loi, struct page *page,
1831                            obd_off offset, struct obd_async_page_ops *ops, 
1832                            void *data, void **res)
1833 {
1834         struct lov_obd *lov = &exp->exp_obd->u.lov;
1835         struct lov_async_page *lap;
1836         int rc;
1837         ENTRY;
1838
1839         if (lsm_bad_magic(lsm))
1840                 RETURN(-EINVAL);
1841         LASSERT(loi == NULL);
1842
1843         OBD_ALLOC(lap, sizeof(*lap));
1844         if (lap == NULL)
1845                 RETURN(-ENOMEM);
1846
1847         lap->lap_magic = LAP_MAGIC;
1848         lap->lap_caller_ops = ops;
1849         lap->lap_caller_data = data;
1850
1851         /* for now only raid 0 which passes through */
1852         lap->lap_stripe = lov_stripe_number(lsm, offset);
1853         lov_stripe_offset(lsm, offset, lap->lap_stripe, &lap->lap_sub_offset);
1854         loi = &lsm->lsm_oinfo[lap->lap_stripe];
1855
1856         /* so the callback doesn't need the lsm */ 
1857         lap->lap_loi_id = loi->loi_id;
1858
1859         rc = obd_prep_async_page(lov->tgts[loi->loi_ost_idx].ltd_exp,
1860                                  lsm, loi, page, lap->lap_sub_offset,
1861                                  &lov_async_page_ops, lap,
1862                                  &lap->lap_sub_cookie);
1863         if (rc) {
1864                 OBD_FREE(lap, sizeof(*lap));
1865                 RETURN(rc);
1866         }
1867         CDEBUG(D_CACHE, "lap %p page %p cookie %p off "LPU64"\n", lap, page,
1868                lap->lap_sub_cookie, offset);
1869         *res = lap;
1870         RETURN(0);
1871 }
1872
1873 static int lov_queue_async_io(struct obd_export *exp,
1874                               struct lov_stripe_md *lsm,
1875                               struct lov_oinfo *loi, void *cookie,
1876                               int cmd, obd_off off, int count,
1877                               obd_flag brw_flags, obd_flag async_flags)
1878 {
1879         struct lov_obd *lov = &exp->exp_obd->u.lov;
1880         struct lov_async_page *lap;
1881         int rc;
1882
1883         LASSERT(loi == NULL);
1884
1885         if (lsm_bad_magic(lsm))
1886                 RETURN(-EINVAL);
1887
1888         lap = lap_from_cookie(cookie);
1889         if (IS_ERR(lap))
1890                 RETURN(PTR_ERR(lap));
1891
1892         loi = &lsm->lsm_oinfo[lap->lap_stripe];
1893         rc = obd_queue_async_io(lov->tgts[loi->loi_ost_idx].ltd_exp, lsm,
1894                                 loi, lap->lap_sub_cookie, cmd, off, count,
1895                                 brw_flags, async_flags);
1896         RETURN(rc);
1897 }
1898
1899 static int lov_set_async_flags(struct obd_export *exp,
1900                                struct lov_stripe_md *lsm,
1901                                struct lov_oinfo *loi, void *cookie,
1902                                obd_flag async_flags)
1903 {
1904         struct lov_obd *lov = &exp->exp_obd->u.lov;
1905         struct lov_async_page *lap;
1906         int rc;
1907
1908         LASSERT(loi == NULL);
1909
1910         if (lsm_bad_magic(lsm))
1911                 RETURN(-EINVAL);
1912
1913         lap = lap_from_cookie(cookie);
1914         if (IS_ERR(lap))
1915                 RETURN(PTR_ERR(lap));
1916
1917         loi = &lsm->lsm_oinfo[lap->lap_stripe];
1918         rc = obd_set_async_flags(lov->tgts[loi->loi_ost_idx].ltd_exp,
1919                                  lsm, loi, lap->lap_sub_cookie, async_flags);
1920         RETURN(rc);
1921 }
1922
1923 static int lov_queue_group_io(struct obd_export *exp,
1924                               struct lov_stripe_md *lsm,
1925                               struct lov_oinfo *loi,
1926                               struct obd_io_group *oig, void *cookie,
1927                               int cmd, obd_off off, int count,
1928                               obd_flag brw_flags, obd_flag async_flags)
1929 {
1930         struct lov_obd *lov = &exp->exp_obd->u.lov;
1931         struct lov_async_page *lap;
1932         int rc;
1933
1934         LASSERT(loi == NULL);
1935
1936         if (lsm_bad_magic(lsm))
1937                 RETURN(-EINVAL);
1938
1939         lap = lap_from_cookie(cookie);
1940         if (IS_ERR(lap))
1941                 RETURN(PTR_ERR(lap));
1942
1943         loi = &lsm->lsm_oinfo[lap->lap_stripe];
1944         rc = obd_queue_group_io(lov->tgts[loi->loi_ost_idx].ltd_exp, lsm, loi,
1945                                 oig, lap->lap_sub_cookie, cmd, off, count,
1946                                 brw_flags, async_flags);
1947         RETURN(rc);
1948 }
1949
1950 /* this isn't exactly optimal.  we may have queued sync io in oscs on
1951  * all stripes, but we don't record that fact at queue time.  so we
1952  * trigger sync io on all stripes. */
1953 static int lov_trigger_group_io(struct obd_export *exp,
1954                                 struct lov_stripe_md *lsm,
1955                                 struct lov_oinfo *loi,
1956                                 struct obd_io_group *oig)
1957 {
1958         struct lov_obd *lov = &exp->exp_obd->u.lov;
1959         int rc = 0, i, err;
1960
1961         LASSERT(loi == NULL);
1962
1963         if (lsm_bad_magic(lsm))
1964                 RETURN(-EINVAL);
1965
1966         for (i = 0, loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count;
1967              i++, loi++) {
1968                 err = obd_trigger_group_io(lov->tgts[loi->loi_ost_idx].ltd_exp, 
1969                                            lsm, loi, oig);
1970                 if (rc == 0 && err != 0)
1971                         rc = err;
1972         };
1973         RETURN(rc);
1974 }
1975
1976 static int lov_teardown_async_page(struct obd_export *exp,
1977                                    struct lov_stripe_md *lsm,
1978                                    struct lov_oinfo *loi, void *cookie)
1979 {
1980         struct lov_obd *lov = &exp->exp_obd->u.lov;
1981         struct lov_async_page *lap;
1982         int rc;
1983
1984         LASSERT(loi == NULL);
1985
1986         if (lsm_bad_magic(lsm))
1987                 RETURN(-EINVAL);
1988
1989         lap = lap_from_cookie(cookie);
1990         if (IS_ERR(lap))
1991                 RETURN(PTR_ERR(lap));
1992
1993         loi = &lsm->lsm_oinfo[lap->lap_stripe];
1994         rc = obd_teardown_async_page(lov->tgts[loi->loi_ost_idx].ltd_exp, 
1995                                      lsm, loi, lap->lap_sub_cookie);
1996         if (rc) {
1997                 CERROR("unable to teardown sub cookie %p: %d\n", 
1998                        lap->lap_sub_cookie, rc);
1999                 RETURN(rc);
2000         }
2001         OBD_FREE(lap, sizeof(*lap));
2002         RETURN(rc);
2003 }
2004
2005 static int lov_enqueue(struct obd_export *exp, struct lov_stripe_md *lsm,
2006                        __u32 type, ldlm_policy_data_t *policy, __u32 mode,
2007                        int *flags, void *bl_cb, void *cp_cb, void *gl_cb,
2008                        void *data,__u32 lvb_len, void *lvb_swabber,
2009                        struct lustre_handle *lockh)
2010 {
2011         struct lov_lock_handles *lov_lockh = NULL;
2012         struct lustre_handle *lov_lockhp;
2013         struct lov_obd *lov;
2014         struct lov_oinfo *loi;
2015         char submd_buf[sizeof(struct lov_stripe_md) + sizeof(struct lov_oinfo)];
2016         struct lov_stripe_md *submd = (void *)submd_buf;
2017         ldlm_error_t rc;
2018         int i, save_flags = *flags;
2019         ENTRY;
2020
2021         if (lsm_bad_magic(lsm))
2022                 RETURN(-EINVAL);
2023
2024         /* we should never be asked to replay a lock this way. */
2025         LASSERT((*flags & LDLM_FL_REPLAY) == 0);
2026
2027         if (!exp || !exp->exp_obd)
2028                 RETURN(-ENODEV);
2029
2030         if (lsm->lsm_stripe_count > 1) {
2031                 lov_lockh = lov_llh_new(lsm);
2032                 if (lov_lockh == NULL)
2033                         RETURN(-ENOMEM);
2034
2035                 lockh->cookie = lov_lockh->llh_handle.h_cookie;
2036                 lov_lockhp = lov_lockh->llh_handles;
2037         } else {
2038                 lov_lockhp = lockh;
2039         }
2040
2041         lov = &exp->exp_obd->u.lov;
2042         for (i = 0, loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count;
2043              i++, loi++, lov_lockhp++) {
2044                 ldlm_policy_data_t sub_ext;
2045
2046                 if (!lov_stripe_intersects(lsm, i, policy->l_extent.start,
2047                                            policy->l_extent.end,
2048                                            &sub_ext.l_extent.start,
2049                                            &sub_ext.l_extent.end))
2050                         continue;
2051
2052                 if (lov->tgts[loi->loi_ost_idx].active == 0) {
2053                         CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
2054                         continue;
2055                 }
2056
2057                 /* XXX LOV STACKING: submd should be from the subobj */
2058                 submd->lsm_object_id = loi->loi_id;
2059                 submd->lsm_stripe_count = 0;
2060                 submd->lsm_oinfo->loi_kms_valid = loi->loi_kms_valid;
2061                 submd->lsm_oinfo->loi_rss = loi->loi_rss;
2062                 submd->lsm_oinfo->loi_kms = loi->loi_kms;
2063                 loi->loi_mtime = submd->lsm_oinfo->loi_mtime;
2064                 /* XXX submd is not fully initialized here */
2065                 *flags = save_flags;
2066                 rc = obd_enqueue(lov->tgts[loi->loi_ost_idx].ltd_exp, submd,
2067                                  type, &sub_ext, mode, flags, bl_cb, cp_cb,
2068                                  gl_cb, data, lvb_len, lvb_swabber, lov_lockhp);
2069
2070                 /* XXX FIXME: This unpleasantness doesn't belong here at *all*.
2071                  * It belongs in the OSC, except that the OSC doesn't have
2072                  * access to the real LOI -- it gets a copy, that we created
2073                  * above, and that copy can be arbitrarily out of date.
2074                  *
2075                  * The LOV API is due for a serious rewriting anyways, and this
2076                  * can be addressed then. */
2077                 if (rc == ELDLM_OK) {
2078                         struct ldlm_lock *lock = ldlm_handle2lock(lov_lockhp);
2079                         __u64 tmp = submd->lsm_oinfo->loi_rss;
2080
2081                         LASSERT(lock != NULL);
2082                         loi->loi_rss = tmp;
2083                         /* Extend KMS up to the end of this lock and no further
2084                          * A lock on [x,y] means a KMS of up to y + 1 bytes! */
2085                         if (tmp > lock->l_policy_data.l_extent.end)
2086                                 tmp = lock->l_policy_data.l_extent.end + 1;
2087                         if (tmp >= loi->loi_kms) {
2088                                 CDEBUG(D_INODE, "lock acquired, setting rss="
2089                                        LPU64", kms="LPU64"\n", loi->loi_rss,
2090                                        tmp);
2091                                 loi->loi_kms = tmp;
2092                                 loi->loi_kms_valid = 1;
2093                         } else {
2094                                 CDEBUG(D_INODE, "lock acquired, setting rss="
2095                                        LPU64"; leaving kms="LPU64", end="LPU64
2096                                        "\n", loi->loi_rss, loi->loi_kms,
2097                                        lock->l_policy_data.l_extent.end);
2098                         }
2099                         ldlm_lock_allow_match(lock);
2100                         LDLM_LOCK_PUT(lock);
2101                 } else if (rc == ELDLM_LOCK_ABORTED &&
2102                            save_flags & LDLM_FL_HAS_INTENT) {
2103                         memset(lov_lockhp, 0, sizeof(*lov_lockhp));
2104                         loi->loi_rss = submd->lsm_oinfo->loi_rss;
2105                         CDEBUG(D_INODE, "glimpsed, setting rss="LPU64"; leaving"
2106                                " kms="LPU64"\n", loi->loi_rss, loi->loi_kms);
2107                 } else {
2108                         memset(lov_lockhp, 0, sizeof(*lov_lockhp));
2109                         if (lov->tgts[loi->loi_ost_idx].active) {
2110                                 CERROR("error: enqueue objid "LPX64" subobj "
2111                                        LPX64" on OST idx %d: rc = %d\n",
2112                                        lsm->lsm_object_id, loi->loi_id,
2113                                        loi->loi_ost_idx, rc);
2114                                 GOTO(out_locks, rc);
2115                         }
2116                 }
2117         }
2118         if (lsm->lsm_stripe_count > 1)
2119                 lov_llh_put(lov_lockh);
2120         RETURN(ELDLM_OK);
2121
2122  out_locks:
2123         while (loi--, lov_lockhp--, i-- > 0) {
2124                 struct lov_stripe_md submd;
2125                 int err;
2126
2127                 if (lov_lockhp->cookie == 0)
2128                         continue;
2129
2130                 /* XXX LOV STACKING: submd should be from the subobj */
2131                 submd.lsm_object_id = loi->loi_id;
2132                 submd.lsm_stripe_count = 0;
2133                 err = obd_cancel(lov->tgts[loi->loi_ost_idx].ltd_exp, &submd,
2134                                  mode, lov_lockhp);
2135                 if (err && lov->tgts[loi->loi_ost_idx].active) {
2136                         CERROR("error: cancelling objid "LPX64" on OST "
2137                                "idx %d after enqueue error: rc = %d\n",
2138                                loi->loi_id, loi->loi_ost_idx, err);
2139                 }
2140         }
2141
2142         if (lsm->lsm_stripe_count > 1) {
2143                 lov_llh_destroy(lov_lockh);
2144                 lov_llh_put(lov_lockh);
2145         }
2146         return rc;
2147 }
2148
2149 static int lov_match(struct obd_export *exp, struct lov_stripe_md *lsm,
2150                      __u32 type, ldlm_policy_data_t *policy, __u32 mode,
2151                      int *flags, void *data, struct lustre_handle *lockh)
2152 {
2153         struct lov_lock_handles *lov_lockh = NULL;
2154         struct lustre_handle *lov_lockhp;
2155         struct lov_obd *lov;
2156         struct lov_oinfo *loi;
2157         struct lov_stripe_md submd;
2158         ldlm_error_t rc = 0;
2159         int i;
2160         ENTRY;
2161
2162         if (lsm_bad_magic(lsm))
2163                 RETURN(-EINVAL);
2164
2165         if (!exp || !exp->exp_obd)
2166                 RETURN(-ENODEV);
2167
2168         if (lsm->lsm_stripe_count > 1) {
2169                 lov_lockh = lov_llh_new(lsm);
2170                 if (lov_lockh == NULL)
2171                         RETURN(-ENOMEM);
2172
2173                 lockh->cookie = lov_lockh->llh_handle.h_cookie;
2174                 lov_lockhp = lov_lockh->llh_handles;
2175         } else {
2176                 lov_lockhp = lockh;
2177         }
2178
2179         lov = &exp->exp_obd->u.lov;
2180         for (i = 0, loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count;
2181              i++, loi++, lov_lockhp++) {
2182                 ldlm_policy_data_t sub_ext;
2183                 int lov_flags;
2184
2185                 if (!lov_stripe_intersects(lsm, i, policy->l_extent.start,
2186                                            policy->l_extent.end,
2187                                            &sub_ext.l_extent.start,
2188                                            &sub_ext.l_extent.end))
2189                         continue;
2190
2191                 if (lov->tgts[loi->loi_ost_idx].active == 0) {
2192                         CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
2193                         rc = -EIO;
2194                         break;
2195                 }
2196
2197                 /* XXX LOV STACKING: submd should be from the subobj */
2198                 submd.lsm_object_id = loi->loi_id;
2199                 submd.lsm_stripe_count = 0;
2200                 lov_flags = *flags;
2201                 /* XXX submd is not fully initialized here */
2202                 rc = obd_match(lov->tgts[loi->loi_ost_idx].ltd_exp, &submd,
2203                                type, &sub_ext, mode, &lov_flags, data,
2204                                lov_lockhp);
2205                 if (rc != 1)
2206                         break;
2207         }
2208         if (rc == 1) {
2209                 if (lsm->lsm_stripe_count > 1) {
2210                         if (*flags & LDLM_FL_TEST_LOCK)
2211                                 lov_llh_destroy(lov_lockh);
2212                         lov_llh_put(lov_lockh);
2213                 }
2214                 RETURN(1);
2215         }
2216
2217         while (loi--, lov_lockhp--, i-- > 0) {
2218                 struct lov_stripe_md submd;
2219                 int err;
2220
2221                 if (lov_lockhp->cookie == 0)
2222                         continue;
2223
2224                 /* XXX LOV STACKING: submd should be from the subobj */
2225                 submd.lsm_object_id = loi->loi_id;
2226                 submd.lsm_stripe_count = 0;
2227                 err = obd_cancel(lov->tgts[loi->loi_ost_idx].ltd_exp, &submd,
2228                                  mode, lov_lockhp);
2229                 if (err && lov->tgts[loi->loi_ost_idx].active) {
2230                         CERROR("error: cancelling objid "LPX64" on OST "
2231                                "idx %d after match failure: rc = %d\n",
2232                                loi->loi_id, loi->loi_ost_idx, err);
2233                 }
2234         }
2235
2236         if (lsm->lsm_stripe_count > 1) {
2237                 lov_llh_destroy(lov_lockh);
2238                 lov_llh_put(lov_lockh);
2239         }
2240         RETURN(rc);
2241 }
2242
2243 static int lov_change_cbdata(struct obd_export *exp,
2244                              struct lov_stripe_md *lsm, ldlm_iterator_t it,
2245                              void *data)
2246 {
2247         struct lov_obd *lov;
2248         struct lov_oinfo *loi;
2249         int rc = 0, i;
2250         ENTRY;
2251
2252         if (lsm_bad_magic(lsm))
2253                 RETURN(-EINVAL);
2254
2255         if (!exp || !exp->exp_obd)
2256                 RETURN(-ENODEV);
2257
2258         lov = &exp->exp_obd->u.lov;
2259         for (i = 0,loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++,loi++) {
2260                 struct lov_stripe_md submd;
2261                 if (lov->tgts[loi->loi_ost_idx].active == 0)
2262                         CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
2263
2264                 submd.lsm_object_id = loi->loi_id;
2265                 submd.lsm_stripe_count = 0;
2266                 rc = obd_change_cbdata(lov->tgts[loi->loi_ost_idx].ltd_exp,
2267                                        &submd, it, data);
2268         }
2269         RETURN(rc);
2270 }
2271
2272 static int lov_cancel(struct obd_export *exp, struct lov_stripe_md *lsm,
2273                       __u32 mode, struct lustre_handle *lockh)
2274 {
2275         struct lov_lock_handles *lov_lockh = NULL;
2276         struct lustre_handle *lov_lockhp;
2277         struct lov_obd *lov;
2278         struct lov_oinfo *loi;
2279         int rc = 0, i;
2280         ENTRY;
2281
2282         if (lsm_bad_magic(lsm))
2283                 RETURN(-EINVAL);
2284
2285         if (!exp || !exp->exp_obd)
2286                 RETURN(-ENODEV);
2287
2288         LASSERT(lockh);
2289         if (lsm->lsm_stripe_count > 1) {
2290                 lov_lockh = lov_handle2llh(lockh);
2291                 if (!lov_lockh) {
2292                         CERROR("LOV: invalid lov lock handle %p\n", lockh);
2293                         RETURN(-EINVAL);
2294                 }
2295
2296                 lov_lockhp = lov_lockh->llh_handles;
2297         } else {
2298                 lov_lockhp = lockh;
2299         }
2300
2301         lov = &exp->exp_obd->u.lov;
2302         for (i = 0, loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count;
2303              i++, loi++, lov_lockhp++) {
2304                 struct lov_stripe_md submd;
2305                 int err;
2306
2307                 if (lov_lockhp->cookie == 0) {
2308                         CDEBUG(D_HA, "lov idx %d subobj "LPX64" no lock?\n",
2309                                loi->loi_ost_idx, loi->loi_id);
2310                         continue;
2311                 }
2312
2313                 /* XXX LOV STACKING: submd should be from the subobj */
2314                 submd.lsm_object_id = loi->loi_id;
2315                 submd.lsm_stripe_count = 0;
2316                 err = obd_cancel(lov->tgts[loi->loi_ost_idx].ltd_exp, &submd,
2317                                  mode, lov_lockhp);
2318                 if (err) {
2319                         if (lov->tgts[loi->loi_ost_idx].active) {
2320                                 CERROR("error: cancel objid "LPX64" subobj "
2321                                        LPX64" on OST idx %d: rc = %d\n",
2322                                        lsm->lsm_object_id,
2323                                        loi->loi_id, loi->loi_ost_idx, err);
2324                                 if (!rc)
2325                                         rc = err;
2326                         }
2327                 }
2328         }
2329
2330         if (lsm->lsm_stripe_count > 1)
2331                 lov_llh_destroy(lov_lockh);
2332         if (lov_lockh != NULL)
2333                 lov_llh_put(lov_lockh);
2334         RETURN(rc);
2335 }
2336
2337 static int lov_cancel_unused(struct obd_export *exp,
2338                              struct lov_stripe_md *lsm, int flags, void *opaque)
2339 {
2340         struct lov_obd *lov;
2341         struct lov_oinfo *loi;
2342         int rc = 0, i;
2343         ENTRY;
2344
2345         if (lsm_bad_magic(lsm))
2346                 RETURN(-EINVAL);
2347
2348         if (!exp || !exp->exp_obd)
2349                 RETURN(-ENODEV);
2350
2351         lov = &exp->exp_obd->u.lov;
2352         for (i = 0,loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++,loi++) {
2353                 struct lov_stripe_md submd;
2354                 int err;
2355
2356                 if (lov->tgts[loi->loi_ost_idx].active == 0)
2357                         CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
2358
2359                 submd.lsm_object_id = loi->loi_id;
2360                 submd.lsm_stripe_count = 0;
2361                 err = obd_cancel_unused(lov->tgts[loi->loi_ost_idx].ltd_exp,
2362                                         &submd, flags, opaque);
2363                 if (err && lov->tgts[loi->loi_ost_idx].active) {
2364                         CERROR("error: cancel unused objid "LPX64" subobj "LPX64
2365                                " on OST idx %d: rc = %d\n", lsm->lsm_object_id,
2366                                loi->loi_id, loi->loi_ost_idx, err);
2367                         if (!rc)
2368                                 rc = err;
2369                 }
2370         }
2371         RETURN(rc);
2372 }
2373
2374 #define LOV_U64_MAX ((__u64)~0ULL)
2375 #define LOV_SUM_MAX(tot, add)                                           \
2376         do {                                                            \
2377                 if ((tot) + (add) < (tot))                              \
2378                         (tot) = LOV_U64_MAX;                            \
2379                 else                                                    \
2380                         (tot) += (add);                                 \
2381         } while(0)
2382
2383 static int lov_statfs(struct obd_device *obd, struct obd_statfs *osfs,
2384                       unsigned long max_age)
2385 {
2386         struct lov_obd *lov = &obd->u.lov;
2387         struct obd_statfs lov_sfs;
2388         int set = 0;
2389         int rc = 0;
2390         int i;
2391         ENTRY;
2392
2393
2394         /* We only get block data from the OBD */
2395         for (i = 0; i < lov->desc.ld_tgt_count; i++) {
2396                 int err;
2397
2398                 if (!lov->tgts[i].active) {
2399                         CDEBUG(D_HA, "lov idx %d inactive\n", i);
2400                         continue;
2401                 }
2402
2403                 err = obd_statfs(class_exp2obd(lov->tgts[i].ltd_exp), &lov_sfs,
2404                                  max_age);
2405                 if (err) {
2406                         if (lov->tgts[i].active && !rc)
2407                                 rc = err;
2408                         continue;
2409                 }
2410
2411                 if (!set) {
2412                         memcpy(osfs, &lov_sfs, sizeof(lov_sfs));
2413                         set = 1;
2414                 } else {
2415                         osfs->os_bfree += lov_sfs.os_bfree;
2416                         osfs->os_bavail += lov_sfs.os_bavail;
2417                         osfs->os_blocks += lov_sfs.os_blocks;
2418                         /* XXX not sure about this one - depends on policy.
2419                          *   - could be minimum if we always stripe on all OBDs
2420                          *     (but that would be wrong for any other policy,
2421                          *     if one of the OBDs has no more objects left)
2422                          *   - could be sum if we stripe whole objects
2423                          *   - could be average, just to give a nice number
2424                          *
2425                          * To give a "reasonable" (if not wholly accurate)
2426                          * number, we divide the total number of free objects
2427                          * by expected stripe count (watch out for overflow).
2428                          */
2429                         LOV_SUM_MAX(osfs->os_files, lov_sfs.os_files);
2430                         LOV_SUM_MAX(osfs->os_ffree, lov_sfs.os_ffree);
2431                 }
2432         }
2433
2434         if (set) {
2435                 __u32 expected_stripes = lov->desc.ld_default_stripe_count ?
2436                                          lov->desc.ld_default_stripe_count :
2437                                          lov->desc.ld_active_tgt_count;
2438
2439                 if (osfs->os_files != LOV_U64_MAX)
2440                         do_div(osfs->os_files, expected_stripes);
2441                 if (osfs->os_ffree != LOV_U64_MAX)
2442                         do_div(osfs->os_ffree, expected_stripes);
2443         } else if (!rc)
2444                 rc = -EIO;
2445
2446         RETURN(rc);
2447 }
2448
2449 static int lov_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
2450                          void *karg, void *uarg)
2451 {
2452         struct obd_device *obddev = class_exp2obd(exp);
2453         struct lov_obd *lov = &obddev->u.lov;
2454         int i, count = lov->desc.ld_tgt_count;
2455         struct obd_uuid *uuidp;
2456         int rc;
2457
2458         ENTRY;
2459
2460         switch (cmd) {
2461         case OBD_IOC_LOV_GET_CONFIG: {
2462                 struct obd_ioctl_data *data = karg;
2463                 struct lov_tgt_desc *tgtdesc;
2464                 struct lov_desc *desc;
2465                 char *buf = NULL;
2466
2467                 buf = NULL;
2468                 len = 0;
2469                 if (obd_ioctl_getdata(&buf, &len, (void *)uarg))
2470                         RETURN(-EINVAL);
2471
2472                 data = (struct obd_ioctl_data *)buf;
2473
2474                 if (sizeof(*desc) > data->ioc_inllen1) {
2475                         OBD_FREE(buf, len);
2476                         RETURN(-EINVAL);
2477                 }
2478
2479                 if (sizeof(uuidp->uuid) * count > data->ioc_inllen2) {
2480                         OBD_FREE(buf, len);
2481                         RETURN(-EINVAL);
2482                 }
2483
2484                 desc = (struct lov_desc *)data->ioc_inlbuf1;
2485                 memcpy(desc, &(lov->desc), sizeof(*desc));
2486
2487                 uuidp = (struct obd_uuid *)data->ioc_inlbuf2;
2488                 tgtdesc = lov->tgts;
2489                 for (i = 0; i < count; i++, uuidp++, tgtdesc++)
2490                         obd_str2uuid(uuidp, tgtdesc->uuid.uuid);
2491
2492                 rc = copy_to_user((void *)uarg, buf, len);
2493                 if (rc)
2494                         rc = -EFAULT;
2495                 obd_ioctl_freedata(buf, len);
2496                 break;
2497         }
2498         case LL_IOC_LOV_SETSTRIPE:
2499                 rc = lov_setstripe(exp, karg, uarg);
2500                 break;
2501         case LL_IOC_LOV_GETSTRIPE:
2502                 rc = lov_getstripe(exp, karg, uarg);
2503                 break;
2504         case LL_IOC_LOV_SETEA:
2505                 rc = lov_setea(exp, karg, uarg);
2506                 break;
2507         default: {
2508                 int set = 0;
2509                 if (count == 0)
2510                         RETURN(-ENOTTY);
2511                 rc = 0;
2512                 for (i = 0; i < count; i++) {
2513                         int err;
2514
2515                         err = obd_iocontrol(cmd, lov->tgts[i].ltd_exp,
2516                                             len, karg, uarg);
2517                         if (err) {
2518                                 if (lov->tgts[i].active) {
2519                                         CERROR("error: iocontrol OSC %s on OST"
2520                                                "idx %d: err = %d\n",
2521                                                lov->tgts[i].uuid.uuid, i, err);
2522                                         if (!rc)
2523                                                 rc = err;
2524                                 }
2525                         } else
2526                                 set = 1;
2527                 }
2528                 if (!set && !rc)
2529                         rc = -EIO;
2530         }
2531         }
2532
2533         RETURN(rc);
2534 }
2535
2536 static int lov_get_info(struct obd_export *exp, __u32 keylen,
2537                         void *key, __u32 *vallen, void *val)
2538 {
2539         struct obd_device *obddev = class_exp2obd(exp);
2540         struct lov_obd *lov = &obddev->u.lov;
2541         int i;
2542         ENTRY;
2543
2544         if (!vallen || !val)
2545                 RETURN(-EFAULT);
2546
2547         if (keylen > strlen("lock_to_stripe") &&
2548             strcmp(key, "lock_to_stripe") == 0) {
2549                 struct {
2550                         char name[16];
2551                         struct ldlm_lock *lock;
2552                         struct lov_stripe_md *lsm;
2553                 } *data = key;
2554                 struct lov_oinfo *loi;
2555                 __u32 *stripe = val;
2556
2557                 if (*vallen < sizeof(*stripe))
2558                         RETURN(-EFAULT);
2559                 *vallen = sizeof(*stripe);
2560
2561                 /* XXX This is another one of those bits that will need to
2562                  * change if we ever actually support nested LOVs.  It uses
2563                  * the lock's export to find out which stripe it is. */
2564                 for (i = 0, loi = data->lsm->lsm_oinfo;
2565                      i < data->lsm->lsm_stripe_count;
2566                      i++, loi++) {
2567                         if (lov->tgts[loi->loi_ost_idx].ltd_exp == 
2568                             data->lock->l_conn_export) {
2569                                 *stripe = i;
2570                                 RETURN(0);
2571                         }
2572                 }
2573                 RETURN(-ENXIO);
2574         } else if (keylen >= strlen("size_to_stripe") &&
2575                    strcmp(key, "size_to_stripe") == 0) {
2576                 struct {
2577                         int stripe_number;
2578                         __u64 size;
2579                         struct lov_stripe_md *lsm;
2580                 } *data = val;
2581
2582                 if (*vallen < sizeof(*data))
2583                         RETURN(-EFAULT);
2584
2585                 data->size = lov_size_to_stripe(data->lsm, data->size,
2586                                                 data->stripe_number);
2587                 RETURN(0);
2588         } else if (keylen >= strlen("last_id") && strcmp(key, "last_id") == 0) {
2589                 obd_id *ids = val;
2590                 int rc, size = sizeof(obd_id);
2591                 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
2592                         if (!lov->tgts[i].active)
2593                                 continue;
2594                         rc = obd_get_info(lov->tgts[i].ltd_exp, keylen, key,
2595                                           &size, &(ids[i]));
2596                         if (rc != 0)
2597                                 RETURN(rc);
2598                 }
2599                 RETURN(0);
2600         } else if (keylen >= strlen("lovdesc") && strcmp(key, "lovdesc") == 0) {
2601                 struct lov_desc *desc_ret = val;
2602                 *desc_ret = lov->desc;
2603                 
2604                 RETURN(0);
2605         }
2606
2607         RETURN(-EINVAL);
2608 }
2609
2610 static int lov_set_info(struct obd_export *exp, obd_count keylen,
2611                         void *key, obd_count vallen, void *val)
2612 {
2613         struct obd_device *obddev = class_exp2obd(exp);
2614         struct lov_obd *lov = &obddev->u.lov;
2615         int i, rc = 0;
2616         ENTRY;
2617
2618 #define KEY_IS(str) \
2619         (keylen == strlen(str) && memcmp(key, str, keylen) == 0)
2620
2621         if (KEY_IS("next_id")) {
2622                 if (vallen != lov->desc.ld_tgt_count)
2623                         RETURN(-EINVAL);
2624                 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
2625                         int er;
2626
2627                         if (!lov->tgts[i].active)
2628                                 continue;
2629
2630                         er = obd_set_info(lov->tgts[i].ltd_exp, keylen, key,
2631                                           sizeof(obd_id), ((obd_id*)val) + i);
2632                         if (!rc)
2633                                 rc = er;
2634                 }
2635                 RETURN(rc);
2636         }
2637
2638         if (KEY_IS("growth_count")) {
2639                 if (vallen != sizeof(int))
2640                         RETURN(-EINVAL);
2641         } else if (KEY_IS("mds_conn") || KEY_IS("unlinked")) {
2642                 if (vallen != 0)
2643                         RETURN(-EINVAL);
2644         } else {
2645                 RETURN(-EINVAL);
2646         }
2647
2648         for (i = 0; i < lov->desc.ld_tgt_count; i++) {
2649                 int er;
2650
2651                 if (val && !obd_uuid_equals(val, &lov->tgts[i].uuid)) 
2652                         continue;
2653
2654                 if (!val && !lov->tgts[i].active)
2655                         continue;
2656
2657                 er = obd_set_info(lov->tgts[i].ltd_exp, keylen, key, vallen,
2658                                    val);
2659                 if (!rc)
2660                         rc = er;
2661         }
2662         RETURN(rc);
2663 #undef KEY_IS
2664
2665 }
2666
2667 /* Merge rss if kms == 0
2668  *
2669  * Even when merging RSS, we will take the KMS value if it's larger.
2670  * This prevents getattr from stomping on dirty cached pages which
2671  * extend the file size. */
2672 __u64 lov_merge_size(struct lov_stripe_md *lsm, int kms)
2673 {
2674         struct lov_oinfo *loi;
2675         __u64 size = 0;
2676         int i;
2677
2678         for (i = 0, loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count;
2679              i++, loi++) {
2680                 obd_size lov_size, tmpsize;
2681
2682                 tmpsize = loi->loi_kms;
2683                 if (kms == 0 && loi->loi_rss > tmpsize)
2684                         tmpsize = loi->loi_rss;
2685
2686                 lov_size = lov_stripe_size(lsm, tmpsize, i);
2687                 if (lov_size > size)
2688                         size = lov_size;
2689         }
2690         return size;
2691 }
2692 EXPORT_SYMBOL(lov_merge_size);
2693
2694 __u64 lov_merge_mtime(struct lov_stripe_md *lsm, __u64 current_time)
2695 {
2696         struct lov_oinfo *loi;
2697         int i;
2698
2699         for (i = 0, loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count;
2700              i++, loi++) {
2701                 if (loi->loi_mtime > current_time)
2702                         current_time = loi->loi_mtime;
2703         }
2704         return current_time;
2705 }
2706 EXPORT_SYMBOL(lov_merge_mtime);
2707
2708 #if 0
2709 struct lov_multi_wait {
2710         struct ldlm_lock *lock;
2711         wait_queue_t      wait;
2712         int               completed;
2713         int               generation;
2714 };
2715
2716 int lov_complete_many(struct obd_export *exp, struct lov_stripe_md *lsm,
2717                       struct lustre_handle *lockh)
2718 {
2719         struct lov_lock_handles *lov_lockh = NULL;
2720         struct lustre_handle *lov_lockhp;
2721         struct lov_obd *lov;
2722         struct lov_oinfo *loi;
2723         struct lov_multi_wait *queues;
2724         int rc = 0, i;
2725         ENTRY;
2726
2727         if (lsm_bad_magic(lsm))
2728                 RETURN(-EINVAL);
2729
2730         if (!exp || !exp->exp_obd)
2731                 RETURN(-ENODEV);
2732
2733         LASSERT(lockh != NULL);
2734         if (lsm->lsm_stripe_count > 1) {
2735                 lov_lockh = lov_handle2llh(lockh);
2736                 if (lov_lockh == NULL) {
2737                         CERROR("LOV: invalid lov lock handle %p\n", lockh);
2738                         RETURN(-EINVAL);
2739                 }
2740
2741                 lov_lockhp = lov_lockh->llh_handles;
2742         } else {
2743                 lov_lockhp = lockh;
2744         }
2745
2746         OBD_ALLOC(queues, lsm->lsm_stripe_count * sizeof(*queues));
2747         if (queues == NULL)
2748                 GOTO(out, rc = -ENOMEM);
2749
2750         lov = &exp->exp_obd->u.lov;
2751         for (i = 0, loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count;
2752              i++, loi++, lov_lockhp++) {
2753                 struct ldlm_lock *lock;
2754                 struct obd_device *obd;
2755                 unsigned long irqflags;
2756
2757                 lock = ldlm_handle2lock(lov_lockhp);
2758                 if (lock == NULL) {
2759                         CDEBUG(D_HA, "lov idx %d subobj "LPX64" no lock?\n",
2760                                loi->loi_ost_idx, loi->loi_id);
2761                         queues[i].completed = 1;
2762                         continue;
2763                 }
2764
2765                 queues[i].lock = lock;
2766                 init_waitqueue_entry(&(queues[i].wait), current);
2767                 add_wait_queue(lock->l_waitq, &(queues[i].wait));
2768
2769                 obd = class_exp2obd(lock->l_conn_export);
2770                 if (obd != NULL)
2771                         imp = obd->u.cli.cl_import;
2772                 if (imp != NULL) {
2773                         spin_lock_irqsave(&imp->imp_lock, irqflags);
2774                         queues[i].generation = imp->imp_generation;
2775                         spin_unlock_irqrestore(&imp->imp_lock, irqflags);
2776                 }
2777         }
2778
2779         lwi = LWI_TIMEOUT_INTR(obd_timeout * HZ, ldlm_expired_completion_wait,
2780                                interrupted_completion_wait, &lwd);
2781         rc = l_wait_event_added(check_multi_complete(queues, lsm), &lwi);
2782
2783         for (i = 0; i < lsm->lsm_stripe_count; i++)
2784                 remove_wait_queue(lock->l_waitq, &(queues[i].wait));
2785
2786         if (rc == -EINTR || rc == -ETIMEDOUT) {
2787
2788
2789         }
2790
2791  out:
2792         if (lov_lockh != NULL)
2793                 lov_llh_put(lov_lockh);
2794         RETURN(rc);
2795 }
2796 #endif
2797
2798 void lov_increase_kms(struct obd_export *exp, struct lov_stripe_md *lsm,
2799                       obd_off size)
2800 {
2801         struct lov_oinfo *loi;
2802         int stripe = 0;
2803         __u64 kms;
2804         ENTRY;
2805
2806         if (size > 0)
2807                 stripe = lov_stripe_number(lsm, size - 1);
2808         kms = lov_size_to_stripe(lsm, size, stripe);
2809         loi = &(lsm->lsm_oinfo[stripe]);
2810
2811         CDEBUG(D_INODE, "stripe %d KMS %sincreasing "LPU64"->"LPU64"\n",
2812                stripe, kms > loi->loi_kms ? "" : "not ", loi->loi_kms, kms);
2813         if (kms > loi->loi_kms)
2814                 loi->loi_kms = kms;
2815         EXIT;
2816 }
2817 EXPORT_SYMBOL(lov_increase_kms);
2818
2819 struct obd_ops lov_obd_ops = {
2820         o_owner:       THIS_MODULE,
2821         o_attach:      lov_attach,
2822         o_detach:      lov_detach,
2823         o_setup:       lov_setup,
2824         o_cleanup:     lov_cleanup,
2825         o_connect:     lov_connect,
2826         o_disconnect:  lov_disconnect,
2827         o_statfs:      lov_statfs,
2828         o_packmd:      lov_packmd,
2829         o_unpackmd:    lov_unpackmd,
2830         o_create:      lov_create,
2831         o_destroy:     lov_destroy,
2832         o_getattr:     lov_getattr,
2833         o_getattr_async: lov_getattr_async,
2834         o_setattr:     lov_setattr,
2835         o_brw:         lov_brw,
2836         o_brw_async:   lov_brw_async,
2837         .o_prep_async_page =    lov_prep_async_page,
2838         .o_queue_async_io =     lov_queue_async_io,
2839         .o_set_async_flags =    lov_set_async_flags,
2840         .o_queue_group_io =     lov_queue_group_io,
2841         .o_trigger_group_io =   lov_trigger_group_io,
2842         .o_teardown_async_page  lov_teardown_async_page,
2843         o_punch:       lov_punch,
2844         o_sync:        lov_sync,
2845         o_enqueue:     lov_enqueue,
2846         o_match:       lov_match,
2847         o_change_cbdata: lov_change_cbdata,
2848         o_cancel:      lov_cancel,
2849         o_cancel_unused: lov_cancel_unused,
2850         o_iocontrol:   lov_iocontrol,
2851         o_get_info:    lov_get_info,
2852         o_set_info:    lov_set_info,
2853         o_llog_init:   lov_llog_init,
2854         o_llog_finish: lov_llog_finish,
2855         o_notify: lov_notify,
2856 };
2857
2858 int __init lov_init(void)
2859 {
2860         struct lprocfs_static_vars lvars;
2861         int rc;
2862
2863         lprocfs_init_vars(lov, &lvars);
2864         rc = class_register_type(&lov_obd_ops, lvars.module_vars,
2865                                  OBD_LOV_DEVICENAME);
2866         RETURN(rc);
2867 }
2868
2869 #ifdef __KERNEL__
2870 static void /*__exit*/ lov_exit(void)
2871 {
2872         class_unregister_type(OBD_LOV_DEVICENAME);
2873 }
2874
2875 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
2876 MODULE_DESCRIPTION("Lustre Logical Object Volume OBD driver");
2877 MODULE_LICENSE("GPL");
2878
2879 module_init(lov_init);
2880 module_exit(lov_exit);
2881 #endif