ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / fs / hfsplus / catalog.c
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
11 #include <linux/sched.h>
12
13 #include "hfsplus_fs.h"
14 #include "hfsplus_raw.h"
15
16 int hfsplus_cat_cmp_key(hfsplus_btree_key *k1, hfsplus_btree_key *k2)
17 {
18         u32 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
25         return hfsplus_unistrcmp(&k1->cat.name, &k2->cat.name);
26 }
27
28 void hfsplus_cat_build_key(hfsplus_btree_key *key, u32 parent,
29                           struct qstr *str)
30 {
31         int len;
32
33         key->cat.parent = cpu_to_be32(parent);
34         if (str) {
35                 hfsplus_asc2uni(&key->cat.name, str->name, str->len);
36                 len = be16_to_cpu(key->cat.name.length);
37         } else
38                 len = key->cat.name.length = 0;
39         key->key_len = cpu_to_be16(6 + 2 * len);
40 }
41
42 static void hfsplus_cat_build_key_uni(hfsplus_btree_key *key, u32 parent,
43                                       struct hfsplus_unistr *name)
44 {
45         int ustrlen;
46
47         ustrlen = be16_to_cpu(name->length);
48         key->cat.parent = cpu_to_be32(parent);
49         key->cat.name.length = cpu_to_be16(ustrlen);
50         ustrlen *= 2;
51         memcpy(key->cat.name.unicode, name->unicode, ustrlen);
52         key->key_len = cpu_to_be16(6 + ustrlen);
53 }
54
55 static void hfsplus_set_perms(struct inode *inode, struct hfsplus_perm *perms)
56 {
57         if (inode->i_flags & S_IMMUTABLE)
58                 perms->rootflags |= HFSPLUS_FLG_IMMUTABLE;
59         else
60                 perms->rootflags &= ~HFSPLUS_FLG_IMMUTABLE;
61         if (inode->i_flags & S_APPEND)
62                 perms->rootflags |= HFSPLUS_FLG_APPEND;
63         else
64                 perms->rootflags &= ~HFSPLUS_FLG_APPEND;
65         HFSPLUS_I(inode).rootflags = perms->rootflags;
66         HFSPLUS_I(inode).userflags = perms->userflags;
67         perms->mode = cpu_to_be16(inode->i_mode);
68         perms->owner = cpu_to_be32(inode->i_uid);
69         perms->group = cpu_to_be32(inode->i_gid);
70 }
71
72 static int hfsplus_cat_build_record(hfsplus_cat_entry *entry, u32 cnid, struct inode *inode)
73 {
74         if (S_ISDIR(inode->i_mode)) {
75                 struct hfsplus_cat_folder *folder;
76
77                 folder = &entry->folder;
78                 memset(folder, 0, sizeof(*folder));
79                 folder->type = cpu_to_be16(HFSPLUS_FOLDER);
80                 folder->id = cpu_to_be32(inode->i_ino);
81                 folder->create_date = folder->content_mod_date =
82                         folder->attribute_mod_date = folder->access_date = hfsp_now2mt();
83                 hfsplus_set_perms(inode, &folder->permissions);
84                 if (inode == HFSPLUS_SB(inode->i_sb).hidden_dir)
85                         /* invisible and namelocked */
86                         folder->user_info.frFlags = cpu_to_be16(0x5000);
87                 return sizeof(*folder);
88         } else {
89                 struct hfsplus_cat_file *file;
90
91                 file = &entry->file;
92                 memset(file, 0, sizeof(*file));
93                 file->type = cpu_to_be16(HFSPLUS_FILE);
94                 file->flags = cpu_to_be16(HFSPLUS_FILE_THREAD_EXISTS);
95                 file->id = cpu_to_be32(cnid);
96                 file->create_date = file->content_mod_date =
97                         file->attribute_mod_date = file->access_date = hfsp_now2mt();
98                 if (cnid == inode->i_ino) {
99                         hfsplus_set_perms(inode, &file->permissions);
100                         file->user_info.fdType = cpu_to_be32(HFSPLUS_SB(inode->i_sb).type);
101                         file->user_info.fdCreator = cpu_to_be32(HFSPLUS_SB(inode->i_sb).creator);
102                         if ((file->permissions.rootflags | file->permissions.userflags) & HFSPLUS_FLG_IMMUTABLE)
103                                 file->flags |= cpu_to_be16(HFSPLUS_FILE_LOCKED);
104                 } else {
105                         file->user_info.fdType = cpu_to_be32(HFSP_HARDLINK_TYPE);
106                         file->user_info.fdCreator = cpu_to_be32(HFSP_HFSPLUS_CREATOR);
107                         file->user_info.fdFlags = cpu_to_be16(0x100);
108                         file->permissions.dev = cpu_to_be32(HFSPLUS_I(inode).dev);
109                 }
110                 return sizeof(*file);
111         }
112 }
113
114 static int hfsplus_fill_cat_thread(hfsplus_cat_entry *entry, int type,
115                                    u32 parentid, struct qstr *str)
116 {
117         entry->type = cpu_to_be16(type);
118         entry->thread.reserved = 0;
119         entry->thread.parentID = cpu_to_be32(parentid);
120         hfsplus_asc2uni(&entry->thread.nodeName, str->name, str->len);
121         return 10 + be16_to_cpu(entry->thread.nodeName.length) * 2;
122 }
123
124 /* Try to get a catalog entry for given catalog id */
125 int hfsplus_find_cat(struct super_block *sb, u32 cnid,
126                      struct hfs_find_data *fd)
127 {
128         hfsplus_cat_entry tmp;
129         int err;
130         u16 type;
131
132         hfsplus_cat_build_key(fd->search_key, cnid, NULL);
133         err = hfs_brec_read(fd, &tmp, sizeof(hfsplus_cat_entry));
134         if (err)
135                 return err;
136
137         type = be16_to_cpu(tmp.type);
138         if (type != HFSPLUS_FOLDER_THREAD && type != HFSPLUS_FILE_THREAD) {
139                 printk("HFS+-fs: Found bad thread record in catalog\n");
140                 return -EIO;
141         }
142
143         hfsplus_cat_build_key_uni(fd->search_key, be32_to_cpu(tmp.thread.parentID),
144                                  &tmp.thread.nodeName);
145         return hfs_brec_find(fd);
146 }
147
148 int hfsplus_create_cat(u32 cnid, struct inode *dir, struct qstr *str, struct inode *inode)
149 {
150         struct hfs_find_data fd;
151         struct super_block *sb;
152         hfsplus_cat_entry entry;
153         int entry_size;
154         int err;
155
156         dprint(DBG_CAT_MOD, "create_cat: %s,%u(%d)\n", str->name, cnid, inode->i_nlink);
157         sb = dir->i_sb;
158         hfs_find_init(HFSPLUS_SB(sb).cat_tree, &fd);
159
160         hfsplus_cat_build_key(fd.search_key, cnid, NULL);
161         entry_size = hfsplus_fill_cat_thread(&entry, S_ISDIR(inode->i_mode) ?
162                         HFSPLUS_FOLDER_THREAD : HFSPLUS_FILE_THREAD,
163                         dir->i_ino, str);
164         err = hfs_brec_find(&fd);
165         if (err != -ENOENT) {
166                 if (!err)
167                         err = -EEXIST;
168                 goto out;
169         }
170         err = hfs_brec_insert(&fd, &entry, entry_size);
171         if (err)
172                 goto out;
173
174         hfsplus_cat_build_key(fd.search_key, dir->i_ino, str);
175         entry_size = hfsplus_cat_build_record(&entry, cnid, inode);
176         err = hfs_brec_find(&fd);
177         if (err != -ENOENT) {
178                 /* panic? */
179                 if (!err)
180                         err = -EEXIST;
181                 goto out;
182         }
183         err = hfs_brec_insert(&fd, &entry, entry_size);
184         if (!err) {
185                 dir->i_size++;
186                 mark_inode_dirty(dir);
187         }
188 out:
189         hfs_find_exit(&fd);
190
191         return err;
192 }
193
194 int hfsplus_delete_cat(u32 cnid, struct inode *dir, struct qstr *str)
195 {
196         struct super_block *sb;
197         struct hfs_find_data fd;
198         struct hfsplus_fork_raw fork;
199         struct list_head *pos;
200         int err, off;
201         u16 type;
202
203         dprint(DBG_CAT_MOD, "delete_cat: %s,%u\n", str ? str->name : NULL, cnid);
204         sb = dir->i_sb;
205         hfs_find_init(HFSPLUS_SB(sb).cat_tree, &fd);
206
207         if (!str) {
208                 int len;
209
210                 hfsplus_cat_build_key(fd.search_key, cnid, NULL);
211                 err = hfs_brec_find(&fd);
212                 if (err)
213                         goto out;
214
215                 off = fd.entryoffset + offsetof(struct hfsplus_cat_thread, nodeName);
216                 fd.search_key->cat.parent = cpu_to_be32(dir->i_ino);
217                 hfs_bnode_read(fd.bnode, &fd.search_key->cat.name.length, off, 2);
218                 len = be16_to_cpu(fd.search_key->cat.name.length) * 2;
219                 hfs_bnode_read(fd.bnode, &fd.search_key->cat.name.unicode, off + 2, len);
220                 fd.search_key->key_len = cpu_to_be16(6 + len);
221         } else
222                 hfsplus_cat_build_key(fd.search_key, dir->i_ino, str);
223
224         err = hfs_brec_find(&fd);
225         if (err)
226                 goto out;
227
228         type = hfs_bnode_read_u16(fd.bnode, fd.entryoffset);
229         if (type == HFSPLUS_FILE) {
230 #if 0
231                 off = fd.entryoffset + offsetof(hfsplus_cat_file, data_fork);
232                 hfs_bnode_read(fd.bnode, &fork, off, sizeof(fork));
233                 hfsplus_free_fork(sb, cnid, &fork, HFSPLUS_TYPE_DATA);
234 #endif
235
236                 off = fd.entryoffset + offsetof(struct hfsplus_cat_file, rsrc_fork);
237                 hfs_bnode_read(fd.bnode, &fork, off, sizeof(fork));
238                 hfsplus_free_fork(sb, cnid, &fork, HFSPLUS_TYPE_RSRC);
239         }
240
241         list_for_each(pos, &HFSPLUS_I(dir).open_dir_list) {
242                 struct hfsplus_readdir_data *rd =
243                         list_entry(pos, struct hfsplus_readdir_data, list);
244                 if (fd.tree->keycmp(fd.search_key, (void *)&rd->key) < 0)
245                         rd->file->f_pos--;
246         }
247
248         err = hfs_brec_remove(&fd);
249         if (err)
250                 goto out;
251
252         hfsplus_cat_build_key(fd.search_key, cnid, NULL);
253         err = hfs_brec_find(&fd);
254         if (err)
255                 goto out;
256
257         err = hfs_brec_remove(&fd);
258         if (err)
259                 goto out;
260
261         dir->i_size--;
262         mark_inode_dirty(dir);
263 out:
264         hfs_find_exit(&fd);
265
266         return err;
267 }
268
269 int hfsplus_rename_cat(u32 cnid,
270                        struct inode *src_dir, struct qstr *src_name,
271                        struct inode *dst_dir, struct qstr *dst_name)
272 {
273         struct super_block *sb;
274         struct hfs_find_data src_fd, dst_fd;
275         hfsplus_cat_entry entry;
276         int entry_size, type;
277         int err = 0;
278
279         dprint(DBG_CAT_MOD, "rename_cat: %u - %lu,%s - %lu,%s\n", cnid, src_dir->i_ino, src_name->name,
280                 dst_dir->i_ino, dst_name->name);
281         sb = src_dir->i_sb;
282         hfs_find_init(HFSPLUS_SB(sb).cat_tree, &src_fd);
283         dst_fd = src_fd;
284
285         /* find the old dir entry and read the data */
286         hfsplus_cat_build_key(src_fd.search_key, src_dir->i_ino, src_name);
287         err = hfs_brec_find(&src_fd);
288         if (err)
289                 goto out;
290
291         hfs_bnode_read(src_fd.bnode, &entry, src_fd.entryoffset,
292                                 src_fd.entrylength);
293
294         /* create new dir entry with the data from the old entry */
295         hfsplus_cat_build_key(dst_fd.search_key, dst_dir->i_ino, dst_name);
296         err = hfs_brec_find(&dst_fd);
297         if (err != -ENOENT) {
298                 if (!err)
299                         err = -EEXIST;
300                 goto out;
301         }
302
303         err = hfs_brec_insert(&dst_fd, &entry, src_fd.entrylength);
304         if (err)
305                 goto out;
306         dst_dir->i_size++;
307         mark_inode_dirty(dst_dir);
308
309         /* finally remove the old entry */
310         hfsplus_cat_build_key(src_fd.search_key, src_dir->i_ino, src_name);
311         err = hfs_brec_find(&src_fd);
312         if (err)
313                 goto out;
314         err = hfs_brec_remove(&src_fd);
315         if (err)
316                 goto out;
317         src_dir->i_size--;
318         mark_inode_dirty(src_dir);
319
320         /* remove old thread entry */
321         hfsplus_cat_build_key(src_fd.search_key, cnid, NULL);
322         err = hfs_brec_find(&src_fd);
323         if (err)
324                 goto out;
325         type = hfs_bnode_read_u16(src_fd.bnode, src_fd.entryoffset);
326         err = hfs_brec_remove(&src_fd);
327         if (err)
328                 goto out;
329
330         /* create new thread entry */
331         hfsplus_cat_build_key(dst_fd.search_key, cnid, NULL);
332         entry_size = hfsplus_fill_cat_thread(&entry, type, dst_dir->i_ino, dst_name);
333         err = hfs_brec_find(&dst_fd);
334         if (err != -ENOENT) {
335                 if (!err)
336                         err = -EEXIST;
337                 goto out;
338         }
339         err = hfs_brec_insert(&dst_fd, &entry, entry_size);
340 out:
341         hfs_bnode_put(dst_fd.bnode);
342         hfs_find_exit(&src_fd);
343         return err;
344 }