4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
34 #include <linux/sched.h>
36 #include <linux/xattr.h>
37 #include <linux/selinux.h>
39 #define DEBUG_SUBSYSTEM S_LLITE
41 #include <obd_support.h>
42 #include <lustre_dlm.h>
43 #include <lustre_eacl.h>
45 #include "llite_internal.h"
47 const struct xattr_handler *get_xattr_type(const char *name)
51 while (ll_xattr_handlers[i]) {
52 size_t len = strlen(ll_xattr_handlers[i]->prefix);
54 if (!strncmp(ll_xattr_handlers[i]->prefix, name, len))
55 return ll_xattr_handlers[i];
61 static int xattr_type_filter(struct ll_sb_info *sbi,
62 const struct xattr_handler *handler)
64 /* No handler means XATTR_OTHER_T */
68 if ((handler->flags == XATTR_ACL_ACCESS_T ||
69 handler->flags == XATTR_ACL_DEFAULT_T) &&
70 !(sbi->ll_flags & LL_SBI_ACL))
73 if (handler->flags == XATTR_USER_T &&
74 !(sbi->ll_flags & LL_SBI_USER_XATTR))
77 if (handler->flags == XATTR_TRUSTED_T &&
78 !capable(CFS_CAP_SYS_ADMIN))
84 static int ll_xattr_set_common(const struct xattr_handler *handler,
85 struct dentry *dentry, struct inode *inode,
86 const char *name, const void *value, size_t size,
89 struct ll_sb_info *sbi = ll_i2sbi(inode);
90 struct ptlrpc_request *req = NULL;
91 const char *pv = value;
97 /* When setxattr() is called with a size of 0 the value is
98 * unconditionally replaced by "". When removexattr() is
99 * called we get a NULL value and XATTR_REPLACE for flags. */
100 if (!value && flags == XATTR_REPLACE) {
101 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_REMOVEXATTR, 1);
102 valid = OBD_MD_FLXATTRRM;
104 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_SETXATTR, 1);
105 valid = OBD_MD_FLXATTR;
108 /* FIXME: enable IMA when the conditions are ready */
109 if (handler->flags == XATTR_SECURITY_T &&
110 (!strcmp(name, "ima") || !strcmp(name, "evm")))
113 rc = xattr_type_filter(sbi, handler);
117 if ((handler->flags == XATTR_ACL_ACCESS_T ||
118 handler->flags == XATTR_ACL_DEFAULT_T) &&
119 #ifdef HAVE_INODE_OWNER_OR_CAPABLE
120 !inode_owner_or_capable(inode))
122 !is_owner_or_cap(inode))
126 /* b10667: ignore lustre special xattr for now */
127 if (!strcmp(name, "hsm") ||
128 ((handler->flags == XATTR_TRUSTED_T && !strcmp(name, "lov")) ||
129 (handler->flags == XATTR_LUSTRE_T && !strcmp(name, "lov"))))
132 /* LU-549: Disable security.selinux when selinux is disabled */
133 if (handler->flags == XATTR_SECURITY_T && !selinux_is_enabled() &&
134 strcmp(name, "selinux") == 0)
138 * In user.* namespace, only regular files and directories can have
139 * extended attributes.
141 if (handler->flags == XATTR_USER_T) {
142 if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
146 fullname = kasprintf(GFP_KERNEL, "%s%s", handler->prefix, name);
150 rc = md_setxattr(sbi->ll_md_exp, ll_inode2fid(inode), valid, fullname,
151 pv, size, flags, ll_i2suppgid(inode), &req);
154 if (rc == -EOPNOTSUPP && handler->flags == XATTR_USER_T) {
155 LCONSOLE_INFO("Disabling user_xattr feature because it is not supported on the server\n");
156 sbi->ll_flags &= ~LL_SBI_USER_XATTR;
161 ptlrpc_req_finished(req);
165 static int get_hsm_state(struct inode *inode, u32 *hus_states)
167 struct md_op_data *op_data;
168 struct hsm_user_state *hus;
175 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
176 LUSTRE_OPC_ANY, hus);
177 if (!IS_ERR(op_data)) {
178 rc = obd_iocontrol(LL_IOC_HSM_STATE_GET, ll_i2mdexp(inode),
179 sizeof(*op_data), op_data, NULL);
181 *hus_states = hus->hus_states;
183 CDEBUG(D_VFSTRACE, "obd_iocontrol failed. rc = %d\n",
186 ll_finish_md_op_data(op_data);
188 rc = PTR_ERR(op_data);
189 CDEBUG(D_VFSTRACE, "Could not prepare the opdata. rc = %d\n",
196 static int ll_adjust_lum(struct inode *inode, struct lov_user_md *lump)
198 struct lov_comp_md_v1 *comp_v1 = (struct lov_comp_md_v1 *)lump;
199 struct lov_user_md *v1 = lump;
200 bool need_clear_release = false;
201 bool release_checked = false;
202 bool is_composite = false;
210 if (lump->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
211 entry_count = comp_v1->lcm_entry_count;
215 for (i = 0; i < entry_count; i++) {
216 if (lump->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
219 ptr += comp_v1->lcm_entries[i].lcme_offset;
220 v1 = (struct lov_user_md *)ptr;
224 * Attributes that are saved via getxattr will always
225 * have the stripe_offset as 0. Instead, the MDS
226 * should be allowed to pick the starting OST index.
229 if (!is_composite && v1->lmm_stripe_offset == 0)
230 v1->lmm_stripe_offset = -1;
232 /* Avoid anyone directly setting the RELEASED flag. */
233 if (v1->lmm_pattern & LOV_PATTERN_F_RELEASED) {
234 if (!release_checked) {
237 rc = get_hsm_state(inode, &state);
241 if (!(state & HS_ARCHIVED))
242 need_clear_release = true;
243 release_checked = true;
245 if (need_clear_release)
246 v1->lmm_pattern ^= LOV_PATTERN_F_RELEASED;
253 static int ll_setstripe_ea(struct dentry *dentry, struct lov_user_md *lump,
256 struct inode *inode = dentry->d_inode;
260 * It is possible to set an xattr to a "" value of zero size.
261 * For this case we are going to treat it as a removal.
266 rc = ll_adjust_lum(inode, lump);
270 if (lump && S_ISREG(inode->i_mode)) {
271 u64 it_flags = FMODE_WRITE;
274 lum_size = ll_lov_user_md_size(lump);
275 if (lum_size < 0 || size < lum_size)
278 rc = ll_lov_setstripe_ea_info(inode, dentry, it_flags, lump,
281 * b=10667: ignore -EEXIST.
282 * Silently eat error on setting trusted.lov/lustre.lov
283 * attribute for platforms that added the default option
284 * to copy all attributes in 'cp' command. Both rsync and
285 * tar --xattrs also will try to set LOVEA for existing
290 } else if (S_ISDIR(inode->i_mode)) {
291 if (size != 0 && size < sizeof(struct lov_user_md))
294 rc = ll_dir_setstripe(inode, lump, 0);
300 static int ll_xattr_set(const struct xattr_handler *handler,
301 struct dentry *dentry, struct inode *inode,
302 const char *name, const void *value, size_t size,
308 CDEBUG(D_VFSTRACE, "VFS Op:inode=" DFID "(%p), xattr %s\n",
309 PFID(ll_inode2fid(inode)), inode, name);
311 /* lustre/trusted.lov.xxx would be passed through xattr API */
312 if (!strcmp(name, "lov")) {
313 int op_type = flags == XATTR_REPLACE ? LPROC_LL_REMOVEXATTR :
316 ll_stats_ops_tally(ll_i2sbi(inode), op_type, 1);
318 return ll_setstripe_ea(dentry, (struct lov_user_md *)value,
320 } else if (!strcmp(name, "lma") || !strcmp(name, "link")) {
321 int op_type = flags == XATTR_REPLACE ? LPROC_LL_REMOVEXATTR :
324 ll_stats_ops_tally(ll_i2sbi(inode), op_type, 1);
328 return ll_xattr_set_common(handler, dentry, inode, name, value, size,
332 int ll_xattr_list(struct inode *inode, const char *name, int type, void *buffer,
333 size_t size, u64 valid)
335 struct ll_inode_info *lli = ll_i2info(inode);
336 struct ll_sb_info *sbi = ll_i2sbi(inode);
337 struct ptlrpc_request *req = NULL;
338 struct mdt_body *body;
343 if (sbi->ll_xattr_cache_enabled && type != XATTR_ACL_ACCESS_T &&
344 (type != XATTR_SECURITY_T || strcmp(name, "security.selinux"))) {
345 rc = ll_xattr_cache_get(inode, name, buffer, size, valid);
347 goto getxattr_nocache;
351 /* Add "system.posix_acl_access" to the list */
352 if (lli->lli_posix_acl && valid & OBD_MD_FLXATTRLS) {
354 rc += sizeof(XATTR_NAME_ACL_ACCESS);
355 } else if (size - rc >= sizeof(XATTR_NAME_ACL_ACCESS)) {
356 memcpy(buffer + rc, XATTR_NAME_ACL_ACCESS,
357 sizeof(XATTR_NAME_ACL_ACCESS));
358 rc += sizeof(XATTR_NAME_ACL_ACCESS);
360 GOTO(out_xattr, rc = -ERANGE);
365 rc = md_getxattr(sbi->ll_md_exp, ll_inode2fid(inode), valid,
370 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
373 /* only detect the xattr size */
375 GOTO(out, rc = body->mbo_eadatasize);
377 if (size < body->mbo_eadatasize) {
378 CERROR("server bug: replied size %u > %u\n",
379 body->mbo_eadatasize, (int)size);
380 GOTO(out, rc = -ERANGE);
383 if (body->mbo_eadatasize == 0)
384 GOTO(out, rc = -ENODATA);
386 /* do not need swab xattr data */
387 xdata = req_capsule_server_sized_get(&req->rq_pill, &RMF_EADATA,
388 body->mbo_eadatasize);
390 GOTO(out, rc = -EFAULT);
392 memcpy(buffer, xdata, body->mbo_eadatasize);
393 rc = body->mbo_eadatasize;
399 if (rc == -EOPNOTSUPP && type == XATTR_USER_T) {
400 LCONSOLE_INFO("%s: disabling user_xattr feature because "
401 "it is not supported on the server: rc = %d\n",
402 ll_get_fsname(inode->i_sb, NULL, 0), rc);
403 sbi->ll_flags &= ~LL_SBI_USER_XATTR;
406 ptlrpc_req_finished(req);
410 static int ll_xattr_get_common(const struct xattr_handler *handler,
411 struct dentry *dentry,
413 const char *name, void *buffer, size_t size)
415 struct ll_sb_info *sbi = ll_i2sbi(inode);
421 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_GETXATTR, 1);
423 rc = xattr_type_filter(sbi, handler);
427 /* LU-549: Disable security.selinux when selinux is disabled */
428 if (handler->flags == XATTR_SECURITY_T && !selinux_is_enabled() &&
429 !strcmp(name, "selinux"))
432 #ifdef CONFIG_FS_POSIX_ACL
433 /* posix acl is under protection of LOOKUP lock. when calling to this,
434 * we just have path resolution to the target inode, so we have great
435 * chance that cached ACL is uptodate.
437 if (handler->flags == XATTR_ACL_ACCESS_T) {
438 struct ll_inode_info *lli = ll_i2info(inode);
439 struct posix_acl *acl;
441 spin_lock(&lli->lli_lock);
442 acl = posix_acl_dup(lli->lli_posix_acl);
443 spin_unlock(&lli->lli_lock);
448 rc = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
449 posix_acl_release(acl);
452 if (handler->flags == XATTR_ACL_DEFAULT_T && !S_ISDIR(inode->i_mode))
456 fullname = kasprintf(GFP_KERNEL, "%s%s", handler->prefix, name);
460 rc = ll_xattr_list(inode, fullname, handler->flags, buffer, size,
466 static ssize_t ll_getxattr_lov(struct inode *inode, void *buf, size_t buf_size)
470 if (S_ISREG(inode->i_mode)) {
471 struct cl_object *obj = ll_i2info(inode)->lli_clob;
472 struct cl_layout cl = {
473 .cl_buf.lb_buf = buf,
474 .cl_buf.lb_len = buf_size,
482 env = cl_env_get(&refcheck);
484 RETURN(PTR_ERR(env));
486 rc = cl_object_layout_get(env, obj, &cl);
491 GOTO(out_env, rc = -ENODATA);
498 LASSERT(buf && rc <= buf_size);
501 * Do not return layout gen for getxattr() since
502 * otherwise it would confuse tar --xattr by
503 * recognizing layout gen as stripe offset when the
504 * file is restored. See LU-2809.
506 if (((struct lov_mds_md *)buf)->lmm_magic == LOV_MAGIC_COMP_V1)
509 ((struct lov_mds_md *)buf)->lmm_layout_gen = 0;
511 cl_env_put(env, &refcheck);
514 } else if (S_ISDIR(inode->i_mode)) {
515 struct ptlrpc_request *req = NULL;
516 struct lov_mds_md *lmm = NULL;
519 rc = ll_dir_getstripe(inode, (void **)&lmm, &lmm_size,
525 GOTO(out_req, rc = lmm_size);
527 if (buf_size < lmm_size)
528 GOTO(out_req, rc = -ERANGE);
530 memcpy(buf, lmm, lmm_size);
531 GOTO(out_req, rc = lmm_size);
534 ptlrpc_req_finished(req);
542 static int ll_xattr_get(const struct xattr_handler *handler,
543 struct dentry *dentry, struct inode *inode,
544 const char *name, void *buffer, size_t size)
549 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), xattr %s\n",
550 PFID(ll_inode2fid(inode)), inode, name);
552 if (!strcmp(name, "lov")) {
553 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_GETXATTR, 1);
555 return ll_getxattr_lov(inode, buffer, size);
558 return ll_xattr_get_common(handler, dentry, inode, name, buffer, size);
561 ssize_t ll_listxattr(struct dentry *dentry, char *buffer, size_t size)
563 struct inode *inode = dentry->d_inode;
564 struct ll_sb_info *sbi = ll_i2sbi(inode);
571 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p)\n",
572 PFID(ll_inode2fid(inode)), inode);
574 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_LISTXATTR, 1);
576 rc = ll_xattr_list(inode, NULL, XATTR_OTHER_T, buffer, size,
582 * If we're being called to get the size of the xattr list
583 * (size == 0) then just assume that a lustre.lov xattr
587 RETURN(rc + sizeof(XATTR_LUSTRE_LOV));
593 len = strnlen(xattr_name, rem - 1) + 1;
595 if (!xattr_type_filter(sbi, get_xattr_type(xattr_name))) {
596 /* Skip OK xattr type, leave it in buffer. */
602 * Move up remaining xattrs in buffer
603 * removing the xattr that is not OK.
605 memmove(xattr_name, xattr_name + len, rem);
609 rc2 = ll_getxattr_lov(inode, NULL, 0);
616 if (size < rc + sizeof(XATTR_LUSTRE_LOV))
619 memcpy(buffer + rc, XATTR_LUSTRE_LOV, sizeof(XATTR_LUSTRE_LOV));
621 RETURN(rc + sizeof(XATTR_LUSTRE_LOV));
624 #ifdef HAVE_XATTR_HANDLER_SIMPLIFIED
625 static int ll_xattr_get_common_4_3(const struct xattr_handler *handler,
626 struct dentry *dentry, const char *name,
627 void *buffer, size_t size)
629 return ll_xattr_get_common(handler, dentry, dentry->d_inode, name,
633 static int ll_xattr_get_4_3(const struct xattr_handler *handler,
634 struct dentry *dentry, const char *name,
635 void *buffer, size_t size)
637 return ll_xattr_get(handler, dentry, dentry->d_inode, name, buffer,
641 static int ll_xattr_set_common_4_3(const struct xattr_handler *handler,
642 struct dentry *dentry, const char *name,
643 const void *value, size_t size, int flags)
645 return ll_xattr_set_common(handler, dentry, dentry->d_inode, name,
649 static int ll_xattr_set_4_3(const struct xattr_handler *handler,
650 struct dentry *dentry, const char *name,
651 const void *value, size_t size, int flags)
653 return ll_xattr_set(handler, dentry, dentry->d_inode, name, value,
657 #elif !defined(HAVE_XATTR_HANDLER_INODE_PARAM)
658 const struct xattr_handler *get_xattr_handler(int handler_flag)
662 while (ll_xattr_handlers[i]) {
663 if (ll_xattr_handlers[i]->flags == handler_flag)
664 return ll_xattr_handlers[i];
670 static int ll_xattr_get_common_3_11(struct dentry *dentry, const char *name,
671 void *buffer, size_t size, int handler_flags)
673 const struct xattr_handler *handler = get_xattr_handler(handler_flags);
678 return ll_xattr_get_common(handler, dentry, dentry->d_inode, name,
682 static int ll_xattr_get_3_11(struct dentry *dentry, const char *name,
683 void *buffer, size_t size, int handler_flags)
685 const struct xattr_handler *handler = get_xattr_handler(handler_flags);
690 return ll_xattr_get(handler, dentry, dentry->d_inode, name, buffer,
694 static int ll_xattr_set_common_3_11(struct dentry *dentry, const char *name,
695 const void *value, size_t size, int flags,
698 const struct xattr_handler *handler = get_xattr_handler(handler_flags);
703 return ll_xattr_set_common(handler, dentry, dentry->d_inode, name,
707 static int ll_xattr_set_3_11(struct dentry *dentry, const char *name,
708 const void *value, size_t size, int flags,
711 const struct xattr_handler *handler = get_xattr_handler(handler_flags);
716 return ll_xattr_set(handler, dentry, dentry->d_inode, name, value,
721 static const struct xattr_handler ll_user_xattr_handler = {
722 .prefix = XATTR_USER_PREFIX,
723 .flags = XATTR_USER_T,
724 #if defined(HAVE_XATTR_HANDLER_SIMPLIFIED)
725 .get = ll_xattr_get_common_4_3,
726 .set = ll_xattr_set_common_4_3,
727 #elif !defined(HAVE_XATTR_HANDLER_INODE_PARAM)
728 .get = ll_xattr_get_common_3_11,
729 .set = ll_xattr_set_common_3_11,
731 .get = ll_xattr_get_common,
732 .set = ll_xattr_set_common,
736 static const struct xattr_handler ll_trusted_xattr_handler = {
737 .prefix = XATTR_TRUSTED_PREFIX,
738 .flags = XATTR_TRUSTED_T,
739 #if defined(HAVE_XATTR_HANDLER_SIMPLIFIED)
740 .get = ll_xattr_get_4_3,
741 .set = ll_xattr_set_4_3,
742 #elif !defined(HAVE_XATTR_HANDLER_INODE_PARAM)
743 .get = ll_xattr_get_3_11,
744 .set = ll_xattr_set_3_11,
751 static const struct xattr_handler ll_security_xattr_handler = {
752 .prefix = XATTR_SECURITY_PREFIX,
753 .flags = XATTR_SECURITY_T,
754 #if defined(HAVE_XATTR_HANDLER_SIMPLIFIED)
755 .get = ll_xattr_get_common_4_3,
756 .set = ll_xattr_set_common_4_3,
757 #elif !defined(HAVE_XATTR_HANDLER_INODE_PARAM)
758 .get = ll_xattr_get_common_3_11,
759 .set = ll_xattr_set_common_3_11,
761 .get = ll_xattr_get_common,
762 .set = ll_xattr_set_common,
766 static const struct xattr_handler ll_acl_access_xattr_handler = {
767 .prefix = XATTR_NAME_POSIX_ACL_ACCESS,
768 .flags = XATTR_ACL_ACCESS_T,
769 #if defined(HAVE_XATTR_HANDLER_SIMPLIFIED)
770 .get = ll_xattr_get_common_4_3,
771 .set = ll_xattr_set_common_4_3,
772 #elif !defined(HAVE_XATTR_HANDLER_INODE_PARAM)
773 .get = ll_xattr_get_common_3_11,
774 .set = ll_xattr_set_common_3_11,
776 .get = ll_xattr_get_common,
777 .set = ll_xattr_set_common,
781 static const struct xattr_handler ll_acl_default_xattr_handler = {
782 .prefix = XATTR_NAME_POSIX_ACL_DEFAULT,
783 .flags = XATTR_ACL_DEFAULT_T,
784 #if defined(HAVE_XATTR_HANDLER_SIMPLIFIED)
785 .get = ll_xattr_get_common_4_3,
786 .set = ll_xattr_set_common_4_3,
787 #elif !defined(HAVE_XATTR_HANDLER_INODE_PARAM)
788 .get = ll_xattr_get_common_3_11,
789 .set = ll_xattr_set_common_3_11,
791 .get = ll_xattr_get_common,
792 .set = ll_xattr_set_common,
796 static const struct xattr_handler ll_lustre_xattr_handler = {
797 .prefix = XATTR_LUSTRE_PREFIX,
798 .flags = XATTR_LUSTRE_T,
799 #if defined(HAVE_XATTR_HANDLER_SIMPLIFIED)
800 .get = ll_xattr_get_4_3,
801 .set = ll_xattr_set_4_3,
802 #elif !defined(HAVE_XATTR_HANDLER_INODE_PARAM)
803 .get = ll_xattr_get_3_11,
804 .set = ll_xattr_set_3_11,
811 const struct xattr_handler *ll_xattr_handlers[] = {
812 &ll_user_xattr_handler,
813 &ll_trusted_xattr_handler,
814 &ll_security_xattr_handler,
815 #ifdef CONFIG_FS_POSIX_ACL
816 &ll_acl_access_xattr_handler,
817 &ll_acl_default_xattr_handler,
819 &ll_lustre_xattr_handler,