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