Whamcloud - gitweb
LU-2869 llite: extended attribute cache
[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;
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         valid = info->mti_body->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->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: "LPX64"\n", info->mti_body->valid);
94                 RETURN(-EINVAL);
95         }
96
97         if (size == -ENODATA) {
98                 size = 0;
99         } else if (size < 0) {
100                 CERROR("Error geting EA size: %d\n", size);
101                 RETURN(size);
102         }
103
104         if (info->mti_body->eadatasize != 0 &&
105             info->mti_body->eadatasize < size)
106                 RETURN(-ERANGE);
107
108         req_capsule_set_size(pill, &RMF_EADATA, RCL_SERVER,
109                              info->mti_body->eadatasize == 0 ? 0 : size);
110         rc = req_capsule_server_pack(pill);
111         if (rc) {
112                 LASSERT(rc < 0);
113                 RETURN(rc);
114         }
115
116         RETURN(size);
117 }
118
119 static int
120 mdt_getxattr_one(struct mdt_thread_info *info,
121                 char *xattr_name, struct md_object *next,
122                 struct lu_buf *buf, struct mdt_export_data *med,
123                 struct lu_ucred *uc)
124 {
125         __u32 remote = exp_connect_rmtclient(info->mti_exp);
126         int flags = CFS_IC_NOTHING, rc;
127
128         ENTRY;
129
130         CDEBUG(D_INODE, "getxattr %s\n", xattr_name);
131
132         rc = mo_xattr_get(info->mti_env, next, buf, xattr_name);
133         if (rc < 0) {
134                 CERROR("getxattr failed: %d\n", rc);
135                 GOTO(out, rc);
136         }
137
138         if (info->mti_body->valid &
139             (OBD_MD_FLRMTLSETFACL | OBD_MD_FLRMTLGETFACL))
140                 flags = CFS_IC_ALL;
141         else if (info->mti_body->valid & OBD_MD_FLRMTRGETFACL)
142                 flags = CFS_IC_MAPPED;
143
144         if (rc > 0 && flags != CFS_IC_NOTHING) {
145                 int rc1;
146
147                 if (unlikely(!remote))
148                         GOTO(out, rc = -EINVAL);
149
150                 rc1 = lustre_posix_acl_xattr_id2client(uc,
151                                 med->med_idmap,
152                                 (posix_acl_xattr_header *)(buf->lb_buf),
153                                 rc, flags);
154                 if (unlikely(rc1 < 0))
155                         rc = rc1;
156         }
157
158 out:
159         return rc;
160 }
161
162 int mdt_getxattr(struct mdt_thread_info *info)
163 {
164         struct ptlrpc_request  *req = mdt_info_req(info);
165         struct mdt_export_data *med = mdt_req2med(req);
166         struct lu_ucred        *uc  = mdt_ucred(info);
167         struct mdt_body        *reqbody;
168         struct mdt_body        *repbody = NULL;
169         struct md_object       *next;
170         struct lu_buf          *buf;
171         __u32                   remote = exp_connect_rmtclient(info->mti_exp);
172         __u32                   perm;
173         int                     easize, rc;
174         obd_valid               valid;
175         ENTRY;
176
177         LASSERT(info->mti_object != NULL);
178         LASSERT(lu_object_assert_exists(&info->mti_object->mot_obj));
179
180         CDEBUG(D_INODE, "getxattr "DFID"\n", PFID(&info->mti_body->fid1));
181
182         reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
183         if (reqbody == NULL)
184                 RETURN(err_serious(-EFAULT));
185
186         rc = mdt_init_ucred(info, reqbody);
187         if (rc)
188                 RETURN(err_serious(rc));
189
190         down_read(&info->mti_object->mot_xattr_sem);
191
192         next = mdt_object_child(info->mti_object);
193
194         if (info->mti_body->valid & OBD_MD_FLRMTRGETFACL) {
195                 if (unlikely(!remote))
196                         GOTO(out, rc = err_serious(-EINVAL));
197
198                 perm = mdt_identity_get_perm(uc->uc_identity, remote,
199                                              req->rq_peer.nid);
200                 if (!(perm & CFS_RMTACL_PERM))
201                         GOTO(out, rc = err_serious(-EPERM));
202
203                 rc = mo_permission(info->mti_env, NULL, next, NULL,
204                                    MAY_RGETFACL);
205                 if (rc)
206                         GOTO(out, rc = err_serious(rc));
207         }
208
209         easize = mdt_getxattr_pack_reply(info);
210         if (easize < 0)
211                 GOTO(out, rc = err_serious(easize));
212
213         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
214         LASSERT(repbody != NULL);
215
216         /* No need further getxattr. */
217         if (easize == 0 || reqbody->eadatasize == 0)
218                 GOTO(out, rc = easize);
219
220         buf = &info->mti_buf;
221         buf->lb_buf = req_capsule_server_get(info->mti_pill, &RMF_EADATA);
222         buf->lb_len = easize;
223
224         valid = info->mti_body->valid & (OBD_MD_FLXATTR | OBD_MD_FLXATTRLS);
225
226         if (valid == OBD_MD_FLXATTR) {
227                 char *xattr_name = req_capsule_client_get(info->mti_pill,
228                                                           &RMF_NAME);
229                 rc = mdt_getxattr_one(info, xattr_name, next, buf, med, uc);
230         } else if (valid == OBD_MD_FLXATTRLS) {
231                 CDEBUG(D_INODE, "listxattr\n");
232
233                 rc = mo_xattr_list(info->mti_env, next, buf);
234                 if (rc < 0)
235                         CDEBUG(D_INFO, "listxattr failed: %d\n", rc);
236         } else if (valid == OBD_MD_FLXATTRALL) {
237                 /*
238                  * The format of the pill is the following:
239                  * EADATA:      attr1\0attr2\0...attrn\0
240                  * EAVALS:      val1val2...valn
241                  * EAVALS_LENS: 4,4,...4
242                  */
243                 char *v, *b;
244                 __u32 *sizes;
245                 int eadatasize, eavallen = 0, eavallens = 0;
246                 struct lu_buf buf2 = { .lb_len = reqbody->eadatasize };
247
248                 /* Fill out EADATA */
249                 eadatasize = mo_xattr_list(info->mti_env, next, buf);
250                 if (eadatasize < 0)
251                         GOTO(out, rc = eadatasize);
252
253                 v = req_capsule_server_get(info->mti_pill, &RMF_EAVALS);
254                 sizes = req_capsule_server_get(info->mti_pill,
255                                                 &RMF_EAVALS_LENS);
256
257                 /* Fill out EAVALS and EAVALS_LENS */
258                 for (b = buf->lb_buf;
259                      b < (char *)buf->lb_buf + eadatasize;
260                      b += strlen(b) + 1, v += rc) {
261                         buf2.lb_buf = v;
262                         rc = mdt_getxattr_one(info, b, next, &buf2, med, uc);
263                         if (rc < 0)
264                                 GOTO(out, rc);
265                         sizes[eavallens] = rc;
266                         buf2.lb_len -= rc;
267                         eavallens++;
268                         eavallen += rc;
269                 }
270
271                 repbody->aclsize = eavallen;
272                 repbody->max_mdsize = eavallens;
273
274                 req_capsule_shrink(info->mti_pill, &RMF_EAVALS,
275                                         eavallen, RCL_SERVER);
276                 req_capsule_shrink(info->mti_pill, &RMF_EAVALS_LENS,
277                                         eavallens * sizeof(__u32), RCL_SERVER);
278                 req_capsule_shrink(info->mti_pill, &RMF_EADATA,
279                                         eadatasize, RCL_SERVER);
280                 rc = eadatasize;
281         } else
282                 LBUG();
283
284         EXIT;
285 out:
286         up_read(&info->mti_object->mot_xattr_sem);
287
288         if (rc >= 0) {
289                 mdt_counter_incr(req, LPROC_MDT_GETXATTR);
290                 repbody->eadatasize = rc;
291                 rc = 0;
292         }
293         mdt_exit_ucred(info);
294         return rc;
295 }
296
297 static int mdt_rmtlsetfacl(struct mdt_thread_info *info,
298                            struct md_object *next,
299                            const char *xattr_name,
300                            ext_acl_xattr_header *header,
301                            posix_acl_xattr_header **out)
302 {
303         struct ptlrpc_request  *req = mdt_info_req(info);
304         struct mdt_export_data *med = mdt_req2med(req);
305         struct lu_ucred        *uc = mdt_ucred(info);
306         struct lu_buf          *buf = &info->mti_buf;
307         int                     rc;
308         ENTRY;
309
310         rc = lustre_ext_acl_xattr_id2server(uc, med->med_idmap, header);
311         if (rc)
312                 RETURN(rc);
313
314         rc = mo_xattr_get(info->mti_env, next, &LU_BUF_NULL, xattr_name);
315         if (rc == -ENODATA)
316                 rc = 0;
317         else if (rc < 0)
318                 RETURN(rc);
319
320         buf->lb_len = rc;
321         if (buf->lb_len > 0) {
322                 OBD_ALLOC_LARGE(buf->lb_buf, buf->lb_len);
323                 if (unlikely(buf->lb_buf == NULL))
324                         RETURN(-ENOMEM);
325
326                 rc = mo_xattr_get(info->mti_env, next, buf, xattr_name);
327                 if (rc < 0) {
328                         CERROR("getxattr failed: %d\n", rc);
329                         GOTO(_out, rc);
330                 }
331         } else
332                 buf->lb_buf = NULL;
333
334         rc = lustre_acl_xattr_merge2posix((posix_acl_xattr_header *)(buf->lb_buf),
335                                           buf->lb_len, header, out);
336         EXIT;
337
338 _out:
339         if (rc <= 0 && buf->lb_buf != NULL)
340                 OBD_FREE_LARGE(buf->lb_buf, buf->lb_len);
341         return rc;
342 }
343
344 int mdt_reint_setxattr(struct mdt_thread_info *info,
345                        struct mdt_lock_handle *unused)
346 {
347         struct ptlrpc_request   *req = mdt_info_req(info);
348         struct lu_ucred         *uc  = mdt_ucred(info);
349         struct mdt_lock_handle  *lh;
350         const struct lu_env     *env  = info->mti_env;
351         struct lu_buf           *buf  = &info->mti_buf;
352         struct mdt_reint_record *rr   = &info->mti_rr;
353         struct md_attr          *ma = &info->mti_attr;
354         struct lu_attr          *attr = &info->mti_attr.ma_attr;
355         struct mdt_object       *obj;
356         struct md_object        *child;
357         __u64                    valid = attr->la_valid;
358         const char              *xattr_name = rr->rr_name;
359         int                      xattr_len = rr->rr_eadatalen;
360         __u64                    lockpart;
361         int                      rc;
362         posix_acl_xattr_header  *new_xattr = NULL;
363         __u32                    remote = exp_connect_rmtclient(info->mti_exp);
364         __u32                    perm;
365         ENTRY;
366
367         CDEBUG(D_INODE, "setxattr for "DFID"\n", PFID(rr->rr_fid1));
368
369         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SETXATTR))
370                 RETURN(err_serious(-ENOMEM));
371
372         CDEBUG(D_INODE, "%s xattr %s\n",
373                valid & OBD_MD_FLXATTR ? "set" : "remove", xattr_name);
374
375         rc = mdt_init_ucred_reint(info);
376         if (rc != 0)
377                 RETURN(rc);
378
379         if (valid & OBD_MD_FLRMTRSETFACL) {
380                 if (unlikely(!remote))
381                         GOTO(out, rc = err_serious(-EINVAL));
382
383                 perm = mdt_identity_get_perm(uc->uc_identity, remote,
384                                              req->rq_peer.nid);
385                 if (!(perm & CFS_RMTACL_PERM))
386                         GOTO(out, rc = err_serious(-EPERM));
387         }
388
389         if (strncmp(xattr_name, XATTR_USER_PREFIX,
390                     sizeof(XATTR_USER_PREFIX) - 1) == 0) {
391                 if (!(exp_connect_flags(req->rq_export) & OBD_CONNECT_XATTR))
392                         GOTO(out, rc = -EOPNOTSUPP);
393                 if (strcmp(xattr_name, XATTR_NAME_LOV) == 0)
394                         GOTO(out, rc = -EACCES);
395                 if (strcmp(xattr_name, XATTR_NAME_LMA) == 0)
396                         GOTO(out, rc = 0);
397                 if (strcmp(xattr_name, XATTR_NAME_LINK) == 0)
398                         GOTO(out, rc = 0);
399         } else if ((valid & OBD_MD_FLXATTR) &&
400                    (strncmp(xattr_name, XATTR_NAME_ACL_ACCESS,
401                             sizeof(XATTR_NAME_ACL_ACCESS) - 1) == 0 ||
402                     strncmp(xattr_name, XATTR_NAME_ACL_DEFAULT,
403                             sizeof(XATTR_NAME_ACL_DEFAULT) - 1) == 0)) {
404                 /* currently lustre limit acl access size */
405                 if (xattr_len > LUSTRE_POSIX_ACL_MAX_SIZE)
406                         GOTO(out, -ERANGE);
407         }
408
409         lockpart = MDS_INODELOCK_UPDATE;
410         /* Revoke all clients' lookup lock, since the access
411          * permissions for this inode is changed when ACL_ACCESS is
412          * set. This isn't needed for ACL_DEFAULT, since that does
413          * not change the access permissions of this inode, nor any
414          * other existing inodes. It is setting the ACLs inherited
415          * by new directories/files at create time. */
416         /* We need revoke both LOOKUP|PERM lock here, see mdt_attr_set. */
417         if (!strcmp(xattr_name, XATTR_NAME_ACL_ACCESS))
418                 lockpart |= MDS_INODELOCK_PERM | MDS_INODELOCK_LOOKUP;
419
420         /* We need to take the lock on behalf of old clients so that newer
421          * clients flush their xattr caches */
422         if (!(valid & OBD_MD_FLXATTRLOCKED))
423                 lockpart |= MDS_INODELOCK_XATTR;
424
425         lh = &info->mti_lh[MDT_LH_PARENT];
426         /* ACLs were sent to clients under LCK_CR locks, so taking LCK_EX
427          * to cancel them. */
428         mdt_lock_reg_init(lh, LCK_EX);
429         obj = mdt_object_find_lock(info, rr->rr_fid1, lh, lockpart);
430         if (IS_ERR(obj))
431                 GOTO(out, rc =  PTR_ERR(obj));
432
433         down_write(&obj->mot_xattr_sem);
434
435         info->mti_mos = obj;
436         rc = mdt_version_get_check_save(info, obj, 0);
437         if (rc)
438                 GOTO(out_unlock, rc);
439
440         if (unlikely(!(valid & OBD_MD_FLCTIME))) {
441                 /* This isn't strictly an error, but all current clients
442                  * should set OBD_MD_FLCTIME when setting attributes. */
443                 CWARN("%s: client miss to set OBD_MD_FLCTIME when "
444                       "setxattr %s: [object "DFID"] [valid "LPU64"]\n",
445                       mdt_obd_name(info->mti_mdt), xattr_name,
446                       PFID(rr->rr_fid1), valid);
447                 attr->la_ctime = cfs_time_current_sec();
448         }
449         attr->la_valid = LA_CTIME;
450         child = mdt_object_child(obj);
451         if (valid & OBD_MD_FLXATTR) {
452                 char *xattr = (void *)rr->rr_eadata;
453
454                 if (xattr_len > 0) {
455                         int flags = 0;
456
457                         if (valid & OBD_MD_FLRMTLSETFACL) {
458                                 if (unlikely(!remote))
459                                         GOTO(out_unlock, rc = -EINVAL);
460
461                                 xattr_len = mdt_rmtlsetfacl(info, child,
462                                                 xattr_name,
463                                                 (ext_acl_xattr_header *)xattr,
464                                                 &new_xattr);
465                                 if (xattr_len < 0)
466                                         GOTO(out_unlock, rc = xattr_len);
467
468                                 xattr = (char *)new_xattr;
469                         }
470
471                         if (attr->la_flags & XATTR_REPLACE)
472                                 flags |= LU_XATTR_REPLACE;
473
474                         if (attr->la_flags & XATTR_CREATE)
475                                 flags |= LU_XATTR_CREATE;
476
477                         mdt_fail_write(env, info->mti_mdt->mdt_bottom,
478                                        OBD_FAIL_MDS_SETXATTR_WRITE);
479
480                         buf->lb_buf = xattr;
481                         buf->lb_len = xattr_len;
482                         rc = mo_xattr_set(env, child, buf, xattr_name, flags);
483                         /* update ctime after xattr changed */
484                         if (rc == 0) {
485                                 ma->ma_attr_flags |= MDS_PERM_BYPASS;
486                                 mo_attr_set(env, child, ma);
487                         }
488                 }
489         } else if (valid & OBD_MD_FLXATTRRM) {
490                 rc = mo_xattr_del(env, child, xattr_name);
491                 /* update ctime after xattr changed */
492                 if (rc == 0) {
493                         ma->ma_attr_flags |= MDS_PERM_BYPASS;
494                         mo_attr_set(env, child, ma);
495                 }
496         } else {
497                 CDEBUG(D_INFO, "valid bits: "LPX64"\n", valid);
498                 rc = -EINVAL;
499         }
500         if (rc == 0)
501                 mdt_counter_incr(req, LPROC_MDT_SETXATTR);
502
503         EXIT;
504 out_unlock:
505         up_write(&obj->mot_xattr_sem);
506         mdt_object_unlock_put(info, obj, lh, rc);
507         if (unlikely(new_xattr != NULL))
508                 lustre_posix_acl_xattr_free(new_xattr, xattr_len);
509 out:
510         mdt_exit_ucred(info);
511         return rc;
512 }