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 2008 Sun Microsystems, Inc. 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 #ifdef CONFIG_FS_POSIX_ACL
133 if (sbi->ll_flags & LL_SBI_RMT_CLIENT &&
134 (xattr_type == XATTR_ACL_ACCESS_T ||
135 xattr_type == XATTR_ACL_DEFAULT_T)) {
136 rce = rct_search(&sbi->ll_rct, cfs_curproc_pid());
138 (rce->rce_ops != RMT_LSETFACL &&
139 rce->rce_ops != RMT_RSETFACL))
142 if (rce->rce_ops == RMT_LSETFACL) {
143 struct eacl_entry *ee;
145 ee = et_search_del(&sbi->ll_et, cfs_curproc_pid(),
146 ll_inode2fid(inode), xattr_type);
148 if (valid & OBD_MD_FLXATTR) {
149 acl = lustre_acl_xattr_merge2ext(
150 (posix_acl_xattr_header *)value,
154 RETURN(PTR_ERR(acl));
156 size = CFS_ACL_XATTR_SIZE(\
157 le32_to_cpu(acl->a_count), \
159 pv = (const char *)acl;
162 } else if (rce->rce_ops == RMT_RSETFACL) {
163 size = lustre_posix_acl_xattr_filter(
164 (posix_acl_xattr_header *)value,
166 if (unlikely(size < 0))
169 pv = (const char *)new_value;
173 valid |= rce_ops2valid(rce->rce_ops);
176 oc = ll_mdscapa_get(inode);
177 rc = md_setxattr(sbi->ll_md_exp, ll_inode2fid(inode), oc,
178 valid, name, pv, size, 0, flags, ll_i2suppgid(inode),
181 #ifdef CONFIG_FS_POSIX_ACL
182 if (new_value != NULL)
183 lustre_posix_acl_xattr_free(new_value, size);
185 lustre_ext_acl_xattr_free(acl);
188 if (rc == -EOPNOTSUPP && xattr_type == XATTR_USER_T) {
189 LCONSOLE_INFO("Disabling user_xattr feature because "
190 "it is not supported on the server\n");
191 sbi->ll_flags &= ~LL_SBI_USER_XATTR;
196 ptlrpc_req_finished(req);
200 int ll_setxattr(struct dentry *dentry, const char *name,
201 const void *value, size_t size, int flags)
203 struct inode *inode = dentry->d_inode;
208 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), xattr %s\n",
209 inode->i_ino, inode->i_generation, inode, name);
211 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_SETXATTR, 1);
213 if ((strncmp(name, XATTR_TRUSTED_PREFIX,
214 sizeof(XATTR_TRUSTED_PREFIX) - 1) == 0 &&
215 strcmp(name + sizeof(XATTR_TRUSTED_PREFIX) - 1, "lov") == 0) ||
216 (strncmp(name, XATTR_LUSTRE_PREFIX,
217 sizeof(XATTR_LUSTRE_PREFIX) - 1) == 0 &&
218 strcmp(name + sizeof(XATTR_LUSTRE_PREFIX) - 1, "lov") == 0)) {
219 struct lov_user_md *lump = (struct lov_user_md *)value;
222 /* Attributes that are saved via getxattr will always have
223 * the stripe_offset as 0. Instead, the MDS should be
224 * allowed to pick the starting OST index. b=17846 */
225 if (lump->lmm_stripe_offset == 0)
226 lump->lmm_stripe_offset = -1;
228 if (S_ISREG(inode->i_mode)) {
230 int flags = FMODE_WRITE;
233 rc = ll_lov_setstripe_ea_info(inode, &f, flags,
234 lump, sizeof(*lump));
235 /* b10667: rc always be 0 here for now */
237 } else if (S_ISDIR(inode->i_mode)) {
238 rc = ll_dir_setstripe(inode, lump, 0);
243 } else if (strcmp(name, XATTR_NAME_LMA) == 0 ||
244 strcmp(name, XATTR_NAME_LINK) == 0)
247 return ll_setxattr_common(inode, name, value, size, flags,
251 int ll_removexattr(struct dentry *dentry, const char *name)
253 struct inode *inode = dentry->d_inode;
258 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), xattr %s\n",
259 inode->i_ino, inode->i_generation, inode, name);
261 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_REMOVEXATTR, 1);
262 return ll_setxattr_common(inode, name, NULL, 0, 0,
267 int ll_getxattr_common(struct inode *inode, const char *name,
268 void *buffer, size_t size, __u64 valid)
270 struct ll_sb_info *sbi = ll_i2sbi(inode);
271 struct ptlrpc_request *req = NULL;
272 struct mdt_body *body;
276 struct rmtacl_ctl_entry *rce = NULL;
279 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n",
280 inode->i_ino, inode->i_generation, inode);
282 /* listxattr have slightly different behavior from of ext3:
283 * without 'user_xattr' ext3 will list all xattr names but
284 * filtered out "^user..*"; we list them all for simplicity.
287 xattr_type = XATTR_OTHER_T;
291 xattr_type = get_xattr_type(name);
292 rc = xattr_type_filter(sbi, xattr_type);
296 #ifdef CONFIG_FS_POSIX_ACL
297 if (sbi->ll_flags & LL_SBI_RMT_CLIENT &&
298 (xattr_type == XATTR_ACL_ACCESS_T ||
299 xattr_type == XATTR_ACL_DEFAULT_T)) {
300 rce = rct_search(&sbi->ll_rct, cfs_curproc_pid());
302 (rce->rce_ops != RMT_LSETFACL &&
303 rce->rce_ops != RMT_LGETFACL &&
304 rce->rce_ops != RMT_RSETFACL &&
305 rce->rce_ops != RMT_RGETFACL))
310 /* posix acl is under protection of LOOKUP lock. when calling to this,
311 * we just have path resolution to the target inode, so we have great
312 * chance that cached ACL is uptodate.
314 #ifdef CONFIG_FS_POSIX_ACL
315 if (xattr_type == XATTR_ACL_ACCESS_T &&
316 !(sbi->ll_flags & LL_SBI_RMT_CLIENT)) {
317 struct ll_inode_info *lli = ll_i2info(inode);
318 struct posix_acl *acl;
320 spin_lock(&lli->lli_lock);
321 acl = posix_acl_dup(lli->lli_posix_acl);
322 spin_unlock(&lli->lli_lock);
327 rc = posix_acl_to_xattr(acl, buffer, size);
328 posix_acl_release(acl);
331 if (xattr_type == XATTR_ACL_DEFAULT_T && !S_ISDIR(inode->i_mode))
336 oc = ll_mdscapa_get(inode);
337 rc = md_getxattr(sbi->ll_md_exp, ll_inode2fid(inode), oc,
338 valid | (rce ? rce_ops2valid(rce->rce_ops) : 0),
339 name, NULL, 0, size, 0, &req);
342 if (rc == -EOPNOTSUPP && xattr_type == XATTR_USER_T) {
343 LCONSOLE_INFO("Disabling user_xattr feature because "
344 "it is not supported on the server\n");
345 sbi->ll_flags &= ~LL_SBI_USER_XATTR;
350 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
353 /* only detect the xattr size */
355 GOTO(out, rc = body->eadatasize);
357 if (size < body->eadatasize) {
358 CERROR("server bug: replied size %u > %u\n",
359 body->eadatasize, (int)size);
360 GOTO(out, rc = -ERANGE);
363 /* do not need swab xattr data */
364 xdata = req_capsule_server_sized_get(&req->rq_pill, &RMF_EADATA,
367 GOTO(out, rc = -EFAULT);
369 #ifdef CONFIG_FS_POSIX_ACL
370 if (body->eadatasize >= 0 && rce && rce->rce_ops == RMT_LSETFACL) {
371 ext_acl_xattr_header *acl;
373 acl = lustre_posix_acl_xattr_2ext((posix_acl_xattr_header *)xdata,
376 GOTO(out, rc = PTR_ERR(acl));
378 rc = ee_add(&sbi->ll_et, cfs_curproc_pid(), ll_inode2fid(inode),
380 if (unlikely(rc < 0)) {
381 lustre_ext_acl_xattr_free(acl);
386 if (xattr_type == XATTR_ACL_ACCESS_T && !body->eadatasize)
387 GOTO(out, rc = -ENODATA);
390 memcpy(buffer, xdata, body->eadatasize);
391 rc = body->eadatasize;
394 ptlrpc_req_finished(req);
398 ssize_t ll_getxattr(struct dentry *dentry, const char *name,
399 void *buffer, size_t size)
401 struct inode *inode = dentry->d_inode;
406 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), xattr %s\n",
407 inode->i_ino, inode->i_generation, inode, name);
409 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_GETXATTR, 1);
411 if ((strncmp(name, XATTR_TRUSTED_PREFIX,
412 sizeof(XATTR_TRUSTED_PREFIX) - 1) == 0 &&
413 strcmp(name + sizeof(XATTR_TRUSTED_PREFIX) - 1, "lov") == 0) ||
414 (strncmp(name, XATTR_LUSTRE_PREFIX,
415 sizeof(XATTR_LUSTRE_PREFIX) - 1) == 0 &&
416 strcmp(name + sizeof(XATTR_LUSTRE_PREFIX) - 1, "lov") == 0)) {
417 struct lov_user_md *lump;
418 struct lov_mds_md *lmm = NULL;
419 struct ptlrpc_request *request = NULL;
420 int rc = 0, lmmsize = 0;
422 if (S_ISREG(inode->i_mode)) {
423 rc = ll_lov_getstripe_ea_info(dentry->d_parent->d_inode,
424 dentry->d_name.name, &lmm,
426 } else if (S_ISDIR(inode->i_mode)) {
427 rc = ll_dir_getstripe(inode, &lmm, &lmmsize, &request);
435 GOTO(out, rc = lmmsize);
437 if (size < lmmsize) {
438 CERROR("server bug: replied size %d > %d for %s (%s)\n",
439 lmmsize, (int)size, dentry->d_name.name, name);
440 GOTO(out, rc = -ERANGE);
443 lump = (struct lov_user_md *)buffer;
444 memcpy(lump, lmm, lmmsize);
448 ptlrpc_req_finished(request);
452 return ll_getxattr_common(inode, name, buffer, size, OBD_MD_FLXATTR);
455 ssize_t ll_listxattr(struct dentry *dentry, char *buffer, size_t size)
457 struct inode *inode = dentry->d_inode;
459 struct lov_mds_md *lmm = NULL;
460 struct ptlrpc_request *request = NULL;
465 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n",
466 inode->i_ino, inode->i_generation, inode);
468 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_LISTXATTR, 1);
470 rc = ll_getxattr_common(inode, NULL, buffer, size, OBD_MD_FLXATTRLS);
472 if (S_ISREG(inode->i_mode)) {
473 struct ll_inode_info *lli = ll_i2info(inode);
474 struct lov_stripe_md *lsm = NULL;
478 } else if (S_ISDIR(inode->i_mode)) {
479 rc2 = ll_dir_getstripe(inode, &lmm, &lmmsize, &request);
485 const int prefix_len = sizeof(XATTR_LUSTRE_PREFIX) - 1;
486 const size_t name_len = sizeof("lov") - 1;
487 const size_t total_len = prefix_len + name_len + 1;
489 if (buffer && (rc + total_len) <= size) {
491 memcpy(buffer,XATTR_LUSTRE_PREFIX, prefix_len);
492 memcpy(buffer+prefix_len, "lov", name_len);
493 buffer[prefix_len + name_len] = '\0';
498 ptlrpc_req_finished(request);