Whamcloud - gitweb
Merge b_md to HEAD for 0.5.19 release.
[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_class.h>
31 #include <linux/obd_support.h>
32
33 /* lov_packdesc() is in mds/mds_lov.c */
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 void lov_dump_lmm(int level, struct lov_mds_md *lmm)
43 {
44         struct lov_object_id *loi;
45         int idx;
46
47         CDEBUG(level, "objid "LPX64", magic %#08x, ost_count %u\n",
48                lmm->lmm_object_id, lmm->lmm_magic, lmm->lmm_ost_count);
49         CDEBUG(level,"stripe_size %u, stripe_count %u, stripe_offset %u\n",
50                lmm->lmm_stripe_size, lmm->lmm_stripe_count,
51                lmm->lmm_stripe_offset);
52         for (idx = 0, loi = lmm->lmm_objects; idx < lmm->lmm_ost_count;
53              idx++, loi++)
54                 CDEBUG(level, "ost idx %u subobj "LPX64"\n", idx,
55                        loi->l_object_id);
56 }
57
58 #define LMM_ASSERT(test)                                                \
59 do {                                                                    \
60         if (!(test)) lov_dump_lmm(D_ERROR, lmm);                        \
61         LASSERT(test); /* so we know what assertion failed */           \
62 } while(0)
63
64 /* Pack LOV object metadata for shipment to the MDS.
65  *
66  * XXX In the future, this will be enhanced to get the EA size from the
67  *     underlying OSC device(s) to get their EA sizes so we can stack
68  *     LOVs properly.  For now lov_mds_md_size() just assumes one obd_id
69  *     per stripe.
70  */
71 int lov_packmd(struct lustre_handle *conn, struct lov_mds_md **lmmp,
72                struct lov_stripe_md *lsm)
73 {
74         struct obd_device *obd = class_conn2obd(conn);
75         struct lov_obd *lov = &obd->u.lov;
76         struct lov_oinfo *loi;
77         struct lov_mds_md *lmm;
78         int ost_count = lov->desc.ld_tgt_count;
79         int stripe_count = ost_count;
80         int lmm_size;
81         int i;
82         ENTRY;
83
84         if (lsm) {
85                 int i, max = 0;
86                 if (lsm->lsm_magic != LOV_MAGIC) {
87                         CERROR("bad mem LOV MAGIC: %#010x != %#010x\n",
88                                lsm->lsm_magic, LOV_MAGIC);
89                         RETURN(-EINVAL);
90                 }
91                 stripe_count = lsm->lsm_stripe_count;
92
93                 for (i = 0,loi = lsm->lsm_oinfo; i < stripe_count; i++,loi++) {
94                         if (loi->loi_ost_idx > max)
95                                 max = loi->loi_ost_idx;
96                 }
97                 ost_count = max + 1;
98         }
99
100         /* XXX LOV STACKING call into osc for sizes */
101         lmm_size = lov_mds_md_size(ost_count);
102
103         if (!lmmp)
104                 RETURN(lmm_size);
105
106         if (*lmmp && !lsm) {
107                 /* endianness */
108                 ost_count = ((*lmmp)->lmm_ost_count);
109                 OBD_FREE(*lmmp, lov_mds_md_size(ost_count));
110                 *lmmp = NULL;
111                 RETURN(0);
112         }
113
114         if (!*lmmp) {
115                 OBD_ALLOC(*lmmp, lmm_size);
116                 if (!*lmmp)
117                         RETURN(-ENOMEM);
118         }
119
120         lmm = *lmmp;
121
122         lmm->lmm_stripe_count = (stripe_count);
123         if (!lsm)
124                 RETURN(lmm_size);
125
126         /* XXX endianness */
127         lmm->lmm_magic = (lsm->lsm_magic);
128         lmm->lmm_object_id = (lsm->lsm_object_id);
129         LASSERT(lsm->lsm_object_id);
130         lmm->lmm_stripe_size = (lsm->lsm_stripe_size);
131         lmm->lmm_stripe_offset = (lsm->lsm_stripe_offset);
132         lmm->lmm_ost_count = (ost_count);
133
134         /* Only fill in the object ids which we are actually using.
135          * Assumes lmm_objects is otherwise zero-filled. */
136         for (i = 0, loi = lsm->lsm_oinfo; i < stripe_count; i++, loi++) {
137                 /* XXX call down to osc_packmd() to do the packing */
138                 LASSERT(loi->loi_id);
139                 lmm->lmm_objects[loi->loi_ost_idx].l_object_id = (loi->loi_id);
140         }
141
142         RETURN(lmm_size);
143 }
144
145 static int lov_get_stripecnt(struct lov_obd *lov, int stripe_count)
146 {
147         if (!stripe_count)
148                 stripe_count = lov->desc.ld_default_stripe_count;
149         if (!stripe_count || stripe_count > lov->desc.ld_active_tgt_count)
150                 stripe_count = lov->desc.ld_active_tgt_count;
151
152         return stripe_count;
153 }
154
155 int lov_unpackmd(struct lustre_handle *conn, struct lov_stripe_md **lsmp,
156                  struct lov_mds_md *lmm)
157 {
158         struct obd_device *obd = class_conn2obd(conn);
159         struct lov_obd *lov = &obd->u.lov;
160         struct lov_stripe_md *lsm;
161         struct lov_oinfo *loi;
162         int ost_count;
163         int ost_offset = 0;
164         int stripe_count;
165         int lsm_size;
166         int i;
167         ENTRY;
168
169         if (lmm) {
170                 /* endianness */
171                 if (lmm->lmm_magic != LOV_MAGIC) {
172                         CERROR("bad wire LOV MAGIC: %#08x != %#08x\n",
173                                lmm->lmm_magic, LOV_MAGIC);
174                         RETURN(-EINVAL);
175                 }
176                 stripe_count = (lmm->lmm_stripe_count);
177                 LASSERT(stripe_count);
178         } else
179                 stripe_count = lov_get_stripecnt(lov, 0);
180
181         /* XXX LOV STACKING call into osc for sizes */
182         lsm_size = lov_stripe_md_size(stripe_count);
183
184         if (!lsmp)
185                 RETURN(lsm_size);
186
187         if (*lsmp && !lmm) {
188                 stripe_count = (*lsmp)->lsm_stripe_count;
189                 OBD_FREE(*lsmp, lov_stripe_md_size(stripe_count));
190                 *lsmp = NULL;
191                 RETURN(0);
192         }
193
194         if (!*lsmp) {
195                 OBD_ALLOC(*lsmp, lsm_size);
196                 if (!*lsmp)
197                         RETURN(-ENOMEM);
198         }
199
200         lsm = *lsmp;
201
202         lsm->lsm_stripe_count = stripe_count;
203         if (!lmm)
204                 RETURN(lsm_size);
205
206         /* XXX endianness */
207         ost_offset = lsm->lsm_stripe_offset = (lmm->lmm_stripe_offset);
208         lsm->lsm_magic = (lmm->lmm_magic);
209         lsm->lsm_object_id = (lmm->lmm_object_id);
210         lsm->lsm_stripe_size = (lmm->lmm_stripe_size);
211
212         ost_count = (lmm->lmm_ost_count);
213
214         LMM_ASSERT(lsm->lsm_object_id);
215         LMM_ASSERT(ost_count);
216
217         for (i = 0, loi = lsm->lsm_oinfo; i < ost_count; i++, ost_offset++) {
218                 ost_offset %= ost_count;
219
220                 if (!lmm->lmm_objects[ost_offset].l_object_id)
221                         continue;
222
223                 LMM_ASSERT(loi - lsm->lsm_oinfo < stripe_count);
224                 /* XXX LOV STACKING call down to osc_unpackmd() */
225                 loi->loi_id = (lmm->lmm_objects[ost_offset].l_object_id);
226                 loi->loi_ost_idx = ost_offset;
227                 loi++;
228         }
229         LMM_ASSERT(loi - lsm->lsm_oinfo > 0);
230         LMM_ASSERT(loi - lsm->lsm_oinfo == stripe_count);
231
232         RETURN(lsm_size);
233 }
234
235 /* Configure object striping information on a new file.
236  *
237  * @lmmu is a pointer to a user struct with one or more of the fields set to
238  * indicate the application preference: lmm_stripe_count, lmm_stripe_size,
239  * lmm_stripe_offset, and lmm_stripe_pattern.  lmm_magic must be LOV_MAGIC.
240  * @lsmp is a pointer to an in-core stripe MD that needs to be filled in.
241  */
242 int lov_setstripe(struct lustre_handle *conn, struct lov_stripe_md **lsmp,
243                   struct lov_mds_md *lmmu)
244 {
245         struct obd_device *obd = class_conn2obd(conn);
246         struct lov_obd *lov = &obd->u.lov;
247         struct lov_mds_md lmm;
248         struct lov_stripe_md *lsm;
249         int stripe_count;
250         int rc;
251         ENTRY;
252
253         rc = copy_from_user(&lmm, lmmu, sizeof(lmm));
254         if (rc)
255                 RETURN(-EFAULT);
256
257         if (lmm.lmm_magic != LOV_MAGIC) {
258                 CERROR("bad wire LOV MAGIC: %#08x != %#08x\n",
259                        lmm.lmm_magic, LOV_MAGIC);
260                 RETURN(-EINVAL);
261         }
262         if (lmm.lmm_stripe_count > lov->desc.ld_tgt_count) {
263                 CERROR("stripe count %d more than OST count %d\n",
264                        (int)lmm.lmm_stripe_count, lov->desc.ld_tgt_count);
265                 RETURN(-EINVAL);
266         }
267         if (lmm.lmm_stripe_offset >= lov->desc.ld_tgt_count) {
268                 CERROR("stripe offset %d more than max OST index %d\n",
269                        (int)lmm.lmm_stripe_count, lov->desc.ld_tgt_count);
270                 RETURN(-EINVAL);
271         }
272         if (lmm.lmm_stripe_size & (PAGE_SIZE - 1)) {
273                 CERROR("stripe size %u not multiple of %lu\n",
274                        lmm.lmm_stripe_size, PAGE_SIZE);
275                 RETURN(-EINVAL);
276         }
277         if (lmm.lmm_stripe_size * lmm.lmm_stripe_count > ~0UL) {
278                 CERROR("stripe width %ux%u > %lu on 32-bit system\n",
279                        lmm.lmm_stripe_size, (int)lmm.lmm_stripe_count, ~0UL);
280                 RETURN(-EINVAL);
281         }
282
283         stripe_count = lov_get_stripecnt(lov, lmm.lmm_stripe_count);
284
285         /* XXX LOV STACKING call into osc for sizes */
286         OBD_ALLOC(lsm, lov_stripe_md_size(stripe_count));
287         if (!lsm)
288                 RETURN(-ENOMEM);
289
290         lsm->lsm_magic = LOV_MAGIC;
291         /* This is all validated in lov_create() */
292         lsm->lsm_stripe_count = stripe_count;
293         lsm->lsm_stripe_offset = lmm.lmm_stripe_offset;
294         lsm->lsm_stripe_size = lmm.lmm_stripe_size;
295
296         *lsmp = lsm;
297
298         RETURN(rc);
299 }
300
301 /* Retrieve object striping information.
302  *
303  * @lmmu is a pointer to an in-core struct with lmm_ost_count indicating
304  * the maximum number of OST indices which will fit in the user buffer.
305  * lmm_magic must be LOV_MAGIC.
306  */
307 int lov_getstripe(struct lustre_handle *conn, struct lov_stripe_md *lsm,
308                   struct lov_mds_md *lmmu)
309 {
310         struct obd_device *obd = class_conn2obd(conn);
311         struct lov_obd *lov = &obd->u.lov;
312         struct lov_mds_md lmm, *lmmk = NULL;
313         int ost_count, rc, lmm_size;
314         ENTRY;
315
316         if (!lsm)
317                 RETURN(-ENODATA);
318
319         rc = copy_from_user(&lmm, lmmu, sizeof(lmm));
320         if (rc)
321                 RETURN(-EFAULT);
322
323         if (lmm.lmm_magic != LOV_MAGIC)
324                 RETURN(-EINVAL);
325
326         ost_count = lov->desc.ld_tgt_count;
327
328         /* XXX we _could_ check if indices > user lmm_ost_count are zero */
329         if (lmm.lmm_ost_count < ost_count)
330                 RETURN(-EOVERFLOW);
331
332         rc = lov_packmd(conn, &lmmk, lsm);
333         if (rc < 0)
334                 RETURN(rc);
335
336         lmm_size = rc;
337         rc = 0;
338
339         if (lmm_size && copy_to_user(lmmu, lmmk, lmm_size))
340                 rc = -EFAULT;
341
342         obd_free_wiremd(conn, &lmmk);
343
344         RETURN(rc);
345 }