blob: 7d9d9c132cef3a5d59a412aa4d0d3c26c10a5fb0 [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>
Tao Ma34e6c592010-01-27 10:21:52 +080010#include <linux/compat.h>
Herbert Poetzlca4d1472006-07-03 17:27:12 -070011
12#define MLOG_MASK_PREFIX ML_INODE
13#include <cluster/masklog.h>
14
15#include "ocfs2.h"
16#include "alloc.h"
17#include "dlmglue.h"
Mark Fashehb2580102007-03-09 16:53:21 -080018#include "file.h"
Herbert Poetzlca4d1472006-07-03 17:27:12 -070019#include "inode.h"
20#include "journal.h"
21
22#include "ocfs2_fs.h"
Adrian Bunk2d562512006-07-10 01:32:51 +020023#include "ioctl.h"
Tao Mad6590722007-12-18 15:47:03 +080024#include "resize.h"
Tao Mabd508732009-09-21 11:25:14 +080025#include "refcounttree.h"
Adrian Bunk2d562512006-07-10 01:32:51 +020026
Herbert Poetzlca4d1472006-07-03 17:27:12 -070027#include <linux/ext2_fs.h>
28
29static int ocfs2_get_inode_attr(struct inode *inode, unsigned *flags)
30{
31 int status;
32
Mark Fashehe63aecb62007-10-18 15:30:42 -070033 status = ocfs2_inode_lock(inode, NULL, 0);
Herbert Poetzlca4d1472006-07-03 17:27:12 -070034 if (status < 0) {
35 mlog_errno(status);
36 return status;
37 }
Jan Kara6e4b0d52007-04-27 11:08:01 -070038 ocfs2_get_inode_flags(OCFS2_I(inode));
Herbert Poetzlca4d1472006-07-03 17:27:12 -070039 *flags = OCFS2_I(inode)->ip_attr;
Mark Fashehe63aecb62007-10-18 15:30:42 -070040 ocfs2_inode_unlock(inode, 0);
Herbert Poetzlca4d1472006-07-03 17:27:12 -070041
42 mlog_exit(status);
43 return status;
44}
45
46static int ocfs2_set_inode_attr(struct inode *inode, unsigned flags,
47 unsigned mask)
48{
49 struct ocfs2_inode_info *ocfs2_inode = OCFS2_I(inode);
50 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fasheh1fabe142006-10-09 18:11:45 -070051 handle_t *handle = NULL;
Herbert Poetzlca4d1472006-07-03 17:27:12 -070052 struct buffer_head *bh = NULL;
53 unsigned oldflags;
54 int status;
55
56 mutex_lock(&inode->i_mutex);
57
Mark Fashehe63aecb62007-10-18 15:30:42 -070058 status = ocfs2_inode_lock(inode, &bh, 1);
Herbert Poetzlca4d1472006-07-03 17:27:12 -070059 if (status < 0) {
60 mlog_errno(status);
61 goto bail;
62 }
63
Herbert Poetzlca4d1472006-07-03 17:27:12 -070064 status = -EACCES;
Satyam Sharma3bd858a2007-07-17 15:00:08 +053065 if (!is_owner_or_cap(inode))
Herbert Poetzlca4d1472006-07-03 17:27:12 -070066 goto bail_unlock;
67
68 if (!S_ISDIR(inode->i_mode))
69 flags &= ~OCFS2_DIRSYNC_FL;
70
Mark Fasheh65eff9c2006-10-09 17:26:22 -070071 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
Herbert Poetzlca4d1472006-07-03 17:27:12 -070072 if (IS_ERR(handle)) {
73 status = PTR_ERR(handle);
74 mlog_errno(status);
75 goto bail_unlock;
76 }
77
78 oldflags = ocfs2_inode->ip_attr;
79 flags = flags & mask;
80 flags |= oldflags & ~mask;
81
82 /*
83 * The IMMUTABLE and APPEND_ONLY flags can only be changed by
84 * the relevant capability.
85 */
86 status = -EPERM;
87 if ((oldflags & OCFS2_IMMUTABLE_FL) || ((flags ^ oldflags) &
88 (OCFS2_APPEND_FL | OCFS2_IMMUTABLE_FL))) {
89 if (!capable(CAP_LINUX_IMMUTABLE))
90 goto bail_unlock;
91 }
92
93 ocfs2_inode->ip_attr = flags;
94 ocfs2_set_inode_flags(inode);
95
96 status = ocfs2_mark_inode_dirty(handle, inode, bh);
97 if (status < 0)
98 mlog_errno(status);
99
Mark Fasheh02dc1af2006-10-09 16:48:10 -0700100 ocfs2_commit_trans(osb, handle);
Herbert Poetzlca4d1472006-07-03 17:27:12 -0700101bail_unlock:
Mark Fashehe63aecb62007-10-18 15:30:42 -0700102 ocfs2_inode_unlock(inode, 1);
Herbert Poetzlca4d1472006-07-03 17:27:12 -0700103bail:
104 mutex_unlock(&inode->i_mutex);
105
Mark Fasheha81cb882008-10-07 14:25:16 -0700106 brelse(bh);
Herbert Poetzlca4d1472006-07-03 17:27:12 -0700107
108 mlog_exit(status);
109 return status;
110}
111
Andi Kleenc9ec1482008-01-27 03:17:17 +0100112long ocfs2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
Herbert Poetzlca4d1472006-07-03 17:27:12 -0700113{
Andi Kleenc9ec1482008-01-27 03:17:17 +0100114 struct inode *inode = filp->f_path.dentry->d_inode;
Herbert Poetzlca4d1472006-07-03 17:27:12 -0700115 unsigned int flags;
Tao Mad6590722007-12-18 15:47:03 +0800116 int new_clusters;
Herbert Poetzlca4d1472006-07-03 17:27:12 -0700117 int status;
Mark Fashehb2580102007-03-09 16:53:21 -0800118 struct ocfs2_space_resv sr;
Tao Ma7909f2b2007-12-18 15:47:25 +0800119 struct ocfs2_new_group_input input;
Tao Mabd508732009-09-21 11:25:14 +0800120 struct reflink_arguments args;
121 const char *old_path, *new_path;
122 bool preserve;
Herbert Poetzlca4d1472006-07-03 17:27:12 -0700123
124 switch (cmd) {
125 case OCFS2_IOC_GETFLAGS:
126 status = ocfs2_get_inode_attr(inode, &flags);
127 if (status < 0)
128 return status;
129
130 flags &= OCFS2_FL_VISIBLE;
131 return put_user(flags, (int __user *) arg);
132 case OCFS2_IOC_SETFLAGS:
133 if (get_user(flags, (int __user *) arg))
134 return -EFAULT;
135
Dave Hansen42a74f22008-02-15 14:37:46 -0800136 status = mnt_want_write(filp->f_path.mnt);
137 if (status)
138 return status;
139 status = ocfs2_set_inode_attr(inode, flags,
Herbert Poetzlca4d1472006-07-03 17:27:12 -0700140 OCFS2_FL_MODIFIABLE);
Dave Hansen42a74f22008-02-15 14:37:46 -0800141 mnt_drop_write(filp->f_path.mnt);
142 return status;
Mark Fashehb2580102007-03-09 16:53:21 -0800143 case OCFS2_IOC_RESVSP:
144 case OCFS2_IOC_RESVSP64:
145 case OCFS2_IOC_UNRESVSP:
146 case OCFS2_IOC_UNRESVSP64:
147 if (copy_from_user(&sr, (int __user *) arg, sizeof(sr)))
148 return -EFAULT;
149
150 return ocfs2_change_file_space(filp, cmd, &sr);
Tao Mad6590722007-12-18 15:47:03 +0800151 case OCFS2_IOC_GROUP_EXTEND:
Mark Fasheh0957f002007-12-18 18:58:18 -0800152 if (!capable(CAP_SYS_RESOURCE))
153 return -EPERM;
154
Tao Mad6590722007-12-18 15:47:03 +0800155 if (get_user(new_clusters, (int __user *)arg))
156 return -EFAULT;
157
158 return ocfs2_group_extend(inode, new_clusters);
Tao Ma7909f2b2007-12-18 15:47:25 +0800159 case OCFS2_IOC_GROUP_ADD:
160 case OCFS2_IOC_GROUP_ADD64:
Mark Fasheh0957f002007-12-18 18:58:18 -0800161 if (!capable(CAP_SYS_RESOURCE))
162 return -EPERM;
163
Tao Ma7909f2b2007-12-18 15:47:25 +0800164 if (copy_from_user(&input, (int __user *) arg, sizeof(input)))
165 return -EFAULT;
166
167 return ocfs2_group_add(inode, &input);
Tao Mabd508732009-09-21 11:25:14 +0800168 case OCFS2_IOC_REFLINK:
169 if (copy_from_user(&args, (struct reflink_arguments *)arg,
170 sizeof(args)))
171 return -EFAULT;
172 old_path = (const char *)(unsigned long)args.old_path;
173 new_path = (const char *)(unsigned long)args.new_path;
174 preserve = (args.preserve != 0);
175
176 return ocfs2_reflink_ioctl(inode, old_path, new_path, preserve);
Herbert Poetzlca4d1472006-07-03 17:27:12 -0700177 default:
178 return -ENOTTY;
179 }
180}
181
Mark Fasheh586d2322007-03-09 15:56:28 -0800182#ifdef CONFIG_COMPAT
183long ocfs2_compat_ioctl(struct file *file, unsigned cmd, unsigned long arg)
184{
Tao Ma34e6c592010-01-27 10:21:52 +0800185 bool preserve;
186 struct reflink_arguments args;
187 struct inode *inode = file->f_path.dentry->d_inode;
188
Mark Fasheh586d2322007-03-09 15:56:28 -0800189 switch (cmd) {
190 case OCFS2_IOC32_GETFLAGS:
191 cmd = OCFS2_IOC_GETFLAGS;
192 break;
193 case OCFS2_IOC32_SETFLAGS:
194 cmd = OCFS2_IOC_SETFLAGS;
195 break;
Mark Fashehb2580102007-03-09 16:53:21 -0800196 case OCFS2_IOC_RESVSP:
197 case OCFS2_IOC_RESVSP64:
198 case OCFS2_IOC_UNRESVSP:
199 case OCFS2_IOC_UNRESVSP64:
Tao Mad6590722007-12-18 15:47:03 +0800200 case OCFS2_IOC_GROUP_EXTEND:
Tao Ma7909f2b2007-12-18 15:47:25 +0800201 case OCFS2_IOC_GROUP_ADD:
202 case OCFS2_IOC_GROUP_ADD64:
Mark Fashehb2580102007-03-09 16:53:21 -0800203 break;
Tao Ma34e6c592010-01-27 10:21:52 +0800204 case OCFS2_IOC_REFLINK:
205 if (copy_from_user(&args, (struct reflink_arguments *)arg,
206 sizeof(args)))
207 return -EFAULT;
208 preserve = (args.preserve != 0);
209
210 return ocfs2_reflink_ioctl(inode, compat_ptr(args.old_path),
211 compat_ptr(args.new_path), preserve);
Mark Fasheh586d2322007-03-09 15:56:28 -0800212 default:
213 return -ENOIOCTLCMD;
214 }
215
Andi Kleenc9ec1482008-01-27 03:17:17 +0100216 return ocfs2_ioctl(file, cmd, arg);
Mark Fasheh586d2322007-03-09 15:56:28 -0800217}
218#endif