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