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