blob: b74b24ecf0e4ba6bfd86d1197954260afca3d6b2 [file] [log] [blame]
Herbert Poetzlca4d1472006-07-03 17:27:12 -07001/*
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"
Mark Fashehb2580102007-03-09 16:53:21 -080017#include "file.h"
Herbert Poetzlca4d1472006-07-03 17:27:12 -070018#include "inode.h"
19#include "journal.h"
20
21#include "ocfs2_fs.h"
Adrian Bunk2d562512006-07-10 01:32:51 +020022#include "ioctl.h"
Tao Mad6590722007-12-18 15:47:03 +080023#include "resize.h"
Adrian Bunk2d562512006-07-10 01:32:51 +020024
Herbert Poetzlca4d1472006-07-03 17:27:12 -070025#include <linux/ext2_fs.h>
26
27static int ocfs2_get_inode_attr(struct inode *inode, unsigned *flags)
28{
29 int status;
30
Mark Fashehe63aecb62007-10-18 15:30:42 -070031 status = ocfs2_inode_lock(inode, NULL, 0);
Herbert Poetzlca4d1472006-07-03 17:27:12 -070032 if (status < 0) {
33 mlog_errno(status);
34 return status;
35 }
Jan Kara6e4b0d52007-04-27 11:08:01 -070036 ocfs2_get_inode_flags(OCFS2_I(inode));
Herbert Poetzlca4d1472006-07-03 17:27:12 -070037 *flags = OCFS2_I(inode)->ip_attr;
Mark Fashehe63aecb62007-10-18 15:30:42 -070038 ocfs2_inode_unlock(inode, 0);
Herbert Poetzlca4d1472006-07-03 17:27:12 -070039
40 mlog_exit(status);
41 return status;
42}
43
44static int ocfs2_set_inode_attr(struct inode *inode, unsigned flags,
45 unsigned mask)
46{
47 struct ocfs2_inode_info *ocfs2_inode = OCFS2_I(inode);
48 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fasheh1fabe142006-10-09 18:11:45 -070049 handle_t *handle = NULL;
Herbert Poetzlca4d1472006-07-03 17:27:12 -070050 struct buffer_head *bh = NULL;
51 unsigned oldflags;
52 int status;
53
54 mutex_lock(&inode->i_mutex);
55
Mark Fashehe63aecb62007-10-18 15:30:42 -070056 status = ocfs2_inode_lock(inode, &bh, 1);
Herbert Poetzlca4d1472006-07-03 17:27:12 -070057 if (status < 0) {
58 mlog_errno(status);
59 goto bail;
60 }
61
62 status = -EROFS;
63 if (IS_RDONLY(inode))
64 goto bail_unlock;
65
66 status = -EACCES;
Satyam Sharma3bd858a2007-07-17 15:00:08 +053067 if (!is_owner_or_cap(inode))
Herbert Poetzlca4d1472006-07-03 17:27:12 -070068 goto bail_unlock;
69
70 if (!S_ISDIR(inode->i_mode))
71 flags &= ~OCFS2_DIRSYNC_FL;
72
Mark Fasheh65eff9c2006-10-09 17:26:22 -070073 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
Herbert Poetzlca4d1472006-07-03 17:27:12 -070074 if (IS_ERR(handle)) {
75 status = PTR_ERR(handle);
76 mlog_errno(status);
77 goto bail_unlock;
78 }
79
80 oldflags = ocfs2_inode->ip_attr;
81 flags = flags & mask;
82 flags |= oldflags & ~mask;
83
84 /*
85 * The IMMUTABLE and APPEND_ONLY flags can only be changed by
86 * the relevant capability.
87 */
88 status = -EPERM;
89 if ((oldflags & OCFS2_IMMUTABLE_FL) || ((flags ^ oldflags) &
90 (OCFS2_APPEND_FL | OCFS2_IMMUTABLE_FL))) {
91 if (!capable(CAP_LINUX_IMMUTABLE))
92 goto bail_unlock;
93 }
94
95 ocfs2_inode->ip_attr = flags;
96 ocfs2_set_inode_flags(inode);
97
98 status = ocfs2_mark_inode_dirty(handle, inode, bh);
99 if (status < 0)
100 mlog_errno(status);
101
Mark Fasheh02dc1af2006-10-09 16:48:10 -0700102 ocfs2_commit_trans(osb, handle);
Herbert Poetzlca4d1472006-07-03 17:27:12 -0700103bail_unlock:
Mark Fashehe63aecb62007-10-18 15:30:42 -0700104 ocfs2_inode_unlock(inode, 1);
Herbert Poetzlca4d1472006-07-03 17:27:12 -0700105bail:
106 mutex_unlock(&inode->i_mutex);
107
108 if (bh)
109 brelse(bh);
110
111 mlog_exit(status);
112 return status;
113}
114
115int ocfs2_ioctl(struct inode * inode, struct file * filp,
116 unsigned int cmd, unsigned long arg)
117{
118 unsigned int flags;
Tao Mad6590722007-12-18 15:47:03 +0800119 int new_clusters;
Herbert Poetzlca4d1472006-07-03 17:27:12 -0700120 int status;
Mark Fashehb2580102007-03-09 16:53:21 -0800121 struct ocfs2_space_resv sr;
Herbert Poetzlca4d1472006-07-03 17:27:12 -0700122
123 switch (cmd) {
124 case OCFS2_IOC_GETFLAGS:
125 status = ocfs2_get_inode_attr(inode, &flags);
126 if (status < 0)
127 return status;
128
129 flags &= OCFS2_FL_VISIBLE;
130 return put_user(flags, (int __user *) arg);
131 case OCFS2_IOC_SETFLAGS:
132 if (get_user(flags, (int __user *) arg))
133 return -EFAULT;
134
135 return ocfs2_set_inode_attr(inode, flags,
136 OCFS2_FL_MODIFIABLE);
Mark Fashehb2580102007-03-09 16:53:21 -0800137 case OCFS2_IOC_RESVSP:
138 case OCFS2_IOC_RESVSP64:
139 case OCFS2_IOC_UNRESVSP:
140 case OCFS2_IOC_UNRESVSP64:
141 if (copy_from_user(&sr, (int __user *) arg, sizeof(sr)))
142 return -EFAULT;
143
144 return ocfs2_change_file_space(filp, cmd, &sr);
Tao Mad6590722007-12-18 15:47:03 +0800145 case OCFS2_IOC_GROUP_EXTEND:
146 if (get_user(new_clusters, (int __user *)arg))
147 return -EFAULT;
148
149 return ocfs2_group_extend(inode, new_clusters);
Herbert Poetzlca4d1472006-07-03 17:27:12 -0700150 default:
151 return -ENOTTY;
152 }
153}
154
Mark Fasheh586d2322007-03-09 15:56:28 -0800155#ifdef CONFIG_COMPAT
156long ocfs2_compat_ioctl(struct file *file, unsigned cmd, unsigned long arg)
157{
158 struct inode *inode = file->f_path.dentry->d_inode;
159 int ret;
160
161 switch (cmd) {
162 case OCFS2_IOC32_GETFLAGS:
163 cmd = OCFS2_IOC_GETFLAGS;
164 break;
165 case OCFS2_IOC32_SETFLAGS:
166 cmd = OCFS2_IOC_SETFLAGS;
167 break;
Mark Fashehb2580102007-03-09 16:53:21 -0800168 case OCFS2_IOC_RESVSP:
169 case OCFS2_IOC_RESVSP64:
170 case OCFS2_IOC_UNRESVSP:
171 case OCFS2_IOC_UNRESVSP64:
Tao Mad6590722007-12-18 15:47:03 +0800172 case OCFS2_IOC_GROUP_EXTEND:
Mark Fashehb2580102007-03-09 16:53:21 -0800173 break;
Mark Fasheh586d2322007-03-09 15:56:28 -0800174 default:
175 return -ENOIOCTLCMD;
176 }
177
178 lock_kernel();
179 ret = ocfs2_ioctl(inode, file, cmd, arg);
180 unlock_kernel();
181 return ret;
182}
183#endif