~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/fs/ocfs2/acl.c

Version: ~ [ linux-5.10-rc6 ] ~ [ linux-5.9.12 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.81 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.161 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.210 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.247 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.247 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.85 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /* -*- mode: c; c-basic-offset: 8; -*-
  2  * vim: noexpandtab sw=8 ts=8 sts=0:
  3  *
  4  * acl.c
  5  *
  6  * Copyright (C) 2004, 2008 Oracle.  All rights reserved.
  7  *
  8  * CREDITS:
  9  * Lots of code in this file is copy from linux/fs/ext3/acl.c.
 10  * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
 11  *
 12  * This program is free software; you can redistribute it and/or
 13  * modify it under the terms of the GNU General Public
 14  * License version 2 as published by the Free Software Foundation.
 15  *
 16  * This program is distributed in the hope that it will be useful,
 17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 19  * General Public License for more details.
 20  */
 21 
 22 #include <linux/init.h>
 23 #include <linux/module.h>
 24 #include <linux/slab.h>
 25 #include <linux/string.h>
 26 
 27 #include <cluster/masklog.h>
 28 
 29 #include "ocfs2.h"
 30 #include "alloc.h"
 31 #include "dlmglue.h"
 32 #include "file.h"
 33 #include "inode.h"
 34 #include "journal.h"
 35 #include "ocfs2_fs.h"
 36 
 37 #include "xattr.h"
 38 #include "acl.h"
 39 
 40 /*
 41  * Convert from xattr value to acl struct.
 42  */
 43 static struct posix_acl *ocfs2_acl_from_xattr(const void *value, size_t size)
 44 {
 45         int n, count;
 46         struct posix_acl *acl;
 47 
 48         if (!value)
 49                 return NULL;
 50         if (size < sizeof(struct posix_acl_entry))
 51                 return ERR_PTR(-EINVAL);
 52 
 53         count = size / sizeof(struct posix_acl_entry);
 54 
 55         acl = posix_acl_alloc(count, GFP_NOFS);
 56         if (!acl)
 57                 return ERR_PTR(-ENOMEM);
 58         for (n = 0; n < count; n++) {
 59                 struct ocfs2_acl_entry *entry =
 60                         (struct ocfs2_acl_entry *)value;
 61 
 62                 acl->a_entries[n].e_tag  = le16_to_cpu(entry->e_tag);
 63                 acl->a_entries[n].e_perm = le16_to_cpu(entry->e_perm);
 64                 switch(acl->a_entries[n].e_tag) {
 65                 case ACL_USER:
 66                         acl->a_entries[n].e_uid =
 67                                 make_kuid(&init_user_ns,
 68                                           le32_to_cpu(entry->e_id));
 69                         break;
 70                 case ACL_GROUP:
 71                         acl->a_entries[n].e_gid =
 72                                 make_kgid(&init_user_ns,
 73                                           le32_to_cpu(entry->e_id));
 74                         break;
 75                 default:
 76                         break;
 77                 }
 78                 value += sizeof(struct posix_acl_entry);
 79 
 80         }
 81         return acl;
 82 }
 83 
 84 /*
 85  * Convert acl struct to xattr value.
 86  */
 87 static void *ocfs2_acl_to_xattr(const struct posix_acl *acl, size_t *size)
 88 {
 89         struct ocfs2_acl_entry *entry = NULL;
 90         char *ocfs2_acl;
 91         size_t n;
 92 
 93         *size = acl->a_count * sizeof(struct posix_acl_entry);
 94 
 95         ocfs2_acl = kmalloc(*size, GFP_NOFS);
 96         if (!ocfs2_acl)
 97                 return ERR_PTR(-ENOMEM);
 98 
 99         entry = (struct ocfs2_acl_entry *)ocfs2_acl;
100         for (n = 0; n < acl->a_count; n++, entry++) {
101                 entry->e_tag  = cpu_to_le16(acl->a_entries[n].e_tag);
102                 entry->e_perm = cpu_to_le16(acl->a_entries[n].e_perm);
103                 switch(acl->a_entries[n].e_tag) {
104                 case ACL_USER:
105                         entry->e_id = cpu_to_le32(
106                                 from_kuid(&init_user_ns,
107                                           acl->a_entries[n].e_uid));
108                         break;
109                 case ACL_GROUP:
110                         entry->e_id = cpu_to_le32(
111                                 from_kgid(&init_user_ns,
112                                           acl->a_entries[n].e_gid));
113                         break;
114                 default:
115                         entry->e_id = cpu_to_le32(ACL_UNDEFINED_ID);
116                         break;
117                 }
118         }
119         return ocfs2_acl;
120 }
121 
122 static struct posix_acl *ocfs2_get_acl_nolock(struct inode *inode,
123                                               int type,
124                                               struct buffer_head *di_bh)
125 {
126         int name_index;
127         char *value = NULL;
128         struct posix_acl *acl;
129         int retval;
130 
131         switch (type) {
132         case ACL_TYPE_ACCESS:
133                 name_index = OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS;
134                 break;
135         case ACL_TYPE_DEFAULT:
136                 name_index = OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT;
137                 break;
138         default:
139                 return ERR_PTR(-EINVAL);
140         }
141 
142         retval = ocfs2_xattr_get_nolock(inode, di_bh, name_index, "", NULL, 0);
143         if (retval > 0) {
144                 value = kmalloc(retval, GFP_NOFS);
145                 if (!value)
146                         return ERR_PTR(-ENOMEM);
147                 retval = ocfs2_xattr_get_nolock(inode, di_bh, name_index,
148                                                 "", value, retval);
149         }
150 
151         if (retval > 0)
152                 acl = ocfs2_acl_from_xattr(value, retval);
153         else if (retval == -ENODATA || retval == 0)
154                 acl = NULL;
155         else
156                 acl = ERR_PTR(retval);
157 
158         kfree(value);
159 
160         return acl;
161 }
162 
163 
164 /*
165  * Get posix acl.
166  */
167 static struct posix_acl *ocfs2_get_acl(struct inode *inode, int type)
168 {
169         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
170         struct buffer_head *di_bh = NULL;
171         struct posix_acl *acl;
172         int ret;
173 
174         if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
175                 return NULL;
176 
177         ret = ocfs2_inode_lock(inode, &di_bh, 0);
178         if (ret < 0) {
179                 mlog_errno(ret);
180                 acl = ERR_PTR(ret);
181                 return acl;
182         }
183 
184         acl = ocfs2_get_acl_nolock(inode, type, di_bh);
185 
186         ocfs2_inode_unlock(inode, 0);
187 
188         brelse(di_bh);
189 
190         return acl;
191 }
192 
193 /*
194  * Helper function to set i_mode in memory and disk. Some call paths
195  * will not have di_bh or a journal handle to pass, in which case it
196  * will create it's own.
197  */
198 static int ocfs2_acl_set_mode(struct inode *inode, struct buffer_head *di_bh,
199                               handle_t *handle, umode_t new_mode)
200 {
201         int ret, commit_handle = 0;
202         struct ocfs2_dinode *di;
203 
204         if (di_bh == NULL) {
205                 ret = ocfs2_read_inode_block(inode, &di_bh);
206                 if (ret) {
207                         mlog_errno(ret);
208                         goto out;
209                 }
210         } else
211                 get_bh(di_bh);
212 
213         if (handle == NULL) {
214                 handle = ocfs2_start_trans(OCFS2_SB(inode->i_sb),
215                                            OCFS2_INODE_UPDATE_CREDITS);
216                 if (IS_ERR(handle)) {
217                         ret = PTR_ERR(handle);
218                         mlog_errno(ret);
219                         goto out_brelse;
220                 }
221 
222                 commit_handle = 1;
223         }
224 
225         di = (struct ocfs2_dinode *)di_bh->b_data;
226         ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
227                                       OCFS2_JOURNAL_ACCESS_WRITE);
228         if (ret) {
229                 mlog_errno(ret);
230                 goto out_commit;
231         }
232 
233         inode->i_mode = new_mode;
234         inode->i_ctime = CURRENT_TIME;
235         di->i_mode = cpu_to_le16(inode->i_mode);
236         di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
237         di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
238 
239         ocfs2_journal_dirty(handle, di_bh);
240 
241 out_commit:
242         if (commit_handle)
243                 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
244 out_brelse:
245         brelse(di_bh);
246 out:
247         return ret;
248 }
249 
250 /*
251  * Set the access or default ACL of an inode.
252  */
253 static int ocfs2_set_acl(handle_t *handle,
254                          struct inode *inode,
255                          struct buffer_head *di_bh,
256                          int type,
257                          struct posix_acl *acl,
258                          struct ocfs2_alloc_context *meta_ac,
259                          struct ocfs2_alloc_context *data_ac)
260 {
261         int name_index;
262         void *value = NULL;
263         size_t size = 0;
264         int ret;
265 
266         if (S_ISLNK(inode->i_mode))
267                 return -EOPNOTSUPP;
268 
269         switch (type) {
270         case ACL_TYPE_ACCESS:
271                 name_index = OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS;
272                 if (acl) {
273                         umode_t mode;
274                         ret = posix_acl_update_mode(inode, &mode, &acl);
275                         if (ret)
276                                 return ret;
277                         ret = ocfs2_acl_set_mode(inode, di_bh,
278                                                  handle, mode);
279                         if (ret)
280                                 return ret;
281                 }
282                 break;
283         case ACL_TYPE_DEFAULT:
284                 name_index = OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT;
285                 if (!S_ISDIR(inode->i_mode))
286                         return acl ? -EACCES : 0;
287                 break;
288         default:
289                 return -EINVAL;
290         }
291 
292         if (acl) {
293                 value = ocfs2_acl_to_xattr(acl, &size);
294                 if (IS_ERR(value))
295                         return (int)PTR_ERR(value);
296         }
297 
298         if (handle)
299                 ret = ocfs2_xattr_set_handle(handle, inode, di_bh, name_index,
300                                              "", value, size, 0,
301                                              meta_ac, data_ac);
302         else
303                 ret = ocfs2_xattr_set(inode, name_index, "", value, size, 0);
304 
305         kfree(value);
306 
307         return ret;
308 }
309 
310 struct posix_acl *ocfs2_iop_get_acl(struct inode *inode, int type)
311 {
312         struct ocfs2_super *osb;
313         struct buffer_head *di_bh = NULL;
314         struct posix_acl *acl;
315         int ret = -EAGAIN;
316 
317         osb = OCFS2_SB(inode->i_sb);
318         if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
319                 return NULL;
320 
321         ret = ocfs2_read_inode_block(inode, &di_bh);
322         if (ret < 0)
323                 return ERR_PTR(ret);
324 
325         acl = ocfs2_get_acl_nolock(inode, type, di_bh);
326 
327         brelse(di_bh);
328 
329         return acl;
330 }
331 
332 int ocfs2_acl_chmod(struct inode *inode)
333 {
334         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
335         struct posix_acl *acl;
336         int ret;
337 
338         if (S_ISLNK(inode->i_mode))
339                 return -EOPNOTSUPP;
340 
341         if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
342                 return 0;
343 
344         acl = ocfs2_get_acl(inode, ACL_TYPE_ACCESS);
345         if (IS_ERR(acl) || !acl)
346                 return PTR_ERR(acl);
347         ret = posix_acl_chmod(&acl, GFP_KERNEL, inode->i_mode);
348         if (ret)
349                 return ret;
350         ret = ocfs2_set_acl(NULL, inode, NULL, ACL_TYPE_ACCESS,
351                             acl, NULL, NULL);
352         posix_acl_release(acl);
353         return ret;
354 }
355 
356 /*
357  * Initialize the ACLs of a new inode. If parent directory has default ACL,
358  * then clone to new inode. Called from ocfs2_mknod.
359  */
360 int ocfs2_init_acl(handle_t *handle,
361                    struct inode *inode,
362                    struct inode *dir,
363                    struct buffer_head *di_bh,
364                    struct buffer_head *dir_bh,
365                    struct ocfs2_alloc_context *meta_ac,
366                    struct ocfs2_alloc_context *data_ac)
367 {
368         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
369         struct posix_acl *acl = NULL;
370         int ret = 0, ret2;
371         umode_t mode;
372 
373         if (!S_ISLNK(inode->i_mode)) {
374                 if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) {
375                         acl = ocfs2_get_acl_nolock(dir, ACL_TYPE_DEFAULT,
376                                                    dir_bh);
377                         if (IS_ERR(acl))
378                                 return PTR_ERR(acl);
379                 }
380                 if (!acl) {
381                         mode = inode->i_mode & ~current_umask();
382                         ret = ocfs2_acl_set_mode(inode, di_bh, handle, mode);
383                         if (ret) {
384                                 mlog_errno(ret);
385                                 goto cleanup;
386                         }
387                 }
388         }
389         if ((osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) && acl) {
390                 if (S_ISDIR(inode->i_mode)) {
391                         ret = ocfs2_set_acl(handle, inode, di_bh,
392                                             ACL_TYPE_DEFAULT, acl,
393                                             meta_ac, data_ac);
394                         if (ret)
395                                 goto cleanup;
396                 }
397                 mode = inode->i_mode;
398                 ret = posix_acl_create(&acl, GFP_NOFS, &mode);
399                 if (ret < 0)
400                         return ret;
401 
402                 ret2 = ocfs2_acl_set_mode(inode, di_bh, handle, mode);
403                 if (ret2) {
404                         mlog_errno(ret2);
405                         ret = ret2;
406                         goto cleanup;
407                 }
408                 if (ret > 0) {
409                         ret = ocfs2_set_acl(handle, inode,
410                                             di_bh, ACL_TYPE_ACCESS,
411                                             acl, meta_ac, data_ac);
412                 }
413         }
414 cleanup:
415         posix_acl_release(acl);
416         return ret;
417 }
418 
419 static size_t ocfs2_xattr_list_acl_access(struct dentry *dentry,
420                                           char *list,
421                                           size_t list_len,
422                                           const char *name,
423                                           size_t name_len,
424                                           int type)
425 {
426         struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
427         const size_t size = sizeof(POSIX_ACL_XATTR_ACCESS);
428 
429         if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
430                 return 0;
431 
432         if (list && size <= list_len)
433                 memcpy(list, POSIX_ACL_XATTR_ACCESS, size);
434         return size;
435 }
436 
437 static size_t ocfs2_xattr_list_acl_default(struct dentry *dentry,
438                                            char *list,
439                                            size_t list_len,
440                                            const char *name,
441                                            size_t name_len,
442                                            int type)
443 {
444         struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
445         const size_t size = sizeof(POSIX_ACL_XATTR_DEFAULT);
446 
447         if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
448                 return 0;
449 
450         if (list && size <= list_len)
451                 memcpy(list, POSIX_ACL_XATTR_DEFAULT, size);
452         return size;
453 }
454 
455 static int ocfs2_xattr_get_acl(struct dentry *dentry, const char *name,
456                 void *buffer, size_t size, int type)
457 {
458         struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
459         struct posix_acl *acl;
460         int ret;
461 
462         if (strcmp(name, "") != 0)
463                 return -EINVAL;
464         if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
465                 return -EOPNOTSUPP;
466 
467         acl = ocfs2_get_acl(dentry->d_inode, type);
468         if (IS_ERR(acl))
469                 return PTR_ERR(acl);
470         if (acl == NULL)
471                 return -ENODATA;
472         ret = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
473         posix_acl_release(acl);
474 
475         return ret;
476 }
477 
478 static int ocfs2_xattr_set_acl(struct dentry *dentry, const char *name,
479                 const void *value, size_t size, int flags, int type)
480 {
481         struct inode *inode = dentry->d_inode;
482         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
483         struct posix_acl *acl;
484         int ret = 0;
485 
486         if (strcmp(name, "") != 0)
487                 return -EINVAL;
488         if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
489                 return -EOPNOTSUPP;
490 
491         if (!inode_owner_or_capable(inode))
492                 return -EPERM;
493 
494         if (value) {
495                 acl = posix_acl_from_xattr(&init_user_ns, value, size);
496                 if (IS_ERR(acl))
497                         return PTR_ERR(acl);
498                 else if (acl) {
499                         ret = posix_acl_valid(acl);
500                         if (ret)
501                                 goto cleanup;
502                 }
503         } else
504                 acl = NULL;
505 
506         ret = ocfs2_set_acl(NULL, inode, NULL, type, acl, NULL, NULL);
507 
508 cleanup:
509         posix_acl_release(acl);
510         return ret;
511 }
512 
513 const struct xattr_handler ocfs2_xattr_acl_access_handler = {
514         .prefix = POSIX_ACL_XATTR_ACCESS,
515         .flags  = ACL_TYPE_ACCESS,
516         .list   = ocfs2_xattr_list_acl_access,
517         .get    = ocfs2_xattr_get_acl,
518         .set    = ocfs2_xattr_set_acl,
519 };
520 
521 const struct xattr_handler ocfs2_xattr_acl_default_handler = {
522         .prefix = POSIX_ACL_XATTR_DEFAULT,
523         .flags  = ACL_TYPE_DEFAULT,
524         .list   = ocfs2_xattr_list_acl_default,
525         .get    = ocfs2_xattr_get_acl,
526         .set    = ocfs2_xattr_set_acl,
527 };
528 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp