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