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