Herbert Poetzl | ca4d147 | 2006-07-03 17:27:12 -0700 | [diff] [blame] | 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" |
Mark Fasheh | b258010 | 2007-03-09 16:53:21 -0800 | [diff] [blame] | 17 | #include "file.h" |
Herbert Poetzl | ca4d147 | 2006-07-03 17:27:12 -0700 | [diff] [blame] | 18 | #include "inode.h" |
| 19 | #include "journal.h" |
| 20 | |
| 21 | #include "ocfs2_fs.h" |
Adrian Bunk | 2d56251 | 2006-07-10 01:32:51 +0200 | [diff] [blame] | 22 | #include "ioctl.h" |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 23 | #include "resize.h" |
Tao Ma | bd50873 | 2009-09-21 11:25:14 +0800 | [diff] [blame] | 24 | #include "refcounttree.h" |
Adrian Bunk | 2d56251 | 2006-07-10 01:32:51 +0200 | [diff] [blame] | 25 | |
Herbert Poetzl | ca4d147 | 2006-07-03 17:27:12 -0700 | [diff] [blame] | 26 | #include <linux/ext2_fs.h> |
| 27 | |
| 28 | static int ocfs2_get_inode_attr(struct inode *inode, unsigned *flags) |
| 29 | { |
| 30 | int status; |
| 31 | |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 32 | status = ocfs2_inode_lock(inode, NULL, 0); |
Herbert Poetzl | ca4d147 | 2006-07-03 17:27:12 -0700 | [diff] [blame] | 33 | if (status < 0) { |
| 34 | mlog_errno(status); |
| 35 | return status; |
| 36 | } |
Jan Kara | 6e4b0d5 | 2007-04-27 11:08:01 -0700 | [diff] [blame] | 37 | ocfs2_get_inode_flags(OCFS2_I(inode)); |
Herbert Poetzl | ca4d147 | 2006-07-03 17:27:12 -0700 | [diff] [blame] | 38 | *flags = OCFS2_I(inode)->ip_attr; |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 39 | ocfs2_inode_unlock(inode, 0); |
Herbert Poetzl | ca4d147 | 2006-07-03 17:27:12 -0700 | [diff] [blame] | 40 | |
| 41 | mlog_exit(status); |
| 42 | return status; |
| 43 | } |
| 44 | |
| 45 | static 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 Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 50 | handle_t *handle = NULL; |
Herbert Poetzl | ca4d147 | 2006-07-03 17:27:12 -0700 | [diff] [blame] | 51 | struct buffer_head *bh = NULL; |
| 52 | unsigned oldflags; |
| 53 | int status; |
| 54 | |
| 55 | mutex_lock(&inode->i_mutex); |
| 56 | |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 57 | status = ocfs2_inode_lock(inode, &bh, 1); |
Herbert Poetzl | ca4d147 | 2006-07-03 17:27:12 -0700 | [diff] [blame] | 58 | if (status < 0) { |
| 59 | mlog_errno(status); |
| 60 | goto bail; |
| 61 | } |
| 62 | |
Herbert Poetzl | ca4d147 | 2006-07-03 17:27:12 -0700 | [diff] [blame] | 63 | status = -EACCES; |
Satyam Sharma | 3bd858a | 2007-07-17 15:00:08 +0530 | [diff] [blame] | 64 | if (!is_owner_or_cap(inode)) |
Herbert Poetzl | ca4d147 | 2006-07-03 17:27:12 -0700 | [diff] [blame] | 65 | goto bail_unlock; |
| 66 | |
| 67 | if (!S_ISDIR(inode->i_mode)) |
| 68 | flags &= ~OCFS2_DIRSYNC_FL; |
| 69 | |
Mark Fasheh | 65eff9c | 2006-10-09 17:26:22 -0700 | [diff] [blame] | 70 | handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS); |
Herbert Poetzl | ca4d147 | 2006-07-03 17:27:12 -0700 | [diff] [blame] | 71 | 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 Fasheh | 02dc1af | 2006-10-09 16:48:10 -0700 | [diff] [blame] | 99 | ocfs2_commit_trans(osb, handle); |
Herbert Poetzl | ca4d147 | 2006-07-03 17:27:12 -0700 | [diff] [blame] | 100 | bail_unlock: |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 101 | ocfs2_inode_unlock(inode, 1); |
Herbert Poetzl | ca4d147 | 2006-07-03 17:27:12 -0700 | [diff] [blame] | 102 | bail: |
| 103 | mutex_unlock(&inode->i_mutex); |
| 104 | |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 105 | brelse(bh); |
Herbert Poetzl | ca4d147 | 2006-07-03 17:27:12 -0700 | [diff] [blame] | 106 | |
| 107 | mlog_exit(status); |
| 108 | return status; |
| 109 | } |
| 110 | |
Andi Kleen | c9ec148 | 2008-01-27 03:17:17 +0100 | [diff] [blame] | 111 | long ocfs2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) |
Herbert Poetzl | ca4d147 | 2006-07-03 17:27:12 -0700 | [diff] [blame] | 112 | { |
Andi Kleen | c9ec148 | 2008-01-27 03:17:17 +0100 | [diff] [blame] | 113 | struct inode *inode = filp->f_path.dentry->d_inode; |
Herbert Poetzl | ca4d147 | 2006-07-03 17:27:12 -0700 | [diff] [blame] | 114 | unsigned int flags; |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 115 | int new_clusters; |
Herbert Poetzl | ca4d147 | 2006-07-03 17:27:12 -0700 | [diff] [blame] | 116 | int status; |
Mark Fasheh | b258010 | 2007-03-09 16:53:21 -0800 | [diff] [blame] | 117 | struct ocfs2_space_resv sr; |
Tao Ma | 7909f2b | 2007-12-18 15:47:25 +0800 | [diff] [blame] | 118 | struct ocfs2_new_group_input input; |
Tao Ma | bd50873 | 2009-09-21 11:25:14 +0800 | [diff] [blame] | 119 | struct reflink_arguments args; |
| 120 | const char *old_path, *new_path; |
| 121 | bool preserve; |
Herbert Poetzl | ca4d147 | 2006-07-03 17:27:12 -0700 | [diff] [blame] | 122 | |
| 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 | |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 135 | status = mnt_want_write(filp->f_path.mnt); |
| 136 | if (status) |
| 137 | return status; |
| 138 | status = ocfs2_set_inode_attr(inode, flags, |
Herbert Poetzl | ca4d147 | 2006-07-03 17:27:12 -0700 | [diff] [blame] | 139 | OCFS2_FL_MODIFIABLE); |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 140 | mnt_drop_write(filp->f_path.mnt); |
| 141 | return status; |
Mark Fasheh | b258010 | 2007-03-09 16:53:21 -0800 | [diff] [blame] | 142 | case OCFS2_IOC_RESVSP: |
| 143 | case OCFS2_IOC_RESVSP64: |
| 144 | case OCFS2_IOC_UNRESVSP: |
| 145 | case OCFS2_IOC_UNRESVSP64: |
| 146 | if (copy_from_user(&sr, (int __user *) arg, sizeof(sr))) |
| 147 | return -EFAULT; |
| 148 | |
| 149 | return ocfs2_change_file_space(filp, cmd, &sr); |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 150 | case OCFS2_IOC_GROUP_EXTEND: |
Mark Fasheh | 0957f00 | 2007-12-18 18:58:18 -0800 | [diff] [blame] | 151 | if (!capable(CAP_SYS_RESOURCE)) |
| 152 | return -EPERM; |
| 153 | |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 154 | if (get_user(new_clusters, (int __user *)arg)) |
| 155 | return -EFAULT; |
| 156 | |
| 157 | return ocfs2_group_extend(inode, new_clusters); |
Tao Ma | 7909f2b | 2007-12-18 15:47:25 +0800 | [diff] [blame] | 158 | case OCFS2_IOC_GROUP_ADD: |
| 159 | case OCFS2_IOC_GROUP_ADD64: |
Mark Fasheh | 0957f00 | 2007-12-18 18:58:18 -0800 | [diff] [blame] | 160 | if (!capable(CAP_SYS_RESOURCE)) |
| 161 | return -EPERM; |
| 162 | |
Tao Ma | 7909f2b | 2007-12-18 15:47:25 +0800 | [diff] [blame] | 163 | if (copy_from_user(&input, (int __user *) arg, sizeof(input))) |
| 164 | return -EFAULT; |
| 165 | |
| 166 | return ocfs2_group_add(inode, &input); |
Tao Ma | bd50873 | 2009-09-21 11:25:14 +0800 | [diff] [blame] | 167 | case OCFS2_IOC_REFLINK: |
| 168 | if (copy_from_user(&args, (struct reflink_arguments *)arg, |
| 169 | sizeof(args))) |
| 170 | return -EFAULT; |
| 171 | old_path = (const char *)(unsigned long)args.old_path; |
| 172 | new_path = (const char *)(unsigned long)args.new_path; |
| 173 | preserve = (args.preserve != 0); |
| 174 | |
| 175 | return ocfs2_reflink_ioctl(inode, old_path, new_path, preserve); |
Herbert Poetzl | ca4d147 | 2006-07-03 17:27:12 -0700 | [diff] [blame] | 176 | default: |
| 177 | return -ENOTTY; |
| 178 | } |
| 179 | } |
| 180 | |
Mark Fasheh | 586d232 | 2007-03-09 15:56:28 -0800 | [diff] [blame] | 181 | #ifdef CONFIG_COMPAT |
| 182 | long ocfs2_compat_ioctl(struct file *file, unsigned cmd, unsigned long arg) |
| 183 | { |
Mark Fasheh | 586d232 | 2007-03-09 15:56:28 -0800 | [diff] [blame] | 184 | switch (cmd) { |
| 185 | case OCFS2_IOC32_GETFLAGS: |
| 186 | cmd = OCFS2_IOC_GETFLAGS; |
| 187 | break; |
| 188 | case OCFS2_IOC32_SETFLAGS: |
| 189 | cmd = OCFS2_IOC_SETFLAGS; |
| 190 | break; |
Mark Fasheh | b258010 | 2007-03-09 16:53:21 -0800 | [diff] [blame] | 191 | case OCFS2_IOC_RESVSP: |
| 192 | case OCFS2_IOC_RESVSP64: |
| 193 | case OCFS2_IOC_UNRESVSP: |
| 194 | case OCFS2_IOC_UNRESVSP64: |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 195 | case OCFS2_IOC_GROUP_EXTEND: |
Tao Ma | 7909f2b | 2007-12-18 15:47:25 +0800 | [diff] [blame] | 196 | case OCFS2_IOC_GROUP_ADD: |
| 197 | case OCFS2_IOC_GROUP_ADD64: |
Tao Ma | bd50873 | 2009-09-21 11:25:14 +0800 | [diff] [blame] | 198 | case OCFS2_IOC_REFLINK: |
Mark Fasheh | b258010 | 2007-03-09 16:53:21 -0800 | [diff] [blame] | 199 | break; |
Mark Fasheh | 586d232 | 2007-03-09 15:56:28 -0800 | [diff] [blame] | 200 | default: |
| 201 | return -ENOIOCTLCMD; |
| 202 | } |
| 203 | |
Andi Kleen | c9ec148 | 2008-01-27 03:17:17 +0100 | [diff] [blame] | 204 | return ocfs2_ioctl(file, cmd, arg); |
Mark Fasheh | 586d232 | 2007-03-09 15:56:28 -0800 | [diff] [blame] | 205 | } |
| 206 | #endif |