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 <uapi/linux/lustre/lustre_ver.h>
44 #include <lustre_eacl.h>
46 #include "llite_internal.h"
48 /* xattr related to IMA(Integrity Measurement Architecture) */
49 #ifndef XATTR_NAME_IMA
50 #define XATTR_NAME_IMA "security.ima"
52 #ifndef XATTR_NAME_EVM
53 #define XATTR_NAME_EVM "security.evm"
57 int get_xattr26_type(const char *name)
59 if (!strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS))
60 return XATTR_ACL_ACCESS_T;
62 if (!strcmp(name, XATTR_NAME_POSIX_ACL_DEFAULT))
63 return XATTR_ACL_DEFAULT_T;
65 if (!strncmp(name, XATTR_USER_PREFIX,
66 sizeof(XATTR_USER_PREFIX) - 1))
69 if (!strncmp(name, XATTR_TRUSTED_PREFIX,
70 sizeof(XATTR_TRUSTED_PREFIX) - 1))
71 return XATTR_TRUSTED_T;
73 if (!strncmp(name, XATTR_SECURITY_PREFIX,
74 sizeof(XATTR_SECURITY_PREFIX) - 1))
75 return XATTR_SECURITY_T;
77 if (!strncmp(name, XATTR_LUSTRE_PREFIX,
78 sizeof(XATTR_LUSTRE_PREFIX) - 1))
79 return XATTR_LUSTRE_T;
85 int xattr_type_filter(struct ll_sb_info *sbi, int xattr_type)
87 if ((xattr_type == XATTR_ACL_ACCESS_T ||
88 xattr_type == XATTR_ACL_DEFAULT_T) &&
89 !(sbi->ll_flags & LL_SBI_ACL))
92 if (xattr_type == XATTR_USER_T && !(sbi->ll_flags & LL_SBI_USER_XATTR))
94 if (xattr_type == XATTR_TRUSTED_T && !cfs_capable(CFS_CAP_SYS_ADMIN))
96 if (xattr_type == XATTR_OTHER_T)
103 int ll_setxattr_common(struct inode *inode, const char *name,
104 const void *value, size_t size,
105 int flags, __u64 valid)
107 struct ll_sb_info *sbi = ll_i2sbi(inode);
108 struct ptlrpc_request *req = NULL;
110 const char *pv = value;
113 /*FIXME: enable IMA when the conditions are ready */
114 if (strncmp(name, XATTR_NAME_IMA,
115 sizeof(XATTR_NAME_IMA)) == 0 ||
116 strncmp(name, XATTR_NAME_EVM,
117 sizeof(XATTR_NAME_EVM)) == 0)
120 xattr_type = get_xattr26_type(name);
121 rc = xattr_type_filter(sbi, xattr_type);
125 if ((xattr_type == XATTR_ACL_ACCESS_T ||
126 xattr_type == 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, XATTR_NAME_HSM) == 0 ||
136 (xattr_type == XATTR_TRUSTED_T &&
137 strcmp(name, XATTR_NAME_LOV) == 0) ||
138 (xattr_type == XATTR_LUSTRE_T &&
139 strcmp(name, "lustre.lov") == 0))
142 /* LU-549: Disable security.selinux when selinux is disabled */
143 if (xattr_type == XATTR_SECURITY_T && !selinux_is_enabled() &&
144 strcmp(name, "security.selinux") == 0)
147 /* In user.* namespace, only regular files and directories can have
148 * extended attributes. */
149 if (xattr_type == XATTR_USER_T) {
150 if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
154 rc = md_setxattr(sbi->ll_md_exp, ll_inode2fid(inode), valid, name, pv,
155 size, flags, ll_i2suppgid(inode), &req);
157 if (rc == -EOPNOTSUPP && xattr_type == XATTR_USER_T) {
158 LCONSOLE_INFO("Disabling user_xattr feature because "
159 "it is not supported on the server\n");
160 sbi->ll_flags &= ~LL_SBI_USER_XATTR;
165 ptlrpc_req_finished(req);
169 static int get_hsm_state(struct inode *inode, __u32 *hus_states)
171 struct md_op_data *op_data;
172 struct hsm_user_state *hus;
179 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
180 LUSTRE_OPC_ANY, hus);
181 if (!IS_ERR(op_data)) {
182 rc = obd_iocontrol(LL_IOC_HSM_STATE_GET, ll_i2mdexp(inode),
183 sizeof(*op_data), op_data, NULL);
185 *hus_states = hus->hus_states;
187 CDEBUG(D_VFSTRACE, "obd_iocontrol failed. rc = %d\n",
190 ll_finish_md_op_data(op_data);
192 rc = PTR_ERR(op_data);
193 CDEBUG(D_VFSTRACE, "Could not prepare the opdata. rc = %d\n",
200 static int ll_adjust_lum(struct inode *inode, struct lov_user_md *lump)
202 struct lov_comp_md_v1 *comp_v1 = (struct lov_comp_md_v1 *)lump;
203 struct lov_user_md *v1 = lump;
204 bool release_checked = false;
205 bool need_clear_release = false;
206 __u16 entry_count = 1;
207 bool is_composite = false;
214 if (lump->lmm_magic == LOV_USER_MAGIC_COMP_V1) {
215 entry_count = comp_v1->lcm_entry_count;
219 for (i = 0; i < entry_count; i++) {
220 if (lump->lmm_magic == LOV_USER_MAGIC_COMP_V1)
221 v1 = (struct lov_user_md *)((char *)comp_v1 +
222 comp_v1->lcm_entries[i].lcme_offset);
224 /* Attributes that are saved via getxattr will always
225 * have the stripe_offset as 0. Instead, the MDS
226 * should be allowed to pick the starting OST index.
228 if (!is_composite && v1->lmm_stripe_offset == 0)
229 v1->lmm_stripe_offset = -1;
231 /* Avoid anyone directly setting the RELEASED flag. */
232 if (v1->lmm_pattern & LOV_PATTERN_F_RELEASED) {
233 if (!release_checked) {
234 __u32 state = HS_NONE;
235 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;
256 rc = ll_adjust_lum(inode, lump);
260 if (lump != NULL && S_ISREG(inode->i_mode)) {
261 u64 it_flags = FMODE_WRITE;
264 lum_size = ll_lov_user_md_size(lump);
265 if (lum_size < 0 || size < lum_size)
268 rc = ll_lov_setstripe_ea_info(inode, dentry, it_flags, lump,
271 * b=10667: ignore -EEXIST.
272 * Silently eat error on setting trusted.lov/lustre.lov
273 * attribute for SuSE 9, it added default option to copy
274 * all attributes in 'cp' command. rsync, tar --xattrs
275 * also will try to set LOVEA for existing files.
279 } else if (S_ISDIR(inode->i_mode)) {
280 rc = ll_dir_setstripe(inode, lump, 0);
286 int ll_setxattr(struct dentry *dentry, const char *name,
287 const void *value, size_t size, int flags)
289 struct inode *inode = dentry->d_inode;
294 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), xattr %s\n",
295 PFID(ll_inode2fid(inode)), inode, name);
297 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_SETXATTR, 1);
299 /* lustre/trusted.lov.xxx would be passed through xattr API */
300 if (strcmp(name, XATTR_NAME_LOV) == 0 ||
301 strcmp(name, XATTR_LUSTRE_LOV) == 0)
302 return ll_setstripe_ea(dentry, (struct lov_user_md *)value,
304 else if (strcmp(name, XATTR_NAME_LMA) == 0 ||
305 strcmp(name, XATTR_NAME_LINK) == 0)
308 return ll_setxattr_common(inode, name, value, size, flags,
312 int ll_removexattr(struct dentry *dentry, const char *name)
314 struct inode *inode = dentry->d_inode;
319 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), xattr %s\n",
320 PFID(ll_inode2fid(inode)), inode, name);
322 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_REMOVEXATTR, 1);
323 return ll_setxattr_common(inode, name, NULL, 0, 0,
327 int ll_getxattr_common(struct inode *inode, const char *name,
328 void *buffer, size_t size, __u64 valid)
330 struct ll_sb_info *sbi = ll_i2sbi(inode);
331 struct ptlrpc_request *req = NULL;
334 struct ll_inode_info *lli = ll_i2info(inode);
337 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p)\n",
338 PFID(ll_inode2fid(inode)), inode);
340 /* listxattr have slightly different behavior from of ext3:
341 * without 'user_xattr' ext3 will list all xattr names but
342 * filtered out "^user..*"; we list them all for simplicity.
345 xattr_type = XATTR_OTHER_T;
349 xattr_type = get_xattr26_type(name);
350 rc = xattr_type_filter(sbi, xattr_type);
354 /* LU-549: Disable security.selinux when selinux is disabled */
355 if (xattr_type == XATTR_SECURITY_T && !selinux_is_enabled() &&
356 strcmp(name, "security.selinux") == 0)
359 #ifdef CONFIG_FS_POSIX_ACL
360 /* posix acl is under protection of LOOKUP lock. when calling to this,
361 * we just have path resolution to the target inode, so we have great
362 * chance that cached ACL is uptodate.
364 if (xattr_type == XATTR_ACL_ACCESS_T) {
365 struct posix_acl *acl;
367 spin_lock(&lli->lli_lock);
368 acl = posix_acl_dup(lli->lli_posix_acl);
369 spin_unlock(&lli->lli_lock);
374 rc = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
375 posix_acl_release(acl);
378 if (xattr_type == XATTR_ACL_DEFAULT_T && !S_ISDIR(inode->i_mode))
383 if (sbi->ll_xattr_cache_enabled &&
384 xattr_type != XATTR_ACL_ACCESS_T &&
385 (xattr_type != XATTR_SECURITY_T ||
386 strcmp(name, "security.selinux") != 0)) {
387 rc = ll_xattr_cache_get(inode, name, buffer, size, valid);
389 goto getxattr_nocache;
393 /* Add "system.posix_acl_access" to the list */
394 if (lli->lli_posix_acl != NULL && valid & OBD_MD_FLXATTRLS) {
396 rc += sizeof(XATTR_NAME_ACL_ACCESS);
397 } else if (size - rc >= sizeof(XATTR_NAME_ACL_ACCESS)) {
398 memcpy(buffer + rc, XATTR_NAME_ACL_ACCESS,
399 sizeof(XATTR_NAME_ACL_ACCESS));
400 rc += sizeof(XATTR_NAME_ACL_ACCESS);
402 GOTO(out_xattr, rc = -ERANGE);
407 rc = md_getxattr(sbi->ll_md_exp, ll_inode2fid(inode), valid,
412 /* only detect the xattr size */
417 GOTO(out, rc = -ERANGE);
419 /* do not need swab xattr data */
420 xdata = req_capsule_server_sized_get(&req->rq_pill, &RMF_EADATA,
423 GOTO(out, rc = -EPROTO);
425 memcpy(buffer, xdata, rc);
431 if (rc == -EOPNOTSUPP && xattr_type == XATTR_USER_T) {
432 LCONSOLE_INFO("%s: disabling user_xattr feature because "
433 "it is not supported on the server: rc = %d\n",
434 ll_get_fsname(inode->i_sb, NULL, 0), rc);
435 sbi->ll_flags &= ~LL_SBI_USER_XATTR;
438 ptlrpc_req_finished(req);
442 static ssize_t ll_getxattr_lov(struct inode *inode, void *buf, size_t buf_size)
446 if (S_ISREG(inode->i_mode)) {
447 struct cl_object *obj = ll_i2info(inode)->lli_clob;
449 struct cl_layout cl = {
450 .cl_buf.lb_buf = buf,
451 .cl_buf.lb_len = buf_size,
458 env = cl_env_get(&refcheck);
460 RETURN(PTR_ERR(env));
462 rc = cl_object_layout_get(env, obj, &cl);
467 GOTO(out_env, rc = -ENODATA);
474 LASSERT(buf != NULL && rc <= buf_size);
476 /* Do not return layout gen for getxattr() since
477 * otherwise it would confuse tar --xattr by
478 * recognizing layout gen as stripe offset when the
479 * file is restored. See LU-2809. */
480 if (((struct lov_mds_md *)buf)->lmm_magic == LOV_MAGIC_COMP_V1)
483 ((struct lov_mds_md *)buf)->lmm_layout_gen = 0;
485 cl_env_put(env, &refcheck);
488 } else if (S_ISDIR(inode->i_mode)) {
489 struct lov_mds_md *lmm = NULL;
491 struct ptlrpc_request *req = NULL;
493 rc = ll_dir_getstripe(inode, (void **)&lmm, &lmm_size,
499 GOTO(out_req, rc = lmm_size);
501 if (buf_size < lmm_size)
502 GOTO(out_req, rc = -ERANGE);
504 memcpy(buf, lmm, lmm_size);
505 GOTO(out_req, rc = lmm_size);
508 ptlrpc_req_finished(req);
516 ssize_t ll_getxattr(struct dentry *dentry, const char *name, void *buf,
519 struct inode *inode = dentry->d_inode;
524 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), xattr %s\n",
525 PFID(ll_inode2fid(inode)), inode, name);
527 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_GETXATTR, 1);
529 if (strcmp(name, XATTR_LUSTRE_LOV) == 0 ||
530 strcmp(name, XATTR_NAME_LOV) == 0)
531 return ll_getxattr_lov(inode, buf, buf_size);
533 return ll_getxattr_common(inode, name, buf, buf_size,
537 ssize_t ll_listxattr(struct dentry *dentry, char *buf, size_t buf_size)
539 struct inode *inode = dentry->d_inode;
540 struct ll_sb_info *sbi = ll_i2sbi(inode);
545 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p)\n",
546 PFID(ll_inode2fid(inode)), inode);
548 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_LISTXATTR, 1);
550 rc = ll_getxattr_common(inode, NULL, buf, buf_size, OBD_MD_FLXATTRLS);
554 /* If we're being called to get the size of the xattr list
555 * (buf_size == 0) then just assume that a lustre.lov xattr
558 RETURN(rc + sizeof(XATTR_LUSTRE_LOV));
564 len = strnlen(xattr_name, rem - 1) + 1;
566 if (xattr_type_filter(sbi, get_xattr26_type(xattr_name)) == 0) {
567 /* Skip OK xattr type, leave it in buffer. */
572 /* Move up remaining xattrs in buffer removing the
573 * xattr that is not OK. */
574 memmove(xattr_name, xattr_name + len, rem);
578 rc2 = ll_getxattr_lov(inode, NULL, 0);
585 if (buf_size < rc + sizeof(XATTR_LUSTRE_LOV))
588 memcpy(buf + rc, XATTR_LUSTRE_LOV, sizeof(XATTR_LUSTRE_LOV));
590 RETURN(rc + sizeof(XATTR_LUSTRE_LOV));