Whamcloud - gitweb
LU-17662 osd-zfs: Support for ZFS 2.2.3
[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_ioctl_old.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                         lu_tgt_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_notify(struct obd_device *obd, struct obd_device *watched,
111                       enum obd_notify_event ev);
112
113 static int lov_connect_osc(struct obd_device *obd, u32 index, int activate,
114                            struct obd_connect_data *data)
115 {
116         struct lov_obd *lov = &obd->u.lov;
117         struct obd_uuid *tgt_uuid;
118         struct obd_device *tgt_obd;
119         static struct obd_uuid lov_osc_uuid = { "LOV_OSC_UUID" };
120         struct obd_import *imp;
121         int rc;
122         ENTRY;
123
124         if (lov->lov_tgts[index] == NULL)
125                 RETURN(-EINVAL);
126
127         tgt_uuid = &lov->lov_tgts[index]->ltd_uuid;
128         tgt_obd = lov->lov_tgts[index]->ltd_obd;
129
130         if (!tgt_obd->obd_set_up) {
131                 rc = -EINVAL;
132                 CERROR("%s: target not set up: rc = %d\n",
133                        obd_uuid2str(tgt_uuid), rc);
134                 RETURN(rc);
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("%s: target register_observer error: rc = %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("%s: target connect error: rc = %d\n",
172                        obd_uuid2str(tgt_uuid), rc);
173                 obd_register_observer(tgt_obd, NULL);
174                 RETURN(-ENODEV);
175         }
176
177         lov->lov_tgts[index]->ltd_reap = 0;
178
179         CDEBUG(D_CONFIG, "Connected tgt idx %d %s (%s) %sactive\n", index,
180                obd_uuid2str(tgt_uuid), tgt_obd->obd_name, activate ? "":"in");
181
182         if (lov->lov_tgts_kobj) {
183                 /* Even if we failed, that's ok */
184                 rc = sysfs_create_link(lov->lov_tgts_kobj,
185                                        &tgt_obd->obd_kset.kobj,
186                                        tgt_obd->obd_name);
187                 if (rc) {
188                         CERROR("%s: can't register LOV target /sys/fs/lustre/%s/%s/target_obds/%s : rc = %d\n",
189                                obd->obd_name, obd->obd_type->typ_name,
190                                obd->obd_name,
191                                lov->lov_tgts[index]->ltd_exp->exp_obd->obd_name,
192                                rc);
193                 }
194         }
195         RETURN(0);
196 }
197
198 static int lov_connect(const struct lu_env *env, struct obd_export **exp,
199                        struct obd_device *obd, struct obd_uuid *cluuid,
200                        struct obd_connect_data *data, 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: rc = %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: rc = %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
290         obd_register_observer(osc_obd, NULL);
291
292         rc = obd_disconnect(tgt->ltd_exp);
293         if (rc) {
294                 CERROR("%s: Target %s disconnect error %d\n",
295                        obd->obd_name, tgt->ltd_uuid.uuid, rc);
296                 rc = 0;
297         }
298
299         tgt->ltd_exp = NULL;
300         RETURN(0);
301 }
302
303 static int lov_disconnect(struct obd_export *exp)
304 {
305         struct obd_device *obd = class_exp2obd(exp);
306         struct lov_obd *lov = &obd->u.lov;
307         u32 index;
308         int rc;
309
310         ENTRY;
311         if (!lov->lov_tgts)
312                 goto out;
313
314         /* Only disconnect the underlying layers on the final disconnect. */
315         if (lov->lov_connects == 0) {
316                 CWARN("%s: was disconnected already #%d\n",
317                       obd->obd_name, lov->lov_connects);
318                 RETURN(0);
319         }
320
321         lov->lov_connects--;
322         if (lov->lov_connects > 0) {
323                 /* why should there be more than 1 connect? */
324                 CWARN("%s: unexpected disconnect #%d\n",
325                       obd->obd_name, lov->lov_connects);
326                 goto out;
327         }
328
329         /* hold another ref so lov_del_obd() doesn't spin in putref each time */
330         lov_tgts_getref(obd);
331
332         for (index = 0; index < lov->desc.ld_tgt_count; index++) {
333                 if (lov->lov_tgts[index] && lov->lov_tgts[index]->ltd_exp) {
334                         /* Disconnection is the last we know about an OBD */
335                         lov_del_target(obd, index, NULL,
336                                        lov->lov_tgts[index]->ltd_gen);
337                 }
338         }
339         lov_tgts_putref(obd);
340
341 out:
342         rc = class_disconnect(exp); /* bz 9811 */
343         RETURN(rc);
344 }
345
346 /* Error codes:
347  *
348  *  -EINVAL  : UUID can't be found in the LOV's target list
349  *  -ENOTCONN: The UUID is found, but the target connection is bad (!)
350  *  -EBADF   : The UUID is found, but the OBD is the wrong type (!)
351  *  any >= 0 : is log target index
352  */
353 static int lov_set_osc_active(struct obd_device *obd, struct obd_uuid *uuid,
354                               enum obd_notify_event ev)
355 {
356         struct lov_obd *lov = &obd->u.lov;
357         struct lov_tgt_desc *tgt;
358         int index;
359         bool activate, active;
360         ENTRY;
361
362         CDEBUG(D_INFO, "Searching in lov %p for uuid %s event(%d)\n",
363                lov, uuid->uuid, ev);
364
365         lov_tgts_getref(obd);
366         for (index = 0; index < lov->desc.ld_tgt_count; index++) {
367                 tgt = lov->lov_tgts[index];
368                 if (tgt && obd_uuid_equals(uuid, &tgt->ltd_uuid))
369                         break;
370         }
371
372         if (index == lov->desc.ld_tgt_count)
373                 GOTO(out, index = -EINVAL);
374
375         if (ev == OBD_NOTIFY_DEACTIVATE || ev == OBD_NOTIFY_ACTIVATE) {
376                 activate = (ev == OBD_NOTIFY_ACTIVATE);
377
378                 /*
379                  * LU-642, initially inactive OSC could miss the obd_connect,
380                  * we make up for it here.
381                  */
382                 if (activate && !tgt->ltd_exp) {
383                         int rc;
384                         struct obd_uuid lov_osc_uuid = {"LOV_OSC_UUID"};
385
386                         rc = obd_connect(NULL, &tgt->ltd_exp, tgt->ltd_obd,
387                                          &lov_osc_uuid, &lov->lov_ocd,
388                                          lov->lov_cache);
389                         if (rc || !tgt->ltd_exp)
390                                 GOTO(out, index = rc);
391                 }
392
393                 if (lov->lov_tgts[index]->ltd_activate == activate) {
394                         CDEBUG(D_INFO, "OSC %s already %sactivate!\n",
395                                uuid->uuid, activate ? "" : "de");
396                 } else {
397                         lov->lov_tgts[index]->ltd_activate = activate;
398                         CDEBUG(D_CONFIG, "%sactivate OSC %s\n",
399                                activate ? "" : "de", obd_uuid2str(uuid));
400                 }
401         } else if (ev == OBD_NOTIFY_INACTIVE || ev == OBD_NOTIFY_ACTIVE) {
402                 active = (ev == OBD_NOTIFY_ACTIVE);
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                 }
409                 CDEBUG(D_CONFIG, "Marking OSC %s %sactive\n",
410                        obd_uuid2str(uuid), active ? "" : "in");
411
412                 lov->lov_tgts[index]->ltd_active = active;
413                 if (active) {
414                         lov->desc.ld_active_tgt_count++;
415                         lov->lov_tgts[index]->ltd_exp->exp_obd->obd_inactive = 0;
416                 } else {
417                         lov->desc.ld_active_tgt_count--;
418                         lov->lov_tgts[index]->ltd_exp->exp_obd->obd_inactive = 1;
419                 }
420         } else {
421                 CERROR("%s: unknown event %d for uuid %s\n", obd->obd_name,
422                        ev, uuid->uuid);
423         }
424
425         if (tgt->ltd_exp)
426                 CDEBUG(D_INFO, "%s: lov idx %d conn %llx\n", obd_uuid2str(uuid),
427                        index, tgt->ltd_exp->exp_handle.h_cookie);
428
429  out:
430         lov_tgts_putref(obd);
431         RETURN(index);
432 }
433
434 static int lov_notify(struct obd_device *obd, struct obd_device *watched,
435                       enum obd_notify_event ev)
436 {
437         int rc = 0;
438         struct lov_obd *lov = &obd->u.lov;
439         ENTRY;
440
441         down_read(&lov->lov_notify_lock);
442         if (!lov->lov_connects)
443                 GOTO(out_notify_lock, rc = 0);
444
445         if (ev == OBD_NOTIFY_ACTIVE || ev == OBD_NOTIFY_INACTIVE ||
446             ev == OBD_NOTIFY_ACTIVATE || ev == OBD_NOTIFY_DEACTIVATE) {
447                 struct obd_uuid *uuid;
448
449                 LASSERT(watched);
450
451                 if (strcmp(watched->obd_type->typ_name, LUSTRE_OSC_NAME)) {
452                         CERROR("unexpected notification of %s %s\n",
453                                watched->obd_type->typ_name, watched->obd_name);
454                         GOTO(out_notify_lock, rc = -EINVAL);
455                 }
456
457                 uuid = &watched->u.cli.cl_target_uuid;
458
459                 /* Set OSC as active before notifying the observer, so the
460                  * observer can use the OSC normally.
461                  */
462                 rc = lov_set_osc_active(obd, uuid, ev);
463                 if (rc < 0) {
464                         CERROR("%s: event %d failed: rc = %d\n", obd->obd_name,
465                                ev, rc);
466                         GOTO(out_notify_lock, rc);
467                 }
468         }
469
470         /* Pass the notification up the chain. */
471         rc = obd_notify_observer(obd, watched, ev);
472
473 out_notify_lock:
474         up_read(&lov->lov_notify_lock);
475
476         RETURN(rc);
477 }
478
479 static int lov_add_target(struct obd_device *obd, struct obd_uuid *uuidp,
480                           u32 index, int gen, int active)
481 {
482         struct lov_obd *lov = &obd->u.lov;
483         struct lov_tgt_desc *tgt;
484         struct obd_device *tgt_obd;
485         int rc;
486
487         ENTRY;
488         CDEBUG(D_CONFIG, "uuid:%s idx:%u gen:%d active:%d\n",
489                uuidp->uuid, index, gen, active);
490
491         if (gen <= 0) {
492                 CERROR("%s: request to add '%s' with invalid generation: %d\n",
493                        obd->obd_name, uuidp->uuid, gen);
494                 RETURN(-EINVAL);
495         }
496
497         tgt_obd = class_find_client_obd(uuidp, LUSTRE_OSC_NAME, &obd->obd_uuid);
498         if (tgt_obd == NULL)
499                 RETURN(-EINVAL);
500
501         mutex_lock(&lov->lov_lock);
502
503         if ((index < lov->lov_tgt_size) && (lov->lov_tgts[index] != NULL)) {
504                 tgt = lov->lov_tgts[index];
505                 rc = -EEXIST;
506                 CERROR("%s: UUID %s already assigned at index %d: rc = %d\n",
507                        obd->obd_name, obd_uuid2str(&tgt->ltd_uuid), index, rc);
508                 mutex_unlock(&lov->lov_lock);
509                 RETURN(rc);
510         }
511
512         if (index >= lov->lov_tgt_size) {
513                 /* We need to reallocate the lov target array. */
514                 struct lov_tgt_desc **newtgts, **old = NULL;
515                 __u32 newsize, oldsize = 0;
516
517                 newsize = max(lov->lov_tgt_size, 2U);
518                 while (newsize < index + 1)
519                         newsize = newsize << 1;
520                 OBD_ALLOC_PTR_ARRAY(newtgts, newsize);
521                 if (newtgts == NULL) {
522                         mutex_unlock(&lov->lov_lock);
523                         RETURN(-ENOMEM);
524                 }
525
526                 if (lov->lov_tgt_size) {
527                         memcpy(newtgts, lov->lov_tgts, sizeof(*newtgts) *
528                                lov->lov_tgt_size);
529                         old = lov->lov_tgts;
530                         oldsize = lov->lov_tgt_size;
531                 }
532
533                 lov->lov_tgts = newtgts;
534                 lov->lov_tgt_size = newsize;
535                 smp_rmb();
536                 if (old)
537                         OBD_FREE_PTR_ARRAY(old, oldsize);
538
539                 CDEBUG(D_CONFIG, "tgts: %p size: %d\n",
540                        lov->lov_tgts, lov->lov_tgt_size);
541         }
542
543         OBD_ALLOC_PTR(tgt);
544         if (!tgt) {
545                 mutex_unlock(&lov->lov_lock);
546                 RETURN(-ENOMEM);
547         }
548
549         rc = lu_tgt_pool_add(&lov->lov_packed, index, lov->lov_tgt_size);
550         if (rc) {
551                 mutex_unlock(&lov->lov_lock);
552                 OBD_FREE_PTR(tgt);
553                 RETURN(rc);
554         }
555
556         tgt->ltd_uuid = *uuidp;
557         tgt->ltd_obd = tgt_obd;
558         /* XXX - add a sanity check on the generation number. */
559         tgt->ltd_gen = gen;
560         tgt->ltd_index = index;
561         tgt->ltd_activate = active;
562         lov->lov_tgts[index] = tgt;
563         if (index >= lov->desc.ld_tgt_count)
564                 lov->desc.ld_tgt_count = index + 1;
565
566         mutex_unlock(&lov->lov_lock);
567
568         CDEBUG(D_CONFIG, "idx=%d ltd_gen=%d ld_tgt_count=%d\n",
569                index, tgt->ltd_gen, lov->desc.ld_tgt_count);
570
571         if (lov->lov_connects == 0) {
572                 /* lov_connect hasn't been called yet. We'll do the
573                  * lov_connect_osc on this target when that fn first runs,
574                  * because we don't know the connect flags yet. */
575                 RETURN(0);
576         }
577
578         lov_tgts_getref(obd);
579
580         rc = lov_connect_osc(obd, index, active, &lov->lov_ocd);
581         if (rc)
582                 GOTO(out, rc);
583
584         /* connect to administrative disabled ost */
585         if (!tgt->ltd_exp)
586                 GOTO(out, rc = 0);
587
588         rc = lov_notify(obd, tgt->ltd_exp->exp_obd,
589                         active ? OBD_NOTIFY_CONNECT : OBD_NOTIFY_INACTIVE);
590
591 out:
592         if (rc) {
593                 CERROR("%s: add failed, deleting %s: rc = %d\n",
594                        obd->obd_name, obd_uuid2str(&tgt->ltd_uuid), rc);
595                 lov_del_target(obd, index, NULL, 0);
596         }
597         lov_tgts_putref(obd);
598         RETURN(rc);
599 }
600
601 /* Schedule a target for deletion */
602 int lov_del_target(struct obd_device *obd, u32 index, struct obd_uuid *uuidp,
603                    int gen)
604 {
605         struct lov_obd *lov = &obd->u.lov;
606         int count = lov->desc.ld_tgt_count;
607         int rc = 0;
608         ENTRY;
609
610         if (index >= count) {
611                 CERROR("%s: LOV target index %d >= number of LOV OBDs %d: rc = %d\n",
612                        obd->obd_name, index, count, -EINVAL);
613                 RETURN(-EINVAL);
614         }
615
616         /* to make sure there's no ongoing lov_notify() now */
617         down_write(&lov->lov_notify_lock);
618         lov_tgts_getref(obd);
619
620         if (!lov->lov_tgts[index]) {
621                 CERROR("%s: LOV target at index %d is not setup: rc = %d\n",
622                        obd->obd_name, index, -EINVAL);
623                 GOTO(out, rc = -EINVAL);
624         }
625
626         if (uuidp && !obd_uuid_equals(uuidp, &lov->lov_tgts[index]->ltd_uuid)) {
627                 CERROR("%s: LOV target UUID %s at index %d doesn't match %s: rc = %d\n",
628                        obd->obd_name, lov_uuid2str(lov, index), index,
629                        obd_uuid2str(uuidp), -EINVAL);
630
631                 GOTO(out, rc = -EINVAL);
632         }
633
634         CDEBUG(D_CONFIG, "uuid: %s idx: %d gen: %d exp: %p active: %d\n",
635                lov_uuid2str(lov, index), index,
636                lov->lov_tgts[index]->ltd_gen, lov->lov_tgts[index]->ltd_exp,
637                lov->lov_tgts[index]->ltd_active);
638
639         lov->lov_tgts[index]->ltd_reap = 1;
640         lov->lov_death_row++;
641         /* we really delete it from lov_tgts_putref() */
642 out:
643         lov_tgts_putref(obd);
644         up_write(&lov->lov_notify_lock);
645
646         RETURN(rc);
647 }
648
649 static void __lov_del_obd(struct obd_device *obd, struct lov_tgt_desc *tgt)
650 {
651         struct obd_device *osc_obd;
652
653         LASSERT(tgt);
654         LASSERT(tgt->ltd_reap);
655
656         osc_obd = class_exp2obd(tgt->ltd_exp);
657
658         CDEBUG(D_CONFIG, "Removing tgt %s : %s\n", 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("%s: LOV setup requires a descriptor: rc = %d\n",
727                        obd->obd_name, -EINVAL);
728                 RETURN(-EINVAL);
729         }
730
731         desc = (struct lov_desc *)lustre_cfg_buf(lcfg, 1);
732
733         if (sizeof(*desc) > LUSTRE_CFG_BUFLEN(lcfg, 1)) {
734                 CERROR("%s: descriptor size wrong: %d > %d: rc = %d\n",
735                        obd->obd_name, (int)sizeof(*desc),
736                        LUSTRE_CFG_BUFLEN(lcfg, 1), -EINVAL);
737                 RETURN(-EINVAL);
738         }
739
740         if (desc->ld_magic != LOV_DESC_MAGIC) {
741                 if (desc->ld_magic == __swab32(LOV_DESC_MAGIC)) {
742                         CDEBUG(D_OTHER, "%s: Swabbing lov desc %p\n",
743                                obd->obd_name, desc);
744                         lustre_swab_lov_desc(desc);
745                 } else {
746                         CERROR("%s: Bad lov desc magic: %#x: rc = %d\n",
747                                obd->obd_name, desc->ld_magic, -EINVAL);
748                         RETURN(-EINVAL);
749                 }
750         }
751
752         lov_fix_desc(desc);
753
754         desc->ld_active_tgt_count = 0;
755         lov->desc = *desc;
756         lov->lov_tgt_size = 0;
757
758         mutex_init(&lov->lov_lock);
759         atomic_set(&lov->lov_refcount, 0);
760         lov->lov_sp_me = LUSTRE_SP_CLI;
761
762         init_rwsem(&lov->lov_notify_lock);
763
764         INIT_LIST_HEAD(&lov->lov_pool_list);
765         lov->lov_pool_count = 0;
766         rc = lov_pool_hash_init(&lov->lov_pools_hash_body);
767         if (rc)
768                 GOTO(out, rc);
769
770         rc = lu_tgt_pool_init(&lov->lov_packed, 0);
771         if (rc)
772                 GOTO(out, rc);
773
774         rc = lov_tunables_init(obd);
775         if (rc)
776                 GOTO(out, rc);
777
778         lov->lov_tgts_kobj = kobject_create_and_add("target_obds",
779                                                     &obd->obd_kset.kobj);
780 out:
781         return rc;
782 }
783
784 static int lov_cleanup(struct obd_device *obd)
785 {
786         struct lov_obd *lov = &obd->u.lov;
787         struct lov_pool_desc *pool, *tmp;
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_entry_safe(pool, tmp, &lov->lov_pool_list, pool_list) {
796                 /* free pool structs */
797                 CDEBUG(D_INFO, "delete pool %p\n", pool);
798                 /* In the function below, .hs_keycmp resolves to
799                  * pool_hashkey_keycmp() */
800                 lov_pool_del(obd, pool->pool_name);
801         }
802         lov_pool_hash_destroy(&lov->lov_pools_hash_body);
803         lu_tgt_pool_free(&lov->lov_packed);
804
805         lprocfs_obd_cleanup(obd);
806         if (lov->lov_tgts) {
807                 int i;
808
809                 lov_tgts_getref(obd);
810                 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
811                         if (!lov->lov_tgts[i])
812                                 continue;
813
814                         /* Inactive targets may never have connected */
815                         if (lov->lov_tgts[i]->ltd_active)
816                                 /* We should never get here - these
817                                  * should have been removed in the
818                                  * disconnect. */
819                                 CERROR("%s: lov tgt %d not cleaned! "
820                                        "deathrow=%d, lovrc=%d\n",
821                                        obd->obd_name, i, lov->lov_death_row,
822                                        atomic_read(&lov->lov_refcount));
823                         lov_del_target(obd, i, NULL, 0);
824                 }
825                 lov_tgts_putref(obd);
826                 OBD_FREE_PTR_ARRAY(lov->lov_tgts, lov->lov_tgt_size);
827                 lov->lov_tgt_size = 0;
828         }
829
830         if (lov->lov_cache != NULL) {
831                 cl_cache_decref(lov->lov_cache);
832                 lov->lov_cache = NULL;
833         }
834
835         RETURN(0);
836 }
837
838 int lov_process_config_base(struct obd_device *obd, struct lustre_cfg *lcfg,
839                             u32 *indexp, int *genp)
840 {
841         struct obd_uuid obd_uuid;
842         int cmd;
843         int rc = 0;
844
845         ENTRY;
846         switch (cmd = lcfg->lcfg_command) {
847         case LCFG_ADD_MDC:
848         case LCFG_DEL_MDC:
849                 break;
850         case LCFG_LOV_ADD_OBD:
851         case LCFG_LOV_ADD_INA:
852         case LCFG_LOV_DEL_OBD: {
853                 u32 index;
854                 int gen;
855
856                 /* lov_modify_tgts add  0:lov_mdsA  1:ost1_UUID  2:0  3:1 */
857                 if (LUSTRE_CFG_BUFLEN(lcfg, 1) > sizeof(obd_uuid.uuid))
858                         GOTO(out, rc = -EINVAL);
859
860                 obd_str2uuid(&obd_uuid,  lustre_cfg_buf(lcfg, 1));
861
862                 rc = kstrtou32(lustre_cfg_buf(lcfg, 2), 10, indexp);
863                 if (rc)
864                         GOTO(out, rc);
865                 rc = kstrtoint(lustre_cfg_buf(lcfg, 3), 10, genp);
866                 if (rc)
867                         GOTO(out, rc);
868                 index = *indexp;
869                 gen = *genp;
870                 if (cmd == LCFG_LOV_ADD_OBD)
871                         rc = lov_add_target(obd, &obd_uuid, index, gen, 1);
872                 else if (cmd == LCFG_LOV_ADD_INA)
873                         rc = lov_add_target(obd, &obd_uuid, index, gen, 0);
874                 else
875                         rc = lov_del_target(obd, index, &obd_uuid, gen);
876
877                 GOTO(out, rc);
878         }
879         case LCFG_PARAM: {
880                 struct lov_desc *desc = &(obd->u.lov.desc);
881                 ssize_t count;
882
883                 if (!desc)
884                         GOTO(out, rc = -EINVAL);
885
886                 count = class_modify_config(lcfg, PARAM_LOV,
887                                             &obd->obd_kset.kobj);
888                 GOTO(out, rc = count < 0 ? count : 0);
889         }
890         case LCFG_POOL_NEW:
891         case LCFG_POOL_ADD:
892         case LCFG_POOL_DEL:
893         case LCFG_POOL_REM:
894                 GOTO(out, rc);
895
896         default: {
897                 CERROR("%s: Unknown command: %d: rc = %d\n",
898                        obd->obd_name, lcfg->lcfg_command, -EINVAL);
899                 GOTO(out, rc = -EINVAL);
900         }
901         } /* end switch */
902 out:
903         RETURN(rc);
904 }
905
906 static int lov_statfs(const struct lu_env *env, struct obd_export *exp,
907                       struct obd_statfs *osfs, time64_t max_age, __u32 flags)
908 {
909         struct obd_device *obd = class_exp2obd(exp);
910         struct lov_obd *lov = &obd->u.lov;
911         struct obd_info oinfo = {
912                 .oi_osfs = osfs,
913                 .oi_flags = flags,
914         };
915         struct ptlrpc_request_set *rqset;
916         struct lov_request_set *set = NULL;
917         struct lov_request *req;
918         int rc = 0;
919         int rc2;
920
921         ENTRY;
922
923         rqset = ptlrpc_prep_set();
924         if (rqset == NULL)
925                 RETURN(-ENOMEM);
926
927         rc = lov_prep_statfs_set(obd, &oinfo, &set);
928         if (rc < 0)
929                 GOTO(out_rqset, rc);
930
931         list_for_each_entry(req, &set->set_list, rq_link) {
932                 rc = obd_statfs_async(lov->lov_tgts[req->rq_idx]->ltd_exp,
933                                       &req->rq_oi, max_age, rqset);
934                 if (rc < 0)
935                         GOTO(out_set, rc);
936         }
937
938         rc = ptlrpc_set_wait(env, rqset);
939
940 out_set:
941         if (rc < 0)
942                 atomic_set(&set->set_completes, 0);
943
944         rc2 = lov_fini_statfs_set(set);
945         if (rc == 0)
946                 rc = rc2;
947
948 out_rqset:
949         ptlrpc_set_destroy(rqset);
950
951         RETURN(rc);
952 }
953
954 static int lov_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
955                          void *karg, void __user *uarg)
956 {
957         struct obd_device *obd = class_exp2obd(exp);
958         struct lov_obd *lov = &obd->u.lov;
959         int i = 0, rc = 0, count = lov->desc.ld_tgt_count;
960
961         ENTRY;
962         CDEBUG(D_IOCTL, "%s: cmd=%x len=%u karg=%pK uarg=%pK\n",
963                exp->exp_obd->obd_name, cmd, len, karg, uarg);
964
965         /* exit early for unknown ioctl types. */
966         if (unlikely(_IOC_TYPE(cmd) != 'f' && !IOC_OSC_SET_ACTIVE_ALLOW(cmd)))
967                 RETURN(OBD_IOC_DEBUG(D_IOCTL, obd->obd_name, cmd, "unknown",
968                                      -ENOTTY));
969
970         /* can't do a generic karg == NULL check here, since it is too noisy and
971          * we need to return -ENOTTY for unsupported ioctls instead of -EINVAL.
972          */
973         switch (cmd) {
974         case IOC_OBD_STATFS: {
975                 struct obd_ioctl_data *data;
976                 struct obd_device *osc_obd;
977                 struct obd_statfs stat_buf = {0};
978                 struct obd_import *imp;
979                 __u32 index;
980                 __u32 flags;
981
982                 if (unlikely(karg == NULL))
983                         RETURN(OBD_IOC_ERROR(obd->obd_name, cmd, "karg=null",
984                                              -EINVAL));
985                 data = karg;
986
987                 memcpy(&index, data->ioc_inlbuf2, sizeof(index));
988                 if (index >= count)
989                         RETURN(-ENODEV);
990
991                 if (!lov->lov_tgts[index])
992                         /* Try again with the next index */
993                         RETURN(-EAGAIN);
994
995                 osc_obd = class_exp2obd(lov->lov_tgts[index]->ltd_exp);
996                 if (!osc_obd)
997                         RETURN(-EINVAL);
998
999                 imp = osc_obd->u.cli.cl_import;
1000                 if (!lov->lov_tgts[index]->ltd_active &&
1001                     imp->imp_state != LUSTRE_IMP_IDLE)
1002                         RETURN(-ENODATA);
1003
1004                 /* copy UUID */
1005                 if (copy_to_user(data->ioc_pbuf2, obd2cli_tgt(osc_obd),
1006                                  min_t(unsigned long, data->ioc_plen2,
1007                                        sizeof(struct obd_uuid))))
1008                         RETURN(-EFAULT);
1009
1010                 memcpy(&flags, data->ioc_inlbuf1, sizeof(flags));
1011                 flags = flags & LL_STATFS_NODELAY ? OBD_STATFS_NODELAY : 0;
1012
1013                 /* got statfs data */
1014                 rc = obd_statfs(NULL, lov->lov_tgts[index]->ltd_exp, &stat_buf,
1015                                 ktime_get_seconds() - OBD_STATFS_CACHE_SECONDS,
1016                                 flags);
1017                 if (rc)
1018                         RETURN(rc);
1019                 if (copy_to_user(data->ioc_pbuf1, &stat_buf,
1020                                  min_t(unsigned long, data->ioc_plen1,
1021                                        sizeof(struct obd_statfs))))
1022                         RETURN(-EFAULT);
1023                 break;
1024         }
1025         case OBD_IOC_QUOTACTL: {
1026                 struct if_quotactl *qctl;
1027                 struct lov_tgt_desc *tgt = NULL;
1028                 struct obd_quotactl *oqctl;
1029                 struct obd_import *imp;
1030
1031                 if (unlikely(karg == NULL))
1032                         RETURN(OBD_IOC_ERROR(obd->obd_name, cmd, "karg=null",
1033                                              -EINVAL));
1034                 qctl = karg;
1035
1036                 if (qctl->qc_valid == QC_OSTIDX) {
1037                         if (count <= qctl->qc_idx)
1038                                 RETURN(-EINVAL);
1039
1040                         tgt = lov->lov_tgts[qctl->qc_idx];
1041                         if (!tgt)
1042                                 RETURN(-ENODEV);
1043
1044                         if (!tgt->ltd_exp)
1045                                 RETURN(-EINVAL);
1046                 } else if (qctl->qc_valid == QC_UUID) {
1047                         for (i = 0; i < count; i++) {
1048                                 tgt = lov->lov_tgts[i];
1049                                 if (!tgt ||
1050                                     !obd_uuid_equals(&tgt->ltd_uuid,
1051                                                      &qctl->obd_uuid))
1052                                         continue;
1053
1054                                 if (tgt->ltd_exp == NULL)
1055                                         RETURN(-EINVAL);
1056
1057                                 break;
1058                         }
1059                 } else {
1060                         RETURN(-EINVAL);
1061                 }
1062
1063                 if (i >= count)
1064                         RETURN(-EAGAIN);
1065
1066                 LASSERT(tgt && tgt->ltd_exp);
1067                 imp = class_exp2cliimp(tgt->ltd_exp);
1068                 if (!tgt->ltd_active && imp->imp_state != LUSTRE_IMP_IDLE) {
1069                         qctl->qc_valid = QC_OSTIDX;
1070                         qctl->obd_uuid = tgt->ltd_uuid;
1071                         RETURN(-ENODATA);
1072                 }
1073
1074                 OBD_ALLOC_PTR(oqctl);
1075                 if (!oqctl)
1076                         RETURN(-ENOMEM);
1077
1078                 QCTL_COPY(oqctl, qctl);
1079                 rc = obd_quotactl(tgt->ltd_exp, oqctl);
1080                 if (rc == 0) {
1081                         QCTL_COPY_NO_PNAME(qctl, oqctl);
1082                         qctl->qc_valid = QC_OSTIDX;
1083                         qctl->obd_uuid = tgt->ltd_uuid;
1084                 }
1085                 OBD_FREE_PTR(oqctl);
1086                 break;
1087         }
1088         default: {
1089                 int set = 0;
1090
1091                 if (count == 0)
1092                         RETURN(-ENOTTY);
1093
1094                 for (i = 0; i < count; i++) {
1095                         int err;
1096                         struct obd_device *osc_obd;
1097
1098                         /* OST was disconnected */
1099                         if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_exp)
1100                                 continue;
1101
1102                         /* ll_umount_begin() sets force on lov, pass to osc */
1103                         osc_obd = class_exp2obd(lov->lov_tgts[i]->ltd_exp);
1104                         if (osc_obd)
1105                                 osc_obd->obd_force = obd->obd_force;
1106                         err = obd_iocontrol(cmd, lov->lov_tgts[i]->ltd_exp,
1107                                             len, karg, uarg);
1108                         if (err) {
1109                                 if (lov->lov_tgts[i]->ltd_active) {
1110                                         OBD_IOC_DEBUG(err == -ENOTTY ?
1111                                                       D_IOCTL : D_WARNING,
1112                                                       obd->obd_name, cmd,
1113                                                       lov_uuid2str(lov, i),
1114                                                       err);
1115                                         if (!rc)
1116                                                 rc = err;
1117
1118                                         if (err == -ENOTTY)
1119                                                 break;
1120                                 }
1121                         } else {
1122                                 set = 1;
1123                         }
1124                 }
1125                 if (!set && !rc)
1126                         rc = -EIO;
1127                 break;
1128         }
1129         }
1130
1131         RETURN(rc);
1132 }
1133
1134 static int lov_get_info(const struct lu_env *env, struct obd_export *exp,
1135                         __u32 keylen, void *key, __u32 *vallen, void *val)
1136 {
1137         struct obd_device *obd = class_exp2obd(exp);
1138         struct lov_obd *lov = &obd->u.lov;
1139         struct lov_desc *ld = &lov->desc;
1140         int rc = 0;
1141         ENTRY;
1142
1143         if (vallen == NULL || val == NULL)
1144                 RETURN(-EFAULT);
1145
1146         lov_tgts_getref(obd);
1147
1148         if (KEY_IS(KEY_MAX_EASIZE)) {
1149                 *((u32 *)val) = exp->exp_connect_data.ocd_max_easize;
1150         } else if (KEY_IS(KEY_DEFAULT_EASIZE)) {
1151                 u32 def_stripe_count = min_t(u32, ld->ld_default_stripe_count,
1152                                              LOV_MAX_STRIPE_COUNT);
1153
1154                 *((u32 *)val) = lov_mds_md_size(def_stripe_count, LOV_MAGIC_V3);
1155         } else if (KEY_IS(KEY_TGT_COUNT)) {
1156                 *((int *)val) = lov->desc.ld_tgt_count;
1157         } else {
1158                 rc = -EINVAL;
1159         }
1160
1161         lov_tgts_putref(obd);
1162
1163         RETURN(rc);
1164 }
1165
1166 static int lov_set_info_async(const struct lu_env *env, struct obd_export *exp,
1167                               __u32 keylen, void *key,
1168                               __u32 vallen, void *val,
1169                               struct ptlrpc_request_set *set)
1170 {
1171         struct obd_device *obd = class_exp2obd(exp);
1172         struct lov_obd *lov = &obd->u.lov;
1173         struct lov_tgt_desc *tgt;
1174         bool do_inactive = false, no_set = false;
1175         u32 i;
1176         int rc = 0;
1177         int err;
1178
1179         ENTRY;
1180
1181         if (set == NULL) {
1182                 no_set = true;
1183                 set = ptlrpc_prep_set();
1184                 if (!set)
1185                         RETURN(-ENOMEM);
1186         }
1187
1188         lov_tgts_getref(obd);
1189
1190         if (KEY_IS(KEY_CHECKSUM))
1191                 do_inactive = true;
1192
1193         for (i = 0; i < lov->desc.ld_tgt_count; i++) {
1194                 tgt = lov->lov_tgts[i];
1195
1196                 /* OST was disconnected */
1197                 if (tgt == NULL || tgt->ltd_exp == NULL)
1198                         continue;
1199
1200                 /* OST is inactive and we don't want inactive OSCs */
1201                 if (!tgt->ltd_active && !do_inactive)
1202                         continue;
1203
1204                 err = obd_set_info_async(env, tgt->ltd_exp, keylen, key,
1205                                          vallen, val, set);
1206
1207                 if (rc == 0)
1208                         rc = err;
1209         }
1210
1211         /* cycle through MDC target for Data-on-MDT */
1212         for (i = 0; i < LOV_MDC_TGT_MAX; i++) {
1213                 struct obd_device *mdc;
1214
1215                 mdc = lov->lov_mdc_tgts[i].lmtd_mdc;
1216                 if (mdc == NULL)
1217                         continue;
1218
1219                 err = obd_set_info_async(env, mdc->obd_self_export,
1220                                          keylen, key, vallen, val, set);
1221                 if (rc == 0)
1222                         rc = err;
1223         }
1224
1225         lov_tgts_putref(obd);
1226         if (no_set) {
1227                 err = ptlrpc_set_wait(env, set);
1228                 if (rc == 0)
1229                         rc = err;
1230                 ptlrpc_set_destroy(set);
1231         }
1232         RETURN(rc);
1233 }
1234
1235 void lov_stripe_lock(struct lov_stripe_md *md)
1236 __acquires(&md->lsm_lock)
1237 {
1238         LASSERT(md->lsm_lock_owner != current->pid);
1239         spin_lock(&md->lsm_lock);
1240         LASSERT(md->lsm_lock_owner == 0);
1241         md->lsm_lock_owner = current->pid;
1242 }
1243
1244 void lov_stripe_unlock(struct lov_stripe_md *md)
1245 __releases(&md->lsm_lock)
1246 {
1247         LASSERT(md->lsm_lock_owner == current->pid);
1248         md->lsm_lock_owner = 0;
1249         spin_unlock(&md->lsm_lock);
1250 }
1251
1252 static int lov_quotactl(struct obd_device *obd, struct obd_export *exp,
1253                         struct obd_quotactl *oqctl)
1254 {
1255         struct lov_obd *lov = &obd->u.lov;
1256         struct lov_tgt_desc *tgt;
1257         struct lov_pool_desc *pool = NULL;
1258         struct list_head *lst = NULL;
1259         __u64 curspace = 0;
1260         __u64 bhardlimit = 0;
1261         int i, rc = 0;
1262
1263         ENTRY;
1264         if (oqctl->qc_cmd != Q_GETOQUOTA &&
1265             oqctl->qc_cmd != LUSTRE_Q_SETQUOTA &&
1266             oqctl->qc_cmd != LUSTRE_Q_GETQUOTAPOOL &&
1267             oqctl->qc_cmd != LUSTRE_Q_ITEROQUOTA) {
1268                 rc = -EFAULT;
1269                 CERROR("%s: bad quota opc %x for lov obd: rc = %d\n",
1270                        obd->obd_name, oqctl->qc_cmd, rc);
1271                 RETURN(rc);
1272         }
1273
1274         if (oqctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) {
1275                 pool = lov_pool_find(obd, oqctl->qc_poolname);
1276                 if (!pool)
1277                         RETURN(-ENOENT);
1278                 /* Set Q_GETOQUOTA back as targets report it's own
1279                  * usage and doesn't care about pools */
1280                 oqctl->qc_cmd = Q_GETOQUOTA;
1281         }
1282
1283         if (oqctl->qc_cmd == LUSTRE_Q_ITEROQUOTA)
1284                 lst = (struct list_head *)oqctl->qc_iter_list;
1285
1286         /* for lov tgt */
1287         lov_tgts_getref(obd);
1288         for (i = 0; i < lov->desc.ld_tgt_count; i++) {
1289                 int err;
1290
1291                 tgt = lov->lov_tgts[i];
1292
1293                 if (!tgt)
1294                         continue;
1295
1296                 if (pool &&
1297                     lu_tgt_check_index(tgt->ltd_index, &pool->pool_obds))
1298                         continue;
1299
1300                 if (!tgt->ltd_active || tgt->ltd_reap) {
1301                         if (oqctl->qc_cmd == Q_GETOQUOTA &&
1302                             lov->lov_tgts[i]->ltd_activate) {
1303                                 rc = -ENETDOWN;
1304                                 CERROR("%s: ost %d is inactive: rc = %d\n",
1305                                        obd->obd_name, i, rc);
1306                         } else {
1307                                 CDEBUG(D_HA, "ost %d is inactive\n", i);
1308                         }
1309                         continue;
1310                 }
1311
1312                 if (oqctl->qc_cmd == LUSTRE_Q_ITEROQUOTA)
1313                         err = obd_quota_iter(tgt->ltd_exp, oqctl, lst);
1314                 else
1315                         err = obd_quotactl(tgt->ltd_exp, oqctl);
1316
1317                 if (err) {
1318                         if (tgt->ltd_active && !rc)
1319                                 rc = err;
1320                         continue;
1321                 }
1322
1323                 if (oqctl->qc_cmd == Q_GETOQUOTA) {
1324                         curspace += oqctl->qc_dqblk.dqb_curspace;
1325                         bhardlimit += oqctl->qc_dqblk.dqb_bhardlimit;
1326                 }
1327         }
1328         lov_tgts_putref(obd);
1329         if (pool)
1330                 lov_pool_putref(pool);
1331
1332         if (oqctl->qc_cmd == Q_GETOQUOTA) {
1333                 oqctl->qc_dqblk.dqb_curspace = curspace;
1334                 oqctl->qc_dqblk.dqb_bhardlimit = bhardlimit;
1335         }
1336
1337         RETURN(rc);
1338 }
1339
1340 static const struct obd_ops lov_obd_ops = {
1341         .o_owner                = THIS_MODULE,
1342         .o_setup                = lov_setup,
1343         .o_cleanup              = lov_cleanup,
1344         .o_connect              = lov_connect,
1345         .o_disconnect           = lov_disconnect,
1346         .o_statfs               = lov_statfs,
1347         .o_iocontrol            = lov_iocontrol,
1348         .o_get_info             = lov_get_info,
1349         .o_set_info_async       = lov_set_info_async,
1350         .o_notify               = lov_notify,
1351         .o_pool_new             = lov_pool_new,
1352         .o_pool_rem             = lov_pool_remove,
1353         .o_pool_add             = lov_pool_add,
1354         .o_pool_del             = lov_pool_del,
1355         .o_quotactl             = lov_quotactl,
1356 };
1357
1358 struct kmem_cache *lov_oinfo_slab;
1359
1360 static int __init lov_init(void)
1361 {
1362         int rc;
1363         ENTRY;
1364
1365         /* print an address of _any_ initialized kernel symbol from this
1366          * module, to allow debugging with gdb that doesn't support data
1367          * symbols from modules.*/
1368         CDEBUG(D_INFO, "Lustre LOV module (%p).\n", &lov_caches);
1369
1370         rc = libcfs_setup();
1371         if (rc)
1372                 return rc;
1373
1374         rc = lu_kmem_init(lov_caches);
1375         if (rc)
1376                 return rc;
1377
1378         lov_oinfo_slab = kmem_cache_create("lov_oinfo",
1379                                            sizeof(struct lov_oinfo), 0,
1380                                            SLAB_HWCACHE_ALIGN, NULL);
1381         if (lov_oinfo_slab == NULL) {
1382                 lu_kmem_fini(lov_caches);
1383                 return -ENOMEM;
1384         }
1385
1386         rc = class_register_type(&lov_obd_ops, NULL, true,
1387                                  LUSTRE_LOV_NAME, &lov_device_type);
1388         if (rc) {
1389                 kmem_cache_destroy(lov_oinfo_slab);
1390                 lu_kmem_fini(lov_caches);
1391         }
1392
1393         RETURN(rc);
1394 }
1395
1396 static void __exit lov_exit(void)
1397 {
1398         class_unregister_type(LUSTRE_LOV_NAME);
1399         kmem_cache_destroy(lov_oinfo_slab);
1400         lu_kmem_fini(lov_caches);
1401 }
1402
1403 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
1404 MODULE_DESCRIPTION("Lustre Logical Object Volume");
1405 MODULE_VERSION(LUSTRE_VERSION_STRING);
1406 MODULE_LICENSE("GPL");
1407
1408 module_init(lov_init);
1409 module_exit(lov_exit);