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 int mdt_getxattr(struct mdt_thread_info *info)
164 struct ptlrpc_request *req = mdt_info_req(info);
165 struct mdt_export_data *med = mdt_req2med(req);
166 struct lu_ucred *uc = mdt_ucred(info);
167 struct mdt_body *reqbody;
168 struct mdt_body *repbody = NULL;
169 struct md_object *next;
171 __u32 remote = exp_connect_rmtclient(info->mti_exp);
177 LASSERT(info->mti_object != NULL);
178 LASSERT(lu_object_assert_exists(&info->mti_object->mot_obj));
180 CDEBUG(D_INODE, "getxattr "DFID"\n", PFID(&info->mti_body->fid1));
182 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
184 RETURN(err_serious(-EFAULT));
186 rc = mdt_init_ucred(info, reqbody);
188 RETURN(err_serious(rc));
190 down_read(&info->mti_object->mot_xattr_sem);
192 next = mdt_object_child(info->mti_object);
194 if (info->mti_body->valid & OBD_MD_FLRMTRGETFACL) {
195 if (unlikely(!remote))
196 GOTO(out, rc = err_serious(-EINVAL));
198 perm = mdt_identity_get_perm(uc->uc_identity, remote,
200 if (!(perm & CFS_RMTACL_PERM))
201 GOTO(out, rc = err_serious(-EPERM));
203 rc = mo_permission(info->mti_env, NULL, next, NULL,
206 GOTO(out, rc = err_serious(rc));
209 easize = mdt_getxattr_pack_reply(info);
211 GOTO(out, rc = err_serious(easize));
213 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
214 LASSERT(repbody != NULL);
216 /* No need further getxattr. */
217 if (easize == 0 || reqbody->eadatasize == 0)
218 GOTO(out, rc = easize);
220 buf = &info->mti_buf;
221 buf->lb_buf = req_capsule_server_get(info->mti_pill, &RMF_EADATA);
222 buf->lb_len = easize;
224 valid = info->mti_body->valid & (OBD_MD_FLXATTR | OBD_MD_FLXATTRLS);
226 if (valid == OBD_MD_FLXATTR) {
227 char *xattr_name = req_capsule_client_get(info->mti_pill,
229 rc = mdt_getxattr_one(info, xattr_name, next, buf, med, uc);
230 } else if (valid == OBD_MD_FLXATTRLS) {
231 CDEBUG(D_INODE, "listxattr\n");
233 rc = mo_xattr_list(info->mti_env, next, buf);
235 CDEBUG(D_INFO, "listxattr failed: %d\n", rc);
236 } else if (valid == OBD_MD_FLXATTRALL) {
238 * The format of the pill is the following:
239 * EADATA: attr1\0attr2\0...attrn\0
240 * EAVALS: val1val2...valn
241 * EAVALS_LENS: 4,4,...4
245 int eadatasize, eavallen = 0, eavallens = 0;
246 struct lu_buf buf2 = { .lb_len = reqbody->eadatasize };
248 /* Fill out EADATA */
249 eadatasize = mo_xattr_list(info->mti_env, next, buf);
251 GOTO(out, rc = eadatasize);
253 v = req_capsule_server_get(info->mti_pill, &RMF_EAVALS);
254 sizes = req_capsule_server_get(info->mti_pill,
257 /* Fill out EAVALS and EAVALS_LENS */
258 for (b = buf->lb_buf;
259 b < (char *)buf->lb_buf + eadatasize;
260 b += strlen(b) + 1, v += rc) {
262 rc = mdt_getxattr_one(info, b, next, &buf2, med, uc);
265 sizes[eavallens] = rc;
271 repbody->aclsize = eavallen;
272 repbody->max_mdsize = eavallens;
274 req_capsule_shrink(info->mti_pill, &RMF_EAVALS,
275 eavallen, RCL_SERVER);
276 req_capsule_shrink(info->mti_pill, &RMF_EAVALS_LENS,
277 eavallens * sizeof(__u32), RCL_SERVER);
278 req_capsule_shrink(info->mti_pill, &RMF_EADATA,
279 eadatasize, RCL_SERVER);
286 up_read(&info->mti_object->mot_xattr_sem);
289 mdt_counter_incr(req, LPROC_MDT_GETXATTR);
290 repbody->eadatasize = rc;
293 mdt_exit_ucred(info);
297 static int mdt_rmtlsetfacl(struct mdt_thread_info *info,
298 struct md_object *next,
299 const char *xattr_name,
300 ext_acl_xattr_header *header,
301 posix_acl_xattr_header **out)
303 struct ptlrpc_request *req = mdt_info_req(info);
304 struct mdt_export_data *med = mdt_req2med(req);
305 struct lu_ucred *uc = mdt_ucred(info);
306 struct lu_buf *buf = &info->mti_buf;
310 rc = lustre_ext_acl_xattr_id2server(uc, med->med_idmap, header);
314 rc = mo_xattr_get(info->mti_env, next, &LU_BUF_NULL, xattr_name);
321 if (buf->lb_len > 0) {
322 OBD_ALLOC_LARGE(buf->lb_buf, buf->lb_len);
323 if (unlikely(buf->lb_buf == NULL))
326 rc = mo_xattr_get(info->mti_env, next, buf, xattr_name);
328 CERROR("getxattr failed: %d\n", rc);
334 rc = lustre_acl_xattr_merge2posix((posix_acl_xattr_header *)(buf->lb_buf),
335 buf->lb_len, header, out);
339 if (rc <= 0 && buf->lb_buf != NULL)
340 OBD_FREE_LARGE(buf->lb_buf, buf->lb_len);
344 int mdt_reint_setxattr(struct mdt_thread_info *info,
345 struct mdt_lock_handle *unused)
347 struct ptlrpc_request *req = mdt_info_req(info);
348 struct lu_ucred *uc = mdt_ucred(info);
349 struct mdt_lock_handle *lh;
350 const struct lu_env *env = info->mti_env;
351 struct lu_buf *buf = &info->mti_buf;
352 struct mdt_reint_record *rr = &info->mti_rr;
353 struct md_attr *ma = &info->mti_attr;
354 struct lu_attr *attr = &info->mti_attr.ma_attr;
355 struct mdt_object *obj;
356 struct md_object *child;
357 __u64 valid = attr->la_valid;
358 const char *xattr_name = rr->rr_name;
359 int xattr_len = rr->rr_eadatalen;
362 posix_acl_xattr_header *new_xattr = NULL;
363 __u32 remote = exp_connect_rmtclient(info->mti_exp);
367 CDEBUG(D_INODE, "setxattr for "DFID"\n", PFID(rr->rr_fid1));
369 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SETXATTR))
370 RETURN(err_serious(-ENOMEM));
372 CDEBUG(D_INODE, "%s xattr %s\n",
373 valid & OBD_MD_FLXATTR ? "set" : "remove", xattr_name);
375 rc = mdt_init_ucred_reint(info);
379 if (valid & OBD_MD_FLRMTRSETFACL) {
380 if (unlikely(!remote))
381 GOTO(out, rc = err_serious(-EINVAL));
383 perm = mdt_identity_get_perm(uc->uc_identity, remote,
385 if (!(perm & CFS_RMTACL_PERM))
386 GOTO(out, rc = err_serious(-EPERM));
389 if (strncmp(xattr_name, XATTR_USER_PREFIX,
390 sizeof(XATTR_USER_PREFIX) - 1) == 0) {
391 if (!(exp_connect_flags(req->rq_export) & OBD_CONNECT_XATTR))
392 GOTO(out, rc = -EOPNOTSUPP);
393 if (strcmp(xattr_name, XATTR_NAME_LOV) == 0)
394 GOTO(out, rc = -EACCES);
395 if (strcmp(xattr_name, XATTR_NAME_LMA) == 0)
397 if (strcmp(xattr_name, XATTR_NAME_LINK) == 0)
399 } else if ((valid & OBD_MD_FLXATTR) &&
400 (strncmp(xattr_name, XATTR_NAME_ACL_ACCESS,
401 sizeof(XATTR_NAME_ACL_ACCESS) - 1) == 0 ||
402 strncmp(xattr_name, XATTR_NAME_ACL_DEFAULT,
403 sizeof(XATTR_NAME_ACL_DEFAULT) - 1) == 0)) {
404 /* currently lustre limit acl access size */
405 if (xattr_len > LUSTRE_POSIX_ACL_MAX_SIZE)
409 lockpart = MDS_INODELOCK_UPDATE;
410 /* Revoke all clients' lookup lock, since the access
411 * permissions for this inode is changed when ACL_ACCESS is
412 * set. This isn't needed for ACL_DEFAULT, since that does
413 * not change the access permissions of this inode, nor any
414 * other existing inodes. It is setting the ACLs inherited
415 * by new directories/files at create time. */
416 /* We need revoke both LOOKUP|PERM lock here, see mdt_attr_set. */
417 if (!strcmp(xattr_name, XATTR_NAME_ACL_ACCESS))
418 lockpart |= MDS_INODELOCK_PERM | MDS_INODELOCK_LOOKUP;
420 /* We need to take the lock on behalf of old clients so that newer
421 * clients flush their xattr caches */
422 if (!(valid & OBD_MD_FLXATTRLOCKED))
423 lockpart |= MDS_INODELOCK_XATTR;
425 lh = &info->mti_lh[MDT_LH_PARENT];
426 /* ACLs were sent to clients under LCK_CR locks, so taking LCK_EX
428 mdt_lock_reg_init(lh, LCK_EX);
429 obj = mdt_object_find_lock(info, rr->rr_fid1, lh, lockpart);
431 GOTO(out, rc = PTR_ERR(obj));
433 down_write(&obj->mot_xattr_sem);
436 rc = mdt_version_get_check_save(info, obj, 0);
438 GOTO(out_unlock, rc);
440 if (unlikely(!(valid & OBD_MD_FLCTIME))) {
441 /* This isn't strictly an error, but all current clients
442 * should set OBD_MD_FLCTIME when setting attributes. */
443 CWARN("%s: client miss to set OBD_MD_FLCTIME when "
444 "setxattr %s: [object "DFID"] [valid "LPU64"]\n",
445 mdt_obd_name(info->mti_mdt), xattr_name,
446 PFID(rr->rr_fid1), valid);
447 attr->la_ctime = cfs_time_current_sec();
449 attr->la_valid = LA_CTIME;
450 child = mdt_object_child(obj);
451 if (valid & OBD_MD_FLXATTR) {
452 char *xattr = (void *)rr->rr_eadata;
457 if (valid & OBD_MD_FLRMTLSETFACL) {
458 if (unlikely(!remote))
459 GOTO(out_unlock, rc = -EINVAL);
461 xattr_len = mdt_rmtlsetfacl(info, child,
463 (ext_acl_xattr_header *)xattr,
466 GOTO(out_unlock, rc = xattr_len);
468 xattr = (char *)new_xattr;
471 if (attr->la_flags & XATTR_REPLACE)
472 flags |= LU_XATTR_REPLACE;
474 if (attr->la_flags & XATTR_CREATE)
475 flags |= LU_XATTR_CREATE;
477 mdt_fail_write(env, info->mti_mdt->mdt_bottom,
478 OBD_FAIL_MDS_SETXATTR_WRITE);
481 buf->lb_len = xattr_len;
482 rc = mo_xattr_set(env, child, buf, xattr_name, flags);
483 /* update ctime after xattr changed */
485 ma->ma_attr_flags |= MDS_PERM_BYPASS;
486 mo_attr_set(env, child, ma);
489 } else if (valid & OBD_MD_FLXATTRRM) {
490 rc = mo_xattr_del(env, child, xattr_name);
491 /* update ctime after xattr changed */
493 ma->ma_attr_flags |= MDS_PERM_BYPASS;
494 mo_attr_set(env, child, ma);
497 CDEBUG(D_INFO, "valid bits: "LPX64"\n", valid);
501 mdt_counter_incr(req, LPROC_MDT_SETXATTR);
505 up_write(&obj->mot_xattr_sem);
506 mdt_object_unlock_put(info, obj, lh, rc);
507 if (unlikely(new_xattr != NULL))
508 lustre_posix_acl_xattr_free(new_xattr, xattr_len);
510 mdt_exit_ucred(info);