staging/lustre/obdclass: remove local_storage.[ch]
[deliverable/linux.git] / drivers / staging / lustre / lustre / lov / lov_obd.c
CommitLineData
d7e09d03
PT
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, 2012, 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
9fdaf8c0 45#include "../../include/linux/libcfs/libcfs.h"
d7e09d03 46
0cf0f7a7
GKH
47#include "../include/obd_support.h"
48#include "../include/lustre_lib.h"
49#include "../include/lustre_net.h"
50#include "../include/lustre/lustre_idl.h"
51#include "../include/lustre_dlm.h"
52#include "../include/lustre_mds.h"
53#include "../include/obd_class.h"
0cf0f7a7
GKH
54#include "../include/lprocfs_status.h"
55#include "../include/lustre_param.h"
56#include "../include/cl_object.h"
57#include "../include/lclient.h" /* for cl_client_lru */
58#include "../include/lustre/ll_fiemap.h"
59#include "../include/lustre_fid.h"
d7e09d03
PT
60
61#include "lov_internal.h"
62
63/* Keep a refcount of lov->tgt usage to prevent racing with addition/deletion.
64 Any function that expects lov_tgts to remain stationary must take a ref. */
65static void lov_getref(struct obd_device *obd)
66{
67 struct lov_obd *lov = &obd->u.lov;
68
69 /* nobody gets through here until lov_putref is done */
70 mutex_lock(&lov->lov_lock);
71 atomic_inc(&lov->lov_refcount);
72 mutex_unlock(&lov->lov_lock);
73 return;
74}
75
76static void __lov_del_obd(struct obd_device *obd, struct lov_tgt_desc *tgt);
77
78static void lov_putref(struct obd_device *obd)
79{
80 struct lov_obd *lov = &obd->u.lov;
81
82 mutex_lock(&lov->lov_lock);
83 /* ok to dec to 0 more than once -- ltd_exp's will be null */
84 if (atomic_dec_and_test(&lov->lov_refcount) && lov->lov_death_row) {
85 LIST_HEAD(kill);
86 int i;
87 struct lov_tgt_desc *tgt, *n;
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
115static int lov_set_osc_active(struct obd_device *obd, struct obd_uuid *uuid,
116 enum obd_notify_event ev);
117static int lov_notify(struct obd_device *obd, struct obd_device *watched,
118 enum obd_notify_event ev, void *data);
119
120
121#define MAX_STRING_SIZE 128
122int lov_connect_obd(struct obd_device *obd, __u32 index, int activate,
123 struct obd_connect_data *data)
124{
125 struct lov_obd *lov = &obd->u.lov;
126 struct obd_uuid *tgt_uuid;
127 struct obd_device *tgt_obd;
128 static struct obd_uuid lov_osc_uuid = { "LOV_OSC_UUID" };
129 struct obd_import *imp;
b59fe845 130 struct proc_dir_entry *lov_proc_dir;
d7e09d03 131 int rc;
d7e09d03
PT
132
133 if (!lov->lov_tgts[index])
0a3bdb00 134 return -EINVAL;
d7e09d03
PT
135
136 tgt_uuid = &lov->lov_tgts[index]->ltd_uuid;
137 tgt_obd = lov->lov_tgts[index]->ltd_obd;
138
139 if (!tgt_obd->obd_set_up) {
140 CERROR("Target %s not set up\n", obd_uuid2str(tgt_uuid));
0a3bdb00 141 return -EINVAL;
d7e09d03
PT
142 }
143
144 /* override the sp_me from lov */
145 tgt_obd->u.cli.cl_sp_me = lov->lov_sp_me;
146
147 if (data && (data->ocd_connect_flags & OBD_CONNECT_INDEX))
148 data->ocd_index = index;
149
150 /*
151 * Divine LOV knows that OBDs under it are OSCs.
152 */
153 imp = tgt_obd->u.cli.cl_import;
154
155 if (activate) {
156 tgt_obd->obd_no_recov = 0;
157 /* FIXME this is probably supposed to be
158 ptlrpc_set_import_active. Horrible naming. */
159 ptlrpc_activate_import(imp);
160 }
161
162 rc = obd_register_observer(tgt_obd, obd);
163 if (rc) {
164 CERROR("Target %s register_observer error %d\n",
165 obd_uuid2str(tgt_uuid), rc);
0a3bdb00 166 return rc;
d7e09d03
PT
167 }
168
169
170 if (imp->imp_invalid) {
171 CDEBUG(D_CONFIG, "not connecting OSC %s; administratively "
172 "disabled\n", obd_uuid2str(tgt_uuid));
0a3bdb00 173 return 0;
d7e09d03
PT
174 }
175
176 rc = obd_connect(NULL, &lov->lov_tgts[index]->ltd_exp, tgt_obd,
177 &lov_osc_uuid, data, NULL);
178 if (rc || !lov->lov_tgts[index]->ltd_exp) {
179 CERROR("Target %s connect error %d\n",
180 obd_uuid2str(tgt_uuid), rc);
0a3bdb00 181 return -ENODEV;
d7e09d03
PT
182 }
183
184 lov->lov_tgts[index]->ltd_reap = 0;
185
186 CDEBUG(D_CONFIG, "Connected tgt idx %d %s (%s) %sactive\n", index,
187 obd_uuid2str(tgt_uuid), tgt_obd->obd_name, activate ? "":"in");
188
73bb1da6 189 lov_proc_dir = obd->obd_proc_private;
d7e09d03
PT
190 if (lov_proc_dir) {
191 struct obd_device *osc_obd = lov->lov_tgts[index]->ltd_exp->exp_obd;
b59fe845 192 struct proc_dir_entry *osc_symlink;
d7e09d03
PT
193
194 LASSERT(osc_obd != NULL);
195 LASSERT(osc_obd->obd_magic == OBD_DEVICE_MAGIC);
196 LASSERT(osc_obd->obd_type->typ_name != NULL);
197
198 osc_symlink = lprocfs_add_symlink(osc_obd->obd_name,
199 lov_proc_dir,
200 "../../../%s/%s",
201 osc_obd->obd_type->typ_name,
202 osc_obd->obd_name);
203 if (osc_symlink == NULL) {
204 CERROR("could not register LOV target "
205 "/proc/fs/lustre/%s/%s/target_obds/%s.",
206 obd->obd_type->typ_name, obd->obd_name,
207 osc_obd->obd_name);
208 lprocfs_remove(&lov_proc_dir);
73bb1da6 209 obd->obd_proc_private = NULL;
d7e09d03
PT
210 }
211 }
212
0a3bdb00 213 return 0;
d7e09d03
PT
214}
215
216static int lov_connect(const struct lu_env *env,
217 struct obd_export **exp, struct obd_device *obd,
218 struct obd_uuid *cluuid, struct obd_connect_data *data,
219 void *localdata)
220{
221 struct lov_obd *lov = &obd->u.lov;
222 struct lov_tgt_desc *tgt;
223 struct lustre_handle conn;
224 int i, rc;
d7e09d03
PT
225
226 CDEBUG(D_CONFIG, "connect #%d\n", lov->lov_connects);
227
228 rc = class_connect(&conn, obd, cluuid);
229 if (rc)
0a3bdb00 230 return rc;
d7e09d03
PT
231
232 *exp = class_conn2export(&conn);
233
234 /* Why should there ever be more than 1 connect? */
235 lov->lov_connects++;
236 LASSERT(lov->lov_connects == 1);
237
238 memset(&lov->lov_ocd, 0, sizeof(lov->lov_ocd));
239 if (data)
240 lov->lov_ocd = *data;
241
242 obd_getref(obd);
243 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
244 tgt = lov->lov_tgts[i];
245 if (!tgt || obd_uuid_empty(&tgt->ltd_uuid))
246 continue;
247 /* Flags will be lowest common denominator */
248 rc = lov_connect_obd(obd, i, tgt->ltd_activate, &lov->lov_ocd);
249 if (rc) {
250 CERROR("%s: lov connect tgt %d failed: %d\n",
251 obd->obd_name, i, rc);
252 continue;
253 }
254 /* connect to administrative disabled ost */
255 if (!lov->lov_tgts[i]->ltd_exp)
256 continue;
257
258 rc = lov_notify(obd, lov->lov_tgts[i]->ltd_exp->exp_obd,
259 OBD_NOTIFY_CONNECT, (void *)&i);
260 if (rc) {
261 CERROR("%s error sending notify %d\n",
262 obd->obd_name, rc);
263 }
264 }
265 obd_putref(obd);
266
0a3bdb00 267 return 0;
d7e09d03
PT
268}
269
270static int lov_disconnect_obd(struct obd_device *obd, struct lov_tgt_desc *tgt)
271{
b59fe845 272 struct proc_dir_entry *lov_proc_dir;
d7e09d03
PT
273 struct lov_obd *lov = &obd->u.lov;
274 struct obd_device *osc_obd;
275 int rc;
d7e09d03
PT
276
277 osc_obd = class_exp2obd(tgt->ltd_exp);
278 CDEBUG(D_CONFIG, "%s: disconnecting target %s\n",
3456a40e 279 obd->obd_name, osc_obd ? osc_obd->obd_name : "NULL");
d7e09d03
PT
280
281 if (tgt->ltd_active) {
282 tgt->ltd_active = 0;
283 lov->desc.ld_active_tgt_count--;
284 tgt->ltd_exp->exp_obd->obd_inactive = 1;
285 }
286
d7e09d03 287 if (osc_obd) {
3456a40e
RS
288 lov_proc_dir = obd->obd_proc_private;
289 if (lov_proc_dir) {
290 lprocfs_remove_proc_entry(osc_obd->obd_name, lov_proc_dir);
291 }
d7e09d03
PT
292 /* Pass it on to our clients.
293 * XXX This should be an argument to disconnect,
294 * XXX not a back-door flag on the OBD. Ah well.
295 */
296 osc_obd->obd_force = obd->obd_force;
297 osc_obd->obd_fail = obd->obd_fail;
298 osc_obd->obd_no_recov = obd->obd_no_recov;
299 }
300
301 obd_register_observer(osc_obd, NULL);
302
303 rc = obd_disconnect(tgt->ltd_exp);
304 if (rc) {
305 CERROR("Target %s disconnect error %d\n",
306 tgt->ltd_uuid.uuid, rc);
307 rc = 0;
308 }
309
310 tgt->ltd_exp = NULL;
0a3bdb00 311 return 0;
d7e09d03
PT
312}
313
314static int lov_disconnect(struct obd_export *exp)
315{
316 struct obd_device *obd = class_exp2obd(exp);
317 struct lov_obd *lov = &obd->u.lov;
318 int i, rc;
d7e09d03
PT
319
320 if (!lov->lov_tgts)
321 goto out;
322
323 /* Only disconnect the underlying layers on the final disconnect. */
324 lov->lov_connects--;
325 if (lov->lov_connects != 0) {
326 /* why should there be more than 1 connect? */
327 CERROR("disconnect #%d\n", lov->lov_connects);
328 goto out;
329 }
330
331 /* Let's hold another reference so lov_del_obd doesn't spin through
332 putref every time */
333 obd_getref(obd);
334
335 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
336 if (lov->lov_tgts[i] && lov->lov_tgts[i]->ltd_exp) {
337 /* Disconnection is the last we know about an obd */
2046a81d 338 lov_del_target(obd, i, NULL, lov->lov_tgts[i]->ltd_gen);
d7e09d03
PT
339 }
340 }
341 obd_putref(obd);
342
343out:
344 rc = class_disconnect(exp); /* bz 9811 */
0a3bdb00 345 return rc;
d7e09d03
PT
346}
347
348/* Error codes:
349 *
350 * -EINVAL : UUID can't be found in the LOV's target list
351 * -ENOTCONN: The UUID is found, but the target connection is bad (!)
352 * -EBADF : The UUID is found, but the OBD is the wrong type (!)
353 * any >= 0 : is log target index
354 */
355static int lov_set_osc_active(struct obd_device *obd, struct obd_uuid *uuid,
356 enum obd_notify_event ev)
357{
358 struct lov_obd *lov = &obd->u.lov;
359 struct lov_tgt_desc *tgt;
360 int index, activate, active;
d7e09d03
PT
361
362 CDEBUG(D_INFO, "Searching in lov %p for uuid %s event(%d)\n",
363 lov, uuid->uuid, ev);
364
365 obd_getref(obd);
366 for (index = 0; index < lov->desc.ld_tgt_count; index++) {
367 tgt = lov->lov_tgts[index];
368 if (!tgt)
369 continue;
370 /*
371 * LU-642, initially inactive OSC could miss the obd_connect,
372 * we make up for it here.
373 */
374 if (ev == OBD_NOTIFY_ACTIVATE && tgt->ltd_exp == NULL &&
375 obd_uuid_equals(uuid, &tgt->ltd_uuid)) {
376 struct obd_uuid lov_osc_uuid = {"LOV_OSC_UUID"};
377
378 obd_connect(NULL, &tgt->ltd_exp, tgt->ltd_obd,
379 &lov_osc_uuid, &lov->lov_ocd, NULL);
380 }
381 if (!tgt->ltd_exp)
382 continue;
383
55f5a824 384 CDEBUG(D_INFO, "lov idx %d is %s conn %#llx\n",
d7e09d03
PT
385 index, obd_uuid2str(&tgt->ltd_uuid),
386 tgt->ltd_exp->exp_handle.h_cookie);
387 if (obd_uuid_equals(uuid, &tgt->ltd_uuid))
388 break;
389 }
390
391 if (index == lov->desc.ld_tgt_count)
392 GOTO(out, index = -EINVAL);
393
394 if (ev == OBD_NOTIFY_DEACTIVATE || ev == OBD_NOTIFY_ACTIVATE) {
395 activate = (ev == OBD_NOTIFY_ACTIVATE) ? 1 : 0;
396
397 if (lov->lov_tgts[index]->ltd_activate == activate) {
398 CDEBUG(D_INFO, "OSC %s already %sactivate!\n",
399 uuid->uuid, activate ? "" : "de");
400 } else {
401 lov->lov_tgts[index]->ltd_activate = activate;
402 CDEBUG(D_CONFIG, "%sactivate OSC %s\n",
403 activate ? "" : "de", obd_uuid2str(uuid));
404 }
405
406 } else if (ev == OBD_NOTIFY_INACTIVE || ev == OBD_NOTIFY_ACTIVE) {
407 active = (ev == OBD_NOTIFY_ACTIVE) ? 1 : 0;
408
409 if (lov->lov_tgts[index]->ltd_active == active) {
410 CDEBUG(D_INFO, "OSC %s already %sactive!\n",
411 uuid->uuid, active ? "" : "in");
412 GOTO(out, index);
413 } else {
414 CDEBUG(D_CONFIG, "Marking OSC %s %sactive\n",
415 obd_uuid2str(uuid), active ? "" : "in");
416 }
417
418 lov->lov_tgts[index]->ltd_active = active;
419 if (active) {
420 lov->desc.ld_active_tgt_count++;
421 lov->lov_tgts[index]->ltd_exp->exp_obd->obd_inactive = 0;
422 } else {
423 lov->desc.ld_active_tgt_count--;
424 lov->lov_tgts[index]->ltd_exp->exp_obd->obd_inactive = 1;
425 }
426 } else {
427 CERROR("Unknown event(%d) for uuid %s", ev, uuid->uuid);
428 }
429
430 out:
431 obd_putref(obd);
0a3bdb00 432 return index;
d7e09d03
PT
433}
434
435static int lov_notify(struct obd_device *obd, struct obd_device *watched,
436 enum obd_notify_event ev, void *data)
437{
438 int rc = 0;
439 struct lov_obd *lov = &obd->u.lov;
d7e09d03
PT
440
441 down_read(&lov->lov_notify_lock);
442 if (!lov->lov_connects) {
443 up_read(&lov->lov_notify_lock);
0a3bdb00 444 return rc;
d7e09d03
PT
445 }
446
447 if (ev == OBD_NOTIFY_ACTIVE || ev == OBD_NOTIFY_INACTIVE ||
448 ev == OBD_NOTIFY_ACTIVATE || ev == OBD_NOTIFY_DEACTIVATE) {
449 struct obd_uuid *uuid;
450
451 LASSERT(watched);
452
453 if (strcmp(watched->obd_type->typ_name, LUSTRE_OSC_NAME)) {
454 up_read(&lov->lov_notify_lock);
455 CERROR("unexpected notification of %s %s!\n",
456 watched->obd_type->typ_name,
457 watched->obd_name);
0a3bdb00 458 return -EINVAL;
d7e09d03
PT
459 }
460 uuid = &watched->u.cli.cl_target_uuid;
461
462 /* Set OSC as active before notifying the observer, so the
463 * observer can use the OSC normally.
464 */
465 rc = lov_set_osc_active(obd, uuid, ev);
466 if (rc < 0) {
467 up_read(&lov->lov_notify_lock);
468 CERROR("event(%d) of %s failed: %d\n", ev,
469 obd_uuid2str(uuid), rc);
0a3bdb00 470 return rc;
d7e09d03
PT
471 }
472 /* active event should be pass lov target index as data */
473 data = &rc;
474 }
475
476 /* Pass the notification up the chain. */
477 if (watched) {
478 rc = obd_notify_observer(obd, watched, ev, data);
479 } else {
480 /* NULL watched means all osc's in the lov (only for syncs) */
481 /* sync event should be send lov idx as data */
482 struct lov_obd *lov = &obd->u.lov;
483 int i, is_sync;
484
485 data = &i;
486 is_sync = (ev == OBD_NOTIFY_SYNC) ||
487 (ev == OBD_NOTIFY_SYNC_NONBLOCK);
488
489 obd_getref(obd);
490 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
491 if (!lov->lov_tgts[i])
492 continue;
493
494 /* don't send sync event if target not
495 * connected/activated */
496 if (is_sync && !lov->lov_tgts[i]->ltd_active)
497 continue;
498
499 rc = obd_notify_observer(obd, lov->lov_tgts[i]->ltd_obd,
500 ev, data);
501 if (rc) {
502 CERROR("%s: notify %s of %s failed %d\n",
503 obd->obd_name,
504 obd->obd_observer->obd_name,
505 lov->lov_tgts[i]->ltd_obd->obd_name,
506 rc);
507 }
508 }
509 obd_putref(obd);
510 }
511
512 up_read(&lov->lov_notify_lock);
0a3bdb00 513 return rc;
d7e09d03
PT
514}
515
516static int lov_add_target(struct obd_device *obd, struct obd_uuid *uuidp,
517 __u32 index, int gen, int active)
518{
519 struct lov_obd *lov = &obd->u.lov;
520 struct lov_tgt_desc *tgt;
521 struct obd_device *tgt_obd;
522 int rc;
d7e09d03
PT
523
524 CDEBUG(D_CONFIG, "uuid:%s idx:%d gen:%d active:%d\n",
525 uuidp->uuid, index, gen, active);
526
527 if (gen <= 0) {
528 CERROR("request to add OBD %s with invalid generation: %d\n",
529 uuidp->uuid, gen);
0a3bdb00 530 return -EINVAL;
d7e09d03
PT
531 }
532
533 tgt_obd = class_find_client_obd(uuidp, LUSTRE_OSC_NAME,
534 &obd->obd_uuid);
535 if (tgt_obd == NULL)
0a3bdb00 536 return -EINVAL;
d7e09d03
PT
537
538 mutex_lock(&lov->lov_lock);
539
540 if ((index < lov->lov_tgt_size) && (lov->lov_tgts[index] != NULL)) {
541 tgt = lov->lov_tgts[index];
542 CERROR("UUID %s already assigned at LOV target index %d\n",
543 obd_uuid2str(&tgt->ltd_uuid), index);
544 mutex_unlock(&lov->lov_lock);
0a3bdb00 545 return -EEXIST;
d7e09d03
PT
546 }
547
548 if (index >= lov->lov_tgt_size) {
549 /* We need to reallocate the lov target array. */
550 struct lov_tgt_desc **newtgts, **old = NULL;
551 __u32 newsize, oldsize = 0;
552
2c0514ee 553 newsize = max_t(__u32, lov->lov_tgt_size, 2);
d7e09d03
PT
554 while (newsize < index + 1)
555 newsize = newsize << 1;
556 OBD_ALLOC(newtgts, sizeof(*newtgts) * newsize);
557 if (newtgts == NULL) {
558 mutex_unlock(&lov->lov_lock);
0a3bdb00 559 return -ENOMEM;
d7e09d03
PT
560 }
561
562 if (lov->lov_tgt_size) {
563 memcpy(newtgts, lov->lov_tgts, sizeof(*newtgts) *
564 lov->lov_tgt_size);
565 old = lov->lov_tgts;
566 oldsize = lov->lov_tgt_size;
567 }
568
569 lov->lov_tgts = newtgts;
570 lov->lov_tgt_size = newsize;
571 smp_rmb();
572 if (old)
573 OBD_FREE(old, sizeof(*old) * oldsize);
574
575 CDEBUG(D_CONFIG, "tgts: %p size: %d\n",
576 lov->lov_tgts, lov->lov_tgt_size);
577 }
578
579 OBD_ALLOC_PTR(tgt);
580 if (!tgt) {
581 mutex_unlock(&lov->lov_lock);
0a3bdb00 582 return -ENOMEM;
d7e09d03
PT
583 }
584
585 rc = lov_ost_pool_add(&lov->lov_packed, index, lov->lov_tgt_size);
586 if (rc) {
587 mutex_unlock(&lov->lov_lock);
588 OBD_FREE_PTR(tgt);
0a3bdb00 589 return rc;
d7e09d03
PT
590 }
591
592 tgt->ltd_uuid = *uuidp;
593 tgt->ltd_obd = tgt_obd;
594 /* XXX - add a sanity check on the generation number. */
595 tgt->ltd_gen = gen;
596 tgt->ltd_index = index;
597 tgt->ltd_activate = active;
598 lov->lov_tgts[index] = tgt;
599 if (index >= lov->desc.ld_tgt_count)
600 lov->desc.ld_tgt_count = index + 1;
601
602 mutex_unlock(&lov->lov_lock);
603
604 CDEBUG(D_CONFIG, "idx=%d ltd_gen=%d ld_tgt_count=%d\n",
605 index, tgt->ltd_gen, lov->desc.ld_tgt_count);
606
607 rc = obd_notify(obd, tgt_obd, OBD_NOTIFY_CREATE, &index);
608
609 if (lov->lov_connects == 0) {
610 /* lov_connect hasn't been called yet. We'll do the
611 lov_connect_obd on this target when that fn first runs,
612 because we don't know the connect flags yet. */
0a3bdb00 613 return 0;
d7e09d03
PT
614 }
615
616 obd_getref(obd);
617
618 rc = lov_connect_obd(obd, index, active, &lov->lov_ocd);
619 if (rc)
620 GOTO(out, rc);
621
622 /* connect to administrative disabled ost */
623 if (!tgt->ltd_exp)
624 GOTO(out, rc = 0);
625
626 if (lov->lov_cache != NULL) {
627 rc = obd_set_info_async(NULL, tgt->ltd_exp,
628 sizeof(KEY_CACHE_SET), KEY_CACHE_SET,
629 sizeof(struct cl_client_cache), lov->lov_cache,
630 NULL);
631 if (rc < 0)
632 GOTO(out, rc);
633 }
634
635 rc = lov_notify(obd, tgt->ltd_exp->exp_obd,
636 active ? OBD_NOTIFY_CONNECT : OBD_NOTIFY_INACTIVE,
637 (void *)&index);
638
639out:
640 if (rc) {
641 CERROR("add failed (%d), deleting %s\n", rc,
642 obd_uuid2str(&tgt->ltd_uuid));
2046a81d 643 lov_del_target(obd, index, NULL, 0);
d7e09d03
PT
644 }
645 obd_putref(obd);
0a3bdb00 646 return rc;
d7e09d03
PT
647}
648
649/* Schedule a target for deletion */
650int lov_del_target(struct obd_device *obd, __u32 index,
651 struct obd_uuid *uuidp, int gen)
652{
653 struct lov_obd *lov = &obd->u.lov;
654 int count = lov->desc.ld_tgt_count;
655 int rc = 0;
d7e09d03
PT
656
657 if (index >= count) {
658 CERROR("LOV target index %d >= number of LOV OBDs %d.\n",
659 index, count);
0a3bdb00 660 return -EINVAL;
d7e09d03
PT
661 }
662
663 /* to make sure there's no ongoing lov_notify() now */
664 down_write(&lov->lov_notify_lock);
665 obd_getref(obd);
666
667 if (!lov->lov_tgts[index]) {
668 CERROR("LOV target at index %d is not setup.\n", index);
669 GOTO(out, rc = -EINVAL);
670 }
671
672 if (uuidp && !obd_uuid_equals(uuidp, &lov->lov_tgts[index]->ltd_uuid)) {
673 CERROR("LOV target UUID %s at index %d doesn't match %s.\n",
674 lov_uuid2str(lov, index), index,
675 obd_uuid2str(uuidp));
676 GOTO(out, rc = -EINVAL);
677 }
678
679 CDEBUG(D_CONFIG, "uuid: %s idx: %d gen: %d exp: %p active: %d\n",
680 lov_uuid2str(lov, index), index,
681 lov->lov_tgts[index]->ltd_gen, lov->lov_tgts[index]->ltd_exp,
682 lov->lov_tgts[index]->ltd_active);
683
684 lov->lov_tgts[index]->ltd_reap = 1;
685 lov->lov_death_row++;
686 /* we really delete it from obd_putref */
687out:
688 obd_putref(obd);
689 up_write(&lov->lov_notify_lock);
690
0a3bdb00 691 return rc;
d7e09d03
PT
692}
693
694static void __lov_del_obd(struct obd_device *obd, struct lov_tgt_desc *tgt)
695{
696 struct obd_device *osc_obd;
697
698 LASSERT(tgt);
699 LASSERT(tgt->ltd_reap);
700
701 osc_obd = class_exp2obd(tgt->ltd_exp);
702
703 CDEBUG(D_CONFIG, "Removing tgt %s : %s\n",
704 tgt->ltd_uuid.uuid,
705 osc_obd ? osc_obd->obd_name : "<no obd>");
706
707 if (tgt->ltd_exp)
708 lov_disconnect_obd(obd, tgt);
709
710 OBD_FREE_PTR(tgt);
711
712 /* Manual cleanup - no cleanup logs to clean up the osc's. We must
713 do it ourselves. And we can't do it from lov_cleanup,
714 because we just lost our only reference to it. */
715 if (osc_obd)
716 class_manual_cleanup(osc_obd);
717}
718
719void lov_fix_desc_stripe_size(__u64 *val)
720{
50dc198a
AD
721 if (*val < LOV_MIN_STRIPE_SIZE) {
722 if (*val != 0)
723 LCONSOLE_INFO("Increasing default stripe size to "
724 "minimum %u\n",
081b7265
JH
725 LOV_DESC_STRIPE_SIZE_DEFAULT);
726 *val = LOV_DESC_STRIPE_SIZE_DEFAULT;
d7e09d03
PT
727 } else if (*val & (LOV_MIN_STRIPE_SIZE - 1)) {
728 *val &= ~(LOV_MIN_STRIPE_SIZE - 1);
b0f5aad5 729 LCONSOLE_WARN("Changing default stripe size to %llu (a multiple of %u)\n",
d7e09d03
PT
730 *val, LOV_MIN_STRIPE_SIZE);
731 }
732}
733
734void lov_fix_desc_stripe_count(__u32 *val)
735{
736 if (*val == 0)
737 *val = 1;
738}
739
740void lov_fix_desc_pattern(__u32 *val)
741{
742 /* from lov_setstripe */
743 if ((*val != 0) && (*val != LOV_PATTERN_RAID0)) {
744 LCONSOLE_WARN("Unknown stripe pattern: %#x\n", *val);
745 *val = 0;
746 }
747}
748
749void lov_fix_desc_qos_maxage(__u32 *val)
750{
d7e09d03 751 if (*val == 0)
081b7265 752 *val = LOV_DESC_QOS_MAXAGE_DEFAULT;
d7e09d03
PT
753}
754
755void lov_fix_desc(struct lov_desc *desc)
756{
757 lov_fix_desc_stripe_size(&desc->ld_default_stripe_size);
758 lov_fix_desc_stripe_count(&desc->ld_default_stripe_count);
759 lov_fix_desc_pattern(&desc->ld_pattern);
760 lov_fix_desc_qos_maxage(&desc->ld_qos_maxage);
761}
762
763int lov_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
764{
2046a81d 765 struct lprocfs_static_vars lvars = { NULL };
d7e09d03
PT
766 struct lov_desc *desc;
767 struct lov_obd *lov = &obd->u.lov;
768 int rc;
d7e09d03
PT
769
770 if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
771 CERROR("LOV setup requires a descriptor\n");
0a3bdb00 772 return -EINVAL;
d7e09d03
PT
773 }
774
775 desc = (struct lov_desc *)lustre_cfg_buf(lcfg, 1);
776
777 if (sizeof(*desc) > LUSTRE_CFG_BUFLEN(lcfg, 1)) {
778 CERROR("descriptor size wrong: %d > %d\n",
779 (int)sizeof(*desc), LUSTRE_CFG_BUFLEN(lcfg, 1));
0a3bdb00 780 return -EINVAL;
d7e09d03
PT
781 }
782
783 if (desc->ld_magic != LOV_DESC_MAGIC) {
784 if (desc->ld_magic == __swab32(LOV_DESC_MAGIC)) {
785 CDEBUG(D_OTHER, "%s: Swabbing lov desc %p\n",
786 obd->obd_name, desc);
787 lustre_swab_lov_desc(desc);
788 } else {
789 CERROR("%s: Bad lov desc magic: %#x\n",
790 obd->obd_name, desc->ld_magic);
0a3bdb00 791 return -EINVAL;
d7e09d03
PT
792 }
793 }
794
795 lov_fix_desc(desc);
796
797 desc->ld_active_tgt_count = 0;
798 lov->desc = *desc;
799 lov->lov_tgt_size = 0;
800
801 mutex_init(&lov->lov_lock);
802 atomic_set(&lov->lov_refcount, 0);
803 lov->lov_sp_me = LUSTRE_SP_CLI;
804
805 init_rwsem(&lov->lov_notify_lock);
806
807 lov->lov_pools_hash_body = cfs_hash_create("POOLS", HASH_POOLS_CUR_BITS,
808 HASH_POOLS_MAX_BITS,
809 HASH_POOLS_BKT_BITS, 0,
810 CFS_HASH_MIN_THETA,
811 CFS_HASH_MAX_THETA,
812 &pool_hash_operations,
813 CFS_HASH_DEFAULT);
814 INIT_LIST_HEAD(&lov->lov_pool_list);
815 lov->lov_pool_count = 0;
816 rc = lov_ost_pool_init(&lov->lov_packed, 0);
817 if (rc)
818 GOTO(out, rc);
819
820 lprocfs_lov_init_vars(&lvars);
821 lprocfs_obd_setup(obd, lvars.obd_vars);
f267cdb4 822#if defined (CONFIG_PROC_FS)
d7e09d03 823 {
5907838a 824 int rc1;
d7e09d03 825
5907838a 826 rc1 = lprocfs_seq_create(obd->obd_proc_entry, "target_obd",
d7e09d03 827 0444, &lov_proc_target_fops, obd);
5907838a 828 if (rc1)
d7e09d03
PT
829 CWARN("Error adding the target_obd file\n");
830 }
831#endif
832 lov->lov_pool_proc_entry = lprocfs_register("pools",
833 obd->obd_proc_entry,
834 NULL, NULL);
835
0a3bdb00 836 return 0;
d7e09d03
PT
837
838out:
839 return rc;
840}
841
842static int lov_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
843{
844 int rc = 0;
845 struct lov_obd *lov = &obd->u.lov;
846
d7e09d03
PT
847 switch (stage) {
848 case OBD_CLEANUP_EARLY: {
849 int i;
850 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
851 if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_active)
852 continue;
853 obd_precleanup(class_exp2obd(lov->lov_tgts[i]->ltd_exp),
854 OBD_CLEANUP_EARLY);
855 }
856 break;
857 }
a5d490c2 858 default:
d7e09d03
PT
859 break;
860 }
a5d490c2 861
0a3bdb00 862 return rc;
d7e09d03
PT
863}
864
865static int lov_cleanup(struct obd_device *obd)
866{
867 struct lov_obd *lov = &obd->u.lov;
868 struct list_head *pos, *tmp;
869 struct pool_desc *pool;
d7e09d03
PT
870
871 list_for_each_safe(pos, tmp, &lov->lov_pool_list) {
872 pool = list_entry(pos, struct pool_desc, pool_list);
873 /* free pool structs */
874 CDEBUG(D_INFO, "delete pool %p\n", pool);
875 /* In the function below, .hs_keycmp resolves to
876 * pool_hashkey_keycmp() */
877 /* coverity[overrun-buffer-val] */
878 lov_pool_del(obd, pool->pool_name);
879 }
880 cfs_hash_putref(lov->lov_pools_hash_body);
881 lov_ost_pool_free(&lov->lov_packed);
882
883 lprocfs_obd_cleanup(obd);
884 if (lov->lov_tgts) {
885 int i;
886 obd_getref(obd);
887 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
888 if (!lov->lov_tgts[i])
889 continue;
890
891 /* Inactive targets may never have connected */
892 if (lov->lov_tgts[i]->ltd_active ||
893 atomic_read(&lov->lov_refcount))
894 /* We should never get here - these
895 should have been removed in the
896 disconnect. */
897 CERROR("lov tgt %d not cleaned!"
898 " deathrow=%d, lovrc=%d\n",
899 i, lov->lov_death_row,
900 atomic_read(&lov->lov_refcount));
e6be8e63 901 lov_del_target(obd, i, NULL, 0);
d7e09d03
PT
902 }
903 obd_putref(obd);
904 OBD_FREE(lov->lov_tgts, sizeof(*lov->lov_tgts) *
905 lov->lov_tgt_size);
906 lov->lov_tgt_size = 0;
907 }
0a3bdb00 908 return 0;
d7e09d03
PT
909}
910
911int lov_process_config_base(struct obd_device *obd, struct lustre_cfg *lcfg,
912 __u32 *indexp, int *genp)
913{
914 struct obd_uuid obd_uuid;
915 int cmd;
916 int rc = 0;
d7e09d03 917
5f3a68f9 918 switch (cmd = lcfg->lcfg_command) {
d7e09d03
PT
919 case LCFG_LOV_ADD_OBD:
920 case LCFG_LOV_ADD_INA:
921 case LCFG_LOV_DEL_OBD: {
922 __u32 index;
923 int gen;
924 /* lov_modify_tgts add 0:lov_mdsA 1:ost1_UUID 2:0 3:1 */
925 if (LUSTRE_CFG_BUFLEN(lcfg, 1) > sizeof(obd_uuid.uuid))
926 GOTO(out, rc = -EINVAL);
927
928 obd_str2uuid(&obd_uuid, lustre_cfg_buf(lcfg, 1));
929
930 if (sscanf(lustre_cfg_buf(lcfg, 2), "%d", indexp) != 1)
931 GOTO(out, rc = -EINVAL);
932 if (sscanf(lustre_cfg_buf(lcfg, 3), "%d", genp) != 1)
933 GOTO(out, rc = -EINVAL);
934 index = *indexp;
935 gen = *genp;
936 if (cmd == LCFG_LOV_ADD_OBD)
937 rc = lov_add_target(obd, &obd_uuid, index, gen, 1);
938 else if (cmd == LCFG_LOV_ADD_INA)
939 rc = lov_add_target(obd, &obd_uuid, index, gen, 0);
940 else
941 rc = lov_del_target(obd, index, &obd_uuid, gen);
942 GOTO(out, rc);
943 }
944 case LCFG_PARAM: {
e6be8e63 945 struct lprocfs_static_vars lvars = { NULL };
d7e09d03
PT
946 struct lov_desc *desc = &(obd->u.lov.desc);
947
948 if (!desc)
949 GOTO(out, rc = -EINVAL);
950
951 lprocfs_lov_init_vars(&lvars);
952
953 rc = class_process_proc_param(PARAM_LOV, lvars.obd_vars,
954 lcfg, obd);
955 if (rc > 0)
956 rc = 0;
957 GOTO(out, rc);
958 }
959 case LCFG_POOL_NEW:
960 case LCFG_POOL_ADD:
961 case LCFG_POOL_DEL:
962 case LCFG_POOL_REM:
963 GOTO(out, rc);
964
965 default: {
966 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
967 GOTO(out, rc = -EINVAL);
968
969 }
970 }
971out:
0a3bdb00 972 return rc;
d7e09d03
PT
973}
974
975static int lov_recreate(struct obd_export *exp, struct obdo *src_oa,
976 struct lov_stripe_md **ea, struct obd_trans_info *oti)
977{
978 struct lov_stripe_md *obj_mdp, *lsm;
979 struct lov_obd *lov = &exp->exp_obd->u.lov;
980 unsigned ost_idx;
981 int rc, i;
d7e09d03
PT
982
983 LASSERT(src_oa->o_valid & OBD_MD_FLFLAGS &&
984 src_oa->o_flags & OBD_FL_RECREATE_OBJS);
985
986 OBD_ALLOC(obj_mdp, sizeof(*obj_mdp));
987 if (obj_mdp == NULL)
0a3bdb00 988 return -ENOMEM;
d7e09d03
PT
989
990 ost_idx = src_oa->o_nlink;
991 lsm = *ea;
992 if (lsm == NULL)
993 GOTO(out, rc = -EINVAL);
994 if (ost_idx >= lov->desc.ld_tgt_count ||
995 !lov->lov_tgts[ost_idx])
996 GOTO(out, rc = -EINVAL);
997
998 for (i = 0; i < lsm->lsm_stripe_count; i++) {
999 if (lsm->lsm_oinfo[i]->loi_ost_idx == ost_idx) {
1000 if (ostid_id(&lsm->lsm_oinfo[i]->loi_oi) !=
1001 ostid_id(&src_oa->o_oi))
1002 GOTO(out, rc = -EINVAL);
1003 break;
1004 }
1005 }
1006 if (i == lsm->lsm_stripe_count)
1007 GOTO(out, rc = -EINVAL);
1008
1009 rc = obd_create(NULL, lov->lov_tgts[ost_idx]->ltd_exp,
1010 src_oa, &obj_mdp, oti);
1011out:
1012 OBD_FREE(obj_mdp, sizeof(*obj_mdp));
0a3bdb00 1013 return rc;
d7e09d03
PT
1014}
1015
1016/* the LOV expects oa->o_id to be set to the LOV object id */
1017static int lov_create(const struct lu_env *env, struct obd_export *exp,
1018 struct obdo *src_oa, struct lov_stripe_md **ea,
1019 struct obd_trans_info *oti)
1020{
1021 struct lov_obd *lov;
1022 int rc = 0;
d7e09d03
PT
1023
1024 LASSERT(ea != NULL);
1025 if (exp == NULL)
0a3bdb00 1026 return -EINVAL;
d7e09d03
PT
1027
1028 if ((src_oa->o_valid & OBD_MD_FLFLAGS) &&
1029 src_oa->o_flags == OBD_FL_DELORPHAN) {
1030 /* should be used with LOV anymore */
1031 LBUG();
1032 }
1033
1034 lov = &exp->exp_obd->u.lov;
1035 if (!lov->desc.ld_active_tgt_count)
0a3bdb00 1036 return -EIO;
d7e09d03
PT
1037
1038 obd_getref(exp->exp_obd);
1039 /* Recreate a specific object id at the given OST index */
1040 if ((src_oa->o_valid & OBD_MD_FLFLAGS) &&
1041 (src_oa->o_flags & OBD_FL_RECREATE_OBJS)) {
1042 rc = lov_recreate(exp, src_oa, ea, oti);
1043 }
1044
1045 obd_putref(exp->exp_obd);
0a3bdb00 1046 return rc;
d7e09d03
PT
1047}
1048
1049#define ASSERT_LSM_MAGIC(lsmp) \
1050do { \
1051 LASSERT((lsmp) != NULL); \
1052 LASSERTF(((lsmp)->lsm_magic == LOV_MAGIC_V1 || \
1053 (lsmp)->lsm_magic == LOV_MAGIC_V3), \
1054 "%p->lsm_magic=%x\n", (lsmp), (lsmp)->lsm_magic); \
1055} while (0)
1056
1057static int lov_destroy(const struct lu_env *env, struct obd_export *exp,
1058 struct obdo *oa, struct lov_stripe_md *lsm,
1059 struct obd_trans_info *oti, struct obd_export *md_exp,
1060 void *capa)
1061{
1062 struct lov_request_set *set;
1063 struct obd_info oinfo;
1064 struct lov_request *req;
1065 struct list_head *pos;
1066 struct lov_obd *lov;
1067 int rc = 0, err = 0;
d7e09d03
PT
1068
1069 ASSERT_LSM_MAGIC(lsm);
1070
1071 if (!exp || !exp->exp_obd)
0a3bdb00 1072 return -ENODEV;
d7e09d03
PT
1073
1074 if (oa->o_valid & OBD_MD_FLCOOKIE) {
1075 LASSERT(oti);
1076 LASSERT(oti->oti_logcookies);
1077 }
1078
1079 lov = &exp->exp_obd->u.lov;
1080 obd_getref(exp->exp_obd);
1081 rc = lov_prep_destroy_set(exp, &oinfo, oa, lsm, oti, &set);
1082 if (rc)
1083 GOTO(out, rc);
1084
66e7a94a 1085 list_for_each(pos, &set->set_list) {
d7e09d03
PT
1086 req = list_entry(pos, struct lov_request, rq_link);
1087
1088 if (oa->o_valid & OBD_MD_FLCOOKIE)
1089 oti->oti_logcookies = set->set_cookies + req->rq_stripe;
1090
1091 err = obd_destroy(env, lov->lov_tgts[req->rq_idx]->ltd_exp,
1092 req->rq_oi.oi_oa, NULL, oti, NULL, capa);
1093 err = lov_update_common_set(set, req, err);
1094 if (err) {
1095 CERROR("%s: destroying objid "DOSTID" subobj "
1096 DOSTID" on OST idx %d: rc = %d\n",
1097 exp->exp_obd->obd_name, POSTID(&oa->o_oi),
1098 POSTID(&req->rq_oi.oi_oa->o_oi),
1099 req->rq_idx, err);
1100 if (!rc)
1101 rc = err;
1102 }
1103 }
1104
1105 if (rc == 0) {
1106 LASSERT(lsm_op_find(lsm->lsm_magic) != NULL);
1107 rc = lsm_op_find(lsm->lsm_magic)->lsm_destroy(lsm, oa, md_exp);
1108 }
1109 err = lov_fini_destroy_set(set);
1110out:
1111 obd_putref(exp->exp_obd);
0a3bdb00 1112 return rc ? rc : err;
d7e09d03
PT
1113}
1114
d7e09d03
PT
1115static int lov_getattr_interpret(struct ptlrpc_request_set *rqset,
1116 void *data, int rc)
1117{
1118 struct lov_request_set *lovset = (struct lov_request_set *)data;
1119 int err;
d7e09d03 1120
1208bcd8 1121 /* don't do attribute merge if this async op failed */
d7e09d03
PT
1122 if (rc)
1123 atomic_set(&lovset->set_completes, 0);
1124 err = lov_fini_getattr_set(lovset);
0a3bdb00 1125 return rc ? rc : err;
d7e09d03
PT
1126}
1127
1128static int lov_getattr_async(struct obd_export *exp, struct obd_info *oinfo,
1129 struct ptlrpc_request_set *rqset)
1130{
1131 struct lov_request_set *lovset;
1132 struct lov_obd *lov;
1133 struct list_head *pos;
1134 struct lov_request *req;
1135 int rc = 0, err;
d7e09d03
PT
1136
1137 LASSERT(oinfo);
1138 ASSERT_LSM_MAGIC(oinfo->oi_md);
1139
1140 if (!exp || !exp->exp_obd)
0a3bdb00 1141 return -ENODEV;
d7e09d03
PT
1142
1143 lov = &exp->exp_obd->u.lov;
1144
1145 rc = lov_prep_getattr_set(exp, oinfo, &lovset);
1146 if (rc)
0a3bdb00 1147 return rc;
d7e09d03
PT
1148
1149 CDEBUG(D_INFO, "objid "DOSTID": %ux%u byte stripes\n",
1150 POSTID(&oinfo->oi_md->lsm_oi), oinfo->oi_md->lsm_stripe_count,
1151 oinfo->oi_md->lsm_stripe_size);
1152
1153 list_for_each(pos, &lovset->set_list) {
1154 req = list_entry(pos, struct lov_request, rq_link);
1155
1156 CDEBUG(D_INFO, "objid "DOSTID"[%d] has subobj "DOSTID" at idx"
1157 "%u\n", POSTID(&oinfo->oi_oa->o_oi), req->rq_stripe,
1158 POSTID(&req->rq_oi.oi_oa->o_oi), req->rq_idx);
1159 rc = obd_getattr_async(lov->lov_tgts[req->rq_idx]->ltd_exp,
1160 &req->rq_oi, rqset);
1161 if (rc) {
1162 CERROR("%s: getattr objid "DOSTID" subobj"
1163 DOSTID" on OST idx %d: rc = %d\n",
1164 exp->exp_obd->obd_name,
1165 POSTID(&oinfo->oi_oa->o_oi),
1166 POSTID(&req->rq_oi.oi_oa->o_oi),
1167 req->rq_idx, rc);
1168 GOTO(out, rc);
1169 }
1170 }
1171
1172 if (!list_empty(&rqset->set_requests)) {
1173 LASSERT(rc == 0);
66e7a94a 1174 LASSERT(rqset->set_interpret == NULL);
d7e09d03
PT
1175 rqset->set_interpret = lov_getattr_interpret;
1176 rqset->set_arg = (void *)lovset;
0a3bdb00 1177 return rc;
d7e09d03
PT
1178 }
1179out:
1180 if (rc)
1181 atomic_set(&lovset->set_completes, 0);
1182 err = lov_fini_getattr_set(lovset);
0a3bdb00 1183 return rc ? rc : err;
d7e09d03
PT
1184}
1185
d7e09d03
PT
1186static int lov_setattr_interpret(struct ptlrpc_request_set *rqset,
1187 void *data, int rc)
1188{
1189 struct lov_request_set *lovset = (struct lov_request_set *)data;
1190 int err;
d7e09d03
PT
1191
1192 if (rc)
1193 atomic_set(&lovset->set_completes, 0);
1194 err = lov_fini_setattr_set(lovset);
0a3bdb00 1195 return rc ? rc : err;
d7e09d03
PT
1196}
1197
1198/* If @oti is given, the request goes from MDS and responses from OSTs are not
1199 needed. Otherwise, a client is waiting for responses. */
1200static int lov_setattr_async(struct obd_export *exp, struct obd_info *oinfo,
1201 struct obd_trans_info *oti,
1202 struct ptlrpc_request_set *rqset)
1203{
1204 struct lov_request_set *set;
1205 struct lov_request *req;
1206 struct list_head *pos;
1207 struct lov_obd *lov;
1208 int rc = 0;
d7e09d03
PT
1209
1210 LASSERT(oinfo);
1211 ASSERT_LSM_MAGIC(oinfo->oi_md);
1212 if (oinfo->oi_oa->o_valid & OBD_MD_FLCOOKIE) {
1213 LASSERT(oti);
1214 LASSERT(oti->oti_logcookies);
1215 }
1216
1217 if (!exp || !exp->exp_obd)
0a3bdb00 1218 return -ENODEV;
d7e09d03
PT
1219
1220 lov = &exp->exp_obd->u.lov;
1221 rc = lov_prep_setattr_set(exp, oinfo, oti, &set);
1222 if (rc)
0a3bdb00 1223 return rc;
d7e09d03
PT
1224
1225 CDEBUG(D_INFO, "objid "DOSTID": %ux%u byte stripes\n",
1226 POSTID(&oinfo->oi_md->lsm_oi),
1227 oinfo->oi_md->lsm_stripe_count,
1228 oinfo->oi_md->lsm_stripe_size);
1229
1230 list_for_each(pos, &set->set_list) {
1231 req = list_entry(pos, struct lov_request, rq_link);
1232
1233 if (oinfo->oi_oa->o_valid & OBD_MD_FLCOOKIE)
1234 oti->oti_logcookies = set->set_cookies + req->rq_stripe;
1235
1236 CDEBUG(D_INFO, "objid "DOSTID"[%d] has subobj "DOSTID" at idx"
1237 "%u\n", POSTID(&oinfo->oi_oa->o_oi), req->rq_stripe,
1238 POSTID(&req->rq_oi.oi_oa->o_oi), req->rq_idx);
1239
1240 rc = obd_setattr_async(lov->lov_tgts[req->rq_idx]->ltd_exp,
1241 &req->rq_oi, oti, rqset);
1242 if (rc) {
1243 CERROR("error: setattr objid "DOSTID" subobj"
1244 DOSTID" on OST idx %d: rc = %d\n",
1245 POSTID(&set->set_oi->oi_oa->o_oi),
1246 POSTID(&req->rq_oi.oi_oa->o_oi),
1247 req->rq_idx, rc);
1248 break;
1249 }
1250 }
1251
1252 /* If we are not waiting for responses on async requests, return. */
1253 if (rc || !rqset || list_empty(&rqset->set_requests)) {
1254 int err;
1255 if (rc)
1256 atomic_set(&set->set_completes, 0);
1257 err = lov_fini_setattr_set(set);
0a3bdb00 1258 return rc ? rc : err;
d7e09d03
PT
1259 }
1260
1261 LASSERT(rqset->set_interpret == NULL);
1262 rqset->set_interpret = lov_setattr_interpret;
1263 rqset->set_arg = (void *)set;
1264
0a3bdb00 1265 return 0;
d7e09d03
PT
1266}
1267
d7e09d03
PT
1268/* find any ldlm lock of the inode in lov
1269 * return 0 not find
1270 * 1 find one
1271 * < 0 error */
1272static int lov_find_cbdata(struct obd_export *exp,
1273 struct lov_stripe_md *lsm, ldlm_iterator_t it,
1274 void *data)
1275{
1276 struct lov_obd *lov;
1277 int rc = 0, i;
d7e09d03
PT
1278
1279 ASSERT_LSM_MAGIC(lsm);
1280
1281 if (!exp || !exp->exp_obd)
0a3bdb00 1282 return -ENODEV;
d7e09d03
PT
1283
1284 lov = &exp->exp_obd->u.lov;
1285 for (i = 0; i < lsm->lsm_stripe_count; i++) {
1286 struct lov_stripe_md submd;
1287 struct lov_oinfo *loi = lsm->lsm_oinfo[i];
1288
1289 if (!lov->lov_tgts[loi->loi_ost_idx]) {
1290 CDEBUG(D_HA, "lov idx %d NULL \n", loi->loi_ost_idx);
1291 continue;
1292 }
1293 submd.lsm_oi = loi->loi_oi;
1294 submd.lsm_stripe_count = 0;
1295 rc = obd_find_cbdata(lov->lov_tgts[loi->loi_ost_idx]->ltd_exp,
1296 &submd, it, data);
1297 if (rc != 0)
0a3bdb00 1298 return rc;
d7e09d03 1299 }
0a3bdb00 1300 return rc;
d7e09d03
PT
1301}
1302
d7e09d03
PT
1303int lov_statfs_interpret(struct ptlrpc_request_set *rqset, void *data, int rc)
1304{
1305 struct lov_request_set *lovset = (struct lov_request_set *)data;
1306 int err;
d7e09d03
PT
1307
1308 if (rc)
1309 atomic_set(&lovset->set_completes, 0);
1310
1311 err = lov_fini_statfs_set(lovset);
0a3bdb00 1312 return rc ? rc : err;
d7e09d03
PT
1313}
1314
1315static int lov_statfs_async(struct obd_export *exp, struct obd_info *oinfo,
1316 __u64 max_age, struct ptlrpc_request_set *rqset)
1317{
1318 struct obd_device *obd = class_exp2obd(exp);
1319 struct lov_request_set *set;
1320 struct lov_request *req;
1321 struct list_head *pos;
1322 struct lov_obd *lov;
1323 int rc = 0;
d7e09d03
PT
1324
1325 LASSERT(oinfo != NULL);
1326 LASSERT(oinfo->oi_osfs != NULL);
1327
1328 lov = &obd->u.lov;
1329 rc = lov_prep_statfs_set(obd, oinfo, &set);
1330 if (rc)
0a3bdb00 1331 return rc;
d7e09d03 1332
66e7a94a 1333 list_for_each(pos, &set->set_list) {
d7e09d03
PT
1334 req = list_entry(pos, struct lov_request, rq_link);
1335 rc = obd_statfs_async(lov->lov_tgts[req->rq_idx]->ltd_exp,
1336 &req->rq_oi, max_age, rqset);
1337 if (rc)
1338 break;
1339 }
1340
1341 if (rc || list_empty(&rqset->set_requests)) {
1342 int err;
1343 if (rc)
1344 atomic_set(&set->set_completes, 0);
1345 err = lov_fini_statfs_set(set);
0a3bdb00 1346 return rc ? rc : err;
d7e09d03
PT
1347 }
1348
1349 LASSERT(rqset->set_interpret == NULL);
1350 rqset->set_interpret = lov_statfs_interpret;
1351 rqset->set_arg = (void *)set;
0a3bdb00 1352 return 0;
d7e09d03
PT
1353}
1354
1355static int lov_statfs(const struct lu_env *env, struct obd_export *exp,
1356 struct obd_statfs *osfs, __u64 max_age, __u32 flags)
1357{
1358 struct ptlrpc_request_set *set = NULL;
1359 struct obd_info oinfo = { { { 0 } } };
1360 int rc = 0;
d7e09d03
PT
1361
1362 /* for obdclass we forbid using obd_statfs_rqset, but prefer using async
1363 * statfs requests */
1364 set = ptlrpc_prep_set();
1365 if (set == NULL)
0a3bdb00 1366 return -ENOMEM;
d7e09d03
PT
1367
1368 oinfo.oi_osfs = osfs;
1369 oinfo.oi_flags = flags;
1370 rc = lov_statfs_async(exp, &oinfo, max_age, set);
1371 if (rc == 0)
1372 rc = ptlrpc_set_wait(set);
1373 ptlrpc_set_destroy(set);
1374
0a3bdb00 1375 return rc;
d7e09d03
PT
1376}
1377
1378static int lov_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1379 void *karg, void *uarg)
1380{
1381 struct obd_device *obddev = class_exp2obd(exp);
1382 struct lov_obd *lov = &obddev->u.lov;
1383 int i = 0, rc = 0, count = lov->desc.ld_tgt_count;
1384 struct obd_uuid *uuidp;
d7e09d03
PT
1385
1386 switch (cmd) {
1387 case IOC_OBD_STATFS: {
1388 struct obd_ioctl_data *data = karg;
1389 struct obd_device *osc_obd;
1390 struct obd_statfs stat_buf = {0};
1391 __u32 index;
1392 __u32 flags;
1393
1394 memcpy(&index, data->ioc_inlbuf2, sizeof(__u32));
1395 if ((index >= count))
0a3bdb00 1396 return -ENODEV;
d7e09d03
PT
1397
1398 if (!lov->lov_tgts[index])
1399 /* Try again with the next index */
0a3bdb00 1400 return -EAGAIN;
d7e09d03 1401 if (!lov->lov_tgts[index]->ltd_active)
0a3bdb00 1402 return -ENODATA;
d7e09d03
PT
1403
1404 osc_obd = class_exp2obd(lov->lov_tgts[index]->ltd_exp);
1405 if (!osc_obd)
0a3bdb00 1406 return -EINVAL;
d7e09d03
PT
1407
1408 /* copy UUID */
1409 if (copy_to_user(data->ioc_pbuf2, obd2cli_tgt(osc_obd),
1410 min((int) data->ioc_plen2,
1411 (int) sizeof(struct obd_uuid))))
0a3bdb00 1412 return -EFAULT;
d7e09d03 1413
bcc3b704 1414 flags = uarg ? *(__u32 *)uarg : 0;
d7e09d03
PT
1415 /* got statfs data */
1416 rc = obd_statfs(NULL, lov->lov_tgts[index]->ltd_exp, &stat_buf,
1417 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
1418 flags);
1419 if (rc)
0a3bdb00 1420 return rc;
d7e09d03
PT
1421 if (copy_to_user(data->ioc_pbuf1, &stat_buf,
1422 min((int) data->ioc_plen1,
1423 (int) sizeof(stat_buf))))
0a3bdb00 1424 return -EFAULT;
d7e09d03
PT
1425 break;
1426 }
1427 case OBD_IOC_LOV_GET_CONFIG: {
1428 struct obd_ioctl_data *data;
1429 struct lov_desc *desc;
1430 char *buf = NULL;
1431 __u32 *genp;
1432
1433 len = 0;
1434 if (obd_ioctl_getdata(&buf, &len, (void *)uarg))
0a3bdb00 1435 return -EINVAL;
d7e09d03
PT
1436
1437 data = (struct obd_ioctl_data *)buf;
1438
1439 if (sizeof(*desc) > data->ioc_inllen1) {
1440 obd_ioctl_freedata(buf, len);
0a3bdb00 1441 return -EINVAL;
d7e09d03
PT
1442 }
1443
1444 if (sizeof(uuidp->uuid) * count > data->ioc_inllen2) {
1445 obd_ioctl_freedata(buf, len);
0a3bdb00 1446 return -EINVAL;
d7e09d03
PT
1447 }
1448
1449 if (sizeof(__u32) * count > data->ioc_inllen3) {
1450 obd_ioctl_freedata(buf, len);
0a3bdb00 1451 return -EINVAL;
d7e09d03
PT
1452 }
1453
1454 desc = (struct lov_desc *)data->ioc_inlbuf1;
1455 memcpy(desc, &(lov->desc), sizeof(*desc));
1456
1457 uuidp = (struct obd_uuid *)data->ioc_inlbuf2;
1458 genp = (__u32 *)data->ioc_inlbuf3;
1459 /* the uuid will be empty for deleted OSTs */
1460 for (i = 0; i < count; i++, uuidp++, genp++) {
1461 if (!lov->lov_tgts[i])
1462 continue;
1463 *uuidp = lov->lov_tgts[i]->ltd_uuid;
1464 *genp = lov->lov_tgts[i]->ltd_gen;
1465 }
1466
1467 if (copy_to_user((void *)uarg, buf, len))
1468 rc = -EFAULT;
1469 obd_ioctl_freedata(buf, len);
1470 break;
1471 }
d7e09d03
PT
1472 case LL_IOC_LOV_GETSTRIPE:
1473 rc = lov_getstripe(exp, karg, uarg);
1474 break;
d7e09d03
PT
1475 case OBD_IOC_QUOTACTL: {
1476 struct if_quotactl *qctl = karg;
1477 struct lov_tgt_desc *tgt = NULL;
1478 struct obd_quotactl *oqctl;
1479
1480 if (qctl->qc_valid == QC_OSTIDX) {
1481 if (qctl->qc_idx < 0 || count <= qctl->qc_idx)
0a3bdb00 1482 return -EINVAL;
d7e09d03
PT
1483
1484 tgt = lov->lov_tgts[qctl->qc_idx];
1485 if (!tgt || !tgt->ltd_exp)
0a3bdb00 1486 return -EINVAL;
d7e09d03
PT
1487 } else if (qctl->qc_valid == QC_UUID) {
1488 for (i = 0; i < count; i++) {
1489 tgt = lov->lov_tgts[i];
1490 if (!tgt ||
1491 !obd_uuid_equals(&tgt->ltd_uuid,
1492 &qctl->obd_uuid))
1493 continue;
1494
1495 if (tgt->ltd_exp == NULL)
0a3bdb00 1496 return -EINVAL;
d7e09d03
PT
1497
1498 break;
1499 }
1500 } else {
0a3bdb00 1501 return -EINVAL;
d7e09d03
PT
1502 }
1503
1504 if (i >= count)
0a3bdb00 1505 return -EAGAIN;
d7e09d03
PT
1506
1507 LASSERT(tgt && tgt->ltd_exp);
1508 OBD_ALLOC_PTR(oqctl);
1509 if (!oqctl)
0a3bdb00 1510 return -ENOMEM;
d7e09d03
PT
1511
1512 QCTL_COPY(oqctl, qctl);
1513 rc = obd_quotactl(tgt->ltd_exp, oqctl);
1514 if (rc == 0) {
1515 QCTL_COPY(qctl, oqctl);
1516 qctl->qc_valid = QC_OSTIDX;
1517 qctl->obd_uuid = tgt->ltd_uuid;
1518 }
1519 OBD_FREE_PTR(oqctl);
1520 break;
1521 }
1522 default: {
1523 int set = 0;
1524
1525 if (count == 0)
0a3bdb00 1526 return -ENOTTY;
d7e09d03
PT
1527
1528 for (i = 0; i < count; i++) {
1529 int err;
1530 struct obd_device *osc_obd;
1531
1532 /* OST was disconnected */
1533 if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_exp)
1534 continue;
1535
1536 /* ll_umount_begin() sets force flag but for lov, not
1537 * osc. Let's pass it through */
1538 osc_obd = class_exp2obd(lov->lov_tgts[i]->ltd_exp);
1539 osc_obd->obd_force = obddev->obd_force;
1540 err = obd_iocontrol(cmd, lov->lov_tgts[i]->ltd_exp,
1541 len, karg, uarg);
1542 if (err == -ENODATA && cmd == OBD_IOC_POLL_QUOTACHECK) {
0a3bdb00 1543 return err;
d7e09d03
PT
1544 } else if (err) {
1545 if (lov->lov_tgts[i]->ltd_active) {
1546 CDEBUG(err == -ENOTTY ?
1547 D_IOCTL : D_WARNING,
1548 "iocontrol OSC %s on OST "
1549 "idx %d cmd %x: err = %d\n",
1550 lov_uuid2str(lov, i),
1551 i, cmd, err);
1552 if (!rc)
1553 rc = err;
1554 }
1555 } else {
1556 set = 1;
1557 }
1558 }
1559 if (!set && !rc)
1560 rc = -EIO;
1561 }
1562 }
1563
0a3bdb00 1564 return rc;
d7e09d03
PT
1565}
1566
1567#define FIEMAP_BUFFER_SIZE 4096
1568
1569/**
1570 * Non-zero fe_logical indicates that this is a continuation FIEMAP
1571 * call. The local end offset and the device are sent in the first
1572 * fm_extent. This function calculates the stripe number from the index.
1573 * This function returns a stripe_no on which mapping is to be restarted.
1574 *
1575 * This function returns fm_end_offset which is the in-OST offset at which
1576 * mapping should be restarted. If fm_end_offset=0 is returned then caller
1577 * will re-calculate proper offset in next stripe.
1578 * Note that the first extent is passed to lov_get_info via the value field.
1579 *
1580 * \param fiemap fiemap request header
1581 * \param lsm striping information for the file
1582 * \param fm_start logical start of mapping
1583 * \param fm_end logical end of mapping
1584 * \param start_stripe starting stripe will be returned in this
1585 */
21aef7d9
OD
1586u64 fiemap_calc_fm_end_offset(struct ll_user_fiemap *fiemap,
1587 struct lov_stripe_md *lsm, u64 fm_start,
1588 u64 fm_end, int *start_stripe)
d7e09d03 1589{
21aef7d9
OD
1590 u64 local_end = fiemap->fm_extents[0].fe_logical;
1591 u64 lun_start, lun_end;
1592 u64 fm_end_offset;
d7e09d03
PT
1593 int stripe_no = -1, i;
1594
1595 if (fiemap->fm_extent_count == 0 ||
1596 fiemap->fm_extents[0].fe_logical == 0)
1597 return 0;
1598
1599 /* Find out stripe_no from ost_index saved in the fe_device */
1600 for (i = 0; i < lsm->lsm_stripe_count; i++) {
1601 if (lsm->lsm_oinfo[i]->loi_ost_idx ==
1602 fiemap->fm_extents[0].fe_device) {
1603 stripe_no = i;
1604 break;
1605 }
1606 }
1607 if (stripe_no == -1)
1608 return -EINVAL;
1609
1610 /* If we have finished mapping on previous device, shift logical
1611 * offset to start of next device */
1612 if ((lov_stripe_intersects(lsm, stripe_no, fm_start, fm_end,
1613 &lun_start, &lun_end)) != 0 &&
1614 local_end < lun_end) {
1615 fm_end_offset = local_end;
1616 *start_stripe = stripe_no;
1617 } else {
1618 /* This is a special value to indicate that caller should
1619 * calculate offset in next stripe. */
1620 fm_end_offset = 0;
1621 *start_stripe = (stripe_no + 1) % lsm->lsm_stripe_count;
1622 }
1623
1624 return fm_end_offset;
1625}
1626
1627/**
1628 * We calculate on which OST the mapping will end. If the length of mapping
1629 * is greater than (stripe_size * stripe_count) then the last_stripe will
1630 * will be one just before start_stripe. Else we check if the mapping
1631 * intersects each OST and find last_stripe.
1632 * This function returns the last_stripe and also sets the stripe_count
1633 * over which the mapping is spread
1634 *
1635 * \param lsm striping information for the file
1636 * \param fm_start logical start of mapping
1637 * \param fm_end logical end of mapping
1638 * \param start_stripe starting stripe of the mapping
1639 * \param stripe_count the number of stripes across which to map is returned
1640 *
1641 * \retval last_stripe return the last stripe of the mapping
1642 */
21aef7d9
OD
1643int fiemap_calc_last_stripe(struct lov_stripe_md *lsm, u64 fm_start,
1644 u64 fm_end, int start_stripe,
d7e09d03
PT
1645 int *stripe_count)
1646{
1647 int last_stripe;
21aef7d9 1648 u64 obd_start, obd_end;
d7e09d03
PT
1649 int i, j;
1650
1651 if (fm_end - fm_start > lsm->lsm_stripe_size * lsm->lsm_stripe_count) {
1652 last_stripe = (start_stripe < 1 ? lsm->lsm_stripe_count - 1 :
1653 start_stripe - 1);
1654 *stripe_count = lsm->lsm_stripe_count;
1655 } else {
1656 for (j = 0, i = start_stripe; j < lsm->lsm_stripe_count;
1657 i = (i + 1) % lsm->lsm_stripe_count, j++) {
1658 if ((lov_stripe_intersects(lsm, i, fm_start, fm_end,
1659 &obd_start, &obd_end)) == 0)
1660 break;
1661 }
1662 *stripe_count = j;
1663 last_stripe = (start_stripe + j - 1) %lsm->lsm_stripe_count;
1664 }
1665
1666 return last_stripe;
1667}
1668
1669/**
1670 * Set fe_device and copy extents from local buffer into main return buffer.
1671 *
1672 * \param fiemap fiemap request header
1673 * \param lcl_fm_ext array of local fiemap extents to be copied
1674 * \param ost_index OST index to be written into the fm_device field for each
1675 extent
1676 * \param ext_count number of extents to be copied
1677 * \param current_extent where to start copying in main extent array
1678 */
1679void fiemap_prepare_and_copy_exts(struct ll_user_fiemap *fiemap,
1680 struct ll_fiemap_extent *lcl_fm_ext,
1681 int ost_index, unsigned int ext_count,
1682 int current_extent)
1683{
1684 char *to;
1685 int ext;
1686
1687 for (ext = 0; ext < ext_count; ext++) {
1688 lcl_fm_ext[ext].fe_device = ost_index;
1689 lcl_fm_ext[ext].fe_flags |= FIEMAP_EXTENT_NET;
1690 }
1691
1692 /* Copy fm_extent's from fm_local to return buffer */
1693 to = (char *)fiemap + fiemap_count_to_size(current_extent);
1694 memcpy(to, lcl_fm_ext, ext_count * sizeof(struct ll_fiemap_extent));
1695}
1696
1697/**
1698 * Break down the FIEMAP request and send appropriate calls to individual OSTs.
1699 * This also handles the restarting of FIEMAP calls in case mapping overflows
1700 * the available number of extents in single call.
1701 */
1702static int lov_fiemap(struct lov_obd *lov, __u32 keylen, void *key,
1703 __u32 *vallen, void *val, struct lov_stripe_md *lsm)
1704{
1705 struct ll_fiemap_info_key *fm_key = key;
1706 struct ll_user_fiemap *fiemap = val;
1707 struct ll_user_fiemap *fm_local = NULL;
1708 struct ll_fiemap_extent *lcl_fm_ext;
1709 int count_local;
1710 unsigned int get_num_extents = 0;
1711 int ost_index = 0, actual_start_stripe, start_stripe;
21aef7d9
OD
1712 u64 fm_start, fm_end, fm_length, fm_end_offset;
1713 u64 curr_loc;
d7e09d03
PT
1714 int current_extent = 0, rc = 0, i;
1715 int ost_eof = 0; /* EOF for object */
1716 int ost_done = 0; /* done with required mapping for this OST? */
1717 int last_stripe;
1718 int cur_stripe = 0, cur_stripe_wrap = 0, stripe_count;
1719 unsigned int buffer_size = FIEMAP_BUFFER_SIZE;
1720
5dd16419 1721 if (!lsm_has_objects(lsm))
d7e09d03
PT
1722 GOTO(out, rc = 0);
1723
1724 if (fiemap_count_to_size(fm_key->fiemap.fm_extent_count) < buffer_size)
1725 buffer_size = fiemap_count_to_size(fm_key->fiemap.fm_extent_count);
1726
1727 OBD_ALLOC_LARGE(fm_local, buffer_size);
1728 if (fm_local == NULL)
1729 GOTO(out, rc = -ENOMEM);
1730 lcl_fm_ext = &fm_local->fm_extents[0];
1731
1732 count_local = fiemap_size_to_count(buffer_size);
1733
1734 memcpy(fiemap, &fm_key->fiemap, sizeof(*fiemap));
1735 fm_start = fiemap->fm_start;
1736 fm_length = fiemap->fm_length;
1737 /* Calculate start stripe, last stripe and length of mapping */
1738 actual_start_stripe = start_stripe = lov_stripe_number(lsm, fm_start);
1739 fm_end = (fm_length == ~0ULL ? fm_key->oa.o_size :
1740 fm_start + fm_length - 1);
1741 /* If fm_length != ~0ULL but fm_start+fm_length-1 exceeds file size */
1742 if (fm_end > fm_key->oa.o_size)
1743 fm_end = fm_key->oa.o_size;
1744
1745 last_stripe = fiemap_calc_last_stripe(lsm, fm_start, fm_end,
1746 actual_start_stripe, &stripe_count);
1747
1748 fm_end_offset = fiemap_calc_fm_end_offset(fiemap, lsm, fm_start,
1749 fm_end, &start_stripe);
1750 if (fm_end_offset == -EINVAL)
1751 GOTO(out, rc = -EINVAL);
1752
ebdc4fc5
BJ
1753 if (fiemap_count_to_size(fiemap->fm_extent_count) > *vallen)
1754 fiemap->fm_extent_count = fiemap_size_to_count(*vallen);
d7e09d03
PT
1755 if (fiemap->fm_extent_count == 0) {
1756 get_num_extents = 1;
1757 count_local = 0;
1758 }
d7e09d03
PT
1759 /* Check each stripe */
1760 for (cur_stripe = start_stripe, i = 0; i < stripe_count;
1761 i++, cur_stripe = (cur_stripe + 1) % lsm->lsm_stripe_count) {
21aef7d9
OD
1762 u64 req_fm_len; /* Stores length of required mapping */
1763 u64 len_mapped_single_call;
1764 u64 lun_start, lun_end, obd_object_end;
d7e09d03
PT
1765 unsigned int ext_count;
1766
1767 cur_stripe_wrap = cur_stripe;
1768
1769 /* Find out range of mapping on this stripe */
1770 if ((lov_stripe_intersects(lsm, cur_stripe, fm_start, fm_end,
1771 &lun_start, &obd_object_end)) == 0)
1772 continue;
1773
1774 /* If this is a continuation FIEMAP call and we are on
1775 * starting stripe then lun_start needs to be set to
1776 * fm_end_offset */
1777 if (fm_end_offset != 0 && cur_stripe == start_stripe)
1778 lun_start = fm_end_offset;
1779
1780 if (fm_length != ~0ULL) {
1781 /* Handle fm_start + fm_length overflow */
1782 if (fm_start + fm_length < fm_start)
1783 fm_length = ~0ULL - fm_start;
1784 lun_end = lov_size_to_stripe(lsm, fm_start + fm_length,
1785 cur_stripe);
1786 } else {
1787 lun_end = ~0ULL;
1788 }
1789
1790 if (lun_start == lun_end)
1791 continue;
1792
1793 req_fm_len = obd_object_end - lun_start;
1794 fm_local->fm_length = 0;
1795 len_mapped_single_call = 0;
1796
1797 /* If the output buffer is very large and the objects have many
1798 * extents we may need to loop on a single OST repeatedly */
1799 ost_eof = 0;
1800 ost_done = 0;
1801 do {
1802 if (get_num_extents == 0) {
1803 /* Don't get too many extents. */
1804 if (current_extent + count_local >
1805 fiemap->fm_extent_count)
1806 count_local = fiemap->fm_extent_count -
1807 current_extent;
1808 }
1809
1810 lun_start += len_mapped_single_call;
1811 fm_local->fm_length = req_fm_len - len_mapped_single_call;
1812 req_fm_len = fm_local->fm_length;
1813 fm_local->fm_extent_count = count_local;
1814 fm_local->fm_mapped_extents = 0;
1815 fm_local->fm_flags = fiemap->fm_flags;
1816
1817 fm_key->oa.o_oi = lsm->lsm_oinfo[cur_stripe]->loi_oi;
1818 ost_index = lsm->lsm_oinfo[cur_stripe]->loi_ost_idx;
1819
1820 if (ost_index < 0 || ost_index >=lov->desc.ld_tgt_count)
1821 GOTO(out, rc = -EINVAL);
1822
1823 /* If OST is inactive, return extent with UNKNOWN flag */
1824 if (!lov->lov_tgts[ost_index]->ltd_active) {
1825 fm_local->fm_flags |= FIEMAP_EXTENT_LAST;
1826 fm_local->fm_mapped_extents = 1;
1827
1828 lcl_fm_ext[0].fe_logical = lun_start;
1829 lcl_fm_ext[0].fe_length = obd_object_end -
1830 lun_start;
1831 lcl_fm_ext[0].fe_flags |= FIEMAP_EXTENT_UNKNOWN;
1832
1833 goto inactive_tgt;
1834 }
1835
1836 fm_local->fm_start = lun_start;
1837 fm_local->fm_flags &= ~FIEMAP_FLAG_DEVICE_ORDER;
1838 memcpy(&fm_key->fiemap, fm_local, sizeof(*fm_local));
1839 *vallen=fiemap_count_to_size(fm_local->fm_extent_count);
1840 rc = obd_get_info(NULL,
1841 lov->lov_tgts[ost_index]->ltd_exp,
1842 keylen, key, vallen, fm_local, lsm);
1843 if (rc != 0)
1844 GOTO(out, rc);
1845
1846inactive_tgt:
1847 ext_count = fm_local->fm_mapped_extents;
1848 if (ext_count == 0) {
1849 ost_done = 1;
1850 /* If last stripe has hole at the end,
1851 * then we need to return */
1852 if (cur_stripe_wrap == last_stripe) {
1853 fiemap->fm_mapped_extents = 0;
1854 goto finish;
1855 }
1856 break;
1857 }
1858
1859 /* If we just need num of extents then go to next device */
1860 if (get_num_extents) {
1861 current_extent += ext_count;
1862 break;
1863 }
1864
1865 len_mapped_single_call = lcl_fm_ext[ext_count-1].fe_logical -
1866 lun_start + lcl_fm_ext[ext_count - 1].fe_length;
1867
1868 /* Have we finished mapping on this device? */
1869 if (req_fm_len <= len_mapped_single_call)
1870 ost_done = 1;
1871
1872 /* Clear the EXTENT_LAST flag which can be present on
1873 * last extent */
1874 if (lcl_fm_ext[ext_count-1].fe_flags & FIEMAP_EXTENT_LAST)
1875 lcl_fm_ext[ext_count - 1].fe_flags &=
1876 ~FIEMAP_EXTENT_LAST;
1877
1878 curr_loc = lov_stripe_size(lsm,
1879 lcl_fm_ext[ext_count - 1].fe_logical+
1880 lcl_fm_ext[ext_count - 1].fe_length,
1881 cur_stripe);
1882 if (curr_loc >= fm_key->oa.o_size)
1883 ost_eof = 1;
1884
1885 fiemap_prepare_and_copy_exts(fiemap, lcl_fm_ext,
1886 ost_index, ext_count,
1887 current_extent);
1888
1889 current_extent += ext_count;
1890
1891 /* Ran out of available extents? */
1892 if (current_extent >= fiemap->fm_extent_count)
1893 goto finish;
1894 } while (ost_done == 0 && ost_eof == 0);
1895
1896 if (cur_stripe_wrap == last_stripe)
1897 goto finish;
1898 }
1899
1900finish:
1901 /* Indicate that we are returning device offsets unless file just has
1902 * single stripe */
1903 if (lsm->lsm_stripe_count > 1)
1904 fiemap->fm_flags |= FIEMAP_FLAG_DEVICE_ORDER;
1905
1906 if (get_num_extents)
1907 goto skip_last_device_calc;
1908
1909 /* Check if we have reached the last stripe and whether mapping for that
1910 * stripe is done. */
1911 if (cur_stripe_wrap == last_stripe) {
1912 if (ost_done || ost_eof)
1913 fiemap->fm_extents[current_extent - 1].fe_flags |=
1914 FIEMAP_EXTENT_LAST;
1915 }
1916
1917skip_last_device_calc:
1918 fiemap->fm_mapped_extents = current_extent;
1919
1920out:
1921 OBD_FREE_LARGE(fm_local, buffer_size);
1922 return rc;
1923}
1924
1925static int lov_get_info(const struct lu_env *env, struct obd_export *exp,
1926 __u32 keylen, void *key, __u32 *vallen, void *val,
1927 struct lov_stripe_md *lsm)
1928{
1929 struct obd_device *obddev = class_exp2obd(exp);
1930 struct lov_obd *lov = &obddev->u.lov;
1931 int i, rc;
d7e09d03
PT
1932
1933 if (!vallen || !val)
0a3bdb00 1934 return -EFAULT;
d7e09d03
PT
1935
1936 obd_getref(obddev);
1937
1938 if (KEY_IS(KEY_LOCK_TO_STRIPE)) {
1939 struct {
1940 char name[16];
1941 struct ldlm_lock *lock;
1942 } *data = key;
1943 struct ldlm_res_id *res_id = &data->lock->l_resource->lr_name;
1944 struct lov_oinfo *loi;
1945 __u32 *stripe = val;
1946
1947 if (*vallen < sizeof(*stripe))
1948 GOTO(out, rc = -EFAULT);
1949 *vallen = sizeof(*stripe);
1950
1951 /* XXX This is another one of those bits that will need to
1952 * change if we ever actually support nested LOVs. It uses
1953 * the lock's export to find out which stripe it is. */
1954 /* XXX - it's assumed all the locks for deleted OSTs have
1955 * been cancelled. Also, the export for deleted OSTs will
1956 * be NULL and won't match the lock's export. */
1957 for (i = 0; i < lsm->lsm_stripe_count; i++) {
1958 loi = lsm->lsm_oinfo[i];
1959 if (!lov->lov_tgts[loi->loi_ost_idx])
1960 continue;
1961 if (lov->lov_tgts[loi->loi_ost_idx]->ltd_exp ==
1962 data->lock->l_conn_export &&
1963 ostid_res_name_eq(&loi->loi_oi, res_id)) {
1964 *stripe = i;
1965 GOTO(out, rc = 0);
1966 }
1967 }
1968 LDLM_ERROR(data->lock, "lock on inode without such object");
1969 dump_lsm(D_ERROR, lsm);
1970 GOTO(out, rc = -ENXIO);
1971 } else if (KEY_IS(KEY_LAST_ID)) {
1972 struct obd_id_info *info = val;
21aef7d9 1973 __u32 size = sizeof(u64);
d7e09d03
PT
1974 struct lov_tgt_desc *tgt;
1975
1976 LASSERT(*vallen == sizeof(struct obd_id_info));
1977 tgt = lov->lov_tgts[info->idx];
1978
1979 if (!tgt || !tgt->ltd_active)
1980 GOTO(out, rc = -ESRCH);
1981
1982 rc = obd_get_info(env, tgt->ltd_exp, keylen, key,
1983 &size, info->data, NULL);
1984 GOTO(out, rc = 0);
1985 } else if (KEY_IS(KEY_LOVDESC)) {
1986 struct lov_desc *desc_ret = val;
1987 *desc_ret = lov->desc;
1988
1989 GOTO(out, rc = 0);
1990 } else if (KEY_IS(KEY_FIEMAP)) {
1991 rc = lov_fiemap(lov, keylen, key, vallen, val, lsm);
1992 GOTO(out, rc);
1993 } else if (KEY_IS(KEY_CONNECT_FLAG)) {
1994 struct lov_tgt_desc *tgt;
bcc3b704 1995 __u64 ost_idx = *((__u64 *)val);
d7e09d03
PT
1996
1997 LASSERT(*vallen == sizeof(__u64));
1998 LASSERT(ost_idx < lov->desc.ld_tgt_count);
1999 tgt = lov->lov_tgts[ost_idx];
2000
2001 if (!tgt || !tgt->ltd_exp)
2002 GOTO(out, rc = -ESRCH);
2003
2004 *((__u64 *)val) = exp_connect_flags(tgt->ltd_exp);
2005 GOTO(out, rc = 0);
2006 } else if (KEY_IS(KEY_TGT_COUNT)) {
2007 *((int *)val) = lov->desc.ld_tgt_count;
2008 GOTO(out, rc = 0);
2009 }
2010
2011 rc = -EINVAL;
2012
2013out:
2014 obd_putref(obddev);
0a3bdb00 2015 return rc;
d7e09d03
PT
2016}
2017
2018static int lov_set_info_async(const struct lu_env *env, struct obd_export *exp,
21aef7d9 2019 u32 keylen, void *key, u32 vallen,
d7e09d03
PT
2020 void *val, struct ptlrpc_request_set *set)
2021{
2022 struct obd_device *obddev = class_exp2obd(exp);
2023 struct lov_obd *lov = &obddev->u.lov;
21aef7d9 2024 u32 count;
d7e09d03
PT
2025 int i, rc = 0, err;
2026 struct lov_tgt_desc *tgt;
2027 unsigned incr, check_uuid,
2028 do_inactive, no_set;
2029 unsigned next_id = 0, mds_con = 0, capa = 0;
d7e09d03
PT
2030
2031 incr = check_uuid = do_inactive = no_set = 0;
2032 if (set == NULL) {
2033 no_set = 1;
2034 set = ptlrpc_prep_set();
2035 if (!set)
0a3bdb00 2036 return -ENOMEM;
d7e09d03
PT
2037 }
2038
2039 obd_getref(obddev);
2040 count = lov->desc.ld_tgt_count;
2041
2042 if (KEY_IS(KEY_NEXT_ID)) {
2043 count = vallen / sizeof(struct obd_id_info);
21aef7d9 2044 vallen = sizeof(u64);
d7e09d03
PT
2045 incr = sizeof(struct obd_id_info);
2046 do_inactive = 1;
2047 next_id = 1;
2048 } else if (KEY_IS(KEY_CHECKSUM)) {
2049 do_inactive = 1;
2050 } else if (KEY_IS(KEY_EVICT_BY_NID)) {
2051 /* use defaults: do_inactive = incr = 0; */
2052 } else if (KEY_IS(KEY_MDS_CONN)) {
2053 mds_con = 1;
2054 } else if (KEY_IS(KEY_CAPA_KEY)) {
2055 capa = 1;
2056 } else if (KEY_IS(KEY_CACHE_SET)) {
2057 LASSERT(lov->lov_cache == NULL);
2058 lov->lov_cache = val;
2059 do_inactive = 1;
2060 }
2061
2062 for (i = 0; i < count; i++, val = (char *)val + incr) {
2063 if (next_id) {
bcc3b704 2064 tgt = lov->lov_tgts[((struct obd_id_info *)val)->idx];
d7e09d03
PT
2065 } else {
2066 tgt = lov->lov_tgts[i];
2067 }
2068 /* OST was disconnected */
2069 if (!tgt || !tgt->ltd_exp)
2070 continue;
2071
2072 /* OST is inactive and we don't want inactive OSCs */
2073 if (!tgt->ltd_active && !do_inactive)
2074 continue;
2075
2076 if (mds_con) {
2077 struct mds_group_info *mgi;
2078
2079 LASSERT(vallen == sizeof(*mgi));
2080 mgi = (struct mds_group_info *)val;
2081
2082 /* Only want a specific OSC */
2083 if (mgi->uuid && !obd_uuid_equals(mgi->uuid,
2084 &tgt->ltd_uuid))
2085 continue;
2086
2087 err = obd_set_info_async(env, tgt->ltd_exp,
2088 keylen, key, sizeof(int),
2089 &mgi->group, set);
2090 } else if (next_id) {
2091 err = obd_set_info_async(env, tgt->ltd_exp,
2092 keylen, key, vallen,
bcc3b704 2093 ((struct obd_id_info *)val)->data, set);
d7e09d03 2094 } else if (capa) {
bcc3b704 2095 struct mds_capa_info *info = (struct mds_capa_info *)val;
d7e09d03
PT
2096
2097 LASSERT(vallen == sizeof(*info));
2098
2099 /* Only want a specific OSC */
2100 if (info->uuid &&
2101 !obd_uuid_equals(info->uuid, &tgt->ltd_uuid))
2102 continue;
2103
2104 err = obd_set_info_async(env, tgt->ltd_exp, keylen,
2105 key, sizeof(*info->capa),
2106 info->capa, set);
2107 } else {
2108 /* Only want a specific OSC */
2109 if (check_uuid &&
2110 !obd_uuid_equals(val, &tgt->ltd_uuid))
2111 continue;
2112
2113 err = obd_set_info_async(env, tgt->ltd_exp,
2114 keylen, key, vallen, val, set);
2115 }
2116
2117 if (!rc)
2118 rc = err;
2119 }
2120
2121 obd_putref(obddev);
2122 if (no_set) {
2123 err = ptlrpc_set_wait(set);
2124 if (!rc)
2125 rc = err;
2126 ptlrpc_set_destroy(set);
2127 }
0a3bdb00 2128 return rc;
d7e09d03
PT
2129}
2130
d7e09d03
PT
2131void lov_stripe_lock(struct lov_stripe_md *md)
2132{
2133 LASSERT(md->lsm_lock_owner != current_pid());
2134 spin_lock(&md->lsm_lock);
2135 LASSERT(md->lsm_lock_owner == 0);
2136 md->lsm_lock_owner = current_pid();
2137}
2138EXPORT_SYMBOL(lov_stripe_lock);
2139
2140void lov_stripe_unlock(struct lov_stripe_md *md)
2141{
2142 LASSERT(md->lsm_lock_owner == current_pid());
2143 md->lsm_lock_owner = 0;
2144 spin_unlock(&md->lsm_lock);
2145}
2146EXPORT_SYMBOL(lov_stripe_unlock);
2147
2148static int lov_quotactl(struct obd_device *obd, struct obd_export *exp,
2149 struct obd_quotactl *oqctl)
2150{
2151 struct lov_obd *lov = &obd->u.lov;
2152 struct lov_tgt_desc *tgt;
2153 __u64 curspace = 0;
2154 __u64 bhardlimit = 0;
2155 int i, rc = 0;
d7e09d03
PT
2156
2157 if (oqctl->qc_cmd != LUSTRE_Q_QUOTAON &&
2158 oqctl->qc_cmd != LUSTRE_Q_QUOTAOFF &&
2159 oqctl->qc_cmd != Q_GETOQUOTA &&
2160 oqctl->qc_cmd != Q_INITQUOTA &&
2161 oqctl->qc_cmd != LUSTRE_Q_SETQUOTA &&
2162 oqctl->qc_cmd != Q_FINVALIDATE) {
2163 CERROR("bad quota opc %x for lov obd", oqctl->qc_cmd);
0a3bdb00 2164 return -EFAULT;
d7e09d03
PT
2165 }
2166
2167 /* for lov tgt */
2168 obd_getref(obd);
2169 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
2170 int err;
2171
2172 tgt = lov->lov_tgts[i];
2173
2174 if (!tgt)
2175 continue;
2176
2177 if (!tgt->ltd_active || tgt->ltd_reap) {
2178 if (oqctl->qc_cmd == Q_GETOQUOTA &&
2179 lov->lov_tgts[i]->ltd_activate) {
2180 rc = -EREMOTEIO;
2181 CERROR("ost %d is inactive\n", i);
2182 } else {
2183 CDEBUG(D_HA, "ost %d is inactive\n", i);
2184 }
2185 continue;
2186 }
2187
2188 err = obd_quotactl(tgt->ltd_exp, oqctl);
2189 if (err) {
2190 if (tgt->ltd_active && !rc)
2191 rc = err;
2192 continue;
2193 }
2194
2195 if (oqctl->qc_cmd == Q_GETOQUOTA) {
2196 curspace += oqctl->qc_dqblk.dqb_curspace;
2197 bhardlimit += oqctl->qc_dqblk.dqb_bhardlimit;
2198 }
2199 }
2200 obd_putref(obd);
2201
2202 if (oqctl->qc_cmd == Q_GETOQUOTA) {
2203 oqctl->qc_dqblk.dqb_curspace = curspace;
2204 oqctl->qc_dqblk.dqb_bhardlimit = bhardlimit;
2205 }
0a3bdb00 2206 return rc;
d7e09d03
PT
2207}
2208
2209static int lov_quotacheck(struct obd_device *obd, struct obd_export *exp,
2210 struct obd_quotactl *oqctl)
2211{
2212 struct lov_obd *lov = &obd->u.lov;
2213 int i, rc = 0;
d7e09d03
PT
2214
2215 obd_getref(obd);
2216
2217 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
2218 if (!lov->lov_tgts[i])
2219 continue;
2220
2221 /* Skip quota check on the administratively disabled OSTs. */
2222 if (!lov->lov_tgts[i]->ltd_activate) {
2223 CWARN("lov idx %d was administratively disabled, "
2224 "skip quotacheck on it.\n", i);
2225 continue;
2226 }
2227
2228 if (!lov->lov_tgts[i]->ltd_active) {
2229 CERROR("lov idx %d inactive\n", i);
2230 rc = -EIO;
2231 goto out;
2232 }
2233 }
2234
2235 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
2236 int err;
2237
2238 if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_activate)
2239 continue;
2240
2241 err = obd_quotacheck(lov->lov_tgts[i]->ltd_exp, oqctl);
2242 if (err && !rc)
2243 rc = err;
2244 }
2245
2246out:
2247 obd_putref(obd);
2248
0a3bdb00 2249 return rc;
d7e09d03
PT
2250}
2251
2252struct obd_ops lov_obd_ops = {
2253 .o_owner = THIS_MODULE,
2254 .o_setup = lov_setup,
2255 .o_precleanup = lov_precleanup,
2256 .o_cleanup = lov_cleanup,
5e448831 2257 /*.o_process_config = lov_process_config,*/
d7e09d03
PT
2258 .o_connect = lov_connect,
2259 .o_disconnect = lov_disconnect,
2260 .o_statfs = lov_statfs,
2261 .o_statfs_async = lov_statfs_async,
2262 .o_packmd = lov_packmd,
2263 .o_unpackmd = lov_unpackmd,
2264 .o_create = lov_create,
2265 .o_destroy = lov_destroy,
d7e09d03 2266 .o_getattr_async = lov_getattr_async,
d7e09d03 2267 .o_setattr_async = lov_setattr_async,
d7e09d03 2268 .o_adjust_kms = lov_adjust_kms,
d7e09d03 2269 .o_find_cbdata = lov_find_cbdata,
d7e09d03
PT
2270 .o_iocontrol = lov_iocontrol,
2271 .o_get_info = lov_get_info,
2272 .o_set_info_async = lov_set_info_async,
d7e09d03
PT
2273 .o_notify = lov_notify,
2274 .o_pool_new = lov_pool_new,
2275 .o_pool_rem = lov_pool_remove,
2276 .o_pool_add = lov_pool_add,
2277 .o_pool_del = lov_pool_del,
2278 .o_getref = lov_getref,
2279 .o_putref = lov_putref,
2280 .o_quotactl = lov_quotactl,
2281 .o_quotacheck = lov_quotacheck,
2282};
2283
2284struct kmem_cache *lov_oinfo_slab;
2285
d7e09d03
PT
2286int __init lov_init(void)
2287{
e6be8e63 2288 struct lprocfs_static_vars lvars = { NULL };
d7e09d03 2289 int rc;
d7e09d03
PT
2290
2291 /* print an address of _any_ initialized kernel symbol from this
2292 * module, to allow debugging with gdb that doesn't support data
2293 * symbols from modules.*/
2294 CDEBUG(D_INFO, "Lustre LOV module (%p).\n", &lov_caches);
2295
2296 rc = lu_kmem_init(lov_caches);
2297 if (rc)
2298 return rc;
2299
2300 lov_oinfo_slab = kmem_cache_create("lov_oinfo",
2301 sizeof(struct lov_oinfo),
2302 0, SLAB_HWCACHE_ALIGN, NULL);
2303 if (lov_oinfo_slab == NULL) {
2304 lu_kmem_fini(lov_caches);
2305 return -ENOMEM;
2306 }
2307 lprocfs_lov_init_vars(&lvars);
2308
2309 rc = class_register_type(&lov_obd_ops, NULL, lvars.module_vars,
2310 LUSTRE_LOV_NAME, &lov_device_type);
2311
2312 if (rc) {
2313 kmem_cache_destroy(lov_oinfo_slab);
2314 lu_kmem_fini(lov_caches);
2315 }
2316
0a3bdb00 2317 return rc;
d7e09d03
PT
2318}
2319
2320static void /*__exit*/ lov_exit(void)
2321{
2322 class_unregister_type(LUSTRE_LOV_NAME);
2323 kmem_cache_destroy(lov_oinfo_slab);
2324
2325 lu_kmem_fini(lov_caches);
2326}
2327
2328MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
2329MODULE_DESCRIPTION("Lustre Logical Object Volume OBD driver");
2330MODULE_LICENSE("GPL");
6960736c 2331MODULE_VERSION(LUSTRE_VERSION_STRING);
d7e09d03 2332
6960736c
GKH
2333module_init(lov_init);
2334module_exit(lov_exit);
This page took 0.406538 seconds and 5 git commands to generate.