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