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