]>
git.saurik.com Git - apple/xnu.git/blob - bsd/hfs/hfs_link.c
2 * Copyright (c) 1999-2003 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
;
116 cat_cookie_t cookie
= {0};
119 /* We don't allow link nodes in our Private Meta Data folder! */
120 if (dcp
->c_fileid
== hfsmp
->hfs_privdir_desc
.cd_cnid
)
123 if (hfs_freeblks(hfsmp
, 0) == 0)
126 /* Reserve some space in the Catalog file. */
127 if ((retval
= cat_preflight(hfsmp
, (2 * CAT_CREATE
)+ CAT_RENAME
, &cookie
, p
))) {
131 /* Lock catalog b-tree */
132 retval
= hfs_metafilelocking(hfsmp
, kHFSCatalogFileID
, LK_EXCLUSIVE
, p
);
138 * If this is a new hardlink then we need to create the data
139 * node (inode) and replace the original file with a link node.
141 if (cp
->c_nlink
== 2 && (cp
->c_flag
& C_HARDLINK
) == 0) {
143 bzero(&to_desc
, sizeof(to_desc
));
144 to_desc
.cd_parentcnid
= hfsmp
->hfs_privdir_desc
.cd_cnid
;
145 to_desc
.cd_cnid
= cp
->c_fileid
;
148 /* get a unique indirect node number */
149 indnodeno
= ((random() & 0x3fffffff) + 100);
150 MAKE_INODE_NAME(inodename
, indnodeno
);
152 /* move source file to data node directory */
153 to_desc
.cd_nameptr
= inodename
;
154 to_desc
.cd_namelen
= strlen(inodename
);
156 retval
= cat_rename(hfsmp
, &cp
->c_desc
, &hfsmp
->hfs_privdir_desc
,
159 } while (retval
== EEXIST
);
163 /* Replace source file with link node */
164 retval
= createindirectlink(hfsmp
, indnodeno
, cp
->c_parentcnid
,
165 cp
->c_desc
.cd_nameptr
, &cp
->c_desc
.cd_cnid
);
167 /* put it source file back */
172 err
= cat_rename(hfsmp
, &to_desc
, &dcp
->c_desc
, &cp
->c_desc
, NULL
);
174 panic("hfs_makelink: error %d from cat_rename backout 1", err
);
177 (void) cat_rename(hfsmp
, &to_desc
, &dcp
->c_desc
, &cp
->c_desc
, NULL
);
181 cp
->c_rdev
= indnodeno
;
183 indnodeno
= cp
->c_rdev
;
187 * Create a catalog entry for the new link (parentID + name).
189 retval
= createindirectlink(hfsmp
, indnodeno
, dcp
->c_fileid
, cnp
->cn_nameptr
, NULL
);
190 if (retval
&& newlink
) {
191 /* Get rid of new link */
192 (void) cat_delete(hfsmp
, &cp
->c_desc
, &cp
->c_attr
);
194 /* Put the source file back */
199 err
= cat_rename(hfsmp
, &to_desc
, &dcp
->c_desc
, &cp
->c_desc
, NULL
);
201 panic("hfs_makelink: error %d from cat_rename backout 2", err
);
204 (void) cat_rename(hfsmp
, &to_desc
, &dcp
->c_desc
, &cp
->c_desc
, NULL
);
210 * Finally, if this is a new hardlink then:
211 * - update HFS Private Data dir
212 * - mark the cnode as a hard link
215 hfsmp
->hfs_privdir_attr
.ca_entries
++;
216 (void)cat_update(hfsmp
, &hfsmp
->hfs_privdir_desc
,
217 &hfsmp
->hfs_privdir_attr
, NULL
, NULL
);
218 hfs_volupdate(hfsmp
, VOL_MKFILE
, 0);
219 cp
->c_flag
|= (C_CHANGE
| C_HARDLINK
);
223 /* Unlock catalog b-tree */
224 (void) hfs_metafilelocking(hfsmp
, kHFSCatalogFileID
, LK_RELEASE
, p
);
226 cat_postflight(hfsmp
, &cookie
, p
);
237 IN WILLRELE struct vnode *vp;
238 IN struct vnode *targetPar_vp;
239 IN struct componentname *cnp;
245 struct vop_link_args
/* {
247 struct vnode *a_tdvp;
248 struct componentname *a_cnp;
251 struct hfsmount
*hfsmp
;
252 struct vnode
*vp
= ap
->a_vp
;
253 struct vnode
*tdvp
= ap
->a_tdvp
;
254 struct componentname
*cnp
= ap
->a_cnp
;
255 struct proc
*p
= cnp
->cn_proc
;
264 if ((cnp
->cn_flags
& HASBUF
) == 0)
265 panic("hfs_link: no name");
267 if (tdvp
->v_mount
!= vp
->v_mount
) {
268 VOP_ABORTOP(tdvp
, cnp
);
272 if (VTOVCB(tdvp
)->vcbSigWord
!= kHFSPlusSigWord
)
273 return err_link(ap
); /* hfs disks don't support hard links */
275 if (hfsmp
->hfs_privdir_desc
.cd_cnid
== 0)
276 return err_link(ap
); /* no private metadata dir, no links possible */
278 if (tdvp
!= vp
&& (error
= vn_lock(vp
, LK_EXCLUSIVE
, p
))) {
279 VOP_ABORTOP(tdvp
, cnp
);
285 if (cp
->c_nlink
>= HFS_LINK_MAX
) {
286 VOP_ABORTOP(tdvp
, cnp
);
290 if (cp
->c_flags
& (IMMUTABLE
| APPEND
)) {
291 VOP_ABORTOP(tdvp
, cnp
);
295 if (vp
->v_type
== VBLK
|| vp
->v_type
== VCHR
) {
296 VOP_ABORTOP(tdvp
, cnp
);
297 error
= EINVAL
; /* cannot link to a special file */
301 hfs_global_shared_lock_acquire(hfsmp
);
303 if (journal_start_transaction(hfsmp
->jnl
) != 0) {
304 hfs_global_shared_lock_release(hfsmp
);
305 VOP_ABORTOP(tdvp
, cnp
);
306 error
= EINVAL
; /* cannot link to a special file */
312 cp
->c_flag
|= C_CHANGE
;
315 error
= VOP_UPDATE(vp
, &tv
, &tv
, 1);
317 error
= hfs_makelink(hfsmp
, cp
, tdcp
, cnp
);
321 cp
->c_flag
|= C_CHANGE
;
323 /* Update the target directory and volume stats */
326 tdcp
->c_flag
|= C_CHANGE
| C_UPDATE
;
328 (void) VOP_UPDATE(tdvp
, &tv
, &tv
, 0);
330 hfs_volupdate(hfsmp
, VOL_MKFILE
,
331 (tdcp
->c_cnid
== kHFSRootFolderID
));
334 // XXXdbg - need to do this here as well because cp could have changed
335 (void) VOP_UPDATE(vp
, &tv
, &tv
, 1);
339 journal_end_transaction(hfsmp
->jnl
);
341 hfs_global_shared_lock_release(hfsmp
);
343 /* free the pathname buffer */
345 char *tmp
= cnp
->cn_pnbuf
;
346 cnp
->cn_pnbuf
= NULL
;
347 cnp
->cn_flags
&= ~HASBUF
;
348 FREE_ZONE(tmp
, cnp
->cn_pnlen
, M_NAMEI
);
351 HFS_KNOTE(vp
, NOTE_LINK
);
352 HFS_KNOTE(tdvp
, NOTE_WRITE
);
356 VOP_UNLOCK(vp
, 0, p
);