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