4 * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
7 #include <linux/quotaops.h>
14 * Convert from filesystem to in-memory representation.
16 static struct posix_acl
*
17 ext4_acl_from_disk(struct super_block
*sb
, const void *value
, size_t size
)
19 const char *end
= (char *)value
+ size
;
21 struct posix_acl
*acl
;
25 if (size
< sizeof(ext4_acl_header
))
26 return ERR_PTR(-EINVAL
);
27 if (((ext4_acl_header
*)value
)->a_version
!=
28 cpu_to_le32(EXT4_ACL_VERSION
))
29 return ERR_PTR(-EINVAL
);
30 value
= (char *)value
+ sizeof(ext4_acl_header
);
31 count
= ext4_acl_count(size
);
33 return ERR_PTR(-EINVAL
);
36 acl
= posix_acl_alloc(count
, GFP_NOFS
);
38 return ERR_PTR(-ENOMEM
);
39 for (n
= 0; n
< count
; n
++) {
40 ext4_acl_entry
*entry
=
41 (ext4_acl_entry
*)value
;
42 if ((char *)value
+ sizeof(ext4_acl_entry_short
) > end
)
44 acl
->a_entries
[n
].e_tag
= le16_to_cpu(entry
->e_tag
);
45 acl
->a_entries
[n
].e_perm
= le16_to_cpu(entry
->e_perm
);
47 switch (acl
->a_entries
[n
].e_tag
) {
52 value
= (char *)value
+
53 sizeof(ext4_acl_entry_short
);
57 value
= (char *)value
+ sizeof(ext4_acl_entry
);
58 if ((char *)value
> end
)
60 acl
->a_entries
[n
].e_uid
=
61 make_kuid(sb
->s_user_ns
,
62 le32_to_cpu(entry
->e_id
));
63 if (!uid_valid(acl
->a_entries
[n
].e_uid
))
67 value
= (char *)value
+ sizeof(ext4_acl_entry
);
68 if ((char *)value
> end
)
70 acl
->a_entries
[n
].e_gid
=
71 make_kgid(sb
->s_user_ns
,
72 le32_to_cpu(entry
->e_id
));
73 if (!gid_valid(acl
->a_entries
[n
].e_gid
))
86 posix_acl_release(acl
);
87 return ERR_PTR(-EINVAL
);
91 * Convert from in-memory to filesystem representation.
94 ext4_acl_to_disk(struct super_block
*sb
, const struct posix_acl
*acl
,
97 ext4_acl_header
*ext_acl
;
103 *size
= ext4_acl_size(acl
->a_count
);
104 ext_acl
= kmalloc(sizeof(ext4_acl_header
) + acl
->a_count
*
105 sizeof(ext4_acl_entry
), GFP_NOFS
);
107 return ERR_PTR(-ENOMEM
);
108 ext_acl
->a_version
= cpu_to_le32(EXT4_ACL_VERSION
);
109 e
= (char *)ext_acl
+ sizeof(ext4_acl_header
);
110 for (n
= 0; n
< acl
->a_count
; n
++) {
111 const struct posix_acl_entry
*acl_e
= &acl
->a_entries
[n
];
112 ext4_acl_entry
*entry
= (ext4_acl_entry
*)e
;
113 entry
->e_tag
= cpu_to_le16(acl_e
->e_tag
);
114 entry
->e_perm
= cpu_to_le16(acl_e
->e_perm
);
115 switch (acl_e
->e_tag
) {
117 uid
= from_kuid(sb
->s_user_ns
, acl_e
->e_uid
);
118 if (uid
== (uid_t
)-1)
120 entry
->e_id
= cpu_to_le32(uid
);
121 e
+= sizeof(ext4_acl_entry
);
124 gid
= from_kgid(sb
->s_user_ns
, acl_e
->e_gid
);
125 if (gid
== (gid_t
)-1)
127 entry
->e_id
= cpu_to_le32(gid
);
128 e
+= sizeof(ext4_acl_entry
);
135 e
+= sizeof(ext4_acl_entry_short
);
142 return (char *)ext_acl
;
146 return ERR_PTR(-EINVAL
);
150 * Inode operation get_posix_acl().
152 * inode->i_mutex: don't care
155 ext4_get_acl(struct inode
*inode
, int type
)
159 struct posix_acl
*acl
;
163 case ACL_TYPE_ACCESS
:
164 name_index
= EXT4_XATTR_INDEX_POSIX_ACL_ACCESS
;
166 case ACL_TYPE_DEFAULT
:
167 name_index
= EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT
;
172 retval
= ext4_xattr_get(inode
, name_index
, "", NULL
, 0);
174 value
= kmalloc(retval
, GFP_NOFS
);
176 return ERR_PTR(-ENOMEM
);
177 retval
= ext4_xattr_get(inode
, name_index
, "", value
, retval
);
180 acl
= ext4_acl_from_disk(inode
->i_sb
, value
, retval
);
181 else if (retval
== -ENODATA
|| retval
== -ENOSYS
)
184 acl
= ERR_PTR(retval
);
191 * Set the access or default ACL of an inode.
193 * inode->i_mutex: down unless called from ext4_new_inode
196 __ext4_set_acl(handle_t
*handle
, struct inode
*inode
, int type
,
197 struct posix_acl
*acl
, int xattr_flags
)
205 case ACL_TYPE_ACCESS
:
206 name_index
= EXT4_XATTR_INDEX_POSIX_ACL_ACCESS
;
209 case ACL_TYPE_DEFAULT
:
210 name_index
= EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT
;
211 if (!S_ISDIR(inode
->i_mode
))
212 return acl
? -EACCES
: 0;
219 value
= ext4_acl_to_disk(inode
->i_sb
, acl
, &size
);
221 return (int)PTR_ERR(value
);
224 error
= ext4_xattr_set_handle(handle
, inode
, name_index
, "",
225 value
, size
, xattr_flags
);
229 set_cached_acl(inode
, type
, acl
);
236 ext4_set_acl(struct inode
*inode
, struct posix_acl
*acl
, int type
)
239 int error
, credits
, retries
= 0;
240 size_t acl_size
= acl
? ext4_acl_size(acl
->a_count
) : 0;
241 umode_t mode
= inode
->i_mode
;
244 error
= dquot_initialize(inode
);
248 error
= ext4_xattr_set_credits(inode
, acl_size
, false /* is_create */,
253 handle
= ext4_journal_start(inode
, EXT4_HT_XATTR
, credits
);
255 return PTR_ERR(handle
);
257 if ((type
== ACL_TYPE_ACCESS
) && acl
) {
258 error
= posix_acl_update_mode(inode
, &mode
, &acl
);
264 error
= __ext4_set_acl(handle
, inode
, type
, acl
, 0 /* xattr_flags */);
265 if (!error
&& update_mode
) {
266 inode
->i_mode
= mode
;
267 inode
->i_ctime
= current_time(inode
);
268 ext4_mark_inode_dirty(handle
, inode
);
271 ext4_journal_stop(handle
);
272 if (error
== -ENOSPC
&& ext4_should_retry_alloc(inode
->i_sb
, &retries
))
278 * Initialize the ACLs of a new inode. Called from ext4_new_inode.
281 * inode->i_mutex: up (access to inode is still exclusive)
284 ext4_init_acl(handle_t
*handle
, struct inode
*inode
, struct inode
*dir
)
286 struct posix_acl
*default_acl
, *acl
;
289 error
= posix_acl_create(dir
, &inode
->i_mode
, &default_acl
, &acl
);
294 error
= __ext4_set_acl(handle
, inode
, ACL_TYPE_DEFAULT
,
295 default_acl
, XATTR_CREATE
);
296 posix_acl_release(default_acl
);
300 error
= __ext4_set_acl(handle
, inode
, ACL_TYPE_ACCESS
,
302 posix_acl_release(acl
);