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