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