Whamcloud - gitweb
LU-7473 acl: increase ACL entries limitation
[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_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)
123 {
124         const struct lu_env *env = info->mti_env;
125         char *v, *b, *eadatahead, *eadatatail;
126         __u32 *sizes;
127         int eadatasize, eavallen = 0, eavallens = 0, rc;
128
129         ENTRY;
130
131         /*
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
136          */
137
138         eadatahead = buf->lb_buf;
139
140         /* Fill out EADATA first */
141         rc = mo_xattr_list(env, next, buf);
142         if (rc < 0)
143                 GOTO(out_shrink, rc);
144
145         eadatasize = rc;
146         eadatatail = eadatahead + eadatasize;
147
148         v = req_capsule_server_get(info->mti_pill, &RMF_EAVALS);
149         sizes = req_capsule_server_get(info->mti_pill, &RMF_EAVALS_LENS);
150
151         /* Fill out EAVALS and EAVALS_LENS */
152         for (b = eadatahead; b < eadatatail; b += strlen(b) + 1, v += rc) {
153                 buf->lb_buf = v;
154                 buf->lb_len = reqbody->mbo_eadatasize - eavallen;
155                 rc = mo_xattr_get(env, next, buf, b);
156                 if (rc < 0)
157                         GOTO(out_shrink, rc);
158
159                 sizes[eavallens] = rc;
160                 eavallens++;
161                 eavallen += rc;
162         }
163
164 out_shrink:
165         if (rc < 0) {
166                 eadatasize = 0;
167                 eavallens = 0;
168                 eavallen = 0;
169         }
170         repbody->mbo_aclsize = eavallen;
171         repbody->mbo_max_mdsize = eavallens;
172
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);
177
178         if (rc >= 0)
179                 RETURN(eadatasize);
180         return rc;
181 }
182
183 int mdt_getxattr(struct mdt_thread_info *info)
184 {
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;
189         struct lu_buf          *buf;
190         int                     easize, rc;
191         u64                     valid;
192         ENTRY;
193
194         LASSERT(info->mti_object != NULL);
195         LASSERT(lu_object_assert_exists(&info->mti_object->mot_obj));
196
197         CDEBUG(D_INODE, "getxattr "DFID"\n", PFID(&info->mti_body->mbo_fid1));
198
199         reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
200         if (reqbody == NULL)
201                 RETURN(err_serious(-EFAULT));
202
203         rc = mdt_init_ucred(info, reqbody);
204         if (rc)
205                 RETURN(err_serious(rc));
206
207         next = mdt_object_child(info->mti_object);
208         easize = mdt_getxattr_pack_reply(info);
209         if (easize < 0)
210                 GOTO(out, rc = err_serious(easize));
211
212         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
213         LASSERT(repbody != NULL);
214
215         /* No need further getxattr. */
216         if (easize == 0 || reqbody->mbo_eadatasize == 0)
217                 GOTO(out, rc = easize);
218
219         buf = &info->mti_buf;
220         buf->lb_buf = req_capsule_server_get(info->mti_pill, &RMF_EADATA);
221         buf->lb_len = easize;
222
223         valid = info->mti_body->mbo_valid & (OBD_MD_FLXATTR | OBD_MD_FLXATTRLS);
224
225         if (valid == OBD_MD_FLXATTR) {
226                 char *xattr_name = req_capsule_client_get(info->mti_pill,
227                                                           &RMF_NAME);
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");
231
232                 rc = mo_xattr_list(info->mti_env, next, buf);
233                 if (rc < 0)
234                         CDEBUG(D_INFO, "listxattr failed: %d\n", rc);
235         } else if (valid == OBD_MD_FLXATTRALL) {
236                 rc = mdt_getxattr_all(info, reqbody, repbody,
237                                       buf, next);
238         } else
239                 LBUG();
240
241         EXIT;
242 out:
243         if (rc >= 0) {
244                 mdt_counter_incr(req, LPROC_MDT_GETXATTR);
245                 repbody->mbo_eadatasize = rc;
246                 rc = 0;
247         }
248         mdt_exit_ucred(info);
249         return rc;
250 }
251
252 int mdt_reint_setxattr(struct mdt_thread_info *info,
253                        struct mdt_lock_handle *unused)
254 {
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;
268         __u64                    lockpart = MDS_INODELOCK_UPDATE;
269         int                      rc;
270         ENTRY;
271
272         CDEBUG(D_INODE, "setxattr for "DFID": %s %s\n", PFID(rr->rr_fid1),
273                valid & OBD_MD_FLXATTR ? "set" : "remove", xattr_name);
274
275         if (info->mti_dlm_req)
276                 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
277
278         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SETXATTR))
279                 RETURN(err_serious(-ENOMEM));
280
281         rc = mdt_init_ucred_reint(info);
282         if (rc != 0)
283                 RETURN(rc);
284
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) {
291
292                 if (!md_capable(mdt_ucred(info), CFS_CAP_SYS_ADMIN))
293                         GOTO(out, rc = -EPERM);
294
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)
304                         GOTO(out, rc = 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;
309
310                 if ((xattr_len > info->mti_mdt->mdt_max_ea_size) ||
311                      (!exp_connect_large_acl(exp) &&
312                       xattr_len > LUSTRE_POSIX_ACL_MAX_SIZE_OLD))
313                         GOTO(out, rc = -ERANGE);
314
315                 nodemap = nodemap_get_from_exp(exp);
316                 if (IS_ERR(nodemap))
317                         GOTO(out, rc = PTR_ERR(nodemap));
318
319                 rc = nodemap_map_acl(nodemap, rr->rr_eadata, xattr_len,
320                                      NODEMAP_CLIENT_TO_FS);
321                 nodemap_putref(nodemap);
322                 if (rc < 0)
323                         GOTO(out, rc);
324
325                 /* ACLs were mapped out, return an error so the user knows */
326                 if (rc != xattr_len)
327                         GOTO(out, rc = -EPERM);
328         } else if ((strlen(xattr_name) > strlen(XATTR_LUSTRE_LOV) + 1) &&
329                    strncmp(xattr_name, XATTR_LUSTRE_LOV,
330                            strlen(XATTR_LUSTRE_LOV)) == 0) {
331
332                 if (strncmp(xattr_name, XATTR_LUSTRE_LOV".add",
333                             strlen(XATTR_LUSTRE_LOV".add")) &&
334                     strncmp(xattr_name, XATTR_LUSTRE_LOV".set",
335                             strlen(XATTR_LUSTRE_LOV".set")) &&
336                     strncmp(xattr_name, XATTR_LUSTRE_LOV".del",
337                             strlen(XATTR_LUSTRE_LOV".del"))) {
338                         CERROR("%s: invalid xattr name: %s\n",
339                                mdt_obd_name(info->mti_mdt), xattr_name);
340                         GOTO(out, rc = -EINVAL);
341                 }
342
343                 lockpart |= MDS_INODELOCK_LAYOUT;
344         }
345
346         /* Revoke all clients' lookup lock, since the access
347          * permissions for this inode is changed when ACL_ACCESS is
348          * set. This isn't needed for ACL_DEFAULT, since that does
349          * not change the access permissions of this inode, nor any
350          * other existing inodes. It is setting the ACLs inherited
351          * by new directories/files at create time. */
352         /* We need revoke both LOOKUP|PERM lock here, see mdt_attr_set. */
353         if (!strcmp(xattr_name, XATTR_NAME_ACL_ACCESS))
354                 lockpart |= MDS_INODELOCK_PERM | MDS_INODELOCK_LOOKUP;
355         /* We need to take the lock on behalf of old clients so that newer
356          * clients flush their xattr caches */
357         else
358                 lockpart |= MDS_INODELOCK_XATTR;
359
360         lh = &info->mti_lh[MDT_LH_PARENT];
361         /* ACLs were sent to clients under LCK_CR locks, so taking LCK_EX
362          * to cancel them. */
363         mdt_lock_reg_init(lh, LCK_EX);
364         obj = mdt_object_find_lock(info, rr->rr_fid1, lh, lockpart);
365         if (IS_ERR(obj))
366                 GOTO(out, rc = PTR_ERR(obj));
367
368         tgt_vbr_obj_set(env, mdt_obj2dt(obj));
369         rc = mdt_version_get_check_save(info, obj, 0);
370         if (rc)
371                 GOTO(out_unlock, rc);
372
373         if (unlikely(!(valid & OBD_MD_FLCTIME))) {
374                 /* This isn't strictly an error, but all current clients
375                  * should set OBD_MD_FLCTIME when setting attributes. */
376                 CWARN("%s: client miss to set OBD_MD_FLCTIME when "
377                       "setxattr %s: [object "DFID"] [valid %llu]\n",
378                       mdt_obd_name(info->mti_mdt), xattr_name,
379                       PFID(rr->rr_fid1), valid);
380                 attr->la_ctime = cfs_time_current_sec();
381         }
382         attr->la_valid = LA_CTIME;
383         child = mdt_object_child(obj);
384         if (valid & OBD_MD_FLXATTR) {
385                 int     flags = 0;
386
387                 if (attr->la_flags & XATTR_REPLACE)
388                         flags |= LU_XATTR_REPLACE;
389
390                 if (attr->la_flags & XATTR_CREATE)
391                         flags |= LU_XATTR_CREATE;
392
393                 mdt_fail_write(env, info->mti_mdt->mdt_bottom,
394                                OBD_FAIL_MDS_SETXATTR_WRITE);
395
396                 buf->lb_buf = rr->rr_eadata;
397                 buf->lb_len = xattr_len;
398                 rc = mo_xattr_set(env, child, buf, xattr_name, flags);
399                 /* update ctime after xattr changed */
400                 if (rc == 0) {
401                         ma->ma_attr_flags |= MDS_PERM_BYPASS;
402                         mo_attr_set(env, child, ma);
403                 }
404         } else if (valid & OBD_MD_FLXATTRRM) {
405                 rc = mo_xattr_del(env, child, xattr_name);
406                 /* update ctime after xattr changed */
407                 if (rc == 0) {
408                         ma->ma_attr_flags |= MDS_PERM_BYPASS;
409                         mo_attr_set(env, child, ma);
410                 }
411         } else {
412                 CDEBUG(D_INFO, "valid bits: %#llx\n", valid);
413                 rc = -EINVAL;
414         }
415
416         if (rc == 0)
417                 mdt_counter_incr(req, LPROC_MDT_SETXATTR);
418
419         EXIT;
420 out_unlock:
421         mdt_object_unlock_put(info, obj, lh, rc);
422 out:
423         mdt_exit_ucred(info);
424         return rc;
425 }