include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit...
[linux-2.6-block.git] / fs / cifs / xattr.c
CommitLineData
1da177e4
LT
1/*
2 * fs/cifs/xattr.c
3 *
79a58d1f 4 * Copyright (c) International Business Machines Corp., 2003, 2007
1da177e4
LT
5 * Author(s): Steve French (sfrench@us.ibm.com)
6 *
7 * This library is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published
9 * by the Free Software Foundation; either version 2.1 of the License, or
10 * (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
15 * the GNU Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22#include <linux/fs.h>
23#include <linux/posix_acl_xattr.h>
5a0e3ad6 24#include <linux/slab.h>
1da177e4
LT
25#include "cifsfs.h"
26#include "cifspdu.h"
27#include "cifsglob.h"
28#include "cifsproto.h"
29#include "cifs_debug.h"
30
31#define MAX_EA_VALUE_SIZE 65535
32#define CIFS_XATTR_DOS_ATTRIB "user.DosAttrib"
33#define CIFS_XATTR_USER_PREFIX "user."
34#define CIFS_XATTR_SYSTEM_PREFIX "system."
35#define CIFS_XATTR_OS2_PREFIX "os2."
36#define CIFS_XATTR_SECURITY_PREFIX ".security"
37#define CIFS_XATTR_TRUSTED_PREFIX "trusted."
38#define XATTR_TRUSTED_PREFIX_LEN 8
39#define XATTR_SECURITY_PREFIX_LEN 9
40/* BB need to add server (Samba e.g) support for security and trusted prefix */
50c2f753 41
1da177e4
LT
42
43
79a58d1f 44int cifs_removexattr(struct dentry *direntry, const char *ea_name)
1da177e4
LT
45{
46 int rc = -EOPNOTSUPP;
47#ifdef CONFIG_CIFS_XATTR
48 int xid;
49 struct cifs_sb_info *cifs_sb;
50 struct cifsTconInfo *pTcon;
79a58d1f
SF
51 struct super_block *sb;
52 char *full_path;
53
54 if (direntry == NULL)
1da177e4 55 return -EIO;
79a58d1f 56 if (direntry->d_inode == NULL)
1da177e4
LT
57 return -EIO;
58 sb = direntry->d_inode->i_sb;
79a58d1f 59 if (sb == NULL)
1da177e4
LT
60 return -EIO;
61 xid = GetXid();
79a58d1f 62
1da177e4
LT
63 cifs_sb = CIFS_SB(sb);
64 pTcon = cifs_sb->tcon;
79a58d1f 65
1da177e4 66 full_path = build_path_from_dentry(direntry);
79a58d1f 67 if (full_path == NULL) {
0f3bc09e 68 rc = -ENOMEM;
1da177e4 69 FreeXid(xid);
0f3bc09e 70 return rc;
1da177e4 71 }
79a58d1f
SF
72 if (ea_name == NULL) {
73 cFYI(1, ("Null xattr names not supported"));
74 } else if (strncmp(ea_name, CIFS_XATTR_USER_PREFIX, 5)
75 && (strncmp(ea_name, CIFS_XATTR_OS2_PREFIX, 4))) {
76 cFYI(1,
77 ("illegal xattr request %s (only user namespace supported)",
78 ea_name));
1da177e4
LT
79 /* BB what if no namespace prefix? */
80 /* Should we just pass them to server, except for
81 system and perhaps security prefixes? */
82 } else {
79a58d1f 83 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
1da177e4
LT
84 goto remove_ea_exit;
85
79a58d1f
SF
86 ea_name += 5; /* skip past user. prefix */
87 rc = CIFSSMBSetEA(xid, pTcon, full_path, ea_name, NULL,
737b758c
SF
88 (__u16)0, cifs_sb->local_nls,
89 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
1da177e4
LT
90 }
91remove_ea_exit:
f99d49ad 92 kfree(full_path);
1da177e4
LT
93 FreeXid(xid);
94#endif
95 return rc;
96}
97
79a58d1f
SF
98int cifs_setxattr(struct dentry *direntry, const char *ea_name,
99 const void *ea_value, size_t value_size, int flags)
1da177e4
LT
100{
101 int rc = -EOPNOTSUPP;
102#ifdef CONFIG_CIFS_XATTR
103 int xid;
104 struct cifs_sb_info *cifs_sb;
105 struct cifsTconInfo *pTcon;
79a58d1f
SF
106 struct super_block *sb;
107 char *full_path;
1da177e4 108
79a58d1f 109 if (direntry == NULL)
1da177e4 110 return -EIO;
79a58d1f 111 if (direntry->d_inode == NULL)
1da177e4
LT
112 return -EIO;
113 sb = direntry->d_inode->i_sb;
79a58d1f 114 if (sb == NULL)
1da177e4
LT
115 return -EIO;
116 xid = GetXid();
117
118 cifs_sb = CIFS_SB(sb);
119 pTcon = cifs_sb->tcon;
120
1da177e4 121 full_path = build_path_from_dentry(direntry);
79a58d1f 122 if (full_path == NULL) {
0f3bc09e 123 rc = -ENOMEM;
1da177e4 124 FreeXid(xid);
0f3bc09e 125 return rc;
1da177e4
LT
126 }
127 /* return dos attributes as pseudo xattr */
128 /* return alt name if available as pseudo attr */
129
130 /* if proc/fs/cifs/streamstoxattr is set then
79a58d1f 131 search server for EAs or streams to
1da177e4 132 returns as xattrs */
79a58d1f
SF
133 if (value_size > MAX_EA_VALUE_SIZE) {
134 cFYI(1, ("size of EA value too large"));
f99d49ad 135 kfree(full_path);
1da177e4
LT
136 FreeXid(xid);
137 return -EOPNOTSUPP;
138 }
139
79a58d1f
SF
140 if (ea_name == NULL) {
141 cFYI(1, ("Null xattr names not supported"));
142 } else if (strncmp(ea_name, CIFS_XATTR_USER_PREFIX, 5) == 0) {
143 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
1da177e4 144 goto set_ea_exit;
ad7a2926 145 if (strncmp(ea_name, CIFS_XATTR_DOS_ATTRIB, 14) == 0)
79a58d1f 146 cFYI(1, ("attempt to set cifs inode metadata"));
ad7a2926 147
1da177e4 148 ea_name += 5; /* skip past user. prefix */
79a58d1f 149 rc = CIFSSMBSetEA(xid, pTcon, full_path, ea_name, ea_value,
737b758c
SF
150 (__u16)value_size, cifs_sb->local_nls,
151 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
79a58d1f
SF
152 } else if (strncmp(ea_name, CIFS_XATTR_OS2_PREFIX, 4) == 0) {
153 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
1da177e4
LT
154 goto set_ea_exit;
155
156 ea_name += 4; /* skip past os2. prefix */
79a58d1f 157 rc = CIFSSMBSetEA(xid, pTcon, full_path, ea_name, ea_value,
737b758c
SF
158 (__u16)value_size, cifs_sb->local_nls,
159 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
1da177e4 160 } else {
79a58d1f
SF
161 int temp;
162 temp = strncmp(ea_name, POSIX_ACL_XATTR_ACCESS,
1da177e4
LT
163 strlen(POSIX_ACL_XATTR_ACCESS));
164 if (temp == 0) {
165#ifdef CONFIG_CIFS_POSIX
79a58d1f
SF
166 if (sb->s_flags & MS_POSIXACL)
167 rc = CIFSSMBSetPosixACL(xid, pTcon, full_path,
168 ea_value, (const int)value_size,
169 ACL_TYPE_ACCESS, cifs_sb->local_nls,
170 cifs_sb->mnt_cifs_flags &
737b758c 171 CIFS_MOUNT_MAP_SPECIAL_CHR);
79a58d1f 172 cFYI(1, ("set POSIX ACL rc %d", rc));
1da177e4 173#else
79a58d1f 174 cFYI(1, ("set POSIX ACL not supported"));
1da177e4 175#endif
79a58d1f
SF
176 } else if (strncmp(ea_name, POSIX_ACL_XATTR_DEFAULT,
177 strlen(POSIX_ACL_XATTR_DEFAULT)) == 0) {
1da177e4 178#ifdef CONFIG_CIFS_POSIX
79a58d1f
SF
179 if (sb->s_flags & MS_POSIXACL)
180 rc = CIFSSMBSetPosixACL(xid, pTcon, full_path,
181 ea_value, (const int)value_size,
737b758c 182 ACL_TYPE_DEFAULT, cifs_sb->local_nls,
79a58d1f 183 cifs_sb->mnt_cifs_flags &
737b758c 184 CIFS_MOUNT_MAP_SPECIAL_CHR);
79a58d1f 185 cFYI(1, ("set POSIX default ACL rc %d", rc));
1da177e4 186#else
79a58d1f 187 cFYI(1, ("set default POSIX ACL not supported"));
1da177e4
LT
188#endif
189 } else {
63135e08
SF
190 cFYI(1, ("illegal xattr request %s (only user namespace"
191 " supported)", ea_name));
1da177e4 192 /* BB what if no namespace prefix? */
79a58d1f 193 /* Should we just pass them to server, except for
1da177e4
LT
194 system and perhaps security prefixes? */
195 }
196 }
197
198set_ea_exit:
f99d49ad 199 kfree(full_path);
1da177e4
LT
200 FreeXid(xid);
201#endif
202 return rc;
203}
204
79a58d1f
SF
205ssize_t cifs_getxattr(struct dentry *direntry, const char *ea_name,
206 void *ea_value, size_t buf_size)
1da177e4
LT
207{
208 ssize_t rc = -EOPNOTSUPP;
209#ifdef CONFIG_CIFS_XATTR
210 int xid;
211 struct cifs_sb_info *cifs_sb;
212 struct cifsTconInfo *pTcon;
79a58d1f
SF
213 struct super_block *sb;
214 char *full_path;
1da177e4 215
79a58d1f 216 if (direntry == NULL)
1da177e4 217 return -EIO;
79a58d1f 218 if (direntry->d_inode == NULL)
1da177e4
LT
219 return -EIO;
220 sb = direntry->d_inode->i_sb;
79a58d1f 221 if (sb == NULL)
1da177e4
LT
222 return -EIO;
223
224 xid = GetXid();
225
226 cifs_sb = CIFS_SB(sb);
227 pTcon = cifs_sb->tcon;
228
1da177e4 229 full_path = build_path_from_dentry(direntry);
79a58d1f 230 if (full_path == NULL) {
0f3bc09e 231 rc = -ENOMEM;
1da177e4 232 FreeXid(xid);
0f3bc09e 233 return rc;
1da177e4
LT
234 }
235 /* return dos attributes as pseudo xattr */
236 /* return alt name if available as pseudo attr */
79a58d1f
SF
237 if (ea_name == NULL) {
238 cFYI(1, ("Null xattr names not supported"));
239 } else if (strncmp(ea_name, CIFS_XATTR_USER_PREFIX, 5) == 0) {
240 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
1da177e4
LT
241 goto get_ea_exit;
242
79a58d1f
SF
243 if (strncmp(ea_name, CIFS_XATTR_DOS_ATTRIB, 14) == 0) {
244 cFYI(1, ("attempt to query cifs inode metadata"));
1da177e4
LT
245 /* revalidate/getattr then populate from inode */
246 } /* BB add else when above is implemented */
247 ea_name += 5; /* skip past user. prefix */
31c0519f 248 rc = CIFSSMBQAllEAs(xid, pTcon, full_path, ea_name, ea_value,
737b758c
SF
249 buf_size, cifs_sb->local_nls,
250 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
79a58d1f
SF
251 } else if (strncmp(ea_name, CIFS_XATTR_OS2_PREFIX, 4) == 0) {
252 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
1da177e4
LT
253 goto get_ea_exit;
254
255 ea_name += 4; /* skip past os2. prefix */
31c0519f 256 rc = CIFSSMBQAllEAs(xid, pTcon, full_path, ea_name, ea_value,
737b758c
SF
257 buf_size, cifs_sb->local_nls,
258 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
79a58d1f 259 } else if (strncmp(ea_name, POSIX_ACL_XATTR_ACCESS,
0a4b92c0 260 strlen(POSIX_ACL_XATTR_ACCESS)) == 0) {
1da177e4 261#ifdef CONFIG_CIFS_POSIX
79a58d1f 262 if (sb->s_flags & MS_POSIXACL)
1da0c78b 263 rc = CIFSSMBGetPosixACL(xid, pTcon, full_path,
79a58d1f 264 ea_value, buf_size, ACL_TYPE_ACCESS,
737b758c 265 cifs_sb->local_nls,
79a58d1f 266 cifs_sb->mnt_cifs_flags &
737b758c 267 CIFS_MOUNT_MAP_SPECIAL_CHR);
d12fd121 268#ifdef CONFIG_CIFS_EXPERIMENTAL
ad7a2926 269 else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) {
0a4b92c0 270 __u16 fid;
4b18f2a9 271 int oplock = 0;
630f3f0c
SF
272 struct cifs_ntsd *pacl = NULL;
273 __u32 buflen = 0;
63d2583f 274 if (experimEnabled)
d12fd121
SF
275 rc = CIFSSMBOpen(xid, pTcon, full_path,
276 FILE_OPEN, GENERIC_READ, 0, &fid,
277 &oplock, NULL, cifs_sb->local_nls,
278 cifs_sb->mnt_cifs_flags &
279 CIFS_MOUNT_MAP_SPECIAL_CHR);
280 /* else rc is EOPNOTSUPP from above */
281
63d2583f 282 if (rc == 0) {
630f3f0c
SF
283 rc = CIFSSMBGetCIFSACL(xid, pTcon, fid, &pacl,
284 &buflen);
2fe87f02 285 CIFSSMBClose(xid, pTcon, fid);
0a4b92c0 286 }
d12fd121
SF
287 }
288#endif /* EXPERIMENTAL */
79a58d1f
SF
289#else
290 cFYI(1, ("query POSIX ACL not supported yet"));
1da177e4 291#endif /* CONFIG_CIFS_POSIX */
79a58d1f 292 } else if (strncmp(ea_name, POSIX_ACL_XATTR_DEFAULT,
0a4b92c0 293 strlen(POSIX_ACL_XATTR_DEFAULT)) == 0) {
1da177e4 294#ifdef CONFIG_CIFS_POSIX
79a58d1f 295 if (sb->s_flags & MS_POSIXACL)
1da0c78b 296 rc = CIFSSMBGetPosixACL(xid, pTcon, full_path,
79a58d1f 297 ea_value, buf_size, ACL_TYPE_DEFAULT,
737b758c 298 cifs_sb->local_nls,
79a58d1f 299 cifs_sb->mnt_cifs_flags &
737b758c 300 CIFS_MOUNT_MAP_SPECIAL_CHR);
79a58d1f
SF
301#else
302 cFYI(1, ("query POSIX default ACL not supported yet"));
1da177e4 303#endif
79a58d1f
SF
304 } else if (strncmp(ea_name,
305 CIFS_XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) == 0) {
306 cFYI(1, ("Trusted xattr namespace not supported yet"));
307 } else if (strncmp(ea_name,
308 CIFS_XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN) == 0) {
309 cFYI(1, ("Security xattr namespace not supported yet"));
ad7a2926 310 } else
79a58d1f
SF
311 cFYI(1,
312 ("illegal xattr request %s (only user namespace supported)",
313 ea_name));
1da177e4 314
79a58d1f 315 /* We could add an additional check for streams ie
1da177e4 316 if proc/fs/cifs/streamstoxattr is set then
79a58d1f 317 search server for EAs or streams to
1da177e4
LT
318 returns as xattrs */
319
79a58d1f
SF
320 if (rc == -EINVAL)
321 rc = -EOPNOTSUPP;
1da177e4
LT
322
323get_ea_exit:
f99d49ad 324 kfree(full_path);
1da177e4
LT
325 FreeXid(xid);
326#endif
327 return rc;
328}
329
79a58d1f 330ssize_t cifs_listxattr(struct dentry *direntry, char *data, size_t buf_size)
1da177e4
LT
331{
332 ssize_t rc = -EOPNOTSUPP;
333#ifdef CONFIG_CIFS_XATTR
334 int xid;
335 struct cifs_sb_info *cifs_sb;
336 struct cifsTconInfo *pTcon;
79a58d1f
SF
337 struct super_block *sb;
338 char *full_path;
1da177e4 339
79a58d1f 340 if (direntry == NULL)
1da177e4 341 return -EIO;
79a58d1f 342 if (direntry->d_inode == NULL)
1da177e4
LT
343 return -EIO;
344 sb = direntry->d_inode->i_sb;
79a58d1f 345 if (sb == NULL)
1da177e4 346 return -EIO;
1da177e4
LT
347
348 cifs_sb = CIFS_SB(sb);
349 pTcon = cifs_sb->tcon;
350
79a58d1f 351 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
ea4c07d7
SF
352 return -EOPNOTSUPP;
353
354 xid = GetXid();
355
1da177e4 356 full_path = build_path_from_dentry(direntry);
79a58d1f 357 if (full_path == NULL) {
0f3bc09e 358 rc = -ENOMEM;
1da177e4 359 FreeXid(xid);
0f3bc09e 360 return rc;
1da177e4
LT
361 }
362 /* return dos attributes as pseudo xattr */
363 /* return alt name if available as pseudo attr */
364
365 /* if proc/fs/cifs/streamstoxattr is set then
79a58d1f 366 search server for EAs or streams to
1da177e4 367 returns as xattrs */
31c0519f
JL
368 rc = CIFSSMBQAllEAs(xid, pTcon, full_path, NULL, data,
369 buf_size, cifs_sb->local_nls,
79a58d1f 370 cifs_sb->mnt_cifs_flags &
737b758c 371 CIFS_MOUNT_MAP_SPECIAL_CHR);
1da177e4 372
f99d49ad 373 kfree(full_path);
1da177e4
LT
374 FreeXid(xid);
375#endif
376 return rc;
377}