xattr.c 10.6 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 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
 *   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>
#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"
#define CIFS_XATTR_USER_PREFIX "user."
#define CIFS_XATTR_SYSTEM_PREFIX "system."
#define CIFS_XATTR_OS2_PREFIX "os2."
#define CIFS_XATTR_SECURITY_PREFIX ".security"
#define CIFS_XATTR_TRUSTED_PREFIX "trusted."
#define XATTR_TRUSTED_PREFIX_LEN  8
#define XATTR_SECURITY_PREFIX_LEN 9
/* BB need to add server (Samba e.g) support for security and trusted prefix */
40

L
Linus Torvalds 已提交
41 42


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

	if (direntry == NULL)
L
Linus Torvalds 已提交
54
		return -EIO;
S
Steve French 已提交
55
	if (direntry->d_inode == NULL)
L
Linus Torvalds 已提交
56 57
		return -EIO;
	sb = direntry->d_inode->i_sb;
S
Steve French 已提交
58
	if (sb == NULL)
L
Linus Torvalds 已提交
59 60
		return -EIO;
	xid = GetXid();
S
Steve French 已提交
61

L
Linus Torvalds 已提交
62 63
	cifs_sb = CIFS_SB(sb);
	pTcon = cifs_sb->tcon;
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;
L
Linus Torvalds 已提交
68
		FreeXid(xid);
69
		return rc;
L
Linus Torvalds 已提交
70
	}
