patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / fs / hfs / catalog.c
1 /*
2  *  linux/fs/hfs/catalog.c
3  *
4  * Copyright (C) 1995-1997  Paul H. Hargrove
5  * (C) 2003 Ardis Technologies <roman@ardistech.com>
6  * This file may be distributed under the terms of the GNU General Public License.
7  *
8  * This file contains the functions related to the catalog B-tree.
9  *
10  * Cache code shamelessly stolen from
11  *     linux/fs/inode.c Copyright (C) 1991, 1992  Linus Torvalds
12  *     re-shamelessly stolen Copyright (C) 1997 Linus Torvalds
13  */
14
15 #include "hfs_fs.h"
16 #include "btree.h"
17
18 /*
19  * hfs_cat_build_key()
20  *
21  * Given the ID of the parent and the name build a search key.
22  */
23 void hfs_cat_build_key(btree_key *key, u32 parent, struct qstr *name)
24 {
25         key->cat.reserved = 0;
26         key->cat.ParID = cpu_to_be32(parent);
27         if (name) {
28                 hfs_triv2mac(&key->cat.CName, name);
29                 key->key_len = 6 + key->cat.CName.len;
30         } else {
31                 memset(&key->cat.CName, 0, sizeof(struct hfs_name));
32                 key->key_len = 6;
33         }
34 }
35
36 int hfs_cat_build_record(hfs_cat_rec *rec, u32 cnid, struct inode *inode)
37 {
38         u32 mtime = hfs_mtime();
39
40         memset(rec, 0, sizeof(*rec));
41         if (S_ISDIR(inode->i_mode)) {
42                 rec->type = HFS_CDR_DIR;
43                 rec->dir.DirID = cpu_to_be32(cnid);
44                 rec->dir.CrDat = mtime;
45                 rec->dir.MdDat = mtime;
46                 rec->dir.BkDat = 0;
47                 rec->dir.UsrInfo.frView = cpu_to_be16(0xff);
48                 return sizeof(struct hfs_cat_dir);
49         } else {
50                 /* init some fields for the file record */
51                 rec->type = HFS_CDR_FIL;
52                 rec->file.Flags = HFS_FIL_USED | HFS_FIL_THD;
53                 if (!(inode->i_mode & S_IWUSR))
54                         rec->file.Flags |= HFS_FIL_LOCK;
55                 rec->file.FlNum = cpu_to_be32(cnid);
56                 rec->file.CrDat = mtime;
57                 rec->file.MdDat = mtime;
58                 rec->file.BkDat = 0;
59                 rec->file.UsrWds.fdType = HFS_SB(inode->i_sb)->s_type;
60                 rec->file.UsrWds.fdCreator = HFS_SB(inode->i_sb)->s_creator;
61                 return sizeof(struct hfs_cat_file);
62         }
63 }
64
65 static int hfs_cat_build_thread(hfs_cat_rec *rec, int type,
66                                 u32 parentid, struct qstr *name)
67 {
68         rec->type = type;
69         memset(rec->thread.reserved, 0, sizeof(rec->thread.reserved));
70         rec->thread.ParID = cpu_to_be32(parentid);
71         hfs_triv2mac(&rec->thread.CName, name);
72         return sizeof(struct hfs_cat_thread);
73 }
74
75 /*
76  * create_entry()
77  *
78  * Add a new file or directory to the catalog B-tree and
79  * return a (struct hfs_cat_entry) for it in '*result'.
80  */
81 int hfs_cat_create(u32 cnid, struct inode *dir, struct qstr *str, struct inode *inode)
82 {
83         struct hfs_find_data fd;
84         struct super_block *sb;
85         union hfs_cat_rec entry;
86         int entry_size;
87         int err;
88
89         dprint(DBG_CAT_MOD, "create_cat: %s,%u(%d)\n", str->name, cnid, inode->i_nlink);
90         if (dir->i_size >= HFS_MAX_VALENCE)
91                 return -ENOSPC;
92
93         sb = dir->i_sb;
94         hfs_find_init(HFS_SB(sb)->cat_tree, &fd);
95
96         hfs_cat_build_key(fd.search_key, cnid, NULL);
97         entry_size = hfs_cat_build_thread(&entry, S_ISDIR(inode->i_mode) ?
98                         HFS_CDR_THD : HFS_CDR_FTH,
99                         dir->i_ino, str);
100         err = hfs_brec_find(&fd);
101         if (err != -ENOENT) {
102                 if (!err)
103                         err = -EEXIST;
104                 goto err2;
105         }
106         err = hfs_brec_insert(&fd, &entry, entry_size);
107         if (err)
108                 goto err2;
109
110         hfs_cat_build_key(fd.search_key, dir->i_ino, str);
111         entry_size = hfs_cat_build_record(&entry, cnid, inode);
112         err = hfs_brec_find(&fd);
113         if (err != -ENOENT) {
114                 /* panic? */
115                 if (!err)
116                         err = -EEXIST;
117                 goto err1;
118         }
119         err = hfs_brec_insert(&fd, &entry, entry_size);
120         if (err)
121                 goto err1;
122
123         dir->i_size++;
124         dir->i_mtime = dir->i_ctime = CURRENT_TIME;
125         mark_inode_dirty(dir);
126         hfs_find_exit(&fd);
127         return 0;
128
129 err1:
130         hfs_cat_build_key(fd.search_key, cnid, NULL);
131         if (!hfs_brec_find(&fd))
132                 hfs_brec_remove(&fd);
133 err2:
134         hfs_find_exit(&fd);
135         return err;
136 }
137
138 /*
139  * hfs_cat_compare()
140  *
141  * Description:
142  *   This is the comparison function used for the catalog B-tree.  In
143  *   comparing catalog B-tree entries, the parent id is the most
144  *   significant field (compared as unsigned ints).  The name field is
145  *   the least significant (compared in "Macintosh lexical order",
146  *   see hfs_strcmp() in string.c)
147  * Input Variable(s):
148  *   struct hfs_cat_key *key1: pointer to the first key to compare
149  *   struct hfs_cat_key *key2: pointer to the second key to compare
150  * Output Variable(s):
151  *   NONE
152  * Returns:
153  *   int: negative if key1<key2, positive if key1>key2, and 0 if key1==key2
154  * Preconditions:
155  *   key1 and key2 point to "valid" (struct hfs_cat_key)s.
156  * Postconditions:
157  *   This function has no side-effects
158  */
159 int hfs_cat_keycmp(const btree_key *key1, const btree_key *key2)
160 {
161         int retval;
162
163         retval = be32_to_cpu(key1->cat.ParID) - be32_to_cpu(key2->cat.ParID);
164         if (!retval)
165                 retval = hfs_strcmp(key1->cat.CName.name, key1->cat.CName.len,
166                                     key2->cat.CName.name, key2->cat.CName.len);
167
168         return retval;
169 }
170
171 /* Try to get a catalog entry for given catalog id */
172 // move to read_super???
173 int hfs_cat_find_brec(struct super_block *sb, u32 cnid,
174                       struct hfs_find_data *fd)
175 {
176         hfs_cat_rec rec;
177         int res, len, type;
178
179         hfs_cat_build_key(fd->search_key, cnid, NULL);
180         res = hfs_brec_read(fd, &rec, sizeof(rec));
181         if (res)
182                 return res;
183
184         type = rec.type;
185         if (type != HFS_CDR_THD && type != HFS_CDR_FTH) {
186                 printk("HFS-fs: Found bad thread record in catalog\n");
187                 return -EIO;
188         }
189
190         fd->search_key->cat.ParID = rec.thread.ParID;
191         len = fd->search_key->cat.CName.len = rec.thread.CName.len;
192         memcpy(fd->search_key->cat.CName.name, rec.thread.CName.name, len);
193         return hfs_brec_find(fd);
194 }
195
196
197 /*
198  * hfs_cat_delete()
199  *
200  * Delete the indicated file or directory.
201  * The associated thread is also removed unless ('with_thread'==0).
202  */
203 int hfs_cat_delete(u32 cnid, struct inode *dir, struct qstr *str)
204 {
205         struct super_block *sb;
206         struct hfs_find_data fd;
207         struct list_head *pos;
208         int res, type;
209
210         dprint(DBG_CAT_MOD, "delete_cat: %s,%u\n", str ? str->name : NULL, cnid);
211         sb = dir->i_sb;
212         hfs_find_init(HFS_SB(sb)->cat_tree, &fd);
213
214         hfs_cat_build_key(fd.search_key, dir->i_ino, str);
215         res = hfs_brec_find(&fd);
216         if (res)
217                 goto out;
218
219         type = hfs_bnode_read_u8(fd.bnode, fd.entryoffset);
220         if (type == HFS_CDR_FIL) {
221                 struct hfs_cat_file file;
222                 hfs_bnode_read(fd.bnode, &file, fd.entryoffset, sizeof(file));
223                 if (be32_to_cpu(file.FlNum) == cnid) {
224 #if 0
225                         hfs_free_fork(sb, &file, HFS_FK_DATA);
226 #endif
227                         hfs_free_fork(sb, &file, HFS_FK_RSRC);
228                 }
229         }
230
231         list_for_each(pos, &HFS_I(dir)->open_dir_list) {
232                 struct hfs_readdir_data *rd =
233                         list_entry(pos, struct hfs_readdir_data, list);
234                 if (fd.tree->keycmp(fd.search_key, (void *)&rd->key) < 0)
235                         rd->file->f_pos--;
236         }
237
238         res = hfs_brec_remove(&fd);
239         if (res)
240                 goto out;
241
242         hfs_cat_build_key(fd.search_key, cnid, NULL);
243         res = hfs_brec_find(&fd);
244         if (!res) {
245                 res = hfs_brec_remove(&fd);
246                 if (res)
247                         goto out;
248         }
249
250         dir->i_size--;
251         dir->i_mtime = dir->i_ctime = CURRENT_TIME;
252         mark_inode_dirty(dir);
253         res = 0;
254 out:
255         hfs_find_exit(&fd);
256
257         return res;
258 }
259
260 /*
261  * hfs_cat_move()
262  *
263  * Rename a file or directory, possibly to a new directory.
264  * If the destination exists it is removed and a
265  * (struct hfs_cat_entry) for it is returned in '*result'.
266  */
267 int hfs_cat_move(u32 cnid, struct inode *src_dir, struct qstr *src_name,
268                  struct inode *dst_dir, struct qstr *dst_name)
269 {
270         struct super_block *sb;
271         struct hfs_find_data src_fd, dst_fd;
272         union hfs_cat_rec entry;
273         int entry_size, type;
274         int err;
275
276         dprint(DBG_CAT_MOD, "rename_cat: %u - %lu,%s - %lu,%s\n", cnid, src_dir->i_ino, src_name->name,
277                 dst_dir->i_ino, dst_name->name);
278         sb = src_dir->i_sb;
279         hfs_find_init(HFS_SB(sb)->cat_tree, &src_fd);
280         dst_fd = src_fd;
281
282         /* find the old dir entry and read the data */
283         hfs_cat_build_key(src_fd.search_key, src_dir->i_ino, src_name);
284         err = hfs_brec_find(&src_fd);
285         if (err)
286                 goto out;
287
288         hfs_bnode_read(src_fd.bnode, &entry, src_fd.entryoffset,
289                             src_fd.entrylength);
290
291         /* create new dir entry with the data from the old entry */
292         hfs_cat_build_key(dst_fd.search_key, dst_dir->i_ino, dst_name);
293         err = hfs_brec_find(&dst_fd);
294         if (err != -ENOENT) {
295                 if (!err)
296                         err = -EEXIST;
297                 goto out;
298         }
299
300         err = hfs_brec_insert(&dst_fd, &entry, src_fd.entrylength);
301         if (err)
302                 goto out;
303         dst_dir->i_size++;
304         dst_dir->i_mtime = dst_dir->i_ctime = CURRENT_TIME;
305         mark_inode_dirty(dst_dir);
306
307         /* finally remove the old entry */
308         hfs_cat_build_key(src_fd.search_key, src_dir->i_ino, src_name);
309         err = hfs_brec_find(&src_fd);
310         if (err)
311                 goto out;
312         err = hfs_brec_remove(&src_fd);
313         if (err)
314                 goto out;
315         src_dir->i_size--;
316         src_dir->i_mtime = src_dir->i_ctime = CURRENT_TIME;
317         mark_inode_dirty(src_dir);
318
319         type = entry.type;
320         if (type == HFS_CDR_FIL && !(entry.file.Flags & HFS_FIL_THD))
321                 goto out;
322
323         /* remove old thread entry */
324         hfs_cat_build_key(src_fd.search_key, cnid, NULL);
325         err = hfs_brec_find(&src_fd);
326         if (err)
327                 goto out;
328         err = hfs_brec_remove(&src_fd);
329         if (err)
330                 goto out;
331
332         /* create new thread entry */
333         hfs_cat_build_key(dst_fd.search_key, cnid, NULL);
334         entry_size = hfs_cat_build_thread(&entry, type == HFS_CDR_FIL ? HFS_CDR_FTH : HFS_CDR_THD,
335                                         dst_dir->i_ino, dst_name);
336         err = hfs_brec_find(&dst_fd);
337         if (err != -ENOENT) {
338                 if (!err)
339                         err = -EEXIST;
340                 goto out;
341         }
342         err = hfs_brec_insert(&dst_fd, &entry, entry_size);
343 out:
344         hfs_bnode_put(dst_fd.bnode);
345         hfs_find_exit(&src_fd);
346         return err;
347 }