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 req_capsule_set_size(pill, &RMF_EADATA, RCL_SERVER,
103 info->mti_body->mbo_eadatasize == 0 ? 0 : size);
104 rc = req_capsule_server_pack(pill);
110 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETXATTR_PACK))
116 static int mdt_getxattr_all(struct mdt_thread_info *info,
117 struct mdt_body *reqbody, struct mdt_body *repbody,
118 struct lu_buf *buf, struct md_object *next)
120 const struct lu_env *env = info->mti_env;
121 char *v, *b, *eadatahead, *eadatatail;
123 int eadatasize, eavallen = 0, eavallens = 0, rc;
128 * The format of the pill is the following:
129 * EADATA: attr1\0attr2\0...attrn\0
130 * EAVALS: val1val2...valn
131 * EAVALS_LENS: 4,4,...4
134 eadatahead = buf->lb_buf;
136 /* Fill out EADATA first */
137 rc = mo_xattr_list(env, next, buf);
139 GOTO(out_shrink, rc);
142 eadatatail = eadatahead + eadatasize;
144 v = req_capsule_server_get(info->mti_pill, &RMF_EAVALS);
145 sizes = req_capsule_server_get(info->mti_pill, &RMF_EAVALS_LENS);
147 /* Fill out EAVALS and EAVALS_LENS */
148 for (b = eadatahead; b < eadatatail; b += strlen(b) + 1, v += rc) {
150 buf->lb_len = reqbody->mbo_eadatasize - eavallen;
151 rc = mo_xattr_get(env, next, buf, b);
153 GOTO(out_shrink, rc);
155 sizes[eavallens] = rc;
166 repbody->mbo_aclsize = eavallen;
167 repbody->mbo_max_mdsize = eavallens;
169 req_capsule_shrink(info->mti_pill, &RMF_EAVALS, eavallen, RCL_SERVER);
170 req_capsule_shrink(info->mti_pill, &RMF_EAVALS_LENS,
171 eavallens * sizeof(__u32), RCL_SERVER);
172 req_capsule_shrink(info->mti_pill, &RMF_EADATA, eadatasize, RCL_SERVER);
179 int mdt_getxattr(struct mdt_thread_info *info)
181 struct ptlrpc_request *req = mdt_info_req(info);
182 struct mdt_body *reqbody;
183 struct mdt_body *repbody = NULL;
184 struct md_object *next;
190 LASSERT(info->mti_object != NULL);
191 LASSERT(lu_object_assert_exists(&info->mti_object->mot_obj));
193 CDEBUG(D_INODE, "getxattr "DFID"\n", PFID(&info->mti_body->mbo_fid1));
195 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
197 RETURN(err_serious(-EFAULT));
199 rc = mdt_init_ucred(info, reqbody);
201 RETURN(err_serious(rc));
203 next = mdt_object_child(info->mti_object);
204 easize = mdt_getxattr_pack_reply(info);
206 GOTO(out, rc = err_serious(easize));
208 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
209 LASSERT(repbody != NULL);
211 /* No need further getxattr. */
212 if (easize == 0 || reqbody->mbo_eadatasize == 0)
213 GOTO(out, rc = easize);
215 buf = &info->mti_buf;
216 buf->lb_buf = req_capsule_server_get(info->mti_pill, &RMF_EADATA);
217 buf->lb_len = easize;
219 valid = info->mti_body->mbo_valid & (OBD_MD_FLXATTR | OBD_MD_FLXATTRLS);
221 if (valid == OBD_MD_FLXATTR) {
222 char *xattr_name = req_capsule_client_get(info->mti_pill,
224 rc = mo_xattr_get(info->mti_env, next, buf, xattr_name);
225 } else if (valid == OBD_MD_FLXATTRLS) {
226 CDEBUG(D_INODE, "listxattr\n");
228 rc = mo_xattr_list(info->mti_env, next, buf);
230 CDEBUG(D_INFO, "listxattr failed: %d\n", rc);
231 } else if (valid == OBD_MD_FLXATTRALL) {
232 rc = mdt_getxattr_all(info, reqbody, repbody,
240 mdt_counter_incr(req, LPROC_MDT_GETXATTR);
241 repbody->mbo_eadatasize = rc;
244 mdt_exit_ucred(info);
248 int mdt_reint_setxattr(struct mdt_thread_info *info,
249 struct mdt_lock_handle *unused)
251 struct ptlrpc_request *req = mdt_info_req(info);
252 struct mdt_lock_handle *lh;
253 const struct lu_env *env = info->mti_env;
254 struct lu_buf *buf = &info->mti_buf;
255 struct mdt_reint_record *rr = &info->mti_rr;
256 struct md_attr *ma = &info->mti_attr;
257 struct lu_attr *attr = &info->mti_attr.ma_attr;
258 struct mdt_object *obj;
259 struct md_object *child;
260 struct obd_export *exp = info->mti_exp;
261 __u64 valid = attr->la_valid;
262 const char *xattr_name = rr->rr_name.ln_name;
263 int xattr_len = rr->rr_eadatalen;
264 __u64 lockpart = MDS_INODELOCK_UPDATE;
268 CDEBUG(D_INODE, "setxattr for "DFID": %s %s\n", PFID(rr->rr_fid1),
269 valid & OBD_MD_FLXATTR ? "set" : "remove", xattr_name);
271 if (info->mti_dlm_req)
272 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
274 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SETXATTR))
275 RETURN(err_serious(-ENOMEM));
277 rc = mdt_init_ucred_reint(info);
281 if (strncmp(xattr_name, XATTR_USER_PREFIX,
282 sizeof(XATTR_USER_PREFIX) - 1) == 0) {
283 if (!(exp_connect_flags(req->rq_export) & OBD_CONNECT_XATTR))
284 GOTO(out, rc = -EOPNOTSUPP);
285 } else if (strncmp(xattr_name, XATTR_TRUSTED_PREFIX,
286 sizeof(XATTR_TRUSTED_PREFIX) - 1) == 0) {
288 if (!md_capable(mdt_ucred(info), CFS_CAP_SYS_ADMIN))
289 GOTO(out, rc = -EPERM);
291 if (strcmp(xattr_name, XATTR_NAME_LOV) == 0 ||
292 strcmp(xattr_name, XATTR_NAME_LMA) == 0 ||
293 strcmp(xattr_name, XATTR_NAME_LMV) == 0 ||
294 strcmp(xattr_name, XATTR_NAME_LINK) == 0 ||
295 strcmp(xattr_name, XATTR_NAME_FID) == 0 ||
296 strcmp(xattr_name, XATTR_NAME_VERSION) == 0 ||
297 strcmp(xattr_name, XATTR_NAME_SOM) == 0 ||
298 strcmp(xattr_name, XATTR_NAME_HSM) == 0 ||
299 strcmp(xattr_name, XATTR_NAME_LFSCK_NAMESPACE) == 0)
301 } else if ((valid & OBD_MD_FLXATTR) &&
302 (strcmp(xattr_name, XATTR_NAME_ACL_ACCESS) == 0 ||
303 strcmp(xattr_name, XATTR_NAME_ACL_DEFAULT) == 0)) {
304 struct lu_nodemap *nodemap;
306 /* currently lustre limit acl access size */
307 if (xattr_len > LUSTRE_POSIX_ACL_MAX_SIZE)
308 GOTO(out, rc = -ERANGE);
310 nodemap = nodemap_get_from_exp(exp);
312 GOTO(out, rc = PTR_ERR(nodemap));
314 rc = nodemap_map_acl(nodemap, rr->rr_eadata, xattr_len,
315 NODEMAP_CLIENT_TO_FS);
316 nodemap_putref(nodemap);
320 /* ACLs were mapped out, return an error so the user knows */
322 GOTO(out, rc = -EPERM);
323 } else if ((strlen(xattr_name) > strlen(XATTR_LUSTRE_LOV) + 1) &&
324 strncmp(xattr_name, XATTR_LUSTRE_LOV,
325 strlen(XATTR_LUSTRE_LOV)) == 0) {
327 if (strncmp(xattr_name, XATTR_LUSTRE_LOV".add",
328 strlen(XATTR_LUSTRE_LOV".add")) &&
329 strncmp(xattr_name, XATTR_LUSTRE_LOV".set",
330 strlen(XATTR_LUSTRE_LOV".set")) &&
331 strncmp(xattr_name, XATTR_LUSTRE_LOV".del",
332 strlen(XATTR_LUSTRE_LOV".del"))) {
333 CERROR("%s: invalid xattr name: %s\n",
334 mdt_obd_name(info->mti_mdt), xattr_name);
335 GOTO(out, rc = -EINVAL);
338 lockpart |= MDS_INODELOCK_LAYOUT;
341 /* Revoke all clients' lookup lock, since the access
342 * permissions for this inode is changed when ACL_ACCESS is
343 * set. This isn't needed for ACL_DEFAULT, since that does
344 * not change the access permissions of this inode, nor any
345 * other existing inodes. It is setting the ACLs inherited
346 * by new directories/files at create time. */
347 /* We need revoke both LOOKUP|PERM lock here, see mdt_attr_set. */
348 if (!strcmp(xattr_name, XATTR_NAME_ACL_ACCESS))
349 lockpart |= MDS_INODELOCK_PERM | MDS_INODELOCK_LOOKUP;
350 /* We need to take the lock on behalf of old clients so that newer
351 * clients flush their xattr caches */
353 lockpart |= MDS_INODELOCK_XATTR;
355 lh = &info->mti_lh[MDT_LH_PARENT];
356 /* ACLs were sent to clients under LCK_CR locks, so taking LCK_EX
358 mdt_lock_reg_init(lh, LCK_EX);
359 obj = mdt_object_find_lock(info, rr->rr_fid1, lh, lockpart);
361 GOTO(out, rc = PTR_ERR(obj));
363 tgt_vbr_obj_set(env, mdt_obj2dt(obj));
364 rc = mdt_version_get_check_save(info, obj, 0);
366 GOTO(out_unlock, rc);
368 if (unlikely(!(valid & OBD_MD_FLCTIME))) {
369 /* This isn't strictly an error, but all current clients
370 * should set OBD_MD_FLCTIME when setting attributes. */
371 CWARN("%s: client miss to set OBD_MD_FLCTIME when "
372 "setxattr %s: [object "DFID"] [valid %llu]\n",
373 mdt_obd_name(info->mti_mdt), xattr_name,
374 PFID(rr->rr_fid1), valid);
375 attr->la_ctime = cfs_time_current_sec();
377 attr->la_valid = LA_CTIME;
378 child = mdt_object_child(obj);
379 if (valid & OBD_MD_FLXATTR) {
382 if (attr->la_flags & XATTR_REPLACE)
383 flags |= LU_XATTR_REPLACE;
385 if (attr->la_flags & XATTR_CREATE)
386 flags |= LU_XATTR_CREATE;
388 mdt_fail_write(env, info->mti_mdt->mdt_bottom,
389 OBD_FAIL_MDS_SETXATTR_WRITE);
391 buf->lb_buf = rr->rr_eadata;
392 buf->lb_len = xattr_len;
393 rc = mo_xattr_set(env, child, buf, xattr_name, flags);
394 /* update ctime after xattr changed */
396 ma->ma_attr_flags |= MDS_PERM_BYPASS;
397 mo_attr_set(env, child, ma);
399 } else if (valid & OBD_MD_FLXATTRRM) {
400 rc = mo_xattr_del(env, child, xattr_name);
401 /* update ctime after xattr changed */
403 ma->ma_attr_flags |= MDS_PERM_BYPASS;
404 mo_attr_set(env, child, ma);
407 CDEBUG(D_INFO, "valid bits: %#llx\n", valid);
412 mdt_counter_incr(req, LPROC_MDT_SETXATTR);
416 mdt_object_unlock_put(info, obj, lh, rc);
418 mdt_exit_ucred(info);