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 #ifndef HAVE_XATTR_HANDLER_NAME
48 static inline const char *xattr_prefix(const struct xattr_handler *handler)
50 return handler->prefix;
54 const struct xattr_handler *get_xattr_type(const char *name)
58 for (i = 0; ll_xattr_handlers[i]; i++) {
59 const char *prefix = xattr_prefix(ll_xattr_handlers[i]);
60 size_t prefix_len = strlen(prefix);
62 if (!strncmp(prefix, name, prefix_len))
63 return ll_xattr_handlers[i];
69 static int xattr_type_filter(struct ll_sb_info *sbi,
70 const struct xattr_handler *handler)
72 /* No handler means XATTR_OTHER_T */
76 if ((handler->flags == XATTR_ACL_ACCESS_T ||
77 handler->flags == XATTR_ACL_DEFAULT_T) &&
78 !(sbi->ll_flags & LL_SBI_ACL))
81 if (handler->flags == XATTR_USER_T &&
82 !(sbi->ll_flags & LL_SBI_USER_XATTR))
85 if (handler->flags == XATTR_TRUSTED_T &&
86 !capable(CFS_CAP_SYS_ADMIN))
92 static int ll_xattr_set_common(const struct xattr_handler *handler,
93 struct dentry *dentry, struct inode *inode,
94 const char *name, const void *value, size_t size,
97 struct ll_sb_info *sbi = ll_i2sbi(inode);
98 struct ptlrpc_request *req = NULL;
99 const char *pv = value;
105 /* When setxattr() is called with a size of 0 the value is
106 * unconditionally replaced by "". When removexattr() is
107 * called we get a NULL value and XATTR_REPLACE for flags. */
108 if (!value && flags == XATTR_REPLACE) {
109 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_REMOVEXATTR, 1);
110 valid = OBD_MD_FLXATTRRM;
112 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_SETXATTR, 1);
113 valid = OBD_MD_FLXATTR;
116 /* FIXME: enable IMA when the conditions are ready */
117 if (handler->flags == XATTR_SECURITY_T &&
118 (!strcmp(name, "ima") || !strcmp(name, "evm")))
121 rc = xattr_type_filter(sbi, handler);
125 if ((handler->flags == XATTR_ACL_ACCESS_T ||
126 handler->flags == XATTR_ACL_DEFAULT_T) &&
127 #ifdef HAVE_INODE_OWNER_OR_CAPABLE
128 !inode_owner_or_capable(inode))
130 !is_owner_or_cap(inode))
134 /* b10667: ignore lustre special xattr for now */
135 if (!strcmp(name, "hsm") ||
136 ((handler->flags == XATTR_TRUSTED_T && !strcmp(name, "lov")) ||
137 (handler->flags == XATTR_LUSTRE_T && !strcmp(name, "lov"))))
140 /* LU-549: Disable security.selinux when selinux is disabled */
141 if (handler->flags == XATTR_SECURITY_T && !selinux_is_enabled() &&
142 strcmp(name, "selinux") == 0)
146 * In user.* namespace, only regular files and directories can have
147 * extended attributes.
149 if (handler->flags == XATTR_USER_T) {
150 if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
154 fullname = kasprintf(GFP_KERNEL, "%s%s", xattr_prefix(handler), name);
158 rc = md_setxattr(sbi->ll_md_exp, ll_inode2fid(inode), valid, fullname,
159 pv, size, flags, ll_i2suppgid(inode), &req);
162 if (rc == -EOPNOTSUPP && handler->flags == XATTR_USER_T) {
163 LCONSOLE_INFO("Disabling user_xattr feature because it is not supported on the server\n");
164 sbi->ll_flags &= ~LL_SBI_USER_XATTR;
169 ptlrpc_req_finished(req);
173 static int get_hsm_state(struct inode *inode, u32 *hus_states)
175 struct md_op_data *op_data;
176 struct hsm_user_state *hus;
183 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
184 LUSTRE_OPC_ANY, hus);
185 if (!IS_ERR(op_data)) {
186 rc = obd_iocontrol(LL_IOC_HSM_STATE_GET, ll_i2mdexp(inode),
187 sizeof(*op_data), op_data, NULL);
189 *hus_states = hus->hus_states;
191 CDEBUG(D_VFSTRACE, "obd_iocontrol failed. rc = %d\n",
194 ll_finish_md_op_data(op_data);
196 rc = PTR_ERR(op_data);
197 CDEBUG(D_VFSTRACE, "Could not prepare the opdata. rc = %d\n",
204 static int ll_adjust_lum(struct inode *inode, struct lov_user_md *lump)
206 struct lov_comp_md_v1 *comp_v1 = (struct lov_comp_md_v1 *)lump;
207 struct lov_user_md *v1 = lump;
208 bool need_clear_release = false;
209 bool release_checked = false;
210 bool is_composite = false;
218 if (lump->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
219 entry_count = comp_v1->lcm_entry_count;
223 for (i = 0; i < entry_count; i++) {
224 if (lump->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
227 ptr += comp_v1->lcm_entries[i].lcme_offset;
228 v1 = (struct lov_user_md *)ptr;
232 * Attributes that are saved via getxattr will always
233 * have the stripe_offset as 0. Instead, the MDS
234 * should be allowed to pick the starting OST index.
237 if (!is_composite && v1->lmm_stripe_offset == 0)
238 v1->lmm_stripe_offset = -1;
240 /* Avoid anyone directly setting the RELEASED flag. */
241 if (v1->lmm_pattern & LOV_PATTERN_F_RELEASED) {
242 if (!release_checked) {
245 rc = get_hsm_state(inode, &state);
249 if (!(state & HS_ARCHIVED))
250 need_clear_release = true;
251 release_checked = true;
253 if (need_clear_release)
254 v1->lmm_pattern ^= LOV_PATTERN_F_RELEASED;
261 static int ll_setstripe_ea(struct dentry *dentry, struct lov_user_md *lump,
264 struct inode *inode = dentry->d_inode;
268 * It is possible to set an xattr to a "" value of zero size.
269 * For this case we are going to treat it as a removal.
274 rc = ll_adjust_lum(inode, lump);
278 if (lump && S_ISREG(inode->i_mode)) {
279 u64 it_flags = FMODE_WRITE;
282 lum_size = ll_lov_user_md_size(lump);
283 if (lum_size < 0 || size < lum_size)
286 rc = ll_lov_setstripe_ea_info(inode, dentry, it_flags, lump,
289 * b=10667: ignore -EEXIST.
290 * Silently eat error on setting trusted.lov/lustre.lov
291 * attribute for platforms that added the default option
292 * to copy all attributes in 'cp' command. Both rsync and
293 * tar --xattrs also will try to set LOVEA for existing
298 } else if (S_ISDIR(inode->i_mode)) {
299 if (size != 0 && size < sizeof(struct lov_user_md))
302 rc = ll_dir_setstripe(inode, lump, 0);
308 static int ll_xattr_set(const struct xattr_handler *handler,
309 struct dentry *dentry, struct inode *inode,
310 const char *name, const void *value, size_t size,
316 CDEBUG(D_VFSTRACE, "VFS Op:inode=" DFID "(%p), xattr %s\n",
317 PFID(ll_inode2fid(inode)), inode, name);
319 /* lustre/trusted.lov.xxx would be passed through xattr API */
320 if (!strcmp(name, "lov")) {
321 int op_type = flags == XATTR_REPLACE ? LPROC_LL_REMOVEXATTR :
324 ll_stats_ops_tally(ll_i2sbi(inode), op_type, 1);
326 return ll_setstripe_ea(dentry, (struct lov_user_md *)value,
328 } else if (!strcmp(name, "lma") || !strcmp(name, "link")) {
329 int op_type = flags == XATTR_REPLACE ? LPROC_LL_REMOVEXATTR :
332 ll_stats_ops_tally(ll_i2sbi(inode), op_type, 1);
336 return ll_xattr_set_common(handler, dentry, inode, name, value, size,
340 int ll_xattr_list(struct inode *inode, const char *name, int type, void *buffer,
341 size_t size, u64 valid)
343 struct ll_inode_info *lli = ll_i2info(inode);
344 struct ll_sb_info *sbi = ll_i2sbi(inode);
345 struct ptlrpc_request *req = NULL;
346 struct mdt_body *body;
351 if (sbi->ll_xattr_cache_enabled && type != XATTR_ACL_ACCESS_T &&
352 (type != XATTR_SECURITY_T || strcmp(name, "security.selinux"))) {
353 rc = ll_xattr_cache_get(inode, name, buffer, size, valid);
355 goto getxattr_nocache;
359 /* Add "system.posix_acl_access" to the list */
360 if (lli->lli_posix_acl && valid & OBD_MD_FLXATTRLS) {
362 rc += sizeof(XATTR_NAME_ACL_ACCESS);
363 } else if (size - rc >= sizeof(XATTR_NAME_ACL_ACCESS)) {
364 memcpy(buffer + rc, XATTR_NAME_ACL_ACCESS,
365 sizeof(XATTR_NAME_ACL_ACCESS));
366 rc += sizeof(XATTR_NAME_ACL_ACCESS);
368 GOTO(out_xattr, rc = -ERANGE);
373 rc = md_getxattr(sbi->ll_md_exp, ll_inode2fid(inode), valid,
378 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
381 /* only detect the xattr size */
383 /* LU-11109: Older MDTs do not distinguish
384 * between nonexistent xattrs and zero length
385 * values in this case. Newer MDTs will return
386 * -ENODATA or set OBD_MD_FLXATTR. */
387 GOTO(out, rc = body->mbo_eadatasize);
390 if (size < body->mbo_eadatasize) {
391 CERROR("server bug: replied size %u > %u\n",
392 body->mbo_eadatasize, (int)size);
393 GOTO(out, rc = -ERANGE);
396 if (body->mbo_eadatasize == 0) {
397 /* LU-11109: Newer MDTs set OBD_MD_FLXATTR on
398 * success so that we can distinguish between
399 * zero length value and nonexistent xattr.
401 * If OBD_MD_FLXATTR is not set then we keep
402 * the old behavior and return -ENODATA for
403 * getxattr() when mbo_eadatasize is 0. But
404 * -ENODATA only makes sense for getxattr()
405 * and not for listxattr(). */
406 if (body->mbo_valid & OBD_MD_FLXATTR)
408 else if (valid == OBD_MD_FLXATTR)
409 GOTO(out, rc = -ENODATA);
414 /* do not need swab xattr data */
415 xdata = req_capsule_server_sized_get(&req->rq_pill, &RMF_EADATA,
416 body->mbo_eadatasize);
418 GOTO(out, rc = -EFAULT);
420 memcpy(buffer, xdata, body->mbo_eadatasize);
421 rc = body->mbo_eadatasize;
427 if (rc == -EOPNOTSUPP && type == XATTR_USER_T) {
428 LCONSOLE_INFO("%s: disabling user_xattr feature because "
429 "it is not supported on the server: rc = %d\n",
430 ll_get_fsname(inode->i_sb, NULL, 0), rc);
431 sbi->ll_flags &= ~LL_SBI_USER_XATTR;
434 ptlrpc_req_finished(req);
438 static int ll_xattr_get_common(const struct xattr_handler *handler,
439 struct dentry *dentry,
441 const char *name, void *buffer, size_t size)
443 struct ll_sb_info *sbi = ll_i2sbi(inode);
449 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_GETXATTR, 1);
451 rc = xattr_type_filter(sbi, handler);
455 /* LU-549: Disable security.selinux when selinux is disabled */
456 if (handler->flags == XATTR_SECURITY_T && !selinux_is_enabled() &&
457 !strcmp(name, "selinux"))
460 #ifdef CONFIG_FS_POSIX_ACL
461 /* posix acl is under protection of LOOKUP lock. when calling to this,
462 * we just have path resolution to the target inode, so we have great
463 * chance that cached ACL is uptodate.
465 if (handler->flags == XATTR_ACL_ACCESS_T) {
466 struct ll_inode_info *lli = ll_i2info(inode);
467 struct posix_acl *acl;
469 spin_lock(&lli->lli_lock);
470 acl = posix_acl_dup(lli->lli_posix_acl);
471 spin_unlock(&lli->lli_lock);
476 rc = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
477 posix_acl_release(acl);
480 if (handler->flags == XATTR_ACL_DEFAULT_T && !S_ISDIR(inode->i_mode))
484 fullname = kasprintf(GFP_KERNEL, "%s%s", xattr_prefix(handler), name);
488 rc = ll_xattr_list(inode, fullname, handler->flags, buffer, size,
494 static ssize_t ll_getxattr_lov(struct inode *inode, void *buf, size_t buf_size)
498 if (S_ISREG(inode->i_mode)) {
499 struct cl_object *obj = ll_i2info(inode)->lli_clob;
500 struct cl_layout cl = {
501 .cl_buf.lb_buf = buf,
502 .cl_buf.lb_len = buf_size,
510 env = cl_env_get(&refcheck);
512 RETURN(PTR_ERR(env));
514 rc = cl_object_layout_get(env, obj, &cl);
519 GOTO(out_env, rc = -ENODATA);
526 LASSERT(buf && rc <= buf_size);
529 * Do not return layout gen for getxattr() since
530 * otherwise it would confuse tar --xattr by
531 * recognizing layout gen as stripe offset when the
532 * file is restored. See LU-2809.
534 if (((struct lov_mds_md *)buf)->lmm_magic == LOV_MAGIC_COMP_V1)
537 ((struct lov_mds_md *)buf)->lmm_layout_gen = 0;
539 cl_env_put(env, &refcheck);
542 } else if (S_ISDIR(inode->i_mode)) {
543 struct ptlrpc_request *req = NULL;
544 struct lov_mds_md *lmm = NULL;
547 rc = ll_dir_getstripe(inode, (void **)&lmm, &lmm_size,
553 GOTO(out_req, rc = lmm_size);
555 if (buf_size < lmm_size)
556 GOTO(out_req, rc = -ERANGE);
558 memcpy(buf, lmm, lmm_size);
559 GOTO(out_req, rc = lmm_size);
562 ptlrpc_req_finished(req);
570 static int ll_xattr_get(const struct xattr_handler *handler,
571 struct dentry *dentry, struct inode *inode,
572 const char *name, void *buffer, size_t size)
577 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), xattr %s\n",
578 PFID(ll_inode2fid(inode)), inode, name);
580 if (!strcmp(name, "lov")) {
581 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_GETXATTR, 1);
583 return ll_getxattr_lov(inode, buffer, size);
586 return ll_xattr_get_common(handler, dentry, inode, name, buffer, size);
589 ssize_t ll_listxattr(struct dentry *dentry, char *buffer, size_t size)
591 struct inode *inode = dentry->d_inode;
592 struct ll_sb_info *sbi = ll_i2sbi(inode);
599 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p)\n",
600 PFID(ll_inode2fid(inode)), inode);
602 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_LISTXATTR, 1);
604 rc = ll_xattr_list(inode, NULL, XATTR_OTHER_T, buffer, size,
610 * If we're being called to get the size of the xattr list
611 * (size == 0) then just assume that a lustre.lov xattr
615 RETURN(rc + sizeof(XATTR_LUSTRE_LOV));
621 len = strnlen(xattr_name, rem - 1) + 1;
623 if (!xattr_type_filter(sbi, get_xattr_type(xattr_name))) {
624 /* Skip OK xattr type, leave it in buffer. */
630 * Move up remaining xattrs in buffer
631 * removing the xattr that is not OK.
633 memmove(xattr_name, xattr_name + len, rem);
637 rc2 = ll_getxattr_lov(inode, NULL, 0);
644 if (size < rc + sizeof(XATTR_LUSTRE_LOV))
647 memcpy(buffer + rc, XATTR_LUSTRE_LOV, sizeof(XATTR_LUSTRE_LOV));
649 RETURN(rc + sizeof(XATTR_LUSTRE_LOV));
652 #ifdef HAVE_XATTR_HANDLER_SIMPLIFIED
653 static int ll_xattr_get_common_4_3(const struct xattr_handler *handler,
654 struct dentry *dentry, const char *name,
655 void *buffer, size_t size)
657 return ll_xattr_get_common(handler, dentry, dentry->d_inode, name,
661 static int ll_xattr_get_4_3(const struct xattr_handler *handler,
662 struct dentry *dentry, const char *name,
663 void *buffer, size_t size)
665 return ll_xattr_get(handler, dentry, dentry->d_inode, name, buffer,
669 static int ll_xattr_set_common_4_3(const struct xattr_handler *handler,
670 struct dentry *dentry, const char *name,
671 const void *value, size_t size, int flags)
673 return ll_xattr_set_common(handler, dentry, dentry->d_inode, name,
677 static int ll_xattr_set_4_3(const struct xattr_handler *handler,
678 struct dentry *dentry, const char *name,
679 const void *value, size_t size, int flags)
681 return ll_xattr_set(handler, dentry, dentry->d_inode, name, value,
685 #elif !defined(HAVE_XATTR_HANDLER_INODE_PARAM)
686 const struct xattr_handler *get_xattr_handler(int handler_flag)
690 while (ll_xattr_handlers[i]) {
691 if (ll_xattr_handlers[i]->flags == handler_flag)
692 return ll_xattr_handlers[i];
698 static int ll_xattr_get_common_3_11(struct dentry *dentry, const char *name,
699 void *buffer, size_t size, int handler_flags)
701 const struct xattr_handler *handler = get_xattr_handler(handler_flags);
706 return ll_xattr_get_common(handler, dentry, dentry->d_inode, name,
710 static int ll_xattr_get_3_11(struct dentry *dentry, const char *name,
711 void *buffer, size_t size, int handler_flags)
713 const struct xattr_handler *handler = get_xattr_handler(handler_flags);
718 return ll_xattr_get(handler, dentry, dentry->d_inode, name, buffer,
722 static int ll_xattr_set_common_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_common(handler, dentry, dentry->d_inode, name,
735 static int ll_xattr_set_3_11(struct dentry *dentry, const char *name,
736 const void *value, size_t size, int flags,
739 const struct xattr_handler *handler = get_xattr_handler(handler_flags);
744 return ll_xattr_set(handler, dentry, dentry->d_inode, name, value,
749 static const struct xattr_handler ll_user_xattr_handler = {
750 .prefix = XATTR_USER_PREFIX,
751 .flags = XATTR_USER_T,
752 #if defined(HAVE_XATTR_HANDLER_SIMPLIFIED)
753 .get = ll_xattr_get_common_4_3,
754 .set = ll_xattr_set_common_4_3,
755 #elif !defined(HAVE_XATTR_HANDLER_INODE_PARAM)
756 .get = ll_xattr_get_common_3_11,
757 .set = ll_xattr_set_common_3_11,
759 .get = ll_xattr_get_common,
760 .set = ll_xattr_set_common,
764 static const struct xattr_handler ll_trusted_xattr_handler = {
765 .prefix = XATTR_TRUSTED_PREFIX,
766 .flags = XATTR_TRUSTED_T,
767 #if defined(HAVE_XATTR_HANDLER_SIMPLIFIED)
768 .get = ll_xattr_get_4_3,
769 .set = ll_xattr_set_4_3,
770 #elif !defined(HAVE_XATTR_HANDLER_INODE_PARAM)
771 .get = ll_xattr_get_3_11,
772 .set = ll_xattr_set_3_11,
779 static const struct xattr_handler ll_security_xattr_handler = {
780 .prefix = XATTR_SECURITY_PREFIX,
781 .flags = XATTR_SECURITY_T,
782 #if defined(HAVE_XATTR_HANDLER_SIMPLIFIED)
783 .get = ll_xattr_get_common_4_3,
784 .set = ll_xattr_set_common_4_3,
785 #elif !defined(HAVE_XATTR_HANDLER_INODE_PARAM)
786 .get = ll_xattr_get_common_3_11,
787 .set = ll_xattr_set_common_3_11,
789 .get = ll_xattr_get_common,
790 .set = ll_xattr_set_common,
794 static const struct xattr_handler ll_acl_access_xattr_handler = {
795 #ifdef HAVE_XATTR_HANDLER_NAME
796 .name = XATTR_NAME_POSIX_ACL_ACCESS,
798 .prefix = XATTR_NAME_POSIX_ACL_ACCESS,
800 .flags = XATTR_ACL_ACCESS_T,
801 #if defined(HAVE_XATTR_HANDLER_SIMPLIFIED)
802 .get = ll_xattr_get_common_4_3,
803 .set = ll_xattr_set_common_4_3,
804 #elif !defined(HAVE_XATTR_HANDLER_INODE_PARAM)
805 .get = ll_xattr_get_common_3_11,
806 .set = ll_xattr_set_common_3_11,
808 .get = ll_xattr_get_common,
809 .set = ll_xattr_set_common,
813 static const struct xattr_handler ll_acl_default_xattr_handler = {
814 #ifdef HAVE_XATTR_HANDLER_NAME
815 .name = XATTR_NAME_POSIX_ACL_DEFAULT,
817 .prefix = XATTR_NAME_POSIX_ACL_DEFAULT,
819 .flags = XATTR_ACL_DEFAULT_T,
820 #if defined(HAVE_XATTR_HANDLER_SIMPLIFIED)
821 .get = ll_xattr_get_common_4_3,
822 .set = ll_xattr_set_common_4_3,
823 #elif !defined(HAVE_XATTR_HANDLER_INODE_PARAM)
824 .get = ll_xattr_get_common_3_11,
825 .set = ll_xattr_set_common_3_11,
827 .get = ll_xattr_get_common,
828 .set = ll_xattr_set_common,
832 static const struct xattr_handler ll_lustre_xattr_handler = {
833 .prefix = XATTR_LUSTRE_PREFIX,
834 .flags = XATTR_LUSTRE_T,
835 #if defined(HAVE_XATTR_HANDLER_SIMPLIFIED)
836 .get = ll_xattr_get_4_3,
837 .set = ll_xattr_set_4_3,
838 #elif !defined(HAVE_XATTR_HANDLER_INODE_PARAM)
839 .get = ll_xattr_get_3_11,
840 .set = ll_xattr_set_3_11,
847 const struct xattr_handler *ll_xattr_handlers[] = {
848 &ll_user_xattr_handler,
849 &ll_trusted_xattr_handler,
850 &ll_security_xattr_handler,
851 #ifdef CONFIG_FS_POSIX_ACL
852 &ll_acl_access_xattr_handler,
853 &ll_acl_default_xattr_handler,
855 &ll_lustre_xattr_handler,