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