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> |
Tao Ma | 34e6c59 | 2010-01-27 10:21:52 +0800 | [diff] [blame] | 10 | #include <linux/compat.h> |
Herbert Poetzl | ca4d147 | 2006-07-03 17:27:12 -0700 | [diff] [blame] | 11 | |
Herbert Poetzl | ca4d147 | 2006-07-03 17:27:12 -0700 | [diff] [blame] | 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" |
Tristan Ye | 3e5db17 | 2011-05-24 15:25:54 +0800 | [diff] [blame^] | 25 | #include "sysfile.h" |
| 26 | #include "dir.h" |
| 27 | #include "buffer_head_io.h" |
Adrian Bunk | 2d56251 | 2006-07-10 01:32:51 +0200 | [diff] [blame] | 28 | |
Herbert Poetzl | ca4d147 | 2006-07-03 17:27:12 -0700 | [diff] [blame] | 29 | #include <linux/ext2_fs.h> |
| 30 | |
Tristan Ye | ddee5cd | 2010-05-22 16:26:33 +0800 | [diff] [blame] | 31 | #define o2info_from_user(a, b) \ |
| 32 | copy_from_user(&(a), (b), sizeof(a)) |
| 33 | #define o2info_to_user(a, b) \ |
| 34 | copy_to_user((typeof(a) __user *)b, &(a), sizeof(a)) |
| 35 | |
| 36 | /* |
| 37 | * This call is void because we are already reporting an error that may |
| 38 | * be -EFAULT. The error will be returned from the ioctl(2) call. It's |
| 39 | * just a best-effort to tell userspace that this request caused the error. |
| 40 | */ |
Tristan Ye | 8aa1fa3 | 2011-05-24 15:22:59 +0800 | [diff] [blame] | 41 | static inline void o2info_set_request_error(struct ocfs2_info_request *kreq, |
Tristan Ye | ddee5cd | 2010-05-22 16:26:33 +0800 | [diff] [blame] | 42 | struct ocfs2_info_request __user *req) |
| 43 | { |
| 44 | kreq->ir_flags |= OCFS2_INFO_FL_ERROR; |
| 45 | (void)put_user(kreq->ir_flags, (__u32 __user *)&(req->ir_flags)); |
| 46 | } |
| 47 | |
Tristan Ye | 8aa1fa3 | 2011-05-24 15:22:59 +0800 | [diff] [blame] | 48 | static inline void o2info_set_request_filled(struct ocfs2_info_request *req) |
Tristan Ye | 1936a26 | 2011-01-30 14:25:59 +0800 | [diff] [blame] | 49 | { |
| 50 | req->ir_flags |= OCFS2_INFO_FL_FILLED; |
| 51 | } |
| 52 | |
Tristan Ye | 8aa1fa3 | 2011-05-24 15:22:59 +0800 | [diff] [blame] | 53 | static inline void o2info_clear_request_filled(struct ocfs2_info_request *req) |
Tristan Ye | 1936a26 | 2011-01-30 14:25:59 +0800 | [diff] [blame] | 54 | { |
| 55 | req->ir_flags &= ~OCFS2_INFO_FL_FILLED; |
| 56 | } |
| 57 | |
Tristan Ye | 3e5db17 | 2011-05-24 15:25:54 +0800 | [diff] [blame^] | 58 | static inline int o2info_coherent(struct ocfs2_info_request *req) |
| 59 | { |
| 60 | return (!(req->ir_flags & OCFS2_INFO_FL_NON_COHERENT)); |
| 61 | } |
| 62 | |
Herbert Poetzl | ca4d147 | 2006-07-03 17:27:12 -0700 | [diff] [blame] | 63 | static int ocfs2_get_inode_attr(struct inode *inode, unsigned *flags) |
| 64 | { |
| 65 | int status; |
| 66 | |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 67 | status = ocfs2_inode_lock(inode, NULL, 0); |
Herbert Poetzl | ca4d147 | 2006-07-03 17:27:12 -0700 | [diff] [blame] | 68 | if (status < 0) { |
| 69 | mlog_errno(status); |
| 70 | return status; |
| 71 | } |
Jan Kara | 6e4b0d5 | 2007-04-27 11:08:01 -0700 | [diff] [blame] | 72 | ocfs2_get_inode_flags(OCFS2_I(inode)); |
Herbert Poetzl | ca4d147 | 2006-07-03 17:27:12 -0700 | [diff] [blame] | 73 | *flags = OCFS2_I(inode)->ip_attr; |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 74 | ocfs2_inode_unlock(inode, 0); |
Herbert Poetzl | ca4d147 | 2006-07-03 17:27:12 -0700 | [diff] [blame] | 75 | |
Herbert Poetzl | ca4d147 | 2006-07-03 17:27:12 -0700 | [diff] [blame] | 76 | return status; |
| 77 | } |
| 78 | |
| 79 | static int ocfs2_set_inode_attr(struct inode *inode, unsigned flags, |
| 80 | unsigned mask) |
| 81 | { |
| 82 | struct ocfs2_inode_info *ocfs2_inode = OCFS2_I(inode); |
| 83 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 84 | handle_t *handle = NULL; |
Herbert Poetzl | ca4d147 | 2006-07-03 17:27:12 -0700 | [diff] [blame] | 85 | struct buffer_head *bh = NULL; |
| 86 | unsigned oldflags; |
| 87 | int status; |
| 88 | |
| 89 | mutex_lock(&inode->i_mutex); |
| 90 | |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 91 | status = ocfs2_inode_lock(inode, &bh, 1); |
Herbert Poetzl | ca4d147 | 2006-07-03 17:27:12 -0700 | [diff] [blame] | 92 | if (status < 0) { |
| 93 | mlog_errno(status); |
| 94 | goto bail; |
| 95 | } |
| 96 | |
Herbert Poetzl | ca4d147 | 2006-07-03 17:27:12 -0700 | [diff] [blame] | 97 | status = -EACCES; |
Serge E. Hallyn | 2e14967 | 2011-03-23 16:43:26 -0700 | [diff] [blame] | 98 | if (!inode_owner_or_capable(inode)) |
Herbert Poetzl | ca4d147 | 2006-07-03 17:27:12 -0700 | [diff] [blame] | 99 | goto bail_unlock; |
| 100 | |
| 101 | if (!S_ISDIR(inode->i_mode)) |
| 102 | flags &= ~OCFS2_DIRSYNC_FL; |
| 103 | |
Mark Fasheh | 65eff9c | 2006-10-09 17:26:22 -0700 | [diff] [blame] | 104 | handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS); |
Herbert Poetzl | ca4d147 | 2006-07-03 17:27:12 -0700 | [diff] [blame] | 105 | if (IS_ERR(handle)) { |
| 106 | status = PTR_ERR(handle); |
| 107 | mlog_errno(status); |
| 108 | goto bail_unlock; |
| 109 | } |
| 110 | |
| 111 | oldflags = ocfs2_inode->ip_attr; |
| 112 | flags = flags & mask; |
| 113 | flags |= oldflags & ~mask; |
| 114 | |
| 115 | /* |
| 116 | * The IMMUTABLE and APPEND_ONLY flags can only be changed by |
| 117 | * the relevant capability. |
| 118 | */ |
| 119 | status = -EPERM; |
| 120 | if ((oldflags & OCFS2_IMMUTABLE_FL) || ((flags ^ oldflags) & |
| 121 | (OCFS2_APPEND_FL | OCFS2_IMMUTABLE_FL))) { |
| 122 | if (!capable(CAP_LINUX_IMMUTABLE)) |
| 123 | goto bail_unlock; |
| 124 | } |
| 125 | |
| 126 | ocfs2_inode->ip_attr = flags; |
| 127 | ocfs2_set_inode_flags(inode); |
| 128 | |
| 129 | status = ocfs2_mark_inode_dirty(handle, inode, bh); |
| 130 | if (status < 0) |
| 131 | mlog_errno(status); |
| 132 | |
Mark Fasheh | 02dc1af | 2006-10-09 16:48:10 -0700 | [diff] [blame] | 133 | ocfs2_commit_trans(osb, handle); |
Herbert Poetzl | ca4d147 | 2006-07-03 17:27:12 -0700 | [diff] [blame] | 134 | bail_unlock: |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 135 | ocfs2_inode_unlock(inode, 1); |
Herbert Poetzl | ca4d147 | 2006-07-03 17:27:12 -0700 | [diff] [blame] | 136 | bail: |
| 137 | mutex_unlock(&inode->i_mutex); |
| 138 | |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 139 | brelse(bh); |
Herbert Poetzl | ca4d147 | 2006-07-03 17:27:12 -0700 | [diff] [blame] | 140 | |
Herbert Poetzl | ca4d147 | 2006-07-03 17:27:12 -0700 | [diff] [blame] | 141 | return status; |
| 142 | } |
| 143 | |
Tristan Ye | ddee5cd | 2010-05-22 16:26:33 +0800 | [diff] [blame] | 144 | int ocfs2_info_handle_blocksize(struct inode *inode, |
| 145 | struct ocfs2_info_request __user *req) |
| 146 | { |
| 147 | int status = -EFAULT; |
| 148 | struct ocfs2_info_blocksize oib; |
| 149 | |
| 150 | if (o2info_from_user(oib, req)) |
| 151 | goto bail; |
| 152 | |
| 153 | oib.ib_blocksize = inode->i_sb->s_blocksize; |
Tristan Ye | 1936a26 | 2011-01-30 14:25:59 +0800 | [diff] [blame] | 154 | |
Tristan Ye | 8aa1fa3 | 2011-05-24 15:22:59 +0800 | [diff] [blame] | 155 | o2info_set_request_filled(&oib.ib_req); |
Tristan Ye | ddee5cd | 2010-05-22 16:26:33 +0800 | [diff] [blame] | 156 | |
| 157 | if (o2info_to_user(oib, req)) |
| 158 | goto bail; |
| 159 | |
| 160 | status = 0; |
| 161 | bail: |
| 162 | if (status) |
Tristan Ye | 8aa1fa3 | 2011-05-24 15:22:59 +0800 | [diff] [blame] | 163 | o2info_set_request_error(&oib.ib_req, req); |
Tristan Ye | ddee5cd | 2010-05-22 16:26:33 +0800 | [diff] [blame] | 164 | |
| 165 | return status; |
| 166 | } |
| 167 | |
| 168 | int ocfs2_info_handle_clustersize(struct inode *inode, |
| 169 | struct ocfs2_info_request __user *req) |
| 170 | { |
| 171 | int status = -EFAULT; |
| 172 | struct ocfs2_info_clustersize oic; |
| 173 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 174 | |
| 175 | if (o2info_from_user(oic, req)) |
| 176 | goto bail; |
| 177 | |
| 178 | oic.ic_clustersize = osb->s_clustersize; |
Tristan Ye | 1936a26 | 2011-01-30 14:25:59 +0800 | [diff] [blame] | 179 | |
Tristan Ye | 8aa1fa3 | 2011-05-24 15:22:59 +0800 | [diff] [blame] | 180 | o2info_set_request_filled(&oic.ic_req); |
Tristan Ye | ddee5cd | 2010-05-22 16:26:33 +0800 | [diff] [blame] | 181 | |
| 182 | if (o2info_to_user(oic, req)) |
| 183 | goto bail; |
| 184 | |
| 185 | status = 0; |
| 186 | bail: |
| 187 | if (status) |
Tristan Ye | 8aa1fa3 | 2011-05-24 15:22:59 +0800 | [diff] [blame] | 188 | o2info_set_request_error(&oic.ic_req, req); |
Tristan Ye | ddee5cd | 2010-05-22 16:26:33 +0800 | [diff] [blame] | 189 | |
| 190 | return status; |
| 191 | } |
| 192 | |
| 193 | int ocfs2_info_handle_maxslots(struct inode *inode, |
| 194 | struct ocfs2_info_request __user *req) |
| 195 | { |
| 196 | int status = -EFAULT; |
| 197 | struct ocfs2_info_maxslots oim; |
| 198 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 199 | |
| 200 | if (o2info_from_user(oim, req)) |
| 201 | goto bail; |
| 202 | |
| 203 | oim.im_max_slots = osb->max_slots; |
Tristan Ye | 1936a26 | 2011-01-30 14:25:59 +0800 | [diff] [blame] | 204 | |
Tristan Ye | 8aa1fa3 | 2011-05-24 15:22:59 +0800 | [diff] [blame] | 205 | o2info_set_request_filled(&oim.im_req); |
Tristan Ye | ddee5cd | 2010-05-22 16:26:33 +0800 | [diff] [blame] | 206 | |
| 207 | if (o2info_to_user(oim, req)) |
| 208 | goto bail; |
| 209 | |
| 210 | status = 0; |
| 211 | bail: |
| 212 | if (status) |
Tristan Ye | 8aa1fa3 | 2011-05-24 15:22:59 +0800 | [diff] [blame] | 213 | o2info_set_request_error(&oim.im_req, req); |
Tristan Ye | ddee5cd | 2010-05-22 16:26:33 +0800 | [diff] [blame] | 214 | |
| 215 | return status; |
| 216 | } |
| 217 | |
| 218 | int ocfs2_info_handle_label(struct inode *inode, |
| 219 | struct ocfs2_info_request __user *req) |
| 220 | { |
| 221 | int status = -EFAULT; |
| 222 | struct ocfs2_info_label oil; |
| 223 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 224 | |
| 225 | if (o2info_from_user(oil, req)) |
| 226 | goto bail; |
| 227 | |
| 228 | memcpy(oil.il_label, osb->vol_label, OCFS2_MAX_VOL_LABEL_LEN); |
Tristan Ye | 1936a26 | 2011-01-30 14:25:59 +0800 | [diff] [blame] | 229 | |
Tristan Ye | 8aa1fa3 | 2011-05-24 15:22:59 +0800 | [diff] [blame] | 230 | o2info_set_request_filled(&oil.il_req); |
Tristan Ye | ddee5cd | 2010-05-22 16:26:33 +0800 | [diff] [blame] | 231 | |
| 232 | if (o2info_to_user(oil, req)) |
| 233 | goto bail; |
| 234 | |
| 235 | status = 0; |
| 236 | bail: |
| 237 | if (status) |
Tristan Ye | 8aa1fa3 | 2011-05-24 15:22:59 +0800 | [diff] [blame] | 238 | o2info_set_request_error(&oil.il_req, req); |
Tristan Ye | ddee5cd | 2010-05-22 16:26:33 +0800 | [diff] [blame] | 239 | |
| 240 | return status; |
| 241 | } |
| 242 | |
| 243 | int ocfs2_info_handle_uuid(struct inode *inode, |
| 244 | struct ocfs2_info_request __user *req) |
| 245 | { |
| 246 | int status = -EFAULT; |
| 247 | struct ocfs2_info_uuid oiu; |
| 248 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 249 | |
| 250 | if (o2info_from_user(oiu, req)) |
| 251 | goto bail; |
| 252 | |
| 253 | memcpy(oiu.iu_uuid_str, osb->uuid_str, OCFS2_TEXT_UUID_LEN + 1); |
Tristan Ye | 1936a26 | 2011-01-30 14:25:59 +0800 | [diff] [blame] | 254 | |
Tristan Ye | 8aa1fa3 | 2011-05-24 15:22:59 +0800 | [diff] [blame] | 255 | o2info_set_request_filled(&oiu.iu_req); |
Tristan Ye | ddee5cd | 2010-05-22 16:26:33 +0800 | [diff] [blame] | 256 | |
| 257 | if (o2info_to_user(oiu, req)) |
| 258 | goto bail; |
| 259 | |
| 260 | status = 0; |
| 261 | bail: |
| 262 | if (status) |
Tristan Ye | 8aa1fa3 | 2011-05-24 15:22:59 +0800 | [diff] [blame] | 263 | o2info_set_request_error(&oiu.iu_req, req); |
Tristan Ye | ddee5cd | 2010-05-22 16:26:33 +0800 | [diff] [blame] | 264 | |
| 265 | return status; |
| 266 | } |
| 267 | |
| 268 | int ocfs2_info_handle_fs_features(struct inode *inode, |
| 269 | struct ocfs2_info_request __user *req) |
| 270 | { |
| 271 | int status = -EFAULT; |
| 272 | struct ocfs2_info_fs_features oif; |
| 273 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 274 | |
| 275 | if (o2info_from_user(oif, req)) |
| 276 | goto bail; |
| 277 | |
| 278 | oif.if_compat_features = osb->s_feature_compat; |
| 279 | oif.if_incompat_features = osb->s_feature_incompat; |
| 280 | oif.if_ro_compat_features = osb->s_feature_ro_compat; |
Tristan Ye | 1936a26 | 2011-01-30 14:25:59 +0800 | [diff] [blame] | 281 | |
Tristan Ye | 8aa1fa3 | 2011-05-24 15:22:59 +0800 | [diff] [blame] | 282 | o2info_set_request_filled(&oif.if_req); |
Tristan Ye | ddee5cd | 2010-05-22 16:26:33 +0800 | [diff] [blame] | 283 | |
| 284 | if (o2info_to_user(oif, req)) |
| 285 | goto bail; |
| 286 | |
| 287 | status = 0; |
| 288 | bail: |
| 289 | if (status) |
Tristan Ye | 8aa1fa3 | 2011-05-24 15:22:59 +0800 | [diff] [blame] | 290 | o2info_set_request_error(&oif.if_req, req); |
Tristan Ye | ddee5cd | 2010-05-22 16:26:33 +0800 | [diff] [blame] | 291 | |
| 292 | return status; |
| 293 | } |
| 294 | |
| 295 | int ocfs2_info_handle_journal_size(struct inode *inode, |
| 296 | struct ocfs2_info_request __user *req) |
| 297 | { |
| 298 | int status = -EFAULT; |
| 299 | struct ocfs2_info_journal_size oij; |
| 300 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 301 | |
| 302 | if (o2info_from_user(oij, req)) |
| 303 | goto bail; |
| 304 | |
| 305 | oij.ij_journal_size = osb->journal->j_inode->i_size; |
| 306 | |
Tristan Ye | 8aa1fa3 | 2011-05-24 15:22:59 +0800 | [diff] [blame] | 307 | o2info_set_request_filled(&oij.ij_req); |
Tristan Ye | ddee5cd | 2010-05-22 16:26:33 +0800 | [diff] [blame] | 308 | |
| 309 | if (o2info_to_user(oij, req)) |
| 310 | goto bail; |
| 311 | |
| 312 | status = 0; |
| 313 | bail: |
| 314 | if (status) |
Tristan Ye | 8aa1fa3 | 2011-05-24 15:22:59 +0800 | [diff] [blame] | 315 | o2info_set_request_error(&oij.ij_req, req); |
Tristan Ye | ddee5cd | 2010-05-22 16:26:33 +0800 | [diff] [blame] | 316 | |
| 317 | return status; |
| 318 | } |
| 319 | |
Tristan Ye | 3e5db17 | 2011-05-24 15:25:54 +0800 | [diff] [blame^] | 320 | int ocfs2_info_scan_inode_alloc(struct ocfs2_super *osb, |
| 321 | struct inode *inode_alloc, u64 blkno, |
| 322 | struct ocfs2_info_freeinode *fi, u32 slot) |
| 323 | { |
| 324 | int status = 0, unlock = 0; |
| 325 | |
| 326 | struct buffer_head *bh = NULL; |
| 327 | struct ocfs2_dinode *dinode_alloc = NULL; |
| 328 | |
| 329 | if (inode_alloc) |
| 330 | mutex_lock(&inode_alloc->i_mutex); |
| 331 | |
| 332 | if (o2info_coherent(&fi->ifi_req)) { |
| 333 | status = ocfs2_inode_lock(inode_alloc, &bh, 0); |
| 334 | if (status < 0) { |
| 335 | mlog_errno(status); |
| 336 | goto bail; |
| 337 | } |
| 338 | unlock = 1; |
| 339 | } else { |
| 340 | status = ocfs2_read_blocks_sync(osb, blkno, 1, &bh); |
| 341 | if (status < 0) { |
| 342 | mlog_errno(status); |
| 343 | goto bail; |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | dinode_alloc = (struct ocfs2_dinode *)bh->b_data; |
| 348 | |
| 349 | fi->ifi_stat[slot].lfi_total = |
| 350 | le32_to_cpu(dinode_alloc->id1.bitmap1.i_total); |
| 351 | fi->ifi_stat[slot].lfi_free = |
| 352 | le32_to_cpu(dinode_alloc->id1.bitmap1.i_total) - |
| 353 | le32_to_cpu(dinode_alloc->id1.bitmap1.i_used); |
| 354 | |
| 355 | bail: |
| 356 | if (unlock) |
| 357 | ocfs2_inode_unlock(inode_alloc, 0); |
| 358 | |
| 359 | if (inode_alloc) |
| 360 | mutex_unlock(&inode_alloc->i_mutex); |
| 361 | |
| 362 | brelse(bh); |
| 363 | |
| 364 | return status; |
| 365 | } |
| 366 | |
| 367 | int ocfs2_info_handle_freeinode(struct inode *inode, |
| 368 | struct ocfs2_info_request __user *req) |
| 369 | { |
| 370 | u32 i; |
| 371 | u64 blkno = -1; |
| 372 | char namebuf[40]; |
| 373 | int status = -EFAULT, type = INODE_ALLOC_SYSTEM_INODE; |
| 374 | struct ocfs2_info_freeinode *oifi = NULL; |
| 375 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 376 | struct inode *inode_alloc = NULL; |
| 377 | |
| 378 | oifi = kzalloc(sizeof(struct ocfs2_info_freeinode), GFP_KERNEL); |
| 379 | if (!oifi) { |
| 380 | status = -ENOMEM; |
| 381 | mlog_errno(status); |
| 382 | goto bail; |
| 383 | } |
| 384 | |
| 385 | if (o2info_from_user(*oifi, req)) |
| 386 | goto bail; |
| 387 | |
| 388 | oifi->ifi_slotnum = osb->max_slots; |
| 389 | |
| 390 | for (i = 0; i < oifi->ifi_slotnum; i++) { |
| 391 | if (o2info_coherent(&oifi->ifi_req)) { |
| 392 | inode_alloc = ocfs2_get_system_file_inode(osb, type, i); |
| 393 | if (!inode_alloc) { |
| 394 | mlog(ML_ERROR, "unable to get alloc inode in " |
| 395 | "slot %u\n", i); |
| 396 | status = -EIO; |
| 397 | goto bail; |
| 398 | } |
| 399 | } else { |
| 400 | ocfs2_sprintf_system_inode_name(namebuf, |
| 401 | sizeof(namebuf), |
| 402 | type, i); |
| 403 | status = ocfs2_lookup_ino_from_name(osb->sys_root_inode, |
| 404 | namebuf, |
| 405 | strlen(namebuf), |
| 406 | &blkno); |
| 407 | if (status < 0) { |
| 408 | status = -ENOENT; |
| 409 | goto bail; |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | status = ocfs2_info_scan_inode_alloc(osb, inode_alloc, blkno, oifi, i); |
| 414 | if (status < 0) |
| 415 | goto bail; |
| 416 | |
| 417 | iput(inode_alloc); |
| 418 | inode_alloc = NULL; |
| 419 | } |
| 420 | |
| 421 | o2info_set_request_filled(&oifi->ifi_req); |
| 422 | |
| 423 | if (o2info_to_user(*oifi, req)) |
| 424 | goto bail; |
| 425 | |
| 426 | status = 0; |
| 427 | bail: |
| 428 | if (status) |
| 429 | o2info_set_request_error(&oifi->ifi_req, req); |
| 430 | |
| 431 | kfree(oifi); |
| 432 | |
| 433 | return status; |
| 434 | } |
| 435 | |
Tristan Ye | ddee5cd | 2010-05-22 16:26:33 +0800 | [diff] [blame] | 436 | int ocfs2_info_handle_unknown(struct inode *inode, |
| 437 | struct ocfs2_info_request __user *req) |
| 438 | { |
| 439 | int status = -EFAULT; |
| 440 | struct ocfs2_info_request oir; |
| 441 | |
| 442 | if (o2info_from_user(oir, req)) |
| 443 | goto bail; |
| 444 | |
Tristan Ye | 8aa1fa3 | 2011-05-24 15:22:59 +0800 | [diff] [blame] | 445 | o2info_clear_request_filled(&oir); |
Tristan Ye | ddee5cd | 2010-05-22 16:26:33 +0800 | [diff] [blame] | 446 | |
| 447 | if (o2info_to_user(oir, req)) |
| 448 | goto bail; |
| 449 | |
| 450 | status = 0; |
| 451 | bail: |
| 452 | if (status) |
Tristan Ye | 8aa1fa3 | 2011-05-24 15:22:59 +0800 | [diff] [blame] | 453 | o2info_set_request_error(&oir, req); |
Tristan Ye | ddee5cd | 2010-05-22 16:26:33 +0800 | [diff] [blame] | 454 | |
| 455 | return status; |
| 456 | } |
| 457 | |
| 458 | /* |
| 459 | * Validate and distinguish OCFS2_IOC_INFO requests. |
| 460 | * |
| 461 | * - validate the magic number. |
| 462 | * - distinguish different requests. |
| 463 | * - validate size of different requests. |
| 464 | */ |
| 465 | int ocfs2_info_handle_request(struct inode *inode, |
| 466 | struct ocfs2_info_request __user *req) |
| 467 | { |
| 468 | int status = -EFAULT; |
| 469 | struct ocfs2_info_request oir; |
| 470 | |
| 471 | if (o2info_from_user(oir, req)) |
| 472 | goto bail; |
| 473 | |
| 474 | status = -EINVAL; |
| 475 | if (oir.ir_magic != OCFS2_INFO_MAGIC) |
| 476 | goto bail; |
| 477 | |
| 478 | switch (oir.ir_code) { |
| 479 | case OCFS2_INFO_BLOCKSIZE: |
| 480 | if (oir.ir_size == sizeof(struct ocfs2_info_blocksize)) |
| 481 | status = ocfs2_info_handle_blocksize(inode, req); |
| 482 | break; |
| 483 | case OCFS2_INFO_CLUSTERSIZE: |
| 484 | if (oir.ir_size == sizeof(struct ocfs2_info_clustersize)) |
| 485 | status = ocfs2_info_handle_clustersize(inode, req); |
| 486 | break; |
| 487 | case OCFS2_INFO_MAXSLOTS: |
| 488 | if (oir.ir_size == sizeof(struct ocfs2_info_maxslots)) |
| 489 | status = ocfs2_info_handle_maxslots(inode, req); |
| 490 | break; |
| 491 | case OCFS2_INFO_LABEL: |
| 492 | if (oir.ir_size == sizeof(struct ocfs2_info_label)) |
| 493 | status = ocfs2_info_handle_label(inode, req); |
| 494 | break; |
| 495 | case OCFS2_INFO_UUID: |
| 496 | if (oir.ir_size == sizeof(struct ocfs2_info_uuid)) |
| 497 | status = ocfs2_info_handle_uuid(inode, req); |
| 498 | break; |
| 499 | case OCFS2_INFO_FS_FEATURES: |
| 500 | if (oir.ir_size == sizeof(struct ocfs2_info_fs_features)) |
| 501 | status = ocfs2_info_handle_fs_features(inode, req); |
| 502 | break; |
| 503 | case OCFS2_INFO_JOURNAL_SIZE: |
| 504 | if (oir.ir_size == sizeof(struct ocfs2_info_journal_size)) |
| 505 | status = ocfs2_info_handle_journal_size(inode, req); |
| 506 | break; |
Tristan Ye | 3e5db17 | 2011-05-24 15:25:54 +0800 | [diff] [blame^] | 507 | case OCFS2_INFO_FREEINODE: |
| 508 | if (oir.ir_size == sizeof(struct ocfs2_info_freeinode)) |
| 509 | status = ocfs2_info_handle_freeinode(inode, req); |
| 510 | break; |
Tristan Ye | ddee5cd | 2010-05-22 16:26:33 +0800 | [diff] [blame] | 511 | default: |
| 512 | status = ocfs2_info_handle_unknown(inode, req); |
| 513 | break; |
| 514 | } |
| 515 | |
| 516 | bail: |
| 517 | return status; |
| 518 | } |
| 519 | |
| 520 | int ocfs2_get_request_ptr(struct ocfs2_info *info, int idx, |
| 521 | u64 *req_addr, int compat_flag) |
| 522 | { |
| 523 | int status = -EFAULT; |
| 524 | u64 __user *bp = NULL; |
| 525 | |
| 526 | if (compat_flag) { |
| 527 | #ifdef CONFIG_COMPAT |
| 528 | /* |
| 529 | * pointer bp stores the base address of a pointers array, |
| 530 | * which collects all addresses of separate request. |
| 531 | */ |
| 532 | bp = (u64 __user *)(unsigned long)compat_ptr(info->oi_requests); |
| 533 | #else |
| 534 | BUG(); |
| 535 | #endif |
| 536 | } else |
| 537 | bp = (u64 __user *)(unsigned long)(info->oi_requests); |
| 538 | |
| 539 | if (o2info_from_user(*req_addr, bp + idx)) |
| 540 | goto bail; |
| 541 | |
| 542 | status = 0; |
| 543 | bail: |
| 544 | return status; |
| 545 | } |
| 546 | |
| 547 | /* |
| 548 | * OCFS2_IOC_INFO handles an array of requests passed from userspace. |
| 549 | * |
| 550 | * ocfs2_info_handle() recevies a large info aggregation, grab and |
| 551 | * validate the request count from header, then break it into small |
| 552 | * pieces, later specific handlers can handle them one by one. |
| 553 | * |
| 554 | * Idea here is to make each separate request small enough to ensure |
| 555 | * a better backward&forward compatibility, since a small piece of |
| 556 | * request will be less likely to be broken if disk layout get changed. |
| 557 | */ |
| 558 | int ocfs2_info_handle(struct inode *inode, struct ocfs2_info *info, |
| 559 | int compat_flag) |
| 560 | { |
| 561 | int i, status = 0; |
| 562 | u64 req_addr; |
| 563 | struct ocfs2_info_request __user *reqp; |
| 564 | |
| 565 | if ((info->oi_count > OCFS2_INFO_MAX_REQUEST) || |
| 566 | (!info->oi_requests)) { |
| 567 | status = -EINVAL; |
| 568 | goto bail; |
| 569 | } |
| 570 | |
| 571 | for (i = 0; i < info->oi_count; i++) { |
| 572 | |
| 573 | status = ocfs2_get_request_ptr(info, i, &req_addr, compat_flag); |
| 574 | if (status) |
| 575 | break; |
| 576 | |
| 577 | reqp = (struct ocfs2_info_request *)(unsigned long)req_addr; |
| 578 | if (!reqp) { |
| 579 | status = -EINVAL; |
| 580 | goto bail; |
| 581 | } |
| 582 | |
| 583 | status = ocfs2_info_handle_request(inode, reqp); |
| 584 | if (status) |
| 585 | break; |
| 586 | } |
| 587 | |
| 588 | bail: |
| 589 | return status; |
| 590 | } |
| 591 | |
Andi Kleen | c9ec148 | 2008-01-27 03:17:17 +0100 | [diff] [blame] | 592 | long ocfs2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) |
Herbert Poetzl | ca4d147 | 2006-07-03 17:27:12 -0700 | [diff] [blame] | 593 | { |
Andi Kleen | c9ec148 | 2008-01-27 03:17:17 +0100 | [diff] [blame] | 594 | struct inode *inode = filp->f_path.dentry->d_inode; |
Herbert Poetzl | ca4d147 | 2006-07-03 17:27:12 -0700 | [diff] [blame] | 595 | unsigned int flags; |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 596 | int new_clusters; |
Herbert Poetzl | ca4d147 | 2006-07-03 17:27:12 -0700 | [diff] [blame] | 597 | int status; |
Mark Fasheh | b258010 | 2007-03-09 16:53:21 -0800 | [diff] [blame] | 598 | struct ocfs2_space_resv sr; |
Tao Ma | 7909f2b | 2007-12-18 15:47:25 +0800 | [diff] [blame] | 599 | struct ocfs2_new_group_input input; |
Tao Ma | bd50873 | 2009-09-21 11:25:14 +0800 | [diff] [blame] | 600 | struct reflink_arguments args; |
| 601 | const char *old_path, *new_path; |
| 602 | bool preserve; |
Tristan Ye | ddee5cd | 2010-05-22 16:26:33 +0800 | [diff] [blame] | 603 | struct ocfs2_info info; |
Herbert Poetzl | ca4d147 | 2006-07-03 17:27:12 -0700 | [diff] [blame] | 604 | |
| 605 | switch (cmd) { |
| 606 | case OCFS2_IOC_GETFLAGS: |
| 607 | status = ocfs2_get_inode_attr(inode, &flags); |
| 608 | if (status < 0) |
| 609 | return status; |
| 610 | |
| 611 | flags &= OCFS2_FL_VISIBLE; |
| 612 | return put_user(flags, (int __user *) arg); |
| 613 | case OCFS2_IOC_SETFLAGS: |
| 614 | if (get_user(flags, (int __user *) arg)) |
| 615 | return -EFAULT; |
| 616 | |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 617 | status = mnt_want_write(filp->f_path.mnt); |
| 618 | if (status) |
| 619 | return status; |
| 620 | status = ocfs2_set_inode_attr(inode, flags, |
Herbert Poetzl | ca4d147 | 2006-07-03 17:27:12 -0700 | [diff] [blame] | 621 | OCFS2_FL_MODIFIABLE); |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 622 | mnt_drop_write(filp->f_path.mnt); |
| 623 | return status; |
Mark Fasheh | b258010 | 2007-03-09 16:53:21 -0800 | [diff] [blame] | 624 | case OCFS2_IOC_RESVSP: |
| 625 | case OCFS2_IOC_RESVSP64: |
| 626 | case OCFS2_IOC_UNRESVSP: |
| 627 | case OCFS2_IOC_UNRESVSP64: |
| 628 | if (copy_from_user(&sr, (int __user *) arg, sizeof(sr))) |
| 629 | return -EFAULT; |
| 630 | |
| 631 | return ocfs2_change_file_space(filp, cmd, &sr); |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 632 | case OCFS2_IOC_GROUP_EXTEND: |
Mark Fasheh | 0957f00 | 2007-12-18 18:58:18 -0800 | [diff] [blame] | 633 | if (!capable(CAP_SYS_RESOURCE)) |
| 634 | return -EPERM; |
| 635 | |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 636 | if (get_user(new_clusters, (int __user *)arg)) |
| 637 | return -EFAULT; |
| 638 | |
| 639 | return ocfs2_group_extend(inode, new_clusters); |
Tao Ma | 7909f2b | 2007-12-18 15:47:25 +0800 | [diff] [blame] | 640 | case OCFS2_IOC_GROUP_ADD: |
| 641 | case OCFS2_IOC_GROUP_ADD64: |
Mark Fasheh | 0957f00 | 2007-12-18 18:58:18 -0800 | [diff] [blame] | 642 | if (!capable(CAP_SYS_RESOURCE)) |
| 643 | return -EPERM; |
| 644 | |
Tao Ma | 7909f2b | 2007-12-18 15:47:25 +0800 | [diff] [blame] | 645 | if (copy_from_user(&input, (int __user *) arg, sizeof(input))) |
| 646 | return -EFAULT; |
| 647 | |
| 648 | return ocfs2_group_add(inode, &input); |
Tao Ma | bd50873 | 2009-09-21 11:25:14 +0800 | [diff] [blame] | 649 | case OCFS2_IOC_REFLINK: |
| 650 | if (copy_from_user(&args, (struct reflink_arguments *)arg, |
| 651 | sizeof(args))) |
| 652 | return -EFAULT; |
| 653 | old_path = (const char *)(unsigned long)args.old_path; |
| 654 | new_path = (const char *)(unsigned long)args.new_path; |
| 655 | preserve = (args.preserve != 0); |
| 656 | |
| 657 | return ocfs2_reflink_ioctl(inode, old_path, new_path, preserve); |
Tristan Ye | ddee5cd | 2010-05-22 16:26:33 +0800 | [diff] [blame] | 658 | case OCFS2_IOC_INFO: |
| 659 | if (copy_from_user(&info, (struct ocfs2_info __user *)arg, |
| 660 | sizeof(struct ocfs2_info))) |
| 661 | return -EFAULT; |
| 662 | |
| 663 | return ocfs2_info_handle(inode, &info, 0); |
Herbert Poetzl | ca4d147 | 2006-07-03 17:27:12 -0700 | [diff] [blame] | 664 | default: |
| 665 | return -ENOTTY; |
| 666 | } |
| 667 | } |
| 668 | |
Mark Fasheh | 586d232 | 2007-03-09 15:56:28 -0800 | [diff] [blame] | 669 | #ifdef CONFIG_COMPAT |
| 670 | long ocfs2_compat_ioctl(struct file *file, unsigned cmd, unsigned long arg) |
| 671 | { |
Tao Ma | 34e6c59 | 2010-01-27 10:21:52 +0800 | [diff] [blame] | 672 | bool preserve; |
| 673 | struct reflink_arguments args; |
| 674 | struct inode *inode = file->f_path.dentry->d_inode; |
Tristan Ye | ddee5cd | 2010-05-22 16:26:33 +0800 | [diff] [blame] | 675 | struct ocfs2_info info; |
Tao Ma | 34e6c59 | 2010-01-27 10:21:52 +0800 | [diff] [blame] | 676 | |
Mark Fasheh | 586d232 | 2007-03-09 15:56:28 -0800 | [diff] [blame] | 677 | switch (cmd) { |
| 678 | case OCFS2_IOC32_GETFLAGS: |
| 679 | cmd = OCFS2_IOC_GETFLAGS; |
| 680 | break; |
| 681 | case OCFS2_IOC32_SETFLAGS: |
| 682 | cmd = OCFS2_IOC_SETFLAGS; |
| 683 | break; |
Mark Fasheh | b258010 | 2007-03-09 16:53:21 -0800 | [diff] [blame] | 684 | case OCFS2_IOC_RESVSP: |
| 685 | case OCFS2_IOC_RESVSP64: |
| 686 | case OCFS2_IOC_UNRESVSP: |
| 687 | case OCFS2_IOC_UNRESVSP64: |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 688 | case OCFS2_IOC_GROUP_EXTEND: |
Tao Ma | 7909f2b | 2007-12-18 15:47:25 +0800 | [diff] [blame] | 689 | case OCFS2_IOC_GROUP_ADD: |
| 690 | case OCFS2_IOC_GROUP_ADD64: |
Mark Fasheh | b258010 | 2007-03-09 16:53:21 -0800 | [diff] [blame] | 691 | break; |
Tao Ma | 34e6c59 | 2010-01-27 10:21:52 +0800 | [diff] [blame] | 692 | case OCFS2_IOC_REFLINK: |
| 693 | if (copy_from_user(&args, (struct reflink_arguments *)arg, |
| 694 | sizeof(args))) |
| 695 | return -EFAULT; |
| 696 | preserve = (args.preserve != 0); |
| 697 | |
| 698 | return ocfs2_reflink_ioctl(inode, compat_ptr(args.old_path), |
| 699 | compat_ptr(args.new_path), preserve); |
Tristan Ye | ddee5cd | 2010-05-22 16:26:33 +0800 | [diff] [blame] | 700 | case OCFS2_IOC_INFO: |
| 701 | if (copy_from_user(&info, (struct ocfs2_info __user *)arg, |
| 702 | sizeof(struct ocfs2_info))) |
| 703 | return -EFAULT; |
| 704 | |
| 705 | return ocfs2_info_handle(inode, &info, 1); |
Mark Fasheh | 586d232 | 2007-03-09 15:56:28 -0800 | [diff] [blame] | 706 | default: |
| 707 | return -ENOIOCTLCMD; |
| 708 | } |
| 709 | |
Andi Kleen | c9ec148 | 2008-01-27 03:17:17 +0100 | [diff] [blame] | 710 | return ocfs2_ioctl(file, cmd, arg); |
Mark Fasheh | 586d232 | 2007-03-09 15:56:28 -0800 | [diff] [blame] | 711 | } |
| 712 | #endif |