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