Whamcloud - gitweb
LU-16118 build: Workaround __write_overflow_field errors
[fs/lustre-release.git] / lustre / obdclass / md_attrs.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 021110-1307, USA
20  *
21  * GPL HEADER END
22  */
23 /*
24  * Copyright (c) 2012, 2017, Intel Corporation.
25  * Use is subject to license terms.
26  *
27  * Author: Johann Lombardi <johann.lombardi@intel.com>
28  */
29 #include <llog_swab.h>
30 #include <lustre_swab.h>
31 #include <obd.h>
32 #include <md_object.h>
33
34 /**
35  * Initialize new \a lma. Only fid is stored.
36  *
37  * \param lma - is the new LMA structure to be initialized
38  * \param fid - is the FID of the object this LMA belongs to
39  * \param incompat - features that MDS must understand to access object
40  */
41 void lustre_lma_init(struct lustre_mdt_attrs *lma, const struct lu_fid *fid,
42                      __u32 compat, __u32 incompat)
43 {
44         lma->lma_compat   = compat;
45         lma->lma_incompat = incompat;
46         lma->lma_self_fid = *fid;
47
48         /* If a field is added in struct lustre_mdt_attrs, zero it explicitly
49          * and change the test below. */
50         BUILD_BUG_ON(sizeof(*lma) !=
51                      (offsetof(struct lustre_mdt_attrs, lma_self_fid) +
52                       sizeof(lma->lma_self_fid)));
53 }
54 EXPORT_SYMBOL(lustre_lma_init);
55
56 /**
57  * Swab, if needed, LMA structure which is stored on-disk in little-endian order.
58  *
59  * \param lma - is a pointer to the LMA structure to be swabbed.
60  */
61 void lustre_lma_swab(struct lustre_mdt_attrs *lma)
62 {
63 #ifdef __BIG_ENDIAN
64         __swab32s(&lma->lma_compat);
65         __swab32s(&lma->lma_incompat);
66         lustre_swab_lu_fid(&lma->lma_self_fid);
67 #endif
68 }
69 EXPORT_SYMBOL(lustre_lma_swab);
70
71 void lustre_loa_init(struct lustre_ost_attrs *loa, const struct lu_fid *fid,
72                      __u32 compat, __u32 incompat)
73 {
74         BUILD_BUG_ON(sizeof(*loa) != LMA_OLD_SIZE);
75
76         memset_startat(loa, 0, loa_parent_fid);
77         lustre_lma_init(&loa->loa_lma, fid, compat, incompat);
78 }
79 EXPORT_SYMBOL(lustre_loa_init);
80
81 /**
82  * Swab, if needed, LOA (for OST-object only) structure with LMA EA and PFID EA
83  * combined together are stored on-disk in little-endian order.
84  *
85  * \param[in] loa       - the pointer to the LOA structure to be swabbed.
86  * \param[in] to_cpu    - to indicate swab for CPU order or not.
87  */
88 void lustre_loa_swab(struct lustre_ost_attrs *loa, bool to_cpu)
89 {
90         struct lustre_mdt_attrs *lma = &loa->loa_lma;
91 #ifdef __BIG_ENDIAN
92         __u32 compat = lma->lma_compat;
93 #endif
94
95         lustre_lma_swab(lma);
96 #ifdef __BIG_ENDIAN
97         if (to_cpu)
98                 compat = lma->lma_compat;
99
100         if (compat & LMAC_STRIPE_INFO) {
101                 lustre_swab_lu_fid(&loa->loa_parent_fid);
102                 __swab32s(&loa->loa_stripe_size);
103         }
104         if (compat & LMAC_COMP_INFO) {
105                 __swab32s(&loa->loa_comp_id);
106                 __swab64s(&loa->loa_comp_start);
107                 __swab64s(&loa->loa_comp_end);
108         }
109 #endif
110 }
111 EXPORT_SYMBOL(lustre_loa_swab);
112
113 /**
114  * Swab, if needed, SOM structure which is stored on-disk in little-endian
115  * order.
116  *
117  * \param attrs - is a pointer to the SOM structure to be swabbed.
118  */
119 void lustre_som_swab(struct lustre_som_attrs *attrs)
120 {
121 #ifdef __BIG_ENDIAN
122         __swab16s(&attrs->lsa_valid);
123         __swab64s(&attrs->lsa_size);
124         __swab64s(&attrs->lsa_blocks);
125 #endif
126 }
127 EXPORT_SYMBOL(lustre_som_swab);
128
129 /**
130  * Swab, if needed, HSM structure which is stored on-disk in little-endian
131  * order.
132  *
133  * \param attrs - is a pointer to the HSM structure to be swabbed.
134  */
135 void lustre_hsm_swab(struct hsm_attrs *attrs)
136 {
137 #ifdef __BIG_ENDIAN
138         __swab32s(&attrs->hsm_compat);
139         __swab32s(&attrs->hsm_flags);
140         __swab64s(&attrs->hsm_arch_id);
141         __swab64s(&attrs->hsm_arch_ver);
142 #endif
143 }
144
145 /*
146  * Swab and extract HSM attributes from on-disk xattr.
147  *
148  * \param buf - is a buffer containing the on-disk HSM extended attribute.
149  * \param rc  - is the HSM xattr stored in \a buf
150  * \param mh  - is the md_hsm structure where to extract HSM attributes.
151  */
152 int lustre_buf2hsm(void *buf, int rc, struct md_hsm *mh)
153 {
154         struct hsm_attrs *attrs = (struct hsm_attrs *)buf;
155         ENTRY;
156
157         if (rc == 0 ||  rc == -ENODATA)
158                 /* no HSM attributes */
159                 RETURN(-ENODATA);
160
161         if (rc < 0)
162                 /* error hit while fetching xattr */
163                 RETURN(rc);
164
165         /* unpack HSM attributes */
166         lustre_hsm_swab(attrs);
167
168         /* fill md_hsm structure */
169         mh->mh_compat   = attrs->hsm_compat;
170         mh->mh_flags    = attrs->hsm_flags;
171         mh->mh_arch_id  = attrs->hsm_arch_id;
172         mh->mh_arch_ver = attrs->hsm_arch_ver;
173
174         RETURN(0);
175 }
176 EXPORT_SYMBOL(lustre_buf2hsm);
177
178 /*
179  * Pack HSM attributes.
180  *
181  * \param buf - is the output buffer where to pack the on-disk HSM xattr.
182  * \param mh  - is the md_hsm structure to pack.
183  */
184 void lustre_hsm2buf(void *buf, const struct md_hsm *mh)
185 {
186         struct hsm_attrs *attrs = (struct hsm_attrs *)buf;
187         ENTRY;
188
189         /* copy HSM attributes */
190         attrs->hsm_compat   = mh->mh_compat;
191         attrs->hsm_flags    = mh->mh_flags;
192         attrs->hsm_arch_id  = mh->mh_arch_id;
193         attrs->hsm_arch_ver = mh->mh_arch_ver;
194
195         /* pack xattr */
196         lustre_hsm_swab(attrs);
197 }
198 EXPORT_SYMBOL(lustre_hsm2buf);