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