xattr.c 12.2 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 31 32 33
#include "cifsfs.h"
#include "cifspdu.h"
#include "cifsglob.h"
#include "cifsproto.h"
#include "cifs_debug.h"

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

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

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

	if (direntry == NULL)
L
Linus Torvalds 已提交
50
		return -EIO;
S
Steve French 已提交
51
	if (direntry->d_inode == NULL)
L
Linus Torvalds 已提交
52 53
		return -EIO;
	sb = direntry->d_inode->i_sb;
S
Steve French 已提交
54
	if (sb == NULL)
L
Linus Torvalds 已提交
55
		return -EIO;
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 88 89
		if (pTcon->ses->server->ops->set_EA)
			rc = pTcon->ses->server->ops->set_EA(xid, pTcon,
				full_path, ea_name, NULL, (__u16)0,
				cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
					CIFS_MOUNT_MAP_SPECIAL_CHR);
L
Linus Torvalds 已提交
90 91
	}
remove_ea_exit:
J
Jesper Juhl 已提交
92
	kfree(full_path);
93
	free_xid(xid);
94
	cifs_put_tlink(tlink);
L
Linus Torvalds 已提交
95 96 97 98
#endif
	return rc;
}

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

S
Steve French 已提交
111
	if (direntry == NULL)
L
Linus Torvalds 已提交
112
		return -EIO;
S
Steve French 已提交
113
	if (direntry->d_inode == NULL)
L
Linus Torvalds 已提交
114 115
		return -EIO;
	sb = direntry->d_inode->i_sb;
S
Steve French 已提交
116
	if (sb == NULL)
L
Linus Torvalds 已提交
117 118 119
		return -EIO;

	cifs_sb = CIFS_SB(sb);
120 121 122 123 124
	tlink = cifs_sb_tlink(cifs_sb);
	if (IS_ERR(tlink))
		return PTR_ERR(tlink);
	pTcon = tlink_tcon(tlink);

125
	xid = get_xid();
L
Linus Torvalds 已提交
126 127

	full_path = build_path_from_dentry(direntry);
S
Steve French 已提交
128
	if (full_path == NULL) {
129
		rc = -ENOMEM;
130
		goto set_ea_exit;
L
Linus Torvalds 已提交
131 132 133 134 135
	}
	/* 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 已提交
136
		search server for EAs or streams to
L
Linus Torvalds 已提交
137
		returns as xattrs */
S
Steve French 已提交
138
	if (value_size > MAX_EA_VALUE_SIZE) {
139
		cifs_dbg(FYI, "size of EA value too large\n");
140 141
		rc = -EOPNOTSUPP;
		goto set_ea_exit;
L
Linus Torvalds 已提交
142 143
	}

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

153
		ea_name += XATTR_USER_PREFIX_LEN; /* skip past user. prefix */
154 155 156 157 158
		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,
				cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
					CIFS_MOUNT_MAP_SPECIAL_CHR);
159 160
	} else if (strncmp(ea_name, XATTR_OS2_PREFIX, XATTR_OS2_PREFIX_LEN)
		   == 0) {
S
Steve French 已提交
161
		if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
L
Linus Torvalds 已提交
162 163
			goto set_ea_exit;

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

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

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

S
Steve French 已提交
246
	if (direntry == NULL)
L
Linus Torvalds 已提交
247
		return -EIO;
S
Steve French 已提交
248
	if (direntry->d_inode == NULL)
L
Linus Torvalds 已提交
249 250
		return -EIO;
	sb = direntry->d_inode->i_sb;
S
Steve French 已提交
251
	if (sb == NULL)
L
Linus Torvalds 已提交
252 253 254
		return -EIO;

	cifs_sb = CIFS_SB(sb);
255 256 257 258 259
	tlink = cifs_sb_tlink(cifs_sb);
	if (IS_ERR(tlink))
		return PTR_ERR(tlink);
	pTcon = tlink_tcon(tlink);

260
	xid = get_xid();
L
Linus Torvalds 已提交
261 262

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

S
Steve French 已提交
276
		if (strncmp(ea_name, CIFS_XATTR_DOS_ATTRIB, 14) == 0) {
277
			cifs_dbg(FYI, "attempt to query cifs inode metadata\n");
L
Linus Torvalds 已提交
278 279
			/* revalidate/getattr then populate from inode */
		} /* BB add else when above is implemented */
280
		ea_name += XATTR_USER_PREFIX_LEN; /* skip past user. prefix */
281 282 283 284 285
		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,
				cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
					CIFS_MOUNT_MAP_SPECIAL_CHR);
