Whamcloud - gitweb
undo-io: add new calls to and speed up the undo io manager
[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         errcode_t               retval;
35         struct ext2_inode       inode;
36         ext2_ino_t              scratch_ino;
37         blk64_t                 blk;
38         int                     fastlink, inlinelink;
39         unsigned int            target_len;
40         char                    *block_buf = 0;
41
42         EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
43
44         /* The Linux kernel doesn't allow for links longer than a block */
45         target_len = strlen(target);
46         if (target_len > fs->blocksize) {
47                 retval = EXT2_ET_INVALID_ARGUMENT;
48                 goto cleanup;
49         }
50
51         /*
52          * Allocate a data block for slow links
53          */
54         memset(&inode, 0, sizeof(struct ext2_inode));
55         fastlink = (target_len < sizeof(inode.i_block));
56         if (!fastlink) {
57                 retval = ext2fs_new_block2(fs, ext2fs_find_inode_goal(fs, ino,
58                                                                       &inode,
59                                                                       0),
60                                            NULL, &blk);
61                 if (retval)
62                         goto cleanup;
63                 retval = ext2fs_get_mem(fs->blocksize, &block_buf);
64                 if (retval)
65                         goto cleanup;
66         }
67
68         /*
69          * Allocate an inode, if necessary
70          */
71         if (!ino) {
72                 retval = ext2fs_new_inode(fs, parent, LINUX_S_IFLNK | 0755,
73                                           0, &ino);
74                 if (retval)
75                         goto cleanup;
76         }
77
78         /*
79          * Create the inode structure....
80          */
81         inode.i_mode = LINUX_S_IFLNK | 0777;
82         inode.i_uid = inode.i_gid = 0;
83         inode.i_links_count = 1;
84         ext2fs_inode_size_set(fs, &inode, target_len);
85         /* The time fields are set by ext2fs_write_new_inode() */
86
87         inlinelink = !fastlink &&
88                      (fs->super->s_feature_incompat &
89                                         EXT4_FEATURE_INCOMPAT_INLINE_DATA) &&
90                      (target_len < fs->blocksize);
91         if (fastlink) {
92                 /* Fast symlinks, target stored in inode */
93                 strcpy((char *)&inode.i_block, target);
94         } else if (inlinelink) {
95                 /* Try inserting an inline data symlink */
96                 inode.i_flags |= EXT4_INLINE_DATA_FL;
97                 retval = ext2fs_write_new_inode(fs, ino, &inode);
98                 if (retval)
99                         goto cleanup;
100                 retval = ext2fs_inline_data_set(fs, ino, &inode, target,
101                                                 target_len);
102                 if (retval) {
103                         inode.i_flags &= ~EXT4_INLINE_DATA_FL;
104                         inlinelink = 0;
105                         goto need_block;
106                 }
107                 retval = ext2fs_read_inode(fs, ino, &inode);
108                 if (retval)
109                         goto cleanup;
110         } else {
111 need_block:
112                 ext2fs_iblk_set(fs, &inode, 1);
113                 /* Slow symlinks, target stored in the first block */
114                 memset(block_buf, 0, fs->blocksize);
115                 strncpy(block_buf, target, fs->blocksize);
116                 if (fs->super->s_feature_incompat &
117                     EXT3_FEATURE_INCOMPAT_EXTENTS) {
118                         /*
119                          * The extent bmap is setup after the inode and block
120                          * have been written out below.
121                          */
122                         inode.i_flags |= EXT4_EXTENTS_FL;
123                 }
124         }
125
126         /*
127          * Write out the inode and inode data block.  The inode generation
128          * number is assigned by write_new_inode, which means that the
129          * operations using ino must come after it.
130          */
131         if (inlinelink)
132                 retval = ext2fs_write_inode(fs, ino, &inode);
133         else
134                 retval = ext2fs_write_new_inode(fs, ino, &inode);
135         if (retval)
136                 goto cleanup;
137
138         if (!fastlink && !inlinelink) {
139                 retval = ext2fs_bmap2(fs, ino, &inode, NULL, BMAP_SET, 0, NULL,
140                                       &blk);
141                 if (retval)
142                         goto cleanup;
143
144                 retval = io_channel_write_blk64(fs->io, blk, 1, block_buf);
145                 if (retval)
146                         goto cleanup;
147         }
148
149         /*
150          * Link the symlink into the filesystem hierarchy
151          */
152         if (name) {
153                 retval = ext2fs_lookup(fs, parent, name, strlen(name), 0,
154                                        &scratch_ino);
155                 if (!retval) {
156                         retval = EXT2_ET_FILE_EXISTS;
157                         goto cleanup;
158                 }
159                 if (retval != EXT2_ET_FILE_NOT_FOUND)
160                         goto cleanup;
161                 retval = ext2fs_link(fs, parent, name, ino, EXT2_FT_SYMLINK);
162                 if (retval)
163                         goto cleanup;
164         }
165
166         /*
167          * Update accounting....
168          */
169         if (!fastlink && !inlinelink)
170                 ext2fs_block_alloc_stats2(fs, blk, +1);
171         ext2fs_inode_alloc_stats2(fs, ino, +1, 0);
172
173 cleanup:
174         if (block_buf)
175                 ext2fs_free_mem(&block_buf);
176         return retval;
177 }