Whamcloud - gitweb
- lots of fixes and cleanups in cobd and cmobd.
[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
52 #include "lov_internal.h"
53
54 static int lov_stripe_offset(struct lov_stripe_md *lsm, obd_off lov_off,
55                              int stripeno, obd_off *obd_off);
56
57 struct lov_lock_handles {
58         struct portals_handle llh_handle;
59         atomic_t llh_refcount;
60         int llh_stripe_count;
61         struct lustre_handle llh_handles[0];
62 };
63
64 static void lov_llh_addref(void *llhp)
65 {
66         struct lov_lock_handles *llh = llhp;
67
68         atomic_inc(&llh->llh_refcount);
69         CDEBUG(D_INFO, "GETting llh %p : new refcount %d\n", llh,
70                atomic_read(&llh->llh_refcount));
71 }
72
73 static struct lov_lock_handles *lov_llh_new(struct lov_stripe_md *lsm)
74 {
75         struct lov_lock_handles *llh;
76
77         OBD_ALLOC(llh, sizeof *llh +
78                   sizeof(*llh->llh_handles) * lsm->lsm_stripe_count);
79         if (llh == NULL) {
80                 CERROR("out of memory\n");
81                 return NULL;
82         }
83         atomic_set(&llh->llh_refcount, 2);
84         llh->llh_stripe_count = lsm->lsm_stripe_count;
85         INIT_LIST_HEAD(&llh->llh_handle.h_link);
86         class_handle_hash(&llh->llh_handle, lov_llh_addref);
87         return llh;
88 }
89
90 static struct lov_lock_handles *lov_handle2llh(struct lustre_handle *handle)
91 {
92         ENTRY;
93         LASSERT(handle != NULL);
94         RETURN(class_handle2object(handle->cookie));
95 }
96
97 static void lov_llh_put(struct lov_lock_handles *llh)
98 {
99         CDEBUG(D_INFO, "PUTting llh %p : new refcount %d\n", llh,
100                atomic_read(&llh->llh_refcount) - 1);
101         LASSERT(atomic_read(&llh->llh_refcount) > 0 &&
102                 atomic_read(&llh->llh_refcount) < 0x5a5a);
103         if (atomic_dec_and_test(&llh->llh_refcount)) {
104                 LASSERT(list_empty(&llh->llh_handle.h_link));
105                 OBD_FREE(llh, sizeof *llh +
106                          sizeof(*llh->llh_handles) * llh->llh_stripe_count);
107         }
108 }
109
110 static void lov_llh_destroy(struct lov_lock_handles *llh)
111 {
112         class_handle_unhash(&llh->llh_handle);
113         lov_llh_put(llh);
114 }
115
116 /* obd methods */
117 #define MAX_STRING_SIZE 128
118 static int lov_connect_obd(struct obd_device *obd, struct lov_tgt_desc *tgt,
119                            int activate, unsigned long connect_flags)
120 {
121         struct obd_uuid lov_osc_uuid = { "LOV_OSC_UUID" };
122         struct obd_uuid *tgt_uuid = &tgt->uuid;
123
124 #ifdef __KERNEL__
125         struct proc_dir_entry *lov_proc_dir;
126 #endif
127         struct lov_obd *lov = &obd->u.lov;
128         struct lustre_handle conn = {0, };
129         struct obd_device *tgt_obd;
130         int rc;
131         ENTRY;
132
133         tgt_obd = class_find_client_obd(tgt_uuid, LUSTRE_OSC_NAME,
134                                         &obd->obd_uuid);
135
136         if (!tgt_obd) {
137                 CERROR("Target %s not attached\n", tgt_uuid->uuid);
138                 RETURN(-EINVAL);
139         }
140
141         if (!tgt_obd->obd_set_up) {
142                 CERROR("Target %s not set up\n", tgt_uuid->uuid);
143                 RETURN(-EINVAL);
144         }
145
146         if (activate) {
147                 tgt_obd->obd_no_recov = 0;
148                 ptlrpc_activate_import(tgt_obd->u.cli.cl_import);
149         }
150
151         if (tgt_obd->u.cli.cl_import->imp_invalid) {
152                 CERROR("not connecting OSC %s; administratively "
153                        "disabled\n", tgt_uuid->uuid);
154                 rc = obd_register_observer(tgt_obd, obd);
155                 if (rc) {
156                         CERROR("Target %s register_observer error %d; "
157                                "will not be able to reactivate\n",
158                                tgt_uuid->uuid, rc);
159                 }
160                 RETURN(0);
161         }
162
163         rc = obd_connect(&conn, tgt_obd, &lov_osc_uuid, connect_flags);
164         if (rc) {
165                 CERROR("Target %s connect error %d\n", tgt_uuid->uuid, rc);
166                 RETURN(rc);
167         }
168         tgt->ltd_exp = class_conn2export(&conn);
169
170         rc = obd_register_observer(tgt_obd, obd);
171         if (rc) {
172                 CERROR("Target %s register_observer error %d\n",
173                        tgt_uuid->uuid, rc);
174                 obd_disconnect(tgt->ltd_exp, 0);
175                 tgt->ltd_exp = NULL;
176                 RETURN(rc);
177         }
178
179         tgt->active = 1;
180         lov->desc.ld_active_tgt_count++;
181
182 #ifdef __KERNEL__
183         lov_proc_dir = lprocfs_srch(obd->obd_proc_entry, "target_obds");
184         if (lov_proc_dir) {
185                 struct obd_device *osc_obd = class_conn2obd(&conn);
186                 struct proc_dir_entry *osc_symlink;
187                 char name[MAX_STRING_SIZE + 1];
188
189                 LASSERT(osc_obd != NULL);
190                 LASSERT(osc_obd->obd_type != NULL);
191                 LASSERT(osc_obd->obd_type->typ_name != NULL);
192                 name[MAX_STRING_SIZE] = '\0';
193                 snprintf(name, MAX_STRING_SIZE, "../../../%s/%s",
194                          osc_obd->obd_type->typ_name,
195                          osc_obd->obd_name);
196                 osc_symlink = proc_symlink(osc_obd->obd_name, lov_proc_dir,
197                                            name);
198                 if (osc_symlink == NULL) {
199                         CERROR("could not register LOV target "
200                                "/proc/fs/lustre/%s/%s/target_obds/%s\n",
201                                obd->obd_type->typ_name, obd->obd_name,
202                                osc_obd->obd_name);
203                         lprocfs_remove(lov_proc_dir);
204                         lov_proc_dir = NULL;
205                 }
206         }
207 #endif
208
209         RETURN(0);
210 }
211
212 static int lov_connect(struct lustre_handle *conn, struct obd_device *obd,
213                        struct obd_uuid *cluuid, unsigned long connect_flags)
214 {
215 #ifdef __KERNEL__
216         struct proc_dir_entry *lov_proc_dir;
217 #endif
218         struct lov_obd *lov = &obd->u.lov;
219         struct lov_tgt_desc *tgt;
220         struct obd_export *exp;
221         int rc, rc2, i;
222         ENTRY;
223
224         rc = class_connect(conn, obd, cluuid);
225         if (rc)
226                 RETURN(rc);
227
228         exp = class_conn2export(conn);
229
230         /* We don't want to actually do the underlying connections more than
231          * once, so keep track. */
232         lov->refcount++;
233         if (lov->refcount > 1) {
234                 class_export_put(exp);
235                 RETURN(0);
236         }
237
238 #ifdef __KERNEL__
239         lov_proc_dir = lprocfs_register("target_obds", obd->obd_proc_entry,
240                                         NULL, NULL);
241         if (IS_ERR(lov_proc_dir)) {
242                 CERROR("could not register /proc/fs/lustre/%s/%s/target_obds.",
243                        obd->obd_type->typ_name, obd->obd_name);
244                 lov_proc_dir = NULL;
245         }
246 #endif
247
248         /* connect_flags is the MDS number, save for use in lov_add_obd */
249         lov->lov_connect_flags = connect_flags;
250         for (i = 0, tgt = lov->tgts; i < lov->desc.ld_tgt_count; i++, tgt++) {
251                 if (obd_uuid_empty(&tgt->uuid))
252                         continue;
253                 rc = lov_connect_obd(obd, tgt, 0, connect_flags);
254                 if (rc)
255                         GOTO(out_disc, rc);
256         }
257
258         class_export_put(exp);
259         RETURN (0);
260
261  out_disc:
262 #ifdef __KERNEL__
263         if (lov_proc_dir)
264                 lprocfs_remove(lov_proc_dir);
265 #endif
266
267         while (i-- > 0) {
268                 struct obd_uuid uuid;
269                 --tgt;
270                 --lov->desc.ld_active_tgt_count;
271                 tgt->active = 0;
272                 /* save for CERROR below; (we know it's terminated) */
273                 uuid = tgt->uuid;
274                 rc2 = obd_disconnect(tgt->ltd_exp, 0);
275                 if (rc2)
276                         CERROR("error: LOV target %s disconnect on OST idx %d: "
277                                "rc = %d\n", uuid.uuid, i, rc2);
278         }
279         class_disconnect(exp, 0);
280         RETURN (rc);
281 }
282
283 static int lov_disconnect_obd(struct obd_device *obd, 
284                               struct lov_tgt_desc *tgt,
285                               unsigned long flags)
286 {
287 #ifdef __KERNEL__
288         struct proc_dir_entry *lov_proc_dir;
289 #endif
290         struct obd_device *osc_obd = class_exp2obd(tgt->ltd_exp);
291         struct lov_obd *lov = &obd->u.lov;
292         int rc;
293         ENTRY;
294
295 #ifdef __KERNEL__
296         lov_proc_dir = lprocfs_srch(obd->obd_proc_entry, "target_obds");
297         if (lov_proc_dir) {
298                 struct proc_dir_entry *osc_symlink;
299
300                 osc_symlink = lprocfs_srch(lov_proc_dir, osc_obd->obd_name);
301                 if (osc_symlink) {
302                         lprocfs_remove(osc_symlink);
303                 } else {
304                         CERROR("/proc/fs/lustre/%s/%s/target_obds/%s missing\n",
305                                obd->obd_type->typ_name, obd->obd_name,
306                                osc_obd->obd_name);
307                 }
308         }
309 #endif
310         
311         if (obd->obd_no_recov) {
312                 /* Pass it on to our clients.
313                  * XXX This should be an argument to disconnect,
314                  * XXX not a back-door flag on the OBD.  Ah well.
315                  */
316                 if (osc_obd)
317                         osc_obd->obd_no_recov = 1;
318         }
319
320         obd_register_observer(tgt->ltd_exp->exp_obd, NULL);
321         rc = obd_disconnect(tgt->ltd_exp, flags);
322         if (rc) {
323                 if (tgt->active) {
324                         CERROR("Target %s disconnect error %d\n",
325                                tgt->uuid.uuid, rc);
326                 }
327                 rc = 0;
328         }
329
330         if (tgt->active) {
331                 tgt->active = 0;
332                 lov->desc.ld_active_tgt_count--;
333         }
334
335         tgt->ltd_exp = NULL;
336         RETURN(0);
337 }
338
339 static int lov_disconnect(struct obd_export *exp, unsigned long flags)
340 {
341         struct obd_device *obd = class_exp2obd(exp);
342 #ifdef __KERNEL__
343         struct proc_dir_entry *lov_proc_dir;
344 #endif
345         struct lov_obd *lov = &obd->u.lov;
346         struct lov_tgt_desc *tgt;
347         int rc, i;
348         ENTRY;
349
350         if (!lov->tgts)
351                 goto out_local;
352
353         /* Only disconnect the underlying layers on the final disconnect. */
354         lov->refcount--;
355         if (lov->refcount != 0)
356                 goto out_local;
357
358         for (i = 0, tgt = lov->tgts; i < lov->desc.ld_tgt_count; i++, tgt++) {
359                 if (tgt->ltd_exp)
360                         lov_disconnect_obd(obd, tgt, flags);
361         }
362
363 #ifdef __KERNEL__
364         lov_proc_dir = lprocfs_srch(obd->obd_proc_entry, "target_obds");
365         if (lov_proc_dir) {
366                 lprocfs_remove(lov_proc_dir);
367         } else {
368                 CERROR("/proc/fs/lustre/%s/%s/target_obds missing.",
369                        obd->obd_type->typ_name, obd->obd_name);
370         }
371 #endif
372
373  out_local:
374         rc = class_disconnect(exp, 0);
375         RETURN(rc);
376 }
377
378 /* Error codes:
379  *
380  *  -EINVAL  : UUID can't be found in the LOV's target list
381  *  -ENOTCONN: The UUID is found, but the target connection is bad (!)
382  *  -EBADF   : The UUID is found, but the OBD is the wrong type (!)
383  */
384 static int lov_set_osc_active(struct lov_obd *lov, struct obd_uuid *uuid,
385                               int activate)
386 {
387         struct lov_tgt_desc *tgt;
388         int i, rc = 0;
389         ENTRY;
390
391         CDEBUG(D_INFO, "Searching in lov %p for uuid %s (activate=%d)\n",
392                lov, uuid->uuid, activate);
393
394         spin_lock(&lov->lov_lock);
395         for (i = 0, tgt = lov->tgts; i < lov->desc.ld_tgt_count; i++, tgt++) {
396                 CDEBUG(D_INFO, "lov idx %d is %s conn "LPX64"\n",
397                        i, tgt->uuid.uuid, tgt->ltd_exp->exp_handle.h_cookie);
398                 if (strncmp(uuid->uuid, tgt->uuid.uuid, sizeof uuid->uuid) == 0)
399                         break;
400         }
401
402         if (i == lov->desc.ld_tgt_count)
403                 GOTO(out, rc = -EINVAL);
404
405
406         if (tgt->active == activate) {
407                 CDEBUG(D_INFO, "OSC %s already %sactive!\n", uuid->uuid,                       
408                         activate ? "" : "in");
409                 GOTO(out, rc);
410         }
411
412         CDEBUG(D_INFO, "Marking OSC %s %sactive\n", uuid->uuid,
413                activate ? "" : "in");
414
415         tgt->active = activate;
416         if (activate)
417                 lov->desc.ld_active_tgt_count++;
418         else
419                 lov->desc.ld_active_tgt_count--;
420
421         EXIT;
422  out:
423         spin_unlock(&lov->lov_lock);
424         return rc;
425 }
426
427 static int lov_notify(struct obd_device *obd, struct obd_device *watched,
428                       int active, void *data)
429 {
430         int rc;
431         struct obd_uuid *uuid;
432         ENTRY;
433
434         if (strcmp(watched->obd_type->typ_name, LUSTRE_OSC_NAME)) {
435                 CERROR("unexpected notification of %s %s!\n",
436                        watched->obd_type->typ_name,
437                        watched->obd_name);
438                 return -EINVAL;
439         }
440         uuid = &watched->u.cli.cl_import->imp_target_uuid;
441
442         /* Set OSC as active before notifying the observer, so the
443          * observer can use the OSC normally.  
444          */
445         rc = lov_set_osc_active(&obd->u.lov, uuid, active);
446         if (rc) {
447                 CERROR("%sactivation of %s failed: %d\n",
448                        active ? "" : "de", uuid->uuid, rc);
449                 RETURN(rc);
450         }
451
452         if (obd->obd_observer)
453                 /* Pass the notification up the chain. */
454                 rc = obd_notify(obd->obd_observer, watched, active, data);
455
456         RETURN(rc);
457 }
458
459 int lov_attach(struct obd_device *dev, obd_count len, void *data)
460 {
461         struct lprocfs_static_vars lvars;
462         int rc;
463
464         lprocfs_init_vars(lov, &lvars);
465         rc = lprocfs_obd_attach(dev, lvars.obd_vars);
466         if (rc == 0) {
467 #ifdef __KERNEL__
468                 struct proc_dir_entry *entry;
469
470                 entry = create_proc_entry("target_obd_status", 0444, 
471                                           dev->obd_proc_entry);
472                 if (entry == NULL) {
473                         rc = -ENOMEM;
474                 } else {
475                         entry->proc_fops = &lov_proc_target_fops;
476                         entry->data = dev;
477                 }
478 #endif
479         }
480         return rc;
481 }
482
483 int lov_detach(struct obd_device *dev)
484 {
485         return lprocfs_obd_detach(dev);
486 }
487
488 static int lov_setup(struct obd_device *obd, obd_count len, void *buf)
489 {
490         struct lov_obd *lov = &obd->u.lov;
491         struct lustre_cfg *lcfg = buf;
492         struct lov_desc *desc;
493         int count;
494         ENTRY;
495
496         if (lcfg->lcfg_inllen1 < 1) {
497                 CERROR("LOV setup requires a descriptor\n");
498                 RETURN(-EINVAL);
499         }
500
501         desc = (struct lov_desc *)lcfg->lcfg_inlbuf1;
502         if (sizeof(*desc) > lcfg->lcfg_inllen1) {
503                 CERROR("descriptor size wrong: %d > %d\n",
504                        (int)sizeof(*desc), lcfg->lcfg_inllen1);
505                 RETURN(-EINVAL);
506         }
507  
508         /* Because of 64-bit divide/mod operations only work with a 32-bit
509          * divisor in a 32-bit kernel, we cannot support a stripe width
510          * of 4GB or larger on 32-bit CPUs.
511          */
512        
513         count = desc->ld_default_stripe_count;
514         if (count && (count * desc->ld_default_stripe_size) > ~0UL) {
515                 CERROR("LOV: stripe width "LPU64"x%u > %lu on 32-bit system\n",
516                        desc->ld_default_stripe_size, count, ~0UL);
517                 RETURN(-EINVAL);
518         }
519         if (desc->ld_tgt_count > 0) {
520                 lov->bufsize= sizeof(struct lov_tgt_desc) * desc->ld_tgt_count;
521         } else {
522                 lov->bufsize = sizeof(struct lov_tgt_desc) * LOV_MAX_TGT_COUNT;  
523         }
524         OBD_ALLOC(lov->tgts, lov->bufsize);
525         if (lov->tgts == NULL) {
526                 lov->bufsize = 0;
527                 CERROR("couldn't allocate %d bytes for target table.\n",
528                        lov->bufsize);
529                 RETURN(-EINVAL);
530         }
531
532         desc->ld_tgt_count = 0;
533         desc->ld_active_tgt_count = 0;
534         lov->desc = *desc;
535         spin_lock_init(&lov->lov_lock);
536         sema_init(&lov->lov_llog_sem, 1);
537
538         RETURN(0);
539 }
540
541 static int lov_cleanup(struct obd_device *obd, int flags)
542 {
543         struct lov_obd *lov = &obd->u.lov;
544
545         OBD_FREE(lov->tgts, lov->bufsize);
546         RETURN(0);
547 }
548
549 static int
550 lov_add_obd(struct obd_device *obd, struct obd_uuid *uuidp, int index, int gen)
551 {
552         struct lov_obd *lov = &obd->u.lov;
553         struct lov_tgt_desc *tgt;
554         int rc;
555         ENTRY;
556
557         CDEBUG(D_CONFIG, "uuid: %s idx: %d gen: %d\n",
558                uuidp->uuid, index, gen);
559
560         if ((index < 0) || (index >= LOV_MAX_TGT_COUNT)) {
561                 CERROR("request to add OBD %s at invalid index: %d\n",
562                        uuidp->uuid, index);
563                 RETURN(-EINVAL);
564         }
565
566         if (gen <= 0) {
567                 CERROR("request to add OBD %s with invalid generation: %d\n",
568                        uuidp->uuid, gen);
569                 RETURN(-EINVAL);
570         }
571
572         tgt = lov->tgts + index;
573         if (!obd_uuid_empty(&tgt->uuid)) {
574                 CERROR("OBD already assigned at LOV target index %d\n",
575                        index);
576                 RETURN(-EEXIST);
577         }
578
579         tgt->uuid = *uuidp;
580         /* XXX - add a sanity check on the generation number. */
581         tgt->ltd_gen = gen;
582
583         if (index >= lov->desc.ld_tgt_count)
584                 lov->desc.ld_tgt_count = index + 1;
585
586         CDEBUG(D_CONFIG, "idx: %d ltd_gen: %d ld_tgt_count: %d\n",
587                 index, tgt->ltd_gen, lov->desc.ld_tgt_count);
588
589         if (lov->refcount == 0)
590                 RETURN(0);
591
592         if (tgt->ltd_exp) {
593                 struct obd_device *osc_obd;
594
595                 osc_obd = class_exp2obd(tgt->ltd_exp);
596                 if (osc_obd)
597                         osc_obd->obd_no_recov = 0;
598         }
599
600         rc = lov_connect_obd(obd, tgt, 1, lov->lov_connect_flags);
601         if (rc)
602                 GOTO(out, rc);
603
604         if (obd->obd_observer) {
605                 /* tell the mds_lov about the new target */
606                 rc = obd_notify(obd->obd_observer, tgt->ltd_exp->exp_obd, 1,
607                                 (void *)index);
608         }
609
610         GOTO(out, rc);
611  out:
612         if (rc && tgt->ltd_exp != NULL)
613                 lov_disconnect_obd(obd, tgt, 0);
614         return rc;
615 }
616
617 static int
618 lov_del_obd(struct obd_device *obd, struct obd_uuid *uuidp, int index, int gen)
619 {
620         struct lov_obd *lov = &obd->u.lov;
621         struct lov_tgt_desc *tgt;
622         int count = lov->desc.ld_tgt_count;
623         int rc = 0;
624         ENTRY;
625
626         CDEBUG(D_CONFIG, "uuid: %s idx: %d gen: %d\n",
627                uuidp->uuid, index, gen);
628
629         if (index >= count) {
630                 CERROR("LOV target index %d >= number of LOV OBDs %d.\n",
631                        index, count);
632                 RETURN(-EINVAL);
633         }
634
635         tgt = lov->tgts + index;
636
637         if (obd_uuid_empty(&tgt->uuid)) {
638                 CERROR("LOV target at index %d is not setup.\n", index);
639                 RETURN(-EINVAL);
640         }
641
642         if (strncmp(uuidp->uuid, tgt->uuid.uuid, sizeof uuidp->uuid) != 0) {
643                 CERROR("LOV target UUID %s at index %d doesn't match %s.\n",
644                        tgt->uuid.uuid, index, uuidp->uuid);
645                 RETURN(-EINVAL);
646         }
647
648         if (tgt->ltd_exp) {
649                 struct obd_device *osc_obd;
650
651                 osc_obd = class_exp2obd(tgt->ltd_exp);
652                 if (osc_obd) {
653                         osc_obd->obd_no_recov = 1;
654                         rc = obd_llog_finish(osc_obd, &osc_obd->obd_llogs, 1);
655                         if (rc)
656                                 CERROR("osc_llog_finish error: %d\n", rc);
657                 }
658                 lov_disconnect_obd(obd, tgt, 0);
659         }
660
661         /* XXX - right now there is a dependency on ld_tgt_count being the
662          * maximum tgt index for computing the mds_max_easize. So we can't
663          * shrink it. */
664
665         /* lt_gen = 0 will mean it will not match the gen of any valid loi */
666         memset(tgt, 0, sizeof(*tgt));
667
668         CDEBUG(D_CONFIG, "uuid: %s idx: %d gen: %d exp: %p active: %d\n",
669                tgt->uuid.uuid, index, tgt->ltd_gen, tgt->ltd_exp, tgt->active);
670
671         RETURN(rc);
672 }
673
674 static int lov_process_config(struct obd_device *obd, obd_count len, void *buf)
675 {
676         struct lustre_cfg *lcfg = buf;
677         struct obd_uuid obd_uuid;
678         int cmd;
679         int index;
680         int gen;
681         int rc = 0;
682         ENTRY;
683
684         switch(cmd = lcfg->lcfg_command) {
685         case LCFG_LOV_ADD_OBD:
686         case LCFG_LOV_DEL_OBD: {
687                 if (lcfg->lcfg_inllen1 > sizeof(obd_uuid.uuid))
688                         GOTO(out, rc = -EINVAL);
689
690                 obd_str2uuid(&obd_uuid, lcfg->lcfg_inlbuf1);
691
692                 if (sscanf(lcfg->lcfg_inlbuf2, "%d", &index) != 1)
693                         GOTO(out, rc = -EINVAL);
694                 if (sscanf(lcfg->lcfg_inlbuf3, "%d", &gen) != 1)
695                         GOTO(out, rc = -EINVAL);
696                 if (cmd == LCFG_LOV_ADD_OBD)
697                         rc = lov_add_obd(obd, &obd_uuid, index, gen);
698                 else
699                         rc = lov_del_obd(obd, &obd_uuid, index, gen);
700                 GOTO(out, rc);
701         }
702         default: {
703                 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
704                 GOTO(out, rc = -EINVAL);
705
706         }
707         }
708 out:
709         RETURN(rc);
710 }
711
712 /* compute object size given "stripeno" and the ost size */
713 static obd_size lov_stripe_size(struct lov_stripe_md *lsm, obd_size ost_size,
714                                 int stripeno)
715 {
716         unsigned long ssize  = lsm->lsm_stripe_size;
717         unsigned long swidth = ssize * lsm->lsm_stripe_count;
718         unsigned long stripe_size;
719         obd_size lov_size;
720
721         if (ost_size == 0)
722                 return 0;
723
724         /* do_div(a, b) returns a % b, and a = a / b */
725         stripe_size = do_div(ost_size, ssize);
726         if (stripe_size)
727                 lov_size = ost_size * swidth + stripeno * ssize + stripe_size;
728         else
729                 lov_size = (ost_size - 1) * swidth + (stripeno + 1) * ssize;
730
731         return lov_size;
732 }
733
734 static void lov_merge_attrs(struct obdo *tgt, struct obdo *src, obd_valid valid,
735                             struct lov_stripe_md *lsm, int stripeno, int *set)
736 {
737         valid &= src->o_valid;
738
739         if (*set) {
740                 if (valid & OBD_MD_FLSIZE) {
741                         /* this handles sparse files properly */
742                         obd_size lov_size;
743
744                         lov_size = lov_stripe_size(lsm, src->o_size, stripeno);
745                         if (lov_size > tgt->o_size)
746                                 tgt->o_size = lov_size;
747                 }
748                 if (valid & OBD_MD_FLBLOCKS)
749                         tgt->o_blocks += src->o_blocks;
750                 if (valid & OBD_MD_FLBLKSZ)
751                         tgt->o_blksize += src->o_blksize;
752                 if (valid & OBD_MD_FLCTIME && tgt->o_ctime < src->o_ctime)
753                         tgt->o_ctime = src->o_ctime;
754                 if (valid & OBD_MD_FLMTIME && tgt->o_mtime < src->o_mtime)
755                         tgt->o_mtime = src->o_mtime;
756         } else {
757                 memcpy(tgt, src, sizeof(*tgt));
758                 tgt->o_id = lsm->lsm_object_id;
759                 if (valid & OBD_MD_FLSIZE)
760                         tgt->o_size = lov_stripe_size(lsm,src->o_size,stripeno);
761                 *set = 1;
762         }
763 }
764
765 #ifndef log2
766 #define log2(n) ffz(~(n))
767 #endif
768
769 static int lov_clear_orphans(struct obd_export *export, struct obdo *src_oa,
770                              struct lov_stripe_md **ea,
771                              struct obd_trans_info *oti)
772 {
773         struct lov_obd *lov;
774         struct obdo *tmp_oa;
775         struct obd_uuid *ost_uuid = NULL;
776         int rc = 0, i;
777         ENTRY;
778
779         LASSERT(src_oa->o_valid & OBD_MD_FLFLAGS &&
780                 src_oa->o_flags == OBD_FL_DELORPHAN);
781
782         lov = &export->exp_obd->u.lov;
783
784         tmp_oa = obdo_alloc();
785         if (tmp_oa == NULL)
786                 RETURN(-ENOMEM);
787
788         if (src_oa->o_valid & OBD_MD_FLINLINE) {
789                 ost_uuid = (struct obd_uuid *)src_oa->o_inline;
790                 CDEBUG(D_HA, "clearing orphans only for %s\n",
791                        ost_uuid->uuid);
792         }
793
794         for (i = 0; i < lov->desc.ld_tgt_count; i++) {
795                 struct lov_stripe_md obj_md;
796                 struct lov_stripe_md *obj_mdp = &obj_md;
797                 int err;
798
799                 /* if called for a specific target, we don't
800                    care if it is not active. */
801                 if (lov->tgts[i].active == 0 && ost_uuid == NULL) {
802                         CDEBUG(D_HA, "lov idx %d inactive\n", i);
803                         continue;
804                 }
805
806                 if (ost_uuid && !obd_uuid_equals(ost_uuid, &lov->tgts[i].uuid))
807                         continue;
808
809                 memcpy(tmp_oa, src_oa, sizeof(*tmp_oa));
810
811                 /* XXX: LOV STACKING: use real "obj_mdp" sub-data */
812                 err = obd_create(lov->tgts[i].ltd_exp, tmp_oa, &obj_mdp, oti);
813                 if (err)
814                         /* This export will be disabled until it is recovered,
815                            and then orphan recovery will be completed. */
816                         CERROR("error in orphan recovery on OST idx %d/%d: "
817                                "rc = %d\n", i, lov->desc.ld_tgt_count, err);
818
819                 if (ost_uuid)
820                         break;
821         }
822         obdo_free(tmp_oa);
823         RETURN(rc);
824 }
825
826 #define LOV_CREATE_RESEED_INTERVAL 1000
827
828 /* the LOV expects oa->o_id to be set to the LOV object id */
829 static int lov_create(struct obd_export *exp, struct obdo *src_oa,
830                       struct lov_stripe_md **ea, struct obd_trans_info *oti)
831 {
832         static int ost_start_idx, ost_start_count;
833         struct lov_obd *lov;
834         struct lov_stripe_md *lsm;
835         struct lov_oinfo *loi = NULL;
836         struct obdo *tmp_oa, *ret_oa;
837         struct llog_cookie *cookies = NULL;
838         unsigned ost_count, ost_idx;
839         int set = 0, obj_alloc = 0, cookie_sent = 0, rc = 0, i;
840         ENTRY;
841
842         LASSERT(ea != NULL);
843
844         if ((src_oa->o_valid & OBD_MD_FLFLAGS) &&
845             src_oa->o_flags == OBD_FL_DELORPHAN) {
846                 rc = lov_clear_orphans(exp, src_oa, ea, oti);
847                 RETURN(rc);
848         }
849
850         if (exp == NULL)
851                 RETURN(-EINVAL);
852
853         lov = &exp->exp_obd->u.lov;
854
855         if (!lov->desc.ld_active_tgt_count)
856                 RETURN(-EIO);
857
858         /* Recreate a specific object id at the given OST index */
859         if ((src_oa->o_valid & OBD_MD_FLFLAGS) &&
860             (src_oa->o_flags & OBD_FL_RECREATE_OBJS)) {
861                  struct lov_stripe_md obj_md;
862                  struct lov_stripe_md *obj_mdp = &obj_md;
863
864                  ost_idx = src_oa->o_nlink;
865                  lsm = *ea;
866                  if (lsm == NULL)
867                         RETURN(-EINVAL);
868                  if (ost_idx >= lov->desc.ld_tgt_count)
869                          RETURN(-EINVAL);
870                  for (i = 0; i < lsm->lsm_stripe_count; i++) {
871                          if (lsm->lsm_oinfo[i].loi_ost_idx == ost_idx) {
872                                  if (lsm->lsm_oinfo[i].loi_id != src_oa->o_id ||
873                                      lsm->lsm_oinfo[i].loi_gr != src_oa->o_gr) {
874                                          RETURN(-EINVAL);
875                                  }
876                                  break;
877                          }
878                  }
879                  if (i == lsm->lsm_stripe_count)
880                          RETURN(-EINVAL);
881
882                  rc = obd_create(lov->tgts[ost_idx].ltd_exp, src_oa,
883                                  &obj_mdp, oti);
884                  RETURN(rc);
885         }
886
887         ret_oa = obdo_alloc();
888         if (!ret_oa)
889                 RETURN(-ENOMEM);
890
891         tmp_oa = obdo_alloc();
892         if (!tmp_oa)
893                 GOTO(out_oa, rc = -ENOMEM);
894
895         lsm = *ea;
896         if (lsm == NULL) {
897                 int stripes;
898                 ost_count = lov_get_stripecnt(lov, 0);
899
900                 /* If the MDS file was truncated up to some size, stripe over
901                  * enough OSTs to allow the file to be created at that size. */
902                 if (src_oa->o_valid & OBD_MD_FLSIZE) {
903                         stripes=((src_oa->o_size+LUSTRE_STRIPE_MAXBYTES)>>12)-1;
904                         do_div(stripes, (__u32)(LUSTRE_STRIPE_MAXBYTES >> 12));
905
906                         if (stripes > lov->desc.ld_active_tgt_count)
907                                 RETURN(-EFBIG);
908                         if (stripes < ost_count)
909                                 stripes = ost_count;
910                 } else {
911                         stripes = ost_count;
912                 }
913
914                 rc = lov_alloc_memmd(&lsm, stripes, lov->desc.ld_pattern ?
915                                      lov->desc.ld_pattern : LOV_PATTERN_RAID0);
916                 if (rc < 0)
917                         GOTO(out_tmp, rc);
918
919                 rc = 0;
920         }
921
922         ost_count = lov->desc.ld_tgt_count;
923
924         LASSERT(src_oa->o_gr > 0);
925         LASSERT(src_oa->o_valid & OBD_MD_FLID);
926         lsm->lsm_object_id = src_oa->o_id;
927         lsm->lsm_object_gr = src_oa->o_gr;
928         if (!lsm->lsm_stripe_size)
929                 lsm->lsm_stripe_size = lov->desc.ld_default_stripe_size;
930         if (!lsm->lsm_pattern) {
931                 lsm->lsm_pattern = lov->desc.ld_pattern ?
932                         lov->desc.ld_pattern : LOV_PATTERN_RAID0;
933         }
934
935         if (*ea == NULL || lsm->lsm_oinfo[0].loi_ost_idx >= ost_count) {
936                 if (--ost_start_count <= 0) {
937                         ost_start_idx = ll_insecure_random_int();
938                         ost_start_count = LOV_CREATE_RESEED_INTERVAL;
939                 } else if (lsm->lsm_stripe_count >=
940                            lov->desc.ld_active_tgt_count) {
941                         /* If we allocate from all of the stripes, make the
942                          * next file start on the next OST. */
943                         ++ost_start_idx;
944                 }
945                 ost_idx = ost_start_idx % ost_count;
946         } else {
947                 ost_idx = lsm->lsm_oinfo[0].loi_ost_idx;
948         }
949
950         CDEBUG(D_INODE, "allocating %d subobjs for objid "LPX64" at idx %d\n",
951                lsm->lsm_stripe_count, lsm->lsm_object_id, ost_idx);
952
953         /* XXX LOV STACKING: need to figure out how many real OSCs */
954         if (oti && (src_oa->o_valid & OBD_MD_FLCOOKIE)) {
955                 oti_alloc_cookies(oti, lsm->lsm_stripe_count);
956                 if (!oti->oti_logcookies)
957                         GOTO(out_cleanup, rc = -ENOMEM);
958                 cookies = oti->oti_logcookies;
959         }
960
961         loi = lsm->lsm_oinfo;
962         for (i = 0; i < ost_count; i++, ost_idx = (ost_idx + 1) % ost_count) {
963                 struct lov_stripe_md obj_md;
964                 struct lov_stripe_md *obj_mdp = &obj_md;
965                 int err;
966
967                 ++ost_start_idx;
968                 if (lov->tgts[ost_idx].active == 0) {
969                         if (!obd_uuid_empty(&lov->tgts[ost_idx].uuid))
970                                 CDEBUG(D_HA, "lov idx %d inactive\n", ost_idx);
971                         continue;
972                 }
973
974                 /* create data objects with "parent" OA */
975                 memcpy(tmp_oa, src_oa, sizeof(*tmp_oa));
976
977                 /* XXX When we start creating objects on demand, we need to
978                  *     make sure that we always create the object on the
979                  *     stripe which holds the existing file size.
980                  */
981                 if (src_oa->o_valid & OBD_MD_FLSIZE) {
982                         if (lov_stripe_offset(lsm, src_oa->o_size, i,
983                                               &tmp_oa->o_size) < 0 &&
984                             tmp_oa->o_size)
985                                 tmp_oa->o_size--;
986
987                         CDEBUG(D_INODE, "stripe %d has size "LPU64"/"LPU64"\n",
988                                i, tmp_oa->o_size, src_oa->o_size);
989                 }
990
991
992                 /* XXX: LOV STACKING: use real "obj_mdp" sub-data */
993                 err = obd_create(lov->tgts[ost_idx].ltd_exp, tmp_oa, &obj_mdp,
994                                  oti);
995                 if (err) {
996                         if (lov->tgts[ost_idx].active) {
997                                 CERROR("error creating objid "LPX64" sub-object"
998                                        " on OST idx %d/%d: rc = %d\n",
999                                        src_oa->o_id, ost_idx,
1000                                        lsm->lsm_stripe_count, err);
1001                                 if (err > 0) {
1002                                         CERROR("obd_create returned invalid "
1003                                                "err %d\n", err);
1004                                         err = -EIO;
1005                                 }
1006                         }
1007                         if (!rc)
1008                                 rc = err;
1009                         continue;
1010                 }
1011                 if (oti->oti_objid)
1012                         oti->oti_objid[ost_idx] = tmp_oa->o_id;
1013                 loi->loi_id = tmp_oa->o_id;
1014                 loi->loi_gr = tmp_oa->o_gr;
1015                 loi->loi_ost_idx = ost_idx;
1016                 loi->loi_ost_gen = lov->tgts[ost_idx].ltd_gen;
1017                 CDEBUG(D_INODE, "objid "LPX64" has subobj "LPX64" at "
1018                       "idx %d gen %d\n", lsm->lsm_object_id, loi->loi_id,
1019                        ost_idx, loi->loi_ost_gen);
1020
1021                 lov_merge_attrs(ret_oa, tmp_oa, tmp_oa->o_valid, lsm,
1022                                 obj_alloc, &set);
1023                 loi_init(loi);
1024
1025                 if (cookies)
1026                         ++oti->oti_logcookies;
1027                 if (tmp_oa->o_valid & OBD_MD_FLCOOKIE)
1028                         ++cookie_sent;
1029                 ++obj_alloc;
1030                 ++loi;
1031
1032                 /* If we have allocated enough objects, we are OK */
1033                 if (obj_alloc == lsm->lsm_stripe_count)
1034                         GOTO(out_done, rc = 0);
1035         }
1036
1037         if (obj_alloc == 0) {
1038                 if (rc == 0)
1039                         rc = -EIO;
1040                 GOTO(out_cleanup, rc);
1041         }
1042
1043         /* If we were passed specific striping params, then a failure to
1044          * meet those requirements is an error, since we can't reallocate
1045          * that memory (it might be part of a larger array or something).
1046          *
1047          * We can only get here if lsm_stripe_count was originally > 1.
1048          */
1049         if (*ea != NULL) {
1050                 CERROR("can't lstripe objid "LPX64": have %u want %u, rc %d\n",
1051                        lsm->lsm_object_id, obj_alloc, lsm->lsm_stripe_count,rc);
1052                 if (rc == 0)
1053                         rc = -EFBIG;
1054                 GOTO(out_cleanup, rc);
1055         } else {
1056                 struct lov_stripe_md *lsm_new;
1057                 /* XXX LOV STACKING call into osc for sizes */
1058                 unsigned oldsize, newsize;
1059
1060                 if (oti && cookies && cookie_sent) {
1061                         oldsize = lsm->lsm_stripe_count * sizeof(*cookies);
1062                         newsize = obj_alloc * sizeof(*cookies);
1063
1064                         oti_alloc_cookies(oti, obj_alloc);
1065                         if (oti->oti_logcookies) {
1066                                 memcpy(oti->oti_logcookies, cookies, newsize);
1067                                 OBD_FREE(cookies, oldsize);
1068                                 cookies = oti->oti_logcookies;
1069                         } else {
1070                                 CWARN("'leaking' %d bytes\n", oldsize-newsize);
1071                         }
1072                 }
1073
1074                 CWARN("using fewer stripes for object "LPX64": old %u new %u\n",
1075                       lsm->lsm_object_id, lsm->lsm_stripe_count, obj_alloc);
1076                 oldsize = lov_stripe_md_size(lsm->lsm_stripe_count);
1077                 newsize = lov_stripe_md_size(obj_alloc);
1078                 OBD_ALLOC(lsm_new, newsize);
1079                 if (lsm_new != NULL) {
1080                         memcpy(lsm_new, lsm, newsize);
1081                         lsm_new->lsm_stripe_count = obj_alloc;
1082                         OBD_FREE(lsm, oldsize);
1083                         lsm = lsm_new;
1084                 } else {
1085                         CWARN("'leaking' %d bytes\n", oldsize - newsize);
1086                 }
1087                 rc = 0;
1088         }
1089         EXIT;
1090  out_done:
1091         *ea = lsm;
1092         if (src_oa->o_valid & OBD_MD_FLSIZE &&
1093             ret_oa->o_size != src_oa->o_size) {
1094                 CERROR("original size "LPU64" isn't new object size "LPU64"\n",
1095                        src_oa->o_size, ret_oa->o_size);
1096                 LBUG();
1097         }
1098         ret_oa->o_id = src_oa->o_id;
1099         ret_oa->o_gr = src_oa->o_gr;
1100         ret_oa->o_valid |= OBD_MD_FLGROUP;
1101         memcpy(src_oa, ret_oa, sizeof(*src_oa));
1102
1103  out_tmp:
1104         obdo_free(tmp_oa);
1105  out_oa:
1106         obdo_free(ret_oa);
1107         if (oti && cookies) {
1108                 oti->oti_logcookies = cookies;
1109                 if (!cookie_sent) {
1110                         oti_free_cookies(oti);
1111                         src_oa->o_valid &= ~OBD_MD_FLCOOKIE;
1112                 } else {
1113                         src_oa->o_valid |= OBD_MD_FLCOOKIE;
1114                 }
1115         }
1116         RETURN(rc);
1117
1118  out_cleanup:
1119         while (obj_alloc-- > 0) {
1120                 struct obd_export *sub_exp;
1121                 int err;
1122
1123                 --loi;
1124                 sub_exp = lov->tgts[loi->loi_ost_idx].ltd_exp;
1125                 /* destroy already created objects here */
1126                 memcpy(tmp_oa, src_oa, sizeof(*tmp_oa));
1127                 tmp_oa->o_id = loi->loi_id;
1128
1129                 err = obd_destroy(sub_exp, tmp_oa, NULL, oti);
1130                 if (err)
1131                         CERROR("Failed to uncreate objid "LPX64" subobj "LPX64
1132                                " on OST idx %d: rc = %d\n", src_oa->o_id,
1133                                loi->loi_id, loi->loi_ost_idx, err);
1134         }
1135         if (*ea == NULL)
1136                 obd_free_memmd(exp, &lsm);
1137         goto out_tmp;
1138 }
1139
1140 static int lov_revalidate_policy(struct lov_obd *lov, struct lov_stripe_md *lsm)
1141 {
1142         static int next_idx = 0;
1143         struct lov_tgt_desc *tgt;
1144         int i, count;
1145
1146         /* XXX - we should do something clever and take lsm
1147          * into account but just do round robin for now. */
1148
1149         /* last_idx must always be less that count because
1150          * ld_tgt_count currently cannot shrink. */
1151         count = lov->desc.ld_tgt_count;
1152
1153         for (i = next_idx, tgt = lov->tgts + i; i < count; i++, tgt++) {
1154                 if (tgt->active) {
1155                         next_idx = (i + 1) % count;
1156                         RETURN(i);
1157                 }
1158         }
1159
1160         for (i = 0, tgt = lov->tgts; i < next_idx; i++, tgt++) {
1161                 if (tgt->active) {
1162                         next_idx = (i + 1) % count;
1163                         RETURN(i);
1164                 }
1165         }
1166
1167         RETURN(-EIO);
1168 }
1169
1170 #define lsm_bad_magic(LSMP)                                     \
1171 ({                                                              \
1172         struct lov_stripe_md *_lsm__ = (LSMP);                  \
1173         int _ret__ = 0;                                         \
1174         if (!_lsm__) {                                          \
1175                 CERROR("LOV requires striping ea\n");           \
1176                 _ret__ = 1;                                     \
1177         } else if (_lsm__->lsm_magic != LOV_MAGIC) {            \
1178                 CERROR("LOV striping magic bad %#x != %#x\n",   \
1179                        _lsm__->lsm_magic, LOV_MAGIC);           \
1180                 _ret__ = 1;                                     \
1181         }                                                       \
1182         _ret__;                                                 \
1183 })
1184
1185 static int lov_destroy(struct obd_export *exp, struct obdo *oa,
1186                        struct lov_stripe_md *lsm, struct obd_trans_info *oti)
1187 {
1188         struct obdo *tmp = NULL;
1189         struct lov_oinfo *loi;
1190         struct lov_obd *lov;
1191         int rc = 0, i;
1192         ENTRY;
1193
1194         if (lsm_bad_magic(lsm))
1195                 RETURN(-EINVAL);
1196
1197         if (!exp || !exp->exp_obd)
1198                 RETURN(-ENODEV);
1199
1200         lov = &exp->exp_obd->u.lov;
1201         loi = lsm->lsm_oinfo;
1202         for (i = 0; i < lsm->lsm_stripe_count; i++, loi++) {
1203                 int err;
1204
1205                 if (lov->tgts[loi->loi_ost_idx].active == 0) {
1206                         CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
1207                         /* Orphan clean up will (someday) fix this up. */
1208                         if (oti != NULL && oa->o_valid & OBD_MD_FLCOOKIE)
1209                                 oti->oti_logcookies++;
1210                         continue;
1211                 }
1212
1213                 tmp = obdo_alloc();
1214                 if (tmp == NULL)
1215                         RETURN(-ENOMEM);
1216                 memcpy(tmp, oa, sizeof(*tmp));
1217                 tmp->o_id = loi->loi_id;
1218                 err = obd_destroy(lov->tgts[loi->loi_ost_idx].ltd_exp,
1219                                   tmp, NULL, oti);
1220                 obdo_free(tmp);
1221                 if (err && lov->tgts[loi->loi_ost_idx].active) {
1222                         CDEBUG(D_INODE, "error: destroying objid "LPX64" subobj "
1223                                LPX64" on OST idx %d: rc = %d\n",
1224                                oa->o_id, loi->loi_id, loi->loi_ost_idx, err);
1225                         if (!rc)
1226                                 rc = err;
1227                 }
1228         }
1229         RETURN(rc);
1230 }
1231
1232 static int lov_getattr(struct obd_export *exp, struct obdo *oa,
1233                        struct lov_stripe_md *lsm)
1234 {
1235         struct obdo *tmp = NULL;
1236         int i, rc = 0, set = 0;
1237         struct lov_oinfo *loi;
1238         struct lov_obd *lov;
1239         ENTRY;
1240
1241         if (lsm_bad_magic(lsm))
1242                 RETURN(-EINVAL);
1243
1244         if (!exp || !exp->exp_obd)
1245                 RETURN(-ENODEV);
1246
1247         lov = &exp->exp_obd->u.lov;
1248
1249         CDEBUG(D_INFO, "objid "LPX64": %ux%u byte stripes\n",
1250                lsm->lsm_object_id, lsm->lsm_stripe_count, lsm->lsm_stripe_size);
1251         for (i = 0,loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++,loi++) {
1252                 int err;
1253
1254                 if (lov->tgts[loi->loi_ost_idx].active == 0) {
1255                         CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
1256                         continue;
1257                 }
1258
1259                 CDEBUG(D_INFO, "objid "LPX64"[%d] has subobj "LPX64" at idx "
1260                        "%u\n", oa->o_id, i, loi->loi_id, loi->loi_ost_idx);
1261                 /* create data objects with "parent" OA */
1262                 tmp = obdo_alloc();
1263                 if (tmp == NULL)
1264                         RETURN(-ENOMEM);
1265                 memcpy(tmp, oa, sizeof(*tmp));
1266                 tmp->o_id = loi->loi_id;
1267
1268                 err = obd_getattr(lov->tgts[loi->loi_ost_idx].ltd_exp,
1269                                   tmp, NULL);
1270                 if (err) {
1271                         if (lov->tgts[loi->loi_ost_idx].active) {
1272                                 CERROR("error: getattr objid "LPX64" subobj "
1273                                        LPX64" on OST idx %d: rc = %d\n",
1274                                        oa->o_id, loi->loi_id, loi->loi_ost_idx,
1275                                        err);
1276                                 obdo_free(tmp);
1277                                 RETURN(err);
1278                         }
1279                 } else {
1280                         lov_merge_attrs(oa, tmp, tmp->o_valid, lsm, i, &set);
1281                 }
1282                 obdo_free(tmp);
1283         }
1284         if (!set)
1285                 rc = -EIO;
1286         RETURN(rc);
1287 }
1288
1289 static int lov_getattr_interpret(struct ptlrpc_request_set *rqset, void *data,
1290                                  int rc)
1291 {
1292         struct lov_getattr_async_args *aa = data;
1293         struct lov_stripe_md *lsm = aa->aa_lsm;
1294         struct obdo          *oa = aa->aa_oa;
1295         struct obdo          *obdos = aa->aa_obdos;
1296         struct lov_oinfo     *loi;
1297         int                   i;
1298         int                   set = 0;
1299         ENTRY;
1300
1301         if (rc == 0) {
1302                 /* NB all stripe requests succeeded to get here */
1303
1304                 loi = lsm->lsm_oinfo;
1305                 for (i = 0; i < lsm->lsm_stripe_count; i++, loi++) {
1306                         if (obdos[i].o_valid == 0)      /* inactive stripe */
1307                                 continue;
1308
1309                         lov_merge_attrs(oa, &obdos[i], obdos[i].o_valid, lsm,
1310                                         i, &set);
1311                 }
1312
1313                 if (!set) {
1314                         CERROR ("No stripes had valid attrs\n");
1315                         rc = -EIO;
1316                 }
1317         }
1318
1319         OBD_FREE (obdos, lsm->lsm_stripe_count * sizeof (*obdos));
1320         RETURN (rc);
1321 }
1322
1323 static int lov_getattr_async(struct obd_export *exp, struct obdo *oa,
1324                               struct lov_stripe_md *lsm,
1325                               struct ptlrpc_request_set *rqset)
1326 {
1327         struct obdo *obdos;
1328         struct lov_obd *lov;
1329         struct lov_oinfo *loi;
1330         struct lov_getattr_async_args *aa;
1331         int i, rc = 0, set = 0;
1332         ENTRY;
1333
1334         if (lsm_bad_magic(lsm))
1335                 RETURN(-EINVAL);
1336
1337         if (!exp || !exp->exp_obd)
1338                 RETURN(-ENODEV);
1339
1340         lov = &exp->exp_obd->u.lov;
1341
1342         OBD_ALLOC (obdos, lsm->lsm_stripe_count * sizeof (*obdos));
1343         if (obdos == NULL)
1344                 RETURN(-ENOMEM);
1345
1346         CDEBUG(D_INFO, "objid "LPX64": %ux%u byte stripes\n",
1347                lsm->lsm_object_id, lsm->lsm_stripe_count, lsm->lsm_stripe_size);
1348         for (i = 0,loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++,loi++) {
1349                 int err;
1350
1351                 if (lov->tgts[loi->loi_ost_idx].active == 0) {
1352                         CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
1353                         /* leaves obdos[i].obd_valid unset */
1354                         continue;
1355                 }
1356
1357                 CDEBUG(D_INFO, "objid "LPX64"[%d] has subobj "LPX64" at "
1358                        "idx %u gen %d\n", oa->o_id, i, loi->loi_id,
1359                        loi->loi_ost_idx, loi->loi_ost_gen);
1360
1361                 /* create data objects with "parent" OA */
1362                 memcpy(&obdos[i], oa, sizeof(obdos[i]));
1363                 obdos[i].o_id = loi->loi_id;
1364
1365                 err = obd_getattr_async(lov->tgts[loi->loi_ost_idx].ltd_exp,
1366                                          &obdos[i], NULL, rqset);
1367                 if (err) {
1368                         CERROR("error: getattr objid "LPX64" subobj "
1369                                LPX64" on OST idx %d: rc = %d\n",
1370                                oa->o_id, loi->loi_id, loi->loi_ost_idx,
1371                                err);
1372                         GOTO(out_obdos, rc = err);
1373                 }
1374                 set = 1;
1375         }
1376         if (!set)
1377                 GOTO (out_obdos, rc = -EIO);
1378
1379         LASSERT (rqset->set_interpret == NULL);
1380         rqset->set_interpret = lov_getattr_interpret;
1381         LASSERT (sizeof (rqset->set_args) >= sizeof (*aa));
1382         aa = (struct lov_getattr_async_args *)&rqset->set_args;
1383         aa->aa_lsm = lsm;
1384         aa->aa_oa = oa;
1385         aa->aa_obdos = obdos;
1386         aa->aa_lov = lov;
1387         GOTO(out, rc = 0);
1388
1389 out_obdos:
1390         OBD_FREE (obdos, lsm->lsm_stripe_count * sizeof (*obdos));
1391 out:
1392         RETURN(rc);
1393 }
1394
1395
1396 static int lov_setattr(struct obd_export *exp, struct obdo *src_oa,
1397                        struct lov_stripe_md *lsm, struct obd_trans_info *oti)
1398 {
1399         struct obdo *tmp_oa, *ret_oa;
1400         struct lov_obd *lov;
1401         struct lov_oinfo *loi;
1402         int rc = 0, i, set = 0;
1403         ENTRY;
1404
1405         if (lsm_bad_magic(lsm))
1406                 RETURN(-EINVAL);
1407
1408         if (!exp || !exp->exp_obd)
1409                 RETURN(-ENODEV);
1410
1411         /* for now, we only expect time updates here */
1412         LASSERT(!(src_oa->o_valid & ~(OBD_MD_FLID|OBD_MD_FLTYPE | OBD_MD_FLMODE|
1413                                       OBD_MD_FLATIME | OBD_MD_FLMTIME |
1414                                       OBD_MD_FLCTIME | OBD_MD_FLFLAGS |
1415                                       OBD_MD_FLSIZE | OBD_MD_FLGROUP)));
1416
1417         LASSERT(!(src_oa->o_valid & OBD_MD_FLGROUP) || src_oa->o_gr > 0);
1418
1419         ret_oa = obdo_alloc();
1420         if (!ret_oa)
1421                 RETURN(-ENOMEM);
1422
1423         tmp_oa = obdo_alloc();
1424         if (!tmp_oa)
1425                 GOTO(out_oa, rc = -ENOMEM);
1426
1427         lov = &exp->exp_obd->u.lov;
1428         for (i = 0,loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++,loi++) {
1429                 int err;
1430
1431                 if (lov->tgts[loi->loi_ost_idx].active == 0) {
1432                         CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
1433                         continue;
1434                 }
1435
1436                 memcpy(tmp_oa, src_oa, sizeof(*tmp_oa));
1437                 tmp_oa->o_id = loi->loi_id;
1438                 LASSERT(!(tmp_oa->o_valid & OBD_MD_FLGROUP) || tmp_oa->o_gr>0);
1439
1440                 if (src_oa->o_valid & OBD_MD_FLSIZE) {
1441                         if (lov_stripe_offset(lsm, src_oa->o_size, i,
1442                                               &tmp_oa->o_size) < 0 &&
1443                             tmp_oa->o_size)
1444                                 tmp_oa->o_size--;
1445
1446                         CDEBUG(D_INODE, "stripe %d has size "LPU64"/"LPU64"\n",
1447                                i, tmp_oa->o_size, src_oa->o_size);
1448                 }
1449
1450                 err = obd_setattr(lov->tgts[loi->loi_ost_idx].ltd_exp, tmp_oa,
1451                                   NULL, NULL);
1452                 if (err) {
1453                         if (lov->tgts[loi->loi_ost_idx].active) {
1454                                 CERROR("error: setattr objid "LPX64" subobj "
1455                                        LPX64" on OST idx %d: rc = %d\n",
1456                                        src_oa->o_id, loi->loi_id,
1457                                        loi->loi_ost_idx, err);
1458                                 if (!rc)
1459                                         rc = err;
1460                         }
1461                         continue;
1462                 }
1463                 lov_merge_attrs(ret_oa, tmp_oa, tmp_oa->o_valid, lsm, i, &set);
1464         }
1465         if (!set && !rc)
1466                 rc = -EIO;
1467
1468         ret_oa->o_id = src_oa->o_id;
1469         memcpy(src_oa, ret_oa, sizeof(*src_oa));
1470         GOTO(out_tmp, rc);
1471 out_tmp:
1472         obdo_free(tmp_oa);
1473 out_oa:
1474         obdo_free(ret_oa);
1475         return rc;
1476 }
1477
1478 /* we have an offset in file backed by an lov and want to find out where
1479  * that offset lands in our given stripe of the file.  for the easy
1480  * case where the offset is within the stripe, we just have to scale the
1481  * offset down to make it relative to the stripe instead of the lov.
1482  *
1483  * the harder case is what to do when the offset doesn't intersect the
1484  * stripe.  callers will want start offsets clamped ahead to the start
1485  * of the nearest stripe in the file.  end offsets similarly clamped to the
1486  * nearest ending byte of a stripe in the file:
1487  *
1488  * all this function does is move offsets to the nearest region of the
1489  * stripe, and it does its work "mod" the full length of all the stripes.
1490  * consider a file with 3 stripes:
1491  *
1492  *             S                                              E
1493  * ---------------------------------------------------------------------
1494  * |    0    |     1     |     2     |    0    |     1     |     2     |
1495  * ---------------------------------------------------------------------
1496  *
1497  * to find stripe 1's offsets for S and E, it divides by the full stripe
1498  * width and does its math in the context of a single set of stripes:
1499  *
1500  *             S         E
1501  * -----------------------------------
1502  * |    0    |     1     |     2     |
1503  * -----------------------------------
1504  *
1505  * it'll notice that E is outside stripe 1 and clamp it to the end of the
1506  * stripe, then multiply it back out by lov_off to give the real offsets in
1507  * the stripe:
1508  *
1509  *   S                   E
1510  * ---------------------------------------------------------------------
1511  * |    1    |     1     |     1     |    1    |     1     |     1     |
1512  * ---------------------------------------------------------------------
1513  *
1514  * it would have done similarly and pulled S forward to the start of a 1
1515  * stripe if, say, S had landed in a 0 stripe.
1516  *
1517  * this rounding isn't always correct.  consider an E lov offset that lands
1518  * on a 0 stripe, the "mod stripe width" math will pull it forward to the
1519  * start of a 1 stripe, when in fact it wanted to be rounded back to the end
1520  * of a previous 1 stripe.  this logic is handled by callers and this is why:
1521  *
1522  * this function returns < 0 when the offset was "before" the stripe and
1523  * was moved forward to the start of the stripe in question;  0 when it
1524  * falls in the stripe and no shifting was done; > 0 when the offset
1525  * was outside the stripe and was pulled back to its final byte. */
1526 static int lov_stripe_offset(struct lov_stripe_md *lsm, obd_off lov_off,
1527                              int stripeno, obd_off *obd_off)
1528 {
1529         unsigned long ssize  = lsm->lsm_stripe_size;
1530         unsigned long swidth = ssize * lsm->lsm_stripe_count;
1531         unsigned long stripe_off, this_stripe;
1532         int ret = 0;
1533
1534         if (lov_off == OBD_OBJECT_EOF) {
1535                 *obd_off = OBD_OBJECT_EOF;
1536                 return 0;
1537         }
1538
1539         /* do_div(a, b) returns a % b, and a = a / b */
1540         stripe_off = do_div(lov_off, swidth);
1541
1542         this_stripe = stripeno * ssize;
1543         if (stripe_off < this_stripe) {
1544                 stripe_off = 0;
1545                 ret = -1;
1546         } else {
1547                 stripe_off -= this_stripe;
1548
1549                 if (stripe_off >= ssize) {
1550                         stripe_off = ssize;
1551                         ret = 1;
1552                 }
1553         }
1554
1555         *obd_off = lov_off * ssize + stripe_off;
1556         return ret;
1557 }
1558
1559 /* Given a whole-file size and a stripe number, give the file size which
1560  * corresponds to the individual object of that stripe.
1561  *
1562  * This behaves basically in the same was as lov_stripe_offset, except that
1563  * file sizes falling before the beginning of a stripe are clamped to the end
1564  * of the previous stripe, not the beginning of the next:
1565  *
1566  *                                               S
1567  * ---------------------------------------------------------------------
1568  * |    0    |     1     |     2     |    0    |     1     |     2     |
1569  * ---------------------------------------------------------------------
1570  *
1571  * if clamped to stripe 2 becomes:
1572  *
1573  *                                   S
1574  * ---------------------------------------------------------------------
1575  * |    0    |     1     |     2     |    0    |     1     |     2     |
1576  * ---------------------------------------------------------------------
1577  */
1578 static obd_off lov_size_to_stripe(struct lov_stripe_md *lsm, obd_off file_size,
1579                                   int stripeno)
1580 {
1581         unsigned long ssize  = lsm->lsm_stripe_size;
1582         unsigned long swidth = ssize * lsm->lsm_stripe_count;
1583         unsigned long stripe_off, this_stripe;
1584
1585         if (file_size == OBD_OBJECT_EOF)
1586                 return OBD_OBJECT_EOF;
1587
1588         /* do_div(a, b) returns a % b, and a = a / b */
1589         stripe_off = do_div(file_size, swidth);
1590
1591         this_stripe = stripeno * ssize;
1592         if (stripe_off < this_stripe) {
1593                 /* Move to end of previous stripe, or zero */
1594                 if (file_size > 0) {
1595                         file_size--;
1596                         stripe_off = ssize;
1597                 } else {
1598                         stripe_off = 0;
1599                 }
1600         } else {
1601                 stripe_off -= this_stripe;
1602
1603                 if (stripe_off >= ssize) {
1604                         /* Clamp to end of this stripe */
1605                         stripe_off = ssize;
1606                 }
1607         }
1608
1609         return (file_size * ssize + stripe_off);
1610 }
1611
1612 /* given an extent in an lov and a stripe, calculate the extent of the stripe
1613  * that is contained within the lov extent.  this returns true if the given
1614  * stripe does intersect with the lov extent. */
1615 static int lov_stripe_intersects(struct lov_stripe_md *lsm, int stripeno,
1616                                  obd_off start, obd_off end,
1617                                  obd_off *obd_start, obd_off *obd_end)
1618 {
1619         int start_side = 0, end_side = 0;
1620
1621         switch (lsm->lsm_pattern) {
1622         case LOV_PATTERN_RAID0:
1623                 start_side = lov_stripe_offset(lsm, start, stripeno, obd_start);
1624                 end_side = lov_stripe_offset(lsm, end, stripeno, obd_end);
1625                 break;
1626         case LOV_PATTERN_CMOBD:
1627                 *obd_start = start;
1628                 *obd_end = end;
1629                 start_side = end_side = 0;
1630                 break;
1631         default:
1632                 LBUG();
1633         }
1634
1635         CDEBUG(D_INODE, "["LPU64"->"LPU64"] -> [(%d) "LPU64"->"LPU64" (%d)]\n",
1636                start, end, start_side, *obd_start, *obd_end, end_side);
1637
1638         /* this stripe doesn't intersect the file extent when neither
1639          * start or the end intersected the stripe and obd_start and
1640          * obd_end got rounded up to the save value. */
1641         if (start_side != 0 && end_side != 0 && *obd_start == *obd_end)
1642                 return 0;
1643
1644         /* as mentioned in the lov_stripe_offset commentary, end
1645          * might have been shifted in the wrong direction.  This
1646          * happens when an end offset is before the stripe when viewed
1647          * through the "mod stripe size" math. we detect it being shifted
1648          * in the wrong direction and touch it up.
1649          * interestingly, this can't underflow since end must be > start
1650          * if we passed through the previous check.
1651          * (should we assert for that somewhere?) */
1652         if (end_side != 0)
1653                 (*obd_end)--;
1654
1655         return 1;
1656 }
1657
1658 /* compute which stripe number "lov_off" will be written into */
1659 static int lov_stripe_number(struct lov_stripe_md *lsm, obd_off lov_off)
1660 {
1661         unsigned long ssize  = lsm->lsm_stripe_size;
1662         unsigned long swidth = ssize * lsm->lsm_stripe_count;
1663         unsigned long stripe_off;
1664
1665         if (lsm->lsm_pattern == LOV_PATTERN_CMOBD)
1666                 return 0;
1667
1668         stripe_off = do_div(lov_off, swidth);
1669
1670         return stripe_off / ssize;
1671 }
1672
1673 static int lov_revalidate_md(struct obd_export *exp, struct obdo *src_oa,
1674                              struct lov_stripe_md *ea,
1675                              struct obd_trans_info *oti)
1676 {
1677         struct obd_export *osc_exp;
1678         struct lov_obd *lov = &exp->exp_obd->u.lov;
1679         struct lov_stripe_md *lsm = ea;
1680         struct lov_stripe_md obj_md;
1681         struct lov_stripe_md *obj_mdp = &obj_md;
1682         struct lov_oinfo *loi;
1683         struct obdo *tmp_oa;
1684         int ost_idx, updates = 0, i;
1685         ENTRY;
1686
1687         tmp_oa = obdo_alloc();
1688         if (tmp_oa == NULL)
1689                 RETURN(-ENOMEM);
1690
1691         loi = lsm->lsm_oinfo;
1692         for (i = 0; i < lsm->lsm_stripe_count; i++, loi++) {
1693                 int rc;
1694                 if (!obd_uuid_empty(&lov->tgts[loi->loi_ost_idx].uuid))
1695                         continue;
1696
1697                 ost_idx = lov_revalidate_policy(lov, lsm);
1698                 if (ost_idx < 0) {
1699                         /* FIXME: punt for now. */
1700                         CERROR("lov_revalidate_policy failed; no active "
1701                                "OSCs?\n");
1702                         continue;
1703                 }
1704
1705                 /* create a new object */
1706                 memcpy(tmp_oa, src_oa, sizeof(*tmp_oa));
1707                 /* XXX: LOV STACKING: use real "obj_mdp" sub-data */
1708                 osc_exp = lov->tgts[ost_idx].ltd_exp;
1709                 rc = obd_create(osc_exp, tmp_oa, &obj_mdp, oti);
1710                 if (rc) {
1711                         CERROR("error creating new subobj at idx %d; "
1712                                "rc = %d\n", ost_idx, rc);
1713                         continue;
1714                 }
1715                 if (oti->oti_objid)
1716                         oti->oti_objid[ost_idx] = tmp_oa->o_id;
1717                 loi->loi_id = tmp_oa->o_id;
1718                 loi->loi_ost_idx = ost_idx;
1719                 loi->loi_ost_gen = lov->tgts[ost_idx].ltd_gen;
1720                 CDEBUG(D_INODE, "replacing objid "LPX64" subobj "LPX64
1721                        " with idx %d gen %d.\n", lsm->lsm_object_id,
1722                        loi->loi_id, ost_idx, loi->loi_ost_gen);
1723                 updates = 1;
1724         }
1725
1726         /* If we got an error revalidating an entry there's no need to
1727          * cleanup up objects we allocated here because the bad entry
1728          * still points to a deleted OST. */
1729
1730         obdo_free(tmp_oa);
1731         RETURN(updates);
1732 }
1733
1734 /* FIXME: maybe we'll just make one node the authoritative attribute node, then
1735  * we can send this 'punch' to just the authoritative node and the nodes
1736  * that the punch will affect. */
1737 static int lov_punch(struct obd_export *exp, struct obdo *oa,
1738                      struct lov_stripe_md *lsm,
1739                      obd_off start, obd_off end, struct obd_trans_info *oti)
1740 {
1741         struct obdo *tmp = NULL;
1742         struct lov_oinfo *loi;
1743         struct lov_obd *lov;
1744         int rc = 0, i;
1745         ENTRY;
1746
1747         if (lsm_bad_magic(lsm))
1748                 RETURN(-EINVAL);
1749
1750         if (!exp || !exp->exp_obd)
1751                 RETURN(-ENODEV);
1752
1753         lov = &exp->exp_obd->u.lov;
1754         for (i = 0,loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++,loi++) {
1755                 obd_off starti, endi;
1756                 int err;
1757
1758                 if (lov->tgts[loi->loi_ost_idx].active == 0) {
1759                         CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
1760                         continue;
1761                 }
1762
1763                 if (!lov_stripe_intersects(lsm, i, start, end, &starti, &endi))
1764                         continue;
1765
1766                 /* create data objects with "parent" OA */
1767                 tmp = obdo_alloc();
1768                 if (tmp == NULL)
1769                         RETURN(-ENOMEM);
1770                 memcpy(tmp, oa, sizeof(*tmp));
1771                 tmp->o_id = loi->loi_id;
1772
1773                 err = obd_punch(lov->tgts[loi->loi_ost_idx].ltd_exp,
1774                                 tmp, NULL, starti, endi, NULL);
1775                 obdo_free(tmp);
1776                 if (err) {
1777                         if (lov->tgts[loi->loi_ost_idx].active) {
1778                                 CERROR("error: punch objid "LPX64" subobj "LPX64
1779                                        " on OST idx %d: rc = %d\n", oa->o_id,
1780                                        loi->loi_id, loi->loi_ost_idx, err);
1781                         }
1782                         if (!rc)
1783                                 rc = err;
1784                 } else {
1785                         loi->loi_kms = loi->loi_rss = starti;
1786                 }
1787         }
1788         RETURN(rc);
1789 }
1790
1791 static int lov_sync(struct obd_export *exp, struct obdo *oa,
1792                     struct lov_stripe_md *lsm, obd_off start, obd_off end)
1793 {
1794         struct obdo *tmp;
1795         struct lov_obd *lov;
1796         struct lov_oinfo *loi;
1797         int rc = 0, i;
1798         ENTRY;
1799
1800         if (lsm_bad_magic(lsm))
1801                 RETURN(-EINVAL);
1802
1803         if (!exp->exp_obd)
1804                 RETURN(-ENODEV);
1805
1806         tmp = obdo_alloc();
1807         if (!tmp)
1808                 RETURN(-ENOMEM);
1809
1810         lov = &exp->exp_obd->u.lov;
1811         for (i = 0,loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++,loi++) {
1812                 obd_off starti, endi;
1813                 int err;
1814
1815                 if (lov->tgts[loi->loi_ost_idx].active == 0) {
1816                         CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
1817                         continue;
1818                 }
1819
1820                 if (!lov_stripe_intersects(lsm, i, start, end, &starti, &endi))
1821                         continue;
1822
1823                 memcpy(tmp, oa, sizeof(*tmp));
1824                 tmp->o_id = loi->loi_id;
1825
1826                 err = obd_sync(lov->tgts[loi->loi_ost_idx].ltd_exp, tmp, NULL,
1827                                starti, endi);
1828                 if (err) {
1829                         if (lov->tgts[loi->loi_ost_idx].active) {
1830                                 CERROR("error: fsync objid "LPX64" subobj "LPX64
1831                                        " on OST idx %d: rc = %d\n", oa->o_id,
1832                                        loi->loi_id, loi->loi_ost_idx, err);
1833                         }
1834                         if (!rc)
1835                                 rc = err;
1836                 }
1837         }
1838
1839         obdo_free(tmp);
1840         RETURN(rc);
1841 }
1842
1843 static int lov_brw_check(struct lov_obd *lov, struct obdo *oa,
1844                          struct lov_stripe_md *lsm,
1845                          obd_count oa_bufs, struct brw_page *pga)
1846 {
1847         int i, rc = 0;
1848         ENTRY;
1849
1850         /* The caller just wants to know if there's a chance that this
1851          * I/O can succeed */
1852         for (i = 0; i < oa_bufs; i++) {
1853                 int stripe = lov_stripe_number(lsm, pga[i].disk_offset);
1854                 int ost = lsm->lsm_oinfo[stripe].loi_ost_idx;
1855                 obd_off start, end;
1856
1857                 if (!lov_stripe_intersects(lsm, i, pga[i].disk_offset,
1858                                            pga[i].disk_offset + pga[i].count,
1859                                            &start, &end))
1860                         continue;
1861
1862                 if (lov->tgts[ost].active == 0) {
1863                         CDEBUG(D_HA, "lov idx %d inactive\n", ost);
1864                         RETURN(-EIO);
1865                 }
1866                 rc = obd_brw(OBD_BRW_CHECK, lov->tgts[ost].ltd_exp, oa,
1867                              NULL, 1, &pga[i], NULL);
1868                 if (rc)
1869                         break;
1870         }
1871         RETURN(rc);
1872 }
1873
1874 static int lov_brw(int cmd, struct obd_export *exp, struct obdo *src_oa,
1875                    struct lov_stripe_md *lsm, obd_count oa_bufs,
1876                    struct brw_page *pga, struct obd_trans_info *oti)
1877 {
1878         struct {
1879                 int bufct;
1880                 int index;
1881                 int subcount;
1882                 struct lov_stripe_md lsm;
1883                 int ost_idx;
1884         } *stripeinfo, *si, *si_last;
1885         struct obdo *ret_oa = NULL, *tmp_oa = NULL;
1886         struct lov_obd *lov;
1887         struct brw_page *ioarr;
1888         struct lov_oinfo *loi;
1889         int rc = 0, i, *where, stripe_count = lsm->lsm_stripe_count, set = 0;
1890         ENTRY;
1891
1892         if (lsm_bad_magic(lsm))
1893                 RETURN(-EINVAL);
1894
1895         lov = &exp->exp_obd->u.lov;
1896
1897         if (cmd == OBD_BRW_CHECK) {
1898                 rc = lov_brw_check(lov, src_oa, lsm, oa_bufs, pga);
1899                 RETURN(rc);
1900         }
1901
1902         OBD_ALLOC(stripeinfo, stripe_count * sizeof(*stripeinfo));
1903         if (!stripeinfo)
1904                 RETURN(-ENOMEM);
1905
1906         OBD_ALLOC(where, sizeof(*where) * oa_bufs);
1907         if (!where)
1908                 GOTO(out_sinfo, rc = -ENOMEM);
1909
1910         OBD_ALLOC(ioarr, sizeof(*ioarr) * oa_bufs);
1911         if (!ioarr)
1912                 GOTO(out_where, rc = -ENOMEM);
1913
1914         if (src_oa) {
1915                 ret_oa = obdo_alloc();
1916                 if (!ret_oa)
1917                         GOTO(out_ioarr, rc = -ENOMEM);
1918
1919                 tmp_oa = obdo_alloc();
1920                 if (!tmp_oa)
1921                         GOTO(out_oa, rc = -ENOMEM);
1922         }
1923
1924         for (i = 0; i < oa_bufs; i++) {
1925                 where[i] = lov_stripe_number(lsm, pga[i].disk_offset);
1926                 stripeinfo[where[i]].bufct++;
1927         }
1928
1929         for (i = 0, loi = lsm->lsm_oinfo, si_last = si = stripeinfo;
1930              i < stripe_count; i++, loi++, si_last = si, si++) {
1931                 if (i > 0)
1932                         si->index = si_last->index + si_last->bufct;
1933                 si->lsm.lsm_object_id = loi->loi_id;
1934                 si->lsm.lsm_object_gr = lsm->lsm_object_gr;
1935                 si->ost_idx = loi->loi_ost_idx;
1936         }
1937
1938         for (i = 0; i < oa_bufs; i++) {
1939                 int which = where[i];
1940                 int shift;
1941
1942                 shift = stripeinfo[which].index + stripeinfo[which].subcount;
1943                 LASSERT(shift < oa_bufs);
1944                 ioarr[shift] = pga[i];
1945                 lov_stripe_offset(lsm, pga[i].disk_offset, which,
1946                                   &ioarr[shift].disk_offset);
1947                 stripeinfo[which].subcount++;
1948         }
1949
1950         for (i = 0, si = stripeinfo; i < stripe_count; i++, si++) {
1951                 int shift = si->index;
1952
1953                 if (lov->tgts[si->ost_idx].active == 0) {
1954                         CDEBUG(D_HA, "lov idx %d inactive\n", si->ost_idx);
1955                         GOTO(out_oa, rc = -EIO);
1956                 }
1957
1958                 if (si->bufct) {
1959                         LASSERT(shift < oa_bufs);
1960                         if (src_oa)
1961                                 memcpy(tmp_oa, src_oa, sizeof(*tmp_oa));
1962
1963                         tmp_oa->o_id = si->lsm.lsm_object_id;
1964                         rc = obd_brw(cmd, lov->tgts[si->ost_idx].ltd_exp,
1965                                      tmp_oa, &si->lsm, si->bufct,
1966                                      &ioarr[shift], oti);
1967                         if (rc)
1968                                 GOTO(out_oa, rc);
1969
1970                         lov_merge_attrs(ret_oa, tmp_oa, tmp_oa->o_valid, lsm,
1971                                         i, &set);
1972                 }
1973         }
1974
1975         ret_oa->o_id = src_oa->o_id;
1976         memcpy(src_oa, ret_oa, sizeof(*src_oa));
1977
1978         GOTO(out_oa, rc);
1979  out_oa:
1980         if (tmp_oa)
1981                 obdo_free(tmp_oa);
1982         if (ret_oa)
1983                 obdo_free(ret_oa);
1984  out_ioarr:
1985         OBD_FREE(ioarr, sizeof(*ioarr) * oa_bufs);
1986  out_where:
1987         OBD_FREE(where, sizeof(*where) * oa_bufs);
1988  out_sinfo:
1989         OBD_FREE(stripeinfo, stripe_count * sizeof(*stripeinfo));
1990         return rc;
1991 }
1992
1993 static int lov_brw_interpret(struct ptlrpc_request_set *reqset, void *data,
1994                              int rc)
1995 {
1996         struct lov_brw_async_args *aa = data;
1997         struct lov_stripe_md *lsm = aa->aa_lsm;
1998         obd_count             oa_bufs = aa->aa_oa_bufs;
1999         struct obdo          *oa = aa->aa_oa;
2000         struct obdo          *obdos = aa->aa_obdos;
2001         struct brw_page      *ioarr = aa->aa_ioarr;
2002         struct lov_oinfo     *loi;
2003         int i, set = 0;
2004         ENTRY;
2005
2006         if (rc == 0) {
2007                 /* NB all stripe requests succeeded to get here */
2008
2009                 for (i = 0, loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count;
2010                      i++, loi++) {
2011                         if (obdos[i].o_valid == 0)      /* inactive stripe */
2012                                 continue;
2013
2014                         lov_merge_attrs(oa, &obdos[i], obdos[i].o_valid, lsm,
2015                                         i, &set);
2016                 }
2017
2018                 if (!set) {
2019                         CERROR("No stripes had valid attrs\n");
2020                         rc = -EIO;
2021                 }
2022         }
2023         oa->o_id = lsm->lsm_object_id;
2024
2025         OBD_FREE(obdos, lsm->lsm_stripe_count * sizeof(*obdos));
2026         OBD_FREE(ioarr, sizeof(*ioarr) * oa_bufs);
2027         RETURN(rc);
2028 }
2029
2030 static int lov_brw_async(int cmd, struct obd_export *exp, struct obdo *oa,
2031                          struct lov_stripe_md *lsm, obd_count oa_bufs,
2032                          struct brw_page *pga, struct ptlrpc_request_set *set,
2033                          struct obd_trans_info *oti)
2034 {
2035         struct {
2036                 int bufct;
2037                 int index;
2038                 int subcount;
2039                 struct lov_stripe_md lsm;
2040                 int ost_idx;
2041         } *stripeinfo, *si, *si_last;
2042         struct lov_obd *lov;
2043         struct brw_page *ioarr;
2044         struct obdo *obdos = NULL;
2045         struct lov_oinfo *loi;
2046         struct lov_brw_async_args *aa;
2047         int rc = 0, i, *where, stripe_count = lsm->lsm_stripe_count;
2048         ENTRY;
2049
2050         if (lsm_bad_magic(lsm))
2051                 RETURN(-EINVAL);
2052
2053         lov = &exp->exp_obd->u.lov;
2054
2055         if (cmd == OBD_BRW_CHECK) {
2056                 rc = lov_brw_check(lov, oa, lsm, oa_bufs, pga);
2057                 RETURN(rc);
2058         }
2059
2060         OBD_ALLOC(stripeinfo, stripe_count * sizeof(*stripeinfo));
2061         if (!stripeinfo)
2062                 RETURN(-ENOMEM);
2063
2064         OBD_ALLOC(where, sizeof(*where) * oa_bufs);
2065         if (!where)
2066                 GOTO(out_sinfo, rc = -ENOMEM);
2067
2068         if (oa) {
2069                 OBD_ALLOC(obdos, sizeof(*obdos) * stripe_count);
2070                 if (!obdos)
2071                         GOTO(out_where, rc = -ENOMEM);
2072         }
2073
2074         OBD_ALLOC(ioarr, sizeof(*ioarr) * oa_bufs);
2075         if (!ioarr)
2076                 GOTO(out_obdos, rc = -ENOMEM);
2077
2078         for (i = 0; i < oa_bufs; i++) {
2079                 where[i] = lov_stripe_number(lsm, pga[i].disk_offset);
2080                 stripeinfo[where[i]].bufct++;
2081         }
2082
2083         for (i = 0, loi = lsm->lsm_oinfo, si_last = si = stripeinfo;
2084              i < stripe_count; i++, loi++, si_last = si, si++) {
2085                 if (i > 0)
2086                         si->index = si_last->index + si_last->bufct;
2087                 si->lsm.lsm_object_id = loi->loi_id;
2088                 si->ost_idx = loi->loi_ost_idx;
2089
2090                 if (oa) {
2091                         memcpy(&obdos[i], oa, sizeof(*obdos));
2092                         obdos[i].o_id = si->lsm.lsm_object_id;
2093                 }
2094         }
2095
2096         for (i = 0; i < oa_bufs; i++) {
2097                 int which = where[i];
2098                 int shift;
2099
2100                 shift = stripeinfo[which].index + stripeinfo[which].subcount;
2101                 LASSERT(shift < oa_bufs);
2102                 ioarr[shift] = pga[i];
2103                 lov_stripe_offset(lsm, pga[i].disk_offset, which,
2104                                   &ioarr[shift].disk_offset);
2105                 stripeinfo[which].subcount++;
2106         }
2107
2108         for (i = 0, si = stripeinfo; i < stripe_count; i++, si++) {
2109                 int shift = si->index;
2110
2111                 if (si->bufct == 0)
2112                         continue;
2113
2114                 if (lov->tgts[si->ost_idx].active == 0) {
2115                         CDEBUG(D_HA, "lov idx %d inactive\n", si->ost_idx);
2116                         GOTO(out_ioarr, rc = -EIO);
2117                 }
2118
2119                 LASSERT(shift < oa_bufs);
2120
2121                 rc = obd_brw_async(cmd, lov->tgts[si->ost_idx].ltd_exp,
2122                                    &obdos[i], &si->lsm, si->bufct,
2123                                    &ioarr[shift], set, oti);
2124                 if (rc)
2125                         GOTO(out_ioarr, rc);
2126         }
2127         LASSERT(rc == 0);
2128         LASSERT(set->set_interpret == NULL);
2129         set->set_interpret = (set_interpreter_func)lov_brw_interpret;
2130         LASSERT(sizeof(set->set_args) >= sizeof(struct lov_brw_async_args));
2131         aa = (struct lov_brw_async_args *)&set->set_args;
2132         aa->aa_lsm = lsm;
2133         aa->aa_obdos = obdos;
2134         aa->aa_oa = oa;
2135         aa->aa_ioarr = ioarr;
2136         aa->aa_oa_bufs = oa_bufs;
2137
2138         /* Don't free ioarr or obdos - that's done in lov_brw_interpret */
2139         GOTO(out_where, rc);
2140
2141  out_ioarr:
2142         OBD_FREE(ioarr, sizeof(*ioarr) * oa_bufs);
2143  out_obdos:
2144         OBD_FREE(obdos, stripe_count * sizeof(*obdos));
2145  out_where:
2146         OBD_FREE(where, sizeof(*where) * oa_bufs);
2147  out_sinfo:
2148         OBD_FREE(stripeinfo, stripe_count * sizeof(*stripeinfo));
2149         return rc;
2150 }
2151
2152 struct lov_async_page *lap_from_cookie(void *cookie)
2153 {
2154         struct lov_async_page *lap = cookie;
2155         if (lap->lap_magic != LAP_MAGIC)
2156                 return ERR_PTR(-EINVAL);
2157         return lap;
2158 };
2159
2160 static int lov_ap_make_ready(void *data, int cmd)
2161 {
2162         struct lov_async_page *lap = lap_from_cookie(data);
2163         /* XXX should these assert? */
2164         if (IS_ERR(lap))
2165                 return -EINVAL;
2166
2167         return lap->lap_caller_ops->ap_make_ready(lap->lap_caller_data, cmd);
2168 }
2169 static int lov_ap_refresh_count(void *data, int cmd)
2170 {
2171         struct lov_async_page *lap = lap_from_cookie(data);
2172         if (IS_ERR(lap))
2173                 return -EINVAL;
2174
2175         return lap->lap_caller_ops->ap_refresh_count(lap->lap_caller_data,
2176                                                      cmd);
2177 }
2178 static void lov_ap_fill_obdo(void *data, int cmd, struct obdo *oa)
2179 {
2180         struct lov_async_page *lap = lap_from_cookie(data);
2181         /* XXX should these assert? */
2182         if (IS_ERR(lap))
2183                 return;
2184
2185         lap->lap_caller_ops->ap_fill_obdo(lap->lap_caller_data, cmd, oa);
2186         /* XXX woah, shouldn't we be altering more here?  size? */
2187         oa->o_id = lap->lap_loi_id;
2188 }
2189
2190 static void lov_ap_completion(void *data, int cmd, struct obdo *oa, int rc)
2191 {
2192         struct lov_async_page *lap = lap_from_cookie(data);
2193         if (IS_ERR(lap))
2194                 return;
2195
2196         /* in a raid1 regime this would down a count of many ios
2197          * in flight, onl calling the caller_ops completion when all
2198          * the raid1 ios are complete */
2199         lap->lap_caller_ops->ap_completion(lap->lap_caller_data, cmd, oa, rc);
2200 }
2201
2202 static struct obd_async_page_ops lov_async_page_ops = {
2203         .ap_make_ready =        lov_ap_make_ready,
2204         .ap_refresh_count =     lov_ap_refresh_count,
2205         .ap_fill_obdo =         lov_ap_fill_obdo,
2206         .ap_completion =        lov_ap_completion,
2207 };
2208
2209 static int lov_prep_async_page(struct obd_export *exp,
2210                                struct lov_stripe_md *lsm,
2211                                struct lov_oinfo *loi, struct page *page,
2212                                obd_off offset, struct obd_async_page_ops *ops,
2213                                void *data, void **res)
2214 {
2215         struct lov_obd *lov = &exp->exp_obd->u.lov;
2216         struct lov_async_page *lap;
2217         int rc, stripe;
2218         ENTRY;
2219
2220         if (lsm_bad_magic(lsm))
2221                 RETURN(-EINVAL);
2222         LASSERT(loi == NULL);
2223
2224         stripe = lov_stripe_number(lsm, offset);
2225         loi = &lsm->lsm_oinfo[stripe];
2226
2227         if (obd_uuid_empty(&lov->tgts[loi->loi_ost_idx].uuid))
2228                 RETURN(-EIO);
2229         if (lov->tgts[loi->loi_ost_idx].active == 0)
2230                 RETURN(-EIO);
2231         if (lov->tgts[loi->loi_ost_idx].ltd_exp == NULL) {
2232                 CERROR("ltd_exp == NULL, but OST idx %d doesn't appear to be "
2233                        "deleted or inactive.\n", loi->loi_ost_idx);
2234                 RETURN(-EIO);
2235         }
2236
2237         OBD_ALLOC(lap, sizeof(*lap));
2238         if (lap == NULL)
2239                 RETURN(-ENOMEM);
2240
2241         lap->lap_magic = LAP_MAGIC;
2242         lap->lap_caller_ops = ops;
2243         lap->lap_caller_data = data;
2244
2245         /* FIXME handle multiple oscs after landing b_raid1 */
2246         lap->lap_stripe = stripe;
2247         switch (lsm->lsm_pattern) {
2248                 case LOV_PATTERN_RAID0:
2249                         lov_stripe_offset(lsm, offset, lap->lap_stripe, 
2250                                           &lap->lap_sub_offset);
2251                         break;
2252                 case LOV_PATTERN_CMOBD:
2253                         lap->lap_sub_offset = offset;
2254                         break;
2255                 default:
2256                         LBUG();
2257         }
2258
2259         /* so the callback doesn't need the lsm */
2260         lap->lap_loi_id = loi->loi_id;
2261
2262         rc = obd_prep_async_page(lov->tgts[loi->loi_ost_idx].ltd_exp,
2263                                  lsm, loi, page, lap->lap_sub_offset,
2264                                  &lov_async_page_ops, lap,
2265                                  &lap->lap_sub_cookie);
2266         if (rc) {
2267                 OBD_FREE(lap, sizeof(*lap));
2268                 RETURN(rc);
2269         }
2270         CDEBUG(D_CACHE, "lap %p page %p cookie %p off "LPU64"\n", lap, page,
2271                lap->lap_sub_cookie, offset);
2272         *res = lap;
2273         RETURN(0);
2274 }
2275
2276 static int lov_queue_async_io(struct obd_export *exp,
2277                               struct lov_stripe_md *lsm,
2278                               struct lov_oinfo *loi, void *cookie,
2279                               int cmd, obd_off off, int count,
2280                               obd_flags brw_flags, obd_flags async_flags)
2281 {
2282         struct lov_obd *lov = &exp->exp_obd->u.lov;
2283         struct lov_async_page *lap;
2284         int rc;
2285
2286         LASSERT(loi == NULL);
2287
2288         if (lsm_bad_magic(lsm))
2289                 RETURN(-EINVAL);
2290
2291         lap = lap_from_cookie(cookie);
2292         if (IS_ERR(lap))
2293                 RETURN(PTR_ERR(lap));
2294
2295         loi = &lsm->lsm_oinfo[lap->lap_stripe];
2296
2297         rc = obd_queue_async_io(lov->tgts[loi->loi_ost_idx].ltd_exp, lsm,
2298                                 loi, lap->lap_sub_cookie, cmd, off, count,
2299                                 brw_flags, async_flags);
2300         RETURN(rc);
2301 }
2302
2303 static int lov_set_async_flags(struct obd_export *exp,
2304                                struct lov_stripe_md *lsm,
2305                                struct lov_oinfo *loi, void *cookie,
2306                                obd_flags async_flags)
2307 {
2308         struct lov_obd *lov = &exp->exp_obd->u.lov;
2309         struct lov_async_page *lap;
2310         int rc;
2311
2312         LASSERT(loi == NULL);
2313
2314         if (lsm_bad_magic(lsm))
2315                 RETURN(-EINVAL);
2316
2317         lap = lap_from_cookie(cookie);
2318         if (IS_ERR(lap))
2319                 RETURN(PTR_ERR(lap));
2320
2321         loi = &lsm->lsm_oinfo[lap->lap_stripe];
2322
2323         rc = obd_set_async_flags(lov->tgts[loi->loi_ost_idx].ltd_exp,
2324                                  lsm, loi, lap->lap_sub_cookie, async_flags);
2325         RETURN(rc);
2326 }
2327
2328 static int lov_queue_group_io(struct obd_export *exp,
2329                               struct lov_stripe_md *lsm,
2330                               struct lov_oinfo *loi,
2331                               struct obd_io_group *oig, void *cookie,
2332                               int cmd, obd_off off, int count,
2333                               obd_flags brw_flags, obd_flags async_flags)
2334 {
2335         struct lov_obd *lov = &exp->exp_obd->u.lov;
2336         struct lov_async_page *lap;
2337         int rc;
2338
2339         LASSERT(loi == NULL);
2340
2341         if (lsm_bad_magic(lsm))
2342                 RETURN(-EINVAL);
2343
2344         lap = lap_from_cookie(cookie);
2345         if (IS_ERR(lap))
2346                 RETURN(PTR_ERR(lap));
2347
2348         loi = &lsm->lsm_oinfo[lap->lap_stripe];
2349
2350         rc = obd_queue_group_io(lov->tgts[loi->loi_ost_idx].ltd_exp, lsm, loi,
2351                                 oig, lap->lap_sub_cookie, cmd, off, count,
2352                                 brw_flags, async_flags);
2353         RETURN(rc);
2354 }
2355
2356 /* this isn't exactly optimal.  we may have queued sync io in oscs on
2357  * all stripes, but we don't record that fact at queue time.  so we
2358  * trigger sync io on all stripes. */
2359 static int lov_trigger_group_io(struct obd_export *exp,
2360                                 struct lov_stripe_md *lsm,
2361                                 struct lov_oinfo *loi,
2362                                 struct obd_io_group *oig)
2363 {
2364         struct lov_obd *lov = &exp->exp_obd->u.lov;
2365         int rc = 0, i, err;
2366
2367         LASSERT(loi == NULL);
2368
2369         if (lsm_bad_magic(lsm))
2370                 RETURN(-EINVAL);
2371
2372         loi = lsm->lsm_oinfo;
2373         for (i = 0; i < lsm->lsm_stripe_count; i++, loi++) {
2374                 if (lov->tgts[loi->loi_ost_idx].active == 0) {
2375                         CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
2376                         continue;
2377                 }
2378
2379                 err = obd_trigger_group_io(lov->tgts[loi->loi_ost_idx].ltd_exp,
2380                                            lsm, loi, oig);
2381                 if (rc == 0 && err != 0)
2382                         rc = err;
2383         };
2384         RETURN(rc);
2385 }
2386
2387 static int lov_teardown_async_page(struct obd_export *exp,
2388                                    struct lov_stripe_md *lsm,
2389                                    struct lov_oinfo *loi, void *cookie)
2390 {
2391         struct lov_obd *lov = &exp->exp_obd->u.lov;
2392         struct lov_async_page *lap;
2393         int rc;
2394
2395         LASSERT(loi == NULL);
2396
2397         if (lsm_bad_magic(lsm))
2398                 RETURN(-EINVAL);
2399
2400         lap = lap_from_cookie(cookie);
2401         if (IS_ERR(lap))
2402                 RETURN(PTR_ERR(lap));
2403
2404         loi = &lsm->lsm_oinfo[lap->lap_stripe];
2405
2406         rc = obd_teardown_async_page(lov->tgts[loi->loi_ost_idx].ltd_exp,
2407                                      lsm, loi, lap->lap_sub_cookie);
2408         if (rc) {
2409                 CERROR("unable to teardown sub cookie %p: %d\n",
2410                        lap->lap_sub_cookie, rc);
2411                 RETURN(rc);
2412         }
2413         OBD_FREE(lap, sizeof(*lap));
2414         RETURN(rc);
2415 }
2416
2417 static int lov_enqueue(struct obd_export *exp, struct lov_stripe_md *lsm,
2418                        __u32 type, ldlm_policy_data_t *policy, __u32 mode,
2419                        int *flags, void *bl_cb, void *cp_cb, void *gl_cb,
2420                        void *data,__u32 lvb_len, void *lvb_swabber,
2421                        struct lustre_handle *lockh)
2422 {
2423         struct lov_lock_handles *lov_lockh = NULL;
2424         struct lustre_handle *lov_lockhp;
2425         struct lov_obd *lov;
2426         struct lov_oinfo *loi;
2427         char submd_buf[sizeof(struct lov_stripe_md) + sizeof(struct lov_oinfo)];
2428         struct lov_stripe_md *submd = (void *)submd_buf;
2429         ldlm_error_t rc;
2430         int i, save_flags = *flags;
2431         ENTRY;
2432
2433         if (lsm_bad_magic(lsm))
2434                 RETURN(-EINVAL);
2435
2436         /* we should never be asked to replay a lock this way. */
2437         LASSERT((*flags & LDLM_FL_REPLAY) == 0);
2438
2439         if (!exp || !exp->exp_obd)
2440                 RETURN(-ENODEV);
2441
2442         if (lsm->lsm_stripe_count > 1) {
2443                 lov_lockh = lov_llh_new(lsm);
2444                 if (lov_lockh == NULL)
2445                         RETURN(-ENOMEM);
2446
2447                 lockh->cookie = lov_lockh->llh_handle.h_cookie;
2448                 lov_lockhp = lov_lockh->llh_handles;
2449         } else {
2450                 lov_lockhp = lockh;
2451         }
2452
2453         lov = &exp->exp_obd->u.lov;
2454         for (i = 0, loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count;
2455              i++, loi++, lov_lockhp++) {
2456                 ldlm_policy_data_t sub_ext;
2457                 obd_off start, end;
2458
2459                 if (!lov_stripe_intersects(lsm, i, policy->l_extent.start,
2460                                            policy->l_extent.end, &start,
2461                                            &end))
2462                         continue;
2463
2464                 sub_ext.l_extent.start = start;
2465                 sub_ext.l_extent.end = end;
2466                 sub_ext.l_extent.gid = policy->l_extent.gid;
2467
2468                 if (lov->tgts[loi->loi_ost_idx].active == 0) {
2469                         CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
2470                         continue;
2471                 }
2472
2473                 /* XXX LOV STACKING: submd should be from the subobj */
2474                 submd->lsm_object_id = loi->loi_id;
2475                 submd->lsm_object_gr = lsm->lsm_object_gr;
2476                 submd->lsm_stripe_count = 0;
2477                 submd->lsm_oinfo->loi_kms_valid = loi->loi_kms_valid;
2478                 submd->lsm_oinfo->loi_rss = loi->loi_rss;
2479                 submd->lsm_oinfo->loi_kms = loi->loi_kms;
2480                 submd->lsm_oinfo->loi_blocks = loi->loi_blocks;
2481                 loi->loi_mtime = submd->lsm_oinfo->loi_mtime;
2482                 /* XXX submd is not fully initialized here */
2483                 *flags = save_flags;
2484                 rc = obd_enqueue(lov->tgts[loi->loi_ost_idx].ltd_exp, submd,
2485                                  type, &sub_ext, mode, flags, bl_cb, cp_cb,
2486                                  gl_cb, data, lvb_len, lvb_swabber, lov_lockhp);
2487
2488                 /* XXX FIXME: This unpleasantness doesn't belong here at *all*.
2489                  * It belongs in the OSC, except that the OSC doesn't have
2490                  * access to the real LOI -- it gets a copy, that we created
2491                  * above, and that copy can be arbitrarily out of date.
2492                  *
2493                  * The LOV API is due for a serious rewriting anyways, and this
2494                  * can be addressed then. */
2495                 if (rc == ELDLM_OK) {
2496                         struct ldlm_lock *lock = ldlm_handle2lock(lov_lockhp);
2497                         __u64 tmp = submd->lsm_oinfo->loi_rss;
2498
2499                         LASSERT(lock != NULL);
2500                         loi->loi_rss = tmp;
2501                         loi->loi_blocks = submd->lsm_oinfo->loi_blocks;
2502                         /* Extend KMS up to the end of this lock and no further
2503                          * A lock on [x,y] means a KMS of up to y + 1 bytes! */
2504                         if (tmp > lock->l_policy_data.l_extent.end)
2505                                 tmp = lock->l_policy_data.l_extent.end + 1;
2506                         if (tmp >= loi->loi_kms) {
2507                                 CDEBUG(D_INODE, "lock acquired, setting rss="
2508                                        LPU64", kms="LPU64"\n", loi->loi_rss,
2509                                        tmp);
2510                                 loi->loi_kms = tmp;
2511                                 loi->loi_kms_valid = 1;
2512                         } else {
2513                                 CDEBUG(D_INODE, "lock acquired, setting rss="
2514                                        LPU64"; leaving kms="LPU64", end="LPU64
2515                                        "\n", loi->loi_rss, loi->loi_kms,
2516                                        lock->l_policy_data.l_extent.end);
2517                         }
2518                         ldlm_lock_allow_match(lock);
2519                         LDLM_LOCK_PUT(lock);
2520                 } else if (rc == ELDLM_LOCK_ABORTED &&
2521                            save_flags & LDLM_FL_HAS_INTENT) {
2522                         memset(lov_lockhp, 0, sizeof(*lov_lockhp));
2523                         loi->loi_rss = submd->lsm_oinfo->loi_rss;
2524                         loi->loi_blocks = submd->lsm_oinfo->loi_blocks;
2525                         CDEBUG(D_INODE, "glimpsed, setting rss="LPU64"; leaving"
2526                                " kms="LPU64"\n", loi->loi_rss, loi->loi_kms);
2527                 } else {
2528                         memset(lov_lockhp, 0, sizeof(*lov_lockhp));
2529                         if (lov->tgts[loi->loi_ost_idx].active) {
2530                                 CERROR("error: enqueue objid "LPX64" subobj "
2531                                        LPX64" on OST idx %d: rc = %d\n",
2532                                        lsm->lsm_object_id, loi->loi_id,
2533                                        loi->loi_ost_idx, rc);
2534                                 GOTO(out_locks, rc);
2535                         }
2536                 }
2537         }
2538         if (lsm->lsm_stripe_count > 1)
2539                 lov_llh_put(lov_lockh);
2540         RETURN(ELDLM_OK);
2541
2542  out_locks:
2543         while (loi--, lov_lockhp--, i-- > 0) {
2544                 struct lov_stripe_md submd;
2545                 int err;
2546
2547                 if (lov_lockhp->cookie == 0)
2548                         continue;
2549
2550                 /* XXX LOV STACKING: submd should be from the subobj */
2551                 submd.lsm_object_id = loi->loi_id;
2552                 submd.lsm_object_gr = lsm->lsm_object_gr;
2553                 submd.lsm_stripe_count = 0;
2554                 err = obd_cancel(lov->tgts[loi->loi_ost_idx].ltd_exp, &submd,
2555                                  mode, lov_lockhp);
2556                 if (err && lov->tgts[loi->loi_ost_idx].active) {
2557                         CERROR("error: cancelling objid "LPX64" on OST "
2558                                "idx %d after enqueue error: rc = %d\n",
2559                                loi->loi_id, loi->loi_ost_idx, err);
2560                 }
2561         }
2562
2563         if (lsm->lsm_stripe_count > 1) {
2564                 lov_llh_destroy(lov_lockh);
2565                 lov_llh_put(lov_lockh);
2566         }
2567         return rc;
2568 }
2569
2570 static int lov_match(struct obd_export *exp, struct lov_stripe_md *lsm,
2571                      __u32 type, ldlm_policy_data_t *policy, __u32 mode,
2572                      int *flags, void *data, struct lustre_handle *lockh)
2573 {
2574         struct lov_lock_handles *lov_lockh = NULL;
2575         struct lustre_handle *lov_lockhp;
2576         struct lov_obd *lov;
2577         struct lov_oinfo *loi;
2578         struct lov_stripe_md submd;
2579         ldlm_error_t rc = 0;
2580         int i;
2581         ENTRY;
2582
2583         if (lsm_bad_magic(lsm))
2584                 RETURN(-EINVAL);
2585
2586         if (!exp || !exp->exp_obd)
2587                 RETURN(-ENODEV);
2588
2589         if (lsm->lsm_stripe_count > 1) {
2590                 lov_lockh = lov_llh_new(lsm);
2591                 if (lov_lockh == NULL)
2592                         RETURN(-ENOMEM);
2593
2594                 lockh->cookie = lov_lockh->llh_handle.h_cookie;
2595                 lov_lockhp = lov_lockh->llh_handles;
2596         } else {
2597                 lov_lockhp = lockh;
2598         }
2599
2600         lov = &exp->exp_obd->u.lov;
2601         for (i = 0, loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count;
2602              i++, loi++, lov_lockhp++) {
2603                 ldlm_policy_data_t sub_ext;
2604                 obd_off start, end;
2605                 int lov_flags;
2606
2607                 if (!lov_stripe_intersects(lsm, i, policy->l_extent.start,
2608                                            policy->l_extent.end, &start, &end))
2609                         continue;
2610
2611                 sub_ext.l_extent.start = start;
2612                 sub_ext.l_extent.end = end;
2613
2614                 if (obd_uuid_empty(&lov->tgts[loi->loi_ost_idx].uuid)) {
2615                         CDEBUG(D_HA, "lov idx %d deleted\n", loi->loi_ost_idx);
2616                         continue;
2617                 }
2618                 if (lov->tgts[loi->loi_ost_idx].active == 0) {
2619                         CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
2620                         rc = -EIO;
2621                         break;
2622                 }
2623
2624                 /* XXX LOV STACKING: submd should be from the subobj */
2625                 submd.lsm_object_id = loi->loi_id;
2626                 submd.lsm_object_gr = lsm->lsm_object_gr;
2627                 submd.lsm_stripe_count = 0;
2628                 lov_flags = *flags;
2629                 /* XXX submd is not fully initialized here */
2630                 rc = obd_match(lov->tgts[loi->loi_ost_idx].ltd_exp, &submd,
2631                                type, &sub_ext, mode, &lov_flags, data,
2632                                lov_lockhp);
2633                 if (rc != 1)
2634                         break;
2635         }
2636         if (rc == 1) {
2637                 if (lsm->lsm_stripe_count > 1) {
2638                         if (*flags & LDLM_FL_TEST_LOCK)
2639                                 lov_llh_destroy(lov_lockh);
2640                         lov_llh_put(lov_lockh);
2641                 }
2642                 RETURN(1);
2643         }
2644
2645         while (loi--, lov_lockhp--, i-- > 0) {
2646                 struct lov_stripe_md submd;
2647                 int err;
2648
2649                 if (lov_lockhp->cookie == 0)
2650                         continue;
2651
2652                 /* XXX LOV STACKING: submd should be from the subobj */
2653                 submd.lsm_object_id = loi->loi_id;
2654                 submd.lsm_object_gr = lsm->lsm_object_gr;
2655                 submd.lsm_stripe_count = 0;
2656                 err = obd_cancel(lov->tgts[loi->loi_ost_idx].ltd_exp, &submd,
2657                                  mode, lov_lockhp);
2658                 if (err && lov->tgts[loi->loi_ost_idx].active) {
2659                         CERROR("error: cancelling objid "LPX64" on OST "
2660                                "idx %d after match failure: rc = %d\n",
2661                                loi->loi_id, loi->loi_ost_idx, err);
2662                 }
2663         }
2664
2665         if (lsm->lsm_stripe_count > 1) {
2666                 lov_llh_destroy(lov_lockh);
2667                 lov_llh_put(lov_lockh);
2668         }
2669         RETURN(rc);
2670 }
2671
2672 static int lov_change_cbdata(struct obd_export *exp,
2673                              struct lov_stripe_md *lsm, ldlm_iterator_t it,
2674                              void *data)
2675 {
2676         struct lov_obd *lov;
2677         struct lov_oinfo *loi;
2678         int rc = 0, i;
2679         ENTRY;
2680
2681         if (lsm_bad_magic(lsm))
2682                 RETURN(-EINVAL);
2683
2684         if (!exp || !exp->exp_obd)
2685                 RETURN(-ENODEV);
2686
2687         LASSERT(lsm->lsm_object_gr > 0);
2688
2689         lov = &exp->exp_obd->u.lov;
2690         for (i = 0,loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++,loi++) {
2691                 struct lov_stripe_md submd;
2692                 if (lov->tgts[loi->loi_ost_idx].active == 0) {
2693                         CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
2694                         continue;
2695                 }
2696
2697                 submd.lsm_object_id = loi->loi_id;
2698                 submd.lsm_object_gr = lsm->lsm_object_gr;
2699                 submd.lsm_stripe_count = 0;
2700                 rc = obd_change_cbdata(lov->tgts[loi->loi_ost_idx].ltd_exp,
2701                                        &submd, it, data);
2702         }
2703         RETURN(rc);
2704 }
2705
2706 static int lov_cancel(struct obd_export *exp, struct lov_stripe_md *lsm,
2707                       __u32 mode, struct lustre_handle *lockh)
2708 {
2709         struct lov_lock_handles *lov_lockh = NULL;
2710         struct lustre_handle *lov_lockhp;
2711         struct lov_obd *lov;
2712         struct lov_oinfo *loi;
2713         int rc = 0, i;
2714         ENTRY;
2715
2716         if (lsm_bad_magic(lsm))
2717                 RETURN(-EINVAL);
2718
2719         if (!exp || !exp->exp_obd)
2720                 RETURN(-ENODEV);
2721
2722         LASSERT(lsm->lsm_object_gr > 0);
2723
2724         LASSERT(lockh);
2725         if (lsm->lsm_stripe_count > 1) {
2726                 lov_lockh = lov_handle2llh(lockh);
2727                 if (!lov_lockh) {
2728                         CERROR("LOV: invalid lov lock handle %p\n", lockh);
2729                         RETURN(-EINVAL);
2730                 }
2731
2732                 lov_lockhp = lov_lockh->llh_handles;
2733         } else {
2734                 lov_lockhp = lockh;
2735         }
2736
2737         lov = &exp->exp_obd->u.lov;
2738         for (i = 0, loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count;
2739              i++, loi++, lov_lockhp++) {
2740                 struct lov_stripe_md submd;
2741                 int err;
2742
2743                 if (lov_lockhp->cookie == 0) {
2744                         CDEBUG(D_HA, "lov idx %d subobj "LPX64" no lock?\n",
2745                                loi->loi_ost_idx, loi->loi_id);
2746                         continue;
2747                 }
2748
2749                 /* XXX LOV STACKING: submd should be from the subobj */
2750                 submd.lsm_object_id = loi->loi_id;
2751                 submd.lsm_object_gr = lsm->lsm_object_gr;
2752                 submd.lsm_stripe_count = 0;
2753                 err = obd_cancel(lov->tgts[loi->loi_ost_idx].ltd_exp, &submd,
2754                                  mode, lov_lockhp);
2755                 if (err) {
2756                         if (lov->tgts[loi->loi_ost_idx].active) {
2757                                 CERROR("error: cancel objid "LPX64" subobj "
2758                                        LPX64" on OST idx %d: rc = %d\n",
2759                                        lsm->lsm_object_id,
2760                                        loi->loi_id, loi->loi_ost_idx, err);
2761                                 if (!rc)
2762                                         rc = err;
2763                         }
2764                 }
2765         }
2766
2767         if (lsm->lsm_stripe_count > 1)
2768                 lov_llh_destroy(lov_lockh);
2769         if (lov_lockh != NULL)
2770                 lov_llh_put(lov_lockh);
2771         RETURN(rc);
2772 }
2773
2774 static int lov_cancel_unused(struct obd_export *exp,
2775                              struct lov_stripe_md *lsm, int flags, void *opaque)
2776 {
2777         struct lov_obd *lov;
2778         struct lov_oinfo *loi;
2779         int rc = 0, i;
2780         ENTRY;
2781
2782         lov = &exp->exp_obd->u.lov;
2783         if (lsm == NULL) {
2784                 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
2785                         int err = obd_cancel_unused(lov->tgts[i].ltd_exp, NULL,
2786                                                     flags, opaque);
2787                         if (!rc)
2788                                 rc = err;
2789                 }
2790                 RETURN(rc);
2791         }
2792
2793         if (lsm_bad_magic(lsm))
2794                 RETURN(-EINVAL);
2795
2796         if (!exp || !exp->exp_obd)
2797                 RETURN(-ENODEV);
2798
2799         LASSERT(lsm->lsm_object_gr > 0);
2800
2801         for (i = 0,loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++,loi++) {
2802                 struct lov_stripe_md submd;
2803                 int err;
2804
2805                 if (lov->tgts[loi->loi_ost_idx].active == 0)
2806                         CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
2807
2808                 submd.lsm_object_id = loi->loi_id;
2809                 submd.lsm_object_gr = lsm->lsm_object_gr;
2810                 submd.lsm_stripe_count = 0;
2811                 err = obd_cancel_unused(lov->tgts[loi->loi_ost_idx].ltd_exp,
2812                                         &submd, flags, opaque);
2813                 if (err && lov->tgts[loi->loi_ost_idx].active) {
2814                         CERROR("error: cancel unused objid "LPX64" subobj "LPX64
2815                                " on OST idx %d: rc = %d\n", lsm->lsm_object_id,
2816                                loi->loi_id, loi->loi_ost_idx, err);
2817                         if (!rc)
2818                                 rc = err;
2819                 }
2820         }
2821         RETURN(rc);
2822 }
2823
2824 #define LOV_U64_MAX ((__u64)~0ULL)
2825 #define LOV_SUM_MAX(tot, add)                                           \
2826         do {                                                            \
2827                 if ((tot) + (add) < (tot))                              \
2828                         (tot) = LOV_U64_MAX;                            \
2829                 else                                                    \
2830                         (tot) += (add);                                 \
2831         } while(0)
2832
2833 static int lov_statfs(struct obd_device *obd, struct obd_statfs *osfs,
2834                       unsigned long max_age)
2835 {
2836         struct lov_obd *lov = &obd->u.lov;
2837         struct obd_statfs lov_sfs;
2838         int set = 0;
2839         int rc = 0;
2840         int i;
2841         ENTRY;
2842
2843
2844         /* We only get block data from the OBD */
2845         for (i = 0; i < lov->desc.ld_tgt_count; i++) {
2846                 int err;
2847                 if (!lov->tgts[i].active) {
2848                         CDEBUG(D_HA, "lov idx %d inactive\n", i);
2849                         continue;
2850                 }
2851
2852                 err = obd_statfs(class_exp2obd(lov->tgts[i].ltd_exp), &lov_sfs,
2853                                  max_age);
2854                 if (err) {
2855                         if (lov->tgts[i].active && !rc)
2856                                 rc = err;
2857                         continue;
2858                 }
2859
2860                 if (!set) {
2861                         memcpy(osfs, &lov_sfs, sizeof(lov_sfs));
2862                         set = 1;
2863                 } else {
2864                         osfs->os_bfree += lov_sfs.os_bfree;
2865                         osfs->os_bavail += lov_sfs.os_bavail;
2866                         osfs->os_blocks += lov_sfs.os_blocks;
2867                         /* XXX not sure about this one - depends on policy.
2868                          *   - could be minimum if we always stripe on all OBDs
2869                          *     (but that would be wrong for any other policy,
2870                          *     if one of the OBDs has no more objects left)
2871                          *   - could be sum if we stripe whole objects
2872                          *   - could be average, just to give a nice number
2873                          *
2874                          * To give a "reasonable" (if not wholly accurate)
2875                          * number, we divide the total number of free objects
2876                          * by expected stripe count (watch out for overflow).
2877                          */
2878                         LOV_SUM_MAX(osfs->os_files, lov_sfs.os_files);
2879                         LOV_SUM_MAX(osfs->os_ffree, lov_sfs.os_ffree);
2880                 }
2881         }
2882
2883         if (set) {
2884                 __u32 expected_stripes = lov->desc.ld_default_stripe_count ?
2885                                          lov->desc.ld_default_stripe_count :
2886                                          lov->desc.ld_active_tgt_count;
2887
2888                 if (osfs->os_files != LOV_U64_MAX)
2889                         do_div(osfs->os_files, expected_stripes);
2890                 if (osfs->os_ffree != LOV_U64_MAX)
2891                         do_div(osfs->os_ffree, expected_stripes);
2892         } else if (!rc)
2893                 rc = -EIO;
2894
2895         RETURN(rc);
2896 }
2897
2898 static int lov_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
2899                          void *karg, void *uarg)
2900 {
2901         struct obd_device *obddev = class_exp2obd(exp);
2902         struct lov_obd *lov = &obddev->u.lov;
2903         int i, rc, count = lov->desc.ld_tgt_count;
2904         struct obd_uuid *uuidp;
2905         ENTRY;
2906
2907         switch (cmd) {
2908         case OBD_IOC_LOV_GET_CONFIG: {
2909                 struct obd_ioctl_data *data = karg;
2910                 struct lov_tgt_desc *tgtdesc;
2911                 struct lov_desc *desc;
2912                 char *buf = NULL;
2913                 __u32 *genp;
2914
2915                 buf = NULL;
2916                 len = 0;
2917                 if (obd_ioctl_getdata(&buf, &len, (void *)uarg))
2918                         RETURN(-EINVAL);
2919
2920                 data = (struct obd_ioctl_data *)buf;
2921
2922                 if (sizeof(*desc) > data->ioc_inllen1) {
2923                         obd_ioctl_freedata(buf, len);
2924                         RETURN(-EINVAL);
2925                 }
2926
2927                 if (sizeof(uuidp->uuid) * count > data->ioc_inllen2) {
2928                         obd_ioctl_freedata(buf, len);
2929                         RETURN(-EINVAL);
2930                 }
2931
2932                 if (sizeof(__u32) * count > data->ioc_inllen3) {
2933                         obd_ioctl_freedata(buf, len);
2934                         RETURN(-EINVAL);
2935                 }
2936
2937                 desc = (struct lov_desc *)data->ioc_inlbuf1;
2938                 memcpy(desc, &(lov->desc), sizeof(*desc));
2939
2940                 uuidp = (struct obd_uuid *)data->ioc_inlbuf2;
2941                 genp = (__u32 *)data->ioc_inlbuf3;
2942                 tgtdesc = lov->tgts;
2943                 /* the uuid will be empty for deleted OSTs */
2944                 for (i = 0; i < count; i++, uuidp++, genp++, tgtdesc++) {
2945                         obd_str2uuid(uuidp, tgtdesc->uuid.uuid);
2946                         *genp = tgtdesc->ltd_gen;
2947                 }
2948
2949                 rc = copy_to_user((void *)uarg, buf, len);
2950                 if (rc)
2951                         rc = -EFAULT;
2952                 obd_ioctl_freedata(buf, len);
2953                 break;
2954         }
2955         case LL_IOC_LOV_SETSTRIPE:
2956                 rc = lov_setstripe(exp, karg, uarg);
2957                 break;
2958         case LL_IOC_LOV_GETSTRIPE:
2959                 rc = lov_getstripe(exp, karg, uarg);
2960                 break;
2961         case LL_IOC_LOV_SETEA:
2962                 rc = lov_setea(exp, karg, uarg);
2963                 break;
2964         default: {
2965                 int set = 0;
2966                 if (count == 0)
2967                         RETURN(-ENOTTY);
2968                 rc = 0;
2969                 for (i = 0; i < count; i++) {
2970                         int err;
2971
2972                         /* OST was deleted */
2973                         if (obd_uuid_empty(&lov->tgts[i].uuid))
2974                                 continue;
2975
2976                         err = obd_iocontrol(cmd, lov->tgts[i].ltd_exp,
2977                                             len, karg, uarg);
2978                         if (err) {
2979                                 if (lov->tgts[i].active) {
2980                                         CERROR("error: iocontrol OSC %s on OST"
2981                                                "idx %d: cmd %x err = %d\n",
2982                                                lov->tgts[i].uuid.uuid, i,
2983                                                cmd, err);
2984                                         if (!rc)
2985                                                 rc = err;
2986                                 }
2987                         } else
2988                                 set = 1;
2989                 }
2990                 if (!set && !rc)
2991                         rc = -EIO;
2992         }
2993         }
2994
2995         RETURN(rc);
2996 }
2997
2998 static int lov_get_info(struct obd_export *exp, __u32 keylen,
2999                         void *key, __u32 *vallen, void *val)
3000 {
3001         struct obd_device *obddev = class_exp2obd(exp);
3002         struct lov_obd *lov = &obddev->u.lov;
3003         int i;
3004         ENTRY;
3005
3006         if (!vallen || !val)
3007                 RETURN(-EFAULT);
3008
3009         if (keylen > strlen("lock_to_stripe") &&
3010             strcmp(key, "lock_to_stripe") == 0) {
3011                 struct {
3012                         char name[16];
3013                         struct ldlm_lock *lock;
3014                         struct lov_stripe_md *lsm;
3015                 } *data = key;
3016                 struct lov_oinfo *loi;
3017                 struct ldlm_res_id *res_id = &data->lock->l_resource->lr_name;
3018                 __u32 *stripe = val;
3019
3020                 if (*vallen < sizeof(*stripe))
3021                         RETURN(-EFAULT);
3022                 *vallen = sizeof(*stripe);
3023
3024                 /* XXX This is another one of those bits that will need to
3025                  * change if we ever actually support nested LOVs.  It uses
3026                  * the lock's export to find out which stripe it is. */
3027                 /* XXX - it's assumed all the locks for deleted OSTs have
3028                  * been cancelled. Also, the export for deleted OSTs will
3029                  * be NULL and won't match the lock's export. */
3030                 for (i = 0, loi = data->lsm->lsm_oinfo;
3031                      i < data->lsm->lsm_stripe_count;
3032                      i++, loi++) {
3033                         if (lov->tgts[loi->loi_ost_idx].ltd_exp ==
3034                                         data->lock->l_conn_export &&
3035                             loi->loi_id == res_id->name[0] &&
3036                             loi->loi_gr == res_id->name[2]) {
3037                                 *stripe = i;
3038                                 RETURN(0);
3039                         }
3040                 }
3041                 LDLM_ERROR(data->lock, "lock on inode without such object\n");
3042                 dump_lsm(D_ERROR, data->lsm);
3043                 RETURN(-ENXIO);
3044         } else if (keylen >= strlen("size_to_stripe") &&
3045                    strcmp(key, "size_to_stripe") == 0) {
3046                 struct {
3047                         int stripe_number;
3048                         __u64 size;
3049                         struct lov_stripe_md *lsm;
3050                 } *data = val;
3051
3052                 if (*vallen < sizeof(*data))
3053                         RETURN(-EFAULT);
3054
3055                 data->size = lov_size_to_stripe(data->lsm, data->size,
3056                                                 data->stripe_number);
3057                 RETURN(0);
3058         } else if (keylen >= strlen("last_id") && strcmp(key, "last_id") == 0) {
3059                 obd_id *ids = val;
3060                 int rc, size = sizeof(obd_id);
3061                 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
3062                         if (!lov->tgts[i].active)
3063                                 continue;
3064                         rc = obd_get_info(lov->tgts[i].ltd_exp, keylen, key,
3065                                           &size, &(ids[i]));
3066                         if (rc != 0)
3067                                 RETURN(rc);
3068                 }
3069                 RETURN(0);
3070         } else if (keylen >= strlen("lovdesc") && strcmp(key, "lovdesc") == 0) {
3071                 struct lov_desc *desc_ret = val;
3072                 *desc_ret = lov->desc;
3073
3074                 RETURN(0);
3075         }
3076
3077         RETURN(-EINVAL);
3078 }
3079
3080 static int lov_set_info(struct obd_export *exp, obd_count keylen,
3081                         void *key, obd_count vallen, void *val)
3082 {
3083         struct obd_device *obddev = class_exp2obd(exp);
3084         struct lov_obd *lov = &obddev->u.lov;
3085         int i, rc = 0;
3086         ENTRY;
3087
3088 #define KEY_IS(str) \
3089         (keylen == strlen(str) && memcmp(key, str, keylen) == 0)
3090
3091         if (KEY_IS("next_id")) {
3092                 if (vallen != lov->desc.ld_tgt_count)
3093                         RETURN(-EINVAL);
3094                 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
3095                         int er;
3096
3097                         /* OST was deleted */
3098                         if (obd_uuid_empty(&lov->tgts[i].uuid))
3099                                 continue;
3100
3101                         /* initialize all OSCs, even inactive ones */
3102
3103                         er = obd_set_info(lov->tgts[i].ltd_exp, keylen, key,
3104                                           sizeof(obd_id), ((obd_id*)val) + i);
3105                         if (!rc)
3106                                 rc = er;
3107                 }
3108                 RETURN(rc);
3109         }
3110         if (KEY_IS("growth_count")) {
3111                 if (vallen != sizeof(int))
3112                         RETURN(-EINVAL);
3113         } else if (KEY_IS("mds_conn")) {
3114                 if (vallen != sizeof(__u32))
3115                         RETURN(-EINVAL);
3116         } else if (KEY_IS("unlinked") || KEY_IS("unrecovery")) {
3117                 if (vallen != 0)
3118                         RETURN(-EINVAL);
3119         } else {
3120                 RETURN(-EINVAL);
3121         }
3122
3123         for (i = 0; i < lov->desc.ld_tgt_count; i++) {
3124                 int er;
3125
3126                 /* OST was deleted */
3127                 if (obd_uuid_empty(&lov->tgts[i].uuid))
3128                         continue;
3129
3130                 if (!val && !lov->tgts[i].active)
3131                         continue;
3132
3133                 er = obd_set_info(lov->tgts[i].ltd_exp, keylen, key, vallen,
3134                                   val);
3135                 if (!rc)
3136                         rc = er;
3137         }
3138         RETURN(rc);
3139 #undef KEY_IS
3140
3141 }
3142
3143 /* Merge rss if @kms_only == 0
3144  *
3145  * Even when merging RSS, we will take the KMS value if it's larger.
3146  * This prevents getattr from stomping on dirty cached pages which
3147  * extend the file size. */
3148 __u64 lov_merge_size(struct lov_stripe_md *lsm, int kms_only)
3149 {
3150         struct lov_oinfo *loi;
3151         __u64 size = 0;
3152         int i;
3153
3154         for (i = 0, loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count;
3155              i++, loi++) {
3156                 obd_size lov_size, tmpsize;
3157
3158                 tmpsize = loi->loi_kms;
3159                 if (kms_only == 0 && loi->loi_rss > tmpsize)
3160                         tmpsize = loi->loi_rss;
3161
3162                 lov_size = lov_stripe_size(lsm, tmpsize, i);
3163                 if (lov_size > size)
3164                         size = lov_size;
3165         }
3166
3167         return size;
3168 }
3169 EXPORT_SYMBOL(lov_merge_size);
3170
3171 /* Merge blocks */
3172 __u64 lov_merge_blocks(struct lov_stripe_md *lsm)
3173 {
3174         struct lov_oinfo *loi;
3175         __u64 blocks = 0;
3176         int i;
3177
3178         for (i = 0, loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count;
3179              i++, loi++) {
3180                 blocks += loi->loi_blocks;
3181         }
3182         return blocks;
3183 }
3184 EXPORT_SYMBOL(lov_merge_blocks);
3185
3186 __u64 lov_merge_mtime(struct lov_stripe_md *lsm, __u64 current_time)
3187 {
3188         struct lov_oinfo *loi;
3189         int i;
3190
3191         for (i = 0, loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count;
3192              i++, loi++) {
3193                 if (loi->loi_mtime > current_time)
3194                         current_time = loi->loi_mtime;
3195         }
3196         return current_time;
3197 }
3198 EXPORT_SYMBOL(lov_merge_mtime);
3199
3200 #if 0
3201 struct lov_multi_wait {
3202         struct ldlm_lock *lock;
3203         wait_queue_t      wait;
3204         int               completed;
3205         int               generation;
3206 };
3207
3208 int lov_complete_many(struct obd_export *exp, struct lov_stripe_md *lsm,
3209                       struct lustre_handle *lockh)
3210 {
3211         struct lov_lock_handles *lov_lockh = NULL;
3212         struct lustre_handle *lov_lockhp;
3213         struct lov_obd *lov;
3214         struct lov_oinfo *loi;
3215         struct lov_multi_wait *queues;
3216         int rc = 0, i;
3217         ENTRY;
3218
3219         if (lsm_bad_magic(lsm))
3220                 RETURN(-EINVAL);
3221
3222         if (!exp || !exp->exp_obd)
3223                 RETURN(-ENODEV);
3224
3225         LASSERT(lockh != NULL);
3226         if (lsm->lsm_stripe_count > 1) {
3227                 lov_lockh = lov_handle2llh(lockh);
3228                 if (lov_lockh == NULL) {
3229                         CERROR("LOV: invalid lov lock handle %p\n", lockh);
3230                         RETURN(-EINVAL);
3231                 }
3232
3233                 lov_lockhp = lov_lockh->llh_handles;
3234         } else {
3235                 lov_lockhp = lockh;
3236         }
3237
3238         OBD_ALLOC(queues, lsm->lsm_stripe_count * sizeof(*queues));
3239         if (queues == NULL)
3240                 GOTO(out, rc = -ENOMEM);
3241
3242         lov = &exp->exp_obd->u.lov;
3243         for (i = 0, loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count;
3244              i++, loi++, lov_lockhp++) {
3245                 struct ldlm_lock *lock;
3246                 struct obd_device *obd;
3247                 unsigned long irqflags;
3248
3249                 lock = ldlm_handle2lock(lov_lockhp);
3250                 if (lock == NULL) {
3251                         CDEBUG(D_HA, "lov idx %d subobj "LPX64" no lock?\n",
3252                                loi->loi_ost_idx, loi->loi_id);
3253                         queues[i].completed = 1;
3254                         continue;
3255                 }
3256
3257                 queues[i].lock = lock;
3258                 init_waitqueue_entry(&(queues[i].wait), current);
3259                 add_wait_queue(lock->l_waitq, &(queues[i].wait));
3260
3261                 obd = class_exp2obd(lock->l_conn_export);
3262                 if (obd != NULL)
3263                         imp = obd->u.cli.cl_import;
3264                 if (imp != NULL) {
3265                         spin_lock_irqsave(&imp->imp_lock, irqflags);
3266                         queues[i].generation = imp->imp_generation;
3267                         spin_unlock_irqrestore(&imp->imp_lock, irqflags);
3268                 }
3269         }
3270
3271         lwi = LWI_TIMEOUT_INTR(obd_timeout * HZ, ldlm_expired_completion_wait,
3272                                interrupted_completion_wait, &lwd);
3273         rc = l_wait_event_added(check_multi_complete(queues, lsm), &lwi);
3274
3275         for (i = 0; i < lsm->lsm_stripe_count; i++)
3276                 remove_wait_queue(lock->l_waitq, &(queues[i].wait));
3277
3278         if (rc == -EINTR || rc == -ETIMEDOUT) {
3279
3280
3281         }
3282
3283  out:
3284         if (lov_lockh != NULL)
3285                 lov_llh_put(lov_lockh);
3286         RETURN(rc);
3287 }
3288 #endif
3289
3290 void lov_increase_kms(struct obd_export *exp, struct lov_stripe_md *lsm,
3291                       obd_off size)
3292 {
3293         struct lov_oinfo *loi;
3294         int stripe = 0;
3295         __u64 kms;
3296         ENTRY;
3297
3298         if (size > 0)
3299                 stripe = lov_stripe_number(lsm, size - 1);
3300         kms = lov_size_to_stripe(lsm, size, stripe);
3301         loi = &(lsm->lsm_oinfo[stripe]);
3302
3303         CDEBUG(D_INODE, "stripe %d KMS %sincreasing "LPU64"->"LPU64"\n",
3304                stripe, kms > loi->loi_kms ? "" : "not ", loi->loi_kms, kms);
3305         if (kms > loi->loi_kms)
3306                 loi->loi_kms = kms;
3307         EXIT;
3308 }
3309 EXPORT_SYMBOL(lov_increase_kms);
3310
3311 struct obd_ops lov_obd_ops = {
3312         .o_owner               = THIS_MODULE,
3313         .o_attach              = lov_attach,
3314         .o_detach              = lov_detach,
3315         .o_setup               = lov_setup,
3316         .o_cleanup             = lov_cleanup,
3317         .o_process_config      = lov_process_config,
3318         .o_connect             = lov_connect,
3319         .o_disconnect          = lov_disconnect,
3320         .o_statfs              = lov_statfs,
3321         .o_packmd              = lov_packmd,
3322         .o_unpackmd            = lov_unpackmd,
3323         .o_revalidate_md       = lov_revalidate_md,
3324         .o_create              = lov_create,
3325         .o_destroy             = lov_destroy,
3326         .o_getattr             = lov_getattr,
3327         .o_getattr_async       = lov_getattr_async,
3328         .o_setattr             = lov_setattr,
3329         .o_brw                 = lov_brw,
3330         .o_brw_async           = lov_brw_async,
3331         .o_prep_async_page     = lov_prep_async_page,
3332         .o_queue_async_io      = lov_queue_async_io,
3333         .o_set_async_flags     = lov_set_async_flags,
3334         .o_queue_group_io      = lov_queue_group_io,
3335         .o_trigger_group_io    = lov_trigger_group_io,
3336         .o_teardown_async_page = lov_teardown_async_page,
3337         .o_punch               = lov_punch,
3338         .o_sync                = lov_sync,
3339         .o_enqueue             = lov_enqueue,
3340         .o_match               = lov_match,
3341         .o_change_cbdata       = lov_change_cbdata,
3342         .o_cancel              = lov_cancel,
3343         .o_cancel_unused       = lov_cancel_unused,
3344         .o_iocontrol           = lov_iocontrol,
3345         .o_get_info            = lov_get_info,
3346         .o_set_info            = lov_set_info,
3347         .o_llog_init           = lov_llog_init,
3348         .o_llog_finish         = lov_llog_finish,
3349         .o_notify              = lov_notify,
3350 };
3351
3352 int __init lov_init(void)
3353 {
3354         struct lprocfs_static_vars lvars;
3355         int rc;
3356         ENTRY;
3357
3358         lprocfs_init_vars(lov, &lvars);
3359         rc = class_register_type(&lov_obd_ops, NULL, lvars.module_vars,
3360                                  OBD_LOV_DEVICENAME);
3361         RETURN(rc);
3362 }
3363
3364 #ifdef __KERNEL__
3365 static void /*__exit*/ lov_exit(void)
3366 {
3367         class_unregister_type(OBD_LOV_DEVICENAME);
3368 }
3369
3370 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
3371 MODULE_DESCRIPTION("Lustre Logical Object Volume OBD driver");
3372 MODULE_LICENSE("GPL");
3373
3374 module_init(lov_init);
3375 module_exit(lov_exit);
3376 #endif