Whamcloud - gitweb
LU-8851 nodemap: add uid/gid only flags to control mapping
[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, 2016, Intel Corporation.
25  * Use is subject to license terms.
26  *
27  * Author: Johann Lombardi <johann.lombardi@intel.com>
28  */
29
30 #include <lustre/lustre_idl.h>
31
32 #include <llog_swab.h>
33 #include <lustre_swab.h>
34 #include <obd.h>
35 #include <md_object.h>
36
37 /**
38  * Initialize new \a lma. Only fid is stored.
39  *
40  * \param lma - is the new LMA structure to be initialized
41  * \param fid - is the FID of the object this LMA belongs to
42  * \param incompat - features that MDS must understand to access object
43  */
44 void lustre_lma_init(struct lustre_mdt_attrs *lma, const struct lu_fid *fid,
45                      __u32 compat, __u32 incompat)
46 {
47         lma->lma_compat   = compat;
48         lma->lma_incompat = incompat;
49         lma->lma_self_fid = *fid;
50
51         /* If a field is added in struct lustre_mdt_attrs, zero it explicitly
52          * and change the test below. */
53         LASSERT(sizeof(*lma) ==
54                 (offsetof(struct lustre_mdt_attrs, lma_self_fid) +
55                  sizeof(lma->lma_self_fid)));
56 }
57 EXPORT_SYMBOL(lustre_lma_init);
58
59 /**
60  * Swab, if needed, LMA structure which is stored on-disk in little-endian order.
61  *
62  * \param lma - is a pointer to the LMA structure to be swabbed.
63  */
64 void lustre_lma_swab(struct lustre_mdt_attrs *lma)
65 {
66 #ifdef __BIG_ENDIAN
67         __swab32s(&lma->lma_compat);
68         __swab32s(&lma->lma_incompat);
69         lustre_swab_lu_fid(&lma->lma_self_fid);
70 #endif
71 }
72 EXPORT_SYMBOL(lustre_lma_swab);
73
74 void lustre_loa_init(struct lustre_ost_attrs *loa, const struct lu_fid *fid,
75                      __u32 compat, __u32 incompat)
76 {
77         CLASSERT(sizeof(*loa) == LMA_OLD_SIZE);
78
79         memset(&loa->loa_parent_fid, 0,
80                sizeof(*loa) - offsetof(typeof(*loa), loa_parent_fid));
81         lustre_lma_init(&loa->loa_lma, fid, compat, incompat);
82 }
83 EXPORT_SYMBOL(lustre_loa_init);
84
85 /**
86  * Swab, if needed, LOA (for OST-object only) structure with LMA EA and PFID EA
87  * combined together are stored on-disk in little-endian order.
88  *
89  * \param[in] loa       - the pointer to the LOA structure to be swabbed.
90  * \param[in] to_cpu    - to indicate swab for CPU order or not.
91  */
92 void lustre_loa_swab(struct lustre_ost_attrs *loa, bool to_cpu)
93 {
94         struct lustre_mdt_attrs *lma = &loa->loa_lma;
95 #ifdef __BIG_ENDIAN
96         __u32 compat = lma->lma_compat;
97 #endif
98
99         lustre_lma_swab(lma);
100 #ifdef __BIG_ENDIAN
101         if (to_cpu)
102                 compat = lma->lma_compat;
103
104         if (compat & LMAC_STRIPE_INFO) {
105                 lustre_swab_lu_fid(&loa->loa_parent_fid);
106                 __swab32s(&loa->loa_stripe_size);
107         }
108         if (compat & LMAC_COMP_INFO) {
109                 __swab32s(&loa->loa_comp_id);
110                 __swab64s(&loa->loa_comp_start);
111                 __swab64s(&loa->loa_comp_end);
112         }
113 #endif
114 }
115 EXPORT_SYMBOL(lustre_loa_swab);
116
117 /**
118  * Swab, if needed, HSM structure which is stored on-disk in little-endian
119  * order.
120  *
121  * \param attrs - is a pointer to the HSM structure to be swabbed.
122  */
123 void lustre_hsm_swab(struct hsm_attrs *attrs)
124 {
125 #ifdef __BIG_ENDIAN
126         __swab32s(&attrs->hsm_compat);
127         __swab32s(&attrs->hsm_flags);
128         __swab64s(&attrs->hsm_arch_id);
129         __swab64s(&attrs->hsm_arch_ver);
130 #endif
131 }
132
133 /*
134  * Swab and extract HSM attributes from on-disk xattr.
135  *
136  * \param buf - is a buffer containing the on-disk HSM extended attribute.
137  * \param rc  - is the HSM xattr stored in \a buf
138  * \param mh  - is the md_hsm structure where to extract HSM attributes.
139  */
140 int lustre_buf2hsm(void *buf, int rc, struct md_hsm *mh)
141 {
142         struct hsm_attrs *attrs = (struct hsm_attrs *)buf;
143         ENTRY;
144
145         if (rc == 0 ||  rc == -ENODATA)
146                 /* no HSM attributes */
147                 RETURN(-ENODATA);
148
149         if (rc < 0)
150                 /* error hit while fetching xattr */
151                 RETURN(rc);
152
153         /* unpack HSM attributes */
154         lustre_hsm_swab(attrs);
155
156         /* fill md_hsm structure */
157         mh->mh_compat   = attrs->hsm_compat;
158         mh->mh_flags    = attrs->hsm_flags;
159         mh->mh_arch_id  = attrs->hsm_arch_id;
160         mh->mh_arch_ver = attrs->hsm_arch_ver;
161
162         RETURN(0);
163 }
164 EXPORT_SYMBOL(lustre_buf2hsm);
165
166 /*
167  * Pack HSM attributes.
168  *
169  * \param buf - is the output buffer where to pack the on-disk HSM xattr.
170  * \param mh  - is the md_hsm structure to pack.
171  */
172 void lustre_hsm2buf(void *buf, const struct md_hsm *mh)
173 {
174         struct hsm_attrs *attrs = (struct hsm_attrs *)buf;
175         ENTRY;
176
177         /* copy HSM attributes */
178         attrs->hsm_compat   = mh->mh_compat;
179         attrs->hsm_flags    = mh->mh_flags;
180         attrs->hsm_arch_id  = mh->mh_arch_id;
181         attrs->hsm_arch_ver = mh->mh_arch_ver;
182
183         /* pack xattr */
184         lustre_hsm_swab(attrs);
185 }
186 EXPORT_SYMBOL(lustre_hsm2buf);