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, 2017, 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) {
79 size = mo_xattr_list(info->mti_env,
80 mdt_object_child(info->mti_object),
82 } else if (valid == OBD_MD_FLXATTRALL) {
84 /* N.B. eadatasize = 0 is not valid for FLXATTRALL */
85 /* We could calculate accurate sizes, but this would
86 * introduce a lot of overhead, let's do it later... */
87 size = info->mti_body->mbo_eadatasize;
88 req_capsule_set_size(pill, &RMF_EAVALS, RCL_SERVER, size);
89 req_capsule_set_size(pill, &RMF_EAVALS_LENS, RCL_SERVER, size);
91 CDEBUG(D_INFO, "Valid bits: %#llx\n",
92 info->mti_body->mbo_valid);
96 if (size == -ENODATA) {
98 } else if (size < 0) {
99 if (size != -EOPNOTSUPP && size != -ENOENT)
100 CERROR("%s: error geting EA size for '%s': rc = %d\n",
101 mdt_obd_name(info->mti_mdt), xattr_name, size);
105 if (req_capsule_has_field(pill, &RMF_ACL, RCL_SERVER))
106 req_capsule_set_size(pill, &RMF_ACL, RCL_SERVER,
107 LUSTRE_POSIX_ACL_MAX_SIZE_OLD);
109 req_capsule_set_size(pill, &RMF_EADATA, RCL_SERVER,
110 info->mti_body->mbo_eadatasize == 0 ? 0 : size);
111 rc = req_capsule_server_pack(pill);
117 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETXATTR_PACK))
123 static int mdt_nodemap_map_acl(struct mdt_thread_info *info, void *buf,
124 size_t size, const char *name,
125 enum nodemap_tree_type tree_type)
127 struct lu_nodemap *nodemap;
128 struct obd_export *exp = info->mti_exp;
133 if (strcmp(name, XATTR_NAME_ACL_ACCESS) == 0 ||
134 strcmp(name, XATTR_NAME_ACL_DEFAULT) == 0) {
135 if (size > info->mti_mdt->mdt_max_ea_size ||
136 (!exp_connect_large_acl(exp) &&
137 size > LUSTRE_POSIX_ACL_MAX_SIZE_OLD))
138 GOTO(out, rc = -ERANGE);
140 nodemap = nodemap_get_from_exp(exp);
142 GOTO(out, rc = PTR_ERR(nodemap));
144 rc = nodemap_map_acl(nodemap, buf, size, tree_type);
145 nodemap_putref(nodemap);
153 static int mdt_getxattr_all(struct mdt_thread_info *info,
154 struct mdt_body *reqbody, struct mdt_body *repbody,
155 struct lu_buf *buf, struct md_object *next)
157 const struct lu_env *env = info->mti_env;
158 char *v, *b, *eadatahead, *eadatatail;
160 int eadatasize, eavallen = 0, eavallens = 0, rc;
165 * The format of the pill is the following:
166 * EADATA: attr1\0attr2\0...attrn\0
167 * EAVALS: val1val2...valn
168 * EAVALS_LENS: 4,4,...4
171 eadatahead = buf->lb_buf;
173 /* Fill out EADATA first */
174 rc = mo_xattr_list(env, next, buf);
176 GOTO(out_shrink, rc);
179 eadatatail = eadatahead + eadatasize;
181 v = req_capsule_server_get(info->mti_pill, &RMF_EAVALS);
182 sizes = req_capsule_server_get(info->mti_pill, &RMF_EAVALS_LENS);
184 /* Fill out EAVALS and EAVALS_LENS */
185 for (b = eadatahead; b < eadatatail; b += strlen(b) + 1, v += rc) {
187 buf->lb_len = reqbody->mbo_eadatasize - eavallen;
188 rc = mo_xattr_get(env, next, buf, b);
190 GOTO(out_shrink, rc);
191 rc = mdt_nodemap_map_acl(info, buf->lb_buf, rc, b,
192 NODEMAP_FS_TO_CLIENT);
194 GOTO(out_shrink, rc);
195 sizes[eavallens] = rc;
206 repbody->mbo_aclsize = eavallen;
207 repbody->mbo_max_mdsize = eavallens;
209 req_capsule_shrink(info->mti_pill, &RMF_EAVALS, eavallen, RCL_SERVER);
210 req_capsule_shrink(info->mti_pill, &RMF_EAVALS_LENS,
211 eavallens * sizeof(__u32), RCL_SERVER);
212 req_capsule_shrink(info->mti_pill, &RMF_EADATA, eadatasize, RCL_SERVER);
219 int mdt_getxattr(struct mdt_thread_info *info)
221 struct ptlrpc_request *req = mdt_info_req(info);
222 struct mdt_body *reqbody;
223 struct mdt_body *repbody = NULL;
224 struct md_object *next;
230 LASSERT(info->mti_object != NULL);
231 LASSERT(lu_object_assert_exists(&info->mti_object->mot_obj));
233 CDEBUG(D_INODE, "getxattr "DFID"\n", PFID(&info->mti_body->mbo_fid1));
235 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
237 RETURN(err_serious(-EFAULT));
239 rc = mdt_init_ucred(info, reqbody);
241 RETURN(err_serious(rc));
243 next = mdt_object_child(info->mti_object);
244 easize = mdt_getxattr_pack_reply(info);
246 GOTO(out, rc = err_serious(easize));
248 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
249 LASSERT(repbody != NULL);
251 /* No need further getxattr. */
252 if (easize == 0 || reqbody->mbo_eadatasize == 0)
253 GOTO(out, rc = easize);
255 buf = &info->mti_buf;
256 buf->lb_buf = req_capsule_server_get(info->mti_pill, &RMF_EADATA);
257 buf->lb_len = easize;
259 valid = info->mti_body->mbo_valid & (OBD_MD_FLXATTR | OBD_MD_FLXATTRLS);
261 if (valid == OBD_MD_FLXATTR) {
262 const char *xattr_name = req_capsule_client_get(info->mti_pill,
264 rc = mo_xattr_get(info->mti_env, next, buf, xattr_name);
265 rc = mdt_nodemap_map_acl(info, buf->lb_buf, rc, xattr_name,
266 NODEMAP_FS_TO_CLIENT);
267 } else if (valid == OBD_MD_FLXATTRLS) {
268 CDEBUG(D_INODE, "listxattr\n");
270 rc = mo_xattr_list(info->mti_env, next, buf);
272 CDEBUG(D_INFO, "listxattr failed: %d\n", rc);
273 } else if (valid == OBD_MD_FLXATTRALL) {
274 rc = mdt_getxattr_all(info, reqbody, repbody,
282 mdt_counter_incr(req, LPROC_MDT_GETXATTR);
283 repbody->mbo_eadatasize = rc;
286 mdt_exit_ucred(info);
290 int mdt_reint_setxattr(struct mdt_thread_info *info,
291 struct mdt_lock_handle *unused)
293 struct ptlrpc_request *req = mdt_info_req(info);
294 struct mdt_lock_handle *lh;
295 const struct lu_env *env = info->mti_env;
296 struct lu_buf *buf = &info->mti_buf;
297 struct mdt_reint_record *rr = &info->mti_rr;
298 struct md_attr *ma = &info->mti_attr;
299 struct lu_attr *attr = &info->mti_attr.ma_attr;
300 struct mdt_object *obj;
301 struct md_object *child;
302 __u64 valid = attr->la_valid;
303 const char *xattr_name = rr->rr_name.ln_name;
304 int xattr_len = rr->rr_eadatalen;
305 __u64 lockpart = MDS_INODELOCK_UPDATE;
309 CDEBUG(D_INODE, "setxattr for "DFID": %s %s\n", PFID(rr->rr_fid1),
310 valid & OBD_MD_FLXATTR ? "set" : "remove", xattr_name);
312 if (info->mti_dlm_req)
313 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
315 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SETXATTR))
316 RETURN(err_serious(-ENOMEM));
318 rc = mdt_init_ucred_reint(info);
322 if (strncmp(xattr_name, XATTR_USER_PREFIX,
323 sizeof(XATTR_USER_PREFIX) - 1) == 0) {
324 if (!(exp_connect_flags(req->rq_export) & OBD_CONNECT_XATTR))
325 GOTO(out, rc = -EOPNOTSUPP);
326 } else if (strncmp(xattr_name, XATTR_TRUSTED_PREFIX,
327 sizeof(XATTR_TRUSTED_PREFIX) - 1) == 0) {
329 if (!md_capable(mdt_ucred(info), CFS_CAP_SYS_ADMIN))
330 GOTO(out, rc = -EPERM);
332 if (strcmp(xattr_name, XATTR_NAME_LOV) == 0 ||
333 strcmp(xattr_name, XATTR_NAME_LMA) == 0 ||
334 strcmp(xattr_name, XATTR_NAME_LMV) == 0 ||
335 strcmp(xattr_name, XATTR_NAME_LINK) == 0 ||
336 strcmp(xattr_name, XATTR_NAME_FID) == 0 ||
337 strcmp(xattr_name, XATTR_NAME_VERSION) == 0 ||
338 strcmp(xattr_name, XATTR_NAME_SOM) == 0 ||
339 strcmp(xattr_name, XATTR_NAME_HSM) == 0 ||
340 strcmp(xattr_name, XATTR_NAME_LFSCK_NAMESPACE) == 0)
342 } else if ((valid & OBD_MD_FLXATTR) &&
343 (strcmp(xattr_name, XATTR_NAME_ACL_ACCESS) == 0 ||
344 strcmp(xattr_name, XATTR_NAME_ACL_DEFAULT) == 0)) {
345 rc = mdt_nodemap_map_acl(info, rr->rr_eadata, xattr_len,
346 xattr_name, NODEMAP_CLIENT_TO_FS);
349 /* ACLs were mapped out, return an error so the user knows */
351 GOTO(out, rc = -EPERM);
352 } else if ((strlen(xattr_name) > strlen(XATTR_LUSTRE_LOV) + 1) &&
353 strncmp(xattr_name, XATTR_LUSTRE_LOV,
354 strlen(XATTR_LUSTRE_LOV)) == 0) {
356 if (strncmp(xattr_name, XATTR_LUSTRE_LOV".add",
357 strlen(XATTR_LUSTRE_LOV".add")) &&
358 strncmp(xattr_name, XATTR_LUSTRE_LOV".set",
359 strlen(XATTR_LUSTRE_LOV".set")) &&
360 strncmp(xattr_name, XATTR_LUSTRE_LOV".del",
361 strlen(XATTR_LUSTRE_LOV".del"))) {
362 CERROR("%s: invalid xattr name: %s\n",
363 mdt_obd_name(info->mti_mdt), xattr_name);
364 GOTO(out, rc = -EINVAL);
367 lockpart |= MDS_INODELOCK_LAYOUT;
370 /* Revoke all clients' lookup lock, since the access
371 * permissions for this inode is changed when ACL_ACCESS is
372 * set. This isn't needed for ACL_DEFAULT, since that does
373 * not change the access permissions of this inode, nor any
374 * other existing inodes. It is setting the ACLs inherited
375 * by new directories/files at create time. */
376 /* We need revoke both LOOKUP|PERM lock here, see mdt_attr_set. */
377 if (!strcmp(xattr_name, XATTR_NAME_ACL_ACCESS))
378 lockpart |= MDS_INODELOCK_PERM | MDS_INODELOCK_LOOKUP;
379 /* We need to take the lock on behalf of old clients so that newer
380 * clients flush their xattr caches */
382 lockpart |= MDS_INODELOCK_XATTR;
384 lh = &info->mti_lh[MDT_LH_PARENT];
385 /* ACLs were sent to clients under LCK_CR locks, so taking LCK_EX
387 mdt_lock_reg_init(lh, LCK_EX);
388 obj = mdt_object_find_lock(info, rr->rr_fid1, lh, lockpart);
390 GOTO(out, rc = PTR_ERR(obj));
392 tgt_vbr_obj_set(env, mdt_obj2dt(obj));
393 rc = mdt_version_get_check_save(info, obj, 0);
395 GOTO(out_unlock, rc);
397 if (unlikely(!(valid & OBD_MD_FLCTIME))) {
398 /* This isn't strictly an error, but all current clients
399 * should set OBD_MD_FLCTIME when setting attributes. */
400 CWARN("%s: client miss to set OBD_MD_FLCTIME when "
401 "setxattr %s: [object "DFID"] [valid %llu]\n",
402 mdt_obd_name(info->mti_mdt), xattr_name,
403 PFID(rr->rr_fid1), valid);
404 attr->la_ctime = ktime_get_real_seconds();
406 attr->la_valid = LA_CTIME;
407 child = mdt_object_child(obj);
408 if (valid & OBD_MD_FLXATTR) {
411 if (attr->la_flags & XATTR_REPLACE)
412 flags |= LU_XATTR_REPLACE;
414 if (attr->la_flags & XATTR_CREATE)
415 flags |= LU_XATTR_CREATE;
417 mdt_fail_write(env, info->mti_mdt->mdt_bottom,
418 OBD_FAIL_MDS_SETXATTR_WRITE);
420 buf->lb_buf = rr->rr_eadata;
421 buf->lb_len = xattr_len;
422 rc = mo_xattr_set(env, child, buf, xattr_name, flags);
423 /* update ctime after xattr changed */
425 ma->ma_attr_flags |= MDS_PERM_BYPASS;
426 mo_attr_set(env, child, ma);
428 } else if (valid & OBD_MD_FLXATTRRM) {
429 rc = mo_xattr_del(env, child, xattr_name);
430 /* update ctime after xattr changed */
432 ma->ma_attr_flags |= MDS_PERM_BYPASS;
433 mo_attr_set(env, child, ma);
436 CDEBUG(D_INFO, "valid bits: %#llx\n", valid);
441 mdt_counter_incr(req, LPROC_MDT_SETXATTR);
445 mdt_object_unlock_put(info, obj, lh, rc);
447 mdt_exit_ucred(info);