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