aboutsummaryrefslogtreecommitdiffstats
path: root/xattr.c
blob: 3000d7300c61b61c67e04e58a74b537840442975 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*
 * Maintained by Jeff Garzik <jgarzik@pobox.com>
 *
 * Copyright 2006-2007 Red Hat, Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; see the file COPYING.  If not, write to
 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <attr/xattr.h>
#include <db.h>
#include "dbfs.h"

static int dbfs_xattr_list_read(DB_TXN *txn, DBT *key, DBT *val, char *keystr, guint64 ino)
{
	snprintf(keystr, 32, "/xattr/%Lu", (unsigned long long) ino);

	memset(key, 0, sizeof(*key));
	key->data = keystr;
	key->size = strlen(keystr);

	memset(val, 0, sizeof(*val));
	val->flags = DB_DBT_MALLOC;

	return gfs->meta->get(gfs->meta, txn, key, val, 0);
}

static int dbfs_xattr_list_add(DB_TXN *txn, guint64 ino, const char *name)
{
	struct dbfs_xlist *ent;
	char keystr[32];
	DBT key, val;
	size_t alloc_len;
	size_t name_len = strlen(name);
	int rc;

	/* get list from db */
	rc = dbfs_xattr_list_read(txn, &key, &val, keystr, ino);
	if (rc && (rc != DB_NOTFOUND))
		return -EIO;

	alloc_len = dbfs_xlist_next(name_len);

	/* if list not found, create new one */
	if (rc == DB_NOTFOUND) {
		ent = malloc(alloc_len);
		if (!ent)
			return -ENOMEM;

		val.data = ent;
		val.size = alloc_len;
	}

	/* otherwise, append to existing list */
	else {
		void *mem;
		size_t old_size;

		alloc_len += val.size;
		mem = realloc(val.data, alloc_len);
		if (!mem) {
			rc = -ENOMEM;
			goto out;
		}

		old_size = val.size;
		val.data = mem;
		val.size = alloc_len;

		mem += old_size;
		ent = mem;
	}

	/* fill in list entry at tail of list */
	ent->namelen = GUINT32_TO_LE(name_len);
	memcpy(ent->name, name, name_len);

	/* store new list in db */
	rc = gfs->meta->put(gfs->meta, txn, &key, &val, 0) ? -EIO : 0;

out:
	free(val.data);
	return rc;
}

static int dbfs_xattr_list_del(DB_TXN *txn, guint64 ino, const char *name)
{
	size_t name_len = strlen(name);
	struct dbfs_xlist *ent;
	char keystr[32];
	DBT key, val;
	int rc;
	long bytes;
	void *mem;
	size_t ssize = 0;
	unsigned int entries = 0;

	/* get list from db */
	rc = dbfs_xattr_list_read(txn, &key, &val, keystr, ino);
	if (rc == DB_NOTFOUND)
		return -ENOENT;
	if (rc)
		return -EIO;

	/* find entry in list */
	mem = val.data;
	bytes = val.size;
	while (bytes > 0) {
		entries++;
		ent = mem;
		ssize = dbfs_xlist_next(GUINT32_FROM_LE(ent->namelen));
		if (ssize > bytes) {		/* data corrupt */
			rc = -EIO;
			goto out;
		}

		if (!memcmp(ent->name, name, name_len))
			break;

		bytes -= ssize;
	}

	/* if not found, exit */
	if (bytes <= 0) {
		rc = -ENOENT;
		goto out;
	}

	/* if at least one entry will exist post-delete, update db */
	if (entries > 1) {
		/* swallow entry */
		memmove(mem, mem + ssize, bytes - ssize);
		val.size -= ssize;

		/* store new list in db */
		rc = gfs->meta->put(gfs->meta, txn, &key, &val, 0) ? -EIO : 0;
	}

	/* otherwise, delete db entry */
	else
		rc = dbmeta_del(txn, keystr);

out:
	free(val.data);
	return rc;
}

