]>
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 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
27 #include <sys/systm.h>
28 #include <sys/kernel.h>
29 #include <sys/malloc.h>
30 #include <sys/mount.h>
31 #include <sys/namei.h>
33 #include <sys/vnode.h>
34 #include <vfs/vfs_support.h>
35 #include <libkern/libkern.h>
38 #include "hfs_catalog.h"
39 #include "hfs_format.h"
40 #include "hfs_endian.h"
44 * Create a new indirect link
46 * An indirect link is a reference to a data node. The only useable
47 * fields in the link are the link number, parentID, name and text
48 * encoding. All other catalog fields are ignored.
51 createindirectlink(struct hfsmount
*hfsmp
, u_int32_t linknum
,
52 u_int32_t linkparid
, char *linkName
, cnid_t
*linkcnid
)
54 struct FndrFileInfo
*fip
;
59 /* Setup the descriptor */
60 bzero(&desc
, sizeof(desc
));
61 desc
.cd_nameptr
= linkName
;
62 desc
.cd_namelen
= strlen(linkName
);
63 desc
.cd_parentcnid
= linkparid
;
65 /* Setup the default attributes */
66 bzero(&attr
, sizeof(attr
));
68 /* links are matched to data nodes by link ID and to volumes by create date */
69 attr
.ca_rdev
= linknum
; /* note: cat backend overloads ca_rdev to be the linknum when nlink = 0 */
70 attr
.ca_itime
= HFSTOVCB(hfsmp
)->vcbCrDate
;
71 attr
.ca_mode
= S_IFREG
;
73 fip
= (struct FndrFileInfo
*)&attr
.ca_finderinfo
;
74 fip
->fdType
= SWAP_BE32 (kHardLinkFileType
); /* 'hlnk' */
75 fip
->fdCreator
= SWAP_BE32 (kHFSPlusCreator
); /* 'hfs+' */
76 fip
->fdFlags
= SWAP_BE16 (kHasBeenInited
);
78 hfs_global_shared_lock_acquire(hfsmp
);
80 if (journal_start_transaction(hfsmp
->jnl
) != 0) {
81 hfs_global_shared_lock_release(hfsmp
);
86 /* Create the indirect link directly in the catalog */
87 result
= cat_create(hfsmp
, &desc
, &attr
, NULL
);
89 if (result
== 0 && linkcnid
!= NULL
)
90 *linkcnid
= attr
.ca_fileid
;
93 journal_end_transaction(hfsmp
->jnl
);
95 hfs_global_shared_lock_release(hfsmp
);
102 * 2 locks are needed (dvp and vp)
103 * also need catalog lock
105 * caller's responsibility:
106 * componentname cleanup
107 * unlocking dvp and vp
110 hfs_makelink(struct hfsmount
*hfsmp
, struct cnode
*cp
, struct cnode
*dcp
,
111 struct componentname
*cnp
)
113 struct proc
*p
= cnp
->cn_proc
;
114 u_int32_t indnodeno
= 0;
116 struct cat_desc to_desc
;
121 /* We don't allow link nodes in our Private Meta Data folder! */
122 if (dcp
->c_fileid
== hfsmp
->hfs_privdir_desc
.cd_cnid
)
125 if (hfs_freeblks(hfsmp
, 0) == 0)
128 /* Lock catalog b-tree */
129 retval
= hfs_metafilelocking(hfsmp
, kHFSCatalogFileID
, LK_EXCLUSIVE
, p
);
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 */
146 indnodeno
= ((random() & 0x3fffffff) + 100);
147 MAKE_INODE_NAME(inodename
, indnodeno
);
149 /* move source file to data node directory */
150 to_desc
.cd_nameptr
= inodename
;
151 to_desc
.cd_namelen
= strlen(inodename
);
153 retval
= cat_rename(hfsmp
, &cp
->c_desc
, &hfsmp
->hfs_privdir_desc
,
156 } while (retval
== EEXIST
);
160 /* Replace source file with link node */
161 retval
= createindirectlink(hfsmp
, indnodeno
, cp
->c_parentcnid
,
162 cp
->c_desc
.cd_nameptr
, &cp
->c_desc
.cd_cnid
);
164 /* put it source file back */
169 err
= cat_rename(hfsmp
, &to_desc
, &dcp
->c_desc
, &cp
->c_desc
, NULL
);
171 panic("hfs_makelink: error %d from cat_rename backout 1", err
);
174 (void) cat_rename(hfsmp
, &to_desc
, &dcp
->c_desc
, &cp
->c_desc
, NULL
);
178 cp
->c_rdev
= indnodeno
;
180 indnodeno
= cp
->c_rdev
;
184 * Create a catalog entry for the new link (parentID + name).
186 retval
= createindirectlink(hfsmp
, indnodeno
, dcp
->c_fileid
, cnp
->cn_nameptr
, NULL
);
187 if (retval
&& newlink
) {
188 /* Get rid of new link */
189 (void) cat_delete(hfsmp
, &cp
->c_desc
, &cp
->c_attr
);
191 /* Put the source file back */
196 err
= cat_rename(hfsmp
, &to_desc
, &dcp
->c_desc
, &cp
->c_desc
, NULL
);
198 panic("hfs_makelink: error %d from cat_rename backout 2", err
);
201 (void) cat_rename(hfsmp
, &to_desc
, &dcp
->c_desc
, &cp
->c_desc
, NULL
);
207 * Finally, if this is a new hardlink then:
208 * - update HFS Private Data dir
209 * - mark the cnode as a hard link
212 hfsmp
->hfs_privdir_attr
.ca_entries
++;
213 (void)cat_update(hfsmp
, &hfsmp
->hfs_privdir_desc
,
214 &hfsmp
->hfs_privdir_attr
, NULL
, NULL
);
215 hfs_volupdate(hfsmp
, VOL_MKFILE
, 0);
216 cp
->c_flag
|= (C_CHANGE
| C_HARDLINK
);
220 /* Unlock catalog b-tree */
221 (void) hfs_metafilelocking(hfsmp
, kHFSCatalogFileID
, LK_RELEASE
, p
);
233 IN WILLRELE struct vnode *vp;
234 IN struct vnode *targetPar_vp;
235 IN struct componentname *cnp;
240 struct vop_link_args
/* {
242 struct vnode *a_tdvp;
243 struct componentname *a_cnp;
246 struct hfsmount
*hfsmp
;
247 struct vnode
*vp
= ap
->a_vp
;
248 struct vnode
*tdvp
= ap
->a_tdvp
;
249 struct componentname
*cnp
= ap
->a_cnp
;
250 struct proc
*p
= cnp
->cn_proc
;
259 if ((cnp
->cn_flags
& HASBUF
) == 0)
260 panic("hfs_link: no name");
262 if (tdvp
->v_mount
!= vp
->v_mount
) {
263 VOP_ABORTOP(tdvp
, cnp
);
267 if (VTOVCB(tdvp
)->vcbSigWord
!= kHFSPlusSigWord
)
268 return err_link(ap
); /* hfs disks don't support hard links */
270 if (hfsmp
->hfs_private_metadata_dir
== 0)
271 return err_link(ap
); /* no private metadata dir, no links possible */
273 if (tdvp
!= vp
&& (error
= vn_lock(vp
, LK_EXCLUSIVE
, p
))) {
274 VOP_ABORTOP(tdvp
, cnp
);
280 if (cp
->c_nlink
>= HFS_LINK_MAX
) {
281 VOP_ABORTOP(tdvp
, cnp
);
285 if (cp
->c_flags
& (IMMUTABLE
| APPEND
)) {
286 VOP_ABORTOP(tdvp
, cnp
);
290 if (vp
->v_type
== VBLK
|| vp
->v_type
== VCHR
) {
291 VOP_ABORTOP(tdvp
, cnp
);
292 error
= EINVAL
; /* cannot link to a special file */
296 hfs_global_shared_lock_acquire(hfsmp
);
298 if (journal_start_transaction(hfsmp
->jnl
) != 0) {
299 hfs_global_shared_lock_release(hfsmp
);
300 VOP_ABORTOP(tdvp
, cnp
);
301 error
= EINVAL
; /* cannot link to a special file */
307 cp
->c_flag
|= C_CHANGE
;
310 error
= VOP_UPDATE(vp
, &tv
, &tv
, 1);
312 error
= hfs_makelink(hfsmp
, cp
, tdcp
, cnp
);
316 cp
->c_flag
|= C_CHANGE
;
318 /* Update the target directory and volume stats */
321 tdcp
->c_flag
|= C_CHANGE
| C_UPDATE
;
323 (void) VOP_UPDATE(tdvp
, &tv
, &tv
, 0);
325 hfs_volupdate(hfsmp
, VOL_MKFILE
,
326 (tdcp
->c_cnid
== kHFSRootFolderID
));
329 // XXXdbg - need to do this here as well because cp could have changed
330 error
= VOP_UPDATE(vp
, &tv
, &tv
, 1);
332 FREE_ZONE(cnp
->cn_pnbuf
, cnp
->cn_pnlen
, M_NAMEI
);
335 journal_end_transaction(hfsmp
->jnl
);
337 hfs_global_shared_lock_release(hfsmp
);
341 VOP_UNLOCK(vp
, 0, p
);