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, 2012, Whamcloud, Inc.
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 #ifdef HAVE_SELINUX_IS_ENABLED
41 #include <linux/selinux.h>
44 #define DEBUG_SUBSYSTEM S_LLITE
46 #include <obd_support.h>
47 #include <lustre_lite.h>
48 #include <lustre_dlm.h>
49 #include <lustre_ver.h>
50 #include <lustre_acl.h>
52 #include "llite_internal.h"
54 #define XATTR_USER_T (1)
55 #define XATTR_TRUSTED_T (2)
56 #define XATTR_SECURITY_T (3)
57 #define XATTR_ACL_ACCESS_T (4)
58 #define XATTR_ACL_DEFAULT_T (5)
59 #define XATTR_LUSTRE_T (6)
60 #define XATTR_OTHER_T (7)
63 int get_xattr_type(const char *name)
65 if (!strcmp(name, POSIX_ACL_XATTR_ACCESS))
66 return XATTR_ACL_ACCESS_T;
68 if (!strcmp(name, POSIX_ACL_XATTR_DEFAULT))
69 return XATTR_ACL_DEFAULT_T;
71 if (!strncmp(name, XATTR_USER_PREFIX,
72 sizeof(XATTR_USER_PREFIX) - 1))
75 if (!strncmp(name, XATTR_TRUSTED_PREFIX,
76 sizeof(XATTR_TRUSTED_PREFIX) - 1))
77 return XATTR_TRUSTED_T;
79 if (!strncmp(name, XATTR_SECURITY_PREFIX,
80 sizeof(XATTR_SECURITY_PREFIX) - 1))
81 return XATTR_SECURITY_T;
83 if (!strncmp(name, XATTR_LUSTRE_PREFIX,
84 sizeof(XATTR_LUSTRE_PREFIX) - 1))
85 return XATTR_LUSTRE_T;
91 int xattr_type_filter(struct ll_sb_info *sbi, int xattr_type)
93 if ((xattr_type == XATTR_ACL_ACCESS_T ||
94 xattr_type == XATTR_ACL_DEFAULT_T) &&
95 !(sbi->ll_flags & LL_SBI_ACL))
98 if (xattr_type == XATTR_USER_T && !(sbi->ll_flags & LL_SBI_USER_XATTR))
100 if (xattr_type == XATTR_TRUSTED_T && !cfs_capable(CFS_CAP_SYS_ADMIN))
102 if (xattr_type == XATTR_OTHER_T)
109 int ll_setxattr_common(struct inode *inode, const char *name,
110 const void *value, size_t size,
111 int flags, __u64 valid)
113 struct ll_sb_info *sbi = ll_i2sbi(inode);
114 struct ptlrpc_request *req;
117 posix_acl_xattr_header *new_value = NULL;
118 struct rmtacl_ctl_entry *rce = NULL;
119 ext_acl_xattr_header *acl = NULL;
120 const char *pv = value;
123 xattr_type = get_xattr_type(name);
124 rc = xattr_type_filter(sbi, xattr_type);
128 /* b10667: ignore lustre special xattr for now */
129 if ((xattr_type == XATTR_TRUSTED_T && strcmp(name, "trusted.lov") == 0) ||
130 (xattr_type == XATTR_LUSTRE_T && strcmp(name, "lustre.lov") == 0))
133 /* b15587: ignore security.capability xattr for now */
134 if ((xattr_type == XATTR_SECURITY_T &&
135 strcmp(name, "security.capability") == 0))
138 /* LU-549: Disable security.selinux when selinux is disabled */
139 if (xattr_type == XATTR_SECURITY_T && !selinux_is_enabled() &&
140 strcmp(name, "security.selinux") == 0)
143 #ifdef CONFIG_FS_POSIX_ACL
144 if (sbi->ll_flags & LL_SBI_RMT_CLIENT &&
145 (xattr_type == XATTR_ACL_ACCESS_T ||
146 xattr_type == XATTR_ACL_DEFAULT_T)) {
147 rce = rct_search(&sbi->ll_rct, cfs_curproc_pid());
149 (rce->rce_ops != RMT_LSETFACL &&
150 rce->rce_ops != RMT_RSETFACL))
153 if (rce->rce_ops == RMT_LSETFACL) {
154 struct eacl_entry *ee;
156 ee = et_search_del(&sbi->ll_et, cfs_curproc_pid(),
157 ll_inode2fid(inode), xattr_type);
159 if (valid & OBD_MD_FLXATTR) {
160 acl = lustre_acl_xattr_merge2ext(
161 (posix_acl_xattr_header *)value,
165 RETURN(PTR_ERR(acl));
167 size = CFS_ACL_XATTR_SIZE(\
168 le32_to_cpu(acl->a_count), \
170 pv = (const char *)acl;
173 } else if (rce->rce_ops == RMT_RSETFACL) {
174 size = lustre_posix_acl_xattr_filter(
175 (posix_acl_xattr_header *)value,
177 if (unlikely(size < 0))
180 pv = (const char *)new_value;
184 valid |= rce_ops2valid(rce->rce_ops);
187 oc = ll_mdscapa_get(inode);
188 rc = md_setxattr(sbi->ll_md_exp, ll_inode2fid(inode), oc,
189 valid, name, pv, size, 0, flags, ll_i2suppgid(inode),
192 #ifdef CONFIG_FS_POSIX_ACL
193 if (new_value != NULL)
194 lustre_posix_acl_xattr_free(new_value, size);
196 lustre_ext_acl_xattr_free(acl);
199 if (rc == -EOPNOTSUPP && xattr_type == XATTR_USER_T) {
200 LCONSOLE_INFO("Disabling user_xattr feature because "
201 "it is not supported on the server\n");
202 sbi->ll_flags &= ~LL_SBI_USER_XATTR;
207 ptlrpc_req_finished(req);
211 int ll_setxattr(struct dentry *dentry, const char *name,
212 const void *value, size_t size, int flags)
214 struct inode *inode = dentry->d_inode;
219 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), xattr %s\n",
220 inode->i_ino, inode->i_generation, inode, name);
222 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_SETXATTR, 1);
224 if ((strncmp(name, XATTR_TRUSTED_PREFIX,
225 sizeof(XATTR_TRUSTED_PREFIX) - 1) == 0 &&
226 strcmp(name + sizeof(XATTR_TRUSTED_PREFIX) - 1, "lov") == 0) ||
227 (strncmp(name, XATTR_LUSTRE_PREFIX,
228 sizeof(XATTR_LUSTRE_PREFIX) - 1) == 0 &&
229 strcmp(name + sizeof(XATTR_LUSTRE_PREFIX) - 1, "lov") == 0)) {
230 struct lov_user_md *lump = (struct lov_user_md *)value;
233 /* Attributes that are saved via getxattr will always have
234 * the stripe_offset as 0. Instead, the MDS should be
235 * allowed to pick the starting OST index. b=17846 */
236 if (lump != NULL && lump->lmm_stripe_offset == 0)
237 lump->lmm_stripe_offset = -1;
239 if (lump != NULL && S_ISREG(inode->i_mode)) {
241 int flags = FMODE_WRITE;
244 rc = ll_lov_setstripe_ea_info(inode, &f, flags,
245 lump, sizeof(*lump));
246 /* b10667: rc always be 0 here for now */
248 } else if (S_ISDIR(inode->i_mode)) {
249 rc = ll_dir_setstripe(inode, lump, 0);
254 } else if (strcmp(name, XATTR_NAME_LMA) == 0 ||
255 strcmp(name, XATTR_NAME_LINK) == 0)
258 return ll_setxattr_common(inode, name, value, size, flags,
262 int ll_removexattr(struct dentry *dentry, const char *name)
264 struct inode *inode = dentry->d_inode;
269 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), xattr %s\n",
270 inode->i_ino, inode->i_generation, inode, name);
272 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_REMOVEXATTR, 1);
273 return ll_setxattr_common(inode, name, NULL, 0, 0,
278 int ll_getxattr_common(struct inode *inode, const char *name,
279 void *buffer, size_t size, __u64 valid)
281 struct ll_sb_info *sbi = ll_i2sbi(inode);
282 struct ptlrpc_request *req = NULL;
283 struct mdt_body *body;
287 struct rmtacl_ctl_entry *rce = NULL;
290 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n",
291 inode->i_ino, inode->i_generation, inode);
293 /* listxattr have slightly different behavior from of ext3:
294 * without 'user_xattr' ext3 will list all xattr names but
295 * filtered out "^user..*"; we list them all for simplicity.
298 xattr_type = XATTR_OTHER_T;
302 xattr_type = get_xattr_type(name);
303 rc = xattr_type_filter(sbi, xattr_type);
307 /* b15587: ignore security.capability xattr for now */
308 if ((xattr_type == XATTR_SECURITY_T &&
309 strcmp(name, "security.capability") == 0))
312 /* LU-549: Disable security.selinux when selinux is disabled */
313 if (xattr_type == XATTR_SECURITY_T && !selinux_is_enabled() &&
314 strcmp(name, "security.selinux") == 0)
317 #ifdef CONFIG_FS_POSIX_ACL
318 if (sbi->ll_flags & LL_SBI_RMT_CLIENT &&
319 (xattr_type == XATTR_ACL_ACCESS_T ||
320 xattr_type == XATTR_ACL_DEFAULT_T)) {
321 rce = rct_search(&sbi->ll_rct, cfs_curproc_pid());
323 (rce->rce_ops != RMT_LSETFACL &&
324 rce->rce_ops != RMT_LGETFACL &&
325 rce->rce_ops != RMT_RSETFACL &&
326 rce->rce_ops != RMT_RGETFACL))
330 /* posix acl is under protection of LOOKUP lock. when calling to this,
331 * we just have path resolution to the target inode, so we have great
332 * chance that cached ACL is uptodate.
334 if (xattr_type == XATTR_ACL_ACCESS_T &&
335 !(sbi->ll_flags & LL_SBI_RMT_CLIENT)) {
336 struct ll_inode_info *lli = ll_i2info(inode);
337 struct posix_acl *acl;
339 spin_lock(&lli->lli_lock);
340 acl = posix_acl_dup(lli->lli_posix_acl);
341 spin_unlock(&lli->lli_lock);
346 rc = posix_acl_to_xattr(acl, buffer, size);
347 posix_acl_release(acl);
350 if (xattr_type == XATTR_ACL_DEFAULT_T && !S_ISDIR(inode->i_mode))
355 oc = ll_mdscapa_get(inode);
356 rc = md_getxattr(sbi->ll_md_exp, ll_inode2fid(inode), oc,
357 valid | (rce ? rce_ops2valid(rce->rce_ops) : 0),
358 name, NULL, 0, size, 0, &req);
361 if (rc == -EOPNOTSUPP && xattr_type == XATTR_USER_T) {
362 LCONSOLE_INFO("Disabling user_xattr feature because "
363 "it is not supported on the server\n");
364 sbi->ll_flags &= ~LL_SBI_USER_XATTR;
369 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
372 /* only detect the xattr size */
374 GOTO(out, rc = body->eadatasize);
376 if (size < body->eadatasize) {
377 CERROR("server bug: replied size %u > %u\n",
378 body->eadatasize, (int)size);
379 GOTO(out, rc = -ERANGE);
382 if (body->eadatasize == 0)
383 GOTO(out, rc = -ENODATA);
385 /* do not need swab xattr data */
386 xdata = req_capsule_server_sized_get(&req->rq_pill, &RMF_EADATA,
389 GOTO(out, rc = -EFAULT);
391 #ifdef CONFIG_FS_POSIX_ACL
392 if (body->eadatasize >= 0 && rce && rce->rce_ops == RMT_LSETFACL) {
393 ext_acl_xattr_header *acl;
395 acl = lustre_posix_acl_xattr_2ext((posix_acl_xattr_header *)xdata,
398 GOTO(out, rc = PTR_ERR(acl));
400 rc = ee_add(&sbi->ll_et, cfs_curproc_pid(), ll_inode2fid(inode),
402 if (unlikely(rc < 0)) {
403 lustre_ext_acl_xattr_free(acl);
409 if (body->eadatasize == 0) {
413 memcpy(buffer, xdata, body->eadatasize);
414 rc = body->eadatasize;
418 ptlrpc_req_finished(req);
422 ssize_t ll_getxattr(struct dentry *dentry, const char *name,
423 void *buffer, size_t size)
425 struct inode *inode = dentry->d_inode;
430 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), xattr %s\n",
431 inode->i_ino, inode->i_generation, inode, name);
433 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_GETXATTR, 1);
435 if ((strncmp(name, XATTR_TRUSTED_PREFIX,
436 sizeof(XATTR_TRUSTED_PREFIX) - 1) == 0 &&
437 strcmp(name + sizeof(XATTR_TRUSTED_PREFIX) - 1, "lov") == 0) ||
438 (strncmp(name, XATTR_LUSTRE_PREFIX,
439 sizeof(XATTR_LUSTRE_PREFIX) - 1) == 0 &&
440 strcmp(name + sizeof(XATTR_LUSTRE_PREFIX) - 1, "lov") == 0)) {
441 struct lov_stripe_md *lsm;
442 struct lov_user_md *lump;
443 struct lov_mds_md *lmm = NULL;
444 struct ptlrpc_request *request = NULL;
445 int rc = 0, lmmsize = 0;
447 if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
450 if (size == 0 && S_ISDIR(inode->i_mode)) {
451 /* XXX directory EA is fix for now, optimize to save
453 GOTO(out, rc = sizeof(struct lov_user_md));
456 lsm = ccc_inode_lsm_get(inode);
458 if (S_ISDIR(inode->i_mode)) {
459 rc = ll_dir_getstripe(inode, &lmm,
465 /* LSM is present already after lookup/getattr call.
466 * we need to grab layout lock once it is implemented */
467 rc = obd_packmd(ll_i2dtexp(inode), &lmm, lsm);
470 ccc_inode_lsm_put(inode, lsm);
476 /* used to call ll_get_max_mdsize() forward to get
477 * the maximum buffer size, while some apps (such as
478 * rsync 3.0.x) care much about the exact xattr value
484 if (size < lmmsize) {
485 CERROR("server bug: replied size %d > %d for %s (%s)\n",
486 lmmsize, (int)size, dentry->d_name.name, name);
487 GOTO(out, rc = -ERANGE);
490 lump = (struct lov_user_md *)buffer;
491 memcpy(lump, lmm, lmmsize);
496 ptlrpc_req_finished(request);
498 obd_free_diskmd(ll_i2dtexp(inode), &lmm);
502 return ll_getxattr_common(inode, name, buffer, size, OBD_MD_FLXATTR);
505 ssize_t ll_listxattr(struct dentry *dentry, char *buffer, size_t size)
507 struct inode *inode = dentry->d_inode;
509 struct lov_mds_md *lmm = NULL;
510 struct ptlrpc_request *request = NULL;
515 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n",
516 inode->i_ino, inode->i_generation, inode);
518 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_LISTXATTR, 1);
520 rc = ll_getxattr_common(inode, NULL, buffer, size, OBD_MD_FLXATTRLS);
524 if (buffer != NULL) {
525 struct ll_sb_info *sbi = ll_i2sbi(inode);
526 char *xattr_name = buffer;
530 xlen = strnlen(xattr_name, rem - 1) + 1;
532 if (xattr_type_filter(sbi,
533 get_xattr_type(xattr_name)) == 0) {
534 /* skip OK xattr type
540 /* move up remaining xattrs in buffer
541 * removing the xattr that is not OK
543 memmove(xattr_name, xattr_name + xlen, rem);
547 if (S_ISREG(inode->i_mode)) {
548 if (!ll_i2info(inode)->lli_has_smd)
550 } else if (S_ISDIR(inode->i_mode)) {
551 rc2 = ll_dir_getstripe(inode, &lmm, &lmmsize, &request);
556 } else if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)) {
557 const int prefix_len = sizeof(XATTR_LUSTRE_PREFIX) - 1;
558 const size_t name_len = sizeof("lov") - 1;
559 const size_t total_len = prefix_len + name_len + 1;
561 if (buffer && (rc + total_len) <= size) {
563 memcpy(buffer, XATTR_LUSTRE_PREFIX, prefix_len);
564 memcpy(buffer + prefix_len, "lov", name_len);
565 buffer[prefix_len + name_len] = '\0';
570 ptlrpc_req_finished(request);