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