blob: c4237b8ad9c6a6ee9c0c6e161afef1b35b25c328 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/cifs/xattr.c
3 *
Steve French79a58d12007-07-06 22:44:50 +00004 * Copyright (c) International Business Machines Corp., 2003, 2007
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * 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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Mimi Zoharf995e742011-08-11 16:00:47 -040025#include <linux/xattr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "cifsfs.h"
27#include "cifspdu.h"
28#include "cifsglob.h"
29#include "cifsproto.h"
30#include "cifs_debug.h"
Steve French2baa2682014-09-27 02:19:01 -050031#include "cifs_fs_sb.h"
32#include "cifs_unicode.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34#define MAX_EA_VALUE_SIZE 65535
Shirish Pargaonkarfbeba8b2010-11-27 11:37:54 -060035#define CIFS_XATTR_CIFS_ACL "system.cifs_acl"
Steve Frencha958fff2016-09-19 11:14:06 -050036#define CIFS_XATTR_ATTRIB "cifs.dosattrib" /* full name: user.cifs.dosattrib */
Mimi Zoharf995e742011-08-11 16:00:47 -040037
Linus Torvalds1da177e2005-04-16 15:20:36 -070038/* BB need to add server (Samba e.g) support for security and trusted prefix */
Steve French50c2f752007-07-13 00:33:32 +000039
Andreas Gruenbachera9ae0082016-04-22 12:11:38 +020040enum { XATTR_USER, XATTR_CIFS_ACL, XATTR_ACL_ACCESS, XATTR_ACL_DEFAULT };
41
42static int cifs_xattr_set(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -040043 struct dentry *dentry, struct inode *inode,
44 const char *name, const void *value,
45 size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046{
47 int rc = -EOPNOTSUPP;
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +040048 unsigned int xid;
Andreas Gruenbachera9ae0082016-04-22 12:11:38 +020049 struct super_block *sb = dentry->d_sb;
Al Viro5fdccfe2016-04-10 17:07:33 -040050 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
Jeff Layton7ffec372010-09-29 19:51:11 -040051 struct tcon_link *tlink;
Steve French96daf2b2011-05-27 04:34:02 +000052 struct cifs_tcon *pTcon;
Steve French79a58d12007-07-06 22:44:50 +000053 char *full_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Jeff Layton7ffec372010-09-29 19:51:11 -040055 tlink = cifs_sb_tlink(cifs_sb);
56 if (IS_ERR(tlink))
57 return PTR_ERR(tlink);
58 pTcon = tlink_tcon(tlink);
59
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +040060 xid = get_xid();
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Andreas Gruenbachera9ae0082016-04-22 12:11:38 +020062 full_path = build_path_from_dentry(dentry);
Steve French79a58d12007-07-06 22:44:50 +000063 if (full_path == NULL) {
Suresh Jayaraman0f3bc092009-06-25 18:12:34 +053064 rc = -ENOMEM;
Andreas Gruenbachera9ae0082016-04-22 12:11:38 +020065 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 }
67 /* return dos attributes as pseudo xattr */
68 /* return alt name if available as pseudo attr */
69
70 /* if proc/fs/cifs/streamstoxattr is set then
Steve French79a58d12007-07-06 22:44:50 +000071 search server for EAs or streams to
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 returns as xattrs */
Andreas Gruenbachera9ae0082016-04-22 12:11:38 +020073 if (size > MAX_EA_VALUE_SIZE) {
Joe Perchesf96637b2013-05-04 22:12:25 -050074 cifs_dbg(FYI, "size of EA value too large\n");
Jeff Layton7ffec372010-09-29 19:51:11 -040075 rc = -EOPNOTSUPP;
Andreas Gruenbachera9ae0082016-04-22 12:11:38 +020076 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 }
78
Andreas Gruenbachera9ae0082016-04-22 12:11:38 +020079 switch (handler->flags) {
80 case XATTR_USER:
Steve French79a58d12007-07-06 22:44:50 +000081 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
Andreas Gruenbachera9ae0082016-04-22 12:11:38 +020082 goto out;
Steve Frenchad7a2922008-02-07 23:25:02 +000083
Steve French666753c2014-01-26 23:53:43 -060084 if (pTcon->ses->server->ops->set_EA)
85 rc = pTcon->ses->server->ops->set_EA(xid, pTcon,
Andreas Gruenbachera9ae0082016-04-22 12:11:38 +020086 full_path, name, value, (__u16)size,
Steve French2baa2682014-09-27 02:19:01 -050087 cifs_sb->local_nls, cifs_remap(cifs_sb));
Andreas Gruenbachera9ae0082016-04-22 12:11:38 +020088 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Andreas Gruenbachera9ae0082016-04-22 12:11:38 +020090 case XATTR_CIFS_ACL: {
Santosh Nayakb0f8ef22012-03-02 11:47:26 +053091#ifdef CONFIG_CIFS_ACL
92 struct cifs_ntsd *pacl;
Andreas Gruenbachera9ae0082016-04-22 12:11:38 +020093
94 if (!value)
95 goto out;
96 pacl = kmalloc(size, GFP_KERNEL);
Steve Frenchb73b9a42011-04-19 18:27:10 +000097 if (!pacl) {
Steve Frenchb73b9a42011-04-19 18:27:10 +000098 rc = -ENOMEM;
99 } else {
Andreas Gruenbachera9ae0082016-04-22 12:11:38 +0200100 memcpy(pacl, value, size);
101 if (value &&
102 pTcon->ses->server->ops->set_acl)
Steve French83e3bc22014-02-02 23:31:47 -0600103 rc = pTcon->ses->server->ops->set_acl(pacl,
Al Viro59301222016-05-27 10:19:30 -0400104 size, inode,
Steve French83e3bc22014-02-02 23:31:47 -0600105 full_path, CIFS_ACL_DACL);
106 else
107 rc = -EOPNOTSUPP;
Steve Frenchb73b9a42011-04-19 18:27:10 +0000108 if (rc == 0) /* force revalidate of the inode */
Al Viro59301222016-05-27 10:19:30 -0400109 CIFS_I(inode)->time = 0;
Steve Frenchb73b9a42011-04-19 18:27:10 +0000110 kfree(pacl);
Santosh Nayakb0f8ef22012-03-02 11:47:26 +0530111 }
Steve Frenchb73b9a42011-04-19 18:27:10 +0000112#endif /* CONFIG_CIFS_ACL */
Andreas Gruenbachera9ae0082016-04-22 12:11:38 +0200113 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 }
115
Andreas Gruenbachera9ae0082016-04-22 12:11:38 +0200116 case XATTR_ACL_ACCESS:
117#ifdef CONFIG_CIFS_POSIX
118 if (!value)
119 goto out;
120 if (sb->s_flags & MS_POSIXACL)
121 rc = CIFSSMBSetPosixACL(xid, pTcon, full_path,
122 value, (const int)size,
123 ACL_TYPE_ACCESS, cifs_sb->local_nls,
124 cifs_remap(cifs_sb));
125#endif /* CONFIG_CIFS_POSIX */
126 break;
127
128 case XATTR_ACL_DEFAULT:
129#ifdef CONFIG_CIFS_POSIX
130 if (!value)
131 goto out;
132 if (sb->s_flags & MS_POSIXACL)
133 rc = CIFSSMBSetPosixACL(xid, pTcon, full_path,
134 value, (const int)size,
135 ACL_TYPE_DEFAULT, cifs_sb->local_nls,
136 cifs_remap(cifs_sb));
137#endif /* CONFIG_CIFS_POSIX */
138 break;
139 }
140
141out:
Jesper Juhlf99d49a2005-11-07 01:01:34 -0800142 kfree(full_path);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400143 free_xid(xid);
Jeff Layton7ffec372010-09-29 19:51:11 -0400144 cifs_put_tlink(tlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 return rc;
146}
147
Steve Frencha958fff2016-09-19 11:14:06 -0500148static int cifs_attrib_get(struct dentry *dentry,
149 struct inode *inode, void *value,
150 size_t size)
151{
152 ssize_t rc;
153 __u32 *pattribute;
154
155 rc = cifs_revalidate_dentry_attr(dentry);
156
157 if (rc)
158 return rc;
159
160 if ((value == NULL) || (size == 0))
161 return sizeof(__u32);
162 else if (size < sizeof(__u32))
163 return -ERANGE;
164
165 /* return dos attributes as pseudo xattr */
166 pattribute = (__u32 *)value;
167 *pattribute = CIFS_I(inode)->cifsAttrs;
168
169 return sizeof(__u32);
170}
171
172
Andreas Gruenbachera9ae0082016-04-22 12:11:38 +0200173static int cifs_xattr_get(const struct xattr_handler *handler,
174 struct dentry *dentry, struct inode *inode,
175 const char *name, void *value, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
177 ssize_t rc = -EOPNOTSUPP;
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400178 unsigned int xid;
Andreas Gruenbachera9ae0082016-04-22 12:11:38 +0200179 struct super_block *sb = dentry->d_sb;
Al Viro5fdccfe2016-04-10 17:07:33 -0400180 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
Jeff Layton7ffec372010-09-29 19:51:11 -0400181 struct tcon_link *tlink;
Steve French96daf2b2011-05-27 04:34:02 +0000182 struct cifs_tcon *pTcon;
Steve French79a58d12007-07-06 22:44:50 +0000183 char *full_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Jeff Layton7ffec372010-09-29 19:51:11 -0400185 tlink = cifs_sb_tlink(cifs_sb);
186 if (IS_ERR(tlink))
187 return PTR_ERR(tlink);
188 pTcon = tlink_tcon(tlink);
189
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400190 xid = get_xid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Andreas Gruenbachera9ae0082016-04-22 12:11:38 +0200192 full_path = build_path_from_dentry(dentry);
Steve French79a58d12007-07-06 22:44:50 +0000193 if (full_path == NULL) {
Suresh Jayaraman0f3bc092009-06-25 18:12:34 +0530194 rc = -ENOMEM;
Andreas Gruenbachera9ae0082016-04-22 12:11:38 +0200195 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 }
Steve Frencha958fff2016-09-19 11:14:06 -0500197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 /* return alt name if available as pseudo attr */
Andreas Gruenbachera9ae0082016-04-22 12:11:38 +0200199 switch (handler->flags) {
200 case XATTR_USER:
Steve Frencha958fff2016-09-19 11:14:06 -0500201 cifs_dbg(FYI, "%s:querying user xattr %s\n", __func__, name);
202 if (strcmp(name, CIFS_XATTR_ATTRIB) == 0) {
203 rc = cifs_attrib_get(dentry, inode, value, size);
204 break;
205 }
206
Steve French79a58d12007-07-06 22:44:50 +0000207 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
Andreas Gruenbachera9ae0082016-04-22 12:11:38 +0200208 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Steve French666753c2014-01-26 23:53:43 -0600210 if (pTcon->ses->server->ops->query_all_EAs)
211 rc = pTcon->ses->server->ops->query_all_EAs(xid, pTcon,
Andreas Gruenbachera9ae0082016-04-22 12:11:38 +0200212 full_path, name, value, size,
Steve French2baa2682014-09-27 02:19:01 -0500213 cifs_sb->local_nls, cifs_remap(cifs_sb));
Andreas Gruenbachera9ae0082016-04-22 12:11:38 +0200214 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Andreas Gruenbachera9ae0082016-04-22 12:11:38 +0200216 case XATTR_CIFS_ACL: {
Shirish Pargaonkarfbeba8b2010-11-27 11:37:54 -0600217#ifdef CONFIG_CIFS_ACL
Andreas Gruenbachera9ae0082016-04-22 12:11:38 +0200218 u32 acllen;
219 struct cifs_ntsd *pacl;
Shirish Pargaonkarfbeba8b2010-11-27 11:37:54 -0600220
Andreas Gruenbachera9ae0082016-04-22 12:11:38 +0200221 if (pTcon->ses->server->ops->get_acl == NULL)
222 goto out; /* rc already EOPNOTSUPP */
Steve French83e3bc22014-02-02 23:31:47 -0600223
Andreas Gruenbachera9ae0082016-04-22 12:11:38 +0200224 pacl = pTcon->ses->server->ops->get_acl(cifs_sb,
225 inode, full_path, &acllen);
226 if (IS_ERR(pacl)) {
227 rc = PTR_ERR(pacl);
228 cifs_dbg(VFS, "%s: error %zd getting sec desc\n",
229 __func__, rc);
230 } else {
231 if (value) {
232 if (acllen > size)
233 acllen = -ERANGE;
234 else
235 memcpy(value, pacl, acllen);
Shirish Pargaonkarfbeba8b2010-11-27 11:37:54 -0600236 }
Andreas Gruenbachera9ae0082016-04-22 12:11:38 +0200237 rc = acllen;
238 kfree(pacl);
239 }
240#endif /* CONFIG_CIFS_ACL */
241 break;
242 }
243
244 case XATTR_ACL_ACCESS:
245#ifdef CONFIG_CIFS_POSIX
246 if (sb->s_flags & MS_POSIXACL)
247 rc = CIFSSMBGetPosixACL(xid, pTcon, full_path,
248 value, size, ACL_TYPE_ACCESS,
249 cifs_sb->local_nls,
250 cifs_remap(cifs_sb));
251#endif /* CONFIG_CIFS_POSIX */
252 break;
253
254 case XATTR_ACL_DEFAULT:
255#ifdef CONFIG_CIFS_POSIX
256 if (sb->s_flags & MS_POSIXACL)
257 rc = CIFSSMBGetPosixACL(xid, pTcon, full_path,
258 value, size, ACL_TYPE_DEFAULT,
259 cifs_sb->local_nls,
260 cifs_remap(cifs_sb));
261#endif /* CONFIG_CIFS_POSIX */
262 break;
263 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Steve French79a58d12007-07-06 22:44:50 +0000265 /* We could add an additional check for streams ie
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 if proc/fs/cifs/streamstoxattr is set then
Steve French79a58d12007-07-06 22:44:50 +0000267 search server for EAs or streams to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 returns as xattrs */
269
Steve French79a58d12007-07-06 22:44:50 +0000270 if (rc == -EINVAL)
271 rc = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Andreas Gruenbachera9ae0082016-04-22 12:11:38 +0200273out:
Jesper Juhlf99d49a2005-11-07 01:01:34 -0800274 kfree(full_path);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400275 free_xid(xid);
Jeff Layton7ffec372010-09-29 19:51:11 -0400276 cifs_put_tlink(tlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 return rc;
278}
279
Steve French79a58d12007-07-06 22:44:50 +0000280ssize_t cifs_listxattr(struct dentry *direntry, char *data, size_t buf_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281{
282 ssize_t rc = -EOPNOTSUPP;
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400283 unsigned int xid;
Al Viro5fdccfe2016-04-10 17:07:33 -0400284 struct cifs_sb_info *cifs_sb = CIFS_SB(direntry->d_sb);
Jeff Layton7ffec372010-09-29 19:51:11 -0400285 struct tcon_link *tlink;
Steve French96daf2b2011-05-27 04:34:02 +0000286 struct cifs_tcon *pTcon;
Steve French79a58d12007-07-06 22:44:50 +0000287 char *full_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Steve French79a58d12007-07-06 22:44:50 +0000289 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
Steve Frenchea4c07d2006-08-16 19:44:25 +0000290 return -EOPNOTSUPP;
291
Jeff Layton7ffec372010-09-29 19:51:11 -0400292 tlink = cifs_sb_tlink(cifs_sb);
293 if (IS_ERR(tlink))
294 return PTR_ERR(tlink);
295 pTcon = tlink_tcon(tlink);
296
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400297 xid = get_xid();
Steve Frenchea4c07d2006-08-16 19:44:25 +0000298
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 full_path = build_path_from_dentry(direntry);
Steve French79a58d12007-07-06 22:44:50 +0000300 if (full_path == NULL) {
Suresh Jayaraman0f3bc092009-06-25 18:12:34 +0530301 rc = -ENOMEM;
Jeff Layton7ffec372010-09-29 19:51:11 -0400302 goto list_ea_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 }
304 /* return dos attributes as pseudo xattr */
305 /* return alt name if available as pseudo attr */
306
307 /* if proc/fs/cifs/streamstoxattr is set then
Steve French79a58d12007-07-06 22:44:50 +0000308 search server for EAs or streams to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 returns as xattrs */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Steve French666753c2014-01-26 23:53:43 -0600311 if (pTcon->ses->server->ops->query_all_EAs)
312 rc = pTcon->ses->server->ops->query_all_EAs(xid, pTcon,
313 full_path, NULL, data, buf_size,
Steve French2baa2682014-09-27 02:19:01 -0500314 cifs_sb->local_nls, cifs_remap(cifs_sb));
Jeff Layton7ffec372010-09-29 19:51:11 -0400315list_ea_exit:
Jesper Juhlf99d49a2005-11-07 01:01:34 -0800316 kfree(full_path);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400317 free_xid(xid);
Jeff Layton7ffec372010-09-29 19:51:11 -0400318 cifs_put_tlink(tlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 return rc;
320}
Andreas Gruenbachera9ae0082016-04-22 12:11:38 +0200321
322static const struct xattr_handler cifs_user_xattr_handler = {
323 .prefix = XATTR_USER_PREFIX,
324 .flags = XATTR_USER,
325 .get = cifs_xattr_get,
326 .set = cifs_xattr_set,
327};
328
329/* os2.* attributes are treated like user.* attributes */
330static const struct xattr_handler cifs_os2_xattr_handler = {
331 .prefix = XATTR_OS2_PREFIX,
332 .flags = XATTR_USER,
333 .get = cifs_xattr_get,
334 .set = cifs_xattr_set,
335};
336
337static const struct xattr_handler cifs_cifs_acl_xattr_handler = {
338 .name = CIFS_XATTR_CIFS_ACL,
339 .flags = XATTR_CIFS_ACL,
340 .get = cifs_xattr_get,
341 .set = cifs_xattr_set,
342};
343
344static const struct xattr_handler cifs_posix_acl_access_xattr_handler = {
345 .name = XATTR_NAME_POSIX_ACL_ACCESS,
346 .flags = XATTR_ACL_ACCESS,
347 .get = cifs_xattr_get,
348 .set = cifs_xattr_set,
349};
350
351static const struct xattr_handler cifs_posix_acl_default_xattr_handler = {
352 .name = XATTR_NAME_POSIX_ACL_DEFAULT,
353 .flags = XATTR_ACL_DEFAULT,
354 .get = cifs_xattr_get,
355 .set = cifs_xattr_set,
356};
357
358const struct xattr_handler *cifs_xattr_handlers[] = {
359 &cifs_user_xattr_handler,
360 &cifs_os2_xattr_handler,
361 &cifs_cifs_acl_xattr_handler,
362 &cifs_posix_acl_access_xattr_handler,
363 &cifs_posix_acl_default_xattr_handler,
364 NULL
365};