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_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 if (flags == XATTR_REPLACE) {
98 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_REMOVEXATTR, 1);
99 valid = OBD_MD_FLXATTRRM;
101 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_SETXATTR, 1);
102 valid = OBD_MD_FLXATTR;
105 /* FIXME: enable IMA when the conditions are ready */
106 if (handler->flags == XATTR_SECURITY_T &&
107 (!strcmp(name, "ima") || !strcmp(name, "evm")))
110 rc = xattr_type_filter(sbi, handler);
114 if ((handler->flags == XATTR_ACL_ACCESS_T ||
115 handler->flags == XATTR_ACL_DEFAULT_T) &&
116 #ifdef HAVE_INODE_OWNER_OR_CAPABLE
117 !inode_owner_or_capable(inode))
119 !is_owner_or_cap(inode))
123 /* b10667: ignore lustre special xattr for now */
124 if (!strcmp(name, "hsm") ||
125 ((handler->flags == XATTR_TRUSTED_T && !strcmp(name, "lov")) ||
126 (handler->flags == XATTR_LUSTRE_T && !strcmp(name, "lov"))))
129 /* LU-549: Disable security.selinux when selinux is disabled */
130 if (handler->flags == XATTR_SECURITY_T && !selinux_is_enabled() &&
131 strcmp(name, "selinux") == 0)
135 * In user.* namespace, only regular files and directories can have
136 * extended attributes.
138 if (handler->flags == XATTR_USER_T) {
139 if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
143 fullname = kasprintf(GFP_KERNEL, "%s%s", handler->prefix, name);
147 rc = md_setxattr(sbi->ll_md_exp, ll_inode2fid(inode), valid, fullname,
148 pv, size, 0, flags, ll_i2suppgid(inode), &req);
151 if (rc == -EOPNOTSUPP && handler->flags == XATTR_USER_T) {
152 LCONSOLE_INFO("Disabling user_xattr feature because it is not supported on the server\n");
153 sbi->ll_flags &= ~LL_SBI_USER_XATTR;
158 ptlrpc_req_finished(req);
162 static int get_hsm_state(struct inode *inode, u32 *hus_states)
164 struct md_op_data *op_data;
165 struct hsm_user_state *hus;
172 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
173 LUSTRE_OPC_ANY, hus);
174 if (!IS_ERR(op_data)) {
175 rc = obd_iocontrol(LL_IOC_HSM_STATE_GET, ll_i2mdexp(inode),
176 sizeof(*op_data), op_data, NULL);
178 *hus_states = hus->hus_states;
180 CDEBUG(D_VFSTRACE, "obd_iocontrol failed. rc = %d\n",
183 ll_finish_md_op_data(op_data);
185 rc = PTR_ERR(op_data);
186 CDEBUG(D_VFSTRACE, "Could not prepare the opdata. rc = %d\n",
193 static int ll_adjust_lum(struct inode *inode, struct lov_user_md *lump)
195 struct lov_comp_md_v1 *comp_v1 = (struct lov_comp_md_v1 *)lump;
196 struct lov_user_md *v1 = lump;
197 bool need_clear_release = false;
198 bool release_checked = false;
199 bool is_composite = false;
207 if (lump->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
208 entry_count = comp_v1->lcm_entry_count;
212 for (i = 0; i < entry_count; i++) {
213 if (lump->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
216 ptr += comp_v1->lcm_entries[i].lcme_offset;
217 v1 = (struct lov_user_md *)ptr;
221 * Attributes that are saved via getxattr will always
222 * have the stripe_offset as 0. Instead, the MDS
223 * should be allowed to pick the starting OST index.
226 if (!is_composite && v1->lmm_stripe_offset == 0)
227 v1->lmm_stripe_offset = -1;
229 /* Avoid anyone directly setting the RELEASED flag. */
230 if (v1->lmm_pattern & LOV_PATTERN_F_RELEASED) {
231 if (!release_checked) {
234 rc = get_hsm_state(inode, &state);
238 if (!(state & HS_ARCHIVED))
239 need_clear_release = true;
240 release_checked = true;
242 if (need_clear_release)
243 v1->lmm_pattern ^= LOV_PATTERN_F_RELEASED;
250 static int ll_setstripe_ea(struct dentry *dentry, struct lov_user_md *lump,
253 struct inode *inode = dentry->d_inode;
257 * It is possible to set an xattr to a "" value of zero size.
258 * For this case we are going to treat it as a removal.
263 rc = ll_adjust_lum(inode, lump);
267 if (lump && S_ISREG(inode->i_mode)) {
268 u64 it_flags = FMODE_WRITE;
271 lum_size = ll_lov_user_md_size(lump);
272 if (lum_size < 0 || size < lum_size)
275 rc = ll_lov_setstripe_ea_info(inode, dentry, it_flags, lump,
278 * b=10667: ignore -EEXIST.
279 * Silently eat error on setting trusted.lov/lustre.lov
280 * attribute for platforms that added the default option
281 * to copy all attributes in 'cp' command. Both rsync and
282 * tar --xattrs also will try to set LOVEA for existing
287 } else if (S_ISDIR(inode->i_mode)) {
288 if (size != 0 && size < sizeof(struct lov_user_md))
291 rc = ll_dir_setstripe(inode, lump, 0);
297 static int ll_xattr_set(const struct xattr_handler *handler,
298 struct dentry *dentry, struct inode *inode,
299 const char *name, const void *value, size_t size,
305 CDEBUG(D_VFSTRACE, "VFS Op:inode=" DFID "(%p), xattr %s\n",
306 PFID(ll_inode2fid(inode)), inode, name);
308 /* lustre/trusted.lov.xxx would be passed through xattr API */
309 if (!strcmp(name, "lov")) {
310 int op_type = flags == XATTR_REPLACE ? LPROC_LL_REMOVEXATTR :
313 ll_stats_ops_tally(ll_i2sbi(inode), op_type, 1);
315 return ll_setstripe_ea(dentry, (struct lov_user_md *)value,
317 } else if (!strcmp(name, "lma") || !strcmp(name, "link")) {
318 int op_type = flags == XATTR_REPLACE ? LPROC_LL_REMOVEXATTR :
321 ll_stats_ops_tally(ll_i2sbi(inode), op_type, 1);
325 return ll_xattr_set_common(handler, dentry, inode, name, value, size,
329 int ll_xattr_list(struct inode *inode, const char *name, int type, void *buffer,
330 size_t size, u64 valid)
332 struct ll_inode_info *lli = ll_i2info(inode);
333 struct ll_sb_info *sbi = ll_i2sbi(inode);
334 struct ptlrpc_request *req = NULL;
335 struct mdt_body *body;
340 if (sbi->ll_xattr_cache_enabled && type != XATTR_ACL_ACCESS_T &&
341 (type != XATTR_SECURITY_T || strcmp(name, "security.selinux"))) {
342 rc = ll_xattr_cache_get(inode, name, buffer, size, valid);
344 goto getxattr_nocache;
348 /* Add "system.posix_acl_access" to the list */
349 if (lli->lli_posix_acl && valid & OBD_MD_FLXATTRLS) {
351 rc += sizeof(XATTR_NAME_ACL_ACCESS);
352 } else if (size - rc >= sizeof(XATTR_NAME_ACL_ACCESS)) {
353 memcpy(buffer + rc, XATTR_NAME_ACL_ACCESS,
354 sizeof(XATTR_NAME_ACL_ACCESS));
355 rc += sizeof(XATTR_NAME_ACL_ACCESS);
357 GOTO(out_xattr, rc = -ERANGE);
362 rc = md_getxattr(sbi->ll_md_exp, ll_inode2fid(inode),
363 valid, name, NULL, 0, size, 0, &req);
367 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
370 /* only detect the xattr size */
372 GOTO(out, rc = body->mbo_eadatasize);
374 if (size < body->mbo_eadatasize) {
375 CERROR("server bug: replied size %u > %u\n",
376 body->mbo_eadatasize, (int)size);
377 GOTO(out, rc = -ERANGE);
380 if (body->mbo_eadatasize == 0)
381 GOTO(out, rc = -ENODATA);
383 /* do not need swab xattr data */
384 xdata = req_capsule_server_sized_get(&req->rq_pill, &RMF_EADATA,
385 body->mbo_eadatasize);
387 GOTO(out, rc = -EFAULT);
389 memcpy(buffer, xdata, body->mbo_eadatasize);
390 rc = body->mbo_eadatasize;
396 if (rc == -EOPNOTSUPP && type == XATTR_USER_T) {
397 LCONSOLE_INFO("%s: disabling user_xattr feature because "
398 "it is not supported on the server: rc = %d\n",
399 ll_get_fsname(inode->i_sb, NULL, 0), rc);
400 sbi->ll_flags &= ~LL_SBI_USER_XATTR;
403 ptlrpc_req_finished(req);
407 static int ll_xattr_get_common(const struct xattr_handler *handler,
408 struct dentry *dentry,
410 const char *name, void *buffer, size_t size)
412 struct ll_sb_info *sbi = ll_i2sbi(inode);
418 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_GETXATTR, 1);
420 rc = xattr_type_filter(sbi, handler);
424 /* LU-549: Disable security.selinux when selinux is disabled */
425 if (handler->flags == XATTR_SECURITY_T && !selinux_is_enabled() &&
426 !strcmp(name, "selinux"))
429 #ifdef CONFIG_FS_POSIX_ACL
430 /* posix acl is under protection of LOOKUP lock. when calling to this,
431 * we just have path resolution to the target inode, so we have great
432 * chance that cached ACL is uptodate.
434 if (handler->flags == XATTR_ACL_ACCESS_T) {
435 struct ll_inode_info *lli = ll_i2info(inode);
436 struct posix_acl *acl;
438 spin_lock(&lli->lli_lock);
439 acl = posix_acl_dup(lli->lli_posix_acl);
440 spin_unlock(&lli->lli_lock);
445 rc = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
446 posix_acl_release(acl);
449 if (handler->flags == XATTR_ACL_DEFAULT_T && !S_ISDIR(inode->i_mode))
453 fullname = kasprintf(GFP_KERNEL, "%s%s", handler->prefix, name);
457 rc = ll_xattr_list(inode, fullname, handler->flags, buffer, size,
463 static ssize_t ll_getxattr_lov(struct inode *inode, void *buf, size_t buf_size)
467 if (S_ISREG(inode->i_mode)) {
468 struct cl_object *obj = ll_i2info(inode)->lli_clob;
469 struct cl_layout cl = {
470 .cl_buf.lb_buf = buf,
471 .cl_buf.lb_len = buf_size,
479 env = cl_env_get(&refcheck);
481 RETURN(PTR_ERR(env));
483 rc = cl_object_layout_get(env, obj, &cl);
488 GOTO(out_env, rc = -ENODATA);
495 LASSERT(buf && rc <= buf_size);
498 * Do not return layout gen for getxattr() since
499 * otherwise it would confuse tar --xattr by
500 * recognizing layout gen as stripe offset when the
501 * file is restored. See LU-2809.
503 if (((struct lov_mds_md *)buf)->lmm_magic == LOV_MAGIC_COMP_V1)
506 ((struct lov_mds_md *)buf)->lmm_layout_gen = 0;
508 cl_env_put(env, &refcheck);
511 } else if (S_ISDIR(inode->i_mode)) {
512 struct ptlrpc_request *req = NULL;
513 struct lov_mds_md *lmm = NULL;
516 rc = ll_dir_getstripe(inode, (void **)&lmm, &lmm_size,
522 GOTO(out_req, rc = lmm_size);
524 if (buf_size < lmm_size)
525 GOTO(out_req, rc = -ERANGE);
527 memcpy(buf, lmm, lmm_size);
528 GOTO(out_req, rc = lmm_size);
531 ptlrpc_req_finished(req);
539 static int ll_xattr_get(const struct xattr_handler *handler,
540 struct dentry *dentry, struct inode *inode,
541 const char *name, void *buffer, size_t size)
546 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), xattr %s\n",
547 PFID(ll_inode2fid(inode)), inode, name);
549 if (!strcmp(name, "lov")) {
550 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_GETXATTR, 1);
552 return ll_getxattr_lov(inode, buffer, size);
555 return ll_xattr_get_common(handler, dentry, inode, name, buffer, size);
558 ssize_t ll_listxattr(struct dentry *dentry, char *buffer, size_t size)
560 struct inode *inode = dentry->d_inode;
561 struct ll_sb_info *sbi = ll_i2sbi(inode);
568 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p)\n",
569 PFID(ll_inode2fid(inode)), inode);
571 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_LISTXATTR, 1);
573 rc = ll_xattr_list(inode, NULL, XATTR_OTHER_T, buffer, size,
579 * If we're being called to get the size of the xattr list
580 * (size == 0) then just assume that a lustre.lov xattr
584 RETURN(rc + sizeof(XATTR_LUSTRE_LOV));
590 len = strnlen(xattr_name, rem - 1) + 1;
592 if (!xattr_type_filter(sbi, get_xattr_type(xattr_name))) {
593 /* Skip OK xattr type, leave it in buffer. */
599 * Move up remaining xattrs in buffer
600 * removing the xattr that is not OK.
602 memmove(xattr_name, xattr_name + len, rem);
606 rc2 = ll_getxattr_lov(inode, NULL, 0);
613 if (size < rc + sizeof(XATTR_LUSTRE_LOV))
616 memcpy(buffer + rc, XATTR_LUSTRE_LOV, sizeof(XATTR_LUSTRE_LOV));
618 RETURN(rc + sizeof(XATTR_LUSTRE_LOV));
621 #ifdef HAVE_XATTR_HANDLER_SIMPLIFIED
622 static int ll_xattr_get_common_4_3(const struct xattr_handler *handler,
623 struct dentry *dentry, const char *name,
624 void *buffer, size_t size)
626 return ll_xattr_get_common(handler, dentry, dentry->d_inode, name,
630 static int ll_xattr_get_4_3(const struct xattr_handler *handler,
631 struct dentry *dentry, const char *name,
632 void *buffer, size_t size)
634 return ll_xattr_get(handler, dentry, dentry->d_inode, name, buffer,
638 static int ll_xattr_set_common_4_3(const struct xattr_handler *handler,
639 struct dentry *dentry, const char *name,
640 const void *value, size_t size, int flags)
642 return ll_xattr_set_common(handler, dentry, dentry->d_inode, name,
646 static int ll_xattr_set_4_3(const struct xattr_handler *handler,
647 struct dentry *dentry, const char *name,
648 const void *value, size_t size, int flags)
650 return ll_xattr_set(handler, dentry, dentry->d_inode, name, value,
654 #elif !defined(HAVE_XATTR_HANDLER_INODE_PARAM)
655 const struct xattr_handler *get_xattr_handler(int handler_flag)
659 while (ll_xattr_handlers[i]) {
660 if (ll_xattr_handlers[i]->flags == handler_flag)
661 return ll_xattr_handlers[i];
667 static int ll_xattr_get_common_3_11(struct dentry *dentry, const char *name,
668 void *buffer, size_t size, int handler_flags)
670 const struct xattr_handler *handler = get_xattr_handler(handler_flags);
675 return ll_xattr_get_common(handler, dentry, dentry->d_inode, name,
679 static int ll_xattr_get_3_11(struct dentry *dentry, const char *name,
680 void *buffer, size_t size, int handler_flags)
682 const struct xattr_handler *handler = get_xattr_handler(handler_flags);
687 return ll_xattr_get(handler, dentry, dentry->d_inode, name, buffer,
691 static int ll_xattr_set_common_3_11(struct dentry *dentry, const char *name,
692 const void *value, size_t size, int flags,
695 const struct xattr_handler *handler = get_xattr_handler(handler_flags);
700 return ll_xattr_set_common(handler, dentry, dentry->d_inode, name,
704 static int ll_xattr_set_3_11(struct dentry *dentry, const char *name,
705 const void *value, size_t size, int flags,
708 const struct xattr_handler *handler = get_xattr_handler(handler_flags);
713 return ll_xattr_set(handler, dentry, dentry->d_inode, name, value,
718 static const struct xattr_handler ll_user_xattr_handler = {
719 .prefix = XATTR_USER_PREFIX,
720 .flags = XATTR_USER_T,
721 #if defined(HAVE_XATTR_HANDLER_SIMPLIFIED)
722 .get = ll_xattr_get_common_4_3,
723 .set = ll_xattr_set_common_4_3,
724 #elif !defined(HAVE_XATTR_HANDLER_INODE_PARAM)
725 .get = ll_xattr_get_common_3_11,
726 .set = ll_xattr_set_common_3_11,
728 .get = ll_xattr_get_common,
729 .set = ll_xattr_set_common,
733 static const struct xattr_handler ll_trusted_xattr_handler = {
734 .prefix = XATTR_TRUSTED_PREFIX,
735 .flags = XATTR_TRUSTED_T,
736 #if defined(HAVE_XATTR_HANDLER_SIMPLIFIED)
737 .get = ll_xattr_get_4_3,
738 .set = ll_xattr_set_4_3,
739 #elif !defined(HAVE_XATTR_HANDLER_INODE_PARAM)
740 .get = ll_xattr_get_3_11,
741 .set = ll_xattr_set_3_11,
748 static const struct xattr_handler ll_security_xattr_handler = {
749 .prefix = XATTR_SECURITY_PREFIX,
750 .flags = XATTR_SECURITY_T,
751 #if defined(HAVE_XATTR_HANDLER_SIMPLIFIED)
752 .get = ll_xattr_get_common_4_3,
753 .set = ll_xattr_set_common_4_3,
754 #elif !defined(HAVE_XATTR_HANDLER_INODE_PARAM)
755 .get = ll_xattr_get_common_3_11,
756 .set = ll_xattr_set_common_3_11,
758 .get = ll_xattr_get_common,
759 .set = ll_xattr_set_common,
763 static const struct xattr_handler ll_acl_access_xattr_handler = {
764 .prefix = XATTR_NAME_POSIX_ACL_ACCESS,
765 .flags = XATTR_ACL_ACCESS_T,
766 #if defined(HAVE_XATTR_HANDLER_SIMPLIFIED)
767 .get = ll_xattr_get_common_4_3,
768 .set = ll_xattr_set_common_4_3,
769 #elif !defined(HAVE_XATTR_HANDLER_INODE_PARAM)
770 .get = ll_xattr_get_common_3_11,
771 .set = ll_xattr_set_common_3_11,
773 .get = ll_xattr_get_common,
774 .set = ll_xattr_set_common,
778 static const struct xattr_handler ll_acl_default_xattr_handler = {
779 .prefix = XATTR_NAME_POSIX_ACL_DEFAULT,
780 .flags = XATTR_ACL_DEFAULT_T,
781 #if defined(HAVE_XATTR_HANDLER_SIMPLIFIED)
782 .get = ll_xattr_get_common_4_3,
783 .set = ll_xattr_set_common_4_3,
784 #elif !defined(HAVE_XATTR_HANDLER_INODE_PARAM)
785 .get = ll_xattr_get_common_3_11,
786 .set = ll_xattr_set_common_3_11,
788 .get = ll_xattr_get_common,
789 .set = ll_xattr_set_common,
793 static const struct xattr_handler ll_lustre_xattr_handler = {
794 .prefix = XATTR_LUSTRE_PREFIX,
795 .flags = XATTR_LUSTRE_T,
796 #if defined(HAVE_XATTR_HANDLER_SIMPLIFIED)
797 .get = ll_xattr_get_4_3,
798 .set = ll_xattr_set_4_3,
799 #elif !defined(HAVE_XATTR_HANDLER_INODE_PARAM)
800 .get = ll_xattr_get_3_11,
801 .set = ll_xattr_set_3_11,
808 const struct xattr_handler *ll_xattr_handlers[] = {
809 &ll_user_xattr_handler,
810 &ll_trusted_xattr_handler,
811 &ll_security_xattr_handler,
812 #ifdef CONFIG_FS_POSIX_ACL
813 &ll_acl_access_xattr_handler,
814 &ll_acl_default_xattr_handler,
816 &ll_lustre_xattr_handler,