286
	} else if (strncmp(ea_name, XATTR_OS2_PREFIX, XATTR_OS2_PREFIX_LEN) == 0) {
S
Steve French 已提交
287
		if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
L
Linus Torvalds 已提交
288 289
			goto get_ea_exit;

290
		ea_name += XATTR_OS2_PREFIX_LEN; /* skip past os2. prefix */
291 292 293 294 295
		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,
				cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
					CIFS_MOUNT_MAP_SPECIAL_CHR);
S
Steve French 已提交
296
	} else if (strncmp(ea_name, POSIX_ACL_XATTR_ACCESS,
297
			  strlen(POSIX_ACL_XATTR_ACCESS)) == 0) {
L
Linus Torvalds 已提交
298
#ifdef CONFIG_CIFS_POSIX
S
Steve French 已提交
299
		if (sb->s_flags & MS_POSIXACL)
300
			rc = CIFSSMBGetPosixACL(xid, pTcon, full_path,
S
Steve French 已提交
301
				ea_value, buf_size, ACL_TYPE_ACCESS,
302
				cifs_sb->local_nls,
S
Steve French 已提交
303
				cifs_sb->mnt_cifs_flags &
304
					CIFS_MOUNT_MAP_SPECIAL_CHR);
S
Steve French 已提交
305
#else
306
		cifs_dbg(FYI, "Query POSIX ACL not supported yet\n");
L
Linus Torvalds 已提交
307
#endif /* CONFIG_CIFS_POSIX */
S
Steve French 已提交
308
	} else if (strncmp(ea_name, POSIX_ACL_XATTR_DEFAULT,
309
			  strlen(POSIX_ACL_XATTR_DEFAULT)) == 0) {
L
Linus Torvalds 已提交
310
#ifdef CONFIG_CIFS_POSIX
S
Steve French 已提交
311
		if (sb->s_flags & MS_POSIXACL)
312
			rc = CIFSSMBGetPosixACL(xid, pTcon, full_path,
S
Steve French 已提交
313
				ea_value, buf_size, ACL_TYPE_DEFAULT,
314
				cifs_sb->local_nls,
S
Steve French 已提交
315
				cifs_sb->mnt_cifs_flags &
316
					CIFS_MOUNT_MAP_SPECIAL_CHR);
S
Steve French 已提交
317
#else
318
		cifs_dbg(FYI, "Query POSIX default ACL not supported yet\n");
319 320 321 322 323 324 325 326 327 328 329
#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;

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

S
Steve French 已提交
356
	/* We could add an additional check for streams ie
L
Linus Torvalds 已提交
357
	    if proc/fs/cifs/streamstoxattr is set then
S
Steve French 已提交
358
		search server for EAs or streams to
L
Linus Torvalds 已提交
359 360
		returns as xattrs */

S
Steve French 已提交
361 362
	if (rc == -EINVAL)
		rc = -EOPNOTSUPP;
L
Linus Torvalds 已提交
363 364

get_ea_exit:
J
Jesper Juhl 已提交
365
	kfree(full_path);
366
	free_xid(xid);
367
	cifs_put_tlink(tlink);
L
Linus Torvalds 已提交
368 369 370 371
#endif
	return rc;
}

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

S
Steve French 已提交
383
	if (direntry == NULL)
L
Linus Torvalds 已提交
384
		return -EIO;
S
Steve French 已提交
385
	if (direntry->d_inode == NULL)
L
Linus Torvalds 已提交
386 387
		return -EIO;
	sb = direntry->d_inode->i_sb;
S
Steve French 已提交
388
	if (sb == NULL)
L
Linus Torvalds 已提交
389 390 391
		return -EIO;

	cifs_sb = CIFS_SB(sb);
S
Steve French 已提交
392
	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
393 394
		return -EOPNOTSUPP;

395 396 397 398 399
	tlink = cifs_sb_tlink(cifs_sb);
	if (IS_ERR(tlink))
		return PTR_ERR(tlink);
	pTcon = tlink_tcon(tlink);

400
	xid = get_xid();
401

L
Linus Torvalds 已提交
402
	full_path = build_path_from_dentry(direntry);
S
Steve French 已提交
403
	if (full_path == NULL) {
404
		rc = -ENOMEM;
405
		goto list_ea_exit;
L
Linus Torvalds 已提交
406 407 408 409 410
	}
	/* 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 已提交
411
		search server for EAs or streams to
L
Linus Torvalds 已提交
412 413
		returns as xattrs */

414 415 416 417 418
	if (pTcon->ses->server->ops->query_all_EAs)
		rc = pTcon->ses->server->ops->query_all_EAs(xid, pTcon,
				full_path, NULL, data, buf_size,
				cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
					CIFS_MOUNT_MAP_SPECIAL_CHR);
419
list_ea_exit:
J
Jesper Juhl 已提交
420
	kfree(full_path);
421
	free_xid(xid);
422
	cifs_put_tlink(tlink);
L
Linus Torvalds 已提交
423 424 425
#endif
	return rc;
}