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