Whamcloud - gitweb
LU-9791 obd: always call lprocfs_obd_setup
[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, 2016, 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         } 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->targets_proc_entry != NULL) {
184                 struct proc_dir_entry *osc_symlink;
185                 struct obd_device *osc_obd;
186
187                 osc_obd = lov->lov_tgts[index]->ltd_exp->exp_obd;
188
189                 LASSERT(osc_obd != NULL);
190                 LASSERT(osc_obd->obd_magic == OBD_DEVICE_MAGIC);
191                 LASSERT(osc_obd->obd_type->typ_name != NULL);
192
193                 osc_symlink = lprocfs_add_symlink(osc_obd->obd_name,
194                                                   lov->targets_proc_entry,
195                                                   "../../../%s/%s",
196                                                   osc_obd->obd_type->typ_name,
197                                                   osc_obd->obd_name);
198                 if (osc_symlink == NULL) {
199                         CERROR("cannot register LOV target "
200                                "/proc/fs/lustre/%s/%s/target_obds/%s\n",
201                                obd->obd_type->typ_name, obd->obd_name,
202                                osc_obd->obd_name);
203                 }
204         }
205         RETURN(0);
206 }
207
208 static int lov_connect(const struct lu_env *env,
209                        struct obd_export **exp, struct obd_device *obd,
210                        struct obd_uuid *cluuid, struct obd_connect_data *data,
211                        void *localdata)
212 {
213         struct lov_obd *lov = &obd->u.lov;
214         struct lov_tgt_desc *tgt;
215         struct lustre_handle conn;
216         int i, rc;
217         ENTRY;
218
219         CDEBUG(D_CONFIG, "connect #%d\n", lov->lov_connects);
220
221         rc = class_connect(&conn, obd, cluuid);
222         if (rc)
223                 RETURN(rc);
224
225         *exp = class_conn2export(&conn);
226
227         /* Why should there ever be more than 1 connect? */
228         lov->lov_connects++;
229         LASSERT(lov->lov_connects == 1);
230
231         memset(&lov->lov_ocd, 0, sizeof(lov->lov_ocd));
232         if (data)
233                 lov->lov_ocd = *data;
234
235         lov->targets_proc_entry = lprocfs_register("target_obds",
236                                                    obd->obd_proc_entry,
237                                                    NULL, NULL);
238         if (IS_ERR(lov->targets_proc_entry)) {
239                 CERROR("%s: cannot register "
240                        "/proc/fs/lustre/%s/%s/target_obds\n",
241                        obd->obd_name, obd->obd_type->typ_name, obd->obd_name);
242                 lov->targets_proc_entry = NULL;
243         }
244
245         obd_getref(obd);
246         for (i = 0; i < lov->desc.ld_tgt_count; i++) {
247                 tgt = lov->lov_tgts[i];
248                 if (!tgt || obd_uuid_empty(&tgt->ltd_uuid))
249                         continue;
250                 /* Flags will be lowest common denominator */
251                 rc = lov_connect_obd(obd, i, tgt->ltd_activate, &lov->lov_ocd);
252                 if (rc) {
253                         CERROR("%s: lov connect tgt %d failed: %d\n",
254                                obd->obd_name, i, rc);
255                         continue;
256                 }
257                 /* connect to administrative disabled ost */
258                 if (!lov->lov_tgts[i]->ltd_exp)
259                         continue;
260
261                 rc = lov_notify(obd, lov->lov_tgts[i]->ltd_exp->exp_obd,
262                                 OBD_NOTIFY_CONNECT);
263                 if (rc) {
264                         CERROR("%s error sending notify %d\n",
265                                obd->obd_name, rc);
266                 }
267         }
268         obd_putref(obd);
269
270         RETURN(0);
271 }
272
273 static int lov_disconnect_obd(struct obd_device *obd, struct lov_tgt_desc *tgt)
274 {
275         struct lov_obd *lov = &obd->u.lov;
276         struct obd_device *osc_obd;
277         int rc;
278         ENTRY;
279
280         osc_obd = class_exp2obd(tgt->ltd_exp);
281         CDEBUG(D_CONFIG, "%s: disconnecting target %s\n",
282                obd->obd_name, osc_obd->obd_name);
283
284         if (tgt->ltd_active) {
285                 tgt->ltd_active = 0;
286                 lov->desc.ld_active_tgt_count--;
287                 tgt->ltd_exp->exp_obd->obd_inactive = 1;
288         }
289
290         if (osc_obd) {
291                 /* Pass it on to our clients.
292                  * XXX This should be an argument to disconnect,
293                  * XXX not a back-door flag on the OBD.  Ah well.
294                  */
295                 osc_obd->obd_force = obd->obd_force;
296                 osc_obd->obd_fail = obd->obd_fail;
297                 osc_obd->obd_no_recov = obd->obd_no_recov;
298
299                 if (lov->targets_proc_entry != NULL)
300                         lprocfs_remove_proc_entry(osc_obd->obd_name,
301                                                   lov->targets_proc_entry);
302         }
303
304         obd_register_observer(osc_obd, NULL);
305
306         rc = obd_disconnect(tgt->ltd_exp);
307         if (rc) {
308                 CERROR("Target %s disconnect error %d\n",
309                        tgt->ltd_uuid.uuid, rc);
310                 rc = 0;
311         }
312
313         tgt->ltd_exp = NULL;
314         RETURN(0);
315 }
316
317 static int lov_disconnect(struct obd_export *exp)
318 {
319         struct obd_device *obd = class_exp2obd(exp);
320         struct lov_obd *lov = &obd->u.lov;
321         int i, rc;
322         ENTRY;
323
324         if (!lov->lov_tgts)
325                 goto out;
326
327         /* Only disconnect the underlying layers on the final disconnect. */
328         lov->lov_connects--;
329         if (lov->lov_connects != 0) {
330                 /* why should there be more than 1 connect? */
331                 CERROR("disconnect #%d\n", lov->lov_connects);
332                 goto out;
333         }
334
335         /* Let's hold another reference so lov_del_obd doesn't spin through
336            putref every time */
337         obd_getref(obd);
338
339         for (i = 0; i < lov->desc.ld_tgt_count; i++) {
340                 if (lov->lov_tgts[i] && lov->lov_tgts[i]->ltd_exp) {
341                         /* Disconnection is the last we know about an obd */
342                         lov_del_target(obd, i, NULL, lov->lov_tgts[i]->ltd_gen);
343                 }
344         }
345         obd_putref(obd);
346
347         if (lov->targets_proc_entry != NULL)
348                 lprocfs_remove(&lov->targets_proc_entry);
349
350 out:
351         rc = class_disconnect(exp); /* bz 9811 */
352         RETURN(rc);
353 }
354
355 /* Error codes:
356  *
357  *  -EINVAL  : UUID can't be found in the LOV's target list
358  *  -ENOTCONN: The UUID is found, but the target connection is bad (!)
359  *  -EBADF   : The UUID is found, but the OBD is the wrong type (!)
360  *  any >= 0 : is log target index
361  */
362 static int lov_set_osc_active(struct obd_device *obd, struct obd_uuid *uuid,
363                               enum obd_notify_event ev)
364 {
365         struct lov_obd *lov = &obd->u.lov;
366         struct lov_tgt_desc *tgt;
367         int index, activate, active;
368         ENTRY;
369
370         CDEBUG(D_INFO, "Searching in lov %p for uuid %s event(%d)\n",
371                lov, uuid->uuid, ev);
372
373         obd_getref(obd);
374         for (index = 0; index < lov->desc.ld_tgt_count; index++) {
375                 tgt = lov->lov_tgts[index];
376                 if (!tgt)
377                         continue;
378                 /*
379                  * LU-642, initially inactive OSC could miss the obd_connect,
380                  * we make up for it here.
381                  */
382                 if (ev == OBD_NOTIFY_ACTIVATE && tgt->ltd_exp == NULL &&
383                     obd_uuid_equals(uuid, &tgt->ltd_uuid)) {
384                         struct obd_uuid lov_osc_uuid = {"LOV_OSC_UUID"};
385
386                         obd_connect(NULL, &tgt->ltd_exp, tgt->ltd_obd,
387                                     &lov_osc_uuid, &lov->lov_ocd, NULL);
388                 }
389                 if (!tgt->ltd_exp)
390                         continue;
391
392                 CDEBUG(D_INFO, "lov idx %d is %s conn %#llx\n",
393                        index, obd_uuid2str(&tgt->ltd_uuid),
394                        tgt->ltd_exp->exp_handle.h_cookie);
395                 if (obd_uuid_equals(uuid, &tgt->ltd_uuid))
396                         break;
397         }
398
399         if (index == lov->desc.ld_tgt_count)
400                 GOTO(out, index = -EINVAL);
401
402         if (ev == OBD_NOTIFY_DEACTIVATE || ev == OBD_NOTIFY_ACTIVATE) {
403                 activate = (ev == OBD_NOTIFY_ACTIVATE) ? 1 : 0;
404
405                 if (lov->lov_tgts[index]->ltd_activate == activate) {
406                         CDEBUG(D_INFO, "OSC %s already %sactivate!\n",
407                                uuid->uuid, activate ? "" : "de");
408                 } else {
409                         lov->lov_tgts[index]->ltd_activate = activate;
410                         CDEBUG(D_CONFIG, "%sactivate OSC %s\n",
411                                activate ? "" : "de", obd_uuid2str(uuid));
412                 }
413
414         } else if (ev == OBD_NOTIFY_INACTIVE || ev == OBD_NOTIFY_ACTIVE) {
415                 active = (ev == OBD_NOTIFY_ACTIVE) ? 1 : 0;
416
417                 if (lov->lov_tgts[index]->ltd_active == active) {
418                         CDEBUG(D_INFO, "OSC %s already %sactive!\n",
419                                uuid->uuid, active ? "" : "in");
420                         GOTO(out, index);
421                 } else {
422                         CDEBUG(D_CONFIG, "Marking OSC %s %sactive\n",
423                                obd_uuid2str(uuid), active ? "" : "in");
424                 }
425
426                 lov->lov_tgts[index]->ltd_active = active;
427                 if (active) {
428                         lov->desc.ld_active_tgt_count++;
429                         lov->lov_tgts[index]->ltd_exp->exp_obd->obd_inactive = 0;
430                 } else {
431                         lov->desc.ld_active_tgt_count--;
432                         lov->lov_tgts[index]->ltd_exp->exp_obd->obd_inactive = 1;
433                 }
434         } else {
435                 CERROR("%s: unknown event %d for uuid %s\n", obd->obd_name,
436                        ev, uuid->uuid);
437         }
438
439  out:
440         obd_putref(obd);
441         RETURN(index);
442 }
443
444 static int lov_notify(struct obd_device *obd, struct obd_device *watched,
445                       enum obd_notify_event ev)
446 {
447         int rc = 0;
448         struct lov_obd *lov = &obd->u.lov;
449         ENTRY;
450
451         down_read(&lov->lov_notify_lock);
452         if (!lov->lov_connects)
453                 GOTO(out_notify_lock, rc = 0);
454
455         if (ev == OBD_NOTIFY_ACTIVE || ev == OBD_NOTIFY_INACTIVE ||
456             ev == OBD_NOTIFY_ACTIVATE || ev == OBD_NOTIFY_DEACTIVATE) {
457                 struct obd_uuid *uuid;
458
459                 LASSERT(watched);
460
461                 if (strcmp(watched->obd_type->typ_name, LUSTRE_OSC_NAME)) {
462                         CERROR("unexpected notification of %s %s\n",
463                                watched->obd_type->typ_name, watched->obd_name);
464                         GOTO(out_notify_lock, rc = -EINVAL);
465                 }
466
467                 uuid = &watched->u.cli.cl_target_uuid;
468
469                 /* Set OSC as active before notifying the observer, so the
470                  * observer can use the OSC normally.
471                  */
472                 rc = lov_set_osc_active(obd, uuid, ev);
473                 if (rc < 0) {
474                         CERROR("%s: event %d failed: rc = %d\n", obd->obd_name,
475                                ev, rc);
476                         GOTO(out_notify_lock, rc);
477                 }
478         }
479
480         /* Pass the notification up the chain. */
481         rc = obd_notify_observer(obd, watched, ev);
482
483 out_notify_lock:
484         up_read(&lov->lov_notify_lock);
485
486         RETURN(rc);
487 }
488
489 static int lov_add_target(struct obd_device *obd, struct obd_uuid *uuidp,
490                           __u32 index, int gen, int active)
491 {
492         struct lov_obd *lov = &obd->u.lov;
493         struct lov_tgt_desc *tgt;
494         struct obd_device *tgt_obd;
495         int rc;
496         ENTRY;
497
498         CDEBUG(D_CONFIG, "uuid:%s idx:%d gen:%d active:%d\n",
499                uuidp->uuid, index, gen, active);
500
501         if (gen <= 0) {
502                 CERROR("request to add OBD %s with invalid generation: %d\n",
503                        uuidp->uuid, gen);
504                 RETURN(-EINVAL);
505         }
506
507         tgt_obd = class_find_client_obd(uuidp, LUSTRE_OSC_NAME,
508                                         &obd->obd_uuid);
509         if (tgt_obd == NULL)
510                 RETURN(-EINVAL);
511
512         mutex_lock(&lov->lov_lock);
513
514         if ((index < lov->lov_tgt_size) && (lov->lov_tgts[index] != NULL)) {
515                 tgt = lov->lov_tgts[index];
516                 CERROR("UUID %s already assigned at LOV target index %d\n",
517                        obd_uuid2str(&tgt->ltd_uuid), index);
518                 mutex_unlock(&lov->lov_lock);
519                 RETURN(-EEXIST);
520         }
521
522         if (index >= lov->lov_tgt_size) {
523                 /* We need to reallocate the lov target array. */
524                 struct lov_tgt_desc **newtgts, **old = NULL;
525                 __u32 newsize, oldsize = 0;
526
527                 newsize = max(lov->lov_tgt_size, (__u32)2);
528                 while (newsize < index + 1)
529                         newsize = newsize << 1;
530                 OBD_ALLOC(newtgts, sizeof(*newtgts) * newsize);
531                 if (newtgts == NULL) {
532                         mutex_unlock(&lov->lov_lock);
533                         RETURN(-ENOMEM);
534                 }
535
536                 if (lov->lov_tgt_size) {
537                         memcpy(newtgts, lov->lov_tgts, sizeof(*newtgts) *
538                                lov->lov_tgt_size);
539                         old = lov->lov_tgts;
540                         oldsize = lov->lov_tgt_size;
541                 }
542
543                 lov->lov_tgts = newtgts;
544                 lov->lov_tgt_size = newsize;
545                 smp_rmb();
546                 if (old)
547                         OBD_FREE(old, sizeof(*old) * oldsize);
548
549                 CDEBUG(D_CONFIG, "tgts: %p size: %d\n",
550                        lov->lov_tgts, lov->lov_tgt_size);
551         }
552
553         OBD_ALLOC_PTR(tgt);
554         if (!tgt) {
555                 mutex_unlock(&lov->lov_lock);
556                 RETURN(-ENOMEM);
557         }
558
559         rc = lov_ost_pool_add(&lov->lov_packed, index, lov->lov_tgt_size);
560         if (rc) {
561                 mutex_unlock(&lov->lov_lock);
562                 OBD_FREE_PTR(tgt);
563                 RETURN(rc);
564         }
565
566         tgt->ltd_uuid = *uuidp;
567         tgt->ltd_obd = tgt_obd;
568         /* XXX - add a sanity check on the generation number. */
569         tgt->ltd_gen = gen;
570         tgt->ltd_index = index;
571         tgt->ltd_activate = active;
572         lov->lov_tgts[index] = tgt;
573         if (index >= lov->desc.ld_tgt_count)
574                 lov->desc.ld_tgt_count = index + 1;
575
576         mutex_unlock(&lov->lov_lock);
577
578         CDEBUG(D_CONFIG, "idx=%d ltd_gen=%d ld_tgt_count=%d\n",
579                 index, tgt->ltd_gen, lov->desc.ld_tgt_count);
580
581         if (lov->lov_connects == 0) {
582                 /* lov_connect hasn't been called yet. We'll do the
583                    lov_connect_obd on this target when that fn first runs,
584                    because we don't know the connect flags yet. */
585                 RETURN(0);
586         }
587
588         obd_getref(obd);
589
590         rc = lov_connect_obd(obd, index, active, &lov->lov_ocd);
591         if (rc)
592                 GOTO(out, rc);
593
594         /* connect to administrative disabled ost */
595         if (!tgt->ltd_exp)
596                 GOTO(out, rc = 0);
597
598         if (lov->lov_cache != NULL) {
599                 rc = obd_set_info_async(NULL, tgt->ltd_exp,
600                                 sizeof(KEY_CACHE_SET), KEY_CACHE_SET,
601                                 sizeof(struct cl_client_cache), lov->lov_cache,
602                                 NULL);
603                 if (rc < 0)
604                         GOTO(out, rc);
605         }
606
607         rc = lov_notify(obd, tgt->ltd_exp->exp_obd,
608                         active ? OBD_NOTIFY_CONNECT : OBD_NOTIFY_INACTIVE);
609
610 out:
611         if (rc) {
612                 CERROR("add failed (%d), deleting %s\n", rc,
613                        obd_uuid2str(&tgt->ltd_uuid));
614                 lov_del_target(obd, index, NULL, 0);
615         }
616         obd_putref(obd);
617         RETURN(rc);
618 }
619
620 /* Schedule a target for deletion */
621 int lov_del_target(struct obd_device *obd, __u32 index,
622                    struct obd_uuid *uuidp, int gen)
623 {
624         struct lov_obd *lov = &obd->u.lov;
625         int count = lov->desc.ld_tgt_count;
626         int rc = 0;
627         ENTRY;
628
629         if (index >= count) {
630                 CERROR("LOV target index %d >= number of LOV OBDs %d.\n",
631                        index, count);
632                 RETURN(-EINVAL);
633         }
634
635         /* to make sure there's no ongoing lov_notify() now */
636         down_write(&lov->lov_notify_lock);
637         obd_getref(obd);
638
639         if (!lov->lov_tgts[index]) {
640                 CERROR("LOV target at index %d is not setup.\n", index);
641                 GOTO(out, rc = -EINVAL);
642         }
643
644         if (uuidp && !obd_uuid_equals(uuidp, &lov->lov_tgts[index]->ltd_uuid)) {
645                 CERROR("LOV target UUID %s at index %d doesn't match %s.\n",
646                        lov_uuid2str(lov, index), index,
647                        obd_uuid2str(uuidp));
648                 GOTO(out, rc = -EINVAL);
649         }
650
651         CDEBUG(D_CONFIG, "uuid: %s idx: %d gen: %d exp: %p active: %d\n",
652                lov_uuid2str(lov, index), index,
653                lov->lov_tgts[index]->ltd_gen, lov->lov_tgts[index]->ltd_exp,
654                lov->lov_tgts[index]->ltd_active);
655
656         lov->lov_tgts[index]->ltd_reap = 1;
657         lov->lov_death_row++;
658         /* we really delete it from obd_putref */
659 out:
660         obd_putref(obd);
661         up_write(&lov->lov_notify_lock);
662
663         RETURN(rc);
664 }
665
666 static void __lov_del_obd(struct obd_device *obd, struct lov_tgt_desc *tgt)
667 {
668         struct obd_device *osc_obd;
669
670         LASSERT(tgt);
671         LASSERT(tgt->ltd_reap);
672
673         osc_obd = class_exp2obd(tgt->ltd_exp);
674
675         CDEBUG(D_CONFIG, "Removing tgt %s : %s\n",
676                tgt->ltd_uuid.uuid,
677                osc_obd ? osc_obd->obd_name : "<no obd>");
678
679         if (tgt->ltd_exp)
680                 lov_disconnect_obd(obd, tgt);
681
682         OBD_FREE_PTR(tgt);
683
684         /* Manual cleanup - no cleanup logs to clean up the osc's.  We must
685            do it ourselves. And we can't do it from lov_cleanup,
686            because we just lost our only reference to it. */
687         if (osc_obd)
688                 class_manual_cleanup(osc_obd);
689 }
690
691 void lov_fix_desc_stripe_size(__u64 *val)
692 {
693         if (*val < LOV_MIN_STRIPE_SIZE) {
694                 if (*val != 0)
695                         LCONSOLE_INFO("Increasing default stripe size to "
696                                       "minimum %u\n",
697                                       LOV_DESC_STRIPE_SIZE_DEFAULT);
698                 *val = LOV_DESC_STRIPE_SIZE_DEFAULT;
699         } else if (*val & (LOV_MIN_STRIPE_SIZE - 1)) {
700                 *val &= ~(LOV_MIN_STRIPE_SIZE - 1);
701                 LCONSOLE_WARN("Changing default stripe size to %llu (a "
702                               "multiple of %u)\n",
703                               *val, LOV_MIN_STRIPE_SIZE);
704         }
705 }
706
707 void lov_fix_desc_stripe_count(__u32 *val)
708 {
709         if (*val == 0)
710                 *val = 1;
711 }
712
713 void lov_fix_desc_pattern(__u32 *val)
714 {
715         /* from lov_setstripe */
716         if ((*val != 0) && (*val != LOV_PATTERN_RAID0)) {
717                 LCONSOLE_WARN("Unknown stripe pattern: %#x\n", *val);
718                 *val = 0;
719         }
720 }
721
722 void lov_fix_desc_qos_maxage(__u32 *val)
723 {
724         if (*val == 0)
725                 *val = LOV_DESC_QOS_MAXAGE_DEFAULT;
726 }
727
728 void lov_fix_desc(struct lov_desc *desc)
729 {
730         lov_fix_desc_stripe_size(&desc->ld_default_stripe_size);
731         lov_fix_desc_stripe_count(&desc->ld_default_stripe_count);
732         lov_fix_desc_pattern(&desc->ld_pattern);
733         lov_fix_desc_qos_maxage(&desc->ld_qos_maxage);
734 }
735
736 int lov_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
737 {
738         struct lov_desc *desc;
739         struct lov_obd *lov = &obd->u.lov;
740 #ifdef CONFIG_PROC_FS
741         struct obd_type *type;
742 #endif
743         int rc;
744         ENTRY;
745
746         if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
747                 CERROR("LOV setup requires a descriptor\n");
748                 RETURN(-EINVAL);
749         }
750
751         desc = (struct lov_desc *)lustre_cfg_buf(lcfg, 1);
752
753         if (sizeof(*desc) > LUSTRE_CFG_BUFLEN(lcfg, 1)) {
754                 CERROR("descriptor size wrong: %d > %d\n",
755                        (int)sizeof(*desc), LUSTRE_CFG_BUFLEN(lcfg, 1));
756                 RETURN(-EINVAL);
757         }
758
759         if (desc->ld_magic != LOV_DESC_MAGIC) {
760                 if (desc->ld_magic == __swab32(LOV_DESC_MAGIC)) {
761                             CDEBUG(D_OTHER, "%s: Swabbing lov desc %p\n",
762                                    obd->obd_name, desc);
763                             lustre_swab_lov_desc(desc);
764                 } else {
765                         CERROR("%s: Bad lov desc magic: %#x\n",
766                                obd->obd_name, desc->ld_magic);
767                         RETURN(-EINVAL);
768                 }
769         }
770
771         lov_fix_desc(desc);
772
773         desc->ld_active_tgt_count = 0;
774         lov->desc = *desc;
775         lov->lov_tgt_size = 0;
776
777         mutex_init(&lov->lov_lock);
778         atomic_set(&lov->lov_refcount, 0);
779         lov->lov_sp_me = LUSTRE_SP_CLI;
780
781         init_rwsem(&lov->lov_notify_lock);
782
783         lov->lov_pools_hash_body = cfs_hash_create("POOLS", HASH_POOLS_CUR_BITS,
784                                                    HASH_POOLS_MAX_BITS,
785                                                    HASH_POOLS_BKT_BITS, 0,
786                                                    CFS_HASH_MIN_THETA,
787                                                    CFS_HASH_MAX_THETA,
788                                                    &pool_hash_operations,
789                                                    CFS_HASH_DEFAULT);
790         INIT_LIST_HEAD(&lov->lov_pool_list);
791         lov->lov_pool_count = 0;
792         rc = lov_ost_pool_init(&lov->lov_packed, 0);
793         if (rc)
794                 GOTO(out, rc);
795
796 #ifdef CONFIG_PROC_FS
797         obd->obd_vars = lprocfs_lov_obd_vars;
798         /* If this is true then both client (lov) and server
799          * (lod) are on the same node. The lod layer if loaded
800          * first will register the lov proc directory. In that
801          * case obd->obd_type->typ_procroot will be not set.
802          * Instead we use type->typ_procsym as the parent.
803          */
804         type = class_search_type(LUSTRE_LOD_NAME);
805         if (type && type->typ_procsym) {
806                 obd->obd_proc_entry = lprocfs_register(obd->obd_name,
807                                                        type->typ_procsym,
808                                                        obd->obd_vars, obd);
809                 if (IS_ERR(obd->obd_proc_entry)) {
810                         rc = PTR_ERR(obd->obd_proc_entry);
811                         CERROR("error %d setting up lprocfs for %s\n", rc,
812                                obd->obd_name);
813                         obd->obd_proc_entry = NULL;
814                 }
815         }
816
817         rc = lprocfs_obd_setup(obd, false);
818         if (!rc) {
819                 rc = lprocfs_seq_create(obd->obd_proc_entry, "target_obd",
820                                         0444, &lov_proc_target_fops, obd);
821                 if (rc)
822                         CWARN("Error adding the target_obd file\n");
823
824                 lov->lov_pool_proc_entry = lprocfs_register("pools",
825                                                             obd->obd_proc_entry,
826                                                             NULL, NULL);
827                 if (IS_ERR(lov->lov_pool_proc_entry)) {
828                         rc = PTR_ERR(lov->lov_pool_proc_entry);
829                         CERROR("error %d setting up lprocfs for pools\n", rc);
830                         lov->lov_pool_proc_entry = NULL;
831                 }
832         }
833 #endif
834         RETURN(0);
835
836 out:
837         return rc;
838 }
839
840 static int lov_cleanup(struct obd_device *obd)
841 {
842         struct lov_obd *lov = &obd->u.lov;
843         struct list_head *pos, *tmp;
844         struct pool_desc *pool;
845         ENTRY;
846
847         list_for_each_safe(pos, tmp, &lov->lov_pool_list) {
848                 pool = list_entry(pos, struct pool_desc, pool_list);
849                 /* free pool structs */
850                 CDEBUG(D_INFO, "delete pool %p\n", pool);
851                 /* In the function below, .hs_keycmp resolves to
852                  * pool_hashkey_keycmp() */
853                 /* coverity[overrun-buffer-val] */
854                 lov_pool_del(obd, pool->pool_name);
855         }
856         cfs_hash_putref(lov->lov_pools_hash_body);
857         lov_ost_pool_free(&lov->lov_packed);
858
859         lprocfs_obd_cleanup(obd);
860         if (lov->lov_tgts) {
861                 int i;
862                 obd_getref(obd);
863                 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
864                         if (!lov->lov_tgts[i])
865                                 continue;
866
867                         /* Inactive targets may never have connected */
868                         if (lov->lov_tgts[i]->ltd_active ||
869                             atomic_read(&lov->lov_refcount))
870                                 /* We should never get here - these
871                                  * should have been removed in the
872                                  * disconnect. */
873                                 CERROR("%s: lov tgt %d not cleaned! "
874                                        "deathrow=%d, lovrc=%d\n",
875                                        obd->obd_name, i, lov->lov_death_row,
876                                        atomic_read(&lov->lov_refcount));
877                         lov_del_target(obd, i, NULL, 0);
878                 }
879                 obd_putref(obd);
880                 OBD_FREE(lov->lov_tgts, sizeof(*lov->lov_tgts) *
881                          lov->lov_tgt_size);
882                 lov->lov_tgt_size = 0;
883         }
884
885         if (lov->lov_cache != NULL) {
886                 cl_cache_decref(lov->lov_cache);
887                 lov->lov_cache = NULL;
888         }
889
890         RETURN(0);
891 }
892
893 int lov_process_config_base(struct obd_device *obd, struct lustre_cfg *lcfg,
894                             __u32 *indexp, int *genp)
895 {
896         struct obd_uuid obd_uuid;
897         int cmd;
898         int rc = 0;
899         ENTRY;
900
901         switch(cmd = lcfg->lcfg_command) {
902         case LCFG_LOV_ADD_OBD:
903         case LCFG_LOV_ADD_INA:
904         case LCFG_LOV_DEL_OBD: {
905                 __u32 index;
906                 int gen;
907                 /* lov_modify_tgts add  0:lov_mdsA  1:ost1_UUID  2:0  3:1 */
908                 if (LUSTRE_CFG_BUFLEN(lcfg, 1) > sizeof(obd_uuid.uuid))
909                         GOTO(out, rc = -EINVAL);
910
911                 obd_str2uuid(&obd_uuid,  lustre_cfg_buf(lcfg, 1));
912
913                 if (sscanf(lustre_cfg_buf(lcfg, 2), "%u", indexp) != 1)
914                         GOTO(out, rc = -EINVAL);
915                 if (sscanf(lustre_cfg_buf(lcfg, 3), "%d", genp) != 1)
916                         GOTO(out, rc = -EINVAL);
917                 index = *indexp;
918                 gen = *genp;
919                 if (cmd == LCFG_LOV_ADD_OBD)
920                         rc = lov_add_target(obd, &obd_uuid, index, gen, 1);
921                 else if (cmd == LCFG_LOV_ADD_INA)
922                         rc = lov_add_target(obd, &obd_uuid, index, gen, 0);
923                 else
924                         rc = lov_del_target(obd, index, &obd_uuid, gen);
925                 GOTO(out, rc);
926         }
927         case LCFG_PARAM: {
928                 struct lov_desc *desc = &(obd->u.lov.desc);
929
930                 if (!desc)
931                         GOTO(out, rc = -EINVAL);
932
933                 rc = class_process_proc_param(PARAM_LOV, obd->obd_vars,
934                                               lcfg, obd);
935                 if (rc > 0)
936                         rc = 0;
937                 GOTO(out, rc);
938         }
939         case LCFG_POOL_NEW:
940         case LCFG_POOL_ADD:
941         case LCFG_POOL_DEL:
942         case LCFG_POOL_REM:
943                 GOTO(out, rc);
944
945         default: {
946                 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
947                 GOTO(out, rc = -EINVAL);
948
949         }
950         }
951 out:
952         RETURN(rc);
953 }
954
955 static int
956 lov_statfs_interpret(struct ptlrpc_request_set *rqset, void *data, int rc)
957 {
958         struct lov_request_set *lovset = (struct lov_request_set *)data;
959         int err;
960         ENTRY;
961
962         if (rc)
963                 atomic_set(&lovset->set_completes, 0);
964
965         err = lov_fini_statfs_set(lovset);
966         RETURN(rc ? rc : err);
967 }
968
969 static int lov_statfs_async(struct obd_export *exp, struct obd_info *oinfo,
970                             __u64 max_age, struct ptlrpc_request_set *rqset)
971 {
972         struct obd_device      *obd = class_exp2obd(exp);
973         struct lov_request_set *set;
974         struct lov_request *req;
975         struct list_head *pos;
976         struct lov_obd *lov;
977         int rc = 0;
978         ENTRY;
979
980         LASSERT(oinfo != NULL);
981         LASSERT(oinfo->oi_osfs != NULL);
982
983         lov = &obd->u.lov;
984         rc = lov_prep_statfs_set(obd, oinfo, &set);
985         if (rc)
986                 RETURN(rc);
987
988         list_for_each(pos, &set->set_list) {
989                 req = list_entry(pos, struct lov_request, rq_link);
990                 rc = obd_statfs_async(lov->lov_tgts[req->rq_idx]->ltd_exp,
991                                       &req->rq_oi, max_age, rqset);
992                 if (rc)
993                         break;
994         }
995
996         if (rc || list_empty(&rqset->set_requests)) {
997                 int err;
998                 if (rc)
999                         atomic_set(&set->set_completes, 0);
1000                 err = lov_fini_statfs_set(set);
1001                 RETURN(rc ? rc : err);
1002         }
1003
1004         LASSERT(rqset->set_interpret == NULL);
1005         rqset->set_interpret = lov_statfs_interpret;
1006         rqset->set_arg = (void *)set;
1007         RETURN(0);
1008 }
1009
1010 static int lov_statfs(const struct lu_env *env, struct obd_export *exp,
1011                       struct obd_statfs *osfs, __u64 max_age, __u32 flags)
1012 {
1013         struct ptlrpc_request_set *set = NULL;
1014         struct obd_info oinfo = {
1015                 .oi_osfs = osfs,
1016                 .oi_flags = flags,
1017         };
1018         int rc = 0;
1019
1020         ENTRY;
1021
1022         /* for obdclass we forbid using obd_statfs_rqset, but prefer using async
1023          * statfs requests */
1024         set = ptlrpc_prep_set();
1025         if (set == NULL)
1026                 RETURN(-ENOMEM);
1027
1028         rc = lov_statfs_async(exp, &oinfo, max_age, set);
1029         if (rc == 0)
1030                 rc = ptlrpc_set_wait(set);
1031
1032         ptlrpc_set_destroy(set);
1033
1034         RETURN(rc);
1035 }
1036
1037 static int lov_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1038                          void *karg, void __user *uarg)
1039 {
1040         struct obd_device *obddev = class_exp2obd(exp);
1041         struct lov_obd *lov = &obddev->u.lov;
1042         int i = 0, rc = 0, count = lov->desc.ld_tgt_count;
1043         struct obd_uuid *uuidp;
1044         ENTRY;
1045
1046         switch (cmd) {
1047         case IOC_OBD_STATFS: {
1048                 struct obd_ioctl_data *data = karg;
1049                 struct obd_device *osc_obd;
1050                 struct obd_statfs stat_buf = {0};
1051                 __u32 index;
1052                 __u32 flags;
1053
1054                 memcpy(&index, data->ioc_inlbuf2, sizeof(index));
1055                 if ((index >= count))
1056                         RETURN(-ENODEV);
1057
1058                 if (!lov->lov_tgts[index])
1059                         /* Try again with the next index */
1060                         RETURN(-EAGAIN);
1061                 if (!lov->lov_tgts[index]->ltd_active)
1062                         RETURN(-ENODATA);
1063
1064                 osc_obd = class_exp2obd(lov->lov_tgts[index]->ltd_exp);
1065                 if (!osc_obd)
1066                         RETURN(-EINVAL);
1067
1068                 /* copy UUID */
1069                 if (copy_to_user(data->ioc_pbuf2, obd2cli_tgt(osc_obd),
1070                                  min_t(unsigned long, data->ioc_plen2,
1071                                        sizeof(struct obd_uuid))))
1072                         RETURN(-EFAULT);
1073
1074                 memcpy(&flags, data->ioc_inlbuf1, sizeof(flags));
1075                 flags = flags & LL_STATFS_NODELAY ? OBD_STATFS_NODELAY : 0;
1076
1077                 /* got statfs data */
1078                 rc = obd_statfs(NULL, lov->lov_tgts[index]->ltd_exp, &stat_buf,
1079                                 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
1080                                 flags);
1081                 if (rc)
1082                         RETURN(rc);
1083                 if (copy_to_user(data->ioc_pbuf1, &stat_buf,
1084                                  min_t(unsigned long, data->ioc_plen1,
1085                                        sizeof(struct obd_statfs))))
1086                         RETURN(-EFAULT);
1087                 break;
1088         }
1089         case OBD_IOC_LOV_GET_CONFIG: {
1090                 struct obd_ioctl_data *data;
1091                 struct lov_desc *desc;
1092                 char *buf = NULL;
1093                 __u32 *genp;
1094
1095                 len = 0;
1096                 if (obd_ioctl_getdata(&buf, &len, uarg))
1097                         RETURN(-EINVAL);
1098
1099                 data = (struct obd_ioctl_data *)buf;
1100
1101                 if (sizeof(*desc) > data->ioc_inllen1) {
1102                         OBD_FREE_LARGE(buf, len);
1103                         RETURN(-EINVAL);
1104                 }
1105
1106                 if (sizeof(uuidp->uuid) * count > data->ioc_inllen2) {
1107                         OBD_FREE_LARGE(buf, len);
1108                         RETURN(-EINVAL);
1109                 }
1110
1111                 if (sizeof(__u32) * count > data->ioc_inllen3) {
1112                         OBD_FREE_LARGE(buf, len);
1113                         RETURN(-EINVAL);
1114                 }
1115
1116                 desc = (struct lov_desc *)data->ioc_inlbuf1;
1117                 memcpy(desc, &(lov->desc), sizeof(*desc));
1118
1119                 uuidp = (struct obd_uuid *)data->ioc_inlbuf2;
1120                 genp = (__u32 *)data->ioc_inlbuf3;
1121                 /* the uuid will be empty for deleted OSTs */
1122                 for (i = 0; i < count; i++, uuidp++, genp++) {
1123                         if (!lov->lov_tgts[i])
1124                                 continue;
1125                         *uuidp = lov->lov_tgts[i]->ltd_uuid;
1126                         *genp = lov->lov_tgts[i]->ltd_gen;
1127                 }
1128
1129                 if (copy_to_user(uarg, buf, len))
1130                         rc = -EFAULT;
1131                 OBD_FREE_LARGE(buf, len);
1132                 break;
1133         }
1134         case OBD_IOC_QUOTACTL: {
1135                 struct if_quotactl *qctl = karg;
1136                 struct lov_tgt_desc *tgt = NULL;
1137                 struct obd_quotactl *oqctl;
1138
1139                 if (qctl->qc_valid == QC_OSTIDX) {
1140                         if (count <= qctl->qc_idx)
1141                                 RETURN(-EINVAL);
1142
1143                         tgt = lov->lov_tgts[qctl->qc_idx];
1144                         if (!tgt || !tgt->ltd_exp)
1145                                 RETURN(-EINVAL);
1146                 } else if (qctl->qc_valid == QC_UUID) {
1147                         for (i = 0; i < count; i++) {
1148                                 tgt = lov->lov_tgts[i];
1149                                 if (!tgt ||
1150                                     !obd_uuid_equals(&tgt->ltd_uuid,
1151                                                      &qctl->obd_uuid))
1152                                         continue;
1153
1154                                 if (tgt->ltd_exp == NULL)
1155                                         RETURN(-EINVAL);
1156
1157                                 break;
1158                         }
1159                 } else {
1160                         RETURN(-EINVAL);
1161                 }
1162
1163                 if (i >= count)
1164                         RETURN(-EAGAIN);
1165
1166                 LASSERT(tgt && tgt->ltd_exp);
1167                 OBD_ALLOC_PTR(oqctl);
1168                 if (!oqctl)
1169                         RETURN(-ENOMEM);
1170
1171                 QCTL_COPY(oqctl, qctl);
1172                 rc = obd_quotactl(tgt->ltd_exp, oqctl);
1173                 if (rc == 0) {
1174                         QCTL_COPY(qctl, oqctl);
1175                         qctl->qc_valid = QC_OSTIDX;
1176                         qctl->obd_uuid = tgt->ltd_uuid;
1177                 }
1178                 OBD_FREE_PTR(oqctl);
1179                 break;
1180         }
1181         default: {
1182                 int set = 0;
1183
1184                 if (count == 0)
1185                         RETURN(-ENOTTY);
1186
1187                 for (i = 0; i < count; i++) {
1188                         int err;
1189                         struct obd_device *osc_obd;
1190
1191                         /* OST was disconnected */
1192                         if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_exp)
1193                                 continue;
1194
1195                         /* ll_umount_begin() sets force flag but for lov, not
1196                          * osc. Let's pass it through */
1197                         osc_obd = class_exp2obd(lov->lov_tgts[i]->ltd_exp);
1198                         osc_obd->obd_force = obddev->obd_force;
1199                         err = obd_iocontrol(cmd, lov->lov_tgts[i]->ltd_exp,
1200                                             len, karg, uarg);
1201                         if (err) {
1202                                 if (lov->lov_tgts[i]->ltd_active) {
1203                                         CDEBUG(err == -ENOTTY ?
1204                                                D_IOCTL : D_WARNING,
1205                                                "iocontrol OSC %s on OST "
1206                                                "idx %d cmd %x: err = %d\n",
1207                                                lov_uuid2str(lov, i),
1208                                                i, cmd, err);
1209                                         if (!rc)
1210                                                 rc = err;
1211                                 }
1212                         } else {
1213                                 set = 1;
1214                         }
1215                 }
1216                 if (!set && !rc)
1217                         rc = -EIO;
1218         }
1219         }
1220
1221         RETURN(rc);
1222 }
1223
1224 static int lov_get_info(const struct lu_env *env, struct obd_export *exp,
1225                         __u32 keylen, void *key, __u32 *vallen, void *val)
1226 {
1227         struct obd_device *obddev = class_exp2obd(exp);
1228         struct lov_obd *lov = &obddev->u.lov;
1229         struct lov_desc *ld = &lov->desc;
1230         int rc = 0;
1231         ENTRY;
1232
1233         if (vallen == NULL || val == NULL)
1234                 RETURN(-EFAULT);
1235
1236         obd_getref(obddev);
1237
1238         if (KEY_IS(KEY_MAX_EASIZE)) {
1239                 u32 max_stripe_count = min_t(u32, ld->ld_active_tgt_count,
1240                                              LOV_MAX_STRIPE_COUNT);
1241
1242                 *((u32 *)val) = lov_mds_md_size(max_stripe_count, LOV_MAGIC_V3);
1243         } else if (KEY_IS(KEY_DEFAULT_EASIZE)) {
1244                 u32 def_stripe_count = min_t(u32, ld->ld_default_stripe_count,
1245                                              LOV_MAX_STRIPE_COUNT);
1246
1247                 *((u32 *)val) = lov_mds_md_size(def_stripe_count, LOV_MAGIC_V3);
1248         } else if (KEY_IS(KEY_TGT_COUNT)) {
1249                 *((int *)val) = lov->desc.ld_tgt_count;
1250         } else {
1251                 rc = -EINVAL;
1252         }
1253
1254         obd_putref(obddev);
1255
1256         RETURN(rc);
1257 }
1258
1259 static int lov_set_info_async(const struct lu_env *env, struct obd_export *exp,
1260                               __u32 keylen, void *key,
1261                               __u32 vallen, void *val,
1262                               struct ptlrpc_request_set *set)
1263 {
1264         struct obd_device *obddev = class_exp2obd(exp);
1265         struct lov_obd *lov = &obddev->u.lov;
1266         struct lov_tgt_desc *tgt;
1267         int do_inactive = 0;
1268         int no_set = 0;
1269         u32 count;
1270         u32 i;
1271         int rc = 0;
1272         int err;
1273         ENTRY;
1274
1275         if (set == NULL) {
1276                 no_set = 1;
1277                 set = ptlrpc_prep_set();
1278                 if (!set)
1279                         RETURN(-ENOMEM);
1280         }
1281
1282         obd_getref(obddev);
1283         count = lov->desc.ld_tgt_count;
1284
1285         if (KEY_IS(KEY_CHECKSUM)) {
1286                 do_inactive = 1;
1287         } else if (KEY_IS(KEY_CACHE_SET)) {
1288                 LASSERT(lov->lov_cache == NULL);
1289                 lov->lov_cache = val;
1290                 do_inactive = 1;
1291                 cl_cache_incref(lov->lov_cache);
1292         }
1293
1294         for (i = 0; i < count; i++) {
1295                 tgt = lov->lov_tgts[i];
1296
1297                 /* OST was disconnected */
1298                 if (!tgt || !tgt->ltd_exp)
1299                         continue;
1300
1301                 /* OST is inactive and we don't want inactive OSCs */
1302                 if (!tgt->ltd_active && !do_inactive)
1303                         continue;
1304
1305                 err = obd_set_info_async(env, tgt->ltd_exp, keylen, key,
1306                                          vallen, val, set);
1307                 if (!rc)
1308                         rc = err;
1309         }
1310
1311         obd_putref(obddev);
1312         if (no_set) {
1313                 err = ptlrpc_set_wait(set);
1314                 if (!rc)
1315                         rc = err;
1316                 ptlrpc_set_destroy(set);
1317         }
1318         RETURN(rc);
1319 }
1320
1321 void lov_stripe_lock(struct lov_stripe_md *md)
1322 __acquires(&md->lsm_lock)
1323 {
1324         LASSERT(md->lsm_lock_owner != current_pid());
1325         spin_lock(&md->lsm_lock);
1326         LASSERT(md->lsm_lock_owner == 0);
1327         md->lsm_lock_owner = current_pid();
1328 }
1329
1330 void lov_stripe_unlock(struct lov_stripe_md *md)
1331 __releases(&md->lsm_lock)
1332 {
1333         LASSERT(md->lsm_lock_owner == current_pid());
1334         md->lsm_lock_owner = 0;
1335         spin_unlock(&md->lsm_lock);
1336 }
1337
1338 static int lov_quotactl(struct obd_device *obd, struct obd_export *exp,
1339                         struct obd_quotactl *oqctl)
1340 {
1341         struct lov_obd      *lov = &obd->u.lov;
1342         struct lov_tgt_desc *tgt;
1343         __u64                curspace = 0;
1344         __u64                bhardlimit = 0;
1345         int                  i, rc = 0;
1346         ENTRY;
1347
1348         if (oqctl->qc_cmd != Q_GETOQUOTA &&
1349             oqctl->qc_cmd != LUSTRE_Q_SETQUOTA) {
1350                 CERROR("%s: bad quota opc %x for lov obd\n",
1351                        obd->obd_name, oqctl->qc_cmd);
1352                 RETURN(-EFAULT);
1353         }
1354
1355         /* for lov tgt */
1356         obd_getref(obd);
1357         for (i = 0; i < lov->desc.ld_tgt_count; i++) {
1358                 int err;
1359
1360                 tgt = lov->lov_tgts[i];
1361
1362                 if (!tgt)
1363                         continue;
1364
1365                 if (!tgt->ltd_active || tgt->ltd_reap) {
1366                         if (oqctl->qc_cmd == Q_GETOQUOTA &&
1367                             lov->lov_tgts[i]->ltd_activate) {
1368                                 rc = -ENETDOWN;
1369                                 CERROR("ost %d is inactive\n", i);
1370                         } else {
1371                                 CDEBUG(D_HA, "ost %d is inactive\n", i);
1372                         }
1373                         continue;
1374                 }
1375
1376                 err = obd_quotactl(tgt->ltd_exp, oqctl);
1377                 if (err) {
1378                         if (tgt->ltd_active && !rc)
1379                                 rc = err;
1380                         continue;
1381                 }
1382
1383                 if (oqctl->qc_cmd == Q_GETOQUOTA) {
1384                         curspace += oqctl->qc_dqblk.dqb_curspace;
1385                         bhardlimit += oqctl->qc_dqblk.dqb_bhardlimit;
1386                 }
1387         }
1388         obd_putref(obd);
1389
1390         if (oqctl->qc_cmd == Q_GETOQUOTA) {
1391                 oqctl->qc_dqblk.dqb_curspace = curspace;
1392                 oqctl->qc_dqblk.dqb_bhardlimit = bhardlimit;
1393         }
1394         RETURN(rc);
1395 }
1396
1397 static struct obd_ops lov_obd_ops = {
1398         .o_owner                = THIS_MODULE,
1399         .o_setup                = lov_setup,
1400         .o_cleanup              = lov_cleanup,
1401         .o_connect              = lov_connect,
1402         .o_disconnect           = lov_disconnect,
1403         .o_statfs               = lov_statfs,
1404         .o_statfs_async         = lov_statfs_async,
1405         .o_iocontrol            = lov_iocontrol,
1406         .o_get_info             = lov_get_info,
1407         .o_set_info_async       = lov_set_info_async,
1408         .o_notify               = lov_notify,
1409         .o_pool_new             = lov_pool_new,
1410         .o_pool_rem             = lov_pool_remove,
1411         .o_pool_add             = lov_pool_add,
1412         .o_pool_del             = lov_pool_del,
1413         .o_getref               = lov_getref,
1414         .o_putref               = lov_putref,
1415         .o_quotactl             = lov_quotactl,
1416 };
1417
1418 struct kmem_cache *lov_oinfo_slab;
1419
1420 static int __init lov_init(void)
1421 {
1422         bool enable_proc = true;
1423         struct obd_type *type;
1424         int rc;
1425         ENTRY;
1426
1427         /* print an address of _any_ initialized kernel symbol from this
1428          * module, to allow debugging with gdb that doesn't support data
1429          * symbols from modules.*/
1430         CDEBUG(D_INFO, "Lustre LOV module (%p).\n", &lov_caches);
1431
1432         rc = lu_kmem_init(lov_caches);
1433         if (rc)
1434                 return rc;
1435
1436         lov_oinfo_slab = kmem_cache_create("lov_oinfo",
1437                                            sizeof(struct lov_oinfo), 0,
1438                                            SLAB_HWCACHE_ALIGN, NULL);
1439         if (lov_oinfo_slab == NULL) {
1440                 lu_kmem_fini(lov_caches);
1441                 return -ENOMEM;
1442         }
1443
1444         type = class_search_type(LUSTRE_LOD_NAME);
1445         if (type != NULL && type->typ_procsym != NULL)
1446                 enable_proc = false;
1447
1448         rc = class_register_type(&lov_obd_ops, NULL, enable_proc, NULL,
1449                                  LUSTRE_LOV_NAME, &lov_device_type);
1450
1451         if (rc) {
1452                 kmem_cache_destroy(lov_oinfo_slab);
1453                 lu_kmem_fini(lov_caches);
1454         }
1455
1456         RETURN(rc);
1457 }
1458
1459 static void __exit lov_exit(void)
1460 {
1461         class_unregister_type(LUSTRE_LOV_NAME);
1462         kmem_cache_destroy(lov_oinfo_slab);
1463         lu_kmem_fini(lov_caches);
1464 }
1465
1466 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
1467 MODULE_DESCRIPTION("Lustre Logical Object Volume");
1468 MODULE_VERSION(LUSTRE_VERSION_STRING);
1469 MODULE_LICENSE("GPL");
1470
1471 module_init(lov_init);
1472 module_exit(lov_exit);