Whamcloud - gitweb
several fixes: expiry timer adjusted.
[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  * Copyright (C) 2002, 2003 Cluster File Systems, Inc.
5  * Author: Phil Schwan <phil@clusterfs.com>
6  *         Peter Braam <braam@clusterfs.com>
7  *         Mike Shaver <shaver@clusterfs.com>
8  *
9  *   This file is part of Lustre, http://www.lustre.org.
10  *
11  *   Lustre is free software; you can redistribute it and/or
12  *   modify it under the terms of version 2 of the GNU General Public
13  *   License as published by the Free Software Foundation.
14  *
15  *   Lustre is distributed in the hope that it will be useful,
16  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *   GNU General Public License for more details.
19  *
20  *   You should have received a copy of the GNU General Public License
21  *   along with Lustre; if not, write to the Free Software
22  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  */
24
25 #ifndef EXPORT_SYMTAB
26 # define EXPORT_SYMTAB
27 #endif
28 #define DEBUG_SUBSYSTEM S_LOV
29 #ifdef __KERNEL__
30 #include <linux/slab.h>
31 #include <linux/module.h>
32 #include <linux/init.h>
33 #include <linux/slab.h>
34 #include <linux/pagemap.h>
35 #include <linux/seq_file.h>
36 #include <asm/div64.h>
37 #else
38 #include <liblustre.h>
39 #endif
40
41 #include <linux/obd_support.h>
42 #include <linux/lustre_lib.h>
43 #include <linux/lustre_net.h>
44 #include <linux/lustre_idl.h>
45 #include <linux/lustre_dlm.h>
46 #include <linux/lustre_mds.h>
47 #include <linux/obd_class.h>
48 #include <linux/obd_lov.h>
49 #include <linux/obd_ost.h>
50 #include <linux/lprocfs_status.h>
51 #include <linux/lustre_audit.h>
52 #include "lov_internal.h"
53
54 /* obd methods */
55 #define MAX_STRING_SIZE 128
56 static int lov_connect_obd(struct obd_device *obd, struct lov_tgt_desc *tgt,
57                            int activate, struct obd_connect_data *conn_data,
58                            unsigned long connect_flags)
59 {
60         struct obd_uuid lov_osc_uuid = { "LOV_OSC_UUID" };
61         struct obd_uuid *tgt_uuid = &tgt->uuid;
62
63 #ifdef __KERNEL__
64         struct proc_dir_entry *lov_proc_dir;
65 #endif
66         struct lov_obd *lov = &obd->u.lov;
67         struct lustre_handle conn = {0, };
68         struct obd_device *tgt_obd;
69         int rc;
70         ENTRY;
71
72         tgt_obd = class_find_client_obd(tgt_uuid, OBD_OSC_DEVICENAME,
73                                         &obd->obd_uuid);
74
75         if (!tgt_obd) {
76                 CERROR("Target %s not attached\n", tgt_uuid->uuid);
77                 RETURN(-EINVAL);
78         }
79
80         if (!tgt_obd->obd_set_up) {
81                 CERROR("Target %s not set up\n", tgt_uuid->uuid);
82                 RETURN(-EINVAL);
83         }
84
85         if (activate) {
86                 tgt_obd->obd_no_recov = 0;
87                 ptlrpc_activate_import(tgt_obd->u.cli.cl_import);
88         }
89
90         if (tgt_obd->u.cli.cl_import->imp_invalid) {
91                 CERROR("not connecting OSC %s; administratively "
92                        "disabled\n", tgt_uuid->uuid);
93                 rc = obd_register_observer(tgt_obd, obd);
94                 if (rc) {
95                         CERROR("Target %s register_observer error %d; "
96                                "will not be able to reactivate\n",
97                                tgt_uuid->uuid, rc);
98                 }
99                 RETURN(0);
100         }
101
102         rc = obd_connect(&conn, tgt_obd, &lov_osc_uuid, conn_data,
103                          connect_flags);
104         if (rc) {
105                 CERROR("Target %s connect error %d\n", tgt_uuid->uuid, rc);
106                 RETURN(rc);
107         }
108         tgt->ltd_exp = class_conn2export(&conn);
109
110         rc = obd_register_observer(tgt_obd, obd);
111         if (rc) {
112                 CERROR("Target %s register_observer error %d\n",
113                        tgt_uuid->uuid, rc);
114                 obd_disconnect(tgt->ltd_exp, 0);
115                 tgt->ltd_exp = NULL;
116                 RETURN(rc);
117         }
118
119         lov_tgt_set_flags(lov, tgt, LTD_ACTIVE);
120
121 #ifdef __KERNEL__
122         lov_proc_dir = lprocfs_srch(obd->obd_proc_entry, "target_obds");
123         if (lov_proc_dir) {
124                 struct obd_device *osc_obd = class_conn2obd(&conn);
125                 struct proc_dir_entry *osc_symlink;
126                 char name[MAX_STRING_SIZE + 1];
127
128                 LASSERT(osc_obd != NULL);
129                 LASSERT(osc_obd->obd_type != NULL);
130                 LASSERT(osc_obd->obd_type->typ_name != NULL);
131                 name[MAX_STRING_SIZE] = '\0';
132                 snprintf(name, MAX_STRING_SIZE, "../../../%s/%s",
133                          osc_obd->obd_type->typ_name,
134                          osc_obd->obd_name);
135                 osc_symlink = proc_symlink(osc_obd->obd_name, lov_proc_dir,
136                                            name);
137                 if (osc_symlink == NULL) {
138                         CERROR("could not register LOV target "
139                                "/proc/fs/lustre/%s/%s/target_obds/%s\n",
140                                obd->obd_type->typ_name, obd->obd_name,
141                                osc_obd->obd_name);
142                         lprocfs_remove(lov_proc_dir);
143                         lov_proc_dir = NULL;
144                 }
145         }
146 #endif
147
148         RETURN(0);
149 }
150
151 static int lov_connect(struct lustre_handle *conn, struct obd_device *obd,
152                        struct obd_uuid *cluuid, struct obd_connect_data *data,
153                        unsigned long flags)
154 {
155 #ifdef __KERNEL__
156         struct proc_dir_entry *lov_proc_dir;
157 #endif
158         struct lov_obd *lov = &obd->u.lov;
159         struct lov_tgt_desc *tgt;
160         struct obd_export *exp;
161         int rc, rc2, i;
162         ENTRY;
163
164         rc = class_connect(conn, obd, cluuid);
165         if (rc)
166                 RETURN(rc);
167
168         exp = class_conn2export(conn);
169
170         /* We don't want to actually do the underlying connections more than
171          * once, so keep track. */
172         lov->refcount++;
173         if (lov->refcount > 1) {
174                 class_export_put(exp);
175                 RETURN(0);
176         }
177
178 #ifdef __KERNEL__
179         lov_proc_dir = lprocfs_register("target_obds", obd->obd_proc_entry,
180                                         NULL, NULL);
181         if (IS_ERR(lov_proc_dir)) {
182                 CERROR("could not register /proc/fs/lustre/%s/%s/target_obds.",
183                        obd->obd_type->typ_name, obd->obd_name);
184                 lov_proc_dir = NULL;
185         }
186 #endif
187
188         /* connect_flags is the MDS number, save for use in lov_add_obd */
189         lov->lov_connect_flags = flags;
190         for (i = 0, tgt = lov->tgts; i < lov->desc.ld_tgt_count; i++, tgt++) {
191                 if (obd_uuid_empty(&tgt->uuid))
192                         continue;
193                 rc = lov_connect_obd(obd, tgt, 0, data, flags);
194                 if (rc)
195                         GOTO(out_disc, rc);
196         }
197
198         class_export_put(exp);
199         RETURN (0);
200
201  out_disc:
202 #ifdef __KERNEL__
203         if (lov_proc_dir)
204                 lprocfs_remove(lov_proc_dir);
205 #endif
206
207         while (i-- > 0) {
208                 struct obd_uuid uuid;
209                 --tgt;
210                 lov_tgt_clear_flags(lov, tgt, LTD_ACTIVE);
211
212                 /* save for CERROR below; (we know it's terminated) */
213                 uuid = tgt->uuid;
214                 rc2 = obd_disconnect(tgt->ltd_exp, 0);
215                 if (rc2)
216                         CERROR("error: LOV target %s disconnect on OST idx %d: "
217                                "rc = %d\n", uuid.uuid, i, rc2);
218         }
219         class_disconnect(exp, 0);
220         RETURN (rc);
221 }
222
223 static int lov_disconnect_obd(struct obd_device *obd, 
224                               struct lov_tgt_desc *tgt,
225                               unsigned long flags)
226 {
227 #ifdef __KERNEL__
228         struct proc_dir_entry *lov_proc_dir;
229 #endif
230         struct obd_device *osc_obd = class_exp2obd(tgt->ltd_exp);
231         struct lov_obd *lov = &obd->u.lov;
232         int rc;
233         ENTRY;
234
235 #ifdef __KERNEL__
236         lov_proc_dir = lprocfs_srch(obd->obd_proc_entry, "target_obds");
237         if (lov_proc_dir) {
238                 struct proc_dir_entry *osc_symlink;
239
240                 osc_symlink = lprocfs_srch(lov_proc_dir, osc_obd->obd_name);
241                 if (osc_symlink) {
242                         lprocfs_remove(osc_symlink);
243                 } else {
244                         CERROR("/proc/fs/lustre/%s/%s/target_obds/%s missing\n",
245                                obd->obd_type->typ_name, obd->obd_name,
246                                osc_obd->obd_name);
247                 }
248         }
249 #endif
250         if (obd->obd_no_recov) {
251                 /* Pass it on to our clients.
252                  * XXX This should be an argument to disconnect,
253                  * XXX not a back-door flag on the OBD.  Ah well.
254                  */
255                 if (osc_obd)
256                         osc_obd->obd_no_recov = 1;
257         }
258
259         /* XXX - shouldn't this be after the obd_disconnect() ? */
260         obd_register_observer(tgt->ltd_exp->exp_obd, NULL);
261
262         rc = obd_disconnect(tgt->ltd_exp, flags);
263         if (rc) {
264                 if (lov_tgt_active(lov, tgt, 0)) {
265                         lov_tgt_decref(lov, tgt);
266                         CERROR("Target %s disconnect error %d\n",
267                                tgt->uuid.uuid, rc);
268                 }
269         }
270
271         CDEBUG(D_CONFIG, "idx: %d flags: 0x%x active: %d\n",
272                tgt - lov->tgts, tgt->ltd_flags,
273                lov->desc.ld_active_tgt_count);
274
275         lov_tgt_clear_flags(lov, tgt, LTD_ACTIVE);
276         tgt->ltd_exp = NULL;
277         RETURN(0);
278 }
279
280 static int lov_disconnect(struct obd_export *exp, unsigned long flags)
281 {
282         struct obd_device *obd = class_exp2obd(exp);
283 #ifdef __KERNEL__
284         struct proc_dir_entry *lov_proc_dir;
285 #endif
286         struct lov_obd *lov = &obd->u.lov;
287         struct lov_tgt_desc *tgt;
288         int rc, i;
289         ENTRY;
290
291         if (!lov->tgts)
292                 goto out_local;
293
294         /* Only disconnect the underlying layers on the final disconnect. */
295         lov->refcount--;
296         if (lov->refcount != 0)
297                 goto out_local;
298
299         for (i = 0, tgt = lov->tgts; i < lov->desc.ld_tgt_count; i++, tgt++) {
300                 if (tgt->ltd_exp)
301                         lov_disconnect_obd(obd, tgt, flags);
302         }
303
304 #ifdef __KERNEL__
305         lov_proc_dir = lprocfs_srch(obd->obd_proc_entry, "target_obds");
306         if (lov_proc_dir) {
307                 lprocfs_remove(lov_proc_dir);
308         } else {
309                 CERROR("/proc/fs/lustre/%s/%s/target_obds missing.",
310                        obd->obd_type->typ_name, obd->obd_name);
311         }
312 #endif
313         
314  out_local:
315         rc = class_disconnect(exp, 0);
316         RETURN(rc);
317 }
318
319 /* Error codes:
320  *
321  *  -EINVAL  : UUID can't be found in the LOV's target list
322  *  -ENOTCONN: The UUID is found, but the target connection is bad (!)
323  *  -EBADF   : The UUID is found, but the OBD is the wrong type (!)
324  */
325 static int lov_set_osc_active(struct lov_obd *lov, struct obd_uuid *uuid,
326                               int activate)
327 {
328         struct lov_tgt_desc *tgt;
329         int i, rc = 0;
330         ENTRY;
331
332         CDEBUG(D_INFO, "Searching in lov %p for uuid %s (activate=%d)\n",
333                lov, uuid->uuid, activate);
334
335         lov_tgts_lock(lov);
336         for (i = 0, tgt = lov->tgts; i < lov->desc.ld_tgt_count; i++, tgt++) {
337                 if (tgt->ltd_exp == NULL)
338                         continue;
339
340                 CDEBUG(D_INFO, "lov idx %d is %s conn "LPX64"\n",
341                        i, tgt->uuid.uuid, tgt->ltd_exp->exp_handle.h_cookie);
342                 
343                 if (obd_uuid_equals(uuid, &tgt->uuid))
344                         break;
345         }
346
347         if (i == lov->desc.ld_tgt_count)
348                 GOTO(out, rc = -EINVAL);
349
350         if (activate) {
351                 if (tgt->ltd_flags & LTD_ACTIVE) {
352                         CDEBUG(D_CONFIG|D_INFO, "OSC %s already active!\n",
353                                uuid->uuid);
354                         GOTO(out, rc);
355                 }
356                 CDEBUG(D_INFO, "Marking OSC %s active\n", uuid->uuid);
357                 tgt->ltd_flags |= LTD_ACTIVE;
358                 lov->desc.ld_active_tgt_count++;
359         } else {
360                 if ((tgt->ltd_flags & LTD_ACTIVE) == 0) {
361                         CDEBUG(D_CONFIG|D_INFO, "OSC %s already inactive!\n",
362                                uuid->uuid);
363                         GOTO(out, rc);
364                 }
365                 CDEBUG(D_INFO, "Marking OSC %s inactive\n", uuid->uuid);
366                 tgt->ltd_flags &= ~LTD_ACTIVE;
367                 lov->desc.ld_active_tgt_count--;
368         }
369  
370         EXIT;
371  out:
372         lov_tgts_unlock(lov);
373         return rc;
374 }
375
376 static int lov_notify(struct obd_device *obd, struct obd_device *watched,
377                       int active, void *data)
378 {
379         struct obd_uuid *uuid;
380         int rc;
381         ENTRY;
382
383         if (strcmp(watched->obd_type->typ_name, OBD_OSC_DEVICENAME)) {
384                 CERROR("unexpected notification of %s %s!\n",
385                        watched->obd_type->typ_name,
386                        watched->obd_name);
387                 return -EINVAL;
388         }
389
390         uuid = &watched->u.cli.cl_import->imp_target_uuid;
391
392         /* Set OSC as active before notifying the observer, so the
393          * observer can use the OSC normally.  
394          */
395         rc = lov_set_osc_active(&obd->u.lov, uuid, active);
396         if (rc) {
397                 CERROR("%sactivation of %s failed: %d\n",
398                        active ? "" : "de", uuid->uuid, rc);
399                 RETURN(rc);
400         }
401
402         if (obd->obd_observer)
403                 /* Pass the notification up the chain. */
404                 rc = obd_notify(obd->obd_observer, watched, active, data);
405
406         RETURN(rc);
407 }
408
409 int lov_attach(struct obd_device *dev, obd_count len, void *data)
410 {
411         struct lprocfs_static_vars lvars;
412         int rc;
413
414         lprocfs_init_vars(lov, &lvars);
415         rc = lprocfs_obd_attach(dev, lvars.obd_vars);
416         if (rc == 0) {
417 #ifdef __KERNEL__
418                 struct proc_dir_entry *entry;
419
420                 entry = create_proc_entry("target_obd_status", 0444, 
421                                           dev->obd_proc_entry);
422                 if (entry == NULL) {
423                         rc = -ENOMEM;
424                 } else {
425                         entry->proc_fops = &lov_proc_target_fops;
426                         entry->data = dev;
427                 }
428 #endif
429         }
430         return rc;
431 }
432
433 int lov_detach(struct obd_device *dev)
434 {
435         return lprocfs_obd_detach(dev);
436 }
437
438 static int lov_setup(struct obd_device *obd, obd_count len, void *buf)
439 {
440         struct lov_obd *lov = &obd->u.lov;
441         struct lustre_cfg *lcfg = buf;
442         struct lov_desc *desc;
443         int count;
444         ENTRY;
445
446         if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
447                 CERROR("LOV setup requires a descriptor\n");
448                 RETURN(-EINVAL);
449         }
450
451         desc = (struct lov_desc *)lustre_cfg_string(lcfg, 1);
452         if (sizeof(*desc) > LUSTRE_CFG_BUFLEN(lcfg, 1)) {
453                 CERROR("descriptor size wrong: %d > %d\n",
454                        (int)sizeof(*desc), LUSTRE_CFG_BUFLEN(lcfg, 1));
455                 RETURN(-EINVAL);
456         }
457  
458         /* Because of 64-bit divide/mod operations only work with a 32-bit
459          * divisor in a 32-bit kernel, we cannot support a stripe width
460          * of 4GB or larger on 32-bit CPUs.
461          */
462        
463         count = desc->ld_default_stripe_count;
464         if (count && (count * desc->ld_default_stripe_size) > ~0UL) {
465                 CERROR("LOV: stripe width "LPU64"x%u > %lu on 32-bit system\n",
466                        desc->ld_default_stripe_size, count, ~0UL);
467                 RETURN(-EINVAL);
468         }
469         if (desc->ld_tgt_count > 0) {
470                 lov->bufsize= sizeof(struct lov_tgt_desc) * desc->ld_tgt_count;
471         } else {
472                 lov->bufsize = sizeof(struct lov_tgt_desc) * LOV_MAX_TGT_COUNT;  
473         }
474         OBD_ALLOC(lov->tgts, lov->bufsize);
475         if (lov->tgts == NULL) {
476                 lov->bufsize = 0;
477                 CERROR("couldn't allocate %d bytes for target table.\n",
478                        lov->bufsize);
479                 RETURN(-EINVAL);
480         }
481
482         desc->ld_tgt_count = 0;
483         desc->ld_active_tgt_count = 0;
484         lov->desc = *desc;
485         spin_lock_init(&lov->lov_lock);
486         sema_init(&lov->lov_llog_sem, 1);
487         init_waitqueue_head(&lov->lov_tgt_waitq);
488
489         RETURN(0);
490 }
491
492 static int lov_cleanup(struct obd_device *obd, int flags)
493 {
494         struct lov_obd *lov = &obd->u.lov;
495
496         OBD_FREE(lov->tgts, lov->bufsize);
497         RETURN(0);
498 }
499
500 static int
501 lov_add_obd(struct obd_device *obd, struct obd_uuid *uuidp, int index, int gen)
502 {
503         struct lov_obd *lov = &obd->u.lov;
504         struct lov_tgt_desc *tgt;
505         int rc;
506         ENTRY;
507
508         CDEBUG(D_CONFIG, "uuid: %s idx: %d gen: %d\n",
509                uuidp->uuid, index, gen);
510
511         if ((index < 0) || (index >= LOV_MAX_TGT_COUNT)) {
512                 CERROR("request to add OBD %s at invalid index: %d\n",
513                        uuidp->uuid, index);
514                 RETURN(-EINVAL);
515         }
516
517         if (gen <= 0) {
518                 CERROR("request to add OBD %s with invalid generation: %d\n",
519                        uuidp->uuid, gen);
520                 RETURN(-EINVAL);
521         }
522
523         tgt = lov->tgts + index;
524
525         lov_tgts_lock(lov);
526
527         if (!obd_uuid_empty(&tgt->uuid)) {
528                 lov_tgts_unlock(lov);
529                 CERROR("OBD already assigned at LOV target index %d\n",
530                        index);
531                 RETURN(-EEXIST);
532         }
533
534         tgt->uuid = *uuidp;
535         /* XXX - add a sanity check on the generation number. */
536         tgt->ltd_gen = gen;
537         tgt->ltd_flags = 0;
538
539         if (index >= lov->desc.ld_tgt_count)
540                 lov->desc.ld_tgt_count = index + 1;
541
542         lov_tgts_unlock(lov);
543
544         CDEBUG(D_CONFIG, "idx: %d ltd_gen: %d ld_tgt_count: %d\n",
545                 index, tgt->ltd_gen, lov->desc.ld_tgt_count);
546
547         if (lov->refcount == 0)
548                 RETURN(0);
549
550         /* We don't need to lock the tgt entry here because the
551          * config events are single threaded. Therefore only the
552          * current thread can be changing the tgts tbl. */
553         if (tgt->ltd_exp) {
554                 struct obd_device *osc_obd;
555
556                 osc_obd = class_exp2obd(tgt->ltd_exp);
557                 if (osc_obd)
558                         osc_obd->obd_no_recov = 0;
559         }
560
561         rc = lov_connect_obd(obd, tgt, 0, NULL, lov->lov_connect_flags);
562         if (rc)
563                 GOTO(out, rc);
564
565         if (obd->obd_observer) {
566                 /* tell the mds_lov about the new target */
567                 rc = obd_notify(obd->obd_observer, tgt->ltd_exp->exp_obd, 1,
568                                 (void *)index);
569         }
570
571         GOTO(out, rc);
572  out:
573         if (rc && tgt->ltd_exp != NULL)
574                 lov_disconnect_obd(obd, tgt, 0);
575         return rc;
576 }
577
578 static int
579 lov_del_obd(struct obd_device *obd, struct obd_uuid *uuidp, int index, int gen)
580 {
581         struct lov_obd *lov = &obd->u.lov;
582         struct lov_tgt_desc *tgt;
583         struct l_wait_info lwi = { 0 };
584         int count = lov->desc.ld_tgt_count;
585         int rc = 0;
586         ENTRY;
587
588         CDEBUG(D_CONFIG, "uuid: %s idx: %d gen: %d\n",
589                uuidp->uuid, index, gen);
590
591         if (index >= count) {
592                 CERROR("LOV target index %d >= number of LOV OBDs %d.\n",
593                        index, count);
594                 RETURN(-EINVAL);
595         }
596
597         tgt = lov->tgts + index;
598
599         if (obd_uuid_empty(&tgt->uuid)) {
600                 CERROR("LOV target at index %d is not setup.\n", index);
601                 RETURN(-EINVAL);
602         }
603
604         if (!obd_uuid_equals(uuidp, &tgt->uuid)) {
605                 CERROR("LOV target UUID %s at index %d doesn't match %s.\n",
606                        tgt->uuid.uuid, index, uuidp->uuid);
607                 RETURN(-EINVAL);
608         }
609
610         lov_tgt_set_flags(lov, tgt, LTD_DEL_PENDING);
611
612         if (tgt->ltd_exp) {
613                 struct obd_device *osc_obd;
614
615                 rc = obd_cancel_unused(tgt->ltd_exp, NULL,
616                                        LDLM_FL_CONFIG_CHANGE, NULL);
617                 if (rc != 0)
618                         CWARN("obd_cancel_unused(osc): %d\n", rc);
619
620                 osc_obd = class_exp2obd(tgt->ltd_exp);
621                 if (osc_obd) {
622                         osc_obd->obd_no_recov = 1;
623                         rc = obd_llog_finish(osc_obd, &osc_obd->obd_llogs, 1);
624                         if (rc)
625                                 CERROR("osc_llog_finish error: %d\n", rc);
626                 }
627
628                 lov_disconnect_obd(obd, tgt, 0);
629         }
630
631         CDEBUG(D_CONFIG, "Sleeping until LOV target index %d UUID %s "
632                "is quiesced; ltd_refcount: %d ld_active_tgt_count: %d.\n",
633                index, uuidp->uuid, tgt->ltd_refcount,
634                lov->desc.ld_active_tgt_count);
635
636         /* XXX - should we set a timeout ? */
637         lwi = LWI_TIMEOUT_INTR(0, NULL, NULL, NULL);
638         rc = l_wait_event(lov->lov_tgt_waitq, (tgt->ltd_refcount == 0), &lwi);
639         if (rc) {
640                 lov_tgt_clear_flags(lov, tgt, LTD_DEL_PENDING);
641                 CERROR("LOV target delete UUID %s index %d aborted: %d.\n",
642                        uuidp->uuid, index, rc);
643                 RETURN(rc);
644         }
645
646         CDEBUG(D_CONFIG, "LOV target index %d UUID %s is quiesced; "
647                "ltd_refcount: %d ld_active_tgt_count: %d.\n",
648                index, uuidp->uuid, tgt->ltd_refcount,
649                lov->desc.ld_active_tgt_count);
650
651         /* XXX - right now there is a dependency on ld_tgt_count being the
652          * maximum tgt index for computing the mds_max_easize. So we can't
653          * shrink it. */
654
655         /* lt_gen = 0 will mean it will not match the gen of any valid loi */
656         memset(tgt, 0, sizeof(*tgt));
657         RETURN(rc);
658 }
659
660 static int lov_process_config(struct obd_device *obd, obd_count len, void *buf)
661 {
662         struct lustre_cfg *lcfg = buf;
663         struct obd_uuid obd_uuid;
664         int cmd;
665         int index;
666         int gen;
667         int rc = 0;
668         ENTRY;
669
670         switch(cmd = lcfg->lcfg_command) {
671         case LCFG_LOV_ADD_OBD:
672         case LCFG_LOV_DEL_OBD: {
673                 if (LUSTRE_CFG_BUFLEN(lcfg, 1) > sizeof(obd_uuid.uuid))
674                         GOTO(out, rc = -EINVAL);
675
676                 obd_str2uuid(&obd_uuid, lustre_cfg_string(lcfg, 1));
677
678                 if (sscanf(lustre_cfg_buf(lcfg, 2), "%d", &index) != 1)
679                         GOTO(out, rc = -EINVAL);
680                 if (sscanf(lustre_cfg_buf(lcfg, 3), "%d", &gen) != 1)
681                         GOTO(out, rc = -EINVAL);
682                 if (cmd == LCFG_LOV_ADD_OBD)
683                         rc = lov_add_obd(obd, &obd_uuid, index, gen);
684                 else
685                         rc = lov_del_obd(obd, &obd_uuid, index, gen);
686                 GOTO(out, rc);
687         }
688         default: {
689                 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
690                 GOTO(out, rc = -EINVAL);
691
692         }
693         }
694 out:
695         RETURN(rc);
696 }
697
698 #ifndef log2
699 #define log2(n) ffz(~(n))
700 #endif
701
702 static int lov_clear_orphans(struct obd_export *export,
703                              struct obdo *src_oa,
704                              struct lov_stripe_md **ea,
705                              struct obd_trans_info *oti)
706 {
707         struct lov_obd *lov;
708         struct obdo *tmp_oa;
709         struct obd_uuid *ost_uuid = NULL;
710         struct lov_tgt_desc *tgt;
711         int rc = 0, i;
712         ENTRY;
713
714         LASSERT(src_oa->o_valid & OBD_MD_FLFLAGS &&
715                 src_oa->o_flags == OBD_FL_DELORPHAN);
716
717         lov = &export->exp_obd->u.lov;
718
719         tmp_oa = obdo_alloc();
720         if (tmp_oa == NULL)
721                 RETURN(-ENOMEM);
722
723         if (src_oa->o_valid & OBD_MD_FLINLINE) {
724                 ost_uuid = (struct obd_uuid *)src_oa->o_inline;
725                 CDEBUG(D_HA, "clearing orphans only for %s\n",
726                        ost_uuid->uuid);
727         }
728
729         for (i = 0, tgt = lov->tgts; i < lov->desc.ld_tgt_count; i++, tgt++) {
730                 struct lov_stripe_md obj_md;
731                 struct lov_stripe_md *obj_mdp = &obj_md;
732                 int active = 0;
733                 int err;
734
735                 /* if called for a specific target, we don't
736                    care if it is not active. */
737                 if (lov_tgt_active(lov, tgt, 0)) {
738                         active = 1;
739                 } else if (ost_uuid == NULL) {
740                         CDEBUG(D_HA, "lov idx %d inactive\n", i);
741                         continue;
742                 }
743
744                 if (ost_uuid && !obd_uuid_equals(ost_uuid, &tgt->uuid)) {
745                         if (active)
746                                 lov_tgt_decref(lov, tgt);
747                         continue;
748                 }
749
750                 /* 
751                  * setting up objid OSS objects should be destroyed starting
752                  * from it.
753                  */
754                 memcpy(tmp_oa, src_oa, sizeof(*tmp_oa));
755                 tmp_oa->o_valid |= OBD_MD_FLID;
756                 tmp_oa->o_id = oti->oti_objid[i];
757
758                 /* XXX: LOV STACKING: use real "obj_mdp" sub-data */
759                 err = obd_create(tgt->ltd_exp, tmp_oa, NULL, 0, &obj_mdp, oti);
760                 if (err)
761                         /* This export will be disabled until it is recovered,
762                            and then orphan recovery will be completed. */
763                         CERROR("error in orphan recovery on OST idx %d/%d: "
764                                "rc = %d\n", i, lov->desc.ld_tgt_count, err);
765
766                 if (active)
767                         lov_tgt_decref(lov, tgt);
768
769                 if (ost_uuid)
770                         break;
771         }
772
773         obdo_free(tmp_oa);
774         RETURN(rc);
775 }
776
777 /* the LOV expects oa->o_id to be set to the LOV object id */
778 static int
779 lov_create(struct obd_export *exp, struct obdo *src_oa,
780            void *acl, int acl_size, struct lov_stripe_md **ea,
781            struct obd_trans_info *oti)
782 {
783         struct lov_request_set *set = NULL;
784         struct list_head *pos;
785         struct lov_obd *lov;
786         int rc = 0;
787         ENTRY;
788
789         LASSERT(ea != NULL);
790         if (exp == NULL)
791                 RETURN(-EINVAL);
792
793         if ((src_oa->o_valid & OBD_MD_FLFLAGS) &&
794             src_oa->o_flags == OBD_FL_DELORPHAN) {
795                 rc = lov_clear_orphans(exp, src_oa, ea, oti);
796                 RETURN(rc);
797         }
798
799         LASSERT(oti->oti_flags & OBD_MODE_CROW);
800                 
801         /* main creation loop */
802         rc = lov_prep_create_set(exp, ea, src_oa, oti, &set);
803         if (rc)
804                 RETURN(rc);
805
806         lov = &exp->exp_obd->u.lov;
807
808         list_for_each (pos, &set->set_list) {
809                 struct lov_request *req = 
810                         list_entry(pos, struct lov_request, rq_link);
811
812                 /* XXX: LOV STACKING: use real "obj_mdp" sub-data */
813                 rc = obd_create(lov->tgts[req->rq_idx].ltd_exp,
814                                 req->rq_oa, NULL, 0, &req->rq_md, oti);
815                 lov_update_create_set(set, req, rc);
816         }
817         rc = lov_fini_create_set(set, ea);
818         RETURN(rc);
819 }
820
821 #define lsm_bad_magic(LSMP)                                     \
822 ({                                                              \
823         struct lov_stripe_md *_lsm__ = (LSMP);                  \
824         int _ret__ = 0;                                         \
825         if (!_lsm__) {                                          \
826                 CERROR("LOV requires striping ea\n");           \
827                 _ret__ = 1;                                     \
828         } else if (_lsm__->lsm_magic != LOV_MAGIC) {            \
829                 CERROR("LOV striping magic bad %#x != %#x\n",   \
830                        _lsm__->lsm_magic, LOV_MAGIC);           \
831                 _ret__ = 1;                                     \
832         }                                                       \
833         _ret__;                                                 \
834 })
835
836 static int lov_destroy(struct obd_export *exp, struct obdo *oa,
837                        struct lov_stripe_md *lsm, struct obd_trans_info *oti)
838 {
839         struct lov_request_set *set;
840         struct lov_request *req;
841         struct list_head *pos;
842         struct lov_obd *lov;
843         int rc = 0;
844         ENTRY;
845
846         if (lsm_bad_magic(lsm))
847                 RETURN(-EINVAL);
848
849         if (!exp || !exp->exp_obd)
850                 RETURN(-ENODEV);
851
852         lov = &exp->exp_obd->u.lov;
853         rc = lov_prep_destroy_set(exp, oa, lsm, oti, &set);
854         if (rc)
855                 RETURN(rc);
856
857         list_for_each (pos, &set->set_list) {
858                 int err;
859                 req = list_entry(pos, struct lov_request, rq_link);
860
861                 /* XXX update the cookie position */
862                 oti->oti_logcookies = set->set_cookies + req->rq_stripe;
863                 rc = obd_destroy(lov->tgts[req->rq_idx].ltd_exp, req->rq_oa,
864                                  NULL, oti);
865                 err = lov_update_common_set(set, req, rc);
866                 if (rc) {
867                         CERROR("error: destroying objid "LPX64" subobj "
868                                LPX64" on OST idx %d: rc = %d\n", 
869                                set->set_oa->o_id, req->rq_oa->o_id, 
870                                req->rq_idx, rc);
871                         if (!rc)
872                                 rc = err;
873                 }
874         }
875         lov_fini_destroy_set(set);
876         RETURN(rc);
877 }
878
879 static int lov_getattr(struct obd_export *exp, struct obdo *oa,
880                        struct lov_stripe_md *lsm)
881 {
882         struct lov_request_set *set;
883         struct lov_request *req;
884         struct list_head *pos;
885         struct lov_obd *lov;
886         int err = 0, rc = 0;
887         ENTRY;
888
889         if (lsm_bad_magic(lsm))
890                 RETURN(-EINVAL);
891
892         if (!exp || !exp->exp_obd)
893                 RETURN(-ENODEV);
894
895         lov = &exp->exp_obd->u.lov;
896         
897         rc = lov_prep_getattr_set(exp, oa, lsm, &set);
898         if (rc)
899                 RETURN(rc);
900
901         list_for_each (pos, &set->set_list) {
902                 req = list_entry(pos, struct lov_request, rq_link);
903                 
904                 CDEBUG(D_INFO, "objid "LPX64"[%d] has subobj "LPX64" at idx "
905                        "%u\n", oa->o_id, req->rq_stripe, req->rq_oa->o_id, 
906                        req->rq_idx);
907
908                 rc = obd_getattr(lov->tgts[req->rq_idx].ltd_exp, 
909                                  req->rq_oa, NULL);
910                 err = lov_update_common_set(set, req, rc);
911                 if (err) {
912                         CERROR("error: getattr objid "LPX64" subobj "
913                                LPX64" on OST idx %d: rc = %d\n",
914                                set->set_oa->o_id, req->rq_oa->o_id, 
915                                req->rq_idx, err);
916                         break;
917                 }
918         }
919         
920         rc = lov_fini_getattr_set(set);
921         if (err)
922                 rc = err;
923         RETURN(rc);
924 }
925
926 static int lov_getattr_interpret(struct ptlrpc_request_set *rqset, void *data,
927                                  int rc)
928 {
929         struct lov_request_set *lovset = (struct lov_request_set *)data;
930         ENTRY;
931
932         /* don't do attribute merge if this aysnc op failed */
933         if (rc) {
934                 lovset->set_completes = 0;
935                 lov_fini_getattr_set(lovset);
936         } else {
937                 rc = lov_fini_getattr_set(lovset);
938         }
939         RETURN (rc);
940 }
941
942 static int lov_getattr_async(struct obd_export *exp, struct obdo *oa,
943                               struct lov_stripe_md *lsm,
944                               struct ptlrpc_request_set *rqset)
945 {
946         struct lov_request_set *lovset;
947         struct lov_obd *lov;
948         struct list_head *pos;
949         struct lov_request *req;
950         int rc = 0;
951         ENTRY;
952
953         if (lsm_bad_magic(lsm))
954                 RETURN(-EINVAL);
955
956         if (!exp || !exp->exp_obd)
957                 RETURN(-ENODEV);
958
959         lov = &exp->exp_obd->u.lov;
960
961         rc = lov_prep_getattr_set(exp, oa, lsm, &lovset);
962         if (rc)
963                 RETURN(rc);
964
965         CDEBUG(D_INFO, "objid "LPX64": %ux%u byte stripes\n",
966                lsm->lsm_object_id, lsm->lsm_stripe_count, lsm->lsm_stripe_size);
967
968         list_for_each (pos, &lovset->set_list) {
969                 req = list_entry(pos, struct lov_request, rq_link);
970                 
971                 CDEBUG(D_INFO, "objid "LPX64"[%d] has subobj "LPX64" at idx "
972                        "%u\n", oa->o_id, req->rq_stripe, req->rq_oa->o_id, 
973                        req->rq_idx);
974                 rc = obd_getattr_async(lov->tgts[req->rq_idx].ltd_exp,
975                                        req->rq_oa, NULL, rqset);
976                 if (rc) {
977                         CERROR("error: getattr objid "LPX64" subobj "
978                                LPX64" on OST idx %d: rc = %d\n",
979                                lovset->set_oa->o_id, req->rq_oa->o_id, 
980                                req->rq_idx, rc);
981                         GOTO(out, rc);
982                 }
983                 lov_update_common_set(lovset, req, rc);
984         }
985         
986         LASSERT(rc == 0);
987         LASSERT (rqset->set_interpret == NULL);
988         rqset->set_interpret = lov_getattr_interpret;
989         rqset->set_arg = (void *)lovset;
990         RETURN(rc);
991 out:
992         LASSERT(rc);
993         lov_fini_getattr_set(lovset);
994         RETURN(rc);
995 }
996
997 static int lov_setattr(struct obd_export *exp, struct obdo *src_oa,
998                        struct lov_stripe_md *lsm, struct obd_trans_info *oti,
999                        struct lustre_capa *capa)
1000 {
1001         struct lov_request_set *set;
1002         struct lov_obd *lov;
1003         struct list_head *pos;
1004         struct lov_request *req;
1005         int err = 0, rc = 0;
1006         ENTRY;
1007
1008         if (lsm_bad_magic(lsm))
1009                 RETURN(-EINVAL);
1010
1011         if (!exp || !exp->exp_obd)
1012                 RETURN(-ENODEV);
1013
1014         LASSERT(!(src_oa->o_valid & ~(OBD_MD_FLID|OBD_MD_FLTYPE | OBD_MD_FLMODE|
1015                                       OBD_MD_FLATIME | OBD_MD_FLMTIME |
1016                                       OBD_MD_FLCTIME | OBD_MD_FLFLAGS |
1017                                       OBD_MD_FLSIZE | OBD_MD_FLGROUP |
1018                                       OBD_MD_FLUID | OBD_MD_FLGID |
1019                                       OBD_MD_FLINLINE | OBD_MD_FLIFID)));
1020
1021         LASSERT(!(src_oa->o_valid & OBD_MD_FLGROUP) || src_oa->o_gr > 0);
1022
1023         lov = &exp->exp_obd->u.lov;
1024         rc = lov_prep_setattr_set(exp, src_oa, lsm, NULL, &set);
1025         if (rc)
1026                 RETURN(rc);
1027
1028         list_for_each (pos, &set->set_list) {
1029                 req = list_entry(pos, struct lov_request, rq_link);
1030                 
1031                 rc = obd_setattr(lov->tgts[req->rq_idx].ltd_exp, req->rq_oa,
1032                                  NULL, NULL, NULL);
1033                 err = lov_update_common_set(set, req, rc);
1034                 if (err) {
1035                         CERROR("error: setattr objid "LPX64" subobj "
1036                                LPX64" on OST idx %d: rc = %d\n",
1037                                set->set_oa->o_id, req->rq_oa->o_id,
1038                                req->rq_idx, err);
1039                         if (!rc)
1040                                 rc = err;
1041                 }
1042         }
1043         err = lov_fini_setattr_set(set);
1044         if (!rc)
1045                 rc = err;
1046         RETURN(rc);
1047 }
1048
1049 static int lov_revalidate_policy(struct lov_obd *lov, struct lov_stripe_md *lsm)
1050 {
1051         static int next_idx = 0;
1052         struct lov_tgt_desc *tgt;
1053         int i, count;
1054         ENTRY;
1055
1056         /* XXX - we should do something clever and take lsm
1057          * into account but just do round robin for now. */
1058
1059         /* last_idx must always be less that count because
1060          * ld_tgt_count currently cannot shrink. */
1061         count = lov->desc.ld_tgt_count;
1062
1063         for (i = next_idx, tgt = lov->tgts + i; i < count; i++, tgt++) {
1064                 if (lov_tgt_active(lov, tgt, 0)) {
1065                         next_idx = (i + 1) % count;
1066                         RETURN(i);
1067                 }
1068         }
1069
1070         for (i = 0, tgt = lov->tgts; i < next_idx; i++, tgt++) {
1071                 if (lov_tgt_active(lov, tgt, 0)) {
1072                         next_idx = (i + 1) % count;
1073                         RETURN(i);
1074                 }
1075         }
1076
1077         RETURN(-EIO);
1078 }
1079
1080 static int lov_revalidate_md(struct obd_export *exp, struct obdo *src_oa,
1081                              struct lov_stripe_md *ea,
1082                              struct obd_trans_info *oti)
1083 {
1084         struct obd_export *osc_exp;
1085         struct lov_obd *lov = &exp->exp_obd->u.lov;
1086         struct lov_stripe_md *lsm = ea;
1087         struct lov_stripe_md obj_md;
1088         struct lov_stripe_md *obj_mdp = &obj_md;
1089         struct lov_oinfo *loi;
1090         struct obdo *tmp_oa;
1091         int ost_idx, updates = 0, i;
1092         ENTRY;
1093
1094         tmp_oa = obdo_alloc();
1095         if (tmp_oa == NULL)
1096                 RETURN(-ENOMEM);
1097
1098         loi = lsm->lsm_oinfo;
1099         for (i = 0; i < lsm->lsm_stripe_count; i++, loi++) {
1100                 struct lov_tgt_desc *tgt;
1101                 int rc;
1102
1103                 if (!lov_tgt_changed(lov, loi))
1104                         continue;
1105
1106                 /* returns an active tgt. tgt->ltd_refcount is incremented. */
1107                 ost_idx = lov_revalidate_policy(lov, lsm);
1108                 if (ost_idx < 0) {
1109                         /* FIXME: punt for now. */
1110                         CERROR("lov_revalidate_policy failed; no active "
1111                                "OSCs?\n");
1112                         continue;
1113                 }
1114
1115                 /* create a new object */
1116                 memcpy(tmp_oa, src_oa, sizeof(*tmp_oa));
1117                 /* XXX: LOV STACKING: use real "obj_mdp" sub-data */
1118                 tgt = lov->tgts + ost_idx;
1119                 osc_exp = tgt->ltd_exp;
1120                 rc = obd_create(osc_exp, tmp_oa, NULL, 0, &obj_mdp, oti);
1121                 if (rc) {
1122                         lov_tgt_decref(lov, tgt);
1123                         CERROR("error creating new subobj at idx %d; "
1124                                "rc = %d\n", ost_idx, rc);
1125                         continue;
1126                 }
1127
1128                 CDEBUG(D_INODE,
1129                        "replacing idx %d gen %d objid "LPX64" subobj "LPX64" "
1130                        "with idx %d gen %d objid "LPX64" subobj "LPX64".\n",
1131                        loi->loi_ost_idx, loi->loi_ost_gen,
1132                        loi->loi_id, loi->loi_gr,
1133                        ost_idx, tgt->ltd_gen, tmp_oa->o_id, tmp_oa->o_gr);
1134
1135                 if (oti->oti_objid)
1136                         oti->oti_objid[ost_idx] = tmp_oa->o_id;
1137                 loi->loi_id = tmp_oa->o_id;
1138                 loi->loi_gr = tmp_oa->o_gr;
1139                 loi->loi_ost_idx = ost_idx;
1140                 loi->loi_ost_gen = tgt->ltd_gen;
1141                 lov_tgt_decref(lov, tgt);
1142                 updates = 1;
1143         }
1144
1145         /* If we got an error revalidating an entry there's no need to
1146          * cleanup up objects we allocated here because the bad entry
1147          * still points to a deleted OST. */
1148
1149         obdo_free(tmp_oa);
1150         RETURN(updates);
1151 }
1152
1153 /* FIXME: maybe we'll just make one node the authoritative attribute node, then
1154  * we can send this 'punch' to just the authoritative node and the nodes
1155  * that the punch will affect. */
1156 static int lov_punch(struct obd_export *exp, struct obdo *oa,
1157                      struct lov_stripe_md *lsm, obd_off start,
1158                      obd_off end, struct obd_trans_info *oti,
1159                      struct lustre_capa *capa)
1160 {
1161         struct lov_request_set *set;
1162         struct lov_obd *lov;
1163         struct list_head *pos;
1164         struct lov_request *req;
1165         int err = 0, rc = 0;
1166         ENTRY;
1167
1168         if (lsm_bad_magic(lsm))
1169                 RETURN(-EINVAL);
1170
1171         if (!exp || !exp->exp_obd)
1172                 RETURN(-ENODEV);
1173
1174         lov = &exp->exp_obd->u.lov;
1175         rc = lov_prep_punch_set(exp, oa, lsm, start, end, oti, &set);
1176         if (rc)
1177                 RETURN(rc);
1178
1179         list_for_each (pos, &set->set_list) {
1180                 req = list_entry(pos, struct lov_request, rq_link);
1181
1182                 rc = obd_punch(lov->tgts[req->rq_idx].ltd_exp, req->rq_oa, 
1183                                NULL, req->rq_extent.start, 
1184                                req->rq_extent.end, NULL, capa);
1185                 err = lov_update_punch_set(set, req, rc);
1186                 if (err) {
1187                         CERROR("error: punch objid "LPX64" subobj "LPX64
1188                                " on OST idx %d: rc = %d\n", set->set_oa->o_id,
1189                                req->rq_oa->o_id, req->rq_idx, rc);
1190                         if (!rc)
1191                                 rc = err;
1192                 }
1193         }
1194         err = lov_fini_punch_set(set);
1195         if (!rc)
1196                 rc = err;
1197         RETURN(rc);
1198 }
1199
1200 static int lov_sync(struct obd_export *exp, struct obdo *oa,
1201                     struct lov_stripe_md *lsm, obd_off start, obd_off end)
1202 {
1203         struct lov_request_set *set;
1204         struct lov_obd *lov;
1205         struct list_head *pos;
1206         struct lov_request *req;
1207         int err = 0, rc = 0;
1208         ENTRY;
1209
1210         if (lsm_bad_magic(lsm))
1211                 RETURN(-EINVAL);
1212
1213         if (!exp->exp_obd)
1214                 RETURN(-ENODEV);
1215
1216         lov = &exp->exp_obd->u.lov;
1217         rc = lov_prep_sync_set(exp, oa, lsm, start, end, &set);
1218         if (rc)
1219                 RETURN(rc);
1220
1221         list_for_each (pos, &set->set_list) {
1222                 req = list_entry(pos, struct lov_request, rq_link);
1223
1224                 rc = obd_sync(lov->tgts[req->rq_idx].ltd_exp, req->rq_oa, 
1225                               NULL, req->rq_extent.start, req->rq_extent.end);
1226                 err = lov_update_common_set(set, req, rc);
1227                 if (err) {
1228                         CERROR("error: fsync objid "LPX64" subobj "LPX64
1229                                " on OST idx %d: rc = %d\n", set->set_oa->o_id,
1230                                req->rq_oa->o_id, req->rq_idx, rc);
1231                         if (!rc)
1232                                 rc = err;
1233                 }
1234         }
1235         err = lov_fini_sync_set(set);
1236         if (!rc)
1237                 rc = err;
1238         RETURN(rc);
1239 }
1240
1241 static int lov_brw_check(struct lov_obd *lov, struct obdo *oa,
1242                          struct lov_stripe_md *lsm,
1243                          obd_count oa_bufs, struct brw_page *pga)
1244 {
1245         int i, rc = 0;
1246         ENTRY;
1247
1248         /* The caller just wants to know if there's a chance that this
1249          * I/O can succeed */
1250         for (i = 0; i < oa_bufs; i++) {
1251                 struct lov_oinfo *loi;
1252                 struct lov_tgt_desc *tgt;
1253                 int stripe;
1254                 obd_off start, end;
1255
1256                 if (!lov_stripe_intersects(lsm, i, pga[i].disk_offset,
1257                                            pga[i].disk_offset + pga[i].count,
1258                                            &start, &end))
1259                         continue;
1260
1261                 stripe = lov_stripe_number(lsm, pga[i].disk_offset);
1262                 loi = lsm->lsm_oinfo + stripe;
1263                 tgt = lov->tgts + loi->loi_ost_idx;
1264
1265                 if (!lov_tgt_ready(lov, tgt, loi->loi_ost_gen)) {
1266                         CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
1267                         RETURN(-EIO);
1268                 }
1269                 rc = obd_brw(OBD_BRW_CHECK, tgt->ltd_exp, oa,
1270                              NULL, 1, &pga[i], NULL);
1271                 lov_tgt_decref(lov, tgt);
1272                 if (rc)
1273                         break;
1274         }
1275         RETURN(rc);
1276 }
1277
1278 static int lov_brw(int cmd, struct obd_export *exp, struct obdo *src_oa,
1279                    struct lov_stripe_md *lsm, obd_count oa_bufs,
1280                    struct brw_page *pga, struct obd_trans_info *oti)
1281 {
1282         struct lov_request_set *set;
1283         struct lov_request *req;
1284         struct list_head *pos;
1285         struct lov_obd *lov = &exp->exp_obd->u.lov;
1286         int err, rc = 0;
1287         ENTRY;
1288
1289         if (lsm_bad_magic(lsm))
1290                 RETURN(-EINVAL);
1291
1292         if (cmd == OBD_BRW_CHECK) {
1293                 rc = lov_brw_check(lov, src_oa, lsm, oa_bufs, pga);
1294                 RETURN(rc);
1295         }
1296
1297         rc = lov_prep_brw_set(exp, src_oa, lsm, oa_bufs, pga, oti, &set);
1298         if (rc)
1299                 RETURN(rc);
1300
1301         list_for_each (pos, &set->set_list) {
1302                 struct obd_export *sub_exp;
1303                 struct brw_page *sub_pga;
1304                 req = list_entry(pos, struct lov_request, rq_link);
1305                 
1306                 sub_exp = lov->tgts[req->rq_idx].ltd_exp;
1307                 sub_pga = set->set_pga + req->rq_pgaidx;
1308                 rc = obd_brw(cmd, sub_exp, req->rq_oa, req->rq_md, 
1309                              req->rq_oabufs, sub_pga, oti);
1310                 if (rc)
1311                         break;
1312                 lov_update_common_set(set, req, rc);
1313         }
1314
1315         err = lov_fini_brw_set(set);
1316         if (!rc)
1317                 rc = err;
1318         RETURN(rc);
1319 }
1320
1321 static int lov_brw_interpret(struct ptlrpc_request_set *reqset, void *data,
1322                              int rc)
1323 {
1324         struct lov_request_set *lovset = (struct lov_request_set *)data;
1325         ENTRY;
1326         
1327         if (rc) {
1328                 lovset->set_completes = 0;
1329                 lov_fini_brw_set(lovset);
1330         } else {
1331                 rc = lov_fini_brw_set(lovset);
1332         }
1333                 
1334         RETURN(rc);
1335 }
1336
1337 static int lov_brw_async(int cmd, struct obd_export *exp, struct obdo *oa,
1338                          struct lov_stripe_md *lsm, obd_count oa_bufs,
1339                          struct brw_page *pga, struct ptlrpc_request_set *set,
1340                          struct obd_trans_info *oti)
1341 {
1342         struct lov_request_set *lovset;
1343         struct lov_request *req;
1344         struct list_head *pos;
1345         struct lov_obd *lov = &exp->exp_obd->u.lov;
1346         int rc = 0;
1347         ENTRY;
1348
1349         if (lsm_bad_magic(lsm))
1350                 RETURN(-EINVAL);
1351
1352         if (cmd == OBD_BRW_CHECK) {
1353                 rc = lov_brw_check(lov, oa, lsm, oa_bufs, pga);
1354                 RETURN(rc);
1355         }
1356
1357         rc = lov_prep_brw_set(exp, oa, lsm, oa_bufs, pga, oti, &lovset);
1358         if (rc)
1359                 RETURN(rc);
1360
1361         list_for_each (pos, &lovset->set_list) {
1362                 struct obd_export *sub_exp;
1363                 struct brw_page *sub_pga;
1364                 req = list_entry(pos, struct lov_request, rq_link);
1365                 
1366                 sub_exp = lov->tgts[req->rq_idx].ltd_exp;
1367                 sub_pga = lovset->set_pga + req->rq_pgaidx;
1368                 rc = obd_brw_async(cmd, sub_exp, req->rq_oa, req->rq_md,
1369                                    req->rq_oabufs, sub_pga, set, oti);
1370                 if (rc)
1371                         GOTO(out, rc);
1372                 lov_update_common_set(lovset, req, rc);
1373         }
1374         LASSERT(rc == 0);
1375         LASSERT(set->set_interpret == NULL);
1376         set->set_interpret = (set_interpreter_func)lov_brw_interpret;
1377         set->set_arg = (void *)lovset;
1378         
1379         RETURN(rc);
1380 out:
1381         lov_fini_brw_set(lovset);
1382         RETURN(rc);
1383 }
1384
1385 static int lov_ap_make_ready(void *data, int cmd)
1386 {
1387         struct lov_async_page *lap = LAP_FROM_COOKIE(data);
1388
1389         return lap->lap_caller_ops->ap_make_ready(lap->lap_caller_data, cmd);
1390 }
1391 static int lov_ap_refresh_count(void *data, int cmd)
1392 {
1393         struct lov_async_page *lap = LAP_FROM_COOKIE(data);
1394
1395         return lap->lap_caller_ops->ap_refresh_count(lap->lap_caller_data,
1396                                                      cmd);
1397 }
1398 static void lov_ap_fill_obdo(void *data, int cmd, struct obdo *oa)
1399 {
1400         struct lov_async_page *lap = LAP_FROM_COOKIE(data);
1401
1402         lap->lap_caller_ops->ap_fill_obdo(lap->lap_caller_data, cmd, oa);
1403         /* XXX woah, shouldn't we be altering more here?  size? */
1404         oa->o_id = lap->lap_loi_id;
1405 }
1406
1407 static void lov_ap_completion(void *data, int cmd, struct obdo *oa, int rc)
1408 {
1409         struct lov_async_page *lap = LAP_FROM_COOKIE(data);
1410
1411         /* in a raid1 regime this would down a count of many ios
1412          * in flight, onl calling the caller_ops completion when all
1413          * the raid1 ios are complete */
1414         lap->lap_caller_ops->ap_completion(lap->lap_caller_data, cmd, oa, rc);
1415 }
1416
1417 static struct obd_async_page_ops lov_async_page_ops = {
1418         .ap_make_ready =        lov_ap_make_ready,
1419         .ap_refresh_count =     lov_ap_refresh_count,
1420         .ap_fill_obdo =         lov_ap_fill_obdo,
1421         .ap_completion =        lov_ap_completion,
1422 };
1423
1424 static int lov_prep_async_page(struct obd_export *exp,
1425                                struct lov_stripe_md *lsm,
1426                                struct lov_oinfo *loi, struct page *page,
1427                                obd_off offset, struct obd_async_page_ops *ops,
1428                                void *data, void **res)
1429 {
1430         struct lov_obd *lov = &exp->exp_obd->u.lov;
1431         struct lov_tgt_desc *tgt;
1432         struct lov_async_page *lap;
1433         int rc, stripe;
1434         ENTRY;
1435
1436         if (lsm_bad_magic(lsm))
1437                 RETURN(-EINVAL);
1438         LASSERT(loi == NULL);
1439
1440         stripe = lov_stripe_number(lsm, offset);
1441         loi = &lsm->lsm_oinfo[stripe];
1442
1443         OBD_ALLOC(lap, sizeof(*lap));
1444         if (lap == NULL)
1445                 RETURN(-ENOMEM);
1446
1447         lap->lap_magic = LAP_MAGIC;
1448         lap->lap_caller_ops = ops;
1449         lap->lap_caller_data = data;
1450
1451         /* FIXME handle multiple oscs after landing b_raid1 */
1452         lap->lap_stripe = stripe;
1453         switch (lsm->lsm_pattern) {
1454                 case LOV_PATTERN_RAID0:
1455                         lov_stripe_offset(lsm, offset, lap->lap_stripe, 
1456                                           &lap->lap_sub_offset);
1457                         break;
1458                 case LOV_PATTERN_CMOBD:
1459                         lap->lap_sub_offset = offset;
1460                         break;
1461                 default:
1462                         LBUG();
1463         }
1464
1465         /* so the callback doesn't need the lsm */
1466         lap->lap_loi_id = loi->loi_id;
1467
1468         tgt = lov->tgts + loi->loi_ost_idx;
1469
1470         if (lov_tgt_ready(lov, tgt, loi->loi_ost_gen)) {
1471                 rc = obd_prep_async_page(tgt->ltd_exp, lsm, loi, page,
1472                                          lap->lap_sub_offset,
1473                                          &lov_async_page_ops, lap,
1474                                          &lap->lap_sub_cookie);
1475         } else {
1476                 rc = -EIO;
1477         }
1478
1479         if (rc) {
1480                 OBD_FREE(lap, sizeof(*lap));
1481                 RETURN(rc);
1482         }
1483         CDEBUG(D_CACHE, "lap %p page %p cookie %p off "LPU64"\n", lap, page,
1484                lap->lap_sub_cookie, offset);
1485         *res = lap;
1486         RETURN(0);
1487 }
1488
1489 static int lov_queue_async_io(struct obd_export *exp,
1490                               struct lov_stripe_md *lsm,
1491                               struct lov_oinfo *loi, void *cookie,
1492                               int cmd, obd_off off, int count,
1493                               obd_flags brw_flags, obd_flags async_flags)
1494 {
1495         struct lov_obd *lov = &exp->exp_obd->u.lov;
1496         struct lov_tgt_desc *tgt;
1497         struct lov_async_page *lap;
1498         int rc;
1499
1500         LASSERT(loi == NULL);
1501
1502         if (lsm_bad_magic(lsm))
1503                 RETURN(-EINVAL);
1504
1505         lap = LAP_FROM_COOKIE(cookie);
1506         loi = &lsm->lsm_oinfo[lap->lap_stripe];
1507         tgt = lov->tgts + loi->loi_ost_idx;
1508
1509         if (!lov_tgt_ready(lov, tgt, loi->loi_ost_gen) && 
1510             !lov_tgt_pending(lov, tgt, loi->loi_ost_gen))
1511                  RETURN(-EIO);
1512
1513         rc = obd_queue_async_io(tgt->ltd_exp, lsm, loi, lap->lap_sub_cookie,
1514                                 cmd, off, count, brw_flags, async_flags);
1515
1516         lov_tgt_decref(lov, tgt);
1517         RETURN(rc);
1518 }
1519
1520 static int lov_set_async_flags(struct obd_export *exp,
1521                                struct lov_stripe_md *lsm,
1522                                struct lov_oinfo *loi, void *cookie,
1523                                obd_flags async_flags)
1524 {
1525         struct lov_obd *lov = &exp->exp_obd->u.lov;
1526         struct lov_tgt_desc *tgt;
1527         struct lov_async_page *lap;
1528         int rc;
1529
1530         LASSERT(loi == NULL);
1531
1532         if (lsm_bad_magic(lsm))
1533                 RETURN(-EINVAL);
1534
1535         lap = LAP_FROM_COOKIE(cookie);
1536         loi = &lsm->lsm_oinfo[lap->lap_stripe];
1537         tgt = lov->tgts + loi->loi_ost_idx;
1538
1539         if (!lov_tgt_ready(lov, tgt, loi->loi_ost_gen) && 
1540             !lov_tgt_pending(lov, tgt, loi->loi_ost_gen))
1541                  RETURN(-EIO);
1542         
1543         rc = obd_set_async_flags(tgt->ltd_exp, lsm, loi, lap->lap_sub_cookie,
1544                                  async_flags);
1545
1546         lov_tgt_decref(lov, tgt);
1547         RETURN(rc);
1548 }
1549
1550 static int lov_queue_group_io(struct obd_export *exp,
1551                               struct lov_stripe_md *lsm,
1552                               struct lov_oinfo *loi,
1553                               struct obd_io_group *oig, void *cookie,
1554                               int cmd, obd_off off, int count,
1555                               obd_flags brw_flags, obd_flags async_flags)
1556 {
1557         struct lov_obd *lov = &exp->exp_obd->u.lov;
1558         struct lov_tgt_desc *tgt;
1559         struct lov_async_page *lap;
1560         int rc;
1561
1562         LASSERT(loi == NULL);
1563
1564         if (lsm_bad_magic(lsm))
1565                 RETURN(-EINVAL);
1566
1567         lap = LAP_FROM_COOKIE(cookie);
1568         loi = &lsm->lsm_oinfo[lap->lap_stripe];
1569         tgt = lov->tgts + loi->loi_ost_idx;
1570
1571         if (!lov_tgt_ready(lov, tgt, loi->loi_ost_gen) && 
1572             !lov_tgt_pending(lov, tgt, loi->loi_ost_gen))
1573                  RETURN(-EIO);
1574
1575         rc = obd_queue_group_io(tgt->ltd_exp, lsm, loi, oig,
1576                                 lap->lap_sub_cookie, cmd, off, count,
1577                                 brw_flags, async_flags);
1578
1579         lov_tgt_decref(lov, tgt);
1580         RETURN(rc);
1581 }
1582
1583 /* this isn't exactly optimal.  we may have queued sync io in oscs on
1584  * all stripes, but we don't record that fact at queue time.  so we
1585  * trigger sync io on all stripes. */
1586 static int lov_trigger_group_io(struct obd_export *exp,
1587                                 struct lov_stripe_md *lsm,
1588                                 struct lov_oinfo *loi,
1589                                 struct obd_io_group *oig)
1590 {
1591         struct lov_obd *lov = &exp->exp_obd->u.lov;
1592         int rc = 0, i, err;
1593
1594         LASSERT(loi == NULL);
1595
1596         if (lsm_bad_magic(lsm))
1597                 RETURN(-EINVAL);
1598
1599         loi = lsm->lsm_oinfo;
1600         for (i = 0; i < lsm->lsm_stripe_count; i++, loi++) {
1601                 struct lov_tgt_desc *tgt = lov->tgts + loi->loi_ost_idx;
1602
1603                 if (!lov_tgt_ready(lov, tgt, loi->loi_ost_gen)) {
1604                         CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
1605                         continue;
1606                 }
1607
1608                 err = obd_trigger_group_io(tgt->ltd_exp, lsm, loi, oig);
1609
1610                 lov_tgt_decref(lov, tgt);
1611
1612                 if (rc == 0 && err != 0)
1613                         rc = err;
1614         };
1615         RETURN(rc);
1616 }
1617
1618 static int lov_teardown_async_page(struct obd_export *exp,
1619                                    struct lov_stripe_md *lsm,
1620                                    struct lov_oinfo *loi, void *cookie)
1621 {
1622         struct lov_obd *lov = &exp->exp_obd->u.lov;
1623         struct lov_tgt_desc *tgt;
1624         struct lov_async_page *lap;
1625         int rc;
1626         ENTRY;
1627
1628         LASSERT(loi == NULL);
1629
1630         if (lsm_bad_magic(lsm))
1631                 RETURN(-EINVAL);
1632
1633         lap = LAP_FROM_COOKIE(cookie);
1634
1635         loi = &lsm->lsm_oinfo[lap->lap_stripe];
1636         tgt = lov->tgts + loi->loi_ost_idx;
1637
1638         rc = obd_teardown_async_page(tgt->ltd_exp, lsm, loi,
1639                                      lap->lap_sub_cookie);
1640         lov_tgt_decref(lov, tgt);
1641
1642         if (rc) {
1643                 CERROR("unable to teardown sub cookie %p: %d\n",
1644                        lap->lap_sub_cookie, rc);
1645                 RETURN(rc);
1646         }
1647         OBD_FREE(lap, sizeof(*lap));
1648         RETURN(rc);
1649 }
1650
1651 static int lov_enqueue(struct obd_export *exp, struct lov_stripe_md *lsm,
1652                        __u32 type, ldlm_policy_data_t *policy, __u32 mode,
1653                        int *flags, void *bl_cb, void *cp_cb, void *gl_cb,
1654                        void *data,__u32 lvb_len, void *lvb_swabber,
1655                        struct lustre_handle *lockh)
1656 {
1657         struct lov_request_set *set;
1658         struct lov_request *req;
1659         struct list_head *pos;
1660         struct lustre_handle *lov_lockhp;
1661         struct lov_obd *lov;
1662         ldlm_error_t rc;
1663         int save_flags = *flags;
1664         ENTRY;
1665
1666         if (lsm_bad_magic(lsm))
1667                 RETURN(-EINVAL);
1668
1669         /* we should never be asked to replay a lock this way. */
1670         LASSERT((*flags & LDLM_FL_REPLAY) == 0);
1671
1672         if (!exp || !exp->exp_obd)
1673                 RETURN(-ENODEV);
1674
1675         lov = &exp->exp_obd->u.lov;
1676         rc = lov_prep_enqueue_set(exp, lsm, policy, mode, lockh, &set);
1677         if (rc)
1678                 RETURN(rc);
1679
1680         list_for_each (pos, &set->set_list) {
1681                 ldlm_policy_data_t sub_policy;
1682                 req = list_entry(pos, struct lov_request, rq_link);
1683                 lov_lockhp = set->set_lockh->llh_handles + req->rq_stripe;
1684                 LASSERT(lov_lockhp);
1685
1686                 *flags = save_flags;
1687                 sub_policy.l_extent.start = req->rq_extent.start;
1688                 sub_policy.l_extent.end = req->rq_extent.end;
1689
1690                 rc = obd_enqueue(lov->tgts[req->rq_idx].ltd_exp, req->rq_md,
1691                                  type, &sub_policy, mode, flags, bl_cb,
1692                                  cp_cb, gl_cb, data, lvb_len, lvb_swabber,
1693                                  lov_lockhp);
1694                 rc = lov_update_enqueue_set(set, req, rc, save_flags);
1695                 if (rc != ELDLM_OK)
1696                         break;
1697         }
1698
1699         lov_fini_enqueue_set(set, mode);
1700         RETURN(rc);
1701 }
1702
1703 static int lov_match(struct obd_export *exp, struct lov_stripe_md *lsm,
1704                      __u32 type, ldlm_policy_data_t *policy, __u32 mode,
1705                      int *flags, void *data, struct lustre_handle *lockh)
1706 {
1707         struct lov_request_set *set;
1708         struct lov_request *req;
1709         struct list_head *pos;
1710         struct lov_obd *lov = &exp->exp_obd->u.lov;
1711         struct lustre_handle *lov_lockhp;
1712         int lov_flags, rc = 0;
1713         ENTRY;
1714
1715         if (lsm_bad_magic(lsm))
1716                 RETURN(-EINVAL);
1717
1718         if (!exp || !exp->exp_obd)
1719                 RETURN(-ENODEV);
1720
1721         lov = &exp->exp_obd->u.lov;
1722         rc = lov_prep_match_set(exp, lsm, policy, mode, lockh, &set);
1723         if (rc)
1724                 RETURN(rc);
1725
1726         list_for_each (pos, &set->set_list) {
1727                 ldlm_policy_data_t sub_policy;
1728                 req = list_entry(pos, struct lov_request, rq_link);
1729                 lov_lockhp = set->set_lockh->llh_handles + req->rq_stripe;
1730                 LASSERT(lov_lockhp);
1731
1732                 sub_policy.l_extent.start = req->rq_extent.start;
1733                 sub_policy.l_extent.end = req->rq_extent.end;
1734                 lov_flags = *flags;
1735
1736                 rc = obd_match(lov->tgts[req->rq_idx].ltd_exp, req->rq_md,
1737                                type, &sub_policy, mode, &lov_flags, data,
1738                                lov_lockhp);
1739                 rc = lov_update_match_set(set, req, rc);
1740                 if (rc != 1)
1741                         break;
1742         }
1743         lov_fini_match_set(set, mode, *flags);
1744         RETURN(rc);
1745 }
1746
1747 static int lov_change_cbdata(struct obd_export *exp,
1748                              struct lov_stripe_md *lsm, ldlm_iterator_t it,
1749                              void *data)
1750 {
1751         struct lov_obd *lov;
1752         struct lov_oinfo *loi;
1753         int rc = 0, i;
1754         ENTRY;
1755
1756         if (lsm_bad_magic(lsm))
1757                 RETURN(-EINVAL);
1758
1759         if (!exp || !exp->exp_obd)
1760                 RETURN(-ENODEV);
1761
1762         LASSERT(lsm->lsm_object_gr > 0);
1763
1764         lov = &exp->exp_obd->u.lov;
1765         for (i = 0,loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++,loi++) {
1766                 struct lov_tgt_desc *tgt = lov->tgts + loi->loi_ost_idx;
1767                 struct lov_stripe_md submd;
1768
1769                 submd.lsm_object_id = loi->loi_id;
1770                 submd.lsm_object_gr = lsm->lsm_object_gr;
1771                 submd.lsm_stripe_count = 0;
1772                 rc = obd_change_cbdata(tgt->ltd_exp, &submd, it, data);
1773                 lov_tgt_decref(lov, tgt);
1774         }
1775         RETURN(rc);
1776 }
1777
1778 static int lov_cancel(struct obd_export *exp, struct lov_stripe_md *lsm,
1779                       __u32 mode, struct lustre_handle *lockh)
1780 {
1781         struct lov_request_set *set;
1782         struct lov_request *req;
1783         struct list_head *pos;
1784         struct lov_obd *lov = &exp->exp_obd->u.lov;
1785         struct lustre_handle *lov_lockhp;
1786         int err = 0, rc = 0;
1787         ENTRY;
1788
1789         if (lsm_bad_magic(lsm))
1790                 RETURN(-EINVAL);
1791
1792         if (!exp || !exp->exp_obd)
1793                 RETURN(-ENODEV);
1794
1795         LASSERT(lsm->lsm_object_gr > 0);
1796
1797         LASSERT(lockh);
1798         lov = &exp->exp_obd->u.lov;
1799         rc = lov_prep_cancel_set(exp, lsm, mode, lockh, &set);
1800         if (rc)
1801                 RETURN(rc);
1802
1803         list_for_each (pos, &set->set_list) {
1804                 req = list_entry(pos, struct lov_request, rq_link);
1805                 lov_lockhp = set->set_lockh->llh_handles + req->rq_stripe;
1806
1807                 rc = obd_cancel(lov->tgts[req->rq_idx].ltd_exp, req->rq_md,
1808                                 mode, lov_lockhp);
1809                 rc = lov_update_common_set(set, req, rc);
1810                 if (rc) {
1811                         CERROR("error: cancel objid "LPX64" subobj "
1812                                LPX64" on OST idx %d: rc = %d\n",
1813                                lsm->lsm_object_id,
1814                                req->rq_md->lsm_object_id, req->rq_idx, rc);
1815                         err = rc;
1816                 }
1817  
1818         }
1819         lov_fini_cancel_set(set);
1820         RETURN(err);
1821 }
1822
1823 static int lov_cancel_unused(struct obd_export *exp,
1824                              struct lov_stripe_md *lsm, 
1825                              int flags, void *opaque)
1826 {
1827         struct lov_obd *lov;
1828         struct lov_oinfo *loi;
1829         struct lov_tgt_desc *tgt;
1830         int rc = 0, i;
1831         ENTRY;
1832
1833         lov = &exp->exp_obd->u.lov;
1834         if (lsm == NULL) {
1835                 tgt = lov->tgts;
1836                 for (i = 0; i < lov->desc.ld_tgt_count; i++, tgt++) {
1837                         int err;
1838
1839                         if (lov_tgt_ready(lov, tgt, 0)) {
1840                                 err = obd_cancel_unused(tgt->ltd_exp, NULL,
1841                                                         flags, opaque);
1842                                 lov_tgt_decref(lov, tgt);
1843                         } else {
1844                                 if (obd_uuid_empty(&tgt->uuid))
1845                                         continue;
1846                                 CERROR("error: cancel unused "
1847                                        "OST idx %d: OST inactive.\n", i);
1848                                 err = -EIO;
1849                         }
1850                         if (!rc)
1851                                 rc = err;
1852                 }
1853                 RETURN(rc);
1854         }
1855
1856         if (lsm_bad_magic(lsm))
1857                 RETURN(-EINVAL);
1858
1859         if (!exp || !exp->exp_obd)
1860                 RETURN(-ENODEV);
1861
1862         LASSERT(lsm->lsm_object_gr > 0);
1863
1864         for (i = 0,loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++,loi++) {
1865                 struct lov_stripe_md submd;
1866                 int err;
1867
1868                 submd.lsm_object_id = loi->loi_id;
1869                 submd.lsm_object_gr = lsm->lsm_object_gr;
1870                 submd.lsm_stripe_count = 0;
1871
1872                 tgt = lov->tgts + loi->loi_ost_idx;
1873                 if (lov_tgt_ready(lov, tgt, loi->loi_ost_gen)) {
1874                         err = obd_cancel_unused(tgt->ltd_exp, &submd, flags,
1875                                                 opaque);
1876                         lov_tgt_decref(lov, tgt);
1877                         if (err && lov_tgt_ready(lov, tgt, loi->loi_ost_gen)) {
1878                                 lov_tgt_decref(lov, tgt);
1879                                 CERROR("error: cancel unused objid "LPX64" "
1880                                        "subobj "LPX64" OST idx %d: rc = %d\n",
1881                                        lsm->lsm_object_id, loi->loi_id,
1882                                        loi->loi_ost_idx, err);
1883                         }
1884                 } else {
1885                         err = -EIO;
1886                         CERROR("error: cancel unused objid "LPX64" "
1887                                "subobj "LPX64" on OST idx %d: OST inactive.\n",
1888                                lsm->lsm_object_id,
1889                                loi->loi_id, loi->loi_ost_idx);
1890                 }
1891
1892                 if (!rc)
1893                         rc = err;
1894         }
1895         RETURN(rc);
1896 }
1897
1898 #define LOV_U64_MAX ((__u64)~0ULL)
1899 #define LOV_SUM_MAX(tot, add)                                           \
1900         do {                                                            \
1901                 if ((tot) + (add) < (tot))                              \
1902                         (tot) = LOV_U64_MAX;                            \
1903                 else                                                    \
1904                         (tot) += (add);                                 \
1905         } while(0)
1906
1907 static int lov_statfs(struct obd_device *obd, struct obd_statfs *osfs,
1908                       unsigned long max_age)
1909 {
1910         struct lov_obd *lov = &obd->u.lov;
1911         struct lov_tgt_desc *tgt = lov->tgts;
1912         struct obd_statfs lov_sfs;
1913         int set = 0;
1914         int rc = 0;
1915         int i;
1916         ENTRY;
1917
1918
1919         /* We only get block data from the OBD */
1920         for (i = 0; i < lov->desc.ld_tgt_count; i++, tgt++) {
1921                 int err;
1922
1923                 if (!lov_tgt_active(lov, tgt, 0)) {
1924                         CDEBUG(D_HA, "lov idx %d inactive\n", i);
1925                         continue;
1926                 }
1927
1928                 err = obd_statfs(class_exp2obd(tgt->ltd_exp), &lov_sfs,max_age);
1929                 lov_tgt_decref(lov, tgt);
1930                 if (err) {
1931                         if (lov_tgt_active(lov, tgt, 0)) {
1932                                 lov_tgt_decref(lov, tgt);
1933                                 if (!rc)
1934                                         rc = err;
1935                         }
1936                         continue;
1937                 }
1938
1939                 if (!set) {
1940                         memcpy(osfs, &lov_sfs, sizeof(lov_sfs));
1941                         set = 1;
1942                 } else {
1943                         osfs->os_bfree += lov_sfs.os_bfree;
1944                         osfs->os_bavail += lov_sfs.os_bavail;
1945                         osfs->os_blocks += lov_sfs.os_blocks;
1946                         /* XXX not sure about this one - depends on policy.
1947                          *   - could be minimum if we always stripe on all OBDs
1948                          *     (but that would be wrong for any other policy,
1949                          *     if one of the OBDs has no more objects left)
1950                          *   - could be sum if we stripe whole objects
1951                          *   - could be average, just to give a nice number
1952                          *
1953                          * To give a "reasonable" (if not wholly accurate)
1954                          * number, we divide the total number of free objects
1955                          * by expected stripe count (watch out for overflow).
1956                          */
1957                         LOV_SUM_MAX(osfs->os_files, lov_sfs.os_files);
1958                         LOV_SUM_MAX(osfs->os_ffree, lov_sfs.os_ffree);
1959                 }
1960         }
1961
1962         if (set) {
1963                 __u32 expected_stripes = lov->desc.ld_default_stripe_count ?
1964                                          lov->desc.ld_default_stripe_count :
1965                                          lov->desc.ld_active_tgt_count;
1966
1967                 if ((lov->desc.ld_default_stripe_count == 0) ||
1968                     (lov->desc.ld_default_stripe_count >
1969                      lov->desc.ld_active_tgt_count)) {
1970                         expected_stripes = lov->desc.ld_active_tgt_count;
1971                 } else {
1972                         expected_stripes = lov->desc.ld_default_stripe_count;
1973                 }
1974
1975                 if (osfs->os_files != LOV_U64_MAX)
1976                         do_div(osfs->os_files, expected_stripes);
1977                 if (osfs->os_ffree != LOV_U64_MAX)
1978                         do_div(osfs->os_ffree, expected_stripes);
1979         } else if (!rc)
1980                 rc = -EIO;
1981
1982         RETURN(rc);
1983 }
1984
1985 static int lov_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1986                          void *karg, void *uarg)
1987 {
1988         struct obd_device *obddev = class_exp2obd(exp);
1989         struct lov_obd *lov = &obddev->u.lov;
1990         struct lov_tgt_desc *tgt;
1991         int i, rc, count = lov->desc.ld_tgt_count;
1992         struct obd_uuid *uuidp;
1993         ENTRY;
1994
1995         switch (cmd) {
1996         case OBD_IOC_LOV_GET_CONFIG: {
1997                 struct obd_ioctl_data *data = karg;
1998                 struct lov_desc *desc;
1999                 char *buf = NULL;
2000                 __u32 *genp;
2001
2002                 buf = NULL;
2003                 len = 0;
2004                 if (obd_ioctl_getdata(&buf, &len, (void *)uarg))
2005                         RETURN(-EINVAL);
2006
2007                 data = (struct obd_ioctl_data *)buf;
2008
2009                 if (sizeof(*desc) > data->ioc_inllen1) {
2010                         obd_ioctl_freedata(buf, len);
2011                         RETURN(-EINVAL);
2012                 }
2013
2014                 if (sizeof(uuidp->uuid) * count > data->ioc_inllen2) {
2015                         obd_ioctl_freedata(buf, len);
2016                         RETURN(-EINVAL);
2017                 }
2018
2019                 if (sizeof(__u32) * count > data->ioc_inllen3) {
2020                         obd_ioctl_freedata(buf, len);
2021                         RETURN(-EINVAL);
2022                 }
2023
2024                 desc = (struct lov_desc *)data->ioc_inlbuf1;
2025                 memcpy(desc, &(lov->desc), sizeof(*desc));
2026
2027                 uuidp = (struct obd_uuid *)data->ioc_inlbuf2;
2028                 genp = (__u32 *)data->ioc_inlbuf3;
2029                 /* the uuid will be empty for deleted OSTs */
2030                 lov_tgts_lock(lov);
2031                 tgt = lov->tgts;
2032                 for (i = 0; i < count; i++, uuidp++, genp++, tgt++) {
2033                         obd_str2uuid(uuidp, (char *)tgt->uuid.uuid);
2034                         *genp = tgt->ltd_gen;
2035                 }
2036                 lov_tgts_unlock(lov);
2037
2038                 rc = copy_to_user((void *)uarg, buf, len);
2039                 if (rc)
2040                         rc = -EFAULT;
2041                 obd_ioctl_freedata(buf, len);
2042                 break;
2043         }
2044         case LL_IOC_LOV_SETSTRIPE:
2045                 rc = lov_setstripe(exp, karg, uarg);
2046                 break;
2047         case LL_IOC_LOV_GETSTRIPE:
2048                 rc = lov_getstripe(exp, karg, uarg);
2049                 break;
2050         case LL_IOC_LOV_SETEA:
2051                 rc = lov_setea(exp, karg, uarg);
2052                 break;
2053         default: {
2054                 int set = 0;
2055                 if (count == 0)
2056                         RETURN(-ENOTTY);
2057                 rc = 0;
2058                 for (i = 0, tgt = lov->tgts; i < count; i++, tgt++) {
2059                         int err;
2060
2061                         if (!lov_tgt_active(lov, tgt, 0)) {
2062                                 CERROR("error: iocontrol failed for OSC %s on "
2063                                        "OST idx %d: OST inactive.\n",
2064                                        tgt->uuid.uuid, i);
2065                                 if (!rc)
2066                                         rc = -EIO;
2067                                 continue;
2068                         }
2069
2070                         err = obd_iocontrol(cmd, tgt->ltd_exp, len, karg, uarg);
2071                         lov_tgt_decref(lov, tgt);
2072                         if (err) {
2073                                 if (lov_tgt_ready(lov, tgt, 0)) {
2074                                         lov_tgt_decref(lov, tgt);
2075                                         CERROR("error: iocontrol OSC %s on OST "
2076                                                "idx %d cmd %x: err = %d\n",
2077                                                lov->tgts[i].uuid.uuid, i,
2078                                                cmd, err);
2079                                         if (!rc)
2080                                                 rc = err;
2081                                 }
2082                         } else
2083                                 set = 1;
2084                 }
2085                 if (!set && !rc)
2086                         rc = -EIO;
2087         }
2088         }
2089
2090         RETURN(rc);
2091 }
2092
2093 static int lov_get_info(struct obd_export *exp, __u32 keylen,
2094                         void *key, __u32 *vallen, void *val)
2095 {
2096         struct obd_device *obddev = class_exp2obd(exp);
2097         struct lov_obd *lov = &obddev->u.lov;
2098         struct lov_tgt_desc *tgt;
2099         int i;
2100         ENTRY;
2101
2102         if (!vallen || !val)
2103                 RETURN(-EFAULT);
2104
2105         if (keylen > strlen("lock_to_stripe") &&
2106             strcmp(key, "lock_to_stripe") == 0) {
2107                 struct {
2108                         char name[16];
2109                         struct ldlm_lock *lock;
2110                         struct lov_stripe_md *lsm;
2111                 } *data = key;
2112                 struct lov_oinfo *loi;
2113                 struct ldlm_res_id *res_id = &data->lock->l_resource->lr_name;
2114                 __u32 *stripe = val;
2115
2116                 if (*vallen < sizeof(*stripe))
2117                         RETURN(-EFAULT);
2118                 *vallen = sizeof(*stripe);
2119
2120                 /* XXX This is another one of those bits that will need to
2121                  * change if we ever actually support nested LOVs.  It uses
2122                  * the lock's export to find out which stripe it is. */
2123                 /* XXX - it's assumed all the locks for deleted OSTs have
2124                  * been cancelled. Also, the export for deleted OSTs will
2125                  * be NULL and won't match the lock's export. */
2126                 lov_tgts_lock(lov);
2127                 for (i = 0, loi = data->lsm->lsm_oinfo;
2128                      i < data->lsm->lsm_stripe_count;
2129                      i++, loi++) {
2130                         if (lov->tgts[loi->loi_ost_idx].ltd_exp ==
2131                                         data->lock->l_conn_export &&
2132                             loi->loi_id == res_id->name[0] &&
2133                             loi->loi_gr == res_id->name[2]) {
2134                                 lov_tgts_unlock(lov);
2135                                 *stripe = i;
2136                                 RETURN(0);
2137                         }
2138                 }
2139                 lov_tgts_unlock(lov);
2140                 LDLM_ERROR(data->lock, "lock on inode without such object");
2141                 dump_lsm(D_ERROR, data->lsm);
2142                 portals_debug_dumpstack(NULL);
2143                 RETURN(-ENXIO);
2144         } else if (keylen >= strlen("size_to_stripe") &&
2145                    strcmp(key, "size_to_stripe") == 0) {
2146                 struct {
2147                         int stripe_number;
2148                         __u64 size;
2149                         struct lov_stripe_md *lsm;
2150                 } *data = val;
2151
2152                 if (*vallen < sizeof(*data))
2153                         RETURN(-EFAULT);
2154
2155                 data->size = lov_size_to_stripe(data->lsm, data->size,
2156                                                 data->stripe_number);
2157                 RETURN(0);
2158         } else if (keylen >= strlen("last_id") && strcmp(key, "last_id") == 0) {
2159                 __u32 size = sizeof(obd_id);
2160                 obd_id *ids = val;
2161                 int rc = 0;
2162
2163                 tgt = lov->tgts;
2164                 for (i = 0; i < lov->desc.ld_tgt_count; i++, tgt++) {
2165                         if (!lov_tgt_active(lov, tgt, 0))
2166                                 continue;
2167                         rc = obd_get_info(tgt->ltd_exp, keylen, key, &size,
2168                                           ids + i);
2169                         lov_tgt_decref(lov, tgt);
2170                         if (rc != 0)
2171                                 RETURN(rc);
2172                 }
2173                 RETURN(0);
2174         } else if (keylen >= strlen("lovdesc") && strcmp(key, "lovdesc") == 0) {
2175                 struct lov_desc *desc_ret = val;
2176                 *desc_ret = lov->desc;
2177
2178                 RETURN(0);
2179         }
2180
2181         RETURN(-EINVAL);
2182 }
2183
2184 static int lov_set_info(struct obd_export *exp, obd_count keylen,
2185                         void *key, obd_count vallen, void *val)
2186 {
2187         struct obd_device *obddev = class_exp2obd(exp);
2188         struct lov_obd *lov = &obddev->u.lov;
2189         struct lov_tgt_desc *tgt;
2190         int i, rc = 0, err;
2191         ENTRY;
2192
2193 #define KEY_IS(str) \
2194         (keylen == strlen(str) && memcmp(key, str, keylen) == 0)
2195
2196         if (KEY_IS("async")) {
2197                 struct lov_desc *desc = &lov->desc;
2198
2199                 if (vallen != sizeof(int))
2200                         RETURN(-EINVAL);
2201                 lov->async = *((int*) val);
2202
2203                 lov_tgts_lock(lov);
2204                 tgt = lov->tgts;
2205                 for (i = 0; i < desc->ld_tgt_count; i++, tgt++) {
2206                         struct obd_uuid *tgt_uuid = &tgt->uuid;
2207                         struct obd_device *tgt_obd;
2208
2209                         tgt_obd = class_find_client_obd(tgt_uuid,
2210                                                         OBD_OSC_DEVICENAME,
2211                                                         &obddev->obd_uuid);
2212                         if (!tgt_obd) {
2213                                 CERROR("Target %s not attached\n",
2214                                         tgt_uuid->uuid);
2215                                 if (!rc)
2216                                         rc = -EINVAL;
2217                                 continue;
2218                         }
2219
2220                         err = obd_set_info(tgt_obd->obd_self_export,
2221                                            keylen, key, vallen, val);
2222                         if (err) {
2223                                 CERROR("Failed to set async on target %s\n",
2224                                         tgt_obd->obd_name);
2225                                 if (!rc)
2226                                         rc = err;
2227                         }
2228                 }
2229                 lov_tgts_unlock(lov);
2230                 RETURN(rc);
2231         }
2232
2233         if (KEY_IS("mds_conn")) {
2234                 if (vallen != sizeof(__u32))
2235                         RETURN(-EINVAL);
2236         } else if (KEY_IS("unlinked") || KEY_IS("unrecovery")) {
2237                 if (vallen != 0)
2238                         RETURN(-EINVAL);
2239         } else if (KEY_IS("sec") || KEY_IS("sec_flags")) {
2240                 struct lov_tgt_desc *tgt;
2241                 struct obd_export *exp;
2242                 int rc = 0, err, i;
2243
2244                 lov_tgts_lock(lov);
2245                 tgt = lov->tgts;
2246                 for (i = 0; i < lov->desc.ld_tgt_count; i++, tgt++) {
2247                         exp = tgt->ltd_exp;
2248                         /* during setup time the connections to osc might
2249                          * haven't been established.
2250                          */
2251                         if (exp == NULL) {
2252                                 struct obd_device *tgt_obd;
2253
2254                                 tgt_obd = class_find_client_obd(&tgt->uuid,
2255                                                                 OBD_OSC_DEVICENAME,
2256                                                                 &obddev->obd_uuid);
2257                                 if (!tgt_obd) {
2258                                         CERROR("can't set security flavor, "
2259                                                "device %s not attached?\n",
2260                                                 tgt->uuid.uuid);
2261                                         rc = -EINVAL;
2262                                         continue;
2263                                 }
2264                                 exp = tgt_obd->obd_self_export;
2265                         }
2266
2267                         err = obd_set_info(exp, keylen, key, vallen, val);
2268                         if (!rc)
2269                                 rc = err;
2270                 }
2271                 lov_tgts_unlock(lov);
2272
2273                 RETURN(rc);
2274         } else if (KEY_IS("auditlog")) {
2275                 ptl_nid_t nid = 0;
2276                 struct audit_msg * msg = val;
2277                 int i = 0, len = sizeof(nid);
2278                 struct obd_export * ost_exp;
2279                 //try to find client nid
2280                 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
2281                         if (!lov->tgts[i].ltd_exp)
2282                                 continue;
2283                         rc = obd_get_info(lov->tgts[i].ltd_exp, 10,
2284                                           "client_nid", &len, &nid);
2285                         if (!rc)
2286                                 break;
2287                 }
2288                 
2289                 i = (__u32)nid % lov->desc.ld_tgt_count;
2290                 
2291                 CDEBUG(D_INFO, "Select OST %i for nid %.8x\n",i, (__u32)nid);
2292                 
2293                 ost_exp = lov->tgts[i].ltd_exp;
2294                 if (!ost_exp) {
2295                         CERROR("can't send auditlog data,"
2296                                "device %s is not attached?\n",
2297                                lov->tgts[i].uuid.uuid);
2298                         RETURN(-EFAULT);
2299                 }
2300                 msg->nid = nid;   
2301                 rc = obd_set_info(ost_exp, keylen, key, vallen, val);
2302                 
2303                 RETURN(rc);               
2304         } else if (KEY_IS("audit_obj")) {
2305                 struct audit_lov_msg * msg = val;
2306                 int i = 0, index;
2307                 struct obd_export * ost_exp;
2308                 struct lov_oinfo *loi;
2309                 struct obdo *oa = NULL;
2310                 
2311                 oa = obdo_alloc();
2312                 if (oa == NULL)
2313                         RETURN(-ENOMEM);
2314
2315                 //set audit for all objects of file
2316                 CDEBUG(D_INFO, "Set audit 0x%x for objects\n", (__u32)msg->mask);
2317                 //use o_fid to store audit mask
2318                 oa->o_fid = msg->mask;
2319                 oa->o_uid = msg->uid;
2320                 oa->o_gid = msg->gid;
2321                 //iterate over all objects and set audit for each
2322                 for (i = 0, loi = msg->lsm->lsm_oinfo;
2323                      i < msg->lsm->lsm_stripe_count; i++, loi++) {
2324                         index = loi->loi_ost_idx;
2325                         ost_exp = lov->tgts[index].ltd_exp;
2326                         if (!ost_exp) {
2327                                 CERROR("can't send audit_obj data,"
2328                                        "device %s is not attached?\n",
2329                                        lov->tgts[i].uuid.uuid);
2330                                 continue;
2331                         }
2332                         oa->o_id = loi->loi_id;
2333                         oa->o_gr = loi->loi_gr;
2334                         
2335                         obd_set_info(ost_exp, keylen, key, sizeof(*oa), oa);
2336                 }
2337
2338                 obdo_free(oa);
2339                 RETURN(0);
2340         } else if (KEY_IS("audit")) {
2341                 int i = 0;
2342                 struct obd_export * ost_exp;
2343
2344                 //set audit for whole fs on OSS
2345                 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
2346                         ost_exp = lov->tgts[i].ltd_exp;
2347                         if (!ost_exp) {
2348                                 CERROR("can't send audit_fs data,"
2349                                        "device %s is not attached?\n",
2350                                        lov->tgts[i].uuid.uuid);
2351                                 continue;
2352                         }
2353                         obd_set_info(ost_exp, keylen, key, vallen, val);
2354                 }
2355                 
2356                 RETURN(rc);           
2357         } else if (KEY_IS("flush_cred") || KEY_IS("crypto_cb") ||
2358                    KEY_IS("capa_key")) {
2359                 struct lov_tgt_desc *tgt;
2360                 int rc = 0, i;
2361
2362                 tgt = lov->tgts;
2363                 for (i = 0; i < lov->desc.ld_tgt_count; i++, tgt++) {
2364                         if (!lov_tgt_active(lov, tgt, 0))
2365                                 continue;
2366                         rc = obd_set_info(tgt->ltd_exp,
2367                                           keylen, key, vallen, val);
2368                         lov_tgt_decref(lov, tgt);
2369                         if (rc)
2370                                 RETURN(rc);
2371                 }
2372
2373                 RETURN(0);
2374         } else if (KEY_IS("setext")) {
2375                 struct lov_tgt_desc *tgt;
2376                 int rc = 0, i;
2377
2378                 for (i = 0, tgt = lov->tgts; i < lov->desc.ld_tgt_count;
2379                      i++, tgt++) {
2380                         if (!tgt->ltd_exp)
2381                                 continue;
2382                         rc = obd_set_info(tgt->ltd_exp,
2383                                           keylen, key, vallen, val);
2384                         if (rc)
2385                                 RETURN(rc);
2386                 }
2387
2388                 RETURN(0);
2389         } else {
2390                 RETURN(-EINVAL);
2391         }
2392
2393         tgt = lov->tgts;
2394         for (i = 0; i < lov->desc.ld_tgt_count; i++, tgt++) {
2395                 int active = 0;
2396
2397                 if (val) {
2398                         if (!obd_uuid_equals(val, &tgt->uuid))
2399                                 continue;
2400                 } else if (!lov_tgt_active(lov, tgt, 0)) {
2401                         continue;
2402                 } else {
2403                         active = 1;
2404                 }
2405
2406                 err = obd_set_info(tgt->ltd_exp, keylen, key, vallen, val);
2407                 if (!rc)
2408                         rc = err;
2409                 if (active)
2410                         lov_tgt_decref(lov, tgt);
2411         }
2412         RETURN(rc);
2413 #undef KEY_IS
2414 }
2415
2416 #if 0
2417 struct lov_multi_wait {
2418         struct ldlm_lock *lock;
2419         wait_queue_t      wait;
2420         int               completed;
2421         int               generation;
2422 };
2423
2424 int lov_complete_many(struct obd_export *exp, struct lov_stripe_md *lsm,
2425                       struct lustre_handle *lockh)
2426 {
2427         struct lov_lock_handles *lov_lockh = NULL;
2428         struct lustre_handle *lov_lockhp;
2429         struct lov_obd *lov;
2430         struct lov_oinfo *loi;
2431         struct lov_multi_wait *queues;
2432         int rc = 0, i;
2433         ENTRY;
2434
2435         if (lsm_bad_magic(lsm))
2436                 RETURN(-EINVAL);
2437
2438         if (!exp || !exp->exp_obd)
2439                 RETURN(-ENODEV);
2440
2441         LASSERT(lockh != NULL);
2442         if (lsm->lsm_stripe_count > 1) {
2443                 lov_lockh = lov_handle2llh(lockh);
2444                 if (lov_lockh == NULL) {
2445                         CERROR("LOV: invalid lov lock handle %p\n", lockh);
2446                         RETURN(-EINVAL);
2447                 }
2448
2449                 lov_lockhp = lov_lockh->llh_handles;
2450         } else {
2451                 lov_lockhp = lockh;
2452         }
2453
2454         OBD_ALLOC(queues, lsm->lsm_stripe_count * sizeof(*queues));
2455         if (queues == NULL)
2456                 GOTO(out, rc = -ENOMEM);
2457
2458         lov = &exp->exp_obd->u.lov;
2459         for (i = 0, loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count;
2460              i++, loi++, lov_lockhp++) {
2461                 struct ldlm_lock *lock;
2462                 struct obd_device *obd;
2463                 unsigned long irqflags;
2464
2465                 lock = ldlm_handle2lock(lov_lockhp);
2466                 if (lock == NULL) {
2467                         CDEBUG(D_HA, "lov idx %d subobj "LPX64" no lock?\n",
2468                                loi->loi_ost_idx, loi->loi_id);
2469                         queues[i].completed = 1;
2470                         continue;
2471                 }
2472
2473                 queues[i].lock = lock;
2474                 init_waitqueue_entry(&(queues[i].wait), current);
2475                 add_wait_queue(lock->l_waitq, &(queues[i].wait));
2476
2477                 obd = class_exp2obd(lock->l_conn_export);
2478                 if (obd != NULL)
2479                         imp = obd->u.cli.cl_import;
2480                 if (imp != NULL) {
2481                         spin_lock_irqsave(&imp->imp_lock, irqflags);
2482                         queues[i].generation = imp->imp_generation;
2483                         spin_unlock_irqrestore(&imp->imp_lock, irqflags);
2484                 }
2485         }
2486
2487         lwi = LWI_TIMEOUT_INTR(obd_timeout * HZ, ldlm_expired_completion_wait,
2488                                interrupted_completion_wait, &lwd);
2489         rc = l_wait_event_added(check_multi_complete(queues, lsm), &lwi);
2490
2491         for (i = 0; i < lsm->lsm_stripe_count; i++)
2492                 remove_wait_queue(lock->l_waitq, &(queues[i].wait));
2493
2494         if (rc == -EINTR || rc == -ETIMEDOUT) {
2495
2496
2497         }
2498
2499  out:
2500         if (lov_lockh != NULL)
2501                 lov_llh_put(lov_lockh);
2502         RETURN(rc);
2503 }
2504 #endif
2505
2506 struct obd_ops lov_obd_ops = {
2507         .o_owner               = THIS_MODULE,
2508         .o_attach              = lov_attach,
2509         .o_detach              = lov_detach,
2510         .o_setup               = lov_setup,
2511         .o_cleanup             = lov_cleanup,
2512         .o_process_config      = lov_process_config,
2513         .o_connect             = lov_connect,
2514         .o_disconnect          = lov_disconnect,
2515         .o_statfs              = lov_statfs,
2516         .o_packmd              = lov_packmd,
2517         .o_unpackmd            = lov_unpackmd,
2518         .o_revalidate_md       = lov_revalidate_md,
2519         .o_create              = lov_create,
2520         .o_destroy             = lov_destroy,
2521         .o_getattr             = lov_getattr,
2522         .o_getattr_async       = lov_getattr_async,
2523         .o_setattr             = lov_setattr,
2524         .o_brw                 = lov_brw,
2525         .o_brw_async           = lov_brw_async,
2526         .o_prep_async_page     = lov_prep_async_page,
2527         .o_queue_async_io      = lov_queue_async_io,
2528         .o_set_async_flags     = lov_set_async_flags,
2529         .o_queue_group_io      = lov_queue_group_io,
2530         .o_trigger_group_io    = lov_trigger_group_io,
2531         .o_teardown_async_page = lov_teardown_async_page,
2532         .o_adjust_kms          = lov_adjust_kms,
2533         .o_punch               = lov_punch,
2534         .o_sync                = lov_sync,
2535         .o_enqueue             = lov_enqueue,
2536         .o_match               = lov_match,
2537         .o_change_cbdata       = lov_change_cbdata,
2538         .o_cancel              = lov_cancel,
2539         .o_cancel_unused       = lov_cancel_unused,
2540         .o_iocontrol           = lov_iocontrol,
2541         .o_get_info            = lov_get_info,
2542         .o_set_info            = lov_set_info,
2543         .o_llog_init           = lov_llog_init,
2544         .o_llog_finish         = lov_llog_finish,
2545         .o_notify              = lov_notify,
2546 };
2547
2548 int __init lov_init(void)
2549 {
2550         struct lprocfs_static_vars lvars;
2551         int rc;
2552         ENTRY;
2553
2554         lprocfs_init_vars(lov, &lvars);
2555         rc = class_register_type(&lov_obd_ops, NULL, lvars.module_vars,
2556                                  OBD_LOV_DEVICENAME);
2557         RETURN(rc);
2558 }
2559
2560 #ifdef __KERNEL__
2561 static void /*__exit*/ lov_exit(void)
2562 {
2563         class_unregister_type(OBD_LOV_DEVICENAME);
2564 }
2565
2566 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
2567 MODULE_DESCRIPTION("Lustre Logical Object Volume OBD driver");
2568 MODULE_LICENSE("GPL");
2569
2570 module_init(lov_init);
2571 module_exit(lov_exit);
2572 #endif