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