2 * symlink.c --- make a symlink in the filesystem, based on mkdir.c
4 * Copyright (c) 2012, Intel Corporation.
8 * This file may be redistributed under the terms of the GNU Library
9 * General Public License, version 2.
25 #include <sys/types.h>
31 errcode_t
ext2fs_symlink(ext2_filsys fs
, ext2_ino_t parent
, ext2_ino_t ino
,
32 const char *name
, const char *target
)
35 struct ext2_inode inode
;
36 ext2_ino_t scratch_ino
;
38 int fastlink
, inlinelink
;
39 unsigned int target_len
;
42 EXT2_CHECK_MAGIC(fs
, EXT2_ET_MAGIC_EXT2FS_FILSYS
);
44 /* The Linux kernel doesn't allow for links longer than a block */
45 target_len
= strnlen(target
, fs
->blocksize
+ 1);
46 if (target_len
> fs
->blocksize
) {
47 retval
= EXT2_ET_INVALID_ARGUMENT
;
52 * Allocate a data block for slow links
54 retval
= ext2fs_get_mem(fs
->blocksize
+1, &block_buf
);
57 memset(block_buf
, 0, fs
->blocksize
+1);
58 strncpy(block_buf
, target
, fs
->blocksize
);
60 memset(&inode
, 0, sizeof(struct ext2_inode
));
61 fastlink
= (target_len
< sizeof(inode
.i_block
));
63 retval
= ext2fs_new_block2(fs
, ext2fs_find_inode_goal(fs
, ino
,
72 * Allocate an inode, if necessary
75 retval
= ext2fs_new_inode(fs
, parent
, LINUX_S_IFLNK
| 0755,
82 * Create the inode structure....
84 inode
.i_mode
= LINUX_S_IFLNK
| 0777;
85 inode
.i_uid
= inode
.i_gid
= 0;
86 inode
.i_links_count
= 1;
87 ext2fs_inode_size_set(fs
, &inode
, target_len
);
88 /* The time fields are set by ext2fs_write_new_inode() */
90 inlinelink
= !fastlink
&&
91 (fs
->super
->s_feature_incompat
&
92 EXT4_FEATURE_INCOMPAT_INLINE_DATA
) &&
93 (target_len
< fs
->blocksize
);
95 /* Fast symlinks, target stored in inode */
96 strcpy((char *)&inode
.i_block
, target
);
97 } else if (inlinelink
) {
98 /* Try inserting an inline data symlink */
99 inode
.i_flags
|= EXT4_INLINE_DATA_FL
;
100 retval
= ext2fs_write_new_inode(fs
, ino
, &inode
);
103 retval
= ext2fs_inline_data_set(fs
, ino
, &inode
, block_buf
,
106 inode
.i_flags
&= ~EXT4_INLINE_DATA_FL
;
110 retval
= ext2fs_read_inode(fs
, ino
, &inode
);
115 /* Slow symlinks, target stored in the first block */
116 ext2fs_iblk_set(fs
, &inode
, 1);
117 if (fs
->super
->s_feature_incompat
&
118 EXT3_FEATURE_INCOMPAT_EXTENTS
) {
120 * The extent bmap is setup after the inode and block
121 * have been written out below.
123 inode
.i_flags
|= EXT4_EXTENTS_FL
;
128 * Write out the inode and inode data block. The inode generation
129 * number is assigned by write_new_inode, which means that the
130 * operations using ino must come after it.
133 retval
= ext2fs_write_inode(fs
, ino
, &inode
);
135 retval
= ext2fs_write_new_inode(fs
, ino
, &inode
);
139 if (!fastlink
&& !inlinelink
) {
140 retval
= ext2fs_bmap2(fs
, ino
, &inode
, NULL
, BMAP_SET
, 0, NULL
,
145 retval
= io_channel_write_blk64(fs
->io
, blk
, 1, block_buf
);
151 * Link the symlink into the filesystem hierarchy
154 retval
= ext2fs_lookup(fs
, parent
, name
, strlen(name
), 0,
157 retval
= EXT2_ET_FILE_EXISTS
;
160 if (retval
!= EXT2_ET_FILE_NOT_FOUND
)
162 retval
= ext2fs_link(fs
, parent
, name
, ino
, EXT2_FT_SYMLINK
);
168 * Update accounting....
170 if (!fastlink
&& !inlinelink
)
171 ext2fs_block_alloc_stats2(fs
, blk
, +1);
172 ext2fs_inode_alloc_stats2(fs
, ino
, +1, 0);
176 ext2fs_free_mem(&block_buf
);