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