blob: f457d2ca51ab8e68a3e60cd9028620fa029f8f18 [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
23int hfsplus_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
24 unsigned long arg)
25{
26 unsigned int flags;
27
28 switch (cmd) {
29 case HFSPLUS_IOC_EXT2_GETFLAGS:
30 flags = 0;
31 if (HFSPLUS_I(inode).rootflags & HFSPLUS_FLG_IMMUTABLE)
David Howells36695672006-08-29 19:06:16 +010032 flags |= FS_IMMUTABLE_FL; /* EXT2_IMMUTABLE_FL */
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 if (HFSPLUS_I(inode).rootflags & HFSPLUS_FLG_APPEND)
David Howells36695672006-08-29 19:06:16 +010034 flags |= FS_APPEND_FL; /* EXT2_APPEND_FL */
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 if (HFSPLUS_I(inode).userflags & HFSPLUS_FLG_NODUMP)
David Howells36695672006-08-29 19:06:16 +010036 flags |= FS_NODUMP_FL; /* EXT2_NODUMP_FL */
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 return put_user(flags, (int __user *)arg);
38 case HFSPLUS_IOC_EXT2_SETFLAGS: {
Dave Hansen42a74f22008-02-15 14:37:46 -080039 int err = 0;
40 err = mnt_want_write(filp->f_path.mnt);
41 if (err)
42 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Dave Hansen42a74f22008-02-15 14:37:46 -080044 if (!is_owner_or_cap(inode)) {
45 err = -EACCES;
46 goto setflags_out;
47 }
48 if (get_user(flags, (int __user *)arg)) {
49 err = -EFAULT;
50 goto setflags_out;
51 }
David Howells36695672006-08-29 19:06:16 +010052 if (flags & (FS_IMMUTABLE_FL|FS_APPEND_FL) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 HFSPLUS_I(inode).rootflags & (HFSPLUS_FLG_IMMUTABLE|HFSPLUS_FLG_APPEND)) {
Dave Hansen42a74f22008-02-15 14:37:46 -080054 if (!capable(CAP_LINUX_IMMUTABLE)) {
55 err = -EPERM;
56 goto setflags_out;
57 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 }
59
60 /* don't silently ignore unsupported ext2 flags */
Dave Hansen42a74f22008-02-15 14:37:46 -080061 if (flags & ~(FS_IMMUTABLE_FL|FS_APPEND_FL|FS_NODUMP_FL)) {
62 err = -EOPNOTSUPP;
63 goto setflags_out;
64 }
David Howells36695672006-08-29 19:06:16 +010065 if (flags & FS_IMMUTABLE_FL) { /* EXT2_IMMUTABLE_FL */
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 inode->i_flags |= S_IMMUTABLE;
67 HFSPLUS_I(inode).rootflags |= HFSPLUS_FLG_IMMUTABLE;
68 } else {
69 inode->i_flags &= ~S_IMMUTABLE;
70 HFSPLUS_I(inode).rootflags &= ~HFSPLUS_FLG_IMMUTABLE;
71 }
David Howells36695672006-08-29 19:06:16 +010072 if (flags & FS_APPEND_FL) { /* EXT2_APPEND_FL */
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 inode->i_flags |= S_APPEND;
74 HFSPLUS_I(inode).rootflags |= HFSPLUS_FLG_APPEND;
75 } else {
76 inode->i_flags &= ~S_APPEND;
77 HFSPLUS_I(inode).rootflags &= ~HFSPLUS_FLG_APPEND;
78 }
David Howells36695672006-08-29 19:06:16 +010079 if (flags & FS_NODUMP_FL) /* EXT2_NODUMP_FL */
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 HFSPLUS_I(inode).userflags |= HFSPLUS_FLG_NODUMP;
81 else
82 HFSPLUS_I(inode).userflags &= ~HFSPLUS_FLG_NODUMP;
83
84 inode->i_ctime = CURRENT_TIME_SEC;
85 mark_inode_dirty(inode);
Dave Hansen42a74f22008-02-15 14:37:46 -080086setflags_out:
87 mnt_drop_write(filp->f_path.mnt);
88 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 }
90 default:
91 return -ENOTTY;
92 }
93}
94
95int hfsplus_setxattr(struct dentry *dentry, const char *name,
96 const void *value, size_t size, int flags)
97{
98 struct inode *inode = dentry->d_inode;
99 struct hfs_find_data fd;
100 hfsplus_cat_entry entry;
101 struct hfsplus_cat_file *file;
102 int res;
103
104 if (!S_ISREG(inode->i_mode) || HFSPLUS_IS_RSRC(inode))
105 return -EOPNOTSUPP;
106
107 res = hfs_find_init(HFSPLUS_SB(inode->i_sb).cat_tree, &fd);
108 if (res)
109 return res;
110 res = hfsplus_find_cat(inode->i_sb, inode->i_ino, &fd);
111 if (res)
112 goto out;
113 hfs_bnode_read(fd.bnode, &entry, fd.entryoffset,
114 sizeof(struct hfsplus_cat_file));
115 file = &entry.file;
116
117 if (!strcmp(name, "hfs.type")) {
118 if (size == 4)
119 memcpy(&file->user_info.fdType, value, 4);
120 else
121 res = -ERANGE;
122 } else if (!strcmp(name, "hfs.creator")) {
123 if (size == 4)
124 memcpy(&file->user_info.fdCreator, value, 4);
125 else
126 res = -ERANGE;
127 } else
128 res = -EOPNOTSUPP;
129 if (!res)
130 hfs_bnode_write(fd.bnode, &entry, fd.entryoffset,
131 sizeof(struct hfsplus_cat_file));
132out:
133 hfs_find_exit(&fd);
134 return res;
135}
136
137ssize_t hfsplus_getxattr(struct dentry *dentry, const char *name,
138 void *value, size_t size)
139{
140 struct inode *inode = dentry->d_inode;
141 struct hfs_find_data fd;
142 hfsplus_cat_entry entry;
143 struct hfsplus_cat_file *file;
144 ssize_t res = 0;
145
146 if (!S_ISREG(inode->i_mode) || HFSPLUS_IS_RSRC(inode))
147 return -EOPNOTSUPP;
148
149 if (size) {
150 res = hfs_find_init(HFSPLUS_SB(inode->i_sb).cat_tree, &fd);
151 if (res)
152 return res;
153 res = hfsplus_find_cat(inode->i_sb, inode->i_ino, &fd);
154 if (res)
155 goto out;
156 hfs_bnode_read(fd.bnode, &entry, fd.entryoffset,
157 sizeof(struct hfsplus_cat_file));
158 }
159 file = &entry.file;
160
161 if (!strcmp(name, "hfs.type")) {
162 if (size >= 4) {
163 memcpy(value, &file->user_info.fdType, 4);
164 res = 4;
165 } else
166 res = size ? -ERANGE : 4;
167 } else if (!strcmp(name, "hfs.creator")) {
168 if (size >= 4) {
169 memcpy(value, &file->user_info.fdCreator, 4);
170 res = 4;
171 } else
172 res = size ? -ERANGE : 4;
173 } else
174 res = -ENODATA;
175out:
176 if (size)
177 hfs_find_exit(&fd);
178 return res;
179}
180
181#define HFSPLUS_ATTRLIST_SIZE (sizeof("hfs.creator")+sizeof("hfs.type"))
182
183ssize_t hfsplus_listxattr(struct dentry *dentry, char *buffer, size_t size)
184{
185 struct inode *inode = dentry->d_inode;
186
187 if (!S_ISREG(inode->i_mode) || HFSPLUS_IS_RSRC(inode))
188 return -EOPNOTSUPP;
189
190 if (!buffer || !size)
191 return HFSPLUS_ATTRLIST_SIZE;
192 if (size < HFSPLUS_ATTRLIST_SIZE)
193 return -ERANGE;
194 strcpy(buffer, "hfs.type");
195 strcpy(buffer + sizeof("hfs.type"), "hfs.creator");
196
197 return HFSPLUS_ATTRLIST_SIZE;
198}