2 * linux/fs/hfsplus/catalog.c
5 * Brad Boyer (flar@allandria.com)
6 * (C) 2003 Ardis Technologies <roman@ardistech.com>
8 * Handling of catalog records
12 #include "hfsplus_fs.h"
13 #include "hfsplus_raw.h"
15 int hfsplus_cat_case_cmp_key(const hfsplus_btree_key
*k1
,
16 const hfsplus_btree_key
*k2
)
23 return be32_to_cpu(k1p
) < be32_to_cpu(k2p
) ? -1 : 1;
25 return hfsplus_strcasecmp(&k1
->cat
.name
, &k2
->cat
.name
);
28 int hfsplus_cat_bin_cmp_key(const hfsplus_btree_key
*k1
,
29 const hfsplus_btree_key
*k2
)
36 return be32_to_cpu(k1p
) < be32_to_cpu(k2p
) ? -1 : 1;
38 return hfsplus_strcmp(&k1
->cat
.name
, &k2
->cat
.name
);
41 /* Generates key for catalog file/folders record. */
42 int hfsplus_cat_build_key(struct super_block
*sb
,
43 hfsplus_btree_key
*key
, u32 parent
, struct qstr
*str
)
47 key
->cat
.parent
= cpu_to_be32(parent
);
48 err
= hfsplus_asc2uni(sb
, &key
->cat
.name
, HFSPLUS_MAX_STRLEN
,
50 if (unlikely(err
< 0))
53 len
= be16_to_cpu(key
->cat
.name
.length
);
54 key
->key_len
= cpu_to_be16(6 + 2 * len
);
58 /* Generates key for catalog thread record. */
59 void hfsplus_cat_build_key_with_cnid(struct super_block
*sb
,
60 hfsplus_btree_key
*key
, u32 parent
)
62 key
->cat
.parent
= cpu_to_be32(parent
);
63 key
->cat
.name
.length
= 0;
64 key
->key_len
= cpu_to_be16(6);
67 static void hfsplus_cat_build_key_uni(hfsplus_btree_key
*key
, u32 parent
,
68 struct hfsplus_unistr
*name
)
72 ustrlen
= be16_to_cpu(name
->length
);
73 key
->cat
.parent
= cpu_to_be32(parent
);
74 key
->cat
.name
.length
= cpu_to_be16(ustrlen
);
76 memcpy(key
->cat
.name
.unicode
, name
->unicode
, ustrlen
);
77 key
->key_len
= cpu_to_be16(6 + ustrlen
);
80 void hfsplus_cat_set_perms(struct inode
*inode
, struct hfsplus_perm
*perms
)
82 if (inode
->i_flags
& S_IMMUTABLE
)
83 perms
->rootflags
|= HFSPLUS_FLG_IMMUTABLE
;
85 perms
->rootflags
&= ~HFSPLUS_FLG_IMMUTABLE
;
86 if (inode
->i_flags
& S_APPEND
)
87 perms
->rootflags
|= HFSPLUS_FLG_APPEND
;
89 perms
->rootflags
&= ~HFSPLUS_FLG_APPEND
;
91 perms
->userflags
= HFSPLUS_I(inode
)->userflags
;
92 perms
->mode
= cpu_to_be16(inode
->i_mode
);
93 perms
->owner
= cpu_to_be32(i_uid_read(inode
));
94 perms
->group
= cpu_to_be32(i_gid_read(inode
));
96 if (S_ISREG(inode
->i_mode
))
97 perms
->dev
= cpu_to_be32(inode
->i_nlink
);
98 else if (S_ISBLK(inode
->i_mode
) || S_ISCHR(inode
->i_mode
))
99 perms
->dev
= cpu_to_be32(inode
->i_rdev
);
104 static int hfsplus_cat_build_record(hfsplus_cat_entry
*entry
,
105 u32 cnid
, struct inode
*inode
)
107 struct hfsplus_sb_info
*sbi
= HFSPLUS_SB(inode
->i_sb
);
109 if (S_ISDIR(inode
->i_mode
)) {
110 struct hfsplus_cat_folder
*folder
;
112 folder
= &entry
->folder
;
113 memset(folder
, 0, sizeof(*folder
));
114 folder
->type
= cpu_to_be16(HFSPLUS_FOLDER
);
115 if (test_bit(HFSPLUS_SB_HFSX
, &sbi
->flags
))
116 folder
->flags
|= cpu_to_be16(HFSPLUS_HAS_FOLDER_COUNT
);
117 folder
->id
= cpu_to_be32(inode
->i_ino
);
118 HFSPLUS_I(inode
)->create_date
=
119 folder
->create_date
=
120 folder
->content_mod_date
=
121 folder
->attribute_mod_date
=
122 folder
->access_date
= hfsp_now2mt();
123 hfsplus_cat_set_perms(inode
, &folder
->permissions
);
124 if (inode
== sbi
->hidden_dir
)
125 /* invisible and namelocked */
126 folder
->user_info
.frFlags
= cpu_to_be16(0x5000);
127 return sizeof(*folder
);
129 struct hfsplus_cat_file
*file
;
132 memset(file
, 0, sizeof(*file
));
133 file
->type
= cpu_to_be16(HFSPLUS_FILE
);
134 file
->flags
= cpu_to_be16(HFSPLUS_FILE_THREAD_EXISTS
);
135 file
->id
= cpu_to_be32(cnid
);
136 HFSPLUS_I(inode
)->create_date
=
138 file
->content_mod_date
=
139 file
->attribute_mod_date
=
140 file
->access_date
= hfsp_now2mt();
141 if (cnid
== inode
->i_ino
) {
142 hfsplus_cat_set_perms(inode
, &file
->permissions
);
143 if (S_ISLNK(inode
->i_mode
)) {
144 file
->user_info
.fdType
=
145 cpu_to_be32(HFSP_SYMLINK_TYPE
);
146 file
->user_info
.fdCreator
=
147 cpu_to_be32(HFSP_SYMLINK_CREATOR
);
149 file
->user_info
.fdType
=
150 cpu_to_be32(sbi
->type
);
151 file
->user_info
.fdCreator
=
152 cpu_to_be32(sbi
->creator
);
154 if (HFSPLUS_FLG_IMMUTABLE
&
155 (file
->permissions
.rootflags
|
156 file
->permissions
.userflags
))
158 cpu_to_be16(HFSPLUS_FILE_LOCKED
);
160 file
->user_info
.fdType
=
161 cpu_to_be32(HFSP_HARDLINK_TYPE
);
162 file
->user_info
.fdCreator
=
163 cpu_to_be32(HFSP_HFSPLUS_CREATOR
);
164 file
->user_info
.fdFlags
=
167 HFSPLUS_I(sbi
->hidden_dir
)->create_date
;
168 file
->permissions
.dev
=
169 cpu_to_be32(HFSPLUS_I(inode
)->linkid
);
171 return sizeof(*file
);
175 static int hfsplus_fill_cat_thread(struct super_block
*sb
,
176 hfsplus_cat_entry
*entry
, int type
,
177 u32 parentid
, struct qstr
*str
)
181 entry
->type
= cpu_to_be16(type
);
182 entry
->thread
.reserved
= 0;
183 entry
->thread
.parentID
= cpu_to_be32(parentid
);
184 err
= hfsplus_asc2uni(sb
, &entry
->thread
.nodeName
, HFSPLUS_MAX_STRLEN
,
185 str
->name
, str
->len
);
186 if (unlikely(err
< 0))
189 return 10 + be16_to_cpu(entry
->thread
.nodeName
.length
) * 2;
192 /* Try to get a catalog entry for given catalog id */
193 int hfsplus_find_cat(struct super_block
*sb
, u32 cnid
,
194 struct hfs_find_data
*fd
)
196 hfsplus_cat_entry tmp
;
200 hfsplus_cat_build_key_with_cnid(sb
, fd
->search_key
, cnid
);
201 err
= hfs_brec_read(fd
, &tmp
, sizeof(hfsplus_cat_entry
));
205 type
= be16_to_cpu(tmp
.type
);
206 if (type
!= HFSPLUS_FOLDER_THREAD
&& type
!= HFSPLUS_FILE_THREAD
) {
207 pr_err("found bad thread record in catalog\n");
211 if (be16_to_cpu(tmp
.thread
.nodeName
.length
) > 255) {
212 pr_err("catalog name length corrupted\n");
216 hfsplus_cat_build_key_uni(fd
->search_key
,
217 be32_to_cpu(tmp
.thread
.parentID
),
218 &tmp
.thread
.nodeName
);
219 return hfs_brec_find(fd
, hfs_find_rec_by_key
);
222 static void hfsplus_subfolders_inc(struct inode
*dir
)
224 struct hfsplus_sb_info
*sbi
= HFSPLUS_SB(dir
->i_sb
);
226 if (test_bit(HFSPLUS_SB_HFSX
, &sbi
->flags
)) {
228 * Increment subfolder count. Note, the value is only meaningful
229 * for folders with HFSPLUS_HAS_FOLDER_COUNT flag set.
231 HFSPLUS_I(dir
)->subfolders
++;
235 static void hfsplus_subfolders_dec(struct inode
*dir
)
237 struct hfsplus_sb_info
*sbi
= HFSPLUS_SB(dir
->i_sb
);
239 if (test_bit(HFSPLUS_SB_HFSX
, &sbi
->flags
)) {
241 * Decrement subfolder count. Note, the value is only meaningful
242 * for folders with HFSPLUS_HAS_FOLDER_COUNT flag set.
244 * Check for zero. Some subfolders may have been created
245 * by an implementation ignorant of this counter.
247 if (HFSPLUS_I(dir
)->subfolders
)
248 HFSPLUS_I(dir
)->subfolders
--;
252 int hfsplus_create_cat(u32 cnid
, struct inode
*dir
,
253 struct qstr
*str
, struct inode
*inode
)
255 struct super_block
*sb
= dir
->i_sb
;
256 struct hfs_find_data fd
;
257 hfsplus_cat_entry entry
;
261 hfs_dbg(CAT_MOD
, "create_cat: %s,%u(%d)\n",
262 str
->name
, cnid
, inode
->i_nlink
);
263 err
= hfs_find_init(HFSPLUS_SB(sb
)->cat_tree
, &fd
);
268 * Fail early and avoid ENOSPC during the btree operations. We may
269 * have to split the root node at most once.
271 err
= hfs_bmap_reserve(fd
.tree
, 2 * fd
.tree
->depth
);
275 hfsplus_cat_build_key_with_cnid(sb
, fd
.search_key
, cnid
);
276 entry_size
= hfsplus_fill_cat_thread(sb
, &entry
,
277 S_ISDIR(inode
->i_mode
) ?
278 HFSPLUS_FOLDER_THREAD
: HFSPLUS_FILE_THREAD
,
280 if (unlikely(entry_size
< 0)) {
285 err
= hfs_brec_find(&fd
, hfs_find_rec_by_key
);
286 if (err
!= -ENOENT
) {
291 err
= hfs_brec_insert(&fd
, &entry
, entry_size
);
295 err
= hfsplus_cat_build_key(sb
, fd
.search_key
, dir
->i_ino
, str
);
299 entry_size
= hfsplus_cat_build_record(&entry
, cnid
, inode
);
300 err
= hfs_brec_find(&fd
, hfs_find_rec_by_key
);
301 if (err
!= -ENOENT
) {
307 err
= hfs_brec_insert(&fd
, &entry
, entry_size
);
312 if (S_ISDIR(inode
->i_mode
))
313 hfsplus_subfolders_inc(dir
);
314 dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME_SEC
;
315 hfsplus_mark_inode_dirty(dir
, HFSPLUS_I_CAT_DIRTY
);
321 hfsplus_cat_build_key_with_cnid(sb
, fd
.search_key
, cnid
);
322 if (!hfs_brec_find(&fd
, hfs_find_rec_by_key
))
323 hfs_brec_remove(&fd
);
329 int hfsplus_delete_cat(u32 cnid
, struct inode
*dir
, struct qstr
*str
)
331 struct super_block
*sb
= dir
->i_sb
;
332 struct hfs_find_data fd
;
333 struct hfsplus_fork_raw fork
;
334 struct list_head
*pos
;
338 hfs_dbg(CAT_MOD
, "delete_cat: %s,%u\n", str
? str
->name
: NULL
, cnid
);
339 err
= hfs_find_init(HFSPLUS_SB(sb
)->cat_tree
, &fd
);
344 * Fail early and avoid ENOSPC during the btree operations. We may
345 * have to split the root node at most once.
347 err
= hfs_bmap_reserve(fd
.tree
, 2 * (int)fd
.tree
->depth
- 2);
354 hfsplus_cat_build_key_with_cnid(sb
, fd
.search_key
, cnid
);
355 err
= hfs_brec_find(&fd
, hfs_find_rec_by_key
);
359 off
= fd
.entryoffset
+
360 offsetof(struct hfsplus_cat_thread
, nodeName
);
361 fd
.search_key
->cat
.parent
= cpu_to_be32(dir
->i_ino
);
362 hfs_bnode_read(fd
.bnode
,
363 &fd
.search_key
->cat
.name
.length
, off
, 2);
364 len
= be16_to_cpu(fd
.search_key
->cat
.name
.length
) * 2;
365 hfs_bnode_read(fd
.bnode
,
366 &fd
.search_key
->cat
.name
.unicode
,
368 fd
.search_key
->key_len
= cpu_to_be16(6 + len
);
370 err
= hfsplus_cat_build_key(sb
, fd
.search_key
, dir
->i_ino
, str
);
375 err
= hfs_brec_find(&fd
, hfs_find_rec_by_key
);
379 type
= hfs_bnode_read_u16(fd
.bnode
, fd
.entryoffset
);
380 if (type
== HFSPLUS_FILE
) {
382 off
= fd
.entryoffset
+ offsetof(hfsplus_cat_file
, data_fork
);
383 hfs_bnode_read(fd
.bnode
, &fork
, off
, sizeof(fork
));
384 hfsplus_free_fork(sb
, cnid
, &fork
, HFSPLUS_TYPE_DATA
);
387 off
= fd
.entryoffset
+
388 offsetof(struct hfsplus_cat_file
, rsrc_fork
);
389 hfs_bnode_read(fd
.bnode
, &fork
, off
, sizeof(fork
));
390 hfsplus_free_fork(sb
, cnid
, &fork
, HFSPLUS_TYPE_RSRC
);
393 list_for_each(pos
, &HFSPLUS_I(dir
)->open_dir_list
) {
394 struct hfsplus_readdir_data
*rd
=
395 list_entry(pos
, struct hfsplus_readdir_data
, list
);
396 if (fd
.tree
->keycmp(fd
.search_key
, (void *)&rd
->key
) < 0)
400 err
= hfs_brec_remove(&fd
);
404 hfsplus_cat_build_key_with_cnid(sb
, fd
.search_key
, cnid
);
405 err
= hfs_brec_find(&fd
, hfs_find_rec_by_key
);
409 err
= hfs_brec_remove(&fd
);
414 if (type
== HFSPLUS_FOLDER
)
415 hfsplus_subfolders_dec(dir
);
416 dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME_SEC
;
417 hfsplus_mark_inode_dirty(dir
, HFSPLUS_I_CAT_DIRTY
);
419 if (type
== HFSPLUS_FILE
|| type
== HFSPLUS_FOLDER
) {
420 if (HFSPLUS_SB(sb
)->attr_tree
)
421 hfsplus_delete_all_attrs(dir
, cnid
);
430 int hfsplus_rename_cat(u32 cnid
,
431 struct inode
*src_dir
, struct qstr
*src_name
,
432 struct inode
*dst_dir
, struct qstr
*dst_name
)
434 struct super_block
*sb
= src_dir
->i_sb
;
435 struct hfs_find_data src_fd
, dst_fd
;
436 hfsplus_cat_entry entry
;
437 int entry_size
, type
;
440 hfs_dbg(CAT_MOD
, "rename_cat: %u - %lu,%s - %lu,%s\n",
441 cnid
, src_dir
->i_ino
, src_name
->name
,
442 dst_dir
->i_ino
, dst_name
->name
);
443 err
= hfs_find_init(HFSPLUS_SB(sb
)->cat_tree
, &src_fd
);
449 * Fail early and avoid ENOSPC during the btree operations. We may
450 * have to split the root node at most twice.
452 err
= hfs_bmap_reserve(src_fd
.tree
, 4 * (int)src_fd
.tree
->depth
- 1);
456 /* find the old dir entry and read the data */
457 err
= hfsplus_cat_build_key(sb
, src_fd
.search_key
,
458 src_dir
->i_ino
, src_name
);
462 err
= hfs_brec_find(&src_fd
, hfs_find_rec_by_key
);
465 if (src_fd
.entrylength
> sizeof(entry
) || src_fd
.entrylength
< 0) {
470 hfs_bnode_read(src_fd
.bnode
, &entry
, src_fd
.entryoffset
,
472 type
= be16_to_cpu(entry
.type
);
474 /* create new dir entry with the data from the old entry */
475 err
= hfsplus_cat_build_key(sb
, dst_fd
.search_key
,
476 dst_dir
->i_ino
, dst_name
);
480 err
= hfs_brec_find(&dst_fd
, hfs_find_rec_by_key
);
481 if (err
!= -ENOENT
) {
487 err
= hfs_brec_insert(&dst_fd
, &entry
, src_fd
.entrylength
);
491 if (type
== HFSPLUS_FOLDER
)
492 hfsplus_subfolders_inc(dst_dir
);
493 dst_dir
->i_mtime
= dst_dir
->i_ctime
= CURRENT_TIME_SEC
;
495 /* finally remove the old entry */
496 err
= hfsplus_cat_build_key(sb
, src_fd
.search_key
,
497 src_dir
->i_ino
, src_name
);
501 err
= hfs_brec_find(&src_fd
, hfs_find_rec_by_key
);
504 err
= hfs_brec_remove(&src_fd
);
508 if (type
== HFSPLUS_FOLDER
)
509 hfsplus_subfolders_dec(src_dir
);
510 src_dir
->i_mtime
= src_dir
->i_ctime
= CURRENT_TIME_SEC
;
512 /* remove old thread entry */
513 hfsplus_cat_build_key_with_cnid(sb
, src_fd
.search_key
, cnid
);
514 err
= hfs_brec_find(&src_fd
, hfs_find_rec_by_key
);
517 type
= hfs_bnode_read_u16(src_fd
.bnode
, src_fd
.entryoffset
);
518 err
= hfs_brec_remove(&src_fd
);
522 /* create new thread entry */
523 hfsplus_cat_build_key_with_cnid(sb
, dst_fd
.search_key
, cnid
);
524 entry_size
= hfsplus_fill_cat_thread(sb
, &entry
, type
,
525 dst_dir
->i_ino
, dst_name
);
526 if (unlikely(entry_size
< 0)) {
531 err
= hfs_brec_find(&dst_fd
, hfs_find_rec_by_key
);
532 if (err
!= -ENOENT
) {
537 err
= hfs_brec_insert(&dst_fd
, &entry
, entry_size
);
539 hfsplus_mark_inode_dirty(dst_dir
, HFSPLUS_I_CAT_DIRTY
);
540 hfsplus_mark_inode_dirty(src_dir
, HFSPLUS_I_CAT_DIRTY
);
542 hfs_bnode_put(dst_fd
.bnode
);
543 hfs_find_exit(&src_fd
);