1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
32 * Copyright (c) 2011, 2012, Whamcloud, Inc.
35 * This file is part of Lustre, http://www.lustre.org/
36 * Lustre is a trademark of Sun Microsystems, Inc.
38 * lustre/mdt/mdt_xattr.c
40 * Lustre Metadata Target (mdt) extended attributes management.
42 * Author: Peter Braam <braam@clusterfs.com>
43 * Author: Andreas Dilger <adilger@clusterfs.com>
44 * Author: Phil Schwan <phil@clusterfs.com>
45 * Author: Huang Hua <huanghua@clusterfs.com>
49 # define EXPORT_SYMTAB
51 #define DEBUG_SUBSYSTEM S_MDS
53 #include <lustre_acl.h>
54 #include "mdt_internal.h"
57 /* return EADATA length to the caller. negative value means error */
58 static int mdt_getxattr_pack_reply(struct mdt_thread_info * info)
60 struct req_capsule *pill = info->mti_pill ;
61 struct ptlrpc_request *req = mdt_info_req(info);
63 __u64 valid = info->mti_body->valid;
64 static const char user_string[] = "user.";
68 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETXATTR_PACK))
71 /* Determine how many bytes we need */
72 if (valid & OBD_MD_FLXATTR) {
73 xattr_name = req_capsule_client_get(pill, &RMF_NAME);
77 if (!(req->rq_export->exp_connect_flags & OBD_CONNECT_XATTR) &&
78 !strncmp(xattr_name, user_string, sizeof(user_string) - 1))
81 size = mo_xattr_get(info->mti_env,
82 mdt_object_child(info->mti_object),
83 &LU_BUF_NULL, xattr_name);
84 } else if (valid & OBD_MD_FLXATTRLS) {
85 size = mo_xattr_list(info->mti_env,
86 mdt_object_child(info->mti_object),
89 CDEBUG(D_INFO, "Valid bits: "LPX64"\n", info->mti_body->valid);
93 if (size == -ENODATA) {
95 } else if (size < 0) {
96 CERROR("Error geting EA size: %d\n", size);
100 if (info->mti_body->eadatasize != 0 &&
101 info->mti_body->eadatasize < size)
104 req_capsule_set_size(pill, &RMF_EADATA, RCL_SERVER,
105 info->mti_body->eadatasize == 0 ? 0 : size);
106 rc = req_capsule_server_pack(pill);
115 int mdt_getxattr(struct mdt_thread_info *info)
117 struct ptlrpc_request *req = mdt_info_req(info);
118 struct mdt_export_data *med = mdt_req2med(req);
119 struct md_ucred *uc = mdt_ucred(info);
120 struct mdt_body *reqbody;
121 struct mdt_body *repbody = NULL;
122 struct md_object *next;
124 __u32 remote = exp_connect_rmtclient(info->mti_exp);
129 LASSERT(info->mti_object != NULL);
130 LASSERT(lu_object_assert_exists(&info->mti_object->mot_obj.mo_lu));
132 CDEBUG(D_INODE, "getxattr "DFID"\n", PFID(&info->mti_body->fid1));
134 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
136 RETURN(err_serious(-EFAULT));
138 rc = mdt_init_ucred(info, reqbody);
140 RETURN(err_serious(rc));
142 next = mdt_object_child(info->mti_object);
144 if (info->mti_body->valid & OBD_MD_FLRMTRGETFACL) {
145 if (unlikely(!remote))
146 GOTO(out, rc = err_serious(-EINVAL));
148 perm = mdt_identity_get_perm(uc->mu_identity, remote,
150 if (!(perm & CFS_RMTACL_PERM))
151 GOTO(out, rc = err_serious(-EPERM));
153 rc = mo_permission(info->mti_env, NULL, next, NULL,
156 GOTO(out, rc = err_serious(rc));
159 easize = mdt_getxattr_pack_reply(info);
161 GOTO(out, rc = err_serious(easize));
163 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
164 LASSERT(repbody != NULL);
166 /* No need further getxattr. */
167 if (easize == 0 || reqbody->eadatasize == 0)
168 GOTO(out, rc = easize);
171 buf = &info->mti_buf;
172 buf->lb_buf = req_capsule_server_get(info->mti_pill, &RMF_EADATA);
173 buf->lb_len = easize;
175 if (info->mti_body->valid & OBD_MD_FLXATTR) {
176 int flags = CFS_IC_NOTHING;
177 char *xattr_name = req_capsule_client_get(info->mti_pill,
179 CDEBUG(D_INODE, "getxattr %s\n", xattr_name);
181 rc = mo_xattr_get(info->mti_env, next, buf, xattr_name);
183 CERROR("getxattr failed: %d\n", rc);
187 if (info->mti_body->valid &
188 (OBD_MD_FLRMTLSETFACL | OBD_MD_FLRMTLGETFACL))
190 else if (info->mti_body->valid & OBD_MD_FLRMTRGETFACL)
191 flags = CFS_IC_MAPPED;
193 if (rc > 0 && flags != CFS_IC_NOTHING) {
196 if (unlikely(!remote))
197 GOTO(out, rc = -EINVAL);
199 rc1 = lustre_posix_acl_xattr_id2client(uc,
201 (posix_acl_xattr_header *)(buf->lb_buf),
203 if (unlikely(rc1 < 0))
206 } else if (info->mti_body->valid & OBD_MD_FLXATTRLS) {
207 CDEBUG(D_INODE, "listxattr\n");
209 rc = mo_xattr_list(info->mti_env, next, buf);
211 CDEBUG(D_INFO, "listxattr failed: %d\n", rc);
218 mdt_counter_incr(req->rq_export, LPROC_MDT_GETXATTR);
219 repbody->eadatasize = rc;
222 mdt_exit_ucred(info);
226 static int mdt_rmtlsetfacl(struct mdt_thread_info *info,
227 struct md_object *next,
228 const char *xattr_name,
229 ext_acl_xattr_header *header,
230 posix_acl_xattr_header **out)
232 struct ptlrpc_request *req = mdt_info_req(info);
233 struct mdt_export_data *med = mdt_req2med(req);
234 struct md_ucred *uc = mdt_ucred(info);
235 struct lu_buf *buf = &info->mti_buf;
239 rc = lustre_ext_acl_xattr_id2server(uc, med->med_idmap, header);
243 rc = mo_xattr_get(info->mti_env, next, &LU_BUF_NULL, xattr_name);
250 if (buf->lb_len > 0) {
251 OBD_ALLOC_LARGE(buf->lb_buf, buf->lb_len);
252 if (unlikely(buf->lb_buf == NULL))
255 rc = mo_xattr_get(info->mti_env, next, buf, xattr_name);
257 CERROR("getxattr failed: %d\n", rc);
263 rc = lustre_acl_xattr_merge2posix((posix_acl_xattr_header *)(buf->lb_buf),
264 buf->lb_len, header, out);
268 if (rc <= 0 && buf->lb_buf != NULL)
269 OBD_FREE_LARGE(buf->lb_buf, buf->lb_len);
273 int mdt_reint_setxattr(struct mdt_thread_info *info,
274 struct mdt_lock_handle *unused)
276 struct ptlrpc_request *req = mdt_info_req(info);
277 struct md_ucred *uc = mdt_ucred(info);
278 struct mdt_lock_handle *lh;
279 const struct lu_env *env = info->mti_env;
280 struct lu_buf *buf = &info->mti_buf;
281 struct mdt_reint_record *rr = &info->mti_rr;
282 struct md_attr *ma = &info->mti_attr;
283 struct lu_attr *attr = &info->mti_attr.ma_attr;
284 struct mdt_object *obj;
285 struct md_object *child;
286 __u64 valid = attr->la_valid;
287 const char *xattr_name = rr->rr_name;
288 int xattr_len = rr->rr_eadatalen;
291 posix_acl_xattr_header *new_xattr = NULL;
292 __u32 remote = exp_connect_rmtclient(info->mti_exp);
296 CDEBUG(D_INODE, "setxattr for "DFID"\n", PFID(rr->rr_fid1));
298 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SETXATTR))
299 RETURN(err_serious(-ENOMEM));
301 CDEBUG(D_INODE, "%s xattr %s\n",
302 valid & OBD_MD_FLXATTR ? "set" : "remove", xattr_name);
304 rc = mdt_init_ucred_reint(info);
308 if (valid & OBD_MD_FLRMTRSETFACL) {
309 if (unlikely(!remote))
310 GOTO(out, rc = err_serious(-EINVAL));
312 perm = mdt_identity_get_perm(uc->mu_identity, remote,
314 if (!(perm & CFS_RMTACL_PERM))
315 GOTO(out, rc = err_serious(-EPERM));
318 if (strncmp(xattr_name, XATTR_USER_PREFIX,
319 sizeof(XATTR_USER_PREFIX) - 1) == 0) {
320 if (!(req->rq_export->exp_connect_flags & OBD_CONNECT_XATTR))
321 GOTO(out, rc = -EOPNOTSUPP);
322 if (strcmp(xattr_name, XATTR_NAME_LOV) == 0)
323 GOTO(out, rc = -EACCES);
324 if (strcmp(xattr_name, XATTR_NAME_LMA) == 0)
326 if (strcmp(xattr_name, XATTR_NAME_LINK) == 0)
328 } else if ((valid & OBD_MD_FLXATTR) &&
329 (strncmp(xattr_name, XATTR_NAME_ACL_ACCESS,
330 sizeof(XATTR_NAME_ACL_ACCESS) - 1) == 0 ||
331 strncmp(xattr_name, XATTR_NAME_ACL_DEFAULT,
332 sizeof(XATTR_NAME_ACL_DEFAULT) - 1) == 0)) {
333 /* currently lustre limit acl access size */
334 if (xattr_len > LUSTRE_POSIX_ACL_MAX_SIZE)
338 lockpart = MDS_INODELOCK_UPDATE;
339 /* Revoke all clients' lookup lock, since the access
340 * permissions for this inode is changed when ACL_ACCESS is
341 * set. This isn't needed for ACL_DEFAULT, since that does
342 * not change the access permissions of this inode, nor any
343 * other existing inodes. It is setting the ACLs inherited
344 * by new directories/files at create time. */
345 if (!strcmp(xattr_name, XATTR_NAME_ACL_ACCESS))
346 lockpart |= MDS_INODELOCK_LOOKUP;
348 lh = &info->mti_lh[MDT_LH_PARENT];
349 /* ACLs were sent to clients under LCK_CR locks, so taking LCK_EX
351 mdt_lock_reg_init(lh, LCK_EX);
352 obj = mdt_object_find_lock(info, rr->rr_fid1, lh, lockpart);
354 GOTO(out, rc = PTR_ERR(obj));
357 rc = mdt_version_get_check_save(info, obj, 0);
359 GOTO(out_unlock, rc);
361 if (unlikely(!(valid & OBD_MD_FLCTIME))) {
362 /* This isn't strictly an error, but all current clients
363 * should set OBD_MD_FLCTIME when setting attributes. */
364 CWARN("%s: client miss to set OBD_MD_FLCTIME when "
365 "setxattr %s: [object "DFID"] [valid "LPU64"]\n",
366 info->mti_exp->exp_obd->obd_name, xattr_name,
367 PFID(rr->rr_fid1), valid);
368 attr->la_ctime = cfs_time_current_sec();
370 attr->la_valid = LA_CTIME;
371 child = mdt_object_child(obj);
372 if (valid & OBD_MD_FLXATTR) {
373 char *xattr = (void *)rr->rr_eadata;
378 if (valid & OBD_MD_FLRMTLSETFACL) {
379 if (unlikely(!remote))
380 GOTO(out_unlock, rc = -EINVAL);
382 xattr_len = mdt_rmtlsetfacl(info, child,
384 (ext_acl_xattr_header *)xattr,
387 GOTO(out_unlock, rc = xattr_len);
389 xattr = (char *)new_xattr;
392 if (attr->la_flags & XATTR_REPLACE)
393 flags |= LU_XATTR_REPLACE;
395 if (attr->la_flags & XATTR_CREATE)
396 flags |= LU_XATTR_CREATE;
398 mdt_fail_write(env, info->mti_mdt->mdt_bottom,
399 OBD_FAIL_MDS_SETXATTR_WRITE);
402 buf->lb_len = xattr_len;
403 rc = mo_xattr_set(env, child, buf, xattr_name, flags);
404 /* update ctime after xattr changed */
406 ma->ma_attr_flags |= MDS_PERM_BYPASS;
407 mo_attr_set(env, child, ma);
410 } else if (valid & OBD_MD_FLXATTRRM) {
411 rc = mo_xattr_del(env, child, xattr_name);
412 /* update ctime after xattr changed */
414 ma->ma_attr_flags |= MDS_PERM_BYPASS;
415 mo_attr_set(env, child, ma);
418 CDEBUG(D_INFO, "valid bits: "LPX64"\n", valid);
422 mdt_counter_incr(req->rq_export, LPROC_MDT_SETXATTR);
426 mdt_object_unlock_put(info, obj, lh, rc);
427 if (unlikely(new_xattr != NULL))
428 lustre_posix_acl_xattr_free(new_xattr, xattr_len);
430 mdt_exit_ucred(info);