Whamcloud - gitweb
LU-6635 lfsck: block replacing the OST-object for test
[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, 2015, 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 <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, void *data);
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, (void *)&i);
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, void *data)
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                 up_read(&lov->lov_notify_lock);
456                 RETURN(rc);
457         }
458
459         if (ev == OBD_NOTIFY_ACTIVE || ev == OBD_NOTIFY_INACTIVE ||
460             ev == OBD_NOTIFY_ACTIVATE || ev == OBD_NOTIFY_DEACTIVATE) {
461                 struct obd_uuid *uuid;
462
463                 LASSERT(watched);
464
465                 if (strcmp(watched->obd_type->typ_name, LUSTRE_OSC_NAME)) {
466                         up_read(&lov->lov_notify_lock);
467                         CERROR("unexpected notification of %s %s!\n",
468                                watched->obd_type->typ_name,
469                                watched->obd_name);
470                         RETURN(-EINVAL);
471                 }
472                 uuid = &watched->u.cli.cl_target_uuid;
473
474                 /* Set OSC as active before notifying the observer, so the
475                  * observer can use the OSC normally.
476                  */
477                 rc = lov_set_osc_active(obd, uuid, ev);
478                 if (rc < 0) {
479                         up_read(&lov->lov_notify_lock);
480                         CERROR("event(%d) of %s failed: %d\n", ev,
481                                obd_uuid2str(uuid), rc);
482                         RETURN(rc);
483                 }
484                 /* active event should be pass lov target index as data */
485                 data = &rc;
486         }
487
488         /* Pass the notification up the chain. */
489         if (watched) {
490                 rc = obd_notify_observer(obd, watched, ev, data);
491         } else {
492                 /* NULL watched means all osc's in the lov (only for syncs) */
493                 /* sync event should be send lov idx as data */
494                 struct lov_obd *lov = &obd->u.lov;
495                 int i, is_sync;
496
497                 data = &i;
498                 is_sync = (ev == OBD_NOTIFY_SYNC) ||
499                           (ev == OBD_NOTIFY_SYNC_NONBLOCK);
500
501                 obd_getref(obd);
502                 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
503                         if (!lov->lov_tgts[i])
504                                 continue;
505
506                         /* don't send sync event if target not
507                          * connected/activated */
508                         if (is_sync &&  !lov->lov_tgts[i]->ltd_active)
509                                 continue;
510
511                         rc = obd_notify_observer(obd, lov->lov_tgts[i]->ltd_obd,
512                                                  ev, data);
513                         if (rc) {
514                                 CERROR("%s: notify %s of %s failed %d\n",
515                                        obd->obd_name,
516                                        obd->obd_observer->obd_name,
517                                        lov->lov_tgts[i]->ltd_obd->obd_name,
518                                        rc);
519                         }
520                 }
521                 obd_putref(obd);
522         }
523
524         up_read(&lov->lov_notify_lock);
525         RETURN(rc);
526 }
527
528 static int lov_add_target(struct obd_device *obd, struct obd_uuid *uuidp,
529                           __u32 index, int gen, int active)
530 {
531         struct lov_obd *lov = &obd->u.lov;
532         struct lov_tgt_desc *tgt;
533         struct obd_device *tgt_obd;
534         int rc;
535         ENTRY;
536
537         CDEBUG(D_CONFIG, "uuid:%s idx:%d gen:%d active:%d\n",
538                uuidp->uuid, index, gen, active);
539
540         if (gen <= 0) {
541                 CERROR("request to add OBD %s with invalid generation: %d\n",
542                        uuidp->uuid, gen);
543                 RETURN(-EINVAL);
544         }
545
546         tgt_obd = class_find_client_obd(uuidp, LUSTRE_OSC_NAME,
547                                         &obd->obd_uuid);
548         if (tgt_obd == NULL)
549                 RETURN(-EINVAL);
550
551         mutex_lock(&lov->lov_lock);
552
553         if ((index < lov->lov_tgt_size) && (lov->lov_tgts[index] != NULL)) {
554                 tgt = lov->lov_tgts[index];
555                 CERROR("UUID %s already assigned at LOV target index %d\n",
556                        obd_uuid2str(&tgt->ltd_uuid), index);
557                 mutex_unlock(&lov->lov_lock);
558                 RETURN(-EEXIST);
559         }
560
561         if (index >= lov->lov_tgt_size) {
562                 /* We need to reallocate the lov target array. */
563                 struct lov_tgt_desc **newtgts, **old = NULL;
564                 __u32 newsize, oldsize = 0;
565
566                 newsize = max(lov->lov_tgt_size, (__u32)2);
567                 while (newsize < index + 1)
568                         newsize = newsize << 1;
569                 OBD_ALLOC(newtgts, sizeof(*newtgts) * newsize);
570                 if (newtgts == NULL) {
571                         mutex_unlock(&lov->lov_lock);
572                         RETURN(-ENOMEM);
573                 }
574
575                 if (lov->lov_tgt_size) {
576                         memcpy(newtgts, lov->lov_tgts, sizeof(*newtgts) *
577                                lov->lov_tgt_size);
578                         old = lov->lov_tgts;
579                         oldsize = lov->lov_tgt_size;
580                 }
581
582                 lov->lov_tgts = newtgts;
583                 lov->lov_tgt_size = newsize;
584                 smp_rmb();
585                 if (old)
586                         OBD_FREE(old, sizeof(*old) * oldsize);
587
588                 CDEBUG(D_CONFIG, "tgts: %p size: %d\n",
589                        lov->lov_tgts, lov->lov_tgt_size);
590         }
591
592         OBD_ALLOC_PTR(tgt);
593         if (!tgt) {
594                 mutex_unlock(&lov->lov_lock);
595                 RETURN(-ENOMEM);
596         }
597
598         rc = lov_ost_pool_add(&lov->lov_packed, index, lov->lov_tgt_size);
599         if (rc) {
600                 mutex_unlock(&lov->lov_lock);
601                 OBD_FREE_PTR(tgt);
602                 RETURN(rc);
603         }
604
605         tgt->ltd_uuid = *uuidp;
606         tgt->ltd_obd = tgt_obd;
607         /* XXX - add a sanity check on the generation number. */
608         tgt->ltd_gen = gen;
609         tgt->ltd_index = index;
610         tgt->ltd_activate = active;
611         lov->lov_tgts[index] = tgt;
612         if (index >= lov->desc.ld_tgt_count)
613                 lov->desc.ld_tgt_count = index + 1;
614
615         mutex_unlock(&lov->lov_lock);
616
617         CDEBUG(D_CONFIG, "idx=%d ltd_gen=%d ld_tgt_count=%d\n",
618                 index, tgt->ltd_gen, lov->desc.ld_tgt_count);
619
620         rc = obd_notify(obd, tgt_obd, OBD_NOTIFY_CREATE, &index);
621
622         if (lov->lov_connects == 0) {
623                 /* lov_connect hasn't been called yet. We'll do the
624                    lov_connect_obd on this target when that fn first runs,
625                    because we don't know the connect flags yet. */
626                 RETURN(0);
627         }
628
629         obd_getref(obd);
630
631         rc = lov_connect_obd(obd, index, active, &lov->lov_ocd);
632         if (rc)
633                 GOTO(out, rc);
634
635         /* connect to administrative disabled ost */
636         if (!tgt->ltd_exp)
637                 GOTO(out, rc = 0);
638
639         if (lov->lov_cache != NULL) {
640                 rc = obd_set_info_async(NULL, tgt->ltd_exp,
641                                 sizeof(KEY_CACHE_SET), KEY_CACHE_SET,
642                                 sizeof(struct cl_client_cache), lov->lov_cache,
643                                 NULL);
644                 if (rc < 0)
645                         GOTO(out, rc);
646         }
647
648         rc = lov_notify(obd, tgt->ltd_exp->exp_obd,
649                         active ? OBD_NOTIFY_CONNECT : OBD_NOTIFY_INACTIVE,
650                         (void *)&index);
651
652 out:
653         if (rc) {
654                 CERROR("add failed (%d), deleting %s\n", rc,
655                        obd_uuid2str(&tgt->ltd_uuid));
656                 lov_del_target(obd, index, NULL, 0);
657         }
658         obd_putref(obd);
659         RETURN(rc);
660 }
661
662 /* Schedule a target for deletion */
663 int lov_del_target(struct obd_device *obd, __u32 index,
664                    struct obd_uuid *uuidp, int gen)
665 {
666         struct lov_obd *lov = &obd->u.lov;
667         int count = lov->desc.ld_tgt_count;
668         int rc = 0;
669         ENTRY;
670
671         if (index >= count) {
672                 CERROR("LOV target index %d >= number of LOV OBDs %d.\n",
673                        index, count);
674                 RETURN(-EINVAL);
675         }
676
677         /* to make sure there's no ongoing lov_notify() now */
678         down_write(&lov->lov_notify_lock);
679         obd_getref(obd);
680
681         if (!lov->lov_tgts[index]) {
682                 CERROR("LOV target at index %d is not setup.\n", index);
683                 GOTO(out, rc = -EINVAL);
684         }
685
686         if (uuidp && !obd_uuid_equals(uuidp, &lov->lov_tgts[index]->ltd_uuid)) {
687                 CERROR("LOV target UUID %s at index %d doesn't match %s.\n",
688                        lov_uuid2str(lov, index), index,
689                        obd_uuid2str(uuidp));
690                 GOTO(out, rc = -EINVAL);
691         }
692
693         CDEBUG(D_CONFIG, "uuid: %s idx: %d gen: %d exp: %p active: %d\n",
694                lov_uuid2str(lov, index), index,
695                lov->lov_tgts[index]->ltd_gen, lov->lov_tgts[index]->ltd_exp,
696                lov->lov_tgts[index]->ltd_active);
697
698         lov->lov_tgts[index]->ltd_reap = 1;
699         lov->lov_death_row++;
700         /* we really delete it from obd_putref */
701 out:
702         obd_putref(obd);
703         up_write(&lov->lov_notify_lock);
704
705         RETURN(rc);
706 }
707
708 static void __lov_del_obd(struct obd_device *obd, struct lov_tgt_desc *tgt)
709 {
710         struct obd_device *osc_obd;
711
712         LASSERT(tgt);
713         LASSERT(tgt->ltd_reap);
714
715         osc_obd = class_exp2obd(tgt->ltd_exp);
716
717         CDEBUG(D_CONFIG, "Removing tgt %s : %s\n",
718                tgt->ltd_uuid.uuid,
719                osc_obd ? osc_obd->obd_name : "<no obd>");
720
721         if (tgt->ltd_exp)
722                 lov_disconnect_obd(obd, tgt);
723
724         OBD_FREE_PTR(tgt);
725
726         /* Manual cleanup - no cleanup logs to clean up the osc's.  We must
727            do it ourselves. And we can't do it from lov_cleanup,
728            because we just lost our only reference to it. */
729         if (osc_obd)
730                 class_manual_cleanup(osc_obd);
731 }
732
733 void lov_fix_desc_stripe_size(__u64 *val)
734 {
735         if (*val < LOV_MIN_STRIPE_SIZE) {
736                 if (*val != 0)
737                         LCONSOLE_INFO("Increasing default stripe size to "
738                                       "minimum %u\n",
739                                       LOV_DESC_STRIPE_SIZE_DEFAULT);
740                 *val = LOV_DESC_STRIPE_SIZE_DEFAULT;
741         } else if (*val & (LOV_MIN_STRIPE_SIZE - 1)) {
742                 *val &= ~(LOV_MIN_STRIPE_SIZE - 1);
743                 LCONSOLE_WARN("Changing default stripe size to %llu (a "
744                               "multiple of %u)\n",
745                               *val, LOV_MIN_STRIPE_SIZE);
746         }
747 }
748
749 void lov_fix_desc_stripe_count(__u32 *val)
750 {
751         if (*val == 0)
752                 *val = 1;
753 }
754
755 void lov_fix_desc_pattern(__u32 *val)
756 {
757         /* from lov_setstripe */
758         if ((*val != 0) && (*val != LOV_PATTERN_RAID0)) {
759                 LCONSOLE_WARN("Unknown stripe pattern: %#x\n", *val);
760                 *val = 0;
761         }
762 }
763
764 void lov_fix_desc_qos_maxage(__u32 *val)
765 {
766         if (*val == 0)
767                 *val = LOV_DESC_QOS_MAXAGE_DEFAULT;
768 }
769
770 void lov_fix_desc(struct lov_desc *desc)
771 {
772         lov_fix_desc_stripe_size(&desc->ld_default_stripe_size);
773         lov_fix_desc_stripe_count(&desc->ld_default_stripe_count);
774         lov_fix_desc_pattern(&desc->ld_pattern);
775         lov_fix_desc_qos_maxage(&desc->ld_qos_maxage);
776 }
777
778 int lov_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
779 {
780         struct lov_desc *desc;
781         struct lov_obd *lov = &obd->u.lov;
782 #ifdef CONFIG_PROC_FS
783         struct obd_type *type;
784 #endif
785         int rc;
786         ENTRY;
787
788         if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
789                 CERROR("LOV setup requires a descriptor\n");
790                 RETURN(-EINVAL);
791         }
792
793         desc = (struct lov_desc *)lustre_cfg_buf(lcfg, 1);
794
795         if (sizeof(*desc) > LUSTRE_CFG_BUFLEN(lcfg, 1)) {
796                 CERROR("descriptor size wrong: %d > %d\n",
797                        (int)sizeof(*desc), LUSTRE_CFG_BUFLEN(lcfg, 1));
798                 RETURN(-EINVAL);
799         }
800
801         if (desc->ld_magic != LOV_DESC_MAGIC) {
802                 if (desc->ld_magic == __swab32(LOV_DESC_MAGIC)) {
803                             CDEBUG(D_OTHER, "%s: Swabbing lov desc %p\n",
804                                    obd->obd_name, desc);
805                             lustre_swab_lov_desc(desc);
806                 } else {
807                         CERROR("%s: Bad lov desc magic: %#x\n",
808                                obd->obd_name, desc->ld_magic);
809                         RETURN(-EINVAL);
810                 }
811         }
812
813         lov_fix_desc(desc);
814
815         desc->ld_active_tgt_count = 0;
816         lov->desc = *desc;
817         lov->lov_tgt_size = 0;
818
819         mutex_init(&lov->lov_lock);
820         atomic_set(&lov->lov_refcount, 0);
821         lov->lov_sp_me = LUSTRE_SP_CLI;
822
823         init_rwsem(&lov->lov_notify_lock);
824
825         lov->lov_pools_hash_body = cfs_hash_create("POOLS", HASH_POOLS_CUR_BITS,
826                                                    HASH_POOLS_MAX_BITS,
827                                                    HASH_POOLS_BKT_BITS, 0,
828                                                    CFS_HASH_MIN_THETA,
829                                                    CFS_HASH_MAX_THETA,
830                                                    &pool_hash_operations,
831                                                    CFS_HASH_DEFAULT);
832         INIT_LIST_HEAD(&lov->lov_pool_list);
833         lov->lov_pool_count = 0;
834         rc = lov_ost_pool_init(&lov->lov_packed, 0);
835         if (rc)
836                 GOTO(out, rc);
837
838 #ifdef CONFIG_PROC_FS
839         obd->obd_vars = lprocfs_lov_obd_vars;
840         /* If this is true then both client (lov) and server
841          * (lod) are on the same node. The lod layer if loaded
842          * first will register the lov proc directory. In that
843          * case obd->obd_type->typ_procroot will be not set.
844          * Instead we use type->typ_procsym as the parent. */
845         type = class_search_type(LUSTRE_LOD_NAME);
846         if (type != NULL && type->typ_procsym != NULL) {
847                 obd->obd_proc_entry = lprocfs_register(obd->obd_name,
848                                                        type->typ_procsym,
849                                                        obd->obd_vars, obd);
850                 if (IS_ERR(obd->obd_proc_entry)) {
851                         rc = PTR_ERR(obd->obd_proc_entry);
852                         CERROR("error %d setting up lprocfs for %s\n", rc,
853                                obd->obd_name);
854                         obd->obd_proc_entry = NULL;
855                 }
856         } else {
857                 rc = lprocfs_obd_setup(obd);
858         }
859
860         if (rc == 0) {
861                 rc = lprocfs_seq_create(obd->obd_proc_entry, "target_obd",
862                                         0444, &lov_proc_target_fops, obd);
863                 if (rc)
864                         CWARN("Error adding the target_obd file\n");
865
866                 lov->lov_pool_proc_entry = lprocfs_register("pools",
867                                                             obd->obd_proc_entry,
868                                                             NULL, NULL);
869                 if (IS_ERR(lov->lov_pool_proc_entry)) {
870                         rc = PTR_ERR(lov->lov_pool_proc_entry);
871                         CERROR("error %d setting up lprocfs for pools\n", rc);
872                         lov->lov_pool_proc_entry = NULL;
873                 }
874         }
875 #endif
876         RETURN(0);
877
878 out:
879         return rc;
880 }
881
882 static int lov_cleanup(struct obd_device *obd)
883 {
884         struct lov_obd *lov = &obd->u.lov;
885         struct list_head *pos, *tmp;
886         struct pool_desc *pool;
887         ENTRY;
888
889         list_for_each_safe(pos, tmp, &lov->lov_pool_list) {
890                 pool = list_entry(pos, struct pool_desc, pool_list);
891                 /* free pool structs */
892                 CDEBUG(D_INFO, "delete pool %p\n", pool);
893                 /* In the function below, .hs_keycmp resolves to
894                  * pool_hashkey_keycmp() */
895                 /* coverity[overrun-buffer-val] */
896                 lov_pool_del(obd, pool->pool_name);
897         }
898         cfs_hash_putref(lov->lov_pools_hash_body);
899         lov_ost_pool_free(&lov->lov_packed);
900
901         lprocfs_obd_cleanup(obd);
902         if (lov->lov_tgts) {
903                 int i;
904                 obd_getref(obd);
905                 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
906                         if (!lov->lov_tgts[i])
907                                 continue;
908
909                         /* Inactive targets may never have connected */
910                         if (lov->lov_tgts[i]->ltd_active ||
911                             atomic_read(&lov->lov_refcount))
912                                 /* We should never get here - these
913                                  * should have been removed in the
914                                  * disconnect. */
915                                 CERROR("%s: lov tgt %d not cleaned! "
916                                        "deathrow=%d, lovrc=%d\n",
917                                        obd->obd_name, i, lov->lov_death_row,
918                                        atomic_read(&lov->lov_refcount));
919                         lov_del_target(obd, i, NULL, 0);
920                 }
921                 obd_putref(obd);
922                 OBD_FREE(lov->lov_tgts, sizeof(*lov->lov_tgts) *
923                          lov->lov_tgt_size);
924                 lov->lov_tgt_size = 0;
925         }
926
927         if (lov->lov_cache != NULL) {
928                 cl_cache_decref(lov->lov_cache);
929                 lov->lov_cache = NULL;
930         }
931
932         RETURN(0);
933 }
934
935 int lov_process_config_base(struct obd_device *obd, struct lustre_cfg *lcfg,
936                             __u32 *indexp, int *genp)
937 {
938         struct obd_uuid obd_uuid;
939         int cmd;
940         int rc = 0;
941         ENTRY;
942
943         switch(cmd = lcfg->lcfg_command) {
944         case LCFG_LOV_ADD_OBD:
945         case LCFG_LOV_ADD_INA:
946         case LCFG_LOV_DEL_OBD: {
947                 __u32 index;
948                 int gen;
949                 /* lov_modify_tgts add  0:lov_mdsA  1:ost1_UUID  2:0  3:1 */
950                 if (LUSTRE_CFG_BUFLEN(lcfg, 1) > sizeof(obd_uuid.uuid))
951                         GOTO(out, rc = -EINVAL);
952
953                 obd_str2uuid(&obd_uuid,  lustre_cfg_buf(lcfg, 1));
954
955                 if (sscanf(lustre_cfg_buf(lcfg, 2), "%u", indexp) != 1)
956                         GOTO(out, rc = -EINVAL);
957                 if (sscanf(lustre_cfg_buf(lcfg, 3), "%d", genp) != 1)
958                         GOTO(out, rc = -EINVAL);
959                 index = *indexp;
960                 gen = *genp;
961                 if (cmd == LCFG_LOV_ADD_OBD)
962                         rc = lov_add_target(obd, &obd_uuid, index, gen, 1);
963                 else if (cmd == LCFG_LOV_ADD_INA)
964                         rc = lov_add_target(obd, &obd_uuid, index, gen, 0);
965                 else
966                         rc = lov_del_target(obd, index, &obd_uuid, gen);
967                 GOTO(out, rc);
968         }
969         case LCFG_PARAM: {
970                 struct lov_desc *desc = &(obd->u.lov.desc);
971
972                 if (!desc)
973                         GOTO(out, rc = -EINVAL);
974
975                 rc = class_process_proc_param(PARAM_LOV, obd->obd_vars,
976                                               lcfg, obd);
977                 if (rc > 0)
978                         rc = 0;
979                 GOTO(out, rc);
980         }
981         case LCFG_POOL_NEW:
982         case LCFG_POOL_ADD:
983         case LCFG_POOL_DEL:
984         case LCFG_POOL_REM:
985                 GOTO(out, rc);
986
987         default: {
988                 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
989                 GOTO(out, rc = -EINVAL);
990
991         }
992         }
993 out:
994         RETURN(rc);
995 }
996
997 static int
998 lov_statfs_interpret(struct ptlrpc_request_set *rqset, void *data, int rc)
999 {
1000         struct lov_request_set *lovset = (struct lov_request_set *)data;
1001         int err;
1002         ENTRY;
1003
1004         if (rc)
1005                 atomic_set(&lovset->set_completes, 0);
1006
1007         err = lov_fini_statfs_set(lovset);
1008         RETURN(rc ? rc : err);
1009 }
1010
1011 static int lov_statfs_async(struct obd_export *exp, struct obd_info *oinfo,
1012                             __u64 max_age, struct ptlrpc_request_set *rqset)
1013 {
1014         struct obd_device      *obd = class_exp2obd(exp);
1015         struct lov_request_set *set;
1016         struct lov_request *req;
1017         struct list_head *pos;
1018         struct lov_obd *lov;
1019         int rc = 0;
1020         ENTRY;
1021
1022         LASSERT(oinfo != NULL);
1023         LASSERT(oinfo->oi_osfs != NULL);
1024
1025         lov = &obd->u.lov;
1026         rc = lov_prep_statfs_set(obd, oinfo, &set);
1027         if (rc)
1028                 RETURN(rc);
1029
1030         list_for_each(pos, &set->set_list) {
1031                 req = list_entry(pos, struct lov_request, rq_link);
1032                 rc = obd_statfs_async(lov->lov_tgts[req->rq_idx]->ltd_exp,
1033                                       &req->rq_oi, max_age, rqset);
1034                 if (rc)
1035                         break;
1036         }
1037
1038         if (rc || list_empty(&rqset->set_requests)) {
1039                 int err;
1040                 if (rc)
1041                         atomic_set(&set->set_completes, 0);
1042                 err = lov_fini_statfs_set(set);
1043                 RETURN(rc ? rc : err);
1044         }
1045
1046         LASSERT(rqset->set_interpret == NULL);
1047         rqset->set_interpret = lov_statfs_interpret;
1048         rqset->set_arg = (void *)set;
1049         RETURN(0);
1050 }
1051
1052 static int lov_statfs(const struct lu_env *env, struct obd_export *exp,
1053                       struct obd_statfs *osfs, __u64 max_age, __u32 flags)
1054 {
1055         struct ptlrpc_request_set *set = NULL;
1056         struct obd_info oinfo = {
1057                 .oi_osfs = osfs,
1058                 .oi_flags = flags,
1059         };
1060         int rc = 0;
1061
1062         ENTRY;
1063
1064         /* for obdclass we forbid using obd_statfs_rqset, but prefer using async
1065          * statfs requests */
1066         set = ptlrpc_prep_set();
1067         if (set == NULL)
1068                 RETURN(-ENOMEM);
1069
1070         rc = lov_statfs_async(exp, &oinfo, max_age, set);
1071         if (rc == 0)
1072                 rc = ptlrpc_set_wait(set);
1073
1074         ptlrpc_set_destroy(set);
1075
1076         RETURN(rc);
1077 }
1078
1079 static int lov_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1080                          void *karg, void __user *uarg)
1081 {
1082         struct obd_device *obddev = class_exp2obd(exp);
1083         struct lov_obd *lov = &obddev->u.lov;
1084         int i = 0, rc = 0, count = lov->desc.ld_tgt_count;
1085         struct obd_uuid *uuidp;
1086         ENTRY;
1087
1088         switch (cmd) {
1089         case IOC_OBD_STATFS: {
1090                 struct obd_ioctl_data *data = karg;
1091                 struct obd_device *osc_obd;
1092                 struct obd_statfs stat_buf = {0};
1093                 __u32 index;
1094                 __u32 flags;
1095
1096                 memcpy(&index, data->ioc_inlbuf2, sizeof(index));
1097                 if ((index >= count))
1098                         RETURN(-ENODEV);
1099
1100                 if (!lov->lov_tgts[index])
1101                         /* Try again with the next index */
1102                         RETURN(-EAGAIN);
1103                 if (!lov->lov_tgts[index]->ltd_active)
1104                         RETURN(-ENODATA);
1105
1106                 osc_obd = class_exp2obd(lov->lov_tgts[index]->ltd_exp);
1107                 if (!osc_obd)
1108                         RETURN(-EINVAL);
1109
1110                 /* copy UUID */
1111                 if (copy_to_user(data->ioc_pbuf2, obd2cli_tgt(osc_obd),
1112                                  min_t(unsigned long, data->ioc_plen2,
1113                                        sizeof(struct obd_uuid))))
1114                         RETURN(-EFAULT);
1115
1116                 memcpy(&flags, data->ioc_inlbuf1, sizeof(flags));
1117                 flags = flags & LL_STATFS_NODELAY ? OBD_STATFS_NODELAY : 0;
1118
1119                 /* got statfs data */
1120                 rc = obd_statfs(NULL, lov->lov_tgts[index]->ltd_exp, &stat_buf,
1121                                 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
1122                                 flags);
1123                 if (rc)
1124                         RETURN(rc);
1125                 if (copy_to_user(data->ioc_pbuf1, &stat_buf,
1126                                  min_t(unsigned long, data->ioc_plen1,
1127                                        sizeof(struct obd_statfs))))
1128                         RETURN(-EFAULT);
1129                 break;
1130         }
1131         case OBD_IOC_LOV_GET_CONFIG: {
1132                 struct obd_ioctl_data *data;
1133                 struct lov_desc *desc;
1134                 char *buf = NULL;
1135                 __u32 *genp;
1136
1137                 len = 0;
1138                 if (obd_ioctl_getdata(&buf, &len, uarg))
1139                         RETURN(-EINVAL);
1140
1141                 data = (struct obd_ioctl_data *)buf;
1142
1143                 if (sizeof(*desc) > data->ioc_inllen1) {
1144                         obd_ioctl_freedata(buf, len);
1145                         RETURN(-EINVAL);
1146                 }
1147
1148                 if (sizeof(uuidp->uuid) * count > data->ioc_inllen2) {
1149                         obd_ioctl_freedata(buf, len);
1150                         RETURN(-EINVAL);
1151                 }
1152
1153                 if (sizeof(__u32) * count > data->ioc_inllen3) {
1154                         obd_ioctl_freedata(buf, len);
1155                         RETURN(-EINVAL);
1156                 }
1157
1158                 desc = (struct lov_desc *)data->ioc_inlbuf1;
1159                 memcpy(desc, &(lov->desc), sizeof(*desc));
1160
1161                 uuidp = (struct obd_uuid *)data->ioc_inlbuf2;
1162                 genp = (__u32 *)data->ioc_inlbuf3;
1163                 /* the uuid will be empty for deleted OSTs */
1164                 for (i = 0; i < count; i++, uuidp++, genp++) {
1165                         if (!lov->lov_tgts[i])
1166                                 continue;
1167                         *uuidp = lov->lov_tgts[i]->ltd_uuid;
1168                         *genp = lov->lov_tgts[i]->ltd_gen;
1169                 }
1170
1171                 if (copy_to_user(uarg, buf, len))
1172                         rc = -EFAULT;
1173                 obd_ioctl_freedata(buf, len);
1174                 break;
1175         }
1176         case OBD_IOC_QUOTACTL: {
1177                 struct if_quotactl *qctl = karg;
1178                 struct lov_tgt_desc *tgt = NULL;
1179                 struct obd_quotactl *oqctl;
1180
1181                 if (qctl->qc_valid == QC_OSTIDX) {
1182                         if (count <= qctl->qc_idx)
1183                                 RETURN(-EINVAL);
1184
1185                         tgt = lov->lov_tgts[qctl->qc_idx];
1186                         if (!tgt || !tgt->ltd_exp)
1187                                 RETURN(-EINVAL);
1188                 } else if (qctl->qc_valid == QC_UUID) {
1189                         for (i = 0; i < count; i++) {
1190                                 tgt = lov->lov_tgts[i];
1191                                 if (!tgt ||
1192                                     !obd_uuid_equals(&tgt->ltd_uuid,
1193                                                      &qctl->obd_uuid))
1194                                         continue;
1195
1196                                 if (tgt->ltd_exp == NULL)
1197                                         RETURN(-EINVAL);
1198
1199                                 break;
1200                         }
1201                 } else {
1202                         RETURN(-EINVAL);
1203                 }
1204
1205                 if (i >= count)
1206                         RETURN(-EAGAIN);
1207
1208                 LASSERT(tgt && tgt->ltd_exp);
1209                 OBD_ALLOC_PTR(oqctl);
1210                 if (!oqctl)
1211                         RETURN(-ENOMEM);
1212
1213                 QCTL_COPY(oqctl, qctl);
1214                 rc = obd_quotactl(tgt->ltd_exp, oqctl);
1215                 if (rc == 0) {
1216                         QCTL_COPY(qctl, oqctl);
1217                         qctl->qc_valid = QC_OSTIDX;
1218                         qctl->obd_uuid = tgt->ltd_uuid;
1219                 }
1220                 OBD_FREE_PTR(oqctl);
1221                 break;
1222         }
1223         default: {
1224                 int set = 0;
1225
1226                 if (count == 0)
1227                         RETURN(-ENOTTY);
1228
1229                 for (i = 0; i < count; i++) {
1230                         int err;
1231                         struct obd_device *osc_obd;
1232
1233                         /* OST was disconnected */
1234                         if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_exp)
1235                                 continue;
1236
1237                         /* ll_umount_begin() sets force flag but for lov, not
1238                          * osc. Let's pass it through */
1239                         osc_obd = class_exp2obd(lov->lov_tgts[i]->ltd_exp);
1240                         osc_obd->obd_force = obddev->obd_force;
1241                         err = obd_iocontrol(cmd, lov->lov_tgts[i]->ltd_exp,
1242                                             len, karg, uarg);
1243                         if (err) {
1244                                 if (lov->lov_tgts[i]->ltd_active) {
1245                                         CDEBUG(err == -ENOTTY ?
1246                                                D_IOCTL : D_WARNING,
1247                                                "iocontrol OSC %s on OST "
1248                                                "idx %d cmd %x: err = %d\n",
1249                                                lov_uuid2str(lov, i),
1250                                                i, cmd, err);
1251                                         if (!rc)
1252                                                 rc = err;
1253                                 }
1254                         } else {
1255                                 set = 1;
1256                         }
1257                 }
1258                 if (!set && !rc)
1259                         rc = -EIO;
1260         }
1261         }
1262
1263         RETURN(rc);
1264 }
1265
1266 static int lov_get_info(const struct lu_env *env, struct obd_export *exp,
1267                         __u32 keylen, void *key, __u32 *vallen, void *val)
1268 {
1269         struct obd_device *obddev = class_exp2obd(exp);
1270         struct lov_obd *lov = &obddev->u.lov;
1271         struct lov_desc *ld = &lov->desc;
1272         int rc = 0;
1273         ENTRY;
1274
1275         if (vallen == NULL || val == NULL)
1276                 RETURN(-EFAULT);
1277
1278         obd_getref(obddev);
1279
1280         if (KEY_IS(KEY_MAX_EASIZE)) {
1281                 u32 max_stripe_count = min_t(u32, ld->ld_active_tgt_count,
1282                                              LOV_MAX_STRIPE_COUNT);
1283
1284                 *((u32 *)val) = lov_mds_md_size(max_stripe_count, LOV_MAGIC_V3);
1285         } else if (KEY_IS(KEY_DEFAULT_EASIZE)) {
1286                 u32 def_stripe_count = min_t(u32, ld->ld_default_stripe_count,
1287                                              LOV_MAX_STRIPE_COUNT);
1288
1289                 *((u32 *)val) = lov_mds_md_size(def_stripe_count, LOV_MAGIC_V3);
1290         } else if (KEY_IS(KEY_TGT_COUNT)) {
1291                 *((int *)val) = lov->desc.ld_tgt_count;
1292         } else {
1293                 rc = -EINVAL;
1294         }
1295
1296         obd_putref(obddev);
1297
1298         RETURN(rc);
1299 }
1300
1301 static int lov_set_info_async(const struct lu_env *env, struct obd_export *exp,
1302                               __u32 keylen, void *key,
1303                               __u32 vallen, void *val,
1304                               struct ptlrpc_request_set *set)
1305 {
1306         struct obd_device *obddev = class_exp2obd(exp);
1307         struct lov_obd *lov = &obddev->u.lov;
1308         struct lov_tgt_desc *tgt;
1309         int do_inactive = 0;
1310         int no_set = 0;
1311         u32 count;
1312         u32 i;
1313         int rc = 0;
1314         int err;
1315         ENTRY;
1316
1317         if (set == NULL) {
1318                 no_set = 1;
1319                 set = ptlrpc_prep_set();
1320                 if (!set)
1321                         RETURN(-ENOMEM);
1322         }
1323
1324         obd_getref(obddev);
1325         count = lov->desc.ld_tgt_count;
1326
1327         if (KEY_IS(KEY_CHECKSUM)) {
1328                 do_inactive = 1;
1329         } else if (KEY_IS(KEY_CACHE_SET)) {
1330                 LASSERT(lov->lov_cache == NULL);
1331                 lov->lov_cache = val;
1332                 do_inactive = 1;
1333                 cl_cache_incref(lov->lov_cache);
1334         }
1335
1336         for (i = 0; i < count; i++) {
1337                 tgt = lov->lov_tgts[i];
1338
1339                 /* OST was disconnected */
1340                 if (!tgt || !tgt->ltd_exp)
1341                         continue;
1342
1343                 /* OST is inactive and we don't want inactive OSCs */
1344                 if (!tgt->ltd_active && !do_inactive)
1345                         continue;
1346
1347                 err = obd_set_info_async(env, tgt->ltd_exp, keylen, key,
1348                                          vallen, val, set);
1349                 if (!rc)
1350                         rc = err;
1351         }
1352
1353         obd_putref(obddev);
1354         if (no_set) {
1355                 err = ptlrpc_set_wait(set);
1356                 if (!rc)
1357                         rc = err;
1358                 ptlrpc_set_destroy(set);
1359         }
1360         RETURN(rc);
1361 }
1362
1363 void lov_stripe_lock(struct lov_stripe_md *md)
1364 __acquires(&md->lsm_lock)
1365 {
1366         LASSERT(md->lsm_lock_owner != current_pid());
1367         spin_lock(&md->lsm_lock);
1368         LASSERT(md->lsm_lock_owner == 0);
1369         md->lsm_lock_owner = current_pid();
1370 }
1371
1372 void lov_stripe_unlock(struct lov_stripe_md *md)
1373 __releases(&md->lsm_lock)
1374 {
1375         LASSERT(md->lsm_lock_owner == current_pid());
1376         md->lsm_lock_owner = 0;
1377         spin_unlock(&md->lsm_lock);
1378 }
1379
1380 static int lov_quotactl(struct obd_device *obd, struct obd_export *exp,
1381                         struct obd_quotactl *oqctl)
1382 {
1383         struct lov_obd      *lov = &obd->u.lov;
1384         struct lov_tgt_desc *tgt;
1385         __u64                curspace = 0;
1386         __u64                bhardlimit = 0;
1387         int                  i, rc = 0;
1388         ENTRY;
1389
1390         if (oqctl->qc_cmd != Q_GETOQUOTA &&
1391             oqctl->qc_cmd != LUSTRE_Q_SETQUOTA) {
1392                 CERROR("%s: bad quota opc %x for lov obd\n",
1393                        obd->obd_name, oqctl->qc_cmd);
1394                 RETURN(-EFAULT);
1395         }
1396
1397         /* for lov tgt */
1398         obd_getref(obd);
1399         for (i = 0; i < lov->desc.ld_tgt_count; i++) {
1400                 int err;
1401
1402                 tgt = lov->lov_tgts[i];
1403
1404                 if (!tgt)
1405                         continue;
1406
1407                 if (!tgt->ltd_active || tgt->ltd_reap) {
1408                         if (oqctl->qc_cmd == Q_GETOQUOTA &&
1409                             lov->lov_tgts[i]->ltd_activate) {
1410                                 rc = -ENETDOWN;
1411                                 CERROR("ost %d is inactive\n", i);
1412                         } else {
1413                                 CDEBUG(D_HA, "ost %d is inactive\n", i);
1414                         }
1415                         continue;
1416                 }
1417
1418                 err = obd_quotactl(tgt->ltd_exp, oqctl);
1419                 if (err) {
1420                         if (tgt->ltd_active && !rc)
1421                                 rc = err;
1422                         continue;
1423                 }
1424
1425                 if (oqctl->qc_cmd == Q_GETOQUOTA) {
1426                         curspace += oqctl->qc_dqblk.dqb_curspace;
1427                         bhardlimit += oqctl->qc_dqblk.dqb_bhardlimit;
1428                 }
1429         }
1430         obd_putref(obd);
1431
1432         if (oqctl->qc_cmd == Q_GETOQUOTA) {
1433                 oqctl->qc_dqblk.dqb_curspace = curspace;
1434                 oqctl->qc_dqblk.dqb_bhardlimit = bhardlimit;
1435         }
1436         RETURN(rc);
1437 }
1438
1439 static struct obd_ops lov_obd_ops = {
1440         .o_owner                = THIS_MODULE,
1441         .o_setup                = lov_setup,
1442         .o_cleanup              = lov_cleanup,
1443         .o_connect              = lov_connect,
1444         .o_disconnect           = lov_disconnect,
1445         .o_statfs               = lov_statfs,
1446         .o_statfs_async         = lov_statfs_async,
1447         .o_iocontrol            = lov_iocontrol,
1448         .o_get_info             = lov_get_info,
1449         .o_set_info_async       = lov_set_info_async,
1450         .o_notify               = lov_notify,
1451         .o_pool_new             = lov_pool_new,
1452         .o_pool_rem             = lov_pool_remove,
1453         .o_pool_add             = lov_pool_add,
1454         .o_pool_del             = lov_pool_del,
1455         .o_getref               = lov_getref,
1456         .o_putref               = lov_putref,
1457         .o_quotactl             = lov_quotactl,
1458 };
1459
1460 struct kmem_cache *lov_oinfo_slab;
1461
1462 static int __init lov_init(void)
1463 {
1464         bool enable_proc = true;
1465         struct obd_type *type;
1466         int rc;
1467         ENTRY;
1468
1469         /* print an address of _any_ initialized kernel symbol from this
1470          * module, to allow debugging with gdb that doesn't support data
1471          * symbols from modules.*/
1472         CDEBUG(D_INFO, "Lustre LOV module (%p).\n", &lov_caches);
1473
1474         rc = lu_kmem_init(lov_caches);
1475         if (rc)
1476                 return rc;
1477
1478         lov_oinfo_slab = kmem_cache_create("lov_oinfo",
1479                                            sizeof(struct lov_oinfo), 0,
1480                                            SLAB_HWCACHE_ALIGN, NULL);
1481         if (lov_oinfo_slab == NULL) {
1482                 lu_kmem_fini(lov_caches);
1483                 return -ENOMEM;
1484         }
1485
1486         type = class_search_type(LUSTRE_LOD_NAME);
1487         if (type != NULL && type->typ_procsym != NULL)
1488                 enable_proc = false;
1489
1490         rc = class_register_type(&lov_obd_ops, NULL, enable_proc, NULL,
1491                                  LUSTRE_LOV_NAME, &lov_device_type);
1492
1493         if (rc) {
1494                 kmem_cache_destroy(lov_oinfo_slab);
1495                 lu_kmem_fini(lov_caches);
1496         }
1497
1498         RETURN(rc);
1499 }
1500
1501 static void __exit lov_exit(void)
1502 {
1503         class_unregister_type(LUSTRE_LOV_NAME);
1504         kmem_cache_destroy(lov_oinfo_slab);
1505         lu_kmem_fini(lov_caches);
1506 }
1507
1508 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
1509 MODULE_DESCRIPTION("Lustre Logical Object Volume");
1510 MODULE_VERSION(LUSTRE_VERSION_STRING);
1511 MODULE_LICENSE("GPL");
1512
1513 module_init(lov_init);
1514 module_exit(lov_exit);