]> git.saurik.com Git - apple/xnu.git/blame - bsd/hfs/hfs_vnops.c
xnu-1504.15.3.tar.gz
[apple/xnu.git] / bsd / hfs / hfs_vnops.c
CommitLineData
9bccf70c 1/*
b0d623f7 2 * Copyright (c) 2000-2009 Apple Inc. All rights reserved.
5d5c5d0d 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
9bccf70c 5 *
2d21ac55
A
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.
8f6c56a5 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
9bccf70c
A
27 */
28
29#include <sys/systm.h>
cf7d32b8 30#include <sys/param.h>
b0d623f7 31#include <sys/kernel.h>
91447636 32#include <sys/file_internal.h>
9bccf70c
A
33#include <sys/dirent.h>
34#include <sys/stat.h>
35#include <sys/buf.h>
36#include <sys/mount.h>
2d21ac55 37#include <sys/vnode_if.h>
91447636 38#include <sys/vnode_internal.h>
9bccf70c 39#include <sys/malloc.h>
9bccf70c 40#include <sys/ubc.h>
2d21ac55 41#include <sys/ubc_internal.h>
91447636 42#include <sys/paths.h>
9bccf70c 43#include <sys/quota.h>
55e303ae
A
44#include <sys/time.h>
45#include <sys/disk.h>
91447636
A
46#include <sys/kauth.h>
47#include <sys/uio_internal.h>
9bccf70c
A
48
49#include <miscfs/specfs/specdev.h>
50#include <miscfs/fifofs/fifo.h>
51#include <vfs/vfs_support.h>
52#include <machine/spl.h>
53
54#include <sys/kdebug.h>
3a60a9f5 55#include <sys/sysctl.h>
9bccf70c
A
56
57#include "hfs.h"
58#include "hfs_catalog.h"
59#include "hfs_cnode.h"
9bccf70c
A
60#include "hfs_dbg.h"
61#include "hfs_mount.h"
62#include "hfs_quota.h"
63#include "hfs_endian.h"
64
65#include "hfscommon/headers/BTreesInternal.h"
66#include "hfscommon/headers/FileMgrInternal.h"
67
55e303ae 68#define KNDETACH_VNLOCKED 0x00000001
9bccf70c 69
9bccf70c
A
70/* Global vfs data structures for hfs */
71
3a60a9f5
A
72/* Always F_FULLFSYNC? 1=yes,0=no (default due to "various" reasons is 'no') */
73int always_do_fullfsync = 0;
b0d623f7
A
74SYSCTL_DECL(_vfs_generic);
75SYSCTL_INT (_vfs_generic, OID_AUTO, always_do_fullfsync, CTLFLAG_RW, &always_do_fullfsync, 0, "always F_FULLFSYNC when fsync is called");
9bccf70c 76
91447636
A
77static int hfs_makenode(struct vnode *dvp, struct vnode **vpp,
78 struct componentname *cnp, struct vnode_attr *vap,
79 vfs_context_t ctx);
9bccf70c 80
2d21ac55
A
81static int hfs_metasync(struct hfsmount *hfsmp, daddr64_t node, __unused struct proc *p);
82static int hfs_metasync_all(struct hfsmount *hfsmp);
9bccf70c 83
55e303ae
A
84static int hfs_removedir(struct vnode *, struct vnode *, struct componentname *,
85 int);
86
87static int hfs_removefile(struct vnode *, struct vnode *, struct componentname *,
b0d623f7 88 int, int, int, struct vnode *);
2d21ac55
A
89
90#if FIFO
91static int hfsfifo_read(struct vnop_read_args *);
92static int hfsfifo_write(struct vnop_write_args *);
93static int hfsfifo_close(struct vnop_close_args *);
2d21ac55
A
94
95extern int (**fifo_vnodeop_p)(void *);
96#endif /* FIFO */
91447636
A
97
98static int hfs_vnop_close(struct vnop_close_args*);
99static int hfs_vnop_create(struct vnop_create_args*);
100static int hfs_vnop_exchange(struct vnop_exchange_args*);
101static int hfs_vnop_fsync(struct vnop_fsync_args*);
102static int hfs_vnop_mkdir(struct vnop_mkdir_args*);
103static int hfs_vnop_mknod(struct vnop_mknod_args*);
104static int hfs_vnop_getattr(struct vnop_getattr_args*);
105static int hfs_vnop_open(struct vnop_open_args*);
106static int hfs_vnop_readdir(struct vnop_readdir_args*);
107static int hfs_vnop_remove(struct vnop_remove_args*);
108static int hfs_vnop_rename(struct vnop_rename_args*);
109static int hfs_vnop_rmdir(struct vnop_rmdir_args*);
110static int hfs_vnop_symlink(struct vnop_symlink_args*);
111static int hfs_vnop_setattr(struct vnop_setattr_args*);
2d21ac55
A
112static int hfs_vnop_readlink(struct vnop_readlink_args *);
113static int hfs_vnop_pathconf(struct vnop_pathconf_args *);
2d21ac55
A
114static int hfs_vnop_whiteout(struct vnop_whiteout_args *);
115static int hfsspec_read(struct vnop_read_args *);
116static int hfsspec_write(struct vnop_write_args *);
117static int hfsspec_close(struct vnop_close_args *);
55e303ae
A
118
119/* Options for hfs_removedir and hfs_removefile */
91447636 120#define HFSRM_SKIP_RESERVE 0x01
55e303ae
A
121
122
9bccf70c 123
9bccf70c
A
124
125/*****************************************************************************
126*
127* Common Operations on vnodes
128*
129*****************************************************************************/
130
131/*
91447636
A
132 * Create a regular file.
133 */
9bccf70c 134static int
91447636 135hfs_vnop_create(struct vnop_create_args *ap)
9bccf70c 136{
2d21ac55
A
137 int error;
138
139again:
140 error = hfs_makenode(ap->a_dvp, ap->a_vpp, ap->a_cnp, ap->a_vap, ap->a_context);
141
142 /*
143 * We speculatively skipped the original lookup of the leaf
144 * for CREATE. Since it exists, go get it as long as they
145 * didn't want an exclusive create.
146 */
147 if ((error == EEXIST) && !(ap->a_vap->va_vaflags & VA_EXCLUSIVE)) {
148 struct vnop_lookup_args args;
149
150 args.a_desc = &vnop_lookup_desc;
151 args.a_dvp = ap->a_dvp;
152 args.a_vpp = ap->a_vpp;
153 args.a_cnp = ap->a_cnp;
154 args.a_context = ap->a_context;
155 args.a_cnp->cn_nameiop = LOOKUP;
156 error = hfs_vnop_lookup(&args);
157 /*
158 * We can also race with remove for this file.
159 */
160 if (error == ENOENT) {
161 goto again;
162 }
163
164 /* Make sure it was file. */
165 if ((error == 0) && !vnode_isreg(*args.a_vpp)) {
166 vnode_put(*args.a_vpp);
167 error = EEXIST;
168 }
169 args.a_cnp->cn_nameiop = CREATE;
170 }
171 return (error);
9bccf70c
A
172}
173
9bccf70c 174/*
91447636
A
175 * Make device special file.
176 */
9bccf70c 177static int
91447636 178hfs_vnop_mknod(struct vnop_mknod_args *ap)
9bccf70c 179{
91447636
A
180 struct vnode_attr *vap = ap->a_vap;
181 struct vnode *dvp = ap->a_dvp;
9bccf70c
A
182 struct vnode **vpp = ap->a_vpp;
183 struct cnode *cp;
184 int error;
185
91447636
A
186 if (VTOVCB(dvp)->vcbSigWord != kHFSPlusSigWord) {
187 return (ENOTSUP);
9bccf70c
A
188 }
189
190 /* Create the vnode */
91447636 191 error = hfs_makenode(dvp, vpp, ap->a_cnp, vap, ap->a_context);
9bccf70c
A
192 if (error)
193 return (error);
91447636 194
9bccf70c 195 cp = VTOC(*vpp);
91447636
A
196 cp->c_touch_acctime = TRUE;
197 cp->c_touch_chgtime = TRUE;
198 cp->c_touch_modtime = TRUE;
199
9bccf70c
A
200 if ((vap->va_rdev != VNOVAL) &&
201 (vap->va_type == VBLK || vap->va_type == VCHR))
202 cp->c_rdev = vap->va_rdev;
91447636 203
9bccf70c
A
204 return (0);
205}
206
b0d623f7
A
207#if HFS_COMPRESSION
208/*
209 * hfs_ref_data_vp(): returns the data fork vnode for a given cnode.
210 * In the (hopefully rare) case where the data fork vnode is not
211 * present, it will use hfs_vget() to create a new vnode for the
212 * data fork.
213 *
214 * NOTE: If successful and a vnode is returned, the caller is responsible
215 * for releasing the returned vnode with vnode_rele().
216 */
217static int
218hfs_ref_data_vp(struct cnode *cp, struct vnode **data_vp, int skiplock)
219{
7e4a7d39
A
220 int vref = 0;
221
b0d623f7
A
222 if (!data_vp || !cp) /* sanity check incoming parameters */
223 return EINVAL;
224
225 /* maybe we should take the hfs cnode lock here, and if so, use the skiplock parameter to tell us not to */
226
227 if (!skiplock) hfs_lock(cp, HFS_SHARED_LOCK);
228 struct vnode *c_vp = cp->c_vp;
229 if (c_vp) {
230 /* we already have a data vnode */
231 *data_vp = c_vp;
7e4a7d39 232 vref = vnode_ref(*data_vp);
b0d623f7 233 if (!skiplock) hfs_unlock(cp);
7e4a7d39
A
234 if (vref == 0) {
235 return 0;
236 }
237 return EINVAL;
b0d623f7
A
238 }
239 /* no data fork vnode in the cnode, so ask hfs for one. */
240
241 if (!cp->c_rsrc_vp) {
242 /* if we don't have either a c_vp or c_rsrc_vp, we can't really do anything useful */
243 *data_vp = NULL;
244 if (!skiplock) hfs_unlock(cp);
245 return EINVAL;
246 }
247
248 if (0 == hfs_vget(VTOHFS(cp->c_rsrc_vp), cp->c_cnid, data_vp, 1) &&
249 0 != data_vp) {
7e4a7d39 250 vref = vnode_ref(*data_vp);
b0d623f7
A
251 vnode_put(*data_vp);
252 if (!skiplock) hfs_unlock(cp);
7e4a7d39
A
253 if (vref == 0) {
254 return 0;
255 }
256 return EINVAL;
b0d623f7
A
257 }
258 /* there was an error getting the vnode */
259 *data_vp = NULL;
260 if (!skiplock) hfs_unlock(cp);
261 return EINVAL;
262}
263
264/*
265 * hfs_lazy_init_decmpfs_cnode(): returns the decmpfs_cnode for a cnode,
266 * allocating it if necessary; returns NULL if there was an allocation error
267 */
268static decmpfs_cnode *
269hfs_lazy_init_decmpfs_cnode(struct cnode *cp)
270{
271 if (!cp->c_decmp) {
272 decmpfs_cnode *dp = NULL;
273 MALLOC_ZONE(dp, decmpfs_cnode *, sizeof(decmpfs_cnode), M_DECMPFS_CNODE, M_WAITOK);
274 if (!dp) {
275 /* error allocating a decmpfs cnode */
276 return NULL;
277 }
278 decmpfs_cnode_init(dp);
279 if (!OSCompareAndSwapPtr(NULL, dp, (void * volatile *)&cp->c_decmp)) {
280 /* another thread got here first, so free the decmpfs_cnode we allocated */
281 decmpfs_cnode_destroy(dp);
282 FREE_ZONE(dp, sizeof(*dp), M_DECMPFS_CNODE);
283 }
284 }
285
286 return cp->c_decmp;
287}
288
289/*
290 * hfs_file_is_compressed(): returns 1 if the file is compressed, and 0 (zero) if not.
291 * if the file's compressed flag is set, makes sure that the decmpfs_cnode field
292 * is allocated by calling hfs_lazy_init_decmpfs_cnode(), then makes sure it is populated,
293 * or else fills it in via the decmpfs_file_is_compressed() function.
294 */
295int
296hfs_file_is_compressed(struct cnode *cp, int skiplock)
297{
298 int ret = 0;
299
300 /* fast check to see if file is compressed. If flag is clear, just answer no */
301 if (!(cp->c_flags & UF_COMPRESSED)) {
302 return 0;
303 }
304
305 decmpfs_cnode *dp = hfs_lazy_init_decmpfs_cnode(cp);
306 if (!dp) {
307 /* error allocating a decmpfs cnode, treat the file as uncompressed */
308 return 0;
309 }
310
311 /* flag was set, see if the decmpfs_cnode state is valid (zero == invalid) */
312 uint32_t decmpfs_state = decmpfs_cnode_get_vnode_state(dp);
313 switch(decmpfs_state) {
314 case FILE_IS_COMPRESSED:
315 case FILE_IS_CONVERTING: /* treat decompressing files as if they are compressed */
316 return 1;
317 case FILE_IS_NOT_COMPRESSED:
318 return 0;
319 /* otherwise the state is not cached yet */
320 }
321
322 /* decmpfs hasn't seen this file yet, so call decmpfs_file_is_compressed() to init the decmpfs_cnode struct */
323 struct vnode *data_vp = NULL;
324 if (0 == hfs_ref_data_vp(cp, &data_vp, skiplock)) {
325 if (data_vp) {
326 ret = decmpfs_file_is_compressed(data_vp, VTOCMP(data_vp)); // fill in decmpfs_cnode
327 vnode_rele(data_vp);
328 }
329 }
330 return ret;
331}
332
333/* hfs_uncompressed_size_of_compressed_file() - get the uncompressed size of the file.
334 * if the caller has passed a valid vnode (has a ref count > 0), then hfsmp and fid are not required.
335 * if the caller doesn't have a vnode, pass NULL in vp, and pass valid hfsmp and fid.
336 * files size is returned in size (required)
337 */
338int
339hfs_uncompressed_size_of_compressed_file(struct hfsmount *hfsmp, struct vnode *vp, cnid_t fid, off_t *size, int skiplock)
340{
341 int ret = 0;
342 int putaway = 0; /* flag to remember if we used hfs_vget() */
343
344 if (!size) {
345 return EINVAL; /* no place to put the file size */
346 }
347
348 if (NULL == vp) {
349 if (!hfsmp || !fid) { /* make sure we have the required parameters */
350 return EINVAL;
351 }
352 if (0 != hfs_vget(hfsmp, fid, &vp, skiplock)) { /* vnode is null, use hfs_vget() to get it */
353 vp = NULL;
354 } else {
355 putaway = 1; /* note that hfs_vget() was used to aquire the vnode */
356 }
357 }
358 /* this double check for compression (hfs_file_is_compressed)
359 * ensures the cached size is present in case decmpfs hasn't
360 * encountered this node yet.
361 */
362 if ( ( NULL != vp ) && hfs_file_is_compressed(VTOC(vp), skiplock) ) {
363 *size = decmpfs_cnode_get_vnode_cached_size(VTOCMP(vp)); /* file info will be cached now, so get size */
364 } else {
365 ret = EINVAL;
366 }
367
368 if (putaway) { /* did we use hfs_vget() to get this vnode? */
369 vnode_put(vp); /* if so, release it and set it to null */
370 vp = NULL;
371 }
372 return ret;
373}
374
375int
376hfs_hides_rsrc(vfs_context_t ctx, struct cnode *cp, int skiplock)
377{
378 if (ctx == decmpfs_ctx)
379 return 0;
380 if (!hfs_file_is_compressed(cp, skiplock))
381 return 0;
382 return decmpfs_hides_rsrc(ctx, cp->c_decmp);
383}
384
385int
386hfs_hides_xattr(vfs_context_t ctx, struct cnode *cp, const char *name, int skiplock)
387{
388 if (ctx == decmpfs_ctx)
389 return 0;
390 if (!hfs_file_is_compressed(cp, skiplock))
391 return 0;
392 return decmpfs_hides_xattr(ctx, cp->c_decmp, name);
393}
394#endif /* HFS_COMPRESSION */
395
9bccf70c 396/*
91447636
A
397 * Open a file/directory.
398 */
9bccf70c 399static int
91447636 400hfs_vnop_open(struct vnop_open_args *ap)
9bccf70c
A
401{
402 struct vnode *vp = ap->a_vp;
91447636 403 struct filefork *fp;
55e303ae 404 struct timeval tv;
91447636 405 int error;
b0d623f7
A
406 static int past_bootup = 0;
407 struct cnode *cp = VTOC(vp);
408 struct hfsmount *hfsmp = VTOHFS(vp);
409
410#if HFS_COMPRESSION
411 if (ap->a_mode & FWRITE) {
412 /* open for write */
413 if ( hfs_file_is_compressed(cp, 1) ) { /* 1 == don't take the cnode lock */
414 /* opening a compressed file for write, so convert it to decompressed */
415 struct vnode *data_vp = NULL;
416 error = hfs_ref_data_vp(cp, &data_vp, 1); /* 1 == don't take the cnode lock */
417 if (0 == error) {
418 if (data_vp) {
419 error = decmpfs_decompress_file(data_vp, VTOCMP(data_vp), -1, 1, 0);
420 vnode_rele(data_vp);
421 } else {
422 error = EINVAL;
423 }
424 }
425 if (error != 0)
426 return error;
427 }
428 } else {
429 /* open for read */
430 if (hfs_file_is_compressed(cp, 1) ) { /* 1 == don't take the cnode lock */
431 if (VNODE_IS_RSRC(vp)) {
432 /* opening the resource fork of a compressed file, so nothing to do */
433 } else {
434 /* opening a compressed file for read, make sure it validates */
435 error = decmpfs_validate_compressed_file(vp, VTOCMP(vp));
436 if (error != 0)
437 return error;
438 }
439 }
440 }
441#endif
9bccf70c
A
442
443 /*
444 * Files marked append-only must be opened for appending.
445 */
b0d623f7 446 if ((cp->c_flags & APPEND) && !vnode_isdir(vp) &&
9bccf70c
A
447 (ap->a_mode & (FWRITE | O_APPEND)) == FWRITE)
448 return (EPERM);
449
91447636
A
450 if (vnode_isreg(vp) && !UBCINFOEXISTS(vp))
451 return (EBUSY); /* file is in use by the kernel */
55e303ae 452
91447636 453 /* Don't allow journal file to be opened externally. */
b0d623f7 454 if (cp->c_fileid == hfsmp->hfs_jnlfileid)
91447636 455 return (EPERM);
b0d623f7
A
456
457 /* If we're going to write to the file, initialize quotas. */
458#if QUOTA
459 if ((ap->a_mode & FWRITE) && (hfsmp->hfs_flags & HFS_QUOTAS))
460 (void)hfs_getinoquota(cp);
461#endif /* QUOTA */
462
55e303ae
A
463 /*
464 * On the first (non-busy) open of a fragmented
465 * file attempt to de-frag it (if its less than 20MB).
466 */
b0d623f7
A
467 if ((hfsmp->hfs_flags & HFS_READ_ONLY) ||
468 (hfsmp->jnl == NULL) ||
2d21ac55
A
469#if NAMEDSTREAMS
470 !vnode_isreg(vp) || vnode_isinuse(vp, 0) || vnode_isnamedstream(vp)) {
471#else
91447636 472 !vnode_isreg(vp) || vnode_isinuse(vp, 0)) {
2d21ac55 473#endif
55e303ae
A
474 return (0);
475 }
91447636 476
b0d623f7 477 if ((error = hfs_lock(cp, HFS_EXCLUSIVE_LOCK)))
91447636 478 return (error);
55e303ae
A
479 fp = VTOF(vp);
480 if (fp->ff_blocks &&
481 fp->ff_extents[7].blockCount != 0 &&
482 fp->ff_size <= (20 * 1024 * 1024)) {
b0d623f7 483 int no_mods = 0;
2d21ac55 484 struct timeval now;
55e303ae
A
485 /*
486 * Wait until system bootup is done (3 min).
2d21ac55
A
487 * And don't relocate a file that's been modified
488 * within the past minute -- this can lead to
489 * system thrashing.
55e303ae 490 */
b0d623f7
A
491
492 if (!past_bootup) {
493 microuptime(&tv);
494 if (tv.tv_sec > (60*3)) {
495 past_bootup = 1;
496 }
497 }
498
2d21ac55 499 microtime(&now);
b0d623f7
A
500 if ((now.tv_sec - cp->c_mtime) > 60) {
501 no_mods = 1;
502 }
503
504 if (past_bootup && no_mods) {
505 (void) hfs_relocate(vp, hfsmp->nextAllocation + 4096,
506 vfs_context_ucred(ap->a_context),
507 vfs_context_proc(ap->a_context));
55e303ae 508 }
55e303ae 509 }
b0d623f7 510 hfs_unlock(cp);
55e303ae 511
9bccf70c
A
512 return (0);
513}
514
9bccf70c 515
91447636
A
516/*
517 * Close a file/directory.
518 */
9bccf70c 519static int
91447636
A
520hfs_vnop_close(ap)
521 struct vnop_close_args /* {
9bccf70c
A
522 struct vnode *a_vp;
523 int a_fflag;
91447636 524 vfs_context_t a_context;
9bccf70c
A
525 } */ *ap;
526{
527 register struct vnode *vp = ap->a_vp;
91447636
A
528 register struct cnode *cp;
529 struct proc *p = vfs_context_proc(ap->a_context);
530 struct hfsmount *hfsmp;
531 int busy;
cf7d32b8 532 int tooktrunclock = 0;
b0d623f7 533 int knownrefs = 0;
55e303ae 534
91447636 535 if ( hfs_lock(VTOC(vp), HFS_EXCLUSIVE_LOCK) != 0)
9bccf70c 536 return (0);
91447636
A
537 cp = VTOC(vp);
538 hfsmp = VTOHFS(vp);
9bccf70c 539
b0d623f7
A
540 /*
541 * If the rsrc fork is a named stream, it can cause the data fork to
542 * stay around, preventing de-allocation of these blocks.
543 * Do checks for truncation on close. Purge extra extents if they exist.
544 * Make sure the vp is not a directory, and that it has a resource fork,
545 * and that resource fork is also a named stream.
cf7d32b8 546 */
b0d623f7 547
cf7d32b8
A
548 if ((vp->v_type == VREG) && (cp->c_rsrc_vp)
549 && (vnode_isnamedstream(cp->c_rsrc_vp))) {
550 uint32_t blks;
551
552 blks = howmany(VTOF(vp)->ff_size, VTOVCB(vp)->blockSize);
553 /*
b0d623f7
A
554 * If there are extra blocks and there are only 2 refs on
555 * this vp (ourselves + rsrc fork holding ref on us), go ahead
556 * and try to truncate.
cf7d32b8
A
557 */
558 if ((blks < VTOF(vp)->ff_blocks) && (!vnode_isinuse(vp, 2))) {
b0d623f7
A
559 // release cnode lock; must acquire truncate lock BEFORE cnode lock
560 hfs_unlock(cp);
cf7d32b8
A
561
562 hfs_lock_truncate(cp, TRUE);
563 tooktrunclock = 1;
b0d623f7
A
564
565 if (hfs_lock(VTOC(vp), HFS_EXCLUSIVE_LOCK) != 0) {
cf7d32b8 566 hfs_unlock_truncate(cp, TRUE);
b0d623f7
A
567 // bail out if we can't re-acquire cnode lock
568 return 0;
cf7d32b8 569 }
b0d623f7 570 // now re-test to make sure it's still valid
cf7d32b8
A
571 if (cp->c_rsrc_vp) {
572 knownrefs = 1 + vnode_isnamedstream(cp->c_rsrc_vp);
b0d623f7
A
573 if (!vnode_isinuse(vp, knownrefs)){
574 // now we can truncate the file, if necessary
cf7d32b8 575 blks = howmany(VTOF(vp)->ff_size, VTOVCB(vp)->blockSize);
b0d623f7
A
576 if (blks < VTOF(vp)->ff_blocks){
577 (void) hfs_truncate(vp, VTOF(vp)->ff_size, IO_NDELAY, 0, 0, ap->a_context);
cf7d32b8
A
578 }
579 }
580 }
581 }
582 }
583
b0d623f7 584
91447636
A
585 // if we froze the fs and we're exiting, then "thaw" the fs
586 if (hfsmp->hfs_freezing_proc == p && proc_exiting(p)) {
587 hfsmp->hfs_freezing_proc = NULL;
588 hfs_global_exclusive_lock_release(hfsmp);
3a60a9f5 589 lck_rw_unlock_exclusive(&hfsmp->hfs_insync);
91447636 590 }
9bccf70c 591
91447636 592 busy = vnode_isinuse(vp, 1);
9bccf70c 593
91447636
A
594 if (busy) {
595 hfs_touchtimes(VTOHFS(vp), cp);
596 }
597 if (vnode_isdir(vp)) {
598 hfs_reldirhints(cp, busy);
599 } else if (vnode_issystem(vp) && !busy) {
600 vnode_recycle(vp);
9bccf70c 601 }
b0d623f7
A
602
603 if (tooktrunclock){
cf7d32b8
A
604 hfs_unlock_truncate(cp, TRUE);
605 }
91447636 606 hfs_unlock(cp);
e2fac8b1
A
607
608 if (ap->a_fflag & FWASWRITTEN) {
609 hfs_sync_ejectable(hfsmp);
610 }
611
9bccf70c
A
612 return (0);
613}
614
615/*
91447636
A
616 * Get basic attributes.
617 */
9bccf70c 618static int
91447636 619hfs_vnop_getattr(struct vnop_getattr_args *ap)
9bccf70c 620{
2d21ac55
A
621#define VNODE_ATTR_TIMES \
622 (VNODE_ATTR_va_access_time|VNODE_ATTR_va_change_time|VNODE_ATTR_va_modify_time)
623#define VNODE_ATTR_AUTH \
624 (VNODE_ATTR_va_mode | VNODE_ATTR_va_uid | VNODE_ATTR_va_gid | \
625 VNODE_ATTR_va_flags | VNODE_ATTR_va_acl)
626
9bccf70c 627 struct vnode *vp = ap->a_vp;
91447636 628 struct vnode_attr *vap = ap->a_vap;
935ed37a 629 struct vnode *rvp = NULLVP;
91447636
A
630 struct hfsmount *hfsmp;
631 struct cnode *cp;
2d21ac55 632 uint64_t data_size;
91447636
A
633 enum vtype v_type;
634 int error = 0;
91447636 635 cp = VTOC(vp);
2d21ac55 636
b0d623f7
A
637#if HFS_COMPRESSION
638 /* we need to inspect the decmpfs state of the file before we take the hfs cnode lock */
639 int compressed = 0;
640 int hide_size = 0;
641 off_t uncompressed_size = -1;
642 if (VATTR_IS_ACTIVE(vap, va_data_size) || VATTR_IS_ACTIVE(vap, va_total_alloc) || VATTR_IS_ACTIVE(vap, va_data_alloc) || VATTR_IS_ACTIVE(vap, va_total_size)) {
643 /* we only care about whether the file is compressed if asked for the uncompressed size */
644 if (VNODE_IS_RSRC(vp)) {
645 /* if it's a resource fork, decmpfs may want us to hide the size */
646 hide_size = hfs_hides_rsrc(ap->a_context, cp, 0);
647 } else {
648 /* if it's a data fork, we need to know if it was compressed so we can report the uncompressed size */
649 compressed = hfs_file_is_compressed(cp, 0);
650 }
651 if (compressed && (VATTR_IS_ACTIVE(vap, va_data_size) || VATTR_IS_ACTIVE(vap, va_total_size))) {
652 if (0 != hfs_uncompressed_size_of_compressed_file(NULL, vp, 0, &uncompressed_size, 0)) {
653 /* failed to get the uncompressed size, we'll check for this later */
654 uncompressed_size = -1;
655 }
656 }
657 }
658#endif
659
2d21ac55
A
660 /*
661 * Shortcut for vnode_authorize path. Each of the attributes
662 * in this set is updated atomically so we don't need to take
663 * the cnode lock to access them.
664 */
665 if ((vap->va_active & ~VNODE_ATTR_AUTH) == 0) {
666 /* Make sure file still exists. */
667 if (cp->c_flag & C_NOEXISTS)
668 return (ENOENT);
669
670 vap->va_uid = cp->c_uid;
671 vap->va_gid = cp->c_gid;
672 vap->va_mode = cp->c_mode;
673 vap->va_flags = cp->c_flags;
674 vap->va_supported |= VNODE_ATTR_AUTH & ~VNODE_ATTR_va_acl;
675
676 if ((cp->c_attr.ca_recflags & kHFSHasSecurityMask) == 0) {
677 vap->va_acl = (kauth_acl_t) KAUTH_FILESEC_NONE;
678 VATTR_SET_SUPPORTED(vap, va_acl);
679 }
b0d623f7 680
2d21ac55
A
681 return (0);
682 }
b0d623f7 683
91447636 684 hfsmp = VTOHFS(vp);
91447636 685 v_type = vnode_vtype(vp);
2d21ac55
A
686 /*
687 * If time attributes are requested and we have cnode times
688 * that require updating, then acquire an exclusive lock on
689 * the cnode before updating the times. Otherwise we can
690 * just acquire a shared lock.
691 */
692 if ((vap->va_active & VNODE_ATTR_TIMES) &&
693 (cp->c_touch_acctime || cp->c_touch_chgtime || cp->c_touch_modtime)) {
694 if ((error = hfs_lock(cp, HFS_EXCLUSIVE_LOCK)))
695 return (error);
696 hfs_touchtimes(hfsmp, cp);
b0d623f7
A
697 }
698 else {
2d21ac55
A
699 if ((error = hfs_lock(cp, HFS_SHARED_LOCK)))
700 return (error);
701 }
702
91447636 703 if (v_type == VDIR) {
2d21ac55
A
704 data_size = (cp->c_entries + 2) * AVERAGE_HFSDIRENTRY_SIZE;
705
91447636 706 if (VATTR_IS_ACTIVE(vap, va_nlink)) {
2d21ac55 707 int nlink;
91447636 708
2d21ac55
A
709 /*
710 * For directories, the va_nlink is esentially a count
711 * of the ".." references to a directory plus the "."
712 * reference and the directory itself. So for HFS+ this
713 * becomes the sub-directory count plus two.
714 *
715 * In the absence of a sub-directory count we use the
716 * directory's item count. This will be too high in
717 * most cases since it also includes files.
718 */
719 if ((hfsmp->hfs_flags & HFS_FOLDERCOUNT) &&
720 (cp->c_attr.ca_recflags & kHFSHasFolderCountMask))
721 nlink = cp->c_attr.ca_dircount; /* implied ".." entries */
722 else
723 nlink = cp->c_entries;
724
725 /* Account for ourself and our "." entry */
726 nlink += 2;
727 /* Hide our private directories. */
728 if (cp->c_cnid == kHFSRootFolderID) {
729 if (hfsmp->hfs_private_desc[FILE_HARDLINKS].cd_cnid != 0) {
730 --nlink;
731 }
732 if (hfsmp->hfs_private_desc[DIR_HARDLINKS].cd_cnid != 0) {
733 --nlink;
734 }
91447636 735 }
2d21ac55
A
736 VATTR_RETURN(vap, va_nlink, (u_int64_t)nlink);
737 }
91447636
A
738 if (VATTR_IS_ACTIVE(vap, va_nchildren)) {
739 int entries;
740
741 entries = cp->c_entries;
2d21ac55
A
742 /* Hide our private files and directories. */
743 if (cp->c_cnid == kHFSRootFolderID) {
744 if (hfsmp->hfs_private_desc[FILE_HARDLINKS].cd_cnid != 0)
745 --entries;
746 if (hfsmp->hfs_private_desc[DIR_HARDLINKS].cd_cnid != 0)
747 --entries;
748 if (hfsmp->jnl || ((hfsmp->vcbAtrb & kHFSVolumeJournaledMask) && (hfsmp->hfs_flags & HFS_READ_ONLY)))
749 entries -= 2; /* hide the journal files */
91447636
A
750 }
751 VATTR_RETURN(vap, va_nchildren, entries);
752 }
2d21ac55
A
753 /*
754 * The va_dirlinkcount is the count of real directory hard links.
755 * (i.e. its not the sum of the implied "." and ".." references)
756 */
757 if (VATTR_IS_ACTIVE(vap, va_dirlinkcount)) {
758 VATTR_RETURN(vap, va_dirlinkcount, (uint32_t)cp->c_linkcount);
759 }
760 } else /* !VDIR */ {
761 data_size = VCTOF(vp, cp)->ff_size;
762
763 VATTR_RETURN(vap, va_nlink, (u_int64_t)cp->c_linkcount);
764 if (VATTR_IS_ACTIVE(vap, va_data_alloc)) {
765 u_int64_t blocks;
766
b0d623f7
A
767#if HFS_COMPRESSION
768 if (hide_size) {
769 VATTR_RETURN(vap, va_data_alloc, 0);
770 } else if (compressed) {
771 /* for compressed files, we report all allocated blocks as belonging to the data fork */
772 blocks = cp->c_blocks;
773 VATTR_RETURN(vap, va_data_alloc, blocks * (u_int64_t)hfsmp->blockSize);
774 }
775 else
776#endif
777 {
778 blocks = VCTOF(vp, cp)->ff_blocks;
779 VATTR_RETURN(vap, va_data_alloc, blocks * (u_int64_t)hfsmp->blockSize);
780 }
2d21ac55 781 }
91447636 782 }
9bccf70c 783
91447636
A
784 /* conditional because 64-bit arithmetic can be expensive */
785 if (VATTR_IS_ACTIVE(vap, va_total_size)) {
786 if (v_type == VDIR) {
2d21ac55 787 VATTR_RETURN(vap, va_total_size, (cp->c_entries + 2) * AVERAGE_HFSDIRENTRY_SIZE);
91447636 788 } else {
b0d623f7 789 u_int64_t total_size = ~0ULL;
91447636 790 struct cnode *rcp;
b0d623f7
A
791#if HFS_COMPRESSION
792 if (hide_size) {
793 /* we're hiding the size of this file, so just return 0 */
794 total_size = 0;
795 } else if (compressed) {
796 if (uncompressed_size == -1) {
797 /*
798 * We failed to get the uncompressed size above,
799 * so we'll fall back to the standard path below
800 * since total_size is still -1
801 */
802 } else {
803 /* use the uncompressed size we fetched above */
804 total_size = uncompressed_size;
805 }
91447636 806 }
b0d623f7
A
807#endif
808 if (total_size == ~0ULL) {
809 if (cp->c_datafork) {
810 total_size = cp->c_datafork->ff_size;
91447636 811 }
b0d623f7
A
812
813 if (cp->c_blocks - VTOF(vp)->ff_blocks) {
814 /* We deal with rsrc fork vnode iocount at the end of the function */
b7266188 815 error = hfs_vgetrsrc(hfsmp, vp, &rvp, TRUE, TRUE);
b0d623f7 816 if (error) {
b7266188
A
817 /*
818 * hfs_vgetrsrc may have returned a vnode in rvp even though
819 * we got an error, because we specified error_on_unlinked.
820 * We need to drop the iocount after we release the cnode lock, so
821 * it will be taken care of at the end of the function if it's needed.
822 */
b0d623f7
A
823 goto out;
824 }
825
826 rcp = VTOC(rvp);
827 if (rcp && rcp->c_rsrcfork) {
828 total_size += rcp->c_rsrcfork->ff_size;
829 }
91447636
A
830 }
831 }
b0d623f7 832
91447636 833 VATTR_RETURN(vap, va_total_size, total_size);
91447636 834 }
9bccf70c 835 }
91447636
A
836 if (VATTR_IS_ACTIVE(vap, va_total_alloc)) {
837 if (v_type == VDIR) {
838 VATTR_RETURN(vap, va_total_alloc, 0);
839 } else {
2d21ac55 840 VATTR_RETURN(vap, va_total_alloc, (u_int64_t)cp->c_blocks * (u_int64_t)hfsmp->blockSize);
91447636 841 }
9bccf70c
A
842 }
843
9bccf70c 844 /*
91447636
A
845 * If the VFS wants extended security data, and we know that we
846 * don't have any (because it never told us it was setting any)
847 * then we can return the supported bit and no data. If we do
848 * have extended security, we can just leave the bit alone and
849 * the VFS will use the fallback path to fetch it.
9bccf70c 850 */
91447636
A
851 if (VATTR_IS_ACTIVE(vap, va_acl)) {
852 if ((cp->c_attr.ca_recflags & kHFSHasSecurityMask) == 0) {
2d21ac55 853 vap->va_acl = (kauth_acl_t) KAUTH_FILESEC_NONE;
91447636
A
854 VATTR_SET_SUPPORTED(vap, va_acl);
855 }
55e303ae 856 }
91447636 857 if (VATTR_IS_ACTIVE(vap, va_access_time)) {
2d21ac55 858 /* Access times are lazily updated, get current time if needed */
91447636
A
859 if (cp->c_touch_acctime) {
860 struct timeval tv;
861
862 microtime(&tv);
863 vap->va_access_time.tv_sec = tv.tv_sec;
864 } else {
865 vap->va_access_time.tv_sec = cp->c_atime;
866 }
867 vap->va_access_time.tv_nsec = 0;
868 VATTR_SET_SUPPORTED(vap, va_access_time);
869 }
2d21ac55
A
870 vap->va_create_time.tv_sec = cp->c_itime;
871 vap->va_create_time.tv_nsec = 0;
91447636
A
872 vap->va_modify_time.tv_sec = cp->c_mtime;
873 vap->va_modify_time.tv_nsec = 0;
91447636
A
874 vap->va_change_time.tv_sec = cp->c_ctime;
875 vap->va_change_time.tv_nsec = 0;
91447636 876 vap->va_backup_time.tv_sec = cp->c_btime;
2d21ac55
A
877 vap->va_backup_time.tv_nsec = 0;
878
879 /* XXX is this really a good 'optimal I/O size'? */
880 vap->va_iosize = hfsmp->hfs_logBlockSize;
881 vap->va_uid = cp->c_uid;
882 vap->va_gid = cp->c_gid;
883 vap->va_mode = cp->c_mode;
884 vap->va_flags = cp->c_flags;
91447636 885
9bccf70c
A
886 /*
887 * Exporting file IDs from HFS Plus:
888 *
889 * For "normal" files the c_fileid is the same value as the
890 * c_cnid. But for hard link files, they are different - the
891 * c_cnid belongs to the active directory entry (ie the link)
892 * and the c_fileid is for the actual inode (ie the data file).
893 *
91447636
A
894 * The stat call (getattr) uses va_fileid and the Carbon APIs,
895 * which are hardlink-ignorant, will ask for va_linkid.
9bccf70c 896 */
2d21ac55 897 vap->va_fileid = (u_int64_t)cp->c_fileid;
935ed37a
A
898 /*
899 * We need to use the origin cache for both hardlinked files
900 * and directories. Hardlinked directories have multiple cnids
901 * and parents (one per link). Hardlinked files also have their
902 * own parents and link IDs separate from the indirect inode number.
903 * If we don't use the cache, we could end up vending the wrong ID
904 * because the cnode will only reflect the link that was looked up most recently.
905 */
906 if (cp->c_flag & C_HARDLINK) {
2d21ac55
A
907 vap->va_linkid = (u_int64_t)hfs_currentcnid(cp);
908 vap->va_parentid = (u_int64_t)hfs_currentparent(cp);
909 } else {
910 vap->va_linkid = (u_int64_t)cp->c_cnid;
911 vap->va_parentid = (u_int64_t)cp->c_parentcnid;
912 }
b0d623f7 913 vap->va_fsid = hfsmp->hfs_raw_dev;
2d21ac55
A
914 vap->va_filerev = 0;
915 vap->va_encoding = cp->c_encoding;
916 vap->va_rdev = (v_type == VBLK || v_type == VCHR) ? cp->c_rdev : 0;
b0d623f7
A
917#if HFS_COMPRESSION
918 if (VATTR_IS_ACTIVE(vap, va_data_size)) {
919 if (hide_size)
920 vap->va_data_size = 0;
921 else if (compressed) {
922 if (uncompressed_size == -1) {
923 /* failed to get the uncompressed size above, so just return data_size */
924 vap->va_data_size = data_size;
925 } else {
926 /* use the uncompressed size we fetched above */
927 vap->va_data_size = uncompressed_size;
928 }
929 } else
930 vap->va_data_size = data_size;
931// vap->va_supported |= VNODE_ATTR_va_data_size;
932 VATTR_SET_SUPPORTED(vap, va_data_size);
933 }
934#else
2d21ac55 935 vap->va_data_size = data_size;
b0d623f7
A
936 vap->va_supported |= VNODE_ATTR_va_data_size;
937#endif
938
2d21ac55
A
939 /* Mark them all at once instead of individual VATTR_SET_SUPPORTED calls. */
940 vap->va_supported |= VNODE_ATTR_va_create_time | VNODE_ATTR_va_modify_time |
941 VNODE_ATTR_va_change_time| VNODE_ATTR_va_backup_time |
942 VNODE_ATTR_va_iosize | VNODE_ATTR_va_uid |
943 VNODE_ATTR_va_gid | VNODE_ATTR_va_mode |
944 VNODE_ATTR_va_flags |VNODE_ATTR_va_fileid |
945 VNODE_ATTR_va_linkid | VNODE_ATTR_va_parentid |
946 VNODE_ATTR_va_fsid | VNODE_ATTR_va_filerev |
b0d623f7 947 VNODE_ATTR_va_encoding | VNODE_ATTR_va_rdev;
91447636 948
b0d623f7
A
949 /* If this is the root, let VFS to find out the mount name, which
950 * may be different from the real name. Otherwise, we need to take care
951 * for hardlinked files, which need to be looked up, if necessary
935ed37a 952 */
2d21ac55 953 if (VATTR_IS_ACTIVE(vap, va_name) && (cp->c_cnid != kHFSRootFolderID)) {
935ed37a
A
954 struct cat_desc linkdesc;
955 int lockflags;
956 int uselinkdesc = 0;
957 cnid_t nextlinkid = 0;
b0d623f7 958 cnid_t prevlinkid = 0;
935ed37a
A
959
960 /* Get the name for ATTR_CMN_NAME. We need to take special care for hardlinks
961 * here because the info. for the link ID requested by getattrlist may be
962 * different than what's currently in the cnode. This is because the cnode
963 * will be filled in with the information for the most recent link ID that went
964 * through namei/lookup(). If there are competing lookups for hardlinks that point
b0d623f7
A
965 * to the same inode, one (or more) getattrlists could be vended incorrect name information.
966 * Also, we need to beware of open-unlinked files which could have a namelen of 0.
935ed37a 967 */
b0d623f7
A
968
969 if ((cp->c_flag & C_HARDLINK) &&
935ed37a 970 ((cp->c_desc.cd_namelen == 0) || (vap->va_linkid != cp->c_cnid))) {
b0d623f7
A
971 /* If we have no name and our link ID is the raw inode number, then we may
972 * have an open-unlinked file. Go to the next link in this case.
935ed37a
A
973 */
974 if ((cp->c_desc.cd_namelen == 0) && (vap->va_linkid == cp->c_fileid)) {
b0d623f7 975 if ((error = hfs_lookuplink(hfsmp, vap->va_linkid, &prevlinkid, &nextlinkid))){
935ed37a
A
976 goto out;
977 }
b0d623f7 978 }
935ed37a 979 else {
b0d623f7 980 /* just use link obtained from vap above */
935ed37a 981 nextlinkid = vap->va_linkid;
2d21ac55 982 }
b0d623f7
A
983
984 /* We need to probe the catalog for the descriptor corresponding to the link ID
985 * stored in nextlinkid. Note that we don't know if we have the exclusive lock
986 * for the cnode here, so we can't just update the descriptor. Instead,
987 * we should just store the descriptor's value locally and then use it to pass
988 * out the name value as needed below.
989 */
990 if (nextlinkid){
935ed37a 991 lockflags = hfs_systemfile_lock(hfsmp, SFL_CATALOG, HFS_SHARED_LOCK);
b0d623f7
A
992 error = cat_findname(hfsmp, nextlinkid, &linkdesc);
993 hfs_systemfile_unlock(hfsmp, lockflags);
935ed37a
A
994 if (error == 0) {
995 uselinkdesc = 1;
996 }
2d21ac55 997 }
935ed37a 998 }
b0d623f7
A
999
1000 /* By this point, we've either patched up the name above and the c_desc
1001 * points to the correct data, or it already did, in which case we just proceed
1002 * by copying the name into the vap. Note that we will never set va_name to
1003 * supported if nextlinkid is never initialized. This could happen in the degenerate
1004 * case above involving the raw inode number, where it has no nextlinkid. In this case
1005 * we will simply not mark the name bit as supported.
935ed37a
A
1006 */
1007 if (uselinkdesc) {
b0d623f7 1008 strlcpy(vap->va_name, (const char*) linkdesc.cd_nameptr, MAXPATHLEN);
935ed37a 1009 VATTR_SET_SUPPORTED(vap, va_name);
b0d623f7
A
1010 cat_releasedesc(&linkdesc);
1011 }
935ed37a 1012 else if (cp->c_desc.cd_namelen) {
b0d623f7 1013 strlcpy(vap->va_name, (const char*) cp->c_desc.cd_nameptr, MAXPATHLEN);
935ed37a 1014 VATTR_SET_SUPPORTED(vap, va_name);
91447636 1015 }
9bccf70c 1016 }
9bccf70c 1017
91447636
A
1018out:
1019 hfs_unlock(cp);
b0d623f7
A
1020 /*
1021 * We need to vnode_put the rsrc fork vnode only *after* we've released
1022 * the cnode lock, since vnode_put can trigger an inactive call, which
1023 * will go back into HFS and try to acquire a cnode lock.
935ed37a 1024 */
91447636 1025 if (rvp) {
b0d623f7 1026 vnode_put (rvp);
91447636 1027 }
b0d623f7 1028
91447636
A
1029 return (error);
1030}
9bccf70c
A
1031
1032static int
91447636
A
1033hfs_vnop_setattr(ap)
1034 struct vnop_setattr_args /* {
9bccf70c 1035 struct vnode *a_vp;
91447636
A
1036 struct vnode_attr *a_vap;
1037 vfs_context_t a_context;
9bccf70c
A
1038 } */ *ap;
1039{
91447636 1040 struct vnode_attr *vap = ap->a_vap;
9bccf70c 1041 struct vnode *vp = ap->a_vp;
91447636
A
1042 struct cnode *cp = NULL;
1043 struct hfsmount *hfsmp;
1044 kauth_cred_t cred = vfs_context_ucred(ap->a_context);
1045 struct proc *p = vfs_context_proc(ap->a_context);
1046 int error = 0;
1047 uid_t nuid;
1048 gid_t ngid;
9bccf70c 1049
b0d623f7
A
1050#if HFS_COMPRESSION
1051 int decmpfs_reset_state = 0;
1052 /*
1053 we call decmpfs_update_attributes even if the file is not compressed
1054 because we want to update the incoming flags if the xattrs are invalid
1055 */
1056 error = decmpfs_update_attributes(vp, vap);
1057 if (error)
1058 return error;
1059#endif
1060
91447636 1061 hfsmp = VTOHFS(vp);
9bccf70c 1062
91447636
A
1063 /* Don't allow modification of the journal file. */
1064 if (hfsmp->hfs_jnlfileid == VTOC(vp)->c_fileid) {
1065 return (EPERM);
55e303ae
A
1066 }
1067
91447636
A
1068 /*
1069 * File size change request.
1070 * We are guaranteed that this is not a directory, and that
1071 * the filesystem object is writeable.
b0d623f7
A
1072 *
1073 * NOTE: HFS COMPRESSION depends on the data_size being set *before* the bsd flags are updated
91447636
A
1074 */
1075 VATTR_SET_SUPPORTED(vap, va_data_size);
1076 if (VATTR_IS_ACTIVE(vap, va_data_size) && !vnode_islnk(vp)) {
b0d623f7
A
1077#if HFS_COMPRESSION
1078 /* keep the compressed state locked until we're done truncating the file */
1079 decmpfs_cnode *dp = VTOCMP(vp);
1080 if (!dp) {
1081 /*
1082 * call hfs_lazy_init_decmpfs_cnode() to make sure that the decmpfs_cnode
1083 * is filled in; we need a decmpfs_cnode to lock out decmpfs state changes
1084 * on this file while it's truncating
1085 */
1086 dp = hfs_lazy_init_decmpfs_cnode(VTOC(vp));
1087 if (!dp) {
1088 /* failed to allocate a decmpfs_cnode */
1089 return ENOMEM; /* what should this be? */
1090 }
1091 }
1092
1093 decmpfs_lock_compressed_data(dp, 1);
1094 if (hfs_file_is_compressed(VTOC(vp), 1)) {
1095 error = decmpfs_decompress_file(vp, dp, -1/*vap->va_data_size*/, 0, 1);
1096 if (error != 0) {
1097 decmpfs_unlock_compressed_data(dp, 1);
1098 return error;
1099 }
1100 }
1101#endif
55e303ae 1102
91447636
A
1103 /* Take truncate lock before taking cnode lock. */
1104 hfs_lock_truncate(VTOC(vp), TRUE);
2d21ac55
A
1105
1106 /* Perform the ubc_setsize before taking the cnode lock. */
1107 ubc_setsize(vp, vap->va_data_size);
1108
91447636 1109 if ((error = hfs_lock(VTOC(vp), HFS_EXCLUSIVE_LOCK))) {
2d21ac55 1110 hfs_unlock_truncate(VTOC(vp), TRUE);
b0d623f7
A
1111#if HFS_COMPRESSION
1112 decmpfs_unlock_compressed_data(dp, 1);
1113#endif
91447636
A
1114 return (error);
1115 }
1116 cp = VTOC(vp);
55e303ae 1117
b0d623f7 1118 error = hfs_truncate(vp, vap->va_data_size, vap->va_vaflags & 0xffff, 1, 0, ap->a_context);
91447636 1119
2d21ac55 1120 hfs_unlock_truncate(cp, TRUE);
b0d623f7
A
1121#if HFS_COMPRESSION
1122 decmpfs_unlock_compressed_data(dp, 1);
1123#endif
91447636
A
1124 if (error)
1125 goto out;
1126 }
1127 if (cp == NULL) {
1128 if ((error = hfs_lock(VTOC(vp), HFS_EXCLUSIVE_LOCK)))
9bccf70c 1129 return (error);
91447636 1130 cp = VTOC(vp);
9bccf70c
A
1131 }
1132
2d21ac55
A
1133 /*
1134 * If it is just an access time update request by itself
1135 * we know the request is from kernel level code, and we
1136 * can delay it without being as worried about consistency.
1137 * This change speeds up mmaps, in the rare case that they
1138 * get caught behind a sync.
1139 */
1140
1141 if (vap->va_active == VNODE_ATTR_va_access_time) {
1142 cp->c_touch_acctime=TRUE;
1143 goto out;
1144 }
1145
1146
1147
91447636
A
1148 /*
1149 * Owner/group change request.
1150 * We are guaranteed that the new owner/group is valid and legal.
1151 */
1152 VATTR_SET_SUPPORTED(vap, va_uid);
1153 VATTR_SET_SUPPORTED(vap, va_gid);
1154 nuid = VATTR_IS_ACTIVE(vap, va_uid) ? vap->va_uid : (uid_t)VNOVAL;
1155 ngid = VATTR_IS_ACTIVE(vap, va_gid) ? vap->va_gid : (gid_t)VNOVAL;
1156 if (((nuid != (uid_t)VNOVAL) || (ngid != (gid_t)VNOVAL)) &&
1157 ((error = hfs_chown(vp, nuid, ngid, cred, p)) != 0))
1158 goto out;
b4c24cb9 1159
91447636
A
1160 /*
1161 * Mode change request.
1162 * We are guaranteed that the mode value is valid and that in
1163 * conjunction with the owner and group, this change is legal.
b0d623f7 1164 */
91447636
A
1165 VATTR_SET_SUPPORTED(vap, va_mode);
1166 if (VATTR_IS_ACTIVE(vap, va_mode) &&
1167 ((error = hfs_chmod(vp, (int)vap->va_mode, cred, p)) != 0))
1168 goto out;
b4c24cb9 1169
91447636
A
1170 /*
1171 * File flags change.
1172 * We are guaranteed that only flags allowed to change given the
1173 * current securelevel are being changed.
1174 */
1175 VATTR_SET_SUPPORTED(vap, va_flags);
cc9f6e38 1176 if (VATTR_IS_ACTIVE(vap, va_flags)) {
2d21ac55
A
1177 u_int16_t *fdFlags;
1178
b0d623f7
A
1179#if HFS_COMPRESSION
1180 if ((cp->c_flags ^ vap->va_flags) & UF_COMPRESSED) {
1181 /*
1182 * the UF_COMPRESSED was toggled, so reset our cached compressed state
1183 * but we don't want to actually do the update until we've released the cnode lock down below
1184 * NOTE: turning the flag off doesn't actually decompress the file, so that we can
1185 * turn off the flag and look at the "raw" file for debugging purposes
1186 */
1187 decmpfs_reset_state = 1;
1188 }
1189#endif
1190
cc9f6e38
A
1191 cp->c_flags = vap->va_flags;
1192 cp->c_touch_chgtime = TRUE;
2d21ac55
A
1193
1194 /*
1195 * Mirror the UF_HIDDEN flag to the invisible bit of the Finder Info.
1196 *
1197 * The fdFlags for files and frFlags for folders are both 8 bytes
1198 * into the userInfo (the first 16 bytes of the Finder Info). They
1199 * are both 16-bit fields.
1200 */
1201 fdFlags = (u_int16_t *) &cp->c_finderinfo[8];
1202 if (vap->va_flags & UF_HIDDEN)
1203 *fdFlags |= OSSwapHostToBigConstInt16(kFinderInvisibleMask);
1204 else
1205 *fdFlags &= ~OSSwapHostToBigConstInt16(kFinderInvisibleMask);
9bccf70c 1206 }
91447636
A
1207
1208 /*
1209 * Timestamp updates.
1210 */
1211 VATTR_SET_SUPPORTED(vap, va_create_time);
1212 VATTR_SET_SUPPORTED(vap, va_access_time);
1213 VATTR_SET_SUPPORTED(vap, va_modify_time);
1214 VATTR_SET_SUPPORTED(vap, va_backup_time);
1215 VATTR_SET_SUPPORTED(vap, va_change_time);
1216 if (VATTR_IS_ACTIVE(vap, va_create_time) ||
1217 VATTR_IS_ACTIVE(vap, va_access_time) ||
1218 VATTR_IS_ACTIVE(vap, va_modify_time) ||
1219 VATTR_IS_ACTIVE(vap, va_backup_time)) {
91447636
A
1220 if (VATTR_IS_ACTIVE(vap, va_create_time))
1221 cp->c_itime = vap->va_create_time.tv_sec;
1222 if (VATTR_IS_ACTIVE(vap, va_access_time)) {
1223 cp->c_atime = vap->va_access_time.tv_sec;
1224 cp->c_touch_acctime = FALSE;
9bccf70c 1225 }
91447636
A
1226 if (VATTR_IS_ACTIVE(vap, va_modify_time)) {
1227 cp->c_mtime = vap->va_modify_time.tv_sec;
1228 cp->c_touch_modtime = FALSE;
1229 cp->c_touch_chgtime = TRUE;
1230
9bccf70c
A
1231 /*
1232 * The utimes system call can reset the modification
1233 * time but it doesn't know about HFS create times.
91447636 1234 * So we need to ensure that the creation time is
9bccf70c
A
1235 * always at least as old as the modification time.
1236 */
1237 if ((VTOVCB(vp)->vcbSigWord == kHFSPlusSigWord) &&
91447636
A
1238 (cp->c_cnid != kHFSRootFolderID) &&
1239 (cp->c_mtime < cp->c_itime)) {
1240 cp->c_itime = cp->c_mtime;
9bccf70c
A
1241 }
1242 }
91447636
A
1243 if (VATTR_IS_ACTIVE(vap, va_backup_time))
1244 cp->c_btime = vap->va_backup_time.tv_sec;
1245 cp->c_flag |= C_MODIFIED;
9bccf70c 1246 }
91447636
A
1247
1248 /*
1249 * Set name encoding.
1250 */
1251 VATTR_SET_SUPPORTED(vap, va_encoding);
1252 if (VATTR_IS_ACTIVE(vap, va_encoding)) {
1253 cp->c_encoding = vap->va_encoding;
1254 hfs_setencodingbits(hfsmp, cp->c_encoding);
9bccf70c 1255 }
91447636 1256
91447636 1257 if ((error = hfs_update(vp, TRUE)) != 0)
2d21ac55 1258 goto out;
91447636 1259out:
b0d623f7
A
1260 if (cp) {
1261 /* Purge origin cache for cnode, since caller now has correct link ID for it
1262 * We purge it here since it was acquired for us during lookup, and we no longer need it.
1263 */
1264 if ((cp->c_flag & C_HARDLINK) && (vp->v_type != VDIR)){
1265 hfs_relorigin(cp, 0);
1266 }
1267
91447636 1268 hfs_unlock(cp);
b0d623f7
A
1269#if HFS_COMPRESSION
1270 if (decmpfs_reset_state) {
1271 /*
1272 * we've changed the UF_COMPRESSED flag, so reset the decmpfs state for this cnode
1273 * but don't do it while holding the hfs cnode lock
1274 */
1275 decmpfs_cnode *dp = VTOCMP(vp);
1276 if (!dp) {
1277 /*
1278 * call hfs_lazy_init_decmpfs_cnode() to make sure that the decmpfs_cnode
1279 * is filled in; we need a decmpfs_cnode to prevent decmpfs state changes
1280 * on this file if it's locked
1281 */
1282 dp = hfs_lazy_init_decmpfs_cnode(VTOC(vp));
1283 if (!dp) {
1284 /* failed to allocate a decmpfs_cnode */
1285 return ENOMEM; /* what should this be? */
1286 }
1287 }
1288 decmpfs_cnode_set_vnode_state(dp, FILE_TYPE_UNKNOWN, 0);
1289 }
1290#endif
1291 }
9bccf70c
A
1292 return (error);
1293}
1294
1295
1296/*
1297 * Change the mode on a file.
1298 * cnode must be locked before calling.
1299 */
55e303ae 1300__private_extern__
9bccf70c 1301int
2d21ac55 1302hfs_chmod(struct vnode *vp, int mode, __unused kauth_cred_t cred, __unused struct proc *p)
9bccf70c
A
1303{
1304 register struct cnode *cp = VTOC(vp);
9bccf70c
A
1305
1306 if (VTOVCB(vp)->vcbSigWord != kHFSPlusSigWord)
1307 return (0);
1308
b4c24cb9
A
1309 // XXXdbg - don't allow modification of the journal or journal_info_block
1310 if (VTOHFS(vp)->jnl && cp && cp->c_datafork) {
1311 struct HFSPlusExtentDescriptor *extd;
1312
55e303ae 1313 extd = &cp->c_datafork->ff_extents[0];
b4c24cb9
A
1314 if (extd->startBlock == VTOVCB(vp)->vcbJinfoBlock || extd->startBlock == VTOHFS(vp)->jnl_start) {
1315 return EPERM;
1316 }
1317 }
1318
9bccf70c 1319#if OVERRIDE_UNKNOWN_PERMISSIONS
91447636 1320 if (((unsigned int)vfs_flags(VTOVFS(vp))) & MNT_UNKNOWNPERMISSIONS) {
9bccf70c
A
1321 return (0);
1322 };
1323#endif
9bccf70c
A
1324 cp->c_mode &= ~ALLPERMS;
1325 cp->c_mode |= (mode & ALLPERMS);
91447636 1326 cp->c_touch_chgtime = TRUE;
9bccf70c
A
1327 return (0);
1328}
1329
1330
55e303ae 1331__private_extern__
9bccf70c 1332int
91447636 1333hfs_write_access(struct vnode *vp, kauth_cred_t cred, struct proc *p, Boolean considerFlags)
9bccf70c
A
1334{
1335 struct cnode *cp = VTOC(vp);
9bccf70c 1336 int retval = 0;
91447636 1337 int is_member;
9bccf70c
A
1338
1339 /*
1340 * Disallow write attempts on read-only file systems;
1341 * unless the file is a socket, fifo, or a block or
1342 * character device resident on the file system.
1343 */
91447636 1344 switch (vnode_vtype(vp)) {
9bccf70c
A
1345 case VDIR:
1346 case VLNK:
1347 case VREG:
55e303ae 1348 if (VTOHFS(vp)->hfs_flags & HFS_READ_ONLY)
9bccf70c 1349 return (EROFS);
55e303ae 1350 break;
9bccf70c
A
1351 default:
1352 break;
1353 }
1354
1355 /* If immutable bit set, nobody gets to write it. */
1356 if (considerFlags && (cp->c_flags & IMMUTABLE))
1357 return (EPERM);
1358
1359 /* Otherwise, user id 0 always gets access. */
91447636 1360 if (!suser(cred, NULL))
9bccf70c
A
1361 return (0);
1362
1363 /* Otherwise, check the owner. */
1364 if ((retval = hfs_owner_rights(VTOHFS(vp), cp->c_uid, cred, p, false)) == 0)
1365 return ((cp->c_mode & S_IWUSR) == S_IWUSR ? 0 : EACCES);
1366
1367 /* Otherwise, check the groups. */
91447636
A
1368 if (kauth_cred_ismember_gid(cred, cp->c_gid, &is_member) == 0 && is_member) {
1369 return ((cp->c_mode & S_IWGRP) == S_IWGRP ? 0 : EACCES);
9bccf70c
A
1370 }
1371
1372 /* Otherwise, check everyone else. */
1373 return ((cp->c_mode & S_IWOTH) == S_IWOTH ? 0 : EACCES);
1374}
1375
1376
9bccf70c
A
1377/*
1378 * Perform chown operation on cnode cp;
1379 * code must be locked prior to call.
1380 */
55e303ae 1381__private_extern__
9bccf70c 1382int
2d21ac55
A
1383#if !QUOTA
1384hfs_chown(struct vnode *vp, uid_t uid, gid_t gid, __unused kauth_cred_t cred,
1385 __unused struct proc *p)
1386#else
91447636 1387hfs_chown(struct vnode *vp, uid_t uid, gid_t gid, kauth_cred_t cred,
2d21ac55
A
1388 __unused struct proc *p)
1389#endif
9bccf70c
A
1390{
1391 register struct cnode *cp = VTOC(vp);
1392 uid_t ouid;
1393 gid_t ogid;
9bccf70c 1394#if QUOTA
2d21ac55 1395 int error = 0;
9bccf70c
A
1396 register int i;
1397 int64_t change;
1398#endif /* QUOTA */
1399
1400 if (VTOVCB(vp)->vcbSigWord != kHFSPlusSigWord)
91447636 1401 return (ENOTSUP);
9bccf70c 1402
91447636 1403 if (((unsigned int)vfs_flags(VTOVFS(vp))) & MNT_UNKNOWNPERMISSIONS)
9bccf70c
A
1404 return (0);
1405
1406 if (uid == (uid_t)VNOVAL)
1407 uid = cp->c_uid;
1408 if (gid == (gid_t)VNOVAL)
1409 gid = cp->c_gid;
91447636
A
1410
1411#if 0 /* we are guaranteed that this is already the case */
9bccf70c
A
1412 /*
1413 * If we don't own the file, are trying to change the owner
1414 * of the file, or are not a member of the target group,
1415 * the caller must be superuser or the call fails.
1416 */
91447636
A
1417 if ((kauth_cred_getuid(cred) != cp->c_uid || uid != cp->c_uid ||
1418 (gid != cp->c_gid &&
1419 (kauth_cred_ismember_gid(cred, gid, &is_member) || !is_member))) &&
1420 (error = suser(cred, 0)))
9bccf70c 1421 return (error);
91447636 1422#endif
9bccf70c
A
1423
1424 ogid = cp->c_gid;
1425 ouid = cp->c_uid;
1426#if QUOTA
1427 if ((error = hfs_getinoquota(cp)))
1428 return (error);
1429 if (ouid == uid) {
91447636 1430 dqrele(cp->c_dquot[USRQUOTA]);
9bccf70c
A
1431 cp->c_dquot[USRQUOTA] = NODQUOT;
1432 }
1433 if (ogid == gid) {
91447636 1434 dqrele(cp->c_dquot[GRPQUOTA]);
9bccf70c
A
1435 cp->c_dquot[GRPQUOTA] = NODQUOT;
1436 }
1437
1438 /*
1439 * Eventually need to account for (fake) a block per directory
91447636
A
1440 * if (vnode_isdir(vp))
1441 * change = VTOHFS(vp)->blockSize;
1442 * else
9bccf70c
A
1443 */
1444
1445 change = (int64_t)(cp->c_blocks) * (int64_t)VTOVCB(vp)->blockSize;
1446 (void) hfs_chkdq(cp, -change, cred, CHOWN);
1447 (void) hfs_chkiq(cp, -1, cred, CHOWN);
1448 for (i = 0; i < MAXQUOTAS; i++) {
91447636 1449 dqrele(cp->c_dquot[i]);
9bccf70c
A
1450 cp->c_dquot[i] = NODQUOT;
1451 }
1452#endif /* QUOTA */
1453 cp->c_gid = gid;
1454 cp->c_uid = uid;
1455#if QUOTA
1456 if ((error = hfs_getinoquota(cp)) == 0) {
1457 if (ouid == uid) {
91447636 1458 dqrele(cp->c_dquot[USRQUOTA]);
9bccf70c
A
1459 cp->c_dquot[USRQUOTA] = NODQUOT;
1460 }
1461 if (ogid == gid) {
91447636 1462 dqrele(cp->c_dquot[GRPQUOTA]);
9bccf70c
A
1463 cp->c_dquot[GRPQUOTA] = NODQUOT;
1464 }
1465 if ((error = hfs_chkdq(cp, change, cred, CHOWN)) == 0) {
1466 if ((error = hfs_chkiq(cp, 1, cred, CHOWN)) == 0)
1467 goto good;
1468 else
1469 (void) hfs_chkdq(cp, -change, cred, CHOWN|FORCE);
1470 }
1471 for (i = 0; i < MAXQUOTAS; i++) {
91447636 1472 dqrele(cp->c_dquot[i]);
9bccf70c
A
1473 cp->c_dquot[i] = NODQUOT;
1474 }
1475 }
1476 cp->c_gid = ogid;
1477 cp->c_uid = ouid;
1478 if (hfs_getinoquota(cp) == 0) {
1479 if (ouid == uid) {
91447636 1480 dqrele(cp->c_dquot[USRQUOTA]);
9bccf70c
A
1481 cp->c_dquot[USRQUOTA] = NODQUOT;
1482 }
1483 if (ogid == gid) {
91447636 1484 dqrele(cp->c_dquot[GRPQUOTA]);
9bccf70c
A
1485 cp->c_dquot[GRPQUOTA] = NODQUOT;
1486 }
1487 (void) hfs_chkdq(cp, change, cred, FORCE|CHOWN);
1488 (void) hfs_chkiq(cp, 1, cred, FORCE|CHOWN);
1489 (void) hfs_getinoquota(cp);
1490 }
1491 return (error);
1492good:
1493 if (hfs_getinoquota(cp))
1494 panic("hfs_chown: lost quota");
1495#endif /* QUOTA */
1496
2d21ac55
A
1497
1498 /*
1499 According to the SUSv3 Standard, chown() shall mark
1500 for update the st_ctime field of the file.
1501 (No exceptions mentioned)
1502 */
91447636 1503 cp->c_touch_chgtime = TRUE;
9bccf70c
A
1504 return (0);
1505}
1506
1507
1508/*
91447636
A
1509 * The hfs_exchange routine swaps the fork data in two files by
1510 * exchanging some of the information in the cnode. It is used
1511 * to preserve the file ID when updating an existing file, in
1512 * case the file is being tracked through its file ID. Typically
1513 * its used after creating a new file during a safe-save.
9bccf70c 1514 */
9bccf70c 1515static int
91447636
A
1516hfs_vnop_exchange(ap)
1517 struct vnop_exchange_args /* {
9bccf70c
A
1518 struct vnode *a_fvp;
1519 struct vnode *a_tvp;
91447636
A
1520 int a_options;
1521 vfs_context_t a_context;
9bccf70c
A
1522 } */ *ap;
1523{
1524 struct vnode *from_vp = ap->a_fvp;
1525 struct vnode *to_vp = ap->a_tvp;
91447636
A
1526 struct cnode *from_cp;
1527 struct cnode *to_cp;
1528 struct hfsmount *hfsmp;
9bccf70c
A
1529 struct cat_desc tempdesc;
1530 struct cat_attr tempattr;
2d21ac55
A
1531 const unsigned char *from_nameptr;
1532 const unsigned char *to_nameptr;
1533 char from_iname[32];
1534 char to_iname[32];
1535 u_int32_t tempflag;
1536 cnid_t from_parid;
1537 cnid_t to_parid;
91447636
A
1538 int lockflags;
1539 int error = 0, started_tr = 0, got_cookie = 0;
1540 cat_cookie_t cookie;
9bccf70c
A
1541
1542 /* The files must be on the same volume. */
91447636 1543 if (vnode_mount(from_vp) != vnode_mount(to_vp))
9bccf70c
A
1544 return (EXDEV);
1545
91447636
A
1546 if (from_vp == to_vp)
1547 return (EINVAL);
1548
b0d623f7
A
1549#if HFS_COMPRESSION
1550 if ( hfs_file_is_compressed(VTOC(from_vp), 0) ) {
1551 if ( 0 != ( error = decmpfs_decompress_file(from_vp, VTOCMP(from_vp), -1, 0, 1) ) ) {
1552 return error;
1553 }
1554 }
1555
1556 if ( hfs_file_is_compressed(VTOC(to_vp), 0) ) {
1557 if ( 0 != ( error = decmpfs_decompress_file(to_vp, VTOCMP(to_vp), -1, 0, 1) ) ) {
1558 return error;
1559 }
1560 }
1561#endif // HFS_COMPRESSION
1562
91447636
A
1563 if ((error = hfs_lockpair(VTOC(from_vp), VTOC(to_vp), HFS_EXCLUSIVE_LOCK)))
1564 return (error);
1565
1566 from_cp = VTOC(from_vp);
1567 to_cp = VTOC(to_vp);
1568 hfsmp = VTOHFS(from_vp);
1569
9bccf70c 1570 /* Only normal files can be exchanged. */
91447636 1571 if (!vnode_isreg(from_vp) || !vnode_isreg(to_vp) ||
91447636
A
1572 VNODE_IS_RSRC(from_vp) || VNODE_IS_RSRC(to_vp)) {
1573 error = EINVAL;
1574 goto exit;
1575 }
9bccf70c 1576
b4c24cb9
A
1577 // XXXdbg - don't allow modification of the journal or journal_info_block
1578 if (hfsmp->jnl) {
1579 struct HFSPlusExtentDescriptor *extd;
1580
1581 if (from_cp->c_datafork) {
55e303ae 1582 extd = &from_cp->c_datafork->ff_extents[0];
b4c24cb9 1583 if (extd->startBlock == VTOVCB(from_vp)->vcbJinfoBlock || extd->startBlock == hfsmp->jnl_start) {
91447636
A
1584 error = EPERM;
1585 goto exit;
b4c24cb9
A
1586 }
1587 }
1588
1589 if (to_cp->c_datafork) {
55e303ae 1590 extd = &to_cp->c_datafork->ff_extents[0];
b4c24cb9 1591 if (extd->startBlock == VTOVCB(to_vp)->vcbJinfoBlock || extd->startBlock == hfsmp->jnl_start) {
91447636
A
1592 error = EPERM;
1593 goto exit;
b4c24cb9
A
1594 }
1595 }
1596 }
1597
91447636
A
1598 if ((error = hfs_start_transaction(hfsmp)) != 0) {
1599 goto exit;
b4c24cb9 1600 }
91447636 1601 started_tr = 1;
b4c24cb9 1602
55e303ae
A
1603 /*
1604 * Reserve some space in the Catalog file.
1605 */
91447636
A
1606 if ((error = cat_preflight(hfsmp, CAT_EXCHANGE, &cookie, vfs_context_proc(ap->a_context)))) {
1607 goto exit;
55e303ae 1608 }
91447636 1609 got_cookie = 1;
9bccf70c
A
1610
1611 /* The backend code always tries to delete the virtual
2d21ac55 1612 * extent id for exchanging files so we need to lock
9bccf70c
A
1613 * the extents b-tree.
1614 */
91447636 1615 lockflags = hfs_systemfile_lock(hfsmp, SFL_CATALOG | SFL_EXTENTS | SFL_ATTRIBUTE, HFS_EXCLUSIVE_LOCK);
9bccf70c 1616
2d21ac55
A
1617 /* Account for the location of the catalog objects. */
1618 if (from_cp->c_flag & C_HARDLINK) {
1619 MAKE_INODE_NAME(from_iname, sizeof(from_iname),
1620 from_cp->c_attr.ca_linkref);
1621 from_nameptr = (unsigned char *)from_iname;
1622 from_parid = hfsmp->hfs_private_desc[FILE_HARDLINKS].cd_cnid;
1623 from_cp->c_hint = 0;
1624 } else {
1625 from_nameptr = from_cp->c_desc.cd_nameptr;
1626 from_parid = from_cp->c_parentcnid;
1627 }
1628 if (to_cp->c_flag & C_HARDLINK) {
1629 MAKE_INODE_NAME(to_iname, sizeof(to_iname),
1630 to_cp->c_attr.ca_linkref);
1631 to_nameptr = (unsigned char *)to_iname;
1632 to_parid = hfsmp->hfs_private_desc[FILE_HARDLINKS].cd_cnid;
1633 to_cp->c_hint = 0;
1634 } else {
1635 to_nameptr = to_cp->c_desc.cd_nameptr;
1636 to_parid = to_cp->c_parentcnid;
1637 }
1638
9bccf70c 1639 /* Do the exchange */
2d21ac55
A
1640 error = ExchangeFileIDs(hfsmp, from_nameptr, to_nameptr, from_parid,
1641 to_parid, from_cp->c_hint, to_cp->c_hint);
91447636 1642 hfs_systemfile_unlock(hfsmp, lockflags);
9bccf70c 1643
91447636
A
1644 /*
1645 * Note that we don't need to exchange any extended attributes
1646 * since the attributes are keyed by file ID.
1647 */
9bccf70c
A
1648
1649 if (error != E_NONE) {
91447636
A
1650 error = MacToVFSError(error);
1651 goto exit;
9bccf70c
A
1652 }
1653
1654 /* Purge the vnodes from the name cache */
1655 if (from_vp)
1656 cache_purge(from_vp);
1657 if (to_vp)
1658 cache_purge(to_vp);
1659
1660 /* Save a copy of from attributes before swapping. */
1661 bcopy(&from_cp->c_desc, &tempdesc, sizeof(struct cat_desc));
1662 bcopy(&from_cp->c_attr, &tempattr, sizeof(struct cat_attr));
2d21ac55 1663 tempflag = from_cp->c_flag & (C_HARDLINK | C_HASXATTRS);
9bccf70c
A
1664
1665 /*
1666 * Swap the descriptors and all non-fork related attributes.
1667 * (except the modify date)
1668 */
1669 bcopy(&to_cp->c_desc, &from_cp->c_desc, sizeof(struct cat_desc));
b4c24cb9 1670
9bccf70c
A
1671 from_cp->c_hint = 0;
1672 from_cp->c_fileid = from_cp->c_cnid;
1673 from_cp->c_itime = to_cp->c_itime;
1674 from_cp->c_btime = to_cp->c_btime;
1675 from_cp->c_atime = to_cp->c_atime;
1676 from_cp->c_ctime = to_cp->c_ctime;
1677 from_cp->c_gid = to_cp->c_gid;
1678 from_cp->c_uid = to_cp->c_uid;
1679 from_cp->c_flags = to_cp->c_flags;
1680 from_cp->c_mode = to_cp->c_mode;
2d21ac55
A
1681 from_cp->c_linkcount = to_cp->c_linkcount;
1682 from_cp->c_flag = to_cp->c_flag & (C_HARDLINK | C_HASXATTRS);
3a60a9f5 1683 from_cp->c_attr.ca_recflags = to_cp->c_attr.ca_recflags;
9bccf70c
A
1684 bcopy(to_cp->c_finderinfo, from_cp->c_finderinfo, 32);
1685
1686 bcopy(&tempdesc, &to_cp->c_desc, sizeof(struct cat_desc));
1687 to_cp->c_hint = 0;
1688 to_cp->c_fileid = to_cp->c_cnid;
1689 to_cp->c_itime = tempattr.ca_itime;
1690 to_cp->c_btime = tempattr.ca_btime;
1691 to_cp->c_atime = tempattr.ca_atime;
1692 to_cp->c_ctime = tempattr.ca_ctime;
1693 to_cp->c_gid = tempattr.ca_gid;
1694 to_cp->c_uid = tempattr.ca_uid;
1695 to_cp->c_flags = tempattr.ca_flags;
1696 to_cp->c_mode = tempattr.ca_mode;
2d21ac55
A
1697 to_cp->c_linkcount = tempattr.ca_linkcount;
1698 to_cp->c_flag = tempflag;
3a60a9f5 1699 to_cp->c_attr.ca_recflags = tempattr.ca_recflags;
9bccf70c
A
1700 bcopy(tempattr.ca_finderinfo, to_cp->c_finderinfo, 32);
1701
91447636 1702 /* Rehash the cnodes using their new file IDs */
b0d623f7 1703 hfs_chash_rehash(hfsmp, from_cp, to_cp);
90556fb8
A
1704
1705 /*
1706 * When a file moves out of "Cleanup At Startup"
1707 * we can drop its NODUMP status.
1708 */
1709 if ((from_cp->c_flags & UF_NODUMP) &&
1710 (from_cp->c_parentcnid != to_cp->c_parentcnid)) {
1711 from_cp->c_flags &= ~UF_NODUMP;
91447636 1712 from_cp->c_touch_chgtime = TRUE;
90556fb8 1713 }
90556fb8
A
1714 if ((to_cp->c_flags & UF_NODUMP) &&
1715 (to_cp->c_parentcnid != from_cp->c_parentcnid)) {
1716 to_cp->c_flags &= ~UF_NODUMP;
91447636 1717 to_cp->c_touch_chgtime = TRUE;
90556fb8
A
1718 }
1719
91447636
A
1720exit:
1721 if (got_cookie) {
1722 cat_postflight(hfsmp, &cookie, vfs_context_proc(ap->a_context));
b4c24cb9 1723 }
91447636
A
1724 if (started_tr) {
1725 hfs_end_transaction(hfsmp);
b4c24cb9
A
1726 }
1727
91447636 1728 hfs_unlockpair(from_cp, to_cp);
9bccf70c
A
1729 return (error);
1730}
1731
1732
1733/*
91447636
A
1734 * cnode must be locked
1735 */
1736__private_extern__
1737int
1738hfs_fsync(struct vnode *vp, int waitfor, int fullsync, struct proc *p)
9bccf70c 1739{
9bccf70c
A
1740 struct cnode *cp = VTOC(vp);
1741 struct filefork *fp = NULL;
1742 int retval = 0;
91447636 1743 struct hfsmount *hfsmp = VTOHFS(vp);
b0d623f7 1744 struct rl_entry *invalid_range;
9bccf70c 1745 struct timeval tv;
b0d623f7
A
1746 int waitdata; /* attributes necessary for data retrieval */
1747 int wait; /* all other attributes (e.g. atime, etc.) */
91447636
A
1748 int lockflag;
1749 int took_trunc_lock = 0;
b0d623f7 1750 boolean_t trunc_lock_exclusive = FALSE;
9bccf70c 1751
b0d623f7
A
1752 /*
1753 * Applications which only care about data integrity rather than full
1754 * file integrity may opt out of (delay) expensive metadata update
1755 * operations as a performance optimization.
1756 */
91447636 1757 wait = (waitfor == MNT_WAIT);
b0d623f7 1758 waitdata = (waitfor == MNT_DWAIT) | wait;
2d21ac55
A
1759 if (always_do_fullfsync)
1760 fullsync = 1;
1761
9bccf70c 1762 /* HFS directories don't have any data blocks. */
91447636 1763 if (vnode_isdir(vp))
9bccf70c 1764 goto metasync;
b0d623f7 1765 fp = VTOF(vp);
9bccf70c
A
1766
1767 /*
1768 * For system files flush the B-tree header and
1769 * for regular files write out any clusters
1770 */
91447636 1771 if (vnode_issystem(vp)) {
b4c24cb9
A
1772 if (VTOF(vp)->fcbBTCBPtr != NULL) {
1773 // XXXdbg
55e303ae 1774 if (hfsmp->jnl == NULL) {
b4c24cb9
A
1775 BTFlushPath(VTOF(vp));
1776 }
1777 }
91447636
A
1778 } else if (UBCINFOEXISTS(vp)) {
1779 hfs_unlock(cp);
b0d623f7 1780 hfs_lock_truncate(cp, trunc_lock_exclusive);
91447636 1781 took_trunc_lock = 1;
9bccf70c 1782
b0d623f7
A
1783 if (fp->ff_unallocblocks != 0) {
1784 hfs_unlock_truncate(cp, trunc_lock_exclusive);
1785
1786 trunc_lock_exclusive = TRUE;
1787 hfs_lock_truncate(cp, trunc_lock_exclusive);
1788 }
91447636 1789 /* Don't hold cnode lock when calling into cluster layer. */
b0d623f7 1790 (void) cluster_push(vp, waitdata ? IO_SYNC : 0);
91447636
A
1791
1792 hfs_lock(cp, HFS_FORCE_LOCK);
1793 }
9bccf70c
A
1794 /*
1795 * When MNT_WAIT is requested and the zero fill timeout
1796 * has expired then we must explicitly zero out any areas
1797 * that are currently marked invalid (holes).
90556fb8
A
1798 *
1799 * Files with NODUMP can bypass zero filling here.
9bccf70c 1800 */
b0d623f7
A
1801 if (fp && (((cp->c_flag & C_ALWAYS_ZEROFILL) && !TAILQ_EMPTY(&fp->ff_invalidranges)) ||
1802 ((wait || (cp->c_flag & C_ZFWANTSYNC)) &&
1803 ((cp->c_flags & UF_NODUMP) == 0) &&
1804 UBCINFOEXISTS(vp) && (vnode_issystem(vp) ==0) &&
1805 cp->c_zftimeout != 0))) {
1806
91447636 1807 microuptime(&tv);
b0d623f7 1808 if ((cp->c_flag & C_ALWAYS_ZEROFILL) == 0 && !fullsync && tv.tv_sec < (long)cp->c_zftimeout) {
9bccf70c
A
1809 /* Remember that a force sync was requested. */
1810 cp->c_flag |= C_ZFWANTSYNC;
91447636
A
1811 goto datasync;
1812 }
b0d623f7
A
1813 if (!TAILQ_EMPTY(&fp->ff_invalidranges)) {
1814 if (!took_trunc_lock || trunc_lock_exclusive == FALSE) {
1815 hfs_unlock(cp);
1816 if (took_trunc_lock)
1817 hfs_unlock_truncate(cp, trunc_lock_exclusive);
9bccf70c 1818
b0d623f7
A
1819 trunc_lock_exclusive = TRUE;
1820 hfs_lock_truncate(cp, trunc_lock_exclusive);
1821 hfs_lock(cp, HFS_FORCE_LOCK);
1822 took_trunc_lock = 1;
1823 }
1824 while ((invalid_range = TAILQ_FIRST(&fp->ff_invalidranges))) {
1825 off_t start = invalid_range->rl_start;
1826 off_t end = invalid_range->rl_end;
9bccf70c 1827
b0d623f7
A
1828 /* The range about to be written must be validated
1829 * first, so that VNOP_BLOCKMAP() will return the
1830 * appropriate mapping for the cluster code:
1831 */
1832 rl_remove(start, end, &fp->ff_invalidranges);
9bccf70c 1833
b0d623f7
A
1834 /* Don't hold cnode lock when calling into cluster layer. */
1835 hfs_unlock(cp);
1836 (void) cluster_write(vp, (struct uio *) 0,
1837 fp->ff_size, end + 1, start, (off_t)0,
1838 IO_HEADZEROFILL | IO_NOZERODIRTY | IO_NOCACHE);
1839 hfs_lock(cp, HFS_FORCE_LOCK);
1840 cp->c_flag |= C_MODIFIED;
1841 }
91447636 1842 hfs_unlock(cp);
b0d623f7 1843 (void) cluster_push(vp, waitdata ? IO_SYNC : 0);
91447636 1844 hfs_lock(cp, HFS_FORCE_LOCK);
9bccf70c 1845 }
9bccf70c
A
1846 cp->c_flag &= ~C_ZFWANTSYNC;
1847 cp->c_zftimeout = 0;
1848 }
91447636 1849datasync:
b0d623f7
A
1850 if (took_trunc_lock) {
1851 hfs_unlock_truncate(cp, trunc_lock_exclusive);
1852 took_trunc_lock = 0;
1853 }
91447636
A
1854 /*
1855 * if we have a journal and if journal_active() returns != 0 then the
1856 * we shouldn't do anything to a locked block (because it is part
1857 * of a transaction). otherwise we'll just go through the normal
1858 * code path and flush the buffer. note journal_active() can return
1859 * -1 if the journal is invalid -- however we still need to skip any
1860 * locked blocks as they get cleaned up when we finish the transaction
1861 * or close the journal.
1862 */
1863 // if (hfsmp->jnl && journal_active(hfsmp->jnl) >= 0)
1864 if (hfsmp->jnl)
1865 lockflag = BUF_SKIP_LOCKED;
1866 else
1867 lockflag = 0;
9bccf70c
A
1868
1869 /*
1870 * Flush all dirty buffers associated with a vnode.
1871 */
b0d623f7 1872 buf_flushdirtyblks(vp, waitdata, lockflag, "hfs_fsync");
9bccf70c
A
1873
1874metasync:
91447636
A
1875 if (vnode_isreg(vp) && vnode_issystem(vp)) {
1876 if (VTOF(vp)->fcbBTCBPtr != NULL) {
1877 microuptime(&tv);
9bccf70c 1878 BTSetLastSync(VTOF(vp), tv.tv_sec);
91447636
A
1879 }
1880 cp->c_touch_acctime = FALSE;
1881 cp->c_touch_chgtime = FALSE;
1882 cp->c_touch_modtime = FALSE;
3a60a9f5 1883 } else if ( !(vp->v_flag & VSWAP) ) /* User file */ {
91447636 1884 retval = hfs_update(vp, wait);
9bccf70c 1885
2d21ac55
A
1886 /*
1887 * When MNT_WAIT is requested push out the catalog record for
1888 * this file. If they asked for a full fsync, we can skip this
1889 * because the journal_flush or hfs_metasync_all will push out
1890 * all of the metadata changes.
1891 */
1892 if ((retval == 0) && wait && !fullsync && cp->c_hint &&
9bccf70c 1893 !ISSET(cp->c_flag, C_DELETED | C_NOEXISTS)) {
91447636 1894 hfs_metasync(VTOHFS(vp), (daddr64_t)cp->c_hint, p);
2d21ac55 1895 }
55e303ae 1896
2d21ac55
A
1897 /*
1898 * If this was a full fsync, make sure all metadata
1899 * changes get to stable storage.
1900 */
1901 if (fullsync) {
55e303ae 1902 if (hfsmp->jnl) {
b0d623f7 1903 hfs_journal_flush(hfsmp);
55e303ae 1904 } else {
2d21ac55 1905 retval = hfs_metasync_all(hfsmp);
91447636
A
1906 /* XXX need to pass context! */
1907 VNOP_IOCTL(hfsmp->hfs_devvp, DKIOCSYNCHRONIZECACHE, NULL, FWRITE, NULL);
55e303ae
A
1908 }
1909 }
9bccf70c
A
1910 }
1911
1912 return (retval);
1913}
1914
91447636 1915
9bccf70c
A
1916/* Sync an hfs catalog b-tree node */
1917static int
2d21ac55 1918hfs_metasync(struct hfsmount *hfsmp, daddr64_t node, __unused struct proc *p)
9bccf70c 1919{
91447636
A
1920 vnode_t vp;
1921 buf_t bp;
1922 int lockflags;
9bccf70c
A
1923
1924 vp = HFSTOVCB(hfsmp)->catalogRefNum;
1925
b4c24cb9
A
1926 // XXXdbg - don't need to do this on a journaled volume
1927 if (hfsmp->jnl) {
1928 return 0;
1929 }
1930
91447636 1931 lockflags = hfs_systemfile_lock(hfsmp, SFL_CATALOG, HFS_EXCLUSIVE_LOCK);
9bccf70c
A
1932 /*
1933 * Look for a matching node that has been delayed
1934 * but is not part of a set (B_LOCKED).
91447636
A
1935 *
1936 * BLK_ONLYVALID causes buf_getblk to return a
1937 * buf_t for the daddr64_t specified only if it's
1938 * currently resident in the cache... the size
1939 * parameter to buf_getblk is ignored when this flag
1940 * is set
9bccf70c 1941 */
91447636
A
1942 bp = buf_getblk(vp, node, 0, 0, 0, BLK_META | BLK_ONLYVALID);
1943
1944 if (bp) {
1945 if ((buf_flags(bp) & (B_LOCKED | B_DELWRI)) == B_DELWRI)
1946 (void) VNOP_BWRITE(bp);
1947 else
1948 buf_brelse(bp);
9bccf70c 1949 }
91447636
A
1950
1951 hfs_systemfile_unlock(hfsmp, lockflags);
9bccf70c
A
1952
1953 return (0);
1954}
1955
91447636 1956
2d21ac55
A
1957/*
1958 * Sync all hfs B-trees. Use this instead of journal_flush for a volume
1959 * without a journal. Note that the volume bitmap does not get written;
1960 * we rely on fsck_hfs to fix that up (which it can do without any loss
1961 * of data).
1962 */
1963static int
1964hfs_metasync_all(struct hfsmount *hfsmp)
1965{
1966 int lockflags;
1967
1968 /* Lock all of the B-trees so we get a mutually consistent state */
1969 lockflags = hfs_systemfile_lock(hfsmp,
1970 SFL_CATALOG|SFL_EXTENTS|SFL_ATTRIBUTE, HFS_EXCLUSIVE_LOCK);
1971
1972 /* Sync each of the B-trees */
1973 if (hfsmp->hfs_catalog_vp)
1974 hfs_btsync(hfsmp->hfs_catalog_vp, 0);
1975 if (hfsmp->hfs_extents_vp)
1976 hfs_btsync(hfsmp->hfs_extents_vp, 0);
1977 if (hfsmp->hfs_attribute_vp)
1978 hfs_btsync(hfsmp->hfs_attribute_vp, 0);
1979
1980 /* Wait for all of the writes to complete */
1981 if (hfsmp->hfs_catalog_vp)
1982 vnode_waitforwrites(hfsmp->hfs_catalog_vp, 0, 0, 0, "hfs_metasync_all");
1983 if (hfsmp->hfs_extents_vp)
1984 vnode_waitforwrites(hfsmp->hfs_extents_vp, 0, 0, 0, "hfs_metasync_all");
1985 if (hfsmp->hfs_attribute_vp)
1986 vnode_waitforwrites(hfsmp->hfs_attribute_vp, 0, 0, 0, "hfs_metasync_all");
1987
1988 hfs_systemfile_unlock(hfsmp, lockflags);
1989
1990 return 0;
1991}
1992
1993
91447636
A
1994/*ARGSUSED 1*/
1995static int
2d21ac55 1996hfs_btsync_callback(struct buf *bp, __unused void *dummy)
91447636
A
1997{
1998 buf_clearflags(bp, B_LOCKED);
1999 (void) buf_bawrite(bp);
2000
2001 return(BUF_CLAIMED);
2002}
2003
2004
9bccf70c
A
2005__private_extern__
2006int
2007hfs_btsync(struct vnode *vp, int sync_transaction)
2008{
2009 struct cnode *cp = VTOC(vp);
9bccf70c 2010 struct timeval tv;
91447636 2011 int flags = 0;
9bccf70c 2012
91447636
A
2013 if (sync_transaction)
2014 flags |= BUF_SKIP_NONLOCKED;
9bccf70c
A
2015 /*
2016 * Flush all dirty buffers associated with b-tree.
2017 */
91447636 2018 buf_iterate(vp, hfs_btsync_callback, flags, 0);
b4c24cb9 2019
91447636
A
2020 microuptime(&tv);
2021 if (vnode_issystem(vp) && (VTOF(vp)->fcbBTCBPtr != NULL))
9bccf70c 2022 (void) BTSetLastSync(VTOF(vp), tv.tv_sec);
91447636
A
2023 cp->c_touch_acctime = FALSE;
2024 cp->c_touch_chgtime = FALSE;
2025 cp->c_touch_modtime = FALSE;
9bccf70c
A
2026
2027 return 0;
2028}
2029
2030/*
91447636 2031 * Remove a directory.
9bccf70c
A
2032 */
2033static int
91447636
A
2034hfs_vnop_rmdir(ap)
2035 struct vnop_rmdir_args /* {
9bccf70c
A
2036 struct vnode *a_dvp;
2037 struct vnode *a_vp;
2038 struct componentname *a_cnp;
91447636 2039 vfs_context_t a_context;
9bccf70c
A
2040 } */ *ap;
2041{
91447636
A
2042 struct vnode *dvp = ap->a_dvp;
2043 struct vnode *vp = ap->a_vp;
2d21ac55
A
2044 struct cnode *dcp = VTOC(dvp);
2045 struct cnode *cp = VTOC(vp);
91447636
A
2046 int error;
2047
2d21ac55 2048 if (!S_ISDIR(cp->c_mode)) {
91447636
A
2049 return (ENOTDIR);
2050 }
2051 if (dvp == vp) {
2052 return (EINVAL);
2053 }
2d21ac55 2054 if ((error = hfs_lockpair(dcp, cp, HFS_EXCLUSIVE_LOCK))) {
91447636 2055 return (error);
2d21ac55 2056 }
b0d623f7
A
2057
2058 /* Check for a race with rmdir on the parent directory */
2059 if (dcp->c_flag & (C_DELETED | C_NOEXISTS)) {
2060 hfs_unlockpair (dcp, cp);
2061 return ENOENT;
2062 }
91447636 2063 error = hfs_removedir(dvp, vp, ap->a_cnp, 0);
b0d623f7 2064
2d21ac55 2065 hfs_unlockpair(dcp, cp);
91447636
A
2066
2067 return (error);
55e303ae
A
2068}
2069
2070/*
91447636
A
2071 * Remove a directory
2072 *
2073 * Both dvp and vp cnodes are locked
55e303ae
A
2074 */
2075static int
91447636
A
2076hfs_removedir(struct vnode *dvp, struct vnode *vp, struct componentname *cnp,
2077 int skip_reserve)
55e303ae 2078{
9bccf70c
A
2079 struct cnode *cp;
2080 struct cnode *dcp;
2081 struct hfsmount * hfsmp;
91447636 2082 struct cat_desc desc;
91447636 2083 int lockflags;
2d21ac55 2084 int error = 0, started_tr = 0;
9bccf70c
A
2085
2086 cp = VTOC(vp);
2087 dcp = VTOC(dvp);
2088 hfsmp = VTOHFS(vp);
2089
2d21ac55 2090 if (dcp == cp) {
9bccf70c 2091 return (EINVAL); /* cannot remove "." */
2d21ac55
A
2092 }
2093 if (cp->c_flag & (C_NOEXISTS | C_DELETED)) {
2094 return (0);
2095 }
2096 if (cp->c_entries != 0) {
2097 return (ENOTEMPTY);
2098 }
2099
2100 /* Check if we're removing the last link to an empty directory. */
2101 if (cp->c_flag & C_HARDLINK) {
2102 /* We could also return EBUSY here */
2103 return hfs_unlink(hfsmp, dvp, vp, cnp, skip_reserve);
2104 }
b0d623f7
A
2105
2106 /*
2107 * We want to make sure that if the directory has a lot of attributes, we process them
2108 * in separate transactions to ensure we don't panic in the journal with a gigantic
2109 * transaction. This means we'll let hfs_removefile deal with the directory, which generally
2110 * follows the same codepath as open-unlinked files. Note that the last argument to
2111 * hfs_removefile specifies that it is supposed to handle directories for this case.
2112 */
2d21ac55
A
2113 if ((hfsmp->hfs_attribute_vp != NULL) &&
2114 (cp->c_attr.ca_recflags & kHFSHasAttributesMask) != 0) {
2115
b0d623f7 2116 return hfs_removefile(dvp, vp, cnp, 0, 0, 1, NULL);
2d21ac55
A
2117 }
2118
2119 dcp->c_flag |= C_DIR_MODIFICATION;
b4c24cb9 2120
d7e50217 2121#if QUOTA
2d21ac55
A
2122 if (hfsmp->hfs_flags & HFS_QUOTAS)
2123 (void)hfs_getinoquota(cp);
d7e50217 2124#endif
91447636
A
2125 if ((error = hfs_start_transaction(hfsmp)) != 0) {
2126 goto out;
b4c24cb9 2127 }
91447636 2128 started_tr = 1;
b4c24cb9 2129
9bccf70c
A
2130 /*
2131 * Verify the directory is empty (and valid).
2132 * (Rmdir ".." won't be valid since
2133 * ".." will contain a reference to
2134 * the current directory and thus be
2135 * non-empty.)
2136 */
9bccf70c
A
2137 if ((dcp->c_flags & APPEND) || (cp->c_flags & (IMMUTABLE | APPEND))) {
2138 error = EPERM;
2139 goto out;
2140 }
2141
2142 /* Remove the entry from the namei cache: */
2143 cache_purge(vp);
2144
91447636
A
2145 /*
2146 * Protect against a race with rename by using the component
2147 * name passed in and parent id from dvp (instead of using
2148 * the cp->c_desc which may have changed).
2149 */
2d21ac55 2150 desc.cd_nameptr = (const u_int8_t *)cnp->cn_nameptr;
91447636 2151 desc.cd_namelen = cnp->cn_namelen;
2d21ac55 2152 desc.cd_parentcnid = dcp->c_fileid;
91447636 2153 desc.cd_cnid = cp->c_cnid;
2d21ac55
A
2154 desc.cd_flags = CD_ISDIR;
2155 desc.cd_encoding = cp->c_encoding;
2156 desc.cd_hint = 0;
2157
2158 if (!hfs_valid_cnode(hfsmp, dvp, cnp, cp->c_fileid)) {
2159 error = 0;
2160 goto out;
2161 }
9bccf70c 2162
9bccf70c 2163 /* Remove entry from catalog */
2d21ac55
A
2164 lockflags = hfs_systemfile_lock(hfsmp, SFL_CATALOG | SFL_ATTRIBUTE | SFL_BITMAP, HFS_EXCLUSIVE_LOCK);
2165
2166 if (!skip_reserve) {
2167 /*
2168 * Reserve some space in the Catalog file.
2169 */
2170 if ((error = cat_preflight(hfsmp, CAT_DELETE, NULL, 0))) {
2171 hfs_systemfile_unlock(hfsmp, lockflags);
2172 goto out;
2173 }
2174 }
2175
91447636
A
2176 error = cat_delete(hfsmp, &desc, &cp->c_attr);
2177 if (error == 0) {
2d21ac55
A
2178 /* The parent lost a child */
2179 if (dcp->c_entries > 0)
2180 dcp->c_entries--;
2181 DEC_FOLDERCOUNT(hfsmp, dcp->c_attr);
2182 dcp->c_dirchangecnt++;
2183 dcp->c_touch_chgtime = TRUE;
2184 dcp->c_touch_modtime = TRUE;
2185 hfs_touchtimes(hfsmp, cp);
2186 (void) cat_update(hfsmp, &dcp->c_desc, &dcp->c_attr, NULL, NULL);
2187 cp->c_flag &= ~(C_MODIFIED | C_FORCEUPDATE);
91447636 2188 }
2d21ac55 2189
91447636 2190 hfs_systemfile_unlock(hfsmp, lockflags);
9bccf70c 2191
91447636
A
2192 if (error)
2193 goto out;
9bccf70c
A
2194
2195#if QUOTA
2d21ac55
A
2196 if (hfsmp->hfs_flags & HFS_QUOTAS)
2197 (void)hfs_chkiq(cp, -1, NOCRED, 0);
9bccf70c
A
2198#endif /* QUOTA */
2199
9bccf70c
A
2200 hfs_volupdate(hfsmp, VOL_RMDIR, (dcp->c_cnid == kHFSRootFolderID));
2201
2d21ac55
A
2202 /*
2203 * directory open or in use (e.g. opendir() or current working
2204 * directory for some process); wait for inactive to actually
2205 * remove catalog entry
2206 */
2207 if (vnode_isinuse(vp, 0)) {
2208 cp->c_flag |= C_DELETED;
2209 } else {
2d21ac55
A
2210 cp->c_flag |= C_NOEXISTS;
2211 }
9bccf70c 2212out:
2d21ac55
A
2213 dcp->c_flag &= ~C_DIR_MODIFICATION;
2214 wakeup((caddr_t)&dcp->c_flag);
2215
b4c24cb9 2216 if (started_tr) {
91447636 2217 hfs_end_transaction(hfsmp);
b4c24cb9
A
2218 }
2219
9bccf70c
A
2220 return (error);
2221}
2222
9bccf70c 2223
91447636
A
2224/*
2225 * Remove a file or link.
2226 */
9bccf70c 2227static int
91447636
A
2228hfs_vnop_remove(ap)
2229 struct vnop_remove_args /* {
9bccf70c
A
2230 struct vnode *a_dvp;
2231 struct vnode *a_vp;
2232 struct componentname *a_cnp;
91447636
A
2233 int a_flags;
2234 vfs_context_t a_context;
9bccf70c
A
2235 } */ *ap;
2236{
91447636
A
2237 struct vnode *dvp = ap->a_dvp;
2238 struct vnode *vp = ap->a_vp;
2d21ac55
A
2239 struct cnode *dcp = VTOC(dvp);
2240 struct cnode *cp = VTOC(vp);
b0d623f7
A
2241 struct vnode *rvp = NULL;
2242 struct hfsmount *hfsmp = VTOHFS(vp);
2243 int error=0, recycle_rsrc=0;
2244 int drop_rsrc_vnode = 0;
2245 int vref;
91447636
A
2246
2247 if (dvp == vp) {
2248 return (EINVAL);
2249 }
2250
b0d623f7
A
2251 /*
2252 * We need to grab the cnode lock on 'cp' before the lockpair()
2253 * to get an iocount on the rsrc fork BEFORE we enter hfs_removefile.
2254 * To prevent other deadlocks, it's best to call hfs_vgetrsrc in a way that
2255 * allows it to drop the cnode lock that it expects to be held coming in.
2256 * If we don't, we could commit a lock order violation, causing a deadlock.
2257 * In order to safely get the rsrc vnode with an iocount, we need to only hold the
2258 * lock on the file temporarily. Unlike hfs_vnop_rename, we don't have to worry
2259 * about one rsrc fork getting recycled for another, but we do want to ensure
2260 * that there are no deadlocks due to lock ordering issues.
2261 *
2262 * Note: this function may be invoked for directory hardlinks, so just skip these
2263 * steps if 'vp' is a directory.
2264 */
2265
2266
2267 if ((vp->v_type == VLNK) || (vp->v_type == VREG)) {
2268
2269 if ((error = hfs_lock (cp, HFS_EXCLUSIVE_LOCK))) {
2270 return (error);
2271 }
b7266188 2272 error = hfs_vgetrsrc(hfsmp, vp, &rvp, TRUE, TRUE);
b0d623f7
A
2273 hfs_unlock(cp);
2274 if (error) {
b7266188
A
2275 /* We may have gotten a rsrc vp out even though we got an error back. */
2276 if (rvp) {
2277 vnode_put(rvp);
2278 rvp = NULL;
2279 }
2280 return error;
b0d623f7
A
2281 }
2282 drop_rsrc_vnode = 1;
2283 }
2284 /* Now that we may have an iocount on rvp, do the lock pair */
2d21ac55 2285 hfs_lock_truncate(cp, TRUE);
91447636 2286
4a3eedf9
A
2287 if ((error = hfs_lockpair(dcp, cp, HFS_EXCLUSIVE_LOCK))) {
2288 hfs_unlock_truncate(cp, TRUE);
b0d623f7
A
2289 /* drop the iocount on rvp if necessary */
2290 if (drop_rsrc_vnode) {
2291 vnode_put (rvp);
2292 }
4a3eedf9
A
2293 return (error);
2294 }
b0d623f7
A
2295
2296 /*
2297 * Check to see if we raced rmdir for the parent directory
2298 * hfs_removefile already checks for a race on vp/cp
2299 */
2300 if (dcp->c_flag & (C_DELETED | C_NOEXISTS)) {
2301 error = ENOENT;
2302 goto rm_done;
2303 }
2304
2305 error = hfs_removefile(dvp, vp, ap->a_cnp, ap->a_flags, 0, 0, rvp);
2d21ac55
A
2306
2307 //
2308 // If the remove succeeded and it's an open-unlinked file that has
2309 // a resource fork vnode that's not in use, we will want to recycle
2310 // the rvp *after* we're done unlocking everything. Otherwise the
2311 // resource vnode will keep a v_parent reference on this vnode which
2312 // prevents it from going through inactive/reclaim which means that
2313 // the disk space associated with this file won't get free'd until
2314 // something forces the resource vnode to get recycled (and that can
2315 // take a very long time).
2316 //
b0d623f7
A
2317 if (error == 0 && (cp->c_flag & C_DELETED) &&
2318 (rvp) && !vnode_isinuse(rvp, 0)) {
2d21ac55
A
2319 recycle_rsrc = 1;
2320 }
91447636 2321
4a3eedf9
A
2322 /*
2323 * Drop the truncate lock before unlocking the cnode
2324 * (which can potentially perform a vnode_put and
2325 * recycle the vnode which in turn might require the
2326 * truncate lock)
2327 */
b0d623f7 2328rm_done:
2d21ac55 2329 hfs_unlock_truncate(cp, TRUE);
4a3eedf9 2330 hfs_unlockpair(dcp, cp);
2d21ac55 2331
b0d623f7
A
2332 if (recycle_rsrc) {
2333 vref = vnode_ref(rvp);
2334 if (vref == 0) {
2335 /* vnode_ref could return an error, only release if we got a ref */
2336 vnode_rele(rvp);
2337 }
2d21ac55 2338 vnode_recycle(rvp);
2d21ac55
A
2339 }
2340
b0d623f7
A
2341 if (drop_rsrc_vnode) {
2342 /* drop iocount on rsrc fork, was obtained at beginning of fxn */
2343 vnode_put(rvp);
2344 }
2345
91447636 2346 return (error);
55e303ae
A
2347}
2348
2349
91447636
A
2350static int
2351hfs_removefile_callback(struct buf *bp, void *hfsmp) {
2352
2353 if ( !(buf_flags(bp) & B_META))
2d21ac55 2354 panic("hfs: symlink bp @ %p is not marked meta-data!\n", bp);
91447636
A
2355 /*
2356 * it's part of the current transaction, kill it.
2357 */
2358 journal_kill_block(((struct hfsmount *)hfsmp)->jnl, bp);
2359
2360 return (BUF_CLAIMED);
2361}
55e303ae
A
2362
2363/*
2364 * hfs_removefile
2365 *
91447636 2366 * Similar to hfs_vnop_remove except there are additional options.
b0d623f7
A
2367 * This function may be used to remove directories if they have
2368 * lots of EA's -- note the 'allow_dirs' argument.
2369 *
2370 * The 'rvp' argument is used to pass in a resource fork vnode with
2371 * an iocount to prevent it from getting recycled during usage. If it
2372 * is NULL, then it is assumed the caller is a VNOP that cannot operate
2373 * on resource forks, like hfs_vnop_symlink or hfs_removedir. Otherwise in
2374 * a VNOP that takes multiple vnodes, we could violate lock order and
2375 * cause a deadlock.
91447636
A
2376 *
2377 * Requires cnode and truncate locks to be held.
55e303ae
A
2378 */
2379static int
91447636 2380hfs_removefile(struct vnode *dvp, struct vnode *vp, struct componentname *cnp,
b0d623f7 2381 int flags, int skip_reserve, int allow_dirs, struct vnode *rvp)
55e303ae 2382{
9bccf70c
A
2383 struct cnode *cp;
2384 struct cnode *dcp;
2385 struct hfsmount *hfsmp;
91447636
A
2386 struct cat_desc desc;
2387 struct timeval tv;
2388 vfs_context_t ctx = cnp->cn_context;
9bccf70c
A
2389 int dataforkbusy = 0;
2390 int rsrcforkbusy = 0;
2391 int truncated = 0;
91447636 2392 int lockflags;
9bccf70c 2393 int error = 0;
2d21ac55 2394 int started_tr = 0;
4a3eedf9 2395 int isbigfile = 0, defer_remove=0, isdir=0;
9bccf70c
A
2396
2397 cp = VTOC(vp);
2398 dcp = VTOC(dvp);
2399 hfsmp = VTOHFS(vp);
91447636 2400
2d21ac55 2401 /* Check if we lost a race post lookup. */
91447636 2402 if (cp->c_flag & (C_NOEXISTS | C_DELETED)) {
2d21ac55 2403 return (0);
91447636 2404 }
2d21ac55
A
2405
2406 if (!hfs_valid_cnode(hfsmp, dvp, cnp, cp->c_fileid)) {
2407 return 0;
9bccf70c
A
2408 }
2409
2410 /* Make sure a remove is permitted */
91447636 2411 if (VNODE_IS_RSRC(vp)) {
2d21ac55 2412 return (EPERM);
b4c24cb9 2413 }
2d21ac55
A
2414 /* Don't allow deleting the journal or journal_info_block. */
2415 if (hfsmp->jnl &&
2416 (cp->c_fileid == hfsmp->hfs_jnlfileid || cp->c_fileid == hfsmp->hfs_jnlinfoblkid)) {
2417 return (EPERM);
2418 }
2419 /*
2420 * Hard links require special handling.
2421 */
2422 if (cp->c_flag & C_HARDLINK) {
2423 if ((flags & VNODE_REMOVE_NODELETEBUSY) && vnode_isinuse(vp, 0)) {
2424 return (EBUSY);
2425 } else {
2426 /* A directory hard link with a link count of one is
2427 * treated as a regular directory. Therefore it should
2428 * only be removed using rmdir().
2429 */
2430 if ((vnode_isdir(vp) == 1) && (cp->c_linkcount == 1) &&
2431 (allow_dirs == 0)) {
2432 return (EPERM);
2433 }
2434 return hfs_unlink(hfsmp, dvp, vp, cnp, skip_reserve);
2435 }
2436 }
2437 /* Directories should call hfs_rmdir! (unless they have a lot of attributes) */
2438 if (vnode_isdir(vp)) {
2439 if (allow_dirs == 0)
2440 return (EPERM); /* POSIX */
2441 isdir = 1;
2442 }
2443 /* Sanity check the parent ids. */
2444 if ((cp->c_parentcnid != hfsmp->hfs_private_desc[FILE_HARDLINKS].cd_cnid) &&
2445 (cp->c_parentcnid != dcp->c_fileid)) {
2446 return (EINVAL);
2447 }
2448
2449 dcp->c_flag |= C_DIR_MODIFICATION;
2450
2451 // this guy is going away so mark him as such
2452 cp->c_flag |= C_DELETED;
2453
2454
2455 /* Remove our entry from the namei cache. */
2456 cache_purge(vp);
9bccf70c
A
2457
2458 /*
b0d623f7
A
2459 * We expect the caller, if operating on files,
2460 * will have passed in a resource fork vnode with
2461 * an iocount, even if there was no content.
2462 * We only do the hfs_truncate on the rsrc fork
2463 * if we know that it DID have content, however.
2464 * This has the bonus of not requiring us to defer
2465 * its removal, unless it is in use.
9bccf70c 2466 */
b0d623f7 2467
2d21ac55
A
2468 /* Check if this file is being used. */
2469 if (isdir == 0) {
2470 dataforkbusy = vnode_isinuse(vp, 0);
b0d623f7
A
2471 /* Only need to defer resource fork removal if in use and has content */
2472 if (rvp && (cp->c_blocks - VTOF(vp)->ff_blocks)) {
2473 rsrcforkbusy = vnode_isinuse(rvp, 0);
2474 }
2d21ac55
A
2475 }
2476
2477 /* Check if we have to break the deletion into multiple pieces. */
2478 if (isdir == 0) {
2479 isbigfile = ((cp->c_datafork->ff_size >= HFS_BIGFILE_SIZE) && overflow_extents(VTOF(vp)));
b4c24cb9
A
2480 }
2481
2d21ac55
A
2482 /* Check if the file has xattrs. If it does we'll have to delete them in
2483 individual transactions in case there are too many */
2484 if ((hfsmp->hfs_attribute_vp != NULL) &&
2485 (cp->c_attr.ca_recflags & kHFSHasAttributesMask) != 0) {
4a3eedf9 2486 defer_remove = 1;
2d21ac55 2487 }
55e303ae 2488
9bccf70c
A
2489 /*
2490 * Carbon semantics prohibit deleting busy files.
91447636 2491 * (enforced when VNODE_REMOVE_NODELETEBUSY is requested)
9bccf70c 2492 */
91447636
A
2493 if (dataforkbusy || rsrcforkbusy) {
2494 if ((flags & VNODE_REMOVE_NODELETEBUSY) ||
2d21ac55 2495 (hfsmp->hfs_private_desc[FILE_HARDLINKS].cd_cnid == 0)) {
91447636
A
2496 error = EBUSY;
2497 goto out;
2498 }
9bccf70c
A
2499 }
2500
d7e50217 2501#if QUOTA
2d21ac55
A
2502 if (hfsmp->hfs_flags & HFS_QUOTAS)
2503 (void)hfs_getinoquota(cp);
d7e50217
A
2504#endif /* QUOTA */
2505
2d21ac55
A
2506 /* Check if we need a ubc_setsize. */
2507 if (isdir == 0 && (!dataforkbusy || !rsrcforkbusy)) {
91447636 2508 /*
0c530ab8
A
2509 * A ubc_setsize can cause a pagein so defer it
2510 * until after the cnode lock is dropped. The
2511 * cnode lock cannot be dropped/reacquired here
2512 * since we might already hold the journal lock.
91447636 2513 */
91447636 2514 if (!dataforkbusy && cp->c_datafork->ff_blocks && !isbigfile) {
0c530ab8 2515 cp->c_flag |= C_NEED_DATA_SETSIZE;
91447636
A
2516 }
2517 if (!rsrcforkbusy && rvp) {
0c530ab8 2518 cp->c_flag |= C_NEED_RSRC_SETSIZE;
91447636 2519 }
91447636
A
2520 }
2521
2522 if ((error = hfs_start_transaction(hfsmp)) != 0) {
2523 goto out;
b4c24cb9 2524 }
91447636 2525 started_tr = 1;
b4c24cb9 2526
b4c24cb9 2527 // XXXdbg - if we're journaled, kill any dirty symlink buffers
91447636
A
2528 if (hfsmp->jnl && vnode_islnk(vp))
2529 buf_iterate(vp, hfs_removefile_callback, BUF_SKIP_NONLOCKED, (void *)hfsmp);
b4c24cb9 2530
9bccf70c
A
2531 /*
2532 * Truncate any non-busy forks. Busy forks will
2d21ac55 2533 * get truncated when their vnode goes inactive.
b0d623f7
A
2534 * Note that we will only enter this region if we
2535 * can avoid creating an open-unlinked file. If
2536 * either region is busy, we will have to create an open
2537 * unlinked file.
91447636
A
2538 * Since we're already inside a transaction,
2539 * tell hfs_truncate to skip the ubc_setsize.
9bccf70c 2540 */
b0d623f7
A
2541 if (isdir == 0 && (!dataforkbusy && !rsrcforkbusy)) {
2542 /*
2543 * Note that 5th argument to hfs_truncate indicates whether or not
2544 * hfs_update calls should be suppressed in call to do_hfs_truncate
2545 */
55e303ae 2546 if (!dataforkbusy && !isbigfile && cp->c_datafork->ff_blocks != 0) {
b0d623f7
A
2547 /* skip update in hfs_truncate */
2548 error = hfs_truncate(vp, (off_t)0, IO_NDELAY, 1, 1, ctx);
9bccf70c
A
2549 if (error)
2550 goto out;
2551 truncated = 1;
2552 }
2553 if (!rsrcforkbusy && rvp) {
b0d623f7
A
2554 /* skip update in hfs_truncate */
2555 error = hfs_truncate(rvp, (off_t)0, IO_NDELAY, 1, 1, ctx);
90556fb8 2556 if (error)
9bccf70c 2557 goto out;
9bccf70c
A
2558 truncated = 1;
2559 }
2560 }
91447636
A
2561
2562 /*
2563 * Protect against a race with rename by using the component
2564 * name passed in and parent id from dvp (instead of using
b0d623f7
A
2565 * the cp->c_desc which may have changed). Also, be aware that
2566 * because we allow directories to be passed in, we need to special case
2567 * this temporary descriptor in case we were handed a directory.
91447636 2568 */
b0d623f7
A
2569 if (isdir) {
2570 desc.cd_flags = CD_ISDIR;
2571 }
2572 else {
2573 desc.cd_flags = 0;
2574 }
91447636 2575 desc.cd_encoding = cp->c_desc.cd_encoding;
2d21ac55 2576 desc.cd_nameptr = (const u_int8_t *)cnp->cn_nameptr;
91447636 2577 desc.cd_namelen = cnp->cn_namelen;
2d21ac55 2578 desc.cd_parentcnid = dcp->c_fileid;
91447636 2579 desc.cd_hint = cp->c_desc.cd_hint;
2d21ac55 2580 desc.cd_cnid = cp->c_cnid;
91447636
A
2581 microtime(&tv);
2582
9bccf70c 2583 /*
2d21ac55 2584 * There are two cases to consider:
b0d623f7 2585 * 1. File/Dir is busy/big/defer_remove ==> move/rename the file/dir
2d21ac55 2586 * 2. File is not in use ==> remove the file
b0d623f7
A
2587 *
2588 * We can get a directory in case 1 because it may have had lots of attributes,
2589 * which need to get removed here.
9bccf70c 2590 */
4a3eedf9 2591 if (dataforkbusy || rsrcforkbusy || isbigfile || defer_remove) {
9bccf70c
A
2592 char delname[32];
2593 struct cat_desc to_desc;
2594 struct cat_desc todir_desc;
2595
2596 /*
b0d623f7
A
2597 * Orphan this file or directory (move to hidden directory).
2598 * Again, we need to take care that we treat directories as directories,
2599 * and files as files. Because directories with attributes can be passed in
2600 * check to make sure that we have a directory or a file before filling in the
2601 * temporary descriptor's flags. We keep orphaned directories AND files in
2602 * the FILE_HARDLINKS private directory since we're generalizing over all
2603 * orphaned filesystem objects.
9bccf70c
A
2604 */
2605 bzero(&todir_desc, sizeof(todir_desc));
2606 todir_desc.cd_parentcnid = 2;
2607
2d21ac55 2608 MAKE_DELETED_NAME(delname, sizeof(delname), cp->c_fileid);
9bccf70c 2609 bzero(&to_desc, sizeof(to_desc));
2d21ac55 2610 to_desc.cd_nameptr = (const u_int8_t *)delname;
9bccf70c 2611 to_desc.cd_namelen = strlen(delname);
2d21ac55 2612 to_desc.cd_parentcnid = hfsmp->hfs_private_desc[FILE_HARDLINKS].cd_cnid;
b0d623f7
A
2613 if (isdir) {
2614 to_desc.cd_flags = CD_ISDIR;
2615 }
2616 else {
2617 to_desc.cd_flags = 0;
2618 }
9bccf70c
A
2619 to_desc.cd_cnid = cp->c_cnid;
2620
91447636 2621 lockflags = hfs_systemfile_lock(hfsmp, SFL_CATALOG, HFS_EXCLUSIVE_LOCK);
2d21ac55
A
2622 if (!skip_reserve) {
2623 if ((error = cat_preflight(hfsmp, CAT_RENAME, NULL, 0))) {
2624 hfs_systemfile_unlock(hfsmp, lockflags);
2625 goto out;
2626 }
2627 }
9bccf70c 2628
91447636 2629 error = cat_rename(hfsmp, &desc, &todir_desc,
9bccf70c
A
2630 &to_desc, (struct cat_desc *)NULL);
2631
b4c24cb9 2632 if (error == 0) {
2d21ac55
A
2633 hfsmp->hfs_private_attr[FILE_HARDLINKS].ca_entries++;
2634 if (isdir == 1) {
2635 INC_FOLDERCOUNT(hfsmp, hfsmp->hfs_private_attr[FILE_HARDLINKS]);
2636 }
2637 (void) cat_update(hfsmp, &hfsmp->hfs_private_desc[FILE_HARDLINKS],
2638 &hfsmp->hfs_private_attr[FILE_HARDLINKS], NULL, NULL);
91447636
A
2639
2640 /* Update the parent directory */
2641 if (dcp->c_entries > 0)
2642 dcp->c_entries--;
2d21ac55
A
2643 if (isdir == 1) {
2644 DEC_FOLDERCOUNT(hfsmp, dcp->c_attr);
2645 }
2646 dcp->c_dirchangecnt++;
91447636
A
2647 dcp->c_ctime = tv.tv_sec;
2648 dcp->c_mtime = tv.tv_sec;
2649 (void) cat_update(hfsmp, &dcp->c_desc, &dcp->c_attr, NULL, NULL);
2650
b0d623f7 2651 /* Update the file or directory's state */
91447636
A
2652 cp->c_flag |= C_DELETED;
2653 cp->c_ctime = tv.tv_sec;
2d21ac55 2654 --cp->c_linkcount;
91447636 2655 (void) cat_update(hfsmp, &to_desc, &cp->c_attr, NULL, NULL);
b4c24cb9 2656 }
91447636
A
2657 hfs_systemfile_unlock(hfsmp, lockflags);
2658 if (error)
2659 goto out;
9bccf70c
A
2660
2661 } else /* Not busy */ {
2662
90556fb8 2663 if (cp->c_blocks > 0) {
55e303ae
A
2664 printf("hfs_remove: attempting to delete a non-empty file %s\n",
2665 cp->c_desc.cd_nameptr);
90556fb8
A
2666 error = EBUSY;
2667 goto out;
2668 }
9bccf70c 2669
2d21ac55
A
2670 lockflags = hfs_systemfile_lock(hfsmp, SFL_CATALOG | SFL_ATTRIBUTE | SFL_BITMAP, HFS_EXCLUSIVE_LOCK);
2671 if (!skip_reserve) {
2672 if ((error = cat_preflight(hfsmp, CAT_DELETE, NULL, 0))) {
2673 hfs_systemfile_unlock(hfsmp, lockflags);
2674 goto out;
2675 }
2676 }
b4c24cb9 2677
91447636 2678 error = cat_delete(hfsmp, &desc, &cp->c_attr);
9bccf70c 2679
90556fb8 2680 if (error && error != ENXIO && error != ENOENT && truncated) {
55e303ae 2681 if ((cp->c_datafork && cp->c_datafork->ff_size != 0) ||
b0d623f7 2682 (cp->c_rsrcfork && cp->c_rsrcfork->ff_size != 0)) {
b7266188
A
2683 off_t data_size = 0;
2684 off_t rsrc_size = 0;
2685 if (cp->c_datafork) {
2686 data_size = cp->c_datafork->ff_size;
2687 }
2688 if (cp->c_rsrcfork) {
2689 rsrc_size = cp->c_rsrcfork->ff_size;
2690 }
b0d623f7
A
2691 printf("hfs: remove: couldn't delete a truncated file (%s)"
2692 "(error %d, data sz %lld; rsrc sz %lld)",
b7266188 2693 cp->c_desc.cd_nameptr, error, data_size, rsrc_size);
b0d623f7 2694 hfs_mark_volume_inconsistent(hfsmp);
b4c24cb9
A
2695 } else {
2696 printf("hfs: remove: strangely enough, deleting truncated file %s (%d) got err %d\n",
b0d623f7
A
2697 cp->c_desc.cd_nameptr, cp->c_attr.ca_fileid, error);
2698 }
b4c24cb9 2699 }
b0d623f7 2700
91447636 2701 if (error == 0) {
91447636
A
2702 /* Update the parent directory */
2703 if (dcp->c_entries > 0)
2704 dcp->c_entries--;
2d21ac55 2705 dcp->c_dirchangecnt++;
91447636
A
2706 dcp->c_ctime = tv.tv_sec;
2707 dcp->c_mtime = tv.tv_sec;
2708 (void) cat_update(hfsmp, &dcp->c_desc, &dcp->c_attr, NULL, NULL);
2709 }
2710 hfs_systemfile_unlock(hfsmp, lockflags);
2711 if (error)
2712 goto out;
9bccf70c
A
2713
2714#if QUOTA
2d21ac55
A
2715 if (hfsmp->hfs_flags & HFS_QUOTAS)
2716 (void)hfs_chkiq(cp, -1, NOCRED, 0);
9bccf70c
A
2717#endif /* QUOTA */
2718
91447636 2719 cp->c_flag |= C_NOEXISTS;
2d21ac55 2720 cp->c_flag &= ~C_DELETED;
b0d623f7
A
2721 truncated = 0; // because the catalog entry is gone
2722
91447636 2723 cp->c_touch_chgtime = TRUE; /* XXX needed ? */
2d21ac55 2724 --cp->c_linkcount;
91447636 2725
b0d623f7
A
2726 /*
2727 * We must never get a directory if we're in this else block. We could
2728 * accidentally drop the number of files in the volume header if we did.
2729 */
9bccf70c
A
2730 hfs_volupdate(hfsmp, VOL_RMFILE, (dcp->c_cnid == kHFSRootFolderID));
2731 }
2732
2733 /*
2734 * All done with this cnode's descriptor...
2735 *
2d21ac55
A
2736 * Note: all future catalog calls for this cnode must be by
2737 * fileid only. This is OK for HFS (which doesn't have file
2738 * thread records) since HFS doesn't support the removal of
2739 * busy files.
9bccf70c
A
2740 */
2741 cat_releasedesc(&cp->c_desc);
2742
55e303ae 2743out:
2d21ac55
A
2744 if (error) {
2745 cp->c_flag &= ~C_DELETED;
b4c24cb9 2746 }
b4c24cb9 2747
9bccf70c
A
2748 /* Commit the truncation to the catalog record */
2749 if (truncated) {
91447636
A
2750 cp->c_flag |= C_FORCEUPDATE;
2751 cp->c_touch_chgtime = TRUE;
2752 cp->c_touch_modtime = TRUE;
2753 (void) hfs_update(vp, 0);
9bccf70c 2754 }
b4c24cb9 2755
b4c24cb9 2756 if (started_tr) {
91447636 2757 hfs_end_transaction(hfsmp);
b4c24cb9
A
2758 }
2759
2d21ac55
A
2760 dcp->c_flag &= ~C_DIR_MODIFICATION;
2761 wakeup((caddr_t)&dcp->c_flag);
2762
9bccf70c
A
2763 return (error);
2764}
2765
2766
2767__private_extern__ void
2768replace_desc(struct cnode *cp, struct cat_desc *cdp)
2769{
2d21ac55 2770 // fixes 4348457 and 4463138
0c530ab8 2771 if (&cp->c_desc == cdp) {
2d21ac55 2772 return;
0c530ab8
A
2773 }
2774
9bccf70c
A
2775 /* First release allocated name buffer */
2776 if (cp->c_desc.cd_flags & CD_HASBUF && cp->c_desc.cd_nameptr != 0) {
2d21ac55 2777 const u_int8_t *name = cp->c_desc.cd_nameptr;
9bccf70c
A
2778
2779 cp->c_desc.cd_nameptr = 0;
2780 cp->c_desc.cd_namelen = 0;
2781 cp->c_desc.cd_flags &= ~CD_HASBUF;
2d21ac55 2782 vfs_removename((const char *)name);
9bccf70c
A
2783 }
2784 bcopy(cdp, &cp->c_desc, sizeof(cp->c_desc));
2785
2786 /* Cnode now owns the name buffer */
2787 cdp->cd_nameptr = 0;
2788 cdp->cd_namelen = 0;
2789 cdp->cd_flags &= ~CD_HASBUF;
2790}
2791
2792
9bccf70c
A
2793/*
2794 * Rename a cnode.
2795 *
91447636
A
2796 * The VFS layer guarantees that:
2797 * - source and destination will either both be directories, or
2798 * both not be directories.
2799 * - all the vnodes are from the same file system
d7e50217 2800 *
91447636 2801 * When the target is a directory, HFS must ensure that its empty.
b0d623f7
A
2802 *
2803 * Note that this function requires up to 6 vnodes in order to work properly
2804 * if it is operating on files (and not on directories). This is because only
2805 * files can have resource forks, and we now require iocounts to be held on the
2806 * vnodes corresponding to the resource forks (if applicable) as well as
2807 * the files or directories undergoing rename. The problem with not holding
2808 * iocounts on the resource fork vnodes is that it can lead to a deadlock
2809 * situation: The rsrc fork of the source file may be recycled and reclaimed
2810 * in order to provide a vnode for the destination file's rsrc fork. Since
2811 * data and rsrc forks share the same cnode, we'd eventually try to lock the
2812 * source file's cnode in order to sync its rsrc fork to disk, but it's already
2813 * been locked. By taking the rsrc fork vnodes up front we ensure that they
2814 * cannot be recycled, and that the situation mentioned above cannot happen.
9bccf70c 2815 */
9bccf70c 2816static int
91447636
A
2817hfs_vnop_rename(ap)
2818 struct vnop_rename_args /* {
9bccf70c
A
2819 struct vnode *a_fdvp;
2820 struct vnode *a_fvp;
2821 struct componentname *a_fcnp;
2822 struct vnode *a_tdvp;
2823 struct vnode *a_tvp;
2824 struct componentname *a_tcnp;
91447636 2825 vfs_context_t a_context;
9bccf70c
A
2826 } */ *ap;
2827{
2828 struct vnode *tvp = ap->a_tvp;
2829 struct vnode *tdvp = ap->a_tdvp;
2830 struct vnode *fvp = ap->a_fvp;
2831 struct vnode *fdvp = ap->a_fdvp;
b0d623f7
A
2832 struct vnode *fvp_rsrc = NULLVP;
2833 struct vnode *tvp_rsrc = NULLVP;
9bccf70c
A
2834 struct componentname *tcnp = ap->a_tcnp;
2835 struct componentname *fcnp = ap->a_fcnp;
91447636
A
2836 struct proc *p = vfs_context_proc(ap->a_context);
2837 struct cnode *fcp;
2838 struct cnode *fdcp;
2839 struct cnode *tdcp;
2840 struct cnode *tcp;
b0d623f7 2841 struct cnode *error_cnode;
9bccf70c
A
2842 struct cat_desc from_desc;
2843 struct cat_desc to_desc;
2844 struct cat_desc out_desc;
91447636
A
2845 struct hfsmount *hfsmp;
2846 cat_cookie_t cookie;
2847 int tvp_deleted = 0;
2848 int started_tr = 0, got_cookie = 0;
2849 int took_trunc_lock = 0;
2850 int lockflags;
2851 int error;
cf7d32b8 2852 int recycle_rsrc = 0;
b0d623f7
A
2853
2854
2855 /*
2856 * Before grabbing the four locks, we may need to get an iocount on the resource fork
2857 * vnodes in question, just like hfs_vnop_remove. If fvp and tvp are not
2858 * directories, then go ahead and grab the resource fork vnodes now
2859 * one at a time. We don't actively need the fvp_rsrc to do the rename operation,
2860 * but we need the iocount to prevent the vnode from getting recycled/reclaimed
2861 * during the middle of the VNOP.
2862 */
2863
2864
2865 if ((vnode_isreg(fvp)) || (vnode_islnk(fvp))) {
2866
2867 if ((error = hfs_lock (VTOC(fvp), HFS_EXCLUSIVE_LOCK))) {
2868 return (error);
2869 }
b7266188
A
2870
2871 /*
2872 * We care if we race against rename/delete with this cnode, so we'll
2873 * error out if this file becomes open-unlinked during this call.
2874 */
2875 error = hfs_vgetrsrc(VTOHFS(fvp), fvp, &fvp_rsrc, TRUE, TRUE);
b0d623f7
A
2876 hfs_unlock (VTOC(fvp));
2877 if (error) {
b7266188
A
2878 if (fvp_rsrc) {
2879 vnode_put (fvp_rsrc);
2880 }
b0d623f7
A
2881 return error;
2882 }
2883 }
2884
2885 if (tvp && (vnode_isreg(tvp) || vnode_islnk(tvp))) {
2886 /*
2887 * Lock failure is OK on tvp, since we may race with a remove on the dst.
2888 * But this shouldn't stop rename from proceeding, so only try to
2889 * grab the resource fork if the lock succeeded.
2890 */
2891 if (hfs_lock (VTOC(tvp), HFS_EXCLUSIVE_LOCK) == 0) {
b7266188
A
2892 tcp = VTOC(tvp);
2893
2894 /*
2895 * We only care if we get an open-unlinked file on the dst so we
2896 * know to null out tvp/tcp to make the rename operation act
2897 * as if they never existed. Because they're effectively out of the
2898 * namespace already it's fine to do this. If this is true, then
2899 * make sure to unlock the cnode and drop the iocount only after the unlock.
2900 */
2901 error = hfs_vgetrsrc(VTOHFS(tvp), tvp, &tvp_rsrc, TRUE, TRUE);
2902 hfs_unlock (tcp);
b0d623f7 2903 if (error) {
b7266188
A
2904 /*
2905 * Since we specify TRUE for error-on-unlinked in hfs_vgetrsrc,
2906 * we can get a rsrc fork vp even if it returns an error.
2907 */
2908 tcp = NULL;
2909 tvp = NULL;
2910 if (tvp_rsrc) {
2911 vnode_put (tvp_rsrc);
2912 tvp_rsrc = NULLVP;
b0d623f7 2913 }
b7266188
A
2914 /* just bypass truncate lock and act as if we never got tcp/tvp */
2915 goto retry;
b0d623f7
A
2916 }
2917 }
2918 }
2919
2920 /* When tvp exists, take the truncate lock for hfs_removefile(). */
2d21ac55 2921 if (tvp && (vnode_isreg(tvp) || vnode_islnk(tvp))) {
91447636
A
2922 hfs_lock_truncate(VTOC(tvp), TRUE);
2923 took_trunc_lock = 1;
2924 }
9bccf70c 2925
2d21ac55 2926 retry:
91447636 2927 error = hfs_lockfour(VTOC(fdvp), VTOC(fvp), VTOC(tdvp), tvp ? VTOC(tvp) : NULL,
b0d623f7 2928 HFS_EXCLUSIVE_LOCK, &error_cnode);
91447636 2929 if (error) {
593a1d5f 2930 if (took_trunc_lock) {
b0d623f7 2931 hfs_unlock_truncate(VTOC(tvp), TRUE);
593a1d5f
A
2932 took_trunc_lock = 0;
2933 }
b0d623f7
A
2934 /*
2935 * tvp might no longer exist. If the cause of the lock failure
2936 * was tvp, then we can try again with tvp/tcp set to NULL.
2937 * This is ok because the vfs syscall will vnode_put the vnodes
2938 * after we return from hfs_vnop_rename.
2939 */
2940 if ((error == ENOENT) && (tvp != NULL) && (error_cnode == VTOC(tvp))) {
2941 tcp = NULL;
2942 tvp = NULL;
2943 goto retry;
2944 }
2945 /* otherwise, drop iocounts on the rsrc forks and bail out */
2946 if (fvp_rsrc) {
2947 vnode_put (fvp_rsrc);
2948 }
2949 if (tvp_rsrc) {
2950 vnode_put (tvp_rsrc);
2951 }
91447636 2952 return (error);
55e303ae
A
2953 }
2954
91447636
A
2955 fdcp = VTOC(fdvp);
2956 fcp = VTOC(fvp);
2957 tdcp = VTOC(tdvp);
2958 tcp = tvp ? VTOC(tvp) : NULL;
2959 hfsmp = VTOHFS(tdvp);
55e303ae 2960
b0d623f7
A
2961 /* Ensure we didn't race src or dst parent directories with rmdir. */
2962 if (fdcp->c_flag & (C_NOEXISTS | C_DELETED)) {
2963 error = ENOENT;
2964 goto out;
2965 }
2966
2967 if (tdcp->c_flag & (C_NOEXISTS | C_DELETED)) {
2968 error = ENOENT;
2969 goto out;
2970 }
2971
2972
2973 /* Check for a race against unlink. The hfs_valid_cnode checks validate
2974 * the parent/child relationship with fdcp and tdcp, as well as the
2975 * component name of the target cnodes.
2976 */
2d21ac55 2977 if ((fcp->c_flag & (C_NOEXISTS | C_DELETED)) || !hfs_valid_cnode(hfsmp, fdvp, fcnp, fcp->c_fileid)) {
91447636
A
2978 error = ENOENT;
2979 goto out;
9bccf70c 2980 }
91447636 2981
2d21ac55
A
2982 if (tcp && ((tcp->c_flag & (C_NOEXISTS | C_DELETED)) || !hfs_valid_cnode(hfsmp, tdvp, tcnp, tcp->c_fileid))) {
2983 //
2984 // hmm, the destination vnode isn't valid any more.
2985 // in this case we can just drop him and pretend he
2986 // never existed in the first place.
2987 //
2988 if (took_trunc_lock) {
2989 hfs_unlock_truncate(VTOC(tvp), TRUE);
2990 took_trunc_lock = 0;
2991 }
2992
2993 hfs_unlockfour(fdcp, fcp, tdcp, tcp);
2994
2995 tcp = NULL;
2996 tvp = NULL;
2997
2998 // retry the locking with tvp null'ed out
2999 goto retry;
3000 }
3001
3002 fdcp->c_flag |= C_DIR_MODIFICATION;
3003 if (fdvp != tdvp) {
3004 tdcp->c_flag |= C_DIR_MODIFICATION;
3005 }
3006
3007 /*
3008 * Disallow renaming of a directory hard link if the source and
3009 * destination parent directories are different, or a directory whose
3010 * descendant is a directory hard link and the one of the ancestors
3011 * of the destination directory is a directory hard link.
3012 */
3013 if (vnode_isdir(fvp) && (fdvp != tdvp)) {
3014 if (fcp->c_flag & C_HARDLINK) {
3015 error = EPERM;
3016 goto out;
3017 }
3018 if (fcp->c_attr.ca_recflags & kHFSHasChildLinkMask) {
3019 lockflags = hfs_systemfile_lock(hfsmp, SFL_CATALOG, HFS_SHARED_LOCK);
3020 if (cat_check_link_ancestry(hfsmp, tdcp->c_fileid, 0)) {
3021 error = EPERM;
3022 hfs_systemfile_unlock(hfsmp, lockflags);
3023 goto out;
3024 }
3025 hfs_systemfile_unlock(hfsmp, lockflags);
3026 }
3027 }
3028
9bccf70c 3029 /*
55e303ae
A
3030 * The following edge case is caught here:
3031 * (to cannot be a descendent of from)
3032 *
3033 * o fdvp
3034 * /
3035 * /
3036 * o fvp
3037 * \
3038 * \
3039 * o tdvp
3040 * /
3041 * /
3042 * o tvp
9bccf70c 3043 */
2d21ac55 3044 if (tdcp->c_parentcnid == fcp->c_fileid) {
55e303ae
A
3045 error = EINVAL;
3046 goto out;
3047 }
3048
3049 /*
3050 * The following two edge cases are caught here:
3051 * (note tvp is not empty)
3052 *
3053 * o tdvp o tdvp
3054 * / /
3055 * / /
3056 * o tvp tvp o fdvp
3057 * \ \
3058 * \ \
3059 * o fdvp o fvp
3060 * /
3061 * /
3062 * o fvp
3063 */
91447636 3064 if (tvp && vnode_isdir(tvp) && (tcp->c_entries != 0) && fvp != tvp) {
55e303ae
A
3065 error = ENOTEMPTY;
3066 goto out;
3067 }
3068
3069 /*
3070 * The following edge case is caught here:
3071 * (the from child and parent are the same)
3072 *
3073 * o tdvp
3074 * /
3075 * /
3076 * fdvp o fvp
3077 */
3078 if (fdvp == fvp) {
3079 error = EINVAL;
d7e50217 3080 goto out;
9bccf70c
A
3081 }
3082
3083 /*
d7e50217 3084 * Make sure "from" vnode and its parent are changeable.
9bccf70c 3085 */
91447636 3086 if ((fcp->c_flags & (IMMUTABLE | APPEND)) || (fdcp->c_flags & APPEND)) {
d7e50217
A
3087 error = EPERM;
3088 goto out;
9bccf70c
A
3089 }
3090
9bccf70c 3091 /*
d7e50217
A
3092 * If the destination parent directory is "sticky", then the
3093 * user must own the parent directory, or the destination of
3094 * the rename, otherwise the destination may not be changed
3095 * (except by root). This implements append-only directories.
3096 *
55e303ae 3097 * Note that checks for immutable and write access are done
91447636 3098 * by the call to hfs_removefile.
9bccf70c 3099 */
d7e50217 3100 if (tvp && (tdcp->c_mode & S_ISTXT) &&
91447636
A
3101 (suser(vfs_context_ucred(tcnp->cn_context), NULL)) &&
3102 (kauth_cred_getuid(vfs_context_ucred(tcnp->cn_context)) != tdcp->c_uid) &&
3103 (hfs_owner_rights(hfsmp, tcp->c_uid, vfs_context_ucred(tcnp->cn_context), p, false)) ) {
55e303ae
A
3104 error = EPERM;
3105 goto out;
9bccf70c
A
3106 }
3107
55e303ae
A
3108#if QUOTA
3109 if (tvp)
91447636 3110 (void)hfs_getinoquota(tcp);
55e303ae 3111#endif
91447636 3112 /* Preflighting done, take fvp out of the name space. */
55e303ae
A
3113 cache_purge(fvp);
3114
9bccf70c 3115 bzero(&from_desc, sizeof(from_desc));
2d21ac55 3116 from_desc.cd_nameptr = (const u_int8_t *)fcnp->cn_nameptr;
9bccf70c 3117 from_desc.cd_namelen = fcnp->cn_namelen;
2d21ac55 3118 from_desc.cd_parentcnid = fdcp->c_fileid;
9bccf70c
A
3119 from_desc.cd_flags = fcp->c_desc.cd_flags & ~(CD_HASBUF | CD_DECOMPOSED);
3120 from_desc.cd_cnid = fcp->c_cnid;
d7e50217 3121
9bccf70c 3122 bzero(&to_desc, sizeof(to_desc));
2d21ac55 3123 to_desc.cd_nameptr = (const u_int8_t *)tcnp->cn_nameptr;
9bccf70c 3124 to_desc.cd_namelen = tcnp->cn_namelen;
2d21ac55 3125 to_desc.cd_parentcnid = tdcp->c_fileid;
9bccf70c
A
3126 to_desc.cd_flags = fcp->c_desc.cd_flags & ~(CD_HASBUF | CD_DECOMPOSED);
3127 to_desc.cd_cnid = fcp->c_cnid;
3128
91447636
A
3129 if ((error = hfs_start_transaction(hfsmp)) != 0) {
3130 goto out;
3131 }
3132 started_tr = 1;
3133
2d21ac55
A
3134 /* hfs_vnop_link() and hfs_vnop_rename() set kHFSHasChildLinkMask
3135 * inside a journal transaction and without holding a cnode lock.
3136 * As setting of this bit depends on being in journal transaction for
3137 * concurrency, check this bit again after we start journal transaction for rename
3138 * to ensure that this directory does not have any descendant that
3139 * is a directory hard link.
3140 */
3141 if (vnode_isdir(fvp) && (fdvp != tdvp)) {
3142 if (fcp->c_attr.ca_recflags & kHFSHasChildLinkMask) {
3143 lockflags = hfs_systemfile_lock(hfsmp, SFL_CATALOG, HFS_SHARED_LOCK);
3144 if (cat_check_link_ancestry(hfsmp, tdcp->c_fileid, 0)) {
3145 error = EPERM;
3146 hfs_systemfile_unlock(hfsmp, lockflags);
3147 goto out;
3148 }
3149 hfs_systemfile_unlock(hfsmp, lockflags);
3150 }
3151 }
3152
91447636
A
3153 // if it's a hardlink then re-lookup the name so
3154 // that we get the correct cnid in from_desc (see
3155 // the comment in hfs_removefile for more details)
3156 //
3157 if (fcp->c_flag & C_HARDLINK) {
3158 struct cat_desc tmpdesc;
3159 cnid_t real_cnid;
3160
2d21ac55 3161 tmpdesc.cd_nameptr = (const u_int8_t *)fcnp->cn_nameptr;
91447636 3162 tmpdesc.cd_namelen = fcnp->cn_namelen;
2d21ac55 3163 tmpdesc.cd_parentcnid = fdcp->c_fileid;
91447636 3164 tmpdesc.cd_hint = fdcp->c_childhint;
2d21ac55
A
3165 tmpdesc.cd_flags = fcp->c_desc.cd_flags & CD_ISDIR;
3166 tmpdesc.cd_encoding = 0;
91447636
A
3167
3168 lockflags = hfs_systemfile_lock(hfsmp, SFL_CATALOG, HFS_SHARED_LOCK);
3169
3170 if (cat_lookup(hfsmp, &tmpdesc, 0, NULL, NULL, NULL, &real_cnid) != 0) {
3171 hfs_systemfile_unlock(hfsmp, lockflags);
3172 goto out;
55e303ae 3173 }
91447636
A
3174
3175 // use the real cnid instead of whatever happened to be there
3176 from_desc.cd_cnid = real_cnid;
3177 hfs_systemfile_unlock(hfsmp, lockflags);
55e303ae
A
3178 }
3179
3180 /*
3181 * Reserve some space in the Catalog file.
3182 */
3183 if ((error = cat_preflight(hfsmp, CAT_RENAME + CAT_DELETE, &cookie, p))) {
3184 goto out;
3185 }
91447636 3186 got_cookie = 1;
55e303ae
A
3187
3188 /*
91447636 3189 * If the destination exists then it may need to be removed.
55e303ae 3190 */
55e303ae 3191 if (tvp) {
55e303ae 3192 /*
2d21ac55
A
3193 * When fvp matches tvp they could be case variants
3194 * or matching hard links.
55e303ae 3195 */
91447636 3196 if (fvp == tvp) {
2d21ac55
A
3197 if (!(fcp->c_flag & C_HARDLINK)) {
3198 goto skip_rm; /* simple case variant */
3199
3200 } else if ((fdvp != tdvp) ||
3201 (hfsmp->hfs_flags & HFS_CASE_SENSITIVE)) {
3202 goto out; /* matching hardlinks, nothing to do */
3203
3204 } else if (hfs_namecmp((const u_int8_t *)fcnp->cn_nameptr, fcnp->cn_namelen,
3205 (const u_int8_t *)tcnp->cn_nameptr, tcnp->cn_namelen) == 0) {
3206 goto skip_rm; /* case-variant hardlink in the same dir */
3207 } else {
3208 goto out; /* matching hardlink, nothing to do */
91447636 3209 }
91447636
A
3210 }
3211
3212 if (vnode_isdir(tvp))
3213 error = hfs_removedir(tdvp, tvp, tcnp, HFSRM_SKIP_RESERVE);
3214 else {
b0d623f7
A
3215 error = hfs_removefile(tdvp, tvp, tcnp, 0, HFSRM_SKIP_RESERVE, 0, tvp_rsrc);
3216
3217 /*
3218 * If the destination file had a rsrc fork vnode, it may have been cleaned up
3219 * in hfs_removefile if it was not busy (had no usecounts). This is possible
3220 * because we grabbed the iocount on the rsrc fork safely at the beginning
3221 * of the function before we did the lockfour. However, we may still need
3222 * to take action to prevent block leaks, so aggressively recycle the vnode
3223 * if possible. The vnode cannot be recycled because we hold an iocount on it.
cf7d32b8 3224 */
b0d623f7
A
3225
3226 if ((error == 0) && (tcp->c_flag & C_DELETED) && tvp_rsrc && !vnode_isinuse(tvp_rsrc, 0)) {
cf7d32b8 3227 recycle_rsrc = 1;
b0d623f7 3228 }
91447636 3229 }
55e303ae 3230
55e303ae
A
3231 if (error)
3232 goto out;
91447636 3233 tvp_deleted = 1;
55e303ae 3234 }
2d21ac55 3235skip_rm:
55e303ae 3236 /*
b0d623f7
A
3237 * All done with tvp and fvp.
3238 *
e2fac8b1
A
3239 * We also jump to this point if there was no destination observed during lookup and namei.
3240 * However, because only iocounts are held at the VFS layer, there is nothing preventing a
3241 * competing thread from racing us and creating a file or dir at the destination of this rename
3242 * operation. If this occurs, it may cause us to get a spurious EEXIST out of the cat_rename
3243 * call below. To preserve rename's atomicity, we need to signal VFS to re-drive the
3244 * namei/lookup and restart the rename operation. EEXIST is an allowable errno to be bubbled
3245 * out of the rename syscall, but not for this reason, since it is a synonym errno for ENOTEMPTY.
3246 * To signal VFS, we return ERECYCLE (which is also used for lookup restarts). This errno
3247 * will be swallowed and it will restart the operation.
55e303ae 3248 */
b0d623f7 3249
91447636 3250 lockflags = hfs_systemfile_lock(hfsmp, SFL_CATALOG, HFS_EXCLUSIVE_LOCK);
d7e50217 3251 error = cat_rename(hfsmp, &from_desc, &tdcp->c_desc, &to_desc, &out_desc);
91447636 3252 hfs_systemfile_unlock(hfsmp, lockflags);
55e303ae
A
3253
3254 if (error) {
e2fac8b1
A
3255 if (error == EEXIST) {
3256 error = ERECYCLE;
3257 }
d7e50217 3258 goto out;
55e303ae 3259 }
9bccf70c 3260
91447636 3261 /* Invalidate negative cache entries in the destination directory */
2d21ac55 3262 if (tdcp->c_flag & C_NEG_ENTRIES) {
91447636 3263 cache_purge_negatives(tdvp);
2d21ac55
A
3264 tdcp->c_flag &= ~C_NEG_ENTRIES;
3265 }
91447636 3266
d7e50217 3267 /* Update cnode's catalog descriptor */
91447636 3268 replace_desc(fcp, &out_desc);
2d21ac55 3269 fcp->c_parentcnid = tdcp->c_fileid;
91447636 3270 fcp->c_hint = 0;
9bccf70c 3271
91447636 3272 hfs_volupdate(hfsmp, vnode_isdir(fvp) ? VOL_RMDIR : VOL_RMFILE,
d7e50217 3273 (fdcp->c_cnid == kHFSRootFolderID));
91447636 3274 hfs_volupdate(hfsmp, vnode_isdir(fvp) ? VOL_MKDIR : VOL_MKFILE,
d7e50217
A
3275 (tdcp->c_cnid == kHFSRootFolderID));
3276
d7e50217 3277 /* Update both parent directories. */
b4c24cb9 3278 if (fdvp != tdvp) {
2d21ac55
A
3279 if (vnode_isdir(fvp)) {
3280 /* If the source directory has directory hard link
3281 * descendants, set the kHFSHasChildLinkBit in the
3282 * destination parent hierarchy
3283 */
3284 if ((fcp->c_attr.ca_recflags & kHFSHasChildLinkMask) &&
3285 !(tdcp->c_attr.ca_recflags & kHFSHasChildLinkMask)) {
3286
3287 tdcp->c_attr.ca_recflags |= kHFSHasChildLinkMask;
3288
3289 error = cat_set_childlinkbit(hfsmp, tdcp->c_parentcnid);
3290 if (error) {
3291 printf ("hfs_vnop_rename: error updating parent chain for %u\n", tdcp->c_cnid);
3292 error = 0;
3293 }
3294 }
3295 INC_FOLDERCOUNT(hfsmp, tdcp->c_attr);
3296 DEC_FOLDERCOUNT(hfsmp, fdcp->c_attr);
3297 }
d7e50217 3298 tdcp->c_entries++;
2d21ac55 3299 tdcp->c_dirchangecnt++;
d7e50217
A
3300 if (fdcp->c_entries > 0)
3301 fdcp->c_entries--;
2d21ac55 3302 fdcp->c_dirchangecnt++;
91447636
A
3303 fdcp->c_touch_chgtime = TRUE;
3304 fdcp->c_touch_modtime = TRUE;
3305
3306 fdcp->c_flag |= C_FORCEUPDATE; // XXXdbg - force it out!
3307 (void) hfs_update(fdvp, 0);
b4c24cb9 3308 }
d7e50217 3309 tdcp->c_childhint = out_desc.cd_hint; /* Cache directory's location */
91447636
A
3310 tdcp->c_touch_chgtime = TRUE;
3311 tdcp->c_touch_modtime = TRUE;
b4c24cb9 3312
91447636
A
3313 tdcp->c_flag |= C_FORCEUPDATE; // XXXdbg - force it out!
3314 (void) hfs_update(tdvp, 0);
d7e50217 3315out:
91447636 3316 if (got_cookie) {
55e303ae
A
3317 cat_postflight(hfsmp, &cookie, p);
3318 }
b4c24cb9 3319 if (started_tr) {
91447636 3320 hfs_end_transaction(hfsmp);
b4c24cb9
A
3321 }
3322
2d21ac55
A
3323 fdcp->c_flag &= ~C_DIR_MODIFICATION;
3324 wakeup((caddr_t)&fdcp->c_flag);
3325 if (fdvp != tdvp) {
3326 tdcp->c_flag &= ~C_DIR_MODIFICATION;
3327 wakeup((caddr_t)&tdcp->c_flag);
3328 }
3329
91447636 3330 if (took_trunc_lock)
2d21ac55 3331 hfs_unlock_truncate(VTOC(tvp), TRUE);
91447636
A
3332
3333 hfs_unlockfour(fdcp, fcp, tdcp, tcp);
b0d623f7
A
3334
3335 /*
3336 * Now that we've dropped all of the locks, we need to force an inactive and a recycle
3337 * on the old destination's rsrc fork to prevent a leak of its blocks. Note that
3338 * doing the ref/rele is to twiddle the VL_NEEDINACTIVE bit of the vnode's flags, so that
3339 * on the last vnode_put for this vnode, we will force inactive to get triggered.
3340 * We hold an iocount from the beginning of this function so we know it couldn't have been
3341 * recycled already.
cf7d32b8 3342 */
b0d623f7
A
3343 if (recycle_rsrc) {
3344 int vref;
3345 vref = vnode_ref(tvp_rsrc);
3346 if (vref == 0) {
3347 vnode_rele(tvp_rsrc);
3348 }
3349 vnode_recycle(tvp_rsrc);
cf7d32b8
A
3350 }
3351
b0d623f7
A
3352 /* Now vnode_put the resource forks vnodes if necessary */
3353 if (tvp_rsrc) {
3354 vnode_put(tvp_rsrc);
3355 }
3356 if (fvp_rsrc) {
3357 vnode_put(fvp_rsrc);
3358 }
cf7d32b8 3359
d7e50217 3360 /* After tvp is removed the only acceptable error is EIO */
55e303ae 3361 if (error && tvp_deleted)
d7e50217
A
3362 error = EIO;
3363
3364 return (error);
9bccf70c
A
3365}
3366
3367
9bccf70c 3368/*
91447636
A
3369 * Make a directory.
3370 */
9bccf70c 3371static int
91447636 3372hfs_vnop_mkdir(struct vnop_mkdir_args *ap)
9bccf70c 3373{
91447636
A
3374 /***** HACK ALERT ********/
3375 ap->a_cnp->cn_flags |= MAKEENTRY;
3376 return hfs_makenode(ap->a_dvp, ap->a_vpp, ap->a_cnp, ap->a_vap, ap->a_context);
9bccf70c
A
3377}
3378
3379
3380/*
91447636
A
3381 * Create a symbolic link.
3382 */
9bccf70c 3383static int
91447636 3384hfs_vnop_symlink(struct vnop_symlink_args *ap)
9bccf70c 3385{
91447636
A
3386 struct vnode **vpp = ap->a_vpp;
3387 struct vnode *dvp = ap->a_dvp;
3388 struct vnode *vp = NULL;
2d21ac55 3389 struct cnode *cp = NULL;
b4c24cb9 3390 struct hfsmount *hfsmp;
9bccf70c 3391 struct filefork *fp;
9bccf70c 3392 struct buf *bp = NULL;
91447636
A
3393 char *datap;
3394 int started_tr = 0;
2d21ac55
A
3395 u_int32_t len;
3396 int error;
9bccf70c
A
3397
3398 /* HFS standard disks don't support symbolic links */
91447636
A
3399 if (VTOVCB(dvp)->vcbSigWord != kHFSPlusSigWord)
3400 return (ENOTSUP);
9bccf70c
A
3401
3402 /* Check for empty target name */
91447636 3403 if (ap->a_target[0] == 0)
9bccf70c 3404 return (EINVAL);
b4c24cb9 3405
2d21ac55
A
3406 hfsmp = VTOHFS(dvp);
3407 len = strlen(ap->a_target);
3408
3409 /* Check for free space */
3410 if (((u_int64_t)hfs_freeblks(hfsmp, 0) * (u_int64_t)hfsmp->blockSize) < len) {
3411 return (ENOSPC);
3412 }
3413
9bccf70c 3414 /* Create the vnode */
91447636
A
3415 ap->a_vap->va_mode |= S_IFLNK;
3416 if ((error = hfs_makenode(dvp, vpp, ap->a_cnp, ap->a_vap, ap->a_context))) {
3417 goto out;
b4c24cb9 3418 }
9bccf70c 3419 vp = *vpp;
2d21ac55
A
3420 if ((error = hfs_lock(VTOC(vp), HFS_EXCLUSIVE_LOCK))) {
3421 goto out;
3422 }
3423 cp = VTOC(vp);
9bccf70c 3424 fp = VTOF(vp);
2d21ac55
A
3425
3426 if (cp->c_flag & (C_NOEXISTS | C_DELETED)) {
3427 goto out;
3428 }
9bccf70c 3429
d7e50217 3430#if QUOTA
2d21ac55 3431 (void)hfs_getinoquota(cp);
d7e50217
A
3432#endif /* QUOTA */
3433
91447636
A
3434 if ((error = hfs_start_transaction(hfsmp)) != 0) {
3435 goto out;
b4c24cb9 3436 }
91447636 3437 started_tr = 1;
b4c24cb9 3438
91447636
A
3439 /*
3440 * Allocate space for the link.
3441 *
3442 * Since we're already inside a transaction,
3443 * tell hfs_truncate to skip the ubc_setsize.
3444 *
3445 * Don't need truncate lock since a symlink is treated as a system file.
3446 */
b0d623f7 3447 error = hfs_truncate(vp, len, IO_NOZEROFILL, 1, 0, ap->a_context);
2d21ac55
A
3448
3449 /* On errors, remove the symlink file */
3450 if (error) {
3451 /*
3452 * End the transaction so we don't re-take the cnode lock
3453 * below while inside a transaction (lock order violation).
3454 */
3455 hfs_end_transaction(hfsmp);
3456
3457 /* hfs_removefile() requires holding the truncate lock */
3458 hfs_unlock(cp);
3459 hfs_lock_truncate(cp, TRUE);
3460 hfs_lock(cp, HFS_FORCE_LOCK);
3461
3462 if (hfs_start_transaction(hfsmp) != 0) {
3463 started_tr = 0;
3464 hfs_unlock_truncate(cp, TRUE);
3465 goto out;
3466 }
3467
b0d623f7 3468 (void) hfs_removefile(dvp, vp, ap->a_cnp, 0, 0, 0, NULL);
2d21ac55
A
3469 hfs_unlock_truncate(cp, TRUE);
3470 goto out;
3471 }
9bccf70c
A
3472
3473 /* Write the link to disk */
593a1d5f 3474 bp = buf_getblk(vp, (daddr64_t)0, roundup((int)fp->ff_size, hfsmp->hfs_physical_block_size),
9bccf70c 3475 0, 0, BLK_META);
b4c24cb9
A
3476 if (hfsmp->jnl) {
3477 journal_modify_block_start(hfsmp->jnl, bp);
3478 }
91447636
A
3479 datap = (char *)buf_dataptr(bp);
3480 bzero(datap, buf_size(bp));
3481 bcopy(ap->a_target, datap, len);
3482
b4c24cb9 3483 if (hfsmp->jnl) {
2d21ac55 3484 journal_modify_block_end(hfsmp->jnl, bp, NULL, NULL);
b4c24cb9 3485 } else {
91447636 3486 buf_bawrite(bp);
b4c24cb9 3487 }
91447636
A
3488 /*
3489 * We defered the ubc_setsize for hfs_truncate
3490 * since we were inside a transaction.
3491 *
3492 * We don't need to drop the cnode lock here
3493 * since this is a symlink.
3494 */
3495 ubc_setsize(vp, len);
9bccf70c 3496out:
91447636
A
3497 if (started_tr)
3498 hfs_end_transaction(hfsmp);
2d21ac55
A
3499 if ((cp != NULL) && (vp != NULL)) {
3500 hfs_unlock(cp);
3501 }
3502 if (error) {
3503 if (vp) {
3504 vnode_put(vp);
3505 }
3506 *vpp = NULL;
b4c24cb9 3507 }
9bccf70c
A
3508 return (error);
3509}
3510
3511
91447636
A
3512/* structures to hold a "." or ".." directory entry */
3513struct hfs_stddotentry {
3514 u_int32_t d_fileno; /* unique file number */
3515 u_int16_t d_reclen; /* length of this structure */
3516 u_int8_t d_type; /* dirent file type */
3517 u_int8_t d_namlen; /* len of filename */
3518 char d_name[4]; /* "." or ".." */
9bccf70c
A
3519};
3520
91447636
A
3521struct hfs_extdotentry {
3522 u_int64_t d_fileno; /* unique file number */
3523 u_int64_t d_seekoff; /* seek offset (optional, used by servers) */
3524 u_int16_t d_reclen; /* length of this structure */
3525 u_int16_t d_namlen; /* len of filename */
3526 u_int8_t d_type; /* dirent file type */
3527 u_char d_name[3]; /* "." or ".." */
3528};
9bccf70c 3529
91447636
A
3530typedef union {
3531 struct hfs_stddotentry std;
3532 struct hfs_extdotentry ext;
3533} hfs_dotentry_t;
9bccf70c
A
3534
3535/*
91447636
A
3536 * hfs_vnop_readdir reads directory entries into the buffer pointed
3537 * to by uio, in a filesystem independent format. Up to uio_resid
3538 * bytes of data can be transferred. The data in the buffer is a
3539 * series of packed dirent structures where each one contains the
3540 * following entries:
3541 *
3542 * u_int32_t d_fileno; // file number of entry
3543 * u_int16_t d_reclen; // length of this record
3544 * u_int8_t d_type; // file type
3545 * u_int8_t d_namlen; // length of string in d_name
3546 * char d_name[MAXNAMELEN+1]; // null terminated file name
3547 *
3548 * The current position (uio_offset) refers to the next block of
3549 * entries. The offset can only be set to a value previously
3550 * returned by hfs_vnop_readdir or zero. This offset does not have
3551 * to match the number of bytes returned (in uio_resid).
3552 *
3553 * In fact, the offset used by HFS is essentially an index (26 bits)
3554 * with a tag (6 bits). The tag is for associating the next request
3555 * with the current request. This enables us to have multiple threads
3556 * reading the directory while the directory is also being modified.
3557 *
3558 * Each tag/index pair is tied to a unique directory hint. The hint
3559 * contains information (filename) needed to build the catalog b-tree
3560 * key for finding the next set of entries.
2d21ac55
A
3561 *
3562 * If the directory is marked as deleted-but-in-use (cp->c_flag & C_DELETED),
3563 * do NOT synthesize entries for "." and "..".
9bccf70c 3564 */
9bccf70c 3565static int
91447636
A
3566hfs_vnop_readdir(ap)
3567 struct vnop_readdir_args /* {
3568 vnode_t a_vp;
3569 uio_t a_uio;
3570 int a_flags;
3571 int *a_eofflag;
3572 int *a_numdirent;
3573 vfs_context_t a_context;
9bccf70c
A
3574 } */ *ap;
3575{
91447636
A
3576 struct vnode *vp = ap->a_vp;
3577 uio_t uio = ap->a_uio;
3578 struct cnode *cp;
3579 struct hfsmount *hfsmp;
3580 directoryhint_t *dirhint = NULL;
3581 directoryhint_t localhint;
3582 off_t offset;
3583 off_t startoffset;
3584 int error = 0;
9bccf70c 3585 int eofflag = 0;
91447636
A
3586 user_addr_t user_start = 0;
3587 user_size_t user_len = 0;
3588 int index;
3589 unsigned int tag;
3590 int items;
3591 int lockflags;
3592 int extended;
3593 int nfs_cookies;
91447636
A
3594 cnid_t cnid_hint = 0;
3595
3596 items = 0;
3597 startoffset = offset = uio_offset(uio);
91447636
A
3598 extended = (ap->a_flags & VNODE_READDIR_EXTENDED);
3599 nfs_cookies = extended && (ap->a_flags & VNODE_READDIR_REQSEEKOFF);
3600
3601 /* Sanity check the uio data. */
b0d623f7 3602 if (uio_iovcnt(uio) > 1)
91447636 3603 return (EINVAL);
91447636
A
3604 /* Note that the dirhint calls require an exclusive lock. */
3605 if ((error = hfs_lock(VTOC(vp), HFS_EXCLUSIVE_LOCK)))
3606 return (error);
3607 cp = VTOC(vp);
3608 hfsmp = VTOHFS(vp);
55e303ae 3609
91447636
A
3610 /* Pick up cnid hint (if any). */
3611 if (nfs_cookies) {
3612 cnid_hint = (cnid_t)(uio_offset(uio) >> 32);
3613 uio_setoffset(uio, uio_offset(uio) & 0x00000000ffffffffLL);
3a60a9f5
A
3614 if (cnid_hint == INT_MAX) { /* searching pass the last item */
3615 eofflag = 1;
3616 goto out;
3617 }
91447636
A
3618 }
3619 /*
2d21ac55
A
3620 * Synthesize entries for "." and "..", unless the directory has
3621 * been deleted, but not closed yet (lazy delete in progress).
91447636 3622 */
2d21ac55 3623 if (offset == 0 && !(cp->c_flag & C_DELETED)) {
91447636
A
3624 hfs_dotentry_t dotentry[2];
3625 size_t uiosize;
3626
3627 if (extended) {
3628 struct hfs_extdotentry *entry = &dotentry[0].ext;
3629
3630 entry->d_fileno = cp->c_cnid;
3631 entry->d_reclen = sizeof(struct hfs_extdotentry);
3632 entry->d_type = DT_DIR;
3633 entry->d_namlen = 1;
3634 entry->d_name[0] = '.';
3635 entry->d_name[1] = '\0';
3636 entry->d_name[2] = '\0';
3637 entry->d_seekoff = 1;
3638
3639 ++entry;
3640 entry->d_fileno = cp->c_parentcnid;
3641 entry->d_reclen = sizeof(struct hfs_extdotentry);
3642 entry->d_type = DT_DIR;
3643 entry->d_namlen = 2;
3644 entry->d_name[0] = '.';
3645 entry->d_name[1] = '.';
3646 entry->d_name[2] = '\0';
3647 entry->d_seekoff = 2;
3648 uiosize = 2 * sizeof(struct hfs_extdotentry);
3649 } else {
3650 struct hfs_stddotentry *entry = &dotentry[0].std;
3651
3652 entry->d_fileno = cp->c_cnid;
3653 entry->d_reclen = sizeof(struct hfs_stddotentry);
3654 entry->d_type = DT_DIR;
3655 entry->d_namlen = 1;
3656 *(int *)&entry->d_name[0] = 0;
3657 entry->d_name[0] = '.';
3658
3659 ++entry;
3660 entry->d_fileno = cp->c_parentcnid;
3661 entry->d_reclen = sizeof(struct hfs_stddotentry);
3662 entry->d_type = DT_DIR;
3663 entry->d_namlen = 2;
3664 *(int *)&entry->d_name[0] = 0;
3665 entry->d_name[0] = '.';
3666 entry->d_name[1] = '.';
3667 uiosize = 2 * sizeof(struct hfs_stddotentry);
3668 }
3669 if ((error = uiomove((caddr_t)&dotentry, uiosize, uio))) {
3670 goto out;
3671 }
3672 offset += 2;
3673 }
9bccf70c 3674
91447636
A
3675 /* If there are no real entries then we're done. */
3676 if (cp->c_entries == 0) {
3677 error = 0;
3678 eofflag = 1;
3679 uio_setoffset(uio, offset);
3680 goto seekoffcalc;
3681 }
3682
3683 //
b4c24cb9
A
3684 // We have to lock the user's buffer here so that we won't
3685 // fault on it after we've acquired a shared lock on the
3686 // catalog file. The issue is that you can get a 3-way
3687 // deadlock if someone else starts a transaction and then
3688 // tries to lock the catalog file but can't because we're
3689 // here and we can't service our page fault because VM is
3690 // blocked trying to start a transaction as a result of
3691 // trying to free up pages for our page fault. It's messy
2d21ac55 3692 // but it does happen on dual-processors that are paging
b4c24cb9
A
3693 // heavily (see radar 3082639 for more info). By locking
3694 // the buffer up-front we prevent ourselves from faulting
3695 // while holding the shared catalog file lock.
3696 //
3697 // Fortunately this and hfs_search() are the only two places
3698 // currently (10/30/02) that can fault on user data with a
3699 // shared lock on the catalog file.
3700 //
91447636
A
3701 if (hfsmp->jnl && uio_isuserspace(uio)) {
3702 user_start = uio_curriovbase(uio);
3703 user_len = uio_curriovlen(uio);
b4c24cb9 3704
91447636
A
3705 if ((error = vslock(user_start, user_len)) != 0) {
3706 user_start = 0;
3707 goto out;
b4c24cb9
A
3708 }
3709 }
91447636
A
3710 /* Convert offset into a catalog directory index. */
3711 index = (offset & HFS_INDEX_MASK) - 2;
3712 tag = offset & ~HFS_INDEX_MASK;
3713
3714 /* Lock catalog during cat_findname and cat_getdirentries. */
3715 lockflags = hfs_systemfile_lock(hfsmp, SFL_CATALOG, HFS_SHARED_LOCK);
3716
3717 /* When called from NFS, try and resolve a cnid hint. */
3718 if (nfs_cookies && cnid_hint != 0) {
3719 if (cat_findname(hfsmp, cnid_hint, &localhint.dh_desc) == 0) {
2d21ac55 3720 if ( localhint.dh_desc.cd_parentcnid == cp->c_fileid) {
91447636
A
3721 localhint.dh_index = index - 1;
3722 localhint.dh_time = 0;
b36670ce 3723 bzero(&localhint.dh_link, sizeof(localhint.dh_link));
91447636
A
3724 dirhint = &localhint; /* don't forget to release the descriptor */
3725 } else {
3726 cat_releasedesc(&localhint.dh_desc);
3727 }
9bccf70c 3728 }
9bccf70c
A
3729 }
3730
91447636
A
3731 /* Get a directory hint (cnode must be locked exclusive) */
3732 if (dirhint == NULL) {
2d21ac55 3733 dirhint = hfs_getdirhint(cp, ((index - 1) & HFS_INDEX_MASK) | tag, 0);
55e303ae 3734
91447636
A
3735 /* Hide tag from catalog layer. */
3736 dirhint->dh_index &= HFS_INDEX_MASK;
3737 if (dirhint->dh_index == HFS_INDEX_MASK) {
3738 dirhint->dh_index = -1;
ccc36f2f 3739 }
55e303ae 3740 }
91447636 3741
2d21ac55
A
3742 if (index == 0) {
3743 dirhint->dh_threadhint = cp->c_dirthreadhint;
3744 }
7e4a7d39
A
3745 else {
3746 /*
3747 * If we have a non-zero index, there is a possibility that during the last
3748 * call to hfs_vnop_readdir we hit EOF for this directory. If that is the case
3749 * then we don't want to return any new entries for the caller. Just return 0
3750 * items, mark the eofflag, and bail out. Because we won't have done any work, the
3751 * code at the end of the function will release the dirhint for us.
3752 *
3753 * Don't forget to unlock the catalog lock on the way out, too.
3754 */
3755 if (dirhint->dh_desc.cd_flags & CD_EOF) {
3756 error = 0;
3757 eofflag = 1;
3758 uio_setoffset(uio, startoffset);
3759 hfs_systemfile_unlock (hfsmp, lockflags);
3760
3761 goto seekoffcalc;
3762 }
3763 }
2d21ac55 3764
91447636 3765 /* Pack the buffer with dirent entries. */
3a60a9f5 3766 error = cat_getdirentries(hfsmp, cp->c_entries, dirhint, uio, extended, &items, &eofflag);
55e303ae 3767
2d21ac55
A
3768 if (index == 0 && error == 0) {
3769 cp->c_dirthreadhint = dirhint->dh_threadhint;
3770 }
3771
91447636
A
3772 hfs_systemfile_unlock(hfsmp, lockflags);
3773
3774 if (error != 0) {
3775 goto out;
3776 }
3777
3778 /* Get index to the next item */
3779 index += items;
3780
3781 if (items >= (int)cp->c_entries) {
9bccf70c 3782 eofflag = 1;
9bccf70c
A
3783 }
3784
91447636
A
3785 /* Convert catalog directory index back into an offset. */
3786 while (tag == 0)
3787 tag = (++cp->c_dirhinttag) << HFS_INDEX_BITS;
3788 uio_setoffset(uio, (index + 2) | tag);
3789 dirhint->dh_index |= tag;
9bccf70c 3790
91447636
A
3791seekoffcalc:
3792 cp->c_touch_acctime = TRUE;
9bccf70c 3793
91447636
A
3794 if (ap->a_numdirent) {
3795 if (startoffset == 0)
3796 items += 2;
3797 *ap->a_numdirent = items;
9bccf70c 3798 }
9bccf70c 3799
91447636 3800out:
d1ecb069 3801 if (user_start) {
b4c24cb9
A
3802 vsunlock(user_start, user_len, TRUE);
3803 }
91447636
A
3804 /* If we didn't do anything then go ahead and dump the hint. */
3805 if ((dirhint != NULL) &&
3806 (dirhint != &localhint) &&
3807 (uio_offset(uio) == startoffset)) {
3808 hfs_reldirhint(cp, dirhint);
3809 eofflag = 1;
3810 }
3811 if (ap->a_eofflag) {
9bccf70c 3812 *ap->a_eofflag = eofflag;
91447636
A
3813 }
3814 if (dirhint == &localhint) {
3815 cat_releasedesc(&localhint.dh_desc);
3816 }
3817 hfs_unlock(cp);
3818 return (error);
9bccf70c
A
3819}
3820
3821
3822/*
91447636
A
3823 * Read contents of a symbolic link.
3824 */
9bccf70c 3825static int
91447636
A
3826hfs_vnop_readlink(ap)
3827 struct vnop_readlink_args /* {
9bccf70c
A
3828 struct vnode *a_vp;
3829 struct uio *a_uio;
91447636 3830 vfs_context_t a_context;
9bccf70c
A
3831 } */ *ap;
3832{
9bccf70c
A
3833 struct vnode *vp = ap->a_vp;
3834 struct cnode *cp;
3835 struct filefork *fp;
91447636 3836 int error;
9bccf70c 3837
91447636 3838 if (!vnode_islnk(vp))
9bccf70c
A
3839 return (EINVAL);
3840
91447636
A
3841 if ((error = hfs_lock(VTOC(vp), HFS_EXCLUSIVE_LOCK)))
3842 return (error);
9bccf70c
A
3843 cp = VTOC(vp);
3844 fp = VTOF(vp);
3845
3846 /* Zero length sym links are not allowed */
3847 if (fp->ff_size == 0 || fp->ff_size > MAXPATHLEN) {
2d21ac55 3848 printf("hfs: zero length symlink on fileid %d\n", cp->c_fileid);
91447636
A
3849 error = EINVAL;
3850 goto exit;
9bccf70c
A
3851 }
3852
3853 /* Cache the path so we don't waste buffer cache resources */
3854 if (fp->ff_symlinkptr == NULL) {
3855 struct buf *bp = NULL;
3856
3857 MALLOC(fp->ff_symlinkptr, char *, fp->ff_size, M_TEMP, M_WAITOK);
b0d623f7
A
3858 if (fp->ff_symlinkptr == NULL) {
3859 error = ENOMEM;
3860 goto exit;
3861 }
91447636 3862 error = (int)buf_meta_bread(vp, (daddr64_t)0,
593a1d5f 3863 roundup((int)fp->ff_size, VTOHFS(vp)->hfs_physical_block_size),
91447636
A
3864 vfs_context_ucred(ap->a_context), &bp);
3865 if (error) {
9bccf70c 3866 if (bp)
91447636 3867 buf_brelse(bp);
9bccf70c
A
3868 if (fp->ff_symlinkptr) {
3869 FREE(fp->ff_symlinkptr, M_TEMP);
3870 fp->ff_symlinkptr = NULL;
3871 }
91447636 3872 goto exit;
9bccf70c 3873 }
91447636 3874 bcopy((char *)buf_dataptr(bp), fp->ff_symlinkptr, (size_t)fp->ff_size);
9bccf70c 3875
91447636
A
3876 if (VTOHFS(vp)->jnl && (buf_flags(bp) & B_LOCKED) == 0) {
3877 buf_markinvalid(bp); /* data no longer needed */
3878 }
3879 buf_brelse(bp);
55e303ae 3880 }
91447636 3881 error = uiomove((caddr_t)fp->ff_symlinkptr, (int)fp->ff_size, ap->a_uio);
9bccf70c 3882
91447636
A
3883 /*
3884 * Keep track blocks read
3885 */
3886 if ((VTOHFS(vp)->hfc_stage == HFC_RECORDING) && (error == 0)) {
3887
3888 /*
3889 * If this file hasn't been seen since the start of
3890 * the current sampling period then start over.
3891 */
3892 if (cp->c_atime < VTOHFS(vp)->hfc_timebase)
3893 VTOF(vp)->ff_bytesread = fp->ff_size;
3894 else
3895 VTOF(vp)->ff_bytesread += fp->ff_size;
3896
3897 // if (VTOF(vp)->ff_bytesread > fp->ff_size)
3898 // cp->c_touch_acctime = TRUE;
3899 }
9bccf70c 3900
91447636
A
3901exit:
3902 hfs_unlock(cp);
3903 return (error);
9bccf70c
A
3904}
3905
3906
3907/*
91447636
A
3908 * Get configurable pathname variables.
3909 */
9bccf70c 3910static int
91447636
A
3911hfs_vnop_pathconf(ap)
3912 struct vnop_pathconf_args /* {
9bccf70c
A
3913 struct vnode *a_vp;
3914 int a_name;
3915 int *a_retval;
91447636 3916 vfs_context_t a_context;
9bccf70c
A
3917 } */ *ap;
3918{
9bccf70c
A
3919 switch (ap->a_name) {
3920 case _PC_LINK_MAX:
91447636 3921 if (VTOHFS(ap->a_vp)->hfs_flags & HFS_STANDARD)
9bccf70c 3922 *ap->a_retval = 1;
91447636
A
3923 else
3924 *ap->a_retval = HFS_LINK_MAX;
9bccf70c
A
3925 break;
3926 case _PC_NAME_MAX:
91447636
A
3927 if (VTOHFS(ap->a_vp)->hfs_flags & HFS_STANDARD)
3928 *ap->a_retval = kHFSMaxFileNameChars; /* 255 */
3929 else
3930 *ap->a_retval = kHFSPlusMaxFileNameChars; /* 31 */
9bccf70c
A
3931 break;
3932 case _PC_PATH_MAX:
91447636 3933 *ap->a_retval = PATH_MAX; /* 1024 */
9bccf70c 3934 break;
55e303ae
A
3935 case _PC_PIPE_BUF:
3936 *ap->a_retval = PIPE_BUF;
3937 break;
9bccf70c 3938 case _PC_CHOWN_RESTRICTED:
2d21ac55 3939 *ap->a_retval = 200112; /* _POSIX_CHOWN_RESTRICTED */
9bccf70c
A
3940 break;
3941 case _PC_NO_TRUNC:
2d21ac55 3942 *ap->a_retval = 200112; /* _POSIX_NO_TRUNC */
9bccf70c
A
3943 break;
3944 case _PC_NAME_CHARS_MAX:
3945 *ap->a_retval = kHFSPlusMaxFileNameChars;
3946 break;
3947 case _PC_CASE_SENSITIVE:
55e303ae
A
3948 if (VTOHFS(ap->a_vp)->hfs_flags & HFS_CASE_SENSITIVE)
3949 *ap->a_retval = 1;
3950 else
3951 *ap->a_retval = 0;
9bccf70c
A
3952 break;
3953 case _PC_CASE_PRESERVING:
3954 *ap->a_retval = 1;
3955 break;
2d21ac55 3956 case _PC_FILESIZEBITS:
b0d623f7
A
3957 if (VTOHFS(ap->a_vp)->hfs_flags & HFS_STANDARD)
3958 *ap->a_retval = 32;
3959 else
3960 *ap->a_retval = 64; /* number of bits to store max file size */
2d21ac55 3961 break;
9bccf70c 3962 default:
55e303ae 3963 return (EINVAL);
9bccf70c
A
3964 }
3965
91447636 3966 return (0);
9bccf70c
A
3967}
3968
3969
9bccf70c 3970/*
91447636
A
3971 * Update a cnode's on-disk metadata.
3972 *
3973 * If waitfor is set, then wait for the disk write of
3974 * the node to complete.
3975 *
3976 * The cnode must be locked exclusive
9bccf70c 3977 */
91447636
A
3978__private_extern__
3979int
3980hfs_update(struct vnode *vp, __unused int waitfor)
9bccf70c 3981{
91447636 3982 struct cnode *cp = VTOC(vp);
9bccf70c
A
3983 struct proc *p;
3984 struct cat_fork *dataforkp = NULL;
3985 struct cat_fork *rsrcforkp = NULL;
3986 struct cat_fork datafork;
4a3eedf9 3987 struct cat_fork rsrcfork;
b4c24cb9 3988 struct hfsmount *hfsmp;
91447636 3989 int lockflags;
9bccf70c
A
3990 int error;
3991
91447636 3992 p = current_proc();
b4c24cb9
A
3993 hfsmp = VTOHFS(vp);
3994
2d21ac55
A
3995 if (((vnode_issystem(vp) && (cp->c_cnid < kHFSFirstUserCatalogNodeID))) ||
3996 hfsmp->hfs_catalog_vp == NULL){
91447636
A
3997 return (0);
3998 }
3999 if ((hfsmp->hfs_flags & HFS_READ_ONLY) || (cp->c_mode == 0)) {
4000 cp->c_flag &= ~C_MODIFIED;
4001 cp->c_touch_acctime = 0;
4002 cp->c_touch_chgtime = 0;
4003 cp->c_touch_modtime = 0;
9bccf70c
A
4004 return (0);
4005 }
4006
91447636 4007 hfs_touchtimes(hfsmp, cp);
9bccf70c
A
4008
4009 /* Nothing to update. */
91447636 4010 if ((cp->c_flag & (C_MODIFIED | C_FORCEUPDATE)) == 0) {
9bccf70c 4011 return (0);
b4c24cb9 4012 }
9bccf70c
A
4013
4014 if (cp->c_datafork)
4015 dataforkp = &cp->c_datafork->ff_data;
4016 if (cp->c_rsrcfork)
4017 rsrcforkp = &cp->c_rsrcfork->ff_data;
4018
9bccf70c
A
4019 /*
4020 * For delayed allocations updates are
4021 * postponed until an fsync or the file
4022 * gets written to disk.
4023 *
4024 * Deleted files can defer meta data updates until inactive.
55e303ae
A
4025 *
4026 * If we're ever called with the C_FORCEUPDATE flag though
4027 * we have to do the update.
9bccf70c 4028 */
55e303ae 4029 if (ISSET(cp->c_flag, C_FORCEUPDATE) == 0 &&
2d21ac55 4030 (ISSET(cp->c_flag, C_DELETED) ||
9bccf70c 4031 (dataforkp && cp->c_datafork->ff_unallocblocks) ||
55e303ae 4032 (rsrcforkp && cp->c_rsrcfork->ff_unallocblocks))) {
91447636 4033 // cp->c_flag &= ~(C_ACCESS | C_CHANGE | C_UPDATE);
9bccf70c 4034 cp->c_flag |= C_MODIFIED;
b4c24cb9 4035
9bccf70c
A
4036 return (0);
4037 }
4038
91447636
A
4039 if ((error = hfs_start_transaction(hfsmp)) != 0) {
4040 return error;
b4c24cb9 4041 }
b4c24cb9 4042
9bccf70c
A
4043 /*
4044 * For files with invalid ranges (holes) the on-disk
4045 * field representing the size of the file (cf_size)
4046 * must be no larger than the start of the first hole.
4047 */
b0d623f7 4048 if (dataforkp && !TAILQ_EMPTY(&cp->c_datafork->ff_invalidranges)) {
9bccf70c 4049 bcopy(dataforkp, &datafork, sizeof(datafork));
b0d623f7 4050 datafork.cf_size = TAILQ_FIRST(&cp->c_datafork->ff_invalidranges)->rl_start;
9bccf70c 4051 dataforkp = &datafork;
55e303ae
A
4052 } else if (dataforkp && (cp->c_datafork->ff_unallocblocks != 0)) {
4053 // always make sure the block count and the size
4054 // of the file match the number of blocks actually
4055 // allocated to the file on disk
4056 bcopy(dataforkp, &datafork, sizeof(datafork));
4057 // make sure that we don't assign a negative block count
4058 if (cp->c_datafork->ff_blocks < cp->c_datafork->ff_unallocblocks) {
4059 panic("hfs: ff_blocks %d is less than unalloc blocks %d\n",
4060 cp->c_datafork->ff_blocks, cp->c_datafork->ff_unallocblocks);
4061 }
4062 datafork.cf_blocks = (cp->c_datafork->ff_blocks - cp->c_datafork->ff_unallocblocks);
4063 datafork.cf_size = datafork.cf_blocks * HFSTOVCB(hfsmp)->blockSize;
4064 dataforkp = &datafork;
9bccf70c
A
4065 }
4066
4a3eedf9
A
4067 /*
4068 * For resource forks with delayed allocations, make sure
4069 * the block count and file size match the number of blocks
4070 * actually allocated to the file on disk.
4071 */
4072 if (rsrcforkp && (cp->c_rsrcfork->ff_unallocblocks != 0)) {
4073 bcopy(rsrcforkp, &rsrcfork, sizeof(rsrcfork));
4074 rsrcfork.cf_blocks = (cp->c_rsrcfork->ff_blocks - cp->c_rsrcfork->ff_unallocblocks);
4075 rsrcfork.cf_size = rsrcfork.cf_blocks * HFSTOVCB(hfsmp)->blockSize;
4076 rsrcforkp = &rsrcfork;
4077 }
4078
9bccf70c
A
4079 /*
4080 * Lock the Catalog b-tree file.
9bccf70c 4081 */
2d21ac55 4082 lockflags = hfs_systemfile_lock(hfsmp, SFL_CATALOG, HFS_EXCLUSIVE_LOCK);
9bccf70c
A
4083
4084 /* XXX - waitfor is not enforced */
b4c24cb9 4085 error = cat_update(hfsmp, &cp->c_desc, &cp->c_attr, dataforkp, rsrcforkp);
9bccf70c 4086
91447636 4087 hfs_systemfile_unlock(hfsmp, lockflags);
b4c24cb9 4088
55e303ae 4089 /* After the updates are finished, clear the flags */
91447636 4090 cp->c_flag &= ~(C_MODIFIED | C_FORCEUPDATE);
55e303ae 4091
91447636 4092 hfs_end_transaction(hfsmp);
9bccf70c 4093
9bccf70c
A
4094 return (error);
4095}
4096
4097/*
4098 * Allocate a new node
2d21ac55 4099 * Note - Function does not create and return a vnode for whiteout creation.
9bccf70c
A
4100 */
4101static int
91447636
A
4102hfs_makenode(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
4103 struct vnode_attr *vap, vfs_context_t ctx)
9bccf70c 4104{
91447636 4105 struct cnode *cp = NULL;
b0d623f7 4106 struct cnode *dcp = NULL;
9bccf70c
A
4107 struct vnode *tvp;
4108 struct hfsmount *hfsmp;
9bccf70c
A
4109 struct cat_desc in_desc, out_desc;
4110 struct cat_attr attr;
91447636 4111 struct timeval tv;
91447636 4112 int lockflags;
2d21ac55 4113 int error, started_tr = 0;
9bccf70c 4114 enum vtype vnodetype;
91447636 4115 int mode;
9bccf70c 4116
b0d623f7 4117 if ((error = hfs_lock(VTOC(dvp), HFS_EXCLUSIVE_LOCK)))
2d21ac55
A
4118 return (error);
4119
b0d623f7
A
4120 /* set the cnode pointer only after successfully acquiring lock */
4121 dcp = VTOC(dvp);
0b4c1975
A
4122
4123 /* Don't allow creation of new entries in open-unlinked directories */
4124 if ((error = hfs_checkdeleted (dcp))) {
4125 hfs_unlock (dcp);
4126 return error;
4127 }
4128
2d21ac55
A
4129 dcp->c_flag |= C_DIR_MODIFICATION;
4130
9bccf70c
A
4131 hfsmp = VTOHFS(dvp);
4132 *vpp = NULL;
4133 tvp = NULL;
91447636
A
4134 out_desc.cd_flags = 0;
4135 out_desc.cd_nameptr = NULL;
4136
2d21ac55
A
4137 vnodetype = vap->va_type;
4138 if (vnodetype == VNON)
4139 vnodetype = VREG;
4140 mode = MAKEIMODE(vnodetype, vap->va_mode);
9bccf70c 4141
9bccf70c 4142 /* Check if were out of usable disk space. */
2d21ac55 4143 if ((hfs_freeblks(hfsmp, 1) == 0) && (vfs_context_suser(ctx) != 0)) {
9bccf70c
A
4144 error = ENOSPC;
4145 goto exit;
4146 }
4147
91447636
A
4148 microtime(&tv);
4149
9bccf70c
A
4150 /* Setup the default attributes */
4151 bzero(&attr, sizeof(attr));
4152 attr.ca_mode = mode;
2d21ac55
A
4153 attr.ca_linkcount = 1;
4154 if (VATTR_IS_ACTIVE(vap, va_rdev)) {
4155 attr.ca_rdev = vap->va_rdev;
4156 }
4157 if (VATTR_IS_ACTIVE(vap, va_create_time)) {
4158 VATTR_SET_SUPPORTED(vap, va_create_time);
4159 attr.ca_itime = vap->va_create_time.tv_sec;
4160 } else {
4161 attr.ca_itime = tv.tv_sec;
4162 }
4163 if ((hfsmp->hfs_flags & HFS_STANDARD) && gTimeZone.tz_dsttime) {
4164 attr.ca_itime += 3600; /* Same as what hfs_update does */
9bccf70c 4165 }
2d21ac55 4166 attr.ca_atime = attr.ca_ctime = attr.ca_mtime = attr.ca_itime;
91447636 4167 attr.ca_atimeondisk = attr.ca_atime;
2d21ac55
A
4168 if (VATTR_IS_ACTIVE(vap, va_flags)) {
4169 VATTR_SET_SUPPORTED(vap, va_flags);
4170 attr.ca_flags = vap->va_flags;
4171 }
4172
4173 /*
4174 * HFS+ only: all files get ThreadExists
4175 * HFSX only: dirs get HasFolderCount
4176 */
4177 if (!(hfsmp->hfs_flags & HFS_STANDARD)) {
4178 if (vnodetype == VDIR) {
4179 if (hfsmp->hfs_flags & HFS_FOLDERCOUNT)
4180 attr.ca_recflags = kHFSHasFolderCountMask;
4181 } else {
4182 attr.ca_recflags = kHFSThreadExistsMask;
4183 }
4184 }
91447636
A
4185
4186 attr.ca_uid = vap->va_uid;
4187 attr.ca_gid = vap->va_gid;
4188 VATTR_SET_SUPPORTED(vap, va_mode);
4189 VATTR_SET_SUPPORTED(vap, va_uid);
4190 VATTR_SET_SUPPORTED(vap, va_gid);
9bccf70c 4191
b0d623f7
A
4192#if QUOTA
4193 /* check to see if this node's creation would cause us to go over
4194 * quota. If so, abort this operation.
4195 */
4196 if (hfsmp->hfs_flags & HFS_QUOTAS) {
4197 if ((error = hfs_quotacheck(hfsmp, 1, attr.ca_uid, attr.ca_gid,
4198 vfs_context_ucred(ctx)))) {
4199 goto exit;
4200 }
4201 }
4202#endif
4203
4204
9bccf70c
A
4205 /* Tag symlinks with a type and creator. */
4206 if (vnodetype == VLNK) {
4207 struct FndrFileInfo *fip;
4208
4209 fip = (struct FndrFileInfo *)&attr.ca_finderinfo;
4210 fip->fdType = SWAP_BE32(kSymLinkFileType);
4211 fip->fdCreator = SWAP_BE32(kSymLinkCreator);
4212 }
9bccf70c
A
4213 if (cnp->cn_flags & ISWHITEOUT)
4214 attr.ca_flags |= UF_OPAQUE;
4215
4216 /* Setup the descriptor */
2d21ac55 4217 in_desc.cd_nameptr = (const u_int8_t *)cnp->cn_nameptr;
9bccf70c 4218 in_desc.cd_namelen = cnp->cn_namelen;
2d21ac55 4219 in_desc.cd_parentcnid = dcp->c_fileid;
9bccf70c 4220 in_desc.cd_flags = S_ISDIR(mode) ? CD_ISDIR : 0;
91447636
A
4221 in_desc.cd_hint = dcp->c_childhint;
4222 in_desc.cd_encoding = 0;
9bccf70c 4223
91447636
A
4224 if ((error = hfs_start_transaction(hfsmp)) != 0) {
4225 goto exit;
b4c24cb9 4226 }
91447636 4227 started_tr = 1;
b4c24cb9 4228
2d21ac55
A
4229 // have to also lock the attribute file because cat_create() needs
4230 // to check that any fileID it wants to use does not have orphaned
4231 // attributes in it.
4232 lockflags = hfs_systemfile_lock(hfsmp, SFL_CATALOG | SFL_ATTRIBUTE, HFS_EXCLUSIVE_LOCK);
4233
4234 /* Reserve some space in the Catalog file. */
4235 if ((error = cat_preflight(hfsmp, CAT_CREATE, NULL, 0))) {
4236 hfs_systemfile_unlock(hfsmp, lockflags);
55e303ae
A
4237 goto exit;
4238 }
9bccf70c 4239 error = cat_create(hfsmp, &in_desc, &attr, &out_desc);
91447636
A
4240 if (error == 0) {
4241 /* Update the parent directory */
4242 dcp->c_childhint = out_desc.cd_hint; /* Cache directory's location */
91447636 4243 dcp->c_entries++;
2d21ac55
A
4244 if (vnodetype == VDIR) {
4245 INC_FOLDERCOUNT(hfsmp, dcp->c_attr);
4246 }
4247 dcp->c_dirchangecnt++;
91447636
A
4248 dcp->c_ctime = tv.tv_sec;
4249 dcp->c_mtime = tv.tv_sec;
4250 (void) cat_update(hfsmp, &dcp->c_desc, &dcp->c_attr, NULL, NULL);
91447636
A
4251 }
4252 hfs_systemfile_unlock(hfsmp, lockflags);
9bccf70c
A
4253 if (error)
4254 goto exit;
4255
91447636 4256 /* Invalidate negative cache entries in the directory */
2d21ac55 4257 if (dcp->c_flag & C_NEG_ENTRIES) {
91447636 4258 cache_purge_negatives(dvp);
2d21ac55
A
4259 dcp->c_flag &= ~C_NEG_ENTRIES;
4260 }
91447636 4261
9bccf70c
A
4262 hfs_volupdate(hfsmp, vnodetype == VDIR ? VOL_MKDIR : VOL_MKFILE,
4263 (dcp->c_cnid == kHFSRootFolderID));
4264
b4c24cb9
A
4265 // XXXdbg
4266 // have to end the transaction here before we call hfs_getnewvnode()
4267 // because that can cause us to try and reclaim a vnode on a different
4268 // file system which could cause us to start a transaction which can
4269 // deadlock with someone on that other file system (since we could be
4270 // holding two transaction locks as well as various vnodes and we did
4271 // not obtain the locks on them in the proper order).
91447636 4272 //
b4c24cb9
A
4273 // NOTE: this means that if the quota check fails or we have to update
4274 // the change time on a block-special device that those changes
4275 // will happen as part of independent transactions.
4276 //
4277 if (started_tr) {
91447636
A
4278 hfs_end_transaction(hfsmp);
4279 started_tr = 0;
b4c24cb9
A
4280 }
4281
2d21ac55
A
4282 /* Do not create vnode for whiteouts */
4283 if (S_ISWHT(mode)) {
4284 goto exit;
4285 }
4286
91447636
A
4287 /*
4288 * Create a vnode for the object just created.
b0d623f7 4289 *
7e4a7d39
A
4290 * NOTE: Maintaining the cnode lock on the parent directory is important,
4291 * as it prevents race conditions where other threads want to look up entries
4292 * in the directory and/or add things as we are in the process of creating
4293 * the vnode below. However, this has the potential for causing a
4294 * double lock panic when dealing with shadow files on a HFS boot partition.
4295 * The panic could occur if we are not cleaning up after ourselves properly
4296 * when done with a shadow file or in the error cases. The error would occur if we
4297 * try to create a new vnode, and then end up reclaiming another shadow vnode to
4298 * create the new one. However, if everything is working properly, this should
4299 * be a non-issue as we would never enter that reclaim codepath.
91447636
A
4300 *
4301 * The cnode is locked on successful return.
4302 */
2d21ac55 4303 error = hfs_getnewvnode(hfsmp, dvp, cnp, &out_desc, GNV_CREATE, &attr, NULL, &tvp);
9bccf70c
A
4304 if (error)
4305 goto exit;
4306
9bccf70c 4307 cp = VTOC(tvp);
9bccf70c
A
4308 *vpp = tvp;
4309exit:
4310 cat_releasedesc(&out_desc);
b0d623f7 4311
90556fb8 4312 /*
7e4a7d39 4313 * Make sure we release cnode lock on dcp.
90556fb8 4314 */
b0d623f7 4315 if (dcp) {
2d21ac55
A
4316 dcp->c_flag &= ~C_DIR_MODIFICATION;
4317 wakeup((caddr_t)&dcp->c_flag);
4318
91447636
A
4319 hfs_unlock(dcp);
4320 }
4321 if (error == 0 && cp != NULL) {
4322 hfs_unlock(cp);
b4c24cb9 4323 }
91447636
A
4324 if (started_tr) {
4325 hfs_end_transaction(hfsmp);
4326 started_tr = 0;
b4c24cb9
A
4327 }
4328
9bccf70c
A
4329 return (error);
4330}
4331
4332
b7266188
A
4333
4334/* hfs_vgetrsrc acquires a resource fork vnode corresponding to the cnode that is
4335 * found in 'vp'. The rsrc fork vnode is returned with the cnode locked and iocount
4336 * on the rsrc vnode.
4337 *
4338 * *rvpp is an output argument for returning the pointer to the resource fork vnode.
4339 * In most cases, the resource fork vnode will not be set if we return an error.
4340 * However, if error_on_unlinked is set, we may have already acquired the resource fork vnode
4341 * before we discover the error (the file has gone open-unlinked). In this case only,
4342 * we may return a vnode in the output argument despite an error.
4343 *
4344 * If can_drop_lock is set, then it is safe for this function to temporarily drop
4345 * and then re-acquire the cnode lock. We may need to do this, for example, in order to
4346 * acquire an iocount or promote our lock.
4347 *
4348 * error_on_unlinked is an argument which indicates that we are to return an error if we
4349 * discover that the cnode has gone into an open-unlinked state ( C_DELETED or C_NOEXISTS)
4350 * is set in the cnode flags. This is only necessary if can_drop_lock is true, otherwise
4351 * there's really no reason to double-check for errors on the cnode.
91447636 4352 */
b7266188 4353
91447636
A
4354__private_extern__
4355int
b7266188
A
4356hfs_vgetrsrc(struct hfsmount *hfsmp, struct vnode *vp,
4357 struct vnode **rvpp, int can_drop_lock, int error_on_unlinked)
9bccf70c
A
4358{
4359 struct vnode *rvp;
cf7d32b8 4360 struct vnode *dvp = NULLVP;
9bccf70c
A
4361 struct cnode *cp = VTOC(vp);
4362 int error;
91447636 4363 int vid;
b7266188
A
4364 int delete_status = 0;
4365
4366
4367 /*
4368 * Need to check the status of the cnode to validate it hasn't
4369 * gone open-unlinked on us before we can actually do work with it.
4370 */
4371 delete_status = hfs_checkdeleted (cp);
4372 if ((delete_status) && (error_on_unlinked)) {
4373 return delete_status;
4374 }
9bccf70c 4375
4a3eedf9 4376restart:
2d21ac55 4377 /* Attempt to use exising vnode */
9bccf70c 4378 if ((rvp = cp->c_rsrc_vp)) {
91447636
A
4379 vid = vnode_vid(rvp);
4380
2d21ac55
A
4381 /*
4382 * It is not safe to hold the cnode lock when calling vnode_getwithvid()
4383 * for the alternate fork -- vnode_getwithvid() could deadlock waiting
4384 * for a VL_WANTTERM while another thread has an iocount on the alternate
4385 * fork vnode and is attempting to acquire the common cnode lock.
4386 *
4387 * But it's also not safe to drop the cnode lock when we're holding
4388 * multiple cnode locks, like during a hfs_removefile() operation
4389 * since we could lock out of order when re-acquiring the cnode lock.
4390 *
4391 * So we can only drop the lock here if its safe to drop it -- which is
4392 * most of the time with the exception being hfs_removefile().
4393 */
4394 if (can_drop_lock)
4395 hfs_unlock(cp);
4396
91447636 4397 error = vnode_getwithvid(rvp, vid);
2d21ac55 4398
4a3eedf9 4399 if (can_drop_lock) {
2d21ac55 4400 (void) hfs_lock(cp, HFS_FORCE_LOCK);
b7266188
A
4401
4402 /*
4403 * When we relinquished our cnode lock, the cnode could have raced
4404 * with a delete and gotten deleted. If the caller did not want
4405 * us to ignore open-unlinked files, then re-check the C_DELETED
4406 * state and see if we need to return an ENOENT here because the item
4407 * got deleted in the intervening time.
4408 */
4409 if (error_on_unlinked) {
4410 if ((delete_status = hfs_checkdeleted(cp))) {
4411 /*
4412 * If error == 0, this means that we succeeded in acquiring an iocount on the
4413 * rsrc fork vnode. However, if we're in this block of code, that
4414 * means that we noticed that the cnode has gone open-unlinked. In
4415 * this case, the caller requested that we not do any other work and
4416 * return an errno. The caller will be responsible for dropping the
4417 * iocount we just acquired because we can't do it until we've released
4418 * the cnode lock.
4419 */
4420 if (error == 0) {
4421 *rvpp = rvp;
4422 }
4423 return delete_status;
4424 }
4425 }
4426
4a3eedf9
A
4427 /*
4428 * When our lock was relinquished, the resource fork
4429 * could have been recycled. Check for this and try
4430 * again.
4431 */
4432 if (error == ENOENT)
4433 goto restart;
4434 }
9bccf70c 4435 if (error) {
2d21ac55 4436 const char * name = (const char *)VTOC(vp)->c_desc.cd_nameptr;
9bccf70c
A
4437
4438 if (name)
4a3eedf9
A
4439 printf("hfs_vgetrsrc: couldn't get resource"
4440 " fork for %s, err %d\n", name, error);
9bccf70c
A
4441 return (error);
4442 }
4443 } else {
4444 struct cat_fork rsrcfork;
91447636 4445 struct componentname cn;
b0d623f7
A
4446 struct cat_desc *descptr = NULL;
4447 struct cat_desc to_desc;
4448 char delname[32];
91447636 4449 int lockflags;
9bccf70c 4450
743b1565
A
4451 /*
4452 * Make sure cnode lock is exclusive, if not upgrade it.
4453 *
4454 * We assume that we were called from a read-only VNOP (getattr)
4455 * and that its safe to have the cnode lock dropped and reacquired.
4456 */
4457 if (cp->c_lockowner != current_thread()) {
b0d623f7 4458 if (!can_drop_lock) {
2d21ac55 4459 return (EINVAL);
b0d623f7 4460 }
743b1565 4461 /*
b7266188 4462 * If the upgrade fails we lose the lock and
743b1565
A
4463 * have to take the exclusive lock on our own.
4464 */
2d21ac55 4465 if (lck_rw_lock_shared_to_exclusive(&cp->c_rwlock) == FALSE)
743b1565
A
4466 lck_rw_lock_exclusive(&cp->c_rwlock);
4467 cp->c_lockowner = current_thread();
4468 }
4469
b0d623f7
A
4470 /*
4471 * hfs_vgetsrc may be invoked for a cnode that has already been marked
4472 * C_DELETED. This is because we need to continue to provide rsrc
4473 * fork access to open-unlinked files. In this case, build a fake descriptor
4474 * like in hfs_removefile. If we don't do this, buildkey will fail in
b7266188
A
4475 * cat_lookup because this cnode has no name in its descriptor. However,
4476 * only do this if the caller did not specify that they wanted us to
4477 * error out upon encountering open-unlinked files.
b0d623f7
A
4478 */
4479
b7266188
A
4480 if ((error_on_unlinked) && (can_drop_lock)) {
4481 if ((error = hfs_checkdeleted (cp))) {
4482 return error;
4483 }
4484 }
4485
b0d623f7
A
4486 if ((cp->c_flag & C_DELETED ) && (cp->c_desc.cd_namelen == 0)) {
4487 bzero (&to_desc, sizeof(to_desc));
4488 bzero (delname, 32);
4489 MAKE_DELETED_NAME(delname, sizeof(delname), cp->c_fileid);
4490 to_desc.cd_nameptr = (const u_int8_t*) delname;
4491 to_desc.cd_namelen = strlen(delname);
4492 to_desc.cd_parentcnid = hfsmp->hfs_private_desc[FILE_HARDLINKS].cd_cnid;
4493 to_desc.cd_flags = 0;
4494 to_desc.cd_cnid = cp->c_cnid;
4495
4496 descptr = &to_desc;
4497 }
4498 else {
4499 descptr = &cp->c_desc;
4500 }
4501
4502
91447636 4503 lockflags = hfs_systemfile_lock(hfsmp, SFL_CATALOG, HFS_SHARED_LOCK);
9bccf70c
A
4504
4505 /* Get resource fork data */
b0d623f7 4506 error = cat_lookup(hfsmp, descptr, 1, (struct cat_desc *)0,
91447636 4507 (struct cat_attr *)0, &rsrcfork, NULL);
9bccf70c 4508
91447636 4509 hfs_systemfile_unlock(hfsmp, lockflags);
b0d623f7 4510 if (error) {
9bccf70c 4511 return (error);
b0d623f7 4512 }
91447636
A
4513 /*
4514 * Supply hfs_getnewvnode with a component name.
4515 */
4516 cn.cn_pnbuf = NULL;
b0d623f7 4517 if (descptr->cd_nameptr) {
91447636
A
4518 MALLOC_ZONE(cn.cn_pnbuf, caddr_t, MAXPATHLEN, M_NAMEI, M_WAITOK);
4519 cn.cn_nameiop = LOOKUP;
4520 cn.cn_flags = ISLASTCN | HASBUF;
4521 cn.cn_context = NULL;
4522 cn.cn_pnlen = MAXPATHLEN;
4523 cn.cn_nameptr = cn.cn_pnbuf;
4524 cn.cn_hash = 0;
4525 cn.cn_consume = 0;
2d21ac55 4526 cn.cn_namelen = snprintf(cn.cn_nameptr, MAXPATHLEN,
b0d623f7 4527 "%s%s", descptr->cd_nameptr,
2d21ac55 4528 _PATH_RSRCFORKSPEC);
91447636 4529 }
cf7d32b8
A
4530 dvp = vnode_getparent(vp);
4531 error = hfs_getnewvnode(hfsmp, dvp, cn.cn_pnbuf ? &cn : NULL,
b0d623f7 4532 descptr, GNV_WANTRSRC | GNV_SKIPLOCK, &cp->c_attr,
2d21ac55 4533 &rsrcfork, &rvp);
cf7d32b8
A
4534 if (dvp)
4535 vnode_put(dvp);
91447636
A
4536 if (cn.cn_pnbuf)
4537 FREE_ZONE(cn.cn_pnbuf, cn.cn_pnlen, M_NAMEI);
9bccf70c
A
4538 if (error)
4539 return (error);
4540 }
4541
4542 *rvpp = rvp;
4543 return (0);
4544}
4545
9bccf70c
A
4546/*
4547 * Wrapper for special device reads
4548 */
4549static int
4550hfsspec_read(ap)
91447636 4551 struct vnop_read_args /* {
9bccf70c
A
4552 struct vnode *a_vp;
4553 struct uio *a_uio;
4554 int a_ioflag;
91447636 4555 vfs_context_t a_context;
9bccf70c
A
4556 } */ *ap;
4557{
4558 /*
4559 * Set access flag.
4560 */
91447636
A
4561 VTOC(ap->a_vp)->c_touch_acctime = TRUE;
4562 return (VOCALL (spec_vnodeop_p, VOFFSET(vnop_read), ap));
9bccf70c
A
4563}
4564
4565/*
4566 * Wrapper for special device writes
4567 */
4568static int
4569hfsspec_write(ap)
91447636 4570 struct vnop_write_args /* {
9bccf70c
A
4571 struct vnode *a_vp;
4572 struct uio *a_uio;
4573 int a_ioflag;
91447636 4574 vfs_context_t a_context;
9bccf70c
A
4575 } */ *ap;
4576{
4577 /*
4578 * Set update and change flags.
4579 */
91447636
A
4580 VTOC(ap->a_vp)->c_touch_chgtime = TRUE;
4581 VTOC(ap->a_vp)->c_touch_modtime = TRUE;
4582 return (VOCALL (spec_vnodeop_p, VOFFSET(vnop_write), ap));
9bccf70c
A
4583}
4584
4585/*
4586 * Wrapper for special device close
4587 *
4588 * Update the times on the cnode then do device close.
4589 */
4590static int
4591hfsspec_close(ap)
91447636 4592 struct vnop_close_args /* {
9bccf70c
A
4593 struct vnode *a_vp;
4594 int a_fflag;
91447636 4595 vfs_context_t a_context;
9bccf70c
A
4596 } */ *ap;
4597{
4598 struct vnode *vp = ap->a_vp;
91447636 4599 struct cnode *cp;
9bccf70c 4600
b0d623f7 4601 if (vnode_isinuse(ap->a_vp, 0)) {
91447636
A
4602 if (hfs_lock(VTOC(vp), HFS_EXCLUSIVE_LOCK) == 0) {
4603 cp = VTOC(vp);
4604 hfs_touchtimes(VTOHFS(vp), cp);
4605 hfs_unlock(cp);
4606 }
4607 }
4608 return (VOCALL (spec_vnodeop_p, VOFFSET(vnop_close), ap));
9bccf70c
A
4609}
4610
4611#if FIFO
4612/*
4613 * Wrapper for fifo reads
4614 */
4615static int
4616hfsfifo_read(ap)
91447636 4617 struct vnop_read_args /* {
9bccf70c
A
4618 struct vnode *a_vp;
4619 struct uio *a_uio;
4620 int a_ioflag;
91447636 4621 vfs_context_t a_context;
9bccf70c
A
4622 } */ *ap;
4623{
9bccf70c
A
4624 /*
4625 * Set access flag.
4626 */
91447636
A
4627 VTOC(ap->a_vp)->c_touch_acctime = TRUE;
4628 return (VOCALL (fifo_vnodeop_p, VOFFSET(vnop_read), ap));
9bccf70c
A
4629}
4630
4631/*
4632 * Wrapper for fifo writes
4633 */
4634static int
4635hfsfifo_write(ap)
91447636 4636 struct vnop_write_args /* {
9bccf70c
A
4637 struct vnode *a_vp;
4638 struct uio *a_uio;
4639 int a_ioflag;
91447636 4640 vfs_context_t a_context;
9bccf70c
A
4641 } */ *ap;
4642{
9bccf70c
A
4643 /*
4644 * Set update and change flags.
4645 */
91447636
A
4646 VTOC(ap->a_vp)->c_touch_chgtime = TRUE;
4647 VTOC(ap->a_vp)->c_touch_modtime = TRUE;
4648 return (VOCALL (fifo_vnodeop_p, VOFFSET(vnop_write), ap));
9bccf70c
A
4649}
4650
4651/*
4652 * Wrapper for fifo close
4653 *
4654 * Update the times on the cnode then do device close.
4655 */
4656static int
4657hfsfifo_close(ap)
91447636 4658 struct vnop_close_args /* {
9bccf70c
A
4659 struct vnode *a_vp;
4660 int a_fflag;
91447636 4661 vfs_context_t a_context;
9bccf70c
A
4662 } */ *ap;
4663{
9bccf70c 4664 struct vnode *vp = ap->a_vp;
91447636 4665 struct cnode *cp;
9bccf70c 4666
91447636
A
4667 if (vnode_isinuse(ap->a_vp, 1)) {
4668 if (hfs_lock(VTOC(vp), HFS_EXCLUSIVE_LOCK) == 0) {
4669 cp = VTOC(vp);
4670 hfs_touchtimes(VTOHFS(vp), cp);
4671 hfs_unlock(cp);
4672 }
4673 }
4674 return (VOCALL (fifo_vnodeop_p, VOFFSET(vnop_close), ap));
9bccf70c 4675}
55e303ae 4676
55e303ae 4677
9bccf70c
A
4678#endif /* FIFO */
4679
91447636
A
4680/*
4681 * Synchronize a file's in-core state with that on disk.
4682 */
4683static int
4684hfs_vnop_fsync(ap)
4685 struct vnop_fsync_args /* {
4686 struct vnode *a_vp;
4687 int a_waitfor;
4688 vfs_context_t a_context;
4689 } */ *ap;
4690{
4691 struct vnode* vp = ap->a_vp;
4692 int error;
4693
4694 /*
4695 * We need to allow ENOENT lock errors since unlink
4696 * systenm call can call VNOP_FSYNC during vclean.
4697 */
4698 error = hfs_lock(VTOC(vp), HFS_EXCLUSIVE_LOCK);
4699 if (error)
4700 return (0);
4701
4702 error = hfs_fsync(vp, ap->a_waitfor, 0, vfs_context_proc(ap->a_context));
4703
4704 hfs_unlock(VTOC(vp));
4705 return (error);
4706}
9bccf70c 4707
2d21ac55
A
4708
4709static int
4710hfs_vnop_whiteout(ap)
4711 struct vnop_whiteout_args /* {
4712 struct vnode *a_dvp;
4713 struct componentname *a_cnp;
4714 int a_flags;
4715 vfs_context_t a_context;
4716 } */ *ap;
4717{
4718 int error = 0;
4719 struct vnode *vp = NULL;
4720 struct vnode_attr va;
4721 struct vnop_lookup_args lookup_args;
4722 struct vnop_remove_args remove_args;
4723 struct hfsmount *hfsmp;
4724
4725 hfsmp = VTOHFS(ap->a_dvp);
4726 if (hfsmp->hfs_flags & HFS_STANDARD) {
4727 error = ENOTSUP;
4728 goto exit;
4729 }
4730
4731 switch (ap->a_flags) {
4732 case LOOKUP:
4733 error = 0;
4734 break;
4735
4736 case CREATE:
4737 VATTR_INIT(&va);
4738 VATTR_SET(&va, va_type, VREG);
4739 VATTR_SET(&va, va_mode, S_IFWHT);
4740 VATTR_SET(&va, va_uid, 0);
4741 VATTR_SET(&va, va_gid, 0);
4742
4743 error = hfs_makenode(ap->a_dvp, &vp, ap->a_cnp, &va, ap->a_context);
4744 /* No need to release the vnode as no vnode is created for whiteouts */
4745 break;
4746
4747 case DELETE:
4748 lookup_args.a_dvp = ap->a_dvp;
4749 lookup_args.a_vpp = &vp;
4750 lookup_args.a_cnp = ap->a_cnp;
4751 lookup_args.a_context = ap->a_context;
4752
4753 error = hfs_vnop_lookup(&lookup_args);
4754 if (error) {
4755 break;
4756 }
4757
4758 remove_args.a_dvp = ap->a_dvp;
4759 remove_args.a_vp = vp;
4760 remove_args.a_cnp = ap->a_cnp;
4761 remove_args.a_flags = 0;
4762 remove_args.a_context = ap->a_context;
4763
4764 error = hfs_vnop_remove(&remove_args);
4765 vnode_put(vp);
4766 break;
4767
4768 default:
4769 panic("hfs_vnop_whiteout: unknown operation (flag = %x)\n", ap->a_flags);
4770 };
4771
4772exit:
4773 return (error);
4774}
9bccf70c
A
4775
4776int (**hfs_vnodeop_p)(void *);
b0d623f7 4777int (**hfs_std_vnodeop_p) (void *);
9bccf70c
A
4778
4779#define VOPFUNC int (*)(void *)
4780
b0d623f7
A
4781static int hfs_readonly_op (__unused void* ap) { return (EROFS); }
4782
4783/*
4784 * In 10.6 and forward, HFS Standard is read-only and deprecated. The vnop table below
4785 * is for use with HFS standard to block out operations that would modify the file system
4786 */
4787
4788struct vnodeopv_entry_desc hfs_standard_vnodeop_entries[] = {
4789 { &vnop_default_desc, (VOPFUNC)vn_default_error },
4790 { &vnop_lookup_desc, (VOPFUNC)hfs_vnop_lookup }, /* lookup */
4791 { &vnop_create_desc, (VOPFUNC)hfs_readonly_op }, /* create (READONLY) */
4792 { &vnop_mknod_desc, (VOPFUNC)hfs_readonly_op }, /* mknod (READONLY) */
4793 { &vnop_open_desc, (VOPFUNC)hfs_vnop_open }, /* open */
4794 { &vnop_close_desc, (VOPFUNC)hfs_vnop_close }, /* close */
4795 { &vnop_getattr_desc, (VOPFUNC)hfs_vnop_getattr }, /* getattr */
4796 { &vnop_setattr_desc, (VOPFUNC)hfs_readonly_op }, /* setattr */
4797 { &vnop_read_desc, (VOPFUNC)hfs_vnop_read }, /* read */
4798 { &vnop_write_desc, (VOPFUNC)hfs_readonly_op }, /* write (READONLY) */
4799 { &vnop_ioctl_desc, (VOPFUNC)hfs_vnop_ioctl }, /* ioctl */
4800 { &vnop_select_desc, (VOPFUNC)hfs_vnop_select }, /* select */
4801 { &vnop_revoke_desc, (VOPFUNC)nop_revoke }, /* revoke */
4802 { &vnop_exchange_desc, (VOPFUNC)hfs_readonly_op }, /* exchange (READONLY)*/
4803 { &vnop_mmap_desc, (VOPFUNC)err_mmap }, /* mmap */
4804 { &vnop_fsync_desc, (VOPFUNC)hfs_readonly_op}, /* fsync (READONLY) */
4805 { &vnop_remove_desc, (VOPFUNC)hfs_readonly_op }, /* remove (READONLY) */
4806 { &vnop_link_desc, (VOPFUNC)hfs_readonly_op }, /* link ( READONLLY) */
4807 { &vnop_rename_desc, (VOPFUNC)hfs_readonly_op }, /* rename (READONLY)*/
4808 { &vnop_mkdir_desc, (VOPFUNC)hfs_readonly_op }, /* mkdir (READONLY) */
4809 { &vnop_rmdir_desc, (VOPFUNC)hfs_readonly_op }, /* rmdir (READONLY) */
4810 { &vnop_symlink_desc, (VOPFUNC)hfs_readonly_op }, /* symlink (READONLY) */
4811 { &vnop_readdir_desc, (VOPFUNC)hfs_vnop_readdir }, /* readdir */
4812 { &vnop_readdirattr_desc, (VOPFUNC)hfs_vnop_readdirattr }, /* readdirattr */
4813 { &vnop_readlink_desc, (VOPFUNC)hfs_vnop_readlink }, /* readlink */
4814 { &vnop_inactive_desc, (VOPFUNC)hfs_vnop_inactive }, /* inactive */
4815 { &vnop_reclaim_desc, (VOPFUNC)hfs_vnop_reclaim }, /* reclaim */
4816 { &vnop_strategy_desc, (VOPFUNC)hfs_vnop_strategy }, /* strategy */
4817 { &vnop_pathconf_desc, (VOPFUNC)hfs_vnop_pathconf }, /* pathconf */
4818 { &vnop_advlock_desc, (VOPFUNC)err_advlock }, /* advlock */
4819 { &vnop_allocate_desc, (VOPFUNC)hfs_readonly_op }, /* allocate (READONLY) */
4820 { &vnop_searchfs_desc, (VOPFUNC)hfs_vnop_search }, /* search fs */
4821 { &vnop_bwrite_desc, (VOPFUNC)hfs_readonly_op }, /* bwrite (READONLY) */
4822 { &vnop_pagein_desc, (VOPFUNC)hfs_vnop_pagein }, /* pagein */
4823 { &vnop_pageout_desc,(VOPFUNC) hfs_readonly_op }, /* pageout (READONLY) */
4824 { &vnop_copyfile_desc, (VOPFUNC)hfs_readonly_op }, /* copyfile (READONLY)*/
4825 { &vnop_blktooff_desc, (VOPFUNC)hfs_vnop_blktooff }, /* blktooff */
4826 { &vnop_offtoblk_desc, (VOPFUNC)hfs_vnop_offtoblk }, /* offtoblk */
4827 { &vnop_blockmap_desc, (VOPFUNC)hfs_vnop_blockmap }, /* blockmap */
4828 { &vnop_getxattr_desc, (VOPFUNC)hfs_vnop_getxattr},
4829 { &vnop_setxattr_desc, (VOPFUNC)hfs_readonly_op}, /* set xattr (READONLY) */
4830 { &vnop_removexattr_desc, (VOPFUNC)hfs_readonly_op}, /* remove xattr (READONLY) */
4831 { &vnop_listxattr_desc, (VOPFUNC)hfs_vnop_listxattr},
4832 { &vnop_whiteout_desc, (VOPFUNC)hfs_readonly_op}, /* whiteout (READONLY) */
4833#if NAMEDSTREAMS
4834 { &vnop_getnamedstream_desc, (VOPFUNC)hfs_vnop_getnamedstream },
4835 { &vnop_makenamedstream_desc, (VOPFUNC)hfs_readonly_op },
4836 { &vnop_removenamedstream_desc, (VOPFUNC)hfs_readonly_op },
4837#endif
4838 { NULL, (VOPFUNC)NULL }
4839};
4840
4841struct vnodeopv_desc hfs_std_vnodeop_opv_desc =
4842{ &hfs_std_vnodeop_p, hfs_standard_vnodeop_entries };
4843
4844
4845/* VNOP table for HFS+ */
9bccf70c 4846struct vnodeopv_entry_desc hfs_vnodeop_entries[] = {
91447636
A
4847 { &vnop_default_desc, (VOPFUNC)vn_default_error },
4848 { &vnop_lookup_desc, (VOPFUNC)hfs_vnop_lookup }, /* lookup */
4849 { &vnop_create_desc, (VOPFUNC)hfs_vnop_create }, /* create */
4850 { &vnop_mknod_desc, (VOPFUNC)hfs_vnop_mknod }, /* mknod */
4851 { &vnop_open_desc, (VOPFUNC)hfs_vnop_open }, /* open */
4852 { &vnop_close_desc, (VOPFUNC)hfs_vnop_close }, /* close */
4853 { &vnop_getattr_desc, (VOPFUNC)hfs_vnop_getattr }, /* getattr */
4854 { &vnop_setattr_desc, (VOPFUNC)hfs_vnop_setattr }, /* setattr */
4855 { &vnop_read_desc, (VOPFUNC)hfs_vnop_read }, /* read */
4856 { &vnop_write_desc, (VOPFUNC)hfs_vnop_write }, /* write */
4857 { &vnop_ioctl_desc, (VOPFUNC)hfs_vnop_ioctl }, /* ioctl */
4858 { &vnop_select_desc, (VOPFUNC)hfs_vnop_select }, /* select */
4859 { &vnop_revoke_desc, (VOPFUNC)nop_revoke }, /* revoke */
4860 { &vnop_exchange_desc, (VOPFUNC)hfs_vnop_exchange }, /* exchange */
4861 { &vnop_mmap_desc, (VOPFUNC)err_mmap }, /* mmap */
4862 { &vnop_fsync_desc, (VOPFUNC)hfs_vnop_fsync }, /* fsync */
4863 { &vnop_remove_desc, (VOPFUNC)hfs_vnop_remove }, /* remove */
4864 { &vnop_link_desc, (VOPFUNC)hfs_vnop_link }, /* link */
4865 { &vnop_rename_desc, (VOPFUNC)hfs_vnop_rename }, /* rename */
4866 { &vnop_mkdir_desc, (VOPFUNC)hfs_vnop_mkdir }, /* mkdir */
4867 { &vnop_rmdir_desc, (VOPFUNC)hfs_vnop_rmdir }, /* rmdir */
4868 { &vnop_symlink_desc, (VOPFUNC)hfs_vnop_symlink }, /* symlink */
4869 { &vnop_readdir_desc, (VOPFUNC)hfs_vnop_readdir }, /* readdir */
4870 { &vnop_readdirattr_desc, (VOPFUNC)hfs_vnop_readdirattr }, /* readdirattr */
4871 { &vnop_readlink_desc, (VOPFUNC)hfs_vnop_readlink }, /* readlink */
4872 { &vnop_inactive_desc, (VOPFUNC)hfs_vnop_inactive }, /* inactive */
4873 { &vnop_reclaim_desc, (VOPFUNC)hfs_vnop_reclaim }, /* reclaim */
4874 { &vnop_strategy_desc, (VOPFUNC)hfs_vnop_strategy }, /* strategy */
4875 { &vnop_pathconf_desc, (VOPFUNC)hfs_vnop_pathconf }, /* pathconf */
4876 { &vnop_advlock_desc, (VOPFUNC)err_advlock }, /* advlock */
4877 { &vnop_allocate_desc, (VOPFUNC)hfs_vnop_allocate }, /* allocate */
4878 { &vnop_searchfs_desc, (VOPFUNC)hfs_vnop_search }, /* search fs */
4879 { &vnop_bwrite_desc, (VOPFUNC)hfs_vnop_bwrite }, /* bwrite */
4880 { &vnop_pagein_desc, (VOPFUNC)hfs_vnop_pagein }, /* pagein */
4881 { &vnop_pageout_desc,(VOPFUNC) hfs_vnop_pageout }, /* pageout */
4882 { &vnop_copyfile_desc, (VOPFUNC)err_copyfile }, /* copyfile */
4883 { &vnop_blktooff_desc, (VOPFUNC)hfs_vnop_blktooff }, /* blktooff */
4884 { &vnop_offtoblk_desc, (VOPFUNC)hfs_vnop_offtoblk }, /* offtoblk */
4885 { &vnop_blockmap_desc, (VOPFUNC)hfs_vnop_blockmap }, /* blockmap */
91447636
A
4886 { &vnop_getxattr_desc, (VOPFUNC)hfs_vnop_getxattr},
4887 { &vnop_setxattr_desc, (VOPFUNC)hfs_vnop_setxattr},
4888 { &vnop_removexattr_desc, (VOPFUNC)hfs_vnop_removexattr},
4889 { &vnop_listxattr_desc, (VOPFUNC)hfs_vnop_listxattr},
2d21ac55
A
4890 { &vnop_whiteout_desc, (VOPFUNC)hfs_vnop_whiteout},
4891#if NAMEDSTREAMS
4892 { &vnop_getnamedstream_desc, (VOPFUNC)hfs_vnop_getnamedstream },
4893 { &vnop_makenamedstream_desc, (VOPFUNC)hfs_vnop_makenamedstream },
4894 { &vnop_removenamedstream_desc, (VOPFUNC)hfs_vnop_removenamedstream },
4895#endif
9bccf70c
A
4896 { NULL, (VOPFUNC)NULL }
4897};
4898
4899struct vnodeopv_desc hfs_vnodeop_opv_desc =
4900{ &hfs_vnodeop_p, hfs_vnodeop_entries };
4901
b0d623f7
A
4902
4903/* Spec Op vnop table for HFS+ */
9bccf70c
A
4904int (**hfs_specop_p)(void *);
4905struct vnodeopv_entry_desc hfs_specop_entries[] = {
91447636
A
4906 { &vnop_default_desc, (VOPFUNC)vn_default_error },
4907 { &vnop_lookup_desc, (VOPFUNC)spec_lookup }, /* lookup */
4908 { &vnop_create_desc, (VOPFUNC)spec_create }, /* create */
4909 { &vnop_mknod_desc, (VOPFUNC)spec_mknod }, /* mknod */
4910 { &vnop_open_desc, (VOPFUNC)spec_open }, /* open */
4911 { &vnop_close_desc, (VOPFUNC)hfsspec_close }, /* close */
4912 { &vnop_getattr_desc, (VOPFUNC)hfs_vnop_getattr }, /* getattr */
4913 { &vnop_setattr_desc, (VOPFUNC)hfs_vnop_setattr }, /* setattr */
4914 { &vnop_read_desc, (VOPFUNC)hfsspec_read }, /* read */
4915 { &vnop_write_desc, (VOPFUNC)hfsspec_write }, /* write */
4916 { &vnop_ioctl_desc, (VOPFUNC)spec_ioctl }, /* ioctl */
4917 { &vnop_select_desc, (VOPFUNC)spec_select }, /* select */
4918 { &vnop_revoke_desc, (VOPFUNC)spec_revoke }, /* revoke */
4919 { &vnop_mmap_desc, (VOPFUNC)spec_mmap }, /* mmap */
4920 { &vnop_fsync_desc, (VOPFUNC)hfs_vnop_fsync }, /* fsync */
4921 { &vnop_remove_desc, (VOPFUNC)spec_remove }, /* remove */
4922 { &vnop_link_desc, (VOPFUNC)spec_link }, /* link */
4923 { &vnop_rename_desc, (VOPFUNC)spec_rename }, /* rename */
4924 { &vnop_mkdir_desc, (VOPFUNC)spec_mkdir }, /* mkdir */
4925 { &vnop_rmdir_desc, (VOPFUNC)spec_rmdir }, /* rmdir */
4926 { &vnop_symlink_desc, (VOPFUNC)spec_symlink }, /* symlink */
4927 { &vnop_readdir_desc, (VOPFUNC)spec_readdir }, /* readdir */
4928 { &vnop_readlink_desc, (VOPFUNC)spec_readlink }, /* readlink */
4929 { &vnop_inactive_desc, (VOPFUNC)hfs_vnop_inactive }, /* inactive */
4930 { &vnop_reclaim_desc, (VOPFUNC)hfs_vnop_reclaim }, /* reclaim */
4931 { &vnop_strategy_desc, (VOPFUNC)spec_strategy }, /* strategy */
4932 { &vnop_pathconf_desc, (VOPFUNC)spec_pathconf }, /* pathconf */
4933 { &vnop_advlock_desc, (VOPFUNC)err_advlock }, /* advlock */
4934 { &vnop_bwrite_desc, (VOPFUNC)hfs_vnop_bwrite },
91447636
A
4935 { &vnop_pagein_desc, (VOPFUNC)hfs_vnop_pagein }, /* Pagein */
4936 { &vnop_pageout_desc, (VOPFUNC)hfs_vnop_pageout }, /* Pageout */
b0d623f7 4937 { &vnop_copyfile_desc, (VOPFUNC)err_copyfile }, /* copyfile */
91447636
A
4938 { &vnop_blktooff_desc, (VOPFUNC)hfs_vnop_blktooff }, /* blktooff */
4939 { &vnop_offtoblk_desc, (VOPFUNC)hfs_vnop_offtoblk }, /* offtoblk */
9bccf70c
A
4940 { (struct vnodeop_desc*)NULL, (VOPFUNC)NULL }
4941};
4942struct vnodeopv_desc hfs_specop_opv_desc =
4943 { &hfs_specop_p, hfs_specop_entries };
4944
4945#if FIFO
b0d623f7 4946/* HFS+ FIFO VNOP table */
9bccf70c
A
4947int (**hfs_fifoop_p)(void *);
4948struct vnodeopv_entry_desc hfs_fifoop_entries[] = {
91447636
A
4949 { &vnop_default_desc, (VOPFUNC)vn_default_error },
4950 { &vnop_lookup_desc, (VOPFUNC)fifo_lookup }, /* lookup */
4951 { &vnop_create_desc, (VOPFUNC)fifo_create }, /* create */
4952 { &vnop_mknod_desc, (VOPFUNC)fifo_mknod }, /* mknod */
4953 { &vnop_open_desc, (VOPFUNC)fifo_open }, /* open */
4954 { &vnop_close_desc, (VOPFUNC)hfsfifo_close }, /* close */
4955 { &vnop_getattr_desc, (VOPFUNC)hfs_vnop_getattr }, /* getattr */
4956 { &vnop_setattr_desc, (VOPFUNC)hfs_vnop_setattr }, /* setattr */
4957 { &vnop_read_desc, (VOPFUNC)hfsfifo_read }, /* read */
4958 { &vnop_write_desc, (VOPFUNC)hfsfifo_write }, /* write */
4959 { &vnop_ioctl_desc, (VOPFUNC)fifo_ioctl }, /* ioctl */
4960 { &vnop_select_desc, (VOPFUNC)fifo_select }, /* select */
4961 { &vnop_revoke_desc, (VOPFUNC)fifo_revoke }, /* revoke */
4962 { &vnop_mmap_desc, (VOPFUNC)fifo_mmap }, /* mmap */
4963 { &vnop_fsync_desc, (VOPFUNC)hfs_vnop_fsync }, /* fsync */
4964 { &vnop_remove_desc, (VOPFUNC)fifo_remove }, /* remove */
4965 { &vnop_link_desc, (VOPFUNC)fifo_link }, /* link */
4966 { &vnop_rename_desc, (VOPFUNC)fifo_rename }, /* rename */
4967 { &vnop_mkdir_desc, (VOPFUNC)fifo_mkdir }, /* mkdir */
4968 { &vnop_rmdir_desc, (VOPFUNC)fifo_rmdir }, /* rmdir */
4969 { &vnop_symlink_desc, (VOPFUNC)fifo_symlink }, /* symlink */
4970 { &vnop_readdir_desc, (VOPFUNC)fifo_readdir }, /* readdir */
4971 { &vnop_readlink_desc, (VOPFUNC)fifo_readlink }, /* readlink */
4972 { &vnop_inactive_desc, (VOPFUNC)hfs_vnop_inactive }, /* inactive */
4973 { &vnop_reclaim_desc, (VOPFUNC)hfs_vnop_reclaim }, /* reclaim */
4974 { &vnop_strategy_desc, (VOPFUNC)fifo_strategy }, /* strategy */
4975 { &vnop_pathconf_desc, (VOPFUNC)fifo_pathconf }, /* pathconf */
4976 { &vnop_advlock_desc, (VOPFUNC)err_advlock }, /* advlock */
4977 { &vnop_bwrite_desc, (VOPFUNC)hfs_vnop_bwrite },
4978 { &vnop_pagein_desc, (VOPFUNC)hfs_vnop_pagein }, /* Pagein */
4979 { &vnop_pageout_desc, (VOPFUNC)hfs_vnop_pageout }, /* Pageout */
4980 { &vnop_copyfile_desc, (VOPFUNC)err_copyfile }, /* copyfile */
4981 { &vnop_blktooff_desc, (VOPFUNC)hfs_vnop_blktooff }, /* blktooff */
4982 { &vnop_offtoblk_desc, (VOPFUNC)hfs_vnop_offtoblk }, /* offtoblk */
4983 { &vnop_blockmap_desc, (VOPFUNC)hfs_vnop_blockmap }, /* blockmap */
9bccf70c
A
4984 { (struct vnodeop_desc*)NULL, (VOPFUNC)NULL }
4985};
4986struct vnodeopv_desc hfs_fifoop_opv_desc =
4987 { &hfs_fifoop_p, hfs_fifoop_entries };
4988#endif /* FIFO */
4989
4990
4991