]>
git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - fs/hfs/catalog.c
2 * linux/fs/hfs/catalog.c
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.
8 * This file contains the functions related to the catalog B-tree.
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
21 * Given the ID of the parent and the name build a search key.
23 void hfs_cat_build_key(struct super_block
*sb
, btree_key
*key
, u32 parent
, const struct qstr
*name
)
25 key
->cat
.reserved
= 0;
26 key
->cat
.ParID
= cpu_to_be32(parent
);
28 hfs_asc2mac(sb
, &key
->cat
.CName
, name
);
29 key
->key_len
= 6 + key
->cat
.CName
.len
;
31 memset(&key
->cat
.CName
, 0, sizeof(struct hfs_name
));
36 static int hfs_cat_build_record(hfs_cat_rec
*rec
, u32 cnid
, struct inode
*inode
)
38 __be32 mtime
= hfs_mtime();
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
;
47 rec
->dir
.UsrInfo
.frView
= cpu_to_be16(0xff);
48 return sizeof(struct hfs_cat_dir
);
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
;
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
);
65 static int hfs_cat_build_thread(struct super_block
*sb
,
66 hfs_cat_rec
*rec
, int type
,
67 u32 parentid
, const struct qstr
*name
)
70 memset(rec
->thread
.reserved
, 0, sizeof(rec
->thread
.reserved
));
71 rec
->thread
.ParID
= cpu_to_be32(parentid
);
72 hfs_asc2mac(sb
, &rec
->thread
.CName
, name
);
73 return sizeof(struct hfs_cat_thread
);
79 * Add a new file or directory to the catalog B-tree and
80 * return a (struct hfs_cat_entry) for it in '*result'.
82 int hfs_cat_create(u32 cnid
, struct inode
*dir
, const struct qstr
*str
, struct inode
*inode
)
84 struct hfs_find_data fd
;
85 struct super_block
*sb
;
86 union hfs_cat_rec entry
;
90 hfs_dbg(CAT_MOD
, "create_cat: %s,%u(%d)\n",
91 str
->name
, cnid
, inode
->i_nlink
);
92 if (dir
->i_size
>= HFS_MAX_VALENCE
)
96 err
= hfs_find_init(HFS_SB(sb
)->cat_tree
, &fd
);
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
) ?
102 HFS_CDR_THD
: HFS_CDR_FTH
,
104 err
= hfs_brec_find(&fd
);
105 if (err
!= -ENOENT
) {
110 err
= hfs_brec_insert(&fd
, &entry
, entry_size
);
114 hfs_cat_build_key(sb
, fd
.search_key
, dir
->i_ino
, str
);
115 entry_size
= hfs_cat_build_record(&entry
, cnid
, inode
);
116 err
= hfs_brec_find(&fd
);
117 if (err
!= -ENOENT
) {
123 err
= hfs_brec_insert(&fd
, &entry
, entry_size
);
128 dir
->i_mtime
= dir
->i_ctime
= current_time(dir
);
129 mark_inode_dirty(dir
);
134 hfs_cat_build_key(sb
, fd
.search_key
, cnid
, NULL
);
135 if (!hfs_brec_find(&fd
))
136 hfs_brec_remove(&fd
);
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)
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):
157 * int: negative if key1<key2, positive if key1>key2, and 0 if key1==key2
159 * key1 and key2 point to "valid" (struct hfs_cat_key)s.
161 * This function has no side-effects
163 int hfs_cat_keycmp(const btree_key
*key1
, const btree_key
*key2
)
167 k1p
= key1
->cat
.ParID
;
168 k2p
= key2
->cat
.ParID
;
171 return be32_to_cpu(k1p
) < be32_to_cpu(k2p
) ? -1 : 1;
173 return hfs_strcmp(key1
->cat
.CName
.name
, key1
->cat
.CName
.len
,
174 key2
->cat
.CName
.name
, key2
->cat
.CName
.len
);
177 /* Try to get a catalog entry for given catalog id */
178 // move to read_super???
179 int hfs_cat_find_brec(struct super_block
*sb
, u32 cnid
,
180 struct hfs_find_data
*fd
)
185 hfs_cat_build_key(sb
, fd
->search_key
, cnid
, NULL
);
186 res
= hfs_brec_read(fd
, &rec
, sizeof(rec
));
191 if (type
!= HFS_CDR_THD
&& type
!= HFS_CDR_FTH
) {
192 pr_err("found bad thread record in catalog\n");
196 fd
->search_key
->cat
.ParID
= rec
.thread
.ParID
;
197 len
= fd
->search_key
->cat
.CName
.len
= rec
.thread
.CName
.len
;
198 if (len
> HFS_NAMELEN
) {
199 pr_err("bad catalog namelength\n");
202 memcpy(fd
->search_key
->cat
.CName
.name
, rec
.thread
.CName
.name
, len
);
203 return hfs_brec_find(fd
);
210 * Delete the indicated file or directory.
211 * The associated thread is also removed unless ('with_thread'==0).
213 int hfs_cat_delete(u32 cnid
, struct inode
*dir
, const struct qstr
*str
)
215 struct super_block
*sb
;
216 struct hfs_find_data fd
;
217 struct hfs_readdir_data
*rd
;
220 hfs_dbg(CAT_MOD
, "delete_cat: %s,%u\n", str
? str
->name
: NULL
, cnid
);
222 res
= hfs_find_init(HFS_SB(sb
)->cat_tree
, &fd
);
226 hfs_cat_build_key(sb
, fd
.search_key
, dir
->i_ino
, str
);
227 res
= hfs_brec_find(&fd
);
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
) {
237 hfs_free_fork(sb
, &file
, HFS_FK_DATA
);
239 hfs_free_fork(sb
, &file
, HFS_FK_RSRC
);
243 /* we only need to take spinlock for exclusion with ->release() */
244 spin_lock(&HFS_I(dir
)->open_dir_lock
);
245 list_for_each_entry(rd
, &HFS_I(dir
)->open_dir_list
, list
) {
246 if (fd
.tree
->keycmp(fd
.search_key
, (void *)&rd
->key
) < 0)
249 spin_unlock(&HFS_I(dir
)->open_dir_lock
);
251 res
= hfs_brec_remove(&fd
);
255 hfs_cat_build_key(sb
, fd
.search_key
, cnid
, NULL
);
256 res
= hfs_brec_find(&fd
);
258 res
= hfs_brec_remove(&fd
);
264 dir
->i_mtime
= dir
->i_ctime
= current_time(dir
);
265 mark_inode_dirty(dir
);
276 * Rename a file or directory, possibly to a new directory.
277 * If the destination exists it is removed and a
278 * (struct hfs_cat_entry) for it is returned in '*result'.
280 int hfs_cat_move(u32 cnid
, struct inode
*src_dir
, const struct qstr
*src_name
,
281 struct inode
*dst_dir
, const struct qstr
*dst_name
)
283 struct super_block
*sb
;
284 struct hfs_find_data src_fd
, dst_fd
;
285 union hfs_cat_rec entry
;
286 int entry_size
, type
;
289 hfs_dbg(CAT_MOD
, "rename_cat: %u - %lu,%s - %lu,%s\n",
290 cnid
, src_dir
->i_ino
, src_name
->name
,
291 dst_dir
->i_ino
, dst_name
->name
);
293 err
= hfs_find_init(HFS_SB(sb
)->cat_tree
, &src_fd
);
298 /* find the old dir entry and read the data */
299 hfs_cat_build_key(sb
, src_fd
.search_key
, src_dir
->i_ino
, src_name
);
300 err
= hfs_brec_find(&src_fd
);
303 if (src_fd
.entrylength
> sizeof(entry
) || src_fd
.entrylength
< 0) {
308 hfs_bnode_read(src_fd
.bnode
, &entry
, src_fd
.entryoffset
,
311 /* create new dir entry with the data from the old entry */
312 hfs_cat_build_key(sb
, dst_fd
.search_key
, dst_dir
->i_ino
, dst_name
);
313 err
= hfs_brec_find(&dst_fd
);
314 if (err
!= -ENOENT
) {
320 err
= hfs_brec_insert(&dst_fd
, &entry
, src_fd
.entrylength
);
324 dst_dir
->i_mtime
= dst_dir
->i_ctime
= current_time(dst_dir
);
325 mark_inode_dirty(dst_dir
);
327 /* finally remove the old entry */
328 hfs_cat_build_key(sb
, src_fd
.search_key
, src_dir
->i_ino
, src_name
);
329 err
= hfs_brec_find(&src_fd
);
332 err
= hfs_brec_remove(&src_fd
);
336 src_dir
->i_mtime
= src_dir
->i_ctime
= current_time(src_dir
);
337 mark_inode_dirty(src_dir
);
340 if (type
== HFS_CDR_FIL
&& !(entry
.file
.Flags
& HFS_FIL_THD
))
343 /* remove old thread entry */
344 hfs_cat_build_key(sb
, src_fd
.search_key
, cnid
, NULL
);
345 err
= hfs_brec_find(&src_fd
);
348 err
= hfs_brec_remove(&src_fd
);
352 /* create new thread entry */
353 hfs_cat_build_key(sb
, dst_fd
.search_key
, cnid
, NULL
);
354 entry_size
= hfs_cat_build_thread(sb
, &entry
, type
== HFS_CDR_FIL
? HFS_CDR_FTH
: HFS_CDR_THD
,
355 dst_dir
->i_ino
, dst_name
);
356 err
= hfs_brec_find(&dst_fd
);
357 if (err
!= -ENOENT
) {
362 err
= hfs_brec_insert(&dst_fd
, &entry
, entry_size
);
364 hfs_bnode_put(dst_fd
.bnode
);
365 hfs_find_exit(&src_fd
);