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" |
| 17 | #include "inode.h" |
| 18 | #include "journal.h" |
| 19 | |
| 20 | #include "ocfs2_fs.h" |
Adrian Bunk | 2d56251 | 2006-07-10 01:32:51 +0200 | [diff] [blame] | 21 | #include "ioctl.h" |
| 22 | |
Herbert Poetzl | ca4d147 | 2006-07-03 17:27:12 -0700 | [diff] [blame] | 23 | #include <linux/ext2_fs.h> |
| 24 | |
| 25 | static int ocfs2_get_inode_attr(struct inode *inode, unsigned *flags) |
| 26 | { |
| 27 | int status; |
| 28 | |
Mark Fasheh | 4bcec18 | 2006-10-09 16:02:40 -0700 | [diff] [blame] | 29 | status = ocfs2_meta_lock(inode, NULL, 0); |
Herbert Poetzl | ca4d147 | 2006-07-03 17:27:12 -0700 | [diff] [blame] | 30 | if (status < 0) { |
| 31 | mlog_errno(status); |
| 32 | return status; |
| 33 | } |
| 34 | *flags = OCFS2_I(inode)->ip_attr; |
| 35 | ocfs2_meta_unlock(inode, 0); |
| 36 | |
| 37 | mlog_exit(status); |
| 38 | return status; |
| 39 | } |
| 40 | |
| 41 | static int ocfs2_set_inode_attr(struct inode *inode, unsigned flags, |
| 42 | unsigned mask) |
| 43 | { |
| 44 | struct ocfs2_inode_info *ocfs2_inode = OCFS2_I(inode); |
| 45 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 46 | handle_t *handle = NULL; |
Herbert Poetzl | ca4d147 | 2006-07-03 17:27:12 -0700 | [diff] [blame] | 47 | struct buffer_head *bh = NULL; |
| 48 | unsigned oldflags; |
| 49 | int status; |
| 50 | |
| 51 | mutex_lock(&inode->i_mutex); |
| 52 | |
Mark Fasheh | 4bcec18 | 2006-10-09 16:02:40 -0700 | [diff] [blame] | 53 | status = ocfs2_meta_lock(inode, &bh, 1); |
Herbert Poetzl | ca4d147 | 2006-07-03 17:27:12 -0700 | [diff] [blame] | 54 | if (status < 0) { |
| 55 | mlog_errno(status); |
| 56 | goto bail; |
| 57 | } |
| 58 | |
| 59 | status = -EROFS; |
| 60 | if (IS_RDONLY(inode)) |
| 61 | goto bail_unlock; |
| 62 | |
| 63 | status = -EACCES; |
| 64 | if ((current->fsuid != inode->i_uid) && !capable(CAP_FOWNER)) |
| 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: |
| 101 | ocfs2_meta_unlock(inode, 1); |
| 102 | bail: |
| 103 | mutex_unlock(&inode->i_mutex); |
| 104 | |
| 105 | if (bh) |
| 106 | brelse(bh); |
| 107 | |
| 108 | mlog_exit(status); |
| 109 | return status; |
| 110 | } |
| 111 | |
| 112 | int ocfs2_ioctl(struct inode * inode, struct file * filp, |
| 113 | unsigned int cmd, unsigned long arg) |
| 114 | { |
| 115 | unsigned int flags; |
| 116 | int status; |
| 117 | |
| 118 | switch (cmd) { |
| 119 | case OCFS2_IOC_GETFLAGS: |
| 120 | status = ocfs2_get_inode_attr(inode, &flags); |
| 121 | if (status < 0) |
| 122 | return status; |
| 123 | |
| 124 | flags &= OCFS2_FL_VISIBLE; |
| 125 | return put_user(flags, (int __user *) arg); |
| 126 | case OCFS2_IOC_SETFLAGS: |
| 127 | if (get_user(flags, (int __user *) arg)) |
| 128 | return -EFAULT; |
| 129 | |
| 130 | return ocfs2_set_inode_attr(inode, flags, |
| 131 | OCFS2_FL_MODIFIABLE); |
| 132 | default: |
| 133 | return -ENOTTY; |
| 134 | } |
| 135 | } |
| 136 | |