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