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