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, 2013, 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 */
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,
291 int ll_getxattr_common(struct inode *inode, const char *name,
292 void *buffer, size_t size, __u64 valid)
294 struct ll_sb_info *sbi = ll_i2sbi(inode);
295 struct ptlrpc_request *req = NULL;
296 struct mdt_body *body;
300 struct rmtacl_ctl_entry *rce = NULL;
301 struct ll_inode_info *lli = ll_i2info(inode);
304 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p)\n",
305 PFID(ll_inode2fid(inode)), inode);
307 /* listxattr have slightly different behavior from of ext3:
308 * without 'user_xattr' ext3 will list all xattr names but
309 * filtered out "^user..*"; we list them all for simplicity.
312 xattr_type = XATTR_OTHER_T;
316 xattr_type = get_xattr_type(name);
317 rc = xattr_type_filter(sbi, xattr_type);
321 /* b15587: ignore security.capability xattr for now */
322 if ((xattr_type == XATTR_SECURITY_T &&
323 strcmp(name, "security.capability") == 0))
326 /* LU-549: Disable security.selinux when selinux is disabled */
327 if (xattr_type == XATTR_SECURITY_T && !selinux_is_enabled() &&
328 strcmp(name, "security.selinux") == 0)
331 #ifdef CONFIG_FS_POSIX_ACL
332 if (sbi->ll_flags & LL_SBI_RMT_CLIENT &&
333 (xattr_type == XATTR_ACL_ACCESS_T ||
334 xattr_type == XATTR_ACL_DEFAULT_T)) {
335 rce = rct_search(&sbi->ll_rct, current_pid());
337 (rce->rce_ops != RMT_LSETFACL &&
338 rce->rce_ops != RMT_LGETFACL &&
339 rce->rce_ops != RMT_RSETFACL &&
340 rce->rce_ops != RMT_RGETFACL))
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 !(sbi->ll_flags & LL_SBI_RMT_CLIENT)) {
351 struct posix_acl *acl;
353 spin_lock(&lli->lli_lock);
354 acl = posix_acl_dup(lli->lli_posix_acl);
355 spin_unlock(&lli->lli_lock);
360 rc = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
361 posix_acl_release(acl);
364 if (xattr_type == XATTR_ACL_DEFAULT_T && !S_ISDIR(inode->i_mode))
369 if (sbi->ll_xattr_cache_enabled && xattr_type != XATTR_ACL_ACCESS_T) {
370 rc = ll_xattr_cache_get(inode, name, buffer, size, valid);
372 goto getxattr_nocache;
376 /* Add "system.posix_acl_access" to the list */
377 if (lli->lli_posix_acl != NULL && valid & OBD_MD_FLXATTRLS) {
379 rc += sizeof(XATTR_NAME_ACL_ACCESS);
380 } else if (size - rc >= sizeof(XATTR_NAME_ACL_ACCESS)) {
381 memcpy(buffer + rc, XATTR_NAME_ACL_ACCESS,
382 sizeof(XATTR_NAME_ACL_ACCESS));
383 rc += sizeof(XATTR_NAME_ACL_ACCESS);
385 GOTO(out_xattr, rc = -ERANGE);
390 oc = ll_mdscapa_get(inode);
391 rc = md_getxattr(sbi->ll_md_exp, ll_inode2fid(inode), oc,
392 valid | (rce ? rce_ops2valid(rce->rce_ops) : 0),
393 name, NULL, 0, size, 0, &req);
399 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
402 /* only detect the xattr size */
404 GOTO(out, rc = body->mbo_eadatasize);
406 if (size < body->mbo_eadatasize) {
407 CERROR("server bug: replied size %u > %u\n",
408 body->mbo_eadatasize, (int)size);
409 GOTO(out, rc = -ERANGE);
412 if (body->mbo_eadatasize == 0)
413 GOTO(out, rc = -ENODATA);
415 /* do not need swab xattr data */
416 xdata = req_capsule_server_sized_get(&req->rq_pill, &RMF_EADATA,
417 body->mbo_eadatasize);
419 GOTO(out, rc = -EFAULT);
421 memcpy(buffer, xdata, body->mbo_eadatasize);
422 rc = body->mbo_eadatasize;
425 #ifdef CONFIG_FS_POSIX_ACL
426 if (rce != NULL && rce->rce_ops == RMT_LSETFACL) {
427 ext_acl_xattr_header *acl;
429 acl = lustre_posix_acl_xattr_2ext(buffer, rc);
431 GOTO(out, rc = PTR_ERR(acl));
433 rc = ee_add(&sbi->ll_et, current_pid(), ll_inode2fid(inode),
435 if (unlikely(rc < 0)) {
436 lustre_ext_acl_xattr_free(acl);
444 if (rc == -EOPNOTSUPP && xattr_type == XATTR_USER_T) {
445 LCONSOLE_INFO("%s: disabling user_xattr feature because "
446 "it is not supported on the server: rc = %d\n",
447 ll_get_fsname(inode->i_sb, NULL, 0), rc);
448 sbi->ll_flags &= ~LL_SBI_USER_XATTR;
451 ptlrpc_req_finished(req);
455 ssize_t ll_getxattr(struct dentry *dentry, const char *name,
456 void *buffer, size_t size)
458 struct inode *inode = dentry->d_inode;
463 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), xattr %s\n",
464 PFID(ll_inode2fid(inode)), inode, name);
466 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_GETXATTR, 1);
468 if ((strncmp(name, XATTR_TRUSTED_PREFIX,
469 sizeof(XATTR_TRUSTED_PREFIX) - 1) == 0 &&
470 strcmp(name + sizeof(XATTR_TRUSTED_PREFIX) - 1, "lov") == 0) ||
471 (strncmp(name, XATTR_LUSTRE_PREFIX,
472 sizeof(XATTR_LUSTRE_PREFIX) - 1) == 0 &&
473 strcmp(name + sizeof(XATTR_LUSTRE_PREFIX) - 1, "lov") == 0)) {
474 struct lov_stripe_md *lsm;
475 struct lov_user_md *lump;
476 struct lov_mds_md *lmm = NULL;
477 struct ptlrpc_request *request = NULL;
478 int rc = 0, lmmsize = 0;
480 if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
483 lsm = ccc_inode_lsm_get(inode);
485 if (S_ISDIR(inode->i_mode)) {
486 rc = ll_dir_getstripe(inode, (void **)&lmm,
487 &lmmsize, &request, 0);
492 /* LSM is present already after lookup/getattr call.
493 * we need to grab layout lock once it is implemented */
494 rc = obd_packmd(ll_i2dtexp(inode), &lmm, lsm);
497 ccc_inode_lsm_put(inode, lsm);
503 /* used to call ll_get_max_mdsize() forward to get
504 * the maximum buffer size, while some apps (such as
505 * rsync 3.0.x) care much about the exact xattr value
511 if (size < lmmsize) {
512 CERROR("server bug: replied size %d > %d for %s (%s)\n",
513 lmmsize, (int)size, dentry->d_name.name, name);
514 GOTO(out, rc = -ERANGE);
517 lump = (struct lov_user_md *)buffer;
518 memcpy(lump, lmm, lmmsize);
519 /* do not return layout gen for getxattr otherwise it would
520 * confuse tar --xattr by recognizing layout gen as stripe
521 * offset when the file is restored. See LU-2809. */
522 lump->lmm_layout_gen = 0;
527 ptlrpc_req_finished(request);
529 obd_free_diskmd(ll_i2dtexp(inode), &lmm);
533 return ll_getxattr_common(inode, name, buffer, size, OBD_MD_FLXATTR);
536 ssize_t ll_listxattr(struct dentry *dentry, char *buffer, size_t size)
538 struct inode *inode = dentry->d_inode;
540 struct lov_mds_md *lmm = NULL;
541 struct ptlrpc_request *request = NULL;
546 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p)\n",
547 PFID(ll_inode2fid(inode)), inode);
549 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_LISTXATTR, 1);
551 rc = ll_getxattr_common(inode, NULL, buffer, size, OBD_MD_FLXATTRLS);
555 if (buffer != NULL) {
556 struct ll_sb_info *sbi = ll_i2sbi(inode);
557 char *xattr_name = buffer;
561 xlen = strnlen(xattr_name, rem - 1) + 1;
563 if (xattr_type_filter(sbi,
564 get_xattr_type(xattr_name)) == 0) {
565 /* skip OK xattr type
571 /* move up remaining xattrs in buffer
572 * removing the xattr that is not OK
574 memmove(xattr_name, xattr_name + xlen, rem);
578 if (S_ISREG(inode->i_mode)) {
579 if (!ll_i2info(inode)->lli_has_smd)
581 } else if (S_ISDIR(inode->i_mode)) {
582 rc2 = ll_dir_getstripe(inode, (void **)&lmm, &lmmsize, &request,
588 } else if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)) {
589 const int prefix_len = sizeof(XATTR_LUSTRE_PREFIX) - 1;
590 const size_t name_len = sizeof("lov") - 1;
591 const size_t total_len = prefix_len + name_len + 1;
593 if (((rc + total_len) > size) && (buffer != NULL)) {
594 ptlrpc_req_finished(request);
598 if (buffer != NULL) {
600 memcpy(buffer, XATTR_LUSTRE_PREFIX, prefix_len);
601 memcpy(buffer + prefix_len, "lov", name_len);
602 buffer[prefix_len + name_len] = '\0';
607 ptlrpc_req_finished(request);