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