Whamcloud - gitweb
land b_md onto HEAD. the highlights:
[fs/lustre-release.git] / lustre / lov / lov_pack.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (C) 2002 Cluster File Systems, Inc. <adilger@clusterfs.com>
5  *
6  *   This file is part of Lustre, http://www.lustre.org.
7  *
8  *   Lustre is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Lustre is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with Lustre; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  *
21  * (Un)packing of OST/MDS requests
22  *
23  */
24
25 #define DEBUG_SUBSYSTEM S_LLITE
26
27 #include <linux/lustre_net.h>
28 #include <linux/obd.h>
29 #include <linux/obd_lov.h>
30 #include <linux/obd_support.h>
31
32 /* lov_packdesc() is in mds/mds_lov.c */
33
34 void lov_unpackdesc(struct lov_desc *ld)
35 {
36         ld->ld_tgt_count = NTOH__u32(ld->ld_tgt_count);
37         ld->ld_default_stripe_count = HTON__u32(ld->ld_default_stripe_count);
38         ld->ld_default_stripe_size = HTON__u32(ld->ld_default_stripe_size);
39         ld->ld_pattern = HTON__u32(ld->ld_pattern);
40 }
41
42 /* Pack LOV object metadata for shipment to the MDS.
43  *
44  * XXX In the future, this will be enhanced to get the EA size from the
45  *     underlying OSC device(s) to get their EA sizes so we can stack
46  *     LOVs properly.  For now lov_mds_md_size() just assumes one obd_id
47  *     per stripe.
48  */
49 int lov_packmd(struct lustre_handle *conn, struct lov_mds_md **lmmp,
50                struct lov_stripe_md *lsm)
51 {
52         struct obd_device *obd = class_conn2obd(conn);
53         struct lov_obd *lov = &obd->u.lov;
54         struct lov_oinfo *loi;
55         struct lov_mds_md *lmm;
56         int ost_count = lov->desc.ld_tgt_count;
57         int stripe_count = ost_count;
58         int lmm_size;
59         int i;
60         ENTRY;
61
62         if (lsm) {
63                 if (lsm->lsm_magic != LOV_MAGIC) {
64                         CERROR("bad mem LOV MAGIC: %#08x != %#08x\n",
65                                lsm->lsm_magic, LOV_MAGIC);
66                         RETURN(-EINVAL);
67                 }
68                 stripe_count = lsm->lsm_stripe_count;
69         }
70
71         /* XXX LOV STACKING call into osc for sizes */
72         lmm_size = lov_mds_md_size(ost_count);
73
74         if (!lmmp)
75                 RETURN(lmm_size);
76
77         if (*lmmp && !lsm) {
78                 /* endianness */
79                 ost_count = ((*lmmp)->lmm_ost_count);
80                 OBD_FREE(*lmmp, lov_mds_md_size(ost_count));
81                 *lmmp = NULL;
82                 RETURN(0);
83         }
84
85         if (!*lmmp) {
86                 OBD_ALLOC(*lmmp, lmm_size);
87                 if (!*lmmp)
88                         RETURN(-ENOMEM);
89         }
90
91         lmm = *lmmp;
92
93         lmm->lmm_stripe_count = (stripe_count);
94         if (!lsm)
95                 RETURN(lmm_size);
96         /* XXX endianness */
97         lmm->lmm_magic = (lsm->lsm_magic);
98         lmm->lmm_object_id = (lsm->lsm_object_id);
99         LASSERT(lsm->lsm_object_id);
100         lmm->lmm_stripe_size = (lsm->lsm_stripe_size);
101         lmm->lmm_stripe_pattern = (lsm->lsm_stripe_pattern);
102         lmm->lmm_stripe_offset = (lsm->lsm_stripe_offset);
103         lmm->lmm_ost_count = (lov->desc.ld_tgt_count);
104
105         /* Only fill in the object ids which we are actually using.
106          * Assumes lmm_objects is otherwise zero-filled. */
107         for (i = 0, loi = lsm->lsm_oinfo; i < stripe_count; i++, loi++) {
108                 /* XXX call down to osc_packmd() to do the packing */
109                 LASSERT(loi->loi_id);
110                 lmm->lmm_objects[loi->loi_ost_idx].l_object_id = (loi->loi_id);
111         }
112
113         RETURN(lmm_size);
114 }
115
116 int lov_unpackmd(struct lustre_handle *conn, struct lov_stripe_md **lsmp,
117                  struct lov_mds_md *lmm)
118 {
119         struct obd_device *obd = class_conn2obd(conn);
120         struct lov_obd *lov = &obd->u.lov;
121         struct lov_stripe_md *lsm;
122         struct lov_oinfo *loi;
123         int ost_count = lov->desc.ld_active_tgt_count;
124         int ost_offset = 0;
125         int stripe_count = 0;
126         int lsm_size;
127         int i;
128         ENTRY;
129
130         if (lmm) {
131                 /* endianness */
132                 if (lmm->lmm_magic != LOV_MAGIC) {
133                         CERROR("bad wire LOV MAGIC: %#08x != %#08x\n",
134                                lmm->lmm_magic, LOV_MAGIC);
135                         RETURN(-EINVAL);
136                 }
137                 stripe_count = (lmm->lmm_stripe_count);
138         }
139
140         if (!stripe_count)
141                 stripe_count = lov->desc.ld_default_stripe_count;
142         if (!stripe_count || stripe_count > ost_count)
143                 stripe_count = ost_count;
144
145         /* XXX LOV STACKING call into osc for sizes */
146         lsm_size = lov_stripe_md_size(stripe_count);
147
148         if (!lsmp)
149                 RETURN(lsm_size);
150
151         if (*lsmp && !lmm) {
152                 stripe_count = (*lsmp)->lsm_stripe_count;
153                 OBD_FREE(*lsmp, lov_stripe_md_size(stripe_count));
154                 *lsmp = NULL;
155                 RETURN(0);
156         }
157
158         if (!*lsmp) {
159                 OBD_ALLOC(*lsmp, lsm_size);
160                 if (!*lsmp)
161                         RETURN(-ENOMEM);
162         }
163
164         lsm = *lsmp;
165
166         lsm->lsm_stripe_count = stripe_count;
167         if (!lmm)
168                 RETURN(lsm_size);
169
170         /* XXX endianness */
171         ost_offset = lsm->lsm_stripe_offset = (lmm->lmm_stripe_offset);
172         lsm->lsm_magic = (lmm->lmm_magic);
173         lsm->lsm_object_id = (lmm->lmm_object_id);
174         LASSERT(lsm->lsm_object_id);
175         lsm->lsm_stripe_size = (lmm->lmm_stripe_size);
176         lsm->lsm_stripe_pattern = (lmm->lmm_stripe_pattern);
177
178         for (i = 0, loi = lsm->lsm_oinfo; i < ost_count; i++, ost_offset++) {
179                 ost_offset %= ost_count;
180
181                 if (!lmm->lmm_objects[ost_offset].l_object_id)
182                         continue;
183
184                 LASSERT(loi - lsm->lsm_oinfo < stripe_count);
185                 /* XXX LOV STACKING call down to osc_unpackmd() */
186                 loi->loi_id = (lmm->lmm_objects[ost_offset].l_object_id);
187                 loi->loi_ost_idx = ost_offset;
188                 loi++;
189         }
190         LASSERT(loi - lsm->lsm_oinfo == stripe_count);
191
192         RETURN(lsm_size);
193 }