xattr.c 12.0 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3
/*
 *   fs/cifs/xattr.c
 *
S
Steve French 已提交
4
 *   Copyright (c) International Business Machines  Corp., 2003, 2007
L
Linus Torvalds 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *   Author(s): Steve French (sfrench@us.ibm.com)
 *
 *   This library is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as published
 *   by the Free Software Foundation; either version 2.1 of the License, or
 *   (at your option) any later version.
 *
 *   This library 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 Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public License
 *   along with this library; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#include <linux/fs.h>
#include <linux/posix_acl_xattr.h>
24
#include <linux/slab.h>
25
#include <linux/xattr.h>
L
Linus Torvalds 已提交
26 27 28 29 30
#include "cifsfs.h"
#include "cifspdu.h"
#include "cifsglob.h"
#include "cifsproto.h"
#include "cifs_debug.h"
31 32
#include "cifs_fs_sb.h"
#include "cifs_unicode.h"
L
Linus Torvalds 已提交
33 34 35

#define MAX_EA_VALUE_SIZE 65535
#define CIFS_XATTR_DOS_ATTRIB "user.DosAttrib"
36
#define CIFS_XATTR_CIFS_ACL "system.cifs_acl"
L
Linus Torvalds 已提交
37

38
/* BB need to add server (Samba e.g) support for security and trusted prefix */
L
Linus Torvalds 已提交
39

S
Steve French 已提交
40
int cifs_removexattr(struct dentry *direntry, const char *ea_name)
L
Linus Torvalds 已提交
41 42 43
{
	int rc = -EOPNOTSUPP;
#ifdef CONFIG_CIFS_XATTR
44
	unsigned int xid;
L
Linus Torvalds 已提交
45
	struct cifs_sb_info *cifs_sb;
46
	struct tcon_link *tlink;
47
	struct cifs_tcon *pTcon;
S
Steve French 已提交
48
	struct super_block *sb;
49
	char *full_path = NULL;
S
Steve French 已提交
50 51

	if (direntry == NULL)
L
Linus Torvalds 已提交
52
		return -EIO;
53
	if (d_really_is_negative(direntry))
L
Linus Torvalds 已提交
54
		return -EIO;
55
	sb = direntry->d_sb;
S
Steve French 已提交
56

L
Linus Torvalds 已提交
57
	cifs_sb = CIFS_SB(sb);
58 59 60 61 62
	tlink = cifs_sb_tlink(cifs_sb);
	if (IS_ERR(tlink))
		return PTR_ERR(tlink);
	pTcon = tlink_tcon(tlink);

63
	xid = get_xid();
S
Steve French 已提交
64

L
Linus Torvalds 已提交
65
	full_path = build_path_from_dentry(direntry);
S
Steve French 已提交
66
	if (full_path == NULL) {
67
		rc = -ENOMEM;
68
		goto remove_ea_exit;
L
Linus Torvalds 已提交
69
	}
S
Steve French 已提交
70
	if (ea_name == NULL) {
71
		cifs_dbg(FYI, "Null xattr names not supported\n");
72 73
	} else if (strncmp(ea_name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN)
		&& (strncmp(ea_name, XATTR_OS2_PREFIX, XATTR_OS2_PREFIX_LEN))) {
74 75 76
		cifs_dbg(FYI,
			 "illegal xattr request %s (only user namespace supported)\n",
			 ea_name);
L
Linus Torvalds 已提交
77 78 79 80
		/* BB what if no namespace prefix? */
		/* Should we just pass them to server, except for
		system and perhaps security prefixes? */
	} else {
S
Steve French 已提交
81
		if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
L
Linus Torvalds 已提交
82 83
			goto remove_ea_exit;

84
		ea_name += XATTR_USER_PREFIX_LEN; /* skip past user. prefix */
85 86 87
		if (pTcon->ses->server->ops->set_EA)
			rc = pTcon->ses->server->ops->set_EA(xid, pTcon,
				full_path, ea_name, NULL, (__u16)0,
88
				cifs_sb->local_nls, cifs_remap(cifs_sb));
L
Linus Torvalds 已提交
89 90
	}
remove_ea_exit:
J
Jesper Juhl 已提交
91
	kfree(full_path);
92
	free_xid(xid);
93
	cifs_put_tlink(tlink);
L
Linus Torvalds 已提交
94 95 96 97
#endif
	return rc;
}

