xattr.c 10.4 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 40 41 42
 *   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 */
  


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 {
S
Steve French 已提交
187
			cFYI(1, ("illegal xattr request %s (only user namespace supported)", ea_name));
L
Linus Torvalds 已提交
188
		  /* BB what if no namespace prefix? */
S
Steve French 已提交
189
		  /* Should we just pass them to server, except for
L
Linus Torvalds 已提交
190 191 192 193 194
		  system and perhaps security prefixes? */
		}
	}

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

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

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

	xid = GetXid();

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

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

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

		ea_name += 4; /* skip past os2. prefix */
S
Steve French 已提交
251
		rc = CIFSSMBQueryEA(xid, pTcon, full_path, ea_name, ea_value,
252 253
			buf_size, cifs_sb->local_nls,
			cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
S
Steve French 已提交
254
	} else if (strncmp(ea_name, POSIX_ACL_XATTR_ACCESS,
255
			  strlen(POSIX_ACL_XATTR_ACCESS)) == 0) {
L
Linus Torvalds 已提交
256
#ifdef CONFIG_CIFS_POSIX
S
Steve French 已提交
257
		if (sb->s_flags & MS_POSIXACL)
258
			rc = CIFSSMBGetPosixACL(xid, pTcon, full_path,
S
Steve French 已提交
259
				ea_value, buf_size, ACL_TYPE_ACCESS,
260
				cifs_sb->local_nls,
S
Steve French 已提交
261
				cifs_sb->mnt_cifs_flags &
262
					CIFS_MOUNT_MAP_SPECIAL_CHR);
263 264 265 266 267 268 269 270 271 272 273 274
/*		else if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) {
			__u16 fid;
			int oplock = FALSE;
			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);
			if(rc == 0) {
				rc = CIFSSMBGetCIFSACL(xid, pTcon, fid,
					ea_value, buf_size,
					ACL_TYPE_ACCESS);
275
				CIFSSMBClose(xid, pTcon, fid);
276 277
			}
		} */  /* BB enable after fixing up return data */
S
Steve French 已提交
278 279
#else
		cFYI(1, ("query POSIX ACL not supported yet"));
L
Linus Torvalds 已提交
280
#endif /* CONFIG_CIFS_POSIX */
S
Steve French 已提交
281
	} else if (strncmp(ea_name, POSIX_ACL_XATTR_DEFAULT,
282
			  strlen(POSIX_ACL_XATTR_DEFAULT)) == 0) {
L
Linus Torvalds 已提交
283
#ifdef CONFIG_CIFS_POSIX
S
Steve French 已提交
284
		if (sb->s_flags & MS_POSIXACL)
285
			rc = CIFSSMBGetPosixACL(xid, pTcon, full_path,
S
Steve French 已提交
286
				ea_value, buf_size, ACL_TYPE_DEFAULT,
287
				cifs_sb->local_nls,
S
Steve French 已提交
288
				cifs_sb->mnt_cifs_flags &
289
					CIFS_MOUNT_MAP_SPECIAL_CHR);
S
Steve French 已提交
290 291
#else
		cFYI(1, ("query POSIX default ACL not supported yet"));
L
Linus Torvalds 已提交
292
#endif
S
Steve French 已提交
293 294 295 296 297 298
	} 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 已提交
299
	} else {
S
Steve French 已提交
300 301 302
		cFYI(1,
		    ("illegal xattr request %s (only user namespace supported)",
			ea_name));
L
Linus Torvalds 已提交
303 304
	}

S
Steve French 已提交
305
	/* We could add an additional check for streams ie
L
Linus Torvalds 已提交
306
	    if proc/fs/cifs/streamstoxattr is set then
S
Steve French 已提交
307
		search server for EAs or streams to
L
Linus Torvalds 已提交
308 309
		returns as xattrs */

S
Steve French 已提交
310 311
	if (rc == -EINVAL)
		rc = -EOPNOTSUPP;
L
Linus Torvalds 已提交
312 313

get_ea_exit:
J
Jesper Juhl 已提交
314
	kfree(full_path);
L
Linus Torvalds 已提交
315 316 317 318 319
	FreeXid(xid);
#endif
	return rc;
}

S
Steve French 已提交
320
ssize_t cifs_listxattr(struct dentry *direntry, char *data, size_t buf_size)
L
Linus Torvalds 已提交
321 322 323 324 325 326
{
	ssize_t rc = -EOPNOTSUPP;
#ifdef CONFIG_CIFS_XATTR
	int xid;
	struct cifs_sb_info *cifs_sb;
	struct cifsTconInfo *pTcon;
S
Steve French 已提交
327 328
	struct super_block *sb;
	char *full_path;
L
Linus Torvalds 已提交
329

S
Steve French 已提交
330
	if (direntry == NULL)
L
Linus Torvalds 已提交
331
		return -EIO;
S
Steve French 已提交
332
	if (direntry->d_inode == NULL)
L
Linus Torvalds 已提交
333 334
		return -EIO;
	sb = direntry->d_inode->i_sb;
S
Steve French 已提交
335
	if (sb == NULL)
L
Linus Torvalds 已提交
336 337 338 339 340
		return -EIO;

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

S
Steve French 已提交
341
	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
342 343 344 345
		return -EOPNOTSUPP;

	xid = GetXid();

L
Linus Torvalds 已提交
346
	full_path = build_path_from_dentry(direntry);
S
Steve French 已提交
347
	if (full_path == NULL) {
L
Linus Torvalds 已提交
348 349 350 351 352 353 354
		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 已提交
355
		search server for EAs or streams to
L
Linus Torvalds 已提交
356
		returns as xattrs */
S
Steve French 已提交
357
	rc = CIFSSMBQAllEAs(xid, pTcon, full_path, data, buf_size,
358
				cifs_sb->local_nls,
S
Steve French 已提交
359
				cifs_sb->mnt_cifs_flags &
360
					CIFS_MOUNT_MAP_SPECIAL_CHR);
L
Linus Torvalds 已提交
361

J
Jesper Juhl 已提交
362
	kfree(full_path);
L
Linus Torvalds 已提交
363 364 365 366
	FreeXid(xid);
#endif
	return rc;
}