159547c8a40bc073ce1fb4ccaf847662b9de0954
[cascardo/linux.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_DOS_ATTRIB "user.DosAttrib"
36 #define CIFS_XATTR_CIFS_ACL "system.cifs_acl"
37
38 /* BB need to add server (Samba e.g) support for security and trusted prefix */
39
40 int cifs_removexattr(struct dentry *direntry, const char *ea_name)
41 {
42         int rc = -EOPNOTSUPP;
43 #ifdef CONFIG_CIFS_XATTR
44         unsigned int xid;
45         struct cifs_sb_info *cifs_sb = CIFS_SB(direntry->d_sb);
46         struct tcon_link *tlink;
47         struct cifs_tcon *pTcon;
48         char *full_path = NULL;
49
50         tlink = cifs_sb_tlink(cifs_sb);
51         if (IS_ERR(tlink))
52                 return PTR_ERR(tlink);
53         pTcon = tlink_tcon(tlink);
54
55         xid = get_xid();
56
57         full_path = build_path_from_dentry(direntry);
58         if (full_path == NULL) {
59                 rc = -ENOMEM;
60                 goto remove_ea_exit;
61         }
62         if (ea_name == NULL) {
63                 cifs_dbg(FYI, "Null xattr names not supported\n");
64         } else if (strncmp(ea_name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN)
65                 && (strncmp(ea_name, XATTR_OS2_PREFIX, XATTR_OS2_PREFIX_LEN))) {
66                 cifs_dbg(FYI,
67                          "illegal xattr request %s (only user namespace supported)\n",
68                          ea_name);
69                 /* BB what if no namespace prefix? */
70                 /* Should we just pass them to server, except for
71                 system and perhaps security prefixes? */
72         } else {
73                 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
74                         goto remove_ea_exit;
75
76                 ea_name += XATTR_USER_PREFIX_LEN; /* skip past user. prefix */
77                 if (pTcon->ses->server->ops->set_EA)
78                         rc = pTcon->ses->server->ops->set_EA(xid, pTcon,
79                                 full_path, ea_name, NULL, (__u16)0,
80                                 cifs_sb->local_nls, cifs_remap(cifs_sb));
81         }
82 remove_ea_exit:
83         kfree(full_path);
84         free_xid(xid);
85         cifs_put_tlink(tlink);
86 #endif
87         return rc;
88 }
89
90 int cifs_setxattr(struct dentry *direntry, const char *ea_name,
91                   const void *ea_value, size_t value_size, int flags)
92 {
93         int rc = -EOPNOTSUPP;
94 #ifdef CONFIG_CIFS_XATTR
95         unsigned int xid;
96         struct super_block *sb = direntry->d_sb;
97         struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
98         struct tcon_link *tlink;
99         struct cifs_tcon *pTcon;
100         char *full_path;
101
102         tlink = cifs_sb_tlink(cifs_sb);
103         if (IS_ERR(tlink))
104                 return PTR_ERR(tlink);
105         pTcon = tlink_tcon(tlink);
106
107         xid = get_xid();
108
109         full_path = build_path_from_dentry(direntry);
110         if (full_path == NULL) {
111                 rc = -ENOMEM;
112                 goto set_ea_exit;
113         }
114         /* return dos attributes as pseudo xattr */
115         /* return alt name if available as pseudo attr */
116
117         /* if proc/fs/cifs/streamstoxattr is set then
118                 search server for EAs or streams to
119                 returns as xattrs */
120         if (value_size > MAX_EA_VALUE_SIZE) {
121                 cifs_dbg(FYI, "size of EA value too large\n");
122                 rc = -EOPNOTSUPP;
123                 goto set_ea_exit;
124         }
125
126         if (ea_name == NULL) {
127                 cifs_dbg(FYI, "Null xattr names not supported\n");
128         } else if (strncmp(ea_name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN)
129                    == 0) {
130                 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
131                         goto set_ea_exit;
132                 if (strncmp(ea_name, CIFS_XATTR_DOS_ATTRIB, 14) == 0)
133                         cifs_dbg(FYI, "attempt to set cifs inode metadata\n");
134
135                 ea_name += XATTR_USER_PREFIX_LEN; /* skip past user. prefix */
136                 if (pTcon->ses->server->ops->set_EA)
137                         rc = pTcon->ses->server->ops->set_EA(xid, pTcon,
138                                 full_path, ea_name, ea_value, (__u16)value_size,
139                                 cifs_sb->local_nls, cifs_remap(cifs_sb));
140         } else if (strncmp(ea_name, XATTR_OS2_PREFIX, XATTR_OS2_PREFIX_LEN)
141                    == 0) {
142                 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
143                         goto set_ea_exit;
144
145                 ea_name += XATTR_OS2_PREFIX_LEN; /* skip past os2. prefix */
146                 if (pTcon->ses->server->ops->set_EA)
147                         rc = pTcon->ses->server->ops->set_EA(xid, pTcon,
148                                 full_path, ea_name, ea_value, (__u16)value_size,
149                                 cifs_sb->local_nls, cifs_remap(cifs_sb));
150         } else if (strncmp(ea_name, CIFS_XATTR_CIFS_ACL,
151                         strlen(CIFS_XATTR_CIFS_ACL)) == 0) {
152 #ifdef CONFIG_CIFS_ACL
153                 struct cifs_ntsd *pacl;
154                 pacl = kmalloc(value_size, GFP_KERNEL);
155                 if (!pacl) {
156                         rc = -ENOMEM;
157                 } else {
158                         memcpy(pacl, ea_value, value_size);
159                         if (pTcon->ses->server->ops->set_acl)
160                                 rc = pTcon->ses->server->ops->set_acl(pacl,
161                                                 value_size, d_inode(direntry),
162                                                 full_path, CIFS_ACL_DACL);
163                         else
164                                 rc = -EOPNOTSUPP;
165                         if (rc == 0) /* force revalidate of the inode */
166                                 CIFS_I(d_inode(direntry))->time = 0;
167                         kfree(pacl);
168                 }
169 #else
170                 cifs_dbg(FYI, "Set CIFS ACL not supported yet\n");
171 #endif /* CONFIG_CIFS_ACL */
172         } else {
173                 int temp;
174                 temp = strncmp(ea_name, XATTR_NAME_POSIX_ACL_ACCESS,
175                         strlen(XATTR_NAME_POSIX_ACL_ACCESS));
176                 if (temp == 0) {
177 #ifdef CONFIG_CIFS_POSIX
178                         if (sb->s_flags & MS_POSIXACL)
179                                 rc = CIFSSMBSetPosixACL(xid, pTcon, full_path,
180                                         ea_value, (const int)value_size,
181                                         ACL_TYPE_ACCESS, cifs_sb->local_nls,
182                                         cifs_remap(cifs_sb));
183                         cifs_dbg(FYI, "set POSIX ACL rc %d\n", rc);
184 #else
185                         cifs_dbg(FYI, "set POSIX ACL not supported\n");
186 #endif
187                 } else if (strncmp(ea_name, XATTR_NAME_POSIX_ACL_DEFAULT,
188                                    strlen(XATTR_NAME_POSIX_ACL_DEFAULT)) == 0) {
189 #ifdef CONFIG_CIFS_POSIX
190                         if (sb->s_flags & MS_POSIXACL)
191                                 rc = CIFSSMBSetPosixACL(xid, pTcon, full_path,
192                                         ea_value, (const int)value_size,
193                                         ACL_TYPE_DEFAULT, cifs_sb->local_nls,
194                                         cifs_remap(cifs_sb));
195                         cifs_dbg(FYI, "set POSIX default ACL rc %d\n", rc);
196 #else
197                         cifs_dbg(FYI, "set default POSIX ACL not supported\n");
198 #endif
199                 } else {
200                         cifs_dbg(FYI, "illegal xattr request %s (only user namespace supported)\n",
201                                  ea_name);
202                   /* BB what if no namespace prefix? */
203                   /* Should we just pass them to server, except for
204                   system and perhaps security prefixes? */
205                 }
206         }
207
208 set_ea_exit:
209         kfree(full_path);
210         free_xid(xid);
211         cifs_put_tlink(tlink);
212 #endif
213         return rc;
214 }
215
216 ssize_t cifs_getxattr(struct dentry *direntry, const char *ea_name,
217         void *ea_value, size_t buf_size)
218 {
219         ssize_t rc = -EOPNOTSUPP;
220 #ifdef CONFIG_CIFS_XATTR
221         unsigned int xid;
222         struct super_block *sb = direntry->d_sb;
223         struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
224         struct tcon_link *tlink;
225         struct cifs_tcon *pTcon;
226         char *full_path;
227
228         tlink = cifs_sb_tlink(cifs_sb);
229         if (IS_ERR(tlink))
230                 return PTR_ERR(tlink);
231         pTcon = tlink_tcon(tlink);
232
233         xid = get_xid();
234
235         full_path = build_path_from_dentry(direntry);
236         if (full_path == NULL) {
237                 rc = -ENOMEM;
238                 goto get_ea_exit;
239         }
240         /* return dos attributes as pseudo xattr */
241         /* return alt name if available as pseudo attr */
242         if (ea_name == NULL) {
243                 cifs_dbg(FYI, "Null xattr names not supported\n");
244         } else if (strncmp(ea_name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN)
245                    == 0) {
246                 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
247                         goto get_ea_exit;
248
249                 if (strncmp(ea_name, CIFS_XATTR_DOS_ATTRIB, 14) == 0) {
250                         cifs_dbg(FYI, "attempt to query cifs inode metadata\n");
251                         /* revalidate/getattr then populate from inode */
252                 } /* BB add else when above is implemented */
253                 ea_name += XATTR_USER_PREFIX_LEN; /* skip past user. prefix */
254                 if (pTcon->ses->server->ops->query_all_EAs)
255                         rc = pTcon->ses->server->ops->query_all_EAs(xid, pTcon,
256                                 full_path, ea_name, ea_value, buf_size,
257                                 cifs_sb->local_nls, cifs_remap(cifs_sb));
258         } else if (strncmp(ea_name, XATTR_OS2_PREFIX, XATTR_OS2_PREFIX_LEN) == 0) {
259                 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
260                         goto get_ea_exit;
261
262                 ea_name += XATTR_OS2_PREFIX_LEN; /* skip past os2. prefix */
263                 if (pTcon->ses->server->ops->query_all_EAs)
264                         rc = pTcon->ses->server->ops->query_all_EAs(xid, pTcon,
265                                 full_path, ea_name, ea_value, buf_size,
266                                 cifs_sb->local_nls, cifs_remap(cifs_sb));
267         } else if (strncmp(ea_name, XATTR_NAME_POSIX_ACL_ACCESS,
268                           strlen(XATTR_NAME_POSIX_ACL_ACCESS)) == 0) {
269 #ifdef CONFIG_CIFS_POSIX
270                 if (sb->s_flags & MS_POSIXACL)
271                         rc = CIFSSMBGetPosixACL(xid, pTcon, full_path,
272                                 ea_value, buf_size, ACL_TYPE_ACCESS,
273                                 cifs_sb->local_nls,
274                                 cifs_remap(cifs_sb));
275 #else
276                 cifs_dbg(FYI, "Query POSIX ACL not supported yet\n");
277 #endif /* CONFIG_CIFS_POSIX */
278         } else if (strncmp(ea_name, XATTR_NAME_POSIX_ACL_DEFAULT,
279                           strlen(XATTR_NAME_POSIX_ACL_DEFAULT)) == 0) {
280 #ifdef CONFIG_CIFS_POSIX
281                 if (sb->s_flags & MS_POSIXACL)
282                         rc = CIFSSMBGetPosixACL(xid, pTcon, full_path,
283                                 ea_value, buf_size, ACL_TYPE_DEFAULT,
284                                 cifs_sb->local_nls,
285                                 cifs_remap(cifs_sb));
286 #else
287                 cifs_dbg(FYI, "Query POSIX default ACL not supported yet\n");
288 #endif /* CONFIG_CIFS_POSIX */
289         } else if (strncmp(ea_name, CIFS_XATTR_CIFS_ACL,
290                                 strlen(CIFS_XATTR_CIFS_ACL)) == 0) {
291 #ifdef CONFIG_CIFS_ACL
292                         u32 acllen;
293                         struct cifs_ntsd *pacl;
294
295                         if (pTcon->ses->server->ops->get_acl == NULL)
296                                 goto get_ea_exit; /* rc already EOPNOTSUPP */
297
298                         pacl = pTcon->ses->server->ops->get_acl(cifs_sb,
299                                         d_inode(direntry), full_path, &acllen);
300                         if (IS_ERR(pacl)) {
301                                 rc = PTR_ERR(pacl);
302                                 cifs_dbg(VFS, "%s: error %zd getting sec desc\n",
303                                          __func__, rc);
304                         } else {
305                                 if (ea_value) {
306                                         if (acllen > buf_size)
307                                                 acllen = -ERANGE;
308                                         else
309                                                 memcpy(ea_value, pacl, acllen);
310                                 }
311                                 rc = acllen;
312                                 kfree(pacl);
313                         }
314 #else
315                         cifs_dbg(FYI, "Query CIFS ACL not supported yet\n");
316 #endif /* CONFIG_CIFS_ACL */
317         } else if (strncmp(ea_name,
318                   XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) == 0) {
319                 cifs_dbg(FYI, "Trusted xattr namespace not supported yet\n");
320         } else if (strncmp(ea_name,
321                   XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN) == 0) {
322                 cifs_dbg(FYI, "Security xattr namespace not supported yet\n");
323         } else
324                 cifs_dbg(FYI,
325                          "illegal xattr request %s (only user namespace supported)\n",
326                          ea_name);
327
328         /* We could add an additional check for streams ie
329             if proc/fs/cifs/streamstoxattr is set then
330                 search server for EAs or streams to
331                 returns as xattrs */
332
333         if (rc == -EINVAL)
334                 rc = -EOPNOTSUPP;
335
336 get_ea_exit:
337         kfree(full_path);
338         free_xid(xid);
339         cifs_put_tlink(tlink);
340 #endif
341         return rc;
342 }
343
344 ssize_t cifs_listxattr(struct dentry *direntry, char *data, size_t buf_size)
345 {
346         ssize_t rc = -EOPNOTSUPP;
347 #ifdef CONFIG_CIFS_XATTR
348         unsigned int xid;
349         struct cifs_sb_info *cifs_sb = CIFS_SB(direntry->d_sb);
350         struct tcon_link *tlink;
351         struct cifs_tcon *pTcon;
352         char *full_path;
353
354         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
355                 return -EOPNOTSUPP;
356
357         tlink = cifs_sb_tlink(cifs_sb);
358         if (IS_ERR(tlink))
359                 return PTR_ERR(tlink);
360         pTcon = tlink_tcon(tlink);
361
362         xid = get_xid();
363
364         full_path = build_path_from_dentry(direntry);
365         if (full_path == NULL) {
366                 rc = -ENOMEM;
367                 goto list_ea_exit;
368         }
369         /* return dos attributes as pseudo xattr */
370         /* return alt name if available as pseudo attr */
371
372         /* if proc/fs/cifs/streamstoxattr is set then
373                 search server for EAs or streams to
374                 returns as xattrs */
375
376         if (pTcon->ses->server->ops->query_all_EAs)
377                 rc = pTcon->ses->server->ops->query_all_EAs(xid, pTcon,
378                                 full_path, NULL, data, buf_size,
379                                 cifs_sb->local_nls, cifs_remap(cifs_sb));
380 list_ea_exit:
381         kfree(full_path);
382         free_xid(xid);
383         cifs_put_tlink(tlink);
384 #endif
385         return rc;
386 }