S
Steve French 已提交
98 99
int cifs_setxattr(struct dentry *direntry, const char *ea_name,
		  const void *ea_value, size_t value_size, int flags)
L
Linus Torvalds 已提交
100 101 102
{
	int rc = -EOPNOTSUPP;
#ifdef CONFIG_CIFS_XATTR
103
	unsigned int xid;
L
Linus Torvalds 已提交
104
	struct cifs_sb_info *cifs_sb;
105
	struct tcon_link *tlink;
106
	struct cifs_tcon *pTcon;
S
Steve French 已提交
107 108
	struct super_block *sb;
	char *full_path;
L
Linus Torvalds 已提交
109

S
Steve French 已提交
110
	if (direntry == NULL)
L
Linus Torvalds 已提交
111
		return -EIO;
112
	if (d_really_is_negative(direntry))
L
Linus Torvalds 已提交
113
		return -EIO;
114
	sb = direntry->d_sb;
L
Linus Torvalds 已提交
115 116

	cifs_sb = CIFS_SB(sb);
117 118 119 120 121
	tlink = cifs_sb_tlink(cifs_sb);
	if (IS_ERR(tlink))
		return PTR_ERR(tlink);
	pTcon = tlink_tcon(tlink);

122
	xid = get_xid();
L
Linus Torvalds 已提交
123 124

	full_path = build_path_from_dentry(direntry);
S
Steve French 已提交
125
	if (full_path == NULL) {
126
		rc = -ENOMEM;
127
		goto set_ea_exit;
L
Linus Torvalds 已提交
128 129 130 131 132
	}
	/* return dos attributes as pseudo xattr */
	/* return alt name if available as pseudo attr */

	/* if proc/fs/cifs/streamstoxattr is set then
S
Steve French 已提交
133
		search server for EAs or streams to
L
Linus Torvalds 已提交
134
		returns as xattrs */
S
Steve French 已提交
135
	if (value_size > MAX_EA_VALUE_SIZE) {
136
		cifs_dbg(FYI, "size of EA value too large\n");
137 138
		rc = -EOPNOTSUPP;
		goto set_ea_exit;
L
Linus Torvalds 已提交
139 140
	}

S
Steve French 已提交
141
	if (ea_name == NULL) {
142
		cifs_dbg(FYI, "Null xattr names not supported\n");
143 144
	} else if (strncmp(ea_name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN)
		   == 0) {
S
Steve French 已提交
145
		if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
L
Linus Torvalds 已提交
146
			goto set_ea_exit;
S
Steve French 已提交
147
		if (strncmp(ea_name, CIFS_XATTR_DOS_ATTRIB, 14) == 0)
148
			cifs_dbg(FYI, "attempt to set cifs inode metadata\n");
S
Steve French 已提交
149

150
		ea_name += XATTR_USER_PREFIX_LEN; /* skip past user. prefix */
151 152 153
		if (pTcon->ses->server->ops->set_EA)
			rc = pTcon->ses->server->ops->set_EA(xid, pTcon,
				full_path, ea_name, ea_value, (__u16)value_size,
154
				cifs_sb->local_nls, cifs_remap(cifs_sb));
155 156
	} else if (strncmp(ea_name, XATTR_OS2_PREFIX, XATTR_OS2_PREFIX_LEN)
		   == 0) {
S
Steve French 已提交
157
		if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
L
Linus Torvalds 已提交
158 159
			goto set_ea_exit;

160
		ea_name += XATTR_OS2_PREFIX_LEN; /* skip past os2. prefix */
161 162 163
		if (pTcon->ses->server->ops->set_EA)
			rc = pTcon->ses->server->ops->set_EA(xid, pTcon,
				full_path, ea_name, ea_value, (__u16)value_size,
164
				cifs_sb->local_nls, cifs_remap(cifs_sb));
165 166
	} else if (strncmp(ea_name, CIFS_XATTR_CIFS_ACL,
			strlen(CIFS_XATTR_CIFS_ACL)) == 0) {
167 168
#ifdef CONFIG_CIFS_ACL
		struct cifs_ntsd *pacl;
169 170 171 172 173
		pacl = kmalloc(value_size, GFP_KERNEL);
		if (!pacl) {
			rc = -ENOMEM;
		} else {
			memcpy(pacl, ea_value, value_size);
174 175
			if (pTcon->ses->server->ops->set_acl)
				rc = pTcon->ses->server->ops->set_acl(pacl,
176
						value_size, d_inode(direntry),
177 178 179
						full_path, CIFS_ACL_DACL);
			else
				rc = -EOPNOTSUPP;
180
			if (rc == 0) /* force revalidate of the inode */
181
				CIFS_I(d_inode(direntry))->time = 0;
182
			kfree(pacl);
183
		}
184
#else
185
		cifs_dbg(FYI, "Set CIFS ACL not supported yet\n");
186
#endif /* CONFIG_CIFS_ACL */
L
Linus Torvalds 已提交
187
	} else {
S
Steve French 已提交
188
		int temp;
189 190
		temp = strncmp(ea_name, XATTR_NAME_POSIX_ACL_ACCESS,
			strlen(XATTR_NAME_POSIX_ACL_ACCESS));
L
Linus Torvalds 已提交
191 192
		if (temp == 0) {
#ifdef CONFIG_CIFS_POSIX
S
Steve French 已提交
193 194 195 196
			if (sb->s_flags & MS_POSIXACL)
				rc = CIFSSMBSetPosixACL(xid, pTcon, full_path,
					ea_value, (const int)value_size,
					ACL_TYPE_ACCESS, cifs_sb->local_nls,
197
					cifs_remap(cifs_sb));
198
			cifs_dbg(FYI, "set POSIX ACL rc %d\n", rc);
L
Linus Torvalds 已提交
199
#else
200
			cifs_dbg(FYI, "set POSIX ACL not supported\n");
L
Linus Torvalds 已提交
201
#endif
202 203
		} else if (strncmp(ea_name, XATTR_NAME_POSIX_ACL_DEFAULT,
				   strlen(XATTR_NAME_POSIX_ACL_DEFAULT)) == 0) {
L
Linus Torvalds 已提交
204
#ifdef CONFIG_CIFS_POSIX
S
Steve French 已提交
205 206 207
			if (sb->s_flags & MS_POSIXACL)
				rc = CIFSSMBSetPosixACL(xid, pTcon, full_path,
					ea_value, (const int)value_size,
208
					ACL_TYPE_DEFAULT, cifs_sb->local_nls,
209
					cifs_remap(cifs_sb));
210
			cifs_dbg(FYI, "set POSIX default ACL rc %d\n", rc);
L
Linus Torvalds 已提交
211
#else
212
			cifs_dbg(FYI, "set default POSIX ACL not supported\n");
L
Linus Torvalds 已提交
213 214
#endif
		} else {
215 216
			cifs_dbg(FYI, "illegal xattr request %s (only user namespace supported)\n",
				 ea_name);
L
Linus Torvalds 已提交
217
		  /* BB what if no namespace prefix? */
S
Steve French 已提交
218
		  /* Should we just pass them to server, except for
L
Linus Torvalds 已提交
219 220 221 222 223
		  system and perhaps security prefixes? */
		}
	}

set_ea_exit:
J
Jesper Juhl 已提交
224
	kfree(full_path);
225
	free_xid(xid);
226
	cifs_put_tlink(tlink);
L
Linus Torvalds 已提交
227 228 229 230
#endif
	return rc;
}

