1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * linux/mds/mds_xattr.c
5 * Lustre Metadata Server (mds) extended attributes handling
7 * Copyright (C) 2004-2005 Cluster File Systems, Inc.
9 * This file is part of the Lustre file system, http://www.lustre.org
10 * Lustre is a trademark of Cluster File Systems, Inc.
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.
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.
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.
29 # define EXPORT_SYMTAB
31 #define DEBUG_SUBSYSTEM S_MDS
34 #include <obd_support.h>
35 #include <obd_class.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>
44 #include "mds_internal.h"
46 #ifndef XATTR_NAME_ACL_ACCESS
47 #define XATTR_NAME_ACL_ACCESS "system.posix_acl_access"
50 static int mds_getxattr_pack_msg(struct ptlrpc_request *req,
52 struct mds_body *body)
54 struct inode *inode = de->d_inode;
56 int size[3] = { sizeof(struct ptlrpc_body), sizeof(*body) };
57 int bufcnt = 2, rc = -EOPNOTSUPP, rc2;
62 if (body->valid & OBD_MD_FLXATTR) {
63 xattr_name = lustre_msg_string(req->rq_reqmsg, REQ_REC_OFF+1,0);
65 CERROR("can't extract xattr name\n");
69 if (!(req->rq_export->exp_connect_flags & OBD_CONNECT_XATTR) &&
70 (strncmp(xattr_name, "user.", 5) == 0))
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);
79 CERROR("valid bits: "LPX64"\n", body->valid);
84 if (rc != -ENODATA && rc != -EOPNOTSUPP)
85 CWARN("get inode %lu EA size error: %d\n",
89 size[bufcnt++] = min_t(int, body->eadatasize, rc);
92 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETXATTR_PACK)) {
93 CERROR("failed MDS_GETXATTR_PACK test\n");
94 req->rq_status = -ENOMEM;
98 rc2 = lustre_pack_reply(req, bufcnt, size, NULL);
107 static int mds_getxattr_internal(struct obd_device *obd,
108 struct dentry *dentry,
109 struct ptlrpc_request *req,
110 struct mds_body *reqbody)
112 struct mds_body *repbody;
113 struct inode *inode = dentry->d_inode;
116 int buflen, rc = -EOPNOTSUPP;
120 GOTO(out, rc = -ENOENT);
122 repbody = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
124 LASSERT(repbody != NULL);
126 buflen = lustre_msg_buflen(req->rq_repmsg, REPLY_REC_OFF + 1);
128 buf = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF + 1, buflen);
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);
134 if (inode->i_op && inode->i_op->getxattr) {
135 rc = inode->i_op->getxattr(dentry, xattr_name,
139 if (rc < 0 && rc != -ENODATA && rc != -EOPNOTSUPP &&
141 CDEBUG(D_OTHER, "getxattr failed: %d\n", rc);
142 } else if (reqbody->valid & OBD_MD_FLXATTRLS) {
143 DEBUG_REQ(D_INODE, req, "listxattr");
145 if (inode->i_op && inode->i_op->listxattr) {
146 rc = inode->i_op->listxattr(dentry, buf, buflen);
149 CDEBUG(D_OTHER, "listxattr failed: %d\n", rc);
154 repbody->eadatasize = rc;
162 int mds_getxattr(struct ptlrpc_request *req)
164 struct mds_obd *mds = mds_req2mds(req);
165 struct obd_device *obd = req->rq_export->exp_obd;
166 struct lvfs_run_ctxt saved;
168 struct mds_body *body;
169 struct lvfs_ucred uc = {0,};
173 mds_counter_incr(req->rq_export, LPROC_MDS_GETXATTR);
175 body = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*body),
176 lustre_swab_mds_body);
180 rc = mds_init_ucred(&uc, req, REQ_REC_OFF);
184 push_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
185 de = mds_fid2dentry(mds, &body->fid1, NULL);
187 rc = req->rq_status = PTR_ERR(de);
191 rc = mds_getxattr_pack_msg(req, de, body);
192 if (rc != 0 || req->rq_status)
195 rc = mds_getxattr_internal(obd, de, req, body);
200 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
202 mds_exit_ucred(&uc, mds);
207 * alwasy return 0, and set req->rq_status as error number in case
211 int mds_setxattr_internal(struct ptlrpc_request *req, struct mds_body *body)
213 struct mds_obd *mds = mds_req2mds(req);
214 struct obd_device *obd = req->rq_export->exp_obd;
216 struct inode *inode = NULL;
217 struct lustre_handle lockh;
222 int rc = -EOPNOTSUPP, err = 0;
228 DEBUG_REQ(D_INODE, req, "setxattr "LPU64"/%u",
229 body->fid1.id, body->fid1.generation);
231 MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
233 lockpart = MDS_INODELOCK_UPDATE;
235 /* various sanity check for xattr name */
236 xattr_name = lustre_msg_string(req->rq_reqmsg, REQ_REC_OFF + 1, 0);
238 CERROR("can't extract xattr name\n");
239 GOTO(out, rc = -EPROTO);
242 DEBUG_REQ(D_INODE, req, "%sxattr %s",
243 body->valid & OBD_MD_FLXATTR ? "set" : "remove",
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);
251 if (!(req->rq_export->exp_connect_flags & OBD_CONNECT_XATTR) &&
252 (strncmp(xattr_name, "user.", 5) == 0)) {
253 GOTO(out, rc = -EOPNOTSUPP);
256 if (!strcmp(xattr_name, XATTR_NAME_ACL_ACCESS))
257 lockpart |= MDS_INODELOCK_LOOKUP;
259 de = mds_fid2locked_dentry(obd, &body->fid1, NULL, LCK_EX,
262 GOTO(out, rc = PTR_ERR(de));
267 OBD_FAIL_WRITE(OBD_FAIL_MDS_SETXATTR_WRITE, inode->i_sb);
269 /* filter_op simply use setattr one */
270 handle = fsfilt_start(obd, inode, FSFILT_OP_SETATTR, NULL);
272 GOTO(out_dput, rc = PTR_ERR(handle));
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);
281 xattrlen = lustre_msg_buflen(req->rq_reqmsg,
284 xattr = lustre_msg_buf(req->rq_reqmsg,
285 REQ_REC_OFF+2, xattrlen);
287 LOCK_INODE_MUTEX(inode);
288 rc = inode->i_op->setxattr(de, xattr_name, xattr,
289 xattrlen, body->flags);
290 UNLOCK_INODE_MUTEX(inode);
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);
299 CERROR("valid bits: "LPX64"\n", body->valid);
305 err = mds_finish_transno(mds, inode, handle, req, rc, 0, 0);
310 ldlm_lock_decref(&lockh, LCK_EX);
312 ptlrpc_save_lock (req, &lockh, LCK_EX);
321 int mds_setxattr(struct ptlrpc_request *req)
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,};
331 mds_counter_incr(req->rq_export, LPROC_MDS_SETXATTR);
333 body = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*body),
334 lustre_swab_mds_body);
338 if (lustre_msg_bufcount(req->rq_reqmsg) < 3)
341 rc = mds_init_ucred(&uc, req, REQ_REC_OFF);
345 push_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
347 rc = lustre_pack_reply(req, 1, NULL, NULL);
351 rc = mds_setxattr_internal(req, body);
354 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
356 mds_exit_ucred(&uc, mds);