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