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 #ifndef XATTR_NAME_POSIX_ACL_ACCESS
61 # define XATTR_NAME_POSIX_ACL_ACCESS POSIX_ACL_XATTR_ACCESS
64 #ifndef XATTR_NAME_POSIX_ACL_DEFAULT
65 # define XATTR_NAME_POSIX_ACL_DEFAULT POSIX_ACL_XATTR_DEFAULT
68 #define XATTR_USER_T (1)
69 #define XATTR_TRUSTED_T (2)
70 #define XATTR_SECURITY_T (3)
71 #define XATTR_ACL_ACCESS_T (4)
72 #define XATTR_ACL_DEFAULT_T (5)
73 #define XATTR_LUSTRE_T (6)
74 #define XATTR_OTHER_T (7)
77 int get_xattr_type(const char *name)
79 if (!strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS))
80 return XATTR_ACL_ACCESS_T;
82 if (!strcmp(name, XATTR_NAME_POSIX_ACL_DEFAULT))
83 return XATTR_ACL_DEFAULT_T;
85 if (!strncmp(name, XATTR_USER_PREFIX,
86 sizeof(XATTR_USER_PREFIX) - 1))
89 if (!strncmp(name, XATTR_TRUSTED_PREFIX,
90 sizeof(XATTR_TRUSTED_PREFIX) - 1))
91 return XATTR_TRUSTED_T;
93 if (!strncmp(name, XATTR_SECURITY_PREFIX,
94 sizeof(XATTR_SECURITY_PREFIX) - 1))
95 return XATTR_SECURITY_T;
97 if (!strncmp(name, XATTR_LUSTRE_PREFIX,
98 sizeof(XATTR_LUSTRE_PREFIX) - 1))
99 return XATTR_LUSTRE_T;
101 return XATTR_OTHER_T;
105 int xattr_type_filter(struct ll_sb_info *sbi, int xattr_type)
107 if ((xattr_type == XATTR_ACL_ACCESS_T ||
108 xattr_type == XATTR_ACL_DEFAULT_T) &&
109 !(sbi->ll_flags & LL_SBI_ACL))
112 if (xattr_type == XATTR_USER_T && !(sbi->ll_flags & LL_SBI_USER_XATTR))
114 if (xattr_type == XATTR_TRUSTED_T && !cfs_capable(CFS_CAP_SYS_ADMIN))
116 if (xattr_type == XATTR_OTHER_T)
123 int ll_setxattr_common(struct inode *inode, const char *name,
124 const void *value, size_t size,
125 int flags, __u64 valid)
127 struct ll_sb_info *sbi = ll_i2sbi(inode);
128 struct ptlrpc_request *req = NULL;
130 const char *pv = value;
133 /*FIXME: enable IMA when the conditions are ready */
134 if (strncmp(name, XATTR_NAME_IMA,
135 sizeof(XATTR_NAME_IMA)) == 0 ||
136 strncmp(name, XATTR_NAME_EVM,
137 sizeof(XATTR_NAME_EVM)) == 0)
140 xattr_type = get_xattr_type(name);
141 rc = xattr_type_filter(sbi, xattr_type);
145 if ((xattr_type == XATTR_ACL_ACCESS_T ||
146 xattr_type == XATTR_ACL_DEFAULT_T) &&
147 #ifdef HAVE_INODE_OWNER_OR_CAPABLE
148 !inode_owner_or_capable(inode))
150 !is_owner_or_cap(inode))
154 /* b10667: ignore lustre special xattr for now */
155 if (strcmp(name, XATTR_NAME_HSM) == 0 ||
156 (xattr_type == XATTR_TRUSTED_T &&
157 strcmp(name, XATTR_NAME_LOV) == 0) ||
158 (xattr_type == XATTR_LUSTRE_T &&
159 strcmp(name, "lustre.lov") == 0))
162 /* b15587: ignore security.capability xattr for now */
163 if ((xattr_type == XATTR_SECURITY_T &&
164 strcmp(name, "security.capability") == 0))
167 /* LU-549: Disable security.selinux when selinux is disabled */
168 if (xattr_type == XATTR_SECURITY_T && !selinux_is_enabled() &&
169 strcmp(name, "security.selinux") == 0)
172 rc = md_setxattr(sbi->ll_md_exp, ll_inode2fid(inode), valid, name, pv,
173 size, 0, flags, ll_i2suppgid(inode), &req);
175 if (rc == -EOPNOTSUPP && xattr_type == XATTR_USER_T) {
176 LCONSOLE_INFO("Disabling user_xattr feature because "
177 "it is not supported on the server\n");
178 sbi->ll_flags &= ~LL_SBI_USER_XATTR;
183 ptlrpc_req_finished(req);
187 static int get_hsm_state(struct inode *inode, __u32 *hus_states)
189 struct md_op_data *op_data;
190 struct hsm_user_state *hus;
197 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
198 LUSTRE_OPC_ANY, hus);
199 if (!IS_ERR(op_data)) {
200 rc = obd_iocontrol(LL_IOC_HSM_STATE_GET, ll_i2mdexp(inode),
201 sizeof(*op_data), op_data, NULL);
203 *hus_states = hus->hus_states;
205 CDEBUG(D_VFSTRACE, "obd_iocontrol failed. rc = %d\n",
208 ll_finish_md_op_data(op_data);
210 rc = PTR_ERR(op_data);
211 CDEBUG(D_VFSTRACE, "Could not prepare the opdata. rc = %d\n",
218 int ll_setxattr(struct dentry *dentry, const char *name,
219 const void *value, size_t size, int flags)
221 struct inode *inode = dentry->d_inode;
226 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), xattr %s\n",
227 PFID(ll_inode2fid(inode)), inode, name);
229 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_SETXATTR, 1);
231 if ((strncmp(name, XATTR_TRUSTED_PREFIX,
232 sizeof(XATTR_TRUSTED_PREFIX) - 1) == 0 &&
233 strcmp(name + sizeof(XATTR_TRUSTED_PREFIX) - 1, "lov") == 0) ||
234 (strncmp(name, XATTR_LUSTRE_PREFIX,
235 sizeof(XATTR_LUSTRE_PREFIX) - 1) == 0 &&
236 strcmp(name + sizeof(XATTR_LUSTRE_PREFIX) - 1, "lov") == 0)) {
237 struct lov_user_md *lump = (struct lov_user_md *)value;
240 /* Attributes that are saved via getxattr will always have
241 * the stripe_offset as 0. Instead, the MDS should be
242 * allowed to pick the starting OST index. b=17846 */
243 if (lump != NULL && lump->lmm_stripe_offset == 0)
244 lump->lmm_stripe_offset = -1;
245 /* Avoid anyone directly setting the RELEASED flag. */
247 (lump->lmm_pattern & LOV_PATTERN_F_RELEASED)) {
248 /* Only if we have a released flag check if the file
249 * was indeed archived. */
250 __u32 state = HS_NONE;
251 rc = get_hsm_state(inode, &state);
254 if (!(state & HS_ARCHIVED)) {
256 "hus_states state = %x, pattern = %x\n",
257 state, lump->lmm_pattern);
258 /* Here the state is: real file is not
259 * archived but user is requesting to set
260 * the RELEASED flag so we mask off the
261 * released flag from the request */
262 lump->lmm_pattern ^= LOV_PATTERN_F_RELEASED;
266 if (lump != NULL && 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)
273 return 0; /* b=10667: ignore error */
275 memset(&f, 0, sizeof(f)); /* f.f_flags is used below */
276 f.f_path.dentry = dentry;
277 rc = ll_lov_setstripe_ea_info(inode, &f, it_flags, lump,
279 /* b=10667: rc always be 0 here for now */
281 } else if (S_ISDIR(inode->i_mode)) {
282 rc = ll_dir_setstripe(inode, lump, 0);
287 } else if (strcmp(name, XATTR_NAME_LMA) == 0 ||
288 strcmp(name, XATTR_NAME_LINK) == 0)
291 return ll_setxattr_common(inode, name, value, size, flags,
295 int ll_removexattr(struct dentry *dentry, const char *name)
297 struct inode *inode = dentry->d_inode;
302 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), xattr %s\n",
303 PFID(ll_inode2fid(inode)), inode, name);
305 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_REMOVEXATTR, 1);
306 return ll_setxattr_common(inode, name, NULL, 0, 0,
310 int ll_getxattr_common(struct inode *inode, const char *name,
311 void *buffer, size_t size, __u64 valid)
313 struct ll_sb_info *sbi = ll_i2sbi(inode);
314 struct ptlrpc_request *req = NULL;
315 struct mdt_body *body;
318 struct ll_inode_info *lli = ll_i2info(inode);
321 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p)\n",
322 PFID(ll_inode2fid(inode)), inode);
324 /* listxattr have slightly different behavior from of ext3:
325 * without 'user_xattr' ext3 will list all xattr names but
326 * filtered out "^user..*"; we list them all for simplicity.
329 xattr_type = XATTR_OTHER_T;
333 xattr_type = get_xattr_type(name);
334 rc = xattr_type_filter(sbi, xattr_type);
338 /* b15587: ignore security.capability xattr for now */
339 if ((xattr_type == XATTR_SECURITY_T &&
340 strcmp(name, "security.capability") == 0))
343 /* LU-549: Disable security.selinux when selinux is disabled */
344 if (xattr_type == XATTR_SECURITY_T && !selinux_is_enabled() &&
345 strcmp(name, "security.selinux") == 0)
348 #ifdef CONFIG_FS_POSIX_ACL
349 /* posix acl is under protection of LOOKUP lock. when calling to this,
350 * we just have path resolution to the target inode, so we have great
351 * chance that cached ACL is uptodate.
353 if (xattr_type == XATTR_ACL_ACCESS_T) {
354 struct posix_acl *acl;
356 spin_lock(&lli->lli_lock);
357 acl = posix_acl_dup(lli->lli_posix_acl);
358 spin_unlock(&lli->lli_lock);
363 rc = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
364 posix_acl_release(acl);
367 if (xattr_type == XATTR_ACL_DEFAULT_T && !S_ISDIR(inode->i_mode))
372 if (sbi->ll_xattr_cache_enabled &&
373 xattr_type != XATTR_ACL_ACCESS_T &&
374 (xattr_type != XATTR_SECURITY_T ||
375 strcmp(name, "security.selinux") != 0)) {
376 rc = ll_xattr_cache_get(inode, name, buffer, size, valid);
378 goto getxattr_nocache;
382 /* Add "system.posix_acl_access" to the list */
383 if (lli->lli_posix_acl != NULL && valid & OBD_MD_FLXATTRLS) {
385 rc += sizeof(XATTR_NAME_ACL_ACCESS);
386 } else if (size - rc >= sizeof(XATTR_NAME_ACL_ACCESS)) {
387 memcpy(buffer + rc, XATTR_NAME_ACL_ACCESS,
388 sizeof(XATTR_NAME_ACL_ACCESS));
389 rc += sizeof(XATTR_NAME_ACL_ACCESS);
391 GOTO(out_xattr, rc = -ERANGE);
396 rc = md_getxattr(sbi->ll_md_exp, ll_inode2fid(inode),
397 valid, name, NULL, 0, size, 0, &req);
402 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
405 /* only detect the xattr size */
407 GOTO(out, rc = body->mbo_eadatasize);
409 if (size < body->mbo_eadatasize) {
410 CERROR("server bug: replied size %u > %u\n",
411 body->mbo_eadatasize, (int)size);
412 GOTO(out, rc = -ERANGE);
415 if (body->mbo_eadatasize == 0)
416 GOTO(out, rc = -ENODATA);
418 /* do not need swab xattr data */
419 xdata = req_capsule_server_sized_get(&req->rq_pill, &RMF_EADATA,
420 body->mbo_eadatasize);
422 GOTO(out, rc = -EFAULT);
424 memcpy(buffer, xdata, body->mbo_eadatasize);
425 rc = body->mbo_eadatasize;
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 ((struct lov_mds_md *)buf)->lmm_layout_gen = 0;
482 cl_env_put(env, &refcheck);
485 } else if (S_ISDIR(inode->i_mode)) {
486 struct lov_mds_md *lmm = NULL;
488 struct ptlrpc_request *req = NULL;
490 rc = ll_dir_getstripe(inode, (void **)&lmm, &lmm_size,
496 GOTO(out_req, rc = lmm_size);
498 if (buf_size < lmm_size)
499 GOTO(out_req, rc = -ERANGE);
501 memcpy(buf, lmm, lmm_size);
502 GOTO(out_req, rc = lmm_size);
505 ptlrpc_req_finished(req);
513 ssize_t ll_getxattr(struct dentry *dentry, const char *name, void *buf,
516 struct inode *inode = dentry->d_inode;
521 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), xattr %s\n",
522 PFID(ll_inode2fid(inode)), inode, name);
524 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_GETXATTR, 1);
526 if (strcmp(name, XATTR_LUSTRE_LOV) == 0 ||
527 strcmp(name, XATTR_NAME_LOV) == 0)
528 return ll_getxattr_lov(inode, buf, buf_size);
530 return ll_getxattr_common(inode, name, buf, buf_size,
534 ssize_t ll_listxattr(struct dentry *dentry, char *buf, size_t buf_size)
536 struct inode *inode = dentry->d_inode;
537 struct ll_sb_info *sbi = ll_i2sbi(inode);
542 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p)\n",
543 PFID(ll_inode2fid(inode)), inode);
545 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_LISTXATTR, 1);
547 rc = ll_getxattr_common(inode, NULL, buf, buf_size, OBD_MD_FLXATTRLS);
551 /* If we're being called to get the size of the xattr list
552 * (buf_size == 0) then just assume that a lustre.lov xattr
555 RETURN(rc + sizeof(XATTR_LUSTRE_LOV));
561 len = strnlen(xattr_name, rem - 1) + 1;
563 if (xattr_type_filter(sbi, get_xattr_type(xattr_name)) == 0) {
564 /* Skip OK xattr type, leave it in buffer. */
569 /* Move up remaining xattrs in buffer removing the
570 * xattr that is not OK. */
571 memmove(xattr_name, xattr_name + len, rem);
575 rc2 = ll_getxattr_lov(inode, NULL, 0);
582 if (buf_size < rc + sizeof(XATTR_LUSTRE_LOV))
585 memcpy(buf + rc, XATTR_LUSTRE_LOV, sizeof(XATTR_LUSTRE_LOV));
587 RETURN(rc + sizeof(XATTR_LUSTRE_LOV));