Whamcloud - gitweb
xattr support in md/dt stack
[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                     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,
68                             sizeof(user_string) - 1) == 0)
69                         return -EOPNOTSUPP;
70
71                 rc = mo_xattr_get(info->mti_ctxt,
72                                   mdt_object_child(info->mti_object),
73                                   NULL, 0, xattr_name);
74         } else if ((valid & OBD_MD_FLXATTRLS) == OBD_MD_FLXATTRLS) {
75                 rc = mo_xattr_list(info->mti_ctxt,
76                                    mdt_object_child(info->mti_object),
77                                    NULL, 0);
78         } else {
79                 CERROR("valid bits: "LPX64"\n", info->mti_body->valid);
80                 return -EINVAL;
81         }
82
83         if (rc < 0) {
84                 if (rc != -ENODATA && rc != -EOPNOTSUPP) {
85                         CWARN("get EA size error: %d\n", rc);
86                         return rc;
87                 }
88                 rc = 0;
89         } else {
90                 rc =  min_t(int, info->mti_body->eadatasize, rc);
91         }
92         req_capsule_set_size(pill, &RMF_EADATA, RCL_SERVER, rc);
93
94         rc = req_capsule_pack(pill);
95         return rc;
96 }
97
98
99 int mdt_getxattr(struct mdt_thread_info *info)
100 {
101         int     rc;
102         struct  md_object *next;
103         char   *buf;
104         int     buflen;
105         struct  mdt_body *rep_body;
106
107         ENTRY;
108
109         LASSERT(info->mti_object != NULL);
110         LASSERT(lu_object_assert_exists(info->mti_ctxt,
111                                  &info->mti_object->mot_obj.mo_lu));
112
113         CDEBUG(D_INODE, "getxattr "DFID3"\n",
114                         PFID3(&info->mti_body->fid1));
115
116         if (MDT_FAIL_CHECK(OBD_FAIL_MDS_GETXATTR_PACK))
117                 RETURN(-ENOMEM);
118
119         next = mdt_object_child(info->mti_object);
120
121         rc = mdt_getxattr_pack_reply(info);
122         if (rc != 0)
123                 RETURN(rc);
124
125         buf = req_capsule_server_get(&info->mti_pill, &RMF_EADATA);
126         rep_body = req_capsule_server_get(&info->mti_pill, &RMF_MDT_BODY);
127         buflen = req_capsule_get_size(&info->mti_pill, &RMF_EADATA, RCL_SERVER);
128         if (buflen == 0)
129                 GOTO(no_xattr, rc = 0);
130
131         if (info->mti_body->valid & OBD_MD_FLXATTR) {
132                 char *xattr_name = req_capsule_client_get(&info->mti_pill,
133                                                           &RMF_NAME);
134                 CDEBUG(D_INODE, "getxattr %s\n", xattr_name);
135
136                 rc = mo_xattr_get(info->mti_ctxt, next,
137                                   buf, buflen, xattr_name);
138
139                 if (rc < 0 && rc != -ENODATA && rc != -EOPNOTSUPP &&
140                     rc != -ERANGE)
141                         CDEBUG(D_INODE, "getxattr failed: %d\n", rc);
142         } else if (info->mti_body->valid & OBD_MD_FLXATTRLS) {
143                 CDEBUG(D_INODE, "listxattr\n");
144
145                 rc = mo_xattr_list(info->mti_ctxt, next, buf, buflen);
146                 if (rc < 0)
147                         CDEBUG(D_OTHER, "listxattr failed: %d\n", rc);
148         } else
149                 LBUG();
150
151         if (rc >= 0) {
152 no_xattr:
153                 rep_body->eadatasize = rc;
154                 rc = 0;
155         }
156
157         RETURN(rc);
158 }
159
160
161 int mdt_setxattr(struct mdt_thread_info *info)
162 {
163         struct ptlrpc_request  *req = mdt_info_req(info);
164         const char              user_string[] = "user.";
165         const char              trust_string[] = "trusted.";
166         struct mdt_lock_handle *lh;
167         struct req_capsule       *pill = &info->mti_pill;
168         struct mdt_object        *obj  = info->mti_object;
169         const struct mdt_body    *body = info->mti_body;
170         const struct lu_context  *ctx  = info->mti_ctxt;
171         struct md_object       *child  = mdt_object_child(obj);
172         __u64                   valid  = body->valid;
173         char                   *xattr_name;
174         int                     xattr_len;
175         __u64                   lockpart;
176         int                     rc;
177         ENTRY;
178
179         CDEBUG(D_INODE, "setxattr "DFID3"\n", PFID3(&body->fid1));
180
181         if (MDT_FAIL_CHECK(OBD_FAIL_MDS_SETXATTR))
182                 RETURN(-ENOMEM);
183
184         /* various sanity check for xattr name */
185         xattr_name = req_capsule_client_get(pill, &RMF_NAME);
186         if (!xattr_name)
187                 GOTO(out, rc = -EFAULT);
188
189         CDEBUG(D_INODE, "%s xattr %s\n",
190                   body->valid & OBD_MD_FLXATTR ? "set" : "remove", xattr_name);
191
192         if (strncmp(xattr_name, trust_string, sizeof(trust_string) - 1) == 0) {
193                 if (strcmp(xattr_name + 8, XATTR_NAME_LOV) == 0)
194                         GOTO(out, rc = -EACCES);
195         }
196
197         if (!(req->rq_export->exp_connect_flags & OBD_CONNECT_XATTR) &&
198             (strncmp(xattr_name, user_string, sizeof(user_string) - 1) == 0)) {
199                 GOTO(out, rc = -EOPNOTSUPP);
200         }
201
202         lockpart = MDS_INODELOCK_UPDATE;
203         if (!strcmp(xattr_name, XATTR_NAME_ACL_ACCESS))
204                 lockpart |= MDS_INODELOCK_LOOKUP;
205
206         lh = &info->mti_lh[MDT_LH_PARENT];
207         lh->mlh_mode = LCK_EX;
208         rc = mdt_object_lock(info, obj, lh, lockpart);
209         if (rc != 0)
210                 GOTO(out, rc);
211
212         if ((valid & OBD_MD_FLXATTR) == OBD_MD_FLXATTR) {
213                 char * xattr;
214                 if (!req_capsule_field_present(pill, &RMF_EADATA)) {
215                         CERROR("no xattr data supplied\n");
216                         GOTO(out_unlock, rc = -EFAULT);
217                 }
218
219                 xattr_len = req_capsule_get_size(pill, &RMF_EADATA, RCL_CLIENT);
220                 if (xattr_len) {
221                         int flags = 0;
222                         xattr = req_capsule_client_get(pill, &RMF_EADATA);
223
224                         if (body->flags & XATTR_REPLACE)
225                                 flags |= LU_XATTR_REPLACE;
226
227                         if (body->flags & XATTR_CREATE)
228                                 flags |= LU_XATTR_CREATE;
229
230                         rc = mo_xattr_set(ctx, child, xattr,
231                                           xattr_len, xattr_name, flags);
232                 }
233         } else if ((valid & OBD_MD_FLXATTRRM) == OBD_MD_FLXATTRRM) {
234                 rc = mo_xattr_del(ctx, child, xattr_name);
235         } else {
236                 CERROR("valid bits: "LPX64"\n", body->valid);
237                 rc = -EINVAL;
238         }
239         EXIT;
240 out_unlock:
241         mdt_object_unlock(info, obj, lh, rc);
242 out:
243         return rc;
244 }