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