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