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