Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/hfsplus/ioctl.c |
| 3 | * |
| 4 | * Copyright (C) 2003 |
| 5 | * Ethan Benson <erbenson@alaska.net> |
| 6 | * partially derived from linux/fs/ext2/ioctl.c |
| 7 | * Copyright (C) 1993, 1994, 1995 |
| 8 | * Remy Card (card@masi.ibp.fr) |
| 9 | * Laboratoire MASI - Institut Blaise Pascal |
| 10 | * Universite Pierre et Marie Curie (Paris VI) |
| 11 | * |
| 12 | * hfsplus ioctls |
| 13 | */ |
| 14 | |
Randy Dunlap | 16f7e0f | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 15 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/fs.h> |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 17 | #include <linux/mount.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/sched.h> |
| 19 | #include <linux/xattr.h> |
| 20 | #include <asm/uaccess.h> |
| 21 | #include "hfsplus_fs.h" |
| 22 | |
Matthew Garrett | a051f71 | 2012-02-06 15:14:40 -0500 | [diff] [blame] | 23 | /* |
| 24 | * "Blessing" an HFS+ filesystem writes metadata to the superblock informing |
| 25 | * the platform firmware which file to boot from |
| 26 | */ |
| 27 | static int hfsplus_ioctl_bless(struct file *file, int __user *user_flags) |
| 28 | { |
| 29 | struct dentry *dentry = file->f_path.dentry; |
| 30 | struct inode *inode = dentry->d_inode; |
| 31 | struct hfsplus_sb_info *sbi = HFSPLUS_SB(inode->i_sb); |
| 32 | struct hfsplus_vh *vh = sbi->s_vhdr; |
| 33 | struct hfsplus_vh *bvh = sbi->s_backup_vhdr; |
Matthew Garrett | 7dea9665 | 2012-06-17 17:05:25 -0400 | [diff] [blame] | 34 | u32 cnid = (unsigned long)dentry->d_fsdata; |
Matthew Garrett | a051f71 | 2012-02-06 15:14:40 -0500 | [diff] [blame] | 35 | |
| 36 | if (!capable(CAP_SYS_ADMIN)) |
| 37 | return -EPERM; |
| 38 | |
| 39 | mutex_lock(&sbi->vh_mutex); |
| 40 | |
| 41 | /* Directory containing the bootable system */ |
| 42 | vh->finder_info[0] = bvh->finder_info[0] = |
| 43 | cpu_to_be32(parent_ino(dentry)); |
| 44 | |
Matthew Garrett | 7dea9665 | 2012-06-17 17:05:25 -0400 | [diff] [blame] | 45 | /* |
| 46 | * Bootloader. Just using the inode here breaks in the case of |
| 47 | * hard links - the firmware wants the ID of the hard link file, |
| 48 | * but the inode points at the indirect inode |
| 49 | */ |
| 50 | vh->finder_info[1] = bvh->finder_info[1] = cpu_to_be32(cnid); |
Matthew Garrett | a051f71 | 2012-02-06 15:14:40 -0500 | [diff] [blame] | 51 | |
| 52 | /* Per spec, the OS X system folder - same as finder_info[0] here */ |
| 53 | vh->finder_info[5] = bvh->finder_info[5] = |
| 54 | cpu_to_be32(parent_ino(dentry)); |
| 55 | |
| 56 | mutex_unlock(&sbi->vh_mutex); |
| 57 | return 0; |
| 58 | } |
| 59 | |
Christoph Hellwig | 9474456 | 2010-10-01 05:41:31 +0200 | [diff] [blame] | 60 | static int hfsplus_ioctl_getflags(struct file *file, int __user *user_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | { |
Christoph Hellwig | 9474456 | 2010-10-01 05:41:31 +0200 | [diff] [blame] | 62 | struct inode *inode = file->f_path.dentry->d_inode; |
Christoph Hellwig | 6af502d | 2010-10-01 05:43:31 +0200 | [diff] [blame] | 63 | struct hfsplus_inode_info *hip = HFSPLUS_I(inode); |
Christoph Hellwig | 9474456 | 2010-10-01 05:41:31 +0200 | [diff] [blame] | 64 | unsigned int flags = 0; |
| 65 | |
Christoph Hellwig | 722c55d | 2010-10-14 09:54:33 -0400 | [diff] [blame] | 66 | if (inode->i_flags & S_IMMUTABLE) |
Christoph Hellwig | 9474456 | 2010-10-01 05:41:31 +0200 | [diff] [blame] | 67 | flags |= FS_IMMUTABLE_FL; |
Anton Salikhmetov | 596276c | 2010-12-16 14:44:51 +0200 | [diff] [blame] | 68 | if (inode->i_flags & S_APPEND) |
Christoph Hellwig | 9474456 | 2010-10-01 05:41:31 +0200 | [diff] [blame] | 69 | flags |= FS_APPEND_FL; |
Christoph Hellwig | 6af502d | 2010-10-01 05:43:31 +0200 | [diff] [blame] | 70 | if (hip->userflags & HFSPLUS_FLG_NODUMP) |
Christoph Hellwig | 9474456 | 2010-10-01 05:41:31 +0200 | [diff] [blame] | 71 | flags |= FS_NODUMP_FL; |
| 72 | |
| 73 | return put_user(flags, user_flags); |
| 74 | } |
| 75 | |
| 76 | static int hfsplus_ioctl_setflags(struct file *file, int __user *user_flags) |
| 77 | { |
| 78 | struct inode *inode = file->f_path.dentry->d_inode; |
Christoph Hellwig | 6af502d | 2010-10-01 05:43:31 +0200 | [diff] [blame] | 79 | struct hfsplus_inode_info *hip = HFSPLUS_I(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | unsigned int flags; |
Christoph Hellwig | 9474456 | 2010-10-01 05:41:31 +0200 | [diff] [blame] | 81 | int err = 0; |
| 82 | |
Al Viro | a561be7 | 2011-11-23 11:57:51 -0500 | [diff] [blame] | 83 | err = mnt_want_write_file(file); |
Christoph Hellwig | 9474456 | 2010-10-01 05:41:31 +0200 | [diff] [blame] | 84 | if (err) |
Christoph Hellwig | 6333816 | 2010-10-01 05:41:35 +0200 | [diff] [blame] | 85 | goto out; |
Christoph Hellwig | 9474456 | 2010-10-01 05:41:31 +0200 | [diff] [blame] | 86 | |
Serge E. Hallyn | 2e14967 | 2011-03-23 16:43:26 -0700 | [diff] [blame] | 87 | if (!inode_owner_or_capable(inode)) { |
Christoph Hellwig | 9474456 | 2010-10-01 05:41:31 +0200 | [diff] [blame] | 88 | err = -EACCES; |
| 89 | goto out_drop_write; |
| 90 | } |
| 91 | |
| 92 | if (get_user(flags, user_flags)) { |
| 93 | err = -EFAULT; |
| 94 | goto out_drop_write; |
| 95 | } |
| 96 | |
Christoph Hellwig | 6333816 | 2010-10-01 05:41:35 +0200 | [diff] [blame] | 97 | mutex_lock(&inode->i_mutex); |
| 98 | |
Christoph Hellwig | 722c55d | 2010-10-14 09:54:33 -0400 | [diff] [blame] | 99 | if ((flags & (FS_IMMUTABLE_FL|FS_APPEND_FL)) || |
| 100 | inode->i_flags & (S_IMMUTABLE|S_APPEND)) { |
Christoph Hellwig | 9474456 | 2010-10-01 05:41:31 +0200 | [diff] [blame] | 101 | if (!capable(CAP_LINUX_IMMUTABLE)) { |
| 102 | err = -EPERM; |
Christoph Hellwig | 6333816 | 2010-10-01 05:41:35 +0200 | [diff] [blame] | 103 | goto out_unlock_inode; |
Christoph Hellwig | 9474456 | 2010-10-01 05:41:31 +0200 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | |
| 107 | /* don't silently ignore unsupported ext2 flags */ |
| 108 | if (flags & ~(FS_IMMUTABLE_FL|FS_APPEND_FL|FS_NODUMP_FL)) { |
| 109 | err = -EOPNOTSUPP; |
Christoph Hellwig | 6333816 | 2010-10-01 05:41:35 +0200 | [diff] [blame] | 110 | goto out_unlock_inode; |
Christoph Hellwig | 9474456 | 2010-10-01 05:41:31 +0200 | [diff] [blame] | 111 | } |
Christoph Hellwig | 722c55d | 2010-10-14 09:54:33 -0400 | [diff] [blame] | 112 | |
| 113 | if (flags & FS_IMMUTABLE_FL) |
Christoph Hellwig | 9474456 | 2010-10-01 05:41:31 +0200 | [diff] [blame] | 114 | inode->i_flags |= S_IMMUTABLE; |
Christoph Hellwig | 722c55d | 2010-10-14 09:54:33 -0400 | [diff] [blame] | 115 | else |
Christoph Hellwig | 9474456 | 2010-10-01 05:41:31 +0200 | [diff] [blame] | 116 | inode->i_flags &= ~S_IMMUTABLE; |
Christoph Hellwig | 722c55d | 2010-10-14 09:54:33 -0400 | [diff] [blame] | 117 | |
| 118 | if (flags & FS_APPEND_FL) |
Christoph Hellwig | 9474456 | 2010-10-01 05:41:31 +0200 | [diff] [blame] | 119 | inode->i_flags |= S_APPEND; |
Christoph Hellwig | 722c55d | 2010-10-14 09:54:33 -0400 | [diff] [blame] | 120 | else |
Christoph Hellwig | 9474456 | 2010-10-01 05:41:31 +0200 | [diff] [blame] | 121 | inode->i_flags &= ~S_APPEND; |
Christoph Hellwig | 722c55d | 2010-10-14 09:54:33 -0400 | [diff] [blame] | 122 | |
Christoph Hellwig | 9474456 | 2010-10-01 05:41:31 +0200 | [diff] [blame] | 123 | if (flags & FS_NODUMP_FL) |
Christoph Hellwig | 6af502d | 2010-10-01 05:43:31 +0200 | [diff] [blame] | 124 | hip->userflags |= HFSPLUS_FLG_NODUMP; |
Christoph Hellwig | 9474456 | 2010-10-01 05:41:31 +0200 | [diff] [blame] | 125 | else |
Christoph Hellwig | 6af502d | 2010-10-01 05:43:31 +0200 | [diff] [blame] | 126 | hip->userflags &= ~HFSPLUS_FLG_NODUMP; |
Christoph Hellwig | 9474456 | 2010-10-01 05:41:31 +0200 | [diff] [blame] | 127 | |
| 128 | inode->i_ctime = CURRENT_TIME_SEC; |
| 129 | mark_inode_dirty(inode); |
| 130 | |
Christoph Hellwig | 6333816 | 2010-10-01 05:41:35 +0200 | [diff] [blame] | 131 | out_unlock_inode: |
Dan Carpenter | e50fb58 | 2010-10-25 20:39:07 +0200 | [diff] [blame] | 132 | mutex_unlock(&inode->i_mutex); |
Christoph Hellwig | 9474456 | 2010-10-01 05:41:31 +0200 | [diff] [blame] | 133 | out_drop_write: |
Al Viro | 2a79f17 | 2011-12-09 08:06:57 -0500 | [diff] [blame] | 134 | mnt_drop_write_file(file); |
Christoph Hellwig | 6333816 | 2010-10-01 05:41:35 +0200 | [diff] [blame] | 135 | out: |
Christoph Hellwig | 9474456 | 2010-10-01 05:41:31 +0200 | [diff] [blame] | 136 | return err; |
| 137 | } |
| 138 | |
| 139 | long hfsplus_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 140 | { |
| 141 | void __user *argp = (void __user *)arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | |
| 143 | switch (cmd) { |
| 144 | case HFSPLUS_IOC_EXT2_GETFLAGS: |
Christoph Hellwig | 9474456 | 2010-10-01 05:41:31 +0200 | [diff] [blame] | 145 | return hfsplus_ioctl_getflags(file, argp); |
| 146 | case HFSPLUS_IOC_EXT2_SETFLAGS: |
| 147 | return hfsplus_ioctl_setflags(file, argp); |
Matthew Garrett | a051f71 | 2012-02-06 15:14:40 -0500 | [diff] [blame] | 148 | case HFSPLUS_IOC_BLESS: |
| 149 | return hfsplus_ioctl_bless(file, argp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | return -ENOTTY; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | int hfsplus_setxattr(struct dentry *dentry, const char *name, |
| 156 | const void *value, size_t size, int flags) |
| 157 | { |
| 158 | struct inode *inode = dentry->d_inode; |
| 159 | struct hfs_find_data fd; |
| 160 | hfsplus_cat_entry entry; |
| 161 | struct hfsplus_cat_file *file; |
| 162 | int res; |
| 163 | |
| 164 | if (!S_ISREG(inode->i_mode) || HFSPLUS_IS_RSRC(inode)) |
| 165 | return -EOPNOTSUPP; |
| 166 | |
Christoph Hellwig | dd73a01 | 2010-10-01 05:42:59 +0200 | [diff] [blame] | 167 | res = hfs_find_init(HFSPLUS_SB(inode->i_sb)->cat_tree, &fd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | if (res) |
| 169 | return res; |
| 170 | res = hfsplus_find_cat(inode->i_sb, inode->i_ino, &fd); |
| 171 | if (res) |
| 172 | goto out; |
| 173 | hfs_bnode_read(fd.bnode, &entry, fd.entryoffset, |
| 174 | sizeof(struct hfsplus_cat_file)); |
| 175 | file = &entry.file; |
| 176 | |
| 177 | if (!strcmp(name, "hfs.type")) { |
| 178 | if (size == 4) |
| 179 | memcpy(&file->user_info.fdType, value, 4); |
| 180 | else |
| 181 | res = -ERANGE; |
| 182 | } else if (!strcmp(name, "hfs.creator")) { |
| 183 | if (size == 4) |
| 184 | memcpy(&file->user_info.fdCreator, value, 4); |
| 185 | else |
| 186 | res = -ERANGE; |
| 187 | } else |
| 188 | res = -EOPNOTSUPP; |
Christoph Hellwig | e349470 | 2010-11-23 14:38:15 +0100 | [diff] [blame] | 189 | if (!res) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | hfs_bnode_write(fd.bnode, &entry, fd.entryoffset, |
| 191 | sizeof(struct hfsplus_cat_file)); |
Christoph Hellwig | e349470 | 2010-11-23 14:38:15 +0100 | [diff] [blame] | 192 | hfsplus_mark_inode_dirty(inode, HFSPLUS_I_CAT_DIRTY); |
| 193 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | out: |
| 195 | hfs_find_exit(&fd); |
| 196 | return res; |
| 197 | } |
| 198 | |
| 199 | ssize_t hfsplus_getxattr(struct dentry *dentry, const char *name, |
| 200 | void *value, size_t size) |
| 201 | { |
| 202 | struct inode *inode = dentry->d_inode; |
| 203 | struct hfs_find_data fd; |
| 204 | hfsplus_cat_entry entry; |
| 205 | struct hfsplus_cat_file *file; |
| 206 | ssize_t res = 0; |
| 207 | |
| 208 | if (!S_ISREG(inode->i_mode) || HFSPLUS_IS_RSRC(inode)) |
| 209 | return -EOPNOTSUPP; |
| 210 | |
| 211 | if (size) { |
Christoph Hellwig | dd73a01 | 2010-10-01 05:42:59 +0200 | [diff] [blame] | 212 | res = hfs_find_init(HFSPLUS_SB(inode->i_sb)->cat_tree, &fd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | if (res) |
| 214 | return res; |
| 215 | res = hfsplus_find_cat(inode->i_sb, inode->i_ino, &fd); |
| 216 | if (res) |
| 217 | goto out; |
| 218 | hfs_bnode_read(fd.bnode, &entry, fd.entryoffset, |
| 219 | sizeof(struct hfsplus_cat_file)); |
| 220 | } |
| 221 | file = &entry.file; |
| 222 | |
| 223 | if (!strcmp(name, "hfs.type")) { |
| 224 | if (size >= 4) { |
| 225 | memcpy(value, &file->user_info.fdType, 4); |
| 226 | res = 4; |
| 227 | } else |
| 228 | res = size ? -ERANGE : 4; |
| 229 | } else if (!strcmp(name, "hfs.creator")) { |
| 230 | if (size >= 4) { |
| 231 | memcpy(value, &file->user_info.fdCreator, 4); |
| 232 | res = 4; |
| 233 | } else |
| 234 | res = size ? -ERANGE : 4; |
| 235 | } else |
Christoph Hellwig | 46bf36e | 2010-10-15 05:45:00 -0700 | [diff] [blame] | 236 | res = -EOPNOTSUPP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | out: |
| 238 | if (size) |
| 239 | hfs_find_exit(&fd); |
| 240 | return res; |
| 241 | } |
| 242 | |
| 243 | #define HFSPLUS_ATTRLIST_SIZE (sizeof("hfs.creator")+sizeof("hfs.type")) |
| 244 | |
| 245 | ssize_t hfsplus_listxattr(struct dentry *dentry, char *buffer, size_t size) |
| 246 | { |
| 247 | struct inode *inode = dentry->d_inode; |
| 248 | |
| 249 | if (!S_ISREG(inode->i_mode) || HFSPLUS_IS_RSRC(inode)) |
| 250 | return -EOPNOTSUPP; |
| 251 | |
| 252 | if (!buffer || !size) |
| 253 | return HFSPLUS_ATTRLIST_SIZE; |
| 254 | if (size < HFSPLUS_ATTRLIST_SIZE) |
| 255 | return -ERANGE; |
| 256 | strcpy(buffer, "hfs.type"); |
| 257 | strcpy(buffer + sizeof("hfs.type"), "hfs.creator"); |
| 258 | |
| 259 | return HFSPLUS_ATTRLIST_SIZE; |
| 260 | } |