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.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2016, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/mdt/mdt_xattr.c
34 * Lustre Metadata Target (mdt) extended attributes management.
36 * Author: Peter Braam <braam@clusterfs.com>
37 * Author: Andreas Dilger <adilger@clusterfs.com>
38 * Author: Phil Schwan <phil@clusterfs.com>
39 * Author: Huang Hua <huanghua@clusterfs.com>
42 #define DEBUG_SUBSYSTEM S_MDS
44 #include <linux/xattr.h>
45 #include <obd_class.h>
46 #include <lustre_nodemap.h>
47 #include <lustre_acl.h>
48 #include "mdt_internal.h"
51 /* return EADATA length to the caller. negative value means error */
52 static int mdt_getxattr_pack_reply(struct mdt_thread_info * info)
54 struct req_capsule *pill = info->mti_pill ;
55 struct ptlrpc_request *req = mdt_info_req(info);
58 static const char user_string[] = "user.";
62 valid = info->mti_body->mbo_valid & (OBD_MD_FLXATTR | OBD_MD_FLXATTRLS);
64 /* Determine how many bytes we need */
65 if (valid == OBD_MD_FLXATTR) {
66 xattr_name = req_capsule_client_get(pill, &RMF_NAME);
70 if (!(exp_connect_flags(req->rq_export) & OBD_CONNECT_XATTR) &&
71 !strncmp(xattr_name, user_string, sizeof(user_string) - 1))
74 size = mo_xattr_get(info->mti_env,
75 mdt_object_child(info->mti_object),
76 &LU_BUF_NULL, xattr_name);
77 } else if (valid == OBD_MD_FLXATTRLS) {
78 size = mo_xattr_list(info->mti_env,
79 mdt_object_child(info->mti_object),
81 } else if (valid == OBD_MD_FLXATTRALL) {
82 /* N.B. eadatasize = 0 is not valid for FLXATTRALL */
83 /* We could calculate accurate sizes, but this would
84 * introduce a lot of overhead, let's do it later... */
85 size = info->mti_body->mbo_eadatasize;
86 req_capsule_set_size(pill, &RMF_EAVALS, RCL_SERVER, size);
87 req_capsule_set_size(pill, &RMF_EAVALS_LENS, RCL_SERVER, size);
89 CDEBUG(D_INFO, "Valid bits: %#llx\n",
90 info->mti_body->mbo_valid);
94 if (size == -ENODATA) {
96 } else if (size < 0) {
97 if (size != -EOPNOTSUPP)
98 CERROR("Error geting EA size: %d\n", size);
102 if (req_capsule_has_field(pill, &RMF_ACL, RCL_SERVER))
103 req_capsule_set_size(pill, &RMF_ACL, RCL_SERVER,
104 LUSTRE_POSIX_ACL_MAX_SIZE_OLD);
106 req_capsule_set_size(pill, &RMF_EADATA, RCL_SERVER,
107 info->mti_body->mbo_eadatasize == 0 ? 0 : size);
108 rc = req_capsule_server_pack(pill);
114 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETXATTR_PACK))
120 static int mdt_nodemap_map_acl(struct mdt_thread_info *info, void *buf,
121 size_t size, const char *name,
122 enum nodemap_tree_type tree_type)
124 struct lu_nodemap *nodemap;
125 struct obd_export *exp = info->mti_exp;
130 if (strcmp(name, XATTR_NAME_ACL_ACCESS) == 0 ||
131 strcmp(name, XATTR_NAME_ACL_DEFAULT) == 0) {
132 if (size > info->mti_mdt->mdt_max_ea_size ||
133 (!exp_connect_large_acl(exp) &&
134 size > LUSTRE_POSIX_ACL_MAX_SIZE_OLD))
135 GOTO(out, rc = -ERANGE);
137 nodemap = nodemap_get_from_exp(exp);
139 GOTO(out, rc = PTR_ERR(nodemap));
141 rc = nodemap_map_acl(nodemap, buf, size, tree_type);
142 nodemap_putref(nodemap);
150 static int mdt_getxattr_all(struct mdt_thread_info *info,
151 struct mdt_body *reqbody, struct mdt_body *repbody,
152 struct lu_buf *buf, struct md_object *next)
154 const struct lu_env *env = info->mti_env;
155 char *v, *b, *eadatahead, *eadatatail;
157 int eadatasize, eavallen = 0, eavallens = 0, rc;
162 * The format of the pill is the following:
163 * EADATA: attr1\0attr2\0...attrn\0
164 * EAVALS: val1val2...valn
165 * EAVALS_LENS: 4,4,...4
168 eadatahead = buf->lb_buf;
170 /* Fill out EADATA first */
171 rc = mo_xattr_list(env, next, buf);
173 GOTO(out_shrink, rc);
176 eadatatail = eadatahead + eadatasize;
178 v = req_capsule_server_get(info->mti_pill, &RMF_EAVALS);
179 sizes = req_capsule_server_get(info->mti_pill, &RMF_EAVALS_LENS);
181 /* Fill out EAVALS and EAVALS_LENS */
182 for (b = eadatahead; b < eadatatail; b += strlen(b) + 1, v += rc) {
184 buf->lb_len = reqbody->mbo_eadatasize - eavallen;
185 rc = mo_xattr_get(env, next, buf, b);
187 GOTO(out_shrink, rc);
188 rc = mdt_nodemap_map_acl(info, buf->lb_buf, rc, b,
189 NODEMAP_FS_TO_CLIENT);
191 GOTO(out_shrink, rc);
192 sizes[eavallens] = rc;
203 repbody->mbo_aclsize = eavallen;
204 repbody->mbo_max_mdsize = eavallens;
206 req_capsule_shrink(info->mti_pill, &RMF_EAVALS, eavallen, RCL_SERVER);
207 req_capsule_shrink(info->mti_pill, &RMF_EAVALS_LENS,
208 eavallens * sizeof(__u32), RCL_SERVER);
209 req_capsule_shrink(info->mti_pill, &RMF_EADATA, eadatasize, RCL_SERVER);
216 int mdt_getxattr(struct mdt_thread_info *info)
218 struct ptlrpc_request *req = mdt_info_req(info);
219 struct mdt_body *reqbody;
220 struct mdt_body *repbody = NULL;
221 struct md_object *next;
227 LASSERT(info->mti_object != NULL);
228 LASSERT(lu_object_assert_exists(&info->mti_object->mot_obj));
230 CDEBUG(D_INODE, "getxattr "DFID"\n", PFID(&info->mti_body->mbo_fid1));
232 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
234 RETURN(err_serious(-EFAULT));
236 rc = mdt_init_ucred(info, reqbody);
238 RETURN(err_serious(rc));
240 next = mdt_object_child(info->mti_object);
241 easize = mdt_getxattr_pack_reply(info);
243 GOTO(out, rc = err_serious(easize));
245 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
246 LASSERT(repbody != NULL);
248 /* No need further getxattr. */
249 if (easize == 0 || reqbody->mbo_eadatasize == 0)
250 GOTO(out, rc = easize);
252 buf = &info->mti_buf;
253 buf->lb_buf = req_capsule_server_get(info->mti_pill, &RMF_EADATA);
254 buf->lb_len = easize;
256 valid = info->mti_body->mbo_valid & (OBD_MD_FLXATTR | OBD_MD_FLXATTRLS);
258 if (valid == OBD_MD_FLXATTR) {
259 const char *xattr_name = req_capsule_client_get(info->mti_pill,
261 rc = mo_xattr_get(info->mti_env, next, buf, xattr_name);
262 rc = mdt_nodemap_map_acl(info, buf->lb_buf, rc, xattr_name,
263 NODEMAP_FS_TO_CLIENT);
264 } else if (valid == OBD_MD_FLXATTRLS) {
265 CDEBUG(D_INODE, "listxattr\n");
267 rc = mo_xattr_list(info->mti_env, next, buf);
269 CDEBUG(D_INFO, "listxattr failed: %d\n", rc);
270 } else if (valid == OBD_MD_FLXATTRALL) {
271 rc = mdt_getxattr_all(info, reqbody, repbody,
279 mdt_counter_incr(req, LPROC_MDT_GETXATTR);
280 repbody->mbo_eadatasize = rc;
283 mdt_exit_ucred(info);
287 int mdt_reint_setxattr(struct mdt_thread_info *info,
288 struct mdt_lock_handle *unused)
290 struct ptlrpc_request *req = mdt_info_req(info);
291 struct mdt_lock_handle *lh;
292 const struct lu_env *env = info->mti_env;
293 struct lu_buf *buf = &info->mti_buf;
294 struct mdt_reint_record *rr = &info->mti_rr;
295 struct md_attr *ma = &info->mti_attr;
296 struct lu_attr *attr = &info->mti_attr.ma_attr;
297 struct mdt_object *obj;
298 struct md_object *child;
299 __u64 valid = attr->la_valid;
300 const char *xattr_name = rr->rr_name.ln_name;
301 int xattr_len = rr->rr_eadatalen;
302 __u64 lockpart = MDS_INODELOCK_UPDATE;
306 CDEBUG(D_INODE, "setxattr for "DFID": %s %s\n", PFID(rr->rr_fid1),
307 valid & OBD_MD_FLXATTR ? "set" : "remove", xattr_name);
309 if (info->mti_dlm_req)
310 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
312 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SETXATTR))
313 RETURN(err_serious(-ENOMEM));
315 rc = mdt_init_ucred_reint(info);
319 if (strncmp(xattr_name, XATTR_USER_PREFIX,
320 sizeof(XATTR_USER_PREFIX) - 1) == 0) {
321 if (!(exp_connect_flags(req->rq_export) & OBD_CONNECT_XATTR))
322 GOTO(out, rc = -EOPNOTSUPP);
323 } else if (strncmp(xattr_name, XATTR_TRUSTED_PREFIX,
324 sizeof(XATTR_TRUSTED_PREFIX) - 1) == 0) {
326 if (!md_capable(mdt_ucred(info), CFS_CAP_SYS_ADMIN))
327 GOTO(out, rc = -EPERM);
329 if (strcmp(xattr_name, XATTR_NAME_LOV) == 0 ||
330 strcmp(xattr_name, XATTR_NAME_LMA) == 0 ||
331 strcmp(xattr_name, XATTR_NAME_LMV) == 0 ||
332 strcmp(xattr_name, XATTR_NAME_LINK) == 0 ||
333 strcmp(xattr_name, XATTR_NAME_FID) == 0 ||
334 strcmp(xattr_name, XATTR_NAME_VERSION) == 0 ||
335 strcmp(xattr_name, XATTR_NAME_SOM) == 0 ||
336 strcmp(xattr_name, XATTR_NAME_HSM) == 0 ||
337 strcmp(xattr_name, XATTR_NAME_LFSCK_NAMESPACE) == 0)
339 } else if ((valid & OBD_MD_FLXATTR) &&
340 (strcmp(xattr_name, XATTR_NAME_ACL_ACCESS) == 0 ||
341 strcmp(xattr_name, XATTR_NAME_ACL_DEFAULT) == 0)) {
342 rc = mdt_nodemap_map_acl(info, rr->rr_eadata, xattr_len,
343 xattr_name, NODEMAP_CLIENT_TO_FS);
346 /* ACLs were mapped out, return an error so the user knows */
348 GOTO(out, rc = -EPERM);
349 } else if ((strlen(xattr_name) > strlen(XATTR_LUSTRE_LOV) + 1) &&
350 strncmp(xattr_name, XATTR_LUSTRE_LOV,
351 strlen(XATTR_LUSTRE_LOV)) == 0) {
353 if (strncmp(xattr_name, XATTR_LUSTRE_LOV".add",
354 strlen(XATTR_LUSTRE_LOV".add")) &&
355 strncmp(xattr_name, XATTR_LUSTRE_LOV".set",
356 strlen(XATTR_LUSTRE_LOV".set")) &&
357 strncmp(xattr_name, XATTR_LUSTRE_LOV".del",
358 strlen(XATTR_LUSTRE_LOV".del"))) {
359 CERROR("%s: invalid xattr name: %s\n",
360 mdt_obd_name(info->mti_mdt), xattr_name);
361 GOTO(out, rc = -EINVAL);
364 lockpart |= MDS_INODELOCK_LAYOUT;
367 /* Revoke all clients' lookup lock, since the access
368 * permissions for this inode is changed when ACL_ACCESS is
369 * set. This isn't needed for ACL_DEFAULT, since that does
370 * not change the access permissions of this inode, nor any
371 * other existing inodes. It is setting the ACLs inherited
372 * by new directories/files at create time. */
373 /* We need revoke both LOOKUP|PERM lock here, see mdt_attr_set. */
374 if (!strcmp(xattr_name, XATTR_NAME_ACL_ACCESS))
375 lockpart |= MDS_INODELOCK_PERM | MDS_INODELOCK_LOOKUP;
376 /* We need to take the lock on behalf of old clients so that newer
377 * clients flush their xattr caches */
379 lockpart |= MDS_INODELOCK_XATTR;
381 lh = &info->mti_lh[MDT_LH_PARENT];
382 /* ACLs were sent to clients under LCK_CR locks, so taking LCK_EX
384 mdt_lock_reg_init(lh, LCK_EX);
385 obj = mdt_object_find_lock(info, rr->rr_fid1, lh, lockpart);
387 GOTO(out, rc = PTR_ERR(obj));
389 tgt_vbr_obj_set(env, mdt_obj2dt(obj));
390 rc = mdt_version_get_check_save(info, obj, 0);
392 GOTO(out_unlock, rc);
394 if (unlikely(!(valid & OBD_MD_FLCTIME))) {
395 /* This isn't strictly an error, but all current clients
396 * should set OBD_MD_FLCTIME when setting attributes. */
397 CWARN("%s: client miss to set OBD_MD_FLCTIME when "
398 "setxattr %s: [object "DFID"] [valid %llu]\n",
399 mdt_obd_name(info->mti_mdt), xattr_name,
400 PFID(rr->rr_fid1), valid);
401 attr->la_ctime = cfs_time_current_sec();
403 attr->la_valid = LA_CTIME;
404 child = mdt_object_child(obj);
405 if (valid & OBD_MD_FLXATTR) {
408 if (attr->la_flags & XATTR_REPLACE)
409 flags |= LU_XATTR_REPLACE;
411 if (attr->la_flags & XATTR_CREATE)
412 flags |= LU_XATTR_CREATE;
414 mdt_fail_write(env, info->mti_mdt->mdt_bottom,
415 OBD_FAIL_MDS_SETXATTR_WRITE);
417 buf->lb_buf = rr->rr_eadata;
418 buf->lb_len = xattr_len;
419 rc = mo_xattr_set(env, child, buf, xattr_name, flags);
420 /* update ctime after xattr changed */
422 ma->ma_attr_flags |= MDS_PERM_BYPASS;
423 mo_attr_set(env, child, ma);
425 } else if (valid & OBD_MD_FLXATTRRM) {
426 rc = mo_xattr_del(env, child, xattr_name);
427 /* update ctime after xattr changed */
429 ma->ma_attr_flags |= MDS_PERM_BYPASS;
430 mo_attr_set(env, child, ma);
433 CDEBUG(D_INFO, "valid bits: %#llx\n", valid);
438 mdt_counter_incr(req, LPROC_MDT_SETXATTR);
442 mdt_object_unlock_put(info, obj, lh, rc);
444 mdt_exit_ucred(info);