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