]> git.saurik.com Git - apple/hfs.git/blob - livefiles_hfs_plugin/lf_hfs_cnode.c
hfs-522.0.9.tar.gz
[apple/hfs.git] / livefiles_hfs_plugin / lf_hfs_cnode.c
1 /* Copyright © 2017-2018 Apple Inc. All rights reserved.
2 *
3 * lf_hfs_cnode.c
4 * livefiles_hfs
5 *
6 * Created by Or Haimovich on 20/3/18.
7 */
8
9 #include "lf_hfs_cnode.h"
10 #include "lf_hfs.h"
11 #include "lf_hfs_vfsops.h"
12 #include "lf_hfs_chash.h"
13 #include "lf_hfs_vfsutils.h"
14 #include "lf_hfs_vnops.h"
15 #include "lf_hfs_logger.h"
16 #include "lf_hfs_utils.h"
17 #include "lf_hfs_btrees_internal.h"
18 #include "lf_hfs_readwrite_ops.h"
19 #include "lf_hfs_utils.h"
20 #include <sys/stat.h>
21 #include "lf_hfs_xattr.h"
22 #include "lf_hfs_link.h"
23 #include "lf_hfs_generic_buf.h"
24
25 static void
26 hfs_reclaim_cnode(struct cnode *cp)
27 {
28 /*
29 * If the descriptor has a name then release it
30 */
31 if ((cp->c_desc.cd_flags & CD_HASBUF) && (cp->c_desc.cd_nameptr != 0))
32 {
33 cp->c_desc.cd_flags &= ~CD_HASBUF;
34 cp->c_desc.cd_namelen = 0;
35 hfs_free((void*)cp->c_desc.cd_nameptr);
36 cp->c_desc.cd_nameptr = NULL;
37 }
38
39 /*
40 * We only call this function if we are in hfs_vnop_reclaim and
41 * attempting to reclaim a cnode with only one live fork. Because the vnode
42 * went through reclaim, any future attempts to use this item will have to
43 * go through lookup again, which will need to create a new vnode. Thus,
44 * destroying the locks below is safe.
45 */
46
47 lf_lck_rw_destroy(&cp->c_rwlock);
48 lf_cond_destroy(&cp->c_cacsh_cond);
49 lf_lck_rw_destroy(&cp->c_truncatelock);
50
51 hfs_free(cp);
52 }
53
54 /*
55 * hfs_getnewvnode - get new default vnode
56 *
57 * The vnode is returned with an iocount and the cnode locked.
58 * The cnode of the parent vnode 'dvp' may or may not be locked, depending on
59 * the circumstances. The cnode in question (if acquiring the resource fork),
60 * may also already be locked at the time we enter this function.
61 *
62 * Note that there are both input and output flag arguments to this function.
63 * If one of the input flags (specifically, GNV_USE_VP), is set, then
64 * hfs_getnewvnode will use the parameter *vpp, which is traditionally only
65 * an output parameter, as both an input and output parameter. It will use
66 * the vnode provided in the output, and pass it to vnode_create with the
67 * proper flavor so that a new vnode is _NOT_ created on our behalf when
68 * we dispatch to VFS. This may be important in various HFS vnode creation
69 * routines, such a create or get-resource-fork, because we risk deadlock if
70 * jetsam is involved.
71 *
72 * Deadlock potential exists if jetsam is synchronously invoked while we are waiting
73 * for a vnode to be recycled in order to give it the identity we want. If jetsam
74 * happens to target a process for termination that is blocked in-kernel, waiting to
75 * acquire the cnode lock on our parent 'dvp', while our current thread has it locked,
76 * neither side will make forward progress and the watchdog timer will eventually fire.
77 * To prevent this, a caller of hfs_getnewvnode may choose to proactively force
78 * any necessary vnode reclamation/recycling while it is not holding any locks and
79 * thus not prone to deadlock. If this is the case, GNV_USE_VP will be set and
80 * the parameter will be used as described above.
81 *
82 * !!! <NOTE> !!!!
83 * In circumstances when GNV_USE_VP is set, this function _MUST_ clean up and either consume
84 * or dispose of the provided vnode. We funnel all errors to a single return value so that
85 * if provided_vp is still non-NULL, then we will dispose of the vnode. This will occur in
86 * all error cases of this function -- anywhere we zero/NULL out the *vpp parameter. It may
87 * also occur if the current thread raced with another to create the same vnode, and we
88 * find the entry already present in the cnode hash.
89 * !!! </NOTE> !!!
90 */
91 int
92 hfs_getnewvnode(struct hfsmount *hfsmp, struct vnode *dvp, struct componentname *cnp, struct cat_desc *descp, int flags, struct cat_attr *attrp, struct cat_fork *forkp, struct vnode **vpp, int *out_flags)
93 {
94 struct mount *mp = HFSTOVFS(hfsmp);
95 struct vnode *vp = NULL;
96 struct vnode **cvpp;
97 struct vnode *tvp = NULL;
98 struct cnode *cp = NULL;
99 struct filefork *fp = NULL;
100 struct vnode *provided_vp = NULL;
101 struct vnode_fsparam vfsp = {0};
102 enum vtype vtype = IFTOVT(attrp->ca_mode);
103 int retval = 0;
104 int hflags = 0;
105 int issystemfile = (descp->cd_flags & CD_ISMETA) && (vtype == VREG);
106 int wantrsrc = flags & GNV_WANTRSRC;;
107 int need_update_identity = 0;
108
109 /* Zero out the out_flags */
110 *out_flags = 0;
111
112 if (flags & GNV_USE_VP)
113 {
114 /* Store the provided VP for later use */
115 provided_vp = *vpp;
116 }
117
118 /* Zero out the vpp regardless of provided input */
119 *vpp = NULL;
120
121 if (attrp->ca_fileid == 0)
122 {
123 retval = ENOENT;
124 goto gnv_exit;
125 }
126
127 /* Sanity checks: */
128 if ( (vtype == VBAD) ||
129 ( (vtype != VDIR && forkp &&
130 ( (attrp->ca_blocks < forkp->cf_blocks) || (howmany((uint64_t)forkp->cf_size, hfsmp->blockSize) > forkp->cf_blocks) ||
131 ( (vtype == VLNK) && ((uint64_t)forkp->cf_size > MAXPATHLEN) ) ) ) ) )
132 {
133 /* Mark the FS as corrupt and bail out */
134 hfs_mark_inconsistent(hfsmp, HFS_INCONSISTENCY_DETECTED);
135 retval = EINVAL;
136 goto gnv_exit;
137 }
138
139 /*
140 * Get a cnode (new or existing)
141 */
142 cp = hfs_chash_getcnode(hfsmp, attrp->ca_fileid, vpp, wantrsrc, (flags & GNV_SKIPLOCK), out_flags, &hflags);
143
144 /*
145 * If the id is no longer valid for lookups we'll get back a NULL cp.
146 */
147 if (cp == NULL)
148 {
149 retval = ENOENT;
150 goto gnv_exit;
151 }
152
153 /*
154 * We may have been provided a vnode via
155 * GNV_USE_VP. In this case, we have raced with
156 * a 2nd thread to create the target vnode. The provided
157 * vnode that was passed in will be dealt with at the
158 * end of the function, as we don't zero out the field
159 * until we're ready to pass responsibility to VFS.
160 */
161
162
163 /*
164 * If we get a cnode/vnode pair out of hfs_chash_getcnode, then update the
165 * descriptor in the cnode as needed if the cnode represents a hardlink.
166 * We want the caller to get the most up-to-date copy of the descriptor
167 * as possible. However, we only do anything here if there was a valid vnode.
168 * If there isn't a vnode, then the cnode is brand new and needs to be initialized
169 * as it doesn't have a descriptor or cat_attr yet.
170 *
171 * If we are about to replace the descriptor with the user-supplied one, then validate
172 * that the descriptor correctly acknowledges this item is a hardlink. We could be
173 * subject to a race where the calling thread invoked cat_lookup, got a valid lookup
174 * result but the file was not yet a hardlink. With sufficient delay between there
175 * and here, we might accidentally copy in the raw inode ID into the descriptor in the
176 * call below. If the descriptor's CNID is the same as the fileID then it must
177 * not yet have been a hardlink when the lookup occurred.
178 */
179
180 if (!(cp->c_flag & (C_DELETED | C_NOEXISTS)))
181 {
182 //
183 // If the bytes of the filename in the descp do not match the bytes in the
184 // cnp (and we're not looking up the resource fork), then we want to update
185 // the vnode identity to contain the bytes that HFS stores so that when an
186 // fsevent gets generated, it has the correct filename. otherwise daemons
187 // that match filenames produced by fsevents with filenames they have stored
188 // elsewhere (e.g. bladerunner, backupd, mds), the filenames will not match.
189 // See: <rdar://problem/8044697> FSEvents doesn't always decompose diacritical unicode chars in the paths of the changed directories
190 // for more details.
191 //
192 if (*vpp && cnp && cnp->cn_nameptr && descp && descp->cd_nameptr && strncmp((const char *)cnp->cn_nameptr, (const char *)descp->cd_nameptr, descp->cd_namelen) != 0)
193 {
194 vnode_update_identity (*vpp, dvp, (const char *)descp->cd_nameptr, descp->cd_namelen, 0, VNODE_UPDATE_NAME);
195 }
196
197 if ((cp->c_flag & C_HARDLINK) && descp->cd_nameptr && descp->cd_namelen > 0)
198 {
199 /* If cnode is uninitialized, its c_attr will be zeroed out; cnids wont match. */
200 if ((descp->cd_cnid == cp->c_attr.ca_fileid) && (attrp->ca_linkcount != cp->c_attr.ca_linkcount))
201 {
202
203 if ((flags & GNV_SKIPLOCK) == 0)
204 {
205 /*
206 * Then we took the lock. Drop it before calling
207 * vnode_put, which may invoke hfs_vnop_inactive and need to take
208 * the cnode lock again.
209 */
210 hfs_unlock(cp);
211 }
212
213 /*
214 * Emit ERECYCLE and GNV_CAT_ATTRCHANGED to
215 * force a re-drive in the lookup routine.
216 * Drop the iocount on the vnode obtained from
217 * chash_getcnode if needed.
218 */
219 if (*vpp != NULL)
220 {
221 hfs_free(*vpp);
222 *vpp = NULL;
223 }
224
225 /*
226 * If we raced with VNOP_RECLAIM for this vnode, the hash code could
227 * have observed it after the c_vp or c_rsrc_vp fields had been torn down;
228 * the hash code peeks at those fields without holding the cnode lock because
229 * it needs to be fast. As a result, we may have set H_ATTACH in the chash
230 * call above. Since we're bailing out, unset whatever flags we just set, and
231 * wake up all waiters for this cnode.
232 */
233 if (hflags)
234 {
235 hfs_chashwakeup(hfsmp, cp, hflags);
236 }
237
238 *out_flags = GNV_CAT_ATTRCHANGED;
239 retval = ERECYCLE;
240 goto gnv_exit;
241 }
242 else
243 {
244 /*
245 * Otherwise, CNID != fileid. Go ahead and copy in the new descriptor.
246 *
247 * Replacing the descriptor here is fine because we looked up the item without
248 * a vnode in hand before. If a vnode existed, its identity must be attached to this
249 * item. We are not susceptible to the lookup fastpath issue at this point.
250 */
251 replace_desc(cp, descp);
252
253 /*
254 * This item was a hardlink, and its name needed to be updated. By replacing the
255 * descriptor above, we've now updated the cnode's internal representation of
256 * its link ID/CNID, parent ID, and its name. However, VFS must now be alerted
257 * to the fact that this vnode now has a new parent, since we cannot guarantee
258 * that the new link lived in the same directory as the alternative name for
259 * this item.
260 */
261 if ((*vpp != NULL) && (cnp || cp->c_desc.cd_nameptr))
262 {
263 /* we could be requesting the rsrc of a hardlink file... */
264 if (cp->c_desc.cd_nameptr)
265 {
266 // Update the identity with what we have stored on disk as the name of this file.
267 vnode_update_identity (*vpp, dvp, (const char *)cp->c_desc.cd_nameptr, cp->c_desc.cd_namelen, 0, (VNODE_UPDATE_PARENT | VNODE_UPDATE_NAME));
268 }
269 else if (cnp)
270 {
271 vnode_update_identity (*vpp, dvp, cnp->cn_nameptr, cnp->cn_namelen, cnp->cn_hash, (VNODE_UPDATE_PARENT | VNODE_UPDATE_NAME));
272 }
273 }
274 }
275 }
276 }
277
278 /*
279 * At this point, we have performed hardlink and open-unlinked checks
280 * above. We have now validated the state of the vnode that was given back
281 * to us from the cnode hash code and find it safe to return.
282 */
283 if (*vpp != NULL)
284 {
285 retval = 0;
286 goto gnv_exit;
287 }
288
289 /*
290 * If this is a new cnode then initialize it.
291 */
292 if (ISSET(cp->c_hflag, H_ALLOC))
293 {
294 lf_lck_rw_init(&cp->c_truncatelock);
295
296 /* Make sure its still valid (ie exists on disk). */
297 if (!(flags & GNV_CREATE))
298 {
299 int error = 0;
300 if (!hfs_valid_cnode (hfsmp, dvp, (wantrsrc ? NULL : cnp), cp->c_fileid, attrp, &error))
301 {
302 hfs_chash_abort(hfsmp, cp);
303 if ((flags & GNV_SKIPLOCK) == 0)
304 {
305 hfs_unlock(cp);
306 }
307
308 hfs_reclaim_cnode(cp);
309 *vpp = NULL;
310 /*
311 * If we hit this case, that means that the entry was there in the catalog when
312 * we did a cat_lookup earlier. Think hfs_lookup. However, in between the time
313 * that we checked the catalog and the time we went to get a vnode/cnode for it,
314 * it had been removed from the namespace and the vnode totally reclaimed. As a result,
315 * it's not there in the catalog during the check in hfs_valid_cnode and we bubble out
316 * an ENOENT. To indicate to the caller that they should really double-check the
317 * entry (it could have been renamed over and gotten a new fileid), we mark a bit
318 * in the output flags.
319 */
320 if (error == ENOENT)
321 {
322 *out_flags = GNV_CAT_DELETED;
323 retval = ENOENT;
324 goto gnv_exit;
325 }
326
327 /*
328 * Also, we need to protect the cat_attr acquired during hfs_lookup and passed into
329 * this function as an argument because the catalog may have changed w.r.t hardlink
330 * link counts and the firstlink field. If that validation check fails, then let
331 * lookup re-drive itself to get valid/consistent data with the same failure condition below.
332 */
333 if (error == ERECYCLE)
334 {
335 *out_flags = GNV_CAT_ATTRCHANGED;
336 retval = ERECYCLE;
337 goto gnv_exit;
338 }
339 }
340 }
341 bcopy(attrp, &cp->c_attr, sizeof(struct cat_attr));
342 bcopy(descp, &cp->c_desc, sizeof(struct cat_desc));
343
344 /* The name was inherited so clear descriptor state... */
345 descp->cd_nameptr = NULL;
346 descp->cd_namelen = 0;
347 descp->cd_flags &= ~CD_HASBUF;
348
349 /* Tag hardlinks */
350 if ( (vtype == VREG || vtype == VDIR || vtype == VSOCK || vtype == VFIFO) &&
351 (descp->cd_cnid != attrp->ca_fileid || ISSET(attrp->ca_recflags, kHFSHasLinkChainMask) ) )
352 {
353 cp->c_flag |= C_HARDLINK;
354 }
355
356 /*
357 * Fix-up dir link counts.
358 *
359 * Earlier versions of Leopard used ca_linkcount for posix
360 * nlink support (effectively the sub-directory count + 2).
361 * That is now accomplished using the ca_dircount field with
362 * the corresponding kHFSHasFolderCountMask flag.
363 *
364 * For directories the ca_linkcount is the true link count,
365 * tracking the number of actual hardlinks to a directory.
366 *
367 * We only do this if the mount has HFS_FOLDERCOUNT set;
368 * at the moment, we only set that for HFSX volumes.
369 */
370 if ( (hfsmp->hfs_flags & HFS_FOLDERCOUNT) && (vtype == VDIR) &&
371 (!(attrp->ca_recflags & kHFSHasFolderCountMask)) && (cp->c_attr.ca_linkcount > 1) )
372 {
373 if (cp->c_attr.ca_entries == 0)
374 {
375 cp->c_attr.ca_dircount = 0;
376 }
377 else
378 {
379 cp->c_attr.ca_dircount = cp->c_attr.ca_linkcount - 2;
380 }
381
382 cp->c_attr.ca_linkcount = 1;
383 cp->c_attr.ca_recflags |= kHFSHasFolderCountMask;
384 if ( !(hfsmp->hfs_flags & HFS_READ_ONLY) )
385 {
386 cp->c_flag |= C_MODIFIED;
387 }
388 }
389
390 /* Mark the output flag that we're vending a new cnode */
391 *out_flags |= GNV_NEW_CNODE;
392 }
393
394 if (vtype == VDIR)
395 {
396 if (cp->c_vp != NULL)
397 {
398 LFHFS_LOG(LEVEL_ERROR, "hfs_getnewvnode: orphaned vnode (data)");
399 assert(0);
400 }
401 cvpp = &cp->c_vp;
402 }
403 else
404 {
405 /*
406 * Allocate and initialize a file fork...
407 */
408 fp = hfs_malloc(sizeof(struct filefork));
409 if (fp == NULL)
410 {
411 retval = ENOMEM;
412 goto gnv_exit;
413 }
414 memset(fp,0,sizeof(struct filefork));
415
416 fp->ff_cp = cp;
417 if (forkp)
418 {
419 bcopy(forkp, &fp->ff_data, sizeof(struct cat_fork));
420 }
421 else
422 {
423 bzero(&fp->ff_data, sizeof(struct cat_fork));
424 }
425 rl_init(&fp->ff_invalidranges);
426 fp->ff_sysfileinfo = 0;
427
428 if (wantrsrc)
429 {
430 if (cp->c_rsrcfork != NULL)
431 {
432 LFHFS_LOG(LEVEL_ERROR, "hfs_getnewvnode: orphaned rsrc fork");
433 hfs_assert(0);
434 }
435 if (cp->c_rsrc_vp != NULL)
436 {
437 LFHFS_LOG(LEVEL_ERROR, "hfs_getnewvnode: orphaned vnode (rsrc)");
438 hfs_assert(0);
439 }
440 cp->c_rsrcfork = fp;
441 cvpp = &cp->c_rsrc_vp;
442 if ( (tvp = cp->c_vp) != NULL )
443 {
444 cp->c_flag |= C_NEED_DVNODE_PUT;
445 }
446 }
447 else
448 {
449 if (cp->c_datafork != NULL)
450 {
451 LFHFS_LOG(LEVEL_ERROR, "hfs_getnewvnode: orphaned data fork");
452 hfs_assert(0);
453 }
454 if (cp->c_vp != NULL)
455 {
456 LFHFS_LOG(LEVEL_ERROR, "hfs_getnewvnode: orphaned vnode (data)");
457 hfs_assert(0);
458 }
459
460 cp->c_datafork = fp;
461 cvpp = &cp->c_vp;
462 if ( (tvp = cp->c_rsrc_vp) != NULL)
463 {
464 cp->c_flag |= C_NEED_RVNODE_PUT;
465 }
466 }
467 }
468 #if LF_HFS_FULL_VNODE_SUPPORT
469 if (tvp != NULL)
470 {
471 /*
472 * grab an iocount on the vnode we weren't
473 * interested in (i.e. we want the resource fork
474 * but the cnode already has the data fork)
475 * to prevent it from being
476 * recycled by us when we call vnode_create
477 * which will result in a deadlock when we
478 * try to take the cnode lock in hfs_vnop_fsync or
479 * hfs_vnop_reclaim... vnode_get can be called here
480 * because we already hold the cnode lock which will
481 * prevent the vnode from changing identity until
482 * we drop it.. vnode_get will not block waiting for
483 * a change of state... however, it will return an
484 * error if the current iocount == 0 and we've already
485 * started to terminate the vnode... we don't need/want to
486 * grab an iocount in the case since we can't cause
487 * the fileystem to be re-entered on this thread for this vp
488 *
489 * the matching vnode_put will happen in hfs_unlock
490 * after we've dropped the cnode lock
491 */
492 if ( vnode_get(tvp) != 0)
493 {
494 cp->c_flag &= ~(C_NEED_RVNODE_PUT | C_NEED_DVNODE_PUT);
495 }
496 }
497 #endif
498
499 vfsp.vnfs_mp = mp;
500 vfsp.vnfs_vtype = vtype;
501 vfsp.vnfs_str = "hfs";
502 if ((cp->c_flag & C_HARDLINK) && (vtype == VDIR))
503 {
504 vfsp.vnfs_dvp = NULL; /* no parent for me! */
505 vfsp.vnfs_cnp = NULL; /* no name for me! */
506 }
507 else
508 {
509 vfsp.vnfs_dvp = dvp;
510 if (cnp)
511 {
512 vfsp.vnfs_cnp = hfs_malloc(sizeof(struct componentname));
513 if (vfsp.vnfs_cnp == NULL)
514 {
515 if (fp)
516 {
517 hfs_free(fp);
518 }
519 retval = ENOMEM;
520 goto gnv_exit;
521 }
522
523 memcpy((void*) vfsp.vnfs_cnp, (void*)cnp, sizeof(struct componentname));
524 vfsp.vnfs_cnp->cn_nameptr = lf_hfs_utils_allocate_and_copy_string( (char*) cnp->cn_nameptr, cnp->cn_namelen );
525
526 } else {
527 // Incase of ScanID of hardlinks, take the filename from the cnode
528 if (cp && cp->c_desc.cd_nameptr) {
529 vfsp.vnfs_cnp = hfs_malloc(sizeof(struct componentname));
530 if (vfsp.vnfs_cnp == NULL) {
531 if (fp) hfs_free(fp);
532 retval = ENOMEM;
533 goto gnv_exit;
534 }
535 bzero(vfsp.vnfs_cnp, sizeof(struct componentname));
536 vfsp.vnfs_cnp->cn_nameptr = lf_hfs_utils_allocate_and_copy_string( (char*) cp->c_desc.cd_nameptr, cp->c_desc.cd_namelen );
537 vfsp.vnfs_cnp->cn_namelen = cp->c_desc.cd_namelen;
538 }
539 }
540 }
541
542 vfsp.vnfs_fsnode = cp;
543 vfsp.vnfs_rdev = 0;
544
545 if (forkp)
546 {
547 vfsp.vnfs_filesize = forkp->cf_size;
548 }
549 else
550 {
551 vfsp.vnfs_filesize = 0;
552 }
553
554 if (cnp && cnp->cn_nameptr && cp->c_desc.cd_nameptr && strncmp((const char *)cnp->cn_nameptr, (const char *)cp->c_desc.cd_nameptr, cp->c_desc.cd_namelen) != 0)
555 {
556 //
557 // We don't want VFS to add an entry for this vnode because the name in the
558 // cnp does not match the bytes stored on disk for this file. Instead we'll
559 // update the identity later after the vnode is created and we'll do so with
560 // the correct bytes for this filename. For more details, see:
561 // <rdar://problem/8044697> FSEvents doesn't always decompose diacritical unicode chars in the paths of the changed directories
562 //
563 need_update_identity = 1;
564 }
565
566
567 /* Tag system files */
568 vfsp.vnfs_marksystem = issystemfile;
569
570 /* Tag root directory */
571 if (descp->cd_cnid == kHFSRootFolderID)
572 {
573 vfsp.vnfs_markroot = 1;
574 }
575 else
576 {
577 vfsp.vnfs_markroot = 0;
578 }
579
580 /*
581 * If provided_vp was non-NULL, then it is an already-allocated (but not
582 * initialized) vnode. We simply need to initialize it to this identity.
583 * If it was NULL, then assume that we need to call vnode_create with the
584 * normal arguments/types.
585 */
586 if (provided_vp)
587 {
588 vp = provided_vp;
589 /*
590 * After we assign the value of provided_vp into 'vp' (so that it can be
591 * mutated safely by vnode_initialize), we can NULL it out. At this point, the disposal
592 * and handling of the provided vnode will be the responsibility of VFS, which will
593 * clean it up and vnode_put it properly if vnode_initialize fails.
594 */
595 provided_vp = NULL;
596 retval = vnode_initialize (sizeof(struct vnode_fsparam), &vfsp, &vp);
597 /* See error handling below for resolving provided_vp */
598 }
599 else
600 {
601 /* Do a standard vnode_create */
602 retval = vnode_create (sizeof(struct vnode_fsparam), &vfsp, &vp);
603 }
604
605 /*
606 * We used a local variable to hold the result of vnode_create/vnode_initialize so that
607 * on error cases in vnode_create we won't accidentally harm the cnode's fields
608 */
609
610 if (retval)
611 {
612 /* Clean up if we encountered an error */
613 if (fp) {
614 if (fp == cp->c_datafork)
615 cp->c_datafork = NULL;
616 else
617 cp->c_rsrcfork = NULL;
618
619 hfs_free(fp);
620 }
621 /*
622 * If this is a newly created cnode or a vnode reclaim
623 * occurred during the attachment, then cleanup the cnode.
624 */
625 if ((cp->c_vp == NULL) && (cp->c_rsrc_vp == NULL))
626 {
627 hfs_chash_abort(hfsmp, cp);
628
629 if ((flags & GNV_SKIPLOCK) == 0)
630 {
631 hfs_unlock(cp);
632 }
633 hfs_reclaim_cnode(cp);
634 }
635 else
636 {
637 hfs_chashwakeup(hfsmp, cp, H_ALLOC | H_ATTACH);
638 if ((flags & GNV_SKIPLOCK) == 0)
639 {
640 hfs_unlock(cp);
641 }
642 }
643 *vpp = NULL;
644 goto gnv_exit;
645 }
646
647 /* If no error, then assign the value into the cnode's fields */
648 *cvpp = vp;
649
650 if (cp->c_flag & C_HARDLINK)
651 {
652 //TBD - this set is for vfs -> since we have the C_HARDLINK
653 // currently disable this set.
654 //vnode_setmultipath(vp);
655 }
656
657 if (vp && need_update_identity)
658 {
659 //
660 // As above, update the name of the vnode if the bytes stored in hfs do not match
661 // the bytes in the cnp. See this radar:
662 // <rdar://problem/8044697> FSEvents doesn't always decompose diacritical unicode chars in the paths of the changed directories
663 // for more details.
664 //
665 vnode_update_identity (vp, dvp, (const char *)cp->c_desc.cd_nameptr, cp->c_desc.cd_namelen, 0, VNODE_UPDATE_NAME);
666 }
667 /*
668 * Tag resource fork vnodes as needing an VNOP_INACTIVE
669 * so that any deferred removes (open unlinked files)
670 * have the chance to process the resource fork.
671 */
672 if (vp && VNODE_IS_RSRC(vp))
673 {
674 vnode_rele(vp);
675 }
676 hfs_chashwakeup(hfsmp, cp, H_ALLOC | H_ATTACH);
677
678 SET_NODE_AS_VALID(vp);
679 *vpp = vp;
680 retval = 0;
681
682 gnv_exit:
683 if (provided_vp)
684 {
685 /* Release our empty vnode if it was not used */
686 vnode_rele (provided_vp);
687 }
688 return retval;
689 }
690
691 /*
692 * Check ordering of two cnodes. Return true if they are are in-order.
693 */
694 static int
695 hfs_isordered(struct cnode *cp1, struct cnode *cp2)
696 {
697 if (cp1 == cp2)
698 return (0);
699 if (cp1 == NULL || cp2 == (struct cnode *)0xffffffff)
700 return (1);
701 if (cp2 == NULL || cp1 == (struct cnode *)0xffffffff)
702 return (0);
703 /*
704 * Locking order is cnode address order.
705 */
706 return (cp1 < cp2);
707 }
708
709 /*
710 * Acquire 4 cnode locks.
711 * - locked in cnode address order (lesser address first).
712 * - all or none of the locks are taken
713 * - only one lock taken per cnode (dup cnodes are skipped)
714 * - some of the cnode pointers may be null
715 */
716 int
717 hfs_lockfour(struct cnode *cp1, struct cnode *cp2, struct cnode *cp3,
718 struct cnode *cp4, enum hfs_locktype locktype, struct cnode **error_cnode)
719 {
720 struct cnode * a[3];
721 struct cnode * b[3];
722 struct cnode * list[4];
723 struct cnode * tmp;
724 int i, j, k;
725 int error;
726 if (error_cnode) {
727 *error_cnode = NULL;
728 }
729
730 if (hfs_isordered(cp1, cp2))
731 {
732 a[0] = cp1; a[1] = cp2;
733 }
734 else {
735 a[0] = cp2; a[1] = cp1;
736 }
737 if (hfs_isordered(cp3, cp4)) {
738 b[0] = cp3; b[1] = cp4;
739 } else {
740 b[0] = cp4; b[1] = cp3;
741 }
742 a[2] = (struct cnode *)0xffffffff; /* sentinel value */
743 b[2] = (struct cnode *)0xffffffff; /* sentinel value */
744
745 /*
746 * Build the lock list, skipping over duplicates
747 */
748 for (i = 0, j = 0, k = 0; (i < 2 || j < 2); ) {
749 tmp = hfs_isordered(a[i], b[j]) ? a[i++] : b[j++];
750 if (k == 0 || tmp != list[k-1])
751 list[k++] = tmp;
752 }
753
754 /*
755 * Now we can lock using list[0 - k].
756 * Skip over NULL entries.
757 */
758 for (i = 0; i < k; ++i) {
759 if (list[i])
760 if ((error = hfs_lock(list[i], locktype, HFS_LOCK_DEFAULT))) {
761 /* Only stuff error_cnode if requested */
762 if (error_cnode) {
763 *error_cnode = list[i];
764 }
765 /* Drop any locks we acquired. */
766 while (--i >= 0) {
767 if (list[i])
768 hfs_unlock(list[i]);
769 }
770 return (error);
771 }
772 }
773 return (0);
774 }
775
776 /*
777 * Unlock a group of cnodes.
778 */
779 void
780 hfs_unlockfour(struct cnode *cp1, struct cnode *cp2, struct cnode *cp3, struct cnode *cp4)
781 {
782 struct cnode * list[4];
783 int i, k = 0;
784
785 if (cp1) {
786 hfs_unlock(cp1);
787 list[k++] = cp1;
788 }
789 if (cp2) {
790 for (i = 0; i < k; ++i) {
791 if (list[i] == cp2)
792 goto skip1;
793 }
794 hfs_unlock(cp2);
795 list[k++] = cp2;
796 }
797 skip1:
798 if (cp3) {
799 for (i = 0; i < k; ++i) {
800 if (list[i] == cp3)
801 goto skip2;
802 }
803 hfs_unlock(cp3);
804 list[k++] = cp3;
805 }
806 skip2:
807 if (cp4) {
808 for (i = 0; i < k; ++i) {
809 if (list[i] == cp4)
810 return;
811 }
812 hfs_unlock(cp4);
813 }
814 }
815
816 /*
817 * Lock a cnode.
818 * N.B. If you add any failure cases, *make* sure hfs_lock_always works
819 */
820 int
821 hfs_lock(struct cnode *cp, enum hfs_locktype locktype, enum hfs_lockflags flags)
822 {
823 pthread_t thread = pthread_self();
824
825 if (cp->c_lockowner == thread)
826 {
827 /*
828 * Only the extents and bitmap files support lock recursion
829 * here. The other system files support lock recursion in
830 * hfs_systemfile_lock. Eventually, we should change to
831 * handle recursion solely in hfs_systemfile_lock.
832 */
833 if ((cp->c_fileid == kHFSExtentsFileID) || (cp->c_fileid == kHFSAllocationFileID))
834 {
835 cp->c_syslockcount++;
836 }
837 else
838 {
839 LFHFS_LOG(LEVEL_ERROR, "hfs_lock: locking against myself!");
840 hfs_assert(0);
841 }
842 }
843 else if (locktype == HFS_SHARED_LOCK)
844 {
845 lf_lck_rw_lock_shared(&cp->c_rwlock);
846 cp->c_lockowner = HFS_SHARED_OWNER;
847 }
848 else if (locktype == HFS_TRY_EXCLUSIVE_LOCK)
849 {
850 if (!lf_lck_rw_try_lock(&cp->c_rwlock, LCK_RW_TYPE_EXCLUSIVE))
851 {
852 cp->c_lockowner = thread;
853
854 /* Only the extents and bitmap files support lock recursion. */
855 if ((cp->c_fileid == kHFSExtentsFileID) || (cp->c_fileid == kHFSAllocationFileID))
856 {
857 cp->c_syslockcount = 1;
858 }
859 }
860 else
861 {
862 return (1);
863 }
864 }
865 else
866 { /* HFS_EXCLUSIVE_LOCK */
867 lf_lck_rw_lock_exclusive(&cp->c_rwlock);
868 cp->c_lockowner = thread;
869 /* Only the extents and bitmap files support lock recursion. */
870 if ((cp->c_fileid == kHFSExtentsFileID) || (cp->c_fileid == kHFSAllocationFileID))
871 {
872 cp->c_syslockcount = 1;
873 }
874 }
875
876 /*
877 * Skip cnodes for regular files that no longer exist
878 * (marked deleted, catalog entry gone).
879 */
880 if (((flags & HFS_LOCK_ALLOW_NOEXISTS) == 0) && ((cp->c_desc.cd_flags & CD_ISMETA) == 0) && (cp->c_flag & C_NOEXISTS))
881 {
882 hfs_unlock(cp);
883 return (ENOENT);
884 }
885 return (0);
886 }
887
888 /*
889 * Unlock a cnode.
890 */
891 void
892 hfs_unlock(struct cnode *cp)
893 {
894 u_int32_t c_flag = 0;
895
896 /*
897 * Only the extents and bitmap file's support lock recursion.
898 */
899 if ((cp->c_fileid == kHFSExtentsFileID) || (cp->c_fileid == kHFSAllocationFileID))
900 {
901 if (--cp->c_syslockcount > 0)
902 {
903 return;
904 }
905 }
906
907 pthread_t thread = pthread_self();
908
909 if (cp->c_lockowner == thread)
910 {
911 c_flag = cp->c_flag;
912
913 // If we have the truncate lock, we must defer the puts
914 if (cp->c_truncatelockowner == thread)
915 {
916 if (ISSET(c_flag, C_NEED_DVNODE_PUT)
917 && !cp->c_need_dvnode_put_after_truncate_unlock)
918 {
919 CLR(c_flag, C_NEED_DVNODE_PUT);
920 cp->c_need_dvnode_put_after_truncate_unlock = true;
921 }
922 if (ISSET(c_flag, C_NEED_RVNODE_PUT)
923 && !cp->c_need_rvnode_put_after_truncate_unlock)
924 {
925 CLR(c_flag, C_NEED_RVNODE_PUT);
926 cp->c_need_rvnode_put_after_truncate_unlock = true;
927 }
928 }
929
930 CLR(cp->c_flag, (C_NEED_DATA_SETSIZE | C_NEED_RSRC_SETSIZE | C_NEED_DVNODE_PUT | C_NEED_RVNODE_PUT));
931
932 cp->c_lockowner = NULL;
933 lf_lck_rw_unlock_exclusive(&cp->c_rwlock);
934 }
935 else
936 {
937 cp->c_lockowner = NULL;
938 lf_lck_rw_unlock_shared(&cp->c_rwlock);
939 }
940
941 #if LF_HFS_FULL_VNODE_SUPPORT
942 /* Perform any vnode post processing after cnode lock is dropped. */
943 if (vp)
944 {
945 if (c_flag & C_NEED_DATA_SETSIZE)
946 {
947 ubc_setsize(vp, VTOF(vp)->ff_size);
948 }
949 if (c_flag & C_NEED_DVNODE_PUT)
950 {
951 vnode_put(vp);
952 }
953 }
954 if (rvp)
955 {
956 if (c_flag & C_NEED_RSRC_SETSIZE)
957 {
958 ubc_setsize(rvp, VTOF(rvp)->ff_size);
959 }
960 if (c_flag & C_NEED_RVNODE_PUT)
961 {
962 vnode_put(rvp);
963 }
964 }
965 #endif
966 }
967
968 /*
969 * hfs_valid_cnode
970 *
971 * This function is used to validate data that is stored in-core against what is contained
972 * in the catalog. Common uses include validating that the parent-child relationship still exist
973 * for a specific directory entry (guaranteeing it has not been renamed into a different spot) at
974 * the point of the check.
975 */
976 int
977 hfs_valid_cnode(struct hfsmount *hfsmp, struct vnode *dvp, struct componentname *cnp, cnid_t cnid, struct cat_attr *cattr, int *error)
978 {
979 struct cat_attr attr;
980 struct cat_desc cndesc;
981 int stillvalid = 0;
982
983 /* System files are always valid */
984 if (cnid < kHFSFirstUserCatalogNodeID)
985 {
986 *error = 0;
987 return (1);
988 }
989
990 /* XXX optimization: check write count in dvp */
991 int lockflags = hfs_systemfile_lock(hfsmp, SFL_CATALOG, HFS_SHARED_LOCK);
992
993 if (dvp && cnp)
994 {
995 int lookup = 0;
996 struct cat_fork fork;
997 bzero(&cndesc, sizeof(cndesc));
998 cndesc.cd_nameptr = (const u_int8_t *)cnp->cn_nameptr;
999 cndesc.cd_namelen = cnp->cn_namelen;
1000 cndesc.cd_parentcnid = VTOC(dvp)->c_fileid;
1001 cndesc.cd_hint = VTOC(dvp)->c_childhint;
1002
1003 /*
1004 * We have to be careful when calling cat_lookup. The result argument
1005 * 'attr' may get different results based on whether or not you ask
1006 * for the filefork to be supplied as output. This is because cat_lookupbykey
1007 * will attempt to do basic validation/smoke tests against the resident
1008 * extents if there are no overflow extent records, but it needs someplace
1009 * in memory to store the on-disk fork structures.
1010 *
1011 * Since hfs_lookup calls cat_lookup with a filefork argument, we should
1012 * do the same here, to verify that block count differences are not
1013 * due to calling the function with different styles. cat_lookupbykey
1014 * will request the volume be fsck'd if there is true on-disk corruption
1015 * where the number of blocks does not match the number generated by
1016 * summing the number of blocks in the resident extents.
1017 */
1018 lookup = cat_lookup (hfsmp, &cndesc, 0, NULL, &attr, &fork, NULL);
1019
1020 if ((lookup == 0) && (cnid == attr.ca_fileid))
1021 {
1022 stillvalid = 1;
1023 *error = 0;
1024 }
1025 else
1026 {
1027 *error = ENOENT;
1028 }
1029 /*
1030 * In hfs_getnewvnode, we may encounter a time-of-check vs. time-of-vnode creation
1031 * race. Specifically, if there is no vnode/cnode pair for the directory entry
1032 * being looked up, we have to go to the catalog. But since we don't hold any locks (aside
1033 * from the dvp in 'shared' mode) there is nothing to protect us against the catalog record
1034 * changing in between the time we do the cat_lookup there and the time we re-grab the
1035 * catalog lock above to do another cat_lookup.
1036 *
1037 * However, we need to check more than just the CNID and parent-child name relationships above.
1038 * Hardlinks can suffer the same race in the following scenario: Suppose we do a
1039 * cat_lookup, and find a leaf record and a raw inode for a hardlink. Now, we have
1040 * the cat_attr in hand (passed in above). But in between then and now, the vnode was
1041 * created by a competing hfs_getnewvnode call, and is manipulated and reclaimed before we get
1042 * a chance to do anything. This is possible if there are a lot of threads thrashing around
1043 * with the cnode hash. In this case, if we don't check/validate the cat_attr in-hand, we will
1044 * blindly stuff it into the cnode, which will make the in-core data inconsistent with what is
1045 * on disk. So validate the cat_attr below, if required. This race cannot happen if the cnode/vnode
1046 * already exists, as it does in the case of rename and delete.
1047 */
1048 if (stillvalid && cattr != NULL)
1049 {
1050 if (cattr->ca_linkcount != attr.ca_linkcount)
1051 {
1052 stillvalid = 0;
1053 *error = ERECYCLE;
1054 goto notvalid;
1055 }
1056
1057 if (cattr->ca_union1.cau_linkref != attr.ca_union1.cau_linkref)
1058 {
1059 stillvalid = 0;
1060 *error = ERECYCLE;
1061 goto notvalid;
1062 }
1063
1064 if (cattr->ca_union3.cau_firstlink != attr.ca_union3.cau_firstlink)
1065 {
1066 stillvalid = 0;
1067 *error = ERECYCLE;
1068 goto notvalid;
1069 }
1070 if (cattr->ca_union2.cau_blocks != attr.ca_union2.cau_blocks)
1071 {
1072 stillvalid = 0;
1073 *error = ERECYCLE;
1074 goto notvalid;
1075 }
1076 }
1077 }
1078 else
1079 {
1080 if (cat_idlookup(hfsmp, cnid, 0, 0, NULL, NULL, NULL) == 0)
1081 {
1082 stillvalid = 1;
1083 *error = 0;
1084 }
1085 else
1086 {
1087 *error = ENOENT;
1088 }
1089 }
1090
1091 notvalid:
1092 hfs_systemfile_unlock(hfsmp, lockflags);
1093
1094 return (stillvalid);
1095 }
1096
1097 /*
1098 * Protect a cnode against a truncation.
1099 *
1100 * Used mainly by read/write since they don't hold the
1101 * cnode lock across calls to the cluster layer.
1102 *
1103 * The process doing a truncation must take the lock
1104 * exclusive. The read/write processes can take it
1105 * shared. The locktype argument is the same as supplied to
1106 * hfs_lock.
1107 */
1108 void
1109 hfs_lock_truncate(struct cnode *cp, enum hfs_locktype locktype, enum hfs_lockflags flags)
1110 {
1111 pthread_t thread = pthread_self();
1112
1113 if (cp->c_truncatelockowner == thread) {
1114 /*
1115 * Ignore grabbing the lock if it the current thread already
1116 * holds exclusive lock.
1117 *
1118 * This is needed on the hfs_vnop_pagein path where we need to ensure
1119 * the file does not change sizes while we are paging in. However,
1120 * we may already hold the lock exclusive due to another
1121 * VNOP from earlier in the call stack. So if we already hold
1122 * the truncate lock exclusive, allow it to proceed, but ONLY if
1123 * it's in the recursive case.
1124 */
1125 if ((flags & HFS_LOCK_SKIP_IF_EXCLUSIVE) == 0)
1126 {
1127 LFHFS_LOG(LEVEL_ERROR, "hfs_lock_truncate: cnode %p locked!", cp);
1128 hfs_assert(0);
1129 }
1130 } else if (locktype == HFS_SHARED_LOCK) {
1131 lf_lck_rw_lock_shared(&cp->c_truncatelock);
1132 cp->c_truncatelockowner = HFS_SHARED_OWNER;
1133 } else { /* HFS_EXCLUSIVE_LOCK */
1134 lf_lck_rw_lock_exclusive(&cp->c_truncatelock);
1135 cp->c_truncatelockowner = thread;
1136 }
1137 }
1138
1139 /*
1140 * Unlock the truncate lock, which protects against size changes.
1141 *
1142 * If HFS_LOCK_SKIP_IF_EXCLUSIVE flag was set, it means that a previous
1143 * hfs_lock_truncate() might have skipped grabbing a lock because
1144 * the current thread was already holding the lock exclusive and
1145 * we may need to return from this function without actually unlocking
1146 * the truncate lock.
1147 */
1148 void
1149 hfs_unlock_truncate(struct cnode *cp, enum hfs_lockflags flags)
1150 {
1151 pthread_t thread = pthread_self();
1152
1153 /*
1154 * If HFS_LOCK_SKIP_IF_EXCLUSIVE is set in the flags AND the current
1155 * lock owner of the truncate lock is our current thread, then
1156 * we must have skipped taking the lock earlier by in
1157 * hfs_lock_truncate() by setting HFS_LOCK_SKIP_IF_EXCLUSIVE in the
1158 * flags (as the current thread was current lock owner).
1159 *
1160 * If HFS_LOCK_SKIP_IF_EXCLUSIVE is not set (most of the time) then
1161 * we check the lockowner field to infer whether the lock was taken
1162 * exclusively or shared in order to know what underlying lock
1163 * routine to call.
1164 */
1165 if (flags & HFS_LOCK_SKIP_IF_EXCLUSIVE) {
1166 if (cp->c_truncatelockowner == thread) {
1167 return;
1168 }
1169 }
1170
1171 /* HFS_LOCK_EXCLUSIVE */
1172 if (thread == cp->c_truncatelockowner) {
1173 // vnode_t vp = NULL, rvp = NULL;
1174
1175 /*
1176 * If there are pending set sizes, the cnode lock should be dropped
1177 * first.
1178 */
1179 hfs_assert(!(cp->c_lockowner == thread
1180 && ISSET(cp->c_flag, C_NEED_DATA_SETSIZE | C_NEED_RSRC_SETSIZE)));
1181
1182 // if (cp->c_need_dvnode_put_after_truncate_unlock) {
1183 // vp = cp->c_vp;
1184 // cp->c_need_dvnode_put_after_truncate_unlock = false;
1185 // }
1186 // if (cp->c_need_rvnode_put_after_truncate_unlock) {
1187 // rvp = cp->c_rsrc_vp;
1188 // cp->c_need_rvnode_put_after_truncate_unlock = false;
1189 // }
1190
1191 cp->c_truncatelockowner = NULL;
1192 lf_lck_rw_unlock_exclusive(&cp->c_truncatelock);
1193 //
1194 // // Do the puts now
1195 // if (vp)
1196 // vnode_put(vp);
1197 // if (rvp)
1198 // vnode_put(rvp);
1199 } else
1200 { /* HFS_LOCK_SHARED */
1201 lf_lck_rw_unlock_shared(&cp->c_truncatelock);
1202 }
1203 }
1204
1205 /*
1206 * Lock a pair of cnodes.
1207 */
1208 int
1209 hfs_lockpair(struct cnode *cp1, struct cnode *cp2, enum hfs_locktype locktype)
1210 {
1211 struct cnode *first, *last;
1212 int error;
1213
1214 /*
1215 * If cnodes match then just lock one.
1216 */
1217 if (cp1 == cp2)
1218 {
1219 return hfs_lock(cp1, locktype, HFS_LOCK_DEFAULT);
1220 }
1221
1222 /*
1223 * Lock in cnode address order.
1224 */
1225 if (cp1 < cp2)
1226 {
1227 first = cp1;
1228 last = cp2;
1229 }
1230 else
1231 {
1232 first = cp2;
1233 last = cp1;
1234 }
1235
1236 if ( (error = hfs_lock(first, locktype, HFS_LOCK_DEFAULT)))
1237 {
1238 return (error);
1239 }
1240 if ( (error = hfs_lock(last, locktype, HFS_LOCK_DEFAULT)))
1241 {
1242 hfs_unlock(first);
1243 return (error);
1244 }
1245 return (0);
1246 }
1247
1248 /*
1249 * Unlock a pair of cnodes.
1250 */
1251 void
1252 hfs_unlockpair(struct cnode *cp1, struct cnode *cp2)
1253 {
1254 hfs_unlock(cp1);
1255 if (cp2 != cp1)
1256 hfs_unlock(cp2);
1257 }
1258
1259 /*
1260 * Increase the gen count by 1; if it wraps around to 0, increment by
1261 * two. The cnode *must* be locked exclusively by the caller.
1262 *
1263 * You may think holding the lock is unnecessary because we only need
1264 * to change the counter, but consider this sequence of events: thread
1265 * A calls hfs_incr_gencount and the generation counter is 2 upon
1266 * entry. A context switch occurs and thread B increments the counter
1267 * to 3, thread C now gets the generation counter (for whatever
1268 * purpose), and then another thread makes another change and the
1269 * generation counter is incremented again---it's now 4. Now thread A
1270 * continues and it sets the generation counter back to 3. So you can
1271 * see, thread C would miss the change that caused the generation
1272 * counter to increment to 4 and for this reason the cnode *must*
1273 * always be locked exclusively.
1274 */
1275 uint32_t hfs_incr_gencount (struct cnode *cp)
1276 {
1277 u_int8_t *finfo = NULL;
1278 u_int32_t gcount = 0;
1279
1280 /* overlay the FinderInfo to the correct pointer, and advance */
1281 finfo = (u_int8_t*)cp->c_finderinfo;
1282 finfo = finfo + 16;
1283
1284 /*
1285 * FinderInfo is written out in big endian... make sure to convert it to host
1286 * native before we use it.
1287 *
1288 * NOTE: the write_gen_counter is stored in the same location in both the
1289 * FndrExtendedFileInfo and FndrExtendedDirInfo structs (it's the
1290 * last 32-bit word) so it is safe to have one code path here.
1291 */
1292 if (S_ISDIR(cp->c_attr.ca_mode) || S_ISREG(cp->c_attr.ca_mode))
1293 {
1294 struct FndrExtendedFileInfo *extinfo = (struct FndrExtendedFileInfo *)finfo;
1295 gcount = extinfo->write_gen_counter;
1296
1297 /* Was it zero to begin with (file originated in 10.8 or earlier?) */
1298 if (gcount == 0)
1299 {
1300 gcount++;
1301 }
1302
1303 /* now bump it */
1304 gcount++;
1305
1306 /* Did it wrap around ? */
1307 if (gcount == 0)
1308 {
1309 gcount++;
1310 }
1311 extinfo->write_gen_counter = OSSwapHostToBigInt32 (gcount);
1312
1313 SET(cp->c_flag, C_MINOR_MOD);
1314 }
1315 else
1316 {
1317 gcount = 0;
1318 }
1319
1320 return gcount;
1321 }
1322
1323 void hfs_write_gencount (struct cat_attr *attrp, uint32_t gencount)
1324 {
1325 u_int8_t *finfo = NULL;
1326
1327 /* overlay the FinderInfo to the correct pointer, and advance */
1328 finfo = (u_int8_t*)attrp->ca_finderinfo;
1329 finfo = finfo + 16;
1330
1331 /*
1332 * Make sure to write it out as big endian, since that's how
1333 * finder info is defined.
1334 *
1335 * Generation count is only supported for files.
1336 */
1337 if (S_ISREG(attrp->ca_mode)) {
1338 struct FndrExtendedFileInfo *extinfo = (struct FndrExtendedFileInfo *)finfo;
1339 extinfo->write_gen_counter = OSSwapHostToBigInt32(gencount);
1340 }
1341
1342 /* If it were neither directory/file, then we'd bail out */
1343 return;
1344 }
1345
1346 void hfs_clear_might_be_dirty_flag(cnode_t *cp)
1347 {
1348 /*
1349 * If we're about to touch both mtime and ctime, we can clear the
1350 * C_MIGHT_BE_DIRTY_FROM_MAPPING since we can guarantee that
1351 * subsequent page-outs can only be for data made dirty before
1352 * now.
1353 */
1354 CLR(cp->c_flag, C_MIGHT_BE_DIRTY_FROM_MAPPING);
1355 }
1356
1357 /*
1358 * Touch cnode times based on c_touch_xxx flags
1359 *
1360 * cnode must be locked exclusive
1361 *
1362 * This will also update the volume modify time
1363 */
1364 void
1365 hfs_touchtimes(struct hfsmount *hfsmp, struct cnode* cp)
1366 {
1367
1368 if (ISSET(hfsmp->hfs_flags, HFS_READ_ONLY) || ISSET(cp->c_flag, C_NOEXISTS)) {
1369 cp->c_touch_acctime = FALSE;
1370 cp->c_touch_chgtime = FALSE;
1371 cp->c_touch_modtime = FALSE;
1372 CLR(cp->c_flag, C_NEEDS_DATEADDED);
1373 return;
1374 }
1375
1376 if (cp->c_touch_acctime || cp->c_touch_chgtime ||
1377 cp->c_touch_modtime || (cp->c_flag & C_NEEDS_DATEADDED)) {
1378 struct timeval tv;
1379 int touchvol = 0;
1380
1381 if (cp->c_touch_modtime && cp->c_touch_chgtime)
1382 hfs_clear_might_be_dirty_flag(cp);
1383
1384 microtime(&tv);
1385
1386 if (cp->c_touch_acctime) {
1387 /*
1388 * When the access time is the only thing changing, we
1389 * won't necessarily write it to disk immediately. We
1390 * only do the atime update at vnode recycle time, when
1391 * fsync is called or when there's another reason to write
1392 * to the metadata.
1393 */
1394 cp->c_atime = tv.tv_sec;
1395 cp->c_touch_acctime = FALSE;
1396 }
1397 if (cp->c_touch_modtime) {
1398 cp->c_touch_modtime = FALSE;
1399 time_t new_time = tv.tv_sec;
1400 if (cp->c_mtime != new_time) {
1401 cp->c_mtime = new_time;
1402 cp->c_flag |= C_MINOR_MOD;
1403 touchvol = 1;
1404 }
1405 }
1406 if (cp->c_touch_chgtime) {
1407 cp->c_touch_chgtime = FALSE;
1408 if (cp->c_ctime != tv.tv_sec) {
1409 cp->c_ctime = tv.tv_sec;
1410 cp->c_flag |= C_MINOR_MOD;
1411 touchvol = 1;
1412 }
1413 }
1414
1415 if (cp->c_flag & C_NEEDS_DATEADDED) {
1416 hfs_write_dateadded (&(cp->c_attr), tv.tv_sec);
1417 cp->c_flag |= C_MINOR_MOD;
1418 /* untwiddle the bit */
1419 cp->c_flag &= ~C_NEEDS_DATEADDED;
1420 touchvol = 1;
1421 }
1422
1423 /* Touch the volume modtime if needed */
1424 if (touchvol) {
1425 hfs_note_header_minor_change(hfsmp);
1426 HFSTOVCB(hfsmp)->vcbLsMod = tv.tv_sec;
1427 }
1428 }
1429 }
1430
1431 /*
1432 * Per HI and Finder requirements, HFS should add in the
1433 * date/time that a particular directory entry was added
1434 * to the containing directory.
1435 * This is stored in the extended Finder Info for the
1436 * item in question.
1437 *
1438 * Note that this field is also set explicitly in the hfs_vnop_setxattr code.
1439 * We must ignore user attempts to set this part of the finderinfo, and
1440 * so we need to save a local copy of the date added, write in the user
1441 * finderinfo, then stuff the value back in.
1442 */
1443 void hfs_write_dateadded (struct cat_attr *attrp, uint64_t dateadded)
1444 {
1445 u_int8_t *finfo = NULL;
1446
1447 /* overlay the FinderInfo to the correct pointer, and advance */
1448 finfo = (u_int8_t*)attrp->ca_finderinfo;
1449 finfo = finfo + 16;
1450
1451 /*
1452 * Make sure to write it out as big endian, since that's how
1453 * finder info is defined.
1454 *
1455 * NOTE: This is a Unix-epoch timestamp, not a HFS/Traditional Mac timestamp.
1456 */
1457 if (S_ISREG(attrp->ca_mode)) {
1458 struct FndrExtendedFileInfo *extinfo = (struct FndrExtendedFileInfo *)finfo;
1459 extinfo->date_added = OSSwapHostToBigInt32(dateadded);
1460 attrp->ca_recflags |= kHFSHasDateAddedMask;
1461 }
1462 else if (S_ISDIR(attrp->ca_mode)) {
1463 struct FndrExtendedDirInfo *extinfo = (struct FndrExtendedDirInfo *)finfo;
1464 extinfo->date_added = OSSwapHostToBigInt32(dateadded);
1465 attrp->ca_recflags |= kHFSHasDateAddedMask;
1466 }
1467 /* If it were neither directory/file, then we'd bail out */
1468 return;
1469 }
1470
1471 static u_int32_t
1472 hfs_get_dateadded_internal(const uint8_t *finderinfo, mode_t mode)
1473 {
1474 const uint8_t *finfo = NULL;
1475 u_int32_t dateadded = 0;
1476
1477 /* overlay the FinderInfo to the correct pointer, and advance */
1478 finfo = finderinfo + 16;
1479
1480 /*
1481 * FinderInfo is written out in big endian... make sure to convert it to host
1482 * native before we use it.
1483 */
1484 if (S_ISREG(mode)) {
1485 const struct FndrExtendedFileInfo *extinfo = (const struct FndrExtendedFileInfo *)finfo;
1486 dateadded = OSSwapBigToHostInt32 (extinfo->date_added);
1487 }
1488 else if (S_ISDIR(mode)) {
1489 const struct FndrExtendedDirInfo *extinfo = (const struct FndrExtendedDirInfo *)finfo;
1490 dateadded = OSSwapBigToHostInt32 (extinfo->date_added);
1491 }
1492
1493 return dateadded;
1494 }
1495
1496 u_int32_t
1497 hfs_get_dateadded(struct cnode *cp)
1498 {
1499 if ((cp->c_attr.ca_recflags & kHFSHasDateAddedMask) == 0) {
1500 /* Date added was never set. Return 0. */
1501 return (0);
1502 }
1503
1504 return (hfs_get_dateadded_internal((u_int8_t*)cp->c_finderinfo,
1505 cp->c_attr.ca_mode));
1506 }
1507
1508 static bool
1509 hfs_cnode_isinuse(struct cnode *cp, uint32_t uRefCount)
1510 {
1511 return (cp->uOpenLookupRefCount > uRefCount);
1512 }
1513
1514 /*
1515 * hfs_cnode_teardown
1516 *
1517 * This is an internal function that is invoked from both hfs_vnop_inactive
1518 * and hfs_vnop_reclaim. As VNOP_INACTIVE is not necessarily called from vnodes
1519 * being recycled and reclaimed, it is important that we do any post-processing
1520 * necessary for the cnode in both places. Important tasks include things such as
1521 * releasing the blocks from an open-unlinked file when all references to it have dropped,
1522 * and handling resource forks separately from data forks.
1523 *
1524 * Note that we take only the vnode as an argument here (rather than the cnode).
1525 * Recall that each cnode supports two forks (rsrc/data), and we can always get the right
1526 * cnode from either of the vnodes, but the reverse is not true -- we can't determine which
1527 * vnode we need to reclaim if only the cnode is supplied.
1528 *
1529 * This function is idempotent and safe to call from both hfs_vnop_inactive and hfs_vnop_reclaim
1530 * if both are invoked right after the other. In the second call, most of this function's if()
1531 * conditions will fail, since they apply generally to cnodes still marked with C_DELETED.
1532 * As a quick check to see if this function is necessary, determine if the cnode is already
1533 * marked C_NOEXISTS. If it is, then it is safe to skip this function. The only tasks that
1534 * remain for cnodes marked in such a fashion is to teardown their fork references and
1535 * release all directory hints and hardlink origins. However, both of those are done
1536 * in hfs_vnop_reclaim. hfs_update, by definition, is not necessary if the cnode's catalog
1537 * entry is no longer there.
1538 *
1539 * 'reclaim' argument specifies whether or not we were called from hfs_vnop_reclaim. If we are
1540 * invoked from hfs_vnop_reclaim, we can not call functions that cluster_push since the UBC info
1541 * is totally gone by that point.
1542 *
1543 * Assumes that both truncate and cnode locks for 'cp' are held.
1544 */
1545 static int
1546 hfs_cnode_teardown (struct vnode *vp, int reclaim)
1547 {
1548 int forkcount = 0;
1549 enum vtype v_type = vp->sFSParams.vnfs_vtype;
1550 struct cnode* cp = VTOC(vp);
1551 int error = 0;
1552 bool started_tr = false;
1553 struct hfsmount *hfsmp = VTOHFS(vp);
1554 int truncated = 0;
1555 cat_cookie_t cookie;
1556 int cat_reserve = 0;
1557 int lockflags = 0;
1558 int ea_error = 0;
1559
1560 if (cp->c_datafork) {
1561 ++forkcount;
1562 }
1563 if (cp->c_rsrcfork) {
1564 ++forkcount;
1565 }
1566
1567 /*
1568 * Remove any directory hints or cached origins
1569 */
1570 if (v_type == VDIR) {
1571 hfs_reldirhints(cp, 0);
1572 }
1573 if (cp->c_flag & C_HARDLINK) {
1574 hfs_relorigins(cp);
1575 }
1576 /*
1577 * -- Handle open unlinked files --
1578 *
1579 * If the vnode is in use, it means a force unmount is in progress
1580 * in which case we defer cleaning up until either we come back
1581 * through here via hfs_vnop_reclaim, at which point the UBC
1582 * information will have been torn down and the vnode might no
1583 * longer be in use, or if it's still in use, it will get cleaned
1584 * up when next remounted.
1585 */
1586 if (ISSET(cp->c_flag, C_DELETED) && !hfs_cnode_isinuse(cp, 0)) {
1587 /*
1588 * This check is slightly complicated. We should only truncate data
1589 * in very specific cases for open-unlinked files. This is because
1590 * we want to ensure that the resource fork continues to be available
1591 * if the caller has the data fork open. However, this is not symmetric;
1592 * someone who has the resource fork open need not be able to access the data
1593 * fork once the data fork has gone inactive.
1594 *
1595 * If we're the last fork, then we have cleaning up to do.
1596 *
1597 * A) last fork, and vp == c_vp
1598 * Truncate away own fork data. If rsrc fork is not in core, truncate it too.
1599 *
1600 * B) last fork, and vp == c_rsrc_vp
1601 * Truncate ourselves, assume data fork has been cleaned due to C).
1602 *
1603 * If we're not the last fork, then things are a little different:
1604 *
1605 * C) not the last fork, vp == c_vp
1606 * Truncate ourselves. Once the file has gone out of the namespace,
1607 * it cannot be further opened. Further access to the rsrc fork may
1608 * continue, however.
1609 *
1610 * D) not the last fork, vp == c_rsrc_vp
1611 * Don't enter the block below, just clean up vnode and push it out of core.
1612 */
1613
1614 if ((v_type == VREG || v_type == VLNK) &&
1615 ((forkcount == 1) || (!VNODE_IS_RSRC(vp)))) {
1616
1617 /* Truncate away our own fork data. (Case A, B, C above) */
1618 if (VTOF(vp) && VTOF(vp)->ff_blocks != 0) {
1619 /*
1620 * SYMLINKS only:
1621 *
1622 * Encapsulate the entire change (including truncating the link) in
1623 * nested transactions if we are modifying a symlink, because we know that its
1624 * file length will be at most 4k, and we can fit both the truncation and
1625 * any relevant bitmap changes into a single journal transaction. We also want
1626 * the kill_block code to execute in the same transaction so that any dirty symlink
1627 * blocks will not be written. Otherwise, rely on
1628 * hfs_truncate doing its own transactions to ensure that we don't blow up
1629 * the journal.
1630 */
1631 if (!started_tr && (v_type == VLNK)) {
1632 if (hfs_start_transaction(hfsmp) != 0) {
1633 error = EINVAL;
1634 goto out;
1635 }
1636 else {
1637 started_tr = true;
1638 }
1639 }
1640
1641 /*
1642 * At this point, we have decided that this cnode is
1643 * suitable for full removal. We are about to deallocate
1644 * its blocks and remove its entry from the catalog.
1645 * If it was a symlink, then it's possible that the operation
1646 * which created it is still in the current transaction group
1647 * due to coalescing. Take action here to kill the data blocks
1648 * of the symlink out of the journal before moving to
1649 * deallocate the blocks. We need to be in the middle of
1650 * a transaction before calling buf_iterate like this.
1651 *
1652 * Note: we have to kill any potential symlink buffers out of
1653 * the journal prior to deallocating their blocks. This is so
1654 * that we don't race with another thread that may be doing an
1655 * an allocation concurrently and pick up these blocks. It could
1656 * generate I/O against them which could go out ahead of our journal
1657 * transaction.
1658 */
1659
1660 if (hfsmp->jnl && vnode_islnk(vp)) {
1661 lf_hfs_generic_buf_write_iterate(vp, hfs_removefile_callback, BUF_SKIP_NONLOCKED, (void *)hfsmp);
1662 }
1663
1664 /*
1665 * This truncate call (and the one below) is fine from VNOP_RECLAIM's
1666 * context because we're only removing blocks, not zero-filling new
1667 * ones. The C_DELETED check above makes things much simpler.
1668 */
1669 error = hfs_truncate(vp, (off_t)0, IO_NDELAY, 0);
1670 if (error) {
1671 goto out;
1672 }
1673 truncated = 1;
1674
1675 /* (SYMLINKS ONLY): Close/End our transaction after truncating the file record */
1676 if (started_tr) {
1677 hfs_end_transaction(hfsmp);
1678 started_tr = false;
1679 }
1680
1681 }
1682
1683 /*
1684 * Truncate away the resource fork, if we represent the data fork and
1685 * it is the last fork. That means, by definition, the rsrc fork is not in
1686 * core. To avoid bringing a vnode into core for the sole purpose of deleting the
1687 * data in the resource fork, we call cat_lookup directly, then hfs_release_storage
1688 * to get rid of the resource fork's data. Note that because we are holding the
1689 * cnode lock, it is impossible for a competing thread to create the resource fork
1690 * vnode from underneath us while we do this.
1691 *
1692 * This is invoked via case A above only.
1693 */
1694 if ((cp->c_blocks > 0) && (forkcount == 1) && (vp != cp->c_rsrc_vp)) {
1695 struct cat_lookup_buffer *lookup_rsrc = NULL;
1696 struct cat_desc *desc_ptr = NULL;
1697
1698 lookup_rsrc = hfs_mallocz(sizeof(struct cat_lookup_buffer));
1699
1700 if (cp->c_desc.cd_namelen == 0) {
1701 /* Initialize the rsrc descriptor for lookup if necessary*/
1702 MAKE_DELETED_NAME (lookup_rsrc->lookup_name, HFS_TEMPLOOKUP_NAMELEN, cp->c_fileid);
1703
1704 lookup_rsrc->lookup_desc.cd_nameptr = (const uint8_t*) lookup_rsrc->lookup_name;
1705 lookup_rsrc->lookup_desc.cd_namelen = strlen (lookup_rsrc->lookup_name);
1706 lookup_rsrc->lookup_desc.cd_parentcnid = hfsmp->hfs_private_desc[FILE_HARDLINKS].cd_cnid;
1707 lookup_rsrc->lookup_desc.cd_cnid = cp->c_cnid;
1708
1709 desc_ptr = &lookup_rsrc->lookup_desc;
1710 }
1711 else {
1712 desc_ptr = &cp->c_desc;
1713 }
1714
1715 lockflags = hfs_systemfile_lock (hfsmp, SFL_CATALOG, HFS_SHARED_LOCK);
1716
1717 error = cat_lookup (hfsmp, desc_ptr, 1, (struct cat_desc *) NULL, (struct cat_attr*) NULL, &lookup_rsrc->lookup_fork.ff_data, NULL);
1718
1719 hfs_systemfile_unlock (hfsmp, lockflags);
1720
1721 if (error) {
1722 hfs_free(lookup_rsrc);
1723 goto out;
1724 }
1725
1726 /*
1727 * Make the filefork in our temporary struct look like a real
1728 * filefork. Fill in the cp, sysfileinfo and rangelist fields..
1729 */
1730 rl_init (&lookup_rsrc->lookup_fork.ff_invalidranges);
1731 lookup_rsrc->lookup_fork.ff_cp = cp;
1732
1733 /*
1734 * If there were no errors, then we have the catalog's fork information
1735 * for the resource fork in question. Go ahead and delete the data in it now.
1736 */
1737
1738 error = hfs_release_storage (hfsmp, NULL, &lookup_rsrc->lookup_fork, cp->c_fileid);
1739 hfs_free(lookup_rsrc);
1740
1741 if (error) {
1742 goto out;
1743 }
1744
1745 /*
1746 * This fileid's resource fork extents have now been fully deleted on-disk
1747 * and this CNID is no longer valid. At this point, we should be able to
1748 * zero out cp->c_blocks to indicate there is no data left in this file.
1749 */
1750 cp->c_blocks = 0;
1751 }
1752 }
1753
1754 /*
1755 * If we represent the last fork (or none in the case of a dir),
1756 * and the cnode has become open-unlinked...
1757 *
1758 * We check c_blocks here because it is possible in the force
1759 * unmount case for the data fork to be in use but the resource
1760 * fork to not be in use in which case we will truncate the
1761 * resource fork, but not the data fork. It will get cleaned
1762 * up upon next mount.
1763 */
1764 if (forkcount <= 1 && !cp->c_blocks) {
1765 /*
1766 * If it has EA's, then we need to get rid of them.
1767 *
1768 * Note that this must happen outside of any other transactions
1769 * because it starts/ends its own transactions and grabs its
1770 * own locks. This is to prevent a file with a lot of attributes
1771 * from creating a transaction that is too large (which panics).
1772 */
1773 if (ISSET(cp->c_attr.ca_recflags, kHFSHasAttributesMask))
1774 {
1775 ea_error = hfs_removeallattr(hfsmp, cp->c_fileid, &started_tr);
1776 if (ea_error)
1777 goto out;
1778 }
1779
1780 /*
1781 * Remove the cnode's catalog entry and release all blocks it
1782 * may have been using.
1783 */
1784
1785 /*
1786 * Mark cnode in transit so that no one can get this
1787 * cnode from cnode hash.
1788 */
1789 // hfs_chash_mark_in_transit(hfsmp, cp);
1790 // XXXdbg - remove the cnode from the hash table since it's deleted
1791 // otherwise someone could go to sleep on the cnode and not
1792 // be woken up until this vnode gets recycled which could be
1793 // a very long time...
1794 hfs_chashremove(hfsmp, cp);
1795
1796 cp->c_flag |= C_NOEXISTS; // XXXdbg
1797 cp->c_rdev = 0;
1798
1799 if (!started_tr) {
1800 if (hfs_start_transaction(hfsmp) != 0) {
1801 error = EINVAL;
1802 goto out;
1803 }
1804 started_tr = true;
1805 }
1806
1807 /*
1808 * Reserve some space in the Catalog file.
1809 */
1810 if ((error = cat_preflight(hfsmp, CAT_DELETE, &cookie))) {
1811 goto out;
1812 }
1813 cat_reserve = 1;
1814
1815 lockflags = hfs_systemfile_lock(hfsmp, SFL_CATALOG | SFL_ATTRIBUTE, HFS_EXCLUSIVE_LOCK);
1816
1817 if (cp->c_blocks > 0) {
1818 LFHFS_LOG(LEVEL_ERROR, "hfs_inactive: deleting non-empty%sfile %d, "
1819 "blks %d\n", VNODE_IS_RSRC(vp) ? " rsrc " : " ",
1820 (int)cp->c_fileid, (int)cp->c_blocks);
1821 }
1822
1823 //
1824 // release the name pointer in the descriptor so that
1825 // cat_delete() will use the file-id to do the deletion.
1826 // in the case of hard links this is imperative (in the
1827 // case of regular files the fileid and cnid are the
1828 // same so it doesn't matter).
1829 //
1830 cat_releasedesc(&cp->c_desc);
1831
1832 /*
1833 * The descriptor name may be zero,
1834 * in which case the fileid is used.
1835 */
1836 error = cat_delete(hfsmp, &cp->c_desc, &cp->c_attr);
1837
1838 if (error && truncated && (error != ENXIO)) {
1839 LFHFS_LOG(LEVEL_ERROR, "hfs_inactive: couldn't delete a truncated file!");
1840 }
1841
1842 /* Update HFS Private Data dir */
1843 if (error == 0) {
1844 hfsmp->hfs_private_attr[FILE_HARDLINKS].ca_entries--;
1845 if (vnode_isdir(vp)) {
1846 DEC_FOLDERCOUNT(hfsmp, hfsmp->hfs_private_attr[FILE_HARDLINKS]);
1847 }
1848 (void)cat_update(hfsmp, &hfsmp->hfs_private_desc[FILE_HARDLINKS],
1849 &hfsmp->hfs_private_attr[FILE_HARDLINKS], NULL, NULL);
1850 }
1851
1852 hfs_systemfile_unlock(hfsmp, lockflags);
1853
1854 if (error) {
1855 goto out;
1856 }
1857
1858 /* Already set C_NOEXISTS at the beginning of this block */
1859 cp->c_flag &= ~C_DELETED;
1860 cp->c_touch_chgtime = TRUE;
1861 cp->c_touch_modtime = TRUE;
1862
1863 if (error == 0)
1864 hfs_volupdate(hfsmp, (v_type == VDIR) ? VOL_RMDIR : VOL_RMFILE, 0);
1865 }
1866 } // if <open unlinked>
1867
1868 hfs_update(vp, reclaim ? HFS_UPDATE_FORCE : 0);
1869
1870 /*
1871 * Since we are about to finish what might be an inactive call, propagate
1872 * any remaining modified or touch bits from the cnode to the vnode. This
1873 * serves as a hint to vnode recycling that we shouldn't recycle this vnode
1874 * synchronously.
1875 *
1876 * For now, if the node *only* has a dirty atime, we don't mark
1877 * the vnode as dirty. VFS's asynchronous recycling can actually
1878 * lead to worse performance than having it synchronous. When VFS
1879 * is fixed to be more performant, we can be more honest about
1880 * marking vnodes as dirty when it's only the atime that's dirty.
1881 */
1882 #if LF_HFS_FULL_VNODE_SUPPORT
1883 //TBD - need to decide how we mark a file as dirty
1884 if (hfs_is_dirty(cp) == HFS_DIRTY || ISSET(cp->c_flag, C_DELETED)) {
1885 vnode_setdirty(vp);
1886 } else {
1887 vnode_cleardirty(vp);
1888 }
1889 #endif
1890
1891 out:
1892 if (cat_reserve)
1893 cat_postflight(hfsmp, &cookie);
1894
1895 if (started_tr) {
1896 hfs_end_transaction(hfsmp);
1897 started_tr = false;
1898 }
1899
1900 return error;
1901 }
1902
1903
1904 /*
1905 * Reclaim a cnode so that it can be used for other purposes.
1906 */
1907 int
1908 hfs_vnop_reclaim(struct vnode *vp)
1909 {
1910 struct cnode* cp = VTOC(vp);
1911 struct filefork *fp = NULL;
1912 struct filefork *altfp = NULL;
1913 struct hfsmount *hfsmp = VTOHFS(vp);
1914 int reclaim_cnode = 0;
1915 int err = 0;
1916
1917 /*
1918 * We don't take the truncate lock since by the time reclaim comes along,
1919 * all dirty pages have been synced and nobody should be competing
1920 * with us for this thread.
1921 */
1922 hfs_chash_mark_in_transit(hfsmp, cp);
1923
1924 hfs_lock(cp, HFS_EXCLUSIVE_LOCK, HFS_LOCK_DEFAULT);
1925 lf_hfs_generic_buf_cache_LockBufCache();
1926
1927 //In case we have other open lookups
1928 //We need to decrease the counter and exit
1929 if (cp->uOpenLookupRefCount > 1)
1930 {
1931 hfs_chash_lower_OpenLookupCounter(cp);
1932 hfs_chashwakeup(hfsmp, cp, H_ALLOC | H_TRANSIT);
1933 lf_hfs_generic_buf_cache_UnLockBufCache();
1934 hfs_unlock(cp);
1935 return err;
1936 }
1937
1938 if (cp->uOpenLookupRefCount == 0) assert(0);
1939
1940 hfs_chash_lower_OpenLookupCounter(cp);
1941 lf_hfs_generic_buf_cache_remove_vnode(vp);
1942
1943 lf_hfs_generic_buf_cache_UnLockBufCache();
1944
1945 /*
1946 * Sync to disk any remaining data in the cnode/vnode. This includes
1947 * a call to hfs_update if the cnode has outbound data.
1948 *
1949 * If C_NOEXISTS is set on the cnode, then there's nothing teardown needs to do
1950 * because the catalog entry for this cnode is already gone.
1951 */
1952 INVALIDATE_NODE(vp);
1953
1954 if (!ISSET(cp->c_flag, C_NOEXISTS)) {
1955 err = hfs_cnode_teardown(vp, 1);
1956 if (err)
1957 {
1958 return err;
1959 }
1960 }
1961
1962 if (vp->sFSParams.vnfs_cnp)
1963 {
1964 if (vp->sFSParams.vnfs_cnp->cn_nameptr)
1965 hfs_free(vp->sFSParams.vnfs_cnp->cn_nameptr);
1966 hfs_free(vp->sFSParams.vnfs_cnp);
1967 }
1968
1969
1970 /*
1971 * Find file fork for this vnode (if any)
1972 * Also check if another fork is active
1973 */
1974 if (cp->c_vp == vp) {
1975 fp = cp->c_datafork;
1976 altfp = cp->c_rsrcfork;
1977
1978 cp->c_datafork = NULL;
1979 cp->c_vp = NULL;
1980 } else if (cp->c_rsrc_vp == vp) {
1981 fp = cp->c_rsrcfork;
1982 altfp = cp->c_datafork;
1983
1984 cp->c_rsrcfork = NULL;
1985 cp->c_rsrc_vp = NULL;
1986 } else {
1987 LFHFS_LOG(LEVEL_ERROR, "hfs_vnop_reclaim: vp points to wrong cnode (vp=%p cp->c_vp=%p cp->c_rsrc_vp=%p)\n", vp, cp->c_vp, cp->c_rsrc_vp);
1988 hfs_assert(0);
1989 }
1990
1991 /*
1992 * On the last fork, remove the cnode from its hash chain.
1993 */
1994 if (altfp == NULL) {
1995 /* If we can't remove it then the cnode must persist! */
1996 if (hfs_chashremove(hfsmp, cp) == 0)
1997 reclaim_cnode = 1;
1998 /*
1999 * Remove any directory hints
2000 */
2001 if (vnode_isdir(vp)) {
2002 hfs_reldirhints(cp, 0);
2003 }
2004
2005 if(cp->c_flag & C_HARDLINK) {
2006 hfs_relorigins(cp);
2007 }
2008 }
2009 /* Release the file fork and related data */
2010 if (fp)
2011 {
2012 /* Dump cached symlink data */
2013 if (vnode_islnk(vp) && (fp->ff_symlinkptr != NULL)) {
2014 hfs_free(fp->ff_symlinkptr);
2015 }
2016 rl_remove_all(&fp->ff_invalidranges);
2017 hfs_free(fp);
2018 }
2019
2020 /*
2021 * If there was only one active fork then we can release the cnode.
2022 */
2023 if (reclaim_cnode) {
2024 hfs_unlock(cp);
2025 hfs_chashwakeup(hfsmp, cp, H_ALLOC);
2026 hfs_reclaim_cnode(cp);
2027 }
2028 else
2029 {
2030 /*
2031 * cnode in use. If it is a directory, it could have
2032 * no live forks. Just release the lock.
2033 */
2034 hfs_unlock(cp);
2035 }
2036
2037 hfs_free(vp);
2038 vp = NULL;
2039 return (0);
2040 }