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