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 (MDT_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 if (!strcmp(xattr_name, XATTR_NAME_LUSTRE_ACL))
72 size = RMTACL_SIZE_MAX;
74 size = mo_xattr_get(info->mti_env,
75 mdt_object_child(info->mti_object),
76 &LU_BUF_NULL, xattr_name);
77 } else if ((valid & OBD_MD_FLXATTRLS) == OBD_MD_FLXATTRLS) {
78 size = mo_xattr_list(info->mti_env,
79 mdt_object_child(info->mti_object),
82 CERROR("Valid bits: "LPX64"\n", info->mti_body->valid);
89 else if (size != -EOPNOTSUPP) {
90 CERROR("Error geting EA size: %d\n", size);
95 if (info->mti_body->eadatasize != 0 &&
96 info->mti_body->eadatasize < size)
99 req_capsule_set_size(pill, &RMF_EADATA, RCL_SERVER,
100 min_t(int, size, info->mti_body->eadatasize));
102 rc = req_capsule_pack(pill);
111 static int do_remote_getfacl(struct mdt_thread_info *info, struct lu_buf *buf)
113 struct ptlrpc_request *req = mdt_info_req(info);
118 if (!buf->lb_buf || (buf->lb_len != RMTACL_SIZE_MAX))
121 cmd = req_capsule_client_get(&info->mti_pill, &RMF_EADATA);
123 CERROR("missing getfacl command!\n");
127 rc = mdt_rmtacl_upcall(info, cmd, buf);
129 CERROR("remote acl upcall failed: %d\n", rc);
131 lustre_shrink_reply(req, REPLY_REC_OFF + 1, strlen(buf->lb_buf) + 1, 0);
132 RETURN(rc ?: strlen(buf->lb_buf) + 1);
135 int mdt_getxattr(struct mdt_thread_info *info)
137 struct mdt_body *reqbody;
138 struct mdt_body *repbody = NULL;
139 struct md_object *next;
144 LASSERT(info->mti_object != NULL);
145 LASSERT(lu_object_assert_exists(&info->mti_object->mot_obj.mo_lu));
147 CDEBUG(D_INODE, "getxattr "DFID"\n", PFID(&info->mti_body->fid1));
149 reqbody = req_capsule_client_get(&info->mti_pill, &RMF_MDT_BODY);
151 RETURN(err_serious(-EFAULT));
153 rc = mdt_init_ucred(info, reqbody);
155 RETURN(err_serious(rc));
157 easize = mdt_getxattr_pack_reply(info);
159 GOTO(out, rc = err_serious(easize));
161 repbody = req_capsule_server_get(&info->mti_pill, &RMF_MDT_BODY);
162 LASSERT(repbody != NULL);
164 /* No need further getxattr. */
165 if (easize == 0 || reqbody->eadatasize == 0)
166 GOTO(out, rc = easize);
168 buf = &info->mti_buf;
169 buf->lb_buf = req_capsule_server_get(&info->mti_pill, &RMF_EADATA);
170 buf->lb_len = easize;
171 next = mdt_object_child(info->mti_object);
173 if (info->mti_body->valid & OBD_MD_FLXATTR) {
174 char *xattr_name = req_capsule_client_get(&info->mti_pill,
176 CDEBUG(D_INODE, "getxattr %s\n", xattr_name);
178 if (!strcmp(xattr_name, XATTR_NAME_LUSTRE_ACL))
179 rc = do_remote_getfacl(info, buf);
181 rc = mo_xattr_get(info->mti_env, next, buf, xattr_name);
184 CERROR("getxattr failed: %d\n", rc);
185 } else if (info->mti_body->valid & OBD_MD_FLXATTRLS) {
186 CDEBUG(D_INODE, "listxattr\n");
188 rc = mo_xattr_list(info->mti_env, next, buf);
190 CERROR("listxattr failed: %d\n", rc);
197 repbody->eadatasize = rc;
200 mdt_exit_ucred(info);
204 /* return EADATA length to the caller. negative value means error */
205 static int mdt_setxattr_pack_reply(struct mdt_thread_info * info)
207 struct req_capsule *pill = &info->mti_pill ;
208 __u64 valid = info->mti_body->valid;
211 if ((valid & OBD_MD_FLXATTR) == OBD_MD_FLXATTR) {
214 xattr_name = req_capsule_client_get(pill, &RMF_NAME);
218 if (!strcmp(xattr_name, XATTR_NAME_LUSTRE_ACL))
219 rc = RMTACL_SIZE_MAX;
222 req_capsule_set_size(pill, &RMF_EADATA, RCL_SERVER, rc);
224 rc1 = req_capsule_pack(pill);
226 return rc = rc1 ? rc1 : rc;
229 static int do_remote_setfacl(struct mdt_thread_info *info)
231 struct ptlrpc_request *req = mdt_info_req(info);
232 struct lu_buf *buf = &info->mti_buf;
237 cmd = req_capsule_client_get(&info->mti_pill, &RMF_EADATA);
239 CERROR("missing setfacl command!\n");
243 buf->lb_buf = req_capsule_server_get(&info->mti_pill, &RMF_EADATA);
244 LASSERT(buf->lb_buf);
245 buf->lb_len = RMTACL_SIZE_MAX;
247 rc = mdt_rmtacl_upcall(info, cmd, buf);
249 CERROR("remote acl upcall failed: %d\n", rc);
251 lustre_shrink_reply(req, REPLY_REC_OFF, strlen(buf->lb_buf) + 1, 0);
255 int mdt_setxattr(struct mdt_thread_info *info)
257 struct ptlrpc_request *req = mdt_info_req(info);
258 struct mdt_body *reqbody;
259 const char user_string[] = "user.";
260 const char trust_string[] = "trusted.";
261 struct mdt_lock_handle *lh;
262 struct req_capsule *pill = &info->mti_pill;
263 struct mdt_object *obj = info->mti_object;
264 struct mdt_body *body = (struct mdt_body *)info->mti_body;
265 const struct lu_env *env = info->mti_env;
266 struct md_object *child = mdt_object_child(obj);
267 struct lu_buf *buf = &info->mti_buf;
268 __u64 valid = body->valid;
275 CDEBUG(D_INODE, "setxattr "DFID"\n", PFID(&body->fid1));
277 if (MDT_FAIL_CHECK(OBD_FAIL_MDS_SETXATTR))
278 RETURN(err_serious(-ENOMEM));
280 reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
282 RETURN(err_serious(-EFAULT));
284 rc = mdt_init_ucred(info, reqbody);
286 RETURN(err_serious(rc));
288 rc = mdt_setxattr_pack_reply(info);
290 GOTO(out, rc = err_serious(rc));
292 /* various sanity check for xattr name */
293 xattr_name = req_capsule_client_get(pill, &RMF_NAME);
295 GOTO(out, rc = err_serious(-EFAULT));
297 CDEBUG(D_INODE, "%s xattr %s\n",
298 body->valid & OBD_MD_FLXATTR ? "set" : "remove", xattr_name);
300 if (((valid & OBD_MD_FLXATTR) == OBD_MD_FLXATTR) &&
301 (!strcmp(xattr_name, XATTR_NAME_LUSTRE_ACL))) {
302 rc = do_remote_setfacl(info);
306 if (strncmp(xattr_name, trust_string, sizeof(trust_string) - 1) == 0) {
307 if (strcmp(xattr_name + 8, XATTR_NAME_LOV) == 0)
308 GOTO(out, rc = -EACCES);
311 if (!(req->rq_export->exp_connect_flags & OBD_CONNECT_XATTR) &&
312 (strncmp(xattr_name, user_string, sizeof(user_string) - 1) == 0)) {
313 GOTO(out, rc = -EOPNOTSUPP);
316 lockpart = MDS_INODELOCK_UPDATE;
317 if (!strcmp(xattr_name, XATTR_NAME_ACL_ACCESS))
318 lockpart |= MDS_INODELOCK_LOOKUP;
320 lh = &info->mti_lh[MDT_LH_PARENT];
321 mdt_lock_reg_init(lh, LCK_PW);
322 rc = mdt_object_lock(info, obj, lh, lockpart, MDT_LOCAL_LOCK);
326 if ((valid & OBD_MD_FLXATTR) == OBD_MD_FLXATTR) {
328 if (!req_capsule_field_present(pill, &RMF_EADATA, RCL_CLIENT)) {
329 CERROR("no xattr data supplied\n");
330 GOTO(out_unlock, rc = -EFAULT);
333 xattr_len = req_capsule_get_size(pill, &RMF_EADATA, RCL_CLIENT);
336 xattr = req_capsule_client_get(pill, &RMF_EADATA);
338 if (body->flags & XATTR_REPLACE)
339 flags |= LU_XATTR_REPLACE;
341 if (body->flags & XATTR_CREATE)
342 flags |= LU_XATTR_CREATE;
344 mdt_fail_write(env, info->mti_mdt->mdt_bottom,
345 OBD_FAIL_MDS_SETXATTR_WRITE);
348 buf->lb_len = xattr_len;
349 rc = mo_xattr_set(env, child, buf, xattr_name, flags);
351 } else if ((valid & OBD_MD_FLXATTRRM) == OBD_MD_FLXATTRRM) {
352 rc = mo_xattr_del(env, child, xattr_name);
354 CERROR("valid bits: "LPX64"\n", body->valid);
359 mdt_object_unlock(info, obj, lh, rc);
361 mdt_exit_ucred(info);