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
23 #include <linux/posix_acl_xattr.h>
24 #include <linux/slab.h>
25 #include <linux/xattr.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
;
62 FILE_BASIC_INFO info_buf
;
64 if ((value
== NULL
) || (size
!= sizeof(__u32
)))
67 memset(&info_buf
, 0, sizeof(info_buf
));
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
,
74 CIFS_I(inode
)->cifsAttrs
= attrib
;
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
;
86 FILE_BASIC_INFO info_buf
;
88 if ((value
== NULL
) || (size
!= sizeof(__u64
)))
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
,
98 CIFS_I(inode
)->createtime
= creation_time
;
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
;
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
;
116 tlink
= cifs_sb_tlink(cifs_sb
);
118 return PTR_ERR(tlink
);
119 pTcon
= tlink_tcon(tlink
);
123 full_path
= build_path_from_dentry(dentry
);
124 if (full_path
== NULL
) {
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
134 if (size
> MAX_EA_VALUE_SIZE
) {
135 cifs_dbg(FYI
, "size of EA value too large\n");
140 switch (handler
->flags
) {
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
,
147 if (rc
== 0) /* force revalidate of the inode */
148 CIFS_I(inode
)->time
= 0;
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;
159 if (cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_NO_XATTR
)
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
);
169 case XATTR_CIFS_NTSD
:
170 case XATTR_CIFS_NTSD_FULL
: {
171 struct cifs_ntsd
*pacl
;
175 pacl
= kmalloc(size
, GFP_KERNEL
);
179 memcpy(pacl
, value
, size
);
180 if (pTcon
->ses
->server
->ops
->set_acl
) {
184 switch (handler
->flags
) {
185 case XATTR_CIFS_NTSD_FULL
:
186 aclflags
= (CIFS_ACL_OWNER
|
190 case XATTR_CIFS_NTSD
:
191 aclflags
= (CIFS_ACL_OWNER
|
196 aclflags
= CIFS_ACL_DACL
;
199 rc
= pTcon
->ses
->server
->ops
->set_acl(pacl
,
200 size
, inode
, full_path
, aclflags
);
204 if (rc
== 0) /* force revalidate of the inode */
205 CIFS_I(inode
)->time
= 0;
211 case XATTR_ACL_ACCESS
:
212 #ifdef CONFIG_CIFS_POSIX
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 */
223 case XATTR_ACL_DEFAULT
:
224 #ifdef CONFIG_CIFS_POSIX
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 */
239 cifs_put_tlink(tlink
);
243 static int cifs_attrib_get(struct dentry
*dentry
,
244 struct inode
*inode
, void *value
,
250 rc
= cifs_revalidate_dentry_attr(dentry
);
255 if ((value
== NULL
) || (size
== 0))
256 return sizeof(__u32
);
257 else if (size
< sizeof(__u32
))
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
)
273 rc
= cifs_revalidate_dentry_attr(dentry
);
277 if ((value
== NULL
) || (size
== 0))
278 return sizeof(__u64
);
279 else if (size
< sizeof(__u64
))
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
;
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
;
301 tlink
= cifs_sb_tlink(cifs_sb
);
303 return PTR_ERR(tlink
);
304 pTcon
= tlink_tcon(tlink
);
308 full_path
= build_path_from_dentry(dentry
);
309 if (full_path
== NULL
) {
314 /* return alt name if available as pseudo attr */
315 switch (handler
->flags
) {
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
);
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
);
328 if (cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_NO_XATTR
)
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
);
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
;
354 pacl
= pTcon
->ses
->server
->ops
->get_acl(cifs_sb
,
355 inode
, full_path
, &acllen
, extra_info
);
358 cifs_dbg(VFS
, "%s: error %zd getting sec desc\n",
365 memcpy(value
, pacl
, acllen
);
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
,
379 cifs_remap(cifs_sb
));
380 #endif /* CONFIG_CIFS_POSIX */
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
,
389 cifs_remap(cifs_sb
));
390 #endif /* CONFIG_CIFS_POSIX */
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
405 cifs_put_tlink(tlink
);
409 ssize_t
cifs_listxattr(struct dentry
*direntry
, char *data
, size_t buf_size
)
411 ssize_t rc
= -EOPNOTSUPP
;
413 struct cifs_sb_info
*cifs_sb
= CIFS_SB(direntry
->d_sb
);
414 struct tcon_link
*tlink
;
415 struct cifs_tcon
*pTcon
;
418 if (cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_NO_XATTR
)
421 tlink
= cifs_sb_tlink(cifs_sb
);
423 return PTR_ERR(tlink
);
424 pTcon
= tlink_tcon(tlink
);
428 full_path
= build_path_from_dentry(direntry
);
429 if (full_path
== NULL
) {
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
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
);
446 cifs_put_tlink(tlink
);
450 static const struct xattr_handler cifs_user_xattr_handler
= {
451 .prefix
= XATTR_USER_PREFIX
,
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
,
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
,