1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
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>
9 * This file is part of Lustre, http://www.lustre.org.
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.
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.
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.
26 #define DEBUG_SUBSYSTEM S_LOV
28 #include <linux/slab.h>
29 #include <linux/module.h>
30 #include <linux/init.h>
31 #include <linux/random.h>
32 #include <linux/slab.h>
33 #include <linux/pagemap.h>
34 #include <asm/div64.h>
36 #include <liblustre.h>
39 #include <linux/obd_support.h>
40 #include <linux/lustre_lib.h>
41 #include <linux/lustre_net.h>
42 #include <linux/lustre_idl.h>
43 #include <linux/lustre_lite.h> /* for LL_IOC_LOV_[GS]ETSTRIPE */
44 #include <linux/lustre_mds.h>
45 #include <linux/obd_class.h>
46 #include <linux/obd_lov.h>
47 #include <linux/seq_file.h>
48 #include <linux/lprocfs_status.h>
50 #include "lov_internal.h"
52 static int lov_stripe_offset(struct lov_stripe_md *lsm, obd_off lov_off,
53 int stripeno, obd_off *obd_off);
55 struct lov_file_handles {
56 struct portals_handle lfh_handle;
57 atomic_t lfh_refcount;
58 struct list_head lfh_list;
60 struct obd_client_handle *lfh_och;
63 struct lov_lock_handles {
64 struct portals_handle llh_handle;
65 atomic_t llh_refcount;
67 struct lustre_handle llh_handles[0];
70 /* lov_file_handles helpers */
71 static void lov_lfh_addref(void *lfhp)
73 struct lov_file_handles *lfh = lfhp;
75 atomic_inc(&lfh->lfh_refcount);
76 CDEBUG(D_MALLOC, "GETting lfh %p : new refcount %d\n", lfh,
77 atomic_read(&lfh->lfh_refcount));
80 static struct lov_file_handles *lov_lfh_new(void)
82 struct lov_file_handles *lfh;
84 OBD_ALLOC(lfh, sizeof *lfh);
86 CERROR("out of memory\n");
90 atomic_set(&lfh->lfh_refcount, 2);
92 INIT_LIST_HEAD(&lfh->lfh_handle.h_link);
93 class_handle_hash(&lfh->lfh_handle, lov_lfh_addref);
98 static struct lov_file_handles *lov_handle2lfh(struct lustre_handle *handle)
101 LASSERT(handle != NULL);
102 RETURN(class_handle2object(handle->cookie));
105 static void lov_lfh_put(struct lov_file_handles *lfh)
107 CDEBUG(D_MALLOC, "PUTting lfh %p : new refcount %d\n", lfh,
108 atomic_read(&lfh->lfh_refcount) - 1);
109 LASSERT(atomic_read(&lfh->lfh_refcount) > 0 &&
110 atomic_read(&lfh->lfh_refcount) < 0x5a5a);
111 if (atomic_dec_and_test(&lfh->lfh_refcount)) {
112 LASSERT(list_empty(&lfh->lfh_handle.h_link));
113 OBD_FREE(lfh, sizeof *lfh);
117 static void lov_lfh_destroy(struct lov_file_handles *lfh)
119 class_handle_unhash(&lfh->lfh_handle);
123 static void lov_llh_addref(void *llhp)
125 struct lov_lock_handles *llh = llhp;
127 atomic_inc(&llh->llh_refcount);
128 CDEBUG(D_INFO, "GETting llh %p : new refcount %d\n", llh,
129 atomic_read(&llh->llh_refcount));
132 static struct lov_lock_handles *lov_llh_new(struct lov_stripe_md *lsm)
134 struct lov_lock_handles *llh;
136 OBD_ALLOC(llh, sizeof *llh +
137 sizeof(*llh->llh_handles) * lsm->lsm_stripe_count);
139 CERROR("out of memory\n");
142 atomic_set(&llh->llh_refcount, 2);
143 llh->llh_stripe_count = lsm->lsm_stripe_count;
144 INIT_LIST_HEAD(&llh->llh_handle.h_link);
145 class_handle_hash(&llh->llh_handle, lov_llh_addref);
149 static struct lov_lock_handles *lov_handle2llh(struct lustre_handle *handle)
152 LASSERT(handle != NULL);
153 RETURN(class_handle2object(handle->cookie));
156 static void lov_llh_put(struct lov_lock_handles *llh)
158 CDEBUG(D_INFO, "PUTting llh %p : new refcount %d\n", llh,
159 atomic_read(&llh->llh_refcount) - 1);
160 LASSERT(atomic_read(&llh->llh_refcount) > 0 &&
161 atomic_read(&llh->llh_refcount) < 0x5a5a);
162 if (atomic_dec_and_test(&llh->llh_refcount)) {
163 LASSERT(list_empty(&llh->llh_handle.h_link));
164 OBD_FREE(llh, sizeof *llh +
165 sizeof(*llh->llh_handles) * llh->llh_stripe_count);
169 static void lov_llh_destroy(struct lov_lock_handles *llh)
171 class_handle_unhash(&llh->llh_handle);
176 int lov_attach(struct obd_device *dev, obd_count len, void *data)
178 struct lprocfs_static_vars lvars;
179 struct proc_dir_entry *entry;
182 lprocfs_init_vars(lov, &lvars);
183 rc = lprocfs_obd_attach(dev, lvars.obd_vars);
187 entry = create_proc_entry("target_obd", 0444, dev->obd_proc_entry);
190 entry->proc_fops = &lov_proc_target_fops;
196 int lov_detach(struct obd_device *dev)
198 return lprocfs_obd_detach(dev);
201 static int lov_connect(struct lustre_handle *conn, struct obd_device *obd,
202 struct obd_uuid *cluuid)
204 struct ptlrpc_request *req = NULL;
205 struct lov_obd *lov = &obd->u.lov;
206 struct client_obd *mdc = &lov->mdcobd->u.cli;
207 struct lov_desc *desc = &lov->desc;
208 struct lov_desc *mdesc;
209 struct lov_tgt_desc *tgts;
210 struct obd_export *exp;
211 struct lustre_handle mdc_conn;
212 struct obd_uuid lov_mds_uuid = {"LOV_MDS_UUID"};
213 struct obd_uuid *uuids;
217 rc = class_connect(conn, obd, cluuid);
221 exp = class_conn2export(conn);
222 spin_lock_init(&exp->exp_lov_data.led_lock);
223 INIT_LIST_HEAD(&exp->exp_lov_data.led_open_head);
225 /* We don't want to actually do the underlying connections more than
226 * once, so keep track. */
228 if (lov->refcount > 1) {
229 class_export_put(exp);
233 /* retrieve LOV metadata from MDS */
234 rc = obd_connect(&mdc_conn, lov->mdcobd, &lov_mds_uuid);
236 CERROR("cannot connect to mdc: rc = %d\n", rc);
240 rc = mdc_getlovinfo(obd, &mdc_conn, &req);
241 rc2 = obd_disconnect(&mdc_conn, 0);
243 CERROR("cannot get lov info %d\n", rc);
248 CERROR("error disconnecting from MDS %d\n", rc2);
249 GOTO(out_req, rc = rc2);
252 /* mdc_getlovinfo() has checked and swabbed the reply. It has also
253 * done some simple checks (e.g. #uuids consistent with desc, uuid
254 * array fits in LOV_MAX_UUID_BUFFER_SIZE and all uuids are
255 * terminated), but I still need to verify it makes overall
257 mdesc = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*mdesc));
258 LASSERT(mdesc != NULL);
259 LASSERT_REPSWABBED(req, 0);
263 /* XXX We need a separate LOV 'service' UUID from the client device
264 * UUID so that we can mount more than once on a client */
265 if (!obd_uuid_equals(&obd->obd_uuid, &desc->ld_uuid)) {
266 CERROR("LOV desc: uuid %s not on mds device (%s)\n",
267 obd->obd_uuid.uuid, desc->ld_uuid.uuid);
268 GOTO(out_req, rc = -EINVAL);
271 /* Because of 64-bit divide/mod operations only work with a 32-bit
272 * divisor in a 32-bit kernel, we cannot support a stripe width
273 * of 4GB or larger on 32-bit CPUs.
275 if ((desc->ld_default_stripe_count ?
276 desc->ld_default_stripe_count : desc->ld_tgt_count) *
277 desc->ld_default_stripe_size > ~0UL) {
278 CERROR("LOV: stripe width "LPU64"x%u > %lu on 32-bit system\n",
279 desc->ld_default_stripe_size,
280 desc->ld_default_stripe_count ?
281 desc->ld_default_stripe_count : desc->ld_tgt_count,~0UL);
282 GOTO(out_req, rc = -EINVAL);
285 /* We know ld_tgt_count is reasonable (the array of UUIDS fits in
286 * the maximum buffer size, so we won't be making outrageous
287 * demands on memory here. */
288 lov->bufsize = sizeof(struct lov_tgt_desc) * desc->ld_tgt_count;
289 OBD_ALLOC(lov->tgts, lov->bufsize);
290 if (lov->tgts == NULL) {
291 CERROR("Out of memory\n");
292 GOTO(out_req, rc = -ENOMEM);
295 uuids = lustre_msg_buf(req->rq_repmsg, 1,
296 sizeof(*uuids) * desc->ld_tgt_count);
297 LASSERT(uuids != NULL);
298 LASSERT_REPSWABBED(req, 1);
300 for (i = 0, tgts = lov->tgts; i < desc->ld_tgt_count; i++, tgts++) {
301 struct obd_uuid *uuid = &tgts->uuid;
302 struct obd_device *tgt_obd;
303 struct obd_uuid lov_osc_uuid = { "LOV_OSC_UUID" };
305 /* NULL termination already checked */
308 tgt_obd = client_tgtuuid2obd(uuid);
311 CERROR("Target %s not attached\n", uuid->uuid);
312 GOTO(out_disc, rc = -EINVAL);
315 if (!tgt_obd->obd_set_up) {
316 CERROR("Target %s not set up\n", uuid->uuid);
317 GOTO(out_disc, rc = -EINVAL);
320 rc = obd_connect(&tgts->conn, tgt_obd, &lov_osc_uuid);
323 CERROR("Target %s connect error %d\n", uuid->uuid, rc);
327 rc = obd_iocontrol(IOC_OSC_REGISTER_LOV, &tgts->conn,
328 sizeof(struct obd_device *), obd, NULL);
330 CERROR("Target %s REGISTER_LOV error %d\n",
332 obd_disconnect(&tgts->conn, 0);
336 desc->ld_active_tgt_count++;
340 mdc->cl_max_mds_easize = obd_size_diskmd(conn, NULL);
341 mdc->cl_max_mds_cookiesize = desc->ld_tgt_count *
342 sizeof(struct llog_cookie);
343 ptlrpc_req_finished(req);
344 class_export_put(exp);
349 struct obd_uuid uuid;
351 --desc->ld_active_tgt_count;
353 /* save for CERROR below; (we know it's terminated) */
355 rc2 = obd_disconnect(&tgts->conn, 0);
357 CERROR("error: LOV target %s disconnect on OST idx %d: "
358 "rc = %d\n", uuid.uuid, i, rc2);
360 OBD_FREE(lov->tgts, lov->bufsize);
362 ptlrpc_req_finished (req);
364 class_export_put(exp);
365 class_disconnect(conn, 0);
369 static int lov_disconnect(struct lustre_handle *conn, int flags)
371 struct obd_device *obd = class_conn2obd(conn);
372 struct lov_obd *lov = &obd->u.lov;
373 struct obd_export *exp;
374 struct list_head *p, *n;
381 /* Only disconnect the underlying layers on the final disconnect. */
383 if (lov->refcount != 0)
386 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
387 if (obd->obd_no_recov) {
388 /* Pass it on to our clients.
389 * XXX This should be an argument to disconnect,
390 * XXX not a back-door flag on the OBD. Ah well.
392 struct obd_device *osc_obd =
393 class_conn2obd(&lov->tgts[i].conn);
394 osc_obd->obd_no_recov = 1;
396 rc = obd_disconnect(&lov->tgts[i].conn, flags);
398 if (lov->tgts[i].active) {
399 CERROR("Target %s disconnect error %d\n",
400 lov->tgts[i].uuid.uuid, rc);
404 if (lov->tgts[i].active) {
405 lov->desc.ld_active_tgt_count--;
406 lov->tgts[i].active = 0;
409 OBD_FREE(lov->tgts, lov->bufsize);
414 exp = class_conn2export(conn);
416 CERROR("export handle "LPU64" invalid! If you can reproduce, "
417 "please send a full debug log to phik\n", conn->cookie);
420 spin_lock(&exp->exp_lov_data.led_lock);
421 list_for_each_safe(p, n, &exp->exp_lov_data.led_open_head) {
422 /* XXX close these, instead of just discarding them? */
423 struct lov_file_handles *lfh;
424 lfh = list_entry(p, typeof(*lfh), lfh_list);
425 CERROR("discarding open LOV handle %p:"LPX64"\n",
426 lfh, lfh->lfh_handle.h_cookie);
427 list_del(&lfh->lfh_list);
428 OBD_FREE(lfh->lfh_och, lfh->lfh_count * FD_OSTDATA_SIZE);
429 lov_lfh_destroy(lfh);
432 spin_unlock(&exp->exp_lov_data.led_lock);
433 class_export_put(exp);
435 rc = class_disconnect(conn, 0);
441 * -EINVAL : UUID can't be found in the LOV's target list
442 * -ENOTCONN: The UUID is found, but the target connection is bad (!)
443 * -EBADF : The UUID is found, but the OBD is the wrong type (!)
445 static int lov_set_osc_active(struct lov_obd *lov, struct obd_uuid *uuid,
448 struct obd_device *obd;
449 struct lov_tgt_desc *tgt;
453 CDEBUG(D_INFO, "Searching in lov %p for uuid %s (activate=%d)\n",
454 lov, uuid->uuid, activate);
456 spin_lock(&lov->lov_lock);
457 for (i = 0, tgt = lov->tgts; i < lov->desc.ld_tgt_count; i++, tgt++) {
458 CDEBUG(D_INFO, "lov idx %d is %s conn "LPX64"\n",
459 i, tgt->uuid.uuid, tgt->conn.cookie);
460 if (strncmp(uuid->uuid, tgt->uuid.uuid, sizeof uuid->uuid) == 0)
464 if (i == lov->desc.ld_tgt_count)
465 GOTO(out, rc = -EINVAL);
467 obd = class_conn2obd(&tgt->conn);
469 /* This can happen if OST failure races with node shutdown */
470 GOTO(out, rc = -ENOTCONN);
473 CDEBUG(D_INFO, "Found OBD %s=%s device %d (%p) type %s at LOV idx %d\n",
474 obd->obd_name, obd->obd_uuid.uuid, obd->obd_minor, obd,
475 obd->obd_type->typ_name, i);
476 LASSERT(strcmp(obd->obd_type->typ_name, "osc") == 0);
478 if (tgt->active == activate) {
479 CDEBUG(D_INFO, "OBD %p already %sactive!\n", obd,
480 activate ? "" : "in");
484 CDEBUG(D_INFO, "Marking OBD %p %sactive\n", obd, activate ? "" : "in");
486 tgt->active = activate;
491 * if (file_handle uses this_osc)
492 * if (has_no_filehandle)
493 * open(file_handle, this_osc);
496 lov->desc.ld_active_tgt_count++;
499 * Should I invalidate filehandles that refer to this OSC, so
500 * that I reopen them during reactivation?
502 /* XXX disconnect from OSC? */
503 lov->desc.ld_active_tgt_count--;
506 #warning "FIXME: walk open files list for objects that need opening"
509 spin_unlock(&lov->lov_lock);
513 static int lov_setup(struct obd_device *obd, obd_count len, void *buf)
515 struct obd_ioctl_data *data = buf;
516 struct lov_obd *lov = &obd->u.lov;
520 if (data->ioc_inllen1 < 1) {
521 CERROR("LOV setup requires an MDC name\n");
525 spin_lock_init(&lov->lov_lock);
526 lov->mdcobd = class_name2obd(data->ioc_inlbuf1);
528 CERROR("LOV %s cannot locate MDC %s\n", obd->obd_uuid.uuid,
535 /* compute object size given "stripeno" and the ost size */
536 static obd_size lov_stripe_size(struct lov_stripe_md *lsm, obd_size ost_size,
539 unsigned long ssize = lsm->lsm_stripe_size;
540 unsigned long swidth = ssize * lsm->lsm_stripe_count;
541 unsigned long stripe_size;
547 /* do_div(a, b) returns a % b, and a = a / b */
548 stripe_size = do_div(ost_size, ssize);
551 lov_size = ost_size * swidth + stripeno * ssize + stripe_size;
553 lov_size = (ost_size - 1) * swidth + (stripeno + 1) * ssize;
558 static void lov_merge_attrs(struct obdo *tgt, struct obdo *src, obd_flag valid,
559 struct lov_stripe_md *lsm, int stripeno, int *set)
561 valid &= src->o_valid;
564 if (valid & OBD_MD_FLSIZE) {
565 /* this handles sparse files properly */
568 lov_size = lov_stripe_size(lsm, src->o_size, stripeno);
569 if (lov_size > tgt->o_size)
570 tgt->o_size = lov_size;
572 if (valid & OBD_MD_FLBLOCKS)
573 tgt->o_blocks += src->o_blocks;
574 if (valid & OBD_MD_FLBLKSZ)
575 tgt->o_blksize += src->o_blksize;
576 if (valid & OBD_MD_FLCTIME && tgt->o_ctime < src->o_ctime)
577 tgt->o_ctime = src->o_ctime;
578 if (valid & OBD_MD_FLMTIME && tgt->o_mtime < src->o_mtime)
579 tgt->o_mtime = src->o_mtime;
581 memcpy(tgt, src, sizeof(*tgt));
582 tgt->o_id = lsm->lsm_object_id;
583 if (valid & OBD_MD_FLSIZE)
584 tgt->o_size = lov_stripe_size(lsm,src->o_size,stripeno);
590 #define log2(n) ffz(~(n))
593 /* the LOV expects oa->o_id to be set to the LOV object id */
594 static int lov_create(struct lustre_handle *conn, struct obdo *src_oa,
595 struct lov_stripe_md **ea, struct obd_trans_info *oti)
597 struct obd_export *export = class_conn2export(conn);
599 struct lov_stripe_md *lsm;
600 struct lov_oinfo *loi = NULL;
601 struct obdo *tmp_oa, *ret_oa;
602 struct llog_cookie *cookies = NULL;
603 unsigned ost_count, ost_idx;
604 int set = 0, obj_alloc = 0, cookie_sent = 0, rc = 0, i;
612 lov = &export->exp_obd->u.lov;
614 if (!lov->desc.ld_active_tgt_count)
615 GOTO(out_exp, rc = -EIO);
617 ret_oa = obdo_alloc();
619 GOTO(out_exp, rc = -ENOMEM);
621 tmp_oa = obdo_alloc();
623 GOTO(out_oa, rc = -ENOMEM);
629 ost_count = lov_get_stripecnt(lov, 0);
631 /* If the MDS file was truncated up to some size, stripe over
632 * enough OSTs to allow the file to be created at that size.
634 if (src_oa->o_valid & OBD_MD_FLSIZE) {
635 stripes=((src_oa->o_size+LUSTRE_STRIPE_MAXBYTES)>>12)-1;
636 do_div(stripes, (__u32)(LUSTRE_STRIPE_MAXBYTES >> 12));
638 if (stripes > lov->desc.ld_active_tgt_count)
639 GOTO(out_exp, rc = -EFBIG);
640 if (stripes < ost_count)
645 rc = lov_alloc_memmd(&lsm, stripes);
652 ost_count = lov->desc.ld_tgt_count;
654 LASSERT(src_oa->o_valid & OBD_MD_FLID);
655 lsm->lsm_object_id = src_oa->o_id;
656 if (!lsm->lsm_stripe_size)
657 lsm->lsm_stripe_size = lov->desc.ld_default_stripe_size;
659 if (!*ea || lsm->lsm_stripe_offset >= ost_count) {
660 get_random_bytes(&ost_idx, 2);
661 ost_idx %= ost_count;
663 ost_idx = lsm->lsm_stripe_offset;
666 CDEBUG(D_INODE, "allocating %d subobjs for objid "LPX64" at idx %d\n",
667 lsm->lsm_stripe_count, lsm->lsm_object_id, ost_idx);
669 /* XXX LOV STACKING: need to figure out how many real OSCs */
670 if (oti && (src_oa->o_valid & OBD_MD_FLCOOKIE)) {
671 oti_alloc_cookies(oti, lsm->lsm_stripe_count);
672 if (!oti->oti_logcookies)
673 GOTO(out_cleanup, rc = -ENOMEM);
674 cookies = oti->oti_logcookies;
677 loi = lsm->lsm_oinfo;
678 for (i = 0; i < ost_count; i++, ost_idx = (ost_idx + 1) % ost_count) {
679 struct lov_stripe_md obj_md;
680 struct lov_stripe_md *obj_mdp = &obj_md;
683 if (lov->tgts[ost_idx].active == 0) {
684 CDEBUG(D_HA, "lov idx %d inactive\n", ost_idx);
688 /* create data objects with "parent" OA */
689 memcpy(tmp_oa, src_oa, sizeof(*tmp_oa));
691 /* XXX When we start creating objects on demand, we need to
692 * make sure that we always create the object on the
693 * stripe which holds the existing file size.
695 if (src_oa->o_valid & OBD_MD_FLSIZE) {
696 if (lov_stripe_offset(lsm, src_oa->o_size, i,
697 &tmp_oa->o_size) < 0 &&
701 CDEBUG(D_INODE, "stripe %d has size "LPU64"/"LPU64"\n",
702 i, tmp_oa->o_size, src_oa->o_size);
705 /* XXX: LOV STACKING: use real "obj_mdp" sub-data */
706 err = obd_create(&lov->tgts[ost_idx].conn, tmp_oa,&obj_mdp,oti);
708 if (lov->tgts[ost_idx].active) {
709 CERROR("error creating objid "LPX64" sub-object"
710 " on OST idx %d/%d: rc = %d\n",
711 src_oa->o_id, ost_idx,
712 lsm->lsm_stripe_count, err);
714 CERROR("obd_create returned invalid "
723 loi->loi_id = tmp_oa->o_id;
724 loi->loi_ost_idx = ost_idx;
725 CDEBUG(D_INODE, "objid "LPX64" has subobj "LPX64" at idx %d\n",
726 lsm->lsm_object_id, loi->loi_id, ost_idx);
729 lsm->lsm_stripe_offset = ost_idx;
730 lov_merge_attrs(ret_oa, tmp_oa, tmp_oa->o_valid, lsm,
732 loi->loi_dirty_ot = &loi->loi_dirty_ot_inline;
733 ot_init(loi->loi_dirty_ot);
736 ++oti->oti_logcookies;
737 if (tmp_oa->o_valid & OBD_MD_FLCOOKIE)
742 /* If we have allocated enough objects, we are OK */
743 if (obj_alloc == lsm->lsm_stripe_count)
744 GOTO(out_done, rc = 0);
747 if (obj_alloc == 0) {
750 GOTO(out_cleanup, rc);
753 /* If we were passed specific striping params, then a failure to
754 * meet those requirements is an error, since we can't reallocate
755 * that memory (it might be part of a larger array or something).
757 * We can only get here if lsm_stripe_count was originally > 1.
760 CERROR("can't lstripe objid "LPX64": have %u want %u, rc %d\n",
761 lsm->lsm_object_id, obj_alloc, lsm->lsm_stripe_count,rc);
764 GOTO(out_cleanup, rc);
766 struct lov_stripe_md *lsm_new;
767 /* XXX LOV STACKING call into osc for sizes */
768 unsigned oldsize, newsize;
770 if (oti && cookies && cookie_sent) {
771 oldsize = lsm->lsm_stripe_count * sizeof(*cookies);
772 newsize = obj_alloc * sizeof(*cookies);
774 oti_alloc_cookies(oti, obj_alloc);
775 if (oti->oti_logcookies) {
776 memcpy(oti->oti_logcookies, cookies, newsize);
777 OBD_FREE(cookies, oldsize);
778 cookies = oti->oti_logcookies;
780 CWARN("'leaking' %d bytes\n", oldsize-newsize);
784 CERROR("reallocating LSM for objid "LPX64": old %u new %u\n",
785 lsm->lsm_object_id, lsm->lsm_stripe_count, obj_alloc);
786 oldsize = lov_stripe_md_size(lsm->lsm_stripe_count);
787 newsize = lov_stripe_md_size(obj_alloc);
788 OBD_ALLOC(lsm_new, newsize);
789 if (lsm_new != NULL) {
790 memcpy(lsm_new, lsm, newsize);
791 lsm_new->lsm_stripe_count = obj_alloc;
792 OBD_FREE(lsm, newsize);
795 CWARN("'leaking' %d bytes\n", oldsize - newsize);
802 if (src_oa->o_valid & OBD_MD_FLSIZE &&
803 ret_oa->o_size != src_oa->o_size) {
804 CERROR("original size "LPU64" isn't new object size "LPU64"\n",
805 src_oa->o_size, ret_oa->o_size);
808 ret_oa->o_id = src_oa->o_id;
809 memcpy(src_oa, ret_oa, sizeof(*src_oa));
815 if (oti && cookies) {
816 oti->oti_logcookies = cookies;
818 oti_free_cookies(oti);
819 src_oa->o_valid &= ~OBD_MD_FLCOOKIE;
821 src_oa->o_valid |= OBD_MD_FLCOOKIE;
825 class_export_put(export);
829 while (obj_alloc-- > 0) {
833 /* destroy already created objects here */
834 memcpy(tmp_oa, src_oa, sizeof(*tmp_oa));
835 tmp_oa->o_id = loi->loi_id;
837 if (oti && cookie_sent) {
838 err = obd_log_cancel(&lov->tgts[loi->loi_ost_idx].conn,
839 NULL, 1, --oti->oti_logcookies,
840 OBD_LLOG_FL_SENDNOW);
842 CERROR("Failed to cancel objid "LPX64" subobj "
843 LPX64" cookie on OST idx %d: rc = %d\n",
844 src_oa->o_id, loi->loi_id,
845 loi->loi_ost_idx, err);
848 err = obd_destroy(&lov->tgts[loi->loi_ost_idx].conn, tmp_oa,
851 CERROR("Failed to uncreate objid "LPX64" subobj "LPX64
852 " on OST idx %d: rc = %d\n", src_oa->o_id,
853 loi->loi_id, loi->loi_ost_idx, err);
856 obd_free_memmd(conn, &lsm);
860 #define lsm_bad_magic(LSMP) \
862 struct lov_stripe_md *_lsm__ = (LSMP); \
865 CERROR("LOV requires striping ea\n"); \
867 } else if (_lsm__->lsm_magic != LOV_MAGIC) { \
868 CERROR("LOV striping magic bad %#x != %#x\n", \
869 _lsm__->lsm_magic, LOV_MAGIC); \
875 static int lov_destroy(struct lustre_handle *conn, struct obdo *oa,
876 struct lov_stripe_md *lsm, struct obd_trans_info *oti)
879 struct obd_export *export = class_conn2export(conn);
881 struct lov_oinfo *loi;
882 struct lov_file_handles *lfh = NULL;
886 if (lsm_bad_magic(lsm))
887 GOTO(out, rc = -EINVAL);
889 if (!export || !export->exp_obd)
890 GOTO(out, rc = -ENODEV);
892 if (oa->o_valid & OBD_MD_FLHANDLE)
893 lfh = lov_handle2lfh(obdo_handle(oa));
895 lov = &export->exp_obd->u.lov;
896 for (i = 0,loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++,loi++) {
898 if (lov->tgts[loi->loi_ost_idx].active == 0) {
899 CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
900 /* Orphan clean up will (someday) fix this up. */
904 memcpy(&tmp, oa, sizeof(tmp));
905 tmp.o_id = loi->loi_id;
907 memcpy(obdo_handle(&tmp), &lfh->lfh_och[i].och_fh,
908 sizeof(lfh->lfh_och[i].och_fh));
910 tmp.o_valid &= ~OBD_MD_FLHANDLE;
911 err = obd_destroy(&lov->tgts[loi->loi_ost_idx].conn, &tmp,
913 if (err && lov->tgts[loi->loi_ost_idx].active) {
914 CERROR("error: destroying objid "LPX64" subobj "
915 LPX64" on OST idx %d: rc = %d\n",
916 oa->o_id, loi->loi_id, loi->loi_ost_idx, err);
925 class_export_put(export);
929 static int lov_getattr(struct lustre_handle *conn, struct obdo *oa,
930 struct lov_stripe_md *lsm)
933 struct obd_export *export = class_conn2export(conn);
935 struct lov_oinfo *loi;
936 struct lov_file_handles *lfh = NULL;
937 int i, rc = 0, set = 0;
940 if (lsm_bad_magic(lsm))
941 GOTO(out, rc = -EINVAL);
943 if (!export || !export->exp_obd)
944 GOTO(out, rc = -ENODEV);
946 lov = &export->exp_obd->u.lov;
948 if (oa->o_valid & OBD_MD_FLHANDLE)
949 lfh = lov_handle2lfh(obdo_handle(oa));
951 CDEBUG(D_INFO, "objid "LPX64": %ux%u byte stripes\n",
952 lsm->lsm_object_id, lsm->lsm_stripe_count, lsm->lsm_stripe_size);
953 for (i = 0,loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++,loi++) {
956 if (lov->tgts[loi->loi_ost_idx].active == 0) {
957 CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
961 CDEBUG(D_INFO, "objid "LPX64"[%d] has subobj "LPX64" at idx "
962 "%u\n", oa->o_id, i, loi->loi_id, loi->loi_ost_idx);
963 /* create data objects with "parent" OA */
964 memcpy(&tmp, oa, sizeof(tmp));
965 tmp.o_id = loi->loi_id;
967 memcpy(obdo_handle(&tmp), &lfh->lfh_och[i].och_fh,
968 sizeof(lfh->lfh_och[i].och_fh));
970 tmp.o_valid &= ~OBD_MD_FLHANDLE;
972 err = obd_getattr(&lov->tgts[loi->loi_ost_idx].conn, &tmp,NULL);
974 if (lov->tgts[loi->loi_ost_idx].active) {
975 CERROR("error: getattr objid "LPX64" subobj "
976 LPX64" on OST idx %d: rc = %d\n",
977 oa->o_id, loi->loi_id, loi->loi_ost_idx,
982 lov_merge_attrs(oa, &tmp, tmp.o_valid, lsm, i, &set);
991 class_export_put(export);
995 static int lov_getattr_interpret(struct ptlrpc_request_set *rqset, void *data,
998 struct lov_getattr_async_args *aa = data;
999 struct lov_stripe_md *lsm = aa->aa_lsm;
1000 struct obdo *oa = aa->aa_oa;
1001 struct obdo *obdos = aa->aa_obdos;
1002 struct lov_oinfo *loi;
1008 /* NB all stripe requests succeeded to get here */
1010 for (i = 0, loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count;
1012 if (obdos[i].o_valid == 0) /* inactive stripe */
1015 lov_merge_attrs(oa, &obdos[i], obdos[i].o_valid, lsm,
1020 CERROR ("No stripes had valid attrs\n");
1025 OBD_FREE (obdos, lsm->lsm_stripe_count * sizeof (*obdos));
1029 static int lov_getattr_async (struct lustre_handle *conn, struct obdo *oa,
1030 struct lov_stripe_md *lsm,
1031 struct ptlrpc_request_set *rqset)
1034 struct obd_export *export = class_conn2export(conn);
1035 struct lov_obd *lov;
1036 struct lov_oinfo *loi;
1037 struct lov_file_handles *lfh = NULL;
1038 struct lov_getattr_async_args *aa;
1045 CERROR("LOV requires striping ea\n");
1046 GOTO(out, rc = -EINVAL);
1049 if (lsm->lsm_magic != LOV_MAGIC) {
1050 CERROR("LOV striping magic bad %#x != %#x\n",
1051 lsm->lsm_magic, LOV_MAGIC);
1052 GOTO(out, rc = -EINVAL);
1055 if (!export || !export->exp_obd)
1056 GOTO(out, rc = -ENODEV);
1058 lov = &export->exp_obd->u.lov;
1060 OBD_ALLOC (obdos, lsm->lsm_stripe_count * sizeof (*obdos));
1062 GOTO (out, rc = -ENOMEM);
1064 if (oa->o_valid & OBD_MD_FLHANDLE)
1065 lfh = lov_handle2lfh(obdo_handle(oa));
1067 CDEBUG(D_INFO, "objid "LPX64": %ux%u byte stripes\n",
1068 lsm->lsm_object_id, lsm->lsm_stripe_count, lsm->lsm_stripe_size);
1069 for (i = 0,loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++,loi++) {
1072 if (lov->tgts[loi->loi_ost_idx].active == 0) {
1073 CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
1074 /* leaves obdos[i].obd_valid unset */
1078 CDEBUG(D_INFO, "objid "LPX64"[%d] has subobj "LPX64" at idx "
1079 "%u\n", oa->o_id, i, loi->loi_id, loi->loi_ost_idx);
1080 /* create data objects with "parent" OA */
1081 memcpy(&obdos[i], oa, sizeof(obdos[i]));
1082 obdos[i].o_id = loi->loi_id;
1084 memcpy(obdo_handle(&obdos[i]), &lfh->lfh_och[i].och_fh,
1085 sizeof(lfh->lfh_och[i].och_fh));
1087 obdos[i].o_valid &= ~OBD_MD_FLHANDLE;
1089 err = obd_getattr_async (&lov->tgts[loi->loi_ost_idx].conn,
1090 &obdos[i], NULL, rqset);
1092 CERROR("error: getattr objid "LPX64" subobj "
1093 LPX64" on OST idx %d: rc = %d\n",
1094 oa->o_id, loi->loi_id, loi->loi_ost_idx,
1096 GOTO(out_obdos, rc = err);
1101 GOTO (out_obdos, rc = -EIO);
1103 LASSERT (rqset->set_interpret == NULL);
1104 rqset->set_interpret = lov_getattr_interpret;
1105 LASSERT (sizeof (rqset->set_args) >= sizeof (*aa));
1106 aa = (struct lov_getattr_async_args *)&rqset->set_args;
1109 aa->aa_obdos = obdos;
1113 OBD_FREE (obdos, lsm->lsm_stripe_count * sizeof (*obdos));
1117 class_export_put(export);
1121 static int lov_setattr(struct lustre_handle *conn, struct obdo *src_oa,
1122 struct lov_stripe_md *lsm, struct obd_trans_info *oti)
1124 struct obdo *tmp_oa, *ret_oa;
1125 struct obd_export *export = class_conn2export(conn);
1126 struct lov_obd *lov;
1127 struct lov_oinfo *loi;
1128 struct lov_file_handles *lfh = NULL;
1129 int rc = 0, i, set = 0;
1132 if (lsm_bad_magic(lsm))
1133 GOTO(out, rc = -EINVAL);
1135 if (!export || !export->exp_obd)
1136 GOTO(out, rc = -ENODEV);
1138 /* for now, we only expect time updates here */
1139 LASSERT(!(src_oa->o_valid & ~(OBD_MD_FLID|OBD_MD_FLTYPE|OBD_MD_FLMODE|
1140 OBD_MD_FLATIME | OBD_MD_FLMTIME |
1142 ret_oa = obdo_alloc();
1144 GOTO(out, rc = -ENOMEM);
1146 tmp_oa = obdo_alloc();
1148 GOTO(out_oa, rc = -ENOMEM);
1150 lov = &export->exp_obd->u.lov;
1151 for (i = 0,loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++,loi++) {
1154 if (lov->tgts[loi->loi_ost_idx].active == 0) {
1155 CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
1159 memcpy(tmp_oa, src_oa, sizeof(*tmp_oa));
1162 memcpy(obdo_handle(tmp_oa), &lfh->lfh_och[i].och_fh,
1163 sizeof(lfh->lfh_och[i].och_fh));
1165 tmp_oa->o_valid &= ~OBD_MD_FLHANDLE;
1167 tmp_oa->o_id = loi->loi_id;
1169 err = obd_setattr(&lov->tgts[loi->loi_ost_idx].conn, tmp_oa,
1172 if (lov->tgts[loi->loi_ost_idx].active) {
1173 CERROR("error: setattr objid "LPX64" subobj "
1174 LPX64" on OST idx %d: rc = %d\n",
1175 src_oa->o_id, loi->loi_id,
1176 loi->loi_ost_idx, err);
1183 lov_merge_attrs(ret_oa, tmp_oa, tmp_oa->o_valid, lsm, i, &set);
1190 ret_oa->o_id = src_oa->o_id;
1191 memcpy(src_oa, ret_oa, sizeof(*src_oa));
1198 class_export_put(export);
1202 static int lov_open(struct lustre_handle *conn, struct obdo *src_oa,
1203 struct lov_stripe_md *lsm, struct obd_trans_info *oti,
1204 struct obd_client_handle *och)
1206 struct obdo *tmp_oa, *ret_oa;
1207 struct obd_export *export = class_conn2export(conn);
1208 struct lov_obd *lov;
1209 struct lov_oinfo *loi;
1210 struct lov_file_handles *lfh = NULL;
1211 int set = 0, rc = 0, i;
1213 LASSERT(och != NULL);
1215 if (lsm_bad_magic(lsm))
1216 GOTO(out_exp, rc = -EINVAL);
1218 if (!export || !export->exp_obd)
1219 GOTO(out_exp, rc = -ENODEV);
1221 ret_oa = obdo_alloc();
1223 GOTO(out_exp, rc = -ENOMEM);
1225 tmp_oa = obdo_alloc();
1227 GOTO(out_oa, rc = -ENOMEM);
1229 lfh = lov_lfh_new();
1231 GOTO(out_tmp, rc = -ENOMEM);
1232 OBD_ALLOC(lfh->lfh_och, lsm->lsm_stripe_count * sizeof(*och));
1234 GOTO(out_lfh, rc = -ENOMEM);
1236 lov = &export->exp_obd->u.lov;
1238 src_oa->o_blocks = 0;
1239 for (i = 0,loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++,loi++) {
1240 if (lov->tgts[loi->loi_ost_idx].active == 0) {
1241 CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
1245 /* create data objects with "parent" OA */
1246 memcpy(tmp_oa, src_oa, sizeof(*tmp_oa));
1247 tmp_oa->o_id = loi->loi_id;
1249 rc = obd_open(&lov->tgts[loi->loi_ost_idx].conn, tmp_oa,
1250 NULL, NULL, &lfh->lfh_och[i]);
1252 if (!lov->tgts[loi->loi_ost_idx].active) {
1256 CERROR("error: open objid "LPX64" subobj "LPX64
1257 " on OST idx %d: rc = %d\n",
1258 src_oa->o_id, lsm->lsm_oinfo[i].loi_id,
1259 loi->loi_ost_idx, rc);
1263 lov_merge_attrs(ret_oa, tmp_oa, tmp_oa->o_valid, lsm, i, &set);
1266 lfh->lfh_count = lsm->lsm_stripe_count;
1267 och->och_fh.cookie = lfh->lfh_handle.h_cookie;
1268 obdo_handle(ret_oa)->cookie = lfh->lfh_handle.h_cookie;
1269 ret_oa->o_valid |= OBD_MD_FLHANDLE;
1270 ret_oa->o_id = src_oa->o_id;
1271 memcpy(src_oa, ret_oa, sizeof(*src_oa));
1273 /* lfh refcount transfers to list */
1274 spin_lock(&export->exp_lov_data.led_lock);
1275 list_add(&lfh->lfh_list, &export->exp_lov_data.led_open_head);
1276 spin_unlock(&export->exp_lov_data.led_lock);
1284 class_export_put(export);
1288 for (i--, loi = &lsm->lsm_oinfo[i]; i >= 0; i--, loi--) {
1291 if (lov->tgts[loi->loi_ost_idx].active == 0)
1294 memcpy(tmp_oa, src_oa, sizeof(*tmp_oa));
1295 tmp_oa->o_id = loi->loi_id;
1296 memcpy(obdo_handle(tmp_oa), &lfh->lfh_och[i], FD_OSTDATA_SIZE);
1298 err = obd_close(&lov->tgts[loi->loi_ost_idx].conn, tmp_oa,
1300 if (err && lov->tgts[loi->loi_ost_idx].active) {
1301 CERROR("error: closing objid "LPX64" subobj "LPX64
1302 " on OST idx %d after open error: rc=%d\n",
1303 src_oa->o_id, loi->loi_id, loi->loi_ost_idx,err);
1307 OBD_FREE(lfh->lfh_och, lsm->lsm_stripe_count * FD_OSTDATA_SIZE);
1309 lov_lfh_destroy(lfh);
1314 static int lov_close(struct lustre_handle *conn, struct obdo *oa,
1315 struct lov_stripe_md *lsm, struct obd_trans_info *oti)
1318 struct obd_export *export = class_conn2export(conn);
1319 struct lov_obd *lov;
1320 struct lov_oinfo *loi;
1321 struct lov_file_handles *lfh = NULL;
1325 if (lsm_bad_magic(lsm))
1326 GOTO(out, rc = -EINVAL);
1328 if (!export || !export->exp_obd)
1329 GOTO(out, rc = -ENODEV);
1331 if (oa->o_valid & OBD_MD_FLHANDLE)
1332 lfh = lov_handle2lfh(obdo_handle(oa));
1336 lov = &export->exp_obd->u.lov;
1337 for (i = 0,loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++,loi++) {
1340 /* create data objects with "parent" OA */
1341 memcpy(&tmp, oa, sizeof(tmp));
1342 tmp.o_id = loi->loi_id;
1344 memcpy(obdo_handle(&tmp), &lfh->lfh_och[i],
1347 tmp.o_valid &= ~OBD_MD_FLHANDLE;
1349 err = obd_close(&lov->tgts[loi->loi_ost_idx].conn, &tmp,
1352 if (lov->tgts[loi->loi_ost_idx].active) {
1353 CERROR("error: close objid "LPX64" subobj "LPX64
1354 " on OST idx %d: rc = %d\n", oa->o_id,
1355 loi->loi_id, loi->loi_ost_idx, err);
1362 spin_lock(&export->exp_lov_data.led_lock);
1363 list_del(&lfh->lfh_list);
1364 spin_unlock(&export->exp_lov_data.led_lock);
1365 lov_lfh_put(lfh); /* drop the reference owned by the list */
1367 OBD_FREE(lfh->lfh_och, lsm->lsm_stripe_count * FD_OSTDATA_SIZE);
1368 lov_lfh_destroy(lfh);
1369 LASSERT(atomic_read(&lfh->lfh_refcount) == 1);
1370 lov_lfh_put(lfh); /* balance handle2lfh above */
1375 class_export_put(export);
1379 /* we have an offset in file backed by an lov and want to find out where
1380 * that offset lands in our given stripe of the file. for the easy
1381 * case where the offset is within the stripe, we just have to scale the
1382 * offset down to make it relative to the stripe instead of the lov.
1384 * the harder case is what to do when the offset doesn't intersect the
1385 * stripe. callers will want start offsets clamped ahead to the start
1386 * of the nearest stripe in the file. end offsets similarly clamped to the
1387 * nearest ending byte of a stripe in the file:
1389 * all this function does is move offsets to the nearest region of the
1390 * stripe, and it does its work "mod" the full length of all the stripes.
1391 * consider a file with 3 stripes:
1394 * ---------------------------------------------------------------------
1395 * | 0 | 1 | 2 | 0 | 1 | 2 |
1396 * ---------------------------------------------------------------------
1398 * to find stripe 1's offsets for S and E, it divides by the full stripe
1399 * width and does its math in the context of a single set of stripes:
1402 * -----------------------------------
1404 * -----------------------------------
1406 * it'll notice that E is outside stripe 1 and clamp it to the end of the
1407 * stripe, then multiply it back out by lov_off to give the real offsets in
1411 * ---------------------------------------------------------------------
1412 * | 1 | 1 | 1 | 1 | 1 | 1 |
1413 * ---------------------------------------------------------------------
1415 * it would have done similarly and pulled S forward to the start of a 1
1416 * stripe if, say, S had landed in a 0 stripe.
1418 * this rounding isn't always correct. consider an E lov offset that lands
1419 * on a 0 stripe, the "mod stripe width" math will pull it forward to the
1420 * start of a 1 stripe, when in fact it wanted to be rounded back to the end
1421 * of a previous 1 stripe. this logic is handled by callers and this is why:
1423 * this function returns < 0 when the offset was "before" the stripe and
1424 * was moved forward to the start of the stripe in question; 0 when it
1425 * falls in the stripe and no shifting was done; > 0 when the offset
1426 * was outside the stripe and was pulled back to its final byte. */
1427 static int lov_stripe_offset(struct lov_stripe_md *lsm, obd_off lov_off,
1428 int stripeno, obd_off *obd_off)
1430 unsigned long ssize = lsm->lsm_stripe_size;
1431 unsigned long swidth = ssize * lsm->lsm_stripe_count;
1432 unsigned long stripe_off, this_stripe;
1435 if (lov_off == OBD_OBJECT_EOF) {
1436 *obd_off = OBD_OBJECT_EOF;
1440 /* do_div(a, b) returns a % b, and a = a / b */
1441 stripe_off = do_div(lov_off, swidth);
1443 this_stripe = stripeno * ssize;
1444 if (stripe_off < this_stripe) {
1448 stripe_off -= this_stripe;
1450 if (stripe_off >= ssize) {
1456 *obd_off = lov_off * ssize + stripe_off;
1460 /* given an extent in an lov and a stripe, calculate the extent of the stripe
1461 * that is contained within the lov extent. this returns true if the given
1462 * stripe does intersect with the lov extent. */
1463 static int lov_stripe_intersects(struct lov_stripe_md *lsm, int stripeno,
1464 obd_off start, obd_off end,
1465 obd_off *obd_start, obd_off *obd_end)
1467 int start_side, end_side;
1469 start_side = lov_stripe_offset(lsm, start, stripeno, obd_start);
1470 end_side = lov_stripe_offset(lsm, end, stripeno, obd_end);
1472 CDEBUG(D_INODE, "["LPU64"->"LPU64"] -> [(%d) "LPU64"->"LPU64" (%d)]\n",
1473 start, end, start_side, *obd_start, *obd_end, end_side);
1475 /* this stripe doesn't intersect the file extent when neither
1476 * start or the end intersected the stripe and obd_start and
1477 * obd_end got rounded up to the save value. */
1478 if (start_side != 0 && end_side != 0 && *obd_start == *obd_end)
1481 /* as mentioned in the lov_stripe_offset commentary, end
1482 * might have been shifted in the wrong direction. This
1483 * happens when an end offset is before the stripe when viewed
1484 * through the "mod stripe size" math. we detect it being shifted
1485 * in the wrong direction and touch it up.
1486 * interestingly, this can't underflow since end must be > start
1487 * if we passed through the previous check.
1488 * (should we assert for that somewhere?) */
1495 /* compute which stripe number "lov_off" will be written into */
1496 static int lov_stripe_number(struct lov_stripe_md *lsm, obd_off lov_off)
1498 unsigned long ssize = lsm->lsm_stripe_size;
1499 unsigned long swidth = ssize * lsm->lsm_stripe_count;
1500 unsigned long stripe_off;
1502 stripe_off = do_div(lov_off, swidth);
1504 return stripe_off / ssize;
1507 /* FIXME: maybe we'll just make one node the authoritative attribute node, then
1508 * we can send this 'punch' to just the authoritative node and the nodes
1509 * that the punch will affect. */
1510 static int lov_punch(struct lustre_handle *conn, struct obdo *oa,
1511 struct lov_stripe_md *lsm,
1512 obd_off start, obd_off end, struct obd_trans_info *oti)
1515 struct obd_export *export = class_conn2export(conn);
1516 struct lov_obd *lov;
1517 struct lov_oinfo *loi;
1518 struct lov_file_handles *lfh = NULL;
1522 if (lsm_bad_magic(lsm))
1523 GOTO(out, rc = -EINVAL);
1525 if (!export || !export->exp_obd)
1526 GOTO(out, rc = -ENODEV);
1528 if (oa->o_valid & OBD_MD_FLHANDLE)
1529 lfh = lov_handle2lfh(obdo_handle(oa));
1531 lov = &export->exp_obd->u.lov;
1532 for (i = 0,loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++,loi++) {
1533 obd_off starti, endi;
1536 if (lov->tgts[loi->loi_ost_idx].active == 0) {
1537 CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
1541 if (!lov_stripe_intersects(lsm, i, start, end, &starti, &endi))
1544 /* create data objects with "parent" OA */
1545 memcpy(&tmp, oa, sizeof(tmp));
1546 tmp.o_id = loi->loi_id;
1548 memcpy(obdo_handle(&tmp), &lfh->lfh_och[i].och_fh,
1549 sizeof(lfh->lfh_och[i].och_fh));
1551 tmp.o_valid &= ~OBD_MD_FLHANDLE;
1553 err = obd_punch(&lov->tgts[loi->loi_ost_idx].conn, &tmp, NULL,
1554 starti, endi, NULL);
1556 if (lov->tgts[loi->loi_ost_idx].active) {
1557 CERROR("error: punch objid "LPX64" subobj "LPX64
1558 " on OST idx %d: rc = %d\n", oa->o_id,
1559 loi->loi_id, loi->loi_ost_idx, err);
1569 class_export_put(export);
1573 static int lov_brw_check(struct lov_obd *lov, struct lov_stripe_md *lsm,
1574 obd_count oa_bufs, struct brw_page *pga)
1578 /* The caller just wants to know if there's a chance that this
1579 * I/O can succeed */
1580 for (i = 0; i < oa_bufs; i++) {
1581 int stripe = lov_stripe_number(lsm, pga[i].off);
1582 int ost = lsm->lsm_oinfo[stripe].loi_ost_idx;
1583 struct ldlm_extent ext, subext;
1584 ext.start = pga[i].off;
1585 ext.start = pga[i].off + pga[i].count;
1587 if (!lov_stripe_intersects(lsm, i, ext.start, ext.end,
1588 &subext.start, &subext.end))
1591 if (lov->tgts[ost].active == 0) {
1592 CDEBUG(D_HA, "lov idx %d inactive\n", ost);
1599 static int lov_brw(int cmd, struct lustre_handle *conn, struct obdo *src_oa,
1600 struct lov_stripe_md *lsm, obd_count oa_bufs,
1601 struct brw_page *pga, struct obd_trans_info *oti)
1607 struct lov_stripe_md lsm;
1609 } *stripeinfo, *si, *si_last;
1610 struct obd_export *export = class_conn2export(conn);
1611 struct obdo *ret_oa = NULL, *tmp_oa = NULL;
1612 struct lov_file_handles *lfh = NULL;
1613 struct lov_obd *lov;
1614 struct brw_page *ioarr;
1615 struct lov_oinfo *loi;
1616 int rc = 0, i, *where, stripe_count = lsm->lsm_stripe_count, set = 0;
1619 if (lsm_bad_magic(lsm))
1620 GOTO(out_exp, rc = -EINVAL);
1622 lov = &export->exp_obd->u.lov;
1624 if (cmd == OBD_BRW_CHECK) {
1625 rc = lov_brw_check(lov, lsm, oa_bufs, pga);
1629 OBD_ALLOC(stripeinfo, stripe_count * sizeof(*stripeinfo));
1631 GOTO(out_exp, rc = -ENOMEM);
1633 OBD_ALLOC(where, sizeof(*where) * oa_bufs);
1635 GOTO(out_sinfo, rc = -ENOMEM);
1637 OBD_ALLOC(ioarr, sizeof(*ioarr) * oa_bufs);
1639 GOTO(out_where, rc = -ENOMEM);
1642 ret_oa = obdo_alloc();
1644 GOTO(out_ioarr, rc = -ENOMEM);
1646 tmp_oa = obdo_alloc();
1648 GOTO(out_oa, rc = -ENOMEM);
1650 if (src_oa->o_valid & OBD_MD_FLHANDLE)
1651 lfh = lov_handle2lfh(obdo_handle(src_oa));
1653 src_oa->o_valid &= ~OBD_MD_FLHANDLE;
1656 for (i = 0; i < oa_bufs; i++) {
1657 where[i] = lov_stripe_number(lsm, pga[i].off);
1658 stripeinfo[where[i]].bufct++;
1661 for (i = 0, loi = lsm->lsm_oinfo, si_last = si = stripeinfo;
1662 i < stripe_count; i++, loi++, si_last = si, si++) {
1664 si->index = si_last->index + si_last->bufct;
1665 si->lsm.lsm_object_id = loi->loi_id;
1666 si->ost_idx = loi->loi_ost_idx;
1669 for (i = 0; i < oa_bufs; i++) {
1670 int which = where[i];
1673 shift = stripeinfo[which].index + stripeinfo[which].subcount;
1674 LASSERT(shift < oa_bufs);
1675 ioarr[shift] = pga[i];
1676 lov_stripe_offset(lsm, pga[i].off, which, &ioarr[shift].off);
1677 stripeinfo[which].subcount++;
1680 for (i = 0, si = stripeinfo; i < stripe_count; i++, si++) {
1681 int shift = si->index;
1683 if (lov->tgts[si->ost_idx].active == 0) {
1684 CDEBUG(D_HA, "lov idx %d inactive\n", si->ost_idx);
1685 GOTO(out_oa, rc = -EIO);
1689 LASSERT(shift < oa_bufs);
1691 memcpy(tmp_oa, src_oa, sizeof(*tmp_oa));
1693 memcpy(obdo_handle(tmp_oa),
1694 &lfh->lfh_och[i].och_fh,
1695 sizeof(lfh->lfh_och[i].och_fh));
1698 tmp_oa->o_id = si->lsm.lsm_object_id;
1699 rc = obd_brw(cmd, &lov->tgts[si->ost_idx].conn, tmp_oa,
1700 &si->lsm, si->bufct, &ioarr[shift],
1703 GOTO(out_ioarr, rc);
1705 lov_merge_attrs(ret_oa, tmp_oa, tmp_oa->o_valid, lsm,
1710 ret_oa->o_id = src_oa->o_id;
1711 memcpy(src_oa, ret_oa, sizeof(*src_oa));
1720 OBD_FREE(ioarr, sizeof(*ioarr) * oa_bufs);
1722 OBD_FREE(where, sizeof(*where) * oa_bufs);
1726 OBD_FREE(stripeinfo, stripe_count * sizeof(*stripeinfo));
1728 class_export_put(export);
1732 static int lov_brw_interpret(struct ptlrpc_request_set *rqset,
1733 struct lov_brw_async_args *aa, int rc)
1735 struct lov_stripe_md *lsm = aa->aa_lsm;
1736 obd_count oa_bufs = aa->aa_oa_bufs;
1737 struct obdo *oa = aa->aa_oa;
1738 struct obdo *obdos = aa->aa_obdos;
1739 struct brw_page *ioarr = aa->aa_ioarr;
1740 struct lov_oinfo *loi;
1745 /* NB all stripe requests succeeded to get here */
1747 for (i = 0, loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count;
1749 if (obdos[i].o_valid == 0) /* inactive stripe */
1752 lov_merge_attrs(oa, &obdos[i], obdos[i].o_valid, lsm,
1757 CERROR("No stripes had valid attrs\n");
1761 oa->o_id = lsm->lsm_object_id;
1763 OBD_FREE(obdos, lsm->lsm_stripe_count * sizeof(*obdos));
1764 OBD_FREE(ioarr, sizeof(*ioarr) * oa_bufs);
1768 static int lov_brw_async(int cmd, struct lustre_handle *conn, struct obdo *oa,
1769 struct lov_stripe_md *lsm, obd_count oa_bufs,
1770 struct brw_page *pga, struct ptlrpc_request_set *set,
1771 struct obd_trans_info *oti)
1777 struct lov_stripe_md lsm;
1779 } *stripeinfo, *si, *si_last;
1780 struct obd_export *export = class_conn2export(conn);
1781 struct lov_obd *lov;
1782 struct lov_file_handles *lfh = NULL;
1783 struct brw_page *ioarr;
1784 struct obdo *obdos = NULL;
1785 struct lov_oinfo *loi;
1786 struct lov_brw_async_args *aa;
1787 int rc = 0, i, *where, stripe_count = lsm->lsm_stripe_count;
1790 if (lsm_bad_magic(lsm))
1791 GOTO(out_exp, rc = -EINVAL);
1793 lov = &export->exp_obd->u.lov;
1795 if (cmd == OBD_BRW_CHECK) {
1796 rc = lov_brw_check(lov, lsm, oa_bufs, pga);
1800 OBD_ALLOC(stripeinfo, stripe_count * sizeof(*stripeinfo));
1802 GOTO(out_exp, rc = -ENOMEM);
1804 OBD_ALLOC(where, sizeof(*where) * oa_bufs);
1806 GOTO(out_sinfo, rc = -ENOMEM);
1809 OBD_ALLOC(obdos, sizeof(*obdos) * stripe_count);
1811 GOTO(out_where, rc = -ENOMEM);
1813 if (oa->o_valid & OBD_MD_FLHANDLE)
1814 lfh = lov_handle2lfh(obdo_handle(oa));
1816 oa->o_valid &= ~OBD_MD_FLHANDLE;
1819 OBD_ALLOC(ioarr, sizeof(*ioarr) * oa_bufs);
1821 GOTO(out_obdos, rc = -ENOMEM);
1823 for (i = 0; i < oa_bufs; i++) {
1824 where[i] = lov_stripe_number(lsm, pga[i].off);
1825 stripeinfo[where[i]].bufct++;
1828 for (i = 0, loi = lsm->lsm_oinfo, si_last = si = stripeinfo;
1829 i < stripe_count; i++, loi++, si_last = si, si++) {
1831 si->index = si_last->index + si_last->bufct;
1832 si->lsm.lsm_object_id = loi->loi_id;
1833 si->ost_idx = loi->loi_ost_idx;
1836 memcpy(&obdos[i], oa, sizeof(*obdos));
1837 obdos[i].o_id = si->lsm.lsm_object_id;
1839 memcpy(obdo_handle(&obdos[i]),
1840 &lfh->lfh_och[i].och_fh,
1841 sizeof(lfh->lfh_och[i].och_fh));
1845 for (i = 0; i < oa_bufs; i++) {
1846 int which = where[i];
1849 shift = stripeinfo[which].index + stripeinfo[which].subcount;
1850 LASSERT(shift < oa_bufs);
1851 ioarr[shift] = pga[i];
1852 lov_stripe_offset(lsm, pga[i].off, which, &ioarr[shift].off);
1853 stripeinfo[which].subcount++;
1856 for (i = 0, si = stripeinfo; i < stripe_count; i++, si++) {
1857 int shift = si->index;
1862 if (lov->tgts[si->ost_idx].active == 0) {
1863 CDEBUG(D_HA, "lov idx %d inactive\n", si->ost_idx);
1864 GOTO(out_ioarr, rc = -EIO);
1867 LASSERT(shift < oa_bufs);
1869 rc = obd_brw_async(cmd, &lov->tgts[si->ost_idx].conn,
1870 &obdos[i], &si->lsm, si->bufct,
1871 &ioarr[shift], set, oti);
1873 GOTO(out_ioarr, rc);
1876 LASSERT(set->set_interpret == NULL);
1877 set->set_interpret = (set_interpreter_func)lov_brw_interpret;
1878 LASSERT(sizeof(set->set_args) >= sizeof(struct lov_brw_async_args));
1879 aa = (struct lov_brw_async_args *)&set->set_args;
1881 aa->aa_obdos = obdos;
1883 aa->aa_ioarr = ioarr;
1884 aa->aa_oa_bufs = oa_bufs;
1886 /* Don't free ioarr or obdos - that's done in lov_brw_interpret */
1887 GOTO(out_where, rc);
1890 OBD_FREE(ioarr, sizeof(*ioarr) * oa_bufs);
1892 OBD_FREE(obdos, stripe_count * sizeof(*obdos));
1894 OBD_FREE(where, sizeof(*where) * oa_bufs);
1898 OBD_FREE(stripeinfo, stripe_count * sizeof(*stripeinfo));
1900 class_export_put(export);
1904 static int lov_enqueue(struct lustre_handle *conn, struct lov_stripe_md *lsm,
1905 struct lustre_handle *parent_lock,
1906 __u32 type, void *cookie, int cookielen, __u32 mode,
1907 int *flags, void *cb, void *data,
1908 struct lustre_handle *lockh)
1910 struct obd_export *export = class_conn2export(conn);
1911 struct lov_lock_handles *lov_lockh = NULL;
1912 struct lustre_handle *lov_lockhp;
1913 struct lov_obd *lov;
1914 struct lov_oinfo *loi;
1915 struct lov_stripe_md submd;
1920 if (lsm_bad_magic(lsm))
1921 GOTO(out_exp, rc = -EINVAL);
1923 /* we should never be asked to replay a lock this way. */
1924 LASSERT((*flags & LDLM_FL_REPLAY) == 0);
1926 if (!export || !export->exp_obd)
1927 GOTO(out_exp, rc = -ENODEV);
1929 if (lsm->lsm_stripe_count > 1) {
1930 lov_lockh = lov_llh_new(lsm);
1931 if (lov_lockh == NULL)
1932 GOTO(out_exp, rc = -ENOMEM);
1934 lockh->cookie = lov_lockh->llh_handle.h_cookie;
1935 lov_lockhp = lov_lockh->llh_handles;
1940 lov = &export->exp_obd->u.lov;
1941 for (i = 0, loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count;
1942 i++, loi++, lov_lockhp++) {
1943 struct ldlm_extent *extent = (struct ldlm_extent *)cookie;
1944 struct ldlm_extent sub_ext;
1947 if (!lov_stripe_intersects(lsm, i, extent->start, extent->end,
1948 &sub_ext.start, &sub_ext.end))
1951 if (lov->tgts[loi->loi_ost_idx].active == 0) {
1952 CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
1956 /* XXX LOV STACKING: submd should be from the subobj */
1957 submd.lsm_object_id = loi->loi_id;
1958 submd.lsm_stripe_count = 0;
1959 /* XXX submd is not fully initialized here */
1961 rc = obd_enqueue(&(lov->tgts[loi->loi_ost_idx].conn), &submd,
1962 parent_lock, type, &sub_ext, sizeof(sub_ext),
1963 mode, flags, cb, data, lov_lockhp);
1965 // XXX add a lock debug statement here
1966 if (rc != ELDLM_OK) {
1967 memset(lov_lockhp, 0, sizeof(*lov_lockhp));
1968 if (lov->tgts[loi->loi_ost_idx].active) {
1969 CERROR("error: enqueue objid "LPX64" subobj "
1970 LPX64" on OST idx %d: rc = %d\n",
1971 lsm->lsm_object_id, loi->loi_id,
1972 loi->loi_ost_idx, rc);
1973 GOTO(out_locks, rc);
1977 if (lsm->lsm_stripe_count > 1)
1978 lov_llh_put(lov_lockh);
1979 GOTO(out_exp, rc = ELDLM_OK);
1982 while (loi--, lov_lockhp--, i-- > 0) {
1983 struct lov_stripe_md submd;
1986 if (lov_lockhp->cookie == 0)
1989 /* XXX LOV STACKING: submd should be from the subobj */
1990 submd.lsm_object_id = loi->loi_id;
1991 submd.lsm_stripe_count = 0;
1992 err = obd_cancel(&lov->tgts[loi->loi_ost_idx].conn, &submd,
1994 if (err && lov->tgts[loi->loi_ost_idx].active) {
1995 CERROR("error: cancelling objid "LPX64" on OST "
1996 "idx %d after enqueue error: rc = %d\n",
1997 loi->loi_id, loi->loi_ost_idx, err);
2001 if (lsm->lsm_stripe_count > 1) {
2002 lov_llh_destroy(lov_lockh);
2003 lov_llh_put(lov_lockh);
2006 class_export_put(export);
2010 static int lov_match(struct lustre_handle *conn, struct lov_stripe_md *lsm,
2011 __u32 type, void *cookie, int cookielen, __u32 mode,
2012 int *flags, void *data, struct lustre_handle *lockh)
2014 struct obd_export *export = class_conn2export(conn);
2015 struct lov_lock_handles *lov_lockh = NULL;
2016 struct lustre_handle *lov_lockhp;
2017 struct lov_obd *lov;
2018 struct lov_oinfo *loi;
2019 struct lov_stripe_md submd;
2020 ldlm_error_t rc = 0;
2024 if (lsm_bad_magic(lsm))
2025 GOTO(out_exp, rc = -EINVAL);
2027 if (!export || !export->exp_obd)
2028 GOTO(out_exp, rc = -ENODEV);
2030 if (lsm->lsm_stripe_count > 1) {
2031 lov_lockh = lov_llh_new(lsm);
2032 if (lov_lockh == NULL)
2033 GOTO(out_exp, rc = -ENOMEM);
2035 lockh->cookie = lov_lockh->llh_handle.h_cookie;
2036 lov_lockhp = lov_lockh->llh_handles;
2041 lov = &export->exp_obd->u.lov;
2042 for (i = 0, loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count;
2043 i++, loi++, lov_lockhp++) {
2044 struct ldlm_extent *extent = (struct ldlm_extent *)cookie;
2045 struct ldlm_extent sub_ext;
2048 if (!lov_stripe_intersects(lsm, i, extent->start, extent->end,
2049 &sub_ext.start, &sub_ext.end))
2052 if (lov->tgts[loi->loi_ost_idx].active == 0) {
2053 CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
2058 /* XXX LOV STACKING: submd should be from the subobj */
2059 submd.lsm_object_id = loi->loi_id;
2060 submd.lsm_stripe_count = 0;
2062 /* XXX submd is not fully initialized here */
2063 rc = obd_match(&(lov->tgts[loi->loi_ost_idx].conn), &submd,
2064 type, &sub_ext, sizeof(sub_ext), mode,
2065 &lov_flags, data, lov_lockhp);
2070 if (lsm->lsm_stripe_count > 1)
2071 lov_llh_put(lov_lockh);
2075 while (loi--, lov_lockhp--, i-- > 0) {
2076 struct lov_stripe_md submd;
2079 if (lov_lockhp->cookie == 0)
2082 /* XXX LOV STACKING: submd should be from the subobj */
2083 submd.lsm_object_id = loi->loi_id;
2084 submd.lsm_stripe_count = 0;
2085 err = obd_cancel(&lov->tgts[loi->loi_ost_idx].conn, &submd,
2087 if (err && lov->tgts[loi->loi_ost_idx].active) {
2088 CERROR("error: cancelling objid "LPX64" on OST "
2089 "idx %d after match failure: rc = %d\n",
2090 loi->loi_id, loi->loi_ost_idx, err);
2094 if (lsm->lsm_stripe_count > 1) {
2095 lov_llh_destroy(lov_lockh);
2096 lov_llh_put(lov_lockh);
2099 class_export_put(export);
2103 static int lov_cancel(struct lustre_handle *conn, struct lov_stripe_md *lsm,
2104 __u32 mode, struct lustre_handle *lockh)
2106 struct obd_export *export = class_conn2export(conn);
2107 struct lov_lock_handles *lov_lockh = NULL;
2108 struct lustre_handle *lov_lockhp;
2109 struct lov_obd *lov;
2110 struct lov_oinfo *loi;
2114 if (lsm_bad_magic(lsm))
2115 GOTO(out, rc = -EINVAL);
2117 if (!export || !export->exp_obd)
2118 GOTO(out, rc = -ENODEV);
2121 if (lsm->lsm_stripe_count > 1) {
2122 lov_lockh = lov_handle2llh(lockh);
2124 CERROR("LOV: invalid lov lock handle %p\n", lockh);
2125 GOTO(out, rc = -EINVAL);
2128 lov_lockhp = lov_lockh->llh_handles;
2133 lov = &export->exp_obd->u.lov;
2134 for (i = 0, loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count;
2135 i++, loi++, lov_lockhp++) {
2136 struct lov_stripe_md submd;
2139 if (lov_lockhp->cookie == 0) {
2140 CDEBUG(D_HA, "lov idx %d subobj "LPX64" no lock?\n",
2141 loi->loi_ost_idx, loi->loi_id);
2145 /* XXX LOV STACKING: submd should be from the subobj */
2146 submd.lsm_object_id = loi->loi_id;
2147 submd.lsm_stripe_count = 0;
2148 err = obd_cancel(&lov->tgts[loi->loi_ost_idx].conn, &submd,
2151 if (lov->tgts[loi->loi_ost_idx].active) {
2152 CERROR("error: cancel objid "LPX64" subobj "
2153 LPX64" on OST idx %d: rc = %d\n",
2155 loi->loi_id, loi->loi_ost_idx, err);
2162 if (lsm->lsm_stripe_count > 1)
2163 lov_llh_destroy(lov_lockh);
2164 if (lov_lockh != NULL)
2165 lov_llh_put(lov_lockh);
2168 class_export_put(export);
2172 static int lov_cancel_unused(struct lustre_handle *conn,
2173 struct lov_stripe_md *lsm, int flags, void *opaque)
2175 struct obd_export *export = class_conn2export(conn);
2176 struct lov_obd *lov;
2177 struct lov_oinfo *loi;
2181 if (lsm_bad_magic(lsm))
2182 GOTO(out, rc = -EINVAL);
2184 if (!export || !export->exp_obd)
2185 GOTO(out, rc = -ENODEV);
2187 lov = &export->exp_obd->u.lov;
2188 for (i = 0,loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++,loi++) {
2189 struct lov_stripe_md submd;
2192 if (lov->tgts[loi->loi_ost_idx].active == 0)
2193 CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
2195 submd.lsm_object_id = loi->loi_id;
2196 submd.lsm_stripe_count = 0;
2197 err = obd_cancel_unused(&lov->tgts[loi->loi_ost_idx].conn,
2198 &submd, flags, opaque);
2199 if (err && lov->tgts[loi->loi_ost_idx].active) {
2200 CERROR("error: cancel unused objid "LPX64" subobj "LPX64
2201 " on OST idx %d: rc = %d\n", lsm->lsm_object_id,
2202 loi->loi_id, loi->loi_ost_idx, err);
2209 class_export_put(export);
2213 #define LOV_U64_MAX ((__u64)~0ULL)
2214 #define LOV_SUM_MAX(tot, add) \
2216 if ((tot) + (add) < (tot)) \
2217 (tot) = LOV_U64_MAX; \
2222 static int lov_statfs(struct obd_device *obd, struct obd_statfs *osfs,
2223 unsigned long max_age)
2225 struct lov_obd *lov = &obd->u.lov;
2226 struct obd_statfs lov_sfs;
2233 /* We only get block data from the OBD */
2234 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
2237 if (!lov->tgts[i].active) {
2238 CDEBUG(D_HA, "lov idx %d inactive\n", i);
2242 err = obd_statfs(class_conn2obd(&lov->tgts[i].conn), &lov_sfs,
2245 if (lov->tgts[i].active && !rc)
2251 memcpy(osfs, &lov_sfs, sizeof(lov_sfs));
2254 osfs->os_bfree += lov_sfs.os_bfree;
2255 osfs->os_bavail += lov_sfs.os_bavail;
2256 osfs->os_blocks += lov_sfs.os_blocks;
2257 /* XXX not sure about this one - depends on policy.
2258 * - could be minimum if we always stripe on all OBDs
2259 * (but that would be wrong for any other policy,
2260 * if one of the OBDs has no more objects left)
2261 * - could be sum if we stripe whole objects
2262 * - could be average, just to give a nice number
2264 * To give a "reasonable" (if not wholly accurate)
2265 * number, we divide the total number of free objects
2266 * by expected stripe count (watch out for overflow).
2268 LOV_SUM_MAX(osfs->os_files, lov_sfs.os_files);
2269 LOV_SUM_MAX(osfs->os_ffree, lov_sfs.os_ffree);
2274 __u32 expected_stripes = lov->desc.ld_default_stripe_count ?
2275 lov->desc.ld_default_stripe_count :
2276 lov->desc.ld_active_tgt_count;
2278 if (osfs->os_files != LOV_U64_MAX)
2279 do_div(osfs->os_files, expected_stripes);
2280 if (osfs->os_ffree != LOV_U64_MAX)
2281 do_div(osfs->os_ffree, expected_stripes);
2288 static int lov_iocontrol(unsigned int cmd, struct lustre_handle *conn, int len,
2289 void *karg, void *uarg)
2291 struct obd_device *obddev = class_conn2obd(conn);
2292 struct lov_obd *lov = &obddev->u.lov;
2293 int i, count = lov->desc.ld_tgt_count;
2294 struct obd_uuid *uuidp;
2300 case IOC_LOV_SET_OSC_ACTIVE: {
2301 struct obd_ioctl_data *data = karg;
2302 uuidp = (struct obd_uuid *)data->ioc_inlbuf1;
2303 rc = lov_set_osc_active(lov, uuidp, data->ioc_offset);
2306 case OBD_IOC_LOV_GET_CONFIG: {
2307 struct obd_ioctl_data *data = karg;
2308 struct lov_tgt_desc *tgtdesc;
2309 struct lov_desc *desc;
2314 if (obd_ioctl_getdata(&buf, &len, (void *)uarg))
2317 data = (struct obd_ioctl_data *)buf;
2319 if (sizeof(*desc) > data->ioc_inllen1) {
2324 if (sizeof(uuidp->uuid) * count > data->ioc_inllen2) {
2329 desc = (struct lov_desc *)data->ioc_inlbuf1;
2330 memcpy(desc, &(lov->desc), sizeof(*desc));
2332 uuidp = (struct obd_uuid *)data->ioc_inlbuf2;
2333 tgtdesc = lov->tgts;
2334 for (i = 0; i < count; i++, uuidp++, tgtdesc++)
2335 obd_str2uuid(uuidp, tgtdesc->uuid.uuid);
2337 rc = copy_to_user((void *)uarg, buf, len);
2340 obd_ioctl_freedata(buf, len);
2343 case LL_IOC_LOV_SETSTRIPE:
2344 rc = lov_setstripe(conn, karg, uarg);
2346 case LL_IOC_LOV_GETSTRIPE:
2347 rc = lov_getstripe(conn, karg, uarg);
2354 for (i = 0; i < count; i++) {
2357 err = obd_iocontrol(cmd, &lov->tgts[i].conn,
2360 if (lov->tgts[i].active) {
2361 CERROR("error: iocontrol OSC %s on OST"
2362 "idx %d: err = %d\n",
2363 lov->tgts[i].uuid.uuid, i, err);
2378 static int lov_get_info(struct lustre_handle *conn, __u32 keylen,
2379 void *key, __u32 *vallen, void *val)
2381 struct obd_device *obddev = class_conn2obd(conn);
2382 struct lov_obd *lov = &obddev->u.lov;
2386 if (!vallen || !val)
2389 if (keylen > strlen("lock_to_stripe") &&
2390 strcmp(key, "lock_to_stripe") == 0) {
2393 struct ldlm_lock *lock;
2394 struct lov_stripe_md *lsm;
2396 __u32 *stripe = val;
2397 struct lov_oinfo *loi;
2399 if (*vallen < sizeof(*stripe))
2401 *vallen = sizeof(*stripe);
2403 /* XXX This is another one of those bits that will need to
2404 * change if we ever actually support nested LOVs. It uses
2405 * the lock's connection to find out which stripe it is. */
2406 for (i = 0, loi = data->lsm->lsm_oinfo;
2407 i < data->lsm->lsm_stripe_count;
2409 if (lov->tgts[loi->loi_ost_idx].conn.cookie ==
2410 data->lock->l_connh->cookie) {
2421 static int lov_set_info(struct lustre_handle *conn, obd_count keylen,
2422 void *key, obd_count vallen, void *val)
2424 struct obd_device *obddev = class_conn2obd(conn);
2425 struct lov_obd *lov = &obddev->u.lov;
2429 if (keylen < strlen("mds_conn") ||
2430 memcmp(key, "mds_conn", strlen("mds_conn")) != 0)
2433 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
2435 er = obd_set_info(&lov->tgts[i].conn, keylen, key, vallen, val);
2442 static int lov_mark_page_dirty(struct lustre_handle *conn,
2443 struct lov_stripe_md *lsm, unsigned long offset)
2445 struct lov_obd *lov = &class_conn2obd(conn)->u.lov;
2446 struct lov_oinfo *loi;
2447 struct lov_stripe_md *submd;
2452 if (lsm_bad_magic(lsm))
2455 OBD_ALLOC(submd, lov_stripe_md_size(1));
2459 stripe = lov_stripe_number(lsm, (obd_off)offset << PAGE_CACHE_SHIFT);
2460 lov_stripe_offset(lsm, (obd_off)offset << PAGE_CACHE_SHIFT, stripe,
2462 off >>= PAGE_CACHE_SHIFT;
2464 loi = &lsm->lsm_oinfo[stripe];
2465 CDEBUG(D_INODE, "off %lu => off %lu on stripe %d\n", offset,
2466 (unsigned long)off, stripe);
2467 submd->lsm_oinfo[0].loi_dirty_ot = &loi->loi_dirty_ot_inline;
2469 rc = obd_mark_page_dirty(&lov->tgts[loi->loi_ost_idx].conn, submd, off);
2470 OBD_FREE(submd, lov_stripe_md_size(1));
2474 static int lov_clear_dirty_pages(struct lustre_handle *conn,
2475 struct lov_stripe_md *lsm, unsigned long start,
2476 unsigned long end, unsigned long *cleared)
2479 struct obd_export *export = class_conn2export(conn);
2480 __u64 start_off = (__u64)start << PAGE_CACHE_SHIFT;
2481 __u64 end_off = (__u64)end << PAGE_CACHE_SHIFT;
2482 __u64 obd_start, obd_end;
2483 struct lov_stripe_md *submd = NULL;
2484 struct lov_obd *lov;
2485 struct lov_oinfo *loi;
2487 unsigned long osc_cleared;
2492 if (lsm_bad_magic(lsm))
2493 GOTO(out_exp, rc = -EINVAL);
2495 if (!export || !export->exp_obd)
2496 GOTO(out_exp, rc = -ENODEV);
2498 OBD_ALLOC(submd, lov_stripe_md_size(1));
2500 GOTO(out_exp, rc = -ENOMEM);
2502 lov = &export->exp_obd->u.lov;
2504 for (i = 0, loi = lsm->lsm_oinfo;
2505 i < lsm->lsm_stripe_count;
2507 if (lov->tgts[loi->loi_ost_idx].active == 0) {
2508 CDEBUG(D_HA, "lov idx %d inactive\n", loi->loi_ost_idx);
2512 if(!lov_stripe_intersects(lsm, i, start_off, end_off,
2513 &obd_start, &obd_end))
2515 obd_start >>= PAGE_CACHE_SHIFT;
2516 obd_end >>= PAGE_CACHE_SHIFT;
2518 CDEBUG(D_INODE, "offs [%lu,%lu] => offs [%lu,%lu] stripe %d\n",
2519 start, end, (unsigned long)obd_start,
2520 (unsigned long)obd_end, loi->loi_ost_idx);
2521 submd->lsm_oinfo[0].loi_dirty_ot = &loi->loi_dirty_ot_inline;
2522 rc = obd_clear_dirty_pages(&lov->tgts[loi->loi_ost_idx].conn,
2523 submd, obd_start, obd_end,
2527 *cleared += osc_cleared;
2531 OBD_FREE(submd, lov_stripe_md_size(1));
2532 class_export_put(export);
2536 static int lov_last_dirty_offset(struct lustre_handle *conn,
2537 struct lov_stripe_md *lsm,
2538 unsigned long *offset)
2540 struct obd_export *export = class_conn2export(conn);
2541 struct lov_stripe_md *submd = NULL;
2542 struct lov_obd *lov;
2543 struct lov_oinfo *loi;
2544 unsigned long tmp, count, skip;
2548 if (lsm_bad_magic(lsm))
2549 GOTO(out_exp, rc = -EINVAL);
2551 if (!export || !export->exp_obd)
2552 GOTO(out_exp, rc = -ENODEV);
2554 OBD_ALLOC(submd, lov_stripe_md_size(1));
2556 GOTO(out_exp, rc = -ENOMEM);
2559 lov = &export->exp_obd->u.lov;
2562 for (i = 0, loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++,loi++){
2563 count = lsm->lsm_stripe_size >> PAGE_CACHE_SHIFT;
2564 skip = (lsm->lsm_stripe_count - 1) * count;
2566 submd->lsm_oinfo[0].loi_dirty_ot = &loi->loi_dirty_ot_inline;
2568 err = obd_last_dirty_offset(&lov->tgts[loi->loi_ost_idx].conn,
2573 GOTO(out_exp, rc = err);
2577 tmp += (tmp/count * skip) + (i * count);
2583 OBD_FREE(submd, lov_stripe_md_size(1));
2584 class_export_put(export);
2588 /* For LOV catalogs, we "nest" catalogs from the parent catalog. What this
2589 * means is that the parent catalog has a bunch of log cookies that are
2590 * pointing at one catalog for each OSC. The OSC catalogs in turn hold
2591 * cookies for actual log files. */
2592 static int lov_get_catalogs(struct lov_obd *lov, struct llog_handle *cathandle)
2597 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
2598 lov->tgts[i].ltd_cathandle = llog_new_log(cathandle,
2599 &lov->tgts[i].uuid);
2600 if (IS_ERR(lov->tgts[i].ltd_cathandle))
2602 rc = llog_init_catalog(cathandle, &lov->tgts[i].uuid);
2606 lov->lo_catalog_loaded = 1;
2610 llog_delete_log(cathandle, lov->tgts[i].ltd_cathandle);
2611 llog_close_log(cathandle, lov->tgts[i].ltd_cathandle);
2616 /* Add log records for each OSC that this object is striped over, and return
2617 * cookies for each one. We _would_ have nice abstraction here, except that
2618 * we need to keep cookies in stripe order, even if some are NULL, so that
2619 * the right cookies are passed back to the right OSTs at the client side.
2620 * Unset cookies should be all-zero (which will never occur naturally). */
2621 static int lov_log_add(struct lustre_handle *conn,
2622 struct llog_handle *cathandle,
2623 struct llog_trans_hdr *rec, struct lov_stripe_md *lsm,
2624 struct llog_cookie *logcookies, int numcookies)
2626 struct obd_device *obd = class_conn2obd(conn);
2627 struct lov_obd *lov = &obd->u.lov;
2628 struct lov_oinfo *loi;
2632 LASSERT(logcookies && numcookies >= lsm->lsm_stripe_count);
2634 if (unlikely(!lov->lo_catalog_loaded))
2635 lov_get_catalogs(lov, cathandle);
2637 for (i = 0,loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++,loi++) {
2638 rc += obd_log_add(&lov->tgts[loi->loi_ost_idx].conn,
2639 lov->tgts[loi->loi_ost_idx].ltd_cathandle,
2640 rec, NULL, logcookies + rc, numcookies - rc);
2646 static int lov_log_cancel(struct lustre_handle *conn, struct lov_stripe_md *lsm,
2647 int count, struct llog_cookie *cookies, int flags)
2649 struct obd_export *export = class_conn2export(conn);
2650 struct lov_obd *lov;
2651 struct lov_oinfo *loi;
2655 LASSERT(lsm != NULL);
2656 if (export == NULL || export->exp_obd == NULL)
2657 GOTO(out, rc = -ENODEV);
2659 LASSERT(count == lsm->lsm_stripe_count);
2661 loi = lsm->lsm_oinfo;
2662 lov = &export->exp_obd->u.lov;
2663 for (i = 0; i < count; i++, cookies++, loi++) {
2666 err = obd_log_cancel(&lov->tgts[loi->loi_ost_idx].conn,
2667 NULL, 1, cookies, flags);
2668 if (err && lov->tgts[loi->loi_ost_idx].active) {
2669 CERROR("error: objid "LPX64" subobj "LPX64
2670 " on OST idx %d: rc = %d\n", lsm->lsm_object_id,
2671 loi->loi_id, loi->loi_ost_idx, err);
2678 class_export_put(export);
2682 struct obd_ops lov_obd_ops = {
2683 o_owner: THIS_MODULE,
2684 o_attach: lov_attach,
2685 o_detach: lov_detach,
2687 o_connect: lov_connect,
2688 o_disconnect: lov_disconnect,
2689 o_statfs: lov_statfs,
2690 o_packmd: lov_packmd,
2691 o_unpackmd: lov_unpackmd,
2692 o_create: lov_create,
2693 o_destroy: lov_destroy,
2694 o_getattr: lov_getattr,
2695 o_getattr_async: lov_getattr_async,
2696 o_setattr: lov_setattr,
2700 o_brw_async: lov_brw_async,
2702 o_enqueue: lov_enqueue,
2704 o_cancel: lov_cancel,
2705 o_cancel_unused: lov_cancel_unused,
2706 o_iocontrol: lov_iocontrol,
2707 o_get_info: lov_get_info,
2708 o_set_info: lov_set_info,
2709 o_log_add: lov_log_add,
2710 o_log_cancel: lov_log_cancel,
2711 o_mark_page_dirty: lov_mark_page_dirty,
2712 o_clear_dirty_pages: lov_clear_dirty_pages,
2713 o_last_dirty_offset: lov_last_dirty_offset,
2716 int __init lov_init(void)
2718 struct lprocfs_static_vars lvars;
2721 lprocfs_init_vars(lov, &lvars);
2722 rc = class_register_type(&lov_obd_ops, lvars.module_vars,
2723 OBD_LOV_DEVICENAME);
2727 static void /*__exit*/ lov_exit(void)
2729 class_unregister_type(OBD_LOV_DEVICENAME);
2733 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
2734 MODULE_DESCRIPTION("Lustre Logical Object Volume OBD driver");
2735 MODULE_LICENSE("GPL");
2737 module_init(lov_init);
2738 module_exit(lov_exit);