]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - fs/cifs/xattr.c
Merge tag 'fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/arm...
[mirror_ubuntu-bionic-kernel.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>
f995e740 25#include <linux/xattr.h>
1da177e4
LT
26#include "cifsfs.h"
27#include "cifspdu.h"
28#include "cifsglob.h"
29#include "cifsproto.h"
30#include "cifs_debug.h"
31
32#define MAX_EA_VALUE_SIZE 65535
33#define CIFS_XATTR_DOS_ATTRIB "user.DosAttrib"
fbeba8bb 34#define CIFS_XATTR_CIFS_ACL "system.cifs_acl"
1da177e4 35
f995e740 36/* BB need to add server (Samba e.g) support for security and trusted prefix */
1da177e4 37
79a58d1f 38int cifs_removexattr(struct dentry *direntry, const char *ea_name)
1da177e4
LT
39{
40 int rc = -EOPNOTSUPP;
41#ifdef CONFIG_CIFS_XATTR
42 int xid;
43 struct cifs_sb_info *cifs_sb;
7ffec372 44 struct tcon_link *tlink;
96daf2b0 45 struct cifs_tcon *pTcon;
79a58d1f 46 struct super_block *sb;
7ffec372 47 char *full_path = NULL;
79a58d1f
SF
48
49 if (direntry == NULL)
1da177e4 50 return -EIO;
79a58d1f 51 if (direntry->d_inode == NULL)
1da177e4
LT
52 return -EIO;
53 sb = direntry->d_inode->i_sb;
79a58d1f 54 if (sb == NULL)
1da177e4 55 return -EIO;
79a58d1f 56
1da177e4 57 cifs_sb = CIFS_SB(sb);
7ffec372
JL
58 tlink = cifs_sb_tlink(cifs_sb);
59 if (IS_ERR(tlink))
60 return PTR_ERR(tlink);
61 pTcon = tlink_tcon(tlink);
62
63 xid = GetXid();
79a58d1f 64
1da177e4 65 full_path = build_path_from_dentry(direntry);
79a58d1f 66 if (full_path == NULL) {
0f3bc09e 67 rc = -ENOMEM;
7ffec372 68 goto remove_ea_exit;
1da177e4 69 }
79a58d1f 70 if (ea_name == NULL) {
b6b38f70 71 cFYI(1, "Null xattr names not supported");
f995e740
MZ
72 } else if (strncmp(ea_name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN)
73 && (strncmp(ea_name, XATTR_OS2_PREFIX, XATTR_OS2_PREFIX_LEN))) {
79a58d1f 74 cFYI(1,
b6b38f70
JP
75 "illegal xattr request %s (only user namespace supported)",
76 ea_name);
1da177e4
LT
77 /* BB what if no namespace prefix? */
78 /* Should we just pass them to server, except for
79 system and perhaps security prefixes? */
80 } else {
79a58d1f 81 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
1da177e4
LT
82 goto remove_ea_exit;
83
f995e740 84 ea_name += XATTR_USER_PREFIX_LEN; /* skip past user. prefix */
79a58d1f 85 rc = CIFSSMBSetEA(xid, pTcon, full_path, ea_name, NULL,
737b758c
SF
86 (__u16)0, cifs_sb->local_nls,
87 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
1da177e4
LT
88 }
89remove_ea_exit:
f99d49ad 90 kfree(full_path);
1da177e4 91 FreeXid(xid);
7ffec372 92 cifs_put_tlink(tlink);
1da177e4
LT
93#endif
94 return rc;
95}
96
79a58d1f
SF
97int cifs_setxattr(struct dentry *direntry, const char *ea_name,
98 const void *ea_value, size_t value_size, int flags)
1da177e4
LT
99{
100 int rc = -EOPNOTSUPP;
101#ifdef CONFIG_CIFS_XATTR
102 int xid;
103 struct cifs_sb_info *cifs_sb;
7ffec372 104 struct tcon_link *tlink;
96daf2b0 105 struct cifs_tcon *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 115 return -EIO;
1da177e4
LT
116
117 cifs_sb = CIFS_SB(sb);
7ffec372
JL
118 tlink = cifs_sb_tlink(cifs_sb);
119 if (IS_ERR(tlink))
120 return PTR_ERR(tlink);
121 pTcon = tlink_tcon(tlink);
122
123 xid = GetXid();
1da177e4 124
1da177e4 125 full_path = build_path_from_dentry(direntry);
79a58d1f 126 if (full_path == NULL) {
0f3bc09e 127 rc = -ENOMEM;
7ffec372 128 goto set_ea_exit;
1da177e4
LT
129 }
130 /* return dos attributes as pseudo xattr */
131 /* return alt name if available as pseudo attr */
132
133 /* if proc/fs/cifs/streamstoxattr is set then
79a58d1f 134 search server for EAs or streams to
1da177e4 135 returns as xattrs */
79a58d1f 136 if (value_size > MAX_EA_VALUE_SIZE) {
b6b38f70 137 cFYI(1, "size of EA value too large");
7ffec372
JL
138 rc = -EOPNOTSUPP;
139 goto set_ea_exit;
1da177e4
LT
140 }
141
79a58d1f 142 if (ea_name == NULL) {
b6b38f70 143 cFYI(1, "Null xattr names not supported");
f995e740
MZ
144 } else if (strncmp(ea_name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN)
145 == 0) {
79a58d1f 146 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
1da177e4 147 goto set_ea_exit;
ad7a2926 148 if (strncmp(ea_name, CIFS_XATTR_DOS_ATTRIB, 14) == 0)
b6b38f70 149 cFYI(1, "attempt to set cifs inode metadata");
ad7a2926 150
f995e740 151 ea_name += XATTR_USER_PREFIX_LEN; /* skip past user. prefix */
79a58d1f 152 rc = CIFSSMBSetEA(xid, pTcon, full_path, ea_name, ea_value,
737b758c
SF
153 (__u16)value_size, cifs_sb->local_nls,
154 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
f995e740
MZ
155 } else if (strncmp(ea_name, XATTR_OS2_PREFIX, XATTR_OS2_PREFIX_LEN)
156 == 0) {
79a58d1f 157 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
1da177e4
LT
158 goto set_ea_exit;
159
f995e740 160 ea_name += XATTR_OS2_PREFIX_LEN; /* skip past os2. prefix */
79a58d1f 161 rc = CIFSSMBSetEA(xid, pTcon, full_path, ea_name, ea_value,
737b758c
SF
162 (__u16)value_size, cifs_sb->local_nls,
163 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
b73b9a4b
SF
164 } else if (strncmp(ea_name, CIFS_XATTR_CIFS_ACL,
165 strlen(CIFS_XATTR_CIFS_ACL)) == 0) {
b0f8ef20
SN
166#ifdef CONFIG_CIFS_ACL
167 struct cifs_ntsd *pacl;
b73b9a4b
SF
168 pacl = kmalloc(value_size, GFP_KERNEL);
169 if (!pacl) {
170 cFYI(1, "%s: Can't allocate memory for ACL",
171 __func__);
172 rc = -ENOMEM;
173 } else {
b73b9a4b
SF
174 memcpy(pacl, ea_value, value_size);
175 rc = set_cifs_acl(pacl, value_size,
a5ff3769 176 direntry->d_inode, full_path, CIFS_ACL_DACL);
b73b9a4b
SF
177 if (rc == 0) /* force revalidate of the inode */
178 CIFS_I(direntry->d_inode)->time = 0;
179 kfree(pacl);
b0f8ef20 180 }
b73b9a4b
SF
181#else
182 cFYI(1, "Set CIFS ACL not supported yet");
183#endif /* CONFIG_CIFS_ACL */
1da177e4 184 } else {
79a58d1f
SF
185 int temp;
186 temp = strncmp(ea_name, POSIX_ACL_XATTR_ACCESS,
1da177e4
LT
187 strlen(POSIX_ACL_XATTR_ACCESS));
188 if (temp == 0) {
189#ifdef CONFIG_CIFS_POSIX
79a58d1f
SF
190 if (sb->s_flags & MS_POSIXACL)
191 rc = CIFSSMBSetPosixACL(xid, pTcon, full_path,
192 ea_value, (const int)value_size,
193 ACL_TYPE_ACCESS, cifs_sb->local_nls,
194 cifs_sb->mnt_cifs_flags &
737b758c 195 CIFS_MOUNT_MAP_SPECIAL_CHR);
b6b38f70 196 cFYI(1, "set POSIX ACL rc %d", rc);
1da177e4 197#else
b6b38f70 198 cFYI(1, "set POSIX ACL not supported");
1da177e4 199#endif
79a58d1f
SF
200 } else if (strncmp(ea_name, POSIX_ACL_XATTR_DEFAULT,
201 strlen(POSIX_ACL_XATTR_DEFAULT)) == 0) {
1da177e4 202#ifdef CONFIG_CIFS_POSIX
79a58d1f
SF
203 if (sb->s_flags & MS_POSIXACL)
204 rc = CIFSSMBSetPosixACL(xid, pTcon, full_path,
205 ea_value, (const int)value_size,
737b758c 206 ACL_TYPE_DEFAULT, cifs_sb->local_nls,
79a58d1f 207 cifs_sb->mnt_cifs_flags &
737b758c 208 CIFS_MOUNT_MAP_SPECIAL_CHR);
b6b38f70 209 cFYI(1, "set POSIX default ACL rc %d", rc);
1da177e4 210#else
b6b38f70 211 cFYI(1, "set default POSIX ACL not supported");
1da177e4
LT
212#endif
213 } else {
b6b38f70
JP
214 cFYI(1, "illegal xattr request %s (only user namespace"
215 " supported)", ea_name);
1da177e4 216 /* BB what if no namespace prefix? */
79a58d1f 217 /* Should we just pass them to server, except for
1da177e4
LT
218 system and perhaps security prefixes? */
219 }
220 }
221
222set_ea_exit:
f99d49ad 223 kfree(full_path);
1da177e4 224 FreeXid(xid);
7ffec372 225 cifs_put_tlink(tlink);
1da177e4
LT
226#endif
227 return rc;
228}
229
79a58d1f
SF
230ssize_t cifs_getxattr(struct dentry *direntry, const char *ea_name,
231 void *ea_value, size_t buf_size)
1da177e4
LT
232{
233 ssize_t rc = -EOPNOTSUPP;
234#ifdef CONFIG_CIFS_XATTR
235 int xid;
236 struct cifs_sb_info *cifs_sb;
7ffec372 237 struct tcon_link *tlink;
96daf2b0 238 struct cifs_tcon *pTcon;
79a58d1f
SF
239 struct super_block *sb;
240 char *full_path;
1da177e4 241
79a58d1f 242 if (direntry == NULL)
1da177e4 243 return -EIO;
79a58d1f 244 if (direntry->d_inode == NULL)
1da177e4
LT
245 return -EIO;
246 sb = direntry->d_inode->i_sb;
79a58d1f 247 if (sb == NULL)
1da177e4
LT
248 return -EIO;
249
1da177e4 250 cifs_sb = CIFS_SB(sb);
7ffec372
JL
251 tlink = cifs_sb_tlink(cifs_sb);
252 if (IS_ERR(tlink))
253 return PTR_ERR(tlink);
254 pTcon = tlink_tcon(tlink);
255
256 xid = GetXid();
1da177e4 257
1da177e4 258 full_path = build_path_from_dentry(direntry);
79a58d1f 259 if (full_path == NULL) {
0f3bc09e 260 rc = -ENOMEM;
7ffec372 261 goto get_ea_exit;
1da177e4
LT
262 }
263 /* return dos attributes as pseudo xattr */
264 /* return alt name if available as pseudo attr */
79a58d1f 265 if (ea_name == NULL) {
b6b38f70 266 cFYI(1, "Null xattr names not supported");
f995e740
MZ
267 } else if (strncmp(ea_name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN)
268 == 0) {
79a58d1f 269 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
1da177e4
LT
270 goto get_ea_exit;
271
79a58d1f 272 if (strncmp(ea_name, CIFS_XATTR_DOS_ATTRIB, 14) == 0) {
b6b38f70 273 cFYI(1, "attempt to query cifs inode metadata");
1da177e4
LT
274 /* revalidate/getattr then populate from inode */
275 } /* BB add else when above is implemented */
f995e740 276 ea_name += XATTR_USER_PREFIX_LEN; /* skip past user. prefix */
31c0519f 277 rc = CIFSSMBQAllEAs(xid, pTcon, full_path, ea_name, ea_value,
737b758c
SF
278 buf_size, cifs_sb->local_nls,
279 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
f995e740 280 } else if (strncmp(ea_name, XATTR_OS2_PREFIX, XATTR_OS2_PREFIX_LEN) == 0) {
79a58d1f 281 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
1da177e4
LT
282 goto get_ea_exit;
283
f995e740 284 ea_name += XATTR_OS2_PREFIX_LEN; /* skip past os2. prefix */
31c0519f 285 rc = CIFSSMBQAllEAs(xid, pTcon, full_path, ea_name, ea_value,
737b758c
SF
286 buf_size, cifs_sb->local_nls,
287 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
79a58d1f 288 } else if (strncmp(ea_name, POSIX_ACL_XATTR_ACCESS,
0a4b92c0 289 strlen(POSIX_ACL_XATTR_ACCESS)) == 0) {
1da177e4 290#ifdef CONFIG_CIFS_POSIX
79a58d1f 291 if (sb->s_flags & MS_POSIXACL)
1da0c78b 292 rc = CIFSSMBGetPosixACL(xid, pTcon, full_path,
79a58d1f 293 ea_value, buf_size, ACL_TYPE_ACCESS,
737b758c 294 cifs_sb->local_nls,
79a58d1f 295 cifs_sb->mnt_cifs_flags &
737b758c 296 CIFS_MOUNT_MAP_SPECIAL_CHR);
79a58d1f 297#else
fbeba8bb 298 cFYI(1, "Query POSIX ACL not supported yet");
1da177e4 299#endif /* CONFIG_CIFS_POSIX */
79a58d1f 300 } else if (strncmp(ea_name, POSIX_ACL_XATTR_DEFAULT,
0a4b92c0 301 strlen(POSIX_ACL_XATTR_DEFAULT)) == 0) {
1da177e4 302#ifdef CONFIG_CIFS_POSIX
79a58d1f 303 if (sb->s_flags & MS_POSIXACL)
1da0c78b 304 rc = CIFSSMBGetPosixACL(xid, pTcon, full_path,
79a58d1f 305 ea_value, buf_size, ACL_TYPE_DEFAULT,
737b758c 306 cifs_sb->local_nls,
79a58d1f 307 cifs_sb->mnt_cifs_flags &
737b758c 308 CIFS_MOUNT_MAP_SPECIAL_CHR);
79a58d1f 309#else
fbeba8bb
SP
310 cFYI(1, "Query POSIX default ACL not supported yet");
311#endif /* CONFIG_CIFS_POSIX */
312 } else if (strncmp(ea_name, CIFS_XATTR_CIFS_ACL,
313 strlen(CIFS_XATTR_CIFS_ACL)) == 0) {
314#ifdef CONFIG_CIFS_ACL
315 u32 acllen;
316 struct cifs_ntsd *pacl;
317
318 pacl = get_cifs_acl(cifs_sb, direntry->d_inode,
319 full_path, &acllen);
320 if (IS_ERR(pacl)) {
321 rc = PTR_ERR(pacl);
322 cERROR(1, "%s: error %zd getting sec desc",
323 __func__, rc);
324 } else {
325 if (ea_value) {
326 if (acllen > buf_size)
327 acllen = -ERANGE;
328 else
329 memcpy(ea_value, pacl, acllen);
330 }
331 rc = acllen;
332 kfree(pacl);
333 }
334#else
335 cFYI(1, "Query CIFS ACL not supported yet");
336#endif /* CONFIG_CIFS_ACL */
79a58d1f 337 } else if (strncmp(ea_name,
f995e740 338 XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) == 0) {
b6b38f70 339 cFYI(1, "Trusted xattr namespace not supported yet");
79a58d1f 340 } else if (strncmp(ea_name,
f995e740 341 XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN) == 0) {
b6b38f70 342 cFYI(1, "Security xattr namespace not supported yet");
ad7a2926 343 } else
79a58d1f 344 cFYI(1,
b6b38f70
JP
345 "illegal xattr request %s (only user namespace supported)",
346 ea_name);
1da177e4 347
79a58d1f 348 /* We could add an additional check for streams ie
1da177e4 349 if proc/fs/cifs/streamstoxattr is set then
79a58d1f 350 search server for EAs or streams to
1da177e4
LT
351 returns as xattrs */
352
79a58d1f
SF
353 if (rc == -EINVAL)
354 rc = -EOPNOTSUPP;
1da177e4
LT
355
356get_ea_exit:
f99d49ad 357 kfree(full_path);
1da177e4 358 FreeXid(xid);
7ffec372 359 cifs_put_tlink(tlink);
1da177e4
LT
360#endif
361 return rc;
362}
363
79a58d1f 364ssize_t cifs_listxattr(struct dentry *direntry, char *data, size_t buf_size)
1da177e4
LT
365{
366 ssize_t rc = -EOPNOTSUPP;
367#ifdef CONFIG_CIFS_XATTR
368 int xid;
369 struct cifs_sb_info *cifs_sb;
7ffec372 370 struct tcon_link *tlink;
96daf2b0 371 struct cifs_tcon *pTcon;
79a58d1f
SF
372 struct super_block *sb;
373 char *full_path;
1da177e4 374
79a58d1f 375 if (direntry == NULL)
1da177e4 376 return -EIO;
79a58d1f 377 if (direntry->d_inode == NULL)
1da177e4
LT
378 return -EIO;
379 sb = direntry->d_inode->i_sb;
79a58d1f 380 if (sb == NULL)
1da177e4 381 return -EIO;
1da177e4
LT
382
383 cifs_sb = CIFS_SB(sb);
79a58d1f 384 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
ea4c07d7
SF
385 return -EOPNOTSUPP;
386
7ffec372
JL
387 tlink = cifs_sb_tlink(cifs_sb);
388 if (IS_ERR(tlink))
389 return PTR_ERR(tlink);
390 pTcon = tlink_tcon(tlink);
391
ea4c07d7
SF
392 xid = GetXid();
393
1da177e4 394 full_path = build_path_from_dentry(direntry);
79a58d1f 395 if (full_path == NULL) {
0f3bc09e 396 rc = -ENOMEM;
7ffec372 397 goto list_ea_exit;
1da177e4
LT
398 }
399 /* return dos attributes as pseudo xattr */
400 /* return alt name if available as pseudo attr */
401
402 /* if proc/fs/cifs/streamstoxattr is set then
79a58d1f 403 search server for EAs or streams to
1da177e4 404 returns as xattrs */
31c0519f
JL
405 rc = CIFSSMBQAllEAs(xid, pTcon, full_path, NULL, data,
406 buf_size, cifs_sb->local_nls,
79a58d1f 407 cifs_sb->mnt_cifs_flags &
737b758c 408 CIFS_MOUNT_MAP_SPECIAL_CHR);
1da177e4 409
7ffec372 410list_ea_exit:
f99d49ad 411 kfree(full_path);
1da177e4 412 FreeXid(xid);
7ffec372 413 cifs_put_tlink(tlink);
1da177e4
LT
414#endif
415 return rc;
416}