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.
36 * lustre/mdt/mdt_xattr.c
38 * Lustre Metadata Target (mdt) extended attributes management.
40 * Author: Peter Braam <braam@clusterfs.com>
41 * Author: Andreas Dilger <adilger@clusterfs.com>
42 * Author: Phil Schwan <phil@clusterfs.com>
43 * Author: Huang Hua <huanghua@clusterfs.com>
47 # define EXPORT_SYMTAB
49 #define DEBUG_SUBSYSTEM S_MDS
51 /* prerequisite for linux/xattr.h */
52 #include <linux/types.h>
53 /* prerequisite for linux/xattr.h */
55 /* XATTR_{REPLACE,CREATE} */
56 #include <linux/xattr.h>
58 #include "mdt_internal.h"
61 /* return EADATA length to the caller. negative value means error */
62 static int mdt_getxattr_pack_reply(struct mdt_thread_info * info)
64 struct req_capsule *pill = info->mti_pill ;
65 struct ptlrpc_request *req = mdt_info_req(info);
67 __u64 valid = info->mti_body->valid;
68 static const char user_string[] = "user.";
72 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETXATTR_PACK))
75 /* Determine how many bytes we need */
76 if (valid & OBD_MD_FLXATTR) {
77 xattr_name = req_capsule_client_get(pill, &RMF_NAME);
81 if (!(req->rq_export->exp_connect_flags & OBD_CONNECT_XATTR) &&
82 !strncmp(xattr_name, user_string, sizeof(user_string) - 1))
85 size = mo_xattr_get(info->mti_env,
86 mdt_object_child(info->mti_object),
87 &LU_BUF_NULL, xattr_name);
88 } else if (valid & OBD_MD_FLXATTRLS) {
89 size = mo_xattr_list(info->mti_env,
90 mdt_object_child(info->mti_object),
93 CDEBUG(D_INFO, "Valid bits: "LPX64"\n", info->mti_body->valid);
100 else if (size != -EOPNOTSUPP) {
101 CDEBUG(D_INFO, "Error geting EA size: %d\n", size);
106 if (info->mti_body->eadatasize != 0 &&
107 info->mti_body->eadatasize < size)
110 req_capsule_set_size(pill, &RMF_EADATA, RCL_SERVER,
111 min_t(int, size, info->mti_body->eadatasize));
113 rc = req_capsule_server_pack(pill);
122 int mdt_getxattr(struct mdt_thread_info *info)
124 struct ptlrpc_request *req = mdt_info_req(info);
125 struct mdt_export_data *med = mdt_req2med(req);
126 struct md_ucred *uc = mdt_ucred(info);
127 struct mdt_body *reqbody;
128 struct mdt_body *repbody = NULL;
129 struct md_object *next;
131 __u32 remote = exp_connect_rmtclient(info->mti_exp);
136 LASSERT(info->mti_object != NULL);
137 LASSERT(lu_object_assert_exists(&info->mti_object->mot_obj.mo_lu));
139 CDEBUG(D_INODE, "getxattr "DFID"\n", PFID(&info->mti_body->fid1));
141 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
143 RETURN(err_serious(-EFAULT));
145 rc = mdt_init_ucred(info, reqbody);
147 RETURN(err_serious(rc));
149 next = mdt_object_child(info->mti_object);
151 if (info->mti_body->valid & OBD_MD_FLRMTRGETFACL) {
152 if (unlikely(!remote))
153 GOTO(out, rc = err_serious(-EINVAL));
155 perm = mdt_identity_get_perm(uc->mu_identity, remote,
157 if (!(perm & CFS_RMTACL_PERM))
158 GOTO(out, rc = err_serious(-EPERM));
160 rc = mo_permission(info->mti_env, NULL, next, NULL,
163 GOTO(out, rc = err_serious(rc));
166 easize = mdt_getxattr_pack_reply(info);
168 GOTO(out, rc = err_serious(easize));
170 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
171 LASSERT(repbody != NULL);
173 /* No need further getxattr. */
174 if (easize == 0 || reqbody->eadatasize == 0)
175 GOTO(out, rc = easize);
177 buf = &info->mti_buf;
178 buf->lb_buf = req_capsule_server_get(info->mti_pill, &RMF_EADATA);
179 buf->lb_len = easize;
181 if (info->mti_body->valid & OBD_MD_FLXATTR) {
182 int flags = CFS_IC_NOTHING;
183 char *xattr_name = req_capsule_client_get(info->mti_pill,
185 CDEBUG(D_INODE, "getxattr %s\n", xattr_name);
187 rc = mo_xattr_get(info->mti_env, next, buf, xattr_name);
189 CERROR("getxattr failed: %d\n", rc);
193 if (info->mti_body->valid &
194 (OBD_MD_FLRMTLSETFACL | OBD_MD_FLRMTLGETFACL))
196 else if (info->mti_body->valid & OBD_MD_FLRMTRGETFACL)
197 flags = CFS_IC_MAPPED;
199 if (rc > 0 && flags != CFS_IC_NOTHING) {
202 if (unlikely(!remote))
203 GOTO(out, rc = -EINVAL);
205 rc1 = lustre_posix_acl_xattr_id2client(uc,
207 (posix_acl_xattr_header *)(buf->lb_buf),
209 if (unlikely(rc1 < 0))
212 } else if (info->mti_body->valid & OBD_MD_FLXATTRLS) {
213 CDEBUG(D_INODE, "listxattr\n");
215 rc = mo_xattr_list(info->mti_env, next, buf);
217 CDEBUG(D_INFO, "listxattr failed: %d\n", rc);
224 repbody->eadatasize = rc;
227 mdt_exit_ucred(info);
231 static int mdt_rmtlsetfacl(struct mdt_thread_info *info,
232 struct md_object *next,
233 const char *xattr_name,
234 ext_acl_xattr_header *header,
235 posix_acl_xattr_header **out)
237 struct ptlrpc_request *req = mdt_info_req(info);
238 struct mdt_export_data *med = mdt_req2med(req);
239 struct md_ucred *uc = mdt_ucred(info);
240 struct lu_buf *buf = &info->mti_buf;
244 rc = lustre_ext_acl_xattr_id2server(uc, med->med_idmap, header);
248 rc = mo_xattr_get(info->mti_env, next, &LU_BUF_NULL, xattr_name);
255 if (buf->lb_len > 0) {
256 OBD_ALLOC(buf->lb_buf, buf->lb_len);
257 if (unlikely(buf->lb_buf == NULL))
260 rc = mo_xattr_get(info->mti_env, next, buf, xattr_name);
262 CERROR("getxattr failed: %d\n", rc);
268 rc = lustre_acl_xattr_merge2posix((posix_acl_xattr_header *)(buf->lb_buf),
269 buf->lb_len, header, out);
273 if (rc <= 0 && buf->lb_buf != NULL)
274 OBD_FREE(buf->lb_buf, buf->lb_len);
278 int mdt_reint_setxattr(struct mdt_thread_info *info,
279 struct mdt_lock_handle *unused)
281 struct ptlrpc_request *req = mdt_info_req(info);
282 struct md_ucred *uc = mdt_ucred(info);
283 struct mdt_lock_handle *lh;
284 struct req_capsule *pill = info->mti_pill;
285 const struct lu_env *env = info->mti_env;
286 struct lu_buf *buf = &info->mti_buf;
287 struct mdt_reint_record *rr = &info->mti_rr;
288 struct md_attr *ma = &info->mti_attr;
289 struct lu_attr *attr = &info->mti_attr.ma_attr;
290 struct mdt_object *obj;
291 struct md_object *child;
292 __u64 valid = attr->la_valid;
293 const char *xattr_name;
297 posix_acl_xattr_header *new_xattr = NULL;
298 __u32 remote = exp_connect_rmtclient(info->mti_exp);
302 CDEBUG(D_INODE, "setxattr for "DFID"\n", PFID(rr->rr_fid1));
304 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SETXATTR))
305 RETURN(err_serious(-ENOMEM));
307 xattr_name = rr->rr_name;
309 CDEBUG(D_INODE, "%s xattr %s\n",
310 valid & OBD_MD_FLXATTR ? "set" : "remove", xattr_name);
312 rc = mdt_init_ucred_reint(info);
316 if (valid & OBD_MD_FLRMTRSETFACL) {
317 if (unlikely(!remote))
318 GOTO(out, rc = err_serious(-EINVAL));
320 perm = mdt_identity_get_perm(uc->mu_identity, remote,
322 if (!(perm & CFS_RMTACL_PERM))
323 GOTO(out, rc = err_serious(-EPERM));
326 /* various sanity check for xattr name */
327 xattr_name = req_capsule_client_get(pill, &RMF_NAME);
329 GOTO(out, rc = err_serious(-EFAULT));
331 if (strncmp(xattr_name, XATTR_USER_PREFIX,
332 sizeof(XATTR_USER_PREFIX) - 1) == 0) {
333 if (strcmp(xattr_name, XATTR_NAME_LOV) == 0)
334 GOTO(out, rc = -EACCES);
335 if (strcmp(xattr_name, XATTR_NAME_LMA) == 0)
337 if (strcmp(xattr_name, XATTR_NAME_LINK) == 0)
341 if (!(req->rq_export->exp_connect_flags & OBD_CONNECT_XATTR) &&
342 (strncmp(xattr_name, XATTR_USER_PREFIX,
343 sizeof(XATTR_USER_PREFIX) - 1) == 0)) {
344 GOTO(out, rc = -EOPNOTSUPP);
347 lockpart = MDS_INODELOCK_UPDATE;
348 if (!strcmp(xattr_name, XATTR_NAME_ACL_ACCESS))
349 lockpart |= MDS_INODELOCK_LOOKUP;
351 lh = &info->mti_lh[MDT_LH_PARENT];
352 mdt_lock_reg_init(lh, LCK_PW);
353 obj = mdt_object_find_lock(info, rr->rr_fid1, lh, lockpart);
355 GOTO(out, rc = PTR_ERR(obj));
357 if (unlikely(!(valid & OBD_MD_FLCTIME))) {
358 CWARN("client miss to set OBD_MD_FLCTIME when "
359 "setxattr: [object "DFID"] [valid %llu]\n",
360 PFID(rr->rr_fid1), valid);
361 attr->la_ctime = cfs_time_current_sec();
363 attr->la_valid = LA_CTIME;
364 child = mdt_object_child(obj);
365 if (valid & OBD_MD_FLXATTR) {
368 if (!req_capsule_field_present(pill, &RMF_EADATA, RCL_CLIENT)) {
369 CDEBUG(D_INFO, "no xattr data supplied\n");
370 GOTO(out_unlock, rc = -EFAULT);
373 xattr_len = req_capsule_get_size(pill, &RMF_EADATA, RCL_CLIENT);
377 xattr = req_capsule_client_get(pill, &RMF_EADATA);
379 if (valid & OBD_MD_FLRMTLSETFACL) {
380 if (unlikely(!remote))
381 GOTO(out_unlock, rc = -EINVAL);
383 xattr_len = mdt_rmtlsetfacl(info, child,
385 (ext_acl_xattr_header *)xattr,
388 GOTO(out_unlock, rc = xattr_len);
390 xattr = (char *)new_xattr;
393 if (attr->la_flags & XATTR_REPLACE)
394 flags |= LU_XATTR_REPLACE;
396 if (attr->la_flags & XATTR_CREATE)
397 flags |= LU_XATTR_CREATE;
399 mdt_fail_write(env, info->mti_mdt->mdt_bottom,
400 OBD_FAIL_MDS_SETXATTR_WRITE);
403 buf->lb_len = xattr_len;
404 rc = mo_xattr_set(env, child, buf, xattr_name, flags);
405 /* update ctime after xattr changed */
407 ma->ma_attr_flags |= MDS_PERM_BYPASS;
408 mo_attr_set(env, child, ma);
411 } else if (valid & OBD_MD_FLXATTRRM) {
412 rc = mo_xattr_del(env, child, xattr_name);
413 /* update ctime after xattr changed */
415 ma->ma_attr_flags |= MDS_PERM_BYPASS;
416 mo_attr_set(env, child, ma);
419 CDEBUG(D_INFO, "valid bits: "LPX64"\n", valid);
424 mdt_object_unlock_put(info, obj, lh, rc);
425 if (unlikely(new_xattr != NULL))
426 lustre_posix_acl_xattr_free(new_xattr, xattr_len);
428 mdt_exit_ucred(info);