Blame view

fs/9p/acl.c 7.9 KB
85ff872d3   Aneesh Kumar K.V   fs/9p: Implement ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  /*
   * Copyright IBM Corporation, 2010
   * Author Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
   *
   * This program is free software; you can redistribute it and/or modify it
   * under the terms of version 2.1 of the GNU Lesser General Public License
   * as published by the Free Software Foundation.
   *
   * This program is distributed in the hope that it would be useful, but
   * WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
   *
   */
  
  #include <linux/module.h>
  #include <linux/fs.h>
  #include <net/9p/9p.h>
  #include <net/9p/client.h>
  #include <linux/slab.h>
22d8dcdf8   Aneesh Kumar K.V   fs/9p: Implement ...
20
  #include <linux/sched.h>
85ff872d3   Aneesh Kumar K.V   fs/9p: Implement ...
21
22
23
  #include <linux/posix_acl_xattr.h>
  #include "xattr.h"
  #include "acl.h"
76381a42e   Aneesh Kumar K.V   fs/9p: Add access...
24
  #include "v9fs.h"
5ffc0cb30   Aneesh Kumar K.V   fs/9p: Add inode ...
25
  #include "v9fs_vfs.h"
0f235caea   Al Viro   9p: switch v9fs_s...
26
  #include "fid.h"
