Whamcloud - gitweb
e876da09a1ea9e4a6b027a895b162d14336fbdaf
[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                 GOTO(out, rc);
935         }
936         case LCFG_POOL_NEW:
937         case LCFG_POOL_ADD:
938         case LCFG_POOL_DEL:
939         case LCFG_POOL_REM:
940                 GOTO(out, rc);
941
942         default: {
943                 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
944                 GOTO(out, rc = -EINVAL);
945
946         }
947         }
948 out:
949         RETURN(rc);
950 }
951
952 #ifndef log2
953 #define log2(n) ffz(~(n))
954 #endif
955
956 static int lov_clear_orphans(struct obd_export *export, struct obdo *src_oa,
957                              struct lov_stripe_md **ea,
958                              struct obd_trans_info *oti)
959 {
960         struct lov_obd *lov;
961         struct obdo *tmp_oa;
962         struct obd_uuid *ost_uuid = NULL;
963         int rc = 0, i;
964         ENTRY;
965
966         LASSERT(src_oa->o_valid & OBD_MD_FLFLAGS &&
967                 src_oa->o_flags == OBD_FL_DELORPHAN);
968
969         lov = &export->exp_obd->u.lov;
970
971         OBDO_ALLOC(tmp_oa);
972         if (tmp_oa == NULL)
973                 RETURN(-ENOMEM);
974
975         if (oti->oti_ost_uuid) {
976                 ost_uuid = oti->oti_ost_uuid;
977                 CDEBUG(D_HA, "clearing orphans only for %s\n",
978                        ost_uuid->uuid);
979         }
980
981         lov_getref(export->exp_obd);
982         for (i = 0; i < lov->desc.ld_tgt_count; i++) {
983                 struct lov_stripe_md obj_md;
984                 struct lov_stripe_md *obj_mdp = &obj_md;
985                 struct lov_tgt_desc *tgt;
986                 int err;
987
988                 tgt = lov->lov_tgts[i];
989                 if (!tgt)
990                         continue;
991
992                 /* if called for a specific target, we don't
993                    care if it is not active. */
994                 if (!lov->lov_tgts[i]->ltd_active && ost_uuid == NULL) {
995                         CDEBUG(D_HA, "lov idx %d inactive\n", i);
996                         continue;
997                 }
998
999                 if (ost_uuid && !obd_uuid_equals(ost_uuid, &tgt->ltd_uuid))
1000                         continue;
1001
1002                 CDEBUG(D_CONFIG,"Clear orphans for %d:%s\n", i,
1003                        obd_uuid2str(ost_uuid));
1004
1005                 memcpy(tmp_oa, src_oa, sizeof(*tmp_oa));
1006
1007                 LASSERT(lov->lov_tgts[i]->ltd_exp);
1008                 /* XXX: LOV STACKING: use real "obj_mdp" sub-data */
1009                 err = obd_create(lov->lov_tgts[i]->ltd_exp,
1010                                  tmp_oa, &obj_mdp, oti);
1011                 if (err) {
1012                         /* This export will be disabled until it is recovered,
1013                            and then orphan recovery will be completed. */
1014                         CERROR("error in orphan recovery on OST idx %d/%d: "
1015                                "rc = %d\n", i, lov->desc.ld_tgt_count, err);
1016                         rc = err;
1017                 }
1018
1019                 if (ost_uuid)
1020                         break;
1021         }
1022         lov_putref(export->exp_obd);
1023
1024         OBDO_FREE(tmp_oa);
1025         RETURN(rc);
1026 }
1027
1028 static int lov_recreate(struct obd_export *exp, struct obdo *src_oa,
1029                         struct lov_stripe_md **ea, struct obd_trans_info *oti)
1030 {
1031         struct lov_stripe_md *obj_mdp, *lsm;
1032         struct lov_obd *lov = &exp->exp_obd->u.lov;
1033         unsigned ost_idx;
1034         int rc, i;
1035         ENTRY;
1036
1037         LASSERT(src_oa->o_valid & OBD_MD_FLFLAGS &&
1038                 src_oa->o_flags & OBD_FL_RECREATE_OBJS);
1039
1040         OBD_ALLOC(obj_mdp, sizeof(*obj_mdp));
1041         if (obj_mdp == NULL)
1042                 RETURN(-ENOMEM);
1043
1044         ost_idx = src_oa->o_nlink;
1045         lsm = *ea;
1046         if (lsm == NULL)
1047                 GOTO(out, rc = -EINVAL);
1048         if (ost_idx >= lov->desc.ld_tgt_count ||
1049             !lov->lov_tgts[ost_idx])
1050                 GOTO(out, rc = -EINVAL);
1051
1052         for (i = 0; i < lsm->lsm_stripe_count; i++) {
1053                 if (lsm->lsm_oinfo[i]->loi_ost_idx == ost_idx) {
1054                         if (lsm->lsm_oinfo[i]->loi_id != src_oa->o_id)
1055                                 GOTO(out, rc = -EINVAL);
1056                         break;
1057                 }
1058         }
1059         if (i == lsm->lsm_stripe_count)
1060                 GOTO(out, rc = -EINVAL);
1061
1062         rc = obd_create(lov->lov_tgts[ost_idx]->ltd_exp, src_oa, &obj_mdp, oti);
1063 out:
1064         OBD_FREE(obj_mdp, sizeof(*obj_mdp));
1065         RETURN(rc);
1066 }
1067
1068 /* the LOV expects oa->o_id to be set to the LOV object id */
1069 static int lov_create(struct obd_export *exp, struct obdo *src_oa,
1070                       struct lov_stripe_md **ea, struct obd_trans_info *oti)
1071 {
1072         struct lov_obd *lov;
1073         struct obd_info oinfo;
1074         struct lov_request_set *set = NULL;
1075         struct lov_request *req;
1076         struct obd_statfs osfs;
1077         __u64 maxage;
1078         int rc = 0;
1079         ENTRY;
1080
1081         LASSERT(ea != NULL);
1082         if (exp == NULL)
1083                 RETURN(-EINVAL);
1084
1085         if ((src_oa->o_valid & OBD_MD_FLFLAGS) &&
1086             src_oa->o_flags == OBD_FL_DELORPHAN) {
1087                 rc = lov_clear_orphans(exp, src_oa, ea, oti);
1088                 RETURN(rc);
1089         }
1090
1091         lov = &exp->exp_obd->u.lov;
1092         if (!lov->desc.ld_active_tgt_count)
1093                 RETURN(-EIO);
1094
1095         lov_getref(exp->exp_obd);
1096         /* Recreate a specific object id at the given OST index */
1097         if ((src_oa->o_valid & OBD_MD_FLFLAGS) &&
1098             (src_oa->o_flags & OBD_FL_RECREATE_OBJS)) {
1099                  rc = lov_recreate(exp, src_oa, ea, oti);
1100                  GOTO(out, rc);
1101         }
1102
1103         maxage = cfs_time_shift_64(-lov->desc.ld_qos_maxage);
1104         obd_statfs_rqset(exp->exp_obd, &osfs, maxage, OBD_STATFS_NODELAY);
1105
1106         rc = lov_prep_create_set(exp, &oinfo, ea, src_oa, oti, &set);
1107         if (rc)
1108                 GOTO(out, rc);
1109
1110         list_for_each_entry(req, &set->set_list, rq_link) {
1111                 /* XXX: LOV STACKING: use real "obj_mdp" sub-data */
1112                 rc = obd_create(lov->lov_tgts[req->rq_idx]->ltd_exp,
1113                                 req->rq_oi.oi_oa, &req->rq_oi.oi_md, oti);
1114                 lov_update_create_set(set, req, rc);
1115         }
1116         rc = lov_fini_create_set(set, ea);
1117 out:
1118         lov_putref(exp->exp_obd);
1119         RETURN(rc);
1120 }
1121
1122 #define ASSERT_LSM_MAGIC(lsmp)                                                  \
1123 do {                                                                            \
1124         LASSERT((lsmp) != NULL);                                                \
1125         LASSERTF(((lsmp)->lsm_magic == LOV_MAGIC_V1 ||                          \
1126                  (lsmp)->lsm_magic == LOV_MAGIC_V3 ||                           \
1127                  (lsmp)->lsm_magic == LOV_MAGIC_JOIN), "%p->lsm_magic=%x\n",    \
1128                  (lsmp), (lsmp)->lsm_magic);                                    \
1129 } while (0)
1130
1131 static int lov_destroy(struct obd_export *exp, struct obdo *oa,
1132                        struct lov_stripe_md *lsm, struct obd_trans_info *oti,
1133                        struct obd_export *md_exp)
1134 {
1135         struct lov_request_set *set;
1136         struct obd_info oinfo;
1137         struct lov_request *req;
1138         struct list_head *pos;
1139         struct lov_obd *lov;
1140         int rc = 0, err = 0;
1141         ENTRY;
1142
1143         ASSERT_LSM_MAGIC(lsm);
1144
1145         if (!exp || !exp->exp_obd)
1146                 RETURN(-ENODEV);
1147
1148         if (oa->o_valid & OBD_MD_FLCOOKIE) {
1149                 LASSERT(oti);
1150                 LASSERT(oti->oti_logcookies);
1151         }
1152
1153         lov = &exp->exp_obd->u.lov;
1154         lov_getref(exp->exp_obd);
1155         rc = lov_prep_destroy_set(exp, &oinfo, oa, lsm, oti, &set);
1156         if (rc)
1157                 GOTO(out, rc);
1158
1159         list_for_each (pos, &set->set_list) {
1160                 req = list_entry(pos, struct lov_request, rq_link);
1161
1162                 if (oa->o_valid & OBD_MD_FLCOOKIE)
1163                         oti->oti_logcookies = set->set_cookies + req->rq_stripe;
1164
1165                 err = obd_destroy(lov->lov_tgts[req->rq_idx]->ltd_exp,
1166                                   req->rq_oi.oi_oa, NULL, oti, NULL);
1167                 err = lov_update_common_set(set, req, err);
1168                 if (err) {
1169                         CERROR("error: destroying objid "LPX64" subobj "
1170                                LPX64" on OST idx %d: rc = %d\n",
1171                                oa->o_id, req->rq_oi.oi_oa->o_id,
1172                                req->rq_idx, err);
1173                         if (!rc)
1174                                 rc = err;
1175                 }
1176         }
1177
1178         if (rc == 0) {
1179                 LASSERT(lsm_op_find(lsm->lsm_magic) != NULL);
1180                 rc = lsm_op_find(lsm->lsm_magic)->lsm_destroy(lsm, oa, md_exp);
1181         }
1182         err = lov_fini_destroy_set(set);
1183 out:
1184         lov_putref(exp->exp_obd);
1185         RETURN(rc ? rc : err);
1186 }
1187
1188 static int lov_getattr(struct obd_export *exp, struct obd_info *oinfo)
1189 {
1190         struct lov_request_set *set;
1191         struct lov_request *req;
1192         struct list_head *pos;
1193         struct lov_obd *lov;
1194         int err = 0, rc = 0;
1195         ENTRY;
1196
1197         LASSERT(oinfo);
1198         ASSERT_LSM_MAGIC(oinfo->oi_md);
1199
1200         if (!exp || !exp->exp_obd)
1201                 RETURN(-ENODEV);
1202
1203         lov = &exp->exp_obd->u.lov;
1204
1205         rc = lov_prep_getattr_set(exp, oinfo, &set);
1206         if (rc)
1207                 RETURN(rc);
1208
1209         list_for_each (pos, &set->set_list) {
1210                 req = list_entry(pos, struct lov_request, rq_link);
1211
1212                 CDEBUG(D_INFO, "objid "LPX64"[%d] has subobj "LPX64" at idx "
1213                        "%u\n", oinfo->oi_oa->o_id, req->rq_stripe,
1214                        req->rq_oi.oi_oa->o_id, req->rq_idx);
1215
1216                 rc = obd_getattr(lov->lov_tgts[req->rq_idx]->ltd_exp,
1217                                  &req->rq_oi);
1218                 err = lov_update_common_set(set, req, rc);
1219                 if (err) {
1220                         CERROR("error: getattr objid "LPX64" subobj "
1221                                LPX64" on OST idx %d: rc = %d\n",
1222                                oinfo->oi_oa->o_id, req->rq_oi.oi_oa->o_id,
1223                                req->rq_idx, err);
1224                         break;
1225                 }
1226         }
1227
1228         rc = lov_fini_getattr_set(set);
1229         if (err)
1230                 rc = err;
1231         RETURN(rc);
1232 }
1233
1234 static int lov_getattr_interpret(struct ptlrpc_request_set *rqset,
1235                                  void *data, int rc)
1236 {
1237         struct lov_request_set *lovset = (struct lov_request_set *)data;
1238         int err;
1239         ENTRY;
1240
1241         /* don't do attribute merge if this aysnc op failed */
1242         if (rc)
1243                 lovset->set_completes = 0;
1244         err = lov_fini_getattr_set(lovset);
1245         RETURN(rc ? rc : err);
1246 }
1247
1248 static int lov_getattr_async(struct obd_export *exp, struct obd_info *oinfo,
1249                               struct ptlrpc_request_set *rqset)
1250 {
1251         struct lov_request_set *lovset;
1252         struct lov_obd *lov;
1253         struct list_head *pos;
1254         struct lov_request *req;
1255         int rc = 0, err;
1256         ENTRY;
1257
1258         LASSERT(oinfo);
1259         ASSERT_LSM_MAGIC(oinfo->oi_md);
1260
1261         if (!exp || !exp->exp_obd)
1262                 RETURN(-ENODEV);
1263
1264         lov = &exp->exp_obd->u.lov;
1265
1266         rc = lov_prep_getattr_set(exp, oinfo, &lovset);
1267         if (rc)
1268                 RETURN(rc);
1269
1270         CDEBUG(D_INFO, "objid "LPX64": %ux%u byte stripes\n",
1271                oinfo->oi_md->lsm_object_id, oinfo->oi_md->lsm_stripe_count,
1272                oinfo->oi_md->lsm_stripe_size);
1273
1274         list_for_each (pos, &lovset->set_list) {
1275                 req = list_entry(pos, struct lov_request, rq_link);
1276
1277                 CDEBUG(D_INFO, "objid "LPX64"[%d] has subobj "LPX64" at idx "
1278                        "%u\n", oinfo->oi_oa->o_id, req->rq_stripe,
1279                        req->rq_oi.oi_oa->o_id, req->rq_idx);
1280                 rc = obd_getattr_async(lov->lov_tgts[req->rq_idx]->ltd_exp,
1281                                        &req->rq_oi, rqset);
1282                 if (rc) {
1283                         CERROR("error: getattr objid "LPX64" subobj "
1284                                LPX64" on OST idx %d: rc = %d\n",
1285                                oinfo->oi_oa->o_id, req->rq_oi.oi_oa->o_id,
1286                                req->rq_idx, rc);
1287                         GOTO(out, rc);
1288                 }
1289         }
1290
1291         if (!list_empty(&rqset->set_requests)) {
1292                 LASSERT(rc == 0);
1293                 LASSERT (rqset->set_interpret == NULL);
1294                 rqset->set_interpret = lov_getattr_interpret;
1295                 rqset->set_arg = (void *)lovset;
1296                 RETURN(rc);
1297         }
1298 out:
1299         if (rc)
1300                 lovset->set_completes = 0;
1301         err = lov_fini_getattr_set(lovset);
1302         RETURN(rc ? rc : err);
1303 }
1304
1305 static int lov_setattr(struct obd_export *exp, struct obd_info *oinfo,
1306                        struct obd_trans_info *oti)
1307 {
1308         struct lov_request_set *set;
1309         struct lov_obd *lov;
1310         struct list_head *pos;
1311         struct lov_request *req;
1312         int err = 0, rc = 0;
1313         ENTRY;
1314
1315         LASSERT(oinfo);
1316         ASSERT_LSM_MAGIC(oinfo->oi_md);
1317
1318         if (!exp || !exp->exp_obd)
1319                 RETURN(-ENODEV);
1320
1321         /* for now, we only expect the following updates here */
1322         LASSERT(!(oinfo->oi_oa->o_valid & ~(OBD_MD_FLID | OBD_MD_FLTYPE |
1323                                             OBD_MD_FLMODE | OBD_MD_FLATIME |
1324                                             OBD_MD_FLMTIME | OBD_MD_FLCTIME |
1325                                             OBD_MD_FLFLAGS | OBD_MD_FLSIZE |
1326                                             OBD_MD_FLGROUP | OBD_MD_FLUID |
1327                                             OBD_MD_FLGID | OBD_MD_FLFID |
1328                                             OBD_MD_FLGENER)));
1329         lov = &exp->exp_obd->u.lov;
1330         rc = lov_prep_setattr_set(exp, oinfo, oti, &set);
1331         if (rc)
1332                 RETURN(rc);
1333
1334         list_for_each (pos, &set->set_list) {
1335                 req = list_entry(pos, struct lov_request, rq_link);
1336
1337                 rc = obd_setattr(lov->lov_tgts[req->rq_idx]->ltd_exp,
1338                                  &req->rq_oi, NULL);
1339                 err = lov_update_setattr_set(set, req, rc);
1340                 if (err) {
1341                         CERROR("error: setattr objid "LPX64" subobj "
1342                                LPX64" on OST idx %d: rc = %d\n",
1343                                set->set_oi->oi_oa->o_id,
1344                                req->rq_oi.oi_oa->o_id, req->rq_idx, err);
1345                         if (!rc)
1346                                 rc = err;
1347                 }
1348         }
1349         err = lov_fini_setattr_set(set);
1350         if (!rc)
1351                 rc = err;
1352         RETURN(rc);
1353 }
1354
1355 static int lov_setattr_interpret(struct ptlrpc_request_set *rqset,
1356                                  void *data, int rc)
1357 {
1358         struct lov_request_set *lovset = (struct lov_request_set *)data;
1359         int err;
1360         ENTRY;
1361
1362         if (rc)
1363                 lovset->set_completes = 0;
1364         err = lov_fini_setattr_set(lovset);
1365         RETURN(rc ? rc : err);
1366 }
1367
1368 /* If @oti is given, the request goes from MDS and responses from OSTs are not
1369    needed. Otherwise, a client is waiting for responses. */
1370 static int lov_setattr_async(struct obd_export *exp, struct obd_info *oinfo,
1371                              struct obd_trans_info *oti,
1372                              struct ptlrpc_request_set *rqset)
1373 {
1374         struct lov_request_set *set;
1375         struct lov_request *req;
1376         struct list_head *pos;
1377         struct lov_obd *lov;
1378         int rc = 0;
1379         ENTRY;
1380
1381         LASSERT(oinfo);
1382         ASSERT_LSM_MAGIC(oinfo->oi_md);
1383         if (oinfo->oi_oa->o_valid & OBD_MD_FLCOOKIE) {
1384                 LASSERT(oti);
1385                 LASSERT(oti->oti_logcookies);
1386         }
1387
1388         if (!exp || !exp->exp_obd)
1389                 RETURN(-ENODEV);
1390
1391         lov = &exp->exp_obd->u.lov;
1392         rc = lov_prep_setattr_set(exp, oinfo, oti, &set);
1393         if (rc)
1394                 RETURN(rc);
1395
1396         CDEBUG(D_INFO, "objid "LPX64": %ux%u byte stripes\n",
1397                oinfo->oi_md->lsm_object_id, oinfo->oi_md->lsm_stripe_count,
1398                oinfo->oi_md->lsm_stripe_size);
1399
1400         list_for_each (pos, &set->set_list) {
1401                 req = list_entry(pos, struct lov_request, rq_link);
1402
1403                 if (oinfo->oi_oa->o_valid & OBD_MD_FLCOOKIE)
1404                         oti->oti_logcookies = set->set_cookies + req->rq_stripe;
1405
1406                 CDEBUG(D_INFO, "objid "LPX64"[%d] has subobj "LPX64" at idx "
1407                        "%u\n", oinfo->oi_oa->o_id, req->rq_stripe,
1408                        req->rq_oi.oi_oa->o_id, req->rq_idx);
1409
1410                 rc = obd_setattr_async(lov->lov_tgts[req->rq_idx]->ltd_exp,
1411                                        &req->rq_oi, oti, rqset);
1412                 if (rc) {
1413                         CERROR("error: setattr objid "LPX64" subobj "
1414                                LPX64" on OST idx %d: rc = %d\n",
1415                                set->set_oi->oi_oa->o_id,
1416                                req->rq_oi.oi_oa->o_id,
1417                                req->rq_idx, rc);
1418                         break;
1419                 }
1420         }
1421
1422         /* If we are not waiting for responses on async requests, return. */
1423         if (rc || !rqset || list_empty(&rqset->set_requests)) {
1424                 int err;
1425                 if (rc)
1426                         set->set_completes = 0;
1427                 err = lov_fini_setattr_set(set);
1428                 RETURN(rc ? rc : err);
1429         }
1430
1431         LASSERT(rqset->set_interpret == NULL);
1432         rqset->set_interpret = lov_setattr_interpret;
1433         rqset->set_arg = (void *)set;
1434
1435         RETURN(0);
1436 }
1437
1438 static int lov_punch_interpret(struct ptlrpc_request_set *rqset,
1439                                void *data, int rc)
1440 {
1441         struct lov_request_set *lovset = (struct lov_request_set *)data;
1442         int err;
1443         ENTRY;
1444
1445         if (rc)
1446                 lovset->set_completes = 0;
1447         err = lov_fini_punch_set(lovset);
1448         RETURN(rc ? rc : err);
1449 }
1450
1451 /* FIXME: maybe we'll just make one node the authoritative attribute node, then
1452  * we can send this 'punch' to just the authoritative node and the nodes
1453  * that the punch will affect. */
1454 static int lov_punch(struct obd_export *exp, struct obd_info *oinfo,
1455                      struct obd_trans_info *oti,
1456                      struct ptlrpc_request_set *rqset)
1457 {
1458         struct lov_request_set *set;
1459         struct lov_obd *lov;
1460         struct list_head *pos;
1461         struct lov_request *req;
1462         int rc = 0;
1463         ENTRY;
1464
1465         LASSERT(oinfo);
1466         ASSERT_LSM_MAGIC(oinfo->oi_md);
1467
1468         if (!exp || !exp->exp_obd)
1469                 RETURN(-ENODEV);
1470
1471         lov = &exp->exp_obd->u.lov;
1472         rc = lov_prep_punch_set(exp, oinfo, oti, &set);
1473         if (rc)
1474                 RETURN(rc);
1475
1476         list_for_each (pos, &set->set_list) {
1477                 req = list_entry(pos, struct lov_request, rq_link);
1478
1479                 rc = obd_punch(lov->lov_tgts[req->rq_idx]->ltd_exp,
1480                                &req->rq_oi, NULL, rqset);
1481                 if (rc) {
1482                         CERROR("error: punch objid "LPX64" subobj "LPX64
1483                                " on OST idx %d: rc = %d\n",
1484                                set->set_oi->oi_oa->o_id,
1485                                req->rq_oi.oi_oa->o_id, req->rq_idx, rc);
1486                         break;
1487                 }
1488         }
1489
1490         if (rc || list_empty(&rqset->set_requests)) {
1491                 int err;
1492                 err = lov_fini_punch_set(set);
1493                 RETURN(rc ? rc : err);
1494         }
1495
1496         LASSERT(rqset->set_interpret == NULL);
1497         rqset->set_interpret = lov_punch_interpret;
1498         rqset->set_arg = (void *)set;
1499
1500         RETURN(0);
1501 }
1502
1503 static int lov_sync(struct obd_export *exp, struct obdo *oa,
1504                     struct lov_stripe_md *lsm, obd_off start, obd_off end,
1505                     void *capa)
1506 {
1507         struct lov_request_set *set;
1508         struct obd_info oinfo;
1509         struct lov_obd *lov;
1510         struct list_head *pos;
1511         struct lov_request *req;
1512         int err = 0, rc = 0;
1513         ENTRY;
1514
1515         ASSERT_LSM_MAGIC(lsm);
1516
1517         if (!exp->exp_obd)
1518                 RETURN(-ENODEV);
1519
1520         lov = &exp->exp_obd->u.lov;
1521         rc = lov_prep_sync_set(exp, &oinfo, oa, lsm, start, end, &set);
1522         if (rc)
1523                 RETURN(rc);
1524
1525         list_for_each (pos, &set->set_list) {
1526                 req = list_entry(pos, struct lov_request, rq_link);
1527
1528                 rc = obd_sync(lov->lov_tgts[req->rq_idx]->ltd_exp,
1529                               req->rq_oi.oi_oa, NULL,
1530                               req->rq_oi.oi_policy.l_extent.start,
1531                               req->rq_oi.oi_policy.l_extent.end, capa);
1532                 err = lov_update_common_set(set, req, rc);
1533                 if (err) {
1534                         CERROR("error: fsync objid "LPX64" subobj "LPX64
1535                                " on OST idx %d: rc = %d\n",
1536                                set->set_oi->oi_oa->o_id,
1537                                req->rq_oi.oi_oa->o_id, req->rq_idx, rc);
1538                         if (!rc)
1539                                 rc = err;
1540                 }
1541         }
1542         err = lov_fini_sync_set(set);
1543         if (!rc)
1544                 rc = err;
1545         RETURN(rc);
1546 }
1547
1548 static int lov_brw_check(struct lov_obd *lov, struct obd_info *lov_oinfo,
1549                          obd_count oa_bufs, struct brw_page *pga)
1550 {
1551         struct obd_info oinfo = { { { 0 } } };
1552         int i, rc = 0;
1553
1554         oinfo.oi_oa = lov_oinfo->oi_oa;
1555
1556         /* The caller just wants to know if there's a chance that this
1557          * I/O can succeed */
1558         for (i = 0; i < oa_bufs; i++) {
1559                 int stripe = lov_stripe_number(lov_oinfo->oi_md, pga[i].off);
1560                 int ost = lov_oinfo->oi_md->lsm_oinfo[stripe]->loi_ost_idx;
1561                 obd_off start, end;
1562
1563                 if (!lov_stripe_intersects(lov_oinfo->oi_md, i, pga[i].off,
1564                                            pga[i].off + pga[i].count - 1,
1565                                            &start, &end))
1566                         continue;
1567
1568                 if (!lov->lov_tgts[ost] || !lov->lov_tgts[ost]->ltd_active) {
1569                         CDEBUG(D_HA, "lov idx %d inactive\n", ost);
1570                         return -EIO;
1571                 }
1572
1573                 rc = obd_brw(OBD_BRW_CHECK, lov->lov_tgts[ost]->ltd_exp, &oinfo,
1574                              1, &pga[i], NULL);
1575                 if (rc)
1576                         break;
1577         }
1578         return rc;
1579 }
1580
1581 static int lov_brw(int cmd, struct obd_export *exp, struct obd_info *oinfo,
1582                    obd_count oa_bufs, struct brw_page *pga,
1583                    struct obd_trans_info *oti)
1584 {
1585         struct lov_request_set *set;
1586         struct lov_request *req;
1587         struct list_head *pos;
1588         struct lov_obd *lov = &exp->exp_obd->u.lov;
1589         int err, rc = 0;
1590         ENTRY;
1591
1592         ASSERT_LSM_MAGIC(oinfo->oi_md);
1593
1594         if (cmd == OBD_BRW_CHECK) {
1595                 rc = lov_brw_check(lov, oinfo, oa_bufs, pga);
1596                 RETURN(rc);
1597         }
1598
1599         rc = lov_prep_brw_set(exp, oinfo, oa_bufs, pga, oti, &set);
1600         if (rc)
1601                 RETURN(rc);
1602
1603         list_for_each (pos, &set->set_list) {
1604                 struct obd_export *sub_exp;
1605                 struct brw_page *sub_pga;
1606                 req = list_entry(pos, struct lov_request, rq_link);
1607
1608                 sub_exp = lov->lov_tgts[req->rq_idx]->ltd_exp;
1609                 sub_pga = set->set_pga + req->rq_pgaidx;
1610                 rc = obd_brw(cmd, sub_exp, &req->rq_oi, req->rq_oabufs,
1611                              sub_pga, oti);
1612                 if (rc)
1613                         break;
1614                 lov_update_common_set(set, req, rc);
1615         }
1616
1617         err = lov_fini_brw_set(set);
1618         if (!rc)
1619                 rc = err;
1620         RETURN(rc);
1621 }
1622
1623 static int lov_enqueue_interpret(struct ptlrpc_request_set *rqset,
1624                                  void *data, int rc)
1625 {
1626         struct lov_request_set *lovset = (struct lov_request_set *)data;
1627         ENTRY;
1628         rc = lov_fini_enqueue_set(lovset, lovset->set_ei->ei_mode, rc, rqset);
1629         RETURN(rc);
1630 }
1631
1632 static int lov_enqueue(struct obd_export *exp, struct obd_info *oinfo,
1633                        struct ldlm_enqueue_info *einfo,
1634                        struct ptlrpc_request_set *rqset)
1635 {
1636         ldlm_mode_t mode = einfo->ei_mode;
1637         struct lov_request_set *set;
1638         struct lov_request *req;
1639         struct list_head *pos;
1640         struct lov_obd *lov;
1641         ldlm_error_t rc;
1642         ENTRY;
1643
1644         LASSERT(oinfo);
1645         ASSERT_LSM_MAGIC(oinfo->oi_md);
1646         LASSERT(mode == (mode & -mode));
1647
1648         /* we should never be asked to replay a lock this way. */
1649         LASSERT((oinfo->oi_flags & LDLM_FL_REPLAY) == 0);
1650
1651         if (!exp || !exp->exp_obd)
1652                 RETURN(-ENODEV);
1653
1654         lov = &exp->exp_obd->u.lov;
1655         rc = lov_prep_enqueue_set(exp, oinfo, einfo, &set);
1656         if (rc)
1657                 RETURN(rc);
1658
1659         list_for_each (pos, &set->set_list) {
1660                 req = list_entry(pos, struct lov_request, rq_link);
1661
1662                 rc = obd_enqueue(lov->lov_tgts[req->rq_idx]->ltd_exp,
1663                                  &req->rq_oi, einfo, rqset);
1664                 if (rc != ELDLM_OK)
1665                         GOTO(out, rc);
1666         }
1667
1668         if (rqset && !list_empty(&rqset->set_requests)) {
1669                 LASSERT(rc == 0);
1670                 LASSERT(rqset->set_interpret == NULL);
1671                 rqset->set_interpret = lov_enqueue_interpret;
1672                 rqset->set_arg = (void *)set;
1673                 RETURN(rc);
1674         }
1675 out:
1676         rc = lov_fini_enqueue_set(set, mode, rc, rqset);
1677         RETURN(rc);
1678 }
1679
1680 static int lov_change_cbdata(struct obd_export *exp,
1681                              struct lov_stripe_md *lsm, ldlm_iterator_t it,
1682                              void *data)
1683 {
1684         struct lov_obd *lov;
1685         int rc = 0, i;
1686         ENTRY;
1687
1688         ASSERT_LSM_MAGIC(lsm);
1689
1690         if (!exp || !exp->exp_obd)
1691                 RETURN(-ENODEV);
1692
1693         LASSERT(lsm->lsm_object_gr > 0);
1694
1695         lov = &exp->exp_obd->u.lov;
1696         for (i = 0; i < lsm->lsm_stripe_count; i++) {
1697                 struct lov_stripe_md submd;
1698                 struct lov_oinfo *loi = lsm->lsm_oinfo[i];
1699
1700                 if (!lov->lov_tgts[loi->loi_ost_idx]) {
1701                         CDEBUG(D_HA, "lov idx %d NULL \n", loi->loi_ost_idx);
1702                         continue;
1703                 }
1704
1705                 submd.lsm_object_id = loi->loi_id;
1706                 submd.lsm_object_gr = lsm->lsm_object_gr;
1707                 submd.lsm_stripe_count = 0;
1708                 rc = obd_change_cbdata(lov->lov_tgts[loi->loi_ost_idx]->ltd_exp,
1709                                        &submd, it, data);
1710         }
1711         RETURN(rc);
1712 }
1713
1714 static int lov_cancel(struct obd_export *exp, struct lov_stripe_md *lsm,
1715                       __u32 mode, struct lustre_handle *lockh)
1716 {
1717         struct lov_request_set *set;
1718         struct obd_info oinfo;
1719         struct lov_request *req;
1720         struct list_head *pos;
1721         struct lov_obd *lov = &exp->exp_obd->u.lov;
1722         struct lustre_handle *lov_lockhp;
1723         int err = 0, rc = 0;
1724         ENTRY;
1725
1726         ASSERT_LSM_MAGIC(lsm);
1727
1728         if (!exp || !exp->exp_obd)
1729                 RETURN(-ENODEV);
1730
1731         LASSERT(lsm->lsm_object_gr > 0);
1732         LASSERT(lockh);
1733         lov = &exp->exp_obd->u.lov;
1734         rc = lov_prep_cancel_set(exp, &oinfo, lsm, mode, lockh, &set);
1735         if (rc)
1736                 RETURN(rc);
1737
1738         list_for_each (pos, &set->set_list) {
1739                 req = list_entry(pos, struct lov_request, rq_link);
1740                 lov_lockhp = set->set_lockh->llh_handles + req->rq_stripe;
1741
1742                 rc = obd_cancel(lov->lov_tgts[req->rq_idx]->ltd_exp,
1743                                 req->rq_oi.oi_md, mode, lov_lockhp);
1744                 rc = lov_update_common_set(set, req, rc);
1745                 if (rc) {
1746                         CERROR("error: cancel objid "LPX64" subobj "
1747                                LPX64" on OST idx %d: rc = %d\n",
1748                                lsm->lsm_object_id,
1749                                req->rq_oi.oi_md->lsm_object_id,
1750                                req->rq_idx, rc);
1751                         err = rc;
1752                 }
1753
1754         }
1755         lov_fini_cancel_set(set);
1756         RETURN(err);
1757 }
1758
1759 static int lov_cancel_unused(struct obd_export *exp,
1760                              struct lov_stripe_md *lsm,
1761                              int flags, void *opaque)
1762 {
1763         struct lov_obd *lov;
1764         int rc = 0, i;
1765         ENTRY;
1766
1767         if (!exp || !exp->exp_obd)
1768                 RETURN(-ENODEV);
1769
1770         lov = &exp->exp_obd->u.lov;
1771         if (lsm == NULL) {
1772                 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
1773                         int err;
1774                         if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_exp)
1775                                 continue;
1776
1777                         err = obd_cancel_unused(lov->lov_tgts[i]->ltd_exp, NULL,
1778                                                 flags, opaque);
1779                         if (!rc)
1780                                 rc = err;
1781                 }
1782                 RETURN(rc);
1783         }
1784
1785         ASSERT_LSM_MAGIC(lsm);
1786
1787         LASSERT(lsm->lsm_object_gr > 0);
1788         for (i = 0; i < lsm->lsm_stripe_count; i++) {
1789                 struct lov_stripe_md submd;
1790                 struct lov_oinfo *loi = lsm->lsm_oinfo[i];
1791                 int err;
1792
1793                 if (!lov->lov_tgts[loi->loi_ost_idx]) {
1794                         CDEBUG(D_HA, "lov idx %d NULL\n", loi->loi_ost_idx);
1795                         continue;
1796                 }
1797
1798                 if (!lov->lov_tgts[loi->loi_ost_idx]->ltd_active)
1799                         CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
1800
1801                 submd.lsm_object_id = loi->loi_id;
1802                 submd.lsm_object_gr = lsm->lsm_object_gr;
1803                 submd.lsm_stripe_count = 0;
1804                 err = obd_cancel_unused(lov->lov_tgts[loi->loi_ost_idx]->ltd_exp,
1805                                         &submd, flags, opaque);
1806                 if (err && lov->lov_tgts[loi->loi_ost_idx]->ltd_active) {
1807                         CERROR("error: cancel unused objid "LPX64" subobj "LPX64
1808                                " on OST idx %d: rc = %d\n", lsm->lsm_object_id,
1809                                loi->loi_id, loi->loi_ost_idx, err);
1810                         if (!rc)
1811                                 rc = err;
1812                 }
1813         }
1814         RETURN(rc);
1815 }
1816
1817 static int lov_statfs_interpret(struct ptlrpc_request_set *rqset,
1818                                 void *data, int rc)
1819 {
1820         struct lov_request_set *lovset = (struct lov_request_set *)data;
1821         int err;
1822         ENTRY;
1823
1824         if (rc)
1825                 lovset->set_completes = 0;
1826
1827         err = lov_fini_statfs_set(lovset);
1828         RETURN(rc ? rc : err);
1829 }
1830
1831 static int lov_statfs_async(struct obd_device *obd, struct obd_info *oinfo,
1832                             __u64 max_age, struct ptlrpc_request_set *rqset)
1833 {
1834         struct lov_request_set *set;
1835         struct lov_request *req;
1836         struct list_head *pos;
1837         struct lov_obd *lov;
1838         int rc = 0;
1839         ENTRY;
1840
1841         LASSERT(oinfo != NULL);
1842         LASSERT(oinfo->oi_osfs != NULL);
1843
1844         lov = &obd->u.lov;
1845         rc = lov_prep_statfs_set(obd, oinfo, &set);
1846         if (rc)
1847                 RETURN(rc);
1848
1849         list_for_each (pos, &set->set_list) {
1850                 struct obd_device *osc_obd;
1851
1852                 req = list_entry(pos, struct lov_request, rq_link);
1853
1854                 osc_obd = class_exp2obd(lov->lov_tgts[req->rq_idx]->ltd_exp);
1855                 rc = obd_statfs_async(osc_obd, &req->rq_oi, max_age, rqset);
1856                 if (rc)
1857                         break;
1858         }
1859
1860         if (rc || list_empty(&rqset->set_requests)) {
1861                 int err;
1862                 if (rc)
1863                         set->set_completes = 0;
1864                 err = lov_fini_statfs_set(set);
1865                 RETURN(rc ? rc : err);
1866         }
1867
1868         LASSERT(rqset->set_interpret == NULL);
1869         rqset->set_interpret = lov_statfs_interpret;
1870         rqset->set_arg = (void *)set;
1871         RETURN(0);
1872 }
1873
1874 static int lov_statfs(struct obd_device *obd, struct obd_statfs *osfs,
1875                       __u64 max_age, __u32 flags)
1876 {
1877         struct ptlrpc_request_set *set = NULL;
1878         struct obd_info oinfo = { { { 0 } } };
1879         int rc = 0;
1880         ENTRY;
1881
1882
1883         /* for obdclass we forbid using obd_statfs_rqset, but prefer using async
1884          * statfs requests */
1885         set = ptlrpc_prep_set();
1886         if (set == NULL)
1887                 RETURN(-ENOMEM);
1888
1889         oinfo.oi_osfs = osfs;
1890         oinfo.oi_flags = flags;
1891         rc = lov_statfs_async(obd, &oinfo, max_age, set);
1892         if (rc == 0)
1893                 rc = ptlrpc_set_wait(set);
1894         ptlrpc_set_destroy(set);
1895
1896         RETURN(rc);
1897 }
1898
1899 static int lov_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1900                          void *karg, void *uarg)
1901 {
1902         struct obd_device *obddev = class_exp2obd(exp);
1903         struct lov_obd *lov = &obddev->u.lov;
1904         int i, rc = 0, count = lov->desc.ld_tgt_count;
1905         struct obd_uuid *uuidp;
1906         ENTRY;
1907
1908         switch (cmd) {
1909         case IOC_OBD_STATFS: {
1910                 struct obd_ioctl_data *data = karg;
1911                 struct obd_device *osc_obd;
1912                 struct obd_statfs stat_buf = {0};
1913                 __u32 index;
1914
1915                 memcpy(&index, data->ioc_inlbuf2, sizeof(__u32));
1916                 LASSERT(data->ioc_plen1 == sizeof(struct obd_statfs));
1917
1918                 if ((index >= count))
1919                         RETURN(-ENODEV);
1920
1921                 if (!lov->lov_tgts[index])
1922                         /* Try again with the next index */
1923                         RETURN(-EAGAIN);
1924                 if (!lov->lov_tgts[index]->ltd_active)
1925                         RETURN(-ENODATA);
1926
1927                 osc_obd = class_exp2obd(lov->lov_tgts[index]->ltd_exp);
1928                 if (!osc_obd)
1929                         RETURN(-EINVAL);
1930
1931                 /* got statfs data */
1932                 rc = obd_statfs(osc_obd, &stat_buf,
1933                                 cfs_time_current_64() - HZ, 0);
1934                 if (rc)
1935                         RETURN(rc);
1936                 if (copy_to_user(data->ioc_pbuf1, &stat_buf, data->ioc_plen1))
1937                         RETURN(-EFAULT);
1938                 /* copy UUID */
1939                 if (copy_to_user(data->ioc_pbuf2, obd2cli_tgt(osc_obd),
1940                                  data->ioc_plen2))
1941                         RETURN(-EFAULT);
1942                 break;
1943         }
1944         case OBD_IOC_LOV_GET_CONFIG: {
1945                 struct obd_ioctl_data *data;
1946                 struct lov_desc *desc;
1947                 char *buf = NULL;
1948                 __u32 *genp;
1949
1950                 len = 0;
1951                 if (obd_ioctl_getdata(&buf, &len, (void *)uarg))
1952                         RETURN(-EINVAL);
1953
1954                 data = (struct obd_ioctl_data *)buf;
1955
1956                 if (sizeof(*desc) > data->ioc_inllen1) {
1957                         obd_ioctl_freedata(buf, len);
1958                         RETURN(-EINVAL);
1959                 }
1960
1961                 if (sizeof(uuidp->uuid) * count > data->ioc_inllen2) {
1962                         obd_ioctl_freedata(buf, len);
1963                         RETURN(-EINVAL);
1964                 }
1965
1966                 if (sizeof(__u32) * count > data->ioc_inllen3) {
1967                         obd_ioctl_freedata(buf, len);
1968                         RETURN(-EINVAL);
1969                 }
1970
1971                 desc = (struct lov_desc *)data->ioc_inlbuf1;
1972                 memcpy(desc, &(lov->desc), sizeof(*desc));
1973
1974                 uuidp = (struct obd_uuid *)data->ioc_inlbuf2;
1975                 genp = (__u32 *)data->ioc_inlbuf3;
1976                 /* the uuid will be empty for deleted OSTs */
1977                 for (i = 0; i < count; i++, uuidp++, genp++) {
1978                         if (!lov->lov_tgts[i])
1979                                 continue;
1980                         *uuidp = lov->lov_tgts[i]->ltd_uuid;
1981                         *genp = lov->lov_tgts[i]->ltd_gen;
1982                 }
1983
1984                 if (copy_to_user((void *)uarg, buf, len))
1985                         rc = -EFAULT;
1986                 obd_ioctl_freedata(buf, len);
1987                 break;
1988         }
1989         case LL_IOC_LOV_SETSTRIPE:
1990                 rc = lov_setstripe(exp, karg, uarg);
1991                 break;
1992         case LL_IOC_LOV_GETSTRIPE:
1993                 rc = lov_getstripe(exp, karg, uarg);
1994                 break;
1995         case LL_IOC_LOV_SETEA:
1996                 rc = lov_setea(exp, karg, uarg);
1997                 break;
1998         default: {
1999                 int set = 0;
2000
2001                 if (count == 0)
2002                         RETURN(-ENOTTY);
2003
2004                 for (i = 0; i < count; i++) {
2005                         int err;
2006
2007                         /* OST was disconnected */
2008                         if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_exp)
2009                                 continue;
2010
2011                         err = obd_iocontrol(cmd, lov->lov_tgts[i]->ltd_exp,
2012                                             len, karg, uarg);
2013                         if (err == -ENODATA && cmd == OBD_IOC_POLL_QUOTACHECK) {
2014                                 RETURN(err);
2015                         } else if (err) {
2016                                 if (lov->lov_tgts[i]->ltd_active) {
2017                                         CDEBUG(err == -ENOTTY ?
2018                                                D_IOCTL : D_WARNING,
2019                                                "iocontrol OSC %s on OST "
2020                                                "idx %d cmd %x: err = %d\n",
2021                                                lov_uuid2str(lov, i),
2022                                                i, cmd, err);
2023                                         if (!rc)
2024                                                 rc = err;
2025                                 }
2026                         } else {
2027                                 set = 1;
2028                         }
2029                 }
2030                 if (!set && !rc)
2031                         rc = -EIO;
2032         }
2033         }
2034
2035         RETURN(rc);
2036 }
2037
2038 #define FIEMAP_BUFFER_SIZE 4096
2039
2040 /**
2041  * Non-zero fe_logical indicates that this is a continuation FIEMAP
2042  * call. The local end offset and the device are sent in the first
2043  * fm_extent. This function calculates the stripe number from the index.
2044  * This function returns a stripe_no on which mapping is to be restarted.
2045  *
2046  * This function returns fm_end_offset which is the in-OST offset at which
2047  * mapping should be restarted. If fm_end_offset=0 is returned then caller
2048  * will re-calculate proper offset in next stripe.
2049  * Note that the first extent is passed to lov_get_info via the value field.
2050  *
2051  * \param fiemap fiemap request header
2052  * \param lsm striping information for the file
2053  * \param fm_start logical start of mapping
2054  * \param fm_end logical end of mapping
2055  * \param start_stripe starting stripe will be returned in this
2056  */
2057 obd_size fiemap_calc_fm_end_offset(struct ll_user_fiemap *fiemap,
2058                                    struct lov_stripe_md *lsm, obd_size fm_start,
2059                                    obd_size fm_end, int *start_stripe)
2060 {
2061         obd_size local_end = fiemap->fm_extents[0].fe_logical;
2062         obd_off lun_start, lun_end;
2063         obd_size fm_end_offset;
2064         int stripe_no = -1, i;
2065
2066         if (fiemap->fm_extent_count == 0 ||
2067             fiemap->fm_extents[0].fe_logical == 0)
2068                 return 0;
2069
2070         /* Find out stripe_no from ost_index saved in the fe_device */
2071         for (i = 0; i < lsm->lsm_stripe_count; i++) {
2072                 if (lsm->lsm_oinfo[i]->loi_ost_idx ==
2073                                         fiemap->fm_extents[0].fe_device) {
2074                         stripe_no = i;
2075                         break;
2076                 }
2077         }
2078
2079         /* If we have finished mapping on previous device, shift logical
2080          * offset to start of next device */
2081         if ((lov_stripe_intersects(lsm, stripe_no, fm_start, fm_end,
2082                                    &lun_start, &lun_end)) != 0 &&
2083                                    local_end < lun_end) {
2084                 fm_end_offset = local_end;
2085                 *start_stripe = stripe_no;
2086         } else {
2087                 /* This is a special value to indicate that caller should
2088                  * calculate offset in next stripe. */
2089                 fm_end_offset = 0;
2090                 *start_stripe = (stripe_no + 1) % lsm->lsm_stripe_count;
2091         }
2092
2093         return fm_end_offset;
2094 }
2095
2096 /**
2097  * We calculate on which OST the mapping will end. If the length of mapping
2098  * is greater than (stripe_size * stripe_count) then the last_stripe will
2099  * will be one just before start_stripe. Else we check if the mapping
2100  * intersects each OST and find last_stripe.
2101  * This function returns the last_stripe and also sets the stripe_count
2102  * over which the mapping is spread
2103  *
2104  * \param lsm striping information for the file
2105  * \param fm_start logical start of mapping
2106  * \param fm_end logical end of mapping
2107  * \param start_stripe starting stripe of the mapping
2108  * \param stripe_count the number of stripes across which to map is returned
2109  *
2110  * \retval last_stripe return the last stripe of the mapping
2111  */
2112 int fiemap_calc_last_stripe(struct lov_stripe_md *lsm, obd_size fm_start,
2113                             obd_size fm_end, int start_stripe,
2114                             int *stripe_count)
2115 {
2116         int last_stripe;
2117         obd_off obd_start, obd_end;
2118         int i, j;
2119
2120         if (fm_end - fm_start > lsm->lsm_stripe_size * lsm->lsm_stripe_count) {
2121                 last_stripe = (start_stripe < 1 ? lsm->lsm_stripe_count - 1 :
2122                                                               start_stripe - 1);
2123                 *stripe_count = lsm->lsm_stripe_count;
2124         } else {
2125                 for (j = 0, i = start_stripe; j < lsm->lsm_stripe_count;
2126                      i = (i + 1) % lsm->lsm_stripe_count, j++) {
2127                         if ((lov_stripe_intersects(lsm, i, fm_start, fm_end,
2128                                                    &obd_start, &obd_end)) == 0)
2129                                 break;
2130                 }
2131                 *stripe_count = j;
2132                 last_stripe = (start_stripe + j - 1) %lsm->lsm_stripe_count;
2133         }
2134
2135         return last_stripe;
2136 }
2137
2138 /**
2139  * Set fe_device and copy extents from local buffer into main return buffer.
2140  *
2141  * \param fiemap fiemap request header
2142  * \param lcl_fm_ext array of local fiemap extents to be copied
2143  * \param ost_index OST index to be written into the fm_device field for each
2144                     extent
2145  * \param ext_count number of extents to be copied
2146  * \param current_extent where to start copying in main extent array
2147  */
2148 void fiemap_prepare_and_copy_exts(struct ll_user_fiemap *fiemap,
2149                                   struct ll_fiemap_extent *lcl_fm_ext,
2150                                   int ost_index, unsigned int ext_count,
2151                                   int current_extent)
2152 {
2153         char *to;
2154         int ext;
2155
2156         for (ext = 0; ext < ext_count; ext++) {
2157                 lcl_fm_ext[ext].fe_device = ost_index;
2158                 lcl_fm_ext[ext].fe_flags |= FIEMAP_EXTENT_NET;
2159         }
2160
2161         /* Copy fm_extent's from fm_local to return buffer */
2162         to = (char *)fiemap + fiemap_count_to_size(current_extent);
2163         memcpy(to, lcl_fm_ext, ext_count * sizeof(struct ll_fiemap_extent));
2164 }
2165
2166 /**
2167  * Break down the FIEMAP request and send appropriate calls to individual OSTs.
2168  * This also handles the restarting of FIEMAP calls in case mapping overflows
2169  * the available number of extents in single call.
2170  */
2171 static int lov_fiemap(struct lov_obd *lov, __u32 keylen, void *key,
2172                       __u32 *vallen, void *val, struct lov_stripe_md *lsm)
2173 {
2174         struct ll_fiemap_info_key *fm_key = key;
2175         struct ll_user_fiemap *fiemap = val;
2176         struct ll_user_fiemap *fm_local = NULL;
2177         struct ll_fiemap_extent *lcl_fm_ext;
2178         int count_local;
2179         unsigned int get_num_extents = 0;
2180         int ost_index = 0, actual_start_stripe, start_stripe;
2181         obd_size fm_start, fm_end, fm_length, fm_end_offset = 0;
2182         obd_size curr_loc;
2183         int current_extent = 0, rc = 0, i;
2184         int ost_eof = 0; /* EOF for object */
2185         int ost_done = 0; /* done with required mapping for this OST? */
2186         int last_stripe;
2187         int cur_stripe = 0, cur_stripe_wrap = 0, stripe_count;
2188         unsigned int buffer_size = FIEMAP_BUFFER_SIZE;
2189
2190         if (lsm == NULL)
2191                 GOTO(out, rc = 0);
2192
2193         if (fiemap_count_to_size(fm_key->fiemap.fm_extent_count) < buffer_size)
2194                 buffer_size = fiemap_count_to_size(fm_key->fiemap.fm_extent_count);
2195
2196         OBD_ALLOC(fm_local, buffer_size);
2197         if (fm_local == NULL)
2198                 GOTO(out, rc = -ENOMEM);
2199         lcl_fm_ext = &fm_local->fm_extents[0];
2200
2201         count_local = fiemap_size_to_count(buffer_size);
2202
2203         memcpy(fiemap, &fm_key->fiemap, sizeof(*fiemap));
2204         fm_start = fiemap->fm_start;
2205         fm_length = fiemap->fm_length;
2206         /* Calculate start stripe, last stripe and length of mapping */
2207         actual_start_stripe = start_stripe = lov_stripe_number(lsm, fm_start);
2208         fm_end = (fm_length == ~0ULL ? fm_key->oa.o_size :
2209                                                 fm_start + fm_length - 1);
2210         /* If fm_length != ~0ULL but fm_start+fm_length-1 exceeds file size */
2211         if (fm_end > fm_key->oa.o_size)
2212                 fm_end = fm_key->oa.o_size;
2213
2214         last_stripe = fiemap_calc_last_stripe(lsm, fm_start, fm_end,
2215                                             actual_start_stripe, &stripe_count);
2216
2217         fm_end_offset = fiemap_calc_fm_end_offset(fiemap, lsm, fm_start, fm_end,
2218                                                   &start_stripe);
2219
2220         if (fiemap->fm_extent_count == 0) {
2221                 get_num_extents = 1;
2222                 count_local = 0;
2223         }
2224
2225         /* Check each stripe */
2226         for (cur_stripe = start_stripe, i = 0; i < stripe_count;
2227              i++, cur_stripe = (cur_stripe + 1) % lsm->lsm_stripe_count) {
2228                 obd_size req_fm_len; /* Stores length of required mapping */
2229                 obd_size len_mapped_single_call;
2230                 obd_off lun_start, lun_end, obd_object_end;
2231                 unsigned int ext_count;
2232
2233                 cur_stripe_wrap = cur_stripe;
2234
2235                 /* Find out range of mapping on this stripe */
2236                 if ((lov_stripe_intersects(lsm, cur_stripe, fm_start, fm_end,
2237                                            &lun_start, &obd_object_end)) == 0)
2238                         continue;
2239
2240                 /* If this is a continuation FIEMAP call and we are on
2241                  * starting stripe then lun_start needs to be set to
2242                  * fm_end_offset */
2243                 if (fm_end_offset != 0 && cur_stripe == start_stripe)
2244                         lun_start = fm_end_offset;
2245
2246                 if (fm_length != ~0ULL) {
2247                         /* Handle fm_start + fm_length overflow */
2248                         if (fm_start + fm_length < fm_start)
2249                                 fm_length = ~0ULL - fm_start;
2250                         lun_end = lov_size_to_stripe(lsm, fm_start + fm_length,
2251                                                      cur_stripe);
2252                 } else {
2253                         lun_end = ~0ULL;
2254                 }
2255
2256                 if (lun_start == lun_end)
2257                         continue;
2258
2259                 req_fm_len = obd_object_end - lun_start;
2260                 fm_local->fm_length = 0;
2261                 len_mapped_single_call = 0;
2262
2263                 /* If the output buffer is very large and the objects have many
2264                  * extents we may need to loop on a single OST repeatedly */
2265                 ost_eof = 0;
2266                 ost_done = 0;
2267                 do {
2268                         if (get_num_extents == 0) {
2269                                 /* Don't get too many extents. */
2270                                 if (current_extent + count_local >
2271                                     fiemap->fm_extent_count)
2272                                         count_local = fiemap->fm_extent_count -
2273                                                                  current_extent;
2274                         }
2275
2276                         lun_start += len_mapped_single_call;
2277                         fm_local->fm_length = req_fm_len - len_mapped_single_call;
2278                         req_fm_len = fm_local->fm_length;
2279                         fm_local->fm_extent_count = count_local;
2280                         fm_local->fm_mapped_extents = 0;
2281                         fm_local->fm_flags = fiemap->fm_flags;
2282
2283                         fm_key->oa.o_id = lsm->lsm_oinfo[cur_stripe]->loi_id;
2284                         ost_index = lsm->lsm_oinfo[cur_stripe]->loi_ost_idx;
2285
2286                         if (ost_index < 0 || ost_index >=lov->desc.ld_tgt_count)
2287                                 GOTO(out, rc = -EINVAL);
2288
2289                         /* If OST is inactive, return extent with UNKNOWN flag */
2290                         if (lov && !lov->lov_tgts[ost_index]->ltd_active) {
2291                                 fm_local->fm_flags |= FIEMAP_EXTENT_LAST;
2292                                 fm_local->fm_mapped_extents = 1;
2293
2294                                 lcl_fm_ext[0].fe_logical = lun_start;
2295                                 lcl_fm_ext[0].fe_length = obd_object_end -
2296                                                                       lun_start;
2297                                 lcl_fm_ext[0].fe_flags |= FIEMAP_EXTENT_UNKNOWN;
2298
2299                                 goto inactive_tgt;
2300                         }
2301
2302                         fm_local->fm_start = lun_start;
2303                         fm_local->fm_flags &= ~FIEMAP_FLAG_DEVICE_ORDER;
2304                         memcpy(&fm_key->fiemap, fm_local, sizeof(*fm_local));
2305                         *vallen=fiemap_count_to_size(fm_local->fm_extent_count);
2306                         rc = obd_get_info(lov->lov_tgts[ost_index]->ltd_exp,
2307                                           keylen, key, vallen, fm_local, lsm);
2308                         if (rc != 0)
2309                                 GOTO(out, rc);
2310
2311 inactive_tgt:
2312                         ext_count = fm_local->fm_mapped_extents;
2313                         if (ext_count == 0) {
2314                                 ost_done = 1;
2315                                 /* If last stripe has hole at the end,
2316                                  * then we need to return */
2317                                 if (cur_stripe_wrap == last_stripe) {
2318                                         fiemap->fm_mapped_extents = 0;
2319                                         goto finish;
2320                                 }
2321                                 break;
2322                         }
2323
2324                         /* If we just need num of extents then go to next device */
2325                         if (get_num_extents) {
2326                                 current_extent += ext_count;
2327                                 break;
2328                         }
2329
2330                         len_mapped_single_call = lcl_fm_ext[ext_count-1].fe_logical -
2331                                   lun_start + lcl_fm_ext[ext_count - 1].fe_length;
2332
2333                         /* Have we finished mapping on this device? */
2334                         if (req_fm_len <= len_mapped_single_call)
2335                                 ost_done = 1;
2336
2337                         /* Clear the EXTENT_LAST flag which can be present on
2338                          * last extent */
2339                         if (lcl_fm_ext[ext_count-1].fe_flags & FIEMAP_EXTENT_LAST)
2340                                 lcl_fm_ext[ext_count - 1].fe_flags &=
2341                                                             ~FIEMAP_EXTENT_LAST;
2342
2343                         curr_loc = lov_stripe_size(lsm,
2344                                            lcl_fm_ext[ext_count - 1].fe_logical+
2345                                            lcl_fm_ext[ext_count - 1].fe_length,
2346                                            cur_stripe);
2347                         if (curr_loc >= fm_key->oa.o_size)
2348                                 ost_eof = 1;
2349
2350                         fiemap_prepare_and_copy_exts(fiemap, lcl_fm_ext,
2351                                                      ost_index, ext_count,
2352                                                      current_extent);
2353
2354                         current_extent += ext_count;
2355
2356                         /* Ran out of available extents? */
2357                         if (current_extent >= fiemap->fm_extent_count)
2358                                 goto finish;
2359                 } while (ost_done == 0 && ost_eof == 0);
2360
2361                 if (cur_stripe_wrap == last_stripe)
2362                         goto finish;
2363         }
2364
2365 finish:
2366         /* Indicate that we are returning device offsets unless file just has
2367          * single stripe */
2368         if (lsm->lsm_stripe_count > 1)
2369                 fiemap->fm_flags |= FIEMAP_FLAG_DEVICE_ORDER;
2370
2371         if (get_num_extents)
2372                 goto skip_last_device_calc;
2373
2374         /* Check if we have reached the last stripe and whether mapping for that
2375          * stripe is done. */
2376         if (cur_stripe_wrap == last_stripe) {
2377                 if (ost_done || ost_eof)
2378                         fiemap->fm_extents[current_extent - 1].fe_flags |=
2379                                                              FIEMAP_EXTENT_LAST;
2380         }
2381
2382 skip_last_device_calc:
2383         fiemap->fm_mapped_extents = current_extent;
2384
2385 out:
2386         OBD_FREE(fm_local, buffer_size);
2387         return rc;
2388 }
2389
2390 static int lov_get_info(struct obd_export *exp, __u32 keylen,
2391                         void *key, __u32 *vallen, void *val,
2392                         struct lov_stripe_md *lsm)
2393 {
2394         struct obd_device *obddev = class_exp2obd(exp);
2395         struct lov_obd *lov = &obddev->u.lov;
2396         int i, rc;
2397         ENTRY;
2398
2399         if (!vallen || !val)
2400                 RETURN(-EFAULT);
2401
2402         lov_getref(obddev);
2403
2404         if (KEY_IS(KEY_LOCK_TO_STRIPE)) {
2405                 struct {
2406                         char name[16];
2407                         struct ldlm_lock *lock;
2408                 } *data = key;
2409                 struct ldlm_res_id *res_id = &data->lock->l_resource->lr_name;
2410                 struct lov_oinfo *loi;
2411                 __u32 *stripe = val;
2412
2413                 if (*vallen < sizeof(*stripe))
2414                         GOTO(out, rc = -EFAULT);
2415                 *vallen = sizeof(*stripe);
2416
2417                 /* XXX This is another one of those bits that will need to
2418                  * change if we ever actually support nested LOVs.  It uses
2419                  * the lock's export to find out which stripe it is. */
2420                 /* XXX - it's assumed all the locks for deleted OSTs have
2421                  * been cancelled. Also, the export for deleted OSTs will
2422                  * be NULL and won't match the lock's export. */
2423                 for (i = 0; i < lsm->lsm_stripe_count; i++) {
2424                         loi = lsm->lsm_oinfo[i];
2425                         if (!lov->lov_tgts[loi->loi_ost_idx])
2426                                 continue;
2427                         if (lov->lov_tgts[loi->loi_ost_idx]->ltd_exp ==
2428                             data->lock->l_conn_export &&
2429                             osc_res_name_eq(loi->loi_id, loi->loi_gr, res_id)) {
2430                                 *stripe = i;
2431                                 GOTO(out, rc = 0);
2432                         }
2433                 }
2434                 LDLM_ERROR(data->lock, "lock on inode without such object");
2435                 dump_lsm(D_ERROR, lsm);
2436                 GOTO(out, rc = -ENXIO);
2437         } else if (KEY_IS(KEY_LAST_ID)) {
2438                 struct obd_id_info *info = val;
2439                 __u32 size = sizeof(obd_id);
2440                 struct lov_tgt_desc *tgt;
2441
2442                 LASSERT(*vallen == sizeof(struct obd_id_info));
2443                 tgt = lov->lov_tgts[info->idx];
2444
2445                 if (!tgt || !tgt->ltd_active)
2446                         GOTO(out, rc = -ESRCH);
2447
2448                 rc = obd_get_info(tgt->ltd_exp, keylen, key, &size, info->data, NULL);
2449                 GOTO(out, rc = 0);
2450         } else if (KEY_IS(KEY_LOVDESC)) {
2451                 struct lov_desc *desc_ret = val;
2452                 *desc_ret = lov->desc;
2453
2454                 GOTO(out, rc = 0);
2455         } else if (KEY_IS(KEY_FIEMAP)) {
2456                 rc = lov_fiemap(lov, keylen, key, vallen, val, lsm);
2457                 GOTO(out, rc);
2458         }
2459
2460         rc = -EINVAL;
2461
2462 out:
2463         lov_putref(obddev);
2464         RETURN(rc);
2465 }
2466
2467 static int lov_set_info_async(struct obd_export *exp, obd_count keylen,
2468                               void *key, obd_count vallen, void *val,
2469                               struct ptlrpc_request_set *set)
2470 {
2471         struct obd_device *obddev = class_exp2obd(exp);
2472         struct lov_obd *lov = &obddev->u.lov;
2473         obd_count count;
2474         int i, rc = 0, err;
2475         struct lov_tgt_desc *tgt;
2476         unsigned incr, check_uuid,
2477                  do_inactive, no_set;
2478         unsigned next_id = 0,  mds_con = 0;
2479         ENTRY;
2480
2481         incr = check_uuid = do_inactive = no_set = 0;
2482         if (set == NULL) {
2483                 no_set = 1;
2484                 set = ptlrpc_prep_set();
2485                 if (!set)
2486                         RETURN(-ENOMEM);
2487         }
2488
2489         lov_getref(obddev);
2490         count = lov->desc.ld_tgt_count;
2491
2492         if (KEY_IS(KEY_NEXT_ID)) {
2493                 count = vallen / sizeof(struct obd_id_info);
2494                 vallen = sizeof(obd_id);
2495                 incr = sizeof(struct obd_id_info);
2496                 do_inactive = 1;
2497                 next_id = 1;
2498         } else if (KEY_IS(KEY_CHECKSUM)) {
2499                 do_inactive = 1;
2500         } else if (KEY_IS(KEY_UNLINKED)) {
2501                 check_uuid = val ? 1 : 0;
2502         } else if (KEY_IS(KEY_EVICT_BY_NID)) {
2503                 /* use defaults:  do_inactive = incr = 0; */
2504         } else if (KEY_IS(KEY_MDS_CONN)) {
2505                 mds_con = 1;
2506         }
2507
2508         for (i = 0; i < count; i++, val = (char *)val + incr) {
2509                 if (next_id) {
2510                         tgt = lov->lov_tgts[((struct obd_id_info*)val)->idx];
2511                 } else {
2512                         tgt = lov->lov_tgts[i];
2513                 }
2514                 /* OST was disconnected */
2515                 if (!tgt || !tgt->ltd_exp)
2516                         continue;
2517
2518                 /* OST is inactive and we don't want inactive OSCs */
2519                 if (!tgt->ltd_active && !do_inactive)
2520                         continue;
2521
2522                 if (mds_con) {
2523                         struct mds_group_info *mgi;
2524
2525                         LASSERT(vallen == sizeof(*mgi));
2526                         mgi = (struct mds_group_info *)val;
2527
2528                         /* Only want a specific OSC */
2529                         if (mgi->uuid && !obd_uuid_equals(mgi->uuid,
2530                                                 &tgt->ltd_uuid))
2531                                 continue;
2532
2533                         err = obd_set_info_async(tgt->ltd_exp,
2534                                          keylen, key, sizeof(int),
2535                                          &mgi->group, set);
2536                 } else if (next_id) {
2537                         err = obd_set_info_async(tgt->ltd_exp,
2538                                          keylen, key, vallen,
2539                                          ((struct obd_id_info*)val)->data, set);
2540                 } else  {
2541                         /* Only want a specific OSC */
2542                         if (check_uuid &&
2543                             !obd_uuid_equals(val, &tgt->ltd_uuid))
2544                                 continue;
2545
2546                         err = obd_set_info_async(tgt->ltd_exp,
2547                                          keylen, key, vallen, val, set);
2548                 }
2549
2550                 if (!rc)
2551                         rc = err;
2552         }
2553
2554         lov_putref(obddev);
2555         if (no_set) {
2556                 err = ptlrpc_set_wait(set);
2557                 if (!rc)
2558                         rc = err;
2559                 ptlrpc_set_destroy(set);
2560         }
2561         RETURN(rc);
2562 }
2563
2564 static int lov_checkmd(struct obd_export *exp, struct obd_export *md_exp,
2565                        struct lov_stripe_md *lsm)
2566 {
2567         int rc;
2568         ENTRY;
2569
2570         if (!lsm)
2571                 RETURN(0);
2572         LASSERT(md_exp);
2573         LASSERT(lsm_op_find(lsm->lsm_magic) != NULL);
2574         rc = lsm_op_find(lsm->lsm_magic)->lsm_revalidate(lsm, md_exp->exp_obd);
2575
2576         RETURN(rc);
2577 }
2578
2579 int lov_test_and_clear_async_rc(struct lov_stripe_md *lsm)
2580 {
2581         int i, rc = 0;
2582         ENTRY;
2583
2584         for (i = 0; i < lsm->lsm_stripe_count; i++) {
2585                 struct lov_oinfo *loi = lsm->lsm_oinfo[i];
2586                 if (loi->loi_ar.ar_rc && !rc)
2587                         rc = loi->loi_ar.ar_rc;
2588                 loi->loi_ar.ar_rc = 0;
2589         }
2590         RETURN(rc);
2591 }
2592 EXPORT_SYMBOL(lov_test_and_clear_async_rc);
2593
2594
2595 static int lov_extent_calc(struct obd_export *exp, struct lov_stripe_md *lsm,
2596                            int cmd, __u64 *offset)
2597 {
2598         __u32 ssize = lsm->lsm_stripe_size;
2599         __u64 start;
2600
2601         start = *offset;
2602         do_div(start, ssize);
2603         start = start * ssize;
2604
2605         CDEBUG(D_DLMTRACE, "offset "LPU64", stripe %u, start "LPU64
2606                            ", end "LPU64"\n", *offset, ssize, start,
2607                            start + ssize - 1);
2608         if (cmd == OBD_CALC_STRIPE_END) {
2609                 *offset = start + ssize - 1;
2610         } else if (cmd == OBD_CALC_STRIPE_START) {
2611                 *offset = start;
2612         } else {
2613                 LBUG();
2614         }
2615
2616         RETURN(0);
2617 }
2618
2619
2620 #if 0
2621 struct lov_multi_wait {
2622         struct ldlm_lock *lock;
2623         wait_queue_t      wait;
2624         int               completed;
2625         int               generation;
2626 };
2627
2628 int lov_complete_many(struct obd_export *exp, struct lov_stripe_md *lsm,
2629                       struct lustre_handle *lockh)
2630 {
2631         struct lov_lock_handles *lov_lockh = NULL;
2632         struct lustre_handle *lov_lockhp;
2633         struct lov_obd *lov;
2634         struct lov_oinfo *loi;
2635         struct lov_multi_wait *queues;
2636         int rc = 0, i;
2637         ENTRY;
2638
2639         ASSERT_LSM_MAGIC(lsm);
2640
2641         if (!exp || !exp->exp_obd)
2642                 RETURN(-ENODEV);
2643
2644         LASSERT(lockh != NULL);
2645         if (lsm->lsm_stripe_count > 1) {
2646                 lov_lockh = lov_handle2llh(lockh);
2647                 if (lov_lockh == NULL) {
2648                         CERROR("LOV: invalid lov lock handle %p\n", lockh);
2649                         RETURN(-EINVAL);
2650                 }
2651
2652                 lov_lockhp = lov_lockh->llh_handles;
2653         } else {
2654                 lov_lockhp = lockh;
2655         }
2656
2657         OBD_ALLOC(queues, lsm->lsm_stripe_count * sizeof(*queues));
2658         if (queues == NULL)
2659                 GOTO(out, rc = -ENOMEM);
2660
2661         lov = &exp->exp_obd->u.lov;
2662         for (i = 0, loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count;
2663              i++, loi++, lov_lockhp++) {
2664                 struct ldlm_lock *lock;
2665                 struct obd_device *obd;
2666
2667                 lock = ldlm_handle2lock_long(lov_lockhp, 0);
2668                 if (lock == NULL) {
2669                         CDEBUG(D_HA, "lov idx %d subobj "LPX64" no lock?\n",
2670                                loi->loi_ost_idx, loi->loi_id);
2671                         queues[i].completed = 1;
2672                         continue;
2673                 }
2674
2675                 queues[i].lock = lock;
2676                 init_waitqueue_entry(&(queues[i].wait), current);
2677                 add_wait_queue(lock->l_waitq, &(queues[i].wait));
2678
2679                 obd = class_exp2obd(lock->l_conn_export);
2680                 if (obd != NULL)
2681                         imp = obd->u.cli.cl_import;
2682                 if (imp != NULL) {
2683                         spin_lock(&imp->imp_lock);
2684                         queues[i].generation = imp->imp_generation;
2685                         spin_unlock(&imp->imp_lock);
2686                 }
2687         }
2688
2689         lwi = LWI_TIMEOUT_INTR(obd_timeout * HZ, ldlm_expired_completion_wait,
2690                                interrupted_completion_wait, &lwd);
2691         rc = l_wait_event_added(check_multi_complete(queues, lsm), &lwi);
2692
2693         for (i = 0; i < lsm->lsm_stripe_count; i++)
2694                 remove_wait_queue(lock->l_waitq, &(queues[i].wait));
2695
2696         if (rc == -EINTR || rc == -ETIMEDOUT) {
2697
2698
2699         }
2700
2701  out:
2702         if (lov_lockh != NULL)
2703                 lov_llh_put(lov_lockh);
2704         RETURN(rc);
2705 }
2706 #endif
2707
2708 void lov_stripe_lock(struct lov_stripe_md *md)
2709 {
2710         LASSERT(md->lsm_lock_owner != cfs_curproc_pid());
2711         spin_lock(&md->lsm_lock);
2712         LASSERT(md->lsm_lock_owner == 0);
2713         md->lsm_lock_owner = cfs_curproc_pid();
2714 }
2715 EXPORT_SYMBOL(lov_stripe_lock);
2716
2717 void lov_stripe_unlock(struct lov_stripe_md *md)
2718 {
2719         LASSERT(md->lsm_lock_owner == cfs_curproc_pid());
2720         md->lsm_lock_owner = 0;
2721         spin_unlock(&md->lsm_lock);
2722 }
2723 EXPORT_SYMBOL(lov_stripe_unlock);
2724
2725
2726 struct obd_ops lov_obd_ops = {
2727         .o_owner               = THIS_MODULE,
2728         .o_setup               = lov_setup,
2729         .o_precleanup          = lov_precleanup,
2730         .o_cleanup             = lov_cleanup,
2731         //.o_process_config      = lov_process_config,
2732         .o_connect             = lov_connect,
2733         .o_disconnect          = lov_disconnect,
2734         .o_statfs              = lov_statfs,
2735         .o_statfs_async        = lov_statfs_async,
2736         .o_packmd              = lov_packmd,
2737         .o_unpackmd            = lov_unpackmd,
2738         .o_checkmd             = lov_checkmd,
2739         .o_create              = lov_create,
2740         .o_destroy             = lov_destroy,
2741         .o_getattr             = lov_getattr,
2742         .o_getattr_async       = lov_getattr_async,
2743         .o_setattr             = lov_setattr,
2744         .o_setattr_async       = lov_setattr_async,
2745         .o_brw                 = lov_brw,
2746         .o_merge_lvb           = lov_merge_lvb,
2747         .o_adjust_kms          = lov_adjust_kms,
2748         .o_punch               = lov_punch,
2749         .o_sync                = lov_sync,
2750         .o_enqueue             = lov_enqueue,
2751         .o_change_cbdata       = lov_change_cbdata,
2752         .o_cancel              = lov_cancel,
2753         .o_cancel_unused       = lov_cancel_unused,
2754         .o_iocontrol           = lov_iocontrol,
2755         .o_get_info            = lov_get_info,
2756         .o_set_info_async      = lov_set_info_async,
2757         .o_extent_calc         = lov_extent_calc,
2758         .o_llog_init           = lov_llog_init,
2759         .o_llog_finish         = lov_llog_finish,
2760         .o_notify              = lov_notify,
2761         .o_pool_new            = lov_pool_new,
2762         .o_pool_rem            = lov_pool_remove,
2763         .o_pool_add            = lov_pool_add,
2764         .o_pool_del            = lov_pool_del,
2765 };
2766
2767 static quota_interface_t *quota_interface;
2768 extern quota_interface_t lov_quota_interface;
2769
2770 cfs_mem_cache_t *lov_oinfo_slab;
2771
2772 extern struct lu_kmem_descr lov_caches[];
2773
2774 int __init lov_init(void)
2775 {
2776         struct lprocfs_static_vars lvars = { 0 };
2777         int rc, rc2;
2778         ENTRY;
2779
2780         /* print an address of _any_ initialized kernel symbol from this
2781          * module, to allow debugging with gdb that doesn't support data
2782          * symbols from modules.*/
2783         CDEBUG(D_CONSOLE, "Lustre LOV module (%p).\n", &lov_caches);
2784
2785         rc = lu_kmem_init(lov_caches);
2786         if (rc)
2787                 return rc;
2788
2789         lov_oinfo_slab = cfs_mem_cache_create("lov_oinfo",
2790                                               sizeof(struct lov_oinfo),
2791                                               0, SLAB_HWCACHE_ALIGN);
2792         if (lov_oinfo_slab == NULL) {
2793                 lu_kmem_fini(lov_caches);
2794                 return -ENOMEM;
2795         }
2796         lprocfs_lov_init_vars(&lvars);
2797
2798         request_module("lquota");
2799         quota_interface = PORTAL_SYMBOL_GET(lov_quota_interface);
2800         init_obd_quota_ops(quota_interface, &lov_obd_ops);
2801
2802         rc = class_register_type(&lov_obd_ops, NULL, lvars.module_vars,
2803                                  LUSTRE_LOV_NAME, &lov_device_type);
2804
2805         if (rc) {
2806                 if (quota_interface)
2807                         PORTAL_SYMBOL_PUT(lov_quota_interface);
2808                 rc2 = cfs_mem_cache_destroy(lov_oinfo_slab);
2809                 LASSERT(rc2 == 0);
2810                 lu_kmem_fini(lov_caches);
2811         }
2812
2813         RETURN(rc);
2814 }
2815
2816 #ifdef __KERNEL__
2817 static void /*__exit*/ lov_exit(void)
2818 {
2819         int rc;
2820
2821         lu_device_type_fini(&lov_device_type);
2822         lu_kmem_fini(lov_caches);
2823
2824         if (quota_interface)
2825                 PORTAL_SYMBOL_PUT(lov_quota_interface);
2826
2827         class_unregister_type(LUSTRE_LOV_NAME);
2828         rc = cfs_mem_cache_destroy(lov_oinfo_slab);
2829         LASSERT(rc == 0);
2830 }
2831
2832 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
2833 MODULE_DESCRIPTION("Lustre Logical Object Volume OBD driver");
2834 MODULE_LICENSE("GPL");
2835
2836 cfs_module(lov, LUSTRE_VERSION_STRING, lov_init, lov_exit);
2837 #endif