Whamcloud - gitweb
d28a6c90c76797e6b60c64b6d7306f8922029e0f
[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                 stripe_count = lsm->lsm_stripe_count;
64
65         /* XXX LOV STACKING call into osc for sizes */
66         lmm_size = lov_mds_md_size(ost_count);
67
68         if (!lmmp)
69                 RETURN(lmm_size);
70
71         if (*lmmp && !lsm) {
72                 /* endianness */
73                 ost_count = ((*lmmp)->lmm_ost_count);
74                 OBD_FREE(*lmmp, lov_mds_md_size(ost_count));
75                 *lmmp = NULL;
76                 RETURN(0);
77         }
78
79         if (!*lmmp) {
80                 OBD_ALLOC(*lmmp, lmm_size);
81                 if (!*lmmp)
82                         RETURN(-ENOMEM);
83         }
84
85         lmm = *lmmp;
86
87         lmm->lmm_stripe_count = (stripe_count);
88         if (!lsm)
89                 RETURN(lmm_size);
90         /* XXX endianness */
91         lmm->lmm_magic = (lsm->lsm_magic);
92         lmm->lmm_object_id = (lsm->lsm_object_id);
93         lmm->lmm_stripe_size = (lsm->lsm_stripe_size);
94         lmm->lmm_stripe_pattern = (lsm->lsm_stripe_pattern);
95         lmm->lmm_stripe_offset = (lsm->lsm_stripe_offset);
96         lmm->lmm_ost_count = (lov->desc.ld_tgt_count);
97
98         /* Only fill in the object ids which we are actually using.
99          * Assumes lmm_objects is otherwise zero-filled. */
100         for (i = 0, loi = lsm->lsm_oinfo; i < stripe_count; i++, loi++)
101                 /* XXX call down to osc_packmd() to do the packing */
102                 lmm->lmm_objects[loi->loi_ost_idx].l_object_id = (loi->loi_id);
103
104         RETURN(lmm_size);
105 }
106
107 int lov_unpackmd(struct lustre_handle *conn, struct lov_stripe_md **lsmp,
108                  struct lov_mds_md *lmm)
109 {
110         struct obd_device *obd = class_conn2obd(conn);
111         struct lov_obd *lov = &obd->u.lov;
112         struct lov_stripe_md *lsm;
113         struct lov_oinfo *loi;
114         int ost_count = lov->desc.ld_active_tgt_count;
115         int ost_offset = 0;
116         int stripe_count = 0;
117         int lsm_size;
118         int i;
119         ENTRY;
120
121         if (lmm)
122                 /* endianness */
123                 stripe_count = (lmm->lmm_stripe_count);
124
125         if (!stripe_count)
126                 stripe_count = lov->desc.ld_default_stripe_count;
127         if (!stripe_count || stripe_count > ost_count)
128                 stripe_count = ost_count;
129
130         /* XXX LOV STACKING call into osc for sizes */
131         lsm_size = lov_stripe_md_size(stripe_count);
132
133         if (!lsmp)
134                 RETURN(lsm_size);
135
136         if (*lsmp && !lmm) {
137                 stripe_count = (*lsmp)->lsm_stripe_count;
138                 OBD_FREE(*lsmp, lov_stripe_md_size(stripe_count));
139                 *lsmp = NULL;
140                 RETURN(0);
141         }
142
143         if (!*lsmp) {
144                 OBD_ALLOC(*lsmp, lsm_size);
145                 if (!*lsmp)
146                         RETURN(-ENOMEM);
147         }
148
149         lsm = *lsmp;
150
151         lsm->lsm_stripe_count = stripe_count;
152         if (!lmm)
153                 RETURN(lsm_size);
154
155         /* XXX endianness */
156         ost_offset = lsm->lsm_stripe_offset = (lmm->lmm_stripe_offset);
157         lsm->lsm_magic = (lmm->lmm_magic);
158         lsm->lsm_object_id = (lmm->lmm_object_id);
159         lsm->lsm_stripe_size = (lmm->lmm_stripe_size);
160         lsm->lsm_stripe_pattern = (lmm->lmm_stripe_pattern);
161
162         for (i = 0, loi = lsm->lsm_oinfo; i < ost_count; i++, ost_offset++) {
163                 ost_offset %= ost_count;
164
165                 if (!lmm->lmm_objects[ost_offset].l_object_id)
166                         continue;
167
168                 LASSERT(loi - lsm->lsm_oinfo < stripe_count);
169                 /* XXX LOV STACKING call down to osc_unpackmd() */
170                 loi->loi_id = (lmm->lmm_objects[ost_offset].l_object_id);
171                 loi->loi_ost_idx = ost_offset;
172                 loi++;
173         }
174
175         RETURN(lsm_size);
176 }