Merge branch 'next' of git://git.kernel.org/pub/scm/linux/kernel/git/rzhang/linux
[linux-2.6-block.git] / fs / cifs / xattr.c
CommitLineData
1da177e4
LT
1/*
2 * fs/cifs/xattr.c
3 *
79a58d1f 4 * Copyright (c) International Business Machines Corp., 2003, 2007
1da177e4
LT
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>
5a0e3ad6 24#include <linux/slab.h>
f995e740 25#include <linux/xattr.h>
1da177e4
LT
26#include "cifsfs.h"
27#include "cifspdu.h"
28#include "cifsglob.h"
29#include "cifsproto.h"
30#include "cifs_debug.h"
2baa2682
SF
31#include "cifs_fs_sb.h"
32#include "cifs_unicode.h"
1da177e4
LT
33
34#define MAX_EA_VALUE_SIZE 65535
fbeba8bb 35#define CIFS_XATTR_CIFS_ACL "system.cifs_acl"
1da177e4 36
f995e740 37/* BB need to add server (Samba e.g) support for security and trusted prefix */
1da177e4 38
a9ae008f 39enum { XATTR_USER, XATTR_CIFS_ACL, XATTR_ACL_ACCESS, XATTR_ACL_DEFAULT };
7ffec372 40
a9ae008f 41static int cifs_xattr_set(const struct xattr_handler *handler,
59301226
AV
42 struct dentry *dentry, struct inode *inode,
43 const char *name, const void *value,
44 size_t size, int flags)
1da177e4
LT
45{
46 int rc = -EOPNOTSUPP;
6d5786a3 47 unsigned int xid;
a9ae008f 48 struct super_block *sb = dentry->d_sb;
5fdccfef 49 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
7ffec372 50 struct tcon_link *tlink;
96daf2b0 51 struct cifs_tcon *pTcon;
79a58d1f 52 char *full_path;
1da177e4 53
7ffec372
JL
54 tlink = cifs_sb_tlink(cifs_sb);
55 if (IS_ERR(tlink))
56 return PTR_ERR(tlink);
57 pTcon = tlink_tcon(tlink);
58
6d5786a3 59 xid = get_xid();
1da177e4 60
a9ae008f 61 full_path = build_path_from_dentry(dentry);
79a58d1f 62 if (full_path == NULL) {
0f3bc09e 63 rc = -ENOMEM;
a9ae008f 64 goto out;
1da177e4
LT
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
79a58d1f 70 search server for EAs or streams to
1da177e4 71 returns as xattrs */
a9ae008f 72 if (size > MAX_EA_VALUE_SIZE) {
f96637be 73 cifs_dbg(FYI, "size of EA value too large\n");
7ffec372 74 rc = -EOPNOTSUPP;
a9ae008f 75 goto out;
1da177e4
LT
76 }
77
a9ae008f
AG
78 switch (handler->flags) {
79 case XATTR_USER:
79a58d1f 80 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
a9ae008f 81 goto out;
ad7a2926 82
666753c3
SF
83 if (pTcon->ses->server->ops->set_EA)
84 rc = pTcon->ses->server->ops->set_EA(xid, pTcon,
a9ae008f 85 full_path, name, value, (__u16)size,
2baa2682 86 cifs_sb->local_nls, cifs_remap(cifs_sb));
a9ae008f 87 break;
1da177e4 88
a9ae008f 89 case XATTR_CIFS_ACL: {
b0f8ef20
SN
90#ifdef CONFIG_CIFS_ACL
91 struct cifs_ntsd *pacl;
a9ae008f
AG
92
93 if (!value)
94 goto out;
95 pacl = kmalloc(size, GFP_KERNEL);
b73b9a4b 96 if (!pacl) {
b73b9a4b
SF
97 rc = -ENOMEM;
98 } else {
a9ae008f
AG
99 memcpy(pacl, value, size);
100 if (value &&
101 pTcon->ses->server->ops->set_acl)
83e3bc23 102 rc = pTcon->ses->server->ops->set_acl(pacl,
59301226 103 size, inode,
83e3bc23
SF
104 full_path, CIFS_ACL_DACL);
105 else
106 rc = -EOPNOTSUPP;
b73b9a4b 107 if (rc == 0) /* force revalidate of the inode */
59301226 108 CIFS_I(inode)->time = 0;
b73b9a4b 109 kfree(pacl);
b0f8ef20 110 }
b73b9a4b 111#endif /* CONFIG_CIFS_ACL */
a9ae008f
AG
112 break;
113 }
114
115 case XATTR_ACL_ACCESS:
1da177e4 116#ifdef CONFIG_CIFS_POSIX
a9ae008f
AG
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:
1da177e4 128#ifdef CONFIG_CIFS_POSIX
a9ae008f
AG
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;
1da177e4
LT
138 }
139
a9ae008f 140out:
f99d49ad 141 kfree(full_path);
6d5786a3 142 free_xid(xid);
7ffec372 143 cifs_put_tlink(tlink);
1da177e4
LT
144 return rc;
145}
146
a9ae008f
AG
147static 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)
1da177e4
LT
150{
151 ssize_t rc = -EOPNOTSUPP;
6d5786a3 152 unsigned int xid;
a9ae008f 153 struct super_block *sb = dentry->d_sb;
5fdccfef 154 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
7ffec372 155 struct tcon_link *tlink;
96daf2b0 156 struct cifs_tcon *pTcon;
79a58d1f 157 char *full_path;
1da177e4 158
7ffec372
JL
159 tlink = cifs_sb_tlink(cifs_sb);
160 if (IS_ERR(tlink))
161 return PTR_ERR(tlink);
162 pTcon = tlink_tcon(tlink);
163
6d5786a3 164 xid = get_xid();
1da177e4 165
a9ae008f 166 full_path = build_path_from_dentry(dentry);
79a58d1f 167 if (full_path == NULL) {
0f3bc09e 168 rc = -ENOMEM;
a9ae008f 169 goto out;
1da177e4
LT
170 }
171 /* return dos attributes as pseudo xattr */
172 /* return alt name if available as pseudo attr */
a9ae008f
AG
173 switch (handler->flags) {
174 case XATTR_USER:
79a58d1f 175 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
a9ae008f 176 goto out;
1da177e4 177
666753c3
SF
178 if (pTcon->ses->server->ops->query_all_EAs)
179 rc = pTcon->ses->server->ops->query_all_EAs(xid, pTcon,
a9ae008f 180 full_path, name, value, size,
2baa2682 181 cifs_sb->local_nls, cifs_remap(cifs_sb));
a9ae008f 182 break;
1da177e4 183
a9ae008f
AG
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:
1da177e4 213#ifdef CONFIG_CIFS_POSIX
79a58d1f 214 if (sb->s_flags & MS_POSIXACL)
1da0c78b 215 rc = CIFSSMBGetPosixACL(xid, pTcon, full_path,
a9ae008f 216 value, size, ACL_TYPE_ACCESS,
737b758c 217 cifs_sb->local_nls,
2baa2682 218 cifs_remap(cifs_sb));
a9ae008f
AG
219#endif /* CONFIG_CIFS_POSIX */
220 break;
221
222 case XATTR_ACL_DEFAULT:
1da177e4 223#ifdef CONFIG_CIFS_POSIX
79a58d1f 224 if (sb->s_flags & MS_POSIXACL)
1da0c78b 225 rc = CIFSSMBGetPosixACL(xid, pTcon, full_path,
a9ae008f 226 value, size, ACL_TYPE_DEFAULT,
737b758c 227 cifs_sb->local_nls,
2baa2682 228 cifs_remap(cifs_sb));
a9ae008f
AG
229#endif /* CONFIG_CIFS_POSIX */
230 break;
231 }
1da177e4 232
79a58d1f 233 /* We could add an additional check for streams ie
1da177e4 234 if proc/fs/cifs/streamstoxattr is set then
79a58d1f 235 search server for EAs or streams to
1da177e4
LT
236 returns as xattrs */
237
79a58d1f
SF
238 if (rc == -EINVAL)
239 rc = -EOPNOTSUPP;
1da177e4 240
a9ae008f 241out:
f99d49ad 242 kfree(full_path);
6d5786a3 243 free_xid(xid);
7ffec372 244 cifs_put_tlink(tlink);
1da177e4
LT
245 return rc;
246}
247
79a58d1f 248ssize_t cifs_listxattr(struct dentry *direntry, char *data, size_t buf_size)
1da177e4
LT
249{
250 ssize_t rc = -EOPNOTSUPP;
6d5786a3 251 unsigned int xid;
5fdccfef 252 struct cifs_sb_info *cifs_sb = CIFS_SB(direntry->d_sb);
7ffec372 253 struct tcon_link *tlink;
96daf2b0 254 struct cifs_tcon *pTcon;
79a58d1f 255 char *full_path;
1da177e4 256
79a58d1f 257 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
ea4c07d7
SF
258 return -EOPNOTSUPP;
259
7ffec372
JL
260 tlink = cifs_sb_tlink(cifs_sb);
261 if (IS_ERR(tlink))
262 return PTR_ERR(tlink);
263 pTcon = tlink_tcon(tlink);
264
6d5786a3 265 xid = get_xid();
ea4c07d7 266
1da177e4 267 full_path = build_path_from_dentry(direntry);
79a58d1f 268 if (full_path == NULL) {
0f3bc09e 269 rc = -ENOMEM;
7ffec372 270 goto list_ea_exit;
1da177e4
LT
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
79a58d1f 276 search server for EAs or streams to
1da177e4 277 returns as xattrs */
1da177e4 278
666753c3
SF
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,
2baa2682 282 cifs_sb->local_nls, cifs_remap(cifs_sb));
7ffec372 283list_ea_exit:
f99d49ad 284 kfree(full_path);
6d5786a3 285 free_xid(xid);
7ffec372 286 cifs_put_tlink(tlink);
1da177e4
LT
287 return rc;
288}
a9ae008f
AG
289
290static 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 */
298static 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
305static 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
312static 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
319static 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
326const 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};