Whamcloud - gitweb
b=16890 a build fix for --disable-quota
[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 /*
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 < 0) {
92                 if (size == -ENODATA)
93                         size = 0;
94                 else if (size != -EOPNOTSUPP) {
95                         CDEBUG(D_INFO, "Error geting EA size: %d\n", size);
96                         RETURN(size);
97                 }
98         }
99
100         if (info->mti_body->eadatasize != 0 &&
101             info->mti_body->eadatasize < size)
102                 RETURN(-ERANGE);
103
104         req_capsule_set_size(pill, &RMF_EADATA, RCL_SERVER,
105                              min_t(int, size, info->mti_body->eadatasize));
106
107         rc = req_capsule_server_pack(pill);
108         if (rc) {
109                 LASSERT(rc < 0);
110                 RETURN(rc);
111         }
112
113         RETURN(size);
114 }
115
116 int mdt_getxattr(struct mdt_thread_info *info)
117 {
118         struct ptlrpc_request  *req = mdt_info_req(info);
119         struct mdt_export_data *med = mdt_req2med(req);
120         struct md_ucred        *uc  = mdt_ucred(info);
121         struct mdt_body        *reqbody;
122         struct mdt_body        *repbody = NULL;
123         struct md_object       *next;
124         struct lu_buf          *buf;
125         __u32                   remote = exp_connect_rmtclient(info->mti_exp);
126         __u32                   perm;
127         int                     easize, rc;
128         ENTRY;
129
130         LASSERT(info->mti_object != NULL);
131         LASSERT(lu_object_assert_exists(&info->mti_object->mot_obj.mo_lu));
132
133         CDEBUG(D_INODE, "getxattr "DFID"\n", PFID(&info->mti_body->fid1));
134
135         reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
136         if (reqbody == NULL)
137                 RETURN(err_serious(-EFAULT));
138
139         rc = mdt_init_ucred(info, reqbody);
140         if (rc)
141                 RETURN(err_serious(rc));
142
143         next = mdt_object_child(info->mti_object);
144
145         if (info->mti_body->valid & OBD_MD_FLRMTRGETFACL) {
146                 if (unlikely(!remote))
147                         GOTO(out, rc = err_serious(-EINVAL));
148
149                 perm = mdt_identity_get_perm(uc->mu_identity, remote,
150                                              req->rq_peer.nid);
151                 if (!(perm & CFS_RMTACL_PERM))
152                         GOTO(out, rc = err_serious(-EPERM));
153
154                 rc = mo_permission(info->mti_env, NULL, next, NULL,
155                                    MAY_RGETFACL);
156                 if (rc)
157                         GOTO(out, rc = err_serious(rc));
158         }
159
160         easize = mdt_getxattr_pack_reply(info);
161         if (easize < 0)
162                 GOTO(out, rc = err_serious(easize));
163
164         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
165         LASSERT(repbody != NULL);
166
167         /* No need further getxattr. */
168         if (easize == 0 || reqbody->eadatasize == 0)
169                 GOTO(out, rc = easize);
170
171         buf = &info->mti_buf;
172         buf->lb_buf = req_capsule_server_get(info->mti_pill, &RMF_EADATA);
173         buf->lb_len = easize;
174
175         if (info->mti_body->valid & OBD_MD_FLXATTR) {
176                 int flags = CFS_IC_NOTHING;
177                 char *xattr_name = req_capsule_client_get(info->mti_pill,
178                                                           &RMF_NAME);
179                 CDEBUG(D_INODE, "getxattr %s\n", xattr_name);
180
181                 rc = mo_xattr_get(info->mti_env, next, buf, xattr_name);
182                 if (rc < 0) {
183                         CERROR("getxattr failed: %d\n", rc);
184                         GOTO(out, rc);
185                 }
186
187                 if (info->mti_body->valid &
188                     (OBD_MD_FLRMTLSETFACL | OBD_MD_FLRMTLGETFACL))
189                         flags = CFS_IC_ALL;
190                 else if (info->mti_body->valid & OBD_MD_FLRMTRGETFACL)
191                         flags = CFS_IC_MAPPED;
192
193                 if (rc > 0 && flags != CFS_IC_NOTHING) {
194                         int rc1;
195
196                         if (unlikely(!remote))
197                                 GOTO(out, rc = -EINVAL);
198
199                         rc1 = lustre_posix_acl_xattr_id2client(uc,
200                                         med->med_idmap,
201                                         (posix_acl_xattr_header *)(buf->lb_buf),
202                                         rc, flags);
203                         if (unlikely(rc1 < 0))
204                                 rc = rc1;
205                 }
206         } else if (info->mti_body->valid & OBD_MD_FLXATTRLS) {
207                 CDEBUG(D_INODE, "listxattr\n");
208
209                 rc = mo_xattr_list(info->mti_env, next, buf);
210                 if (rc < 0)
211                         CDEBUG(D_INFO, "listxattr failed: %d\n", rc);
212         } else
213                 LBUG();
214
215         EXIT;
216 out:
217         if (rc >= 0) {
218                 repbody->eadatasize = rc;
219                 rc = 0;
220         }
221         mdt_exit_ucred(info);
222         return rc;
223 }
224
225 static int mdt_rmtlsetfacl(struct mdt_thread_info *info,
226                            struct md_object *next,
227                            const char *xattr_name,
228                            ext_acl_xattr_header *header,
229                            posix_acl_xattr_header **out)
230 {
231         struct ptlrpc_request  *req = mdt_info_req(info);
232         struct mdt_export_data *med = mdt_req2med(req);
233         struct md_ucred        *uc = mdt_ucred(info);
234         struct lu_buf          *buf = &info->mti_buf;
235         int                     rc;
236         ENTRY;
237
238         rc = lustre_ext_acl_xattr_id2server(uc, med->med_idmap, header);
239         if (rc)
240                 RETURN(rc);
241  
242         rc = mo_xattr_get(info->mti_env, next, &LU_BUF_NULL, xattr_name);
243         if (rc == -ENODATA)
244                 rc = 0;
245         else if (rc < 0)
246                 RETURN(rc);
247
248         buf->lb_len = rc;
249         if (buf->lb_len > 0) {
250                 OBD_ALLOC(buf->lb_buf, buf->lb_len);
251                 if (unlikely(buf->lb_buf == NULL))
252                         RETURN(-ENOMEM);
253
254                 rc = mo_xattr_get(info->mti_env, next, buf, xattr_name);
255                 if (rc < 0) {
256                         CERROR("getxattr failed: %d\n", rc);
257                         GOTO(_out, rc);
258                 }
259         } else
260                 buf->lb_buf = NULL;
261
262         rc = lustre_acl_xattr_merge2posix((posix_acl_xattr_header *)(buf->lb_buf),
263                                           buf->lb_len, header, out);
264         EXIT;
265
266 _out:
267         if (rc <= 0 && buf->lb_buf != NULL)
268                 OBD_FREE(buf->lb_buf, buf->lb_len);
269         return rc;
270 }
271
272 int mdt_reint_setxattr(struct mdt_thread_info *info,
273                        struct mdt_lock_handle *unused)
274 {
275         struct ptlrpc_request   *req = mdt_info_req(info);
276         struct md_ucred         *uc  = mdt_ucred(info);
277         struct mdt_lock_handle  *lh;
278         struct req_capsule      *pill = info->mti_pill;
279         const struct lu_env     *env  = info->mti_env;
280         struct lu_buf           *buf  = &info->mti_buf;
281         struct mdt_reint_record *rr   = &info->mti_rr;
282         struct md_attr          *ma = &info->mti_attr;
283         struct lu_attr          *attr = &info->mti_attr.ma_attr;
284         struct mdt_object       *obj; 
285         struct md_object        *child;
286         __u64                    valid = attr->la_valid;
287         const char              *xattr_name;
288         int                      xattr_len = 0;
289         __u64                    lockpart;
290         int                      rc;
291         posix_acl_xattr_header  *new_xattr = NULL;
292         __u32                    remote = exp_connect_rmtclient(info->mti_exp);
293         __u32                    perm;
294         ENTRY;
295
296         CDEBUG(D_INODE, "setxattr for "DFID"\n", PFID(rr->rr_fid1));
297
298         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SETXATTR))
299                 RETURN(err_serious(-ENOMEM));
300
301         xattr_name = rr->rr_name;
302
303         CDEBUG(D_INODE, "%s xattr %s\n",
304                valid & OBD_MD_FLXATTR ? "set" : "remove", xattr_name);
305
306         rc = mdt_init_ucred_reint(info);
307         if (rc != 0)
308                 RETURN(rc);
309
310         if (valid & OBD_MD_FLRMTRSETFACL) {
311                 if (unlikely(!remote))
312                         GOTO(out, rc = err_serious(-EINVAL));
313
314                 perm = mdt_identity_get_perm(uc->mu_identity, remote,
315                                              req->rq_peer.nid);
316                 if (!(perm & CFS_RMTACL_PERM))
317                         GOTO(out, rc = err_serious(-EPERM));
318         }
319
320         /* various sanity check for xattr name */
321         xattr_name = req_capsule_client_get(pill, &RMF_NAME);
322         if (!xattr_name)
323                 GOTO(out, rc = err_serious(-EFAULT));
324
325         if (strncmp(xattr_name, XATTR_USER_PREFIX,
326                     sizeof(XATTR_USER_PREFIX) - 1) == 0) {
327                 if (!(req->rq_export->exp_connect_flags & OBD_CONNECT_XATTR))
328                         GOTO(out, rc = -EOPNOTSUPP);
329                 if (strcmp(xattr_name, XATTR_NAME_LOV) == 0)
330                         GOTO(out, rc = -EACCES);
331                 if (strcmp(xattr_name, XATTR_NAME_LMA) == 0)
332                         GOTO(out, rc = 0);
333                 if (strcmp(xattr_name, XATTR_NAME_LINK) == 0)
334                         GOTO(out, rc = 0);
335         } else if ((valid & OBD_MD_FLXATTR) &&
336                    (strncmp(xattr_name, XATTR_NAME_ACL_ACCESS,
337                              sizeof(XATTR_NAME_ACL_ACCESS) - 1) == 0)) {
338                 /* currently lustre limit acl access size */
339                 xattr_len = req_capsule_get_size(pill, &RMF_EADATA, RCL_CLIENT);
340
341                 if (xattr_len > LUSTRE_POSIX_ACL_MAX_SIZE)
342                         GOTO(out, -ERANGE);
343         }
344
345         lockpart = MDS_INODELOCK_UPDATE;
346         if (!strcmp(xattr_name, XATTR_NAME_ACL_ACCESS))
347                 lockpart |= MDS_INODELOCK_LOOKUP;
348
349         lh = &info->mti_lh[MDT_LH_PARENT];
350         mdt_lock_reg_init(lh, LCK_PW);
351         obj = mdt_object_find_lock(info, rr->rr_fid1, lh, lockpart);
352         if (IS_ERR(obj))
353                 GOTO(out, rc =  PTR_ERR(obj));
354
355         info->mti_mos = obj;
356         rc = mdt_version_get_check_save(info, obj, 0);
357         if (rc)
358                 GOTO(out_unlock, rc);
359
360         if (unlikely(!(valid & OBD_MD_FLCTIME))) {
361                 CWARN("client miss to set OBD_MD_FLCTIME when "
362                       "setxattr: [object "DFID"] [valid "LPU64"]\n",
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;
370
371                 if (!req_capsule_field_present(pill, &RMF_EADATA, RCL_CLIENT)) {
372                         CDEBUG(D_INFO, "no xattr data supplied\n");
373                         GOTO(out_unlock, rc = -EFAULT);
374                 }
375
376                 xattr_len = req_capsule_get_size(pill, &RMF_EADATA, RCL_CLIENT);
377                 if (xattr_len) {
378                         int flags = 0;
379
380                         xattr = req_capsule_client_get(pill, &RMF_EADATA);
381
382                         if (valid & OBD_MD_FLRMTLSETFACL) {
383                                 if (unlikely(!remote))
384                                         GOTO(out_unlock, rc = -EINVAL);
385
386                                 xattr_len = mdt_rmtlsetfacl(info, child,
387                                                 xattr_name,
388                                                 (ext_acl_xattr_header *)xattr,
389                                                 &new_xattr);
390                                 if (xattr_len < 0)
391                                         GOTO(out_unlock, rc = xattr_len);
392
393                                 xattr = (char *)new_xattr;
394                         }
395
396                         if (attr->la_flags & XATTR_REPLACE)
397                                 flags |= LU_XATTR_REPLACE;
398
399                         if (attr->la_flags & XATTR_CREATE)
400                                 flags |= LU_XATTR_CREATE;
401
402                         mdt_fail_write(env, info->mti_mdt->mdt_bottom,
403                                        OBD_FAIL_MDS_SETXATTR_WRITE);
404
405                         buf->lb_buf = xattr;
406                         buf->lb_len = xattr_len;
407                         rc = mo_xattr_set(env, child, buf, xattr_name, flags);
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                 }
414         } else if (valid & OBD_MD_FLXATTRRM) {
415                 rc = mo_xattr_del(env, child, xattr_name);
416                 /* update ctime after xattr changed */
417                 if (rc == 0) {
418                         ma->ma_attr_flags |= MDS_PERM_BYPASS;
419                         mo_attr_set(env, child, ma);
420                 }
421         } else {
422                 CDEBUG(D_INFO, "valid bits: "LPX64"\n", valid);
423                 rc = -EINVAL;
424         }
425         EXIT;
426 out_unlock:
427         mdt_object_unlock_put(info, obj, lh, rc);
428         if (unlikely(new_xattr != NULL))
429                 lustre_posix_acl_xattr_free(new_xattr, xattr_len);
430 out:
431         mdt_exit_ucred(info);
432         return rc;
433 }