]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - fs/cifs/xattr.c
Merge tag 'topic/drm-misc-2016-06-01' of git://anongit.freedesktop.org/drm-intel...
[mirror_ubuntu-zesty-kernel.git] / fs / cifs / xattr.c
1 /*
2 * fs/cifs/xattr.c
3 *
4 * Copyright (c) International Business Machines Corp., 2003, 2007
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>
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"
33
34 #define MAX_EA_VALUE_SIZE 65535
35 #define CIFS_XATTR_CIFS_ACL "system.cifs_acl"
36
37 /* BB need to add server (Samba e.g) support for security and trusted prefix */
38
39 enum { XATTR_USER, XATTR_CIFS_ACL, XATTR_ACL_ACCESS, XATTR_ACL_DEFAULT };
40
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)
45 {
46 int rc = -EOPNOTSUPP;
47 unsigned int xid;
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;
52 char *full_path;
53
54 tlink = cifs_sb_tlink(cifs_sb);
55 if (IS_ERR(tlink))
56 return PTR_ERR(tlink);
57 pTcon = tlink_tcon(tlink);
58
59 xid = get_xid();
60
61 full_path = build_path_from_dentry(dentry);
62 if (full_path == NULL) {
63 rc = -ENOMEM;
64 goto out;
65 }
66 /* return dos attributes as pseudo xattr */
67 /* return alt name if available as pseudo attr */
68
69 /* if proc/fs/cifs/streamstoxattr is set then
70 search server for EAs or streams to
71 returns as xattrs */
72 if (size > MAX_EA_VALUE_SIZE) {
73 cifs_dbg(FYI, "size of EA value too large\n");
74 rc = -EOPNOTSUPP;
75 goto out;
76 }
77
78 switch (handler->flags) {
79 case XATTR_USER:
80 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
81 goto out;
82
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));
87 break;
88
89 case XATTR_CIFS_ACL: {
90 #ifdef CONFIG_CIFS_ACL
91 struct cifs_ntsd *pacl;
92
93 if (!value)
94 goto out;
95 pacl = kmalloc(size, GFP_KERNEL);
96 if (!pacl) {
97 rc = -ENOMEM;
98 } else {
99 memcpy(pacl, value, size);
100 if (value &&
101 pTcon->ses->server->ops->set_acl)
102 rc = pTcon->ses->server->ops->set_acl(pacl,
103 size, inode,
104 full_path, CIFS_ACL_DACL);
105 else
106 rc = -EOPNOTSUPP;
107 if (rc == 0) /* force revalidate of the inode */
108 CIFS_I(inode)->time = 0;
109 kfree(pacl);
110 }
111 #endif /* CONFIG_CIFS_ACL */
112 break;
113 }
114
115 case XATTR_ACL_ACCESS:
116 #ifdef CONFIG_CIFS_POSIX
117 if (!value)
118 goto out;
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 */
125 break;
126
127 case XATTR_ACL_DEFAULT:
128 #ifdef CONFIG_CIFS_POSIX
129 if (!value)
130 goto out;
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 */
137 break;
138 }
139
140 out:
141 kfree(full_path);
142 free_xid(xid);
143 cifs_put_tlink(tlink);
144 return rc;
145 }
146
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)
150 {
151 ssize_t rc = -EOPNOTSUPP;
152 unsigned int xid;
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;
157 char *full_path;
158
159 tlink = cifs_sb_tlink(cifs_sb);
160 if (IS_ERR(tlink))
161 return PTR_ERR(tlink);
162 pTcon = tlink_tcon(tlink);
163
164 xid = get_xid();
165
166 full_path = build_path_from_dentry(dentry);
167 if (full_path == NULL) {
168 rc = -ENOMEM;
169 goto out;
170 }
171 /* return dos attributes as pseudo xattr */
172 /* return alt name if available as pseudo attr */
173 switch (handler->flags) {
174 case XATTR_USER:
175 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
176 goto out;
177
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));
182 break;
183
184 case XATTR_CIFS_ACL: {
185 #ifdef CONFIG_CIFS_ACL
186 u32 acllen;
187 struct cifs_ntsd *pacl;
188
189 if (pTcon->ses->server->ops->get_acl == NULL)
190 goto out; /* rc already EOPNOTSUPP */
191
192 pacl = pTcon->ses->server->ops->get_acl(cifs_sb,
193 inode, full_path, &acllen);
194 if (IS_ERR(pacl)) {
195 rc = PTR_ERR(pacl);
196 cifs_dbg(VFS, "%s: error %zd getting sec desc\n",
197 __func__, rc);
198 } else {
199 if (value) {
200 if (acllen > size)
201 acllen = -ERANGE;
202 else
203 memcpy(value, pacl, acllen);
204 }
205 rc = acllen;
206 kfree(pacl);
207 }
208 #endif /* CONFIG_CIFS_ACL */
209 break;
210 }
211
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,
217 cifs_sb->local_nls,
218 cifs_remap(cifs_sb));
219 #endif /* CONFIG_CIFS_POSIX */
220 break;
221
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,
227 cifs_sb->local_nls,
228 cifs_remap(cifs_sb));
229 #endif /* CONFIG_CIFS_POSIX */
230 break;
231 }
232
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
236 returns as xattrs */
237
238 if (rc == -EINVAL)
239 rc = -EOPNOTSUPP;
240
241 out:
242 kfree(full_path);
243 free_xid(xid);
244 cifs_put_tlink(tlink);
245 return rc;
246 }
247
248 ssize_t cifs_listxattr(struct dentry *direntry, char *data, size_t buf_size)
249 {
250 ssize_t rc = -EOPNOTSUPP;
251 unsigned int xid;
252 struct cifs_sb_info *cifs_sb = CIFS_SB(direntry->d_sb);
253 struct tcon_link *tlink;
254 struct cifs_tcon *pTcon;
255 char *full_path;
256
257 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
258 return -EOPNOTSUPP;
259
260 tlink = cifs_sb_tlink(cifs_sb);
261 if (IS_ERR(tlink))
262 return PTR_ERR(tlink);
263 pTcon = tlink_tcon(tlink);
264
265 xid = get_xid();
266
267 full_path = build_path_from_dentry(direntry);
268 if (full_path == NULL) {
269 rc = -ENOMEM;
270 goto list_ea_exit;
271 }
272 /* return dos attributes as pseudo xattr */
273 /* return alt name if available as pseudo attr */
274
275 /* if proc/fs/cifs/streamstoxattr is set then
276 search server for EAs or streams to
277 returns as xattrs */
278
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));
283 list_ea_exit:
284 kfree(full_path);
285 free_xid(xid);
286 cifs_put_tlink(tlink);
287 return rc;
288 }
289
290 static const struct xattr_handler cifs_user_xattr_handler = {
291 .prefix = XATTR_USER_PREFIX,
292 .flags = XATTR_USER,
293 .get = cifs_xattr_get,
294 .set = cifs_xattr_set,
295 };
296
297 /* os2.* attributes are treated like user.* attributes */
298 static const struct xattr_handler cifs_os2_xattr_handler = {
299 .prefix = XATTR_OS2_PREFIX,
300 .flags = XATTR_USER,
301 .get = cifs_xattr_get,
302 .set = cifs_xattr_set,
303 };
304
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,
310 };
311
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,
317 };
318
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,
324 };
325
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,
332 NULL
333 };