hfsplus: fix an artifact in ioctl flag checking
[linux-2.6-block.git] / fs / hfsplus / catalog.c
CommitLineData
1da177e4
LT
1/*
2 * linux/fs/hfsplus/catalog.c
3 *
4 * Copyright (C) 2001
5 * Brad Boyer (flar@allandria.com)
6 * (C) 2003 Ardis Technologies <roman@ardistech.com>
7 *
8 * Handling of catalog records
9 */
10
1da177e4
LT
11
12#include "hfsplus_fs.h"
13#include "hfsplus_raw.h"
14
2179d372
DE
15int hfsplus_cat_case_cmp_key(const hfsplus_btree_key *k1,
16 const hfsplus_btree_key *k2)
1da177e4
LT
17{
18 __be32 k1p, k2p;
19
20 k1p = k1->cat.parent;
21 k2p = k2->cat.parent;
22 if (k1p != k2p)
23 return be32_to_cpu(k1p) < be32_to_cpu(k2p) ? -1 : 1;
24
2179d372
DE
25 return hfsplus_strcasecmp(&k1->cat.name, &k2->cat.name);
26}
27
28int hfsplus_cat_bin_cmp_key(const hfsplus_btree_key *k1,
29 const hfsplus_btree_key *k2)
30{
31 __be32 k1p, k2p;
32
33 k1p = k1->cat.parent;
34 k2p = k2->cat.parent;
35 if (k1p != k2p)
36 return be32_to_cpu(k1p) < be32_to_cpu(k2p) ? -1 : 1;
37
38 return hfsplus_strcmp(&k1->cat.name, &k2->cat.name);
1da177e4
LT
39}
40
41void hfsplus_cat_build_key(struct super_block *sb, hfsplus_btree_key *key,
42 u32 parent, struct qstr *str)
43{
44 int len;
45
46 key->cat.parent = cpu_to_be32(parent);
47 if (str) {
48 hfsplus_asc2uni(sb, &key->cat.name, str->name, str->len);
49 len = be16_to_cpu(key->cat.name.length);
50 } else {
51 key->cat.name.length = 0;
52 len = 0;
53 }
54 key->key_len = cpu_to_be16(6 + 2 * len);
55}
56
57static void hfsplus_cat_build_key_uni(hfsplus_btree_key *key, u32 parent,
58 struct hfsplus_unistr *name)
59{
60 int ustrlen;
61
62 ustrlen = be16_to_cpu(name->length);
63 key->cat.parent = cpu_to_be32(parent);
64 key->cat.name.length = cpu_to_be16(ustrlen);
65 ustrlen *= 2;
66 memcpy(key->cat.name.unicode, name->unicode, ustrlen);
67 key->key_len = cpu_to_be16(6 + ustrlen);
68}
69
90e61690 70void hfsplus_cat_set_perms(struct inode *inode, struct hfsplus_perm *perms)
1da177e4
LT
71{
72 if (inode->i_flags & S_IMMUTABLE)
73 perms->rootflags |= HFSPLUS_FLG_IMMUTABLE;
74 else
75 perms->rootflags &= ~HFSPLUS_FLG_IMMUTABLE;
76 if (inode->i_flags & S_APPEND)
77 perms->rootflags |= HFSPLUS_FLG_APPEND;
78 else
79 perms->rootflags &= ~HFSPLUS_FLG_APPEND;
90e61690
CH
80
81 perms->userflags = HFSPLUS_I(inode)->userflags;
1da177e4
LT
82 perms->mode = cpu_to_be16(inode->i_mode);
83 perms->owner = cpu_to_be32(inode->i_uid);
84 perms->group = cpu_to_be32(inode->i_gid);
90e61690
CH
85
86 if (S_ISREG(inode->i_mode))
87 perms->dev = cpu_to_be32(inode->i_nlink);
88 else if (S_ISBLK(inode->i_mode) || S_ISCHR(inode->i_mode))
89 perms->dev = cpu_to_be32(inode->i_rdev);
90 else
91 perms->dev = 0;
1da177e4
LT
92}
93
94static int hfsplus_cat_build_record(hfsplus_cat_entry *entry, u32 cnid, struct inode *inode)
95{
dd73a01a
CH
96 struct hfsplus_sb_info *sbi = HFSPLUS_SB(inode->i_sb);
97
1da177e4
LT
98 if (S_ISDIR(inode->i_mode)) {
99 struct hfsplus_cat_folder *folder;
100
101 folder = &entry->folder;
102 memset(folder, 0, sizeof(*folder));
103 folder->type = cpu_to_be16(HFSPLUS_FOLDER);
104 folder->id = cpu_to_be32(inode->i_ino);
6af502de 105 HFSPLUS_I(inode)->create_date =
9a4cad95
RZ
106 folder->create_date =
107 folder->content_mod_date =
108 folder->attribute_mod_date =
109 folder->access_date = hfsp_now2mt();
90e61690 110 hfsplus_cat_set_perms(inode, &folder->permissions);
dd73a01a 111 if (inode == sbi->hidden_dir)
1da177e4
LT
112 /* invisible and namelocked */
113 folder->user_info.frFlags = cpu_to_be16(0x5000);
114 return sizeof(*folder);
115 } else {
116 struct hfsplus_cat_file *file;
117
118 file = &entry->file;
119 memset(file, 0, sizeof(*file));
120 file->type = cpu_to_be16(HFSPLUS_FILE);
121 file->flags = cpu_to_be16(HFSPLUS_FILE_THREAD_EXISTS);
122 file->id = cpu_to_be32(cnid);
6af502de 123 HFSPLUS_I(inode)->create_date =
9a4cad95
RZ
124 file->create_date =
125 file->content_mod_date =
126 file->attribute_mod_date =
127 file->access_date = hfsp_now2mt();
1da177e4 128 if (cnid == inode->i_ino) {
90e61690 129 hfsplus_cat_set_perms(inode, &file->permissions);
6b192832
RZ
130 if (S_ISLNK(inode->i_mode)) {
131 file->user_info.fdType = cpu_to_be32(HFSP_SYMLINK_TYPE);
132 file->user_info.fdCreator = cpu_to_be32(HFSP_SYMLINK_CREATOR);
133 } else {
dd73a01a
CH
134 file->user_info.fdType = cpu_to_be32(sbi->type);
135 file->user_info.fdCreator = cpu_to_be32(sbi->creator);
6b192832 136 }
1da177e4
LT
137 if ((file->permissions.rootflags | file->permissions.userflags) & HFSPLUS_FLG_IMMUTABLE)
138 file->flags |= cpu_to_be16(HFSPLUS_FILE_LOCKED);
139 } else {
140 file->user_info.fdType = cpu_to_be32(HFSP_HARDLINK_TYPE);
141 file->user_info.fdCreator = cpu_to_be32(HFSP_HFSPLUS_CREATOR);
142 file->user_info.fdFlags = cpu_to_be16(0x100);
6af502de 143 file->create_date = HFSPLUS_I(sbi->hidden_dir)->create_date;
f6089ff8 144 file->permissions.dev = cpu_to_be32(HFSPLUS_I(inode)->linkid);
1da177e4
LT
145 }
146 return sizeof(*file);
147 }
148}
149
150static int hfsplus_fill_cat_thread(struct super_block *sb,
151 hfsplus_cat_entry *entry, int type,
152 u32 parentid, struct qstr *str)
153{
154 entry->type = cpu_to_be16(type);
155 entry->thread.reserved = 0;
156 entry->thread.parentID = cpu_to_be32(parentid);
157 hfsplus_asc2uni(sb, &entry->thread.nodeName, str->name, str->len);
158 return 10 + be16_to_cpu(entry->thread.nodeName.length) * 2;
159}
160
161/* Try to get a catalog entry for given catalog id */
162int hfsplus_find_cat(struct super_block *sb, u32 cnid,
163 struct hfs_find_data *fd)
164{
165 hfsplus_cat_entry tmp;
166 int err;
167 u16 type;
168
169 hfsplus_cat_build_key(sb, fd->search_key, cnid, NULL);
170 err = hfs_brec_read(fd, &tmp, sizeof(hfsplus_cat_entry));
171 if (err)
172 return err;
173
174 type = be16_to_cpu(tmp.type);
175 if (type != HFSPLUS_FOLDER_THREAD && type != HFSPLUS_FILE_THREAD) {
634725a9 176 printk(KERN_ERR "hfs: found bad thread record in catalog\n");
1da177e4
LT
177 return -EIO;
178 }
179
efc7ffcb
ES
180 if (be16_to_cpu(tmp.thread.nodeName.length) > 255) {
181 printk(KERN_ERR "hfs: catalog name length corrupted\n");
182 return -EIO;
183 }
184
1da177e4
LT
185 hfsplus_cat_build_key_uni(fd->search_key, be32_to_cpu(tmp.thread.parentID),
186 &tmp.thread.nodeName);
187 return hfs_brec_find(fd);
188}
189
190int hfsplus_create_cat(u32 cnid, struct inode *dir, struct qstr *str, struct inode *inode)
191{
dd73a01a 192 struct super_block *sb = dir->i_sb;
1da177e4 193 struct hfs_find_data fd;
1da177e4
LT
194 hfsplus_cat_entry entry;
195 int entry_size;
196 int err;
197
198 dprint(DBG_CAT_MOD, "create_cat: %s,%u(%d)\n", str->name, cnid, inode->i_nlink);
dd73a01a 199 hfs_find_init(HFSPLUS_SB(sb)->cat_tree, &fd);
1da177e4
LT
200
201 hfsplus_cat_build_key(sb, fd.search_key, cnid, NULL);
202 entry_size = hfsplus_fill_cat_thread(sb, &entry, S_ISDIR(inode->i_mode) ?
203 HFSPLUS_FOLDER_THREAD : HFSPLUS_FILE_THREAD,
204 dir->i_ino, str);
205 err = hfs_brec_find(&fd);
206 if (err != -ENOENT) {
207 if (!err)
208 err = -EEXIST;
209 goto err2;
210 }
211 err = hfs_brec_insert(&fd, &entry, entry_size);
212 if (err)
213 goto err2;
214
215 hfsplus_cat_build_key(sb, fd.search_key, dir->i_ino, str);
216 entry_size = hfsplus_cat_build_record(&entry, cnid, inode);
217 err = hfs_brec_find(&fd);
218 if (err != -ENOENT) {
219 /* panic? */
220 if (!err)
221 err = -EEXIST;
222 goto err1;
223 }
224 err = hfs_brec_insert(&fd, &entry, entry_size);
225 if (err)
226 goto err1;
227
228 dir->i_size++;
229 dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
e3494705
CH
230 hfsplus_mark_inode_dirty(dir, HFSPLUS_I_CAT_DIRTY);
231
1da177e4
LT
232 hfs_find_exit(&fd);
233 return 0;
234
235err1:
236 hfsplus_cat_build_key(sb, fd.search_key, cnid, NULL);
237 if (!hfs_brec_find(&fd))
238 hfs_brec_remove(&fd);
239err2:
240 hfs_find_exit(&fd);
241 return err;
242}
243
244int hfsplus_delete_cat(u32 cnid, struct inode *dir, struct qstr *str)
245{
dd73a01a 246 struct super_block *sb = dir->i_sb;
1da177e4
LT
247 struct hfs_find_data fd;
248 struct hfsplus_fork_raw fork;
249 struct list_head *pos;
250 int err, off;
251 u16 type;
252
253 dprint(DBG_CAT_MOD, "delete_cat: %s,%u\n", str ? str->name : NULL, cnid);
dd73a01a 254 hfs_find_init(HFSPLUS_SB(sb)->cat_tree, &fd);
1da177e4
LT
255
256 if (!str) {
257 int len;
258
259 hfsplus_cat_build_key(sb, fd.search_key, cnid, NULL);
260 err = hfs_brec_find(&fd);
261 if (err)
262 goto out;
263
264 off = fd.entryoffset + offsetof(struct hfsplus_cat_thread, nodeName);
265 fd.search_key->cat.parent = cpu_to_be32(dir->i_ino);
266 hfs_bnode_read(fd.bnode, &fd.search_key->cat.name.length, off, 2);
267 len = be16_to_cpu(fd.search_key->cat.name.length) * 2;
268 hfs_bnode_read(fd.bnode, &fd.search_key->cat.name.unicode, off + 2, len);
269 fd.search_key->key_len = cpu_to_be16(6 + len);
270 } else
271 hfsplus_cat_build_key(sb, fd.search_key, dir->i_ino, str);
272
273 err = hfs_brec_find(&fd);
274 if (err)
275 goto out;
276
277 type = hfs_bnode_read_u16(fd.bnode, fd.entryoffset);
278 if (type == HFSPLUS_FILE) {
279#if 0
280 off = fd.entryoffset + offsetof(hfsplus_cat_file, data_fork);
281 hfs_bnode_read(fd.bnode, &fork, off, sizeof(fork));
282 hfsplus_free_fork(sb, cnid, &fork, HFSPLUS_TYPE_DATA);
283#endif
284
285 off = fd.entryoffset + offsetof(struct hfsplus_cat_file, rsrc_fork);
286 hfs_bnode_read(fd.bnode, &fork, off, sizeof(fork));
287 hfsplus_free_fork(sb, cnid, &fork, HFSPLUS_TYPE_RSRC);
288 }
289
6af502de 290 list_for_each(pos, &HFSPLUS_I(dir)->open_dir_list) {
1da177e4
LT
291 struct hfsplus_readdir_data *rd =
292 list_entry(pos, struct hfsplus_readdir_data, list);
293 if (fd.tree->keycmp(fd.search_key, (void *)&rd->key) < 0)
294 rd->file->f_pos--;
295 }
296
297 err = hfs_brec_remove(&fd);
298 if (err)
299 goto out;
300
301 hfsplus_cat_build_key(sb, fd.search_key, cnid, NULL);
302 err = hfs_brec_find(&fd);
303 if (err)
304 goto out;
305
306 err = hfs_brec_remove(&fd);
307 if (err)
308 goto out;
309
310 dir->i_size--;
311 dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
e3494705 312 hfsplus_mark_inode_dirty(dir, HFSPLUS_I_CAT_DIRTY);
1da177e4
LT
313out:
314 hfs_find_exit(&fd);
315
316 return err;
317}
318
319int hfsplus_rename_cat(u32 cnid,
320 struct inode *src_dir, struct qstr *src_name,
321 struct inode *dst_dir, struct qstr *dst_name)
322{
dd73a01a 323 struct super_block *sb = src_dir->i_sb;
1da177e4
LT
324 struct hfs_find_data src_fd, dst_fd;
325 hfsplus_cat_entry entry;
326 int entry_size, type;
327 int err = 0;
328
329 dprint(DBG_CAT_MOD, "rename_cat: %u - %lu,%s - %lu,%s\n", cnid, src_dir->i_ino, src_name->name,
330 dst_dir->i_ino, dst_name->name);
dd73a01a 331 hfs_find_init(HFSPLUS_SB(sb)->cat_tree, &src_fd);
1da177e4
LT
332 dst_fd = src_fd;
333
334 /* find the old dir entry and read the data */
335 hfsplus_cat_build_key(sb, src_fd.search_key, src_dir->i_ino, src_name);
336 err = hfs_brec_find(&src_fd);
337 if (err)
338 goto out;
339
340 hfs_bnode_read(src_fd.bnode, &entry, src_fd.entryoffset,
341 src_fd.entrylength);
342
343 /* create new dir entry with the data from the old entry */
344 hfsplus_cat_build_key(sb, dst_fd.search_key, dst_dir->i_ino, dst_name);
345 err = hfs_brec_find(&dst_fd);
346 if (err != -ENOENT) {
347 if (!err)
348 err = -EEXIST;
349 goto out;
350 }
351
352 err = hfs_brec_insert(&dst_fd, &entry, src_fd.entrylength);
353 if (err)
354 goto out;
355 dst_dir->i_size++;
356 dst_dir->i_mtime = dst_dir->i_ctime = CURRENT_TIME_SEC;
1da177e4
LT
357
358 /* finally remove the old entry */
359 hfsplus_cat_build_key(sb, src_fd.search_key, src_dir->i_ino, src_name);
360 err = hfs_brec_find(&src_fd);
361 if (err)
362 goto out;
363 err = hfs_brec_remove(&src_fd);
364 if (err)
365 goto out;
366 src_dir->i_size--;
367 src_dir->i_mtime = src_dir->i_ctime = CURRENT_TIME_SEC;
1da177e4
LT
368
369 /* remove old thread entry */
370 hfsplus_cat_build_key(sb, src_fd.search_key, cnid, NULL);
371 err = hfs_brec_find(&src_fd);
372 if (err)
373 goto out;
374 type = hfs_bnode_read_u16(src_fd.bnode, src_fd.entryoffset);
375 err = hfs_brec_remove(&src_fd);
376 if (err)
377 goto out;
378
379 /* create new thread entry */
380 hfsplus_cat_build_key(sb, dst_fd.search_key, cnid, NULL);
381 entry_size = hfsplus_fill_cat_thread(sb, &entry, type, dst_dir->i_ino, dst_name);
382 err = hfs_brec_find(&dst_fd);
383 if (err != -ENOENT) {
384 if (!err)
385 err = -EEXIST;
386 goto out;
387 }
388 err = hfs_brec_insert(&dst_fd, &entry, entry_size);
e3494705
CH
389
390 hfsplus_mark_inode_dirty(dst_dir, HFSPLUS_I_CAT_DIRTY);
391 hfsplus_mark_inode_dirty(src_dir, HFSPLUS_I_CAT_DIRTY);
1da177e4
LT
392out:
393 hfs_bnode_put(dst_fd.bnode);
394 hfs_find_exit(&src_fd);
395 return err;
396}