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);
59 static const char user_string[] = "user.";
63 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETXATTR_PACK))
66 valid = info->mti_body->valid & (OBD_MD_FLXATTR | OBD_MD_FLXATTRLS);
68 /* Determine how many bytes we need */
69 if (valid == OBD_MD_FLXATTR) {
70 xattr_name = req_capsule_client_get(pill, &RMF_NAME);
74 if (!(exp_connect_flags(req->rq_export) & OBD_CONNECT_XATTR) &&
75 !strncmp(xattr_name, user_string, sizeof(user_string) - 1))
78 size = mo_xattr_get(info->mti_env,
79 mdt_object_child(info->mti_object),
80 &LU_BUF_NULL, xattr_name);
81 } else if (valid == OBD_MD_FLXATTRLS) {
82 size = mo_xattr_list(info->mti_env,
83 mdt_object_child(info->mti_object),
85 } else if (valid == OBD_MD_FLXATTRALL) {
86 /* N.B. eadatasize = 0 is not valid for FLXATTRALL */
87 /* We could calculate accurate sizes, but this would
88 * introduce a lot of overhead, let's do it later... */
89 size = info->mti_body->eadatasize;
90 req_capsule_set_size(pill, &RMF_EAVALS, RCL_SERVER, size);
91 req_capsule_set_size(pill, &RMF_EAVALS_LENS, RCL_SERVER, size);
93 CDEBUG(D_INFO, "Valid bits: "LPX64"\n", info->mti_body->valid);
97 if (size == -ENODATA) {
99 } else if (size < 0) {
100 CERROR("Error geting EA size: %d\n", size);
104 if (info->mti_body->eadatasize != 0 &&
105 info->mti_body->eadatasize < size)
108 req_capsule_set_size(pill, &RMF_EADATA, RCL_SERVER,
109 info->mti_body->eadatasize == 0 ? 0 : size);
110 rc = req_capsule_server_pack(pill);
120 mdt_getxattr_one(struct mdt_thread_info *info,
121 char *xattr_name, struct md_object *next,
122 struct lu_buf *buf, struct mdt_export_data *med,
125 __u32 remote = exp_connect_rmtclient(info->mti_exp);
126 int flags = CFS_IC_NOTHING, rc;
130 CDEBUG(D_INODE, "getxattr %s\n", xattr_name);
132 rc = mo_xattr_get(info->mti_env, next, buf, xattr_name);
134 CERROR("getxattr failed: %d\n", rc);
138 if (info->mti_body->valid &
139 (OBD_MD_FLRMTLSETFACL | OBD_MD_FLRMTLGETFACL))
141 else if (info->mti_body->valid & OBD_MD_FLRMTRGETFACL)
142 flags = CFS_IC_MAPPED;
144 if (rc > 0 && flags != CFS_IC_NOTHING) {
147 if (unlikely(!remote))
148 GOTO(out, rc = -EINVAL);
150 rc1 = lustre_posix_acl_xattr_id2client(uc,
152 (posix_acl_xattr_header *)(buf->lb_buf),
154 if (unlikely(rc1 < 0))
162 static int mdt_getxattr_all(struct mdt_thread_info *info,
163 struct mdt_body *reqbody, struct mdt_body *repbody,
164 struct lu_buf *buf, struct md_object *next)
166 const struct lu_env *env = info->mti_env;
167 struct ptlrpc_request *req = mdt_info_req(info);
168 struct mdt_export_data *med = mdt_req2med(req);
169 struct lu_ucred *uc = mdt_ucred(info);
170 char *v, *b, *eadatahead, *eadatatail;
172 int eadatasize, eavallen = 0, eavallens = 0, rc;
177 * The format of the pill is the following:
178 * EADATA: attr1\0attr2\0...attrn\0
179 * EAVALS: val1val2...valn
180 * EAVALS_LENS: 4,4,...4
183 eadatahead = buf->lb_buf;
185 /* Fill out EADATA first */
186 eadatasize = mo_xattr_list(env, next, buf);
188 GOTO(out, rc = eadatasize);
190 eadatatail = eadatahead + eadatasize;
192 v = req_capsule_server_get(info->mti_pill, &RMF_EAVALS);
193 sizes = req_capsule_server_get(info->mti_pill, &RMF_EAVALS_LENS);
195 /* Fill out EAVALS and EAVALS_LENS */
196 for (b = eadatahead; b < eadatatail; b += strlen(b) + 1, v += rc) {
198 buf->lb_len = reqbody->eadatasize - eavallen;
199 rc = mdt_getxattr_one(info, b, next, buf, med, uc);
203 sizes[eavallens] = rc;
208 repbody->aclsize = eavallen;
209 repbody->max_mdsize = eavallens;
211 req_capsule_shrink(info->mti_pill, &RMF_EAVALS, eavallen, RCL_SERVER);
212 req_capsule_shrink(info->mti_pill, &RMF_EAVALS_LENS,
213 eavallens * sizeof(__u32), RCL_SERVER);
214 req_capsule_shrink(info->mti_pill, &RMF_EADATA, eadatasize, RCL_SERVER);
216 GOTO(out, rc = eadatasize);
221 int mdt_getxattr(struct mdt_thread_info *info)
223 struct ptlrpc_request *req = mdt_info_req(info);
224 struct mdt_export_data *med = mdt_req2med(req);
225 struct lu_ucred *uc = mdt_ucred(info);
226 struct mdt_body *reqbody;
227 struct mdt_body *repbody = NULL;
228 struct md_object *next;
230 __u32 remote = exp_connect_rmtclient(info->mti_exp);
236 LASSERT(info->mti_object != NULL);
237 LASSERT(lu_object_assert_exists(&info->mti_object->mot_obj));
239 CDEBUG(D_INODE, "getxattr "DFID"\n", PFID(&info->mti_body->fid1));
241 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
243 RETURN(err_serious(-EFAULT));
245 rc = mdt_init_ucred(info, reqbody);
247 RETURN(err_serious(rc));
249 next = mdt_object_child(info->mti_object);
251 if (info->mti_body->valid & OBD_MD_FLRMTRGETFACL) {
252 if (unlikely(!remote))
253 GOTO(out, rc = err_serious(-EINVAL));
255 perm = mdt_identity_get_perm(uc->uc_identity, remote,
257 if (!(perm & CFS_RMTACL_PERM))
258 GOTO(out, rc = err_serious(-EPERM));
260 rc = mo_permission(info->mti_env, NULL, next, NULL,
263 GOTO(out, rc = err_serious(rc));
266 easize = mdt_getxattr_pack_reply(info);
268 GOTO(out, rc = err_serious(easize));
270 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
271 LASSERT(repbody != NULL);
273 /* No need further getxattr. */
274 if (easize == 0 || reqbody->eadatasize == 0)
275 GOTO(out, rc = easize);
277 buf = &info->mti_buf;
278 buf->lb_buf = req_capsule_server_get(info->mti_pill, &RMF_EADATA);
279 buf->lb_len = easize;
281 valid = info->mti_body->valid & (OBD_MD_FLXATTR | OBD_MD_FLXATTRLS);
283 if (valid == OBD_MD_FLXATTR) {
284 char *xattr_name = req_capsule_client_get(info->mti_pill,
286 rc = mdt_getxattr_one(info, xattr_name, next, buf, med, uc);
287 } else if (valid == OBD_MD_FLXATTRLS) {
288 CDEBUG(D_INODE, "listxattr\n");
290 rc = mo_xattr_list(info->mti_env, next, buf);
292 CDEBUG(D_INFO, "listxattr failed: %d\n", rc);
293 } else if (valid == OBD_MD_FLXATTRALL) {
294 rc = mdt_getxattr_all(info, reqbody, repbody,
302 mdt_counter_incr(req, LPROC_MDT_GETXATTR);
303 repbody->eadatasize = rc;
306 mdt_exit_ucred(info);
310 static int mdt_rmtlsetfacl(struct mdt_thread_info *info,
311 struct md_object *next,
312 const char *xattr_name,
313 ext_acl_xattr_header *header,
314 posix_acl_xattr_header **out)
316 struct ptlrpc_request *req = mdt_info_req(info);
317 struct mdt_export_data *med = mdt_req2med(req);
318 struct lu_ucred *uc = mdt_ucred(info);
319 struct lu_buf *buf = &info->mti_buf;
323 rc = lustre_ext_acl_xattr_id2server(uc, med->med_idmap, header);
327 rc = mo_xattr_get(info->mti_env, next, &LU_BUF_NULL, xattr_name);
334 if (buf->lb_len > 0) {
335 OBD_ALLOC_LARGE(buf->lb_buf, buf->lb_len);
336 if (unlikely(buf->lb_buf == NULL))
339 rc = mo_xattr_get(info->mti_env, next, buf, xattr_name);
341 CERROR("getxattr failed: %d\n", rc);
347 rc = lustre_acl_xattr_merge2posix((posix_acl_xattr_header *)(buf->lb_buf),
348 buf->lb_len, header, out);
352 if (rc <= 0 && buf->lb_buf != NULL)
353 OBD_FREE_LARGE(buf->lb_buf, buf->lb_len);
357 int mdt_reint_setxattr(struct mdt_thread_info *info,
358 struct mdt_lock_handle *unused)
360 struct ptlrpc_request *req = mdt_info_req(info);
361 struct lu_ucred *uc = mdt_ucred(info);
362 struct mdt_lock_handle *lh;
363 const struct lu_env *env = info->mti_env;
364 struct lu_buf *buf = &info->mti_buf;
365 struct mdt_reint_record *rr = &info->mti_rr;
366 struct md_attr *ma = &info->mti_attr;
367 struct lu_attr *attr = &info->mti_attr.ma_attr;
368 struct mdt_object *obj;
369 struct md_object *child;
370 __u64 valid = attr->la_valid;
371 const char *xattr_name = rr->rr_name;
372 int xattr_len = rr->rr_eadatalen;
375 posix_acl_xattr_header *new_xattr = NULL;
376 __u32 remote = exp_connect_rmtclient(info->mti_exp);
380 CDEBUG(D_INODE, "setxattr for "DFID"\n", PFID(rr->rr_fid1));
382 if (info->mti_dlm_req)
383 ldlm_request_cancel(req, info->mti_dlm_req, 0);
385 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SETXATTR))
386 RETURN(err_serious(-ENOMEM));
388 CDEBUG(D_INODE, "%s xattr %s\n",
389 valid & OBD_MD_FLXATTR ? "set" : "remove", xattr_name);
391 rc = mdt_init_ucred_reint(info);
395 if (valid & OBD_MD_FLRMTRSETFACL) {
396 if (unlikely(!remote))
397 GOTO(out, rc = err_serious(-EINVAL));
399 perm = mdt_identity_get_perm(uc->uc_identity, remote,
401 if (!(perm & CFS_RMTACL_PERM))
402 GOTO(out, rc = err_serious(-EPERM));
405 if (strncmp(xattr_name, XATTR_USER_PREFIX,
406 sizeof(XATTR_USER_PREFIX) - 1) == 0) {
407 if (!(exp_connect_flags(req->rq_export) & OBD_CONNECT_XATTR))
408 GOTO(out, rc = -EOPNOTSUPP);
409 if (strcmp(xattr_name, XATTR_NAME_LOV) == 0)
410 GOTO(out, rc = -EACCES);
411 if (strcmp(xattr_name, XATTR_NAME_LMA) == 0)
413 if (strcmp(xattr_name, XATTR_NAME_LINK) == 0)
415 } else if ((valid & OBD_MD_FLXATTR) &&
416 (strncmp(xattr_name, XATTR_NAME_ACL_ACCESS,
417 sizeof(XATTR_NAME_ACL_ACCESS) - 1) == 0 ||
418 strncmp(xattr_name, XATTR_NAME_ACL_DEFAULT,
419 sizeof(XATTR_NAME_ACL_DEFAULT) - 1) == 0)) {
420 /* currently lustre limit acl access size */
421 if (xattr_len > LUSTRE_POSIX_ACL_MAX_SIZE)
425 lockpart = MDS_INODELOCK_UPDATE;
426 /* Revoke all clients' lookup lock, since the access
427 * permissions for this inode is changed when ACL_ACCESS is
428 * set. This isn't needed for ACL_DEFAULT, since that does
429 * not change the access permissions of this inode, nor any
430 * other existing inodes. It is setting the ACLs inherited
431 * by new directories/files at create time. */
432 /* We need revoke both LOOKUP|PERM lock here, see mdt_attr_set. */
433 if (!strcmp(xattr_name, XATTR_NAME_ACL_ACCESS))
434 lockpart |= MDS_INODELOCK_PERM | MDS_INODELOCK_LOOKUP;
435 /* We need to take the lock on behalf of old clients so that newer
436 * clients flush their xattr caches */
438 lockpart |= MDS_INODELOCK_XATTR;
440 lh = &info->mti_lh[MDT_LH_PARENT];
441 /* ACLs were sent to clients under LCK_CR locks, so taking LCK_EX
443 mdt_lock_reg_init(lh, LCK_EX);
444 obj = mdt_object_find_lock(info, rr->rr_fid1, lh, lockpart);
446 GOTO(out, rc = PTR_ERR(obj));
449 rc = mdt_version_get_check_save(info, obj, 0);
451 GOTO(out_unlock, rc);
453 if (unlikely(!(valid & OBD_MD_FLCTIME))) {
454 /* This isn't strictly an error, but all current clients
455 * should set OBD_MD_FLCTIME when setting attributes. */
456 CWARN("%s: client miss to set OBD_MD_FLCTIME when "
457 "setxattr %s: [object "DFID"] [valid "LPU64"]\n",
458 mdt_obd_name(info->mti_mdt), xattr_name,
459 PFID(rr->rr_fid1), valid);
460 attr->la_ctime = cfs_time_current_sec();
462 attr->la_valid = LA_CTIME;
463 child = mdt_object_child(obj);
464 if (valid & OBD_MD_FLXATTR) {
465 char *xattr = (void *)rr->rr_eadata;
470 if (valid & OBD_MD_FLRMTLSETFACL) {
471 if (unlikely(!remote))
472 GOTO(out_unlock, rc = -EINVAL);
474 xattr_len = mdt_rmtlsetfacl(info, child,
476 (ext_acl_xattr_header *)xattr,
479 GOTO(out_unlock, rc = xattr_len);
481 xattr = (char *)new_xattr;
484 if (attr->la_flags & XATTR_REPLACE)
485 flags |= LU_XATTR_REPLACE;
487 if (attr->la_flags & XATTR_CREATE)
488 flags |= LU_XATTR_CREATE;
490 mdt_fail_write(env, info->mti_mdt->mdt_bottom,
491 OBD_FAIL_MDS_SETXATTR_WRITE);
494 buf->lb_len = xattr_len;
495 rc = mo_xattr_set(env, child, buf, xattr_name, flags);
496 /* update ctime after xattr changed */
498 ma->ma_attr_flags |= MDS_PERM_BYPASS;
499 mo_attr_set(env, child, ma);
502 } else if (valid & OBD_MD_FLXATTRRM) {
503 rc = mo_xattr_del(env, child, xattr_name);
504 /* update ctime after xattr changed */
506 ma->ma_attr_flags |= MDS_PERM_BYPASS;
507 mo_attr_set(env, child, ma);
510 CDEBUG(D_INFO, "valid bits: "LPX64"\n", valid);
514 mdt_counter_incr(req, LPROC_MDT_SETXATTR);
518 mdt_object_unlock_put(info, obj, lh, rc);
519 if (unlikely(new_xattr != NULL))
520 lustre_posix_acl_xattr_free(new_xattr, xattr_len);
522 mdt_exit_ucred(info);