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  *  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                 CDEBUG(D_INFO, "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                         CDEBUG(D_INFO, "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_server_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                         CDEBUG(D_INFO, "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,
214                            struct md_object *next,
215                            const char *xattr_name,
216                            ext_acl_xattr_header *header,
217                            posix_acl_xattr_header **out)
218 {
219         struct ptlrpc_request  *req = mdt_info_req(info);
220         struct mdt_export_data *med = mdt_req2med(req);
221         struct md_ucred        *uc = mdt_ucred(info);
222         struct lu_buf          *buf = &info->mti_buf;
223         int                     rc;
224         ENTRY;
225
226         rc = lustre_ext_acl_xattr_id2server(uc, med->med_idmap, header);
227         if (rc)
228                 RETURN(rc);
229  
230         rc = mo_xattr_get(info->mti_env, next, &LU_BUF_NULL, xattr_name);
231         if (rc == -ENODATA)
232                 rc = 0;
233         else if (rc < 0)
234                 RETURN(rc);
235
236         buf->lb_len = rc;
237         if (buf->lb_len > 0) {
238                 OBD_ALLOC(buf->lb_buf, buf->lb_len);
239                 if (unlikely(buf->lb_buf == NULL))
240                         RETURN(-ENOMEM);
241
242                 rc = mo_xattr_get(info->mti_env, next, buf, xattr_name);
243                 if (rc < 0) {
244                         CERROR("getxattr failed: %d\n", rc);
245                         GOTO(_out, rc);
246                 }
247         } else
248                 buf->lb_buf = NULL;
249
250         rc = lustre_acl_xattr_merge2posix((posix_acl_xattr_header *)(buf->lb_buf),
251                                           buf->lb_len, header, out);
252         EXIT;
253
254 _out:
255         if (rc <= 0 && buf->lb_buf != NULL)
256                 OBD_FREE(buf->lb_buf, buf->lb_len);
257         return rc;
258 }
259
260 int mdt_reint_setxattr(struct mdt_thread_info *info,
261                        struct mdt_lock_handle *unused)
262 {
263         struct ptlrpc_request   *req = mdt_info_req(info);
264         struct mdt_export_data  *med = mdt_req2med(req);
265         struct md_ucred         *uc  = mdt_ucred(info);
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         const struct lu_env     *env  = info->mti_env;
271         struct lu_buf           *buf  = &info->mti_buf;
272         struct mdt_reint_record *rr   = &info->mti_rr;
273         struct lu_attr          *attr = &info->mti_attr.ma_attr;
274         struct mdt_object       *obj; 
275         struct md_object        *child;
276         __u64                    valid = attr->la_valid;
277         const char              *xattr_name;
278         int                      xattr_len = 0;
279         __u64                    lockpart;
280         int                      rc;
281         posix_acl_xattr_header  *new_xattr = NULL;
282         ENTRY;
283
284         CDEBUG(D_INODE, "setxattr for "DFID"\n", PFID(rr->rr_fid1));
285
286         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SETXATTR))
287                 RETURN(err_serious(-ENOMEM));
288
289         xattr_name = rr->rr_name;
290
291         CDEBUG(D_INODE, "%s xattr %s\n",
292                valid & OBD_MD_FLXATTR ? "set" : "remove", xattr_name);
293
294         rc = mdt_init_ucred_reint(info);
295         if (rc != 0)
296                 RETURN(rc);
297
298         if (valid & OBD_MD_FLRMTRSETFACL) {
299                 __u32 perm = mdt_identity_get_perm(uc->mu_identity,
300                                                    med->med_rmtclient,
301                                                    req->rq_peer.nid);
302
303                 LASSERT(med->med_rmtclient);
304                 if (!(perm & CFS_RMTACL_PERM))
305                         GOTO(out, rc = err_serious(-EPERM));
306         }
307
308         /* various sanity check for xattr name */
309         xattr_name = req_capsule_client_get(pill, &RMF_NAME);
310         if (!xattr_name)
311                 GOTO(out, rc = err_serious(-EFAULT));
312
313         if (strncmp(xattr_name, trust_string, sizeof(trust_string) - 1) == 0) {
314                 if (strcmp(xattr_name + 8, XATTR_NAME_LOV) == 0)
315                         GOTO(out, rc = -EACCES);
316         }
317
318         if (!(req->rq_export->exp_connect_flags & OBD_CONNECT_XATTR) &&
319             (strncmp(xattr_name, user_string, sizeof(user_string) - 1) == 0)) {
320                 GOTO(out, rc = -EOPNOTSUPP);
321         }
322
323         lockpart = MDS_INODELOCK_UPDATE;
324         if (!strcmp(xattr_name, XATTR_NAME_ACL_ACCESS))
325                 lockpart |= MDS_INODELOCK_LOOKUP;
326
327         lh = &info->mti_lh[MDT_LH_PARENT];
328         mdt_lock_reg_init(lh, LCK_PW);
329         obj = mdt_object_find_lock(info, rr->rr_fid1, lh, lockpart);
330         if (IS_ERR(obj))
331                 GOTO(out, rc =  PTR_ERR(obj));
332
333         child = mdt_object_child(obj);
334         if ((valid & OBD_MD_FLXATTR) == OBD_MD_FLXATTR) {
335                 char * xattr;
336
337                 if (!req_capsule_field_present(pill, &RMF_EADATA, RCL_CLIENT)) {
338                         CDEBUG(D_INFO, "no xattr data supplied\n");
339                         GOTO(out_unlock, rc = -EFAULT);
340                 }
341
342                 xattr_len = req_capsule_get_size(pill, &RMF_EADATA, RCL_CLIENT);
343                 if (xattr_len) {
344                         int flags = 0;
345
346                         xattr = req_capsule_client_get(pill, &RMF_EADATA);
347
348                         if (valid & OBD_MD_FLRMTLSETFACL) {
349                                 LASSERT(med->med_rmtclient);
350                                 xattr_len = mdt_rmtlsetfacl(info, child,
351                                                 xattr_name,
352                                                 (ext_acl_xattr_header *)xattr,
353                                                 &new_xattr);
354                                 if (xattr_len < 0)
355                                         GOTO(out_unlock, rc = xattr_len);
356
357                                 xattr = (char *)new_xattr;
358                         }
359
360                         if (attr->la_flags & XATTR_REPLACE)
361                                 flags |= LU_XATTR_REPLACE;
362
363                         if (attr->la_flags & XATTR_CREATE)
364                                 flags |= LU_XATTR_CREATE;
365
366                         mdt_fail_write(env, info->mti_mdt->mdt_bottom,
367                                        OBD_FAIL_MDS_SETXATTR_WRITE);
368
369                         buf->lb_buf = xattr;
370                         buf->lb_len = xattr_len;
371                         rc = mo_xattr_set(env, child, buf, xattr_name, flags);
372                 }
373         } else if ((valid & OBD_MD_FLXATTRRM) == OBD_MD_FLXATTRRM) {
374                 rc = mo_xattr_del(env, child, xattr_name);
375         } else {
376                 CDEBUG(D_INFO, "valid bits: "LPX64"\n", valid);
377                 rc = -EINVAL;
378         }
379         EXIT;
380 out_unlock:
381         mdt_object_unlock_put(info, obj, lh, rc);
382         if (unlikely(new_xattr != NULL))
383                 lustre_posix_acl_xattr_free(new_xattr, xattr_len);
384 out:
385         mdt_exit_ucred(info);
386         return rc;
387 }