r4242: added support for storing xattrs in a tdb. This allows all advanced NT
[kamenim/samba.git] / source4 / ntvfs / posix / xattr_tdb.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    POSIX NTVFS backend - xattr support using a tdb
5
6    Copyright (C) Andrew Tridgell 2004
7
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.
12    
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.
17    
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.
21 */
22
23 #include "includes.h"
24 #include "vfs_posix.h"
25
26 #define XATTR_LIST_ATTR ".xattr_list"
27
28 /*
29   we need to maintain a list of attributes on each file, so that unlink
30   can automatically clean them up
31 */
32 static NTSTATUS xattr_tdb_add_list(struct pvfs_state *pvfs, const char *attr_name, 
33                                    const char *fname, int fd)
34 {
35         DATA_BLOB blob;
36         TALLOC_CTX *mem_ctx;
37         const char *s;
38         NTSTATUS status;
39         size_t len;
40
41         if (strcmp(attr_name, XATTR_LIST_ATTR) == 0) {
42                 return NT_STATUS_OK;
43         }
44
45         mem_ctx = talloc(pvfs, 0);
46
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                 talloc_free(mem_ctx);
51                 return NT_STATUS_OK;
52         }
53
54         for (s=blob.data; s < (char *)(blob.data+blob.length); s += strlen(s) + 1) {
55                 if (strcmp(attr_name, s) == 0) {
56                         talloc_free(mem_ctx);
57                         return NT_STATUS_OK;
58                 }
59         }
60
61         len = strlen(attr_name) + 1;
62
63         blob.data = talloc_realloc_p(mem_ctx, blob.data, uint8_t, blob.length + len);
64         if (blob.data == NULL) {
65                 talloc_free(mem_ctx);
66                 return NT_STATUS_NO_MEMORY;             
67         }
68         memcpy(blob.data + blob.length, attr_name, len);
69         blob.length += len;
70
71         status = push_xattr_blob_tdb(pvfs, XATTR_LIST_ATTR, fname, fd, &blob);
72         talloc_free(mem_ctx);
73
74         return status;
75 }
76
77 /*
78   form a key for using in the ea_db
79 */
80 static NTSTATUS get_ea_db_key(TALLOC_CTX *mem_ctx,
81                               const char *attr_name,
82                               const char *fname, int fd, 
83                               TDB_DATA *key)
84 {
85         struct stat st;
86         size_t len = strlen(attr_name);
87
88         if (fd == -1) {
89                 if (stat(fname, &st) == -1) {
90                         return NT_STATUS_NOT_FOUND;
91                 }
92         } else {
93                 if (fstat(fd, &st) == -1) {
94                         return NT_STATUS_NOT_FOUND;
95                 }
96         }
97
98         key->dptr = talloc_array_p(mem_ctx, char, 16 + len);
99         if (key->dptr == NULL) {
100                 return NT_STATUS_NO_MEMORY;
101         }
102         key->dsize = 16 + len;
103
104         SBVAL(key->dptr, 0, st.st_dev);
105         SBVAL(key->dptr, 8, st.st_ino);
106         memcpy(key->dptr+16, attr_name, len);
107         
108         return NT_STATUS_OK;
109 }
110
111 /*
112   pull a xattr as a blob, using the ea_db tdb
113 */
114 NTSTATUS pull_xattr_blob_tdb(struct pvfs_state *pvfs,
115                              TALLOC_CTX *mem_ctx,
116                              const char *attr_name, 
117                              const char *fname, 
118                              int fd, 
119                              size_t estimated_size,
120                              DATA_BLOB *blob)
121 {
122         TDB_DATA tkey, tdata;
123         NTSTATUS status;
124
125         status = get_ea_db_key(mem_ctx, attr_name, fname, fd, &tkey);
126         if (!NT_STATUS_IS_OK(status)) {
127                 return status;
128         }
129
130         tdata = tdb_fetch(pvfs->ea_db->tdb, tkey);
131         if (tdata.dptr == NULL) {
132                 return NT_STATUS_NOT_FOUND;
133         }
134
135         *blob = data_blob_talloc(mem_ctx, tdata.dptr, tdata.dsize);
136         free(tdata.dptr);
137         if (blob->data == NULL) {
138                 return NT_STATUS_NO_MEMORY;
139         }
140
141         return NT_STATUS_OK;    
142 }
143
144 /*
145   push a xattr as a blob, using ea_db
146 */
147 NTSTATUS push_xattr_blob_tdb(struct pvfs_state *pvfs,
148                              const char *attr_name, 
149                              const char *fname, 
150                              int fd, 
151                              const DATA_BLOB *blob)
152 {
153         TDB_DATA tkey, tdata;
154         NTSTATUS status;
155
156         status = get_ea_db_key(pvfs, attr_name, fname, fd, &tkey);
157         if (!NT_STATUS_IS_OK(status)) {
158                 return status;
159         }
160         
161         tdata.dptr = blob->data;
162         tdata.dsize = blob->length;
163
164         if (tdb_chainlock(pvfs->ea_db->tdb, tkey) != 0) {
165                 talloc_free(tkey.dptr);
166                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
167         }
168
169         status = xattr_tdb_add_list(pvfs, attr_name, fname, fd);
170         if (!NT_STATUS_IS_OK(status)) {
171                 goto done;
172         }
173
174         if (tdb_store(pvfs->ea_db->tdb, tkey, tdata, TDB_REPLACE) == -1) {
175                 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
176         }
177
178 done:
179         tdb_chainunlock(pvfs->ea_db->tdb, tkey);
180         talloc_free(tkey.dptr);
181         return status;  
182 }
183
184
185 /*
186   delete a xattr
187 */
188 NTSTATUS delete_xattr_tdb(struct pvfs_state *pvfs, const char *attr_name, 
189                           const char *fname, int fd)
190 {
191         TDB_DATA tkey;
192         NTSTATUS status;
193
194         status = get_ea_db_key(NULL, attr_name, fname, fd, &tkey);
195         if (!NT_STATUS_IS_OK(status)) {
196                 return status;
197         }
198         
199         if (tdb_delete(pvfs->ea_db->tdb, tkey) == -1) {
200                 talloc_free(tkey.dptr);
201                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
202         }
203
204         talloc_free(tkey.dptr);
205         return NT_STATUS_OK;    
206 }
207
208
209
210 /*
211   delete all xattrs for a file
212 */
213 NTSTATUS unlink_xattr_tdb(struct pvfs_state *pvfs, const char *fname)
214 {
215         TALLOC_CTX *mem_ctx = talloc(pvfs, 0);
216         DATA_BLOB blob;
217         const char *s;
218         NTSTATUS status;
219
220         status = pull_xattr_blob_tdb(pvfs, mem_ctx, XATTR_LIST_ATTR, 
221                                      fname, -1, 100, &blob);
222         if (!NT_STATUS_IS_OK(status)) {
223                 talloc_free(mem_ctx);
224                 return NT_STATUS_OK;
225         }
226
227         for (s=blob.data; s < (char *)(blob.data+blob.length); s += strlen(s) + 1) {
228                 delete_xattr_tdb(pvfs, s, fname, -1);
229         }
230
231         return delete_xattr_tdb(pvfs, XATTR_LIST_ATTR, fname, -1);
232 }