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