blob: 9569b39257ec0c94f8cdf914850be6d8027ce08c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 */
Roman Zippel328b9222005-09-06 15:18:49 -070023void hfs_cat_build_key(struct super_block *sb, btree_key *key, u32 parent, struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -070024{
25 key->cat.reserved = 0;
26 key->cat.ParID = cpu_to_be32(parent);
27 if (name) {
Roman Zippel328b9222005-09-06 15:18:49 -070028 hfs_asc2mac(sb, &key->cat.CName, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 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
36static int hfs_cat_build_record(hfs_cat_rec *rec, u32 cnid, struct inode *inode)
37{
38 __be32 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
Roman Zippel328b9222005-09-06 15:18:49 -070065static int hfs_cat_build_thread(struct super_block *sb,
66 hfs_cat_rec *rec, int type,
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 u32 parentid, struct qstr *name)
68{
69 rec->type = type;
70 memset(rec->thread.reserved, 0, sizeof(rec->thread.reserved));
71 rec->thread.ParID = cpu_to_be32(parentid);
Roman Zippel328b9222005-09-06 15:18:49 -070072 hfs_asc2mac(sb, &rec->thread.CName, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 return sizeof(struct hfs_cat_thread);
74}
75
76/*
77 * create_entry()
78 *
79 * Add a new file or directory to the catalog B-tree and
80 * return a (struct hfs_cat_entry) for it in '*result'.
81 */
82int hfs_cat_create(u32 cnid, struct inode *dir, struct qstr *str, struct inode *inode)
83{
84 struct hfs_find_data fd;
85 struct super_block *sb;
86 union hfs_cat_rec entry;
87 int entry_size;
88 int err;
89
90 dprint(DBG_CAT_MOD, "create_cat: %s,%u(%d)\n", str->name, cnid, inode->i_nlink);
91 if (dir->i_size >= HFS_MAX_VALENCE)
92 return -ENOSPC;
93
94 sb = dir->i_sb;
Alexey Khoroshilov9509f172013-04-30 15:27:52 -070095 err = hfs_find_init(HFS_SB(sb)->cat_tree, &fd);
96 if (err)
97 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Roman Zippel328b9222005-09-06 15:18:49 -070099 hfs_cat_build_key(sb, fd.search_key, cnid, NULL);
100 entry_size = hfs_cat_build_thread(sb, &entry, S_ISDIR(inode->i_mode) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 HFS_CDR_THD : HFS_CDR_FTH,
102 dir->i_ino, str);
103 err = hfs_brec_find(&fd);
104 if (err != -ENOENT) {
105 if (!err)
106 err = -EEXIST;
107 goto err2;
108 }
109 err = hfs_brec_insert(&fd, &entry, entry_size);
110 if (err)
111 goto err2;
112
Roman Zippel328b9222005-09-06 15:18:49 -0700113 hfs_cat_build_key(sb, fd.search_key, dir->i_ino, str);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 entry_size = hfs_cat_build_record(&entry, cnid, inode);
115 err = hfs_brec_find(&fd);
116 if (err != -ENOENT) {
117 /* panic? */
118 if (!err)
119 err = -EEXIST;
120 goto err1;
121 }
122 err = hfs_brec_insert(&fd, &entry, entry_size);
123 if (err)
124 goto err1;
125
126 dir->i_size++;
127 dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
128 mark_inode_dirty(dir);
129 hfs_find_exit(&fd);
130 return 0;
131
132err1:
Roman Zippel328b9222005-09-06 15:18:49 -0700133 hfs_cat_build_key(sb, fd.search_key, cnid, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 if (!hfs_brec_find(&fd))
135 hfs_brec_remove(&fd);
136err2:
137 hfs_find_exit(&fd);
138 return err;
139}
140
141/*
142 * hfs_cat_compare()
143 *
144 * Description:
145 * This is the comparison function used for the catalog B-tree. In
146 * comparing catalog B-tree entries, the parent id is the most
147 * significant field (compared as unsigned ints). The name field is
148 * the least significant (compared in "Macintosh lexical order",
149 * see hfs_strcmp() in string.c)
150 * Input Variable(s):
151 * struct hfs_cat_key *key1: pointer to the first key to compare
152 * struct hfs_cat_key *key2: pointer to the second key to compare
153 * Output Variable(s):
154 * NONE
155 * Returns:
156 * int: negative if key1<key2, positive if key1>key2, and 0 if key1==key2
157 * Preconditions:
158 * key1 and key2 point to "valid" (struct hfs_cat_key)s.
159 * Postconditions:
160 * This function has no side-effects
161 */
162int hfs_cat_keycmp(const btree_key *key1, const btree_key *key2)
163{
164 int retval;
165
166 retval = be32_to_cpu(key1->cat.ParID) - be32_to_cpu(key2->cat.ParID);
167 if (!retval)
168 retval = hfs_strcmp(key1->cat.CName.name, key1->cat.CName.len,
169 key2->cat.CName.name, key2->cat.CName.len);
170
171 return retval;
172}
173
174/* Try to get a catalog entry for given catalog id */
175// move to read_super???
176int hfs_cat_find_brec(struct super_block *sb, u32 cnid,
177 struct hfs_find_data *fd)
178{
179 hfs_cat_rec rec;
180 int res, len, type;
181
Roman Zippel328b9222005-09-06 15:18:49 -0700182 hfs_cat_build_key(sb, fd->search_key, cnid, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 res = hfs_brec_read(fd, &rec, sizeof(rec));
184 if (res)
185 return res;
186
187 type = rec.type;
188 if (type != HFS_CDR_THD && type != HFS_CDR_FTH) {
Roman Zippel7cf3cc32006-01-18 17:43:07 -0800189 printk(KERN_ERR "hfs: found bad thread record in catalog\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 return -EIO;
191 }
192
193 fd->search_key->cat.ParID = rec.thread.ParID;
194 len = fd->search_key->cat.CName.len = rec.thread.CName.len;
Eric Sesterhennd38b7aa2008-10-15 22:04:11 -0700195 if (len > HFS_NAMELEN) {
196 printk(KERN_ERR "hfs: bad catalog namelength\n");
197 return -EIO;
198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 memcpy(fd->search_key->cat.CName.name, rec.thread.CName.name, len);
200 return hfs_brec_find(fd);
201}
202
203
204/*
205 * hfs_cat_delete()
206 *
207 * Delete the indicated file or directory.
208 * The associated thread is also removed unless ('with_thread'==0).
209 */
210int hfs_cat_delete(u32 cnid, struct inode *dir, struct qstr *str)
211{
212 struct super_block *sb;
213 struct hfs_find_data fd;
214 struct list_head *pos;
215 int res, type;
216
217 dprint(DBG_CAT_MOD, "delete_cat: %s,%u\n", str ? str->name : NULL, cnid);
218 sb = dir->i_sb;
Alexey Khoroshilov9509f172013-04-30 15:27:52 -0700219 res = hfs_find_init(HFS_SB(sb)->cat_tree, &fd);
220 if (res)
221 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Roman Zippel328b9222005-09-06 15:18:49 -0700223 hfs_cat_build_key(sb, fd.search_key, dir->i_ino, str);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 res = hfs_brec_find(&fd);
225 if (res)
226 goto out;
227
228 type = hfs_bnode_read_u8(fd.bnode, fd.entryoffset);
229 if (type == HFS_CDR_FIL) {
230 struct hfs_cat_file file;
231 hfs_bnode_read(fd.bnode, &file, fd.entryoffset, sizeof(file));
232 if (be32_to_cpu(file.FlNum) == cnid) {
233#if 0
234 hfs_free_fork(sb, &file, HFS_FK_DATA);
235#endif
236 hfs_free_fork(sb, &file, HFS_FK_RSRC);
237 }
238 }
239
240 list_for_each(pos, &HFS_I(dir)->open_dir_list) {
241 struct hfs_readdir_data *rd =
242 list_entry(pos, struct hfs_readdir_data, list);
243 if (fd.tree->keycmp(fd.search_key, (void *)&rd->key) < 0)
244 rd->file->f_pos--;
245 }
246
247 res = hfs_brec_remove(&fd);
248 if (res)
249 goto out;
250
Roman Zippel328b9222005-09-06 15:18:49 -0700251 hfs_cat_build_key(sb, fd.search_key, cnid, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 res = hfs_brec_find(&fd);
253 if (!res) {
254 res = hfs_brec_remove(&fd);
255 if (res)
256 goto out;
257 }
258
259 dir->i_size--;
260 dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
261 mark_inode_dirty(dir);
262 res = 0;
263out:
264 hfs_find_exit(&fd);
265
266 return res;
267}
268
269/*
270 * hfs_cat_move()
271 *
272 * Rename a file or directory, possibly to a new directory.
273 * If the destination exists it is removed and a
274 * (struct hfs_cat_entry) for it is returned in '*result'.
275 */
276int hfs_cat_move(u32 cnid, struct inode *src_dir, struct qstr *src_name,
277 struct inode *dst_dir, struct qstr *dst_name)
278{
279 struct super_block *sb;
280 struct hfs_find_data src_fd, dst_fd;
281 union hfs_cat_rec entry;
282 int entry_size, type;
283 int err;
284
285 dprint(DBG_CAT_MOD, "rename_cat: %u - %lu,%s - %lu,%s\n", cnid, src_dir->i_ino, src_name->name,
286 dst_dir->i_ino, dst_name->name);
287 sb = src_dir->i_sb;
Alexey Khoroshilov9509f172013-04-30 15:27:52 -0700288 err = hfs_find_init(HFS_SB(sb)->cat_tree, &src_fd);
289 if (err)
290 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 dst_fd = src_fd;
292
293 /* find the old dir entry and read the data */
Roman Zippel328b9222005-09-06 15:18:49 -0700294 hfs_cat_build_key(sb, src_fd.search_key, src_dir->i_ino, src_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 err = hfs_brec_find(&src_fd);
296 if (err)
297 goto out;
Amerigo Wangec81aec2009-12-14 17:57:37 -0800298 if (src_fd.entrylength > sizeof(entry) || src_fd.entrylength < 0) {
299 err = -EIO;
300 goto out;
301 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
303 hfs_bnode_read(src_fd.bnode, &entry, src_fd.entryoffset,
304 src_fd.entrylength);
305
306 /* create new dir entry with the data from the old entry */
Roman Zippel328b9222005-09-06 15:18:49 -0700307 hfs_cat_build_key(sb, dst_fd.search_key, dst_dir->i_ino, dst_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 err = hfs_brec_find(&dst_fd);
309 if (err != -ENOENT) {
310 if (!err)
311 err = -EEXIST;
312 goto out;
313 }
314
315 err = hfs_brec_insert(&dst_fd, &entry, src_fd.entrylength);
316 if (err)
317 goto out;
318 dst_dir->i_size++;
319 dst_dir->i_mtime = dst_dir->i_ctime = CURRENT_TIME_SEC;
320 mark_inode_dirty(dst_dir);
321
322 /* finally remove the old entry */
Roman Zippel328b9222005-09-06 15:18:49 -0700323 hfs_cat_build_key(sb, src_fd.search_key, src_dir->i_ino, src_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 err = hfs_brec_find(&src_fd);
325 if (err)
326 goto out;
327 err = hfs_brec_remove(&src_fd);
328 if (err)
329 goto out;
330 src_dir->i_size--;
331 src_dir->i_mtime = src_dir->i_ctime = CURRENT_TIME_SEC;
332 mark_inode_dirty(src_dir);
333
334 type = entry.type;
335 if (type == HFS_CDR_FIL && !(entry.file.Flags & HFS_FIL_THD))
336 goto out;
337
338 /* remove old thread entry */
Roman Zippel328b9222005-09-06 15:18:49 -0700339 hfs_cat_build_key(sb, src_fd.search_key, cnid, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 err = hfs_brec_find(&src_fd);
341 if (err)
342 goto out;
343 err = hfs_brec_remove(&src_fd);
344 if (err)
345 goto out;
346
347 /* create new thread entry */
Roman Zippel328b9222005-09-06 15:18:49 -0700348 hfs_cat_build_key(sb, dst_fd.search_key, cnid, NULL);
349 entry_size = hfs_cat_build_thread(sb, &entry, type == HFS_CDR_FIL ? HFS_CDR_FTH : HFS_CDR_THD,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 dst_dir->i_ino, dst_name);
351 err = hfs_brec_find(&dst_fd);
352 if (err != -ENOENT) {
353 if (!err)
354 err = -EEXIST;
355 goto out;
356 }
357 err = hfs_brec_insert(&dst_fd, &entry, entry_size);
358out:
359 hfs_bnode_put(dst_fd.bnode);
360 hfs_find_exit(&src_fd);
361 return err;
362}