1 // SPDX-License-Identifier: GPL-2.0
5 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 only,
9 * as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License version 2 for more details (a copy is included
15 * in the LICENSE file that accompanied this code).
17 * You should have received a copy of the GNU General Public License
18 * version 2 along with this program; If not, see
19 * http://www.gnu.org/licenses/gpl-2.0.html
24 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
25 * Use is subject to license terms.
27 * Copyright (c) 2011, 2015, Intel Corporation.
30 * This file is part of Lustre, http://www.lustre.org/
31 * Lustre is a trademark of Sun Microsystems, Inc.
35 #include <linux/sched.h>
37 #include <linux/xattr.h>
38 #include <linux/selinux.h>
40 #define DEBUG_SUBSYSTEM S_LLITE
42 #include <obd_support.h>
43 #include <lustre_dlm.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(CAP_SYS_ADMIN))
85 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;
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 rc = xattr_type_filter(sbi, handler);
109 if ((handler->flags == XATTR_ACL_ACCESS_T ||
110 handler->flags == XATTR_ACL_DEFAULT_T) &&
111 !inode_owner_or_capable(inode))
114 /* b10667: ignore lustre special xattr for now */
115 if (!strcmp(name, "hsm") ||
116 ((handler->flags == XATTR_TRUSTED_T && !strcmp(name, "lov")) ||
117 (handler->flags == XATTR_LUSTRE_T && !strcmp(name, "lov"))))
120 /* LU-549: Disable security.selinux when selinux is disabled */
121 if (handler->flags == XATTR_SECURITY_T && !selinux_is_enabled() &&
122 strcmp(name, "selinux") == 0)
125 /*FIXME: enable IMA when the conditions are ready */
126 if (handler->flags == XATTR_SECURITY_T &&
127 (!strcmp(name, "ima") || !strcmp(name, "evm")))
131 * In user.* namespace, only regular files and directories can have
132 * extended attributes.
134 if (handler->flags == XATTR_USER_T) {
135 if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
139 fullname = kasprintf(GFP_KERNEL, "%s%s", handler->prefix, name);
142 rc = md_setxattr(sbi->ll_md_exp, ll_inode2fid(inode),
143 valid, fullname, pv, size, 0, flags,
144 ll_i2suppgid(inode), &req);
147 if (rc == -EOPNOTSUPP && handler->flags == XATTR_USER_T) {
148 LCONSOLE_INFO("Disabling user_xattr feature because it is not supported on the server\n");
149 sbi->ll_flags &= ~LL_SBI_USER_XATTR;
154 ptlrpc_req_finished(req);
158 static int get_hsm_state(struct inode *inode, u32 *hus_states)
160 struct md_op_data *op_data;
161 struct hsm_user_state *hus;
164 hus = kzalloc(sizeof(*hus), GFP_NOFS);
168 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
169 LUSTRE_OPC_ANY, hus);
170 if (!IS_ERR(op_data)) {
171 rc = obd_iocontrol(LL_IOC_HSM_STATE_GET, ll_i2mdexp(inode),
172 sizeof(*op_data), op_data, NULL);
174 *hus_states = hus->hus_states;
176 CDEBUG(D_VFSTRACE, "obd_iocontrol failed. rc = %d\n",
179 ll_finish_md_op_data(op_data);
181 rc = PTR_ERR(op_data);
182 CDEBUG(D_VFSTRACE, "Could not prepare the opdata. rc = %d\n",
189 static int ll_adjust_lum(struct inode *inode, struct lov_user_md *lump)
196 /* Attributes that are saved via getxattr will always have
197 * the stripe_offset as 0. Instead, the MDS should be
198 * allowed to pick the starting OST index. b=17846
200 if (lump->lmm_stripe_offset == 0)
201 lump->lmm_stripe_offset = -1;
203 /* Avoid anyone directly setting the RELEASED flag. */
204 if (lump->lmm_pattern & LOV_PATTERN_F_RELEASED) {
205 /* Only if we have a released flag check if the file
206 * was indeed archived.
210 rc = get_hsm_state(inode, &state);
214 if (!(state & HS_ARCHIVED)) {
216 "hus_states state = %x, pattern = %x\n",
217 state, lump->lmm_pattern);
219 * Here the state is: real file is not
220 * archived but user is requesting to set
221 * the RELEASED flag so we mask off the
222 * released flag from the request
224 lump->lmm_pattern ^= LOV_PATTERN_F_RELEASED;
231 static int ll_setstripe_ea(struct dentry *dentry, struct lov_user_md *lump,
234 struct inode *inode = d_inode(dentry);
238 * It is possible to set an xattr to a "" value of zero size.
239 * For this case we are going to treat it as a removal.
244 rc = ll_adjust_lum(inode, lump);
248 if (lump && S_ISREG(inode->i_mode)) {
249 __u64 it_flags = FMODE_WRITE;
252 lum_size = ll_lov_user_md_size(lump);
253 if (lum_size < 0 || size < lum_size)
256 rc = ll_lov_setstripe_ea_info(inode, dentry, it_flags, lump,
259 * b=10667: ignore -EEXIST.
260 * Silently eat error on setting trusted.lov/lustre.lov
261 * attribute for platforms that added the default option
262 * to copy all attributes in 'cp' command. Both rsync and
263 * tar --xattrs also will try to set LOVEA for existing
268 } else if (S_ISDIR(inode->i_mode)) {
269 if (size != 0 && size < sizeof(struct lov_user_md))
272 rc = ll_dir_setstripe(inode, lump, 0);
278 static int ll_xattr_set(const struct xattr_handler *handler,
279 struct dentry *dentry, struct inode *inode,
280 const char *name, const void *value, size_t size,
286 CDEBUG(D_VFSTRACE, "VFS Op:inode=" DFID "(%p), xattr %s\n",
287 PFID(ll_inode2fid(inode)), inode, name);
289 /* lustre/trusted.lov.xxx would be passed through xattr API */
290 if (!strcmp(name, "lov")) {
291 int op_type = flags == XATTR_REPLACE ? LPROC_LL_REMOVEXATTR :
294 ll_stats_ops_tally(ll_i2sbi(inode), op_type, 1);
296 return ll_setstripe_ea(dentry, (struct lov_user_md *)value,
298 } else if (!strcmp(name, "lma") || !strcmp(name, "link")) {
299 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_SETXATTR, 1);
303 return ll_xattr_set_common(handler, dentry, inode, name, value, size,
308 ll_xattr_list(struct inode *inode, const char *name, int type, void *buffer,
309 size_t size, __u64 valid)
311 struct ll_inode_info *lli = ll_i2info(inode);
312 struct ll_sb_info *sbi = ll_i2sbi(inode);
313 struct ptlrpc_request *req = NULL;
314 struct mdt_body *body;
318 if (sbi->ll_xattr_cache_enabled && type != XATTR_ACL_ACCESS_T &&
319 (type != XATTR_SECURITY_T || strcmp(name, "security.selinux"))) {
320 rc = ll_xattr_cache_get(inode, name, buffer, size, valid);
322 goto getxattr_nocache;
326 /* Add "system.posix_acl_access" to the list */
327 if (lli->lli_posix_acl && valid & OBD_MD_FLXATTRLS) {
329 rc += sizeof(XATTR_NAME_ACL_ACCESS);
330 } else if (size - rc >= sizeof(XATTR_NAME_ACL_ACCESS)) {
331 memcpy(buffer + rc, XATTR_NAME_ACL_ACCESS,
332 sizeof(XATTR_NAME_ACL_ACCESS));
333 rc += sizeof(XATTR_NAME_ACL_ACCESS);
341 rc = md_getxattr(sbi->ll_md_exp, ll_inode2fid(inode),
342 valid, name, NULL, 0, size, 0, &req);
346 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
349 /* only detect the xattr size */
351 rc = body->mbo_eadatasize;
355 if (size < body->mbo_eadatasize) {
356 CERROR("server bug: replied size %u > %u\n",
357 body->mbo_eadatasize, (int)size);
362 if (body->mbo_eadatasize == 0) {
367 /* do not need swab xattr data */
368 xdata = req_capsule_server_sized_get(&req->rq_pill, &RMF_EADATA,
369 body->mbo_eadatasize);
375 memcpy(buffer, xdata, body->mbo_eadatasize);
376 rc = body->mbo_eadatasize;
380 if (rc == -EOPNOTSUPP && type == XATTR_USER_T) {
382 "%s: disabling user_xattr feature because it is not supported on the server: rc = %d\n",
383 ll_get_fsname(inode->i_sb, NULL, 0), rc);
384 sbi->ll_flags &= ~LL_SBI_USER_XATTR;
387 ptlrpc_req_finished(req);
391 static int ll_xattr_get_common(const struct xattr_handler *handler,
392 struct dentry *dentry, struct inode *inode,
393 const char *name, void *buffer, size_t size)
395 struct ll_sb_info *sbi = ll_i2sbi(inode);
396 #ifdef CONFIG_FS_POSIX_ACL
397 struct ll_inode_info *lli = ll_i2info(inode);
402 CDEBUG(D_VFSTRACE, "VFS Op:inode=" DFID "(%p)\n",
403 PFID(ll_inode2fid(inode)), inode);
405 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_GETXATTR, 1);
407 rc = xattr_type_filter(sbi, handler);
411 /* LU-549: Disable security.selinux when selinux is disabled */
412 if (handler->flags == XATTR_SECURITY_T && !selinux_is_enabled() &&
413 !strcmp(name, "selinux"))
416 #ifdef CONFIG_FS_POSIX_ACL
417 /* posix acl is under protection of LOOKUP lock. when calling to this,
418 * we just have path resolution to the target inode, so we have great
419 * chance that cached ACL is uptodate.
421 if (handler->flags == XATTR_ACL_ACCESS_T) {
422 struct posix_acl *acl;
424 spin_lock(&lli->lli_lock);
425 acl = posix_acl_dup(lli->lli_posix_acl);
426 spin_unlock(&lli->lli_lock);
431 rc = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
432 posix_acl_release(acl);
435 if (handler->flags == XATTR_ACL_DEFAULT_T && !S_ISDIR(inode->i_mode))
438 fullname = kasprintf(GFP_KERNEL, "%s%s", handler->prefix, name);
441 rc = ll_xattr_list(inode, fullname, handler->flags, buffer, size,
447 static ssize_t ll_getxattr_lov(struct inode *inode, void *buf, size_t buf_size)
451 if (S_ISREG(inode->i_mode)) {
452 struct cl_object *obj = ll_i2info(inode)->lli_clob;
453 struct cl_layout cl = {
454 .cl_buf.lb_buf = buf,
455 .cl_buf.lb_len = buf_size,
463 env = cl_env_get(&refcheck);
467 rc = cl_object_layout_get(env, obj, &cl);
481 LASSERT(buf && rc <= buf_size);
484 * Do not return layout gen for getxattr() since
485 * otherwise it would confuse tar --xattr by
486 * recognizing layout gen as stripe offset when the
487 * file is restored. See LU-2809.
489 ((struct lov_mds_md *)buf)->lmm_layout_gen = 0;
491 cl_env_put(env, &refcheck);
494 } else if (S_ISDIR(inode->i_mode)) {
495 struct ptlrpc_request *req = NULL;
496 struct lov_mds_md *lmm = NULL;
499 rc = ll_dir_getstripe(inode, (void **)&lmm, &lmm_size,
509 if (buf_size < lmm_size) {
514 memcpy(buf, lmm, lmm_size);
518 ptlrpc_req_finished(req);
526 static int ll_xattr_get(const struct xattr_handler *handler,
527 struct dentry *dentry, struct inode *inode,
528 const char *name, void *buffer, size_t size)
533 CDEBUG(D_VFSTRACE, "VFS Op:inode=" DFID "(%p), xattr %s\n",
534 PFID(ll_inode2fid(inode)), inode, name);
536 if (!strcmp(name, "lov")) {
537 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_GETXATTR, 1);
539 return ll_getxattr_lov(inode, buffer, size);
542 return ll_xattr_get_common(handler, dentry, inode, name, buffer, size);
545 ssize_t ll_listxattr(struct dentry *dentry, char *buffer, size_t size)
547 struct inode *inode = d_inode(dentry);
548 struct ll_sb_info *sbi = ll_i2sbi(inode);
555 CDEBUG(D_VFSTRACE, "VFS Op:inode=" DFID "(%p)\n",
556 PFID(ll_inode2fid(inode)), inode);
558 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_LISTXATTR, 1);
560 rc = ll_xattr_list(inode, NULL, XATTR_OTHER_T, buffer, size,
565 * If we're being called to get the size of the xattr list
566 * (buf_size == 0) then just assume that a lustre.lov xattr
570 return rc + sizeof(XATTR_LUSTRE_LOV);
576 len = strnlen(xattr_name, rem - 1) + 1;
578 if (!xattr_type_filter(sbi, get_xattr_type(xattr_name))) {
579 /* Skip OK xattr type leave it in buffer */
585 * Move up remaining xattrs in buffer
586 * removing the xattr that is not OK
588 memmove(xattr_name, xattr_name + len, rem);
592 rc2 = ll_getxattr_lov(inode, NULL, 0);
599 if (size < rc + sizeof(XATTR_LUSTRE_LOV))
602 memcpy(buffer + rc, XATTR_LUSTRE_LOV, sizeof(XATTR_LUSTRE_LOV));
604 return rc + sizeof(XATTR_LUSTRE_LOV);
607 static const struct xattr_handler ll_user_xattr_handler = {
608 .prefix = XATTR_USER_PREFIX,
609 .flags = XATTR_USER_T,
610 .get = ll_xattr_get_common,
611 .set = ll_xattr_set_common,
614 static const struct xattr_handler ll_trusted_xattr_handler = {
615 .prefix = XATTR_TRUSTED_PREFIX,
616 .flags = XATTR_TRUSTED_T,
621 static const struct xattr_handler ll_security_xattr_handler = {
622 .prefix = XATTR_SECURITY_PREFIX,
623 .flags = XATTR_SECURITY_T,
624 .get = ll_xattr_get_common,
625 .set = ll_xattr_set_common,
628 static const struct xattr_handler ll_acl_access_xattr_handler = {
629 .prefix = XATTR_NAME_POSIX_ACL_ACCESS,
630 .flags = XATTR_ACL_ACCESS_T,
631 .get = ll_xattr_get_common,
632 .set = ll_xattr_set_common,
635 static const struct xattr_handler ll_acl_default_xattr_handler = {
636 .prefix = XATTR_NAME_POSIX_ACL_DEFAULT,
637 .flags = XATTR_ACL_DEFAULT_T,
638 .get = ll_xattr_get_common,
639 .set = ll_xattr_set_common,
642 static const struct xattr_handler ll_lustre_xattr_handler = {
643 .prefix = XATTR_LUSTRE_PREFIX,
644 .flags = XATTR_LUSTRE_T,
649 const struct xattr_handler *ll_xattr_handlers[] = {
650 &ll_user_xattr_handler,
651 &ll_trusted_xattr_handler,
652 &ll_security_xattr_handler,
653 #ifdef CONFIG_FS_POSIX_ACL
654 &ll_acl_access_xattr_handler,
655 &ll_acl_default_xattr_handler,
657 &ll_lustre_xattr_handler,