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, 2015, 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 <linux/xattr.h>
49 #include <obd_class.h>
50 #include <lustre_nodemap.h>
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);
62 static const char user_string[] = "user.";
66 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETXATTR_PACK))
69 valid = info->mti_body->mbo_valid & (OBD_MD_FLXATTR | OBD_MD_FLXATTRLS);
71 /* Determine how many bytes we need */
72 if (valid == OBD_MD_FLXATTR) {
73 xattr_name = req_capsule_client_get(pill, &RMF_NAME);
77 if (!(exp_connect_flags(req->rq_export) & OBD_CONNECT_XATTR) &&
78 !strncmp(xattr_name, user_string, sizeof(user_string) - 1))
81 size = mo_xattr_get(info->mti_env,
82 mdt_object_child(info->mti_object),
83 &LU_BUF_NULL, xattr_name);
84 } else if (valid == OBD_MD_FLXATTRLS) {
85 size = mo_xattr_list(info->mti_env,
86 mdt_object_child(info->mti_object),
88 } else if (valid == OBD_MD_FLXATTRALL) {
89 /* N.B. eadatasize = 0 is not valid for FLXATTRALL */
90 /* We could calculate accurate sizes, but this would
91 * introduce a lot of overhead, let's do it later... */
92 size = info->mti_body->mbo_eadatasize;
93 req_capsule_set_size(pill, &RMF_EAVALS, RCL_SERVER, size);
94 req_capsule_set_size(pill, &RMF_EAVALS_LENS, RCL_SERVER, size);
96 CDEBUG(D_INFO, "Valid bits: "LPX64"\n",
97 info->mti_body->mbo_valid);
101 if (size == -ENODATA) {
103 } else if (size < 0) {
104 if (size != -EOPNOTSUPP)
105 CERROR("Error geting EA size: %d\n", size);
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);
120 static int mdt_getxattr_all(struct mdt_thread_info *info,
121 struct mdt_body *reqbody, struct mdt_body *repbody,
122 struct lu_buf *buf, struct md_object *next)
124 const struct lu_env *env = info->mti_env;
125 char *v, *b, *eadatahead, *eadatatail;
127 int eadatasize, eavallen = 0, eavallens = 0, rc;
132 * The format of the pill is the following:
133 * EADATA: attr1\0attr2\0...attrn\0
134 * EAVALS: val1val2...valn
135 * EAVALS_LENS: 4,4,...4
138 eadatahead = buf->lb_buf;
140 /* Fill out EADATA first */
141 rc = mo_xattr_list(env, next, buf);
143 GOTO(out_shrink, rc);
146 eadatatail = eadatahead + eadatasize;
148 v = req_capsule_server_get(info->mti_pill, &RMF_EAVALS);
149 sizes = req_capsule_server_get(info->mti_pill, &RMF_EAVALS_LENS);
151 /* Fill out EAVALS and EAVALS_LENS */
152 for (b = eadatahead; b < eadatatail; b += strlen(b) + 1, v += rc) {
154 buf->lb_len = reqbody->mbo_eadatasize - eavallen;
155 rc = mo_xattr_get(env, next, buf, b);
157 GOTO(out_shrink, rc);
159 sizes[eavallens] = rc;
170 repbody->mbo_aclsize = eavallen;
171 repbody->mbo_max_mdsize = eavallens;
173 req_capsule_shrink(info->mti_pill, &RMF_EAVALS, eavallen, RCL_SERVER);
174 req_capsule_shrink(info->mti_pill, &RMF_EAVALS_LENS,
175 eavallens * sizeof(__u32), RCL_SERVER);
176 req_capsule_shrink(info->mti_pill, &RMF_EADATA, eadatasize, RCL_SERVER);
183 int mdt_getxattr(struct mdt_thread_info *info)
185 struct ptlrpc_request *req = mdt_info_req(info);
186 struct mdt_body *reqbody;
187 struct mdt_body *repbody = NULL;
188 struct md_object *next;
194 LASSERT(info->mti_object != NULL);
195 LASSERT(lu_object_assert_exists(&info->mti_object->mot_obj));
197 CDEBUG(D_INODE, "getxattr "DFID"\n", PFID(&info->mti_body->mbo_fid1));
199 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
201 RETURN(err_serious(-EFAULT));
203 rc = mdt_init_ucred(info, reqbody);
205 RETURN(err_serious(rc));
207 next = mdt_object_child(info->mti_object);
208 easize = mdt_getxattr_pack_reply(info);
210 GOTO(out, rc = err_serious(easize));
212 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
213 LASSERT(repbody != NULL);
215 /* No need further getxattr. */
216 if (easize == 0 || reqbody->mbo_eadatasize == 0)
217 GOTO(out, rc = easize);
219 buf = &info->mti_buf;
220 buf->lb_buf = req_capsule_server_get(info->mti_pill, &RMF_EADATA);
221 buf->lb_len = easize;
223 valid = info->mti_body->mbo_valid & (OBD_MD_FLXATTR | OBD_MD_FLXATTRLS);
225 if (valid == OBD_MD_FLXATTR) {
226 char *xattr_name = req_capsule_client_get(info->mti_pill,
228 rc = mo_xattr_get(info->mti_env, next, buf, xattr_name);
229 } else if (valid == OBD_MD_FLXATTRLS) {
230 CDEBUG(D_INODE, "listxattr\n");
232 rc = mo_xattr_list(info->mti_env, next, buf);
234 CDEBUG(D_INFO, "listxattr failed: %d\n", rc);
235 } else if (valid == OBD_MD_FLXATTRALL) {
236 rc = mdt_getxattr_all(info, reqbody, repbody,
244 mdt_counter_incr(req, LPROC_MDT_GETXATTR);
245 repbody->mbo_eadatasize = rc;
248 mdt_exit_ucred(info);
252 int mdt_reint_setxattr(struct mdt_thread_info *info,
253 struct mdt_lock_handle *unused)
255 struct ptlrpc_request *req = mdt_info_req(info);
256 struct mdt_lock_handle *lh;
257 const struct lu_env *env = info->mti_env;
258 struct lu_buf *buf = &info->mti_buf;
259 struct mdt_reint_record *rr = &info->mti_rr;
260 struct md_attr *ma = &info->mti_attr;
261 struct lu_attr *attr = &info->mti_attr.ma_attr;
262 struct mdt_object *obj;
263 struct md_object *child;
264 struct obd_export *exp = info->mti_exp;
265 __u64 valid = attr->la_valid;
266 const char *xattr_name = rr->rr_name.ln_name;
267 int xattr_len = rr->rr_eadatalen;
272 CDEBUG(D_INODE, "setxattr for "DFID": %s %s\n", PFID(rr->rr_fid1),
273 valid & OBD_MD_FLXATTR ? "set" : "remove", xattr_name);
275 if (info->mti_dlm_req)
276 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
278 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SETXATTR))
279 RETURN(err_serious(-ENOMEM));
281 rc = mdt_init_ucred_reint(info);
285 if (strncmp(xattr_name, XATTR_USER_PREFIX,
286 sizeof(XATTR_USER_PREFIX) - 1) == 0) {
287 if (!(exp_connect_flags(req->rq_export) & OBD_CONNECT_XATTR))
288 GOTO(out, rc = -EOPNOTSUPP);
289 } else if (strncmp(xattr_name, XATTR_TRUSTED_PREFIX,
290 sizeof(XATTR_TRUSTED_PREFIX) - 1) == 0) {
292 if (!md_capable(mdt_ucred(info), CFS_CAP_SYS_ADMIN))
293 GOTO(out, rc = -EPERM);
295 if (strcmp(xattr_name, XATTR_NAME_LOV) == 0 ||
296 strcmp(xattr_name, XATTR_NAME_LMA) == 0 ||
297 strcmp(xattr_name, XATTR_NAME_LMV) == 0 ||
298 strcmp(xattr_name, XATTR_NAME_LINK) == 0 ||
299 strcmp(xattr_name, XATTR_NAME_FID) == 0 ||
300 strcmp(xattr_name, XATTR_NAME_VERSION) == 0 ||
301 strcmp(xattr_name, XATTR_NAME_SOM) == 0 ||
302 strcmp(xattr_name, XATTR_NAME_HSM) == 0 ||
303 strcmp(xattr_name, XATTR_NAME_LFSCK_NAMESPACE) == 0)
305 } else if ((valid & OBD_MD_FLXATTR) &&
306 (strcmp(xattr_name, XATTR_NAME_ACL_ACCESS) == 0 ||
307 strcmp(xattr_name, XATTR_NAME_ACL_DEFAULT) == 0)) {
308 struct lu_nodemap *nodemap;
310 /* currently lustre limit acl access size */
311 if (xattr_len > LUSTRE_POSIX_ACL_MAX_SIZE)
312 GOTO(out, rc = -ERANGE);
314 nodemap = nodemap_get_from_exp(exp);
316 GOTO(out, rc = PTR_ERR(nodemap));
318 rc = nodemap_map_acl(nodemap, rr->rr_eadata, xattr_len,
319 NODEMAP_CLIENT_TO_FS);
320 nodemap_putref(nodemap);
324 /* ACLs were mapped out, return an error so the user knows */
326 GOTO(out, rc = -EPERM);
329 lockpart = MDS_INODELOCK_UPDATE;
330 /* Revoke all clients' lookup lock, since the access
331 * permissions for this inode is changed when ACL_ACCESS is
332 * set. This isn't needed for ACL_DEFAULT, since that does
333 * not change the access permissions of this inode, nor any
334 * other existing inodes. It is setting the ACLs inherited
335 * by new directories/files at create time. */
336 /* We need revoke both LOOKUP|PERM lock here, see mdt_attr_set. */
337 if (!strcmp(xattr_name, XATTR_NAME_ACL_ACCESS))
338 lockpart |= MDS_INODELOCK_PERM | MDS_INODELOCK_LOOKUP;
339 /* We need to take the lock on behalf of old clients so that newer
340 * clients flush their xattr caches */
342 lockpart |= MDS_INODELOCK_XATTR;
344 lh = &info->mti_lh[MDT_LH_PARENT];
345 /* ACLs were sent to clients under LCK_CR locks, so taking LCK_EX
347 mdt_lock_reg_init(lh, LCK_EX);
348 obj = mdt_object_find_lock(info, rr->rr_fid1, lh, lockpart);
350 GOTO(out, rc = PTR_ERR(obj));
352 tgt_vbr_obj_set(env, mdt_obj2dt(obj));
353 rc = mdt_version_get_check_save(info, obj, 0);
355 GOTO(out_unlock, rc);
357 if (unlikely(!(valid & OBD_MD_FLCTIME))) {
358 /* This isn't strictly an error, but all current clients
359 * should set OBD_MD_FLCTIME when setting attributes. */
360 CWARN("%s: client miss to set OBD_MD_FLCTIME when "
361 "setxattr %s: [object "DFID"] [valid "LPU64"]\n",
362 mdt_obd_name(info->mti_mdt), xattr_name,
363 PFID(rr->rr_fid1), valid);
364 attr->la_ctime = cfs_time_current_sec();
366 attr->la_valid = LA_CTIME;
367 child = mdt_object_child(obj);
368 if (valid & OBD_MD_FLXATTR) {
371 if (attr->la_flags & XATTR_REPLACE)
372 flags |= LU_XATTR_REPLACE;
374 if (attr->la_flags & XATTR_CREATE)
375 flags |= LU_XATTR_CREATE;
377 mdt_fail_write(env, info->mti_mdt->mdt_bottom,
378 OBD_FAIL_MDS_SETXATTR_WRITE);
380 buf->lb_buf = rr->rr_eadata;
381 buf->lb_len = xattr_len;
382 rc = mo_xattr_set(env, child, buf, xattr_name, flags);
383 /* update ctime after xattr changed */
385 ma->ma_attr_flags |= MDS_PERM_BYPASS;
386 mo_attr_set(env, child, ma);
388 } else if (valid & OBD_MD_FLXATTRRM) {
389 rc = mo_xattr_del(env, child, xattr_name);
390 /* update ctime after xattr changed */
392 ma->ma_attr_flags |= MDS_PERM_BYPASS;
393 mo_attr_set(env, child, ma);
396 CDEBUG(D_INFO, "valid bits: "LPX64"\n", valid);
400 mdt_counter_incr(req, LPROC_MDT_SETXATTR);
404 mdt_object_unlock_put(info, obj, lh, rc);
406 mdt_exit_ucred(info);