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, 2014, 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/selinux.h>
42 #define DEBUG_SUBSYSTEM S_LLITE
44 #include <obd_support.h>
45 #include <lustre_dlm.h>
46 #include <lustre_ver.h>
47 #include <lustre_eacl.h>
49 #include "llite_internal.h"
51 #define XATTR_USER_T (1)
52 #define XATTR_TRUSTED_T (2)
53 #define XATTR_SECURITY_T (3)
54 #define XATTR_ACL_ACCESS_T (4)
55 #define XATTR_ACL_DEFAULT_T (5)
56 #define XATTR_LUSTRE_T (6)
57 #define XATTR_OTHER_T (7)
60 int get_xattr_type(const char *name)
62 if (!strcmp(name, POSIX_ACL_XATTR_ACCESS))
63 return XATTR_ACL_ACCESS_T;
65 if (!strcmp(name, POSIX_ACL_XATTR_DEFAULT))
66 return XATTR_ACL_DEFAULT_T;
68 if (!strncmp(name, XATTR_USER_PREFIX,
69 sizeof(XATTR_USER_PREFIX) - 1))
72 if (!strncmp(name, XATTR_TRUSTED_PREFIX,
73 sizeof(XATTR_TRUSTED_PREFIX) - 1))
74 return XATTR_TRUSTED_T;
76 if (!strncmp(name, XATTR_SECURITY_PREFIX,
77 sizeof(XATTR_SECURITY_PREFIX) - 1))
78 return XATTR_SECURITY_T;
80 if (!strncmp(name, XATTR_LUSTRE_PREFIX,
81 sizeof(XATTR_LUSTRE_PREFIX) - 1))
82 return XATTR_LUSTRE_T;
88 int xattr_type_filter(struct ll_sb_info *sbi, int xattr_type)
90 if ((xattr_type == XATTR_ACL_ACCESS_T ||
91 xattr_type == XATTR_ACL_DEFAULT_T) &&
92 !(sbi->ll_flags & LL_SBI_ACL))
95 if (xattr_type == XATTR_USER_T && !(sbi->ll_flags & LL_SBI_USER_XATTR))
97 if (xattr_type == XATTR_TRUSTED_T && !cfs_capable(CFS_CAP_SYS_ADMIN))
99 if (xattr_type == XATTR_OTHER_T)
106 int ll_setxattr_common(struct inode *inode, const char *name,
107 const void *value, size_t size,
108 int flags, __u64 valid)
110 struct ll_sb_info *sbi = ll_i2sbi(inode);
111 struct ptlrpc_request *req = NULL;
114 posix_acl_xattr_header *new_value = NULL;
115 struct rmtacl_ctl_entry *rce = NULL;
116 ext_acl_xattr_header *acl = NULL;
117 const char *pv = value;
120 xattr_type = get_xattr_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 ((xattr_type == XATTR_TRUSTED_T && strcmp(name, "trusted.lov") == 0) ||
136 (xattr_type == XATTR_LUSTRE_T && strcmp(name, "lustre.lov") == 0))
139 /* b15587: ignore security.capability xattr for now */
140 if ((xattr_type == XATTR_SECURITY_T &&
141 strcmp(name, "security.capability") == 0))
144 /* LU-549: Disable security.selinux when selinux is disabled */
145 if (xattr_type == XATTR_SECURITY_T && !selinux_is_enabled() &&
146 strcmp(name, "security.selinux") == 0)
149 #ifdef CONFIG_FS_POSIX_ACL
150 if (sbi->ll_flags & LL_SBI_RMT_CLIENT &&
151 (xattr_type == XATTR_ACL_ACCESS_T ||
152 xattr_type == XATTR_ACL_DEFAULT_T)) {
153 rce = rct_search(&sbi->ll_rct, current_pid());
155 (rce->rce_ops != RMT_LSETFACL &&
156 rce->rce_ops != RMT_RSETFACL))
159 if (rce->rce_ops == RMT_LSETFACL) {
160 struct eacl_entry *ee;
162 ee = et_search_del(&sbi->ll_et, current_pid(),
163 ll_inode2fid(inode), xattr_type);
165 if (valid & OBD_MD_FLXATTR) {
166 acl = lustre_acl_xattr_merge2ext(
167 (posix_acl_xattr_header *)value,
171 RETURN(PTR_ERR(acl));
173 size = CFS_ACL_XATTR_SIZE(\
174 le32_to_cpu(acl->a_count), \
176 pv = (const char *)acl;
179 } else if (rce->rce_ops == RMT_RSETFACL) {
180 int acl_size = lustre_posix_acl_xattr_filter(
181 (posix_acl_xattr_header *)value,
183 if (unlikely(acl_size < 0))
187 pv = (const char *)new_value;
191 valid |= rce_ops2valid(rce->rce_ops);
194 oc = ll_mdscapa_get(inode);
195 rc = md_setxattr(sbi->ll_md_exp, ll_inode2fid(inode), oc,
196 valid, name, pv, size, 0, flags,
197 ll_i2suppgid(inode), &req);
199 #ifdef CONFIG_FS_POSIX_ACL
200 if (new_value != NULL)
201 lustre_posix_acl_xattr_free(new_value, size);
203 lustre_ext_acl_xattr_free(acl);
206 if (rc == -EOPNOTSUPP && xattr_type == XATTR_USER_T) {
207 LCONSOLE_INFO("Disabling user_xattr feature because "
208 "it is not supported on the server\n");
209 sbi->ll_flags &= ~LL_SBI_USER_XATTR;
214 ptlrpc_req_finished(req);
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;
246 if (lump != NULL && S_ISREG(inode->i_mode)) {
248 __u64 it_flags = FMODE_WRITE;
251 lum_size = ll_lov_user_md_size(lump);
252 if (lum_size < 0 || size < lum_size)
253 return 0; /* b=10667: ignore error */
255 memset(&f, 0, sizeof(f)); /* f.f_flags is used below */
256 f.f_path.dentry = dentry;
257 rc = ll_lov_setstripe_ea_info(inode, &f, it_flags, lump,
259 /* b=10667: rc always be 0 here for now */
261 } else if (S_ISDIR(inode->i_mode)) {
262 rc = ll_dir_setstripe(inode, lump, 0);
267 } else if (strcmp(name, XATTR_NAME_LMA) == 0 ||
268 strcmp(name, XATTR_NAME_LINK) == 0)
271 return ll_setxattr_common(inode, name, value, size, flags,
275 int ll_removexattr(struct dentry *dentry, const char *name)
277 struct inode *inode = dentry->d_inode;
282 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), xattr %s\n",
283 PFID(ll_inode2fid(inode)), inode, name);
285 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_REMOVEXATTR, 1);
286 return ll_setxattr_common(inode, name, NULL, 0, 0,
290 int ll_getxattr_common(struct inode *inode, const char *name,
291 void *buffer, size_t size, __u64 valid)
293 struct ll_sb_info *sbi = ll_i2sbi(inode);
294 struct ptlrpc_request *req = NULL;
295 struct mdt_body *body;
299 struct rmtacl_ctl_entry *rce = NULL;
300 struct ll_inode_info *lli = ll_i2info(inode);
303 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p)\n",
304 PFID(ll_inode2fid(inode)), inode);
306 /* listxattr have slightly different behavior from of ext3:
307 * without 'user_xattr' ext3 will list all xattr names but
308 * filtered out "^user..*"; we list them all for simplicity.
311 xattr_type = XATTR_OTHER_T;
315 xattr_type = get_xattr_type(name);
316 rc = xattr_type_filter(sbi, xattr_type);
320 /* b15587: ignore security.capability xattr for now */
321 if ((xattr_type == XATTR_SECURITY_T &&
322 strcmp(name, "security.capability") == 0))
325 /* LU-549: Disable security.selinux when selinux is disabled */
326 if (xattr_type == XATTR_SECURITY_T && !selinux_is_enabled() &&
327 strcmp(name, "security.selinux") == 0)
330 #ifdef CONFIG_FS_POSIX_ACL
331 if (sbi->ll_flags & LL_SBI_RMT_CLIENT &&
332 (xattr_type == XATTR_ACL_ACCESS_T ||
333 xattr_type == XATTR_ACL_DEFAULT_T)) {
334 rce = rct_search(&sbi->ll_rct, current_pid());
336 (rce->rce_ops != RMT_LSETFACL &&
337 rce->rce_ops != RMT_LGETFACL &&
338 rce->rce_ops != RMT_RSETFACL &&
339 rce->rce_ops != RMT_RGETFACL))
343 /* posix acl is under protection of LOOKUP lock. when calling to this,
344 * we just have path resolution to the target inode, so we have great
345 * chance that cached ACL is uptodate.
347 if (xattr_type == XATTR_ACL_ACCESS_T &&
348 !(sbi->ll_flags & LL_SBI_RMT_CLIENT)) {
350 struct posix_acl *acl;
352 spin_lock(&lli->lli_lock);
353 acl = posix_acl_dup(lli->lli_posix_acl);
354 spin_unlock(&lli->lli_lock);
359 rc = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
360 posix_acl_release(acl);
363 if (xattr_type == XATTR_ACL_DEFAULT_T && !S_ISDIR(inode->i_mode))
368 if (sbi->ll_xattr_cache_enabled && xattr_type != XATTR_ACL_ACCESS_T) {
369 rc = ll_xattr_cache_get(inode, name, buffer, size, valid);
371 goto getxattr_nocache;
375 /* Add "system.posix_acl_access" to the list */
376 if (lli->lli_posix_acl != NULL && valid & OBD_MD_FLXATTRLS) {
378 rc += sizeof(XATTR_NAME_ACL_ACCESS);
379 } else if (size - rc >= sizeof(XATTR_NAME_ACL_ACCESS)) {
380 memcpy(buffer + rc, XATTR_NAME_ACL_ACCESS,
381 sizeof(XATTR_NAME_ACL_ACCESS));
382 rc += sizeof(XATTR_NAME_ACL_ACCESS);
384 GOTO(out_xattr, rc = -ERANGE);
389 oc = ll_mdscapa_get(inode);
390 rc = md_getxattr(sbi->ll_md_exp, ll_inode2fid(inode), oc,
391 valid | (rce ? rce_ops2valid(rce->rce_ops) : 0),
392 name, NULL, 0, size, 0, &req);
398 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
401 /* only detect the xattr size */
403 GOTO(out, rc = body->mbo_eadatasize);
405 if (size < body->mbo_eadatasize) {
406 CERROR("server bug: replied size %u > %u\n",
407 body->mbo_eadatasize, (int)size);
408 GOTO(out, rc = -ERANGE);
411 if (body->mbo_eadatasize == 0)
412 GOTO(out, rc = -ENODATA);
414 /* do not need swab xattr data */
415 xdata = req_capsule_server_sized_get(&req->rq_pill, &RMF_EADATA,
416 body->mbo_eadatasize);
418 GOTO(out, rc = -EFAULT);
420 memcpy(buffer, xdata, body->mbo_eadatasize);
421 rc = body->mbo_eadatasize;
424 #ifdef CONFIG_FS_POSIX_ACL
425 if (rce != NULL && rce->rce_ops == RMT_LSETFACL) {
426 ext_acl_xattr_header *acl;
428 acl = lustre_posix_acl_xattr_2ext(buffer, rc);
430 GOTO(out, rc = PTR_ERR(acl));
432 rc = ee_add(&sbi->ll_et, current_pid(), ll_inode2fid(inode),
434 if (unlikely(rc < 0)) {
435 lustre_ext_acl_xattr_free(acl);
443 if (rc == -EOPNOTSUPP && xattr_type == XATTR_USER_T) {
444 LCONSOLE_INFO("%s: disabling user_xattr feature because "
445 "it is not supported on the server: rc = %d\n",
446 ll_get_fsname(inode->i_sb, NULL, 0), rc);
447 sbi->ll_flags &= ~LL_SBI_USER_XATTR;
450 ptlrpc_req_finished(req);
454 static ssize_t ll_getxattr_lov(struct inode *inode, void *buf, size_t buf_size)
458 if (S_ISREG(inode->i_mode)) {
459 struct cl_object *obj = ll_i2info(inode)->lli_clob;
461 struct cl_layout cl = {
462 .cl_buf.lb_buf = buf,
463 .cl_buf.lb_len = buf_size,
470 env = cl_env_get(&refcheck);
472 RETURN(PTR_ERR(env));
474 rc = cl_object_layout_get(env, obj, &cl);
479 GOTO(out_env, rc = -ENODATA);
486 LASSERT(buf != NULL && rc <= buf_size);
488 /* Do not return layout gen for getxattr() since
489 * otherwise it would confuse tar --xattr by
490 * recognizing layout gen as stripe offset when the
491 * file is restored. See LU-2809. */
492 ((struct lov_mds_md *)buf)->lmm_layout_gen = 0;
494 cl_env_put(env, &refcheck);
497 } else if (S_ISDIR(inode->i_mode)) {
498 struct lov_mds_md *lmm = NULL;
500 struct ptlrpc_request *req = NULL;
502 rc = ll_dir_getstripe(inode, (void **)&lmm, &lmm_size,
508 GOTO(out_req, rc = lmm_size);
510 if (buf_size < lmm_size)
511 GOTO(out_req, rc = -ERANGE);
513 memcpy(buf, lmm, lmm_size);
514 GOTO(out_req, rc = lmm_size);
517 ptlrpc_req_finished(req);
525 ssize_t ll_getxattr(struct dentry *dentry, const char *name, void *buf,
528 struct inode *inode = dentry->d_inode;
533 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), xattr %s\n",
534 PFID(ll_inode2fid(inode)), inode, name);
536 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_GETXATTR, 1);
538 if (strcmp(name, XATTR_LUSTRE_LOV) == 0 ||
539 strcmp(name, XATTR_NAME_LOV) == 0)
540 return ll_getxattr_lov(inode, buf, buf_size);
542 return ll_getxattr_common(inode, name, buf, buf_size,
546 ssize_t ll_listxattr(struct dentry *dentry, char *buf, size_t buf_size)
548 struct inode *inode = dentry->d_inode;
549 struct ll_sb_info *sbi = ll_i2sbi(inode);
554 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p)\n",
555 PFID(ll_inode2fid(inode)), inode);
557 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_LISTXATTR, 1);
559 rc = ll_getxattr_common(inode, NULL, buf, buf_size, OBD_MD_FLXATTRLS);
563 /* If we're being called to get the size of the xattr list
564 * (buf_size == 0) then just assume that a lustre.lov xattr
567 RETURN(rc + sizeof(XATTR_LUSTRE_LOV));
573 len = strnlen(xattr_name, rem - 1) + 1;
575 if (xattr_type_filter(sbi, get_xattr_type(xattr_name)) == 0) {
576 /* Skip OK xattr type, leave it in buffer. */
581 /* Move up remaining xattrs in buffer removing the
582 * xattr that is not OK. */
583 memmove(xattr_name, xattr_name + len, rem);
587 rc2 = ll_getxattr_lov(inode, NULL, 0);
594 if (buf_size < rc + sizeof(XATTR_LUSTRE_LOV))
597 memcpy(buf + rc, XATTR_LUSTRE_LOV, sizeof(XATTR_LUSTRE_LOV));
599 RETURN(rc + sizeof(XATTR_LUSTRE_LOV));