Merge tag 'regmap-fix-v5.11-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux/fpc-iii.git] / fs / cifs / xattr.c
blob6b658a1172ef00e1ec5ff7c40435346561e3e290
1 /*
2 * fs/cifs/xattr.c
4 * Copyright (c) International Business Machines Corp., 2003, 2007
5 * Author(s): Steve French (sfrench@us.ibm.com)
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.
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.
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
22 #include <linux/fs.h>
23 #include <linux/posix_acl_xattr.h>
24 #include <linux/slab.h>
25 #include <linux/xattr.h>
26 #include "cifsfs.h"
27 #include "cifspdu.h"
28 #include "cifsglob.h"
29 #include "cifsproto.h"
30 #include "cifs_debug.h"
31 #include "cifs_fs_sb.h"
32 #include "cifs_unicode.h"
34 #define MAX_EA_VALUE_SIZE CIFSMaxBufSize
35 #define CIFS_XATTR_CIFS_ACL "system.cifs_acl" /* DACL only */
36 #define CIFS_XATTR_CIFS_NTSD "system.cifs_ntsd" /* owner plus DACL */
37 #define CIFS_XATTR_CIFS_NTSD_FULL "system.cifs_ntsd_full" /* owner/DACL/SACL */
38 #define CIFS_XATTR_ATTRIB "cifs.dosattrib" /* full name: user.cifs.dosattrib */
39 #define CIFS_XATTR_CREATETIME "cifs.creationtime" /* user.cifs.creationtime */
41 * Although these three are just aliases for the above, need to move away from
42 * confusing users and using the 20+ year old term 'cifs' when it is no longer
43 * secure, replaced by SMB2 (then even more highly secure SMB3) many years ago
45 #define SMB3_XATTR_CIFS_ACL "system.smb3_acl" /* DACL only */
46 #define SMB3_XATTR_CIFS_NTSD "system.smb3_ntsd" /* owner plus DACL */
47 #define SMB3_XATTR_CIFS_NTSD_FULL "system.smb3_ntsd_full" /* owner/DACL/SACL */
48 #define SMB3_XATTR_ATTRIB "smb3.dosattrib" /* full name: user.smb3.dosattrib */
49 #define SMB3_XATTR_CREATETIME "smb3.creationtime" /* user.smb3.creationtime */
50 /* BB need to add server (Samba e.g) support for security and trusted prefix */
52 enum { XATTR_USER, XATTR_CIFS_ACL, XATTR_ACL_ACCESS, XATTR_ACL_DEFAULT,
53 XATTR_CIFS_NTSD, XATTR_CIFS_NTSD_FULL };
55 static int cifs_attrib_set(unsigned int xid, struct cifs_tcon *pTcon,
56 struct inode *inode, char *full_path,
57 const void *value, size_t size)
59 ssize_t rc = -EOPNOTSUPP;
60 __u32 *pattrib = (__u32 *)value;
61 __u32 attrib;
62 FILE_BASIC_INFO info_buf;
64 if ((value == NULL) || (size != sizeof(__u32)))
65 return -ERANGE;
67 memset(&info_buf, 0, sizeof(info_buf));
68 attrib = *pattrib;
69 info_buf.Attributes = cpu_to_le32(attrib);
70 if (pTcon->ses->server->ops->set_file_info)
71 rc = pTcon->ses->server->ops->set_file_info(inode, full_path,
72 &info_buf, xid);
73 if (rc == 0)
74 CIFS_I(inode)->cifsAttrs = attrib;
76 return rc;
79 static int cifs_creation_time_set(unsigned int xid, struct cifs_tcon *pTcon,
80 struct inode *inode, char *full_path,
81 const void *value, size_t size)
83 ssize_t rc = -EOPNOTSUPP;
84 __u64 *pcreation_time = (__u64 *)value;
85 __u64 creation_time;
86 FILE_BASIC_INFO info_buf;
88 if ((value == NULL) || (size != sizeof(__u64)))
89 return -ERANGE;
91 memset(&info_buf, 0, sizeof(info_buf));
92 creation_time = *pcreation_time;
93 info_buf.CreationTime = cpu_to_le64(creation_time);
94 if (pTcon->ses->server->ops->set_file_info)
95 rc = pTcon->ses->server->ops->set_file_info(inode, full_path,
96 &info_buf, xid);
97 if (rc == 0)
98 CIFS_I(inode)->createtime = creation_time;
100 return rc;
103 static int cifs_xattr_set(const struct xattr_handler *handler,
104 struct dentry *dentry, struct inode *inode,
105 const char *name, const void *value,
106 size_t size, int flags)
108 int rc = -EOPNOTSUPP;
109 unsigned int xid;
110 struct super_block *sb = dentry->d_sb;
111 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
112 struct tcon_link *tlink;
113 struct cifs_tcon *pTcon;
114 char *full_path;
116 tlink = cifs_sb_tlink(cifs_sb);
117 if (IS_ERR(tlink))
118 return PTR_ERR(tlink);
119 pTcon = tlink_tcon(tlink);
121 xid = get_xid();
123 full_path = build_path_from_dentry(dentry);
124 if (full_path == NULL) {
125 rc = -ENOMEM;
126 goto out;
128 /* return dos attributes as pseudo xattr */
129 /* return alt name if available as pseudo attr */
131 /* if proc/fs/cifs/streamstoxattr is set then
132 search server for EAs or streams to
133 returns as xattrs */
134 if (size > MAX_EA_VALUE_SIZE) {
135 cifs_dbg(FYI, "size of EA value too large\n");
136 rc = -EOPNOTSUPP;
137 goto out;
140 switch (handler->flags) {
141 case XATTR_USER:
142 cifs_dbg(FYI, "%s:setting user xattr %s\n", __func__, name);
143 if ((strcmp(name, CIFS_XATTR_ATTRIB) == 0) ||
144 (strcmp(name, SMB3_XATTR_ATTRIB) == 0)) {
145 rc = cifs_attrib_set(xid, pTcon, inode, full_path,
146 value, size);
147 if (rc == 0) /* force revalidate of the inode */
148 CIFS_I(inode)->time = 0;
149 break;
150 } else if ((strcmp(name, CIFS_XATTR_CREATETIME) == 0) ||
151 (strcmp(name, SMB3_XATTR_CREATETIME) == 0)) {
152 rc = cifs_creation_time_set(xid, pTcon, inode,
153 full_path, value, size);
154 if (rc == 0) /* force revalidate of the inode */
155 CIFS_I(inode)->time = 0;
156 break;
159 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
160 goto out;
162 if (pTcon->ses->server->ops->set_EA)
163 rc = pTcon->ses->server->ops->set_EA(xid, pTcon,
164 full_path, name, value, (__u16)size,
165 cifs_sb->local_nls, cifs_sb);
166 break;
168 case XATTR_CIFS_ACL:
169 case XATTR_CIFS_NTSD:
170 case XATTR_CIFS_NTSD_FULL: {
171 struct cifs_ntsd *pacl;
173 if (!value)
174 goto out;
175 pacl = kmalloc(size, GFP_KERNEL);
176 if (!pacl) {
177 rc = -ENOMEM;
178 } else {
179 memcpy(pacl, value, size);
180 if (pTcon->ses->server->ops->set_acl) {
181 int aclflags = 0;
182 rc = 0;
184 switch (handler->flags) {
185 case XATTR_CIFS_NTSD_FULL:
186 aclflags = (CIFS_ACL_OWNER |
187 CIFS_ACL_DACL |
188 CIFS_ACL_SACL);
189 break;
190 case XATTR_CIFS_NTSD:
191 aclflags = (CIFS_ACL_OWNER |
192 CIFS_ACL_DACL);
193 break;
194 case XATTR_CIFS_ACL:
195 default:
196 aclflags = CIFS_ACL_DACL;
199 rc = pTcon->ses->server->ops->set_acl(pacl,
200 size, inode, full_path, aclflags);
201 } else {
202 rc = -EOPNOTSUPP;
204 if (rc == 0) /* force revalidate of the inode */
205 CIFS_I(inode)->time = 0;
206 kfree(pacl);
208 break;
211 case XATTR_ACL_ACCESS:
212 #ifdef CONFIG_CIFS_POSIX
213 if (!value)
214 goto out;
215 if (sb->s_flags & SB_POSIXACL)
216 rc = CIFSSMBSetPosixACL(xid, pTcon, full_path,
217 value, (const int)size,
218 ACL_TYPE_ACCESS, cifs_sb->local_nls,
219 cifs_remap(cifs_sb));
220 #endif /* CONFIG_CIFS_POSIX */
221 break;
223 case XATTR_ACL_DEFAULT:
224 #ifdef CONFIG_CIFS_POSIX
225 if (!value)
226 goto out;
227 if (sb->s_flags & SB_POSIXACL)
228 rc = CIFSSMBSetPosixACL(xid, pTcon, full_path,
229 value, (const int)size,
230 ACL_TYPE_DEFAULT, cifs_sb->local_nls,
231 cifs_remap(cifs_sb));
232 #endif /* CONFIG_CIFS_POSIX */
233 break;
236 out:
237 kfree(full_path);
238 free_xid(xid);
239 cifs_put_tlink(tlink);
240 return rc;
243 static int cifs_attrib_get(struct dentry *dentry,
244 struct inode *inode, void *value,
245 size_t size)
247 ssize_t rc;
248 __u32 *pattribute;
250 rc = cifs_revalidate_dentry_attr(dentry);
252 if (rc)
253 return rc;
255 if ((value == NULL) || (size == 0))
256 return sizeof(__u32);
257 else if (size < sizeof(__u32))
258 return -ERANGE;
260 /* return dos attributes as pseudo xattr */
261 pattribute = (__u32 *)value;
262 *pattribute = CIFS_I(inode)->cifsAttrs;
264 return sizeof(__u32);
267 static int cifs_creation_time_get(struct dentry *dentry, struct inode *inode,
268 void *value, size_t size)
270 ssize_t rc;
271 __u64 *pcreatetime;
273 rc = cifs_revalidate_dentry_attr(dentry);
274 if (rc)
275 return rc;
277 if ((value == NULL) || (size == 0))
278 return sizeof(__u64);
279 else if (size < sizeof(__u64))
280 return -ERANGE;
282 /* return dos attributes as pseudo xattr */
283 pcreatetime = (__u64 *)value;
284 *pcreatetime = CIFS_I(inode)->createtime;
285 return sizeof(__u64);
289 static int cifs_xattr_get(const struct xattr_handler *handler,
290 struct dentry *dentry, struct inode *inode,
291 const char *name, void *value, size_t size)
293 ssize_t rc = -EOPNOTSUPP;
294 unsigned int xid;
295 struct super_block *sb = dentry->d_sb;
296 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
297 struct tcon_link *tlink;
298 struct cifs_tcon *pTcon;
299 char *full_path;
301 tlink = cifs_sb_tlink(cifs_sb);
302 if (IS_ERR(tlink))
303 return PTR_ERR(tlink);
304 pTcon = tlink_tcon(tlink);
306 xid = get_xid();
308 full_path = build_path_from_dentry(dentry);
309 if (full_path == NULL) {
310 rc = -ENOMEM;
311 goto out;
314 /* return alt name if available as pseudo attr */
315 switch (handler->flags) {
316 case XATTR_USER:
317 cifs_dbg(FYI, "%s:querying user xattr %s\n", __func__, name);
318 if ((strcmp(name, CIFS_XATTR_ATTRIB) == 0) ||
319 (strcmp(name, SMB3_XATTR_ATTRIB) == 0)) {
320 rc = cifs_attrib_get(dentry, inode, value, size);
321 break;
322 } else if ((strcmp(name, CIFS_XATTR_CREATETIME) == 0) ||
323 (strcmp(name, SMB3_XATTR_CREATETIME) == 0)) {
324 rc = cifs_creation_time_get(dentry, inode, value, size);
325 break;
328 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
329 goto out;
331 if (pTcon->ses->server->ops->query_all_EAs)
332 rc = pTcon->ses->server->ops->query_all_EAs(xid, pTcon,
333 full_path, name, value, size, cifs_sb);
334 break;
336 case XATTR_CIFS_ACL:
337 case XATTR_CIFS_NTSD:
338 case XATTR_CIFS_NTSD_FULL: {
340 * fetch owner, DACL, and SACL if asked for full descriptor,
341 * fetch owner and DACL otherwise
343 u32 acllen, extra_info;
344 struct cifs_ntsd *pacl;
346 if (pTcon->ses->server->ops->get_acl == NULL)
347 goto out; /* rc already EOPNOTSUPP */
349 if (handler->flags == XATTR_CIFS_NTSD_FULL) {
350 extra_info = SACL_SECINFO;
351 } else {
352 extra_info = 0;
354 pacl = pTcon->ses->server->ops->get_acl(cifs_sb,
355 inode, full_path, &acllen, extra_info);
356 if (IS_ERR(pacl)) {
357 rc = PTR_ERR(pacl);
358 cifs_dbg(VFS, "%s: error %zd getting sec desc\n",
359 __func__, rc);
360 } else {
361 if (value) {
362 if (acllen > size)
363 acllen = -ERANGE;
364 else
365 memcpy(value, pacl, acllen);
367 rc = acllen;
368 kfree(pacl);
370 break;
373 case XATTR_ACL_ACCESS:
374 #ifdef CONFIG_CIFS_POSIX
375 if (sb->s_flags & SB_POSIXACL)
376 rc = CIFSSMBGetPosixACL(xid, pTcon, full_path,
377 value, size, ACL_TYPE_ACCESS,
378 cifs_sb->local_nls,
379 cifs_remap(cifs_sb));
380 #endif /* CONFIG_CIFS_POSIX */
381 break;
383 case XATTR_ACL_DEFAULT:
384 #ifdef CONFIG_CIFS_POSIX
385 if (sb->s_flags & SB_POSIXACL)
386 rc = CIFSSMBGetPosixACL(xid, pTcon, full_path,
387 value, size, ACL_TYPE_DEFAULT,
388 cifs_sb->local_nls,
389 cifs_remap(cifs_sb));
390 #endif /* CONFIG_CIFS_POSIX */
391 break;
394 /* We could add an additional check for streams ie
395 if proc/fs/cifs/streamstoxattr is set then
396 search server for EAs or streams to
397 returns as xattrs */
399 if (rc == -EINVAL)
400 rc = -EOPNOTSUPP;
402 out:
403 kfree(full_path);
404 free_xid(xid);
405 cifs_put_tlink(tlink);
406 return rc;
409 ssize_t cifs_listxattr(struct dentry *direntry, char *data, size_t buf_size)
411 ssize_t rc = -EOPNOTSUPP;
412 unsigned int xid;
413 struct cifs_sb_info *cifs_sb = CIFS_SB(direntry->d_sb);
414 struct tcon_link *tlink;
415 struct cifs_tcon *pTcon;
416 char *full_path;
418 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
419 return -EOPNOTSUPP;
421 tlink = cifs_sb_tlink(cifs_sb);
422 if (IS_ERR(tlink))
423 return PTR_ERR(tlink);
424 pTcon = tlink_tcon(tlink);
426 xid = get_xid();
428 full_path = build_path_from_dentry(direntry);
429 if (full_path == NULL) {
430 rc = -ENOMEM;
431 goto list_ea_exit;
433 /* return dos attributes as pseudo xattr */
434 /* return alt name if available as pseudo attr */
436 /* if proc/fs/cifs/streamstoxattr is set then
437 search server for EAs or streams to
438 returns as xattrs */
440 if (pTcon->ses->server->ops->query_all_EAs)
441 rc = pTcon->ses->server->ops->query_all_EAs(xid, pTcon,
442 full_path, NULL, data, buf_size, cifs_sb);
443 list_ea_exit:
444 kfree(full_path);
445 free_xid(xid);
446 cifs_put_tlink(tlink);
447 return rc;
450 static const struct xattr_handler cifs_user_xattr_handler = {
451 .prefix = XATTR_USER_PREFIX,
452 .flags = XATTR_USER,
453 .get = cifs_xattr_get,
454 .set = cifs_xattr_set,
457 /* os2.* attributes are treated like user.* attributes */
458 static const struct xattr_handler cifs_os2_xattr_handler = {
459 .prefix = XATTR_OS2_PREFIX,
460 .flags = XATTR_USER,
461 .get = cifs_xattr_get,
462 .set = cifs_xattr_set,
465 static const struct xattr_handler cifs_cifs_acl_xattr_handler = {
466 .name = CIFS_XATTR_CIFS_ACL,
467 .flags = XATTR_CIFS_ACL,
468 .get = cifs_xattr_get,
469 .set = cifs_xattr_set,
473 * Although this is just an alias for the above, need to move away from
474 * confusing users and using the 20 year old term 'cifs' when it is no
475 * longer secure and was replaced by SMB2/SMB3 a long time ago, and
476 * SMB3 and later are highly secure.
478 static const struct xattr_handler smb3_acl_xattr_handler = {
479 .name = SMB3_XATTR_CIFS_ACL,
480 .flags = XATTR_CIFS_ACL,
481 .get = cifs_xattr_get,
482 .set = cifs_xattr_set,
485 static const struct xattr_handler cifs_cifs_ntsd_xattr_handler = {
486 .name = CIFS_XATTR_CIFS_NTSD,
487 .flags = XATTR_CIFS_NTSD,
488 .get = cifs_xattr_get,
489 .set = cifs_xattr_set,
493 * Although this is just an alias for the above, need to move away from
494 * confusing users and using the 20 year old term 'cifs' when it is no
495 * longer secure and was replaced by SMB2/SMB3 a long time ago, and
496 * SMB3 and later are highly secure.
498 static const struct xattr_handler smb3_ntsd_xattr_handler = {
499 .name = SMB3_XATTR_CIFS_NTSD,
500 .flags = XATTR_CIFS_NTSD,
501 .get = cifs_xattr_get,
502 .set = cifs_xattr_set,
505 static const struct xattr_handler cifs_cifs_ntsd_full_xattr_handler = {
506 .name = CIFS_XATTR_CIFS_NTSD_FULL,
507 .flags = XATTR_CIFS_NTSD_FULL,
508 .get = cifs_xattr_get,
509 .set = cifs_xattr_set,
513 * Although this is just an alias for the above, need to move away from
514 * confusing users and using the 20 year old term 'cifs' when it is no
515 * longer secure and was replaced by SMB2/SMB3 a long time ago, and
516 * SMB3 and later are highly secure.
518 static const struct xattr_handler smb3_ntsd_full_xattr_handler = {
519 .name = SMB3_XATTR_CIFS_NTSD_FULL,
520 .flags = XATTR_CIFS_NTSD_FULL,
521 .get = cifs_xattr_get,
522 .set = cifs_xattr_set,
526 static const struct xattr_handler cifs_posix_acl_access_xattr_handler = {
527 .name = XATTR_NAME_POSIX_ACL_ACCESS,
528 .flags = XATTR_ACL_ACCESS,
529 .get = cifs_xattr_get,
530 .set = cifs_xattr_set,
533 static const struct xattr_handler cifs_posix_acl_default_xattr_handler = {
534 .name = XATTR_NAME_POSIX_ACL_DEFAULT,
535 .flags = XATTR_ACL_DEFAULT,
536 .get = cifs_xattr_get,
537 .set = cifs_xattr_set,
540 const struct xattr_handler *cifs_xattr_handlers[] = {
541 &cifs_user_xattr_handler,
542 &cifs_os2_xattr_handler,
543 &cifs_cifs_acl_xattr_handler,
544 &smb3_acl_xattr_handler, /* alias for above since avoiding "cifs" */
545 &cifs_cifs_ntsd_xattr_handler,
546 &smb3_ntsd_xattr_handler, /* alias for above since avoiding "cifs" */
547 &cifs_cifs_ntsd_full_xattr_handler,
548 &smb3_ntsd_full_xattr_handler, /* alias for above since avoiding "cifs" */
549 &cifs_posix_acl_access_xattr_handler,
550 &cifs_posix_acl_default_xattr_handler,
551 NULL