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 #ifdef HAVE_LINUX_SELINUX_IS_ENABLED
38 #include <linux/selinux.h>
41 #define DEBUG_SUBSYSTEM S_LLITE
43 #include <obd_support.h>
44 #include <lustre_dlm.h>
45 #include <lustre_eacl.h>
46 #include <lustre_swab.h>
48 #include "llite_internal.h"
50 #ifndef HAVE_XATTR_HANDLER_NAME
51 static inline const char *xattr_prefix(const struct xattr_handler *handler)
53 return handler->prefix;
57 #ifdef HAVE_LINUX_SELINUX_IS_ENABLED
58 # define test_xattr_is_selinux_disabled(handler, name) \
59 ((handler)->flags == XATTR_SECURITY_T && \
60 !selinux_is_enabled() && \
61 strcmp((name), "selinux") == 0)
63 # define test_xattr_is_selinux_disabled(handler, name) \
64 ((handler)->flags == XATTR_SECURITY_T && \
65 strcmp((name), "selinux") == 0)
68 const struct xattr_handler *get_xattr_type(const char *name)
72 for (i = 0; ll_xattr_handlers[i]; i++) {
73 const char *prefix = xattr_prefix(ll_xattr_handlers[i]);
74 size_t prefix_len = strlen(prefix);
76 if (!strncmp(prefix, name, prefix_len))
77 return ll_xattr_handlers[i];
83 static int xattr_type_filter(struct ll_sb_info *sbi,
84 const struct xattr_handler *handler)
86 /* No handler means XATTR_OTHER_T */
90 if ((handler->flags == XATTR_ACL_ACCESS_T ||
91 handler->flags == XATTR_ACL_DEFAULT_T) &&
92 !(sbi->ll_flags & LL_SBI_ACL))
95 if (handler->flags == XATTR_USER_T &&
96 !(sbi->ll_flags & LL_SBI_USER_XATTR))
99 if (handler->flags == XATTR_TRUSTED_T &&
100 !capable(CFS_CAP_SYS_ADMIN))
106 static int ll_xattr_set_common(const struct xattr_handler *handler,
107 struct dentry *dentry, struct inode *inode,
108 const char *name, const void *value, size_t size,
111 struct ll_sb_info *sbi = ll_i2sbi(inode);
112 struct ptlrpc_request *req = NULL;
113 const char *pv = value;
119 /* When setxattr() is called with a size of 0 the value is
120 * unconditionally replaced by "". When removexattr() is
121 * called we get a NULL value and XATTR_REPLACE for flags. */
122 if (!value && flags == XATTR_REPLACE) {
123 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_REMOVEXATTR, 1);
124 valid = OBD_MD_FLXATTRRM;
126 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_SETXATTR, 1);
127 valid = OBD_MD_FLXATTR;
130 /* FIXME: enable IMA when the conditions are ready */
131 if (handler->flags == XATTR_SECURITY_T &&
132 (!strcmp(name, "ima") || !strcmp(name, "evm")))
135 rc = xattr_type_filter(sbi, handler);
139 if ((handler->flags == XATTR_ACL_ACCESS_T ||
140 handler->flags == XATTR_ACL_DEFAULT_T) &&
141 !inode_owner_or_capable(inode))
144 /* b10667: ignore lustre special xattr for now */
145 if (!strcmp(name, "hsm") ||
146 ((handler->flags == XATTR_TRUSTED_T && !strcmp(name, "lov")) ||
147 (handler->flags == XATTR_LUSTRE_T && !strcmp(name, "lov"))))
150 /* LU-549: Disable security.selinux when selinux is disabled */
151 if (test_xattr_is_selinux_disabled(handler, name))
155 * In user.* namespace, only regular files and directories can have
156 * extended attributes.
158 if (handler->flags == XATTR_USER_T) {
159 if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
163 fullname = kasprintf(GFP_KERNEL, "%s%s", xattr_prefix(handler), name);
167 rc = md_setxattr(sbi->ll_md_exp, ll_inode2fid(inode), valid, fullname,
168 pv, size, flags, ll_i2suppgid(inode), &req);
171 if (rc == -EOPNOTSUPP && handler->flags == XATTR_USER_T) {
172 LCONSOLE_INFO("Disabling user_xattr feature because it is not supported on the server\n");
173 sbi->ll_flags &= ~LL_SBI_USER_XATTR;
178 ptlrpc_req_finished(req);
182 static int get_hsm_state(struct inode *inode, u32 *hus_states)
184 struct md_op_data *op_data;
185 struct hsm_user_state *hus;
192 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
193 LUSTRE_OPC_ANY, hus);
194 if (!IS_ERR(op_data)) {
195 rc = obd_iocontrol(LL_IOC_HSM_STATE_GET, ll_i2mdexp(inode),
196 sizeof(*op_data), op_data, NULL);
198 *hus_states = hus->hus_states;
200 CDEBUG(D_VFSTRACE, "obd_iocontrol failed. rc = %d\n",
203 ll_finish_md_op_data(op_data);
205 rc = PTR_ERR(op_data);
206 CDEBUG(D_VFSTRACE, "Could not prepare the opdata. rc = %d\n",
213 static int ll_adjust_lum(struct inode *inode, struct lov_user_md *lump)
215 struct lov_comp_md_v1 *comp_v1 = (struct lov_comp_md_v1 *)lump;
216 struct lov_user_md *v1 = lump;
217 bool need_clear_release = false;
218 bool release_checked = false;
219 bool is_composite = false;
227 if (lump->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
228 entry_count = comp_v1->lcm_entry_count;
232 for (i = 0; i < entry_count; i++) {
233 if (lump->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
236 ptr += comp_v1->lcm_entries[i].lcme_offset;
237 v1 = (struct lov_user_md *)ptr;
241 * Attributes that are saved via getxattr will always
242 * have the stripe_offset as 0. Instead, the MDS
243 * should be allowed to pick the starting OST index.
246 if (!is_composite && v1->lmm_stripe_offset == 0)
247 v1->lmm_stripe_offset = -1;
249 /* Avoid anyone directly setting the RELEASED flag. */
250 if (v1->lmm_pattern & LOV_PATTERN_F_RELEASED) {
251 if (!release_checked) {
254 rc = get_hsm_state(inode, &state);
258 if (!(state & HS_ARCHIVED))
259 need_clear_release = true;
260 release_checked = true;
262 if (need_clear_release)
263 v1->lmm_pattern ^= LOV_PATTERN_F_RELEASED;
270 static int ll_setstripe_ea(struct dentry *dentry, struct lov_user_md *lump,
273 struct inode *inode = dentry->d_inode;
277 * It is possible to set an xattr to a "" value of zero size.
278 * For this case we are going to treat it as a removal.
283 rc = ll_adjust_lum(inode, lump);
287 if (lump && S_ISREG(inode->i_mode)) {
288 u64 it_flags = FMODE_WRITE;
291 lum_size = ll_lov_user_md_size(lump);
292 if (lum_size < 0 || size < lum_size)
295 rc = ll_lov_setstripe_ea_info(inode, dentry, it_flags, lump,
298 * b=10667: ignore -EEXIST.
299 * Silently eat error on setting trusted.lov/lustre.lov
300 * attribute for platforms that added the default option
301 * to copy all attributes in 'cp' command. Both rsync and
302 * tar --xattrs also will try to set LOVEA for existing
307 } else if (S_ISDIR(inode->i_mode)) {
308 if (size != 0 && size < sizeof(struct lov_user_md))
311 rc = ll_dir_setstripe(inode, lump, 0);
317 static int ll_xattr_set(const struct xattr_handler *handler,
318 struct dentry *dentry, struct inode *inode,
319 const char *name, const void *value, size_t size,
325 CDEBUG(D_VFSTRACE, "VFS Op:inode=" DFID "(%p), xattr %s\n",
326 PFID(ll_inode2fid(inode)), inode, name);
328 /* lustre/trusted.lov.xxx would be passed through xattr API */
329 if (!strcmp(name, "lov")) {
330 int op_type = flags == XATTR_REPLACE ? LPROC_LL_REMOVEXATTR :
333 ll_stats_ops_tally(ll_i2sbi(inode), op_type, 1);
335 return ll_setstripe_ea(dentry, (struct lov_user_md *)value,
337 } else if (!strcmp(name, "lma") || !strcmp(name, "link")) {
338 int op_type = flags == XATTR_REPLACE ? LPROC_LL_REMOVEXATTR :
341 ll_stats_ops_tally(ll_i2sbi(inode), op_type, 1);
345 if (strncmp(name, "lov.", 4) == 0 &&
346 (__swab32(((struct lov_user_md *)value)->lmm_magic) &
347 le32_to_cpu(LOV_MAGIC_MASK)) == le32_to_cpu(LOV_MAGIC_MAGIC))
348 lustre_swab_lov_user_md((struct lov_user_md *)value, 0);
350 return ll_xattr_set_common(handler, dentry, inode, name, value, size,
354 int ll_xattr_list(struct inode *inode, const char *name, int type, void *buffer,
355 size_t size, u64 valid)
357 struct ll_inode_info *lli = ll_i2info(inode);
358 struct ll_sb_info *sbi = ll_i2sbi(inode);
359 struct ptlrpc_request *req = NULL;
364 if (sbi->ll_xattr_cache_enabled && type != XATTR_ACL_ACCESS_T &&
365 (type != XATTR_SECURITY_T || strcmp(name, "security.selinux"))) {
366 rc = ll_xattr_cache_get(inode, name, buffer, size, valid);
368 goto getxattr_nocache;
372 /* Add "system.posix_acl_access" to the list */
373 if (lli->lli_posix_acl && valid & OBD_MD_FLXATTRLS) {
375 rc += sizeof(XATTR_NAME_ACL_ACCESS);
376 } else if (size - rc >= sizeof(XATTR_NAME_ACL_ACCESS)) {
377 memcpy(buffer + rc, XATTR_NAME_ACL_ACCESS,
378 sizeof(XATTR_NAME_ACL_ACCESS));
379 rc += sizeof(XATTR_NAME_ACL_ACCESS);
381 GOTO(out_xattr, rc = -ERANGE);
386 rc = md_getxattr(sbi->ll_md_exp, ll_inode2fid(inode), valid,
391 /* only detect the xattr size */
396 GOTO(out, rc = -ERANGE);
398 /* do not need swab xattr data */
399 xdata = req_capsule_server_sized_get(&req->rq_pill, &RMF_EADATA,
402 GOTO(out, rc = -EPROTO);
404 memcpy(buffer, xdata, rc);
410 if (rc == -EOPNOTSUPP && type == XATTR_USER_T) {
411 LCONSOLE_INFO("%s: disabling user_xattr feature because "
412 "it is not supported on the server: rc = %d\n",
414 sbi->ll_flags &= ~LL_SBI_USER_XATTR;
417 ptlrpc_req_finished(req);
421 static int ll_xattr_get_common(const struct xattr_handler *handler,
422 struct dentry *dentry,
424 const char *name, void *buffer, size_t size)
426 struct ll_sb_info *sbi = ll_i2sbi(inode);
432 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_GETXATTR, 1);
434 rc = xattr_type_filter(sbi, handler);
438 /* LU-549: Disable security.selinux when selinux is disabled */
439 if (test_xattr_is_selinux_disabled(handler, name))
442 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
443 /* posix acl is under protection of LOOKUP lock. when calling to this,
444 * we just have path resolution to the target inode, so we have great
445 * chance that cached ACL is uptodate.
447 if (handler->flags == XATTR_ACL_ACCESS_T) {
448 struct ll_inode_info *lli = ll_i2info(inode);
449 struct posix_acl *acl;
451 spin_lock(&lli->lli_lock);
452 acl = posix_acl_dup(lli->lli_posix_acl);
453 spin_unlock(&lli->lli_lock);
458 rc = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
459 posix_acl_release(acl);
462 if (handler->flags == XATTR_ACL_DEFAULT_T && !S_ISDIR(inode->i_mode))
466 fullname = kasprintf(GFP_KERNEL, "%s%s", xattr_prefix(handler), name);
470 rc = ll_xattr_list(inode, fullname, handler->flags, buffer, size,
476 static ssize_t ll_getxattr_lov(struct inode *inode, void *buf, size_t buf_size)
480 if (S_ISREG(inode->i_mode)) {
481 struct cl_object *obj = ll_i2info(inode)->lli_clob;
482 struct cl_layout cl = {
483 .cl_buf.lb_buf = buf,
484 .cl_buf.lb_len = buf_size,
492 env = cl_env_get(&refcheck);
494 RETURN(PTR_ERR(env));
496 rc = cl_object_layout_get(env, obj, &cl);
501 GOTO(out_env, rc = -ENODATA);
508 LASSERT(buf && rc <= buf_size);
511 * Do not return layout gen for getxattr() since
512 * otherwise it would confuse tar --xattr by
513 * recognizing layout gen as stripe offset when the
514 * file is restored. See LU-2809.
516 if ((((struct lov_mds_md *)buf)->lmm_magic &
517 __swab32(LOV_MAGIC_MAGIC)) == __swab32(LOV_MAGIC_MAGIC))
518 lustre_swab_lov_user_md((struct lov_user_md *)buf,
521 switch (((struct lov_mds_md *)buf)->lmm_magic) {
524 case LOV_MAGIC_SPECIFIC:
525 ((struct lov_mds_md *)buf)->lmm_layout_gen = 0;
527 case LOV_MAGIC_COMP_V1:
528 case LOV_MAGIC_FOREIGN:
531 CERROR("Invalid LOV magic %08x\n",
532 ((struct lov_mds_md *)buf)->lmm_magic);
533 GOTO(out_env, rc = -EINVAL);
537 cl_env_put(env, &refcheck);
540 } else if (S_ISDIR(inode->i_mode)) {
541 struct ptlrpc_request *req = NULL;
542 struct lov_mds_md *lmm = NULL;
545 rc = ll_dir_getstripe(inode, (void **)&lmm, &lmm_size,
551 GOTO(out_req, rc = lmm_size);
553 if (buf_size < lmm_size)
554 GOTO(out_req, rc = -ERANGE);
556 memcpy(buf, lmm, lmm_size);
557 GOTO(out_req, rc = lmm_size);
560 ptlrpc_req_finished(req);
568 static int ll_xattr_get(const struct xattr_handler *handler,
569 struct dentry *dentry, struct inode *inode,
570 const char *name, void *buffer, size_t size)
575 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), xattr %s\n",
576 PFID(ll_inode2fid(inode)), inode, name);
578 if (!strcmp(name, "lov")) {
579 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_GETXATTR, 1);
581 return ll_getxattr_lov(inode, buffer, size);
584 return ll_xattr_get_common(handler, dentry, inode, name, buffer, size);
587 ssize_t ll_listxattr(struct dentry *dentry, char *buffer, size_t size)
589 struct inode *inode = dentry->d_inode;
590 struct ll_sb_info *sbi = ll_i2sbi(inode);
597 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p)\n",
598 PFID(ll_inode2fid(inode)), inode);
600 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_LISTXATTR, 1);
602 rc = ll_xattr_list(inode, NULL, XATTR_OTHER_T, buffer, size,
608 * If we're being called to get the size of the xattr list
609 * (size == 0) then just assume that a lustre.lov xattr
613 RETURN(rc + sizeof(XATTR_LUSTRE_LOV));
619 len = strnlen(xattr_name, rem - 1) + 1;
621 if (!xattr_type_filter(sbi, get_xattr_type(xattr_name))) {
622 /* Skip OK xattr type, leave it in buffer. */
628 * Move up remaining xattrs in buffer
629 * removing the xattr that is not OK.
631 memmove(xattr_name, xattr_name + len, rem);
635 rc2 = ll_getxattr_lov(inode, NULL, 0);
642 if (size < rc + sizeof(XATTR_LUSTRE_LOV))
645 memcpy(buffer + rc, XATTR_LUSTRE_LOV, sizeof(XATTR_LUSTRE_LOV));
647 RETURN(rc + sizeof(XATTR_LUSTRE_LOV));
650 #ifdef HAVE_XATTR_HANDLER_SIMPLIFIED
651 static int ll_xattr_get_common_4_3(const struct xattr_handler *handler,
652 struct dentry *dentry, const char *name,
653 void *buffer, size_t size)
655 return ll_xattr_get_common(handler, dentry, dentry->d_inode, name,
659 static int ll_xattr_get_4_3(const struct xattr_handler *handler,
660 struct dentry *dentry, const char *name,
661 void *buffer, size_t size)
663 return ll_xattr_get(handler, dentry, dentry->d_inode, name, buffer,
667 static int ll_xattr_set_common_4_3(const struct xattr_handler *handler,
668 struct dentry *dentry, const char *name,
669 const void *value, size_t size, int flags)
671 return ll_xattr_set_common(handler, dentry, dentry->d_inode, name,
675 static int ll_xattr_set_4_3(const struct xattr_handler *handler,
676 struct dentry *dentry, const char *name,
677 const void *value, size_t size, int flags)
679 return ll_xattr_set(handler, dentry, dentry->d_inode, name, value,
683 #elif !defined(HAVE_XATTR_HANDLER_INODE_PARAM)
684 const struct xattr_handler *get_xattr_handler(int handler_flag)
688 while (ll_xattr_handlers[i]) {
689 if (ll_xattr_handlers[i]->flags == handler_flag)
690 return ll_xattr_handlers[i];
696 static int ll_xattr_get_common_3_11(struct dentry *dentry, const char *name,
697 void *buffer, size_t size, int handler_flags)
699 const struct xattr_handler *handler = get_xattr_handler(handler_flags);
704 return ll_xattr_get_common(handler, dentry, dentry->d_inode, name,
708 static int ll_xattr_get_3_11(struct dentry *dentry, const char *name,
709 void *buffer, size_t size, int handler_flags)
711 const struct xattr_handler *handler = get_xattr_handler(handler_flags);
716 return ll_xattr_get(handler, dentry, dentry->d_inode, name, buffer,
720 static int ll_xattr_set_common_3_11(struct dentry *dentry, const char *name,
721 const void *value, size_t size, int flags,
724 const struct xattr_handler *handler = get_xattr_handler(handler_flags);
729 return ll_xattr_set_common(handler, dentry, dentry->d_inode, name,
733 static int ll_xattr_set_3_11(struct dentry *dentry, const char *name,
734 const void *value, size_t size, int flags,
737 const struct xattr_handler *handler = get_xattr_handler(handler_flags);
742 return ll_xattr_set(handler, dentry, dentry->d_inode, name, value,
747 static const struct xattr_handler ll_user_xattr_handler = {
748 .prefix = XATTR_USER_PREFIX,
749 .flags = XATTR_USER_T,
750 #if defined(HAVE_XATTR_HANDLER_SIMPLIFIED)
751 .get = ll_xattr_get_common_4_3,
752 .set = ll_xattr_set_common_4_3,
753 #elif !defined(HAVE_XATTR_HANDLER_INODE_PARAM)
754 .get = ll_xattr_get_common_3_11,
755 .set = ll_xattr_set_common_3_11,
757 .get = ll_xattr_get_common,
758 .set = ll_xattr_set_common,
762 static const struct xattr_handler ll_trusted_xattr_handler = {
763 .prefix = XATTR_TRUSTED_PREFIX,
764 .flags = XATTR_TRUSTED_T,
765 #if defined(HAVE_XATTR_HANDLER_SIMPLIFIED)
766 .get = ll_xattr_get_4_3,
767 .set = ll_xattr_set_4_3,
768 #elif !defined(HAVE_XATTR_HANDLER_INODE_PARAM)
769 .get = ll_xattr_get_3_11,
770 .set = ll_xattr_set_3_11,
777 static const struct xattr_handler ll_security_xattr_handler = {
778 .prefix = XATTR_SECURITY_PREFIX,
779 .flags = XATTR_SECURITY_T,
780 #if defined(HAVE_XATTR_HANDLER_SIMPLIFIED)
781 .get = ll_xattr_get_common_4_3,
782 .set = ll_xattr_set_common_4_3,
783 #elif !defined(HAVE_XATTR_HANDLER_INODE_PARAM)
784 .get = ll_xattr_get_common_3_11,
785 .set = ll_xattr_set_common_3_11,
787 .get = ll_xattr_get_common,
788 .set = ll_xattr_set_common,
792 static const struct xattr_handler ll_acl_access_xattr_handler = {
793 #ifdef HAVE_XATTR_HANDLER_NAME
794 .name = XATTR_NAME_POSIX_ACL_ACCESS,
796 .prefix = XATTR_NAME_POSIX_ACL_ACCESS,
798 .flags = XATTR_ACL_ACCESS_T,
799 #if defined(HAVE_XATTR_HANDLER_SIMPLIFIED)
800 .get = ll_xattr_get_common_4_3,
801 .set = ll_xattr_set_common_4_3,
802 #elif !defined(HAVE_XATTR_HANDLER_INODE_PARAM)
803 .get = ll_xattr_get_common_3_11,
804 .set = ll_xattr_set_common_3_11,
806 .get = ll_xattr_get_common,
807 .set = ll_xattr_set_common,
811 static const struct xattr_handler ll_acl_default_xattr_handler = {
812 #ifdef HAVE_XATTR_HANDLER_NAME
813 .name = XATTR_NAME_POSIX_ACL_DEFAULT,
815 .prefix = XATTR_NAME_POSIX_ACL_DEFAULT,
817 .flags = XATTR_ACL_DEFAULT_T,
818 #if defined(HAVE_XATTR_HANDLER_SIMPLIFIED)
819 .get = ll_xattr_get_common_4_3,
820 .set = ll_xattr_set_common_4_3,
821 #elif !defined(HAVE_XATTR_HANDLER_INODE_PARAM)
822 .get = ll_xattr_get_common_3_11,
823 .set = ll_xattr_set_common_3_11,
825 .get = ll_xattr_get_common,
826 .set = ll_xattr_set_common,
830 static const struct xattr_handler ll_lustre_xattr_handler = {
831 .prefix = XATTR_LUSTRE_PREFIX,
832 .flags = XATTR_LUSTRE_T,
833 #if defined(HAVE_XATTR_HANDLER_SIMPLIFIED)
834 .get = ll_xattr_get_4_3,
835 .set = ll_xattr_set_4_3,
836 #elif !defined(HAVE_XATTR_HANDLER_INODE_PARAM)
837 .get = ll_xattr_get_3_11,
838 .set = ll_xattr_set_3_11,
845 const struct xattr_handler *ll_xattr_handlers[] = {
846 &ll_user_xattr_handler,
847 &ll_trusted_xattr_handler,
848 &ll_security_xattr_handler,
849 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
850 &ll_acl_access_xattr_handler,
851 &ll_acl_default_xattr_handler,
853 &ll_lustre_xattr_handler,