• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <linux/capability.h>
2 #include <linux/fs.h>
3 #include <linux/posix_acl.h>
4 #include "reiserfs.h"
5 #include <linux/errno.h>
6 #include <linux/pagemap.h>
7 #include <linux/xattr.h>
8 #include <linux/slab.h>
9 #include <linux/posix_acl_xattr.h>
10 #include "xattr.h"
11 #include "acl.h"
12 #include <linux/uaccess.h>
13 
14 static int __reiserfs_set_acl(struct reiserfs_transaction_handle *th,
15 			    struct inode *inode, int type,
16 			    struct posix_acl *acl);
17 
18 
19 int
reiserfs_set_acl(struct inode * inode,struct posix_acl * acl,int type)20 reiserfs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
21 {
22 	int error, error2;
23 	struct reiserfs_transaction_handle th;
24 	size_t jcreate_blocks;
25 	int size = acl ? posix_acl_xattr_size(acl->a_count) : 0;
26 
27 
28 	/*
29 	 * Pessimism: We can't assume that anything from the xattr root up
30 	 * has been created.
31 	 */
32 
33 	jcreate_blocks = reiserfs_xattr_jcreate_nblocks(inode) +
34 			 reiserfs_xattr_nblocks(inode, size) * 2;
35 
36 	reiserfs_write_lock(inode->i_sb);
37 	error = journal_begin(&th, inode->i_sb, jcreate_blocks);
38 	reiserfs_write_unlock(inode->i_sb);
39 	if (error == 0) {
40 		if (type == ACL_TYPE_ACCESS && acl) {
41 			error = posix_acl_update_mode(inode, &inode->i_mode,
42 						      &acl);
43 			if (error)
44 				goto unlock;
45 		}
46 		error = __reiserfs_set_acl(&th, inode, type, acl);
47 unlock:
48 		reiserfs_write_lock(inode->i_sb);
49 		error2 = journal_end(&th);
50 		reiserfs_write_unlock(inode->i_sb);
51 		if (error2)
52 			error = error2;
53 	}
54 
55 	return error;
56 }
57 
58 /*
59  * Convert from filesystem to in-memory representation.
60  */
reiserfs_posix_acl_from_disk(const void * value,size_t size)61 static struct posix_acl *reiserfs_posix_acl_from_disk(const void *value, size_t size)
62 {
63 	const char *end = (char *)value + size;
64 	int n, count;
65 	struct posix_acl *acl;
66 
67 	if (!value)
68 		return NULL;
69 	if (size < sizeof(reiserfs_acl_header))
70 		return ERR_PTR(-EINVAL);
71 	if (((reiserfs_acl_header *) value)->a_version !=
72 	    cpu_to_le32(REISERFS_ACL_VERSION))
73 		return ERR_PTR(-EINVAL);
74 	value = (char *)value + sizeof(reiserfs_acl_header);
75 	count = reiserfs_acl_count(size);
76 	if (count < 0)
77 		return ERR_PTR(-EINVAL);
78 	if (count == 0)
79 		return NULL;
80 	acl = posix_acl_alloc(count, GFP_NOFS);
81 	if (!acl)
82 		return ERR_PTR(-ENOMEM);
83 	for (n = 0; n < count; n++) {
84 		reiserfs_acl_entry *entry = (reiserfs_acl_entry *) value;
85 		if ((char *)value + sizeof(reiserfs_acl_entry_short) > end)
86 			goto fail;
87 		acl->a_entries[n].e_tag = le16_to_cpu(entry->e_tag);
88 		acl->a_entries[n].e_perm = le16_to_cpu(entry->e_perm);
89 		switch (acl->a_entries[n].e_tag) {
90 		case ACL_USER_OBJ:
91 		case ACL_GROUP_OBJ:
92 		case ACL_MASK:
93 		case ACL_OTHER:
94 			value = (char *)value +
95 			    sizeof(reiserfs_acl_entry_short);
96 			break;
97 
98 		case ACL_USER:
99 			value = (char *)value + sizeof(reiserfs_acl_entry);
100 			if ((char *)value > end)
101 				goto fail;
102 			acl->a_entries[n].e_uid =
103 				make_kuid(&init_user_ns,
104 					  le32_to_cpu(entry->e_id));
105 			break;
106 		case ACL_GROUP:
107 			value = (char *)value + sizeof(reiserfs_acl_entry);
108 			if ((char *)value > end)
109 				goto fail;
110 			acl->a_entries[n].e_gid =
111 				make_kgid(&init_user_ns,
112 					  le32_to_cpu(entry->e_id));
113 			break;
114 
115 		default:
116 			goto fail;
117 		}
118 	}
119 	if (value != end)
120 		goto fail;
121 	return acl;
122 
123 fail:
124 	posix_acl_release(acl);
125 	return ERR_PTR(-EINVAL);
126 }
127 
128 /*
129  * Convert from in-memory to filesystem representation.
130  */
reiserfs_posix_acl_to_disk(const struct posix_acl * acl,size_t * size)131 static void *reiserfs_posix_acl_to_disk(const struct posix_acl *acl, size_t * size)
132 {
133 	reiserfs_acl_header *ext_acl;
134 	char *e;
135 	int n;
136 
137 	*size = reiserfs_acl_size(acl->a_count);
138 	ext_acl = kmalloc(sizeof(reiserfs_acl_header) +
139 						  acl->a_count *
140 						  sizeof(reiserfs_acl_entry),
141 						  GFP_NOFS);
142 	if (!ext_acl)
143 		return ERR_PTR(-ENOMEM);
144 	ext_acl->a_version = cpu_to_le32(REISERFS_ACL_VERSION);
145 	e = (char *)ext_acl + sizeof(reiserfs_acl_header);
146 	for (n = 0; n < acl->a_count; n++) {
147 		const struct posix_acl_entry *acl_e = &acl->a_entries[n];
148 		reiserfs_acl_entry *entry = (reiserfs_acl_entry *) e;
149 		entry->e_tag = cpu_to_le16(acl->a_entries[n].e_tag);
150 		entry->e_perm = cpu_to_le16(acl->a_entries[n].e_perm);
151 		switch (acl->a_entries[n].e_tag) {
152 		case ACL_USER:
153 			entry->e_id = cpu_to_le32(
154 				from_kuid(&init_user_ns, acl_e->e_uid));
155 			e += sizeof(reiserfs_acl_entry);
156 			break;
157 		case ACL_GROUP:
158 			entry->e_id = cpu_to_le32(
159 				from_kgid(&init_user_ns, acl_e->e_gid));
160 			e += sizeof(reiserfs_acl_entry);
161 			break;
162 
163 		case ACL_USER_OBJ:
164 		case ACL_GROUP_OBJ:
165 		case ACL_MASK:
166 		case ACL_OTHER:
167 			e += sizeof(reiserfs_acl_entry_short);
168 			break;
169 
170 		default:
171 			goto fail;
172 		}
173 	}
174 	return (char *)ext_acl;
175 
176 fail:
177 	kfree(ext_acl);
178 	return ERR_PTR(-EINVAL);
179 }
180 
181 /*
182  * Inode operation get_posix_acl().
183  *
184  * inode->i_mutex: down
185  * BKL held [before 2.5.x]
186  */
reiserfs_get_acl(struct inode * inode,int type)187 struct posix_acl *reiserfs_get_acl(struct inode *inode, int type)
188 {
189 	char *name, *value;
190 	struct posix_acl *acl;
191 	int size;
192 	int retval;
193 
194 	switch (type) {
195 	case ACL_TYPE_ACCESS:
196 		name = POSIX_ACL_XATTR_ACCESS;
197 		break;
198 	case ACL_TYPE_DEFAULT:
199 		name = POSIX_ACL_XATTR_DEFAULT;
200 		break;
201 	default:
202 		BUG();
203 	}
204 
205 	size = reiserfs_xattr_get(inode, name, NULL, 0);
206 	if (size < 0) {
207 		if (size == -ENODATA || size == -ENOSYS) {
208 			set_cached_acl(inode, type, NULL);
209 			return NULL;
210 		}
211 		return ERR_PTR(size);
212 	}
213 
214 	value = kmalloc(size, GFP_NOFS);
215 	if (!value)
216 		return ERR_PTR(-ENOMEM);
217 
218 	retval = reiserfs_xattr_get(inode, name, value, size);
219 	if (retval == -ENODATA || retval == -ENOSYS) {
220 		/*
221 		 * This shouldn't actually happen as it should have
222 		 * been caught above.. but just in case
223 		 */
224 		acl = NULL;
225 	} else if (retval < 0) {
226 		acl = ERR_PTR(retval);
227 	} else {
228 		acl = reiserfs_posix_acl_from_disk(value, retval);
229 	}
230 	if (!IS_ERR(acl))
231 		set_cached_acl(inode, type, acl);
232 
233 	kfree(value);
234 	return acl;
235 }
236 
237 /*
238  * Inode operation set_posix_acl().
239  *
240  * inode->i_mutex: down
241  * BKL held [before 2.5.x]
242  */
243 static int
__reiserfs_set_acl(struct reiserfs_transaction_handle * th,struct inode * inode,int type,struct posix_acl * acl)244 __reiserfs_set_acl(struct reiserfs_transaction_handle *th, struct inode *inode,
245 		 int type, struct posix_acl *acl)
246 {
247 	char *name;
248 	void *value = NULL;
249 	size_t size = 0;
250 	int error;
251 
252 	switch (type) {
253 	case ACL_TYPE_ACCESS:
254 		name = POSIX_ACL_XATTR_ACCESS;
255 		break;
256 	case ACL_TYPE_DEFAULT:
257 		name = POSIX_ACL_XATTR_DEFAULT;
258 		if (!S_ISDIR(inode->i_mode))
259 			return acl ? -EACCES : 0;
260 		break;
261 	default:
262 		return -EINVAL;
263 	}
264 
265 	if (acl) {
266 		value = reiserfs_posix_acl_to_disk(acl, &size);
267 		if (IS_ERR(value))
268 			return (int)PTR_ERR(value);
269 	}
270 
271 	error = reiserfs_xattr_set_handle(th, inode, name, value, size, 0);
272 
273 	/*
274 	 * Ensure that the inode gets dirtied if we're only using
275 	 * the mode bits and an old ACL didn't exist. We don't need
276 	 * to check if the inode is hashed here since we won't get
277 	 * called by reiserfs_inherit_default_acl().
278 	 */
279 	if (error == -ENODATA) {
280 		error = 0;
281 		if (type == ACL_TYPE_ACCESS) {
282 			inode->i_ctime = CURRENT_TIME_SEC;
283 			mark_inode_dirty(inode);
284 		}
285 	}
286 
287 	kfree(value);
288 
289 	if (!error)
290 		set_cached_acl(inode, type, acl);
291 
292 	return error;
293 }
294 
295 /*
296  * dir->i_mutex: locked,
297  * inode is new and not released into the wild yet
298  */
299 int
reiserfs_inherit_default_acl(struct reiserfs_transaction_handle * th,struct inode * dir,struct dentry * dentry,struct inode * inode)300 reiserfs_inherit_default_acl(struct reiserfs_transaction_handle *th,
301 			     struct inode *dir, struct dentry *dentry,
302 			     struct inode *inode)
303 {
304 	struct posix_acl *default_acl, *acl;
305 	int err = 0;
306 
307 	/* ACLs only get applied to files and directories */
308 	if (S_ISLNK(inode->i_mode))
309 		return 0;
310 
311 	/*
312 	 * ACLs can only be used on "new" objects, so if it's an old object
313 	 * there is nothing to inherit from
314 	 */
315 	if (get_inode_sd_version(dir) == STAT_DATA_V1)
316 		goto apply_umask;
317 
318 	/*
319 	 * Don't apply ACLs to objects in the .reiserfs_priv tree.. This
320 	 * would be useless since permissions are ignored, and a pain because
321 	 * it introduces locking cycles
322 	 */
323 	if (IS_PRIVATE(dir)) {
324 		inode->i_flags |= S_PRIVATE;
325 		goto apply_umask;
326 	}
327 
328 	err = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl);
329 	if (err)
330 		return err;
331 
332 	if (default_acl) {
333 		err = __reiserfs_set_acl(th, inode, ACL_TYPE_DEFAULT,
334 					 default_acl);
335 		posix_acl_release(default_acl);
336 	}
337 	if (acl) {
338 		if (!err)
339 			err = __reiserfs_set_acl(th, inode, ACL_TYPE_ACCESS,
340 						 acl);
341 		posix_acl_release(acl);
342 	}
343 
344 	return err;
345 
346 apply_umask:
347 	/* no ACL, apply umask */
348 	inode->i_mode &= ~current_umask();
349 	return err;
350 }
351 
352 /* This is used to cache the default acl before a new object is created.
353  * The biggest reason for this is to get an idea of how many blocks will
354  * actually be required for the create operation if we must inherit an ACL.
355  * An ACL write can add up to 3 object creations and an additional file write
356  * so we'd prefer not to reserve that many blocks in the journal if we can.
357  * It also has the advantage of not loading the ACL with a transaction open,
358  * this may seem silly, but if the owner of the directory is doing the
359  * creation, the ACL may not be loaded since the permissions wouldn't require
360  * it.
361  * We return the number of blocks required for the transaction.
362  */
reiserfs_cache_default_acl(struct inode * inode)363 int reiserfs_cache_default_acl(struct inode *inode)
364 {
365 	struct posix_acl *acl;
366 	int nblocks = 0;
367 
368 	if (IS_PRIVATE(inode))
369 		return 0;
370 
371 	acl = reiserfs_get_acl(inode, ACL_TYPE_DEFAULT);
372 
373 	if (acl && !IS_ERR(acl)) {
374 		int size = reiserfs_acl_size(acl->a_count);
375 
376 		/* Other xattrs can be created during inode creation. We don't
377 		 * want to claim too many blocks, so we check to see if we
378 		 * we need to create the tree to the xattrs, and then we
379 		 * just want two files. */
380 		nblocks = reiserfs_xattr_jcreate_nblocks(inode);
381 		nblocks += JOURNAL_BLOCKS_PER_OBJECT(inode->i_sb);
382 
383 		REISERFS_I(inode)->i_flags |= i_has_xattr_dir;
384 
385 		/* We need to account for writes + bitmaps for two files */
386 		nblocks += reiserfs_xattr_nblocks(inode, size) * 4;
387 		posix_acl_release(acl);
388 	}
389 
390 	return nblocks;
391 }
392 
393 /*
394  * Called under i_mutex
395  */
reiserfs_acl_chmod(struct inode * inode)396 int reiserfs_acl_chmod(struct inode *inode)
397 {
398 	if (IS_PRIVATE(inode))
399 		return 0;
400 	if (get_inode_sd_version(inode) == STAT_DATA_V1 ||
401 	    !reiserfs_posixacl(inode->i_sb))
402 		return 0;
403 
404 	return posix_acl_chmod(inode, inode->i_mode);
405 }
406