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, 2016, 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_ver.h>
44 #include <lustre_eacl.h>
46 #include "llite_internal.h"
48 const struct xattr_handler *get_xattr_type(const char *name)
52 while (ll_xattr_handlers[i]) {
53 size_t len = strlen(ll_xattr_handlers[i]->prefix);
55 if (!strncmp(ll_xattr_handlers[i]->prefix, name, len))
56 return ll_xattr_handlers[i];
62 static int xattr_type_filter(struct ll_sb_info *sbi,
63 const struct xattr_handler *handler)
65 /* No handler means XATTR_OTHER_T */
69 if ((handler->flags == XATTR_ACL_ACCESS_T ||
70 handler->flags == XATTR_ACL_DEFAULT_T) &&
71 !(sbi->ll_flags & LL_SBI_ACL))
74 if (handler->flags == XATTR_USER_T &&
75 !(sbi->ll_flags & LL_SBI_USER_XATTR))
78 if (handler->flags == XATTR_TRUSTED_T &&
79 !capable(CFS_CAP_SYS_ADMIN))
85 static int ll_xattr_set_common(const struct xattr_handler *handler,
86 struct dentry *dentry, struct inode *inode,
87 const char *name, const void *value, size_t size,
90 struct ll_sb_info *sbi = ll_i2sbi(inode);
91 struct ptlrpc_request *req = NULL;
92 const char *pv = value;
98 if (flags == XATTR_REPLACE) {
99 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_REMOVEXATTR, 1);
100 valid = OBD_MD_FLXATTRRM;
102 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_SETXATTR, 1);
103 valid = OBD_MD_FLXATTR;
106 /* FIXME: enable IMA when the conditions are ready */
107 if (handler->flags == XATTR_SECURITY_T &&
108 (!strcmp(name, "ima") || !strcmp(name, "evm")))
111 rc = xattr_type_filter(sbi, handler);
115 if ((handler->flags == XATTR_ACL_ACCESS_T ||
116 handler->flags == XATTR_ACL_DEFAULT_T) &&
117 #ifdef HAVE_INODE_OWNER_OR_CAPABLE
118 !inode_owner_or_capable(inode))
120 !is_owner_or_cap(inode))
124 /* b10667: ignore lustre special xattr for now */
125 if (!strcmp(name, "hsm") ||
126 ((handler->flags == XATTR_TRUSTED_T && !strcmp(name, "lov")) ||
127 (handler->flags == XATTR_LUSTRE_T && !strcmp(name, "lov"))))
130 /* LU-549: Disable security.selinux when selinux is disabled */
131 if (handler->flags == XATTR_SECURITY_T && !selinux_is_enabled() &&
132 strcmp(name, "selinux") == 0)
136 * In user.* namespace, only regular files and directories can have
137 * extended attributes.
139 if (handler->flags == XATTR_USER_T) {
140 if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
144 fullname = kasprintf(GFP_KERNEL, "%s%s", handler->prefix, name);
148 rc = md_setxattr(sbi->ll_md_exp, ll_inode2fid(inode), valid, fullname,
149 pv, size, 0, flags, ll_i2suppgid(inode), &req);
152 if (rc == -EOPNOTSUPP && handler->flags == XATTR_USER_T) {
153 LCONSOLE_INFO("Disabling user_xattr feature because it is not supported on the server\n");
154 sbi->ll_flags &= ~LL_SBI_USER_XATTR;
159 ptlrpc_req_finished(req);
163 static int get_hsm_state(struct inode *inode, u32 *hus_states)
165 struct md_op_data *op_data;
166 struct hsm_user_state *hus;
173 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
174 LUSTRE_OPC_ANY, hus);
175 if (!IS_ERR(op_data)) {
176 rc = obd_iocontrol(LL_IOC_HSM_STATE_GET, ll_i2mdexp(inode),
177 sizeof(*op_data), op_data, NULL);
179 *hus_states = hus->hus_states;
181 CDEBUG(D_VFSTRACE, "obd_iocontrol failed. rc = %d\n",
184 ll_finish_md_op_data(op_data);
186 rc = PTR_ERR(op_data);
187 CDEBUG(D_VFSTRACE, "Could not prepare the opdata. rc = %d\n",
194 static int ll_adjust_lum(struct inode *inode, struct lov_user_md *lump)
196 struct lov_comp_md_v1 *comp_v1 = (struct lov_comp_md_v1 *)lump;
197 struct lov_user_md *v1 = lump;
198 bool need_clear_release = false;
199 bool release_checked = false;
200 bool is_composite = false;
208 if (lump->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
209 entry_count = comp_v1->lcm_entry_count;
213 for (i = 0; i < entry_count; i++) {
214 if (lump->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
217 ptr += comp_v1->lcm_entries[i].lcme_offset;
218 v1 = (struct lov_user_md *)ptr;
222 * Attributes that are saved via getxattr will always
223 * have the stripe_offset as 0. Instead, the MDS
224 * should be allowed to pick the starting OST index.
227 if (!is_composite && v1->lmm_stripe_offset == 0)
228 v1->lmm_stripe_offset = -1;
230 /* Avoid anyone directly setting the RELEASED flag. */
231 if (v1->lmm_pattern & LOV_PATTERN_F_RELEASED) {
232 if (!release_checked) {
235 rc = get_hsm_state(inode, &state);
239 if (!(state & HS_ARCHIVED))
240 need_clear_release = true;
241 release_checked = true;
243 if (need_clear_release)
244 v1->lmm_pattern ^= LOV_PATTERN_F_RELEASED;
251 static int ll_setstripe_ea(struct dentry *dentry, struct lov_user_md *lump,
254 struct inode *inode = dentry->d_inode;
258 * It is possible to set an xattr to a "" value of zero size.
259 * For this case we are going to treat it as a removal.
264 rc = ll_adjust_lum(inode, lump);
268 if (lump && S_ISREG(inode->i_mode)) {
269 u64 it_flags = FMODE_WRITE;
272 lum_size = ll_lov_user_md_size(lump);
273 if (lum_size < 0 || size < lum_size)
276 rc = ll_lov_setstripe_ea_info(inode, dentry, it_flags, lump,
279 * b=10667: ignore -EEXIST.
280 * Silently eat error on setting trusted.lov/lustre.lov
281 * attribute for platforms that added the default option
282 * to copy all attributes in 'cp' command. Both rsync and
283 * tar --xattrs also will try to set LOVEA for existing
288 } else if (S_ISDIR(inode->i_mode)) {
289 if (size != 0 && size < sizeof(struct lov_user_md))
292 rc = ll_dir_setstripe(inode, lump, 0);
298 static int ll_xattr_set(const struct xattr_handler *handler,
299 struct dentry *dentry, struct inode *inode,
300 const char *name, const void *value, size_t size,
306 CDEBUG(D_VFSTRACE, "VFS Op:inode=" DFID "(%p), xattr %s\n",
307 PFID(ll_inode2fid(inode)), inode, name);
309 /* lustre/trusted.lov.xxx would be passed through xattr API */
310 if (!strcmp(name, "lov")) {
311 int op_type = flags == XATTR_REPLACE ? LPROC_LL_REMOVEXATTR :
314 ll_stats_ops_tally(ll_i2sbi(inode), op_type, 1);
316 return ll_setstripe_ea(dentry, (struct lov_user_md *)value,
318 } else if (!strcmp(name, "lma") || !strcmp(name, "link")) {
319 int op_type = flags == XATTR_REPLACE ? LPROC_LL_REMOVEXATTR :
322 ll_stats_ops_tally(ll_i2sbi(inode), op_type, 1);
326 return ll_xattr_set_common(handler, dentry, inode, name, value, size,
330 int ll_xattr_list(struct inode *inode, const char *name, int type, void *buffer,
331 size_t size, u64 valid)
333 struct ll_inode_info *lli = ll_i2info(inode);
334 struct ll_sb_info *sbi = ll_i2sbi(inode);
335 struct ptlrpc_request *req = NULL;
336 struct mdt_body *body;
341 if (sbi->ll_xattr_cache_enabled && type != XATTR_ACL_ACCESS_T &&
342 (type != XATTR_SECURITY_T || strcmp(name, "security.selinux"))) {
343 rc = ll_xattr_cache_get(inode, name, buffer, size, valid);
345 goto getxattr_nocache;
349 /* Add "system.posix_acl_access" to the list */
350 if (lli->lli_posix_acl && valid & OBD_MD_FLXATTRLS) {
352 rc += sizeof(XATTR_NAME_ACL_ACCESS);
353 } else if (size - rc >= sizeof(XATTR_NAME_ACL_ACCESS)) {
354 memcpy(buffer + rc, XATTR_NAME_ACL_ACCESS,
355 sizeof(XATTR_NAME_ACL_ACCESS));
356 rc += sizeof(XATTR_NAME_ACL_ACCESS);
358 GOTO(out_xattr, rc = -ERANGE);
363 rc = md_getxattr(sbi->ll_md_exp, ll_inode2fid(inode),
364 valid, name, NULL, 0, size, 0, &req);
368 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
371 /* only detect the xattr size */
373 GOTO(out, rc = body->mbo_eadatasize);
375 if (size < body->mbo_eadatasize) {
376 CERROR("server bug: replied size %u > %u\n",
377 body->mbo_eadatasize, (int)size);
378 GOTO(out, rc = -ERANGE);
381 if (body->mbo_eadatasize == 0)
382 GOTO(out, rc = -ENODATA);
384 /* do not need swab xattr data */
385 xdata = req_capsule_server_sized_get(&req->rq_pill, &RMF_EADATA,
386 body->mbo_eadatasize);
388 GOTO(out, rc = -EFAULT);
390 memcpy(buffer, xdata, body->mbo_eadatasize);
391 rc = body->mbo_eadatasize;
397 if (rc == -EOPNOTSUPP && type == XATTR_USER_T) {
398 LCONSOLE_INFO("%s: disabling user_xattr feature because "
399 "it is not supported on the server: rc = %d\n",
400 ll_get_fsname(inode->i_sb, NULL, 0), rc);
401 sbi->ll_flags &= ~LL_SBI_USER_XATTR;
404 ptlrpc_req_finished(req);
408 static int ll_xattr_get_common(const struct xattr_handler *handler,
409 struct dentry *dentry,
411 const char *name, void *buffer, size_t size)
413 struct ll_sb_info *sbi = ll_i2sbi(inode);
417 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_GETXATTR, 1);
419 rc = xattr_type_filter(sbi, handler);
423 /* LU-549: Disable security.selinux when selinux is disabled */
424 if (handler->flags == XATTR_SECURITY_T && !selinux_is_enabled() &&
425 !strcmp(name, "selinux"))
428 #ifdef CONFIG_FS_POSIX_ACL
429 /* posix acl is under protection of LOOKUP lock. when calling to this,
430 * we just have path resolution to the target inode, so we have great
431 * chance that cached ACL is uptodate.
433 if (handler->flags == XATTR_ACL_ACCESS_T) {
434 struct ll_inode_info *lli = ll_i2info(inode);
435 struct posix_acl *acl;
437 spin_lock(&lli->lli_lock);
438 acl = posix_acl_dup(lli->lli_posix_acl);
439 spin_unlock(&lli->lli_lock);
444 rc = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
445 posix_acl_release(acl);
448 if (handler->flags == XATTR_ACL_DEFAULT_T && !S_ISDIR(inode->i_mode))
452 fullname = kasprintf(GFP_KERNEL, "%s%s", handler->prefix, name);
456 rc = ll_xattr_list(inode, fullname, handler->flags, buffer, size,
462 static ssize_t ll_getxattr_lov(struct inode *inode, void *buf, size_t buf_size)
466 if (S_ISREG(inode->i_mode)) {
467 struct cl_object *obj = ll_i2info(inode)->lli_clob;
468 struct cl_layout cl = {
469 .cl_buf.lb_buf = buf,
470 .cl_buf.lb_len = buf_size,
478 env = cl_env_get(&refcheck);
480 RETURN(PTR_ERR(env));
482 rc = cl_object_layout_get(env, obj, &cl);
487 GOTO(out_env, rc = -ENODATA);
494 LASSERT(buf && rc <= buf_size);
497 * Do not return layout gen for getxattr() since
498 * otherwise it would confuse tar --xattr by
499 * recognizing layout gen as stripe offset when the
500 * file is restored. See LU-2809.
502 if (((struct lov_mds_md *)buf)->lmm_magic == LOV_MAGIC_COMP_V1)
505 ((struct lov_mds_md *)buf)->lmm_layout_gen = 0;
507 cl_env_put(env, &refcheck);
510 } else if (S_ISDIR(inode->i_mode)) {
511 struct ptlrpc_request *req = NULL;
512 struct lov_mds_md *lmm = NULL;
515 rc = ll_dir_getstripe(inode, (void **)&lmm, &lmm_size,
521 GOTO(out_req, rc = lmm_size);
523 if (buf_size < lmm_size)
524 GOTO(out_req, rc = -ERANGE);
526 memcpy(buf, lmm, lmm_size);
527 GOTO(out_req, rc = lmm_size);
530 ptlrpc_req_finished(req);
538 static int ll_xattr_get(const struct xattr_handler *handler,
539 struct dentry *dentry, struct inode *inode,
540 const char *name, void *buffer, size_t size)
545 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), xattr %s\n",
546 PFID(ll_inode2fid(inode)), inode, name);
548 if (!strcmp(name, "lov")) {
549 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_GETXATTR, 1);
551 return ll_getxattr_lov(inode, buffer, size);
554 return ll_xattr_get_common(handler, dentry, inode, name, buffer, size);
557 ssize_t ll_listxattr(struct dentry *dentry, char *buffer, size_t size)
559 struct inode *inode = dentry->d_inode;
560 struct ll_sb_info *sbi = ll_i2sbi(inode);
567 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p)\n",
568 PFID(ll_inode2fid(inode)), inode);
570 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_LISTXATTR, 1);
572 rc = ll_xattr_list(inode, NULL, XATTR_OTHER_T, buffer, size,
578 * If we're being called to get the size of the xattr list
579 * (size == 0) then just assume that a lustre.lov xattr
583 RETURN(rc + sizeof(XATTR_LUSTRE_LOV));
589 len = strnlen(xattr_name, rem - 1) + 1;
591 if (!xattr_type_filter(sbi, get_xattr_type(xattr_name))) {
592 /* Skip OK xattr type, leave it in buffer. */
598 * Move up remaining xattrs in buffer
599 * removing the xattr that is not OK.
601 memmove(xattr_name, xattr_name + len, rem);
605 rc2 = ll_getxattr_lov(inode, NULL, 0);
612 if (size < rc + sizeof(XATTR_LUSTRE_LOV))
615 memcpy(buffer + rc, XATTR_LUSTRE_LOV, sizeof(XATTR_LUSTRE_LOV));
617 RETURN(rc + sizeof(XATTR_LUSTRE_LOV));
620 #ifdef HAVE_XATTR_HANDLER_SIMPLIFIED
621 static int ll_xattr_get_common_4_3(const struct xattr_handler *handler,
622 struct dentry *dentry, const char *name,
623 void *buffer, size_t size)
625 return ll_xattr_get_common(handler, dentry, dentry->d_inode, name,
629 static int ll_xattr_get_4_3(const struct xattr_handler *handler,
630 struct dentry *dentry, const char *name,
631 void *buffer, size_t size)
633 return ll_xattr_get(handler, dentry, dentry->d_inode, name, buffer,
637 static int ll_xattr_set_common_4_3(const struct xattr_handler *handler,
638 struct dentry *dentry, const char *name,
639 const void *value, size_t size, int flags)
641 return ll_xattr_set_common(handler, dentry, dentry->d_inode, name,
645 static int ll_xattr_set_4_3(const struct xattr_handler *handler,
646 struct dentry *dentry, const char *name,
647 const void *value, size_t size, int flags)
649 return ll_xattr_set(handler, dentry, dentry->d_inode, name, value,
653 #elif !defined(HAVE_XATTR_HANDLER_INODE_PARAM)
654 const struct xattr_handler *get_xattr_handler(int handler_flag)
658 while (ll_xattr_handlers[i]) {
659 if (ll_xattr_handlers[i]->flags == handler_flag)
660 return ll_xattr_handlers[i];
666 static int ll_xattr_get_common_3_11(struct dentry *dentry, const char *name,
667 void *buffer, size_t size, int handler_flags)
669 const struct xattr_handler *handler = get_xattr_handler(handler_flags);
674 return ll_xattr_get_common(handler, dentry, dentry->d_inode, name,
678 static int ll_xattr_get_3_11(struct dentry *dentry, const char *name,
679 void *buffer, size_t size, int handler_flags)
681 const struct xattr_handler *handler = get_xattr_handler(handler_flags);
686 return ll_xattr_get(handler, dentry, dentry->d_inode, name, buffer,
690 static int ll_xattr_set_common_3_11(struct dentry *dentry, const char *name,
691 const void *value, size_t size, int flags,
694 const struct xattr_handler *handler = get_xattr_handler(handler_flags);
699 return ll_xattr_set_common(handler, dentry, dentry->d_inode, name,
703 static int ll_xattr_set_3_11(struct dentry *dentry, const char *name,
704 const void *value, size_t size, int flags,
707 const struct xattr_handler *handler = get_xattr_handler(handler_flags);
712 return ll_xattr_set(handler, dentry, dentry->d_inode, name, value,
717 static const struct xattr_handler ll_user_xattr_handler = {
718 .prefix = XATTR_USER_PREFIX,
719 .flags = XATTR_USER_T,
720 #if defined(HAVE_XATTR_HANDLER_SIMPLIFIED)
721 .get = ll_xattr_get_common_4_3,
722 .set = ll_xattr_set_common_4_3,
723 #elif !defined(HAVE_XATTR_HANDLER_INODE_PARAM)
724 .get = ll_xattr_get_common_3_11,
725 .set = ll_xattr_set_common_3_11,
727 .get = ll_xattr_get_common,
728 .set = ll_xattr_set_common,
732 static const struct xattr_handler ll_trusted_xattr_handler = {
733 .prefix = XATTR_TRUSTED_PREFIX,
734 .flags = XATTR_TRUSTED_T,
735 #if defined(HAVE_XATTR_HANDLER_SIMPLIFIED)
736 .get = ll_xattr_get_4_3,
737 .set = ll_xattr_set_4_3,
738 #elif !defined(HAVE_XATTR_HANDLER_INODE_PARAM)
739 .get = ll_xattr_get_3_11,
740 .set = ll_xattr_set_3_11,
747 static const struct xattr_handler ll_security_xattr_handler = {
748 .prefix = XATTR_SECURITY_PREFIX,
749 .flags = XATTR_SECURITY_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_acl_access_xattr_handler = {
763 .prefix = XATTR_NAME_POSIX_ACL_ACCESS,
764 .flags = XATTR_ACL_ACCESS_T,
765 #if defined(HAVE_XATTR_HANDLER_SIMPLIFIED)
766 .get = ll_xattr_get_common_4_3,
767 .set = ll_xattr_set_common_4_3,
768 #elif !defined(HAVE_XATTR_HANDLER_INODE_PARAM)
769 .get = ll_xattr_get_common_3_11,
770 .set = ll_xattr_set_common_3_11,
772 .get = ll_xattr_get_common,
773 .set = ll_xattr_set_common,
777 static const struct xattr_handler ll_acl_default_xattr_handler = {
778 .prefix = XATTR_NAME_POSIX_ACL_DEFAULT,
779 .flags = XATTR_ACL_DEFAULT_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_lustre_xattr_handler = {
793 .prefix = XATTR_LUSTRE_PREFIX,
794 .flags = XATTR_LUSTRE_T,
795 #if defined(HAVE_XATTR_HANDLER_SIMPLIFIED)
796 .get = ll_xattr_get_4_3,
797 .set = ll_xattr_set_4_3,
798 #elif !defined(HAVE_XATTR_HANDLER_INODE_PARAM)
799 .get = ll_xattr_get_3_11,
800 .set = ll_xattr_set_3_11,
807 const struct xattr_handler *ll_xattr_handlers[] = {
808 &ll_user_xattr_handler,
809 &ll_trusted_xattr_handler,
810 &ll_security_xattr_handler,
811 #ifdef CONFIG_FS_POSIX_ACL
812 &ll_acl_access_xattr_handler,
813 &ll_acl_default_xattr_handler,
815 &ll_lustre_xattr_handler,