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