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