Whamcloud - gitweb
- merge with 1_5,some fixes.
[fs/lustre-release.git] / lustre / mds / mds_xattr.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  linux/mds/mds_xattr.c
5  *  Lustre Metadata Server (mds) extended attributes handling
6  *
7  *  Copyright (C) 2004-2005 Cluster File Systems, Inc.
8  *
9  *   This file is part of the Lustre file system, http://www.lustre.org
10  *   Lustre is a trademark of Cluster File Systems, Inc.
11  *
12  *   You may have signed or agreed to another license before downloading
13  *   this software.  If so, you are bound by the terms and conditions
14  *   of that agreement, and the following does not apply to you.  See the
15  *   LICENSE file included with this distribution for more information.
16  *
17  *   If you did not agree to a different license, then this copy of Lustre
18  *   is open source software; you can redistribute it and/or modify it
19  *   under the terms of version 2 of the GNU General Public License as
20  *   published by the Free Software Foundation.
21  *
22  *   In either case, Lustre is distributed in the hope that it will be
23  *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
24  *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  *   license text for more details.
26  */
27
28 #ifndef EXPORT_SYMTAB
29 # define EXPORT_SYMTAB
30 #endif
31 #define DEBUG_SUBSYSTEM S_MDS
32
33 #include <linux/fs.h>
34 #include <obd_support.h>
35 #include <obd_class.h>
36 #include <obd.h>
37 #include <lustre_lib.h>
38 #include <lustre/lustre_idl.h>
39 #include <lustre_mds.h>
40 #include <lustre_dlm.h>
41 #include <lustre_fsfilt.h>
42 #include <lustre_ucache.h>
43
44 #include "mds_internal.h" 
45
46 #ifndef XATTR_NAME_ACL_ACCESS
47 #define XATTR_NAME_ACL_ACCESS   "system.posix_acl_access"
48 #endif
49
50 static int mds_getxattr_pack_msg(struct ptlrpc_request *req,
51                                  struct dentry *de,
52                                  struct mds_body *body)
53 {
54         struct inode *inode = de->d_inode;
55         char *xattr_name;
56         int size[3] = { sizeof(struct ptlrpc_body), sizeof(*body) };
57         int bufcnt = 2, rc = -EOPNOTSUPP, rc2;
58
59         if (inode == NULL)
60                 return -ENOENT;
61
62         if (body->valid & OBD_MD_FLXATTR) {
63                 xattr_name = lustre_msg_string(req->rq_reqmsg, REQ_REC_OFF+1,0);
64                 if (!xattr_name) {
65                         CERROR("can't extract xattr name\n");
66                         return -EFAULT;
67                 }
68
69                 if (!(req->rq_export->exp_connect_flags & OBD_CONNECT_XATTR) &&
70                     (strncmp(xattr_name, "user.", 5) == 0))
71                         return -EOPNOTSUPP;
72
73                 if (inode->i_op && inode->i_op->getxattr)
74                         rc = inode->i_op->getxattr(de, xattr_name, NULL, 0);
75         } else if (body->valid & OBD_MD_FLXATTRLS) {
76                 if (inode->i_op && inode->i_op->listxattr)
77                         rc = inode->i_op->listxattr(de, NULL, 0);
78         } else {
79                 CERROR("valid bits: "LPX64"\n", body->valid);
80                 return -EINVAL;
81         }
82
83         if (rc < 0) {
84                 if (rc != -ENODATA && rc != -EOPNOTSUPP)
85                         CWARN("get inode %lu EA size error: %d\n",
86                               inode->i_ino, rc);
87                 bufcnt = 1;
88         } else {
89                 size[bufcnt++] = min_t(int, body->eadatasize, rc);
90         }
91
92         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETXATTR_PACK)) {
93                 CERROR("failed MDS_GETXATTR_PACK test\n");
94                 req->rq_status = -ENOMEM;
95                 return -ENOMEM;
96         }
97
98         rc2 = lustre_pack_reply(req, bufcnt, size, NULL);
99         if (rc2)
100                 return rc2;
101
102         if (rc < 0)
103                 req->rq_status = rc;
104         return 0;
105 }
106
107 static int mds_getxattr_internal(struct obd_device *obd,
108                                  struct dentry *dentry,
109                                  struct ptlrpc_request *req,
110                                  struct mds_body *reqbody)
111 {
112         struct mds_body *repbody;
113         struct inode *inode = dentry->d_inode;
114         char *xattr_name;
115         void *buf = NULL;
116         int buflen, rc = -EOPNOTSUPP;
117         ENTRY;
118
119         if (inode == NULL)
120                 GOTO(out, rc = -ENOENT);
121
122         repbody = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
123                                  sizeof(*repbody));
124         LASSERT(repbody != NULL);
125
126         buflen = lustre_msg_buflen(req->rq_repmsg, REPLY_REC_OFF + 1);
127         if (buflen)
128                 buf = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF + 1, buflen);
129
130         if (reqbody->valid & OBD_MD_FLXATTR) {
131                 xattr_name = lustre_msg_string(req->rq_reqmsg, REQ_REC_OFF+1,0);
132                 DEBUG_REQ(D_INODE, req, "getxattr %s\n", xattr_name);
133
134                 if (inode->i_op && inode->i_op->getxattr) {
135                         lock_24kernel();
136                         rc = inode->i_op->getxattr(dentry, xattr_name,
137                                                    buf, buflen);
138                         unlock_24kernel();
139                 }
140
141                 if (rc < 0 && rc != -ENODATA && rc != -EOPNOTSUPP &&
142                     rc != -ERANGE)
143                         CDEBUG(D_OTHER, "getxattr failed: %d\n", rc);
144         } else if (reqbody->valid & OBD_MD_FLXATTRLS) {
145                 DEBUG_REQ(D_INODE, req, "listxattr\n");
146
147                 if (inode->i_op && inode->i_op->listxattr) {
148                         lock_24kernel();
149                         rc = inode->i_op->listxattr(dentry, buf, buflen);
150                         unlock_24kernel();
151                 }
152                 if (rc < 0)
153                         CDEBUG(D_OTHER, "listxattr failed: %d\n", rc);
154         } else
155                 LBUG();
156
157         if (rc >= 0) {
158                 repbody->eadatasize = rc;
159                 rc = 0;
160         }
161 out:
162         req->rq_status = rc;
163         RETURN(0);
164 }
165
166 int mds_getxattr(struct ptlrpc_request *req)
167 {
168         struct mds_obd *mds = mds_req2mds(req);
169         struct obd_device *obd = req->rq_export->exp_obd;
170         struct lvfs_run_ctxt saved;
171         struct dentry *de;
172         struct mds_body *body;
173         struct lvfs_ucred uc = { NULL, };
174         int rc = 0;
175         ENTRY;
176
177         body = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*body),
178                                   lustre_swab_mds_body);
179         if (body == NULL)
180                 RETURN(-EFAULT);
181
182         rc = mds_init_ucred(&uc, req, REQ_REC_OFF);
183         if (rc)
184                 GOTO(out_ucred, rc);
185
186         push_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
187         de = mds_fid2dentry(mds, &body->fid1, NULL);
188         if (IS_ERR(de)) {
189                 rc = req->rq_status = PTR_ERR(de);
190                 GOTO(out_pop, rc);
191         }
192
193         rc = mds_getxattr_pack_msg(req, de, body);
194         if (rc != 0 || req->rq_status)
195                 GOTO(out_dput, rc);
196
197         rc = mds_getxattr_internal(obd, de, req, body);
198
199 out_dput:
200         l_dput(de);
201 out_pop:
202         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
203 out_ucred:
204         mds_exit_ucred(&uc, mds);
205         return rc;
206 }
207
208 /*
209  * alwasy return 0, and set req->rq_status as error number in case
210  * of failures.
211  */
212 static
213 int mds_setxattr_internal(struct ptlrpc_request *req, struct mds_body *body)
214 {
215         struct mds_obd *mds = mds_req2mds(req);
216         struct obd_device *obd = req->rq_export->exp_obd;
217         struct dentry *de;
218         struct inode *inode = NULL;
219         struct lustre_handle lockh;
220         void *handle = NULL;
221         char *xattr_name;
222         char *xattr = NULL;
223         int xattrlen;
224         int rc = -EOPNOTSUPP, err = 0;
225         __u64 lockpart;
226         ENTRY;
227
228         LASSERT(body);
229
230         DEBUG_REQ(D_INODE, req, "setxattr "LPU64"/%u",
231                   body->fid1.id, body->fid1.generation);
232
233         MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
234
235         lockpart = MDS_INODELOCK_UPDATE;
236
237         /* various sanity check for xattr name */
238         xattr_name = lustre_msg_string(req->rq_reqmsg, REQ_REC_OFF + 1, 0);
239         if (!xattr_name) {
240                 CERROR("can't extract xattr name\n");
241                 GOTO(out, rc = -EPROTO);
242         }
243
244         DEBUG_REQ(D_INODE, req, "%sxattr %s\n",
245                   body->valid & OBD_MD_FLXATTR ? "set" : "remove",
246                   xattr_name);
247
248         if (strncmp(xattr_name, "trusted.", 8) == 0) {
249                 if (strcmp(xattr_name + 8, XATTR_LUSTRE_MDS_LOV_EA) == 0)
250                         GOTO(out, rc = -EACCES);
251         }
252
253         if (!(req->rq_export->exp_connect_flags & OBD_CONNECT_XATTR) &&
254             (strncmp(xattr_name, "user.", 5) == 0)) {
255                 GOTO(out, rc = -EOPNOTSUPP);
256         }
257
258         if (!strcmp(xattr_name, XATTR_NAME_ACL_ACCESS))
259                 lockpart |= MDS_INODELOCK_LOOKUP;
260
261         de = mds_fid2locked_dentry(obd, &body->fid1, NULL, LCK_EX,
262                                    &lockh, lockpart);
263         if (IS_ERR(de))
264                 GOTO(out, rc = PTR_ERR(de));
265
266         inode = de->d_inode;
267         LASSERT(inode);
268
269         OBD_FAIL_WRITE(OBD_FAIL_MDS_SETXATTR_WRITE, inode->i_sb);
270
271         /* filter_op simply use setattr one */
272         handle = fsfilt_start(obd, inode, FSFILT_OP_SETATTR, NULL);
273         if (IS_ERR(handle))
274                 GOTO(out_dput, rc = PTR_ERR(handle));
275
276         if (body->valid & OBD_MD_FLXATTR) {
277                 if (inode->i_op && inode->i_op->setxattr) {
278                         if (lustre_msg_bufcount(req->rq_reqmsg) < 4) {
279                                 CERROR("no xattr data supplied\n");
280                                 GOTO(out_trans, rc = -EFAULT);
281                         }
282
283                         xattrlen = lustre_msg_buflen(req->rq_reqmsg,
284                                                      REQ_REC_OFF + 2);
285                         if (xattrlen)
286                                 xattr = lustre_msg_buf(req->rq_reqmsg,
287                                                        REQ_REC_OFF+2, xattrlen);
288
289                         LOCK_INODE_MUTEX(inode);
290                         lock_24kernel();
291                         rc = inode->i_op->setxattr(de, xattr_name, xattr,
292                                                    xattrlen, body->flags);
293                         unlock_24kernel();
294                         UNLOCK_INODE_MUTEX(inode);
295                 }
296         } else if (body->valid & OBD_MD_FLXATTRRM) {
297                 if (inode->i_op && inode->i_op->removexattr) {
298                         LOCK_INODE_MUTEX(inode);
299                         lock_24kernel();
300                         rc = inode->i_op->removexattr(de, xattr_name);
301                         unlock_24kernel();
302                         UNLOCK_INODE_MUTEX(inode);
303                 }
304         } else {
305                 CERROR("valid bits: "LPX64"\n", body->valid);
306                 rc = -EINVAL;
307         }
308
309         LASSERT(rc <= 0);
310 out_trans:
311         err = mds_finish_transno(mds, inode, handle, req, rc, 0);
312
313 out_dput:
314         l_dput(de);
315         if (rc)
316                 ldlm_lock_decref(&lockh, LCK_EX);
317         else
318                 ptlrpc_save_lock (req, &lockh, LCK_EX);
319
320         if (err && !rc)
321                 rc = err;
322 out:
323         req->rq_status = rc;
324         return 0;
325 }
326
327 int mds_setxattr(struct ptlrpc_request *req)
328 {
329         struct mds_obd *mds = mds_req2mds(req);
330         struct obd_device *obd = req->rq_export->exp_obd;
331         struct lvfs_run_ctxt saved;
332         struct mds_body *body;
333         struct lvfs_ucred uc = { NULL, };
334         int rc;
335         ENTRY;
336
337         body = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*body),
338                                   lustre_swab_mds_body);
339         if (body == NULL)
340                 RETURN(-EFAULT);
341
342         if (lustre_msg_bufcount(req->rq_reqmsg) < 3)
343                 RETURN(-EFAULT);
344
345         rc = mds_init_ucred(&uc, req, REQ_REC_OFF);
346         if (rc)
347                 GOTO(out_ucred, rc);
348
349         push_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
350
351         rc = lustre_pack_reply(req, 1, NULL, NULL);
352         if (rc)
353                 GOTO(out_pop, rc);
354
355         rc = mds_setxattr_internal(req, body);
356
357 out_pop:
358         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
359 out_ucred:
360         mds_exit_ucred(&uc, mds);
361         return rc;
362 }