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