2 * linux/fs/ocfs2/ioctl.c
4 * Copyright (C) 2006 Herbert Poetzl
5 * adapted from Remy Card's ext2/ioctl.c
9 #include <linux/mount.h>
10 #include <linux/compat.h>
12 #define MLOG_MASK_PREFIX ML_INODE
13 #include <cluster/masklog.h>
25 #include "refcounttree.h"
27 #include <linux/ext2_fs.h>
29 #define o2info_from_user(a, b) \
30 copy_from_user(&(a), (b), sizeof(a))
31 #define o2info_to_user(a, b) \
32 copy_to_user((typeof(a) __user *)b, &(a), sizeof(a))
35 * This call is void because we are already reporting an error that may
36 * be -EFAULT. The error will be returned from the ioctl(2) call. It's
37 * just a best-effort to tell userspace that this request caused the error.
39 static inline void __o2info_set_request_error(struct ocfs2_info_request *kreq,
40 struct ocfs2_info_request __user *req)
42 kreq->ir_flags |= OCFS2_INFO_FL_ERROR;
43 (void)put_user(kreq->ir_flags, (__u32 __user *)&(req->ir_flags));
46 #define o2info_set_request_error(a, b) \
47 __o2info_set_request_error((struct ocfs2_info_request *)&(a), b)
49 static int ocfs2_get_inode_attr(struct inode *inode, unsigned *flags)
53 status = ocfs2_inode_lock(inode, NULL, 0);
58 ocfs2_get_inode_flags(OCFS2_I(inode));
59 *flags = OCFS2_I(inode)->ip_attr;
60 ocfs2_inode_unlock(inode, 0);
65 static int ocfs2_set_inode_attr(struct inode *inode, unsigned flags,
68 struct ocfs2_inode_info *ocfs2_inode = OCFS2_I(inode);
69 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
70 handle_t *handle = NULL;
71 struct buffer_head *bh = NULL;
75 mutex_lock(&inode->i_mutex);
77 status = ocfs2_inode_lock(inode, &bh, 1);
84 if (!is_owner_or_cap(inode))
87 if (!S_ISDIR(inode->i_mode))
88 flags &= ~OCFS2_DIRSYNC_FL;
90 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
92 status = PTR_ERR(handle);
97 oldflags = ocfs2_inode->ip_attr;
99 flags |= oldflags & ~mask;
102 * The IMMUTABLE and APPEND_ONLY flags can only be changed by
103 * the relevant capability.
106 if ((oldflags & OCFS2_IMMUTABLE_FL) || ((flags ^ oldflags) &
107 (OCFS2_APPEND_FL | OCFS2_IMMUTABLE_FL))) {
108 if (!capable(CAP_LINUX_IMMUTABLE))
112 ocfs2_inode->ip_attr = flags;
113 ocfs2_set_inode_flags(inode);
115 status = ocfs2_mark_inode_dirty(handle, inode, bh);
119 ocfs2_commit_trans(osb, handle);
121 ocfs2_inode_unlock(inode, 1);
123 mutex_unlock(&inode->i_mutex);
130 int ocfs2_info_handle_blocksize(struct inode *inode,
131 struct ocfs2_info_request __user *req)
133 int status = -EFAULT;
134 struct ocfs2_info_blocksize oib;
136 if (o2info_from_user(oib, req))
139 oib.ib_blocksize = inode->i_sb->s_blocksize;
140 oib.ib_req.ir_flags |= OCFS2_INFO_FL_FILLED;
142 if (o2info_to_user(oib, req))
148 o2info_set_request_error(oib, req);
153 int ocfs2_info_handle_clustersize(struct inode *inode,
154 struct ocfs2_info_request __user *req)
156 int status = -EFAULT;
157 struct ocfs2_info_clustersize oic;
158 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
160 if (o2info_from_user(oic, req))
163 oic.ic_clustersize = osb->s_clustersize;
164 oic.ic_req.ir_flags |= OCFS2_INFO_FL_FILLED;
166 if (o2info_to_user(oic, req))
172 o2info_set_request_error(oic, req);
177 int ocfs2_info_handle_maxslots(struct inode *inode,
178 struct ocfs2_info_request __user *req)
180 int status = -EFAULT;
181 struct ocfs2_info_maxslots oim;
182 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
184 if (o2info_from_user(oim, req))
187 oim.im_max_slots = osb->max_slots;
188 oim.im_req.ir_flags |= OCFS2_INFO_FL_FILLED;
190 if (o2info_to_user(oim, req))
196 o2info_set_request_error(oim, req);
201 int ocfs2_info_handle_label(struct inode *inode,
202 struct ocfs2_info_request __user *req)
204 int status = -EFAULT;
205 struct ocfs2_info_label oil;
206 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
208 if (o2info_from_user(oil, req))
211 memcpy(oil.il_label, osb->vol_label, OCFS2_MAX_VOL_LABEL_LEN);
212 oil.il_req.ir_flags |= OCFS2_INFO_FL_FILLED;
214 if (o2info_to_user(oil, req))
220 o2info_set_request_error(oil, req);
225 int ocfs2_info_handle_uuid(struct inode *inode,
226 struct ocfs2_info_request __user *req)
228 int status = -EFAULT;
229 struct ocfs2_info_uuid oiu;
230 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
232 if (o2info_from_user(oiu, req))
235 memcpy(oiu.iu_uuid_str, osb->uuid_str, OCFS2_TEXT_UUID_LEN + 1);
236 oiu.iu_req.ir_flags |= OCFS2_INFO_FL_FILLED;
238 if (o2info_to_user(oiu, req))
244 o2info_set_request_error(oiu, req);
249 int ocfs2_info_handle_fs_features(struct inode *inode,
250 struct ocfs2_info_request __user *req)
252 int status = -EFAULT;
253 struct ocfs2_info_fs_features oif;
254 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
256 if (o2info_from_user(oif, req))
259 oif.if_compat_features = osb->s_feature_compat;
260 oif.if_incompat_features = osb->s_feature_incompat;
261 oif.if_ro_compat_features = osb->s_feature_ro_compat;
262 oif.if_req.ir_flags |= OCFS2_INFO_FL_FILLED;
264 if (o2info_to_user(oif, req))
270 o2info_set_request_error(oif, req);
275 int ocfs2_info_handle_journal_size(struct inode *inode,
276 struct ocfs2_info_request __user *req)
278 int status = -EFAULT;
279 struct ocfs2_info_journal_size oij;
280 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
282 if (o2info_from_user(oij, req))
285 oij.ij_journal_size = osb->journal->j_inode->i_size;
287 oij.ij_req.ir_flags |= OCFS2_INFO_FL_FILLED;
289 if (o2info_to_user(oij, req))
295 o2info_set_request_error(oij, req);
300 int ocfs2_info_handle_unknown(struct inode *inode,
301 struct ocfs2_info_request __user *req)
303 int status = -EFAULT;
304 struct ocfs2_info_request oir;
306 if (o2info_from_user(oir, req))
309 oir.ir_flags &= ~OCFS2_INFO_FL_FILLED;
311 if (o2info_to_user(oir, req))
317 o2info_set_request_error(oir, req);
323 * Validate and distinguish OCFS2_IOC_INFO requests.
325 * - validate the magic number.
326 * - distinguish different requests.
327 * - validate size of different requests.
329 int ocfs2_info_handle_request(struct inode *inode,
330 struct ocfs2_info_request __user *req)
332 int status = -EFAULT;
333 struct ocfs2_info_request oir;
335 if (o2info_from_user(oir, req))
339 if (oir.ir_magic != OCFS2_INFO_MAGIC)
342 switch (oir.ir_code) {
343 case OCFS2_INFO_BLOCKSIZE:
344 if (oir.ir_size == sizeof(struct ocfs2_info_blocksize))
345 status = ocfs2_info_handle_blocksize(inode, req);
347 case OCFS2_INFO_CLUSTERSIZE:
348 if (oir.ir_size == sizeof(struct ocfs2_info_clustersize))
349 status = ocfs2_info_handle_clustersize(inode, req);
351 case OCFS2_INFO_MAXSLOTS:
352 if (oir.ir_size == sizeof(struct ocfs2_info_maxslots))
353 status = ocfs2_info_handle_maxslots(inode, req);
355 case OCFS2_INFO_LABEL:
356 if (oir.ir_size == sizeof(struct ocfs2_info_label))
357 status = ocfs2_info_handle_label(inode, req);
359 case OCFS2_INFO_UUID:
360 if (oir.ir_size == sizeof(struct ocfs2_info_uuid))
361 status = ocfs2_info_handle_uuid(inode, req);
363 case OCFS2_INFO_FS_FEATURES:
364 if (oir.ir_size == sizeof(struct ocfs2_info_fs_features))
365 status = ocfs2_info_handle_fs_features(inode, req);
367 case OCFS2_INFO_JOURNAL_SIZE:
368 if (oir.ir_size == sizeof(struct ocfs2_info_journal_size))
369 status = ocfs2_info_handle_journal_size(inode, req);
372 status = ocfs2_info_handle_unknown(inode, req);
380 int ocfs2_get_request_ptr(struct ocfs2_info *info, int idx,
381 u64 *req_addr, int compat_flag)
383 int status = -EFAULT;
384 u64 __user *bp = NULL;
389 * pointer bp stores the base address of a pointers array,
390 * which collects all addresses of separate request.
392 bp = (u64 __user *)(unsigned long)compat_ptr(info->oi_requests);
397 bp = (u64 __user *)(unsigned long)(info->oi_requests);
399 if (o2info_from_user(*req_addr, bp + idx))
408 * OCFS2_IOC_INFO handles an array of requests passed from userspace.
410 * ocfs2_info_handle() recevies a large info aggregation, grab and
411 * validate the request count from header, then break it into small
412 * pieces, later specific handlers can handle them one by one.
414 * Idea here is to make each separate request small enough to ensure
415 * a better backward&forward compatibility, since a small piece of
416 * request will be less likely to be broken if disk layout get changed.
418 int ocfs2_info_handle(struct inode *inode, struct ocfs2_info *info,
423 struct ocfs2_info_request __user *reqp;
425 if ((info->oi_count > OCFS2_INFO_MAX_REQUEST) ||
426 (!info->oi_requests)) {
431 for (i = 0; i < info->oi_count; i++) {
433 status = ocfs2_get_request_ptr(info, i, &req_addr, compat_flag);
437 reqp = (struct ocfs2_info_request *)(unsigned long)req_addr;
443 status = ocfs2_info_handle_request(inode, reqp);
452 long ocfs2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
454 struct inode *inode = filp->f_path.dentry->d_inode;
458 struct ocfs2_space_resv sr;
459 struct ocfs2_new_group_input input;
460 struct reflink_arguments args;
461 const char *old_path, *new_path;
463 struct ocfs2_info info;
466 case OCFS2_IOC_GETFLAGS:
467 status = ocfs2_get_inode_attr(inode, &flags);
471 flags &= OCFS2_FL_VISIBLE;
472 return put_user(flags, (int __user *) arg);
473 case OCFS2_IOC_SETFLAGS:
474 if (get_user(flags, (int __user *) arg))
477 status = mnt_want_write(filp->f_path.mnt);
480 status = ocfs2_set_inode_attr(inode, flags,
481 OCFS2_FL_MODIFIABLE);
482 mnt_drop_write(filp->f_path.mnt);
484 case OCFS2_IOC_RESVSP:
485 case OCFS2_IOC_RESVSP64:
486 case OCFS2_IOC_UNRESVSP:
487 case OCFS2_IOC_UNRESVSP64:
488 if (copy_from_user(&sr, (int __user *) arg, sizeof(sr)))
491 return ocfs2_change_file_space(filp, cmd, &sr);
492 case OCFS2_IOC_GROUP_EXTEND:
493 if (!capable(CAP_SYS_RESOURCE))
496 if (get_user(new_clusters, (int __user *)arg))
499 return ocfs2_group_extend(inode, new_clusters);
500 case OCFS2_IOC_GROUP_ADD:
501 case OCFS2_IOC_GROUP_ADD64:
502 if (!capable(CAP_SYS_RESOURCE))
505 if (copy_from_user(&input, (int __user *) arg, sizeof(input)))
508 return ocfs2_group_add(inode, &input);
509 case OCFS2_IOC_REFLINK:
510 if (copy_from_user(&args, (struct reflink_arguments *)arg,
513 old_path = (const char *)(unsigned long)args.old_path;
514 new_path = (const char *)(unsigned long)args.new_path;
515 preserve = (args.preserve != 0);
517 return ocfs2_reflink_ioctl(inode, old_path, new_path, preserve);
519 if (copy_from_user(&info, (struct ocfs2_info __user *)arg,
520 sizeof(struct ocfs2_info)))
523 return ocfs2_info_handle(inode, &info, 0);
530 long ocfs2_compat_ioctl(struct file *file, unsigned cmd, unsigned long arg)
533 struct reflink_arguments args;
534 struct inode *inode = file->f_path.dentry->d_inode;
535 struct ocfs2_info info;
538 case OCFS2_IOC32_GETFLAGS:
539 cmd = OCFS2_IOC_GETFLAGS;
541 case OCFS2_IOC32_SETFLAGS:
542 cmd = OCFS2_IOC_SETFLAGS;
544 case OCFS2_IOC_RESVSP:
545 case OCFS2_IOC_RESVSP64:
546 case OCFS2_IOC_UNRESVSP:
547 case OCFS2_IOC_UNRESVSP64:
548 case OCFS2_IOC_GROUP_EXTEND:
549 case OCFS2_IOC_GROUP_ADD:
550 case OCFS2_IOC_GROUP_ADD64:
552 case OCFS2_IOC_REFLINK:
553 if (copy_from_user(&args, (struct reflink_arguments *)arg,
556 preserve = (args.preserve != 0);
558 return ocfs2_reflink_ioctl(inode, compat_ptr(args.old_path),
559 compat_ptr(args.new_path), preserve);
561 if (copy_from_user(&info, (struct ocfs2_info __user *)arg,
562 sizeof(struct ocfs2_info)))
565 return ocfs2_info_handle(inode, &info, 1);
570 return ocfs2_ioctl(file, cmd, arg);