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