Whamcloud - gitweb
- small debug and cleanups
[fs/lustre-release.git] / lustre / lov / lov_obd.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Copyright (C) 2002-2006 Cluster File Systems, Inc.
5  * Author: Phil Schwan <phil@clusterfs.com>
6  *         Peter Braam <braam@clusterfs.com>
7  *         Mike Shaver <shaver@clusterfs.com>
8  *         Nathan Rutman <nathan@clusterfs.com>
9  *
10  *   This file is part of the Lustre file system, http://www.lustre.org
11  *   Lustre is a trademark of Cluster File Systems, Inc.
12  *
13  *   You may have signed or agreed to another license before downloading
14  *   this software.  If so, you are bound by the terms and conditions
15  *   of that agreement, and the following does not apply to you.  See the
16  *   LICENSE file included with this distribution for more information.
17  *
18  *   If you did not agree to a different license, then this copy of Lustre
19  *   is open source software; you can redistribute it and/or modify it
20  *   under the terms of version 2 of the GNU General Public License as
21  *   published by the Free Software Foundation.
22  *
23  *   In either case, Lustre is distributed in the hope that it will be
24  *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
25  *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26  *   license text for more details.
27  */
28
29 #ifndef EXPORT_SYMTAB
30 # define EXPORT_SYMTAB
31 #endif
32 #define DEBUG_SUBSYSTEM S_LOV
33 #ifdef __KERNEL__
34 #include <libcfs/libcfs.h>
35 #else
36 #include <liblustre.h>
37 #endif
38
39 #include <obd_support.h>
40 #include <lustre_lib.h>
41 #include <lustre_net.h>
42 #include <lustre/lustre_idl.h>
43 #include <lustre_dlm.h>
44 #include <lustre_mds.h>
45 #include <lustre_debug.h>
46 #include <obd_class.h>
47 #include <obd_lov.h>
48 #include <obd_ost.h>
49 #include <lprocfs_status.h>
50 #include <lustre_param.h>
51
52 #include "lov_internal.h"
53
54
55 /* Keep a refcount of lov->tgt usage to prevent racing with addition/deletion.
56    Any function that expects lov_tgts to remain stationary must take a ref. */
57 void lov_getref(struct obd_device *obd)
58 {
59         struct lov_obd *lov = &obd->u.lov;
60
61         /* nobody gets through here until lov_putref is done */
62         mutex_down(&lov->lov_lock);
63         atomic_inc(&lov->lov_refcount);
64         mutex_up(&lov->lov_lock);
65         return;
66 }
67
68 static void __lov_del_obd(struct obd_device *obd, __u32 index);
69
70 void lov_putref(struct obd_device *obd)
71 {
72         struct lov_obd *lov = &obd->u.lov;
73         mutex_down(&lov->lov_lock);
74         /* ok to dec to 0 more than once -- ltd_exp's will be null */
75         if (atomic_dec_and_test(&lov->lov_refcount) && lov->lov_death_row) {
76                 int i;
77                 CDEBUG(D_CONFIG, "destroying %d lov targets\n",
78                        lov->lov_death_row);
79                 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
80                         if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_reap)
81                                 continue;
82                         /* Disconnect and delete from list */
83                         __lov_del_obd(obd, i);
84                         lov->lov_death_row--;
85                 }
86         }
87         mutex_up(&lov->lov_lock);
88 }
89
90 #define MAX_STRING_SIZE 128
91 static int lov_connect_obd(struct obd_device *obd, __u32 index, int activate,
92                            struct obd_connect_data *data)
93 {
94         struct lov_obd *lov = &obd->u.lov;
95         struct obd_uuid tgt_uuid = lov->lov_tgts[index]->ltd_uuid;
96         struct obd_device *tgt_obd;
97         struct obd_uuid lov_osc_uuid = { "LOV_OSC_UUID" };
98         struct lustre_handle conn = {0, };
99         struct obd_import *imp;
100 #ifdef __KERNEL__
101         cfs_proc_dir_entry_t *lov_proc_dir;
102 #endif
103         int rc;
104         ENTRY;
105
106         if (!lov->lov_tgts[index])
107                 RETURN(-EINVAL);
108
109         tgt_obd = class_find_client_obd(&tgt_uuid, LUSTRE_OSC_NAME,
110                                         &obd->obd_uuid);
111
112         if (!tgt_obd) {
113                 CERROR("Target %s not attached\n", obd_uuid2str(&tgt_uuid));
114                 RETURN(-EINVAL);
115         }
116         if (!tgt_obd->obd_set_up) {
117                 CERROR("Target %s not set up\n", obd_uuid2str(&tgt_uuid));
118                 RETURN(-EINVAL);
119         }
120
121         if (activate) {
122                 tgt_obd->obd_no_recov = 0;
123                 /* FIXME this is probably supposed to be
124                    ptlrpc_set_import_active.  Horrible naming. */
125                 ptlrpc_activate_import(tgt_obd->u.cli.cl_import);
126         }
127
128         /*
129          * Divine LOV knows that OBDs under it are OSCs.
130          */
131         imp = tgt_obd->u.cli.cl_import;
132
133         if (imp->imp_invalid) {
134                 CERROR("not connecting OSC %s; administratively "
135                        "disabled\n", obd_uuid2str(&tgt_uuid));
136                 rc = obd_register_observer(tgt_obd, obd);
137                 if (rc) {
138                         CERROR("Target %s register_observer error %d; "
139                                "will not be able to reactivate\n",
140                                obd_uuid2str(&tgt_uuid), rc);
141                 }
142                 RETURN(0);
143         }
144
145         if (data && (data->ocd_connect_flags & OBD_CONNECT_INDEX))
146                 data->ocd_index = index;
147
148         rc = obd_connect(NULL, &conn, tgt_obd, &lov_osc_uuid, data);
149         if (rc) {
150                 CERROR("Target %s connect error %d\n",
151                        obd_uuid2str(&tgt_uuid), rc);
152                 RETURN(rc);
153         }
154         lov->lov_tgts[index]->ltd_exp = class_conn2export(&conn);
155         if (!lov->lov_tgts[index]->ltd_exp) {
156                 CERROR("Target %s: null export!\n", obd_uuid2str(&tgt_uuid));
157                 RETURN(-ENODEV);
158         }
159
160         rc = obd_register_observer(tgt_obd, obd);
161         if (rc) {
162                 CERROR("Target %s register_observer error %d\n",
163                        obd_uuid2str(&tgt_uuid), rc);
164                 obd_disconnect(lov->lov_tgts[index]->ltd_exp);
165                 lov->lov_tgts[index]->ltd_exp = NULL;
166                 RETURN(rc);
167         }
168
169         lov->lov_tgts[index]->ltd_reap = 0;
170         if (activate) {
171                 lov->lov_tgts[index]->ltd_active = 1;
172                 lov->desc.ld_active_tgt_count++;
173         }
174         CDEBUG(D_CONFIG, "Connected tgt idx %d %s (%s) %sactive\n", index,
175                obd_uuid2str(&tgt_uuid), tgt_obd->obd_name, activate ? "":"in");
176
177 #ifdef __KERNEL__
178         lov_proc_dir = lprocfs_srch(obd->obd_proc_entry, "target_obds");
179         if (lov_proc_dir) {
180                 struct obd_device *osc_obd = class_conn2obd(&conn);
181                 cfs_proc_dir_entry_t *osc_symlink;
182                 char name[MAX_STRING_SIZE];
183
184                 LASSERT(osc_obd != NULL);
185                 LASSERT(osc_obd->obd_magic == OBD_DEVICE_MAGIC);
186                 LASSERT(osc_obd->obd_type->typ_name != NULL);
187                 snprintf(name, MAX_STRING_SIZE, "../../../%s/%s",
188                          osc_obd->obd_type->typ_name,
189                          osc_obd->obd_name);
190                 osc_symlink = proc_symlink(osc_obd->obd_name, lov_proc_dir,
191                                            name);
192                 if (osc_symlink == NULL) {
193                         CERROR("could not register LOV target "
194                                "/proc/fs/lustre/%s/%s/target_obds/%s.",
195                                obd->obd_type->typ_name, obd->obd_name,
196                                osc_obd->obd_name);
197                         lprocfs_remove(lov_proc_dir);
198                         lov_proc_dir = NULL;
199                 }
200         }
201 #endif
202
203         rc = qos_add_tgt(obd, index);
204         if (rc)
205                 CERROR("qos_add_tgt failed %d\n", rc);
206
207         RETURN(0);
208 }
209
210 static int lov_connect(const struct lu_env *env,
211                        struct lustre_handle *conn, struct obd_device *obd,
212                        struct obd_uuid *cluuid, struct obd_connect_data *data)
213 {
214         struct lov_obd *lov = &obd->u.lov;
215         struct lov_tgt_desc *tgt;
216         int i, rc;
217         ENTRY;
218
219         CDEBUG(D_CONFIG, "connect #%d\n", lov->lov_connects);
220
221         rc = class_connect(conn, obd, cluuid);
222         if (rc)
223                 RETURN(rc);
224
225         /* Why should there ever be more than 1 connect? */
226         lov->lov_connects++;
227         LASSERT(lov->lov_connects == 1);
228
229         memset(&lov->lov_ocd, 0, sizeof(lov->lov_ocd));
230         if (data)
231                 lov->lov_ocd = *data;
232
233         lov_getref(obd);
234         for (i = 0; i < lov->desc.ld_tgt_count; i++) {
235                 tgt = lov->lov_tgts[i];
236                 if (!tgt || obd_uuid_empty(&tgt->ltd_uuid))
237                         continue;
238                 /* Flags will be lowest common denominator */
239                 rc = lov_connect_obd(obd, i, tgt->ltd_activate, &lov->lov_ocd);
240                 if (rc) {
241                         CERROR("%s: lov connect tgt %d failed: %d\n",
242                                obd->obd_name, i, rc);
243                         continue;
244                 }
245         }
246         lov_putref(obd);
247
248         RETURN(0);
249 }
250
251 static int lov_disconnect_obd(struct obd_device *obd, __u32 index)
252 {
253         cfs_proc_dir_entry_t *lov_proc_dir;
254         struct lov_obd *lov = &obd->u.lov;
255         struct obd_device *osc_obd =
256                 class_exp2obd(lov->lov_tgts[index]->ltd_exp);
257         int rc;
258         ENTRY;
259
260         CDEBUG(D_CONFIG, "%s: disconnecting target %s\n",
261                obd->obd_name, osc_obd->obd_name);
262
263         if (lov->lov_tgts[index]->ltd_active) {
264                 lov->lov_tgts[index]->ltd_active = 0;
265                 lov->desc.ld_active_tgt_count--;
266         }
267
268         lov_proc_dir = lprocfs_srch(obd->obd_proc_entry, "target_obds");
269         if (lov_proc_dir) {
270                 cfs_proc_dir_entry_t *osc_symlink;
271
272                 osc_symlink = lprocfs_srch(lov_proc_dir, osc_obd->obd_name);
273                 if (osc_symlink) {
274                         lprocfs_remove(osc_symlink);
275                 } else {
276                         CERROR("/proc/fs/lustre/%s/%s/target_obds/%s missing.",
277                                obd->obd_type->typ_name, obd->obd_name,
278                                osc_obd->obd_name);
279                 }
280         }
281
282         if (obd->obd_no_recov) {
283                 /* Pass it on to our clients.
284                  * XXX This should be an argument to disconnect,
285                  * XXX not a back-door flag on the OBD.  Ah well.
286                  */
287                 if (osc_obd)
288                         osc_obd->obd_no_recov = 1;
289         }
290
291         obd_register_observer(osc_obd, NULL);
292
293         rc = obd_disconnect(lov->lov_tgts[index]->ltd_exp);
294         if (rc) {
295                 CERROR("Target %s disconnect error %d\n",
296                        lov_uuid2str(lov, index), rc);
297                 rc = 0;
298         }
299
300         qos_del_tgt(obd, index);
301
302         lov->lov_tgts[index]->ltd_exp = NULL;
303         RETURN(0);
304 }
305
306 static int lov_del_target(struct obd_device *obd, __u32 index,
307                           struct obd_uuid *uuidp, int gen);
308
309 static int lov_disconnect(struct obd_export *exp)
310 {
311         struct obd_device *obd = class_exp2obd(exp);
312         struct lov_obd *lov = &obd->u.lov;
313         int i, rc;
314         ENTRY;
315
316         if (!lov->lov_tgts)
317                 goto out;
318
319         /* Only disconnect the underlying layers on the final disconnect. */
320         lov->lov_connects--;
321         if (lov->lov_connects != 0) {
322                 /* why should there be more than 1 connect? */
323                 CERROR("disconnect #%d\n", lov->lov_connects);
324                 goto out;
325         }
326
327         /* Let's hold another reference so lov_del_obd doesn't spin through
328            putref every time */
329         lov_getref(obd);
330         for (i = 0; i < lov->desc.ld_tgt_count; i++) {
331                 if (lov->lov_tgts[i] && lov->lov_tgts[i]->ltd_exp) {
332                         /* Disconnection is the last we know about an obd */
333                         lov_del_target(obd, i, 0, lov->lov_tgts[i]->ltd_gen);
334                 }
335         }
336         lov_putref(obd);
337
338 out:
339         rc = class_disconnect(exp); /* bz 9811 */
340         RETURN(rc);
341 }
342
343 /* Error codes:
344  *
345  *  -EINVAL  : UUID can't be found in the LOV's target list
346  *  -ENOTCONN: The UUID is found, but the target connection is bad (!)
347  *  -EBADF   : The UUID is found, but the OBD is the wrong type (!)
348  */
349 static int lov_set_osc_active(struct obd_device *obd, struct obd_uuid *uuid,
350                               int activate)
351 {
352         struct lov_obd *lov = &obd->u.lov;
353         struct lov_tgt_desc *tgt;
354         int i, rc = 0;
355         ENTRY;
356
357         CDEBUG(D_INFO, "Searching in lov %p for uuid %s (activate=%d)\n",
358                lov, uuid->uuid, activate);
359
360         lov_getref(obd);
361         for (i = 0; i < lov->desc.ld_tgt_count; i++) {
362                 tgt = lov->lov_tgts[i];
363                 if (!tgt || !tgt->ltd_exp)
364                         continue;
365
366                 CDEBUG(D_INFO, "lov idx %d is %s conn "LPX64"\n",
367                        i, obd_uuid2str(&tgt->ltd_uuid),
368                        tgt->ltd_exp->exp_handle.h_cookie);
369                 if (obd_uuid_equals(uuid, &tgt->ltd_uuid))
370                         break;
371         }
372
373         if (i == lov->desc.ld_tgt_count)
374                 GOTO(out, rc = -EINVAL);
375
376         if (lov->lov_tgts[i]->ltd_active == activate) {
377                 CDEBUG(D_INFO, "OSC %s already %sactive!\n", uuid->uuid,
378                        activate ? "" : "in");
379                 GOTO(out, rc);
380         }
381
382         CDEBUG(D_CONFIG, "Marking OSC %s %sactive\n", obd_uuid2str(uuid),
383                activate ? "" : "in");
384
385         lov->lov_tgts[i]->ltd_active = activate;
386         if (activate)
387                 lov->desc.ld_active_tgt_count++;
388         else
389                 lov->desc.ld_active_tgt_count--;
390         /* remove any old qos penalty */
391         lov->lov_tgts[i]->ltd_qos.ltq_penalty = 0;
392
393  out:
394         lov_putref(obd);
395         RETURN(rc);
396 }
397
398 static int lov_notify(struct obd_device *obd, struct obd_device *watched,
399                       enum obd_notify_event ev, void *data)
400 {
401         int rc = 0;
402         ENTRY;
403
404         if (ev == OBD_NOTIFY_ACTIVE || ev == OBD_NOTIFY_INACTIVE) {
405                 struct obd_uuid *uuid;
406
407                 LASSERT(watched);
408
409                 if (strcmp(watched->obd_type->typ_name, LUSTRE_OSC_NAME)) {
410                         CERROR("unexpected notification of %s %s!\n",
411                                watched->obd_type->typ_name,
412                                watched->obd_name);
413                         RETURN(-EINVAL);
414                 }
415                 uuid = &watched->u.cli.cl_target_uuid;
416
417                 /* Set OSC as active before notifying the observer, so the
418                  * observer can use the OSC normally.
419                  */
420                 rc = lov_set_osc_active(obd, uuid, ev == OBD_NOTIFY_ACTIVE);
421                 if (rc) {
422                         CERROR("%sactivation of %s failed: %d\n",
423                                (ev == OBD_NOTIFY_ACTIVE) ? "" : "de",
424                                obd_uuid2str(uuid), rc);
425                         RETURN(rc);
426                 }
427         }
428
429         /* Pass the notification up the chain. */
430         if (watched) {
431                 rc = obd_notify_observer(obd, watched, ev, data);
432         } else {
433                 /* NULL watched means all osc's in the lov (only for syncs) */
434                 struct lov_obd *lov = &obd->u.lov;
435                 struct obd_device *tgt_obd;
436                 int i;
437                 lov_getref(obd);
438                 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
439                         if (!lov->lov_tgts[i])
440                                 continue;
441                         tgt_obd = class_exp2obd(lov->lov_tgts[i]->ltd_exp);
442                         rc = obd_notify_observer(obd, tgt_obd, ev, data);
443                         if (rc) {
444                                 CERROR("%s: notify %s of %s failed %d\n",
445                                        obd->obd_name,
446                                        obd->obd_observer->obd_name,
447                                        tgt_obd->obd_name, rc);
448                                 break;
449                         }
450                 }
451                 lov_putref(obd);
452         }
453
454         RETURN(rc);
455 }
456
457 static int lov_add_target(struct obd_device *obd, struct obd_uuid *uuidp,
458                           __u32 index, int gen, int active)
459 {
460         struct lov_obd *lov = &obd->u.lov;
461         struct lov_tgt_desc *tgt;
462         int rc;
463         ENTRY;
464
465         CDEBUG(D_CONFIG, "uuid:%s idx:%d gen:%d active:%d\n",
466                uuidp->uuid, index, gen, active);
467
468         if (gen <= 0) {
469                 CERROR("request to add OBD %s with invalid generation: %d\n",
470                        uuidp->uuid, gen);
471                 RETURN(-EINVAL);
472         }
473
474         mutex_down(&lov->lov_lock);
475
476         if ((index < lov->lov_tgt_size) && (lov->lov_tgts[index] != NULL)) {
477                 tgt = lov->lov_tgts[index];
478                 CERROR("UUID %s already assigned at LOV target index %d\n",
479                        obd_uuid2str(&tgt->ltd_uuid), index);
480                 mutex_up(&lov->lov_lock);
481                 RETURN(-EEXIST);
482         }
483
484         if (index >= lov->lov_tgt_size) {
485                 /* We need to reallocate the lov target array. */
486                 struct lov_tgt_desc **newtgts, **old = NULL;
487                 __u32 newsize, oldsize = 0;
488
489                 newsize = max(lov->lov_tgt_size, (__u32)2);
490                 while (newsize < index + 1)
491                         newsize = newsize << 1;
492                 OBD_ALLOC(newtgts, sizeof(*newtgts) * newsize);
493                 if (newtgts == NULL) {
494                         mutex_up(&lov->lov_lock);
495                         RETURN(-ENOMEM);
496                 }
497
498                 if (lov->lov_tgt_size) {
499                         memcpy(newtgts, lov->lov_tgts, sizeof(*newtgts) *
500                                lov->lov_tgt_size);
501                         old = lov->lov_tgts;
502                         oldsize = lov->lov_tgt_size;
503                 }
504
505                 lov->lov_tgts = newtgts;
506                 lov->lov_tgt_size = newsize;
507 #ifdef __KERNEL__
508                 smp_rmb();
509 #endif
510                 if (old)
511                         OBD_FREE(old, sizeof(*old) * oldsize);
512
513                 CDEBUG(D_CONFIG, "tgts: %p size: %d\n",
514                        lov->lov_tgts, lov->lov_tgt_size);
515         }
516
517
518         OBD_ALLOC_PTR(tgt);
519         if (!tgt) {
520                 mutex_up(&lov->lov_lock);
521                 RETURN(-ENOMEM);
522         }
523
524         memset(tgt, 0, sizeof(*tgt));
525         tgt->ltd_uuid = *uuidp;
526         /* XXX - add a sanity check on the generation number. */
527         tgt->ltd_gen = gen;
528         tgt->ltd_index = index;
529         tgt->ltd_activate = active;
530         lov->lov_tgts[index] = tgt;
531         if (index >= lov->desc.ld_tgt_count)
532                 lov->desc.ld_tgt_count = index + 1;
533         mutex_up(&lov->lov_lock);
534
535         CDEBUG(D_CONFIG, "idx=%d ltd_gen=%d ld_tgt_count=%d\n",
536                 index, tgt->ltd_gen, lov->desc.ld_tgt_count);
537
538         if (lov->lov_connects == 0) {
539                 /* lov_connect hasn't been called yet. We'll do the
540                    lov_connect_obd on this target when that fn first runs,
541                    because we don't know the connect flags yet. */
542                 RETURN(0);
543         }
544
545         lov_getref(obd);
546
547         rc = lov_connect_obd(obd, index, active, &lov->lov_ocd);
548         if (rc)
549                 GOTO(out, rc);
550
551         rc = lov_notify(obd, tgt->ltd_exp->exp_obd,
552                         active ? OBD_NOTIFY_ACTIVE : OBD_NOTIFY_INACTIVE,
553                         (void *)&index);
554
555 out:
556         if (rc) {
557                 CERROR("add failed (%d), deleting %s\n", rc,
558                        obd_uuid2str(&tgt->ltd_uuid));
559                 lov_del_target(obd, index, 0, 0);
560         }
561         lov_putref(obd);
562         RETURN(rc);
563 }
564
565 /* Schedule a target for deletion */
566 static int lov_del_target(struct obd_device *obd, __u32 index,
567                           struct obd_uuid *uuidp, int gen)
568 {
569         struct lov_obd *lov = &obd->u.lov;
570         int count = lov->desc.ld_tgt_count;
571         int rc = 0;
572         ENTRY;
573
574         if (index >= count) {
575                 CERROR("LOV target index %d >= number of LOV OBDs %d.\n",
576                        index, count);
577                 RETURN(-EINVAL);
578         }
579
580         lov_getref(obd);
581
582         if (!lov->lov_tgts[index]) {
583                 CERROR("LOV target at index %d is not setup.\n", index);
584                 GOTO(out, rc = -EINVAL);
585         }
586
587         if (uuidp && !obd_uuid_equals(uuidp, &lov->lov_tgts[index]->ltd_uuid)) {
588                 CERROR("LOV target UUID %s at index %d doesn't match %s.\n",
589                        lov_uuid2str(lov, index), index,
590                        obd_uuid2str(uuidp));
591                 GOTO(out, rc = -EINVAL);
592         }
593
594         CDEBUG(D_CONFIG, "uuid: %s idx: %d gen: %d exp: %p active: %d\n",
595                lov_uuid2str(lov, index), index,
596                lov->lov_tgts[index]->ltd_gen, lov->lov_tgts[index]->ltd_exp,
597                lov->lov_tgts[index]->ltd_active);
598
599         lov->lov_tgts[index]->ltd_reap = 1;
600         lov->lov_death_row++;
601         /* we really delete it from lov_putref */
602 out:
603         lov_putref(obd);
604
605         RETURN(rc);
606 }
607
608 /* We are holding lov_lock */
609 static void __lov_del_obd(struct obd_device *obd, __u32 index)
610 {
611         struct lov_obd *lov = &obd->u.lov;
612         struct obd_device *osc_obd;
613         struct lov_tgt_desc *tgt = lov->lov_tgts[index];
614
615         LASSERT(tgt);
616         LASSERT(tgt->ltd_reap);
617
618         osc_obd = class_exp2obd(tgt->ltd_exp);
619
620         CDEBUG(D_CONFIG, "Removing tgt %s : %s\n",
621                lov_uuid2str(lov, index),
622                osc_obd ? osc_obd->obd_name : "<no obd>");
623
624         if (tgt->ltd_exp)
625                 lov_disconnect_obd(obd, index);
626
627         /* XXX - right now there is a dependency on ld_tgt_count being the
628          * maximum tgt index for computing the mds_max_easize. So we can't
629          * shrink it. */
630
631         lov->lov_tgts[index] = NULL;
632         OBD_FREE_PTR(tgt);
633
634         /* Manual cleanup - no cleanup logs to clean up the osc's.  We must
635            do it ourselves. And we can't do it from lov_cleanup,
636            because we just lost our only reference to it. */
637         if (osc_obd) {
638                 /* Use lov's force/fail flags. */
639                 osc_obd->obd_force = obd->obd_force;
640                 osc_obd->obd_fail = obd->obd_fail;
641                 class_manual_cleanup(osc_obd);
642         }
643 }
644
645 void lov_fix_desc(struct lov_desc *desc)
646 {
647         if (desc->ld_default_stripe_size < PTLRPC_MAX_BRW_SIZE) {
648                 LCONSOLE_WARN("Increasing default stripe size to min %u\n",
649                               PTLRPC_MAX_BRW_SIZE);
650                 desc->ld_default_stripe_size = PTLRPC_MAX_BRW_SIZE;
651         } else if (desc->ld_default_stripe_size & (LOV_MIN_STRIPE_SIZE - 1)) {
652                 desc->ld_default_stripe_size &= ~(LOV_MIN_STRIPE_SIZE - 1);
653                 LCONSOLE_WARN("Changing default stripe size to "LPU64" (a "
654                               "multiple of %u)\n",
655                               desc->ld_default_stripe_size,LOV_MIN_STRIPE_SIZE);
656         }
657
658         if (desc->ld_default_stripe_count == 0)
659                 desc->ld_default_stripe_count = 1;
660
661         /* from lov_setstripe */
662         if ((desc->ld_pattern != 0) &&
663             (desc->ld_pattern != LOV_PATTERN_RAID0)) {
664                 LCONSOLE_WARN("Unknown stripe pattern: %#x\n",desc->ld_pattern);
665                 desc->ld_pattern = 0;
666         }
667
668         if (desc->ld_qos_maxage == 0)
669                 desc->ld_qos_maxage = QOS_DEFAULT_MAXAGE;
670 }
671
672 static int lov_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
673 {
674         struct lprocfs_static_vars lvars;
675         struct lov_desc *desc;
676         struct lov_obd *lov = &obd->u.lov;
677         int count;
678         ENTRY;
679
680         if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
681                 CERROR("LOV setup requires a descriptor\n");
682                 RETURN(-EINVAL);
683         }
684
685         desc = (struct lov_desc *)lustre_cfg_buf(lcfg, 1);
686
687         if (sizeof(*desc) > LUSTRE_CFG_BUFLEN(lcfg, 1)) {
688                 CERROR("descriptor size wrong: %d > %d\n",
689                        (int)sizeof(*desc), LUSTRE_CFG_BUFLEN(lcfg, 1));
690                 RETURN(-EINVAL);
691         }
692
693         if (desc->ld_magic != LOV_DESC_MAGIC) {
694                 if (desc->ld_magic == __swab32(LOV_DESC_MAGIC)) {
695                             CDEBUG(D_OTHER, "%s: Swabbing lov desc %p\n",
696                                    obd->obd_name, desc);
697                             lustre_swab_lov_desc(desc);
698                 } else {
699                         CERROR("%s: Bad lov desc magic: %#x\n",
700                                obd->obd_name, desc->ld_magic);
701                         RETURN(-EINVAL);
702                 }
703         }
704
705         lov_fix_desc(desc);
706
707         /* Because of 64-bit divide/mod operations only work with a 32-bit
708          * divisor in a 32-bit kernel, we cannot support a stripe width
709          * of 4GB or larger on 32-bit CPUs. */
710         count = desc->ld_default_stripe_count;
711         if ((count > 0 ? count : desc->ld_tgt_count) *
712             desc->ld_default_stripe_size > 0xffffffff) {
713                 CERROR("LOV: stripe width "LPU64"x%u > 4294967295 bytes\n",
714                        desc->ld_default_stripe_size, count);
715                 RETURN(-EINVAL);
716         }
717
718         desc->ld_active_tgt_count = 0;
719         lov->desc = *desc;
720         lov->lov_tgt_size = 0;
721         sema_init(&lov->lov_lock, 1);
722         atomic_set(&lov->lov_refcount, 0);
723         INIT_LIST_HEAD(&lov->lov_qos.lq_oss_list);
724         init_rwsem(&lov->lov_qos.lq_rw_sem);
725         lov->lov_qos.lq_dirty = 1;
726         lov->lov_qos.lq_dirty_rr = 1;
727         lov->lov_qos.lq_reset = 1;
728
729         lprocfs_init_vars(lov, &lvars);
730         lprocfs_obd_setup(obd, lvars.obd_vars);
731 #ifdef LPROCFS
732         {
733                 cfs_proc_dir_entry_t *entry;
734
735                 entry = create_proc_entry("target_obd", 0444,
736                                           obd->obd_proc_entry);
737                 if (entry != NULL) {
738                         entry->proc_fops = &lov_proc_target_fops;
739                         entry->data = obd;
740                 }
741         }
742 #endif
743
744         RETURN(0);
745 }
746
747 static int lov_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
748 {
749         int rc = 0;
750         ENTRY;
751
752         switch (stage) {
753         case OBD_CLEANUP_EARLY: {
754                 struct lov_obd *lov = &obd->u.lov;
755                 int i;
756                 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
757                         if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_active)
758                                 continue;
759                         obd_precleanup(class_exp2obd(lov->lov_tgts[i]->ltd_exp),
760                                        OBD_CLEANUP_EARLY);
761                 }
762                 break;
763         }
764         case OBD_CLEANUP_EXPORTS:
765                 break;
766         case OBD_CLEANUP_SELF_EXP:
767                 rc = obd_llog_finish(obd, 0);
768                 if (rc != 0)
769                         CERROR("failed to cleanup llogging subsystems\n");
770                 break;
771         case OBD_CLEANUP_OBD:
772                 break;
773         }
774         RETURN(rc);
775 }
776
777 static int lov_cleanup(struct obd_device *obd)
778 {
779         struct lov_obd *lov = &obd->u.lov;
780
781         lprocfs_obd_cleanup(obd);
782         if (lov->lov_tgts) {
783                 int i;
784                 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
785                         /* We should never get here - these should have
786                            been removed in the disconnect. */
787                         if (lov->lov_tgts[i]) {
788                                 CERROR("lov tgt %d not cleaned!"
789                                        " deathrow=%d, lovrc=%d\n",
790                                        i, lov->lov_death_row,
791                                        atomic_read(&lov->lov_refcount));
792                                 lov_del_target(obd, i, 0, 0);
793                         }
794                 }
795                 OBD_FREE(lov->lov_tgts, sizeof(*lov->lov_tgts) *
796                          lov->lov_tgt_size);
797                 lov->lov_tgt_size = 0;
798         }
799
800         if (lov->lov_qos.lq_rr_size)
801                 OBD_FREE(lov->lov_qos.lq_rr_array, lov->lov_qos.lq_rr_size);
802
803         RETURN(0);
804 }
805
806 static int lov_process_config(struct obd_device *obd, obd_count len, void *buf)
807 {
808         struct lustre_cfg *lcfg = buf;
809         struct obd_uuid obd_uuid;
810         int cmd;
811         int rc = 0;
812         ENTRY;
813
814         switch(cmd = lcfg->lcfg_command) {
815         case LCFG_LOV_ADD_OBD:
816         case LCFG_LOV_ADD_INA:
817         case LCFG_LOV_DEL_OBD: {
818                 __u32 index;
819                 int gen;
820                 /* lov_modify_tgts add  0:lov_mdsA  1:ost1_UUID  2:0  3:1 */
821                 if (LUSTRE_CFG_BUFLEN(lcfg, 1) > sizeof(obd_uuid.uuid))
822                         GOTO(out, rc = -EINVAL);
823
824                 obd_str2uuid(&obd_uuid,  lustre_cfg_buf(lcfg, 1));
825
826                 if (sscanf(lustre_cfg_buf(lcfg, 2), "%d", &index) != 1)
827                         GOTO(out, rc = -EINVAL);
828                 if (sscanf(lustre_cfg_buf(lcfg, 3), "%d", &gen) != 1)
829                         GOTO(out, rc = -EINVAL);
830                 if (cmd == LCFG_LOV_ADD_OBD)
831                         rc = lov_add_target(obd, &obd_uuid, index, gen, 1);
832                 else if (cmd == LCFG_LOV_ADD_INA)
833                         rc = lov_add_target(obd, &obd_uuid, index, gen, 0);
834                 else
835                         rc = lov_del_target(obd, index, &obd_uuid, gen);
836                 GOTO(out, rc);
837         }
838         case LCFG_PARAM: {
839                 struct lprocfs_static_vars lvars;
840                 struct lov_desc *desc = &(obd->u.lov.desc);
841
842                 if (!desc)
843                         GOTO(out, rc = -EINVAL);
844
845                 lprocfs_init_vars(lov, &lvars);
846
847                 rc = class_process_proc_param(PARAM_LOV, lvars.obd_vars,
848                                               lcfg, obd);
849                 GOTO(out, rc);
850         }
851         default: {
852                 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
853                 GOTO(out, rc = -EINVAL);
854
855         }
856         }
857 out:
858         RETURN(rc);
859 }
860
861 #ifndef log2
862 #define log2(n) ffz(~(n))
863 #endif
864
865 static int lov_clear_orphans(struct obd_export *export, struct obdo *src_oa,
866                              struct lov_stripe_md **ea,
867                              struct obd_trans_info *oti)
868 {
869         struct lov_obd *lov;
870         struct obdo *tmp_oa;
871         struct obd_uuid *ost_uuid = NULL;
872         int rc = 0, i;
873         ENTRY;
874
875         LASSERT(src_oa->o_valid & OBD_MD_FLFLAGS &&
876                 src_oa->o_flags == OBD_FL_DELORPHAN);
877
878         lov = &export->exp_obd->u.lov;
879
880         tmp_oa = obdo_alloc();
881         if (tmp_oa == NULL)
882                 RETURN(-ENOMEM);
883
884         if (src_oa->o_valid & OBD_MD_FLINLINE) {
885                 ost_uuid = (struct obd_uuid *)src_oa->o_inline;
886                 CDEBUG(D_HA, "clearing orphans only for %s\n",
887                        ost_uuid->uuid);
888         }
889
890         lov_getref(export->exp_obd);
891         for (i = 0; i < lov->desc.ld_tgt_count; i++) {
892                 struct lov_stripe_md obj_md;
893                 struct lov_stripe_md *obj_mdp = &obj_md;
894                 struct lov_tgt_desc *tgt;
895                 int err;
896
897                 tgt = lov->lov_tgts[i];
898                 if (!tgt)
899                         continue;
900
901                 /* if called for a specific target, we don't
902                    care if it is not active. */
903                 if (!lov->lov_tgts[i]->ltd_active == 0 && ost_uuid == NULL) {
904                         CDEBUG(D_HA, "lov idx %d inactive\n", i);
905                         continue;
906                 }
907
908                 if (ost_uuid && !obd_uuid_equals(ost_uuid, &tgt->ltd_uuid))
909                         continue;
910
911                 CDEBUG(D_CONFIG,"Clear orphans for %d:%s\n", i,
912                        obd_uuid2str(ost_uuid));
913
914                 memcpy(tmp_oa, src_oa, sizeof(*tmp_oa));
915
916                 LASSERT(lov->lov_tgts[i]->ltd_exp);
917                 /* XXX: LOV STACKING: use real "obj_mdp" sub-data */
918                 err = obd_create(lov->lov_tgts[i]->ltd_exp,
919                                  tmp_oa, &obj_mdp, oti);
920                 if (err)
921                         /* This export will be disabled until it is recovered,
922                            and then orphan recovery will be completed. */
923                         CERROR("error in orphan recovery on OST idx %d/%d: "
924                                "rc = %d\n", i, lov->desc.ld_tgt_count, err);
925
926                 if (ost_uuid)
927                         break;
928         }
929         lov_putref(export->exp_obd);
930
931         obdo_free(tmp_oa);
932         RETURN(rc);
933 }
934
935 static int lov_recreate(struct obd_export *exp, struct obdo *src_oa,
936                         struct lov_stripe_md **ea, struct obd_trans_info *oti)
937 {
938         struct lov_stripe_md *obj_mdp, *lsm;
939         struct lov_obd *lov = &exp->exp_obd->u.lov;
940         unsigned ost_idx;
941         int rc, i;
942         ENTRY;
943
944         LASSERT(src_oa->o_valid & OBD_MD_FLFLAGS &&
945                 src_oa->o_flags & OBD_FL_RECREATE_OBJS);
946
947         OBD_ALLOC(obj_mdp, sizeof(*obj_mdp));
948         if (obj_mdp == NULL)
949                 RETURN(-ENOMEM);
950
951         ost_idx = src_oa->o_nlink;
952         lsm = *ea;
953         if (lsm == NULL)
954                 GOTO(out, rc = -EINVAL);
955         if (ost_idx >= lov->desc.ld_tgt_count ||
956             !lov->lov_tgts[ost_idx])
957                 GOTO(out, rc = -EINVAL);
958
959         for (i = 0; i < lsm->lsm_stripe_count; i++) {
960                 if (lsm->lsm_oinfo[i].loi_ost_idx == ost_idx) {
961                         if (lsm->lsm_oinfo[i].loi_id != src_oa->o_id)
962                                 GOTO(out, rc = -EINVAL);
963                         break;
964                 }
965         }
966         if (i == lsm->lsm_stripe_count)
967                 GOTO(out, rc = -EINVAL);
968
969         rc = obd_create(lov->lov_tgts[ost_idx]->ltd_exp, src_oa, &obj_mdp, oti);
970 out:
971         OBD_FREE(obj_mdp, sizeof(*obj_mdp));
972         RETURN(rc);
973 }
974
975 /* the LOV expects oa->o_id to be set to the LOV object id */
976 static int lov_create(struct obd_export *exp, struct obdo *src_oa,
977                       struct lov_stripe_md **ea, struct obd_trans_info *oti)
978 {
979         struct lov_obd *lov;
980         struct obd_info oinfo;
981         struct lov_request_set *set = NULL;
982         struct lov_request *req;
983         struct obd_statfs osfs;
984         __u64 maxage;
985         int rc = 0;
986         ENTRY;
987
988         LASSERT(ea != NULL);
989         if (exp == NULL)
990                 RETURN(-EINVAL);
991
992         if ((src_oa->o_valid & OBD_MD_FLFLAGS) &&
993             src_oa->o_flags == OBD_FL_DELORPHAN) {
994                 rc = lov_clear_orphans(exp, src_oa, ea, oti);
995                 RETURN(rc);
996         }
997
998         lov = &exp->exp_obd->u.lov;
999         if (!lov->desc.ld_active_tgt_count)
1000                 RETURN(-EIO);
1001
1002         /* Recreate a specific object id at the given OST index */
1003         if ((src_oa->o_valid & OBD_MD_FLFLAGS) &&
1004             (src_oa->o_flags & OBD_FL_RECREATE_OBJS)) {
1005                  rc = lov_recreate(exp, src_oa, ea, oti);
1006                  RETURN(rc);
1007         }
1008
1009         maxage = cfs_time_shift_64(-lov->desc.ld_qos_maxage);
1010         obd_statfs_rqset(exp->exp_obd, &osfs, maxage);
1011
1012         rc = lov_prep_create_set(exp, &oinfo, ea, src_oa, oti, &set);
1013         if (rc)
1014                 RETURN(rc);
1015
1016         list_for_each_entry(req, &set->set_list, rq_link) {
1017                 /* XXX: LOV STACKING: use real "obj_mdp" sub-data */
1018                 rc = obd_create(lov->lov_tgts[req->rq_idx]->ltd_exp,
1019                                 req->rq_oi.oi_oa, &req->rq_oi.oi_md, oti);
1020                 lov_update_create_set(set, req, rc);
1021         }
1022         rc = lov_fini_create_set(set, ea);
1023         RETURN(rc);
1024 }
1025
1026 #define ASSERT_LSM_MAGIC(lsmp)                                                  \
1027 do {                                                                            \
1028         LASSERT((lsmp) != NULL);                                                \
1029         LASSERTF(((lsmp)->lsm_magic == LOV_MAGIC ||                             \
1030                  (lsmp)->lsm_magic == LOV_MAGIC_JOIN), "%p->lsm_magic=%x\n",    \
1031                  (lsmp), (lsmp)->lsm_magic);                                    \
1032 } while (0)
1033
1034 static int lov_destroy(struct obd_export *exp, struct obdo *oa,
1035                        struct lov_stripe_md *lsm, struct obd_trans_info *oti,
1036                        struct obd_export *md_exp)
1037 {
1038         struct lov_request_set *set;
1039         struct obd_info oinfo;
1040         struct lov_request *req;
1041         struct list_head *pos;
1042         struct lov_obd *lov;
1043         int rc = 0, err;
1044         ENTRY;
1045
1046         ASSERT_LSM_MAGIC(lsm);
1047
1048         if (!exp || !exp->exp_obd)
1049                 RETURN(-ENODEV);
1050
1051         if (oa->o_valid & OBD_MD_FLCOOKIE) {
1052                 LASSERT(oti);
1053                 LASSERT(oti->oti_logcookies);
1054         }
1055
1056         lov = &exp->exp_obd->u.lov;
1057         rc = lov_prep_destroy_set(exp, &oinfo, oa, lsm, oti, &set);
1058         if (rc)
1059                 RETURN(rc);
1060
1061         list_for_each (pos, &set->set_list) {
1062                 int err;
1063                 req = list_entry(pos, struct lov_request, rq_link);
1064
1065                 if (oa->o_valid & OBD_MD_FLCOOKIE)
1066                         oti->oti_logcookies = set->set_cookies + req->rq_stripe;
1067
1068                 err = obd_destroy(lov->lov_tgts[req->rq_idx]->ltd_exp,
1069                                   req->rq_oi.oi_oa, NULL, oti, NULL);
1070                 err = lov_update_common_set(set, req, err);
1071                 if (err) {
1072                         CERROR("error: destroying objid "LPX64" subobj "
1073                                LPX64" on OST idx %d: rc = %d\n",
1074                                oa->o_id, req->rq_oi.oi_oa->o_id,
1075                                req->rq_idx, err);
1076                         if (!rc)
1077                                 rc = err;
1078                 }
1079         }
1080
1081         if (rc == 0) {
1082                 LASSERT(lsm_op_find(lsm->lsm_magic) != NULL);
1083                 rc = lsm_op_find(lsm->lsm_magic)->lsm_destroy(lsm, oa, md_exp);
1084         }
1085         err = lov_fini_destroy_set(set);
1086         RETURN(rc ? rc : err);
1087 }
1088
1089 static int lov_getattr(struct obd_export *exp, struct obd_info *oinfo)
1090 {
1091         struct lov_request_set *set;
1092         struct lov_request *req;
1093         struct list_head *pos;
1094         struct lov_obd *lov;
1095         int err = 0, rc = 0;
1096         ENTRY;
1097
1098         LASSERT(oinfo);
1099         ASSERT_LSM_MAGIC(oinfo->oi_md);
1100
1101         if (!exp || !exp->exp_obd)
1102                 RETURN(-ENODEV);
1103
1104         lov = &exp->exp_obd->u.lov;
1105
1106         rc = lov_prep_getattr_set(exp, oinfo, &set);
1107         if (rc)
1108                 RETURN(rc);
1109
1110         list_for_each (pos, &set->set_list) {
1111                 req = list_entry(pos, struct lov_request, rq_link);
1112
1113                 CDEBUG(D_INFO, "objid "LPX64"[%d] has subobj "LPX64" at idx "
1114                        "%u\n", oinfo->oi_oa->o_id, req->rq_stripe,
1115                        req->rq_oi.oi_oa->o_id, req->rq_idx);
1116
1117                 rc = obd_getattr(lov->lov_tgts[req->rq_idx]->ltd_exp,
1118                                  &req->rq_oi);
1119                 err = lov_update_common_set(set, req, rc);
1120                 if (err) {
1121                         CERROR("error: getattr objid "LPX64" subobj "
1122                                LPX64" on OST idx %d: rc = %d\n",
1123                                oinfo->oi_oa->o_id, req->rq_oi.oi_oa->o_id,
1124                                req->rq_idx, err);
1125                         break;
1126                 }
1127         }
1128
1129         rc = lov_fini_getattr_set(set);
1130         if (err)
1131                 rc = err;
1132         RETURN(rc);
1133 }
1134
1135 static int lov_getattr_interpret(struct ptlrpc_request_set *rqset,
1136                                  void *data, int rc)
1137 {
1138         struct lov_request_set *lovset = (struct lov_request_set *)data;
1139         int err;
1140         ENTRY;
1141
1142         /* don't do attribute merge if this aysnc op failed */
1143         if (rc)
1144                 lovset->set_completes = 0;
1145         err = lov_fini_getattr_set(lovset);
1146         RETURN(rc ? rc : err);
1147 }
1148
1149 static int lov_getattr_async(struct obd_export *exp, struct obd_info *oinfo,
1150                               struct ptlrpc_request_set *rqset)
1151 {
1152         struct lov_request_set *lovset;
1153         struct lov_obd *lov;
1154         struct list_head *pos;
1155         struct lov_request *req;
1156         int rc = 0, err;
1157         ENTRY;
1158
1159         LASSERT(oinfo);
1160         ASSERT_LSM_MAGIC(oinfo->oi_md);
1161
1162         if (!exp || !exp->exp_obd)
1163                 RETURN(-ENODEV);
1164
1165         lov = &exp->exp_obd->u.lov;
1166
1167         rc = lov_prep_getattr_set(exp, oinfo, &lovset);
1168         if (rc)
1169                 RETURN(rc);
1170
1171         CDEBUG(D_INFO, "objid "LPX64": %ux%u byte stripes\n",
1172                oinfo->oi_md->lsm_object_id, oinfo->oi_md->lsm_stripe_count,
1173                oinfo->oi_md->lsm_stripe_size);
1174
1175         list_for_each (pos, &lovset->set_list) {
1176                 req = list_entry(pos, struct lov_request, rq_link);
1177
1178                 CDEBUG(D_INFO, "objid "LPX64"[%d] has subobj "LPX64" at idx "
1179                        "%u\n", oinfo->oi_oa->o_id, req->rq_stripe,
1180                        req->rq_oi.oi_oa->o_id, req->rq_idx);
1181                 rc = obd_getattr_async(lov->lov_tgts[req->rq_idx]->ltd_exp,
1182                                        &req->rq_oi, rqset);
1183                 if (rc) {
1184                         CERROR("error: getattr objid "LPX64" subobj "
1185                                LPX64" on OST idx %d: rc = %d\n",
1186                                oinfo->oi_oa->o_id, req->rq_oi.oi_oa->o_id,
1187                                req->rq_idx, rc);
1188                         GOTO(out, rc);
1189                 }
1190         }
1191
1192         if (!list_empty(&rqset->set_requests)) {
1193                 LASSERT(rc == 0);
1194                 LASSERT (rqset->set_interpret == NULL);
1195                 rqset->set_interpret = lov_getattr_interpret;
1196                 rqset->set_arg = (void *)lovset;
1197                 RETURN(rc);
1198         }
1199 out:
1200         if (rc)
1201                 lovset->set_completes = 0;
1202         err = lov_fini_getattr_set(lovset);
1203         RETURN(rc ? rc : err);
1204 }
1205
1206 static int lov_setattr(struct obd_export *exp, struct obd_info *oinfo,
1207                        struct obd_trans_info *oti)
1208 {
1209         struct lov_request_set *set;
1210         struct lov_obd *lov;
1211         struct list_head *pos;
1212         struct lov_request *req;
1213         int err = 0, rc = 0;
1214         ENTRY;
1215
1216         LASSERT(oinfo);
1217         ASSERT_LSM_MAGIC(oinfo->oi_md);
1218
1219         if (!exp || !exp->exp_obd)
1220                 RETURN(-ENODEV);
1221
1222         /* for now, we only expect the following updates here */
1223         LASSERT(!(oinfo->oi_oa->o_valid & ~(OBD_MD_FLID | OBD_MD_FLTYPE |
1224                                             OBD_MD_FLMODE | OBD_MD_FLATIME |
1225                                             OBD_MD_FLMTIME | OBD_MD_FLCTIME |
1226                                             OBD_MD_FLFLAGS | OBD_MD_FLSIZE |
1227                                             OBD_MD_FLGROUP | OBD_MD_FLUID |
1228                                             OBD_MD_FLGID | OBD_MD_FLINLINE |
1229                                             OBD_MD_FLFID | OBD_MD_FLGENER)));
1230         lov = &exp->exp_obd->u.lov;
1231         rc = lov_prep_setattr_set(exp, oinfo, oti, &set);
1232         if (rc)
1233                 RETURN(rc);
1234
1235         list_for_each (pos, &set->set_list) {
1236                 req = list_entry(pos, struct lov_request, rq_link);
1237
1238                 rc = obd_setattr(lov->lov_tgts[req->rq_idx]->ltd_exp,
1239                                  &req->rq_oi, NULL);
1240                 err = lov_update_setattr_set(set, req, rc);
1241                 if (err) {
1242                         CERROR("error: setattr objid "LPX64" subobj "
1243                                LPX64" on OST idx %d: rc = %d\n",
1244                                set->set_oi->oi_oa->o_id,
1245                                req->rq_oi.oi_oa->o_id, req->rq_idx, err);
1246                         if (!rc)
1247                                 rc = err;
1248                 }
1249         }
1250         err = lov_fini_setattr_set(set);
1251         if (!rc)
1252                 rc = err;
1253         RETURN(rc);
1254 }
1255
1256 static int lov_setattr_interpret(struct ptlrpc_request_set *rqset,
1257                                  void *data, int rc)
1258 {
1259         struct lov_request_set *lovset = (struct lov_request_set *)data;
1260         int err;
1261         ENTRY;
1262
1263         if (rc)
1264                 lovset->set_completes = 0;
1265         err = lov_fini_setattr_set(lovset);
1266         RETURN(rc ? rc : err);
1267 }
1268
1269 /* If @oti is given, the request goes from MDS and responses from OSTs are not
1270    needed. Otherwise, a client is waiting for responses. */
1271 static int lov_setattr_async(struct obd_export *exp, struct obd_info *oinfo,
1272                              struct obd_trans_info *oti,
1273                              struct ptlrpc_request_set *rqset)
1274 {
1275         struct lov_request_set *set;
1276         struct lov_request *req;
1277         struct list_head *pos;
1278         struct lov_obd *lov;
1279         int rc = 0;
1280         ENTRY;
1281
1282         LASSERT(oinfo);
1283         ASSERT_LSM_MAGIC(oinfo->oi_md);
1284         if (oinfo->oi_oa->o_valid & OBD_MD_FLCOOKIE) {
1285                 LASSERT(oti);
1286                 LASSERT(oti->oti_logcookies);
1287         }
1288
1289         if (!exp || !exp->exp_obd)
1290                 RETURN(-ENODEV);
1291
1292         lov = &exp->exp_obd->u.lov;
1293         rc = lov_prep_setattr_set(exp, oinfo, oti, &set);
1294         if (rc)
1295                 RETURN(rc);
1296
1297         CDEBUG(D_INFO, "objid "LPX64": %ux%u byte stripes\n",
1298                oinfo->oi_md->lsm_object_id, oinfo->oi_md->lsm_stripe_count,
1299                oinfo->oi_md->lsm_stripe_size);
1300
1301         list_for_each (pos, &set->set_list) {
1302                 req = list_entry(pos, struct lov_request, rq_link);
1303
1304                 if (oinfo->oi_oa->o_valid & OBD_MD_FLCOOKIE)
1305                         oti->oti_logcookies = set->set_cookies + req->rq_stripe;
1306
1307                 CDEBUG(D_INFO, "objid "LPX64"[%d] has subobj "LPX64" at idx "
1308                        "%u\n", oinfo->oi_oa->o_id, req->rq_stripe,
1309                        req->rq_oi.oi_oa->o_id, req->rq_idx);
1310
1311                 rc = obd_setattr_async(lov->lov_tgts[req->rq_idx]->ltd_exp,
1312                                        &req->rq_oi, oti, rqset);
1313                 if (rc) {
1314                         CERROR("error: setattr objid "LPX64" subobj "
1315                                LPX64" on OST idx %d: rc = %d\n",
1316                                set->set_oi->oi_oa->o_id,
1317                                req->rq_oi.oi_oa->o_id,
1318                                req->rq_idx, rc);
1319                         break;
1320                 }
1321         }
1322
1323         /* If we are not waiting for responses on async requests, return. */
1324         if (rc || !rqset || list_empty(&rqset->set_requests)) {
1325                 int err;
1326                 if (rc)
1327                         set->set_completes = 0;
1328                 err = lov_fini_setattr_set(set);
1329                 RETURN(rc ? rc : err);
1330         }
1331
1332         LASSERT(rqset->set_interpret == NULL);
1333         rqset->set_interpret = lov_setattr_interpret;
1334         rqset->set_arg = (void *)set;
1335
1336         RETURN(0);
1337 }
1338
1339 static int lov_punch_interpret(struct ptlrpc_request_set *rqset,
1340                                void *data, int rc)
1341 {
1342         struct lov_request_set *lovset = (struct lov_request_set *)data;
1343         int err;
1344         ENTRY;
1345
1346         if (rc)
1347                 lovset->set_completes = 0;
1348         err = lov_fini_punch_set(lovset);
1349         RETURN(rc ? rc : err);
1350 }
1351
1352 /* FIXME: maybe we'll just make one node the authoritative attribute node, then
1353  * we can send this 'punch' to just the authoritative node and the nodes
1354  * that the punch will affect. */
1355 static int lov_punch(struct obd_export *exp, struct obd_info *oinfo,
1356                      struct obd_trans_info *oti,
1357                      struct ptlrpc_request_set *rqset)
1358 {
1359         struct lov_request_set *set;
1360         struct lov_obd *lov;
1361         struct list_head *pos;
1362         struct lov_request *req;
1363         int rc = 0;
1364         ENTRY;
1365
1366         LASSERT(oinfo);
1367         ASSERT_LSM_MAGIC(oinfo->oi_md);
1368
1369         if (!exp || !exp->exp_obd)
1370                 RETURN(-ENODEV);
1371
1372         lov = &exp->exp_obd->u.lov;
1373         rc = lov_prep_punch_set(exp, oinfo, oti, &set);
1374         if (rc)
1375                 RETURN(rc);
1376
1377         list_for_each (pos, &set->set_list) {
1378                 req = list_entry(pos, struct lov_request, rq_link);
1379
1380                 rc = obd_punch(lov->lov_tgts[req->rq_idx]->ltd_exp,
1381                                &req->rq_oi, NULL, rqset);
1382                 if (rc) {
1383                         CERROR("error: punch objid "LPX64" subobj "LPX64
1384                                " on OST idx %d: rc = %d\n",
1385                                set->set_oi->oi_oa->o_id,
1386                                req->rq_oi.oi_oa->o_id, req->rq_idx, rc);
1387                         break;
1388                 }
1389         }
1390
1391         if (rc || list_empty(&rqset->set_requests)) {
1392                 int err;
1393                 err = lov_fini_punch_set(set);
1394                 RETURN(rc ? rc : err);
1395         }
1396
1397         LASSERT(rqset->set_interpret == NULL);
1398         rqset->set_interpret = lov_punch_interpret;
1399         rqset->set_arg = (void *)set;
1400
1401         RETURN(0);
1402 }
1403
1404 static int lov_sync(struct obd_export *exp, struct obdo *oa,
1405                     struct lov_stripe_md *lsm, obd_off start, obd_off end,
1406                     void *capa)
1407 {
1408         struct lov_request_set *set;
1409         struct obd_info oinfo;
1410         struct lov_obd *lov;
1411         struct list_head *pos;
1412         struct lov_request *req;
1413         int err = 0, rc = 0;
1414         ENTRY;
1415
1416         ASSERT_LSM_MAGIC(lsm);
1417
1418         if (!exp->exp_obd)
1419                 RETURN(-ENODEV);
1420
1421         lov = &exp->exp_obd->u.lov;
1422         rc = lov_prep_sync_set(exp, &oinfo, oa, lsm, start, end, &set);
1423         if (rc)
1424                 RETURN(rc);
1425
1426         list_for_each (pos, &set->set_list) {
1427                 req = list_entry(pos, struct lov_request, rq_link);
1428
1429                 rc = obd_sync(lov->lov_tgts[req->rq_idx]->ltd_exp,
1430                               req->rq_oi.oi_oa, NULL,
1431                               req->rq_oi.oi_policy.l_extent.start,
1432                               req->rq_oi.oi_policy.l_extent.end, capa);
1433                 err = lov_update_common_set(set, req, rc);
1434                 if (err) {
1435                         CERROR("error: fsync objid "LPX64" subobj "LPX64
1436                                " on OST idx %d: rc = %d\n",
1437                                set->set_oi->oi_oa->o_id,
1438                                req->rq_oi.oi_oa->o_id, req->rq_idx, rc);
1439                         if (!rc)
1440                                 rc = err;
1441                 }
1442         }
1443         err = lov_fini_sync_set(set);
1444         if (!rc)
1445                 rc = err;
1446         RETURN(rc);
1447 }
1448
1449 static int lov_brw_check(struct lov_obd *lov, struct obd_info *lov_oinfo,
1450                          obd_count oa_bufs, struct brw_page *pga)
1451 {
1452         struct obd_info oinfo = { { { 0 } } };
1453         int i, rc = 0;
1454
1455         oinfo.oi_oa = lov_oinfo->oi_oa;
1456
1457         /* The caller just wants to know if there's a chance that this
1458          * I/O can succeed */
1459         for (i = 0; i < oa_bufs; i++) {
1460                 int stripe = lov_stripe_number(lov_oinfo->oi_md, pga[i].off);
1461                 int ost = lov_oinfo->oi_md->lsm_oinfo[stripe].loi_ost_idx;
1462                 obd_off start, end;
1463
1464                 if (!lov_stripe_intersects(lov_oinfo->oi_md, i, pga[i].off,
1465                                            pga[i].off + pga[i].count,
1466                                            &start, &end))
1467                         continue;
1468
1469                 if (!lov->lov_tgts[ost] || !lov->lov_tgts[ost]->ltd_active) {
1470                         CDEBUG(D_HA, "lov idx %d inactive\n", ost);
1471                         return -EIO;
1472                 }
1473
1474                 rc = obd_brw(OBD_BRW_CHECK, lov->lov_tgts[ost]->ltd_exp, &oinfo,
1475                              1, &pga[i], NULL);
1476                 if (rc)
1477                         break;
1478         }
1479         return rc;
1480 }
1481
1482 static int lov_brw(int cmd, struct obd_export *exp, struct obd_info *oinfo,
1483                    obd_count oa_bufs, struct brw_page *pga,
1484                    struct obd_trans_info *oti)
1485 {
1486         struct lov_request_set *set;
1487         struct lov_request *req;
1488         struct list_head *pos;
1489         struct lov_obd *lov = &exp->exp_obd->u.lov;
1490         int err, rc = 0;
1491         ENTRY;
1492
1493         ASSERT_LSM_MAGIC(oinfo->oi_md);
1494
1495         if (cmd == OBD_BRW_CHECK) {
1496                 rc = lov_brw_check(lov, oinfo, oa_bufs, pga);
1497                 RETURN(rc);
1498         }
1499
1500         rc = lov_prep_brw_set(exp, oinfo, oa_bufs, pga, oti, &set);
1501         if (rc)
1502                 RETURN(rc);
1503
1504         list_for_each (pos, &set->set_list) {
1505                 struct obd_export *sub_exp;
1506                 struct brw_page *sub_pga;
1507                 req = list_entry(pos, struct lov_request, rq_link);
1508
1509                 sub_exp = lov->lov_tgts[req->rq_idx]->ltd_exp;
1510                 sub_pga = set->set_pga + req->rq_pgaidx;
1511                 rc = obd_brw(cmd, sub_exp, &req->rq_oi, req->rq_oabufs,
1512                              sub_pga, oti);
1513                 if (rc)
1514                         break;
1515                 lov_update_common_set(set, req, rc);
1516         }
1517
1518         err = lov_fini_brw_set(set);
1519         if (!rc)
1520                 rc = err;
1521         RETURN(rc);
1522 }
1523
1524 static int lov_brw_interpret(struct ptlrpc_request_set *reqset, void *data,
1525                              int rc)
1526 {
1527         struct lov_request_set *lovset = (struct lov_request_set *)data;
1528         ENTRY;
1529
1530         if (rc) {
1531                 lovset->set_completes = 0;
1532                 lov_fini_brw_set(lovset);
1533         } else {
1534                 rc = lov_fini_brw_set(lovset);
1535         }
1536
1537         RETURN(rc);
1538 }
1539
1540 static int lov_brw_async(int cmd, struct obd_export *exp,
1541                          struct obd_info *oinfo, obd_count oa_bufs,
1542                          struct brw_page *pga, struct obd_trans_info *oti,
1543                          struct ptlrpc_request_set *set)
1544 {
1545         struct lov_request_set *lovset;
1546         struct lov_request *req;
1547         struct list_head *pos;
1548         struct lov_obd *lov = &exp->exp_obd->u.lov;
1549         int rc = 0;
1550         ENTRY;
1551
1552         LASSERT(oinfo);
1553         ASSERT_LSM_MAGIC(oinfo->oi_md);
1554
1555         if (cmd == OBD_BRW_CHECK) {
1556                 rc = lov_brw_check(lov, oinfo, oa_bufs, pga);
1557                 RETURN(rc);
1558         }
1559
1560         rc = lov_prep_brw_set(exp, oinfo, oa_bufs, pga, oti, &lovset);
1561         if (rc)
1562                 RETURN(rc);
1563
1564         list_for_each (pos, &lovset->set_list) {
1565                 struct obd_export *sub_exp;
1566                 struct brw_page *sub_pga;
1567                 req = list_entry(pos, struct lov_request, rq_link);
1568
1569                 sub_exp = lov->lov_tgts[req->rq_idx]->ltd_exp;
1570                 sub_pga = lovset->set_pga + req->rq_pgaidx;
1571                 rc = obd_brw_async(cmd, sub_exp, &req->rq_oi, req->rq_oabufs,
1572                                    sub_pga, oti, set);
1573                 if (rc)
1574                         GOTO(out, rc);
1575                 lov_update_common_set(lovset, req, rc);
1576         }
1577         LASSERT(rc == 0);
1578         LASSERT(set->set_interpret == NULL);
1579         set->set_interpret = (set_interpreter_func)lov_brw_interpret;
1580         set->set_arg = (void *)lovset;
1581
1582         RETURN(rc);
1583 out:
1584         lov_fini_brw_set(lovset);
1585         RETURN(rc);
1586 }
1587
1588 static int lov_ap_make_ready(void *data, int cmd)
1589 {
1590         struct lov_async_page *lap = LAP_FROM_COOKIE(data);
1591
1592         return lap->lap_caller_ops->ap_make_ready(lap->lap_caller_data, cmd);
1593 }
1594
1595 static int lov_ap_refresh_count(void *data, int cmd)
1596 {
1597         struct lov_async_page *lap = LAP_FROM_COOKIE(data);
1598
1599         return lap->lap_caller_ops->ap_refresh_count(lap->lap_caller_data,
1600                                                      cmd);
1601 }
1602
1603 static void lov_ap_fill_obdo(void *data, int cmd, struct obdo *oa)
1604 {
1605         struct lov_async_page *lap = LAP_FROM_COOKIE(data);
1606
1607         lap->lap_caller_ops->ap_fill_obdo(lap->lap_caller_data, cmd, oa);
1608         /* XXX woah, shouldn't we be altering more here?  size? */
1609         oa->o_id = lap->lap_loi_id;
1610         oa->o_gr = lap->lap_loi_gr;
1611         oa->o_valid |= OBD_MD_FLGROUP;
1612         oa->o_stripe_idx = lap->lap_stripe;
1613 }
1614
1615 static void lov_ap_update_obdo(void *data, int cmd, struct obdo *oa,
1616                                obd_valid valid)
1617 {
1618         struct lov_async_page *lap = LAP_FROM_COOKIE(data);
1619
1620         lap->lap_caller_ops->ap_update_obdo(lap->lap_caller_data, cmd,oa,valid);
1621 }
1622
1623 static int lov_ap_completion(void *data, int cmd, struct obdo *oa, int rc)
1624 {
1625         struct lov_async_page *lap = LAP_FROM_COOKIE(data);
1626
1627         /* in a raid1 regime this would down a count of many ios
1628          * in flight, onl calling the caller_ops completion when all
1629          * the raid1 ios are complete */
1630         rc = lap->lap_caller_ops->ap_completion(lap->lap_caller_data,cmd,oa,rc);
1631         return rc;
1632 }
1633
1634 static struct obd_capa *lov_ap_lookup_capa(void *data, int cmd)
1635 {
1636         struct lov_async_page *lap = LAP_FROM_COOKIE(data);
1637
1638         return lap->lap_caller_ops->ap_lookup_capa(lap->lap_caller_data, cmd);
1639 }
1640
1641 static struct obd_async_page_ops lov_async_page_ops = {
1642         .ap_make_ready =        lov_ap_make_ready,
1643         .ap_refresh_count =     lov_ap_refresh_count,
1644         .ap_fill_obdo =         lov_ap_fill_obdo,
1645         .ap_update_obdo =       lov_ap_update_obdo,
1646         .ap_completion =        lov_ap_completion,
1647         .ap_lookup_capa =       lov_ap_lookup_capa,
1648 };
1649
1650 int lov_prep_async_page(struct obd_export *exp, struct lov_stripe_md *lsm,
1651                            struct lov_oinfo *loi, cfs_page_t *page,
1652                            obd_off offset, struct obd_async_page_ops *ops,
1653                            void *data, void **res)
1654 {
1655         struct lov_obd *lov = &exp->exp_obd->u.lov;
1656         struct lov_async_page *lap;
1657         int rc = 0;
1658         ENTRY;
1659
1660         if (!page) {
1661                 int i = 0;
1662                 /* Find an existing osc so we can get it's stupid sizeof(*oap).
1663                    Only because of this layering limitation will a client
1664                    mount with no osts fail */
1665                 while (!lov->lov_tgts || !lov->lov_tgts[i] ||
1666                        !lov->lov_tgts[i]->ltd_exp) {
1667                         i++;
1668                         if (i >= lov->desc.ld_tgt_count)
1669                                 RETURN(-ENOMEDIUM);
1670                 }
1671                 rc = size_round(sizeof(*lap)) +
1672                         obd_prep_async_page(lov->lov_tgts[i]->ltd_exp, NULL,
1673                                             NULL, NULL, 0, NULL, NULL, NULL);
1674                 RETURN(rc);
1675         }
1676         ASSERT_LSM_MAGIC(lsm);
1677         LASSERT(loi == NULL);
1678
1679         lap = *res;
1680         lap->lap_magic = LOV_AP_MAGIC;
1681         lap->lap_caller_ops = ops;
1682         lap->lap_caller_data = data;
1683
1684         /* for now only raid 0 which passes through */
1685         lap->lap_stripe = lov_stripe_number(lsm, offset);
1686         lov_stripe_offset(lsm, offset, lap->lap_stripe, &lap->lap_sub_offset);
1687         loi = &lsm->lsm_oinfo[lap->lap_stripe];
1688
1689         /* so the callback doesn't need the lsm */
1690         lap->lap_loi_id = loi->loi_id;
1691         lap->lap_loi_gr = lsm->lsm_object_gr;
1692         LASSERT(lsm->lsm_object_gr > 0);
1693         
1694         lap->lap_sub_cookie = (void *)lap + size_round(sizeof(*lap));
1695
1696         rc = obd_prep_async_page(lov->lov_tgts[loi->loi_ost_idx]->ltd_exp,
1697                                  lsm, loi, page, lap->lap_sub_offset,
1698                                  &lov_async_page_ops, lap,
1699                                  &lap->lap_sub_cookie);
1700         if (rc)
1701                 RETURN(rc);
1702         CDEBUG(D_CACHE, "lap %p page %p cookie %p off "LPU64"\n", lap, page,
1703                lap->lap_sub_cookie, offset);
1704         RETURN(0);
1705 }
1706
1707 static int lov_queue_async_io(struct obd_export *exp,
1708                               struct lov_stripe_md *lsm,
1709                               struct lov_oinfo *loi, void *cookie,
1710                               int cmd, obd_off off, int count,
1711                               obd_flag brw_flags, obd_flag async_flags)
1712 {
1713         struct lov_obd *lov = &exp->exp_obd->u.lov;
1714         struct lov_async_page *lap;
1715         int rc;
1716
1717         LASSERT(loi == NULL);
1718
1719         ASSERT_LSM_MAGIC(lsm);
1720
1721         lap = LAP_FROM_COOKIE(cookie);
1722
1723         loi = &lsm->lsm_oinfo[lap->lap_stripe];
1724
1725         rc = obd_queue_async_io(lov->lov_tgts[loi->loi_ost_idx]->ltd_exp, lsm,
1726                                 loi, lap->lap_sub_cookie, cmd, off, count,
1727                                 brw_flags, async_flags);
1728         RETURN(rc);
1729 }
1730
1731 static int lov_set_async_flags(struct obd_export *exp,
1732                                struct lov_stripe_md *lsm,
1733                                struct lov_oinfo *loi, void *cookie,
1734                                obd_flag async_flags)
1735 {
1736         struct lov_obd *lov = &exp->exp_obd->u.lov;
1737         struct lov_async_page *lap;
1738         int rc;
1739
1740         LASSERT(loi == NULL);
1741
1742         ASSERT_LSM_MAGIC(lsm);
1743
1744         lap = LAP_FROM_COOKIE(cookie);
1745
1746         loi = &lsm->lsm_oinfo[lap->lap_stripe];
1747
1748         rc = obd_set_async_flags(lov->lov_tgts[loi->loi_ost_idx]->ltd_exp,
1749                                  lsm, loi, lap->lap_sub_cookie, async_flags);
1750         RETURN(rc);
1751 }
1752
1753 static int lov_queue_group_io(struct obd_export *exp,
1754                               struct lov_stripe_md *lsm,
1755                               struct lov_oinfo *loi,
1756                               struct obd_io_group *oig, void *cookie,
1757                               int cmd, obd_off off, int count,
1758                               obd_flag brw_flags, obd_flag async_flags)
1759 {
1760         struct lov_obd *lov = &exp->exp_obd->u.lov;
1761         struct lov_async_page *lap;
1762         int rc;
1763
1764         LASSERT(loi == NULL);
1765
1766         ASSERT_LSM_MAGIC(lsm);
1767
1768         lap = LAP_FROM_COOKIE(cookie);
1769
1770         loi = &lsm->lsm_oinfo[lap->lap_stripe];
1771
1772         rc = obd_queue_group_io(lov->lov_tgts[loi->loi_ost_idx]->ltd_exp, lsm,
1773                                 loi, oig, lap->lap_sub_cookie, cmd, off, count,
1774                                 brw_flags, async_flags);
1775         RETURN(rc);
1776 }
1777
1778 /* this isn't exactly optimal.  we may have queued sync io in oscs on
1779  * all stripes, but we don't record that fact at queue time.  so we
1780  * trigger sync io on all stripes. */
1781 static int lov_trigger_group_io(struct obd_export *exp,
1782                                 struct lov_stripe_md *lsm,
1783                                 struct lov_oinfo *loi,
1784                                 struct obd_io_group *oig)
1785 {
1786         struct lov_obd *lov = &exp->exp_obd->u.lov;
1787         int rc = 0, i, err;
1788
1789         LASSERT(loi == NULL);
1790
1791         ASSERT_LSM_MAGIC(lsm);
1792
1793         loi = lsm->lsm_oinfo;
1794         for (i = 0; i < lsm->lsm_stripe_count; i++, loi++) {
1795                 if (!lov->lov_tgts[loi->loi_ost_idx] ||
1796                     !lov->lov_tgts[loi->loi_ost_idx]->ltd_active) {
1797                         CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
1798                         continue;
1799                 }
1800
1801                 err = obd_trigger_group_io(lov->lov_tgts[loi->loi_ost_idx]->ltd_exp,
1802                                            lsm, loi, oig);
1803                 if (rc == 0 && err != 0)
1804                         rc = err;
1805         };
1806         RETURN(rc);
1807 }
1808
1809 static int lov_teardown_async_page(struct obd_export *exp,
1810                                    struct lov_stripe_md *lsm,
1811                                    struct lov_oinfo *loi, void *cookie)
1812 {
1813         struct lov_obd *lov = &exp->exp_obd->u.lov;
1814         struct lov_async_page *lap;
1815         int rc;
1816
1817         LASSERT(loi == NULL);
1818
1819         ASSERT_LSM_MAGIC(lsm);
1820
1821         lap = LAP_FROM_COOKIE(cookie);
1822
1823         loi = &lsm->lsm_oinfo[lap->lap_stripe];
1824
1825         rc = obd_teardown_async_page(lov->lov_tgts[loi->loi_ost_idx]->ltd_exp,
1826                                      lsm, loi, lap->lap_sub_cookie);
1827         if (rc) {
1828                 CERROR("unable to teardown sub cookie %p: %d\n",
1829                        lap->lap_sub_cookie, rc);
1830                 RETURN(rc);
1831         }
1832         RETURN(rc);
1833 }
1834
1835 static int lov_enqueue_interpret(struct ptlrpc_request_set *rqset,
1836                                  void *data, int rc)
1837 {
1838         struct lov_request_set *lovset = (struct lov_request_set *)data;
1839         ENTRY;
1840         rc = lov_fini_enqueue_set(lovset, lovset->set_ei->ei_mode, rc);
1841         RETURN(rc);
1842 }
1843
1844 static int lov_enqueue(struct obd_export *exp, struct obd_info *oinfo,
1845                        struct obd_enqueue_info *einfo)
1846 {
1847         struct lov_request_set *set;
1848         struct lov_request *req;
1849         struct list_head *pos;
1850         struct lov_obd *lov;
1851         ldlm_error_t rc;
1852         ENTRY;
1853
1854         LASSERT(oinfo);
1855         ASSERT_LSM_MAGIC(oinfo->oi_md);
1856
1857         /* we should never be asked to replay a lock this way. */
1858         LASSERT((einfo->ei_flags & LDLM_FL_REPLAY) == 0);
1859
1860         if (!exp || !exp->exp_obd)
1861                 RETURN(-ENODEV);
1862
1863         lov = &exp->exp_obd->u.lov;
1864         rc = lov_prep_enqueue_set(exp, oinfo, einfo, &set);
1865         if (rc)
1866                 RETURN(rc);
1867
1868         list_for_each (pos, &set->set_list) {
1869                 req = list_entry(pos, struct lov_request, rq_link);
1870
1871                 rc = obd_enqueue(lov->lov_tgts[req->rq_idx]->ltd_exp,
1872                                  &req->rq_oi, einfo);
1873                 if (rc != ELDLM_OK)
1874                         GOTO(out, rc);
1875         }
1876
1877         if (einfo->ei_rqset && !list_empty(&einfo->ei_rqset->set_requests)) {
1878                 LASSERT(rc == 0);
1879                 LASSERT(einfo->ei_rqset->set_interpret == NULL);
1880                 einfo->ei_rqset->set_interpret = lov_enqueue_interpret;
1881                 einfo->ei_rqset->set_arg = (void *)set;
1882                 RETURN(rc);
1883         }
1884 out:
1885         rc = lov_fini_enqueue_set(set, einfo->ei_mode, rc);
1886         RETURN(rc);
1887 }
1888
1889 static int lov_match(struct obd_export *exp, struct lov_stripe_md *lsm,
1890                      __u32 type, ldlm_policy_data_t *policy, __u32 mode,
1891                      int *flags, void *data, struct lustre_handle *lockh)
1892 {
1893         struct lov_request_set *set;
1894         struct obd_info oinfo;
1895         struct lov_request *req;
1896         struct list_head *pos;
1897         struct lov_obd *lov = &exp->exp_obd->u.lov;
1898         struct lustre_handle *lov_lockhp;
1899         int lov_flags, rc = 0;
1900         ENTRY;
1901
1902         ASSERT_LSM_MAGIC(lsm);
1903
1904         if (!exp || !exp->exp_obd)
1905                 RETURN(-ENODEV);
1906
1907         lov = &exp->exp_obd->u.lov;
1908         rc = lov_prep_match_set(exp, &oinfo, lsm, policy, mode, lockh, &set);
1909         if (rc)
1910                 RETURN(rc);
1911
1912         list_for_each (pos, &set->set_list) {
1913                 ldlm_policy_data_t sub_policy;
1914                 req = list_entry(pos, struct lov_request, rq_link);
1915                 lov_lockhp = set->set_lockh->llh_handles + req->rq_stripe;
1916                 LASSERT(lov_lockhp);
1917
1918                 lov_flags = *flags;
1919                 sub_policy.l_extent = req->rq_oi.oi_policy.l_extent;
1920
1921                 rc = obd_match(lov->lov_tgts[req->rq_idx]->ltd_exp,
1922                                req->rq_oi.oi_md, type, &sub_policy,
1923                                mode, &lov_flags, data, lov_lockhp);
1924                 rc = lov_update_match_set(set, req, rc);
1925                 if (rc != 1)
1926                         break;
1927         }
1928         lov_fini_match_set(set, mode, *flags);
1929         RETURN(rc);
1930 }
1931
1932 static int lov_change_cbdata(struct obd_export *exp,
1933                              struct lov_stripe_md *lsm, ldlm_iterator_t it,
1934                              void *data)
1935 {
1936         struct lov_obd *lov;
1937         struct lov_oinfo *loi;
1938         int rc = 0, i;
1939         ENTRY;
1940
1941         ASSERT_LSM_MAGIC(lsm);
1942
1943         if (!exp || !exp->exp_obd)
1944                 RETURN(-ENODEV);
1945
1946         LASSERT(lsm->lsm_object_gr > 0);
1947
1948         lov = &exp->exp_obd->u.lov;
1949         for (i = 0,loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++,loi++) {
1950                 struct lov_stripe_md submd;
1951
1952                 submd.lsm_object_id = loi->loi_id;
1953                 submd.lsm_object_gr = lsm->lsm_object_gr;
1954                 submd.lsm_stripe_count = 0;
1955                 rc = obd_change_cbdata(lov->lov_tgts[loi->loi_ost_idx]->ltd_exp,
1956                                        &submd, it, data);
1957         }
1958         RETURN(rc);
1959 }
1960
1961 static int lov_cancel(struct obd_export *exp, struct lov_stripe_md *lsm,
1962                       __u32 mode, struct lustre_handle *lockh)
1963 {
1964         struct lov_request_set *set;
1965         struct obd_info oinfo;
1966         struct lov_request *req;
1967         struct list_head *pos;
1968         struct lov_obd *lov = &exp->exp_obd->u.lov;
1969         struct lustre_handle *lov_lockhp;
1970         int err = 0, rc = 0;
1971         ENTRY;
1972
1973         ASSERT_LSM_MAGIC(lsm);
1974
1975         if (!exp || !exp->exp_obd)
1976                 RETURN(-ENODEV);
1977
1978         LASSERT(lsm->lsm_object_gr > 0);
1979         LASSERT(lockh);
1980         lov = &exp->exp_obd->u.lov;
1981         rc = lov_prep_cancel_set(exp, &oinfo, lsm, mode, lockh, &set);
1982         if (rc)
1983                 RETURN(rc);
1984
1985         list_for_each (pos, &set->set_list) {
1986                 req = list_entry(pos, struct lov_request, rq_link);
1987                 lov_lockhp = set->set_lockh->llh_handles + req->rq_stripe;
1988
1989                 rc = obd_cancel(lov->lov_tgts[req->rq_idx]->ltd_exp,
1990                                 req->rq_oi.oi_md, mode, lov_lockhp);
1991                 rc = lov_update_common_set(set, req, rc);
1992                 if (rc) {
1993                         CERROR("error: cancel objid "LPX64" subobj "
1994                                LPX64" on OST idx %d: rc = %d\n",
1995                                lsm->lsm_object_id,
1996                                req->rq_oi.oi_md->lsm_object_id,
1997                                req->rq_idx, rc);
1998                         err = rc;
1999                 }
2000
2001         }
2002         lov_fini_cancel_set(set);
2003         RETURN(err);
2004 }
2005
2006 static int lov_cancel_unused(struct obd_export *exp,
2007                              struct lov_stripe_md *lsm,
2008                              int flags, void *opaque)
2009 {
2010         struct lov_oinfo *loi;
2011         struct lov_obd *lov;
2012         int rc = 0, i;
2013         ENTRY;
2014
2015         if (!exp || !exp->exp_obd)
2016                 RETURN(-ENODEV);
2017
2018         lov = &exp->exp_obd->u.lov;
2019         if (lsm == NULL) {
2020                 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
2021                         int err;
2022                         if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_exp)
2023                                 continue;
2024
2025                         err = obd_cancel_unused(lov->lov_tgts[i]->ltd_exp, NULL,
2026                                                 flags, opaque);
2027                         if (!rc)
2028                                 rc = err;
2029                 }
2030                 RETURN(rc);
2031         }
2032
2033         ASSERT_LSM_MAGIC(lsm);
2034
2035         LASSERT(lsm->lsm_object_gr > 0);
2036         for (i = 0,loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++,loi++) {
2037                 struct lov_stripe_md submd;
2038                 int err;
2039
2040                 if (!lov->lov_tgts[loi->loi_ost_idx] ||
2041                     !lov->lov_tgts[loi->loi_ost_idx]->ltd_active)
2042                         CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
2043
2044                 submd.lsm_object_id = loi->loi_id;
2045                 submd.lsm_object_gr = lsm->lsm_object_gr;
2046                 submd.lsm_stripe_count = 0;
2047                 err = obd_cancel_unused(lov->lov_tgts[loi->loi_ost_idx]->ltd_exp,
2048                                         &submd, flags, opaque);
2049                 if (err && lov->lov_tgts[loi->loi_ost_idx]->ltd_active) {
2050                         CERROR("error: cancel unused objid "LPX64" subobj "LPX64
2051                                " on OST idx %d: rc = %d\n", lsm->lsm_object_id,
2052                                loi->loi_id, loi->loi_ost_idx, err);
2053                         if (!rc)
2054                                 rc = err;
2055                 }
2056         }
2057         RETURN(rc);
2058 }
2059
2060 static int lov_join_lru(struct obd_export *exp,
2061                         struct lov_stripe_md *lsm, int join)
2062 {
2063         struct lov_obd *lov;
2064         struct lov_oinfo *loi;
2065         int i, count = 0;
2066         ENTRY;
2067
2068         ASSERT_LSM_MAGIC(lsm);
2069         if (!exp || !exp->exp_obd)
2070                 RETURN(-ENODEV);
2071
2072         lov = &exp->exp_obd->u.lov;
2073         for (i = 0,loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++,loi++) {
2074                 struct lov_stripe_md submd;
2075                 int rc = 0;
2076
2077                 if (!lov->lov_tgts[loi->loi_ost_idx] ||
2078                     !lov->lov_tgts[loi->loi_ost_idx]->ltd_active)
2079                         CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
2080
2081                 submd.lsm_object_id = loi->loi_id;
2082                 submd.lsm_object_gr = lsm->lsm_object_gr;
2083                 submd.lsm_stripe_count = 0;
2084                 rc = obd_join_lru(lov->lov_tgts[loi->loi_ost_idx]->ltd_exp,
2085                                   &submd, join);
2086                 if (rc < 0) {
2087                         CERROR("join lru failed. objid: "LPX64" subobj: "LPX64
2088                                " ostidx: %d rc: %d\n", lsm->lsm_object_id,
2089                                loi->loi_id, loi->loi_ost_idx, rc);
2090                         return rc;
2091                 } else {
2092                         count += rc;
2093                 }
2094         }
2095         RETURN(count);
2096 }
2097
2098 static int lov_statfs_interpret(struct ptlrpc_request_set *rqset,
2099                                 void *data, int rc)
2100 {
2101         struct lov_request_set *lovset = (struct lov_request_set *)data;
2102         int err;
2103         ENTRY;
2104
2105         if (rc)
2106                 lovset->set_completes = 0;
2107
2108         err = lov_fini_statfs_set(lovset);
2109         RETURN(rc ? rc : err);
2110 }
2111
2112 static int lov_statfs_async(struct obd_device *obd, struct obd_info *oinfo,
2113                             __u64 max_age, struct ptlrpc_request_set *rqset)
2114 {
2115         struct lov_request_set *set;
2116         struct lov_request *req;
2117         struct list_head *pos;
2118         struct lov_obd *lov;
2119         int rc = 0;
2120
2121         LASSERT(oinfo != NULL);
2122         LASSERT(oinfo->oi_osfs != NULL);
2123
2124         lov = &obd->u.lov;
2125         rc = lov_prep_statfs_set(obd, oinfo, &set);
2126         if (rc)
2127                 RETURN(rc);
2128
2129         list_for_each (pos, &set->set_list) {
2130                 struct obd_device *osc_obd;
2131
2132                 req = list_entry(pos, struct lov_request, rq_link);
2133
2134                 osc_obd = class_exp2obd(lov->lov_tgts[req->rq_idx]->ltd_exp);
2135                 rc = obd_statfs_async(osc_obd, &req->rq_oi, max_age, rqset);
2136                 if (rc)
2137                         break;
2138         }
2139
2140         if (rc || list_empty(&rqset->set_requests)) {
2141                 int err;
2142                 if (rc)
2143                         set->set_completes = 0;
2144                 err = lov_fini_statfs_set(set);
2145                 RETURN(rc ? rc : err);
2146         }
2147
2148         LASSERT(rqset->set_interpret == NULL);
2149         rqset->set_interpret = lov_statfs_interpret;
2150         rqset->set_arg = (void *)set;
2151         RETURN(0);
2152 }
2153
2154 static int lov_statfs(struct obd_device *obd, struct obd_statfs *osfs,
2155                       __u64 max_age)
2156 {
2157         struct lov_obd *lov = &obd->u.lov;
2158         struct obd_statfs lov_sfs;
2159         int set = 0;
2160         int rc = 0, err;
2161         int i;
2162         ENTRY;
2163
2164         /* We only get block data from the OBD */
2165         for (i = 0; i < lov->desc.ld_tgt_count; i++) {
2166                 if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_active) {
2167                         CDEBUG(D_HA, "lov idx %d inactive\n", i);
2168                         continue;
2169                 }
2170
2171                 err = obd_statfs(class_exp2obd(lov->lov_tgts[i]->ltd_exp),
2172                                  &lov_sfs, max_age);
2173                 if (err) {
2174                         if (lov->lov_tgts[i]->ltd_active && !rc)
2175                                 rc = err;
2176                         continue;
2177                 }
2178                 qos_update(lov, i, &lov_sfs);
2179
2180                 lov_update_statfs(class_exp2obd(lov->lov_tgts[i]->ltd_exp),
2181                                   osfs, &lov_sfs, set);
2182                 set++;
2183         }
2184
2185         err = lov_fini_statfs(obd, osfs, set);
2186         RETURN(rc ? rc : err);
2187 }
2188
2189 static int lov_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
2190                          void *karg, void *uarg)
2191 {
2192         struct obd_device *obddev = class_exp2obd(exp);
2193         struct lov_obd *lov = &obddev->u.lov;
2194         int i, rc, count = lov->desc.ld_tgt_count;
2195         struct obd_uuid *uuidp;
2196         ENTRY;
2197
2198         switch (cmd) {
2199         case OBD_IOC_LOV_GET_CONFIG: {
2200                 struct obd_ioctl_data *data;
2201                 struct lov_desc *desc;
2202                 char *buf = NULL;
2203                 __u32 *genp;
2204
2205                 len = 0;
2206                 if (obd_ioctl_getdata(&buf, &len, (void *)uarg))
2207                         RETURN(-EINVAL);
2208
2209                 data = (struct obd_ioctl_data *)buf;
2210
2211                 if (sizeof(*desc) > data->ioc_inllen1) {
2212                         obd_ioctl_freedata(buf, len);
2213                         RETURN(-EINVAL);
2214                 }
2215
2216                 if (sizeof(uuidp->uuid) * count > data->ioc_inllen2) {
2217                         obd_ioctl_freedata(buf, len);
2218                         RETURN(-EINVAL);
2219                 }
2220
2221                 if (sizeof(__u32) * count > data->ioc_inllen3) {
2222                         obd_ioctl_freedata(buf, len);
2223                         RETURN(-EINVAL);
2224                 }
2225
2226                 desc = (struct lov_desc *)data->ioc_inlbuf1;
2227                 memcpy(desc, &(lov->desc), sizeof(*desc));
2228
2229                 uuidp = (struct obd_uuid *)data->ioc_inlbuf2;
2230                 genp = (__u32 *)data->ioc_inlbuf3;
2231                 /* the uuid will be empty for deleted OSTs */
2232                 for (i = 0; i < count; i++, uuidp++, genp++) {
2233                         if (!lov->lov_tgts[i])
2234                                 continue;
2235                         *uuidp = lov->lov_tgts[i]->ltd_uuid;
2236                         *genp = lov->lov_tgts[i]->ltd_gen;
2237                 }
2238
2239                 rc = copy_to_user((void *)uarg, buf, len);
2240                 if (rc)
2241                         rc = -EFAULT;
2242                 obd_ioctl_freedata(buf, len);
2243                 break;
2244         }
2245         case LL_IOC_LOV_SETSTRIPE:
2246                 rc = lov_setstripe(exp, karg, uarg);
2247                 break;
2248         case LL_IOC_LOV_GETSTRIPE:
2249                 rc = lov_getstripe(exp, karg, uarg);
2250                 break;
2251         case LL_IOC_LOV_SETEA:
2252                 rc = lov_setea(exp, karg, uarg);
2253                 break;
2254         default: {
2255                 int set = 0;
2256
2257                 if (count == 0)
2258                         RETURN(-ENOTTY);
2259
2260                 rc = 0;
2261                 for (i = 0; i < count; i++) {
2262                         int err;
2263
2264                         /* OST was disconnected */
2265                         if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_exp)
2266                                 continue;
2267
2268                         err = obd_iocontrol(cmd, lov->lov_tgts[i]->ltd_exp,
2269                                             len, karg, uarg);
2270                         if (err == -ENODATA && cmd == OBD_IOC_POLL_QUOTACHECK) {
2271                                 RETURN(err);
2272                         } else if (err) {
2273                                 if (lov->lov_tgts[i]->ltd_active) {
2274                                         CDEBUG(D_WARNING,
2275                                                "iocontrol OSC %s on OST "
2276                                                "idx %d cmd %x: err = %d\n",
2277                                                lov_uuid2str(lov, i),
2278                                                i, cmd, err);
2279                                         if (!rc)
2280                                                 rc = err;
2281                                 }
2282                         } else {
2283                                 set = 1;
2284                         }
2285                 }
2286                 if (!set && !rc)
2287                         rc = -EIO;
2288         }
2289         }
2290
2291         RETURN(rc);
2292 }
2293
2294 static int lov_get_info(struct obd_export *exp, __u32 keylen,
2295                         void *key, __u32 *vallen, void *val)
2296 {
2297         struct obd_device *obddev = class_exp2obd(exp);
2298         struct lov_obd *lov = &obddev->u.lov;
2299         int i, rc;
2300         ENTRY;
2301
2302         if (!vallen || !val)
2303                 RETURN(-EFAULT);
2304
2305         lov_getref(obddev);
2306
2307         if (keylen > strlen("lock_to_stripe") &&
2308             strcmp(key, "lock_to_stripe") == 0) {
2309                 struct {
2310                         char name[16];
2311                         struct ldlm_lock *lock;
2312                         struct lov_stripe_md *lsm;
2313                 } *data = key;
2314                 struct ldlm_res_id *res_id = &data->lock->l_resource->lr_name;
2315                 struct lov_oinfo *loi;
2316                 __u32 *stripe = val;
2317
2318                 if (*vallen < sizeof(*stripe))
2319                         GOTO(out, rc = -EFAULT);
2320                 *vallen = sizeof(*stripe);
2321
2322                 /* XXX This is another one of those bits that will need to
2323                  * change if we ever actually support nested LOVs.  It uses
2324                  * the lock's export to find out which stripe it is. */
2325                 /* XXX - it's assumed all the locks for deleted OSTs have
2326                  * been cancelled. Also, the export for deleted OSTs will
2327                  * be NULL and won't match the lock's export. */
2328                 for (i = 0, loi = data->lsm->lsm_oinfo;
2329                      i < data->lsm->lsm_stripe_count;
2330                      i++, loi++) {
2331                         if (!lov->lov_tgts[loi->loi_ost_idx])
2332                                 continue;
2333                         if (lov->lov_tgts[loi->loi_ost_idx]->ltd_exp ==
2334                             data->lock->l_conn_export &&
2335                             loi->loi_id == res_id->name[0] &&
2336                             loi->loi_gr == res_id->name[2]) {
2337                                 *stripe = i;
2338                                 GOTO(out, rc = 0);
2339                         }
2340                 }
2341                 LDLM_ERROR(data->lock, "lock on inode without such object");
2342                 dump_lsm(D_ERROR, data->lsm);
2343                 GOTO(out, rc = -ENXIO);
2344         } else if (keylen >= strlen("last_id") && strcmp(key, "last_id") == 0) {
2345                 obd_id *ids = val;
2346                 int size = sizeof(obd_id);
2347                 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
2348                         if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_active)
2349                                 continue;
2350                         rc = obd_get_info(lov->lov_tgts[i]->ltd_exp,
2351                                           keylen, key, &size, &(ids[i]));
2352                         if (rc != 0)
2353                                 GOTO(out, rc);
2354                 }
2355                 GOTO(out, rc = 0);
2356         } else if (keylen >= strlen(KEY_LOVDESC) &&
2357                    strcmp(key, KEY_LOVDESC) == 0) {
2358                 struct lov_desc *desc_ret = val;
2359                 *desc_ret = lov->desc;
2360
2361                 GOTO(out, rc = 0);
2362         }
2363
2364         rc = -EINVAL;
2365 out:
2366         lov_putref(obddev);
2367         RETURN(rc);
2368 }
2369
2370 static int lov_set_info_async(struct obd_export *exp, obd_count keylen,
2371                               void *key, obd_count vallen, void *val,
2372                               struct ptlrpc_request_set *set)
2373 {
2374         struct obd_device *obddev = class_exp2obd(exp);
2375         struct lov_obd *lov = &obddev->u.lov;
2376         int i, rc = 0, err;
2377         int no_set = !set;
2378         ENTRY;
2379
2380         if (no_set) {
2381                 set = ptlrpc_prep_set();
2382                 if (!set)
2383                         RETURN(-ENOMEM);
2384         }
2385
2386         if (KEY_IS(KEY_NEXT_ID)) {
2387                 if (vallen > lov->desc.ld_tgt_count)
2388                         RETURN(-EINVAL);
2389                 vallen = sizeof(obd_id);
2390         }
2391
2392         lov_getref(obddev);
2393
2394         if (KEY_IS(KEY_NEXT_ID) || KEY_IS("checksum")) {
2395                 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
2396                         /* OST was disconnected */
2397                         if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_exp)
2398                                 continue;
2399
2400                         /* hit all OSCs, even inactive ones */
2401                         err = obd_set_info_async(lov->lov_tgts[i]->ltd_exp,
2402                                                  keylen, key, vallen,
2403                                                  ((obd_id*)val) + i, set);
2404                         if (!rc)
2405                                 rc = err;
2406                 }
2407                 GOTO(out, rc);
2408         }
2409
2410         if (KEY_IS("evict_by_nid") || KEY_IS(KEY_CAPA_KEY)) {
2411                 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
2412                         /* OST was disconnected or is inactive */
2413                         if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_active)
2414                                 continue;
2415
2416                         err = obd_set_info_async(lov->lov_tgts[i]->ltd_exp,
2417                                                  keylen, key, vallen, val, set);
2418                         if (!rc)
2419                                 rc = err;
2420                 }
2421                 GOTO(out, rc);
2422         }
2423         if (KEY_IS(KEY_MDS_CONN)) {
2424                 struct mds_group_info mgi;
2425
2426                 LASSERT(vallen == sizeof(mgi));
2427                 mgi =  (*(struct mds_group_info *)val);
2428
2429                 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
2430                         if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_exp)
2431                                 continue;
2432
2433                         if (mgi.uuid && !obd_uuid_equals(mgi.uuid,
2434                                                 &lov->lov_tgts[i]->ltd_uuid))
2435                                 continue;
2436                         if (!val && !lov->lov_tgts[i]->ltd_active)
2437                                 continue;
2438
2439                         err = obd_set_info_async(lov->lov_tgts[i]->ltd_exp,
2440                                                  keylen, key, sizeof(int),
2441                                                  &mgi.group, set);
2442                         if (!rc)
2443                                 rc = err;
2444                 }
2445                 GOTO(out, rc);
2446         }
2447
2448         if (KEY_IS("unlinked")) {
2449                 if (vallen != 0 && KEY_IS("unlinked"))
2450                         GOTO(out, rc = -EINVAL);
2451         } else if (KEY_IS(KEY_FLUSH_CTX)) {
2452                 /* fall through */
2453         } else {
2454                 GOTO(out, rc = -EINVAL);
2455         }
2456
2457         for (i = 0; i < lov->desc.ld_tgt_count; i++) {
2458                 if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_exp)
2459                         continue;
2460
2461                 if (val && !obd_uuid_equals(val, &lov->lov_tgts[i]->ltd_uuid))
2462                         continue;
2463
2464                 if (!val && !lov->lov_tgts[i]->ltd_active)
2465                         continue;
2466
2467                 err = obd_set_info_async(lov->lov_tgts[i]->ltd_exp,
2468                                          keylen, key, vallen, val, set);
2469                 if (!rc)
2470                         rc = err;
2471         }
2472 out:
2473         lov_putref(obddev);
2474         if (no_set) {
2475                 err = ptlrpc_set_wait(set);
2476                 if (!rc)
2477                         rc = err;
2478                 ptlrpc_set_destroy(set);
2479         }
2480         RETURN(rc);
2481 }
2482
2483 static int lov_checkmd(struct obd_export *exp, struct obd_export *md_exp,
2484                        struct lov_stripe_md *lsm)
2485 {
2486         int rc;
2487         ENTRY;
2488
2489         if (!lsm)
2490                 RETURN(0);
2491         LASSERT(md_exp);
2492         LASSERT(lsm_op_find(lsm->lsm_magic) != NULL);
2493         rc = lsm_op_find(lsm->lsm_magic)->lsm_revalidate(lsm, md_exp->exp_obd);
2494
2495         RETURN(rc);
2496 }
2497
2498 int lov_test_and_clear_async_rc(struct lov_stripe_md *lsm)
2499 {
2500         struct lov_oinfo *loi;
2501         int i, rc = 0;
2502         ENTRY;
2503
2504         for (i = 0, loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count;
2505              i++, loi++) {
2506                 if (loi->loi_ar.ar_rc && !rc)
2507                         rc = loi->loi_ar.ar_rc;
2508                 loi->loi_ar.ar_rc = 0;
2509         }
2510         RETURN(rc);
2511 }
2512 EXPORT_SYMBOL(lov_test_and_clear_async_rc);
2513
2514
2515 static int lov_extent_calc(struct obd_export *exp, struct lov_stripe_md *lsm,
2516                            int cmd, obd_off *offset)
2517 {
2518         unsigned long ssize  = lsm->lsm_stripe_size;
2519         obd_off start;
2520
2521         start = *offset;
2522         do_div(start, ssize);
2523         start = start * ssize;
2524
2525         CDEBUG(D_DLMTRACE, "offset %Lu, stripe %lu, start %Lu, end %Lu\n",
2526                *offset, ssize, start, start + ssize - 1);
2527         if (cmd == OBD_CALC_STRIPE_END) {
2528                 *offset = start + ssize - 1;
2529         } else if (cmd == OBD_CALC_STRIPE_START) {
2530                 *offset = start;
2531         } else {
2532                 LBUG();
2533         }
2534
2535         RETURN(0);
2536 }
2537
2538
2539 #if 0
2540 struct lov_multi_wait {
2541         struct ldlm_lock *lock;
2542         wait_queue_t      wait;
2543         int               completed;
2544         int               generation;
2545 };
2546
2547 int lov_complete_many(struct obd_export *exp, struct lov_stripe_md *lsm,
2548                       struct lustre_handle *lockh)
2549 {
2550         struct lov_lock_handles *lov_lockh = NULL;
2551         struct lustre_handle *lov_lockhp;
2552         struct lov_obd *lov;
2553         struct lov_oinfo *loi;
2554         struct lov_multi_wait *queues;
2555         int rc = 0, i;
2556         ENTRY;
2557
2558         ASSERT_LSM_MAGIC(lsm);
2559
2560         if (!exp || !exp->exp_obd)
2561                 RETURN(-ENODEV);
2562
2563         LASSERT(lockh != NULL);
2564         if (lsm->lsm_stripe_count > 1) {
2565                 lov_lockh = lov_handle2llh(lockh);
2566                 if (lov_lockh == NULL) {
2567                         CERROR("LOV: invalid lov lock handle %p\n", lockh);
2568                         RETURN(-EINVAL);
2569                 }
2570
2571                 lov_lockhp = lov_lockh->llh_handles;
2572         } else {
2573                 lov_lockhp = lockh;
2574         }
2575
2576         OBD_ALLOC(queues, lsm->lsm_stripe_count * sizeof(*queues));
2577         if (queues == NULL)
2578                 GOTO(out, rc = -ENOMEM);
2579
2580         lov = &exp->exp_obd->u.lov;
2581         for (i = 0, loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count;
2582              i++, loi++, lov_lockhp++) {
2583                 struct ldlm_lock *lock;
2584                 struct obd_device *obd;
2585
2586                 lock = ldlm_handle2lock(lov_lockhp);
2587                 if (lock == NULL) {
2588                         CDEBUG(D_HA, "lov idx %d subobj "LPX64" no lock?\n",
2589                                loi->loi_ost_idx, loi->loi_id);
2590                         queues[i].completed = 1;
2591                         continue;
2592                 }
2593
2594                 queues[i].lock = lock;
2595                 init_waitqueue_entry(&(queues[i].wait), current);
2596                 add_wait_queue(lock->l_waitq, &(queues[i].wait));
2597
2598                 obd = class_exp2obd(lock->l_conn_export);
2599                 if (obd != NULL)
2600                         imp = obd->u.cli.cl_import;
2601                 if (imp != NULL) {
2602                         spin_lock(&imp->imp_lock);
2603                         queues[i].generation = imp->imp_generation;
2604                         spin_unlock(&imp->imp_lock);
2605                 }
2606         }
2607
2608         lwi = LWI_TIMEOUT_INTR(obd_timeout * HZ, ldlm_expired_completion_wait,
2609                                interrupted_completion_wait, &lwd);
2610         rc = l_wait_event_added(check_multi_complete(queues, lsm), &lwi);
2611
2612         for (i = 0; i < lsm->lsm_stripe_count; i++)
2613                 remove_wait_queue(lock->l_waitq, &(queues[i].wait));
2614
2615         if (rc == -EINTR || rc == -ETIMEDOUT) {
2616
2617
2618         }
2619
2620  out:
2621         if (lov_lockh != NULL)
2622                 lov_llh_put(lov_lockh);
2623         RETURN(rc);
2624 }
2625 #endif
2626
2627 void lov_stripe_lock(struct lov_stripe_md *md)
2628 {
2629         LASSERT(md->lsm_lock_owner != cfs_current());
2630         spin_lock(&md->lsm_lock);
2631         LASSERT(md->lsm_lock_owner == NULL);
2632         md->lsm_lock_owner = cfs_current();
2633 }
2634 EXPORT_SYMBOL(lov_stripe_lock);
2635
2636 void lov_stripe_unlock(struct lov_stripe_md *md)
2637 {
2638         LASSERT(md->lsm_lock_owner == cfs_current());
2639         md->lsm_lock_owner = NULL;
2640         spin_unlock(&md->lsm_lock);
2641 }
2642 EXPORT_SYMBOL(lov_stripe_unlock);
2643
2644 struct obd_ops lov_obd_ops = {
2645         .o_owner               = THIS_MODULE,
2646         .o_setup               = lov_setup,
2647         .o_precleanup          = lov_precleanup,
2648         .o_cleanup             = lov_cleanup,
2649         .o_process_config      = lov_process_config,
2650         .o_connect             = lov_connect,
2651         .o_disconnect          = lov_disconnect,
2652         .o_statfs              = lov_statfs,
2653         .o_statfs_async        = lov_statfs_async,
2654         .o_packmd              = lov_packmd,
2655         .o_unpackmd            = lov_unpackmd,
2656         .o_checkmd             = lov_checkmd,
2657         .o_create              = lov_create,
2658         .o_destroy             = lov_destroy,
2659         .o_getattr             = lov_getattr,
2660         .o_getattr_async       = lov_getattr_async,
2661         .o_setattr             = lov_setattr,
2662         .o_setattr_async       = lov_setattr_async,
2663         .o_brw                 = lov_brw,
2664         .o_brw_async           = lov_brw_async,
2665         .o_prep_async_page     = lov_prep_async_page,
2666         .o_queue_async_io      = lov_queue_async_io,
2667         .o_set_async_flags     = lov_set_async_flags,
2668         .o_queue_group_io      = lov_queue_group_io,
2669         .o_trigger_group_io    = lov_trigger_group_io,
2670         .o_teardown_async_page = lov_teardown_async_page,
2671         .o_merge_lvb           = lov_merge_lvb,
2672         .o_adjust_kms          = lov_adjust_kms,
2673         .o_punch               = lov_punch,
2674         .o_sync                = lov_sync,
2675         .o_enqueue             = lov_enqueue,
2676         .o_match               = lov_match,
2677         .o_change_cbdata       = lov_change_cbdata,
2678         .o_cancel              = lov_cancel,
2679         .o_cancel_unused       = lov_cancel_unused,
2680         .o_join_lru            = lov_join_lru,
2681         .o_iocontrol           = lov_iocontrol,
2682         .o_get_info            = lov_get_info,
2683         .o_set_info_async      = lov_set_info_async,
2684         .o_extent_calc         = lov_extent_calc,
2685         .o_llog_init           = lov_llog_init,
2686         .o_llog_finish         = lov_llog_finish,
2687         .o_notify              = lov_notify,
2688 };
2689
2690 static quota_interface_t *quota_interface;
2691 extern quota_interface_t lov_quota_interface;
2692
2693 int __init lov_init(void)
2694 {
2695         struct lprocfs_static_vars lvars;
2696         int rc;
2697         ENTRY;
2698
2699         lprocfs_init_vars(lov, &lvars);
2700
2701         request_module("lquota");
2702         quota_interface = PORTAL_SYMBOL_GET(lov_quota_interface);
2703         init_obd_quota_ops(quota_interface, &lov_obd_ops);
2704
2705         rc = class_register_type(&lov_obd_ops, NULL, lvars.module_vars,
2706                                  LUSTRE_LOV_NAME, NULL);
2707         if (rc && quota_interface)
2708                 PORTAL_SYMBOL_PUT(lov_quota_interface);
2709
2710         RETURN(rc);
2711 }
2712
2713 #ifdef __KERNEL__
2714 static void /*__exit*/ lov_exit(void)
2715 {
2716         if (quota_interface)
2717                 PORTAL_SYMBOL_PUT(lov_quota_interface);
2718
2719         class_unregister_type(LUSTRE_LOV_NAME);
2720 }
2721
2722 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
2723 MODULE_DESCRIPTION("Lustre Logical Object Volume OBD driver");
2724 MODULE_LICENSE("GPL");
2725
2726 cfs_module(lov, LUSTRE_VERSION_STRING, lov_init, lov_exit);
2727 #endif