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>
33 * Incredibly, libc5 doesn't appear to have strnlen. So we have to
36 static int my_strnlen(const char * s, int count)
40 while (count-- && *cp)
44 #define strnlen(str, x) my_strnlen((str),(x))
47 errcode_t ext2fs_symlink(ext2_filsys fs, ext2_ino_t parent, ext2_ino_t ino,
48 const char *name, const char *target)
51 struct ext2_inode inode;
52 ext2_ino_t scratch_ino;
54 int fastlink, inlinelink;
55 unsigned int target_len;
57 int drop_refcount = 0;
59 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
62 * The Linux kernel doesn't allow for links longer than a block
63 * (counting the NUL terminator)
65 target_len = strnlen(target, fs->blocksize + 1);
66 if (target_len >= fs->blocksize) {
67 retval = EXT2_ET_INVALID_ARGUMENT;
72 * Allocate a data block for slow links
74 retval = ext2fs_get_mem(fs->blocksize, &block_buf);
77 memset(block_buf, 0, fs->blocksize);
78 strncpy(block_buf, target, fs->blocksize);
80 memset(&inode, 0, sizeof(struct ext2_inode));
81 fastlink = (target_len < sizeof(inode.i_block));
83 retval = ext2fs_new_block2(fs, ext2fs_find_inode_goal(fs, ino,
92 * Allocate an inode, if necessary
95 retval = ext2fs_new_inode(fs, parent, LINUX_S_IFLNK | 0755,
102 * Create the inode structure....
104 inode.i_mode = LINUX_S_IFLNK | 0777;
105 inode.i_uid = inode.i_gid = 0;
106 inode.i_links_count = 1;
107 ext2fs_inode_size_set(fs, &inode, target_len);
108 /* The time fields are set by ext2fs_write_new_inode() */
110 inlinelink = !fastlink && ext2fs_has_feature_inline_data(fs->super);
112 /* Fast symlinks, target stored in inode */
113 strcpy((char *)&inode.i_block, target);
114 } else if (inlinelink) {
115 /* Try inserting an inline data symlink */
116 inode.i_flags |= EXT4_INLINE_DATA_FL;
117 retval = ext2fs_write_new_inode(fs, ino, &inode);
120 retval = ext2fs_inline_data_set(fs, ino, &inode, block_buf,
123 inode.i_flags &= ~EXT4_INLINE_DATA_FL;
127 retval = ext2fs_read_inode(fs, ino, &inode);
132 /* Slow symlinks, target stored in the first block */
133 ext2fs_iblk_set(fs, &inode, 1);
134 if (ext2fs_has_feature_extents(fs->super)) {
136 * The extent bmap is setup after the inode and block
137 * have been written out below.
139 inode.i_flags |= EXT4_EXTENTS_FL;
144 * Write out the inode and inode data block. The inode generation
145 * number is assigned by write_new_inode, which means that the
146 * operations using ino must come after it.
149 retval = ext2fs_write_inode(fs, ino, &inode);
151 retval = ext2fs_write_new_inode(fs, ino, &inode);
155 if (!fastlink && !inlinelink) {
156 retval = ext2fs_bmap2(fs, ino, &inode, NULL, BMAP_SET, 0, NULL,
161 retval = io_channel_write_blk64(fs->io, blk, 1, block_buf);
167 * Update accounting....
169 if (!fastlink && !inlinelink)
170 ext2fs_block_alloc_stats2(fs, blk, +1);
171 ext2fs_inode_alloc_stats2(fs, ino, +1, 0);
175 * Link the symlink into the filesystem hierarchy
178 retval = ext2fs_lookup(fs, parent, name, strlen(name), 0,
181 retval = EXT2_ET_FILE_EXISTS;
184 if (retval != EXT2_ET_FILE_NOT_FOUND)
186 retval = ext2fs_link(fs, parent, name, ino, EXT2_FT_SYMLINK);
194 ext2fs_free_mem(&block_buf);
196 if (!fastlink && !inlinelink)
197 ext2fs_block_alloc_stats2(fs, blk, -1);
198 ext2fs_inode_alloc_stats2(fs, ino, -1, 0);
204 * Test whether an inode is a fast symlink.
206 * A fast symlink has its symlink data stored in inode->i_block.
208 int ext2fs_is_fast_symlink(struct ext2_inode *inode)
210 return LINUX_S_ISLNK(inode->i_mode) && EXT2_I_SIZE(inode) &&
211 EXT2_I_SIZE(inode) < sizeof(inode->i_block);