4 * Copyright (c) International Business Machines Corp., 2003, 2007
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 65535
35 #define CIFS_XATTR_CIFS_ACL "system.cifs_acl"
37 /* BB need to add server (Samba e.g) support for security and trusted prefix */
39 enum { XATTR_USER, XATTR_CIFS_ACL, XATTR_ACL_ACCESS, XATTR_ACL_DEFAULT };
41 static int cifs_xattr_set(const struct xattr_handler *handler,
42 struct dentry *dentry, struct inode *inode,
43 const char *name, const void *value,
44 size_t size, int flags)
48 struct super_block *sb = dentry->d_sb;
49 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
50 struct tcon_link *tlink;
51 struct cifs_tcon *pTcon;
54 tlink = cifs_sb_tlink(cifs_sb);
56 return PTR_ERR(tlink);
57 pTcon = tlink_tcon(tlink);
61 full_path = build_path_from_dentry(dentry);
62 if (full_path == NULL) {
66 /* return dos attributes as pseudo xattr */
67 /* return alt name if available as pseudo attr */
69 /* if proc/fs/cifs/streamstoxattr is set then
70 search server for EAs or streams to
72 if (size > MAX_EA_VALUE_SIZE) {
73 cifs_dbg(FYI, "size of EA value too large\n");
78 switch (handler->flags) {
80 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
83 if (pTcon->ses->server->ops->set_EA)
84 rc = pTcon->ses->server->ops->set_EA(xid, pTcon,
85 full_path, name, value, (__u16)size,
86 cifs_sb->local_nls, cifs_remap(cifs_sb));
89 case XATTR_CIFS_ACL: {
90 #ifdef CONFIG_CIFS_ACL
91 struct cifs_ntsd *pacl;
95 pacl = kmalloc(size, GFP_KERNEL);
99 memcpy(pacl, value, size);
101 pTcon->ses->server->ops->set_acl)
102 rc = pTcon->ses->server->ops->set_acl(pacl,
104 full_path, CIFS_ACL_DACL);
107 if (rc == 0) /* force revalidate of the inode */
108 CIFS_I(inode)->time = 0;
111 #endif /* CONFIG_CIFS_ACL */
115 case XATTR_ACL_ACCESS:
116 #ifdef CONFIG_CIFS_POSIX
119 if (sb->s_flags & MS_POSIXACL)
120 rc = CIFSSMBSetPosixACL(xid, pTcon, full_path,
121 value, (const int)size,
122 ACL_TYPE_ACCESS, cifs_sb->local_nls,
123 cifs_remap(cifs_sb));
124 #endif /* CONFIG_CIFS_POSIX */
127 case XATTR_ACL_DEFAULT:
128 #ifdef CONFIG_CIFS_POSIX
131 if (sb->s_flags & MS_POSIXACL)
132 rc = CIFSSMBSetPosixACL(xid, pTcon, full_path,
133 value, (const int)size,
134 ACL_TYPE_DEFAULT, cifs_sb->local_nls,
135 cifs_remap(cifs_sb));
136 #endif /* CONFIG_CIFS_POSIX */
143 cifs_put_tlink(tlink);
147 static int cifs_xattr_get(const struct xattr_handler *handler,
148 struct dentry *dentry, struct inode *inode,
149 const char *name, void *value, size_t size)
151 ssize_t rc = -EOPNOTSUPP;
153 struct super_block *sb = dentry->d_sb;
154 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
155 struct tcon_link *tlink;
156 struct cifs_tcon *pTcon;
159 tlink = cifs_sb_tlink(cifs_sb);
161 return PTR_ERR(tlink);
162 pTcon = tlink_tcon(tlink);
166 full_path = build_path_from_dentry(dentry);
167 if (full_path == NULL) {
171 /* return dos attributes as pseudo xattr */
172 /* return alt name if available as pseudo attr */
173 switch (handler->flags) {
175 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
178 if (pTcon->ses->server->ops->query_all_EAs)
179 rc = pTcon->ses->server->ops->query_all_EAs(xid, pTcon,
180 full_path, name, value, size,
181 cifs_sb->local_nls, cifs_remap(cifs_sb));
184 case XATTR_CIFS_ACL: {
185 #ifdef CONFIG_CIFS_ACL
187 struct cifs_ntsd *pacl;
189 if (pTcon->ses->server->ops->get_acl == NULL)
190 goto out; /* rc already EOPNOTSUPP */
192 pacl = pTcon->ses->server->ops->get_acl(cifs_sb,
193 inode, full_path, &acllen);
196 cifs_dbg(VFS, "%s: error %zd getting sec desc\n",
203 memcpy(value, pacl, acllen);
208 #endif /* CONFIG_CIFS_ACL */
212 case XATTR_ACL_ACCESS:
213 #ifdef CONFIG_CIFS_POSIX
214 if (sb->s_flags & MS_POSIXACL)
215 rc = CIFSSMBGetPosixACL(xid, pTcon, full_path,
216 value, size, ACL_TYPE_ACCESS,
218 cifs_remap(cifs_sb));
219 #endif /* CONFIG_CIFS_POSIX */
222 case XATTR_ACL_DEFAULT:
223 #ifdef CONFIG_CIFS_POSIX
224 if (sb->s_flags & MS_POSIXACL)
225 rc = CIFSSMBGetPosixACL(xid, pTcon, full_path,
226 value, size, ACL_TYPE_DEFAULT,
228 cifs_remap(cifs_sb));
229 #endif /* CONFIG_CIFS_POSIX */
233 /* We could add an additional check for streams ie
234 if proc/fs/cifs/streamstoxattr is set then
235 search server for EAs or streams to
244 cifs_put_tlink(tlink);
248 ssize_t cifs_listxattr(struct dentry *direntry, char *data, size_t buf_size)
250 ssize_t rc = -EOPNOTSUPP;
252 struct cifs_sb_info *cifs_sb = CIFS_SB(direntry->d_sb);
253 struct tcon_link *tlink;
254 struct cifs_tcon *pTcon;
257 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
260 tlink = cifs_sb_tlink(cifs_sb);
262 return PTR_ERR(tlink);
263 pTcon = tlink_tcon(tlink);
267 full_path = build_path_from_dentry(direntry);
268 if (full_path == NULL) {
272 /* return dos attributes as pseudo xattr */
273 /* return alt name if available as pseudo attr */
275 /* if proc/fs/cifs/streamstoxattr is set then
276 search server for EAs or streams to
279 if (pTcon->ses->server->ops->query_all_EAs)
280 rc = pTcon->ses->server->ops->query_all_EAs(xid, pTcon,
281 full_path, NULL, data, buf_size,
282 cifs_sb->local_nls, cifs_remap(cifs_sb));
286 cifs_put_tlink(tlink);
290 static const struct xattr_handler cifs_user_xattr_handler = {
291 .prefix = XATTR_USER_PREFIX,
293 .get = cifs_xattr_get,
294 .set = cifs_xattr_set,
297 /* os2.* attributes are treated like user.* attributes */
298 static const struct xattr_handler cifs_os2_xattr_handler = {
299 .prefix = XATTR_OS2_PREFIX,
301 .get = cifs_xattr_get,
302 .set = cifs_xattr_set,
305 static const struct xattr_handler cifs_cifs_acl_xattr_handler = {
306 .name = CIFS_XATTR_CIFS_ACL,
307 .flags = XATTR_CIFS_ACL,
308 .get = cifs_xattr_get,
309 .set = cifs_xattr_set,
312 static const struct xattr_handler cifs_posix_acl_access_xattr_handler = {
313 .name = XATTR_NAME_POSIX_ACL_ACCESS,
314 .flags = XATTR_ACL_ACCESS,
315 .get = cifs_xattr_get,
316 .set = cifs_xattr_set,
319 static const struct xattr_handler cifs_posix_acl_default_xattr_handler = {
320 .name = XATTR_NAME_POSIX_ACL_DEFAULT,
321 .flags = XATTR_ACL_DEFAULT,
322 .get = cifs_xattr_get,
323 .set = cifs_xattr_set,
326 const struct xattr_handler *cifs_xattr_handlers[] = {
327 &cifs_user_xattr_handler,
328 &cifs_os2_xattr_handler,
329 &cifs_cifs_acl_xattr_handler,
330 &cifs_posix_acl_access_xattr_handler,
331 &cifs_posix_acl_default_xattr_handler,