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, 2012, Whamcloud, Inc.
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>
47 # define EXPORT_SYMTAB
49 #define DEBUG_SUBSYSTEM S_MDS
51 #include <lustre_acl.h>
52 #include "mdt_internal.h"
55 /* return EADATA length to the caller. negative value means error */
56 static int mdt_getxattr_pack_reply(struct mdt_thread_info * info)
58 struct req_capsule *pill = info->mti_pill ;
59 struct ptlrpc_request *req = mdt_info_req(info);
61 __u64 valid = info->mti_body->valid;
62 static const char user_string[] = "user.";
66 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETXATTR_PACK))
69 /* Determine how many bytes we need */
70 if (valid & OBD_MD_FLXATTR) {
71 xattr_name = req_capsule_client_get(pill, &RMF_NAME);
75 if (!(req->rq_export->exp_connect_flags & OBD_CONNECT_XATTR) &&
76 !strncmp(xattr_name, user_string, sizeof(user_string) - 1))
79 size = mo_xattr_get(info->mti_env,
80 mdt_object_child(info->mti_object),
81 &LU_BUF_NULL, xattr_name);
82 } else if (valid & OBD_MD_FLXATTRLS) {
83 size = mo_xattr_list(info->mti_env,
84 mdt_object_child(info->mti_object),
87 CDEBUG(D_INFO, "Valid bits: "LPX64"\n", info->mti_body->valid);
91 if (size == -ENODATA) {
93 } else if (size < 0) {
94 CERROR("Error geting EA size: %d\n", size);
98 if (info->mti_body->eadatasize != 0 &&
99 info->mti_body->eadatasize < size)
102 req_capsule_set_size(pill, &RMF_EADATA, RCL_SERVER,
103 info->mti_body->eadatasize == 0 ? 0 : size);
104 rc = req_capsule_server_pack(pill);
113 int mdt_getxattr(struct mdt_thread_info *info)
115 struct ptlrpc_request *req = mdt_info_req(info);
116 struct mdt_export_data *med = mdt_req2med(req);
117 struct md_ucred *uc = mdt_ucred(info);
118 struct mdt_body *reqbody;
119 struct mdt_body *repbody = NULL;
120 struct md_object *next;
122 __u32 remote = exp_connect_rmtclient(info->mti_exp);
127 LASSERT(info->mti_object != NULL);
128 LASSERT(lu_object_assert_exists(&info->mti_object->mot_obj.mo_lu));
130 CDEBUG(D_INODE, "getxattr "DFID"\n", PFID(&info->mti_body->fid1));
132 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
134 RETURN(err_serious(-EFAULT));
136 rc = mdt_init_ucred(info, reqbody);
138 RETURN(err_serious(rc));
140 next = mdt_object_child(info->mti_object);
142 if (info->mti_body->valid & OBD_MD_FLRMTRGETFACL) {
143 if (unlikely(!remote))
144 GOTO(out, rc = err_serious(-EINVAL));
146 perm = mdt_identity_get_perm(uc->mu_identity, remote,
148 if (!(perm & CFS_RMTACL_PERM))
149 GOTO(out, rc = err_serious(-EPERM));
151 rc = mo_permission(info->mti_env, NULL, next, NULL,
154 GOTO(out, rc = err_serious(rc));
157 easize = mdt_getxattr_pack_reply(info);
159 GOTO(out, rc = err_serious(easize));
161 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
162 LASSERT(repbody != NULL);
164 /* No need further getxattr. */
165 if (easize == 0 || reqbody->eadatasize == 0)
166 GOTO(out, rc = easize);
169 buf = &info->mti_buf;
170 buf->lb_buf = req_capsule_server_get(info->mti_pill, &RMF_EADATA);
171 buf->lb_len = easize;
173 if (info->mti_body->valid & OBD_MD_FLXATTR) {
174 int flags = CFS_IC_NOTHING;
175 char *xattr_name = req_capsule_client_get(info->mti_pill,
177 CDEBUG(D_INODE, "getxattr %s\n", xattr_name);
179 rc = mo_xattr_get(info->mti_env, next, buf, xattr_name);
181 CERROR("getxattr failed: %d\n", rc);
185 if (info->mti_body->valid &
186 (OBD_MD_FLRMTLSETFACL | OBD_MD_FLRMTLGETFACL))
188 else if (info->mti_body->valid & OBD_MD_FLRMTRGETFACL)
189 flags = CFS_IC_MAPPED;
191 if (rc > 0 && flags != CFS_IC_NOTHING) {
194 if (unlikely(!remote))
195 GOTO(out, rc = -EINVAL);
197 rc1 = lustre_posix_acl_xattr_id2client(uc,
199 (posix_acl_xattr_header *)(buf->lb_buf),
201 if (unlikely(rc1 < 0))
204 } else if (info->mti_body->valid & OBD_MD_FLXATTRLS) {
205 CDEBUG(D_INODE, "listxattr\n");
207 rc = mo_xattr_list(info->mti_env, next, buf);
209 CDEBUG(D_INFO, "listxattr failed: %d\n", rc);
216 mdt_counter_incr(req->rq_export, LPROC_MDT_GETXATTR);
217 repbody->eadatasize = rc;
220 mdt_exit_ucred(info);
224 static int mdt_rmtlsetfacl(struct mdt_thread_info *info,
225 struct md_object *next,
226 const char *xattr_name,
227 ext_acl_xattr_header *header,
228 posix_acl_xattr_header **out)
230 struct ptlrpc_request *req = mdt_info_req(info);
231 struct mdt_export_data *med = mdt_req2med(req);
232 struct md_ucred *uc = mdt_ucred(info);
233 struct lu_buf *buf = &info->mti_buf;
237 rc = lustre_ext_acl_xattr_id2server(uc, med->med_idmap, header);
241 rc = mo_xattr_get(info->mti_env, next, &LU_BUF_NULL, xattr_name);
248 if (buf->lb_len > 0) {
249 OBD_ALLOC_LARGE(buf->lb_buf, buf->lb_len);
250 if (unlikely(buf->lb_buf == NULL))
253 rc = mo_xattr_get(info->mti_env, next, buf, xattr_name);
255 CERROR("getxattr failed: %d\n", rc);
261 rc = lustre_acl_xattr_merge2posix((posix_acl_xattr_header *)(buf->lb_buf),
262 buf->lb_len, header, out);
266 if (rc <= 0 && buf->lb_buf != NULL)
267 OBD_FREE_LARGE(buf->lb_buf, buf->lb_len);
271 int mdt_reint_setxattr(struct mdt_thread_info *info,
272 struct mdt_lock_handle *unused)
274 struct ptlrpc_request *req = mdt_info_req(info);
275 struct md_ucred *uc = mdt_ucred(info);
276 struct mdt_lock_handle *lh;
277 const struct lu_env *env = info->mti_env;
278 struct lu_buf *buf = &info->mti_buf;
279 struct mdt_reint_record *rr = &info->mti_rr;
280 struct md_attr *ma = &info->mti_attr;
281 struct lu_attr *attr = &info->mti_attr.ma_attr;
282 struct mdt_object *obj;
283 struct md_object *child;
284 __u64 valid = attr->la_valid;
285 const char *xattr_name = rr->rr_name;
286 int xattr_len = rr->rr_eadatalen;
289 posix_acl_xattr_header *new_xattr = NULL;
290 __u32 remote = exp_connect_rmtclient(info->mti_exp);
294 CDEBUG(D_INODE, "setxattr for "DFID"\n", PFID(rr->rr_fid1));
296 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SETXATTR))
297 RETURN(err_serious(-ENOMEM));
299 CDEBUG(D_INODE, "%s xattr %s\n",
300 valid & OBD_MD_FLXATTR ? "set" : "remove", xattr_name);
302 rc = mdt_init_ucred_reint(info);
306 if (valid & OBD_MD_FLRMTRSETFACL) {
307 if (unlikely(!remote))
308 GOTO(out, rc = err_serious(-EINVAL));
310 perm = mdt_identity_get_perm(uc->mu_identity, remote,
312 if (!(perm & CFS_RMTACL_PERM))
313 GOTO(out, rc = err_serious(-EPERM));
316 if (strncmp(xattr_name, XATTR_USER_PREFIX,
317 sizeof(XATTR_USER_PREFIX) - 1) == 0) {
318 if (!(req->rq_export->exp_connect_flags & OBD_CONNECT_XATTR))
319 GOTO(out, rc = -EOPNOTSUPP);
320 if (strcmp(xattr_name, XATTR_NAME_LOV) == 0)
321 GOTO(out, rc = -EACCES);
322 if (strcmp(xattr_name, XATTR_NAME_LMA) == 0)
324 if (strcmp(xattr_name, XATTR_NAME_LINK) == 0)
326 } else if ((valid & OBD_MD_FLXATTR) &&
327 (strncmp(xattr_name, XATTR_NAME_ACL_ACCESS,
328 sizeof(XATTR_NAME_ACL_ACCESS) - 1) == 0 ||
329 strncmp(xattr_name, XATTR_NAME_ACL_DEFAULT,
330 sizeof(XATTR_NAME_ACL_DEFAULT) - 1) == 0)) {
331 /* currently lustre limit acl access size */
332 if (xattr_len > LUSTRE_POSIX_ACL_MAX_SIZE)
336 lockpart = MDS_INODELOCK_UPDATE;
337 /* Revoke all clients' lookup lock, since the access
338 * permissions for this inode is changed when ACL_ACCESS is
339 * set. This isn't needed for ACL_DEFAULT, since that does
340 * not change the access permissions of this inode, nor any
341 * other existing inodes. It is setting the ACLs inherited
342 * by new directories/files at create time. */
343 if (!strcmp(xattr_name, XATTR_NAME_ACL_ACCESS))
344 lockpart |= MDS_INODELOCK_LOOKUP;
346 lh = &info->mti_lh[MDT_LH_PARENT];
347 /* ACLs were sent to clients under LCK_CR locks, so taking LCK_EX
349 mdt_lock_reg_init(lh, LCK_EX);
350 obj = mdt_object_find_lock(info, rr->rr_fid1, lh, lockpart);
352 GOTO(out, rc = PTR_ERR(obj));
355 rc = mdt_version_get_check_save(info, obj, 0);
357 GOTO(out_unlock, rc);
359 if (unlikely(!(valid & OBD_MD_FLCTIME))) {
360 /* This isn't strictly an error, but all current clients
361 * should set OBD_MD_FLCTIME when setting attributes. */
362 CWARN("%s: client miss to set OBD_MD_FLCTIME when "
363 "setxattr %s: [object "DFID"] [valid "LPU64"]\n",
364 info->mti_exp->exp_obd->obd_name, xattr_name,
365 PFID(rr->rr_fid1), valid);
366 attr->la_ctime = cfs_time_current_sec();
368 attr->la_valid = LA_CTIME;
369 child = mdt_object_child(obj);
370 if (valid & OBD_MD_FLXATTR) {
371 char *xattr = (void *)rr->rr_eadata;
376 if (valid & OBD_MD_FLRMTLSETFACL) {
377 if (unlikely(!remote))
378 GOTO(out_unlock, rc = -EINVAL);
380 xattr_len = mdt_rmtlsetfacl(info, child,
382 (ext_acl_xattr_header *)xattr,
385 GOTO(out_unlock, rc = xattr_len);
387 xattr = (char *)new_xattr;
390 if (attr->la_flags & XATTR_REPLACE)
391 flags |= LU_XATTR_REPLACE;
393 if (attr->la_flags & XATTR_CREATE)
394 flags |= LU_XATTR_CREATE;
396 mdt_fail_write(env, info->mti_mdt->mdt_bottom,
397 OBD_FAIL_MDS_SETXATTR_WRITE);
400 buf->lb_len = xattr_len;
401 rc = mo_xattr_set(env, child, buf, xattr_name, flags);
402 /* update ctime after xattr changed */
404 ma->ma_attr_flags |= MDS_PERM_BYPASS;
405 mo_attr_set(env, child, ma);
408 } else if (valid & OBD_MD_FLXATTRRM) {
409 rc = mo_xattr_del(env, child, xattr_name);
410 /* update ctime after xattr changed */
412 ma->ma_attr_flags |= MDS_PERM_BYPASS;
413 mo_attr_set(env, child, ma);
416 CDEBUG(D_INFO, "valid bits: "LPX64"\n", valid);
420 mdt_counter_incr(req->rq_export, LPROC_MDT_SETXATTR);
424 mdt_object_unlock_put(info, obj, lh, rc);
425 if (unlikely(new_xattr != NULL))
426 lustre_posix_acl_xattr_free(new_xattr, xattr_len);
428 mdt_exit_ucred(info);