summaryrefslogtreecommitdiff
path: root/usr/src/lib/smbsrv/libfksmbsrv/common/fake_xattr.c
blob: 25c90a1e4238a30348f82dd344875465d2172df7 (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
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
 * Copyright 2014 Nexenta Systems, Inc.  All rights reserved.
 */

#include <sys/types.h>
#include <sys/vnode.h>
#include <sys/debug.h>

#include <attr.h>
#include <libnvpair.h>

static uint64_t zero_times[2];

static int
getxva_parse_nvl(xvattr_t *xvap,
    xoptattr_t *xoap, nvlist_t *nvl);

/*
 * See similar code to parse the nvlist in:
 * uts/common/fs/xattr.c : xattr_file_write()
 */
int
fop__getxvattr(vnode_t *vp, xvattr_t *xvap)
{
	nvlist_t *nvl = NULL;
	xoptattr_t *xoap = NULL;
	int error;

	if ((xoap = xva_getxoptattr(xvap)) == NULL) {
		return (EINVAL);
	}

	error = fgetattr(vp->v_fd, XATTR_VIEW_READWRITE, &nvl);
	if (error == 0) {
		error = getxva_parse_nvl(xvap, xoap, nvl);
		nvlist_free(nvl);
		nvl = NULL;
	}

	/*
	 * Also get the readonly attrs, but don't fail.
	 */
	if (fgetattr(vp->v_fd, XATTR_VIEW_READONLY, &nvl) == 0) {
		(void) getxva_parse_nvl(xvap, xoap, nvl);
		nvlist_free(nvl);
	}

	return (error);
}

static int
getxva_parse_nvl(xvattr_t *xvap,
    xoptattr_t *xoap, nvlist_t *nvl)
{
	nvpair_t *pair = NULL;
	int error;

	while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
		data_type_t type;
		f_attr_t attr;
		boolean_t value = B_FALSE;
		uint64_t *times = zero_times;
		uint_t nelems = 2;

		/*
		 * Validate the name and type of each attribute.
		 * Log any unknown names and continue.  This will
		 * help if additional attributes are added later.
		 */
		type = nvpair_type(pair);
		attr = name_to_attr(nvpair_name(pair));
		if (attr == F_ATTR_INVAL)
			continue;

		/*
		 * Verify nvlist type matches required type and view is OK
		 */

		if (type != attr_to_data_type(attr) ||
		    (attr_to_xattr_view(attr) == XATTR_VIEW_READONLY))
			continue;

		/*
		 * For OWNERSID/GROUPSID, just skip.
		 */
		if ((attr == F_OWNERSID || attr == F_GROUPSID))
			continue;

		/*
		 * Retrieve data from nvpair
		 */
		switch (type) {
		case DATA_TYPE_BOOLEAN_VALUE:
			if (nvpair_value_boolean_value(pair, &value)) {
				error = EINVAL;
				goto out;
			}
			break;

		case DATA_TYPE_UINT64_ARRAY:
			if (nvpair_value_uint64_array(pair, &times, &nelems)) {
				error = EINVAL;
				goto out;
			}
			if (nelems < 2)
				continue;
			break;

		case DATA_TYPE_NVLIST:
			continue;

		case DATA_TYPE_UINT8_ARRAY:
			continue;

		default:
			error = EINVAL;
			goto out;
		}

		switch (attr) {
		/*
		 * If we have several similar optional attributes to
		 * process then we should do it all together here so that
		 * xoap and the requested bitmap can be set in one place.
		 */
		case F_READONLY:
			XVA_SET_RTN(xvap, XAT_READONLY);
			xoap->xoa_readonly = value;
			break;

		case F_HIDDEN:
			XVA_SET_RTN(xvap, XAT_HIDDEN);
			xoap->xoa_hidden = value;
			break;

		case F_SYSTEM:
			XVA_SET_RTN(xvap, XAT_SYSTEM);
			xoap->xoa_system = value;
			break;

		case F_ARCHIVE:
			XVA_SET_RTN(xvap, XAT_ARCHIVE);
			xoap->xoa_archive = value;
			break;

		case F_CRTIME:
			XVA_SET_RTN(xvap, XAT_CREATETIME);
			xoap->xoa_createtime.tv_sec = times[0];
			xoap->xoa_createtime.tv_nsec = times[1];
			break;

		case F_REPARSE:
			XVA_SET_RTN(xvap, XAT_REPARSE);
			xoap->xoa_reparse = value;
			break;

		case F_OFFLINE:
			XVA_SET_RTN(xvap, XAT_OFFLINE);
			xoap->xoa_offline = value;
			break;

		case F_SPARSE:
			XVA_SET_RTN(xvap, XAT_SPARSE);
			xoap->xoa_sparse = value;
			break;

		default:
			break;
		}
	}
	error = 0;

out:
	return (error);
}

/*
 * See similar code to build the nvlist in:
 * uts/common/fs/xattr.c : xattr_fill_nvlist()
 */
int
fop__setxvattr(vnode_t *vp, xvattr_t *xvap)
{
	uint64_t times[2];
	nvlist_t *nvl;
	int error;
	xoptattr_t *xoap;	/* Pointer to optional attributes */

	if ((xoap = xva_getxoptattr(xvap)) == NULL)
		return (EINVAL);

	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP))
		return (ENOMEM);

	if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
		VERIFY(nvlist_add_boolean_value(nvl,
		    attr_to_name(F_READONLY),
		    xoap->xoa_readonly) == 0);
	}

	if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
		VERIFY(nvlist_add_boolean_value(nvl,
		    attr_to_name(F_HIDDEN),
		    xoap->xoa_hidden) == 0);
	}

	if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
		VERIFY(nvlist_add_boolean_value(nvl,
		    attr_to_name(F_SYSTEM),
		    xoap->xoa_system) == 0);
	}

	if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
		VERIFY(nvlist_add_boolean_value(nvl,
		    attr_to_name(F_ARCHIVE),
		    xoap->xoa_archive) == 0);
	}

	if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
		times[0] = xoap->xoa_createtime.tv_sec;
		times[1] = xoap->xoa_createtime.tv_nsec;
		VERIFY(nvlist_add_uint64_array(nvl,
		    attr_to_name(F_CRTIME),
		    times, 2) == 0);
	}

	if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
		VERIFY(nvlist_add_boolean_value(nvl,
		    attr_to_name(F_REPARSE),
		    xoap->xoa_reparse) == 0);
	}

	if (XVA_ISSET_REQ(xvap, XAT_OFFLINE)) {
		VERIFY(nvlist_add_boolean_value(nvl,
		    attr_to_name(F_OFFLINE),
		    xoap->xoa_offline) == 0);
	}

	if (XVA_ISSET_REQ(xvap, XAT_SPARSE)) {
		VERIFY(nvlist_add_boolean_value(nvl,
		    attr_to_name(F_SPARSE),
		    xoap->xoa_sparse) == 0);
	}

	error = fsetattr(vp->v_fd, XATTR_VIEW_READWRITE, nvl);

	nvlist_free(nvl);

	return (error);
}