Whamcloud - gitweb
- make HEAD from b_post_cmd3
[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", 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");
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 = {0,};
174         int rc = 0;
175         ENTRY;
176
177         mds_counter_incr(req->rq_export, LPROC_MDS_GETXATTR);
178
179         body = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*body),
180                                   lustre_swab_mds_body);
181         if (body == NULL)
182                 RETURN(-EFAULT);
183
184         rc = mds_init_ucred(&uc, req, REQ_REC_OFF);
185         if (rc)
186                 GOTO(out_ucred, rc);
187
188         push_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
189         de = mds_fid2dentry(mds, &body->fid1, NULL);
190         if (IS_ERR(de)) {
191                 rc = req->rq_status = PTR_ERR(de);
192                 GOTO(out_pop, rc);
193         }
194
195         rc = mds_getxattr_pack_msg(req, de, body);
196         if (rc != 0 || req->rq_status)
197                 GOTO(out_dput, rc);
198
199         rc = mds_getxattr_internal(obd, de, req, body);
200
201 out_dput:
202         l_dput(de);
203 out_pop:
204         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
205 out_ucred:
206         mds_exit_ucred(&uc, mds);
207         return rc;
208 }
209
210 /*
211  * alwasy return 0, and set req->rq_status as error number in case
212  * of failures.
213  */
214 static
215 int mds_setxattr_internal(struct ptlrpc_request *req, struct mds_body *body)
216 {
217         struct mds_obd *mds = mds_req2mds(req);
218         struct obd_device *obd = req->rq_export->exp_obd;
219         struct dentry *de;
220         struct inode *inode = NULL;
221         struct lustre_handle lockh;
222         void *handle = NULL;
223         char *xattr_name;
224         char *xattr = NULL;
225         int xattrlen;
226         int rc = -EOPNOTSUPP, err = 0;
227         __u64 lockpart;
228         ENTRY;
229
230         LASSERT(body);
231
232         DEBUG_REQ(D_INODE, req, "setxattr "LPU64"/%u",
233                   body->fid1.id, body->fid1.generation);
234
235         MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
236
237         lockpart = MDS_INODELOCK_UPDATE;
238
239         /* various sanity check for xattr name */
240         xattr_name = lustre_msg_string(req->rq_reqmsg, REQ_REC_OFF + 1, 0);
241         if (!xattr_name) {
242                 CERROR("can't extract xattr name\n");
243                 GOTO(out, rc = -EPROTO);
244         }
245
246         DEBUG_REQ(D_INODE, req, "%sxattr %s",
247                   body->valid & OBD_MD_FLXATTR ? "set" : "remove",
248                   xattr_name);
249
250         if (strncmp(xattr_name, "trusted.", 8) == 0) {
251                 if (strcmp(xattr_name + 8, XATTR_LUSTRE_MDS_LOV_EA) == 0)
252                         GOTO(out, rc = -EACCES);
253         }
254
255         if (!(req->rq_export->exp_connect_flags & OBD_CONNECT_XATTR) &&
256             (strncmp(xattr_name, "user.", 5) == 0)) {
257                 GOTO(out, rc = -EOPNOTSUPP);
258         }
259
260         if (!strcmp(xattr_name, XATTR_NAME_ACL_ACCESS))
261                 lockpart |= MDS_INODELOCK_LOOKUP;
262
263         de = mds_fid2locked_dentry(obd, &body->fid1, NULL, LCK_EX,
264                                    &lockh, lockpart);
265         if (IS_ERR(de))
266                 GOTO(out, rc = PTR_ERR(de));
267
268         inode = de->d_inode;
269         LASSERT(inode);
270
271         OBD_FAIL_WRITE(OBD_FAIL_MDS_SETXATTR_WRITE, inode->i_sb);
272
273         /* filter_op simply use setattr one */
274         handle = fsfilt_start(obd, inode, FSFILT_OP_SETATTR, NULL);
275         if (IS_ERR(handle))
276                 GOTO(out_dput, rc = PTR_ERR(handle));
277
278         if (body->valid & OBD_MD_FLXATTR) {
279                 if (inode->i_op && inode->i_op->setxattr) {
280                         if (lustre_msg_bufcount(req->rq_reqmsg) < 4) {
281                                 CERROR("no xattr data supplied\n");
282                                 GOTO(out_trans, rc = -EFAULT);
283                         }
284
285                         xattrlen = lustre_msg_buflen(req->rq_reqmsg,
286                                                      REQ_REC_OFF + 2);
287                         if (xattrlen)
288                                 xattr = lustre_msg_buf(req->rq_reqmsg,
289                                                        REQ_REC_OFF+2, xattrlen);
290
291                         LOCK_INODE_MUTEX(inode);
292                         lock_24kernel();
293                         rc = inode->i_op->setxattr(de, xattr_name, xattr,
294                                                    xattrlen, body->flags);
295                         unlock_24kernel();
296                         UNLOCK_INODE_MUTEX(inode);
297                 }
298         } else if (body->valid & OBD_MD_FLXATTRRM) {
299                 if (inode->i_op && inode->i_op->removexattr) {
300                         LOCK_INODE_MUTEX(inode);
301                         lock_24kernel();
302                         rc = inode->i_op->removexattr(de, xattr_name);
303                         unlock_24kernel();
304                         UNLOCK_INODE_MUTEX(inode);
305                 }
306         } else {
307                 CERROR("valid bits: "LPX64"\n", body->valid);
308                 rc = -EINVAL;
309         }
310
311         LASSERT(rc <= 0);
312 out_trans:
313         err = mds_finish_transno(mds, inode, handle, req, rc, 0, 0);
314
315 out_dput:
316         l_dput(de);
317         if (rc)
318                 ldlm_lock_decref(&lockh, LCK_EX);
319         else
320                 ptlrpc_save_lock (req, &lockh, LCK_EX);
321
322         if (err && !rc)
323                 rc = err;
324 out:
325         req->rq_status = rc;
326         return 0;
327 }
328
329 int mds_setxattr(struct ptlrpc_request *req)
330 {
331         struct mds_obd *mds = mds_req2mds(req);
332         struct obd_device *obd = req->rq_export->exp_obd;
333         struct lvfs_run_ctxt saved;
334         struct mds_body *body;
335         struct lvfs_ucred uc = {0,};
336         int rc;
337         ENTRY;
338
339         mds_counter_incr(req->rq_export, LPROC_MDS_SETXATTR);
340
341         body = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*body),
342                                   lustre_swab_mds_body);
343         if (body == NULL)
344                 RETURN(-EFAULT);
345
346         if (lustre_msg_bufcount(req->rq_reqmsg) < 3)
347                 RETURN(-EFAULT);
348
349         rc = mds_init_ucred(&uc, req, REQ_REC_OFF);
350         if (rc)
351                 GOTO(out_ucred, rc);
352
353         push_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
354
355         rc = lustre_pack_reply(req, 1, NULL, NULL);
356         if (rc)
357                 GOTO(out_pop, rc);
358
359         rc = mds_setxattr_internal(req, body);
360
361 out_pop:
362         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
363 out_ucred:
364         mds_exit_ucred(&uc, mds);
365         return rc;
366 }