S
Steve French 已提交
71 72 73 74 75 76 77
	if (ea_name == NULL) {
		cFYI(1, ("Null xattr names not supported"));
	} else if (strncmp(ea_name, CIFS_XATTR_USER_PREFIX, 5)
		&& (strncmp(ea_name, CIFS_XATTR_OS2_PREFIX, 4))) {
		cFYI(1,
		    ("illegal xattr request %s (only user namespace supported)",
			ea_name));
L
Linus Torvalds 已提交
78 79 80 81
		/* BB what if no namespace prefix? */
		/* Should we just pass them to server, except for
		system and perhaps security prefixes? */
	} else {
S
Steve French 已提交
82
		if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
L
Linus Torvalds 已提交
83 84
			goto remove_ea_exit;

S
Steve French 已提交
85 86
		ea_name += 5; /* skip past user. prefix */
		rc = CIFSSMBSetEA(xid, pTcon, full_path, ea_name, NULL,
87 88
			(__u16)0, cifs_sb->local_nls,
			cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
L
Linus Torvalds 已提交
89 90
	}
remove_ea_exit:
J
Jesper Juhl 已提交
91
	kfree(full_path);
L
Linus Torvalds 已提交
92 93 94 95 96
	FreeXid(xid);
#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 102 103 104
{
	int rc = -EOPNOTSUPP;
#ifdef CONFIG_CIFS_XATTR
	int xid;
	struct cifs_sb_info *cifs_sb;
	struct cifsTconInfo *pTcon;
S
Steve French 已提交
105 106
	struct super_block *sb;
	char *full_path;
L
Linus Torvalds 已提交
107

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

	cifs_sb = CIFS_SB(sb);
	pTcon = cifs_sb->tcon;

	full_path = build_path_from_dentry(direntry);
S
Steve French 已提交
121
	if (full_path == NULL) {
122
		rc = -ENOMEM;
L
Linus Torvalds 已提交
123
		FreeXid(xid);
124
		return rc;
L
Linus Torvalds 已提交
125 126 127 128 129
	}
	/* 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 已提交
130
		search server for EAs or streams to
L
Linus Torvalds 已提交
131
		returns as xattrs */
S
Steve French 已提交
132 133
	if (value_size > MAX_EA_VALUE_SIZE) {
		cFYI(1, ("size of EA value too large"));
J
Jesper Juhl 已提交
134
		kfree(full_path);
L
Linus Torvalds 已提交
135 136 137 138
		FreeXid(xid);
		return -EOPNOTSUPP;
	}

S
Steve French 已提交
139 140 141 142
	if (ea_name == NULL) {
		cFYI(1, ("Null xattr names not supported"));
	} else if (strncmp(ea_name, CIFS_XATTR_USER_PREFIX, 5) == 0) {
		if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
L
Linus Torvalds 已提交
143
			goto set_ea_exit;
S
Steve French 已提交
144
		if (strncmp(ea_name, CIFS_XATTR_DOS_ATTRIB, 14) == 0)
S
Steve French 已提交
145
			cFYI(1, ("attempt to set cifs inode metadata"));
S
Steve French 已提交
146

L
Linus Torvalds 已提交
147
		ea_name += 5; /* skip past user. prefix */
S
Steve French 已提交
148
		rc = CIFSSMBSetEA(xid, pTcon, full_path, ea_name, ea_value,
149 150
			(__u16)value_size, cifs_sb->local_nls,
			cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
S
Steve French 已提交
151 152
	} else if (strncmp(ea_name, CIFS_XATTR_OS2_PREFIX, 4) == 0) {
		if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
L
Linus Torvalds 已提交
153 154 155
			goto set_ea_exit;

		ea_name += 4; /* skip past os2. prefix */
S
Steve French 已提交
156
		rc = CIFSSMBSetEA(xid, pTcon, full_path, ea_name, ea_value,
157 158
			(__u16)value_size, cifs_sb->local_nls,
			cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
L
Linus Torvalds 已提交
159
	} else {
S
Steve French 已提交
160 161
		int temp;
		temp = strncmp(ea_name, POSIX_ACL_XATTR_ACCESS,
L
Linus Torvalds 已提交
162 163 164
			strlen(POSIX_ACL_XATTR_ACCESS));
		if (temp == 0) {
#ifdef CONFIG_CIFS_POSIX
S
Steve French 已提交
165 166 167 168 169
			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 &
170
						CIFS_MOUNT_MAP_SPECIAL_CHR);
S
Steve French 已提交
171
			cFYI(1, ("set POSIX ACL rc %d", rc));
L
Linus Torvalds 已提交
172
#else
S
Steve French 已提交
173
			cFYI(1, ("set POSIX ACL not supported"));
L
Linus Torvalds 已提交
174
#endif
S
Steve French 已提交
175 176
		} else if (strncmp(ea_name, POSIX_ACL_XATTR_DEFAULT,
				   strlen(POSIX_ACL_XATTR_DEFAULT)) == 0) {
L
Linus Torvalds 已提交
177
#ifdef CONFIG_CIFS_POSIX
S
Steve French 已提交
178 179 180
			if (sb->s_flags & MS_POSIXACL)
				rc = CIFSSMBSetPosixACL(xid, pTcon, full_path,
					ea_value, (const int)value_size,
181
					ACL_TYPE_DEFAULT, cifs_sb->local_nls,
S
Steve French 已提交
182
					cifs_sb->mnt_cifs_flags &
183
						CIFS_MOUNT_MAP_SPECIAL_CHR);
S
Steve French 已提交
184
			cFYI(1, ("set POSIX default ACL rc %d", rc));
L
Linus Torvalds 已提交
185
#else
S
Steve French 已提交
186
			cFYI(1, ("set default POSIX ACL not supported"));
L
Linus Torvalds 已提交
187 188
#endif
		} else {
189 190
			cFYI(1, ("illegal xattr request %s (only user namespace"
				 " supported)", ea_name));
L
Linus Torvalds 已提交
191
		  /* BB what if no namespace prefix? */
S
Steve French 已提交
192
		  /* Should we just pass them to server, except for
L
Linus Torvalds 已提交
193 194 195 196 197
		  system and perhaps security prefixes? */
		}
	}

set_ea_exit:
J
Jesper Juhl 已提交
198
	kfree(full_path);
L
Linus Torvalds 已提交
199 200 201 202 203
	FreeXid(xid);
#endif
	return rc;
}

S
Steve French 已提交
204 205
ssize_t cifs_getxattr(struct dentry *direntry, const char *ea_name,
	void *ea_value, size_t buf_size)
L
Linus Torvalds 已提交
206 207 208 209 210 211
{
	ssize_t rc = -EOPNOTSUPP;
#ifdef CONFIG_CIFS_XATTR
	int xid;
	struct cifs_sb_info *cifs_sb;
	struct cifsTconInfo *pTcon;
S
Steve French 已提交
212 213
	struct super_block *sb;
	char *full_path;
L
Linus Torvalds 已提交
214

S
Steve French 已提交
215
	if (direntry == NULL)
L
Linus Torvalds 已提交
216
		return -EIO;
S
Steve French 已提交
217
	if (direntry->d_inode == NULL)
L
Linus Torvalds 已提交
218 219
		return -EIO;
	sb = direntry->d_inode->i_sb;
S
Steve French 已提交
220
	if (sb == NULL)
L
Linus Torvalds 已提交
221 222 223 224 225 226 227 228
		return -EIO;

	xid = GetXid();

	cifs_sb = CIFS_SB(sb);
	pTcon = cifs_sb->tcon;

	full_path = build_path_from_dentry(direntry);
S
Steve French 已提交
229
	if (full_path == NULL) {
230
		rc = -ENOMEM;
L
Linus Torvalds 已提交
231
		FreeXid(xid);
232
		return rc;
L
Linus Torvalds 已提交
233 234 235
	}
	/* return dos attributes as pseudo xattr */
	/* return alt name if available as pseudo attr */
S
Steve French 已提交
236 237 238 239
	if (ea_name == NULL) {
		cFYI(1, ("Null xattr names not supported"));
	} else if (strncmp(ea_name, CIFS_XATTR_USER_PREFIX, 5) == 0) {
		if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
L
Linus Torvalds 已提交
240 241
			goto get_ea_exit;

S
Steve French 已提交
242 243
		if (strncmp(ea_name, CIFS_XATTR_DOS_ATTRIB, 14) == 0) {
			cFYI(1, ("attempt to query cifs inode metadata"));
L
Linus Torvalds 已提交
244 245 246
			/* revalidate/getattr then populate from inode */
		} /* BB add else when above is implemented */
		ea_name += 5; /* skip past user. prefix */
247
		rc = CIFSSMBQAllEAs(xid, pTcon, full_path, ea_name, ea_value,
248 249
			buf_size, cifs_sb->local_nls,
			cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
S
Steve French 已提交
250 251
	} else if (strncmp(ea_name, CIFS_XATTR_OS2_PREFIX, 4) == 0) {
		if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
L
Linus Torvalds 已提交
252 253 254
			goto get_ea_exit;

		ea_name += 4; /* skip past os2. prefix */
255
		rc = CIFSSMBQAllEAs(xid, pTcon, full_path, ea_name, ea_value,
256 257
			buf_size, cifs_sb->local_nls,
			cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
S
Steve French 已提交
258
	} else if (strncmp(ea_name, POSIX_ACL_XATTR_ACCESS,
259
			  strlen(POSIX_ACL_XATTR_ACCESS)) == 0) {
L
Linus Torvalds 已提交
260
#ifdef CONFIG_CIFS_POSIX
S
Steve French 已提交
261
		if (sb->s_flags & MS_POSIXACL)
262
			rc = CIFSSMBGetPosixACL(xid, pTcon, full_path,
S
Steve French 已提交
263
				ea_value, buf_size, ACL_TYPE_ACCESS,
264
				cifs_sb->local_nls,
S
Steve French 已提交
265
				cifs_sb->mnt_cifs_flags &
266
					CIFS_MOUNT_MAP_SPECIAL_CHR);
S
Steve French 已提交
267
#ifdef CONFIG_CIFS_EXPERIMENTAL
S
Steve French 已提交
268
		else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) {
269
			__u16 fid;
270
			int oplock = 0;
S
Steve French 已提交
271 272
			struct cifs_ntsd *pacl = NULL;
			__u32 buflen = 0;
273
			if (experimEnabled)
S
Steve French 已提交
274 275 276 277 278 279 280
				rc = CIFSSMBOpen(xid, pTcon, full_path,
					FILE_OPEN, GENERIC_READ, 0, &fid,
					&oplock, NULL, cifs_sb->local_nls,
					cifs_sb->mnt_cifs_flags &
					CIFS_MOUNT_MAP_SPECIAL_CHR);
			/* else rc is EOPNOTSUPP from above */

281
			if (rc == 0) {
S
Steve French 已提交
282 283
				rc = CIFSSMBGetCIFSACL(xid, pTcon, fid, &pacl,
						      &buflen);
284
				CIFSSMBClose(xid, pTcon, fid);
285
			}
S
Steve French 已提交
286 287
		}
#endif /* EXPERIMENTAL */
S
Steve French 已提交
288 289
#else
		cFYI(1, ("query POSIX ACL not supported yet"));
L
Linus Torvalds 已提交
290
#endif /* CONFIG_CIFS_POSIX */
S
Steve French 已提交
291
	} else if (strncmp(ea_name, POSIX_ACL_XATTR_DEFAULT,
292
			  strlen(POSIX_ACL_XATTR_DEFAULT)) == 0) {
L
Linus Torvalds 已提交
293
#ifdef CONFIG_CIFS_POSIX
S
Steve French 已提交
294
		if (sb->s_flags & MS_POSIXACL)
295
			rc = CIFSSMBGetPosixACL(xid, pTcon, full_path,
S
Steve French 已提交
296
				ea_value, buf_size, ACL_TYPE_DEFAULT,
297
				cifs_sb->local_nls,
S
Steve French 已提交
298
				cifs_sb->mnt_cifs_flags &
299
					CIFS_MOUNT_MAP_SPECIAL_CHR);
S
Steve French 已提交
300 301
#else
		cFYI(1, ("query POSIX default ACL not supported yet"));
L
Linus Torvalds 已提交
302
#endif
S
Steve French 已提交
303 304 305 306 307 308
	} else if (strncmp(ea_name,
		  CIFS_XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) == 0) {
		cFYI(1, ("Trusted xattr namespace not supported yet"));
	} else if (strncmp(ea_name,
		  CIFS_XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN) == 0) {
		cFYI(1, ("Security xattr namespace not supported yet"));
S
Steve French 已提交
309
	} else
S
Steve French 已提交
310 311 312
		cFYI(1,
		    ("illegal xattr request %s (only user namespace supported)",
			ea_name));
L
Linus Torvalds 已提交
313

S
Steve French 已提交
314
	/* We could add an additional check for streams ie
L
Linus Torvalds 已提交
315
	    if proc/fs/cifs/streamstoxattr is set then
S
Steve French 已提交
316
		search server for EAs or streams to
L
Linus Torvalds 已提交
317 318
		returns as xattrs */

S
Steve French 已提交
319 320
	if (rc == -EINVAL)
		rc = -EOPNOTSUPP;
L
Linus Torvalds 已提交
321 322

get_ea_exit:
J
Jesper Juhl 已提交
323
	kfree(full_path);
L
Linus Torvalds 已提交
324 325 326 327 328
	FreeXid(xid);
#endif
	return rc;
}

S
Steve French 已提交
329
ssize_t cifs_listxattr(struct dentry *direntry, char *data, size_t buf_size)
L
Linus Torvalds 已提交
330 331 332 333 334 335
{
	ssize_t rc = -EOPNOTSUPP;
#ifdef CONFIG_CIFS_XATTR
	int xid;
	struct cifs_sb_info *cifs_sb;
	struct cifsTconInfo *pTcon;
S
Steve French 已提交
336 337
	struct super_block *sb;
	char *full_path;
L
Linus Torvalds 已提交
338

S
Steve French 已提交
339
	if (direntry == NULL)
L
Linus Torvalds 已提交
340
		return -EIO;
S
Steve French 已提交
341
	if (direntry->d_inode == NULL)
L
Linus Torvalds 已提交
342 343
		return -EIO;
	sb = direntry->d_inode->i_sb;
S
Steve French 已提交
344
	if (sb == NULL)
L
Linus Torvalds 已提交
345 346 347 348 349
		return -EIO;

	cifs_sb = CIFS_SB(sb);
	pTcon = cifs_sb->tcon;

S
Steve French 已提交
350
	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
351 352 353 354
		return -EOPNOTSUPP;

	xid = GetXid();

L
Linus Torvalds 已提交
355
	full_path = build_path_from_dentry(direntry);
S
Steve French 已提交
356
	if (full_path == NULL) {
357
		rc = -ENOMEM;
L
Linus Torvalds 已提交
358
		FreeXid(xid);
359
		return rc;
L
Linus Torvalds 已提交
360 361 362 363 364
	}
	/* 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 已提交
365
		search server for EAs or streams to
L
Linus Torvalds 已提交
366
		returns as xattrs */
367 368
	rc = CIFSSMBQAllEAs(xid, pTcon, full_path, NULL, data,
				buf_size, cifs_sb->local_nls,
S
Steve French 已提交
369
				cifs_sb->mnt_cifs_flags &
370
					CIFS_MOUNT_MAP_SPECIAL_CHR);
L
Linus Torvalds 已提交
371

J
Jesper Juhl 已提交
372
	kfree(full_path);
L
Linus Torvalds 已提交
373 374 375 376
	FreeXid(xid);
#endif
	return rc;
}