]>
git.saurik.com Git - apple/xnu.git/blob - bsd/hfs/hfs_link.c
2 * Copyright (c) 1999-2002 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>
28 #include <sys/namei.h>
30 #include <sys/vnode.h>
31 #include <vfs/vfs_support.h>
32 #include <libkern/libkern.h>
35 #include "hfs_catalog.h"
36 #include "hfs_format.h"
37 #include "hfs_endian.h"
41 * Create a new indirect link
43 * An indirect link is a reference to a data node. The only useable
44 * fields in the link are the link number, parentID, name and text
45 * encoding. All other catalog fields are ignored.
48 createindirectlink(struct hfsmount
*hfsmp
, u_int32_t linknum
,
49 u_int32_t linkparid
, char *linkName
, cnid_t
*linkcnid
)
51 struct FndrFileInfo
*fip
;
56 /* Setup the descriptor */
57 bzero(&desc
, sizeof(desc
));
58 desc
.cd_nameptr
= linkName
;
59 desc
.cd_namelen
= strlen(linkName
);
60 desc
.cd_parentcnid
= linkparid
;
62 /* Setup the default attributes */
63 bzero(&attr
, sizeof(attr
));
65 /* links are matched to data nodes by link ID and to volumes by create date */
66 attr
.ca_rdev
= linknum
; /* note: cat backend overloads ca_rdev to be the linknum when nlink = 0 */
67 attr
.ca_itime
= HFSTOVCB(hfsmp
)->vcbCrDate
;
68 attr
.ca_mode
= S_IFREG
;
70 fip
= (struct FndrFileInfo
*)&attr
.ca_finderinfo
;
71 fip
->fdType
= SWAP_BE32 (kHardLinkFileType
); /* 'hlnk' */
72 fip
->fdCreator
= SWAP_BE32 (kHFSPlusCreator
); /* 'hfs+' */
73 fip
->fdFlags
= SWAP_BE16 (kHasBeenInited
);
75 hfs_global_shared_lock_acquire(hfsmp
);
77 if (journal_start_transaction(hfsmp
->jnl
) != 0) {
78 hfs_global_shared_lock_release(hfsmp
);
83 /* Create the indirect link directly in the catalog */
84 result
= cat_create(hfsmp
, &desc
, &attr
, NULL
);
86 if (result
== 0 && linkcnid
!= NULL
)
87 *linkcnid
= attr
.ca_fileid
;
90 journal_end_transaction(hfsmp
->jnl
);
92 hfs_global_shared_lock_release(hfsmp
);
99 * 2 locks are needed (dvp and vp)
100 * also need catalog lock
102 * caller's responsibility:
103 * componentname cleanup
104 * unlocking dvp and vp
107 hfs_makelink(struct hfsmount
*hfsmp
, struct cnode
*cp
, struct cnode
*dcp
,
108 struct componentname
*cnp
)
110 struct proc
*p
= cnp
->cn_proc
;
111 u_int32_t indnodeno
= 0;
113 struct cat_desc to_desc
;
118 /* We don't allow link nodes in our Private Meta Data folder! */
119 if (dcp
->c_fileid
== hfsmp
->hfs_privdir_desc
.cd_cnid
)
122 if (hfs_freeblks(hfsmp
, 0) == 0)
125 /* Lock catalog b-tree */
126 retval
= hfs_metafilelocking(hfsmp
, kHFSCatalogFileID
, LK_EXCLUSIVE
, p
);
132 * If this is a new hardlink then we need to create the data
133 * node (inode) and replace the original file with a link node.
135 if (cp
->c_nlink
== 2 && (cp
->c_flag
& C_HARDLINK
) == 0) {
137 bzero(&to_desc
, sizeof(to_desc
));
138 to_desc
.cd_parentcnid
= hfsmp
->hfs_privdir_desc
.cd_cnid
;
139 to_desc
.cd_cnid
= cp
->c_fileid
;
142 /* get a unique indirect node number */
143 indnodeno
= ((random() & 0x3fffffff) + 100);
144 MAKE_INODE_NAME(inodename
, indnodeno
);
146 /* move source file to data node directory */
147 to_desc
.cd_nameptr
= inodename
;
148 to_desc
.cd_namelen
= strlen(inodename
);
150 retval
= cat_rename(hfsmp
, &cp
->c_desc
, &hfsmp
->hfs_privdir_desc
,
153 } while (retval
== EEXIST
);
157 /* Replace source file with link node */
158 retval
= createindirectlink(hfsmp
, indnodeno
, cp
->c_parentcnid
,
159 cp
->c_desc
.cd_nameptr
, &cp
->c_desc
.cd_cnid
);
161 /* put it source file back */
166 err
= cat_rename(hfsmp
, &to_desc
, &dcp
->c_desc
, &cp
->c_desc
, NULL
);
168 panic("hfs_makelink: error %d from cat_rename backout 1", err
);
171 (void) cat_rename(hfsmp
, &to_desc
, &dcp
->c_desc
, &cp
->c_desc
, NULL
);
175 cp
->c_rdev
= indnodeno
;
177 indnodeno
= cp
->c_rdev
;
181 * Create a catalog entry for the new link (parentID + name).
183 retval
= createindirectlink(hfsmp
, indnodeno
, dcp
->c_fileid
, cnp
->cn_nameptr
, NULL
);
184 if (retval
&& newlink
) {
185 /* Get rid of new link */
186 (void) cat_delete(hfsmp
, &cp
->c_desc
, &cp
->c_attr
);
188 /* Put the source file back */
193 err
= cat_rename(hfsmp
, &to_desc
, &dcp
->c_desc
, &cp
->c_desc
, NULL
);
195 panic("hfs_makelink: error %d from cat_rename backout 2", err
);
198 (void) cat_rename(hfsmp
, &to_desc
, &dcp
->c_desc
, &cp
->c_desc
, NULL
);
204 * Finally, if this is a new hardlink then:
205 * - update HFS Private Data dir
206 * - mark the cnode as a hard link
209 hfsmp
->hfs_privdir_attr
.ca_entries
++;
210 (void)cat_update(hfsmp
, &hfsmp
->hfs_privdir_desc
,
211 &hfsmp
->hfs_privdir_attr
, NULL
, NULL
);
212 hfs_volupdate(hfsmp
, VOL_MKFILE
, 0);
213 cp
->c_flag
|= (C_CHANGE
| C_HARDLINK
);
217 /* Unlock catalog b-tree */
218 (void) hfs_metafilelocking(hfsmp
, kHFSCatalogFileID
, LK_RELEASE
, p
);
230 IN WILLRELE struct vnode *vp;
231 IN struct vnode *targetPar_vp;
232 IN struct componentname *cnp;
237 struct vop_link_args
/* {
239 struct vnode *a_tdvp;
240 struct componentname *a_cnp;
243 struct hfsmount
*hfsmp
;
244 struct vnode
*vp
= ap
->a_vp
;
245 struct vnode
*tdvp
= ap
->a_tdvp
;
246 struct componentname
*cnp
= ap
->a_cnp
;
247 struct proc
*p
= cnp
->cn_proc
;
256 if ((cnp
->cn_flags
& HASBUF
) == 0)
257 panic("hfs_link: no name");
259 if (tdvp
->v_mount
!= vp
->v_mount
) {
260 VOP_ABORTOP(tdvp
, cnp
);
264 if (VTOVCB(tdvp
)->vcbSigWord
!= kHFSPlusSigWord
)
265 return err_link(ap
); /* hfs disks don't support hard links */
267 if (hfsmp
->hfs_private_metadata_dir
== 0)
268 return err_link(ap
); /* no private metadata dir, no links possible */
270 if (tdvp
!= vp
&& (error
= vn_lock(vp
, LK_EXCLUSIVE
, p
))) {
271 VOP_ABORTOP(tdvp
, cnp
);
277 if (cp
->c_nlink
>= HFS_LINK_MAX
) {
278 VOP_ABORTOP(tdvp
, cnp
);
282 if (cp
->c_flags
& (IMMUTABLE
| APPEND
)) {
283 VOP_ABORTOP(tdvp
, cnp
);
287 if (vp
->v_type
== VBLK
|| vp
->v_type
== VCHR
) {
288 VOP_ABORTOP(tdvp
, cnp
);
289 error
= EINVAL
; /* cannot link to a special file */
293 hfs_global_shared_lock_acquire(hfsmp
);
295 if (journal_start_transaction(hfsmp
->jnl
) != 0) {
296 hfs_global_shared_lock_release(hfsmp
);
297 VOP_ABORTOP(tdvp
, cnp
);
298 error
= EINVAL
; /* cannot link to a special file */
304 cp
->c_flag
|= C_CHANGE
;
307 error
= VOP_UPDATE(vp
, &tv
, &tv
, 1);
309 error
= hfs_makelink(hfsmp
, cp
, tdcp
, cnp
);
313 cp
->c_flag
|= C_CHANGE
;
315 /* Update the target directory and volume stats */
318 tdcp
->c_flag
|= C_CHANGE
| C_UPDATE
;
320 (void) VOP_UPDATE(tdvp
, &tv
, &tv
, 0);
322 hfs_volupdate(hfsmp
, VOL_MKFILE
,
323 (tdcp
->c_cnid
== kHFSRootFolderID
));
326 // XXXdbg - need to do this here as well because cp could have changed
327 error
= VOP_UPDATE(vp
, &tv
, &tv
, 1);
329 FREE_ZONE(cnp
->cn_pnbuf
, cnp
->cn_pnlen
, M_NAMEI
);
332 journal_end_transaction(hfsmp
->jnl
);
334 hfs_global_shared_lock_release(hfsmp
);
338 VOP_UNLOCK(vp
, 0, p
);