blob: 99627f8a0a1829b1b8007d9523aee26775dd9329 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Dunlap16f7e0f2006-01-11 12:17:46 -080015#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/fs.h>
Dave Hansen42a74f22008-02-15 14:37:46 -080017#include <linux/mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <asm/uaccess.h>
20#include "hfsplus_fs.h"
21
Matthew Garretta051f712012-02-06 15:14:40 -050022/*
23 * "Blessing" an HFS+ filesystem writes metadata to the superblock informing
24 * the platform firmware which file to boot from
25 */
26static int hfsplus_ioctl_bless(struct file *file, int __user *user_flags)
27{
28 struct dentry *dentry = file->f_path.dentry;
David Howells2b0143b2015-03-17 22:25:59 +000029 struct inode *inode = d_inode(dentry);
Matthew Garretta051f712012-02-06 15:14:40 -050030 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 Garrett7dea96652012-06-17 17:05:25 -040033 u32 cnid = (unsigned long)dentry->d_fsdata;
Matthew Garretta051f712012-02-06 15:14:40 -050034
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 Garrett7dea96652012-06-17 17:05:25 -040044 /*
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 Garretta051f712012-02-06 15:14:40 -050050
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 Hellwig94744562010-10-01 05:41:31 +020059static int hfsplus_ioctl_getflags(struct file *file, int __user *user_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060{
Al Viro496ad9a2013-01-23 17:07:38 -050061 struct inode *inode = file_inode(file);
Christoph Hellwig6af502d2010-10-01 05:43:31 +020062 struct hfsplus_inode_info *hip = HFSPLUS_I(inode);
Christoph Hellwig94744562010-10-01 05:41:31 +020063 unsigned int flags = 0;
64
Christoph Hellwig722c55d2010-10-14 09:54:33 -040065 if (inode->i_flags & S_IMMUTABLE)
Christoph Hellwig94744562010-10-01 05:41:31 +020066 flags |= FS_IMMUTABLE_FL;
Anton Salikhmetov596276c2010-12-16 14:44:51 +020067 if (inode->i_flags & S_APPEND)
Christoph Hellwig94744562010-10-01 05:41:31 +020068 flags |= FS_APPEND_FL;
Christoph Hellwig6af502d2010-10-01 05:43:31 +020069 if (hip->userflags & HFSPLUS_FLG_NODUMP)
Christoph Hellwig94744562010-10-01 05:41:31 +020070 flags |= FS_NODUMP_FL;
71
72 return put_user(flags, user_flags);
73}
74
75static int hfsplus_ioctl_setflags(struct file *file, int __user *user_flags)
76{
Al Viro496ad9a2013-01-23 17:07:38 -050077 struct inode *inode = file_inode(file);
Christoph Hellwig6af502d2010-10-01 05:43:31 +020078 struct hfsplus_inode_info *hip = HFSPLUS_I(inode);
Fabian Frederick73d28d572015-04-16 12:47:04 -070079 unsigned int flags, new_fl = 0;
Christoph Hellwig94744562010-10-01 05:41:31 +020080 int err = 0;
81
Al Viroa561be72011-11-23 11:57:51 -050082 err = mnt_want_write_file(file);
Christoph Hellwig94744562010-10-01 05:41:31 +020083 if (err)
Christoph Hellwig63338162010-10-01 05:41:35 +020084 goto out;
Christoph Hellwig94744562010-10-01 05:41:31 +020085
Serge E. Hallyn2e149672011-03-23 16:43:26 -070086 if (!inode_owner_or_capable(inode)) {
Christoph Hellwig94744562010-10-01 05:41:31 +020087 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 Viro59551022016-01-22 15:40:57 -050096 inode_lock(inode);
Christoph Hellwig63338162010-10-01 05:41:35 +020097
Christoph Hellwig722c55d2010-10-14 09:54:33 -040098 if ((flags & (FS_IMMUTABLE_FL|FS_APPEND_FL)) ||
99 inode->i_flags & (S_IMMUTABLE|S_APPEND)) {
Christoph Hellwig94744562010-10-01 05:41:31 +0200100 if (!capable(CAP_LINUX_IMMUTABLE)) {
101 err = -EPERM;
Christoph Hellwig63338162010-10-01 05:41:35 +0200102 goto out_unlock_inode;
Christoph Hellwig94744562010-10-01 05:41:31 +0200103 }
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 Hellwig63338162010-10-01 05:41:35 +0200109 goto out_unlock_inode;
Christoph Hellwig94744562010-10-01 05:41:31 +0200110 }
Christoph Hellwig722c55d2010-10-14 09:54:33 -0400111
112 if (flags & FS_IMMUTABLE_FL)
Fabian Frederick73d28d572015-04-16 12:47:04 -0700113 new_fl |= S_IMMUTABLE;
Christoph Hellwig722c55d2010-10-14 09:54:33 -0400114
115 if (flags & FS_APPEND_FL)
Fabian Frederick73d28d572015-04-16 12:47:04 -0700116 new_fl |= S_APPEND;
117
118 inode_set_flags(inode, new_fl, S_IMMUTABLE | S_APPEND);
Christoph Hellwig722c55d2010-10-14 09:54:33 -0400119
Christoph Hellwig94744562010-10-01 05:41:31 +0200120 if (flags & FS_NODUMP_FL)
Christoph Hellwig6af502d2010-10-01 05:43:31 +0200121 hip->userflags |= HFSPLUS_FLG_NODUMP;
Christoph Hellwig94744562010-10-01 05:41:31 +0200122 else
Christoph Hellwig6af502d2010-10-01 05:43:31 +0200123 hip->userflags &= ~HFSPLUS_FLG_NODUMP;
Christoph Hellwig94744562010-10-01 05:41:31 +0200124
Deepa Dinamani02027d42016-09-14 07:48:05 -0700125 inode->i_ctime = current_time(inode);
Christoph Hellwig94744562010-10-01 05:41:31 +0200126 mark_inode_dirty(inode);
127
Christoph Hellwig63338162010-10-01 05:41:35 +0200128out_unlock_inode:
Al Viro59551022016-01-22 15:40:57 -0500129 inode_unlock(inode);
Christoph Hellwig94744562010-10-01 05:41:31 +0200130out_drop_write:
Al Viro2a79f172011-12-09 08:06:57 -0500131 mnt_drop_write_file(file);
Christoph Hellwig63338162010-10-01 05:41:35 +0200132out:
Christoph Hellwig94744562010-10-01 05:41:31 +0200133 return err;
134}
135
136long hfsplus_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
137{
138 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140 switch (cmd) {
141 case HFSPLUS_IOC_EXT2_GETFLAGS:
Christoph Hellwig94744562010-10-01 05:41:31 +0200142 return hfsplus_ioctl_getflags(file, argp);
143 case HFSPLUS_IOC_EXT2_SETFLAGS:
144 return hfsplus_ioctl_setflags(file, argp);
Matthew Garretta051f712012-02-06 15:14:40 -0500145 case HFSPLUS_IOC_BLESS:
146 return hfsplus_ioctl_bless(file, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 return -ENOTTY;
149 }
150}