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/selinux.h>
38 #define DEBUG_SUBSYSTEM S_LLITE
40 #include "../include/obd_support.h"
41 #include "../include/lustre_dlm.h"
42 #include "../include/lustre_ver.h"
43 #include "../include/lustre_eacl.h"
45 #include "llite_internal.h"
48 int get_xattr_type(const char *name)
50 if (!strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS))
51 return XATTR_ACL_ACCESS_T;
53 if (!strcmp(name, XATTR_NAME_POSIX_ACL_DEFAULT))
54 return XATTR_ACL_DEFAULT_T;
56 if (!strncmp(name, XATTR_USER_PREFIX,
57 sizeof(XATTR_USER_PREFIX) - 1))
60 if (!strncmp(name, XATTR_TRUSTED_PREFIX,
61 sizeof(XATTR_TRUSTED_PREFIX) - 1))
62 return XATTR_TRUSTED_T;
64 if (!strncmp(name, XATTR_SECURITY_PREFIX,
65 sizeof(XATTR_SECURITY_PREFIX) - 1))
66 return XATTR_SECURITY_T;
68 if (!strncmp(name, XATTR_LUSTRE_PREFIX,
69 sizeof(XATTR_LUSTRE_PREFIX) - 1))
70 return XATTR_LUSTRE_T;
76 int xattr_type_filter(struct ll_sb_info *sbi, int xattr_type)
78 if ((xattr_type == XATTR_ACL_ACCESS_T ||
79 xattr_type == XATTR_ACL_DEFAULT_T) &&
80 !(sbi->ll_flags & LL_SBI_ACL))
83 if (xattr_type == XATTR_USER_T && !(sbi->ll_flags & LL_SBI_USER_XATTR))
85 if (xattr_type == XATTR_TRUSTED_T && !capable(CFS_CAP_SYS_ADMIN))
87 if (xattr_type == XATTR_OTHER_T)
94 ll_xattr_set_common(const struct xattr_handler *handler,
95 struct dentry *dentry, struct inode *inode,
96 const char *name, const void *value, size_t size,
99 char fullname[strlen(handler->prefix) + strlen(name) + 1];
100 struct ll_sb_info *sbi = ll_i2sbi(inode);
101 struct ptlrpc_request *req = NULL;
102 const char *pv = value;
106 /* When setxattr() is called with a size of 0 the value is
107 * unconditionally replaced by "". When removexattr() is
108 * called we get a NULL value and XATTR_REPLACE for flags.
110 if (!value && flags == XATTR_REPLACE) {
111 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_REMOVEXATTR, 1);
112 valid = OBD_MD_FLXATTRRM;
114 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_SETXATTR, 1);
115 valid = OBD_MD_FLXATTR;
118 rc = xattr_type_filter(sbi, handler->flags);
122 if ((handler->flags == XATTR_ACL_ACCESS_T ||
123 handler->flags == XATTR_ACL_DEFAULT_T) &&
124 !inode_owner_or_capable(inode))
127 /* b10667: ignore lustre special xattr for now */
128 if ((handler->flags == XATTR_TRUSTED_T && !strcmp(name, "lov")) ||
129 (handler->flags == XATTR_LUSTRE_T && !strcmp(name, "lov")))
132 /* b15587: ignore security.capability xattr for now */
133 if ((handler->flags == XATTR_SECURITY_T &&
134 !strcmp(name, "capability")))
137 /* LU-549: Disable security.selinux when selinux is disabled */
138 if (handler->flags == XATTR_SECURITY_T && !selinux_is_enabled() &&
139 strcmp(name, "selinux") == 0)
142 sprintf(fullname, "%s%s\n", handler->prefix, name);
143 rc = md_setxattr(sbi->ll_md_exp, ll_inode2fid(inode),
144 valid, fullname, pv, size, 0, flags,
145 ll_i2suppgid(inode), &req);
147 if (rc == -EOPNOTSUPP && handler->flags == XATTR_USER_T) {
148 LCONSOLE_INFO("Disabling user_xattr feature because it is not supported on the server\n");
149 sbi->ll_flags &= ~LL_SBI_USER_XATTR;
154 ptlrpc_req_finished(req);
158 static int ll_xattr_set(const struct xattr_handler *handler,
159 struct dentry *dentry, struct inode *inode,
160 const char *name, const void *value, size_t size,
166 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), xattr %s\n",
167 PFID(ll_inode2fid(inode)), inode, name);
169 if (!strcmp(name, "lov")) {
170 struct lov_user_md *lump = (struct lov_user_md *)value;
171 int op_type = flags == XATTR_REPLACE ? LPROC_LL_REMOVEXATTR :
175 ll_stats_ops_tally(ll_i2sbi(inode), op_type, 1);
177 if (size != 0 && size < sizeof(struct lov_user_md))
181 * It is possible to set an xattr to a "" value of zero size.
182 * For this case we are going to treat it as a removal.
187 /* Attributes that are saved via getxattr will always have
188 * the stripe_offset as 0. Instead, the MDS should be
189 * allowed to pick the starting OST index. b=17846
191 if (lump && lump->lmm_stripe_offset == 0)
192 lump->lmm_stripe_offset = -1;
194 if (lump && S_ISREG(inode->i_mode)) {
195 __u64 it_flags = FMODE_WRITE;
198 lum_size = ll_lov_user_md_size(lump);
199 if (lum_size < 0 || size < lum_size)
200 return 0; /* b=10667: ignore error */
202 rc = ll_lov_setstripe_ea_info(inode, dentry, it_flags,
204 /* b=10667: rc always be 0 here for now */
206 } else if (S_ISDIR(inode->i_mode)) {
207 rc = ll_dir_setstripe(inode, lump, 0);
212 } else if (!strcmp(name, "lma") || !strcmp(name, "link")) {
213 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_SETXATTR, 1);
217 return ll_xattr_set_common(handler, dentry, inode, name, value, size,
222 ll_xattr_list(struct inode *inode, const char *name, int type, void *buffer,
223 size_t size, __u64 valid)
225 struct ll_inode_info *lli = ll_i2info(inode);
226 struct ll_sb_info *sbi = ll_i2sbi(inode);
227 struct ptlrpc_request *req = NULL;
228 struct mdt_body *body;
232 if (sbi->ll_xattr_cache_enabled && type != XATTR_ACL_ACCESS_T) {
233 rc = ll_xattr_cache_get(inode, name, buffer, size, valid);
235 goto getxattr_nocache;
239 /* Add "system.posix_acl_access" to the list */
240 if (lli->lli_posix_acl && valid & OBD_MD_FLXATTRLS) {
242 rc += sizeof(XATTR_NAME_ACL_ACCESS);
243 } else if (size - rc >= sizeof(XATTR_NAME_ACL_ACCESS)) {
244 memcpy(buffer + rc, XATTR_NAME_ACL_ACCESS,
245 sizeof(XATTR_NAME_ACL_ACCESS));
246 rc += sizeof(XATTR_NAME_ACL_ACCESS);
254 rc = md_getxattr(sbi->ll_md_exp, ll_inode2fid(inode),
255 valid, name, NULL, 0, size, 0, &req);
259 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
262 /* only detect the xattr size */
264 rc = body->mbo_eadatasize;
268 if (size < body->mbo_eadatasize) {
269 CERROR("server bug: replied size %u > %u\n",
270 body->mbo_eadatasize, (int)size);
275 if (body->mbo_eadatasize == 0) {
280 /* do not need swab xattr data */
281 xdata = req_capsule_server_sized_get(&req->rq_pill, &RMF_EADATA,
282 body->mbo_eadatasize);
288 memcpy(buffer, xdata, body->mbo_eadatasize);
289 rc = body->mbo_eadatasize;
293 if (rc == -EOPNOTSUPP && type == XATTR_USER_T) {
295 "%s: disabling user_xattr feature because it is not supported on the server: rc = %d\n",
296 ll_get_fsname(inode->i_sb, NULL, 0), rc);
297 sbi->ll_flags &= ~LL_SBI_USER_XATTR;
300 ptlrpc_req_finished(req);
304 static int ll_xattr_get_common(const struct xattr_handler *handler,
305 struct dentry *dentry, struct inode *inode,
306 const char *name, void *buffer, size_t size)
308 char fullname[strlen(handler->prefix) + strlen(name) + 1];
309 struct ll_sb_info *sbi = ll_i2sbi(inode);
310 #ifdef CONFIG_FS_POSIX_ACL
311 struct ll_inode_info *lli = ll_i2info(inode);
315 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p)\n",
316 PFID(ll_inode2fid(inode)), inode);
318 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_GETXATTR, 1);
320 rc = xattr_type_filter(sbi, handler->flags);
324 /* b15587: ignore security.capability xattr for now */
325 if ((handler->flags == XATTR_SECURITY_T && !strcmp(name, "capability")))
328 /* LU-549: Disable security.selinux when selinux is disabled */
329 if (handler->flags == XATTR_SECURITY_T && !selinux_is_enabled() &&
330 !strcmp(name, "selinux"))
333 #ifdef CONFIG_FS_POSIX_ACL
334 /* posix acl is under protection of LOOKUP lock. when calling to this,
335 * we just have path resolution to the target inode, so we have great
336 * chance that cached ACL is uptodate.
338 if (handler->flags == XATTR_ACL_ACCESS_T) {
339 struct posix_acl *acl;
341 spin_lock(&lli->lli_lock);
342 acl = posix_acl_dup(lli->lli_posix_acl);
343 spin_unlock(&lli->lli_lock);
348 rc = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
349 posix_acl_release(acl);
352 if (handler->flags == XATTR_ACL_DEFAULT_T && !S_ISDIR(inode->i_mode))
355 sprintf(fullname, "%s%s\n", handler->prefix, name);
356 return ll_xattr_list(inode, fullname, handler->flags, buffer, size,
360 static int ll_xattr_get(const struct xattr_handler *handler,
361 struct dentry *dentry, struct inode *inode,
362 const char *name, void *buffer, size_t size)
367 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), xattr %s\n",
368 PFID(ll_inode2fid(inode)), inode, name);
370 if (!strcmp(name, "lov")) {
371 struct lov_stripe_md *lsm;
372 struct lov_user_md *lump;
373 struct lov_mds_md *lmm = NULL;
374 struct ptlrpc_request *request = NULL;
375 int rc = 0, lmmsize = 0;
377 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_GETXATTR, 1);
379 if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
382 lsm = ccc_inode_lsm_get(inode);
384 if (S_ISDIR(inode->i_mode)) {
385 rc = ll_dir_getstripe(inode, (void **)&lmm,
386 &lmmsize, &request, 0);
391 /* LSM is present already after lookup/getattr call.
392 * we need to grab layout lock once it is implemented
394 rc = obd_packmd(ll_i2dtexp(inode), &lmm, lsm);
397 ccc_inode_lsm_put(inode, lsm);
403 /* used to call ll_get_max_mdsize() forward to get
404 * the maximum buffer size, while some apps (such as
405 * rsync 3.0.x) care much about the exact xattr value
412 if (size < lmmsize) {
413 CERROR("server bug: replied size %d > %d for %pd (%s)\n",
414 lmmsize, (int)size, dentry, name);
420 memcpy(lump, lmm, lmmsize);
421 /* do not return layout gen for getxattr otherwise it would
422 * confuse tar --xattr by recognizing layout gen as stripe
423 * offset when the file is restored. See LU-2809.
425 lump->lmm_layout_gen = 0;
430 ptlrpc_req_finished(request);
432 obd_free_diskmd(ll_i2dtexp(inode), &lmm);
436 return ll_xattr_get_common(handler, dentry, inode, name, buffer, size);
439 ssize_t ll_listxattr(struct dentry *dentry, char *buffer, size_t size)
441 struct inode *inode = d_inode(dentry);
443 struct lov_mds_md *lmm = NULL;
444 struct ptlrpc_request *request = NULL;
449 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p)\n",
450 PFID(ll_inode2fid(inode)), inode);
452 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_LISTXATTR, 1);
454 rc = ll_xattr_list(inode, NULL, XATTR_OTHER_T, buffer, size,
460 struct ll_sb_info *sbi = ll_i2sbi(inode);
461 char *xattr_name = buffer;
465 xlen = strnlen(xattr_name, rem - 1) + 1;
467 if (xattr_type_filter(sbi,
468 get_xattr_type(xattr_name)) == 0) {
469 /* skip OK xattr type
475 /* move up remaining xattrs in buffer
476 * removing the xattr that is not OK
478 memmove(xattr_name, xattr_name + xlen, rem);
482 if (S_ISREG(inode->i_mode)) {
483 if (!ll_i2info(inode)->lli_has_smd)
485 } else if (S_ISDIR(inode->i_mode)) {
486 rc2 = ll_dir_getstripe(inode, (void **)&lmm, &lmmsize,
493 } else if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)) {
494 const int prefix_len = sizeof(XATTR_LUSTRE_PREFIX) - 1;
495 const size_t name_len = sizeof("lov") - 1;
496 const size_t total_len = prefix_len + name_len + 1;
498 if (((rc + total_len) > size) && buffer) {
499 ptlrpc_req_finished(request);
505 memcpy(buffer, XATTR_LUSTRE_PREFIX, prefix_len);
506 memcpy(buffer + prefix_len, "lov", name_len);
507 buffer[prefix_len + name_len] = '\0';
512 ptlrpc_req_finished(request);
518 static const struct xattr_handler ll_user_xattr_handler = {
519 .prefix = XATTR_USER_PREFIX,
520 .flags = XATTR_USER_T,
521 .get = ll_xattr_get_common,
522 .set = ll_xattr_set_common,
525 static const struct xattr_handler ll_trusted_xattr_handler = {
526 .prefix = XATTR_TRUSTED_PREFIX,
527 .flags = XATTR_TRUSTED_T,
532 static const struct xattr_handler ll_security_xattr_handler = {
533 .prefix = XATTR_SECURITY_PREFIX,
534 .flags = XATTR_SECURITY_T,
535 .get = ll_xattr_get_common,
536 .set = ll_xattr_set_common,
539 static const struct xattr_handler ll_acl_access_xattr_handler = {
540 .prefix = XATTR_NAME_POSIX_ACL_ACCESS,
541 .flags = XATTR_ACL_ACCESS_T,
542 .get = ll_xattr_get_common,
543 .set = ll_xattr_set_common,
546 static const struct xattr_handler ll_acl_default_xattr_handler = {
547 .prefix = XATTR_NAME_POSIX_ACL_DEFAULT,
548 .flags = XATTR_ACL_DEFAULT_T,
549 .get = ll_xattr_get_common,
550 .set = ll_xattr_set_common,
553 static const struct xattr_handler ll_lustre_xattr_handler = {
554 .prefix = XATTR_LUSTRE_PREFIX,
555 .flags = XATTR_LUSTRE_T,
560 const struct xattr_handler *ll_xattr_handlers[] = {
561 &ll_user_xattr_handler,
562 &ll_trusted_xattr_handler,
563 &ll_security_xattr_handler,
564 #ifdef CONFIG_FS_POSIX_ACL
565 &ll_acl_access_xattr_handler,
566 &ll_acl_default_xattr_handler,
568 &ll_lustre_xattr_handler,