]> git.saurik.com Git - apple/xnu.git/blob - bsd/hfs/hfs_vnops.c
xnu-792.tar.gz
[apple/xnu.git] / bsd / hfs / hfs_vnops.c
1 /*
2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22
23 #include <sys/systm.h>
24 #include <sys/kernel.h>
25 #include <sys/file_internal.h>
26 #include <sys/dirent.h>
27 #include <sys/stat.h>
28 #include <sys/buf.h>
29 #include <sys/mount.h>
30 #include <sys/vnode_internal.h>
31 #include <sys/malloc.h>
32 #include <sys/ubc.h>
33 #include <sys/paths.h>
34 #include <sys/quota.h>
35 #include <sys/time.h>
36 #include <sys/disk.h>
37 #include <sys/kauth.h>
38 #include <sys/uio_internal.h>
39
40 #include <miscfs/specfs/specdev.h>
41 #include <miscfs/fifofs/fifo.h>
42 #include <vfs/vfs_support.h>
43 #include <machine/spl.h>
44
45 #include <sys/kdebug.h>
46
47 #include "hfs.h"
48 #include "hfs_catalog.h"
49 #include "hfs_cnode.h"
50 #include "hfs_dbg.h"
51 #include "hfs_mount.h"
52 #include "hfs_quota.h"
53 #include "hfs_endian.h"
54
55 #include "hfscommon/headers/BTreesInternal.h"
56 #include "hfscommon/headers/FileMgrInternal.h"
57
58 #define MAKE_DELETED_NAME(NAME,FID) \
59 (void) sprintf((NAME), "%s%d", HFS_DELETE_PREFIX, (FID))
60
61 #define KNDETACH_VNLOCKED 0x00000001
62
63 #define CARBON_TEMP_DIR_NAME "Cleanup At Startup"
64
65
66 /* Global vfs data structures for hfs */
67
68
69 extern unsigned long strtoul(const char *, char **, int);
70
71 static int hfs_makenode(struct vnode *dvp, struct vnode **vpp,
72 struct componentname *cnp, struct vnode_attr *vap,
73 vfs_context_t ctx);
74
75 static int hfs_metasync(struct hfsmount *hfsmp, daddr64_t node, struct proc *p);
76
77 static int hfs_removedir(struct vnode *, struct vnode *, struct componentname *,
78 int);
79
80 static int hfs_removefile(struct vnode *, struct vnode *, struct componentname *,
81 int, int);
82
83 static int hfs_vnop_close(struct vnop_close_args*);
84 static int hfs_vnop_create(struct vnop_create_args*);
85 static int hfs_vnop_exchange(struct vnop_exchange_args*);
86 static int hfs_vnop_fsync(struct vnop_fsync_args*);
87 static int hfs_vnop_mkdir(struct vnop_mkdir_args*);
88 static int hfs_vnop_mknod(struct vnop_mknod_args*);
89 static int hfs_vnop_getattr(struct vnop_getattr_args*);
90 static int hfs_vnop_open(struct vnop_open_args*);
91 static int hfs_vnop_readdir(struct vnop_readdir_args*);
92 static int hfs_vnop_remove(struct vnop_remove_args*);
93 static int hfs_vnop_rename(struct vnop_rename_args*);
94 static int hfs_vnop_rmdir(struct vnop_rmdir_args*);
95 static int hfs_vnop_symlink(struct vnop_symlink_args*);
96 static int hfs_vnop_setattr(struct vnop_setattr_args*);
97
98 /* Options for hfs_removedir and hfs_removefile */
99 #define HFSRM_SKIP_RESERVE 0x01
100
101
102 int hfs_write_access(struct vnode *vp, kauth_cred_t cred, struct proc *p, Boolean considerFlags);
103
104 int hfs_chflags(struct vnode *vp, uint32_t flags, kauth_cred_t cred,
105 struct proc *p);
106 int hfs_chmod(struct vnode *vp, int mode, kauth_cred_t cred,
107 struct proc *p);
108 int hfs_chown(struct vnode *vp, uid_t uid, gid_t gid,
109 kauth_cred_t cred, struct proc *p);
110
111 /*****************************************************************************
112 *
113 * Common Operations on vnodes
114 *
115 *****************************************************************************/
116
117 /*
118 * Create a regular file.
119 */
120 static int
121 hfs_vnop_create(struct vnop_create_args *ap)
122 {
123 return hfs_makenode(ap->a_dvp, ap->a_vpp, ap->a_cnp, ap->a_vap, ap->a_context);
124 }
125
126 /*
127 * Make device special file.
128 */
129 static int
130 hfs_vnop_mknod(struct vnop_mknod_args *ap)
131 {
132 struct vnode_attr *vap = ap->a_vap;
133 struct vnode *dvp = ap->a_dvp;
134 struct vnode **vpp = ap->a_vpp;
135 struct cnode *cp;
136 int error;
137
138 if (VTOVCB(dvp)->vcbSigWord != kHFSPlusSigWord) {
139 return (ENOTSUP);
140 }
141
142 /* Create the vnode */
143 error = hfs_makenode(dvp, vpp, ap->a_cnp, vap, ap->a_context);
144 if (error)
145 return (error);
146
147 cp = VTOC(*vpp);
148 cp->c_touch_acctime = TRUE;
149 cp->c_touch_chgtime = TRUE;
150 cp->c_touch_modtime = TRUE;
151
152 if ((vap->va_rdev != VNOVAL) &&
153 (vap->va_type == VBLK || vap->va_type == VCHR))
154 cp->c_rdev = vap->va_rdev;
155
156 return (0);
157 }
158
159 /*
160 * Open a file/directory.
161 */
162 static int
163 hfs_vnop_open(struct vnop_open_args *ap)
164 {
165 struct vnode *vp = ap->a_vp;
166 struct filefork *fp;
167 struct timeval tv;
168 int error;
169
170 /*
171 * Files marked append-only must be opened for appending.
172 */
173 if ((VTOC(vp)->c_flags & APPEND) && !vnode_isdir(vp) &&
174 (ap->a_mode & (FWRITE | O_APPEND)) == FWRITE)
175 return (EPERM);
176
177 if (vnode_isreg(vp) && !UBCINFOEXISTS(vp))
178 return (EBUSY); /* file is in use by the kernel */
179
180 /* Don't allow journal file to be opened externally. */
181 if (VTOC(vp)->c_fileid == VTOHFS(vp)->hfs_jnlfileid)
182 return (EPERM);
183 /*
184 * On the first (non-busy) open of a fragmented
185 * file attempt to de-frag it (if its less than 20MB).
186 */
187 if ((VTOHFS(vp)->hfs_flags & HFS_READ_ONLY) ||
188 (VTOHFS(vp)->jnl == NULL) ||
189 !vnode_isreg(vp) || vnode_isinuse(vp, 0)) {
190 return (0);
191 }
192
193 if ((error = hfs_lock(VTOC(vp), HFS_EXCLUSIVE_LOCK)))
194 return (error);
195 fp = VTOF(vp);
196 if (fp->ff_blocks &&
197 fp->ff_extents[7].blockCount != 0 &&
198 fp->ff_size <= (20 * 1024 * 1024)) {
199 /*
200 * Wait until system bootup is done (3 min).
201 */
202 microuptime(&tv);
203 if (tv.tv_sec > (60 * 3)) {
204 (void) hfs_relocate(vp, VTOVCB(vp)->nextAllocation + 4096,
205 vfs_context_ucred(ap->a_context),
206 vfs_context_proc(ap->a_context));
207 }
208 }
209 hfs_unlock(VTOC(vp));
210
211 return (0);
212 }
213
214
215 /*
216 * Close a file/directory.
217 */
218 static int
219 hfs_vnop_close(ap)
220 struct vnop_close_args /* {
221 struct vnode *a_vp;
222 int a_fflag;
223 vfs_context_t a_context;
224 } */ *ap;
225 {
226 register struct vnode *vp = ap->a_vp;
227 register struct cnode *cp;
228 struct proc *p = vfs_context_proc(ap->a_context);
229 struct hfsmount *hfsmp;
230 int busy;
231
232 if ( hfs_lock(VTOC(vp), HFS_EXCLUSIVE_LOCK) != 0)
233 return (0);
234 cp = VTOC(vp);
235 hfsmp = VTOHFS(vp);
236
237 // if we froze the fs and we're exiting, then "thaw" the fs
238 if (hfsmp->hfs_freezing_proc == p && proc_exiting(p)) {
239 hfsmp->hfs_freezing_proc = NULL;
240 hfs_global_exclusive_lock_release(hfsmp);
241 }
242
243 busy = vnode_isinuse(vp, 1);
244
245 if (busy) {
246 hfs_touchtimes(VTOHFS(vp), cp);
247 }
248 if (vnode_isdir(vp)) {
249 hfs_reldirhints(cp, busy);
250 } else if (vnode_issystem(vp) && !busy) {
251 vnode_recycle(vp);
252 }
253
254 hfs_unlock(cp);
255 return (0);
256 }
257
258 /*
259 * Get basic attributes.
260 */
261 static int
262 hfs_vnop_getattr(struct vnop_getattr_args *ap)
263 {
264 struct vnode *vp = ap->a_vp;
265 struct vnode_attr *vap = ap->a_vap;
266 struct vnode *rvp = NULL;
267 struct hfsmount *hfsmp;
268 struct cnode *cp;
269 enum vtype v_type;
270 int error = 0;
271
272 if ((error = hfs_lock(VTOC(vp), HFS_SHARED_LOCK))) {
273 return (error);
274 }
275 cp = VTOC(vp);
276 hfsmp = VTOHFS(vp);
277 hfs_touchtimes(hfsmp, cp);
278 v_type = vnode_vtype(vp);
279
280 VATTR_RETURN(vap, va_rdev, (v_type == VBLK || v_type == VCHR) ? cp->c_rdev : 0);
281 if (v_type == VDIR) {
282 if (VATTR_IS_ACTIVE(vap, va_nlink)) {
283 int entries;
284
285 entries = cp->c_nlink;
286 if (vnode_isvroot(vp)) {
287 if (hfsmp->hfs_privdir_desc.cd_cnid != 0)
288 --entries; /* hide private dir */
289 if (hfsmp->jnl)
290 entries -= 2; /* hide the journal files */
291 }
292 VATTR_RETURN(vap, va_nlink, (uint64_t)entries);
293 }
294
295 if (VATTR_IS_ACTIVE(vap, va_nchildren)) {
296 int entries;
297
298 entries = cp->c_entries;
299 if (vnode_isvroot(vp)) {
300 if (hfsmp->hfs_privdir_desc.cd_cnid != 0)
301 --entries; /* hide private dir */
302 if (hfsmp->jnl)
303 entries -= 2; /* hide the journal files */
304 }
305 VATTR_RETURN(vap, va_nchildren, entries);
306 }
307 } else {
308 VATTR_RETURN(vap, va_nlink, (uint64_t)cp->c_nlink);
309 }
310
311 /* conditional because 64-bit arithmetic can be expensive */
312 if (VATTR_IS_ACTIVE(vap, va_total_size)) {
313 if (v_type == VDIR) {
314 VATTR_RETURN(vap, va_total_size, cp->c_nlink * AVERAGE_HFSDIRENTRY_SIZE);
315 } else {
316 uint64_t total_size = 0;
317 struct cnode *rcp;
318
319 if (cp->c_datafork) {
320 total_size = cp->c_datafork->ff_size;
321 }
322
323 if (cp->c_blocks - VTOF(vp)->ff_blocks) {
324 /* hfs_vgetrsrc does not use struct proc - therefore passing NULL */
325 error = hfs_vgetrsrc(hfsmp, vp, &rvp, NULL);
326 if (error) {
327 goto out;
328 }
329
330 rcp = VTOC(rvp);
331 if (rcp && rcp->c_rsrcfork) {
332 total_size += rcp->c_rsrcfork->ff_size;
333 }
334 }
335
336 VATTR_RETURN(vap, va_total_size, total_size);
337 /* Include size of attibute data (extents), if any */
338 if (cp->c_attrblks) {
339 vap->va_total_size += (uint64_t)cp->c_attrblks * (uint64_t)hfsmp->blockSize;
340 }
341 }
342 }
343 if (VATTR_IS_ACTIVE(vap, va_total_alloc)) {
344 if (v_type == VDIR) {
345 VATTR_RETURN(vap, va_total_alloc, 0);
346 } else {
347 VATTR_RETURN(vap, va_total_alloc, (uint64_t)cp->c_blocks * (uint64_t)hfsmp->blockSize);
348 /* Include size of attibute data (extents), if any */
349 if (cp->c_attrblks) {
350 vap->va_total_alloc += (uint64_t)cp->c_attrblks * (uint64_t)hfsmp->blockSize;
351 }
352 }
353 }
354 /* XXX broken... if ask for "data size" of rsrc fork vp you get rsrc fork size! */
355 if (v_type == VDIR) {
356 VATTR_RETURN(vap, va_data_size, cp->c_nlink * AVERAGE_HFSDIRENTRY_SIZE);
357 } else {
358 VATTR_RETURN(vap, va_data_size, VTOF(vp)->ff_size);
359 }
360 if (VATTR_IS_ACTIVE(vap, va_data_alloc) && (v_type != VDIR)) {
361 /* XXX do we need to account for ff_unallocblocks ? */
362 VATTR_RETURN(vap, va_data_alloc, (uint64_t)VTOF(vp)->ff_blocks * (uint64_t)hfsmp->blockSize);
363 }
364 /* XXX is this really a good 'optimal I/O size'? */
365 VATTR_RETURN(vap, va_iosize, hfsmp->hfs_logBlockSize);
366 VATTR_RETURN(vap, va_uid, cp->c_uid);
367 VATTR_RETURN(vap, va_gid, cp->c_gid);
368 VATTR_RETURN(vap, va_mode, cp->c_mode);
369 #if 0
370 /* XXX is S_IFXATTR still needed ??? */
371 if (VNODE_IS_RSRC(vp))
372 vap->va_mode |= S_IFXATTR;
373 #endif
374 VATTR_RETURN(vap, va_flags, cp->c_flags);
375
376 /*
377 * If the VFS wants extended security data, and we know that we
378 * don't have any (because it never told us it was setting any)
379 * then we can return the supported bit and no data. If we do
380 * have extended security, we can just leave the bit alone and
381 * the VFS will use the fallback path to fetch it.
382 */
383 if (VATTR_IS_ACTIVE(vap, va_acl)) {
384 if ((cp->c_attr.ca_recflags & kHFSHasSecurityMask) == 0) {
385 vap->va_acl = KAUTH_FILESEC_NONE;
386 VATTR_SET_SUPPORTED(vap, va_acl);
387 }
388 }
389 vap->va_create_time.tv_sec = cp->c_itime;
390 vap->va_create_time.tv_nsec = 0;
391 VATTR_SET_SUPPORTED(vap, va_create_time);
392
393 if (VATTR_IS_ACTIVE(vap, va_access_time)) {
394 /* Access times are lazyily updated, get current time if needed */
395 if (cp->c_touch_acctime) {
396 struct timeval tv;
397
398 microtime(&tv);
399 vap->va_access_time.tv_sec = tv.tv_sec;
400 } else {
401 vap->va_access_time.tv_sec = cp->c_atime;
402 }
403 vap->va_access_time.tv_nsec = 0;
404 VATTR_SET_SUPPORTED(vap, va_access_time);
405 }
406 vap->va_modify_time.tv_sec = cp->c_mtime;
407 vap->va_modify_time.tv_nsec = 0;
408 VATTR_SET_SUPPORTED(vap, va_modify_time);
409 vap->va_change_time.tv_sec = cp->c_ctime;
410 vap->va_change_time.tv_nsec = 0;
411 VATTR_SET_SUPPORTED(vap, va_change_time);
412 vap->va_backup_time.tv_sec = cp->c_btime;
413 vap->va_backup_time.tv_nsec = 0;
414 VATTR_SET_SUPPORTED(vap, va_backup_time);
415
416 /*
417 * Exporting file IDs from HFS Plus:
418 *
419 * For "normal" files the c_fileid is the same value as the
420 * c_cnid. But for hard link files, they are different - the
421 * c_cnid belongs to the active directory entry (ie the link)
422 * and the c_fileid is for the actual inode (ie the data file).
423 *
424 * The stat call (getattr) uses va_fileid and the Carbon APIs,
425 * which are hardlink-ignorant, will ask for va_linkid.
426 */
427 VATTR_RETURN(vap, va_fileid, (uint64_t)cp->c_fileid);
428 VATTR_RETURN(vap, va_linkid, (uint64_t)cp->c_cnid);
429 VATTR_RETURN(vap, va_parentid, (uint64_t)cp->c_parentcnid);
430 VATTR_RETURN(vap, va_fsid, cp->c_dev);
431 VATTR_RETURN(vap, va_filerev, 0);
432
433 VATTR_RETURN(vap, va_encoding, cp->c_encoding);
434
435 /* if this is the root, let VFS to find out the mount name, which may be different from the real name */
436 if (VATTR_IS_ACTIVE(vap, va_name) && !vnode_isvroot(vp)) {
437 /* Return the name for ATTR_CMN_NAME */
438 if (cp->c_desc.cd_namelen == 0) {
439 error = ENOENT;
440 goto out;
441 }
442
443 strncpy(vap->va_name, cp->c_desc.cd_nameptr, MAXPATHLEN);
444 vap->va_name[MAXPATHLEN-1] = '\0';
445 VATTR_SET_SUPPORTED(vap, va_name);
446 }
447
448 out:
449 hfs_unlock(cp);
450 if (rvp) {
451 vnode_put(rvp);
452 }
453 return (error);
454 }
455
456 static int
457 hfs_vnop_setattr(ap)
458 struct vnop_setattr_args /* {
459 struct vnode *a_vp;
460 struct vnode_attr *a_vap;
461 vfs_context_t a_context;
462 } */ *ap;
463 {
464 struct vnode_attr *vap = ap->a_vap;
465 struct vnode *vp = ap->a_vp;
466 struct cnode *cp = NULL;
467 struct hfsmount *hfsmp;
468 kauth_cred_t cred = vfs_context_ucred(ap->a_context);
469 struct proc *p = vfs_context_proc(ap->a_context);
470 int error = 0;
471 uid_t nuid;
472 gid_t ngid;
473
474 hfsmp = VTOHFS(vp);
475
476 /* Don't allow modification of the journal file. */
477 if (hfsmp->hfs_jnlfileid == VTOC(vp)->c_fileid) {
478 return (EPERM);
479 }
480
481 /*
482 * File size change request.
483 * We are guaranteed that this is not a directory, and that
484 * the filesystem object is writeable.
485 */
486 VATTR_SET_SUPPORTED(vap, va_data_size);
487 if (VATTR_IS_ACTIVE(vap, va_data_size) && !vnode_islnk(vp)) {
488
489 /* Take truncate lock before taking cnode lock. */
490 hfs_lock_truncate(VTOC(vp), TRUE);
491 if ((error = hfs_lock(VTOC(vp), HFS_EXCLUSIVE_LOCK))) {
492 hfs_unlock_truncate(VTOC(vp));
493 return (error);
494 }
495 cp = VTOC(vp);
496
497 error = hfs_truncate(vp, vap->va_data_size, vap->va_vaflags & 0xffff, 0, ap->a_context);
498
499 hfs_unlock_truncate(cp);
500 if (error)
501 goto out;
502 }
503 if (cp == NULL) {
504 if ((error = hfs_lock(VTOC(vp), HFS_EXCLUSIVE_LOCK)))
505 return (error);
506 cp = VTOC(vp);
507 }
508
509 /*
510 * Owner/group change request.
511 * We are guaranteed that the new owner/group is valid and legal.
512 */
513 VATTR_SET_SUPPORTED(vap, va_uid);
514 VATTR_SET_SUPPORTED(vap, va_gid);
515 nuid = VATTR_IS_ACTIVE(vap, va_uid) ? vap->va_uid : (uid_t)VNOVAL;
516 ngid = VATTR_IS_ACTIVE(vap, va_gid) ? vap->va_gid : (gid_t)VNOVAL;
517 if (((nuid != (uid_t)VNOVAL) || (ngid != (gid_t)VNOVAL)) &&
518 ((error = hfs_chown(vp, nuid, ngid, cred, p)) != 0))
519 goto out;
520
521 /*
522 * Mode change request.
523 * We are guaranteed that the mode value is valid and that in
524 * conjunction with the owner and group, this change is legal.
525 */
526 VATTR_SET_SUPPORTED(vap, va_mode);
527 if (VATTR_IS_ACTIVE(vap, va_mode) &&
528 ((error = hfs_chmod(vp, (int)vap->va_mode, cred, p)) != 0))
529 goto out;
530
531 /*
532 * File flags change.
533 * We are guaranteed that only flags allowed to change given the
534 * current securelevel are being changed.
535 */
536 VATTR_SET_SUPPORTED(vap, va_flags);
537 if (VATTR_IS_ACTIVE(vap, va_flags) &&
538 ((error = hfs_chflags(vp, vap->va_flags, cred, p)) != 0))
539 goto out;
540
541 /*
542 * If the file's extended security data is being changed, we
543 * need to note the change. Note that because we don't store
544 * the data, we do not set the SUPPORTED bit; this will cause
545 * the VFS to use a fallback strategy.
546 */
547 if (VATTR_IS_ACTIVE(vap, va_acl)) {
548 /* Remember if any ACL data was set or cleared. */
549 if (vap->va_acl == NULL) {
550 /* being cleared */
551 if (cp->c_attr.ca_recflags & kHFSHasSecurityMask) {
552 cp->c_attr.ca_recflags &= ~kHFSHasSecurityMask;
553 cp->c_touch_chgtime = TRUE;
554 }
555 } else {
556 /* being set */
557 if ((cp->c_attr.ca_recflags & kHFSHasSecurityMask) == 0) {
558 cp->c_attr.ca_recflags |= kHFSHasSecurityMask;
559 cp->c_touch_chgtime = TRUE;
560 }
561 }
562 }
563
564 /*
565 * Timestamp updates.
566 */
567 VATTR_SET_SUPPORTED(vap, va_create_time);
568 VATTR_SET_SUPPORTED(vap, va_access_time);
569 VATTR_SET_SUPPORTED(vap, va_modify_time);
570 VATTR_SET_SUPPORTED(vap, va_backup_time);
571 VATTR_SET_SUPPORTED(vap, va_change_time);
572 if (VATTR_IS_ACTIVE(vap, va_create_time) ||
573 VATTR_IS_ACTIVE(vap, va_access_time) ||
574 VATTR_IS_ACTIVE(vap, va_modify_time) ||
575 VATTR_IS_ACTIVE(vap, va_backup_time)) {
576 if (vnode_islnk(vp))
577 goto done;
578 if (VATTR_IS_ACTIVE(vap, va_create_time))
579 cp->c_itime = vap->va_create_time.tv_sec;
580 if (VATTR_IS_ACTIVE(vap, va_access_time)) {
581 cp->c_atime = vap->va_access_time.tv_sec;
582 cp->c_touch_acctime = FALSE;
583 }
584 if (VATTR_IS_ACTIVE(vap, va_modify_time)) {
585 cp->c_mtime = vap->va_modify_time.tv_sec;
586 cp->c_touch_modtime = FALSE;
587 cp->c_touch_chgtime = TRUE;
588
589 /*
590 * The utimes system call can reset the modification
591 * time but it doesn't know about HFS create times.
592 * So we need to ensure that the creation time is
593 * always at least as old as the modification time.
594 */
595 if ((VTOVCB(vp)->vcbSigWord == kHFSPlusSigWord) &&
596 (cp->c_cnid != kHFSRootFolderID) &&
597 (cp->c_mtime < cp->c_itime)) {
598 cp->c_itime = cp->c_mtime;
599 }
600 }
601 if (VATTR_IS_ACTIVE(vap, va_backup_time))
602 cp->c_btime = vap->va_backup_time.tv_sec;
603 cp->c_flag |= C_MODIFIED;
604 }
605
606 /*
607 * Set name encoding.
608 */
609 VATTR_SET_SUPPORTED(vap, va_encoding);
610 if (VATTR_IS_ACTIVE(vap, va_encoding)) {
611 cp->c_encoding = vap->va_encoding;
612 hfs_setencodingbits(hfsmp, cp->c_encoding);
613 }
614
615 done:
616 if ((error = hfs_update(vp, TRUE)) != 0)
617 goto out;
618 HFS_KNOTE(vp, NOTE_ATTRIB);
619 out:
620 if (cp)
621 hfs_unlock(cp);
622 return (error);
623 }
624
625
626 /*
627 * Change the mode on a file.
628 * cnode must be locked before calling.
629 */
630 __private_extern__
631 int
632 hfs_chmod(struct vnode *vp, int mode, kauth_cred_t cred, struct proc *p)
633 {
634 register struct cnode *cp = VTOC(vp);
635 int error;
636
637 if (VTOVCB(vp)->vcbSigWord != kHFSPlusSigWord)
638 return (0);
639
640 // XXXdbg - don't allow modification of the journal or journal_info_block
641 if (VTOHFS(vp)->jnl && cp && cp->c_datafork) {
642 struct HFSPlusExtentDescriptor *extd;
643
644 extd = &cp->c_datafork->ff_extents[0];
645 if (extd->startBlock == VTOVCB(vp)->vcbJinfoBlock || extd->startBlock == VTOHFS(vp)->jnl_start) {
646 return EPERM;
647 }
648 }
649
650 #if OVERRIDE_UNKNOWN_PERMISSIONS
651 if (((unsigned int)vfs_flags(VTOVFS(vp))) & MNT_UNKNOWNPERMISSIONS) {
652 return (0);
653 };
654 #endif
655 cp->c_mode &= ~ALLPERMS;
656 cp->c_mode |= (mode & ALLPERMS);
657 cp->c_touch_chgtime = TRUE;
658 return (0);
659 }
660
661
662 __private_extern__
663 int
664 hfs_write_access(struct vnode *vp, kauth_cred_t cred, struct proc *p, Boolean considerFlags)
665 {
666 struct cnode *cp = VTOC(vp);
667 int retval = 0;
668 int is_member;
669
670 /*
671 * Disallow write attempts on read-only file systems;
672 * unless the file is a socket, fifo, or a block or
673 * character device resident on the file system.
674 */
675 switch (vnode_vtype(vp)) {
676 case VDIR:
677 case VLNK:
678 case VREG:
679 if (VTOHFS(vp)->hfs_flags & HFS_READ_ONLY)
680 return (EROFS);
681 break;
682 default:
683 break;
684 }
685
686 /* If immutable bit set, nobody gets to write it. */
687 if (considerFlags && (cp->c_flags & IMMUTABLE))
688 return (EPERM);
689
690 /* Otherwise, user id 0 always gets access. */
691 if (!suser(cred, NULL))
692 return (0);
693
694 /* Otherwise, check the owner. */
695 if ((retval = hfs_owner_rights(VTOHFS(vp), cp->c_uid, cred, p, false)) == 0)
696 return ((cp->c_mode & S_IWUSR) == S_IWUSR ? 0 : EACCES);
697
698 /* Otherwise, check the groups. */
699 if (kauth_cred_ismember_gid(cred, cp->c_gid, &is_member) == 0 && is_member) {
700 return ((cp->c_mode & S_IWGRP) == S_IWGRP ? 0 : EACCES);
701 }
702
703 /* Otherwise, check everyone else. */
704 return ((cp->c_mode & S_IWOTH) == S_IWOTH ? 0 : EACCES);
705 }
706
707
708
709 /*
710 * Change the flags on a file or directory.
711 * cnode must be locked before calling.
712 */
713 __private_extern__
714 int
715 hfs_chflags(struct vnode *vp, uint32_t flags, __unused kauth_cred_t cred, __unused struct proc *p)
716 {
717 register struct cnode *cp = VTOC(vp);
718
719 cp->c_flags &= SF_SETTABLE;
720 cp->c_flags |= (flags & UF_SETTABLE);
721 cp->c_touch_chgtime = TRUE;
722
723 return (0);
724 }
725
726
727 /*
728 * Perform chown operation on cnode cp;
729 * code must be locked prior to call.
730 */
731 __private_extern__
732 int
733 hfs_chown(struct vnode *vp, uid_t uid, gid_t gid, kauth_cred_t cred,
734 struct proc *p)
735 {
736 register struct cnode *cp = VTOC(vp);
737 uid_t ouid;
738 gid_t ogid;
739 int error = 0;
740 int is_member;
741 #if QUOTA
742 register int i;
743 int64_t change;
744 #endif /* QUOTA */
745
746 if (VTOVCB(vp)->vcbSigWord != kHFSPlusSigWord)
747 return (ENOTSUP);
748
749 if (((unsigned int)vfs_flags(VTOVFS(vp))) & MNT_UNKNOWNPERMISSIONS)
750 return (0);
751
752 if (uid == (uid_t)VNOVAL)
753 uid = cp->c_uid;
754 if (gid == (gid_t)VNOVAL)
755 gid = cp->c_gid;
756
757 #if 0 /* we are guaranteed that this is already the case */
758 /*
759 * If we don't own the file, are trying to change the owner
760 * of the file, or are not a member of the target group,
761 * the caller must be superuser or the call fails.
762 */
763 if ((kauth_cred_getuid(cred) != cp->c_uid || uid != cp->c_uid ||
764 (gid != cp->c_gid &&
765 (kauth_cred_ismember_gid(cred, gid, &is_member) || !is_member))) &&
766 (error = suser(cred, 0)))
767 return (error);
768 #endif
769
770 ogid = cp->c_gid;
771 ouid = cp->c_uid;
772 #if QUOTA
773 if ((error = hfs_getinoquota(cp)))
774 return (error);
775 if (ouid == uid) {
776 dqrele(cp->c_dquot[USRQUOTA]);
777 cp->c_dquot[USRQUOTA] = NODQUOT;
778 }
779 if (ogid == gid) {
780 dqrele(cp->c_dquot[GRPQUOTA]);
781 cp->c_dquot[GRPQUOTA] = NODQUOT;
782 }
783
784 /*
785 * Eventually need to account for (fake) a block per directory
786 * if (vnode_isdir(vp))
787 * change = VTOHFS(vp)->blockSize;
788 * else
789 */
790
791 change = (int64_t)(cp->c_blocks) * (int64_t)VTOVCB(vp)->blockSize;
792 (void) hfs_chkdq(cp, -change, cred, CHOWN);
793 (void) hfs_chkiq(cp, -1, cred, CHOWN);
794 for (i = 0; i < MAXQUOTAS; i++) {
795 dqrele(cp->c_dquot[i]);
796 cp->c_dquot[i] = NODQUOT;
797 }
798 #endif /* QUOTA */
799 cp->c_gid = gid;
800 cp->c_uid = uid;
801 #if QUOTA
802 if ((error = hfs_getinoquota(cp)) == 0) {
803 if (ouid == uid) {
804 dqrele(cp->c_dquot[USRQUOTA]);
805 cp->c_dquot[USRQUOTA] = NODQUOT;
806 }
807 if (ogid == gid) {
808 dqrele(cp->c_dquot[GRPQUOTA]);
809 cp->c_dquot[GRPQUOTA] = NODQUOT;
810 }
811 if ((error = hfs_chkdq(cp, change, cred, CHOWN)) == 0) {
812 if ((error = hfs_chkiq(cp, 1, cred, CHOWN)) == 0)
813 goto good;
814 else
815 (void) hfs_chkdq(cp, -change, cred, CHOWN|FORCE);
816 }
817 for (i = 0; i < MAXQUOTAS; i++) {
818 dqrele(cp->c_dquot[i]);
819 cp->c_dquot[i] = NODQUOT;
820 }
821 }
822 cp->c_gid = ogid;
823 cp->c_uid = ouid;
824 if (hfs_getinoquota(cp) == 0) {
825 if (ouid == uid) {
826 dqrele(cp->c_dquot[USRQUOTA]);
827 cp->c_dquot[USRQUOTA] = NODQUOT;
828 }
829 if (ogid == gid) {
830 dqrele(cp->c_dquot[GRPQUOTA]);
831 cp->c_dquot[GRPQUOTA] = NODQUOT;
832 }
833 (void) hfs_chkdq(cp, change, cred, FORCE|CHOWN);
834 (void) hfs_chkiq(cp, 1, cred, FORCE|CHOWN);
835 (void) hfs_getinoquota(cp);
836 }
837 return (error);
838 good:
839 if (hfs_getinoquota(cp))
840 panic("hfs_chown: lost quota");
841 #endif /* QUOTA */
842
843 if (ouid != uid || ogid != gid)
844 cp->c_touch_chgtime = TRUE;
845 return (0);
846 }
847
848
849 /*
850 * The hfs_exchange routine swaps the fork data in two files by
851 * exchanging some of the information in the cnode. It is used
852 * to preserve the file ID when updating an existing file, in
853 * case the file is being tracked through its file ID. Typically
854 * its used after creating a new file during a safe-save.
855 */
856 static int
857 hfs_vnop_exchange(ap)
858 struct vnop_exchange_args /* {
859 struct vnode *a_fvp;
860 struct vnode *a_tvp;
861 int a_options;
862 vfs_context_t a_context;
863 } */ *ap;
864 {
865 struct vnode *from_vp = ap->a_fvp;
866 struct vnode *to_vp = ap->a_tvp;
867 struct cnode *from_cp;
868 struct cnode *to_cp;
869 struct hfsmount *hfsmp;
870 struct cat_desc tempdesc;
871 struct cat_attr tempattr;
872 int lockflags;
873 int error = 0, started_tr = 0, got_cookie = 0;
874 cat_cookie_t cookie;
875
876 /* The files must be on the same volume. */
877 if (vnode_mount(from_vp) != vnode_mount(to_vp))
878 return (EXDEV);
879
880 if (from_vp == to_vp)
881 return (EINVAL);
882
883 if ((error = hfs_lockpair(VTOC(from_vp), VTOC(to_vp), HFS_EXCLUSIVE_LOCK)))
884 return (error);
885
886 from_cp = VTOC(from_vp);
887 to_cp = VTOC(to_vp);
888 hfsmp = VTOHFS(from_vp);
889
890 /* Only normal files can be exchanged. */
891 if (!vnode_isreg(from_vp) || !vnode_isreg(to_vp) ||
892 (from_cp->c_flag & C_HARDLINK) || (to_cp->c_flag & C_HARDLINK) ||
893 VNODE_IS_RSRC(from_vp) || VNODE_IS_RSRC(to_vp)) {
894 error = EINVAL;
895 goto exit;
896 }
897
898 // XXXdbg - don't allow modification of the journal or journal_info_block
899 if (hfsmp->jnl) {
900 struct HFSPlusExtentDescriptor *extd;
901
902 if (from_cp->c_datafork) {
903 extd = &from_cp->c_datafork->ff_extents[0];
904 if (extd->startBlock == VTOVCB(from_vp)->vcbJinfoBlock || extd->startBlock == hfsmp->jnl_start) {
905 error = EPERM;
906 goto exit;
907 }
908 }
909
910 if (to_cp->c_datafork) {
911 extd = &to_cp->c_datafork->ff_extents[0];
912 if (extd->startBlock == VTOVCB(to_vp)->vcbJinfoBlock || extd->startBlock == hfsmp->jnl_start) {
913 error = EPERM;
914 goto exit;
915 }
916 }
917 }
918
919 if ((error = hfs_start_transaction(hfsmp)) != 0) {
920 goto exit;
921 }
922 started_tr = 1;
923
924 /*
925 * Reserve some space in the Catalog file.
926 */
927 bzero(&cookie, sizeof(cookie));
928 if ((error = cat_preflight(hfsmp, CAT_EXCHANGE, &cookie, vfs_context_proc(ap->a_context)))) {
929 goto exit;
930 }
931 got_cookie = 1;
932
933 /* The backend code always tries to delete the virtual
934 * extent id for exchanging files so we neeed to lock
935 * the extents b-tree.
936 */
937 lockflags = hfs_systemfile_lock(hfsmp, SFL_CATALOG | SFL_EXTENTS | SFL_ATTRIBUTE, HFS_EXCLUSIVE_LOCK);
938
939 /* Do the exchange */
940 error = ExchangeFileIDs(hfsmp,
941 from_cp->c_desc.cd_nameptr,
942 to_cp->c_desc.cd_nameptr,
943 from_cp->c_parentcnid,
944 to_cp->c_parentcnid,
945 from_cp->c_hint,
946 to_cp->c_hint);
947 hfs_systemfile_unlock(hfsmp, lockflags);
948
949 /*
950 * Note that we don't need to exchange any extended attributes
951 * since the attributes are keyed by file ID.
952 */
953
954 if (error != E_NONE) {
955 error = MacToVFSError(error);
956 goto exit;
957 }
958
959 /* Purge the vnodes from the name cache */
960 if (from_vp)
961 cache_purge(from_vp);
962 if (to_vp)
963 cache_purge(to_vp);
964
965 /* Save a copy of from attributes before swapping. */
966 bcopy(&from_cp->c_desc, &tempdesc, sizeof(struct cat_desc));
967 bcopy(&from_cp->c_attr, &tempattr, sizeof(struct cat_attr));
968
969 /*
970 * Swap the descriptors and all non-fork related attributes.
971 * (except the modify date)
972 */
973 bcopy(&to_cp->c_desc, &from_cp->c_desc, sizeof(struct cat_desc));
974
975 from_cp->c_hint = 0;
976 from_cp->c_fileid = from_cp->c_cnid;
977 from_cp->c_itime = to_cp->c_itime;
978 from_cp->c_btime = to_cp->c_btime;
979 from_cp->c_atime = to_cp->c_atime;
980 from_cp->c_ctime = to_cp->c_ctime;
981 from_cp->c_gid = to_cp->c_gid;
982 from_cp->c_uid = to_cp->c_uid;
983 from_cp->c_flags = to_cp->c_flags;
984 from_cp->c_mode = to_cp->c_mode;
985 bcopy(to_cp->c_finderinfo, from_cp->c_finderinfo, 32);
986
987 bcopy(&tempdesc, &to_cp->c_desc, sizeof(struct cat_desc));
988 to_cp->c_hint = 0;
989 to_cp->c_fileid = to_cp->c_cnid;
990 to_cp->c_itime = tempattr.ca_itime;
991 to_cp->c_btime = tempattr.ca_btime;
992 to_cp->c_atime = tempattr.ca_atime;
993 to_cp->c_ctime = tempattr.ca_ctime;
994 to_cp->c_gid = tempattr.ca_gid;
995 to_cp->c_uid = tempattr.ca_uid;
996 to_cp->c_flags = tempattr.ca_flags;
997 to_cp->c_mode = tempattr.ca_mode;
998 bcopy(tempattr.ca_finderinfo, to_cp->c_finderinfo, 32);
999
1000 /* Rehash the cnodes using their new file IDs */
1001 hfs_chash_rehash(from_cp, to_cp);
1002
1003 /*
1004 * When a file moves out of "Cleanup At Startup"
1005 * we can drop its NODUMP status.
1006 */
1007 if ((from_cp->c_flags & UF_NODUMP) &&
1008 (from_cp->c_parentcnid != to_cp->c_parentcnid)) {
1009 from_cp->c_flags &= ~UF_NODUMP;
1010 from_cp->c_touch_chgtime = TRUE;
1011 }
1012 if ((to_cp->c_flags & UF_NODUMP) &&
1013 (to_cp->c_parentcnid != from_cp->c_parentcnid)) {
1014 to_cp->c_flags &= ~UF_NODUMP;
1015 to_cp->c_touch_chgtime = TRUE;
1016 }
1017
1018 HFS_KNOTE(from_vp, NOTE_ATTRIB);
1019 HFS_KNOTE(to_vp, NOTE_ATTRIB);
1020
1021 exit:
1022 if (got_cookie) {
1023 cat_postflight(hfsmp, &cookie, vfs_context_proc(ap->a_context));
1024 }
1025 if (started_tr) {
1026 hfs_end_transaction(hfsmp);
1027 }
1028
1029 hfs_unlockpair(from_cp, to_cp);
1030 return (error);
1031 }
1032
1033
1034 /*
1035 * cnode must be locked
1036 */
1037 __private_extern__
1038 int
1039 hfs_fsync(struct vnode *vp, int waitfor, int fullsync, struct proc *p)
1040 {
1041 struct cnode *cp = VTOC(vp);
1042 struct filefork *fp = NULL;
1043 int retval = 0;
1044 struct hfsmount *hfsmp = VTOHFS(vp);
1045 struct timeval tv;
1046 int wait;
1047 int lockflag;
1048 int took_trunc_lock = 0;
1049
1050 wait = (waitfor == MNT_WAIT);
1051
1052 /* HFS directories don't have any data blocks. */
1053 if (vnode_isdir(vp))
1054 goto metasync;
1055
1056 /*
1057 * For system files flush the B-tree header and
1058 * for regular files write out any clusters
1059 */
1060 if (vnode_issystem(vp)) {
1061 if (VTOF(vp)->fcbBTCBPtr != NULL) {
1062 // XXXdbg
1063 if (hfsmp->jnl == NULL) {
1064 BTFlushPath(VTOF(vp));
1065 }
1066 }
1067 } else if (UBCINFOEXISTS(vp)) {
1068 hfs_unlock(cp);
1069 hfs_lock_truncate(cp, TRUE);
1070 took_trunc_lock = 1;
1071
1072 /* Don't hold cnode lock when calling into cluster layer. */
1073 (void) cluster_push(vp, 0);
1074
1075 hfs_lock(cp, HFS_FORCE_LOCK);
1076 }
1077 /*
1078 * When MNT_WAIT is requested and the zero fill timeout
1079 * has expired then we must explicitly zero out any areas
1080 * that are currently marked invalid (holes).
1081 *
1082 * Files with NODUMP can bypass zero filling here.
1083 */
1084 if ((wait || (cp->c_flag & C_ZFWANTSYNC)) &&
1085 ((cp->c_flags & UF_NODUMP) == 0) &&
1086 UBCINFOEXISTS(vp) && (fp = VTOF(vp)) &&
1087 cp->c_zftimeout != 0) {
1088 microuptime(&tv);
1089 if (tv.tv_sec < cp->c_zftimeout) {
1090 /* Remember that a force sync was requested. */
1091 cp->c_flag |= C_ZFWANTSYNC;
1092 goto datasync;
1093 }
1094 if (!took_trunc_lock) {
1095 hfs_unlock(cp);
1096 hfs_lock_truncate(cp, TRUE);
1097 hfs_lock(cp, HFS_FORCE_LOCK);
1098 took_trunc_lock = 1;
1099 }
1100
1101 while (!CIRCLEQ_EMPTY(&fp->ff_invalidranges)) {
1102 struct rl_entry *invalid_range = CIRCLEQ_FIRST(&fp->ff_invalidranges);
1103 off_t start = invalid_range->rl_start;
1104 off_t end = invalid_range->rl_end;
1105
1106 /* The range about to be written must be validated
1107 * first, so that VNOP_BLOCKMAP() will return the
1108 * appropriate mapping for the cluster code:
1109 */
1110 rl_remove(start, end, &fp->ff_invalidranges);
1111
1112 /* Don't hold cnode lock when calling into cluster layer. */
1113 hfs_unlock(cp);
1114 (void) cluster_write(vp, (struct uio *) 0,
1115 fp->ff_size, end + 1, start, (off_t)0,
1116 IO_HEADZEROFILL | IO_NOZERODIRTY | IO_NOCACHE);
1117 hfs_lock(cp, HFS_FORCE_LOCK);
1118 cp->c_flag |= C_MODIFIED;
1119 }
1120 hfs_unlock(cp);
1121 (void) cluster_push(vp, 0);
1122 hfs_lock(cp, HFS_FORCE_LOCK);
1123
1124 cp->c_flag &= ~C_ZFWANTSYNC;
1125 cp->c_zftimeout = 0;
1126 }
1127 datasync:
1128 if (took_trunc_lock)
1129 hfs_unlock_truncate(cp);
1130
1131 /*
1132 * if we have a journal and if journal_active() returns != 0 then the
1133 * we shouldn't do anything to a locked block (because it is part
1134 * of a transaction). otherwise we'll just go through the normal
1135 * code path and flush the buffer. note journal_active() can return
1136 * -1 if the journal is invalid -- however we still need to skip any
1137 * locked blocks as they get cleaned up when we finish the transaction
1138 * or close the journal.
1139 */
1140 // if (hfsmp->jnl && journal_active(hfsmp->jnl) >= 0)
1141 if (hfsmp->jnl)
1142 lockflag = BUF_SKIP_LOCKED;
1143 else
1144 lockflag = 0;
1145
1146 /*
1147 * Flush all dirty buffers associated with a vnode.
1148 */
1149 buf_flushdirtyblks(vp, wait, lockflag, "hfs_fsync");
1150
1151 metasync:
1152 if (vnode_isreg(vp) && vnode_issystem(vp)) {
1153 if (VTOF(vp)->fcbBTCBPtr != NULL) {
1154 microuptime(&tv);
1155 BTSetLastSync(VTOF(vp), tv.tv_sec);
1156 }
1157 cp->c_touch_acctime = FALSE;
1158 cp->c_touch_chgtime = FALSE;
1159 cp->c_touch_modtime = FALSE;
1160 } else /* User file */ {
1161 retval = hfs_update(vp, wait);
1162
1163 /* When MNT_WAIT is requested push out any delayed meta data */
1164 if ((retval == 0) && wait && cp->c_hint &&
1165 !ISSET(cp->c_flag, C_DELETED | C_NOEXISTS)) {
1166 hfs_metasync(VTOHFS(vp), (daddr64_t)cp->c_hint, p);
1167 }
1168
1169 // make sure that we've really been called from the user
1170 // fsync() and if so push out any pending transactions
1171 // that this file might is a part of (and get them on
1172 // stable storage).
1173 if (fullsync) {
1174 if (hfsmp->jnl) {
1175 journal_flush(hfsmp->jnl);
1176 } else {
1177 /* XXX need to pass context! */
1178 VNOP_IOCTL(hfsmp->hfs_devvp, DKIOCSYNCHRONIZECACHE, NULL, FWRITE, NULL);
1179 }
1180 }
1181 }
1182
1183 return (retval);
1184 }
1185
1186
1187 /* Sync an hfs catalog b-tree node */
1188 static int
1189 hfs_metasync(struct hfsmount *hfsmp, daddr64_t node, struct proc *p)
1190 {
1191 vnode_t vp;
1192 buf_t bp;
1193 int lockflags;
1194
1195 vp = HFSTOVCB(hfsmp)->catalogRefNum;
1196
1197 // XXXdbg - don't need to do this on a journaled volume
1198 if (hfsmp->jnl) {
1199 return 0;
1200 }
1201
1202 lockflags = hfs_systemfile_lock(hfsmp, SFL_CATALOG, HFS_EXCLUSIVE_LOCK);
1203 /*
1204 * Look for a matching node that has been delayed
1205 * but is not part of a set (B_LOCKED).
1206 *
1207 * BLK_ONLYVALID causes buf_getblk to return a
1208 * buf_t for the daddr64_t specified only if it's
1209 * currently resident in the cache... the size
1210 * parameter to buf_getblk is ignored when this flag
1211 * is set
1212 */
1213 bp = buf_getblk(vp, node, 0, 0, 0, BLK_META | BLK_ONLYVALID);
1214
1215 if (bp) {
1216 if ((buf_flags(bp) & (B_LOCKED | B_DELWRI)) == B_DELWRI)
1217 (void) VNOP_BWRITE(bp);
1218 else
1219 buf_brelse(bp);
1220 }
1221
1222 hfs_systemfile_unlock(hfsmp, lockflags);
1223
1224 return (0);
1225 }
1226
1227
1228 /*ARGSUSED 1*/
1229 static int
1230 hfs_btsync_callback(struct buf *bp, void *dummy)
1231 {
1232 buf_clearflags(bp, B_LOCKED);
1233 (void) buf_bawrite(bp);
1234
1235 return(BUF_CLAIMED);
1236 }
1237
1238
1239 __private_extern__
1240 int
1241 hfs_btsync(struct vnode *vp, int sync_transaction)
1242 {
1243 struct cnode *cp = VTOC(vp);
1244 struct timeval tv;
1245 int flags = 0;
1246
1247 if (sync_transaction)
1248 flags |= BUF_SKIP_NONLOCKED;
1249 /*
1250 * Flush all dirty buffers associated with b-tree.
1251 */
1252 buf_iterate(vp, hfs_btsync_callback, flags, 0);
1253
1254 microuptime(&tv);
1255 if (vnode_issystem(vp) && (VTOF(vp)->fcbBTCBPtr != NULL))
1256 (void) BTSetLastSync(VTOF(vp), tv.tv_sec);
1257 cp->c_touch_acctime = FALSE;
1258 cp->c_touch_chgtime = FALSE;
1259 cp->c_touch_modtime = FALSE;
1260
1261 return 0;
1262 }
1263
1264 /*
1265 * Remove a directory.
1266 */
1267 static int
1268 hfs_vnop_rmdir(ap)
1269 struct vnop_rmdir_args /* {
1270 struct vnode *a_dvp;
1271 struct vnode *a_vp;
1272 struct componentname *a_cnp;
1273 vfs_context_t a_context;
1274 } */ *ap;
1275 {
1276 struct vnode *dvp = ap->a_dvp;
1277 struct vnode *vp = ap->a_vp;
1278 int error;
1279
1280 if (!vnode_isdir(vp)) {
1281 return (ENOTDIR);
1282 }
1283 if (dvp == vp) {
1284 return (EINVAL);
1285 }
1286 if ((error = hfs_lockpair(VTOC(dvp), VTOC(vp), HFS_EXCLUSIVE_LOCK)))
1287 return (error);
1288
1289 error = hfs_removedir(dvp, vp, ap->a_cnp, 0);
1290
1291 hfs_unlockpair(VTOC(dvp), VTOC(vp));
1292
1293 return (error);
1294 }
1295
1296 /*
1297 * Remove a directory
1298 *
1299 * Both dvp and vp cnodes are locked
1300 */
1301 static int
1302 hfs_removedir(struct vnode *dvp, struct vnode *vp, struct componentname *cnp,
1303 int skip_reserve)
1304 {
1305 vfs_context_t ctx = cnp->cn_context;
1306 struct proc *p = vfs_context_proc(ctx);
1307 struct cnode *cp;
1308 struct cnode *dcp;
1309 struct hfsmount * hfsmp;
1310 struct cat_desc desc;
1311 cat_cookie_t cookie;
1312 int lockflags;
1313 int error = 0, started_tr = 0, got_cookie = 0;
1314
1315 cp = VTOC(vp);
1316 dcp = VTOC(dvp);
1317 hfsmp = VTOHFS(vp);
1318
1319 if (dcp == cp)
1320 return (EINVAL); /* cannot remove "." */
1321
1322 #if QUOTA
1323 (void)hfs_getinoquota(cp);
1324 #endif
1325 if ((error = hfs_start_transaction(hfsmp)) != 0) {
1326 goto out;
1327 }
1328 started_tr = 1;
1329
1330 if (!skip_reserve) {
1331 /*
1332 * Reserve some space in the Catalog file.
1333 */
1334 bzero(&cookie, sizeof(cookie));
1335 if ((error = cat_preflight(hfsmp, CAT_DELETE, &cookie, p))) {
1336 goto out;
1337 }
1338 got_cookie = 1;
1339 }
1340
1341 /*
1342 * Verify the directory is empty (and valid).
1343 * (Rmdir ".." won't be valid since
1344 * ".." will contain a reference to
1345 * the current directory and thus be
1346 * non-empty.)
1347 */
1348 if (cp->c_entries != 0) {
1349 error = ENOTEMPTY;
1350 goto out;
1351 }
1352 if ((dcp->c_flags & APPEND) || (cp->c_flags & (IMMUTABLE | APPEND))) {
1353 error = EPERM;
1354 goto out;
1355 }
1356
1357 if (cp->c_entries > 0)
1358 panic("hfs_rmdir: attempting to delete a non-empty directory!");
1359
1360 /* Remove the entry from the namei cache: */
1361 cache_purge(vp);
1362
1363 /*
1364 * Protect against a race with rename by using the component
1365 * name passed in and parent id from dvp (instead of using
1366 * the cp->c_desc which may have changed).
1367 */
1368 bzero(&desc, sizeof(desc));
1369 desc.cd_nameptr = cnp->cn_nameptr;
1370 desc.cd_namelen = cnp->cn_namelen;
1371 desc.cd_parentcnid = dcp->c_cnid;
1372 desc.cd_cnid = cp->c_cnid;
1373
1374 /* Remove entry from catalog */
1375 lockflags = hfs_systemfile_lock(hfsmp, SFL_CATALOG | SFL_ATTRIBUTE, HFS_EXCLUSIVE_LOCK);
1376 error = cat_delete(hfsmp, &desc, &cp->c_attr);
1377 if (error == 0) {
1378 /* Delete any attributes, ignore errors */
1379 (void) hfs_removeallattr(hfsmp, cp->c_fileid);
1380 }
1381 hfs_systemfile_unlock(hfsmp, lockflags);
1382
1383 if (error)
1384 goto out;
1385
1386 #if QUOTA
1387 (void)hfs_chkiq(cp, -1, NOCRED, 0);
1388 #endif /* QUOTA */
1389
1390 /* The parent lost a child */
1391 if (dcp->c_entries > 0)
1392 dcp->c_entries--;
1393 if (dcp->c_nlink > 0)
1394 dcp->c_nlink--;
1395 dcp->c_touch_chgtime = TRUE;
1396 dcp->c_touch_modtime = TRUE;
1397
1398 dcp->c_flag |= C_FORCEUPDATE; // XXXdbg - don't screw around, force this guy out
1399
1400 (void) hfs_update(dvp, 0);
1401 HFS_KNOTE(dvp, NOTE_WRITE | NOTE_LINK);
1402
1403 hfs_volupdate(hfsmp, VOL_RMDIR, (dcp->c_cnid == kHFSRootFolderID));
1404
1405 cp->c_mode = 0; /* Makes the vnode go away...see inactive */
1406 cp->c_flag |= C_NOEXISTS;
1407 out:
1408 HFS_KNOTE(vp, NOTE_DELETE);
1409
1410 if (got_cookie) {
1411 cat_postflight(hfsmp, &cookie, p);
1412 }
1413 if (started_tr) {
1414 hfs_end_transaction(hfsmp);
1415 }
1416
1417 return (error);
1418 }
1419
1420
1421 /*
1422 * Remove a file or link.
1423 */
1424 static int
1425 hfs_vnop_remove(ap)
1426 struct vnop_remove_args /* {
1427 struct vnode *a_dvp;
1428 struct vnode *a_vp;
1429 struct componentname *a_cnp;
1430 int a_flags;
1431 vfs_context_t a_context;
1432 } */ *ap;
1433 {
1434 struct vnode *dvp = ap->a_dvp;
1435 struct vnode *vp = ap->a_vp;
1436 int error;
1437
1438 if (dvp == vp) {
1439 return (EINVAL);
1440 }
1441
1442 hfs_lock_truncate(VTOC(vp), TRUE);
1443
1444 if ((error = hfs_lockpair(VTOC(dvp), VTOC(vp), HFS_EXCLUSIVE_LOCK)))
1445 goto out;
1446
1447 error = hfs_removefile(dvp, vp, ap->a_cnp, ap->a_flags, 0);
1448
1449 hfs_unlockpair(VTOC(dvp), VTOC(vp));
1450 out:
1451 hfs_unlock_truncate(VTOC(vp));
1452 return (error);
1453 }
1454
1455
1456 static int
1457 hfs_removefile_callback(struct buf *bp, void *hfsmp) {
1458
1459 if ( !(buf_flags(bp) & B_META))
1460 panic("hfs: symlink bp @ 0x%x is not marked meta-data!\n", bp);
1461 /*
1462 * it's part of the current transaction, kill it.
1463 */
1464 journal_kill_block(((struct hfsmount *)hfsmp)->jnl, bp);
1465
1466 return (BUF_CLAIMED);
1467 }
1468
1469 /*
1470 * hfs_removefile
1471 *
1472 * Similar to hfs_vnop_remove except there are additional options.
1473 *
1474 * Requires cnode and truncate locks to be held.
1475 */
1476 static int
1477 hfs_removefile(struct vnode *dvp, struct vnode *vp, struct componentname *cnp,
1478 int flags, int skip_reserve)
1479 {
1480 struct vnode *rvp = NULL;
1481 struct cnode *cp;
1482 struct cnode *dcp;
1483 struct hfsmount *hfsmp;
1484 struct cat_desc desc;
1485 struct timeval tv;
1486 vfs_context_t ctx = cnp->cn_context;
1487 int dataforkbusy = 0;
1488 int rsrcforkbusy = 0;
1489 int truncated = 0;
1490 cat_cookie_t cookie;
1491 int lockflags;
1492 int error = 0;
1493 int started_tr = 0, got_cookie = 0;
1494 int isbigfile = 0;
1495 cnid_t real_cnid = 0;
1496
1497 /* Directories should call hfs_rmdir! */
1498 if (vnode_isdir(vp)) {
1499 return (EISDIR);
1500 }
1501
1502 cp = VTOC(vp);
1503 dcp = VTOC(dvp);
1504 hfsmp = VTOHFS(vp);
1505
1506 if (cp->c_flag & (C_NOEXISTS | C_DELETED)) {
1507 return 0;
1508 }
1509
1510 // if it's not a hardlink, check that the parent
1511 // cnid is the same as the directory cnid
1512 if ( (cp->c_flag & C_HARDLINK) == 0
1513 && (cp->c_parentcnid != hfsmp->hfs_privdir_desc.cd_cnid)
1514 && (cp->c_parentcnid != dcp->c_cnid)) {
1515 error = EINVAL;
1516 goto out;
1517 }
1518
1519 /* Make sure a remove is permitted */
1520 if (VNODE_IS_RSRC(vp)) {
1521 error = EPERM;
1522 goto out;
1523 }
1524
1525 /*
1526 * Aquire a vnode for a non-empty resource fork.
1527 * (needed for hfs_truncate)
1528 */
1529 if (cp->c_blocks - VTOF(vp)->ff_blocks) {
1530 error = hfs_vgetrsrc(hfsmp, vp, &rvp, 0);
1531 if (error)
1532 goto out;
1533 }
1534
1535 // XXXdbg - don't allow deleting the journal or journal_info_block
1536 if (hfsmp->jnl && cp->c_datafork) {
1537 struct HFSPlusExtentDescriptor *extd;
1538
1539 extd = &cp->c_datafork->ff_extents[0];
1540 if (extd->startBlock == HFSTOVCB(hfsmp)->vcbJinfoBlock || extd->startBlock == hfsmp->jnl_start) {
1541 error = EPERM;
1542 goto out;
1543 }
1544 }
1545
1546 /*
1547 * Check if this file is being used.
1548 */
1549 if (vnode_isinuse(vp, 0))
1550 dataforkbusy = 1;
1551 if (rvp && vnode_isinuse(rvp, 0))
1552 rsrcforkbusy = 1;
1553
1554 // need this to check if we have to break the deletion
1555 // into multiple pieces
1556 isbigfile = (VTOC(vp)->c_datafork->ff_size >= HFS_BIGFILE_SIZE);
1557
1558 /*
1559 * Carbon semantics prohibit deleting busy files.
1560 * (enforced when VNODE_REMOVE_NODELETEBUSY is requested)
1561 */
1562 if (dataforkbusy || rsrcforkbusy) {
1563 if ((flags & VNODE_REMOVE_NODELETEBUSY) ||
1564 (hfsmp->hfs_privdir_desc.cd_cnid == 0)) {
1565 error = EBUSY;
1566 goto out;
1567 }
1568 }
1569
1570 #if QUOTA
1571 (void)hfs_getinoquota(cp);
1572 #endif /* QUOTA */
1573
1574 /*
1575 * We do the ubc_setsize before the hfs_truncate
1576 * since we'll be inside a transaction.
1577 */
1578 if ((cp->c_flag & C_HARDLINK) == 0 &&
1579 (!dataforkbusy || !rsrcforkbusy)) {
1580 /*
1581 * A ubc_setsize can cause a pagein here
1582 * so we need to the drop cnode lock. Note
1583 * that we still hold the truncate lock.
1584 */
1585 hfs_unlock(cp);
1586 if (!dataforkbusy && cp->c_datafork->ff_blocks && !isbigfile) {
1587 ubc_setsize(vp, 0);
1588 }
1589 if (!rsrcforkbusy && rvp) {
1590 ubc_setsize(rvp, 0);
1591 }
1592 hfs_lock(cp, HFS_FORCE_LOCK);
1593 } else {
1594 struct cat_desc cndesc;
1595
1596 // for hard links, re-lookup the name that was passed
1597 // in so we get the correct cnid for the name (as
1598 // opposed to the c_cnid in the cnode which could have
1599 // been changed before this node got locked).
1600 bzero(&cndesc, sizeof(cndesc));
1601 cndesc.cd_nameptr = cnp->cn_nameptr;
1602 cndesc.cd_namelen = cnp->cn_namelen;
1603 cndesc.cd_parentcnid = VTOC(dvp)->c_cnid;
1604 cndesc.cd_hint = VTOC(dvp)->c_childhint;
1605
1606 lockflags = hfs_systemfile_lock(hfsmp, SFL_CATALOG, HFS_SHARED_LOCK);
1607
1608 if (cat_lookup(hfsmp, &cndesc, 0, NULL, NULL, NULL, &real_cnid) != 0) {
1609 hfs_systemfile_unlock(hfsmp, lockflags);
1610 error = ENOENT;
1611 goto out;
1612 }
1613
1614 hfs_systemfile_unlock(hfsmp, lockflags);
1615 }
1616
1617 if ((error = hfs_start_transaction(hfsmp)) != 0) {
1618 goto out;
1619 }
1620 started_tr = 1;
1621
1622 if (!skip_reserve) {
1623 /*
1624 * Reserve some space in the Catalog file.
1625 */
1626 if ((error = cat_preflight(hfsmp, CAT_DELETE, &cookie, 0))) {
1627 goto out;
1628 }
1629 got_cookie = 1;
1630 }
1631
1632 /* Remove our entry from the namei cache. */
1633 cache_purge(vp);
1634
1635 // XXXdbg - if we're journaled, kill any dirty symlink buffers
1636 if (hfsmp->jnl && vnode_islnk(vp))
1637 buf_iterate(vp, hfs_removefile_callback, BUF_SKIP_NONLOCKED, (void *)hfsmp);
1638
1639 /*
1640 * Truncate any non-busy forks. Busy forks will
1641 * get trucated when their vnode goes inactive.
1642 *
1643 * Since we're already inside a transaction,
1644 * tell hfs_truncate to skip the ubc_setsize.
1645 *
1646 * (Note: hard links are truncated in VOP_INACTIVE)
1647 */
1648 if ((cp->c_flag & C_HARDLINK) == 0) {
1649 int mode = cp->c_mode;
1650
1651 if (!dataforkbusy && !isbigfile && cp->c_datafork->ff_blocks != 0) {
1652 cp->c_mode = 0; /* Suppress hfs_update */
1653 error = hfs_truncate(vp, (off_t)0, IO_NDELAY, 1, ctx);
1654 cp->c_mode = mode;
1655 if (error)
1656 goto out;
1657 truncated = 1;
1658 }
1659 if (!rsrcforkbusy && rvp) {
1660 cp->c_mode = 0; /* Suppress hfs_update */
1661 error = hfs_truncate(rvp, (off_t)0, IO_NDELAY, 1, ctx);
1662 cp->c_mode = mode;
1663 if (error)
1664 goto out;
1665 truncated = 1;
1666 }
1667 }
1668
1669 /*
1670 * Protect against a race with rename by using the component
1671 * name passed in and parent id from dvp (instead of using
1672 * the cp->c_desc which may have changed).
1673 */
1674 desc.cd_flags = 0;
1675 desc.cd_encoding = cp->c_desc.cd_encoding;
1676 desc.cd_nameptr = cnp->cn_nameptr;
1677 desc.cd_namelen = cnp->cn_namelen;
1678 desc.cd_parentcnid = dcp->c_cnid;
1679 desc.cd_hint = cp->c_desc.cd_hint;
1680 if (real_cnid) {
1681 // if it was a hardlink we had to re-lookup the cnid
1682 desc.cd_cnid = real_cnid;
1683 } else {
1684 desc.cd_cnid = cp->c_cnid;
1685 }
1686 microtime(&tv);
1687
1688 /*
1689 * There are 3 remove cases to consider:
1690 * 1. File is a hardlink ==> remove the link
1691 * 2. File is busy (in use) ==> move/rename the file
1692 * 3. File is not in use ==> remove the file
1693 */
1694
1695 if (cp->c_flag & C_HARDLINK) {
1696 lockflags = hfs_systemfile_lock(hfsmp, SFL_CATALOG, HFS_EXCLUSIVE_LOCK);
1697
1698 /* Delete the link record */
1699 error = cat_delete(hfsmp, &desc, &cp->c_attr);
1700 if (error == 0) {
1701 /* Update the parent directory */
1702 if (dcp->c_entries > 0)
1703 dcp->c_entries--;
1704 if (dcp->c_nlink > 0)
1705 dcp->c_nlink--;
1706 dcp->c_ctime = tv.tv_sec;
1707 dcp->c_mtime = tv.tv_sec;
1708 (void ) cat_update(hfsmp, &dcp->c_desc, &dcp->c_attr, NULL, NULL);
1709
1710 if (--cp->c_nlink < 1) {
1711 char inodename[32];
1712 char delname[32];
1713 struct cat_desc to_desc;
1714 struct cat_desc from_desc;
1715
1716 /*
1717 * This is now esentially an open deleted file.
1718 * Rename it to reflect this state which makes
1719 * orphan file cleanup easier (see hfs_remove_orphans).
1720 * Note: a rename failure here is not fatal.
1721 */
1722 MAKE_INODE_NAME(inodename, cp->c_rdev);
1723 bzero(&from_desc, sizeof(from_desc));
1724 from_desc.cd_nameptr = inodename;
1725 from_desc.cd_namelen = strlen(inodename);
1726 from_desc.cd_parentcnid = hfsmp->hfs_privdir_desc.cd_cnid;
1727 from_desc.cd_flags = 0;
1728 from_desc.cd_cnid = cp->c_fileid;
1729
1730 MAKE_DELETED_NAME(delname, cp->c_fileid);
1731 bzero(&to_desc, sizeof(to_desc));
1732 to_desc.cd_nameptr = delname;
1733 to_desc.cd_namelen = strlen(delname);
1734 to_desc.cd_parentcnid = hfsmp->hfs_privdir_desc.cd_cnid;
1735 to_desc.cd_flags = 0;
1736 to_desc.cd_cnid = cp->c_fileid;
1737
1738 error = cat_rename(hfsmp, &from_desc, &hfsmp->hfs_privdir_desc,
1739 &to_desc, (struct cat_desc *)NULL);
1740 if (error != 0) {
1741 panic("hfs_removefile: error %d from cat_rename(%s %s) cp 0x%x\n",
1742 inodename, delname, cp);
1743 }
1744 if (error == 0) {
1745 /* Update the file's state */
1746 cp->c_flag |= C_DELETED;
1747 cp->c_ctime = tv.tv_sec;
1748 (void) cat_update(hfsmp, &to_desc, &cp->c_attr, NULL, NULL);
1749 }
1750 } else {
1751 /* Update the file's state */
1752 cp->c_ctime = tv.tv_sec;
1753 (void) cat_update(hfsmp, &cp->c_desc, &cp->c_attr, NULL, NULL);
1754 }
1755 }
1756 hfs_systemfile_unlock(hfsmp, lockflags);
1757 if (error != 0)
1758 goto out;
1759
1760 hfs_volupdate(hfsmp, VOL_RMFILE, (dcp->c_cnid == kHFSRootFolderID));
1761
1762 } else if (dataforkbusy || rsrcforkbusy || isbigfile) {
1763 char delname[32];
1764 struct cat_desc to_desc;
1765 struct cat_desc todir_desc;
1766
1767 /*
1768 * Orphan this file (move to hidden directory).
1769 */
1770 bzero(&todir_desc, sizeof(todir_desc));
1771 todir_desc.cd_parentcnid = 2;
1772
1773 MAKE_DELETED_NAME(delname, cp->c_fileid);
1774 bzero(&to_desc, sizeof(to_desc));
1775 to_desc.cd_nameptr = delname;
1776 to_desc.cd_namelen = strlen(delname);
1777 to_desc.cd_parentcnid = hfsmp->hfs_privdir_desc.cd_cnid;
1778 to_desc.cd_flags = 0;
1779 to_desc.cd_cnid = cp->c_cnid;
1780
1781 lockflags = hfs_systemfile_lock(hfsmp, SFL_CATALOG, HFS_EXCLUSIVE_LOCK);
1782
1783 error = cat_rename(hfsmp, &desc, &todir_desc,
1784 &to_desc, (struct cat_desc *)NULL);
1785
1786 if (error == 0) {
1787 hfsmp->hfs_privdir_attr.ca_entries++;
1788 (void) cat_update(hfsmp, &hfsmp->hfs_privdir_desc,
1789 &hfsmp->hfs_privdir_attr, NULL, NULL);
1790
1791 /* Update the parent directory */
1792 if (dcp->c_entries > 0)
1793 dcp->c_entries--;
1794 if (dcp->c_nlink > 0)
1795 dcp->c_nlink--;
1796 dcp->c_ctime = tv.tv_sec;
1797 dcp->c_mtime = tv.tv_sec;
1798 (void) cat_update(hfsmp, &dcp->c_desc, &dcp->c_attr, NULL, NULL);
1799
1800 /* Update the file's state */
1801 cp->c_flag |= C_DELETED;
1802 cp->c_ctime = tv.tv_sec;
1803 --cp->c_nlink;
1804 (void) cat_update(hfsmp, &to_desc, &cp->c_attr, NULL, NULL);
1805 }
1806 hfs_systemfile_unlock(hfsmp, lockflags);
1807 if (error)
1808 goto out;
1809
1810 } else /* Not busy */ {
1811
1812 if (cp->c_blocks > 0) {
1813 printf("hfs_remove: attempting to delete a non-empty file %s\n",
1814 cp->c_desc.cd_nameptr);
1815 error = EBUSY;
1816 goto out;
1817 }
1818
1819 lockflags = hfs_systemfile_lock(hfsmp, SFL_CATALOG | SFL_ATTRIBUTE, HFS_EXCLUSIVE_LOCK);
1820
1821 error = cat_delete(hfsmp, &desc, &cp->c_attr);
1822
1823 if (error && error != ENXIO && error != ENOENT && truncated) {
1824 if ((cp->c_datafork && cp->c_datafork->ff_size != 0) ||
1825 (cp->c_rsrcfork && cp->c_rsrcfork->ff_size != 0)) {
1826 panic("hfs: remove: couldn't delete a truncated file! (%d, data sz %lld; rsrc sz %lld)",
1827 error, cp->c_datafork->ff_size, cp->c_rsrcfork->ff_size);
1828 } else {
1829 printf("hfs: remove: strangely enough, deleting truncated file %s (%d) got err %d\n",
1830 cp->c_desc.cd_nameptr, cp->c_attr.ca_fileid, error);
1831 }
1832 }
1833 if (error == 0) {
1834 /* Delete any attributes, ignore errors */
1835 (void) hfs_removeallattr(hfsmp, cp->c_fileid);
1836
1837 /* Update the parent directory */
1838 if (dcp->c_entries > 0)
1839 dcp->c_entries--;
1840 if (dcp->c_nlink > 0)
1841 dcp->c_nlink--;
1842 dcp->c_ctime = tv.tv_sec;
1843 dcp->c_mtime = tv.tv_sec;
1844 (void) cat_update(hfsmp, &dcp->c_desc, &dcp->c_attr, NULL, NULL);
1845 }
1846 hfs_systemfile_unlock(hfsmp, lockflags);
1847 if (error)
1848 goto out;
1849
1850 #if QUOTA
1851 (void)hfs_chkiq(cp, -1, NOCRED, 0);
1852 #endif /* QUOTA */
1853
1854 cp->c_mode = 0;
1855 truncated = 0; // because the catalog entry is gone
1856 cp->c_flag |= C_NOEXISTS;
1857 cp->c_touch_chgtime = TRUE; /* XXX needed ? */
1858 --cp->c_nlink;
1859
1860 hfs_volupdate(hfsmp, VOL_RMFILE, (dcp->c_cnid == kHFSRootFolderID));
1861 }
1862
1863 /*
1864 * All done with this cnode's descriptor...
1865 *
1866 * Note: all future catalog calls for this cnode must be
1867 * by fileid only. This is OK for HFS (which doesn't have
1868 * file thread records) since HFS doesn't support hard
1869 * links or the removal of busy files.
1870 */
1871 cat_releasedesc(&cp->c_desc);
1872
1873 HFS_KNOTE(dvp, NOTE_WRITE);
1874
1875 out:
1876 if (got_cookie) {
1877 cat_postflight(hfsmp, &cookie, 0);
1878 }
1879
1880 /* Commit the truncation to the catalog record */
1881 if (truncated) {
1882 cp->c_flag |= C_FORCEUPDATE;
1883 cp->c_touch_chgtime = TRUE;
1884 cp->c_touch_modtime = TRUE;
1885 (void) hfs_update(vp, 0);
1886 }
1887
1888 if (started_tr) {
1889 hfs_end_transaction(hfsmp);
1890 }
1891
1892 HFS_KNOTE(vp, NOTE_DELETE);
1893 if (rvp) {
1894 HFS_KNOTE(rvp, NOTE_DELETE);
1895 /* Defer the vnode_put on rvp until the hfs_unlock(). */
1896 cp->c_flag |= C_NEED_RVNODE_PUT;
1897 };
1898
1899 return (error);
1900 }
1901
1902
1903 __private_extern__ void
1904 replace_desc(struct cnode *cp, struct cat_desc *cdp)
1905 {
1906 /* First release allocated name buffer */
1907 if (cp->c_desc.cd_flags & CD_HASBUF && cp->c_desc.cd_nameptr != 0) {
1908 char *name = cp->c_desc.cd_nameptr;
1909
1910 cp->c_desc.cd_nameptr = 0;
1911 cp->c_desc.cd_namelen = 0;
1912 cp->c_desc.cd_flags &= ~CD_HASBUF;
1913 vfs_removename(name);
1914 }
1915 bcopy(cdp, &cp->c_desc, sizeof(cp->c_desc));
1916
1917 /* Cnode now owns the name buffer */
1918 cdp->cd_nameptr = 0;
1919 cdp->cd_namelen = 0;
1920 cdp->cd_flags &= ~CD_HASBUF;
1921 }
1922
1923
1924 /*
1925 * Rename a cnode.
1926 *
1927 * The VFS layer guarantees that:
1928 * - source and destination will either both be directories, or
1929 * both not be directories.
1930 * - all the vnodes are from the same file system
1931 *
1932 * When the target is a directory, HFS must ensure that its empty.
1933 */
1934 static int
1935 hfs_vnop_rename(ap)
1936 struct vnop_rename_args /* {
1937 struct vnode *a_fdvp;
1938 struct vnode *a_fvp;
1939 struct componentname *a_fcnp;
1940 struct vnode *a_tdvp;
1941 struct vnode *a_tvp;
1942 struct componentname *a_tcnp;
1943 vfs_context_t a_context;
1944 } */ *ap;
1945 {
1946 struct vnode *tvp = ap->a_tvp;
1947 struct vnode *tdvp = ap->a_tdvp;
1948 struct vnode *fvp = ap->a_fvp;
1949 struct vnode *fdvp = ap->a_fdvp;
1950 struct componentname *tcnp = ap->a_tcnp;
1951 struct componentname *fcnp = ap->a_fcnp;
1952 struct proc *p = vfs_context_proc(ap->a_context);
1953 struct cnode *fcp;
1954 struct cnode *fdcp;
1955 struct cnode *tdcp;
1956 struct cnode *tcp;
1957 struct cat_desc from_desc;
1958 struct cat_desc to_desc;
1959 struct cat_desc out_desc;
1960 struct hfsmount *hfsmp;
1961 cat_cookie_t cookie;
1962 int tvp_deleted = 0;
1963 int started_tr = 0, got_cookie = 0;
1964 int took_trunc_lock = 0;
1965 int lockflags;
1966 int error;
1967
1968 /* When tvp exist, take the truncate lock for the hfs_removefile(). */
1969 if (tvp && vnode_isreg(tvp)) {
1970 hfs_lock_truncate(VTOC(tvp), TRUE);
1971 took_trunc_lock = 1;
1972 }
1973
1974 error = hfs_lockfour(VTOC(fdvp), VTOC(fvp), VTOC(tdvp), tvp ? VTOC(tvp) : NULL,
1975 HFS_EXCLUSIVE_LOCK);
1976 if (error) {
1977 if (took_trunc_lock)
1978 hfs_unlock_truncate(VTOC(tvp));
1979 return (error);
1980 }
1981
1982 fdcp = VTOC(fdvp);
1983 fcp = VTOC(fvp);
1984 tdcp = VTOC(tdvp);
1985 tcp = tvp ? VTOC(tvp) : NULL;
1986 hfsmp = VTOHFS(tdvp);
1987
1988 /* Check for a race against unlink. */
1989 if (fcp->c_flag & C_NOEXISTS) {
1990 error = ENOENT;
1991 goto out;
1992 }
1993
1994 /*
1995 * The following edge case is caught here:
1996 * (to cannot be a descendent of from)
1997 *
1998 * o fdvp
1999 * /
2000 * /
2001 * o fvp
2002 * \
2003 * \
2004 * o tdvp
2005 * /
2006 * /
2007 * o tvp
2008 */
2009 if (tdcp->c_parentcnid == fcp->c_cnid) {
2010 error = EINVAL;
2011 goto out;
2012 }
2013
2014 /*
2015 * The following two edge cases are caught here:
2016 * (note tvp is not empty)
2017 *
2018 * o tdvp o tdvp
2019 * / /
2020 * / /
2021 * o tvp tvp o fdvp
2022 * \ \
2023 * \ \
2024 * o fdvp o fvp
2025 * /
2026 * /
2027 * o fvp
2028 */
2029 if (tvp && vnode_isdir(tvp) && (tcp->c_entries != 0) && fvp != tvp) {
2030 error = ENOTEMPTY;
2031 goto out;
2032 }
2033
2034 /*
2035 * The following edge case is caught here:
2036 * (the from child and parent are the same)
2037 *
2038 * o tdvp
2039 * /
2040 * /
2041 * fdvp o fvp
2042 */
2043 if (fdvp == fvp) {
2044 error = EINVAL;
2045 goto out;
2046 }
2047
2048 /*
2049 * Make sure "from" vnode and its parent are changeable.
2050 */
2051 if ((fcp->c_flags & (IMMUTABLE | APPEND)) || (fdcp->c_flags & APPEND)) {
2052 error = EPERM;
2053 goto out;
2054 }
2055
2056 /*
2057 * If the destination parent directory is "sticky", then the
2058 * user must own the parent directory, or the destination of
2059 * the rename, otherwise the destination may not be changed
2060 * (except by root). This implements append-only directories.
2061 *
2062 * Note that checks for immutable and write access are done
2063 * by the call to hfs_removefile.
2064 */
2065 if (tvp && (tdcp->c_mode & S_ISTXT) &&
2066 (suser(vfs_context_ucred(tcnp->cn_context), NULL)) &&
2067 (kauth_cred_getuid(vfs_context_ucred(tcnp->cn_context)) != tdcp->c_uid) &&
2068 (hfs_owner_rights(hfsmp, tcp->c_uid, vfs_context_ucred(tcnp->cn_context), p, false)) ) {
2069 error = EPERM;
2070 goto out;
2071 }
2072
2073 #if QUOTA
2074 if (tvp)
2075 (void)hfs_getinoquota(tcp);
2076 #endif
2077 /* Preflighting done, take fvp out of the name space. */
2078 cache_purge(fvp);
2079
2080 /*
2081 * When a file moves out of "Cleanup At Startup"
2082 * we can drop its NODUMP status.
2083 */
2084 if ((fcp->c_flags & UF_NODUMP) &&
2085 vnode_isreg(fvp) &&
2086 (fdvp != tdvp) &&
2087 (fdcp->c_desc.cd_nameptr != NULL) &&
2088 (strcmp(fdcp->c_desc.cd_nameptr, CARBON_TEMP_DIR_NAME) == 0)) {
2089 fcp->c_flags &= ~UF_NODUMP;
2090 fcp->c_touch_chgtime = TRUE;
2091 (void) hfs_update(fvp, 0);
2092 }
2093
2094 bzero(&from_desc, sizeof(from_desc));
2095 from_desc.cd_nameptr = fcnp->cn_nameptr;
2096 from_desc.cd_namelen = fcnp->cn_namelen;
2097 from_desc.cd_parentcnid = fdcp->c_cnid;
2098 from_desc.cd_flags = fcp->c_desc.cd_flags & ~(CD_HASBUF | CD_DECOMPOSED);
2099 from_desc.cd_cnid = fcp->c_cnid;
2100
2101 bzero(&to_desc, sizeof(to_desc));
2102 to_desc.cd_nameptr = tcnp->cn_nameptr;
2103 to_desc.cd_namelen = tcnp->cn_namelen;
2104 to_desc.cd_parentcnid = tdcp->c_cnid;
2105 to_desc.cd_flags = fcp->c_desc.cd_flags & ~(CD_HASBUF | CD_DECOMPOSED);
2106 to_desc.cd_cnid = fcp->c_cnid;
2107
2108 if ((error = hfs_start_transaction(hfsmp)) != 0) {
2109 goto out;
2110 }
2111 started_tr = 1;
2112
2113 // if it's a hardlink then re-lookup the name so
2114 // that we get the correct cnid in from_desc (see
2115 // the comment in hfs_removefile for more details)
2116 //
2117 if (fcp->c_flag & C_HARDLINK) {
2118 struct cat_desc tmpdesc;
2119 cnid_t real_cnid;
2120
2121 bzero(&tmpdesc, sizeof(tmpdesc));
2122 tmpdesc.cd_nameptr = fcnp->cn_nameptr;
2123 tmpdesc.cd_namelen = fcnp->cn_namelen;
2124 tmpdesc.cd_parentcnid = fdcp->c_cnid;
2125 tmpdesc.cd_hint = fdcp->c_childhint;
2126
2127 lockflags = hfs_systemfile_lock(hfsmp, SFL_CATALOG, HFS_SHARED_LOCK);
2128
2129 if (cat_lookup(hfsmp, &tmpdesc, 0, NULL, NULL, NULL, &real_cnid) != 0) {
2130 hfs_systemfile_unlock(hfsmp, lockflags);
2131 goto out;
2132 }
2133
2134 // use the real cnid instead of whatever happened to be there
2135 from_desc.cd_cnid = real_cnid;
2136 hfs_systemfile_unlock(hfsmp, lockflags);
2137 }
2138
2139 /*
2140 * Reserve some space in the Catalog file.
2141 */
2142 bzero(&cookie, sizeof(cookie));
2143 if ((error = cat_preflight(hfsmp, CAT_RENAME + CAT_DELETE, &cookie, p))) {
2144 goto out;
2145 }
2146 got_cookie = 1;
2147
2148 /*
2149 * If the destination exists then it may need to be removed.
2150 */
2151 if (tvp) {
2152 /*
2153 * When fvp matches tvp they must be case variants
2154 * or hard links.
2155 */
2156 if (fvp == tvp) {
2157 /*
2158 * If this a hard link with different parents
2159 * and its not a case variant then tvp should
2160 * be removed.
2161 */
2162 if (!((fcp->c_flag & C_HARDLINK) &&
2163 ((fdvp != tdvp) ||
2164 (hfs_namecmp(fcnp->cn_nameptr, fcnp->cn_namelen,
2165 tcnp->cn_nameptr, tcnp->cn_namelen) != 0)))) {
2166 goto skip;
2167 }
2168 } else {
2169 cache_purge(tvp);
2170 }
2171
2172 if (vnode_isdir(tvp))
2173 error = hfs_removedir(tdvp, tvp, tcnp, HFSRM_SKIP_RESERVE);
2174 else {
2175 error = hfs_removefile(tdvp, tvp, tcnp, 0, HFSRM_SKIP_RESERVE);
2176 }
2177
2178 if (error)
2179 goto out;
2180 tvp_deleted = 1;
2181 }
2182 skip:
2183 /*
2184 * All done with tvp and fvp
2185 */
2186
2187 lockflags = hfs_systemfile_lock(hfsmp, SFL_CATALOG, HFS_EXCLUSIVE_LOCK);
2188 error = cat_rename(hfsmp, &from_desc, &tdcp->c_desc, &to_desc, &out_desc);
2189 hfs_systemfile_unlock(hfsmp, lockflags);
2190
2191 if (error) {
2192 goto out;
2193 }
2194
2195 /* Invalidate negative cache entries in the destination directory */
2196 if (hfsmp->hfs_flags & HFS_CASE_SENSITIVE)
2197 cache_purge_negatives(tdvp);
2198
2199 /* Update cnode's catalog descriptor */
2200 replace_desc(fcp, &out_desc);
2201 fcp->c_parentcnid = tdcp->c_cnid;
2202 fcp->c_hint = 0;
2203
2204 hfs_volupdate(hfsmp, vnode_isdir(fvp) ? VOL_RMDIR : VOL_RMFILE,
2205 (fdcp->c_cnid == kHFSRootFolderID));
2206 hfs_volupdate(hfsmp, vnode_isdir(fvp) ? VOL_MKDIR : VOL_MKFILE,
2207 (tdcp->c_cnid == kHFSRootFolderID));
2208
2209 /* Update both parent directories. */
2210 if (fdvp != tdvp) {
2211 tdcp->c_nlink++;
2212 tdcp->c_entries++;
2213 if (fdcp->c_nlink > 0)
2214 fdcp->c_nlink--;
2215 if (fdcp->c_entries > 0)
2216 fdcp->c_entries--;
2217 fdcp->c_touch_chgtime = TRUE;
2218 fdcp->c_touch_modtime = TRUE;
2219
2220 fdcp->c_flag |= C_FORCEUPDATE; // XXXdbg - force it out!
2221 (void) hfs_update(fdvp, 0);
2222 }
2223 tdcp->c_childhint = out_desc.cd_hint; /* Cache directory's location */
2224 tdcp->c_touch_chgtime = TRUE;
2225 tdcp->c_touch_modtime = TRUE;
2226
2227 tdcp->c_flag |= C_FORCEUPDATE; // XXXdbg - force it out!
2228 (void) hfs_update(tdvp, 0);
2229 out:
2230 if (got_cookie) {
2231 cat_postflight(hfsmp, &cookie, p);
2232 }
2233 if (started_tr) {
2234 hfs_end_transaction(hfsmp);
2235 }
2236
2237 /* Note that if hfs_removedir or hfs_removefile was invoked above they will already have
2238 generated a NOTE_WRITE for tdvp and a NOTE_DELETE for tvp.
2239 */
2240 if (error == 0) {
2241 HFS_KNOTE(fvp, NOTE_RENAME);
2242 HFS_KNOTE(fdvp, NOTE_WRITE);
2243 if (tdvp != fdvp) HFS_KNOTE(tdvp, NOTE_WRITE);
2244 };
2245
2246 if (took_trunc_lock)
2247 hfs_unlock_truncate(VTOC(tvp));
2248
2249 hfs_unlockfour(fdcp, fcp, tdcp, tcp);
2250
2251 /* After tvp is removed the only acceptable error is EIO */
2252 if (error && tvp_deleted)
2253 error = EIO;
2254
2255 return (error);
2256 }
2257
2258
2259 /*
2260 * Make a directory.
2261 */
2262 static int
2263 hfs_vnop_mkdir(struct vnop_mkdir_args *ap)
2264 {
2265 /***** HACK ALERT ********/
2266 ap->a_cnp->cn_flags |= MAKEENTRY;
2267 return hfs_makenode(ap->a_dvp, ap->a_vpp, ap->a_cnp, ap->a_vap, ap->a_context);
2268 }
2269
2270
2271 /*
2272 * Create a symbolic link.
2273 */
2274 static int
2275 hfs_vnop_symlink(struct vnop_symlink_args *ap)
2276 {
2277 struct vnode **vpp = ap->a_vpp;
2278 struct vnode *dvp = ap->a_dvp;
2279 struct vnode *vp = NULL;
2280 struct hfsmount *hfsmp;
2281 struct filefork *fp;
2282 struct buf *bp = NULL;
2283 char *datap;
2284 int started_tr = 0;
2285 int len, error;
2286
2287 /* HFS standard disks don't support symbolic links */
2288 if (VTOVCB(dvp)->vcbSigWord != kHFSPlusSigWord)
2289 return (ENOTSUP);
2290
2291 /* Check for empty target name */
2292 if (ap->a_target[0] == 0)
2293 return (EINVAL);
2294
2295 /* Create the vnode */
2296 ap->a_vap->va_mode |= S_IFLNK;
2297 if ((error = hfs_makenode(dvp, vpp, ap->a_cnp, ap->a_vap, ap->a_context))) {
2298 goto out;
2299 }
2300 vp = *vpp;
2301 if ((error = hfs_lock(VTOC(vp), HFS_EXCLUSIVE_LOCK)))
2302 return (error);
2303 fp = VTOF(vp);
2304 hfsmp = VTOHFS(dvp);
2305 len = strlen(ap->a_target);
2306
2307 #if QUOTA
2308 (void)hfs_getinoquota(VTOC(vp));
2309 #endif /* QUOTA */
2310
2311 if ((error = hfs_start_transaction(hfsmp)) != 0) {
2312 goto out;
2313 }
2314 started_tr = 1;
2315
2316 /*
2317 * Allocate space for the link.
2318 *
2319 * Since we're already inside a transaction,
2320 * tell hfs_truncate to skip the ubc_setsize.
2321 *
2322 * Don't need truncate lock since a symlink is treated as a system file.
2323 */
2324 error = hfs_truncate(vp, len, IO_NOZEROFILL, 1, ap->a_context);
2325 if (error)
2326 goto out; /* XXX need to remove link */
2327
2328 /* Write the link to disk */
2329 bp = buf_getblk(vp, (daddr64_t)0, roundup((int)fp->ff_size, VTOHFS(vp)->hfs_phys_block_size),
2330 0, 0, BLK_META);
2331 if (hfsmp->jnl) {
2332 journal_modify_block_start(hfsmp->jnl, bp);
2333 }
2334 datap = (char *)buf_dataptr(bp);
2335 bzero(datap, buf_size(bp));
2336 bcopy(ap->a_target, datap, len);
2337
2338 if (hfsmp->jnl) {
2339 journal_modify_block_end(hfsmp->jnl, bp);
2340 } else {
2341 buf_bawrite(bp);
2342 }
2343 /*
2344 * We defered the ubc_setsize for hfs_truncate
2345 * since we were inside a transaction.
2346 *
2347 * We don't need to drop the cnode lock here
2348 * since this is a symlink.
2349 */
2350 ubc_setsize(vp, len);
2351 out:
2352 if (started_tr)
2353 hfs_end_transaction(hfsmp);
2354 if (vp) {
2355 hfs_unlock(VTOC(vp));
2356 }
2357 return (error);
2358 }
2359
2360
2361 /* structures to hold a "." or ".." directory entry */
2362 struct hfs_stddotentry {
2363 u_int32_t d_fileno; /* unique file number */
2364 u_int16_t d_reclen; /* length of this structure */
2365 u_int8_t d_type; /* dirent file type */
2366 u_int8_t d_namlen; /* len of filename */
2367 char d_name[4]; /* "." or ".." */
2368 };
2369
2370 struct hfs_extdotentry {
2371 u_int64_t d_fileno; /* unique file number */
2372 u_int64_t d_seekoff; /* seek offset (optional, used by servers) */
2373 u_int16_t d_reclen; /* length of this structure */
2374 u_int16_t d_namlen; /* len of filename */
2375 u_int8_t d_type; /* dirent file type */
2376 u_char d_name[3]; /* "." or ".." */
2377 };
2378
2379 typedef union {
2380 struct hfs_stddotentry std;
2381 struct hfs_extdotentry ext;
2382 } hfs_dotentry_t;
2383
2384 /*
2385 * hfs_vnop_readdir reads directory entries into the buffer pointed
2386 * to by uio, in a filesystem independent format. Up to uio_resid
2387 * bytes of data can be transferred. The data in the buffer is a
2388 * series of packed dirent structures where each one contains the
2389 * following entries:
2390 *
2391 * u_int32_t d_fileno; // file number of entry
2392 * u_int16_t d_reclen; // length of this record
2393 * u_int8_t d_type; // file type
2394 * u_int8_t d_namlen; // length of string in d_name
2395 * char d_name[MAXNAMELEN+1]; // null terminated file name
2396 *
2397 * The current position (uio_offset) refers to the next block of
2398 * entries. The offset can only be set to a value previously
2399 * returned by hfs_vnop_readdir or zero. This offset does not have
2400 * to match the number of bytes returned (in uio_resid).
2401 *
2402 * In fact, the offset used by HFS is essentially an index (26 bits)
2403 * with a tag (6 bits). The tag is for associating the next request
2404 * with the current request. This enables us to have multiple threads
2405 * reading the directory while the directory is also being modified.
2406 *
2407 * Each tag/index pair is tied to a unique directory hint. The hint
2408 * contains information (filename) needed to build the catalog b-tree
2409 * key for finding the next set of entries.
2410 */
2411 static int
2412 hfs_vnop_readdir(ap)
2413 struct vnop_readdir_args /* {
2414 vnode_t a_vp;
2415 uio_t a_uio;
2416 int a_flags;
2417 int *a_eofflag;
2418 int *a_numdirent;
2419 vfs_context_t a_context;
2420 } */ *ap;
2421 {
2422 struct vnode *vp = ap->a_vp;
2423 uio_t uio = ap->a_uio;
2424 struct cnode *cp;
2425 struct hfsmount *hfsmp;
2426 directoryhint_t *dirhint = NULL;
2427 directoryhint_t localhint;
2428 off_t offset;
2429 off_t startoffset;
2430 int error = 0;
2431 int eofflag = 0;
2432 user_addr_t user_start = 0;
2433 user_size_t user_len = 0;
2434 int index;
2435 unsigned int tag;
2436 int items;
2437 int lockflags;
2438 int extended;
2439 int nfs_cookies;
2440 caddr_t bufstart;
2441 cnid_t cnid_hint = 0;
2442
2443 items = 0;
2444 startoffset = offset = uio_offset(uio);
2445 bufstart = CAST_DOWN(caddr_t, uio_iov_base(uio));
2446 extended = (ap->a_flags & VNODE_READDIR_EXTENDED);
2447 nfs_cookies = extended && (ap->a_flags & VNODE_READDIR_REQSEEKOFF);
2448
2449 /* Sanity check the uio data. */
2450 if ((uio_iovcnt(uio) > 1) ||
2451 (uio_resid(uio) < (int)sizeof(struct dirent))) {
2452 return (EINVAL);
2453 }
2454 /* Note that the dirhint calls require an exclusive lock. */
2455 if ((error = hfs_lock(VTOC(vp), HFS_EXCLUSIVE_LOCK)))
2456 return (error);
2457 cp = VTOC(vp);
2458 hfsmp = VTOHFS(vp);
2459
2460 /* Pick up cnid hint (if any). */
2461 if (nfs_cookies) {
2462 cnid_hint = (cnid_t)(uio_offset(uio) >> 32);
2463 uio_setoffset(uio, uio_offset(uio) & 0x00000000ffffffffLL);
2464 }
2465 /*
2466 * Synthesize entries for "." and ".."
2467 */
2468 if (offset == 0) {
2469 hfs_dotentry_t dotentry[2];
2470 size_t uiosize;
2471
2472 if (extended) {
2473 struct hfs_extdotentry *entry = &dotentry[0].ext;
2474
2475 entry->d_fileno = cp->c_cnid;
2476 entry->d_reclen = sizeof(struct hfs_extdotentry);
2477 entry->d_type = DT_DIR;
2478 entry->d_namlen = 1;
2479 entry->d_name[0] = '.';
2480 entry->d_name[1] = '\0';
2481 entry->d_name[2] = '\0';
2482 entry->d_seekoff = 1;
2483
2484 ++entry;
2485 entry->d_fileno = cp->c_parentcnid;
2486 entry->d_reclen = sizeof(struct hfs_extdotentry);
2487 entry->d_type = DT_DIR;
2488 entry->d_namlen = 2;
2489 entry->d_name[0] = '.';
2490 entry->d_name[1] = '.';
2491 entry->d_name[2] = '\0';
2492 entry->d_seekoff = 2;
2493 uiosize = 2 * sizeof(struct hfs_extdotentry);
2494 } else {
2495 struct hfs_stddotentry *entry = &dotentry[0].std;
2496
2497 entry->d_fileno = cp->c_cnid;
2498 entry->d_reclen = sizeof(struct hfs_stddotentry);
2499 entry->d_type = DT_DIR;
2500 entry->d_namlen = 1;
2501 *(int *)&entry->d_name[0] = 0;
2502 entry->d_name[0] = '.';
2503
2504 ++entry;
2505 entry->d_fileno = cp->c_parentcnid;
2506 entry->d_reclen = sizeof(struct hfs_stddotentry);
2507 entry->d_type = DT_DIR;
2508 entry->d_namlen = 2;
2509 *(int *)&entry->d_name[0] = 0;
2510 entry->d_name[0] = '.';
2511 entry->d_name[1] = '.';
2512 uiosize = 2 * sizeof(struct hfs_stddotentry);
2513 }
2514 if ((error = uiomove((caddr_t)&dotentry, uiosize, uio))) {
2515 goto out;
2516 }
2517 offset += 2;
2518 }
2519
2520 /* If there are no real entries then we're done. */
2521 if (cp->c_entries == 0) {
2522 error = 0;
2523 eofflag = 1;
2524 uio_setoffset(uio, offset);
2525 goto seekoffcalc;
2526 }
2527
2528 //
2529 // We have to lock the user's buffer here so that we won't
2530 // fault on it after we've acquired a shared lock on the
2531 // catalog file. The issue is that you can get a 3-way
2532 // deadlock if someone else starts a transaction and then
2533 // tries to lock the catalog file but can't because we're
2534 // here and we can't service our page fault because VM is
2535 // blocked trying to start a transaction as a result of
2536 // trying to free up pages for our page fault. It's messy
2537 // but it does happen on dual-procesors that are paging
2538 // heavily (see radar 3082639 for more info). By locking
2539 // the buffer up-front we prevent ourselves from faulting
2540 // while holding the shared catalog file lock.
2541 //
2542 // Fortunately this and hfs_search() are the only two places
2543 // currently (10/30/02) that can fault on user data with a
2544 // shared lock on the catalog file.
2545 //
2546 if (hfsmp->jnl && uio_isuserspace(uio)) {
2547 user_start = uio_curriovbase(uio);
2548 user_len = uio_curriovlen(uio);
2549
2550 if ((error = vslock(user_start, user_len)) != 0) {
2551 user_start = 0;
2552 goto out;
2553 }
2554 }
2555 /* Convert offset into a catalog directory index. */
2556 index = (offset & HFS_INDEX_MASK) - 2;
2557 tag = offset & ~HFS_INDEX_MASK;
2558
2559 /* Lock catalog during cat_findname and cat_getdirentries. */
2560 lockflags = hfs_systemfile_lock(hfsmp, SFL_CATALOG, HFS_SHARED_LOCK);
2561
2562 /* When called from NFS, try and resolve a cnid hint. */
2563 if (nfs_cookies && cnid_hint != 0) {
2564 if (cat_findname(hfsmp, cnid_hint, &localhint.dh_desc) == 0) {
2565 if ( localhint.dh_desc.cd_parentcnid == cp->c_cnid) {
2566 localhint.dh_index = index - 1;
2567 localhint.dh_time = 0;
2568 localhint.dh_link.sle_next = 0;
2569 dirhint = &localhint; /* don't forget to release the descriptor */
2570 } else {
2571 cat_releasedesc(&localhint.dh_desc);
2572 }
2573 }
2574 }
2575
2576 /* Get a directory hint (cnode must be locked exclusive) */
2577 if (dirhint == NULL) {
2578 dirhint = hfs_getdirhint(cp, ((index - 1) & HFS_INDEX_MASK) | tag);
2579
2580 /* Hide tag from catalog layer. */
2581 dirhint->dh_index &= HFS_INDEX_MASK;
2582 if (dirhint->dh_index == HFS_INDEX_MASK) {
2583 dirhint->dh_index = -1;
2584 }
2585 }
2586
2587 /* Pack the buffer with dirent entries. */
2588 error = cat_getdirentries(hfsmp, cp->c_entries, dirhint, uio, extended, &items);
2589
2590 hfs_systemfile_unlock(hfsmp, lockflags);
2591
2592 if (error != 0) {
2593 goto out;
2594 }
2595
2596 /* Get index to the next item */
2597 index += items;
2598
2599 if (items >= (int)cp->c_entries) {
2600 eofflag = 1;
2601 }
2602
2603 /* Convert catalog directory index back into an offset. */
2604 while (tag == 0)
2605 tag = (++cp->c_dirhinttag) << HFS_INDEX_BITS;
2606 uio_setoffset(uio, (index + 2) | tag);
2607 dirhint->dh_index |= tag;
2608
2609 seekoffcalc:
2610 cp->c_touch_acctime = TRUE;
2611
2612 if (ap->a_numdirent) {
2613 if (startoffset == 0)
2614 items += 2;
2615 *ap->a_numdirent = items;
2616 }
2617
2618 out:
2619 if (hfsmp->jnl && user_start) {
2620 vsunlock(user_start, user_len, TRUE);
2621 }
2622 /* If we didn't do anything then go ahead and dump the hint. */
2623 if ((dirhint != NULL) &&
2624 (dirhint != &localhint) &&
2625 (uio_offset(uio) == startoffset)) {
2626 hfs_reldirhint(cp, dirhint);
2627 eofflag = 1;
2628 }
2629 if (ap->a_eofflag) {
2630 *ap->a_eofflag = eofflag;
2631 }
2632 if (dirhint == &localhint) {
2633 cat_releasedesc(&localhint.dh_desc);
2634 }
2635 hfs_unlock(cp);
2636 return (error);
2637 }
2638
2639
2640 /*
2641 * Read contents of a symbolic link.
2642 */
2643 static int
2644 hfs_vnop_readlink(ap)
2645 struct vnop_readlink_args /* {
2646 struct vnode *a_vp;
2647 struct uio *a_uio;
2648 vfs_context_t a_context;
2649 } */ *ap;
2650 {
2651 struct vnode *vp = ap->a_vp;
2652 struct cnode *cp;
2653 struct filefork *fp;
2654 int error;
2655
2656 if (!vnode_islnk(vp))
2657 return (EINVAL);
2658
2659 if ((error = hfs_lock(VTOC(vp), HFS_EXCLUSIVE_LOCK)))
2660 return (error);
2661 cp = VTOC(vp);
2662 fp = VTOF(vp);
2663
2664 /* Zero length sym links are not allowed */
2665 if (fp->ff_size == 0 || fp->ff_size > MAXPATHLEN) {
2666 VTOVCB(vp)->vcbFlags |= kHFS_DamagedVolume;
2667 error = EINVAL;
2668 goto exit;
2669 }
2670
2671 /* Cache the path so we don't waste buffer cache resources */
2672 if (fp->ff_symlinkptr == NULL) {
2673 struct buf *bp = NULL;
2674
2675 MALLOC(fp->ff_symlinkptr, char *, fp->ff_size, M_TEMP, M_WAITOK);
2676 error = (int)buf_meta_bread(vp, (daddr64_t)0,
2677 roundup((int)fp->ff_size,
2678 VTOHFS(vp)->hfs_phys_block_size),
2679 vfs_context_ucred(ap->a_context), &bp);
2680 if (error) {
2681 if (bp)
2682 buf_brelse(bp);
2683 if (fp->ff_symlinkptr) {
2684 FREE(fp->ff_symlinkptr, M_TEMP);
2685 fp->ff_symlinkptr = NULL;
2686 }
2687 goto exit;
2688 }
2689 bcopy((char *)buf_dataptr(bp), fp->ff_symlinkptr, (size_t)fp->ff_size);
2690
2691 if (VTOHFS(vp)->jnl && (buf_flags(bp) & B_LOCKED) == 0) {
2692 buf_markinvalid(bp); /* data no longer needed */
2693 }
2694 buf_brelse(bp);
2695 }
2696 error = uiomove((caddr_t)fp->ff_symlinkptr, (int)fp->ff_size, ap->a_uio);
2697
2698 /*
2699 * Keep track blocks read
2700 */
2701 if ((VTOHFS(vp)->hfc_stage == HFC_RECORDING) && (error == 0)) {
2702
2703 /*
2704 * If this file hasn't been seen since the start of
2705 * the current sampling period then start over.
2706 */
2707 if (cp->c_atime < VTOHFS(vp)->hfc_timebase)
2708 VTOF(vp)->ff_bytesread = fp->ff_size;
2709 else
2710 VTOF(vp)->ff_bytesread += fp->ff_size;
2711
2712 // if (VTOF(vp)->ff_bytesread > fp->ff_size)
2713 // cp->c_touch_acctime = TRUE;
2714 }
2715
2716 exit:
2717 hfs_unlock(cp);
2718 return (error);
2719 }
2720
2721
2722 /*
2723 * Get configurable pathname variables.
2724 */
2725 static int
2726 hfs_vnop_pathconf(ap)
2727 struct vnop_pathconf_args /* {
2728 struct vnode *a_vp;
2729 int a_name;
2730 int *a_retval;
2731 vfs_context_t a_context;
2732 } */ *ap;
2733 {
2734 switch (ap->a_name) {
2735 case _PC_LINK_MAX:
2736 if (VTOHFS(ap->a_vp)->hfs_flags & HFS_STANDARD)
2737 *ap->a_retval = 1;
2738 else
2739 *ap->a_retval = HFS_LINK_MAX;
2740 break;
2741 case _PC_NAME_MAX:
2742 if (VTOHFS(ap->a_vp)->hfs_flags & HFS_STANDARD)
2743 *ap->a_retval = kHFSMaxFileNameChars; /* 255 */
2744 else
2745 *ap->a_retval = kHFSPlusMaxFileNameChars; /* 31 */
2746 break;
2747 case _PC_PATH_MAX:
2748 *ap->a_retval = PATH_MAX; /* 1024 */
2749 break;
2750 case _PC_PIPE_BUF:
2751 *ap->a_retval = PIPE_BUF;
2752 break;
2753 case _PC_CHOWN_RESTRICTED:
2754 *ap->a_retval = 1;
2755 break;
2756 case _PC_NO_TRUNC:
2757 *ap->a_retval = 0;
2758 break;
2759 case _PC_NAME_CHARS_MAX:
2760 *ap->a_retval = kHFSPlusMaxFileNameChars;
2761 break;
2762 case _PC_CASE_SENSITIVE:
2763 if (VTOHFS(ap->a_vp)->hfs_flags & HFS_CASE_SENSITIVE)
2764 *ap->a_retval = 1;
2765 else
2766 *ap->a_retval = 0;
2767 break;
2768 case _PC_CASE_PRESERVING:
2769 *ap->a_retval = 1;
2770 break;
2771 default:
2772 return (EINVAL);
2773 }
2774
2775 return (0);
2776 }
2777
2778
2779 /*
2780 * Update a cnode's on-disk metadata.
2781 *
2782 * If waitfor is set, then wait for the disk write of
2783 * the node to complete.
2784 *
2785 * The cnode must be locked exclusive
2786 */
2787 __private_extern__
2788 int
2789 hfs_update(struct vnode *vp, __unused int waitfor)
2790 {
2791 struct cnode *cp = VTOC(vp);
2792 struct proc *p;
2793 struct cat_fork *dataforkp = NULL;
2794 struct cat_fork *rsrcforkp = NULL;
2795 struct cat_fork datafork;
2796 struct hfsmount *hfsmp;
2797 int lockflags;
2798 int error;
2799
2800 p = current_proc();
2801 hfsmp = VTOHFS(vp);
2802
2803 if (vnode_issystem(vp) && (cp->c_cnid < kHFSFirstUserCatalogNodeID)) {
2804 return (0);
2805 }
2806 if ((hfsmp->hfs_flags & HFS_READ_ONLY) || (cp->c_mode == 0)) {
2807 cp->c_flag &= ~C_MODIFIED;
2808 cp->c_touch_acctime = 0;
2809 cp->c_touch_chgtime = 0;
2810 cp->c_touch_modtime = 0;
2811 return (0);
2812 }
2813
2814 hfs_touchtimes(hfsmp, cp);
2815
2816 /* Nothing to update. */
2817 if ((cp->c_flag & (C_MODIFIED | C_FORCEUPDATE)) == 0) {
2818 return (0);
2819 }
2820
2821 if (cp->c_datafork)
2822 dataforkp = &cp->c_datafork->ff_data;
2823 if (cp->c_rsrcfork)
2824 rsrcforkp = &cp->c_rsrcfork->ff_data;
2825
2826 /*
2827 * For delayed allocations updates are
2828 * postponed until an fsync or the file
2829 * gets written to disk.
2830 *
2831 * Deleted files can defer meta data updates until inactive.
2832 *
2833 * If we're ever called with the C_FORCEUPDATE flag though
2834 * we have to do the update.
2835 */
2836 if (ISSET(cp->c_flag, C_FORCEUPDATE) == 0 &&
2837 (ISSET(cp->c_flag, C_DELETED) ||
2838 (dataforkp && cp->c_datafork->ff_unallocblocks) ||
2839 (rsrcforkp && cp->c_rsrcfork->ff_unallocblocks))) {
2840 // cp->c_flag &= ~(C_ACCESS | C_CHANGE | C_UPDATE);
2841 cp->c_flag |= C_MODIFIED;
2842
2843 HFS_KNOTE(vp, NOTE_ATTRIB);
2844
2845 return (0);
2846 }
2847
2848 if ((error = hfs_start_transaction(hfsmp)) != 0) {
2849 return error;
2850 }
2851
2852 /*
2853 * For files with invalid ranges (holes) the on-disk
2854 * field representing the size of the file (cf_size)
2855 * must be no larger than the start of the first hole.
2856 */
2857 if (dataforkp && !CIRCLEQ_EMPTY(&cp->c_datafork->ff_invalidranges)) {
2858 bcopy(dataforkp, &datafork, sizeof(datafork));
2859 datafork.cf_size = CIRCLEQ_FIRST(&cp->c_datafork->ff_invalidranges)->rl_start;
2860 dataforkp = &datafork;
2861 } else if (dataforkp && (cp->c_datafork->ff_unallocblocks != 0)) {
2862 // always make sure the block count and the size
2863 // of the file match the number of blocks actually
2864 // allocated to the file on disk
2865 bcopy(dataforkp, &datafork, sizeof(datafork));
2866 // make sure that we don't assign a negative block count
2867 if (cp->c_datafork->ff_blocks < cp->c_datafork->ff_unallocblocks) {
2868 panic("hfs: ff_blocks %d is less than unalloc blocks %d\n",
2869 cp->c_datafork->ff_blocks, cp->c_datafork->ff_unallocblocks);
2870 }
2871 datafork.cf_blocks = (cp->c_datafork->ff_blocks - cp->c_datafork->ff_unallocblocks);
2872 datafork.cf_size = datafork.cf_blocks * HFSTOVCB(hfsmp)->blockSize;
2873 dataforkp = &datafork;
2874 }
2875
2876 /*
2877 * Lock the Catalog b-tree file.
2878 * A shared lock is sufficient since an update doesn't change
2879 * the tree and the lock on vp protects the cnode.
2880 */
2881 lockflags = hfs_systemfile_lock(hfsmp, SFL_CATALOG, HFS_SHARED_LOCK);
2882
2883 /* XXX - waitfor is not enforced */
2884 error = cat_update(hfsmp, &cp->c_desc, &cp->c_attr, dataforkp, rsrcforkp);
2885
2886 hfs_systemfile_unlock(hfsmp, lockflags);
2887
2888 /* After the updates are finished, clear the flags */
2889 cp->c_flag &= ~(C_MODIFIED | C_FORCEUPDATE);
2890
2891 hfs_end_transaction(hfsmp);
2892
2893 HFS_KNOTE(vp, NOTE_ATTRIB);
2894
2895 return (error);
2896 }
2897
2898 /*
2899 * Allocate a new node
2900 */
2901 static int
2902 hfs_makenode(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
2903 struct vnode_attr *vap, vfs_context_t ctx)
2904 {
2905 struct cnode *cp = NULL;
2906 struct cnode *dcp;
2907 struct vnode *tvp;
2908 struct hfsmount *hfsmp;
2909 struct cat_desc in_desc, out_desc;
2910 struct cat_attr attr;
2911 struct timeval tv;
2912 cat_cookie_t cookie;
2913 int lockflags;
2914 int error, started_tr = 0, got_cookie = 0;
2915 enum vtype vnodetype;
2916 int mode;
2917
2918 if ((error = hfs_lock(VTOC(dvp), HFS_EXCLUSIVE_LOCK)))
2919 return (error);
2920 dcp = VTOC(dvp);
2921 hfsmp = VTOHFS(dvp);
2922 *vpp = NULL;
2923 tvp = NULL;
2924 out_desc.cd_flags = 0;
2925 out_desc.cd_nameptr = NULL;
2926
2927 mode = MAKEIMODE(vap->va_type, vap->va_mode);
2928
2929 if ((mode & S_IFMT) == 0)
2930 mode |= S_IFREG;
2931 vnodetype = IFTOVT(mode);
2932
2933 /* Check if were out of usable disk space. */
2934 if ((hfs_freeblks(hfsmp, 1) <= 0) && (suser(vfs_context_ucred(ctx), NULL) != 0)) {
2935 error = ENOSPC;
2936 goto exit;
2937 }
2938
2939 microtime(&tv);
2940
2941 /* Setup the default attributes */
2942 bzero(&attr, sizeof(attr));
2943 attr.ca_mode = mode;
2944 attr.ca_nlink = vnodetype == VDIR ? 2 : 1;
2945 attr.ca_mtime = tv.tv_sec;
2946 if ((VTOVCB(dvp)->vcbSigWord == kHFSSigWord) && gTimeZone.tz_dsttime) {
2947 attr.ca_mtime += 3600; /* Same as what hfs_update does */
2948 }
2949 attr.ca_atime = attr.ca_ctime = attr.ca_itime = attr.ca_mtime;
2950 attr.ca_atimeondisk = attr.ca_atime;
2951
2952 attr.ca_uid = vap->va_uid;
2953 attr.ca_gid = vap->va_gid;
2954 VATTR_SET_SUPPORTED(vap, va_mode);
2955 VATTR_SET_SUPPORTED(vap, va_uid);
2956 VATTR_SET_SUPPORTED(vap, va_gid);
2957
2958 /* Tag symlinks with a type and creator. */
2959 if (vnodetype == VLNK) {
2960 struct FndrFileInfo *fip;
2961
2962 fip = (struct FndrFileInfo *)&attr.ca_finderinfo;
2963 fip->fdType = SWAP_BE32(kSymLinkFileType);
2964 fip->fdCreator = SWAP_BE32(kSymLinkCreator);
2965 }
2966 if (cnp->cn_flags & ISWHITEOUT)
2967 attr.ca_flags |= UF_OPAQUE;
2968
2969 /* Setup the descriptor */
2970 in_desc.cd_nameptr = cnp->cn_nameptr;
2971 in_desc.cd_namelen = cnp->cn_namelen;
2972 in_desc.cd_parentcnid = dcp->c_cnid;
2973 in_desc.cd_flags = S_ISDIR(mode) ? CD_ISDIR : 0;
2974 in_desc.cd_hint = dcp->c_childhint;
2975 in_desc.cd_encoding = 0;
2976
2977 if ((error = hfs_start_transaction(hfsmp)) != 0) {
2978 goto exit;
2979 }
2980 started_tr = 1;
2981
2982 /*
2983 * Reserve some space in the Catalog file.
2984 *
2985 * (we also add CAT_DELETE since our getnewvnode
2986 * request can cause an hfs_inactive call to
2987 * delete an unlinked file)
2988 */
2989 if ((error = cat_preflight(hfsmp, CAT_CREATE | CAT_DELETE, &cookie, 0))) {
2990 goto exit;
2991 }
2992 got_cookie = 1;
2993
2994 lockflags = hfs_systemfile_lock(hfsmp, SFL_CATALOG, HFS_EXCLUSIVE_LOCK);
2995 error = cat_create(hfsmp, &in_desc, &attr, &out_desc);
2996 if (error == 0) {
2997 /* Update the parent directory */
2998 dcp->c_childhint = out_desc.cd_hint; /* Cache directory's location */
2999 dcp->c_nlink++;
3000 dcp->c_entries++;
3001 dcp->c_ctime = tv.tv_sec;
3002 dcp->c_mtime = tv.tv_sec;
3003 (void) cat_update(hfsmp, &dcp->c_desc, &dcp->c_attr, NULL, NULL);
3004 HFS_KNOTE(dvp, NOTE_ATTRIB);
3005 }
3006 hfs_systemfile_unlock(hfsmp, lockflags);
3007 if (error)
3008 goto exit;
3009
3010 /* Invalidate negative cache entries in the directory */
3011 if (hfsmp->hfs_flags & HFS_CASE_SENSITIVE)
3012 cache_purge_negatives(dvp);
3013
3014 if (vnodetype == VDIR) {
3015 HFS_KNOTE(dvp, NOTE_WRITE | NOTE_LINK);
3016 } else {
3017 HFS_KNOTE(dvp, NOTE_WRITE);
3018 };
3019
3020 hfs_volupdate(hfsmp, vnodetype == VDIR ? VOL_MKDIR : VOL_MKFILE,
3021 (dcp->c_cnid == kHFSRootFolderID));
3022
3023 // XXXdbg
3024 // have to end the transaction here before we call hfs_getnewvnode()
3025 // because that can cause us to try and reclaim a vnode on a different
3026 // file system which could cause us to start a transaction which can
3027 // deadlock with someone on that other file system (since we could be
3028 // holding two transaction locks as well as various vnodes and we did
3029 // not obtain the locks on them in the proper order).
3030 //
3031 // NOTE: this means that if the quota check fails or we have to update
3032 // the change time on a block-special device that those changes
3033 // will happen as part of independent transactions.
3034 //
3035 if (started_tr) {
3036 hfs_end_transaction(hfsmp);
3037 started_tr = 0;
3038 }
3039
3040 /*
3041 * Create a vnode for the object just created.
3042 *
3043 * The cnode is locked on successful return.
3044 */
3045 error = hfs_getnewvnode(hfsmp, dvp, cnp, &out_desc, 0, &attr, NULL, &tvp);
3046 if (error)
3047 goto exit;
3048
3049 // XXXdbg
3050 //cache_enter(dvp, tvp, cnp);
3051
3052 cp = VTOC(tvp);
3053 #if QUOTA
3054 /*
3055 * We call hfs_chkiq with FORCE flag so that if we
3056 * fall through to the rmdir we actually have
3057 * accounted for the inode
3058 */
3059 if (vfs_flags(HFSTOVFS(hfsmp)) & MNT_QUOTA) {
3060 if ((error = hfs_getinoquota(cp)) ||
3061 (error = hfs_chkiq(cp, 1, vfs_context_ucred(ctx), FORCE))) {
3062
3063 if (vnode_isdir(tvp))
3064 (void) hfs_removedir(dvp, tvp, cnp, 0);
3065 else {
3066 hfs_unlock(cp);
3067 hfs_lock_truncate(cp, TRUE);
3068 hfs_lock(cp, HFS_FORCE_LOCK);
3069 (void) hfs_removefile(dvp, tvp, cnp, 0, 0);
3070 hfs_unlock_truncate(cp);
3071 }
3072 /*
3073 * we successfully allocated a new vnode, but
3074 * the quota check is telling us we're beyond
3075 * our limit, so we need to dump our lock + reference
3076 */
3077 hfs_unlock(cp);
3078 vnode_put(tvp);
3079
3080 goto exit;
3081 }
3082 }
3083 #endif /* QUOTA */
3084
3085 /* Remember if any ACL data was set. */
3086 if (VATTR_IS_ACTIVE(vap, va_acl) &&
3087 (vap->va_acl != NULL)) {
3088 cp->c_attr.ca_recflags |= kHFSHasSecurityMask;
3089 cp->c_touch_chgtime = TRUE;
3090 (void) hfs_update(tvp, TRUE);
3091 }
3092 *vpp = tvp;
3093 exit:
3094 cat_releasedesc(&out_desc);
3095
3096 if (got_cookie) {
3097 cat_postflight(hfsmp, &cookie, 0);
3098 }
3099 /*
3100 * Check if a file is located in the "Cleanup At Startup"
3101 * directory. If it is then tag it as NODUMP so that we
3102 * can be lazy about zero filling data holes.
3103 */
3104 if ((error == 0) && dvp && (vnodetype == VREG) &&
3105 (dcp->c_desc.cd_nameptr != NULL) &&
3106 (strcmp(dcp->c_desc.cd_nameptr, CARBON_TEMP_DIR_NAME) == 0)) {
3107 struct vnode *ddvp;
3108
3109 hfs_unlock(dcp);
3110 dvp = NULL;
3111
3112 /*
3113 * The parent of "Cleanup At Startup" should
3114 * have the ASCII name of the userid.
3115 */
3116 if (hfs_vget(hfsmp, dcp->c_parentcnid, &ddvp, 0) == 0) {
3117 if (VTOC(ddvp)->c_desc.cd_nameptr) {
3118 uid_t uid;
3119
3120 uid = strtoul(VTOC(ddvp)->c_desc.cd_nameptr, 0, 0);
3121 if ((uid == cp->c_uid) ||
3122 (uid == vfs_context_ucred(ctx)->cr_uid)) {
3123 cp->c_flags |= UF_NODUMP;
3124 cp->c_touch_chgtime = TRUE;
3125 }
3126 }
3127 hfs_unlock(VTOC(ddvp));
3128 vnode_put(ddvp);
3129 }
3130 }
3131 if (dvp) {
3132 hfs_unlock(dcp);
3133 }
3134 if (error == 0 && cp != NULL) {
3135 hfs_unlock(cp);
3136 }
3137 if (started_tr) {
3138 hfs_end_transaction(hfsmp);
3139 started_tr = 0;
3140 }
3141
3142 return (error);
3143 }
3144
3145
3146 /*
3147 * WARNING - assumes caller has cnode lock.
3148 */
3149 __private_extern__
3150 int
3151 hfs_vgetrsrc(struct hfsmount *hfsmp, struct vnode *vp, struct vnode **rvpp, __unused struct proc *p)
3152 {
3153 struct vnode *rvp;
3154 struct cnode *cp = VTOC(vp);
3155 int error;
3156 int vid;
3157
3158 if ((rvp = cp->c_rsrc_vp)) {
3159 vid = vnode_vid(rvp);
3160
3161 /* Use exising vnode */
3162 error = vnode_getwithvid(rvp, vid);
3163 if (error) {
3164 char * name = VTOC(vp)->c_desc.cd_nameptr;
3165
3166 if (name)
3167 printf("hfs_vgetrsrc: couldn't get"
3168 " resource fork for %s\n", name);
3169 return (error);
3170 }
3171 } else {
3172 struct cat_fork rsrcfork;
3173 struct componentname cn;
3174 int lockflags;
3175
3176 lockflags = hfs_systemfile_lock(hfsmp, SFL_CATALOG, HFS_SHARED_LOCK);
3177
3178 /* Get resource fork data */
3179 error = cat_lookup(hfsmp, &cp->c_desc, 1, (struct cat_desc *)0,
3180 (struct cat_attr *)0, &rsrcfork, NULL);
3181
3182 hfs_systemfile_unlock(hfsmp, lockflags);
3183 if (error)
3184 return (error);
3185
3186 /*
3187 * Supply hfs_getnewvnode with a component name.
3188 */
3189 cn.cn_pnbuf = NULL;
3190 if (cp->c_desc.cd_nameptr) {
3191 MALLOC_ZONE(cn.cn_pnbuf, caddr_t, MAXPATHLEN, M_NAMEI, M_WAITOK);
3192 cn.cn_nameiop = LOOKUP;
3193 cn.cn_flags = ISLASTCN | HASBUF;
3194 cn.cn_context = NULL;
3195 cn.cn_pnlen = MAXPATHLEN;
3196 cn.cn_nameptr = cn.cn_pnbuf;
3197 cn.cn_hash = 0;
3198 cn.cn_consume = 0;
3199 cn.cn_namelen = sprintf(cn.cn_nameptr, "%s%s", cp->c_desc.cd_nameptr, _PATH_RSRCFORKSPEC);
3200 }
3201 error = hfs_getnewvnode(hfsmp, vnode_parent(vp), cn.cn_pnbuf ? &cn : NULL,
3202 &cp->c_desc, 2, &cp->c_attr, &rsrcfork, &rvp);
3203 if (cn.cn_pnbuf)
3204 FREE_ZONE(cn.cn_pnbuf, cn.cn_pnlen, M_NAMEI);
3205 if (error)
3206 return (error);
3207 }
3208
3209 *rvpp = rvp;
3210 return (0);
3211 }
3212
3213
3214 static void
3215 filt_hfsdetach(struct knote *kn)
3216 {
3217 struct vnode *vp;
3218
3219 vp = (struct vnode *)kn->kn_hook;
3220 if (vnode_getwithvid(vp, kn->kn_hookid))
3221 return;
3222
3223 if (1) { /* ! KNDETACH_VNLOCKED */
3224 if (hfs_lock(VTOC(vp), HFS_EXCLUSIVE_LOCK) == 0) {
3225 (void) KNOTE_DETACH(&VTOC(vp)->c_knotes, kn);
3226 hfs_unlock(VTOC(vp));
3227 }
3228 }
3229
3230 vnode_put(vp);
3231 }
3232
3233 /*ARGSUSED*/
3234 static int
3235 filt_hfsread(struct knote *kn, long hint)
3236 {
3237 struct vnode *vp = (struct vnode *)kn->kn_hook;
3238 int dropvp = 0;
3239
3240 if (hint == 0) {
3241 if ((vnode_getwithvid(vp, kn->kn_hookid) != 0)) {
3242 hint = NOTE_REVOKE;
3243 } else
3244 dropvp = 1;
3245 }
3246 if (hint == NOTE_REVOKE) {
3247 /*
3248 * filesystem is gone, so set the EOF flag and schedule
3249 * the knote for deletion.
3250 */
3251 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
3252 return (1);
3253 }
3254
3255 /* poll(2) semantics dictate always saying there is data */
3256 kn->kn_data = (!(kn->kn_flags & EV_POLL)) ?
3257 VTOF(vp)->ff_size - kn->kn_fp->f_fglob->fg_offset : 1;
3258
3259 if (dropvp)
3260 vnode_put(vp);
3261
3262 return (kn->kn_data != 0);
3263 }
3264
3265 /*ARGSUSED*/
3266 static int
3267 filt_hfswrite(struct knote *kn, long hint)
3268 {
3269 int dropvp = 0;
3270
3271 if (hint == 0) {
3272 if ((vnode_getwithvid(kn->kn_hook, kn->kn_hookid) != 0)) {
3273 hint = NOTE_REVOKE;
3274 } else
3275 vnode_put(kn->kn_hook);
3276 }
3277 if (hint == NOTE_REVOKE) {
3278 /*
3279 * filesystem is gone, so set the EOF flag and schedule
3280 * the knote for deletion.
3281 */
3282 kn->kn_data = 0;
3283 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
3284 return (1);
3285 }
3286 kn->kn_data = 0;
3287 return (1);
3288 }
3289
3290 static int
3291 filt_hfsvnode(struct knote *kn, long hint)
3292 {
3293
3294 if (hint == 0) {
3295 if ((vnode_getwithvid(kn->kn_hook, kn->kn_hookid) != 0)) {
3296 hint = NOTE_REVOKE;
3297 } else
3298 vnode_put(kn->kn_hook);
3299 }
3300 if (kn->kn_sfflags & hint)
3301 kn->kn_fflags |= hint;
3302 if ((hint == NOTE_REVOKE)) {
3303 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
3304 return (1);
3305 }
3306
3307 return (kn->kn_fflags != 0);
3308 }
3309
3310 static struct filterops hfsread_filtops =
3311 { 1, NULL, filt_hfsdetach, filt_hfsread };
3312 static struct filterops hfswrite_filtops =
3313 { 1, NULL, filt_hfsdetach, filt_hfswrite };
3314 static struct filterops hfsvnode_filtops =
3315 { 1, NULL, filt_hfsdetach, filt_hfsvnode };
3316
3317 /*
3318 * Add a kqueue filter.
3319 */
3320 static int
3321 hfs_vnop_kqfiltadd(
3322 struct vnop_kqfilt_add_args /* {
3323 struct vnode *a_vp;
3324 struct knote *a_kn;
3325 struct proc *p;
3326 vfs_context_t a_context;
3327 } */ *ap)
3328 {
3329 struct vnode *vp = ap->a_vp;
3330 struct knote *kn = ap->a_kn;
3331 int error;
3332
3333 switch (kn->kn_filter) {
3334 case EVFILT_READ:
3335 if (vnode_isreg(vp)) {
3336 kn->kn_fop = &hfsread_filtops;
3337 } else {
3338 return EINVAL;
3339 };
3340 break;
3341 case EVFILT_WRITE:
3342 if (vnode_isreg(vp)) {
3343 kn->kn_fop = &hfswrite_filtops;
3344 } else {
3345 return EINVAL;
3346 };
3347 break;
3348 case EVFILT_VNODE:
3349 kn->kn_fop = &hfsvnode_filtops;
3350 break;
3351 default:
3352 return (1);
3353 }
3354
3355 kn->kn_hook = (caddr_t)vp;
3356 kn->kn_hookid = vnode_vid(vp);
3357
3358 if ((error = hfs_lock(VTOC(vp), HFS_EXCLUSIVE_LOCK)))
3359 return (error);
3360 KNOTE_ATTACH(&VTOC(vp)->c_knotes, kn);
3361 hfs_unlock(VTOC(vp));
3362
3363 return (0);
3364 }
3365
3366 /*
3367 * Remove a kqueue filter
3368 */
3369 static int
3370 hfs_vnop_kqfiltremove(ap)
3371 struct vnop_kqfilt_remove_args /* {
3372 struct vnode *a_vp;
3373 uintptr_t ident;
3374 vfs_context_t a_context;
3375 } */ *ap;
3376 {
3377 int result;
3378
3379 result = ENOTSUP; /* XXX */
3380
3381 return (result);
3382 }
3383
3384 /*
3385 * Wrapper for special device reads
3386 */
3387 static int
3388 hfsspec_read(ap)
3389 struct vnop_read_args /* {
3390 struct vnode *a_vp;
3391 struct uio *a_uio;
3392 int a_ioflag;
3393 vfs_context_t a_context;
3394 } */ *ap;
3395 {
3396 /*
3397 * Set access flag.
3398 */
3399 VTOC(ap->a_vp)->c_touch_acctime = TRUE;
3400 return (VOCALL (spec_vnodeop_p, VOFFSET(vnop_read), ap));
3401 }
3402
3403 /*
3404 * Wrapper for special device writes
3405 */
3406 static int
3407 hfsspec_write(ap)
3408 struct vnop_write_args /* {
3409 struct vnode *a_vp;
3410 struct uio *a_uio;
3411 int a_ioflag;
3412 vfs_context_t a_context;
3413 } */ *ap;
3414 {
3415 /*
3416 * Set update and change flags.
3417 */
3418 VTOC(ap->a_vp)->c_touch_chgtime = TRUE;
3419 VTOC(ap->a_vp)->c_touch_modtime = TRUE;
3420 return (VOCALL (spec_vnodeop_p, VOFFSET(vnop_write), ap));
3421 }
3422
3423 /*
3424 * Wrapper for special device close
3425 *
3426 * Update the times on the cnode then do device close.
3427 */
3428 static int
3429 hfsspec_close(ap)
3430 struct vnop_close_args /* {
3431 struct vnode *a_vp;
3432 int a_fflag;
3433 vfs_context_t a_context;
3434 } */ *ap;
3435 {
3436 struct vnode *vp = ap->a_vp;
3437 struct cnode *cp;
3438
3439 if (vnode_isinuse(ap->a_vp, 1)) {
3440 if (hfs_lock(VTOC(vp), HFS_EXCLUSIVE_LOCK) == 0) {
3441 cp = VTOC(vp);
3442 hfs_touchtimes(VTOHFS(vp), cp);
3443 hfs_unlock(cp);
3444 }
3445 }
3446 return (VOCALL (spec_vnodeop_p, VOFFSET(vnop_close), ap));
3447 }
3448
3449 #if FIFO
3450 /*
3451 * Wrapper for fifo reads
3452 */
3453 static int
3454 hfsfifo_read(ap)
3455 struct vnop_read_args /* {
3456 struct vnode *a_vp;
3457 struct uio *a_uio;
3458 int a_ioflag;
3459 vfs_context_t a_context;
3460 } */ *ap;
3461 {
3462 extern int (**fifo_vnodeop_p)(void *);
3463
3464 /*
3465 * Set access flag.
3466 */
3467 VTOC(ap->a_vp)->c_touch_acctime = TRUE;
3468 return (VOCALL (fifo_vnodeop_p, VOFFSET(vnop_read), ap));
3469 }
3470
3471 /*
3472 * Wrapper for fifo writes
3473 */
3474 static int
3475 hfsfifo_write(ap)
3476 struct vnop_write_args /* {
3477 struct vnode *a_vp;
3478 struct uio *a_uio;
3479 int a_ioflag;
3480 vfs_context_t a_context;
3481 } */ *ap;
3482 {
3483 extern int (**fifo_vnodeop_p)(void *);
3484
3485 /*
3486 * Set update and change flags.
3487 */
3488 VTOC(ap->a_vp)->c_touch_chgtime = TRUE;
3489 VTOC(ap->a_vp)->c_touch_modtime = TRUE;
3490 return (VOCALL (fifo_vnodeop_p, VOFFSET(vnop_write), ap));
3491 }
3492
3493 /*
3494 * Wrapper for fifo close
3495 *
3496 * Update the times on the cnode then do device close.
3497 */
3498 static int
3499 hfsfifo_close(ap)
3500 struct vnop_close_args /* {
3501 struct vnode *a_vp;
3502 int a_fflag;
3503 vfs_context_t a_context;
3504 } */ *ap;
3505 {
3506 extern int (**fifo_vnodeop_p)(void *);
3507 struct vnode *vp = ap->a_vp;
3508 struct cnode *cp;
3509
3510 if (vnode_isinuse(ap->a_vp, 1)) {
3511 if (hfs_lock(VTOC(vp), HFS_EXCLUSIVE_LOCK) == 0) {
3512 cp = VTOC(vp);
3513 hfs_touchtimes(VTOHFS(vp), cp);
3514 hfs_unlock(cp);
3515 }
3516 }
3517 return (VOCALL (fifo_vnodeop_p, VOFFSET(vnop_close), ap));
3518 }
3519
3520 /*
3521 * kqfilt_add wrapper for fifos.
3522 *
3523 * Fall through to hfs kqfilt_add routines if needed
3524 */
3525 int
3526 hfsfifo_kqfilt_add(ap)
3527 struct vnop_kqfilt_add_args *ap;
3528 {
3529 extern int (**fifo_vnodeop_p)(void *);
3530 int error;
3531
3532 error = VOCALL(fifo_vnodeop_p, VOFFSET(vnop_kqfilt_add), ap);
3533 if (error)
3534 error = hfs_vnop_kqfiltadd(ap);
3535 return (error);
3536 }
3537
3538 /*
3539 * kqfilt_remove wrapper for fifos.
3540 *
3541 * Fall through to hfs kqfilt_remove routines if needed
3542 */
3543 int
3544 hfsfifo_kqfilt_remove(ap)
3545 struct vnop_kqfilt_remove_args *ap;
3546 {
3547 extern int (**fifo_vnodeop_p)(void *);
3548 int error;
3549
3550 error = VOCALL(fifo_vnodeop_p, VOFFSET(vnop_kqfilt_remove), ap);
3551 if (error)
3552 error = hfs_vnop_kqfiltremove(ap);
3553 return (error);
3554 }
3555
3556 #endif /* FIFO */
3557
3558 /*
3559 * Synchronize a file's in-core state with that on disk.
3560 */
3561 static int
3562 hfs_vnop_fsync(ap)
3563 struct vnop_fsync_args /* {
3564 struct vnode *a_vp;
3565 int a_waitfor;
3566 vfs_context_t a_context;
3567 } */ *ap;
3568 {
3569 struct vnode* vp = ap->a_vp;
3570 int error;
3571
3572 /*
3573 * We need to allow ENOENT lock errors since unlink
3574 * systenm call can call VNOP_FSYNC during vclean.
3575 */
3576 error = hfs_lock(VTOC(vp), HFS_EXCLUSIVE_LOCK);
3577 if (error)
3578 return (0);
3579
3580 error = hfs_fsync(vp, ap->a_waitfor, 0, vfs_context_proc(ap->a_context));
3581
3582 hfs_unlock(VTOC(vp));
3583 return (error);
3584 }
3585
3586 /*****************************************************************************
3587 *
3588 * VOP Tables
3589 *
3590 *****************************************************************************/
3591 int hfs_vnop_readdirattr(struct vnop_readdirattr_args *); /* in hfs_attrlist.c */
3592 int hfs_vnop_inactive(struct vnop_inactive_args *); /* in hfs_cnode.c */
3593 int hfs_vnop_reclaim(struct vnop_reclaim_args *); /* in hfs_cnode.c */
3594 int hfs_vnop_link(struct vnop_link_args *); /* in hfs_link.c */
3595 int hfs_vnop_lookup(struct vnop_lookup_args *); /* in hfs_lookup.c */
3596 int hfs_vnop_search(struct vnop_searchfs_args *); /* in hfs_search.c */
3597
3598 int hfs_vnop_read(struct vnop_read_args *); /* in hfs_readwrite.c */
3599 int hfs_vnop_write(struct vnop_write_args *); /* in hfs_readwrite.c */
3600 int hfs_vnop_ioctl(struct vnop_ioctl_args *); /* in hfs_readwrite.c */
3601 int hfs_vnop_select(struct vnop_select_args *); /* in hfs_readwrite.c */
3602 int hfs_vnop_strategy(struct vnop_strategy_args *); /* in hfs_readwrite.c */
3603 int hfs_vnop_allocate(struct vnop_allocate_args *); /* in hfs_readwrite.c */
3604 int hfs_vnop_pagein(struct vnop_pagein_args *); /* in hfs_readwrite.c */
3605 int hfs_vnop_pageout(struct vnop_pageout_args *); /* in hfs_readwrite.c */
3606 int hfs_vnop_bwrite(struct vnop_bwrite_args *); /* in hfs_readwrite.c */
3607 int hfs_vnop_blktooff(struct vnop_blktooff_args *); /* in hfs_readwrite.c */
3608 int hfs_vnop_offtoblk(struct vnop_offtoblk_args *); /* in hfs_readwrite.c */
3609 int hfs_vnop_blockmap(struct vnop_blockmap_args *); /* in hfs_readwrite.c */
3610 int hfs_vnop_getxattr(struct vnop_getxattr_args *); /* in hfs_xattr.c */
3611 int hfs_vnop_setxattr(struct vnop_setxattr_args *); /* in hfs_xattr.c */
3612 int hfs_vnop_removexattr(struct vnop_removexattr_args *); /* in hfs_xattr.c */
3613 int hfs_vnop_listxattr(struct vnop_listxattr_args *); /* in hfs_xattr.c */
3614
3615 int (**hfs_vnodeop_p)(void *);
3616
3617 #define VOPFUNC int (*)(void *)
3618
3619 struct vnodeopv_entry_desc hfs_vnodeop_entries[] = {
3620 { &vnop_default_desc, (VOPFUNC)vn_default_error },
3621 { &vnop_lookup_desc, (VOPFUNC)hfs_vnop_lookup }, /* lookup */
3622 { &vnop_create_desc, (VOPFUNC)hfs_vnop_create }, /* create */
3623 { &vnop_mknod_desc, (VOPFUNC)hfs_vnop_mknod }, /* mknod */
3624 { &vnop_open_desc, (VOPFUNC)hfs_vnop_open }, /* open */
3625 { &vnop_close_desc, (VOPFUNC)hfs_vnop_close }, /* close */
3626 { &vnop_getattr_desc, (VOPFUNC)hfs_vnop_getattr }, /* getattr */
3627 { &vnop_setattr_desc, (VOPFUNC)hfs_vnop_setattr }, /* setattr */
3628 { &vnop_read_desc, (VOPFUNC)hfs_vnop_read }, /* read */
3629 { &vnop_write_desc, (VOPFUNC)hfs_vnop_write }, /* write */
3630 { &vnop_ioctl_desc, (VOPFUNC)hfs_vnop_ioctl }, /* ioctl */
3631 { &vnop_select_desc, (VOPFUNC)hfs_vnop_select }, /* select */
3632 { &vnop_revoke_desc, (VOPFUNC)nop_revoke }, /* revoke */
3633 { &vnop_exchange_desc, (VOPFUNC)hfs_vnop_exchange }, /* exchange */
3634 { &vnop_mmap_desc, (VOPFUNC)err_mmap }, /* mmap */
3635 { &vnop_fsync_desc, (VOPFUNC)hfs_vnop_fsync }, /* fsync */
3636 { &vnop_remove_desc, (VOPFUNC)hfs_vnop_remove }, /* remove */
3637 { &vnop_link_desc, (VOPFUNC)hfs_vnop_link }, /* link */
3638 { &vnop_rename_desc, (VOPFUNC)hfs_vnop_rename }, /* rename */
3639 { &vnop_mkdir_desc, (VOPFUNC)hfs_vnop_mkdir }, /* mkdir */
3640 { &vnop_rmdir_desc, (VOPFUNC)hfs_vnop_rmdir }, /* rmdir */
3641 { &vnop_symlink_desc, (VOPFUNC)hfs_vnop_symlink }, /* symlink */
3642 { &vnop_readdir_desc, (VOPFUNC)hfs_vnop_readdir }, /* readdir */
3643 { &vnop_readdirattr_desc, (VOPFUNC)hfs_vnop_readdirattr }, /* readdirattr */
3644 { &vnop_readlink_desc, (VOPFUNC)hfs_vnop_readlink }, /* readlink */
3645 { &vnop_inactive_desc, (VOPFUNC)hfs_vnop_inactive }, /* inactive */
3646 { &vnop_reclaim_desc, (VOPFUNC)hfs_vnop_reclaim }, /* reclaim */
3647 { &vnop_strategy_desc, (VOPFUNC)hfs_vnop_strategy }, /* strategy */
3648 { &vnop_pathconf_desc, (VOPFUNC)hfs_vnop_pathconf }, /* pathconf */
3649 { &vnop_advlock_desc, (VOPFUNC)err_advlock }, /* advlock */
3650 { &vnop_allocate_desc, (VOPFUNC)hfs_vnop_allocate }, /* allocate */
3651 { &vnop_searchfs_desc, (VOPFUNC)hfs_vnop_search }, /* search fs */
3652 { &vnop_bwrite_desc, (VOPFUNC)hfs_vnop_bwrite }, /* bwrite */
3653 { &vnop_pagein_desc, (VOPFUNC)hfs_vnop_pagein }, /* pagein */
3654 { &vnop_pageout_desc,(VOPFUNC) hfs_vnop_pageout }, /* pageout */
3655 { &vnop_copyfile_desc, (VOPFUNC)err_copyfile }, /* copyfile */
3656 { &vnop_blktooff_desc, (VOPFUNC)hfs_vnop_blktooff }, /* blktooff */
3657 { &vnop_offtoblk_desc, (VOPFUNC)hfs_vnop_offtoblk }, /* offtoblk */
3658 { &vnop_blockmap_desc, (VOPFUNC)hfs_vnop_blockmap }, /* blockmap */
3659 { &vnop_kqfilt_add_desc, (VOPFUNC)hfs_vnop_kqfiltadd }, /* kqfilt_add */
3660 { &vnop_kqfilt_remove_desc, (VOPFUNC)hfs_vnop_kqfiltremove }, /* kqfilt_remove */
3661 { &vnop_getxattr_desc, (VOPFUNC)hfs_vnop_getxattr},
3662 { &vnop_setxattr_desc, (VOPFUNC)hfs_vnop_setxattr},
3663 { &vnop_removexattr_desc, (VOPFUNC)hfs_vnop_removexattr},
3664 { &vnop_listxattr_desc, (VOPFUNC)hfs_vnop_listxattr},
3665 { NULL, (VOPFUNC)NULL }
3666 };
3667
3668 struct vnodeopv_desc hfs_vnodeop_opv_desc =
3669 { &hfs_vnodeop_p, hfs_vnodeop_entries };
3670
3671 int (**hfs_specop_p)(void *);
3672 struct vnodeopv_entry_desc hfs_specop_entries[] = {
3673 { &vnop_default_desc, (VOPFUNC)vn_default_error },
3674 { &vnop_lookup_desc, (VOPFUNC)spec_lookup }, /* lookup */
3675 { &vnop_create_desc, (VOPFUNC)spec_create }, /* create */
3676 { &vnop_mknod_desc, (VOPFUNC)spec_mknod }, /* mknod */
3677 { &vnop_open_desc, (VOPFUNC)spec_open }, /* open */
3678 { &vnop_close_desc, (VOPFUNC)hfsspec_close }, /* close */
3679 { &vnop_getattr_desc, (VOPFUNC)hfs_vnop_getattr }, /* getattr */
3680 { &vnop_setattr_desc, (VOPFUNC)hfs_vnop_setattr }, /* setattr */
3681 { &vnop_read_desc, (VOPFUNC)hfsspec_read }, /* read */
3682 { &vnop_write_desc, (VOPFUNC)hfsspec_write }, /* write */
3683 { &vnop_ioctl_desc, (VOPFUNC)spec_ioctl }, /* ioctl */
3684 { &vnop_select_desc, (VOPFUNC)spec_select }, /* select */
3685 { &vnop_revoke_desc, (VOPFUNC)spec_revoke }, /* revoke */
3686 { &vnop_mmap_desc, (VOPFUNC)spec_mmap }, /* mmap */
3687 { &vnop_fsync_desc, (VOPFUNC)hfs_vnop_fsync }, /* fsync */
3688 { &vnop_remove_desc, (VOPFUNC)spec_remove }, /* remove */
3689 { &vnop_link_desc, (VOPFUNC)spec_link }, /* link */
3690 { &vnop_rename_desc, (VOPFUNC)spec_rename }, /* rename */
3691 { &vnop_mkdir_desc, (VOPFUNC)spec_mkdir }, /* mkdir */
3692 { &vnop_rmdir_desc, (VOPFUNC)spec_rmdir }, /* rmdir */
3693 { &vnop_symlink_desc, (VOPFUNC)spec_symlink }, /* symlink */
3694 { &vnop_readdir_desc, (VOPFUNC)spec_readdir }, /* readdir */
3695 { &vnop_readlink_desc, (VOPFUNC)spec_readlink }, /* readlink */
3696 { &vnop_inactive_desc, (VOPFUNC)hfs_vnop_inactive }, /* inactive */
3697 { &vnop_reclaim_desc, (VOPFUNC)hfs_vnop_reclaim }, /* reclaim */
3698 { &vnop_strategy_desc, (VOPFUNC)spec_strategy }, /* strategy */
3699 { &vnop_pathconf_desc, (VOPFUNC)spec_pathconf }, /* pathconf */
3700 { &vnop_advlock_desc, (VOPFUNC)err_advlock }, /* advlock */
3701 { &vnop_bwrite_desc, (VOPFUNC)hfs_vnop_bwrite },
3702 { &vnop_devblocksize_desc, (VOPFUNC)spec_devblocksize }, /* devblocksize */
3703 { &vnop_pagein_desc, (VOPFUNC)hfs_vnop_pagein }, /* Pagein */
3704 { &vnop_pageout_desc, (VOPFUNC)hfs_vnop_pageout }, /* Pageout */
3705 { &vnop_copyfile_desc, (VOPFUNC)err_copyfile }, /* copyfile */
3706 { &vnop_blktooff_desc, (VOPFUNC)hfs_vnop_blktooff }, /* blktooff */
3707 { &vnop_offtoblk_desc, (VOPFUNC)hfs_vnop_offtoblk }, /* offtoblk */
3708 { (struct vnodeop_desc*)NULL, (VOPFUNC)NULL }
3709 };
3710 struct vnodeopv_desc hfs_specop_opv_desc =
3711 { &hfs_specop_p, hfs_specop_entries };
3712
3713 #if FIFO
3714 int (**hfs_fifoop_p)(void *);
3715 struct vnodeopv_entry_desc hfs_fifoop_entries[] = {
3716 { &vnop_default_desc, (VOPFUNC)vn_default_error },
3717 { &vnop_lookup_desc, (VOPFUNC)fifo_lookup }, /* lookup */
3718 { &vnop_create_desc, (VOPFUNC)fifo_create }, /* create */
3719 { &vnop_mknod_desc, (VOPFUNC)fifo_mknod }, /* mknod */
3720 { &vnop_open_desc, (VOPFUNC)fifo_open }, /* open */
3721 { &vnop_close_desc, (VOPFUNC)hfsfifo_close }, /* close */
3722 { &vnop_getattr_desc, (VOPFUNC)hfs_vnop_getattr }, /* getattr */
3723 { &vnop_setattr_desc, (VOPFUNC)hfs_vnop_setattr }, /* setattr */
3724 { &vnop_read_desc, (VOPFUNC)hfsfifo_read }, /* read */
3725 { &vnop_write_desc, (VOPFUNC)hfsfifo_write }, /* write */
3726 { &vnop_ioctl_desc, (VOPFUNC)fifo_ioctl }, /* ioctl */
3727 { &vnop_select_desc, (VOPFUNC)fifo_select }, /* select */
3728 { &vnop_revoke_desc, (VOPFUNC)fifo_revoke }, /* revoke */
3729 { &vnop_mmap_desc, (VOPFUNC)fifo_mmap }, /* mmap */
3730 { &vnop_fsync_desc, (VOPFUNC)hfs_vnop_fsync }, /* fsync */
3731 { &vnop_remove_desc, (VOPFUNC)fifo_remove }, /* remove */
3732 { &vnop_link_desc, (VOPFUNC)fifo_link }, /* link */
3733 { &vnop_rename_desc, (VOPFUNC)fifo_rename }, /* rename */
3734 { &vnop_mkdir_desc, (VOPFUNC)fifo_mkdir }, /* mkdir */
3735 { &vnop_rmdir_desc, (VOPFUNC)fifo_rmdir }, /* rmdir */
3736 { &vnop_symlink_desc, (VOPFUNC)fifo_symlink }, /* symlink */
3737 { &vnop_readdir_desc, (VOPFUNC)fifo_readdir }, /* readdir */
3738 { &vnop_readlink_desc, (VOPFUNC)fifo_readlink }, /* readlink */
3739 { &vnop_inactive_desc, (VOPFUNC)hfs_vnop_inactive }, /* inactive */
3740 { &vnop_reclaim_desc, (VOPFUNC)hfs_vnop_reclaim }, /* reclaim */
3741 { &vnop_strategy_desc, (VOPFUNC)fifo_strategy }, /* strategy */
3742 { &vnop_pathconf_desc, (VOPFUNC)fifo_pathconf }, /* pathconf */
3743 { &vnop_advlock_desc, (VOPFUNC)err_advlock }, /* advlock */
3744 { &vnop_bwrite_desc, (VOPFUNC)hfs_vnop_bwrite },
3745 { &vnop_pagein_desc, (VOPFUNC)hfs_vnop_pagein }, /* Pagein */
3746 { &vnop_pageout_desc, (VOPFUNC)hfs_vnop_pageout }, /* Pageout */
3747 { &vnop_copyfile_desc, (VOPFUNC)err_copyfile }, /* copyfile */
3748 { &vnop_blktooff_desc, (VOPFUNC)hfs_vnop_blktooff }, /* blktooff */
3749 { &vnop_offtoblk_desc, (VOPFUNC)hfs_vnop_offtoblk }, /* offtoblk */
3750 { &vnop_blockmap_desc, (VOPFUNC)hfs_vnop_blockmap }, /* blockmap */
3751 { &vnop_kqfilt_add_desc, (VOPFUNC)hfsfifo_kqfilt_add }, /* kqfilt_add */
3752 { &vnop_kqfilt_remove_desc, (VOPFUNC)hfsfifo_kqfilt_remove }, /* kqfilt_remove */
3753 { (struct vnodeop_desc*)NULL, (VOPFUNC)NULL }
3754 };
3755 struct vnodeopv_desc hfs_fifoop_opv_desc =
3756 { &hfs_fifoop_p, hfs_fifoop_entries };
3757 #endif /* FIFO */
3758
3759
3760