Whamcloud - gitweb
add some code for getattr & ldlm_intent_getattr ( #ifdef 'ed)
[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_reint.c
5  *  Lustre Metadata Target (mdt) reintegration routines
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 #include "mdt_internal.h"
38
39
40 /* return EADATA length to the caller. negative value means error */
41 static int mdt_getxattr_pack_reply(struct mdt_thread_info * info)
42 {
43         char *xattr_name;
44         int rc = -EOPNOTSUPP, rc2;
45         struct req_capsule *pill;
46         struct ptlrpc_request *req = mdt_info_req(info);
47
48         pill = &info->mti_pill;
49
50         /* Imagine how many bytes we need */
51         if (info->mti_body->valid & OBD_MD_FLXATTR) {
52                 xattr_name = req_capsule_client_get(pill, &RMF_NAME);
53                 if (!xattr_name) {
54                         CERROR("can't extract xattr name for getxattr\n");
55                         return -EFAULT;
56                 }
57
58                 if (!(req->rq_export->exp_connect_flags & OBD_CONNECT_XATTR) &&
59                     (strncmp(xattr_name, "user.", 5) == 0))
60                         return -EOPNOTSUPP;
61
62                 rc = mo_xattr_get(info->mti_ctxt, 
63                                   mdt_object_child(info->mti_object), 
64                                   NULL, 0, xattr_name);
65         } else if (info->mti_body->valid & OBD_MD_FLXATTRLS) {
66                 rc = mo_xattr_list(info->mti_ctxt, 
67                                    mdt_object_child(info->mti_object),
68                                    NULL, 0);
69         } else {
70                 CERROR("valid bits: "LPX64"\n", info->mti_body->valid);
71                 return -EINVAL;
72         }
73
74         if (rc < 0) {
75                 if (rc != -ENODATA && rc != -EOPNOTSUPP)
76                         CWARN("get EA size error: %d\n", rc);
77                 /* return empty to client */
78                 req_capsule_extend(&info->mti_pill, &RQF_MDS_SETXATTR);
79         } else {
80                 rc =  min_t(int, info->mti_body->eadatasize, rc);
81                 req_capsule_set_size(pill, &RMF_EADATA, RCL_SERVER, rc);
82         }
83
84         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETXATTR_PACK)) {
85                 CERROR("failed MDS_GETXATTR_PACK test\n");
86                 req->rq_status = -ENOMEM;
87                 return -ENOMEM;
88         }
89
90         rc2 = req_capsule_pack(pill);
91         if (rc2)
92                 return rc2;
93         return rc;
94 }
95
96
97 int mdt_getxattr(struct mdt_thread_info *info)
98 {
99         int     rc;
100         struct  md_object *next;
101         char   *buf;
102         int     buflen;
103         struct  mdt_body *rep_body;
104
105         ENTRY;
106
107         LASSERT(info->mti_object != NULL);
108         LASSERT(lu_object_exists(info->mti_ctxt,
109                                  &info->mti_object->mot_obj.mo_lu));
110         ENTRY;
111
112         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETXATTR_PACK)) {
113                 CERROR(LUSTRE_MDT0_NAME": getxattr lustre_pack_reply failed\n");
114                 RETURN(rc = -ENOMEM);
115         }
116
117         next = mdt_object_child(info->mti_object);
118
119         buflen = mdt_getxattr_pack_reply(info);
120         if (buflen < 0)
121                 RETURN(rc = buflen);
122         buf = req_capsule_server_get(&info->mti_pill,
123                                      &RMF_EADATA);
124         rep_body = req_capsule_server_get(&info->mti_pill,
125                                           &RMF_MDT_BODY);
126
127         if (info->mti_body->valid & OBD_MD_FLXATTR) {
128                 char *xattr_name = req_capsule_client_get(&info->mti_pill, 
129                                                           &RMF_NAME);
130                 CDEBUG(S_MDS, "getxattr %s\n", xattr_name);
131
132                 rc = mo_xattr_get(info->mti_ctxt, next, 
133                                    buf, buflen, xattr_name);
134
135                 if (rc < 0 && rc != -ENODATA && rc != -EOPNOTSUPP &&
136                     rc != -ERANGE)
137                         CDEBUG(S_MDS, "getxattr failed: %d\n", rc);
138         } else if (info->mti_body->valid & OBD_MD_FLXATTRLS) {
139                 CDEBUG(S_MDS, "listxattr\n");
140
141                 rc = mo_xattr_list(info->mti_ctxt, next, buf, buflen);
142                 if (rc < 0)
143                         CDEBUG(D_OTHER, "listxattr failed: %d\n", rc);
144         } else
145                 LBUG();
146
147         if (rc >= 0) {
148                 rep_body->eadatasize = rc;
149                 rc = 0;
150         }
151
152         RETURN(rc);
153         return 0;
154 }
155
156
157 int mdt_setxattr(struct mdt_thread_info *info)
158 {
159         int    rc;
160         char  *xattr_name;
161         int    xattr_len;
162         struct ptlrpc_request *req = mdt_info_req(info);
163         __u64 lockpart;
164         struct mdt_lock_handle *lh;
165         ENTRY;
166
167         lh = &info->mti_lh[MDT_LH_PARENT];
168         lh->mlh_mode = LCK_EX;
169
170 /*        if (req->rq_reqmsg->bufcount < 2)
171  *                RETURN(-EFAULT);
172  */
173         DEBUG_REQ(D_INODE, req, "setxattr "DFID3"\n",
174                   PFID3(&info->mti_body->fid1));
175
176 /*        MDS_CHECK_RESENT(req, mds_reconstruct_generic(req)); */
177
178         lockpart = MDS_INODELOCK_UPDATE;
179
180         /* various sanity check for xattr name */
181         xattr_name = req_capsule_client_get(&info->mti_pill, &RMF_NAME);
182         if (!xattr_name) {
183                 CERROR("can't extract xattr name\n");
184                 GOTO(out, rc = -EPROTO);
185         }
186
187         CDEBUG(D_INODE, "%sxattr %s\n",
188                   info->mti_body->valid & OBD_MD_FLXATTR ? "set" : "remove",
189                   xattr_name);
190
191         if (strncmp(xattr_name, "trusted.", 8) == 0) {
192                 if (strcmp(xattr_name + 8, "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.", 5) == 0)) {
198                 GOTO(out, rc = -EOPNOTSUPP);
199         }
200
201         if (!strcmp(xattr_name, XATTR_NAME_ACL_ACCESS))
202                 lockpart |= MDS_INODELOCK_LOOKUP;
203
204         rc = mdt_object_lock(info->mti_mdt->mdt_namespace, info->mti_object, 
205                                  lh, lockpart);
206         if (rc != 0)
207                 GOTO(out, rc);
208
209
210         if (info->mti_body->valid & OBD_MD_FLXATTR) {
211                 char * xattr; 
212                 if (!req_capsule_field_present(&info->mti_pill, &RMF_EADATA)) {
213                         CERROR("no xattr data supplied\n");
214                         GOTO(out_unlock, rc = -EFAULT);
215                 }
216
217                 xattr_len = req_capsule_get_size(&info->mti_pill, 
218                                                  &RMF_EADATA, RCL_CLIENT);
219                 if (xattr_len) {
220                         xattr = req_capsule_client_get(&info->mti_pill, 
221                                                        &RMF_EADATA);
222
223                         rc = mo_xattr_set(info->mti_ctxt, 
224                                           mdt_object_child(info->mti_object),
225                                           xattr, xattr_len, xattr_name);
226                 }
227         } else if (info->mti_body->valid & OBD_MD_FLXATTRRM) {
228                 rc = mo_xattr_del(info->mti_ctxt, 
229                                      mdt_object_child(info->mti_object),
230                                      xattr_name);
231         } else {
232                 CERROR("valid bits: "LPX64"\n", info->mti_body->valid);
233                 rc = -EINVAL;
234         }
235
236 out_unlock:
237         mdt_object_unlock(info->mti_mdt->mdt_namespace, 
238                           info->mti_object, lh);
239 out:
240         return rc;
241 }