S
Steve French 已提交
231 232
ssize_t cifs_getxattr(struct dentry *direntry, const char *ea_name,
	void *ea_value, size_t buf_size)
L
Linus Torvalds 已提交
233 234 235
{
	ssize_t rc = -EOPNOTSUPP;
#ifdef CONFIG_CIFS_XATTR
236
	unsigned int xid;
L
Linus Torvalds 已提交
237
	struct cifs_sb_info *cifs_sb;
238
	struct tcon_link *tlink;
239
	struct cifs_tcon *pTcon;
S
Steve French 已提交
240 241
	struct super_block *sb;
	char *full_path;
L
Linus Torvalds 已提交
242

S
Steve French 已提交
243
	if (direntry == NULL)
L
Linus Torvalds 已提交
244
		return -EIO;
245
	if (d_really_is_negative(direntry))
L
Linus Torvalds 已提交
246
		return -EIO;
247
	sb = direntry->d_sb;
L
Linus Torvalds 已提交
248 249

	cifs_sb = CIFS_SB(sb);
250 251 252 253 254
	tlink = cifs_sb_tlink(cifs_sb);
	if (IS_ERR(tlink))
		return PTR_ERR(tlink);
	pTcon = tlink_tcon(tlink);

255
	xid = get_xid();
L
Linus Torvalds 已提交
256 257

	full_path = build_path_from_dentry(direntry);
S
Steve French 已提交
258
	if (full_path == NULL) {
259
		rc = -ENOMEM;
260
		goto get_ea_exit;
L
Linus Torvalds 已提交
261 262 263
	}
	/* return dos attributes as pseudo xattr */
	/* return alt name if available as pseudo attr */
S
Steve French 已提交
264
	if (ea_name == NULL) {
265
		cifs_dbg(FYI, "Null xattr names not supported\n");
266 267
	} else if (strncmp(ea_name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN)
		   == 0) {
S
Steve French 已提交
268
		if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
L
Linus Torvalds 已提交
269 270
			goto get_ea_exit;

S
Steve French 已提交
271
		if (strncmp(ea_name, CIFS_XATTR_DOS_ATTRIB, 14) == 0) {
272
			cifs_dbg(FYI, "attempt to query cifs inode metadata\n");
L
Linus Torvalds 已提交
273 274
			/* revalidate/getattr then populate from inode */
		} /* BB add else when above is implemented */
275
		ea_name += XATTR_USER_PREFIX_LEN; /* skip past user. prefix */
276 277 278
		if (pTcon->ses->server->ops->query_all_EAs)
			rc = pTcon->ses->server->ops->query_all_EAs(xid, pTcon,
				full_path, ea_name, ea_value, buf_size,
279
				cifs_sb->local_nls, cifs_remap(cifs_sb));
280
	} else if (strncmp(ea_name, XATTR_OS2_PREFIX, XATTR_OS2_PREFIX_LEN) == 0) {
S
Steve French 已提交
281
		if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
L
Linus Torvalds 已提交
282 283
			goto get_ea_exit;

284
		ea_name += XATTR_OS2_PREFIX_LEN; /* skip past os2. prefix */
285 286 287
		if (pTcon->ses->server->ops->query_all_EAs)
			rc = pTcon->ses->server->ops->query_all_EAs(xid, pTcon,
				full_path, ea_name, ea_value, buf_size,
288
				cifs_sb->local_nls, cifs_remap(cifs_sb));
289 290
	} else if (strncmp(ea_name, XATTR_NAME_POSIX_ACL_ACCESS,
			  strlen(XATTR_NAME_POSIX_ACL_ACCESS)) == 0) {
L
Linus Torvalds 已提交
291
#ifdef CONFIG_CIFS_POSIX
S
Steve French 已提交
292
		if (sb->s_flags & MS_POSIXACL)
293
			rc = CIFSSMBGetPosixACL(xid, pTcon, full_path,
S
Steve French 已提交
294
				ea_value, buf_size, ACL_TYPE_ACCESS,
295
				cifs_sb->local_nls,
296
				cifs_remap(cifs_sb));
S
Steve French 已提交
297
#else
298
		cifs_dbg(FYI, "Query POSIX ACL not supported yet\n");
L
Linus Torvalds 已提交
299
#endif /* CONFIG_CIFS_POSIX */
300 301
	} else if (strncmp(ea_name, XATTR_NAME_POSIX_ACL_DEFAULT,
			  strlen(XATTR_NAME_POSIX_ACL_DEFAULT)) == 0) {
L
Linus Torvalds 已提交
302
#ifdef CONFIG_CIFS_POSIX
S
Steve French 已提交
303
		if (sb->s_flags & MS_POSIXACL)
304
			rc = CIFSSMBGetPosixACL(xid, pTcon, full_path,
S
Steve French 已提交
305
				ea_value, buf_size, ACL_TYPE_DEFAULT,
306
				cifs_sb->local_nls,
307
				cifs_remap(cifs_sb));
S
Steve French 已提交
308
#else
309
		cifs_dbg(FYI, "Query POSIX default ACL not supported yet\n");
310 311 312 313 314 315 316
#endif /* CONFIG_CIFS_POSIX */
	} else if (strncmp(ea_name, CIFS_XATTR_CIFS_ACL,
				strlen(CIFS_XATTR_CIFS_ACL)) == 0) {
#ifdef CONFIG_CIFS_ACL
			u32 acllen;
			struct cifs_ntsd *pacl;

317 318 319 320
			if (pTcon->ses->server->ops->get_acl == NULL)
				goto get_ea_exit; /* rc already EOPNOTSUPP */

			pacl = pTcon->ses->server->ops->get_acl(cifs_sb,
321
					d_inode(direntry), full_path, &acllen);
322 323
			if (IS_ERR(pacl)) {
				rc = PTR_ERR(pacl);
324 325
				cifs_dbg(VFS, "%s: error %zd getting sec desc\n",
					 __func__, rc);
326 327 328 329 330 331 332 333 334 335 336
			} else {
				if (ea_value) {
					if (acllen > buf_size)
						acllen = -ERANGE;
					else
						memcpy(ea_value, pacl, acllen);
				}
				rc = acllen;
				kfree(pacl);
			}
#else
337
			cifs_dbg(FYI, "Query CIFS ACL not supported yet\n");
338
#endif /* CONFIG_CIFS_ACL */
S
Steve French 已提交
339
	} else if (strncmp(ea_name,
340
		  XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) == 0) {
341
		cifs_dbg(FYI, "Trusted xattr namespace not supported yet\n");
S
Steve French 已提交
342
	} else if (strncmp(ea_name,
343
		  XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN) == 0) {
344
		cifs_dbg(FYI, "Security xattr namespace not supported yet\n");
S
Steve French 已提交
345
	} else
346 347 348
		cifs_dbg(FYI,
			 "illegal xattr request %s (only user namespace supported)\n",
			 ea_name);
L
Linus Torvalds 已提交
349

S
Steve French 已提交
350
	/* We could add an additional check for streams ie
L
Linus Torvalds 已提交
351
	    if proc/fs/cifs/streamstoxattr is set then
S
Steve French 已提交
352
		search server for EAs or streams to
L
Linus Torvalds 已提交
353 354
		returns as xattrs */

S
Steve French 已提交
355 356
	if (rc == -EINVAL)
		rc = -EOPNOTSUPP;
L
Linus Torvalds 已提交
357 358

get_ea_exit:
J
Jesper Juhl 已提交
359
	kfree(full_path);
360
	free_xid(xid);
361
	cifs_put_tlink(tlink);
L
Linus Torvalds 已提交
362 363 364 365
#endif
	return rc;
}

