2 * Copyright (c) 2000-2009 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@
29 #include <sys/systm.h>
30 #include <sys/param.h>
31 #include <sys/kernel.h>
32 #include <sys/file_internal.h>
33 #include <sys/dirent.h>
36 #include <sys/mount.h>
37 #include <sys/vnode_if.h>
38 #include <sys/vnode_internal.h>
39 #include <sys/malloc.h>
41 #include <sys/ubc_internal.h>
42 #include <sys/paths.h>
43 #include <sys/quota.h>
46 #include <sys/kauth.h>
47 #include <sys/uio_internal.h>
49 #include <miscfs/specfs/specdev.h>
50 #include <miscfs/fifofs/fifo.h>
51 #include <vfs/vfs_support.h>
52 #include <machine/spl.h>
54 #include <sys/kdebug.h>
55 #include <sys/sysctl.h>
58 #include "hfs_catalog.h"
59 #include "hfs_cnode.h"
61 #include "hfs_mount.h"
62 #include "hfs_quota.h"
63 #include "hfs_endian.h"
65 #include "hfscommon/headers/BTreesInternal.h"
66 #include "hfscommon/headers/FileMgrInternal.h"
68 #define KNDETACH_VNLOCKED 0x00000001
70 /* Global vfs data structures for hfs */
72 /* Always F_FULLFSYNC? 1=yes,0=no (default due to "various" reasons is 'no') */
73 int always_do_fullfsync
= 0;
74 SYSCTL_DECL(_vfs_generic
);
75 SYSCTL_INT (_vfs_generic
, OID_AUTO
, always_do_fullfsync
, CTLFLAG_RW
, &always_do_fullfsync
, 0, "always F_FULLFSYNC when fsync is called");
77 static int hfs_makenode(struct vnode
*dvp
, struct vnode
**vpp
,
78 struct componentname
*cnp
, struct vnode_attr
*vap
,
81 static int hfs_metasync(struct hfsmount
*hfsmp
, daddr64_t node
, __unused
struct proc
*p
);
82 static int hfs_metasync_all(struct hfsmount
*hfsmp
);
84 static int hfs_removedir(struct vnode
*, struct vnode
*, struct componentname
*,
87 static int hfs_removefile(struct vnode
*, struct vnode
*, struct componentname
*,
88 int, int, int, struct vnode
*);
91 static int hfsfifo_read(struct vnop_read_args
*);
92 static int hfsfifo_write(struct vnop_write_args
*);
93 static int hfsfifo_close(struct vnop_close_args
*);
95 extern int (**fifo_vnodeop_p
)(void *);
98 static int hfs_vnop_close(struct vnop_close_args
*);
99 static int hfs_vnop_create(struct vnop_create_args
*);
100 static int hfs_vnop_exchange(struct vnop_exchange_args
*);
101 static int hfs_vnop_fsync(struct vnop_fsync_args
*);
102 static int hfs_vnop_mkdir(struct vnop_mkdir_args
*);
103 static int hfs_vnop_mknod(struct vnop_mknod_args
*);
104 static int hfs_vnop_getattr(struct vnop_getattr_args
*);
105 static int hfs_vnop_open(struct vnop_open_args
*);
106 static int hfs_vnop_readdir(struct vnop_readdir_args
*);
107 static int hfs_vnop_remove(struct vnop_remove_args
*);
108 static int hfs_vnop_rename(struct vnop_rename_args
*);
109 static int hfs_vnop_rmdir(struct vnop_rmdir_args
*);
110 static int hfs_vnop_symlink(struct vnop_symlink_args
*);
111 static int hfs_vnop_setattr(struct vnop_setattr_args
*);
112 static int hfs_vnop_readlink(struct vnop_readlink_args
*);
113 static int hfs_vnop_pathconf(struct vnop_pathconf_args
*);
114 static int hfs_vnop_whiteout(struct vnop_whiteout_args
*);
115 static int hfsspec_read(struct vnop_read_args
*);
116 static int hfsspec_write(struct vnop_write_args
*);
117 static int hfsspec_close(struct vnop_close_args
*);
119 /* Options for hfs_removedir and hfs_removefile */
120 #define HFSRM_SKIP_RESERVE 0x01
125 /*****************************************************************************
127 * Common Operations on vnodes
129 *****************************************************************************/
132 * Create a regular file.
135 hfs_vnop_create(struct vnop_create_args
*ap
)
140 error
= hfs_makenode(ap
->a_dvp
, ap
->a_vpp
, ap
->a_cnp
, ap
->a_vap
, ap
->a_context
);
143 * We speculatively skipped the original lookup of the leaf
144 * for CREATE. Since it exists, go get it as long as they
145 * didn't want an exclusive create.
147 if ((error
== EEXIST
) && !(ap
->a_vap
->va_vaflags
& VA_EXCLUSIVE
)) {
148 struct vnop_lookup_args args
;
150 args
.a_desc
= &vnop_lookup_desc
;
151 args
.a_dvp
= ap
->a_dvp
;
152 args
.a_vpp
= ap
->a_vpp
;
153 args
.a_cnp
= ap
->a_cnp
;
154 args
.a_context
= ap
->a_context
;
155 args
.a_cnp
->cn_nameiop
= LOOKUP
;
156 error
= hfs_vnop_lookup(&args
);
158 * We can also race with remove for this file.
160 if (error
== ENOENT
) {
164 /* Make sure it was file. */
165 if ((error
== 0) && !vnode_isreg(*args
.a_vpp
)) {
166 vnode_put(*args
.a_vpp
);
169 args
.a_cnp
->cn_nameiop
= CREATE
;
175 * Make device special file.
178 hfs_vnop_mknod(struct vnop_mknod_args
*ap
)
180 struct vnode_attr
*vap
= ap
->a_vap
;
181 struct vnode
*dvp
= ap
->a_dvp
;
182 struct vnode
**vpp
= ap
->a_vpp
;
186 if (VTOVCB(dvp
)->vcbSigWord
!= kHFSPlusSigWord
) {
190 /* Create the vnode */
191 error
= hfs_makenode(dvp
, vpp
, ap
->a_cnp
, vap
, ap
->a_context
);
196 cp
->c_touch_acctime
= TRUE
;
197 cp
->c_touch_chgtime
= TRUE
;
198 cp
->c_touch_modtime
= TRUE
;
200 if ((vap
->va_rdev
!= VNOVAL
) &&
201 (vap
->va_type
== VBLK
|| vap
->va_type
== VCHR
))
202 cp
->c_rdev
= vap
->va_rdev
;
209 * hfs_ref_data_vp(): returns the data fork vnode for a given cnode.
210 * In the (hopefully rare) case where the data fork vnode is not
211 * present, it will use hfs_vget() to create a new vnode for the
214 * NOTE: If successful and a vnode is returned, the caller is responsible
215 * for releasing the returned vnode with vnode_rele().
218 hfs_ref_data_vp(struct cnode
*cp
, struct vnode
**data_vp
, int skiplock
)
222 if (!data_vp
|| !cp
) /* sanity check incoming parameters */
225 /* maybe we should take the hfs cnode lock here, and if so, use the skiplock parameter to tell us not to */
227 if (!skiplock
) hfs_lock(cp
, HFS_SHARED_LOCK
);
228 struct vnode
*c_vp
= cp
->c_vp
;
230 /* we already have a data vnode */
232 vref
= vnode_ref(*data_vp
);
233 if (!skiplock
) hfs_unlock(cp
);
239 /* no data fork vnode in the cnode, so ask hfs for one. */
241 if (!cp
->c_rsrc_vp
) {
242 /* if we don't have either a c_vp or c_rsrc_vp, we can't really do anything useful */
244 if (!skiplock
) hfs_unlock(cp
);
248 if (0 == hfs_vget(VTOHFS(cp
->c_rsrc_vp
), cp
->c_cnid
, data_vp
, 1) &&
250 vref
= vnode_ref(*data_vp
);
252 if (!skiplock
) hfs_unlock(cp
);
258 /* there was an error getting the vnode */
260 if (!skiplock
) hfs_unlock(cp
);
265 * hfs_lazy_init_decmpfs_cnode(): returns the decmpfs_cnode for a cnode,
266 * allocating it if necessary; returns NULL if there was an allocation error
268 static decmpfs_cnode
*
269 hfs_lazy_init_decmpfs_cnode(struct cnode
*cp
)
272 decmpfs_cnode
*dp
= NULL
;
273 MALLOC_ZONE(dp
, decmpfs_cnode
*, sizeof(decmpfs_cnode
), M_DECMPFS_CNODE
, M_WAITOK
);
275 /* error allocating a decmpfs cnode */
278 decmpfs_cnode_init(dp
);
279 if (!OSCompareAndSwapPtr(NULL
, dp
, (void * volatile *)&cp
->c_decmp
)) {
280 /* another thread got here first, so free the decmpfs_cnode we allocated */
281 decmpfs_cnode_destroy(dp
);
282 FREE_ZONE(dp
, sizeof(*dp
), M_DECMPFS_CNODE
);
290 * hfs_file_is_compressed(): returns 1 if the file is compressed, and 0 (zero) if not.
291 * if the file's compressed flag is set, makes sure that the decmpfs_cnode field
292 * is allocated by calling hfs_lazy_init_decmpfs_cnode(), then makes sure it is populated,
293 * or else fills it in via the decmpfs_file_is_compressed() function.
296 hfs_file_is_compressed(struct cnode
*cp
, int skiplock
)
300 /* fast check to see if file is compressed. If flag is clear, just answer no */
301 if (!(cp
->c_flags
& UF_COMPRESSED
)) {
305 decmpfs_cnode
*dp
= hfs_lazy_init_decmpfs_cnode(cp
);
307 /* error allocating a decmpfs cnode, treat the file as uncompressed */
311 /* flag was set, see if the decmpfs_cnode state is valid (zero == invalid) */
312 uint32_t decmpfs_state
= decmpfs_cnode_get_vnode_state(dp
);
313 switch(decmpfs_state
) {
314 case FILE_IS_COMPRESSED
:
315 case FILE_IS_CONVERTING
: /* treat decompressing files as if they are compressed */
317 case FILE_IS_NOT_COMPRESSED
:
319 /* otherwise the state is not cached yet */
322 /* decmpfs hasn't seen this file yet, so call decmpfs_file_is_compressed() to init the decmpfs_cnode struct */
323 struct vnode
*data_vp
= NULL
;
324 if (0 == hfs_ref_data_vp(cp
, &data_vp
, skiplock
)) {
326 ret
= decmpfs_file_is_compressed(data_vp
, VTOCMP(data_vp
)); // fill in decmpfs_cnode
333 /* hfs_uncompressed_size_of_compressed_file() - get the uncompressed size of the file.
334 * if the caller has passed a valid vnode (has a ref count > 0), then hfsmp and fid are not required.
335 * if the caller doesn't have a vnode, pass NULL in vp, and pass valid hfsmp and fid.
336 * files size is returned in size (required)
339 hfs_uncompressed_size_of_compressed_file(struct hfsmount
*hfsmp
, struct vnode
*vp
, cnid_t fid
, off_t
*size
, int skiplock
)
342 int putaway
= 0; /* flag to remember if we used hfs_vget() */
345 return EINVAL
; /* no place to put the file size */
349 if (!hfsmp
|| !fid
) { /* make sure we have the required parameters */
352 if (0 != hfs_vget(hfsmp
, fid
, &vp
, skiplock
)) { /* vnode is null, use hfs_vget() to get it */
355 putaway
= 1; /* note that hfs_vget() was used to aquire the vnode */
358 /* this double check for compression (hfs_file_is_compressed)
359 * ensures the cached size is present in case decmpfs hasn't
360 * encountered this node yet.
362 if ( ( NULL
!= vp
) && hfs_file_is_compressed(VTOC(vp
), skiplock
) ) {
363 *size
= decmpfs_cnode_get_vnode_cached_size(VTOCMP(vp
)); /* file info will be cached now, so get size */
368 if (putaway
) { /* did we use hfs_vget() to get this vnode? */
369 vnode_put(vp
); /* if so, release it and set it to null */
376 hfs_hides_rsrc(vfs_context_t ctx
, struct cnode
*cp
, int skiplock
)
378 if (ctx
== decmpfs_ctx
)
380 if (!hfs_file_is_compressed(cp
, skiplock
))
382 return decmpfs_hides_rsrc(ctx
, cp
->c_decmp
);
386 hfs_hides_xattr(vfs_context_t ctx
, struct cnode
*cp
, const char *name
, int skiplock
)
388 if (ctx
== decmpfs_ctx
)
390 if (!hfs_file_is_compressed(cp
, skiplock
))
392 return decmpfs_hides_xattr(ctx
, cp
->c_decmp
, name
);
394 #endif /* HFS_COMPRESSION */
397 * Open a file/directory.
400 hfs_vnop_open(struct vnop_open_args
*ap
)
402 struct vnode
*vp
= ap
->a_vp
;
406 static int past_bootup
= 0;
407 struct cnode
*cp
= VTOC(vp
);
408 struct hfsmount
*hfsmp
= VTOHFS(vp
);
411 if (ap
->a_mode
& FWRITE
) {
413 if ( hfs_file_is_compressed(cp
, 1) ) { /* 1 == don't take the cnode lock */
414 /* opening a compressed file for write, so convert it to decompressed */
415 struct vnode
*data_vp
= NULL
;
416 error
= hfs_ref_data_vp(cp
, &data_vp
, 1); /* 1 == don't take the cnode lock */
419 error
= decmpfs_decompress_file(data_vp
, VTOCMP(data_vp
), -1, 1, 0);
430 if (hfs_file_is_compressed(cp
, 1) ) { /* 1 == don't take the cnode lock */
431 if (VNODE_IS_RSRC(vp
)) {
432 /* opening the resource fork of a compressed file, so nothing to do */
434 /* opening a compressed file for read, make sure it validates */
435 error
= decmpfs_validate_compressed_file(vp
, VTOCMP(vp
));
444 * Files marked append-only must be opened for appending.
446 if ((cp
->c_flags
& APPEND
) && !vnode_isdir(vp
) &&
447 (ap
->a_mode
& (FWRITE
| O_APPEND
)) == FWRITE
)
450 if (vnode_isreg(vp
) && !UBCINFOEXISTS(vp
))
451 return (EBUSY
); /* file is in use by the kernel */
453 /* Don't allow journal file to be opened externally. */
454 if (cp
->c_fileid
== hfsmp
->hfs_jnlfileid
)
457 /* If we're going to write to the file, initialize quotas. */
459 if ((ap
->a_mode
& FWRITE
) && (hfsmp
->hfs_flags
& HFS_QUOTAS
))
460 (void)hfs_getinoquota(cp
);
464 * On the first (non-busy) open of a fragmented
465 * file attempt to de-frag it (if its less than 20MB).
467 if ((hfsmp
->hfs_flags
& HFS_READ_ONLY
) ||
468 (hfsmp
->jnl
== NULL
) ||
470 !vnode_isreg(vp
) || vnode_isinuse(vp
, 0) || vnode_isnamedstream(vp
)) {
472 !vnode_isreg(vp
) || vnode_isinuse(vp
, 0)) {
477 if ((error
= hfs_lock(cp
, HFS_EXCLUSIVE_LOCK
)))
481 fp
->ff_extents
[7].blockCount
!= 0 &&
482 fp
->ff_size
<= (20 * 1024 * 1024)) {
486 * Wait until system bootup is done (3 min).
487 * And don't relocate a file that's been modified
488 * within the past minute -- this can lead to
494 if (tv
.tv_sec
> (60*3)) {
500 if ((now
.tv_sec
- cp
->c_mtime
) > 60) {
504 if (past_bootup
&& no_mods
) {
505 (void) hfs_relocate(vp
, hfsmp
->nextAllocation
+ 4096,
506 vfs_context_ucred(ap
->a_context
),
507 vfs_context_proc(ap
->a_context
));
517 * Close a file/directory.
521 struct vnop_close_args
/* {
524 vfs_context_t a_context;
527 register struct vnode
*vp
= ap
->a_vp
;
528 register struct cnode
*cp
;
529 struct proc
*p
= vfs_context_proc(ap
->a_context
);
530 struct hfsmount
*hfsmp
;
532 int tooktrunclock
= 0;
535 if ( hfs_lock(VTOC(vp
), HFS_EXCLUSIVE_LOCK
) != 0)
541 * If the rsrc fork is a named stream, it can cause the data fork to
542 * stay around, preventing de-allocation of these blocks.
543 * Do checks for truncation on close. Purge extra extents if they exist.
544 * Make sure the vp is not a directory, and that it has a resource fork,
545 * and that resource fork is also a named stream.
548 if ((vp
->v_type
== VREG
) && (cp
->c_rsrc_vp
)
549 && (vnode_isnamedstream(cp
->c_rsrc_vp
))) {
552 blks
= howmany(VTOF(vp
)->ff_size
, VTOVCB(vp
)->blockSize
);
554 * If there are extra blocks and there are only 2 refs on
555 * this vp (ourselves + rsrc fork holding ref on us), go ahead
556 * and try to truncate.
558 if ((blks
< VTOF(vp
)->ff_blocks
) && (!vnode_isinuse(vp
, 2))) {
559 // release cnode lock; must acquire truncate lock BEFORE cnode lock
562 hfs_lock_truncate(cp
, TRUE
);
565 if (hfs_lock(VTOC(vp
), HFS_EXCLUSIVE_LOCK
) != 0) {
566 hfs_unlock_truncate(cp
, TRUE
);
567 // bail out if we can't re-acquire cnode lock
570 // now re-test to make sure it's still valid
572 knownrefs
= 1 + vnode_isnamedstream(cp
->c_rsrc_vp
);
573 if (!vnode_isinuse(vp
, knownrefs
)){
574 // now we can truncate the file, if necessary
575 blks
= howmany(VTOF(vp
)->ff_size
, VTOVCB(vp
)->blockSize
);
576 if (blks
< VTOF(vp
)->ff_blocks
){
577 (void) hfs_truncate(vp
, VTOF(vp
)->ff_size
, IO_NDELAY
, 0, 0, ap
->a_context
);
585 // if we froze the fs and we're exiting, then "thaw" the fs
586 if (hfsmp
->hfs_freezing_proc
== p
&& proc_exiting(p
)) {
587 hfsmp
->hfs_freezing_proc
= NULL
;
588 hfs_global_exclusive_lock_release(hfsmp
);
589 lck_rw_unlock_exclusive(&hfsmp
->hfs_insync
);
592 busy
= vnode_isinuse(vp
, 1);
595 hfs_touchtimes(VTOHFS(vp
), cp
);
597 if (vnode_isdir(vp
)) {
598 hfs_reldirhints(cp
, busy
);
599 } else if (vnode_issystem(vp
) && !busy
) {
604 hfs_unlock_truncate(cp
, TRUE
);
608 if (ap
->a_fflag
& FWASWRITTEN
) {
609 hfs_sync_ejectable(hfsmp
);
616 * Get basic attributes.
619 hfs_vnop_getattr(struct vnop_getattr_args
*ap
)
621 #define VNODE_ATTR_TIMES \
622 (VNODE_ATTR_va_access_time|VNODE_ATTR_va_change_time|VNODE_ATTR_va_modify_time)
623 #define VNODE_ATTR_AUTH \
624 (VNODE_ATTR_va_mode | VNODE_ATTR_va_uid | VNODE_ATTR_va_gid | \
625 VNODE_ATTR_va_flags | VNODE_ATTR_va_acl)
627 struct vnode
*vp
= ap
->a_vp
;
628 struct vnode_attr
*vap
= ap
->a_vap
;
629 struct vnode
*rvp
= NULLVP
;
630 struct hfsmount
*hfsmp
;
638 /* we need to inspect the decmpfs state of the file before we take the hfs cnode lock */
641 off_t uncompressed_size
= -1;
642 if (VATTR_IS_ACTIVE(vap
, va_data_size
) || VATTR_IS_ACTIVE(vap
, va_total_alloc
) || VATTR_IS_ACTIVE(vap
, va_data_alloc
) || VATTR_IS_ACTIVE(vap
, va_total_size
)) {
643 /* we only care about whether the file is compressed if asked for the uncompressed size */
644 if (VNODE_IS_RSRC(vp
)) {
645 /* if it's a resource fork, decmpfs may want us to hide the size */
646 hide_size
= hfs_hides_rsrc(ap
->a_context
, cp
, 0);
648 /* if it's a data fork, we need to know if it was compressed so we can report the uncompressed size */
649 compressed
= hfs_file_is_compressed(cp
, 0);
651 if (compressed
&& (VATTR_IS_ACTIVE(vap
, va_data_size
) || VATTR_IS_ACTIVE(vap
, va_total_size
))) {
652 if (0 != hfs_uncompressed_size_of_compressed_file(NULL
, vp
, 0, &uncompressed_size
, 0)) {
653 /* failed to get the uncompressed size, we'll check for this later */
654 uncompressed_size
= -1;
661 * Shortcut for vnode_authorize path. Each of the attributes
662 * in this set is updated atomically so we don't need to take
663 * the cnode lock to access them.
665 if ((vap
->va_active
& ~VNODE_ATTR_AUTH
) == 0) {
666 /* Make sure file still exists. */
667 if (cp
->c_flag
& C_NOEXISTS
)
670 vap
->va_uid
= cp
->c_uid
;
671 vap
->va_gid
= cp
->c_gid
;
672 vap
->va_mode
= cp
->c_mode
;
673 vap
->va_flags
= cp
->c_flags
;
674 vap
->va_supported
|= VNODE_ATTR_AUTH
& ~VNODE_ATTR_va_acl
;
676 if ((cp
->c_attr
.ca_recflags
& kHFSHasSecurityMask
) == 0) {
677 vap
->va_acl
= (kauth_acl_t
) KAUTH_FILESEC_NONE
;
678 VATTR_SET_SUPPORTED(vap
, va_acl
);
685 v_type
= vnode_vtype(vp
);
687 * If time attributes are requested and we have cnode times
688 * that require updating, then acquire an exclusive lock on
689 * the cnode before updating the times. Otherwise we can
690 * just acquire a shared lock.
692 if ((vap
->va_active
& VNODE_ATTR_TIMES
) &&
693 (cp
->c_touch_acctime
|| cp
->c_touch_chgtime
|| cp
->c_touch_modtime
)) {
694 if ((error
= hfs_lock(cp
, HFS_EXCLUSIVE_LOCK
)))
696 hfs_touchtimes(hfsmp
, cp
);
699 if ((error
= hfs_lock(cp
, HFS_SHARED_LOCK
)))
703 if (v_type
== VDIR
) {
704 data_size
= (cp
->c_entries
+ 2) * AVERAGE_HFSDIRENTRY_SIZE
;
706 if (VATTR_IS_ACTIVE(vap
, va_nlink
)) {
710 * For directories, the va_nlink is esentially a count
711 * of the ".." references to a directory plus the "."
712 * reference and the directory itself. So for HFS+ this
713 * becomes the sub-directory count plus two.
715 * In the absence of a sub-directory count we use the
716 * directory's item count. This will be too high in
717 * most cases since it also includes files.
719 if ((hfsmp
->hfs_flags
& HFS_FOLDERCOUNT
) &&
720 (cp
->c_attr
.ca_recflags
& kHFSHasFolderCountMask
))
721 nlink
= cp
->c_attr
.ca_dircount
; /* implied ".." entries */
723 nlink
= cp
->c_entries
;
725 /* Account for ourself and our "." entry */
727 /* Hide our private directories. */
728 if (cp
->c_cnid
== kHFSRootFolderID
) {
729 if (hfsmp
->hfs_private_desc
[FILE_HARDLINKS
].cd_cnid
!= 0) {
732 if (hfsmp
->hfs_private_desc
[DIR_HARDLINKS
].cd_cnid
!= 0) {
736 VATTR_RETURN(vap
, va_nlink
, (u_int64_t
)nlink
);
738 if (VATTR_IS_ACTIVE(vap
, va_nchildren
)) {
741 entries
= cp
->c_entries
;
742 /* Hide our private files and directories. */
743 if (cp
->c_cnid
== kHFSRootFolderID
) {
744 if (hfsmp
->hfs_private_desc
[FILE_HARDLINKS
].cd_cnid
!= 0)
746 if (hfsmp
->hfs_private_desc
[DIR_HARDLINKS
].cd_cnid
!= 0)
748 if (hfsmp
->jnl
|| ((hfsmp
->vcbAtrb
& kHFSVolumeJournaledMask
) && (hfsmp
->hfs_flags
& HFS_READ_ONLY
)))
749 entries
-= 2; /* hide the journal files */
751 VATTR_RETURN(vap
, va_nchildren
, entries
);
754 * The va_dirlinkcount is the count of real directory hard links.
755 * (i.e. its not the sum of the implied "." and ".." references)
757 if (VATTR_IS_ACTIVE(vap
, va_dirlinkcount
)) {
758 VATTR_RETURN(vap
, va_dirlinkcount
, (uint32_t)cp
->c_linkcount
);
761 data_size
= VCTOF(vp
, cp
)->ff_size
;
763 VATTR_RETURN(vap
, va_nlink
, (u_int64_t
)cp
->c_linkcount
);
764 if (VATTR_IS_ACTIVE(vap
, va_data_alloc
)) {
769 VATTR_RETURN(vap
, va_data_alloc
, 0);
770 } else if (compressed
) {
771 /* for compressed files, we report all allocated blocks as belonging to the data fork */
772 blocks
= cp
->c_blocks
;
773 VATTR_RETURN(vap
, va_data_alloc
, blocks
* (u_int64_t
)hfsmp
->blockSize
);
778 blocks
= VCTOF(vp
, cp
)->ff_blocks
;
779 VATTR_RETURN(vap
, va_data_alloc
, blocks
* (u_int64_t
)hfsmp
->blockSize
);
784 /* conditional because 64-bit arithmetic can be expensive */
785 if (VATTR_IS_ACTIVE(vap
, va_total_size
)) {
786 if (v_type
== VDIR
) {
787 VATTR_RETURN(vap
, va_total_size
, (cp
->c_entries
+ 2) * AVERAGE_HFSDIRENTRY_SIZE
);
789 u_int64_t total_size
= ~0ULL;
793 /* we're hiding the size of this file, so just return 0 */
795 } else if (compressed
) {
796 if (uncompressed_size
== -1) {
798 * We failed to get the uncompressed size above,
799 * so we'll fall back to the standard path below
800 * since total_size is still -1
803 /* use the uncompressed size we fetched above */
804 total_size
= uncompressed_size
;
808 if (total_size
== ~0ULL) {
809 if (cp
->c_datafork
) {
810 total_size
= cp
->c_datafork
->ff_size
;
813 if (cp
->c_blocks
- VTOF(vp
)->ff_blocks
) {
814 /* We deal with rsrc fork vnode iocount at the end of the function */
815 error
= hfs_vgetrsrc(hfsmp
, vp
, &rvp
, TRUE
, TRUE
);
818 * hfs_vgetrsrc may have returned a vnode in rvp even though
819 * we got an error, because we specified error_on_unlinked.
820 * We need to drop the iocount after we release the cnode lock, so
821 * it will be taken care of at the end of the function if it's needed.
827 if (rcp
&& rcp
->c_rsrcfork
) {
828 total_size
+= rcp
->c_rsrcfork
->ff_size
;
833 VATTR_RETURN(vap
, va_total_size
, total_size
);
836 if (VATTR_IS_ACTIVE(vap
, va_total_alloc
)) {
837 if (v_type
== VDIR
) {
838 VATTR_RETURN(vap
, va_total_alloc
, 0);
840 VATTR_RETURN(vap
, va_total_alloc
, (u_int64_t
)cp
->c_blocks
* (u_int64_t
)hfsmp
->blockSize
);
845 * If the VFS wants extended security data, and we know that we
846 * don't have any (because it never told us it was setting any)
847 * then we can return the supported bit and no data. If we do
848 * have extended security, we can just leave the bit alone and
849 * the VFS will use the fallback path to fetch it.
851 if (VATTR_IS_ACTIVE(vap
, va_acl
)) {
852 if ((cp
->c_attr
.ca_recflags
& kHFSHasSecurityMask
) == 0) {
853 vap
->va_acl
= (kauth_acl_t
) KAUTH_FILESEC_NONE
;
854 VATTR_SET_SUPPORTED(vap
, va_acl
);
857 if (VATTR_IS_ACTIVE(vap
, va_access_time
)) {
858 /* Access times are lazily updated, get current time if needed */
859 if (cp
->c_touch_acctime
) {
863 vap
->va_access_time
.tv_sec
= tv
.tv_sec
;
865 vap
->va_access_time
.tv_sec
= cp
->c_atime
;
867 vap
->va_access_time
.tv_nsec
= 0;
868 VATTR_SET_SUPPORTED(vap
, va_access_time
);
870 vap
->va_create_time
.tv_sec
= cp
->c_itime
;
871 vap
->va_create_time
.tv_nsec
= 0;
872 vap
->va_modify_time
.tv_sec
= cp
->c_mtime
;
873 vap
->va_modify_time
.tv_nsec
= 0;
874 vap
->va_change_time
.tv_sec
= cp
->c_ctime
;
875 vap
->va_change_time
.tv_nsec
= 0;
876 vap
->va_backup_time
.tv_sec
= cp
->c_btime
;
877 vap
->va_backup_time
.tv_nsec
= 0;
879 /* XXX is this really a good 'optimal I/O size'? */
880 vap
->va_iosize
= hfsmp
->hfs_logBlockSize
;
881 vap
->va_uid
= cp
->c_uid
;
882 vap
->va_gid
= cp
->c_gid
;
883 vap
->va_mode
= cp
->c_mode
;
884 vap
->va_flags
= cp
->c_flags
;
887 * Exporting file IDs from HFS Plus:
889 * For "normal" files the c_fileid is the same value as the
890 * c_cnid. But for hard link files, they are different - the
891 * c_cnid belongs to the active directory entry (ie the link)
892 * and the c_fileid is for the actual inode (ie the data file).
894 * The stat call (getattr) uses va_fileid and the Carbon APIs,
895 * which are hardlink-ignorant, will ask for va_linkid.
897 vap
->va_fileid
= (u_int64_t
)cp
->c_fileid
;
899 * We need to use the origin cache for both hardlinked files
900 * and directories. Hardlinked directories have multiple cnids
901 * and parents (one per link). Hardlinked files also have their
902 * own parents and link IDs separate from the indirect inode number.
903 * If we don't use the cache, we could end up vending the wrong ID
904 * because the cnode will only reflect the link that was looked up most recently.
906 if (cp
->c_flag
& C_HARDLINK
) {
907 vap
->va_linkid
= (u_int64_t
)hfs_currentcnid(cp
);
908 vap
->va_parentid
= (u_int64_t
)hfs_currentparent(cp
);
910 vap
->va_linkid
= (u_int64_t
)cp
->c_cnid
;
911 vap
->va_parentid
= (u_int64_t
)cp
->c_parentcnid
;
913 vap
->va_fsid
= hfsmp
->hfs_raw_dev
;
915 vap
->va_encoding
= cp
->c_encoding
;
916 vap
->va_rdev
= (v_type
== VBLK
|| v_type
== VCHR
) ? cp
->c_rdev
: 0;
918 if (VATTR_IS_ACTIVE(vap
, va_data_size
)) {
920 vap
->va_data_size
= 0;
921 else if (compressed
) {
922 if (uncompressed_size
== -1) {
923 /* failed to get the uncompressed size above, so just return data_size */
924 vap
->va_data_size
= data_size
;
926 /* use the uncompressed size we fetched above */
927 vap
->va_data_size
= uncompressed_size
;
930 vap
->va_data_size
= data_size
;
931 // vap->va_supported |= VNODE_ATTR_va_data_size;
932 VATTR_SET_SUPPORTED(vap
, va_data_size
);
935 vap
->va_data_size
= data_size
;
936 vap
->va_supported
|= VNODE_ATTR_va_data_size
;
939 /* Mark them all at once instead of individual VATTR_SET_SUPPORTED calls. */
940 vap
->va_supported
|= VNODE_ATTR_va_create_time
| VNODE_ATTR_va_modify_time
|
941 VNODE_ATTR_va_change_time
| VNODE_ATTR_va_backup_time
|
942 VNODE_ATTR_va_iosize
| VNODE_ATTR_va_uid
|
943 VNODE_ATTR_va_gid
| VNODE_ATTR_va_mode
|
944 VNODE_ATTR_va_flags
|VNODE_ATTR_va_fileid
|
945 VNODE_ATTR_va_linkid
| VNODE_ATTR_va_parentid
|
946 VNODE_ATTR_va_fsid
| VNODE_ATTR_va_filerev
|
947 VNODE_ATTR_va_encoding
| VNODE_ATTR_va_rdev
;
949 /* If this is the root, let VFS to find out the mount name, which
950 * may be different from the real name. Otherwise, we need to take care
951 * for hardlinked files, which need to be looked up, if necessary
953 if (VATTR_IS_ACTIVE(vap
, va_name
) && (cp
->c_cnid
!= kHFSRootFolderID
)) {
954 struct cat_desc linkdesc
;
957 cnid_t nextlinkid
= 0;
958 cnid_t prevlinkid
= 0;
960 /* Get the name for ATTR_CMN_NAME. We need to take special care for hardlinks
961 * here because the info. for the link ID requested by getattrlist may be
962 * different than what's currently in the cnode. This is because the cnode
963 * will be filled in with the information for the most recent link ID that went
964 * through namei/lookup(). If there are competing lookups for hardlinks that point
965 * to the same inode, one (or more) getattrlists could be vended incorrect name information.
966 * Also, we need to beware of open-unlinked files which could have a namelen of 0.
969 if ((cp
->c_flag
& C_HARDLINK
) &&
970 ((cp
->c_desc
.cd_namelen
== 0) || (vap
->va_linkid
!= cp
->c_cnid
))) {
971 /* If we have no name and our link ID is the raw inode number, then we may
972 * have an open-unlinked file. Go to the next link in this case.
974 if ((cp
->c_desc
.cd_namelen
== 0) && (vap
->va_linkid
== cp
->c_fileid
)) {
975 if ((error
= hfs_lookuplink(hfsmp
, vap
->va_linkid
, &prevlinkid
, &nextlinkid
))){
980 /* just use link obtained from vap above */
981 nextlinkid
= vap
->va_linkid
;
984 /* We need to probe the catalog for the descriptor corresponding to the link ID
985 * stored in nextlinkid. Note that we don't know if we have the exclusive lock
986 * for the cnode here, so we can't just update the descriptor. Instead,
987 * we should just store the descriptor's value locally and then use it to pass
988 * out the name value as needed below.
991 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
992 error
= cat_findname(hfsmp
, nextlinkid
, &linkdesc
);
993 hfs_systemfile_unlock(hfsmp
, lockflags
);
1000 /* By this point, we've either patched up the name above and the c_desc
1001 * points to the correct data, or it already did, in which case we just proceed
1002 * by copying the name into the vap. Note that we will never set va_name to
1003 * supported if nextlinkid is never initialized. This could happen in the degenerate
1004 * case above involving the raw inode number, where it has no nextlinkid. In this case
1005 * we will simply not mark the name bit as supported.
1008 strlcpy(vap
->va_name
, (const char*) linkdesc
.cd_nameptr
, MAXPATHLEN
);
1009 VATTR_SET_SUPPORTED(vap
, va_name
);
1010 cat_releasedesc(&linkdesc
);
1012 else if (cp
->c_desc
.cd_namelen
) {
1013 strlcpy(vap
->va_name
, (const char*) cp
->c_desc
.cd_nameptr
, MAXPATHLEN
);
1014 VATTR_SET_SUPPORTED(vap
, va_name
);
1021 * We need to vnode_put the rsrc fork vnode only *after* we've released
1022 * the cnode lock, since vnode_put can trigger an inactive call, which
1023 * will go back into HFS and try to acquire a cnode lock.
1033 hfs_vnop_setattr(ap
)
1034 struct vnop_setattr_args
/* {
1036 struct vnode_attr *a_vap;
1037 vfs_context_t a_context;
1040 struct vnode_attr
*vap
= ap
->a_vap
;
1041 struct vnode
*vp
= ap
->a_vp
;
1042 struct cnode
*cp
= NULL
;
1043 struct hfsmount
*hfsmp
;
1044 kauth_cred_t cred
= vfs_context_ucred(ap
->a_context
);
1045 struct proc
*p
= vfs_context_proc(ap
->a_context
);
1051 int decmpfs_reset_state
= 0;
1053 we call decmpfs_update_attributes even if the file is not compressed
1054 because we want to update the incoming flags if the xattrs are invalid
1056 error
= decmpfs_update_attributes(vp
, vap
);
1063 /* Don't allow modification of the journal file. */
1064 if (hfsmp
->hfs_jnlfileid
== VTOC(vp
)->c_fileid
) {
1069 * File size change request.
1070 * We are guaranteed that this is not a directory, and that
1071 * the filesystem object is writeable.
1073 * NOTE: HFS COMPRESSION depends on the data_size being set *before* the bsd flags are updated
1075 VATTR_SET_SUPPORTED(vap
, va_data_size
);
1076 if (VATTR_IS_ACTIVE(vap
, va_data_size
) && !vnode_islnk(vp
)) {
1078 /* keep the compressed state locked until we're done truncating the file */
1079 decmpfs_cnode
*dp
= VTOCMP(vp
);
1082 * call hfs_lazy_init_decmpfs_cnode() to make sure that the decmpfs_cnode
1083 * is filled in; we need a decmpfs_cnode to lock out decmpfs state changes
1084 * on this file while it's truncating
1086 dp
= hfs_lazy_init_decmpfs_cnode(VTOC(vp
));
1088 /* failed to allocate a decmpfs_cnode */
1089 return ENOMEM
; /* what should this be? */
1093 decmpfs_lock_compressed_data(dp
, 1);
1094 if (hfs_file_is_compressed(VTOC(vp
), 1)) {
1095 error
= decmpfs_decompress_file(vp
, dp
, -1/*vap->va_data_size*/, 0, 1);
1097 decmpfs_unlock_compressed_data(dp
, 1);
1103 /* Take truncate lock before taking cnode lock. */
1104 hfs_lock_truncate(VTOC(vp
), TRUE
);
1106 /* Perform the ubc_setsize before taking the cnode lock. */
1107 ubc_setsize(vp
, vap
->va_data_size
);
1109 if ((error
= hfs_lock(VTOC(vp
), HFS_EXCLUSIVE_LOCK
))) {
1110 hfs_unlock_truncate(VTOC(vp
), TRUE
);
1112 decmpfs_unlock_compressed_data(dp
, 1);
1118 error
= hfs_truncate(vp
, vap
->va_data_size
, vap
->va_vaflags
& 0xffff, 1, 0, ap
->a_context
);
1120 hfs_unlock_truncate(cp
, TRUE
);
1122 decmpfs_unlock_compressed_data(dp
, 1);
1128 if ((error
= hfs_lock(VTOC(vp
), HFS_EXCLUSIVE_LOCK
)))
1134 * If it is just an access time update request by itself
1135 * we know the request is from kernel level code, and we
1136 * can delay it without being as worried about consistency.
1137 * This change speeds up mmaps, in the rare case that they
1138 * get caught behind a sync.
1141 if (vap
->va_active
== VNODE_ATTR_va_access_time
) {
1142 cp
->c_touch_acctime
=TRUE
;
1149 * Owner/group change request.
1150 * We are guaranteed that the new owner/group is valid and legal.
1152 VATTR_SET_SUPPORTED(vap
, va_uid
);
1153 VATTR_SET_SUPPORTED(vap
, va_gid
);
1154 nuid
= VATTR_IS_ACTIVE(vap
, va_uid
) ? vap
->va_uid
: (uid_t
)VNOVAL
;
1155 ngid
= VATTR_IS_ACTIVE(vap
, va_gid
) ? vap
->va_gid
: (gid_t
)VNOVAL
;
1156 if (((nuid
!= (uid_t
)VNOVAL
) || (ngid
!= (gid_t
)VNOVAL
)) &&
1157 ((error
= hfs_chown(vp
, nuid
, ngid
, cred
, p
)) != 0))
1161 * Mode change request.
1162 * We are guaranteed that the mode value is valid and that in
1163 * conjunction with the owner and group, this change is legal.
1165 VATTR_SET_SUPPORTED(vap
, va_mode
);
1166 if (VATTR_IS_ACTIVE(vap
, va_mode
) &&
1167 ((error
= hfs_chmod(vp
, (int)vap
->va_mode
, cred
, p
)) != 0))
1171 * File flags change.
1172 * We are guaranteed that only flags allowed to change given the
1173 * current securelevel are being changed.
1175 VATTR_SET_SUPPORTED(vap
, va_flags
);
1176 if (VATTR_IS_ACTIVE(vap
, va_flags
)) {
1180 if ((cp
->c_flags
^ vap
->va_flags
) & UF_COMPRESSED
) {
1182 * the UF_COMPRESSED was toggled, so reset our cached compressed state
1183 * but we don't want to actually do the update until we've released the cnode lock down below
1184 * NOTE: turning the flag off doesn't actually decompress the file, so that we can
1185 * turn off the flag and look at the "raw" file for debugging purposes
1187 decmpfs_reset_state
= 1;
1191 cp
->c_flags
= vap
->va_flags
;
1192 cp
->c_touch_chgtime
= TRUE
;
1195 * Mirror the UF_HIDDEN flag to the invisible bit of the Finder Info.
1197 * The fdFlags for files and frFlags for folders are both 8 bytes
1198 * into the userInfo (the first 16 bytes of the Finder Info). They
1199 * are both 16-bit fields.
1201 fdFlags
= (u_int16_t
*) &cp
->c_finderinfo
[8];
1202 if (vap
->va_flags
& UF_HIDDEN
)
1203 *fdFlags
|= OSSwapHostToBigConstInt16(kFinderInvisibleMask
);
1205 *fdFlags
&= ~OSSwapHostToBigConstInt16(kFinderInvisibleMask
);
1209 * Timestamp updates.
1211 VATTR_SET_SUPPORTED(vap
, va_create_time
);
1212 VATTR_SET_SUPPORTED(vap
, va_access_time
);
1213 VATTR_SET_SUPPORTED(vap
, va_modify_time
);
1214 VATTR_SET_SUPPORTED(vap
, va_backup_time
);
1215 VATTR_SET_SUPPORTED(vap
, va_change_time
);
1216 if (VATTR_IS_ACTIVE(vap
, va_create_time
) ||
1217 VATTR_IS_ACTIVE(vap
, va_access_time
) ||
1218 VATTR_IS_ACTIVE(vap
, va_modify_time
) ||
1219 VATTR_IS_ACTIVE(vap
, va_backup_time
)) {
1220 if (VATTR_IS_ACTIVE(vap
, va_create_time
))
1221 cp
->c_itime
= vap
->va_create_time
.tv_sec
;
1222 if (VATTR_IS_ACTIVE(vap
, va_access_time
)) {
1223 cp
->c_atime
= vap
->va_access_time
.tv_sec
;
1224 cp
->c_touch_acctime
= FALSE
;
1226 if (VATTR_IS_ACTIVE(vap
, va_modify_time
)) {
1227 cp
->c_mtime
= vap
->va_modify_time
.tv_sec
;
1228 cp
->c_touch_modtime
= FALSE
;
1229 cp
->c_touch_chgtime
= TRUE
;
1232 * The utimes system call can reset the modification
1233 * time but it doesn't know about HFS create times.
1234 * So we need to ensure that the creation time is
1235 * always at least as old as the modification time.
1237 if ((VTOVCB(vp
)->vcbSigWord
== kHFSPlusSigWord
) &&
1238 (cp
->c_cnid
!= kHFSRootFolderID
) &&
1239 (cp
->c_mtime
< cp
->c_itime
)) {
1240 cp
->c_itime
= cp
->c_mtime
;
1243 if (VATTR_IS_ACTIVE(vap
, va_backup_time
))
1244 cp
->c_btime
= vap
->va_backup_time
.tv_sec
;
1245 cp
->c_flag
|= C_MODIFIED
;
1249 * Set name encoding.
1251 VATTR_SET_SUPPORTED(vap
, va_encoding
);
1252 if (VATTR_IS_ACTIVE(vap
, va_encoding
)) {
1253 cp
->c_encoding
= vap
->va_encoding
;
1254 hfs_setencodingbits(hfsmp
, cp
->c_encoding
);
1257 if ((error
= hfs_update(vp
, TRUE
)) != 0)
1261 /* Purge origin cache for cnode, since caller now has correct link ID for it
1262 * We purge it here since it was acquired for us during lookup, and we no longer need it.
1264 if ((cp
->c_flag
& C_HARDLINK
) && (vp
->v_type
!= VDIR
)){
1265 hfs_relorigin(cp
, 0);
1270 if (decmpfs_reset_state
) {
1272 * we've changed the UF_COMPRESSED flag, so reset the decmpfs state for this cnode
1273 * but don't do it while holding the hfs cnode lock
1275 decmpfs_cnode
*dp
= VTOCMP(vp
);
1278 * call hfs_lazy_init_decmpfs_cnode() to make sure that the decmpfs_cnode
1279 * is filled in; we need a decmpfs_cnode to prevent decmpfs state changes
1280 * on this file if it's locked
1282 dp
= hfs_lazy_init_decmpfs_cnode(VTOC(vp
));
1284 /* failed to allocate a decmpfs_cnode */
1285 return ENOMEM
; /* what should this be? */
1288 decmpfs_cnode_set_vnode_state(dp
, FILE_TYPE_UNKNOWN
, 0);
1297 * Change the mode on a file.
1298 * cnode must be locked before calling.
1302 hfs_chmod(struct vnode
*vp
, int mode
, __unused kauth_cred_t cred
, __unused
struct proc
*p
)
1304 register struct cnode
*cp
= VTOC(vp
);
1306 if (VTOVCB(vp
)->vcbSigWord
!= kHFSPlusSigWord
)
1309 // XXXdbg - don't allow modification of the journal or journal_info_block
1310 if (VTOHFS(vp
)->jnl
&& cp
&& cp
->c_datafork
) {
1311 struct HFSPlusExtentDescriptor
*extd
;
1313 extd
= &cp
->c_datafork
->ff_extents
[0];
1314 if (extd
->startBlock
== VTOVCB(vp
)->vcbJinfoBlock
|| extd
->startBlock
== VTOHFS(vp
)->jnl_start
) {
1319 #if OVERRIDE_UNKNOWN_PERMISSIONS
1320 if (((unsigned int)vfs_flags(VTOVFS(vp
))) & MNT_UNKNOWNPERMISSIONS
) {
1324 cp
->c_mode
&= ~ALLPERMS
;
1325 cp
->c_mode
|= (mode
& ALLPERMS
);
1326 cp
->c_touch_chgtime
= TRUE
;
1333 hfs_write_access(struct vnode
*vp
, kauth_cred_t cred
, struct proc
*p
, Boolean considerFlags
)
1335 struct cnode
*cp
= VTOC(vp
);
1340 * Disallow write attempts on read-only file systems;
1341 * unless the file is a socket, fifo, or a block or
1342 * character device resident on the file system.
1344 switch (vnode_vtype(vp
)) {
1348 if (VTOHFS(vp
)->hfs_flags
& HFS_READ_ONLY
)
1355 /* If immutable bit set, nobody gets to write it. */
1356 if (considerFlags
&& (cp
->c_flags
& IMMUTABLE
))
1359 /* Otherwise, user id 0 always gets access. */
1360 if (!suser(cred
, NULL
))
1363 /* Otherwise, check the owner. */
1364 if ((retval
= hfs_owner_rights(VTOHFS(vp
), cp
->c_uid
, cred
, p
, false)) == 0)
1365 return ((cp
->c_mode
& S_IWUSR
) == S_IWUSR
? 0 : EACCES
);
1367 /* Otherwise, check the groups. */
1368 if (kauth_cred_ismember_gid(cred
, cp
->c_gid
, &is_member
) == 0 && is_member
) {
1369 return ((cp
->c_mode
& S_IWGRP
) == S_IWGRP
? 0 : EACCES
);
1372 /* Otherwise, check everyone else. */
1373 return ((cp
->c_mode
& S_IWOTH
) == S_IWOTH
? 0 : EACCES
);
1378 * Perform chown operation on cnode cp;
1379 * code must be locked prior to call.
1384 hfs_chown(struct vnode
*vp
, uid_t uid
, gid_t gid
, __unused kauth_cred_t cred
,
1385 __unused
struct proc
*p
)
1387 hfs_chown(struct vnode
*vp
, uid_t uid
, gid_t gid
, kauth_cred_t cred
,
1388 __unused
struct proc
*p
)
1391 register struct cnode
*cp
= VTOC(vp
);
1400 if (VTOVCB(vp
)->vcbSigWord
!= kHFSPlusSigWord
)
1403 if (((unsigned int)vfs_flags(VTOVFS(vp
))) & MNT_UNKNOWNPERMISSIONS
)
1406 if (uid
== (uid_t
)VNOVAL
)
1408 if (gid
== (gid_t
)VNOVAL
)
1411 #if 0 /* we are guaranteed that this is already the case */
1413 * If we don't own the file, are trying to change the owner
1414 * of the file, or are not a member of the target group,
1415 * the caller must be superuser or the call fails.
1417 if ((kauth_cred_getuid(cred
) != cp
->c_uid
|| uid
!= cp
->c_uid
||
1418 (gid
!= cp
->c_gid
&&
1419 (kauth_cred_ismember_gid(cred
, gid
, &is_member
) || !is_member
))) &&
1420 (error
= suser(cred
, 0)))
1427 if ((error
= hfs_getinoquota(cp
)))
1430 dqrele(cp
->c_dquot
[USRQUOTA
]);
1431 cp
->c_dquot
[USRQUOTA
] = NODQUOT
;
1434 dqrele(cp
->c_dquot
[GRPQUOTA
]);
1435 cp
->c_dquot
[GRPQUOTA
] = NODQUOT
;
1439 * Eventually need to account for (fake) a block per directory
1440 * if (vnode_isdir(vp))
1441 * change = VTOHFS(vp)->blockSize;
1445 change
= (int64_t)(cp
->c_blocks
) * (int64_t)VTOVCB(vp
)->blockSize
;
1446 (void) hfs_chkdq(cp
, -change
, cred
, CHOWN
);
1447 (void) hfs_chkiq(cp
, -1, cred
, CHOWN
);
1448 for (i
= 0; i
< MAXQUOTAS
; i
++) {
1449 dqrele(cp
->c_dquot
[i
]);
1450 cp
->c_dquot
[i
] = NODQUOT
;
1456 if ((error
= hfs_getinoquota(cp
)) == 0) {
1458 dqrele(cp
->c_dquot
[USRQUOTA
]);
1459 cp
->c_dquot
[USRQUOTA
] = NODQUOT
;
1462 dqrele(cp
->c_dquot
[GRPQUOTA
]);
1463 cp
->c_dquot
[GRPQUOTA
] = NODQUOT
;
1465 if ((error
= hfs_chkdq(cp
, change
, cred
, CHOWN
)) == 0) {
1466 if ((error
= hfs_chkiq(cp
, 1, cred
, CHOWN
)) == 0)
1469 (void) hfs_chkdq(cp
, -change
, cred
, CHOWN
|FORCE
);
1471 for (i
= 0; i
< MAXQUOTAS
; i
++) {
1472 dqrele(cp
->c_dquot
[i
]);
1473 cp
->c_dquot
[i
] = NODQUOT
;
1478 if (hfs_getinoquota(cp
) == 0) {
1480 dqrele(cp
->c_dquot
[USRQUOTA
]);
1481 cp
->c_dquot
[USRQUOTA
] = NODQUOT
;
1484 dqrele(cp
->c_dquot
[GRPQUOTA
]);
1485 cp
->c_dquot
[GRPQUOTA
] = NODQUOT
;
1487 (void) hfs_chkdq(cp
, change
, cred
, FORCE
|CHOWN
);
1488 (void) hfs_chkiq(cp
, 1, cred
, FORCE
|CHOWN
);
1489 (void) hfs_getinoquota(cp
);
1493 if (hfs_getinoquota(cp
))
1494 panic("hfs_chown: lost quota");
1499 According to the SUSv3 Standard, chown() shall mark
1500 for update the st_ctime field of the file.
1501 (No exceptions mentioned)
1503 cp
->c_touch_chgtime
= TRUE
;
1509 * The hfs_exchange routine swaps the fork data in two files by
1510 * exchanging some of the information in the cnode. It is used
1511 * to preserve the file ID when updating an existing file, in
1512 * case the file is being tracked through its file ID. Typically
1513 * its used after creating a new file during a safe-save.
1516 hfs_vnop_exchange(ap
)
1517 struct vnop_exchange_args
/* {
1518 struct vnode *a_fvp;
1519 struct vnode *a_tvp;
1521 vfs_context_t a_context;
1524 struct vnode
*from_vp
= ap
->a_fvp
;
1525 struct vnode
*to_vp
= ap
->a_tvp
;
1526 struct cnode
*from_cp
;
1527 struct cnode
*to_cp
;
1528 struct hfsmount
*hfsmp
;
1529 struct cat_desc tempdesc
;
1530 struct cat_attr tempattr
;
1531 const unsigned char *from_nameptr
;
1532 const unsigned char *to_nameptr
;
1533 char from_iname
[32];
1539 int error
= 0, started_tr
= 0, got_cookie
= 0;
1540 cat_cookie_t cookie
;
1542 /* The files must be on the same volume. */
1543 if (vnode_mount(from_vp
) != vnode_mount(to_vp
))
1546 if (from_vp
== to_vp
)
1550 if ( hfs_file_is_compressed(VTOC(from_vp
), 0) ) {
1551 if ( 0 != ( error
= decmpfs_decompress_file(from_vp
, VTOCMP(from_vp
), -1, 0, 1) ) ) {
1556 if ( hfs_file_is_compressed(VTOC(to_vp
), 0) ) {
1557 if ( 0 != ( error
= decmpfs_decompress_file(to_vp
, VTOCMP(to_vp
), -1, 0, 1) ) ) {
1561 #endif // HFS_COMPRESSION
1563 if ((error
= hfs_lockpair(VTOC(from_vp
), VTOC(to_vp
), HFS_EXCLUSIVE_LOCK
)))
1566 from_cp
= VTOC(from_vp
);
1567 to_cp
= VTOC(to_vp
);
1568 hfsmp
= VTOHFS(from_vp
);
1570 /* Only normal files can be exchanged. */
1571 if (!vnode_isreg(from_vp
) || !vnode_isreg(to_vp
) ||
1572 VNODE_IS_RSRC(from_vp
) || VNODE_IS_RSRC(to_vp
)) {
1577 // XXXdbg - don't allow modification of the journal or journal_info_block
1579 struct HFSPlusExtentDescriptor
*extd
;
1581 if (from_cp
->c_datafork
) {
1582 extd
= &from_cp
->c_datafork
->ff_extents
[0];
1583 if (extd
->startBlock
== VTOVCB(from_vp
)->vcbJinfoBlock
|| extd
->startBlock
== hfsmp
->jnl_start
) {
1589 if (to_cp
->c_datafork
) {
1590 extd
= &to_cp
->c_datafork
->ff_extents
[0];
1591 if (extd
->startBlock
== VTOVCB(to_vp
)->vcbJinfoBlock
|| extd
->startBlock
== hfsmp
->jnl_start
) {
1598 if ((error
= hfs_start_transaction(hfsmp
)) != 0) {
1604 * Reserve some space in the Catalog file.
1606 if ((error
= cat_preflight(hfsmp
, CAT_EXCHANGE
, &cookie
, vfs_context_proc(ap
->a_context
)))) {
1611 /* The backend code always tries to delete the virtual
1612 * extent id for exchanging files so we need to lock
1613 * the extents b-tree.
1615 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
| SFL_EXTENTS
| SFL_ATTRIBUTE
, HFS_EXCLUSIVE_LOCK
);
1617 /* Account for the location of the catalog objects. */
1618 if (from_cp
->c_flag
& C_HARDLINK
) {
1619 MAKE_INODE_NAME(from_iname
, sizeof(from_iname
),
1620 from_cp
->c_attr
.ca_linkref
);
1621 from_nameptr
= (unsigned char *)from_iname
;
1622 from_parid
= hfsmp
->hfs_private_desc
[FILE_HARDLINKS
].cd_cnid
;
1623 from_cp
->c_hint
= 0;
1625 from_nameptr
= from_cp
->c_desc
.cd_nameptr
;
1626 from_parid
= from_cp
->c_parentcnid
;
1628 if (to_cp
->c_flag
& C_HARDLINK
) {
1629 MAKE_INODE_NAME(to_iname
, sizeof(to_iname
),
1630 to_cp
->c_attr
.ca_linkref
);
1631 to_nameptr
= (unsigned char *)to_iname
;
1632 to_parid
= hfsmp
->hfs_private_desc
[FILE_HARDLINKS
].cd_cnid
;
1635 to_nameptr
= to_cp
->c_desc
.cd_nameptr
;
1636 to_parid
= to_cp
->c_parentcnid
;
1639 /* Do the exchange */
1640 error
= ExchangeFileIDs(hfsmp
, from_nameptr
, to_nameptr
, from_parid
,
1641 to_parid
, from_cp
->c_hint
, to_cp
->c_hint
);
1642 hfs_systemfile_unlock(hfsmp
, lockflags
);
1645 * Note that we don't need to exchange any extended attributes
1646 * since the attributes are keyed by file ID.
1649 if (error
!= E_NONE
) {
1650 error
= MacToVFSError(error
);
1654 /* Purge the vnodes from the name cache */
1656 cache_purge(from_vp
);
1660 /* Save a copy of from attributes before swapping. */
1661 bcopy(&from_cp
->c_desc
, &tempdesc
, sizeof(struct cat_desc
));
1662 bcopy(&from_cp
->c_attr
, &tempattr
, sizeof(struct cat_attr
));
1663 tempflag
= from_cp
->c_flag
& (C_HARDLINK
| C_HASXATTRS
);
1666 * Swap the descriptors and all non-fork related attributes.
1667 * (except the modify date)
1669 bcopy(&to_cp
->c_desc
, &from_cp
->c_desc
, sizeof(struct cat_desc
));
1671 from_cp
->c_hint
= 0;
1672 from_cp
->c_fileid
= from_cp
->c_cnid
;
1673 from_cp
->c_itime
= to_cp
->c_itime
;
1674 from_cp
->c_btime
= to_cp
->c_btime
;
1675 from_cp
->c_atime
= to_cp
->c_atime
;
1676 from_cp
->c_ctime
= to_cp
->c_ctime
;
1677 from_cp
->c_gid
= to_cp
->c_gid
;
1678 from_cp
->c_uid
= to_cp
->c_uid
;
1679 from_cp
->c_flags
= to_cp
->c_flags
;
1680 from_cp
->c_mode
= to_cp
->c_mode
;
1681 from_cp
->c_linkcount
= to_cp
->c_linkcount
;
1682 from_cp
->c_flag
= to_cp
->c_flag
& (C_HARDLINK
| C_HASXATTRS
);
1683 from_cp
->c_attr
.ca_recflags
= to_cp
->c_attr
.ca_recflags
;
1684 bcopy(to_cp
->c_finderinfo
, from_cp
->c_finderinfo
, 32);
1686 bcopy(&tempdesc
, &to_cp
->c_desc
, sizeof(struct cat_desc
));
1688 to_cp
->c_fileid
= to_cp
->c_cnid
;
1689 to_cp
->c_itime
= tempattr
.ca_itime
;
1690 to_cp
->c_btime
= tempattr
.ca_btime
;
1691 to_cp
->c_atime
= tempattr
.ca_atime
;
1692 to_cp
->c_ctime
= tempattr
.ca_ctime
;
1693 to_cp
->c_gid
= tempattr
.ca_gid
;
1694 to_cp
->c_uid
= tempattr
.ca_uid
;
1695 to_cp
->c_flags
= tempattr
.ca_flags
;
1696 to_cp
->c_mode
= tempattr
.ca_mode
;
1697 to_cp
->c_linkcount
= tempattr
.ca_linkcount
;
1698 to_cp
->c_flag
= tempflag
;
1699 to_cp
->c_attr
.ca_recflags
= tempattr
.ca_recflags
;
1700 bcopy(tempattr
.ca_finderinfo
, to_cp
->c_finderinfo
, 32);
1702 /* Rehash the cnodes using their new file IDs */
1703 hfs_chash_rehash(hfsmp
, from_cp
, to_cp
);
1706 * When a file moves out of "Cleanup At Startup"
1707 * we can drop its NODUMP status.
1709 if ((from_cp
->c_flags
& UF_NODUMP
) &&
1710 (from_cp
->c_parentcnid
!= to_cp
->c_parentcnid
)) {
1711 from_cp
->c_flags
&= ~UF_NODUMP
;
1712 from_cp
->c_touch_chgtime
= TRUE
;
1714 if ((to_cp
->c_flags
& UF_NODUMP
) &&
1715 (to_cp
->c_parentcnid
!= from_cp
->c_parentcnid
)) {
1716 to_cp
->c_flags
&= ~UF_NODUMP
;
1717 to_cp
->c_touch_chgtime
= TRUE
;
1722 cat_postflight(hfsmp
, &cookie
, vfs_context_proc(ap
->a_context
));
1725 hfs_end_transaction(hfsmp
);
1728 hfs_unlockpair(from_cp
, to_cp
);
1734 * cnode must be locked
1738 hfs_fsync(struct vnode
*vp
, int waitfor
, int fullsync
, struct proc
*p
)
1740 struct cnode
*cp
= VTOC(vp
);
1741 struct filefork
*fp
= NULL
;
1743 struct hfsmount
*hfsmp
= VTOHFS(vp
);
1744 struct rl_entry
*invalid_range
;
1746 int waitdata
; /* attributes necessary for data retrieval */
1747 int wait
; /* all other attributes (e.g. atime, etc.) */
1749 int took_trunc_lock
= 0;
1750 boolean_t trunc_lock_exclusive
= FALSE
;
1753 * Applications which only care about data integrity rather than full
1754 * file integrity may opt out of (delay) expensive metadata update
1755 * operations as a performance optimization.
1757 wait
= (waitfor
== MNT_WAIT
);
1758 waitdata
= (waitfor
== MNT_DWAIT
) | wait
;
1759 if (always_do_fullfsync
)
1762 /* HFS directories don't have any data blocks. */
1763 if (vnode_isdir(vp
))
1768 * For system files flush the B-tree header and
1769 * for regular files write out any clusters
1771 if (vnode_issystem(vp
)) {
1772 if (VTOF(vp
)->fcbBTCBPtr
!= NULL
) {
1774 if (hfsmp
->jnl
== NULL
) {
1775 BTFlushPath(VTOF(vp
));
1778 } else if (UBCINFOEXISTS(vp
)) {
1780 hfs_lock_truncate(cp
, trunc_lock_exclusive
);
1781 took_trunc_lock
= 1;
1783 if (fp
->ff_unallocblocks
!= 0) {
1784 hfs_unlock_truncate(cp
, trunc_lock_exclusive
);
1786 trunc_lock_exclusive
= TRUE
;
1787 hfs_lock_truncate(cp
, trunc_lock_exclusive
);
1789 /* Don't hold cnode lock when calling into cluster layer. */
1790 (void) cluster_push(vp
, waitdata
? IO_SYNC
: 0);
1792 hfs_lock(cp
, HFS_FORCE_LOCK
);
1795 * When MNT_WAIT is requested and the zero fill timeout
1796 * has expired then we must explicitly zero out any areas
1797 * that are currently marked invalid (holes).
1799 * Files with NODUMP can bypass zero filling here.
1801 if (fp
&& (((cp
->c_flag
& C_ALWAYS_ZEROFILL
) && !TAILQ_EMPTY(&fp
->ff_invalidranges
)) ||
1802 ((wait
|| (cp
->c_flag
& C_ZFWANTSYNC
)) &&
1803 ((cp
->c_flags
& UF_NODUMP
) == 0) &&
1804 UBCINFOEXISTS(vp
) && (vnode_issystem(vp
) ==0) &&
1805 cp
->c_zftimeout
!= 0))) {
1808 if ((cp
->c_flag
& C_ALWAYS_ZEROFILL
) == 0 && !fullsync
&& tv
.tv_sec
< (long)cp
->c_zftimeout
) {
1809 /* Remember that a force sync was requested. */
1810 cp
->c_flag
|= C_ZFWANTSYNC
;
1813 if (!TAILQ_EMPTY(&fp
->ff_invalidranges
)) {
1814 if (!took_trunc_lock
|| trunc_lock_exclusive
== FALSE
) {
1816 if (took_trunc_lock
)
1817 hfs_unlock_truncate(cp
, trunc_lock_exclusive
);
1819 trunc_lock_exclusive
= TRUE
;
1820 hfs_lock_truncate(cp
, trunc_lock_exclusive
);
1821 hfs_lock(cp
, HFS_FORCE_LOCK
);
1822 took_trunc_lock
= 1;
1824 while ((invalid_range
= TAILQ_FIRST(&fp
->ff_invalidranges
))) {
1825 off_t start
= invalid_range
->rl_start
;
1826 off_t end
= invalid_range
->rl_end
;
1828 /* The range about to be written must be validated
1829 * first, so that VNOP_BLOCKMAP() will return the
1830 * appropriate mapping for the cluster code:
1832 rl_remove(start
, end
, &fp
->ff_invalidranges
);
1834 /* Don't hold cnode lock when calling into cluster layer. */
1836 (void) cluster_write(vp
, (struct uio
*) 0,
1837 fp
->ff_size
, end
+ 1, start
, (off_t
)0,
1838 IO_HEADZEROFILL
| IO_NOZERODIRTY
| IO_NOCACHE
);
1839 hfs_lock(cp
, HFS_FORCE_LOCK
);
1840 cp
->c_flag
|= C_MODIFIED
;
1843 (void) cluster_push(vp
, waitdata
? IO_SYNC
: 0);
1844 hfs_lock(cp
, HFS_FORCE_LOCK
);
1846 cp
->c_flag
&= ~C_ZFWANTSYNC
;
1847 cp
->c_zftimeout
= 0;
1850 if (took_trunc_lock
) {
1851 hfs_unlock_truncate(cp
, trunc_lock_exclusive
);
1852 took_trunc_lock
= 0;
1855 * if we have a journal and if journal_active() returns != 0 then the
1856 * we shouldn't do anything to a locked block (because it is part
1857 * of a transaction). otherwise we'll just go through the normal
1858 * code path and flush the buffer. note journal_active() can return
1859 * -1 if the journal is invalid -- however we still need to skip any
1860 * locked blocks as they get cleaned up when we finish the transaction
1861 * or close the journal.
1863 // if (hfsmp->jnl && journal_active(hfsmp->jnl) >= 0)
1865 lockflag
= BUF_SKIP_LOCKED
;
1870 * Flush all dirty buffers associated with a vnode.
1872 buf_flushdirtyblks(vp
, waitdata
, lockflag
, "hfs_fsync");
1875 if (vnode_isreg(vp
) && vnode_issystem(vp
)) {
1876 if (VTOF(vp
)->fcbBTCBPtr
!= NULL
) {
1878 BTSetLastSync(VTOF(vp
), tv
.tv_sec
);
1880 cp
->c_touch_acctime
= FALSE
;
1881 cp
->c_touch_chgtime
= FALSE
;
1882 cp
->c_touch_modtime
= FALSE
;
1883 } else if ( !(vp
->v_flag
& VSWAP
) ) /* User file */ {
1884 retval
= hfs_update(vp
, wait
);
1887 * When MNT_WAIT is requested push out the catalog record for
1888 * this file. If they asked for a full fsync, we can skip this
1889 * because the journal_flush or hfs_metasync_all will push out
1890 * all of the metadata changes.
1892 if ((retval
== 0) && wait
&& !fullsync
&& cp
->c_hint
&&
1893 !ISSET(cp
->c_flag
, C_DELETED
| C_NOEXISTS
)) {
1894 hfs_metasync(VTOHFS(vp
), (daddr64_t
)cp
->c_hint
, p
);
1898 * If this was a full fsync, make sure all metadata
1899 * changes get to stable storage.
1903 hfs_journal_flush(hfsmp
);
1905 retval
= hfs_metasync_all(hfsmp
);
1906 /* XXX need to pass context! */
1907 VNOP_IOCTL(hfsmp
->hfs_devvp
, DKIOCSYNCHRONIZECACHE
, NULL
, FWRITE
, NULL
);
1916 /* Sync an hfs catalog b-tree node */
1918 hfs_metasync(struct hfsmount
*hfsmp
, daddr64_t node
, __unused
struct proc
*p
)
1924 vp
= HFSTOVCB(hfsmp
)->catalogRefNum
;
1926 // XXXdbg - don't need to do this on a journaled volume
1931 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_EXCLUSIVE_LOCK
);
1933 * Look for a matching node that has been delayed
1934 * but is not part of a set (B_LOCKED).
1936 * BLK_ONLYVALID causes buf_getblk to return a
1937 * buf_t for the daddr64_t specified only if it's
1938 * currently resident in the cache... the size
1939 * parameter to buf_getblk is ignored when this flag
1942 bp
= buf_getblk(vp
, node
, 0, 0, 0, BLK_META
| BLK_ONLYVALID
);
1945 if ((buf_flags(bp
) & (B_LOCKED
| B_DELWRI
)) == B_DELWRI
)
1946 (void) VNOP_BWRITE(bp
);
1951 hfs_systemfile_unlock(hfsmp
, lockflags
);
1958 * Sync all hfs B-trees. Use this instead of journal_flush for a volume
1959 * without a journal. Note that the volume bitmap does not get written;
1960 * we rely on fsck_hfs to fix that up (which it can do without any loss
1964 hfs_metasync_all(struct hfsmount
*hfsmp
)
1968 /* Lock all of the B-trees so we get a mutually consistent state */
1969 lockflags
= hfs_systemfile_lock(hfsmp
,
1970 SFL_CATALOG
|SFL_EXTENTS
|SFL_ATTRIBUTE
, HFS_EXCLUSIVE_LOCK
);
1972 /* Sync each of the B-trees */
1973 if (hfsmp
->hfs_catalog_vp
)
1974 hfs_btsync(hfsmp
->hfs_catalog_vp
, 0);
1975 if (hfsmp
->hfs_extents_vp
)
1976 hfs_btsync(hfsmp
->hfs_extents_vp
, 0);
1977 if (hfsmp
->hfs_attribute_vp
)
1978 hfs_btsync(hfsmp
->hfs_attribute_vp
, 0);
1980 /* Wait for all of the writes to complete */
1981 if (hfsmp
->hfs_catalog_vp
)
1982 vnode_waitforwrites(hfsmp
->hfs_catalog_vp
, 0, 0, 0, "hfs_metasync_all");
1983 if (hfsmp
->hfs_extents_vp
)
1984 vnode_waitforwrites(hfsmp
->hfs_extents_vp
, 0, 0, 0, "hfs_metasync_all");
1985 if (hfsmp
->hfs_attribute_vp
)
1986 vnode_waitforwrites(hfsmp
->hfs_attribute_vp
, 0, 0, 0, "hfs_metasync_all");
1988 hfs_systemfile_unlock(hfsmp
, lockflags
);
1996 hfs_btsync_callback(struct buf
*bp
, __unused
void *dummy
)
1998 buf_clearflags(bp
, B_LOCKED
);
1999 (void) buf_bawrite(bp
);
2001 return(BUF_CLAIMED
);
2007 hfs_btsync(struct vnode
*vp
, int sync_transaction
)
2009 struct cnode
*cp
= VTOC(vp
);
2013 if (sync_transaction
)
2014 flags
|= BUF_SKIP_NONLOCKED
;
2016 * Flush all dirty buffers associated with b-tree.
2018 buf_iterate(vp
, hfs_btsync_callback
, flags
, 0);
2021 if (vnode_issystem(vp
) && (VTOF(vp
)->fcbBTCBPtr
!= NULL
))
2022 (void) BTSetLastSync(VTOF(vp
), tv
.tv_sec
);
2023 cp
->c_touch_acctime
= FALSE
;
2024 cp
->c_touch_chgtime
= FALSE
;
2025 cp
->c_touch_modtime
= FALSE
;
2031 * Remove a directory.
2035 struct vnop_rmdir_args
/* {
2036 struct vnode *a_dvp;
2038 struct componentname *a_cnp;
2039 vfs_context_t a_context;
2042 struct vnode
*dvp
= ap
->a_dvp
;
2043 struct vnode
*vp
= ap
->a_vp
;
2044 struct cnode
*dcp
= VTOC(dvp
);
2045 struct cnode
*cp
= VTOC(vp
);
2048 if (!S_ISDIR(cp
->c_mode
)) {
2054 if ((error
= hfs_lockpair(dcp
, cp
, HFS_EXCLUSIVE_LOCK
))) {
2058 /* Check for a race with rmdir on the parent directory */
2059 if (dcp
->c_flag
& (C_DELETED
| C_NOEXISTS
)) {
2060 hfs_unlockpair (dcp
, cp
);
2063 error
= hfs_removedir(dvp
, vp
, ap
->a_cnp
, 0);
2065 hfs_unlockpair(dcp
, cp
);
2071 * Remove a directory
2073 * Both dvp and vp cnodes are locked
2076 hfs_removedir(struct vnode
*dvp
, struct vnode
*vp
, struct componentname
*cnp
,
2081 struct hfsmount
* hfsmp
;
2082 struct cat_desc desc
;
2084 int error
= 0, started_tr
= 0;
2091 return (EINVAL
); /* cannot remove "." */
2093 if (cp
->c_flag
& (C_NOEXISTS
| C_DELETED
)) {
2096 if (cp
->c_entries
!= 0) {
2100 /* Check if we're removing the last link to an empty directory. */
2101 if (cp
->c_flag
& C_HARDLINK
) {
2102 /* We could also return EBUSY here */
2103 return hfs_unlink(hfsmp
, dvp
, vp
, cnp
, skip_reserve
);
2107 * We want to make sure that if the directory has a lot of attributes, we process them
2108 * in separate transactions to ensure we don't panic in the journal with a gigantic
2109 * transaction. This means we'll let hfs_removefile deal with the directory, which generally
2110 * follows the same codepath as open-unlinked files. Note that the last argument to
2111 * hfs_removefile specifies that it is supposed to handle directories for this case.
2113 if ((hfsmp
->hfs_attribute_vp
!= NULL
) &&
2114 (cp
->c_attr
.ca_recflags
& kHFSHasAttributesMask
) != 0) {
2116 return hfs_removefile(dvp
, vp
, cnp
, 0, 0, 1, NULL
);
2119 dcp
->c_flag
|= C_DIR_MODIFICATION
;
2122 if (hfsmp
->hfs_flags
& HFS_QUOTAS
)
2123 (void)hfs_getinoquota(cp
);
2125 if ((error
= hfs_start_transaction(hfsmp
)) != 0) {
2131 * Verify the directory is empty (and valid).
2132 * (Rmdir ".." won't be valid since
2133 * ".." will contain a reference to
2134 * the current directory and thus be
2137 if ((dcp
->c_flags
& APPEND
) || (cp
->c_flags
& (IMMUTABLE
| APPEND
))) {
2142 /* Remove the entry from the namei cache: */
2146 * Protect against a race with rename by using the component
2147 * name passed in and parent id from dvp (instead of using
2148 * the cp->c_desc which may have changed).
2150 desc
.cd_nameptr
= (const u_int8_t
*)cnp
->cn_nameptr
;
2151 desc
.cd_namelen
= cnp
->cn_namelen
;
2152 desc
.cd_parentcnid
= dcp
->c_fileid
;
2153 desc
.cd_cnid
= cp
->c_cnid
;
2154 desc
.cd_flags
= CD_ISDIR
;
2155 desc
.cd_encoding
= cp
->c_encoding
;
2158 if (!hfs_valid_cnode(hfsmp
, dvp
, cnp
, cp
->c_fileid
)) {
2163 /* Remove entry from catalog */
2164 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
| SFL_ATTRIBUTE
| SFL_BITMAP
, HFS_EXCLUSIVE_LOCK
);
2166 if (!skip_reserve
) {
2168 * Reserve some space in the Catalog file.
2170 if ((error
= cat_preflight(hfsmp
, CAT_DELETE
, NULL
, 0))) {
2171 hfs_systemfile_unlock(hfsmp
, lockflags
);
2176 error
= cat_delete(hfsmp
, &desc
, &cp
->c_attr
);
2178 /* The parent lost a child */
2179 if (dcp
->c_entries
> 0)
2181 DEC_FOLDERCOUNT(hfsmp
, dcp
->c_attr
);
2182 dcp
->c_dirchangecnt
++;
2183 dcp
->c_touch_chgtime
= TRUE
;
2184 dcp
->c_touch_modtime
= TRUE
;
2185 hfs_touchtimes(hfsmp
, cp
);
2186 (void) cat_update(hfsmp
, &dcp
->c_desc
, &dcp
->c_attr
, NULL
, NULL
);
2187 cp
->c_flag
&= ~(C_MODIFIED
| C_FORCEUPDATE
);
2190 hfs_systemfile_unlock(hfsmp
, lockflags
);
2196 if (hfsmp
->hfs_flags
& HFS_QUOTAS
)
2197 (void)hfs_chkiq(cp
, -1, NOCRED
, 0);
2200 hfs_volupdate(hfsmp
, VOL_RMDIR
, (dcp
->c_cnid
== kHFSRootFolderID
));
2203 * directory open or in use (e.g. opendir() or current working
2204 * directory for some process); wait for inactive to actually
2205 * remove catalog entry
2207 if (vnode_isinuse(vp
, 0)) {
2208 cp
->c_flag
|= C_DELETED
;
2210 cp
->c_flag
|= C_NOEXISTS
;
2213 dcp
->c_flag
&= ~C_DIR_MODIFICATION
;
2214 wakeup((caddr_t
)&dcp
->c_flag
);
2217 hfs_end_transaction(hfsmp
);
2225 * Remove a file or link.
2229 struct vnop_remove_args
/* {
2230 struct vnode *a_dvp;
2232 struct componentname *a_cnp;
2234 vfs_context_t a_context;
2237 struct vnode
*dvp
= ap
->a_dvp
;
2238 struct vnode
*vp
= ap
->a_vp
;
2239 struct cnode
*dcp
= VTOC(dvp
);
2240 struct cnode
*cp
= VTOC(vp
);
2241 struct vnode
*rvp
= NULL
;
2242 struct hfsmount
*hfsmp
= VTOHFS(vp
);
2243 int error
=0, recycle_rsrc
=0;
2244 int drop_rsrc_vnode
= 0;
2252 * We need to grab the cnode lock on 'cp' before the lockpair()
2253 * to get an iocount on the rsrc fork BEFORE we enter hfs_removefile.
2254 * To prevent other deadlocks, it's best to call hfs_vgetrsrc in a way that
2255 * allows it to drop the cnode lock that it expects to be held coming in.
2256 * If we don't, we could commit a lock order violation, causing a deadlock.
2257 * In order to safely get the rsrc vnode with an iocount, we need to only hold the
2258 * lock on the file temporarily. Unlike hfs_vnop_rename, we don't have to worry
2259 * about one rsrc fork getting recycled for another, but we do want to ensure
2260 * that there are no deadlocks due to lock ordering issues.
2262 * Note: this function may be invoked for directory hardlinks, so just skip these
2263 * steps if 'vp' is a directory.
2267 if ((vp
->v_type
== VLNK
) || (vp
->v_type
== VREG
)) {
2269 if ((error
= hfs_lock (cp
, HFS_EXCLUSIVE_LOCK
))) {
2272 error
= hfs_vgetrsrc(hfsmp
, vp
, &rvp
, TRUE
, TRUE
);
2275 /* We may have gotten a rsrc vp out even though we got an error back. */
2282 drop_rsrc_vnode
= 1;
2284 /* Now that we may have an iocount on rvp, do the lock pair */
2285 hfs_lock_truncate(cp
, TRUE
);
2287 if ((error
= hfs_lockpair(dcp
, cp
, HFS_EXCLUSIVE_LOCK
))) {
2288 hfs_unlock_truncate(cp
, TRUE
);
2289 /* drop the iocount on rvp if necessary */
2290 if (drop_rsrc_vnode
) {
2297 * Check to see if we raced rmdir for the parent directory
2298 * hfs_removefile already checks for a race on vp/cp
2300 if (dcp
->c_flag
& (C_DELETED
| C_NOEXISTS
)) {
2305 error
= hfs_removefile(dvp
, vp
, ap
->a_cnp
, ap
->a_flags
, 0, 0, rvp
);
2308 // If the remove succeeded and it's an open-unlinked file that has
2309 // a resource fork vnode that's not in use, we will want to recycle
2310 // the rvp *after* we're done unlocking everything. Otherwise the
2311 // resource vnode will keep a v_parent reference on this vnode which
2312 // prevents it from going through inactive/reclaim which means that
2313 // the disk space associated with this file won't get free'd until
2314 // something forces the resource vnode to get recycled (and that can
2315 // take a very long time).
2317 if (error
== 0 && (cp
->c_flag
& C_DELETED
) &&
2318 (rvp
) && !vnode_isinuse(rvp
, 0)) {
2323 * Drop the truncate lock before unlocking the cnode
2324 * (which can potentially perform a vnode_put and
2325 * recycle the vnode which in turn might require the
2329 hfs_unlock_truncate(cp
, TRUE
);
2330 hfs_unlockpair(dcp
, cp
);
2333 vref
= vnode_ref(rvp
);
2335 /* vnode_ref could return an error, only release if we got a ref */
2341 if (drop_rsrc_vnode
) {
2342 /* drop iocount on rsrc fork, was obtained at beginning of fxn */
2351 hfs_removefile_callback(struct buf
*bp
, void *hfsmp
) {
2353 if ( !(buf_flags(bp
) & B_META
))
2354 panic("hfs: symlink bp @ %p is not marked meta-data!\n", bp
);
2356 * it's part of the current transaction, kill it.
2358 journal_kill_block(((struct hfsmount
*)hfsmp
)->jnl
, bp
);
2360 return (BUF_CLAIMED
);
2366 * Similar to hfs_vnop_remove except there are additional options.
2367 * This function may be used to remove directories if they have
2368 * lots of EA's -- note the 'allow_dirs' argument.
2370 * The 'rvp' argument is used to pass in a resource fork vnode with
2371 * an iocount to prevent it from getting recycled during usage. If it
2372 * is NULL, then it is assumed the caller is a VNOP that cannot operate
2373 * on resource forks, like hfs_vnop_symlink or hfs_removedir. Otherwise in
2374 * a VNOP that takes multiple vnodes, we could violate lock order and
2377 * Requires cnode and truncate locks to be held.
2380 hfs_removefile(struct vnode
*dvp
, struct vnode
*vp
, struct componentname
*cnp
,
2381 int flags
, int skip_reserve
, int allow_dirs
, struct vnode
*rvp
)
2385 struct hfsmount
*hfsmp
;
2386 struct cat_desc desc
;
2388 vfs_context_t ctx
= cnp
->cn_context
;
2389 int dataforkbusy
= 0;
2390 int rsrcforkbusy
= 0;
2395 int isbigfile
= 0, defer_remove
=0, isdir
=0;
2401 /* Check if we lost a race post lookup. */
2402 if (cp
->c_flag
& (C_NOEXISTS
| C_DELETED
)) {
2406 if (!hfs_valid_cnode(hfsmp
, dvp
, cnp
, cp
->c_fileid
)) {
2410 /* Make sure a remove is permitted */
2411 if (VNODE_IS_RSRC(vp
)) {
2414 /* Don't allow deleting the journal or journal_info_block. */
2416 (cp
->c_fileid
== hfsmp
->hfs_jnlfileid
|| cp
->c_fileid
== hfsmp
->hfs_jnlinfoblkid
)) {
2420 * Hard links require special handling.
2422 if (cp
->c_flag
& C_HARDLINK
) {
2423 if ((flags
& VNODE_REMOVE_NODELETEBUSY
) && vnode_isinuse(vp
, 0)) {
2426 /* A directory hard link with a link count of one is
2427 * treated as a regular directory. Therefore it should
2428 * only be removed using rmdir().
2430 if ((vnode_isdir(vp
) == 1) && (cp
->c_linkcount
== 1) &&
2431 (allow_dirs
== 0)) {
2434 return hfs_unlink(hfsmp
, dvp
, vp
, cnp
, skip_reserve
);
2437 /* Directories should call hfs_rmdir! (unless they have a lot of attributes) */
2438 if (vnode_isdir(vp
)) {
2439 if (allow_dirs
== 0)
2440 return (EPERM
); /* POSIX */
2443 /* Sanity check the parent ids. */
2444 if ((cp
->c_parentcnid
!= hfsmp
->hfs_private_desc
[FILE_HARDLINKS
].cd_cnid
) &&
2445 (cp
->c_parentcnid
!= dcp
->c_fileid
)) {
2449 dcp
->c_flag
|= C_DIR_MODIFICATION
;
2451 // this guy is going away so mark him as such
2452 cp
->c_flag
|= C_DELETED
;
2455 /* Remove our entry from the namei cache. */
2459 * We expect the caller, if operating on files,
2460 * will have passed in a resource fork vnode with
2461 * an iocount, even if there was no content.
2462 * We only do the hfs_truncate on the rsrc fork
2463 * if we know that it DID have content, however.
2464 * This has the bonus of not requiring us to defer
2465 * its removal, unless it is in use.
2468 /* Check if this file is being used. */
2470 dataforkbusy
= vnode_isinuse(vp
, 0);
2471 /* Only need to defer resource fork removal if in use and has content */
2472 if (rvp
&& (cp
->c_blocks
- VTOF(vp
)->ff_blocks
)) {
2473 rsrcforkbusy
= vnode_isinuse(rvp
, 0);
2477 /* Check if we have to break the deletion into multiple pieces. */
2479 isbigfile
= ((cp
->c_datafork
->ff_size
>= HFS_BIGFILE_SIZE
) && overflow_extents(VTOF(vp
)));
2482 /* Check if the file has xattrs. If it does we'll have to delete them in
2483 individual transactions in case there are too many */
2484 if ((hfsmp
->hfs_attribute_vp
!= NULL
) &&
2485 (cp
->c_attr
.ca_recflags
& kHFSHasAttributesMask
) != 0) {
2490 * Carbon semantics prohibit deleting busy files.
2491 * (enforced when VNODE_REMOVE_NODELETEBUSY is requested)
2493 if (dataforkbusy
|| rsrcforkbusy
) {
2494 if ((flags
& VNODE_REMOVE_NODELETEBUSY
) ||
2495 (hfsmp
->hfs_private_desc
[FILE_HARDLINKS
].cd_cnid
== 0)) {
2502 if (hfsmp
->hfs_flags
& HFS_QUOTAS
)
2503 (void)hfs_getinoquota(cp
);
2506 /* Check if we need a ubc_setsize. */
2507 if (isdir
== 0 && (!dataforkbusy
|| !rsrcforkbusy
)) {
2509 * A ubc_setsize can cause a pagein so defer it
2510 * until after the cnode lock is dropped. The
2511 * cnode lock cannot be dropped/reacquired here
2512 * since we might already hold the journal lock.
2514 if (!dataforkbusy
&& cp
->c_datafork
->ff_blocks
&& !isbigfile
) {
2515 cp
->c_flag
|= C_NEED_DATA_SETSIZE
;
2517 if (!rsrcforkbusy
&& rvp
) {
2518 cp
->c_flag
|= C_NEED_RSRC_SETSIZE
;
2522 if ((error
= hfs_start_transaction(hfsmp
)) != 0) {
2527 // XXXdbg - if we're journaled, kill any dirty symlink buffers
2528 if (hfsmp
->jnl
&& vnode_islnk(vp
))
2529 buf_iterate(vp
, hfs_removefile_callback
, BUF_SKIP_NONLOCKED
, (void *)hfsmp
);
2532 * Truncate any non-busy forks. Busy forks will
2533 * get truncated when their vnode goes inactive.
2534 * Note that we will only enter this region if we
2535 * can avoid creating an open-unlinked file. If
2536 * either region is busy, we will have to create an open
2538 * Since we're already inside a transaction,
2539 * tell hfs_truncate to skip the ubc_setsize.
2541 if (isdir
== 0 && (!dataforkbusy
&& !rsrcforkbusy
)) {
2543 * Note that 5th argument to hfs_truncate indicates whether or not
2544 * hfs_update calls should be suppressed in call to do_hfs_truncate
2546 if (!dataforkbusy
&& !isbigfile
&& cp
->c_datafork
->ff_blocks
!= 0) {
2547 /* skip update in hfs_truncate */
2548 error
= hfs_truncate(vp
, (off_t
)0, IO_NDELAY
, 1, 1, ctx
);
2553 if (!rsrcforkbusy
&& rvp
) {
2554 /* skip update in hfs_truncate */
2555 error
= hfs_truncate(rvp
, (off_t
)0, IO_NDELAY
, 1, 1, ctx
);
2563 * Protect against a race with rename by using the component
2564 * name passed in and parent id from dvp (instead of using
2565 * the cp->c_desc which may have changed). Also, be aware that
2566 * because we allow directories to be passed in, we need to special case
2567 * this temporary descriptor in case we were handed a directory.
2570 desc
.cd_flags
= CD_ISDIR
;
2575 desc
.cd_encoding
= cp
->c_desc
.cd_encoding
;
2576 desc
.cd_nameptr
= (const u_int8_t
*)cnp
->cn_nameptr
;
2577 desc
.cd_namelen
= cnp
->cn_namelen
;
2578 desc
.cd_parentcnid
= dcp
->c_fileid
;
2579 desc
.cd_hint
= cp
->c_desc
.cd_hint
;
2580 desc
.cd_cnid
= cp
->c_cnid
;
2584 * There are two cases to consider:
2585 * 1. File/Dir is busy/big/defer_remove ==> move/rename the file/dir
2586 * 2. File is not in use ==> remove the file
2588 * We can get a directory in case 1 because it may have had lots of attributes,
2589 * which need to get removed here.
2591 if (dataforkbusy
|| rsrcforkbusy
|| isbigfile
|| defer_remove
) {
2593 struct cat_desc to_desc
;
2594 struct cat_desc todir_desc
;
2597 * Orphan this file or directory (move to hidden directory).
2598 * Again, we need to take care that we treat directories as directories,
2599 * and files as files. Because directories with attributes can be passed in
2600 * check to make sure that we have a directory or a file before filling in the
2601 * temporary descriptor's flags. We keep orphaned directories AND files in
2602 * the FILE_HARDLINKS private directory since we're generalizing over all
2603 * orphaned filesystem objects.
2605 bzero(&todir_desc
, sizeof(todir_desc
));
2606 todir_desc
.cd_parentcnid
= 2;
2608 MAKE_DELETED_NAME(delname
, sizeof(delname
), cp
->c_fileid
);
2609 bzero(&to_desc
, sizeof(to_desc
));
2610 to_desc
.cd_nameptr
= (const u_int8_t
*)delname
;
2611 to_desc
.cd_namelen
= strlen(delname
);
2612 to_desc
.cd_parentcnid
= hfsmp
->hfs_private_desc
[FILE_HARDLINKS
].cd_cnid
;
2614 to_desc
.cd_flags
= CD_ISDIR
;
2617 to_desc
.cd_flags
= 0;
2619 to_desc
.cd_cnid
= cp
->c_cnid
;
2621 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_EXCLUSIVE_LOCK
);
2622 if (!skip_reserve
) {
2623 if ((error
= cat_preflight(hfsmp
, CAT_RENAME
, NULL
, 0))) {
2624 hfs_systemfile_unlock(hfsmp
, lockflags
);
2629 error
= cat_rename(hfsmp
, &desc
, &todir_desc
,
2630 &to_desc
, (struct cat_desc
*)NULL
);
2633 hfsmp
->hfs_private_attr
[FILE_HARDLINKS
].ca_entries
++;
2635 INC_FOLDERCOUNT(hfsmp
, hfsmp
->hfs_private_attr
[FILE_HARDLINKS
]);
2637 (void) cat_update(hfsmp
, &hfsmp
->hfs_private_desc
[FILE_HARDLINKS
],
2638 &hfsmp
->hfs_private_attr
[FILE_HARDLINKS
], NULL
, NULL
);
2640 /* Update the parent directory */
2641 if (dcp
->c_entries
> 0)
2644 DEC_FOLDERCOUNT(hfsmp
, dcp
->c_attr
);
2646 dcp
->c_dirchangecnt
++;
2647 dcp
->c_ctime
= tv
.tv_sec
;
2648 dcp
->c_mtime
= tv
.tv_sec
;
2649 (void) cat_update(hfsmp
, &dcp
->c_desc
, &dcp
->c_attr
, NULL
, NULL
);
2651 /* Update the file or directory's state */
2652 cp
->c_flag
|= C_DELETED
;
2653 cp
->c_ctime
= tv
.tv_sec
;
2655 (void) cat_update(hfsmp
, &to_desc
, &cp
->c_attr
, NULL
, NULL
);
2657 hfs_systemfile_unlock(hfsmp
, lockflags
);
2661 } else /* Not busy */ {
2663 if (cp
->c_blocks
> 0) {
2664 printf("hfs_remove: attempting to delete a non-empty file %s\n",
2665 cp
->c_desc
.cd_nameptr
);
2670 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
| SFL_ATTRIBUTE
| SFL_BITMAP
, HFS_EXCLUSIVE_LOCK
);
2671 if (!skip_reserve
) {
2672 if ((error
= cat_preflight(hfsmp
, CAT_DELETE
, NULL
, 0))) {
2673 hfs_systemfile_unlock(hfsmp
, lockflags
);
2678 error
= cat_delete(hfsmp
, &desc
, &cp
->c_attr
);
2680 if (error
&& error
!= ENXIO
&& error
!= ENOENT
&& truncated
) {
2681 if ((cp
->c_datafork
&& cp
->c_datafork
->ff_size
!= 0) ||
2682 (cp
->c_rsrcfork
&& cp
->c_rsrcfork
->ff_size
!= 0)) {
2683 off_t data_size
= 0;
2684 off_t rsrc_size
= 0;
2685 if (cp
->c_datafork
) {
2686 data_size
= cp
->c_datafork
->ff_size
;
2688 if (cp
->c_rsrcfork
) {
2689 rsrc_size
= cp
->c_rsrcfork
->ff_size
;
2691 printf("hfs: remove: couldn't delete a truncated file (%s)"
2692 "(error %d, data sz %lld; rsrc sz %lld)",
2693 cp
->c_desc
.cd_nameptr
, error
, data_size
, rsrc_size
);
2694 hfs_mark_volume_inconsistent(hfsmp
);
2696 printf("hfs: remove: strangely enough, deleting truncated file %s (%d) got err %d\n",
2697 cp
->c_desc
.cd_nameptr
, cp
->c_attr
.ca_fileid
, error
);
2702 /* Update the parent directory */
2703 if (dcp
->c_entries
> 0)
2705 dcp
->c_dirchangecnt
++;
2706 dcp
->c_ctime
= tv
.tv_sec
;
2707 dcp
->c_mtime
= tv
.tv_sec
;
2708 (void) cat_update(hfsmp
, &dcp
->c_desc
, &dcp
->c_attr
, NULL
, NULL
);
2710 hfs_systemfile_unlock(hfsmp
, lockflags
);
2715 if (hfsmp
->hfs_flags
& HFS_QUOTAS
)
2716 (void)hfs_chkiq(cp
, -1, NOCRED
, 0);
2719 cp
->c_flag
|= C_NOEXISTS
;
2720 cp
->c_flag
&= ~C_DELETED
;
2721 truncated
= 0; // because the catalog entry is gone
2723 cp
->c_touch_chgtime
= TRUE
; /* XXX needed ? */
2727 * We must never get a directory if we're in this else block. We could
2728 * accidentally drop the number of files in the volume header if we did.
2730 hfs_volupdate(hfsmp
, VOL_RMFILE
, (dcp
->c_cnid
== kHFSRootFolderID
));
2734 * All done with this cnode's descriptor...
2736 * Note: all future catalog calls for this cnode must be by
2737 * fileid only. This is OK for HFS (which doesn't have file
2738 * thread records) since HFS doesn't support the removal of
2741 cat_releasedesc(&cp
->c_desc
);
2745 cp
->c_flag
&= ~C_DELETED
;
2748 /* Commit the truncation to the catalog record */
2750 cp
->c_flag
|= C_FORCEUPDATE
;
2751 cp
->c_touch_chgtime
= TRUE
;
2752 cp
->c_touch_modtime
= TRUE
;
2753 (void) hfs_update(vp
, 0);
2757 hfs_end_transaction(hfsmp
);
2760 dcp
->c_flag
&= ~C_DIR_MODIFICATION
;
2761 wakeup((caddr_t
)&dcp
->c_flag
);
2767 __private_extern__
void
2768 replace_desc(struct cnode
*cp
, struct cat_desc
*cdp
)
2770 // fixes 4348457 and 4463138
2771 if (&cp
->c_desc
== cdp
) {
2775 /* First release allocated name buffer */
2776 if (cp
->c_desc
.cd_flags
& CD_HASBUF
&& cp
->c_desc
.cd_nameptr
!= 0) {
2777 const u_int8_t
*name
= cp
->c_desc
.cd_nameptr
;
2779 cp
->c_desc
.cd_nameptr
= 0;
2780 cp
->c_desc
.cd_namelen
= 0;
2781 cp
->c_desc
.cd_flags
&= ~CD_HASBUF
;
2782 vfs_removename((const char *)name
);
2784 bcopy(cdp
, &cp
->c_desc
, sizeof(cp
->c_desc
));
2786 /* Cnode now owns the name buffer */
2787 cdp
->cd_nameptr
= 0;
2788 cdp
->cd_namelen
= 0;
2789 cdp
->cd_flags
&= ~CD_HASBUF
;
2796 * The VFS layer guarantees that:
2797 * - source and destination will either both be directories, or
2798 * both not be directories.
2799 * - all the vnodes are from the same file system
2801 * When the target is a directory, HFS must ensure that its empty.
2803 * Note that this function requires up to 6 vnodes in order to work properly
2804 * if it is operating on files (and not on directories). This is because only
2805 * files can have resource forks, and we now require iocounts to be held on the
2806 * vnodes corresponding to the resource forks (if applicable) as well as
2807 * the files or directories undergoing rename. The problem with not holding
2808 * iocounts on the resource fork vnodes is that it can lead to a deadlock
2809 * situation: The rsrc fork of the source file may be recycled and reclaimed
2810 * in order to provide a vnode for the destination file's rsrc fork. Since
2811 * data and rsrc forks share the same cnode, we'd eventually try to lock the
2812 * source file's cnode in order to sync its rsrc fork to disk, but it's already
2813 * been locked. By taking the rsrc fork vnodes up front we ensure that they
2814 * cannot be recycled, and that the situation mentioned above cannot happen.
2818 struct vnop_rename_args
/* {
2819 struct vnode *a_fdvp;
2820 struct vnode *a_fvp;
2821 struct componentname *a_fcnp;
2822 struct vnode *a_tdvp;
2823 struct vnode *a_tvp;
2824 struct componentname *a_tcnp;
2825 vfs_context_t a_context;
2828 struct vnode
*tvp
= ap
->a_tvp
;
2829 struct vnode
*tdvp
= ap
->a_tdvp
;
2830 struct vnode
*fvp
= ap
->a_fvp
;
2831 struct vnode
*fdvp
= ap
->a_fdvp
;
2832 struct vnode
*fvp_rsrc
= NULLVP
;
2833 struct vnode
*tvp_rsrc
= NULLVP
;
2834 struct componentname
*tcnp
= ap
->a_tcnp
;
2835 struct componentname
*fcnp
= ap
->a_fcnp
;
2836 struct proc
*p
= vfs_context_proc(ap
->a_context
);
2841 struct cnode
*error_cnode
;
2842 struct cat_desc from_desc
;
2843 struct cat_desc to_desc
;
2844 struct cat_desc out_desc
;
2845 struct hfsmount
*hfsmp
;
2846 cat_cookie_t cookie
;
2847 int tvp_deleted
= 0;
2848 int started_tr
= 0, got_cookie
= 0;
2849 int took_trunc_lock
= 0;
2852 int recycle_rsrc
= 0;
2856 * Before grabbing the four locks, we may need to get an iocount on the resource fork
2857 * vnodes in question, just like hfs_vnop_remove. If fvp and tvp are not
2858 * directories, then go ahead and grab the resource fork vnodes now
2859 * one at a time. We don't actively need the fvp_rsrc to do the rename operation,
2860 * but we need the iocount to prevent the vnode from getting recycled/reclaimed
2861 * during the middle of the VNOP.
2865 if ((vnode_isreg(fvp
)) || (vnode_islnk(fvp
))) {
2867 if ((error
= hfs_lock (VTOC(fvp
), HFS_EXCLUSIVE_LOCK
))) {
2872 * We care if we race against rename/delete with this cnode, so we'll
2873 * error out if this file becomes open-unlinked during this call.
2875 error
= hfs_vgetrsrc(VTOHFS(fvp
), fvp
, &fvp_rsrc
, TRUE
, TRUE
);
2876 hfs_unlock (VTOC(fvp
));
2879 vnode_put (fvp_rsrc
);
2885 if (tvp
&& (vnode_isreg(tvp
) || vnode_islnk(tvp
))) {
2887 * Lock failure is OK on tvp, since we may race with a remove on the dst.
2888 * But this shouldn't stop rename from proceeding, so only try to
2889 * grab the resource fork if the lock succeeded.
2891 if (hfs_lock (VTOC(tvp
), HFS_EXCLUSIVE_LOCK
) == 0) {
2895 * We only care if we get an open-unlinked file on the dst so we
2896 * know to null out tvp/tcp to make the rename operation act
2897 * as if they never existed. Because they're effectively out of the
2898 * namespace already it's fine to do this. If this is true, then
2899 * make sure to unlock the cnode and drop the iocount only after the unlock.
2901 error
= hfs_vgetrsrc(VTOHFS(tvp
), tvp
, &tvp_rsrc
, TRUE
, TRUE
);
2905 * Since we specify TRUE for error-on-unlinked in hfs_vgetrsrc,
2906 * we can get a rsrc fork vp even if it returns an error.
2911 vnode_put (tvp_rsrc
);
2914 /* just bypass truncate lock and act as if we never got tcp/tvp */
2920 /* When tvp exists, take the truncate lock for hfs_removefile(). */
2921 if (tvp
&& (vnode_isreg(tvp
) || vnode_islnk(tvp
))) {
2922 hfs_lock_truncate(VTOC(tvp
), TRUE
);
2923 took_trunc_lock
= 1;
2927 error
= hfs_lockfour(VTOC(fdvp
), VTOC(fvp
), VTOC(tdvp
), tvp
? VTOC(tvp
) : NULL
,
2928 HFS_EXCLUSIVE_LOCK
, &error_cnode
);
2930 if (took_trunc_lock
) {
2931 hfs_unlock_truncate(VTOC(tvp
), TRUE
);
2932 took_trunc_lock
= 0;
2935 * tvp might no longer exist. If the cause of the lock failure
2936 * was tvp, then we can try again with tvp/tcp set to NULL.
2937 * This is ok because the vfs syscall will vnode_put the vnodes
2938 * after we return from hfs_vnop_rename.
2940 if ((error
== ENOENT
) && (tvp
!= NULL
) && (error_cnode
== VTOC(tvp
))) {
2945 /* otherwise, drop iocounts on the rsrc forks and bail out */
2947 vnode_put (fvp_rsrc
);
2950 vnode_put (tvp_rsrc
);
2958 tcp
= tvp
? VTOC(tvp
) : NULL
;
2959 hfsmp
= VTOHFS(tdvp
);
2961 /* Ensure we didn't race src or dst parent directories with rmdir. */
2962 if (fdcp
->c_flag
& (C_NOEXISTS
| C_DELETED
)) {
2967 if (tdcp
->c_flag
& (C_NOEXISTS
| C_DELETED
)) {
2973 /* Check for a race against unlink. The hfs_valid_cnode checks validate
2974 * the parent/child relationship with fdcp and tdcp, as well as the
2975 * component name of the target cnodes.
2977 if ((fcp
->c_flag
& (C_NOEXISTS
| C_DELETED
)) || !hfs_valid_cnode(hfsmp
, fdvp
, fcnp
, fcp
->c_fileid
)) {
2982 if (tcp
&& ((tcp
->c_flag
& (C_NOEXISTS
| C_DELETED
)) || !hfs_valid_cnode(hfsmp
, tdvp
, tcnp
, tcp
->c_fileid
))) {
2984 // hmm, the destination vnode isn't valid any more.
2985 // in this case we can just drop him and pretend he
2986 // never existed in the first place.
2988 if (took_trunc_lock
) {
2989 hfs_unlock_truncate(VTOC(tvp
), TRUE
);
2990 took_trunc_lock
= 0;
2993 hfs_unlockfour(fdcp
, fcp
, tdcp
, tcp
);
2998 // retry the locking with tvp null'ed out
3002 fdcp
->c_flag
|= C_DIR_MODIFICATION
;
3004 tdcp
->c_flag
|= C_DIR_MODIFICATION
;
3008 * Disallow renaming of a directory hard link if the source and
3009 * destination parent directories are different, or a directory whose
3010 * descendant is a directory hard link and the one of the ancestors
3011 * of the destination directory is a directory hard link.
3013 if (vnode_isdir(fvp
) && (fdvp
!= tdvp
)) {
3014 if (fcp
->c_flag
& C_HARDLINK
) {
3018 if (fcp
->c_attr
.ca_recflags
& kHFSHasChildLinkMask
) {
3019 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
3020 if (cat_check_link_ancestry(hfsmp
, tdcp
->c_fileid
, 0)) {
3022 hfs_systemfile_unlock(hfsmp
, lockflags
);
3025 hfs_systemfile_unlock(hfsmp
, lockflags
);
3030 * The following edge case is caught here:
3031 * (to cannot be a descendent of from)
3044 if (tdcp
->c_parentcnid
== fcp
->c_fileid
) {
3050 * The following two edge cases are caught here:
3051 * (note tvp is not empty)
3064 if (tvp
&& vnode_isdir(tvp
) && (tcp
->c_entries
!= 0) && fvp
!= tvp
) {
3070 * The following edge case is caught here:
3071 * (the from child and parent are the same)
3084 * Make sure "from" vnode and its parent are changeable.
3086 if ((fcp
->c_flags
& (IMMUTABLE
| APPEND
)) || (fdcp
->c_flags
& APPEND
)) {
3092 * If the destination parent directory is "sticky", then the
3093 * user must own the parent directory, or the destination of
3094 * the rename, otherwise the destination may not be changed
3095 * (except by root). This implements append-only directories.
3097 * Note that checks for immutable and write access are done
3098 * by the call to hfs_removefile.
3100 if (tvp
&& (tdcp
->c_mode
& S_ISTXT
) &&
3101 (suser(vfs_context_ucred(tcnp
->cn_context
), NULL
)) &&
3102 (kauth_cred_getuid(vfs_context_ucred(tcnp
->cn_context
)) != tdcp
->c_uid
) &&
3103 (hfs_owner_rights(hfsmp
, tcp
->c_uid
, vfs_context_ucred(tcnp
->cn_context
), p
, false)) ) {
3110 (void)hfs_getinoquota(tcp
);
3112 /* Preflighting done, take fvp out of the name space. */
3115 bzero(&from_desc
, sizeof(from_desc
));
3116 from_desc
.cd_nameptr
= (const u_int8_t
*)fcnp
->cn_nameptr
;
3117 from_desc
.cd_namelen
= fcnp
->cn_namelen
;
3118 from_desc
.cd_parentcnid
= fdcp
->c_fileid
;
3119 from_desc
.cd_flags
= fcp
->c_desc
.cd_flags
& ~(CD_HASBUF
| CD_DECOMPOSED
);
3120 from_desc
.cd_cnid
= fcp
->c_cnid
;
3122 bzero(&to_desc
, sizeof(to_desc
));
3123 to_desc
.cd_nameptr
= (const u_int8_t
*)tcnp
->cn_nameptr
;
3124 to_desc
.cd_namelen
= tcnp
->cn_namelen
;
3125 to_desc
.cd_parentcnid
= tdcp
->c_fileid
;
3126 to_desc
.cd_flags
= fcp
->c_desc
.cd_flags
& ~(CD_HASBUF
| CD_DECOMPOSED
);
3127 to_desc
.cd_cnid
= fcp
->c_cnid
;
3129 if ((error
= hfs_start_transaction(hfsmp
)) != 0) {
3134 /* hfs_vnop_link() and hfs_vnop_rename() set kHFSHasChildLinkMask
3135 * inside a journal transaction and without holding a cnode lock.
3136 * As setting of this bit depends on being in journal transaction for
3137 * concurrency, check this bit again after we start journal transaction for rename
3138 * to ensure that this directory does not have any descendant that
3139 * is a directory hard link.
3141 if (vnode_isdir(fvp
) && (fdvp
!= tdvp
)) {
3142 if (fcp
->c_attr
.ca_recflags
& kHFSHasChildLinkMask
) {
3143 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
3144 if (cat_check_link_ancestry(hfsmp
, tdcp
->c_fileid
, 0)) {
3146 hfs_systemfile_unlock(hfsmp
, lockflags
);
3149 hfs_systemfile_unlock(hfsmp
, lockflags
);
3153 // if it's a hardlink then re-lookup the name so
3154 // that we get the correct cnid in from_desc (see
3155 // the comment in hfs_removefile for more details)
3157 if (fcp
->c_flag
& C_HARDLINK
) {
3158 struct cat_desc tmpdesc
;
3161 tmpdesc
.cd_nameptr
= (const u_int8_t
*)fcnp
->cn_nameptr
;
3162 tmpdesc
.cd_namelen
= fcnp
->cn_namelen
;
3163 tmpdesc
.cd_parentcnid
= fdcp
->c_fileid
;
3164 tmpdesc
.cd_hint
= fdcp
->c_childhint
;
3165 tmpdesc
.cd_flags
= fcp
->c_desc
.cd_flags
& CD_ISDIR
;
3166 tmpdesc
.cd_encoding
= 0;
3168 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
3170 if (cat_lookup(hfsmp
, &tmpdesc
, 0, NULL
, NULL
, NULL
, &real_cnid
) != 0) {
3171 hfs_systemfile_unlock(hfsmp
, lockflags
);
3175 // use the real cnid instead of whatever happened to be there
3176 from_desc
.cd_cnid
= real_cnid
;
3177 hfs_systemfile_unlock(hfsmp
, lockflags
);
3181 * Reserve some space in the Catalog file.
3183 if ((error
= cat_preflight(hfsmp
, CAT_RENAME
+ CAT_DELETE
, &cookie
, p
))) {
3189 * If the destination exists then it may need to be removed.
3193 * When fvp matches tvp they could be case variants
3194 * or matching hard links.
3197 if (!(fcp
->c_flag
& C_HARDLINK
)) {
3198 goto skip_rm
; /* simple case variant */
3200 } else if ((fdvp
!= tdvp
) ||
3201 (hfsmp
->hfs_flags
& HFS_CASE_SENSITIVE
)) {
3202 goto out
; /* matching hardlinks, nothing to do */
3204 } else if (hfs_namecmp((const u_int8_t
*)fcnp
->cn_nameptr
, fcnp
->cn_namelen
,
3205 (const u_int8_t
*)tcnp
->cn_nameptr
, tcnp
->cn_namelen
) == 0) {
3206 goto skip_rm
; /* case-variant hardlink in the same dir */
3208 goto out
; /* matching hardlink, nothing to do */
3212 if (vnode_isdir(tvp
))
3213 error
= hfs_removedir(tdvp
, tvp
, tcnp
, HFSRM_SKIP_RESERVE
);
3215 error
= hfs_removefile(tdvp
, tvp
, tcnp
, 0, HFSRM_SKIP_RESERVE
, 0, tvp_rsrc
);
3218 * If the destination file had a rsrc fork vnode, it may have been cleaned up
3219 * in hfs_removefile if it was not busy (had no usecounts). This is possible
3220 * because we grabbed the iocount on the rsrc fork safely at the beginning
3221 * of the function before we did the lockfour. However, we may still need
3222 * to take action to prevent block leaks, so aggressively recycle the vnode
3223 * if possible. The vnode cannot be recycled because we hold an iocount on it.
3226 if ((error
== 0) && (tcp
->c_flag
& C_DELETED
) && tvp_rsrc
&& !vnode_isinuse(tvp_rsrc
, 0)) {
3237 * All done with tvp and fvp.
3239 * We also jump to this point if there was no destination observed during lookup and namei.
3240 * However, because only iocounts are held at the VFS layer, there is nothing preventing a
3241 * competing thread from racing us and creating a file or dir at the destination of this rename
3242 * operation. If this occurs, it may cause us to get a spurious EEXIST out of the cat_rename
3243 * call below. To preserve rename's atomicity, we need to signal VFS to re-drive the
3244 * namei/lookup and restart the rename operation. EEXIST is an allowable errno to be bubbled
3245 * out of the rename syscall, but not for this reason, since it is a synonym errno for ENOTEMPTY.
3246 * To signal VFS, we return ERECYCLE (which is also used for lookup restarts). This errno
3247 * will be swallowed and it will restart the operation.
3250 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_EXCLUSIVE_LOCK
);
3251 error
= cat_rename(hfsmp
, &from_desc
, &tdcp
->c_desc
, &to_desc
, &out_desc
);
3252 hfs_systemfile_unlock(hfsmp
, lockflags
);
3255 if (error
== EEXIST
) {
3261 /* Invalidate negative cache entries in the destination directory */
3262 if (tdcp
->c_flag
& C_NEG_ENTRIES
) {
3263 cache_purge_negatives(tdvp
);
3264 tdcp
->c_flag
&= ~C_NEG_ENTRIES
;
3267 /* Update cnode's catalog descriptor */
3268 replace_desc(fcp
, &out_desc
);
3269 fcp
->c_parentcnid
= tdcp
->c_fileid
;
3272 hfs_volupdate(hfsmp
, vnode_isdir(fvp
) ? VOL_RMDIR
: VOL_RMFILE
,
3273 (fdcp
->c_cnid
== kHFSRootFolderID
));
3274 hfs_volupdate(hfsmp
, vnode_isdir(fvp
) ? VOL_MKDIR
: VOL_MKFILE
,
3275 (tdcp
->c_cnid
== kHFSRootFolderID
));
3277 /* Update both parent directories. */
3279 if (vnode_isdir(fvp
)) {
3280 /* If the source directory has directory hard link
3281 * descendants, set the kHFSHasChildLinkBit in the
3282 * destination parent hierarchy
3284 if ((fcp
->c_attr
.ca_recflags
& kHFSHasChildLinkMask
) &&
3285 !(tdcp
->c_attr
.ca_recflags
& kHFSHasChildLinkMask
)) {
3287 tdcp
->c_attr
.ca_recflags
|= kHFSHasChildLinkMask
;
3289 error
= cat_set_childlinkbit(hfsmp
, tdcp
->c_parentcnid
);
3291 printf ("hfs_vnop_rename: error updating parent chain for %u\n", tdcp
->c_cnid
);
3295 INC_FOLDERCOUNT(hfsmp
, tdcp
->c_attr
);
3296 DEC_FOLDERCOUNT(hfsmp
, fdcp
->c_attr
);
3299 tdcp
->c_dirchangecnt
++;
3300 if (fdcp
->c_entries
> 0)
3302 fdcp
->c_dirchangecnt
++;
3303 fdcp
->c_touch_chgtime
= TRUE
;
3304 fdcp
->c_touch_modtime
= TRUE
;
3306 fdcp
->c_flag
|= C_FORCEUPDATE
; // XXXdbg - force it out!
3307 (void) hfs_update(fdvp
, 0);
3309 tdcp
->c_childhint
= out_desc
.cd_hint
; /* Cache directory's location */
3310 tdcp
->c_touch_chgtime
= TRUE
;
3311 tdcp
->c_touch_modtime
= TRUE
;
3313 tdcp
->c_flag
|= C_FORCEUPDATE
; // XXXdbg - force it out!
3314 (void) hfs_update(tdvp
, 0);
3317 cat_postflight(hfsmp
, &cookie
, p
);
3320 hfs_end_transaction(hfsmp
);
3323 fdcp
->c_flag
&= ~C_DIR_MODIFICATION
;
3324 wakeup((caddr_t
)&fdcp
->c_flag
);
3326 tdcp
->c_flag
&= ~C_DIR_MODIFICATION
;
3327 wakeup((caddr_t
)&tdcp
->c_flag
);
3330 if (took_trunc_lock
)
3331 hfs_unlock_truncate(VTOC(tvp
), TRUE
);
3333 hfs_unlockfour(fdcp
, fcp
, tdcp
, tcp
);
3336 * Now that we've dropped all of the locks, we need to force an inactive and a recycle
3337 * on the old destination's rsrc fork to prevent a leak of its blocks. Note that
3338 * doing the ref/rele is to twiddle the VL_NEEDINACTIVE bit of the vnode's flags, so that
3339 * on the last vnode_put for this vnode, we will force inactive to get triggered.
3340 * We hold an iocount from the beginning of this function so we know it couldn't have been
3345 vref
= vnode_ref(tvp_rsrc
);
3347 vnode_rele(tvp_rsrc
);
3349 vnode_recycle(tvp_rsrc
);
3352 /* Now vnode_put the resource forks vnodes if necessary */
3354 vnode_put(tvp_rsrc
);
3357 vnode_put(fvp_rsrc
);
3360 /* After tvp is removed the only acceptable error is EIO */
3361 if (error
&& tvp_deleted
)
3372 hfs_vnop_mkdir(struct vnop_mkdir_args
*ap
)
3374 /***** HACK ALERT ********/
3375 ap
->a_cnp
->cn_flags
|= MAKEENTRY
;
3376 return hfs_makenode(ap
->a_dvp
, ap
->a_vpp
, ap
->a_cnp
, ap
->a_vap
, ap
->a_context
);
3381 * Create a symbolic link.
3384 hfs_vnop_symlink(struct vnop_symlink_args
*ap
)
3386 struct vnode
**vpp
= ap
->a_vpp
;
3387 struct vnode
*dvp
= ap
->a_dvp
;
3388 struct vnode
*vp
= NULL
;
3389 struct cnode
*cp
= NULL
;
3390 struct hfsmount
*hfsmp
;
3391 struct filefork
*fp
;
3392 struct buf
*bp
= NULL
;
3398 /* HFS standard disks don't support symbolic links */
3399 if (VTOVCB(dvp
)->vcbSigWord
!= kHFSPlusSigWord
)
3402 /* Check for empty target name */
3403 if (ap
->a_target
[0] == 0)
3406 hfsmp
= VTOHFS(dvp
);
3407 len
= strlen(ap
->a_target
);
3409 /* Check for free space */
3410 if (((u_int64_t
)hfs_freeblks(hfsmp
, 0) * (u_int64_t
)hfsmp
->blockSize
) < len
) {
3414 /* Create the vnode */
3415 ap
->a_vap
->va_mode
|= S_IFLNK
;
3416 if ((error
= hfs_makenode(dvp
, vpp
, ap
->a_cnp
, ap
->a_vap
, ap
->a_context
))) {
3420 if ((error
= hfs_lock(VTOC(vp
), HFS_EXCLUSIVE_LOCK
))) {
3426 if (cp
->c_flag
& (C_NOEXISTS
| C_DELETED
)) {
3431 (void)hfs_getinoquota(cp
);
3434 if ((error
= hfs_start_transaction(hfsmp
)) != 0) {
3440 * Allocate space for the link.
3442 * Since we're already inside a transaction,
3443 * tell hfs_truncate to skip the ubc_setsize.
3445 * Don't need truncate lock since a symlink is treated as a system file.
3447 error
= hfs_truncate(vp
, len
, IO_NOZEROFILL
, 1, 0, ap
->a_context
);
3449 /* On errors, remove the symlink file */
3452 * End the transaction so we don't re-take the cnode lock
3453 * below while inside a transaction (lock order violation).
3455 hfs_end_transaction(hfsmp
);
3457 /* hfs_removefile() requires holding the truncate lock */
3459 hfs_lock_truncate(cp
, TRUE
);
3460 hfs_lock(cp
, HFS_FORCE_LOCK
);
3462 if (hfs_start_transaction(hfsmp
) != 0) {
3464 hfs_unlock_truncate(cp
, TRUE
);
3468 (void) hfs_removefile(dvp
, vp
, ap
->a_cnp
, 0, 0, 0, NULL
);
3469 hfs_unlock_truncate(cp
, TRUE
);
3473 /* Write the link to disk */
3474 bp
= buf_getblk(vp
, (daddr64_t
)0, roundup((int)fp
->ff_size
, hfsmp
->hfs_physical_block_size
),
3477 journal_modify_block_start(hfsmp
->jnl
, bp
);
3479 datap
= (char *)buf_dataptr(bp
);
3480 bzero(datap
, buf_size(bp
));
3481 bcopy(ap
->a_target
, datap
, len
);
3484 journal_modify_block_end(hfsmp
->jnl
, bp
, NULL
, NULL
);
3489 * We defered the ubc_setsize for hfs_truncate
3490 * since we were inside a transaction.
3492 * We don't need to drop the cnode lock here
3493 * since this is a symlink.
3495 ubc_setsize(vp
, len
);
3498 hfs_end_transaction(hfsmp
);
3499 if ((cp
!= NULL
) && (vp
!= NULL
)) {
3512 /* structures to hold a "." or ".." directory entry */
3513 struct hfs_stddotentry
{
3514 u_int32_t d_fileno
; /* unique file number */
3515 u_int16_t d_reclen
; /* length of this structure */
3516 u_int8_t d_type
; /* dirent file type */
3517 u_int8_t d_namlen
; /* len of filename */
3518 char d_name
[4]; /* "." or ".." */
3521 struct hfs_extdotentry
{
3522 u_int64_t d_fileno
; /* unique file number */
3523 u_int64_t d_seekoff
; /* seek offset (optional, used by servers) */
3524 u_int16_t d_reclen
; /* length of this structure */
3525 u_int16_t d_namlen
; /* len of filename */
3526 u_int8_t d_type
; /* dirent file type */
3527 u_char d_name
[3]; /* "." or ".." */
3531 struct hfs_stddotentry std
;
3532 struct hfs_extdotentry ext
;
3536 * hfs_vnop_readdir reads directory entries into the buffer pointed
3537 * to by uio, in a filesystem independent format. Up to uio_resid
3538 * bytes of data can be transferred. The data in the buffer is a
3539 * series of packed dirent structures where each one contains the
3540 * following entries:
3542 * u_int32_t d_fileno; // file number of entry
3543 * u_int16_t d_reclen; // length of this record
3544 * u_int8_t d_type; // file type
3545 * u_int8_t d_namlen; // length of string in d_name
3546 * char d_name[MAXNAMELEN+1]; // null terminated file name
3548 * The current position (uio_offset) refers to the next block of
3549 * entries. The offset can only be set to a value previously
3550 * returned by hfs_vnop_readdir or zero. This offset does not have
3551 * to match the number of bytes returned (in uio_resid).
3553 * In fact, the offset used by HFS is essentially an index (26 bits)
3554 * with a tag (6 bits). The tag is for associating the next request
3555 * with the current request. This enables us to have multiple threads
3556 * reading the directory while the directory is also being modified.
3558 * Each tag/index pair is tied to a unique directory hint. The hint
3559 * contains information (filename) needed to build the catalog b-tree
3560 * key for finding the next set of entries.
3562 * If the directory is marked as deleted-but-in-use (cp->c_flag & C_DELETED),
3563 * do NOT synthesize entries for "." and "..".
3566 hfs_vnop_readdir(ap
)
3567 struct vnop_readdir_args
/* {
3573 vfs_context_t a_context;
3576 struct vnode
*vp
= ap
->a_vp
;
3577 uio_t uio
= ap
->a_uio
;
3579 struct hfsmount
*hfsmp
;
3580 directoryhint_t
*dirhint
= NULL
;
3581 directoryhint_t localhint
;
3586 user_addr_t user_start
= 0;
3587 user_size_t user_len
= 0;
3594 cnid_t cnid_hint
= 0;
3597 startoffset
= offset
= uio_offset(uio
);
3598 extended
= (ap
->a_flags
& VNODE_READDIR_EXTENDED
);
3599 nfs_cookies
= extended
&& (ap
->a_flags
& VNODE_READDIR_REQSEEKOFF
);
3601 /* Sanity check the uio data. */
3602 if (uio_iovcnt(uio
) > 1)
3604 /* Note that the dirhint calls require an exclusive lock. */
3605 if ((error
= hfs_lock(VTOC(vp
), HFS_EXCLUSIVE_LOCK
)))
3610 /* Pick up cnid hint (if any). */
3612 cnid_hint
= (cnid_t
)(uio_offset(uio
) >> 32);
3613 uio_setoffset(uio
, uio_offset(uio
) & 0x00000000ffffffffLL
);
3614 if (cnid_hint
== INT_MAX
) { /* searching pass the last item */
3620 * Synthesize entries for "." and "..", unless the directory has
3621 * been deleted, but not closed yet (lazy delete in progress).
3623 if (offset
== 0 && !(cp
->c_flag
& C_DELETED
)) {
3624 hfs_dotentry_t dotentry
[2];
3628 struct hfs_extdotentry
*entry
= &dotentry
[0].ext
;
3630 entry
->d_fileno
= cp
->c_cnid
;
3631 entry
->d_reclen
= sizeof(struct hfs_extdotentry
);
3632 entry
->d_type
= DT_DIR
;
3633 entry
->d_namlen
= 1;
3634 entry
->d_name
[0] = '.';
3635 entry
->d_name
[1] = '\0';
3636 entry
->d_name
[2] = '\0';
3637 entry
->d_seekoff
= 1;
3640 entry
->d_fileno
= cp
->c_parentcnid
;
3641 entry
->d_reclen
= sizeof(struct hfs_extdotentry
);
3642 entry
->d_type
= DT_DIR
;
3643 entry
->d_namlen
= 2;
3644 entry
->d_name
[0] = '.';
3645 entry
->d_name
[1] = '.';
3646 entry
->d_name
[2] = '\0';
3647 entry
->d_seekoff
= 2;
3648 uiosize
= 2 * sizeof(struct hfs_extdotentry
);
3650 struct hfs_stddotentry
*entry
= &dotentry
[0].std
;
3652 entry
->d_fileno
= cp
->c_cnid
;
3653 entry
->d_reclen
= sizeof(struct hfs_stddotentry
);
3654 entry
->d_type
= DT_DIR
;
3655 entry
->d_namlen
= 1;
3656 *(int *)&entry
->d_name
[0] = 0;
3657 entry
->d_name
[0] = '.';
3660 entry
->d_fileno
= cp
->c_parentcnid
;
3661 entry
->d_reclen
= sizeof(struct hfs_stddotentry
);
3662 entry
->d_type
= DT_DIR
;
3663 entry
->d_namlen
= 2;
3664 *(int *)&entry
->d_name
[0] = 0;
3665 entry
->d_name
[0] = '.';
3666 entry
->d_name
[1] = '.';
3667 uiosize
= 2 * sizeof(struct hfs_stddotentry
);
3669 if ((error
= uiomove((caddr_t
)&dotentry
, uiosize
, uio
))) {
3675 /* If there are no real entries then we're done. */
3676 if (cp
->c_entries
== 0) {
3679 uio_setoffset(uio
, offset
);
3684 // We have to lock the user's buffer here so that we won't
3685 // fault on it after we've acquired a shared lock on the
3686 // catalog file. The issue is that you can get a 3-way
3687 // deadlock if someone else starts a transaction and then
3688 // tries to lock the catalog file but can't because we're
3689 // here and we can't service our page fault because VM is
3690 // blocked trying to start a transaction as a result of
3691 // trying to free up pages for our page fault. It's messy
3692 // but it does happen on dual-processors that are paging
3693 // heavily (see radar 3082639 for more info). By locking
3694 // the buffer up-front we prevent ourselves from faulting
3695 // while holding the shared catalog file lock.
3697 // Fortunately this and hfs_search() are the only two places
3698 // currently (10/30/02) that can fault on user data with a
3699 // shared lock on the catalog file.
3701 if (hfsmp
->jnl
&& uio_isuserspace(uio
)) {
3702 user_start
= uio_curriovbase(uio
);
3703 user_len
= uio_curriovlen(uio
);
3705 if ((error
= vslock(user_start
, user_len
)) != 0) {
3710 /* Convert offset into a catalog directory index. */
3711 index
= (offset
& HFS_INDEX_MASK
) - 2;
3712 tag
= offset
& ~HFS_INDEX_MASK
;
3714 /* Lock catalog during cat_findname and cat_getdirentries. */
3715 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
3717 /* When called from NFS, try and resolve a cnid hint. */
3718 if (nfs_cookies
&& cnid_hint
!= 0) {
3719 if (cat_findname(hfsmp
, cnid_hint
, &localhint
.dh_desc
) == 0) {
3720 if ( localhint
.dh_desc
.cd_parentcnid
== cp
->c_fileid
) {
3721 localhint
.dh_index
= index
- 1;
3722 localhint
.dh_time
= 0;
3723 bzero(&localhint
.dh_link
, sizeof(localhint
.dh_link
));
3724 dirhint
= &localhint
; /* don't forget to release the descriptor */
3726 cat_releasedesc(&localhint
.dh_desc
);
3731 /* Get a directory hint (cnode must be locked exclusive) */
3732 if (dirhint
== NULL
) {
3733 dirhint
= hfs_getdirhint(cp
, ((index
- 1) & HFS_INDEX_MASK
) | tag
, 0);
3735 /* Hide tag from catalog layer. */
3736 dirhint
->dh_index
&= HFS_INDEX_MASK
;
3737 if (dirhint
->dh_index
== HFS_INDEX_MASK
) {
3738 dirhint
->dh_index
= -1;
3743 dirhint
->dh_threadhint
= cp
->c_dirthreadhint
;
3747 * If we have a non-zero index, there is a possibility that during the last
3748 * call to hfs_vnop_readdir we hit EOF for this directory. If that is the case
3749 * then we don't want to return any new entries for the caller. Just return 0
3750 * items, mark the eofflag, and bail out. Because we won't have done any work, the
3751 * code at the end of the function will release the dirhint for us.
3753 * Don't forget to unlock the catalog lock on the way out, too.
3755 if (dirhint
->dh_desc
.cd_flags
& CD_EOF
) {
3758 uio_setoffset(uio
, startoffset
);
3759 hfs_systemfile_unlock (hfsmp
, lockflags
);
3765 /* Pack the buffer with dirent entries. */
3766 error
= cat_getdirentries(hfsmp
, cp
->c_entries
, dirhint
, uio
, extended
, &items
, &eofflag
);
3768 if (index
== 0 && error
== 0) {
3769 cp
->c_dirthreadhint
= dirhint
->dh_threadhint
;
3772 hfs_systemfile_unlock(hfsmp
, lockflags
);
3778 /* Get index to the next item */
3781 if (items
>= (int)cp
->c_entries
) {
3785 /* Convert catalog directory index back into an offset. */
3787 tag
= (++cp
->c_dirhinttag
) << HFS_INDEX_BITS
;
3788 uio_setoffset(uio
, (index
+ 2) | tag
);
3789 dirhint
->dh_index
|= tag
;
3792 cp
->c_touch_acctime
= TRUE
;
3794 if (ap
->a_numdirent
) {
3795 if (startoffset
== 0)
3797 *ap
->a_numdirent
= items
;
3802 vsunlock(user_start
, user_len
, TRUE
);
3804 /* If we didn't do anything then go ahead and dump the hint. */
3805 if ((dirhint
!= NULL
) &&
3806 (dirhint
!= &localhint
) &&
3807 (uio_offset(uio
) == startoffset
)) {
3808 hfs_reldirhint(cp
, dirhint
);
3811 if (ap
->a_eofflag
) {
3812 *ap
->a_eofflag
= eofflag
;
3814 if (dirhint
== &localhint
) {
3815 cat_releasedesc(&localhint
.dh_desc
);
3823 * Read contents of a symbolic link.
3826 hfs_vnop_readlink(ap
)
3827 struct vnop_readlink_args
/* {
3830 vfs_context_t a_context;
3833 struct vnode
*vp
= ap
->a_vp
;
3835 struct filefork
*fp
;
3838 if (!vnode_islnk(vp
))
3841 if ((error
= hfs_lock(VTOC(vp
), HFS_EXCLUSIVE_LOCK
)))
3846 /* Zero length sym links are not allowed */
3847 if (fp
->ff_size
== 0 || fp
->ff_size
> MAXPATHLEN
) {
3848 printf("hfs: zero length symlink on fileid %d\n", cp
->c_fileid
);
3853 /* Cache the path so we don't waste buffer cache resources */
3854 if (fp
->ff_symlinkptr
== NULL
) {
3855 struct buf
*bp
= NULL
;
3857 MALLOC(fp
->ff_symlinkptr
, char *, fp
->ff_size
, M_TEMP
, M_WAITOK
);
3858 if (fp
->ff_symlinkptr
== NULL
) {
3862 error
= (int)buf_meta_bread(vp
, (daddr64_t
)0,
3863 roundup((int)fp
->ff_size
, VTOHFS(vp
)->hfs_physical_block_size
),
3864 vfs_context_ucred(ap
->a_context
), &bp
);
3868 if (fp
->ff_symlinkptr
) {
3869 FREE(fp
->ff_symlinkptr
, M_TEMP
);
3870 fp
->ff_symlinkptr
= NULL
;
3874 bcopy((char *)buf_dataptr(bp
), fp
->ff_symlinkptr
, (size_t)fp
->ff_size
);
3876 if (VTOHFS(vp
)->jnl
&& (buf_flags(bp
) & B_LOCKED
) == 0) {
3877 buf_markinvalid(bp
); /* data no longer needed */
3881 error
= uiomove((caddr_t
)fp
->ff_symlinkptr
, (int)fp
->ff_size
, ap
->a_uio
);
3884 * Keep track blocks read
3886 if ((VTOHFS(vp
)->hfc_stage
== HFC_RECORDING
) && (error
== 0)) {
3889 * If this file hasn't been seen since the start of
3890 * the current sampling period then start over.
3892 if (cp
->c_atime
< VTOHFS(vp
)->hfc_timebase
)
3893 VTOF(vp
)->ff_bytesread
= fp
->ff_size
;
3895 VTOF(vp
)->ff_bytesread
+= fp
->ff_size
;
3897 // if (VTOF(vp)->ff_bytesread > fp->ff_size)
3898 // cp->c_touch_acctime = TRUE;
3908 * Get configurable pathname variables.
3911 hfs_vnop_pathconf(ap
)
3912 struct vnop_pathconf_args
/* {
3916 vfs_context_t a_context;
3919 switch (ap
->a_name
) {
3921 if (VTOHFS(ap
->a_vp
)->hfs_flags
& HFS_STANDARD
)
3924 *ap
->a_retval
= HFS_LINK_MAX
;
3927 if (VTOHFS(ap
->a_vp
)->hfs_flags
& HFS_STANDARD
)
3928 *ap
->a_retval
= kHFSMaxFileNameChars
; /* 255 */
3930 *ap
->a_retval
= kHFSPlusMaxFileNameChars
; /* 31 */
3933 *ap
->a_retval
= PATH_MAX
; /* 1024 */
3936 *ap
->a_retval
= PIPE_BUF
;
3938 case _PC_CHOWN_RESTRICTED
:
3939 *ap
->a_retval
= 200112; /* _POSIX_CHOWN_RESTRICTED */
3942 *ap
->a_retval
= 200112; /* _POSIX_NO_TRUNC */
3944 case _PC_NAME_CHARS_MAX
:
3945 *ap
->a_retval
= kHFSPlusMaxFileNameChars
;
3947 case _PC_CASE_SENSITIVE
:
3948 if (VTOHFS(ap
->a_vp
)->hfs_flags
& HFS_CASE_SENSITIVE
)
3953 case _PC_CASE_PRESERVING
:
3956 case _PC_FILESIZEBITS
:
3957 if (VTOHFS(ap
->a_vp
)->hfs_flags
& HFS_STANDARD
)
3960 *ap
->a_retval
= 64; /* number of bits to store max file size */
3971 * Update a cnode's on-disk metadata.
3973 * If waitfor is set, then wait for the disk write of
3974 * the node to complete.
3976 * The cnode must be locked exclusive
3980 hfs_update(struct vnode
*vp
, __unused
int waitfor
)
3982 struct cnode
*cp
= VTOC(vp
);
3984 struct cat_fork
*dataforkp
= NULL
;
3985 struct cat_fork
*rsrcforkp
= NULL
;
3986 struct cat_fork datafork
;
3987 struct cat_fork rsrcfork
;
3988 struct hfsmount
*hfsmp
;
3995 if (((vnode_issystem(vp
) && (cp
->c_cnid
< kHFSFirstUserCatalogNodeID
))) ||
3996 hfsmp
->hfs_catalog_vp
== NULL
){
3999 if ((hfsmp
->hfs_flags
& HFS_READ_ONLY
) || (cp
->c_mode
== 0)) {
4000 cp
->c_flag
&= ~C_MODIFIED
;
4001 cp
->c_touch_acctime
= 0;
4002 cp
->c_touch_chgtime
= 0;
4003 cp
->c_touch_modtime
= 0;
4007 hfs_touchtimes(hfsmp
, cp
);
4009 /* Nothing to update. */
4010 if ((cp
->c_flag
& (C_MODIFIED
| C_FORCEUPDATE
)) == 0) {
4015 dataforkp
= &cp
->c_datafork
->ff_data
;
4017 rsrcforkp
= &cp
->c_rsrcfork
->ff_data
;
4020 * For delayed allocations updates are
4021 * postponed until an fsync or the file
4022 * gets written to disk.
4024 * Deleted files can defer meta data updates until inactive.
4026 * If we're ever called with the C_FORCEUPDATE flag though
4027 * we have to do the update.
4029 if (ISSET(cp
->c_flag
, C_FORCEUPDATE
) == 0 &&
4030 (ISSET(cp
->c_flag
, C_DELETED
) ||
4031 (dataforkp
&& cp
->c_datafork
->ff_unallocblocks
) ||
4032 (rsrcforkp
&& cp
->c_rsrcfork
->ff_unallocblocks
))) {
4033 // cp->c_flag &= ~(C_ACCESS | C_CHANGE | C_UPDATE);
4034 cp
->c_flag
|= C_MODIFIED
;
4039 if ((error
= hfs_start_transaction(hfsmp
)) != 0) {
4044 * For files with invalid ranges (holes) the on-disk
4045 * field representing the size of the file (cf_size)
4046 * must be no larger than the start of the first hole.
4048 if (dataforkp
&& !TAILQ_EMPTY(&cp
->c_datafork
->ff_invalidranges
)) {
4049 bcopy(dataforkp
, &datafork
, sizeof(datafork
));
4050 datafork
.cf_size
= TAILQ_FIRST(&cp
->c_datafork
->ff_invalidranges
)->rl_start
;
4051 dataforkp
= &datafork
;
4052 } else if (dataforkp
&& (cp
->c_datafork
->ff_unallocblocks
!= 0)) {
4053 // always make sure the block count and the size
4054 // of the file match the number of blocks actually
4055 // allocated to the file on disk
4056 bcopy(dataforkp
, &datafork
, sizeof(datafork
));
4057 // make sure that we don't assign a negative block count
4058 if (cp
->c_datafork
->ff_blocks
< cp
->c_datafork
->ff_unallocblocks
) {
4059 panic("hfs: ff_blocks %d is less than unalloc blocks %d\n",
4060 cp
->c_datafork
->ff_blocks
, cp
->c_datafork
->ff_unallocblocks
);
4062 datafork
.cf_blocks
= (cp
->c_datafork
->ff_blocks
- cp
->c_datafork
->ff_unallocblocks
);
4063 datafork
.cf_size
= datafork
.cf_blocks
* HFSTOVCB(hfsmp
)->blockSize
;
4064 dataforkp
= &datafork
;
4068 * For resource forks with delayed allocations, make sure
4069 * the block count and file size match the number of blocks
4070 * actually allocated to the file on disk.
4072 if (rsrcforkp
&& (cp
->c_rsrcfork
->ff_unallocblocks
!= 0)) {
4073 bcopy(rsrcforkp
, &rsrcfork
, sizeof(rsrcfork
));
4074 rsrcfork
.cf_blocks
= (cp
->c_rsrcfork
->ff_blocks
- cp
->c_rsrcfork
->ff_unallocblocks
);
4075 rsrcfork
.cf_size
= rsrcfork
.cf_blocks
* HFSTOVCB(hfsmp
)->blockSize
;
4076 rsrcforkp
= &rsrcfork
;
4080 * Lock the Catalog b-tree file.
4082 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_EXCLUSIVE_LOCK
);
4084 /* XXX - waitfor is not enforced */
4085 error
= cat_update(hfsmp
, &cp
->c_desc
, &cp
->c_attr
, dataforkp
, rsrcforkp
);
4087 hfs_systemfile_unlock(hfsmp
, lockflags
);
4089 /* After the updates are finished, clear the flags */
4090 cp
->c_flag
&= ~(C_MODIFIED
| C_FORCEUPDATE
);
4092 hfs_end_transaction(hfsmp
);
4098 * Allocate a new node
4099 * Note - Function does not create and return a vnode for whiteout creation.
4102 hfs_makenode(struct vnode
*dvp
, struct vnode
**vpp
, struct componentname
*cnp
,
4103 struct vnode_attr
*vap
, vfs_context_t ctx
)
4105 struct cnode
*cp
= NULL
;
4106 struct cnode
*dcp
= NULL
;
4108 struct hfsmount
*hfsmp
;
4109 struct cat_desc in_desc
, out_desc
;
4110 struct cat_attr attr
;
4113 int error
, started_tr
= 0;
4114 enum vtype vnodetype
;
4117 if ((error
= hfs_lock(VTOC(dvp
), HFS_EXCLUSIVE_LOCK
)))
4120 /* set the cnode pointer only after successfully acquiring lock */
4122 dcp
->c_flag
|= C_DIR_MODIFICATION
;
4124 hfsmp
= VTOHFS(dvp
);
4127 out_desc
.cd_flags
= 0;
4128 out_desc
.cd_nameptr
= NULL
;
4130 vnodetype
= vap
->va_type
;
4131 if (vnodetype
== VNON
)
4133 mode
= MAKEIMODE(vnodetype
, vap
->va_mode
);
4135 /* Check if were out of usable disk space. */
4136 if ((hfs_freeblks(hfsmp
, 1) == 0) && (vfs_context_suser(ctx
) != 0)) {
4143 /* Setup the default attributes */
4144 bzero(&attr
, sizeof(attr
));
4145 attr
.ca_mode
= mode
;
4146 attr
.ca_linkcount
= 1;
4147 if (VATTR_IS_ACTIVE(vap
, va_rdev
)) {
4148 attr
.ca_rdev
= vap
->va_rdev
;
4150 if (VATTR_IS_ACTIVE(vap
, va_create_time
)) {
4151 VATTR_SET_SUPPORTED(vap
, va_create_time
);
4152 attr
.ca_itime
= vap
->va_create_time
.tv_sec
;
4154 attr
.ca_itime
= tv
.tv_sec
;
4156 if ((hfsmp
->hfs_flags
& HFS_STANDARD
) && gTimeZone
.tz_dsttime
) {
4157 attr
.ca_itime
+= 3600; /* Same as what hfs_update does */
4159 attr
.ca_atime
= attr
.ca_ctime
= attr
.ca_mtime
= attr
.ca_itime
;
4160 attr
.ca_atimeondisk
= attr
.ca_atime
;
4161 if (VATTR_IS_ACTIVE(vap
, va_flags
)) {
4162 VATTR_SET_SUPPORTED(vap
, va_flags
);
4163 attr
.ca_flags
= vap
->va_flags
;
4167 * HFS+ only: all files get ThreadExists
4168 * HFSX only: dirs get HasFolderCount
4170 if (!(hfsmp
->hfs_flags
& HFS_STANDARD
)) {
4171 if (vnodetype
== VDIR
) {
4172 if (hfsmp
->hfs_flags
& HFS_FOLDERCOUNT
)
4173 attr
.ca_recflags
= kHFSHasFolderCountMask
;
4175 attr
.ca_recflags
= kHFSThreadExistsMask
;
4179 attr
.ca_uid
= vap
->va_uid
;
4180 attr
.ca_gid
= vap
->va_gid
;
4181 VATTR_SET_SUPPORTED(vap
, va_mode
);
4182 VATTR_SET_SUPPORTED(vap
, va_uid
);
4183 VATTR_SET_SUPPORTED(vap
, va_gid
);
4186 /* check to see if this node's creation would cause us to go over
4187 * quota. If so, abort this operation.
4189 if (hfsmp
->hfs_flags
& HFS_QUOTAS
) {
4190 if ((error
= hfs_quotacheck(hfsmp
, 1, attr
.ca_uid
, attr
.ca_gid
,
4191 vfs_context_ucred(ctx
)))) {
4198 /* Tag symlinks with a type and creator. */
4199 if (vnodetype
== VLNK
) {
4200 struct FndrFileInfo
*fip
;
4202 fip
= (struct FndrFileInfo
*)&attr
.ca_finderinfo
;
4203 fip
->fdType
= SWAP_BE32(kSymLinkFileType
);
4204 fip
->fdCreator
= SWAP_BE32(kSymLinkCreator
);
4206 if (cnp
->cn_flags
& ISWHITEOUT
)
4207 attr
.ca_flags
|= UF_OPAQUE
;
4209 /* Setup the descriptor */
4210 in_desc
.cd_nameptr
= (const u_int8_t
*)cnp
->cn_nameptr
;
4211 in_desc
.cd_namelen
= cnp
->cn_namelen
;
4212 in_desc
.cd_parentcnid
= dcp
->c_fileid
;
4213 in_desc
.cd_flags
= S_ISDIR(mode
) ? CD_ISDIR
: 0;
4214 in_desc
.cd_hint
= dcp
->c_childhint
;
4215 in_desc
.cd_encoding
= 0;
4217 if ((error
= hfs_start_transaction(hfsmp
)) != 0) {
4222 // have to also lock the attribute file because cat_create() needs
4223 // to check that any fileID it wants to use does not have orphaned
4224 // attributes in it.
4225 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
| SFL_ATTRIBUTE
, HFS_EXCLUSIVE_LOCK
);
4227 /* Reserve some space in the Catalog file. */
4228 if ((error
= cat_preflight(hfsmp
, CAT_CREATE
, NULL
, 0))) {
4229 hfs_systemfile_unlock(hfsmp
, lockflags
);
4232 error
= cat_create(hfsmp
, &in_desc
, &attr
, &out_desc
);
4234 /* Update the parent directory */
4235 dcp
->c_childhint
= out_desc
.cd_hint
; /* Cache directory's location */
4237 if (vnodetype
== VDIR
) {
4238 INC_FOLDERCOUNT(hfsmp
, dcp
->c_attr
);
4240 dcp
->c_dirchangecnt
++;
4241 dcp
->c_ctime
= tv
.tv_sec
;
4242 dcp
->c_mtime
= tv
.tv_sec
;
4243 (void) cat_update(hfsmp
, &dcp
->c_desc
, &dcp
->c_attr
, NULL
, NULL
);
4245 hfs_systemfile_unlock(hfsmp
, lockflags
);
4249 /* Invalidate negative cache entries in the directory */
4250 if (dcp
->c_flag
& C_NEG_ENTRIES
) {
4251 cache_purge_negatives(dvp
);
4252 dcp
->c_flag
&= ~C_NEG_ENTRIES
;
4255 hfs_volupdate(hfsmp
, vnodetype
== VDIR
? VOL_MKDIR
: VOL_MKFILE
,
4256 (dcp
->c_cnid
== kHFSRootFolderID
));
4259 // have to end the transaction here before we call hfs_getnewvnode()
4260 // because that can cause us to try and reclaim a vnode on a different
4261 // file system which could cause us to start a transaction which can
4262 // deadlock with someone on that other file system (since we could be
4263 // holding two transaction locks as well as various vnodes and we did
4264 // not obtain the locks on them in the proper order).
4266 // NOTE: this means that if the quota check fails or we have to update
4267 // the change time on a block-special device that those changes
4268 // will happen as part of independent transactions.
4271 hfs_end_transaction(hfsmp
);
4275 /* Do not create vnode for whiteouts */
4276 if (S_ISWHT(mode
)) {
4281 * Create a vnode for the object just created.
4283 * NOTE: Maintaining the cnode lock on the parent directory is important,
4284 * as it prevents race conditions where other threads want to look up entries
4285 * in the directory and/or add things as we are in the process of creating
4286 * the vnode below. However, this has the potential for causing a
4287 * double lock panic when dealing with shadow files on a HFS boot partition.
4288 * The panic could occur if we are not cleaning up after ourselves properly
4289 * when done with a shadow file or in the error cases. The error would occur if we
4290 * try to create a new vnode, and then end up reclaiming another shadow vnode to
4291 * create the new one. However, if everything is working properly, this should
4292 * be a non-issue as we would never enter that reclaim codepath.
4294 * The cnode is locked on successful return.
4296 error
= hfs_getnewvnode(hfsmp
, dvp
, cnp
, &out_desc
, GNV_CREATE
, &attr
, NULL
, &tvp
);
4303 cat_releasedesc(&out_desc
);
4306 * Make sure we release cnode lock on dcp.
4309 dcp
->c_flag
&= ~C_DIR_MODIFICATION
;
4310 wakeup((caddr_t
)&dcp
->c_flag
);
4314 if (error
== 0 && cp
!= NULL
) {
4318 hfs_end_transaction(hfsmp
);
4327 /* hfs_vgetrsrc acquires a resource fork vnode corresponding to the cnode that is
4328 * found in 'vp'. The rsrc fork vnode is returned with the cnode locked and iocount
4329 * on the rsrc vnode.
4331 * *rvpp is an output argument for returning the pointer to the resource fork vnode.
4332 * In most cases, the resource fork vnode will not be set if we return an error.
4333 * However, if error_on_unlinked is set, we may have already acquired the resource fork vnode
4334 * before we discover the error (the file has gone open-unlinked). In this case only,
4335 * we may return a vnode in the output argument despite an error.
4337 * If can_drop_lock is set, then it is safe for this function to temporarily drop
4338 * and then re-acquire the cnode lock. We may need to do this, for example, in order to
4339 * acquire an iocount or promote our lock.
4341 * error_on_unlinked is an argument which indicates that we are to return an error if we
4342 * discover that the cnode has gone into an open-unlinked state ( C_DELETED or C_NOEXISTS)
4343 * is set in the cnode flags. This is only necessary if can_drop_lock is true, otherwise
4344 * there's really no reason to double-check for errors on the cnode.
4349 hfs_vgetrsrc(struct hfsmount
*hfsmp
, struct vnode
*vp
,
4350 struct vnode
**rvpp
, int can_drop_lock
, int error_on_unlinked
)
4353 struct vnode
*dvp
= NULLVP
;
4354 struct cnode
*cp
= VTOC(vp
);
4357 int delete_status
= 0;
4361 * Need to check the status of the cnode to validate it hasn't
4362 * gone open-unlinked on us before we can actually do work with it.
4364 delete_status
= hfs_checkdeleted (cp
);
4365 if ((delete_status
) && (error_on_unlinked
)) {
4366 return delete_status
;
4370 /* Attempt to use exising vnode */
4371 if ((rvp
= cp
->c_rsrc_vp
)) {
4372 vid
= vnode_vid(rvp
);
4375 * It is not safe to hold the cnode lock when calling vnode_getwithvid()
4376 * for the alternate fork -- vnode_getwithvid() could deadlock waiting
4377 * for a VL_WANTTERM while another thread has an iocount on the alternate
4378 * fork vnode and is attempting to acquire the common cnode lock.
4380 * But it's also not safe to drop the cnode lock when we're holding
4381 * multiple cnode locks, like during a hfs_removefile() operation
4382 * since we could lock out of order when re-acquiring the cnode lock.
4384 * So we can only drop the lock here if its safe to drop it -- which is
4385 * most of the time with the exception being hfs_removefile().
4390 error
= vnode_getwithvid(rvp
, vid
);
4392 if (can_drop_lock
) {
4393 (void) hfs_lock(cp
, HFS_FORCE_LOCK
);
4396 * When we relinquished our cnode lock, the cnode could have raced
4397 * with a delete and gotten deleted. If the caller did not want
4398 * us to ignore open-unlinked files, then re-check the C_DELETED
4399 * state and see if we need to return an ENOENT here because the item
4400 * got deleted in the intervening time.
4402 if (error_on_unlinked
) {
4403 if ((delete_status
= hfs_checkdeleted(cp
))) {
4405 * If error == 0, this means that we succeeded in acquiring an iocount on the
4406 * rsrc fork vnode. However, if we're in this block of code, that
4407 * means that we noticed that the cnode has gone open-unlinked. In
4408 * this case, the caller requested that we not do any other work and
4409 * return an errno. The caller will be responsible for dropping the
4410 * iocount we just acquired because we can't do it until we've released
4416 return delete_status
;
4421 * When our lock was relinquished, the resource fork
4422 * could have been recycled. Check for this and try
4425 if (error
== ENOENT
)
4429 const char * name
= (const char *)VTOC(vp
)->c_desc
.cd_nameptr
;
4432 printf("hfs_vgetrsrc: couldn't get resource"
4433 " fork for %s, err %d\n", name
, error
);
4437 struct cat_fork rsrcfork
;
4438 struct componentname cn
;
4439 struct cat_desc
*descptr
= NULL
;
4440 struct cat_desc to_desc
;
4445 * Make sure cnode lock is exclusive, if not upgrade it.
4447 * We assume that we were called from a read-only VNOP (getattr)
4448 * and that its safe to have the cnode lock dropped and reacquired.
4450 if (cp
->c_lockowner
!= current_thread()) {
4451 if (!can_drop_lock
) {
4455 * If the upgrade fails we lose the lock and
4456 * have to take the exclusive lock on our own.
4458 if (lck_rw_lock_shared_to_exclusive(&cp
->c_rwlock
) == FALSE
)
4459 lck_rw_lock_exclusive(&cp
->c_rwlock
);
4460 cp
->c_lockowner
= current_thread();
4464 * hfs_vgetsrc may be invoked for a cnode that has already been marked
4465 * C_DELETED. This is because we need to continue to provide rsrc
4466 * fork access to open-unlinked files. In this case, build a fake descriptor
4467 * like in hfs_removefile. If we don't do this, buildkey will fail in
4468 * cat_lookup because this cnode has no name in its descriptor. However,
4469 * only do this if the caller did not specify that they wanted us to
4470 * error out upon encountering open-unlinked files.
4473 if ((error_on_unlinked
) && (can_drop_lock
)) {
4474 if ((error
= hfs_checkdeleted (cp
))) {
4479 if ((cp
->c_flag
& C_DELETED
) && (cp
->c_desc
.cd_namelen
== 0)) {
4480 bzero (&to_desc
, sizeof(to_desc
));
4481 bzero (delname
, 32);
4482 MAKE_DELETED_NAME(delname
, sizeof(delname
), cp
->c_fileid
);
4483 to_desc
.cd_nameptr
= (const u_int8_t
*) delname
;
4484 to_desc
.cd_namelen
= strlen(delname
);
4485 to_desc
.cd_parentcnid
= hfsmp
->hfs_private_desc
[FILE_HARDLINKS
].cd_cnid
;
4486 to_desc
.cd_flags
= 0;
4487 to_desc
.cd_cnid
= cp
->c_cnid
;
4492 descptr
= &cp
->c_desc
;
4496 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
4498 /* Get resource fork data */
4499 error
= cat_lookup(hfsmp
, descptr
, 1, (struct cat_desc
*)0,
4500 (struct cat_attr
*)0, &rsrcfork
, NULL
);
4502 hfs_systemfile_unlock(hfsmp
, lockflags
);
4507 * Supply hfs_getnewvnode with a component name.
4510 if (descptr
->cd_nameptr
) {
4511 MALLOC_ZONE(cn
.cn_pnbuf
, caddr_t
, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
4512 cn
.cn_nameiop
= LOOKUP
;
4513 cn
.cn_flags
= ISLASTCN
| HASBUF
;
4514 cn
.cn_context
= NULL
;
4515 cn
.cn_pnlen
= MAXPATHLEN
;
4516 cn
.cn_nameptr
= cn
.cn_pnbuf
;
4519 cn
.cn_namelen
= snprintf(cn
.cn_nameptr
, MAXPATHLEN
,
4520 "%s%s", descptr
->cd_nameptr
,
4521 _PATH_RSRCFORKSPEC
);
4523 dvp
= vnode_getparent(vp
);
4524 error
= hfs_getnewvnode(hfsmp
, dvp
, cn
.cn_pnbuf
? &cn
: NULL
,
4525 descptr
, GNV_WANTRSRC
| GNV_SKIPLOCK
, &cp
->c_attr
,
4530 FREE_ZONE(cn
.cn_pnbuf
, cn
.cn_pnlen
, M_NAMEI
);
4540 * Wrapper for special device reads
4544 struct vnop_read_args
/* {
4548 vfs_context_t a_context;
4554 VTOC(ap
->a_vp
)->c_touch_acctime
= TRUE
;
4555 return (VOCALL (spec_vnodeop_p
, VOFFSET(vnop_read
), ap
));
4559 * Wrapper for special device writes
4563 struct vnop_write_args
/* {
4567 vfs_context_t a_context;
4571 * Set update and change flags.
4573 VTOC(ap
->a_vp
)->c_touch_chgtime
= TRUE
;
4574 VTOC(ap
->a_vp
)->c_touch_modtime
= TRUE
;
4575 return (VOCALL (spec_vnodeop_p
, VOFFSET(vnop_write
), ap
));
4579 * Wrapper for special device close
4581 * Update the times on the cnode then do device close.
4585 struct vnop_close_args
/* {
4588 vfs_context_t a_context;
4591 struct vnode
*vp
= ap
->a_vp
;
4594 if (vnode_isinuse(ap
->a_vp
, 0)) {
4595 if (hfs_lock(VTOC(vp
), HFS_EXCLUSIVE_LOCK
) == 0) {
4597 hfs_touchtimes(VTOHFS(vp
), cp
);
4601 return (VOCALL (spec_vnodeop_p
, VOFFSET(vnop_close
), ap
));
4606 * Wrapper for fifo reads
4610 struct vnop_read_args
/* {
4614 vfs_context_t a_context;
4620 VTOC(ap
->a_vp
)->c_touch_acctime
= TRUE
;
4621 return (VOCALL (fifo_vnodeop_p
, VOFFSET(vnop_read
), ap
));
4625 * Wrapper for fifo writes
4629 struct vnop_write_args
/* {
4633 vfs_context_t a_context;
4637 * Set update and change flags.
4639 VTOC(ap
->a_vp
)->c_touch_chgtime
= TRUE
;
4640 VTOC(ap
->a_vp
)->c_touch_modtime
= TRUE
;
4641 return (VOCALL (fifo_vnodeop_p
, VOFFSET(vnop_write
), ap
));
4645 * Wrapper for fifo close
4647 * Update the times on the cnode then do device close.
4651 struct vnop_close_args
/* {
4654 vfs_context_t a_context;
4657 struct vnode
*vp
= ap
->a_vp
;
4660 if (vnode_isinuse(ap
->a_vp
, 1)) {
4661 if (hfs_lock(VTOC(vp
), HFS_EXCLUSIVE_LOCK
) == 0) {
4663 hfs_touchtimes(VTOHFS(vp
), cp
);
4667 return (VOCALL (fifo_vnodeop_p
, VOFFSET(vnop_close
), ap
));
4674 * Synchronize a file's in-core state with that on disk.
4678 struct vnop_fsync_args
/* {
4681 vfs_context_t a_context;
4684 struct vnode
* vp
= ap
->a_vp
;
4688 * We need to allow ENOENT lock errors since unlink
4689 * systenm call can call VNOP_FSYNC during vclean.
4691 error
= hfs_lock(VTOC(vp
), HFS_EXCLUSIVE_LOCK
);
4695 error
= hfs_fsync(vp
, ap
->a_waitfor
, 0, vfs_context_proc(ap
->a_context
));
4697 hfs_unlock(VTOC(vp
));
4703 hfs_vnop_whiteout(ap
)
4704 struct vnop_whiteout_args
/* {
4705 struct vnode *a_dvp;
4706 struct componentname *a_cnp;
4708 vfs_context_t a_context;
4712 struct vnode
*vp
= NULL
;
4713 struct vnode_attr va
;
4714 struct vnop_lookup_args lookup_args
;
4715 struct vnop_remove_args remove_args
;
4716 struct hfsmount
*hfsmp
;
4718 hfsmp
= VTOHFS(ap
->a_dvp
);
4719 if (hfsmp
->hfs_flags
& HFS_STANDARD
) {
4724 switch (ap
->a_flags
) {
4731 VATTR_SET(&va
, va_type
, VREG
);
4732 VATTR_SET(&va
, va_mode
, S_IFWHT
);
4733 VATTR_SET(&va
, va_uid
, 0);
4734 VATTR_SET(&va
, va_gid
, 0);
4736 error
= hfs_makenode(ap
->a_dvp
, &vp
, ap
->a_cnp
, &va
, ap
->a_context
);
4737 /* No need to release the vnode as no vnode is created for whiteouts */
4741 lookup_args
.a_dvp
= ap
->a_dvp
;
4742 lookup_args
.a_vpp
= &vp
;
4743 lookup_args
.a_cnp
= ap
->a_cnp
;
4744 lookup_args
.a_context
= ap
->a_context
;
4746 error
= hfs_vnop_lookup(&lookup_args
);
4751 remove_args
.a_dvp
= ap
->a_dvp
;
4752 remove_args
.a_vp
= vp
;
4753 remove_args
.a_cnp
= ap
->a_cnp
;
4754 remove_args
.a_flags
= 0;
4755 remove_args
.a_context
= ap
->a_context
;
4757 error
= hfs_vnop_remove(&remove_args
);
4762 panic("hfs_vnop_whiteout: unknown operation (flag = %x)\n", ap
->a_flags
);
4769 int (**hfs_vnodeop_p
)(void *);
4770 int (**hfs_std_vnodeop_p
) (void *);
4772 #define VOPFUNC int (*)(void *)
4774 static int hfs_readonly_op (__unused
void* ap
) { return (EROFS
); }
4777 * In 10.6 and forward, HFS Standard is read-only and deprecated. The vnop table below
4778 * is for use with HFS standard to block out operations that would modify the file system
4781 struct vnodeopv_entry_desc hfs_standard_vnodeop_entries
[] = {
4782 { &vnop_default_desc
, (VOPFUNC
)vn_default_error
},
4783 { &vnop_lookup_desc
, (VOPFUNC
)hfs_vnop_lookup
}, /* lookup */
4784 { &vnop_create_desc
, (VOPFUNC
)hfs_readonly_op
}, /* create (READONLY) */
4785 { &vnop_mknod_desc
, (VOPFUNC
)hfs_readonly_op
}, /* mknod (READONLY) */
4786 { &vnop_open_desc
, (VOPFUNC
)hfs_vnop_open
}, /* open */
4787 { &vnop_close_desc
, (VOPFUNC
)hfs_vnop_close
}, /* close */
4788 { &vnop_getattr_desc
, (VOPFUNC
)hfs_vnop_getattr
}, /* getattr */
4789 { &vnop_setattr_desc
, (VOPFUNC
)hfs_readonly_op
}, /* setattr */
4790 { &vnop_read_desc
, (VOPFUNC
)hfs_vnop_read
}, /* read */
4791 { &vnop_write_desc
, (VOPFUNC
)hfs_readonly_op
}, /* write (READONLY) */
4792 { &vnop_ioctl_desc
, (VOPFUNC
)hfs_vnop_ioctl
}, /* ioctl */
4793 { &vnop_select_desc
, (VOPFUNC
)hfs_vnop_select
}, /* select */
4794 { &vnop_revoke_desc
, (VOPFUNC
)nop_revoke
}, /* revoke */
4795 { &vnop_exchange_desc
, (VOPFUNC
)hfs_readonly_op
}, /* exchange (READONLY)*/
4796 { &vnop_mmap_desc
, (VOPFUNC
)err_mmap
}, /* mmap */
4797 { &vnop_fsync_desc
, (VOPFUNC
)hfs_readonly_op
}, /* fsync (READONLY) */
4798 { &vnop_remove_desc
, (VOPFUNC
)hfs_readonly_op
}, /* remove (READONLY) */
4799 { &vnop_link_desc
, (VOPFUNC
)hfs_readonly_op
}, /* link ( READONLLY) */
4800 { &vnop_rename_desc
, (VOPFUNC
)hfs_readonly_op
}, /* rename (READONLY)*/
4801 { &vnop_mkdir_desc
, (VOPFUNC
)hfs_readonly_op
}, /* mkdir (READONLY) */
4802 { &vnop_rmdir_desc
, (VOPFUNC
)hfs_readonly_op
}, /* rmdir (READONLY) */
4803 { &vnop_symlink_desc
, (VOPFUNC
)hfs_readonly_op
}, /* symlink (READONLY) */
4804 { &vnop_readdir_desc
, (VOPFUNC
)hfs_vnop_readdir
}, /* readdir */
4805 { &vnop_readdirattr_desc
, (VOPFUNC
)hfs_vnop_readdirattr
}, /* readdirattr */
4806 { &vnop_readlink_desc
, (VOPFUNC
)hfs_vnop_readlink
}, /* readlink */
4807 { &vnop_inactive_desc
, (VOPFUNC
)hfs_vnop_inactive
}, /* inactive */
4808 { &vnop_reclaim_desc
, (VOPFUNC
)hfs_vnop_reclaim
}, /* reclaim */
4809 { &vnop_strategy_desc
, (VOPFUNC
)hfs_vnop_strategy
}, /* strategy */
4810 { &vnop_pathconf_desc
, (VOPFUNC
)hfs_vnop_pathconf
}, /* pathconf */
4811 { &vnop_advlock_desc
, (VOPFUNC
)err_advlock
}, /* advlock */
4812 { &vnop_allocate_desc
, (VOPFUNC
)hfs_readonly_op
}, /* allocate (READONLY) */
4813 { &vnop_searchfs_desc
, (VOPFUNC
)hfs_vnop_search
}, /* search fs */
4814 { &vnop_bwrite_desc
, (VOPFUNC
)hfs_readonly_op
}, /* bwrite (READONLY) */
4815 { &vnop_pagein_desc
, (VOPFUNC
)hfs_vnop_pagein
}, /* pagein */
4816 { &vnop_pageout_desc
,(VOPFUNC
) hfs_readonly_op
}, /* pageout (READONLY) */
4817 { &vnop_copyfile_desc
, (VOPFUNC
)hfs_readonly_op
}, /* copyfile (READONLY)*/
4818 { &vnop_blktooff_desc
, (VOPFUNC
)hfs_vnop_blktooff
}, /* blktooff */
4819 { &vnop_offtoblk_desc
, (VOPFUNC
)hfs_vnop_offtoblk
}, /* offtoblk */
4820 { &vnop_blockmap_desc
, (VOPFUNC
)hfs_vnop_blockmap
}, /* blockmap */
4821 { &vnop_getxattr_desc
, (VOPFUNC
)hfs_vnop_getxattr
},
4822 { &vnop_setxattr_desc
, (VOPFUNC
)hfs_readonly_op
}, /* set xattr (READONLY) */
4823 { &vnop_removexattr_desc
, (VOPFUNC
)hfs_readonly_op
}, /* remove xattr (READONLY) */
4824 { &vnop_listxattr_desc
, (VOPFUNC
)hfs_vnop_listxattr
},
4825 { &vnop_whiteout_desc
, (VOPFUNC
)hfs_readonly_op
}, /* whiteout (READONLY) */
4827 { &vnop_getnamedstream_desc
, (VOPFUNC
)hfs_vnop_getnamedstream
},
4828 { &vnop_makenamedstream_desc
, (VOPFUNC
)hfs_readonly_op
},
4829 { &vnop_removenamedstream_desc
, (VOPFUNC
)hfs_readonly_op
},
4831 { NULL
, (VOPFUNC
)NULL
}
4834 struct vnodeopv_desc hfs_std_vnodeop_opv_desc
=
4835 { &hfs_std_vnodeop_p
, hfs_standard_vnodeop_entries
};
4838 /* VNOP table for HFS+ */
4839 struct vnodeopv_entry_desc hfs_vnodeop_entries
[] = {
4840 { &vnop_default_desc
, (VOPFUNC
)vn_default_error
},
4841 { &vnop_lookup_desc
, (VOPFUNC
)hfs_vnop_lookup
}, /* lookup */
4842 { &vnop_create_desc
, (VOPFUNC
)hfs_vnop_create
}, /* create */
4843 { &vnop_mknod_desc
, (VOPFUNC
)hfs_vnop_mknod
}, /* mknod */
4844 { &vnop_open_desc
, (VOPFUNC
)hfs_vnop_open
}, /* open */
4845 { &vnop_close_desc
, (VOPFUNC
)hfs_vnop_close
}, /* close */
4846 { &vnop_getattr_desc
, (VOPFUNC
)hfs_vnop_getattr
}, /* getattr */
4847 { &vnop_setattr_desc
, (VOPFUNC
)hfs_vnop_setattr
}, /* setattr */
4848 { &vnop_read_desc
, (VOPFUNC
)hfs_vnop_read
}, /* read */
4849 { &vnop_write_desc
, (VOPFUNC
)hfs_vnop_write
}, /* write */
4850 { &vnop_ioctl_desc
, (VOPFUNC
)hfs_vnop_ioctl
}, /* ioctl */
4851 { &vnop_select_desc
, (VOPFUNC
)hfs_vnop_select
}, /* select */
4852 { &vnop_revoke_desc
, (VOPFUNC
)nop_revoke
}, /* revoke */
4853 { &vnop_exchange_desc
, (VOPFUNC
)hfs_vnop_exchange
}, /* exchange */
4854 { &vnop_mmap_desc
, (VOPFUNC
)err_mmap
}, /* mmap */
4855 { &vnop_fsync_desc
, (VOPFUNC
)hfs_vnop_fsync
}, /* fsync */
4856 { &vnop_remove_desc
, (VOPFUNC
)hfs_vnop_remove
}, /* remove */
4857 { &vnop_link_desc
, (VOPFUNC
)hfs_vnop_link
}, /* link */
4858 { &vnop_rename_desc
, (VOPFUNC
)hfs_vnop_rename
}, /* rename */
4859 { &vnop_mkdir_desc
, (VOPFUNC
)hfs_vnop_mkdir
}, /* mkdir */
4860 { &vnop_rmdir_desc
, (VOPFUNC
)hfs_vnop_rmdir
}, /* rmdir */
4861 { &vnop_symlink_desc
, (VOPFUNC
)hfs_vnop_symlink
}, /* symlink */
4862 { &vnop_readdir_desc
, (VOPFUNC
)hfs_vnop_readdir
}, /* readdir */
4863 { &vnop_readdirattr_desc
, (VOPFUNC
)hfs_vnop_readdirattr
}, /* readdirattr */
4864 { &vnop_readlink_desc
, (VOPFUNC
)hfs_vnop_readlink
}, /* readlink */
4865 { &vnop_inactive_desc
, (VOPFUNC
)hfs_vnop_inactive
}, /* inactive */
4866 { &vnop_reclaim_desc
, (VOPFUNC
)hfs_vnop_reclaim
}, /* reclaim */
4867 { &vnop_strategy_desc
, (VOPFUNC
)hfs_vnop_strategy
}, /* strategy */
4868 { &vnop_pathconf_desc
, (VOPFUNC
)hfs_vnop_pathconf
}, /* pathconf */
4869 { &vnop_advlock_desc
, (VOPFUNC
)err_advlock
}, /* advlock */
4870 { &vnop_allocate_desc
, (VOPFUNC
)hfs_vnop_allocate
}, /* allocate */
4871 { &vnop_searchfs_desc
, (VOPFUNC
)hfs_vnop_search
}, /* search fs */
4872 { &vnop_bwrite_desc
, (VOPFUNC
)hfs_vnop_bwrite
}, /* bwrite */
4873 { &vnop_pagein_desc
, (VOPFUNC
)hfs_vnop_pagein
}, /* pagein */
4874 { &vnop_pageout_desc
,(VOPFUNC
) hfs_vnop_pageout
}, /* pageout */
4875 { &vnop_copyfile_desc
, (VOPFUNC
)err_copyfile
}, /* copyfile */
4876 { &vnop_blktooff_desc
, (VOPFUNC
)hfs_vnop_blktooff
}, /* blktooff */
4877 { &vnop_offtoblk_desc
, (VOPFUNC
)hfs_vnop_offtoblk
}, /* offtoblk */
4878 { &vnop_blockmap_desc
, (VOPFUNC
)hfs_vnop_blockmap
}, /* blockmap */
4879 { &vnop_getxattr_desc
, (VOPFUNC
)hfs_vnop_getxattr
},
4880 { &vnop_setxattr_desc
, (VOPFUNC
)hfs_vnop_setxattr
},
4881 { &vnop_removexattr_desc
, (VOPFUNC
)hfs_vnop_removexattr
},
4882 { &vnop_listxattr_desc
, (VOPFUNC
)hfs_vnop_listxattr
},
4883 { &vnop_whiteout_desc
, (VOPFUNC
)hfs_vnop_whiteout
},
4885 { &vnop_getnamedstream_desc
, (VOPFUNC
)hfs_vnop_getnamedstream
},
4886 { &vnop_makenamedstream_desc
, (VOPFUNC
)hfs_vnop_makenamedstream
},
4887 { &vnop_removenamedstream_desc
, (VOPFUNC
)hfs_vnop_removenamedstream
},
4889 { NULL
, (VOPFUNC
)NULL
}
4892 struct vnodeopv_desc hfs_vnodeop_opv_desc
=
4893 { &hfs_vnodeop_p
, hfs_vnodeop_entries
};
4896 /* Spec Op vnop table for HFS+ */
4897 int (**hfs_specop_p
)(void *);
4898 struct vnodeopv_entry_desc hfs_specop_entries
[] = {
4899 { &vnop_default_desc
, (VOPFUNC
)vn_default_error
},
4900 { &vnop_lookup_desc
, (VOPFUNC
)spec_lookup
}, /* lookup */
4901 { &vnop_create_desc
, (VOPFUNC
)spec_create
}, /* create */
4902 { &vnop_mknod_desc
, (VOPFUNC
)spec_mknod
}, /* mknod */
4903 { &vnop_open_desc
, (VOPFUNC
)spec_open
}, /* open */
4904 { &vnop_close_desc
, (VOPFUNC
)hfsspec_close
}, /* close */
4905 { &vnop_getattr_desc
, (VOPFUNC
)hfs_vnop_getattr
}, /* getattr */
4906 { &vnop_setattr_desc
, (VOPFUNC
)hfs_vnop_setattr
}, /* setattr */
4907 { &vnop_read_desc
, (VOPFUNC
)hfsspec_read
}, /* read */
4908 { &vnop_write_desc
, (VOPFUNC
)hfsspec_write
}, /* write */
4909 { &vnop_ioctl_desc
, (VOPFUNC
)spec_ioctl
}, /* ioctl */
4910 { &vnop_select_desc
, (VOPFUNC
)spec_select
}, /* select */
4911 { &vnop_revoke_desc
, (VOPFUNC
)spec_revoke
}, /* revoke */
4912 { &vnop_mmap_desc
, (VOPFUNC
)spec_mmap
}, /* mmap */
4913 { &vnop_fsync_desc
, (VOPFUNC
)hfs_vnop_fsync
}, /* fsync */
4914 { &vnop_remove_desc
, (VOPFUNC
)spec_remove
}, /* remove */
4915 { &vnop_link_desc
, (VOPFUNC
)spec_link
}, /* link */
4916 { &vnop_rename_desc
, (VOPFUNC
)spec_rename
}, /* rename */
4917 { &vnop_mkdir_desc
, (VOPFUNC
)spec_mkdir
}, /* mkdir */
4918 { &vnop_rmdir_desc
, (VOPFUNC
)spec_rmdir
}, /* rmdir */
4919 { &vnop_symlink_desc
, (VOPFUNC
)spec_symlink
}, /* symlink */
4920 { &vnop_readdir_desc
, (VOPFUNC
)spec_readdir
}, /* readdir */
4921 { &vnop_readlink_desc
, (VOPFUNC
)spec_readlink
}, /* readlink */
4922 { &vnop_inactive_desc
, (VOPFUNC
)hfs_vnop_inactive
}, /* inactive */
4923 { &vnop_reclaim_desc
, (VOPFUNC
)hfs_vnop_reclaim
}, /* reclaim */
4924 { &vnop_strategy_desc
, (VOPFUNC
)spec_strategy
}, /* strategy */
4925 { &vnop_pathconf_desc
, (VOPFUNC
)spec_pathconf
}, /* pathconf */
4926 { &vnop_advlock_desc
, (VOPFUNC
)err_advlock
}, /* advlock */
4927 { &vnop_bwrite_desc
, (VOPFUNC
)hfs_vnop_bwrite
},
4928 { &vnop_pagein_desc
, (VOPFUNC
)hfs_vnop_pagein
}, /* Pagein */
4929 { &vnop_pageout_desc
, (VOPFUNC
)hfs_vnop_pageout
}, /* Pageout */
4930 { &vnop_copyfile_desc
, (VOPFUNC
)err_copyfile
}, /* copyfile */
4931 { &vnop_blktooff_desc
, (VOPFUNC
)hfs_vnop_blktooff
}, /* blktooff */
4932 { &vnop_offtoblk_desc
, (VOPFUNC
)hfs_vnop_offtoblk
}, /* offtoblk */
4933 { (struct vnodeop_desc
*)NULL
, (VOPFUNC
)NULL
}
4935 struct vnodeopv_desc hfs_specop_opv_desc
=
4936 { &hfs_specop_p
, hfs_specop_entries
};
4939 /* HFS+ FIFO VNOP table */
4940 int (**hfs_fifoop_p
)(void *);
4941 struct vnodeopv_entry_desc hfs_fifoop_entries
[] = {
4942 { &vnop_default_desc
, (VOPFUNC
)vn_default_error
},
4943 { &vnop_lookup_desc
, (VOPFUNC
)fifo_lookup
}, /* lookup */
4944 { &vnop_create_desc
, (VOPFUNC
)fifo_create
}, /* create */
4945 { &vnop_mknod_desc
, (VOPFUNC
)fifo_mknod
}, /* mknod */
4946 { &vnop_open_desc
, (VOPFUNC
)fifo_open
}, /* open */
4947 { &vnop_close_desc
, (VOPFUNC
)hfsfifo_close
}, /* close */
4948 { &vnop_getattr_desc
, (VOPFUNC
)hfs_vnop_getattr
}, /* getattr */
4949 { &vnop_setattr_desc
, (VOPFUNC
)hfs_vnop_setattr
}, /* setattr */
4950 { &vnop_read_desc
, (VOPFUNC
)hfsfifo_read
}, /* read */
4951 { &vnop_write_desc
, (VOPFUNC
)hfsfifo_write
}, /* write */
4952 { &vnop_ioctl_desc
, (VOPFUNC
)fifo_ioctl
}, /* ioctl */
4953 { &vnop_select_desc
, (VOPFUNC
)fifo_select
}, /* select */
4954 { &vnop_revoke_desc
, (VOPFUNC
)fifo_revoke
}, /* revoke */
4955 { &vnop_mmap_desc
, (VOPFUNC
)fifo_mmap
}, /* mmap */
4956 { &vnop_fsync_desc
, (VOPFUNC
)hfs_vnop_fsync
}, /* fsync */
4957 { &vnop_remove_desc
, (VOPFUNC
)fifo_remove
}, /* remove */
4958 { &vnop_link_desc
, (VOPFUNC
)fifo_link
}, /* link */
4959 { &vnop_rename_desc
, (VOPFUNC
)fifo_rename
}, /* rename */
4960 { &vnop_mkdir_desc
, (VOPFUNC
)fifo_mkdir
}, /* mkdir */
4961 { &vnop_rmdir_desc
, (VOPFUNC
)fifo_rmdir
}, /* rmdir */
4962 { &vnop_symlink_desc
, (VOPFUNC
)fifo_symlink
}, /* symlink */
4963 { &vnop_readdir_desc
, (VOPFUNC
)fifo_readdir
}, /* readdir */
4964 { &vnop_readlink_desc
, (VOPFUNC
)fifo_readlink
}, /* readlink */
4965 { &vnop_inactive_desc
, (VOPFUNC
)hfs_vnop_inactive
}, /* inactive */
4966 { &vnop_reclaim_desc
, (VOPFUNC
)hfs_vnop_reclaim
}, /* reclaim */
4967 { &vnop_strategy_desc
, (VOPFUNC
)fifo_strategy
}, /* strategy */
4968 { &vnop_pathconf_desc
, (VOPFUNC
)fifo_pathconf
}, /* pathconf */
4969 { &vnop_advlock_desc
, (VOPFUNC
)err_advlock
}, /* advlock */
4970 { &vnop_bwrite_desc
, (VOPFUNC
)hfs_vnop_bwrite
},
4971 { &vnop_pagein_desc
, (VOPFUNC
)hfs_vnop_pagein
}, /* Pagein */
4972 { &vnop_pageout_desc
, (VOPFUNC
)hfs_vnop_pageout
}, /* Pageout */
4973 { &vnop_copyfile_desc
, (VOPFUNC
)err_copyfile
}, /* copyfile */
4974 { &vnop_blktooff_desc
, (VOPFUNC
)hfs_vnop_blktooff
}, /* blktooff */
4975 { &vnop_offtoblk_desc
, (VOPFUNC
)hfs_vnop_offtoblk
}, /* offtoblk */
4976 { &vnop_blockmap_desc
, (VOPFUNC
)hfs_vnop_blockmap
}, /* blockmap */
4977 { (struct vnodeop_desc
*)NULL
, (VOPFUNC
)NULL
}
4979 struct vnodeopv_desc hfs_fifoop_opv_desc
=
4980 { &hfs_fifoop_p
, hfs_fifoop_entries
};