Whamcloud - gitweb
Branch HEAD
[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  2008 Sun Microsystems, Inc. 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 /* prerequisite for linux/xattr.h */
52 #include <linux/types.h>
53 /* prerequisite for linux/xattr.h */
54 #include <linux/fs.h>
55 /* XATTR_{REPLACE,CREATE} */
56 #include <linux/xattr.h>
57
58 #include "mdt_internal.h"
59
60
61 /* return EADATA length to the caller. negative value means error */
62 static int mdt_getxattr_pack_reply(struct mdt_thread_info * info)
63 {
64         struct req_capsule     *pill = info->mti_pill ;
65         struct ptlrpc_request  *req = mdt_info_req(info);
66         char                   *xattr_name;
67         __u64                   valid = info->mti_body->valid;
68         static const char       user_string[] = "user.";
69         int                     size, rc;
70         ENTRY;
71         
72         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETXATTR_PACK))
73                 RETURN(-ENOMEM);
74
75         /* Determine how many bytes we need */
76         if (valid & OBD_MD_FLXATTR) {
77                 xattr_name = req_capsule_client_get(pill, &RMF_NAME);
78                 if (!xattr_name)
79                         RETURN(-EFAULT);
80
81                 if (!(req->rq_export->exp_connect_flags & OBD_CONNECT_XATTR) &&
82                     !strncmp(xattr_name, user_string, sizeof(user_string) - 1))
83                         RETURN(-EOPNOTSUPP);
84                 
85                 size = mo_xattr_get(info->mti_env,
86                                     mdt_object_child(info->mti_object),
87                                     &LU_BUF_NULL, xattr_name);
88         } else if (valid & OBD_MD_FLXATTRLS) {
89                 size = mo_xattr_list(info->mti_env,
90                                      mdt_object_child(info->mti_object),
91                                      &LU_BUF_NULL);
92         } else {
93                 CDEBUG(D_INFO, "Valid bits: "LPX64"\n", info->mti_body->valid);
94                 RETURN(-EINVAL);
95         }
96
97         if (size < 0) {
98                 if (size == -ENODATA)
99                         size = 0;
100                 else if (size != -EOPNOTSUPP) {
101                         CDEBUG(D_INFO, "Error geting EA size: %d\n", size);
102                         RETURN(size);
103                 }
104         }
105
106         if (info->mti_body->eadatasize != 0 &&
107             info->mti_body->eadatasize < size)
108                 RETURN(-ERANGE);
109
110         req_capsule_set_size(pill, &RMF_EADATA, RCL_SERVER,
111                              min_t(int, size, info->mti_body->eadatasize));
112
113         rc = req_capsule_server_pack(pill);
114         if (rc) {
115                 LASSERT(rc < 0);
116                 RETURN(rc);
117         }
118
119         RETURN(size);
120 }
121
122 int mdt_getxattr(struct mdt_thread_info *info)
123 {
124         struct ptlrpc_request  *req = mdt_info_req(info);
125         struct mdt_export_data *med = mdt_req2med(req);
126         struct md_ucred        *uc  = mdt_ucred(info);
127         struct mdt_body        *reqbody;
128         struct mdt_body        *repbody = NULL;
129         struct md_object       *next;
130         struct lu_buf          *buf;
131         int                     easize, rc;
132         ENTRY;
133
134         LASSERT(info->mti_object != NULL);
135         LASSERT(lu_object_assert_exists(&info->mti_object->mot_obj.mo_lu));
136
137         CDEBUG(D_INODE, "getxattr "DFID"\n", PFID(&info->mti_body->fid1));
138
139         reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
140         if (reqbody == NULL)
141                 RETURN(err_serious(-EFAULT));
142
143         rc = mdt_init_ucred(info, reqbody);
144         if (rc)
145                 RETURN(err_serious(rc));
146
147         next = mdt_object_child(info->mti_object);
148
149         if (info->mti_body->valid & OBD_MD_FLRMTRGETFACL) {
150                 __u32 perm = mdt_identity_get_perm(uc->mu_identity,
151                                                    med->med_rmtclient,
152                                                    req->rq_peer.nid);
153
154                 LASSERT(med->med_rmtclient);
155                 if (!(perm & CFS_RMTACL_PERM))
156                         GOTO(out, rc = err_serious(-EPERM));
157
158                 rc = mo_permission(info->mti_env, NULL, next, NULL,
159                                    MAY_RGETFACL);
160                 if (rc)
161                         GOTO(out, rc = err_serious(rc));
162         }
163
164         easize = mdt_getxattr_pack_reply(info);
165         if (easize < 0)
166                 GOTO(out, rc = err_serious(easize));
167
168         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
169         LASSERT(repbody != NULL);
170
171         /* No need further getxattr. */
172         if (easize == 0 || reqbody->eadatasize == 0)
173                 GOTO(out, rc = easize);
174
175         buf = &info->mti_buf;
176         buf->lb_buf = req_capsule_server_get(info->mti_pill, &RMF_EADATA);
177         buf->lb_len = easize;
178
179         if (info->mti_body->valid & OBD_MD_FLXATTR) {
180                 int flags = CFS_IC_NOTHING;
181                 char *xattr_name = req_capsule_client_get(info->mti_pill,
182                                                           &RMF_NAME);
183                 CDEBUG(D_INODE, "getxattr %s\n", xattr_name);
184
185                 rc = mo_xattr_get(info->mti_env, next, buf, xattr_name);
186                 if (rc < 0) {
187                         CERROR("getxattr failed: %d\n", rc);
188                         GOTO(out, rc);
189                 }
190
191                 if (info->mti_body->valid &
192                     (OBD_MD_FLRMTLSETFACL | OBD_MD_FLRMTLGETFACL))
193                         flags = CFS_IC_ALL;
194                 else if (info->mti_body->valid & OBD_MD_FLRMTRGETFACL)
195                         flags = CFS_IC_MAPPED;
196
197                 if (rc > 0 && flags != CFS_IC_NOTHING) {
198                         int rc1;
199
200                         LASSERT(med->med_rmtclient);
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                 repbody->eadatasize = rc;
221                 rc = 0;
222         }
223         mdt_exit_ucred(info);
224         return rc;
225 }
226
227 static int mdt_rmtlsetfacl(struct mdt_thread_info *info,
228                            struct md_object *next,
229                            const char *xattr_name,
230                            ext_acl_xattr_header *header,
231                            posix_acl_xattr_header **out)
232 {
233         struct ptlrpc_request  *req = mdt_info_req(info);
234         struct mdt_export_data *med = mdt_req2med(req);
235         struct md_ucred        *uc = mdt_ucred(info);
236         struct lu_buf          *buf = &info->mti_buf;
237         int                     rc;
238         ENTRY;
239
240         rc = lustre_ext_acl_xattr_id2server(uc, med->med_idmap, header);
241         if (rc)
242                 RETURN(rc);
243  
244         rc = mo_xattr_get(info->mti_env, next, &LU_BUF_NULL, xattr_name);
245         if (rc == -ENODATA)
246                 rc = 0;
247         else if (rc < 0)
248                 RETURN(rc);
249
250         buf->lb_len = rc;
251         if (buf->lb_len > 0) {
252                 OBD_ALLOC(buf->lb_buf, buf->lb_len);
253                 if (unlikely(buf->lb_buf == NULL))
254                         RETURN(-ENOMEM);
255
256                 rc = mo_xattr_get(info->mti_env, next, buf, xattr_name);
257                 if (rc < 0) {
258                         CERROR("getxattr failed: %d\n", rc);
259                         GOTO(_out, rc);
260                 }
261         } else
262                 buf->lb_buf = NULL;
263
264         rc = lustre_acl_xattr_merge2posix((posix_acl_xattr_header *)(buf->lb_buf),
265                                           buf->lb_len, header, out);
266         EXIT;
267
268 _out:
269         if (rc <= 0 && buf->lb_buf != NULL)
270                 OBD_FREE(buf->lb_buf, buf->lb_len);
271         return rc;
272 }
273
274 int mdt_reint_setxattr(struct mdt_thread_info *info,
275                        struct mdt_lock_handle *unused)
276 {
277         struct ptlrpc_request   *req = mdt_info_req(info);
278         struct mdt_export_data  *med = mdt_req2med(req);
279         struct md_ucred         *uc  = mdt_ucred(info);
280         const char               user_string[] = "user.";
281         const char               trust_string[] = "trusted.";
282         struct mdt_lock_handle  *lh;
283         struct req_capsule      *pill = info->mti_pill;
284         const struct lu_env     *env  = info->mti_env;
285         struct lu_buf           *buf  = &info->mti_buf;
286         struct mdt_reint_record *rr   = &info->mti_rr;
287         struct md_attr          *ma = &info->mti_attr;
288         struct lu_attr          *attr = &info->mti_attr.ma_attr;
289         struct mdt_object       *obj; 
290         struct md_object        *child;
291         __u64                    valid = attr->la_valid;
292         const char              *xattr_name;
293         int                      xattr_len = 0;
294         __u64                    lockpart;
295         int                      rc;
296         posix_acl_xattr_header  *new_xattr = NULL;
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                 __u32 perm = mdt_identity_get_perm(uc->mu_identity,
315                                                    med->med_rmtclient,
316                                                    req->rq_peer.nid);
317
318                 LASSERT(med->med_rmtclient);
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, trust_string, sizeof(trust_string) - 1) == 0) {
329                 if (strcmp(xattr_name + 8, XATTR_NAME_LOV) == 0)
330                         GOTO(out, rc = -EACCES);
331         }
332
333         if (!(req->rq_export->exp_connect_flags & OBD_CONNECT_XATTR) &&
334             (strncmp(xattr_name, user_string, sizeof(user_string) - 1) == 0)) {
335                 GOTO(out, rc = -EOPNOTSUPP);
336         }
337
338         lockpart = MDS_INODELOCK_UPDATE;
339         if (!strcmp(xattr_name, XATTR_NAME_ACL_ACCESS))
340                 lockpart |= MDS_INODELOCK_LOOKUP;
341
342         lh = &info->mti_lh[MDT_LH_PARENT];
343         mdt_lock_reg_init(lh, LCK_PW);
344         obj = mdt_object_find_lock(info, rr->rr_fid1, lh, lockpart);
345         if (IS_ERR(obj))
346                 GOTO(out, rc =  PTR_ERR(obj));
347
348         if (unlikely(!(valid & OBD_MD_FLCTIME))) {
349                 CWARN("client miss to set OBD_MD_FLCTIME when "
350                       "setxattr: [object "DFID"] [valid %llu]\n",
351                       PFID(rr->rr_fid1), valid);
352                 attr->la_ctime = cfs_time_current_sec();
353         }
354         attr->la_valid = LA_CTIME;
355         child = mdt_object_child(obj);
356         if (valid & OBD_MD_FLXATTR) {
357                 char * xattr;
358
359                 if (!req_capsule_field_present(pill, &RMF_EADATA, RCL_CLIENT)) {
360                         CDEBUG(D_INFO, "no xattr data supplied\n");
361                         GOTO(out_unlock, rc = -EFAULT);
362                 }
363
364                 xattr_len = req_capsule_get_size(pill, &RMF_EADATA, RCL_CLIENT);
365                 if (xattr_len) {
366                         int flags = 0;
367
368                         xattr = req_capsule_client_get(pill, &RMF_EADATA);
369
370                         if (valid & OBD_MD_FLRMTLSETFACL) {
371                                 LASSERT(med->med_rmtclient);
372                                 xattr_len = mdt_rmtlsetfacl(info, child,
373                                                 xattr_name,
374                                                 (ext_acl_xattr_header *)xattr,
375                                                 &new_xattr);
376                                 if (xattr_len < 0)
377                                         GOTO(out_unlock, rc = xattr_len);
378
379                                 xattr = (char *)new_xattr;
380                         }
381
382                         if (attr->la_flags & XATTR_REPLACE)
383                                 flags |= LU_XATTR_REPLACE;
384
385                         if (attr->la_flags & XATTR_CREATE)
386                                 flags |= LU_XATTR_CREATE;
387
388                         mdt_fail_write(env, info->mti_mdt->mdt_bottom,
389                                        OBD_FAIL_MDS_SETXATTR_WRITE);
390
391                         buf->lb_buf = xattr;
392                         buf->lb_len = xattr_len;
393                         rc = mo_xattr_set(env, child, buf, xattr_name, flags);
394                         /* update ctime after xattr changed */
395                         if (rc == 0) {
396                                 ma->ma_attr_flags |= MDS_PERM_BYPASS;
397                                 mo_attr_set(env, child, ma);
398                         }
399                 }
400         } else if (valid & OBD_MD_FLXATTRRM) {
401                 rc = mo_xattr_del(env, child, xattr_name);
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         } else {
408                 CDEBUG(D_INFO, "valid bits: "LPX64"\n", valid);
409                 rc = -EINVAL;
410         }
411         EXIT;
412 out_unlock:
413         mdt_object_unlock_put(info, obj, lh, rc);
414         if (unlikely(new_xattr != NULL))
415                 lustre_posix_acl_xattr_free(new_xattr, xattr_len);
416 out:
417         mdt_exit_ucred(info);
418         return rc;
419 }