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