2 * Copyright (C) 2014-2017 Junjiro R. Okajima
4 * This program, aufs is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 * handling xattr functions
23 #include <linux/posix_acl_xattr.h>
24 #include <linux/xattr.h>
27 static int au_xattr_ignore(int err
, char *name
, unsigned int ignore_flags
)
37 if ((ignore_flags
& AuBrAttr_ICEX
) == AuBrAttr_ICEX
) {
42 #define cmp(brattr, prefix) do { \
43 if (!strncmp(name, XATTR_##prefix##_PREFIX, \
44 XATTR_##prefix##_PREFIX_LEN)) { \
45 if (ignore_flags & AuBrAttr_ICEX_##brattr) \
57 if (ignore_flags
& AuBrAttr_ICEX_OTH
)
64 static const int au_xattr_out_of_list
= AuBrAttr_ICEX_OTH
<< 1;
66 static int au_do_cpup_xattr(struct dentry
*h_dst
, struct dentry
*h_src
,
67 char *name
, char **buf
, unsigned int ignore_flags
,
74 ssz
= vfs_getxattr_alloc(h_src
, name
, buf
, 0, GFP_NOFS
);
76 if (unlikely(err
<= 0)) {
78 || (err
== -EOPNOTSUPP
79 && ((ignore_flags
& au_xattr_out_of_list
)
80 || (au_test_nfs_noacl(d_inode(h_src
))
81 && (!strcmp(name
, XATTR_NAME_POSIX_ACL_ACCESS
)
83 XATTR_NAME_POSIX_ACL_DEFAULT
))))
86 if (err
&& (verbose
|| au_debug_test()))
87 pr_err("%s, err %d\n", name
, err
);
91 /* unlock it temporary */
92 h_idst
= d_inode(h_dst
);
94 err
= vfsub_setxattr(h_dst
, name
, *buf
, ssz
, /*flags*/0);
95 inode_lock_nested(h_idst
, AuLsc_I_CHILD2
);
97 if (verbose
|| au_debug_test())
98 pr_err("%s, err %d\n", name
, err
);
99 err
= au_xattr_ignore(err
, name
, ignore_flags
);
106 int au_cpup_xattr(struct dentry
*h_dst
, struct dentry
*h_src
, int ignore_flags
,
107 unsigned int verbose
)
109 int err
, unlocked
, acl_access
, acl_default
;
111 struct inode
*h_isrc
, *h_idst
;
112 char *value
, *p
, *o
, *e
;
114 /* try stopping to update the source inode while we are referencing */
115 /* there should not be the parent-child relationship between them */
116 h_isrc
= d_inode(h_src
);
117 h_idst
= d_inode(h_dst
);
118 inode_unlock(h_idst
);
119 vfsub_inode_lock_shared_nested(h_isrc
, AuLsc_I_CHILD
);
120 inode_lock_nested(h_idst
, AuLsc_I_CHILD2
);
123 /* some filesystems don't list POSIX ACL, for example tmpfs */
124 ssz
= vfs_listxattr(h_src
, NULL
, 0);
126 if (unlikely(err
< 0)) {
129 || err
== -EOPNOTSUPP
)
130 err
= 0; /* ignore */
139 p
= kmalloc(ssz
, GFP_NOFS
);
143 err
= vfs_listxattr(h_src
, p
, ssz
);
145 inode_unlock_shared(h_isrc
);
147 AuDbg("err %d, ssz %zd\n", err
, ssz
);
148 if (unlikely(err
< 0))
156 while (!err
&& p
< e
) {
157 acl_access
|= !strncmp(p
, XATTR_NAME_POSIX_ACL_ACCESS
,
158 sizeof(XATTR_NAME_POSIX_ACL_ACCESS
) - 1);
159 acl_default
|= !strncmp(p
, XATTR_NAME_POSIX_ACL_DEFAULT
,
160 sizeof(XATTR_NAME_POSIX_ACL_DEFAULT
)
162 err
= au_do_cpup_xattr(h_dst
, h_src
, p
, &value
, ignore_flags
,
167 ignore_flags
|= au_xattr_out_of_list
;
168 if (!err
&& !acl_access
) {
169 err
= au_do_cpup_xattr(h_dst
, h_src
,
170 XATTR_NAME_POSIX_ACL_ACCESS
, &value
,
171 ignore_flags
, verbose
);
174 if (!err
&& !acl_default
) {
175 err
= au_do_cpup_xattr(h_dst
, h_src
,
176 XATTR_NAME_POSIX_ACL_DEFAULT
, &value
,
177 ignore_flags
, verbose
);
187 inode_unlock_shared(h_isrc
);
192 /* ---------------------------------------------------------------------- */
194 static int au_smack_reentering(struct super_block
*sb
)
196 #if IS_ENABLED(CONFIG_SECURITY_SMACK)
198 * as a part of lookup, smack_d_instantiate() is called, and it calls
199 * i_op->getxattr(). ouch.
201 return si_pid_test(sb
);
227 static ssize_t
au_lgxattr(struct dentry
*dentry
, struct au_lgxattr
*arg
)
232 struct super_block
*sb
;
235 reenter
= au_smack_reentering(sb
);
237 err
= si_read_lock(sb
, AuLock_FLUSH
| AuLock_NOPLM
);
241 err
= au_h_path_getattr(dentry
, /*force*/1, &h_path
, reenter
);
244 if (unlikely(!h_path
.dentry
))
245 /* illegally overlapped or something */
246 goto out_di
; /* pretending success */
248 /* always topmost entry only */
251 err
= vfs_listxattr(h_path
.dentry
,
252 arg
->u
.list
.list
, arg
->u
.list
.size
);
255 AuDebugOn(d_is_negative(h_path
.dentry
));
256 err
= vfs_getxattr(h_path
.dentry
,
257 arg
->u
.get
.name
, arg
->u
.get
.value
,
264 di_read_unlock(dentry
, AuLock_IR
);
273 ssize_t
aufs_listxattr(struct dentry
*dentry
, char *list
, size_t size
)
275 struct au_lgxattr arg
= {
276 .type
= AU_XATTR_LIST
,
283 return au_lgxattr(dentry
, &arg
);
286 static ssize_t
au_getxattr(struct dentry
*dentry
,
287 struct inode
*inode __maybe_unused
,
288 const char *name
, void *value
, size_t size
)
290 struct au_lgxattr arg
= {
291 .type
= AU_XATTR_GET
,
299 return au_lgxattr(dentry
, &arg
);
302 static int au_setxattr(struct dentry
*dentry
, struct inode
*inode
,
303 const char *name
, const void *value
, size_t size
,
306 struct au_sxattr arg
= {
307 .type
= AU_XATTR_SET
,
316 return au_sxattr(dentry
, inode
, &arg
);
319 /* ---------------------------------------------------------------------- */
321 static int au_xattr_get(const struct xattr_handler
*handler
,
322 struct dentry
*dentry
, struct inode
*inode
,
323 const char *name
, void *buffer
, size_t size
)
325 return au_getxattr(dentry
, inode
, name
, buffer
, size
);
328 static int au_xattr_set(const struct xattr_handler
*handler
,
329 struct dentry
*dentry
, struct inode
*inode
,
330 const char *name
, const void *value
, size_t size
,
333 return au_setxattr(dentry
, inode
, name
, value
, size
, flags
);
336 static const struct xattr_handler au_xattr_handler
= {
343 static const struct xattr_handler
*au_xattr_handlers
[] = {
344 #ifdef CONFIG_FS_POSIX_ACL
345 &posix_acl_access_xattr_handler
,
346 &posix_acl_default_xattr_handler
,
348 &au_xattr_handler
, /* must be last */
352 void au_xattr_init(struct super_block
*sb
)
354 sb
->s_xattr
= au_xattr_handlers
;