RDMA/core: Split gid_attrs related sysfs from add_port()
[linux-2.6-microblaze.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 #include "cifs_ioctl.h"
34
35 #define MAX_EA_VALUE_SIZE CIFSMaxBufSize
36 #define CIFS_XATTR_CIFS_ACL "system.cifs_acl" /* DACL only */
37 #define CIFS_XATTR_CIFS_NTSD "system.cifs_ntsd" /* owner plus DACL */
38 #define CIFS_XATTR_CIFS_NTSD_FULL "system.cifs_ntsd_full" /* owner/DACL/SACL */
39 #define CIFS_XATTR_ATTRIB "cifs.dosattrib"  /* full name: user.cifs.dosattrib */
40 #define CIFS_XATTR_CREATETIME "cifs.creationtime"  /* user.cifs.creationtime */
41 /*
42  * Although these three are just aliases for the above, need to move away from
43  * confusing users and using the 20+ year old term 'cifs' when it is no longer
44  * secure, replaced by SMB2 (then even more highly secure SMB3) many years ago
45  */
46 #define SMB3_XATTR_CIFS_ACL "system.smb3_acl" /* DACL only */
47 #define SMB3_XATTR_CIFS_NTSD "system.smb3_ntsd" /* owner plus DACL */
48 #define SMB3_XATTR_CIFS_NTSD_FULL "system.smb3_ntsd_full" /* owner/DACL/SACL */
49 #define SMB3_XATTR_ATTRIB "smb3.dosattrib"  /* full name: user.smb3.dosattrib */
50 #define SMB3_XATTR_CREATETIME "smb3.creationtime"  /* user.smb3.creationtime */
51 /* BB need to add server (Samba e.g) support for security and trusted prefix */
52
53 enum { XATTR_USER, XATTR_CIFS_ACL, XATTR_ACL_ACCESS, XATTR_ACL_DEFAULT,
54         XATTR_CIFS_NTSD, XATTR_CIFS_NTSD_FULL };
55
56 static int cifs_attrib_set(unsigned int xid, struct cifs_tcon *pTcon,
57                            struct inode *inode, const char *full_path,
58                            const void *value, size_t size)
59 {
60         ssize_t rc = -EOPNOTSUPP;
61         __u32 *pattrib = (__u32 *)value;
62         __u32 attrib;
63         FILE_BASIC_INFO info_buf;
64
65         if ((value == NULL) || (size != sizeof(__u32)))
66                 return -ERANGE;
67
68         memset(&info_buf, 0, sizeof(info_buf));
69         attrib = *pattrib;
70         info_buf.Attributes = cpu_to_le32(attrib);
71         if (pTcon->ses->server->ops->set_file_info)
72                 rc = pTcon->ses->server->ops->set_file_info(inode, full_path,
73                                 &info_buf, xid);
74         if (rc == 0)
75                 CIFS_I(inode)->cifsAttrs = attrib;
76
77         return rc;
78 }
79
80 static int cifs_creation_time_set(unsigned int xid, struct cifs_tcon *pTcon,
81                                   struct inode *inode, const char *full_path,
82                                   const void *value, size_t size)
83 {
84         ssize_t rc = -EOPNOTSUPP;
85         __u64 *pcreation_time = (__u64 *)value;
86         __u64 creation_time;
87         FILE_BASIC_INFO info_buf;
88
89         if ((value == NULL) || (size != sizeof(__u64)))
90                 return -ERANGE;
91
92         memset(&info_buf, 0, sizeof(info_buf));
93         creation_time = *pcreation_time;
94         info_buf.CreationTime = cpu_to_le64(creation_time);
95         if (pTcon->ses->server->ops->set_file_info)
96                 rc = pTcon->ses->server->ops->set_file_info(inode, full_path,
97                                 &info_buf, xid);
98         if (rc == 0)
99                 CIFS_I(inode)->createtime = creation_time;
100
101         return rc;
102 }
103
104 static int cifs_xattr_set(const struct xattr_handler *handler,
105                           struct user_namespace *mnt_userns,
106                           struct dentry *dentry, struct inode *inode,
107                           const char *name, const void *value,
108                           size_t size, int flags)
109 {
110         int rc = -EOPNOTSUPP;
111         unsigned int xid;
112         struct super_block *sb = dentry->d_sb;
113         struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
114         struct tcon_link *tlink;
115         struct cifs_tcon *pTcon;
116         const char *full_path;
117         void *page;
118
119         tlink = cifs_sb_tlink(cifs_sb);
120         if (IS_ERR(tlink))
121                 return PTR_ERR(tlink);
122         pTcon = tlink_tcon(tlink);
123
124         xid = get_xid();
125         page = alloc_dentry_path();
126
127         full_path = build_path_from_dentry(dentry, page);
128         if (IS_ERR(full_path)) {
129                 rc = PTR_ERR(full_path);
130                 goto out;
131         }
132         /* return dos attributes as pseudo xattr */
133         /* return alt name if available as pseudo attr */
134
135         /* if proc/fs/cifs/streamstoxattr is set then
136                 search server for EAs or streams to
137                 returns as xattrs */
138         if (size > MAX_EA_VALUE_SIZE) {
139                 cifs_dbg(FYI, "size of EA value too large\n");
140                 rc = -EOPNOTSUPP;
141                 goto out;
142         }
143
144         switch (handler->flags) {
145         case XATTR_USER:
146                 cifs_dbg(FYI, "%s:setting user xattr %s\n", __func__, name);
147                 if ((strcmp(name, CIFS_XATTR_ATTRIB) == 0) ||
148                     (strcmp(name, SMB3_XATTR_ATTRIB) == 0)) {
149                         rc = cifs_attrib_set(xid, pTcon, inode, full_path,
150                                         value, size);
151                         if (rc == 0) /* force revalidate of the inode */
152                                 CIFS_I(inode)->time = 0;
153                         break;
154                 } else if ((strcmp(name, CIFS_XATTR_CREATETIME) == 0) ||
155                            (strcmp(name, SMB3_XATTR_CREATETIME) == 0)) {
156                         rc = cifs_creation_time_set(xid, pTcon, inode,
157                                         full_path, value, size);
158                         if (rc == 0) /* force revalidate of the inode */
159                                 CIFS_I(inode)->time = 0;
160                         break;
161                 }
162
163                 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
164                         goto out;
165
166                 if (pTcon->ses->server->ops->set_EA)
167                         rc = pTcon->ses->server->ops->set_EA(xid, pTcon,
168                                 full_path, name, value, (__u16)size,
169                                 cifs_sb->local_nls, cifs_sb);
170                 break;
171
172         case XATTR_CIFS_ACL:
173         case XATTR_CIFS_NTSD:
174         case XATTR_CIFS_NTSD_FULL: {
175                 struct cifs_ntsd *pacl;
176
177                 if (!value)
178                         goto out;
179                 pacl = kmalloc(size, GFP_KERNEL);
180                 if (!pacl) {
181                         rc = -ENOMEM;
182                 } else {
183                         memcpy(pacl, value, size);
184                         if (pTcon->ses->server->ops->set_acl) {
185                                 int aclflags = 0;
186                                 rc = 0;
187
188                                 switch (handler->flags) {
189                                 case XATTR_CIFS_NTSD_FULL:
190                                         aclflags = (CIFS_ACL_OWNER |
191                                                     CIFS_ACL_DACL |
192                                                     CIFS_ACL_SACL);
193                                         break;
194                                 case XATTR_CIFS_NTSD:
195                                         aclflags = (CIFS_ACL_OWNER |
196                                                     CIFS_ACL_DACL);
197                                         break;
198                                 case XATTR_CIFS_ACL:
199                                 default:
200                                         aclflags = CIFS_ACL_DACL;
201                                 }
202
203                                 rc = pTcon->ses->server->ops->set_acl(pacl,
204                                         size, inode, full_path, aclflags);
205                         } else {
206                                 rc = -EOPNOTSUPP;
207                         }
208                         if (rc == 0) /* force revalidate of the inode */
209                                 CIFS_I(inode)->time = 0;
210                         kfree(pacl);
211                 }
212                 break;
213         }
214
215         case XATTR_ACL_ACCESS:
216 #ifdef CONFIG_CIFS_POSIX
217                 if (!value)
218                         goto out;
219                 if (sb->s_flags & SB_POSIXACL)
220                         rc = CIFSSMBSetPosixACL(xid, pTcon, full_path,
221                                 value, (const int)size,
222                                 ACL_TYPE_ACCESS, cifs_sb->local_nls,
223                                 cifs_remap(cifs_sb));
224 #endif  /* CONFIG_CIFS_POSIX */
225                 break;
226
227         case XATTR_ACL_DEFAULT:
228 #ifdef CONFIG_CIFS_POSIX
229                 if (!value)
230                         goto out;
231                 if (sb->s_flags & SB_POSIXACL)
232                         rc = CIFSSMBSetPosixACL(xid, pTcon, full_path,
233                                 value, (const int)size,
234                                 ACL_TYPE_DEFAULT, cifs_sb->local_nls,
235                                 cifs_remap(cifs_sb));
236 #endif  /* CONFIG_CIFS_POSIX */
237                 break;
238         }
239
240 out:
241         free_dentry_path(page);
242         free_xid(xid);
243         cifs_put_tlink(tlink);
244         return rc;
245 }
246
247 static int cifs_attrib_get(struct dentry *dentry,
248                            struct inode *inode, void *value,
249                            size_t size)
250 {
251         ssize_t rc;
252         __u32 *pattribute;
253
254         rc = cifs_revalidate_dentry_attr(dentry);
255
256         if (rc)
257                 return rc;
258
259         if ((value == NULL) || (size == 0))
260                 return sizeof(__u32);
261         else if (size < sizeof(__u32))
262                 return -ERANGE;
263
264         /* return dos attributes as pseudo xattr */
265         pattribute = (__u32 *)value;
266         *pattribute = CIFS_I(inode)->cifsAttrs;
267
268         return sizeof(__u32);
269 }
270
271 static int cifs_creation_time_get(struct dentry *dentry, struct inode *inode,
272                                   void *value, size_t size)
273 {
274         ssize_t rc;
275         __u64 *pcreatetime;
276
277         rc = cifs_revalidate_dentry_attr(dentry);
278         if (rc)
279                 return rc;
280
281         if ((value == NULL) || (size == 0))
282                 return sizeof(__u64);
283         else if (size < sizeof(__u64))
284                 return -ERANGE;
285
286         /* return dos attributes as pseudo xattr */
287         pcreatetime = (__u64 *)value;
288         *pcreatetime = CIFS_I(inode)->createtime;
289         return sizeof(__u64);
290 }
291
292
293 static int cifs_xattr_get(const struct xattr_handler *handler,
294                           struct dentry *dentry, struct inode *inode,
295                           const char *name, void *value, size_t size)
296 {
297         ssize_t rc = -EOPNOTSUPP;
298         unsigned int xid;
299         struct super_block *sb = dentry->d_sb;
300         struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
301         struct tcon_link *tlink;
302         struct cifs_tcon *pTcon;
303         const char *full_path;
304         void *page;
305
306         tlink = cifs_sb_tlink(cifs_sb);
307         if (IS_ERR(tlink))
308                 return PTR_ERR(tlink);
309         pTcon = tlink_tcon(tlink);
310
311         xid = get_xid();
312         page = alloc_dentry_path();
313
314         full_path = build_path_from_dentry(dentry, page);
315         if (IS_ERR(full_path)) {
316                 rc = PTR_ERR(full_path);
317                 goto out;
318         }
319
320         /* return alt name if available as pseudo attr */
321         switch (handler->flags) {
322         case XATTR_USER:
323                 cifs_dbg(FYI, "%s:querying user xattr %s\n", __func__, name);
324                 if ((strcmp(name, CIFS_XATTR_ATTRIB) == 0) ||
325                     (strcmp(name, SMB3_XATTR_ATTRIB) == 0)) {
326                         rc = cifs_attrib_get(dentry, inode, value, size);
327                         break;
328                 } else if ((strcmp(name, CIFS_XATTR_CREATETIME) == 0) ||
329                     (strcmp(name, SMB3_XATTR_CREATETIME) == 0)) {
330                         rc = cifs_creation_time_get(dentry, inode, value, size);
331                         break;
332                 }
333
334                 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
335                         goto out;
336
337                 if (pTcon->ses->server->ops->query_all_EAs)
338                         rc = pTcon->ses->server->ops->query_all_EAs(xid, pTcon,
339                                 full_path, name, value, size, cifs_sb);
340                 break;
341
342         case XATTR_CIFS_ACL:
343         case XATTR_CIFS_NTSD:
344         case XATTR_CIFS_NTSD_FULL: {
345                 /*
346                  * fetch owner, DACL, and SACL if asked for full descriptor,
347                  * fetch owner and DACL otherwise
348                  */
349                 u32 acllen, extra_info;
350                 struct cifs_ntsd *pacl;
351
352                 if (pTcon->ses->server->ops->get_acl == NULL)
353                         goto out; /* rc already EOPNOTSUPP */
354
355                 if (handler->flags == XATTR_CIFS_NTSD_FULL) {
356                         extra_info = SACL_SECINFO;
357                 } else {
358                         extra_info = 0;
359                 }
360                 pacl = pTcon->ses->server->ops->get_acl(cifs_sb,
361                                 inode, full_path, &acllen, extra_info);
362                 if (IS_ERR(pacl)) {
363                         rc = PTR_ERR(pacl);
364                         cifs_dbg(VFS, "%s: error %zd getting sec desc\n",
365                                  __func__, rc);
366                 } else {
367                         if (value) {
368                                 if (acllen > size)
369                                         acllen = -ERANGE;
370                                 else
371                                         memcpy(value, pacl, acllen);
372                         }
373                         rc = acllen;
374                         kfree(pacl);
375                 }
376                 break;
377         }
378
379         case XATTR_ACL_ACCESS:
380 #ifdef CONFIG_CIFS_POSIX
381                 if (sb->s_flags & SB_POSIXACL)
382                         rc = CIFSSMBGetPosixACL(xid, pTcon, full_path,
383                                 value, size, ACL_TYPE_ACCESS,
384                                 cifs_sb->local_nls,
385                                 cifs_remap(cifs_sb));
386 #endif  /* CONFIG_CIFS_POSIX */
387                 break;
388
389         case XATTR_ACL_DEFAULT:
390 #ifdef CONFIG_CIFS_POSIX
391                 if (sb->s_flags & SB_POSIXACL)
392                         rc = CIFSSMBGetPosixACL(xid, pTcon, full_path,
393                                 value, size, ACL_TYPE_DEFAULT,
394                                 cifs_sb->local_nls,
395                                 cifs_remap(cifs_sb));
396 #endif  /* CONFIG_CIFS_POSIX */
397                 break;
398         }
399
400         /* We could add an additional check for streams ie
401             if proc/fs/cifs/streamstoxattr is set then
402                 search server for EAs or streams to
403                 returns as xattrs */
404
405         if (rc == -EINVAL)
406                 rc = -EOPNOTSUPP;
407
408 out:
409         free_dentry_path(page);
410         free_xid(xid);
411         cifs_put_tlink(tlink);
412         return rc;
413 }
414
415 ssize_t cifs_listxattr(struct dentry *direntry, char *data, size_t buf_size)
416 {
417         ssize_t rc = -EOPNOTSUPP;
418         unsigned int xid;
419         struct cifs_sb_info *cifs_sb = CIFS_SB(direntry->d_sb);
420         struct tcon_link *tlink;
421         struct cifs_tcon *pTcon;
422         const char *full_path;
423         void *page;
424
425         if (unlikely(cifs_forced_shutdown(cifs_sb)))
426                 return -EIO;
427
428         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
429                 return -EOPNOTSUPP;
430
431         tlink = cifs_sb_tlink(cifs_sb);
432         if (IS_ERR(tlink))
433                 return PTR_ERR(tlink);
434         pTcon = tlink_tcon(tlink);
435
436         xid = get_xid();
437         page = alloc_dentry_path();
438
439         full_path = build_path_from_dentry(direntry, page);
440         if (IS_ERR(full_path)) {
441                 rc = PTR_ERR(full_path);
442                 goto list_ea_exit;
443         }
444         /* return dos attributes as pseudo xattr */
445         /* return alt name if available as pseudo attr */
446
447         /* if proc/fs/cifs/streamstoxattr is set then
448                 search server for EAs or streams to
449                 returns as xattrs */
450
451         if (pTcon->ses->server->ops->query_all_EAs)
452                 rc = pTcon->ses->server->ops->query_all_EAs(xid, pTcon,
453                                 full_path, NULL, data, buf_size, cifs_sb);
454 list_ea_exit:
455         free_dentry_path(page);
456         free_xid(xid);
457         cifs_put_tlink(tlink);
458         return rc;
459 }
460
461 static const struct xattr_handler cifs_user_xattr_handler = {
462         .prefix = XATTR_USER_PREFIX,
463         .flags = XATTR_USER,
464         .get = cifs_xattr_get,
465         .set = cifs_xattr_set,
466 };
467
468 /* os2.* attributes are treated like user.* attributes */
469 static const struct xattr_handler cifs_os2_xattr_handler = {
470         .prefix = XATTR_OS2_PREFIX,
471         .flags = XATTR_USER,
472         .get = cifs_xattr_get,
473         .set = cifs_xattr_set,
474 };
475
476 static const struct xattr_handler cifs_cifs_acl_xattr_handler = {
477         .name = CIFS_XATTR_CIFS_ACL,
478         .flags = XATTR_CIFS_ACL,
479         .get = cifs_xattr_get,
480         .set = cifs_xattr_set,
481 };
482
483 /*
484  * Although this is just an alias for the above, need to move away from
485  * confusing users and using the 20 year old term 'cifs' when it is no
486  * longer secure and was replaced by SMB2/SMB3 a long time ago, and
487  * SMB3 and later are highly secure.
488  */
489 static const struct xattr_handler smb3_acl_xattr_handler = {
490         .name = SMB3_XATTR_CIFS_ACL,
491         .flags = XATTR_CIFS_ACL,
492         .get = cifs_xattr_get,
493         .set = cifs_xattr_set,
494 };
495
496 static const struct xattr_handler cifs_cifs_ntsd_xattr_handler = {
497         .name = CIFS_XATTR_CIFS_NTSD,
498         .flags = XATTR_CIFS_NTSD,
499         .get = cifs_xattr_get,
500         .set = cifs_xattr_set,
501 };
502
503 /*
504  * Although this is just an alias for the above, need to move away from
505  * confusing users and using the 20 year old term 'cifs' when it is no
506  * longer secure and was replaced by SMB2/SMB3 a long time ago, and
507  * SMB3 and later are highly secure.
508  */
509 static const struct xattr_handler smb3_ntsd_xattr_handler = {
510         .name = SMB3_XATTR_CIFS_NTSD,
511         .flags = XATTR_CIFS_NTSD,
512         .get = cifs_xattr_get,
513         .set = cifs_xattr_set,
514 };
515
516 static const struct xattr_handler cifs_cifs_ntsd_full_xattr_handler = {
517         .name = CIFS_XATTR_CIFS_NTSD_FULL,
518         .flags = XATTR_CIFS_NTSD_FULL,
519         .get = cifs_xattr_get,
520         .set = cifs_xattr_set,
521 };
522
523 /*
524  * Although this is just an alias for the above, need to move away from
525  * confusing users and using the 20 year old term 'cifs' when it is no
526  * longer secure and was replaced by SMB2/SMB3 a long time ago, and
527  * SMB3 and later are highly secure.
528  */
529 static const struct xattr_handler smb3_ntsd_full_xattr_handler = {
530         .name = SMB3_XATTR_CIFS_NTSD_FULL,
531         .flags = XATTR_CIFS_NTSD_FULL,
532         .get = cifs_xattr_get,
533         .set = cifs_xattr_set,
534 };
535
536
537 static const struct xattr_handler cifs_posix_acl_access_xattr_handler = {
538         .name = XATTR_NAME_POSIX_ACL_ACCESS,
539         .flags = XATTR_ACL_ACCESS,
540         .get = cifs_xattr_get,
541         .set = cifs_xattr_set,
542 };
543
544 static const struct xattr_handler cifs_posix_acl_default_xattr_handler = {
545         .name = XATTR_NAME_POSIX_ACL_DEFAULT,
546         .flags = XATTR_ACL_DEFAULT,
547         .get = cifs_xattr_get,
548         .set = cifs_xattr_set,
549 };
550
551 const struct xattr_handler *cifs_xattr_handlers[] = {
552         &cifs_user_xattr_handler,
553         &cifs_os2_xattr_handler,
554         &cifs_cifs_acl_xattr_handler,
555         &smb3_acl_xattr_handler, /* alias for above since avoiding "cifs" */
556         &cifs_cifs_ntsd_xattr_handler,
557         &smb3_ntsd_xattr_handler, /* alias for above since avoiding "cifs" */
558         &cifs_cifs_ntsd_full_xattr_handler,
559         &smb3_ntsd_full_xattr_handler, /* alias for above since avoiding "cifs" */
560         &cifs_posix_acl_access_xattr_handler,
561         &cifs_posix_acl_default_xattr_handler,
562         NULL
563 };