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>
44 #include "llite_internal.h"
46 const struct xattr_handler *get_xattr_type(const char *name)
50 while (ll_xattr_handlers[i]) {
51 size_t len = strlen(ll_xattr_handlers[i]->prefix);
53 if (!strncmp(ll_xattr_handlers[i]->prefix, name, len))
54 return ll_xattr_handlers[i];
60 static int xattr_type_filter(struct ll_sb_info *sbi,
61 const struct xattr_handler *handler)
63 /* No handler means XATTR_OTHER_T */
67 if ((handler->flags == XATTR_ACL_ACCESS_T ||
68 handler->flags == XATTR_ACL_DEFAULT_T) &&
69 !(sbi->ll_flags & LL_SBI_ACL))
72 if (handler->flags == XATTR_USER_T &&
73 !(sbi->ll_flags & LL_SBI_USER_XATTR))
76 if (handler->flags == XATTR_TRUSTED_T &&
77 !capable(CFS_CAP_SYS_ADMIN))
84 ll_xattr_set_common(const struct xattr_handler *handler,
85 struct dentry *dentry, struct inode *inode,
86 const char *name, const void *value, size_t size,
89 char fullname[strlen(handler->prefix) + strlen(name) + 1];
90 struct ll_sb_info *sbi = ll_i2sbi(inode);
91 struct ptlrpc_request *req = NULL;
92 const char *pv = value;
96 /* When setxattr() is called with a size of 0 the value is
97 * unconditionally replaced by "". When removexattr() is
98 * called we get a NULL value and XATTR_REPLACE for flags.
100 if (!value && flags == XATTR_REPLACE) {
101 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_REMOVEXATTR, 1);
102 valid = OBD_MD_FLXATTRRM;
104 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_SETXATTR, 1);
105 valid = OBD_MD_FLXATTR;
108 rc = xattr_type_filter(sbi, handler);
112 if ((handler->flags == XATTR_ACL_ACCESS_T ||
113 handler->flags == XATTR_ACL_DEFAULT_T) &&
114 !inode_owner_or_capable(inode))
117 /* b10667: ignore lustre special xattr for now */
118 if (!strcmp(name, "hsm") ||
119 ((handler->flags == XATTR_TRUSTED_T && !strcmp(name, "lov")) ||
120 (handler->flags == XATTR_LUSTRE_T && !strcmp(name, "lov"))))
123 /* b15587: ignore security.capability xattr for now */
124 if ((handler->flags == XATTR_SECURITY_T &&
125 !strcmp(name, "capability")))
128 /* LU-549: Disable security.selinux when selinux is disabled */
129 if (handler->flags == XATTR_SECURITY_T && !selinux_is_enabled() &&
130 strcmp(name, "selinux") == 0)
133 /*FIXME: enable IMA when the conditions are ready */
134 if (handler->flags == XATTR_SECURITY_T &&
135 (!strcmp(name, "ima") || !strcmp(name, "evm")))
139 * In user.* namespace, only regular files and directories can have
140 * extended attributes.
142 if (handler->flags == XATTR_USER_T) {
143 if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
147 sprintf(fullname, "%s%s\n", handler->prefix, name);
148 rc = md_setxattr(sbi->ll_md_exp, ll_inode2fid(inode),
149 valid, fullname, pv, size, 0, flags,
150 ll_i2suppgid(inode), &req);
152 if (rc == -EOPNOTSUPP && handler->flags == XATTR_USER_T) {
153 LCONSOLE_INFO("Disabling user_xattr feature because it is not supported on the server\n");
154 sbi->ll_flags &= ~LL_SBI_USER_XATTR;
159 ptlrpc_req_finished(req);
163 static int get_hsm_state(struct inode *inode, u32 *hus_states)
165 struct md_op_data *op_data;
166 struct hsm_user_state *hus;
169 hus = kzalloc(sizeof(*hus), GFP_NOFS);
173 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
174 LUSTRE_OPC_ANY, hus);
175 if (!IS_ERR(op_data)) {
176 rc = obd_iocontrol(LL_IOC_HSM_STATE_GET, ll_i2mdexp(inode),
177 sizeof(*op_data), op_data, NULL);
179 *hus_states = hus->hus_states;
181 CDEBUG(D_VFSTRACE, "obd_iocontrol failed. rc = %d\n",
184 ll_finish_md_op_data(op_data);
186 rc = PTR_ERR(op_data);
187 CDEBUG(D_VFSTRACE, "Could not prepare the opdata. rc = %d\n",
194 static int ll_xattr_set(const struct xattr_handler *handler,
195 struct dentry *dentry, struct inode *inode,
196 const char *name, const void *value, size_t size,
202 CDEBUG(D_VFSTRACE, "VFS Op:inode=" DFID "(%p), xattr %s\n",
203 PFID(ll_inode2fid(inode)), inode, name);
205 if (!strcmp(name, "lov")) {
206 struct lov_user_md *lump = (struct lov_user_md *)value;
207 int op_type = flags == XATTR_REPLACE ? LPROC_LL_REMOVEXATTR :
211 ll_stats_ops_tally(ll_i2sbi(inode), op_type, 1);
213 if (size != 0 && size < sizeof(struct lov_user_md))
217 * It is possible to set an xattr to a "" value of zero size.
218 * For this case we are going to treat it as a removal.
223 /* Attributes that are saved via getxattr will always have
224 * the stripe_offset as 0. Instead, the MDS should be
225 * allowed to pick the starting OST index. b=17846
227 if (lump && lump->lmm_stripe_offset == 0)
228 lump->lmm_stripe_offset = -1;
230 /* Avoid anyone directly setting the RELEASED flag. */
231 if (lump && (lump->lmm_pattern & LOV_PATTERN_F_RELEASED)) {
232 /* Only if we have a released flag check if the file
233 * was indeed archived.
237 rc = get_hsm_state(inode, &state);
241 if (!(state & HS_ARCHIVED)) {
243 "hus_states state = %x, pattern = %x\n",
244 state, lump->lmm_pattern);
246 * Here the state is: real file is not
247 * archived but user is requesting to set
248 * the RELEASED flag so we mask off the
249 * released flag from the request
251 lump->lmm_pattern ^= LOV_PATTERN_F_RELEASED;
255 if (lump && S_ISREG(inode->i_mode)) {
256 __u64 it_flags = FMODE_WRITE;
259 lum_size = ll_lov_user_md_size(lump);
260 if (lum_size < 0 || size < lum_size)
261 return 0; /* b=10667: ignore error */
263 rc = ll_lov_setstripe_ea_info(inode, dentry, it_flags,
265 /* b=10667: rc always be 0 here for now */
267 } else if (S_ISDIR(inode->i_mode)) {
268 rc = ll_dir_setstripe(inode, lump, 0);
273 } else if (!strcmp(name, "lma") || !strcmp(name, "link")) {
274 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_SETXATTR, 1);
278 return ll_xattr_set_common(handler, dentry, inode, name, value, size,
283 ll_xattr_list(struct inode *inode, const char *name, int type, void *buffer,
284 size_t size, __u64 valid)
286 struct ll_inode_info *lli = ll_i2info(inode);
287 struct ll_sb_info *sbi = ll_i2sbi(inode);
288 struct ptlrpc_request *req = NULL;
289 struct mdt_body *body;
293 if (sbi->ll_xattr_cache_enabled && type != XATTR_ACL_ACCESS_T &&
294 (type != XATTR_SECURITY_T || strcmp(name, "security.selinux"))) {
295 rc = ll_xattr_cache_get(inode, name, buffer, size, valid);
297 goto getxattr_nocache;
301 /* Add "system.posix_acl_access" to the list */
302 if (lli->lli_posix_acl && valid & OBD_MD_FLXATTRLS) {
304 rc += sizeof(XATTR_NAME_ACL_ACCESS);
305 } else if (size - rc >= sizeof(XATTR_NAME_ACL_ACCESS)) {
306 memcpy(buffer + rc, XATTR_NAME_ACL_ACCESS,
307 sizeof(XATTR_NAME_ACL_ACCESS));
308 rc += sizeof(XATTR_NAME_ACL_ACCESS);
316 rc = md_getxattr(sbi->ll_md_exp, ll_inode2fid(inode),
317 valid, name, NULL, 0, size, 0, &req);
321 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
324 /* only detect the xattr size */
326 rc = body->mbo_eadatasize;
330 if (size < body->mbo_eadatasize) {
331 CERROR("server bug: replied size %u > %u\n",
332 body->mbo_eadatasize, (int)size);
337 if (body->mbo_eadatasize == 0) {
342 /* do not need swab xattr data */
343 xdata = req_capsule_server_sized_get(&req->rq_pill, &RMF_EADATA,
344 body->mbo_eadatasize);
350 memcpy(buffer, xdata, body->mbo_eadatasize);
351 rc = body->mbo_eadatasize;
355 if (rc == -EOPNOTSUPP && type == XATTR_USER_T) {
357 "%s: disabling user_xattr feature because it is not supported on the server: rc = %d\n",
358 ll_get_fsname(inode->i_sb, NULL, 0), rc);
359 sbi->ll_flags &= ~LL_SBI_USER_XATTR;
362 ptlrpc_req_finished(req);
366 static int ll_xattr_get_common(const struct xattr_handler *handler,
367 struct dentry *dentry, struct inode *inode,
368 const char *name, void *buffer, size_t size)
370 char fullname[strlen(handler->prefix) + strlen(name) + 1];
371 struct ll_sb_info *sbi = ll_i2sbi(inode);
372 #ifdef CONFIG_FS_POSIX_ACL
373 struct ll_inode_info *lli = ll_i2info(inode);
377 CDEBUG(D_VFSTRACE, "VFS Op:inode=" DFID "(%p)\n",
378 PFID(ll_inode2fid(inode)), inode);
380 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_GETXATTR, 1);
382 rc = xattr_type_filter(sbi, handler);
386 /* b15587: ignore security.capability xattr for now */
387 if ((handler->flags == XATTR_SECURITY_T && !strcmp(name, "capability")))
390 /* LU-549: Disable security.selinux when selinux is disabled */
391 if (handler->flags == XATTR_SECURITY_T && !selinux_is_enabled() &&
392 !strcmp(name, "selinux"))
395 #ifdef CONFIG_FS_POSIX_ACL
396 /* posix acl is under protection of LOOKUP lock. when calling to this,
397 * we just have path resolution to the target inode, so we have great
398 * chance that cached ACL is uptodate.
400 if (handler->flags == XATTR_ACL_ACCESS_T) {
401 struct posix_acl *acl;
403 spin_lock(&lli->lli_lock);
404 acl = posix_acl_dup(lli->lli_posix_acl);
405 spin_unlock(&lli->lli_lock);
410 rc = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
411 posix_acl_release(acl);
414 if (handler->flags == XATTR_ACL_DEFAULT_T && !S_ISDIR(inode->i_mode))
417 sprintf(fullname, "%s%s\n", handler->prefix, name);
418 return ll_xattr_list(inode, fullname, handler->flags, buffer, size,
422 static ssize_t ll_getxattr_lov(struct inode *inode, void *buf, size_t buf_size)
426 if (S_ISREG(inode->i_mode)) {
427 struct cl_object *obj = ll_i2info(inode)->lli_clob;
428 struct cl_layout cl = {
429 .cl_buf.lb_buf = buf,
430 .cl_buf.lb_len = buf_size,
438 env = cl_env_get(&refcheck);
442 rc = cl_object_layout_get(env, obj, &cl);
456 LASSERT(buf && rc <= buf_size);
459 * Do not return layout gen for getxattr() since
460 * otherwise it would confuse tar --xattr by
461 * recognizing layout gen as stripe offset when the
462 * file is restored. See LU-2809.
464 ((struct lov_mds_md *)buf)->lmm_layout_gen = 0;
466 cl_env_put(env, &refcheck);
469 } else if (S_ISDIR(inode->i_mode)) {
470 struct ptlrpc_request *req = NULL;
471 struct lov_mds_md *lmm = NULL;
474 rc = ll_dir_getstripe(inode, (void **)&lmm, &lmm_size,
484 if (buf_size < lmm_size) {
489 memcpy(buf, lmm, lmm_size);
493 ptlrpc_req_finished(req);
501 static int ll_xattr_get(const struct xattr_handler *handler,
502 struct dentry *dentry, struct inode *inode,
503 const char *name, void *buffer, size_t size)
508 CDEBUG(D_VFSTRACE, "VFS Op:inode=" DFID "(%p), xattr %s\n",
509 PFID(ll_inode2fid(inode)), inode, name);
511 if (!strcmp(name, "lov")) {
512 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_GETXATTR, 1);
514 return ll_getxattr_lov(inode, buffer, size);
517 return ll_xattr_get_common(handler, dentry, inode, name, buffer, size);
520 ssize_t ll_listxattr(struct dentry *dentry, char *buffer, size_t size)
522 struct inode *inode = d_inode(dentry);
523 struct ll_sb_info *sbi = ll_i2sbi(inode);
530 CDEBUG(D_VFSTRACE, "VFS Op:inode=" DFID "(%p)\n",
531 PFID(ll_inode2fid(inode)), inode);
533 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_LISTXATTR, 1);
535 rc = ll_xattr_list(inode, NULL, XATTR_OTHER_T, buffer, size,
540 * If we're being called to get the size of the xattr list
541 * (buf_size == 0) then just assume that a lustre.lov xattr
545 return rc + sizeof(XATTR_LUSTRE_LOV);
551 len = strnlen(xattr_name, rem - 1) + 1;
553 if (!xattr_type_filter(sbi, get_xattr_type(xattr_name))) {
554 /* Skip OK xattr type leave it in buffer */
560 * Move up remaining xattrs in buffer
561 * removing the xattr that is not OK
563 memmove(xattr_name, xattr_name + len, rem);
567 rc2 = ll_getxattr_lov(inode, NULL, 0);
574 if (size < rc + sizeof(XATTR_LUSTRE_LOV))
577 memcpy(buffer + rc, XATTR_LUSTRE_LOV, sizeof(XATTR_LUSTRE_LOV));
579 return rc + sizeof(XATTR_LUSTRE_LOV);
582 static const struct xattr_handler ll_user_xattr_handler = {
583 .prefix = XATTR_USER_PREFIX,
584 .flags = XATTR_USER_T,
585 .get = ll_xattr_get_common,
586 .set = ll_xattr_set_common,
589 static const struct xattr_handler ll_trusted_xattr_handler = {
590 .prefix = XATTR_TRUSTED_PREFIX,
591 .flags = XATTR_TRUSTED_T,
596 static const struct xattr_handler ll_security_xattr_handler = {
597 .prefix = XATTR_SECURITY_PREFIX,
598 .flags = XATTR_SECURITY_T,
599 .get = ll_xattr_get_common,
600 .set = ll_xattr_set_common,
603 static const struct xattr_handler ll_acl_access_xattr_handler = {
604 .prefix = XATTR_NAME_POSIX_ACL_ACCESS,
605 .flags = XATTR_ACL_ACCESS_T,
606 .get = ll_xattr_get_common,
607 .set = ll_xattr_set_common,
610 static const struct xattr_handler ll_acl_default_xattr_handler = {
611 .prefix = XATTR_NAME_POSIX_ACL_DEFAULT,
612 .flags = XATTR_ACL_DEFAULT_T,
613 .get = ll_xattr_get_common,
614 .set = ll_xattr_set_common,
617 static const struct xattr_handler ll_lustre_xattr_handler = {
618 .prefix = XATTR_LUSTRE_PREFIX,
619 .flags = XATTR_LUSTRE_T,
624 const struct xattr_handler *ll_xattr_handlers[] = {
625 &ll_user_xattr_handler,
626 &ll_trusted_xattr_handler,
627 &ll_security_xattr_handler,
628 #ifdef CONFIG_FS_POSIX_ACL
629 &ll_acl_access_xattr_handler,
630 &ll_acl_default_xattr_handler,
632 &ll_lustre_xattr_handler,