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 rc = req_capsule_pack(pill);
99 int mdt_getxattr(struct mdt_thread_info *info)
102 struct md_object *next;
105 struct mdt_body *rep_body;
109 LASSERT(info->mti_object != NULL);
110 LASSERT(lu_object_assert_exists(info->mti_ctxt,
111 &info->mti_object->mot_obj.mo_lu));
113 CDEBUG(D_INODE, "getxattr "DFID3"\n",
114 PFID3(&info->mti_body->fid1));
116 if (MDT_FAIL_CHECK(OBD_FAIL_MDS_GETXATTR_PACK))
119 next = mdt_object_child(info->mti_object);
121 rc = mdt_getxattr_pack_reply(info);
125 buf = req_capsule_server_get(&info->mti_pill, &RMF_EADATA);
126 rep_body = req_capsule_server_get(&info->mti_pill, &RMF_MDT_BODY);
127 buflen = req_capsule_get_size(&info->mti_pill, &RMF_EADATA, RCL_SERVER);
129 GOTO(no_xattr, rc = 0);
131 if (info->mti_body->valid & OBD_MD_FLXATTR) {
132 char *xattr_name = req_capsule_client_get(&info->mti_pill,
134 CDEBUG(D_INODE, "getxattr %s\n", xattr_name);
136 rc = mo_xattr_get(info->mti_ctxt, next,
137 buf, buflen, xattr_name);
139 if (rc < 0 && rc != -ENODATA && rc != -EOPNOTSUPP &&
141 CDEBUG(D_INODE, "getxattr failed: %d\n", rc);
142 } else if (info->mti_body->valid & OBD_MD_FLXATTRLS) {
143 CDEBUG(D_INODE, "listxattr\n");
145 rc = mo_xattr_list(info->mti_ctxt, next, buf, buflen);
147 CDEBUG(D_OTHER, "listxattr failed: %d\n", rc);
153 rep_body->eadatasize = rc;
161 int mdt_setxattr(struct mdt_thread_info *info)
163 struct ptlrpc_request *req = mdt_info_req(info);
164 const char user_string[] = "user.";
165 const char trust_string[] = "trusted.";
166 struct mdt_lock_handle *lh;
167 struct req_capsule *pill = &info->mti_pill;
168 struct mdt_object *obj = info->mti_object;
169 const struct mdt_body *body = info->mti_body;
170 const struct lu_context *ctx = info->mti_ctxt;
171 struct md_object *child = mdt_object_child(obj);
172 __u64 valid = body->valid;
179 CDEBUG(D_INODE, "setxattr "DFID3"\n", PFID3(&body->fid1));
181 if (MDT_FAIL_CHECK(OBD_FAIL_MDS_SETXATTR))
184 /* various sanity check for xattr name */
185 xattr_name = req_capsule_client_get(pill, &RMF_NAME);
187 GOTO(out, rc = -EFAULT);
189 CDEBUG(D_INODE, "%s xattr %s\n",
190 body->valid & OBD_MD_FLXATTR ? "set" : "remove", xattr_name);
192 if (strncmp(xattr_name, trust_string, sizeof(trust_string) - 1) == 0) {
193 if (strcmp(xattr_name + 8, XATTR_NAME_LOV) == 0)
194 GOTO(out, rc = -EACCES);
197 if (!(req->rq_export->exp_connect_flags & OBD_CONNECT_XATTR) &&
198 (strncmp(xattr_name, user_string, sizeof(user_string) - 1) == 0)) {
199 GOTO(out, rc = -EOPNOTSUPP);
202 lockpart = MDS_INODELOCK_UPDATE;
203 if (!strcmp(xattr_name, XATTR_NAME_ACL_ACCESS))
204 lockpart |= MDS_INODELOCK_LOOKUP;
206 lh = &info->mti_lh[MDT_LH_PARENT];
207 lh->mlh_mode = LCK_EX;
208 rc = mdt_object_lock(info, obj, lh, lockpart);
212 if ((valid & OBD_MD_FLXATTR) == OBD_MD_FLXATTR) {
214 if (!req_capsule_field_present(pill, &RMF_EADATA)) {
215 CERROR("no xattr data supplied\n");
216 GOTO(out_unlock, rc = -EFAULT);
219 xattr_len = req_capsule_get_size(pill, &RMF_EADATA, RCL_CLIENT);
222 xattr = req_capsule_client_get(pill, &RMF_EADATA);
224 if (body->flags & XATTR_REPLACE)
225 flags |= LU_XATTR_REPLACE;
227 if (body->flags & XATTR_CREATE)
228 flags |= LU_XATTR_CREATE;
230 rc = mo_xattr_set(ctx, child, xattr,
231 xattr_len, xattr_name, flags);
233 } else if ((valid & OBD_MD_FLXATTRRM) == OBD_MD_FLXATTRRM) {
234 rc = mo_xattr_del(ctx, child, xattr_name);
236 CERROR("valid bits: "LPX64"\n", body->valid);
241 mdt_object_unlock(info, obj, lh, rc);