Whamcloud - gitweb
LU-1146 build: batch update copyright messages
[fs/lustre-release.git] / lustre / mdt / mdt_xattr.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
31  *
32  * Copyright (c) 2011, 2012, Whamcloud, Inc.
33  */
34 /*
35  * This file is part of Lustre, http://www.lustre.org/
36  * Lustre is a trademark of Sun Microsystems, Inc.
37  *
38  * lustre/mdt/mdt_xattr.c
39  *
40  * Lustre Metadata Target (mdt) extended attributes management.
41  *
42  * Author: Peter Braam <braam@clusterfs.com>
43  * Author: Andreas Dilger <adilger@clusterfs.com>
44  * Author: Phil Schwan <phil@clusterfs.com>
45  * Author: Huang Hua <huanghua@clusterfs.com>
46  */
47
48 #ifndef EXPORT_SYMTAB
49 # define EXPORT_SYMTAB
50 #endif
51 #define DEBUG_SUBSYSTEM S_MDS
52
53 #include <lustre_acl.h>
54 #include "mdt_internal.h"
55
56
57 /* return EADATA length to the caller. negative value means error */
58 static int mdt_getxattr_pack_reply(struct mdt_thread_info * info)
59 {
60         struct req_capsule     *pill = info->mti_pill ;
61         struct ptlrpc_request  *req = mdt_info_req(info);
62         char                   *xattr_name;
63         __u64                   valid = info->mti_body->valid;
64         static const char       user_string[] = "user.";
65         int                     size, rc;
66         ENTRY;
67
68         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETXATTR_PACK))
69                 RETURN(-ENOMEM);
70
71         /* Determine how many bytes we need */
72         if (valid & OBD_MD_FLXATTR) {
73                 xattr_name = req_capsule_client_get(pill, &RMF_NAME);
74                 if (!xattr_name)
75                         RETURN(-EFAULT);
76
77                 if (!(req->rq_export->exp_connect_flags & OBD_CONNECT_XATTR) &&
78                     !strncmp(xattr_name, user_string, sizeof(user_string) - 1))
79                         RETURN(-EOPNOTSUPP);
80
81                 size = mo_xattr_get(info->mti_env,
82                                     mdt_object_child(info->mti_object),
83                                     &LU_BUF_NULL, xattr_name);
84         } else if (valid & OBD_MD_FLXATTRLS) {
85                 size = mo_xattr_list(info->mti_env,
86                                      mdt_object_child(info->mti_object),
87                                      &LU_BUF_NULL);
88         } else {
89                 CDEBUG(D_INFO, "Valid bits: "LPX64"\n", info->mti_body->valid);
90                 RETURN(-EINVAL);
91         }
92
93         if (size < 0) {
94                 if (size == -ENODATA)
95                         size = 0;
96                 else if (size != -EOPNOTSUPP) {
97                         CDEBUG(D_INFO, "Error geting EA size: %d\n", size);
98                         RETURN(size);
99                 }
100         }
101
102         if (info->mti_body->eadatasize != 0 &&
103             info->mti_body->eadatasize < size)
104                 RETURN(-ERANGE);
105
106         req_capsule_set_size(pill, &RMF_EADATA, RCL_SERVER,
107                              min_t(int, size, info->mti_body->eadatasize));
108
109         rc = req_capsule_server_pack(pill);
110         if (rc) {
111                 LASSERT(rc < 0);
112                 RETURN(rc);
113         }
114
115         RETURN(size);
116 }
117
118 int mdt_getxattr(struct mdt_thread_info *info)
119 {
120         struct ptlrpc_request  *req = mdt_info_req(info);
121         struct mdt_export_data *med = mdt_req2med(req);
122         struct md_ucred        *uc  = mdt_ucred(info);
123         struct mdt_body        *reqbody;
124         struct mdt_body        *repbody = NULL;
125         struct md_object       *next;
126         struct lu_buf          *buf;
127         __u32                   remote = exp_connect_rmtclient(info->mti_exp);
128         __u32                   perm;
129         int                     easize, rc;
130         ENTRY;
131
132         LASSERT(info->mti_object != NULL);
133         LASSERT(lu_object_assert_exists(&info->mti_object->mot_obj.mo_lu));
134
135         CDEBUG(D_INODE, "getxattr "DFID"\n", PFID(&info->mti_body->fid1));
136
137         reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
138         if (reqbody == NULL)
139                 RETURN(err_serious(-EFAULT));
140
141         rc = mdt_init_ucred(info, reqbody);
142         if (rc)
143                 RETURN(err_serious(rc));
144
145         next = mdt_object_child(info->mti_object);
146
147         if (info->mti_body->valid & OBD_MD_FLRMTRGETFACL) {
148                 if (unlikely(!remote))
149                         GOTO(out, rc = err_serious(-EINVAL));
150
151                 perm = mdt_identity_get_perm(uc->mu_identity, remote,
152                                              req->rq_peer.nid);
153                 if (!(perm & CFS_RMTACL_PERM))
154                         GOTO(out, rc = err_serious(-EPERM));
155
156                 rc = mo_permission(info->mti_env, NULL, next, NULL,
157                                    MAY_RGETFACL);
158                 if (rc)
159                         GOTO(out, rc = err_serious(rc));
160         }
161
162         easize = mdt_getxattr_pack_reply(info);
163         if (easize < 0)
164                 GOTO(out, rc = err_serious(easize));
165
166         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
167         LASSERT(repbody != NULL);
168
169         /* No need further getxattr. */
170         if (easize == 0 || reqbody->eadatasize == 0)
171                 GOTO(out, rc = easize);
172
173         buf = &info->mti_buf;
174         buf->lb_buf = req_capsule_server_get(info->mti_pill, &RMF_EADATA);
175         buf->lb_len = easize;
176
177         if (info->mti_body->valid & OBD_MD_FLXATTR) {
178                 int flags = CFS_IC_NOTHING;
179                 char *xattr_name = req_capsule_client_get(info->mti_pill,
180                                                           &RMF_NAME);
181                 CDEBUG(D_INODE, "getxattr %s\n", xattr_name);
182
183                 rc = mo_xattr_get(info->mti_env, next, buf, xattr_name);
184                 if (rc < 0) {
185                         CERROR("getxattr failed: %d\n", rc);
186                         GOTO(out, rc);
187                 }
188
189                 if (info->mti_body->valid &
190                     (OBD_MD_FLRMTLSETFACL | OBD_MD_FLRMTLGETFACL))
191                         flags = CFS_IC_ALL;
192                 else if (info->mti_body->valid & OBD_MD_FLRMTRGETFACL)
193                         flags = CFS_IC_MAPPED;
194
195                 if (rc > 0 && flags != CFS_IC_NOTHING) {
196                         int rc1;
197
198                         if (unlikely(!remote))
199                                 GOTO(out, rc = -EINVAL);
200
201                         rc1 = lustre_posix_acl_xattr_id2client(uc,
202                                         med->med_idmap,
203                                         (posix_acl_xattr_header *)(buf->lb_buf),
204                                         rc, flags);
205                         if (unlikely(rc1 < 0))
206                                 rc = rc1;
207                 }
208         } else if (info->mti_body->valid & OBD_MD_FLXATTRLS) {
209                 CDEBUG(D_INODE, "listxattr\n");
210
211                 rc = mo_xattr_list(info->mti_env, next, buf);
212                 if (rc < 0)
213                         CDEBUG(D_INFO, "listxattr failed: %d\n", rc);
214         } else
215                 LBUG();
216
217         EXIT;
218 out:
219         if (rc >= 0) {
220                 mdt_counter_incr(req->rq_export, LPROC_MDT_GETXATTR);
221                 repbody->eadatasize = rc;
222                 rc = 0;
223         }
224         mdt_exit_ucred(info);
225         return rc;
226 }
227
228 static int mdt_rmtlsetfacl(struct mdt_thread_info *info,
229                            struct md_object *next,
230                            const char *xattr_name,
231                            ext_acl_xattr_header *header,
232                            posix_acl_xattr_header **out)
233 {
234         struct ptlrpc_request  *req = mdt_info_req(info);
235         struct mdt_export_data *med = mdt_req2med(req);
236         struct md_ucred        *uc = mdt_ucred(info);
237         struct lu_buf          *buf = &info->mti_buf;
238         int                     rc;
239         ENTRY;
240
241         rc = lustre_ext_acl_xattr_id2server(uc, med->med_idmap, header);
242         if (rc)
243                 RETURN(rc);
244
245         rc = mo_xattr_get(info->mti_env, next, &LU_BUF_NULL, xattr_name);
246         if (rc == -ENODATA)
247                 rc = 0;
248         else if (rc < 0)
249                 RETURN(rc);
250
251         buf->lb_len = rc;
252         if (buf->lb_len > 0) {
253                 OBD_ALLOC_LARGE(buf->lb_buf, buf->lb_len);
254                 if (unlikely(buf->lb_buf == NULL))
255                         RETURN(-ENOMEM);
256
257                 rc = mo_xattr_get(info->mti_env, next, buf, xattr_name);
258                 if (rc < 0) {
259                         CERROR("getxattr failed: %d\n", rc);
260                         GOTO(_out, rc);
261                 }
262         } else
263                 buf->lb_buf = NULL;
264
265         rc = lustre_acl_xattr_merge2posix((posix_acl_xattr_header *)(buf->lb_buf),
266                                           buf->lb_len, header, out);
267         EXIT;
268
269 _out:
270         if (rc <= 0 && buf->lb_buf != NULL)
271                 OBD_FREE_LARGE(buf->lb_buf, buf->lb_len);
272         return rc;
273 }
274
275 int mdt_reint_setxattr(struct mdt_thread_info *info,
276                        struct mdt_lock_handle *unused)
277 {
278         struct ptlrpc_request   *req = mdt_info_req(info);
279         struct md_ucred         *uc  = mdt_ucred(info);
280         struct mdt_lock_handle  *lh;
281         struct req_capsule      *pill = info->mti_pill;
282         const struct lu_env     *env  = info->mti_env;
283         struct lu_buf           *buf  = &info->mti_buf;
284         struct mdt_reint_record *rr   = &info->mti_rr;
285         struct md_attr          *ma = &info->mti_attr;
286         struct lu_attr          *attr = &info->mti_attr.ma_attr;
287         struct mdt_object       *obj;
288         struct md_object        *child;
289         __u64                    valid = attr->la_valid;
290         const char              *xattr_name;
291         int                      xattr_len = 0;
292         __u64                    lockpart;
293         int                      rc;
294         posix_acl_xattr_header  *new_xattr = NULL;
295         __u32                    remote = exp_connect_rmtclient(info->mti_exp);
296         __u32                    perm;
297         ENTRY;
298
299         CDEBUG(D_INODE, "setxattr for "DFID"\n", PFID(rr->rr_fid1));
300
301         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SETXATTR))
302                 RETURN(err_serious(-ENOMEM));
303
304         xattr_name = rr->rr_name;
305
306         CDEBUG(D_INODE, "%s xattr %s\n",
307                valid & OBD_MD_FLXATTR ? "set" : "remove", xattr_name);
308
309         rc = mdt_init_ucred_reint(info);
310         if (rc != 0)
311                 RETURN(rc);
312
313         if (valid & OBD_MD_FLRMTRSETFACL) {
314                 if (unlikely(!remote))
315                         GOTO(out, rc = err_serious(-EINVAL));
316
317                 perm = mdt_identity_get_perm(uc->mu_identity, remote,
318                                              req->rq_peer.nid);
319                 if (!(perm & CFS_RMTACL_PERM))
320                         GOTO(out, rc = err_serious(-EPERM));
321         }
322
323         /* various sanity check for xattr name */
324         xattr_name = req_capsule_client_get(pill, &RMF_NAME);
325         if (!xattr_name)
326                 GOTO(out, rc = err_serious(-EFAULT));
327
328         if (strncmp(xattr_name, XATTR_USER_PREFIX,
329                     sizeof(XATTR_USER_PREFIX) - 1) == 0) {
330                 if (!(req->rq_export->exp_connect_flags & OBD_CONNECT_XATTR))
331                         GOTO(out, rc = -EOPNOTSUPP);
332                 if (strcmp(xattr_name, XATTR_NAME_LOV) == 0)
333                         GOTO(out, rc = -EACCES);
334                 if (strcmp(xattr_name, XATTR_NAME_LMA) == 0)
335                         GOTO(out, rc = 0);
336                 if (strcmp(xattr_name, XATTR_NAME_LINK) == 0)
337                         GOTO(out, rc = 0);
338         } else if ((valid & OBD_MD_FLXATTR) &&
339                    (strncmp(xattr_name, XATTR_NAME_ACL_ACCESS,
340                             sizeof(XATTR_NAME_ACL_ACCESS) - 1) == 0 ||
341                     strncmp(xattr_name, XATTR_NAME_ACL_DEFAULT,
342                             sizeof(XATTR_NAME_ACL_DEFAULT) - 1) == 0)) {
343                 /* currently lustre limit acl access size */
344                 xattr_len = req_capsule_get_size(pill, &RMF_EADATA, RCL_CLIENT);
345
346                 if (xattr_len > LUSTRE_POSIX_ACL_MAX_SIZE)
347                         GOTO(out, -ERANGE);
348         }
349
350         lockpart = MDS_INODELOCK_UPDATE;
351         /* Revoke all clients' lookup lock, since the access
352          * permissions for this inode is changed when ACL_ACCESS is
353          * set. This isn't needed for ACL_DEFAULT, since that does
354          * not change the access permissions of this inode, nor any
355          * other existing inodes. It is setting the ACLs inherited
356          * by new directories/files at create time. */
357         if (!strcmp(xattr_name, XATTR_NAME_ACL_ACCESS))
358                 lockpart |= MDS_INODELOCK_LOOKUP;
359
360         lh = &info->mti_lh[MDT_LH_PARENT];
361         /* ACLs were sent to clients under LCK_CR locks, so taking LCK_EX
362          * to cancel them. */
363         mdt_lock_reg_init(lh, LCK_EX);
364         obj = mdt_object_find_lock(info, rr->rr_fid1, lh, lockpart);
365         if (IS_ERR(obj))
366                 GOTO(out, rc =  PTR_ERR(obj));
367
368         info->mti_mos = obj;
369         rc = mdt_version_get_check_save(info, obj, 0);
370         if (rc)
371                 GOTO(out_unlock, rc);
372
373         if (unlikely(!(valid & OBD_MD_FLCTIME))) {
374                 /* This isn't strictly an error, but all current clients
375                  * should set OBD_MD_FLCTIME when setting attributes. */
376                 CWARN("%s: client miss to set OBD_MD_FLCTIME when "
377                       "setxattr %s: [object "DFID"] [valid "LPU64"]\n",
378                       info->mti_exp->exp_obd->obd_name, xattr_name,
379                       PFID(rr->rr_fid1), valid);
380                 attr->la_ctime = cfs_time_current_sec();
381         }
382         attr->la_valid = LA_CTIME;
383         child = mdt_object_child(obj);
384         if (valid & OBD_MD_FLXATTR) {
385                 char * xattr;
386
387                 if (!req_capsule_field_present(pill, &RMF_EADATA, RCL_CLIENT)) {
388                         CDEBUG(D_INFO, "no xattr data supplied\n");
389                         GOTO(out_unlock, rc = -EFAULT);
390                 }
391
392                 xattr_len = req_capsule_get_size(pill, &RMF_EADATA, RCL_CLIENT);
393                 if (xattr_len) {
394                         int flags = 0;
395
396                         xattr = req_capsule_client_get(pill, &RMF_EADATA);
397
398                         if (valid & OBD_MD_FLRMTLSETFACL) {
399                                 if (unlikely(!remote))
400                                         GOTO(out_unlock, rc = -EINVAL);
401
402                                 xattr_len = mdt_rmtlsetfacl(info, child,
403                                                 xattr_name,
404                                                 (ext_acl_xattr_header *)xattr,
405                                                 &new_xattr);
406                                 if (xattr_len < 0)
407                                         GOTO(out_unlock, rc = xattr_len);
408
409                                 xattr = (char *)new_xattr;
410                         }
411
412                         if (attr->la_flags & XATTR_REPLACE)
413                                 flags |= LU_XATTR_REPLACE;
414
415                         if (attr->la_flags & XATTR_CREATE)
416                                 flags |= LU_XATTR_CREATE;
417
418                         mdt_fail_write(env, info->mti_mdt->mdt_bottom,
419                                        OBD_FAIL_MDS_SETXATTR_WRITE);
420
421                         buf->lb_buf = xattr;
422                         buf->lb_len = xattr_len;
423                         rc = mo_xattr_set(env, child, buf, xattr_name, flags);
424                         /* update ctime after xattr changed */
425                         if (rc == 0) {
426                                 ma->ma_attr_flags |= MDS_PERM_BYPASS;
427                                 mo_attr_set(env, child, ma);
428                         }
429                 }
430         } else if (valid & OBD_MD_FLXATTRRM) {
431                 rc = mo_xattr_del(env, child, xattr_name);
432                 /* update ctime after xattr changed */
433                 if (rc == 0) {
434                         ma->ma_attr_flags |= MDS_PERM_BYPASS;
435                         mo_attr_set(env, child, ma);
436                 }
437         } else {
438                 CDEBUG(D_INFO, "valid bits: "LPX64"\n", valid);
439                 rc = -EINVAL;
440         }
441         if (rc == 0)
442                 mdt_counter_incr(req->rq_export, LPROC_MDT_SETXATTR);
443
444         EXIT;
445 out_unlock:
446         mdt_object_unlock_put(info, obj, lh, rc);
447         if (unlikely(new_xattr != NULL))
448                 lustre_posix_acl_xattr_free(new_xattr, xattr_len);
449 out:
450         mdt_exit_ucred(info);
451         return rc;
452 }