blob: ac405f09902651838979e322931ba7a1f1441633 [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>
Arnd Bergmann7cc4bcc2010-04-27 16:24:20 +020020#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <asm/uaccess.h>
22#include "hfsplus_fs.h"
23
Arnd Bergmann7cc4bcc2010-04-27 16:24:20 +020024long hfsplus_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070025{
Arnd Bergmann7cc4bcc2010-04-27 16:24:20 +020026 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 unsigned int flags;
28
Arnd Bergmann7cc4bcc2010-04-27 16:24:20 +020029 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 switch (cmd) {
31 case HFSPLUS_IOC_EXT2_GETFLAGS:
32 flags = 0;
33 if (HFSPLUS_I(inode).rootflags & HFSPLUS_FLG_IMMUTABLE)
David Howells36695672006-08-29 19:06:16 +010034 flags |= FS_IMMUTABLE_FL; /* EXT2_IMMUTABLE_FL */
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 if (HFSPLUS_I(inode).rootflags & HFSPLUS_FLG_APPEND)
David Howells36695672006-08-29 19:06:16 +010036 flags |= FS_APPEND_FL; /* EXT2_APPEND_FL */
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 if (HFSPLUS_I(inode).userflags & HFSPLUS_FLG_NODUMP)
David Howells36695672006-08-29 19:06:16 +010038 flags |= FS_NODUMP_FL; /* EXT2_NODUMP_FL */
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 return put_user(flags, (int __user *)arg);
40 case HFSPLUS_IOC_EXT2_SETFLAGS: {
Dave Hansen42a74f22008-02-15 14:37:46 -080041 int err = 0;
42 err = mnt_want_write(filp->f_path.mnt);
Arnd Bergmann7cc4bcc2010-04-27 16:24:20 +020043 if (err) {
44 unlock_kernel();
Dave Hansen42a74f22008-02-15 14:37:46 -080045 return err;
Arnd Bergmann7cc4bcc2010-04-27 16:24:20 +020046 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Dave Hansen42a74f22008-02-15 14:37:46 -080048 if (!is_owner_or_cap(inode)) {
49 err = -EACCES;
50 goto setflags_out;
51 }
52 if (get_user(flags, (int __user *)arg)) {
53 err = -EFAULT;
54 goto setflags_out;
55 }
David Howells36695672006-08-29 19:06:16 +010056 if (flags & (FS_IMMUTABLE_FL|FS_APPEND_FL) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 HFSPLUS_I(inode).rootflags & (HFSPLUS_FLG_IMMUTABLE|HFSPLUS_FLG_APPEND)) {
Dave Hansen42a74f22008-02-15 14:37:46 -080058 if (!capable(CAP_LINUX_IMMUTABLE)) {
59 err = -EPERM;
60 goto setflags_out;
61 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 }
63
64 /* don't silently ignore unsupported ext2 flags */
Dave Hansen42a74f22008-02-15 14:37:46 -080065 if (flags & ~(FS_IMMUTABLE_FL|FS_APPEND_FL|FS_NODUMP_FL)) {
66 err = -EOPNOTSUPP;
67 goto setflags_out;
68 }
David Howells36695672006-08-29 19:06:16 +010069 if (flags & FS_IMMUTABLE_FL) { /* EXT2_IMMUTABLE_FL */
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 inode->i_flags |= S_IMMUTABLE;
71 HFSPLUS_I(inode).rootflags |= HFSPLUS_FLG_IMMUTABLE;
72 } else {
73 inode->i_flags &= ~S_IMMUTABLE;
74 HFSPLUS_I(inode).rootflags &= ~HFSPLUS_FLG_IMMUTABLE;
75 }
David Howells36695672006-08-29 19:06:16 +010076 if (flags & FS_APPEND_FL) { /* EXT2_APPEND_FL */
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 inode->i_flags |= S_APPEND;
78 HFSPLUS_I(inode).rootflags |= HFSPLUS_FLG_APPEND;
79 } else {
80 inode->i_flags &= ~S_APPEND;
81 HFSPLUS_I(inode).rootflags &= ~HFSPLUS_FLG_APPEND;
82 }
David Howells36695672006-08-29 19:06:16 +010083 if (flags & FS_NODUMP_FL) /* EXT2_NODUMP_FL */
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 HFSPLUS_I(inode).userflags |= HFSPLUS_FLG_NODUMP;
85 else
86 HFSPLUS_I(inode).userflags &= ~HFSPLUS_FLG_NODUMP;
87
88 inode->i_ctime = CURRENT_TIME_SEC;
89 mark_inode_dirty(inode);
Dave Hansen42a74f22008-02-15 14:37:46 -080090setflags_out:
91 mnt_drop_write(filp->f_path.mnt);
Arnd Bergmann7cc4bcc2010-04-27 16:24:20 +020092 unlock_kernel();
Dave Hansen42a74f22008-02-15 14:37:46 -080093 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 }
95 default:
Arnd Bergmann7cc4bcc2010-04-27 16:24:20 +020096 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 return -ENOTTY;
98 }
99}
100
101int hfsplus_setxattr(struct dentry *dentry, const char *name,
102 const void *value, size_t size, int flags)
103{
104 struct inode *inode = dentry->d_inode;
105 struct hfs_find_data fd;
106 hfsplus_cat_entry entry;
107 struct hfsplus_cat_file *file;
108 int res;
109
110 if (!S_ISREG(inode->i_mode) || HFSPLUS_IS_RSRC(inode))
111 return -EOPNOTSUPP;
112
113 res = hfs_find_init(HFSPLUS_SB(inode->i_sb).cat_tree, &fd);
114 if (res)
115 return res;
116 res = hfsplus_find_cat(inode->i_sb, inode->i_ino, &fd);
117 if (res)
118 goto out;
119 hfs_bnode_read(fd.bnode, &entry, fd.entryoffset,
120 sizeof(struct hfsplus_cat_file));
121 file = &entry.file;
122
123 if (!strcmp(name, "hfs.type")) {
124 if (size == 4)
125 memcpy(&file->user_info.fdType, value, 4);
126 else
127 res = -ERANGE;
128 } else if (!strcmp(name, "hfs.creator")) {
129 if (size == 4)
130 memcpy(&file->user_info.fdCreator, value, 4);
131 else
132 res = -ERANGE;
133 } else
134 res = -EOPNOTSUPP;
135 if (!res)
136 hfs_bnode_write(fd.bnode, &entry, fd.entryoffset,
137 sizeof(struct hfsplus_cat_file));
138out:
139 hfs_find_exit(&fd);
140 return res;
141}
142
143ssize_t hfsplus_getxattr(struct dentry *dentry, const char *name,
144 void *value, size_t size)
145{
146 struct inode *inode = dentry->d_inode;
147 struct hfs_find_data fd;
148 hfsplus_cat_entry entry;
149 struct hfsplus_cat_file *file;
150 ssize_t res = 0;
151
152 if (!S_ISREG(inode->i_mode) || HFSPLUS_IS_RSRC(inode))
153 return -EOPNOTSUPP;
154
155 if (size) {
156 res = hfs_find_init(HFSPLUS_SB(inode->i_sb).cat_tree, &fd);
157 if (res)
158 return res;
159 res = hfsplus_find_cat(inode->i_sb, inode->i_ino, &fd);
160 if (res)
161 goto out;
162 hfs_bnode_read(fd.bnode, &entry, fd.entryoffset,
163 sizeof(struct hfsplus_cat_file));
164 }
165 file = &entry.file;
166
167 if (!strcmp(name, "hfs.type")) {
168 if (size >= 4) {
169 memcpy(value, &file->user_info.fdType, 4);
170 res = 4;
171 } else
172 res = size ? -ERANGE : 4;
173 } else if (!strcmp(name, "hfs.creator")) {
174 if (size >= 4) {
175 memcpy(value, &file->user_info.fdCreator, 4);
176 res = 4;
177 } else
178 res = size ? -ERANGE : 4;
179 } else
180 res = -ENODATA;
181out:
182 if (size)
183 hfs_find_exit(&fd);
184 return res;
185}
186
187#define HFSPLUS_ATTRLIST_SIZE (sizeof("hfs.creator")+sizeof("hfs.type"))
188
189ssize_t hfsplus_listxattr(struct dentry *dentry, char *buffer, size_t size)
190{
191 struct inode *inode = dentry->d_inode;
192
193 if (!S_ISREG(inode->i_mode) || HFSPLUS_IS_RSRC(inode))
194 return -EOPNOTSUPP;
195
196 if (!buffer || !size)
197 return HFSPLUS_ATTRLIST_SIZE;
198 if (size < HFSPLUS_ATTRLIST_SIZE)
199 return -ERANGE;
200 strcpy(buffer, "hfs.type");
201 strcpy(buffer + sizeof("hfs.type"), "hfs.creator");
202
203 return HFSPLUS_ATTRLIST_SIZE;
204}