Whamcloud - gitweb
LU-14651 llite: extend inode methods with user namespace arg
[fs/lustre-release.git] / lustre / llite / acl.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lustre/llite/acl.c
33  *
34  * Author: Peter Braam <braam@clusterfs.com>
35  * Author: Phil Schwan <phil@clusterfs.com>
36  * Author: Andreas Dilger <adilger@clusterfs.com>
37  */
38
39 #include "llite_internal.h"
40
41 struct posix_acl *ll_get_acl(struct inode *inode, int type)
42 {
43         struct ll_inode_info *lli = ll_i2info(inode);
44         struct posix_acl *acl = NULL;
45         ENTRY;
46
47         read_lock(&lli->lli_lock);
48         /* VFS' acl_permission_check->check_acl will release the refcount */
49         acl = posix_acl_dup(lli->lli_posix_acl);
50         read_unlock(&lli->lli_lock);
51
52         RETURN(acl);
53 }
54
55 #ifdef HAVE_IOP_SET_ACL
56 int ll_set_acl(struct user_namespace *mnt_userns, struct inode *inode,
57                struct posix_acl *acl, int type)
58 {
59         struct ll_sb_info *sbi = ll_i2sbi(inode);
60         struct ptlrpc_request *req = NULL;
61         const char *name = NULL;
62         char *value = NULL;
63         size_t value_size = 0;
64         int rc = 0;
65         ENTRY;
66
67         switch (type) {
68         case ACL_TYPE_ACCESS:
69                 name = XATTR_NAME_POSIX_ACL_ACCESS;
70                 if (acl)
71                         rc = posix_acl_update_mode(mnt_userns, inode,
72                                                    &inode->i_mode, &acl);
73                 break;
74
75         case ACL_TYPE_DEFAULT:
76                 name = XATTR_NAME_POSIX_ACL_DEFAULT;
77                 if (!S_ISDIR(inode->i_mode))
78                         rc = acl ? -EACCES : 0;
79                 break;
80
81         default:
82                 rc = -EINVAL;
83                 break;
84         }
85         if (rc)
86                 return rc;
87
88         if (acl) {
89                 value_size = posix_acl_xattr_size(acl->a_count);
90                 value = kmalloc(value_size, GFP_NOFS);
91                 if (value == NULL)
92                         GOTO(out, rc = -ENOMEM);
93
94                 rc = posix_acl_to_xattr(&init_user_ns, acl, value, value_size);
95                 if (rc < 0)
96                         GOTO(out_value, rc);
97         }
98
99         rc = md_setxattr(sbi->ll_md_exp, ll_inode2fid(inode),
100                          value ? OBD_MD_FLXATTR : OBD_MD_FLXATTRRM,
101                          name, value, value_size, 0, 0, &req);
102
103         ptlrpc_req_finished(req);
104 out_value:
105         kfree(value);
106 out:
107         if (rc)
108                 forget_cached_acl(inode, type);
109         else
110                 set_cached_acl(inode, type, acl);
111         RETURN(rc);
112 }
113 #endif /* HAVE_IOP_SET_ACL */