Whamcloud - gitweb
- make HEAD from b_post_cmd3
[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 (MDT_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                 if (!strcmp(xattr_name, XATTR_NAME_LUSTRE_ACL))
72                         size = RMTACL_SIZE_MAX;
73                 else
74                         size = mo_xattr_get(info->mti_env,
75                                             mdt_object_child(info->mti_object),
76                                             &LU_BUF_NULL, xattr_name);
77         } else if ((valid & OBD_MD_FLXATTRLS) == OBD_MD_FLXATTRLS) {
78                 size = mo_xattr_list(info->mti_env,
79                                      mdt_object_child(info->mti_object),
80                                      &LU_BUF_NULL);
81         } else {
82                 CERROR("Valid bits: "LPX64"\n", info->mti_body->valid);
83                 RETURN(-EINVAL);
84         }
85
86         if (size < 0) {
87                 if (size == -ENODATA)
88                         size = 0;
89                 else if (size != -EOPNOTSUPP) {
90                         CERROR("Error geting EA size: %d\n", size);
91                         RETURN(size);
92                 }
93         }
94
95         if (info->mti_body->eadatasize != 0 &&
96             info->mti_body->eadatasize < size)
97                 RETURN(-ERANGE);
98
99         req_capsule_set_size(pill, &RMF_EADATA, RCL_SERVER,
100                              min_t(int, size, info->mti_body->eadatasize));
101
102         rc = req_capsule_pack(pill);
103         if (rc) {
104                 LASSERT(rc < 0);
105                 RETURN(rc);
106         }
107
108         RETURN(size);
109 }
110
111 static int do_remote_getfacl(struct mdt_thread_info *info, struct lu_buf *buf)
112 {
113         struct ptlrpc_request *req = mdt_info_req(info);
114         char *cmd;
115         int rc;
116         ENTRY;
117
118         if (!buf->lb_buf || (buf->lb_len != RMTACL_SIZE_MAX))
119                 RETURN(-EINVAL);
120
121         cmd = req_capsule_client_get(&info->mti_pill, &RMF_EADATA);
122         if (!cmd) {
123                 CERROR("missing getfacl command!\n");
124                 RETURN(-EFAULT);
125         }
126
127         rc = mdt_rmtacl_upcall(info, cmd, buf);
128         if (rc)
129                 CERROR("remote acl upcall failed: %d\n", rc);
130
131         lustre_shrink_reply(req, REPLY_REC_OFF + 1, strlen(buf->lb_buf) + 1, 0);
132         RETURN(rc ?: strlen(buf->lb_buf) + 1);
133 }
134
135 int mdt_getxattr(struct mdt_thread_info *info)
136 {
137         struct  mdt_body       *reqbody;
138         struct  mdt_body       *repbody = NULL;
139         struct  md_object      *next;
140         struct  lu_buf         *buf;
141         int                     easize, rc;
142         ENTRY;
143
144         LASSERT(info->mti_object != NULL);
145         LASSERT(lu_object_assert_exists(&info->mti_object->mot_obj.mo_lu));
146
147         CDEBUG(D_INODE, "getxattr "DFID"\n", PFID(&info->mti_body->fid1));
148
149         reqbody = req_capsule_client_get(&info->mti_pill, &RMF_MDT_BODY);
150         if (reqbody == NULL)
151                 RETURN(err_serious(-EFAULT));
152
153         rc = mdt_init_ucred(info, reqbody);
154         if (rc)
155                 RETURN(err_serious(rc));
156
157         easize = mdt_getxattr_pack_reply(info);
158         if (easize < 0)
159                 GOTO(out, rc = err_serious(easize));
160
161         repbody = req_capsule_server_get(&info->mti_pill, &RMF_MDT_BODY);
162         LASSERT(repbody != NULL);
163
164         /* No need further getxattr. */
165         if (easize == 0 || reqbody->eadatasize == 0)
166                 GOTO(out, rc = easize);
167
168         buf = &info->mti_buf;
169         buf->lb_buf = req_capsule_server_get(&info->mti_pill, &RMF_EADATA);
170         buf->lb_len = easize;
171         next = mdt_object_child(info->mti_object);
172
173         if (info->mti_body->valid & OBD_MD_FLXATTR) {
174                 char *xattr_name = req_capsule_client_get(&info->mti_pill,
175                                                           &RMF_NAME);
176                 CDEBUG(D_INODE, "getxattr %s\n", xattr_name);
177
178                 if (!strcmp(xattr_name, XATTR_NAME_LUSTRE_ACL))
179                         rc = do_remote_getfacl(info, buf);
180                 else
181                         rc = mo_xattr_get(info->mti_env, next, buf, xattr_name);
182
183                 if (rc < 0)
184                         CERROR("getxattr failed: %d\n", rc);
185         } else if (info->mti_body->valid & OBD_MD_FLXATTRLS) {
186                 CDEBUG(D_INODE, "listxattr\n");
187
188                 rc = mo_xattr_list(info->mti_env, next, buf);
189                 if (rc < 0)
190                         CERROR("listxattr failed: %d\n", rc);
191         } else
192                 LBUG();
193
194         EXIT;
195 out:
196         if (rc >= 0) {
197                 repbody->eadatasize = rc;
198                 rc = 0;
199         }
200         mdt_exit_ucred(info);
201         return rc;
202 }
203
204 /* return EADATA length to the caller. negative value means error */
205 static int mdt_setxattr_pack_reply(struct mdt_thread_info * info)
206 {
207         struct req_capsule     *pill = &info->mti_pill ;
208         __u64                   valid = info->mti_body->valid;
209         int                     rc = 0, rc1;
210
211         if ((valid & OBD_MD_FLXATTR) == OBD_MD_FLXATTR) {
212                 char *xattr_name;
213
214                 xattr_name = req_capsule_client_get(pill, &RMF_NAME);
215                 if (!xattr_name)
216                         return -EFAULT;
217
218                 if (!strcmp(xattr_name, XATTR_NAME_LUSTRE_ACL))
219                         rc = RMTACL_SIZE_MAX;
220         }
221
222         req_capsule_set_size(pill, &RMF_EADATA, RCL_SERVER, rc);
223
224         rc1 = req_capsule_pack(pill);
225
226         return rc = rc1 ? rc1 : rc;
227 }
228
229 static int do_remote_setfacl(struct mdt_thread_info *info)
230 {
231         struct ptlrpc_request *req = mdt_info_req(info);
232         struct lu_buf         *buf = &info->mti_buf;
233         char *cmd;
234         int rc;
235         ENTRY;
236
237         cmd = req_capsule_client_get(&info->mti_pill, &RMF_EADATA);
238         if (!cmd) {
239                 CERROR("missing setfacl command!\n");
240                 RETURN(-EFAULT);
241         }
242
243         buf->lb_buf = req_capsule_server_get(&info->mti_pill, &RMF_EADATA);
244         LASSERT(buf->lb_buf);
245         buf->lb_len = RMTACL_SIZE_MAX;
246
247         rc = mdt_rmtacl_upcall(info, cmd, buf);
248         if (rc)
249                 CERROR("remote acl upcall failed: %d\n", rc);
250
251         lustre_shrink_reply(req, REPLY_REC_OFF, strlen(buf->lb_buf) + 1, 0);
252         RETURN(rc);
253 }
254
255 int mdt_setxattr(struct mdt_thread_info *info)
256 {
257         struct ptlrpc_request   *req = mdt_info_req(info);
258         struct mdt_body         *reqbody;
259         const char               user_string[] = "user.";
260         const char               trust_string[] = "trusted.";
261         struct mdt_lock_handle  *lh;
262         struct req_capsule      *pill = &info->mti_pill;
263         struct mdt_object       *obj  = info->mti_object;
264         struct mdt_body         *body = (struct mdt_body *)info->mti_body;
265         const struct lu_env     *env  = info->mti_env;
266         struct md_object        *child  = mdt_object_child(obj);
267         struct lu_buf           *buf  = &info->mti_buf;
268         __u64                    valid  = body->valid;
269         char                    *xattr_name;
270         int                      xattr_len;
271         __u64                    lockpart;
272         int                      rc;
273         ENTRY;
274
275         CDEBUG(D_INODE, "setxattr "DFID"\n", PFID(&body->fid1));
276
277         if (MDT_FAIL_CHECK(OBD_FAIL_MDS_SETXATTR))
278                 RETURN(err_serious(-ENOMEM));
279
280         reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
281         if (reqbody == NULL)
282                 RETURN(err_serious(-EFAULT));
283
284         rc = mdt_init_ucred(info, reqbody);
285         if (rc)
286                 RETURN(err_serious(rc));
287
288         rc = mdt_setxattr_pack_reply(info);
289         if (rc < 0)
290                 GOTO(out, rc = err_serious(rc));
291
292         /* various sanity check for xattr name */
293         xattr_name = req_capsule_client_get(pill, &RMF_NAME);
294         if (!xattr_name)
295                 GOTO(out, rc = err_serious(-EFAULT));
296
297         CDEBUG(D_INODE, "%s xattr %s\n",
298                   body->valid & OBD_MD_FLXATTR ? "set" : "remove", xattr_name);
299
300         if (((valid & OBD_MD_FLXATTR) == OBD_MD_FLXATTR) &&
301             (!strcmp(xattr_name, XATTR_NAME_LUSTRE_ACL))) {
302                 rc = do_remote_setfacl(info);
303                 GOTO(out, rc);
304         }
305
306         if (strncmp(xattr_name, trust_string, sizeof(trust_string) - 1) == 0) {
307                 if (strcmp(xattr_name + 8, XATTR_NAME_LOV) == 0)
308                         GOTO(out, rc = -EACCES);
309         }
310
311         if (!(req->rq_export->exp_connect_flags & OBD_CONNECT_XATTR) &&
312             (strncmp(xattr_name, user_string, sizeof(user_string) - 1) == 0)) {
313                 GOTO(out, rc = -EOPNOTSUPP);
314         }
315
316         lockpart = MDS_INODELOCK_UPDATE;
317         if (!strcmp(xattr_name, XATTR_NAME_ACL_ACCESS))
318                 lockpart |= MDS_INODELOCK_LOOKUP;
319
320         lh = &info->mti_lh[MDT_LH_PARENT];
321         mdt_lock_reg_init(lh, LCK_PW);
322         rc = mdt_object_lock(info, obj, lh, lockpart, MDT_LOCAL_LOCK);
323         if (rc != 0)
324                 GOTO(out, rc);
325
326         if ((valid & OBD_MD_FLXATTR) == OBD_MD_FLXATTR) {
327                 char * xattr;
328                 if (!req_capsule_field_present(pill, &RMF_EADATA, RCL_CLIENT)) {
329                         CERROR("no xattr data supplied\n");
330                         GOTO(out_unlock, rc = -EFAULT);
331                 }
332
333                 xattr_len = req_capsule_get_size(pill, &RMF_EADATA, RCL_CLIENT);
334                 if (xattr_len) {
335                         int flags = 0;
336                         xattr = req_capsule_client_get(pill, &RMF_EADATA);
337
338                         if (body->flags & XATTR_REPLACE)
339                                 flags |= LU_XATTR_REPLACE;
340
341                         if (body->flags & XATTR_CREATE)
342                                 flags |= LU_XATTR_CREATE;
343
344                         mdt_fail_write(env, info->mti_mdt->mdt_bottom,
345                                        OBD_FAIL_MDS_SETXATTR_WRITE);
346
347                         buf->lb_buf = xattr;
348                         buf->lb_len = xattr_len;
349                         rc = mo_xattr_set(env, child, buf, xattr_name, flags);
350                 }
351         } else if ((valid & OBD_MD_FLXATTRRM) == OBD_MD_FLXATTRRM) {
352                 rc = mo_xattr_del(env, child, xattr_name);
353         } else {
354                 CERROR("valid bits: "LPX64"\n", body->valid);
355                 rc = -EINVAL;
356         }
357         EXIT;
358 out_unlock:
359         mdt_object_unlock(info, obj, lh, rc);
360 out:
361         mdt_exit_ucred(info);
362         return rc;
363 }