Whamcloud - gitweb
iam: 0. add optional calls to schedule() to improve pdirops testing. 1. fix locking...
[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
57         if (MDT_FAIL_CHECK(OBD_FAIL_MDS_GETXATTR_PACK))
58                 return -ENOMEM;
59
60         /* Determine how many bytes we need */
61         if ((valid & OBD_MD_FLXATTR) == OBD_MD_FLXATTR) {
62                 xattr_name = req_capsule_client_get(pill, &RMF_NAME);
63                 if (!xattr_name)
64                         return -EFAULT;
65
66                 if (!(req->rq_export->exp_connect_flags & OBD_CONNECT_XATTR) &&
67                     !strncmp(xattr_name, user_string, sizeof(user_string) - 1))
68                         return -EOPNOTSUPP;
69
70                 if (!strcmp(xattr_name, XATTR_NAME_LUSTRE_ACL))
71                         size = RMTACL_SIZE_MAX;
72                 else
73                         size = mo_xattr_get(info->mti_env,
74                                             mdt_object_child(info->mti_object),
75                                             &LU_BUF_NULL, xattr_name);
76         } else if ((valid & OBD_MD_FLXATTRLS) == OBD_MD_FLXATTRLS) {
77                 size = mo_xattr_list(info->mti_env,
78                                      mdt_object_child(info->mti_object),
79                                      &LU_BUF_NULL);
80         } else {
81                 CERROR("valid bits: "LPX64"\n", info->mti_body->valid);
82                 return -EINVAL;
83         }
84
85         if (size < 0) {
86                 if (size != -ENODATA && size != -EOPNOTSUPP)
87                         CERROR("get EA size error: %d\n", size);
88                 return size;
89         }
90
91         if (info->mti_body->eadatasize != 0 &&
92             info->mti_body->eadatasize < size)
93                 return -ERANGE;
94
95         req_capsule_set_size(pill, &RMF_EADATA, RCL_SERVER,
96                              min_t(int, size, info->mti_body->eadatasize));
97
98         rc = req_capsule_pack(pill);
99         if (rc) {
100                 LASSERT(rc < 0);
101                 return rc;
102         }
103
104         return size;
105 }
106
107 static int do_remote_getfacl(struct mdt_thread_info *info,
108                              struct lu_fid *fid, struct lu_buf *buf)
109 {
110         struct ptlrpc_request *req = mdt_info_req(info);
111         char *cmd;
112         int rc;
113         ENTRY;
114
115         if (!buf->lb_buf || (buf->lb_len != RMTACL_SIZE_MAX))
116                 RETURN(-EINVAL);
117
118         cmd = req_capsule_client_get(&info->mti_pill, &RMF_EADATA);
119         if (!cmd) {
120                 CERROR("missing getfacl command!\n");
121                 RETURN(-EFAULT);
122         }
123
124         rc = mdt_rmtacl_upcall(info, fid_oid(fid), cmd, buf);
125         if (rc)
126                 CERROR("remote acl upcall failed: %d\n", rc);
127
128         lustre_shrink_reply(req, REPLY_REC_OFF + 1, strlen(buf->lb_buf) + 1, 0);
129         RETURN(rc ?: strlen(buf->lb_buf) + 1);
130 }
131
132 int mdt_getxattr(struct mdt_thread_info *info)
133 {
134         struct  mdt_body       *reqbody;
135         struct  mdt_body       *repbody;
136         struct  md_object      *next;
137         struct  lu_buf         *buf;
138         int                     easize, rc;
139         ENTRY;
140
141         LASSERT(info->mti_object != NULL);
142         LASSERT(lu_object_assert_exists(&info->mti_object->mot_obj.mo_lu));
143
144         CDEBUG(D_INODE, "getxattr "DFID"\n", PFID(&info->mti_body->fid1));
145
146         reqbody = req_capsule_client_get(&info->mti_pill, &RMF_MDT_BODY);
147         if (reqbody == NULL)
148                 RETURN(err_serious(-EFAULT));
149
150         easize = mdt_getxattr_pack_reply(info);
151         if (easize < 0)
152                 RETURN(err_serious(easize));
153
154         repbody = req_capsule_server_get(&info->mti_pill, &RMF_MDT_BODY);
155         LASSERT(repbody);
156
157         rc = mdt_init_ucred(info, reqbody);
158         if (rc)
159                 RETURN(rc);
160
161         /* no need further getxattr */
162         if (easize == 0 || reqbody->eadatasize == 0)
163                 GOTO(out, rc = easize);
164
165         buf = &info->mti_buf;
166         buf->lb_buf = req_capsule_server_get(&info->mti_pill, &RMF_EADATA);
167         buf->lb_len = easize;
168         next = mdt_object_child(info->mti_object);
169
170         if (info->mti_body->valid & OBD_MD_FLXATTR) {
171                 char *xattr_name = req_capsule_client_get(&info->mti_pill,
172                                                           &RMF_NAME);
173                 CDEBUG(D_INODE, "getxattr %s\n", xattr_name);
174
175                 if (!strcmp(xattr_name, XATTR_NAME_LUSTRE_ACL)) {
176                         struct mdt_body *body =
177                                         (struct mdt_body *)info->mti_body;
178
179                         rc = do_remote_getfacl(info, &body->fid1, buf);
180                 } else {
181                         rc = mo_xattr_get(info->mti_env, next, buf, xattr_name);
182                 }
183
184                 if (rc < 0)
185                         CERROR("getxattr failed: %d\n", rc);
186         } else if (info->mti_body->valid & OBD_MD_FLXATTRLS) {
187                 CDEBUG(D_INODE, "listxattr\n");
188
189                 rc = mo_xattr_list(info->mti_env, next, buf);
190                 if (rc < 0)
191                         CERROR("listxattr failed: %d\n", rc);
192         } else
193                 LBUG();
194
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, struct lu_fid *fid)
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, fid_oid(fid), 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         rc = mdt_setxattr_pack_reply(info);
281         if (rc < 0)
282                 RETURN(err_serious(rc));
283
284         reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
285         if (reqbody == NULL)
286                 RETURN(err_serious(-EFAULT));
287
288         rc = mdt_init_ucred(info, reqbody);
289         if (rc)
290                 RETURN(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, &body->fid1);
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_pdo_init(lh, LCK_PW, NULL, 0);
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 }