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