2 * Copyright (c) 2000-2010 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>
48 #include <sys/fsctl.h>
49 #include <sys/cprotect.h>
53 #include <miscfs/specfs/specdev.h>
54 #include <miscfs/fifofs/fifo.h>
55 #include <vfs/vfs_support.h>
56 #include <machine/spl.h>
58 #include <sys/kdebug.h>
59 #include <sys/sysctl.h>
62 #include "hfs_catalog.h"
63 #include "hfs_cnode.h"
65 #include "hfs_mount.h"
66 #include "hfs_quota.h"
67 #include "hfs_endian.h"
69 #include "hfscommon/headers/BTreesInternal.h"
70 #include "hfscommon/headers/FileMgrInternal.h"
72 #define KNDETACH_VNLOCKED 0x00000001
74 /* Global vfs data structures for hfs */
76 /* Always F_FULLFSYNC? 1=yes,0=no (default due to "various" reasons is 'no') */
77 int always_do_fullfsync
= 0;
78 SYSCTL_DECL(_vfs_generic
);
79 SYSCTL_INT (_vfs_generic
, OID_AUTO
, always_do_fullfsync
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &always_do_fullfsync
, 0, "always F_FULLFSYNC when fsync is called");
81 int hfs_makenode(struct vnode
*dvp
, struct vnode
**vpp
,
82 struct componentname
*cnp
, struct vnode_attr
*vap
,
84 int hfs_metasync(struct hfsmount
*hfsmp
, daddr64_t node
, __unused
struct proc
*p
);
85 int hfs_metasync_all(struct hfsmount
*hfsmp
);
87 int hfs_removedir(struct vnode
*, struct vnode
*, struct componentname
*,
89 int hfs_removefile(struct vnode
*, struct vnode
*, struct componentname
*,
90 int, int, int, struct vnode
*, int);
92 int hfs_movedata (struct vnode
*, struct vnode
*);
93 static int hfs_move_fork (struct filefork
*srcfork
, struct cnode
*src
,
94 struct filefork
*dstfork
, struct cnode
*dst
);
98 static int hfsfifo_read(struct vnop_read_args
*);
99 static int hfsfifo_write(struct vnop_write_args
*);
100 static int hfsfifo_close(struct vnop_close_args
*);
102 extern int (**fifo_vnodeop_p
)(void *);
105 int hfs_vnop_close(struct vnop_close_args
*);
106 int hfs_vnop_create(struct vnop_create_args
*);
107 int hfs_vnop_exchange(struct vnop_exchange_args
*);
108 int hfs_vnop_fsync(struct vnop_fsync_args
*);
109 int hfs_vnop_mkdir(struct vnop_mkdir_args
*);
110 int hfs_vnop_mknod(struct vnop_mknod_args
*);
111 int hfs_vnop_getattr(struct vnop_getattr_args
*);
112 int hfs_vnop_open(struct vnop_open_args
*);
113 int hfs_vnop_readdir(struct vnop_readdir_args
*);
114 int hfs_vnop_remove(struct vnop_remove_args
*);
115 int hfs_vnop_rename(struct vnop_rename_args
*);
116 int hfs_vnop_rmdir(struct vnop_rmdir_args
*);
117 int hfs_vnop_symlink(struct vnop_symlink_args
*);
118 int hfs_vnop_setattr(struct vnop_setattr_args
*);
119 int hfs_vnop_readlink(struct vnop_readlink_args
*);
120 int hfs_vnop_pathconf(struct vnop_pathconf_args
*);
121 int hfs_vnop_whiteout(struct vnop_whiteout_args
*);
122 int hfs_vnop_mmap(struct vnop_mmap_args
*ap
);
123 int hfsspec_read(struct vnop_read_args
*);
124 int hfsspec_write(struct vnop_write_args
*);
125 int hfsspec_close(struct vnop_close_args
*);
127 /* Options for hfs_removedir and hfs_removefile */
128 #define HFSRM_SKIP_RESERVE 0x01
133 /*****************************************************************************
135 * Common Operations on vnodes
137 *****************************************************************************/
140 * Create a regular file.
143 hfs_vnop_create(struct vnop_create_args
*ap
)
148 error
= hfs_makenode(ap
->a_dvp
, ap
->a_vpp
, ap
->a_cnp
, ap
->a_vap
, ap
->a_context
);
151 * We speculatively skipped the original lookup of the leaf
152 * for CREATE. Since it exists, go get it as long as they
153 * didn't want an exclusive create.
155 if ((error
== EEXIST
) && !(ap
->a_vap
->va_vaflags
& VA_EXCLUSIVE
)) {
156 struct vnop_lookup_args args
;
158 args
.a_desc
= &vnop_lookup_desc
;
159 args
.a_dvp
= ap
->a_dvp
;
160 args
.a_vpp
= ap
->a_vpp
;
161 args
.a_cnp
= ap
->a_cnp
;
162 args
.a_context
= ap
->a_context
;
163 args
.a_cnp
->cn_nameiop
= LOOKUP
;
164 error
= hfs_vnop_lookup(&args
);
166 * We can also race with remove for this file.
168 if (error
== ENOENT
) {
172 /* Make sure it was file. */
173 if ((error
== 0) && !vnode_isreg(*args
.a_vpp
)) {
174 vnode_put(*args
.a_vpp
);
175 *args
.a_vpp
= NULLVP
;
178 args
.a_cnp
->cn_nameiop
= CREATE
;
184 * Make device special file.
187 hfs_vnop_mknod(struct vnop_mknod_args
*ap
)
189 struct vnode_attr
*vap
= ap
->a_vap
;
190 struct vnode
*dvp
= ap
->a_dvp
;
191 struct vnode
**vpp
= ap
->a_vpp
;
195 if (VTOVCB(dvp
)->vcbSigWord
!= kHFSPlusSigWord
) {
199 /* Create the vnode */
200 error
= hfs_makenode(dvp
, vpp
, ap
->a_cnp
, vap
, ap
->a_context
);
205 cp
->c_touch_acctime
= TRUE
;
206 cp
->c_touch_chgtime
= TRUE
;
207 cp
->c_touch_modtime
= TRUE
;
209 if ((vap
->va_rdev
!= VNOVAL
) &&
210 (vap
->va_type
== VBLK
|| vap
->va_type
== VCHR
))
211 cp
->c_rdev
= vap
->va_rdev
;
218 * hfs_ref_data_vp(): returns the data fork vnode for a given cnode.
219 * In the (hopefully rare) case where the data fork vnode is not
220 * present, it will use hfs_vget() to create a new vnode for the
223 * NOTE: If successful and a vnode is returned, the caller is responsible
224 * for releasing the returned vnode with vnode_rele().
227 hfs_ref_data_vp(struct cnode
*cp
, struct vnode
**data_vp
, int skiplock
)
231 if (!data_vp
|| !cp
) /* sanity check incoming parameters */
234 /* maybe we should take the hfs cnode lock here, and if so, use the skiplock parameter to tell us not to */
236 if (!skiplock
) hfs_lock(cp
, HFS_SHARED_LOCK
);
237 struct vnode
*c_vp
= cp
->c_vp
;
239 /* we already have a data vnode */
241 vref
= vnode_ref(*data_vp
);
242 if (!skiplock
) hfs_unlock(cp
);
248 /* no data fork vnode in the cnode, so ask hfs for one. */
250 if (!cp
->c_rsrc_vp
) {
251 /* if we don't have either a c_vp or c_rsrc_vp, we can't really do anything useful */
253 if (!skiplock
) hfs_unlock(cp
);
257 if (0 == hfs_vget(VTOHFS(cp
->c_rsrc_vp
), cp
->c_cnid
, data_vp
, 1, 0) &&
259 vref
= vnode_ref(*data_vp
);
261 if (!skiplock
) hfs_unlock(cp
);
267 /* there was an error getting the vnode */
269 if (!skiplock
) hfs_unlock(cp
);
274 * hfs_lazy_init_decmpfs_cnode(): returns the decmpfs_cnode for a cnode,
275 * allocating it if necessary; returns NULL if there was an allocation error
277 static decmpfs_cnode
*
278 hfs_lazy_init_decmpfs_cnode(struct cnode
*cp
)
281 decmpfs_cnode
*dp
= NULL
;
282 MALLOC_ZONE(dp
, decmpfs_cnode
*, sizeof(decmpfs_cnode
), M_DECMPFS_CNODE
, M_WAITOK
);
284 /* error allocating a decmpfs cnode */
287 decmpfs_cnode_init(dp
);
288 if (!OSCompareAndSwapPtr(NULL
, dp
, (void * volatile *)&cp
->c_decmp
)) {
289 /* another thread got here first, so free the decmpfs_cnode we allocated */
290 decmpfs_cnode_destroy(dp
);
291 FREE_ZONE(dp
, sizeof(*dp
), M_DECMPFS_CNODE
);
299 * hfs_file_is_compressed(): returns 1 if the file is compressed, and 0 (zero) if not.
300 * if the file's compressed flag is set, makes sure that the decmpfs_cnode field
301 * is allocated by calling hfs_lazy_init_decmpfs_cnode(), then makes sure it is populated,
302 * or else fills it in via the decmpfs_file_is_compressed() function.
305 hfs_file_is_compressed(struct cnode
*cp
, int skiplock
)
309 /* fast check to see if file is compressed. If flag is clear, just answer no */
310 if (!(cp
->c_flags
& UF_COMPRESSED
)) {
314 decmpfs_cnode
*dp
= hfs_lazy_init_decmpfs_cnode(cp
);
316 /* error allocating a decmpfs cnode, treat the file as uncompressed */
320 /* flag was set, see if the decmpfs_cnode state is valid (zero == invalid) */
321 uint32_t decmpfs_state
= decmpfs_cnode_get_vnode_state(dp
);
322 switch(decmpfs_state
) {
323 case FILE_IS_COMPRESSED
:
324 case FILE_IS_CONVERTING
: /* treat decompressing files as if they are compressed */
326 case FILE_IS_NOT_COMPRESSED
:
328 /* otherwise the state is not cached yet */
331 /* decmpfs hasn't seen this file yet, so call decmpfs_file_is_compressed() to init the decmpfs_cnode struct */
332 struct vnode
*data_vp
= NULL
;
333 if (0 == hfs_ref_data_vp(cp
, &data_vp
, skiplock
)) {
335 ret
= decmpfs_file_is_compressed(data_vp
, VTOCMP(data_vp
)); // fill in decmpfs_cnode
342 /* hfs_uncompressed_size_of_compressed_file() - get the uncompressed size of the file.
343 * if the caller has passed a valid vnode (has a ref count > 0), then hfsmp and fid are not required.
344 * if the caller doesn't have a vnode, pass NULL in vp, and pass valid hfsmp and fid.
345 * files size is returned in size (required)
346 * if the indicated file is a directory (or something that doesn't have a data fork), then this call
347 * will return an error and the caller should fall back to treating the item as an uncompressed file
350 hfs_uncompressed_size_of_compressed_file(struct hfsmount
*hfsmp
, struct vnode
*vp
, cnid_t fid
, off_t
*size
, int skiplock
)
353 int putaway
= 0; /* flag to remember if we used hfs_vget() */
356 return EINVAL
; /* no place to put the file size */
360 if (!hfsmp
|| !fid
) { /* make sure we have the required parameters */
363 if (0 != hfs_vget(hfsmp
, fid
, &vp
, skiplock
, 0)) { /* vnode is null, use hfs_vget() to get it */
366 putaway
= 1; /* note that hfs_vget() was used to aquire the vnode */
369 /* this double check for compression (hfs_file_is_compressed)
370 * ensures the cached size is present in case decmpfs hasn't
371 * encountered this node yet.
374 if (hfs_file_is_compressed(VTOC(vp
), skiplock
) ) {
375 *size
= decmpfs_cnode_get_vnode_cached_size(VTOCMP(vp
)); /* file info will be cached now, so get size */
377 if (VTOCMP(vp
) && VTOCMP(vp
)->cmp_type
>= CMP_MAX
) {
378 if (VTOCMP(vp
)->cmp_type
!= DATALESS_CMPFS_TYPE
) {
379 // if we don't recognize this type, just use the real data fork size
380 if (VTOC(vp
)->c_datafork
) {
381 *size
= VTOC(vp
)->c_datafork
->ff_size
;
387 *size
= decmpfs_cnode_get_vnode_cached_size(VTOCMP(vp
)); /* file info will be cached now, so get size */
396 if (putaway
) { /* did we use hfs_vget() to get this vnode? */
397 vnode_put(vp
); /* if so, release it and set it to null */
404 hfs_hides_rsrc(vfs_context_t ctx
, struct cnode
*cp
, int skiplock
)
406 if (ctx
== decmpfs_ctx
)
408 if (!hfs_file_is_compressed(cp
, skiplock
))
410 return decmpfs_hides_rsrc(ctx
, cp
->c_decmp
);
414 hfs_hides_xattr(vfs_context_t ctx
, struct cnode
*cp
, const char *name
, int skiplock
)
416 if (ctx
== decmpfs_ctx
)
418 if (!hfs_file_is_compressed(cp
, skiplock
))
420 return decmpfs_hides_xattr(ctx
, cp
->c_decmp
, name
);
422 #endif /* HFS_COMPRESSION */
425 * Open a file/directory.
428 hfs_vnop_open(struct vnop_open_args
*ap
)
430 struct vnode
*vp
= ap
->a_vp
;
434 static int past_bootup
= 0;
435 struct cnode
*cp
= VTOC(vp
);
436 struct hfsmount
*hfsmp
= VTOHFS(vp
);
439 if (ap
->a_mode
& FWRITE
) {
441 if ( hfs_file_is_compressed(cp
, 1) ) { /* 1 == don't take the cnode lock */
442 /* opening a compressed file for write, so convert it to decompressed */
443 struct vnode
*data_vp
= NULL
;
444 error
= hfs_ref_data_vp(cp
, &data_vp
, 1); /* 1 == don't take the cnode lock */
447 error
= decmpfs_decompress_file(data_vp
, VTOCMP(data_vp
), -1, 1, 0);
458 if (hfs_file_is_compressed(cp
, 1) ) { /* 1 == don't take the cnode lock */
459 if (VNODE_IS_RSRC(vp
)) {
460 /* opening the resource fork of a compressed file, so nothing to do */
462 /* opening a compressed file for read, make sure it validates */
463 error
= decmpfs_validate_compressed_file(vp
, VTOCMP(vp
));
472 * Files marked append-only must be opened for appending.
474 if ((cp
->c_flags
& APPEND
) && !vnode_isdir(vp
) &&
475 (ap
->a_mode
& (FWRITE
| O_APPEND
)) == FWRITE
)
478 if (vnode_isreg(vp
) && !UBCINFOEXISTS(vp
))
479 return (EBUSY
); /* file is in use by the kernel */
481 /* Don't allow journal file to be opened externally. */
482 if (cp
->c_fileid
== hfsmp
->hfs_jnlfileid
)
485 /* If we're going to write to the file, initialize quotas. */
487 if ((ap
->a_mode
& FWRITE
) && (hfsmp
->hfs_flags
& HFS_QUOTAS
))
488 (void)hfs_getinoquota(cp
);
492 * On the first (non-busy) open of a fragmented
493 * file attempt to de-frag it (if its less than 20MB).
495 if ((hfsmp
->hfs_flags
& HFS_READ_ONLY
) ||
496 (hfsmp
->jnl
== NULL
) ||
498 !vnode_isreg(vp
) || vnode_isinuse(vp
, 0) || vnode_isnamedstream(vp
)) {
500 !vnode_isreg(vp
) || vnode_isinuse(vp
, 0)) {
505 if ((error
= hfs_lock(cp
, HFS_EXCLUSIVE_LOCK
)))
509 fp
->ff_extents
[7].blockCount
!= 0 &&
510 fp
->ff_size
<= (20 * 1024 * 1024)) {
514 * Wait until system bootup is done (3 min).
515 * And don't relocate a file that's been modified
516 * within the past minute -- this can lead to
522 if (tv
.tv_sec
> (60*3)) {
528 if ((now
.tv_sec
- cp
->c_mtime
) > 60) {
532 if (past_bootup
&& no_mods
) {
533 (void) hfs_relocate(vp
, hfsmp
->nextAllocation
+ 4096,
534 vfs_context_ucred(ap
->a_context
),
535 vfs_context_proc(ap
->a_context
));
545 * Close a file/directory.
549 struct vnop_close_args
/* {
552 vfs_context_t a_context;
555 register struct vnode
*vp
= ap
->a_vp
;
556 register struct cnode
*cp
;
557 struct proc
*p
= vfs_context_proc(ap
->a_context
);
558 struct hfsmount
*hfsmp
;
560 int tooktrunclock
= 0;
563 if ( hfs_lock(VTOC(vp
), HFS_EXCLUSIVE_LOCK
) != 0)
569 * If the rsrc fork is a named stream, it can cause the data fork to
570 * stay around, preventing de-allocation of these blocks.
571 * Do checks for truncation on close. Purge extra extents if they exist.
572 * Make sure the vp is not a directory, and that it has a resource fork,
573 * and that resource fork is also a named stream.
576 if ((vp
->v_type
== VREG
) && (cp
->c_rsrc_vp
)
577 && (vnode_isnamedstream(cp
->c_rsrc_vp
))) {
580 blks
= howmany(VTOF(vp
)->ff_size
, VTOVCB(vp
)->blockSize
);
582 * If there are extra blocks and there are only 2 refs on
583 * this vp (ourselves + rsrc fork holding ref on us), go ahead
584 * and try to truncate.
586 if ((blks
< VTOF(vp
)->ff_blocks
) && (!vnode_isinuse(vp
, 2))) {
587 // release cnode lock; must acquire truncate lock BEFORE cnode lock
590 hfs_lock_truncate(cp
, HFS_EXCLUSIVE_LOCK
);
593 if (hfs_lock(VTOC(vp
), HFS_EXCLUSIVE_LOCK
) != 0) {
594 hfs_unlock_truncate(cp
, 0);
595 // bail out if we can't re-acquire cnode lock
598 // now re-test to make sure it's still valid
600 knownrefs
= 1 + vnode_isnamedstream(cp
->c_rsrc_vp
);
601 if (!vnode_isinuse(vp
, knownrefs
)){
602 // now we can truncate the file, if necessary
603 blks
= howmany(VTOF(vp
)->ff_size
, VTOVCB(vp
)->blockSize
);
604 if (blks
< VTOF(vp
)->ff_blocks
){
605 (void) hfs_truncate(vp
, VTOF(vp
)->ff_size
, IO_NDELAY
, 0, 0, ap
->a_context
);
613 // if we froze the fs and we're exiting, then "thaw" the fs
614 if (hfsmp
->hfs_freezing_proc
== p
&& proc_exiting(p
)) {
615 hfsmp
->hfs_freezing_proc
= NULL
;
616 hfs_unlock_global (hfsmp
);
617 lck_rw_unlock_exclusive(&hfsmp
->hfs_insync
);
620 busy
= vnode_isinuse(vp
, 1);
623 hfs_touchtimes(VTOHFS(vp
), cp
);
625 if (vnode_isdir(vp
)) {
626 hfs_reldirhints(cp
, busy
);
627 } else if (vnode_issystem(vp
) && !busy
) {
632 hfs_unlock_truncate(cp
, 0);
636 if (ap
->a_fflag
& FWASWRITTEN
) {
637 hfs_sync_ejectable(hfsmp
);
644 * Get basic attributes.
647 hfs_vnop_getattr(struct vnop_getattr_args
*ap
)
649 #define VNODE_ATTR_TIMES \
650 (VNODE_ATTR_va_access_time|VNODE_ATTR_va_change_time|VNODE_ATTR_va_modify_time)
651 #define VNODE_ATTR_AUTH \
652 (VNODE_ATTR_va_mode | VNODE_ATTR_va_uid | VNODE_ATTR_va_gid | \
653 VNODE_ATTR_va_flags | VNODE_ATTR_va_acl)
655 struct vnode
*vp
= ap
->a_vp
;
656 struct vnode_attr
*vap
= ap
->a_vap
;
657 struct vnode
*rvp
= NULLVP
;
658 struct hfsmount
*hfsmp
;
666 /* we need to inspect the decmpfs state of the file before we take the hfs cnode lock */
669 off_t uncompressed_size
= -1;
670 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
)) {
671 /* we only care about whether the file is compressed if asked for the uncompressed size */
672 if (VNODE_IS_RSRC(vp
)) {
673 /* if it's a resource fork, decmpfs may want us to hide the size */
674 hide_size
= hfs_hides_rsrc(ap
->a_context
, cp
, 0);
676 /* if it's a data fork, we need to know if it was compressed so we can report the uncompressed size */
677 compressed
= hfs_file_is_compressed(cp
, 0);
679 if ((VATTR_IS_ACTIVE(vap
, va_data_size
) || VATTR_IS_ACTIVE(vap
, va_total_size
))) {
680 // if it's compressed
681 if (compressed
|| (!VNODE_IS_RSRC(vp
) && cp
->c_decmp
&& cp
->c_decmp
->cmp_type
>= CMP_MAX
)) {
682 if (0 != hfs_uncompressed_size_of_compressed_file(NULL
, vp
, 0, &uncompressed_size
, 0)) {
683 /* failed to get the uncompressed size, we'll check for this later */
684 uncompressed_size
= -1;
686 // fake that it's compressed
695 * Shortcut for vnode_authorize path. Each of the attributes
696 * in this set is updated atomically so we don't need to take
697 * the cnode lock to access them.
699 if ((vap
->va_active
& ~VNODE_ATTR_AUTH
) == 0) {
700 /* Make sure file still exists. */
701 if (cp
->c_flag
& C_NOEXISTS
)
704 vap
->va_uid
= cp
->c_uid
;
705 vap
->va_gid
= cp
->c_gid
;
706 vap
->va_mode
= cp
->c_mode
;
707 vap
->va_flags
= cp
->c_flags
;
708 vap
->va_supported
|= VNODE_ATTR_AUTH
& ~VNODE_ATTR_va_acl
;
710 if ((cp
->c_attr
.ca_recflags
& kHFSHasSecurityMask
) == 0) {
711 vap
->va_acl
= (kauth_acl_t
) KAUTH_FILESEC_NONE
;
712 VATTR_SET_SUPPORTED(vap
, va_acl
);
719 v_type
= vnode_vtype(vp
);
721 * If time attributes are requested and we have cnode times
722 * that require updating, then acquire an exclusive lock on
723 * the cnode before updating the times. Otherwise we can
724 * just acquire a shared lock.
726 if ((vap
->va_active
& VNODE_ATTR_TIMES
) &&
727 (cp
->c_touch_acctime
|| cp
->c_touch_chgtime
|| cp
->c_touch_modtime
)) {
728 if ((error
= hfs_lock(cp
, HFS_EXCLUSIVE_LOCK
)))
730 hfs_touchtimes(hfsmp
, cp
);
733 if ((error
= hfs_lock(cp
, HFS_SHARED_LOCK
)))
737 if (v_type
== VDIR
) {
738 data_size
= (cp
->c_entries
+ 2) * AVERAGE_HFSDIRENTRY_SIZE
;
740 if (VATTR_IS_ACTIVE(vap
, va_nlink
)) {
744 * For directories, the va_nlink is esentially a count
745 * of the ".." references to a directory plus the "."
746 * reference and the directory itself. So for HFS+ this
747 * becomes the sub-directory count plus two.
749 * In the absence of a sub-directory count we use the
750 * directory's item count. This will be too high in
751 * most cases since it also includes files.
753 if ((hfsmp
->hfs_flags
& HFS_FOLDERCOUNT
) &&
754 (cp
->c_attr
.ca_recflags
& kHFSHasFolderCountMask
))
755 nlink
= cp
->c_attr
.ca_dircount
; /* implied ".." entries */
757 nlink
= cp
->c_entries
;
759 /* Account for ourself and our "." entry */
761 /* Hide our private directories. */
762 if (cp
->c_cnid
== kHFSRootFolderID
) {
763 if (hfsmp
->hfs_private_desc
[FILE_HARDLINKS
].cd_cnid
!= 0) {
766 if (hfsmp
->hfs_private_desc
[DIR_HARDLINKS
].cd_cnid
!= 0) {
770 VATTR_RETURN(vap
, va_nlink
, (u_int64_t
)nlink
);
772 if (VATTR_IS_ACTIVE(vap
, va_nchildren
)) {
775 entries
= cp
->c_entries
;
776 /* Hide our private files and directories. */
777 if (cp
->c_cnid
== kHFSRootFolderID
) {
778 if (hfsmp
->hfs_private_desc
[FILE_HARDLINKS
].cd_cnid
!= 0)
780 if (hfsmp
->hfs_private_desc
[DIR_HARDLINKS
].cd_cnid
!= 0)
782 if (hfsmp
->jnl
|| ((hfsmp
->vcbAtrb
& kHFSVolumeJournaledMask
) && (hfsmp
->hfs_flags
& HFS_READ_ONLY
)))
783 entries
-= 2; /* hide the journal files */
785 VATTR_RETURN(vap
, va_nchildren
, entries
);
788 * The va_dirlinkcount is the count of real directory hard links.
789 * (i.e. its not the sum of the implied "." and ".." references)
791 if (VATTR_IS_ACTIVE(vap
, va_dirlinkcount
)) {
792 VATTR_RETURN(vap
, va_dirlinkcount
, (uint32_t)cp
->c_linkcount
);
795 data_size
= VCTOF(vp
, cp
)->ff_size
;
797 VATTR_RETURN(vap
, va_nlink
, (u_int64_t
)cp
->c_linkcount
);
798 if (VATTR_IS_ACTIVE(vap
, va_data_alloc
)) {
803 VATTR_RETURN(vap
, va_data_alloc
, 0);
804 } else if (compressed
) {
805 /* for compressed files, we report all allocated blocks as belonging to the data fork */
806 blocks
= cp
->c_blocks
;
807 VATTR_RETURN(vap
, va_data_alloc
, blocks
* (u_int64_t
)hfsmp
->blockSize
);
812 blocks
= VCTOF(vp
, cp
)->ff_blocks
;
813 VATTR_RETURN(vap
, va_data_alloc
, blocks
* (u_int64_t
)hfsmp
->blockSize
);
818 /* conditional because 64-bit arithmetic can be expensive */
819 if (VATTR_IS_ACTIVE(vap
, va_total_size
)) {
820 if (v_type
== VDIR
) {
821 VATTR_RETURN(vap
, va_total_size
, (cp
->c_entries
+ 2) * AVERAGE_HFSDIRENTRY_SIZE
);
823 u_int64_t total_size
= ~0ULL;
827 /* we're hiding the size of this file, so just return 0 */
829 } else if (compressed
) {
830 if (uncompressed_size
== -1) {
832 * We failed to get the uncompressed size above,
833 * so we'll fall back to the standard path below
834 * since total_size is still -1
837 /* use the uncompressed size we fetched above */
838 total_size
= uncompressed_size
;
842 if (total_size
== ~0ULL) {
843 if (cp
->c_datafork
) {
844 total_size
= cp
->c_datafork
->ff_size
;
847 if (cp
->c_blocks
- VTOF(vp
)->ff_blocks
) {
848 /* We deal with rsrc fork vnode iocount at the end of the function */
849 error
= hfs_vgetrsrc(hfsmp
, vp
, &rvp
, TRUE
, FALSE
);
852 * Note that we call hfs_vgetrsrc with error_on_unlinked
853 * set to FALSE. This is because we may be invoked via
854 * fstat() on an open-unlinked file descriptor and we must
855 * continue to support access to the rsrc fork until it disappears.
856 * The code at the end of this function will be
857 * responsible for releasing the iocount generated by
858 * hfs_vgetrsrc. This is because we can't drop the iocount
859 * without unlocking the cnode first.
865 if (rcp
&& rcp
->c_rsrcfork
) {
866 total_size
+= rcp
->c_rsrcfork
->ff_size
;
871 VATTR_RETURN(vap
, va_total_size
, total_size
);
874 if (VATTR_IS_ACTIVE(vap
, va_total_alloc
)) {
875 if (v_type
== VDIR
) {
876 VATTR_RETURN(vap
, va_total_alloc
, 0);
878 VATTR_RETURN(vap
, va_total_alloc
, (u_int64_t
)cp
->c_blocks
* (u_int64_t
)hfsmp
->blockSize
);
883 * If the VFS wants extended security data, and we know that we
884 * don't have any (because it never told us it was setting any)
885 * then we can return the supported bit and no data. If we do
886 * have extended security, we can just leave the bit alone and
887 * the VFS will use the fallback path to fetch it.
889 if (VATTR_IS_ACTIVE(vap
, va_acl
)) {
890 if ((cp
->c_attr
.ca_recflags
& kHFSHasSecurityMask
) == 0) {
891 vap
->va_acl
= (kauth_acl_t
) KAUTH_FILESEC_NONE
;
892 VATTR_SET_SUPPORTED(vap
, va_acl
);
895 if (VATTR_IS_ACTIVE(vap
, va_access_time
)) {
896 /* Access times are lazily updated, get current time if needed */
897 if (cp
->c_touch_acctime
) {
901 vap
->va_access_time
.tv_sec
= tv
.tv_sec
;
903 vap
->va_access_time
.tv_sec
= cp
->c_atime
;
905 vap
->va_access_time
.tv_nsec
= 0;
906 VATTR_SET_SUPPORTED(vap
, va_access_time
);
908 vap
->va_create_time
.tv_sec
= cp
->c_itime
;
909 vap
->va_create_time
.tv_nsec
= 0;
910 vap
->va_modify_time
.tv_sec
= cp
->c_mtime
;
911 vap
->va_modify_time
.tv_nsec
= 0;
912 vap
->va_change_time
.tv_sec
= cp
->c_ctime
;
913 vap
->va_change_time
.tv_nsec
= 0;
914 vap
->va_backup_time
.tv_sec
= cp
->c_btime
;
915 vap
->va_backup_time
.tv_nsec
= 0;
917 /* See if we need to emit the date added field to the user */
918 if (VATTR_IS_ACTIVE(vap
, va_addedtime
)) {
919 u_int32_t dateadded
= hfs_get_dateadded (cp
);
921 vap
->va_addedtime
.tv_sec
= dateadded
;
922 vap
->va_addedtime
.tv_nsec
= 0;
923 VATTR_SET_SUPPORTED (vap
, va_addedtime
);
928 /* XXX is this really a good 'optimal I/O size'? */
929 vap
->va_iosize
= hfsmp
->hfs_logBlockSize
;
930 vap
->va_uid
= cp
->c_uid
;
931 vap
->va_gid
= cp
->c_gid
;
932 vap
->va_mode
= cp
->c_mode
;
933 vap
->va_flags
= cp
->c_flags
;
936 * Exporting file IDs from HFS Plus:
938 * For "normal" files the c_fileid is the same value as the
939 * c_cnid. But for hard link files, they are different - the
940 * c_cnid belongs to the active directory entry (ie the link)
941 * and the c_fileid is for the actual inode (ie the data file).
943 * The stat call (getattr) uses va_fileid and the Carbon APIs,
944 * which are hardlink-ignorant, will ask for va_linkid.
946 vap
->va_fileid
= (u_int64_t
)cp
->c_fileid
;
948 * We need to use the origin cache for both hardlinked files
949 * and directories. Hardlinked directories have multiple cnids
950 * and parents (one per link). Hardlinked files also have their
951 * own parents and link IDs separate from the indirect inode number.
952 * If we don't use the cache, we could end up vending the wrong ID
953 * because the cnode will only reflect the link that was looked up most recently.
955 if (cp
->c_flag
& C_HARDLINK
) {
956 vap
->va_linkid
= (u_int64_t
)hfs_currentcnid(cp
);
957 vap
->va_parentid
= (u_int64_t
)hfs_currentparent(cp
);
959 vap
->va_linkid
= (u_int64_t
)cp
->c_cnid
;
960 vap
->va_parentid
= (u_int64_t
)cp
->c_parentcnid
;
962 vap
->va_fsid
= hfsmp
->hfs_raw_dev
;
964 vap
->va_encoding
= cp
->c_encoding
;
965 vap
->va_rdev
= (v_type
== VBLK
|| v_type
== VCHR
) ? cp
->c_rdev
: 0;
967 if (VATTR_IS_ACTIVE(vap
, va_data_size
)) {
969 vap
->va_data_size
= 0;
970 else if (compressed
) {
971 if (uncompressed_size
== -1) {
972 /* failed to get the uncompressed size above, so just return data_size */
973 vap
->va_data_size
= data_size
;
975 /* use the uncompressed size we fetched above */
976 vap
->va_data_size
= uncompressed_size
;
979 vap
->va_data_size
= data_size
;
980 // vap->va_supported |= VNODE_ATTR_va_data_size;
981 VATTR_SET_SUPPORTED(vap
, va_data_size
);
984 vap
->va_data_size
= data_size
;
985 vap
->va_supported
|= VNODE_ATTR_va_data_size
;
988 /* Mark them all at once instead of individual VATTR_SET_SUPPORTED calls. */
989 vap
->va_supported
|= VNODE_ATTR_va_create_time
| VNODE_ATTR_va_modify_time
|
990 VNODE_ATTR_va_change_time
| VNODE_ATTR_va_backup_time
|
991 VNODE_ATTR_va_iosize
| VNODE_ATTR_va_uid
|
992 VNODE_ATTR_va_gid
| VNODE_ATTR_va_mode
|
993 VNODE_ATTR_va_flags
|VNODE_ATTR_va_fileid
|
994 VNODE_ATTR_va_linkid
| VNODE_ATTR_va_parentid
|
995 VNODE_ATTR_va_fsid
| VNODE_ATTR_va_filerev
|
996 VNODE_ATTR_va_encoding
| VNODE_ATTR_va_rdev
;
998 /* If this is the root, let VFS to find out the mount name, which
999 * may be different from the real name. Otherwise, we need to take care
1000 * for hardlinked files, which need to be looked up, if necessary
1002 if (VATTR_IS_ACTIVE(vap
, va_name
) && (cp
->c_cnid
!= kHFSRootFolderID
)) {
1003 struct cat_desc linkdesc
;
1005 int uselinkdesc
= 0;
1006 cnid_t nextlinkid
= 0;
1007 cnid_t prevlinkid
= 0;
1009 /* Get the name for ATTR_CMN_NAME. We need to take special care for hardlinks
1010 * here because the info. for the link ID requested by getattrlist may be
1011 * different than what's currently in the cnode. This is because the cnode
1012 * will be filled in with the information for the most recent link ID that went
1013 * through namei/lookup(). If there are competing lookups for hardlinks that point
1014 * to the same inode, one (or more) getattrlists could be vended incorrect name information.
1015 * Also, we need to beware of open-unlinked files which could have a namelen of 0.
1018 if ((cp
->c_flag
& C_HARDLINK
) &&
1019 ((cp
->c_desc
.cd_namelen
== 0) || (vap
->va_linkid
!= cp
->c_cnid
))) {
1020 /* If we have no name and our link ID is the raw inode number, then we may
1021 * have an open-unlinked file. Go to the next link in this case.
1023 if ((cp
->c_desc
.cd_namelen
== 0) && (vap
->va_linkid
== cp
->c_fileid
)) {
1024 if ((error
= hfs_lookup_siblinglinks(hfsmp
, vap
->va_linkid
, &prevlinkid
, &nextlinkid
))){
1029 /* just use link obtained from vap above */
1030 nextlinkid
= vap
->va_linkid
;
1033 /* We need to probe the catalog for the descriptor corresponding to the link ID
1034 * stored in nextlinkid. Note that we don't know if we have the exclusive lock
1035 * for the cnode here, so we can't just update the descriptor. Instead,
1036 * we should just store the descriptor's value locally and then use it to pass
1037 * out the name value as needed below.
1040 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
1041 error
= cat_findname(hfsmp
, nextlinkid
, &linkdesc
);
1042 hfs_systemfile_unlock(hfsmp
, lockflags
);
1049 /* By this point, we've either patched up the name above and the c_desc
1050 * points to the correct data, or it already did, in which case we just proceed
1051 * by copying the name into the vap. Note that we will never set va_name to
1052 * supported if nextlinkid is never initialized. This could happen in the degenerate
1053 * case above involving the raw inode number, where it has no nextlinkid. In this case
1054 * we will simply not mark the name bit as supported.
1057 strlcpy(vap
->va_name
, (const char*) linkdesc
.cd_nameptr
, MAXPATHLEN
);
1058 VATTR_SET_SUPPORTED(vap
, va_name
);
1059 cat_releasedesc(&linkdesc
);
1061 else if (cp
->c_desc
.cd_namelen
) {
1062 strlcpy(vap
->va_name
, (const char*) cp
->c_desc
.cd_nameptr
, MAXPATHLEN
);
1063 VATTR_SET_SUPPORTED(vap
, va_name
);
1070 * We need to vnode_put the rsrc fork vnode only *after* we've released
1071 * the cnode lock, since vnode_put can trigger an inactive call, which
1072 * will go back into HFS and try to acquire a cnode lock.
1082 hfs_vnop_setattr(ap
)
1083 struct vnop_setattr_args
/* {
1085 struct vnode_attr *a_vap;
1086 vfs_context_t a_context;
1089 struct vnode_attr
*vap
= ap
->a_vap
;
1090 struct vnode
*vp
= ap
->a_vp
;
1091 struct cnode
*cp
= NULL
;
1092 struct hfsmount
*hfsmp
;
1093 kauth_cred_t cred
= vfs_context_ucred(ap
->a_context
);
1094 struct proc
*p
= vfs_context_proc(ap
->a_context
);
1100 orig_ctime
= VTOC(vp
)->c_ctime
;
1103 int decmpfs_reset_state
= 0;
1105 we call decmpfs_update_attributes even if the file is not compressed
1106 because we want to update the incoming flags if the xattrs are invalid
1108 error
= decmpfs_update_attributes(vp
, vap
);
1113 // if this is not a size-changing setattr and it is not just
1114 // an atime update, then check for a snapshot.
1116 if (!VATTR_IS_ACTIVE(vap
, va_data_size
) && !(vap
->va_active
== VNODE_ATTR_va_access_time
)) {
1117 check_for_tracked_file(vp
, orig_ctime
, NAMESPACE_HANDLER_METADATA_MOD
, NULL
);
1123 if ((error
= cp_handle_vnop(VTOC(vp
), CP_WRITE_ACCESS
)) != 0) {
1126 #endif /* CONFIG_PROTECT */
1130 /* Don't allow modification of the journal file. */
1131 if (hfsmp
->hfs_jnlfileid
== VTOC(vp
)->c_fileid
) {
1136 * File size change request.
1137 * We are guaranteed that this is not a directory, and that
1138 * the filesystem object is writeable.
1140 * NOTE: HFS COMPRESSION depends on the data_size being set *before* the bsd flags are updated
1142 VATTR_SET_SUPPORTED(vap
, va_data_size
);
1143 if (VATTR_IS_ACTIVE(vap
, va_data_size
) && !vnode_islnk(vp
)) {
1145 /* keep the compressed state locked until we're done truncating the file */
1146 decmpfs_cnode
*dp
= VTOCMP(vp
);
1149 * call hfs_lazy_init_decmpfs_cnode() to make sure that the decmpfs_cnode
1150 * is filled in; we need a decmpfs_cnode to lock out decmpfs state changes
1151 * on this file while it's truncating
1153 dp
= hfs_lazy_init_decmpfs_cnode(VTOC(vp
));
1155 /* failed to allocate a decmpfs_cnode */
1156 return ENOMEM
; /* what should this be? */
1160 check_for_tracked_file(vp
, orig_ctime
, vap
->va_data_size
== 0 ? NAMESPACE_HANDLER_TRUNCATE_OP
|NAMESPACE_HANDLER_DELETE_OP
: NAMESPACE_HANDLER_TRUNCATE_OP
, NULL
);
1162 decmpfs_lock_compressed_data(dp
, 1);
1163 if (hfs_file_is_compressed(VTOC(vp
), 1)) {
1164 error
= decmpfs_decompress_file(vp
, dp
, -1/*vap->va_data_size*/, 0, 1);
1166 decmpfs_unlock_compressed_data(dp
, 1);
1172 /* Take truncate lock before taking cnode lock. */
1173 hfs_lock_truncate(VTOC(vp
), HFS_EXCLUSIVE_LOCK
);
1175 /* Perform the ubc_setsize before taking the cnode lock. */
1176 ubc_setsize(vp
, vap
->va_data_size
);
1178 if ((error
= hfs_lock(VTOC(vp
), HFS_EXCLUSIVE_LOCK
))) {
1179 hfs_unlock_truncate(VTOC(vp
), 0);
1181 decmpfs_unlock_compressed_data(dp
, 1);
1187 error
= hfs_truncate(vp
, vap
->va_data_size
, vap
->va_vaflags
& 0xffff, 1, 0, ap
->a_context
);
1189 hfs_unlock_truncate(cp
, 0);
1191 decmpfs_unlock_compressed_data(dp
, 1);
1197 if ((error
= hfs_lock(VTOC(vp
), HFS_EXCLUSIVE_LOCK
)))
1203 * If it is just an access time update request by itself
1204 * we know the request is from kernel level code, and we
1205 * can delay it without being as worried about consistency.
1206 * This change speeds up mmaps, in the rare case that they
1207 * get caught behind a sync.
1210 if (vap
->va_active
== VNODE_ATTR_va_access_time
) {
1211 cp
->c_touch_acctime
=TRUE
;
1218 * Owner/group change request.
1219 * We are guaranteed that the new owner/group is valid and legal.
1221 VATTR_SET_SUPPORTED(vap
, va_uid
);
1222 VATTR_SET_SUPPORTED(vap
, va_gid
);
1223 nuid
= VATTR_IS_ACTIVE(vap
, va_uid
) ? vap
->va_uid
: (uid_t
)VNOVAL
;
1224 ngid
= VATTR_IS_ACTIVE(vap
, va_gid
) ? vap
->va_gid
: (gid_t
)VNOVAL
;
1225 if (((nuid
!= (uid_t
)VNOVAL
) || (ngid
!= (gid_t
)VNOVAL
)) &&
1226 ((error
= hfs_chown(vp
, nuid
, ngid
, cred
, p
)) != 0))
1230 * Mode change request.
1231 * We are guaranteed that the mode value is valid and that in
1232 * conjunction with the owner and group, this change is legal.
1234 VATTR_SET_SUPPORTED(vap
, va_mode
);
1235 if (VATTR_IS_ACTIVE(vap
, va_mode
) &&
1236 ((error
= hfs_chmod(vp
, (int)vap
->va_mode
, cred
, p
)) != 0))
1240 * File flags change.
1241 * We are guaranteed that only flags allowed to change given the
1242 * current securelevel are being changed.
1244 VATTR_SET_SUPPORTED(vap
, va_flags
);
1245 if (VATTR_IS_ACTIVE(vap
, va_flags
)) {
1249 if ((cp
->c_flags
^ vap
->va_flags
) & UF_COMPRESSED
) {
1251 * the UF_COMPRESSED was toggled, so reset our cached compressed state
1252 * but we don't want to actually do the update until we've released the cnode lock down below
1253 * NOTE: turning the flag off doesn't actually decompress the file, so that we can
1254 * turn off the flag and look at the "raw" file for debugging purposes
1256 decmpfs_reset_state
= 1;
1260 cp
->c_flags
= vap
->va_flags
;
1261 cp
->c_touch_chgtime
= TRUE
;
1264 * Mirror the UF_HIDDEN flag to the invisible bit of the Finder Info.
1266 * The fdFlags for files and frFlags for folders are both 8 bytes
1267 * into the userInfo (the first 16 bytes of the Finder Info). They
1268 * are both 16-bit fields.
1270 fdFlags
= (u_int16_t
*) &cp
->c_finderinfo
[8];
1271 if (vap
->va_flags
& UF_HIDDEN
)
1272 *fdFlags
|= OSSwapHostToBigConstInt16(kFinderInvisibleMask
);
1274 *fdFlags
&= ~OSSwapHostToBigConstInt16(kFinderInvisibleMask
);
1278 * Timestamp updates.
1280 VATTR_SET_SUPPORTED(vap
, va_create_time
);
1281 VATTR_SET_SUPPORTED(vap
, va_access_time
);
1282 VATTR_SET_SUPPORTED(vap
, va_modify_time
);
1283 VATTR_SET_SUPPORTED(vap
, va_backup_time
);
1284 VATTR_SET_SUPPORTED(vap
, va_change_time
);
1285 if (VATTR_IS_ACTIVE(vap
, va_create_time
) ||
1286 VATTR_IS_ACTIVE(vap
, va_access_time
) ||
1287 VATTR_IS_ACTIVE(vap
, va_modify_time
) ||
1288 VATTR_IS_ACTIVE(vap
, va_backup_time
)) {
1289 if (VATTR_IS_ACTIVE(vap
, va_create_time
))
1290 cp
->c_itime
= vap
->va_create_time
.tv_sec
;
1291 if (VATTR_IS_ACTIVE(vap
, va_access_time
)) {
1292 cp
->c_atime
= vap
->va_access_time
.tv_sec
;
1293 cp
->c_touch_acctime
= FALSE
;
1295 if (VATTR_IS_ACTIVE(vap
, va_modify_time
)) {
1296 cp
->c_mtime
= vap
->va_modify_time
.tv_sec
;
1297 cp
->c_touch_modtime
= FALSE
;
1298 cp
->c_touch_chgtime
= TRUE
;
1301 * The utimes system call can reset the modification
1302 * time but it doesn't know about HFS create times.
1303 * So we need to ensure that the creation time is
1304 * always at least as old as the modification time.
1306 if ((VTOVCB(vp
)->vcbSigWord
== kHFSPlusSigWord
) &&
1307 (cp
->c_cnid
!= kHFSRootFolderID
) &&
1308 (cp
->c_mtime
< cp
->c_itime
)) {
1309 cp
->c_itime
= cp
->c_mtime
;
1312 if (VATTR_IS_ACTIVE(vap
, va_backup_time
))
1313 cp
->c_btime
= vap
->va_backup_time
.tv_sec
;
1314 cp
->c_flag
|= C_MODIFIED
;
1318 * Set name encoding.
1320 VATTR_SET_SUPPORTED(vap
, va_encoding
);
1321 if (VATTR_IS_ACTIVE(vap
, va_encoding
)) {
1322 cp
->c_encoding
= vap
->va_encoding
;
1323 hfs_setencodingbits(hfsmp
, cp
->c_encoding
);
1326 if ((error
= hfs_update(vp
, TRUE
)) != 0)
1330 /* Purge origin cache for cnode, since caller now has correct link ID for it
1331 * We purge it here since it was acquired for us during lookup, and we no longer need it.
1333 if ((cp
->c_flag
& C_HARDLINK
) && (vp
->v_type
!= VDIR
)){
1334 hfs_relorigin(cp
, 0);
1339 if (decmpfs_reset_state
) {
1341 * we've changed the UF_COMPRESSED flag, so reset the decmpfs state for this cnode
1342 * but don't do it while holding the hfs cnode lock
1344 decmpfs_cnode
*dp
= VTOCMP(vp
);
1347 * call hfs_lazy_init_decmpfs_cnode() to make sure that the decmpfs_cnode
1348 * is filled in; we need a decmpfs_cnode to prevent decmpfs state changes
1349 * on this file if it's locked
1351 dp
= hfs_lazy_init_decmpfs_cnode(VTOC(vp
));
1353 /* failed to allocate a decmpfs_cnode */
1354 return ENOMEM
; /* what should this be? */
1357 decmpfs_cnode_set_vnode_state(dp
, FILE_TYPE_UNKNOWN
, 0);
1366 * Change the mode on a file.
1367 * cnode must be locked before calling.
1370 hfs_chmod(struct vnode
*vp
, int mode
, __unused kauth_cred_t cred
, __unused
struct proc
*p
)
1372 register struct cnode
*cp
= VTOC(vp
);
1374 if (VTOVCB(vp
)->vcbSigWord
!= kHFSPlusSigWord
)
1377 // XXXdbg - don't allow modification of the journal or journal_info_block
1378 if (VTOHFS(vp
)->jnl
&& cp
&& cp
->c_datafork
) {
1379 struct HFSPlusExtentDescriptor
*extd
;
1381 extd
= &cp
->c_datafork
->ff_extents
[0];
1382 if (extd
->startBlock
== VTOVCB(vp
)->vcbJinfoBlock
|| extd
->startBlock
== VTOHFS(vp
)->jnl_start
) {
1387 #if OVERRIDE_UNKNOWN_PERMISSIONS
1388 if (((unsigned int)vfs_flags(VTOVFS(vp
))) & MNT_UNKNOWNPERMISSIONS
) {
1392 cp
->c_mode
&= ~ALLPERMS
;
1393 cp
->c_mode
|= (mode
& ALLPERMS
);
1394 cp
->c_touch_chgtime
= TRUE
;
1400 hfs_write_access(struct vnode
*vp
, kauth_cred_t cred
, struct proc
*p
, Boolean considerFlags
)
1402 struct cnode
*cp
= VTOC(vp
);
1407 * Disallow write attempts on read-only file systems;
1408 * unless the file is a socket, fifo, or a block or
1409 * character device resident on the file system.
1411 switch (vnode_vtype(vp
)) {
1415 if (VTOHFS(vp
)->hfs_flags
& HFS_READ_ONLY
)
1422 /* If immutable bit set, nobody gets to write it. */
1423 if (considerFlags
&& (cp
->c_flags
& IMMUTABLE
))
1426 /* Otherwise, user id 0 always gets access. */
1427 if (!suser(cred
, NULL
))
1430 /* Otherwise, check the owner. */
1431 if ((retval
= hfs_owner_rights(VTOHFS(vp
), cp
->c_uid
, cred
, p
, false)) == 0)
1432 return ((cp
->c_mode
& S_IWUSR
) == S_IWUSR
? 0 : EACCES
);
1434 /* Otherwise, check the groups. */
1435 if (kauth_cred_ismember_gid(cred
, cp
->c_gid
, &is_member
) == 0 && is_member
) {
1436 return ((cp
->c_mode
& S_IWGRP
) == S_IWGRP
? 0 : EACCES
);
1439 /* Otherwise, check everyone else. */
1440 return ((cp
->c_mode
& S_IWOTH
) == S_IWOTH
? 0 : EACCES
);
1445 * Perform chown operation on cnode cp;
1446 * code must be locked prior to call.
1450 hfs_chown(struct vnode
*vp
, uid_t uid
, gid_t gid
, __unused kauth_cred_t cred
,
1451 __unused
struct proc
*p
)
1453 hfs_chown(struct vnode
*vp
, uid_t uid
, gid_t gid
, kauth_cred_t cred
,
1454 __unused
struct proc
*p
)
1457 register struct cnode
*cp
= VTOC(vp
);
1466 if (VTOVCB(vp
)->vcbSigWord
!= kHFSPlusSigWord
)
1469 if (((unsigned int)vfs_flags(VTOVFS(vp
))) & MNT_UNKNOWNPERMISSIONS
)
1472 if (uid
== (uid_t
)VNOVAL
)
1474 if (gid
== (gid_t
)VNOVAL
)
1477 #if 0 /* we are guaranteed that this is already the case */
1479 * If we don't own the file, are trying to change the owner
1480 * of the file, or are not a member of the target group,
1481 * the caller must be superuser or the call fails.
1483 if ((kauth_cred_getuid(cred
) != cp
->c_uid
|| uid
!= cp
->c_uid
||
1484 (gid
!= cp
->c_gid
&&
1485 (kauth_cred_ismember_gid(cred
, gid
, &is_member
) || !is_member
))) &&
1486 (error
= suser(cred
, 0)))
1493 if ((error
= hfs_getinoquota(cp
)))
1496 dqrele(cp
->c_dquot
[USRQUOTA
]);
1497 cp
->c_dquot
[USRQUOTA
] = NODQUOT
;
1500 dqrele(cp
->c_dquot
[GRPQUOTA
]);
1501 cp
->c_dquot
[GRPQUOTA
] = NODQUOT
;
1505 * Eventually need to account for (fake) a block per directory
1506 * if (vnode_isdir(vp))
1507 * change = VTOHFS(vp)->blockSize;
1511 change
= (int64_t)(cp
->c_blocks
) * (int64_t)VTOVCB(vp
)->blockSize
;
1512 (void) hfs_chkdq(cp
, -change
, cred
, CHOWN
);
1513 (void) hfs_chkiq(cp
, -1, cred
, CHOWN
);
1514 for (i
= 0; i
< MAXQUOTAS
; i
++) {
1515 dqrele(cp
->c_dquot
[i
]);
1516 cp
->c_dquot
[i
] = NODQUOT
;
1522 if ((error
= hfs_getinoquota(cp
)) == 0) {
1524 dqrele(cp
->c_dquot
[USRQUOTA
]);
1525 cp
->c_dquot
[USRQUOTA
] = NODQUOT
;
1528 dqrele(cp
->c_dquot
[GRPQUOTA
]);
1529 cp
->c_dquot
[GRPQUOTA
] = NODQUOT
;
1531 if ((error
= hfs_chkdq(cp
, change
, cred
, CHOWN
)) == 0) {
1532 if ((error
= hfs_chkiq(cp
, 1, cred
, CHOWN
)) == 0)
1535 (void) hfs_chkdq(cp
, -change
, cred
, CHOWN
|FORCE
);
1537 for (i
= 0; i
< MAXQUOTAS
; i
++) {
1538 dqrele(cp
->c_dquot
[i
]);
1539 cp
->c_dquot
[i
] = NODQUOT
;
1544 if (hfs_getinoquota(cp
) == 0) {
1546 dqrele(cp
->c_dquot
[USRQUOTA
]);
1547 cp
->c_dquot
[USRQUOTA
] = NODQUOT
;
1550 dqrele(cp
->c_dquot
[GRPQUOTA
]);
1551 cp
->c_dquot
[GRPQUOTA
] = NODQUOT
;
1553 (void) hfs_chkdq(cp
, change
, cred
, FORCE
|CHOWN
);
1554 (void) hfs_chkiq(cp
, 1, cred
, FORCE
|CHOWN
);
1555 (void) hfs_getinoquota(cp
);
1559 if (hfs_getinoquota(cp
))
1560 panic("hfs_chown: lost quota");
1565 According to the SUSv3 Standard, chown() shall mark
1566 for update the st_ctime field of the file.
1567 (No exceptions mentioned)
1569 cp
->c_touch_chgtime
= TRUE
;
1575 * The hfs_exchange routine swaps the fork data in two files by
1576 * exchanging some of the information in the cnode. It is used
1577 * to preserve the file ID when updating an existing file, in
1578 * case the file is being tracked through its file ID. Typically
1579 * its used after creating a new file during a safe-save.
1582 hfs_vnop_exchange(ap
)
1583 struct vnop_exchange_args
/* {
1584 struct vnode *a_fvp;
1585 struct vnode *a_tvp;
1587 vfs_context_t a_context;
1590 struct vnode
*from_vp
= ap
->a_fvp
;
1591 struct vnode
*to_vp
= ap
->a_tvp
;
1592 struct cnode
*from_cp
;
1593 struct cnode
*to_cp
;
1594 struct hfsmount
*hfsmp
;
1595 struct cat_desc tempdesc
;
1596 struct cat_attr tempattr
;
1597 const unsigned char *from_nameptr
;
1598 const unsigned char *to_nameptr
;
1599 char from_iname
[32];
1605 int error
= 0, started_tr
= 0, got_cookie
= 0;
1606 cat_cookie_t cookie
;
1607 time_t orig_from_ctime
, orig_to_ctime
;
1609 /* The files must be on the same volume. */
1610 if (vnode_mount(from_vp
) != vnode_mount(to_vp
))
1613 if (from_vp
== to_vp
)
1616 orig_from_ctime
= VTOC(from_vp
)->c_ctime
;
1617 orig_to_ctime
= VTOC(to_vp
)->c_ctime
;
1620 if ( hfs_file_is_compressed(VTOC(from_vp
), 0) ) {
1621 if ( 0 != ( error
= decmpfs_decompress_file(from_vp
, VTOCMP(from_vp
), -1, 0, 1) ) ) {
1626 if ( hfs_file_is_compressed(VTOC(to_vp
), 0) ) {
1627 if ( 0 != ( error
= decmpfs_decompress_file(to_vp
, VTOCMP(to_vp
), -1, 0, 1) ) ) {
1631 #endif // HFS_COMPRESSION
1634 * Normally, we want to notify the user handlers about the event,
1635 * except if it's a handler driving the event.
1637 if ((ap
->a_options
& FSOPT_EXCHANGE_DATA_ONLY
) == 0) {
1638 check_for_tracked_file(from_vp
, orig_from_ctime
, NAMESPACE_HANDLER_WRITE_OP
, NULL
);
1639 check_for_tracked_file(to_vp
, orig_to_ctime
, NAMESPACE_HANDLER_WRITE_OP
, NULL
);
1643 * We're doing a data-swap.
1644 * Take the truncate lock/cnode lock, then verify there are no mmap references.
1645 * Issue a hfs_filedone to flush out all of the remaining state for this file.
1646 * Allow the rest of the codeflow to re-acquire the cnode locks in order.
1649 hfs_lock_truncate (VTOC(from_vp
), HFS_SHARED_LOCK
);
1651 if ((error
= hfs_lock(VTOC(from_vp
), HFS_EXCLUSIVE_LOCK
))) {
1652 hfs_unlock_truncate (VTOC(from_vp
), 0);
1656 /* Verify the source file is not in use by anyone besides us (including mmap refs) */
1657 if (vnode_isinuse(from_vp
, 1)) {
1659 hfs_unlock(VTOC(from_vp
));
1660 hfs_unlock_truncate (VTOC(from_vp
), 0);
1664 /* Flush out the data in the source file */
1665 VTOC(from_vp
)->c_flag
|= C_SWAPINPROGRESS
;
1666 error
= hfs_filedone (from_vp
, ap
->a_context
);
1667 VTOC(from_vp
)->c_flag
&= ~C_SWAPINPROGRESS
;
1668 hfs_unlock(VTOC(from_vp
));
1669 hfs_unlock_truncate(VTOC(from_vp
), 0);
1677 if ((error
= hfs_lockpair(VTOC(from_vp
), VTOC(to_vp
), HFS_EXCLUSIVE_LOCK
)))
1680 from_cp
= VTOC(from_vp
);
1681 to_cp
= VTOC(to_vp
);
1682 hfsmp
= VTOHFS(from_vp
);
1684 /* Only normal files can be exchanged. */
1685 if (!vnode_isreg(from_vp
) || !vnode_isreg(to_vp
) ||
1686 VNODE_IS_RSRC(from_vp
) || VNODE_IS_RSRC(to_vp
)) {
1691 // XXXdbg - don't allow modification of the journal or journal_info_block
1693 struct HFSPlusExtentDescriptor
*extd
;
1695 if (from_cp
->c_datafork
) {
1696 extd
= &from_cp
->c_datafork
->ff_extents
[0];
1697 if (extd
->startBlock
== VTOVCB(from_vp
)->vcbJinfoBlock
|| extd
->startBlock
== hfsmp
->jnl_start
) {
1703 if (to_cp
->c_datafork
) {
1704 extd
= &to_cp
->c_datafork
->ff_extents
[0];
1705 if (extd
->startBlock
== VTOVCB(to_vp
)->vcbJinfoBlock
|| extd
->startBlock
== hfsmp
->jnl_start
) {
1713 * Ok, now that all of the pre-flighting is done, call the underlying
1714 * function if needed.
1716 if (ap
->a_options
& FSOPT_EXCHANGE_DATA_ONLY
) {
1717 error
= hfs_movedata(from_vp
, to_vp
);
1722 if ((error
= hfs_start_transaction(hfsmp
)) != 0) {
1728 * Reserve some space in the Catalog file.
1730 if ((error
= cat_preflight(hfsmp
, CAT_EXCHANGE
, &cookie
, vfs_context_proc(ap
->a_context
)))) {
1735 /* The backend code always tries to delete the virtual
1736 * extent id for exchanging files so we need to lock
1737 * the extents b-tree.
1739 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
| SFL_EXTENTS
| SFL_ATTRIBUTE
, HFS_EXCLUSIVE_LOCK
);
1741 /* Account for the location of the catalog objects. */
1742 if (from_cp
->c_flag
& C_HARDLINK
) {
1743 MAKE_INODE_NAME(from_iname
, sizeof(from_iname
),
1744 from_cp
->c_attr
.ca_linkref
);
1745 from_nameptr
= (unsigned char *)from_iname
;
1746 from_parid
= hfsmp
->hfs_private_desc
[FILE_HARDLINKS
].cd_cnid
;
1747 from_cp
->c_hint
= 0;
1749 from_nameptr
= from_cp
->c_desc
.cd_nameptr
;
1750 from_parid
= from_cp
->c_parentcnid
;
1752 if (to_cp
->c_flag
& C_HARDLINK
) {
1753 MAKE_INODE_NAME(to_iname
, sizeof(to_iname
),
1754 to_cp
->c_attr
.ca_linkref
);
1755 to_nameptr
= (unsigned char *)to_iname
;
1756 to_parid
= hfsmp
->hfs_private_desc
[FILE_HARDLINKS
].cd_cnid
;
1759 to_nameptr
= to_cp
->c_desc
.cd_nameptr
;
1760 to_parid
= to_cp
->c_parentcnid
;
1763 /* Do the exchange */
1764 error
= ExchangeFileIDs(hfsmp
, from_nameptr
, to_nameptr
, from_parid
,
1765 to_parid
, from_cp
->c_hint
, to_cp
->c_hint
);
1766 hfs_systemfile_unlock(hfsmp
, lockflags
);
1769 * Note that we don't need to exchange any extended attributes
1770 * since the attributes are keyed by file ID.
1773 if (error
!= E_NONE
) {
1774 error
= MacToVFSError(error
);
1778 /* Purge the vnodes from the name cache */
1780 cache_purge(from_vp
);
1784 /* Save a copy of from attributes before swapping. */
1785 bcopy(&from_cp
->c_desc
, &tempdesc
, sizeof(struct cat_desc
));
1786 bcopy(&from_cp
->c_attr
, &tempattr
, sizeof(struct cat_attr
));
1787 tempflag
= from_cp
->c_flag
& (C_HARDLINK
| C_HASXATTRS
);
1790 * Swap the descriptors and all non-fork related attributes.
1791 * (except the modify date)
1793 bcopy(&to_cp
->c_desc
, &from_cp
->c_desc
, sizeof(struct cat_desc
));
1795 from_cp
->c_hint
= 0;
1796 from_cp
->c_fileid
= from_cp
->c_cnid
;
1797 from_cp
->c_itime
= to_cp
->c_itime
;
1798 from_cp
->c_btime
= to_cp
->c_btime
;
1799 from_cp
->c_atime
= to_cp
->c_atime
;
1800 from_cp
->c_ctime
= to_cp
->c_ctime
;
1801 from_cp
->c_gid
= to_cp
->c_gid
;
1802 from_cp
->c_uid
= to_cp
->c_uid
;
1803 from_cp
->c_flags
= to_cp
->c_flags
;
1804 from_cp
->c_mode
= to_cp
->c_mode
;
1805 from_cp
->c_linkcount
= to_cp
->c_linkcount
;
1806 from_cp
->c_flag
= to_cp
->c_flag
& (C_HARDLINK
| C_HASXATTRS
);
1807 from_cp
->c_attr
.ca_recflags
= to_cp
->c_attr
.ca_recflags
;
1808 bcopy(to_cp
->c_finderinfo
, from_cp
->c_finderinfo
, 32);
1810 bcopy(&tempdesc
, &to_cp
->c_desc
, sizeof(struct cat_desc
));
1812 to_cp
->c_fileid
= to_cp
->c_cnid
;
1813 to_cp
->c_itime
= tempattr
.ca_itime
;
1814 to_cp
->c_btime
= tempattr
.ca_btime
;
1815 to_cp
->c_atime
= tempattr
.ca_atime
;
1816 to_cp
->c_ctime
= tempattr
.ca_ctime
;
1817 to_cp
->c_gid
= tempattr
.ca_gid
;
1818 to_cp
->c_uid
= tempattr
.ca_uid
;
1819 to_cp
->c_flags
= tempattr
.ca_flags
;
1820 to_cp
->c_mode
= tempattr
.ca_mode
;
1821 to_cp
->c_linkcount
= tempattr
.ca_linkcount
;
1822 to_cp
->c_flag
= tempflag
;
1823 to_cp
->c_attr
.ca_recflags
= tempattr
.ca_recflags
;
1824 bcopy(tempattr
.ca_finderinfo
, to_cp
->c_finderinfo
, 32);
1826 /* Rehash the cnodes using their new file IDs */
1827 hfs_chash_rehash(hfsmp
, from_cp
, to_cp
);
1830 * When a file moves out of "Cleanup At Startup"
1831 * we can drop its NODUMP status.
1833 if ((from_cp
->c_flags
& UF_NODUMP
) &&
1834 (from_cp
->c_parentcnid
!= to_cp
->c_parentcnid
)) {
1835 from_cp
->c_flags
&= ~UF_NODUMP
;
1836 from_cp
->c_touch_chgtime
= TRUE
;
1838 if ((to_cp
->c_flags
& UF_NODUMP
) &&
1839 (to_cp
->c_parentcnid
!= from_cp
->c_parentcnid
)) {
1840 to_cp
->c_flags
&= ~UF_NODUMP
;
1841 to_cp
->c_touch_chgtime
= TRUE
;
1846 cat_postflight(hfsmp
, &cookie
, vfs_context_proc(ap
->a_context
));
1849 hfs_end_transaction(hfsmp
);
1852 hfs_unlockpair(from_cp
, to_cp
);
1857 hfs_vnop_mmap(struct vnop_mmap_args
*ap
)
1859 struct vnode
*vp
= ap
->a_vp
;
1862 if (VNODE_IS_RSRC(vp
)) {
1863 /* allow pageins of the resource fork */
1865 int compressed
= hfs_file_is_compressed(VTOC(vp
), 1); /* 1 == don't take the cnode lock */
1866 time_t orig_ctime
= VTOC(vp
)->c_ctime
;
1868 if (!compressed
&& (VTOC(vp
)->c_flags
& UF_COMPRESSED
)) {
1869 error
= check_for_dataless_file(vp
, NAMESPACE_HANDLER_READ_OP
);
1875 if (ap
->a_fflags
& PROT_WRITE
) {
1876 check_for_tracked_file(vp
, orig_ctime
, NAMESPACE_HANDLER_WRITE_OP
, NULL
);
1881 // NOTE: we return ENOTSUP because we want the cluster layer
1882 // to actually do all the real work.
1890 * This is a non-symmetric variant of exchangedata. In this function,
1891 * the contents of the fork in from_vp are moved to the fork
1892 * specified by to_vp.
1894 * The cnodes pointed to by 'from_vp' and 'to_vp' must be locked.
1896 * The vnode pointed to by 'to_vp' *must* be empty prior to invoking this function.
1897 * We impose this restriction because we may not be able to fully delete the entire
1898 * file's contents in a single transaction, particularly if it has a lot of extents.
1899 * In the normal file deletion codepath, the file is screened for two conditions:
1900 * 1) bigger than 400MB, and 2) more than 8 extents. If so, the file is relocated to
1901 * the hidden directory and the deletion is broken up into multiple truncates. We can't
1902 * do that here because both files need to exist in the namespace. The main reason this
1903 * is imposed is that we may have to touch a whole lot of bitmap blocks if there are
1906 * Any data written to 'from_vp' after this call completes is not guaranteed
1910 * vnode from_vp: source file
1911 * vnode to_vp: destination file; must be empty
1914 * EFBIG - Destination file was not empty
1919 int hfs_movedata (struct vnode
*from_vp
, struct vnode
*to_vp
) {
1921 struct cnode
*from_cp
;
1922 struct cnode
*to_cp
;
1923 struct hfsmount
*hfsmp
= NULL
;
1927 int overflow_blocks
;
1931 /* Get the HFS pointers */
1932 from_cp
= VTOC(from_vp
);
1933 to_cp
= VTOC(to_vp
);
1934 hfsmp
= VTOHFS(from_vp
);
1936 /* Verify that neither source/dest file is open-unlinked */
1937 if (from_cp
->c_flag
& (C_DELETED
| C_NOEXISTS
)) {
1942 if (to_cp
->c_flag
& (C_DELETED
| C_NOEXISTS
)) {
1948 * Verify the source file is not in use by anyone besides us.
1950 * This function is typically invoked by a namespace handler
1951 * process responding to a temporarily stalled system call.
1952 * The FD that it is working off of is opened O_EVTONLY, so
1953 * it really has no active usecounts (the kusecount from O_EVTONLY
1954 * is subtracted from the total usecounts).
1956 * As a result, we shouldn't have any active usecounts against
1957 * this vnode when we go to check it below.
1959 if (vnode_isinuse(from_vp
, 0)) {
1964 if (from_cp
->c_rsrc_vp
== from_vp
) {
1969 * We assume that the destination file is already empty.
1970 * Verify that it is.
1973 if (to_cp
->c_rsrcfork
->ff_size
> 0) {
1979 if (to_cp
->c_datafork
->ff_size
> 0) {
1985 /* If the source has the rsrc open, make sure the destination is also the rsrc */
1987 if (to_vp
!= to_cp
->c_rsrc_vp
) {
1993 /* Verify that both forks are data forks */
1994 if (to_vp
!= to_cp
->c_vp
) {
2001 * See if the source file has overflow extents. If it doesn't, we don't
2002 * need to call into MoveData, and the catalog will be enough.
2005 overflow_blocks
= overflow_extents(from_cp
->c_rsrcfork
);
2008 overflow_blocks
= overflow_extents(from_cp
->c_datafork
);
2011 if ((error
= hfs_start_transaction (hfsmp
)) != 0) {
2016 /* Lock the system files: catalog, extents, attributes */
2017 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
| SFL_EXTENTS
| SFL_ATTRIBUTE
, HFS_EXCLUSIVE_LOCK
);
2019 /* Copy over any catalog allocation data into the new spot. */
2021 if ((error
= hfs_move_fork (from_cp
->c_rsrcfork
, from_cp
, to_cp
->c_rsrcfork
, to_cp
))){
2022 hfs_systemfile_unlock(hfsmp
, lockflags
);
2027 if ((error
= hfs_move_fork (from_cp
->c_datafork
, from_cp
, to_cp
->c_datafork
, to_cp
))) {
2028 hfs_systemfile_unlock(hfsmp
, lockflags
);
2034 * Note that because all we're doing is moving the extents around, we can
2035 * probably do this in a single transaction: Each extent record (group of 8)
2036 * is 64 bytes. A extent overflow B-Tree node is typically 4k. This means
2037 * each node can hold roughly ~60 extent records == (480 extents).
2039 * If a file was massively fragmented and had 20k extents, this means we'd
2040 * roughly touch 20k/480 == 41 to 42 nodes, plus the index nodes, for half
2041 * of the operation. (inserting or deleting). So if we're manipulating 80-100
2042 * nodes, this is basically 320k of data to write to the journal in
2045 if (overflow_blocks
!= 0) {
2047 error
= MoveData(hfsmp
, from_cp
->c_cnid
, to_cp
->c_cnid
, 1);
2050 error
= MoveData (hfsmp
, from_cp
->c_cnid
, to_cp
->c_cnid
, 0);
2055 /* Reverse the operation. Copy the fork data back into the source */
2057 hfs_move_fork (to_cp
->c_rsrcfork
, to_cp
, from_cp
->c_rsrcfork
, from_cp
);
2060 hfs_move_fork (to_cp
->c_datafork
, to_cp
, from_cp
->c_datafork
, from_cp
);
2064 struct cat_fork
*src_data
= NULL
;
2065 struct cat_fork
*src_rsrc
= NULL
;
2066 struct cat_fork
*dst_data
= NULL
;
2067 struct cat_fork
*dst_rsrc
= NULL
;
2069 /* Touch the times*/
2070 to_cp
->c_touch_acctime
= TRUE
;
2071 to_cp
->c_touch_chgtime
= TRUE
;
2072 to_cp
->c_touch_modtime
= TRUE
;
2074 from_cp
->c_touch_acctime
= TRUE
;
2075 from_cp
->c_touch_chgtime
= TRUE
;
2076 from_cp
->c_touch_modtime
= TRUE
;
2078 hfs_touchtimes(hfsmp
, to_cp
);
2079 hfs_touchtimes(hfsmp
, from_cp
);
2081 if (from_cp
->c_datafork
) {
2082 src_data
= &from_cp
->c_datafork
->ff_data
;
2084 if (from_cp
->c_rsrcfork
) {
2085 src_rsrc
= &from_cp
->c_rsrcfork
->ff_data
;
2088 if (to_cp
->c_datafork
) {
2089 dst_data
= &to_cp
->c_datafork
->ff_data
;
2091 if (to_cp
->c_rsrcfork
) {
2092 dst_rsrc
= &to_cp
->c_rsrcfork
->ff_data
;
2095 /* Update the catalog nodes */
2096 (void) cat_update(hfsmp
, &from_cp
->c_desc
, &from_cp
->c_attr
,
2097 src_data
, src_rsrc
);
2099 (void) cat_update(hfsmp
, &to_cp
->c_desc
, &to_cp
->c_attr
,
2100 dst_data
, dst_rsrc
);
2103 /* unlock the system files */
2104 hfs_systemfile_unlock(hfsmp
, lockflags
);
2109 hfs_end_transaction(hfsmp
);
2117 * Copy all of the catalog and runtime data in srcfork to dstfork.
2119 * This allows us to maintain the invalid ranges across the movedata operation so
2120 * we don't need to force all of the pending IO right now. In addition, we move all
2121 * non overflow-extent extents into the destination here.
2123 static int hfs_move_fork (struct filefork
*srcfork
, struct cnode
*src_cp
,
2124 struct filefork
*dstfork
, struct cnode
*dst_cp
) {
2125 struct rl_entry
*invalid_range
;
2126 int size
= sizeof(struct HFSPlusExtentDescriptor
);
2127 size
= size
* kHFSPlusExtentDensity
;
2129 /* If the dstfork has any invalid ranges, bail out */
2130 invalid_range
= TAILQ_FIRST(&dstfork
->ff_invalidranges
);
2131 if (invalid_range
!= NULL
) {
2135 if (dstfork
->ff_data
.cf_size
!= 0 || dstfork
->ff_data
.cf_new_size
!= 0) {
2139 /* First copy the invalid ranges */
2140 while ((invalid_range
= TAILQ_FIRST(&srcfork
->ff_invalidranges
))) {
2141 off_t start
= invalid_range
->rl_start
;
2142 off_t end
= invalid_range
->rl_end
;
2144 /* Remove it from the srcfork and add it to dstfork */
2145 rl_remove(start
, end
, &srcfork
->ff_invalidranges
);
2146 rl_add(start
, end
, &dstfork
->ff_invalidranges
);
2150 * Ignore the ff_union. We don't move symlinks or system files.
2151 * Now copy the in-catalog extent information
2153 dstfork
->ff_data
.cf_size
= srcfork
->ff_data
.cf_size
;
2154 dstfork
->ff_data
.cf_new_size
= srcfork
->ff_data
.cf_new_size
;
2155 dstfork
->ff_data
.cf_vblocks
= srcfork
->ff_data
.cf_vblocks
;
2156 dstfork
->ff_data
.cf_blocks
= srcfork
->ff_data
.cf_blocks
;
2158 /* just memcpy the whole array of extents to the new location. */
2159 memcpy (dstfork
->ff_data
.cf_extents
, srcfork
->ff_data
.cf_extents
, size
);
2162 * Copy the cnode attribute data.
2165 src_cp
->c_blocks
-= srcfork
->ff_data
.cf_vblocks
;
2166 src_cp
->c_blocks
-= srcfork
->ff_data
.cf_blocks
;
2168 dst_cp
->c_blocks
+= srcfork
->ff_data
.cf_vblocks
;
2169 dst_cp
->c_blocks
+= srcfork
->ff_data
.cf_blocks
;
2171 /* Now delete the entries in the source fork */
2172 srcfork
->ff_data
.cf_size
= 0;
2173 srcfork
->ff_data
.cf_new_size
= 0;
2174 srcfork
->ff_data
.cf_union
.cfu_bytesread
= 0;
2175 srcfork
->ff_data
.cf_vblocks
= 0;
2176 srcfork
->ff_data
.cf_blocks
= 0;
2178 /* Zero out the old extents */
2179 bzero (srcfork
->ff_data
.cf_extents
, size
);
2186 * cnode must be locked
2189 hfs_fsync(struct vnode
*vp
, int waitfor
, int fullsync
, struct proc
*p
)
2191 struct cnode
*cp
= VTOC(vp
);
2192 struct filefork
*fp
= NULL
;
2194 struct hfsmount
*hfsmp
= VTOHFS(vp
);
2195 struct rl_entry
*invalid_range
;
2197 int waitdata
; /* attributes necessary for data retrieval */
2198 int wait
; /* all other attributes (e.g. atime, etc.) */
2200 int took_trunc_lock
= 0;
2203 * Applications which only care about data integrity rather than full
2204 * file integrity may opt out of (delay) expensive metadata update
2205 * operations as a performance optimization.
2207 wait
= (waitfor
== MNT_WAIT
);
2208 waitdata
= (waitfor
== MNT_DWAIT
) | wait
;
2209 if (always_do_fullfsync
)
2212 /* HFS directories don't have any data blocks. */
2213 if (vnode_isdir(vp
))
2218 * For system files flush the B-tree header and
2219 * for regular files write out any clusters
2221 if (vnode_issystem(vp
)) {
2222 if (VTOF(vp
)->fcbBTCBPtr
!= NULL
) {
2224 if (hfsmp
->jnl
== NULL
) {
2225 BTFlushPath(VTOF(vp
));
2228 } else if (UBCINFOEXISTS(vp
)) {
2230 hfs_lock_truncate(cp
, HFS_SHARED_LOCK
);
2231 took_trunc_lock
= 1;
2233 if (fp
->ff_unallocblocks
!= 0) {
2234 hfs_unlock_truncate(cp
, 0);
2236 hfs_lock_truncate(cp
, HFS_EXCLUSIVE_LOCK
);
2238 /* Don't hold cnode lock when calling into cluster layer. */
2239 (void) cluster_push(vp
, waitdata
? IO_SYNC
: 0);
2241 hfs_lock(cp
, HFS_FORCE_LOCK
);
2244 * When MNT_WAIT is requested and the zero fill timeout
2245 * has expired then we must explicitly zero out any areas
2246 * that are currently marked invalid (holes).
2248 * Files with NODUMP can bypass zero filling here.
2250 if (fp
&& (((cp
->c_flag
& C_ALWAYS_ZEROFILL
) && !TAILQ_EMPTY(&fp
->ff_invalidranges
)) ||
2251 ((wait
|| (cp
->c_flag
& C_ZFWANTSYNC
)) &&
2252 ((cp
->c_flags
& UF_NODUMP
) == 0) &&
2253 UBCINFOEXISTS(vp
) && (vnode_issystem(vp
) ==0) &&
2254 cp
->c_zftimeout
!= 0))) {
2257 if ((cp
->c_flag
& C_ALWAYS_ZEROFILL
) == 0 && !fullsync
&& tv
.tv_sec
< (long)cp
->c_zftimeout
) {
2258 /* Remember that a force sync was requested. */
2259 cp
->c_flag
|= C_ZFWANTSYNC
;
2262 if (!TAILQ_EMPTY(&fp
->ff_invalidranges
)) {
2263 if (!took_trunc_lock
|| (cp
->c_truncatelockowner
== HFS_SHARED_OWNER
)) {
2265 if (took_trunc_lock
) {
2266 hfs_unlock_truncate(cp
, 0);
2268 hfs_lock_truncate(cp
, HFS_EXCLUSIVE_LOCK
);
2269 hfs_lock(cp
, HFS_FORCE_LOCK
);
2270 took_trunc_lock
= 1;
2272 while ((invalid_range
= TAILQ_FIRST(&fp
->ff_invalidranges
))) {
2273 off_t start
= invalid_range
->rl_start
;
2274 off_t end
= invalid_range
->rl_end
;
2276 /* The range about to be written must be validated
2277 * first, so that VNOP_BLOCKMAP() will return the
2278 * appropriate mapping for the cluster code:
2280 rl_remove(start
, end
, &fp
->ff_invalidranges
);
2282 /* Don't hold cnode lock when calling into cluster layer. */
2284 (void) cluster_write(vp
, (struct uio
*) 0,
2285 fp
->ff_size
, end
+ 1, start
, (off_t
)0,
2286 IO_HEADZEROFILL
| IO_NOZERODIRTY
| IO_NOCACHE
);
2287 hfs_lock(cp
, HFS_FORCE_LOCK
);
2288 cp
->c_flag
|= C_MODIFIED
;
2291 (void) cluster_push(vp
, waitdata
? IO_SYNC
: 0);
2292 hfs_lock(cp
, HFS_FORCE_LOCK
);
2294 cp
->c_flag
&= ~C_ZFWANTSYNC
;
2295 cp
->c_zftimeout
= 0;
2298 if (took_trunc_lock
) {
2299 hfs_unlock_truncate(cp
, 0);
2300 took_trunc_lock
= 0;
2303 * if we have a journal and if journal_active() returns != 0 then the
2304 * we shouldn't do anything to a locked block (because it is part
2305 * of a transaction). otherwise we'll just go through the normal
2306 * code path and flush the buffer. note journal_active() can return
2307 * -1 if the journal is invalid -- however we still need to skip any
2308 * locked blocks as they get cleaned up when we finish the transaction
2309 * or close the journal.
2311 // if (hfsmp->jnl && journal_active(hfsmp->jnl) >= 0)
2313 lockflag
= BUF_SKIP_LOCKED
;
2318 * Flush all dirty buffers associated with a vnode.
2320 buf_flushdirtyblks(vp
, waitdata
, lockflag
, "hfs_fsync");
2323 if (vnode_isreg(vp
) && vnode_issystem(vp
)) {
2324 if (VTOF(vp
)->fcbBTCBPtr
!= NULL
) {
2326 BTSetLastSync(VTOF(vp
), tv
.tv_sec
);
2328 cp
->c_touch_acctime
= FALSE
;
2329 cp
->c_touch_chgtime
= FALSE
;
2330 cp
->c_touch_modtime
= FALSE
;
2331 } else if ( !(vp
->v_flag
& VSWAP
) ) /* User file */ {
2332 retval
= hfs_update(vp
, wait
);
2335 * When MNT_WAIT is requested push out the catalog record for
2336 * this file. If they asked for a full fsync, we can skip this
2337 * because the journal_flush or hfs_metasync_all will push out
2338 * all of the metadata changes.
2340 if ((retval
== 0) && wait
&& !fullsync
&& cp
->c_hint
&&
2341 !ISSET(cp
->c_flag
, C_DELETED
| C_NOEXISTS
)) {
2342 hfs_metasync(VTOHFS(vp
), (daddr64_t
)cp
->c_hint
, p
);
2346 * If this was a full fsync, make sure all metadata
2347 * changes get to stable storage.
2351 hfs_journal_flush(hfsmp
, FALSE
);
2353 if (journal_uses_fua(hfsmp
->jnl
)) {
2355 * the journal_flush did NOT issue a sync track cache command,
2356 * and the fullsync indicates we are supposed to flush all cached
2357 * data to the media, so issue the sync track cache command
2360 VNOP_IOCTL(hfsmp
->hfs_devvp
, DKIOCSYNCHRONIZECACHE
, NULL
, FWRITE
, NULL
);
2363 retval
= hfs_metasync_all(hfsmp
);
2364 /* XXX need to pass context! */
2365 VNOP_IOCTL(hfsmp
->hfs_devvp
, DKIOCSYNCHRONIZECACHE
, NULL
, FWRITE
, NULL
);
2374 /* Sync an hfs catalog b-tree node */
2376 hfs_metasync(struct hfsmount
*hfsmp
, daddr64_t node
, __unused
struct proc
*p
)
2382 vp
= HFSTOVCB(hfsmp
)->catalogRefNum
;
2384 // XXXdbg - don't need to do this on a journaled volume
2389 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_EXCLUSIVE_LOCK
);
2391 * Look for a matching node that has been delayed
2392 * but is not part of a set (B_LOCKED).
2394 * BLK_ONLYVALID causes buf_getblk to return a
2395 * buf_t for the daddr64_t specified only if it's
2396 * currently resident in the cache... the size
2397 * parameter to buf_getblk is ignored when this flag
2400 bp
= buf_getblk(vp
, node
, 0, 0, 0, BLK_META
| BLK_ONLYVALID
);
2403 if ((buf_flags(bp
) & (B_LOCKED
| B_DELWRI
)) == B_DELWRI
)
2404 (void) VNOP_BWRITE(bp
);
2409 hfs_systemfile_unlock(hfsmp
, lockflags
);
2416 * Sync all hfs B-trees. Use this instead of journal_flush for a volume
2417 * without a journal. Note that the volume bitmap does not get written;
2418 * we rely on fsck_hfs to fix that up (which it can do without any loss
2422 hfs_metasync_all(struct hfsmount
*hfsmp
)
2426 /* Lock all of the B-trees so we get a mutually consistent state */
2427 lockflags
= hfs_systemfile_lock(hfsmp
,
2428 SFL_CATALOG
|SFL_EXTENTS
|SFL_ATTRIBUTE
, HFS_EXCLUSIVE_LOCK
);
2430 /* Sync each of the B-trees */
2431 if (hfsmp
->hfs_catalog_vp
)
2432 hfs_btsync(hfsmp
->hfs_catalog_vp
, 0);
2433 if (hfsmp
->hfs_extents_vp
)
2434 hfs_btsync(hfsmp
->hfs_extents_vp
, 0);
2435 if (hfsmp
->hfs_attribute_vp
)
2436 hfs_btsync(hfsmp
->hfs_attribute_vp
, 0);
2438 /* Wait for all of the writes to complete */
2439 if (hfsmp
->hfs_catalog_vp
)
2440 vnode_waitforwrites(hfsmp
->hfs_catalog_vp
, 0, 0, 0, "hfs_metasync_all");
2441 if (hfsmp
->hfs_extents_vp
)
2442 vnode_waitforwrites(hfsmp
->hfs_extents_vp
, 0, 0, 0, "hfs_metasync_all");
2443 if (hfsmp
->hfs_attribute_vp
)
2444 vnode_waitforwrites(hfsmp
->hfs_attribute_vp
, 0, 0, 0, "hfs_metasync_all");
2446 hfs_systemfile_unlock(hfsmp
, lockflags
);
2454 hfs_btsync_callback(struct buf
*bp
, __unused
void *dummy
)
2456 buf_clearflags(bp
, B_LOCKED
);
2457 (void) buf_bawrite(bp
);
2459 return(BUF_CLAIMED
);
2464 hfs_btsync(struct vnode
*vp
, int sync_transaction
)
2466 struct cnode
*cp
= VTOC(vp
);
2470 if (sync_transaction
)
2471 flags
|= BUF_SKIP_NONLOCKED
;
2473 * Flush all dirty buffers associated with b-tree.
2475 buf_iterate(vp
, hfs_btsync_callback
, flags
, 0);
2478 if (vnode_issystem(vp
) && (VTOF(vp
)->fcbBTCBPtr
!= NULL
))
2479 (void) BTSetLastSync(VTOF(vp
), tv
.tv_sec
);
2480 cp
->c_touch_acctime
= FALSE
;
2481 cp
->c_touch_chgtime
= FALSE
;
2482 cp
->c_touch_modtime
= FALSE
;
2488 * Remove a directory.
2492 struct vnop_rmdir_args
/* {
2493 struct vnode *a_dvp;
2495 struct componentname *a_cnp;
2496 vfs_context_t a_context;
2499 struct vnode
*dvp
= ap
->a_dvp
;
2500 struct vnode
*vp
= ap
->a_vp
;
2501 struct cnode
*dcp
= VTOC(dvp
);
2502 struct cnode
*cp
= VTOC(vp
);
2506 orig_ctime
= VTOC(vp
)->c_ctime
;
2508 if (!S_ISDIR(cp
->c_mode
)) {
2515 check_for_tracked_file(vp
, orig_ctime
, NAMESPACE_HANDLER_DELETE_OP
, NULL
);
2518 if ((error
= hfs_lockpair(dcp
, cp
, HFS_EXCLUSIVE_LOCK
))) {
2522 /* Check for a race with rmdir on the parent directory */
2523 if (dcp
->c_flag
& (C_DELETED
| C_NOEXISTS
)) {
2524 hfs_unlockpair (dcp
, cp
);
2527 error
= hfs_removedir(dvp
, vp
, ap
->a_cnp
, 0, 0);
2529 hfs_unlockpair(dcp
, cp
);
2535 * Remove a directory
2537 * Both dvp and vp cnodes are locked
2540 hfs_removedir(struct vnode
*dvp
, struct vnode
*vp
, struct componentname
*cnp
,
2541 int skip_reserve
, int only_unlink
)
2545 struct hfsmount
* hfsmp
;
2546 struct cat_desc desc
;
2548 int error
= 0, started_tr
= 0;
2555 return (EINVAL
); /* cannot remove "." */
2557 if (cp
->c_flag
& (C_NOEXISTS
| C_DELETED
)) {
2560 if (cp
->c_entries
!= 0) {
2565 * If the directory is open or in use (e.g. opendir() or current working
2566 * directory for some process); wait for inactive/reclaim to actually
2567 * remove cnode from the catalog. Both inactive and reclaim codepaths are capable
2568 * of removing open-unlinked directories from the catalog, as well as getting rid
2569 * of EAs still on the element. So change only_unlink to true, so that it will get
2572 * Otherwise, we can get into a weird old mess where the directory has C_DELETED,
2573 * but it really means C_NOEXISTS because the item was actually removed from the
2574 * catalog. Then when we try to remove the entry from the catalog later on, it won't
2575 * really be there anymore.
2577 if (vnode_isinuse(vp
, 0)) {
2581 /* Deal with directory hardlinks */
2582 if (cp
->c_flag
& C_HARDLINK
) {
2584 * Note that if we have a directory which was a hardlink at any point,
2585 * its actual directory data is stored in the directory inode in the hidden
2586 * directory rather than the leaf element(s) present in the namespace.
2588 * If there are still other hardlinks to this directory,
2589 * then we'll just eliminate this particular link and the vnode will still exist.
2590 * If this is the last link to an empty directory, then we'll open-unlink the
2591 * directory and it will be only tagged with C_DELETED (as opposed to C_NOEXISTS).
2593 * We could also return EBUSY here.
2596 return hfs_unlink(hfsmp
, dvp
, vp
, cnp
, skip_reserve
);
2600 * In a few cases, we may want to allow the directory to persist in an
2601 * open-unlinked state. If the directory is being open-unlinked (still has usecount
2602 * references), or if it has EAs, or if it was being deleted as part of a rename,
2603 * then we go ahead and move it to the hidden directory.
2605 * If the directory is being open-unlinked, then we want to keep the catalog entry
2606 * alive so that future EA calls and fchmod/fstat etc. do not cause issues later.
2608 * If the directory had EAs, then we want to use the open-unlink trick so that the
2609 * EA removal is not done in one giant transaction. Otherwise, it could cause a panic
2610 * due to overflowing the journal.
2612 * Finally, if it was deleted as part of a rename, we move it to the hidden directory
2613 * in order to maintain rename atomicity.
2615 * Note that the allow_dirs argument to hfs_removefile specifies that it is
2616 * supposed to handle directories for this case.
2619 if (((hfsmp
->hfs_attribute_vp
!= NULL
) &&
2620 ((cp
->c_attr
.ca_recflags
& kHFSHasAttributesMask
) != 0)) ||
2621 (only_unlink
!= 0)) {
2623 int ret
= hfs_removefile(dvp
, vp
, cnp
, 0, 0, 1, NULL
, only_unlink
);
2625 * Even though hfs_vnop_rename calls vnode_recycle for us on tvp we call
2626 * it here just in case we were invoked by rmdir() on a directory that had
2627 * EAs. To ensure that we start reclaiming the space as soon as possible,
2628 * we call vnode_recycle on the directory.
2636 dcp
->c_flag
|= C_DIR_MODIFICATION
;
2639 if (hfsmp
->hfs_flags
& HFS_QUOTAS
)
2640 (void)hfs_getinoquota(cp
);
2642 if ((error
= hfs_start_transaction(hfsmp
)) != 0) {
2648 * Verify the directory is empty (and valid).
2649 * (Rmdir ".." won't be valid since
2650 * ".." will contain a reference to
2651 * the current directory and thus be
2654 if ((dcp
->c_flags
& APPEND
) || (cp
->c_flags
& (IMMUTABLE
| APPEND
))) {
2659 /* Remove the entry from the namei cache: */
2663 * Protect against a race with rename by using the component
2664 * name passed in and parent id from dvp (instead of using
2665 * the cp->c_desc which may have changed).
2667 desc
.cd_nameptr
= (const u_int8_t
*)cnp
->cn_nameptr
;
2668 desc
.cd_namelen
= cnp
->cn_namelen
;
2669 desc
.cd_parentcnid
= dcp
->c_fileid
;
2670 desc
.cd_cnid
= cp
->c_cnid
;
2671 desc
.cd_flags
= CD_ISDIR
;
2672 desc
.cd_encoding
= cp
->c_encoding
;
2675 if (!hfs_valid_cnode(hfsmp
, dvp
, cnp
, cp
->c_fileid
, NULL
, &error
)) {
2680 /* Remove entry from catalog */
2681 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
| SFL_ATTRIBUTE
| SFL_BITMAP
, HFS_EXCLUSIVE_LOCK
);
2683 if (!skip_reserve
) {
2685 * Reserve some space in the Catalog file.
2687 if ((error
= cat_preflight(hfsmp
, CAT_DELETE
, NULL
, 0))) {
2688 hfs_systemfile_unlock(hfsmp
, lockflags
);
2693 error
= cat_delete(hfsmp
, &desc
, &cp
->c_attr
);
2695 /* The parent lost a child */
2696 if (dcp
->c_entries
> 0)
2698 DEC_FOLDERCOUNT(hfsmp
, dcp
->c_attr
);
2699 dcp
->c_dirchangecnt
++;
2700 dcp
->c_touch_chgtime
= TRUE
;
2701 dcp
->c_touch_modtime
= TRUE
;
2702 hfs_touchtimes(hfsmp
, cp
);
2703 (void) cat_update(hfsmp
, &dcp
->c_desc
, &dcp
->c_attr
, NULL
, NULL
);
2704 cp
->c_flag
&= ~(C_MODIFIED
| C_FORCEUPDATE
);
2707 hfs_systemfile_unlock(hfsmp
, lockflags
);
2713 if (hfsmp
->hfs_flags
& HFS_QUOTAS
)
2714 (void)hfs_chkiq(cp
, -1, NOCRED
, 0);
2717 hfs_volupdate(hfsmp
, VOL_RMDIR
, (dcp
->c_cnid
== kHFSRootFolderID
));
2719 /* Mark C_NOEXISTS since the catalog entry is now gone */
2720 cp
->c_flag
|= C_NOEXISTS
;
2722 dcp
->c_flag
&= ~C_DIR_MODIFICATION
;
2723 wakeup((caddr_t
)&dcp
->c_flag
);
2726 hfs_end_transaction(hfsmp
);
2734 * Remove a file or link.
2738 struct vnop_remove_args
/* {
2739 struct vnode *a_dvp;
2741 struct componentname *a_cnp;
2743 vfs_context_t a_context;
2746 struct vnode
*dvp
= ap
->a_dvp
;
2747 struct vnode
*vp
= ap
->a_vp
;
2748 struct cnode
*dcp
= VTOC(dvp
);
2750 struct vnode
*rvp
= NULL
;
2751 struct hfsmount
*hfsmp
= VTOHFS(vp
);
2752 int error
=0, recycle_rsrc
=0;
2753 int drop_rsrc_vnode
= 0;
2760 orig_ctime
= VTOC(vp
)->c_ctime
;
2761 if (!vnode_isnamedstream(vp
)) {
2762 error
= check_for_tracked_file(vp
, orig_ctime
, NAMESPACE_HANDLER_DELETE_OP
, NULL
);
2764 // XXXdbg - decide on a policy for handling namespace handler failures!
2765 // for now we just let them proceed.
2773 * We need to grab the cnode lock on 'cp' before the lockpair()
2774 * to get an iocount on the rsrc fork BEFORE we enter hfs_removefile.
2775 * To prevent other deadlocks, it's best to call hfs_vgetrsrc in a way that
2776 * allows it to drop the cnode lock that it expects to be held coming in.
2777 * If we don't, we could commit a lock order violation, causing a deadlock.
2778 * In order to safely get the rsrc vnode with an iocount, we need to only hold the
2779 * lock on the file temporarily. Unlike hfs_vnop_rename, we don't have to worry
2780 * about one rsrc fork getting recycled for another, but we do want to ensure
2781 * that there are no deadlocks due to lock ordering issues.
2783 * Note: this function may be invoked for directory hardlinks, so just skip these
2784 * steps if 'vp' is a directory.
2788 if ((vp
->v_type
== VLNK
) || (vp
->v_type
== VREG
)) {
2790 if ((error
= hfs_lock (cp
, HFS_EXCLUSIVE_LOCK
))) {
2794 error
= hfs_vgetrsrc(hfsmp
, vp
, &rvp
, TRUE
, TRUE
);
2797 /* we may have gotten an rsrc vp even though we got an error */
2804 drop_rsrc_vnode
= 1;
2806 /* Now that we may have an iocount on rvp, do the lock pair */
2808 hfs_lock_truncate(cp
, HFS_EXCLUSIVE_LOCK
);
2810 if ((error
= hfs_lockpair(dcp
, cp
, HFS_EXCLUSIVE_LOCK
))) {
2811 hfs_unlock_truncate(cp
, 0);
2812 /* drop the iocount on rvp if necessary */
2813 if (drop_rsrc_vnode
) {
2820 * Check to see if we raced rmdir for the parent directory
2821 * hfs_removefile already checks for a race on vp/cp
2823 if (dcp
->c_flag
& (C_DELETED
| C_NOEXISTS
)) {
2828 error
= hfs_removefile(dvp
, vp
, ap
->a_cnp
, ap
->a_flags
, 0, 0, rvp
, 0);
2831 * If the remove succeeded in deleting the file, then we may need to mark
2832 * the resource fork for recycle so that it is reclaimed as quickly
2833 * as possible. If it were not recycled quickly, then this resource fork
2834 * vnode could keep a v_parent reference on the data fork, which prevents it
2835 * from going through reclaim (by giving it extra usecounts), except in the force-
2838 * However, a caveat: we need to continue to supply resource fork
2839 * access to open-unlinked files even if the resource fork is not open. This is
2840 * a requirement for the compressed files work. Luckily, hfs_vgetrsrc will handle
2841 * this already if the data fork has been re-parented to the hidden directory.
2843 * As a result, all we really need to do here is mark the resource fork vnode
2844 * for recycle. If it goes out of core, it can be brought in again if needed.
2845 * If the cnode was instead marked C_NOEXISTS, then there wouldn't be any
2848 if ((error
== 0) && (rvp
)) {
2853 * Drop the truncate lock before unlocking the cnode
2854 * (which can potentially perform a vnode_put and
2855 * recycle the vnode which in turn might require the
2859 hfs_unlock_truncate(cp
, 0);
2860 hfs_unlockpair(dcp
, cp
);
2863 /* inactive or reclaim on rvp will clean up the blocks from the rsrc fork */
2867 if (drop_rsrc_vnode
) {
2868 /* drop iocount on rsrc fork, was obtained at beginning of fxn */
2877 hfs_removefile_callback(struct buf
*bp
, void *hfsmp
) {
2879 if ( !(buf_flags(bp
) & B_META
))
2880 panic("hfs: symlink bp @ %p is not marked meta-data!\n", bp
);
2882 * it's part of the current transaction, kill it.
2884 journal_kill_block(((struct hfsmount
*)hfsmp
)->jnl
, bp
);
2886 return (BUF_CLAIMED
);
2892 * Similar to hfs_vnop_remove except there are additional options.
2893 * This function may be used to remove directories if they have
2894 * lots of EA's -- note the 'allow_dirs' argument.
2896 * The 'rvp' argument is used to pass in a resource fork vnode with
2897 * an iocount to prevent it from getting recycled during usage. If it
2898 * is NULL, then it is assumed the caller is a VNOP that cannot operate
2899 * on resource forks, like hfs_vnop_symlink or hfs_removedir. Otherwise in
2900 * a VNOP that takes multiple vnodes, we could violate lock order and
2903 * Requires cnode and truncate locks to be held.
2906 hfs_removefile(struct vnode
*dvp
, struct vnode
*vp
, struct componentname
*cnp
,
2907 int flags
, int skip_reserve
, int allow_dirs
,
2908 struct vnode
*rvp
, int only_unlink
)
2912 struct hfsmount
*hfsmp
;
2913 struct cat_desc desc
;
2915 int dataforkbusy
= 0;
2916 int rsrcforkbusy
= 0;
2920 int isbigfile
= 0, defer_remove
=0, isdir
=0;
2927 /* Check if we lost a race post lookup. */
2928 if (cp
->c_flag
& (C_NOEXISTS
| C_DELETED
)) {
2932 if (!hfs_valid_cnode(hfsmp
, dvp
, cnp
, cp
->c_fileid
, NULL
, &error
)) {
2936 /* Make sure a remove is permitted */
2937 if (VNODE_IS_RSRC(vp
)) {
2940 /* Don't allow deleting the journal or journal_info_block. */
2942 (cp
->c_fileid
== hfsmp
->hfs_jnlfileid
|| cp
->c_fileid
== hfsmp
->hfs_jnlinfoblkid
)) {
2946 * Hard links require special handling.
2948 if (cp
->c_flag
& C_HARDLINK
) {
2949 if ((flags
& VNODE_REMOVE_NODELETEBUSY
) && vnode_isinuse(vp
, 0)) {
2952 /* A directory hard link with a link count of one is
2953 * treated as a regular directory. Therefore it should
2954 * only be removed using rmdir().
2956 if ((vnode_isdir(vp
) == 1) && (cp
->c_linkcount
== 1) &&
2957 (allow_dirs
== 0)) {
2960 return hfs_unlink(hfsmp
, dvp
, vp
, cnp
, skip_reserve
);
2963 /* Directories should call hfs_rmdir! (unless they have a lot of attributes) */
2964 if (vnode_isdir(vp
)) {
2965 if (allow_dirs
== 0)
2966 return (EPERM
); /* POSIX */
2969 /* Sanity check the parent ids. */
2970 if ((cp
->c_parentcnid
!= hfsmp
->hfs_private_desc
[FILE_HARDLINKS
].cd_cnid
) &&
2971 (cp
->c_parentcnid
!= dcp
->c_fileid
)) {
2975 dcp
->c_flag
|= C_DIR_MODIFICATION
;
2977 // this guy is going away so mark him as such
2978 cp
->c_flag
|= C_DELETED
;
2981 /* Remove our entry from the namei cache. */
2985 * We expect the caller, if operating on files,
2986 * will have passed in a resource fork vnode with
2987 * an iocount, even if there was no content.
2988 * We only do the hfs_truncate on the rsrc fork
2989 * if we know that it DID have content, however.
2990 * This has the bonus of not requiring us to defer
2991 * its removal, unless it is in use.
2994 /* Check if this file is being used. */
2996 dataforkbusy
= vnode_isinuse(vp
, 0);
2997 /* Only need to defer resource fork removal if in use and has content */
2998 if (rvp
&& (cp
->c_blocks
- VTOF(vp
)->ff_blocks
)) {
2999 rsrcforkbusy
= vnode_isinuse(rvp
, 0);
3003 /* Check if we have to break the deletion into multiple pieces. */
3005 isbigfile
= ((cp
->c_datafork
->ff_size
>= HFS_BIGFILE_SIZE
) && overflow_extents(VTOF(vp
)));
3008 /* Check if the file has xattrs. If it does we'll have to delete them in
3009 individual transactions in case there are too many */
3010 if ((hfsmp
->hfs_attribute_vp
!= NULL
) &&
3011 (cp
->c_attr
.ca_recflags
& kHFSHasAttributesMask
) != 0) {
3015 /* If we are explicitly told to only unlink item and move to hidden dir, then do it */
3021 * Carbon semantics prohibit deleting busy files.
3022 * (enforced when VNODE_REMOVE_NODELETEBUSY is requested)
3024 if (dataforkbusy
|| rsrcforkbusy
) {
3025 if ((flags
& VNODE_REMOVE_NODELETEBUSY
) ||
3026 (hfsmp
->hfs_private_desc
[FILE_HARDLINKS
].cd_cnid
== 0)) {
3033 if (hfsmp
->hfs_flags
& HFS_QUOTAS
)
3034 (void)hfs_getinoquota(cp
);
3038 * Do a ubc_setsize to indicate we need to wipe contents if:
3039 * 1) item is a regular file.
3040 * 2) Neither fork is busy AND we are not told to unlink this.
3042 * We need to check for the defer_remove since it can be set without
3043 * having a busy data or rsrc fork
3045 if (isdir
== 0 && (!dataforkbusy
|| !rsrcforkbusy
) && (defer_remove
== 0)) {
3047 * A ubc_setsize can cause a pagein so defer it
3048 * until after the cnode lock is dropped. The
3049 * cnode lock cannot be dropped/reacquired here
3050 * since we might already hold the journal lock.
3052 if (!dataforkbusy
&& cp
->c_datafork
->ff_blocks
&& !isbigfile
) {
3053 cp
->c_flag
|= C_NEED_DATA_SETSIZE
;
3055 if (!rsrcforkbusy
&& rvp
) {
3056 cp
->c_flag
|= C_NEED_RSRC_SETSIZE
;
3060 if ((error
= hfs_start_transaction(hfsmp
)) != 0) {
3065 // XXXdbg - if we're journaled, kill any dirty symlink buffers
3066 if (hfsmp
->jnl
&& vnode_islnk(vp
) && (defer_remove
== 0)) {
3067 buf_iterate(vp
, hfs_removefile_callback
, BUF_SKIP_NONLOCKED
, (void *)hfsmp
);
3071 * Prepare to truncate any non-busy forks. Busy forks will
3072 * get truncated when their vnode goes inactive.
3073 * Note that we will only enter this region if we
3074 * can avoid creating an open-unlinked file. If
3075 * either region is busy, we will have to create an open
3078 * Since we are deleting the file, we need to stagger the runtime
3079 * modifications to do things in such a way that a crash won't
3080 * result in us getting overlapped extents or any other
3081 * bad inconsistencies. As such, we call prepare_release_storage
3082 * which updates the UBC, updates quota information, and releases
3083 * any loaned blocks that belong to this file. No actual
3084 * truncation or bitmap manipulation is done until *AFTER*
3085 * the catalog record is removed.
3087 if (isdir
== 0 && (!dataforkbusy
&& !rsrcforkbusy
) && (only_unlink
== 0)) {
3089 if (!dataforkbusy
&& !isbigfile
&& cp
->c_datafork
->ff_blocks
!= 0) {
3091 error
= hfs_prepare_release_storage (hfsmp
, vp
);
3097 if (!rsrcforkbusy
&& rvp
) {
3098 error
= hfs_prepare_release_storage (hfsmp
, rvp
);
3107 * Protect against a race with rename by using the component
3108 * name passed in and parent id from dvp (instead of using
3109 * the cp->c_desc which may have changed). Also, be aware that
3110 * because we allow directories to be passed in, we need to special case
3111 * this temporary descriptor in case we were handed a directory.
3114 desc
.cd_flags
= CD_ISDIR
;
3119 desc
.cd_encoding
= cp
->c_desc
.cd_encoding
;
3120 desc
.cd_nameptr
= (const u_int8_t
*)cnp
->cn_nameptr
;
3121 desc
.cd_namelen
= cnp
->cn_namelen
;
3122 desc
.cd_parentcnid
= dcp
->c_fileid
;
3123 desc
.cd_hint
= cp
->c_desc
.cd_hint
;
3124 desc
.cd_cnid
= cp
->c_cnid
;
3128 * There are two cases to consider:
3129 * 1. File/Dir is busy/big/defer_remove ==> move/rename the file/dir
3130 * 2. File is not in use ==> remove the file
3132 * We can get a directory in case 1 because it may have had lots of attributes,
3133 * which need to get removed here.
3135 if (dataforkbusy
|| rsrcforkbusy
|| isbigfile
|| defer_remove
) {
3137 struct cat_desc to_desc
;
3138 struct cat_desc todir_desc
;
3141 * Orphan this file or directory (move to hidden directory).
3142 * Again, we need to take care that we treat directories as directories,
3143 * and files as files. Because directories with attributes can be passed in
3144 * check to make sure that we have a directory or a file before filling in the
3145 * temporary descriptor's flags. We keep orphaned directories AND files in
3146 * the FILE_HARDLINKS private directory since we're generalizing over all
3147 * orphaned filesystem objects.
3149 bzero(&todir_desc
, sizeof(todir_desc
));
3150 todir_desc
.cd_parentcnid
= 2;
3152 MAKE_DELETED_NAME(delname
, sizeof(delname
), cp
->c_fileid
);
3153 bzero(&to_desc
, sizeof(to_desc
));
3154 to_desc
.cd_nameptr
= (const u_int8_t
*)delname
;
3155 to_desc
.cd_namelen
= strlen(delname
);
3156 to_desc
.cd_parentcnid
= hfsmp
->hfs_private_desc
[FILE_HARDLINKS
].cd_cnid
;
3158 to_desc
.cd_flags
= CD_ISDIR
;
3161 to_desc
.cd_flags
= 0;
3163 to_desc
.cd_cnid
= cp
->c_cnid
;
3165 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_EXCLUSIVE_LOCK
);
3166 if (!skip_reserve
) {
3167 if ((error
= cat_preflight(hfsmp
, CAT_RENAME
, NULL
, 0))) {
3168 hfs_systemfile_unlock(hfsmp
, lockflags
);
3173 error
= cat_rename(hfsmp
, &desc
, &todir_desc
,
3174 &to_desc
, (struct cat_desc
*)NULL
);
3177 hfsmp
->hfs_private_attr
[FILE_HARDLINKS
].ca_entries
++;
3179 INC_FOLDERCOUNT(hfsmp
, hfsmp
->hfs_private_attr
[FILE_HARDLINKS
]);
3181 (void) cat_update(hfsmp
, &hfsmp
->hfs_private_desc
[FILE_HARDLINKS
],
3182 &hfsmp
->hfs_private_attr
[FILE_HARDLINKS
], NULL
, NULL
);
3184 /* Update the parent directory */
3185 if (dcp
->c_entries
> 0)
3188 DEC_FOLDERCOUNT(hfsmp
, dcp
->c_attr
);
3190 dcp
->c_dirchangecnt
++;
3191 dcp
->c_ctime
= tv
.tv_sec
;
3192 dcp
->c_mtime
= tv
.tv_sec
;
3193 (void) cat_update(hfsmp
, &dcp
->c_desc
, &dcp
->c_attr
, NULL
, NULL
);
3195 /* Update the file or directory's state */
3196 cp
->c_flag
|= C_DELETED
;
3197 cp
->c_ctime
= tv
.tv_sec
;
3199 (void) cat_update(hfsmp
, &to_desc
, &cp
->c_attr
, NULL
, NULL
);
3201 hfs_systemfile_unlock(hfsmp
, lockflags
);
3206 else /* Not busy */ {
3210 int blksize
= hfsmp
->blockSize
;
3212 u_int32_t fileid
= cp
->c_fileid
;
3214 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
| SFL_ATTRIBUTE
| SFL_BITMAP
, HFS_EXCLUSIVE_LOCK
);
3215 if (!skip_reserve
) {
3216 if ((error
= cat_preflight(hfsmp
, CAT_DELETE
, NULL
, 0))) {
3217 hfs_systemfile_unlock(hfsmp
, lockflags
);
3222 error
= cat_delete(hfsmp
, &desc
, &cp
->c_attr
);
3224 if (error
&& error
!= ENXIO
&& error
!= ENOENT
) {
3225 printf("hfs_removefile: deleting file %s (%d), err: %d\n",
3226 cp
->c_desc
.cd_nameptr
, cp
->c_attr
.ca_fileid
, error
);
3230 /* Update the parent directory */
3231 if (dcp
->c_entries
> 0)
3233 dcp
->c_dirchangecnt
++;
3234 dcp
->c_ctime
= tv
.tv_sec
;
3235 dcp
->c_mtime
= tv
.tv_sec
;
3236 (void) cat_update(hfsmp
, &dcp
->c_desc
, &dcp
->c_attr
, NULL
, NULL
);
3238 hfs_systemfile_unlock(hfsmp
, lockflags
);
3244 * Now that we've wiped out the catalog record, the file effectively doesn't
3245 * exist anymore. So update the quota records to reflect the loss of the
3246 * data fork and the resource fork.
3249 if (cp
->c_datafork
->ff_blocks
> 0) {
3250 savedbytes
= ((off_t
)cp
->c_datafork
->ff_blocks
* (off_t
)blksize
);
3251 (void) hfs_chkdq(cp
, (int64_t)-(savedbytes
), NOCRED
, 0);
3254 if (cp
->c_rsrcfork
&& (cp
->c_rsrcfork
->ff_blocks
> 0)) {
3255 savedbytes
= ((off_t
)cp
->c_rsrcfork
->ff_blocks
* (off_t
)blksize
);
3256 (void) hfs_chkdq(cp
, (int64_t)-(savedbytes
), NOCRED
, 0);
3259 if (hfsmp
->hfs_flags
& HFS_QUOTAS
) {
3260 (void)hfs_chkiq(cp
, -1, NOCRED
, 0);
3266 * If we didn't get any errors deleting the catalog entry, then go ahead
3267 * and release the backing store now. The filefork pointers are still valid.
3269 error
= hfs_release_storage (hfsmp
, cp
->c_datafork
, cp
->c_rsrcfork
, fileid
);
3273 * If we encountered an error updating the extents and bitmap,
3274 * mark the volume inconsistent. At this point, the catalog record has
3275 * already been deleted, so we can't recover it at this point. We need
3276 * to proceed and update the volume header and mark the cnode C_NOEXISTS.
3277 * The subsequent fsck should be able to recover the free space for us.
3279 hfs_mark_volume_inconsistent(hfsmp
);
3282 /* reset update_vh to 0, since hfs_release_storage should have done it for us */
3286 cp
->c_flag
|= C_NOEXISTS
;
3287 cp
->c_flag
&= ~C_DELETED
;
3289 cp
->c_touch_chgtime
= TRUE
; /* XXX needed ? */
3293 * We must never get a directory if we're in this else block. We could
3294 * accidentally drop the number of files in the volume header if we did.
3296 hfs_volupdate(hfsmp
, VOL_RMFILE
, (dcp
->c_cnid
== kHFSRootFolderID
));
3301 * All done with this cnode's descriptor...
3303 * Note: all future catalog calls for this cnode must be by
3304 * fileid only. This is OK for HFS (which doesn't have file
3305 * thread records) since HFS doesn't support the removal of
3308 cat_releasedesc(&cp
->c_desc
);
3312 cp
->c_flag
&= ~C_DELETED
;
3317 * If we bailed out earlier, we may need to update the volume header
3318 * to deal with the borrowed blocks accounting.
3320 hfs_volupdate (hfsmp
, VOL_UPDATE
, 0);
3324 hfs_end_transaction(hfsmp
);
3327 dcp
->c_flag
&= ~C_DIR_MODIFICATION
;
3328 wakeup((caddr_t
)&dcp
->c_flag
);
3334 __private_extern__
void
3335 replace_desc(struct cnode
*cp
, struct cat_desc
*cdp
)
3337 // fixes 4348457 and 4463138
3338 if (&cp
->c_desc
== cdp
) {
3342 /* First release allocated name buffer */
3343 if (cp
->c_desc
.cd_flags
& CD_HASBUF
&& cp
->c_desc
.cd_nameptr
!= 0) {
3344 const u_int8_t
*name
= cp
->c_desc
.cd_nameptr
;
3346 cp
->c_desc
.cd_nameptr
= 0;
3347 cp
->c_desc
.cd_namelen
= 0;
3348 cp
->c_desc
.cd_flags
&= ~CD_HASBUF
;
3349 vfs_removename((const char *)name
);
3351 bcopy(cdp
, &cp
->c_desc
, sizeof(cp
->c_desc
));
3353 /* Cnode now owns the name buffer */
3354 cdp
->cd_nameptr
= 0;
3355 cdp
->cd_namelen
= 0;
3356 cdp
->cd_flags
&= ~CD_HASBUF
;
3362 * The VFS layer guarantees that:
3363 * - source and destination will either both be directories, or
3364 * both not be directories.
3365 * - all the vnodes are from the same file system
3367 * When the target is a directory, HFS must ensure that its empty.
3369 * Note that this function requires up to 6 vnodes in order to work properly
3370 * if it is operating on files (and not on directories). This is because only
3371 * files can have resource forks, and we now require iocounts to be held on the
3372 * vnodes corresponding to the resource forks (if applicable) as well as
3373 * the files or directories undergoing rename. The problem with not holding
3374 * iocounts on the resource fork vnodes is that it can lead to a deadlock
3375 * situation: The rsrc fork of the source file may be recycled and reclaimed
3376 * in order to provide a vnode for the destination file's rsrc fork. Since
3377 * data and rsrc forks share the same cnode, we'd eventually try to lock the
3378 * source file's cnode in order to sync its rsrc fork to disk, but it's already
3379 * been locked. By taking the rsrc fork vnodes up front we ensure that they
3380 * cannot be recycled, and that the situation mentioned above cannot happen.
3384 struct vnop_rename_args
/* {
3385 struct vnode *a_fdvp;
3386 struct vnode *a_fvp;
3387 struct componentname *a_fcnp;
3388 struct vnode *a_tdvp;
3389 struct vnode *a_tvp;
3390 struct componentname *a_tcnp;
3391 vfs_context_t a_context;
3394 struct vnode
*tvp
= ap
->a_tvp
;
3395 struct vnode
*tdvp
= ap
->a_tdvp
;
3396 struct vnode
*fvp
= ap
->a_fvp
;
3397 struct vnode
*fdvp
= ap
->a_fdvp
;
3398 struct vnode
*fvp_rsrc
= NULLVP
;
3399 struct vnode
*tvp_rsrc
= NULLVP
;
3400 struct componentname
*tcnp
= ap
->a_tcnp
;
3401 struct componentname
*fcnp
= ap
->a_fcnp
;
3402 struct proc
*p
= vfs_context_proc(ap
->a_context
);
3407 struct cnode
*error_cnode
;
3408 struct cat_desc from_desc
;
3409 struct cat_desc to_desc
;
3410 struct cat_desc out_desc
;
3411 struct hfsmount
*hfsmp
;
3412 cat_cookie_t cookie
;
3413 int tvp_deleted
= 0;
3414 int started_tr
= 0, got_cookie
= 0;
3415 int took_trunc_lock
= 0;
3418 time_t orig_from_ctime
, orig_to_ctime
;
3419 int emit_rename
= 1;
3420 int emit_delete
= 1;
3422 orig_from_ctime
= VTOC(fvp
)->c_ctime
;
3423 if (tvp
&& VTOC(tvp
)) {
3424 orig_to_ctime
= VTOC(tvp
)->c_ctime
;
3429 hfsmp
= VTOHFS(tdvp
);
3431 * Do special case checks here. If fvp == tvp then we need to check the
3432 * cnode with locks held.
3435 int is_hardlink
= 0;
3437 * In this case, we do *NOT* ever emit a DELETE event.
3438 * We may not necessarily emit a RENAME event
3441 if ((error
= hfs_lock(VTOC(fvp
), HFS_SHARED_LOCK
))) {
3444 /* Check to see if the item is a hardlink or not */
3445 is_hardlink
= (VTOC(fvp
)->c_flag
& C_HARDLINK
);
3446 hfs_unlock (VTOC(fvp
));
3449 * If the item is not a hardlink, then case sensitivity must be off, otherwise
3450 * two names should not resolve to the same cnode unless they were case variants.
3455 * Hardlinks are a little trickier. We only want to emit a rename event
3456 * if the item is a hardlink, the parent directories are the same, case sensitivity
3457 * is off, and the case folded names are the same. See the fvp == tvp case below for more
3461 if ((fdvp
== tdvp
) && ((hfsmp
->hfs_flags
& HFS_CASE_SENSITIVE
) == 0)) {
3462 if (hfs_namecmp((const u_int8_t
*)fcnp
->cn_nameptr
, fcnp
->cn_namelen
,
3463 (const u_int8_t
*)tcnp
->cn_nameptr
, tcnp
->cn_namelen
) == 0) {
3464 /* Then in this case only it is ok to emit a rename */
3471 check_for_tracked_file(fvp
, orig_from_ctime
, NAMESPACE_HANDLER_RENAME_OP
, NULL
);
3474 if (tvp
&& VTOC(tvp
)) {
3476 check_for_tracked_file(tvp
, orig_to_ctime
, NAMESPACE_HANDLER_DELETE_OP
, NULL
);
3481 * Before grabbing the four locks, we may need to get an iocount on the resource fork
3482 * vnodes in question, just like hfs_vnop_remove. If fvp and tvp are not
3483 * directories, then go ahead and grab the resource fork vnodes now
3484 * one at a time. We don't actively need the fvp_rsrc to do the rename operation,
3485 * but we need the iocount to prevent the vnode from getting recycled/reclaimed
3486 * during the middle of the VNOP.
3490 if ((vnode_isreg(fvp
)) || (vnode_islnk(fvp
))) {
3492 if ((error
= hfs_lock (VTOC(fvp
), HFS_EXCLUSIVE_LOCK
))) {
3496 * We care if we race against rename/delete with this cp, so we'll error out
3497 * if the file becomes open-unlinked during this call.
3499 error
= hfs_vgetrsrc(VTOHFS(fvp
), fvp
, &fvp_rsrc
, TRUE
, TRUE
);
3500 hfs_unlock (VTOC(fvp
));
3503 vnode_put(fvp_rsrc
);
3509 if (tvp
&& (vnode_isreg(tvp
) || vnode_islnk(tvp
))) {
3511 * Lock failure is OK on tvp, since we may race with a remove on the dst.
3512 * But this shouldn't stop rename from proceeding, so only try to
3513 * grab the resource fork if the lock succeeded.
3515 if (hfs_lock (VTOC(tvp
), HFS_EXCLUSIVE_LOCK
) == 0) {
3518 * We only care if we get an open-unlinked file on the dst so we
3519 * know to null out tvp/tcp to make the rename operation act
3520 * as if they never existed. Because they're effectively out of the
3521 * namespace already it's fine to do this. If this is true, then
3522 * make sure to unlock the cnode and drop the iocount only after the unlock.
3525 error
= hfs_vgetrsrc(VTOHFS(tvp
), tvp
, &tvp_rsrc
, TRUE
, TRUE
);
3529 * Since we specify TRUE for error_on_unlinked in hfs_vgetrsrc,
3530 * we can get a rsrc fork vnode even if it returns an error.
3535 vnode_put (tvp_rsrc
);
3538 /* just bypass truncate lock and act as if we never got tcp/tvp */
3544 /* When tvp exists, take the truncate lock for hfs_removefile(). */
3545 if (tvp
&& (vnode_isreg(tvp
) || vnode_islnk(tvp
))) {
3546 hfs_lock_truncate(VTOC(tvp
), HFS_EXCLUSIVE_LOCK
);
3547 took_trunc_lock
= 1;
3551 error
= hfs_lockfour(VTOC(fdvp
), VTOC(fvp
), VTOC(tdvp
), tvp
? VTOC(tvp
) : NULL
,
3552 HFS_EXCLUSIVE_LOCK
, &error_cnode
);
3554 if (took_trunc_lock
) {
3555 hfs_unlock_truncate(VTOC(tvp
), 0);
3556 took_trunc_lock
= 0;
3559 * tvp might no longer exist. If the cause of the lock failure
3560 * was tvp, then we can try again with tvp/tcp set to NULL.
3561 * This is ok because the vfs syscall will vnode_put the vnodes
3562 * after we return from hfs_vnop_rename.
3564 if ((error
== ENOENT
) && (tvp
!= NULL
) && (error_cnode
== VTOC(tvp
))) {
3569 /* otherwise, drop iocounts on the rsrc forks and bail out */
3571 vnode_put (fvp_rsrc
);
3574 vnode_put (tvp_rsrc
);
3582 tcp
= tvp
? VTOC(tvp
) : NULL
;
3584 /* Ensure we didn't race src or dst parent directories with rmdir. */
3585 if (fdcp
->c_flag
& (C_NOEXISTS
| C_DELETED
)) {
3590 if (tdcp
->c_flag
& (C_NOEXISTS
| C_DELETED
)) {
3596 /* Check for a race against unlink. The hfs_valid_cnode checks validate
3597 * the parent/child relationship with fdcp and tdcp, as well as the
3598 * component name of the target cnodes.
3600 if ((fcp
->c_flag
& (C_NOEXISTS
| C_DELETED
)) || !hfs_valid_cnode(hfsmp
, fdvp
, fcnp
, fcp
->c_fileid
, NULL
, &error
)) {
3605 if (tcp
&& ((tcp
->c_flag
& (C_NOEXISTS
| C_DELETED
)) || !hfs_valid_cnode(hfsmp
, tdvp
, tcnp
, tcp
->c_fileid
, NULL
, &error
))) {
3607 // hmm, the destination vnode isn't valid any more.
3608 // in this case we can just drop him and pretend he
3609 // never existed in the first place.
3611 if (took_trunc_lock
) {
3612 hfs_unlock_truncate(VTOC(tvp
), 0);
3613 took_trunc_lock
= 0;
3617 hfs_unlockfour(fdcp
, fcp
, tdcp
, tcp
);
3622 // retry the locking with tvp null'ed out
3626 fdcp
->c_flag
|= C_DIR_MODIFICATION
;
3628 tdcp
->c_flag
|= C_DIR_MODIFICATION
;
3632 * Disallow renaming of a directory hard link if the source and
3633 * destination parent directories are different, or a directory whose
3634 * descendant is a directory hard link and the one of the ancestors
3635 * of the destination directory is a directory hard link.
3637 if (vnode_isdir(fvp
) && (fdvp
!= tdvp
)) {
3638 if (fcp
->c_flag
& C_HARDLINK
) {
3642 if (fcp
->c_attr
.ca_recflags
& kHFSHasChildLinkMask
) {
3643 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
3644 if (cat_check_link_ancestry(hfsmp
, tdcp
->c_fileid
, 0)) {
3646 hfs_systemfile_unlock(hfsmp
, lockflags
);
3649 hfs_systemfile_unlock(hfsmp
, lockflags
);
3654 * The following edge case is caught here:
3655 * (to cannot be a descendent of from)
3668 if (tdcp
->c_parentcnid
== fcp
->c_fileid
) {
3674 * The following two edge cases are caught here:
3675 * (note tvp is not empty)
3688 if (tvp
&& vnode_isdir(tvp
) && (tcp
->c_entries
!= 0) && fvp
!= tvp
) {
3694 * The following edge case is caught here:
3695 * (the from child and parent are the same)
3708 * Make sure "from" vnode and its parent are changeable.
3710 if ((fcp
->c_flags
& (IMMUTABLE
| APPEND
)) || (fdcp
->c_flags
& APPEND
)) {
3716 * If the destination parent directory is "sticky", then the
3717 * user must own the parent directory, or the destination of
3718 * the rename, otherwise the destination may not be changed
3719 * (except by root). This implements append-only directories.
3721 * Note that checks for immutable and write access are done
3722 * by the call to hfs_removefile.
3724 if (tvp
&& (tdcp
->c_mode
& S_ISTXT
) &&
3725 (suser(vfs_context_ucred(tcnp
->cn_context
), NULL
)) &&
3726 (kauth_cred_getuid(vfs_context_ucred(tcnp
->cn_context
)) != tdcp
->c_uid
) &&
3727 (hfs_owner_rights(hfsmp
, tcp
->c_uid
, vfs_context_ucred(tcnp
->cn_context
), p
, false)) ) {
3734 (void)hfs_getinoquota(tcp
);
3736 /* Preflighting done, take fvp out of the name space. */
3739 bzero(&from_desc
, sizeof(from_desc
));
3740 from_desc
.cd_nameptr
= (const u_int8_t
*)fcnp
->cn_nameptr
;
3741 from_desc
.cd_namelen
= fcnp
->cn_namelen
;
3742 from_desc
.cd_parentcnid
= fdcp
->c_fileid
;
3743 from_desc
.cd_flags
= fcp
->c_desc
.cd_flags
& ~(CD_HASBUF
| CD_DECOMPOSED
);
3744 from_desc
.cd_cnid
= fcp
->c_cnid
;
3746 bzero(&to_desc
, sizeof(to_desc
));
3747 to_desc
.cd_nameptr
= (const u_int8_t
*)tcnp
->cn_nameptr
;
3748 to_desc
.cd_namelen
= tcnp
->cn_namelen
;
3749 to_desc
.cd_parentcnid
= tdcp
->c_fileid
;
3750 to_desc
.cd_flags
= fcp
->c_desc
.cd_flags
& ~(CD_HASBUF
| CD_DECOMPOSED
);
3751 to_desc
.cd_cnid
= fcp
->c_cnid
;
3753 if ((error
= hfs_start_transaction(hfsmp
)) != 0) {
3758 /* hfs_vnop_link() and hfs_vnop_rename() set kHFSHasChildLinkMask
3759 * inside a journal transaction and without holding a cnode lock.
3760 * As setting of this bit depends on being in journal transaction for
3761 * concurrency, check this bit again after we start journal transaction for rename
3762 * to ensure that this directory does not have any descendant that
3763 * is a directory hard link.
3765 if (vnode_isdir(fvp
) && (fdvp
!= tdvp
)) {
3766 if (fcp
->c_attr
.ca_recflags
& kHFSHasChildLinkMask
) {
3767 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
3768 if (cat_check_link_ancestry(hfsmp
, tdcp
->c_fileid
, 0)) {
3770 hfs_systemfile_unlock(hfsmp
, lockflags
);
3773 hfs_systemfile_unlock(hfsmp
, lockflags
);
3777 // if it's a hardlink then re-lookup the name so
3778 // that we get the correct cnid in from_desc (see
3779 // the comment in hfs_removefile for more details)
3781 if (fcp
->c_flag
& C_HARDLINK
) {
3782 struct cat_desc tmpdesc
;
3785 tmpdesc
.cd_nameptr
= (const u_int8_t
*)fcnp
->cn_nameptr
;
3786 tmpdesc
.cd_namelen
= fcnp
->cn_namelen
;
3787 tmpdesc
.cd_parentcnid
= fdcp
->c_fileid
;
3788 tmpdesc
.cd_hint
= fdcp
->c_childhint
;
3789 tmpdesc
.cd_flags
= fcp
->c_desc
.cd_flags
& CD_ISDIR
;
3790 tmpdesc
.cd_encoding
= 0;
3792 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
3794 if (cat_lookup(hfsmp
, &tmpdesc
, 0, NULL
, NULL
, NULL
, &real_cnid
) != 0) {
3795 hfs_systemfile_unlock(hfsmp
, lockflags
);
3799 // use the real cnid instead of whatever happened to be there
3800 from_desc
.cd_cnid
= real_cnid
;
3801 hfs_systemfile_unlock(hfsmp
, lockflags
);
3805 * Reserve some space in the Catalog file.
3807 if ((error
= cat_preflight(hfsmp
, CAT_RENAME
+ CAT_DELETE
, &cookie
, p
))) {
3813 * If the destination exists then it may need to be removed.
3815 * Due to HFS's locking system, we should always move the
3816 * existing 'tvp' element to the hidden directory in hfs_vnop_rename.
3817 * Because the VNOP_LOOKUP call enters and exits the filesystem independently
3818 * of the actual vnop that it was trying to do (stat, link, readlink),
3819 * we must release the cnode lock of that element during the interim to
3820 * do MAC checking, vnode authorization, and other calls. In that time,
3821 * the item can be deleted (or renamed over). However, only in the rename
3822 * case is it inappropriate to return ENOENT from any of those calls. Either
3823 * the call should return information about the old element (stale), or get
3824 * information about the newer element that we are about to write in its place.
3826 * HFS lookup has been modified to detect a rename and re-drive its
3827 * lookup internally. For other calls that have already succeeded in
3828 * their lookup call and are waiting to acquire the cnode lock in order
3829 * to proceed, that cnode lock will not fail due to the cnode being marked
3830 * C_NOEXISTS, because it won't have been marked as such. It will only
3831 * have C_DELETED. Thus, they will simply act on the stale open-unlinked
3832 * element. All future callers will get the new element.
3834 * To implement this behavior, we pass the "only_unlink" argument to
3835 * hfs_removefile and hfs_removedir. This will result in the vnode acting
3836 * as though it is open-unlinked. Additionally, when we are done moving the
3837 * element to the hidden directory, we vnode_recycle the target so that it is
3838 * reclaimed as soon as possible. Reclaim and inactive are both
3839 * capable of clearing out unused blocks for an open-unlinked file or dir.
3843 * When fvp matches tvp they could be case variants
3844 * or matching hard links.
3847 if (!(fcp
->c_flag
& C_HARDLINK
)) {
3849 * If they're not hardlinks, then fvp == tvp must mean we
3850 * are using case-insensitive HFS because case-sensitive would
3851 * not use the same vnode for both. In this case we just update
3852 * the catalog for: a -> A
3854 goto skip_rm
; /* simple case variant */
3857 /* For all cases below, we must be using hardlinks */
3858 else if ((fdvp
!= tdvp
) ||
3859 (hfsmp
->hfs_flags
& HFS_CASE_SENSITIVE
)) {
3861 * If the parent directories are not the same, AND the two items
3862 * are hardlinks, posix says to do nothing:
3863 * dir1/fred <-> dir2/bob and the op was mv dir1/fred -> dir2/bob
3864 * We just return 0 in this case.
3866 * If case sensitivity is on, and we are using hardlinks
3867 * then renaming is supposed to do nothing.
3868 * dir1/fred <-> dir2/FRED, and op == mv dir1/fred -> dir2/FRED
3870 goto out
; /* matching hardlinks, nothing to do */
3872 } else if (hfs_namecmp((const u_int8_t
*)fcnp
->cn_nameptr
, fcnp
->cn_namelen
,
3873 (const u_int8_t
*)tcnp
->cn_nameptr
, tcnp
->cn_namelen
) == 0) {
3875 * If we get here, then the following must be true:
3876 * a) We are running case-insensitive HFS+.
3877 * b) Both paths 'fvp' and 'tvp' are in the same parent directory.
3878 * c) the two names are case-variants of each other.
3880 * In this case, we are really only dealing with a single catalog record
3881 * whose name is being updated.
3883 * op is dir1/fred -> dir1/FRED
3885 * We need to special case the name matching, because if
3886 * dir1/fred <-> dir1/bob were the two links, and the
3887 * op was dir1/fred -> dir1/bob
3888 * That would fail/do nothing.
3890 goto skip_rm
; /* case-variant hardlink in the same dir */
3892 goto out
; /* matching hardlink, nothing to do */
3897 if (vnode_isdir(tvp
)) {
3899 * hfs_removedir will eventually call hfs_removefile on the directory
3900 * we're working on, because only hfs_removefile does the renaming of the
3901 * item to the hidden directory. The directory will stay around in the
3902 * hidden directory with C_DELETED until it gets an inactive or a reclaim.
3903 * That way, we can destroy all of the EAs as needed and allow new ones to be
3906 error
= hfs_removedir(tdvp
, tvp
, tcnp
, HFSRM_SKIP_RESERVE
, 1);
3909 error
= hfs_removefile(tdvp
, tvp
, tcnp
, 0, HFSRM_SKIP_RESERVE
, 0, tvp_rsrc
, 1);
3912 * If the destination file had a resource fork vnode, then we need to get rid of
3913 * its blocks when there are no more references to it. Because the call to
3914 * hfs_removefile above always open-unlinks things, we need to force an inactive/reclaim
3915 * on the resource fork vnode, in order to prevent block leaks. Otherwise,
3916 * the resource fork vnode could prevent the data fork vnode from going out of scope
3917 * because it holds a v_parent reference on it. So we mark it for termination
3918 * with a call to vnode_recycle. hfs_vnop_reclaim has been modified so that it
3919 * can clean up the blocks of open-unlinked files and resource forks.
3921 * We can safely call vnode_recycle on the resource fork because we took an iocount
3922 * reference on it at the beginning of the function.
3925 if ((error
== 0) && (tcp
->c_flag
& C_DELETED
) && (tvp_rsrc
)) {
3926 vnode_recycle(tvp_rsrc
);
3936 /* Mark 'tcp' as being deleted due to a rename */
3937 tcp
->c_flag
|= C_RENAMED
;
3940 * Aggressively mark tvp/tcp for termination to ensure that we recover all blocks
3941 * as quickly as possible.
3947 * All done with tvp and fvp.
3949 * We also jump to this point if there was no destination observed during lookup and namei.
3950 * However, because only iocounts are held at the VFS layer, there is nothing preventing a
3951 * competing thread from racing us and creating a file or dir at the destination of this rename
3952 * operation. If this occurs, it may cause us to get a spurious EEXIST out of the cat_rename
3953 * call below. To preserve rename's atomicity, we need to signal VFS to re-drive the
3954 * namei/lookup and restart the rename operation. EEXIST is an allowable errno to be bubbled
3955 * out of the rename syscall, but not for this reason, since it is a synonym errno for ENOTEMPTY.
3956 * To signal VFS, we return ERECYCLE (which is also used for lookup restarts). This errno
3957 * will be swallowed and it will restart the operation.
3960 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_EXCLUSIVE_LOCK
);
3961 error
= cat_rename(hfsmp
, &from_desc
, &tdcp
->c_desc
, &to_desc
, &out_desc
);
3962 hfs_systemfile_unlock(hfsmp
, lockflags
);
3965 if (error
== EEXIST
) {
3971 /* Invalidate negative cache entries in the destination directory */
3972 if (tdcp
->c_flag
& C_NEG_ENTRIES
) {
3973 cache_purge_negatives(tdvp
);
3974 tdcp
->c_flag
&= ~C_NEG_ENTRIES
;
3977 /* Update cnode's catalog descriptor */
3978 replace_desc(fcp
, &out_desc
);
3979 fcp
->c_parentcnid
= tdcp
->c_fileid
;
3982 /* Now indicate this cnode needs to have date-added written to the finderinfo */
3983 fcp
->c_flag
|= C_NEEDS_DATEADDED
;
3984 (void) hfs_update (fvp
, 0);
3987 hfs_volupdate(hfsmp
, vnode_isdir(fvp
) ? VOL_RMDIR
: VOL_RMFILE
,
3988 (fdcp
->c_cnid
== kHFSRootFolderID
));
3989 hfs_volupdate(hfsmp
, vnode_isdir(fvp
) ? VOL_MKDIR
: VOL_MKFILE
,
3990 (tdcp
->c_cnid
== kHFSRootFolderID
));
3992 /* Update both parent directories. */
3994 if (vnode_isdir(fvp
)) {
3995 /* If the source directory has directory hard link
3996 * descendants, set the kHFSHasChildLinkBit in the
3997 * destination parent hierarchy
3999 if ((fcp
->c_attr
.ca_recflags
& kHFSHasChildLinkMask
) &&
4000 !(tdcp
->c_attr
.ca_recflags
& kHFSHasChildLinkMask
)) {
4002 tdcp
->c_attr
.ca_recflags
|= kHFSHasChildLinkMask
;
4004 error
= cat_set_childlinkbit(hfsmp
, tdcp
->c_parentcnid
);
4006 printf ("hfs_vnop_rename: error updating parent chain for %u\n", tdcp
->c_cnid
);
4010 INC_FOLDERCOUNT(hfsmp
, tdcp
->c_attr
);
4011 DEC_FOLDERCOUNT(hfsmp
, fdcp
->c_attr
);
4014 tdcp
->c_dirchangecnt
++;
4015 if (fdcp
->c_entries
> 0)
4017 fdcp
->c_dirchangecnt
++;
4018 fdcp
->c_touch_chgtime
= TRUE
;
4019 fdcp
->c_touch_modtime
= TRUE
;
4021 fdcp
->c_flag
|= C_FORCEUPDATE
; // XXXdbg - force it out!
4022 (void) hfs_update(fdvp
, 0);
4024 tdcp
->c_childhint
= out_desc
.cd_hint
; /* Cache directory's location */
4025 tdcp
->c_touch_chgtime
= TRUE
;
4026 tdcp
->c_touch_modtime
= TRUE
;
4028 tdcp
->c_flag
|= C_FORCEUPDATE
; // XXXdbg - force it out!
4029 (void) hfs_update(tdvp
, 0);
4032 cat_postflight(hfsmp
, &cookie
, p
);
4035 hfs_end_transaction(hfsmp
);
4038 fdcp
->c_flag
&= ~C_DIR_MODIFICATION
;
4039 wakeup((caddr_t
)&fdcp
->c_flag
);
4041 tdcp
->c_flag
&= ~C_DIR_MODIFICATION
;
4042 wakeup((caddr_t
)&tdcp
->c_flag
);
4045 if (took_trunc_lock
) {
4046 hfs_unlock_truncate(VTOC(tvp
), 0);
4049 hfs_unlockfour(fdcp
, fcp
, tdcp
, tcp
);
4051 /* Now vnode_put the resource forks vnodes if necessary */
4053 vnode_put(tvp_rsrc
);
4056 vnode_put(fvp_rsrc
);
4059 /* After tvp is removed the only acceptable error is EIO */
4060 if (error
&& tvp_deleted
)
4071 hfs_vnop_mkdir(struct vnop_mkdir_args
*ap
)
4073 /***** HACK ALERT ********/
4074 ap
->a_cnp
->cn_flags
|= MAKEENTRY
;
4075 return hfs_makenode(ap
->a_dvp
, ap
->a_vpp
, ap
->a_cnp
, ap
->a_vap
, ap
->a_context
);
4080 * Create a symbolic link.
4083 hfs_vnop_symlink(struct vnop_symlink_args
*ap
)
4085 struct vnode
**vpp
= ap
->a_vpp
;
4086 struct vnode
*dvp
= ap
->a_dvp
;
4087 struct vnode
*vp
= NULL
;
4088 struct cnode
*cp
= NULL
;
4089 struct hfsmount
*hfsmp
;
4090 struct filefork
*fp
;
4091 struct buf
*bp
= NULL
;
4097 /* HFS standard disks don't support symbolic links */
4098 if (VTOVCB(dvp
)->vcbSigWord
!= kHFSPlusSigWord
)
4101 /* Check for empty target name */
4102 if (ap
->a_target
[0] == 0)
4105 hfsmp
= VTOHFS(dvp
);
4106 len
= strlen(ap
->a_target
);
4108 /* Check for free space */
4109 if (((u_int64_t
)hfs_freeblks(hfsmp
, 0) * (u_int64_t
)hfsmp
->blockSize
) < len
) {
4113 /* Create the vnode */
4114 ap
->a_vap
->va_mode
|= S_IFLNK
;
4115 if ((error
= hfs_makenode(dvp
, vpp
, ap
->a_cnp
, ap
->a_vap
, ap
->a_context
))) {
4119 if ((error
= hfs_lock(VTOC(vp
), HFS_EXCLUSIVE_LOCK
))) {
4125 if (cp
->c_flag
& (C_NOEXISTS
| C_DELETED
)) {
4130 (void)hfs_getinoquota(cp
);
4133 if ((error
= hfs_start_transaction(hfsmp
)) != 0) {
4139 * Allocate space for the link.
4141 * Since we're already inside a transaction,
4142 * tell hfs_truncate to skip the ubc_setsize.
4144 * Don't need truncate lock since a symlink is treated as a system file.
4146 error
= hfs_truncate(vp
, len
, IO_NOZEROFILL
, 1, 0, ap
->a_context
);
4148 /* On errors, remove the symlink file */
4151 * End the transaction so we don't re-take the cnode lock
4152 * below while inside a transaction (lock order violation).
4154 hfs_end_transaction(hfsmp
);
4156 /* hfs_removefile() requires holding the truncate lock */
4158 hfs_lock_truncate(cp
, HFS_EXCLUSIVE_LOCK
);
4159 hfs_lock(cp
, HFS_FORCE_LOCK
);
4161 if (hfs_start_transaction(hfsmp
) != 0) {
4163 hfs_unlock_truncate(cp
, TRUE
);
4167 (void) hfs_removefile(dvp
, vp
, ap
->a_cnp
, 0, 0, 0, NULL
, 0);
4168 hfs_unlock_truncate(cp
, 0);
4172 /* Write the link to disk */
4173 bp
= buf_getblk(vp
, (daddr64_t
)0, roundup((int)fp
->ff_size
, hfsmp
->hfs_physical_block_size
),
4176 journal_modify_block_start(hfsmp
->jnl
, bp
);
4178 datap
= (char *)buf_dataptr(bp
);
4179 bzero(datap
, buf_size(bp
));
4180 bcopy(ap
->a_target
, datap
, len
);
4183 journal_modify_block_end(hfsmp
->jnl
, bp
, NULL
, NULL
);
4188 * We defered the ubc_setsize for hfs_truncate
4189 * since we were inside a transaction.
4191 * We don't need to drop the cnode lock here
4192 * since this is a symlink.
4194 ubc_setsize(vp
, len
);
4197 hfs_end_transaction(hfsmp
);
4198 if ((cp
!= NULL
) && (vp
!= NULL
)) {
4211 /* structures to hold a "." or ".." directory entry */
4212 struct hfs_stddotentry
{
4213 u_int32_t d_fileno
; /* unique file number */
4214 u_int16_t d_reclen
; /* length of this structure */
4215 u_int8_t d_type
; /* dirent file type */
4216 u_int8_t d_namlen
; /* len of filename */
4217 char d_name
[4]; /* "." or ".." */
4220 struct hfs_extdotentry
{
4221 u_int64_t d_fileno
; /* unique file number */
4222 u_int64_t d_seekoff
; /* seek offset (optional, used by servers) */
4223 u_int16_t d_reclen
; /* length of this structure */
4224 u_int16_t d_namlen
; /* len of filename */
4225 u_int8_t d_type
; /* dirent file type */
4226 u_char d_name
[3]; /* "." or ".." */
4230 struct hfs_stddotentry std
;
4231 struct hfs_extdotentry ext
;
4235 * hfs_vnop_readdir reads directory entries into the buffer pointed
4236 * to by uio, in a filesystem independent format. Up to uio_resid
4237 * bytes of data can be transferred. The data in the buffer is a
4238 * series of packed dirent structures where each one contains the
4239 * following entries:
4241 * u_int32_t d_fileno; // file number of entry
4242 * u_int16_t d_reclen; // length of this record
4243 * u_int8_t d_type; // file type
4244 * u_int8_t d_namlen; // length of string in d_name
4245 * char d_name[MAXNAMELEN+1]; // null terminated file name
4247 * The current position (uio_offset) refers to the next block of
4248 * entries. The offset can only be set to a value previously
4249 * returned by hfs_vnop_readdir or zero. This offset does not have
4250 * to match the number of bytes returned (in uio_resid).
4252 * In fact, the offset used by HFS is essentially an index (26 bits)
4253 * with a tag (6 bits). The tag is for associating the next request
4254 * with the current request. This enables us to have multiple threads
4255 * reading the directory while the directory is also being modified.
4257 * Each tag/index pair is tied to a unique directory hint. The hint
4258 * contains information (filename) needed to build the catalog b-tree
4259 * key for finding the next set of entries.
4261 * If the directory is marked as deleted-but-in-use (cp->c_flag & C_DELETED),
4262 * do NOT synthesize entries for "." and "..".
4265 hfs_vnop_readdir(ap
)
4266 struct vnop_readdir_args
/* {
4272 vfs_context_t a_context;
4275 struct vnode
*vp
= ap
->a_vp
;
4276 uio_t uio
= ap
->a_uio
;
4278 struct hfsmount
*hfsmp
;
4279 directoryhint_t
*dirhint
= NULL
;
4280 directoryhint_t localhint
;
4285 user_addr_t user_start
= 0;
4286 user_size_t user_len
= 0;
4293 cnid_t cnid_hint
= 0;
4296 startoffset
= offset
= uio_offset(uio
);
4297 extended
= (ap
->a_flags
& VNODE_READDIR_EXTENDED
);
4298 nfs_cookies
= extended
&& (ap
->a_flags
& VNODE_READDIR_REQSEEKOFF
);
4300 /* Sanity check the uio data. */
4301 if (uio_iovcnt(uio
) > 1)
4304 if (VTOC(vp
)->c_flags
& UF_COMPRESSED
) {
4305 int compressed
= hfs_file_is_compressed(VTOC(vp
), 0); /* 0 == take the cnode lock */
4306 if (VTOCMP(vp
) != NULL
&& !compressed
) {
4307 error
= check_for_dataless_file(vp
, NAMESPACE_HANDLER_READ_OP
);
4317 /* Note that the dirhint calls require an exclusive lock. */
4318 if ((error
= hfs_lock(VTOC(vp
), HFS_EXCLUSIVE_LOCK
)))
4321 /* Pick up cnid hint (if any). */
4323 cnid_hint
= (cnid_t
)(uio_offset(uio
) >> 32);
4324 uio_setoffset(uio
, uio_offset(uio
) & 0x00000000ffffffffLL
);
4325 if (cnid_hint
== INT_MAX
) { /* searching pass the last item */
4331 * Synthesize entries for "." and "..", unless the directory has
4332 * been deleted, but not closed yet (lazy delete in progress).
4334 if (offset
== 0 && !(cp
->c_flag
& C_DELETED
)) {
4335 hfs_dotentry_t dotentry
[2];
4339 struct hfs_extdotentry
*entry
= &dotentry
[0].ext
;
4341 entry
->d_fileno
= cp
->c_cnid
;
4342 entry
->d_reclen
= sizeof(struct hfs_extdotentry
);
4343 entry
->d_type
= DT_DIR
;
4344 entry
->d_namlen
= 1;
4345 entry
->d_name
[0] = '.';
4346 entry
->d_name
[1] = '\0';
4347 entry
->d_name
[2] = '\0';
4348 entry
->d_seekoff
= 1;
4351 entry
->d_fileno
= cp
->c_parentcnid
;
4352 entry
->d_reclen
= sizeof(struct hfs_extdotentry
);
4353 entry
->d_type
= DT_DIR
;
4354 entry
->d_namlen
= 2;
4355 entry
->d_name
[0] = '.';
4356 entry
->d_name
[1] = '.';
4357 entry
->d_name
[2] = '\0';
4358 entry
->d_seekoff
= 2;
4359 uiosize
= 2 * sizeof(struct hfs_extdotentry
);
4361 struct hfs_stddotentry
*entry
= &dotentry
[0].std
;
4363 entry
->d_fileno
= cp
->c_cnid
;
4364 entry
->d_reclen
= sizeof(struct hfs_stddotentry
);
4365 entry
->d_type
= DT_DIR
;
4366 entry
->d_namlen
= 1;
4367 *(int *)&entry
->d_name
[0] = 0;
4368 entry
->d_name
[0] = '.';
4371 entry
->d_fileno
= cp
->c_parentcnid
;
4372 entry
->d_reclen
= sizeof(struct hfs_stddotentry
);
4373 entry
->d_type
= DT_DIR
;
4374 entry
->d_namlen
= 2;
4375 *(int *)&entry
->d_name
[0] = 0;
4376 entry
->d_name
[0] = '.';
4377 entry
->d_name
[1] = '.';
4378 uiosize
= 2 * sizeof(struct hfs_stddotentry
);
4380 if ((error
= uiomove((caddr_t
)&dotentry
, uiosize
, uio
))) {
4386 /* If there are no real entries then we're done. */
4387 if (cp
->c_entries
== 0) {
4390 uio_setoffset(uio
, offset
);
4395 // We have to lock the user's buffer here so that we won't
4396 // fault on it after we've acquired a shared lock on the
4397 // catalog file. The issue is that you can get a 3-way
4398 // deadlock if someone else starts a transaction and then
4399 // tries to lock the catalog file but can't because we're
4400 // here and we can't service our page fault because VM is
4401 // blocked trying to start a transaction as a result of
4402 // trying to free up pages for our page fault. It's messy
4403 // but it does happen on dual-processors that are paging
4404 // heavily (see radar 3082639 for more info). By locking
4405 // the buffer up-front we prevent ourselves from faulting
4406 // while holding the shared catalog file lock.
4408 // Fortunately this and hfs_search() are the only two places
4409 // currently (10/30/02) that can fault on user data with a
4410 // shared lock on the catalog file.
4412 if (hfsmp
->jnl
&& uio_isuserspace(uio
)) {
4413 user_start
= uio_curriovbase(uio
);
4414 user_len
= uio_curriovlen(uio
);
4416 if ((error
= vslock(user_start
, user_len
)) != 0) {
4421 /* Convert offset into a catalog directory index. */
4422 index
= (offset
& HFS_INDEX_MASK
) - 2;
4423 tag
= offset
& ~HFS_INDEX_MASK
;
4425 /* Lock catalog during cat_findname and cat_getdirentries. */
4426 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
4428 /* When called from NFS, try and resolve a cnid hint. */
4429 if (nfs_cookies
&& cnid_hint
!= 0) {
4430 if (cat_findname(hfsmp
, cnid_hint
, &localhint
.dh_desc
) == 0) {
4431 if ( localhint
.dh_desc
.cd_parentcnid
== cp
->c_fileid
) {
4432 localhint
.dh_index
= index
- 1;
4433 localhint
.dh_time
= 0;
4434 bzero(&localhint
.dh_link
, sizeof(localhint
.dh_link
));
4435 dirhint
= &localhint
; /* don't forget to release the descriptor */
4437 cat_releasedesc(&localhint
.dh_desc
);
4442 /* Get a directory hint (cnode must be locked exclusive) */
4443 if (dirhint
== NULL
) {
4444 dirhint
= hfs_getdirhint(cp
, ((index
- 1) & HFS_INDEX_MASK
) | tag
, 0);
4446 /* Hide tag from catalog layer. */
4447 dirhint
->dh_index
&= HFS_INDEX_MASK
;
4448 if (dirhint
->dh_index
== HFS_INDEX_MASK
) {
4449 dirhint
->dh_index
= -1;
4454 dirhint
->dh_threadhint
= cp
->c_dirthreadhint
;
4458 * If we have a non-zero index, there is a possibility that during the last
4459 * call to hfs_vnop_readdir we hit EOF for this directory. If that is the case
4460 * then we don't want to return any new entries for the caller. Just return 0
4461 * items, mark the eofflag, and bail out. Because we won't have done any work, the
4462 * code at the end of the function will release the dirhint for us.
4464 * Don't forget to unlock the catalog lock on the way out, too.
4466 if (dirhint
->dh_desc
.cd_flags
& CD_EOF
) {
4469 uio_setoffset(uio
, startoffset
);
4470 hfs_systemfile_unlock (hfsmp
, lockflags
);
4476 /* Pack the buffer with dirent entries. */
4477 error
= cat_getdirentries(hfsmp
, cp
->c_entries
, dirhint
, uio
, extended
, &items
, &eofflag
);
4479 if (index
== 0 && error
== 0) {
4480 cp
->c_dirthreadhint
= dirhint
->dh_threadhint
;
4483 hfs_systemfile_unlock(hfsmp
, lockflags
);
4489 /* Get index to the next item */
4492 if (items
>= (int)cp
->c_entries
) {
4496 /* Convert catalog directory index back into an offset. */
4498 tag
= (++cp
->c_dirhinttag
) << HFS_INDEX_BITS
;
4499 uio_setoffset(uio
, (index
+ 2) | tag
);
4500 dirhint
->dh_index
|= tag
;
4503 cp
->c_touch_acctime
= TRUE
;
4505 if (ap
->a_numdirent
) {
4506 if (startoffset
== 0)
4508 *ap
->a_numdirent
= items
;
4513 vsunlock(user_start
, user_len
, TRUE
);
4515 /* If we didn't do anything then go ahead and dump the hint. */
4516 if ((dirhint
!= NULL
) &&
4517 (dirhint
!= &localhint
) &&
4518 (uio_offset(uio
) == startoffset
)) {
4519 hfs_reldirhint(cp
, dirhint
);
4522 if (ap
->a_eofflag
) {
4523 *ap
->a_eofflag
= eofflag
;
4525 if (dirhint
== &localhint
) {
4526 cat_releasedesc(&localhint
.dh_desc
);
4534 * Read contents of a symbolic link.
4537 hfs_vnop_readlink(ap
)
4538 struct vnop_readlink_args
/* {
4541 vfs_context_t a_context;
4544 struct vnode
*vp
= ap
->a_vp
;
4546 struct filefork
*fp
;
4549 if (!vnode_islnk(vp
))
4552 if ((error
= hfs_lock(VTOC(vp
), HFS_EXCLUSIVE_LOCK
)))
4557 /* Zero length sym links are not allowed */
4558 if (fp
->ff_size
== 0 || fp
->ff_size
> MAXPATHLEN
) {
4563 /* Cache the path so we don't waste buffer cache resources */
4564 if (fp
->ff_symlinkptr
== NULL
) {
4565 struct buf
*bp
= NULL
;
4567 MALLOC(fp
->ff_symlinkptr
, char *, fp
->ff_size
, M_TEMP
, M_WAITOK
);
4568 if (fp
->ff_symlinkptr
== NULL
) {
4572 error
= (int)buf_meta_bread(vp
, (daddr64_t
)0,
4573 roundup((int)fp
->ff_size
, VTOHFS(vp
)->hfs_physical_block_size
),
4574 vfs_context_ucred(ap
->a_context
), &bp
);
4578 if (fp
->ff_symlinkptr
) {
4579 FREE(fp
->ff_symlinkptr
, M_TEMP
);
4580 fp
->ff_symlinkptr
= NULL
;
4584 bcopy((char *)buf_dataptr(bp
), fp
->ff_symlinkptr
, (size_t)fp
->ff_size
);
4586 if (VTOHFS(vp
)->jnl
&& (buf_flags(bp
) & B_LOCKED
) == 0) {
4587 buf_markinvalid(bp
); /* data no longer needed */
4591 error
= uiomove((caddr_t
)fp
->ff_symlinkptr
, (int)fp
->ff_size
, ap
->a_uio
);
4594 * Keep track blocks read
4596 if ((VTOHFS(vp
)->hfc_stage
== HFC_RECORDING
) && (error
== 0)) {
4599 * If this file hasn't been seen since the start of
4600 * the current sampling period then start over.
4602 if (cp
->c_atime
< VTOHFS(vp
)->hfc_timebase
)
4603 VTOF(vp
)->ff_bytesread
= fp
->ff_size
;
4605 VTOF(vp
)->ff_bytesread
+= fp
->ff_size
;
4607 // if (VTOF(vp)->ff_bytesread > fp->ff_size)
4608 // cp->c_touch_acctime = TRUE;
4618 * Get configurable pathname variables.
4621 hfs_vnop_pathconf(ap
)
4622 struct vnop_pathconf_args
/* {
4626 vfs_context_t a_context;
4629 switch (ap
->a_name
) {
4631 if (VTOHFS(ap
->a_vp
)->hfs_flags
& HFS_STANDARD
)
4634 *ap
->a_retval
= HFS_LINK_MAX
;
4637 if (VTOHFS(ap
->a_vp
)->hfs_flags
& HFS_STANDARD
)
4638 *ap
->a_retval
= kHFSMaxFileNameChars
; /* 31 */
4640 *ap
->a_retval
= kHFSPlusMaxFileNameChars
; /* 255 */
4643 *ap
->a_retval
= PATH_MAX
; /* 1024 */
4646 *ap
->a_retval
= PIPE_BUF
;
4648 case _PC_CHOWN_RESTRICTED
:
4649 *ap
->a_retval
= 200112; /* _POSIX_CHOWN_RESTRICTED */
4652 *ap
->a_retval
= 200112; /* _POSIX_NO_TRUNC */
4654 case _PC_NAME_CHARS_MAX
:
4655 if (VTOHFS(ap
->a_vp
)->hfs_flags
& HFS_STANDARD
)
4656 *ap
->a_retval
= kHFSMaxFileNameChars
; /* 31 */
4658 *ap
->a_retval
= kHFSPlusMaxFileNameChars
; /* 255 */
4660 case _PC_CASE_SENSITIVE
:
4661 if (VTOHFS(ap
->a_vp
)->hfs_flags
& HFS_CASE_SENSITIVE
)
4666 case _PC_CASE_PRESERVING
:
4669 case _PC_FILESIZEBITS
:
4670 if (VTOHFS(ap
->a_vp
)->hfs_flags
& HFS_STANDARD
)
4673 *ap
->a_retval
= 64; /* number of bits to store max file size */
4675 case _PC_XATTR_SIZE_BITS
:
4676 /* Number of bits to store maximum extended attribute size */
4677 *ap
->a_retval
= HFS_XATTR_SIZE_BITS
;
4688 * Update a cnode's on-disk metadata.
4690 * If waitfor is set, then wait for the disk write of
4691 * the node to complete.
4693 * The cnode must be locked exclusive
4696 hfs_update(struct vnode
*vp
, __unused
int waitfor
)
4698 struct cnode
*cp
= VTOC(vp
);
4700 struct cat_fork
*dataforkp
= NULL
;
4701 struct cat_fork
*rsrcforkp
= NULL
;
4702 struct cat_fork datafork
;
4703 struct cat_fork rsrcfork
;
4704 struct hfsmount
*hfsmp
;
4711 if (((vnode_issystem(vp
) && (cp
->c_cnid
< kHFSFirstUserCatalogNodeID
))) ||
4712 hfsmp
->hfs_catalog_vp
== NULL
){
4715 if ((hfsmp
->hfs_flags
& HFS_READ_ONLY
) || (cp
->c_mode
== 0)) {
4716 cp
->c_flag
&= ~C_MODIFIED
;
4717 cp
->c_touch_acctime
= 0;
4718 cp
->c_touch_chgtime
= 0;
4719 cp
->c_touch_modtime
= 0;
4723 hfs_touchtimes(hfsmp
, cp
);
4725 /* Nothing to update. */
4726 if ((cp
->c_flag
& (C_MODIFIED
| C_FORCEUPDATE
)) == 0) {
4731 dataforkp
= &cp
->c_datafork
->ff_data
;
4733 rsrcforkp
= &cp
->c_rsrcfork
->ff_data
;
4736 * For delayed allocations updates are
4737 * postponed until an fsync or the file
4738 * gets written to disk.
4740 * Deleted files can defer meta data updates until inactive.
4742 * If we're ever called with the C_FORCEUPDATE flag though
4743 * we have to do the update.
4745 if (ISSET(cp
->c_flag
, C_FORCEUPDATE
) == 0 &&
4746 (ISSET(cp
->c_flag
, C_DELETED
) ||
4747 (dataforkp
&& cp
->c_datafork
->ff_unallocblocks
) ||
4748 (rsrcforkp
&& cp
->c_rsrcfork
->ff_unallocblocks
))) {
4749 // cp->c_flag &= ~(C_ACCESS | C_CHANGE | C_UPDATE);
4750 cp
->c_flag
|= C_MODIFIED
;
4755 if ((error
= hfs_start_transaction(hfsmp
)) != 0) {
4760 * Modify the values passed to cat_update based on whether or not
4761 * the file has invalid ranges or borrowed blocks.
4766 /* copy the datafork into a temporary copy so we don't pollute the cnode's */
4767 bcopy(dataforkp
, &datafork
, sizeof(datafork
));
4768 dataforkp
= &datafork
;
4771 * If there are borrowed blocks, ensure that they are subtracted
4772 * from the total block count before writing the cnode entry to disk.
4773 * Only extents that have actually been marked allocated in the bitmap
4774 * should be reflected in the total block count for this fork.
4776 if (cp
->c_datafork
->ff_unallocblocks
!= 0) {
4777 // make sure that we don't assign a negative block count
4778 if (cp
->c_datafork
->ff_blocks
< cp
->c_datafork
->ff_unallocblocks
) {
4779 panic("hfs: ff_blocks %d is less than unalloc blocks %d\n",
4780 cp
->c_datafork
->ff_blocks
, cp
->c_datafork
->ff_unallocblocks
);
4783 /* Also cap the LEOF to the total number of bytes that are allocated. */
4784 datafork
.cf_blocks
= (cp
->c_datafork
->ff_blocks
- cp
->c_datafork
->ff_unallocblocks
);
4785 datafork
.cf_size
= datafork
.cf_blocks
* HFSTOVCB(hfsmp
)->blockSize
;
4789 * For files with invalid ranges (holes) the on-disk
4790 * field representing the size of the file (cf_size)
4791 * must be no larger than the start of the first hole.
4792 * However, note that if the first invalid range exists
4793 * solely within borrowed blocks, then our LEOF and block
4794 * count should both be zero. As a result, set it to the
4795 * min of the current cf_size and the start of the first
4796 * invalid range, because it may have already been reduced
4797 * to zero by the borrowed blocks check above.
4799 if (!TAILQ_EMPTY(&cp
->c_datafork
->ff_invalidranges
)) {
4800 numbytes
= TAILQ_FIRST(&cp
->c_datafork
->ff_invalidranges
)->rl_start
;
4801 datafork
.cf_size
= MIN((numbytes
), (datafork
.cf_size
));
4806 * For resource forks with delayed allocations, make sure
4807 * the block count and file size match the number of blocks
4808 * actually allocated to the file on disk.
4810 if (rsrcforkp
&& (cp
->c_rsrcfork
->ff_unallocblocks
!= 0)) {
4811 bcopy(rsrcforkp
, &rsrcfork
, sizeof(rsrcfork
));
4812 rsrcfork
.cf_blocks
= (cp
->c_rsrcfork
->ff_blocks
- cp
->c_rsrcfork
->ff_unallocblocks
);
4813 rsrcfork
.cf_size
= rsrcfork
.cf_blocks
* HFSTOVCB(hfsmp
)->blockSize
;
4814 rsrcforkp
= &rsrcfork
;
4818 * Lock the Catalog b-tree file.
4820 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_EXCLUSIVE_LOCK
);
4822 /* XXX - waitfor is not enforced */
4823 error
= cat_update(hfsmp
, &cp
->c_desc
, &cp
->c_attr
, dataforkp
, rsrcforkp
);
4825 hfs_systemfile_unlock(hfsmp
, lockflags
);
4827 /* After the updates are finished, clear the flags */
4828 cp
->c_flag
&= ~(C_MODIFIED
| C_FORCEUPDATE
);
4830 hfs_end_transaction(hfsmp
);
4836 * Allocate a new node
4837 * Note - Function does not create and return a vnode for whiteout creation.
4840 hfs_makenode(struct vnode
*dvp
, struct vnode
**vpp
, struct componentname
*cnp
,
4841 struct vnode_attr
*vap
, vfs_context_t ctx
)
4843 struct cnode
*cp
= NULL
;
4844 struct cnode
*dcp
= NULL
;
4846 struct hfsmount
*hfsmp
;
4847 struct cat_desc in_desc
, out_desc
;
4848 struct cat_attr attr
;
4851 int error
, started_tr
= 0;
4852 enum vtype vnodetype
;
4854 int newvnode_flags
= 0;
4856 u_int32_t gnv_flags
= 0;
4858 if ((error
= hfs_lock(VTOC(dvp
), HFS_EXCLUSIVE_LOCK
)))
4861 /* set the cnode pointer only after successfully acquiring lock */
4864 /* Don't allow creation of new entries in open-unlinked directories */
4865 if ((error
= hfs_checkdeleted(dcp
))) {
4870 dcp
->c_flag
|= C_DIR_MODIFICATION
;
4872 hfsmp
= VTOHFS(dvp
);
4875 out_desc
.cd_flags
= 0;
4876 out_desc
.cd_nameptr
= NULL
;
4878 vnodetype
= vap
->va_type
;
4879 if (vnodetype
== VNON
)
4881 mode
= MAKEIMODE(vnodetype
, vap
->va_mode
);
4884 /* If we're creating a regular file on a CP filesystem, then delay caching */
4885 if ((vnodetype
== VREG
) && (cp_fs_protected (VTOVFS(dvp
)))) {
4890 /* Check if were out of usable disk space. */
4891 if ((hfs_freeblks(hfsmp
, 1) == 0) && (vfs_context_suser(ctx
) != 0)) {
4898 /* Setup the default attributes */
4899 bzero(&attr
, sizeof(attr
));
4900 attr
.ca_mode
= mode
;
4901 attr
.ca_linkcount
= 1;
4902 if (VATTR_IS_ACTIVE(vap
, va_rdev
)) {
4903 attr
.ca_rdev
= vap
->va_rdev
;
4905 if (VATTR_IS_ACTIVE(vap
, va_create_time
)) {
4906 VATTR_SET_SUPPORTED(vap
, va_create_time
);
4907 attr
.ca_itime
= vap
->va_create_time
.tv_sec
;
4909 attr
.ca_itime
= tv
.tv_sec
;
4911 if ((hfsmp
->hfs_flags
& HFS_STANDARD
) && gTimeZone
.tz_dsttime
) {
4912 attr
.ca_itime
+= 3600; /* Same as what hfs_update does */
4914 attr
.ca_atime
= attr
.ca_ctime
= attr
.ca_mtime
= attr
.ca_itime
;
4915 attr
.ca_atimeondisk
= attr
.ca_atime
;
4916 if (VATTR_IS_ACTIVE(vap
, va_flags
)) {
4917 VATTR_SET_SUPPORTED(vap
, va_flags
);
4918 attr
.ca_flags
= vap
->va_flags
;
4922 * HFS+ only: all files get ThreadExists
4923 * HFSX only: dirs get HasFolderCount
4925 if (!(hfsmp
->hfs_flags
& HFS_STANDARD
)) {
4926 if (vnodetype
== VDIR
) {
4927 if (hfsmp
->hfs_flags
& HFS_FOLDERCOUNT
)
4928 attr
.ca_recflags
= kHFSHasFolderCountMask
;
4930 attr
.ca_recflags
= kHFSThreadExistsMask
;
4934 /* Add the date added to the item */
4935 hfs_write_dateadded (&attr
, attr
.ca_atime
);
4937 attr
.ca_uid
= vap
->va_uid
;
4938 attr
.ca_gid
= vap
->va_gid
;
4939 VATTR_SET_SUPPORTED(vap
, va_mode
);
4940 VATTR_SET_SUPPORTED(vap
, va_uid
);
4941 VATTR_SET_SUPPORTED(vap
, va_gid
);
4944 /* check to see if this node's creation would cause us to go over
4945 * quota. If so, abort this operation.
4947 if (hfsmp
->hfs_flags
& HFS_QUOTAS
) {
4948 if ((error
= hfs_quotacheck(hfsmp
, 1, attr
.ca_uid
, attr
.ca_gid
,
4949 vfs_context_ucred(ctx
)))) {
4956 /* Tag symlinks with a type and creator. */
4957 if (vnodetype
== VLNK
) {
4958 struct FndrFileInfo
*fip
;
4960 fip
= (struct FndrFileInfo
*)&attr
.ca_finderinfo
;
4961 fip
->fdType
= SWAP_BE32(kSymLinkFileType
);
4962 fip
->fdCreator
= SWAP_BE32(kSymLinkCreator
);
4964 if (cnp
->cn_flags
& ISWHITEOUT
)
4965 attr
.ca_flags
|= UF_OPAQUE
;
4967 /* Setup the descriptor */
4968 in_desc
.cd_nameptr
= (const u_int8_t
*)cnp
->cn_nameptr
;
4969 in_desc
.cd_namelen
= cnp
->cn_namelen
;
4970 in_desc
.cd_parentcnid
= dcp
->c_fileid
;
4971 in_desc
.cd_flags
= S_ISDIR(mode
) ? CD_ISDIR
: 0;
4972 in_desc
.cd_hint
= dcp
->c_childhint
;
4973 in_desc
.cd_encoding
= 0;
4975 if ((error
= hfs_start_transaction(hfsmp
)) != 0) {
4980 // have to also lock the attribute file because cat_create() needs
4981 // to check that any fileID it wants to use does not have orphaned
4982 // attributes in it.
4983 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
| SFL_ATTRIBUTE
, HFS_EXCLUSIVE_LOCK
);
4985 /* Reserve some space in the Catalog file. */
4986 if ((error
= cat_preflight(hfsmp
, CAT_CREATE
, NULL
, 0))) {
4987 hfs_systemfile_unlock(hfsmp
, lockflags
);
4990 error
= cat_create(hfsmp
, &in_desc
, &attr
, &out_desc
);
4992 /* Update the parent directory */
4993 dcp
->c_childhint
= out_desc
.cd_hint
; /* Cache directory's location */
4995 if (vnodetype
== VDIR
) {
4996 INC_FOLDERCOUNT(hfsmp
, dcp
->c_attr
);
4998 dcp
->c_dirchangecnt
++;
4999 dcp
->c_ctime
= tv
.tv_sec
;
5000 dcp
->c_mtime
= tv
.tv_sec
;
5001 (void) cat_update(hfsmp
, &dcp
->c_desc
, &dcp
->c_attr
, NULL
, NULL
);
5003 hfs_systemfile_unlock(hfsmp
, lockflags
);
5007 /* Invalidate negative cache entries in the directory */
5008 if (dcp
->c_flag
& C_NEG_ENTRIES
) {
5009 cache_purge_negatives(dvp
);
5010 dcp
->c_flag
&= ~C_NEG_ENTRIES
;
5013 hfs_volupdate(hfsmp
, vnodetype
== VDIR
? VOL_MKDIR
: VOL_MKFILE
,
5014 (dcp
->c_cnid
== kHFSRootFolderID
));
5017 // have to end the transaction here before we call hfs_getnewvnode()
5018 // because that can cause us to try and reclaim a vnode on a different
5019 // file system which could cause us to start a transaction which can
5020 // deadlock with someone on that other file system (since we could be
5021 // holding two transaction locks as well as various vnodes and we did
5022 // not obtain the locks on them in the proper order).
5024 // NOTE: this means that if the quota check fails or we have to update
5025 // the change time on a block-special device that those changes
5026 // will happen as part of independent transactions.
5029 hfs_end_transaction(hfsmp
);
5033 /* Do not create vnode for whiteouts */
5034 if (S_ISWHT(mode
)) {
5038 gnv_flags
|= GNV_CREATE
;
5040 gnv_flags
|= GNV_NOCACHE
;
5044 * Create a vnode for the object just created.
5046 * NOTE: Maintaining the cnode lock on the parent directory is important,
5047 * as it prevents race conditions where other threads want to look up entries
5048 * in the directory and/or add things as we are in the process of creating
5049 * the vnode below. However, this has the potential for causing a
5050 * double lock panic when dealing with shadow files on a HFS boot partition.
5051 * The panic could occur if we are not cleaning up after ourselves properly
5052 * when done with a shadow file or in the error cases. The error would occur if we
5053 * try to create a new vnode, and then end up reclaiming another shadow vnode to
5054 * create the new one. However, if everything is working properly, this should
5055 * be a non-issue as we would never enter that reclaim codepath.
5057 * The cnode is locked on successful return.
5059 error
= hfs_getnewvnode(hfsmp
, dvp
, cnp
, &out_desc
, gnv_flags
, &attr
,
5060 NULL
, &tvp
, &newvnode_flags
);
5068 error
= cp_entry_create_keys(cp
);
5070 * If we fail to create keys, then do NOT allow this vnode to percolate out into the
5071 * namespace. Delete it and return the errno that cp_entry_create_keys generated.
5072 * Luckily, we can do this without issues because the entry was newly created
5073 * and we're still holding the directory cnode lock. Because we prevented it from
5074 * getting inserted into the namecache upon vnode creation, all accesss to this file
5075 * would have to go through the directory, whose lock we are still holding.
5079 * If we fail to remove/recycle the item here, we can't do much about it. Log
5080 * a message to the console and then we can backtrack it. The ultimate error
5081 * that will get emitted to userland will be from the failure to create the EA blob.
5083 int err
= hfs_removefile (dvp
, tvp
, cnp
, 0, 0, 0, NULL
, 0);
5085 printf("hfs_makenode: removefile failed (%d) for CP file %p\n", err
, tvp
);
5088 err
= vnode_recycle (tvp
);
5090 printf("hfs_makenode: vnode_recycle failed (%d) for CP file %p\n", err
, tvp
);
5092 /* Drop the iocount on the new vnode to force reclamation/recycling */
5098 /* insert item into name cache if it wasn't already inserted.*/
5100 cache_enter (dvp
, tvp
, cnp
);
5106 * If CONFIG_PROTECT is not enabled, then all items will get automatically added into
5107 * the namecache, as nocache will be set to 0.
5112 * Once we create this vnode, we need to initialize its quota data
5113 * structures, if necessary. We know that it is OK to just go ahead and
5114 * initialize because we've already validated earlier (through the hfs_quotacheck
5115 * function) to see if creating this cnode/vnode would cause us to go over quota.
5117 if (hfsmp
->hfs_flags
& HFS_QUOTAS
) {
5118 (void) hfs_getinoquota(cp
);
5123 cat_releasedesc(&out_desc
);
5126 * Make sure we release cnode lock on dcp.
5129 dcp
->c_flag
&= ~C_DIR_MODIFICATION
;
5130 wakeup((caddr_t
)&dcp
->c_flag
);
5134 if (error
== 0 && cp
!= NULL
) {
5138 hfs_end_transaction(hfsmp
);
5147 * hfs_vgetrsrc acquires a resource fork vnode corresponding to the cnode that is
5148 * found in 'vp'. The rsrc fork vnode is returned with the cnode locked and iocount
5149 * on the rsrc vnode.
5151 * *rvpp is an output argument for returning the pointer to the resource fork vnode.
5152 * In most cases, the resource fork vnode will not be set if we return an error.
5153 * However, if error_on_unlinked is set, we may have already acquired the resource fork vnode
5154 * before we discover the error (the file has gone open-unlinked). In this case only,
5155 * we may return a vnode in the output argument despite an error.
5157 * If can_drop_lock is set, then it is safe for this function to temporarily drop
5158 * and then re-acquire the cnode lock. We may need to do this, for example, in order to
5159 * acquire an iocount or promote our lock.
5161 * error_on_unlinked is an argument which indicates that we are to return an error if we
5162 * discover that the cnode has gone into an open-unlinked state ( C_DELETED or C_NOEXISTS)
5163 * is set in the cnode flags. This is only necessary if can_drop_lock is true, otherwise
5164 * there's really no reason to double-check for errors on the cnode.
5168 hfs_vgetrsrc(struct hfsmount
*hfsmp
, struct vnode
*vp
, struct vnode
**rvpp
,
5169 int can_drop_lock
, int error_on_unlinked
)
5172 struct vnode
*dvp
= NULLVP
;
5173 struct cnode
*cp
= VTOC(vp
);
5176 int delete_status
= 0;
5178 if (vnode_vtype(vp
) == VDIR
) {
5183 * Need to check the status of the cnode to validate it hasn't gone
5184 * open-unlinked on us before we can actually do work with it.
5186 delete_status
= hfs_checkdeleted(cp
);
5187 if ((delete_status
) && (error_on_unlinked
)) {
5188 return delete_status
;
5192 /* Attempt to use existing vnode */
5193 if ((rvp
= cp
->c_rsrc_vp
)) {
5194 vid
= vnode_vid(rvp
);
5197 * It is not safe to hold the cnode lock when calling vnode_getwithvid()
5198 * for the alternate fork -- vnode_getwithvid() could deadlock waiting
5199 * for a VL_WANTTERM while another thread has an iocount on the alternate
5200 * fork vnode and is attempting to acquire the common cnode lock.
5202 * But it's also not safe to drop the cnode lock when we're holding
5203 * multiple cnode locks, like during a hfs_removefile() operation
5204 * since we could lock out of order when re-acquiring the cnode lock.
5206 * So we can only drop the lock here if its safe to drop it -- which is
5207 * most of the time with the exception being hfs_removefile().
5212 error
= vnode_getwithvid(rvp
, vid
);
5214 if (can_drop_lock
) {
5215 (void) hfs_lock(cp
, HFS_FORCE_LOCK
);
5218 * When we relinquished our cnode lock, the cnode could have raced
5219 * with a delete and gotten deleted. If the caller did not want
5220 * us to ignore open-unlinked files, then re-check the C_DELETED
5221 * state and see if we need to return an ENOENT here because the item
5222 * got deleted in the intervening time.
5224 if (error_on_unlinked
) {
5225 if ((delete_status
= hfs_checkdeleted(cp
))) {
5227 * If error == 0, this means that we succeeded in acquiring an iocount on the
5228 * rsrc fork vnode. However, if we're in this block of code, that means that we noticed
5229 * that the cnode has gone open-unlinked. In this case, the caller requested that we
5230 * not do any other work and return an errno. The caller will be responsible for
5231 * dropping the iocount we just acquired because we can't do it until we've released
5237 return delete_status
;
5242 * When our lock was relinquished, the resource fork
5243 * could have been recycled. Check for this and try
5246 if (error
== ENOENT
)
5250 const char * name
= (const char *)VTOC(vp
)->c_desc
.cd_nameptr
;
5253 printf("hfs_vgetrsrc: couldn't get resource"
5254 " fork for %s, err %d\n", name
, error
);
5258 struct cat_fork rsrcfork
;
5259 struct componentname cn
;
5260 struct cat_desc
*descptr
= NULL
;
5261 struct cat_desc to_desc
;
5264 int newvnode_flags
= 0;
5267 * Make sure cnode lock is exclusive, if not upgrade it.
5269 * We assume that we were called from a read-only VNOP (getattr)
5270 * and that its safe to have the cnode lock dropped and reacquired.
5272 if (cp
->c_lockowner
!= current_thread()) {
5273 if (!can_drop_lock
) {
5277 * If the upgrade fails we lose the lock and
5278 * have to take the exclusive lock on our own.
5280 if (lck_rw_lock_shared_to_exclusive(&cp
->c_rwlock
) == FALSE
)
5281 lck_rw_lock_exclusive(&cp
->c_rwlock
);
5282 cp
->c_lockowner
= current_thread();
5286 * hfs_vgetsrc may be invoked for a cnode that has already been marked
5287 * C_DELETED. This is because we need to continue to provide rsrc
5288 * fork access to open-unlinked files. In this case, build a fake descriptor
5289 * like in hfs_removefile. If we don't do this, buildkey will fail in
5290 * cat_lookup because this cnode has no name in its descriptor. However,
5291 * only do this if the caller did not specify that they wanted us to
5292 * error out upon encountering open-unlinked files.
5295 if ((error_on_unlinked
) && (can_drop_lock
)) {
5296 if ((error
= hfs_checkdeleted(cp
))) {
5301 if ((cp
->c_flag
& C_DELETED
) && (cp
->c_desc
.cd_namelen
== 0)) {
5302 bzero (&to_desc
, sizeof(to_desc
));
5303 bzero (delname
, 32);
5304 MAKE_DELETED_NAME(delname
, sizeof(delname
), cp
->c_fileid
);
5305 to_desc
.cd_nameptr
= (const u_int8_t
*) delname
;
5306 to_desc
.cd_namelen
= strlen(delname
);
5307 to_desc
.cd_parentcnid
= hfsmp
->hfs_private_desc
[FILE_HARDLINKS
].cd_cnid
;
5308 to_desc
.cd_flags
= 0;
5309 to_desc
.cd_cnid
= cp
->c_cnid
;
5314 descptr
= &cp
->c_desc
;
5318 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
5320 /* Get resource fork data */
5321 error
= cat_lookup(hfsmp
, descptr
, 1, (struct cat_desc
*)0,
5322 (struct cat_attr
*)0, &rsrcfork
, NULL
);
5324 hfs_systemfile_unlock(hfsmp
, lockflags
);
5329 * Supply hfs_getnewvnode with a component name.
5332 if (descptr
->cd_nameptr
) {
5333 MALLOC_ZONE(cn
.cn_pnbuf
, caddr_t
, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
5334 cn
.cn_nameiop
= LOOKUP
;
5335 cn
.cn_flags
= ISLASTCN
| HASBUF
;
5336 cn
.cn_context
= NULL
;
5337 cn
.cn_pnlen
= MAXPATHLEN
;
5338 cn
.cn_nameptr
= cn
.cn_pnbuf
;
5341 cn
.cn_namelen
= snprintf(cn
.cn_nameptr
, MAXPATHLEN
,
5342 "%s%s", descptr
->cd_nameptr
,
5343 _PATH_RSRCFORKSPEC
);
5345 dvp
= vnode_getparent(vp
);
5346 error
= hfs_getnewvnode(hfsmp
, dvp
, cn
.cn_pnbuf
? &cn
: NULL
,
5347 descptr
, GNV_WANTRSRC
| GNV_SKIPLOCK
, &cp
->c_attr
,
5348 &rsrcfork
, &rvp
, &newvnode_flags
);
5352 FREE_ZONE(cn
.cn_pnbuf
, cn
.cn_pnlen
, M_NAMEI
);
5362 * Wrapper for special device reads
5366 struct vnop_read_args
/* {
5370 vfs_context_t a_context;
5376 VTOC(ap
->a_vp
)->c_touch_acctime
= TRUE
;
5377 return (VOCALL (spec_vnodeop_p
, VOFFSET(vnop_read
), ap
));
5381 * Wrapper for special device writes
5385 struct vnop_write_args
/* {
5389 vfs_context_t a_context;
5393 * Set update and change flags.
5395 VTOC(ap
->a_vp
)->c_touch_chgtime
= TRUE
;
5396 VTOC(ap
->a_vp
)->c_touch_modtime
= TRUE
;
5397 return (VOCALL (spec_vnodeop_p
, VOFFSET(vnop_write
), ap
));
5401 * Wrapper for special device close
5403 * Update the times on the cnode then do device close.
5407 struct vnop_close_args
/* {
5410 vfs_context_t a_context;
5413 struct vnode
*vp
= ap
->a_vp
;
5416 if (vnode_isinuse(ap
->a_vp
, 0)) {
5417 if (hfs_lock(VTOC(vp
), HFS_EXCLUSIVE_LOCK
) == 0) {
5419 hfs_touchtimes(VTOHFS(vp
), cp
);
5423 return (VOCALL (spec_vnodeop_p
, VOFFSET(vnop_close
), ap
));
5428 * Wrapper for fifo reads
5432 struct vnop_read_args
/* {
5436 vfs_context_t a_context;
5442 VTOC(ap
->a_vp
)->c_touch_acctime
= TRUE
;
5443 return (VOCALL (fifo_vnodeop_p
, VOFFSET(vnop_read
), ap
));
5447 * Wrapper for fifo writes
5451 struct vnop_write_args
/* {
5455 vfs_context_t a_context;
5459 * Set update and change flags.
5461 VTOC(ap
->a_vp
)->c_touch_chgtime
= TRUE
;
5462 VTOC(ap
->a_vp
)->c_touch_modtime
= TRUE
;
5463 return (VOCALL (fifo_vnodeop_p
, VOFFSET(vnop_write
), ap
));
5467 * Wrapper for fifo close
5469 * Update the times on the cnode then do device close.
5473 struct vnop_close_args
/* {
5476 vfs_context_t a_context;
5479 struct vnode
*vp
= ap
->a_vp
;
5482 if (vnode_isinuse(ap
->a_vp
, 1)) {
5483 if (hfs_lock(VTOC(vp
), HFS_EXCLUSIVE_LOCK
) == 0) {
5485 hfs_touchtimes(VTOHFS(vp
), cp
);
5489 return (VOCALL (fifo_vnodeop_p
, VOFFSET(vnop_close
), ap
));
5496 * Synchronize a file's in-core state with that on disk.
5500 struct vnop_fsync_args
/* {
5503 vfs_context_t a_context;
5506 struct vnode
* vp
= ap
->a_vp
;
5509 /* Note: We check hfs flags instead of vfs mount flag because during
5510 * read-write update, hfs marks itself read-write much earlier than
5511 * the vfs, and hence won't result in skipping of certain writes like
5512 * zero'ing out of unused nodes, creation of hotfiles btree, etc.
5514 if (VTOHFS(vp
)->hfs_flags
& HFS_READ_ONLY
) {
5519 if ((error
= cp_handle_vnop(VTOC(vp
), CP_WRITE_ACCESS
)) != 0) {
5522 #endif /* CONFIG_PROTECT */
5525 * We need to allow ENOENT lock errors since unlink
5526 * systenm call can call VNOP_FSYNC during vclean.
5528 error
= hfs_lock(VTOC(vp
), HFS_EXCLUSIVE_LOCK
);
5532 error
= hfs_fsync(vp
, ap
->a_waitfor
, 0, vfs_context_proc(ap
->a_context
));
5534 hfs_unlock(VTOC(vp
));
5540 hfs_vnop_whiteout(ap
)
5541 struct vnop_whiteout_args
/* {
5542 struct vnode *a_dvp;
5543 struct componentname *a_cnp;
5545 vfs_context_t a_context;
5549 struct vnode
*vp
= NULL
;
5550 struct vnode_attr va
;
5551 struct vnop_lookup_args lookup_args
;
5552 struct vnop_remove_args remove_args
;
5553 struct hfsmount
*hfsmp
;
5555 hfsmp
= VTOHFS(ap
->a_dvp
);
5556 if (hfsmp
->hfs_flags
& HFS_STANDARD
) {
5561 switch (ap
->a_flags
) {
5568 VATTR_SET(&va
, va_type
, VREG
);
5569 VATTR_SET(&va
, va_mode
, S_IFWHT
);
5570 VATTR_SET(&va
, va_uid
, 0);
5571 VATTR_SET(&va
, va_gid
, 0);
5573 error
= hfs_makenode(ap
->a_dvp
, &vp
, ap
->a_cnp
, &va
, ap
->a_context
);
5574 /* No need to release the vnode as no vnode is created for whiteouts */
5578 lookup_args
.a_dvp
= ap
->a_dvp
;
5579 lookup_args
.a_vpp
= &vp
;
5580 lookup_args
.a_cnp
= ap
->a_cnp
;
5581 lookup_args
.a_context
= ap
->a_context
;
5583 error
= hfs_vnop_lookup(&lookup_args
);
5588 remove_args
.a_dvp
= ap
->a_dvp
;
5589 remove_args
.a_vp
= vp
;
5590 remove_args
.a_cnp
= ap
->a_cnp
;
5591 remove_args
.a_flags
= 0;
5592 remove_args
.a_context
= ap
->a_context
;
5594 error
= hfs_vnop_remove(&remove_args
);
5599 panic("hfs_vnop_whiteout: unknown operation (flag = %x)\n", ap
->a_flags
);
5606 int (**hfs_vnodeop_p
)(void *);
5607 int (**hfs_std_vnodeop_p
) (void *);
5609 #define VOPFUNC int (*)(void *)
5611 static int hfs_readonly_op (__unused
void* ap
) { return (EROFS
); }
5614 * In 10.6 and forward, HFS Standard is read-only and deprecated. The vnop table below
5615 * is for use with HFS standard to block out operations that would modify the file system
5618 struct vnodeopv_entry_desc hfs_standard_vnodeop_entries
[] = {
5619 { &vnop_default_desc
, (VOPFUNC
)vn_default_error
},
5620 { &vnop_lookup_desc
, (VOPFUNC
)hfs_vnop_lookup
}, /* lookup */
5621 { &vnop_create_desc
, (VOPFUNC
)hfs_readonly_op
}, /* create (READONLY) */
5622 { &vnop_mknod_desc
, (VOPFUNC
)hfs_readonly_op
}, /* mknod (READONLY) */
5623 { &vnop_open_desc
, (VOPFUNC
)hfs_vnop_open
}, /* open */
5624 { &vnop_close_desc
, (VOPFUNC
)hfs_vnop_close
}, /* close */
5625 { &vnop_getattr_desc
, (VOPFUNC
)hfs_vnop_getattr
}, /* getattr */
5626 { &vnop_setattr_desc
, (VOPFUNC
)hfs_readonly_op
}, /* setattr */
5627 { &vnop_read_desc
, (VOPFUNC
)hfs_vnop_read
}, /* read */
5628 { &vnop_write_desc
, (VOPFUNC
)hfs_readonly_op
}, /* write (READONLY) */
5629 { &vnop_ioctl_desc
, (VOPFUNC
)hfs_vnop_ioctl
}, /* ioctl */
5630 { &vnop_select_desc
, (VOPFUNC
)hfs_vnop_select
}, /* select */
5631 { &vnop_revoke_desc
, (VOPFUNC
)nop_revoke
}, /* revoke */
5632 { &vnop_exchange_desc
, (VOPFUNC
)hfs_readonly_op
}, /* exchange (READONLY)*/
5633 { &vnop_mmap_desc
, (VOPFUNC
)err_mmap
}, /* mmap */
5634 { &vnop_fsync_desc
, (VOPFUNC
)hfs_readonly_op
}, /* fsync (READONLY) */
5635 { &vnop_remove_desc
, (VOPFUNC
)hfs_readonly_op
}, /* remove (READONLY) */
5636 { &vnop_link_desc
, (VOPFUNC
)hfs_readonly_op
}, /* link ( READONLLY) */
5637 { &vnop_rename_desc
, (VOPFUNC
)hfs_readonly_op
}, /* rename (READONLY)*/
5638 { &vnop_mkdir_desc
, (VOPFUNC
)hfs_readonly_op
}, /* mkdir (READONLY) */
5639 { &vnop_rmdir_desc
, (VOPFUNC
)hfs_readonly_op
}, /* rmdir (READONLY) */
5640 { &vnop_symlink_desc
, (VOPFUNC
)hfs_readonly_op
}, /* symlink (READONLY) */
5641 { &vnop_readdir_desc
, (VOPFUNC
)hfs_vnop_readdir
}, /* readdir */
5642 { &vnop_readdirattr_desc
, (VOPFUNC
)hfs_vnop_readdirattr
}, /* readdirattr */
5643 { &vnop_readlink_desc
, (VOPFUNC
)hfs_vnop_readlink
}, /* readlink */
5644 { &vnop_inactive_desc
, (VOPFUNC
)hfs_vnop_inactive
}, /* inactive */
5645 { &vnop_reclaim_desc
, (VOPFUNC
)hfs_vnop_reclaim
}, /* reclaim */
5646 { &vnop_strategy_desc
, (VOPFUNC
)hfs_vnop_strategy
}, /* strategy */
5647 { &vnop_pathconf_desc
, (VOPFUNC
)hfs_vnop_pathconf
}, /* pathconf */
5648 { &vnop_advlock_desc
, (VOPFUNC
)err_advlock
}, /* advlock */
5649 { &vnop_allocate_desc
, (VOPFUNC
)hfs_readonly_op
}, /* allocate (READONLY) */
5650 { &vnop_searchfs_desc
, (VOPFUNC
)hfs_vnop_search
}, /* search fs */
5651 { &vnop_bwrite_desc
, (VOPFUNC
)hfs_readonly_op
}, /* bwrite (READONLY) */
5652 { &vnop_pagein_desc
, (VOPFUNC
)hfs_vnop_pagein
}, /* pagein */
5653 { &vnop_pageout_desc
,(VOPFUNC
) hfs_readonly_op
}, /* pageout (READONLY) */
5654 { &vnop_copyfile_desc
, (VOPFUNC
)hfs_readonly_op
}, /* copyfile (READONLY)*/
5655 { &vnop_blktooff_desc
, (VOPFUNC
)hfs_vnop_blktooff
}, /* blktooff */
5656 { &vnop_offtoblk_desc
, (VOPFUNC
)hfs_vnop_offtoblk
}, /* offtoblk */
5657 { &vnop_blockmap_desc
, (VOPFUNC
)hfs_vnop_blockmap
}, /* blockmap */
5658 { &vnop_getxattr_desc
, (VOPFUNC
)hfs_vnop_getxattr
},
5659 { &vnop_setxattr_desc
, (VOPFUNC
)hfs_readonly_op
}, /* set xattr (READONLY) */
5660 { &vnop_removexattr_desc
, (VOPFUNC
)hfs_readonly_op
}, /* remove xattr (READONLY) */
5661 { &vnop_listxattr_desc
, (VOPFUNC
)hfs_vnop_listxattr
},
5662 { &vnop_whiteout_desc
, (VOPFUNC
)hfs_readonly_op
}, /* whiteout (READONLY) */
5664 { &vnop_getnamedstream_desc
, (VOPFUNC
)hfs_vnop_getnamedstream
},
5665 { &vnop_makenamedstream_desc
, (VOPFUNC
)hfs_readonly_op
},
5666 { &vnop_removenamedstream_desc
, (VOPFUNC
)hfs_readonly_op
},
5668 { NULL
, (VOPFUNC
)NULL
}
5671 struct vnodeopv_desc hfs_std_vnodeop_opv_desc
=
5672 { &hfs_std_vnodeop_p
, hfs_standard_vnodeop_entries
};
5675 /* VNOP table for HFS+ */
5676 struct vnodeopv_entry_desc hfs_vnodeop_entries
[] = {
5677 { &vnop_default_desc
, (VOPFUNC
)vn_default_error
},
5678 { &vnop_lookup_desc
, (VOPFUNC
)hfs_vnop_lookup
}, /* lookup */
5679 { &vnop_create_desc
, (VOPFUNC
)hfs_vnop_create
}, /* create */
5680 { &vnop_mknod_desc
, (VOPFUNC
)hfs_vnop_mknod
}, /* mknod */
5681 { &vnop_open_desc
, (VOPFUNC
)hfs_vnop_open
}, /* open */
5682 { &vnop_close_desc
, (VOPFUNC
)hfs_vnop_close
}, /* close */
5683 { &vnop_getattr_desc
, (VOPFUNC
)hfs_vnop_getattr
}, /* getattr */
5684 { &vnop_setattr_desc
, (VOPFUNC
)hfs_vnop_setattr
}, /* setattr */
5685 { &vnop_read_desc
, (VOPFUNC
)hfs_vnop_read
}, /* read */
5686 { &vnop_write_desc
, (VOPFUNC
)hfs_vnop_write
}, /* write */
5687 { &vnop_ioctl_desc
, (VOPFUNC
)hfs_vnop_ioctl
}, /* ioctl */
5688 { &vnop_select_desc
, (VOPFUNC
)hfs_vnop_select
}, /* select */
5689 { &vnop_revoke_desc
, (VOPFUNC
)nop_revoke
}, /* revoke */
5690 { &vnop_exchange_desc
, (VOPFUNC
)hfs_vnop_exchange
}, /* exchange */
5691 { &vnop_mmap_desc
, (VOPFUNC
)hfs_vnop_mmap
}, /* mmap */
5692 { &vnop_fsync_desc
, (VOPFUNC
)hfs_vnop_fsync
}, /* fsync */
5693 { &vnop_remove_desc
, (VOPFUNC
)hfs_vnop_remove
}, /* remove */
5694 { &vnop_link_desc
, (VOPFUNC
)hfs_vnop_link
}, /* link */
5695 { &vnop_rename_desc
, (VOPFUNC
)hfs_vnop_rename
}, /* rename */
5696 { &vnop_mkdir_desc
, (VOPFUNC
)hfs_vnop_mkdir
}, /* mkdir */
5697 { &vnop_rmdir_desc
, (VOPFUNC
)hfs_vnop_rmdir
}, /* rmdir */
5698 { &vnop_symlink_desc
, (VOPFUNC
)hfs_vnop_symlink
}, /* symlink */
5699 { &vnop_readdir_desc
, (VOPFUNC
)hfs_vnop_readdir
}, /* readdir */
5700 { &vnop_readdirattr_desc
, (VOPFUNC
)hfs_vnop_readdirattr
}, /* readdirattr */
5701 { &vnop_readlink_desc
, (VOPFUNC
)hfs_vnop_readlink
}, /* readlink */
5702 { &vnop_inactive_desc
, (VOPFUNC
)hfs_vnop_inactive
}, /* inactive */
5703 { &vnop_reclaim_desc
, (VOPFUNC
)hfs_vnop_reclaim
}, /* reclaim */
5704 { &vnop_strategy_desc
, (VOPFUNC
)hfs_vnop_strategy
}, /* strategy */
5705 { &vnop_pathconf_desc
, (VOPFUNC
)hfs_vnop_pathconf
}, /* pathconf */
5706 { &vnop_advlock_desc
, (VOPFUNC
)err_advlock
}, /* advlock */
5707 { &vnop_allocate_desc
, (VOPFUNC
)hfs_vnop_allocate
}, /* allocate */
5708 { &vnop_searchfs_desc
, (VOPFUNC
)hfs_vnop_search
}, /* search fs */
5709 { &vnop_bwrite_desc
, (VOPFUNC
)hfs_vnop_bwrite
}, /* bwrite */
5710 { &vnop_pagein_desc
, (VOPFUNC
)hfs_vnop_pagein
}, /* pagein */
5711 { &vnop_pageout_desc
,(VOPFUNC
) hfs_vnop_pageout
}, /* pageout */
5712 { &vnop_copyfile_desc
, (VOPFUNC
)err_copyfile
}, /* copyfile */
5713 { &vnop_blktooff_desc
, (VOPFUNC
)hfs_vnop_blktooff
}, /* blktooff */
5714 { &vnop_offtoblk_desc
, (VOPFUNC
)hfs_vnop_offtoblk
}, /* offtoblk */
5715 { &vnop_blockmap_desc
, (VOPFUNC
)hfs_vnop_blockmap
}, /* blockmap */
5716 { &vnop_getxattr_desc
, (VOPFUNC
)hfs_vnop_getxattr
},
5717 { &vnop_setxattr_desc
, (VOPFUNC
)hfs_vnop_setxattr
},
5718 { &vnop_removexattr_desc
, (VOPFUNC
)hfs_vnop_removexattr
},
5719 { &vnop_listxattr_desc
, (VOPFUNC
)hfs_vnop_listxattr
},
5720 { &vnop_whiteout_desc
, (VOPFUNC
)hfs_vnop_whiteout
},
5722 { &vnop_getnamedstream_desc
, (VOPFUNC
)hfs_vnop_getnamedstream
},
5723 { &vnop_makenamedstream_desc
, (VOPFUNC
)hfs_vnop_makenamedstream
},
5724 { &vnop_removenamedstream_desc
, (VOPFUNC
)hfs_vnop_removenamedstream
},
5726 { NULL
, (VOPFUNC
)NULL
}
5729 struct vnodeopv_desc hfs_vnodeop_opv_desc
=
5730 { &hfs_vnodeop_p
, hfs_vnodeop_entries
};
5733 /* Spec Op vnop table for HFS+ */
5734 int (**hfs_specop_p
)(void *);
5735 struct vnodeopv_entry_desc hfs_specop_entries
[] = {
5736 { &vnop_default_desc
, (VOPFUNC
)vn_default_error
},
5737 { &vnop_lookup_desc
, (VOPFUNC
)spec_lookup
}, /* lookup */
5738 { &vnop_create_desc
, (VOPFUNC
)spec_create
}, /* create */
5739 { &vnop_mknod_desc
, (VOPFUNC
)spec_mknod
}, /* mknod */
5740 { &vnop_open_desc
, (VOPFUNC
)spec_open
}, /* open */
5741 { &vnop_close_desc
, (VOPFUNC
)hfsspec_close
}, /* close */
5742 { &vnop_getattr_desc
, (VOPFUNC
)hfs_vnop_getattr
}, /* getattr */
5743 { &vnop_setattr_desc
, (VOPFUNC
)hfs_vnop_setattr
}, /* setattr */
5744 { &vnop_read_desc
, (VOPFUNC
)hfsspec_read
}, /* read */
5745 { &vnop_write_desc
, (VOPFUNC
)hfsspec_write
}, /* write */
5746 { &vnop_ioctl_desc
, (VOPFUNC
)spec_ioctl
}, /* ioctl */
5747 { &vnop_select_desc
, (VOPFUNC
)spec_select
}, /* select */
5748 { &vnop_revoke_desc
, (VOPFUNC
)spec_revoke
}, /* revoke */
5749 { &vnop_mmap_desc
, (VOPFUNC
)spec_mmap
}, /* mmap */
5750 { &vnop_fsync_desc
, (VOPFUNC
)hfs_vnop_fsync
}, /* fsync */
5751 { &vnop_remove_desc
, (VOPFUNC
)spec_remove
}, /* remove */
5752 { &vnop_link_desc
, (VOPFUNC
)spec_link
}, /* link */
5753 { &vnop_rename_desc
, (VOPFUNC
)spec_rename
}, /* rename */
5754 { &vnop_mkdir_desc
, (VOPFUNC
)spec_mkdir
}, /* mkdir */
5755 { &vnop_rmdir_desc
, (VOPFUNC
)spec_rmdir
}, /* rmdir */
5756 { &vnop_symlink_desc
, (VOPFUNC
)spec_symlink
}, /* symlink */
5757 { &vnop_readdir_desc
, (VOPFUNC
)spec_readdir
}, /* readdir */
5758 { &vnop_readlink_desc
, (VOPFUNC
)spec_readlink
}, /* readlink */
5759 { &vnop_inactive_desc
, (VOPFUNC
)hfs_vnop_inactive
}, /* inactive */
5760 { &vnop_reclaim_desc
, (VOPFUNC
)hfs_vnop_reclaim
}, /* reclaim */
5761 { &vnop_strategy_desc
, (VOPFUNC
)spec_strategy
}, /* strategy */
5762 { &vnop_pathconf_desc
, (VOPFUNC
)spec_pathconf
}, /* pathconf */
5763 { &vnop_advlock_desc
, (VOPFUNC
)err_advlock
}, /* advlock */
5764 { &vnop_bwrite_desc
, (VOPFUNC
)hfs_vnop_bwrite
},
5765 { &vnop_pagein_desc
, (VOPFUNC
)hfs_vnop_pagein
}, /* Pagein */
5766 { &vnop_pageout_desc
, (VOPFUNC
)hfs_vnop_pageout
}, /* Pageout */
5767 { &vnop_copyfile_desc
, (VOPFUNC
)err_copyfile
}, /* copyfile */
5768 { &vnop_blktooff_desc
, (VOPFUNC
)hfs_vnop_blktooff
}, /* blktooff */
5769 { &vnop_offtoblk_desc
, (VOPFUNC
)hfs_vnop_offtoblk
}, /* offtoblk */
5770 { (struct vnodeop_desc
*)NULL
, (VOPFUNC
)NULL
}
5772 struct vnodeopv_desc hfs_specop_opv_desc
=
5773 { &hfs_specop_p
, hfs_specop_entries
};
5776 /* HFS+ FIFO VNOP table */
5777 int (**hfs_fifoop_p
)(void *);
5778 struct vnodeopv_entry_desc hfs_fifoop_entries
[] = {
5779 { &vnop_default_desc
, (VOPFUNC
)vn_default_error
},
5780 { &vnop_lookup_desc
, (VOPFUNC
)fifo_lookup
}, /* lookup */
5781 { &vnop_create_desc
, (VOPFUNC
)fifo_create
}, /* create */
5782 { &vnop_mknod_desc
, (VOPFUNC
)fifo_mknod
}, /* mknod */
5783 { &vnop_open_desc
, (VOPFUNC
)fifo_open
}, /* open */
5784 { &vnop_close_desc
, (VOPFUNC
)hfsfifo_close
}, /* close */
5785 { &vnop_getattr_desc
, (VOPFUNC
)hfs_vnop_getattr
}, /* getattr */
5786 { &vnop_setattr_desc
, (VOPFUNC
)hfs_vnop_setattr
}, /* setattr */
5787 { &vnop_read_desc
, (VOPFUNC
)hfsfifo_read
}, /* read */
5788 { &vnop_write_desc
, (VOPFUNC
)hfsfifo_write
}, /* write */
5789 { &vnop_ioctl_desc
, (VOPFUNC
)fifo_ioctl
}, /* ioctl */
5790 { &vnop_select_desc
, (VOPFUNC
)fifo_select
}, /* select */
5791 { &vnop_revoke_desc
, (VOPFUNC
)fifo_revoke
}, /* revoke */
5792 { &vnop_mmap_desc
, (VOPFUNC
)fifo_mmap
}, /* mmap */
5793 { &vnop_fsync_desc
, (VOPFUNC
)hfs_vnop_fsync
}, /* fsync */
5794 { &vnop_remove_desc
, (VOPFUNC
)fifo_remove
}, /* remove */
5795 { &vnop_link_desc
, (VOPFUNC
)fifo_link
}, /* link */
5796 { &vnop_rename_desc
, (VOPFUNC
)fifo_rename
}, /* rename */
5797 { &vnop_mkdir_desc
, (VOPFUNC
)fifo_mkdir
}, /* mkdir */
5798 { &vnop_rmdir_desc
, (VOPFUNC
)fifo_rmdir
}, /* rmdir */
5799 { &vnop_symlink_desc
, (VOPFUNC
)fifo_symlink
}, /* symlink */
5800 { &vnop_readdir_desc
, (VOPFUNC
)fifo_readdir
}, /* readdir */
5801 { &vnop_readlink_desc
, (VOPFUNC
)fifo_readlink
}, /* readlink */
5802 { &vnop_inactive_desc
, (VOPFUNC
)hfs_vnop_inactive
}, /* inactive */
5803 { &vnop_reclaim_desc
, (VOPFUNC
)hfs_vnop_reclaim
}, /* reclaim */
5804 { &vnop_strategy_desc
, (VOPFUNC
)fifo_strategy
}, /* strategy */
5805 { &vnop_pathconf_desc
, (VOPFUNC
)fifo_pathconf
}, /* pathconf */
5806 { &vnop_advlock_desc
, (VOPFUNC
)err_advlock
}, /* advlock */
5807 { &vnop_bwrite_desc
, (VOPFUNC
)hfs_vnop_bwrite
},
5808 { &vnop_pagein_desc
, (VOPFUNC
)hfs_vnop_pagein
}, /* Pagein */
5809 { &vnop_pageout_desc
, (VOPFUNC
)hfs_vnop_pageout
}, /* Pageout */
5810 { &vnop_copyfile_desc
, (VOPFUNC
)err_copyfile
}, /* copyfile */
5811 { &vnop_blktooff_desc
, (VOPFUNC
)hfs_vnop_blktooff
}, /* blktooff */
5812 { &vnop_offtoblk_desc
, (VOPFUNC
)hfs_vnop_offtoblk
}, /* offtoblk */
5813 { &vnop_blockmap_desc
, (VOPFUNC
)hfs_vnop_blockmap
}, /* blockmap */
5814 { (struct vnodeop_desc
*)NULL
, (VOPFUNC
)NULL
}
5816 struct vnodeopv_desc hfs_fifoop_opv_desc
=
5817 { &hfs_fifoop_p
, hfs_fifoop_entries
};