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) {
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) {
75 size = mo_xattr_list(info->mti_env,
76 mdt_object_child(info->mti_object),
79 CDEBUG(D_INFO, "Valid bits: "LPX64"\n", info->mti_body->valid);
86 else if (size != -EOPNOTSUPP) {
87 CDEBUG(D_INFO, "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_server_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 CDEBUG(D_INFO, "listxattr failed: %d\n", rc);
206 repbody->eadatasize = rc;
209 mdt_exit_ucred(info);
213 static int mdt_rmtlsetfacl(struct mdt_thread_info *info,
214 struct md_object *next,
215 const char *xattr_name,
216 ext_acl_xattr_header *header,
217 posix_acl_xattr_header **out)
219 struct ptlrpc_request *req = mdt_info_req(info);
220 struct mdt_export_data *med = mdt_req2med(req);
221 struct md_ucred *uc = mdt_ucred(info);
222 struct lu_buf *buf = &info->mti_buf;
226 rc = lustre_ext_acl_xattr_id2server(uc, med->med_idmap, header);
230 rc = mo_xattr_get(info->mti_env, next, &LU_BUF_NULL, xattr_name);
237 if (buf->lb_len > 0) {
238 OBD_ALLOC(buf->lb_buf, buf->lb_len);
239 if (unlikely(buf->lb_buf == NULL))
242 rc = mo_xattr_get(info->mti_env, next, buf, xattr_name);
244 CERROR("getxattr failed: %d\n", rc);
250 rc = lustre_acl_xattr_merge2posix((posix_acl_xattr_header *)(buf->lb_buf),
251 buf->lb_len, header, out);
255 if (rc <= 0 && buf->lb_buf != NULL)
256 OBD_FREE(buf->lb_buf, buf->lb_len);
260 int mdt_reint_setxattr(struct mdt_thread_info *info,
261 struct mdt_lock_handle *unused)
263 struct ptlrpc_request *req = mdt_info_req(info);
264 struct mdt_export_data *med = mdt_req2med(req);
265 struct md_ucred *uc = mdt_ucred(info);
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 const struct lu_env *env = info->mti_env;
271 struct lu_buf *buf = &info->mti_buf;
272 struct mdt_reint_record *rr = &info->mti_rr;
273 struct md_attr *ma = &info->mti_attr;
274 struct lu_attr *attr = &info->mti_attr.ma_attr;
275 struct mdt_object *obj;
276 struct md_object *child;
277 __u64 valid = attr->la_valid;
278 const char *xattr_name;
282 posix_acl_xattr_header *new_xattr = NULL;
285 CDEBUG(D_INODE, "setxattr for "DFID"\n", PFID(rr->rr_fid1));
287 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SETXATTR))
288 RETURN(err_serious(-ENOMEM));
290 xattr_name = rr->rr_name;
292 CDEBUG(D_INODE, "%s xattr %s\n",
293 valid & OBD_MD_FLXATTR ? "set" : "remove", xattr_name);
295 rc = mdt_init_ucred_reint(info);
299 if (valid & OBD_MD_FLRMTRSETFACL) {
300 __u32 perm = mdt_identity_get_perm(uc->mu_identity,
304 LASSERT(med->med_rmtclient);
305 if (!(perm & CFS_RMTACL_PERM))
306 GOTO(out, rc = err_serious(-EPERM));
309 /* various sanity check for xattr name */
310 xattr_name = req_capsule_client_get(pill, &RMF_NAME);
312 GOTO(out, rc = err_serious(-EFAULT));
314 if (strncmp(xattr_name, trust_string, sizeof(trust_string) - 1) == 0) {
315 if (strcmp(xattr_name + 8, XATTR_NAME_LOV) == 0)
316 GOTO(out, rc = -EACCES);
319 if (!(req->rq_export->exp_connect_flags & OBD_CONNECT_XATTR) &&
320 (strncmp(xattr_name, user_string, sizeof(user_string) - 1) == 0)) {
321 GOTO(out, rc = -EOPNOTSUPP);
324 lockpart = MDS_INODELOCK_UPDATE;
325 if (!strcmp(xattr_name, XATTR_NAME_ACL_ACCESS))
326 lockpart |= MDS_INODELOCK_LOOKUP;
328 lh = &info->mti_lh[MDT_LH_PARENT];
329 mdt_lock_reg_init(lh, LCK_PW);
330 obj = mdt_object_find_lock(info, rr->rr_fid1, lh, lockpart);
332 GOTO(out, rc = PTR_ERR(obj));
334 if (unlikely(!(valid & OBD_MD_FLCTIME))) {
335 CWARN("client miss to set OBD_MD_FLCTIME when "
336 "setxattr: [object "DFID"] [valid %llu]\n",
337 PFID(rr->rr_fid1), valid);
338 attr->la_ctime = cfs_time_current_sec();
340 attr->la_valid = LA_CTIME;
341 child = mdt_object_child(obj);
342 if (valid & OBD_MD_FLXATTR) {
345 if (!req_capsule_field_present(pill, &RMF_EADATA, RCL_CLIENT)) {
346 CDEBUG(D_INFO, "no xattr data supplied\n");
347 GOTO(out_unlock, rc = -EFAULT);
350 xattr_len = req_capsule_get_size(pill, &RMF_EADATA, RCL_CLIENT);
354 xattr = req_capsule_client_get(pill, &RMF_EADATA);
356 if (valid & OBD_MD_FLRMTLSETFACL) {
357 LASSERT(med->med_rmtclient);
358 xattr_len = mdt_rmtlsetfacl(info, child,
360 (ext_acl_xattr_header *)xattr,
363 GOTO(out_unlock, rc = xattr_len);
365 xattr = (char *)new_xattr;
368 if (attr->la_flags & XATTR_REPLACE)
369 flags |= LU_XATTR_REPLACE;
371 if (attr->la_flags & XATTR_CREATE)
372 flags |= LU_XATTR_CREATE;
374 mdt_fail_write(env, info->mti_mdt->mdt_bottom,
375 OBD_FAIL_MDS_SETXATTR_WRITE);
378 buf->lb_len = xattr_len;
379 rc = mo_xattr_set(env, child, buf, xattr_name, flags);
380 /* update ctime after xattr changed */
382 ma->ma_attr_flags |= MDS_PERM_BYPASS;
383 mo_attr_set(env, child, ma);
386 } else if (valid & OBD_MD_FLXATTRRM) {
387 rc = mo_xattr_del(env, child, xattr_name);
388 /* update ctime after xattr changed */
390 ma->ma_attr_flags |= MDS_PERM_BYPASS;
391 mo_attr_set(env, child, ma);
394 CDEBUG(D_INFO, "valid bits: "LPX64"\n", valid);
399 mdt_object_unlock_put(info, obj, lh, rc);
400 if (unlikely(new_xattr != NULL))
401 lustre_posix_acl_xattr_free(new_xattr, xattr_len);
403 mdt_exit_ucred(info);