blob: 40a85a3ded6efe6a0a17d08fe2b068ba6b4e369a [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
Christoph Hellwig94744562010-10-01 05:41:31 +020023static int hfsplus_ioctl_getflags(struct file *file, int __user *user_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070024{
Christoph Hellwig94744562010-10-01 05:41:31 +020025 struct inode *inode = file->f_path.dentry->d_inode;
Christoph Hellwig6af502d2010-10-01 05:43:31 +020026 struct hfsplus_inode_info *hip = HFSPLUS_I(inode);
Christoph Hellwig94744562010-10-01 05:41:31 +020027 unsigned int flags = 0;
28
Christoph Hellwig722c55d2010-10-14 09:54:33 -040029 if (inode->i_flags & S_IMMUTABLE)
Christoph Hellwig94744562010-10-01 05:41:31 +020030 flags |= FS_IMMUTABLE_FL;
Christoph Hellwig722c55d2010-10-14 09:54:33 -040031 if (inode->i_flags |= S_APPEND)
Christoph Hellwig94744562010-10-01 05:41:31 +020032 flags |= FS_APPEND_FL;
Christoph Hellwig6af502d2010-10-01 05:43:31 +020033 if (hip->userflags & HFSPLUS_FLG_NODUMP)
Christoph Hellwig94744562010-10-01 05:41:31 +020034 flags |= FS_NODUMP_FL;
35
36 return put_user(flags, user_flags);
37}
38
39static int hfsplus_ioctl_setflags(struct file *file, int __user *user_flags)
40{
41 struct inode *inode = file->f_path.dentry->d_inode;
Christoph Hellwig6af502d2010-10-01 05:43:31 +020042 struct hfsplus_inode_info *hip = HFSPLUS_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 unsigned int flags;
Christoph Hellwig94744562010-10-01 05:41:31 +020044 int err = 0;
45
Christoph Hellwig94744562010-10-01 05:41:31 +020046 err = mnt_want_write(file->f_path.mnt);
47 if (err)
Christoph Hellwig63338162010-10-01 05:41:35 +020048 goto out;
Christoph Hellwig94744562010-10-01 05:41:31 +020049
50 if (!is_owner_or_cap(inode)) {
51 err = -EACCES;
52 goto out_drop_write;
53 }
54
55 if (get_user(flags, user_flags)) {
56 err = -EFAULT;
57 goto out_drop_write;
58 }
59
Christoph Hellwig63338162010-10-01 05:41:35 +020060 mutex_lock(&inode->i_mutex);
61
Christoph Hellwig722c55d2010-10-14 09:54:33 -040062 if ((flags & (FS_IMMUTABLE_FL|FS_APPEND_FL)) ||
63 inode->i_flags & (S_IMMUTABLE|S_APPEND)) {
Christoph Hellwig94744562010-10-01 05:41:31 +020064 if (!capable(CAP_LINUX_IMMUTABLE)) {
65 err = -EPERM;
Christoph Hellwig63338162010-10-01 05:41:35 +020066 goto out_unlock_inode;
Christoph Hellwig94744562010-10-01 05:41:31 +020067 }
68 }
69
70 /* don't silently ignore unsupported ext2 flags */
71 if (flags & ~(FS_IMMUTABLE_FL|FS_APPEND_FL|FS_NODUMP_FL)) {
72 err = -EOPNOTSUPP;
Christoph Hellwig63338162010-10-01 05:41:35 +020073 goto out_unlock_inode;
Christoph Hellwig94744562010-10-01 05:41:31 +020074 }
Christoph Hellwig722c55d2010-10-14 09:54:33 -040075
76 if (flags & FS_IMMUTABLE_FL)
Christoph Hellwig94744562010-10-01 05:41:31 +020077 inode->i_flags |= S_IMMUTABLE;
Christoph Hellwig722c55d2010-10-14 09:54:33 -040078 else
Christoph Hellwig94744562010-10-01 05:41:31 +020079 inode->i_flags &= ~S_IMMUTABLE;
Christoph Hellwig722c55d2010-10-14 09:54:33 -040080
81 if (flags & FS_APPEND_FL)
Christoph Hellwig94744562010-10-01 05:41:31 +020082 inode->i_flags |= S_APPEND;
Christoph Hellwig722c55d2010-10-14 09:54:33 -040083 else
Christoph Hellwig94744562010-10-01 05:41:31 +020084 inode->i_flags &= ~S_APPEND;
Christoph Hellwig722c55d2010-10-14 09:54:33 -040085
Christoph Hellwig94744562010-10-01 05:41:31 +020086 if (flags & FS_NODUMP_FL)
Christoph Hellwig6af502d2010-10-01 05:43:31 +020087 hip->userflags |= HFSPLUS_FLG_NODUMP;
Christoph Hellwig94744562010-10-01 05:41:31 +020088 else
Christoph Hellwig6af502d2010-10-01 05:43:31 +020089 hip->userflags &= ~HFSPLUS_FLG_NODUMP;
Christoph Hellwig94744562010-10-01 05:41:31 +020090
91 inode->i_ctime = CURRENT_TIME_SEC;
92 mark_inode_dirty(inode);
93
Christoph Hellwig63338162010-10-01 05:41:35 +020094out_unlock_inode:
Dan Carpentere50fb582010-10-25 20:39:07 +020095 mutex_unlock(&inode->i_mutex);
Christoph Hellwig94744562010-10-01 05:41:31 +020096out_drop_write:
97 mnt_drop_write(file->f_path.mnt);
Christoph Hellwig63338162010-10-01 05:41:35 +020098out:
Christoph Hellwig94744562010-10-01 05:41:31 +020099 return err;
100}
101
102long hfsplus_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
103{
104 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106 switch (cmd) {
107 case HFSPLUS_IOC_EXT2_GETFLAGS:
Christoph Hellwig94744562010-10-01 05:41:31 +0200108 return hfsplus_ioctl_getflags(file, argp);
109 case HFSPLUS_IOC_EXT2_SETFLAGS:
110 return hfsplus_ioctl_setflags(file, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 return -ENOTTY;
113 }
114}
115
116int hfsplus_setxattr(struct dentry *dentry, const char *name,
117 const void *value, size_t size, int flags)
118{
119 struct inode *inode = dentry->d_inode;
120 struct hfs_find_data fd;
121 hfsplus_cat_entry entry;
122 struct hfsplus_cat_file *file;
123 int res;
124
125 if (!S_ISREG(inode->i_mode) || HFSPLUS_IS_RSRC(inode))
126 return -EOPNOTSUPP;
127
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200128 res = hfs_find_init(HFSPLUS_SB(inode->i_sb)->cat_tree, &fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 if (res)
130 return res;
131 res = hfsplus_find_cat(inode->i_sb, inode->i_ino, &fd);
132 if (res)
133 goto out;
134 hfs_bnode_read(fd.bnode, &entry, fd.entryoffset,
135 sizeof(struct hfsplus_cat_file));
136 file = &entry.file;
137
138 if (!strcmp(name, "hfs.type")) {
139 if (size == 4)
140 memcpy(&file->user_info.fdType, value, 4);
141 else
142 res = -ERANGE;
143 } else if (!strcmp(name, "hfs.creator")) {
144 if (size == 4)
145 memcpy(&file->user_info.fdCreator, value, 4);
146 else
147 res = -ERANGE;
148 } else
149 res = -EOPNOTSUPP;
150 if (!res)
151 hfs_bnode_write(fd.bnode, &entry, fd.entryoffset,
152 sizeof(struct hfsplus_cat_file));
153out:
154 hfs_find_exit(&fd);
155 return res;
156}
157
158ssize_t hfsplus_getxattr(struct dentry *dentry, const char *name,
159 void *value, size_t size)
160{
161 struct inode *inode = dentry->d_inode;
162 struct hfs_find_data fd;
163 hfsplus_cat_entry entry;
164 struct hfsplus_cat_file *file;
165 ssize_t res = 0;
166
167 if (!S_ISREG(inode->i_mode) || HFSPLUS_IS_RSRC(inode))
168 return -EOPNOTSUPP;
169
170 if (size) {
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200171 res = hfs_find_init(HFSPLUS_SB(inode->i_sb)->cat_tree, &fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 if (res)
173 return res;
174 res = hfsplus_find_cat(inode->i_sb, inode->i_ino, &fd);
175 if (res)
176 goto out;
177 hfs_bnode_read(fd.bnode, &entry, fd.entryoffset,
178 sizeof(struct hfsplus_cat_file));
179 }
180 file = &entry.file;
181
182 if (!strcmp(name, "hfs.type")) {
183 if (size >= 4) {
184 memcpy(value, &file->user_info.fdType, 4);
185 res = 4;
186 } else
187 res = size ? -ERANGE : 4;
188 } else if (!strcmp(name, "hfs.creator")) {
189 if (size >= 4) {
190 memcpy(value, &file->user_info.fdCreator, 4);
191 res = 4;
192 } else
193 res = size ? -ERANGE : 4;
194 } else
Christoph Hellwig46bf36e2010-10-15 05:45:00 -0700195 res = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196out:
197 if (size)
198 hfs_find_exit(&fd);
199 return res;
200}
201
202#define HFSPLUS_ATTRLIST_SIZE (sizeof("hfs.creator")+sizeof("hfs.type"))
203
204ssize_t hfsplus_listxattr(struct dentry *dentry, char *buffer, size_t size)
205{
206 struct inode *inode = dentry->d_inode;
207
208 if (!S_ISREG(inode->i_mode) || HFSPLUS_IS_RSRC(inode))
209 return -EOPNOTSUPP;
210
211 if (!buffer || !size)
212 return HFSPLUS_ATTRLIST_SIZE;
213 if (size < HFSPLUS_ATTRLIST_SIZE)
214 return -ERANGE;
215 strcpy(buffer, "hfs.type");
216 strcpy(buffer + sizeof("hfs.type"), "hfs.creator");
217
218 return HFSPLUS_ATTRLIST_SIZE;
219}