| 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, 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 | #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 incompat) |
| 43 | { |
| 44 | lma->lma_compat = 0; |
| 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 | LASSERT(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 | /* Use LUSTRE_MSG_MAGIC to detect local endianess. */ |
| 64 | if (LUSTRE_MSG_MAGIC != cpu_to_le32(LUSTRE_MSG_MAGIC)) { |
| 65 | __swab32s(&lma->lma_compat); |
| 66 | __swab32s(&lma->lma_incompat); |
| 67 | lustre_swab_lu_fid(&lma->lma_self_fid); |
| 68 | } |
| 69 | }; |
| 70 | EXPORT_SYMBOL(lustre_lma_swab); |
| 71 | |
| 72 | /** |
| 73 | * Swab, if needed, SOM structure which is stored on-disk in little-endian |
| 74 | * order. |
| 75 | * |
| 76 | * \param attrs - is a pointer to the SOM structure to be swabbed. |
| 77 | */ |
| 78 | void lustre_som_swab(struct som_attrs *attrs) |
| 79 | { |
| 80 | /* Use LUSTRE_MSG_MAGIC to detect local endianess. */ |
| 81 | if (LUSTRE_MSG_MAGIC != cpu_to_le32(LUSTRE_MSG_MAGIC)) { |
| 82 | __swab32s(&attrs->som_compat); |
| 83 | __swab32s(&attrs->som_incompat); |
| 84 | __swab64s(&attrs->som_ioepoch); |
| 85 | __swab64s(&attrs->som_size); |
| 86 | __swab64s(&attrs->som_blocks); |
| 87 | __swab64s(&attrs->som_mountid); |
| 88 | } |
| 89 | }; |
| 90 | EXPORT_SYMBOL(lustre_som_swab); |
| 91 | |
| 92 | /* |
| 93 | * Swab and extract SOM attributes from on-disk xattr. |
| 94 | * |
| 95 | * \param buf - is a buffer containing the on-disk SOM extended attribute. |
| 96 | * \param rc - is the SOM xattr stored in \a buf |
| 97 | * \param msd - is the md_som_data structure where to extract SOM attributes. |
| 98 | */ |
| 99 | int lustre_buf2som(void *buf, int rc, struct md_som_data *msd) |
| 100 | { |
| 101 | struct som_attrs *attrs = (struct som_attrs *)buf; |
| 102 | |
| 103 | if (rc == 0 || rc == -ENODATA) |
| 104 | /* no SOM attributes */ |
| 105 | return -ENODATA; |
| 106 | |
| 107 | if (rc < 0) |
| 108 | /* error hit while fetching xattr */ |
| 109 | return rc; |
| 110 | |
| 111 | /* check SOM compatibility */ |
| 112 | if (attrs->som_incompat & ~cpu_to_le32(SOM_INCOMPAT_SUPP)) |
| 113 | return -ENODATA; |
| 114 | |
| 115 | /* unpack SOM attributes */ |
| 116 | lustre_som_swab(attrs); |
| 117 | |
| 118 | /* fill in-memory msd structure */ |
| 119 | msd->msd_compat = attrs->som_compat; |
| 120 | msd->msd_incompat = attrs->som_incompat; |
| 121 | msd->msd_ioepoch = attrs->som_ioepoch; |
| 122 | msd->msd_size = attrs->som_size; |
| 123 | msd->msd_blocks = attrs->som_blocks; |
| 124 | msd->msd_mountid = attrs->som_mountid; |
| 125 | |
| 126 | return 0; |
| 127 | } |
| 128 | EXPORT_SYMBOL(lustre_buf2som); |
| 129 | |
| 130 | /** |
| 131 | * Swab, if needed, HSM structure which is stored on-disk in little-endian |
| 132 | * order. |
| 133 | * |
| 134 | * \param attrs - is a pointer to the HSM structure to be swabbed. |
| 135 | */ |
| 136 | void lustre_hsm_swab(struct hsm_attrs *attrs) |
| 137 | { |
| 138 | /* Use LUSTRE_MSG_MAGIC to detect local endianess. */ |
| 139 | if (LUSTRE_MSG_MAGIC != cpu_to_le32(LUSTRE_MSG_MAGIC)) { |
| 140 | __swab32s(&attrs->hsm_compat); |
| 141 | __swab32s(&attrs->hsm_flags); |
| 142 | __swab64s(&attrs->hsm_arch_id); |
| 143 | __swab64s(&attrs->hsm_arch_ver); |
| 144 | } |
| 145 | }; |
| 146 | EXPORT_SYMBOL(lustre_hsm_swab); |
| 147 | |
| 148 | /* |
| 149 | * Swab and extract HSM attributes from on-disk xattr. |
| 150 | * |
| 151 | * \param buf - is a buffer containing the on-disk HSM extended attribute. |
| 152 | * \param rc - is the HSM xattr stored in \a buf |
| 153 | * \param mh - is the md_hsm structure where to extract HSM attributes. |
| 154 | */ |
| 155 | int lustre_buf2hsm(void *buf, int rc, struct md_hsm *mh) |
| 156 | { |
| 157 | struct hsm_attrs *attrs = (struct hsm_attrs *)buf; |
| 158 | |
| 159 | if (rc == 0 || rc == -ENODATA) |
| 160 | /* no HSM attributes */ |
| 161 | return -ENODATA; |
| 162 | |
| 163 | if (rc < 0) |
| 164 | /* error hit while fetching xattr */ |
| 165 | return rc; |
| 166 | |
| 167 | /* unpack HSM attributes */ |
| 168 | lustre_hsm_swab(attrs); |
| 169 | |
| 170 | /* fill md_hsm structure */ |
| 171 | mh->mh_compat = attrs->hsm_compat; |
| 172 | mh->mh_flags = attrs->hsm_flags; |
| 173 | mh->mh_arch_id = attrs->hsm_arch_id; |
| 174 | mh->mh_arch_ver = attrs->hsm_arch_ver; |
| 175 | |
| 176 | return 0; |
| 177 | } |
| 178 | EXPORT_SYMBOL(lustre_buf2hsm); |
| 179 | |
| 180 | /* |
| 181 | * Pack HSM attributes. |
| 182 | * |
| 183 | * \param buf - is the output buffer where to pack the on-disk HSM xattr. |
| 184 | * \param mh - is the md_hsm structure to pack. |
| 185 | */ |
| 186 | void lustre_hsm2buf(void *buf, struct md_hsm *mh) |
| 187 | { |
| 188 | struct hsm_attrs *attrs = (struct hsm_attrs *)buf; |
| 189 | |
| 190 | /* copy HSM attributes */ |
| 191 | attrs->hsm_compat = mh->mh_compat; |
| 192 | attrs->hsm_flags = mh->mh_flags; |
| 193 | attrs->hsm_arch_id = mh->mh_arch_id; |
| 194 | attrs->hsm_arch_ver = mh->mh_arch_ver; |
| 195 | |
| 196 | /* pack xattr */ |
| 197 | lustre_hsm_swab(attrs); |
| 198 | } |
| 199 | EXPORT_SYMBOL(lustre_hsm2buf); |