Whamcloud - gitweb
libext2fs: make symlinks safe for 64bit blocks and extents
[tools/e2fsprogs.git] / lib / ext2fs / symlink.c
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 errcode_t ext2fs_symlink(ext2_filsys fs, ext2_ino_t parent, ext2_ino_t ino,
32                          const char *name, char *target)
33 {
34         ext2_extent_handle_t    handle;
35         errcode_t               retval;
36         struct ext2_inode       inode;
37         ext2_ino_t              scratch_ino;
38         blk64_t                 blk;
39         int                     fastlink;
40         unsigned int            target_len;
41         char                    *block_buf = 0;
42
43         EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
44
45         /* The Linux kernel doesn't allow for links longer than a block */
46         target_len = strlen(target);
47         if (target_len > fs->blocksize) {
48                 retval = EXT2_ET_INVALID_ARGUMENT;
49                 goto cleanup;
50         }
51
52         /*
53          * Allocate a data block for slow links
54          */
55         fastlink = (target_len < sizeof(inode.i_block));
56         if (!fastlink) {
57                 retval = ext2fs_new_block2(fs, 0, 0, &blk);
58                 if (retval)
59                         goto cleanup;
60                 retval = ext2fs_get_mem(fs->blocksize, &block_buf);
61                 if (retval)
62                         goto cleanup;
63         }
64
65         /*
66          * Allocate an inode, if necessary
67          */
68         if (!ino) {
69                 retval = ext2fs_new_inode(fs, parent, LINUX_S_IFLNK | 0755,
70                                           0, &ino);
71                 if (retval)
72                         goto cleanup;
73         }
74
75         /*
76          * Create the inode structure....
77          */
78         memset(&inode, 0, sizeof(struct ext2_inode));
79         inode.i_mode = LINUX_S_IFLNK | 0777;
80         inode.i_uid = inode.i_gid = 0;
81         ext2fs_iblk_set(fs, &inode, fastlink ? 0 : 1);
82         inode.i_links_count = 1;
83         inode.i_size = target_len;
84         /* The time fields are set by ext2fs_write_new_inode() */
85
86         if (fastlink) {
87                 /* Fast symlinks, target stored in inode */
88                 strcpy((char *)&inode.i_block, target);
89         } else {
90                 /* Slow symlinks, target stored in the first block */
91                 memset(block_buf, 0, fs->blocksize);
92                 strcpy(block_buf, target);
93                 if (fs->super->s_feature_incompat &
94                     EXT3_FEATURE_INCOMPAT_EXTENTS) {
95                         /*
96                          * The extent bmap is setup after the inode and block
97                          * have been written out below.
98                          */
99                         inode.i_flags |= EXT4_EXTENTS_FL;
100                 }
101         }
102
103         /*
104          * Write out the inode and inode data block.  The inode generation
105          * number is assigned by write_new_inode, which means that the
106          * operations using ino must come after it.
107          */
108         retval = ext2fs_write_new_inode(fs, ino, &inode);
109         if (retval)
110                 goto cleanup;
111
112         if (!fastlink) {
113                 retval = ext2fs_bmap2(fs, ino, &inode, NULL, BMAP_SET, 0, NULL,
114                                       &blk);
115                 if (retval)
116                         goto cleanup;
117
118                 retval = io_channel_write_blk64(fs->io, blk, 1, block_buf);
119                 if (retval)
120                         goto cleanup;
121         }
122
123         /*
124          * Link the symlink into the filesystem hierarchy
125          */
126         if (name) {
127                 retval = ext2fs_lookup(fs, parent, name, strlen(name), 0,
128                                        &scratch_ino);
129                 if (!retval) {
130                         retval = EXT2_ET_FILE_EXISTS;
131                         goto cleanup;
132                 }
133                 if (retval != EXT2_ET_FILE_NOT_FOUND)
134                         goto cleanup;
135                 retval = ext2fs_link(fs, parent, name, ino, EXT2_FT_SYMLINK);
136                 if (retval)
137                         goto cleanup;
138         }
139
140         /*
141          * Update accounting....
142          */
143         if (!fastlink)
144                 ext2fs_block_alloc_stats2(fs, blk, +1);
145         ext2fs_inode_alloc_stats2(fs, ino, +1, 0);
146
147 cleanup:
148         if (block_buf)
149                 ext2fs_free_mem(&block_buf);
150         return retval;
151 }