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