• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * symlink.c --- make a symlink in the filesystem, based on mkdir.c
3  *
4  * Copyright (c) 2012, Intel Corporation.
5  * All Rights Reserved.
6  *
7  * %Begin-Header%
8  * This file may be redistributed under the terms of the GNU Library
9  * General Public License, version 2.
10  * %End-Header%
11  */
12 
13 #include "config.h"
14 #include <stdio.h>
15 #include <string.h>
16 #if HAVE_UNISTD_H
17 #include <unistd.h>
18 #endif
19 #include <fcntl.h>
20 #include <time.h>
21 #if HAVE_SYS_STAT_H
22 #include <sys/stat.h>
23 #endif
24 #if HAVE_SYS_TYPES_H
25 #include <sys/types.h>
26 #endif
27 
28 #include "ext2_fs.h"
29 #include "ext2fs.h"
30 
31 #ifndef HAVE_STRNLEN
32 /*
33  * Incredibly, libc5 doesn't appear to have strnlen.  So we have to
34  * provide our own.
35  */
my_strnlen(const char * s,int count)36 static int my_strnlen(const char * s, int count)
37 {
38 	const char *cp = s;
39 
40 	while (count-- && *cp)
41 		cp++;
42 	return cp - s;
43 }
44 #define strnlen(str, x) my_strnlen((str),(x))
45 #endif
46 
ext2fs_symlink(ext2_filsys fs,ext2_ino_t parent,ext2_ino_t ino,const char * name,const char * target)47 errcode_t ext2fs_symlink(ext2_filsys fs, ext2_ino_t parent, ext2_ino_t ino,
48 			 const char *name, const char *target)
49 {
50 	errcode_t		retval;
51 	struct ext2_inode	inode;
52 	ext2_ino_t		scratch_ino;
53 	blk64_t			blk;
54 	int			fastlink, inlinelink;
55 	unsigned int		target_len;
56 	char			*block_buf = 0;
57 
58 	EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
59 
60 	/*
61 	 * The Linux kernel doesn't allow for links longer than a block
62 	 * (counting the NUL terminator)
63 	 */
64 	target_len = strnlen(target, fs->blocksize + 1);
65 	if (target_len >= fs->blocksize) {
66 		retval = EXT2_ET_INVALID_ARGUMENT;
67 		goto cleanup;
68 	}
69 
70 	/*
71 	 * Allocate a data block for slow links
72 	 */
73 	retval = ext2fs_get_mem(fs->blocksize, &block_buf);
74 	if (retval)
75 		goto cleanup;
76 	memset(block_buf, 0, fs->blocksize);
77 	strncpy(block_buf, target, fs->blocksize);
78 
79 	memset(&inode, 0, sizeof(struct ext2_inode));
80 	fastlink = (target_len < sizeof(inode.i_block));
81 	if (!fastlink) {
82 		retval = ext2fs_new_block2(fs, ext2fs_find_inode_goal(fs, ino,
83 								      &inode,
84 								      0),
85 					   NULL, &blk);
86 		if (retval)
87 			goto cleanup;
88 	}
89 
90 	/*
91 	 * Allocate an inode, if necessary
92 	 */
93 	if (!ino) {
94 		retval = ext2fs_new_inode(fs, parent, LINUX_S_IFLNK | 0755,
95 					  0, &ino);
96 		if (retval)
97 			goto cleanup;
98 	}
99 
100 	/*
101 	 * Create the inode structure....
102 	 */
103 	inode.i_mode = LINUX_S_IFLNK | 0777;
104 	inode.i_uid = inode.i_gid = 0;
105 	inode.i_links_count = 1;
106 	ext2fs_inode_size_set(fs, &inode, target_len);
107 	/* The time fields are set by ext2fs_write_new_inode() */
108 
109 	inlinelink = !fastlink && ext2fs_has_feature_inline_data(fs->super);
110 	if (fastlink) {
111 		/* Fast symlinks, target stored in inode */
112 		strcpy((char *)&inode.i_block, target);
113 	} else if (inlinelink) {
114 		/* Try inserting an inline data symlink */
115 		inode.i_flags |= EXT4_INLINE_DATA_FL;
116 		retval = ext2fs_write_new_inode(fs, ino, &inode);
117 		if (retval)
118 			goto cleanup;
119 		retval = ext2fs_inline_data_set(fs, ino, &inode, block_buf,
120 						target_len);
121 		if (retval) {
122 			inode.i_flags &= ~EXT4_INLINE_DATA_FL;
123 			inlinelink = 0;
124 			goto need_block;
125 		}
126 		retval = ext2fs_read_inode(fs, ino, &inode);
127 		if (retval)
128 			goto cleanup;
129 	} else {
130 need_block:
131 		/* Slow symlinks, target stored in the first block */
132 		ext2fs_iblk_set(fs, &inode, 1);
133 		if (ext2fs_has_feature_extents(fs->super)) {
134 			/*
135 			 * The extent bmap is setup after the inode and block
136 			 * have been written out below.
137 			 */
138 			inode.i_flags |= EXT4_EXTENTS_FL;
139 		}
140 	}
141 
142 	/*
143 	 * Write out the inode and inode data block.  The inode generation
144 	 * number is assigned by write_new_inode, which means that the
145 	 * operations using ino must come after it.
146 	 */
147 	if (inlinelink)
148 		retval = ext2fs_write_inode(fs, ino, &inode);
149 	else
150 		retval = ext2fs_write_new_inode(fs, ino, &inode);
151 	if (retval)
152 		goto cleanup;
153 
154 	if (!fastlink && !inlinelink) {
155 		retval = ext2fs_bmap2(fs, ino, &inode, NULL, BMAP_SET, 0, NULL,
156 				      &blk);
157 		if (retval)
158 			goto cleanup;
159 
160 		retval = io_channel_write_blk64(fs->io, blk, 1, block_buf);
161 		if (retval)
162 			goto cleanup;
163 	}
164 
165 	/*
166 	 * Link the symlink into the filesystem hierarchy
167 	 */
168 	if (name) {
169 		retval = ext2fs_lookup(fs, parent, name, strlen(name), 0,
170 				       &scratch_ino);
171 		if (!retval) {
172 			retval = EXT2_ET_FILE_EXISTS;
173 			goto cleanup;
174 		}
175 		if (retval != EXT2_ET_FILE_NOT_FOUND)
176 			goto cleanup;
177 		retval = ext2fs_link(fs, parent, name, ino, EXT2_FT_SYMLINK);
178 		if (retval)
179 			goto cleanup;
180 	}
181 
182 	/*
183 	 * Update accounting....
184 	 */
185 	if (!fastlink && !inlinelink)
186 		ext2fs_block_alloc_stats2(fs, blk, +1);
187 	ext2fs_inode_alloc_stats2(fs, ino, +1, 0);
188 
189 cleanup:
190 	if (block_buf)
191 		ext2fs_free_mem(&block_buf);
192 	return retval;
193 }
194 
195 /*
196  * Test whether an inode is a fast symlink.
197  *
198  * A fast symlink has its symlink data stored in inode->i_block.
199  */
ext2fs_is_fast_symlink(struct ext2_inode * inode)200 int ext2fs_is_fast_symlink(struct ext2_inode *inode)
201 {
202 	return LINUX_S_ISLNK(inode->i_mode) && EXT2_I_SIZE(inode) &&
203 	       EXT2_I_SIZE(inode) < sizeof(inode->i_block);
204 }
205