Whamcloud - gitweb
LU-6245 server: remove types abstraction from MDS/MGS code
[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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
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
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2015, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/mdt/mdt_xattr.c
37  *
38  * Lustre Metadata Target (mdt) extended attributes management.
39  *
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>
44  */
45
46 #define DEBUG_SUBSYSTEM S_MDS
47
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"
53
54
55 /* return EADATA length to the caller. negative value means error */
56 static int mdt_getxattr_pack_reply(struct mdt_thread_info * info)
57 {
58         struct req_capsule     *pill = info->mti_pill ;
59         struct ptlrpc_request  *req = mdt_info_req(info);
60         char                   *xattr_name;
61         __u64                   valid;
62         static const char       user_string[] = "user.";
63         int                     size, rc;
64         ENTRY;
65
66         valid = info->mti_body->mbo_valid & (OBD_MD_FLXATTR | OBD_MD_FLXATTRLS);
67
68         /* Determine how many bytes we need */
69         if (valid == OBD_MD_FLXATTR) {
70                 xattr_name = req_capsule_client_get(pill, &RMF_NAME);
71                 if (!xattr_name)
72                         RETURN(-EFAULT);
73
74                 if (!(exp_connect_flags(req->rq_export) & OBD_CONNECT_XATTR) &&
75                     !strncmp(xattr_name, user_string, sizeof(user_string) - 1))
76                         RETURN(-EOPNOTSUPP);
77
78                 size = mo_xattr_get(info->mti_env,
79                                     mdt_object_child(info->mti_object),
80                                     &LU_BUF_NULL, xattr_name);
81         } else if (valid == OBD_MD_FLXATTRLS) {
82                 size = mo_xattr_list(info->mti_env,
83                                      mdt_object_child(info->mti_object),
84                                      &LU_BUF_NULL);
85         } else if (valid == OBD_MD_FLXATTRALL) {
86                 /* N.B. eadatasize = 0 is not valid for FLXATTRALL */
87                 /* We could calculate accurate sizes, but this would
88                  * introduce a lot of overhead, let's do it later... */
89                 size = info->mti_body->mbo_eadatasize;
90                 req_capsule_set_size(pill, &RMF_EAVALS, RCL_SERVER, size);
91                 req_capsule_set_size(pill, &RMF_EAVALS_LENS, RCL_SERVER, size);
92         } else {
93                 CDEBUG(D_INFO, "Valid bits: %#llx\n",
94                        info->mti_body->mbo_valid);
95                 RETURN(-EINVAL);
96         }
97
98         if (size == -ENODATA) {
99                 size = 0;
100         } else if (size < 0) {
101                 if (size != -EOPNOTSUPP)
102                         CERROR("Error geting EA size: %d\n", size);
103                 RETURN(size);
104         }
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;
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                 /* currently lustre limit acl access size */
311                 if (xattr_len > LUSTRE_POSIX_ACL_MAX_SIZE)
312                         GOTO(out, rc = -ERANGE);
313
314                 nodemap = nodemap_get_from_exp(exp);
315                 if (IS_ERR(nodemap))
316                         GOTO(out, rc = PTR_ERR(nodemap));
317
318                 rc = nodemap_map_acl(nodemap, rr->rr_eadata, xattr_len,
319                                      NODEMAP_CLIENT_TO_FS);
320                 nodemap_putref(nodemap);
321                 if (rc < 0)
322                         GOTO(out, rc);
323
324                 /* ACLs were mapped out, return an error so the user knows */
325                 if (rc != xattr_len)
326                         GOTO(out, rc = -EPERM);
327         }
328
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 */
341         else
342                 lockpart |= MDS_INODELOCK_XATTR;
343
344         lh = &info->mti_lh[MDT_LH_PARENT];
345         /* ACLs were sent to clients under LCK_CR locks, so taking LCK_EX
346          * to cancel them. */
347         mdt_lock_reg_init(lh, LCK_EX);
348         obj = mdt_object_find_lock(info, rr->rr_fid1, lh, lockpart);
349         if (IS_ERR(obj))
350                 GOTO(out, rc = PTR_ERR(obj));
351
352         tgt_vbr_obj_set(env, mdt_obj2dt(obj));
353         rc = mdt_version_get_check_save(info, obj, 0);
354         if (rc)
355                 GOTO(out_unlock, rc);
356
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 %llu]\n",
362                       mdt_obd_name(info->mti_mdt), xattr_name,
363                       PFID(rr->rr_fid1), valid);
364                 attr->la_ctime = cfs_time_current_sec();
365         }
366         attr->la_valid = LA_CTIME;
367         child = mdt_object_child(obj);
368         if (valid & OBD_MD_FLXATTR) {
369                 int     flags = 0;
370
371                 if (attr->la_flags & XATTR_REPLACE)
372                         flags |= LU_XATTR_REPLACE;
373
374                 if (attr->la_flags & XATTR_CREATE)
375                         flags |= LU_XATTR_CREATE;
376
377                 mdt_fail_write(env, info->mti_mdt->mdt_bottom,
378                                OBD_FAIL_MDS_SETXATTR_WRITE);
379
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 */
384                 if (rc == 0) {
385                         ma->ma_attr_flags |= MDS_PERM_BYPASS;
386                         mo_attr_set(env, child, ma);
387                 }
388         } else if (valid & OBD_MD_FLXATTRRM) {
389                 rc = mo_xattr_del(env, child, xattr_name);
390                 /* update ctime after xattr changed */
391                 if (rc == 0) {
392                         ma->ma_attr_flags |= MDS_PERM_BYPASS;
393                         mo_attr_set(env, child, ma);
394                 }
395         } else {
396                 CDEBUG(D_INFO, "valid bits: %#llx\n", valid);
397                 rc = -EINVAL;
398         }
399         if (rc == 0)
400                 mdt_counter_incr(req, LPROC_MDT_SETXATTR);
401
402         EXIT;
403 out_unlock:
404         mdt_object_unlock_put(info, obj, lh, rc);
405 out:
406         mdt_exit_ucred(info);
407         return rc;
408 }