]>
git.saurik.com Git - apple/xnu.git/blob - bsd/hfs/hfs_link.c
2 * Copyright (c) 1999-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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.
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
20 * @APPLE_LICENSE_HEADER_END@
24 #include <sys/systm.h>
25 #include <sys/kernel.h>
26 #include <sys/malloc.h>
27 #include <sys/mount.h>
29 #include <sys/vnode.h>
30 #include <vfs/vfs_support.h>
31 #include <libkern/libkern.h>
34 #include "hfs_catalog.h"
35 #include "hfs_format.h"
36 #include "hfs_endian.h"
39 static int cur_link_id
= 0;
43 * Create a new indirect link
45 * An indirect link is a reference to a data node. The only useable
46 * fields in the link are the link number, parentID, name and text
47 * encoding. All other catalog fields are ignored.
50 createindirectlink(struct hfsmount
*hfsmp
, u_int32_t linknum
,
51 u_int32_t linkparid
, char *linkName
, cnid_t
*linkcnid
)
53 struct FndrFileInfo
*fip
;
58 /* Setup the descriptor */
59 bzero(&desc
, sizeof(desc
));
60 desc
.cd_nameptr
= linkName
;
61 desc
.cd_namelen
= strlen(linkName
);
62 desc
.cd_parentcnid
= linkparid
;
64 /* Setup the default attributes */
65 bzero(&attr
, sizeof(attr
));
67 /* links are matched to data nodes by link ID and to volumes by create date */
68 attr
.ca_rdev
= linknum
; /* note: cat backend overloads ca_rdev to be the linknum when nlink = 0 */
69 attr
.ca_itime
= HFSTOVCB(hfsmp
)->vcbCrDate
;
70 attr
.ca_mode
= S_IFREG
;
72 fip
= (struct FndrFileInfo
*)&attr
.ca_finderinfo
;
73 fip
->fdType
= SWAP_BE32 (kHardLinkFileType
); /* 'hlnk' */
74 fip
->fdCreator
= SWAP_BE32 (kHFSPlusCreator
); /* 'hfs+' */
75 fip
->fdFlags
= SWAP_BE16 (kHasBeenInited
);
77 /* Create the indirect link directly in the catalog */
78 result
= cat_create(hfsmp
, &desc
, &attr
, NULL
);
80 if (result
== 0 && linkcnid
!= NULL
)
81 *linkcnid
= attr
.ca_fileid
;
88 * 2 locks are needed (dvp and vp)
89 * also need catalog lock
91 * caller's responsibility:
92 * componentname cleanup
93 * unlocking dvp and vp
96 hfs_makelink(struct hfsmount
*hfsmp
, struct cnode
*cp
, struct cnode
*dcp
,
97 struct componentname
*cnp
)
99 vfs_context_t ctx
= cnp
->cn_context
;
100 struct proc
*p
= vfs_context_proc(ctx
);
101 u_int32_t indnodeno
= 0;
103 struct cat_desc to_desc
;
110 if (cur_link_id
== 0) {
111 cur_link_id
= ((random() & 0x3fffffff) + 100);
112 // printf("hfs: initializing cur link id to: 0x%.8x\n", cur_link_id);
115 /* We don't allow link nodes in our Private Meta Data folder! */
116 if (dcp
->c_fileid
== hfsmp
->hfs_privdir_desc
.cd_cnid
)
119 if (hfs_freeblks(hfsmp
, 0) == 0)
122 bzero(&cookie
, sizeof(cat_cookie_t
));
123 /* Reserve some space in the Catalog file. */
124 if ((retval
= cat_preflight(hfsmp
, (2 * CAT_CREATE
)+ CAT_RENAME
, &cookie
, p
))) {
128 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_EXCLUSIVE_LOCK
);
130 // save off a copy of the current cnid so we can put
131 // it back if we get errors down below
132 orig_cnid
= cp
->c_desc
.cd_cnid
;
135 * If this is a new hardlink then we need to create the data
136 * node (inode) and replace the original file with a link node.
138 if (cp
->c_nlink
== 2 && (cp
->c_flag
& C_HARDLINK
) == 0) {
140 bzero(&to_desc
, sizeof(to_desc
));
141 to_desc
.cd_parentcnid
= hfsmp
->hfs_privdir_desc
.cd_cnid
;
142 to_desc
.cd_cnid
= cp
->c_fileid
;
145 /* get a unique indirect node number */
147 indnodeno
= cp
->c_fileid
;
149 indnodeno
= cur_link_id
++;
152 MAKE_INODE_NAME(inodename
, indnodeno
);
154 /* move source file to data node directory */
155 to_desc
.cd_nameptr
= inodename
;
156 to_desc
.cd_namelen
= strlen(inodename
);
158 retval
= cat_rename(hfsmp
, &cp
->c_desc
, &hfsmp
->hfs_privdir_desc
,
161 if (retval
!= 0 && retval
!= EEXIST
) {
162 printf("hfs_makelink: cat_rename to %s failed (%d). fileid %d\n",
163 inodename
, retval
, cp
->c_fileid
);
166 } while (retval
== EEXIST
);
170 /* Replace source file with link node */
171 retval
= createindirectlink(hfsmp
, indnodeno
, cp
->c_parentcnid
,
172 cp
->c_desc
.cd_nameptr
, &cp
->c_desc
.cd_cnid
);
174 /* put it source file back */
177 // Put this back to what it was before.
178 cp
->c_desc
.cd_cnid
= orig_cnid
;
180 err
= cat_rename(hfsmp
, &to_desc
, &dcp
->c_desc
, &cp
->c_desc
, NULL
);
182 panic("hfs_makelink: error %d from cat_rename backout 1", err
);
185 cp
->c_rdev
= indnodeno
;
187 indnodeno
= cp
->c_rdev
;
191 * Create a catalog entry for the new link (parentID + name).
193 retval
= createindirectlink(hfsmp
, indnodeno
, dcp
->c_fileid
, cnp
->cn_nameptr
, NULL
);
194 if (retval
&& newlink
) {
197 /* Get rid of new link */
198 (void) cat_delete(hfsmp
, &cp
->c_desc
, &cp
->c_attr
);
200 // Put this back to what it was before.
201 cp
->c_desc
.cd_cnid
= orig_cnid
;
203 /* Put the source file back */
204 err
= cat_rename(hfsmp
, &to_desc
, &dcp
->c_desc
, &cp
->c_desc
, NULL
);
206 panic("hfs_makelink: error %d from cat_rename backout 2", err
);
212 * Finally, if this is a new hardlink then:
213 * - update HFS Private Data dir
214 * - mark the cnode as a hard link
220 panic("hfs_makelink: retval %d but newlink = 1!\n", retval
);
223 hfsmp
->hfs_privdir_attr
.ca_entries
++;
224 retval
= cat_update(hfsmp
, &hfsmp
->hfs_privdir_desc
,
225 &hfsmp
->hfs_privdir_attr
, NULL
, NULL
);
227 panic("hfs_makelink: cat_update of privdir failed! (%d)\n",
230 hfs_volupdate(hfsmp
, VOL_MKFILE
, 0);
231 cp
->c_flag
|= C_HARDLINK
;
232 if ((vp
= cp
->c_vp
) != NULLVP
) {
233 if (vnode_get(vp
) == 0) {
234 vnode_set_hard_link(vp
);
238 if ((vp
= cp
->c_rsrc_vp
) != NULLVP
) {
239 if (vnode_get(vp
) == 0) {
240 vnode_set_hard_link(vp
);
244 cp
->c_touch_chgtime
= TRUE
;
245 cp
->c_flag
|= C_FORCEUPDATE
;
247 dcp
->c_flag
|= C_FORCEUPDATE
;
250 hfs_systemfile_unlock(hfsmp
, lockflags
);
252 cat_postflight(hfsmp
, &cookie
, p
);
263 IN WILLRELE struct vnode *vp;
264 IN struct vnode *targetPar_vp;
265 IN struct componentname *cnp;
266 IN vfs_context_t context;
271 hfs_vnop_link(struct vnop_link_args
*ap
)
273 struct hfsmount
*hfsmp
;
274 struct vnode
*vp
= ap
->a_vp
;
275 struct vnode
*tdvp
= ap
->a_tdvp
;
276 struct componentname
*cnp
= ap
->a_cnp
;
280 int error
, ret
, lockflags
;
281 struct cat_desc cndesc
;
283 if (VTOVCB(tdvp
)->vcbSigWord
!= kHFSPlusSigWord
) {
284 return err_link(ap
); /* hfs disks don't support hard links */
286 if (VTOHFS(vp
)->hfs_privdir_desc
.cd_cnid
== 0) {
287 return err_link(ap
); /* no private metadata dir, no links possible */
289 if (vnode_mount(tdvp
) != vnode_mount(vp
)) {
292 if ((error
= hfs_lockpair(VTOC(tdvp
), VTOC(vp
), HFS_EXCLUSIVE_LOCK
))) {
299 if (cp
->c_nlink
>= HFS_LINK_MAX
) {
303 if (cp
->c_flags
& (IMMUTABLE
| APPEND
)) {
307 if (cp
->c_flag
& (C_NOEXISTS
| C_DELETED
)) {
312 v_type
= vnode_vtype(vp
);
313 if (v_type
== VBLK
|| v_type
== VCHR
) {
314 error
= EINVAL
; /* cannot link to a special file */
318 if (hfs_start_transaction(hfsmp
) != 0) {
319 error
= EINVAL
; /* cannot link to a special file */
324 cp
->c_touch_chgtime
= TRUE
;
326 error
= hfs_makelink(hfsmp
, cp
, tdcp
, cnp
);
329 hfs_volupdate(hfsmp
, VOL_UPDATE
, 0);
331 /* Invalidate negative cache entries in the destination directory */
332 if (hfsmp
->hfs_flags
& HFS_CASE_SENSITIVE
)
333 cache_purge_negatives(tdvp
);
335 /* Update the target directory and volume stats */
338 tdcp
->c_touch_chgtime
= TRUE
;
339 tdcp
->c_touch_modtime
= TRUE
;
340 tdcp
->c_flag
|= C_FORCEUPDATE
;
342 error
= hfs_update(tdvp
, 0);
344 panic("hfs_vnop_link: error updating tdvp 0x%x\n", tdvp
);
347 hfs_volupdate(hfsmp
, VOL_MKFILE
,
348 (tdcp
->c_cnid
== kHFSRootFolderID
));
351 cp
->c_flag
|= C_FORCEUPDATE
; // otherwise hfs_update() might skip the update
353 if ((ret
= hfs_update(vp
, TRUE
)) != 0) {
354 panic("hfs_vnop_link: error %d updating vp @ 0x%x\n", ret
, vp
);
357 hfs_end_transaction(hfsmp
);
359 HFS_KNOTE(vp
, NOTE_LINK
);
360 HFS_KNOTE(tdvp
, NOTE_WRITE
);
362 hfs_unlockpair(tdcp
, cp
);