2 Unix SMB/CIFS implementation.
4 POSIX NTVFS backend - xattr support using a tdb
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 "vfs_posix.h"
26 #define XATTR_LIST_ATTR ".xattr_list"
29 we need to maintain a list of attributes on each file, so that unlink
30 can automatically clean them up
32 static NTSTATUS xattr_tdb_add_list(struct pvfs_state *pvfs, const char *attr_name,
33 const char *fname, int fd)
41 if (strcmp(attr_name, XATTR_LIST_ATTR) == 0) {
45 mem_ctx = talloc(pvfs, 0);
47 status = pull_xattr_blob_tdb(pvfs, mem_ctx, XATTR_LIST_ATTR,
48 fname, fd, 100, &blob);
49 if (!NT_STATUS_IS_OK(status)) {
50 blob = data_blob(NULL, 0);
53 for (s=blob.data; s < (char *)(blob.data+blob.length); s += strlen(s) + 1) {
54 if (strcmp(attr_name, s) == 0) {
60 len = strlen(attr_name) + 1;
62 blob.data = talloc_realloc_p(mem_ctx, blob.data, uint8_t, blob.length + len);
63 if (blob.data == NULL) {
65 return NT_STATUS_NO_MEMORY;
67 memcpy(blob.data + blob.length, attr_name, len);
70 status = push_xattr_blob_tdb(pvfs, XATTR_LIST_ATTR, fname, fd, &blob);
77 form a key for using in the ea_db
79 static NTSTATUS get_ea_db_key(TALLOC_CTX *mem_ctx,
80 const char *attr_name,
81 const char *fname, int fd,
85 size_t len = strlen(attr_name);
88 if (stat(fname, &st) == -1) {
89 return NT_STATUS_NOT_FOUND;
92 if (fstat(fd, &st) == -1) {
93 return NT_STATUS_NOT_FOUND;
97 key->dptr = talloc_array_p(mem_ctx, char, 16 + len);
98 if (key->dptr == NULL) {
99 return NT_STATUS_NO_MEMORY;
101 key->dsize = 16 + len;
103 SBVAL(key->dptr, 0, st.st_dev);
104 SBVAL(key->dptr, 8, st.st_ino);
105 memcpy(key->dptr+16, attr_name, len);
111 pull a xattr as a blob, using the ea_db tdb
113 NTSTATUS pull_xattr_blob_tdb(struct pvfs_state *pvfs,
115 const char *attr_name,
118 size_t estimated_size,
121 TDB_DATA tkey, tdata;
124 status = get_ea_db_key(mem_ctx, attr_name, fname, fd, &tkey);
125 if (!NT_STATUS_IS_OK(status)) {
129 tdata = tdb_fetch(pvfs->ea_db->tdb, tkey);
130 if (tdata.dptr == NULL) {
131 return NT_STATUS_NOT_FOUND;
134 *blob = data_blob_talloc(mem_ctx, tdata.dptr, tdata.dsize);
136 if (blob->data == NULL) {
137 return NT_STATUS_NO_MEMORY;
144 push a xattr as a blob, using ea_db
146 NTSTATUS push_xattr_blob_tdb(struct pvfs_state *pvfs,
147 const char *attr_name,
150 const DATA_BLOB *blob)
152 TDB_DATA tkey, tdata;
155 status = get_ea_db_key(pvfs, attr_name, fname, fd, &tkey);
156 if (!NT_STATUS_IS_OK(status)) {
160 tdata.dptr = blob->data;
161 tdata.dsize = blob->length;
163 if (tdb_chainlock(pvfs->ea_db->tdb, tkey) != 0) {
164 talloc_free(tkey.dptr);
165 return NT_STATUS_INTERNAL_DB_CORRUPTION;
168 status = xattr_tdb_add_list(pvfs, attr_name, fname, fd);
169 if (!NT_STATUS_IS_OK(status)) {
173 if (tdb_store(pvfs->ea_db->tdb, tkey, tdata, TDB_REPLACE) == -1) {
174 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
178 tdb_chainunlock(pvfs->ea_db->tdb, tkey);
179 talloc_free(tkey.dptr);
187 NTSTATUS delete_xattr_tdb(struct pvfs_state *pvfs, const char *attr_name,
188 const char *fname, int fd)
193 status = get_ea_db_key(NULL, attr_name, fname, fd, &tkey);
194 if (!NT_STATUS_IS_OK(status)) {
198 if (tdb_delete(pvfs->ea_db->tdb, tkey) == -1) {
199 talloc_free(tkey.dptr);
200 return NT_STATUS_INTERNAL_DB_CORRUPTION;
203 talloc_free(tkey.dptr);
210 delete all xattrs for a file
212 NTSTATUS unlink_xattr_tdb(struct pvfs_state *pvfs, const char *fname)
214 TALLOC_CTX *mem_ctx = talloc(pvfs, 0);
219 status = pull_xattr_blob_tdb(pvfs, mem_ctx, XATTR_LIST_ATTR,
220 fname, -1, 100, &blob);
221 if (!NT_STATUS_IS_OK(status)) {
222 talloc_free(mem_ctx);
226 for (s=blob.data; s < (char *)(blob.data+blob.length); s += strlen(s) + 1) {
227 delete_xattr_tdb(pvfs, s, fname, -1);
230 return delete_xattr_tdb(pvfs, XATTR_LIST_ATTR, fname, -1);