S
Steve French 已提交
366
ssize_t cifs_listxattr(struct dentry *direntry, char *data, size_t buf_size)
L
Linus Torvalds 已提交
367 368 369
{
	ssize_t rc = -EOPNOTSUPP;
#ifdef CONFIG_CIFS_XATTR
370
	unsigned int xid;
L
Linus Torvalds 已提交
371
	struct cifs_sb_info *cifs_sb;
372
	struct tcon_link *tlink;
373
	struct cifs_tcon *pTcon;
S
Steve French 已提交
374 375
	struct super_block *sb;
	char *full_path;
L
Linus Torvalds 已提交
376

S
Steve French 已提交
377
	if (direntry == NULL)
L
Linus Torvalds 已提交
378
		return -EIO;
379
	if (d_really_is_negative(direntry))
L
Linus Torvalds 已提交
380
		return -EIO;
381
	sb = direntry->d_sb;
L
Linus Torvalds 已提交
382 383

	cifs_sb = CIFS_SB(sb);
S
Steve French 已提交
384
	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
385 386
		return -EOPNOTSUPP;

387 388 389 390 391
	tlink = cifs_sb_tlink(cifs_sb);
	if (IS_ERR(tlink))
		return PTR_ERR(tlink);
	pTcon = tlink_tcon(tlink);

392
	xid = get_xid();
393

L
Linus Torvalds 已提交
394
	full_path = build_path_from_dentry(direntry);
S
Steve French 已提交
395
	if (full_path == NULL) {
396
		rc = -ENOMEM;
397
		goto list_ea_exit;
L
Linus Torvalds 已提交
398 399 400 401 402
	}
	/* return dos attributes as pseudo xattr */
	/* return alt name if available as pseudo attr */

	/* if proc/fs/cifs/streamstoxattr is set then
S
Steve French 已提交
403
		search server for EAs or streams to
L
Linus Torvalds 已提交
404 405
		returns as xattrs */

406 407 408
	if (pTcon->ses->server->ops->query_all_EAs)
		rc = pTcon->ses->server->ops->query_all_EAs(xid, pTcon,
				full_path, NULL, data, buf_size,
409
				cifs_sb->local_nls, cifs_remap(cifs_sb));
410
list_ea_exit:
J
Jesper Juhl 已提交
411
	kfree(full_path);
412
	free_xid(xid);
413
	cifs_put_tlink(tlink);
L
Linus Torvalds 已提交
414 415 416
#endif
	return rc;
}