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) {
136 rc = inode->i_op->getxattr(dentry, xattr_name,
141 if (rc < 0 && rc != -ENODATA && rc != -EOPNOTSUPP &&
143 CDEBUG(D_OTHER, "getxattr failed: %d\n", rc);
144 } else if (reqbody->valid & OBD_MD_FLXATTRLS) {
145 DEBUG_REQ(D_INODE, req, "listxattr");
147 if (inode->i_op && inode->i_op->listxattr) {
149 rc = inode->i_op->listxattr(dentry, buf, buflen);
153 CDEBUG(D_OTHER, "listxattr failed: %d\n", rc);
158 repbody->eadatasize = rc;
166 int mds_getxattr(struct ptlrpc_request *req)
168 struct mds_obd *mds = mds_req2mds(req);
169 struct obd_device *obd = req->rq_export->exp_obd;
170 struct lvfs_run_ctxt saved;
172 struct mds_body *body;
173 struct lvfs_ucred uc = {0,};
177 mds_counter_incr(req->rq_export, LPROC_MDS_GETXATTR);
179 body = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*body),
180 lustre_swab_mds_body);
184 rc = mds_init_ucred(&uc, req, REQ_REC_OFF);
188 push_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
189 de = mds_fid2dentry(mds, &body->fid1, NULL);
191 rc = req->rq_status = PTR_ERR(de);
195 rc = mds_getxattr_pack_msg(req, de, body);
196 if (rc != 0 || req->rq_status)
199 rc = mds_getxattr_internal(obd, de, req, body);
204 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
206 mds_exit_ucred(&uc, mds);
211 * alwasy return 0, and set req->rq_status as error number in case
215 int mds_setxattr_internal(struct ptlrpc_request *req, struct mds_body *body)
217 struct mds_obd *mds = mds_req2mds(req);
218 struct obd_device *obd = req->rq_export->exp_obd;
220 struct inode *inode = NULL;
221 struct lustre_handle lockh;
226 int rc = -EOPNOTSUPP, err = 0;
232 DEBUG_REQ(D_INODE, req, "setxattr "LPU64"/%u",
233 body->fid1.id, body->fid1.generation);
235 MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
237 lockpart = MDS_INODELOCK_UPDATE;
239 /* various sanity check for xattr name */
240 xattr_name = lustre_msg_string(req->rq_reqmsg, REQ_REC_OFF + 1, 0);
242 CERROR("can't extract xattr name\n");
243 GOTO(out, rc = -EPROTO);
246 DEBUG_REQ(D_INODE, req, "%sxattr %s",
247 body->valid & OBD_MD_FLXATTR ? "set" : "remove",
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);
255 if (!(req->rq_export->exp_connect_flags & OBD_CONNECT_XATTR) &&
256 (strncmp(xattr_name, "user.", 5) == 0)) {
257 GOTO(out, rc = -EOPNOTSUPP);
260 if (!strcmp(xattr_name, XATTR_NAME_ACL_ACCESS))
261 lockpart |= MDS_INODELOCK_LOOKUP;
263 de = mds_fid2locked_dentry(obd, &body->fid1, NULL, LCK_EX,
266 GOTO(out, rc = PTR_ERR(de));
271 OBD_FAIL_WRITE(OBD_FAIL_MDS_SETXATTR_WRITE, inode->i_sb);
273 /* filter_op simply use setattr one */
274 handle = fsfilt_start(obd, inode, FSFILT_OP_SETATTR, NULL);
276 GOTO(out_dput, rc = PTR_ERR(handle));
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);
285 xattrlen = lustre_msg_buflen(req->rq_reqmsg,
288 xattr = lustre_msg_buf(req->rq_reqmsg,
289 REQ_REC_OFF+2, xattrlen);
291 LOCK_INODE_MUTEX(inode);
293 rc = inode->i_op->setxattr(de, xattr_name, xattr,
294 xattrlen, body->flags);
296 UNLOCK_INODE_MUTEX(inode);
298 } else if (body->valid & OBD_MD_FLXATTRRM) {
299 if (inode->i_op && inode->i_op->removexattr) {
300 LOCK_INODE_MUTEX(inode);
302 rc = inode->i_op->removexattr(de, xattr_name);
304 UNLOCK_INODE_MUTEX(inode);
307 CERROR("valid bits: "LPX64"\n", body->valid);
313 err = mds_finish_transno(mds, inode, handle, req, rc, 0, 0);
318 ldlm_lock_decref(&lockh, LCK_EX);
320 ptlrpc_save_lock (req, &lockh, LCK_EX);
329 int mds_setxattr(struct ptlrpc_request *req)
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,};
339 mds_counter_incr(req->rq_export, LPROC_MDS_SETXATTR);
341 body = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*body),
342 lustre_swab_mds_body);
346 if (lustre_msg_bufcount(req->rq_reqmsg) < 3)
349 rc = mds_init_ucred(&uc, req, REQ_REC_OFF);
353 push_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
355 rc = lustre_pack_reply(req, 1, NULL, NULL);
359 rc = mds_setxattr_internal(req, body);
362 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
364 mds_exit_ucred(&uc, mds);