blob: 7b142f0ce995bd07a5116567a0b2b56e65b8dd1b [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>
Joel Becker6953b4c2008-01-29 16:59:56 -080010#include <linux/smp_lock.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"
Adrian Bunk2d562512006-07-10 01:32:51 +020025
Herbert Poetzlca4d1472006-07-03 17:27:12 -070026#include <linux/ext2_fs.h>
27
28static int ocfs2_get_inode_attr(struct inode *inode, unsigned *flags)
29{
30 int status;
31
Mark Fashehe63aecb62007-10-18 15:30:42 -070032 status = ocfs2_inode_lock(inode, NULL, 0);
Herbert Poetzlca4d1472006-07-03 17:27:12 -070033 if (status < 0) {
34 mlog_errno(status);
35 return status;
36 }
Jan Kara6e4b0d52007-04-27 11:08:01 -070037 ocfs2_get_inode_flags(OCFS2_I(inode));
Herbert Poetzlca4d1472006-07-03 17:27:12 -070038 *flags = OCFS2_I(inode)->ip_attr;
Mark Fashehe63aecb62007-10-18 15:30:42 -070039 ocfs2_inode_unlock(inode, 0);
Herbert Poetzlca4d1472006-07-03 17:27:12 -070040
41 mlog_exit(status);
42 return status;
43}
44
45static int ocfs2_set_inode_attr(struct inode *inode, unsigned flags,
46 unsigned mask)
47{
48 struct ocfs2_inode_info *ocfs2_inode = OCFS2_I(inode);
49 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fasheh1fabe142006-10-09 18:11:45 -070050 handle_t *handle = NULL;
Herbert Poetzlca4d1472006-07-03 17:27:12 -070051 struct buffer_head *bh = NULL;
52 unsigned oldflags;
53 int status;
54
55 mutex_lock(&inode->i_mutex);
56
Mark Fashehe63aecb62007-10-18 15:30:42 -070057 status = ocfs2_inode_lock(inode, &bh, 1);
Herbert Poetzlca4d1472006-07-03 17:27:12 -070058 if (status < 0) {
59 mlog_errno(status);
60 goto bail;
61 }
62
Herbert Poetzlca4d1472006-07-03 17:27:12 -070063 status = -EACCES;
Satyam Sharma3bd858a2007-07-17 15:00:08 +053064 if (!is_owner_or_cap(inode))
Herbert Poetzlca4d1472006-07-03 17:27:12 -070065 goto bail_unlock;
66
67 if (!S_ISDIR(inode->i_mode))
68 flags &= ~OCFS2_DIRSYNC_FL;
69
Mark Fasheh65eff9c2006-10-09 17:26:22 -070070 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
Herbert Poetzlca4d1472006-07-03 17:27:12 -070071 if (IS_ERR(handle)) {
72 status = PTR_ERR(handle);
73 mlog_errno(status);
74 goto bail_unlock;
75 }
76
77 oldflags = ocfs2_inode->ip_attr;
78 flags = flags & mask;
79 flags |= oldflags & ~mask;
80
81 /*
82 * The IMMUTABLE and APPEND_ONLY flags can only be changed by
83 * the relevant capability.
84 */
85 status = -EPERM;
86 if ((oldflags & OCFS2_IMMUTABLE_FL) || ((flags ^ oldflags) &
87 (OCFS2_APPEND_FL | OCFS2_IMMUTABLE_FL))) {
88 if (!capable(CAP_LINUX_IMMUTABLE))
89 goto bail_unlock;
90 }
91
92 ocfs2_inode->ip_attr = flags;
93 ocfs2_set_inode_flags(inode);
94
95 status = ocfs2_mark_inode_dirty(handle, inode, bh);
96 if (status < 0)
97 mlog_errno(status);
98
Mark Fasheh02dc1af2006-10-09 16:48:10 -070099 ocfs2_commit_trans(osb, handle);
Herbert Poetzlca4d1472006-07-03 17:27:12 -0700100bail_unlock:
Mark Fashehe63aecb62007-10-18 15:30:42 -0700101 ocfs2_inode_unlock(inode, 1);
Herbert Poetzlca4d1472006-07-03 17:27:12 -0700102bail:
103 mutex_unlock(&inode->i_mutex);
104
105 if (bh)
106 brelse(bh);
107
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;
Herbert Poetzlca4d1472006-07-03 17:27:12 -0700120
121 switch (cmd) {
122 case OCFS2_IOC_GETFLAGS:
123 status = ocfs2_get_inode_attr(inode, &flags);
124 if (status < 0)
125 return status;
126
127 flags &= OCFS2_FL_VISIBLE;
128 return put_user(flags, (int __user *) arg);
129 case OCFS2_IOC_SETFLAGS:
130 if (get_user(flags, (int __user *) arg))
131 return -EFAULT;
132
Dave Hansen42a74f22008-02-15 14:37:46 -0800133 status = mnt_want_write(filp->f_path.mnt);
134 if (status)
135 return status;
136 status = ocfs2_set_inode_attr(inode, flags,
Herbert Poetzlca4d1472006-07-03 17:27:12 -0700137 OCFS2_FL_MODIFIABLE);
Dave Hansen42a74f22008-02-15 14:37:46 -0800138 mnt_drop_write(filp->f_path.mnt);
139 return status;
Mark Fashehb2580102007-03-09 16:53:21 -0800140 case OCFS2_IOC_RESVSP:
141 case OCFS2_IOC_RESVSP64:
142 case OCFS2_IOC_UNRESVSP:
143 case OCFS2_IOC_UNRESVSP64:
144 if (copy_from_user(&sr, (int __user *) arg, sizeof(sr)))
145 return -EFAULT;
146
147 return ocfs2_change_file_space(filp, cmd, &sr);
Tao Mad6590722007-12-18 15:47:03 +0800148 case OCFS2_IOC_GROUP_EXTEND:
Mark Fasheh0957f002007-12-18 18:58:18 -0800149 if (!capable(CAP_SYS_RESOURCE))
150 return -EPERM;
151
Tao Mad6590722007-12-18 15:47:03 +0800152 if (get_user(new_clusters, (int __user *)arg))
153 return -EFAULT;
154
155 return ocfs2_group_extend(inode, new_clusters);
Tao Ma7909f2b2007-12-18 15:47:25 +0800156 case OCFS2_IOC_GROUP_ADD:
157 case OCFS2_IOC_GROUP_ADD64:
Mark Fasheh0957f002007-12-18 18:58:18 -0800158 if (!capable(CAP_SYS_RESOURCE))
159 return -EPERM;
160
Tao Ma7909f2b2007-12-18 15:47:25 +0800161 if (copy_from_user(&input, (int __user *) arg, sizeof(input)))
162 return -EFAULT;
163
164 return ocfs2_group_add(inode, &input);
Herbert Poetzlca4d1472006-07-03 17:27:12 -0700165 default:
166 return -ENOTTY;
167 }
168}
169
Mark Fasheh586d2322007-03-09 15:56:28 -0800170#ifdef CONFIG_COMPAT
171long ocfs2_compat_ioctl(struct file *file, unsigned cmd, unsigned long arg)
172{
Mark Fasheh586d2322007-03-09 15:56:28 -0800173 switch (cmd) {
174 case OCFS2_IOC32_GETFLAGS:
175 cmd = OCFS2_IOC_GETFLAGS;
176 break;
177 case OCFS2_IOC32_SETFLAGS:
178 cmd = OCFS2_IOC_SETFLAGS;
179 break;
Mark Fashehb2580102007-03-09 16:53:21 -0800180 case OCFS2_IOC_RESVSP:
181 case OCFS2_IOC_RESVSP64:
182 case OCFS2_IOC_UNRESVSP:
183 case OCFS2_IOC_UNRESVSP64:
Tao Mad6590722007-12-18 15:47:03 +0800184 case OCFS2_IOC_GROUP_EXTEND:
Tao Ma7909f2b2007-12-18 15:47:25 +0800185 case OCFS2_IOC_GROUP_ADD:
186 case OCFS2_IOC_GROUP_ADD64:
Mark Fashehb2580102007-03-09 16:53:21 -0800187 break;
Mark Fasheh586d2322007-03-09 15:56:28 -0800188 default:
189 return -ENOIOCTLCMD;
190 }
191
Andi Kleenc9ec1482008-01-27 03:17:17 +0100192 return ocfs2_ioctl(file, cmd, arg);
Mark Fasheh586d2322007-03-09 15:56:28 -0800193}
194#endif