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