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