xattr.c 11.8 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 */
S
Steve French 已提交
85
		rc = CIFSSMBSetEA(xid, pTcon, full_path, ea_name, NULL,
86 87
			(__u16)0, cifs_sb->local_nls,
			cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
L
Linus Torvalds 已提交
88 89
	}
remove_ea_exit:
J
Jesper Juhl 已提交
90
	kfree(full_path);
91
	free_xid(xid);
92
	cifs_put_tlink(tlink);
L
Linus Torvalds 已提交
93 94 95 96
#endif
	return rc;
}

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

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

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

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

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

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

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

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

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

S
Steve French 已提交
240
	if (direntry == NULL)
L
Linus Torvalds 已提交
241
		return -EIO;
S
Steve French 已提交
242
	if (direntry->d_inode == NULL)
L
Linus Torvalds 已提交
243 244
		return -EIO;
	sb = direntry->d_inode->i_sb;
S
Steve French 已提交
245
	if (sb == NULL)
L
Linus Torvalds 已提交
246 247 248
		return -EIO;

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

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

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

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

282
		ea_name += XATTR_OS2_PREFIX_LEN; /* skip past os2. prefix */
283
		rc = CIFSSMBQAllEAs(xid, pTcon, full_path, ea_name, ea_value,
284 285
			buf_size, cifs_sb->local_nls,
			cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
S
Steve French 已提交
286
	} else if (strncmp(ea_name, POSIX_ACL_XATTR_ACCESS,
287
			  strlen(POSIX_ACL_XATTR_ACCESS)) == 0) {
L
Linus Torvalds 已提交
288
#ifdef CONFIG_CIFS_POSIX
S
Steve French 已提交
289
		if (sb->s_flags & MS_POSIXACL)
290
			rc = CIFSSMBGetPosixACL(xid, pTcon, full_path,
S
Steve French 已提交
291
				ea_value, buf_size, ACL_TYPE_ACCESS,
292
				cifs_sb->local_nls,
S
Steve French 已提交
293
				cifs_sb->mnt_cifs_flags &
294
					CIFS_MOUNT_MAP_SPECIAL_CHR);
S
Steve French 已提交
295
#else
296
		cifs_dbg(FYI, "Query POSIX ACL not supported yet\n");
L
Linus Torvalds 已提交
297
#endif /* CONFIG_CIFS_POSIX */
S
Steve French 已提交
298
	} else if (strncmp(ea_name, POSIX_ACL_XATTR_DEFAULT,
299
			  strlen(POSIX_ACL_XATTR_DEFAULT)) == 0) {
L
Linus Torvalds 已提交
300
#ifdef CONFIG_CIFS_POSIX
S
Steve French 已提交
301
		if (sb->s_flags & MS_POSIXACL)
302
			rc = CIFSSMBGetPosixACL(xid, pTcon, full_path,
S
Steve French 已提交
303
				ea_value, buf_size, ACL_TYPE_DEFAULT,
304
				cifs_sb->local_nls,
S
Steve French 已提交
305
				cifs_sb->mnt_cifs_flags &
306
					CIFS_MOUNT_MAP_SPECIAL_CHR);
S
Steve French 已提交
307
#else
308
		cifs_dbg(FYI, "Query POSIX default ACL not supported yet\n");
309 310 311 312 313 314 315 316 317 318 319
#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);
320 321
				cifs_dbg(VFS, "%s: error %zd getting sec desc\n",
					 __func__, rc);
322 323 324 325 326 327 328 329 330 331 332
			} else {
				if (ea_value) {
					if (acllen > buf_size)
						acllen = -ERANGE;
					else
						memcpy(ea_value, pacl, acllen);
				}
				rc = acllen;
				kfree(pacl);
			}
#else
333
			cifs_dbg(FYI, "Query CIFS ACL not supported yet\n");
334
#endif /* CONFIG_CIFS_ACL */
S
Steve French 已提交
335
	} else if (strncmp(ea_name,
336
		  XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) == 0) {
337
		cifs_dbg(FYI, "Trusted xattr namespace not supported yet\n");
S
Steve French 已提交
338
	} else if (strncmp(ea_name,
339
		  XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN) == 0) {
340
		cifs_dbg(FYI, "Security xattr namespace not supported yet\n");
S
Steve French 已提交
341
	} else
342 343 344
		cifs_dbg(FYI,
			 "illegal xattr request %s (only user namespace supported)\n",
			 ea_name);
L
Linus Torvalds 已提交
345

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

S
Steve French 已提交
351 352
	if (rc == -EINVAL)
		rc = -EOPNOTSUPP;
L
Linus Torvalds 已提交
353 354

get_ea_exit:
J
Jesper Juhl 已提交
355
	kfree(full_path);
356
	free_xid(xid);
357
	cifs_put_tlink(tlink);
L
Linus Torvalds 已提交
358 359 360 361
#endif
	return rc;
}

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

S
Steve French 已提交
373
	if (direntry == NULL)
L
Linus Torvalds 已提交
374
		return -EIO;
S
Steve French 已提交
375
	if (direntry->d_inode == NULL)
L
Linus Torvalds 已提交
376 377
		return -EIO;
	sb = direntry->d_inode->i_sb;
S
Steve French 已提交
378
	if (sb == NULL)
L
Linus Torvalds 已提交
379 380 381
		return -EIO;

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

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

390
	xid = get_xid();
391

L
Linus Torvalds 已提交
392
	full_path = build_path_from_dentry(direntry);
S
Steve French 已提交
393
	if (full_path == NULL) {
394
		rc = -ENOMEM;
395
		goto list_ea_exit;
L
Linus Torvalds 已提交
396 397 398 399 400
	}
	/* 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 已提交
401
		search server for EAs or streams to
L
Linus Torvalds 已提交
402
		returns as xattrs */
403 404
	rc = CIFSSMBQAllEAs(xid, pTcon, full_path, NULL, data,
				buf_size, cifs_sb->local_nls,
S
Steve French 已提交
405
				cifs_sb->mnt_cifs_flags &
406
					CIFS_MOUNT_MAP_SPECIAL_CHR);
L
Linus Torvalds 已提交
407

408
list_ea_exit:
J
Jesper Juhl 已提交
409
	kfree(full_path);
410
	free_xid(xid);
411
	cifs_put_tlink(tlink);
L
Linus Torvalds 已提交
412 413 414
#endif
	return rc;
}