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