blob: c640ba57074b8ba58c46c148815c714f20b48939 [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>
19#include <linux/xattr.h>
20#include <asm/uaccess.h>
21#include "hfsplus_fs.h"
22
Matthew Garretta051f712012-02-06 15:14:40 -050023/*
24 * "Blessing" an HFS+ filesystem writes metadata to the superblock informing
25 * the platform firmware which file to boot from
26 */
27static 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;
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
44 /* Bootloader */
45 vh->finder_info[1] = bvh->finder_info[1] = cpu_to_be32(inode->i_ino);
46
47 /* Per spec, the OS X system folder - same as finder_info[0] here */
48 vh->finder_info[5] = bvh->finder_info[5] =
49 cpu_to_be32(parent_ino(dentry));
50
51 mutex_unlock(&sbi->vh_mutex);
52 return 0;
53}
54
Christoph Hellwig94744562010-10-01 05:41:31 +020055static int hfsplus_ioctl_getflags(struct file *file, int __user *user_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
Christoph Hellwig94744562010-10-01 05:41:31 +020057 struct inode *inode = file->f_path.dentry->d_inode;
Christoph Hellwig6af502d2010-10-01 05:43:31 +020058 struct hfsplus_inode_info *hip = HFSPLUS_I(inode);
Christoph Hellwig94744562010-10-01 05:41:31 +020059 unsigned int flags = 0;
60
Christoph Hellwig722c55d2010-10-14 09:54:33 -040061 if (inode->i_flags & S_IMMUTABLE)
Christoph Hellwig94744562010-10-01 05:41:31 +020062 flags |= FS_IMMUTABLE_FL;
Anton Salikhmetov596276c2010-12-16 14:44:51 +020063 if (inode->i_flags & S_APPEND)
Christoph Hellwig94744562010-10-01 05:41:31 +020064 flags |= FS_APPEND_FL;
Christoph Hellwig6af502d2010-10-01 05:43:31 +020065 if (hip->userflags & HFSPLUS_FLG_NODUMP)
Christoph Hellwig94744562010-10-01 05:41:31 +020066 flags |= FS_NODUMP_FL;
67
68 return put_user(flags, user_flags);
69}
70
71static int hfsplus_ioctl_setflags(struct file *file, int __user *user_flags)
72{
73 struct inode *inode = file->f_path.dentry->d_inode;
Christoph Hellwig6af502d2010-10-01 05:43:31 +020074 struct hfsplus_inode_info *hip = HFSPLUS_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 unsigned int flags;
Christoph Hellwig94744562010-10-01 05:41:31 +020076 int err = 0;
77
Al Viroa561be72011-11-23 11:57:51 -050078 err = mnt_want_write_file(file);
Christoph Hellwig94744562010-10-01 05:41:31 +020079 if (err)
Christoph Hellwig63338162010-10-01 05:41:35 +020080 goto out;
Christoph Hellwig94744562010-10-01 05:41:31 +020081
Serge E. Hallyn2e149672011-03-23 16:43:26 -070082 if (!inode_owner_or_capable(inode)) {
Christoph Hellwig94744562010-10-01 05:41:31 +020083 err = -EACCES;
84 goto out_drop_write;
85 }
86
87 if (get_user(flags, user_flags)) {
88 err = -EFAULT;
89 goto out_drop_write;
90 }
91
Christoph Hellwig63338162010-10-01 05:41:35 +020092 mutex_lock(&inode->i_mutex);
93
Christoph Hellwig722c55d2010-10-14 09:54:33 -040094 if ((flags & (FS_IMMUTABLE_FL|FS_APPEND_FL)) ||
95 inode->i_flags & (S_IMMUTABLE|S_APPEND)) {
Christoph Hellwig94744562010-10-01 05:41:31 +020096 if (!capable(CAP_LINUX_IMMUTABLE)) {
97 err = -EPERM;
Christoph Hellwig63338162010-10-01 05:41:35 +020098 goto out_unlock_inode;
Christoph Hellwig94744562010-10-01 05:41:31 +020099 }
100 }
101
102 /* don't silently ignore unsupported ext2 flags */
103 if (flags & ~(FS_IMMUTABLE_FL|FS_APPEND_FL|FS_NODUMP_FL)) {
104 err = -EOPNOTSUPP;
Christoph Hellwig63338162010-10-01 05:41:35 +0200105 goto out_unlock_inode;
Christoph Hellwig94744562010-10-01 05:41:31 +0200106 }
Christoph Hellwig722c55d2010-10-14 09:54:33 -0400107
108 if (flags & FS_IMMUTABLE_FL)
Christoph Hellwig94744562010-10-01 05:41:31 +0200109 inode->i_flags |= S_IMMUTABLE;
Christoph Hellwig722c55d2010-10-14 09:54:33 -0400110 else
Christoph Hellwig94744562010-10-01 05:41:31 +0200111 inode->i_flags &= ~S_IMMUTABLE;
Christoph Hellwig722c55d2010-10-14 09:54:33 -0400112
113 if (flags & FS_APPEND_FL)
Christoph Hellwig94744562010-10-01 05:41:31 +0200114 inode->i_flags |= S_APPEND;
Christoph Hellwig722c55d2010-10-14 09:54:33 -0400115 else
Christoph Hellwig94744562010-10-01 05:41:31 +0200116 inode->i_flags &= ~S_APPEND;
Christoph Hellwig722c55d2010-10-14 09:54:33 -0400117
Christoph Hellwig94744562010-10-01 05:41:31 +0200118 if (flags & FS_NODUMP_FL)
Christoph Hellwig6af502d2010-10-01 05:43:31 +0200119 hip->userflags |= HFSPLUS_FLG_NODUMP;
Christoph Hellwig94744562010-10-01 05:41:31 +0200120 else
Christoph Hellwig6af502d2010-10-01 05:43:31 +0200121 hip->userflags &= ~HFSPLUS_FLG_NODUMP;
Christoph Hellwig94744562010-10-01 05:41:31 +0200122
123 inode->i_ctime = CURRENT_TIME_SEC;
124 mark_inode_dirty(inode);
125
Christoph Hellwig63338162010-10-01 05:41:35 +0200126out_unlock_inode:
Dan Carpentere50fb582010-10-25 20:39:07 +0200127 mutex_unlock(&inode->i_mutex);
Christoph Hellwig94744562010-10-01 05:41:31 +0200128out_drop_write:
Al Viro2a79f172011-12-09 08:06:57 -0500129 mnt_drop_write_file(file);
Christoph Hellwig63338162010-10-01 05:41:35 +0200130out:
Christoph Hellwig94744562010-10-01 05:41:31 +0200131 return err;
132}
133
134long hfsplus_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
135{
136 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138 switch (cmd) {
139 case HFSPLUS_IOC_EXT2_GETFLAGS:
Christoph Hellwig94744562010-10-01 05:41:31 +0200140 return hfsplus_ioctl_getflags(file, argp);
141 case HFSPLUS_IOC_EXT2_SETFLAGS:
142 return hfsplus_ioctl_setflags(file, argp);
Matthew Garretta051f712012-02-06 15:14:40 -0500143 case HFSPLUS_IOC_BLESS:
144 return hfsplus_ioctl_bless(file, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 return -ENOTTY;
147 }
148}
149
150int hfsplus_setxattr(struct dentry *dentry, const char *name,
151 const void *value, size_t size, int flags)
152{
153 struct inode *inode = dentry->d_inode;
154 struct hfs_find_data fd;
155 hfsplus_cat_entry entry;
156 struct hfsplus_cat_file *file;
157 int res;
158
159 if (!S_ISREG(inode->i_mode) || HFSPLUS_IS_RSRC(inode))
160 return -EOPNOTSUPP;
161
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200162 res = hfs_find_init(HFSPLUS_SB(inode->i_sb)->cat_tree, &fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 if (res)
164 return res;
165 res = hfsplus_find_cat(inode->i_sb, inode->i_ino, &fd);
166 if (res)
167 goto out;
168 hfs_bnode_read(fd.bnode, &entry, fd.entryoffset,
169 sizeof(struct hfsplus_cat_file));
170 file = &entry.file;
171
172 if (!strcmp(name, "hfs.type")) {
173 if (size == 4)
174 memcpy(&file->user_info.fdType, value, 4);
175 else
176 res = -ERANGE;
177 } else if (!strcmp(name, "hfs.creator")) {
178 if (size == 4)
179 memcpy(&file->user_info.fdCreator, value, 4);
180 else
181 res = -ERANGE;
182 } else
183 res = -EOPNOTSUPP;
Christoph Hellwige3494702010-11-23 14:38:15 +0100184 if (!res) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 hfs_bnode_write(fd.bnode, &entry, fd.entryoffset,
186 sizeof(struct hfsplus_cat_file));
Christoph Hellwige3494702010-11-23 14:38:15 +0100187 hfsplus_mark_inode_dirty(inode, HFSPLUS_I_CAT_DIRTY);
188 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189out:
190 hfs_find_exit(&fd);
191 return res;
192}
193
194ssize_t hfsplus_getxattr(struct dentry *dentry, const char *name,
195 void *value, size_t size)
196{
197 struct inode *inode = dentry->d_inode;
198 struct hfs_find_data fd;
199 hfsplus_cat_entry entry;
200 struct hfsplus_cat_file *file;
201 ssize_t res = 0;
202
203 if (!S_ISREG(inode->i_mode) || HFSPLUS_IS_RSRC(inode))
204 return -EOPNOTSUPP;
205
206 if (size) {
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200207 res = hfs_find_init(HFSPLUS_SB(inode->i_sb)->cat_tree, &fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 if (res)
209 return res;
210 res = hfsplus_find_cat(inode->i_sb, inode->i_ino, &fd);
211 if (res)
212 goto out;
213 hfs_bnode_read(fd.bnode, &entry, fd.entryoffset,
214 sizeof(struct hfsplus_cat_file));
215 }
216 file = &entry.file;
217
218 if (!strcmp(name, "hfs.type")) {
219 if (size >= 4) {
220 memcpy(value, &file->user_info.fdType, 4);
221 res = 4;
222 } else
223 res = size ? -ERANGE : 4;
224 } else if (!strcmp(name, "hfs.creator")) {
225 if (size >= 4) {
226 memcpy(value, &file->user_info.fdCreator, 4);
227 res = 4;
228 } else
229 res = size ? -ERANGE : 4;
230 } else
Christoph Hellwig46bf36e2010-10-15 05:45:00 -0700231 res = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232out:
233 if (size)
234 hfs_find_exit(&fd);
235 return res;
236}
237
238#define HFSPLUS_ATTRLIST_SIZE (sizeof("hfs.creator")+sizeof("hfs.type"))
239
240ssize_t hfsplus_listxattr(struct dentry *dentry, char *buffer, size_t size)
241{
242 struct inode *inode = dentry->d_inode;
243
244 if (!S_ISREG(inode->i_mode) || HFSPLUS_IS_RSRC(inode))
245 return -EOPNOTSUPP;
246
247 if (!buffer || !size)
248 return HFSPLUS_ATTRLIST_SIZE;
249 if (size < HFSPLUS_ATTRLIST_SIZE)
250 return -ERANGE;
251 strcpy(buffer, "hfs.type");
252 strcpy(buffer + sizeof("hfs.type"), "hfs.creator");
253
254 return HFSPLUS_ATTRLIST_SIZE;
255}