1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
32 * Copyright (c) 2011, 2012, Whamcloud, Inc.
35 * This file is part of Lustre, http://www.lustre.org/
36 * Lustre is a trademark of Sun Microsystems, Inc.
40 #include <linux/sched.h>
42 #include <linux/smp_lock.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 #ifdef CONFIG_FS_POSIX_ACL
139 if (sbi->ll_flags & LL_SBI_RMT_CLIENT &&
140 (xattr_type == XATTR_ACL_ACCESS_T ||
141 xattr_type == XATTR_ACL_DEFAULT_T)) {
142 rce = rct_search(&sbi->ll_rct, cfs_curproc_pid());
144 (rce->rce_ops != RMT_LSETFACL &&
145 rce->rce_ops != RMT_RSETFACL))
148 if (rce->rce_ops == RMT_LSETFACL) {
149 struct eacl_entry *ee;
151 ee = et_search_del(&sbi->ll_et, cfs_curproc_pid(),
152 ll_inode2fid(inode), xattr_type);
154 if (valid & OBD_MD_FLXATTR) {
155 acl = lustre_acl_xattr_merge2ext(
156 (posix_acl_xattr_header *)value,
160 RETURN(PTR_ERR(acl));
162 size = CFS_ACL_XATTR_SIZE(\
163 le32_to_cpu(acl->a_count), \
165 pv = (const char *)acl;
168 } else if (rce->rce_ops == RMT_RSETFACL) {
169 size = lustre_posix_acl_xattr_filter(
170 (posix_acl_xattr_header *)value,
172 if (unlikely(size < 0))
175 pv = (const char *)new_value;
179 valid |= rce_ops2valid(rce->rce_ops);
182 oc = ll_mdscapa_get(inode);
183 rc = md_setxattr(sbi->ll_md_exp, ll_inode2fid(inode), oc,
184 valid, name, pv, size, 0, flags, ll_i2suppgid(inode),
187 #ifdef CONFIG_FS_POSIX_ACL
188 if (new_value != NULL)
189 lustre_posix_acl_xattr_free(new_value, size);
191 lustre_ext_acl_xattr_free(acl);
194 if (rc == -EOPNOTSUPP && xattr_type == XATTR_USER_T) {
195 LCONSOLE_INFO("Disabling user_xattr feature because "
196 "it is not supported on the server\n");
197 sbi->ll_flags &= ~LL_SBI_USER_XATTR;
202 ptlrpc_req_finished(req);
206 int ll_setxattr(struct dentry *dentry, const char *name,
207 const void *value, size_t size, int flags)
209 struct inode *inode = dentry->d_inode;
214 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), xattr %s\n",
215 inode->i_ino, inode->i_generation, inode, name);
217 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_SETXATTR, 1);
219 if ((strncmp(name, XATTR_TRUSTED_PREFIX,
220 sizeof(XATTR_TRUSTED_PREFIX) - 1) == 0 &&
221 strcmp(name + sizeof(XATTR_TRUSTED_PREFIX) - 1, "lov") == 0) ||
222 (strncmp(name, XATTR_LUSTRE_PREFIX,
223 sizeof(XATTR_LUSTRE_PREFIX) - 1) == 0 &&
224 strcmp(name + sizeof(XATTR_LUSTRE_PREFIX) - 1, "lov") == 0)) {
225 struct lov_user_md *lump = (struct lov_user_md *)value;
228 /* Attributes that are saved via getxattr will always have
229 * the stripe_offset as 0. Instead, the MDS should be
230 * allowed to pick the starting OST index. b=17846 */
231 if (lump != NULL && lump->lmm_stripe_offset == 0)
232 lump->lmm_stripe_offset = -1;
234 if (lump != NULL && S_ISREG(inode->i_mode)) {
236 int flags = FMODE_WRITE;
239 rc = ll_lov_setstripe_ea_info(inode, &f, flags,
240 lump, sizeof(*lump));
241 /* b10667: rc always be 0 here for now */
243 } else if (S_ISDIR(inode->i_mode)) {
244 rc = ll_dir_setstripe(inode, lump, 0);
249 } else if (strcmp(name, XATTR_NAME_LMA) == 0 ||
250 strcmp(name, XATTR_NAME_LINK) == 0)
253 return ll_setxattr_common(inode, name, value, size, flags,
257 int ll_removexattr(struct dentry *dentry, const char *name)
259 struct inode *inode = dentry->d_inode;
264 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), xattr %s\n",
265 inode->i_ino, inode->i_generation, inode, name);
267 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_REMOVEXATTR, 1);
268 return ll_setxattr_common(inode, name, NULL, 0, 0,
273 int ll_getxattr_common(struct inode *inode, const char *name,
274 void *buffer, size_t size, __u64 valid)
276 struct ll_sb_info *sbi = ll_i2sbi(inode);
277 struct ptlrpc_request *req = NULL;
278 struct mdt_body *body;
282 struct rmtacl_ctl_entry *rce = NULL;
285 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n",
286 inode->i_ino, inode->i_generation, inode);
288 /* listxattr have slightly different behavior from of ext3:
289 * without 'user_xattr' ext3 will list all xattr names but
290 * filtered out "^user..*"; we list them all for simplicity.
293 xattr_type = XATTR_OTHER_T;
297 xattr_type = get_xattr_type(name);
298 rc = xattr_type_filter(sbi, xattr_type);
302 /* b15587: ignore security.capability xattr for now */
303 if ((xattr_type == XATTR_SECURITY_T &&
304 strcmp(name, "security.capability") == 0))
307 #ifdef CONFIG_FS_POSIX_ACL
308 if (sbi->ll_flags & LL_SBI_RMT_CLIENT &&
309 (xattr_type == XATTR_ACL_ACCESS_T ||
310 xattr_type == XATTR_ACL_DEFAULT_T)) {
311 rce = rct_search(&sbi->ll_rct, cfs_curproc_pid());
313 (rce->rce_ops != RMT_LSETFACL &&
314 rce->rce_ops != RMT_LGETFACL &&
315 rce->rce_ops != RMT_RSETFACL &&
316 rce->rce_ops != RMT_RGETFACL))
320 /* posix acl is under protection of LOOKUP lock. when calling to this,
321 * we just have path resolution to the target inode, so we have great
322 * chance that cached ACL is uptodate.
324 if (xattr_type == XATTR_ACL_ACCESS_T &&
325 !(sbi->ll_flags & LL_SBI_RMT_CLIENT)) {
326 struct ll_inode_info *lli = ll_i2info(inode);
327 struct posix_acl *acl;
329 cfs_spin_lock(&lli->lli_lock);
330 acl = posix_acl_dup(lli->lli_posix_acl);
331 cfs_spin_unlock(&lli->lli_lock);
336 rc = posix_acl_to_xattr(acl, buffer, size);
337 posix_acl_release(acl);
340 if (xattr_type == XATTR_ACL_DEFAULT_T && !S_ISDIR(inode->i_mode))
345 oc = ll_mdscapa_get(inode);
346 rc = md_getxattr(sbi->ll_md_exp, ll_inode2fid(inode), oc,
347 valid | (rce ? rce_ops2valid(rce->rce_ops) : 0),
348 name, NULL, 0, size, 0, &req);
351 if (rc == -EOPNOTSUPP && xattr_type == XATTR_USER_T) {
352 LCONSOLE_INFO("Disabling user_xattr feature because "
353 "it is not supported on the server\n");
354 sbi->ll_flags &= ~LL_SBI_USER_XATTR;
359 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
362 /* only detect the xattr size */
364 GOTO(out, rc = body->eadatasize);
366 if (size < body->eadatasize) {
367 CERROR("server bug: replied size %u > %u\n",
368 body->eadatasize, (int)size);
369 GOTO(out, rc = -ERANGE);
372 if (body->eadatasize == 0)
373 GOTO(out, rc = -ENODATA);
375 /* do not need swab xattr data */
376 xdata = req_capsule_server_sized_get(&req->rq_pill, &RMF_EADATA,
379 GOTO(out, rc = -EFAULT);
381 #ifdef CONFIG_FS_POSIX_ACL
382 if (body->eadatasize >= 0 && rce && rce->rce_ops == RMT_LSETFACL) {
383 ext_acl_xattr_header *acl;
385 acl = lustre_posix_acl_xattr_2ext((posix_acl_xattr_header *)xdata,
388 GOTO(out, rc = PTR_ERR(acl));
390 rc = ee_add(&sbi->ll_et, cfs_curproc_pid(), ll_inode2fid(inode),
392 if (unlikely(rc < 0)) {
393 lustre_ext_acl_xattr_free(acl);
399 if (body->eadatasize == 0) {
403 memcpy(buffer, xdata, body->eadatasize);
404 rc = body->eadatasize;
408 ptlrpc_req_finished(req);
412 ssize_t ll_getxattr(struct dentry *dentry, const char *name,
413 void *buffer, size_t size)
415 struct inode *inode = dentry->d_inode;
420 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), xattr %s\n",
421 inode->i_ino, inode->i_generation, inode, name);
423 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_GETXATTR, 1);
425 if ((strncmp(name, XATTR_TRUSTED_PREFIX,
426 sizeof(XATTR_TRUSTED_PREFIX) - 1) == 0 &&
427 strcmp(name + sizeof(XATTR_TRUSTED_PREFIX) - 1, "lov") == 0) ||
428 (strncmp(name, XATTR_LUSTRE_PREFIX,
429 sizeof(XATTR_LUSTRE_PREFIX) - 1) == 0 &&
430 strcmp(name + sizeof(XATTR_LUSTRE_PREFIX) - 1, "lov") == 0)) {
431 struct lov_user_md *lump;
432 struct lov_mds_md *lmm = NULL;
433 struct ptlrpc_request *request = NULL;
434 int rc = 0, lmmsize = 0;
436 if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
439 if (size == 0 && S_ISDIR(inode->i_mode)) {
440 /* XXX directory EA is fix for now, optimize to save
442 GOTO(out, rc = sizeof(struct lov_user_md));
445 if (!ll_i2info(inode)->lli_smd) {
446 if (S_ISDIR(inode->i_mode)) {
447 rc = ll_dir_getstripe(inode, &lmm,
453 /* LSM is present already after lookup/getattr call.
454 * we need to grab layout lock once it is implemented */
455 rc = obd_packmd(ll_i2dtexp(inode), &lmm,
456 ll_i2info(inode)->lli_smd);
464 /* used to call ll_get_max_mdsize() forward to get
465 * the maximum buffer size, while some apps (such as
466 * rsync 3.0.x) care much about the exact xattr value
472 if (size < lmmsize) {
473 CERROR("server bug: replied size %d > %d for %s (%s)\n",
474 lmmsize, (int)size, dentry->d_name.name, name);
475 GOTO(out, rc = -ERANGE);
478 lump = (struct lov_user_md *)buffer;
479 memcpy(lump, lmm, lmmsize);
484 ptlrpc_req_finished(request);
486 obd_free_diskmd(ll_i2dtexp(inode), &lmm);
490 return ll_getxattr_common(inode, name, buffer, size, OBD_MD_FLXATTR);
493 ssize_t ll_listxattr(struct dentry *dentry, char *buffer, size_t size)
495 struct inode *inode = dentry->d_inode;
497 struct lov_mds_md *lmm = NULL;
498 struct ptlrpc_request *request = NULL;
503 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n",
504 inode->i_ino, inode->i_generation, inode);
506 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_LISTXATTR, 1);
508 rc = ll_getxattr_common(inode, NULL, buffer, size, OBD_MD_FLXATTRLS);
512 if (S_ISREG(inode->i_mode)) {
513 if (ll_i2info(inode)->lli_smd == NULL)
515 } else if (S_ISDIR(inode->i_mode)) {
516 rc2 = ll_dir_getstripe(inode, &lmm, &lmmsize, &request);
521 } else if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)) {
522 const int prefix_len = sizeof(XATTR_LUSTRE_PREFIX) - 1;
523 const size_t name_len = sizeof("lov") - 1;
524 const size_t total_len = prefix_len + name_len + 1;
526 if (buffer && (rc + total_len) <= size) {
528 memcpy(buffer,XATTR_LUSTRE_PREFIX, prefix_len);
529 memcpy(buffer+prefix_len, "lov", name_len);
530 buffer[prefix_len + name_len] = '\0';
535 ptlrpc_req_finished(request);