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.";
57 if (MDT_FAIL_CHECK(OBD_FAIL_MDS_GETXATTR_PACK))
60 /* Determine how many bytes we need */
61 if ((valid & OBD_MD_FLXATTR) == OBD_MD_FLXATTR) {
62 xattr_name = req_capsule_client_get(pill, &RMF_NAME);
66 if (!(req->rq_export->exp_connect_flags & OBD_CONNECT_XATTR) &&
67 !strncmp(xattr_name, user_string, sizeof(user_string) - 1))
70 if (!strcmp(xattr_name, XATTR_NAME_LUSTRE_ACL))
71 size = RMTACL_SIZE_MAX;
73 size = mo_xattr_get(info->mti_env,
74 mdt_object_child(info->mti_object),
75 &LU_BUF_NULL, xattr_name);
76 } else if ((valid & OBD_MD_FLXATTRLS) == OBD_MD_FLXATTRLS) {
77 size = mo_xattr_list(info->mti_env,
78 mdt_object_child(info->mti_object),
81 CERROR("valid bits: "LPX64"\n", info->mti_body->valid);
86 if (size != -ENODATA && size != -EOPNOTSUPP)
87 CERROR("get EA size error: %d\n", size);
91 if (info->mti_body->eadatasize != 0 &&
92 info->mti_body->eadatasize < size)
95 req_capsule_set_size(pill, &RMF_EADATA, RCL_SERVER,
96 min_t(int, size, info->mti_body->eadatasize));
98 rc = req_capsule_pack(pill);
107 static int do_remote_getfacl(struct mdt_thread_info *info,
108 struct lu_fid *fid, struct lu_buf *buf)
110 struct ptlrpc_request *req = mdt_info_req(info);
115 if (!buf->lb_buf || (buf->lb_len != RMTACL_SIZE_MAX))
118 cmd = req_capsule_client_get(&info->mti_pill, &RMF_EADATA);
120 CERROR("missing getfacl command!\n");
124 rc = mdt_rmtacl_upcall(info, fid_oid(fid), cmd, buf);
126 CERROR("remote acl upcall failed: %d\n", rc);
128 lustre_shrink_reply(req, REPLY_REC_OFF + 1, strlen(buf->lb_buf) + 1, 0);
129 RETURN(rc ?: strlen(buf->lb_buf) + 1);
132 int mdt_getxattr(struct mdt_thread_info *info)
134 struct mdt_body *reqbody;
135 struct mdt_body *repbody;
136 struct md_object *next;
141 LASSERT(info->mti_object != NULL);
142 LASSERT(lu_object_assert_exists(&info->mti_object->mot_obj.mo_lu));
144 CDEBUG(D_INODE, "getxattr "DFID"\n", PFID(&info->mti_body->fid1));
146 reqbody = req_capsule_client_get(&info->mti_pill, &RMF_MDT_BODY);
148 RETURN(err_serious(-EFAULT));
150 easize = mdt_getxattr_pack_reply(info);
152 RETURN(err_serious(easize));
154 repbody = req_capsule_server_get(&info->mti_pill, &RMF_MDT_BODY);
157 rc = mdt_init_ucred(info, reqbody);
161 /* no need further getxattr */
162 if (easize == 0 || reqbody->eadatasize == 0)
163 GOTO(out, rc = easize);
165 buf = &info->mti_buf;
166 buf->lb_buf = req_capsule_server_get(&info->mti_pill, &RMF_EADATA);
167 buf->lb_len = easize;
168 next = mdt_object_child(info->mti_object);
170 if (info->mti_body->valid & OBD_MD_FLXATTR) {
171 char *xattr_name = req_capsule_client_get(&info->mti_pill,
173 CDEBUG(D_INODE, "getxattr %s\n", xattr_name);
175 if (!strcmp(xattr_name, XATTR_NAME_LUSTRE_ACL)) {
176 struct mdt_body *body =
177 (struct mdt_body *)info->mti_body;
179 rc = do_remote_getfacl(info, &body->fid1, buf);
181 rc = mo_xattr_get(info->mti_env, next, buf, xattr_name);
185 CERROR("getxattr failed: %d\n", rc);
186 } else if (info->mti_body->valid & OBD_MD_FLXATTRLS) {
187 CDEBUG(D_INODE, "listxattr\n");
189 rc = mo_xattr_list(info->mti_env, next, buf);
191 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, struct lu_fid *fid)
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, fid_oid(fid), 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 rc = mdt_setxattr_pack_reply(info);
282 RETURN(err_serious(rc));
284 reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
286 RETURN(err_serious(-EFAULT));
288 rc = mdt_init_ucred(info, reqbody);
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, &body->fid1);
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_pdo_init(lh, LCK_PW, NULL, 0);
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);