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