85ff872d3   Aneesh Kumar K.V   fs/9p: Implement ...
27
28
29
30
31
  
  static struct posix_acl *__v9fs_get_acl(struct p9_fid *fid, char *name)
  {
  	ssize_t size;
  	void *value = NULL;
009ca3897   Joe Perches   fs/9p: Remove unn...
32
  	struct posix_acl *acl = NULL;
85ff872d3   Aneesh Kumar K.V   fs/9p: Implement ...
33
34
35
36
37
38
39
40
  
  	size = v9fs_fid_xattr_get(fid, name, NULL, 0);
  	if (size > 0) {
  		value = kzalloc(size, GFP_NOFS);
  		if (!value)
  			return ERR_PTR(-ENOMEM);
  		size = v9fs_fid_xattr_get(fid, name, value, size);
  		if (size > 0) {
5f3a4a28e   Eric W. Biederman   userns: Pass a us...
41
  			acl = posix_acl_from_xattr(&init_user_ns, value, size);
85ff872d3   Aneesh Kumar K.V   fs/9p: Implement ...
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
  			if (IS_ERR(acl))
  				goto err_out;
  		}
  	} else if (size == -ENODATA || size == 0 ||
  		   size == -ENOSYS || size == -EOPNOTSUPP) {
  		acl = NULL;
  	} else
  		acl = ERR_PTR(-EIO);
  
  err_out:
  	kfree(value);
  	return acl;
  }
  
  int v9fs_get_acl(struct inode *inode, struct p9_fid *fid)
  {
  	int retval = 0;
  	struct posix_acl *pacl, *dacl;
76381a42e   Aneesh Kumar K.V   fs/9p: Add access...
60
  	struct v9fs_session_info *v9ses;
85ff872d3   Aneesh Kumar K.V   fs/9p: Implement ...
61

76381a42e   Aneesh Kumar K.V   fs/9p: Add access...
62
  	v9ses = v9fs_inode2v9ses(inode);
e782ef710   Venkateswararao Jujjuri (JV)   [fs/9P] Add posix...
63
64
  	if (((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT) ||
  			((v9ses->flags & V9FS_ACL_MASK) != V9FS_POSIX_ACL)) {
76381a42e   Aneesh Kumar K.V   fs/9p: Add access...
65
66
67
68
  		set_cached_acl(inode, ACL_TYPE_DEFAULT, NULL);
  		set_cached_acl(inode, ACL_TYPE_ACCESS, NULL);
  		return 0;
  	}
85ff872d3   Aneesh Kumar K.V   fs/9p: Implement ...
69
  	/* get the default/access acl values and cache them */
97d792992   Andreas Gruenbacher   posix acls: Remov...
70
71
  	dacl = __v9fs_get_acl(fid, XATTR_NAME_POSIX_ACL_DEFAULT);
  	pacl = __v9fs_get_acl(fid, XATTR_NAME_POSIX_ACL_ACCESS);
85ff872d3   Aneesh Kumar K.V   fs/9p: Implement ...
72
73
74
75
  
  	if (!IS_ERR(dacl) && !IS_ERR(pacl)) {
  		set_cached_acl(inode, ACL_TYPE_DEFAULT, dacl);
  		set_cached_acl(inode, ACL_TYPE_ACCESS, pacl);
85ff872d3   Aneesh Kumar K.V   fs/9p: Implement ...
76
77
  	} else
  		retval = -EIO;
c61fa0d6d   Venkateswararao Jujjuri (JV)   [fs/9p] Plug pote...
78
79
80
81
82
  	if (!IS_ERR(dacl))
  		posix_acl_release(dacl);
  
  	if (!IS_ERR(pacl))
  		posix_acl_release(pacl);
85ff872d3   Aneesh Kumar K.V   fs/9p: Implement ...
83
84
85
86
87
88
89
90
91
92
93
  	return retval;
  }
  
  static struct posix_acl *v9fs_get_cached_acl(struct inode *inode, int type)
  {
  	struct posix_acl *acl;
  	/*
  	 * 9p Always cache the acl value when
  	 * instantiating the inode (v9fs_inode_from_fid)
  	 */
  	acl = get_cached_acl(inode, type);
b8a7a3a66   Andreas Gruenbacher   posix_acl: Inode ...
94
  	BUG_ON(is_uncached_acl(acl));
85ff872d3   Aneesh Kumar K.V   fs/9p: Implement ...
95
96
  	return acl;
  }
4e34e719e   Christoph Hellwig   fs: take the ACL ...
97
  struct posix_acl *v9fs_iop_get_acl(struct inode *inode, int type)
85ff872d3   Aneesh Kumar K.V   fs/9p: Implement ...
98
  {
76381a42e   Aneesh Kumar K.V   fs/9p: Add access...
99
100
101
  	struct v9fs_session_info *v9ses;
  
  	v9ses = v9fs_inode2v9ses(inode);
e782ef710   Venkateswararao Jujjuri (JV)   [fs/9P] Add posix...
102
103
  	if (((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT) ||
  			((v9ses->flags & V9FS_ACL_MASK) != V9FS_POSIX_ACL)) {
76381a42e   Aneesh Kumar K.V   fs/9p: Add access...
104
  		/*
e782ef710   Venkateswararao Jujjuri (JV)   [fs/9P] Add posix...
105
  		 * On access = client  and acl = on mode get the acl
76381a42e   Aneesh Kumar K.V   fs/9p: Add access...
106
107
  		 * values from the server
  		 */
4e34e719e   Christoph Hellwig   fs: take the ACL ...
108
  		return NULL;
76381a42e   Aneesh Kumar K.V   fs/9p: Add access...
109
  	}
4e34e719e   Christoph Hellwig   fs: take the ACL ...
110
  	return v9fs_get_cached_acl(inode, type);
85ff872d3   Aneesh Kumar K.V   fs/9p: Implement ...
111
  }
7a4566b0b   Aneesh Kumar K.V   fs/9p: Add xattr ...
112

0f235caea   Al Viro   9p: switch v9fs_s...
113
  static int v9fs_set_acl(struct p9_fid *fid, int type, struct posix_acl *acl)
6e8dc5555   Aneesh Kumar K.V   fs/9p: Update ACL...
114
115
116
117
118
  {
  	int retval;
  	char *name;
  	size_t size;
  	void *buffer;
d344b0fb7   Venkateswararao Jujjuri (JV)   [fs/9p] Initialze...
119
120
  	if (!acl)
  		return 0;
6e8dc5555   Aneesh Kumar K.V   fs/9p: Update ACL...
121
122
123
124
125
  	/* Set a setxattr request to server */
  	size = posix_acl_xattr_size(acl->a_count);
  	buffer = kmalloc(size, GFP_KERNEL);
  	if (!buffer)
  		return -ENOMEM;
5f3a4a28e   Eric W. Biederman   userns: Pass a us...
126
  	retval = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
6e8dc5555   Aneesh Kumar K.V   fs/9p: Update ACL...
127
128
129
130
  	if (retval < 0)
  		goto err_free_out;
  	switch (type) {
  	case ACL_TYPE_ACCESS:
97d792992   Andreas Gruenbacher   posix acls: Remov...
131
  		name = XATTR_NAME_POSIX_ACL_ACCESS;
6e8dc5555   Aneesh Kumar K.V   fs/9p: Update ACL...
132
133
  		break;
  	case ACL_TYPE_DEFAULT:
97d792992   Andreas Gruenbacher   posix acls: Remov...
134
  		name = XATTR_NAME_POSIX_ACL_DEFAULT;
6e8dc5555   Aneesh Kumar K.V   fs/9p: Update ACL...
135
136
137
138
  		break;
  	default:
  		BUG();
  	}
0f235caea   Al Viro   9p: switch v9fs_s...
139
  	retval = v9fs_fid_xattr_set(fid, name, buffer, size, 0);
6e8dc5555   Aneesh Kumar K.V   fs/9p: Update ACL...
140
141
142
143
  err_free_out:
  	kfree(buffer);
  	return retval;
  }
be308f079   Al Viro   9p: switch v9fs_a...
144
  int v9fs_acl_chmod(struct inode *inode, struct p9_fid *fid)
6e8dc5555   Aneesh Kumar K.V   fs/9p: Update ACL...
145
146
  {
  	int retval = 0;
bc26ab5f6   Al Viro   kill boilerplate ...
147
  	struct posix_acl *acl;
6e8dc5555   Aneesh Kumar K.V   fs/9p: Update ACL...
148
149
150
151
152
  
  	if (S_ISLNK(inode->i_mode))
  		return -EOPNOTSUPP;
  	acl = v9fs_get_cached_acl(inode, ACL_TYPE_ACCESS);
  	if (acl) {
5bf3258fd   Christoph Hellwig   fs: make posix_ac...
153
  		retval = __posix_acl_chmod(&acl, GFP_KERNEL, inode->i_mode);
bc26ab5f6   Al Viro   kill boilerplate ...
154
155
  		if (retval)
  			return retval;
7f165aaa7   Al Viro   9p: lift the call...
156
  		set_cached_acl(inode, ACL_TYPE_ACCESS, acl);
0f235caea   Al Viro   9p: switch v9fs_s...
157
  		retval = v9fs_set_acl(fid, ACL_TYPE_ACCESS, acl);
6e8dc5555   Aneesh Kumar K.V   fs/9p: Update ACL...
158
  		posix_acl_release(acl);
6e8dc5555   Aneesh Kumar K.V   fs/9p: Update ACL...
159
160
161
  	}
  	return retval;
  }
3592ac444   Al Viro   9p: switch v9fs_s...
162
  int v9fs_set_create_acl(struct inode *inode, struct p9_fid *fid,
5fa6300ae   Al Viro   9p: split droppin...
163
  			struct posix_acl *dacl, struct posix_acl *acl)
ad77dbce5   Aneesh Kumar K.V   fs/9p: Implement ...
164
  {
3592ac444   Al Viro   9p: switch v9fs_s...
165
166
167
168
  	set_cached_acl(inode, ACL_TYPE_DEFAULT, dacl);
  	set_cached_acl(inode, ACL_TYPE_ACCESS, acl);
  	v9fs_set_acl(fid, ACL_TYPE_DEFAULT, dacl);
  	v9fs_set_acl(fid, ACL_TYPE_ACCESS, acl);
ad77dbce5   Aneesh Kumar K.V   fs/9p: Implement ...
169
170
  	return 0;
  }
5fa6300ae   Al Viro   9p: split droppin...
171
172
173
174
175
176
  void v9fs_put_acl(struct posix_acl *dacl,
  		  struct posix_acl *acl)
  {
  	posix_acl_release(dacl);
  	posix_acl_release(acl);
  }
d3fb61207   Al Viro   switch posix_acl_...
177
  int v9fs_acl_mode(struct inode *dir, umode_t *modep,
ad77dbce5   Aneesh Kumar K.V   fs/9p: Implement ...
178
179
180
  		  struct posix_acl **dpacl, struct posix_acl **pacl)
  {
  	int retval = 0;
d3fb61207   Al Viro   switch posix_acl_...
181
  	umode_t mode = *modep;
ad77dbce5   Aneesh Kumar K.V   fs/9p: Implement ...
182
183
184
185
186
187
188
189
190
191
  	struct posix_acl *acl = NULL;
  
  	if (!S_ISLNK(mode)) {
  		acl = v9fs_get_cached_acl(dir, ACL_TYPE_DEFAULT);
  		if (IS_ERR(acl))
  			return PTR_ERR(acl);
  		if (!acl)
  			mode &= ~current_umask();
  	}
  	if (acl) {
ad77dbce5   Aneesh Kumar K.V   fs/9p: Implement ...
192
  		if (S_ISDIR(mode))
1ec95bf34   Al Viro   9p: close ACL leaks
193
  			*dpacl = posix_acl_dup(acl);
37bc15392   Christoph Hellwig   fs: make posix_ac...
194
  		retval = __posix_acl_create(&acl, GFP_NOFS, &mode);
826cae2f2   Al Viro   kill boilerplates...
195
196
  		if (retval < 0)
  			return retval;
ad77dbce5   Aneesh Kumar K.V   fs/9p: Implement ...
197
  		if (retval > 0)
826cae2f2   Al Viro   kill boilerplates...
198
  			*pacl = acl;
1ec95bf34   Al Viro   9p: close ACL leaks
199
  		else
826cae2f2   Al Viro   kill boilerplates...
200
  			posix_acl_release(acl);
ad77dbce5   Aneesh Kumar K.V   fs/9p: Implement ...
201
202
203
  	}
  	*modep  = mode;
  	return 0;
ad77dbce5   Aneesh Kumar K.V   fs/9p: Implement ...
204
  }
d9a82a040   Andreas Gruenbacher   xattr handlers: P...
205
  static int v9fs_xattr_get_acl(const struct xattr_handler *handler,
b296821a7   Al Viro   xattr_handler: pa...
206
207
  			      struct dentry *dentry, struct inode *inode,
  			      const char *name, void *buffer, size_t size)
7a4566b0b   Aneesh Kumar K.V   fs/9p: Add xattr ...
208
  {
76381a42e   Aneesh Kumar K.V   fs/9p: Add access...
209
  	struct v9fs_session_info *v9ses;
7a4566b0b   Aneesh Kumar K.V   fs/9p: Add xattr ...
210
211
  	struct posix_acl *acl;
  	int error;
42869c8ad   Aneesh Kumar K.V   fs/9p: Add v9fs_d...
212
  	v9ses = v9fs_dentry2v9ses(dentry);
76381a42e   Aneesh Kumar K.V   fs/9p: Add access...
213
214
215
216
  	/*
  	 * We allow set/get/list of acl when access=client is not specified
  	 */
  	if ((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT)
98e9cb571   Andreas Gruenbacher   vfs: Distinguish ...
217
  		return v9fs_xattr_get(dentry, handler->name, buffer, size);
76381a42e   Aneesh Kumar K.V   fs/9p: Add access...
218

b296821a7   Al Viro   xattr_handler: pa...
219
  	acl = v9fs_get_cached_acl(inode, handler->flags);
7a4566b0b   Aneesh Kumar K.V   fs/9p: Add xattr ...
220
221
222
223
  	if (IS_ERR(acl))
  		return PTR_ERR(acl);
  	if (acl == NULL)
  		return -ENODATA;
5f3a4a28e   Eric W. Biederman   userns: Pass a us...
224
  	error = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
7a4566b0b   Aneesh Kumar K.V   fs/9p: Add xattr ...
225
226
227
228
  	posix_acl_release(acl);
  
  	return error;
  }
d9a82a040   Andreas Gruenbacher   xattr handlers: P...
229
  static int v9fs_xattr_set_acl(const struct xattr_handler *handler,
593012268   Al Viro   switch xattr_hand...
230
231
232
  			      struct dentry *dentry, struct inode *inode,
  			      const char *name, const void *value,
  			      size_t size, int flags)
7a4566b0b   Aneesh Kumar K.V   fs/9p: Add xattr ...
233
  {
22d8dcdf8   Aneesh Kumar K.V   fs/9p: Implement ...
234
235
  	int retval;
  	struct posix_acl *acl;
76381a42e   Aneesh Kumar K.V   fs/9p: Add access...
236
  	struct v9fs_session_info *v9ses;
22d8dcdf8   Aneesh Kumar K.V   fs/9p: Implement ...
237

42869c8ad   Aneesh Kumar K.V   fs/9p: Add v9fs_d...
238
  	v9ses = v9fs_dentry2v9ses(dentry);
76381a42e   Aneesh Kumar K.V   fs/9p: Add access...
239
240
241
242
243
  	/*
  	 * set the attribute on the remote. Without even looking at the
  	 * xattr value. We leave it to the server to validate
  	 */
  	if ((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT)
98e9cb571   Andreas Gruenbacher   vfs: Distinguish ...
244
  		return v9fs_xattr_set(dentry, handler->name, value, size,
e409de992   Andreas Gruenbacher   9p: xattr simplif...
245
  				      flags);
76381a42e   Aneesh Kumar K.V   fs/9p: Add access...
246

22d8dcdf8   Aneesh Kumar K.V   fs/9p: Implement ...
247
248
  	if (S_ISLNK(inode->i_mode))
  		return -EOPNOTSUPP;
2e1496707   Serge E. Hallyn   userns: rename is...
249
  	if (!inode_owner_or_capable(inode))
22d8dcdf8   Aneesh Kumar K.V   fs/9p: Implement ...
250
251
252
  		return -EPERM;
  	if (value) {
  		/* update the cached acl value */
5f3a4a28e   Eric W. Biederman   userns: Pass a us...
253
  		acl = posix_acl_from_xattr(&init_user_ns, value, size);
22d8dcdf8   Aneesh Kumar K.V   fs/9p: Implement ...
254
255
256
  		if (IS_ERR(acl))
  			return PTR_ERR(acl);
  		else if (acl) {
0d4d717f2   Eric W. Biederman   vfs: Verify acls ...
257
  			retval = posix_acl_valid(inode->i_sb->s_user_ns, acl);
22d8dcdf8   Aneesh Kumar K.V   fs/9p: Implement ...
258
259
260
261
262
  			if (retval)
  				goto err_out;
  		}
  	} else
  		acl = NULL;
d9a82a040   Andreas Gruenbacher   xattr handlers: P...
263
  	switch (handler->flags) {
22d8dcdf8   Aneesh Kumar K.V   fs/9p: Implement ...
264
  	case ACL_TYPE_ACCESS:
22d8dcdf8   Aneesh Kumar K.V   fs/9p: Implement ...
265
  		if (acl) {
073931017   Jan Kara   posix_acl: Clear ...
266
267
268
269
  			struct iattr iattr;
  
  			retval = posix_acl_update_mode(inode, &iattr.ia_mode, &acl);
  			if (retval)
22d8dcdf8   Aneesh Kumar K.V   fs/9p: Implement ...
270
  				goto err_out;
073931017   Jan Kara   posix_acl: Clear ...
271
272
273
274
275
  			if (!acl) {
  				/*
  				 * ACL can be represented
  				 * by the mode bits. So don't
  				 * update ACL.
22d8dcdf8   Aneesh Kumar K.V   fs/9p: Implement ...
276
  				 */
073931017   Jan Kara   posix_acl: Clear ...
277
278
  				value = NULL;
  				size = 0;
22d8dcdf8   Aneesh Kumar K.V   fs/9p: Implement ...
279
  			}
073931017   Jan Kara   posix_acl: Clear ...
280
281
282
283
284
285
  			iattr.ia_valid = ATTR_MODE;
  			/* FIXME should we update ctime ?
  			 * What is the following setxattr update the
  			 * mode ?
  			 */
  			v9fs_vfs_setattr_dotl(dentry, &iattr);
22d8dcdf8   Aneesh Kumar K.V   fs/9p: Implement ...
286
287
288
  		}
  		break;
  	case ACL_TYPE_DEFAULT:
22d8dcdf8   Aneesh Kumar K.V   fs/9p: Implement ...
289
  		if (!S_ISDIR(inode->i_mode)) {
6f81c1157   Aneesh Kumar K.V   fs/9p: Fix the re...
290
  			retval = acl ? -EINVAL : 0;
22d8dcdf8   Aneesh Kumar K.V   fs/9p: Implement ...
291
292
293
294
295
296
  			goto err_out;
  		}
  		break;
  	default:
  		BUG();
  	}
98e9cb571   Andreas Gruenbacher   vfs: Distinguish ...
297
  	retval = v9fs_xattr_set(dentry, handler->name, value, size, flags);
22d8dcdf8   Aneesh Kumar K.V   fs/9p: Implement ...
298
  	if (!retval)
d9a82a040   Andreas Gruenbacher   xattr handlers: P...
299
  		set_cached_acl(inode, handler->flags, acl);
22d8dcdf8   Aneesh Kumar K.V   fs/9p: Implement ...
300
301
302
  err_out:
  	posix_acl_release(acl);
  	return retval;
7a4566b0b   Aneesh Kumar K.V   fs/9p: Add xattr ...
303
304
305
  }
  
  const struct xattr_handler v9fs_xattr_acl_access_handler = {
98e9cb571   Andreas Gruenbacher   vfs: Distinguish ...
306
  	.name	= XATTR_NAME_POSIX_ACL_ACCESS,
7a4566b0b   Aneesh Kumar K.V   fs/9p: Add xattr ...
307
308
309
310
311
312
  	.flags	= ACL_TYPE_ACCESS,
  	.get	= v9fs_xattr_get_acl,
  	.set	= v9fs_xattr_set_acl,
  };
  
  const struct xattr_handler v9fs_xattr_acl_default_handler = {
98e9cb571   Andreas Gruenbacher   vfs: Distinguish ...
313
  	.name	= XATTR_NAME_POSIX_ACL_DEFAULT,
7a4566b0b   Aneesh Kumar K.V   fs/9p: Add xattr ...
314
315
316
317
  	.flags	= ACL_TYPE_DEFAULT,
  	.get	= v9fs_xattr_get_acl,
  	.set	= v9fs_xattr_set_acl,
  };