Whamcloud - gitweb
b=12860
[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 (data && (data->ocd_connect_flags & OBD_CONNECT_INDEX))
122                 data->ocd_index = index;
123
124         /*
125          * Divine LOV knows that OBDs under it are OSCs.
126          */
127         imp = tgt_obd->u.cli.cl_import;
128
129         if (activate) {
130                 tgt_obd->obd_no_recov = 0;
131                 /* FIXME this is probably supposed to be 
132                    ptlrpc_set_import_active.  Horrible naming. */
133                 ptlrpc_activate_import(imp);
134         }
135
136         if (imp->imp_invalid) {
137                 CERROR("not connecting OSC %s; administratively "
138                        "disabled\n", obd_uuid2str(&tgt_uuid));
139                 rc = obd_register_observer(tgt_obd, obd);
140                 if (rc) {
141                         CERROR("Target %s register_observer error %d; "
142                                "will not be able to reactivate\n",
143                                obd_uuid2str(&tgt_uuid), rc);
144                 }
145                 RETURN(0);
146         }
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                 }
199         }
200 #endif
201
202         rc = qos_add_tgt(obd, index);
203         if (rc)
204                 CERROR("qos_add_tgt failed %d\n", rc);
205
206         RETURN(0);
207 }
208
209 static int lov_connect(const struct lu_env *env,
210                        struct lustre_handle *conn, struct obd_device *obd,
211                        struct obd_uuid *cluuid, struct obd_connect_data *data)
212 {
213         struct lov_obd *lov = &obd->u.lov;
214         struct lov_tgt_desc *tgt;
215         int i, rc;
216         ENTRY;
217
218         CDEBUG(D_CONFIG, "connect #%d\n", lov->lov_connects);
219
220         rc = class_connect(conn, obd, cluuid);
221         if (rc)
222                 RETURN(rc);
223
224         /* Why should there ever be more than 1 connect? */
225         lov->lov_connects++;
226         LASSERT(lov->lov_connects == 1);
227
228         memset(&lov->lov_ocd, 0, sizeof(lov->lov_ocd));
229         if (data)
230                 lov->lov_ocd = *data;
231
232         lov_getref(obd);
233         for (i = 0; i < lov->desc.ld_tgt_count; i++) {
234                 tgt = lov->lov_tgts[i];
235                 if (!tgt || obd_uuid_empty(&tgt->ltd_uuid))
236                         continue;
237                 /* Flags will be lowest common denominator */
238                 rc = lov_connect_obd(obd, i, tgt->ltd_activate, &lov->lov_ocd);
239                 if (rc) {
240                         CERROR("%s: lov connect tgt %d failed: %d\n",
241                                obd->obd_name, i, rc);
242                         continue;
243                 }
244         }
245         lov_putref(obd);
246
247         RETURN(0);
248 }
249
250 static int lov_disconnect_obd(struct obd_device *obd, __u32 index)
251 {
252         cfs_proc_dir_entry_t *lov_proc_dir;
253         struct lov_obd *lov = &obd->u.lov;
254         struct obd_device *osc_obd =
255                 class_exp2obd(lov->lov_tgts[index]->ltd_exp);
256         int rc;
257         ENTRY;
258
259         CDEBUG(D_CONFIG, "%s: disconnecting target %s\n",
260                obd->obd_name, osc_obd->obd_name);
261
262         if (lov->lov_tgts[index]->ltd_active) {
263                 lov->lov_tgts[index]->ltd_active = 0;
264                 lov->desc.ld_active_tgt_count--;
265         }
266
267         lov_proc_dir = lprocfs_srch(obd->obd_proc_entry, "target_obds");
268         if (lov_proc_dir) {
269                 cfs_proc_dir_entry_t *osc_symlink;
270
271                 osc_symlink = lprocfs_srch(lov_proc_dir, osc_obd->obd_name);
272                 if (osc_symlink) {
273                         lprocfs_remove(&osc_symlink);
274                 } else {
275                         CERROR("/proc/fs/lustre/%s/%s/target_obds/%s missing.",
276                                obd->obd_type->typ_name, obd->obd_name,
277                                osc_obd->obd_name);
278                 }
279         }
280
281         if (osc_obd) {
282                 /* Pass it on to our clients.
283                  * XXX This should be an argument to disconnect,
284                  * XXX not a back-door flag on the OBD.  Ah well.
285                  */
286                 osc_obd->obd_force = obd->obd_force;
287                 osc_obd->obd_fail = obd->obd_fail;
288                 osc_obd->obd_no_recov = obd->obd_no_recov;
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                 class_manual_cleanup(osc_obd);
639 }
640
641 void lov_fix_desc(struct lov_desc *desc)
642 {
643         if (desc->ld_default_stripe_size < PTLRPC_MAX_BRW_SIZE) {
644                 LCONSOLE_WARN("Increasing default stripe size to min %u\n",
645                               PTLRPC_MAX_BRW_SIZE);
646                 desc->ld_default_stripe_size = PTLRPC_MAX_BRW_SIZE;
647         } else if (desc->ld_default_stripe_size & (LOV_MIN_STRIPE_SIZE - 1)) {
648                 desc->ld_default_stripe_size &= ~(LOV_MIN_STRIPE_SIZE - 1);
649                 LCONSOLE_WARN("Changing default stripe size to "LPU64" (a "
650                               "multiple of %u)\n",
651                               desc->ld_default_stripe_size,LOV_MIN_STRIPE_SIZE);
652         }
653
654         if (desc->ld_default_stripe_count == 0)
655                 desc->ld_default_stripe_count = 1;
656
657         /* from lov_setstripe */
658         if ((desc->ld_pattern != 0) &&
659             (desc->ld_pattern != LOV_PATTERN_RAID0)) {
660                 LCONSOLE_WARN("Unknown stripe pattern: %#x\n",desc->ld_pattern);
661                 desc->ld_pattern = 0;
662         }
663
664         if (desc->ld_qos_maxage == 0)
665                 desc->ld_qos_maxage = QOS_DEFAULT_MAXAGE;
666 }
667
668 static int lov_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
669 {
670         struct lprocfs_static_vars lvars;
671         struct lov_desc *desc;
672         struct lov_obd *lov = &obd->u.lov;
673         int count;
674         ENTRY;
675
676         if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
677                 CERROR("LOV setup requires a descriptor\n");
678                 RETURN(-EINVAL);
679         }
680
681         desc = (struct lov_desc *)lustre_cfg_buf(lcfg, 1);
682
683         if (sizeof(*desc) > LUSTRE_CFG_BUFLEN(lcfg, 1)) {
684                 CERROR("descriptor size wrong: %d > %d\n",
685                        (int)sizeof(*desc), LUSTRE_CFG_BUFLEN(lcfg, 1));
686                 RETURN(-EINVAL);
687         }
688
689         if (desc->ld_magic != LOV_DESC_MAGIC) {
690                 if (desc->ld_magic == __swab32(LOV_DESC_MAGIC)) {
691                             CDEBUG(D_OTHER, "%s: Swabbing lov desc %p\n",
692                                    obd->obd_name, desc);
693                             lustre_swab_lov_desc(desc);
694                 } else {
695                         CERROR("%s: Bad lov desc magic: %#x\n",
696                                obd->obd_name, desc->ld_magic);
697                         RETURN(-EINVAL);
698                 }
699         }
700
701         lov_fix_desc(desc);
702
703         /* Because of 64-bit divide/mod operations only work with a 32-bit
704          * divisor in a 32-bit kernel, we cannot support a stripe width
705          * of 4GB or larger on 32-bit CPUs. */
706         count = desc->ld_default_stripe_count;
707         if ((count > 0 ? count : desc->ld_tgt_count) *
708             desc->ld_default_stripe_size > 0xffffffff) {
709                 CERROR("LOV: stripe width "LPU64"x%u > 4294967295 bytes\n",
710                        desc->ld_default_stripe_size, count);
711                 RETURN(-EINVAL);
712         }
713
714         desc->ld_active_tgt_count = 0;
715         lov->desc = *desc;
716         lov->lov_tgt_size = 0;
717         sema_init(&lov->lov_lock, 1);
718         atomic_set(&lov->lov_refcount, 0);
719         INIT_LIST_HEAD(&lov->lov_qos.lq_oss_list);
720         init_rwsem(&lov->lov_qos.lq_rw_sem);
721         lov->lov_qos.lq_dirty = 1;
722         lov->lov_qos.lq_dirty_rr = 1;
723         lov->lov_qos.lq_reset = 1;
724         /* Default priority is toward free space balance */
725         lov->lov_qos.lq_prio_free = 232;
726
727         lprocfs_init_vars(lov, &lvars);
728         lprocfs_obd_setup(obd, lvars.obd_vars);
729 #ifdef LPROCFS
730         {
731                 cfs_proc_dir_entry_t *entry;
732
733                 entry = create_proc_entry("target_obd", 0444,
734                                           obd->obd_proc_entry);
735                 if (entry != NULL) {
736                         entry->proc_fops = &lov_proc_target_fops;
737                         entry->data = obd;
738                 }
739         }
740 #endif
741
742         RETURN(0);
743 }
744
745 static int lov_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
746 {
747         int rc = 0;
748         ENTRY;
749
750         switch (stage) {
751         case OBD_CLEANUP_EARLY: {
752                 struct lov_obd *lov = &obd->u.lov;
753                 int i;
754                 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
755                         if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_active)
756                                 continue;
757                         obd_precleanup(class_exp2obd(lov->lov_tgts[i]->ltd_exp),
758                                        OBD_CLEANUP_EARLY);
759                 }
760                 break;
761         }
762         case OBD_CLEANUP_EXPORTS:
763                 break;
764         case OBD_CLEANUP_SELF_EXP:
765                 rc = obd_llog_finish(obd, 0);
766                 if (rc != 0)
767                         CERROR("failed to cleanup llogging subsystems\n");
768                 break;
769         case OBD_CLEANUP_OBD:
770                 break;
771         }
772         RETURN(rc);
773 }
774
775 static int lov_cleanup(struct obd_device *obd)
776 {
777         struct lov_obd *lov = &obd->u.lov;
778
779         lprocfs_obd_cleanup(obd);
780         if (lov->lov_tgts) {
781                 int i;
782                 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
783                         if (lov->lov_tgts[i]) {
784                                 /* Inactive targets may never have connected */
785                                 if (lov->lov_tgts[i]->ltd_active ||
786                                     atomic_read(&lov->lov_refcount)) 
787                                         /* We should never get here - these 
788                                            should have been removed in the 
789                                            disconnect. */
790                                         CERROR("lov tgt %d not cleaned!"
791                                                " deathrow=%d, lovrc=%d\n",
792                                                i, lov->lov_death_row,
793                                                atomic_read(&lov->lov_refcount));
794                                 lov_del_target(obd, i, 0, 0);
795                         }
796                 }
797                 OBD_FREE(lov->lov_tgts, sizeof(*lov->lov_tgts) *
798                          lov->lov_tgt_size);
799                 lov->lov_tgt_size = 0;
800         }
801
802         if (lov->lov_qos.lq_rr_size)
803                 OBD_FREE(lov->lov_qos.lq_rr_array, lov->lov_qos.lq_rr_size);
804
805         RETURN(0);
806 }
807
808 static int lov_process_config(struct obd_device *obd, obd_count len, void *buf)
809 {
810         struct lustre_cfg *lcfg = buf;
811         struct obd_uuid obd_uuid;
812         int cmd;
813         int rc = 0;
814         ENTRY;
815
816         switch(cmd = lcfg->lcfg_command) {
817         case LCFG_LOV_ADD_OBD:
818         case LCFG_LOV_ADD_INA:
819         case LCFG_LOV_DEL_OBD: {
820                 __u32 index;
821                 int gen;
822                 /* lov_modify_tgts add  0:lov_mdsA  1:ost1_UUID  2:0  3:1 */
823                 if (LUSTRE_CFG_BUFLEN(lcfg, 1) > sizeof(obd_uuid.uuid))
824                         GOTO(out, rc = -EINVAL);
825
826                 obd_str2uuid(&obd_uuid,  lustre_cfg_buf(lcfg, 1));
827
828                 if (sscanf(lustre_cfg_buf(lcfg, 2), "%d", &index) != 1)
829                         GOTO(out, rc = -EINVAL);
830                 if (sscanf(lustre_cfg_buf(lcfg, 3), "%d", &gen) != 1)
831                         GOTO(out, rc = -EINVAL);
832                 if (cmd == LCFG_LOV_ADD_OBD)
833                         rc = lov_add_target(obd, &obd_uuid, index, gen, 1);
834                 else if (cmd == LCFG_LOV_ADD_INA)
835                         rc = lov_add_target(obd, &obd_uuid, index, gen, 0);
836                 else
837                         rc = lov_del_target(obd, index, &obd_uuid, gen);
838                 GOTO(out, rc);
839         }
840         case LCFG_PARAM: {
841                 struct lprocfs_static_vars lvars;
842                 struct lov_desc *desc = &(obd->u.lov.desc);
843
844                 if (!desc)
845                         GOTO(out, rc = -EINVAL);
846
847                 lprocfs_init_vars(lov, &lvars);
848
849                 rc = class_process_proc_param(PARAM_LOV, lvars.obd_vars,
850                                               lcfg, obd);
851                 GOTO(out, rc);
852         }
853         default: {
854                 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
855                 GOTO(out, rc = -EINVAL);
856
857         }
858         }
859 out:
860         RETURN(rc);
861 }
862
863 #ifndef log2
864 #define log2(n) ffz(~(n))
865 #endif
866
867 static int lov_clear_orphans(struct obd_export *export, struct obdo *src_oa,
868                              struct lov_stripe_md **ea,
869                              struct obd_trans_info *oti)
870 {
871         struct lov_obd *lov;
872         struct obdo *tmp_oa;
873         struct obd_uuid *ost_uuid = NULL;
874         int rc = 0, i;
875         ENTRY;
876
877         LASSERT(src_oa->o_valid & OBD_MD_FLFLAGS &&
878                 src_oa->o_flags == OBD_FL_DELORPHAN);
879
880         lov = &export->exp_obd->u.lov;
881
882         OBDO_ALLOC(tmp_oa);
883         if (tmp_oa == NULL)
884                 RETURN(-ENOMEM);
885
886         if (src_oa->o_valid & OBD_MD_FLINLINE) {
887                 ost_uuid = (struct obd_uuid *)src_oa->o_inline;
888                 CDEBUG(D_HA, "clearing orphans only for %s\n",
889                        ost_uuid->uuid);
890         }
891
892         lov_getref(export->exp_obd);
893         for (i = 0; i < lov->desc.ld_tgt_count; i++) {
894                 struct lov_stripe_md obj_md;
895                 struct lov_stripe_md *obj_mdp = &obj_md;
896                 struct lov_tgt_desc *tgt;
897                 int err;
898
899                 tgt = lov->lov_tgts[i];
900                 if (!tgt)
901                         continue;
902
903                 /* if called for a specific target, we don't
904                    care if it is not active. */
905                 if (!lov->lov_tgts[i]->ltd_active && ost_uuid == NULL) {
906                         CDEBUG(D_HA, "lov idx %d inactive\n", i);
907                         continue;
908                 }
909
910                 if (ost_uuid && !obd_uuid_equals(ost_uuid, &tgt->ltd_uuid))
911                         continue;
912
913                 CDEBUG(D_CONFIG,"Clear orphans for %d:%s\n", i,
914                        obd_uuid2str(ost_uuid));
915
916                 memcpy(tmp_oa, src_oa, sizeof(*tmp_oa));
917
918                 LASSERT(lov->lov_tgts[i]->ltd_exp);
919                 /* XXX: LOV STACKING: use real "obj_mdp" sub-data */
920                 err = obd_create(lov->lov_tgts[i]->ltd_exp,
921                                  tmp_oa, &obj_mdp, oti);
922                 if (err)
923                         /* This export will be disabled until it is recovered,
924                            and then orphan recovery will be completed. */
925                         CERROR("error in orphan recovery on OST idx %d/%d: "
926                                "rc = %d\n", i, lov->desc.ld_tgt_count, err);
927
928                 if (ost_uuid)
929                         break;
930         }
931         lov_putref(export->exp_obd);
932
933         OBDO_FREE(tmp_oa);
934         RETURN(rc);
935 }
936
937 static int lov_recreate(struct obd_export *exp, struct obdo *src_oa,
938                         struct lov_stripe_md **ea, struct obd_trans_info *oti)
939 {
940         struct lov_stripe_md *obj_mdp, *lsm;
941         struct lov_obd *lov = &exp->exp_obd->u.lov;
942         unsigned ost_idx;
943         int rc, i;
944         ENTRY;
945
946         LASSERT(src_oa->o_valid & OBD_MD_FLFLAGS &&
947                 src_oa->o_flags & OBD_FL_RECREATE_OBJS);
948
949         OBD_ALLOC(obj_mdp, sizeof(*obj_mdp));
950         if (obj_mdp == NULL)
951                 RETURN(-ENOMEM);
952
953         ost_idx = src_oa->o_nlink;
954         lsm = *ea;
955         if (lsm == NULL)
956                 GOTO(out, rc = -EINVAL);
957         if (ost_idx >= lov->desc.ld_tgt_count ||
958             !lov->lov_tgts[ost_idx])
959                 GOTO(out, rc = -EINVAL);
960
961         for (i = 0; i < lsm->lsm_stripe_count; i++) {
962                 if (lsm->lsm_oinfo[i]->loi_ost_idx == ost_idx) {
963                         if (lsm->lsm_oinfo[i]->loi_id != src_oa->o_id)
964                                 GOTO(out, rc = -EINVAL);
965                         break;
966                 }
967         }
968         if (i == lsm->lsm_stripe_count)
969                 GOTO(out, rc = -EINVAL);
970
971         rc = obd_create(lov->lov_tgts[ost_idx]->ltd_exp, src_oa, &obj_mdp, oti);
972 out:
973         OBD_FREE(obj_mdp, sizeof(*obj_mdp));
974         RETURN(rc);
975 }
976
977 /* the LOV expects oa->o_id to be set to the LOV object id */
978 static int lov_create(struct obd_export *exp, struct obdo *src_oa,
979                       struct lov_stripe_md **ea, struct obd_trans_info *oti)
980 {
981         struct lov_obd *lov;
982         struct obd_info oinfo;
983         struct lov_request_set *set = NULL;
984         struct lov_request *req;
985         struct obd_statfs osfs;
986         __u64 maxage;
987         int rc = 0;
988         ENTRY;
989
990         LASSERT(ea != NULL);
991         if (exp == NULL)
992                 RETURN(-EINVAL);
993
994         if ((src_oa->o_valid & OBD_MD_FLFLAGS) &&
995             src_oa->o_flags == OBD_FL_DELORPHAN) {
996                 rc = lov_clear_orphans(exp, src_oa, ea, oti);
997                 RETURN(rc);
998         }
999
1000         lov = &exp->exp_obd->u.lov;
1001         if (!lov->desc.ld_active_tgt_count)
1002                 RETURN(-EIO);
1003
1004         /* Recreate a specific object id at the given OST index */
1005         if ((src_oa->o_valid & OBD_MD_FLFLAGS) &&
1006             (src_oa->o_flags & OBD_FL_RECREATE_OBJS)) {
1007                  rc = lov_recreate(exp, src_oa, ea, oti);
1008                  RETURN(rc);
1009         }
1010
1011         maxage = cfs_time_shift_64(-lov->desc.ld_qos_maxage);
1012         obd_statfs_rqset(exp->exp_obd, &osfs, maxage);
1013
1014         rc = lov_prep_create_set(exp, &oinfo, ea, src_oa, oti, &set);
1015         if (rc)
1016                 RETURN(rc);
1017
1018         list_for_each_entry(req, &set->set_list, rq_link) {
1019                 /* XXX: LOV STACKING: use real "obj_mdp" sub-data */
1020                 rc = obd_create(lov->lov_tgts[req->rq_idx]->ltd_exp,
1021                                 req->rq_oi.oi_oa, &req->rq_oi.oi_md, oti);
1022                 lov_update_create_set(set, req, rc);
1023         }
1024         rc = lov_fini_create_set(set, ea);
1025         RETURN(rc);
1026 }
1027
1028 #define ASSERT_LSM_MAGIC(lsmp)                                                  \
1029 do {                                                                            \
1030         LASSERT((lsmp) != NULL);                                                \
1031         LASSERTF(((lsmp)->lsm_magic == LOV_MAGIC ||                             \
1032                  (lsmp)->lsm_magic == LOV_MAGIC_JOIN), "%p->lsm_magic=%x\n",    \
1033                  (lsmp), (lsmp)->lsm_magic);                                    \
1034 } while (0)
1035
1036 static int lov_destroy(struct obd_export *exp, struct obdo *oa,
1037                        struct lov_stripe_md *lsm, struct obd_trans_info *oti,
1038                        struct obd_export *md_exp)
1039 {
1040         struct lov_request_set *set;
1041         struct obd_info oinfo;
1042         struct lov_request *req;
1043         struct list_head *pos;
1044         struct lov_obd *lov;
1045         int rc = 0, err;
1046         ENTRY;
1047
1048         ASSERT_LSM_MAGIC(lsm);
1049
1050         if (!exp || !exp->exp_obd)
1051                 RETURN(-ENODEV);
1052
1053         if (oa->o_valid & OBD_MD_FLCOOKIE) {
1054                 LASSERT(oti);
1055                 LASSERT(oti->oti_logcookies);
1056         }
1057
1058         lov = &exp->exp_obd->u.lov;
1059         rc = lov_prep_destroy_set(exp, &oinfo, oa, lsm, oti, &set);
1060         if (rc)
1061                 RETURN(rc);
1062
1063         list_for_each (pos, &set->set_list) {
1064                 int err;
1065                 req = list_entry(pos, struct lov_request, rq_link);
1066
1067                 if (oa->o_valid & OBD_MD_FLCOOKIE)
1068                         oti->oti_logcookies = set->set_cookies + req->rq_stripe;
1069
1070                 err = obd_destroy(lov->lov_tgts[req->rq_idx]->ltd_exp,
1071                                   req->rq_oi.oi_oa, NULL, oti, NULL);
1072                 err = lov_update_common_set(set, req, err);
1073                 if (err) {
1074                         CERROR("error: destroying objid "LPX64" subobj "
1075                                LPX64" on OST idx %d: rc = %d\n",
1076                                oa->o_id, req->rq_oi.oi_oa->o_id,
1077                                req->rq_idx, err);
1078                         if (!rc)
1079                                 rc = err;
1080                 }
1081         }
1082
1083         if (rc == 0) {
1084                 LASSERT(lsm_op_find(lsm->lsm_magic) != NULL);
1085                 rc = lsm_op_find(lsm->lsm_magic)->lsm_destroy(lsm, oa, md_exp);
1086         }
1087         err = lov_fini_destroy_set(set);
1088         RETURN(rc ? rc : err);
1089 }
1090
1091 static int lov_getattr(struct obd_export *exp, struct obd_info *oinfo)
1092 {
1093         struct lov_request_set *set;
1094         struct lov_request *req;
1095         struct list_head *pos;
1096         struct lov_obd *lov;
1097         int err = 0, rc = 0;
1098         ENTRY;
1099
1100         LASSERT(oinfo);
1101         ASSERT_LSM_MAGIC(oinfo->oi_md);
1102
1103         if (!exp || !exp->exp_obd)
1104                 RETURN(-ENODEV);
1105
1106         lov = &exp->exp_obd->u.lov;
1107
1108         rc = lov_prep_getattr_set(exp, oinfo, &set);
1109         if (rc)
1110                 RETURN(rc);
1111
1112         list_for_each (pos, &set->set_list) {
1113                 req = list_entry(pos, struct lov_request, rq_link);
1114
1115                 CDEBUG(D_INFO, "objid "LPX64"[%d] has subobj "LPX64" at idx "
1116                        "%u\n", oinfo->oi_oa->o_id, req->rq_stripe,
1117                        req->rq_oi.oi_oa->o_id, req->rq_idx);
1118
1119                 rc = obd_getattr(lov->lov_tgts[req->rq_idx]->ltd_exp,
1120                                  &req->rq_oi);
1121                 err = lov_update_common_set(set, req, rc);
1122                 if (err) {
1123                         CERROR("error: getattr objid "LPX64" subobj "
1124                                LPX64" on OST idx %d: rc = %d\n",
1125                                oinfo->oi_oa->o_id, req->rq_oi.oi_oa->o_id,
1126                                req->rq_idx, err);
1127                         break;
1128                 }
1129         }
1130
1131         rc = lov_fini_getattr_set(set);
1132         if (err)
1133                 rc = err;
1134         RETURN(rc);
1135 }
1136
1137 static int lov_getattr_interpret(struct ptlrpc_request_set *rqset,
1138                                  void *data, int rc)
1139 {
1140         struct lov_request_set *lovset = (struct lov_request_set *)data;
1141         int err;
1142         ENTRY;
1143
1144         /* don't do attribute merge if this aysnc op failed */
1145         if (rc)
1146                 lovset->set_completes = 0;
1147         err = lov_fini_getattr_set(lovset);
1148         RETURN(rc ? rc : err);
1149 }
1150
1151 static int lov_getattr_async(struct obd_export *exp, struct obd_info *oinfo,
1152                               struct ptlrpc_request_set *rqset)
1153 {
1154         struct lov_request_set *lovset;
1155         struct lov_obd *lov;
1156         struct list_head *pos;
1157         struct lov_request *req;
1158         int rc = 0, err;
1159         ENTRY;
1160
1161         LASSERT(oinfo);
1162         ASSERT_LSM_MAGIC(oinfo->oi_md);
1163
1164         if (!exp || !exp->exp_obd)
1165                 RETURN(-ENODEV);
1166
1167         lov = &exp->exp_obd->u.lov;
1168
1169         rc = lov_prep_getattr_set(exp, oinfo, &lovset);
1170         if (rc)
1171                 RETURN(rc);
1172
1173         CDEBUG(D_INFO, "objid "LPX64": %ux%u byte stripes\n",
1174                oinfo->oi_md->lsm_object_id, oinfo->oi_md->lsm_stripe_count,
1175                oinfo->oi_md->lsm_stripe_size);
1176
1177         list_for_each (pos, &lovset->set_list) {
1178                 req = list_entry(pos, struct lov_request, rq_link);
1179
1180                 CDEBUG(D_INFO, "objid "LPX64"[%d] has subobj "LPX64" at idx "
1181                        "%u\n", oinfo->oi_oa->o_id, req->rq_stripe,
1182                        req->rq_oi.oi_oa->o_id, req->rq_idx);
1183                 rc = obd_getattr_async(lov->lov_tgts[req->rq_idx]->ltd_exp,
1184                                        &req->rq_oi, rqset);
1185                 if (rc) {
1186                         CERROR("error: getattr objid "LPX64" subobj "
1187                                LPX64" on OST idx %d: rc = %d\n",
1188                                oinfo->oi_oa->o_id, req->rq_oi.oi_oa->o_id,
1189                                req->rq_idx, rc);
1190                         GOTO(out, rc);
1191                 }
1192         }
1193
1194         if (!list_empty(&rqset->set_requests)) {
1195                 LASSERT(rc == 0);
1196                 LASSERT (rqset->set_interpret == NULL);
1197                 rqset->set_interpret = lov_getattr_interpret;
1198                 rqset->set_arg = (void *)lovset;
1199                 RETURN(rc);
1200         }
1201 out:
1202         if (rc)
1203                 lovset->set_completes = 0;
1204         err = lov_fini_getattr_set(lovset);
1205         RETURN(rc ? rc : err);
1206 }
1207
1208 static int lov_setattr(struct obd_export *exp, struct obd_info *oinfo,
1209                        struct obd_trans_info *oti)
1210 {
1211         struct lov_request_set *set;
1212         struct lov_obd *lov;
1213         struct list_head *pos;
1214         struct lov_request *req;
1215         int err = 0, rc = 0;
1216         ENTRY;
1217
1218         LASSERT(oinfo);
1219         ASSERT_LSM_MAGIC(oinfo->oi_md);
1220
1221         if (!exp || !exp->exp_obd)
1222                 RETURN(-ENODEV);
1223
1224         /* for now, we only expect the following updates here */
1225         LASSERT(!(oinfo->oi_oa->o_valid & ~(OBD_MD_FLID | OBD_MD_FLTYPE |
1226                                             OBD_MD_FLMODE | OBD_MD_FLATIME |
1227                                             OBD_MD_FLMTIME | OBD_MD_FLCTIME |
1228                                             OBD_MD_FLFLAGS | OBD_MD_FLSIZE |
1229                                             OBD_MD_FLGROUP | OBD_MD_FLUID |
1230                                             OBD_MD_FLGID | OBD_MD_FLINLINE |
1231                                             OBD_MD_FLFID | OBD_MD_FLGENER)));
1232         lov = &exp->exp_obd->u.lov;
1233         rc = lov_prep_setattr_set(exp, oinfo, oti, &set);
1234         if (rc)
1235                 RETURN(rc);
1236
1237         list_for_each (pos, &set->set_list) {
1238                 req = list_entry(pos, struct lov_request, rq_link);
1239
1240                 rc = obd_setattr(lov->lov_tgts[req->rq_idx]->ltd_exp,
1241                                  &req->rq_oi, NULL);
1242                 err = lov_update_setattr_set(set, req, rc);
1243                 if (err) {
1244                         CERROR("error: setattr objid "LPX64" subobj "
1245                                LPX64" on OST idx %d: rc = %d\n",
1246                                set->set_oi->oi_oa->o_id,
1247                                req->rq_oi.oi_oa->o_id, req->rq_idx, err);
1248                         if (!rc)
1249                                 rc = err;
1250                 }
1251         }
1252         err = lov_fini_setattr_set(set);
1253         if (!rc)
1254                 rc = err;
1255         RETURN(rc);
1256 }
1257
1258 static int lov_setattr_interpret(struct ptlrpc_request_set *rqset,
1259                                  void *data, int rc)
1260 {
1261         struct lov_request_set *lovset = (struct lov_request_set *)data;
1262         int err;
1263         ENTRY;
1264
1265         if (rc)
1266                 lovset->set_completes = 0;
1267         err = lov_fini_setattr_set(lovset);
1268         RETURN(rc ? rc : err);
1269 }
1270
1271 /* If @oti is given, the request goes from MDS and responses from OSTs are not
1272    needed. Otherwise, a client is waiting for responses. */
1273 static int lov_setattr_async(struct obd_export *exp, struct obd_info *oinfo,
1274                              struct obd_trans_info *oti,
1275                              struct ptlrpc_request_set *rqset)
1276 {
1277         struct lov_request_set *set;
1278         struct lov_request *req;
1279         struct list_head *pos;
1280         struct lov_obd *lov;
1281         int rc = 0;
1282         ENTRY;
1283
1284         LASSERT(oinfo);
1285         ASSERT_LSM_MAGIC(oinfo->oi_md);
1286         if (oinfo->oi_oa->o_valid & OBD_MD_FLCOOKIE) {
1287                 LASSERT(oti);
1288                 LASSERT(oti->oti_logcookies);
1289         }
1290
1291         if (!exp || !exp->exp_obd)
1292                 RETURN(-ENODEV);
1293
1294         lov = &exp->exp_obd->u.lov;
1295         rc = lov_prep_setattr_set(exp, oinfo, oti, &set);
1296         if (rc)
1297                 RETURN(rc);
1298
1299         CDEBUG(D_INFO, "objid "LPX64": %ux%u byte stripes\n",
1300                oinfo->oi_md->lsm_object_id, oinfo->oi_md->lsm_stripe_count,
1301                oinfo->oi_md->lsm_stripe_size);
1302
1303         list_for_each (pos, &set->set_list) {
1304                 req = list_entry(pos, struct lov_request, rq_link);
1305
1306                 if (oinfo->oi_oa->o_valid & OBD_MD_FLCOOKIE)
1307                         oti->oti_logcookies = set->set_cookies + req->rq_stripe;
1308
1309                 CDEBUG(D_INFO, "objid "LPX64"[%d] has subobj "LPX64" at idx "
1310                        "%u\n", oinfo->oi_oa->o_id, req->rq_stripe,
1311                        req->rq_oi.oi_oa->o_id, req->rq_idx);
1312
1313                 rc = obd_setattr_async(lov->lov_tgts[req->rq_idx]->ltd_exp,
1314                                        &req->rq_oi, oti, rqset);
1315                 if (rc) {
1316                         CERROR("error: setattr objid "LPX64" subobj "
1317                                LPX64" on OST idx %d: rc = %d\n",
1318                                set->set_oi->oi_oa->o_id,
1319                                req->rq_oi.oi_oa->o_id,
1320                                req->rq_idx, rc);
1321                         break;
1322                 }
1323         }
1324
1325         /* If we are not waiting for responses on async requests, return. */
1326         if (rc || !rqset || list_empty(&rqset->set_requests)) {
1327                 int err;
1328                 if (rc)
1329                         set->set_completes = 0;
1330                 err = lov_fini_setattr_set(set);
1331                 RETURN(rc ? rc : err);
1332         }
1333
1334         LASSERT(rqset->set_interpret == NULL);
1335         rqset->set_interpret = lov_setattr_interpret;
1336         rqset->set_arg = (void *)set;
1337
1338         RETURN(0);
1339 }
1340
1341 static int lov_punch_interpret(struct ptlrpc_request_set *rqset,
1342                                void *data, int rc)
1343 {
1344         struct lov_request_set *lovset = (struct lov_request_set *)data;
1345         int err;
1346         ENTRY;
1347
1348         if (rc)
1349                 lovset->set_completes = 0;
1350         err = lov_fini_punch_set(lovset);
1351         RETURN(rc ? rc : err);
1352 }
1353
1354 /* FIXME: maybe we'll just make one node the authoritative attribute node, then
1355  * we can send this 'punch' to just the authoritative node and the nodes
1356  * that the punch will affect. */
1357 static int lov_punch(struct obd_export *exp, struct obd_info *oinfo,
1358                      struct obd_trans_info *oti,
1359                      struct ptlrpc_request_set *rqset)
1360 {
1361         struct lov_request_set *set;
1362         struct lov_obd *lov;
1363         struct list_head *pos;
1364         struct lov_request *req;
1365         int rc = 0;
1366         ENTRY;
1367
1368         LASSERT(oinfo);
1369         ASSERT_LSM_MAGIC(oinfo->oi_md);
1370
1371         if (!exp || !exp->exp_obd)
1372                 RETURN(-ENODEV);
1373
1374         lov = &exp->exp_obd->u.lov;
1375         rc = lov_prep_punch_set(exp, oinfo, oti, &set);
1376         if (rc)
1377                 RETURN(rc);
1378
1379         list_for_each (pos, &set->set_list) {
1380                 req = list_entry(pos, struct lov_request, rq_link);
1381
1382                 rc = obd_punch(lov->lov_tgts[req->rq_idx]->ltd_exp,
1383                                &req->rq_oi, NULL, rqset);
1384                 if (rc) {
1385                         CERROR("error: punch objid "LPX64" subobj "LPX64
1386                                " on OST idx %d: rc = %d\n",
1387                                set->set_oi->oi_oa->o_id,
1388                                req->rq_oi.oi_oa->o_id, req->rq_idx, rc);
1389                         break;
1390                 }
1391         }
1392
1393         if (rc || list_empty(&rqset->set_requests)) {
1394                 int err;
1395                 err = lov_fini_punch_set(set);
1396                 RETURN(rc ? rc : err);
1397         }
1398
1399         LASSERT(rqset->set_interpret == NULL);
1400         rqset->set_interpret = lov_punch_interpret;
1401         rqset->set_arg = (void *)set;
1402
1403         RETURN(0);
1404 }
1405
1406 static int lov_sync(struct obd_export *exp, struct obdo *oa,
1407                     struct lov_stripe_md *lsm, obd_off start, obd_off end,
1408                     void *capa)
1409 {
1410         struct lov_request_set *set;
1411         struct obd_info oinfo;
1412         struct lov_obd *lov;
1413         struct list_head *pos;
1414         struct lov_request *req;
1415         int err = 0, rc = 0;
1416         ENTRY;
1417
1418         ASSERT_LSM_MAGIC(lsm);
1419
1420         if (!exp->exp_obd)
1421                 RETURN(-ENODEV);
1422
1423         lov = &exp->exp_obd->u.lov;
1424         rc = lov_prep_sync_set(exp, &oinfo, oa, lsm, start, end, &set);
1425         if (rc)
1426                 RETURN(rc);
1427
1428         list_for_each (pos, &set->set_list) {
1429                 req = list_entry(pos, struct lov_request, rq_link);
1430
1431                 rc = obd_sync(lov->lov_tgts[req->rq_idx]->ltd_exp,
1432                               req->rq_oi.oi_oa, NULL,
1433                               req->rq_oi.oi_policy.l_extent.start,
1434                               req->rq_oi.oi_policy.l_extent.end, capa);
1435                 err = lov_update_common_set(set, req, rc);
1436                 if (err) {
1437                         CERROR("error: fsync objid "LPX64" subobj "LPX64
1438                                " on OST idx %d: rc = %d\n",
1439                                set->set_oi->oi_oa->o_id,
1440                                req->rq_oi.oi_oa->o_id, req->rq_idx, rc);
1441                         if (!rc)
1442                                 rc = err;
1443                 }
1444         }
1445         err = lov_fini_sync_set(set);
1446         if (!rc)
1447                 rc = err;
1448         RETURN(rc);
1449 }
1450
1451 static int lov_brw_check(struct lov_obd *lov, struct obd_info *lov_oinfo,
1452                          obd_count oa_bufs, struct brw_page *pga)
1453 {
1454         struct obd_info oinfo = { { { 0 } } };
1455         int i, rc = 0;
1456
1457         oinfo.oi_oa = lov_oinfo->oi_oa;
1458
1459         /* The caller just wants to know if there's a chance that this
1460          * I/O can succeed */
1461         for (i = 0; i < oa_bufs; i++) {
1462                 int stripe = lov_stripe_number(lov_oinfo->oi_md, pga[i].off);
1463                 int ost = lov_oinfo->oi_md->lsm_oinfo[stripe]->loi_ost_idx;
1464                 obd_off start, end;
1465
1466                 if (!lov_stripe_intersects(lov_oinfo->oi_md, i, pga[i].off,
1467                                            pga[i].off + pga[i].count,
1468                                            &start, &end))
1469                         continue;
1470
1471                 if (!lov->lov_tgts[ost] || !lov->lov_tgts[ost]->ltd_active) {
1472                         CDEBUG(D_HA, "lov idx %d inactive\n", ost);
1473                         return -EIO;
1474                 }
1475
1476                 rc = obd_brw(OBD_BRW_CHECK, lov->lov_tgts[ost]->ltd_exp, &oinfo,
1477                              1, &pga[i], NULL);
1478                 if (rc)
1479                         break;
1480         }
1481         return rc;
1482 }
1483
1484 static int lov_brw(int cmd, struct obd_export *exp, struct obd_info *oinfo,
1485                    obd_count oa_bufs, struct brw_page *pga,
1486                    struct obd_trans_info *oti)
1487 {
1488         struct lov_request_set *set;
1489         struct lov_request *req;
1490         struct list_head *pos;
1491         struct lov_obd *lov = &exp->exp_obd->u.lov;
1492         int err, rc = 0;
1493         ENTRY;
1494
1495         ASSERT_LSM_MAGIC(oinfo->oi_md);
1496
1497         if (cmd == OBD_BRW_CHECK) {
1498                 rc = lov_brw_check(lov, oinfo, oa_bufs, pga);
1499                 RETURN(rc);
1500         }
1501
1502         rc = lov_prep_brw_set(exp, oinfo, oa_bufs, pga, oti, &set);
1503         if (rc)
1504                 RETURN(rc);
1505
1506         list_for_each (pos, &set->set_list) {
1507                 struct obd_export *sub_exp;
1508                 struct brw_page *sub_pga;
1509                 req = list_entry(pos, struct lov_request, rq_link);
1510
1511                 sub_exp = lov->lov_tgts[req->rq_idx]->ltd_exp;
1512                 sub_pga = set->set_pga + req->rq_pgaidx;
1513                 rc = obd_brw(cmd, sub_exp, &req->rq_oi, req->rq_oabufs,
1514                              sub_pga, oti);
1515                 if (rc)
1516                         break;
1517                 lov_update_common_set(set, req, rc);
1518         }
1519
1520         err = lov_fini_brw_set(set);
1521         if (!rc)
1522                 rc = err;
1523         RETURN(rc);
1524 }
1525
1526 static int lov_brw_interpret(struct ptlrpc_request_set *reqset, void *data,
1527                              int rc)
1528 {
1529         struct lov_request_set *lovset = (struct lov_request_set *)data;
1530         ENTRY;
1531
1532         if (rc) {
1533                 lovset->set_completes = 0;
1534                 lov_fini_brw_set(lovset);
1535         } else {
1536                 rc = lov_fini_brw_set(lovset);
1537         }
1538
1539         RETURN(rc);
1540 }
1541
1542 static int lov_brw_async(int cmd, struct obd_export *exp,
1543                          struct obd_info *oinfo, obd_count oa_bufs,
1544                          struct brw_page *pga, struct obd_trans_info *oti,
1545                          struct ptlrpc_request_set *set)
1546 {
1547         struct lov_request_set *lovset;
1548         struct lov_request *req;
1549         struct list_head *pos;
1550         struct lov_obd *lov = &exp->exp_obd->u.lov;
1551         int rc = 0;
1552         ENTRY;
1553
1554         LASSERT(oinfo);
1555         ASSERT_LSM_MAGIC(oinfo->oi_md);
1556
1557         if (cmd == OBD_BRW_CHECK) {
1558                 rc = lov_brw_check(lov, oinfo, oa_bufs, pga);
1559                 RETURN(rc);
1560         }
1561
1562         rc = lov_prep_brw_set(exp, oinfo, oa_bufs, pga, oti, &lovset);
1563         if (rc)
1564                 RETURN(rc);
1565
1566         list_for_each (pos, &lovset->set_list) {
1567                 struct obd_export *sub_exp;
1568                 struct brw_page *sub_pga;
1569                 req = list_entry(pos, struct lov_request, rq_link);
1570
1571                 sub_exp = lov->lov_tgts[req->rq_idx]->ltd_exp;
1572                 sub_pga = lovset->set_pga + req->rq_pgaidx;
1573                 rc = obd_brw_async(cmd, sub_exp, &req->rq_oi, req->rq_oabufs,
1574                                    sub_pga, oti, set);
1575                 if (rc)
1576                         GOTO(out, rc);
1577                 lov_update_common_set(lovset, req, rc);
1578         }
1579         LASSERT(rc == 0);
1580         LASSERT(set->set_interpret == NULL);
1581         set->set_interpret = (set_interpreter_func)lov_brw_interpret;
1582         set->set_arg = (void *)lovset;
1583
1584         RETURN(rc);
1585 out:
1586         lov_fini_brw_set(lovset);
1587         RETURN(rc);
1588 }
1589
1590 static int lov_ap_make_ready(void *data, int cmd)
1591 {
1592         struct lov_async_page *lap = LAP_FROM_COOKIE(data);
1593
1594         return lap->lap_caller_ops->ap_make_ready(lap->lap_caller_data, cmd);
1595 }
1596
1597 static int lov_ap_refresh_count(void *data, int cmd)
1598 {
1599         struct lov_async_page *lap = LAP_FROM_COOKIE(data);
1600
1601         return lap->lap_caller_ops->ap_refresh_count(lap->lap_caller_data,
1602                                                      cmd);
1603 }
1604
1605 static void lov_ap_fill_obdo(void *data, int cmd, struct obdo *oa)
1606 {
1607         struct lov_async_page *lap = LAP_FROM_COOKIE(data);
1608
1609         lap->lap_caller_ops->ap_fill_obdo(lap->lap_caller_data, cmd, oa);
1610         /* XXX woah, shouldn't we be altering more here?  size? */
1611         oa->o_id = lap->lap_loi_id;
1612         oa->o_gr = lap->lap_loi_gr;
1613         oa->o_valid |= OBD_MD_FLGROUP;
1614         oa->o_stripe_idx = lap->lap_stripe;
1615 }
1616
1617 static void lov_ap_update_obdo(void *data, int cmd, struct obdo *oa,
1618                                obd_valid valid)
1619 {
1620         struct lov_async_page *lap = LAP_FROM_COOKIE(data);
1621
1622         lap->lap_caller_ops->ap_update_obdo(lap->lap_caller_data, cmd,oa,valid);
1623 }
1624
1625 static int lov_ap_completion(void *data, int cmd, struct obdo *oa, int rc)
1626 {
1627         struct lov_async_page *lap = LAP_FROM_COOKIE(data);
1628
1629         /* in a raid1 regime this would down a count of many ios
1630          * in flight, onl calling the caller_ops completion when all
1631          * the raid1 ios are complete */
1632         rc = lap->lap_caller_ops->ap_completion(lap->lap_caller_data,cmd,oa,rc);
1633         return rc;
1634 }
1635
1636 static struct obd_capa *lov_ap_lookup_capa(void *data, int cmd)
1637 {
1638         struct lov_async_page *lap = LAP_FROM_COOKIE(data);
1639         return lap->lap_caller_ops->ap_lookup_capa(lap->lap_caller_data, cmd);
1640 }
1641
1642 static struct obd_async_page_ops lov_async_page_ops = {
1643         .ap_make_ready =        lov_ap_make_ready,
1644         .ap_refresh_count =     lov_ap_refresh_count,
1645         .ap_fill_obdo =         lov_ap_fill_obdo,
1646         .ap_update_obdo =       lov_ap_update_obdo,
1647         .ap_completion =        lov_ap_completion,
1648         .ap_lookup_capa =       lov_ap_lookup_capa,
1649 };
1650
1651 int lov_prep_async_page(struct obd_export *exp, struct lov_stripe_md *lsm,
1652                            struct lov_oinfo *loi, cfs_page_t *page,
1653                            obd_off offset, struct obd_async_page_ops *ops,
1654                            void *data, void **res)
1655 {
1656         struct lov_obd *lov = &exp->exp_obd->u.lov;
1657         struct lov_async_page *lap;
1658         int rc = 0;
1659         ENTRY;
1660
1661         if (!page) {
1662                 int i = 0;
1663                 /* Find an existing osc so we can get it's stupid sizeof(*oap).
1664                    Only because of this layering limitation will a client
1665                    mount with no osts fail */
1666                 while (!lov->lov_tgts || !lov->lov_tgts[i] ||
1667                        !lov->lov_tgts[i]->ltd_exp) {
1668                         i++;
1669                         if (i >= lov->desc.ld_tgt_count)
1670                                 RETURN(-ENOMEDIUM);
1671                 }
1672                 rc = size_round(sizeof(*lap)) +
1673                         obd_prep_async_page(lov->lov_tgts[i]->ltd_exp, NULL,
1674                                             NULL, NULL, 0, NULL, NULL, NULL);
1675                 RETURN(rc);
1676         }
1677         ASSERT_LSM_MAGIC(lsm);
1678         LASSERT(loi == NULL);
1679
1680         lap = *res;
1681         lap->lap_magic = LOV_AP_MAGIC;
1682         lap->lap_caller_ops = ops;
1683         lap->lap_caller_data = data;
1684
1685         /* for now only raid 0 which passes through */
1686         lap->lap_stripe = lov_stripe_number(lsm, offset);
1687         lov_stripe_offset(lsm, offset, lap->lap_stripe, &lap->lap_sub_offset);
1688         loi = lsm->lsm_oinfo[lap->lap_stripe];
1689
1690         /* so the callback doesn't need the lsm */
1691         lap->lap_loi_id = loi->loi_id;
1692         lap->lap_loi_gr = lsm->lsm_object_gr;
1693         LASSERT(lsm->lsm_object_gr > 0);
1694         
1695         lap->lap_sub_cookie = (void *)lap + size_round(sizeof(*lap));
1696
1697         rc = obd_prep_async_page(lov->lov_tgts[loi->loi_ost_idx]->ltd_exp,
1698                                  lsm, loi, page, lap->lap_sub_offset,
1699                                  &lov_async_page_ops, lap,
1700                                  &lap->lap_sub_cookie);
1701         if (rc)
1702                 RETURN(rc);
1703         CDEBUG(D_CACHE, "lap %p page %p cookie %p off "LPU64"\n", lap, page,
1704                lap->lap_sub_cookie, offset);
1705         RETURN(0);
1706 }
1707
1708 static int lov_queue_async_io(struct obd_export *exp,
1709                               struct lov_stripe_md *lsm,
1710                               struct lov_oinfo *loi, void *cookie,
1711                               int cmd, obd_off off, int count,
1712                               obd_flag brw_flags, obd_flag async_flags)
1713 {
1714         struct lov_obd *lov = &exp->exp_obd->u.lov;
1715         struct lov_async_page *lap;
1716         int rc;
1717
1718         LASSERT(loi == NULL);
1719
1720         ASSERT_LSM_MAGIC(lsm);
1721
1722         lap = LAP_FROM_COOKIE(cookie);
1723
1724         loi = lsm->lsm_oinfo[lap->lap_stripe];
1725
1726         rc = obd_queue_async_io(lov->lov_tgts[loi->loi_ost_idx]->ltd_exp, lsm,
1727                                 loi, lap->lap_sub_cookie, cmd, off, count,
1728                                 brw_flags, async_flags);
1729         RETURN(rc);
1730 }
1731
1732 static int lov_set_async_flags(struct obd_export *exp,
1733                                struct lov_stripe_md *lsm,
1734                                struct lov_oinfo *loi, void *cookie,
1735                                obd_flag async_flags)
1736 {
1737         struct lov_obd *lov = &exp->exp_obd->u.lov;
1738         struct lov_async_page *lap;
1739         int rc;
1740
1741         LASSERT(loi == NULL);
1742
1743         ASSERT_LSM_MAGIC(lsm);
1744
1745         lap = LAP_FROM_COOKIE(cookie);
1746
1747         loi = lsm->lsm_oinfo[lap->lap_stripe];
1748
1749         rc = obd_set_async_flags(lov->lov_tgts[loi->loi_ost_idx]->ltd_exp,
1750                                  lsm, loi, lap->lap_sub_cookie, async_flags);
1751         RETURN(rc);
1752 }
1753
1754 static int lov_queue_group_io(struct obd_export *exp,
1755                               struct lov_stripe_md *lsm,
1756                               struct lov_oinfo *loi,
1757                               struct obd_io_group *oig, void *cookie,
1758                               int cmd, obd_off off, int count,
1759                               obd_flag brw_flags, obd_flag async_flags)
1760 {
1761         struct lov_obd *lov = &exp->exp_obd->u.lov;
1762         struct lov_async_page *lap;
1763         int rc;
1764
1765         LASSERT(loi == NULL);
1766
1767         ASSERT_LSM_MAGIC(lsm);
1768
1769         lap = LAP_FROM_COOKIE(cookie);
1770
1771         loi = lsm->lsm_oinfo[lap->lap_stripe];
1772
1773         rc = obd_queue_group_io(lov->lov_tgts[loi->loi_ost_idx]->ltd_exp, lsm,
1774                                 loi, oig, lap->lap_sub_cookie, cmd, off, count,
1775                                 brw_flags, async_flags);
1776         RETURN(rc);
1777 }
1778
1779 /* this isn't exactly optimal.  we may have queued sync io in oscs on
1780  * all stripes, but we don't record that fact at queue time.  so we
1781  * trigger sync io on all stripes. */
1782 static int lov_trigger_group_io(struct obd_export *exp,
1783                                 struct lov_stripe_md *lsm,
1784                                 struct lov_oinfo *loi,
1785                                 struct obd_io_group *oig)
1786 {
1787         struct lov_obd *lov = &exp->exp_obd->u.lov;
1788         int rc = 0, i, err;
1789
1790         LASSERT(loi == NULL);
1791
1792         ASSERT_LSM_MAGIC(lsm);
1793
1794         for (i = 0; i < lsm->lsm_stripe_count; i++) {
1795                 loi = lsm->lsm_oinfo[i];
1796                 if (!lov->lov_tgts[loi->loi_ost_idx] ||
1797                     !lov->lov_tgts[loi->loi_ost_idx]->ltd_active) {
1798                         CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
1799                         continue;
1800                 }
1801
1802                 err = obd_trigger_group_io(lov->lov_tgts[loi->loi_ost_idx]->ltd_exp,
1803                                            lsm, loi, oig);
1804                 if (rc == 0 && err != 0)
1805                         rc = err;
1806         };
1807         RETURN(rc);
1808 }
1809
1810 static int lov_teardown_async_page(struct obd_export *exp,
1811                                    struct lov_stripe_md *lsm,
1812                                    struct lov_oinfo *loi, void *cookie)
1813 {
1814         struct lov_obd *lov = &exp->exp_obd->u.lov;
1815         struct lov_async_page *lap;
1816         int rc;
1817
1818         LASSERT(loi == NULL);
1819
1820         ASSERT_LSM_MAGIC(lsm);
1821
1822         lap = LAP_FROM_COOKIE(cookie);
1823
1824         loi = lsm->lsm_oinfo[lap->lap_stripe];
1825
1826         rc = obd_teardown_async_page(lov->lov_tgts[loi->loi_ost_idx]->ltd_exp,
1827                                      lsm, loi, lap->lap_sub_cookie);
1828         if (rc) {
1829                 CERROR("unable to teardown sub cookie %p: %d\n",
1830                        lap->lap_sub_cookie, rc);
1831                 RETURN(rc);
1832         }
1833         RETURN(rc);
1834 }
1835
1836 static int lov_enqueue_interpret(struct ptlrpc_request_set *rqset,
1837                                  void *data, int rc)
1838 {
1839         struct lov_request_set *lovset = (struct lov_request_set *)data;
1840         ENTRY;
1841         rc = lov_fini_enqueue_set(lovset, lovset->set_ei->ei_mode, rc, rqset);
1842         RETURN(rc);
1843 }
1844
1845 static int lov_enqueue(struct obd_export *exp, struct obd_info *oinfo,
1846                        struct ldlm_enqueue_info *einfo,
1847                        struct ptlrpc_request_set *rqset)
1848 {
1849         ldlm_mode_t mode = einfo->ei_mode;
1850         struct lov_request_set *set;
1851         struct lov_request *req;
1852         struct list_head *pos;
1853         struct lov_obd *lov;
1854         ldlm_error_t rc;
1855         ENTRY;
1856
1857         LASSERT(oinfo);
1858         ASSERT_LSM_MAGIC(oinfo->oi_md);
1859         LASSERT(mode == (mode & -mode));
1860
1861         /* we should never be asked to replay a lock this way. */
1862         LASSERT((oinfo->oi_flags & LDLM_FL_REPLAY) == 0);
1863
1864         if (!exp || !exp->exp_obd)
1865                 RETURN(-ENODEV);
1866
1867         lov = &exp->exp_obd->u.lov;
1868         rc = lov_prep_enqueue_set(exp, oinfo, einfo, &set);
1869         if (rc)
1870                 RETURN(rc);
1871
1872         list_for_each (pos, &set->set_list) {
1873                 req = list_entry(pos, struct lov_request, rq_link);
1874
1875                 rc = obd_enqueue(lov->lov_tgts[req->rq_idx]->ltd_exp,
1876                                  &req->rq_oi, einfo, rqset);
1877                 if (rc != ELDLM_OK)
1878                         GOTO(out, rc);
1879         }
1880
1881         if (rqset && !list_empty(&rqset->set_requests)) {
1882                 LASSERT(rc == 0);
1883                 LASSERT(rqset->set_interpret == NULL);
1884                 rqset->set_interpret = lov_enqueue_interpret;
1885                 rqset->set_arg = (void *)set;
1886                 RETURN(rc);
1887         }
1888 out:
1889         rc = lov_fini_enqueue_set(set, mode, rc, rqset);
1890         RETURN(rc);
1891 }
1892
1893 static int lov_match(struct obd_export *exp, struct lov_stripe_md *lsm,
1894                      __u32 type, ldlm_policy_data_t *policy, __u32 mode,
1895                      int *flags, void *data, struct lustre_handle *lockh)
1896 {
1897         struct lov_request_set *set;
1898         struct obd_info oinfo;
1899         struct lov_request *req;
1900         struct list_head *pos;
1901         struct lov_obd *lov = &exp->exp_obd->u.lov;
1902         struct lustre_handle *lov_lockhp;
1903         int lov_flags, rc = 0;
1904         ENTRY;
1905
1906         ASSERT_LSM_MAGIC(lsm);
1907         LASSERT((*flags & LDLM_FL_TEST_LOCK) || mode == (mode & -mode));
1908
1909         if (!exp || !exp->exp_obd)
1910                 RETURN(-ENODEV);
1911
1912         lov = &exp->exp_obd->u.lov;
1913         rc = lov_prep_match_set(exp, &oinfo, lsm, policy, mode, lockh, &set);
1914         if (rc)
1915                 RETURN(rc);
1916
1917         list_for_each (pos, &set->set_list) {
1918                 ldlm_policy_data_t sub_policy;
1919                 req = list_entry(pos, struct lov_request, rq_link);
1920                 lov_lockhp = set->set_lockh->llh_handles + req->rq_stripe;
1921                 LASSERT(lov_lockhp);
1922
1923                 lov_flags = *flags;
1924                 sub_policy.l_extent = req->rq_oi.oi_policy.l_extent;
1925
1926                 rc = obd_match(lov->lov_tgts[req->rq_idx]->ltd_exp,
1927                                req->rq_oi.oi_md, type, &sub_policy,
1928                                mode, &lov_flags, data, lov_lockhp);
1929                 rc = lov_update_match_set(set, req, rc);
1930                 if (rc <= 0)
1931                         break;
1932         }
1933         lov_fini_match_set(set, mode, *flags);
1934         RETURN(rc);
1935 }
1936
1937 static int lov_change_cbdata(struct obd_export *exp,
1938                              struct lov_stripe_md *lsm, ldlm_iterator_t it,
1939                              void *data)
1940 {
1941         struct lov_obd *lov;
1942         int rc = 0, i;
1943         ENTRY;
1944
1945         ASSERT_LSM_MAGIC(lsm);
1946
1947         if (!exp || !exp->exp_obd)
1948                 RETURN(-ENODEV);
1949
1950         LASSERT(lsm->lsm_object_gr > 0);
1951
1952         lov = &exp->exp_obd->u.lov;
1953         for (i = 0; i < lsm->lsm_stripe_count; i++) {
1954                 struct lov_stripe_md submd;
1955                 struct lov_oinfo *loi = lsm->lsm_oinfo[i];
1956
1957                 if (!lov->lov_tgts[loi->loi_ost_idx]) {
1958                         CDEBUG(D_HA, "lov idx %d NULL \n", loi->loi_ost_idx);
1959                         continue;
1960                 }
1961                 
1962                 submd.lsm_object_id = loi->loi_id;
1963                 submd.lsm_object_gr = lsm->lsm_object_gr;
1964                 submd.lsm_stripe_count = 0;
1965                 rc = obd_change_cbdata(lov->lov_tgts[loi->loi_ost_idx]->ltd_exp,
1966                                        &submd, it, data);
1967         }
1968         RETURN(rc);
1969 }
1970
1971 static int lov_cancel(struct obd_export *exp, struct lov_stripe_md *lsm,
1972                       __u32 mode, struct lustre_handle *lockh)
1973 {
1974         struct lov_request_set *set;
1975         struct obd_info oinfo;
1976         struct lov_request *req;
1977         struct list_head *pos;
1978         struct lov_obd *lov = &exp->exp_obd->u.lov;
1979         struct lustre_handle *lov_lockhp;
1980         int err = 0, rc = 0;
1981         ENTRY;
1982
1983         ASSERT_LSM_MAGIC(lsm);
1984
1985         if (!exp || !exp->exp_obd)
1986                 RETURN(-ENODEV);
1987
1988         LASSERT(lsm->lsm_object_gr > 0);
1989         LASSERT(lockh);
1990         lov = &exp->exp_obd->u.lov;
1991         rc = lov_prep_cancel_set(exp, &oinfo, lsm, mode, lockh, &set);
1992         if (rc)
1993                 RETURN(rc);
1994
1995         list_for_each (pos, &set->set_list) {
1996                 req = list_entry(pos, struct lov_request, rq_link);
1997                 lov_lockhp = set->set_lockh->llh_handles + req->rq_stripe;
1998
1999                 rc = obd_cancel(lov->lov_tgts[req->rq_idx]->ltd_exp,
2000                                 req->rq_oi.oi_md, mode, lov_lockhp);
2001                 rc = lov_update_common_set(set, req, rc);
2002                 if (rc) {
2003                         CERROR("error: cancel objid "LPX64" subobj "
2004                                LPX64" on OST idx %d: rc = %d\n",
2005                                lsm->lsm_object_id,
2006                                req->rq_oi.oi_md->lsm_object_id,
2007                                req->rq_idx, rc);
2008                         err = rc;
2009                 }
2010
2011         }
2012         lov_fini_cancel_set(set);
2013         RETURN(err);
2014 }
2015
2016 static int lov_cancel_unused(struct obd_export *exp,
2017                              struct lov_stripe_md *lsm,
2018                              int flags, void *opaque)
2019 {
2020         struct lov_obd *lov;
2021         int rc = 0, i;
2022         ENTRY;
2023
2024         if (!exp || !exp->exp_obd)
2025                 RETURN(-ENODEV);
2026
2027         lov = &exp->exp_obd->u.lov;
2028         if (lsm == NULL) {
2029                 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
2030                         int err;
2031                         if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_exp)
2032                                 continue;
2033
2034                         err = obd_cancel_unused(lov->lov_tgts[i]->ltd_exp, NULL,
2035                                                 flags, opaque);
2036                         if (!rc)
2037                                 rc = err;
2038                 }
2039                 RETURN(rc);
2040         }
2041
2042         ASSERT_LSM_MAGIC(lsm);
2043
2044         LASSERT(lsm->lsm_object_gr > 0);
2045         for (i = 0; i < lsm->lsm_stripe_count; i++) {
2046                 struct lov_stripe_md submd;
2047                 struct lov_oinfo *loi = lsm->lsm_oinfo[i];
2048                 int err;
2049
2050                 if (!lov->lov_tgts[loi->loi_ost_idx]) {
2051                         CDEBUG(D_HA, "lov idx %d NULL\n", loi->loi_ost_idx);
2052                         continue;
2053                 }
2054
2055                 if (!lov->lov_tgts[loi->loi_ost_idx]->ltd_active)
2056                         CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
2057
2058                 submd.lsm_object_id = loi->loi_id;
2059                 submd.lsm_object_gr = lsm->lsm_object_gr;
2060                 submd.lsm_stripe_count = 0;
2061                 err = obd_cancel_unused(lov->lov_tgts[loi->loi_ost_idx]->ltd_exp,
2062                                         &submd, flags, opaque);
2063                 if (err && lov->lov_tgts[loi->loi_ost_idx]->ltd_active) {
2064                         CERROR("error: cancel unused objid "LPX64" subobj "LPX64
2065                                " on OST idx %d: rc = %d\n", lsm->lsm_object_id,
2066                                loi->loi_id, loi->loi_ost_idx, err);
2067                         if (!rc)
2068                                 rc = err;
2069                 }
2070         }
2071         RETURN(rc);
2072 }
2073
2074 static int lov_join_lru(struct obd_export *exp,
2075                         struct lov_stripe_md *lsm, int join)
2076 {
2077         struct lov_obd *lov;
2078         int i, count = 0;
2079         ENTRY;
2080
2081         ASSERT_LSM_MAGIC(lsm);
2082         if (!exp || !exp->exp_obd)
2083                 RETURN(-ENODEV);
2084
2085         lov = &exp->exp_obd->u.lov;
2086         for (i = 0; i < lsm->lsm_stripe_count; i++) {
2087                 struct lov_stripe_md submd;
2088                 struct lov_oinfo *loi = lsm->lsm_oinfo[i];
2089                 int rc = 0;
2090
2091                 if (!lov->lov_tgts[loi->loi_ost_idx]) {
2092                         CDEBUG(D_HA, "lov idx %d NULL\n", loi->loi_ost_idx);
2093                         continue;
2094                 }
2095
2096                 if (!lov->lov_tgts[loi->loi_ost_idx]->ltd_active)
2097                         CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
2098
2099                 submd.lsm_object_id = loi->loi_id;
2100                 submd.lsm_object_gr = lsm->lsm_object_gr;
2101                 submd.lsm_stripe_count = 0;
2102                 rc = obd_join_lru(lov->lov_tgts[loi->loi_ost_idx]->ltd_exp,
2103                                   &submd, join);
2104                 if (rc < 0) {
2105                         CERROR("join lru failed. objid: "LPX64" subobj: "LPX64
2106                                " ostidx: %d rc: %d\n", lsm->lsm_object_id,
2107                                loi->loi_id, loi->loi_ost_idx, rc);
2108                         return rc;
2109                 } else {
2110                         count += rc;
2111                 }
2112         }
2113         RETURN(count);
2114 }
2115
2116 static int lov_statfs_interpret(struct ptlrpc_request_set *rqset,
2117                                 void *data, int rc)
2118 {
2119         struct lov_request_set *lovset = (struct lov_request_set *)data;
2120         int err;
2121         ENTRY;
2122
2123         if (rc)
2124                 lovset->set_completes = 0;
2125
2126         err = lov_fini_statfs_set(lovset);
2127         RETURN(rc ? rc : err);
2128 }
2129
2130 static int lov_statfs_async(struct obd_device *obd, struct obd_info *oinfo,
2131                             __u64 max_age, struct ptlrpc_request_set *rqset)
2132 {
2133         struct lov_request_set *set;
2134         struct lov_request *req;
2135         struct list_head *pos;
2136         struct lov_obd *lov;
2137         int rc = 0;
2138         ENTRY;
2139         
2140         LASSERT(oinfo != NULL);
2141         LASSERT(oinfo->oi_osfs != NULL);
2142
2143         lov = &obd->u.lov;
2144         rc = lov_prep_statfs_set(obd, oinfo, &set);
2145         if (rc)
2146                 RETURN(rc);
2147
2148         list_for_each (pos, &set->set_list) {
2149                 struct obd_device *osc_obd;
2150
2151                 req = list_entry(pos, struct lov_request, rq_link);
2152
2153                 osc_obd = class_exp2obd(lov->lov_tgts[req->rq_idx]->ltd_exp);
2154                 rc = obd_statfs_async(osc_obd, &req->rq_oi, max_age, rqset);
2155                 if (rc)
2156                         break;
2157         }
2158
2159         if (rc || list_empty(&rqset->set_requests)) {
2160                 int err;
2161                 if (rc)
2162                         set->set_completes = 0;
2163                 err = lov_fini_statfs_set(set);
2164                 RETURN(rc ? rc : err);
2165         }
2166
2167         LASSERT(rqset->set_interpret == NULL);
2168         rqset->set_interpret = lov_statfs_interpret;
2169         rqset->set_arg = (void *)set;
2170         RETURN(0);
2171 }
2172
2173 static int lov_statfs(struct obd_device *obd, struct obd_statfs *osfs,
2174                       __u64 max_age)
2175 {
2176         struct lov_obd *lov = &obd->u.lov;
2177         struct obd_statfs lov_sfs;
2178         int set = 0;
2179         int rc = 0, err;
2180         int i;
2181         ENTRY;
2182
2183         /* We only get block data from the OBD */
2184         for (i = 0; i < lov->desc.ld_tgt_count; i++) {
2185                 if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_active) {
2186                         CDEBUG(D_HA, "lov idx %d inactive\n", i);
2187                         continue;
2188                 }
2189
2190                 err = obd_statfs(class_exp2obd(lov->lov_tgts[i]->ltd_exp),
2191                                  &lov_sfs, max_age);
2192                 if (err) {
2193                         if (lov->lov_tgts[i]->ltd_active && !rc)
2194                                 rc = err;
2195                         continue;
2196                 }
2197                 lov_update_statfs(class_exp2obd(lov->lov_tgts[i]->ltd_exp),
2198                                   osfs, &lov_sfs, set);
2199                 set++;
2200         }
2201
2202         err = lov_fini_statfs(obd, osfs, set);
2203         qos_update(lov);
2204
2205         RETURN(rc ? rc : err);
2206 }
2207
2208 static int lov_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
2209                          void *karg, void *uarg)
2210 {
2211         struct obd_device *obddev = class_exp2obd(exp);
2212         struct lov_obd *lov = &obddev->u.lov;
2213         int i, rc, count = lov->desc.ld_tgt_count;
2214         struct obd_uuid *uuidp;
2215         ENTRY;
2216
2217         switch (cmd) {
2218         case OBD_IOC_LOV_GET_CONFIG: {
2219                 struct obd_ioctl_data *data;
2220                 struct lov_desc *desc;
2221                 char *buf = NULL;
2222                 __u32 *genp;
2223
2224                 len = 0;
2225                 if (obd_ioctl_getdata(&buf, &len, (void *)uarg))
2226                         RETURN(-EINVAL);
2227
2228                 data = (struct obd_ioctl_data *)buf;
2229
2230                 if (sizeof(*desc) > data->ioc_inllen1) {
2231                         obd_ioctl_freedata(buf, len);
2232                         RETURN(-EINVAL);
2233                 }
2234
2235                 if (sizeof(uuidp->uuid) * count > data->ioc_inllen2) {
2236                         obd_ioctl_freedata(buf, len);
2237                         RETURN(-EINVAL);
2238                 }
2239
2240                 if (sizeof(__u32) * count > data->ioc_inllen3) {
2241                         obd_ioctl_freedata(buf, len);
2242                         RETURN(-EINVAL);
2243                 }
2244
2245                 desc = (struct lov_desc *)data->ioc_inlbuf1;
2246                 memcpy(desc, &(lov->desc), sizeof(*desc));
2247
2248                 uuidp = (struct obd_uuid *)data->ioc_inlbuf2;
2249                 genp = (__u32 *)data->ioc_inlbuf3;
2250                 /* the uuid will be empty for deleted OSTs */
2251                 for (i = 0; i < count; i++, uuidp++, genp++) {
2252                         if (!lov->lov_tgts[i])
2253                                 continue;
2254                         *uuidp = lov->lov_tgts[i]->ltd_uuid;
2255                         *genp = lov->lov_tgts[i]->ltd_gen;
2256                 }
2257
2258                 rc = copy_to_user((void *)uarg, buf, len);
2259                 if (rc)
2260                         rc = -EFAULT;
2261                 obd_ioctl_freedata(buf, len);
2262                 break;
2263         }
2264         case LL_IOC_LOV_SETSTRIPE:
2265                 rc = lov_setstripe(exp, karg, uarg);
2266                 break;
2267         case LL_IOC_LOV_GETSTRIPE:
2268                 rc = lov_getstripe(exp, karg, uarg);
2269                 break;
2270         case LL_IOC_LOV_SETEA:
2271                 rc = lov_setea(exp, karg, uarg);
2272                 break;
2273         default: {
2274                 int set = 0;
2275
2276                 if (count == 0)
2277                         RETURN(-ENOTTY);
2278
2279                 rc = 0;
2280                 for (i = 0; i < count; i++) {
2281                         int err;
2282
2283                         /* OST was disconnected */
2284                         if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_exp)
2285                                 continue;
2286
2287                         err = obd_iocontrol(cmd, lov->lov_tgts[i]->ltd_exp,
2288                                             len, karg, uarg);
2289                         if (err == -ENODATA && cmd == OBD_IOC_POLL_QUOTACHECK) {
2290                                 RETURN(err);
2291                         } else if (err) {
2292                                 if (lov->lov_tgts[i]->ltd_active) {
2293                                         CDEBUG(err == -ENOTTY ?
2294                                                D_IOCTL : D_WARNING,
2295                                                "iocontrol OSC %s on OST "
2296                                                "idx %d cmd %x: err = %d\n",
2297                                                lov_uuid2str(lov, i),
2298                                                i, cmd, err);
2299                                         if (!rc)
2300                                                 rc = err;
2301                                 }
2302                         } else {
2303                                 set = 1;
2304                         }
2305                 }
2306                 if (!set && !rc)
2307                         rc = -EIO;
2308         }
2309         }
2310
2311         RETURN(rc);
2312 }
2313
2314 static int lov_get_info(struct obd_export *exp, __u32 keylen,
2315                         void *key, __u32 *vallen, void *val)
2316 {
2317         struct obd_device *obddev = class_exp2obd(exp);
2318         struct lov_obd *lov = &obddev->u.lov;
2319         int i, rc;
2320         ENTRY;
2321
2322         if (!vallen || !val)
2323                 RETURN(-EFAULT);
2324
2325         lov_getref(obddev);
2326
2327         if (KEY_IS("lock_to_stripe")) {
2328                 struct {
2329                         char name[16];
2330                         struct ldlm_lock *lock;
2331                         struct lov_stripe_md *lsm;
2332                 } *data = key;
2333                 struct ldlm_res_id *res_id = &data->lock->l_resource->lr_name;
2334                 struct lov_oinfo *loi;
2335                 __u32 *stripe = val;
2336
2337                 if (*vallen < sizeof(*stripe))
2338                         GOTO(out, rc = -EFAULT);
2339                 *vallen = sizeof(*stripe);
2340
2341                 /* XXX This is another one of those bits that will need to
2342                  * change if we ever actually support nested LOVs.  It uses
2343                  * the lock's export to find out which stripe it is. */
2344                 /* XXX - it's assumed all the locks for deleted OSTs have
2345                  * been cancelled. Also, the export for deleted OSTs will
2346                  * be NULL and won't match the lock's export. */
2347                 for (i = 0; i < data->lsm->lsm_stripe_count; i++) {
2348                         loi = data->lsm->lsm_oinfo[i];
2349                         if (!lov->lov_tgts[loi->loi_ost_idx])
2350                                 continue;
2351                         if (lov->lov_tgts[loi->loi_ost_idx]->ltd_exp ==
2352                             data->lock->l_conn_export &&
2353                             loi->loi_id == res_id->name[0] &&
2354                             loi->loi_gr == res_id->name[2]) {
2355                                 *stripe = i;
2356                                 GOTO(out, rc = 0);
2357                         }
2358                 }
2359                 LDLM_ERROR(data->lock, "lock on inode without such object");
2360                 dump_lsm(D_ERROR, data->lsm);
2361                 GOTO(out, rc = -ENXIO);
2362         } else if (KEY_IS("last_id")) {
2363                 obd_id *ids = val;
2364                 int size = sizeof(obd_id);
2365                 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
2366                         if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_active)
2367                                 continue;
2368                         rc = obd_get_info(lov->lov_tgts[i]->ltd_exp,
2369                                           keylen, key, &size, ids + i);
2370                         if (rc != 0)
2371                                 GOTO(out, rc);
2372                 }
2373                 GOTO(out, rc = 0);
2374         } else if (KEY_IS(KEY_LOVDESC)) {
2375                 struct lov_desc *desc_ret = val;
2376                 *desc_ret = lov->desc;
2377
2378                 GOTO(out, rc = 0);
2379         }
2380
2381         rc = -EINVAL;
2382 out:
2383         lov_putref(obddev);
2384         RETURN(rc);
2385 }
2386
2387 static int lov_set_info_async(struct obd_export *exp, obd_count keylen,
2388                               void *key, obd_count vallen, void *val,
2389                               struct ptlrpc_request_set *set)
2390 {
2391         struct obd_device *obddev = class_exp2obd(exp);
2392         struct lov_obd *lov = &obddev->u.lov;
2393         obd_count count;
2394         int i, rc = 0, err;
2395         int no_set = !set;
2396         int incr = 0, check_uuid = 0, do_inactive = 0;
2397         ENTRY;
2398
2399         if (no_set) {
2400                 set = ptlrpc_prep_set();
2401                 if (!set)
2402                         RETURN(-ENOMEM);
2403         }
2404
2405         lov_getref(obddev);
2406         count = lov->desc.ld_tgt_count;
2407
2408         if (KEY_IS(KEY_NEXT_ID)) {
2409                 /* We must use mds's idea of # osts for indexing into
2410                    mds->mds_lov_objids */
2411                 count = vallen / sizeof(obd_id);
2412                 LASSERT(count <= lov->desc.ld_tgt_count);
2413                 vallen = sizeof(obd_id);
2414                 incr = sizeof(obd_id);
2415                 do_inactive = 1;
2416         } else if (KEY_IS("checksum")) {
2417                 do_inactive = 1;
2418         } else if (KEY_IS("unlinked")) {
2419                 check_uuid = val ? 1 : 0;
2420         } else if (KEY_IS("evict_by_nid")) {
2421                 /* use defaults:  do_inactive = incr = 0; */
2422         }
2423
2424         for (i = 0; i < count; i++, val = (char *)val + incr) {
2425                 /* OST was disconnected */
2426                 if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_exp)
2427                         continue;
2428
2429                 /* OST is inactive and we don't want inactive OSCs */
2430                 if (!lov->lov_tgts[i]->ltd_active && !do_inactive)
2431                         continue;
2432
2433                 if (KEY_IS(KEY_MDS_CONN)) {
2434                         struct mds_group_info *mgi;
2435
2436                         LASSERT(vallen == sizeof(*mgi));
2437                         mgi = (struct mds_group_info *)val;
2438                         
2439                         /* Only want a specific OSC */
2440                         if (mgi->uuid && !obd_uuid_equals(mgi->uuid,
2441                                                 &lov->lov_tgts[i]->ltd_uuid))
2442                                 continue;
2443
2444                         err = obd_set_info_async(lov->lov_tgts[i]->ltd_exp,
2445                                          keylen, key, sizeof(int), 
2446                                          &mgi->group, set);
2447                 } else {
2448                         /* Only want a specific OSC */
2449                         if (check_uuid &&
2450                             !obd_uuid_equals(val, &lov->lov_tgts[i]->ltd_uuid))
2451                                 continue;
2452                 
2453                         err = obd_set_info_async(lov->lov_tgts[i]->ltd_exp,
2454                                          keylen, key, vallen, val, set);
2455                 }
2456                 
2457                 if (!rc)
2458                         rc = err;
2459         }
2460
2461         lov_putref(obddev);
2462         if (no_set) {
2463                 err = ptlrpc_set_wait(set);
2464                 if (!rc)
2465                         rc = err;
2466                 ptlrpc_set_destroy(set);
2467         }
2468         RETURN(rc);
2469 }
2470
2471 static int lov_checkmd(struct obd_export *exp, struct obd_export *md_exp,
2472                        struct lov_stripe_md *lsm)
2473 {
2474         int rc;
2475         ENTRY;
2476
2477         if (!lsm)
2478                 RETURN(0);
2479         LASSERT(md_exp);
2480         LASSERT(lsm_op_find(lsm->lsm_magic) != NULL);
2481         rc = lsm_op_find(lsm->lsm_magic)->lsm_revalidate(lsm, md_exp->exp_obd);
2482
2483         RETURN(rc);
2484 }
2485
2486 int lov_test_and_clear_async_rc(struct lov_stripe_md *lsm)
2487 {
2488         int i, rc = 0;
2489         ENTRY;
2490
2491         for (i = 0; i < lsm->lsm_stripe_count; i++) {
2492                 struct lov_oinfo *loi = lsm->lsm_oinfo[i];
2493                 if (loi->loi_ar.ar_rc && !rc)
2494                         rc = loi->loi_ar.ar_rc;
2495                 loi->loi_ar.ar_rc = 0;
2496         }
2497         RETURN(rc);
2498 }
2499 EXPORT_SYMBOL(lov_test_and_clear_async_rc);
2500
2501
2502 static int lov_extent_calc(struct obd_export *exp, struct lov_stripe_md *lsm,
2503                            int cmd, __u64 *offset)
2504 {
2505         __u32 ssize = lsm->lsm_stripe_size;
2506         __u64 start;
2507
2508         start = *offset;
2509         do_div(start, ssize);
2510         start = start * ssize;
2511
2512         CDEBUG(D_DLMTRACE, "offset "LPU64", stripe %u, start "LPU64
2513                            ", end "LPU64"\n", *offset, ssize, start,
2514                            start + ssize - 1);
2515         if (cmd == OBD_CALC_STRIPE_END) {
2516                 *offset = start + ssize - 1;
2517         } else if (cmd == OBD_CALC_STRIPE_START) {
2518                 *offset = start;
2519         } else {
2520                 LBUG();
2521         }
2522
2523         RETURN(0);
2524 }
2525
2526
2527 #if 0
2528 struct lov_multi_wait {
2529         struct ldlm_lock *lock;
2530         wait_queue_t      wait;
2531         int               completed;
2532         int               generation;
2533 };
2534
2535 int lov_complete_many(struct obd_export *exp, struct lov_stripe_md *lsm,
2536                       struct lustre_handle *lockh)
2537 {
2538         struct lov_lock_handles *lov_lockh = NULL;
2539         struct lustre_handle *lov_lockhp;
2540         struct lov_obd *lov;
2541         struct lov_oinfo *loi;
2542         struct lov_multi_wait *queues;
2543         int rc = 0, i;
2544         ENTRY;
2545
2546         ASSERT_LSM_MAGIC(lsm);
2547
2548         if (!exp || !exp->exp_obd)
2549                 RETURN(-ENODEV);
2550
2551         LASSERT(lockh != NULL);
2552         if (lsm->lsm_stripe_count > 1) {
2553                 lov_lockh = lov_handle2llh(lockh);
2554                 if (lov_lockh == NULL) {
2555                         CERROR("LOV: invalid lov lock handle %p\n", lockh);
2556                         RETURN(-EINVAL);
2557                 }
2558
2559                 lov_lockhp = lov_lockh->llh_handles;
2560         } else {
2561                 lov_lockhp = lockh;
2562         }
2563
2564         OBD_ALLOC(queues, lsm->lsm_stripe_count * sizeof(*queues));
2565         if (queues == NULL)
2566                 GOTO(out, rc = -ENOMEM);
2567
2568         lov = &exp->exp_obd->u.lov;
2569         for (i = 0, loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count;
2570              i++, loi++, lov_lockhp++) {
2571                 struct ldlm_lock *lock;
2572                 struct obd_device *obd;
2573
2574                 lock = ldlm_handle2lock(lov_lockhp);
2575                 if (lock == NULL) {
2576                         CDEBUG(D_HA, "lov idx %d subobj "LPX64" no lock?\n",
2577                                loi->loi_ost_idx, loi->loi_id);
2578                         queues[i].completed = 1;
2579                         continue;
2580                 }
2581
2582                 queues[i].lock = lock;
2583                 init_waitqueue_entry(&(queues[i].wait), current);
2584                 add_wait_queue(lock->l_waitq, &(queues[i].wait));
2585
2586                 obd = class_exp2obd(lock->l_conn_export);
2587                 if (obd != NULL)
2588                         imp = obd->u.cli.cl_import;
2589                 if (imp != NULL) {
2590                         spin_lock(&imp->imp_lock);
2591                         queues[i].generation = imp->imp_generation;
2592                         spin_unlock(&imp->imp_lock);
2593                 }
2594         }
2595
2596         lwi = LWI_TIMEOUT_INTR(obd_timeout * HZ, ldlm_expired_completion_wait,
2597                                interrupted_completion_wait, &lwd);
2598         rc = l_wait_event_added(check_multi_complete(queues, lsm), &lwi);
2599
2600         for (i = 0; i < lsm->lsm_stripe_count; i++)
2601                 remove_wait_queue(lock->l_waitq, &(queues[i].wait));
2602
2603         if (rc == -EINTR || rc == -ETIMEDOUT) {
2604
2605
2606         }
2607
2608  out:
2609         if (lov_lockh != NULL)
2610                 lov_llh_put(lov_lockh);
2611         RETURN(rc);
2612 }
2613 #endif
2614
2615 void lov_stripe_lock(struct lov_stripe_md *md)
2616 {
2617         LASSERT(md->lsm_lock_owner != cfs_current());
2618         spin_lock(&md->lsm_lock);
2619         LASSERT(md->lsm_lock_owner == NULL);
2620         md->lsm_lock_owner = cfs_current();
2621 }
2622 EXPORT_SYMBOL(lov_stripe_lock);
2623
2624 void lov_stripe_unlock(struct lov_stripe_md *md)
2625 {
2626         LASSERT(md->lsm_lock_owner == cfs_current());
2627         md->lsm_lock_owner = NULL;
2628         spin_unlock(&md->lsm_lock);
2629 }
2630 EXPORT_SYMBOL(lov_stripe_unlock);
2631
2632 struct obd_ops lov_obd_ops = {
2633         .o_owner               = THIS_MODULE,
2634         .o_setup               = lov_setup,
2635         .o_precleanup          = lov_precleanup,
2636         .o_cleanup             = lov_cleanup,
2637         .o_process_config      = lov_process_config,
2638         .o_connect             = lov_connect,
2639         .o_disconnect          = lov_disconnect,
2640         .o_statfs              = lov_statfs,
2641         .o_statfs_async        = lov_statfs_async,
2642         .o_packmd              = lov_packmd,
2643         .o_unpackmd            = lov_unpackmd,
2644         .o_checkmd             = lov_checkmd,
2645         .o_create              = lov_create,
2646         .o_destroy             = lov_destroy,
2647         .o_getattr             = lov_getattr,
2648         .o_getattr_async       = lov_getattr_async,
2649         .o_setattr             = lov_setattr,
2650         .o_setattr_async       = lov_setattr_async,
2651         .o_brw                 = lov_brw,
2652         .o_brw_async           = lov_brw_async,
2653         .o_prep_async_page     = lov_prep_async_page,
2654         .o_queue_async_io      = lov_queue_async_io,
2655         .o_set_async_flags     = lov_set_async_flags,
2656         .o_queue_group_io      = lov_queue_group_io,
2657         .o_trigger_group_io    = lov_trigger_group_io,
2658         .o_teardown_async_page = lov_teardown_async_page,
2659         .o_merge_lvb           = lov_merge_lvb,
2660         .o_adjust_kms          = lov_adjust_kms,
2661         .o_punch               = lov_punch,
2662         .o_sync                = lov_sync,
2663         .o_enqueue             = lov_enqueue,
2664         .o_match               = lov_match,
2665         .o_change_cbdata       = lov_change_cbdata,
2666         .o_cancel              = lov_cancel,
2667         .o_cancel_unused       = lov_cancel_unused,
2668         .o_join_lru            = lov_join_lru,
2669         .o_iocontrol           = lov_iocontrol,
2670         .o_get_info            = lov_get_info,
2671         .o_set_info_async      = lov_set_info_async,
2672         .o_extent_calc         = lov_extent_calc,
2673         .o_llog_init           = lov_llog_init,
2674         .o_llog_finish         = lov_llog_finish,
2675         .o_notify              = lov_notify,
2676 };
2677
2678 static quota_interface_t *quota_interface;
2679 extern quota_interface_t lov_quota_interface;
2680
2681 cfs_mem_cache_t *lov_oinfo_slab;
2682
2683 int __init lov_init(void)
2684 {
2685         struct lprocfs_static_vars lvars;
2686         int rc, rc2;
2687         ENTRY;
2688
2689         lov_oinfo_slab = cfs_mem_cache_create("lov_oinfo",
2690                                               sizeof(struct lov_oinfo), 
2691                                               0, SLAB_HWCACHE_ALIGN);
2692         if (lov_oinfo_slab == NULL)
2693                 return -ENOMEM;
2694         lprocfs_init_vars(lov, &lvars);
2695
2696         request_module("lquota");
2697         quota_interface = PORTAL_SYMBOL_GET(lov_quota_interface);
2698         init_obd_quota_ops(quota_interface, &lov_obd_ops);
2699
2700         rc = class_register_type(&lov_obd_ops, NULL, lvars.module_vars,
2701                                  LUSTRE_LOV_NAME, NULL);
2702         if (rc) {
2703                 if (quota_interface)
2704                         PORTAL_SYMBOL_PUT(lov_quota_interface);
2705                 rc2 = cfs_mem_cache_destroy(lov_oinfo_slab);
2706                 LASSERT(rc2 == 0);
2707         }
2708
2709         RETURN(rc);
2710 }
2711
2712 #ifdef __KERNEL__
2713 static void /*__exit*/ lov_exit(void)
2714 {
2715         int rc;
2716         
2717         if (quota_interface)
2718                 PORTAL_SYMBOL_PUT(lov_quota_interface);
2719
2720         class_unregister_type(LUSTRE_LOV_NAME);
2721         rc = cfs_mem_cache_destroy(lov_oinfo_slab);
2722         LASSERT(rc == 0);
2723 }
2724
2725 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
2726 MODULE_DESCRIPTION("Lustre Logical Object Volume OBD driver");
2727 MODULE_LICENSE("GPL");
2728
2729 cfs_module(lov, LUSTRE_VERSION_STRING, lov_init, lov_exit);
2730 #endif