Whamcloud - gitweb
LU-11107 mdt: handle nonexistent xattrs correctly
[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, 2017, 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         const char *xattr_name;
57         u64 valid;
58         static const char user_string[] = "user.";
59         int size;
60         int rc = 0;
61         int rc2;
62         ENTRY;
63
64         valid = info->mti_body->mbo_valid & (OBD_MD_FLXATTR | OBD_MD_FLXATTRLS);
65
66         /* Determine how many bytes we need */
67         if (valid == OBD_MD_FLXATTR) {
68                 xattr_name = req_capsule_client_get(pill, &RMF_NAME);
69                 if (!xattr_name)
70                         RETURN(-EFAULT);
71
72                 if (!(exp_connect_flags(req->rq_export) & OBD_CONNECT_XATTR) &&
73                     !strncmp(xattr_name, user_string, sizeof(user_string) - 1))
74                         RETURN(-EOPNOTSUPP);
75
76                 size = mo_xattr_get(info->mti_env,
77                                     mdt_object_child(info->mti_object),
78                                     &LU_BUF_NULL, xattr_name);
79                 if (size == -ENODATA) {
80                         /* XXX: Some client code will not handle -ENODATA
81                          * for XATTR_NAME_LOV (trusted.lov) properly. */
82                         if (strcmp(xattr_name, XATTR_NAME_LOV) == 0)
83                                 rc = 0;
84                         else
85                                 rc = -ENODATA;
86
87                         size = 0;
88                 }
89         } else if (valid == OBD_MD_FLXATTRLS) {
90                 xattr_name = "list";
91                 size = mo_xattr_list(info->mti_env,
92                                      mdt_object_child(info->mti_object),
93                                      &LU_BUF_NULL);
94         } else if (valid == OBD_MD_FLXATTRALL) {
95                 xattr_name = "all";
96                 /* N.B. eadatasize = 0 is not valid for FLXATTRALL */
97                 /* We could calculate accurate sizes, but this would
98                  * introduce a lot of overhead, let's do it later... */
99                 size = info->mti_body->mbo_eadatasize;
100                 req_capsule_set_size(pill, &RMF_EAVALS, RCL_SERVER, size);
101                 req_capsule_set_size(pill, &RMF_EAVALS_LENS, RCL_SERVER, size);
102         } else {
103                 CDEBUG(D_INFO, "Valid bits: %#llx\n",
104                        info->mti_body->mbo_valid);
105                 RETURN(-EINVAL);
106         }
107
108         if (size < 0) {
109                 if (size != -EOPNOTSUPP && size != -ENOENT)
110                         CERROR("%s: error geting EA size for '%s': rc = %d\n",
111                                mdt_obd_name(info->mti_mdt), xattr_name, size);
112                 RETURN(size);
113         }
114
115         if (req_capsule_has_field(pill, &RMF_ACL, RCL_SERVER))
116                 req_capsule_set_size(pill, &RMF_ACL, RCL_SERVER,
117                                      LUSTRE_POSIX_ACL_MAX_SIZE_OLD);
118
119         req_capsule_set_size(pill, &RMF_EADATA, RCL_SERVER,
120                              info->mti_body->mbo_eadatasize == 0 ? 0 : size);
121
122         rc2 = req_capsule_server_pack(pill);
123         if (rc2 < 0)
124                 RETURN(rc2);
125
126         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETXATTR_PACK))
127                 RETURN(-ENOMEM);
128
129         RETURN(rc < 0 ? rc : size);
130 }
131
132 static int mdt_nodemap_map_acl(struct mdt_thread_info *info, void *buf,
133                                size_t size, const char *name,
134                                enum nodemap_tree_type tree_type)
135 {
136         struct lu_nodemap      *nodemap;
137         struct obd_export      *exp = info->mti_exp;
138         int                     rc = size;
139
140         ENTRY;
141
142         if (strcmp(name, XATTR_NAME_ACL_ACCESS) == 0 ||
143             strcmp(name, XATTR_NAME_ACL_DEFAULT) == 0) {
144                 if (size > info->mti_mdt->mdt_max_ea_size ||
145                      (!exp_connect_large_acl(exp) &&
146                       size > LUSTRE_POSIX_ACL_MAX_SIZE_OLD))
147                         GOTO(out, rc = -ERANGE);
148
149                 nodemap = nodemap_get_from_exp(exp);
150                 if (IS_ERR(nodemap))
151                         GOTO(out, rc = PTR_ERR(nodemap));
152
153                 rc = nodemap_map_acl(nodemap, buf, size, tree_type);
154                 nodemap_putref(nodemap);
155                 if (rc < 0)
156                         GOTO(out, rc);
157         }
158 out:
159         RETURN(rc);
160 }
161
162 static int mdt_getxattr_all(struct mdt_thread_info *info,
163                             struct mdt_body *reqbody, struct mdt_body *repbody,
164                             struct lu_buf *buf, struct md_object *next)
165 {
166         const struct lu_env *env = info->mti_env;
167         char *v, *b, *eadatahead, *eadatatail;
168         __u32 *sizes;
169         int eadatasize, eavallen = 0, eavallens = 0, rc;
170
171         ENTRY;
172
173         /*
174          * The format of the pill is the following:
175          * EADATA:      attr1\0attr2\0...attrn\0
176          * EAVALS:      val1val2...valn
177          * EAVALS_LENS: 4,4,...4
178          */
179
180         eadatahead = buf->lb_buf;
181
182         /* Fill out EADATA first */
183         rc = mo_xattr_list(env, next, buf);
184         if (rc < 0)
185                 GOTO(out_shrink, rc);
186
187         eadatasize = rc;
188         eadatatail = eadatahead + eadatasize;
189
190         v = req_capsule_server_get(info->mti_pill, &RMF_EAVALS);
191         sizes = req_capsule_server_get(info->mti_pill, &RMF_EAVALS_LENS);
192
193         /* Fill out EAVALS and EAVALS_LENS */
194         for (b = eadatahead; b < eadatatail; b += strlen(b) + 1, v += rc) {
195                 buf->lb_buf = v;
196                 buf->lb_len = reqbody->mbo_eadatasize - eavallen;
197                 rc = mo_xattr_get(env, next, buf, b);
198                 if (rc < 0)
199                         GOTO(out_shrink, rc);
200                 rc = mdt_nodemap_map_acl(info, buf->lb_buf, rc, b,
201                                          NODEMAP_FS_TO_CLIENT);
202                 if (rc < 0)
203                         GOTO(out_shrink, rc);
204                 sizes[eavallens] = rc;
205                 eavallens++;
206                 eavallen += rc;
207         }
208
209 out_shrink:
210         if (rc < 0) {
211                 eadatasize = 0;
212                 eavallens = 0;
213                 eavallen = 0;
214         }
215         repbody->mbo_aclsize = eavallen;
216         repbody->mbo_max_mdsize = eavallens;
217
218         req_capsule_shrink(info->mti_pill, &RMF_EAVALS, eavallen, RCL_SERVER);
219         req_capsule_shrink(info->mti_pill, &RMF_EAVALS_LENS,
220                            eavallens * sizeof(__u32), RCL_SERVER);
221         req_capsule_shrink(info->mti_pill, &RMF_EADATA, eadatasize, RCL_SERVER);
222
223         if (rc >= 0)
224                 RETURN(eadatasize);
225         return rc;
226 }
227
228 int mdt_getxattr(struct mdt_thread_info *info)
229 {
230         struct ptlrpc_request  *req = mdt_info_req(info);
231         struct mdt_body        *reqbody;
232         struct mdt_body        *repbody = NULL;
233         struct md_object       *next;
234         struct lu_buf          *buf;
235         int                     easize, rc;
236         u64                     valid;
237         ENTRY;
238
239         LASSERT(info->mti_object != NULL);
240         LASSERT(lu_object_assert_exists(&info->mti_object->mot_obj));
241
242         CDEBUG(D_INODE, "getxattr "DFID"\n", PFID(&info->mti_body->mbo_fid1));
243
244         reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
245         if (reqbody == NULL)
246                 RETURN(err_serious(-EFAULT));
247
248         rc = mdt_init_ucred(info, reqbody);
249         if (rc)
250                 RETURN(err_serious(rc));
251
252         next = mdt_object_child(info->mti_object);
253         easize = mdt_getxattr_pack_reply(info);
254         if (easize == -ENODATA)
255                 GOTO(out, rc = easize);
256         else if (easize < 0)
257                 GOTO(out, rc = err_serious(easize));
258
259         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
260         LASSERT(repbody != NULL);
261
262         /* No need further getxattr. */
263         if (easize == 0 || reqbody->mbo_eadatasize == 0)
264                 GOTO(out, rc = easize);
265
266         buf = &info->mti_buf;
267         buf->lb_buf = req_capsule_server_get(info->mti_pill, &RMF_EADATA);
268         buf->lb_len = easize;
269
270         valid = info->mti_body->mbo_valid & (OBD_MD_FLXATTR | OBD_MD_FLXATTRLS);
271
272         if (valid == OBD_MD_FLXATTR) {
273                 const char *xattr_name = req_capsule_client_get(info->mti_pill,
274                                                                 &RMF_NAME);
275                 rc = mo_xattr_get(info->mti_env, next, buf, xattr_name);
276                 if (rc < 0)
277                         GOTO(out, rc);
278
279                 rc = mdt_nodemap_map_acl(info, buf->lb_buf, rc, xattr_name,
280                                          NODEMAP_FS_TO_CLIENT);
281         } else if (valid == OBD_MD_FLXATTRLS) {
282                 CDEBUG(D_INODE, "listxattr\n");
283
284                 rc = mo_xattr_list(info->mti_env, next, buf);
285                 if (rc < 0)
286                         CDEBUG(D_INFO, "listxattr failed: %d\n", rc);
287         } else if (valid == OBD_MD_FLXATTRALL) {
288                 rc = mdt_getxattr_all(info, reqbody, repbody,
289                                       buf, next);
290         } else
291                 LBUG();
292
293         EXIT;
294 out:
295         if (rc >= 0) {
296                 mdt_counter_incr(req, LPROC_MDT_GETXATTR);
297                 repbody->mbo_eadatasize = rc;
298                 rc = 0;
299         }
300         mdt_exit_ucred(info);
301         return rc;
302 }
303
304 int mdt_reint_setxattr(struct mdt_thread_info *info,
305                        struct mdt_lock_handle *unused)
306 {
307         struct ptlrpc_request   *req = mdt_info_req(info);
308         struct mdt_lock_handle  *lh;
309         const struct lu_env     *env  = info->mti_env;
310         struct lu_buf           *buf  = &info->mti_buf;
311         struct mdt_reint_record *rr   = &info->mti_rr;
312         struct md_attr          *ma = &info->mti_attr;
313         struct lu_attr          *attr = &info->mti_attr.ma_attr;
314         struct mdt_object       *obj;
315         struct md_object        *child;
316         __u64                    valid = attr->la_valid;
317         const char              *xattr_name = rr->rr_name.ln_name;
318         int                      xattr_len = rr->rr_eadatalen;
319         __u64                    lockpart = MDS_INODELOCK_UPDATE;
320         int                      rc;
321         ENTRY;
322
323         CDEBUG(D_INODE, "setxattr for "DFID": %s %s\n", PFID(rr->rr_fid1),
324                valid & OBD_MD_FLXATTR ? "set" : "remove", xattr_name);
325
326         if (info->mti_dlm_req)
327                 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
328
329         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SETXATTR))
330                 RETURN(err_serious(-ENOMEM));
331
332         rc = mdt_init_ucred_reint(info);
333         if (rc != 0)
334                 RETURN(rc);
335
336         if (strncmp(xattr_name, XATTR_USER_PREFIX,
337                     sizeof(XATTR_USER_PREFIX) - 1) == 0) {
338                 if (!(exp_connect_flags(req->rq_export) & OBD_CONNECT_XATTR))
339                         GOTO(out, rc = -EOPNOTSUPP);
340         } else if (strncmp(xattr_name, XATTR_TRUSTED_PREFIX,
341                     sizeof(XATTR_TRUSTED_PREFIX) - 1) == 0) {
342
343                 if (!md_capable(mdt_ucred(info), CFS_CAP_SYS_ADMIN))
344                         GOTO(out, rc = -EPERM);
345
346                 if (strcmp(xattr_name, XATTR_NAME_LOV) == 0 ||
347                     strcmp(xattr_name, XATTR_NAME_LMA) == 0 ||
348                     strcmp(xattr_name, XATTR_NAME_LMV) == 0 ||
349                     strcmp(xattr_name, XATTR_NAME_LINK) == 0 ||
350                     strcmp(xattr_name, XATTR_NAME_FID) == 0 ||
351                     strcmp(xattr_name, XATTR_NAME_VERSION) == 0 ||
352                     strcmp(xattr_name, XATTR_NAME_SOM) == 0 ||
353                     strcmp(xattr_name, XATTR_NAME_HSM) == 0 ||
354                     strcmp(xattr_name, XATTR_NAME_LFSCK_NAMESPACE) == 0)
355                         GOTO(out, rc = 0);
356         } else if ((valid & OBD_MD_FLXATTR) &&
357                    (strcmp(xattr_name, XATTR_NAME_ACL_ACCESS) == 0 ||
358                     strcmp(xattr_name, XATTR_NAME_ACL_DEFAULT) == 0)) {
359                 rc = mdt_nodemap_map_acl(info, rr->rr_eadata, xattr_len,
360                                          xattr_name, NODEMAP_CLIENT_TO_FS);
361                 if (rc < 0)
362                         GOTO(out, rc);
363                 /* ACLs were mapped out, return an error so the user knows */
364                 if (rc != xattr_len)
365                         GOTO(out, rc = -EPERM);
366         } else if ((strlen(xattr_name) > strlen(XATTR_LUSTRE_LOV) + 1) &&
367                    strncmp(xattr_name, XATTR_LUSTRE_LOV,
368                            strlen(XATTR_LUSTRE_LOV)) == 0) {
369
370                 if (strncmp(xattr_name, XATTR_LUSTRE_LOV".add",
371                             strlen(XATTR_LUSTRE_LOV".add")) &&
372                     strncmp(xattr_name, XATTR_LUSTRE_LOV".set",
373                             strlen(XATTR_LUSTRE_LOV".set")) &&
374                     strncmp(xattr_name, XATTR_LUSTRE_LOV".del",
375                             strlen(XATTR_LUSTRE_LOV".del"))) {
376                         CERROR("%s: invalid xattr name: %s\n",
377                                mdt_obd_name(info->mti_mdt), xattr_name);
378                         GOTO(out, rc = -EINVAL);
379                 }
380
381                 lockpart |= MDS_INODELOCK_LAYOUT;
382         }
383
384         /* Revoke all clients' lookup lock, since the access
385          * permissions for this inode is changed when ACL_ACCESS is
386          * set. This isn't needed for ACL_DEFAULT, since that does
387          * not change the access permissions of this inode, nor any
388          * other existing inodes. It is setting the ACLs inherited
389          * by new directories/files at create time. */
390         /* We need revoke both LOOKUP|PERM lock here, see mdt_attr_set. */
391         if (!strcmp(xattr_name, XATTR_NAME_ACL_ACCESS))
392                 lockpart |= MDS_INODELOCK_PERM | MDS_INODELOCK_LOOKUP;
393         /* We need to take the lock on behalf of old clients so that newer
394          * clients flush their xattr caches */
395         else
396                 lockpart |= MDS_INODELOCK_XATTR;
397
398         lh = &info->mti_lh[MDT_LH_PARENT];
399         /* ACLs were sent to clients under LCK_CR locks, so taking LCK_EX
400          * to cancel them. */
401         mdt_lock_reg_init(lh, LCK_EX);
402         obj = mdt_object_find_lock(info, rr->rr_fid1, lh, lockpart);
403         if (IS_ERR(obj))
404                 GOTO(out, rc = PTR_ERR(obj));
405
406         tgt_vbr_obj_set(env, mdt_obj2dt(obj));
407         rc = mdt_version_get_check_save(info, obj, 0);
408         if (rc)
409                 GOTO(out_unlock, rc);
410
411         if (unlikely(!(valid & OBD_MD_FLCTIME))) {
412                 /* This isn't strictly an error, but all current clients
413                  * should set OBD_MD_FLCTIME when setting attributes. */
414                 CWARN("%s: client miss to set OBD_MD_FLCTIME when "
415                       "setxattr %s: [object "DFID"] [valid %llu]\n",
416                       mdt_obd_name(info->mti_mdt), xattr_name,
417                       PFID(rr->rr_fid1), valid);
418                 attr->la_ctime = ktime_get_real_seconds();
419         }
420         attr->la_valid = LA_CTIME;
421         child = mdt_object_child(obj);
422         if (valid & OBD_MD_FLXATTR) {
423                 int     flags = 0;
424
425                 if (attr->la_flags & XATTR_REPLACE)
426                         flags |= LU_XATTR_REPLACE;
427
428                 if (attr->la_flags & XATTR_CREATE)
429                         flags |= LU_XATTR_CREATE;
430
431                 mdt_fail_write(env, info->mti_mdt->mdt_bottom,
432                                OBD_FAIL_MDS_SETXATTR_WRITE);
433
434                 buf->lb_buf = rr->rr_eadata;
435                 buf->lb_len = xattr_len;
436                 rc = mo_xattr_set(env, child, buf, xattr_name, flags);
437                 /* update ctime after xattr changed */
438                 if (rc == 0) {
439                         ma->ma_attr_flags |= MDS_PERM_BYPASS;
440                         mo_attr_set(env, child, ma);
441                 }
442         } else if (valid & OBD_MD_FLXATTRRM) {
443                 rc = mo_xattr_del(env, child, xattr_name);
444                 /* update ctime after xattr changed */
445                 if (rc == 0) {
446                         ma->ma_attr_flags |= MDS_PERM_BYPASS;
447                         mo_attr_set(env, child, ma);
448                 }
449         } else {
450                 CDEBUG(D_INFO, "valid bits: %#llx\n", valid);
451                 rc = -EINVAL;
452         }
453
454         if (rc == 0)
455                 mdt_counter_incr(req, LPROC_MDT_SETXATTR);
456
457         EXIT;
458 out_unlock:
459         mdt_object_unlock_put(info, obj, lh, rc);
460 out:
461         mdt_exit_ucred(info);
462         return rc;
463 }