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