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