2 * Copyright (c) 1999-2015 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
30 #include <sys/systm.h>
31 #include <sys/kernel.h>
32 #include <sys/malloc.h>
33 #include <sys/mount.h>
35 #include <sys/vnode.h>
36 #include <vfs/vfs_support.h>
37 #include <libkern/libkern.h>
38 #include <sys/fsctl.h>
41 #include "hfs_catalog.h"
42 #include "hfs_format.h"
43 #include "hfs_endian.h"
46 static int cur_link_id
= 0;
49 * Private directories where hardlink inodes reside.
51 const char *hfs_private_names
[] = {
52 HFSPLUSMETADATAFOLDER
, /* FILE HARDLINKS */
53 HFSPLUS_DIR_METADATA_FOLDER
/* DIRECTORY HARDLINKS */
58 * Hardlink inodes save the head of their link chain in a
59 * private extended attribute. The following calls are
60 * used to access this attribute.
62 static int setfirstlink(struct hfsmount
* hfsmp
, cnid_t fileid
, cnid_t firstlink
);
63 static int getfirstlink(struct hfsmount
* hfsmp
, cnid_t fileid
, cnid_t
*firstlink
);
66 * Create a new catalog link record
68 * An indirect link is a reference to an inode (the real
69 * file or directory record).
71 * All the indirect links for a given inode are chained
72 * together in a doubly linked list.
74 * Pre-Leopard file hard links do not have kHFSHasLinkChainBit
75 * set and do not have first/prev/next link IDs i.e. the values
76 * are zero. If a new link is being added to an existing
77 * pre-Leopard file hard link chain, do not set kHFSHasLinkChainBit.
80 createindirectlink(struct hfsmount
*hfsmp
, u_int32_t linknum
, struct cat_desc
*descp
,
81 cnid_t nextcnid
, cnid_t
*linkcnid
, int is_inode_linkchain_set
)
83 struct FndrFileInfo
*fip
;
87 printf("hfs: createindirectlink: linknum is zero!\n");
91 /* Setup the default attributes */
92 bzero(&attr
, sizeof(attr
));
94 /* Links are matched to inodes by link ID and to volumes by create date */
95 attr
.ca_linkref
= linknum
;
96 attr
.ca_itime
= hfsmp
->hfs_metadata_createdate
;
97 attr
.ca_mode
= S_IFREG
| S_IRUSR
| S_IRGRP
| S_IROTH
;
98 attr
.ca_recflags
= kHFSHasLinkChainMask
| kHFSThreadExistsMask
;
99 attr
.ca_flags
= UF_IMMUTABLE
;
100 fip
= (struct FndrFileInfo
*)&attr
.ca_finderinfo
;
102 if (descp
->cd_flags
& CD_ISDIR
) {
103 fip
->fdType
= SWAP_BE32 (kHFSAliasType
);
104 fip
->fdCreator
= SWAP_BE32 (kHFSAliasCreator
);
105 fip
->fdFlags
= SWAP_BE16 (kIsAlias
);
107 fip
->fdType
= SWAP_BE32 (kHardLinkFileType
);
108 fip
->fdCreator
= SWAP_BE32 (kHFSPlusCreator
);
109 fip
->fdFlags
= SWAP_BE16 (kHasBeenInited
);
110 /* If the file inode does not have kHFSHasLinkChainBit set
111 * and the next link chain ID is zero, assume that this
112 * is pre-Leopard file inode. Therefore clear the bit.
114 if ((is_inode_linkchain_set
== 0) && (nextcnid
== 0)) {
115 attr
.ca_recflags
&= ~kHFSHasLinkChainMask
;
118 /* Create the indirect link directly in the catalog */
119 return cat_createlink(hfsmp
, descp
, &attr
, nextcnid
, linkcnid
);
124 * Make a link to the cnode cp in the directory dp
125 * using the name in cnp. src_vp is the vnode that
126 * corresponds to 'cp' which was part of the arguments to
129 * The cnodes cp and dcp must be locked.
132 hfs_makelink(struct hfsmount
*hfsmp
, struct vnode
*src_vp
, struct cnode
*cp
,
133 struct cnode
*dcp
, struct componentname
*cnp
, vfs_context_t ctx
)
135 struct proc
*p
= vfs_context_proc(ctx
);
136 u_int32_t indnodeno
= 0;
138 struct cat_desc to_desc
;
139 struct cat_desc link_desc
;
146 cnid_t orig_firstlink
;
147 enum privdirtype type
;
149 type
= S_ISDIR(cp
->c_mode
) ? DIR_HARDLINKS
: FILE_HARDLINKS
;
151 if (cur_link_id
== 0) {
152 cur_link_id
= ((random() & 0x3fffffff) + 100);
155 /* We don't allow link nodes in our private system directories. */
156 if (dcp
->c_fileid
== hfsmp
->hfs_private_desc
[FILE_HARDLINKS
].cd_cnid
||
157 dcp
->c_fileid
== hfsmp
->hfs_private_desc
[DIR_HARDLINKS
].cd_cnid
) {
161 bzero(&cookie
, sizeof(cat_cookie_t
));
162 /* Reserve some space in the Catalog file. */
163 if ((retval
= cat_preflight(hfsmp
, (2 * CAT_CREATE
)+ CAT_RENAME
, &cookie
, p
))) {
167 lockflags
= SFL_CATALOG
| SFL_ATTRIBUTE
;
168 /* Directory hard links allocate space for a symlink. */
169 if (type
== DIR_HARDLINKS
) {
170 lockflags
|= SFL_BITMAP
;
172 lockflags
= hfs_systemfile_lock(hfsmp
, lockflags
, HFS_EXCLUSIVE_LOCK
);
174 /* Save the current cnid value so we restore it if an error occurs. */
175 orig_cnid
= cp
->c_desc
.cd_cnid
;
178 * If this is a new hardlink then we need to create the inode
179 * and replace the original file/dir object with a link node.
181 if ((cp
->c_linkcount
== 2) && !(cp
->c_flag
& C_HARDLINK
)) {
183 bzero(&to_desc
, sizeof(to_desc
));
184 to_desc
.cd_parentcnid
= hfsmp
->hfs_private_desc
[type
].cd_cnid
;
185 to_desc
.cd_cnid
= cp
->c_fileid
;
186 to_desc
.cd_flags
= (type
== DIR_HARDLINKS
) ? CD_ISDIR
: 0;
189 if (type
== DIR_HARDLINKS
) {
190 /* Directory hardlinks always use the cnid. */
191 indnodeno
= cp
->c_fileid
;
192 MAKE_DIRINODE_NAME(inodename
, sizeof(inodename
),
195 /* Get a unique indirect node number */
197 indnodeno
= cp
->c_fileid
;
199 indnodeno
= cur_link_id
++;
201 MAKE_INODE_NAME(inodename
, sizeof(inodename
),
204 /* Move original file/dir to data node directory */
205 to_desc
.cd_nameptr
= (const u_int8_t
*)inodename
;
206 to_desc
.cd_namelen
= strlen(inodename
);
208 retval
= cat_rename(hfsmp
, &cp
->c_desc
, &hfsmp
->hfs_private_desc
[type
],
211 if (retval
!= 0 && retval
!= EEXIST
) {
212 printf("hfs_makelink: cat_rename to %s failed (%d) fileid=%d, vol=%s\n",
213 inodename
, retval
, cp
->c_fileid
, hfsmp
->vcbVN
);
215 } while ((retval
== EEXIST
) && (type
== FILE_HARDLINKS
));
220 * Replace original file/dir with a link record.
223 bzero(&link_desc
, sizeof(link_desc
));
224 link_desc
.cd_nameptr
= cp
->c_desc
.cd_nameptr
;
225 link_desc
.cd_namelen
= cp
->c_desc
.cd_namelen
;
226 link_desc
.cd_parentcnid
= cp
->c_parentcnid
;
227 link_desc
.cd_flags
= S_ISDIR(cp
->c_mode
) ? CD_ISDIR
: 0;
229 retval
= createindirectlink(hfsmp
, indnodeno
, &link_desc
, 0, &linkcnid
, true);
233 /* Restore the cnode's cnid. */
234 cp
->c_desc
.cd_cnid
= orig_cnid
;
236 /* Put the original file back. */
237 err
= cat_rename(hfsmp
, &to_desc
, &dcp
->c_desc
, &cp
->c_desc
, NULL
);
239 if (err
!= EIO
&& err
!= ENXIO
)
240 printf("hfs_makelink: error %d from cat_rename backout 1", err
);
241 hfs_mark_inconsistent(hfsmp
, HFS_ROLLBACK_FAILED
);
243 if (retval
!= EIO
&& retval
!= ENXIO
) {
244 printf("hfs_makelink: createindirectlink (1) failed: %d\n", retval
);
249 cp
->c_attr
.ca_linkref
= indnodeno
;
250 cp
->c_desc
.cd_cnid
= linkcnid
;
251 /* Directory hard links store the first link in an attribute. */
252 if (type
== DIR_HARDLINKS
) {
253 if (setfirstlink(hfsmp
, cp
->c_fileid
, linkcnid
) == 0)
254 cp
->c_attr
.ca_recflags
|= kHFSHasAttributesMask
;
255 } else /* FILE_HARDLINKS */ {
256 cp
->c_attr
.ca_firstlink
= linkcnid
;
258 cp
->c_attr
.ca_recflags
|= kHFSHasLinkChainMask
;
260 indnodeno
= cp
->c_attr
.ca_linkref
;
264 * Create a catalog entry for the new link (parentID + name).
267 bzero(&link_desc
, sizeof(link_desc
));
268 link_desc
.cd_nameptr
= (const u_int8_t
*)cnp
->cn_nameptr
;
269 link_desc
.cd_namelen
= strlen(cnp
->cn_nameptr
);
270 link_desc
.cd_parentcnid
= dcp
->c_fileid
;
271 link_desc
.cd_flags
= S_ISDIR(cp
->c_mode
) ? CD_ISDIR
: 0;
273 /* Directory hard links store the first link in an attribute. */
274 if (type
== DIR_HARDLINKS
) {
275 retval
= getfirstlink(hfsmp
, cp
->c_fileid
, &orig_firstlink
);
276 } else /* FILE_HARDLINKS */ {
277 orig_firstlink
= cp
->c_attr
.ca_firstlink
;
280 retval
= createindirectlink(hfsmp
, indnodeno
, &link_desc
,
281 orig_firstlink
, &linkcnid
,
282 (cp
->c_attr
.ca_recflags
& kHFSHasLinkChainMask
));
283 if (retval
&& newlink
) {
286 /* Get rid of new link */
287 (void) cat_delete(hfsmp
, &cp
->c_desc
, &cp
->c_attr
);
289 /* Restore the cnode's cnid. */
290 cp
->c_desc
.cd_cnid
= orig_cnid
;
292 /* Put the original file back. */
293 err
= cat_rename(hfsmp
, &to_desc
, &dcp
->c_desc
, &cp
->c_desc
, NULL
);
295 if (err
!= EIO
&& err
!= ENXIO
)
296 printf("hfs_makelink: error %d from cat_rename backout 2", err
);
297 hfs_mark_inconsistent(hfsmp
, HFS_ROLLBACK_FAILED
);
300 cp
->c_attr
.ca_linkref
= 0;
302 if (retval
!= EIO
&& retval
!= ENXIO
) {
303 printf("hfs_makelink: createindirectlink (2) failed: %d\n", retval
);
307 } else if (retval
== 0) {
309 /* Update the original first link to point back to the new first link. */
310 if (cp
->c_attr
.ca_recflags
& kHFSHasLinkChainMask
) {
311 (void) cat_update_siblinglinks(hfsmp
, orig_firstlink
, linkcnid
, HFS_IGNORABLE_LINK
);
313 /* Update the inode's first link value. */
314 if (type
== DIR_HARDLINKS
) {
315 if (setfirstlink(hfsmp
, cp
->c_fileid
, linkcnid
) == 0)
316 cp
->c_attr
.ca_recflags
|= kHFSHasAttributesMask
;
318 cp
->c_attr
.ca_firstlink
= linkcnid
;
322 * Finally, if this is a new hardlink then:
323 * - update the private system directory
324 * - mark the cnode as a hard link
329 hfsmp
->hfs_private_attr
[type
].ca_entries
++;
330 /* From application perspective, directory hard link is a
331 * normal directory. Therefore count the new directory
332 * hard link for folder count calculation.
334 if (type
== DIR_HARDLINKS
) {
335 INC_FOLDERCOUNT(hfsmp
, hfsmp
->hfs_private_attr
[type
]);
337 retval
= cat_update(hfsmp
, &hfsmp
->hfs_private_desc
[type
],
338 &hfsmp
->hfs_private_attr
[type
], NULL
, NULL
);
340 if (retval
!= EIO
&& retval
!= ENXIO
) {
341 printf("hfs_makelink: cat_update of privdir failed! (%d)\n", retval
);
344 hfs_mark_inconsistent(hfsmp
, HFS_OP_INCOMPLETE
);
346 cp
->c_flag
|= C_HARDLINK
;
349 * Now we need to mark the vnodes as being hardlinks via the vnode_setmultipath call.
350 * Note that we're calling vnode_get here, which should simply add an iocount if possible, without
351 * doing much checking. It's safe to call this because we are protected by the cnode lock, which
352 * ensures that anyone trying to reclaim it will block until we release it. vnode_get will usually
353 * give us an extra iocount, unless the vnode is about to be reclaimed (and has no iocounts).
354 * In that case, we'd error out, but we'd also not care if we added the VISHARDLINK bit to the vnode.
356 * As for the iocount we're about to add, we can't necessarily always call vnode_put here.
357 * If the one we add is the only iocount on the vnode, and there was
358 * sufficient vnode pressure, it could go through VNOP_INACTIVE immediately, which would
359 * require the cnode lock and cause us to double-lock panic. We can only call vnode_put if we know
360 * that the vnode we're operating on is the one with which we came into hfs_vnop_link, because
361 * that means VFS took an iocount on it for us. If it's *not* the one that we came into the call
362 * with, then mark it as NEED_VNODE_PUT to have hfs_unlock drop it for us. hfs_vnop_link will
363 * unlock the cnode when it is finished.
365 if ((vp
= cp
->c_vp
) != NULLVP
) {
366 if (vnode_get(vp
) == 0) {
367 vnode_setmultipath(vp
);
369 /* we have an iocount on data fork vnode already. */
373 cp
->c_flag
|= C_NEED_DVNODE_PUT
;
377 if ((vp
= cp
->c_rsrc_vp
) != NULLVP
) {
378 if (vnode_get(vp
) == 0) {
379 vnode_setmultipath(vp
);
384 cp
->c_flag
|= C_NEED_RVNODE_PUT
;
388 cp
->c_flag
|= C_MODIFIED
;
389 cp
->c_touch_chgtime
= TRUE
;
393 hfs_systemfile_unlock(hfsmp
, lockflags
);
395 cat_postflight(hfsmp
, &cookie
, p
);
397 if (retval
== 0 && newlink
) {
398 hfs_volupdate(hfsmp
, VOL_MKFILE
, 0);
405 * link vnode operation
409 * IN struct componentname *a_cnp;
410 * IN vfs_context_t a_context;
413 hfs_vnop_link(struct vnop_link_args
*ap
)
415 struct hfsmount
*hfsmp
;
416 struct vnode
*vp
= ap
->a_vp
;
417 struct vnode
*tdvp
= ap
->a_tdvp
;
418 struct vnode
*fdvp
= NULLVP
;
419 struct componentname
*cnp
= ap
->a_cnp
;
422 struct cnode
*fdcp
= NULL
;
423 struct cat_desc todesc
;
431 v_type
= vnode_vtype(vp
);
433 /* No hard links in HFS standard file systems. */
434 if (hfsmp
->hfs_flags
& HFS_STANDARD
) {
437 /* Linking to a special file is not permitted. */
438 if (v_type
== VBLK
|| v_type
== VCHR
) {
443 * For now, return ENOTSUP for a symlink target. This can happen
444 * for linkat(2) when called without AT_SYMLINK_FOLLOW.
451 if (v_type
== VDIR
) {
452 #if CONFIG_HFS_DIRLINK
453 /* Make sure our private directory exists. */
454 if (hfsmp
->hfs_private_desc
[DIR_HARDLINKS
].cd_cnid
== 0) {
458 * Directory hardlinks (ADLs) have only been qualified on
459 * journaled HFS+. If/when they are tested on non-journaled
460 * file systems then this test can be removed.
462 if (hfsmp
->jnl
== NULL
) {
466 /* Directory hardlinks also need the parent of the original directory. */
467 if ((error
= hfs_vget(hfsmp
, hfs_currentparent(cp
, /* have_lock: */ false),
472 /* some platforms don't support directory hardlinks. */
476 /* Make sure our private directory exists. */
477 if (hfsmp
->hfs_private_desc
[FILE_HARDLINKS
].cd_cnid
== 0) {
481 if (hfs_freeblks(hfsmp
, 0) == 0) {
488 nspace_snapshot_event(vp
, VTOC(vp
)->c_ctime
, NAMESPACE_HANDLER_LINK_CREATE
, NULL
);
490 /* Lock the cnodes. */
492 if ((error
= hfs_lockfour(VTOC(tdvp
), VTOC(vp
), VTOC(fdvp
), NULL
, HFS_EXCLUSIVE_LOCK
, NULL
))) {
500 if ((error
= hfs_lockpair(VTOC(tdvp
), VTOC(vp
), HFS_EXCLUSIVE_LOCK
))) {
505 /* grab the parent CNID from originlist after grabbing cnode locks */
506 parentcnid
= hfs_currentparent(cp
, /* have_lock: */ true);
509 * Make sure we didn't race the src or dst parent directories with rmdir.
510 * Note that we should only have a src parent directory cnode lock
511 * if we're dealing with a directory hardlink here.
514 if (fdcp
->c_flag
& (C_NOEXISTS
| C_DELETED
)) {
520 if (tdcp
->c_flag
& (C_NOEXISTS
| C_DELETED
)) {
525 /* Check the source for errors:
526 * too many links, immutable, race with unlink
528 if (cp
->c_linkcount
>= HFS_LINK_MAX
) {
532 if (cp
->c_bsdflags
& (IMMUTABLE
| APPEND
)) {
536 if (cp
->c_flag
& (C_NOEXISTS
| C_DELETED
)) {
541 tdcp
->c_flag
|= C_DIR_MODIFICATION
;
543 if (hfs_start_transaction(hfsmp
) != 0) {
549 todesc
.cd_flags
= (v_type
== VDIR
) ? CD_ISDIR
: 0;
550 todesc
.cd_encoding
= 0;
551 todesc
.cd_nameptr
= (const u_int8_t
*)cnp
->cn_nameptr
;
552 todesc
.cd_namelen
= cnp
->cn_namelen
;
553 todesc
.cd_parentcnid
= tdcp
->c_fileid
;
557 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
559 /* If destination exists then we lost a race with create. */
560 if (cat_lookup(hfsmp
, &todesc
, 0, 0, NULL
, NULL
, NULL
, NULL
) == 0) {
564 if (cp
->c_flag
& C_HARDLINK
) {
565 struct cat_attr cattr
;
567 /* If inode is missing then we lost a race with unlink. */
568 if ((cat_idlookup(hfsmp
, cp
->c_fileid
, 0, 0, NULL
, &cattr
, NULL
) != 0) ||
569 (cattr
.ca_fileid
!= cp
->c_fileid
)) {
576 /* If source is missing then we lost a race with unlink. */
577 if ((cat_lookup(hfsmp
, &cp
->c_desc
, 0, 0, NULL
, NULL
, NULL
, &fileid
) != 0) ||
578 (fileid
!= cp
->c_fileid
)) {
584 * All directory links must reside in an non-ARCHIVED hierarchy.
586 if (v_type
== VDIR
) {
588 * - Source parent and destination parent cannot match
589 * - A link is not permitted in the root directory
590 * - Parent of 'pointed at' directory is not the root directory
591 * - The 'pointed at' directory (source) is not an ancestor
592 * of the new directory hard link (destination).
593 * - No ancestor of the new directory hard link (destination)
594 * is a directory hard link.
596 if ((parentcnid
== tdcp
->c_fileid
) ||
597 (tdcp
->c_fileid
== kHFSRootFolderID
) ||
598 (parentcnid
== kHFSRootFolderID
) ||
599 cat_check_link_ancestry(hfsmp
, tdcp
->c_fileid
, cp
->c_fileid
)) {
600 error
= EPERM
; /* abide by the rules, you did not */
604 hfs_systemfile_unlock(hfsmp
, lockflags
);
608 cp
->c_flag
|= C_MODIFIED
;
609 cp
->c_touch_chgtime
= TRUE
;
610 error
= hfs_makelink(hfsmp
, vp
, cp
, tdcp
, cnp
, ap
->a_context
);
613 hfs_volupdate(hfsmp
, VOL_UPDATE
, 0);
615 /* Invalidate negative cache entries in the destination directory */
616 if (tdcp
->c_flag
& C_NEG_ENTRIES
) {
617 cache_purge_negatives(tdvp
);
618 tdcp
->c_flag
&= ~C_NEG_ENTRIES
;
621 /* Update the target directory and volume stats */
623 if (v_type
== VDIR
) {
624 INC_FOLDERCOUNT(hfsmp
, tdcp
->c_attr
);
625 tdcp
->c_attr
.ca_recflags
|= kHFSHasChildLinkMask
;
627 /* Set kHFSHasChildLinkBit in the destination hierarchy */
628 error
= cat_set_childlinkbit(hfsmp
, tdcp
->c_parentcnid
);
630 printf ("hfs_vnop_link: error updating destination parent chain for id=%u, vol=%s\n", tdcp
->c_cnid
, hfsmp
->vcbVN
);
634 tdcp
->c_dirchangecnt
++;
635 tdcp
->c_flag
|= C_MODIFIED
;
636 hfs_incr_gencount(tdcp
);
637 tdcp
->c_touch_chgtime
= TRUE
;
638 tdcp
->c_touch_modtime
= TRUE
;
640 error
= hfs_update(tdvp
, 0);
642 if (error
!= EIO
&& error
!= ENXIO
) {
643 printf("hfs_vnop_link: error %d updating tdvp %p\n", error
, tdvp
);
646 hfs_mark_inconsistent(hfsmp
, HFS_OP_INCOMPLETE
);
649 if ((v_type
== VDIR
) &&
651 ((fdcp
->c_attr
.ca_recflags
& kHFSHasChildLinkMask
) == 0)) {
653 fdcp
->c_attr
.ca_recflags
|= kHFSHasChildLinkMask
;
654 fdcp
->c_flag
|= C_MODIFIED
;
655 fdcp
->c_touch_chgtime
= TRUE
;
656 error
= hfs_update(fdvp
, 0);
658 if (error
!= EIO
&& error
!= ENXIO
) {
659 printf("hfs_vnop_link: error %d updating fdvp %p\n", error
, fdvp
);
660 // No point changing error as it's set immediate below
662 hfs_mark_inconsistent(hfsmp
, HFS_OP_INCOMPLETE
);
665 /* Set kHFSHasChildLinkBit in the source hierarchy */
666 error
= cat_set_childlinkbit(hfsmp
, fdcp
->c_parentcnid
);
668 printf ("hfs_vnop_link: error updating source parent chain for id=%u, vol=%s\n", fdcp
->c_cnid
, hfsmp
->vcbVN
);
672 hfs_volupdate(hfsmp
, VOL_MKFILE
,
673 (tdcp
->c_cnid
== kHFSRootFolderID
));
676 if (error
== 0 && (ret
= hfs_update(vp
, 0)) != 0) {
677 if (ret
!= EIO
&& ret
!= ENXIO
)
678 printf("hfs_vnop_link: error %d updating vp @ %p\n", ret
, vp
);
679 hfs_mark_inconsistent(hfsmp
, HFS_OP_INCOMPLETE
);
684 hfs_systemfile_unlock(hfsmp
, lockflags
);
687 hfs_end_transaction(hfsmp
);
690 tdcp
->c_flag
&= ~C_DIR_MODIFICATION
;
691 wakeup((caddr_t
)&tdcp
->c_flag
);
694 hfs_unlockfour(tdcp
, cp
, fdcp
, NULL
);
696 hfs_unlockpair(tdcp
, cp
);
706 * Remove a link to a hardlink file/dir.
708 * Note: dvp and vp cnodes are already locked.
711 hfs_unlink(struct hfsmount
*hfsmp
, struct vnode
*dvp
, struct vnode
*vp
, struct componentname
*cnp
, int skip_reserve
)
715 struct cat_desc cndesc
;
724 if (hfsmp
->hfs_flags
& HFS_STANDARD
) {
730 dcp
->c_flag
|= C_DIR_MODIFICATION
;
732 /* Remove the entry from the namei cache: */
735 if ((error
= hfs_start_transaction(hfsmp
)) != 0) {
742 * Protect against a race with rename by using the component
743 * name passed in and parent id from dvp (instead of using
744 * the cp->c_desc which may have changed).
746 * Re-lookup the component name so we get the correct cnid
747 * for the name (as opposed to the c_cnid in the cnode which
748 * could have changed before the cnode was locked).
750 cndesc
.cd_flags
= vnode_isdir(vp
) ? CD_ISDIR
: 0;
751 cndesc
.cd_encoding
= cp
->c_desc
.cd_encoding
;
752 cndesc
.cd_nameptr
= (const u_int8_t
*)cnp
->cn_nameptr
;
753 cndesc
.cd_namelen
= cnp
->cn_namelen
;
754 cndesc
.cd_parentcnid
= dcp
->c_fileid
;
755 cndesc
.cd_hint
= dcp
->c_childhint
;
757 lockflags
= SFL_CATALOG
| SFL_ATTRIBUTE
;
758 if (cndesc
.cd_flags
& CD_ISDIR
) {
759 /* We'll be removing the alias resource allocation blocks. */
760 lockflags
|= SFL_BITMAP
;
762 lockflags
= hfs_systemfile_lock(hfsmp
, lockflags
, HFS_EXCLUSIVE_LOCK
);
764 if ((error
= cat_lookuplink(hfsmp
, &cndesc
, &cndesc
.cd_cnid
, &prevlinkid
, &nextlinkid
))) {
768 /* Reserve some space in the catalog file. */
769 if (!skip_reserve
&& (error
= cat_preflight(hfsmp
, 2 * CAT_DELETE
, NULL
, 0))) {
773 /* Purge any cached origin entries for a directory or file hard link. */
774 hfs_relorigin(cp
, dcp
->c_fileid
);
775 if (dcp
->c_fileid
!= dcp
->c_cnid
) {
776 hfs_relorigin(cp
, dcp
->c_cnid
);
779 /* Delete the link record. */
780 if ((error
= cat_deletelink(hfsmp
, &cndesc
))) {
784 /* Update the parent directory. */
785 if (dcp
->c_entries
> 0) {
788 if (cndesc
.cd_flags
& CD_ISDIR
) {
789 DEC_FOLDERCOUNT(hfsmp
, dcp
->c_attr
);
791 dcp
->c_dirchangecnt
++;
792 hfs_incr_gencount(dcp
);
794 dcp
->c_touch_chgtime
= dcp
->c_touch_modtime
= true;
795 dcp
->c_flag
|= C_MODIFIED
;
796 hfs_update(dcp
->c_vp
, 0);
799 * If this is the last link then we need to process the inode.
800 * Otherwise we need to fix up the link chain.
803 if (cp
->c_linkcount
< 1) {
805 struct cat_desc to_desc
;
806 struct cat_desc from_desc
;
809 * If a file inode or directory inode is being deleted, rename
810 * it to an open deleted file. This ensures that deletion
811 * of inode and its corresponding extended attributes does
812 * not overflow the journal. This inode will be deleted
813 * either in hfs_vnop_inactive() or in hfs_remove_orphans().
814 * Note: a rename failure here is not fatal.
816 bzero(&from_desc
, sizeof(from_desc
));
817 bzero(&to_desc
, sizeof(to_desc
));
818 if (vnode_isdir(vp
)) {
819 if (cp
->c_entries
!= 0) {
820 panic("hfs_unlink: dir not empty (id %d, %d entries)", cp
->c_fileid
, cp
->c_entries
);
822 MAKE_DIRINODE_NAME(inodename
, sizeof(inodename
),
823 cp
->c_attr
.ca_linkref
);
824 from_desc
.cd_parentcnid
= hfsmp
->hfs_private_desc
[DIR_HARDLINKS
].cd_cnid
;
825 from_desc
.cd_flags
= CD_ISDIR
;
826 to_desc
.cd_flags
= CD_ISDIR
;
828 MAKE_INODE_NAME(inodename
, sizeof(inodename
),
829 cp
->c_attr
.ca_linkref
);
830 from_desc
.cd_parentcnid
= hfsmp
->hfs_private_desc
[FILE_HARDLINKS
].cd_cnid
;
831 from_desc
.cd_flags
= 0;
832 to_desc
.cd_flags
= 0;
834 from_desc
.cd_nameptr
= (const u_int8_t
*)inodename
;
835 from_desc
.cd_namelen
= strlen(inodename
);
836 from_desc
.cd_cnid
= cp
->c_fileid
;
838 MAKE_DELETED_NAME(delname
, sizeof(delname
), cp
->c_fileid
);
839 to_desc
.cd_nameptr
= (const u_int8_t
*)delname
;
840 to_desc
.cd_namelen
= strlen(delname
);
841 to_desc
.cd_parentcnid
= hfsmp
->hfs_private_desc
[FILE_HARDLINKS
].cd_cnid
;
842 to_desc
.cd_cnid
= cp
->c_fileid
;
844 error
= cat_rename(hfsmp
, &from_desc
, &hfsmp
->hfs_private_desc
[FILE_HARDLINKS
],
845 &to_desc
, (struct cat_desc
*)NULL
);
847 cp
->c_flag
|= C_DELETED
;
848 cp
->c_attr
.ca_recflags
&= ~kHFSHasLinkChainMask
;
849 cp
->c_attr
.ca_firstlink
= 0;
850 if (vnode_isdir(vp
)) {
851 hfsmp
->hfs_private_attr
[DIR_HARDLINKS
].ca_entries
--;
852 DEC_FOLDERCOUNT(hfsmp
, hfsmp
->hfs_private_attr
[DIR_HARDLINKS
]);
854 hfsmp
->hfs_private_attr
[FILE_HARDLINKS
].ca_entries
++;
855 INC_FOLDERCOUNT(hfsmp
, hfsmp
->hfs_private_attr
[FILE_HARDLINKS
]);
857 (void)cat_update(hfsmp
, &hfsmp
->hfs_private_desc
[DIR_HARDLINKS
],
858 &hfsmp
->hfs_private_attr
[DIR_HARDLINKS
], NULL
, NULL
);
859 (void)cat_update(hfsmp
, &hfsmp
->hfs_private_desc
[FILE_HARDLINKS
],
860 &hfsmp
->hfs_private_attr
[FILE_HARDLINKS
], NULL
, NULL
);
863 error
= 0; /* rename failure here is not fatal */
865 } else /* Still some links left */ {
869 * Update the start of the link chain.
870 * Note: Directory hard links store the first link in an attribute.
872 if (vnode_isdir(vp
) &&
873 getfirstlink(hfsmp
, cp
->c_fileid
, &firstlink
) == 0 &&
874 firstlink
== cndesc
.cd_cnid
) {
875 if (setfirstlink(hfsmp
, cp
->c_fileid
, nextlinkid
) == 0)
876 cp
->c_attr
.ca_recflags
|= kHFSHasAttributesMask
;
877 } else if (cp
->c_attr
.ca_firstlink
== cndesc
.cd_cnid
) {
878 cp
->c_attr
.ca_firstlink
= nextlinkid
;
880 /* Update previous link. */
882 (void) cat_update_siblinglinks(hfsmp
, prevlinkid
, HFS_IGNORABLE_LINK
, nextlinkid
);
884 /* Update next link. */
886 (void) cat_update_siblinglinks(hfsmp
, nextlinkid
, prevlinkid
, HFS_IGNORABLE_LINK
);
891 * The call to cat_releasedesc below will only release the name
892 * buffer; it does not zero out the rest of the fields in the
893 * 'cat_desc' data structure.
895 * As a result, since there are still other links at this point,
896 * we need to make the current cnode descriptor point to the raw
897 * inode. If a path-based system call comes along first, it will
898 * replace the descriptor with a valid link ID. If a userland
899 * process already has a file descriptor open, then they will
900 * bypass that lookup, though. Replacing the descriptor CNID with
901 * the raw inode will force it to generate a new full path.
903 cp
->c_cnid
= cp
->c_fileid
;
905 /* Push new link count to disk. */
906 cp
->c_ctime
= tv
.tv_sec
;
907 (void) cat_update(hfsmp
, &cp
->c_desc
, &cp
->c_attr
, NULL
, NULL
);
909 /* All done with the system files. */
910 hfs_systemfile_unlock(hfsmp
, lockflags
);
913 /* Update file system stats. */
914 hfs_volupdate(hfsmp
, VOL_RMFILE
, (dcp
->c_cnid
== kHFSRootFolderID
));
917 * All done with this cnode's descriptor...
919 * Note: all future catalog calls for this cnode may be
920 * by fileid only. This is OK for HFS (which doesn't have
921 * file thread records) since HFS doesn't support hard links.
923 cat_releasedesc(&cp
->c_desc
);
927 hfs_systemfile_unlock(hfsmp
, lockflags
);
930 hfs_end_transaction(hfsmp
);
933 dcp
->c_flag
&= ~C_DIR_MODIFICATION
;
934 wakeup((caddr_t
)&dcp
->c_flag
);
941 * Initialize the HFS+ private system directories.
943 * These directories are used to hold the inodes
944 * for file and directory hardlinks as well as
945 * open-unlinked files.
947 * If they don't yet exist they will get created.
949 * This call is assumed to be made during mount.
952 hfs_privatedir_init(struct hfsmount
* hfsmp
, enum privdirtype type
)
954 struct vnode
* dvp
= NULLVP
;
955 struct cnode
* dcp
= NULL
;
956 struct cat_desc
*priv_descp
;
957 struct cat_attr
*priv_attrp
;
958 struct FndrDirInfo
* fndrinfo
;
964 if (hfsmp
->hfs_flags
& HFS_STANDARD
) {
968 priv_descp
= &hfsmp
->hfs_private_desc
[type
];
969 priv_attrp
= &hfsmp
->hfs_private_attr
[type
];
971 /* Check if directory already exists. */
972 if (priv_descp
->cd_cnid
!= 0) {
976 priv_descp
->cd_parentcnid
= kRootDirID
;
977 priv_descp
->cd_nameptr
= (const u_int8_t
*)hfs_private_names
[type
];
978 priv_descp
->cd_namelen
= strlen((const char *)priv_descp
->cd_nameptr
);
979 priv_descp
->cd_flags
= CD_ISDIR
| CD_DECOMPOSED
;
981 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
982 error
= cat_lookup(hfsmp
, priv_descp
, 0, 0, NULL
, priv_attrp
, NULL
, NULL
);
983 hfs_systemfile_unlock(hfsmp
, lockflags
);
986 if (type
== FILE_HARDLINKS
) {
987 hfsmp
->hfs_metadata_createdate
= priv_attrp
->ca_itime
;
989 priv_descp
->cd_cnid
= priv_attrp
->ca_fileid
;
993 /* Directory is missing, if this is read-only then we're done. */
994 if (hfsmp
->hfs_flags
& HFS_READ_ONLY
) {
998 /* Grab the root directory so we can update it later. */
999 if (hfs_vget(hfsmp
, kRootDirID
, &dvp
, 0, 0) != 0) {
1004 /* Setup the default attributes */
1005 bzero(priv_attrp
, sizeof(struct cat_attr
));
1006 priv_attrp
->ca_flags
= UF_IMMUTABLE
| UF_HIDDEN
;
1007 priv_attrp
->ca_mode
= S_IFDIR
;
1008 if (type
== DIR_HARDLINKS
) {
1009 priv_attrp
->ca_mode
|= S_ISVTX
| S_IRUSR
| S_IXUSR
| S_IRGRP
|
1010 S_IXGRP
| S_IROTH
| S_IXOTH
;
1012 priv_attrp
->ca_linkcount
= 1;
1013 priv_attrp
->ca_itime
= hfsmp
->hfs_itime
;
1014 priv_attrp
->ca_recflags
= kHFSHasFolderCountMask
;
1016 fndrinfo
= (struct FndrDirInfo
*)&priv_attrp
->ca_finderinfo
;
1017 fndrinfo
->frLocation
.v
= SWAP_BE16(16384);
1018 fndrinfo
->frLocation
.h
= SWAP_BE16(16384);
1019 fndrinfo
->frFlags
= SWAP_BE16(kIsInvisible
+ kNameLocked
);
1021 if (hfs_start_transaction(hfsmp
) != 0) {
1026 /* Need the catalog and EA b-trees for CNID acquisition */
1027 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
| SFL_ATTRIBUTE
, HFS_EXCLUSIVE_LOCK
);
1029 /* Make sure there's space in the Catalog file. */
1030 if (cat_preflight(hfsmp
, CAT_CREATE
, NULL
, 0) != 0) {
1031 hfs_systemfile_unlock(hfsmp
, lockflags
);
1035 /* Get the CNID for use */
1037 if ((error
= cat_acquire_cnid(hfsmp
, &new_id
))) {
1038 hfs_systemfile_unlock (hfsmp
, lockflags
);
1042 /* Create the private directory on disk. */
1043 error
= cat_create(hfsmp
, new_id
, priv_descp
, priv_attrp
, NULL
);
1045 priv_descp
->cd_cnid
= priv_attrp
->ca_fileid
;
1047 /* Update the parent directory */
1049 INC_FOLDERCOUNT(hfsmp
, dcp
->c_attr
);
1050 dcp
->c_dirchangecnt
++;
1051 hfs_incr_gencount(dcp
);
1053 dcp
->c_ctime
= tv
.tv_sec
;
1054 dcp
->c_mtime
= tv
.tv_sec
;
1055 (void) cat_update(hfsmp
, &dcp
->c_desc
, &dcp
->c_attr
, NULL
, NULL
);
1058 hfs_systemfile_unlock(hfsmp
, lockflags
);
1063 if (type
== FILE_HARDLINKS
) {
1064 hfsmp
->hfs_metadata_createdate
= priv_attrp
->ca_itime
;
1066 hfs_volupdate(hfsmp
, VOL_MKDIR
, 1);
1069 hfs_end_transaction(hfsmp
);
1075 if ((error
== 0) && (type
== DIR_HARDLINKS
)) {
1076 hfs_xattr_init(hfsmp
);
1082 * Lookup a hardlink link (from chain)
1085 hfs_lookup_siblinglinks(struct hfsmount
*hfsmp
, cnid_t linkfileid
, cnid_t
*prevlinkid
, cnid_t
*nextlinkid
)
1093 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
1095 error
= cat_lookup_siblinglinks(hfsmp
, linkfileid
, prevlinkid
, nextlinkid
);
1096 if (error
== ENOLINK
) {
1097 hfs_systemfile_unlock(hfsmp
, lockflags
);
1098 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_ATTRIBUTE
, HFS_SHARED_LOCK
);
1100 error
= getfirstlink(hfsmp
, linkfileid
, nextlinkid
);
1102 hfs_systemfile_unlock(hfsmp
, lockflags
);
1108 /* Find the oldest / last hardlink in the link chain */
1110 hfs_lookup_lastlink (struct hfsmount
*hfsmp
, cnid_t linkfileid
,
1111 cnid_t
*lastid
, struct cat_desc
*cdesc
) {
1117 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
1119 error
= cat_lookup_lastlink(hfsmp
, linkfileid
, lastid
, cdesc
);
1121 hfs_systemfile_unlock(hfsmp
, lockflags
);
1124 * cat_lookup_lastlink will zero out the lastid/cdesc arguments as needed
1132 * Cache the origin of a directory or file hard link
1134 * cnode must be lock on entry
1137 hfs_savelinkorigin(cnode_t
*cp
, cnid_t parentcnid
)
1139 linkorigin_t
*origin
= NULL
;
1140 thread_t thread
= current_thread();
1142 int maxorigins
= (S_ISDIR(cp
->c_mode
)) ? MAX_CACHED_ORIGINS
: MAX_CACHED_FILE_ORIGINS
;
1144 * Look for an existing origin first. If not found, create/steal one.
1146 TAILQ_FOREACH(origin
, &cp
->c_originlist
, lo_link
) {
1148 if (origin
->lo_thread
== thread
) {
1149 TAILQ_REMOVE(&cp
->c_originlist
, origin
, lo_link
);
1153 if (origin
== NULL
) {
1154 /* Recycle the last (i.e., the oldest) if we have too many. */
1155 if (count
> maxorigins
) {
1156 origin
= TAILQ_LAST(&cp
->c_originlist
, hfs_originhead
);
1157 TAILQ_REMOVE(&cp
->c_originlist
, origin
, lo_link
);
1159 origin
= hfs_malloc(sizeof(linkorigin_t
));
1161 origin
->lo_thread
= thread
;
1163 origin
->lo_cnid
= cp
->c_cnid
;
1164 origin
->lo_parentcnid
= parentcnid
;
1165 TAILQ_INSERT_HEAD(&cp
->c_originlist
, origin
, lo_link
);
1169 * Release any cached origins for a directory or file hard link
1171 * cnode must be lock on entry
1174 hfs_relorigins(struct cnode
*cp
)
1176 linkorigin_t
*origin
, *prev
;
1178 TAILQ_FOREACH_SAFE(origin
, &cp
->c_originlist
, lo_link
, prev
) {
1179 hfs_free(origin
, sizeof(*origin
));
1181 TAILQ_INIT(&cp
->c_originlist
);
1185 * Release a specific origin for a directory or file hard link
1187 * cnode must be lock on entry
1190 hfs_relorigin(struct cnode
*cp
, cnid_t parentcnid
)
1192 linkorigin_t
*origin
, *prev
;
1193 thread_t thread
= current_thread();
1195 TAILQ_FOREACH_SAFE(origin
, &cp
->c_originlist
, lo_link
, prev
) {
1196 if (origin
->lo_thread
== thread
) {
1197 TAILQ_REMOVE(&cp
->c_originlist
, origin
, lo_link
);
1198 hfs_free(origin
, sizeof(*origin
));
1200 } else if (origin
->lo_parentcnid
== parentcnid
) {
1202 * If the threads don't match, then we don't want to
1203 * delete the entry because that might cause other threads
1204 * to fall back and use whatever happens to be in
1205 * c_parentcnid or the wrong link ID. By setting the
1206 * values to zero here, it should serve as an indication
1207 * that the path is no longer valid and that's better than
1208 * using a random parent ID or link ID.
1210 origin
->lo_parentcnid
= 0;
1211 origin
->lo_cnid
= 0;
1217 * Test if a directory or file hard link has a cached origin
1219 * cnode must be lock on entry
1222 hfs_haslinkorigin(cnode_t
*cp
)
1224 if (cp
->c_flag
& C_HARDLINK
) {
1225 linkorigin_t
*origin
;
1226 thread_t thread
= current_thread();
1228 TAILQ_FOREACH(origin
, &cp
->c_originlist
, lo_link
) {
1229 if (origin
->lo_thread
== thread
) {
1230 return origin
->lo_cnid
!= 0;
1238 * Obtain the current parent cnid of a directory or file hard link
1240 * cnode must be lock on entry
1243 hfs_currentparent(cnode_t
*cp
, bool have_lock
)
1245 if (cp
->c_flag
& C_HARDLINK
) {
1247 hfs_lock_always(cp
, HFS_SHARED_LOCK
);
1249 linkorigin_t
*origin
;
1250 thread_t thread
= current_thread();
1252 TAILQ_FOREACH(origin
, &cp
->c_originlist
, lo_link
) {
1253 if (origin
->lo_thread
== thread
) {
1256 return (origin
->lo_parentcnid
);
1263 return (cp
->c_parentcnid
);
1267 * Obtain the current cnid of a directory or file hard link
1269 * cnode must be lock on entry
1272 hfs_currentcnid(cnode_t
*cp
)
1274 if (cp
->c_flag
& C_HARDLINK
) {
1275 linkorigin_t
*origin
;
1276 thread_t thread
= current_thread();
1278 TAILQ_FOREACH(origin
, &cp
->c_originlist
, lo_link
) {
1279 if (origin
->lo_thread
== thread
) {
1280 return (origin
->lo_cnid
);
1284 return (cp
->c_cnid
);
1289 * Set the first link attribute for a given file id.
1291 * The attributes b-tree must already be locked.
1292 * If journaling is enabled, a transaction must already be started.
1295 setfirstlink(struct hfsmount
* hfsmp
, cnid_t fileid
, cnid_t firstlink
)
1298 BTreeIterator
* iterator
;
1299 FSBufferDescriptor btdata
;
1300 u_int8_t attrdata
[FIRST_LINK_XATTR_REC_SIZE
];
1301 HFSPlusAttrData
*dataptr
;
1305 if (hfsmp
->hfs_attribute_cp
== NULL
) {
1308 iterator
= hfs_mallocz(sizeof(*iterator
));
1310 result
= hfs_buildattrkey(fileid
, FIRST_LINK_XATTR_NAME
, (HFSPlusAttrKey
*)&iterator
->key
);
1314 dataptr
= (HFSPlusAttrData
*)&attrdata
[0];
1315 dataptr
->recordType
= kHFSPlusAttrInlineData
;
1316 dataptr
->reserved
[0] = 0;
1317 dataptr
->reserved
[1] = 0;
1320 * Since attrData is variable length, we calculate the size of
1321 * attrData by subtracting the size of all other members of
1322 * structure HFSPlusAttData from the size of attrdata.
1324 (void)snprintf((char *)&dataptr
->attrData
[0],
1325 sizeof(dataptr
) - (4 * sizeof(uint32_t)),
1326 "%lu", (unsigned long)firstlink
);
1327 dataptr
->attrSize
= 1 + strlen((char *)&dataptr
->attrData
[0]);
1329 /* Calculate size of record rounded up to multiple of 2 bytes. */
1330 datasize
= sizeof(HFSPlusAttrData
) - 2 + dataptr
->attrSize
+ ((dataptr
->attrSize
& 1) ? 1 : 0);
1332 btdata
.bufferAddress
= dataptr
;
1333 btdata
.itemSize
= datasize
;
1334 btdata
.itemCount
= 1;
1336 btfile
= hfsmp
->hfs_attribute_cp
->c_datafork
;
1338 /* Insert the attribute. */
1339 result
= BTInsertRecord(btfile
, iterator
, &btdata
, datasize
);
1340 if (result
== btExists
) {
1341 result
= BTReplaceRecord(btfile
, iterator
, &btdata
, datasize
);
1343 (void) BTFlushPath(btfile
);
1345 hfs_free(iterator
, sizeof(*iterator
));
1347 return MacToVFSError(result
);
1351 * Get the first link attribute for a given file id.
1353 * The attributes b-tree must already be locked.
1356 getfirstlink(struct hfsmount
* hfsmp
, cnid_t fileid
, cnid_t
*firstlink
)
1359 BTreeIterator
* iterator
;
1360 FSBufferDescriptor btdata
;
1361 u_int8_t attrdata
[FIRST_LINK_XATTR_REC_SIZE
];
1362 HFSPlusAttrData
*dataptr
;
1366 if (hfsmp
->hfs_attribute_cp
== NULL
) {
1369 iterator
= hfs_mallocz(sizeof(*iterator
));
1371 result
= hfs_buildattrkey(fileid
, FIRST_LINK_XATTR_NAME
, (HFSPlusAttrKey
*)&iterator
->key
);
1375 dataptr
= (HFSPlusAttrData
*)&attrdata
[0];
1376 datasize
= sizeof(attrdata
);
1378 btdata
.bufferAddress
= dataptr
;
1379 btdata
.itemSize
= sizeof(attrdata
);
1380 btdata
.itemCount
= 1;
1382 btfile
= hfsmp
->hfs_attribute_cp
->c_datafork
;
1384 result
= BTSearchRecord(btfile
, iterator
, &btdata
, NULL
, NULL
);
1388 if (dataptr
->attrSize
< 3) {
1392 *firstlink
= strtoul((char*)&dataptr
->attrData
[0], NULL
, 10);
1394 hfs_free(iterator
, sizeof(*iterator
));
1396 return MacToVFSError(result
);
1399 errno_t
hfs_first_link(hfsmount_t
*hfsmp
, cnode_t
*cp
, cnid_t
*link_id
)
1403 if (S_ISDIR(cp
->c_mode
)) {
1404 int lockf
= hfs_systemfile_lock(hfsmp
, SFL_ATTRIBUTE
, HFS_SHARED_LOCK
);
1406 error
= getfirstlink(hfsmp
, cp
->c_fileid
, link_id
);
1408 hfs_systemfile_unlock(hfsmp
, lockf
);
1410 if (cp
->c_attr
.ca_firstlink
)
1411 *link_id
= cp
->c_attr
.ca_firstlink
;
1413 // This can happen if the cnode has been deleted