4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2013, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/mdt/mdt_xattr.c
38 * Lustre Metadata Target (mdt) extended attributes management.
40 * Author: Peter Braam <braam@clusterfs.com>
41 * Author: Andreas Dilger <adilger@clusterfs.com>
42 * Author: Phil Schwan <phil@clusterfs.com>
43 * Author: Huang Hua <huanghua@clusterfs.com>
46 #define DEBUG_SUBSYSTEM S_MDS
48 #include <lustre_acl.h>
49 #include "mdt_internal.h"
52 /* return EADATA length to the caller. negative value means error */
53 static int mdt_getxattr_pack_reply(struct mdt_thread_info * info)
55 struct req_capsule *pill = info->mti_pill ;
56 struct ptlrpc_request *req = mdt_info_req(info);
58 __u64 valid = info->mti_body->valid;
59 static const char user_string[] = "user.";
63 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETXATTR_PACK))
66 /* Determine how many bytes we need */
67 if (valid & OBD_MD_FLXATTR) {
68 xattr_name = req_capsule_client_get(pill, &RMF_NAME);
72 if (!(exp_connect_flags(req->rq_export) & OBD_CONNECT_XATTR) &&
73 !strncmp(xattr_name, user_string, sizeof(user_string) - 1))
76 size = mo_xattr_get(info->mti_env,
77 mdt_object_child(info->mti_object),
78 &LU_BUF_NULL, xattr_name);
79 } else if (valid & OBD_MD_FLXATTRLS) {
80 size = mo_xattr_list(info->mti_env,
81 mdt_object_child(info->mti_object),
84 CDEBUG(D_INFO, "Valid bits: "LPX64"\n", info->mti_body->valid);
88 if (size == -ENODATA) {
90 } else if (size < 0) {
91 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 info->mti_body->eadatasize == 0 ? 0 : size);
101 rc = req_capsule_server_pack(pill);
110 int mdt_getxattr(struct mdt_thread_info *info)
112 struct ptlrpc_request *req = mdt_info_req(info);
113 struct mdt_export_data *med = mdt_req2med(req);
114 struct lu_ucred *uc = mdt_ucred(info);
115 struct mdt_body *reqbody;
116 struct mdt_body *repbody = NULL;
117 struct md_object *next;
119 __u32 remote = exp_connect_rmtclient(info->mti_exp);
124 LASSERT(info->mti_object != NULL);
125 LASSERT(lu_object_assert_exists(&info->mti_object->mot_obj.mo_lu));
127 CDEBUG(D_INODE, "getxattr "DFID"\n", PFID(&info->mti_body->fid1));
129 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
131 RETURN(err_serious(-EFAULT));
133 rc = mdt_init_ucred(info, reqbody);
135 RETURN(err_serious(rc));
137 next = mdt_object_child(info->mti_object);
139 if (info->mti_body->valid & OBD_MD_FLRMTRGETFACL) {
140 if (unlikely(!remote))
141 GOTO(out, rc = err_serious(-EINVAL));
143 perm = mdt_identity_get_perm(uc->uc_identity, remote,
145 if (!(perm & CFS_RMTACL_PERM))
146 GOTO(out, rc = err_serious(-EPERM));
148 rc = mo_permission(info->mti_env, NULL, next, NULL,
151 GOTO(out, rc = err_serious(rc));
154 easize = mdt_getxattr_pack_reply(info);
156 GOTO(out, rc = err_serious(easize));
158 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
159 LASSERT(repbody != NULL);
161 /* No need further getxattr. */
162 if (easize == 0 || reqbody->eadatasize == 0)
163 GOTO(out, rc = easize);
166 buf = &info->mti_buf;
167 buf->lb_buf = req_capsule_server_get(info->mti_pill, &RMF_EADATA);
168 buf->lb_len = easize;
170 if (info->mti_body->valid & OBD_MD_FLXATTR) {
171 int flags = CFS_IC_NOTHING;
172 char *xattr_name = req_capsule_client_get(info->mti_pill,
174 CDEBUG(D_INODE, "getxattr %s\n", xattr_name);
176 rc = mo_xattr_get(info->mti_env, next, buf, xattr_name);
178 CERROR("getxattr failed: %d\n", rc);
182 if (info->mti_body->valid &
183 (OBD_MD_FLRMTLSETFACL | OBD_MD_FLRMTLGETFACL))
185 else if (info->mti_body->valid & OBD_MD_FLRMTRGETFACL)
186 flags = CFS_IC_MAPPED;
188 if (rc > 0 && flags != CFS_IC_NOTHING) {
191 if (unlikely(!remote))
192 GOTO(out, rc = -EINVAL);
194 rc1 = lustre_posix_acl_xattr_id2client(uc,
196 (posix_acl_xattr_header *)(buf->lb_buf),
198 if (unlikely(rc1 < 0))
201 } else if (info->mti_body->valid & OBD_MD_FLXATTRLS) {
202 CDEBUG(D_INODE, "listxattr\n");
204 rc = mo_xattr_list(info->mti_env, next, buf);
206 CDEBUG(D_INFO, "listxattr failed: %d\n", rc);
213 mdt_counter_incr(req, LPROC_MDT_GETXATTR);
214 repbody->eadatasize = rc;
217 mdt_exit_ucred(info);
221 static int mdt_rmtlsetfacl(struct mdt_thread_info *info,
222 struct md_object *next,
223 const char *xattr_name,
224 ext_acl_xattr_header *header,
225 posix_acl_xattr_header **out)
227 struct ptlrpc_request *req = mdt_info_req(info);
228 struct mdt_export_data *med = mdt_req2med(req);
229 struct lu_ucred *uc = mdt_ucred(info);
230 struct lu_buf *buf = &info->mti_buf;
234 rc = lustre_ext_acl_xattr_id2server(uc, med->med_idmap, header);
238 rc = mo_xattr_get(info->mti_env, next, &LU_BUF_NULL, xattr_name);
245 if (buf->lb_len > 0) {
246 OBD_ALLOC_LARGE(buf->lb_buf, buf->lb_len);
247 if (unlikely(buf->lb_buf == NULL))
250 rc = mo_xattr_get(info->mti_env, next, buf, xattr_name);
252 CERROR("getxattr failed: %d\n", rc);
258 rc = lustre_acl_xattr_merge2posix((posix_acl_xattr_header *)(buf->lb_buf),
259 buf->lb_len, header, out);
263 if (rc <= 0 && buf->lb_buf != NULL)
264 OBD_FREE_LARGE(buf->lb_buf, buf->lb_len);
268 int mdt_reint_setxattr(struct mdt_thread_info *info,
269 struct mdt_lock_handle *unused)
271 struct ptlrpc_request *req = mdt_info_req(info);
272 struct lu_ucred *uc = mdt_ucred(info);
273 struct mdt_lock_handle *lh;
274 const struct lu_env *env = info->mti_env;
275 struct lu_buf *buf = &info->mti_buf;
276 struct mdt_reint_record *rr = &info->mti_rr;
277 struct md_attr *ma = &info->mti_attr;
278 struct lu_attr *attr = &info->mti_attr.ma_attr;
279 struct mdt_object *obj;
280 struct md_object *child;
281 __u64 valid = attr->la_valid;
282 const char *xattr_name = rr->rr_name;
283 int xattr_len = rr->rr_eadatalen;
286 posix_acl_xattr_header *new_xattr = NULL;
287 __u32 remote = exp_connect_rmtclient(info->mti_exp);
291 CDEBUG(D_INODE, "setxattr for "DFID"\n", PFID(rr->rr_fid1));
293 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SETXATTR))
294 RETURN(err_serious(-ENOMEM));
296 CDEBUG(D_INODE, "%s xattr %s\n",
297 valid & OBD_MD_FLXATTR ? "set" : "remove", xattr_name);
299 rc = mdt_init_ucred_reint(info);
303 if (valid & OBD_MD_FLRMTRSETFACL) {
304 if (unlikely(!remote))
305 GOTO(out, rc = err_serious(-EINVAL));
307 perm = mdt_identity_get_perm(uc->uc_identity, remote,
309 if (!(perm & CFS_RMTACL_PERM))
310 GOTO(out, rc = err_serious(-EPERM));
313 if (strncmp(xattr_name, XATTR_USER_PREFIX,
314 sizeof(XATTR_USER_PREFIX) - 1) == 0) {
315 if (!(exp_connect_flags(req->rq_export) & OBD_CONNECT_XATTR))
316 GOTO(out, rc = -EOPNOTSUPP);
317 if (strcmp(xattr_name, XATTR_NAME_LOV) == 0)
318 GOTO(out, rc = -EACCES);
319 if (strcmp(xattr_name, XATTR_NAME_LMA) == 0)
321 if (strcmp(xattr_name, XATTR_NAME_LINK) == 0)
323 } else if ((valid & OBD_MD_FLXATTR) &&
324 (strncmp(xattr_name, XATTR_NAME_ACL_ACCESS,
325 sizeof(XATTR_NAME_ACL_ACCESS) - 1) == 0 ||
326 strncmp(xattr_name, XATTR_NAME_ACL_DEFAULT,
327 sizeof(XATTR_NAME_ACL_DEFAULT) - 1) == 0)) {
328 /* currently lustre limit acl access size */
329 if (xattr_len > LUSTRE_POSIX_ACL_MAX_SIZE)
333 lockpart = MDS_INODELOCK_UPDATE;
334 /* Revoke all clients' lookup lock, since the access
335 * permissions for this inode is changed when ACL_ACCESS is
336 * set. This isn't needed for ACL_DEFAULT, since that does
337 * not change the access permissions of this inode, nor any
338 * other existing inodes. It is setting the ACLs inherited
339 * by new directories/files at create time. */
340 /* We need revoke both LOOKUP|PERM lock here, see mdt_attr_set. */
341 if (!strcmp(xattr_name, XATTR_NAME_ACL_ACCESS))
342 lockpart |= MDS_INODELOCK_PERM | MDS_INODELOCK_LOOKUP;
344 lh = &info->mti_lh[MDT_LH_PARENT];
345 /* ACLs were sent to clients under LCK_CR locks, so taking LCK_EX
347 mdt_lock_reg_init(lh, LCK_EX);
348 obj = mdt_object_find_lock(info, rr->rr_fid1, lh, lockpart);
350 GOTO(out, rc = PTR_ERR(obj));
353 rc = mdt_version_get_check_save(info, obj, 0);
355 GOTO(out_unlock, rc);
357 if (unlikely(!(valid & OBD_MD_FLCTIME))) {
358 /* This isn't strictly an error, but all current clients
359 * should set OBD_MD_FLCTIME when setting attributes. */
360 CWARN("%s: client miss to set OBD_MD_FLCTIME when "
361 "setxattr %s: [object "DFID"] [valid "LPU64"]\n",
362 mdt_obd_name(info->mti_mdt), xattr_name,
363 PFID(rr->rr_fid1), valid);
364 attr->la_ctime = cfs_time_current_sec();
366 attr->la_valid = LA_CTIME;
367 child = mdt_object_child(obj);
368 if (valid & OBD_MD_FLXATTR) {
369 char *xattr = (void *)rr->rr_eadata;
374 if (valid & OBD_MD_FLRMTLSETFACL) {
375 if (unlikely(!remote))
376 GOTO(out_unlock, rc = -EINVAL);
378 xattr_len = mdt_rmtlsetfacl(info, child,
380 (ext_acl_xattr_header *)xattr,
383 GOTO(out_unlock, rc = xattr_len);
385 xattr = (char *)new_xattr;
388 if (attr->la_flags & XATTR_REPLACE)
389 flags |= LU_XATTR_REPLACE;
391 if (attr->la_flags & XATTR_CREATE)
392 flags |= LU_XATTR_CREATE;
394 mdt_fail_write(env, info->mti_mdt->mdt_bottom,
395 OBD_FAIL_MDS_SETXATTR_WRITE);
398 buf->lb_len = xattr_len;
399 rc = mo_xattr_set(env, child, buf, xattr_name, flags);
400 /* update ctime after xattr changed */
402 ma->ma_attr_flags |= MDS_PERM_BYPASS;
403 mo_attr_set(env, child, ma);
406 } else if (valid & OBD_MD_FLXATTRRM) {
407 rc = mo_xattr_del(env, child, xattr_name);
408 /* update ctime after xattr changed */
410 ma->ma_attr_flags |= MDS_PERM_BYPASS;
411 mo_attr_set(env, child, ma);
414 CDEBUG(D_INFO, "valid bits: "LPX64"\n", valid);
418 mdt_counter_incr(req, LPROC_MDT_SETXATTR);
422 mdt_object_unlock_put(info, obj, lh, rc);
423 if (unlikely(new_xattr != NULL))
424 lustre_posix_acl_xattr_free(new_xattr, xattr_len);
426 mdt_exit_ucred(info);