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.
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/smp_lock.h>
42 #define DEBUG_SUBSYSTEM S_LLITE
44 #include <obd_support.h>
45 #include <lustre_lite.h>
46 #include <lustre_dlm.h>
47 #include <lustre_ver.h>
48 //#include <lustre_mdc.h>
49 #include <lustre_acl.h>
51 #include "llite_internal.h"
53 #define XATTR_USER_T (1)
54 #define XATTR_TRUSTED_T (2)
55 #define XATTR_SECURITY_T (3)
56 #define XATTR_ACL_ACCESS_T (4)
57 #define XATTR_ACL_DEFAULT_T (5)
58 #define XATTR_LUSTRE_T (6)
59 #define XATTR_OTHER_T (7)
62 int get_xattr_type(const char *name)
64 if (!strcmp(name, POSIX_ACL_XATTR_ACCESS))
65 return XATTR_ACL_ACCESS_T;
67 if (!strcmp(name, POSIX_ACL_XATTR_DEFAULT))
68 return XATTR_ACL_DEFAULT_T;
70 if (!strncmp(name, XATTR_USER_PREFIX,
71 sizeof(XATTR_USER_PREFIX) - 1))
74 if (!strncmp(name, XATTR_TRUSTED_PREFIX,
75 sizeof(XATTR_TRUSTED_PREFIX) - 1))
76 return XATTR_TRUSTED_T;
78 if (!strncmp(name, XATTR_SECURITY_PREFIX,
79 sizeof(XATTR_SECURITY_PREFIX) - 1))
80 return XATTR_SECURITY_T;
82 if (!strncmp(name, XATTR_LUSTRE_PREFIX,
83 sizeof(XATTR_LUSTRE_PREFIX) - 1))
84 return XATTR_LUSTRE_T;
90 int xattr_type_filter(struct ll_sb_info *sbi, int xattr_type)
92 if ((xattr_type == XATTR_ACL_ACCESS_T ||
93 xattr_type == XATTR_ACL_DEFAULT_T) &&
94 !(sbi->ll_flags & LL_SBI_ACL))
97 if (xattr_type == XATTR_USER_T && !(sbi->ll_flags & LL_SBI_USER_XATTR))
99 if (xattr_type == XATTR_TRUSTED_T && !cfs_capable(CFS_CAP_SYS_ADMIN))
101 if (xattr_type == XATTR_OTHER_T)
108 int ll_setxattr_common(struct inode *inode, const char *name,
109 const void *value, size_t size,
110 int flags, __u64 valid)
112 struct ll_sb_info *sbi = ll_i2sbi(inode);
113 struct ptlrpc_request *req;
116 posix_acl_xattr_header *new_value = NULL;
117 struct rmtacl_ctl_entry *rce = NULL;
118 ext_acl_xattr_header *acl = NULL;
119 const char *pv = value;
122 xattr_type = get_xattr_type(name);
123 rc = xattr_type_filter(sbi, xattr_type);
127 /* b10667: ignore lustre special xattr for now */
128 if ((xattr_type == XATTR_TRUSTED_T && strcmp(name, "trusted.lov") == 0) ||
129 (xattr_type == XATTR_LUSTRE_T && strcmp(name, "lustre.lov") == 0))
132 /* b15587: ignore security.capability xattr for now */
133 if ((xattr_type == XATTR_SECURITY_T &&
134 strcmp(name, "security.capability") == 0))
137 #ifdef CONFIG_FS_POSIX_ACL
138 if (sbi->ll_flags & LL_SBI_RMT_CLIENT &&
139 (xattr_type == XATTR_ACL_ACCESS_T ||
140 xattr_type == XATTR_ACL_DEFAULT_T)) {
141 rce = rct_search(&sbi->ll_rct, cfs_curproc_pid());
143 (rce->rce_ops != RMT_LSETFACL &&
144 rce->rce_ops != RMT_RSETFACL))
147 if (rce->rce_ops == RMT_LSETFACL) {
148 struct eacl_entry *ee;
150 ee = et_search_del(&sbi->ll_et, cfs_curproc_pid(),
151 ll_inode2fid(inode), xattr_type);
153 if (valid & OBD_MD_FLXATTR) {
154 acl = lustre_acl_xattr_merge2ext(
155 (posix_acl_xattr_header *)value,
159 RETURN(PTR_ERR(acl));
161 size = CFS_ACL_XATTR_SIZE(\
162 le32_to_cpu(acl->a_count), \
164 pv = (const char *)acl;
167 } else if (rce->rce_ops == RMT_RSETFACL) {
168 size = lustre_posix_acl_xattr_filter(
169 (posix_acl_xattr_header *)value,
171 if (unlikely(size < 0))
174 pv = (const char *)new_value;
178 valid |= rce_ops2valid(rce->rce_ops);
181 oc = ll_mdscapa_get(inode);
182 rc = md_setxattr(sbi->ll_md_exp, ll_inode2fid(inode), oc,
183 valid, name, pv, size, 0, flags, ll_i2suppgid(inode),
186 #ifdef CONFIG_FS_POSIX_ACL
187 if (new_value != NULL)
188 lustre_posix_acl_xattr_free(new_value, size);
190 lustre_ext_acl_xattr_free(acl);
193 if (rc == -EOPNOTSUPP && xattr_type == XATTR_USER_T) {
194 LCONSOLE_INFO("Disabling user_xattr feature because "
195 "it is not supported on the server\n");
196 sbi->ll_flags &= ~LL_SBI_USER_XATTR;
201 ptlrpc_req_finished(req);
205 int ll_setxattr(struct dentry *dentry, const char *name,
206 const void *value, size_t size, int flags)
208 struct inode *inode = dentry->d_inode;
213 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), xattr %s\n",
214 inode->i_ino, inode->i_generation, inode, name);
216 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_SETXATTR, 1);
218 if ((strncmp(name, XATTR_TRUSTED_PREFIX,
219 sizeof(XATTR_TRUSTED_PREFIX) - 1) == 0 &&
220 strcmp(name + sizeof(XATTR_TRUSTED_PREFIX) - 1, "lov") == 0) ||
221 (strncmp(name, XATTR_LUSTRE_PREFIX,
222 sizeof(XATTR_LUSTRE_PREFIX) - 1) == 0 &&
223 strcmp(name + sizeof(XATTR_LUSTRE_PREFIX) - 1, "lov") == 0)) {
224 struct lov_user_md *lump = (struct lov_user_md *)value;
227 /* Attributes that are saved via getxattr will always have
228 * the stripe_offset as 0. Instead, the MDS should be
229 * allowed to pick the starting OST index. b=17846 */
230 if (lump != NULL && lump->lmm_stripe_offset == 0)
231 lump->lmm_stripe_offset = -1;
233 if (lump != NULL && S_ISREG(inode->i_mode)) {
235 int flags = FMODE_WRITE;
238 rc = ll_lov_setstripe_ea_info(inode, &f, flags,
239 lump, sizeof(*lump));
240 /* b10667: rc always be 0 here for now */
242 } else if (S_ISDIR(inode->i_mode)) {
243 rc = ll_dir_setstripe(inode, lump, 0);
248 } else if (strcmp(name, XATTR_NAME_LMA) == 0 ||
249 strcmp(name, XATTR_NAME_LINK) == 0)
252 return ll_setxattr_common(inode, name, value, size, flags,
256 int ll_removexattr(struct dentry *dentry, const char *name)
258 struct inode *inode = dentry->d_inode;
263 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), xattr %s\n",
264 inode->i_ino, inode->i_generation, inode, name);
266 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_REMOVEXATTR, 1);
267 return ll_setxattr_common(inode, name, NULL, 0, 0,
272 int ll_getxattr_common(struct inode *inode, const char *name,
273 void *buffer, size_t size, __u64 valid)
275 struct ll_sb_info *sbi = ll_i2sbi(inode);
276 struct ptlrpc_request *req = NULL;
277 struct mdt_body *body;
281 struct rmtacl_ctl_entry *rce = NULL;
284 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n",
285 inode->i_ino, inode->i_generation, inode);
287 /* listxattr have slightly different behavior from of ext3:
288 * without 'user_xattr' ext3 will list all xattr names but
289 * filtered out "^user..*"; we list them all for simplicity.
292 xattr_type = XATTR_OTHER_T;
296 xattr_type = get_xattr_type(name);
297 rc = xattr_type_filter(sbi, xattr_type);
301 /* b15587: ignore security.capability xattr for now */
302 if ((xattr_type == XATTR_SECURITY_T &&
303 strcmp(name, "security.capability") == 0))
306 #ifdef CONFIG_FS_POSIX_ACL
307 if (sbi->ll_flags & LL_SBI_RMT_CLIENT &&
308 (xattr_type == XATTR_ACL_ACCESS_T ||
309 xattr_type == XATTR_ACL_DEFAULT_T)) {
310 rce = rct_search(&sbi->ll_rct, cfs_curproc_pid());
312 (rce->rce_ops != RMT_LSETFACL &&
313 rce->rce_ops != RMT_LGETFACL &&
314 rce->rce_ops != RMT_RSETFACL &&
315 rce->rce_ops != RMT_RGETFACL))
319 /* posix acl is under protection of LOOKUP lock. when calling to this,
320 * we just have path resolution to the target inode, so we have great
321 * chance that cached ACL is uptodate.
323 if (xattr_type == XATTR_ACL_ACCESS_T &&
324 !(sbi->ll_flags & LL_SBI_RMT_CLIENT)) {
325 struct ll_inode_info *lli = ll_i2info(inode);
326 struct posix_acl *acl;
328 cfs_spin_lock(&lli->lli_lock);
329 acl = posix_acl_dup(lli->lli_posix_acl);
330 cfs_spin_unlock(&lli->lli_lock);
335 rc = posix_acl_to_xattr(acl, buffer, size);
336 posix_acl_release(acl);
339 if (xattr_type == XATTR_ACL_DEFAULT_T && !S_ISDIR(inode->i_mode))
344 oc = ll_mdscapa_get(inode);
345 rc = md_getxattr(sbi->ll_md_exp, ll_inode2fid(inode), oc,
346 valid | (rce ? rce_ops2valid(rce->rce_ops) : 0),
347 name, NULL, 0, size, 0, &req);
350 if (rc == -EOPNOTSUPP && xattr_type == XATTR_USER_T) {
351 LCONSOLE_INFO("Disabling user_xattr feature because "
352 "it is not supported on the server\n");
353 sbi->ll_flags &= ~LL_SBI_USER_XATTR;
358 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
361 /* only detect the xattr size */
363 GOTO(out, rc = body->eadatasize);
365 if (size < body->eadatasize) {
366 CERROR("server bug: replied size %u > %u\n",
367 body->eadatasize, (int)size);
368 GOTO(out, rc = -ERANGE);
371 if (body->eadatasize == 0)
372 GOTO(out, rc = -ENODATA);
374 /* do not need swab xattr data */
375 xdata = req_capsule_server_sized_get(&req->rq_pill, &RMF_EADATA,
378 GOTO(out, rc = -EFAULT);
380 #ifdef CONFIG_FS_POSIX_ACL
381 if (body->eadatasize >= 0 && rce && rce->rce_ops == RMT_LSETFACL) {
382 ext_acl_xattr_header *acl;
384 acl = lustre_posix_acl_xattr_2ext((posix_acl_xattr_header *)xdata,
387 GOTO(out, rc = PTR_ERR(acl));
389 rc = ee_add(&sbi->ll_et, cfs_curproc_pid(), ll_inode2fid(inode),
391 if (unlikely(rc < 0)) {
392 lustre_ext_acl_xattr_free(acl);
398 if (body->eadatasize == 0) {
402 memcpy(buffer, xdata, body->eadatasize);
403 rc = body->eadatasize;
407 ptlrpc_req_finished(req);
411 ssize_t ll_getxattr(struct dentry *dentry, const char *name,
412 void *buffer, size_t size)
414 struct inode *inode = dentry->d_inode;
419 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), xattr %s\n",
420 inode->i_ino, inode->i_generation, inode, name);
422 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_GETXATTR, 1);
424 if ((strncmp(name, XATTR_TRUSTED_PREFIX,
425 sizeof(XATTR_TRUSTED_PREFIX) - 1) == 0 &&
426 strcmp(name + sizeof(XATTR_TRUSTED_PREFIX) - 1, "lov") == 0) ||
427 (strncmp(name, XATTR_LUSTRE_PREFIX,
428 sizeof(XATTR_LUSTRE_PREFIX) - 1) == 0 &&
429 strcmp(name + sizeof(XATTR_LUSTRE_PREFIX) - 1, "lov") == 0)) {
430 struct lov_user_md *lump;
431 struct lov_mds_md *lmm = NULL;
432 struct ptlrpc_request *request = NULL;
433 int rc = 0, lmmsize = 0;
435 if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
438 if (size == 0 && S_ISDIR(inode->i_mode)) {
439 /* XXX directory EA is fix for now, optimize to save
441 GOTO(out, rc = sizeof(struct lov_user_md));
444 if (!ll_i2info(inode)->lli_smd) {
445 if (S_ISDIR(inode->i_mode)) {
446 rc = ll_dir_getstripe(inode, &lmm,
452 /* LSM is present already after lookup/getattr call.
453 * we need to grab layout lock once it is implemented */
454 rc = obd_packmd(ll_i2dtexp(inode), &lmm,
455 ll_i2info(inode)->lli_smd);
463 /* used to call ll_get_max_mdsize() forward to get
464 * the maximum buffer size, while some apps (such as
465 * rsync 3.0.x) care much about the exact xattr value
471 if (size < lmmsize) {
472 CERROR("server bug: replied size %d > %d for %s (%s)\n",
473 lmmsize, (int)size, dentry->d_name.name, name);
474 GOTO(out, rc = -ERANGE);
477 lump = (struct lov_user_md *)buffer;
478 memcpy(lump, lmm, lmmsize);
483 ptlrpc_req_finished(request);
485 obd_free_diskmd(ll_i2dtexp(inode), &lmm);
489 return ll_getxattr_common(inode, name, buffer, size, OBD_MD_FLXATTR);
492 ssize_t ll_listxattr(struct dentry *dentry, char *buffer, size_t size)
494 struct inode *inode = dentry->d_inode;
496 struct lov_mds_md *lmm = NULL;
497 struct ptlrpc_request *request = NULL;
502 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n",
503 inode->i_ino, inode->i_generation, inode);
505 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_LISTXATTR, 1);
507 rc = ll_getxattr_common(inode, NULL, buffer, size, OBD_MD_FLXATTRLS);
511 if (S_ISREG(inode->i_mode)) {
512 struct ll_inode_info *lli = ll_i2info(inode);
513 struct lov_stripe_md *lsm = NULL;
517 } else if (S_ISDIR(inode->i_mode)) {
518 rc2 = ll_dir_getstripe(inode, &lmm, &lmmsize, &request);
523 } else if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)) {
524 const int prefix_len = sizeof(XATTR_LUSTRE_PREFIX) - 1;
525 const size_t name_len = sizeof("lov") - 1;
526 const size_t total_len = prefix_len + name_len + 1;
528 if (buffer && (rc + total_len) <= size) {
530 memcpy(buffer,XATTR_LUSTRE_PREFIX, prefix_len);
531 memcpy(buffer+prefix_len, "lov", name_len);
532 buffer[prefix_len + name_len] = '\0';
537 ptlrpc_req_finished(request);