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> |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 19 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include "hfsplus_fs.h" |
| 21 | |
Matthew Garrett | a051f71 | 2012-02-06 15:14:40 -0500 | [diff] [blame] | 22 | /* |
| 23 | * "Blessing" an HFS+ filesystem writes metadata to the superblock informing |
| 24 | * the platform firmware which file to boot from |
| 25 | */ |
| 26 | static int hfsplus_ioctl_bless(struct file *file, int __user *user_flags) |
| 27 | { |
| 28 | struct dentry *dentry = file->f_path.dentry; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 29 | struct inode *inode = d_inode(dentry); |
Matthew Garrett | a051f71 | 2012-02-06 15:14:40 -0500 | [diff] [blame] | 30 | struct hfsplus_sb_info *sbi = HFSPLUS_SB(inode->i_sb); |
| 31 | struct hfsplus_vh *vh = sbi->s_vhdr; |
| 32 | struct hfsplus_vh *bvh = sbi->s_backup_vhdr; |
Matthew Garrett | 7dea9665 | 2012-06-17 17:05:25 -0400 | [diff] [blame] | 33 | u32 cnid = (unsigned long)dentry->d_fsdata; |
Matthew Garrett | a051f71 | 2012-02-06 15:14:40 -0500 | [diff] [blame] | 34 | |
| 35 | if (!capable(CAP_SYS_ADMIN)) |
| 36 | return -EPERM; |
| 37 | |
| 38 | mutex_lock(&sbi->vh_mutex); |
| 39 | |
| 40 | /* Directory containing the bootable system */ |
| 41 | vh->finder_info[0] = bvh->finder_info[0] = |
| 42 | cpu_to_be32(parent_ino(dentry)); |
| 43 | |
Matthew Garrett | 7dea9665 | 2012-06-17 17:05:25 -0400 | [diff] [blame] | 44 | /* |
| 45 | * Bootloader. Just using the inode here breaks in the case of |
| 46 | * hard links - the firmware wants the ID of the hard link file, |
| 47 | * but the inode points at the indirect inode |
| 48 | */ |
| 49 | vh->finder_info[1] = bvh->finder_info[1] = cpu_to_be32(cnid); |
Matthew Garrett | a051f71 | 2012-02-06 15:14:40 -0500 | [diff] [blame] | 50 | |
| 51 | /* Per spec, the OS X system folder - same as finder_info[0] here */ |
| 52 | vh->finder_info[5] = bvh->finder_info[5] = |
| 53 | cpu_to_be32(parent_ino(dentry)); |
| 54 | |
| 55 | mutex_unlock(&sbi->vh_mutex); |
| 56 | return 0; |
| 57 | } |
| 58 | |
Christoph Hellwig | 9474456 | 2010-10-01 05:41:31 +0200 | [diff] [blame] | 59 | static int hfsplus_ioctl_getflags(struct file *file, int __user *user_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 61 | struct inode *inode = file_inode(file); |
Christoph Hellwig | 6af502d | 2010-10-01 05:43:31 +0200 | [diff] [blame] | 62 | struct hfsplus_inode_info *hip = HFSPLUS_I(inode); |
Christoph Hellwig | 9474456 | 2010-10-01 05:41:31 +0200 | [diff] [blame] | 63 | unsigned int flags = 0; |
| 64 | |
Christoph Hellwig | 722c55d | 2010-10-14 09:54:33 -0400 | [diff] [blame] | 65 | if (inode->i_flags & S_IMMUTABLE) |
Christoph Hellwig | 9474456 | 2010-10-01 05:41:31 +0200 | [diff] [blame] | 66 | flags |= FS_IMMUTABLE_FL; |
Anton Salikhmetov | 596276c | 2010-12-16 14:44:51 +0200 | [diff] [blame] | 67 | if (inode->i_flags & S_APPEND) |
Christoph Hellwig | 9474456 | 2010-10-01 05:41:31 +0200 | [diff] [blame] | 68 | flags |= FS_APPEND_FL; |
Christoph Hellwig | 6af502d | 2010-10-01 05:43:31 +0200 | [diff] [blame] | 69 | if (hip->userflags & HFSPLUS_FLG_NODUMP) |
Christoph Hellwig | 9474456 | 2010-10-01 05:41:31 +0200 | [diff] [blame] | 70 | flags |= FS_NODUMP_FL; |
| 71 | |
| 72 | return put_user(flags, user_flags); |
| 73 | } |
| 74 | |
| 75 | static int hfsplus_ioctl_setflags(struct file *file, int __user *user_flags) |
| 76 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 77 | struct inode *inode = file_inode(file); |
Christoph Hellwig | 6af502d | 2010-10-01 05:43:31 +0200 | [diff] [blame] | 78 | struct hfsplus_inode_info *hip = HFSPLUS_I(inode); |
Fabian Frederick | 73d28d57 | 2015-04-16 12:47:04 -0700 | [diff] [blame] | 79 | unsigned int flags, new_fl = 0; |
Christoph Hellwig | 9474456 | 2010-10-01 05:41:31 +0200 | [diff] [blame] | 80 | int err = 0; |
| 81 | |
Al Viro | a561be7 | 2011-11-23 11:57:51 -0500 | [diff] [blame] | 82 | err = mnt_want_write_file(file); |
Christoph Hellwig | 9474456 | 2010-10-01 05:41:31 +0200 | [diff] [blame] | 83 | if (err) |
Christoph Hellwig | 6333816 | 2010-10-01 05:41:35 +0200 | [diff] [blame] | 84 | goto out; |
Christoph Hellwig | 9474456 | 2010-10-01 05:41:31 +0200 | [diff] [blame] | 85 | |
Serge E. Hallyn | 2e14967 | 2011-03-23 16:43:26 -0700 | [diff] [blame] | 86 | if (!inode_owner_or_capable(inode)) { |
Christoph Hellwig | 9474456 | 2010-10-01 05:41:31 +0200 | [diff] [blame] | 87 | err = -EACCES; |
| 88 | goto out_drop_write; |
| 89 | } |
| 90 | |
| 91 | if (get_user(flags, user_flags)) { |
| 92 | err = -EFAULT; |
| 93 | goto out_drop_write; |
| 94 | } |
| 95 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 96 | inode_lock(inode); |
Christoph Hellwig | 6333816 | 2010-10-01 05:41:35 +0200 | [diff] [blame] | 97 | |
Christoph Hellwig | 722c55d | 2010-10-14 09:54:33 -0400 | [diff] [blame] | 98 | if ((flags & (FS_IMMUTABLE_FL|FS_APPEND_FL)) || |
| 99 | inode->i_flags & (S_IMMUTABLE|S_APPEND)) { |
Christoph Hellwig | 9474456 | 2010-10-01 05:41:31 +0200 | [diff] [blame] | 100 | if (!capable(CAP_LINUX_IMMUTABLE)) { |
| 101 | err = -EPERM; |
Christoph Hellwig | 6333816 | 2010-10-01 05:41:35 +0200 | [diff] [blame] | 102 | goto out_unlock_inode; |
Christoph Hellwig | 9474456 | 2010-10-01 05:41:31 +0200 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | |
| 106 | /* don't silently ignore unsupported ext2 flags */ |
| 107 | if (flags & ~(FS_IMMUTABLE_FL|FS_APPEND_FL|FS_NODUMP_FL)) { |
| 108 | err = -EOPNOTSUPP; |
Christoph Hellwig | 6333816 | 2010-10-01 05:41:35 +0200 | [diff] [blame] | 109 | goto out_unlock_inode; |
Christoph Hellwig | 9474456 | 2010-10-01 05:41:31 +0200 | [diff] [blame] | 110 | } |
Christoph Hellwig | 722c55d | 2010-10-14 09:54:33 -0400 | [diff] [blame] | 111 | |
| 112 | if (flags & FS_IMMUTABLE_FL) |
Fabian Frederick | 73d28d57 | 2015-04-16 12:47:04 -0700 | [diff] [blame] | 113 | new_fl |= S_IMMUTABLE; |
Christoph Hellwig | 722c55d | 2010-10-14 09:54:33 -0400 | [diff] [blame] | 114 | |
| 115 | if (flags & FS_APPEND_FL) |
Fabian Frederick | 73d28d57 | 2015-04-16 12:47:04 -0700 | [diff] [blame] | 116 | new_fl |= S_APPEND; |
| 117 | |
| 118 | inode_set_flags(inode, new_fl, S_IMMUTABLE | S_APPEND); |
Christoph Hellwig | 722c55d | 2010-10-14 09:54:33 -0400 | [diff] [blame] | 119 | |
Christoph Hellwig | 9474456 | 2010-10-01 05:41:31 +0200 | [diff] [blame] | 120 | if (flags & FS_NODUMP_FL) |
Christoph Hellwig | 6af502d | 2010-10-01 05:43:31 +0200 | [diff] [blame] | 121 | hip->userflags |= HFSPLUS_FLG_NODUMP; |
Christoph Hellwig | 9474456 | 2010-10-01 05:41:31 +0200 | [diff] [blame] | 122 | else |
Christoph Hellwig | 6af502d | 2010-10-01 05:43:31 +0200 | [diff] [blame] | 123 | hip->userflags &= ~HFSPLUS_FLG_NODUMP; |
Christoph Hellwig | 9474456 | 2010-10-01 05:41:31 +0200 | [diff] [blame] | 124 | |
Deepa Dinamani | 02027d4 | 2016-09-14 07:48:05 -0700 | [diff] [blame] | 125 | inode->i_ctime = current_time(inode); |
Christoph Hellwig | 9474456 | 2010-10-01 05:41:31 +0200 | [diff] [blame] | 126 | mark_inode_dirty(inode); |
| 127 | |
Christoph Hellwig | 6333816 | 2010-10-01 05:41:35 +0200 | [diff] [blame] | 128 | out_unlock_inode: |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 129 | inode_unlock(inode); |
Christoph Hellwig | 9474456 | 2010-10-01 05:41:31 +0200 | [diff] [blame] | 130 | out_drop_write: |
Al Viro | 2a79f17 | 2011-12-09 08:06:57 -0500 | [diff] [blame] | 131 | mnt_drop_write_file(file); |
Christoph Hellwig | 6333816 | 2010-10-01 05:41:35 +0200 | [diff] [blame] | 132 | out: |
Christoph Hellwig | 9474456 | 2010-10-01 05:41:31 +0200 | [diff] [blame] | 133 | return err; |
| 134 | } |
| 135 | |
| 136 | long hfsplus_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 137 | { |
| 138 | void __user *argp = (void __user *)arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | |
| 140 | switch (cmd) { |
| 141 | case HFSPLUS_IOC_EXT2_GETFLAGS: |
Christoph Hellwig | 9474456 | 2010-10-01 05:41:31 +0200 | [diff] [blame] | 142 | return hfsplus_ioctl_getflags(file, argp); |
| 143 | case HFSPLUS_IOC_EXT2_SETFLAGS: |
| 144 | return hfsplus_ioctl_setflags(file, argp); |
Matthew Garrett | a051f71 | 2012-02-06 15:14:40 -0500 | [diff] [blame] | 145 | case HFSPLUS_IOC_BLESS: |
| 146 | return hfsplus_ioctl_bless(file, argp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | return -ENOTTY; |
| 149 | } |
| 150 | } |