Whamcloud - gitweb
land b_colibri_devel on 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  *  linux/mdt/mdt_xattr.c
5  *  Lustre Metadata Target (mdt) extended attributes management.
6  *
7  *  Copyright (C) 2002-2006 Cluster File Systems, Inc.
8  *   Author: Peter Braam <braam@clusterfs.com>
9  *   Author: Andreas Dilger <adilger@clusterfs.com>
10  *   Author: Phil Schwan <phil@clusterfs.com>
11  *   Author: Huang Hua <huanghua@clusterfs.com>
12  *
13  *   This file is part of the Lustre file system, http://www.lustre.org
14  *   Lustre is a trademark of Cluster File Systems, Inc.
15  *
16  *   You may have signed or agreed to another license before downloading
17  *   this software.  If so, you are bound by the terms and conditions
18  *   of that agreement, and the following does not apply to you.  See the
19  *   LICENSE file included with this distribution for more information.
20  *
21  *   If you did not agree to a different license, then this copy of Lustre
22  *   is open source software; you can redistribute it and/or modify it
23  *   under the terms of version 2 of the GNU General Public License as
24  *   published by the Free Software Foundation.
25  *
26  *   In either case, Lustre is distributed in the hope that it will be
27  *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
28  *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29  *   license text for more details.
30  */
31
32 #ifndef EXPORT_SYMTAB
33 # define EXPORT_SYMTAB
34 #endif
35 #define DEBUG_SUBSYSTEM S_MDS
36
37 /* prerequisite for linux/xattr.h */
38 #include <linux/types.h>
39 /* prerequisite for linux/xattr.h */
40 #include <linux/fs.h>
41 /* XATTR_{REPLACE,CREATE} */
42 #include <linux/xattr.h>
43
44 #include "mdt_internal.h"
45
46
47 /* return EADATA length to the caller. negative value means error */
48 static int mdt_getxattr_pack_reply(struct mdt_thread_info * info)
49 {
50         struct req_capsule     *pill = &info->mti_pill ;
51         struct ptlrpc_request  *req = mdt_info_req(info);
52         char                   *xattr_name;
53         __u64                   valid = info->mti_body->valid;
54         static const char       user_string[] = "user.";
55         int                     size, rc;
56         ENTRY;
57         
58         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETXATTR_PACK))
59                 RETURN(-ENOMEM);
60
61         /* Determine how many bytes we need */
62         if ((valid & OBD_MD_FLXATTR) == OBD_MD_FLXATTR) {
63                 xattr_name = req_capsule_client_get(pill, &RMF_NAME);
64                 if (!xattr_name)
65                         RETURN(-EFAULT);
66
67                 if (!(req->rq_export->exp_connect_flags & OBD_CONNECT_XATTR) &&
68                     !strncmp(xattr_name, user_string, sizeof(user_string) - 1))
69                         RETURN(-EOPNOTSUPP);
70                 
71                         size = mo_xattr_get(info->mti_env,
72                                             mdt_object_child(info->mti_object),
73                                             &LU_BUF_NULL, xattr_name);
74         } else if ((valid & OBD_MD_FLXATTRLS) == OBD_MD_FLXATTRLS) {
75                 size = mo_xattr_list(info->mti_env,
76                                      mdt_object_child(info->mti_object),
77                                      &LU_BUF_NULL);
78         } else {
79                 CERROR("Valid bits: "LPX64"\n", info->mti_body->valid);
80                 RETURN(-EINVAL);
81         }
82
83         if (size < 0) {
84                 if (size == -ENODATA)
85                         size = 0;
86                 else if (size != -EOPNOTSUPP) {
87                         CERROR("Error geting EA size: %d\n", size);
88                         RETURN(size);
89                 }
90         }
91
92         if (info->mti_body->eadatasize != 0 &&
93             info->mti_body->eadatasize < size)
94                 RETURN(-ERANGE);
95
96         req_capsule_set_size(pill, &RMF_EADATA, RCL_SERVER,
97                              min_t(int, size, info->mti_body->eadatasize));
98
99         rc = req_capsule_pack(pill);
100         if (rc) {
101                 LASSERT(rc < 0);
102                 RETURN(rc);
103         }
104
105         RETURN(size);
106 }
107
108 int mdt_getxattr(struct mdt_thread_info *info)
109 {
110         struct ptlrpc_request  *req = mdt_info_req(info);
111         struct mdt_export_data *med = mdt_req2med(req);
112         struct md_ucred        *uc  = mdt_ucred(info);
113         struct  mdt_body       *reqbody;
114         struct  mdt_body       *repbody = NULL;
115         struct  md_object      *next;
116         struct  lu_buf         *buf;
117         int                     easize, rc;
118         ENTRY;
119
120         LASSERT(info->mti_object != NULL);
121         LASSERT(lu_object_assert_exists(&info->mti_object->mot_obj.mo_lu));
122
123         CDEBUG(D_INODE, "getxattr "DFID"\n", PFID(&info->mti_body->fid1));
124
125         reqbody = req_capsule_client_get(&info->mti_pill, &RMF_MDT_BODY);
126         if (reqbody == NULL)
127                 RETURN(err_serious(-EFAULT));
128
129         rc = mdt_init_ucred(info, reqbody);
130         if (rc)
131                 RETURN(err_serious(rc));
132
133         next = mdt_object_child(info->mti_object);
134
135         if (info->mti_body->valid & OBD_MD_FLRMTRGETFACL) {
136                 __u32 perm = mdt_identity_get_perm(uc->mu_identity,
137                                                    med->med_rmtclient,
138                                                    req->rq_peer.nid);
139
140                 LASSERT(med->med_rmtclient);
141                 if (!(perm & CFS_RMTACL_PERM))
142                         GOTO(out, rc = err_serious(-EPERM));
143
144                 rc = mo_permission(info->mti_env, NULL, next, NULL,
145                                    MAY_RGETFACL);
146                 if (rc)
147                         GOTO(out, rc = err_serious(rc));
148         }
149
150         easize = mdt_getxattr_pack_reply(info);
151         if (easize < 0)
152                 GOTO(out, rc = err_serious(easize));
153
154         repbody = req_capsule_server_get(&info->mti_pill, &RMF_MDT_BODY);
155         LASSERT(repbody != NULL);
156
157         /* No need further getxattr. */
158         if (easize == 0 || reqbody->eadatasize == 0)
159                 GOTO(out, rc = easize);
160
161         buf = &info->mti_buf;
162         buf->lb_buf = req_capsule_server_get(&info->mti_pill, &RMF_EADATA);
163         buf->lb_len = easize;
164
165         if (info->mti_body->valid & OBD_MD_FLXATTR) {
166                 int flags = CFS_IC_NOTHING;
167                 char *xattr_name = req_capsule_client_get(&info->mti_pill,
168                                                           &RMF_NAME);
169                 CDEBUG(D_INODE, "getxattr %s\n", xattr_name);
170
171                         rc = mo_xattr_get(info->mti_env, next, buf, xattr_name);
172                 if (rc < 0) {
173                         CERROR("getxattr failed: %d\n", rc);
174                         GOTO(out, rc);
175                 }
176
177                 if (info->mti_body->valid &
178                     (OBD_MD_FLRMTLSETFACL | OBD_MD_FLRMTLGETFACL))
179                         flags = CFS_IC_ALL;
180                 else if (info->mti_body->valid & OBD_MD_FLRMTRGETFACL)
181                         flags = CFS_IC_MAPPED;
182
183                 if (rc > 0 && flags != CFS_IC_NOTHING) {
184                         int rc1;
185
186                         LASSERT(med->med_rmtclient);
187                         rc1 = lustre_posix_acl_xattr_id2client(uc,
188                                         med->med_idmap,
189                                         (posix_acl_xattr_header *)(buf->lb_buf),
190                                         rc, flags);
191                         if (unlikely(rc1 < 0))
192                                 rc = rc1;
193                 }
194         } else if (info->mti_body->valid & OBD_MD_FLXATTRLS) {
195                 CDEBUG(D_INODE, "listxattr\n");
196
197                 rc = mo_xattr_list(info->mti_env, next, buf);
198                 if (rc < 0)
199                         CERROR("listxattr failed: %d\n", rc);
200         } else
201                 LBUG();
202
203         EXIT;
204 out:
205         if (rc >= 0) {
206                 repbody->eadatasize = rc;
207                 rc = 0;
208         }
209         mdt_exit_ucred(info);
210         return rc;
211 }
212
213 static int mdt_rmtlsetfacl(struct mdt_thread_info *info, char *xattr_name,
214                            ext_acl_xattr_header *header,
215                            posix_acl_xattr_header **out)
216 {
217         struct ptlrpc_request *req = mdt_info_req(info);
218         struct mdt_export_data *med = mdt_req2med(req);
219         struct md_ucred *uc = mdt_ucred(info);
220         struct md_object *next = mdt_object_child(info->mti_object);
221         struct lu_buf         *buf = &info->mti_buf;
222         int rc;
223         ENTRY;
224
225         rc = lustre_ext_acl_xattr_id2server(uc, med->med_idmap, header);
226         if (rc)
227                 RETURN(rc);
228  
229         rc = mo_xattr_get(info->mti_env, next, &LU_BUF_NULL, xattr_name);
230         if (rc == -ENODATA)
231                 rc = 0;
232         else if (rc < 0)
233                 RETURN(rc);
234
235         buf->lb_len = rc;
236         if (buf->lb_len > 0) {
237                 OBD_ALLOC(buf->lb_buf, buf->lb_len);
238                 if (unlikely(buf->lb_buf == NULL))
239                         RETURN(-ENOMEM);
240
241                 rc = mo_xattr_get(info->mti_env, next, buf, xattr_name);
242                 if (rc < 0) {
243                         CERROR("getxattr failed: %d\n", rc);
244                         GOTO(_out, rc);
245         }
246         } else
247                 buf->lb_buf = NULL;
248
249         rc = lustre_acl_xattr_merge2posix((posix_acl_xattr_header *)(buf->lb_buf),
250                                           buf->lb_len, header, out);
251         EXIT;
252
253 _out:
254         if (rc <= 0 && buf->lb_buf != NULL)
255                 OBD_FREE(buf->lb_buf, buf->lb_len);
256
257         return rc;
258 }
259
260 int mdt_setxattr(struct mdt_thread_info *info)
261 {
262         struct ptlrpc_request   *req = mdt_info_req(info);
263         struct mdt_export_data  *med = mdt_req2med(req);
264         struct md_ucred         *uc  = mdt_ucred(info);
265         struct mdt_body         *reqbody;
266         const char               user_string[] = "user.";
267         const char               trust_string[] = "trusted.";
268         struct mdt_lock_handle  *lh;
269         struct req_capsule      *pill = &info->mti_pill;
270         struct mdt_object       *obj  = info->mti_object;
271         struct mdt_body         *body = (struct mdt_body *)info->mti_body;
272         const struct lu_env     *env  = info->mti_env;
273         struct md_object        *child  = mdt_object_child(obj);
274         struct lu_buf           *buf  = &info->mti_buf;
275         __u64                    valid  = body->valid;
276         char                    *xattr_name;
277         int                      xattr_len = 0;
278         __u64                    lockpart;
279         int                      rc;
280         posix_acl_xattr_header  *new_xattr = NULL;
281         ENTRY;
282
283         CDEBUG(D_INODE, "setxattr "DFID"\n", PFID(&body->fid1));
284
285         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SETXATTR))
286                 RETURN(err_serious(-ENOMEM));
287
288         reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
289         if (reqbody == NULL)
290                 RETURN(err_serious(-EFAULT));
291
292         rc = mdt_init_ucred(info, reqbody);
293         if (rc)
294                 RETURN(err_serious(rc));
295
296         if (valid & OBD_MD_FLRMTRSETFACL) {
297                 __u32 perm = mdt_identity_get_perm(uc->mu_identity,
298                                                    med->med_rmtclient,
299                                                    req->rq_peer.nid);
300
301                 LASSERT(med->med_rmtclient);
302                 if (!(perm & CFS_RMTACL_PERM))
303                         GOTO(out, rc = err_serious(-EPERM));
304         }
305
306         rc = req_capsule_pack(pill);
307         if (rc < 0)
308                 GOTO(out, rc = err_serious(rc));
309
310         /* various sanity check for xattr name */
311         xattr_name = req_capsule_client_get(pill, &RMF_NAME);
312         if (!xattr_name)
313                 GOTO(out, rc = err_serious(-EFAULT));
314
315         CDEBUG(D_INODE, "%s xattr %s\n",
316                   body->valid & OBD_MD_FLXATTR ? "set" : "remove", xattr_name);
317
318         if (strncmp(xattr_name, trust_string, sizeof(trust_string) - 1) == 0) {
319                 if (strcmp(xattr_name + 8, XATTR_NAME_LOV) == 0)
320                         GOTO(out, rc = -EACCES);
321         }
322
323         if (!(req->rq_export->exp_connect_flags & OBD_CONNECT_XATTR) &&
324             (strncmp(xattr_name, user_string, sizeof(user_string) - 1) == 0)) {
325                 GOTO(out, rc = -EOPNOTSUPP);
326         }
327
328         lockpart = MDS_INODELOCK_UPDATE;
329         if (!strcmp(xattr_name, XATTR_NAME_ACL_ACCESS))
330                 lockpart |= MDS_INODELOCK_LOOKUP;
331
332         lh = &info->mti_lh[MDT_LH_PARENT];
333         mdt_lock_reg_init(lh, LCK_PW);
334         rc = mdt_object_lock(info, obj, lh, lockpart, MDT_LOCAL_LOCK);
335         if (rc != 0)
336                 GOTO(out, rc);
337
338         if ((valid & OBD_MD_FLXATTR) == OBD_MD_FLXATTR) {
339                 char * xattr;
340                 if (!req_capsule_field_present(pill, &RMF_EADATA, RCL_CLIENT)) {
341                         CERROR("no xattr data supplied\n");
342                         GOTO(out_unlock, rc = -EFAULT);
343                 }
344
345                 xattr_len = req_capsule_get_size(pill, &RMF_EADATA, RCL_CLIENT);
346                 if (xattr_len) {
347                         int flags = 0;
348                         xattr = req_capsule_client_get(pill, &RMF_EADATA);
349
350                         if (valid & OBD_MD_FLRMTLSETFACL) {
351                                 LASSERT(med->med_rmtclient);
352                                 xattr_len = mdt_rmtlsetfacl(info, xattr_name,
353                                                 (ext_acl_xattr_header *)xattr,
354                                                 &new_xattr);
355                                 if (xattr_len < 0)
356                                         GOTO(out_unlock, rc = xattr_len);
357
358                                 xattr = (char *)new_xattr;
359                         }
360
361                         if (body->flags & XATTR_REPLACE)
362                                 flags |= LU_XATTR_REPLACE;
363
364                         if (body->flags & XATTR_CREATE)
365                                 flags |= LU_XATTR_CREATE;
366
367                         mdt_fail_write(env, info->mti_mdt->mdt_bottom,
368                                        OBD_FAIL_MDS_SETXATTR_WRITE);
369
370                         buf->lb_buf = xattr;
371                         buf->lb_len = xattr_len;
372                         rc = mo_xattr_set(env, child, buf, xattr_name, flags);
373                 }
374         } else if ((valid & OBD_MD_FLXATTRRM) == OBD_MD_FLXATTRRM) {
375                 rc = mo_xattr_del(env, child, xattr_name);
376         } else {
377                 CERROR("valid bits: "LPX64"\n", body->valid);
378                 rc = -EINVAL;
379         }
380         EXIT;
381 out_unlock:
382         if (unlikely(new_xattr != NULL))
383                 lustre_posix_acl_xattr_free(new_xattr, xattr_len);
384         mdt_object_unlock(info, obj, lh, rc);
385 out:
386         mdt_exit_ucred(info);
387         return rc;
388 }