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