2 * Copyright (c) 2000-2013 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/buf_internal.h>
37 #include <sys/mount.h>
38 #include <sys/vnode_if.h>
39 #include <sys/vnode_internal.h>
40 #include <sys/malloc.h>
42 #include <sys/ubc_internal.h>
43 #include <sys/paths.h>
44 #include <sys/quota.h>
47 #include <sys/kauth.h>
48 #include <sys/uio_internal.h>
49 #include <sys/fsctl.h>
50 #include <sys/cprotect.h>
51 #include <sys/xattr.h>
54 #include <miscfs/specfs/specdev.h>
55 #include <miscfs/fifofs/fifo.h>
56 #include <vfs/vfs_support.h>
57 #include <machine/spl.h>
59 #include <sys/kdebug.h>
60 #include <sys/sysctl.h>
63 #include "hfs_catalog.h"
64 #include "hfs_cnode.h"
66 #include "hfs_mount.h"
67 #include "hfs_quota.h"
68 #include "hfs_endian.h"
70 #include "hfscommon/headers/BTreesInternal.h"
71 #include "hfscommon/headers/FileMgrInternal.h"
73 #define KNDETACH_VNLOCKED 0x00000001
75 /* Global vfs data structures for hfs */
77 /* Always F_FULLFSYNC? 1=yes,0=no (default due to "various" reasons is 'no') */
78 int always_do_fullfsync
= 0;
79 SYSCTL_DECL(_vfs_generic
);
80 SYSCTL_INT (_vfs_generic
, OID_AUTO
, always_do_fullfsync
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &always_do_fullfsync
, 0, "always F_FULLFSYNC when fsync is called");
82 int hfs_makenode(struct vnode
*dvp
, struct vnode
**vpp
,
83 struct componentname
*cnp
, struct vnode_attr
*vap
,
85 int hfs_metasync(struct hfsmount
*hfsmp
, daddr64_t node
, __unused
struct proc
*p
);
86 int hfs_metasync_all(struct hfsmount
*hfsmp
);
88 int hfs_removedir(struct vnode
*, struct vnode
*, struct componentname
*,
90 int hfs_removefile(struct vnode
*, struct vnode
*, struct componentname
*,
91 int, int, int, struct vnode
*, int);
93 /* Used here and in cnode teardown -- for symlinks */
94 int hfs_removefile_callback(struct buf
*bp
, void *hfsmp
);
96 int hfs_movedata (struct vnode
*, struct vnode
*);
97 static int hfs_move_fork (struct filefork
*srcfork
, struct cnode
*src
,
98 struct filefork
*dstfork
, struct cnode
*dst
);
100 decmpfs_cnode
* hfs_lazy_init_decmpfs_cnode (struct cnode
*cp
);
103 static int hfsfifo_read(struct vnop_read_args
*);
104 static int hfsfifo_write(struct vnop_write_args
*);
105 static int hfsfifo_close(struct vnop_close_args
*);
107 extern int (**fifo_vnodeop_p
)(void *);
110 int hfs_vnop_close(struct vnop_close_args
*);
111 int hfs_vnop_create(struct vnop_create_args
*);
112 int hfs_vnop_exchange(struct vnop_exchange_args
*);
113 int hfs_vnop_fsync(struct vnop_fsync_args
*);
114 int hfs_vnop_mkdir(struct vnop_mkdir_args
*);
115 int hfs_vnop_mknod(struct vnop_mknod_args
*);
116 int hfs_vnop_getattr(struct vnop_getattr_args
*);
117 int hfs_vnop_open(struct vnop_open_args
*);
118 int hfs_vnop_readdir(struct vnop_readdir_args
*);
119 int hfs_vnop_remove(struct vnop_remove_args
*);
120 int hfs_vnop_rename(struct vnop_rename_args
*);
121 int hfs_vnop_rmdir(struct vnop_rmdir_args
*);
122 int hfs_vnop_symlink(struct vnop_symlink_args
*);
123 int hfs_vnop_setattr(struct vnop_setattr_args
*);
124 int hfs_vnop_readlink(struct vnop_readlink_args
*);
125 int hfs_vnop_pathconf(struct vnop_pathconf_args
*);
126 int hfs_vnop_whiteout(struct vnop_whiteout_args
*);
127 int hfs_vnop_mmap(struct vnop_mmap_args
*ap
);
128 int hfsspec_read(struct vnop_read_args
*);
129 int hfsspec_write(struct vnop_write_args
*);
130 int hfsspec_close(struct vnop_close_args
*);
132 /* Options for hfs_removedir and hfs_removefile */
133 #define HFSRM_SKIP_RESERVE 0x01
138 /*****************************************************************************
140 * Common Operations on vnodes
142 *****************************************************************************/
145 * Is the given cnode either the .journal or .journal_info_block file on
146 * a volume with an active journal? Many VNOPs use this to deny access
149 * Note: the .journal file on a volume with an external journal still
150 * returns true here, even though it does not actually hold the contents
151 * of the volume's journal.
154 hfs_is_journal_file(struct hfsmount
*hfsmp
, struct cnode
*cp
)
156 if (hfsmp
->jnl
!= NULL
&&
157 (cp
->c_fileid
== hfsmp
->hfs_jnlinfoblkid
||
158 cp
->c_fileid
== hfsmp
->hfs_jnlfileid
)) {
166 * Create a regular file.
169 hfs_vnop_create(struct vnop_create_args
*ap
)
174 error
= hfs_makenode(ap
->a_dvp
, ap
->a_vpp
, ap
->a_cnp
, ap
->a_vap
, ap
->a_context
);
177 * We speculatively skipped the original lookup of the leaf
178 * for CREATE. Since it exists, go get it as long as they
179 * didn't want an exclusive create.
181 if ((error
== EEXIST
) && !(ap
->a_vap
->va_vaflags
& VA_EXCLUSIVE
)) {
182 struct vnop_lookup_args args
;
184 args
.a_desc
= &vnop_lookup_desc
;
185 args
.a_dvp
= ap
->a_dvp
;
186 args
.a_vpp
= ap
->a_vpp
;
187 args
.a_cnp
= ap
->a_cnp
;
188 args
.a_context
= ap
->a_context
;
189 args
.a_cnp
->cn_nameiop
= LOOKUP
;
190 error
= hfs_vnop_lookup(&args
);
192 * We can also race with remove for this file.
194 if (error
== ENOENT
) {
198 /* Make sure it was file. */
199 if ((error
== 0) && !vnode_isreg(*args
.a_vpp
)) {
200 vnode_put(*args
.a_vpp
);
201 *args
.a_vpp
= NULLVP
;
204 args
.a_cnp
->cn_nameiop
= CREATE
;
210 * Make device special file.
213 hfs_vnop_mknod(struct vnop_mknod_args
*ap
)
215 struct vnode_attr
*vap
= ap
->a_vap
;
216 struct vnode
*dvp
= ap
->a_dvp
;
217 struct vnode
**vpp
= ap
->a_vpp
;
221 if (VTOVCB(dvp
)->vcbSigWord
!= kHFSPlusSigWord
) {
225 /* Create the vnode */
226 error
= hfs_makenode(dvp
, vpp
, ap
->a_cnp
, vap
, ap
->a_context
);
231 cp
->c_touch_acctime
= TRUE
;
232 cp
->c_touch_chgtime
= TRUE
;
233 cp
->c_touch_modtime
= TRUE
;
235 if ((vap
->va_rdev
!= VNOVAL
) &&
236 (vap
->va_type
== VBLK
|| vap
->va_type
== VCHR
))
237 cp
->c_rdev
= vap
->va_rdev
;
244 * hfs_ref_data_vp(): returns the data fork vnode for a given cnode.
245 * In the (hopefully rare) case where the data fork vnode is not
246 * present, it will use hfs_vget() to create a new vnode for the
249 * NOTE: If successful and a vnode is returned, the caller is responsible
250 * for releasing the returned vnode with vnode_rele().
253 hfs_ref_data_vp(struct cnode
*cp
, struct vnode
**data_vp
, int skiplock
)
257 if (!data_vp
|| !cp
) /* sanity check incoming parameters */
260 /* maybe we should take the hfs cnode lock here, and if so, use the skiplock parameter to tell us not to */
262 if (!skiplock
) hfs_lock(cp
, HFS_SHARED_LOCK
, HFS_LOCK_DEFAULT
);
263 struct vnode
*c_vp
= cp
->c_vp
;
265 /* we already have a data vnode */
267 vref
= vnode_ref(*data_vp
);
268 if (!skiplock
) hfs_unlock(cp
);
274 /* no data fork vnode in the cnode, so ask hfs for one. */
276 if (!cp
->c_rsrc_vp
) {
277 /* if we don't have either a c_vp or c_rsrc_vp, we can't really do anything useful */
279 if (!skiplock
) hfs_unlock(cp
);
283 if (0 == hfs_vget(VTOHFS(cp
->c_rsrc_vp
), cp
->c_cnid
, data_vp
, 1, 0) &&
285 vref
= vnode_ref(*data_vp
);
287 if (!skiplock
) hfs_unlock(cp
);
293 /* there was an error getting the vnode */
295 if (!skiplock
) hfs_unlock(cp
);
300 * hfs_lazy_init_decmpfs_cnode(): returns the decmpfs_cnode for a cnode,
301 * allocating it if necessary; returns NULL if there was an allocation error.
302 * function is non-static so that it can be used from the FCNTL handler.
305 hfs_lazy_init_decmpfs_cnode(struct cnode
*cp
)
308 decmpfs_cnode
*dp
= NULL
;
309 MALLOC_ZONE(dp
, decmpfs_cnode
*, sizeof(decmpfs_cnode
), M_DECMPFS_CNODE
, M_WAITOK
);
311 /* error allocating a decmpfs cnode */
314 decmpfs_cnode_init(dp
);
315 if (!OSCompareAndSwapPtr(NULL
, dp
, (void * volatile *)&cp
->c_decmp
)) {
316 /* another thread got here first, so free the decmpfs_cnode we allocated */
317 decmpfs_cnode_destroy(dp
);
318 FREE_ZONE(dp
, sizeof(*dp
), M_DECMPFS_CNODE
);
326 * hfs_file_is_compressed(): returns 1 if the file is compressed, and 0 (zero) if not.
327 * if the file's compressed flag is set, makes sure that the decmpfs_cnode field
328 * is allocated by calling hfs_lazy_init_decmpfs_cnode(), then makes sure it is populated,
329 * or else fills it in via the decmpfs_file_is_compressed() function.
332 hfs_file_is_compressed(struct cnode
*cp
, int skiplock
)
336 /* fast check to see if file is compressed. If flag is clear, just answer no */
337 if (!(cp
->c_bsdflags
& UF_COMPRESSED
)) {
341 decmpfs_cnode
*dp
= hfs_lazy_init_decmpfs_cnode(cp
);
343 /* error allocating a decmpfs cnode, treat the file as uncompressed */
347 /* flag was set, see if the decmpfs_cnode state is valid (zero == invalid) */
348 uint32_t decmpfs_state
= decmpfs_cnode_get_vnode_state(dp
);
349 switch(decmpfs_state
) {
350 case FILE_IS_COMPRESSED
:
351 case FILE_IS_CONVERTING
: /* treat decompressing files as if they are compressed */
353 case FILE_IS_NOT_COMPRESSED
:
355 /* otherwise the state is not cached yet */
358 /* decmpfs hasn't seen this file yet, so call decmpfs_file_is_compressed() to init the decmpfs_cnode struct */
359 struct vnode
*data_vp
= NULL
;
360 if (0 == hfs_ref_data_vp(cp
, &data_vp
, skiplock
)) {
362 ret
= decmpfs_file_is_compressed(data_vp
, VTOCMP(data_vp
)); // fill in decmpfs_cnode
369 /* hfs_uncompressed_size_of_compressed_file() - get the uncompressed size of the file.
370 * if the caller has passed a valid vnode (has a ref count > 0), then hfsmp and fid are not required.
371 * if the caller doesn't have a vnode, pass NULL in vp, and pass valid hfsmp and fid.
372 * files size is returned in size (required)
373 * if the indicated file is a directory (or something that doesn't have a data fork), then this call
374 * will return an error and the caller should fall back to treating the item as an uncompressed file
377 hfs_uncompressed_size_of_compressed_file(struct hfsmount
*hfsmp
, struct vnode
*vp
, cnid_t fid
, off_t
*size
, int skiplock
)
380 int putaway
= 0; /* flag to remember if we used hfs_vget() */
383 return EINVAL
; /* no place to put the file size */
387 if (!hfsmp
|| !fid
) { /* make sure we have the required parameters */
390 if (0 != hfs_vget(hfsmp
, fid
, &vp
, skiplock
, 0)) { /* vnode is null, use hfs_vget() to get it */
393 putaway
= 1; /* note that hfs_vget() was used to aquire the vnode */
396 /* this double check for compression (hfs_file_is_compressed)
397 * ensures the cached size is present in case decmpfs hasn't
398 * encountered this node yet.
401 if (hfs_file_is_compressed(VTOC(vp
), skiplock
) ) {
402 *size
= decmpfs_cnode_get_vnode_cached_size(VTOCMP(vp
)); /* file info will be cached now, so get size */
404 if (VTOCMP(vp
) && VTOCMP(vp
)->cmp_type
>= CMP_MAX
) {
405 if (VTOCMP(vp
)->cmp_type
!= DATALESS_CMPFS_TYPE
) {
406 // if we don't recognize this type, just use the real data fork size
407 if (VTOC(vp
)->c_datafork
) {
408 *size
= VTOC(vp
)->c_datafork
->ff_size
;
414 *size
= decmpfs_cnode_get_vnode_cached_size(VTOCMP(vp
)); /* file info will be cached now, so get size */
423 if (putaway
) { /* did we use hfs_vget() to get this vnode? */
424 vnode_put(vp
); /* if so, release it and set it to null */
431 hfs_hides_rsrc(vfs_context_t ctx
, struct cnode
*cp
, int skiplock
)
433 if (ctx
== decmpfs_ctx
)
435 if (!hfs_file_is_compressed(cp
, skiplock
))
437 return decmpfs_hides_rsrc(ctx
, cp
->c_decmp
);
441 hfs_hides_xattr(vfs_context_t ctx
, struct cnode
*cp
, const char *name
, int skiplock
)
443 if (ctx
== decmpfs_ctx
)
445 if (!hfs_file_is_compressed(cp
, skiplock
))
447 return decmpfs_hides_xattr(ctx
, cp
->c_decmp
, name
);
449 #endif /* HFS_COMPRESSION */
452 * Open a file/directory.
455 hfs_vnop_open(struct vnop_open_args
*ap
)
457 struct vnode
*vp
= ap
->a_vp
;
461 static int past_bootup
= 0;
462 struct cnode
*cp
= VTOC(vp
);
463 struct hfsmount
*hfsmp
= VTOHFS(vp
);
466 if (ap
->a_mode
& FWRITE
) {
468 if ( hfs_file_is_compressed(cp
, 1) ) { /* 1 == don't take the cnode lock */
469 /* opening a compressed file for write, so convert it to decompressed */
470 struct vnode
*data_vp
= NULL
;
471 error
= hfs_ref_data_vp(cp
, &data_vp
, 1); /* 1 == don't take the cnode lock */
474 error
= decmpfs_decompress_file(data_vp
, VTOCMP(data_vp
), -1, 1, 0);
485 if (hfs_file_is_compressed(cp
, 1) ) { /* 1 == don't take the cnode lock */
486 if (VNODE_IS_RSRC(vp
)) {
487 /* opening the resource fork of a compressed file, so nothing to do */
489 /* opening a compressed file for read, make sure it validates */
490 error
= decmpfs_validate_compressed_file(vp
, VTOCMP(vp
));
499 * Files marked append-only must be opened for appending.
501 if ((cp
->c_bsdflags
& APPEND
) && !vnode_isdir(vp
) &&
502 (ap
->a_mode
& (FWRITE
| O_APPEND
)) == FWRITE
)
505 if (vnode_isreg(vp
) && !UBCINFOEXISTS(vp
))
506 return (EBUSY
); /* file is in use by the kernel */
508 /* Don't allow journal to be opened externally. */
509 if (hfs_is_journal_file(hfsmp
, cp
))
512 if ((hfsmp
->hfs_flags
& HFS_READ_ONLY
) ||
513 (hfsmp
->jnl
== NULL
) ||
515 !vnode_isreg(vp
) || vnode_isinuse(vp
, 0) || vnode_isnamedstream(vp
)) {
517 !vnode_isreg(vp
) || vnode_isinuse(vp
, 0)) {
522 if ((error
= hfs_lock(cp
, HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
)))
526 /* If we're going to write to the file, initialize quotas. */
527 if ((ap
->a_mode
& FWRITE
) && (hfsmp
->hfs_flags
& HFS_QUOTAS
))
528 (void)hfs_getinoquota(cp
);
532 * On the first (non-busy) open of a fragmented
533 * file attempt to de-frag it (if its less than 20MB).
537 fp
->ff_extents
[7].blockCount
!= 0 &&
538 fp
->ff_size
<= (20 * 1024 * 1024)) {
542 * Wait until system bootup is done (3 min).
543 * And don't relocate a file that's been modified
544 * within the past minute -- this can lead to
550 if (tv
.tv_sec
> (60*3)) {
556 if ((now
.tv_sec
- cp
->c_mtime
) > 60) {
560 if (past_bootup
&& no_mods
) {
561 (void) hfs_relocate(vp
, hfsmp
->nextAllocation
+ 4096,
562 vfs_context_ucred(ap
->a_context
),
563 vfs_context_proc(ap
->a_context
));
574 * Close a file/directory.
578 struct vnop_close_args
/* {
581 vfs_context_t a_context;
584 register struct vnode
*vp
= ap
->a_vp
;
585 register struct cnode
*cp
;
586 struct proc
*p
= vfs_context_proc(ap
->a_context
);
587 struct hfsmount
*hfsmp
;
589 int tooktrunclock
= 0;
592 if ( hfs_lock(VTOC(vp
), HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
) != 0)
598 * If the rsrc fork is a named stream, it can cause the data fork to
599 * stay around, preventing de-allocation of these blocks.
600 * Do checks for truncation on close. Purge extra extents if they exist.
601 * Make sure the vp is not a directory, and that it has a resource fork,
602 * and that resource fork is also a named stream.
605 if ((vp
->v_type
== VREG
) && (cp
->c_rsrc_vp
)
606 && (vnode_isnamedstream(cp
->c_rsrc_vp
))) {
609 blks
= howmany(VTOF(vp
)->ff_size
, VTOVCB(vp
)->blockSize
);
611 * If there are extra blocks and there are only 2 refs on
612 * this vp (ourselves + rsrc fork holding ref on us), go ahead
613 * and try to truncate.
615 if ((blks
< VTOF(vp
)->ff_blocks
) && (!vnode_isinuse(vp
, 2))) {
616 // release cnode lock; must acquire truncate lock BEFORE cnode lock
619 hfs_lock_truncate(cp
, HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
);
622 if (hfs_lock(VTOC(vp
), HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
) != 0) {
623 hfs_unlock_truncate(cp
, HFS_LOCK_DEFAULT
);
624 // bail out if we can't re-acquire cnode lock
627 // now re-test to make sure it's still valid
629 knownrefs
= 1 + vnode_isnamedstream(cp
->c_rsrc_vp
);
630 if (!vnode_isinuse(vp
, knownrefs
)){
631 // now we can truncate the file, if necessary
632 blks
= howmany(VTOF(vp
)->ff_size
, VTOVCB(vp
)->blockSize
);
633 if (blks
< VTOF(vp
)->ff_blocks
){
634 (void) hfs_truncate(vp
, VTOF(vp
)->ff_size
, IO_NDELAY
, 0, 0, ap
->a_context
);
642 // if we froze the fs and we're exiting, then "thaw" the fs
643 if (hfsmp
->hfs_freezing_proc
== p
&& proc_exiting(p
)) {
644 hfsmp
->hfs_freezing_proc
= NULL
;
645 hfs_unlock_global (hfsmp
);
646 lck_rw_unlock_exclusive(&hfsmp
->hfs_insync
);
649 busy
= vnode_isinuse(vp
, 1);
652 hfs_touchtimes(VTOHFS(vp
), cp
);
654 if (vnode_isdir(vp
)) {
655 hfs_reldirhints(cp
, busy
);
656 } else if (vnode_issystem(vp
) && !busy
) {
661 hfs_unlock_truncate(cp
, HFS_LOCK_DEFAULT
);
665 if (ap
->a_fflag
& FWASWRITTEN
) {
666 hfs_sync_ejectable(hfsmp
);
673 * Get basic attributes.
676 hfs_vnop_getattr(struct vnop_getattr_args
*ap
)
678 #define VNODE_ATTR_TIMES \
679 (VNODE_ATTR_va_access_time|VNODE_ATTR_va_change_time|VNODE_ATTR_va_modify_time)
680 #define VNODE_ATTR_AUTH \
681 (VNODE_ATTR_va_mode | VNODE_ATTR_va_uid | VNODE_ATTR_va_gid | \
682 VNODE_ATTR_va_flags | VNODE_ATTR_va_acl)
684 struct vnode
*vp
= ap
->a_vp
;
685 struct vnode_attr
*vap
= ap
->a_vap
;
686 struct vnode
*rvp
= NULLVP
;
687 struct hfsmount
*hfsmp
;
695 /* we need to inspect the decmpfs state of the file before we take the hfs cnode lock */
698 off_t uncompressed_size
= -1;
699 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
)) {
700 /* we only care about whether the file is compressed if asked for the uncompressed size */
701 if (VNODE_IS_RSRC(vp
)) {
702 /* if it's a resource fork, decmpfs may want us to hide the size */
703 hide_size
= hfs_hides_rsrc(ap
->a_context
, cp
, 0);
705 /* if it's a data fork, we need to know if it was compressed so we can report the uncompressed size */
706 compressed
= hfs_file_is_compressed(cp
, 0);
708 if ((VATTR_IS_ACTIVE(vap
, va_data_size
) || VATTR_IS_ACTIVE(vap
, va_total_size
))) {
709 // if it's compressed
710 if (compressed
|| (!VNODE_IS_RSRC(vp
) && cp
->c_decmp
&& cp
->c_decmp
->cmp_type
>= CMP_MAX
)) {
711 if (0 != hfs_uncompressed_size_of_compressed_file(NULL
, vp
, 0, &uncompressed_size
, 0)) {
712 /* failed to get the uncompressed size, we'll check for this later */
713 uncompressed_size
= -1;
715 // fake that it's compressed
724 * Shortcut for vnode_authorize path. Each of the attributes
725 * in this set is updated atomically so we don't need to take
726 * the cnode lock to access them.
728 if ((vap
->va_active
& ~VNODE_ATTR_AUTH
) == 0) {
729 /* Make sure file still exists. */
730 if (cp
->c_flag
& C_NOEXISTS
)
733 vap
->va_uid
= cp
->c_uid
;
734 vap
->va_gid
= cp
->c_gid
;
735 vap
->va_mode
= cp
->c_mode
;
736 vap
->va_flags
= cp
->c_bsdflags
;
737 vap
->va_supported
|= VNODE_ATTR_AUTH
& ~VNODE_ATTR_va_acl
;
739 if ((cp
->c_attr
.ca_recflags
& kHFSHasSecurityMask
) == 0) {
740 vap
->va_acl
= (kauth_acl_t
) KAUTH_FILESEC_NONE
;
741 VATTR_SET_SUPPORTED(vap
, va_acl
);
748 v_type
= vnode_vtype(vp
);
750 * If time attributes are requested and we have cnode times
751 * that require updating, then acquire an exclusive lock on
752 * the cnode before updating the times. Otherwise we can
753 * just acquire a shared lock.
755 if ((vap
->va_active
& VNODE_ATTR_TIMES
) &&
756 (cp
->c_touch_acctime
|| cp
->c_touch_chgtime
|| cp
->c_touch_modtime
)) {
757 if ((error
= hfs_lock(cp
, HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
)))
759 hfs_touchtimes(hfsmp
, cp
);
762 if ((error
= hfs_lock(cp
, HFS_SHARED_LOCK
, HFS_LOCK_DEFAULT
)))
766 if (v_type
== VDIR
) {
767 data_size
= (cp
->c_entries
+ 2) * AVERAGE_HFSDIRENTRY_SIZE
;
769 if (VATTR_IS_ACTIVE(vap
, va_nlink
)) {
773 * For directories, the va_nlink is esentially a count
774 * of the ".." references to a directory plus the "."
775 * reference and the directory itself. So for HFS+ this
776 * becomes the sub-directory count plus two.
778 * In the absence of a sub-directory count we use the
779 * directory's item count. This will be too high in
780 * most cases since it also includes files.
782 if ((hfsmp
->hfs_flags
& HFS_FOLDERCOUNT
) &&
783 (cp
->c_attr
.ca_recflags
& kHFSHasFolderCountMask
))
784 nlink
= cp
->c_attr
.ca_dircount
; /* implied ".." entries */
786 nlink
= cp
->c_entries
;
788 /* Account for ourself and our "." entry */
790 /* Hide our private directories. */
791 if (cp
->c_cnid
== kHFSRootFolderID
) {
792 if (hfsmp
->hfs_private_desc
[FILE_HARDLINKS
].cd_cnid
!= 0) {
795 if (hfsmp
->hfs_private_desc
[DIR_HARDLINKS
].cd_cnid
!= 0) {
799 VATTR_RETURN(vap
, va_nlink
, (u_int64_t
)nlink
);
801 if (VATTR_IS_ACTIVE(vap
, va_nchildren
)) {
804 entries
= cp
->c_entries
;
805 /* Hide our private files and directories. */
806 if (cp
->c_cnid
== kHFSRootFolderID
) {
807 if (hfsmp
->hfs_private_desc
[FILE_HARDLINKS
].cd_cnid
!= 0)
809 if (hfsmp
->hfs_private_desc
[DIR_HARDLINKS
].cd_cnid
!= 0)
811 if (hfsmp
->jnl
|| ((hfsmp
->vcbAtrb
& kHFSVolumeJournaledMask
) && (hfsmp
->hfs_flags
& HFS_READ_ONLY
)))
812 entries
-= 2; /* hide the journal files */
814 VATTR_RETURN(vap
, va_nchildren
, entries
);
817 * The va_dirlinkcount is the count of real directory hard links.
818 * (i.e. its not the sum of the implied "." and ".." references)
820 if (VATTR_IS_ACTIVE(vap
, va_dirlinkcount
)) {
821 VATTR_RETURN(vap
, va_dirlinkcount
, (uint32_t)cp
->c_linkcount
);
824 data_size
= VCTOF(vp
, cp
)->ff_size
;
826 VATTR_RETURN(vap
, va_nlink
, (u_int64_t
)cp
->c_linkcount
);
827 if (VATTR_IS_ACTIVE(vap
, va_data_alloc
)) {
832 VATTR_RETURN(vap
, va_data_alloc
, 0);
833 } else if (compressed
) {
834 /* for compressed files, we report all allocated blocks as belonging to the data fork */
835 blocks
= cp
->c_blocks
;
836 VATTR_RETURN(vap
, va_data_alloc
, blocks
* (u_int64_t
)hfsmp
->blockSize
);
841 blocks
= VCTOF(vp
, cp
)->ff_blocks
;
842 VATTR_RETURN(vap
, va_data_alloc
, blocks
* (u_int64_t
)hfsmp
->blockSize
);
847 /* conditional because 64-bit arithmetic can be expensive */
848 if (VATTR_IS_ACTIVE(vap
, va_total_size
)) {
849 if (v_type
== VDIR
) {
850 VATTR_RETURN(vap
, va_total_size
, (cp
->c_entries
+ 2) * AVERAGE_HFSDIRENTRY_SIZE
);
852 u_int64_t total_size
= ~0ULL;
856 /* we're hiding the size of this file, so just return 0 */
858 } else if (compressed
) {
859 if (uncompressed_size
== -1) {
861 * We failed to get the uncompressed size above,
862 * so we'll fall back to the standard path below
863 * since total_size is still -1
866 /* use the uncompressed size we fetched above */
867 total_size
= uncompressed_size
;
871 if (total_size
== ~0ULL) {
872 if (cp
->c_datafork
) {
873 total_size
= cp
->c_datafork
->ff_size
;
876 if (cp
->c_blocks
- VTOF(vp
)->ff_blocks
) {
877 /* We deal with rsrc fork vnode iocount at the end of the function */
878 error
= hfs_vgetrsrc(hfsmp
, vp
, &rvp
, TRUE
, FALSE
);
881 * Note that we call hfs_vgetrsrc with error_on_unlinked
882 * set to FALSE. This is because we may be invoked via
883 * fstat() on an open-unlinked file descriptor and we must
884 * continue to support access to the rsrc fork until it disappears.
885 * The code at the end of this function will be
886 * responsible for releasing the iocount generated by
887 * hfs_vgetrsrc. This is because we can't drop the iocount
888 * without unlocking the cnode first.
894 if (rcp
&& rcp
->c_rsrcfork
) {
895 total_size
+= rcp
->c_rsrcfork
->ff_size
;
900 VATTR_RETURN(vap
, va_total_size
, total_size
);
903 if (VATTR_IS_ACTIVE(vap
, va_total_alloc
)) {
904 if (v_type
== VDIR
) {
905 VATTR_RETURN(vap
, va_total_alloc
, 0);
907 VATTR_RETURN(vap
, va_total_alloc
, (u_int64_t
)cp
->c_blocks
* (u_int64_t
)hfsmp
->blockSize
);
912 * If the VFS wants extended security data, and we know that we
913 * don't have any (because it never told us it was setting any)
914 * then we can return the supported bit and no data. If we do
915 * have extended security, we can just leave the bit alone and
916 * the VFS will use the fallback path to fetch it.
918 if (VATTR_IS_ACTIVE(vap
, va_acl
)) {
919 if ((cp
->c_attr
.ca_recflags
& kHFSHasSecurityMask
) == 0) {
920 vap
->va_acl
= (kauth_acl_t
) KAUTH_FILESEC_NONE
;
921 VATTR_SET_SUPPORTED(vap
, va_acl
);
924 if (VATTR_IS_ACTIVE(vap
, va_access_time
)) {
925 /* Access times are lazily updated, get current time if needed */
926 if (cp
->c_touch_acctime
) {
930 vap
->va_access_time
.tv_sec
= tv
.tv_sec
;
932 vap
->va_access_time
.tv_sec
= cp
->c_atime
;
934 vap
->va_access_time
.tv_nsec
= 0;
935 VATTR_SET_SUPPORTED(vap
, va_access_time
);
937 vap
->va_create_time
.tv_sec
= cp
->c_itime
;
938 vap
->va_create_time
.tv_nsec
= 0;
939 vap
->va_modify_time
.tv_sec
= cp
->c_mtime
;
940 vap
->va_modify_time
.tv_nsec
= 0;
941 vap
->va_change_time
.tv_sec
= cp
->c_ctime
;
942 vap
->va_change_time
.tv_nsec
= 0;
943 vap
->va_backup_time
.tv_sec
= cp
->c_btime
;
944 vap
->va_backup_time
.tv_nsec
= 0;
946 /* See if we need to emit the date added field to the user */
947 if (VATTR_IS_ACTIVE(vap
, va_addedtime
)) {
948 u_int32_t dateadded
= hfs_get_dateadded (cp
);
950 vap
->va_addedtime
.tv_sec
= dateadded
;
951 vap
->va_addedtime
.tv_nsec
= 0;
952 VATTR_SET_SUPPORTED (vap
, va_addedtime
);
956 /* XXX is this really a good 'optimal I/O size'? */
957 vap
->va_iosize
= hfsmp
->hfs_logBlockSize
;
958 vap
->va_uid
= cp
->c_uid
;
959 vap
->va_gid
= cp
->c_gid
;
960 vap
->va_mode
= cp
->c_mode
;
961 vap
->va_flags
= cp
->c_bsdflags
;
964 * Exporting file IDs from HFS Plus:
966 * For "normal" files the c_fileid is the same value as the
967 * c_cnid. But for hard link files, they are different - the
968 * c_cnid belongs to the active directory entry (ie the link)
969 * and the c_fileid is for the actual inode (ie the data file).
971 * The stat call (getattr) uses va_fileid and the Carbon APIs,
972 * which are hardlink-ignorant, will ask for va_linkid.
974 vap
->va_fileid
= (u_int64_t
)cp
->c_fileid
;
976 * We need to use the origin cache for both hardlinked files
977 * and directories. Hardlinked directories have multiple cnids
978 * and parents (one per link). Hardlinked files also have their
979 * own parents and link IDs separate from the indirect inode number.
980 * If we don't use the cache, we could end up vending the wrong ID
981 * because the cnode will only reflect the link that was looked up most recently.
983 if (cp
->c_flag
& C_HARDLINK
) {
984 vap
->va_linkid
= (u_int64_t
)hfs_currentcnid(cp
);
985 vap
->va_parentid
= (u_int64_t
)hfs_currentparent(cp
);
987 vap
->va_linkid
= (u_int64_t
)cp
->c_cnid
;
988 vap
->va_parentid
= (u_int64_t
)cp
->c_parentcnid
;
990 vap
->va_fsid
= hfsmp
->hfs_raw_dev
;
992 vap
->va_encoding
= cp
->c_encoding
;
993 vap
->va_rdev
= (v_type
== VBLK
|| v_type
== VCHR
) ? cp
->c_rdev
: 0;
995 if (VATTR_IS_ACTIVE(vap
, va_data_size
)) {
997 vap
->va_data_size
= 0;
998 else if (compressed
) {
999 if (uncompressed_size
== -1) {
1000 /* failed to get the uncompressed size above, so just return data_size */
1001 vap
->va_data_size
= data_size
;
1003 /* use the uncompressed size we fetched above */
1004 vap
->va_data_size
= uncompressed_size
;
1007 vap
->va_data_size
= data_size
;
1008 // vap->va_supported |= VNODE_ATTR_va_data_size;
1009 VATTR_SET_SUPPORTED(vap
, va_data_size
);
1012 vap
->va_data_size
= data_size
;
1013 vap
->va_supported
|= VNODE_ATTR_va_data_size
;
1016 /* Mark them all at once instead of individual VATTR_SET_SUPPORTED calls. */
1017 vap
->va_supported
|= VNODE_ATTR_va_create_time
| VNODE_ATTR_va_modify_time
|
1018 VNODE_ATTR_va_change_time
| VNODE_ATTR_va_backup_time
|
1019 VNODE_ATTR_va_iosize
| VNODE_ATTR_va_uid
|
1020 VNODE_ATTR_va_gid
| VNODE_ATTR_va_mode
|
1021 VNODE_ATTR_va_flags
|VNODE_ATTR_va_fileid
|
1022 VNODE_ATTR_va_linkid
| VNODE_ATTR_va_parentid
|
1023 VNODE_ATTR_va_fsid
| VNODE_ATTR_va_filerev
|
1024 VNODE_ATTR_va_encoding
| VNODE_ATTR_va_rdev
;
1026 /* If this is the root, let VFS to find out the mount name, which
1027 * may be different from the real name. Otherwise, we need to take care
1028 * for hardlinked files, which need to be looked up, if necessary
1030 if (VATTR_IS_ACTIVE(vap
, va_name
) && (cp
->c_cnid
!= kHFSRootFolderID
)) {
1031 struct cat_desc linkdesc
;
1033 int uselinkdesc
= 0;
1034 cnid_t nextlinkid
= 0;
1035 cnid_t prevlinkid
= 0;
1037 /* Get the name for ATTR_CMN_NAME. We need to take special care for hardlinks
1038 * here because the info. for the link ID requested by getattrlist may be
1039 * different than what's currently in the cnode. This is because the cnode
1040 * will be filled in with the information for the most recent link ID that went
1041 * through namei/lookup(). If there are competing lookups for hardlinks that point
1042 * to the same inode, one (or more) getattrlists could be vended incorrect name information.
1043 * Also, we need to beware of open-unlinked files which could have a namelen of 0.
1046 if ((cp
->c_flag
& C_HARDLINK
) &&
1047 ((cp
->c_desc
.cd_namelen
== 0) || (vap
->va_linkid
!= cp
->c_cnid
))) {
1049 * If we have no name and our link ID is the raw inode number, then we may
1050 * have an open-unlinked file. Go to the next link in this case.
1052 if ((cp
->c_desc
.cd_namelen
== 0) && (vap
->va_linkid
== cp
->c_fileid
)) {
1053 if ((error
= hfs_lookup_siblinglinks(hfsmp
, vap
->va_linkid
, &prevlinkid
, &nextlinkid
))){
1058 /* just use link obtained from vap above */
1059 nextlinkid
= vap
->va_linkid
;
1062 /* We need to probe the catalog for the descriptor corresponding to the link ID
1063 * stored in nextlinkid. Note that we don't know if we have the exclusive lock
1064 * for the cnode here, so we can't just update the descriptor. Instead,
1065 * we should just store the descriptor's value locally and then use it to pass
1066 * out the name value as needed below.
1069 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
1070 error
= cat_findname(hfsmp
, nextlinkid
, &linkdesc
);
1071 hfs_systemfile_unlock(hfsmp
, lockflags
);
1078 /* By this point, we've either patched up the name above and the c_desc
1079 * points to the correct data, or it already did, in which case we just proceed
1080 * by copying the name into the vap. Note that we will never set va_name to
1081 * supported if nextlinkid is never initialized. This could happen in the degenerate
1082 * case above involving the raw inode number, where it has no nextlinkid. In this case
1083 * we will simply not mark the name bit as supported.
1086 strlcpy(vap
->va_name
, (const char*) linkdesc
.cd_nameptr
, MAXPATHLEN
);
1087 VATTR_SET_SUPPORTED(vap
, va_name
);
1088 cat_releasedesc(&linkdesc
);
1090 else if (cp
->c_desc
.cd_namelen
) {
1091 strlcpy(vap
->va_name
, (const char*) cp
->c_desc
.cd_nameptr
, MAXPATHLEN
);
1092 VATTR_SET_SUPPORTED(vap
, va_name
);
1099 * We need to vnode_put the rsrc fork vnode only *after* we've released
1100 * the cnode lock, since vnode_put can trigger an inactive call, which
1101 * will go back into HFS and try to acquire a cnode lock.
1111 hfs_vnop_setattr(ap
)
1112 struct vnop_setattr_args
/* {
1114 struct vnode_attr *a_vap;
1115 vfs_context_t a_context;
1118 struct vnode_attr
*vap
= ap
->a_vap
;
1119 struct vnode
*vp
= ap
->a_vp
;
1120 struct cnode
*cp
= NULL
;
1121 struct hfsmount
*hfsmp
;
1122 kauth_cred_t cred
= vfs_context_ucred(ap
->a_context
);
1123 struct proc
*p
= vfs_context_proc(ap
->a_context
);
1129 orig_ctime
= VTOC(vp
)->c_ctime
;
1132 int decmpfs_reset_state
= 0;
1134 we call decmpfs_update_attributes even if the file is not compressed
1135 because we want to update the incoming flags if the xattrs are invalid
1137 error
= decmpfs_update_attributes(vp
, vap
);
1142 // if this is not a size-changing setattr and it is not just
1143 // an atime update, then check for a snapshot.
1145 if (!VATTR_IS_ACTIVE(vap
, va_data_size
) && !(vap
->va_active
== VNODE_ATTR_va_access_time
)) {
1146 check_for_tracked_file(vp
, orig_ctime
, NAMESPACE_HANDLER_METADATA_MOD
, NSPACE_REARM_NO_ARG
);
1150 if ((error
= cp_handle_vnop(vp
, CP_WRITE_ACCESS
, 0)) != 0) {
1153 #endif /* CONFIG_PROTECT */
1157 /* Don't allow modification of the journal. */
1158 if (hfs_is_journal_file(hfsmp
, VTOC(vp
))) {
1163 * File size change request.
1164 * We are guaranteed that this is not a directory, and that
1165 * the filesystem object is writeable.
1167 * NOTE: HFS COMPRESSION depends on the data_size being set *before* the bsd flags are updated
1169 VATTR_SET_SUPPORTED(vap
, va_data_size
);
1170 if (VATTR_IS_ACTIVE(vap
, va_data_size
) && !vnode_islnk(vp
)) {
1172 /* keep the compressed state locked until we're done truncating the file */
1173 decmpfs_cnode
*dp
= VTOCMP(vp
);
1176 * call hfs_lazy_init_decmpfs_cnode() to make sure that the decmpfs_cnode
1177 * is filled in; we need a decmpfs_cnode to lock out decmpfs state changes
1178 * on this file while it's truncating
1180 dp
= hfs_lazy_init_decmpfs_cnode(VTOC(vp
));
1182 /* failed to allocate a decmpfs_cnode */
1183 return ENOMEM
; /* what should this be? */
1187 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
);
1189 decmpfs_lock_compressed_data(dp
, 1);
1190 if (hfs_file_is_compressed(VTOC(vp
), 1)) {
1191 error
= decmpfs_decompress_file(vp
, dp
, -1/*vap->va_data_size*/, 0, 1);
1193 decmpfs_unlock_compressed_data(dp
, 1);
1199 /* Take truncate lock before taking cnode lock. */
1200 hfs_lock_truncate(VTOC(vp
), HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
);
1202 /* Perform the ubc_setsize before taking the cnode lock. */
1203 ubc_setsize(vp
, vap
->va_data_size
);
1205 if ((error
= hfs_lock(VTOC(vp
), HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
))) {
1206 hfs_unlock_truncate(VTOC(vp
), HFS_LOCK_DEFAULT
);
1208 decmpfs_unlock_compressed_data(dp
, 1);
1214 error
= hfs_truncate(vp
, vap
->va_data_size
, vap
->va_vaflags
& 0xffff, 1, 0, ap
->a_context
);
1216 hfs_unlock_truncate(cp
, HFS_LOCK_DEFAULT
);
1218 decmpfs_unlock_compressed_data(dp
, 1);
1224 if ((error
= hfs_lock(VTOC(vp
), HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
)))
1230 * If it is just an access time update request by itself
1231 * we know the request is from kernel level code, and we
1232 * can delay it without being as worried about consistency.
1233 * This change speeds up mmaps, in the rare case that they
1234 * get caught behind a sync.
1237 if (vap
->va_active
== VNODE_ATTR_va_access_time
) {
1238 cp
->c_touch_acctime
=TRUE
;
1245 * Owner/group change request.
1246 * We are guaranteed that the new owner/group is valid and legal.
1248 VATTR_SET_SUPPORTED(vap
, va_uid
);
1249 VATTR_SET_SUPPORTED(vap
, va_gid
);
1250 nuid
= VATTR_IS_ACTIVE(vap
, va_uid
) ? vap
->va_uid
: (uid_t
)VNOVAL
;
1251 ngid
= VATTR_IS_ACTIVE(vap
, va_gid
) ? vap
->va_gid
: (gid_t
)VNOVAL
;
1252 if (((nuid
!= (uid_t
)VNOVAL
) || (ngid
!= (gid_t
)VNOVAL
)) &&
1253 ((error
= hfs_chown(vp
, nuid
, ngid
, cred
, p
)) != 0))
1257 * Mode change request.
1258 * We are guaranteed that the mode value is valid and that in
1259 * conjunction with the owner and group, this change is legal.
1261 VATTR_SET_SUPPORTED(vap
, va_mode
);
1262 if (VATTR_IS_ACTIVE(vap
, va_mode
) &&
1263 ((error
= hfs_chmod(vp
, (int)vap
->va_mode
, cred
, p
)) != 0))
1267 * File flags change.
1268 * We are guaranteed that only flags allowed to change given the
1269 * current securelevel are being changed.
1271 VATTR_SET_SUPPORTED(vap
, va_flags
);
1272 if (VATTR_IS_ACTIVE(vap
, va_flags
)) {
1276 if ((cp
->c_bsdflags
^ vap
->va_flags
) & UF_COMPRESSED
) {
1278 * the UF_COMPRESSED was toggled, so reset our cached compressed state
1279 * but we don't want to actually do the update until we've released the cnode lock down below
1280 * NOTE: turning the flag off doesn't actually decompress the file, so that we can
1281 * turn off the flag and look at the "raw" file for debugging purposes
1283 decmpfs_reset_state
= 1;
1287 cp
->c_bsdflags
= vap
->va_flags
;
1288 cp
->c_touch_chgtime
= TRUE
;
1291 * Mirror the UF_HIDDEN flag to the invisible bit of the Finder Info.
1293 * The fdFlags for files and frFlags for folders are both 8 bytes
1294 * into the userInfo (the first 16 bytes of the Finder Info). They
1295 * are both 16-bit fields.
1297 fdFlags
= (u_int16_t
*) &cp
->c_finderinfo
[8];
1298 if (vap
->va_flags
& UF_HIDDEN
)
1299 *fdFlags
|= OSSwapHostToBigConstInt16(kFinderInvisibleMask
);
1301 *fdFlags
&= ~OSSwapHostToBigConstInt16(kFinderInvisibleMask
);
1305 * Timestamp updates.
1307 VATTR_SET_SUPPORTED(vap
, va_create_time
);
1308 VATTR_SET_SUPPORTED(vap
, va_access_time
);
1309 VATTR_SET_SUPPORTED(vap
, va_modify_time
);
1310 VATTR_SET_SUPPORTED(vap
, va_backup_time
);
1311 VATTR_SET_SUPPORTED(vap
, va_change_time
);
1312 if (VATTR_IS_ACTIVE(vap
, va_create_time
) ||
1313 VATTR_IS_ACTIVE(vap
, va_access_time
) ||
1314 VATTR_IS_ACTIVE(vap
, va_modify_time
) ||
1315 VATTR_IS_ACTIVE(vap
, va_backup_time
)) {
1316 if (VATTR_IS_ACTIVE(vap
, va_create_time
))
1317 cp
->c_itime
= vap
->va_create_time
.tv_sec
;
1318 if (VATTR_IS_ACTIVE(vap
, va_access_time
)) {
1319 cp
->c_atime
= vap
->va_access_time
.tv_sec
;
1320 cp
->c_touch_acctime
= FALSE
;
1322 if (VATTR_IS_ACTIVE(vap
, va_modify_time
)) {
1323 cp
->c_mtime
= vap
->va_modify_time
.tv_sec
;
1324 cp
->c_touch_modtime
= FALSE
;
1325 cp
->c_touch_chgtime
= TRUE
;
1328 * The utimes system call can reset the modification
1329 * time but it doesn't know about HFS create times.
1330 * So we need to ensure that the creation time is
1331 * always at least as old as the modification time.
1333 if ((VTOVCB(vp
)->vcbSigWord
== kHFSPlusSigWord
) &&
1334 (cp
->c_cnid
!= kHFSRootFolderID
) &&
1335 (cp
->c_mtime
< cp
->c_itime
)) {
1336 cp
->c_itime
= cp
->c_mtime
;
1339 if (VATTR_IS_ACTIVE(vap
, va_backup_time
))
1340 cp
->c_btime
= vap
->va_backup_time
.tv_sec
;
1341 cp
->c_flag
|= C_MODIFIED
;
1345 * Set name encoding.
1347 VATTR_SET_SUPPORTED(vap
, va_encoding
);
1348 if (VATTR_IS_ACTIVE(vap
, va_encoding
)) {
1349 cp
->c_encoding
= vap
->va_encoding
;
1350 hfs_setencodingbits(hfsmp
, cp
->c_encoding
);
1353 if ((error
= hfs_update(vp
, TRUE
)) != 0)
1357 /* Purge origin cache for cnode, since caller now has correct link ID for it
1358 * We purge it here since it was acquired for us during lookup, and we no longer need it.
1360 if ((cp
->c_flag
& C_HARDLINK
) && (vp
->v_type
!= VDIR
)){
1361 hfs_relorigin(cp
, 0);
1366 if (decmpfs_reset_state
) {
1368 * we've changed the UF_COMPRESSED flag, so reset the decmpfs state for this cnode
1369 * but don't do it while holding the hfs cnode lock
1371 decmpfs_cnode
*dp
= VTOCMP(vp
);
1374 * call hfs_lazy_init_decmpfs_cnode() to make sure that the decmpfs_cnode
1375 * is filled in; we need a decmpfs_cnode to prevent decmpfs state changes
1376 * on this file if it's locked
1378 dp
= hfs_lazy_init_decmpfs_cnode(VTOC(vp
));
1380 /* failed to allocate a decmpfs_cnode */
1381 return ENOMEM
; /* what should this be? */
1384 decmpfs_cnode_set_vnode_state(dp
, FILE_TYPE_UNKNOWN
, 0);
1393 * Change the mode on a file.
1394 * cnode must be locked before calling.
1397 hfs_chmod(struct vnode
*vp
, int mode
, __unused kauth_cred_t cred
, __unused
struct proc
*p
)
1399 register struct cnode
*cp
= VTOC(vp
);
1401 if (VTOVCB(vp
)->vcbSigWord
!= kHFSPlusSigWord
)
1404 // Don't allow modification of the journal or journal_info_block
1405 if (hfs_is_journal_file(VTOHFS(vp
), cp
)) {
1409 #if OVERRIDE_UNKNOWN_PERMISSIONS
1410 if (((unsigned int)vfs_flags(VTOVFS(vp
))) & MNT_UNKNOWNPERMISSIONS
) {
1414 cp
->c_mode
&= ~ALLPERMS
;
1415 cp
->c_mode
|= (mode
& ALLPERMS
);
1416 cp
->c_touch_chgtime
= TRUE
;
1422 hfs_write_access(struct vnode
*vp
, kauth_cred_t cred
, struct proc
*p
, Boolean considerFlags
)
1424 struct cnode
*cp
= VTOC(vp
);
1429 * Disallow write attempts on read-only file systems;
1430 * unless the file is a socket, fifo, or a block or
1431 * character device resident on the file system.
1433 switch (vnode_vtype(vp
)) {
1437 if (VTOHFS(vp
)->hfs_flags
& HFS_READ_ONLY
)
1444 /* If immutable bit set, nobody gets to write it. */
1445 if (considerFlags
&& (cp
->c_bsdflags
& IMMUTABLE
))
1448 /* Otherwise, user id 0 always gets access. */
1449 if (!suser(cred
, NULL
))
1452 /* Otherwise, check the owner. */
1453 if ((retval
= hfs_owner_rights(VTOHFS(vp
), cp
->c_uid
, cred
, p
, false)) == 0)
1454 return ((cp
->c_mode
& S_IWUSR
) == S_IWUSR
? 0 : EACCES
);
1456 /* Otherwise, check the groups. */
1457 if (kauth_cred_ismember_gid(cred
, cp
->c_gid
, &is_member
) == 0 && is_member
) {
1458 return ((cp
->c_mode
& S_IWGRP
) == S_IWGRP
? 0 : EACCES
);
1461 /* Otherwise, check everyone else. */
1462 return ((cp
->c_mode
& S_IWOTH
) == S_IWOTH
? 0 : EACCES
);
1467 * Perform chown operation on cnode cp;
1468 * code must be locked prior to call.
1472 hfs_chown(struct vnode
*vp
, uid_t uid
, gid_t gid
, __unused kauth_cred_t cred
,
1473 __unused
struct proc
*p
)
1475 hfs_chown(struct vnode
*vp
, uid_t uid
, gid_t gid
, kauth_cred_t cred
,
1476 __unused
struct proc
*p
)
1479 register struct cnode
*cp
= VTOC(vp
);
1488 if (VTOVCB(vp
)->vcbSigWord
!= kHFSPlusSigWord
)
1491 if (((unsigned int)vfs_flags(VTOVFS(vp
))) & MNT_UNKNOWNPERMISSIONS
)
1494 if (uid
== (uid_t
)VNOVAL
)
1496 if (gid
== (gid_t
)VNOVAL
)
1499 #if 0 /* we are guaranteed that this is already the case */
1501 * If we don't own the file, are trying to change the owner
1502 * of the file, or are not a member of the target group,
1503 * the caller must be superuser or the call fails.
1505 if ((kauth_cred_getuid(cred
) != cp
->c_uid
|| uid
!= cp
->c_uid
||
1506 (gid
!= cp
->c_gid
&&
1507 (kauth_cred_ismember_gid(cred
, gid
, &is_member
) || !is_member
))) &&
1508 (error
= suser(cred
, 0)))
1515 if ((error
= hfs_getinoquota(cp
)))
1518 dqrele(cp
->c_dquot
[USRQUOTA
]);
1519 cp
->c_dquot
[USRQUOTA
] = NODQUOT
;
1522 dqrele(cp
->c_dquot
[GRPQUOTA
]);
1523 cp
->c_dquot
[GRPQUOTA
] = NODQUOT
;
1527 * Eventually need to account for (fake) a block per directory
1528 * if (vnode_isdir(vp))
1529 * change = VTOHFS(vp)->blockSize;
1533 change
= (int64_t)(cp
->c_blocks
) * (int64_t)VTOVCB(vp
)->blockSize
;
1534 (void) hfs_chkdq(cp
, -change
, cred
, CHOWN
);
1535 (void) hfs_chkiq(cp
, -1, cred
, CHOWN
);
1536 for (i
= 0; i
< MAXQUOTAS
; i
++) {
1537 dqrele(cp
->c_dquot
[i
]);
1538 cp
->c_dquot
[i
] = NODQUOT
;
1544 if ((error
= 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 if ((error
= hfs_chkdq(cp
, change
, cred
, CHOWN
)) == 0) {
1554 if ((error
= hfs_chkiq(cp
, 1, cred
, CHOWN
)) == 0)
1557 (void) hfs_chkdq(cp
, -change
, cred
, CHOWN
|FORCE
);
1559 for (i
= 0; i
< MAXQUOTAS
; i
++) {
1560 dqrele(cp
->c_dquot
[i
]);
1561 cp
->c_dquot
[i
] = NODQUOT
;
1566 if (hfs_getinoquota(cp
) == 0) {
1568 dqrele(cp
->c_dquot
[USRQUOTA
]);
1569 cp
->c_dquot
[USRQUOTA
] = NODQUOT
;
1572 dqrele(cp
->c_dquot
[GRPQUOTA
]);
1573 cp
->c_dquot
[GRPQUOTA
] = NODQUOT
;
1575 (void) hfs_chkdq(cp
, change
, cred
, FORCE
|CHOWN
);
1576 (void) hfs_chkiq(cp
, 1, cred
, FORCE
|CHOWN
);
1577 (void) hfs_getinoquota(cp
);
1581 if (hfs_getinoquota(cp
))
1582 panic("hfs_chown: lost quota");
1587 According to the SUSv3 Standard, chown() shall mark
1588 for update the st_ctime field of the file.
1589 (No exceptions mentioned)
1591 cp
->c_touch_chgtime
= TRUE
;
1597 * hfs_vnop_exchange:
1600 * 'from' vnode/cnode
1606 * hfs_vnop_exchange is used to service the exchangedata(2) system call.
1607 * Per the requirements of that system call, this function "swaps" some
1608 * of the information that lives in one catalog record for some that
1609 * lives in another. Note that not everything is swapped; in particular,
1610 * the extent information stored in each cnode is kept local to that
1611 * cnode. This allows existing file descriptor references to continue
1612 * to operate on the same content, regardless of the location in the
1613 * namespace that the file may have moved to. See inline comments
1614 * in the function for more information.
1617 hfs_vnop_exchange(ap
)
1618 struct vnop_exchange_args
/* {
1619 struct vnode *a_fvp;
1620 struct vnode *a_tvp;
1622 vfs_context_t a_context;
1625 struct vnode
*from_vp
= ap
->a_fvp
;
1626 struct vnode
*to_vp
= ap
->a_tvp
;
1627 struct cnode
*from_cp
;
1628 struct cnode
*to_cp
;
1629 struct hfsmount
*hfsmp
;
1630 struct cat_desc tempdesc
;
1631 struct cat_attr tempattr
;
1632 const unsigned char *from_nameptr
;
1633 const unsigned char *to_nameptr
;
1634 char from_iname
[32];
1636 uint32_t to_flag_special
;
1637 uint32_t from_flag_special
;
1641 int error
= 0, started_tr
= 0, got_cookie
= 0;
1642 cat_cookie_t cookie
;
1643 time_t orig_from_ctime
, orig_to_ctime
;
1646 * VFS does the following checks:
1647 * 1. Validate that both are files.
1648 * 2. Validate that both are on the same mount.
1649 * 3. Validate that they're not the same vnode.
1652 orig_from_ctime
= VTOC(from_vp
)->c_ctime
;
1653 orig_to_ctime
= VTOC(to_vp
)->c_ctime
;
1658 * Do not allow exchangedata/F_MOVEDATAEXTENTS on data-protected filesystems
1659 * because the EAs will not be swapped. As a result, the persistent keys would not
1660 * match and the files will be garbage.
1662 if (cp_fs_protected (vnode_mount(from_vp
))) {
1668 if ( hfs_file_is_compressed(VTOC(from_vp
), 0) ) {
1669 if ( 0 != ( error
= decmpfs_decompress_file(from_vp
, VTOCMP(from_vp
), -1, 0, 1) ) ) {
1674 if ( hfs_file_is_compressed(VTOC(to_vp
), 0) ) {
1675 if ( 0 != ( error
= decmpfs_decompress_file(to_vp
, VTOCMP(to_vp
), -1, 0, 1) ) ) {
1679 #endif // HFS_COMPRESSION
1682 * Normally, we want to notify the user handlers about the event,
1683 * except if it's a handler driving the event.
1685 if ((ap
->a_options
& FSOPT_EXCHANGE_DATA_ONLY
) == 0) {
1686 check_for_tracked_file(from_vp
, orig_from_ctime
, NAMESPACE_HANDLER_WRITE_OP
, NULL
);
1687 check_for_tracked_file(to_vp
, orig_to_ctime
, NAMESPACE_HANDLER_WRITE_OP
, NULL
);
1690 * We're doing a data-swap.
1691 * Take the truncate lock/cnode lock, then verify there are no mmap references.
1692 * Issue a hfs_filedone to flush out all of the remaining state for this file.
1693 * Allow the rest of the codeflow to re-acquire the cnode locks in order.
1696 hfs_lock_truncate (VTOC(from_vp
), HFS_SHARED_LOCK
, HFS_LOCK_DEFAULT
);
1698 if ((error
= hfs_lock(VTOC(from_vp
), HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
))) {
1699 hfs_unlock_truncate (VTOC(from_vp
), HFS_LOCK_DEFAULT
);
1703 /* Verify the source file is not in use by anyone besides us (including mmap refs) */
1704 if (vnode_isinuse(from_vp
, 1)) {
1706 hfs_unlock(VTOC(from_vp
));
1707 hfs_unlock_truncate (VTOC(from_vp
), HFS_LOCK_DEFAULT
);
1711 /* Flush out the data in the source file */
1712 VTOC(from_vp
)->c_flag
|= C_SWAPINPROGRESS
;
1713 error
= hfs_filedone (from_vp
, ap
->a_context
);
1714 VTOC(from_vp
)->c_flag
&= ~C_SWAPINPROGRESS
;
1715 hfs_unlock(VTOC(from_vp
));
1716 hfs_unlock_truncate(VTOC(from_vp
), HFS_LOCK_DEFAULT
);
1723 if ((error
= hfs_lockpair(VTOC(from_vp
), VTOC(to_vp
), HFS_EXCLUSIVE_LOCK
)))
1726 from_cp
= VTOC(from_vp
);
1727 to_cp
= VTOC(to_vp
);
1728 hfsmp
= VTOHFS(from_vp
);
1730 /* Resource forks cannot be exchanged. */
1731 if ( VNODE_IS_RSRC(from_vp
) || VNODE_IS_RSRC(to_vp
)) {
1736 // Don't allow modification of the journal or journal_info_block
1737 if (hfs_is_journal_file(hfsmp
, from_cp
) ||
1738 hfs_is_journal_file(hfsmp
, to_cp
)) {
1744 * Ok, now that all of the pre-flighting is done, call the underlying
1745 * function if needed.
1747 if (ap
->a_options
& FSOPT_EXCHANGE_DATA_ONLY
) {
1748 error
= hfs_movedata(from_vp
, to_vp
);
1753 if ((error
= hfs_start_transaction(hfsmp
)) != 0) {
1759 * Reserve some space in the Catalog file.
1761 if ((error
= cat_preflight(hfsmp
, CAT_EXCHANGE
, &cookie
, vfs_context_proc(ap
->a_context
)))) {
1766 /* The backend code always tries to delete the virtual
1767 * extent id for exchanging files so we need to lock
1768 * the extents b-tree.
1770 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
| SFL_EXTENTS
| SFL_ATTRIBUTE
, HFS_EXCLUSIVE_LOCK
);
1772 /* Account for the location of the catalog objects. */
1773 if (from_cp
->c_flag
& C_HARDLINK
) {
1774 MAKE_INODE_NAME(from_iname
, sizeof(from_iname
),
1775 from_cp
->c_attr
.ca_linkref
);
1776 from_nameptr
= (unsigned char *)from_iname
;
1777 from_parid
= hfsmp
->hfs_private_desc
[FILE_HARDLINKS
].cd_cnid
;
1778 from_cp
->c_hint
= 0;
1780 from_nameptr
= from_cp
->c_desc
.cd_nameptr
;
1781 from_parid
= from_cp
->c_parentcnid
;
1783 if (to_cp
->c_flag
& C_HARDLINK
) {
1784 MAKE_INODE_NAME(to_iname
, sizeof(to_iname
),
1785 to_cp
->c_attr
.ca_linkref
);
1786 to_nameptr
= (unsigned char *)to_iname
;
1787 to_parid
= hfsmp
->hfs_private_desc
[FILE_HARDLINKS
].cd_cnid
;
1790 to_nameptr
= to_cp
->c_desc
.cd_nameptr
;
1791 to_parid
= to_cp
->c_parentcnid
;
1795 * ExchangeFileIDs swaps the extent information attached to two
1796 * different file IDs. It also swaps the extent information that
1797 * may live in the extents-overflow B-Tree.
1799 * We do this in a transaction as this may require a lot of B-Tree nodes
1800 * to do completely, particularly if one of the files in question
1801 * has a lot of extents.
1803 * For example, assume "file1" has fileID 50, and "file2" has fileID 52.
1804 * For the on-disk records, which are assumed to be synced, we will
1805 * first swap the resident inline-8 extents as part of the catalog records.
1806 * Then we will swap any extents overflow records for each file.
1808 * When this function is done, "file1" will have fileID 52, and "file2" will
1811 error
= ExchangeFileIDs(hfsmp
, from_nameptr
, to_nameptr
, from_parid
,
1812 to_parid
, from_cp
->c_hint
, to_cp
->c_hint
);
1813 hfs_systemfile_unlock(hfsmp
, lockflags
);
1816 * Note that we don't need to exchange any extended attributes
1817 * since the attributes are keyed by file ID.
1820 if (error
!= E_NONE
) {
1821 error
= MacToVFSError(error
);
1825 /* Purge the vnodes from the name cache */
1827 cache_purge(from_vp
);
1831 /* Bump both source and destination write counts before any swaps. */
1833 hfs_incr_gencount (from_cp
);
1834 hfs_incr_gencount (to_cp
);
1838 /* Save a copy of "from" attributes before swapping. */
1839 bcopy(&from_cp
->c_desc
, &tempdesc
, sizeof(struct cat_desc
));
1840 bcopy(&from_cp
->c_attr
, &tempattr
, sizeof(struct cat_attr
));
1842 /* Save whether or not each cnode is a hardlink or has EAs */
1843 from_flag_special
= from_cp
->c_flag
& (C_HARDLINK
| C_HASXATTRS
);
1844 to_flag_special
= to_cp
->c_flag
& (C_HARDLINK
| C_HASXATTRS
);
1846 /* Drop the special bits from each cnode */
1847 from_cp
->c_flag
&= ~(C_HARDLINK
| C_HASXATTRS
);
1848 to_cp
->c_flag
&= ~(C_HARDLINK
| C_HASXATTRS
);
1851 * Complete the in-memory portion of the copy.
1853 * ExchangeFileIDs swaps the on-disk records involved. We complete the
1854 * operation by swapping the in-memory contents of the two files here.
1855 * We swap the cnode descriptors, which contain name, BSD attributes,
1856 * timestamps, etc, about the file.
1858 * NOTE: We do *NOT* swap the fileforks of the two cnodes. We have
1859 * already swapped the on-disk extent information. As long as we swap the
1860 * IDs, the in-line resident 8 extents that live in the filefork data
1861 * structure will point to the right data for the new file ID if we leave
1864 * As a result, any file descriptor that points to a particular
1865 * vnode (even though it should change names), will continue
1866 * to point to the same content.
1869 /* Copy the "to" -> "from" cnode */
1870 bcopy(&to_cp
->c_desc
, &from_cp
->c_desc
, sizeof(struct cat_desc
));
1872 from_cp
->c_hint
= 0;
1874 * If 'to' was a hardlink, then we copied over its link ID/CNID/(namespace ID)
1875 * when we bcopy'd the descriptor above. However, the cnode attributes
1876 * are not bcopied. As a result, make sure to swap the file IDs of each item.
1878 * Further, other hardlink attributes must be moved along in this swap:
1879 * the linkcount, the linkref, and the firstlink all need to move
1880 * along with the file IDs. See note below regarding the flags and
1881 * what moves vs. what does not.
1884 * linkcount == total # of hardlinks.
1885 * linkref == the indirect inode pointer.
1886 * firstlink == the first hardlink in the chain (written to the raw inode).
1887 * These three are tied to the fileID and must move along with the rest of the data.
1889 from_cp
->c_fileid
= to_cp
->c_attr
.ca_fileid
;
1891 from_cp
->c_itime
= to_cp
->c_itime
;
1892 from_cp
->c_btime
= to_cp
->c_btime
;
1893 from_cp
->c_atime
= to_cp
->c_atime
;
1894 from_cp
->c_ctime
= to_cp
->c_ctime
;
1895 from_cp
->c_gid
= to_cp
->c_gid
;
1896 from_cp
->c_uid
= to_cp
->c_uid
;
1897 from_cp
->c_bsdflags
= to_cp
->c_bsdflags
;
1898 from_cp
->c_mode
= to_cp
->c_mode
;
1899 from_cp
->c_linkcount
= to_cp
->c_linkcount
;
1900 from_cp
->c_attr
.ca_linkref
= to_cp
->c_attr
.ca_linkref
;
1901 from_cp
->c_attr
.ca_firstlink
= to_cp
->c_attr
.ca_firstlink
;
1904 * The cnode flags need to stay with the cnode and not get transferred
1905 * over along with everything else because they describe the content; they are
1906 * not attributes that reflect changes specific to the file ID. In general,
1907 * fields that are tied to the file ID are the ones that will move.
1909 * This reflects the fact that the file may have borrowed blocks, dirty metadata,
1910 * or other extents, which may not yet have been written to the catalog. If
1911 * they were, they would have been transferred above in the ExchangeFileIDs call above...
1913 * The flags that are special are:
1914 * C_HARDLINK, C_HASXATTRS
1916 * These flags move with the item and file ID in the namespace since their
1917 * state is tied to that of the file ID.
1919 * So to transfer the flags, we have to take the following steps
1920 * 1) Store in a localvar whether or not the special bits are set.
1921 * 2) Drop the special bits from the current flags
1922 * 3) swap the special flag bits to their destination
1924 from_cp
->c_flag
|= to_flag_special
;
1925 from_cp
->c_attr
.ca_recflags
= to_cp
->c_attr
.ca_recflags
;
1926 bcopy(to_cp
->c_finderinfo
, from_cp
->c_finderinfo
, 32);
1929 /* Copy the "from" -> "to" cnode */
1930 bcopy(&tempdesc
, &to_cp
->c_desc
, sizeof(struct cat_desc
));
1933 * Pull the file ID from the tempattr we copied above. We can't assume
1934 * it is the same as the CNID.
1936 to_cp
->c_fileid
= tempattr
.ca_fileid
;
1937 to_cp
->c_itime
= tempattr
.ca_itime
;
1938 to_cp
->c_btime
= tempattr
.ca_btime
;
1939 to_cp
->c_atime
= tempattr
.ca_atime
;
1940 to_cp
->c_ctime
= tempattr
.ca_ctime
;
1941 to_cp
->c_gid
= tempattr
.ca_gid
;
1942 to_cp
->c_uid
= tempattr
.ca_uid
;
1943 to_cp
->c_bsdflags
= tempattr
.ca_flags
;
1944 to_cp
->c_mode
= tempattr
.ca_mode
;
1945 to_cp
->c_linkcount
= tempattr
.ca_linkcount
;
1946 to_cp
->c_attr
.ca_linkref
= tempattr
.ca_linkref
;
1947 to_cp
->c_attr
.ca_firstlink
= tempattr
.ca_firstlink
;
1950 * Only OR in the "from" flags into our cnode flags below.
1951 * Leave the rest of the flags alone.
1953 to_cp
->c_flag
|= from_flag_special
;
1955 to_cp
->c_attr
.ca_recflags
= tempattr
.ca_recflags
;
1956 bcopy(tempattr
.ca_finderinfo
, to_cp
->c_finderinfo
, 32);
1959 /* Rehash the cnodes using their new file IDs */
1960 hfs_chash_rehash(hfsmp
, from_cp
, to_cp
);
1963 * When a file moves out of "Cleanup At Startup"
1964 * we can drop its NODUMP status.
1966 if ((from_cp
->c_bsdflags
& UF_NODUMP
) &&
1967 (from_cp
->c_parentcnid
!= to_cp
->c_parentcnid
)) {
1968 from_cp
->c_bsdflags
&= ~UF_NODUMP
;
1969 from_cp
->c_touch_chgtime
= TRUE
;
1971 if ((to_cp
->c_bsdflags
& UF_NODUMP
) &&
1972 (to_cp
->c_parentcnid
!= from_cp
->c_parentcnid
)) {
1973 to_cp
->c_bsdflags
&= ~UF_NODUMP
;
1974 to_cp
->c_touch_chgtime
= TRUE
;
1979 cat_postflight(hfsmp
, &cookie
, vfs_context_proc(ap
->a_context
));
1982 hfs_end_transaction(hfsmp
);
1985 hfs_unlockpair(from_cp
, to_cp
);
1990 hfs_vnop_mmap(struct vnop_mmap_args
*ap
)
1992 struct vnode
*vp
= ap
->a_vp
;
1995 if (VNODE_IS_RSRC(vp
)) {
1996 /* allow pageins of the resource fork */
1998 int compressed
= hfs_file_is_compressed(VTOC(vp
), 1); /* 1 == don't take the cnode lock */
1999 time_t orig_ctime
= VTOC(vp
)->c_ctime
;
2001 if (!compressed
&& (VTOC(vp
)->c_bsdflags
& UF_COMPRESSED
)) {
2002 error
= check_for_dataless_file(vp
, NAMESPACE_HANDLER_READ_OP
);
2008 if (ap
->a_fflags
& PROT_WRITE
) {
2009 check_for_tracked_file(vp
, orig_ctime
, NAMESPACE_HANDLER_WRITE_OP
, NULL
);
2011 /* even though we're manipulating a cnode field here, we're only monotonically increasing
2012 * the generation counter. The vnode can't be recycled (because we hold a FD in order to cause the
2013 * map to happen). So it's safe to do this without holding the cnode lock. The caller's only
2014 * requirement is that the number has been changed.
2016 struct cnode
*cp
= VTOC(vp
);
2017 if (S_ISREG(cp
->c_attr
.ca_mode
) || S_ISLNK(cp
->c_attr
.ca_mode
)) {
2018 hfs_incr_gencount(cp
);
2024 // NOTE: we return ENOTSUP because we want the cluster layer
2025 // to actually do all the real work.
2033 * This is a non-symmetric variant of exchangedata. In this function,
2034 * the contents of the fork in from_vp are moved to the fork
2035 * specified by to_vp.
2037 * The cnodes pointed to by 'from_vp' and 'to_vp' must be locked.
2039 * The vnode pointed to by 'to_vp' *must* be empty prior to invoking this function.
2040 * We impose this restriction because we may not be able to fully delete the entire
2041 * file's contents in a single transaction, particularly if it has a lot of extents.
2042 * In the normal file deletion codepath, the file is screened for two conditions:
2043 * 1) bigger than 400MB, and 2) more than 8 extents. If so, the file is relocated to
2044 * the hidden directory and the deletion is broken up into multiple truncates. We can't
2045 * do that here because both files need to exist in the namespace. The main reason this
2046 * is imposed is that we may have to touch a whole lot of bitmap blocks if there are
2049 * Any data written to 'from_vp' after this call completes is not guaranteed
2053 * vnode from_vp: source file
2054 * vnode to_vp: destination file; must be empty
2057 * EFBIG - Destination file was not empty
2062 int hfs_movedata (struct vnode
*from_vp
, struct vnode
*to_vp
) {
2064 struct cnode
*from_cp
;
2065 struct cnode
*to_cp
;
2066 struct hfsmount
*hfsmp
= NULL
;
2070 int overflow_blocks
;
2074 /* Get the HFS pointers */
2075 from_cp
= VTOC(from_vp
);
2076 to_cp
= VTOC(to_vp
);
2077 hfsmp
= VTOHFS(from_vp
);
2079 /* Verify that neither source/dest file is open-unlinked */
2080 if (from_cp
->c_flag
& (C_DELETED
| C_NOEXISTS
)) {
2085 if (to_cp
->c_flag
& (C_DELETED
| C_NOEXISTS
)) {
2091 * Verify the source file is not in use by anyone besides us.
2093 * This function is typically invoked by a namespace handler
2094 * process responding to a temporarily stalled system call.
2095 * The FD that it is working off of is opened O_EVTONLY, so
2096 * it really has no active usecounts (the kusecount from O_EVTONLY
2097 * is subtracted from the total usecounts).
2099 * As a result, we shouldn't have any active usecounts against
2100 * this vnode when we go to check it below.
2102 if (vnode_isinuse(from_vp
, 0)) {
2107 if (from_cp
->c_rsrc_vp
== from_vp
) {
2112 * We assume that the destination file is already empty.
2113 * Verify that it is.
2116 if (to_cp
->c_rsrcfork
->ff_size
> 0) {
2122 if (to_cp
->c_datafork
->ff_size
> 0) {
2128 /* If the source has the rsrc open, make sure the destination is also the rsrc */
2130 if (to_vp
!= to_cp
->c_rsrc_vp
) {
2136 /* Verify that both forks are data forks */
2137 if (to_vp
!= to_cp
->c_vp
) {
2144 * See if the source file has overflow extents. If it doesn't, we don't
2145 * need to call into MoveData, and the catalog will be enough.
2148 overflow_blocks
= overflow_extents(from_cp
->c_rsrcfork
);
2151 overflow_blocks
= overflow_extents(from_cp
->c_datafork
);
2154 if ((error
= hfs_start_transaction (hfsmp
)) != 0) {
2159 /* Lock the system files: catalog, extents, attributes */
2160 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
| SFL_EXTENTS
| SFL_ATTRIBUTE
, HFS_EXCLUSIVE_LOCK
);
2162 /* Copy over any catalog allocation data into the new spot. */
2164 if ((error
= hfs_move_fork (from_cp
->c_rsrcfork
, from_cp
, to_cp
->c_rsrcfork
, to_cp
))){
2165 hfs_systemfile_unlock(hfsmp
, lockflags
);
2170 if ((error
= hfs_move_fork (from_cp
->c_datafork
, from_cp
, to_cp
->c_datafork
, to_cp
))) {
2171 hfs_systemfile_unlock(hfsmp
, lockflags
);
2177 * Note that because all we're doing is moving the extents around, we can
2178 * probably do this in a single transaction: Each extent record (group of 8)
2179 * is 64 bytes. A extent overflow B-Tree node is typically 4k. This means
2180 * each node can hold roughly ~60 extent records == (480 extents).
2182 * If a file was massively fragmented and had 20k extents, this means we'd
2183 * roughly touch 20k/480 == 41 to 42 nodes, plus the index nodes, for half
2184 * of the operation. (inserting or deleting). So if we're manipulating 80-100
2185 * nodes, this is basically 320k of data to write to the journal in
2188 if (overflow_blocks
!= 0) {
2190 error
= MoveData(hfsmp
, from_cp
->c_cnid
, to_cp
->c_cnid
, 1);
2193 error
= MoveData (hfsmp
, from_cp
->c_cnid
, to_cp
->c_cnid
, 0);
2198 /* Reverse the operation. Copy the fork data back into the source */
2200 hfs_move_fork (to_cp
->c_rsrcfork
, to_cp
, from_cp
->c_rsrcfork
, from_cp
);
2203 hfs_move_fork (to_cp
->c_datafork
, to_cp
, from_cp
->c_datafork
, from_cp
);
2207 struct cat_fork
*src_data
= NULL
;
2208 struct cat_fork
*src_rsrc
= NULL
;
2209 struct cat_fork
*dst_data
= NULL
;
2210 struct cat_fork
*dst_rsrc
= NULL
;
2212 /* Touch the times*/
2213 to_cp
->c_touch_acctime
= TRUE
;
2214 to_cp
->c_touch_chgtime
= TRUE
;
2215 to_cp
->c_touch_modtime
= TRUE
;
2217 from_cp
->c_touch_acctime
= TRUE
;
2218 from_cp
->c_touch_chgtime
= TRUE
;
2219 from_cp
->c_touch_modtime
= TRUE
;
2221 hfs_touchtimes(hfsmp
, to_cp
);
2222 hfs_touchtimes(hfsmp
, from_cp
);
2224 if (from_cp
->c_datafork
) {
2225 src_data
= &from_cp
->c_datafork
->ff_data
;
2227 if (from_cp
->c_rsrcfork
) {
2228 src_rsrc
= &from_cp
->c_rsrcfork
->ff_data
;
2231 if (to_cp
->c_datafork
) {
2232 dst_data
= &to_cp
->c_datafork
->ff_data
;
2234 if (to_cp
->c_rsrcfork
) {
2235 dst_rsrc
= &to_cp
->c_rsrcfork
->ff_data
;
2238 /* Update the catalog nodes */
2239 (void) cat_update(hfsmp
, &from_cp
->c_desc
, &from_cp
->c_attr
,
2240 src_data
, src_rsrc
);
2242 (void) cat_update(hfsmp
, &to_cp
->c_desc
, &to_cp
->c_attr
,
2243 dst_data
, dst_rsrc
);
2246 /* unlock the system files */
2247 hfs_systemfile_unlock(hfsmp
, lockflags
);
2252 hfs_end_transaction(hfsmp
);
2260 * Copy all of the catalog and runtime data in srcfork to dstfork.
2262 * This allows us to maintain the invalid ranges across the movedata operation so
2263 * we don't need to force all of the pending IO right now. In addition, we move all
2264 * non overflow-extent extents into the destination here.
2266 static int hfs_move_fork (struct filefork
*srcfork
, struct cnode
*src_cp
,
2267 struct filefork
*dstfork
, struct cnode
*dst_cp
) {
2268 struct rl_entry
*invalid_range
;
2269 int size
= sizeof(struct HFSPlusExtentDescriptor
);
2270 size
= size
* kHFSPlusExtentDensity
;
2272 /* If the dstfork has any invalid ranges, bail out */
2273 invalid_range
= TAILQ_FIRST(&dstfork
->ff_invalidranges
);
2274 if (invalid_range
!= NULL
) {
2278 if (dstfork
->ff_data
.cf_size
!= 0 || dstfork
->ff_data
.cf_new_size
!= 0) {
2282 /* First copy the invalid ranges */
2283 while ((invalid_range
= TAILQ_FIRST(&srcfork
->ff_invalidranges
))) {
2284 off_t start
= invalid_range
->rl_start
;
2285 off_t end
= invalid_range
->rl_end
;
2287 /* Remove it from the srcfork and add it to dstfork */
2288 rl_remove(start
, end
, &srcfork
->ff_invalidranges
);
2289 rl_add(start
, end
, &dstfork
->ff_invalidranges
);
2293 * Ignore the ff_union. We don't move symlinks or system files.
2294 * Now copy the in-catalog extent information
2296 dstfork
->ff_data
.cf_size
= srcfork
->ff_data
.cf_size
;
2297 dstfork
->ff_data
.cf_new_size
= srcfork
->ff_data
.cf_new_size
;
2298 dstfork
->ff_data
.cf_vblocks
= srcfork
->ff_data
.cf_vblocks
;
2299 dstfork
->ff_data
.cf_blocks
= srcfork
->ff_data
.cf_blocks
;
2301 /* just memcpy the whole array of extents to the new location. */
2302 memcpy (dstfork
->ff_data
.cf_extents
, srcfork
->ff_data
.cf_extents
, size
);
2305 * Copy the cnode attribute data.
2308 src_cp
->c_blocks
-= srcfork
->ff_data
.cf_vblocks
;
2309 src_cp
->c_blocks
-= srcfork
->ff_data
.cf_blocks
;
2311 dst_cp
->c_blocks
+= srcfork
->ff_data
.cf_vblocks
;
2312 dst_cp
->c_blocks
+= srcfork
->ff_data
.cf_blocks
;
2314 /* Now delete the entries in the source fork */
2315 srcfork
->ff_data
.cf_size
= 0;
2316 srcfork
->ff_data
.cf_new_size
= 0;
2317 srcfork
->ff_data
.cf_union
.cfu_bytesread
= 0;
2318 srcfork
->ff_data
.cf_vblocks
= 0;
2319 srcfork
->ff_data
.cf_blocks
= 0;
2321 /* Zero out the old extents */
2322 bzero (srcfork
->ff_data
.cf_extents
, size
);
2328 * cnode must be locked
2331 hfs_fsync(struct vnode
*vp
, int waitfor
, int fullsync
, struct proc
*p
)
2333 struct cnode
*cp
= VTOC(vp
);
2334 struct filefork
*fp
= NULL
;
2336 struct hfsmount
*hfsmp
= VTOHFS(vp
);
2337 struct rl_entry
*invalid_range
;
2339 int waitdata
; /* attributes necessary for data retrieval */
2340 int wait
; /* all other attributes (e.g. atime, etc.) */
2342 int took_trunc_lock
= 0;
2343 int locked_buffers
= 0;
2346 * Applications which only care about data integrity rather than full
2347 * file integrity may opt out of (delay) expensive metadata update
2348 * operations as a performance optimization.
2350 wait
= (waitfor
== MNT_WAIT
);
2351 waitdata
= (waitfor
== MNT_DWAIT
) | wait
;
2352 if (always_do_fullfsync
)
2355 /* HFS directories don't have any data blocks. */
2356 if (vnode_isdir(vp
))
2361 * For system files flush the B-tree header and
2362 * for regular files write out any clusters
2364 if (vnode_issystem(vp
)) {
2365 if (VTOF(vp
)->fcbBTCBPtr
!= NULL
) {
2367 if (hfsmp
->jnl
== NULL
) {
2368 BTFlushPath(VTOF(vp
));
2371 } else if (UBCINFOEXISTS(vp
)) {
2373 hfs_lock_truncate(cp
, HFS_SHARED_LOCK
, HFS_LOCK_DEFAULT
);
2374 took_trunc_lock
= 1;
2376 if (fp
->ff_unallocblocks
!= 0) {
2377 hfs_unlock_truncate(cp
, HFS_LOCK_DEFAULT
);
2379 hfs_lock_truncate(cp
, HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
);
2381 /* Don't hold cnode lock when calling into cluster layer. */
2382 (void) cluster_push(vp
, waitdata
? IO_SYNC
: 0);
2384 hfs_lock(cp
, HFS_EXCLUSIVE_LOCK
, HFS_LOCK_ALLOW_NOEXISTS
);
2387 * When MNT_WAIT is requested and the zero fill timeout
2388 * has expired then we must explicitly zero out any areas
2389 * that are currently marked invalid (holes).
2391 * Files with NODUMP can bypass zero filling here.
2393 if (fp
&& (((cp
->c_flag
& C_ALWAYS_ZEROFILL
) && !TAILQ_EMPTY(&fp
->ff_invalidranges
)) ||
2394 ((wait
|| (cp
->c_flag
& C_ZFWANTSYNC
)) &&
2395 ((cp
->c_bsdflags
& UF_NODUMP
) == 0) &&
2396 UBCINFOEXISTS(vp
) && (vnode_issystem(vp
) ==0) &&
2397 cp
->c_zftimeout
!= 0))) {
2400 if ((cp
->c_flag
& C_ALWAYS_ZEROFILL
) == 0 && !fullsync
&& tv
.tv_sec
< (long)cp
->c_zftimeout
) {
2401 /* Remember that a force sync was requested. */
2402 cp
->c_flag
|= C_ZFWANTSYNC
;
2405 if (!TAILQ_EMPTY(&fp
->ff_invalidranges
)) {
2406 if (!took_trunc_lock
|| (cp
->c_truncatelockowner
== HFS_SHARED_OWNER
)) {
2408 if (took_trunc_lock
) {
2409 hfs_unlock_truncate(cp
, HFS_LOCK_DEFAULT
);
2411 hfs_lock_truncate(cp
, HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
);
2412 hfs_lock(cp
, HFS_EXCLUSIVE_LOCK
, HFS_LOCK_ALLOW_NOEXISTS
);
2413 took_trunc_lock
= 1;
2415 while ((invalid_range
= TAILQ_FIRST(&fp
->ff_invalidranges
))) {
2416 off_t start
= invalid_range
->rl_start
;
2417 off_t end
= invalid_range
->rl_end
;
2419 /* The range about to be written must be validated
2420 * first, so that VNOP_BLOCKMAP() will return the
2421 * appropriate mapping for the cluster code:
2423 rl_remove(start
, end
, &fp
->ff_invalidranges
);
2425 /* Don't hold cnode lock when calling into cluster layer. */
2427 (void) cluster_write(vp
, (struct uio
*) 0,
2428 fp
->ff_size
, end
+ 1, start
, (off_t
)0,
2429 IO_HEADZEROFILL
| IO_NOZERODIRTY
| IO_NOCACHE
);
2430 hfs_lock(cp
, HFS_EXCLUSIVE_LOCK
, HFS_LOCK_ALLOW_NOEXISTS
);
2431 cp
->c_flag
|= C_MODIFIED
;
2434 (void) cluster_push(vp
, waitdata
? IO_SYNC
: 0);
2435 hfs_lock(cp
, HFS_EXCLUSIVE_LOCK
, HFS_LOCK_ALLOW_NOEXISTS
);
2437 cp
->c_flag
&= ~C_ZFWANTSYNC
;
2438 cp
->c_zftimeout
= 0;
2441 if (took_trunc_lock
) {
2442 hfs_unlock_truncate(cp
, HFS_LOCK_DEFAULT
);
2443 took_trunc_lock
= 0;
2446 * if we have a journal and if journal_active() returns != 0 then the
2447 * we shouldn't do anything to a locked block (because it is part
2448 * of a transaction). otherwise we'll just go through the normal
2449 * code path and flush the buffer. note journal_active() can return
2450 * -1 if the journal is invalid -- however we still need to skip any
2451 * locked blocks as they get cleaned up when we finish the transaction
2452 * or close the journal.
2454 // if (hfsmp->jnl && journal_active(hfsmp->jnl) >= 0)
2456 lockflag
= BUF_SKIP_LOCKED
;
2461 * Flush all dirty buffers associated with a vnode.
2462 * Record how many of them were dirty AND locked (if necessary).
2464 locked_buffers
= buf_flushdirtyblks_skipinfo(vp
, waitdata
, lockflag
, "hfs_fsync");
2465 if ((lockflag
& BUF_SKIP_LOCKED
) && (locked_buffers
) && (vnode_vtype(vp
) == VLNK
)) {
2467 * If there are dirty symlink buffers, then we may need to take action
2468 * to prevent issues later on if we are journaled. If we're fsyncing a
2469 * symlink vnode then we are in one of three cases:
2471 * 1) automatic sync has fired. In this case, we don't want the behavior to change.
2473 * 2) Someone has opened the FD for the symlink (not what it points to)
2474 * and has issued an fsync against it. This should be rare, and we don't
2475 * want the behavior to change.
2477 * 3) We are being called by a vclean which is trying to reclaim this
2478 * symlink vnode. If this is the case, then allowing this fsync to
2479 * proceed WITHOUT flushing the journal could result in the vclean
2480 * invalidating the buffer's blocks before the journal transaction is
2481 * written to disk. To prevent this, we force a journal flush
2482 * if the vnode is in the middle of a recycle (VL_TERMINATE or VL_DEAD is set).
2484 if (vnode_isrecycled(vp
)) {
2490 if (vnode_isreg(vp
) && vnode_issystem(vp
)) {
2491 if (VTOF(vp
)->fcbBTCBPtr
!= NULL
) {
2493 BTSetLastSync(VTOF(vp
), tv
.tv_sec
);
2495 cp
->c_touch_acctime
= FALSE
;
2496 cp
->c_touch_chgtime
= FALSE
;
2497 cp
->c_touch_modtime
= FALSE
;
2498 } else if ( !(vp
->v_flag
& VSWAP
) ) /* User file */ {
2499 retval
= hfs_update(vp
, wait
);
2502 * When MNT_WAIT is requested push out the catalog record for
2503 * this file. If they asked for a full fsync, we can skip this
2504 * because the journal_flush or hfs_metasync_all will push out
2505 * all of the metadata changes.
2507 if ((retval
== 0) && wait
&& !fullsync
&& cp
->c_hint
&&
2508 !ISSET(cp
->c_flag
, C_DELETED
| C_NOEXISTS
)) {
2509 hfs_metasync(VTOHFS(vp
), (daddr64_t
)cp
->c_hint
, p
);
2513 * If this was a full fsync, make sure all metadata
2514 * changes get to stable storage.
2518 hfs_journal_flush(hfsmp
, FALSE
);
2520 if (journal_uses_fua(hfsmp
->jnl
)) {
2522 * the journal_flush did NOT issue a sync track cache command,
2523 * and the fullsync indicates we are supposed to flush all cached
2524 * data to the media, so issue the sync track cache command
2527 VNOP_IOCTL(hfsmp
->hfs_devvp
, DKIOCSYNCHRONIZECACHE
, NULL
, FWRITE
, NULL
);
2530 retval
= hfs_metasync_all(hfsmp
);
2531 /* XXX need to pass context! */
2532 VNOP_IOCTL(hfsmp
->hfs_devvp
, DKIOCSYNCHRONIZECACHE
, NULL
, FWRITE
, NULL
);
2541 /* Sync an hfs catalog b-tree node */
2543 hfs_metasync(struct hfsmount
*hfsmp
, daddr64_t node
, __unused
struct proc
*p
)
2549 vp
= HFSTOVCB(hfsmp
)->catalogRefNum
;
2551 // XXXdbg - don't need to do this on a journaled volume
2556 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_EXCLUSIVE_LOCK
);
2558 * Look for a matching node that has been delayed
2559 * but is not part of a set (B_LOCKED).
2561 * BLK_ONLYVALID causes buf_getblk to return a
2562 * buf_t for the daddr64_t specified only if it's
2563 * currently resident in the cache... the size
2564 * parameter to buf_getblk is ignored when this flag
2567 bp
= buf_getblk(vp
, node
, 0, 0, 0, BLK_META
| BLK_ONLYVALID
);
2570 if ((buf_flags(bp
) & (B_LOCKED
| B_DELWRI
)) == B_DELWRI
)
2571 (void) VNOP_BWRITE(bp
);
2576 hfs_systemfile_unlock(hfsmp
, lockflags
);
2583 * Sync all hfs B-trees. Use this instead of journal_flush for a volume
2584 * without a journal. Note that the volume bitmap does not get written;
2585 * we rely on fsck_hfs to fix that up (which it can do without any loss
2589 hfs_metasync_all(struct hfsmount
*hfsmp
)
2593 /* Lock all of the B-trees so we get a mutually consistent state */
2594 lockflags
= hfs_systemfile_lock(hfsmp
,
2595 SFL_CATALOG
|SFL_EXTENTS
|SFL_ATTRIBUTE
, HFS_EXCLUSIVE_LOCK
);
2597 /* Sync each of the B-trees */
2598 if (hfsmp
->hfs_catalog_vp
)
2599 hfs_btsync(hfsmp
->hfs_catalog_vp
, 0);
2600 if (hfsmp
->hfs_extents_vp
)
2601 hfs_btsync(hfsmp
->hfs_extents_vp
, 0);
2602 if (hfsmp
->hfs_attribute_vp
)
2603 hfs_btsync(hfsmp
->hfs_attribute_vp
, 0);
2605 /* Wait for all of the writes to complete */
2606 if (hfsmp
->hfs_catalog_vp
)
2607 vnode_waitforwrites(hfsmp
->hfs_catalog_vp
, 0, 0, 0, "hfs_metasync_all");
2608 if (hfsmp
->hfs_extents_vp
)
2609 vnode_waitforwrites(hfsmp
->hfs_extents_vp
, 0, 0, 0, "hfs_metasync_all");
2610 if (hfsmp
->hfs_attribute_vp
)
2611 vnode_waitforwrites(hfsmp
->hfs_attribute_vp
, 0, 0, 0, "hfs_metasync_all");
2613 hfs_systemfile_unlock(hfsmp
, lockflags
);
2621 hfs_btsync_callback(struct buf
*bp
, __unused
void *dummy
)
2623 buf_clearflags(bp
, B_LOCKED
);
2624 (void) buf_bawrite(bp
);
2626 return(BUF_CLAIMED
);
2631 hfs_btsync(struct vnode
*vp
, int sync_transaction
)
2633 struct cnode
*cp
= VTOC(vp
);
2637 if (sync_transaction
)
2638 flags
|= BUF_SKIP_NONLOCKED
;
2640 * Flush all dirty buffers associated with b-tree.
2642 buf_iterate(vp
, hfs_btsync_callback
, flags
, 0);
2645 if (vnode_issystem(vp
) && (VTOF(vp
)->fcbBTCBPtr
!= NULL
))
2646 (void) BTSetLastSync(VTOF(vp
), tv
.tv_sec
);
2647 cp
->c_touch_acctime
= FALSE
;
2648 cp
->c_touch_chgtime
= FALSE
;
2649 cp
->c_touch_modtime
= FALSE
;
2655 * Remove a directory.
2659 struct vnop_rmdir_args
/* {
2660 struct vnode *a_dvp;
2662 struct componentname *a_cnp;
2663 vfs_context_t a_context;
2666 struct vnode
*dvp
= ap
->a_dvp
;
2667 struct vnode
*vp
= ap
->a_vp
;
2668 struct cnode
*dcp
= VTOC(dvp
);
2669 struct cnode
*cp
= VTOC(vp
);
2673 orig_ctime
= VTOC(vp
)->c_ctime
;
2675 if (!S_ISDIR(cp
->c_mode
)) {
2682 check_for_tracked_file(vp
, orig_ctime
, NAMESPACE_HANDLER_DELETE_OP
, NULL
);
2685 if ((error
= hfs_lockpair(dcp
, cp
, HFS_EXCLUSIVE_LOCK
))) {
2689 /* Check for a race with rmdir on the parent directory */
2690 if (dcp
->c_flag
& (C_DELETED
| C_NOEXISTS
)) {
2691 hfs_unlockpair (dcp
, cp
);
2694 error
= hfs_removedir(dvp
, vp
, ap
->a_cnp
, 0, 0);
2696 hfs_unlockpair(dcp
, cp
);
2702 * Remove a directory
2704 * Both dvp and vp cnodes are locked
2707 hfs_removedir(struct vnode
*dvp
, struct vnode
*vp
, struct componentname
*cnp
,
2708 int skip_reserve
, int only_unlink
)
2712 struct hfsmount
* hfsmp
;
2713 struct cat_desc desc
;
2715 int error
= 0, started_tr
= 0;
2722 return (EINVAL
); /* cannot remove "." */
2724 if (cp
->c_flag
& (C_NOEXISTS
| C_DELETED
)) {
2727 if (cp
->c_entries
!= 0) {
2732 * If the directory is open or in use (e.g. opendir() or current working
2733 * directory for some process); wait for inactive/reclaim to actually
2734 * remove cnode from the catalog. Both inactive and reclaim codepaths are capable
2735 * of removing open-unlinked directories from the catalog, as well as getting rid
2736 * of EAs still on the element. So change only_unlink to true, so that it will get
2739 * Otherwise, we can get into a weird old mess where the directory has C_DELETED,
2740 * but it really means C_NOEXISTS because the item was actually removed from the
2741 * catalog. Then when we try to remove the entry from the catalog later on, it won't
2742 * really be there anymore.
2744 if (vnode_isinuse(vp
, 0)) {
2748 /* Deal with directory hardlinks */
2749 if (cp
->c_flag
& C_HARDLINK
) {
2751 * Note that if we have a directory which was a hardlink at any point,
2752 * its actual directory data is stored in the directory inode in the hidden
2753 * directory rather than the leaf element(s) present in the namespace.
2755 * If there are still other hardlinks to this directory,
2756 * then we'll just eliminate this particular link and the vnode will still exist.
2757 * If this is the last link to an empty directory, then we'll open-unlink the
2758 * directory and it will be only tagged with C_DELETED (as opposed to C_NOEXISTS).
2760 * We could also return EBUSY here.
2763 return hfs_unlink(hfsmp
, dvp
, vp
, cnp
, skip_reserve
);
2767 * In a few cases, we may want to allow the directory to persist in an
2768 * open-unlinked state. If the directory is being open-unlinked (still has usecount
2769 * references), or if it has EAs, or if it was being deleted as part of a rename,
2770 * then we go ahead and move it to the hidden directory.
2772 * If the directory is being open-unlinked, then we want to keep the catalog entry
2773 * alive so that future EA calls and fchmod/fstat etc. do not cause issues later.
2775 * If the directory had EAs, then we want to use the open-unlink trick so that the
2776 * EA removal is not done in one giant transaction. Otherwise, it could cause a panic
2777 * due to overflowing the journal.
2779 * Finally, if it was deleted as part of a rename, we move it to the hidden directory
2780 * in order to maintain rename atomicity.
2782 * Note that the allow_dirs argument to hfs_removefile specifies that it is
2783 * supposed to handle directories for this case.
2786 if (((hfsmp
->hfs_attribute_vp
!= NULL
) &&
2787 ((cp
->c_attr
.ca_recflags
& kHFSHasAttributesMask
) != 0)) ||
2788 (only_unlink
!= 0)) {
2790 int ret
= hfs_removefile(dvp
, vp
, cnp
, 0, 0, 1, NULL
, only_unlink
);
2792 * Even though hfs_vnop_rename calls vnode_recycle for us on tvp we call
2793 * it here just in case we were invoked by rmdir() on a directory that had
2794 * EAs. To ensure that we start reclaiming the space as soon as possible,
2795 * we call vnode_recycle on the directory.
2803 dcp
->c_flag
|= C_DIR_MODIFICATION
;
2806 if (hfsmp
->hfs_flags
& HFS_QUOTAS
)
2807 (void)hfs_getinoquota(cp
);
2809 if ((error
= hfs_start_transaction(hfsmp
)) != 0) {
2815 * Verify the directory is empty (and valid).
2816 * (Rmdir ".." won't be valid since
2817 * ".." will contain a reference to
2818 * the current directory and thus be
2821 if ((dcp
->c_bsdflags
& APPEND
) || (cp
->c_bsdflags
& (IMMUTABLE
| APPEND
))) {
2826 /* Remove the entry from the namei cache: */
2830 * Protect against a race with rename by using the component
2831 * name passed in and parent id from dvp (instead of using
2832 * the cp->c_desc which may have changed).
2834 desc
.cd_nameptr
= (const u_int8_t
*)cnp
->cn_nameptr
;
2835 desc
.cd_namelen
= cnp
->cn_namelen
;
2836 desc
.cd_parentcnid
= dcp
->c_fileid
;
2837 desc
.cd_cnid
= cp
->c_cnid
;
2838 desc
.cd_flags
= CD_ISDIR
;
2839 desc
.cd_encoding
= cp
->c_encoding
;
2842 if (!hfs_valid_cnode(hfsmp
, dvp
, cnp
, cp
->c_fileid
, NULL
, &error
)) {
2847 /* Remove entry from catalog */
2848 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
| SFL_ATTRIBUTE
| SFL_BITMAP
, HFS_EXCLUSIVE_LOCK
);
2850 if (!skip_reserve
) {
2852 * Reserve some space in the Catalog file.
2854 if ((error
= cat_preflight(hfsmp
, CAT_DELETE
, NULL
, 0))) {
2855 hfs_systemfile_unlock(hfsmp
, lockflags
);
2860 error
= cat_delete(hfsmp
, &desc
, &cp
->c_attr
);
2862 /* The parent lost a child */
2863 if (dcp
->c_entries
> 0)
2865 DEC_FOLDERCOUNT(hfsmp
, dcp
->c_attr
);
2866 dcp
->c_dirchangecnt
++;
2867 dcp
->c_touch_chgtime
= TRUE
;
2868 dcp
->c_touch_modtime
= TRUE
;
2869 hfs_touchtimes(hfsmp
, cp
);
2870 (void) cat_update(hfsmp
, &dcp
->c_desc
, &dcp
->c_attr
, NULL
, NULL
);
2871 cp
->c_flag
&= ~(C_MODIFIED
| C_FORCEUPDATE
);
2874 hfs_systemfile_unlock(hfsmp
, lockflags
);
2880 if (hfsmp
->hfs_flags
& HFS_QUOTAS
)
2881 (void)hfs_chkiq(cp
, -1, NOCRED
, 0);
2884 hfs_volupdate(hfsmp
, VOL_RMDIR
, (dcp
->c_cnid
== kHFSRootFolderID
));
2886 /* Mark C_NOEXISTS since the catalog entry is now gone */
2887 cp
->c_flag
|= C_NOEXISTS
;
2889 dcp
->c_flag
&= ~C_DIR_MODIFICATION
;
2890 wakeup((caddr_t
)&dcp
->c_flag
);
2893 hfs_end_transaction(hfsmp
);
2901 * Remove a file or link.
2905 struct vnop_remove_args
/* {
2906 struct vnode *a_dvp;
2908 struct componentname *a_cnp;
2910 vfs_context_t a_context;
2913 struct vnode
*dvp
= ap
->a_dvp
;
2914 struct vnode
*vp
= ap
->a_vp
;
2915 struct cnode
*dcp
= VTOC(dvp
);
2917 struct vnode
*rvp
= NULL
;
2918 int error
=0, recycle_rsrc
=0;
2919 int recycle_vnode
= 0;
2920 uint32_t rsrc_vid
= 0;
2927 orig_ctime
= VTOC(vp
)->c_ctime
;
2928 if (!vnode_isnamedstream(vp
) && ((ap
->a_flags
& VNODE_REMOVE_SKIP_NAMESPACE_EVENT
) == 0)) {
2929 error
= check_for_tracked_file(vp
, orig_ctime
, NAMESPACE_HANDLER_DELETE_OP
, NULL
);
2931 // XXXdbg - decide on a policy for handling namespace handler failures!
2932 // for now we just let them proceed.
2941 hfs_lock_truncate(cp
, HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
);
2943 if ((error
= hfs_lockpair(dcp
, cp
, HFS_EXCLUSIVE_LOCK
))) {
2944 hfs_unlock_truncate(cp
, HFS_LOCK_DEFAULT
);
2952 * Lazily respond to determining if there is a valid resource fork
2953 * vnode attached to 'cp' if it is a regular file or symlink.
2954 * If the vnode does not exist, then we may proceed without having to
2957 * If, however, it does exist, then we need to acquire an iocount on the
2958 * vnode after acquiring its vid. This ensures that if we have to do I/O
2959 * against it, it can't get recycled from underneath us in the middle
2962 * Note: this function may be invoked for directory hardlinks, so just skip these
2963 * steps if 'vp' is a directory.
2966 if ((vp
->v_type
== VLNK
) || (vp
->v_type
== VREG
)) {
2967 if ((cp
->c_rsrc_vp
) && (rvp
== NULL
)) {
2968 /* We need to acquire the rsrc vnode */
2969 rvp
= cp
->c_rsrc_vp
;
2970 rsrc_vid
= vnode_vid (rvp
);
2972 /* Unlock everything to acquire iocount on the rsrc vnode */
2973 hfs_unlock_truncate (cp
, HFS_LOCK_DEFAULT
);
2974 hfs_unlockpair (dcp
, cp
);
2975 /* Use the vid to maintain identity on rvp */
2976 if (vnode_getwithvid(rvp
, rsrc_vid
)) {
2978 * If this fails, then it was recycled or
2979 * reclaimed in the interim. Reset fields and
2990 * Check to see if we raced rmdir for the parent directory
2991 * hfs_removefile already checks for a race on vp/cp
2993 if (dcp
->c_flag
& (C_DELETED
| C_NOEXISTS
)) {
2998 error
= hfs_removefile(dvp
, vp
, ap
->a_cnp
, ap
->a_flags
, 0, 0, NULL
, 0);
3001 * If the remove succeeded in deleting the file, then we may need to mark
3002 * the resource fork for recycle so that it is reclaimed as quickly
3003 * as possible. If it were not recycled quickly, then this resource fork
3004 * vnode could keep a v_parent reference on the data fork, which prevents it
3005 * from going through reclaim (by giving it extra usecounts), except in the force-
3008 * However, a caveat: we need to continue to supply resource fork
3009 * access to open-unlinked files even if the resource fork is not open. This is
3010 * a requirement for the compressed files work. Luckily, hfs_vgetrsrc will handle
3011 * this already if the data fork has been re-parented to the hidden directory.
3013 * As a result, all we really need to do here is mark the resource fork vnode
3014 * for recycle. If it goes out of core, it can be brought in again if needed.
3015 * If the cnode was instead marked C_NOEXISTS, then there wouldn't be any
3023 * If the target was actually removed from the catalog schedule it for
3024 * full reclamation/inactivation. We hold an iocount on it so it should just
3025 * get marked with MARKTERM
3027 if (cp
->c_flag
& C_NOEXISTS
) {
3034 * Drop the truncate lock before unlocking the cnode
3035 * (which can potentially perform a vnode_put and
3036 * recycle the vnode which in turn might require the
3040 hfs_unlock_truncate(cp
, HFS_LOCK_DEFAULT
);
3041 hfs_unlockpair(dcp
, cp
);
3044 /* inactive or reclaim on rvp will clean up the blocks from the rsrc fork */
3047 if (recycle_vnode
) {
3052 /* drop iocount on rsrc fork, was obtained at beginning of fxn */
3061 hfs_removefile_callback(struct buf
*bp
, void *hfsmp
) {
3063 if ( !(buf_flags(bp
) & B_META
))
3064 panic("hfs: symlink bp @ %p is not marked meta-data!\n", bp
);
3066 * it's part of the current transaction, kill it.
3068 journal_kill_block(((struct hfsmount
*)hfsmp
)->jnl
, bp
);
3070 return (BUF_CLAIMED
);
3076 * Similar to hfs_vnop_remove except there are additional options.
3077 * This function may be used to remove directories if they have
3078 * lots of EA's -- note the 'allow_dirs' argument.
3080 * This function is able to delete blocks & fork data for the resource
3081 * fork even if it does not exist in core (and have a backing vnode).
3082 * It should infer the correct behavior based on the number of blocks
3083 * in the cnode and whether or not the resource fork pointer exists or
3084 * not. As a result, one only need pass in the 'vp' corresponding to the
3085 * data fork of this file (or main vnode in the case of a directory).
3086 * Passing in a resource fork will result in an error.
3088 * Because we do not create any vnodes in this function, we are not at
3089 * risk of deadlocking against ourselves by double-locking.
3091 * Requires cnode and truncate locks to be held.
3094 hfs_removefile(struct vnode
*dvp
, struct vnode
*vp
, struct componentname
*cnp
,
3095 int flags
, int skip_reserve
, int allow_dirs
,
3096 __unused
struct vnode
*rvp
, int only_unlink
)
3100 struct vnode
*rsrc_vp
= NULL
;
3101 struct hfsmount
*hfsmp
;
3102 struct cat_desc desc
;
3104 int dataforkbusy
= 0;
3105 int rsrcforkbusy
= 0;
3109 int isbigfile
= 0, defer_remove
=0, isdir
=0;
3116 /* Check if we lost a race post lookup. */
3117 if (cp
->c_flag
& (C_NOEXISTS
| C_DELETED
)) {
3121 if (!hfs_valid_cnode(hfsmp
, dvp
, cnp
, cp
->c_fileid
, NULL
, &error
)) {
3125 /* Make sure a remove is permitted */
3126 if (VNODE_IS_RSRC(vp
)) {
3131 * We know it's a data fork.
3132 * Probe the cnode to see if we have a valid resource fork
3135 rsrc_vp
= cp
->c_rsrc_vp
;
3138 /* Don't allow deleting the journal or journal_info_block. */
3139 if (hfs_is_journal_file(hfsmp
, cp
)) {
3144 * If removing a symlink, then we need to ensure that the
3145 * data blocks for the symlink are not still in-flight or pending.
3146 * If so, we will unlink the symlink here, making its blocks
3147 * available for re-allocation by a subsequent transaction. That is OK, but
3148 * then the I/O for the data blocks could then go out before the journal
3149 * transaction that created it was flushed, leading to I/O ordering issues.
3151 if (vp
->v_type
== VLNK
) {
3153 * This will block if the asynchronous journal flush is in progress.
3154 * If this symlink is not being renamed over and doesn't have any open FDs,
3155 * then we'll remove it from the journal's bufs below in kill_block.
3157 buf_wait_for_shadow_io (vp
, 0);
3161 * Hard links require special handling.
3163 if (cp
->c_flag
& C_HARDLINK
) {
3164 if ((flags
& VNODE_REMOVE_NODELETEBUSY
) && vnode_isinuse(vp
, 0)) {
3167 /* A directory hard link with a link count of one is
3168 * treated as a regular directory. Therefore it should
3169 * only be removed using rmdir().
3171 if ((vnode_isdir(vp
) == 1) && (cp
->c_linkcount
== 1) &&
3172 (allow_dirs
== 0)) {
3175 return hfs_unlink(hfsmp
, dvp
, vp
, cnp
, skip_reserve
);
3179 /* Directories should call hfs_rmdir! (unless they have a lot of attributes) */
3180 if (vnode_isdir(vp
)) {
3181 if (allow_dirs
== 0)
3182 return (EPERM
); /* POSIX */
3185 /* Sanity check the parent ids. */
3186 if ((cp
->c_parentcnid
!= hfsmp
->hfs_private_desc
[FILE_HARDLINKS
].cd_cnid
) &&
3187 (cp
->c_parentcnid
!= dcp
->c_fileid
)) {
3191 dcp
->c_flag
|= C_DIR_MODIFICATION
;
3193 // this guy is going away so mark him as such
3194 cp
->c_flag
|= C_DELETED
;
3197 /* Remove our entry from the namei cache. */
3201 * If the caller was operating on a file (as opposed to a
3202 * directory with EAs), then we need to figure out
3203 * whether or not it has a valid resource fork vnode.
3205 * If there was a valid resource fork vnode, then we need
3206 * to use hfs_truncate to eliminate its data. If there is
3207 * no vnode, then we hold the cnode lock which would
3208 * prevent it from being created. As a result,
3209 * we can use the data deletion functions which do not
3210 * require that a cnode/vnode pair exist.
3213 /* Check if this file is being used. */
3215 dataforkbusy
= vnode_isinuse(vp
, 0);
3217 * At this point, we know that 'vp' points to the
3218 * a data fork because we checked it up front. And if
3219 * there is no rsrc fork, rsrc_vp will be NULL.
3221 if (rsrc_vp
&& (cp
->c_blocks
- VTOF(vp
)->ff_blocks
)) {
3222 rsrcforkbusy
= vnode_isinuse(rsrc_vp
, 0);
3226 /* Check if we have to break the deletion into multiple pieces. */
3228 isbigfile
= ((cp
->c_datafork
->ff_size
>= HFS_BIGFILE_SIZE
) && overflow_extents(VTOF(vp
)));
3231 /* Check if the file has xattrs. If it does we'll have to delete them in
3232 individual transactions in case there are too many */
3233 if ((hfsmp
->hfs_attribute_vp
!= NULL
) &&
3234 (cp
->c_attr
.ca_recflags
& kHFSHasAttributesMask
) != 0) {
3238 /* If we are explicitly told to only unlink item and move to hidden dir, then do it */
3244 * Carbon semantics prohibit deleting busy files.
3245 * (enforced when VNODE_REMOVE_NODELETEBUSY is requested)
3247 if (dataforkbusy
|| rsrcforkbusy
) {
3248 if ((flags
& VNODE_REMOVE_NODELETEBUSY
) ||
3249 (hfsmp
->hfs_private_desc
[FILE_HARDLINKS
].cd_cnid
== 0)) {
3256 if (hfsmp
->hfs_flags
& HFS_QUOTAS
)
3257 (void)hfs_getinoquota(cp
);
3261 * Do a ubc_setsize to indicate we need to wipe contents if:
3262 * 1) item is a regular file.
3263 * 2) Neither fork is busy AND we are not told to unlink this.
3265 * We need to check for the defer_remove since it can be set without
3266 * having a busy data or rsrc fork
3268 if (isdir
== 0 && (!dataforkbusy
|| !rsrcforkbusy
) && (defer_remove
== 0)) {
3270 * A ubc_setsize can cause a pagein so defer it
3271 * until after the cnode lock is dropped. The
3272 * cnode lock cannot be dropped/reacquired here
3273 * since we might already hold the journal lock.
3275 if (!dataforkbusy
&& cp
->c_datafork
->ff_blocks
&& !isbigfile
) {
3276 cp
->c_flag
|= C_NEED_DATA_SETSIZE
;
3278 if (!rsrcforkbusy
&& rsrc_vp
) {
3279 cp
->c_flag
|= C_NEED_RSRC_SETSIZE
;
3283 if ((error
= hfs_start_transaction(hfsmp
)) != 0) {
3288 // XXXdbg - if we're journaled, kill any dirty symlink buffers
3289 if (hfsmp
->jnl
&& vnode_islnk(vp
) && (defer_remove
== 0)) {
3290 buf_iterate(vp
, hfs_removefile_callback
, BUF_SKIP_NONLOCKED
, (void *)hfsmp
);
3294 * Prepare to truncate any non-busy forks. Busy forks will
3295 * get truncated when their vnode goes inactive.
3296 * Note that we will only enter this region if we
3297 * can avoid creating an open-unlinked file. If
3298 * either region is busy, we will have to create an open
3301 * Since we are deleting the file, we need to stagger the runtime
3302 * modifications to do things in such a way that a crash won't
3303 * result in us getting overlapped extents or any other
3304 * bad inconsistencies. As such, we call prepare_release_storage
3305 * which updates the UBC, updates quota information, and releases
3306 * any loaned blocks that belong to this file. No actual
3307 * truncation or bitmap manipulation is done until *AFTER*
3308 * the catalog record is removed.
3310 if (isdir
== 0 && (!dataforkbusy
&& !rsrcforkbusy
) && (only_unlink
== 0)) {
3312 if (!dataforkbusy
&& !isbigfile
&& cp
->c_datafork
->ff_blocks
!= 0) {
3314 error
= hfs_prepare_release_storage (hfsmp
, vp
);
3322 * If the resource fork vnode does not exist, we can skip this step.
3324 if (!rsrcforkbusy
&& rsrc_vp
) {
3325 error
= hfs_prepare_release_storage (hfsmp
, rsrc_vp
);
3334 * Protect against a race with rename by using the component
3335 * name passed in and parent id from dvp (instead of using
3336 * the cp->c_desc which may have changed). Also, be aware that
3337 * because we allow directories to be passed in, we need to special case
3338 * this temporary descriptor in case we were handed a directory.
3341 desc
.cd_flags
= CD_ISDIR
;
3346 desc
.cd_encoding
= cp
->c_desc
.cd_encoding
;
3347 desc
.cd_nameptr
= (const u_int8_t
*)cnp
->cn_nameptr
;
3348 desc
.cd_namelen
= cnp
->cn_namelen
;
3349 desc
.cd_parentcnid
= dcp
->c_fileid
;
3350 desc
.cd_hint
= cp
->c_desc
.cd_hint
;
3351 desc
.cd_cnid
= cp
->c_cnid
;
3355 * There are two cases to consider:
3356 * 1. File/Dir is busy/big/defer_remove ==> move/rename the file/dir
3357 * 2. File is not in use ==> remove the file
3359 * We can get a directory in case 1 because it may have had lots of attributes,
3360 * which need to get removed here.
3362 if (dataforkbusy
|| rsrcforkbusy
|| isbigfile
|| defer_remove
) {
3364 struct cat_desc to_desc
;
3365 struct cat_desc todir_desc
;
3368 * Orphan this file or directory (move to hidden directory).
3369 * Again, we need to take care that we treat directories as directories,
3370 * and files as files. Because directories with attributes can be passed in
3371 * check to make sure that we have a directory or a file before filling in the
3372 * temporary descriptor's flags. We keep orphaned directories AND files in
3373 * the FILE_HARDLINKS private directory since we're generalizing over all
3374 * orphaned filesystem objects.
3376 bzero(&todir_desc
, sizeof(todir_desc
));
3377 todir_desc
.cd_parentcnid
= 2;
3379 MAKE_DELETED_NAME(delname
, sizeof(delname
), cp
->c_fileid
);
3380 bzero(&to_desc
, sizeof(to_desc
));
3381 to_desc
.cd_nameptr
= (const u_int8_t
*)delname
;
3382 to_desc
.cd_namelen
= strlen(delname
);
3383 to_desc
.cd_parentcnid
= hfsmp
->hfs_private_desc
[FILE_HARDLINKS
].cd_cnid
;
3385 to_desc
.cd_flags
= CD_ISDIR
;
3388 to_desc
.cd_flags
= 0;
3390 to_desc
.cd_cnid
= cp
->c_cnid
;
3392 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_EXCLUSIVE_LOCK
);
3393 if (!skip_reserve
) {
3394 if ((error
= cat_preflight(hfsmp
, CAT_RENAME
, NULL
, 0))) {
3395 hfs_systemfile_unlock(hfsmp
, lockflags
);
3400 error
= cat_rename(hfsmp
, &desc
, &todir_desc
,
3401 &to_desc
, (struct cat_desc
*)NULL
);
3404 hfsmp
->hfs_private_attr
[FILE_HARDLINKS
].ca_entries
++;
3406 INC_FOLDERCOUNT(hfsmp
, hfsmp
->hfs_private_attr
[FILE_HARDLINKS
]);
3408 (void) cat_update(hfsmp
, &hfsmp
->hfs_private_desc
[FILE_HARDLINKS
],
3409 &hfsmp
->hfs_private_attr
[FILE_HARDLINKS
], NULL
, NULL
);
3411 /* Update the parent directory */
3412 if (dcp
->c_entries
> 0)
3415 DEC_FOLDERCOUNT(hfsmp
, dcp
->c_attr
);
3417 dcp
->c_dirchangecnt
++;
3418 dcp
->c_ctime
= tv
.tv_sec
;
3419 dcp
->c_mtime
= tv
.tv_sec
;
3420 (void) cat_update(hfsmp
, &dcp
->c_desc
, &dcp
->c_attr
, NULL
, NULL
);
3422 /* Update the file or directory's state */
3423 cp
->c_flag
|= C_DELETED
;
3424 cp
->c_ctime
= tv
.tv_sec
;
3426 (void) cat_update(hfsmp
, &to_desc
, &cp
->c_attr
, NULL
, NULL
);
3428 hfs_systemfile_unlock(hfsmp
, lockflags
);
3435 * Nobody is using this item; we can safely remove everything.
3437 struct filefork
*temp_rsrc_fork
= NULL
;
3440 int blksize
= hfsmp
->blockSize
;
3442 u_int32_t fileid
= cp
->c_fileid
;
3445 * Figure out if we need to read the resource fork data into
3446 * core before wiping out the catalog record.
3448 * 1) Must not be a directory
3449 * 2) cnode's c_rsrcfork ptr must be NULL.
3450 * 3) rsrc fork must have actual blocks
3452 if ((isdir
== 0) && (cp
->c_rsrcfork
== NULL
) &&
3453 (cp
->c_blocks
- VTOF(vp
)->ff_blocks
)) {
3455 * The resource fork vnode & filefork did not exist.
3456 * Create a temporary one for use in this function only.
3458 MALLOC_ZONE (temp_rsrc_fork
, struct filefork
*, sizeof (struct filefork
), M_HFSFORK
, M_WAITOK
);
3459 bzero(temp_rsrc_fork
, sizeof(struct filefork
));
3460 temp_rsrc_fork
->ff_cp
= cp
;
3461 rl_init(&temp_rsrc_fork
->ff_invalidranges
);
3464 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
| SFL_ATTRIBUTE
| SFL_BITMAP
, HFS_EXCLUSIVE_LOCK
);
3466 /* Look up the resource fork first, if necessary */
3467 if (temp_rsrc_fork
) {
3468 error
= cat_lookup (hfsmp
, &desc
, 1, 0, (struct cat_desc
*) NULL
,
3469 (struct cat_attr
*) NULL
, &temp_rsrc_fork
->ff_data
, NULL
);
3471 FREE_ZONE (temp_rsrc_fork
, sizeof(struct filefork
), M_HFSFORK
);
3472 hfs_systemfile_unlock (hfsmp
, lockflags
);
3477 if (!skip_reserve
) {
3478 if ((error
= cat_preflight(hfsmp
, CAT_DELETE
, NULL
, 0))) {
3479 if (temp_rsrc_fork
) {
3480 FREE_ZONE (temp_rsrc_fork
, sizeof(struct filefork
), M_HFSFORK
);
3482 hfs_systemfile_unlock(hfsmp
, lockflags
);
3487 error
= cat_delete(hfsmp
, &desc
, &cp
->c_attr
);
3489 if (error
&& error
!= ENXIO
&& error
!= ENOENT
) {
3490 printf("hfs_removefile: deleting file %s (id=%d) vol=%s err=%d\n",
3491 cp
->c_desc
.cd_nameptr
, cp
->c_attr
.ca_fileid
, hfsmp
->vcbVN
, error
);
3495 /* Update the parent directory */
3496 if (dcp
->c_entries
> 0)
3498 dcp
->c_dirchangecnt
++;
3499 dcp
->c_ctime
= tv
.tv_sec
;
3500 dcp
->c_mtime
= tv
.tv_sec
;
3501 (void) cat_update(hfsmp
, &dcp
->c_desc
, &dcp
->c_attr
, NULL
, NULL
);
3503 hfs_systemfile_unlock(hfsmp
, lockflags
);
3506 if (temp_rsrc_fork
) {
3507 FREE_ZONE (temp_rsrc_fork
, sizeof(struct filefork
), M_HFSFORK
);
3513 * Now that we've wiped out the catalog record, the file effectively doesn't
3514 * exist anymore. So update the quota records to reflect the loss of the
3515 * data fork and the resource fork.
3518 if (cp
->c_datafork
->ff_blocks
> 0) {
3519 savedbytes
= ((off_t
)cp
->c_datafork
->ff_blocks
* (off_t
)blksize
);
3520 (void) hfs_chkdq(cp
, (int64_t)-(savedbytes
), NOCRED
, 0);
3524 * We may have just deleted the catalog record for a resource fork even
3525 * though it did not exist in core as a vnode. However, just because there
3526 * was a resource fork pointer in the cnode does not mean that it had any blocks.
3528 if (temp_rsrc_fork
|| cp
->c_rsrcfork
) {
3529 if (cp
->c_rsrcfork
) {
3530 if (cp
->c_rsrcfork
->ff_blocks
> 0) {
3531 savedbytes
= ((off_t
)cp
->c_rsrcfork
->ff_blocks
* (off_t
)blksize
);
3532 (void) hfs_chkdq(cp
, (int64_t)-(savedbytes
), NOCRED
, 0);
3536 /* we must have used a temporary fork */
3537 savedbytes
= ((off_t
)temp_rsrc_fork
->ff_blocks
* (off_t
)blksize
);
3538 (void) hfs_chkdq(cp
, (int64_t)-(savedbytes
), NOCRED
, 0);
3542 if (hfsmp
->hfs_flags
& HFS_QUOTAS
) {
3543 (void)hfs_chkiq(cp
, -1, NOCRED
, 0);
3548 * If we didn't get any errors deleting the catalog entry, then go ahead
3549 * and release the backing store now. The filefork pointers are still valid.
3551 if (temp_rsrc_fork
) {
3552 error
= hfs_release_storage (hfsmp
, cp
->c_datafork
, temp_rsrc_fork
, fileid
);
3555 /* if cp->c_rsrcfork == NULL, hfs_release_storage will skip over it. */
3556 error
= hfs_release_storage (hfsmp
, cp
->c_datafork
, cp
->c_rsrcfork
, fileid
);
3560 * If we encountered an error updating the extents and bitmap,
3561 * mark the volume inconsistent. At this point, the catalog record has
3562 * already been deleted, so we can't recover it at this point. We need
3563 * to proceed and update the volume header and mark the cnode C_NOEXISTS.
3564 * The subsequent fsck should be able to recover the free space for us.
3566 hfs_mark_volume_inconsistent(hfsmp
);
3569 /* reset update_vh to 0, since hfs_release_storage should have done it for us */
3573 /* Get rid of the temporary rsrc fork */
3574 if (temp_rsrc_fork
) {
3575 FREE_ZONE (temp_rsrc_fork
, sizeof(struct filefork
), M_HFSFORK
);
3578 cp
->c_flag
|= C_NOEXISTS
;
3579 cp
->c_flag
&= ~C_DELETED
;
3581 cp
->c_touch_chgtime
= TRUE
; /* XXX needed ? */
3585 * We must never get a directory if we're in this else block. We could
3586 * accidentally drop the number of files in the volume header if we did.
3588 hfs_volupdate(hfsmp
, VOL_RMFILE
, (dcp
->c_cnid
== kHFSRootFolderID
));
3593 * All done with this cnode's descriptor...
3595 * Note: all future catalog calls for this cnode must be by
3596 * fileid only. This is OK for HFS (which doesn't have file
3597 * thread records) since HFS doesn't support the removal of
3600 cat_releasedesc(&cp
->c_desc
);
3604 cp
->c_flag
&= ~C_DELETED
;
3609 * If we bailed out earlier, we may need to update the volume header
3610 * to deal with the borrowed blocks accounting.
3612 hfs_volupdate (hfsmp
, VOL_UPDATE
, 0);
3616 hfs_end_transaction(hfsmp
);
3619 dcp
->c_flag
&= ~C_DIR_MODIFICATION
;
3620 wakeup((caddr_t
)&dcp
->c_flag
);
3626 __private_extern__
void
3627 replace_desc(struct cnode
*cp
, struct cat_desc
*cdp
)
3629 // fixes 4348457 and 4463138
3630 if (&cp
->c_desc
== cdp
) {
3634 /* First release allocated name buffer */
3635 if (cp
->c_desc
.cd_flags
& CD_HASBUF
&& cp
->c_desc
.cd_nameptr
!= 0) {
3636 const u_int8_t
*name
= cp
->c_desc
.cd_nameptr
;
3638 cp
->c_desc
.cd_nameptr
= 0;
3639 cp
->c_desc
.cd_namelen
= 0;
3640 cp
->c_desc
.cd_flags
&= ~CD_HASBUF
;
3641 vfs_removename((const char *)name
);
3643 bcopy(cdp
, &cp
->c_desc
, sizeof(cp
->c_desc
));
3645 /* Cnode now owns the name buffer */
3646 cdp
->cd_nameptr
= 0;
3647 cdp
->cd_namelen
= 0;
3648 cdp
->cd_flags
&= ~CD_HASBUF
;
3655 * The VFS layer guarantees that:
3656 * - source and destination will either both be directories, or
3657 * both not be directories.
3658 * - all the vnodes are from the same file system
3660 * When the target is a directory, HFS must ensure that its empty.
3662 * Note that this function requires up to 6 vnodes in order to work properly
3663 * if it is operating on files (and not on directories). This is because only
3664 * files can have resource forks, and we now require iocounts to be held on the
3665 * vnodes corresponding to the resource forks (if applicable) as well as
3666 * the files or directories undergoing rename. The problem with not holding
3667 * iocounts on the resource fork vnodes is that it can lead to a deadlock
3668 * situation: The rsrc fork of the source file may be recycled and reclaimed
3669 * in order to provide a vnode for the destination file's rsrc fork. Since
3670 * data and rsrc forks share the same cnode, we'd eventually try to lock the
3671 * source file's cnode in order to sync its rsrc fork to disk, but it's already
3672 * been locked. By taking the rsrc fork vnodes up front we ensure that they
3673 * cannot be recycled, and that the situation mentioned above cannot happen.
3677 struct vnop_rename_args
/* {
3678 struct vnode *a_fdvp;
3679 struct vnode *a_fvp;
3680 struct componentname *a_fcnp;
3681 struct vnode *a_tdvp;
3682 struct vnode *a_tvp;
3683 struct componentname *a_tcnp;
3684 vfs_context_t a_context;
3687 struct vnode
*tvp
= ap
->a_tvp
;
3688 struct vnode
*tdvp
= ap
->a_tdvp
;
3689 struct vnode
*fvp
= ap
->a_fvp
;
3690 struct vnode
*fdvp
= ap
->a_fdvp
;
3692 * Note that we only need locals for the target/destination's
3693 * resource fork vnode (and only if necessary). We don't care if the
3694 * source has a resource fork vnode or not.
3696 struct vnode
*tvp_rsrc
= NULLVP
;
3697 uint32_t tvp_rsrc_vid
= 0;
3698 struct componentname
*tcnp
= ap
->a_tcnp
;
3699 struct componentname
*fcnp
= ap
->a_fcnp
;
3700 struct proc
*p
= vfs_context_proc(ap
->a_context
);
3705 struct cnode
*error_cnode
;
3706 struct cat_desc from_desc
;
3707 struct cat_desc to_desc
;
3708 struct cat_desc out_desc
;
3709 struct hfsmount
*hfsmp
;
3710 cat_cookie_t cookie
;
3711 int tvp_deleted
= 0;
3712 int started_tr
= 0, got_cookie
= 0;
3713 int took_trunc_lock
= 0;
3716 time_t orig_from_ctime
, orig_to_ctime
;
3717 int emit_rename
= 1;
3718 int emit_delete
= 1;
3721 orig_from_ctime
= VTOC(fvp
)->c_ctime
;
3722 if (tvp
&& VTOC(tvp
)) {
3723 orig_to_ctime
= VTOC(tvp
)->c_ctime
;
3728 hfsmp
= VTOHFS(tdvp
);
3730 * Do special case checks here. If fvp == tvp then we need to check the
3731 * cnode with locks held.
3734 int is_hardlink
= 0;
3736 * In this case, we do *NOT* ever emit a DELETE event.
3737 * We may not necessarily emit a RENAME event
3740 if ((error
= hfs_lock(VTOC(fvp
), HFS_SHARED_LOCK
, HFS_LOCK_DEFAULT
))) {
3743 /* Check to see if the item is a hardlink or not */
3744 is_hardlink
= (VTOC(fvp
)->c_flag
& C_HARDLINK
);
3745 hfs_unlock (VTOC(fvp
));
3748 * If the item is not a hardlink, then case sensitivity must be off, otherwise
3749 * two names should not resolve to the same cnode unless they were case variants.
3754 * Hardlinks are a little trickier. We only want to emit a rename event
3755 * if the item is a hardlink, the parent directories are the same, case sensitivity
3756 * is off, and the case folded names are the same. See the fvp == tvp case below for more
3760 if ((fdvp
== tdvp
) && ((hfsmp
->hfs_flags
& HFS_CASE_SENSITIVE
) == 0)) {
3761 if (hfs_namecmp((const u_int8_t
*)fcnp
->cn_nameptr
, fcnp
->cn_namelen
,
3762 (const u_int8_t
*)tcnp
->cn_nameptr
, tcnp
->cn_namelen
) == 0) {
3763 /* Then in this case only it is ok to emit a rename */
3770 /* c_bsdflags should only be assessed while holding the cnode lock.
3771 * This is not done consistently throughout the code and can result
3772 * in race. This will be fixed via rdar://12181064
3774 if (VTOC(fvp
)->c_bsdflags
& UF_TRACKED
) {
3777 check_for_tracked_file(fvp
, orig_from_ctime
, NAMESPACE_HANDLER_RENAME_OP
, NULL
);
3780 if (tvp
&& VTOC(tvp
)) {
3782 check_for_tracked_file(tvp
, orig_to_ctime
, NAMESPACE_HANDLER_DELETE_OP
, NULL
);
3787 /* When tvp exists, take the truncate lock for hfs_removefile(). */
3788 if (tvp
&& (vnode_isreg(tvp
) || vnode_islnk(tvp
))) {
3789 hfs_lock_truncate(VTOC(tvp
), HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
);
3790 took_trunc_lock
= 1;
3793 error
= hfs_lockfour(VTOC(fdvp
), VTOC(fvp
), VTOC(tdvp
), tvp
? VTOC(tvp
) : NULL
,
3794 HFS_EXCLUSIVE_LOCK
, &error_cnode
);
3796 if (took_trunc_lock
) {
3797 hfs_unlock_truncate(VTOC(tvp
), HFS_LOCK_DEFAULT
);
3798 took_trunc_lock
= 0;
3802 * We hit an error path. If we were trying to re-acquire the locks
3803 * after coming through here once, we might have already obtained
3804 * an iocount on tvp's resource fork vnode. Drop that before dealing
3805 * with the failure. Note this is safe -- since we are in an
3806 * error handling path, we can't be holding the cnode locks.
3809 vnode_put (tvp_rsrc
);
3815 * tvp might no longer exist. If the cause of the lock failure
3816 * was tvp, then we can try again with tvp/tcp set to NULL.
3817 * This is ok because the vfs syscall will vnode_put the vnodes
3818 * after we return from hfs_vnop_rename.
3820 if ((error
== ENOENT
) && (tvp
!= NULL
) && (error_cnode
== VTOC(tvp
))) {
3826 if (emit_rename
&& is_tracked
) {
3827 resolve_nspace_item(fvp
, NAMESPACE_HANDLER_RENAME_FAILED_OP
| NAMESPACE_HANDLER_TRACK_EVENT
);
3836 tcp
= tvp
? VTOC(tvp
) : NULL
;
3839 * Acquire iocounts on the destination's resource fork vnode
3840 * if necessary. If dst/src are files and the dst has a resource
3841 * fork vnode, then we need to try and acquire an iocount on the rsrc vnode.
3842 * If it does not exist, then we don't care and can skip it.
3844 if ((vnode_isreg(fvp
)) || (vnode_islnk(fvp
))) {
3845 if ((tvp
) && (tcp
->c_rsrc_vp
) && (tvp_rsrc
== NULL
)) {
3846 tvp_rsrc
= tcp
->c_rsrc_vp
;
3848 * We can look at the vid here because we're holding the
3849 * cnode lock on the underlying cnode for this rsrc vnode.
3851 tvp_rsrc_vid
= vnode_vid (tvp_rsrc
);
3853 /* Unlock everything to acquire iocount on this rsrc vnode */
3854 if (took_trunc_lock
) {
3855 hfs_unlock_truncate (VTOC(tvp
), HFS_LOCK_DEFAULT
);
3856 took_trunc_lock
= 0;
3858 hfs_unlockfour(fdcp
, fcp
, tdcp
, tcp
);
3860 if (vnode_getwithvid (tvp_rsrc
, tvp_rsrc_vid
)) {
3861 /* iocount acquisition failed. Reset fields and start over.. */
3871 /* Ensure we didn't race src or dst parent directories with rmdir. */
3872 if (fdcp
->c_flag
& (C_NOEXISTS
| C_DELETED
)) {
3877 if (tdcp
->c_flag
& (C_NOEXISTS
| C_DELETED
)) {
3883 /* Check for a race against unlink. The hfs_valid_cnode checks validate
3884 * the parent/child relationship with fdcp and tdcp, as well as the
3885 * component name of the target cnodes.
3887 if ((fcp
->c_flag
& (C_NOEXISTS
| C_DELETED
)) || !hfs_valid_cnode(hfsmp
, fdvp
, fcnp
, fcp
->c_fileid
, NULL
, &error
)) {
3892 if (tcp
&& ((tcp
->c_flag
& (C_NOEXISTS
| C_DELETED
)) || !hfs_valid_cnode(hfsmp
, tdvp
, tcnp
, tcp
->c_fileid
, NULL
, &error
))) {
3894 // hmm, the destination vnode isn't valid any more.
3895 // in this case we can just drop him and pretend he
3896 // never existed in the first place.
3898 if (took_trunc_lock
) {
3899 hfs_unlock_truncate(VTOC(tvp
), HFS_LOCK_DEFAULT
);
3900 took_trunc_lock
= 0;
3904 hfs_unlockfour(fdcp
, fcp
, tdcp
, tcp
);
3909 // retry the locking with tvp null'ed out
3913 fdcp
->c_flag
|= C_DIR_MODIFICATION
;
3915 tdcp
->c_flag
|= C_DIR_MODIFICATION
;
3919 * Disallow renaming of a directory hard link if the source and
3920 * destination parent directories are different, or a directory whose
3921 * descendant is a directory hard link and the one of the ancestors
3922 * of the destination directory is a directory hard link.
3924 if (vnode_isdir(fvp
) && (fdvp
!= tdvp
)) {
3925 if (fcp
->c_flag
& C_HARDLINK
) {
3929 if (fcp
->c_attr
.ca_recflags
& kHFSHasChildLinkMask
) {
3930 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
3931 if (cat_check_link_ancestry(hfsmp
, tdcp
->c_fileid
, 0)) {
3933 hfs_systemfile_unlock(hfsmp
, lockflags
);
3936 hfs_systemfile_unlock(hfsmp
, lockflags
);
3941 * The following edge case is caught here:
3942 * (to cannot be a descendent of from)
3955 if (tdcp
->c_parentcnid
== fcp
->c_fileid
) {
3961 * The following two edge cases are caught here:
3962 * (note tvp is not empty)
3975 if (tvp
&& vnode_isdir(tvp
) && (tcp
->c_entries
!= 0) && fvp
!= tvp
) {
3981 * The following edge case is caught here:
3982 * (the from child and parent are the same)
3995 * Make sure "from" vnode and its parent are changeable.
3997 if ((fcp
->c_bsdflags
& (IMMUTABLE
| APPEND
)) || (fdcp
->c_bsdflags
& APPEND
)) {
4003 * If the destination parent directory is "sticky", then the
4004 * user must own the parent directory, or the destination of
4005 * the rename, otherwise the destination may not be changed
4006 * (except by root). This implements append-only directories.
4008 * Note that checks for immutable and write access are done
4009 * by the call to hfs_removefile.
4011 if (tvp
&& (tdcp
->c_mode
& S_ISTXT
) &&
4012 (suser(vfs_context_ucred(tcnp
->cn_context
), NULL
)) &&
4013 (kauth_cred_getuid(vfs_context_ucred(tcnp
->cn_context
)) != tdcp
->c_uid
) &&
4014 (hfs_owner_rights(hfsmp
, tcp
->c_uid
, vfs_context_ucred(tcnp
->cn_context
), p
, false)) ) {
4019 /* Don't allow modification of the journal or journal_info_block */
4020 if (hfs_is_journal_file(hfsmp
, fcp
) ||
4021 (tcp
&& hfs_is_journal_file(hfsmp
, tcp
))) {
4028 (void)hfs_getinoquota(tcp
);
4030 /* Preflighting done, take fvp out of the name space. */
4033 bzero(&from_desc
, sizeof(from_desc
));
4034 from_desc
.cd_nameptr
= (const u_int8_t
*)fcnp
->cn_nameptr
;
4035 from_desc
.cd_namelen
= fcnp
->cn_namelen
;
4036 from_desc
.cd_parentcnid
= fdcp
->c_fileid
;
4037 from_desc
.cd_flags
= fcp
->c_desc
.cd_flags
& ~(CD_HASBUF
| CD_DECOMPOSED
);
4038 from_desc
.cd_cnid
= fcp
->c_cnid
;
4040 bzero(&to_desc
, sizeof(to_desc
));
4041 to_desc
.cd_nameptr
= (const u_int8_t
*)tcnp
->cn_nameptr
;
4042 to_desc
.cd_namelen
= tcnp
->cn_namelen
;
4043 to_desc
.cd_parentcnid
= tdcp
->c_fileid
;
4044 to_desc
.cd_flags
= fcp
->c_desc
.cd_flags
& ~(CD_HASBUF
| CD_DECOMPOSED
);
4045 to_desc
.cd_cnid
= fcp
->c_cnid
;
4047 if ((error
= hfs_start_transaction(hfsmp
)) != 0) {
4052 /* hfs_vnop_link() and hfs_vnop_rename() set kHFSHasChildLinkMask
4053 * inside a journal transaction and without holding a cnode lock.
4054 * As setting of this bit depends on being in journal transaction for
4055 * concurrency, check this bit again after we start journal transaction for rename
4056 * to ensure that this directory does not have any descendant that
4057 * is a directory hard link.
4059 if (vnode_isdir(fvp
) && (fdvp
!= tdvp
)) {
4060 if (fcp
->c_attr
.ca_recflags
& kHFSHasChildLinkMask
) {
4061 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
4062 if (cat_check_link_ancestry(hfsmp
, tdcp
->c_fileid
, 0)) {
4064 hfs_systemfile_unlock(hfsmp
, lockflags
);
4067 hfs_systemfile_unlock(hfsmp
, lockflags
);
4071 // if it's a hardlink then re-lookup the name so
4072 // that we get the correct cnid in from_desc (see
4073 // the comment in hfs_removefile for more details)
4075 if (fcp
->c_flag
& C_HARDLINK
) {
4076 struct cat_desc tmpdesc
;
4079 tmpdesc
.cd_nameptr
= (const u_int8_t
*)fcnp
->cn_nameptr
;
4080 tmpdesc
.cd_namelen
= fcnp
->cn_namelen
;
4081 tmpdesc
.cd_parentcnid
= fdcp
->c_fileid
;
4082 tmpdesc
.cd_hint
= fdcp
->c_childhint
;
4083 tmpdesc
.cd_flags
= fcp
->c_desc
.cd_flags
& CD_ISDIR
;
4084 tmpdesc
.cd_encoding
= 0;
4086 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
4088 if (cat_lookup(hfsmp
, &tmpdesc
, 0, 0, NULL
, NULL
, NULL
, &real_cnid
) != 0) {
4089 hfs_systemfile_unlock(hfsmp
, lockflags
);
4093 // use the real cnid instead of whatever happened to be there
4094 from_desc
.cd_cnid
= real_cnid
;
4095 hfs_systemfile_unlock(hfsmp
, lockflags
);
4099 * Reserve some space in the Catalog file.
4101 if ((error
= cat_preflight(hfsmp
, CAT_RENAME
+ CAT_DELETE
, &cookie
, p
))) {
4107 * If the destination exists then it may need to be removed.
4109 * Due to HFS's locking system, we should always move the
4110 * existing 'tvp' element to the hidden directory in hfs_vnop_rename.
4111 * Because the VNOP_LOOKUP call enters and exits the filesystem independently
4112 * of the actual vnop that it was trying to do (stat, link, readlink),
4113 * we must release the cnode lock of that element during the interim to
4114 * do MAC checking, vnode authorization, and other calls. In that time,
4115 * the item can be deleted (or renamed over). However, only in the rename
4116 * case is it inappropriate to return ENOENT from any of those calls. Either
4117 * the call should return information about the old element (stale), or get
4118 * information about the newer element that we are about to write in its place.
4120 * HFS lookup has been modified to detect a rename and re-drive its
4121 * lookup internally. For other calls that have already succeeded in
4122 * their lookup call and are waiting to acquire the cnode lock in order
4123 * to proceed, that cnode lock will not fail due to the cnode being marked
4124 * C_NOEXISTS, because it won't have been marked as such. It will only
4125 * have C_DELETED. Thus, they will simply act on the stale open-unlinked
4126 * element. All future callers will get the new element.
4128 * To implement this behavior, we pass the "only_unlink" argument to
4129 * hfs_removefile and hfs_removedir. This will result in the vnode acting
4130 * as though it is open-unlinked. Additionally, when we are done moving the
4131 * element to the hidden directory, we vnode_recycle the target so that it is
4132 * reclaimed as soon as possible. Reclaim and inactive are both
4133 * capable of clearing out unused blocks for an open-unlinked file or dir.
4137 * When fvp matches tvp they could be case variants
4138 * or matching hard links.
4141 if (!(fcp
->c_flag
& C_HARDLINK
)) {
4143 * If they're not hardlinks, then fvp == tvp must mean we
4144 * are using case-insensitive HFS because case-sensitive would
4145 * not use the same vnode for both. In this case we just update
4146 * the catalog for: a -> A
4148 goto skip_rm
; /* simple case variant */
4151 /* For all cases below, we must be using hardlinks */
4152 else if ((fdvp
!= tdvp
) ||
4153 (hfsmp
->hfs_flags
& HFS_CASE_SENSITIVE
)) {
4155 * If the parent directories are not the same, AND the two items
4156 * are hardlinks, posix says to do nothing:
4157 * dir1/fred <-> dir2/bob and the op was mv dir1/fred -> dir2/bob
4158 * We just return 0 in this case.
4160 * If case sensitivity is on, and we are using hardlinks
4161 * then renaming is supposed to do nothing.
4162 * dir1/fred <-> dir2/FRED, and op == mv dir1/fred -> dir2/FRED
4164 goto out
; /* matching hardlinks, nothing to do */
4166 } else if (hfs_namecmp((const u_int8_t
*)fcnp
->cn_nameptr
, fcnp
->cn_namelen
,
4167 (const u_int8_t
*)tcnp
->cn_nameptr
, tcnp
->cn_namelen
) == 0) {
4169 * If we get here, then the following must be true:
4170 * a) We are running case-insensitive HFS+.
4171 * b) Both paths 'fvp' and 'tvp' are in the same parent directory.
4172 * c) the two names are case-variants of each other.
4174 * In this case, we are really only dealing with a single catalog record
4175 * whose name is being updated.
4177 * op is dir1/fred -> dir1/FRED
4179 * We need to special case the name matching, because if
4180 * dir1/fred <-> dir1/bob were the two links, and the
4181 * op was dir1/fred -> dir1/bob
4182 * That would fail/do nothing.
4184 goto skip_rm
; /* case-variant hardlink in the same dir */
4186 goto out
; /* matching hardlink, nothing to do */
4191 if (vnode_isdir(tvp
)) {
4193 * hfs_removedir will eventually call hfs_removefile on the directory
4194 * we're working on, because only hfs_removefile does the renaming of the
4195 * item to the hidden directory. The directory will stay around in the
4196 * hidden directory with C_DELETED until it gets an inactive or a reclaim.
4197 * That way, we can destroy all of the EAs as needed and allow new ones to be
4200 error
= hfs_removedir(tdvp
, tvp
, tcnp
, HFSRM_SKIP_RESERVE
, 1);
4203 error
= hfs_removefile(tdvp
, tvp
, tcnp
, 0, HFSRM_SKIP_RESERVE
, 0, NULL
, 1);
4206 * If the destination file had a resource fork vnode, then we need to get rid of
4207 * its blocks when there are no more references to it. Because the call to
4208 * hfs_removefile above always open-unlinks things, we need to force an inactive/reclaim
4209 * on the resource fork vnode, in order to prevent block leaks. Otherwise,
4210 * the resource fork vnode could prevent the data fork vnode from going out of scope
4211 * because it holds a v_parent reference on it. So we mark it for termination
4212 * with a call to vnode_recycle. hfs_vnop_reclaim has been modified so that it
4213 * can clean up the blocks of open-unlinked files and resource forks.
4215 * We can safely call vnode_recycle on the resource fork because we took an iocount
4216 * reference on it at the beginning of the function.
4219 if ((error
== 0) && (tcp
->c_flag
& C_DELETED
) && (tvp_rsrc
)) {
4220 vnode_recycle(tvp_rsrc
);
4230 /* Mark 'tcp' as being deleted due to a rename */
4231 tcp
->c_flag
|= C_RENAMED
;
4234 * Aggressively mark tvp/tcp for termination to ensure that we recover all blocks
4235 * as quickly as possible.
4241 * All done with tvp and fvp.
4243 * We also jump to this point if there was no destination observed during lookup and namei.
4244 * However, because only iocounts are held at the VFS layer, there is nothing preventing a
4245 * competing thread from racing us and creating a file or dir at the destination of this rename
4246 * operation. If this occurs, it may cause us to get a spurious EEXIST out of the cat_rename
4247 * call below. To preserve rename's atomicity, we need to signal VFS to re-drive the
4248 * namei/lookup and restart the rename operation. EEXIST is an allowable errno to be bubbled
4249 * out of the rename syscall, but not for this reason, since it is a synonym errno for ENOTEMPTY.
4250 * To signal VFS, we return ERECYCLE (which is also used for lookup restarts). This errno
4251 * will be swallowed and it will restart the operation.
4254 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_EXCLUSIVE_LOCK
);
4255 error
= cat_rename(hfsmp
, &from_desc
, &tdcp
->c_desc
, &to_desc
, &out_desc
);
4256 hfs_systemfile_unlock(hfsmp
, lockflags
);
4259 if (error
== EEXIST
) {
4265 /* Invalidate negative cache entries in the destination directory */
4266 if (tdcp
->c_flag
& C_NEG_ENTRIES
) {
4267 cache_purge_negatives(tdvp
);
4268 tdcp
->c_flag
&= ~C_NEG_ENTRIES
;
4271 /* Update cnode's catalog descriptor */
4272 replace_desc(fcp
, &out_desc
);
4273 fcp
->c_parentcnid
= tdcp
->c_fileid
;
4276 /* Now indicate this cnode needs to have date-added written to the finderinfo */
4277 fcp
->c_flag
|= C_NEEDS_DATEADDED
;
4278 (void) hfs_update (fvp
, 0);
4281 hfs_volupdate(hfsmp
, vnode_isdir(fvp
) ? VOL_RMDIR
: VOL_RMFILE
,
4282 (fdcp
->c_cnid
== kHFSRootFolderID
));
4283 hfs_volupdate(hfsmp
, vnode_isdir(fvp
) ? VOL_MKDIR
: VOL_MKFILE
,
4284 (tdcp
->c_cnid
== kHFSRootFolderID
));
4286 /* Update both parent directories. */
4288 if (vnode_isdir(fvp
)) {
4289 /* If the source directory has directory hard link
4290 * descendants, set the kHFSHasChildLinkBit in the
4291 * destination parent hierarchy
4293 if ((fcp
->c_attr
.ca_recflags
& kHFSHasChildLinkMask
) &&
4294 !(tdcp
->c_attr
.ca_recflags
& kHFSHasChildLinkMask
)) {
4296 tdcp
->c_attr
.ca_recflags
|= kHFSHasChildLinkMask
;
4298 error
= cat_set_childlinkbit(hfsmp
, tdcp
->c_parentcnid
);
4300 printf ("hfs_vnop_rename: error updating parent chain for %u\n", tdcp
->c_cnid
);
4304 INC_FOLDERCOUNT(hfsmp
, tdcp
->c_attr
);
4305 DEC_FOLDERCOUNT(hfsmp
, fdcp
->c_attr
);
4308 tdcp
->c_dirchangecnt
++;
4309 if (fdcp
->c_entries
> 0)
4311 fdcp
->c_dirchangecnt
++;
4312 fdcp
->c_touch_chgtime
= TRUE
;
4313 fdcp
->c_touch_modtime
= TRUE
;
4315 fdcp
->c_flag
|= C_FORCEUPDATE
; // XXXdbg - force it out!
4316 (void) hfs_update(fdvp
, 0);
4318 tdcp
->c_childhint
= out_desc
.cd_hint
; /* Cache directory's location */
4319 tdcp
->c_touch_chgtime
= TRUE
;
4320 tdcp
->c_touch_modtime
= TRUE
;
4322 tdcp
->c_flag
|= C_FORCEUPDATE
; // XXXdbg - force it out!
4323 (void) hfs_update(tdvp
, 0);
4325 /* Update the vnode's name now that the rename has completed. */
4326 vnode_update_identity(fvp
, tdvp
, tcnp
->cn_nameptr
, tcnp
->cn_namelen
,
4327 tcnp
->cn_hash
, (VNODE_UPDATE_PARENT
| VNODE_UPDATE_NAME
));
4330 * At this point, we may have a resource fork vnode attached to the
4331 * 'from' vnode. If it exists, we will want to update its name, because
4332 * it contains the old name + _PATH_RSRCFORKSPEC. ("/..namedfork/rsrc").
4334 * Note that the only thing we need to update here is the name attached to
4335 * the vnode, since a resource fork vnode does not have a separate resource
4336 * cnode -- it's still 'fcp'.
4338 if (fcp
->c_rsrc_vp
) {
4339 char* rsrc_path
= NULL
;
4342 /* Create a new temporary buffer that's going to hold the new name */
4343 MALLOC_ZONE (rsrc_path
, caddr_t
, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
4344 len
= snprintf (rsrc_path
, MAXPATHLEN
, "%s%s", tcnp
->cn_nameptr
, _PATH_RSRCFORKSPEC
);
4345 len
= MIN(len
, MAXPATHLEN
);
4348 * vnode_update_identity will do the following for us:
4349 * 1) release reference on the existing rsrc vnode's name.
4350 * 2) copy/insert new name into the name cache
4351 * 3) attach the new name to the resource vnode
4352 * 4) update the vnode's vid
4354 vnode_update_identity (fcp
->c_rsrc_vp
, fvp
, rsrc_path
, len
, 0, (VNODE_UPDATE_NAME
| VNODE_UPDATE_CACHE
));
4356 /* Free the memory associated with the resource fork's name */
4357 FREE_ZONE (rsrc_path
, MAXPATHLEN
, M_NAMEI
);
4361 cat_postflight(hfsmp
, &cookie
, p
);
4364 hfs_end_transaction(hfsmp
);
4367 fdcp
->c_flag
&= ~C_DIR_MODIFICATION
;
4368 wakeup((caddr_t
)&fdcp
->c_flag
);
4370 tdcp
->c_flag
&= ~C_DIR_MODIFICATION
;
4371 wakeup((caddr_t
)&tdcp
->c_flag
);
4374 if (took_trunc_lock
) {
4375 hfs_unlock_truncate(VTOC(tvp
), HFS_LOCK_DEFAULT
);
4378 hfs_unlockfour(fdcp
, fcp
, tdcp
, tcp
);
4380 /* Now vnode_put the resource forks vnodes if necessary */
4382 vnode_put(tvp_rsrc
);
4386 /* After tvp is removed the only acceptable error is EIO */
4387 if (error
&& tvp_deleted
)
4390 if (emit_rename
&& is_tracked
) {
4392 resolve_nspace_item(fvp
, NAMESPACE_HANDLER_RENAME_FAILED_OP
| NAMESPACE_HANDLER_TRACK_EVENT
);
4394 resolve_nspace_item(fvp
, NAMESPACE_HANDLER_RENAME_SUCCESS_OP
| NAMESPACE_HANDLER_TRACK_EVENT
);
4406 hfs_vnop_mkdir(struct vnop_mkdir_args
*ap
)
4408 /***** HACK ALERT ********/
4409 ap
->a_cnp
->cn_flags
|= MAKEENTRY
;
4410 return hfs_makenode(ap
->a_dvp
, ap
->a_vpp
, ap
->a_cnp
, ap
->a_vap
, ap
->a_context
);
4415 * Create a symbolic link.
4418 hfs_vnop_symlink(struct vnop_symlink_args
*ap
)
4420 struct vnode
**vpp
= ap
->a_vpp
;
4421 struct vnode
*dvp
= ap
->a_dvp
;
4422 struct vnode
*vp
= NULL
;
4423 struct cnode
*cp
= NULL
;
4424 struct hfsmount
*hfsmp
;
4425 struct filefork
*fp
;
4426 struct buf
*bp
= NULL
;
4432 /* HFS standard disks don't support symbolic links */
4433 if (VTOVCB(dvp
)->vcbSigWord
!= kHFSPlusSigWord
)
4436 /* Check for empty target name */
4437 if (ap
->a_target
[0] == 0)
4440 hfsmp
= VTOHFS(dvp
);
4441 len
= strlen(ap
->a_target
);
4443 /* Check for free space */
4444 if (((u_int64_t
)hfs_freeblks(hfsmp
, 0) * (u_int64_t
)hfsmp
->blockSize
) < len
) {
4448 /* Create the vnode */
4449 ap
->a_vap
->va_mode
|= S_IFLNK
;
4450 if ((error
= hfs_makenode(dvp
, vpp
, ap
->a_cnp
, ap
->a_vap
, ap
->a_context
))) {
4454 if ((error
= hfs_lock(VTOC(vp
), HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
))) {
4460 if (cp
->c_flag
& (C_NOEXISTS
| C_DELETED
)) {
4465 (void)hfs_getinoquota(cp
);
4468 if ((error
= hfs_start_transaction(hfsmp
)) != 0) {
4474 * Allocate space for the link.
4476 * Since we're already inside a transaction,
4477 * tell hfs_truncate to skip the ubc_setsize.
4479 * Don't need truncate lock since a symlink is treated as a system file.
4481 error
= hfs_truncate(vp
, len
, IO_NOZEROFILL
, 1, 0, ap
->a_context
);
4483 /* On errors, remove the symlink file */
4486 * End the transaction so we don't re-take the cnode lock
4487 * below while inside a transaction (lock order violation).
4489 hfs_end_transaction(hfsmp
);
4491 /* hfs_removefile() requires holding the truncate lock */
4493 hfs_lock_truncate(cp
, HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
);
4494 hfs_lock(cp
, HFS_EXCLUSIVE_LOCK
, HFS_LOCK_ALLOW_NOEXISTS
);
4496 if (hfs_start_transaction(hfsmp
) != 0) {
4498 hfs_unlock_truncate(cp
, HFS_LOCK_DEFAULT
);
4502 (void) hfs_removefile(dvp
, vp
, ap
->a_cnp
, 0, 0, 0, NULL
, 0);
4503 hfs_unlock_truncate(cp
, HFS_LOCK_DEFAULT
);
4507 /* Write the link to disk */
4508 bp
= buf_getblk(vp
, (daddr64_t
)0, roundup((int)fp
->ff_size
, hfsmp
->hfs_physical_block_size
),
4511 journal_modify_block_start(hfsmp
->jnl
, bp
);
4513 datap
= (char *)buf_dataptr(bp
);
4514 bzero(datap
, buf_size(bp
));
4515 bcopy(ap
->a_target
, datap
, len
);
4518 journal_modify_block_end(hfsmp
->jnl
, bp
, NULL
, NULL
);
4523 * We defered the ubc_setsize for hfs_truncate
4524 * since we were inside a transaction.
4526 * We don't need to drop the cnode lock here
4527 * since this is a symlink.
4529 ubc_setsize(vp
, len
);
4532 hfs_end_transaction(hfsmp
);
4533 if ((cp
!= NULL
) && (vp
!= NULL
)) {
4546 /* structures to hold a "." or ".." directory entry */
4547 struct hfs_stddotentry
{
4548 u_int32_t d_fileno
; /* unique file number */
4549 u_int16_t d_reclen
; /* length of this structure */
4550 u_int8_t d_type
; /* dirent file type */
4551 u_int8_t d_namlen
; /* len of filename */
4552 char d_name
[4]; /* "." or ".." */
4555 struct hfs_extdotentry
{
4556 u_int64_t d_fileno
; /* unique file number */
4557 u_int64_t d_seekoff
; /* seek offset (optional, used by servers) */
4558 u_int16_t d_reclen
; /* length of this structure */
4559 u_int16_t d_namlen
; /* len of filename */
4560 u_int8_t d_type
; /* dirent file type */
4561 u_char d_name
[3]; /* "." or ".." */
4565 struct hfs_stddotentry std
;
4566 struct hfs_extdotentry ext
;
4570 * hfs_vnop_readdir reads directory entries into the buffer pointed
4571 * to by uio, in a filesystem independent format. Up to uio_resid
4572 * bytes of data can be transferred. The data in the buffer is a
4573 * series of packed dirent structures where each one contains the
4574 * following entries:
4576 * u_int32_t d_fileno; // file number of entry
4577 * u_int16_t d_reclen; // length of this record
4578 * u_int8_t d_type; // file type
4579 * u_int8_t d_namlen; // length of string in d_name
4580 * char d_name[MAXNAMELEN+1]; // null terminated file name
4582 * The current position (uio_offset) refers to the next block of
4583 * entries. The offset can only be set to a value previously
4584 * returned by hfs_vnop_readdir or zero. This offset does not have
4585 * to match the number of bytes returned (in uio_resid).
4587 * In fact, the offset used by HFS is essentially an index (26 bits)
4588 * with a tag (6 bits). The tag is for associating the next request
4589 * with the current request. This enables us to have multiple threads
4590 * reading the directory while the directory is also being modified.
4592 * Each tag/index pair is tied to a unique directory hint. The hint
4593 * contains information (filename) needed to build the catalog b-tree
4594 * key for finding the next set of entries.
4596 * If the directory is marked as deleted-but-in-use (cp->c_flag & C_DELETED),
4597 * do NOT synthesize entries for "." and "..".
4600 hfs_vnop_readdir(ap
)
4601 struct vnop_readdir_args
/* {
4607 vfs_context_t a_context;
4610 struct vnode
*vp
= ap
->a_vp
;
4611 uio_t uio
= ap
->a_uio
;
4613 struct hfsmount
*hfsmp
;
4614 directoryhint_t
*dirhint
= NULL
;
4615 directoryhint_t localhint
;
4620 user_addr_t user_start
= 0;
4621 user_size_t user_len
= 0;
4628 cnid_t cnid_hint
= 0;
4631 startoffset
= offset
= uio_offset(uio
);
4632 extended
= (ap
->a_flags
& VNODE_READDIR_EXTENDED
);
4633 nfs_cookies
= extended
&& (ap
->a_flags
& VNODE_READDIR_REQSEEKOFF
);
4635 /* Sanity check the uio data. */
4636 if (uio_iovcnt(uio
) > 1)
4639 if (VTOC(vp
)->c_bsdflags
& UF_COMPRESSED
) {
4640 int compressed
= hfs_file_is_compressed(VTOC(vp
), 0); /* 0 == take the cnode lock */
4641 if (VTOCMP(vp
) != NULL
&& !compressed
) {
4642 error
= check_for_dataless_file(vp
, NAMESPACE_HANDLER_READ_OP
);
4652 /* Note that the dirhint calls require an exclusive lock. */
4653 if ((error
= hfs_lock(VTOC(vp
), HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
)))
4656 /* Pick up cnid hint (if any). */
4658 cnid_hint
= (cnid_t
)(uio_offset(uio
) >> 32);
4659 uio_setoffset(uio
, uio_offset(uio
) & 0x00000000ffffffffLL
);
4660 if (cnid_hint
== INT_MAX
) { /* searching pass the last item */
4666 * Synthesize entries for "." and "..", unless the directory has
4667 * been deleted, but not closed yet (lazy delete in progress).
4669 if (offset
== 0 && !(cp
->c_flag
& C_DELETED
)) {
4670 hfs_dotentry_t dotentry
[2];
4674 struct hfs_extdotentry
*entry
= &dotentry
[0].ext
;
4676 entry
->d_fileno
= cp
->c_cnid
;
4677 entry
->d_reclen
= sizeof(struct hfs_extdotentry
);
4678 entry
->d_type
= DT_DIR
;
4679 entry
->d_namlen
= 1;
4680 entry
->d_name
[0] = '.';
4681 entry
->d_name
[1] = '\0';
4682 entry
->d_name
[2] = '\0';
4683 entry
->d_seekoff
= 1;
4686 entry
->d_fileno
= cp
->c_parentcnid
;
4687 entry
->d_reclen
= sizeof(struct hfs_extdotentry
);
4688 entry
->d_type
= DT_DIR
;
4689 entry
->d_namlen
= 2;
4690 entry
->d_name
[0] = '.';
4691 entry
->d_name
[1] = '.';
4692 entry
->d_name
[2] = '\0';
4693 entry
->d_seekoff
= 2;
4694 uiosize
= 2 * sizeof(struct hfs_extdotentry
);
4696 struct hfs_stddotentry
*entry
= &dotentry
[0].std
;
4698 entry
->d_fileno
= cp
->c_cnid
;
4699 entry
->d_reclen
= sizeof(struct hfs_stddotentry
);
4700 entry
->d_type
= DT_DIR
;
4701 entry
->d_namlen
= 1;
4702 *(int *)&entry
->d_name
[0] = 0;
4703 entry
->d_name
[0] = '.';
4706 entry
->d_fileno
= cp
->c_parentcnid
;
4707 entry
->d_reclen
= sizeof(struct hfs_stddotentry
);
4708 entry
->d_type
= DT_DIR
;
4709 entry
->d_namlen
= 2;
4710 *(int *)&entry
->d_name
[0] = 0;
4711 entry
->d_name
[0] = '.';
4712 entry
->d_name
[1] = '.';
4713 uiosize
= 2 * sizeof(struct hfs_stddotentry
);
4715 if ((error
= uiomove((caddr_t
)&dotentry
, uiosize
, uio
))) {
4721 /* If there are no real entries then we're done. */
4722 if (cp
->c_entries
== 0) {
4725 uio_setoffset(uio
, offset
);
4730 // We have to lock the user's buffer here so that we won't
4731 // fault on it after we've acquired a shared lock on the
4732 // catalog file. The issue is that you can get a 3-way
4733 // deadlock if someone else starts a transaction and then
4734 // tries to lock the catalog file but can't because we're
4735 // here and we can't service our page fault because VM is
4736 // blocked trying to start a transaction as a result of
4737 // trying to free up pages for our page fault. It's messy
4738 // but it does happen on dual-processors that are paging
4739 // heavily (see radar 3082639 for more info). By locking
4740 // the buffer up-front we prevent ourselves from faulting
4741 // while holding the shared catalog file lock.
4743 // Fortunately this and hfs_search() are the only two places
4744 // currently (10/30/02) that can fault on user data with a
4745 // shared lock on the catalog file.
4747 if (hfsmp
->jnl
&& uio_isuserspace(uio
)) {
4748 user_start
= uio_curriovbase(uio
);
4749 user_len
= uio_curriovlen(uio
);
4751 if ((error
= vslock(user_start
, user_len
)) != 0) {
4756 /* Convert offset into a catalog directory index. */
4757 index
= (offset
& HFS_INDEX_MASK
) - 2;
4758 tag
= offset
& ~HFS_INDEX_MASK
;
4760 /* Lock catalog during cat_findname and cat_getdirentries. */
4761 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
4763 /* When called from NFS, try and resolve a cnid hint. */
4764 if (nfs_cookies
&& cnid_hint
!= 0) {
4765 if (cat_findname(hfsmp
, cnid_hint
, &localhint
.dh_desc
) == 0) {
4766 if ( localhint
.dh_desc
.cd_parentcnid
== cp
->c_fileid
) {
4767 localhint
.dh_index
= index
- 1;
4768 localhint
.dh_time
= 0;
4769 bzero(&localhint
.dh_link
, sizeof(localhint
.dh_link
));
4770 dirhint
= &localhint
; /* don't forget to release the descriptor */
4772 cat_releasedesc(&localhint
.dh_desc
);
4777 /* Get a directory hint (cnode must be locked exclusive) */
4778 if (dirhint
== NULL
) {
4779 dirhint
= hfs_getdirhint(cp
, ((index
- 1) & HFS_INDEX_MASK
) | tag
, 0);
4781 /* Hide tag from catalog layer. */
4782 dirhint
->dh_index
&= HFS_INDEX_MASK
;
4783 if (dirhint
->dh_index
== HFS_INDEX_MASK
) {
4784 dirhint
->dh_index
= -1;
4789 dirhint
->dh_threadhint
= cp
->c_dirthreadhint
;
4793 * If we have a non-zero index, there is a possibility that during the last
4794 * call to hfs_vnop_readdir we hit EOF for this directory. If that is the case
4795 * then we don't want to return any new entries for the caller. Just return 0
4796 * items, mark the eofflag, and bail out. Because we won't have done any work, the
4797 * code at the end of the function will release the dirhint for us.
4799 * Don't forget to unlock the catalog lock on the way out, too.
4801 if (dirhint
->dh_desc
.cd_flags
& CD_EOF
) {
4804 uio_setoffset(uio
, startoffset
);
4805 hfs_systemfile_unlock (hfsmp
, lockflags
);
4811 /* Pack the buffer with dirent entries. */
4812 error
= cat_getdirentries(hfsmp
, cp
->c_entries
, dirhint
, uio
, ap
->a_flags
, &items
, &eofflag
);
4814 if (index
== 0 && error
== 0) {
4815 cp
->c_dirthreadhint
= dirhint
->dh_threadhint
;
4818 hfs_systemfile_unlock(hfsmp
, lockflags
);
4824 /* Get index to the next item */
4827 if (items
>= (int)cp
->c_entries
) {
4831 /* Convert catalog directory index back into an offset. */
4833 tag
= (++cp
->c_dirhinttag
) << HFS_INDEX_BITS
;
4834 uio_setoffset(uio
, (index
+ 2) | tag
);
4835 dirhint
->dh_index
|= tag
;
4838 cp
->c_touch_acctime
= TRUE
;
4840 if (ap
->a_numdirent
) {
4841 if (startoffset
== 0)
4843 *ap
->a_numdirent
= items
;
4848 vsunlock(user_start
, user_len
, TRUE
);
4850 /* If we didn't do anything then go ahead and dump the hint. */
4851 if ((dirhint
!= NULL
) &&
4852 (dirhint
!= &localhint
) &&
4853 (uio_offset(uio
) == startoffset
)) {
4854 hfs_reldirhint(cp
, dirhint
);
4857 if (ap
->a_eofflag
) {
4858 *ap
->a_eofflag
= eofflag
;
4860 if (dirhint
== &localhint
) {
4861 cat_releasedesc(&localhint
.dh_desc
);
4869 * Read contents of a symbolic link.
4872 hfs_vnop_readlink(ap
)
4873 struct vnop_readlink_args
/* {
4876 vfs_context_t a_context;
4879 struct vnode
*vp
= ap
->a_vp
;
4881 struct filefork
*fp
;
4884 if (!vnode_islnk(vp
))
4887 if ((error
= hfs_lock(VTOC(vp
), HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
)))
4892 /* Zero length sym links are not allowed */
4893 if (fp
->ff_size
== 0 || fp
->ff_size
> MAXPATHLEN
) {
4898 /* Cache the path so we don't waste buffer cache resources */
4899 if (fp
->ff_symlinkptr
== NULL
) {
4900 struct buf
*bp
= NULL
;
4902 MALLOC(fp
->ff_symlinkptr
, char *, fp
->ff_size
, M_TEMP
, M_WAITOK
);
4903 if (fp
->ff_symlinkptr
== NULL
) {
4907 error
= (int)buf_meta_bread(vp
, (daddr64_t
)0,
4908 roundup((int)fp
->ff_size
, VTOHFS(vp
)->hfs_physical_block_size
),
4909 vfs_context_ucred(ap
->a_context
), &bp
);
4913 if (fp
->ff_symlinkptr
) {
4914 FREE(fp
->ff_symlinkptr
, M_TEMP
);
4915 fp
->ff_symlinkptr
= NULL
;
4919 bcopy((char *)buf_dataptr(bp
), fp
->ff_symlinkptr
, (size_t)fp
->ff_size
);
4921 if (VTOHFS(vp
)->jnl
&& (buf_flags(bp
) & B_LOCKED
) == 0) {
4922 buf_markinvalid(bp
); /* data no longer needed */
4926 error
= uiomove((caddr_t
)fp
->ff_symlinkptr
, (int)fp
->ff_size
, ap
->a_uio
);
4929 * Keep track blocks read
4931 if ((VTOHFS(vp
)->hfc_stage
== HFC_RECORDING
) && (error
== 0)) {
4934 * If this file hasn't been seen since the start of
4935 * the current sampling period then start over.
4937 if (cp
->c_atime
< VTOHFS(vp
)->hfc_timebase
)
4938 VTOF(vp
)->ff_bytesread
= fp
->ff_size
;
4940 VTOF(vp
)->ff_bytesread
+= fp
->ff_size
;
4942 // if (VTOF(vp)->ff_bytesread > fp->ff_size)
4943 // cp->c_touch_acctime = TRUE;
4953 * Get configurable pathname variables.
4956 hfs_vnop_pathconf(ap
)
4957 struct vnop_pathconf_args
/* {
4961 vfs_context_t a_context;
4965 int std_hfs
= (VTOHFS(ap
->a_vp
)->hfs_flags
& HFS_STANDARD
);
4966 switch (ap
->a_name
) {
4969 *ap
->a_retval
= HFS_LINK_MAX
;
4979 *ap
->a_retval
= kHFSPlusMaxFileNameChars
; /* 255 */
4983 *ap
->a_retval
= kHFSMaxFileNameChars
; /* 31 */
4988 *ap
->a_retval
= PATH_MAX
; /* 1024 */
4991 *ap
->a_retval
= PIPE_BUF
;
4993 case _PC_CHOWN_RESTRICTED
:
4994 *ap
->a_retval
= 200112; /* _POSIX_CHOWN_RESTRICTED */
4997 *ap
->a_retval
= 200112; /* _POSIX_NO_TRUNC */
4999 case _PC_NAME_CHARS_MAX
:
5001 *ap
->a_retval
= kHFSPlusMaxFileNameChars
; /* 255 */
5005 *ap
->a_retval
= kHFSMaxFileNameChars
; /* 31 */
5009 case _PC_CASE_SENSITIVE
:
5010 if (VTOHFS(ap
->a_vp
)->hfs_flags
& HFS_CASE_SENSITIVE
)
5015 case _PC_CASE_PRESERVING
:
5018 case _PC_FILESIZEBITS
:
5019 /* number of bits to store max file size */
5029 case _PC_XATTR_SIZE_BITS
:
5030 /* Number of bits to store maximum extended attribute size */
5031 *ap
->a_retval
= HFS_XATTR_SIZE_BITS
;
5042 * Update a cnode's on-disk metadata.
5044 * If waitfor is set, then wait for the disk write of
5045 * the node to complete.
5047 * The cnode must be locked exclusive
5050 hfs_update(struct vnode
*vp
, __unused
int waitfor
)
5052 struct cnode
*cp
= VTOC(vp
);
5054 struct cat_fork
*dataforkp
= NULL
;
5055 struct cat_fork
*rsrcforkp
= NULL
;
5056 struct cat_fork datafork
;
5057 struct cat_fork rsrcfork
;
5058 struct hfsmount
*hfsmp
;
5061 uint32_t tstate
= 0;
5066 if (((vnode_issystem(vp
) && (cp
->c_cnid
< kHFSFirstUserCatalogNodeID
))) ||
5067 hfsmp
->hfs_catalog_vp
== NULL
){
5070 if ((hfsmp
->hfs_flags
& HFS_READ_ONLY
) || (cp
->c_mode
== 0)) {
5071 cp
->c_flag
&= ~C_MODIFIED
;
5072 cp
->c_touch_acctime
= 0;
5073 cp
->c_touch_chgtime
= 0;
5074 cp
->c_touch_modtime
= 0;
5077 if (kdebug_enable
) {
5078 if (cp
->c_touch_acctime
)
5079 tstate
|= DBG_HFS_UPDATE_ACCTIME
;
5080 if (cp
->c_touch_modtime
)
5081 tstate
|= DBG_HFS_UPDATE_MODTIME
;
5082 if (cp
->c_touch_chgtime
)
5083 tstate
|= DBG_HFS_UPDATE_CHGTIME
;
5085 if (cp
->c_flag
& C_MODIFIED
)
5086 tstate
|= DBG_HFS_UPDATE_MODIFIED
;
5087 if (cp
->c_flag
& C_FORCEUPDATE
)
5088 tstate
|= DBG_HFS_UPDATE_FORCE
;
5089 if (cp
->c_flag
& C_NEEDS_DATEADDED
)
5090 tstate
|= DBG_HFS_UPDATE_DATEADDED
;
5092 hfs_touchtimes(hfsmp
, cp
);
5094 /* Nothing to update. */
5095 if ((cp
->c_flag
& (C_MODIFIED
| C_FORCEUPDATE
)) == 0) {
5100 dataforkp
= &cp
->c_datafork
->ff_data
;
5102 rsrcforkp
= &cp
->c_rsrcfork
->ff_data
;
5105 * For delayed allocations updates are
5106 * postponed until an fsync or the file
5107 * gets written to disk.
5109 * Deleted files can defer meta data updates until inactive.
5111 * If we're ever called with the C_FORCEUPDATE flag though
5112 * we have to do the update.
5114 if (ISSET(cp
->c_flag
, C_FORCEUPDATE
) == 0 &&
5115 (ISSET(cp
->c_flag
, C_DELETED
) ||
5116 (dataforkp
&& cp
->c_datafork
->ff_unallocblocks
) ||
5117 (rsrcforkp
&& cp
->c_rsrcfork
->ff_unallocblocks
))) {
5118 // cp->c_flag &= ~(C_ACCESS | C_CHANGE | C_UPDATE);
5119 cp
->c_flag
|= C_MODIFIED
;
5124 KERNEL_DEBUG_CONSTANT(0x3018000 | DBG_FUNC_START
, vp
, tstate
, 0, 0, 0);
5126 if ((error
= hfs_start_transaction(hfsmp
)) != 0) {
5128 KERNEL_DEBUG_CONSTANT(0x3018000 | DBG_FUNC_END
, vp
, tstate
, error
, -1, 0);
5133 * Modify the values passed to cat_update based on whether or not
5134 * the file has invalid ranges or borrowed blocks.
5139 /* copy the datafork into a temporary copy so we don't pollute the cnode's */
5140 bcopy(dataforkp
, &datafork
, sizeof(datafork
));
5141 dataforkp
= &datafork
;
5144 * If there are borrowed blocks, ensure that they are subtracted
5145 * from the total block count before writing the cnode entry to disk.
5146 * Only extents that have actually been marked allocated in the bitmap
5147 * should be reflected in the total block count for this fork.
5149 if (cp
->c_datafork
->ff_unallocblocks
!= 0) {
5150 // make sure that we don't assign a negative block count
5151 if (cp
->c_datafork
->ff_blocks
< cp
->c_datafork
->ff_unallocblocks
) {
5152 panic("hfs: ff_blocks %d is less than unalloc blocks %d\n",
5153 cp
->c_datafork
->ff_blocks
, cp
->c_datafork
->ff_unallocblocks
);
5156 /* Also cap the LEOF to the total number of bytes that are allocated. */
5157 datafork
.cf_blocks
= (cp
->c_datafork
->ff_blocks
- cp
->c_datafork
->ff_unallocblocks
);
5158 datafork
.cf_size
= datafork
.cf_blocks
* HFSTOVCB(hfsmp
)->blockSize
;
5162 * For files with invalid ranges (holes) the on-disk
5163 * field representing the size of the file (cf_size)
5164 * must be no larger than the start of the first hole.
5165 * However, note that if the first invalid range exists
5166 * solely within borrowed blocks, then our LEOF and block
5167 * count should both be zero. As a result, set it to the
5168 * min of the current cf_size and the start of the first
5169 * invalid range, because it may have already been reduced
5170 * to zero by the borrowed blocks check above.
5172 if (!TAILQ_EMPTY(&cp
->c_datafork
->ff_invalidranges
)) {
5173 numbytes
= TAILQ_FIRST(&cp
->c_datafork
->ff_invalidranges
)->rl_start
;
5174 datafork
.cf_size
= MIN((numbytes
), (datafork
.cf_size
));
5179 * For resource forks with delayed allocations, make sure
5180 * the block count and file size match the number of blocks
5181 * actually allocated to the file on disk.
5183 if (rsrcforkp
&& (cp
->c_rsrcfork
->ff_unallocblocks
!= 0)) {
5184 bcopy(rsrcforkp
, &rsrcfork
, sizeof(rsrcfork
));
5185 rsrcfork
.cf_blocks
= (cp
->c_rsrcfork
->ff_blocks
- cp
->c_rsrcfork
->ff_unallocblocks
);
5186 rsrcfork
.cf_size
= rsrcfork
.cf_blocks
* HFSTOVCB(hfsmp
)->blockSize
;
5187 rsrcforkp
= &rsrcfork
;
5189 if (kdebug_enable
) {
5190 long dbg_parms
[NUMPARMS
];
5193 dbg_namelen
= NUMPARMS
* sizeof(long);
5194 vn_getpath(vp
, (char *)dbg_parms
, &dbg_namelen
);
5196 if (dbg_namelen
< (int)sizeof(dbg_parms
))
5197 memset((char *)dbg_parms
+ dbg_namelen
, 0, sizeof(dbg_parms
) - dbg_namelen
);
5199 kdebug_lookup_gen_events(dbg_parms
, dbg_namelen
, (void *)vp
, TRUE
);
5203 * Lock the Catalog b-tree file.
5205 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_EXCLUSIVE_LOCK
);
5207 /* XXX - waitfor is not enforced */
5208 error
= cat_update(hfsmp
, &cp
->c_desc
, &cp
->c_attr
, dataforkp
, rsrcforkp
);
5210 hfs_systemfile_unlock(hfsmp
, lockflags
);
5212 /* After the updates are finished, clear the flags */
5213 cp
->c_flag
&= ~(C_MODIFIED
| C_FORCEUPDATE
);
5215 hfs_end_transaction(hfsmp
);
5217 KERNEL_DEBUG_CONSTANT(0x3018000 | DBG_FUNC_END
, vp
, tstate
, error
, 0, 0);
5223 * Allocate a new node
5224 * Note - Function does not create and return a vnode for whiteout creation.
5227 hfs_makenode(struct vnode
*dvp
, struct vnode
**vpp
, struct componentname
*cnp
,
5228 struct vnode_attr
*vap
, vfs_context_t ctx
)
5230 struct cnode
*cp
= NULL
;
5231 struct cnode
*dcp
= NULL
;
5233 struct hfsmount
*hfsmp
;
5234 struct cat_desc in_desc
, out_desc
;
5235 struct cat_attr attr
;
5238 int error
, started_tr
= 0;
5239 enum vtype vnodetype
;
5241 int newvnode_flags
= 0;
5242 u_int32_t gnv_flags
= 0;
5243 int protectable_target
= 0;
5247 struct cprotect
*entry
= NULL
;
5248 int32_t cp_class
= -1;
5249 if (VATTR_IS_ACTIVE(vap
, va_dataprotect_class
)) {
5250 cp_class
= (int32_t)vap
->va_dataprotect_class
;
5252 int protected_mount
= 0;
5256 if ((error
= hfs_lock(VTOC(dvp
), HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
)))
5259 /* set the cnode pointer only after successfully acquiring lock */
5262 /* Don't allow creation of new entries in open-unlinked directories */
5263 if ((error
= hfs_checkdeleted(dcp
))) {
5268 dcp
->c_flag
|= C_DIR_MODIFICATION
;
5270 hfsmp
= VTOHFS(dvp
);
5274 out_desc
.cd_flags
= 0;
5275 out_desc
.cd_nameptr
= NULL
;
5277 vnodetype
= vap
->va_type
;
5278 if (vnodetype
== VNON
)
5280 mode
= MAKEIMODE(vnodetype
, vap
->va_mode
);
5282 if (S_ISDIR (mode
) || S_ISREG (mode
)) {
5283 protectable_target
= 1;
5287 /* Check if were out of usable disk space. */
5288 if ((hfs_freeblks(hfsmp
, 1) == 0) && (vfs_context_suser(ctx
) != 0)) {
5295 /* Setup the default attributes */
5296 bzero(&attr
, sizeof(attr
));
5297 attr
.ca_mode
= mode
;
5298 attr
.ca_linkcount
= 1;
5299 if (VATTR_IS_ACTIVE(vap
, va_rdev
)) {
5300 attr
.ca_rdev
= vap
->va_rdev
;
5302 if (VATTR_IS_ACTIVE(vap
, va_create_time
)) {
5303 VATTR_SET_SUPPORTED(vap
, va_create_time
);
5304 attr
.ca_itime
= vap
->va_create_time
.tv_sec
;
5306 attr
.ca_itime
= tv
.tv_sec
;
5309 if ((hfsmp
->hfs_flags
& HFS_STANDARD
) && gTimeZone
.tz_dsttime
) {
5310 attr
.ca_itime
+= 3600; /* Same as what hfs_update does */
5313 attr
.ca_atime
= attr
.ca_ctime
= attr
.ca_mtime
= attr
.ca_itime
;
5314 attr
.ca_atimeondisk
= attr
.ca_atime
;
5315 if (VATTR_IS_ACTIVE(vap
, va_flags
)) {
5316 VATTR_SET_SUPPORTED(vap
, va_flags
);
5317 attr
.ca_flags
= vap
->va_flags
;
5321 * HFS+ only: all files get ThreadExists
5322 * HFSX only: dirs get HasFolderCount
5324 if (!(hfsmp
->hfs_flags
& HFS_STANDARD
)) {
5325 if (vnodetype
== VDIR
) {
5326 if (hfsmp
->hfs_flags
& HFS_FOLDERCOUNT
)
5327 attr
.ca_recflags
= kHFSHasFolderCountMask
;
5329 attr
.ca_recflags
= kHFSThreadExistsMask
;
5334 if (cp_fs_protected(hfsmp
->hfs_mp
)) {
5335 protected_mount
= 1;
5338 * On a content-protected HFS+/HFSX filesystem, files and directories
5339 * cannot be created without atomically setting/creating the EA that
5340 * contains the protection class metadata and keys at the same time, in
5341 * the same transaction. As a result, pre-set the "EAs exist" flag
5342 * on the cat_attr for protectable catalog record creations. This will
5343 * cause the cnode creation routine in hfs_getnewvnode to mark the cnode
5346 if ((protected_mount
) && (protectable_target
)) {
5347 attr
.ca_recflags
|= kHFSHasAttributesMask
;
5348 /* delay entering in the namecache */
5355 * Add the date added to the item. See above, as
5356 * all of the dates are set to the itime.
5358 hfs_write_dateadded (&attr
, attr
.ca_atime
);
5360 /* Initialize the gen counter to 1 */
5361 hfs_write_gencount(&attr
, (uint32_t)1);
5363 attr
.ca_uid
= vap
->va_uid
;
5364 attr
.ca_gid
= vap
->va_gid
;
5365 VATTR_SET_SUPPORTED(vap
, va_mode
);
5366 VATTR_SET_SUPPORTED(vap
, va_uid
);
5367 VATTR_SET_SUPPORTED(vap
, va_gid
);
5370 /* check to see if this node's creation would cause us to go over
5371 * quota. If so, abort this operation.
5373 if (hfsmp
->hfs_flags
& HFS_QUOTAS
) {
5374 if ((error
= hfs_quotacheck(hfsmp
, 1, attr
.ca_uid
, attr
.ca_gid
,
5375 vfs_context_ucred(ctx
)))) {
5382 /* Tag symlinks with a type and creator. */
5383 if (vnodetype
== VLNK
) {
5384 struct FndrFileInfo
*fip
;
5386 fip
= (struct FndrFileInfo
*)&attr
.ca_finderinfo
;
5387 fip
->fdType
= SWAP_BE32(kSymLinkFileType
);
5388 fip
->fdCreator
= SWAP_BE32(kSymLinkCreator
);
5390 if (cnp
->cn_flags
& ISWHITEOUT
)
5391 attr
.ca_flags
|= UF_OPAQUE
;
5393 /* Setup the descriptor */
5394 in_desc
.cd_nameptr
= (const u_int8_t
*)cnp
->cn_nameptr
;
5395 in_desc
.cd_namelen
= cnp
->cn_namelen
;
5396 in_desc
.cd_parentcnid
= dcp
->c_fileid
;
5397 in_desc
.cd_flags
= S_ISDIR(mode
) ? CD_ISDIR
: 0;
5398 in_desc
.cd_hint
= dcp
->c_childhint
;
5399 in_desc
.cd_encoding
= 0;
5403 * To preserve file creation atomicity with regards to the content protection EA,
5404 * we must create the file in the catalog and then write out its EA in the same
5407 * We only denote the target class in this EA; key generation is not completed
5408 * until the file has been inserted into the catalog and will be done
5409 * in a separate transaction.
5411 if ((protected_mount
) && (protectable_target
)) {
5412 error
= cp_setup_newentry(hfsmp
, dcp
, cp_class
, attr
.ca_mode
, &entry
);
5419 if ((error
= hfs_start_transaction(hfsmp
)) != 0) {
5424 // have to also lock the attribute file because cat_create() needs
5425 // to check that any fileID it wants to use does not have orphaned
5426 // attributes in it.
5427 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
| SFL_ATTRIBUTE
, HFS_EXCLUSIVE_LOCK
);
5430 /* Reserve some space in the Catalog file. */
5431 if ((error
= cat_preflight(hfsmp
, CAT_CREATE
, NULL
, 0))) {
5432 hfs_systemfile_unlock(hfsmp
, lockflags
);
5436 if ((error
= cat_acquire_cnid(hfsmp
, &new_id
))) {
5437 hfs_systemfile_unlock (hfsmp
, lockflags
);
5441 error
= cat_create(hfsmp
, new_id
, &in_desc
, &attr
, &out_desc
);
5443 /* Update the parent directory */
5444 dcp
->c_childhint
= out_desc
.cd_hint
; /* Cache directory's location */
5446 if (vnodetype
== VDIR
) {
5447 INC_FOLDERCOUNT(hfsmp
, dcp
->c_attr
);
5449 dcp
->c_dirchangecnt
++;
5450 dcp
->c_ctime
= tv
.tv_sec
;
5451 dcp
->c_mtime
= tv
.tv_sec
;
5452 (void) cat_update(hfsmp
, &dcp
->c_desc
, &dcp
->c_attr
, NULL
, NULL
);
5456 * If we are creating a content protected file, now is when
5457 * we create the EA. We must create it in the same transaction
5458 * that creates the file. We can also guarantee that the file
5459 * MUST exist because we are still holding the catalog lock
5462 if ((attr
.ca_fileid
!= 0) && (protected_mount
) && (protectable_target
)) {
5463 error
= cp_setxattr (NULL
, entry
, hfsmp
, attr
.ca_fileid
, XATTR_CREATE
);
5468 * If we fail the EA creation, then we need to delete the file.
5469 * Luckily, we are still holding all of the right locks.
5471 delete_err
= cat_delete (hfsmp
, &out_desc
, &attr
);
5472 if (delete_err
== 0) {
5473 /* Update the parent directory */
5474 if (dcp
->c_entries
> 0)
5476 dcp
->c_dirchangecnt
++;
5477 dcp
->c_ctime
= tv
.tv_sec
;
5478 dcp
->c_mtime
= tv
.tv_sec
;
5479 (void) cat_update(hfsmp
, &dcp
->c_desc
, &dcp
->c_attr
, NULL
, NULL
);
5482 /* Emit EINVAL if we fail to create EA*/
5488 hfs_systemfile_unlock(hfsmp
, lockflags
);
5492 /* Invalidate negative cache entries in the directory */
5493 if (dcp
->c_flag
& C_NEG_ENTRIES
) {
5494 cache_purge_negatives(dvp
);
5495 dcp
->c_flag
&= ~C_NEG_ENTRIES
;
5498 hfs_volupdate(hfsmp
, vnodetype
== VDIR
? VOL_MKDIR
: VOL_MKFILE
,
5499 (dcp
->c_cnid
== kHFSRootFolderID
));
5502 // have to end the transaction here before we call hfs_getnewvnode()
5503 // because that can cause us to try and reclaim a vnode on a different
5504 // file system which could cause us to start a transaction which can
5505 // deadlock with someone on that other file system (since we could be
5506 // holding two transaction locks as well as various vnodes and we did
5507 // not obtain the locks on them in the proper order).
5509 // NOTE: this means that if the quota check fails or we have to update
5510 // the change time on a block-special device that those changes
5511 // will happen as part of independent transactions.
5514 hfs_end_transaction(hfsmp
);
5520 * At this point, we must have encountered success with writing the EA.
5521 * Destroy our temporary cprotect (which had no keys).
5524 if ((attr
.ca_fileid
!= 0) && (protected_mount
) && (protectable_target
)) {
5525 cp_entry_destroy (entry
);
5530 /* Do not create vnode for whiteouts */
5531 if (S_ISWHT(mode
)) {
5535 gnv_flags
|= GNV_CREATE
;
5537 gnv_flags
|= GNV_NOCACHE
;
5541 * Create a vnode for the object just created.
5543 * NOTE: Maintaining the cnode lock on the parent directory is important,
5544 * as it prevents race conditions where other threads want to look up entries
5545 * in the directory and/or add things as we are in the process of creating
5546 * the vnode below. However, this has the potential for causing a
5547 * double lock panic when dealing with shadow files on a HFS boot partition.
5548 * The panic could occur if we are not cleaning up after ourselves properly
5549 * when done with a shadow file or in the error cases. The error would occur if we
5550 * try to create a new vnode, and then end up reclaiming another shadow vnode to
5551 * create the new one. However, if everything is working properly, this should
5552 * be a non-issue as we would never enter that reclaim codepath.
5554 * The cnode is locked on successful return.
5556 error
= hfs_getnewvnode(hfsmp
, dvp
, cnp
, &out_desc
, gnv_flags
, &attr
,
5557 NULL
, &tvp
, &newvnode_flags
);
5566 * Now that we have a vnode-in-hand, generate keys for this namespace item.
5567 * If we fail to create the keys, then attempt to delete the item from the
5568 * namespace. If we can't delete the item, that's not desirable but also not fatal..
5569 * All of the places which deal with restoring/unwrapping keys must also be
5570 * prepared to encounter an entry that does not have keys.
5572 if ((protectable_target
) && (protected_mount
)) {
5573 struct cprotect
*keyed_entry
= NULL
;
5575 if (cp
->c_cpentry
== NULL
) {
5576 panic ("hfs_makenode: no cpentry for cnode (%p)", cp
);
5579 error
= cp_generate_keys (hfsmp
, cp
, cp
->c_cpentry
->cp_pclass
, &keyed_entry
);
5582 * Upon success, the keys were generated and written out.
5583 * Update the cp pointer in the cnode.
5585 cp_replace_entry (cp
, keyed_entry
);
5587 cache_enter (dvp
, tvp
, cnp
);
5591 /* If key creation OR the setxattr failed, emit EPERM to userland */
5595 * Beware! This slightly violates the lock ordering for the
5596 * cnode/vnode 'tvp'. Ordinarily, you must acquire the truncate lock
5597 * which guards file size changes before acquiring the normal cnode lock
5598 * and calling hfs_removefile on an item.
5600 * However, in this case, we are still holding the directory lock so
5601 * 'tvp' is not lookup-able and it was a newly created vnode so it
5602 * cannot have any content yet. The only reason we are initiating
5603 * the removefile is because we could not generate content protection keys
5604 * for this namespace item. Note also that we pass a '1' in the allow_dirs
5605 * argument for hfs_removefile because we may be creating a directory here.
5607 * All this to say that while it is technically a violation it is
5608 * impossible to race with another thread for this cnode so it is safe.
5610 int err
= hfs_removefile (dvp
, tvp
, cnp
, 0, 0, 1, NULL
, 0);
5612 printf("hfs_makenode: removefile failed (%d) for CP entry %p\n", err
, tvp
);
5615 /* Release the cnode lock and mark the vnode for termination */
5617 err
= vnode_recycle (tvp
);
5619 printf("hfs_makenode: vnode_recycle failed (%d) for CP entry %p\n", err
, tvp
);
5622 /* Drop the iocount on the new vnode to force reclamation/recycling */
5632 * Once we create this vnode, we need to initialize its quota data
5633 * structures, if necessary. We know that it is OK to just go ahead and
5634 * initialize because we've already validated earlier (through the hfs_quotacheck
5635 * function) to see if creating this cnode/vnode would cause us to go over quota.
5637 if (hfsmp
->hfs_flags
& HFS_QUOTAS
) {
5639 /* cp could have been zeroed earlier */
5640 (void) hfs_getinoquota(cp
);
5646 cat_releasedesc(&out_desc
);
5650 * We may have jumped here in error-handling various situations above.
5651 * If we haven't already dumped the temporary CP used to initialize
5652 * the file atomically, then free it now. cp_entry_destroy should null
5653 * out the pointer if it was called already.
5656 cp_entry_destroy (entry
);
5662 * Make sure we release cnode lock on dcp.
5665 dcp
->c_flag
&= ~C_DIR_MODIFICATION
;
5666 wakeup((caddr_t
)&dcp
->c_flag
);
5670 if (error
== 0 && cp
!= NULL
) {
5674 hfs_end_transaction(hfsmp
);
5683 * hfs_vgetrsrc acquires a resource fork vnode corresponding to the cnode that is
5684 * found in 'vp'. The rsrc fork vnode is returned with the cnode locked and iocount
5685 * on the rsrc vnode.
5687 * *rvpp is an output argument for returning the pointer to the resource fork vnode.
5688 * In most cases, the resource fork vnode will not be set if we return an error.
5689 * However, if error_on_unlinked is set, we may have already acquired the resource fork vnode
5690 * before we discover the error (the file has gone open-unlinked). In this case only,
5691 * we may return a vnode in the output argument despite an error.
5693 * If can_drop_lock is set, then it is safe for this function to temporarily drop
5694 * and then re-acquire the cnode lock. We may need to do this, for example, in order to
5695 * acquire an iocount or promote our lock.
5697 * error_on_unlinked is an argument which indicates that we are to return an error if we
5698 * discover that the cnode has gone into an open-unlinked state ( C_DELETED or C_NOEXISTS)
5699 * is set in the cnode flags. This is only necessary if can_drop_lock is true, otherwise
5700 * there's really no reason to double-check for errors on the cnode.
5704 hfs_vgetrsrc(struct hfsmount
*hfsmp
, struct vnode
*vp
, struct vnode
**rvpp
,
5705 int can_drop_lock
, int error_on_unlinked
)
5708 struct vnode
*dvp
= NULLVP
;
5709 struct cnode
*cp
= VTOC(vp
);
5712 int delete_status
= 0;
5714 if (vnode_vtype(vp
) == VDIR
) {
5719 * Need to check the status of the cnode to validate it hasn't gone
5720 * open-unlinked on us before we can actually do work with it.
5722 delete_status
= hfs_checkdeleted(cp
);
5723 if ((delete_status
) && (error_on_unlinked
)) {
5724 return delete_status
;
5728 /* Attempt to use existing vnode */
5729 if ((rvp
= cp
->c_rsrc_vp
)) {
5730 vid
= vnode_vid(rvp
);
5733 * It is not safe to hold the cnode lock when calling vnode_getwithvid()
5734 * for the alternate fork -- vnode_getwithvid() could deadlock waiting
5735 * for a VL_WANTTERM while another thread has an iocount on the alternate
5736 * fork vnode and is attempting to acquire the common cnode lock.
5738 * But it's also not safe to drop the cnode lock when we're holding
5739 * multiple cnode locks, like during a hfs_removefile() operation
5740 * since we could lock out of order when re-acquiring the cnode lock.
5742 * So we can only drop the lock here if its safe to drop it -- which is
5743 * most of the time with the exception being hfs_removefile().
5748 error
= vnode_getwithvid(rvp
, vid
);
5750 if (can_drop_lock
) {
5751 (void) hfs_lock(cp
, HFS_EXCLUSIVE_LOCK
, HFS_LOCK_ALLOW_NOEXISTS
);
5754 * When we relinquished our cnode lock, the cnode could have raced
5755 * with a delete and gotten deleted. If the caller did not want
5756 * us to ignore open-unlinked files, then re-check the C_DELETED
5757 * state and see if we need to return an ENOENT here because the item
5758 * got deleted in the intervening time.
5760 if (error_on_unlinked
) {
5761 if ((delete_status
= hfs_checkdeleted(cp
))) {
5763 * If error == 0, this means that we succeeded in acquiring an iocount on the
5764 * rsrc fork vnode. However, if we're in this block of code, that means that we noticed
5765 * that the cnode has gone open-unlinked. In this case, the caller requested that we
5766 * not do any other work and return an errno. The caller will be responsible for
5767 * dropping the iocount we just acquired because we can't do it until we've released
5773 return delete_status
;
5778 * When our lock was relinquished, the resource fork
5779 * could have been recycled. Check for this and try
5782 if (error
== ENOENT
)
5786 const char * name
= (const char *)VTOC(vp
)->c_desc
.cd_nameptr
;
5789 printf("hfs_vgetrsrc: couldn't get resource"
5790 " fork for %s, vol=%s, err=%d\n", name
, hfsmp
->vcbVN
, error
);
5794 struct cat_fork rsrcfork
;
5795 struct componentname cn
;
5796 struct cat_desc
*descptr
= NULL
;
5797 struct cat_desc to_desc
;
5800 int newvnode_flags
= 0;
5803 * Make sure cnode lock is exclusive, if not upgrade it.
5805 * We assume that we were called from a read-only VNOP (getattr)
5806 * and that its safe to have the cnode lock dropped and reacquired.
5808 if (cp
->c_lockowner
!= current_thread()) {
5809 if (!can_drop_lock
) {
5813 * If the upgrade fails we lose the lock and
5814 * have to take the exclusive lock on our own.
5816 if (lck_rw_lock_shared_to_exclusive(&cp
->c_rwlock
) == FALSE
)
5817 lck_rw_lock_exclusive(&cp
->c_rwlock
);
5818 cp
->c_lockowner
= current_thread();
5822 * hfs_vgetsrc may be invoked for a cnode that has already been marked
5823 * C_DELETED. This is because we need to continue to provide rsrc
5824 * fork access to open-unlinked files. In this case, build a fake descriptor
5825 * like in hfs_removefile. If we don't do this, buildkey will fail in
5826 * cat_lookup because this cnode has no name in its descriptor. However,
5827 * only do this if the caller did not specify that they wanted us to
5828 * error out upon encountering open-unlinked files.
5831 if ((error_on_unlinked
) && (can_drop_lock
)) {
5832 if ((error
= hfs_checkdeleted(cp
))) {
5837 if ((cp
->c_flag
& C_DELETED
) && (cp
->c_desc
.cd_namelen
== 0)) {
5838 bzero (&to_desc
, sizeof(to_desc
));
5839 bzero (delname
, 32);
5840 MAKE_DELETED_NAME(delname
, sizeof(delname
), cp
->c_fileid
);
5841 to_desc
.cd_nameptr
= (const u_int8_t
*) delname
;
5842 to_desc
.cd_namelen
= strlen(delname
);
5843 to_desc
.cd_parentcnid
= hfsmp
->hfs_private_desc
[FILE_HARDLINKS
].cd_cnid
;
5844 to_desc
.cd_flags
= 0;
5845 to_desc
.cd_cnid
= cp
->c_cnid
;
5850 descptr
= &cp
->c_desc
;
5854 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
5857 * We call cat_idlookup (instead of cat_lookup) below because we can't
5858 * trust the descriptor in the provided cnode for lookups at this point.
5859 * Between the time of the original lookup of this vnode and now, the
5860 * descriptor could have gotten swapped or replaced. If this occurred,
5861 * the parent/name combo originally desired may not necessarily be provided
5862 * if we use the descriptor. Even worse, if the vnode represents
5863 * a hardlink, we could have removed one of the links from the namespace
5864 * but left the descriptor alone, since hfs_unlink does not invalidate
5865 * the descriptor in the cnode if other links still point to the inode.
5867 * Consider the following (slightly contrived) scenario:
5868 * /tmp/a <--> /tmp/b (hardlinks).
5869 * 1. Thread A: open rsrc fork on /tmp/b.
5870 * 1a. Thread A: does lookup, goes out to lunch right before calling getnamedstream.
5871 * 2. Thread B does 'mv /foo/b /tmp/b'
5872 * 2. Thread B succeeds.
5873 * 3. Thread A comes back and wants rsrc fork info for /tmp/b.
5875 * Even though the hardlink backing /tmp/b is now eliminated, the descriptor
5876 * is not removed/updated during the unlink process. So, if you were to
5877 * do a lookup on /tmp/b, you'd acquire an entirely different record's resource
5880 * As a result, we use the fileid, which should be invariant for the lifetime
5881 * of the cnode (possibly barring calls to exchangedata).
5883 * Addendum: We can't do the above for HFS standard since we aren't guaranteed to
5884 * have thread records for files. They were only required for directories. So
5885 * we need to do the lookup with the catalog name. This is OK since hardlinks were
5886 * never allowed on HFS standard.
5889 /* Get resource fork data */
5890 if ((hfsmp
->hfs_flags
& HFS_STANDARD
) == 0) {
5891 error
= cat_idlookup (hfsmp
, cp
->c_fileid
, 0, 1, NULL
, NULL
, &rsrcfork
);
5896 * HFS standard only:
5898 * Get the resource fork for this item with a cat_lookup call, but do not
5899 * force a case lookup since HFS standard is case-insensitive only. We
5900 * don't want the descriptor; just the fork data here. If we tried to
5901 * do a ID lookup (via thread record -> catalog record), then we might fail
5902 * prematurely since, as noted above, thread records were not strictly required
5905 error
= cat_lookup (hfsmp
, descptr
, 1, 0, (struct cat_desc
*)NULL
,
5906 (struct cat_attr
*)NULL
, &rsrcfork
, NULL
);
5910 hfs_systemfile_unlock(hfsmp
, lockflags
);
5915 * Supply hfs_getnewvnode with a component name.
5918 if (descptr
->cd_nameptr
) {
5919 MALLOC_ZONE(cn
.cn_pnbuf
, caddr_t
, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
5920 cn
.cn_nameiop
= LOOKUP
;
5921 cn
.cn_flags
= ISLASTCN
| HASBUF
;
5922 cn
.cn_context
= NULL
;
5923 cn
.cn_pnlen
= MAXPATHLEN
;
5924 cn
.cn_nameptr
= cn
.cn_pnbuf
;
5927 cn
.cn_namelen
= snprintf(cn
.cn_nameptr
, MAXPATHLEN
,
5928 "%s%s", descptr
->cd_nameptr
,
5929 _PATH_RSRCFORKSPEC
);
5931 dvp
= vnode_getparent(vp
);
5932 error
= hfs_getnewvnode(hfsmp
, dvp
, cn
.cn_pnbuf
? &cn
: NULL
,
5933 descptr
, GNV_WANTRSRC
| GNV_SKIPLOCK
, &cp
->c_attr
,
5934 &rsrcfork
, &rvp
, &newvnode_flags
);
5938 FREE_ZONE(cn
.cn_pnbuf
, cn
.cn_pnlen
, M_NAMEI
);
5948 * Wrapper for special device reads
5952 struct vnop_read_args
/* {
5956 vfs_context_t a_context;
5962 VTOC(ap
->a_vp
)->c_touch_acctime
= TRUE
;
5963 return (VOCALL (spec_vnodeop_p
, VOFFSET(vnop_read
), ap
));
5967 * Wrapper for special device writes
5971 struct vnop_write_args
/* {
5975 vfs_context_t a_context;
5979 * Set update and change flags.
5981 VTOC(ap
->a_vp
)->c_touch_chgtime
= TRUE
;
5982 VTOC(ap
->a_vp
)->c_touch_modtime
= TRUE
;
5983 return (VOCALL (spec_vnodeop_p
, VOFFSET(vnop_write
), ap
));
5987 * Wrapper for special device close
5989 * Update the times on the cnode then do device close.
5993 struct vnop_close_args
/* {
5996 vfs_context_t a_context;
5999 struct vnode
*vp
= ap
->a_vp
;
6002 if (vnode_isinuse(ap
->a_vp
, 0)) {
6003 if (hfs_lock(VTOC(vp
), HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
) == 0) {
6005 hfs_touchtimes(VTOHFS(vp
), cp
);
6009 return (VOCALL (spec_vnodeop_p
, VOFFSET(vnop_close
), ap
));
6014 * Wrapper for fifo reads
6018 struct vnop_read_args
/* {
6022 vfs_context_t a_context;
6028 VTOC(ap
->a_vp
)->c_touch_acctime
= TRUE
;
6029 return (VOCALL (fifo_vnodeop_p
, VOFFSET(vnop_read
), ap
));
6033 * Wrapper for fifo writes
6037 struct vnop_write_args
/* {
6041 vfs_context_t a_context;
6045 * Set update and change flags.
6047 VTOC(ap
->a_vp
)->c_touch_chgtime
= TRUE
;
6048 VTOC(ap
->a_vp
)->c_touch_modtime
= TRUE
;
6049 return (VOCALL (fifo_vnodeop_p
, VOFFSET(vnop_write
), ap
));
6053 * Wrapper for fifo close
6055 * Update the times on the cnode then do device close.
6059 struct vnop_close_args
/* {
6062 vfs_context_t a_context;
6065 struct vnode
*vp
= ap
->a_vp
;
6068 if (vnode_isinuse(ap
->a_vp
, 1)) {
6069 if (hfs_lock(VTOC(vp
), HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
) == 0) {
6071 hfs_touchtimes(VTOHFS(vp
), cp
);
6075 return (VOCALL (fifo_vnodeop_p
, VOFFSET(vnop_close
), ap
));
6082 * Synchronize a file's in-core state with that on disk.
6086 struct vnop_fsync_args
/* {
6089 vfs_context_t a_context;
6092 struct vnode
* vp
= ap
->a_vp
;
6095 /* Note: We check hfs flags instead of vfs mount flag because during
6096 * read-write update, hfs marks itself read-write much earlier than
6097 * the vfs, and hence won't result in skipping of certain writes like
6098 * zero'ing out of unused nodes, creation of hotfiles btree, etc.
6100 if (VTOHFS(vp
)->hfs_flags
& HFS_READ_ONLY
) {
6105 if ((error
= cp_handle_vnop(vp
, CP_WRITE_ACCESS
, 0)) != 0) {
6108 #endif /* CONFIG_PROTECT */
6111 * We need to allow ENOENT lock errors since unlink
6112 * systenm call can call VNOP_FSYNC during vclean.
6114 error
= hfs_lock(VTOC(vp
), HFS_EXCLUSIVE_LOCK
, HFS_LOCK_DEFAULT
);
6118 error
= hfs_fsync(vp
, ap
->a_waitfor
, 0, vfs_context_proc(ap
->a_context
));
6120 hfs_unlock(VTOC(vp
));
6126 hfs_vnop_whiteout(ap
)
6127 struct vnop_whiteout_args
/* {
6128 struct vnode *a_dvp;
6129 struct componentname *a_cnp;
6131 vfs_context_t a_context;
6135 struct vnode
*vp
= NULL
;
6136 struct vnode_attr va
;
6137 struct vnop_lookup_args lookup_args
;
6138 struct vnop_remove_args remove_args
;
6139 struct hfsmount
*hfsmp
;
6141 hfsmp
= VTOHFS(ap
->a_dvp
);
6142 if (hfsmp
->hfs_flags
& HFS_STANDARD
) {
6147 switch (ap
->a_flags
) {
6154 VATTR_SET(&va
, va_type
, VREG
);
6155 VATTR_SET(&va
, va_mode
, S_IFWHT
);
6156 VATTR_SET(&va
, va_uid
, 0);
6157 VATTR_SET(&va
, va_gid
, 0);
6159 error
= hfs_makenode(ap
->a_dvp
, &vp
, ap
->a_cnp
, &va
, ap
->a_context
);
6160 /* No need to release the vnode as no vnode is created for whiteouts */
6164 lookup_args
.a_dvp
= ap
->a_dvp
;
6165 lookup_args
.a_vpp
= &vp
;
6166 lookup_args
.a_cnp
= ap
->a_cnp
;
6167 lookup_args
.a_context
= ap
->a_context
;
6169 error
= hfs_vnop_lookup(&lookup_args
);
6174 remove_args
.a_dvp
= ap
->a_dvp
;
6175 remove_args
.a_vp
= vp
;
6176 remove_args
.a_cnp
= ap
->a_cnp
;
6177 remove_args
.a_flags
= 0;
6178 remove_args
.a_context
= ap
->a_context
;
6180 error
= hfs_vnop_remove(&remove_args
);
6185 panic("hfs_vnop_whiteout: unknown operation (flag = %x)\n", ap
->a_flags
);
6192 int (**hfs_vnodeop_p
)(void *);
6194 #define VOPFUNC int (*)(void *)
6198 int (**hfs_std_vnodeop_p
) (void *);
6199 static int hfs_readonly_op (__unused
void* ap
) { return (EROFS
); }
6202 * In 10.6 and forward, HFS Standard is read-only and deprecated. The vnop table below
6203 * is for use with HFS standard to block out operations that would modify the file system
6206 struct vnodeopv_entry_desc hfs_standard_vnodeop_entries
[] = {
6207 { &vnop_default_desc
, (VOPFUNC
)vn_default_error
},
6208 { &vnop_lookup_desc
, (VOPFUNC
)hfs_vnop_lookup
}, /* lookup */
6209 { &vnop_create_desc
, (VOPFUNC
)hfs_readonly_op
}, /* create (READONLY) */
6210 { &vnop_mknod_desc
, (VOPFUNC
)hfs_readonly_op
}, /* mknod (READONLY) */
6211 { &vnop_open_desc
, (VOPFUNC
)hfs_vnop_open
}, /* open */
6212 { &vnop_close_desc
, (VOPFUNC
)hfs_vnop_close
}, /* close */
6213 { &vnop_getattr_desc
, (VOPFUNC
)hfs_vnop_getattr
}, /* getattr */
6214 { &vnop_setattr_desc
, (VOPFUNC
)hfs_readonly_op
}, /* setattr */
6215 { &vnop_read_desc
, (VOPFUNC
)hfs_vnop_read
}, /* read */
6216 { &vnop_write_desc
, (VOPFUNC
)hfs_readonly_op
}, /* write (READONLY) */
6217 { &vnop_ioctl_desc
, (VOPFUNC
)hfs_vnop_ioctl
}, /* ioctl */
6218 { &vnop_select_desc
, (VOPFUNC
)hfs_vnop_select
}, /* select */
6219 { &vnop_revoke_desc
, (VOPFUNC
)nop_revoke
}, /* revoke */
6220 { &vnop_exchange_desc
, (VOPFUNC
)hfs_readonly_op
}, /* exchange (READONLY)*/
6221 { &vnop_mmap_desc
, (VOPFUNC
)err_mmap
}, /* mmap */
6222 { &vnop_fsync_desc
, (VOPFUNC
)hfs_readonly_op
}, /* fsync (READONLY) */
6223 { &vnop_remove_desc
, (VOPFUNC
)hfs_readonly_op
}, /* remove (READONLY) */
6224 { &vnop_link_desc
, (VOPFUNC
)hfs_readonly_op
}, /* link ( READONLLY) */
6225 { &vnop_rename_desc
, (VOPFUNC
)hfs_readonly_op
}, /* rename (READONLY)*/
6226 { &vnop_mkdir_desc
, (VOPFUNC
)hfs_readonly_op
}, /* mkdir (READONLY) */
6227 { &vnop_rmdir_desc
, (VOPFUNC
)hfs_readonly_op
}, /* rmdir (READONLY) */
6228 { &vnop_symlink_desc
, (VOPFUNC
)hfs_readonly_op
}, /* symlink (READONLY) */
6229 { &vnop_readdir_desc
, (VOPFUNC
)hfs_vnop_readdir
}, /* readdir */
6230 { &vnop_readdirattr_desc
, (VOPFUNC
)hfs_vnop_readdirattr
}, /* readdirattr */
6231 { &vnop_readlink_desc
, (VOPFUNC
)hfs_vnop_readlink
}, /* readlink */
6232 { &vnop_inactive_desc
, (VOPFUNC
)hfs_vnop_inactive
}, /* inactive */
6233 { &vnop_reclaim_desc
, (VOPFUNC
)hfs_vnop_reclaim
}, /* reclaim */
6234 { &vnop_strategy_desc
, (VOPFUNC
)hfs_vnop_strategy
}, /* strategy */
6235 { &vnop_pathconf_desc
, (VOPFUNC
)hfs_vnop_pathconf
}, /* pathconf */
6236 { &vnop_advlock_desc
, (VOPFUNC
)err_advlock
}, /* advlock */
6237 { &vnop_allocate_desc
, (VOPFUNC
)hfs_readonly_op
}, /* allocate (READONLY) */
6239 { &vnop_searchfs_desc
, (VOPFUNC
)hfs_vnop_search
}, /* search fs */
6241 { &vnop_searchfs_desc
, (VOPFUNC
)err_searchfs
}, /* search fs */
6243 { &vnop_bwrite_desc
, (VOPFUNC
)hfs_readonly_op
}, /* bwrite (READONLY) */
6244 { &vnop_pagein_desc
, (VOPFUNC
)hfs_vnop_pagein
}, /* pagein */
6245 { &vnop_pageout_desc
,(VOPFUNC
) hfs_readonly_op
}, /* pageout (READONLY) */
6246 { &vnop_copyfile_desc
, (VOPFUNC
)hfs_readonly_op
}, /* copyfile (READONLY)*/
6247 { &vnop_blktooff_desc
, (VOPFUNC
)hfs_vnop_blktooff
}, /* blktooff */
6248 { &vnop_offtoblk_desc
, (VOPFUNC
)hfs_vnop_offtoblk
}, /* offtoblk */
6249 { &vnop_blockmap_desc
, (VOPFUNC
)hfs_vnop_blockmap
}, /* blockmap */
6250 { &vnop_getxattr_desc
, (VOPFUNC
)hfs_vnop_getxattr
},
6251 { &vnop_setxattr_desc
, (VOPFUNC
)hfs_readonly_op
}, /* set xattr (READONLY) */
6252 { &vnop_removexattr_desc
, (VOPFUNC
)hfs_readonly_op
}, /* remove xattr (READONLY) */
6253 { &vnop_listxattr_desc
, (VOPFUNC
)hfs_vnop_listxattr
},
6254 { &vnop_whiteout_desc
, (VOPFUNC
)hfs_readonly_op
}, /* whiteout (READONLY) */
6256 { &vnop_getnamedstream_desc
, (VOPFUNC
)hfs_vnop_getnamedstream
},
6257 { &vnop_makenamedstream_desc
, (VOPFUNC
)hfs_readonly_op
},
6258 { &vnop_removenamedstream_desc
, (VOPFUNC
)hfs_readonly_op
},
6260 { NULL
, (VOPFUNC
)NULL
}
6263 struct vnodeopv_desc hfs_std_vnodeop_opv_desc
=
6264 { &hfs_std_vnodeop_p
, hfs_standard_vnodeop_entries
};
6267 /* VNOP table for HFS+ */
6268 struct vnodeopv_entry_desc hfs_vnodeop_entries
[] = {
6269 { &vnop_default_desc
, (VOPFUNC
)vn_default_error
},
6270 { &vnop_lookup_desc
, (VOPFUNC
)hfs_vnop_lookup
}, /* lookup */
6271 { &vnop_create_desc
, (VOPFUNC
)hfs_vnop_create
}, /* create */
6272 { &vnop_mknod_desc
, (VOPFUNC
)hfs_vnop_mknod
}, /* mknod */
6273 { &vnop_open_desc
, (VOPFUNC
)hfs_vnop_open
}, /* open */
6274 { &vnop_close_desc
, (VOPFUNC
)hfs_vnop_close
}, /* close */
6275 { &vnop_getattr_desc
, (VOPFUNC
)hfs_vnop_getattr
}, /* getattr */
6276 { &vnop_setattr_desc
, (VOPFUNC
)hfs_vnop_setattr
}, /* setattr */
6277 { &vnop_read_desc
, (VOPFUNC
)hfs_vnop_read
}, /* read */
6278 { &vnop_write_desc
, (VOPFUNC
)hfs_vnop_write
}, /* write */
6279 { &vnop_ioctl_desc
, (VOPFUNC
)hfs_vnop_ioctl
}, /* ioctl */
6280 { &vnop_select_desc
, (VOPFUNC
)hfs_vnop_select
}, /* select */
6281 { &vnop_revoke_desc
, (VOPFUNC
)nop_revoke
}, /* revoke */
6282 { &vnop_exchange_desc
, (VOPFUNC
)hfs_vnop_exchange
}, /* exchange */
6283 { &vnop_mmap_desc
, (VOPFUNC
)hfs_vnop_mmap
}, /* mmap */
6284 { &vnop_fsync_desc
, (VOPFUNC
)hfs_vnop_fsync
}, /* fsync */
6285 { &vnop_remove_desc
, (VOPFUNC
)hfs_vnop_remove
}, /* remove */
6286 { &vnop_link_desc
, (VOPFUNC
)hfs_vnop_link
}, /* link */
6287 { &vnop_rename_desc
, (VOPFUNC
)hfs_vnop_rename
}, /* rename */
6288 { &vnop_mkdir_desc
, (VOPFUNC
)hfs_vnop_mkdir
}, /* mkdir */
6289 { &vnop_rmdir_desc
, (VOPFUNC
)hfs_vnop_rmdir
}, /* rmdir */
6290 { &vnop_symlink_desc
, (VOPFUNC
)hfs_vnop_symlink
}, /* symlink */
6291 { &vnop_readdir_desc
, (VOPFUNC
)hfs_vnop_readdir
}, /* readdir */
6292 { &vnop_readdirattr_desc
, (VOPFUNC
)hfs_vnop_readdirattr
}, /* readdirattr */
6293 { &vnop_readlink_desc
, (VOPFUNC
)hfs_vnop_readlink
}, /* readlink */
6294 { &vnop_inactive_desc
, (VOPFUNC
)hfs_vnop_inactive
}, /* inactive */
6295 { &vnop_reclaim_desc
, (VOPFUNC
)hfs_vnop_reclaim
}, /* reclaim */
6296 { &vnop_strategy_desc
, (VOPFUNC
)hfs_vnop_strategy
}, /* strategy */
6297 { &vnop_pathconf_desc
, (VOPFUNC
)hfs_vnop_pathconf
}, /* pathconf */
6298 { &vnop_advlock_desc
, (VOPFUNC
)err_advlock
}, /* advlock */
6299 { &vnop_allocate_desc
, (VOPFUNC
)hfs_vnop_allocate
}, /* allocate */
6301 { &vnop_searchfs_desc
, (VOPFUNC
)hfs_vnop_search
}, /* search fs */
6303 { &vnop_searchfs_desc
, (VOPFUNC
)err_searchfs
}, /* search fs */
6305 { &vnop_bwrite_desc
, (VOPFUNC
)hfs_vnop_bwrite
}, /* bwrite */
6306 { &vnop_pagein_desc
, (VOPFUNC
)hfs_vnop_pagein
}, /* pagein */
6307 { &vnop_pageout_desc
,(VOPFUNC
) hfs_vnop_pageout
}, /* pageout */
6308 { &vnop_copyfile_desc
, (VOPFUNC
)err_copyfile
}, /* copyfile */
6309 { &vnop_blktooff_desc
, (VOPFUNC
)hfs_vnop_blktooff
}, /* blktooff */
6310 { &vnop_offtoblk_desc
, (VOPFUNC
)hfs_vnop_offtoblk
}, /* offtoblk */
6311 { &vnop_blockmap_desc
, (VOPFUNC
)hfs_vnop_blockmap
}, /* blockmap */
6312 { &vnop_getxattr_desc
, (VOPFUNC
)hfs_vnop_getxattr
},
6313 { &vnop_setxattr_desc
, (VOPFUNC
)hfs_vnop_setxattr
},
6314 { &vnop_removexattr_desc
, (VOPFUNC
)hfs_vnop_removexattr
},
6315 { &vnop_listxattr_desc
, (VOPFUNC
)hfs_vnop_listxattr
},
6316 { &vnop_whiteout_desc
, (VOPFUNC
)hfs_vnop_whiteout
},
6318 { &vnop_getnamedstream_desc
, (VOPFUNC
)hfs_vnop_getnamedstream
},
6319 { &vnop_makenamedstream_desc
, (VOPFUNC
)hfs_vnop_makenamedstream
},
6320 { &vnop_removenamedstream_desc
, (VOPFUNC
)hfs_vnop_removenamedstream
},
6322 { NULL
, (VOPFUNC
)NULL
}
6325 struct vnodeopv_desc hfs_vnodeop_opv_desc
=
6326 { &hfs_vnodeop_p
, hfs_vnodeop_entries
};
6329 /* Spec Op vnop table for HFS+ */
6330 int (**hfs_specop_p
)(void *);
6331 struct vnodeopv_entry_desc hfs_specop_entries
[] = {
6332 { &vnop_default_desc
, (VOPFUNC
)vn_default_error
},
6333 { &vnop_lookup_desc
, (VOPFUNC
)spec_lookup
}, /* lookup */
6334 { &vnop_create_desc
, (VOPFUNC
)spec_create
}, /* create */
6335 { &vnop_mknod_desc
, (VOPFUNC
)spec_mknod
}, /* mknod */
6336 { &vnop_open_desc
, (VOPFUNC
)spec_open
}, /* open */
6337 { &vnop_close_desc
, (VOPFUNC
)hfsspec_close
}, /* close */
6338 { &vnop_getattr_desc
, (VOPFUNC
)hfs_vnop_getattr
}, /* getattr */
6339 { &vnop_setattr_desc
, (VOPFUNC
)hfs_vnop_setattr
}, /* setattr */
6340 { &vnop_read_desc
, (VOPFUNC
)hfsspec_read
}, /* read */
6341 { &vnop_write_desc
, (VOPFUNC
)hfsspec_write
}, /* write */
6342 { &vnop_ioctl_desc
, (VOPFUNC
)spec_ioctl
}, /* ioctl */
6343 { &vnop_select_desc
, (VOPFUNC
)spec_select
}, /* select */
6344 { &vnop_revoke_desc
, (VOPFUNC
)spec_revoke
}, /* revoke */
6345 { &vnop_mmap_desc
, (VOPFUNC
)spec_mmap
}, /* mmap */
6346 { &vnop_fsync_desc
, (VOPFUNC
)hfs_vnop_fsync
}, /* fsync */
6347 { &vnop_remove_desc
, (VOPFUNC
)spec_remove
}, /* remove */
6348 { &vnop_link_desc
, (VOPFUNC
)spec_link
}, /* link */
6349 { &vnop_rename_desc
, (VOPFUNC
)spec_rename
}, /* rename */
6350 { &vnop_mkdir_desc
, (VOPFUNC
)spec_mkdir
}, /* mkdir */
6351 { &vnop_rmdir_desc
, (VOPFUNC
)spec_rmdir
}, /* rmdir */
6352 { &vnop_symlink_desc
, (VOPFUNC
)spec_symlink
}, /* symlink */
6353 { &vnop_readdir_desc
, (VOPFUNC
)spec_readdir
}, /* readdir */
6354 { &vnop_readlink_desc
, (VOPFUNC
)spec_readlink
}, /* readlink */
6355 { &vnop_inactive_desc
, (VOPFUNC
)hfs_vnop_inactive
}, /* inactive */
6356 { &vnop_reclaim_desc
, (VOPFUNC
)hfs_vnop_reclaim
}, /* reclaim */
6357 { &vnop_strategy_desc
, (VOPFUNC
)spec_strategy
}, /* strategy */
6358 { &vnop_pathconf_desc
, (VOPFUNC
)spec_pathconf
}, /* pathconf */
6359 { &vnop_advlock_desc
, (VOPFUNC
)err_advlock
}, /* advlock */
6360 { &vnop_bwrite_desc
, (VOPFUNC
)hfs_vnop_bwrite
},
6361 { &vnop_pagein_desc
, (VOPFUNC
)hfs_vnop_pagein
}, /* Pagein */
6362 { &vnop_pageout_desc
, (VOPFUNC
)hfs_vnop_pageout
}, /* Pageout */
6363 { &vnop_copyfile_desc
, (VOPFUNC
)err_copyfile
}, /* copyfile */
6364 { &vnop_blktooff_desc
, (VOPFUNC
)hfs_vnop_blktooff
}, /* blktooff */
6365 { &vnop_offtoblk_desc
, (VOPFUNC
)hfs_vnop_offtoblk
}, /* offtoblk */
6366 { &vnop_getxattr_desc
, (VOPFUNC
)hfs_vnop_getxattr
},
6367 { &vnop_setxattr_desc
, (VOPFUNC
)hfs_vnop_setxattr
},
6368 { &vnop_removexattr_desc
, (VOPFUNC
)hfs_vnop_removexattr
},
6369 { &vnop_listxattr_desc
, (VOPFUNC
)hfs_vnop_listxattr
},
6370 { (struct vnodeop_desc
*)NULL
, (VOPFUNC
)NULL
}
6372 struct vnodeopv_desc hfs_specop_opv_desc
=
6373 { &hfs_specop_p
, hfs_specop_entries
};
6376 /* HFS+ FIFO VNOP table */
6377 int (**hfs_fifoop_p
)(void *);
6378 struct vnodeopv_entry_desc hfs_fifoop_entries
[] = {
6379 { &vnop_default_desc
, (VOPFUNC
)vn_default_error
},
6380 { &vnop_lookup_desc
, (VOPFUNC
)fifo_lookup
}, /* lookup */
6381 { &vnop_create_desc
, (VOPFUNC
)fifo_create
}, /* create */
6382 { &vnop_mknod_desc
, (VOPFUNC
)fifo_mknod
}, /* mknod */
6383 { &vnop_open_desc
, (VOPFUNC
)fifo_open
}, /* open */
6384 { &vnop_close_desc
, (VOPFUNC
)hfsfifo_close
}, /* close */
6385 { &vnop_getattr_desc
, (VOPFUNC
)hfs_vnop_getattr
}, /* getattr */
6386 { &vnop_setattr_desc
, (VOPFUNC
)hfs_vnop_setattr
}, /* setattr */
6387 { &vnop_read_desc
, (VOPFUNC
)hfsfifo_read
}, /* read */
6388 { &vnop_write_desc
, (VOPFUNC
)hfsfifo_write
}, /* write */
6389 { &vnop_ioctl_desc
, (VOPFUNC
)fifo_ioctl
}, /* ioctl */
6390 { &vnop_select_desc
, (VOPFUNC
)fifo_select
}, /* select */
6391 { &vnop_revoke_desc
, (VOPFUNC
)fifo_revoke
}, /* revoke */
6392 { &vnop_mmap_desc
, (VOPFUNC
)fifo_mmap
}, /* mmap */
6393 { &vnop_fsync_desc
, (VOPFUNC
)hfs_vnop_fsync
}, /* fsync */
6394 { &vnop_remove_desc
, (VOPFUNC
)fifo_remove
}, /* remove */
6395 { &vnop_link_desc
, (VOPFUNC
)fifo_link
}, /* link */
6396 { &vnop_rename_desc
, (VOPFUNC
)fifo_rename
}, /* rename */
6397 { &vnop_mkdir_desc
, (VOPFUNC
)fifo_mkdir
}, /* mkdir */
6398 { &vnop_rmdir_desc
, (VOPFUNC
)fifo_rmdir
}, /* rmdir */
6399 { &vnop_symlink_desc
, (VOPFUNC
)fifo_symlink
}, /* symlink */
6400 { &vnop_readdir_desc
, (VOPFUNC
)fifo_readdir
}, /* readdir */
6401 { &vnop_readlink_desc
, (VOPFUNC
)fifo_readlink
}, /* readlink */
6402 { &vnop_inactive_desc
, (VOPFUNC
)hfs_vnop_inactive
}, /* inactive */
6403 { &vnop_reclaim_desc
, (VOPFUNC
)hfs_vnop_reclaim
}, /* reclaim */
6404 { &vnop_strategy_desc
, (VOPFUNC
)fifo_strategy
}, /* strategy */
6405 { &vnop_pathconf_desc
, (VOPFUNC
)fifo_pathconf
}, /* pathconf */
6406 { &vnop_advlock_desc
, (VOPFUNC
)err_advlock
}, /* advlock */
6407 { &vnop_bwrite_desc
, (VOPFUNC
)hfs_vnop_bwrite
},
6408 { &vnop_pagein_desc
, (VOPFUNC
)hfs_vnop_pagein
}, /* Pagein */
6409 { &vnop_pageout_desc
, (VOPFUNC
)hfs_vnop_pageout
}, /* Pageout */
6410 { &vnop_copyfile_desc
, (VOPFUNC
)err_copyfile
}, /* copyfile */
6411 { &vnop_blktooff_desc
, (VOPFUNC
)hfs_vnop_blktooff
}, /* blktooff */
6412 { &vnop_offtoblk_desc
, (VOPFUNC
)hfs_vnop_offtoblk
}, /* offtoblk */
6413 { &vnop_blockmap_desc
, (VOPFUNC
)hfs_vnop_blockmap
}, /* blockmap */
6414 { &vnop_getxattr_desc
, (VOPFUNC
)hfs_vnop_getxattr
},
6415 { &vnop_setxattr_desc
, (VOPFUNC
)hfs_vnop_setxattr
},
6416 { &vnop_removexattr_desc
, (VOPFUNC
)hfs_vnop_removexattr
},
6417 { &vnop_listxattr_desc
, (VOPFUNC
)hfs_vnop_listxattr
},
6418 { (struct vnodeop_desc
*)NULL
, (VOPFUNC
)NULL
}
6420 struct vnodeopv_desc hfs_fifoop_opv_desc
=
6421 { &hfs_fifoop_p
, hfs_fifoop_entries
};