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