Merge tag 'iio-fixes-for-4.6d' of git://git.kernel.org/pub/scm/linux/kernel/git/jic23...
[deliverable/linux.git] / fs / hfs / catalog.c
CommitLineData
1da177e4
LT
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 */
328b9227 23void hfs_cat_build_key(struct super_block *sb, btree_key *key, u32 parent, struct qstr *name)
1da177e4
LT
24{
25 key->cat.reserved = 0;
26 key->cat.ParID = cpu_to_be32(parent);
27 if (name) {
328b9227 28 hfs_asc2mac(sb, &key->cat.CName, name);
1da177e4
LT
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
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
328b9227
RZ
65static int hfs_cat_build_thread(struct super_block *sb,
66 hfs_cat_rec *rec, int type,
1da177e4
LT
67 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);
328b9227 72 hfs_asc2mac(sb, &rec->thread.CName, name);
1da177e4
LT
73 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
c2b3e1f7
JP
90 hfs_dbg(CAT_MOD, "create_cat: %s,%u(%d)\n",
91 str->name, cnid, inode->i_nlink);
1da177e4
LT
92 if (dir->i_size >= HFS_MAX_VALENCE)
93 return -ENOSPC;
94
95 sb = dir->i_sb;
9509f178
AK
96 err = hfs_find_init(HFS_SB(sb)->cat_tree, &fd);
97 if (err)
98 return err;
1da177e4 99
328b9227
RZ
100 hfs_cat_build_key(sb, fd.search_key, cnid, NULL);
101 entry_size = hfs_cat_build_thread(sb, &entry, S_ISDIR(inode->i_mode) ?
1da177e4
LT
102 HFS_CDR_THD : HFS_CDR_FTH,
103 dir->i_ino, str);
104 err = hfs_brec_find(&fd);
105 if (err != -ENOENT) {
106 if (!err)
107 err = -EEXIST;
108 goto err2;
109 }
110 err = hfs_brec_insert(&fd, &entry, entry_size);
111 if (err)
112 goto err2;
113
328b9227 114 hfs_cat_build_key(sb, fd.search_key, dir->i_ino, str);
1da177e4
LT
115 entry_size = hfs_cat_build_record(&entry, cnid, inode);
116 err = hfs_brec_find(&fd);
117 if (err != -ENOENT) {
118 /* panic? */
119 if (!err)
120 err = -EEXIST;
121 goto err1;
122 }
123 err = hfs_brec_insert(&fd, &entry, entry_size);
124 if (err)
125 goto err1;
126
127 dir->i_size++;
128 dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
129 mark_inode_dirty(dir);
130 hfs_find_exit(&fd);
131 return 0;
132
133err1:
328b9227 134 hfs_cat_build_key(sb, fd.search_key, cnid, NULL);
1da177e4
LT
135 if (!hfs_brec_find(&fd))
136 hfs_brec_remove(&fd);
137err2:
138 hfs_find_exit(&fd);
139 return err;
140}
141
142/*
143 * hfs_cat_compare()
144 *
145 * Description:
146 * This is the comparison function used for the catalog B-tree. In
147 * comparing catalog B-tree entries, the parent id is the most
148 * significant field (compared as unsigned ints). The name field is
149 * the least significant (compared in "Macintosh lexical order",
150 * see hfs_strcmp() in string.c)
151 * Input Variable(s):
152 * struct hfs_cat_key *key1: pointer to the first key to compare
153 * struct hfs_cat_key *key2: pointer to the second key to compare
154 * Output Variable(s):
155 * NONE
156 * Returns:
157 * int: negative if key1<key2, positive if key1>key2, and 0 if key1==key2
158 * Preconditions:
159 * key1 and key2 point to "valid" (struct hfs_cat_key)s.
160 * Postconditions:
161 * This function has no side-effects
162 */
163int hfs_cat_keycmp(const btree_key *key1, const btree_key *key2)
164{
ddbc22e2 165 __be32 k1p, k2p;
1da177e4 166
ddbc22e2
RV
167 k1p = key1->cat.ParID;
168 k2p = key2->cat.ParID;
1da177e4 169
ddbc22e2
RV
170 if (k1p != k2p)
171 return be32_to_cpu(k1p) < be32_to_cpu(k2p) ? -1 : 1;
172
173 return hfs_strcmp(key1->cat.CName.name, key1->cat.CName.len,
174 key2->cat.CName.name, key2->cat.CName.len);
1da177e4
LT
175}
176
177/* Try to get a catalog entry for given catalog id */
178// move to read_super???
179int hfs_cat_find_brec(struct super_block *sb, u32 cnid,
180 struct hfs_find_data *fd)
181{
182 hfs_cat_rec rec;
183 int res, len, type;
184
328b9227 185 hfs_cat_build_key(sb, fd->search_key, cnid, NULL);
1da177e4
LT
186 res = hfs_brec_read(fd, &rec, sizeof(rec));
187 if (res)
188 return res;
189
190 type = rec.type;
191 if (type != HFS_CDR_THD && type != HFS_CDR_FTH) {
d6142673 192 pr_err("found bad thread record in catalog\n");
1da177e4
LT
193 return -EIO;
194 }
195
196 fd->search_key->cat.ParID = rec.thread.ParID;
197 len = fd->search_key->cat.CName.len = rec.thread.CName.len;
d38b7aa7 198 if (len > HFS_NAMELEN) {
d6142673 199 pr_err("bad catalog namelength\n");
d38b7aa7
ES
200 return -EIO;
201 }
1da177e4
LT
202 memcpy(fd->search_key->cat.CName.name, rec.thread.CName.name, len);
203 return hfs_brec_find(fd);
204}
205
206
207/*
208 * hfs_cat_delete()
209 *
210 * Delete the indicated file or directory.
211 * The associated thread is also removed unless ('with_thread'==0).
212 */
213int hfs_cat_delete(u32 cnid, struct inode *dir, struct qstr *str)
214{
215 struct super_block *sb;
216 struct hfs_find_data fd;
2c35dea2 217 struct hfs_readdir_data *rd;
1da177e4
LT
218 int res, type;
219
c2b3e1f7 220 hfs_dbg(CAT_MOD, "delete_cat: %s,%u\n", str ? str->name : NULL, cnid);
1da177e4 221 sb = dir->i_sb;
9509f178
AK
222 res = hfs_find_init(HFS_SB(sb)->cat_tree, &fd);
223 if (res)
224 return res;
1da177e4 225
328b9227 226 hfs_cat_build_key(sb, fd.search_key, dir->i_ino, str);
1da177e4
LT
227 res = hfs_brec_find(&fd);
228 if (res)
229 goto out;
230
231 type = hfs_bnode_read_u8(fd.bnode, fd.entryoffset);
232 if (type == HFS_CDR_FIL) {
233 struct hfs_cat_file file;
234 hfs_bnode_read(fd.bnode, &file, fd.entryoffset, sizeof(file));
235 if (be32_to_cpu(file.FlNum) == cnid) {
236#if 0
237 hfs_free_fork(sb, &file, HFS_FK_DATA);
238#endif
239 hfs_free_fork(sb, &file, HFS_FK_RSRC);
240 }
241 }
242
2c35dea2 243 list_for_each_entry(rd, &HFS_I(dir)->open_dir_list, list) {
1da177e4
LT
244 if (fd.tree->keycmp(fd.search_key, (void *)&rd->key) < 0)
245 rd->file->f_pos--;
246 }
247
248 res = hfs_brec_remove(&fd);
249 if (res)
250 goto out;
251
328b9227 252 hfs_cat_build_key(sb, fd.search_key, cnid, NULL);
1da177e4
LT
253 res = hfs_brec_find(&fd);
254 if (!res) {
255 res = hfs_brec_remove(&fd);
256 if (res)
257 goto out;
258 }
259
260 dir->i_size--;
261 dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
262 mark_inode_dirty(dir);
263 res = 0;
264out:
265 hfs_find_exit(&fd);
266
267 return res;
268}
269
270/*
271 * hfs_cat_move()
272 *
273 * Rename a file or directory, possibly to a new directory.
274 * If the destination exists it is removed and a
275 * (struct hfs_cat_entry) for it is returned in '*result'.
276 */
277int hfs_cat_move(u32 cnid, struct inode *src_dir, struct qstr *src_name,
278 struct inode *dst_dir, struct qstr *dst_name)
279{
280 struct super_block *sb;
281 struct hfs_find_data src_fd, dst_fd;
282 union hfs_cat_rec entry;
283 int entry_size, type;
284 int err;
285
c2b3e1f7
JP
286 hfs_dbg(CAT_MOD, "rename_cat: %u - %lu,%s - %lu,%s\n",
287 cnid, src_dir->i_ino, src_name->name,
1da177e4
LT
288 dst_dir->i_ino, dst_name->name);
289 sb = src_dir->i_sb;
9509f178
AK
290 err = hfs_find_init(HFS_SB(sb)->cat_tree, &src_fd);
291 if (err)
292 return err;
1da177e4
LT
293 dst_fd = src_fd;
294
295 /* find the old dir entry and read the data */
328b9227 296 hfs_cat_build_key(sb, src_fd.search_key, src_dir->i_ino, src_name);
1da177e4
LT
297 err = hfs_brec_find(&src_fd);
298 if (err)
299 goto out;
ec81aecb
AW
300 if (src_fd.entrylength > sizeof(entry) || src_fd.entrylength < 0) {
301 err = -EIO;
302 goto out;
303 }
1da177e4
LT
304
305 hfs_bnode_read(src_fd.bnode, &entry, src_fd.entryoffset,
306 src_fd.entrylength);
307
308 /* create new dir entry with the data from the old entry */
328b9227 309 hfs_cat_build_key(sb, dst_fd.search_key, dst_dir->i_ino, dst_name);
1da177e4
LT
310 err = hfs_brec_find(&dst_fd);
311 if (err != -ENOENT) {
312 if (!err)
313 err = -EEXIST;
314 goto out;
315 }
316
317 err = hfs_brec_insert(&dst_fd, &entry, src_fd.entrylength);
318 if (err)
319 goto out;
320 dst_dir->i_size++;
321 dst_dir->i_mtime = dst_dir->i_ctime = CURRENT_TIME_SEC;
322 mark_inode_dirty(dst_dir);
323
324 /* finally remove the old entry */
328b9227 325 hfs_cat_build_key(sb, src_fd.search_key, src_dir->i_ino, src_name);
1da177e4
LT
326 err = hfs_brec_find(&src_fd);
327 if (err)
328 goto out;
329 err = hfs_brec_remove(&src_fd);
330 if (err)
331 goto out;
332 src_dir->i_size--;
333 src_dir->i_mtime = src_dir->i_ctime = CURRENT_TIME_SEC;
334 mark_inode_dirty(src_dir);
335
336 type = entry.type;
337 if (type == HFS_CDR_FIL && !(entry.file.Flags & HFS_FIL_THD))
338 goto out;
339
340 /* remove old thread entry */
328b9227 341 hfs_cat_build_key(sb, src_fd.search_key, cnid, NULL);
1da177e4
LT
342 err = hfs_brec_find(&src_fd);
343 if (err)
344 goto out;
345 err = hfs_brec_remove(&src_fd);
346 if (err)
347 goto out;
348
349 /* create new thread entry */
328b9227
RZ
350 hfs_cat_build_key(sb, dst_fd.search_key, cnid, NULL);
351 entry_size = hfs_cat_build_thread(sb, &entry, type == HFS_CDR_FIL ? HFS_CDR_FTH : HFS_CDR_THD,
1da177e4
LT
352 dst_dir->i_ino, dst_name);
353 err = hfs_brec_find(&dst_fd);
354 if (err != -ENOENT) {
355 if (!err)
356 err = -EEXIST;
357 goto out;
358 }
359 err = hfs_brec_insert(&dst_fd, &entry, entry_size);
360out:
361 hfs_bnode_put(dst_fd.bnode);
362 hfs_find_exit(&src_fd);
363 return err;
364}
This page took 0.824036 seconds and 5 git commands to generate.