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