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 inline void __o2info_set_request_filled(struct ocfs2_info_request *req)
51 req->ir_flags |= OCFS2_INFO_FL_FILLED;
54 #define o2info_set_request_filled(a) \
55 __o2info_set_request_filled((struct ocfs2_info_request *)&(a))
57 static inline void __o2info_clear_request_filled(struct ocfs2_info_request *req)
59 req->ir_flags &= ~OCFS2_INFO_FL_FILLED;
62 #define o2info_clear_request_filled(a) \
63 __o2info_clear_request_filled((struct ocfs2_info_request *)&(a))
65 static int ocfs2_get_inode_attr(struct inode *inode, unsigned *flags)
69 status = ocfs2_inode_lock(inode, NULL, 0);
74 ocfs2_get_inode_flags(OCFS2_I(inode));
75 *flags = OCFS2_I(inode)->ip_attr;
76 ocfs2_inode_unlock(inode, 0);
82 static int ocfs2_set_inode_attr(struct inode *inode, unsigned flags,
85 struct ocfs2_inode_info *ocfs2_inode = OCFS2_I(inode);
86 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
87 handle_t *handle = NULL;
88 struct buffer_head *bh = NULL;
92 mutex_lock(&inode->i_mutex);
94 status = ocfs2_inode_lock(inode, &bh, 1);
101 if (!is_owner_or_cap(inode))
104 if (!S_ISDIR(inode->i_mode))
105 flags &= ~OCFS2_DIRSYNC_FL;
107 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
108 if (IS_ERR(handle)) {
109 status = PTR_ERR(handle);
114 oldflags = ocfs2_inode->ip_attr;
115 flags = flags & mask;
116 flags |= oldflags & ~mask;
119 * The IMMUTABLE and APPEND_ONLY flags can only be changed by
120 * the relevant capability.
123 if ((oldflags & OCFS2_IMMUTABLE_FL) || ((flags ^ oldflags) &
124 (OCFS2_APPEND_FL | OCFS2_IMMUTABLE_FL))) {
125 if (!capable(CAP_LINUX_IMMUTABLE))
129 ocfs2_inode->ip_attr = flags;
130 ocfs2_set_inode_flags(inode);
132 status = ocfs2_mark_inode_dirty(handle, inode, bh);
136 ocfs2_commit_trans(osb, handle);
138 ocfs2_inode_unlock(inode, 1);
140 mutex_unlock(&inode->i_mutex);
148 int ocfs2_info_handle_blocksize(struct inode *inode,
149 struct ocfs2_info_request __user *req)
151 int status = -EFAULT;
152 struct ocfs2_info_blocksize oib;
154 if (o2info_from_user(oib, req))
157 oib.ib_blocksize = inode->i_sb->s_blocksize;
159 o2info_set_request_filled(oib);
161 if (o2info_to_user(oib, req))
167 o2info_set_request_error(oib, req);
172 int ocfs2_info_handle_clustersize(struct inode *inode,
173 struct ocfs2_info_request __user *req)
175 int status = -EFAULT;
176 struct ocfs2_info_clustersize oic;
177 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
179 if (o2info_from_user(oic, req))
182 oic.ic_clustersize = osb->s_clustersize;
184 o2info_set_request_filled(oic);
186 if (o2info_to_user(oic, req))
192 o2info_set_request_error(oic, req);
197 int ocfs2_info_handle_maxslots(struct inode *inode,
198 struct ocfs2_info_request __user *req)
200 int status = -EFAULT;
201 struct ocfs2_info_maxslots oim;
202 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
204 if (o2info_from_user(oim, req))
207 oim.im_max_slots = osb->max_slots;
209 o2info_set_request_filled(oim);
211 if (o2info_to_user(oim, req))
217 o2info_set_request_error(oim, req);
222 int ocfs2_info_handle_label(struct inode *inode,
223 struct ocfs2_info_request __user *req)
225 int status = -EFAULT;
226 struct ocfs2_info_label oil;
227 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
229 if (o2info_from_user(oil, req))
232 memcpy(oil.il_label, osb->vol_label, OCFS2_MAX_VOL_LABEL_LEN);
234 o2info_set_request_filled(oil);
236 if (o2info_to_user(oil, req))
242 o2info_set_request_error(oil, req);
247 int ocfs2_info_handle_uuid(struct inode *inode,
248 struct ocfs2_info_request __user *req)
250 int status = -EFAULT;
251 struct ocfs2_info_uuid oiu;
252 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
254 if (o2info_from_user(oiu, req))
257 memcpy(oiu.iu_uuid_str, osb->uuid_str, OCFS2_TEXT_UUID_LEN + 1);
259 o2info_set_request_filled(oiu);
261 if (o2info_to_user(oiu, req))
267 o2info_set_request_error(oiu, req);
272 int ocfs2_info_handle_fs_features(struct inode *inode,
273 struct ocfs2_info_request __user *req)
275 int status = -EFAULT;
276 struct ocfs2_info_fs_features oif;
277 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
279 if (o2info_from_user(oif, req))
282 oif.if_compat_features = osb->s_feature_compat;
283 oif.if_incompat_features = osb->s_feature_incompat;
284 oif.if_ro_compat_features = osb->s_feature_ro_compat;
286 o2info_set_request_filled(oif);
288 if (o2info_to_user(oif, req))
294 o2info_set_request_error(oif, req);
299 int ocfs2_info_handle_journal_size(struct inode *inode,
300 struct ocfs2_info_request __user *req)
302 int status = -EFAULT;
303 struct ocfs2_info_journal_size oij;
304 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
306 if (o2info_from_user(oij, req))
309 oij.ij_journal_size = osb->journal->j_inode->i_size;
311 o2info_set_request_filled(oij);
313 if (o2info_to_user(oij, req))
319 o2info_set_request_error(oij, req);
324 int ocfs2_info_handle_unknown(struct inode *inode,
325 struct ocfs2_info_request __user *req)
327 int status = -EFAULT;
328 struct ocfs2_info_request oir;
330 if (o2info_from_user(oir, req))
333 o2info_clear_request_filled(oir);
335 if (o2info_to_user(oir, req))
341 o2info_set_request_error(oir, req);
347 * Validate and distinguish OCFS2_IOC_INFO requests.
349 * - validate the magic number.
350 * - distinguish different requests.
351 * - validate size of different requests.
353 int ocfs2_info_handle_request(struct inode *inode,
354 struct ocfs2_info_request __user *req)
356 int status = -EFAULT;
357 struct ocfs2_info_request oir;
359 if (o2info_from_user(oir, req))
363 if (oir.ir_magic != OCFS2_INFO_MAGIC)
366 switch (oir.ir_code) {
367 case OCFS2_INFO_BLOCKSIZE:
368 if (oir.ir_size == sizeof(struct ocfs2_info_blocksize))
369 status = ocfs2_info_handle_blocksize(inode, req);
371 case OCFS2_INFO_CLUSTERSIZE:
372 if (oir.ir_size == sizeof(struct ocfs2_info_clustersize))
373 status = ocfs2_info_handle_clustersize(inode, req);
375 case OCFS2_INFO_MAXSLOTS:
376 if (oir.ir_size == sizeof(struct ocfs2_info_maxslots))
377 status = ocfs2_info_handle_maxslots(inode, req);
379 case OCFS2_INFO_LABEL:
380 if (oir.ir_size == sizeof(struct ocfs2_info_label))
381 status = ocfs2_info_handle_label(inode, req);
383 case OCFS2_INFO_UUID:
384 if (oir.ir_size == sizeof(struct ocfs2_info_uuid))
385 status = ocfs2_info_handle_uuid(inode, req);
387 case OCFS2_INFO_FS_FEATURES:
388 if (oir.ir_size == sizeof(struct ocfs2_info_fs_features))
389 status = ocfs2_info_handle_fs_features(inode, req);
391 case OCFS2_INFO_JOURNAL_SIZE:
392 if (oir.ir_size == sizeof(struct ocfs2_info_journal_size))
393 status = ocfs2_info_handle_journal_size(inode, req);
396 status = ocfs2_info_handle_unknown(inode, req);
404 int ocfs2_get_request_ptr(struct ocfs2_info *info, int idx,
405 u64 *req_addr, int compat_flag)
407 int status = -EFAULT;
408 u64 __user *bp = NULL;
413 * pointer bp stores the base address of a pointers array,
414 * which collects all addresses of separate request.
416 bp = (u64 __user *)(unsigned long)compat_ptr(info->oi_requests);
421 bp = (u64 __user *)(unsigned long)(info->oi_requests);
423 if (o2info_from_user(*req_addr, bp + idx))
432 * OCFS2_IOC_INFO handles an array of requests passed from userspace.
434 * ocfs2_info_handle() recevies a large info aggregation, grab and
435 * validate the request count from header, then break it into small
436 * pieces, later specific handlers can handle them one by one.
438 * Idea here is to make each separate request small enough to ensure
439 * a better backward&forward compatibility, since a small piece of
440 * request will be less likely to be broken if disk layout get changed.
442 int ocfs2_info_handle(struct inode *inode, struct ocfs2_info *info,
447 struct ocfs2_info_request __user *reqp;
449 if ((info->oi_count > OCFS2_INFO_MAX_REQUEST) ||
450 (!info->oi_requests)) {
455 for (i = 0; i < info->oi_count; i++) {
457 status = ocfs2_get_request_ptr(info, i, &req_addr, compat_flag);
461 reqp = (struct ocfs2_info_request *)(unsigned long)req_addr;
467 status = ocfs2_info_handle_request(inode, reqp);
476 long ocfs2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
478 struct inode *inode = filp->f_path.dentry->d_inode;
482 struct ocfs2_space_resv sr;
483 struct ocfs2_new_group_input input;
484 struct reflink_arguments args;
485 const char *old_path, *new_path;
487 struct ocfs2_info info;
490 case OCFS2_IOC_GETFLAGS:
491 status = ocfs2_get_inode_attr(inode, &flags);
495 flags &= OCFS2_FL_VISIBLE;
496 return put_user(flags, (int __user *) arg);
497 case OCFS2_IOC_SETFLAGS:
498 if (get_user(flags, (int __user *) arg))
501 status = mnt_want_write(filp->f_path.mnt);
504 status = ocfs2_set_inode_attr(inode, flags,
505 OCFS2_FL_MODIFIABLE);
506 mnt_drop_write(filp->f_path.mnt);
508 case OCFS2_IOC_RESVSP:
509 case OCFS2_IOC_RESVSP64:
510 case OCFS2_IOC_UNRESVSP:
511 case OCFS2_IOC_UNRESVSP64:
512 if (copy_from_user(&sr, (int __user *) arg, sizeof(sr)))
515 return ocfs2_change_file_space(filp, cmd, &sr);
516 case OCFS2_IOC_GROUP_EXTEND:
517 if (!capable(CAP_SYS_RESOURCE))
520 if (get_user(new_clusters, (int __user *)arg))
523 return ocfs2_group_extend(inode, new_clusters);
524 case OCFS2_IOC_GROUP_ADD:
525 case OCFS2_IOC_GROUP_ADD64:
526 if (!capable(CAP_SYS_RESOURCE))
529 if (copy_from_user(&input, (int __user *) arg, sizeof(input)))
532 return ocfs2_group_add(inode, &input);
533 case OCFS2_IOC_REFLINK:
534 if (copy_from_user(&args, (struct reflink_arguments *)arg,
537 old_path = (const char *)(unsigned long)args.old_path;
538 new_path = (const char *)(unsigned long)args.new_path;
539 preserve = (args.preserve != 0);
541 return ocfs2_reflink_ioctl(inode, old_path, new_path, preserve);
543 if (copy_from_user(&info, (struct ocfs2_info __user *)arg,
544 sizeof(struct ocfs2_info)))
547 return ocfs2_info_handle(inode, &info, 0);
554 long ocfs2_compat_ioctl(struct file *file, unsigned cmd, unsigned long arg)
557 struct reflink_arguments args;
558 struct inode *inode = file->f_path.dentry->d_inode;
559 struct ocfs2_info info;
562 case OCFS2_IOC32_GETFLAGS:
563 cmd = OCFS2_IOC_GETFLAGS;
565 case OCFS2_IOC32_SETFLAGS:
566 cmd = OCFS2_IOC_SETFLAGS;
568 case OCFS2_IOC_RESVSP:
569 case OCFS2_IOC_RESVSP64:
570 case OCFS2_IOC_UNRESVSP:
571 case OCFS2_IOC_UNRESVSP64:
572 case OCFS2_IOC_GROUP_EXTEND:
573 case OCFS2_IOC_GROUP_ADD:
574 case OCFS2_IOC_GROUP_ADD64:
576 case OCFS2_IOC_REFLINK:
577 if (copy_from_user(&args, (struct reflink_arguments *)arg,
580 preserve = (args.preserve != 0);
582 return ocfs2_reflink_ioctl(inode, compat_ptr(args.old_path),
583 compat_ptr(args.new_path), preserve);
585 if (copy_from_user(&info, (struct ocfs2_info __user *)arg,
586 sizeof(struct ocfs2_info)))
589 return ocfs2_info_handle(inode, &info, 1);
594 return ocfs2_ioctl(file, cmd, arg);