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>
44 #include <lustre_swab.h>
46 #include "llite_internal.h"
48 #ifndef HAVE_XATTR_HANDLER_NAME
49 static inline const char *xattr_prefix(const struct xattr_handler *handler)
51 return handler->prefix;
55 const struct xattr_handler *get_xattr_type(const char *name)
59 for (i = 0; ll_xattr_handlers[i]; i++) {
60 const char *prefix = xattr_prefix(ll_xattr_handlers[i]);
61 size_t prefix_len = strlen(prefix);
63 if (!strncmp(prefix, name, prefix_len))
64 return ll_xattr_handlers[i];
70 static int xattr_type_filter(struct ll_sb_info *sbi,
71 const struct xattr_handler *handler)
73 /* No handler means XATTR_OTHER_T */
77 if ((handler->flags == XATTR_ACL_ACCESS_T ||
78 handler->flags == XATTR_ACL_DEFAULT_T) &&
79 !(sbi->ll_flags & LL_SBI_ACL))
82 if (handler->flags == XATTR_USER_T &&
83 !(sbi->ll_flags & LL_SBI_USER_XATTR))
86 if (handler->flags == XATTR_TRUSTED_T &&
87 !capable(CFS_CAP_SYS_ADMIN))
93 static int ll_xattr_set_common(const struct xattr_handler *handler,
94 struct dentry *dentry, struct inode *inode,
95 const char *name, const void *value, size_t size,
98 struct ll_sb_info *sbi = ll_i2sbi(inode);
99 struct ptlrpc_request *req = NULL;
100 const char *pv = value;
106 /* When setxattr() is called with a size of 0 the value is
107 * unconditionally replaced by "". When removexattr() is
108 * called we get a NULL value and XATTR_REPLACE for flags. */
109 if (!value && flags == XATTR_REPLACE) {
110 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_REMOVEXATTR, 1);
111 valid = OBD_MD_FLXATTRRM;
113 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_SETXATTR, 1);
114 valid = OBD_MD_FLXATTR;
117 /* FIXME: enable IMA when the conditions are ready */
118 if (handler->flags == XATTR_SECURITY_T &&
119 (!strcmp(name, "ima") || !strcmp(name, "evm")))
122 rc = xattr_type_filter(sbi, handler);
126 if ((handler->flags == XATTR_ACL_ACCESS_T ||
127 handler->flags == XATTR_ACL_DEFAULT_T) &&
128 !inode_owner_or_capable(inode))
131 /* b10667: ignore lustre special xattr for now */
132 if (!strcmp(name, "hsm") ||
133 ((handler->flags == XATTR_TRUSTED_T && !strcmp(name, "lov")) ||
134 (handler->flags == XATTR_LUSTRE_T && !strcmp(name, "lov"))))
137 /* LU-549: Disable security.selinux when selinux is disabled */
138 if (handler->flags == XATTR_SECURITY_T && !selinux_is_enabled() &&
139 strcmp(name, "selinux") == 0)
143 * In user.* namespace, only regular files and directories can have
144 * extended attributes.
146 if (handler->flags == XATTR_USER_T) {
147 if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
151 fullname = kasprintf(GFP_KERNEL, "%s%s", xattr_prefix(handler), name);
155 rc = md_setxattr(sbi->ll_md_exp, ll_inode2fid(inode), valid, fullname,
156 pv, size, flags, ll_i2suppgid(inode), &req);
159 if (rc == -EOPNOTSUPP && handler->flags == XATTR_USER_T) {
160 LCONSOLE_INFO("Disabling user_xattr feature because it is not supported on the server\n");
161 sbi->ll_flags &= ~LL_SBI_USER_XATTR;
166 ptlrpc_req_finished(req);
170 static int get_hsm_state(struct inode *inode, u32 *hus_states)
172 struct md_op_data *op_data;
173 struct hsm_user_state *hus;
180 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
181 LUSTRE_OPC_ANY, hus);
182 if (!IS_ERR(op_data)) {
183 rc = obd_iocontrol(LL_IOC_HSM_STATE_GET, ll_i2mdexp(inode),
184 sizeof(*op_data), op_data, NULL);
186 *hus_states = hus->hus_states;
188 CDEBUG(D_VFSTRACE, "obd_iocontrol failed. rc = %d\n",
191 ll_finish_md_op_data(op_data);
193 rc = PTR_ERR(op_data);
194 CDEBUG(D_VFSTRACE, "Could not prepare the opdata. rc = %d\n",
201 static int ll_adjust_lum(struct inode *inode, struct lov_user_md *lump)
203 struct lov_comp_md_v1 *comp_v1 = (struct lov_comp_md_v1 *)lump;
204 struct lov_user_md *v1 = lump;
205 bool need_clear_release = false;
206 bool release_checked = false;
207 bool is_composite = false;
215 if (lump->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
216 entry_count = comp_v1->lcm_entry_count;
220 for (i = 0; i < entry_count; i++) {
221 if (lump->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
224 ptr += comp_v1->lcm_entries[i].lcme_offset;
225 v1 = (struct lov_user_md *)ptr;
229 * Attributes that are saved via getxattr will always
230 * have the stripe_offset as 0. Instead, the MDS
231 * should be allowed to pick the starting OST index.
234 if (!is_composite && v1->lmm_stripe_offset == 0)
235 v1->lmm_stripe_offset = -1;
237 /* Avoid anyone directly setting the RELEASED flag. */
238 if (v1->lmm_pattern & LOV_PATTERN_F_RELEASED) {
239 if (!release_checked) {
242 rc = get_hsm_state(inode, &state);
246 if (!(state & HS_ARCHIVED))
247 need_clear_release = true;
248 release_checked = true;
250 if (need_clear_release)
251 v1->lmm_pattern ^= LOV_PATTERN_F_RELEASED;
258 static int ll_setstripe_ea(struct dentry *dentry, struct lov_user_md *lump,
261 struct inode *inode = dentry->d_inode;
265 * It is possible to set an xattr to a "" value of zero size.
266 * For this case we are going to treat it as a removal.
271 rc = ll_adjust_lum(inode, lump);
275 if (lump && S_ISREG(inode->i_mode)) {
276 u64 it_flags = FMODE_WRITE;
279 lum_size = ll_lov_user_md_size(lump);
280 if (lum_size < 0 || size < lum_size)
283 rc = ll_lov_setstripe_ea_info(inode, dentry, it_flags, lump,
286 * b=10667: ignore -EEXIST.
287 * Silently eat error on setting trusted.lov/lustre.lov
288 * attribute for platforms that added the default option
289 * to copy all attributes in 'cp' command. Both rsync and
290 * tar --xattrs also will try to set LOVEA for existing
295 } else if (S_ISDIR(inode->i_mode)) {
296 if (size != 0 && size < sizeof(struct lov_user_md))
299 rc = ll_dir_setstripe(inode, lump, 0);
305 static int ll_xattr_set(const struct xattr_handler *handler,
306 struct dentry *dentry, struct inode *inode,
307 const char *name, const void *value, size_t size,
313 CDEBUG(D_VFSTRACE, "VFS Op:inode=" DFID "(%p), xattr %s\n",
314 PFID(ll_inode2fid(inode)), inode, name);
316 /* lustre/trusted.lov.xxx would be passed through xattr API */
317 if (!strcmp(name, "lov")) {
318 int op_type = flags == XATTR_REPLACE ? LPROC_LL_REMOVEXATTR :
321 ll_stats_ops_tally(ll_i2sbi(inode), op_type, 1);
323 return ll_setstripe_ea(dentry, (struct lov_user_md *)value,
325 } else if (!strcmp(name, "lma") || !strcmp(name, "link")) {
326 int op_type = flags == XATTR_REPLACE ? LPROC_LL_REMOVEXATTR :
329 ll_stats_ops_tally(ll_i2sbi(inode), op_type, 1);
333 if (strncmp(name, "lov.", 4) == 0 &&
334 (__swab32(((struct lov_user_md *)value)->lmm_magic) &
335 le32_to_cpu(LOV_MAGIC_MASK)) == le32_to_cpu(LOV_MAGIC_MAGIC))
336 lustre_swab_lov_user_md((struct lov_user_md *)value);
338 return ll_xattr_set_common(handler, dentry, inode, name, value, size,
342 int ll_xattr_list(struct inode *inode, const char *name, int type, void *buffer,
343 size_t size, u64 valid)
345 struct ll_inode_info *lli = ll_i2info(inode);
346 struct ll_sb_info *sbi = ll_i2sbi(inode);
347 struct ptlrpc_request *req = NULL;
352 if (sbi->ll_xattr_cache_enabled && type != XATTR_ACL_ACCESS_T &&
353 (type != XATTR_SECURITY_T || strcmp(name, "security.selinux"))) {
354 rc = ll_xattr_cache_get(inode, name, buffer, size, valid);
356 goto getxattr_nocache;
360 /* Add "system.posix_acl_access" to the list */
361 if (lli->lli_posix_acl && valid & OBD_MD_FLXATTRLS) {
363 rc += sizeof(XATTR_NAME_ACL_ACCESS);
364 } else if (size - rc >= sizeof(XATTR_NAME_ACL_ACCESS)) {
365 memcpy(buffer + rc, XATTR_NAME_ACL_ACCESS,
366 sizeof(XATTR_NAME_ACL_ACCESS));
367 rc += sizeof(XATTR_NAME_ACL_ACCESS);
369 GOTO(out_xattr, rc = -ERANGE);
374 rc = md_getxattr(sbi->ll_md_exp, ll_inode2fid(inode), valid,
379 /* only detect the xattr size */
384 GOTO(out, rc = -ERANGE);
386 /* do not need swab xattr data */
387 xdata = req_capsule_server_sized_get(&req->rq_pill, &RMF_EADATA,
390 GOTO(out, rc = -EPROTO);
392 memcpy(buffer, xdata, rc);
398 if (rc == -EOPNOTSUPP && type == XATTR_USER_T) {
399 LCONSOLE_INFO("%s: disabling user_xattr feature because "
400 "it is not supported on the server: rc = %d\n",
402 sbi->ll_flags &= ~LL_SBI_USER_XATTR;
405 ptlrpc_req_finished(req);
409 static int ll_xattr_get_common(const struct xattr_handler *handler,
410 struct dentry *dentry,
412 const char *name, void *buffer, size_t size)
414 struct ll_sb_info *sbi = ll_i2sbi(inode);
420 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_GETXATTR, 1);
422 rc = xattr_type_filter(sbi, handler);
426 /* LU-549: Disable security.selinux when selinux is disabled */
427 if (handler->flags == XATTR_SECURITY_T && !selinux_is_enabled() &&
428 !strcmp(name, "selinux"))
431 #ifdef CONFIG_FS_POSIX_ACL
432 /* posix acl is under protection of LOOKUP lock. when calling to this,
433 * we just have path resolution to the target inode, so we have great
434 * chance that cached ACL is uptodate.
436 if (handler->flags == XATTR_ACL_ACCESS_T) {
437 struct ll_inode_info *lli = ll_i2info(inode);
438 struct posix_acl *acl;
440 spin_lock(&lli->lli_lock);
441 acl = posix_acl_dup(lli->lli_posix_acl);
442 spin_unlock(&lli->lli_lock);
447 rc = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
448 posix_acl_release(acl);
451 if (handler->flags == XATTR_ACL_DEFAULT_T && !S_ISDIR(inode->i_mode))
455 fullname = kasprintf(GFP_KERNEL, "%s%s", xattr_prefix(handler), name);
459 rc = ll_xattr_list(inode, fullname, handler->flags, buffer, size,
465 static ssize_t ll_getxattr_lov(struct inode *inode, void *buf, size_t buf_size)
469 if (S_ISREG(inode->i_mode)) {
470 struct cl_object *obj = ll_i2info(inode)->lli_clob;
471 struct cl_layout cl = {
472 .cl_buf.lb_buf = buf,
473 .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 magic = ((struct lov_mds_md *)buf)->lmm_magic;
507 if ((magic & __swab32(LOV_MAGIC_MAGIC)) ==
508 __swab32(LOV_MAGIC_MAGIC))
509 magic = __swab32(magic);
514 case LOV_MAGIC_SPECIFIC:
515 ((struct lov_mds_md *)buf)->lmm_layout_gen = 0;
517 case LOV_MAGIC_COMP_V1:
518 case LOV_MAGIC_FOREIGN:
521 CERROR("Invalid LOV magic %08x\n", magic);
522 GOTO(out_env, rc = -EINVAL);
526 cl_env_put(env, &refcheck);
529 } else if (S_ISDIR(inode->i_mode)) {
530 struct ptlrpc_request *req = NULL;
531 struct lov_mds_md *lmm = NULL;
534 rc = ll_dir_getstripe(inode, (void **)&lmm, &lmm_size,
540 GOTO(out_req, rc = lmm_size);
542 if (buf_size < lmm_size)
543 GOTO(out_req, rc = -ERANGE);
545 memcpy(buf, lmm, lmm_size);
546 GOTO(out_req, rc = lmm_size);
549 ptlrpc_req_finished(req);
557 static int ll_xattr_get(const struct xattr_handler *handler,
558 struct dentry *dentry, struct inode *inode,
559 const char *name, void *buffer, size_t size)
564 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), xattr %s\n",
565 PFID(ll_inode2fid(inode)), inode, name);
567 if (!strcmp(name, "lov")) {
568 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_GETXATTR, 1);
570 return ll_getxattr_lov(inode, buffer, size);
573 return ll_xattr_get_common(handler, dentry, inode, name, buffer, size);
576 ssize_t ll_listxattr(struct dentry *dentry, char *buffer, size_t size)
578 struct inode *inode = dentry->d_inode;
579 struct ll_sb_info *sbi = ll_i2sbi(inode);
586 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p)\n",
587 PFID(ll_inode2fid(inode)), inode);
589 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_LISTXATTR, 1);
591 rc = ll_xattr_list(inode, NULL, XATTR_OTHER_T, buffer, size,
597 * If we're being called to get the size of the xattr list
598 * (size == 0) then just assume that a lustre.lov xattr
602 RETURN(rc + sizeof(XATTR_LUSTRE_LOV));
608 len = strnlen(xattr_name, rem - 1) + 1;
610 if (!xattr_type_filter(sbi, get_xattr_type(xattr_name))) {
611 /* Skip OK xattr type, leave it in buffer. */
617 * Move up remaining xattrs in buffer
618 * removing the xattr that is not OK.
620 memmove(xattr_name, xattr_name + len, rem);
624 rc2 = ll_getxattr_lov(inode, NULL, 0);
631 if (size < rc + sizeof(XATTR_LUSTRE_LOV))
634 memcpy(buffer + rc, XATTR_LUSTRE_LOV, sizeof(XATTR_LUSTRE_LOV));
636 RETURN(rc + sizeof(XATTR_LUSTRE_LOV));
639 #ifdef HAVE_XATTR_HANDLER_SIMPLIFIED
640 static int ll_xattr_get_common_4_3(const struct xattr_handler *handler,
641 struct dentry *dentry, const char *name,
642 void *buffer, size_t size)
644 return ll_xattr_get_common(handler, dentry, dentry->d_inode, name,
648 static int ll_xattr_get_4_3(const struct xattr_handler *handler,
649 struct dentry *dentry, const char *name,
650 void *buffer, size_t size)
652 return ll_xattr_get(handler, dentry, dentry->d_inode, name, buffer,
656 static int ll_xattr_set_common_4_3(const struct xattr_handler *handler,
657 struct dentry *dentry, const char *name,
658 const void *value, size_t size, int flags)
660 return ll_xattr_set_common(handler, dentry, dentry->d_inode, name,
664 static int ll_xattr_set_4_3(const struct xattr_handler *handler,
665 struct dentry *dentry, const char *name,
666 const void *value, size_t size, int flags)
668 return ll_xattr_set(handler, dentry, dentry->d_inode, name, value,
672 #elif !defined(HAVE_XATTR_HANDLER_INODE_PARAM)
673 const struct xattr_handler *get_xattr_handler(int handler_flag)
677 while (ll_xattr_handlers[i]) {
678 if (ll_xattr_handlers[i]->flags == handler_flag)
679 return ll_xattr_handlers[i];
685 static int ll_xattr_get_common_3_11(struct dentry *dentry, const char *name,
686 void *buffer, size_t size, int handler_flags)
688 const struct xattr_handler *handler = get_xattr_handler(handler_flags);
693 return ll_xattr_get_common(handler, dentry, dentry->d_inode, name,
697 static int ll_xattr_get_3_11(struct dentry *dentry, const char *name,
698 void *buffer, size_t size, int handler_flags)
700 const struct xattr_handler *handler = get_xattr_handler(handler_flags);
705 return ll_xattr_get(handler, dentry, dentry->d_inode, name, buffer,
709 static int ll_xattr_set_common_3_11(struct dentry *dentry, const char *name,
710 const void *value, size_t size, int flags,
713 const struct xattr_handler *handler = get_xattr_handler(handler_flags);
718 return ll_xattr_set_common(handler, dentry, dentry->d_inode, name,
722 static int ll_xattr_set_3_11(struct dentry *dentry, const char *name,
723 const void *value, size_t size, int flags,
726 const struct xattr_handler *handler = get_xattr_handler(handler_flags);
731 return ll_xattr_set(handler, dentry, dentry->d_inode, name, value,
736 static const struct xattr_handler ll_user_xattr_handler = {
737 .prefix = XATTR_USER_PREFIX,
738 .flags = XATTR_USER_T,
739 #if defined(HAVE_XATTR_HANDLER_SIMPLIFIED)
740 .get = ll_xattr_get_common_4_3,
741 .set = ll_xattr_set_common_4_3,
742 #elif !defined(HAVE_XATTR_HANDLER_INODE_PARAM)
743 .get = ll_xattr_get_common_3_11,
744 .set = ll_xattr_set_common_3_11,
746 .get = ll_xattr_get_common,
747 .set = ll_xattr_set_common,
751 static const struct xattr_handler ll_trusted_xattr_handler = {
752 .prefix = XATTR_TRUSTED_PREFIX,
753 .flags = XATTR_TRUSTED_T,
754 #if defined(HAVE_XATTR_HANDLER_SIMPLIFIED)
755 .get = ll_xattr_get_4_3,
756 .set = ll_xattr_set_4_3,
757 #elif !defined(HAVE_XATTR_HANDLER_INODE_PARAM)
758 .get = ll_xattr_get_3_11,
759 .set = ll_xattr_set_3_11,
766 static const struct xattr_handler ll_security_xattr_handler = {
767 .prefix = XATTR_SECURITY_PREFIX,
768 .flags = XATTR_SECURITY_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_access_xattr_handler = {
782 #ifdef HAVE_XATTR_HANDLER_NAME
783 .name = XATTR_NAME_POSIX_ACL_ACCESS,
785 .prefix = XATTR_NAME_POSIX_ACL_ACCESS,
787 .flags = XATTR_ACL_ACCESS_T,
788 #if defined(HAVE_XATTR_HANDLER_SIMPLIFIED)
789 .get = ll_xattr_get_common_4_3,
790 .set = ll_xattr_set_common_4_3,
791 #elif !defined(HAVE_XATTR_HANDLER_INODE_PARAM)
792 .get = ll_xattr_get_common_3_11,
793 .set = ll_xattr_set_common_3_11,
795 .get = ll_xattr_get_common,
796 .set = ll_xattr_set_common,
800 static const struct xattr_handler ll_acl_default_xattr_handler = {
801 #ifdef HAVE_XATTR_HANDLER_NAME
802 .name = XATTR_NAME_POSIX_ACL_DEFAULT,
804 .prefix = XATTR_NAME_POSIX_ACL_DEFAULT,
806 .flags = XATTR_ACL_DEFAULT_T,
807 #if defined(HAVE_XATTR_HANDLER_SIMPLIFIED)
808 .get = ll_xattr_get_common_4_3,
809 .set = ll_xattr_set_common_4_3,
810 #elif !defined(HAVE_XATTR_HANDLER_INODE_PARAM)
811 .get = ll_xattr_get_common_3_11,
812 .set = ll_xattr_set_common_3_11,
814 .get = ll_xattr_get_common,
815 .set = ll_xattr_set_common,
819 static const struct xattr_handler ll_lustre_xattr_handler = {
820 .prefix = XATTR_LUSTRE_PREFIX,
821 .flags = XATTR_LUSTRE_T,
822 #if defined(HAVE_XATTR_HANDLER_SIMPLIFIED)
823 .get = ll_xattr_get_4_3,
824 .set = ll_xattr_set_4_3,
825 #elif !defined(HAVE_XATTR_HANDLER_INODE_PARAM)
826 .get = ll_xattr_get_3_11,
827 .set = ll_xattr_set_3_11,
834 const struct xattr_handler *ll_xattr_handlers[] = {
835 &ll_user_xattr_handler,
836 &ll_trusted_xattr_handler,
837 &ll_security_xattr_handler,
838 #ifdef CONFIG_FS_POSIX_ACL
839 &ll_acl_access_xattr_handler,
840 &ll_acl_default_xattr_handler,
842 &ll_lustre_xattr_handler,