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.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2015, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
38 #include <linux/sched.h>
40 #include <linux/xattr.h>
41 #include <linux/selinux.h>
43 #define DEBUG_SUBSYSTEM S_LLITE
45 #include <obd_support.h>
46 #include <lustre_dlm.h>
47 #include <lustre_ver.h>
48 #include <lustre_eacl.h>
50 #include "llite_internal.h"
52 /* xattr related to IMA(Integrity Measurement Architecture) */
53 #ifndef XATTR_NAME_IMA
54 #define XATTR_NAME_IMA "security.ima"
56 #ifndef XATTR_NAME_EVM
57 #define XATTR_NAME_EVM "security.evm"
60 #define XATTR_USER_T (1)
61 #define XATTR_TRUSTED_T (2)
62 #define XATTR_SECURITY_T (3)
63 #define XATTR_ACL_ACCESS_T (4)
64 #define XATTR_ACL_DEFAULT_T (5)
65 #define XATTR_LUSTRE_T (6)
66 #define XATTR_OTHER_T (7)
69 int get_xattr_type(const char *name)
71 if (!strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS))
72 return XATTR_ACL_ACCESS_T;
74 if (!strcmp(name, XATTR_NAME_POSIX_ACL_DEFAULT))
75 return XATTR_ACL_DEFAULT_T;
77 if (!strncmp(name, XATTR_USER_PREFIX,
78 sizeof(XATTR_USER_PREFIX) - 1))
81 if (!strncmp(name, XATTR_TRUSTED_PREFIX,
82 sizeof(XATTR_TRUSTED_PREFIX) - 1))
83 return XATTR_TRUSTED_T;
85 if (!strncmp(name, XATTR_SECURITY_PREFIX,
86 sizeof(XATTR_SECURITY_PREFIX) - 1))
87 return XATTR_SECURITY_T;
89 if (!strncmp(name, XATTR_LUSTRE_PREFIX,
90 sizeof(XATTR_LUSTRE_PREFIX) - 1))
91 return XATTR_LUSTRE_T;
97 int xattr_type_filter(struct ll_sb_info *sbi, int xattr_type)
99 if ((xattr_type == XATTR_ACL_ACCESS_T ||
100 xattr_type == XATTR_ACL_DEFAULT_T) &&
101 !(sbi->ll_flags & LL_SBI_ACL))
104 if (xattr_type == XATTR_USER_T && !(sbi->ll_flags & LL_SBI_USER_XATTR))
106 if (xattr_type == XATTR_TRUSTED_T && !cfs_capable(CFS_CAP_SYS_ADMIN))
108 if (xattr_type == XATTR_OTHER_T)
115 int ll_setxattr_common(struct inode *inode, const char *name,
116 const void *value, size_t size,
117 int flags, __u64 valid)
119 struct ll_sb_info *sbi = ll_i2sbi(inode);
120 struct ptlrpc_request *req = NULL;
122 const char *pv = value;
125 /*FIXME: enable IMA when the conditions are ready */
126 if (strncmp(name, XATTR_NAME_IMA,
127 sizeof(XATTR_NAME_IMA)) == 0 ||
128 strncmp(name, XATTR_NAME_EVM,
129 sizeof(XATTR_NAME_EVM)) == 0)
132 xattr_type = get_xattr_type(name);
133 rc = xattr_type_filter(sbi, xattr_type);
137 if ((xattr_type == XATTR_ACL_ACCESS_T ||
138 xattr_type == XATTR_ACL_DEFAULT_T) &&
139 #ifdef HAVE_INODE_OWNER_OR_CAPABLE
140 !inode_owner_or_capable(inode))
142 !is_owner_or_cap(inode))
146 /* b10667: ignore lustre special xattr for now */
147 if (strcmp(name, XATTR_NAME_HSM) == 0 ||
148 (xattr_type == XATTR_TRUSTED_T &&
149 strcmp(name, XATTR_NAME_LOV) == 0) ||
150 (xattr_type == XATTR_LUSTRE_T &&
151 strcmp(name, "lustre.lov") == 0))
154 /* b15587: ignore security.capability xattr for now */
155 if ((xattr_type == XATTR_SECURITY_T &&
156 strcmp(name, "security.capability") == 0))
159 /* LU-549: Disable security.selinux when selinux is disabled */
160 if (xattr_type == XATTR_SECURITY_T && !selinux_is_enabled() &&
161 strcmp(name, "security.selinux") == 0)
164 rc = md_setxattr(sbi->ll_md_exp, ll_inode2fid(inode), valid, name, pv,
165 size, 0, flags, ll_i2suppgid(inode), &req);
167 if (rc == -EOPNOTSUPP && xattr_type == XATTR_USER_T) {
168 LCONSOLE_INFO("Disabling user_xattr feature because "
169 "it is not supported on the server\n");
170 sbi->ll_flags &= ~LL_SBI_USER_XATTR;
175 ptlrpc_req_finished(req);
179 static int get_hsm_state(struct inode *inode, __u32 *hus_states)
181 struct md_op_data *op_data;
182 struct hsm_user_state *hus;
189 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
190 LUSTRE_OPC_ANY, hus);
191 if (!IS_ERR(op_data)) {
192 rc = obd_iocontrol(LL_IOC_HSM_STATE_GET, ll_i2mdexp(inode),
193 sizeof(*op_data), op_data, NULL);
195 *hus_states = hus->hus_states;
197 CDEBUG(D_VFSTRACE, "obd_iocontrol failed. rc = %d\n",
200 ll_finish_md_op_data(op_data);
202 rc = PTR_ERR(op_data);
203 CDEBUG(D_VFSTRACE, "Could not prepare the opdata. rc = %d\n",
210 int ll_setxattr(struct dentry *dentry, const char *name,
211 const void *value, size_t size, int flags)
213 struct inode *inode = dentry->d_inode;
218 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), xattr %s\n",
219 PFID(ll_inode2fid(inode)), inode, name);
221 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_SETXATTR, 1);
223 if ((strncmp(name, XATTR_TRUSTED_PREFIX,
224 sizeof(XATTR_TRUSTED_PREFIX) - 1) == 0 &&
225 strcmp(name + sizeof(XATTR_TRUSTED_PREFIX) - 1, "lov") == 0) ||
226 (strncmp(name, XATTR_LUSTRE_PREFIX,
227 sizeof(XATTR_LUSTRE_PREFIX) - 1) == 0 &&
228 strcmp(name + sizeof(XATTR_LUSTRE_PREFIX) - 1, "lov") == 0)) {
229 struct lov_user_md *lump = (struct lov_user_md *)value;
232 /* Attributes that are saved via getxattr will always have
233 * the stripe_offset as 0. Instead, the MDS should be
234 * allowed to pick the starting OST index. b=17846 */
235 if (lump != NULL && lump->lmm_stripe_offset == 0)
236 lump->lmm_stripe_offset = -1;
237 /* Avoid anyone directly setting the RELEASED flag. */
239 (lump->lmm_pattern & LOV_PATTERN_F_RELEASED)) {
240 /* Only if we have a released flag check if the file
241 * was indeed archived. */
242 __u32 state = HS_NONE;
243 rc = get_hsm_state(inode, &state);
246 if (!(state & HS_ARCHIVED)) {
248 "hus_states state = %x, pattern = %x\n",
249 state, lump->lmm_pattern);
250 /* Here the state is: real file is not
251 * archived but user is requesting to set
252 * the RELEASED flag so we mask off the
253 * released flag from the request */
254 lump->lmm_pattern ^= LOV_PATTERN_F_RELEASED;
258 if (lump != NULL && S_ISREG(inode->i_mode)) {
260 __u64 it_flags = FMODE_WRITE;
263 lum_size = ll_lov_user_md_size(lump);
264 if (lum_size < 0 || size < lum_size)
265 return 0; /* b=10667: ignore error */
267 memset(&f, 0, sizeof(f)); /* f.f_flags is used below */
268 f.f_path.dentry = dentry;
269 rc = ll_lov_setstripe_ea_info(inode, &f, it_flags, lump,
271 /* b=10667: rc always be 0 here for now */
273 } else if (S_ISDIR(inode->i_mode)) {
274 rc = ll_dir_setstripe(inode, lump, 0);
279 } else if (strcmp(name, XATTR_NAME_LMA) == 0 ||
280 strcmp(name, XATTR_NAME_LINK) == 0)
283 return ll_setxattr_common(inode, name, value, size, flags,
287 int ll_removexattr(struct dentry *dentry, const char *name)
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_REMOVEXATTR, 1);
298 return ll_setxattr_common(inode, name, NULL, 0, 0,
302 int ll_getxattr_common(struct inode *inode, const char *name,
303 void *buffer, size_t size, __u64 valid)
305 struct ll_sb_info *sbi = ll_i2sbi(inode);
306 struct ptlrpc_request *req = NULL;
307 struct mdt_body *body;
310 struct ll_inode_info *lli = ll_i2info(inode);
313 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p)\n",
314 PFID(ll_inode2fid(inode)), inode);
316 /* listxattr have slightly different behavior from of ext3:
317 * without 'user_xattr' ext3 will list all xattr names but
318 * filtered out "^user..*"; we list them all for simplicity.
321 xattr_type = XATTR_OTHER_T;
325 xattr_type = get_xattr_type(name);
326 rc = xattr_type_filter(sbi, xattr_type);
330 /* b15587: ignore security.capability xattr for now */
331 if ((xattr_type == XATTR_SECURITY_T &&
332 strcmp(name, "security.capability") == 0))
335 /* LU-549: Disable security.selinux when selinux is disabled */
336 if (xattr_type == XATTR_SECURITY_T && !selinux_is_enabled() &&
337 strcmp(name, "security.selinux") == 0)
340 #ifdef CONFIG_FS_POSIX_ACL
341 /* posix acl is under protection of LOOKUP lock. when calling to this,
342 * we just have path resolution to the target inode, so we have great
343 * chance that cached ACL is uptodate.
345 if (xattr_type == XATTR_ACL_ACCESS_T) {
346 struct posix_acl *acl;
348 spin_lock(&lli->lli_lock);
349 acl = posix_acl_dup(lli->lli_posix_acl);
350 spin_unlock(&lli->lli_lock);
355 rc = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
356 posix_acl_release(acl);
359 if (xattr_type == XATTR_ACL_DEFAULT_T && !S_ISDIR(inode->i_mode))
364 if (sbi->ll_xattr_cache_enabled &&
365 xattr_type != XATTR_ACL_ACCESS_T &&
366 (xattr_type != XATTR_SECURITY_T ||
367 strcmp(name, "security.selinux") != 0)) {
368 rc = ll_xattr_cache_get(inode, name, buffer, size, valid);
370 goto getxattr_nocache;
374 /* Add "system.posix_acl_access" to the list */
375 if (lli->lli_posix_acl != NULL && valid & OBD_MD_FLXATTRLS) {
377 rc += sizeof(XATTR_NAME_ACL_ACCESS);
378 } else if (size - rc >= sizeof(XATTR_NAME_ACL_ACCESS)) {
379 memcpy(buffer + rc, XATTR_NAME_ACL_ACCESS,
380 sizeof(XATTR_NAME_ACL_ACCESS));
381 rc += sizeof(XATTR_NAME_ACL_ACCESS);
383 GOTO(out_xattr, rc = -ERANGE);
388 rc = md_getxattr(sbi->ll_md_exp, ll_inode2fid(inode),
389 valid, name, NULL, 0, size, 0, &req);
394 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
397 /* only detect the xattr size */
399 GOTO(out, rc = body->mbo_eadatasize);
401 if (size < body->mbo_eadatasize) {
402 CERROR("server bug: replied size %u > %u\n",
403 body->mbo_eadatasize, (int)size);
404 GOTO(out, rc = -ERANGE);
407 if (body->mbo_eadatasize == 0)
408 GOTO(out, rc = -ENODATA);
410 /* do not need swab xattr data */
411 xdata = req_capsule_server_sized_get(&req->rq_pill, &RMF_EADATA,
412 body->mbo_eadatasize);
414 GOTO(out, rc = -EFAULT);
416 memcpy(buffer, xdata, body->mbo_eadatasize);
417 rc = body->mbo_eadatasize;
423 if (rc == -EOPNOTSUPP && xattr_type == XATTR_USER_T) {
424 LCONSOLE_INFO("%s: disabling user_xattr feature because "
425 "it is not supported on the server: rc = %d\n",
426 ll_get_fsname(inode->i_sb, NULL, 0), rc);
427 sbi->ll_flags &= ~LL_SBI_USER_XATTR;
430 ptlrpc_req_finished(req);
434 static ssize_t ll_getxattr_lov(struct inode *inode, void *buf, size_t buf_size)
438 if (S_ISREG(inode->i_mode)) {
439 struct cl_object *obj = ll_i2info(inode)->lli_clob;
441 struct cl_layout cl = {
442 .cl_buf.lb_buf = buf,
443 .cl_buf.lb_len = buf_size,
450 env = cl_env_get(&refcheck);
452 RETURN(PTR_ERR(env));
454 rc = cl_object_layout_get(env, obj, &cl);
459 GOTO(out_env, rc = -ENODATA);
466 LASSERT(buf != NULL && rc <= buf_size);
468 /* Do not return layout gen for getxattr() since
469 * otherwise it would confuse tar --xattr by
470 * recognizing layout gen as stripe offset when the
471 * file is restored. See LU-2809. */
472 ((struct lov_mds_md *)buf)->lmm_layout_gen = 0;
474 cl_env_put(env, &refcheck);
477 } else if (S_ISDIR(inode->i_mode)) {
478 struct lov_mds_md *lmm = NULL;
480 struct ptlrpc_request *req = NULL;
482 rc = ll_dir_getstripe(inode, (void **)&lmm, &lmm_size,
488 GOTO(out_req, rc = lmm_size);
490 if (buf_size < lmm_size)
491 GOTO(out_req, rc = -ERANGE);
493 memcpy(buf, lmm, lmm_size);
494 GOTO(out_req, rc = lmm_size);
497 ptlrpc_req_finished(req);
505 ssize_t ll_getxattr(struct dentry *dentry, const char *name, void *buf,
508 struct inode *inode = dentry->d_inode;
513 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), xattr %s\n",
514 PFID(ll_inode2fid(inode)), inode, name);
516 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_GETXATTR, 1);
518 if (strcmp(name, XATTR_LUSTRE_LOV) == 0 ||
519 strcmp(name, XATTR_NAME_LOV) == 0)
520 return ll_getxattr_lov(inode, buf, buf_size);
522 return ll_getxattr_common(inode, name, buf, buf_size,
526 ssize_t ll_listxattr(struct dentry *dentry, char *buf, size_t buf_size)
528 struct inode *inode = dentry->d_inode;
529 struct ll_sb_info *sbi = ll_i2sbi(inode);
534 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p)\n",
535 PFID(ll_inode2fid(inode)), inode);
537 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_LISTXATTR, 1);
539 rc = ll_getxattr_common(inode, NULL, buf, buf_size, OBD_MD_FLXATTRLS);
543 /* If we're being called to get the size of the xattr list
544 * (buf_size == 0) then just assume that a lustre.lov xattr
547 RETURN(rc + sizeof(XATTR_LUSTRE_LOV));
553 len = strnlen(xattr_name, rem - 1) + 1;
555 if (xattr_type_filter(sbi, get_xattr_type(xattr_name)) == 0) {
556 /* Skip OK xattr type, leave it in buffer. */
561 /* Move up remaining xattrs in buffer removing the
562 * xattr that is not OK. */
563 memmove(xattr_name, xattr_name + len, rem);
567 rc2 = ll_getxattr_lov(inode, NULL, 0);
574 if (buf_size < rc + sizeof(XATTR_LUSTRE_LOV))
577 memcpy(buf + rc, XATTR_LUSTRE_LOV, sizeof(XATTR_LUSTRE_LOV));
579 RETURN(rc + sizeof(XATTR_LUSTRE_LOV));