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