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