Whamcloud - gitweb
4e2276e130b1c7bceeb7ae8f3353f75f86f8a798
[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, 2013, 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 <lustre_acl.h>
49 #include "mdt_internal.h"
50
51
52 /* return EADATA length to the caller. negative value means error */
53 static int mdt_getxattr_pack_reply(struct mdt_thread_info * info)
54 {
55         struct req_capsule     *pill = info->mti_pill ;
56         struct ptlrpc_request  *req = mdt_info_req(info);
57         char                   *xattr_name;
58         __u64                   valid = info->mti_body->valid;
59         static const char       user_string[] = "user.";
60         int                     size, rc;
61         ENTRY;
62
63         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETXATTR_PACK))
64                 RETURN(-ENOMEM);
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         } else if (valid & OBD_MD_FLXATTRLS) {
80                 size = mo_xattr_list(info->mti_env,
81                                      mdt_object_child(info->mti_object),
82                                      &LU_BUF_NULL);
83         } else {
84                 CDEBUG(D_INFO, "Valid bits: "LPX64"\n", info->mti_body->valid);
85                 RETURN(-EINVAL);
86         }
87
88         if (size == -ENODATA) {
89                 size = 0;
90         } else if (size < 0) {
91                 CERROR("Error geting EA size: %d\n", size);
92                 RETURN(size);
93         }
94
95         if (info->mti_body->eadatasize != 0 &&
96             info->mti_body->eadatasize < size)
97                 RETURN(-ERANGE);
98
99         req_capsule_set_size(pill, &RMF_EADATA, RCL_SERVER,
100                              info->mti_body->eadatasize == 0 ? 0 : size);
101         rc = req_capsule_server_pack(pill);
102         if (rc) {
103                 LASSERT(rc < 0);
104                 RETURN(rc);
105         }
106
107         RETURN(size);
108 }
109
110 int mdt_getxattr(struct mdt_thread_info *info)
111 {
112         struct ptlrpc_request  *req = mdt_info_req(info);
113         struct mdt_export_data *med = mdt_req2med(req);
114         struct lu_ucred        *uc  = mdt_ucred(info);
115         struct mdt_body        *reqbody;
116         struct mdt_body        *repbody = NULL;
117         struct md_object       *next;
118         struct lu_buf          *buf;
119         __u32                   remote = exp_connect_rmtclient(info->mti_exp);
120         __u32                   perm;
121         int                     easize, rc;
122         ENTRY;
123
124         LASSERT(info->mti_object != NULL);
125         LASSERT(lu_object_assert_exists(&info->mti_object->mot_obj.mo_lu));
126
127         CDEBUG(D_INODE, "getxattr "DFID"\n", PFID(&info->mti_body->fid1));
128
129         reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
130         if (reqbody == NULL)
131                 RETURN(err_serious(-EFAULT));
132
133         rc = mdt_init_ucred(info, reqbody);
134         if (rc)
135                 RETURN(err_serious(rc));
136
137         next = mdt_object_child(info->mti_object);
138
139         if (info->mti_body->valid & OBD_MD_FLRMTRGETFACL) {
140                 if (unlikely(!remote))
141                         GOTO(out, rc = err_serious(-EINVAL));
142
143                 perm = mdt_identity_get_perm(uc->uc_identity, remote,
144                                              req->rq_peer.nid);
145                 if (!(perm & CFS_RMTACL_PERM))
146                         GOTO(out, rc = err_serious(-EPERM));
147
148                 rc = mo_permission(info->mti_env, NULL, next, NULL,
149                                    MAY_RGETFACL);
150                 if (rc)
151                         GOTO(out, rc = err_serious(rc));
152         }
153
154         easize = mdt_getxattr_pack_reply(info);
155         if (easize < 0)
156                 GOTO(out, rc = err_serious(easize));
157
158         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
159         LASSERT(repbody != NULL);
160
161         /* No need further getxattr. */
162         if (easize == 0 || reqbody->eadatasize == 0)
163                 GOTO(out, rc = easize);
164
165
166         buf = &info->mti_buf;
167         buf->lb_buf = req_capsule_server_get(info->mti_pill, &RMF_EADATA);
168         buf->lb_len = easize;
169
170         if (info->mti_body->valid & OBD_MD_FLXATTR) {
171                 int flags = CFS_IC_NOTHING;
172                 char *xattr_name = req_capsule_client_get(info->mti_pill,
173                                                           &RMF_NAME);
174                 CDEBUG(D_INODE, "getxattr %s\n", xattr_name);
175
176                 rc = mo_xattr_get(info->mti_env, next, buf, xattr_name);
177                 if (rc < 0) {
178                         CERROR("getxattr failed: %d\n", rc);
179                         GOTO(out, rc);
180                 }
181
182                 if (info->mti_body->valid &
183                     (OBD_MD_FLRMTLSETFACL | OBD_MD_FLRMTLGETFACL))
184                         flags = CFS_IC_ALL;
185                 else if (info->mti_body->valid & OBD_MD_FLRMTRGETFACL)
186                         flags = CFS_IC_MAPPED;
187
188                 if (rc > 0 && flags != CFS_IC_NOTHING) {
189                         int rc1;
190
191                         if (unlikely(!remote))
192                                 GOTO(out, rc = -EINVAL);
193
194                         rc1 = lustre_posix_acl_xattr_id2client(uc,
195                                         med->med_idmap,
196                                         (posix_acl_xattr_header *)(buf->lb_buf),
197                                         rc, flags);
198                         if (unlikely(rc1 < 0))
199                                 rc = rc1;
200                 }
201         } else if (info->mti_body->valid & OBD_MD_FLXATTRLS) {
202                 CDEBUG(D_INODE, "listxattr\n");
203
204                 rc = mo_xattr_list(info->mti_env, next, buf);
205                 if (rc < 0)
206                         CDEBUG(D_INFO, "listxattr failed: %d\n", rc);
207         } else
208                 LBUG();
209
210         EXIT;
211 out:
212         if (rc >= 0) {
213                 mdt_counter_incr(req, LPROC_MDT_GETXATTR);
214                 repbody->eadatasize = rc;
215                 rc = 0;
216         }
217         mdt_exit_ucred(info);
218         return rc;
219 }
220
221 static int mdt_rmtlsetfacl(struct mdt_thread_info *info,
222                            struct md_object *next,
223                            const char *xattr_name,
224                            ext_acl_xattr_header *header,
225                            posix_acl_xattr_header **out)
226 {
227         struct ptlrpc_request  *req = mdt_info_req(info);
228         struct mdt_export_data *med = mdt_req2med(req);
229         struct lu_ucred        *uc = mdt_ucred(info);
230         struct lu_buf          *buf = &info->mti_buf;
231         int                     rc;
232         ENTRY;
233
234         rc = lustre_ext_acl_xattr_id2server(uc, med->med_idmap, header);
235         if (rc)
236                 RETURN(rc);
237
238         rc = mo_xattr_get(info->mti_env, next, &LU_BUF_NULL, xattr_name);
239         if (rc == -ENODATA)
240                 rc = 0;
241         else if (rc < 0)
242                 RETURN(rc);
243
244         buf->lb_len = rc;
245         if (buf->lb_len > 0) {
246                 OBD_ALLOC_LARGE(buf->lb_buf, buf->lb_len);
247                 if (unlikely(buf->lb_buf == NULL))
248                         RETURN(-ENOMEM);
249
250                 rc = mo_xattr_get(info->mti_env, next, buf, xattr_name);
251                 if (rc < 0) {
252                         CERROR("getxattr failed: %d\n", rc);
253                         GOTO(_out, rc);
254                 }
255         } else
256                 buf->lb_buf = NULL;
257
258         rc = lustre_acl_xattr_merge2posix((posix_acl_xattr_header *)(buf->lb_buf),
259                                           buf->lb_len, header, out);
260         EXIT;
261
262 _out:
263         if (rc <= 0 && buf->lb_buf != NULL)
264                 OBD_FREE_LARGE(buf->lb_buf, buf->lb_len);
265         return rc;
266 }
267
268 int mdt_reint_setxattr(struct mdt_thread_info *info,
269                        struct mdt_lock_handle *unused)
270 {
271         struct ptlrpc_request   *req = mdt_info_req(info);
272         struct lu_ucred         *uc  = mdt_ucred(info);
273         struct mdt_lock_handle  *lh;
274         const struct lu_env     *env  = info->mti_env;
275         struct lu_buf           *buf  = &info->mti_buf;
276         struct mdt_reint_record *rr   = &info->mti_rr;
277         struct md_attr          *ma = &info->mti_attr;
278         struct lu_attr          *attr = &info->mti_attr.ma_attr;
279         struct mdt_object       *obj;
280         struct md_object        *child;
281         __u64                    valid = attr->la_valid;
282         const char              *xattr_name = rr->rr_name;
283         int                      xattr_len = rr->rr_eadatalen;
284         __u64                    lockpart;
285         int                      rc;
286         posix_acl_xattr_header  *new_xattr = NULL;
287         __u32                    remote = exp_connect_rmtclient(info->mti_exp);
288         __u32                    perm;
289         ENTRY;
290
291         CDEBUG(D_INODE, "setxattr for "DFID"\n", PFID(rr->rr_fid1));
292
293         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SETXATTR))
294                 RETURN(err_serious(-ENOMEM));
295
296         CDEBUG(D_INODE, "%s xattr %s\n",
297                valid & OBD_MD_FLXATTR ? "set" : "remove", xattr_name);
298
299         rc = mdt_init_ucred_reint(info);
300         if (rc != 0)
301                 RETURN(rc);
302
303         if (valid & OBD_MD_FLRMTRSETFACL) {
304                 if (unlikely(!remote))
305                         GOTO(out, rc = err_serious(-EINVAL));
306
307                 perm = mdt_identity_get_perm(uc->uc_identity, remote,
308                                              req->rq_peer.nid);
309                 if (!(perm & CFS_RMTACL_PERM))
310                         GOTO(out, rc = err_serious(-EPERM));
311         }
312
313         if (strncmp(xattr_name, XATTR_USER_PREFIX,
314                     sizeof(XATTR_USER_PREFIX) - 1) == 0) {
315                 if (!(exp_connect_flags(req->rq_export) & OBD_CONNECT_XATTR))
316                         GOTO(out, rc = -EOPNOTSUPP);
317                 if (strcmp(xattr_name, XATTR_NAME_LOV) == 0)
318                         GOTO(out, rc = -EACCES);
319                 if (strcmp(xattr_name, XATTR_NAME_LMA) == 0)
320                         GOTO(out, rc = 0);
321                 if (strcmp(xattr_name, XATTR_NAME_LINK) == 0)
322                         GOTO(out, rc = 0);
323         } else if ((valid & OBD_MD_FLXATTR) &&
324                    (strncmp(xattr_name, XATTR_NAME_ACL_ACCESS,
325                             sizeof(XATTR_NAME_ACL_ACCESS) - 1) == 0 ||
326                     strncmp(xattr_name, XATTR_NAME_ACL_DEFAULT,
327                             sizeof(XATTR_NAME_ACL_DEFAULT) - 1) == 0)) {
328                 /* currently lustre limit acl access size */
329                 if (xattr_len > LUSTRE_POSIX_ACL_MAX_SIZE)
330                         GOTO(out, -ERANGE);
331         }
332
333         lockpart = MDS_INODELOCK_UPDATE;
334         /* Revoke all clients' lookup lock, since the access
335          * permissions for this inode is changed when ACL_ACCESS is
336          * set. This isn't needed for ACL_DEFAULT, since that does
337          * not change the access permissions of this inode, nor any
338          * other existing inodes. It is setting the ACLs inherited
339          * by new directories/files at create time. */
340         /* We need revoke both LOOKUP|PERM lock here, see mdt_attr_set. */
341         if (!strcmp(xattr_name, XATTR_NAME_ACL_ACCESS))
342                 lockpart |= MDS_INODELOCK_PERM | MDS_INODELOCK_LOOKUP;
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         info->mti_mos = 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 "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();
365         }
366         attr->la_valid = LA_CTIME;
367         child = mdt_object_child(obj);
368         if (valid & OBD_MD_FLXATTR) {
369                 char *xattr = (void *)rr->rr_eadata;
370
371                 if (xattr_len > 0) {
372                         int flags = 0;
373
374                         if (valid & OBD_MD_FLRMTLSETFACL) {
375                                 if (unlikely(!remote))
376                                         GOTO(out_unlock, rc = -EINVAL);
377
378                                 xattr_len = mdt_rmtlsetfacl(info, child,
379                                                 xattr_name,
380                                                 (ext_acl_xattr_header *)xattr,
381                                                 &new_xattr);
382                                 if (xattr_len < 0)
383                                         GOTO(out_unlock, rc = xattr_len);
384
385                                 xattr = (char *)new_xattr;
386                         }
387
388                         if (attr->la_flags & XATTR_REPLACE)
389                                 flags |= LU_XATTR_REPLACE;
390
391                         if (attr->la_flags & XATTR_CREATE)
392                                 flags |= LU_XATTR_CREATE;
393
394                         mdt_fail_write(env, info->mti_mdt->mdt_bottom,
395                                        OBD_FAIL_MDS_SETXATTR_WRITE);
396
397                         buf->lb_buf = xattr;
398                         buf->lb_len = xattr_len;
399                         rc = mo_xattr_set(env, child, buf, xattr_name, flags);
400                         /* update ctime after xattr changed */
401                         if (rc == 0) {
402                                 ma->ma_attr_flags |= MDS_PERM_BYPASS;
403                                 mo_attr_set(env, child, ma);
404                         }
405                 }
406         } else if (valid & OBD_MD_FLXATTRRM) {
407                 rc = mo_xattr_del(env, child, xattr_name);
408                 /* update ctime after xattr changed */
409                 if (rc == 0) {
410                         ma->ma_attr_flags |= MDS_PERM_BYPASS;
411                         mo_attr_set(env, child, ma);
412                 }
413         } else {
414                 CDEBUG(D_INFO, "valid bits: "LPX64"\n", valid);
415                 rc = -EINVAL;
416         }
417         if (rc == 0)
418                 mdt_counter_incr(req, LPROC_MDT_SETXATTR);
419
420         EXIT;
421 out_unlock:
422         mdt_object_unlock_put(info, obj, lh, rc);
423         if (unlikely(new_xattr != NULL))
424                 lustre_posix_acl_xattr_free(new_xattr, xattr_len);
425 out:
426         mdt_exit_ucred(info);
427         return rc;
428 }