Whamcloud - gitweb
- landed b_hd_cray_merge3
[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 /* obd methods */
55 #define MAX_STRING_SIZE 128
56 static int lov_connect_obd(struct obd_device *obd, struct lov_tgt_desc *tgt,
57                            int activate, unsigned long connect_flags)
58 {
59         struct obd_uuid lov_osc_uuid = { "LOV_OSC_UUID" };
60         struct obd_uuid *tgt_uuid = &tgt->uuid;
61
62 #ifdef __KERNEL__
63         struct proc_dir_entry *lov_proc_dir;
64 #endif
65         struct lov_obd *lov = &obd->u.lov;
66         struct lustre_handle conn = {0, };
67         struct obd_device *tgt_obd;
68         int rc;
69         ENTRY;
70
71         tgt_obd = class_find_client_obd(tgt_uuid, LUSTRE_OSC_NAME,
72                                         &obd->obd_uuid);
73
74         if (!tgt_obd) {
75                 CERROR("Target %s not attached\n", tgt_uuid->uuid);
76                 RETURN(-EINVAL);
77         }
78
79         if (!tgt_obd->obd_set_up) {
80                 CERROR("Target %s not set up\n", tgt_uuid->uuid);
81                 RETURN(-EINVAL);
82         }
83
84         if (activate) {
85                 tgt_obd->obd_no_recov = 0;
86                 ptlrpc_activate_import(tgt_obd->u.cli.cl_import);
87         }
88
89         if (tgt_obd->u.cli.cl_import->imp_invalid) {
90                 CERROR("not connecting OSC %s; administratively "
91                        "disabled\n", tgt_uuid->uuid);
92                 rc = obd_register_observer(tgt_obd, obd);
93                 if (rc) {
94                         CERROR("Target %s register_observer error %d; "
95                                "will not be able to reactivate\n",
96                                tgt_uuid->uuid, rc);
97                 }
98                 RETURN(0);
99         }
100
101         rc = obd_connect(&conn, tgt_obd, &lov_osc_uuid, connect_flags);
102         if (rc) {
103                 CERROR("Target %s connect error %d\n", tgt_uuid->uuid, rc);
104                 RETURN(rc);
105         }
106         tgt->ltd_exp = class_conn2export(&conn);
107
108         rc = obd_register_observer(tgt_obd, obd);
109         if (rc) {
110                 CERROR("Target %s register_observer error %d\n",
111                        tgt_uuid->uuid, rc);
112                 obd_disconnect(tgt->ltd_exp, 0);
113                 tgt->ltd_exp = NULL;
114                 RETURN(rc);
115         }
116
117         tgt->active = 1;
118         lov->desc.ld_active_tgt_count++;
119
120 #ifdef __KERNEL__
121         lov_proc_dir = lprocfs_srch(obd->obd_proc_entry, "target_obds");
122         if (lov_proc_dir) {
123                 struct obd_device *osc_obd = class_conn2obd(&conn);
124                 struct proc_dir_entry *osc_symlink;
125                 char name[MAX_STRING_SIZE + 1];
126
127                 LASSERT(osc_obd != NULL);
128                 LASSERT(osc_obd->obd_type != NULL);
129                 LASSERT(osc_obd->obd_type->typ_name != NULL);
130                 name[MAX_STRING_SIZE] = '\0';
131                 snprintf(name, MAX_STRING_SIZE, "../../../%s/%s",
132                          osc_obd->obd_type->typ_name,
133                          osc_obd->obd_name);
134                 osc_symlink = proc_symlink(osc_obd->obd_name, lov_proc_dir,
135                                            name);
136                 if (osc_symlink == NULL) {
137                         CERROR("could not register LOV target "
138                                "/proc/fs/lustre/%s/%s/target_obds/%s\n",
139                                obd->obd_type->typ_name, obd->obd_name,
140                                osc_obd->obd_name);
141                         lprocfs_remove(lov_proc_dir);
142                         lov_proc_dir = NULL;
143                 }
144         }
145 #endif
146
147         RETURN(0);
148 }
149
150 static int lov_connect(struct lustre_handle *conn, struct obd_device *obd,
151                        struct obd_uuid *cluuid, unsigned long flags)
152 {
153 #ifdef __KERNEL__
154         struct proc_dir_entry *lov_proc_dir;
155 #endif
156         struct lov_obd *lov = &obd->u.lov;
157         struct lov_tgt_desc *tgt;
158         struct obd_export *exp;
159         int rc, rc2, i;
160         ENTRY;
161
162         rc = class_connect(conn, obd, cluuid);
163         if (rc)
164                 RETURN(rc);
165
166         exp = class_conn2export(conn);
167
168         /* We don't want to actually do the underlying connections more than
169          * once, so keep track. */
170         lov->refcount++;
171         if (lov->refcount > 1) {
172                 class_export_put(exp);
173                 RETURN(0);
174         }
175
176 #ifdef __KERNEL__
177         lov_proc_dir = lprocfs_register("target_obds", obd->obd_proc_entry,
178                                         NULL, NULL);
179         if (IS_ERR(lov_proc_dir)) {
180                 CERROR("could not register /proc/fs/lustre/%s/%s/target_obds.",
181                        obd->obd_type->typ_name, obd->obd_name);
182                 lov_proc_dir = NULL;
183         }
184 #endif
185
186         /* connect_flags is the MDS number, save for use in lov_add_obd */
187         lov->lov_connect_flags = flags;
188         for (i = 0, tgt = lov->tgts; i < lov->desc.ld_tgt_count; i++, tgt++) {
189                 if (obd_uuid_empty(&tgt->uuid))
190                         continue;
191                 rc = lov_connect_obd(obd, tgt, 0, flags);
192                 if (rc)
193                         GOTO(out_disc, rc);
194         }
195
196         class_export_put(exp);
197         RETURN (0);
198
199  out_disc:
200 #ifdef __KERNEL__
201         if (lov_proc_dir)
202                 lprocfs_remove(lov_proc_dir);
203 #endif
204
205         while (i-- > 0) {
206                 struct obd_uuid uuid;
207                 --tgt;
208                 --lov->desc.ld_active_tgt_count;
209                 tgt->active = 0;
210                 /* save for CERROR below; (we know it's terminated) */
211                 uuid = tgt->uuid;
212                 rc2 = obd_disconnect(tgt->ltd_exp, 0);
213                 if (rc2)
214                         CERROR("error: LOV target %s disconnect on OST idx %d: "
215                                "rc = %d\n", uuid.uuid, i, rc2);
216         }
217         class_disconnect(exp, 0);
218         RETURN (rc);
219 }
220
221 static int lov_disconnect_obd(struct obd_device *obd, 
222                               struct lov_tgt_desc *tgt,
223                               unsigned long flags)
224 {
225 #ifdef __KERNEL__
226         struct proc_dir_entry *lov_proc_dir;
227 #endif
228         struct obd_device *osc_obd = class_exp2obd(tgt->ltd_exp);
229         struct lov_obd *lov = &obd->u.lov;
230         int rc;
231         ENTRY;
232
233 #ifdef __KERNEL__
234         lov_proc_dir = lprocfs_srch(obd->obd_proc_entry, "target_obds");
235         if (lov_proc_dir) {
236                 struct proc_dir_entry *osc_symlink;
237
238                 osc_symlink = lprocfs_srch(lov_proc_dir, osc_obd->obd_name);
239                 if (osc_symlink) {
240                         lprocfs_remove(osc_symlink);
241                 } else {
242                         CERROR("/proc/fs/lustre/%s/%s/target_obds/%s missing\n",
243                                obd->obd_type->typ_name, obd->obd_name,
244                                osc_obd->obd_name);
245                 }
246         }
247 #endif
248         if (obd->obd_no_recov) {
249                 /* Pass it on to our clients.
250                  * XXX This should be an argument to disconnect,
251                  * XXX not a back-door flag on the OBD.  Ah well.
252                  */
253                 if (osc_obd)
254                         osc_obd->obd_no_recov = 1;
255         }
256
257         obd_register_observer(tgt->ltd_exp->exp_obd, NULL);
258         rc = obd_disconnect(tgt->ltd_exp, flags);
259         if (rc) {
260                 if (tgt->active) {
261                         CERROR("Target %s disconnect error %d\n",
262                                tgt->uuid.uuid, rc);
263                 }
264                 rc = 0;
265         }
266
267         if (tgt->active) {
268                 tgt->active = 0;
269                 lov->desc.ld_active_tgt_count--;
270         }
271         tgt->ltd_exp = NULL;
272         RETURN(0);
273 }
274
275 static int lov_disconnect(struct obd_export *exp, unsigned long flags)
276 {
277         struct obd_device *obd = class_exp2obd(exp);
278 #ifdef __KERNEL__
279         struct proc_dir_entry *lov_proc_dir;
280 #endif
281         struct lov_obd *lov = &obd->u.lov;
282         struct lov_tgt_desc *tgt;
283         int rc, i;
284         ENTRY;
285
286         if (!lov->tgts)
287                 goto out_local;
288
289         /* Only disconnect the underlying layers on the final disconnect. */
290         lov->refcount--;
291         if (lov->refcount != 0)
292                 goto out_local;
293
294         for (i = 0, tgt = lov->tgts; i < lov->desc.ld_tgt_count; i++, tgt++) {
295                 if (tgt->ltd_exp)
296                         lov_disconnect_obd(obd, tgt, flags);
297         }
298
299 #ifdef __KERNEL__
300         lov_proc_dir = lprocfs_srch(obd->obd_proc_entry, "target_obds");
301         if (lov_proc_dir) {
302                 lprocfs_remove(lov_proc_dir);
303         } else {
304                 CERROR("/proc/fs/lustre/%s/%s/target_obds missing.",
305                        obd->obd_type->typ_name, obd->obd_name);
306         }
307 #endif
308         
309  out_local:
310         rc = class_disconnect(exp, 0);
311         RETURN(rc);
312 }
313
314 /* Error codes:
315  *
316  *  -EINVAL  : UUID can't be found in the LOV's target list
317  *  -ENOTCONN: The UUID is found, but the target connection is bad (!)
318  *  -EBADF   : The UUID is found, but the OBD is the wrong type (!)
319  */
320 static int lov_set_osc_active(struct lov_obd *lov, struct obd_uuid *uuid,
321                               int activate)
322 {
323         struct lov_tgt_desc *tgt;
324         int i, rc = 0;
325         ENTRY;
326
327         CDEBUG(D_INFO, "Searching in lov %p for uuid %s (activate=%d)\n",
328                lov, uuid->uuid, activate);
329
330         spin_lock(&lov->lov_lock);
331         for (i = 0, tgt = lov->tgts; i < lov->desc.ld_tgt_count; i++, tgt++) {
332                 if (tgt->ltd_exp == NULL)
333                         continue;
334
335                 CDEBUG(D_INFO, "lov idx %d is %s conn "LPX64"\n",
336                        i, tgt->uuid.uuid, tgt->ltd_exp->exp_handle.h_cookie);
337                 if (strncmp(uuid->uuid, tgt->uuid.uuid, sizeof uuid->uuid) == 0)
338                         break;
339         }
340
341         if (i == lov->desc.ld_tgt_count)
342                 GOTO(out, rc = -EINVAL);
343
344
345         if (tgt->active == activate) {
346                 CDEBUG(D_INFO, "OSC %s already %sactive!\n", uuid->uuid,                       
347                         activate ? "" : "in");
348                 GOTO(out, rc);
349         }
350
351         CDEBUG(D_INFO, "Marking OSC %s %sactive\n", uuid->uuid,
352                activate ? "" : "in");
353
354         tgt->active = activate;
355         if (activate)
356                 lov->desc.ld_active_tgt_count++;
357         else
358                 lov->desc.ld_active_tgt_count--;
359
360         EXIT;
361  out:
362         spin_unlock(&lov->lov_lock);
363         return rc;
364 }
365
366 static int lov_notify(struct obd_device *obd, struct obd_device *watched,
367                       int active, void *data)
368 {
369         struct obd_uuid *uuid;
370         int rc;
371         ENTRY;
372
373         if (strcmp(watched->obd_type->typ_name, LUSTRE_OSC_NAME)) {
374                 CERROR("unexpected notification of %s %s!\n",
375                        watched->obd_type->typ_name,
376                        watched->obd_name);
377                 return -EINVAL;
378         }
379         uuid = &watched->u.cli.cl_import->imp_target_uuid;
380
381         /* Set OSC as active before notifying the observer, so the
382          * observer can use the OSC normally.  
383          */
384         rc = lov_set_osc_active(&obd->u.lov, uuid, active);
385         if (rc) {
386                 CERROR("%sactivation of %s failed: %d\n",
387                        active ? "" : "de", uuid->uuid, rc);
388                 RETURN(rc);
389         }
390
391         if (obd->obd_observer)
392                 /* Pass the notification up the chain. */
393                 rc = obd_notify(obd->obd_observer, watched, active, data);
394
395         RETURN(rc);
396 }
397
398 int lov_attach(struct obd_device *dev, obd_count len, void *data)
399 {
400         struct lprocfs_static_vars lvars;
401         int rc;
402
403         lprocfs_init_vars(lov, &lvars);
404         rc = lprocfs_obd_attach(dev, lvars.obd_vars);
405         if (rc == 0) {
406 #ifdef __KERNEL__
407                 struct proc_dir_entry *entry;
408
409                 entry = create_proc_entry("target_obd_status", 0444, 
410                                           dev->obd_proc_entry);
411                 if (entry == NULL) {
412                         rc = -ENOMEM;
413                 } else {
414                         entry->proc_fops = &lov_proc_target_fops;
415                         entry->data = dev;
416                 }
417 #endif
418         }
419         return rc;
420 }
421
422 int lov_detach(struct obd_device *dev)
423 {
424         return lprocfs_obd_detach(dev);
425 }
426
427 static int lov_setup(struct obd_device *obd, obd_count len, void *buf)
428 {
429         struct lov_obd *lov = &obd->u.lov;
430         struct lustre_cfg *lcfg = buf;
431         struct lov_desc *desc;
432         int count;
433         ENTRY;
434
435         if (lcfg->lcfg_inllen1 < 1) {
436                 CERROR("LOV setup requires a descriptor\n");
437                 RETURN(-EINVAL);
438         }
439
440         desc = (struct lov_desc *)lcfg->lcfg_inlbuf1;
441         if (sizeof(*desc) > lcfg->lcfg_inllen1) {
442                 CERROR("descriptor size wrong: %d > %d\n",
443                        (int)sizeof(*desc), lcfg->lcfg_inllen1);
444                 RETURN(-EINVAL);
445         }
446  
447         /* Because of 64-bit divide/mod operations only work with a 32-bit
448          * divisor in a 32-bit kernel, we cannot support a stripe width
449          * of 4GB or larger on 32-bit CPUs.
450          */
451        
452         count = desc->ld_default_stripe_count;
453         if (count && (count * desc->ld_default_stripe_size) > ~0UL) {
454                 CERROR("LOV: stripe width "LPU64"x%u > %lu on 32-bit system\n",
455                        desc->ld_default_stripe_size, count, ~0UL);
456                 RETURN(-EINVAL);
457         }
458         if (desc->ld_tgt_count > 0) {
459                 lov->bufsize= sizeof(struct lov_tgt_desc) * desc->ld_tgt_count;
460         } else {
461                 lov->bufsize = sizeof(struct lov_tgt_desc) * LOV_MAX_TGT_COUNT;  
462         }
463         OBD_ALLOC(lov->tgts, lov->bufsize);
464         if (lov->tgts == NULL) {
465                 lov->bufsize = 0;
466                 CERROR("couldn't allocate %d bytes for target table.\n",
467                        lov->bufsize);
468                 RETURN(-EINVAL);
469         }
470
471         desc->ld_tgt_count = 0;
472         desc->ld_active_tgt_count = 0;
473         lov->desc = *desc;
474         spin_lock_init(&lov->lov_lock);
475         sema_init(&lov->lov_llog_sem, 1);
476
477         RETURN(0);
478 }
479
480 static int lov_cleanup(struct obd_device *obd, int flags)
481 {
482         struct lov_obd *lov = &obd->u.lov;
483
484         OBD_FREE(lov->tgts, lov->bufsize);
485         RETURN(0);
486 }
487
488 static int
489 lov_add_obd(struct obd_device *obd, struct obd_uuid *uuidp, int index, int gen)
490 {
491         struct lov_obd *lov = &obd->u.lov;
492         struct lov_tgt_desc *tgt;
493         int rc;
494         ENTRY;
495
496         CDEBUG(D_CONFIG, "uuid: %s idx: %d gen: %d\n",
497                uuidp->uuid, index, gen);
498
499         if ((index < 0) || (index >= LOV_MAX_TGT_COUNT)) {
500                 CERROR("request to add OBD %s at invalid index: %d\n",
501                        uuidp->uuid, index);
502                 RETURN(-EINVAL);
503         }
504
505         if (gen <= 0) {
506                 CERROR("request to add OBD %s with invalid generation: %d\n",
507                        uuidp->uuid, gen);
508                 RETURN(-EINVAL);
509         }
510
511         tgt = lov->tgts + index;
512         if (!obd_uuid_empty(&tgt->uuid)) {
513                 CERROR("OBD already assigned at LOV target index %d\n",
514                        index);
515                 RETURN(-EEXIST);
516         }
517
518         tgt->uuid = *uuidp;
519         /* XXX - add a sanity check on the generation number. */
520         tgt->ltd_gen = gen;
521
522         if (index >= lov->desc.ld_tgt_count)
523                 lov->desc.ld_tgt_count = index + 1;
524
525         CDEBUG(D_CONFIG, "idx: %d ltd_gen: %d ld_tgt_count: %d\n",
526                 index, tgt->ltd_gen, lov->desc.ld_tgt_count);
527
528         if (lov->refcount == 0)
529                 RETURN(0);
530
531         if (tgt->ltd_exp) {
532                 struct obd_device *osc_obd;
533
534                 osc_obd = class_exp2obd(tgt->ltd_exp);
535                 if (osc_obd)
536                         osc_obd->obd_no_recov = 0;
537         }
538
539         rc = lov_connect_obd(obd, tgt, 1, lov->lov_connect_flags);
540         if (rc)
541                 GOTO(out, rc);
542
543         if (obd->obd_observer) {
544                 /* tell the mds_lov about the new target */
545                 rc = obd_notify(obd->obd_observer, tgt->ltd_exp->exp_obd, 1,
546                                 (void *)index);
547         }
548
549         GOTO(out, rc);
550  out:
551         if (rc && tgt->ltd_exp != NULL)
552                 lov_disconnect_obd(obd, tgt, 0);
553         return rc;
554 }
555
556 static int
557 lov_del_obd(struct obd_device *obd, struct obd_uuid *uuidp, int index, int gen)
558 {
559         struct lov_obd *lov = &obd->u.lov;
560         struct lov_tgt_desc *tgt;
561         int count = lov->desc.ld_tgt_count;
562         int rc = 0;
563         ENTRY;
564
565         CDEBUG(D_CONFIG, "uuid: %s idx: %d gen: %d\n",
566                uuidp->uuid, index, gen);
567
568         if (index >= count) {
569                 CERROR("LOV target index %d >= number of LOV OBDs %d.\n",
570                        index, count);
571                 RETURN(-EINVAL);
572         }
573
574         tgt = lov->tgts + index;
575
576         if (obd_uuid_empty(&tgt->uuid)) {
577                 CERROR("LOV target at index %d is not setup.\n", index);
578                 RETURN(-EINVAL);
579         }
580
581         if (strncmp(uuidp->uuid, tgt->uuid.uuid, sizeof uuidp->uuid) != 0) {
582                 CERROR("LOV target UUID %s at index %d doesn't match %s.\n",
583                        tgt->uuid.uuid, index, uuidp->uuid);
584                 RETURN(-EINVAL);
585         }
586
587         if (tgt->ltd_exp) {
588                 struct obd_device *osc_obd;
589
590                 osc_obd = class_exp2obd(tgt->ltd_exp);
591                 if (osc_obd) {
592                         osc_obd->obd_no_recov = 1;
593                         rc = obd_llog_finish(osc_obd, &osc_obd->obd_llogs, 1);
594                         if (rc)
595                                 CERROR("osc_llog_finish error: %d\n", rc);
596                 }
597                 lov_disconnect_obd(obd, tgt, 0);
598         }
599
600         /* XXX - right now there is a dependency on ld_tgt_count being the
601          * maximum tgt index for computing the mds_max_easize. So we can't
602          * shrink it. */
603
604         /* lt_gen = 0 will mean it will not match the gen of any valid loi */
605         memset(tgt, 0, sizeof(*tgt));
606
607         CDEBUG(D_CONFIG, "uuid: %s idx: %d gen: %d exp: %p active: %d\n",
608                tgt->uuid.uuid, index, tgt->ltd_gen, tgt->ltd_exp, tgt->active);
609
610         RETURN(rc);
611 }
612
613 static int lov_process_config(struct obd_device *obd, obd_count len, void *buf)
614 {
615         struct lustre_cfg *lcfg = buf;
616         struct obd_uuid obd_uuid;
617         int cmd;
618         int index;
619         int gen;
620         int rc = 0;
621         ENTRY;
622
623         switch(cmd = lcfg->lcfg_command) {
624         case LCFG_LOV_ADD_OBD:
625         case LCFG_LOV_DEL_OBD: {
626                 if (lcfg->lcfg_inllen1 > sizeof(obd_uuid.uuid))
627                         GOTO(out, rc = -EINVAL);
628
629                 obd_str2uuid(&obd_uuid, lcfg->lcfg_inlbuf1);
630
631                 if (sscanf(lcfg->lcfg_inlbuf2, "%d", &index) != 1)
632                         GOTO(out, rc = -EINVAL);
633                 if (sscanf(lcfg->lcfg_inlbuf3, "%d", &gen) != 1)
634                         GOTO(out, rc = -EINVAL);
635                 if (cmd == LCFG_LOV_ADD_OBD)
636                         rc = lov_add_obd(obd, &obd_uuid, index, gen);
637                 else
638                         rc = lov_del_obd(obd, &obd_uuid, index, gen);
639                 GOTO(out, rc);
640         }
641         default: {
642                 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
643                 GOTO(out, rc = -EINVAL);
644
645         }
646         }
647 out:
648         RETURN(rc);
649 }
650
651 #ifndef log2
652 #define log2(n) ffz(~(n))
653 #endif
654
655 static int lov_clear_orphans(struct obd_export *export, struct obdo *src_oa,
656                              struct lov_stripe_md **ea,
657                              struct obd_trans_info *oti)
658 {
659         struct lov_obd *lov;
660         struct obdo *tmp_oa;
661         struct obd_uuid *ost_uuid = NULL;
662         int rc = 0, i;
663         ENTRY;
664
665         LASSERT(src_oa->o_valid & OBD_MD_FLFLAGS &&
666                 src_oa->o_flags == OBD_FL_DELORPHAN);
667
668         lov = &export->exp_obd->u.lov;
669
670         tmp_oa = obdo_alloc();
671         if (tmp_oa == NULL)
672                 RETURN(-ENOMEM);
673
674         if (src_oa->o_valid & OBD_MD_FLINLINE) {
675                 ost_uuid = (struct obd_uuid *)src_oa->o_inline;
676                 CDEBUG(D_HA, "clearing orphans only for %s\n",
677                        ost_uuid->uuid);
678         }
679
680         for (i = 0; i < lov->desc.ld_tgt_count; i++) {
681                 struct lov_stripe_md obj_md;
682                 struct lov_stripe_md *obj_mdp = &obj_md;
683                 int err;
684
685                 /* if called for a specific target, we don't
686                    care if it is not active. */
687                 if (lov->tgts[i].active == 0 && ost_uuid == NULL) {
688                         CDEBUG(D_HA, "lov idx %d inactive\n", i);
689                         continue;
690                 }
691
692                 if (ost_uuid && !obd_uuid_equals(ost_uuid, &lov->tgts[i].uuid))
693                         continue;
694
695                 memcpy(tmp_oa, src_oa, sizeof(*tmp_oa));
696
697                 /* XXX: LOV STACKING: use real "obj_mdp" sub-data */
698                 err = obd_create(lov->tgts[i].ltd_exp, tmp_oa, &obj_mdp, oti);
699                 if (err)
700                         /* This export will be disabled until it is recovered,
701                            and then orphan recovery will be completed. */
702                         CERROR("error in orphan recovery on OST idx %d/%d: "
703                                "rc = %d\n", i, lov->desc.ld_tgt_count, err);
704
705                 if (ost_uuid)
706                         break;
707         }
708         obdo_free(tmp_oa);
709         RETURN(rc);
710 }
711
712 static int lov_recreate(struct obd_export *exp, struct obdo *src_oa,
713                         struct lov_stripe_md **ea, struct obd_trans_info *oti)
714 {
715         struct lov_stripe_md *obj_mdp, *lsm;
716         struct lov_obd *lov = &exp->exp_obd->u.lov;
717         unsigned ost_idx;
718         int rc, i;
719         ENTRY;
720
721         LASSERT(src_oa->o_valid & OBD_MD_FLFLAGS &&
722                 src_oa->o_flags & OBD_FL_RECREATE_OBJS);
723
724         OBD_ALLOC(obj_mdp, sizeof(*obj_mdp));
725         if (obj_mdp == NULL)
726                 RETURN(-ENOMEM);
727
728         ost_idx = src_oa->o_nlink;
729         lsm = *ea;
730         if (lsm == NULL)
731                 GOTO(out, rc = -EINVAL);
732         if (ost_idx >= lov->desc.ld_tgt_count)
733                 GOTO(out, rc = -EINVAL);
734
735         for (i = 0; i < lsm->lsm_stripe_count; i++) {
736                 if (lsm->lsm_oinfo[i].loi_ost_idx == ost_idx) {
737                         if (lsm->lsm_oinfo[i].loi_id != src_oa->o_id)
738                                 GOTO(out, rc = -EINVAL);
739                         break;
740                 }
741         }
742         if (i == lsm->lsm_stripe_count)
743                 GOTO(out, rc = -EINVAL);
744
745         rc = obd_create(lov->tgts[ost_idx].ltd_exp, src_oa, &obj_mdp, oti);
746 out:
747         OBD_FREE(obj_mdp, sizeof(*obj_mdp));
748         RETURN(rc);
749 }
750
751 /* the LOV expects oa->o_id to be set to the LOV object id */
752 static int lov_create(struct obd_export *exp, struct obdo *src_oa,
753                       struct lov_stripe_md **ea, struct obd_trans_info *oti)
754 {
755         struct lov_request_set *set = NULL;
756         struct list_head *pos;
757         struct lov_obd *lov;
758         int rc = 0;
759         ENTRY;
760
761         LASSERT(ea != NULL);
762         if (exp == NULL)
763                 RETURN(-EINVAL);
764
765         if ((src_oa->o_valid & OBD_MD_FLFLAGS) &&
766             src_oa->o_flags == OBD_FL_DELORPHAN) {
767                 rc = lov_clear_orphans(exp, src_oa, ea, oti);
768                 RETURN(rc);
769         }
770
771         lov = &exp->exp_obd->u.lov;
772         if (!lov->desc.ld_active_tgt_count)
773                 RETURN(-EIO);
774
775         /* Recreate a specific object id at the given OST index */
776         if ((src_oa->o_valid & OBD_MD_FLFLAGS) &&
777             (src_oa->o_flags & OBD_FL_RECREATE_OBJS)) {
778                  rc = lov_recreate(exp, src_oa, ea, oti);
779                  RETURN(rc);
780         }
781
782         rc = lov_prep_create_set(exp, ea, src_oa, oti, &set);
783         if (rc)
784                 RETURN(rc);
785
786         list_for_each (pos, &set->set_list) {
787                 struct lov_request *req = 
788                         list_entry(pos, struct lov_request, rq_link);
789
790                 /* XXX: LOV STACKING: use real "obj_mdp" sub-data */
791                 rc = obd_create(lov->tgts[req->rq_idx].ltd_exp, 
792                                 req->rq_oa, &req->rq_md, oti);
793                 lov_update_create_set(set, req, rc);
794         }
795         rc = lov_fini_create_set(set, ea);
796         RETURN(rc);
797 }
798
799 #define lsm_bad_magic(LSMP)                                     \
800 ({                                                              \
801         struct lov_stripe_md *_lsm__ = (LSMP);                  \
802         int _ret__ = 0;                                         \
803         if (!_lsm__) {                                          \
804                 CERROR("LOV requires striping ea\n");           \
805                 _ret__ = 1;                                     \
806         } else if (_lsm__->lsm_magic != LOV_MAGIC) {            \
807                 CERROR("LOV striping magic bad %#x != %#x\n",   \
808                        _lsm__->lsm_magic, LOV_MAGIC);           \
809                 _ret__ = 1;                                     \
810         }                                                       \
811         _ret__;                                                 \
812 })
813
814 static int lov_destroy(struct obd_export *exp, struct obdo *oa,
815                        struct lov_stripe_md *lsm, struct obd_trans_info *oti)
816 {
817         struct lov_request_set *set;
818         struct lov_request *req;
819         struct list_head *pos;
820         struct lov_obd *lov;
821         int rc = 0;
822         ENTRY;
823
824         if (lsm_bad_magic(lsm))
825                 RETURN(-EINVAL);
826
827         if (!exp || !exp->exp_obd)
828                 RETURN(-ENODEV);
829
830         lov = &exp->exp_obd->u.lov;
831         rc = lov_prep_destroy_set(exp, oa, lsm, oti, &set);
832         if (rc)
833                 RETURN(rc);
834
835         list_for_each (pos, &set->set_list) {
836                 int err;
837                 req = list_entry(pos, struct lov_request, rq_link);
838
839                 /* XXX update the cookie position */
840                 oti->oti_logcookies = set->set_cookies + req->rq_stripe;
841                 rc = obd_destroy(lov->tgts[req->rq_idx].ltd_exp, req->rq_oa,
842                                  NULL, oti);
843                 err = lov_update_common_set(set, req, rc);
844                 if (rc) {
845                         CERROR("error: destroying objid "LPX64" subobj "
846                                LPX64" on OST idx %d: rc = %d\n", 
847                                set->set_oa->o_id, req->rq_oa->o_id, 
848                                req->rq_idx, rc);
849                         if (!rc)
850                                 rc = err;
851                 }
852         }
853         lov_fini_destroy_set(set);
854         RETURN(rc);
855 }
856
857 static int lov_getattr(struct obd_export *exp, struct obdo *oa,
858                        struct lov_stripe_md *lsm)
859 {
860         struct lov_request_set *set;
861         struct lov_request *req;
862         struct list_head *pos;
863         struct lov_obd *lov;
864         int err = 0, rc = 0;
865         ENTRY;
866
867         if (lsm_bad_magic(lsm))
868                 RETURN(-EINVAL);
869
870         if (!exp || !exp->exp_obd)
871                 RETURN(-ENODEV);
872
873         lov = &exp->exp_obd->u.lov;
874         
875         rc = lov_prep_getattr_set(exp, oa, lsm, &set);
876         if (rc)
877                 RETURN(rc);
878
879         list_for_each (pos, &set->set_list) {
880                 req = list_entry(pos, struct lov_request, rq_link);
881                 
882                 CDEBUG(D_INFO, "objid "LPX64"[%d] has subobj "LPX64" at idx "
883                        "%u\n", oa->o_id, req->rq_stripe, req->rq_oa->o_id, 
884                        req->rq_idx);
885
886                 rc = obd_getattr(lov->tgts[req->rq_idx].ltd_exp, 
887                                  req->rq_oa, NULL);
888                 err = lov_update_common_set(set, req, rc);
889                 if (err) {
890                         CERROR("error: getattr objid "LPX64" subobj "
891                                LPX64" on OST idx %d: rc = %d\n",
892                                set->set_oa->o_id, req->rq_oa->o_id, 
893                                req->rq_idx, err);
894                         break;
895                 }
896         }
897         
898         rc = lov_fini_getattr_set(set);
899         if (err)
900                 rc = err;
901         RETURN(rc);
902 }
903
904 static int lov_getattr_interpret(struct ptlrpc_request_set *rqset, void *data,
905                                  int rc)
906 {
907         struct lov_request_set *lovset = (struct lov_request_set *)data;
908         ENTRY;
909
910         /* don't do attribute merge if this aysnc op failed */
911         if (rc) {
912                 lovset->set_completes = 0;
913                 lov_fini_getattr_set(lovset);
914         } else {
915                 rc = lov_fini_getattr_set(lovset);
916         }
917         RETURN (rc);
918 }
919
920 static int lov_getattr_async(struct obd_export *exp, struct obdo *oa,
921                               struct lov_stripe_md *lsm,
922                               struct ptlrpc_request_set *rqset)
923 {
924         struct lov_request_set *lovset;
925         struct lov_obd *lov;
926         struct list_head *pos;
927         struct lov_request *req;
928         int rc = 0;
929         ENTRY;
930
931         if (lsm_bad_magic(lsm))
932                 RETURN(-EINVAL);
933
934         if (!exp || !exp->exp_obd)
935                 RETURN(-ENODEV);
936
937         lov = &exp->exp_obd->u.lov;
938
939         rc = lov_prep_getattr_set(exp, oa, lsm, &lovset);
940         if (rc)
941                 RETURN(rc);
942
943         CDEBUG(D_INFO, "objid "LPX64": %ux%u byte stripes\n",
944                lsm->lsm_object_id, lsm->lsm_stripe_count, lsm->lsm_stripe_size);
945
946         list_for_each (pos, &lovset->set_list) {
947                 req = list_entry(pos, struct lov_request, rq_link);
948                 
949                 CDEBUG(D_INFO, "objid "LPX64"[%d] has subobj "LPX64" at idx "
950                        "%u\n", oa->o_id, req->rq_stripe, req->rq_oa->o_id, 
951                        req->rq_idx);
952                 rc = obd_getattr_async(lov->tgts[req->rq_idx].ltd_exp,
953                                        req->rq_oa, NULL, rqset);
954                 if (rc) {
955                         CERROR("error: getattr objid "LPX64" subobj "
956                                LPX64" on OST idx %d: rc = %d\n",
957                                lovset->set_oa->o_id, req->rq_oa->o_id, 
958                                req->rq_idx, rc);
959                         GOTO(out, rc);
960                 }
961                 lov_update_common_set(lovset, req, rc);
962         }
963         
964         LASSERT(rc == 0);
965         LASSERT (rqset->set_interpret == NULL);
966         rqset->set_interpret = lov_getattr_interpret;
967         rqset->set_arg = (void *)lovset;
968         RETURN(rc);
969 out:
970         LASSERT(rc);
971         lov_fini_getattr_set(lovset);
972         RETURN(rc);
973 }
974
975 static int lov_setattr(struct obd_export *exp, struct obdo *src_oa,
976                        struct lov_stripe_md *lsm, struct obd_trans_info *oti)
977 {
978         struct lov_request_set *set;
979         struct lov_obd *lov;
980         struct list_head *pos;
981         struct lov_request *req;
982         int err = 0, rc = 0;
983         ENTRY;
984
985         if (lsm_bad_magic(lsm))
986                 RETURN(-EINVAL);
987
988         if (!exp || !exp->exp_obd)
989                 RETURN(-ENODEV);
990
991         /* for now, we only expect time updates here */
992         LASSERT(!(src_oa->o_valid & ~(OBD_MD_FLID|OBD_MD_FLTYPE | OBD_MD_FLMODE|
993                                       OBD_MD_FLATIME | OBD_MD_FLMTIME |
994                                       OBD_MD_FLCTIME | OBD_MD_FLFLAGS |
995                                       OBD_MD_FLSIZE | OBD_MD_FLGROUP)));
996
997         LASSERT(!(src_oa->o_valid & OBD_MD_FLGROUP) || src_oa->o_gr > 0);
998
999         lov = &exp->exp_obd->u.lov;
1000         rc = lov_prep_setattr_set(exp, src_oa, lsm, NULL, &set);
1001         if (rc)
1002                 RETURN(rc);
1003
1004         list_for_each (pos, &set->set_list) {
1005                 req = list_entry(pos, struct lov_request, rq_link);
1006                 
1007                 rc = obd_setattr(lov->tgts[req->rq_idx].ltd_exp, req->rq_oa,
1008                                  NULL, NULL);
1009                 err = lov_update_common_set(set, req, rc);
1010                 if (err) {
1011                         CERROR("error: setattr objid "LPX64" subobj "
1012                                LPX64" on OST idx %d: rc = %d\n",
1013                                set->set_oa->o_id, req->rq_oa->o_id,
1014                                req->rq_idx, err);
1015                         if (!rc)
1016                                 rc = err;
1017                 }
1018         }
1019         err = lov_fini_setattr_set(set);
1020         if (!rc)
1021                 rc = err;
1022         RETURN(rc);
1023 }
1024
1025 static int lov_revalidate_policy(struct lov_obd *lov, struct lov_stripe_md *lsm)
1026 {
1027         static int next_idx = 0;
1028         struct lov_tgt_desc *tgt;
1029         int i, count;
1030
1031         /* XXX - we should do something clever and take lsm
1032          * into account but just do round robin for now. */
1033
1034         /* last_idx must always be less that count because
1035          * ld_tgt_count currently cannot shrink. */
1036         count = lov->desc.ld_tgt_count;
1037
1038         for (i = next_idx, tgt = lov->tgts + i; i < count; i++, tgt++) {
1039                 if (tgt->active) {
1040                         next_idx = (i + 1) % count;
1041                         RETURN(i);
1042                 }
1043         }
1044
1045         for (i = 0, tgt = lov->tgts; i < next_idx; i++, tgt++) {
1046                 if (tgt->active) {
1047                         next_idx = (i + 1) % count;
1048                         RETURN(i);
1049                 }
1050         }
1051
1052         RETURN(-EIO);
1053 }
1054
1055 static int lov_revalidate_md(struct obd_export *exp, struct obdo *src_oa,
1056                              struct lov_stripe_md *ea,
1057                              struct obd_trans_info *oti)
1058 {
1059         struct obd_export *osc_exp;
1060         struct lov_obd *lov = &exp->exp_obd->u.lov;
1061         struct lov_stripe_md *lsm = ea;
1062         struct lov_stripe_md obj_md;
1063         struct lov_stripe_md *obj_mdp = &obj_md;
1064         struct lov_oinfo *loi;
1065         struct obdo *tmp_oa;
1066         int ost_idx, updates = 0, i;
1067         ENTRY;
1068
1069         tmp_oa = obdo_alloc();
1070         if (tmp_oa == NULL)
1071                 RETURN(-ENOMEM);
1072
1073         loi = lsm->lsm_oinfo;
1074         for (i = 0; i < lsm->lsm_stripe_count; i++, loi++) {
1075                 int rc;
1076                 if (!obd_uuid_empty(&lov->tgts[loi->loi_ost_idx].uuid))
1077                         continue;
1078
1079                 ost_idx = lov_revalidate_policy(lov, lsm);
1080                 if (ost_idx < 0) {
1081                         /* FIXME: punt for now. */
1082                         CERROR("lov_revalidate_policy failed; no active "
1083                                "OSCs?\n");
1084                         continue;
1085                 }
1086
1087                 /* create a new object */
1088                 memcpy(tmp_oa, src_oa, sizeof(*tmp_oa));
1089                 /* XXX: LOV STACKING: use real "obj_mdp" sub-data */
1090                 osc_exp = lov->tgts[ost_idx].ltd_exp;
1091                 rc = obd_create(osc_exp, tmp_oa, &obj_mdp, oti);
1092                 if (rc) {
1093                         CERROR("error creating new subobj at idx %d; "
1094                                "rc = %d\n", ost_idx, rc);
1095                         continue;
1096                 }
1097                 if (oti->oti_objid)
1098                         oti->oti_objid[ost_idx] = tmp_oa->o_id;
1099                 loi->loi_id = tmp_oa->o_id;
1100                 loi->loi_gr = tmp_oa->o_gr;
1101                 loi->loi_ost_idx = ost_idx;
1102                 loi->loi_ost_gen = lov->tgts[ost_idx].ltd_gen;
1103                 CDEBUG(D_INODE, "replacing objid "LPX64" subobj "LPX64
1104                        " with idx %d gen %d.\n", lsm->lsm_object_id,
1105                        loi->loi_id, ost_idx, loi->loi_ost_gen);
1106                 updates = 1;
1107         }
1108
1109         /* If we got an error revalidating an entry there's no need to
1110          * cleanup up objects we allocated here because the bad entry
1111          * still points to a deleted OST. */
1112
1113         obdo_free(tmp_oa);
1114         RETURN(updates);
1115 }
1116
1117 /* FIXME: maybe we'll just make one node the authoritative attribute node, then
1118  * we can send this 'punch' to just the authoritative node and the nodes
1119  * that the punch will affect. */
1120 static int lov_punch(struct obd_export *exp, struct obdo *oa,
1121                      struct lov_stripe_md *lsm,
1122                      obd_off start, obd_off end, struct obd_trans_info *oti)
1123 {
1124         struct lov_request_set *set;
1125         struct lov_obd *lov;
1126         struct list_head *pos;
1127         struct lov_request *req;
1128         int err = 0, rc = 0;
1129         ENTRY;
1130
1131         if (lsm_bad_magic(lsm))
1132                 RETURN(-EINVAL);
1133
1134         if (!exp || !exp->exp_obd)
1135                 RETURN(-ENODEV);
1136
1137         lov = &exp->exp_obd->u.lov;
1138         rc = lov_prep_punch_set(exp, oa, lsm, start, end, oti, &set);
1139         if (rc)
1140                 RETURN(rc);
1141
1142         list_for_each (pos, &set->set_list) {
1143                 req = list_entry(pos, struct lov_request, rq_link);
1144
1145                 rc = obd_punch(lov->tgts[req->rq_idx].ltd_exp, req->rq_oa, 
1146                                NULL, req->rq_extent.start, 
1147                                req->rq_extent.end, NULL);
1148                 err = lov_update_punch_set(set, req, rc);
1149                 if (err) {
1150                         CERROR("error: punch objid "LPX64" subobj "LPX64
1151                                " on OST idx %d: rc = %d\n", set->set_oa->o_id,
1152                                req->rq_oa->o_id, req->rq_idx, rc);
1153                         if (!rc)
1154                                 rc = err;
1155                 }
1156         }
1157         err = lov_fini_punch_set(set);
1158         if (!rc)
1159                 rc = err;
1160         RETURN(rc);
1161 }
1162
1163 static int lov_sync(struct obd_export *exp, struct obdo *oa,
1164                     struct lov_stripe_md *lsm, obd_off start, obd_off end)
1165 {
1166         struct lov_request_set *set;
1167         struct lov_obd *lov;
1168         struct list_head *pos;
1169         struct lov_request *req;
1170         int err = 0, rc = 0;
1171         ENTRY;
1172
1173         if (lsm_bad_magic(lsm))
1174                 RETURN(-EINVAL);
1175
1176         if (!exp->exp_obd)
1177                 RETURN(-ENODEV);
1178
1179         lov = &exp->exp_obd->u.lov;
1180         rc = lov_prep_sync_set(exp, oa, lsm, start, end, &set);
1181         if (rc)
1182                 RETURN(rc);
1183
1184         list_for_each (pos, &set->set_list) {
1185                 req = list_entry(pos, struct lov_request, rq_link);
1186
1187                 rc = obd_sync(lov->tgts[req->rq_idx].ltd_exp, req->rq_oa, 
1188                               NULL, req->rq_extent.start, req->rq_extent.end);
1189                 err = lov_update_common_set(set, req, rc);
1190                 if (err) {
1191                         CERROR("error: fsync objid "LPX64" subobj "LPX64
1192                                " on OST idx %d: rc = %d\n", set->set_oa->o_id,
1193                                req->rq_oa->o_id, req->rq_idx, rc);
1194                         if (!rc)
1195                                 rc = err;
1196                 }
1197         }
1198         err = lov_fini_sync_set(set);
1199         if (!rc)
1200                 rc = err;
1201         RETURN(rc);
1202 }
1203
1204 static int lov_brw_check(struct lov_obd *lov, struct obdo *oa,
1205                          struct lov_stripe_md *lsm,
1206                          obd_count oa_bufs, struct brw_page *pga)
1207 {
1208         int i, rc = 0;
1209         ENTRY;
1210
1211         /* The caller just wants to know if there's a chance that this
1212          * I/O can succeed */
1213         for (i = 0; i < oa_bufs; i++) {
1214                 int stripe = lov_stripe_number(lsm, pga[i].disk_offset);
1215                 int ost = lsm->lsm_oinfo[stripe].loi_ost_idx;
1216                 obd_off start, end;
1217
1218                 if (!lov_stripe_intersects(lsm, i, pga[i].disk_offset,
1219                                            pga[i].disk_offset + pga[i].count,
1220                                            &start, &end))
1221                         continue;
1222
1223                 if (lov->tgts[ost].active == 0) {
1224                         CDEBUG(D_HA, "lov idx %d inactive\n", ost);
1225                         RETURN(-EIO);
1226                 }
1227                 rc = obd_brw(OBD_BRW_CHECK, lov->tgts[ost].ltd_exp, oa,
1228                              NULL, 1, &pga[i], NULL);
1229                 if (rc)
1230                         break;
1231         }
1232         RETURN(rc);
1233 }
1234
1235 static int lov_brw(int cmd, struct obd_export *exp, struct obdo *src_oa,
1236                    struct lov_stripe_md *lsm, obd_count oa_bufs,
1237                    struct brw_page *pga, struct obd_trans_info *oti)
1238 {
1239         struct lov_request_set *set;
1240         struct lov_request *req;
1241         struct list_head *pos;
1242         struct lov_obd *lov = &exp->exp_obd->u.lov;
1243         int err, rc = 0;
1244         ENTRY;
1245
1246         if (lsm_bad_magic(lsm))
1247                 RETURN(-EINVAL);
1248
1249         if (cmd == OBD_BRW_CHECK) {
1250                 rc = lov_brw_check(lov, src_oa, lsm, oa_bufs, pga);
1251                 RETURN(rc);
1252         }
1253
1254         rc = lov_prep_brw_set(exp, src_oa, lsm, oa_bufs, pga, oti, &set);
1255         if (rc)
1256                 RETURN(rc);
1257
1258         list_for_each (pos, &set->set_list) {
1259                 struct obd_export *sub_exp;
1260                 struct brw_page *sub_pga;
1261                 req = list_entry(pos, struct lov_request, rq_link);
1262                 
1263                 sub_exp = lov->tgts[req->rq_idx].ltd_exp;
1264                 sub_pga = set->set_pga + req->rq_pgaidx;
1265                 rc = obd_brw(cmd, sub_exp, req->rq_oa, req->rq_md, 
1266                              req->rq_oabufs, sub_pga, oti);
1267                 if (rc)
1268                         break;
1269                 lov_update_common_set(set, req, rc);
1270         }
1271
1272         err = lov_fini_brw_set(set);
1273         if (!rc)
1274                 rc = err;
1275         RETURN(rc);
1276 }
1277
1278 static int lov_brw_interpret(struct ptlrpc_request_set *reqset, void *data,
1279                              int rc)
1280 {
1281         struct lov_request_set *lovset = (struct lov_request_set *)data;
1282         ENTRY;
1283         
1284         if (rc) {
1285                 lovset->set_completes = 0;
1286                 lov_fini_brw_set(lovset);
1287         } else {
1288                 rc = lov_fini_brw_set(lovset);
1289         }
1290                 
1291         RETURN(rc);
1292 }
1293
1294 static int lov_brw_async(int cmd, struct obd_export *exp, struct obdo *oa,
1295                          struct lov_stripe_md *lsm, obd_count oa_bufs,
1296                          struct brw_page *pga, struct ptlrpc_request_set *set,
1297                          struct obd_trans_info *oti)
1298 {
1299         struct lov_request_set *lovset;
1300         struct lov_request *req;
1301         struct list_head *pos;
1302         struct lov_obd *lov = &exp->exp_obd->u.lov;
1303         int rc = 0;
1304         ENTRY;
1305
1306         if (lsm_bad_magic(lsm))
1307                 RETURN(-EINVAL);
1308
1309         if (cmd == OBD_BRW_CHECK) {
1310                 rc = lov_brw_check(lov, oa, lsm, oa_bufs, pga);
1311                 RETURN(rc);
1312         }
1313
1314         rc = lov_prep_brw_set(exp, oa, lsm, oa_bufs, pga, oti, &lovset);
1315         if (rc)
1316                 RETURN(rc);
1317
1318         list_for_each (pos, &lovset->set_list) {
1319                 struct obd_export *sub_exp;
1320                 struct brw_page *sub_pga;
1321                 req = list_entry(pos, struct lov_request, rq_link);
1322                 
1323                 sub_exp = lov->tgts[req->rq_idx].ltd_exp;
1324                 sub_pga = lovset->set_pga + req->rq_pgaidx;
1325                 rc = obd_brw_async(cmd, sub_exp, req->rq_oa, req->rq_md,
1326                                    req->rq_oabufs, sub_pga, set, oti);
1327                 if (rc)
1328                         GOTO(out, rc);
1329                 lov_update_common_set(lovset, req, rc);
1330         }
1331         LASSERT(rc == 0);
1332         LASSERT(set->set_interpret == NULL);
1333         set->set_interpret = (set_interpreter_func)lov_brw_interpret;
1334         set->set_arg = (void *)lovset;
1335         
1336         RETURN(rc);
1337 out:
1338         lov_fini_brw_set(lovset);
1339         RETURN(rc);
1340 }
1341
1342 static int lov_ap_make_ready(void *data, int cmd)
1343 {
1344         struct lov_async_page *lap = LAP_FROM_COOKIE(data);
1345
1346         return lap->lap_caller_ops->ap_make_ready(lap->lap_caller_data, cmd);
1347 }
1348 static int lov_ap_refresh_count(void *data, int cmd)
1349 {
1350         struct lov_async_page *lap = LAP_FROM_COOKIE(data);
1351
1352         return lap->lap_caller_ops->ap_refresh_count(lap->lap_caller_data,
1353                                                      cmd);
1354 }
1355 static void lov_ap_fill_obdo(void *data, int cmd, struct obdo *oa)
1356 {
1357         struct lov_async_page *lap = LAP_FROM_COOKIE(data);
1358
1359         lap->lap_caller_ops->ap_fill_obdo(lap->lap_caller_data, cmd, oa);
1360         /* XXX woah, shouldn't we be altering more here?  size? */
1361         oa->o_id = lap->lap_loi_id;
1362 }
1363
1364 static void lov_ap_completion(void *data, int cmd, struct obdo *oa, int rc)
1365 {
1366         struct lov_async_page *lap = LAP_FROM_COOKIE(data);
1367
1368         /* in a raid1 regime this would down a count of many ios
1369          * in flight, onl calling the caller_ops completion when all
1370          * the raid1 ios are complete */
1371         lap->lap_caller_ops->ap_completion(lap->lap_caller_data, cmd, oa, rc);
1372 }
1373
1374 static struct obd_async_page_ops lov_async_page_ops = {
1375         .ap_make_ready =        lov_ap_make_ready,
1376         .ap_refresh_count =     lov_ap_refresh_count,
1377         .ap_fill_obdo =         lov_ap_fill_obdo,
1378         .ap_completion =        lov_ap_completion,
1379 };
1380
1381 static int lov_prep_async_page(struct obd_export *exp,
1382                                struct lov_stripe_md *lsm,
1383                                struct lov_oinfo *loi, struct page *page,
1384                                obd_off offset, struct obd_async_page_ops *ops,
1385                                void *data, void **res)
1386 {
1387         struct lov_obd *lov = &exp->exp_obd->u.lov;
1388         struct lov_async_page *lap;
1389         int rc, stripe;
1390         ENTRY;
1391
1392         if (lsm_bad_magic(lsm))
1393                 RETURN(-EINVAL);
1394         LASSERT(loi == NULL);
1395
1396         stripe = lov_stripe_number(lsm, offset);
1397         loi = &lsm->lsm_oinfo[stripe];
1398
1399         if (obd_uuid_empty(&lov->tgts[loi->loi_ost_idx].uuid))
1400                 RETURN(-EIO);
1401         if (lov->tgts[loi->loi_ost_idx].active == 0)
1402                 RETURN(-EIO);
1403         if (lov->tgts[loi->loi_ost_idx].ltd_exp == NULL) {
1404                 CERROR("ltd_exp == NULL, but OST idx %d doesn't appear to be "
1405                        "deleted or inactive.\n", loi->loi_ost_idx);
1406                 RETURN(-EIO);
1407         }
1408
1409         OBD_ALLOC(lap, sizeof(*lap));
1410         if (lap == NULL)
1411                 RETURN(-ENOMEM);
1412
1413         lap->lap_magic = LAP_MAGIC;
1414         lap->lap_caller_ops = ops;
1415         lap->lap_caller_data = data;
1416
1417         /* FIXME handle multiple oscs after landing b_raid1 */
1418         lap->lap_stripe = stripe;
1419         switch (lsm->lsm_pattern) {
1420                 case LOV_PATTERN_RAID0:
1421                         lov_stripe_offset(lsm, offset, lap->lap_stripe, 
1422                                           &lap->lap_sub_offset);
1423                         break;
1424                 case LOV_PATTERN_CMOBD:
1425                         lap->lap_sub_offset = offset;
1426                         break;
1427                 default:
1428                         LBUG();
1429         }
1430
1431         /* so the callback doesn't need the lsm */
1432         lap->lap_loi_id = loi->loi_id;
1433
1434         rc = obd_prep_async_page(lov->tgts[loi->loi_ost_idx].ltd_exp,
1435                                  lsm, loi, page, lap->lap_sub_offset,
1436                                  &lov_async_page_ops, lap,
1437                                  &lap->lap_sub_cookie);
1438         if (rc) {
1439                 OBD_FREE(lap, sizeof(*lap));
1440                 RETURN(rc);
1441         }
1442         CDEBUG(D_CACHE, "lap %p page %p cookie %p off "LPU64"\n", lap, page,
1443                lap->lap_sub_cookie, offset);
1444         *res = lap;
1445         RETURN(0);
1446 }
1447
1448 static int lov_queue_async_io(struct obd_export *exp,
1449                               struct lov_stripe_md *lsm,
1450                               struct lov_oinfo *loi, void *cookie,
1451                               int cmd, obd_off off, int count,
1452                               obd_flags brw_flags, obd_flags async_flags)
1453 {
1454         struct lov_obd *lov = &exp->exp_obd->u.lov;
1455         struct lov_async_page *lap;
1456         int rc;
1457
1458         LASSERT(loi == NULL);
1459
1460         if (lsm_bad_magic(lsm))
1461                 RETURN(-EINVAL);
1462
1463         lap = LAP_FROM_COOKIE(cookie);
1464
1465         loi = &lsm->lsm_oinfo[lap->lap_stripe];
1466
1467         rc = obd_queue_async_io(lov->tgts[loi->loi_ost_idx].ltd_exp, lsm,
1468                                 loi, lap->lap_sub_cookie, cmd, off, count,
1469                                 brw_flags, async_flags);
1470         RETURN(rc);
1471 }
1472
1473 static int lov_set_async_flags(struct obd_export *exp,
1474                                struct lov_stripe_md *lsm,
1475                                struct lov_oinfo *loi, void *cookie,
1476                                obd_flags async_flags)
1477 {
1478         struct lov_obd *lov = &exp->exp_obd->u.lov;
1479         struct lov_async_page *lap;
1480         int rc;
1481
1482         LASSERT(loi == NULL);
1483
1484         if (lsm_bad_magic(lsm))
1485                 RETURN(-EINVAL);
1486
1487         lap = LAP_FROM_COOKIE(cookie);
1488
1489         loi = &lsm->lsm_oinfo[lap->lap_stripe];
1490
1491         rc = obd_set_async_flags(lov->tgts[loi->loi_ost_idx].ltd_exp,
1492                                  lsm, loi, lap->lap_sub_cookie, async_flags);
1493         RETURN(rc);
1494 }
1495
1496 static int lov_queue_group_io(struct obd_export *exp,
1497                               struct lov_stripe_md *lsm,
1498                               struct lov_oinfo *loi,
1499                               struct obd_io_group *oig, void *cookie,
1500                               int cmd, obd_off off, int count,
1501                               obd_flags brw_flags, obd_flags async_flags)
1502 {
1503         struct lov_obd *lov = &exp->exp_obd->u.lov;
1504         struct lov_async_page *lap;
1505         int rc;
1506
1507         LASSERT(loi == NULL);
1508
1509         if (lsm_bad_magic(lsm))
1510                 RETURN(-EINVAL);
1511
1512         lap = LAP_FROM_COOKIE(cookie);
1513
1514         loi = &lsm->lsm_oinfo[lap->lap_stripe];
1515
1516         rc = obd_queue_group_io(lov->tgts[loi->loi_ost_idx].ltd_exp, lsm, loi,
1517                                 oig, lap->lap_sub_cookie, cmd, off, count,
1518                                 brw_flags, async_flags);
1519         RETURN(rc);
1520 }
1521
1522 /* this isn't exactly optimal.  we may have queued sync io in oscs on
1523  * all stripes, but we don't record that fact at queue time.  so we
1524  * trigger sync io on all stripes. */
1525 static int lov_trigger_group_io(struct obd_export *exp,
1526                                 struct lov_stripe_md *lsm,
1527                                 struct lov_oinfo *loi,
1528                                 struct obd_io_group *oig)
1529 {
1530         struct lov_obd *lov = &exp->exp_obd->u.lov;
1531         int rc = 0, i, err;
1532
1533         LASSERT(loi == NULL);
1534
1535         if (lsm_bad_magic(lsm))
1536                 RETURN(-EINVAL);
1537
1538         loi = lsm->lsm_oinfo;
1539         for (i = 0; i < lsm->lsm_stripe_count; i++, loi++) {
1540                 if (lov->tgts[loi->loi_ost_idx].active == 0) {
1541                         CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
1542                         continue;
1543                 }
1544
1545                 err = obd_trigger_group_io(lov->tgts[loi->loi_ost_idx].ltd_exp,
1546                                            lsm, loi, oig);
1547                 if (rc == 0 && err != 0)
1548                         rc = err;
1549         };
1550         RETURN(rc);
1551 }
1552
1553 static int lov_teardown_async_page(struct obd_export *exp,
1554                                    struct lov_stripe_md *lsm,
1555                                    struct lov_oinfo *loi, void *cookie)
1556 {
1557         struct lov_obd *lov = &exp->exp_obd->u.lov;
1558         struct lov_async_page *lap;
1559         int rc;
1560
1561         LASSERT(loi == NULL);
1562
1563         if (lsm_bad_magic(lsm))
1564                 RETURN(-EINVAL);
1565
1566         lap = LAP_FROM_COOKIE(cookie);
1567
1568         loi = &lsm->lsm_oinfo[lap->lap_stripe];
1569
1570         rc = obd_teardown_async_page(lov->tgts[loi->loi_ost_idx].ltd_exp,
1571                                      lsm, loi, lap->lap_sub_cookie);
1572         if (rc) {
1573                 CERROR("unable to teardown sub cookie %p: %d\n",
1574                        lap->lap_sub_cookie, rc);
1575                 RETURN(rc);
1576         }
1577         OBD_FREE(lap, sizeof(*lap));
1578         RETURN(rc);
1579 }
1580
1581 static int lov_enqueue(struct obd_export *exp, struct lov_stripe_md *lsm,
1582                        __u32 type, ldlm_policy_data_t *policy, __u32 mode,
1583                        int *flags, void *bl_cb, void *cp_cb, void *gl_cb,
1584                        void *data,__u32 lvb_len, void *lvb_swabber,
1585                        struct lustre_handle *lockh)
1586 {
1587         struct lov_request_set *set;
1588         struct lov_request *req;
1589         struct list_head *pos;
1590         struct lustre_handle *lov_lockhp;
1591         struct lov_obd *lov;
1592         ldlm_error_t rc;
1593         int save_flags = *flags;
1594         ENTRY;
1595
1596         if (lsm_bad_magic(lsm))
1597                 RETURN(-EINVAL);
1598
1599         /* we should never be asked to replay a lock this way. */
1600         LASSERT((*flags & LDLM_FL_REPLAY) == 0);
1601
1602         if (!exp || !exp->exp_obd)
1603                 RETURN(-ENODEV);
1604
1605         lov = &exp->exp_obd->u.lov;
1606         rc = lov_prep_enqueue_set(exp, lsm, policy, mode, lockh, &set);
1607         if (rc)
1608                 RETURN(rc);
1609
1610         list_for_each (pos, &set->set_list) {
1611                 ldlm_policy_data_t sub_policy;
1612                 req = list_entry(pos, struct lov_request, rq_link);
1613                 lov_lockhp = set->set_lockh->llh_handles + req->rq_stripe;
1614                 LASSERT(lov_lockhp);
1615
1616                 *flags = save_flags;
1617                 sub_policy.l_extent.start = req->rq_extent.start;
1618                 sub_policy.l_extent.end = req->rq_extent.end;
1619
1620                 rc = obd_enqueue(lov->tgts[req->rq_idx].ltd_exp, req->rq_md,
1621                                  type, &sub_policy, mode, flags, bl_cb,
1622                                  cp_cb, gl_cb, data, lvb_len, lvb_swabber,
1623                                  lov_lockhp);
1624                 rc = lov_update_enqueue_set(set, req, rc, save_flags);
1625                 if (rc != ELDLM_OK)
1626                         break;
1627         }
1628
1629         lov_fini_enqueue_set(set, mode);
1630         RETURN(rc);
1631 }
1632
1633 static int lov_match(struct obd_export *exp, struct lov_stripe_md *lsm,
1634                      __u32 type, ldlm_policy_data_t *policy, __u32 mode,
1635                      int *flags, void *data, struct lustre_handle *lockh)
1636 {
1637         struct lov_request_set *set;
1638         struct lov_request *req;
1639         struct list_head *pos;
1640         struct lov_obd *lov = &exp->exp_obd->u.lov;
1641         struct lustre_handle *lov_lockhp;
1642         int lov_flags, rc = 0;
1643         ENTRY;
1644
1645         if (lsm_bad_magic(lsm))
1646                 RETURN(-EINVAL);
1647
1648         if (!exp || !exp->exp_obd)
1649                 RETURN(-ENODEV);
1650
1651         lov = &exp->exp_obd->u.lov;
1652         rc = lov_prep_match_set(exp, lsm, policy, mode, lockh, &set);
1653         if (rc)
1654                 RETURN(rc);
1655
1656         list_for_each (pos, &set->set_list) {
1657                 ldlm_policy_data_t sub_policy;
1658                 req = list_entry(pos, struct lov_request, rq_link);
1659                 lov_lockhp = set->set_lockh->llh_handles + req->rq_stripe;
1660                 LASSERT(lov_lockhp);
1661
1662                 sub_policy.l_extent.start = req->rq_extent.start;
1663                 sub_policy.l_extent.end = req->rq_extent.end;
1664                 lov_flags = *flags;
1665
1666                 rc = obd_match(lov->tgts[req->rq_idx].ltd_exp, req->rq_md,
1667                                type, &sub_policy, mode, &lov_flags, data,
1668                                lov_lockhp);
1669                 rc = lov_update_match_set(set, req, rc);
1670                 if (rc != 1)
1671                         break;
1672         }
1673         lov_fini_match_set(set, mode, *flags);
1674         RETURN(rc);
1675 }
1676
1677 static int lov_change_cbdata(struct obd_export *exp,
1678                              struct lov_stripe_md *lsm, ldlm_iterator_t it,
1679                              void *data)
1680 {
1681         struct lov_obd *lov;
1682         struct lov_oinfo *loi;
1683         int rc = 0, i;
1684         ENTRY;
1685
1686         if (lsm_bad_magic(lsm))
1687                 RETURN(-EINVAL);
1688
1689         if (!exp || !exp->exp_obd)
1690                 RETURN(-ENODEV);
1691
1692         LASSERT(lsm->lsm_object_gr > 0);
1693
1694         lov = &exp->exp_obd->u.lov;
1695         for (i = 0,loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++,loi++) {
1696                 struct lov_stripe_md submd;
1697                 if (lov->tgts[loi->loi_ost_idx].active == 0) {
1698                         CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
1699                         continue;
1700                 }
1701
1702                 submd.lsm_object_id = loi->loi_id;
1703                 submd.lsm_object_gr = lsm->lsm_object_gr;
1704                 submd.lsm_stripe_count = 0;
1705                 rc = obd_change_cbdata(lov->tgts[loi->loi_ost_idx].ltd_exp,
1706                                        &submd, it, data);
1707         }
1708         RETURN(rc);
1709 }
1710
1711 static int lov_cancel(struct obd_export *exp, struct lov_stripe_md *lsm,
1712                       __u32 mode, struct lustre_handle *lockh)
1713 {
1714         struct lov_request_set *set;
1715         struct lov_request *req;
1716         struct list_head *pos;
1717         struct lov_obd *lov = &exp->exp_obd->u.lov;
1718         struct lustre_handle *lov_lockhp;
1719         int err = 0, rc = 0;
1720         ENTRY;
1721
1722         if (lsm_bad_magic(lsm))
1723                 RETURN(-EINVAL);
1724
1725         if (!exp || !exp->exp_obd)
1726                 RETURN(-ENODEV);
1727
1728         LASSERT(lsm->lsm_object_gr > 0);
1729
1730         LASSERT(lockh);
1731         lov = &exp->exp_obd->u.lov;
1732         rc = lov_prep_cancel_set(exp, lsm, mode, lockh, &set);
1733         if (rc)
1734                 RETURN(rc);
1735
1736         list_for_each (pos, &set->set_list) {
1737                 req = list_entry(pos, struct lov_request, rq_link);
1738                 lov_lockhp = set->set_lockh->llh_handles + req->rq_stripe;
1739
1740                 rc = obd_cancel(lov->tgts[req->rq_idx].ltd_exp, req->rq_md,
1741                                 mode, lov_lockhp);
1742                 rc = lov_update_common_set(set, req, rc);
1743                 if (rc) {
1744                         CERROR("error: cancel objid "LPX64" subobj "
1745                                LPX64" on OST idx %d: rc = %d\n",
1746                                lsm->lsm_object_id,
1747                                req->rq_md->lsm_object_id, req->rq_idx, rc);
1748                         err = rc;
1749                 }
1750  
1751         }
1752         lov_fini_cancel_set(set);
1753         RETURN(err);
1754 }
1755
1756 static int lov_cancel_unused(struct obd_export *exp,
1757                              struct lov_stripe_md *lsm, 
1758                              int flags, void *opaque)
1759 {
1760         struct lov_obd *lov;
1761         struct lov_oinfo *loi;
1762         int rc = 0, i;
1763         ENTRY;
1764
1765         lov = &exp->exp_obd->u.lov;
1766         if (lsm == NULL) {
1767                 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
1768                         int err = obd_cancel_unused(lov->tgts[i].ltd_exp, NULL,
1769                                                     flags, opaque);
1770                         if (!rc)
1771                                 rc = err;
1772                 }
1773                 RETURN(rc);
1774         }
1775
1776         if (lsm_bad_magic(lsm))
1777                 RETURN(-EINVAL);
1778
1779         if (!exp || !exp->exp_obd)
1780                 RETURN(-ENODEV);
1781
1782         LASSERT(lsm->lsm_object_gr > 0);
1783
1784         for (i = 0,loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++,loi++) {
1785                 struct lov_stripe_md submd;
1786                 int err;
1787
1788                 if (lov->tgts[loi->loi_ost_idx].active == 0)
1789                         CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
1790
1791                 submd.lsm_object_id = loi->loi_id;
1792                 submd.lsm_object_gr = lsm->lsm_object_gr;
1793                 submd.lsm_stripe_count = 0;
1794                 err = obd_cancel_unused(lov->tgts[loi->loi_ost_idx].ltd_exp,
1795                                         &submd, flags, opaque);
1796                 if (err && lov->tgts[loi->loi_ost_idx].active) {
1797                         CERROR("error: cancel unused objid "LPX64" subobj "LPX64
1798                                " on OST idx %d: rc = %d\n", lsm->lsm_object_id,
1799                                loi->loi_id, loi->loi_ost_idx, err);
1800                         if (!rc)
1801                                 rc = err;
1802                 }
1803         }
1804         RETURN(rc);
1805 }
1806
1807 #define LOV_U64_MAX ((__u64)~0ULL)
1808 #define LOV_SUM_MAX(tot, add)                                           \
1809         do {                                                            \
1810                 if ((tot) + (add) < (tot))                              \
1811                         (tot) = LOV_U64_MAX;                            \
1812                 else                                                    \
1813                         (tot) += (add);                                 \
1814         } while(0)
1815
1816 static int lov_statfs(struct obd_device *obd, struct obd_statfs *osfs,
1817                       unsigned long max_age)
1818 {
1819         struct lov_obd *lov = &obd->u.lov;
1820         struct obd_statfs lov_sfs;
1821         int set = 0;
1822         int rc = 0;
1823         int i;
1824         ENTRY;
1825
1826
1827         /* We only get block data from the OBD */
1828         for (i = 0; i < lov->desc.ld_tgt_count; i++) {
1829                 int err;
1830                 if (!lov->tgts[i].active) {
1831                         CDEBUG(D_HA, "lov idx %d inactive\n", i);
1832                         continue;
1833                 }
1834
1835                 err = obd_statfs(class_exp2obd(lov->tgts[i].ltd_exp), &lov_sfs,
1836                                  max_age);
1837                 if (err) {
1838                         if (lov->tgts[i].active && !rc)
1839                                 rc = err;
1840                         continue;
1841                 }
1842
1843                 if (!set) {
1844                         memcpy(osfs, &lov_sfs, sizeof(lov_sfs));
1845                         set = 1;
1846                 } else {
1847                         osfs->os_bfree += lov_sfs.os_bfree;
1848                         osfs->os_bavail += lov_sfs.os_bavail;
1849                         osfs->os_blocks += lov_sfs.os_blocks;
1850                         /* XXX not sure about this one - depends on policy.
1851                          *   - could be minimum if we always stripe on all OBDs
1852                          *     (but that would be wrong for any other policy,
1853                          *     if one of the OBDs has no more objects left)
1854                          *   - could be sum if we stripe whole objects
1855                          *   - could be average, just to give a nice number
1856                          *
1857                          * To give a "reasonable" (if not wholly accurate)
1858                          * number, we divide the total number of free objects
1859                          * by expected stripe count (watch out for overflow).
1860                          */
1861                         LOV_SUM_MAX(osfs->os_files, lov_sfs.os_files);
1862                         LOV_SUM_MAX(osfs->os_ffree, lov_sfs.os_ffree);
1863                 }
1864         }
1865
1866         if (set) {
1867                 __u32 expected_stripes = lov->desc.ld_default_stripe_count ?
1868                                          lov->desc.ld_default_stripe_count :
1869                                          lov->desc.ld_active_tgt_count;
1870
1871                 if (osfs->os_files != LOV_U64_MAX)
1872                         do_div(osfs->os_files, expected_stripes);
1873                 if (osfs->os_ffree != LOV_U64_MAX)
1874                         do_div(osfs->os_ffree, expected_stripes);
1875         } else if (!rc)
1876                 rc = -EIO;
1877
1878         RETURN(rc);
1879 }
1880
1881 static int lov_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1882                          void *karg, void *uarg)
1883 {
1884         struct obd_device *obddev = class_exp2obd(exp);
1885         struct lov_obd *lov = &obddev->u.lov;
1886         int i, rc, count = lov->desc.ld_tgt_count;
1887         struct obd_uuid *uuidp;
1888         ENTRY;
1889
1890         switch (cmd) {
1891         case OBD_IOC_LOV_GET_CONFIG: {
1892                 struct obd_ioctl_data *data = karg;
1893                 struct lov_tgt_desc *tgtdesc;
1894                 struct lov_desc *desc;
1895                 char *buf = NULL;
1896                 __u32 *genp;
1897
1898                 buf = NULL;
1899                 len = 0;
1900                 if (obd_ioctl_getdata(&buf, &len, (void *)uarg))
1901                         RETURN(-EINVAL);
1902
1903                 data = (struct obd_ioctl_data *)buf;
1904
1905                 if (sizeof(*desc) > data->ioc_inllen1) {
1906                         obd_ioctl_freedata(buf, len);
1907                         RETURN(-EINVAL);
1908                 }
1909
1910                 if (sizeof(uuidp->uuid) * count > data->ioc_inllen2) {
1911                         obd_ioctl_freedata(buf, len);
1912                         RETURN(-EINVAL);
1913                 }
1914
1915                 if (sizeof(__u32) * count > data->ioc_inllen3) {
1916                         obd_ioctl_freedata(buf, len);
1917                         RETURN(-EINVAL);
1918                 }
1919
1920                 desc = (struct lov_desc *)data->ioc_inlbuf1;
1921                 memcpy(desc, &(lov->desc), sizeof(*desc));
1922
1923                 uuidp = (struct obd_uuid *)data->ioc_inlbuf2;
1924                 genp = (__u32 *)data->ioc_inlbuf3;
1925                 tgtdesc = lov->tgts;
1926                 /* the uuid will be empty for deleted OSTs */
1927                 for (i = 0; i < count; i++, uuidp++, genp++, tgtdesc++) {
1928                         obd_str2uuid(uuidp, tgtdesc->uuid.uuid);
1929                         *genp = tgtdesc->ltd_gen;
1930                 }
1931
1932                 rc = copy_to_user((void *)uarg, buf, len);
1933                 if (rc)
1934                         rc = -EFAULT;
1935                 obd_ioctl_freedata(buf, len);
1936                 break;
1937         }
1938         case LL_IOC_LOV_SETSTRIPE:
1939                 rc = lov_setstripe(exp, karg, uarg);
1940                 break;
1941         case LL_IOC_LOV_GETSTRIPE:
1942                 rc = lov_getstripe(exp, karg, uarg);
1943                 break;
1944         case LL_IOC_LOV_SETEA:
1945                 rc = lov_setea(exp, karg, uarg);
1946                 break;
1947         default: {
1948                 int set = 0;
1949                 if (count == 0)
1950                         RETURN(-ENOTTY);
1951                 rc = 0;
1952                 for (i = 0; i < count; i++) {
1953                         int err;
1954
1955                         /* OST was deleted */
1956                         if (obd_uuid_empty(&lov->tgts[i].uuid))
1957                                 continue;
1958
1959                         err = obd_iocontrol(cmd, lov->tgts[i].ltd_exp,
1960                                             len, karg, uarg);
1961                         if (err) {
1962                                 if (lov->tgts[i].active) {
1963                                         CERROR("error: iocontrol OSC %s on OST "
1964                                                "idx %d cmd %x: err = %d\n",
1965                                                lov->tgts[i].uuid.uuid, i,
1966                                                cmd, err);
1967                                         if (!rc)
1968                                                 rc = err;
1969                                 }
1970                         } else
1971                                 set = 1;
1972                 }
1973                 if (!set && !rc)
1974                         rc = -EIO;
1975         }
1976         }
1977
1978         RETURN(rc);
1979 }
1980
1981 static int lov_get_info(struct obd_export *exp, __u32 keylen,
1982                         void *key, __u32 *vallen, void *val)
1983 {
1984         struct obd_device *obddev = class_exp2obd(exp);
1985         struct lov_obd *lov = &obddev->u.lov;
1986         int i;
1987         ENTRY;
1988
1989         if (!vallen || !val)
1990                 RETURN(-EFAULT);
1991
1992         if (keylen > strlen("lock_to_stripe") &&
1993             strcmp(key, "lock_to_stripe") == 0) {
1994                 struct {
1995                         char name[16];
1996                         struct ldlm_lock *lock;
1997                         struct lov_stripe_md *lsm;
1998                 } *data = key;
1999                 struct lov_oinfo *loi;
2000                 struct ldlm_res_id *res_id = &data->lock->l_resource->lr_name;
2001                 __u32 *stripe = val;
2002
2003                 if (*vallen < sizeof(*stripe))
2004                         RETURN(-EFAULT);
2005                 *vallen = sizeof(*stripe);
2006
2007                 /* XXX This is another one of those bits that will need to
2008                  * change if we ever actually support nested LOVs.  It uses
2009                  * the lock's export to find out which stripe it is. */
2010                 /* XXX - it's assumed all the locks for deleted OSTs have
2011                  * been cancelled. Also, the export for deleted OSTs will
2012                  * be NULL and won't match the lock's export. */
2013                 for (i = 0, loi = data->lsm->lsm_oinfo;
2014                      i < data->lsm->lsm_stripe_count;
2015                      i++, loi++) {
2016                         if (lov->tgts[loi->loi_ost_idx].ltd_exp ==
2017                                         data->lock->l_conn_export &&
2018                             loi->loi_id == res_id->name[0] &&
2019                             loi->loi_gr == res_id->name[2]) {
2020                                 *stripe = i;
2021                                 RETURN(0);
2022                         }
2023                 }
2024                 LDLM_ERROR(data->lock, "lock on inode without such object\n");
2025                 dump_lsm(D_ERROR, data->lsm);
2026                 RETURN(-ENXIO);
2027         } else if (keylen >= strlen("size_to_stripe") &&
2028                    strcmp(key, "size_to_stripe") == 0) {
2029                 struct {
2030                         int stripe_number;
2031                         __u64 size;
2032                         struct lov_stripe_md *lsm;
2033                 } *data = val;
2034
2035                 if (*vallen < sizeof(*data))
2036                         RETURN(-EFAULT);
2037
2038                 data->size = lov_size_to_stripe(data->lsm, data->size,
2039                                                 data->stripe_number);
2040                 RETURN(0);
2041         } else if (keylen >= strlen("last_id") && strcmp(key, "last_id") == 0) {
2042                 obd_id *ids = val;
2043                 int rc, size = sizeof(obd_id);
2044                 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
2045                         if (!lov->tgts[i].active)
2046                                 continue;
2047                         rc = obd_get_info(lov->tgts[i].ltd_exp,
2048                                           keylen, key, &size, &(ids[i]));
2049                         if (rc != 0)
2050                                 RETURN(rc);
2051                 }
2052                 RETURN(0);
2053         } else if (keylen >= strlen("lovdesc") && strcmp(key, "lovdesc") == 0) {
2054                 struct lov_desc *desc_ret = val;
2055                 *desc_ret = lov->desc;
2056
2057                 RETURN(0);
2058         }
2059
2060         RETURN(-EINVAL);
2061 }
2062
2063 static int lov_set_info(struct obd_export *exp, obd_count keylen,
2064                         void *key, obd_count vallen, void *val)
2065 {
2066         struct obd_device *obddev = class_exp2obd(exp);
2067         struct lov_obd *lov = &obddev->u.lov;
2068         int i, rc = 0, err;
2069         ENTRY;
2070
2071 #define KEY_IS(str) \
2072         (keylen == strlen(str) && memcmp(key, str, keylen) == 0)
2073
2074         if (KEY_IS("next_id")) {
2075                 if (vallen != lov->desc.ld_tgt_count)
2076                         RETURN(-EINVAL);
2077                 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
2078                         /* initialize all OSCs, even inactive ones */
2079
2080                         err = obd_set_info(lov->tgts[i].ltd_exp,
2081                                           keylen, key, sizeof(obd_id),
2082                                           ((obd_id*)val) + i);
2083                         if (!rc)
2084                                 rc = err;
2085                 }
2086                 RETURN(rc);
2087         }
2088         if (KEY_IS("async")) {
2089                 struct lov_desc *desc = &lov->desc;
2090                 struct lov_tgt_desc *tgts = lov->tgts;
2091
2092                 if (vallen != sizeof(int))
2093                         RETURN(-EINVAL);
2094                 lov->async = *((int*) val);
2095
2096                 for (i = 0; i < desc->ld_tgt_count; i++, tgts++) {
2097                         struct obd_uuid *tgt_uuid = &tgts->uuid;
2098                         struct obd_device *tgt_obd;
2099
2100                         tgt_obd = class_find_client_obd(tgt_uuid,
2101                                                         LUSTRE_OSC_NAME,
2102                                                         &obddev->obd_uuid);
2103                         if (!tgt_obd) {
2104                                 CERROR("Target %s not attached\n",
2105                                         tgt_uuid->uuid);
2106                                 if (!rc)
2107                                         rc = -EINVAL;
2108                                 continue;
2109                         }
2110
2111                         err = obd_set_info(tgt_obd->obd_self_export,
2112                                            keylen, key, vallen, val);
2113                         if (err) {
2114                                 CERROR("Failed to set async on target %s\n",
2115                                         tgt_obd->obd_name);
2116                                 if (!rc)
2117                                         rc = err;
2118                         }
2119                 }
2120                 RETURN(rc);
2121         }
2122
2123         if (KEY_IS("growth_count")) {
2124                 if (vallen != sizeof(int))
2125                         RETURN(-EINVAL);
2126         } else if (KEY_IS("mds_conn")) {
2127                 if (vallen != sizeof(__u32))
2128                         RETURN(-EINVAL);
2129         } else if (KEY_IS("unlinked") || KEY_IS("unrecovery")) {
2130                 if (vallen != 0)
2131                         RETURN(-EINVAL);
2132         } else if (KEY_IS("sec")) {
2133                 struct lov_tgt_desc *tgt;
2134                 struct obd_export *exp;
2135                 int rc = 0, err, i;
2136
2137                 spin_lock(&lov->lov_lock);
2138                 for (i = 0, tgt = lov->tgts; i < lov->desc.ld_tgt_count;
2139                      i++, tgt++) {
2140                         exp = tgt->ltd_exp;
2141                         /* during setup time the connections to osc might
2142                          * haven't been established.
2143                          */
2144                         if (exp == NULL) {
2145                                 struct obd_device *tgt_obd;
2146
2147                                 tgt_obd = class_find_client_obd(&tgt->uuid,
2148                                                                 LUSTRE_OSC_NAME,
2149                                                                 &obddev->obd_uuid);
2150                                 if (!tgt_obd) {
2151                                         CERROR("can't set security flavor, "
2152                                                "device %s not attached?\n",
2153                                                 tgt->uuid.uuid);
2154                                         rc = -EINVAL;
2155                                         continue;
2156                                 }
2157                                 exp = tgt_obd->obd_self_export;
2158                         }
2159
2160                         err = obd_set_info(exp, keylen, key, vallen, val);
2161                         if (!rc)
2162                                 rc = err;
2163                 }
2164                 spin_unlock(&lov->lov_lock);
2165
2166                 RETURN(rc);
2167         } else {
2168                 RETURN(-EINVAL);
2169         }
2170
2171         for (i = 0; i < lov->desc.ld_tgt_count; i++) {
2172                 if (val && !obd_uuid_equals(val, &lov->tgts[i].uuid))
2173                         continue;
2174
2175                 if (!val && !lov->tgts[i].active)
2176                         continue;
2177
2178                 err = obd_set_info(lov->tgts[i].ltd_exp,
2179                                   keylen, key, vallen, val);
2180                 if (!rc)
2181                         rc = err;
2182         }
2183         RETURN(rc);
2184 #undef KEY_IS
2185 }
2186
2187 #if 0
2188 struct lov_multi_wait {
2189         struct ldlm_lock *lock;
2190         wait_queue_t      wait;
2191         int               completed;
2192         int               generation;
2193 };
2194
2195 int lov_complete_many(struct obd_export *exp, struct lov_stripe_md *lsm,
2196                       struct lustre_handle *lockh)
2197 {
2198         struct lov_lock_handles *lov_lockh = NULL;
2199         struct lustre_handle *lov_lockhp;
2200         struct lov_obd *lov;
2201         struct lov_oinfo *loi;
2202         struct lov_multi_wait *queues;
2203         int rc = 0, i;
2204         ENTRY;
2205
2206         if (lsm_bad_magic(lsm))
2207                 RETURN(-EINVAL);
2208
2209         if (!exp || !exp->exp_obd)
2210                 RETURN(-ENODEV);
2211
2212         LASSERT(lockh != NULL);
2213         if (lsm->lsm_stripe_count > 1) {
2214                 lov_lockh = lov_handle2llh(lockh);
2215                 if (lov_lockh == NULL) {
2216                         CERROR("LOV: invalid lov lock handle %p\n", lockh);
2217                         RETURN(-EINVAL);
2218                 }
2219
2220                 lov_lockhp = lov_lockh->llh_handles;
2221         } else {
2222                 lov_lockhp = lockh;
2223         }
2224
2225         OBD_ALLOC(queues, lsm->lsm_stripe_count * sizeof(*queues));
2226         if (queues == NULL)
2227                 GOTO(out, rc = -ENOMEM);
2228
2229         lov = &exp->exp_obd->u.lov;
2230         for (i = 0, loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count;
2231              i++, loi++, lov_lockhp++) {
2232                 struct ldlm_lock *lock;
2233                 struct obd_device *obd;
2234                 unsigned long irqflags;
2235
2236                 lock = ldlm_handle2lock(lov_lockhp);
2237                 if (lock == NULL) {
2238                         CDEBUG(D_HA, "lov idx %d subobj "LPX64" no lock?\n",
2239                                loi->loi_ost_idx, loi->loi_id);
2240                         queues[i].completed = 1;
2241                         continue;
2242                 }
2243
2244                 queues[i].lock = lock;
2245                 init_waitqueue_entry(&(queues[i].wait), current);
2246                 add_wait_queue(lock->l_waitq, &(queues[i].wait));
2247
2248                 obd = class_exp2obd(lock->l_conn_export);
2249                 if (obd != NULL)
2250                         imp = obd->u.cli.cl_import;
2251                 if (imp != NULL) {
2252                         spin_lock_irqsave(&imp->imp_lock, irqflags);
2253                         queues[i].generation = imp->imp_generation;
2254                         spin_unlock_irqrestore(&imp->imp_lock, irqflags);
2255                 }
2256         }
2257
2258         lwi = LWI_TIMEOUT_INTR(obd_timeout * HZ, ldlm_expired_completion_wait,
2259                                interrupted_completion_wait, &lwd);
2260         rc = l_wait_event_added(check_multi_complete(queues, lsm), &lwi);
2261
2262         for (i = 0; i < lsm->lsm_stripe_count; i++)
2263                 remove_wait_queue(lock->l_waitq, &(queues[i].wait));
2264
2265         if (rc == -EINTR || rc == -ETIMEDOUT) {
2266
2267
2268         }
2269
2270  out:
2271         if (lov_lockh != NULL)
2272                 lov_llh_put(lov_lockh);
2273         RETURN(rc);
2274 }
2275 #endif
2276
2277 struct obd_ops lov_obd_ops = {
2278         .o_owner               = THIS_MODULE,
2279         .o_attach              = lov_attach,
2280         .o_detach              = lov_detach,
2281         .o_setup               = lov_setup,
2282         .o_cleanup             = lov_cleanup,
2283         .o_process_config      = lov_process_config,
2284         .o_connect             = lov_connect,
2285         .o_disconnect          = lov_disconnect,
2286         .o_statfs              = lov_statfs,
2287         .o_packmd              = lov_packmd,
2288         .o_unpackmd            = lov_unpackmd,
2289         .o_revalidate_md       = lov_revalidate_md,
2290         .o_create              = lov_create,
2291         .o_destroy             = lov_destroy,
2292         .o_getattr             = lov_getattr,
2293         .o_getattr_async       = lov_getattr_async,
2294         .o_setattr             = lov_setattr,
2295         .o_brw                 = lov_brw,
2296         .o_brw_async           = lov_brw_async,
2297         .o_prep_async_page     = lov_prep_async_page,
2298         .o_queue_async_io      = lov_queue_async_io,
2299         .o_set_async_flags     = lov_set_async_flags,
2300         .o_queue_group_io      = lov_queue_group_io,
2301         .o_trigger_group_io    = lov_trigger_group_io,
2302         .o_teardown_async_page = lov_teardown_async_page,
2303         .o_adjust_kms          = lov_adjust_kms,
2304         .o_punch               = lov_punch,
2305         .o_sync                = lov_sync,
2306         .o_enqueue             = lov_enqueue,
2307         .o_match               = lov_match,
2308         .o_change_cbdata       = lov_change_cbdata,
2309         .o_cancel              = lov_cancel,
2310         .o_cancel_unused       = lov_cancel_unused,
2311         .o_iocontrol           = lov_iocontrol,
2312         .o_get_info            = lov_get_info,
2313         .o_set_info            = lov_set_info,
2314         .o_llog_init           = lov_llog_init,
2315         .o_llog_finish         = lov_llog_finish,
2316         .o_notify              = lov_notify,
2317 };
2318
2319 int __init lov_init(void)
2320 {
2321         struct lprocfs_static_vars lvars;
2322         int rc;
2323         ENTRY;
2324
2325         lprocfs_init_vars(lov, &lvars);
2326         rc = class_register_type(&lov_obd_ops, NULL, lvars.module_vars,
2327                                  OBD_LOV_DEVICENAME);
2328         RETURN(rc);
2329 }
2330
2331 #ifdef __KERNEL__
2332 static void /*__exit*/ lov_exit(void)
2333 {
2334         class_unregister_type(OBD_LOV_DEVICENAME);
2335 }
2336
2337 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
2338 MODULE_DESCRIPTION("Lustre Logical Object Volume OBD driver");
2339 MODULE_LICENSE("GPL");
2340
2341 module_init(lov_init);
2342 module_exit(lov_exit);
2343 #endif