Whamcloud - gitweb
3cd39f48d88a6758ef69f84cbdf93181f017dbb8
[fs/lustre-release.git] / lustre / mdt / mdt_xattr.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9  *
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).
15  *
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
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2016, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lustre/mdt/mdt_xattr.c
33  *
34  * Lustre Metadata Target (mdt) extended attributes management.
35  *
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>
40  */
41
42 #define DEBUG_SUBSYSTEM S_MDS
43
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"
49
50
51 /* return EADATA length to the caller. negative value means error */
52 static int mdt_getxattr_pack_reply(struct mdt_thread_info * info)
53 {
54         struct req_capsule     *pill = info->mti_pill ;
55         struct ptlrpc_request  *req = mdt_info_req(info);
56         char                   *xattr_name;
57         __u64                   valid;
58         static const char       user_string[] = "user.";
59         int                     size, rc;
60         ENTRY;
61
62         valid = info->mti_body->mbo_valid & (OBD_MD_FLXATTR | OBD_MD_FLXATTRLS);
63
64         /* Determine how many bytes we need */
65         if (valid == OBD_MD_FLXATTR) {
66                 xattr_name = req_capsule_client_get(pill, &RMF_NAME);
67                 if (!xattr_name)
68                         RETURN(-EFAULT);
69
70                 if (!(exp_connect_flags(req->rq_export) & OBD_CONNECT_XATTR) &&
71                     !strncmp(xattr_name, user_string, sizeof(user_string) - 1))
72                         RETURN(-EOPNOTSUPP);
73
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),
80                                      &LU_BUF_NULL);
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);
88         } else {
89                 CDEBUG(D_INFO, "Valid bits: %#llx\n",
90                        info->mti_body->mbo_valid);
91                 RETURN(-EINVAL);
92         }
93
94         if (size == -ENODATA) {
95                 size = 0;
96         } else if (size < 0) {
97                 if (size != -EOPNOTSUPP)
98                         CERROR("Error geting EA size: %d\n", size);
99                 RETURN(size);
100         }
101
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);
105
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);
109         if (rc) {
110                 LASSERT(rc < 0);
111                 RETURN(rc);
112         }
113
114         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETXATTR_PACK))
115                 RETURN(-ENOMEM);
116
117         RETURN(size);
118 }
119
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)
123 {
124         struct lu_nodemap      *nodemap;
125         struct obd_export      *exp = info->mti_exp;
126         int                     rc = size;
127
128         ENTRY;
129
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);
136
137                 nodemap = nodemap_get_from_exp(exp);
138                 if (IS_ERR(nodemap))
139                         GOTO(out, rc = PTR_ERR(nodemap));
140
141                 rc = nodemap_map_acl(nodemap, buf, size, tree_type);
142                 nodemap_putref(nodemap);
143                 if (rc < 0)
144                         GOTO(out, rc);
145         }
146 out:
147         RETURN(rc);
148 }
149
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)
153 {
154         const struct lu_env *env = info->mti_env;
155         char *v, *b, *eadatahead, *eadatatail;
156         __u32 *sizes;
157         int eadatasize, eavallen = 0, eavallens = 0, rc;
158
159         ENTRY;
160
161         /*
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
166          */
167
168         eadatahead = buf->lb_buf;
169
170         /* Fill out EADATA first */
171         rc = mo_xattr_list(env, next, buf);
172         if (rc < 0)
173                 GOTO(out_shrink, rc);
174
175         eadatasize = rc;
176         eadatatail = eadatahead + eadatasize;
177
178         v = req_capsule_server_get(info->mti_pill, &RMF_EAVALS);
179         sizes = req_capsule_server_get(info->mti_pill, &RMF_EAVALS_LENS);
180
181         /* Fill out EAVALS and EAVALS_LENS */
182         for (b = eadatahead; b < eadatatail; b += strlen(b) + 1, v += rc) {
183                 buf->lb_buf = v;
184                 buf->lb_len = reqbody->mbo_eadatasize - eavallen;
185                 rc = mo_xattr_get(env, next, buf, b);
186                 if (rc < 0)
187                         GOTO(out_shrink, rc);
188                 rc = mdt_nodemap_map_acl(info, buf->lb_buf, rc, b,
189                                          NODEMAP_FS_TO_CLIENT);
190                 if (rc < 0)
191                         GOTO(out_shrink, rc);
192                 sizes[eavallens] = rc;
193                 eavallens++;
194                 eavallen += rc;
195         }
196
197 out_shrink:
198         if (rc < 0) {
199                 eadatasize = 0;
200                 eavallens = 0;
201                 eavallen = 0;
202         }
203         repbody->mbo_aclsize = eavallen;
204         repbody->mbo_max_mdsize = eavallens;
205
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);
210
211         if (rc >= 0)
212                 RETURN(eadatasize);
213         return rc;
214 }
215
216 int mdt_getxattr(struct mdt_thread_info *info)
217 {
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;
222         struct lu_buf          *buf;
223         int                     easize, rc;
224         u64                     valid;
225         ENTRY;
226
227         LASSERT(info->mti_object != NULL);
228         LASSERT(lu_object_assert_exists(&info->mti_object->mot_obj));
229
230         CDEBUG(D_INODE, "getxattr "DFID"\n", PFID(&info->mti_body->mbo_fid1));
231
232         reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
233         if (reqbody == NULL)
234                 RETURN(err_serious(-EFAULT));
235
236         rc = mdt_init_ucred(info, reqbody);
237         if (rc)
238                 RETURN(err_serious(rc));
239
240         next = mdt_object_child(info->mti_object);
241         easize = mdt_getxattr_pack_reply(info);
242         if (easize < 0)
243                 GOTO(out, rc = err_serious(easize));
244
245         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
246         LASSERT(repbody != NULL);
247
248         /* No need further getxattr. */
249         if (easize == 0 || reqbody->mbo_eadatasize == 0)
250                 GOTO(out, rc = easize);
251
252         buf = &info->mti_buf;
253         buf->lb_buf = req_capsule_server_get(info->mti_pill, &RMF_EADATA);
254         buf->lb_len = easize;
255
256         valid = info->mti_body->mbo_valid & (OBD_MD_FLXATTR | OBD_MD_FLXATTRLS);
257
258         if (valid == OBD_MD_FLXATTR) {
259                 const char *xattr_name = req_capsule_client_get(info->mti_pill,
260                                                                 &RMF_NAME);
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");
266
267                 rc = mo_xattr_list(info->mti_env, next, buf);
268                 if (rc < 0)
269                         CDEBUG(D_INFO, "listxattr failed: %d\n", rc);
270         } else if (valid == OBD_MD_FLXATTRALL) {
271                 rc = mdt_getxattr_all(info, reqbody, repbody,
272                                       buf, next);
273         } else
274                 LBUG();
275
276         EXIT;
277 out:
278         if (rc >= 0) {
279                 mdt_counter_incr(req, LPROC_MDT_GETXATTR);
280                 repbody->mbo_eadatasize = rc;
281                 rc = 0;
282         }
283         mdt_exit_ucred(info);
284         return rc;
285 }
286
287 int mdt_reint_setxattr(struct mdt_thread_info *info,
288                        struct mdt_lock_handle *unused)
289 {
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;
303         int                      rc;
304         ENTRY;
305
306         CDEBUG(D_INODE, "setxattr for "DFID": %s %s\n", PFID(rr->rr_fid1),
307                valid & OBD_MD_FLXATTR ? "set" : "remove", xattr_name);
308
309         if (info->mti_dlm_req)
310                 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
311
312         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SETXATTR))
313                 RETURN(err_serious(-ENOMEM));
314
315         rc = mdt_init_ucred_reint(info);
316         if (rc != 0)
317                 RETURN(rc);
318
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) {
325
326                 if (!md_capable(mdt_ucred(info), CFS_CAP_SYS_ADMIN))
327                         GOTO(out, rc = -EPERM);
328
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)
338                         GOTO(out, rc = 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);
344                 if (rc < 0)
345                         GOTO(out, rc);
346                 /* ACLs were mapped out, return an error so the user knows */
347                 if (rc != xattr_len)
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) {
352
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);
362                 }
363
364                 lockpart |= MDS_INODELOCK_LAYOUT;
365         }
366
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 */
378         else
379                 lockpart |= MDS_INODELOCK_XATTR;
380
381         lh = &info->mti_lh[MDT_LH_PARENT];
382         /* ACLs were sent to clients under LCK_CR locks, so taking LCK_EX
383          * to cancel them. */
384         mdt_lock_reg_init(lh, LCK_EX);
385         obj = mdt_object_find_lock(info, rr->rr_fid1, lh, lockpart);
386         if (IS_ERR(obj))
387                 GOTO(out, rc = PTR_ERR(obj));
388
389         tgt_vbr_obj_set(env, mdt_obj2dt(obj));
390         rc = mdt_version_get_check_save(info, obj, 0);
391         if (rc)
392                 GOTO(out_unlock, rc);
393
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();
402         }
403         attr->la_valid = LA_CTIME;
404         child = mdt_object_child(obj);
405         if (valid & OBD_MD_FLXATTR) {
406                 int     flags = 0;
407
408                 if (attr->la_flags & XATTR_REPLACE)
409                         flags |= LU_XATTR_REPLACE;
410
411                 if (attr->la_flags & XATTR_CREATE)
412                         flags |= LU_XATTR_CREATE;
413
414                 mdt_fail_write(env, info->mti_mdt->mdt_bottom,
415                                OBD_FAIL_MDS_SETXATTR_WRITE);
416
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 */
421                 if (rc == 0) {
422                         ma->ma_attr_flags |= MDS_PERM_BYPASS;
423                         mo_attr_set(env, child, ma);
424                 }
425         } else if (valid & OBD_MD_FLXATTRRM) {
426                 rc = mo_xattr_del(env, child, xattr_name);
427                 /* update ctime after xattr changed */
428                 if (rc == 0) {
429                         ma->ma_attr_flags |= MDS_PERM_BYPASS;
430                         mo_attr_set(env, child, ma);
431                 }
432         } else {
433                 CDEBUG(D_INFO, "valid bits: %#llx\n", valid);
434                 rc = -EINVAL;
435         }
436
437         if (rc == 0)
438                 mdt_counter_incr(req, LPROC_MDT_SETXATTR);
439
440         EXIT;
441 out_unlock:
442         mdt_object_unlock_put(info, obj, lh, rc);
443 out:
444         mdt_exit_ucred(info);
445         return rc;
446 }