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