1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/mds/mds_xattr.c
38 * Lustre Metadata Server (mds) extended attributes handling
42 # define EXPORT_SYMTAB
44 #define DEBUG_SUBSYSTEM S_MDS
47 #include <obd_support.h>
48 #include <obd_class.h>
50 #include <lustre_lib.h>
51 #include <lustre/lustre_idl.h>
52 #include <lustre_mds.h>
53 #include <lustre_dlm.h>
54 #include <lustre_fsfilt.h>
55 #include <lustre_ucache.h>
57 #include "mds_internal.h"
59 #ifndef XATTR_NAME_ACL_ACCESS
60 #define XATTR_NAME_ACL_ACCESS "system.posix_acl_access"
63 #ifndef XATTR_NAME_ACL_DEFAULT
64 #define XATTR_NAME_ACL_DEFAULT "system.posix_acl_default"
67 static int mds_getxattr_pack_msg(struct ptlrpc_request *req,
69 struct mds_body *body)
71 struct inode *inode = de->d_inode;
73 int size[3] = { sizeof(struct ptlrpc_body), sizeof(*body) };
74 int bufcnt = 2, rc = -EOPNOTSUPP, rc2;
79 if (body->valid & OBD_MD_FLXATTR) {
80 xattr_name = lustre_msg_string(req->rq_reqmsg, REQ_REC_OFF+1,0);
82 CERROR("can't extract xattr name\n");
86 if (!(req->rq_export->exp_connect_flags & OBD_CONNECT_XATTR) &&
87 (strncmp(xattr_name, "user.", 5) == 0))
90 if (inode->i_op && inode->i_op->getxattr)
91 rc = inode->i_op->getxattr(de, xattr_name, NULL, 0);
92 } else if (body->valid & OBD_MD_FLXATTRLS) {
93 if (inode->i_op && inode->i_op->listxattr)
94 rc = inode->i_op->listxattr(de, NULL, 0);
96 CERROR("valid bits: "LPX64"\n", body->valid);
101 if (rc != -ENODATA && rc != -EOPNOTSUPP)
102 CWARN("get inode %lu EA size error: %d\n",
106 size[bufcnt++] = min_t(int, body->eadatasize, rc);
109 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETXATTR_PACK)) {
110 CERROR("failed MDS_GETXATTR_PACK test\n");
111 req->rq_status = -ENOMEM;
115 rc2 = lustre_pack_reply(req, bufcnt, size, NULL);
124 static int mds_getxattr_internal(struct obd_device *obd,
125 struct dentry *dentry,
126 struct ptlrpc_request *req,
127 struct mds_body *reqbody)
129 struct mds_body *repbody;
130 struct inode *inode = dentry->d_inode;
133 int buflen, rc = -EOPNOTSUPP;
137 GOTO(out, rc = -ENOENT);
139 repbody = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
141 LASSERT(repbody != NULL);
143 buflen = lustre_msg_buflen(req->rq_repmsg, REPLY_REC_OFF + 1);
145 buf = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF + 1, buflen);
147 if (reqbody->valid & OBD_MD_FLXATTR) {
148 xattr_name = lustre_msg_string(req->rq_reqmsg, REQ_REC_OFF+1,0);
149 DEBUG_REQ(D_INODE, req, "getxattr %s", xattr_name);
151 if (inode->i_op && inode->i_op->getxattr) {
152 rc = inode->i_op->getxattr(dentry, xattr_name,
156 if (rc < 0 && rc != -ENODATA && rc != -EOPNOTSUPP &&
158 CDEBUG(D_OTHER, "getxattr failed: %d\n", rc);
159 } else if (reqbody->valid & OBD_MD_FLXATTRLS) {
160 DEBUG_REQ(D_INODE, req, "listxattr");
162 if (inode->i_op && inode->i_op->listxattr)
163 rc = inode->i_op->listxattr(dentry, buf, buflen);
165 CDEBUG(D_OTHER, "listxattr failed: %d\n", rc);
170 repbody->eadatasize = rc;
178 int mds_getxattr(struct ptlrpc_request *req)
180 struct mds_obd *mds = mds_req2mds(req);
181 struct obd_device *obd = req->rq_export->exp_obd;
182 struct lvfs_run_ctxt saved;
184 struct mds_body *body;
185 struct lvfs_ucred uc = { NULL, };
189 mds_counter_incr(req->rq_export, LPROC_MDS_GETXATTR);
191 body = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*body),
192 lustre_swab_mds_body);
196 rc = mds_init_ucred(&uc, req, REQ_REC_OFF);
200 push_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
201 de = mds_fid2dentry(mds, &body->fid1, NULL);
203 rc = req->rq_status = PTR_ERR(de);
207 rc = mds_getxattr_pack_msg(req, de, body);
208 if (rc != 0 || req->rq_status)
211 rc = mds_getxattr_internal(obd, de, req, body);
216 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
218 mds_exit_ucred(&uc, mds);
223 * alwasy return 0, and set req->rq_status as error number in case
227 int mds_setxattr_internal(struct ptlrpc_request *req, struct mds_body *body)
229 struct mds_obd *mds = mds_req2mds(req);
230 struct obd_device *obd = req->rq_export->exp_obd;
232 struct inode *inode = NULL;
233 struct inode *inodes[PTLRPC_NUM_VERSIONS] = { NULL };
234 struct lustre_handle lockh;
239 int rc = -EOPNOTSUPP, err = 0, sync = 0;
245 DEBUG_REQ(D_INODE, req, "setxattr "LPU64"/%u",
246 body->fid1.id, body->fid1.generation);
248 MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
250 lockpart = MDS_INODELOCK_UPDATE;
252 /* various sanity check for xattr name */
253 xattr_name = lustre_msg_string(req->rq_reqmsg, REQ_REC_OFF + 1, 0);
255 CERROR("can't extract xattr name\n");
256 GOTO(out, rc = -EPROTO);
259 DEBUG_REQ(D_INODE, req, "%sxattr %s",
260 body->valid & OBD_MD_FLXATTR ? "set" : "remove",
263 if (strncmp(xattr_name, "trusted.", 8) == 0) {
264 if (strcmp(xattr_name + 8, XATTR_LUSTRE_MDS_LOV_EA) == 0)
265 GOTO(out, rc = -EACCES);
266 if (strcmp(xattr_name + 8, "lma") == 0 &&
267 !OBD_FAIL_CHECK(OBD_FAIL_MDS_ALLOW_COMMON_EA_SETTING))
271 if (!(req->rq_export->exp_connect_flags & OBD_CONNECT_XATTR) &&
272 (strncmp(xattr_name, "user.", 5) == 0)) {
273 GOTO(out, rc = -EOPNOTSUPP);
276 /* currently lustre limit xattr size */
277 if (body->valid & OBD_MD_FLXATTR &&
278 (strcmp(xattr_name, XATTR_NAME_ACL_ACCESS) == 0 ||
279 strcmp(xattr_name, XATTR_NAME_ACL_DEFAULT) == 0 )) {
280 xattrlen = lustre_msg_buflen(req->rq_reqmsg,
283 if (xattrlen > LUSTRE_POSIX_ACL_MAX_SIZE)
287 /* Revoke all clients' lookup lock, since the access
288 * permissions for this inode is changed when ACL_ACCESS is
289 * set. This isn't needed for ACL_DEFAULT, since that does
290 * not change the access permissions of this inode, nor any
291 * other existing inodes. It is setting the ACLs inherited
292 * by new directories/files at create time. */
293 if (!strcmp(xattr_name, XATTR_NAME_ACL_ACCESS))
294 lockpart |= MDS_INODELOCK_LOOKUP;
296 de = mds_fid2locked_dentry(obd, &body->fid1, NULL, LCK_EX,
297 &lockh, NULL, 0, lockpart);
299 GOTO(out, rc = PTR_ERR(de));
304 OBD_FAIL_WRITE(obd, OBD_FAIL_MDS_SETXATTR_WRITE, inode->i_sb);
306 /* version recovery check */
307 rc = mds_version_get_check(req, inode, 0);
311 /* filter_op simply use setattr one */
312 handle = fsfilt_start(obd, inode, FSFILT_OP_SETATTR, NULL);
314 GOTO(out_dput, rc = PTR_ERR(handle));
316 if (body->valid & OBD_MD_FLXATTR) {
317 if (inode->i_op && inode->i_op->setxattr) {
318 if (lustre_msg_bufcount(req->rq_reqmsg) < 4) {
319 CERROR("no xattr data supplied\n");
320 GOTO(out_trans, rc = -EFAULT);
323 xattrlen = lustre_msg_buflen(req->rq_reqmsg,
326 xattr = lustre_msg_buf(req->rq_reqmsg,
327 REQ_REC_OFF+2, xattrlen);
329 LOCK_INODE_MUTEX(inode);
330 rc = inode->i_op->setxattr(de, xattr_name, xattr,
331 xattrlen, body->flags);
332 UNLOCK_INODE_MUTEX(inode);
334 } else if (body->valid & OBD_MD_FLXATTRRM) {
335 if (inode->i_op && inode->i_op->removexattr) {
336 LOCK_INODE_MUTEX(inode);
337 rc = inode->i_op->removexattr(de, xattr_name);
338 UNLOCK_INODE_MUTEX(inode);
341 CERROR("valid bits: "LPX64"\n", body->valid);
347 /* security-replated changes may require sync */
348 if (!strcmp(xattr_name, XATTR_NAME_ACL_ACCESS))
349 sync = mds->mds_sync_permission;
351 err = mds_finish_transno(mds, inodes, handle, req, rc, 0, sync);
355 ldlm_lock_decref(&lockh, LCK_EX);
357 ptlrpc_save_lock (req, &lockh, LCK_EX);
366 int mds_setxattr(struct ptlrpc_request *req)
368 struct mds_obd *mds = mds_req2mds(req);
369 struct obd_device *obd = req->rq_export->exp_obd;
370 struct lvfs_run_ctxt saved;
371 struct mds_body *body;
372 struct lvfs_ucred uc = { NULL, };
376 mds_counter_incr(req->rq_export, LPROC_MDS_SETXATTR);
378 body = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*body),
379 lustre_swab_mds_body);
383 if (lustre_msg_bufcount(req->rq_reqmsg) < 3)
386 rc = mds_init_ucred(&uc, req, REQ_REC_OFF);
390 push_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
392 rc = lustre_pack_reply(req, 1, NULL, NULL);
396 rc = mds_setxattr_internal(req, body);
399 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
401 mds_exit_ucred(&uc, mds);