1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (c) 2004 - 2005 Cluster File Systems, Inc.
6 * This file is part of Lustre, http://www.lustre.org.
8 * Lustre is free software; you can redistribute it and/or
9 * modify it under the terms of version 2 of the GNU General Public
10 * License as published by the Free Software Foundation.
12 * Lustre is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Lustre; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include <linux/sched.h>
25 #include <linux/smp_lock.h>
27 #define DEBUG_SUBSYSTEM S_LLITE
29 #include <obd_support.h>
30 #include <lustre_lite.h>
31 #include <lustre_dlm.h>
32 #include <linux/lustre_version.h>
34 #ifndef POSIX_ACL_XATTR_ACCESS
35 #ifndef XATTR_NAME_ACL_ACCESS
36 #define XATTR_NAME_ACL_ACCESS "system.posix_acl_access"
38 #define POSIX_ACL_XATTR_ACCESS XATTR_NAME_ACL_ACCESS
40 #ifndef POSIX_ACL_XATTR_DEFAULT
41 #ifndef XATTR_NAME_ACL_DEFAULT
42 #define XATTR_NAME_ACL_DEFAULT "system.posix_acl_default"
44 #define POSIX_ACL_XATTR_DEFAULT XATTR_NAME_ACL_DEFAULT
47 #include "llite_internal.h"
49 #define XATTR_USER_PREFIX "user."
50 #define XATTR_TRUSTED_PREFIX "trusted."
51 #define XATTR_SECURITY_PREFIX "security."
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_OTHER_T (6)
61 int get_xattr_type(const char *name)
63 if (!strcmp(name, POSIX_ACL_XATTR_ACCESS))
64 return XATTR_ACL_ACCESS_T;
66 if (!strcmp(name, POSIX_ACL_XATTR_DEFAULT))
67 return XATTR_ACL_DEFAULT_T;
69 if (!strncmp(name, XATTR_USER_PREFIX,
70 sizeof(XATTR_USER_PREFIX) - 1))
73 if (!strncmp(name, XATTR_TRUSTED_PREFIX,
74 sizeof(XATTR_TRUSTED_PREFIX) - 1))
75 return XATTR_TRUSTED_T;
77 if (!strncmp(name, XATTR_SECURITY_PREFIX,
78 sizeof(XATTR_SECURITY_PREFIX) - 1))
79 return XATTR_SECURITY_T;
85 int xattr_type_filter(struct ll_sb_info *sbi, int xattr_type)
87 if ((xattr_type == XATTR_ACL_ACCESS_T ||
88 xattr_type == XATTR_ACL_DEFAULT_T) &&
89 !(sbi->ll_flags & LL_SBI_ACL))
92 if (xattr_type == XATTR_USER_T && !(sbi->ll_flags & LL_SBI_USER_XATTR))
94 if (xattr_type == XATTR_TRUSTED_T && !capable(CAP_SYS_ADMIN))
96 if (xattr_type == XATTR_OTHER_T)
103 int ll_setxattr_common(struct inode *inode, const char *name,
104 const void *value, size_t size,
105 int flags, __u64 valid)
107 struct ll_sb_info *sbi = ll_i2sbi(inode);
108 struct ptlrpc_request *req;
114 xattr_type = get_xattr_type(name);
115 rc = xattr_type_filter(sbi, xattr_type);
119 /* b10667: ignore lustre special xattr for now */
120 if (xattr_type == XATTR_TRUSTED_T && strcmp(name, "trusted.lov") == 0)
123 ll_inode2fid(&fid, inode);
124 rc = mdc_setxattr(sbi->ll_mdc_exp, &fid, valid,
125 name, value, size, 0, flags, &req);
127 if (rc == -EOPNOTSUPP && xattr_type == XATTR_USER_T) {
128 LCONSOLE_INFO("Disabling user_xattr feature because "
129 "it is not supported on the server\n");
130 sbi->ll_flags &= ~LL_SBI_USER_XATTR;
135 ptlrpc_req_finished(req);
139 int ll_setxattr(struct dentry *dentry, const char *name,
140 const void *value, size_t size, int flags)
142 struct inode *inode = dentry->d_inode;
147 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), xattr %s\n",
148 inode->i_ino, inode->i_generation, inode, name);
150 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_SETXATTR, 1);
152 if (strncmp(name, XATTR_TRUSTED_PREFIX, 8) == 0 &&
153 strcmp(name + 8, "lov") == 0) {
154 struct lov_user_md *lump = (struct lov_user_md *)value;
157 if (S_ISREG(inode->i_mode)) {
159 int flags = FMODE_WRITE;
162 rc = ll_lov_setstripe_ea_info(inode, &f, flags,
163 lump, sizeof(*lump));
164 /* b10667: rc always be 0 here for now */
166 } else if (S_ISDIR(inode->i_mode)) {
167 rc = ll_dir_setstripe(inode, lump);
173 return ll_setxattr_common(inode, name, value, size, flags,
177 int ll_removexattr(struct dentry *dentry, const char *name)
179 struct inode *inode = dentry->d_inode;
184 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), xattr %s\n",
185 inode->i_ino, inode->i_generation, inode, name);
187 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_REMOVEXATTR, 1);
188 return ll_setxattr_common(inode, name, NULL, 0, 0,
193 int ll_getxattr_common(struct inode *inode, const char *name,
194 void *buffer, size_t size, __u64 valid)
196 struct ll_sb_info *sbi = ll_i2sbi(inode);
197 struct ptlrpc_request *req = NULL;
198 struct mds_body *body;
204 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n",
205 inode->i_ino, inode->i_generation, inode);
208 /* listxattr have slightly different behavior from of ext3:
209 * without 'user_xattr' ext3 will list all xattr names but
210 * filtered out "^user..*"; we list them all for simplicity.
213 xattr_type = XATTR_OTHER_T;
217 xattr_type = get_xattr_type(name);
218 rc = xattr_type_filter(sbi, xattr_type);
222 /* posix acl is under protection of LOOKUP lock. when calling to this,
223 * we just have path resolution to the target inode, so we have great
224 * chance that cached ACL is uptodate.
226 #ifdef CONFIG_FS_POSIX_ACL
227 if (xattr_type == XATTR_ACL_ACCESS_T) {
228 struct ll_inode_info *lli = ll_i2info(inode);
229 struct posix_acl *acl;
231 spin_lock(&lli->lli_lock);
232 acl = posix_acl_dup(lli->lli_posix_acl);
233 spin_unlock(&lli->lli_lock);
238 rc = posix_acl_to_xattr(acl, buffer, size);
239 posix_acl_release(acl);
245 ll_inode2fid(&fid, inode);
246 rc = mdc_getxattr(sbi->ll_mdc_exp, &fid, valid, name, NULL, 0, size,
249 if (rc == -EOPNOTSUPP && xattr_type == XATTR_USER_T) {
250 LCONSOLE_INFO("Disabling user_xattr feature because "
251 "it is not supported on the server\n");
252 sbi->ll_flags &= ~LL_SBI_USER_XATTR;
257 body = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF, sizeof(*body));
259 LASSERT_REPSWABBED(req, REPLY_REC_OFF);
261 /* only detect the xattr size */
263 GOTO(out, rc = body->eadatasize);
265 if (size < body->eadatasize) {
266 CERROR("server bug: replied size %u > %u\n",
267 body->eadatasize, (int)size);
268 GOTO(out, rc = -ERANGE);
271 if (lustre_msg_bufcount(req->rq_repmsg) < 3) {
272 CERROR("reply bufcount %u\n",
273 lustre_msg_bufcount(req->rq_repmsg));
274 GOTO(out, rc = -EFAULT);
277 /* do not need swab xattr data */
278 LASSERT_REPSWAB(req, REPLY_REC_OFF + 1);
279 xdata = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF + 1,
282 CERROR("can't extract: %u : %u\n", body->eadatasize,
283 lustre_msg_buflen(req->rq_repmsg, REPLY_REC_OFF + 1));
284 GOTO(out, rc = -EFAULT);
288 memcpy(buffer, xdata, body->eadatasize);
289 rc = body->eadatasize;
291 ptlrpc_req_finished(req);
295 ssize_t ll_getxattr(struct dentry *dentry, const char *name,
296 void *buffer, size_t size)
298 struct inode *inode = dentry->d_inode;
303 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), xattr %s\n",
304 inode->i_ino, inode->i_generation, inode, name);
306 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_GETXATTR, 1);
308 if (strncmp(name, XATTR_TRUSTED_PREFIX, 8) == 0 &&
309 strcmp(name + 8, "lov") == 0) {
310 struct lov_user_md *lump;
311 struct lov_mds_md *lmm = NULL;
312 struct ptlrpc_request *request = NULL;
315 if (S_ISREG(inode->i_mode)) {
316 rc = ll_lov_getstripe_ea_info(dentry->d_parent->d_inode,
317 dentry->d_name.name, &lmm,
319 } else if (S_ISDIR(inode->i_mode)) {
320 rc = ll_dir_getstripe(inode, &lmm, &lmmsize, &request);
326 GOTO(out, rc = lmmsize);
328 if (size < lmmsize) {
329 CERROR("server bug: replied size %u > %u\n",
331 GOTO(out, rc = -ERANGE);
334 lump = (struct lov_user_md *)buffer;
335 memcpy(lump, lmm, lmmsize);
339 ptlrpc_req_finished(request);
343 return ll_getxattr_common(inode, name, buffer, size, OBD_MD_FLXATTR);
346 ssize_t ll_listxattr(struct dentry *dentry, char *buffer, size_t size)
348 struct inode *inode = dentry->d_inode;
353 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n",
354 inode->i_ino, inode->i_generation, inode);
356 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_LISTXATTR, 1);
358 rc = ll_getxattr_common(inode, NULL, buffer, size, OBD_MD_FLXATTRLS);
360 if (!capable(CAP_SYS_ADMIN)) {
361 struct lov_mds_md *lmm = NULL;
362 struct ptlrpc_request *request = NULL;
365 if (S_ISREG(inode->i_mode)) {
366 struct ll_inode_info *lli = ll_i2info(inode);
367 struct lov_stripe_md *lsm = NULL;
371 } else if (S_ISDIR(inode->i_mode)) {
372 rc2 = ll_dir_getstripe(inode, &lmm, &lmmsize, &request);
378 const int prefix_len = sizeof(XATTR_TRUSTED_PREFIX)-1;
379 const size_t name_len = sizeof("lov") - 1;
380 const size_t total_len = prefix_len + name_len + 1;
382 if (buffer && (rc + total_len) <= size) {
384 memcpy(buffer,XATTR_TRUSTED_PREFIX, prefix_len);
385 memcpy(buffer+prefix_len, "lov", name_len);
386 buffer[prefix_len + name_len] = '\0';
391 ptlrpc_req_finished(request);