Whamcloud - gitweb
LU-1303 lod: introduce lod device
[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 #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 (!(req->rq_export->exp_connect_flags & 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 md_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->mu_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 md_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 md_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->mu_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 (!(req->rq_export->exp_connect_flags & 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         if (!strcmp(xattr_name, XATTR_NAME_ACL_ACCESS))
341                 lockpart |= MDS_INODELOCK_LOOKUP;
342
343         lh = &info->mti_lh[MDT_LH_PARENT];
344         /* ACLs were sent to clients under LCK_CR locks, so taking LCK_EX
345          * to cancel them. */
346         mdt_lock_reg_init(lh, LCK_EX);
347         obj = mdt_object_find_lock(info, rr->rr_fid1, lh, lockpart);
348         if (IS_ERR(obj))
349                 GOTO(out, rc =  PTR_ERR(obj));
350
351         info->mti_mos = obj;
352         rc = mdt_version_get_check_save(info, obj, 0);
353         if (rc)
354                 GOTO(out_unlock, rc);
355
356         if (unlikely(!(valid & OBD_MD_FLCTIME))) {
357                 /* This isn't strictly an error, but all current clients
358                  * should set OBD_MD_FLCTIME when setting attributes. */
359                 CWARN("%s: client miss to set OBD_MD_FLCTIME when "
360                       "setxattr %s: [object "DFID"] [valid "LPU64"]\n",
361                       info->mti_exp->exp_obd->obd_name, xattr_name,
362                       PFID(rr->rr_fid1), valid);
363                 attr->la_ctime = cfs_time_current_sec();
364         }
365         attr->la_valid = LA_CTIME;
366         child = mdt_object_child(obj);
367         if (valid & OBD_MD_FLXATTR) {
368                 char *xattr = (void *)rr->rr_eadata;
369
370                 if (xattr_len > 0) {
371                         int flags = 0;
372
373                         if (valid & OBD_MD_FLRMTLSETFACL) {
374                                 if (unlikely(!remote))
375                                         GOTO(out_unlock, rc = -EINVAL);
376
377                                 xattr_len = mdt_rmtlsetfacl(info, child,
378                                                 xattr_name,
379                                                 (ext_acl_xattr_header *)xattr,
380                                                 &new_xattr);
381                                 if (xattr_len < 0)
382                                         GOTO(out_unlock, rc = xattr_len);
383
384                                 xattr = (char *)new_xattr;
385                         }
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 = xattr;
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                 }
405         } else if (valid & OBD_MD_FLXATTRRM) {
406                 rc = mo_xattr_del(env, child, xattr_name);
407                 /* update ctime after xattr changed */
408                 if (rc == 0) {
409                         ma->ma_attr_flags |= MDS_PERM_BYPASS;
410                         mo_attr_set(env, child, ma);
411                 }
412         } else {
413                 CDEBUG(D_INFO, "valid bits: "LPX64"\n", valid);
414                 rc = -EINVAL;
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         if (unlikely(new_xattr != NULL))
423                 lustre_posix_acl_xattr_free(new_xattr, xattr_len);
424 out:
425         mdt_exit_ucred(info);
426         return rc;
427 }