Whamcloud - gitweb
LU-15934 lod: renew the update llog
[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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  *
31  * lustre/lov/lov_obd.c
32  *
33  * Author: Phil Schwan <phil@clusterfs.com>
34  * Author: Peter Braam <braam@clusterfs.com>
35  * Author: Mike Shaver <shaver@clusterfs.com>
36  * Author: Nathan Rutman <nathan@clusterfs.com>
37  */
38
39 #define DEBUG_SUBSYSTEM S_LOV
40 #include <libcfs/libcfs.h>
41
42 #include <cl_object.h>
43 #include <lustre_dlm.h>
44 #include <lustre_fid.h>
45 #include <uapi/linux/lustre/lustre_ioctl.h>
46 #include <lustre_lib.h>
47 #include <lustre_mds.h>
48 #include <lustre_net.h>
49 #include <uapi/linux/lustre/lustre_param.h>
50 #include <lustre_swab.h>
51 #include <lprocfs_status.h>
52 #include <obd_class.h>
53 #include <obd_support.h>
54
55 #include "lov_internal.h"
56
57 /* Keep a refcount of lov->tgt usage to prevent racing with addition/deletion.
58    Any function that expects lov_tgts to remain stationary must take a ref. */
59 void lov_tgts_getref(struct obd_device *obd)
60 {
61         struct lov_obd *lov = &obd->u.lov;
62
63         /* nobody gets through here until lov_putref is done */
64         mutex_lock(&lov->lov_lock);
65         atomic_inc(&lov->lov_refcount);
66         mutex_unlock(&lov->lov_lock);
67 }
68
69 static void __lov_del_obd(struct obd_device *obd, struct lov_tgt_desc *tgt);
70
71 void lov_tgts_putref(struct obd_device *obd)
72 {
73         struct lov_obd *lov = &obd->u.lov;
74
75         mutex_lock(&lov->lov_lock);
76         /* ok to dec to 0 more than once -- ltd_exp's will be null */
77         if (atomic_dec_and_test(&lov->lov_refcount) && lov->lov_death_row) {
78                 LIST_HEAD(kill);
79                 struct lov_tgt_desc *tgt, *n;
80                 int i;
81
82                 CDEBUG(D_CONFIG, "destroying %d lov targets\n",
83                        lov->lov_death_row);
84                 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
85                         tgt = lov->lov_tgts[i];
86
87                         if (!tgt || !tgt->ltd_reap)
88                                 continue;
89                         list_add(&tgt->ltd_kill, &kill);
90                         /* XXX - right now there is a dependency on ld_tgt_count
91                          * being the maximum tgt index for computing the
92                          * mds_max_easize. So we can't shrink it. */
93                         lu_tgt_pool_remove(&lov->lov_packed, i);
94                         lov->lov_tgts[i] = NULL;
95                         lov->lov_death_row--;
96                 }
97                 mutex_unlock(&lov->lov_lock);
98
99                 list_for_each_entry_safe(tgt, n, &kill, ltd_kill) {
100                         list_del(&tgt->ltd_kill);
101                         /* Disconnect */
102                         __lov_del_obd(obd, tgt);
103                 }
104         } else {
105                 mutex_unlock(&lov->lov_lock);
106         }
107 }
108
109 static int lov_notify(struct obd_device *obd, struct obd_device *watched,
110                       enum obd_notify_event ev);
111
112 int lov_connect_osc(struct obd_device *obd, u32 index, int activate,
113                     struct obd_connect_data *data)
114 {
115         struct lov_obd *lov = &obd->u.lov;
116         struct obd_uuid *tgt_uuid;
117         struct obd_device *tgt_obd;
118         static struct obd_uuid lov_osc_uuid = { "LOV_OSC_UUID" };
119         struct obd_import *imp;
120         int rc;
121         ENTRY;
122
123         if (lov->lov_tgts[index] == NULL)
124                 RETURN(-EINVAL);
125
126         tgt_uuid = &lov->lov_tgts[index]->ltd_uuid;
127         tgt_obd = lov->lov_tgts[index]->ltd_obd;
128
129         if (!tgt_obd->obd_set_up) {
130                 CERROR("Target %s not set up\n", obd_uuid2str(tgt_uuid));
131                 RETURN(-EINVAL);
132         }
133
134         /* override the sp_me from lov */
135         tgt_obd->u.cli.cl_sp_me = lov->lov_sp_me;
136
137         if (data && (data->ocd_connect_flags & OBD_CONNECT_INDEX))
138                 data->ocd_index = index;
139
140         /*
141          * Divine LOV knows that OBDs under it are OSCs.
142          */
143         imp = tgt_obd->u.cli.cl_import;
144
145         if (activate) {
146                 tgt_obd->obd_no_recov = 0;
147                 /* FIXME this is probably supposed to be
148                    ptlrpc_set_import_active.  Horrible naming. */
149                 ptlrpc_activate_import(imp, false);
150         }
151
152         rc = obd_register_observer(tgt_obd, obd);
153         if (rc) {
154                 CERROR("Target %s register_observer error %d\n",
155                        obd_uuid2str(tgt_uuid), rc);
156                 RETURN(rc);
157         }
158
159         if (imp->imp_invalid) {
160                 CDEBUG(D_CONFIG, "%s: not connecting - administratively disabled\n",
161                        obd_uuid2str(tgt_uuid));
162                 RETURN(0);
163         }
164
165         rc = obd_connect(NULL, &lov->lov_tgts[index]->ltd_exp, tgt_obd,
166                          &lov_osc_uuid, data, lov->lov_cache);
167         if (rc || !lov->lov_tgts[index]->ltd_exp) {
168                 CERROR("Target %s connect error %d\n",
169                        obd_uuid2str(tgt_uuid), rc);
170                 RETURN(-ENODEV);
171         }
172
173         lov->lov_tgts[index]->ltd_reap = 0;
174
175         CDEBUG(D_CONFIG, "Connected tgt idx %d %s (%s) %sactive\n", index,
176                obd_uuid2str(tgt_uuid), tgt_obd->obd_name, activate ? "":"in");
177
178         if (lov->lov_tgts_kobj) {
179                 /* Even if we failed, that's ok */
180                 rc = sysfs_create_link(lov->lov_tgts_kobj,
181                                        &tgt_obd->obd_kset.kobj,
182                                        tgt_obd->obd_name);
183                 if (rc) {
184                         CERROR("%s: can't register LOV target /sys/fs/lustre/%s/%s/target_obds/%s : rc = %d\n",
185                                obd->obd_name, obd->obd_type->typ_name,
186                                obd->obd_name,
187                                lov->lov_tgts[index]->ltd_exp->exp_obd->obd_name,
188                                rc);
189                 }
190         }
191         RETURN(0);
192 }
193
194 static int lov_connect(const struct lu_env *env,
195                        struct obd_export **exp, struct obd_device *obd,
196                        struct obd_uuid *cluuid, struct obd_connect_data *data,
197                        void *localdata)
198 {
199         struct lov_obd *lov = &obd->u.lov;
200         struct lov_tgt_desc *tgt;
201         struct lustre_handle conn;
202         int i, rc;
203         ENTRY;
204
205         CDEBUG(D_CONFIG, "connect #%d\n", lov->lov_connects);
206
207         rc = class_connect(&conn, obd, cluuid);
208         if (rc)
209                 RETURN(rc);
210
211         *exp = class_conn2export(&conn);
212
213         /* Why should there ever be more than 1 connect? */
214         lov->lov_connects++;
215         LASSERT(lov->lov_connects == 1);
216
217         memset(&lov->lov_ocd, 0, sizeof(lov->lov_ocd));
218         if (data)
219                 lov->lov_ocd = *data;
220
221         lov_tgts_getref(obd);
222
223         if (localdata) {
224                 lov->lov_cache = localdata;
225                 cl_cache_incref(lov->lov_cache);
226         }
227
228         for (i = 0; i < lov->desc.ld_tgt_count; i++) {
229                 tgt = lov->lov_tgts[i];
230                 if (!tgt || obd_uuid_empty(&tgt->ltd_uuid))
231                         continue;
232                 /* Flags will be lowest common denominator */
233                 rc = lov_connect_osc(obd, i, tgt->ltd_activate, &lov->lov_ocd);
234                 if (rc) {
235                         CERROR("%s: lov connect tgt %d failed: %d\n",
236                                obd->obd_name, i, rc);
237                         continue;
238                 }
239                 /* connect to administrative disabled ost */
240                 if (!lov->lov_tgts[i]->ltd_exp)
241                         continue;
242
243                 rc = lov_notify(obd, lov->lov_tgts[i]->ltd_exp->exp_obd,
244                                 OBD_NOTIFY_CONNECT);
245                 if (rc) {
246                         CERROR("%s error sending notify %d\n",
247                                obd->obd_name, rc);
248                 }
249         }
250
251         lov_tgts_putref(obd);
252
253         RETURN(0);
254 }
255
256 static int lov_disconnect_obd(struct obd_device *obd, struct lov_tgt_desc *tgt)
257 {
258         struct lov_obd *lov = &obd->u.lov;
259         struct obd_device *osc_obd;
260         int rc;
261         ENTRY;
262
263         osc_obd = class_exp2obd(tgt->ltd_exp);
264         CDEBUG(D_CONFIG, "%s: disconnecting target %s\n", obd->obd_name,
265                osc_obd ? osc_obd->obd_name : "<no obd>");
266
267         if (tgt->ltd_active) {
268                 tgt->ltd_active = 0;
269                 lov->desc.ld_active_tgt_count--;
270                 tgt->ltd_exp->exp_obd->obd_inactive = 1;
271         }
272
273         if (osc_obd) {
274                 if (lov->lov_tgts_kobj)
275                         sysfs_remove_link(lov->lov_tgts_kobj,
276                                           osc_obd->obd_name);
277
278                 /* Pass it on to our clients.
279                  * XXX This should be an argument to disconnect,
280                  * XXX not a back-door flag on the OBD.  Ah well.
281                  */
282                 osc_obd->obd_force = obd->obd_force;
283                 osc_obd->obd_fail = obd->obd_fail;
284                 osc_obd->obd_no_recov = obd->obd_no_recov;
285         }
286
287         obd_register_observer(osc_obd, NULL);
288
289         rc = obd_disconnect(tgt->ltd_exp);
290         if (rc) {
291                 CERROR("Target %s disconnect error %d\n",
292                        tgt->ltd_uuid.uuid, rc);
293                 rc = 0;
294         }
295
296         tgt->ltd_exp = NULL;
297         RETURN(0);
298 }
299
300 static int lov_disconnect(struct obd_export *exp)
301 {
302         struct obd_device *obd = class_exp2obd(exp);
303         struct lov_obd *lov = &obd->u.lov;
304         u32 index;
305         int rc;
306
307         ENTRY;
308         if (!lov->lov_tgts)
309                 goto out;
310
311         /* Only disconnect the underlying layers on the final disconnect. */
312         if (lov->lov_connects == 0) {
313                 CWARN("%s: was disconnected already #%d\n",
314                       obd->obd_name, lov->lov_connects);
315                 RETURN(0);
316         }
317
318         lov->lov_connects--;
319         if (lov->lov_connects > 0) {
320                 /* why should there be more than 1 connect? */
321                 CWARN("%s: unexpected disconnect #%d\n",
322                       obd->obd_name, lov->lov_connects);
323                 goto out;
324         }
325
326         /* hold another ref so lov_del_obd() doesn't spin in putref each time */
327         lov_tgts_getref(obd);
328
329         for (index = 0; index < lov->desc.ld_tgt_count; index++) {
330                 if (lov->lov_tgts[index] && lov->lov_tgts[index]->ltd_exp) {
331                         /* Disconnection is the last we know about an OBD */
332                         lov_del_target(obd, index, NULL,
333                                        lov->lov_tgts[index]->ltd_gen);
334                 }
335         }
336         lov_tgts_putref(obd);
337
338 out:
339         rc = class_disconnect(exp); /* bz 9811 */
340         RETURN(rc);
341 }
342
343 /* Error codes:
344  *
345  *  -EINVAL  : UUID can't be found in the LOV's target list
346  *  -ENOTCONN: The UUID is found, but the target connection is bad (!)
347  *  -EBADF   : The UUID is found, but the OBD is the wrong type (!)
348  *  any >= 0 : is log target index
349  */
350 static int lov_set_osc_active(struct obd_device *obd, struct obd_uuid *uuid,
351                               enum obd_notify_event ev)
352 {
353         struct lov_obd *lov = &obd->u.lov;
354         struct lov_tgt_desc *tgt;
355         int index;
356         bool activate, active;
357         ENTRY;
358
359         CDEBUG(D_INFO, "Searching in lov %p for uuid %s event(%d)\n",
360                lov, uuid->uuid, ev);
361
362         lov_tgts_getref(obd);
363         for (index = 0; index < lov->desc.ld_tgt_count; index++) {
364                 tgt = lov->lov_tgts[index];
365                 if (tgt && obd_uuid_equals(uuid, &tgt->ltd_uuid))
366                         break;
367         }
368
369         if (index == lov->desc.ld_tgt_count)
370                 GOTO(out, index = -EINVAL);
371
372         if (ev == OBD_NOTIFY_DEACTIVATE || ev == OBD_NOTIFY_ACTIVATE) {
373                 activate = (ev == OBD_NOTIFY_ACTIVATE);
374
375                 /*
376                  * LU-642, initially inactive OSC could miss the obd_connect,
377                  * we make up for it here.
378                  */
379                 if (activate && !tgt->ltd_exp) {
380                         int rc;
381                         struct obd_uuid lov_osc_uuid = {"LOV_OSC_UUID"};
382
383                         rc = obd_connect(NULL, &tgt->ltd_exp, tgt->ltd_obd,
384                                          &lov_osc_uuid, &lov->lov_ocd,
385                                          lov->lov_cache);
386                         if (rc || !tgt->ltd_exp)
387                                 GOTO(out, index = rc);
388                 }
389
390                 if (lov->lov_tgts[index]->ltd_activate == activate) {
391                         CDEBUG(D_INFO, "OSC %s already %sactivate!\n",
392                                uuid->uuid, activate ? "" : "de");
393                 } else {
394                         lov->lov_tgts[index]->ltd_activate = activate;
395                         CDEBUG(D_CONFIG, "%sactivate OSC %s\n",
396                                activate ? "" : "de", obd_uuid2str(uuid));
397                 }
398         } else if (ev == OBD_NOTIFY_INACTIVE || ev == OBD_NOTIFY_ACTIVE) {
399                 active = (ev == OBD_NOTIFY_ACTIVE);
400
401                 if (lov->lov_tgts[index]->ltd_active == active) {
402                         CDEBUG(D_INFO, "OSC %s already %sactive!\n",
403                                uuid->uuid, active ? "" : "in");
404                         GOTO(out, index);
405                 }
406                 CDEBUG(D_CONFIG, "Marking OSC %s %sactive\n",
407                        obd_uuid2str(uuid), active ? "" : "in");
408
409                 lov->lov_tgts[index]->ltd_active = active;
410                 if (active) {
411                         lov->desc.ld_active_tgt_count++;
412                         lov->lov_tgts[index]->ltd_exp->exp_obd->obd_inactive = 0;
413                 } else {
414                         lov->desc.ld_active_tgt_count--;
415                         lov->lov_tgts[index]->ltd_exp->exp_obd->obd_inactive = 1;
416                 }
417         } else {
418                 CERROR("%s: unknown event %d for uuid %s\n", obd->obd_name,
419                        ev, uuid->uuid);
420         }
421
422         if (tgt->ltd_exp)
423                 CDEBUG(D_INFO, "%s: lov idx %d conn %llx\n", obd_uuid2str(uuid),
424                        index, tgt->ltd_exp->exp_handle.h_cookie);
425
426  out:
427         lov_tgts_putref(obd);
428         RETURN(index);
429 }
430
431 static int lov_notify(struct obd_device *obd, struct obd_device *watched,
432                       enum obd_notify_event ev)
433 {
434         int rc = 0;
435         struct lov_obd *lov = &obd->u.lov;
436         ENTRY;
437
438         down_read(&lov->lov_notify_lock);
439         if (!lov->lov_connects)
440                 GOTO(out_notify_lock, rc = 0);
441
442         if (ev == OBD_NOTIFY_ACTIVE || ev == OBD_NOTIFY_INACTIVE ||
443             ev == OBD_NOTIFY_ACTIVATE || ev == OBD_NOTIFY_DEACTIVATE) {
444                 struct obd_uuid *uuid;
445
446                 LASSERT(watched);
447
448                 if (strcmp(watched->obd_type->typ_name, LUSTRE_OSC_NAME)) {
449                         CERROR("unexpected notification of %s %s\n",
450                                watched->obd_type->typ_name, watched->obd_name);
451                         GOTO(out_notify_lock, rc = -EINVAL);
452                 }
453
454                 uuid = &watched->u.cli.cl_target_uuid;
455
456                 /* Set OSC as active before notifying the observer, so the
457                  * observer can use the OSC normally.
458                  */
459                 rc = lov_set_osc_active(obd, uuid, ev);
460                 if (rc < 0) {
461                         CERROR("%s: event %d failed: rc = %d\n", obd->obd_name,
462                                ev, rc);
463                         GOTO(out_notify_lock, rc);
464                 }
465         }
466
467         /* Pass the notification up the chain. */
468         rc = obd_notify_observer(obd, watched, ev);
469
470 out_notify_lock:
471         up_read(&lov->lov_notify_lock);
472
473         RETURN(rc);
474 }
475
476 static int lov_add_target(struct obd_device *obd, struct obd_uuid *uuidp,
477                           u32 index, int gen, int active)
478 {
479         struct lov_obd *lov = &obd->u.lov;
480         struct lov_tgt_desc *tgt;
481         struct obd_device *tgt_obd;
482         int rc;
483
484         ENTRY;
485         CDEBUG(D_CONFIG, "uuid:%s idx:%u gen:%d active:%d\n",
486                uuidp->uuid, index, gen, active);
487
488         if (gen <= 0) {
489                 CERROR("%s: request to add '%s' with invalid generation: %d\n",
490                        obd->obd_name, uuidp->uuid, gen);
491                 RETURN(-EINVAL);
492         }
493
494         tgt_obd = class_find_client_obd(uuidp, LUSTRE_OSC_NAME, &obd->obd_uuid);
495         if (tgt_obd == NULL)
496                 RETURN(-EINVAL);
497
498         mutex_lock(&lov->lov_lock);
499
500         if ((index < lov->lov_tgt_size) && (lov->lov_tgts[index] != NULL)) {
501                 tgt = lov->lov_tgts[index];
502                 rc = -EEXIST;
503                 CERROR("%s: UUID %s already assigned at index %d: rc = %d\n",
504                        obd->obd_name, obd_uuid2str(&tgt->ltd_uuid), index, rc);
505                 mutex_unlock(&lov->lov_lock);
506                 RETURN(rc);
507         }
508
509         if (index >= lov->lov_tgt_size) {
510                 /* We need to reallocate the lov target array. */
511                 struct lov_tgt_desc **newtgts, **old = NULL;
512                 __u32 newsize, oldsize = 0;
513
514                 newsize = max(lov->lov_tgt_size, 2U);
515                 while (newsize < index + 1)
516                         newsize = newsize << 1;
517                 OBD_ALLOC_PTR_ARRAY(newtgts, newsize);
518                 if (newtgts == NULL) {
519                         mutex_unlock(&lov->lov_lock);
520                         RETURN(-ENOMEM);
521                 }
522
523                 if (lov->lov_tgt_size) {
524                         memcpy(newtgts, lov->lov_tgts, sizeof(*newtgts) *
525                                lov->lov_tgt_size);
526                         old = lov->lov_tgts;
527                         oldsize = lov->lov_tgt_size;
528                 }
529
530                 lov->lov_tgts = newtgts;
531                 lov->lov_tgt_size = newsize;
532                 smp_rmb();
533                 if (old)
534                         OBD_FREE_PTR_ARRAY(old, oldsize);
535
536                 CDEBUG(D_CONFIG, "tgts: %p size: %d\n",
537                        lov->lov_tgts, lov->lov_tgt_size);
538         }
539
540         OBD_ALLOC_PTR(tgt);
541         if (!tgt) {
542                 mutex_unlock(&lov->lov_lock);
543                 RETURN(-ENOMEM);
544         }
545
546         rc = lu_tgt_pool_add(&lov->lov_packed, index, lov->lov_tgt_size);
547         if (rc) {
548                 mutex_unlock(&lov->lov_lock);
549                 OBD_FREE_PTR(tgt);
550                 RETURN(rc);
551         }
552
553         tgt->ltd_uuid = *uuidp;
554         tgt->ltd_obd = tgt_obd;
555         /* XXX - add a sanity check on the generation number. */
556         tgt->ltd_gen = gen;
557         tgt->ltd_index = index;
558         tgt->ltd_activate = active;
559         lov->lov_tgts[index] = tgt;
560         if (index >= lov->desc.ld_tgt_count)
561                 lov->desc.ld_tgt_count = index + 1;
562
563         mutex_unlock(&lov->lov_lock);
564
565         CDEBUG(D_CONFIG, "idx=%d ltd_gen=%d ld_tgt_count=%d\n",
566                 index, tgt->ltd_gen, lov->desc.ld_tgt_count);
567
568         if (lov->lov_connects == 0) {
569                 /* lov_connect hasn't been called yet. We'll do the
570                    lov_connect_osc on this target when that fn first runs,
571                    because we don't know the connect flags yet. */
572                 RETURN(0);
573         }
574
575         lov_tgts_getref(obd);
576
577         rc = lov_connect_osc(obd, index, active, &lov->lov_ocd);
578         if (rc)
579                 GOTO(out, rc);
580
581         /* connect to administrative disabled ost */
582         if (!tgt->ltd_exp)
583                 GOTO(out, rc = 0);
584
585         rc = lov_notify(obd, tgt->ltd_exp->exp_obd,
586                         active ? OBD_NOTIFY_CONNECT : OBD_NOTIFY_INACTIVE);
587
588 out:
589         if (rc) {
590                 CERROR("%s: add failed, deleting %s: rc = %d\n",
591                        obd->obd_name, obd_uuid2str(&tgt->ltd_uuid), rc);
592                 lov_del_target(obd, index, NULL, 0);
593         }
594         lov_tgts_putref(obd);
595         RETURN(rc);
596 }
597
598 /* Schedule a target for deletion */
599 int lov_del_target(struct obd_device *obd, u32 index,
600                    struct obd_uuid *uuidp, int gen)
601 {
602         struct lov_obd *lov = &obd->u.lov;
603         int count = lov->desc.ld_tgt_count;
604         int rc = 0;
605         ENTRY;
606
607         if (index >= count) {
608                 CERROR("LOV target index %d >= number of LOV OBDs %d.\n",
609                        index, count);
610                 RETURN(-EINVAL);
611         }
612
613         /* to make sure there's no ongoing lov_notify() now */
614         down_write(&lov->lov_notify_lock);
615         lov_tgts_getref(obd);
616
617         if (!lov->lov_tgts[index]) {
618                 CERROR("LOV target at index %d is not setup.\n", index);
619                 GOTO(out, rc = -EINVAL);
620         }
621
622         if (uuidp && !obd_uuid_equals(uuidp, &lov->lov_tgts[index]->ltd_uuid)) {
623                 CERROR("LOV target UUID %s at index %d doesn't match %s.\n",
624                        lov_uuid2str(lov, index), index,
625                        obd_uuid2str(uuidp));
626                 GOTO(out, rc = -EINVAL);
627         }
628
629         CDEBUG(D_CONFIG, "uuid: %s idx: %d gen: %d exp: %p active: %d\n",
630                lov_uuid2str(lov, index), index,
631                lov->lov_tgts[index]->ltd_gen, lov->lov_tgts[index]->ltd_exp,
632                lov->lov_tgts[index]->ltd_active);
633
634         lov->lov_tgts[index]->ltd_reap = 1;
635         lov->lov_death_row++;
636         /* we really delete it from lov_tgts_putref() */
637 out:
638         lov_tgts_putref(obd);
639         up_write(&lov->lov_notify_lock);
640
641         RETURN(rc);
642 }
643
644 static void __lov_del_obd(struct obd_device *obd, struct lov_tgt_desc *tgt)
645 {
646         struct obd_device *osc_obd;
647
648         LASSERT(tgt);
649         LASSERT(tgt->ltd_reap);
650
651         osc_obd = class_exp2obd(tgt->ltd_exp);
652
653         CDEBUG(D_CONFIG, "Removing tgt %s : %s\n",
654                tgt->ltd_uuid.uuid,
655                osc_obd ? osc_obd->obd_name : "<no obd>");
656
657         if (tgt->ltd_exp)
658                 lov_disconnect_obd(obd, tgt);
659
660         OBD_FREE_PTR(tgt);
661
662         /* Manual cleanup - no cleanup logs to clean up the osc's.  We must
663            do it ourselves. And we can't do it from lov_cleanup,
664            because we just lost our only reference to it. */
665         if (osc_obd)
666                 class_manual_cleanup(osc_obd);
667 }
668
669 void lov_fix_desc_stripe_size(__u64 *val)
670 {
671         if (*val < LOV_MIN_STRIPE_SIZE) {
672                 if (*val != 0)
673                         LCONSOLE_INFO("Increasing default stripe size to "
674                                       "minimum %u\n",
675                                       LOV_DESC_STRIPE_SIZE_DEFAULT);
676                 *val = LOV_DESC_STRIPE_SIZE_DEFAULT;
677         } else if (*val & (LOV_MIN_STRIPE_SIZE - 1)) {
678                 *val &= ~(LOV_MIN_STRIPE_SIZE - 1);
679                 LCONSOLE_WARN("Changing default stripe size to %llu (a "
680                               "multiple of %u)\n",
681                               *val, LOV_MIN_STRIPE_SIZE);
682         }
683 }
684
685 void lov_fix_desc_stripe_count(__u32 *val)
686 {
687         if (*val == 0)
688                 *val = 1;
689 }
690
691 void lov_fix_desc_pattern(__u32 *val)
692 {
693         /* from lov_setstripe */
694         if ((*val != 0) && !lov_pattern_supported_normal_comp(*val)) {
695                 LCONSOLE_WARN("lov: Unknown stripe pattern: %#x\n", *val);
696                 *val = 0;
697         }
698 }
699
700 void lov_fix_desc_qos_maxage(__u32 *val)
701 {
702         if (*val == 0)
703                 *val = LOV_DESC_QOS_MAXAGE_DEFAULT;
704 }
705
706 void lov_fix_desc(struct lov_desc *desc)
707 {
708         lov_fix_desc_stripe_size(&desc->ld_default_stripe_size);
709         lov_fix_desc_stripe_count(&desc->ld_default_stripe_count);
710         lov_fix_desc_pattern(&desc->ld_pattern);
711         lov_fix_desc_qos_maxage(&desc->ld_qos_maxage);
712 }
713
714 int lov_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
715 {
716         struct lov_desc *desc;
717         struct lov_obd *lov = &obd->u.lov;
718         int rc;
719         ENTRY;
720
721         if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
722                 CERROR("LOV setup requires a descriptor\n");
723                 RETURN(-EINVAL);
724         }
725
726         desc = (struct lov_desc *)lustre_cfg_buf(lcfg, 1);
727
728         if (sizeof(*desc) > LUSTRE_CFG_BUFLEN(lcfg, 1)) {
729                 CERROR("descriptor size wrong: %d > %d\n",
730                        (int)sizeof(*desc), LUSTRE_CFG_BUFLEN(lcfg, 1));
731                 RETURN(-EINVAL);
732         }
733
734         if (desc->ld_magic != LOV_DESC_MAGIC) {
735                 if (desc->ld_magic == __swab32(LOV_DESC_MAGIC)) {
736                             CDEBUG(D_OTHER, "%s: Swabbing lov desc %p\n",
737                                    obd->obd_name, desc);
738                             lustre_swab_lov_desc(desc);
739                 } else {
740                         CERROR("%s: Bad lov desc magic: %#x\n",
741                                obd->obd_name, desc->ld_magic);
742                         RETURN(-EINVAL);
743                 }
744         }
745
746         lov_fix_desc(desc);
747
748         desc->ld_active_tgt_count = 0;
749         lov->desc = *desc;
750         lov->lov_tgt_size = 0;
751
752         mutex_init(&lov->lov_lock);
753         atomic_set(&lov->lov_refcount, 0);
754         lov->lov_sp_me = LUSTRE_SP_CLI;
755
756         init_rwsem(&lov->lov_notify_lock);
757
758         INIT_LIST_HEAD(&lov->lov_pool_list);
759         lov->lov_pool_count = 0;
760         rc = lov_pool_hash_init(&lov->lov_pools_hash_body);
761         if (rc)
762                 GOTO(out, rc);
763
764         rc = lu_tgt_pool_init(&lov->lov_packed, 0);
765         if (rc)
766                 GOTO(out, rc);
767
768         rc = lov_tunables_init(obd);
769         if (rc)
770                 GOTO(out, rc);
771
772         lov->lov_tgts_kobj = kobject_create_and_add("target_obds",
773                                                     &obd->obd_kset.kobj);
774
775 out:
776         return rc;
777 }
778
779 static int lov_cleanup(struct obd_device *obd)
780 {
781         struct lov_obd *lov = &obd->u.lov;
782         struct pool_desc *pool, *tmp;
783         ENTRY;
784
785         if (lov->lov_tgts_kobj) {
786                 kobject_put(lov->lov_tgts_kobj);
787                 lov->lov_tgts_kobj = NULL;
788         }
789
790         list_for_each_entry_safe(pool, tmp, &lov->lov_pool_list, pool_list) {
791                 /* free pool structs */
792                 CDEBUG(D_INFO, "delete pool %p\n", pool);
793                 /* In the function below, .hs_keycmp resolves to
794                  * pool_hashkey_keycmp() */
795                 /* coverity[overrun-buffer-val] */
796                 lov_pool_del(obd, pool->pool_name);
797         }
798         lov_pool_hash_destroy(&lov->lov_pools_hash_body);
799         lu_tgt_pool_free(&lov->lov_packed);
800
801         lprocfs_obd_cleanup(obd);
802         if (lov->lov_tgts) {
803                 int i;
804                 lov_tgts_getref(obd);
805                 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
806                         if (!lov->lov_tgts[i])
807                                 continue;
808
809                         /* Inactive targets may never have connected */
810                         if (lov->lov_tgts[i]->ltd_active)
811                                 /* We should never get here - these
812                                  * should have been removed in the
813                                  * disconnect. */
814                                 CERROR("%s: lov tgt %d not cleaned! "
815                                        "deathrow=%d, lovrc=%d\n",
816                                        obd->obd_name, i, lov->lov_death_row,
817                                        atomic_read(&lov->lov_refcount));
818                         lov_del_target(obd, i, NULL, 0);
819                 }
820                 lov_tgts_putref(obd);
821                 OBD_FREE_PTR_ARRAY(lov->lov_tgts, lov->lov_tgt_size);
822                 lov->lov_tgt_size = 0;
823         }
824
825         if (lov->lov_cache != NULL) {
826                 cl_cache_decref(lov->lov_cache);
827                 lov->lov_cache = NULL;
828         }
829
830         RETURN(0);
831 }
832
833 int lov_process_config_base(struct obd_device *obd, struct lustre_cfg *lcfg,
834                             u32 *indexp, int *genp)
835 {
836         struct obd_uuid obd_uuid;
837         int cmd;
838         int rc = 0;
839
840         ENTRY;
841         switch (cmd = lcfg->lcfg_command) {
842         case LCFG_ADD_MDC:
843         case LCFG_DEL_MDC:
844                 break;
845         case LCFG_LOV_ADD_OBD:
846         case LCFG_LOV_ADD_INA:
847         case LCFG_LOV_DEL_OBD: {
848                 u32 index;
849                 int gen;
850
851                 /* lov_modify_tgts add  0:lov_mdsA  1:ost1_UUID  2:0  3:1 */
852                 if (LUSTRE_CFG_BUFLEN(lcfg, 1) > sizeof(obd_uuid.uuid))
853                         GOTO(out, rc = -EINVAL);
854
855                 obd_str2uuid(&obd_uuid,  lustre_cfg_buf(lcfg, 1));
856
857                 rc = kstrtou32(lustre_cfg_buf(lcfg, 2), 10, indexp);
858                 if (rc)
859                         GOTO(out, rc);
860                 rc = kstrtoint(lustre_cfg_buf(lcfg, 3), 10, genp);
861                 if (rc)
862                         GOTO(out, rc);
863                 index = *indexp;
864                 gen = *genp;
865                 if (cmd == LCFG_LOV_ADD_OBD)
866                         rc = lov_add_target(obd, &obd_uuid, index, gen, 1);
867                 else if (cmd == LCFG_LOV_ADD_INA)
868                         rc = lov_add_target(obd, &obd_uuid, index, gen, 0);
869                 else
870                         rc = lov_del_target(obd, index, &obd_uuid, gen);
871
872                 GOTO(out, rc);
873         }
874         case LCFG_PARAM: {
875                 struct lov_desc *desc = &(obd->u.lov.desc);
876                 ssize_t count;
877
878                 if (!desc)
879                         GOTO(out, rc = -EINVAL);
880
881                 count = class_modify_config(lcfg, PARAM_LOV,
882                                             &obd->obd_kset.kobj);
883                 GOTO(out, rc = count < 0 ? count : 0);
884         }
885         case LCFG_POOL_NEW:
886         case LCFG_POOL_ADD:
887         case LCFG_POOL_DEL:
888         case LCFG_POOL_REM:
889                 GOTO(out, rc);
890
891         default: {
892                 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
893                 GOTO(out, rc = -EINVAL);
894
895         }
896         }
897 out:
898         RETURN(rc);
899 }
900
901 static int lov_statfs(const struct lu_env *env, struct obd_export *exp,
902                       struct obd_statfs *osfs, time64_t max_age, __u32 flags)
903 {
904         struct obd_device *obd = class_exp2obd(exp);
905         struct lov_obd *lov = &obd->u.lov;
906         struct obd_info oinfo = {
907                 .oi_osfs = osfs,
908                 .oi_flags = flags,
909         };
910         struct ptlrpc_request_set *rqset;
911         struct lov_request_set *set = NULL;
912         struct lov_request *req;
913         int rc = 0;
914         int rc2;
915
916         ENTRY;
917
918         rqset = ptlrpc_prep_set();
919         if (rqset == NULL)
920                 RETURN(-ENOMEM);
921
922         rc = lov_prep_statfs_set(obd, &oinfo, &set);
923         if (rc < 0)
924                 GOTO(out_rqset, rc);
925
926         list_for_each_entry(req, &set->set_list, rq_link) {
927                 rc = obd_statfs_async(lov->lov_tgts[req->rq_idx]->ltd_exp,
928                                       &req->rq_oi, max_age, rqset);
929                 if (rc < 0)
930                         GOTO(out_set, rc);
931         }
932
933         rc = ptlrpc_set_wait(env, rqset);
934
935 out_set:
936         if (rc < 0)
937                 atomic_set(&set->set_completes, 0);
938
939         rc2 = lov_fini_statfs_set(set);
940         if (rc == 0)
941                 rc = rc2;
942
943 out_rqset:
944         ptlrpc_set_destroy(rqset);
945
946         RETURN(rc);
947 }
948
949 static int lov_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
950                          void *karg, void __user *uarg)
951 {
952         struct obd_device *obd = class_exp2obd(exp);
953         struct lov_obd *lov = &obd->u.lov;
954         int i = 0, rc = 0, count = lov->desc.ld_tgt_count;
955
956         ENTRY;
957         CDEBUG(D_IOCTL, "%s: cmd=%x len=%u karg=%pK uarg=%pK\n",
958                exp->exp_obd->obd_name, cmd, len, karg, uarg);
959
960         /* exit early for unknown ioctl types */
961         if (unlikely(_IOC_TYPE(cmd) != 'f' && cmd != IOC_OSC_SET_ACTIVE))
962                 RETURN(OBD_IOC_DEBUG(D_IOCTL, obd->obd_name, cmd, "unknown",
963                                      -ENOTTY));
964
965         /* can't do a generic karg == NULL check here, since it is too noisy and
966          * we need to return -ENOTTY for unsupported ioctls instead of -EINVAL.
967          */
968         switch (cmd) {
969         case IOC_OBD_STATFS: {
970                 struct obd_ioctl_data *data;
971                 struct obd_device *osc_obd;
972                 struct obd_statfs stat_buf = {0};
973                 struct obd_import *imp;
974                 __u32 index;
975                 __u32 flags;
976
977                 if (unlikely(karg == NULL))
978                         RETURN(OBD_IOC_ERROR(obd->obd_name, cmd, "karg=null",
979                                              -EINVAL));
980                 data = karg;
981
982                 memcpy(&index, data->ioc_inlbuf2, sizeof(index));
983                 if (index >= count)
984                         RETURN(-ENODEV);
985
986                 if (!lov->lov_tgts[index])
987                         /* Try again with the next index */
988                         RETURN(-EAGAIN);
989
990                 osc_obd = class_exp2obd(lov->lov_tgts[index]->ltd_exp);
991                 if (!osc_obd)
992                         RETURN(-EINVAL);
993
994                 imp = osc_obd->u.cli.cl_import;
995                 if (!lov->lov_tgts[index]->ltd_active &&
996                     imp->imp_state != LUSTRE_IMP_IDLE)
997                         RETURN(-ENODATA);
998
999                 /* copy UUID */
1000                 if (copy_to_user(data->ioc_pbuf2, obd2cli_tgt(osc_obd),
1001                                  min_t(unsigned long, data->ioc_plen2,
1002                                        sizeof(struct obd_uuid))))
1003                         RETURN(-EFAULT);
1004
1005                 memcpy(&flags, data->ioc_inlbuf1, sizeof(flags));
1006                 flags = flags & LL_STATFS_NODELAY ? OBD_STATFS_NODELAY : 0;
1007
1008                 /* got statfs data */
1009                 rc = obd_statfs(NULL, lov->lov_tgts[index]->ltd_exp, &stat_buf,
1010                                 ktime_get_seconds() - OBD_STATFS_CACHE_SECONDS,
1011                                 flags);
1012                 if (rc)
1013                         RETURN(rc);
1014                 if (copy_to_user(data->ioc_pbuf1, &stat_buf,
1015                                  min_t(unsigned long, data->ioc_plen1,
1016                                        sizeof(struct obd_statfs))))
1017                         RETURN(-EFAULT);
1018                 break;
1019         }
1020         case OBD_IOC_QUOTACTL: {
1021                 struct if_quotactl *qctl;
1022                 struct lov_tgt_desc *tgt = NULL;
1023                 struct obd_quotactl *oqctl;
1024                 struct obd_import *imp;
1025
1026                 if (unlikely(karg == NULL))
1027                         RETURN(OBD_IOC_ERROR(obd->obd_name, cmd, "karg=null",
1028                                              -EINVAL));
1029                 qctl = karg;
1030
1031                 if (qctl->qc_valid == QC_OSTIDX) {
1032                         if (count <= qctl->qc_idx)
1033                                 RETURN(-EINVAL);
1034
1035                         tgt = lov->lov_tgts[qctl->qc_idx];
1036                         if (!tgt)
1037                                 RETURN(-ENODEV);
1038
1039                         if (!tgt->ltd_exp)
1040                                 RETURN(-EINVAL);
1041                 } else if (qctl->qc_valid == QC_UUID) {
1042                         for (i = 0; i < count; i++) {
1043                                 tgt = lov->lov_tgts[i];
1044                                 if (!tgt ||
1045                                     !obd_uuid_equals(&tgt->ltd_uuid,
1046                                                      &qctl->obd_uuid))
1047                                         continue;
1048
1049                                 if (tgt->ltd_exp == NULL)
1050                                         RETURN(-EINVAL);
1051
1052                                 break;
1053                         }
1054                 } else {
1055                         RETURN(-EINVAL);
1056                 }
1057
1058                 if (i >= count)
1059                         RETURN(-EAGAIN);
1060
1061                 LASSERT(tgt && tgt->ltd_exp);
1062                 imp = class_exp2cliimp(tgt->ltd_exp);
1063                 if (!tgt->ltd_active && imp->imp_state != LUSTRE_IMP_IDLE) {
1064                         qctl->qc_valid = QC_OSTIDX;
1065                         qctl->obd_uuid = tgt->ltd_uuid;
1066                         RETURN(-ENODATA);
1067                 }
1068
1069                 OBD_ALLOC_PTR(oqctl);
1070                 if (!oqctl)
1071                         RETURN(-ENOMEM);
1072
1073                 QCTL_COPY(oqctl, qctl);
1074                 rc = obd_quotactl(tgt->ltd_exp, oqctl);
1075                 if (rc == 0) {
1076                         QCTL_COPY(qctl, oqctl);
1077                         qctl->qc_valid = QC_OSTIDX;
1078                         qctl->obd_uuid = tgt->ltd_uuid;
1079                 }
1080                 OBD_FREE_PTR(oqctl);
1081                 break;
1082         }
1083         default: {
1084                 int set = 0;
1085
1086                 if (count == 0)
1087                         RETURN(-ENOTTY);
1088
1089                 for (i = 0; i < count; i++) {
1090                         int err;
1091                         struct obd_device *osc_obd;
1092
1093                         /* OST was disconnected */
1094                         if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_exp)
1095                                 continue;
1096
1097                         /* ll_umount_begin() sets force on lov, pass to osc */
1098                         osc_obd = class_exp2obd(lov->lov_tgts[i]->ltd_exp);
1099                         if (osc_obd)
1100                                 osc_obd->obd_force = obd->obd_force;
1101                         err = obd_iocontrol(cmd, lov->lov_tgts[i]->ltd_exp,
1102                                             len, karg, uarg);
1103                         if (err) {
1104                                 if (lov->lov_tgts[i]->ltd_active) {
1105                                         OBD_IOC_DEBUG(err == -ENOTTY ?
1106                                                       D_IOCTL : D_WARNING,
1107                                                       obd->obd_name, cmd,
1108                                                       lov_uuid2str(lov, i),
1109                                                       err);
1110                                         if (!rc)
1111                                                 rc = err;
1112
1113                                         if (err == -ENOTTY)
1114                                                 break;
1115                                 }
1116                         } else {
1117                                 set = 1;
1118                         }
1119                 }
1120                 if (!set && !rc)
1121                         rc = -EIO;
1122                 break;
1123         }
1124         }
1125
1126         RETURN(rc);
1127 }
1128
1129 static int lov_get_info(const struct lu_env *env, struct obd_export *exp,
1130                         __u32 keylen, void *key, __u32 *vallen, void *val)
1131 {
1132         struct obd_device *obd = class_exp2obd(exp);
1133         struct lov_obd *lov = &obd->u.lov;
1134         struct lov_desc *ld = &lov->desc;
1135         int rc = 0;
1136         ENTRY;
1137
1138         if (vallen == NULL || val == NULL)
1139                 RETURN(-EFAULT);
1140
1141         lov_tgts_getref(obd);
1142
1143         if (KEY_IS(KEY_MAX_EASIZE)) {
1144                 *((u32 *)val) = exp->exp_connect_data.ocd_max_easize;
1145         } else if (KEY_IS(KEY_DEFAULT_EASIZE)) {
1146                 u32 def_stripe_count = min_t(u32, ld->ld_default_stripe_count,
1147                                              LOV_MAX_STRIPE_COUNT);
1148
1149                 *((u32 *)val) = lov_mds_md_size(def_stripe_count, LOV_MAGIC_V3);
1150         } else if (KEY_IS(KEY_TGT_COUNT)) {
1151                 *((int *)val) = lov->desc.ld_tgt_count;
1152         } else {
1153                 rc = -EINVAL;
1154         }
1155
1156         lov_tgts_putref(obd);
1157
1158         RETURN(rc);
1159 }
1160
1161 static int lov_set_info_async(const struct lu_env *env, struct obd_export *exp,
1162                               __u32 keylen, void *key,
1163                               __u32 vallen, void *val,
1164                               struct ptlrpc_request_set *set)
1165 {
1166         struct obd_device *obd = class_exp2obd(exp);
1167         struct lov_obd *lov = &obd->u.lov;
1168         struct lov_tgt_desc *tgt;
1169         bool do_inactive = false, no_set = false;
1170         u32 i;
1171         int rc = 0;
1172         int err;
1173
1174         ENTRY;
1175
1176         if (set == NULL) {
1177                 no_set = true;
1178                 set = ptlrpc_prep_set();
1179                 if (!set)
1180                         RETURN(-ENOMEM);
1181         }
1182
1183         lov_tgts_getref(obd);
1184
1185         if (KEY_IS(KEY_CHECKSUM))
1186                 do_inactive = true;
1187
1188         for (i = 0; i < lov->desc.ld_tgt_count; i++) {
1189                 tgt = lov->lov_tgts[i];
1190
1191                 /* OST was disconnected */
1192                 if (tgt == NULL || tgt->ltd_exp == NULL)
1193                         continue;
1194
1195                 /* OST is inactive and we don't want inactive OSCs */
1196                 if (!tgt->ltd_active && !do_inactive)
1197                         continue;
1198
1199                 err = obd_set_info_async(env, tgt->ltd_exp, keylen, key,
1200                                          vallen, val, set);
1201
1202                 if (rc == 0)
1203                         rc = err;
1204         }
1205
1206         /* cycle through MDC target for Data-on-MDT */
1207         for (i = 0; i < LOV_MDC_TGT_MAX; i++) {
1208                 struct obd_device *mdc;
1209
1210                 mdc = lov->lov_mdc_tgts[i].lmtd_mdc;
1211                 if (mdc == NULL)
1212                         continue;
1213
1214                 err = obd_set_info_async(env, mdc->obd_self_export,
1215                                          keylen, key, vallen, val, set);
1216                 if (rc == 0)
1217                         rc = err;
1218         }
1219
1220         lov_tgts_putref(obd);
1221         if (no_set) {
1222                 err = ptlrpc_set_wait(env, set);
1223                 if (rc == 0)
1224                         rc = err;
1225                 ptlrpc_set_destroy(set);
1226         }
1227         RETURN(rc);
1228 }
1229
1230 void lov_stripe_lock(struct lov_stripe_md *md)
1231 __acquires(&md->lsm_lock)
1232 {
1233         LASSERT(md->lsm_lock_owner != current->pid);
1234         spin_lock(&md->lsm_lock);
1235         LASSERT(md->lsm_lock_owner == 0);
1236         md->lsm_lock_owner = current->pid;
1237 }
1238
1239 void lov_stripe_unlock(struct lov_stripe_md *md)
1240 __releases(&md->lsm_lock)
1241 {
1242         LASSERT(md->lsm_lock_owner == current->pid);
1243         md->lsm_lock_owner = 0;
1244         spin_unlock(&md->lsm_lock);
1245 }
1246
1247 static int lov_quotactl(struct obd_device *obd, struct obd_export *exp,
1248                         struct obd_quotactl *oqctl)
1249 {
1250         struct lov_obd *lov = &obd->u.lov;
1251         struct lov_tgt_desc *tgt;
1252         struct pool_desc *pool = NULL;
1253         __u64 curspace = 0;
1254         __u64 bhardlimit = 0;
1255         int i, rc = 0;
1256
1257         ENTRY;
1258         if (oqctl->qc_cmd != Q_GETOQUOTA &&
1259             oqctl->qc_cmd != LUSTRE_Q_SETQUOTA &&
1260             oqctl->qc_cmd != LUSTRE_Q_GETQUOTAPOOL) {
1261                 rc = -EFAULT;
1262                 CERROR("%s: bad quota opc %x for lov obd: rc = %d\n",
1263                        obd->obd_name, oqctl->qc_cmd, rc);
1264                 RETURN(rc);
1265         }
1266
1267         if (oqctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) {
1268                 pool = lov_pool_find(obd, oqctl->qc_poolname);
1269                 if (!pool)
1270                         RETURN(-ENOENT);
1271                 /* Set Q_GETOQUOTA back as targets report it's own
1272                  * usage and doesn't care about pools */
1273                 oqctl->qc_cmd = Q_GETOQUOTA;
1274         }
1275
1276         /* for lov tgt */
1277         lov_tgts_getref(obd);
1278         for (i = 0; i < lov->desc.ld_tgt_count; i++) {
1279                 int err;
1280
1281                 tgt = lov->lov_tgts[i];
1282
1283                 if (!tgt)
1284                         continue;
1285
1286                 if (pool &&
1287                     lu_tgt_check_index(tgt->ltd_index, &pool->pool_obds))
1288                         continue;
1289
1290                 if (!tgt->ltd_active || tgt->ltd_reap) {
1291                         if (oqctl->qc_cmd == Q_GETOQUOTA &&
1292                             lov->lov_tgts[i]->ltd_activate) {
1293                                 rc = -ENETDOWN;
1294                                 CERROR("%s: ost %d is inactive: rc = %d\n",
1295                                        obd->obd_name, i, rc);
1296                         } else {
1297                                 CDEBUG(D_HA, "ost %d is inactive\n", i);
1298                         }
1299                         continue;
1300                 }
1301
1302                 err = obd_quotactl(tgt->ltd_exp, oqctl);
1303                 if (err) {
1304                         if (tgt->ltd_active && !rc)
1305                                 rc = err;
1306                         continue;
1307                 }
1308
1309                 if (oqctl->qc_cmd == Q_GETOQUOTA) {
1310                         curspace += oqctl->qc_dqblk.dqb_curspace;
1311                         bhardlimit += oqctl->qc_dqblk.dqb_bhardlimit;
1312                 }
1313         }
1314         lov_tgts_putref(obd);
1315         if (pool)
1316                 lov_pool_putref(pool);
1317
1318         if (oqctl->qc_cmd == Q_GETOQUOTA) {
1319                 oqctl->qc_dqblk.dqb_curspace = curspace;
1320                 oqctl->qc_dqblk.dqb_bhardlimit = bhardlimit;
1321         }
1322         RETURN(rc);
1323 }
1324
1325 static const struct obd_ops lov_obd_ops = {
1326         .o_owner                = THIS_MODULE,
1327         .o_setup                = lov_setup,
1328         .o_cleanup              = lov_cleanup,
1329         .o_connect              = lov_connect,
1330         .o_disconnect           = lov_disconnect,
1331         .o_statfs               = lov_statfs,
1332         .o_iocontrol            = lov_iocontrol,
1333         .o_get_info             = lov_get_info,
1334         .o_set_info_async       = lov_set_info_async,
1335         .o_notify               = lov_notify,
1336         .o_pool_new             = lov_pool_new,
1337         .o_pool_rem             = lov_pool_remove,
1338         .o_pool_add             = lov_pool_add,
1339         .o_pool_del             = lov_pool_del,
1340         .o_quotactl             = lov_quotactl,
1341 };
1342
1343 struct kmem_cache *lov_oinfo_slab;
1344
1345 static int __init lov_init(void)
1346 {
1347         int rc;
1348         ENTRY;
1349
1350         /* print an address of _any_ initialized kernel symbol from this
1351          * module, to allow debugging with gdb that doesn't support data
1352          * symbols from modules.*/
1353         CDEBUG(D_INFO, "Lustre LOV module (%p).\n", &lov_caches);
1354
1355         rc = lu_kmem_init(lov_caches);
1356         if (rc)
1357                 return rc;
1358
1359         lov_oinfo_slab = kmem_cache_create("lov_oinfo",
1360                                            sizeof(struct lov_oinfo), 0,
1361                                            SLAB_HWCACHE_ALIGN, NULL);
1362         if (lov_oinfo_slab == NULL) {
1363                 lu_kmem_fini(lov_caches);
1364                 return -ENOMEM;
1365         }
1366
1367         rc = class_register_type(&lov_obd_ops, NULL, true,
1368                                  LUSTRE_LOV_NAME, &lov_device_type);
1369         if (rc) {
1370                 kmem_cache_destroy(lov_oinfo_slab);
1371                 lu_kmem_fini(lov_caches);
1372         }
1373
1374         RETURN(rc);
1375 }
1376
1377 static void __exit lov_exit(void)
1378 {
1379         class_unregister_type(LUSTRE_LOV_NAME);
1380         kmem_cache_destroy(lov_oinfo_slab);
1381         lu_kmem_fini(lov_caches);
1382 }
1383
1384 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
1385 MODULE_DESCRIPTION("Lustre Logical Object Volume");
1386 MODULE_VERSION(LUSTRE_VERSION_STRING);
1387 MODULE_LICENSE("GPL");
1388
1389 module_init(lov_init);
1390 module_exit(lov_exit);