patch-2_6_7-vs1_9_1_12
[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 err2;
169         }
170         err = hfs_brec_insert(&fd, &entry, entry_size);
171         if (err)
172                 goto err2;
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 err1;
182         }
183         err = hfs_brec_insert(&fd, &entry, entry_size);
184         if (err)
185                 goto err1;
186
187         dir->i_size++;
188         dir->i_mtime = dir->i_ctime = CURRENT_TIME;
189         mark_inode_dirty(dir);
190         hfs_find_exit(&fd);
191         return 0;
192
193 err1:
194         hfsplus_cat_build_key(fd.search_key, cnid, NULL);
195         if (!hfs_brec_find(&fd))
196                 hfs_brec_remove(&fd);
197 err2:
198         hfs_find_exit(&fd);
199         return err;
200 }
201
202 int hfsplus_delete_cat(u32 cnid, struct inode *dir, struct qstr *str)
203 {
204         struct super_block *sb;
205         struct hfs_find_data fd;
206         struct hfsplus_fork_raw fork;
207         struct list_head *pos;
208         int err, off;
209         u16 type;
210
211         dprint(DBG_CAT_MOD, "delete_cat: %s,%u\n", str ? str->name : NULL, cnid);
212         sb = dir->i_sb;
213         hfs_find_init(HFSPLUS_SB(sb).cat_tree, &fd);
214
215         if (!str) {
216                 int len;
217
218                 hfsplus_cat_build_key(fd.search_key, cnid, NULL);
219                 err = hfs_brec_find(&fd);
220                 if (err)
221                         goto out;
222
223                 off = fd.entryoffset + offsetof(struct hfsplus_cat_thread, nodeName);
224                 fd.search_key->cat.parent = cpu_to_be32(dir->i_ino);
225                 hfs_bnode_read(fd.bnode, &fd.search_key->cat.name.length, off, 2);
226                 len = be16_to_cpu(fd.search_key->cat.name.length) * 2;
227                 hfs_bnode_read(fd.bnode, &fd.search_key->cat.name.unicode, off + 2, len);
228                 fd.search_key->key_len = cpu_to_be16(6 + len);
229         } else
230                 hfsplus_cat_build_key(fd.search_key, dir->i_ino, str);
231
232         err = hfs_brec_find(&fd);
233         if (err)
234                 goto out;
235
236         type = hfs_bnode_read_u16(fd.bnode, fd.entryoffset);
237         if (type == HFSPLUS_FILE) {
238 #if 0
239                 off = fd.entryoffset + offsetof(hfsplus_cat_file, data_fork);
240                 hfs_bnode_read(fd.bnode, &fork, off, sizeof(fork));
241                 hfsplus_free_fork(sb, cnid, &fork, HFSPLUS_TYPE_DATA);
242 #endif
243
244                 off = fd.entryoffset + offsetof(struct hfsplus_cat_file, rsrc_fork);
245                 hfs_bnode_read(fd.bnode, &fork, off, sizeof(fork));
246                 hfsplus_free_fork(sb, cnid, &fork, HFSPLUS_TYPE_RSRC);
247         }
248
249         list_for_each(pos, &HFSPLUS_I(dir).open_dir_list) {
250                 struct hfsplus_readdir_data *rd =
251                         list_entry(pos, struct hfsplus_readdir_data, list);
252                 if (fd.tree->keycmp(fd.search_key, (void *)&rd->key) < 0)
253                         rd->file->f_pos--;
254         }
255
256         err = hfs_brec_remove(&fd);
257         if (err)
258                 goto out;
259
260         hfsplus_cat_build_key(fd.search_key, cnid, NULL);
261         err = hfs_brec_find(&fd);
262         if (err)
263                 goto out;
264
265         err = hfs_brec_remove(&fd);
266         if (err)
267                 goto out;
268
269         dir->i_size--;
270         dir->i_mtime = dir->i_ctime = CURRENT_TIME;
271         mark_inode_dirty(dir);
272 out:
273         hfs_find_exit(&fd);
274
275         return err;
276 }
277
278 int hfsplus_rename_cat(u32 cnid,
279                        struct inode *src_dir, struct qstr *src_name,
280                        struct inode *dst_dir, struct qstr *dst_name)
281 {
282         struct super_block *sb;
283         struct hfs_find_data src_fd, dst_fd;
284         hfsplus_cat_entry entry;
285         int entry_size, type;
286         int err = 0;
287
288         dprint(DBG_CAT_MOD, "rename_cat: %u - %lu,%s - %lu,%s\n", cnid, src_dir->i_ino, src_name->name,
289                 dst_dir->i_ino, dst_name->name);
290         sb = src_dir->i_sb;
291         hfs_find_init(HFSPLUS_SB(sb).cat_tree, &src_fd);
292         dst_fd = src_fd;
293
294         /* find the old dir entry and read the data */
295         hfsplus_cat_build_key(src_fd.search_key, src_dir->i_ino, src_name);
296         err = hfs_brec_find(&src_fd);
297         if (err)
298                 goto out;
299
300         hfs_bnode_read(src_fd.bnode, &entry, src_fd.entryoffset,
301                                 src_fd.entrylength);
302
303         /* create new dir entry with the data from the old entry */
304         hfsplus_cat_build_key(dst_fd.search_key, dst_dir->i_ino, dst_name);
305         err = hfs_brec_find(&dst_fd);
306         if (err != -ENOENT) {
307                 if (!err)
308                         err = -EEXIST;
309                 goto out;
310         }
311
312         err = hfs_brec_insert(&dst_fd, &entry, src_fd.entrylength);
313         if (err)
314                 goto out;
315         dst_dir->i_size++;
316         dst_dir->i_mtime = dst_dir->i_ctime = CURRENT_TIME;
317         mark_inode_dirty(dst_dir);
318
319         /* finally remove the old entry */
320         hfsplus_cat_build_key(src_fd.search_key, src_dir->i_ino, src_name);
321         err = hfs_brec_find(&src_fd);
322         if (err)
323                 goto out;
324         err = hfs_brec_remove(&src_fd);
325         if (err)
326                 goto out;
327         src_dir->i_size--;
328         src_dir->i_mtime = src_dir->i_ctime = CURRENT_TIME;
329         mark_inode_dirty(src_dir);
330
331         /* remove old thread entry */
332         hfsplus_cat_build_key(src_fd.search_key, cnid, NULL);
333         err = hfs_brec_find(&src_fd);
334         if (err)
335                 goto out;
336         type = hfs_bnode_read_u16(src_fd.bnode, src_fd.entryoffset);
337         err = hfs_brec_remove(&src_fd);
338         if (err)
339                 goto out;
340
341         /* create new thread entry */
342         hfsplus_cat_build_key(dst_fd.search_key, cnid, NULL);
343         entry_size = hfsplus_fill_cat_thread(&entry, type, dst_dir->i_ino, dst_name);
344         err = hfs_brec_find(&dst_fd);
345         if (err != -ENOENT) {
346                 if (!err)
347                         err = -EEXIST;
348                 goto out;
349         }
350         err = hfs_brec_insert(&dst_fd, &entry, entry_size);
351 out:
352         hfs_bnode_put(dst_fd.bnode);
353         hfs_find_exit(&src_fd);
354         return err;
355 }