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 <linux/lustre_version.h>
49 #ifndef POSIX_ACL_XATTR_ACCESS
50 #ifndef XATTR_NAME_ACL_ACCESS
51 #define XATTR_NAME_ACL_ACCESS "system.posix_acl_access"
53 #define POSIX_ACL_XATTR_ACCESS XATTR_NAME_ACL_ACCESS
55 #ifndef POSIX_ACL_XATTR_DEFAULT
56 #ifndef XATTR_NAME_ACL_DEFAULT
57 #define XATTR_NAME_ACL_DEFAULT "system.posix_acl_default"
59 #define POSIX_ACL_XATTR_DEFAULT XATTR_NAME_ACL_DEFAULT
62 #include "llite_internal.h"
64 #define XATTR_USER_PREFIX "user."
65 #define XATTR_TRUSTED_PREFIX "trusted."
66 #define XATTR_SECURITY_PREFIX "security."
67 #define XATTR_LUSTRE_PREFIX "lustre."
69 #define XATTR_USER_T (1)
70 #define XATTR_TRUSTED_T (2)
71 #define XATTR_SECURITY_T (3)
72 #define XATTR_ACL_ACCESS_T (4)
73 #define XATTR_ACL_DEFAULT_T (5)
74 #define XATTR_LUSTRE_T (6)
75 #define XATTR_OTHER_T (7)
78 int get_xattr_type(const char *name)
80 if (!strcmp(name, POSIX_ACL_XATTR_ACCESS))
81 return XATTR_ACL_ACCESS_T;
83 if (!strcmp(name, POSIX_ACL_XATTR_DEFAULT))
84 return XATTR_ACL_DEFAULT_T;
86 if (!strncmp(name, XATTR_USER_PREFIX,
87 sizeof(XATTR_USER_PREFIX) - 1))
90 if (!strncmp(name, XATTR_TRUSTED_PREFIX,
91 sizeof(XATTR_TRUSTED_PREFIX) - 1))
92 return XATTR_TRUSTED_T;
94 if (!strncmp(name, XATTR_SECURITY_PREFIX,
95 sizeof(XATTR_SECURITY_PREFIX) - 1))
96 return XATTR_SECURITY_T;
98 if (!strncmp(name, XATTR_LUSTRE_PREFIX,
99 sizeof(XATTR_LUSTRE_PREFIX) - 1))
100 return XATTR_LUSTRE_T;
102 return XATTR_OTHER_T;
106 int xattr_type_filter(struct ll_sb_info *sbi, int xattr_type)
108 if ((xattr_type == XATTR_ACL_ACCESS_T ||
109 xattr_type == XATTR_ACL_DEFAULT_T) &&
110 !(sbi->ll_flags & LL_SBI_ACL))
113 if (xattr_type == XATTR_USER_T && !(sbi->ll_flags & LL_SBI_USER_XATTR))
115 if (xattr_type == XATTR_TRUSTED_T && !cfs_capable(CFS_CAP_SYS_ADMIN))
117 if (xattr_type == XATTR_OTHER_T)
124 int ll_setxattr_common(struct inode *inode, const char *name,
125 const void *value, size_t size,
126 int flags, __u64 valid)
128 struct ll_sb_info *sbi = ll_i2sbi(inode);
129 struct ptlrpc_request *req;
135 xattr_type = get_xattr_type(name);
136 rc = xattr_type_filter(sbi, xattr_type);
140 /* b10667: ignore lustre special xattr for now */
141 if ((xattr_type == XATTR_TRUSTED_T && strcmp(name, "trusted.lov") == 0) ||
142 (xattr_type == XATTR_LUSTRE_T && strcmp(name, "lustre.lov") == 0))
145 ll_inode2fid(&fid, inode);
146 rc = mdc_setxattr(sbi->ll_mdc_exp, &fid, valid,
147 name, value, size, 0, flags, &req);
149 if (rc == -EOPNOTSUPP && xattr_type == XATTR_USER_T) {
150 LCONSOLE_INFO("Disabling user_xattr feature because "
151 "it is not supported on the server\n");
152 sbi->ll_flags &= ~LL_SBI_USER_XATTR;
157 ptlrpc_req_finished(req);
161 int ll_setxattr(struct dentry *dentry, const char *name,
162 const void *value, size_t size, int flags)
164 struct inode *inode = dentry->d_inode;
169 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), xattr %s\n",
170 inode->i_ino, inode->i_generation, inode, name);
172 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_SETXATTR, 1);
174 if ((strncmp(name, XATTR_TRUSTED_PREFIX,
175 sizeof(XATTR_TRUSTED_PREFIX) - 1) == 0 &&
176 strcmp(name + sizeof(XATTR_TRUSTED_PREFIX) - 1, "lov") == 0) ||
177 (strncmp(name, XATTR_LUSTRE_PREFIX,
178 sizeof(XATTR_LUSTRE_PREFIX) - 1) == 0 &&
179 strcmp(name + sizeof(XATTR_LUSTRE_PREFIX) - 1, "lov") == 0)) {
180 struct lov_user_md *lump = (struct lov_user_md *)value;
183 /* Attributes that are saved via getxattr will always have
184 * the stripe_offset as 0. Instead, the MDS should be
185 * allowed to pick the starting OST index. b=17846 */
186 if (lump->lmm_stripe_offset == 0)
187 lump->lmm_stripe_offset = -1;
189 if (S_ISREG(inode->i_mode)) {
191 int flags = FMODE_WRITE;
194 rc = ll_lov_setstripe_ea_info(inode, &f, flags,
195 lump, sizeof(*lump));
196 /* b10667: rc always be 0 here for now */
198 } else if (S_ISDIR(inode->i_mode)) {
199 rc = ll_dir_setstripe(inode, lump, 0);
203 } else if (strcmp(name, "trusted.lma") == 0 &&
204 !OBD_FAIL_CHECK(OBD_FAIL_MDS_ALLOW_COMMON_EA_SETTING))
208 return ll_setxattr_common(inode, name, value, size, flags,
212 int ll_removexattr(struct dentry *dentry, const char *name)
214 struct inode *inode = dentry->d_inode;
219 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), xattr %s\n",
220 inode->i_ino, inode->i_generation, inode, name);
222 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_REMOVEXATTR, 1);
223 return ll_setxattr_common(inode, name, NULL, 0, 0,
228 int ll_getxattr_common(struct inode *inode, const char *name,
229 void *buffer, size_t size, __u64 valid)
231 struct ll_sb_info *sbi = ll_i2sbi(inode);
232 struct ptlrpc_request *req = NULL;
233 struct mds_body *body;
239 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n",
240 inode->i_ino, inode->i_generation, inode);
243 /* listxattr have slightly different behavior from of ext3:
244 * without 'user_xattr' ext3 will list all xattr names but
245 * filtered out "^user..*"; we list them all for simplicity.
248 xattr_type = XATTR_OTHER_T;
252 xattr_type = get_xattr_type(name);
253 rc = xattr_type_filter(sbi, xattr_type);
257 /* posix acl is under protection of LOOKUP lock. when calling to this,
258 * we just have path resolution to the target inode, so we have great
259 * chance that cached ACL is uptodate.
261 #ifdef CONFIG_FS_POSIX_ACL
262 if (xattr_type == XATTR_ACL_ACCESS_T) {
263 struct ll_inode_info *lli = ll_i2info(inode);
264 struct posix_acl *acl;
266 spin_lock(&lli->lli_lock);
267 acl = posix_acl_dup(lli->lli_posix_acl);
268 spin_unlock(&lli->lli_lock);
273 rc = posix_acl_to_xattr(acl, buffer, size);
274 posix_acl_release(acl);
277 if (xattr_type == XATTR_ACL_DEFAULT_T && !S_ISDIR(inode->i_mode))
282 ll_inode2fid(&fid, inode);
283 rc = mdc_getxattr(sbi->ll_mdc_exp, &fid, valid, name, NULL, 0, size,
286 if (rc == -EOPNOTSUPP && xattr_type == XATTR_USER_T) {
287 LCONSOLE_INFO("Disabling user_xattr feature because "
288 "it is not supported on the server\n");
289 sbi->ll_flags &= ~LL_SBI_USER_XATTR;
294 body = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF, sizeof(*body));
296 LASSERT(lustre_rep_swabbed(req, REPLY_REC_OFF));
298 /* only detect the xattr size */
300 GOTO(out, rc = body->eadatasize);
302 if (size < body->eadatasize) {
303 CERROR("server bug: replied size %u > %u\n",
304 body->eadatasize, (int)size);
305 GOTO(out, rc = -ERANGE);
308 if (lustre_msg_bufcount(req->rq_repmsg) < 3) {
309 CERROR("reply bufcount %u\n",
310 lustre_msg_bufcount(req->rq_repmsg));
311 GOTO(out, rc = -EFAULT);
314 /* do not need swab xattr data */
315 lustre_set_rep_swabbed(req, REPLY_REC_OFF + 1);
316 xdata = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF + 1,
319 CERROR("can't extract: %u : %u\n", body->eadatasize,
320 lustre_msg_buflen(req->rq_repmsg, REPLY_REC_OFF + 1));
321 GOTO(out, rc = -EFAULT);
325 memcpy(buffer, xdata, body->eadatasize);
326 rc = body->eadatasize;
328 ptlrpc_req_finished(req);
332 ssize_t ll_getxattr(struct dentry *dentry, const char *name,
333 void *buffer, size_t size)
335 struct inode *inode = dentry->d_inode;
340 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), xattr %s\n",
341 inode->i_ino, inode->i_generation, inode, name);
343 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_GETXATTR, 1);
345 if ((strncmp(name, XATTR_TRUSTED_PREFIX,
346 sizeof(XATTR_TRUSTED_PREFIX) - 1) == 0 &&
347 strcmp(name + sizeof(XATTR_TRUSTED_PREFIX) - 1, "lov") == 0) ||
348 (strncmp(name, XATTR_LUSTRE_PREFIX,
349 sizeof(XATTR_LUSTRE_PREFIX) - 1) == 0 &&
350 strcmp(name + sizeof(XATTR_LUSTRE_PREFIX) - 1, "lov") == 0)) {
351 struct lov_user_md *lump;
352 struct lov_mds_md *lmm = NULL;
353 struct ptlrpc_request *request = NULL;
354 int rc = 0, lmmsize = 0;
356 if (S_ISREG(inode->i_mode)) {
357 rc = ll_lov_getstripe_ea_info(dentry->d_parent->d_inode,
358 dentry->d_name.name, &lmm,
360 } else if (S_ISDIR(inode->i_mode)) {
361 rc = ll_dir_getstripe(inode, &lmm, &lmmsize, &request);
369 GOTO(out, rc = lmmsize);
371 if (size < lmmsize) {
372 CERROR("server bug: replied size %d > %d for %s (%s)\n",
373 lmmsize, (int)size, dentry->d_name.name, name);
374 GOTO(out, rc = -ERANGE);
377 lump = (struct lov_user_md *)buffer;
378 memcpy(lump, lmm, lmmsize);
382 ptlrpc_req_finished(request);
386 return ll_getxattr_common(inode, name, buffer, size, OBD_MD_FLXATTR);
389 ssize_t ll_listxattr(struct dentry *dentry, char *buffer, size_t size)
391 struct inode *inode = dentry->d_inode;
393 struct lov_mds_md *lmm = NULL;
394 struct ptlrpc_request *request = NULL;
399 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n",
400 inode->i_ino, inode->i_generation, inode);
402 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_LISTXATTR, 1);
404 rc = ll_getxattr_common(inode, NULL, buffer, size, OBD_MD_FLXATTRLS);
406 if (S_ISREG(inode->i_mode)) {
407 struct ll_inode_info *lli = ll_i2info(inode);
408 struct lov_stripe_md *lsm = NULL;
412 } else if (S_ISDIR(inode->i_mode)) {
413 rc2 = ll_dir_getstripe(inode, &lmm, &lmmsize, &request);
419 const int prefix_len = sizeof(XATTR_LUSTRE_PREFIX) - 1;
420 const size_t name_len = sizeof("lov") - 1;
421 const size_t total_len = prefix_len + name_len + 1;
423 if (buffer && (rc + total_len) <= size) {
425 memcpy(buffer,XATTR_LUSTRE_PREFIX, prefix_len);
426 memcpy(buffer+prefix_len, "lov", name_len);
427 buffer[prefix_len + name_len] = '\0';
432 ptlrpc_req_finished(request);