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,
68 sizeof(user_string) - 1) == 0)
71 rc = mo_xattr_get(info->mti_ctxt,
72 mdt_object_child(info->mti_object),
74 } else if ((valid & OBD_MD_FLXATTRLS) == OBD_MD_FLXATTRLS) {
75 rc = mo_xattr_list(info->mti_ctxt,
76 mdt_object_child(info->mti_object),
79 CERROR("valid bits: "LPX64"\n", info->mti_body->valid);
84 if (rc != -ENODATA && rc != -EOPNOTSUPP) {
85 CWARN("get EA size error: %d\n", rc);
90 rc = min_t(int, info->mti_body->eadatasize, rc);
92 req_capsule_set_size(pill, &RMF_EADATA, RCL_SERVER, rc);
94 rc1 = req_capsule_pack(pill);
96 return rc = !rc1? rc1 : rc;
100 int mdt_getxattr(struct mdt_thread_info *info)
103 struct md_object *next;
106 struct mdt_body *rep_body;
110 LASSERT(info->mti_object != NULL);
111 LASSERT(lu_object_assert_exists(&info->mti_object->mot_obj.mo_lu));
113 CDEBUG(D_INODE, "getxattr "DFID"\n",
114 PFID(&info->mti_body->fid1));
116 next = mdt_object_child(info->mti_object);
118 rc = mdt_getxattr_pack_reply(info);
122 rep_body = req_capsule_server_get(&info->mti_pill, &RMF_MDT_BODY);
123 /*No EA, just go back*/
127 buf = req_capsule_server_get(&info->mti_pill, &RMF_EADATA);
130 if (info->mti_body->valid & OBD_MD_FLXATTR) {
131 char *xattr_name = req_capsule_client_get(&info->mti_pill,
133 CDEBUG(D_INODE, "getxattr %s\n", xattr_name);
135 rc = mo_xattr_get(info->mti_ctxt, next,
136 buf, buflen, xattr_name);
138 if (rc < 0 && rc != -ENODATA && rc != -EOPNOTSUPP &&
140 CDEBUG(D_INODE, "getxattr failed: %d\n", rc);
141 } else if (info->mti_body->valid & OBD_MD_FLXATTRLS) {
142 CDEBUG(D_INODE, "listxattr\n");
144 rc = mo_xattr_list(info->mti_ctxt, next, buf, buflen);
146 CDEBUG(D_OTHER, "listxattr failed: %d\n", rc);
152 rep_body->eadatasize = rc;
160 int mdt_setxattr(struct mdt_thread_info *info)
162 struct ptlrpc_request *req = mdt_info_req(info);
163 const char user_string[] = "user.";
164 const char trust_string[] = "trusted.";
165 struct mdt_lock_handle *lh;
166 struct req_capsule *pill = &info->mti_pill;
167 struct mdt_object *obj = info->mti_object;
168 const struct mdt_body *body = info->mti_body;
169 const struct lu_context *ctx = info->mti_ctxt;
170 struct md_object *child = mdt_object_child(obj);
171 __u64 valid = body->valid;
178 CDEBUG(D_INODE, "setxattr "DFID"\n", PFID(&body->fid1));
180 if (MDT_FAIL_CHECK(OBD_FAIL_MDS_SETXATTR))
183 /* various sanity check for xattr name */
184 xattr_name = req_capsule_client_get(pill, &RMF_NAME);
186 GOTO(out, rc = -EFAULT);
188 CDEBUG(D_INODE, "%s xattr %s\n",
189 body->valid & OBD_MD_FLXATTR ? "set" : "remove", xattr_name);
191 if (strncmp(xattr_name, trust_string, sizeof(trust_string) - 1) == 0) {
192 if (strcmp(xattr_name + 8, XATTR_NAME_LOV) == 0)
193 GOTO(out, rc = -EACCES);
196 if (!(req->rq_export->exp_connect_flags & OBD_CONNECT_XATTR) &&
197 (strncmp(xattr_name, user_string, sizeof(user_string) - 1) == 0)) {
198 GOTO(out, rc = -EOPNOTSUPP);
201 lockpart = MDS_INODELOCK_UPDATE;
202 if (!strcmp(xattr_name, XATTR_NAME_ACL_ACCESS))
203 lockpart |= MDS_INODELOCK_LOOKUP;
205 lh = &info->mti_lh[MDT_LH_PARENT];
206 lh->mlh_mode = LCK_EX;
207 rc = mdt_object_lock(info, obj, lh, lockpart);
211 if ((valid & OBD_MD_FLXATTR) == OBD_MD_FLXATTR) {
213 if (!req_capsule_field_present(pill, &RMF_EADATA, RCL_CLIENT)) {
214 CERROR("no xattr data supplied\n");
215 GOTO(out_unlock, rc = -EFAULT);
218 xattr_len = req_capsule_get_size(pill, &RMF_EADATA, RCL_CLIENT);
221 xattr = req_capsule_client_get(pill, &RMF_EADATA);
223 if (body->flags & XATTR_REPLACE)
224 flags |= LU_XATTR_REPLACE;
226 if (body->flags & XATTR_CREATE)
227 flags |= LU_XATTR_CREATE;
228 mdt_fail_write(ctx, info->mti_mdt->mdt_bottom,
229 OBD_FAIL_MDS_SETXATTR_WRITE);
231 rc = mo_xattr_set(ctx, child, xattr,
232 xattr_len, xattr_name, flags);
234 } else if ((valid & OBD_MD_FLXATTRRM) == OBD_MD_FLXATTRRM) {
235 rc = mo_xattr_del(ctx, child, xattr_name);
237 CERROR("valid bits: "LPX64"\n", body->valid);
242 mdt_object_unlock(info, obj, lh, rc);