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