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, 2015, 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 /* 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"
56 #define XATTR_USER_T (1)
57 #define XATTR_TRUSTED_T (2)
58 #define XATTR_SECURITY_T (3)
59 #define XATTR_ACL_ACCESS_T (4)
60 #define XATTR_ACL_DEFAULT_T (5)
61 #define XATTR_LUSTRE_T (6)
62 #define XATTR_OTHER_T (7)
65 int get_xattr_type(const char *name)
67 if (!strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS))
68 return XATTR_ACL_ACCESS_T;
70 if (!strcmp(name, XATTR_NAME_POSIX_ACL_DEFAULT))
71 return XATTR_ACL_DEFAULT_T;
73 if (!strncmp(name, XATTR_USER_PREFIX,
74 sizeof(XATTR_USER_PREFIX) - 1))
77 if (!strncmp(name, XATTR_TRUSTED_PREFIX,
78 sizeof(XATTR_TRUSTED_PREFIX) - 1))
79 return XATTR_TRUSTED_T;
81 if (!strncmp(name, XATTR_SECURITY_PREFIX,
82 sizeof(XATTR_SECURITY_PREFIX) - 1))
83 return XATTR_SECURITY_T;
85 if (!strncmp(name, XATTR_LUSTRE_PREFIX,
86 sizeof(XATTR_LUSTRE_PREFIX) - 1))
87 return XATTR_LUSTRE_T;
93 int xattr_type_filter(struct ll_sb_info *sbi, int xattr_type)
95 if ((xattr_type == XATTR_ACL_ACCESS_T ||
96 xattr_type == XATTR_ACL_DEFAULT_T) &&
97 !(sbi->ll_flags & LL_SBI_ACL))
100 if (xattr_type == XATTR_USER_T && !(sbi->ll_flags & LL_SBI_USER_XATTR))
102 if (xattr_type == XATTR_TRUSTED_T && !cfs_capable(CFS_CAP_SYS_ADMIN))
104 if (xattr_type == XATTR_OTHER_T)
111 int ll_setxattr_common(struct inode *inode, const char *name,
112 const void *value, size_t size,
113 int flags, __u64 valid)
115 struct ll_sb_info *sbi = ll_i2sbi(inode);
116 struct ptlrpc_request *req = NULL;
118 const char *pv = value;
121 /*FIXME: enable IMA when the conditions are ready */
122 if (strncmp(name, XATTR_NAME_IMA,
123 sizeof(XATTR_NAME_IMA)) == 0 ||
124 strncmp(name, XATTR_NAME_EVM,
125 sizeof(XATTR_NAME_EVM)) == 0)
128 xattr_type = get_xattr_type(name);
129 rc = xattr_type_filter(sbi, xattr_type);
133 if ((xattr_type == XATTR_ACL_ACCESS_T ||
134 xattr_type == XATTR_ACL_DEFAULT_T) &&
135 #ifdef HAVE_INODE_OWNER_OR_CAPABLE
136 !inode_owner_or_capable(inode))
138 !is_owner_or_cap(inode))
142 /* b10667: ignore lustre special xattr for now */
143 if (strcmp(name, XATTR_NAME_HSM) == 0 ||
144 (xattr_type == XATTR_TRUSTED_T &&
145 strcmp(name, XATTR_NAME_LOV) == 0) ||
146 (xattr_type == XATTR_LUSTRE_T &&
147 strcmp(name, "lustre.lov") == 0))
150 /* b15587: ignore security.capability xattr for now */
151 if ((xattr_type == XATTR_SECURITY_T &&
152 strcmp(name, "security.capability") == 0))
155 /* LU-549: Disable security.selinux when selinux is disabled */
156 if (xattr_type == XATTR_SECURITY_T && !selinux_is_enabled() &&
157 strcmp(name, "security.selinux") == 0)
160 /* In user.* namespace, only regular files and directories can have
161 * extended attributes. */
162 if (xattr_type == XATTR_USER_T) {
163 if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
167 rc = md_setxattr(sbi->ll_md_exp, ll_inode2fid(inode), valid, name, pv,
168 size, 0, flags, ll_i2suppgid(inode), &req);
170 if (rc == -EOPNOTSUPP && xattr_type == XATTR_USER_T) {
171 LCONSOLE_INFO("Disabling user_xattr feature because "
172 "it is not supported on the server\n");
173 sbi->ll_flags &= ~LL_SBI_USER_XATTR;
178 ptlrpc_req_finished(req);
182 static int get_hsm_state(struct inode *inode, __u32 *hus_states)
184 struct md_op_data *op_data;
185 struct hsm_user_state *hus;
192 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
193 LUSTRE_OPC_ANY, hus);
194 if (!IS_ERR(op_data)) {
195 rc = obd_iocontrol(LL_IOC_HSM_STATE_GET, ll_i2mdexp(inode),
196 sizeof(*op_data), op_data, NULL);
198 *hus_states = hus->hus_states;
200 CDEBUG(D_VFSTRACE, "obd_iocontrol failed. rc = %d\n",
203 ll_finish_md_op_data(op_data);
205 rc = PTR_ERR(op_data);
206 CDEBUG(D_VFSTRACE, "Could not prepare the opdata. rc = %d\n",
213 int ll_setxattr(struct dentry *dentry, const char *name,
214 const void *value, size_t size, int flags)
216 struct inode *inode = dentry->d_inode;
221 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), xattr %s\n",
222 PFID(ll_inode2fid(inode)), inode, name);
224 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_SETXATTR, 1);
226 if ((strncmp(name, XATTR_TRUSTED_PREFIX,
227 sizeof(XATTR_TRUSTED_PREFIX) - 1) == 0 &&
228 strcmp(name + sizeof(XATTR_TRUSTED_PREFIX) - 1, "lov") == 0) ||
229 (strncmp(name, XATTR_LUSTRE_PREFIX,
230 sizeof(XATTR_LUSTRE_PREFIX) - 1) == 0 &&
231 strcmp(name + sizeof(XATTR_LUSTRE_PREFIX) - 1, "lov") == 0)) {
232 struct lov_user_md *lump = (struct lov_user_md *)value;
235 /* Attributes that are saved via getxattr will always have
236 * the stripe_offset as 0. Instead, the MDS should be
237 * allowed to pick the starting OST index. b=17846 */
238 if (lump != NULL && lump->lmm_stripe_offset == 0)
239 lump->lmm_stripe_offset = -1;
240 /* Avoid anyone directly setting the RELEASED flag. */
242 (lump->lmm_pattern & LOV_PATTERN_F_RELEASED)) {
243 /* Only if we have a released flag check if the file
244 * was indeed archived. */
245 __u32 state = HS_NONE;
246 rc = get_hsm_state(inode, &state);
249 if (!(state & HS_ARCHIVED)) {
251 "hus_states state = %x, pattern = %x\n",
252 state, lump->lmm_pattern);
253 /* Here the state is: real file is not
254 * archived but user is requesting to set
255 * the RELEASED flag so we mask off the
256 * released flag from the request */
257 lump->lmm_pattern ^= LOV_PATTERN_F_RELEASED;
261 if (lump != NULL && S_ISREG(inode->i_mode)) {
263 __u64 it_flags = FMODE_WRITE;
266 lum_size = ll_lov_user_md_size(lump);
267 if (lum_size < 0 || size < lum_size)
268 return 0; /* b=10667: ignore error */
270 memset(&f, 0, sizeof(f)); /* f.f_flags is used below */
271 f.f_path.dentry = dentry;
272 rc = ll_lov_setstripe_ea_info(inode, &f, it_flags, lump,
274 /* b=10667: rc always be 0 here for now */
276 } else if (S_ISDIR(inode->i_mode)) {
277 rc = ll_dir_setstripe(inode, lump, 0);
282 } else if (strcmp(name, XATTR_NAME_LMA) == 0 ||
283 strcmp(name, XATTR_NAME_LINK) == 0)
286 return ll_setxattr_common(inode, name, value, size, flags,
290 int ll_removexattr(struct dentry *dentry, const char *name)
292 struct inode *inode = dentry->d_inode;
297 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), xattr %s\n",
298 PFID(ll_inode2fid(inode)), inode, name);
300 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_REMOVEXATTR, 1);
301 return ll_setxattr_common(inode, name, NULL, 0, 0,
305 int ll_getxattr_common(struct inode *inode, const char *name,
306 void *buffer, size_t size, __u64 valid)
308 struct ll_sb_info *sbi = ll_i2sbi(inode);
309 struct ptlrpc_request *req = NULL;
310 struct mdt_body *body;
313 struct ll_inode_info *lli = ll_i2info(inode);
316 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p)\n",
317 PFID(ll_inode2fid(inode)), inode);
319 /* listxattr have slightly different behavior from of ext3:
320 * without 'user_xattr' ext3 will list all xattr names but
321 * filtered out "^user..*"; we list them all for simplicity.
324 xattr_type = XATTR_OTHER_T;
328 xattr_type = get_xattr_type(name);
329 rc = xattr_type_filter(sbi, xattr_type);
333 /* b15587: ignore security.capability xattr for now */
334 if ((xattr_type == XATTR_SECURITY_T &&
335 strcmp(name, "security.capability") == 0))
338 /* LU-549: Disable security.selinux when selinux is disabled */
339 if (xattr_type == XATTR_SECURITY_T && !selinux_is_enabled() &&
340 strcmp(name, "security.selinux") == 0)
343 #ifdef CONFIG_FS_POSIX_ACL
344 /* posix acl is under protection of LOOKUP lock. when calling to this,
345 * we just have path resolution to the target inode, so we have great
346 * chance that cached ACL is uptodate.
348 if (xattr_type == XATTR_ACL_ACCESS_T) {
349 struct posix_acl *acl;
351 spin_lock(&lli->lli_lock);
352 acl = posix_acl_dup(lli->lli_posix_acl);
353 spin_unlock(&lli->lli_lock);
358 rc = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
359 posix_acl_release(acl);
362 if (xattr_type == XATTR_ACL_DEFAULT_T && !S_ISDIR(inode->i_mode))
367 if (sbi->ll_xattr_cache_enabled &&
368 xattr_type != XATTR_ACL_ACCESS_T &&
369 (xattr_type != XATTR_SECURITY_T ||
370 strcmp(name, "security.selinux") != 0)) {
371 rc = ll_xattr_cache_get(inode, name, buffer, size, valid);
373 goto getxattr_nocache;
377 /* Add "system.posix_acl_access" to the list */
378 if (lli->lli_posix_acl != NULL && valid & OBD_MD_FLXATTRLS) {
380 rc += sizeof(XATTR_NAME_ACL_ACCESS);
381 } else if (size - rc >= sizeof(XATTR_NAME_ACL_ACCESS)) {
382 memcpy(buffer + rc, XATTR_NAME_ACL_ACCESS,
383 sizeof(XATTR_NAME_ACL_ACCESS));
384 rc += sizeof(XATTR_NAME_ACL_ACCESS);
386 GOTO(out_xattr, rc = -ERANGE);
391 rc = md_getxattr(sbi->ll_md_exp, ll_inode2fid(inode),
392 valid, name, NULL, 0, size, 0, &req);
397 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
400 /* only detect the xattr size */
402 GOTO(out, rc = body->mbo_eadatasize);
404 if (size < body->mbo_eadatasize) {
405 CERROR("server bug: replied size %u > %u\n",
406 body->mbo_eadatasize, (int)size);
407 GOTO(out, rc = -ERANGE);
410 if (body->mbo_eadatasize == 0)
411 GOTO(out, rc = -ENODATA);
413 /* do not need swab xattr data */
414 xdata = req_capsule_server_sized_get(&req->rq_pill, &RMF_EADATA,
415 body->mbo_eadatasize);
417 GOTO(out, rc = -EFAULT);
419 memcpy(buffer, xdata, body->mbo_eadatasize);
420 rc = body->mbo_eadatasize;
426 if (rc == -EOPNOTSUPP && xattr_type == XATTR_USER_T) {
427 LCONSOLE_INFO("%s: disabling user_xattr feature because "
428 "it is not supported on the server: rc = %d\n",
429 ll_get_fsname(inode->i_sb, NULL, 0), rc);
430 sbi->ll_flags &= ~LL_SBI_USER_XATTR;
433 ptlrpc_req_finished(req);
437 static ssize_t ll_getxattr_lov(struct inode *inode, void *buf, size_t buf_size)
441 if (S_ISREG(inode->i_mode)) {
442 struct cl_object *obj = ll_i2info(inode)->lli_clob;
444 struct cl_layout cl = {
445 .cl_buf.lb_buf = buf,
446 .cl_buf.lb_len = buf_size,
453 env = cl_env_get(&refcheck);
455 RETURN(PTR_ERR(env));
457 rc = cl_object_layout_get(env, obj, &cl);
462 GOTO(out_env, rc = -ENODATA);
469 LASSERT(buf != NULL && rc <= buf_size);
471 /* Do not return layout gen for getxattr() since
472 * otherwise it would confuse tar --xattr by
473 * recognizing layout gen as stripe offset when the
474 * file is restored. See LU-2809. */
475 ((struct lov_mds_md *)buf)->lmm_layout_gen = 0;
477 cl_env_put(env, &refcheck);
480 } else if (S_ISDIR(inode->i_mode)) {
481 struct lov_mds_md *lmm = NULL;
483 struct ptlrpc_request *req = NULL;
485 rc = ll_dir_getstripe(inode, (void **)&lmm, &lmm_size,
491 GOTO(out_req, rc = lmm_size);
493 if (buf_size < lmm_size)
494 GOTO(out_req, rc = -ERANGE);
496 memcpy(buf, lmm, lmm_size);
497 GOTO(out_req, rc = lmm_size);
500 ptlrpc_req_finished(req);
508 ssize_t ll_getxattr(struct dentry *dentry, const char *name, void *buf,
511 struct inode *inode = dentry->d_inode;
516 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), xattr %s\n",
517 PFID(ll_inode2fid(inode)), inode, name);
519 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_GETXATTR, 1);
521 if (strcmp(name, XATTR_LUSTRE_LOV) == 0 ||
522 strcmp(name, XATTR_NAME_LOV) == 0)
523 return ll_getxattr_lov(inode, buf, buf_size);
525 return ll_getxattr_common(inode, name, buf, buf_size,
529 ssize_t ll_listxattr(struct dentry *dentry, char *buf, size_t buf_size)
531 struct inode *inode = dentry->d_inode;
532 struct ll_sb_info *sbi = ll_i2sbi(inode);
537 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p)\n",
538 PFID(ll_inode2fid(inode)), inode);
540 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_LISTXATTR, 1);
542 rc = ll_getxattr_common(inode, NULL, buf, buf_size, OBD_MD_FLXATTRLS);
546 /* If we're being called to get the size of the xattr list
547 * (buf_size == 0) then just assume that a lustre.lov xattr
550 RETURN(rc + sizeof(XATTR_LUSTRE_LOV));
556 len = strnlen(xattr_name, rem - 1) + 1;
558 if (xattr_type_filter(sbi, get_xattr_type(xattr_name)) == 0) {
559 /* Skip OK xattr type, leave it in buffer. */
564 /* Move up remaining xattrs in buffer removing the
565 * xattr that is not OK. */
566 memmove(xattr_name, xattr_name + len, rem);
570 rc2 = ll_getxattr_lov(inode, NULL, 0);
577 if (buf_size < rc + sizeof(XATTR_LUSTRE_LOV))
580 memcpy(buf + rc, XATTR_LUSTRE_LOV, sizeof(XATTR_LUSTRE_LOV));
582 RETURN(rc + sizeof(XATTR_LUSTRE_LOV));