blob: 14fbd95fd6642954cc6576988605c111d87d1558 [file] [log] [blame]
David Teiglandb3b94fa2006-01-16 16:50:04 +00001/*
2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
Steven Whitehouse3a8a9a12006-05-18 15:09:15 -04003 * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
David Teiglandb3b94fa2006-01-16 16:50:04 +00004 *
5 * This copyrighted material is made available to anyone wishing to use,
6 * modify, copy, or redistribute it subject to the terms and conditions
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -04007 * of the GNU General Public License version 2.
David Teiglandb3b94fa2006-01-16 16:50:04 +00008 */
9
David Teiglandb3b94fa2006-01-16 16:50:04 +000010#include <linux/slab.h>
11#include <linux/spinlock.h>
12#include <linux/completion.h>
13#include <linux/buffer_head.h>
Alexey Dobriyanaa0ac362007-07-15 23:40:39 -070014#include <linux/capability.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000015#include <linux/xattr.h>
Steven Whitehouse5c676f62006-02-27 17:23:27 -050016#include <linux/gfs2_ondisk.h>
Fabio Massimo Di Nitto7d308592006-09-19 07:56:29 +020017#include <linux/lm_interface.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000018#include <asm/uaccess.h>
19
20#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050021#include "incore.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000022#include "acl.h"
23#include "eaops.h"
24#include "eattr.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050025#include "util.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000026
27/**
28 * gfs2_ea_name2type - get the type of the ea, and truncate type from the name
29 * @namep: ea name, possibly with type appended
30 *
31 * Returns: GFS2_EATYPE_XXX
32 */
33
Steven Whitehousecca195c2006-09-05 13:15:18 -040034unsigned int gfs2_ea_name2type(const char *name, const char **truncated_name)
David Teiglandb3b94fa2006-01-16 16:50:04 +000035{
36 unsigned int type;
37
38 if (strncmp(name, "system.", 7) == 0) {
39 type = GFS2_EATYPE_SYS;
40 if (truncated_name)
Steven Whitehousecca195c2006-09-05 13:15:18 -040041 *truncated_name = name + sizeof("system.") - 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +000042 } else if (strncmp(name, "user.", 5) == 0) {
43 type = GFS2_EATYPE_USR;
44 if (truncated_name)
Steven Whitehousecca195c2006-09-05 13:15:18 -040045 *truncated_name = name + sizeof("user.") - 1;
Ryan O'Hara639b6d72006-05-22 10:08:35 -040046 } else if (strncmp(name, "security.", 9) == 0) {
47 type = GFS2_EATYPE_SECURITY;
48 if (truncated_name)
Steven Whitehousecca195c2006-09-05 13:15:18 -040049 *truncated_name = name + sizeof("security.") - 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +000050 } else {
51 type = GFS2_EATYPE_UNUSED;
52 if (truncated_name)
53 *truncated_name = NULL;
54 }
55
56 return type;
57}
58
59static int user_eo_get(struct gfs2_inode *ip, struct gfs2_ea_request *er)
60{
David Teiglandb3b94fa2006-01-16 16:50:04 +000061 return gfs2_ea_get_i(ip, er);
62}
63
64static int user_eo_set(struct gfs2_inode *ip, struct gfs2_ea_request *er)
65{
David Teiglandb3b94fa2006-01-16 16:50:04 +000066 return gfs2_ea_set_i(ip, er);
67}
68
69static int user_eo_remove(struct gfs2_inode *ip, struct gfs2_ea_request *er)
70{
David Teiglandb3b94fa2006-01-16 16:50:04 +000071 return gfs2_ea_remove_i(ip, er);
72}
73
74static int system_eo_get(struct gfs2_inode *ip, struct gfs2_ea_request *er)
75{
76 if (!GFS2_ACL_IS_ACCESS(er->er_name, er->er_name_len) &&
77 !GFS2_ACL_IS_DEFAULT(er->er_name, er->er_name_len) &&
78 !capable(CAP_SYS_ADMIN))
79 return -EPERM;
80
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040081 if (GFS2_SB(&ip->i_inode)->sd_args.ar_posix_acl == 0 &&
David Teiglandb3b94fa2006-01-16 16:50:04 +000082 (GFS2_ACL_IS_ACCESS(er->er_name, er->er_name_len) ||
83 GFS2_ACL_IS_DEFAULT(er->er_name, er->er_name_len)))
84 return -EOPNOTSUPP;
85
David Teiglandb3b94fa2006-01-16 16:50:04 +000086 return gfs2_ea_get_i(ip, er);
87}
88
89static int system_eo_set(struct gfs2_inode *ip, struct gfs2_ea_request *er)
90{
91 int remove = 0;
92 int error;
93
94 if (GFS2_ACL_IS_ACCESS(er->er_name, er->er_name_len)) {
95 if (!(er->er_flags & GFS2_ERF_MODE)) {
Steven Whitehouseb60623c2006-11-01 12:22:46 -050096 er->er_mode = ip->i_inode.i_mode;
David Teiglandb3b94fa2006-01-16 16:50:04 +000097 er->er_flags |= GFS2_ERF_MODE;
98 }
99 error = gfs2_acl_validate_set(ip, 1, er,
100 &remove, &er->er_mode);
101 if (error)
102 return error;
103 error = gfs2_ea_set_i(ip, er);
104 if (error)
105 return error;
106 if (remove)
107 gfs2_ea_remove_i(ip, er);
108 return 0;
109
110 } else if (GFS2_ACL_IS_DEFAULT(er->er_name, er->er_name_len)) {
111 error = gfs2_acl_validate_set(ip, 0, er,
112 &remove, NULL);
113 if (error)
114 return error;
115 if (!remove)
116 error = gfs2_ea_set_i(ip, er);
117 else {
118 error = gfs2_ea_remove_i(ip, er);
119 if (error == -ENODATA)
120 error = 0;
121 }
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400122 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000123 }
124
125 return -EPERM;
126}
127
128static int system_eo_remove(struct gfs2_inode *ip, struct gfs2_ea_request *er)
129{
130 if (GFS2_ACL_IS_ACCESS(er->er_name, er->er_name_len)) {
131 int error = gfs2_acl_validate_remove(ip, 1);
132 if (error)
133 return error;
134
135 } else if (GFS2_ACL_IS_DEFAULT(er->er_name, er->er_name_len)) {
136 int error = gfs2_acl_validate_remove(ip, 0);
137 if (error)
138 return error;
139
140 } else
141 return -EPERM;
142
143 return gfs2_ea_remove_i(ip, er);
144}
145
Ryan O'Hara639b6d72006-05-22 10:08:35 -0400146static int security_eo_get(struct gfs2_inode *ip, struct gfs2_ea_request *er)
147{
Ryan O'Hara639b6d72006-05-22 10:08:35 -0400148 return gfs2_ea_get_i(ip, er);
149}
150
151static int security_eo_set(struct gfs2_inode *ip, struct gfs2_ea_request *er)
152{
Ryan O'Hara639b6d72006-05-22 10:08:35 -0400153 return gfs2_ea_set_i(ip, er);
154}
155
156static int security_eo_remove(struct gfs2_inode *ip, struct gfs2_ea_request *er)
157{
Ryan O'Hara639b6d72006-05-22 10:08:35 -0400158 return gfs2_ea_remove_i(ip, er);
159}
160
Denis Cheng4ef29002007-07-31 18:31:11 +0800161static const struct gfs2_eattr_operations gfs2_user_eaops = {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000162 .eo_get = user_eo_get,
163 .eo_set = user_eo_set,
164 .eo_remove = user_eo_remove,
165 .eo_name = "user",
166};
167
Denis Cheng4ef29002007-07-31 18:31:11 +0800168const struct gfs2_eattr_operations gfs2_system_eaops = {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000169 .eo_get = system_eo_get,
170 .eo_set = system_eo_set,
171 .eo_remove = system_eo_remove,
172 .eo_name = "system",
173};
174
Denis Cheng4ef29002007-07-31 18:31:11 +0800175static const struct gfs2_eattr_operations gfs2_security_eaops = {
Ryan O'Hara639b6d72006-05-22 10:08:35 -0400176 .eo_get = security_eo_get,
177 .eo_set = security_eo_set,
178 .eo_remove = security_eo_remove,
179 .eo_name = "security",
180};
181
Denis Cheng4ef29002007-07-31 18:31:11 +0800182const struct gfs2_eattr_operations *gfs2_ea_ops[] = {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000183 NULL,
184 &gfs2_user_eaops,
185 &gfs2_system_eaops,
Ryan O'Harae70409f2006-05-25 17:36:15 -0400186 &gfs2_security_eaops,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000187};
188