ocfs2: add ext2 attributes
[safe/jmp/linux-2.6] / fs / ocfs2 / ioctl.c
1 /*
2  * linux/fs/ocfs2/ioctl.c
3  *
4  * Copyright (C) 2006 Herbert Poetzl
5  * adapted from Remy Card's ext2/ioctl.c
6  */
7
8 #include <linux/fs.h>
9 #include <linux/mount.h>
10
11 #define MLOG_MASK_PREFIX ML_INODE
12 #include <cluster/masklog.h>
13
14 #include "ocfs2.h"
15 #include "alloc.h"
16 #include "dlmglue.h"
17 #include "inode.h"
18 #include "journal.h"
19
20 #include "ocfs2_fs.h"
21 #include <linux/ext2_fs.h>
22
23 static int ocfs2_get_inode_attr(struct inode *inode, unsigned *flags)
24 {
25         int status;
26
27         status = ocfs2_meta_lock(inode, NULL, NULL, 0);
28         if (status < 0) {
29                 mlog_errno(status);
30                 return status;
31         }
32         *flags = OCFS2_I(inode)->ip_attr;
33         ocfs2_meta_unlock(inode, 0);
34
35         mlog_exit(status);
36         return status;
37 }
38
39 static int ocfs2_set_inode_attr(struct inode *inode, unsigned flags,
40                                 unsigned mask)
41 {
42         struct ocfs2_inode_info *ocfs2_inode = OCFS2_I(inode);
43         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
44         struct ocfs2_journal_handle *handle = NULL;
45         struct buffer_head *bh = NULL;
46         unsigned oldflags;
47         int status;
48
49         mutex_lock(&inode->i_mutex);
50
51         status = ocfs2_meta_lock(inode, NULL, &bh, 1);
52         if (status < 0) {
53                 mlog_errno(status);
54                 goto bail;
55         }
56
57         status = -EROFS;
58         if (IS_RDONLY(inode))
59                 goto bail_unlock;
60
61         status = -EACCES;
62         if ((current->fsuid != inode->i_uid) && !capable(CAP_FOWNER))
63                 goto bail_unlock;
64
65         if (!S_ISDIR(inode->i_mode))
66                 flags &= ~OCFS2_DIRSYNC_FL;
67
68         handle = ocfs2_start_trans(osb, NULL, OCFS2_INODE_UPDATE_CREDITS);
69         if (IS_ERR(handle)) {
70                 status = PTR_ERR(handle);
71                 mlog_errno(status);
72                 goto bail_unlock;
73         }
74
75         oldflags = ocfs2_inode->ip_attr;
76         flags = flags & mask;
77         flags |= oldflags & ~mask;
78
79         /*
80          * The IMMUTABLE and APPEND_ONLY flags can only be changed by
81          * the relevant capability.
82          */
83         status = -EPERM;
84         if ((oldflags & OCFS2_IMMUTABLE_FL) || ((flags ^ oldflags) &
85                 (OCFS2_APPEND_FL | OCFS2_IMMUTABLE_FL))) {
86                 if (!capable(CAP_LINUX_IMMUTABLE))
87                         goto bail_unlock;
88         }
89
90         ocfs2_inode->ip_attr = flags;
91         ocfs2_set_inode_flags(inode);
92
93         status = ocfs2_mark_inode_dirty(handle, inode, bh);
94         if (status < 0)
95                 mlog_errno(status);
96
97         ocfs2_commit_trans(handle);
98 bail_unlock:
99         ocfs2_meta_unlock(inode, 1);
100 bail:
101         mutex_unlock(&inode->i_mutex);
102
103         if (bh)
104                 brelse(bh);
105
106         mlog_exit(status);
107         return status;
108 }
109
110 int ocfs2_ioctl(struct inode * inode, struct file * filp,
111         unsigned int cmd, unsigned long arg)
112 {
113         unsigned int flags;
114         int status;
115
116         switch (cmd) {
117         case OCFS2_IOC_GETFLAGS:
118                 status = ocfs2_get_inode_attr(inode, &flags);
119                 if (status < 0)
120                         return status;
121
122                 flags &= OCFS2_FL_VISIBLE;
123                 return put_user(flags, (int __user *) arg);
124         case OCFS2_IOC_SETFLAGS:
125                 if (get_user(flags, (int __user *) arg))
126                         return -EFAULT;
127
128                 return ocfs2_set_inode_attr(inode, flags,
129                         OCFS2_FL_MODIFIABLE);
130         default:
131                 return -ENOTTY;
132         }
133 }
134