int dbfs_xattr_list(DB_TXN *txn, guint64 ino, void **buf_out, size_t *buflen_out)
{
	struct dbfs_xlist *ent;
	char keystr[32];
	DBT key, val;
	void *mem, *name_list;
	size_t name_list_len, ssize, name_len;
	long bytes;
	char null = 0;
	int rc;

	*buf_out = NULL;
	*buflen_out = 0;

	/* get list from db */
	rc = dbfs_xattr_list_read(txn, &key, &val, keystr, ino);
	if (rc == DB_NOTFOUND)
		return 0;
	if (rc)
		return -EIO;
	if (val.size == 0)
		return 0;

	/* allocate output buffer */
	name_list = malloc(val.size);
	if (!name_list) {
		rc = -ENOMEM;
		goto out;
	}
	name_list_len = 0;

	/* fill output buffer */
	mem = val.data;
	bytes = val.size;
	while (bytes > 0) {
		ent = mem;
		name_len = GUINT32_FROM_LE(ent->namelen);
		ssize = dbfs_xlist_next(name_len);

		if (ssize > bytes) {		/* data corrupt */
			rc = -EIO;
			goto out;
		}

		memcpy(name_list + name_list_len, ent->name, name_len);
		name_list_len += name_len;

		memcpy(name_list + name_list_len, &null, 1);
		name_list_len++;

		bytes -= ssize;
	}

	*buf_out = name_list;
	*buflen_out = name_list_len;

out:
	free(val.data);
	return rc;
}

static int dbfs_xattr_read(DB_TXN *txn, guint64 ino, const char *name, DBT *val)
{
	char key_str[DBFS_XATTR_NAME_LEN + 32];
	DBT key;
	int rc;

	snprintf(key_str, sizeof(key_str),
		 "/xattr/%Lu/%s", (unsigned long long) ino, name);

	memset(&key, 0, sizeof(key));
	key.data = key_str;
	key.size = strlen(key_str);

	memset(val, 0, sizeof(*val));
	val->flags = DB_DBT_MALLOC;

	rc = gfs->meta->get(gfs->meta, txn, &key, val, 0);
	if (rc == DB_NOTFOUND)
		return -EINVAL;
	return rc ? -EIO : 0;
}

static int dbfs_xattr_write(DB_TXN *txn, guint64 ino, const char *name,
			    const void *buf, size_t buflen)
{
	char key_str[DBFS_XATTR_NAME_LEN + 32];
	DBT key, val;

	snprintf(key_str, sizeof(key_str),
		 "/xattr/%Lu/%s", (unsigned long long) ino, name);

	memset(&key, 0, sizeof(key));
	key.data = key_str;
	key.size = strlen(key_str);

	memset(&val, 0, sizeof(val));
	val.data = (void *) buf;
	val.size = buflen;

	return gfs->meta->put(gfs->meta, txn, &key, &val, 0) ? -EIO : 0;
}

int dbfs_xattr_get(DB_TXN *txn, guint64 ino_n, const char *name,
		   void **buf_out, size_t *buflen_out)
{
	int rc;
	DBT val;

	rc = dbfs_xattr_read(txn, ino_n, name, &val);
	if (rc)
		return rc;

	*buf_out = val.data;
	*buflen_out = val.size;

	return 0;
}

int dbfs_xattr_set(DB_TXN *txn, guint64 ino_n, const char *name,
		   const void *buf, size_t buflen, int flags)
{
	void *current = NULL;
	size_t current_len = 0;
	size_t name_len = strlen(name);
	int rc, exists;

	if ((!name) || (!*name) || (name_len > DBFS_XATTR_NAME_LEN) ||
	    (!g_utf8_validate(name, name_len, NULL)) ||
	    (buflen > DBFS_XATTR_MAX_LEN))
		return -EINVAL;

	rc = dbfs_xattr_get(txn, ino_n, name, &current, &current_len);
	if (rc && (rc != -EINVAL))
		return rc;

	exists = (current == NULL);
	free(current);

	if (exists && (flags & XATTR_CREATE))
		return -EEXIST;
	if (!exists && (flags & XATTR_REPLACE))
		return -ENOATTR;

	rc = dbfs_xattr_write(txn, ino_n, name, buf, buflen);
	if (rc)
		return rc;

	rc = dbfs_xattr_list_add(txn, ino_n, name);
	if (rc)
		return rc;

	return 0;
}

int dbfs_xattr_remove(DB_TXN *txn, guint64 ino_n, const char *name,
		      gboolean update_list)
{
	char key_str[DBFS_XATTR_NAME_LEN + 32];

	snprintf(key_str, sizeof(key_str),
		 "/xattr/%Lu/%s", (unsigned long long) ino_n, name);

	if (update_list) {
		int rc = dbfs_xattr_list_del(txn, ino_n, name);
		if (rc)
			return rc;
	}

	return dbmeta_del(txn, key_str);
}