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