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 if (S_ISREG(inode->i_mode)) {
224 int flags = FMODE_WRITE;
227 rc = ll_lov_setstripe_ea_info(inode, &f, flags,
228 lump, sizeof(*lump));
229 /* b10667: rc always be 0 here for now */
231 } else if (S_ISDIR(inode->i_mode)) {
232 rc = ll_dir_setstripe(inode, lump, 0);
237 } else if (strcmp(name, XATTR_NAME_LMA) == 0 ||
238 strcmp(name, XATTR_NAME_LINK) == 0)
241 return ll_setxattr_common(inode, name, value, size, flags,
245 int ll_removexattr(struct dentry *dentry, const char *name)
247 struct inode *inode = dentry->d_inode;
252 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), xattr %s\n",
253 inode->i_ino, inode->i_generation, inode, name);
255 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_REMOVEXATTR, 1);
256 return ll_setxattr_common(inode, name, NULL, 0, 0,
261 int ll_getxattr_common(struct inode *inode, const char *name,
262 void *buffer, size_t size, __u64 valid)
264 struct ll_sb_info *sbi = ll_i2sbi(inode);
265 struct ptlrpc_request *req = NULL;
266 struct mdt_body *body;
270 struct rmtacl_ctl_entry *rce = NULL;
273 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n",
274 inode->i_ino, inode->i_generation, inode);
276 /* listxattr have slightly different behavior from of ext3:
277 * without 'user_xattr' ext3 will list all xattr names but
278 * filtered out "^user..*"; we list them all for simplicity.
281 xattr_type = XATTR_OTHER_T;
285 xattr_type = get_xattr_type(name);
286 rc = xattr_type_filter(sbi, xattr_type);
290 #ifdef CONFIG_FS_POSIX_ACL
291 if (sbi->ll_flags & LL_SBI_RMT_CLIENT &&
292 (xattr_type == XATTR_ACL_ACCESS_T ||
293 xattr_type == XATTR_ACL_DEFAULT_T)) {
294 rce = rct_search(&sbi->ll_rct, cfs_curproc_pid());
296 (rce->rce_ops != RMT_LSETFACL &&
297 rce->rce_ops != RMT_LGETFACL &&
298 rce->rce_ops != RMT_RSETFACL &&
299 rce->rce_ops != RMT_RGETFACL))
304 /* posix acl is under protection of LOOKUP lock. when calling to this,
305 * we just have path resolution to the target inode, so we have great
306 * chance that cached ACL is uptodate.
308 #ifdef CONFIG_FS_POSIX_ACL
309 if (xattr_type == XATTR_ACL_ACCESS_T &&
310 !(sbi->ll_flags & LL_SBI_RMT_CLIENT)) {
311 struct ll_inode_info *lli = ll_i2info(inode);
312 struct posix_acl *acl;
314 spin_lock(&lli->lli_lock);
315 acl = posix_acl_dup(lli->lli_posix_acl);
316 spin_unlock(&lli->lli_lock);
321 rc = posix_acl_to_xattr(acl, buffer, size);
322 posix_acl_release(acl);
325 if (xattr_type == XATTR_ACL_DEFAULT_T && !S_ISDIR(inode->i_mode))
330 oc = ll_mdscapa_get(inode);
331 rc = md_getxattr(sbi->ll_md_exp, ll_inode2fid(inode), oc,
332 valid | (rce ? rce_ops2valid(rce->rce_ops) : 0),
333 name, NULL, 0, size, 0, &req);
336 if (rc == -EOPNOTSUPP && xattr_type == XATTR_USER_T) {
337 LCONSOLE_INFO("Disabling user_xattr feature because "
338 "it is not supported on the server\n");
339 sbi->ll_flags &= ~LL_SBI_USER_XATTR;
344 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
347 /* only detect the xattr size */
349 GOTO(out, rc = body->eadatasize);
351 if (size < body->eadatasize) {
352 CERROR("server bug: replied size %u > %u\n",
353 body->eadatasize, (int)size);
354 GOTO(out, rc = -ERANGE);
357 /* do not need swab xattr data */
358 xdata = req_capsule_server_sized_get(&req->rq_pill, &RMF_EADATA,
361 GOTO(out, rc = -EFAULT);
363 #ifdef CONFIG_FS_POSIX_ACL
364 if (body->eadatasize >= 0 && rce && rce->rce_ops == RMT_LSETFACL) {
365 ext_acl_xattr_header *acl;
367 acl = lustre_posix_acl_xattr_2ext((posix_acl_xattr_header *)xdata,
370 GOTO(out, rc = PTR_ERR(acl));
372 rc = ee_add(&sbi->ll_et, cfs_curproc_pid(), ll_inode2fid(inode),
374 if (unlikely(rc < 0)) {
375 lustre_ext_acl_xattr_free(acl);
380 if (xattr_type == XATTR_ACL_ACCESS_T && !body->eadatasize)
381 GOTO(out, rc = -ENODATA);
384 memcpy(buffer, xdata, body->eadatasize);
385 rc = body->eadatasize;
388 ptlrpc_req_finished(req);
392 ssize_t ll_getxattr(struct dentry *dentry, const char *name,
393 void *buffer, size_t size)
395 struct inode *inode = dentry->d_inode;
400 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), xattr %s\n",
401 inode->i_ino, inode->i_generation, inode, name);
403 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_GETXATTR, 1);
405 if ((strncmp(name, XATTR_TRUSTED_PREFIX,
406 sizeof(XATTR_TRUSTED_PREFIX) - 1) == 0 &&
407 strcmp(name + sizeof(XATTR_TRUSTED_PREFIX) - 1, "lov") == 0) ||
408 (strncmp(name, XATTR_LUSTRE_PREFIX,
409 sizeof(XATTR_LUSTRE_PREFIX) - 1) == 0 &&
410 strcmp(name + sizeof(XATTR_LUSTRE_PREFIX) - 1, "lov") == 0)) {
411 struct lov_user_md *lump;
412 struct lov_mds_md *lmm = NULL;
413 struct ptlrpc_request *request = NULL;
414 int rc = 0, lmmsize = 0;
416 if (S_ISREG(inode->i_mode)) {
417 rc = ll_lov_getstripe_ea_info(dentry->d_parent->d_inode,
418 dentry->d_name.name, &lmm,
420 } else if (S_ISDIR(inode->i_mode)) {
421 rc = ll_dir_getstripe(inode, &lmm, &lmmsize, &request);
429 GOTO(out, rc = lmmsize);
431 if (size < lmmsize) {
432 CERROR("server bug: replied size %d > %d for %s (%s)\n",
433 lmmsize, (int)size, dentry->d_name.name, name);
434 GOTO(out, rc = -ERANGE);
437 lump = (struct lov_user_md *)buffer;
438 memcpy(lump, lmm, lmmsize);
442 ptlrpc_req_finished(request);
446 return ll_getxattr_common(inode, name, buffer, size, OBD_MD_FLXATTR);
449 ssize_t ll_listxattr(struct dentry *dentry, char *buffer, size_t size)
451 struct inode *inode = dentry->d_inode;
453 struct lov_mds_md *lmm = NULL;
454 struct ptlrpc_request *request = NULL;
459 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n",
460 inode->i_ino, inode->i_generation, inode);
462 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_LISTXATTR, 1);
464 rc = ll_getxattr_common(inode, NULL, buffer, size, OBD_MD_FLXATTRLS);
466 if (S_ISREG(inode->i_mode)) {
467 struct ll_inode_info *lli = ll_i2info(inode);
468 struct lov_stripe_md *lsm = NULL;
472 } else if (S_ISDIR(inode->i_mode)) {
473 rc2 = ll_dir_getstripe(inode, &lmm, &lmmsize, &request);
479 const int prefix_len = sizeof(XATTR_LUSTRE_PREFIX) - 1;
480 const size_t name_len = sizeof("lov") - 1;
481 const size_t total_len = prefix_len + name_len + 1;
483 if (buffer && (rc + total_len) <= size) {
485 memcpy(buffer,XATTR_LUSTRE_PREFIX, prefix_len);
486 memcpy(buffer+prefix_len, "lov", name_len);
487 buffer[prefix_len + name_len] = '\0';
492 ptlrpc_req_finished(request);