2 Unix SMB/CIFS implementation.
4 POSIX NTVFS backend - xattr support
6 Copyright (C) Andrew Tridgell 2004
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "system/filesys.h"
25 #include "vfs_posix.h"
26 #include "librpc/gen_ndr/ndr_xattr.h"
29 pull a xattr as a blob, from either a file or a file descriptor
31 static NTSTATUS pull_xattr_blob(struct pvfs_state *pvfs,
33 const char *attr_name,
36 size_t estimated_size,
39 #if HAVE_XATTR_SUPPORT
42 *blob = data_blob_talloc(mem_ctx, NULL, estimated_size);
43 if (blob->data == NULL) {
44 return NT_STATUS_NO_MEMORY;
49 ret = fgetxattr(fd, attr_name, blob->data, estimated_size);
51 ret = getxattr(fname, attr_name, blob->data, estimated_size);
53 if (ret == -1 && errno == ERANGE) {
55 blob->data = talloc_realloc(mem_ctx, blob->data, estimated_size);
56 if (blob->data == NULL) {
57 return NT_STATUS_NO_MEMORY;
59 blob->length = estimated_size;
65 return pvfs_map_errno(pvfs, errno);
72 return NT_STATUS_NOT_SUPPORTED;
77 push a xattr as a blob, from either a file or a file descriptor
79 static NTSTATUS push_xattr_blob(struct pvfs_state *pvfs,
80 const char *attr_name,
83 const DATA_BLOB *blob)
85 #if HAVE_XATTR_SUPPORT
89 ret = fsetxattr(fd, attr_name, blob->data, blob->length, 0);
91 ret = setxattr(fname, attr_name, blob->data, blob->length, 0);
94 return pvfs_map_errno(pvfs, errno);
99 return NT_STATUS_NOT_SUPPORTED;
104 load a NDR structure from a xattr
106 static NTSTATUS pvfs_xattr_ndr_load(struct pvfs_state *pvfs,
108 const char *fname, int fd, const char *attr_name,
109 void *p, ndr_pull_flags_fn_t pull_fn)
114 status = pull_xattr_blob(pvfs, mem_ctx, attr_name, fname,
115 fd, XATTR_DOSATTRIB_ESTIMATED_SIZE, &blob);
116 if (!NT_STATUS_IS_OK(status)) {
121 status = ndr_pull_struct_blob(&blob, mem_ctx, p, pull_fn);
123 data_blob_free(&blob);
129 save a NDR structure into a xattr
131 static NTSTATUS pvfs_xattr_ndr_save(struct pvfs_state *pvfs,
132 const char *fname, int fd, const char *attr_name,
133 void *p, ndr_push_flags_fn_t push_fn)
135 TALLOC_CTX *mem_ctx = talloc(NULL, 0);
139 status = ndr_push_struct_blob(&blob, mem_ctx, p, (ndr_push_flags_fn_t)push_fn);
140 if (!NT_STATUS_IS_OK(status)) {
141 talloc_free(mem_ctx);
145 status = push_xattr_blob(pvfs, attr_name, fname, fd, &blob);
146 talloc_free(mem_ctx);
153 fill in file attributes from extended attributes
155 NTSTATUS pvfs_dosattrib_load(struct pvfs_state *pvfs, struct pvfs_filename *name, int fd)
158 struct xattr_DosAttrib attrib;
159 TALLOC_CTX *mem_ctx = talloc(name, 0);
160 struct xattr_DosInfo1 *info1;
162 if (!(pvfs->flags & PVFS_FLAG_XATTR_ENABLE)) {
166 status = pvfs_xattr_ndr_load(pvfs, mem_ctx, name->full_name,
167 fd, XATTR_DOSATTRIB_NAME,
169 (ndr_pull_flags_fn_t)ndr_pull_xattr_DosAttrib);
171 /* if the filesystem doesn't support them, then tell pvfs not to try again */
172 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_SUPPORTED)) {
173 DEBUG(5,("pvfs_xattr: xattr not supported in filesystem\n"));
174 pvfs->flags &= ~PVFS_FLAG_XATTR_ENABLE;
175 talloc_free(mem_ctx);
179 /* not having a DosAttrib is not an error */
180 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
181 talloc_free(mem_ctx);
185 if (!NT_STATUS_IS_OK(status)) {
186 talloc_free(mem_ctx);
190 switch (attrib.version) {
192 info1 = &attrib.info.info1;
193 name->dos.attrib = pvfs_attrib_normalise(info1->attrib);
194 name->dos.ea_size = info1->ea_size;
195 if (name->st.st_size == info1->size) {
196 name->dos.alloc_size = info1->alloc_size;
198 if (info1->create_time != 0) {
199 name->dos.create_time = info1->create_time;
201 if (info1->change_time != 0) {
202 name->dos.change_time = info1->change_time;
207 DEBUG(0,("ERROR: Unsupported xattr DosAttrib version %d on '%s'\n",
208 attrib.version, name->full_name));
209 talloc_free(mem_ctx);
210 return NT_STATUS_INVALID_LEVEL;
213 talloc_free(mem_ctx);
219 save the file attribute into the xattr
221 NTSTATUS pvfs_dosattrib_save(struct pvfs_state *pvfs, struct pvfs_filename *name, int fd)
223 struct xattr_DosAttrib attrib;
224 struct xattr_DosInfo1 *info1;
226 if (!(pvfs->flags & PVFS_FLAG_XATTR_ENABLE)) {
231 info1 = &attrib.info.info1;
233 name->dos.attrib = pvfs_attrib_normalise(name->dos.attrib);
235 info1->attrib = name->dos.attrib;
236 info1->ea_size = name->dos.ea_size;
237 info1->size = name->st.st_size;
238 info1->alloc_size = name->dos.alloc_size;
239 info1->create_time = name->dos.create_time;
240 info1->change_time = name->dos.change_time;
242 return pvfs_xattr_ndr_save(pvfs, name->full_name, fd,
243 XATTR_DOSATTRIB_NAME, &attrib,
244 (ndr_push_flags_fn_t)ndr_push_xattr_DosAttrib);
249 load the set of DOS EAs
251 NTSTATUS pvfs_doseas_load(struct pvfs_state *pvfs, struct pvfs_filename *name, int fd,
252 struct xattr_DosEAs *eas)
256 if (!(pvfs->flags & PVFS_FLAG_XATTR_ENABLE)) {
259 status = pvfs_xattr_ndr_load(pvfs, eas, name->full_name, fd, XATTR_DOSEAS_NAME,
260 eas, (ndr_pull_flags_fn_t)ndr_pull_xattr_DosEAs);
261 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
268 save the set of DOS EAs
270 NTSTATUS pvfs_doseas_save(struct pvfs_state *pvfs, struct pvfs_filename *name, int fd,
271 struct xattr_DosEAs *eas)
273 if (!(pvfs->flags & PVFS_FLAG_XATTR_ENABLE)) {
276 return pvfs_xattr_ndr_save(pvfs, name->full_name, fd, XATTR_DOSEAS_NAME, eas,
277 (ndr_push_flags_fn_t)ndr_push_xattr_DosEAs);