1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * linux/mdt/mdt_xattr.c
5 * Lustre Metadata Target (mdt) extended attributes management.
7 * Copyright (C) 2002-2006 Cluster File Systems, Inc.
8 * Author: Peter Braam <braam@clusterfs.com>
9 * Author: Andreas Dilger <adilger@clusterfs.com>
10 * Author: Phil Schwan <phil@clusterfs.com>
11 * Author: Huang Hua <huanghua@clusterfs.com>
13 * This file is part of the Lustre file system, http://www.lustre.org
14 * Lustre is a trademark of Cluster File Systems, Inc.
16 * You may have signed or agreed to another license before downloading
17 * this software. If so, you are bound by the terms and conditions
18 * of that agreement, and the following does not apply to you. See the
19 * LICENSE file included with this distribution for more information.
21 * If you did not agree to a different license, then this copy of Lustre
22 * is open source software; you can redistribute it and/or modify it
23 * under the terms of version 2 of the GNU General Public License as
24 * published by the Free Software Foundation.
26 * In either case, Lustre is distributed in the hope that it will be
27 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
28 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * license text for more details.
33 # define EXPORT_SYMTAB
35 #define DEBUG_SUBSYSTEM S_MDS
37 /* prerequisite for linux/xattr.h */
38 #include <linux/types.h>
39 /* prerequisite for linux/xattr.h */
41 /* XATTR_{REPLACE,CREATE} */
42 #include <linux/xattr.h>
44 #include "mdt_internal.h"
47 /* return EADATA length to the caller. negative value means error */
48 static int mdt_getxattr_pack_reply(struct mdt_thread_info * info)
50 struct req_capsule *pill = &info->mti_pill ;
51 struct ptlrpc_request *req = mdt_info_req(info);
53 __u64 valid = info->mti_body->valid;
54 static const char user_string[] = "user.";
58 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETXATTR_PACK))
61 /* Determine how many bytes we need */
62 if ((valid & OBD_MD_FLXATTR) == OBD_MD_FLXATTR) {
63 xattr_name = req_capsule_client_get(pill, &RMF_NAME);
67 if (!(req->rq_export->exp_connect_flags & OBD_CONNECT_XATTR) &&
68 !strncmp(xattr_name, user_string, sizeof(user_string) - 1))
71 size = mo_xattr_get(info->mti_env,
72 mdt_object_child(info->mti_object),
73 &LU_BUF_NULL, xattr_name);
74 } else if ((valid & OBD_MD_FLXATTRLS) == OBD_MD_FLXATTRLS) {
75 size = mo_xattr_list(info->mti_env,
76 mdt_object_child(info->mti_object),
79 CERROR("Valid bits: "LPX64"\n", info->mti_body->valid);
86 else if (size != -EOPNOTSUPP) {
87 CERROR("Error geting EA size: %d\n", size);
92 if (info->mti_body->eadatasize != 0 &&
93 info->mti_body->eadatasize < size)
96 req_capsule_set_size(pill, &RMF_EADATA, RCL_SERVER,
97 min_t(int, size, info->mti_body->eadatasize));
99 rc = req_capsule_pack(pill);
108 int mdt_getxattr(struct mdt_thread_info *info)
110 struct ptlrpc_request *req = mdt_info_req(info);
111 struct mdt_export_data *med = mdt_req2med(req);
112 struct md_ucred *uc = mdt_ucred(info);
113 struct mdt_body *reqbody;
114 struct mdt_body *repbody = NULL;
115 struct md_object *next;
120 LASSERT(info->mti_object != NULL);
121 LASSERT(lu_object_assert_exists(&info->mti_object->mot_obj.mo_lu));
123 CDEBUG(D_INODE, "getxattr "DFID"\n", PFID(&info->mti_body->fid1));
125 reqbody = req_capsule_client_get(&info->mti_pill, &RMF_MDT_BODY);
127 RETURN(err_serious(-EFAULT));
129 rc = mdt_init_ucred(info, reqbody);
131 RETURN(err_serious(rc));
133 next = mdt_object_child(info->mti_object);
135 if (info->mti_body->valid & OBD_MD_FLRMTRGETFACL) {
136 __u32 perm = mdt_identity_get_perm(uc->mu_identity,
140 LASSERT(med->med_rmtclient);
141 if (!(perm & CFS_RMTACL_PERM))
142 GOTO(out, rc = err_serious(-EPERM));
144 rc = mo_permission(info->mti_env, NULL, next, NULL,
147 GOTO(out, rc = err_serious(rc));
150 easize = mdt_getxattr_pack_reply(info);
152 GOTO(out, rc = err_serious(easize));
154 repbody = req_capsule_server_get(&info->mti_pill, &RMF_MDT_BODY);
155 LASSERT(repbody != NULL);
157 /* No need further getxattr. */
158 if (easize == 0 || reqbody->eadatasize == 0)
159 GOTO(out, rc = easize);
161 buf = &info->mti_buf;
162 buf->lb_buf = req_capsule_server_get(&info->mti_pill, &RMF_EADATA);
163 buf->lb_len = easize;
165 if (info->mti_body->valid & OBD_MD_FLXATTR) {
166 int flags = CFS_IC_NOTHING;
167 char *xattr_name = req_capsule_client_get(&info->mti_pill,
169 CDEBUG(D_INODE, "getxattr %s\n", xattr_name);
171 rc = mo_xattr_get(info->mti_env, next, buf, xattr_name);
173 CERROR("getxattr failed: %d\n", rc);
177 if (info->mti_body->valid &
178 (OBD_MD_FLRMTLSETFACL | OBD_MD_FLRMTLGETFACL))
180 else if (info->mti_body->valid & OBD_MD_FLRMTRGETFACL)
181 flags = CFS_IC_MAPPED;
183 if (rc > 0 && flags != CFS_IC_NOTHING) {
186 LASSERT(med->med_rmtclient);
187 rc1 = lustre_posix_acl_xattr_id2client(uc,
189 (posix_acl_xattr_header *)(buf->lb_buf),
191 if (unlikely(rc1 < 0))
194 } else if (info->mti_body->valid & OBD_MD_FLXATTRLS) {
195 CDEBUG(D_INODE, "listxattr\n");
197 rc = mo_xattr_list(info->mti_env, next, buf);
199 CERROR("listxattr failed: %d\n", rc);
206 repbody->eadatasize = rc;
209 mdt_exit_ucred(info);
213 static int mdt_rmtlsetfacl(struct mdt_thread_info *info, char *xattr_name,
214 ext_acl_xattr_header *header,
215 posix_acl_xattr_header **out)
217 struct ptlrpc_request *req = mdt_info_req(info);
218 struct mdt_export_data *med = mdt_req2med(req);
219 struct md_ucred *uc = mdt_ucred(info);
220 struct md_object *next = mdt_object_child(info->mti_object);
221 struct lu_buf *buf = &info->mti_buf;
225 rc = lustre_ext_acl_xattr_id2server(uc, med->med_idmap, header);
229 rc = mo_xattr_get(info->mti_env, next, &LU_BUF_NULL, xattr_name);
236 if (buf->lb_len > 0) {
237 OBD_ALLOC(buf->lb_buf, buf->lb_len);
238 if (unlikely(buf->lb_buf == NULL))
241 rc = mo_xattr_get(info->mti_env, next, buf, xattr_name);
243 CERROR("getxattr failed: %d\n", rc);
249 rc = lustre_acl_xattr_merge2posix((posix_acl_xattr_header *)(buf->lb_buf),
250 buf->lb_len, header, out);
254 if (rc <= 0 && buf->lb_buf != NULL)
255 OBD_FREE(buf->lb_buf, buf->lb_len);
260 int mdt_setxattr(struct mdt_thread_info *info)
262 struct ptlrpc_request *req = mdt_info_req(info);
263 struct mdt_export_data *med = mdt_req2med(req);
264 struct md_ucred *uc = mdt_ucred(info);
265 struct mdt_body *reqbody;
266 const char user_string[] = "user.";
267 const char trust_string[] = "trusted.";
268 struct mdt_lock_handle *lh;
269 struct req_capsule *pill = &info->mti_pill;
270 struct mdt_object *obj = info->mti_object;
271 struct mdt_body *body = (struct mdt_body *)info->mti_body;
272 const struct lu_env *env = info->mti_env;
273 struct md_object *child = mdt_object_child(obj);
274 struct lu_buf *buf = &info->mti_buf;
275 __u64 valid = body->valid;
280 posix_acl_xattr_header *new_xattr = NULL;
283 CDEBUG(D_INODE, "setxattr "DFID"\n", PFID(&body->fid1));
285 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SETXATTR))
286 RETURN(err_serious(-ENOMEM));
288 reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
290 RETURN(err_serious(-EFAULT));
292 rc = mdt_init_ucred(info, reqbody);
294 RETURN(err_serious(rc));
296 if (valid & OBD_MD_FLRMTRSETFACL) {
297 __u32 perm = mdt_identity_get_perm(uc->mu_identity,
301 LASSERT(med->med_rmtclient);
302 if (!(perm & CFS_RMTACL_PERM))
303 GOTO(out, rc = err_serious(-EPERM));
306 rc = req_capsule_pack(pill);
308 GOTO(out, rc = err_serious(rc));
310 /* various sanity check for xattr name */
311 xattr_name = req_capsule_client_get(pill, &RMF_NAME);
313 GOTO(out, rc = err_serious(-EFAULT));
315 CDEBUG(D_INODE, "%s xattr %s\n",
316 body->valid & OBD_MD_FLXATTR ? "set" : "remove", xattr_name);
318 if (strncmp(xattr_name, trust_string, sizeof(trust_string) - 1) == 0) {
319 if (strcmp(xattr_name + 8, XATTR_NAME_LOV) == 0)
320 GOTO(out, rc = -EACCES);
323 if (!(req->rq_export->exp_connect_flags & OBD_CONNECT_XATTR) &&
324 (strncmp(xattr_name, user_string, sizeof(user_string) - 1) == 0)) {
325 GOTO(out, rc = -EOPNOTSUPP);
328 lockpart = MDS_INODELOCK_UPDATE;
329 if (!strcmp(xattr_name, XATTR_NAME_ACL_ACCESS))
330 lockpart |= MDS_INODELOCK_LOOKUP;
332 lh = &info->mti_lh[MDT_LH_PARENT];
333 mdt_lock_reg_init(lh, LCK_PW);
334 rc = mdt_object_lock(info, obj, lh, lockpart, MDT_LOCAL_LOCK);
338 if ((valid & OBD_MD_FLXATTR) == OBD_MD_FLXATTR) {
340 if (!req_capsule_field_present(pill, &RMF_EADATA, RCL_CLIENT)) {
341 CERROR("no xattr data supplied\n");
342 GOTO(out_unlock, rc = -EFAULT);
345 xattr_len = req_capsule_get_size(pill, &RMF_EADATA, RCL_CLIENT);
348 xattr = req_capsule_client_get(pill, &RMF_EADATA);
350 if (valid & OBD_MD_FLRMTLSETFACL) {
351 LASSERT(med->med_rmtclient);
352 xattr_len = mdt_rmtlsetfacl(info, xattr_name,
353 (ext_acl_xattr_header *)xattr,
356 GOTO(out_unlock, rc = xattr_len);
358 xattr = (char *)new_xattr;
361 if (body->flags & XATTR_REPLACE)
362 flags |= LU_XATTR_REPLACE;
364 if (body->flags & XATTR_CREATE)
365 flags |= LU_XATTR_CREATE;
367 mdt_fail_write(env, info->mti_mdt->mdt_bottom,
368 OBD_FAIL_MDS_SETXATTR_WRITE);
371 buf->lb_len = xattr_len;
372 rc = mo_xattr_set(env, child, buf, xattr_name, flags);
374 } else if ((valid & OBD_MD_FLXATTRRM) == OBD_MD_FLXATTRRM) {
375 rc = mo_xattr_del(env, child, xattr_name);
377 CERROR("valid bits: "LPX64"\n", body->valid);
382 if (unlikely(new_xattr != NULL))
383 lustre_posix_acl_xattr_free(new_xattr, xattr_len);
384 mdt_object_unlock(info, obj, lh, rc);
386 mdt_exit_ucred(info);