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