xattr.c 10.5 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) {
L
Linus Torvalds 已提交
67 68 69
		FreeXid(xid);
		return -ENOMEM;
	}
S
Steve French 已提交
70 71 72 73 74 75 76
	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 已提交
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;

S
Steve French 已提交
84 85
		ea_name += 5; /* skip past user. prefix */
		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);
L
Linus Torvalds 已提交
91 92 93 94 95
	FreeXid(xid);
#endif
	return rc;
}

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

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

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

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

S
Steve French 已提交
137 138 139 140
	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 已提交
141
			goto set_ea_exit;
S
Steve French 已提交
142 143
		if (strncmp(ea_name, CIFS_XATTR_DOS_ATTRIB, 14) == 0) {
			cFYI(1, ("attempt to set cifs inode metadata"));
L
Linus Torvalds 已提交
144 145
		}
		ea_name += 5; /* skip past user. prefix */
S
Steve French 已提交
146
		rc = CIFSSMBSetEA(xid, pTcon, full_path, ea_name, ea_value,
147 148
			(__u16)value_size, cifs_sb->local_nls,
			cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
S
Steve French 已提交
149 150
	} else if (strncmp(ea_name, CIFS_XATTR_OS2_PREFIX, 4) == 0) {
		if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
L
Linus Torvalds 已提交
151 152 153
			goto set_ea_exit;

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

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

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

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

	xid = GetXid();

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

	full_path = build_path_from_dentry(direntry);
S
Steve French 已提交
227
	if (full_path == NULL) {
L
Linus Torvalds 已提交
228 229 230 231 232
		FreeXid(xid);
		return -ENOMEM;
	}
	/* return dos attributes as pseudo xattr */
	/* return alt name if available as pseudo attr */
S
Steve French 已提交
233 234 235 236
	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 已提交
237 238
			goto get_ea_exit;

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

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

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

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

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

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

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

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

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

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

	xid = GetXid();

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

J
Jesper Juhl 已提交
369
	kfree(full_path);
L
Linus Torvalds 已提交
370 371 372 373
	FreeXid(xid);
#endif
	return rc;
}