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