Whamcloud - gitweb
ext2fs: update allocation info earlier in ext2fs_mkdir() and ext2fs_symlink()
[tools/e2fsprogs.git] / lib / ext2fs / symlink.c
index b2ef66c..a66fb7e 100644 (file)
 #include "ext2_fs.h"
 #include "ext2fs.h"
 
+#ifndef HAVE_STRNLEN
+/*
+ * Incredibly, libc5 doesn't appear to have strnlen.  So we have to
+ * provide our own.
+ */
+static int my_strnlen(const char * s, int count)
+{
+       const char *cp = s;
+
+       while (count-- && *cp)
+               cp++;
+       return cp - s;
+}
+#define strnlen(str, x) my_strnlen((str),(x))
+#endif
+
 errcode_t ext2fs_symlink(ext2_filsys fs, ext2_ino_t parent, ext2_ino_t ino,
-                        const char *name, char *target)
+                        const char *name, const char *target)
 {
        errcode_t               retval;
        struct ext2_inode       inode;
        ext2_ino_t              scratch_ino;
        blk64_t                 blk;
-       int                     fastlink;
+       int                     fastlink, inlinelink;
        unsigned int            target_len;
        char                    *block_buf = 0;
+       int                     drop_refcount = 0;
 
        EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
 
-       /* The Linux kernel doesn't allow for links longer than a block */
-       target_len = strlen(target);
-       if (target_len > fs->blocksize) {
+       /*
+        * The Linux kernel doesn't allow for links longer than a block
+        * (counting the NUL terminator)
+        */
+       target_len = strnlen(target, fs->blocksize + 1);
+       if (target_len >= fs->blocksize) {
                retval = EXT2_ET_INVALID_ARGUMENT;
                goto cleanup;
        }
@@ -51,12 +71,19 @@ errcode_t ext2fs_symlink(ext2_filsys fs, ext2_ino_t parent, ext2_ino_t ino,
        /*
         * Allocate a data block for slow links
         */
+       retval = ext2fs_get_mem(fs->blocksize, &block_buf);
+       if (retval)
+               goto cleanup;
+       memset(block_buf, 0, fs->blocksize);
+       strncpy(block_buf, target, fs->blocksize);
+
+       memset(&inode, 0, sizeof(struct ext2_inode));
        fastlink = (target_len < sizeof(inode.i_block));
        if (!fastlink) {
-               retval = ext2fs_new_block2(fs, 0, 0, &blk);
-               if (retval)
-                       goto cleanup;
-               retval = ext2fs_get_mem(fs->blocksize, &block_buf);
+               retval = ext2fs_new_block2(fs, ext2fs_find_inode_goal(fs, ino,
+                                                                     &inode,
+                                                                     0),
+                                          NULL, &blk);
                if (retval)
                        goto cleanup;
        }
@@ -74,23 +101,37 @@ errcode_t ext2fs_symlink(ext2_filsys fs, ext2_ino_t parent, ext2_ino_t ino,
        /*
         * Create the inode structure....
         */
-       memset(&inode, 0, sizeof(struct ext2_inode));
        inode.i_mode = LINUX_S_IFLNK | 0777;
        inode.i_uid = inode.i_gid = 0;
-       ext2fs_iblk_set(fs, &inode, fastlink ? 0 : 1);
        inode.i_links_count = 1;
-       inode.i_size = target_len;
+       ext2fs_inode_size_set(fs, &inode, target_len);
        /* The time fields are set by ext2fs_write_new_inode() */
 
+       inlinelink = !fastlink && ext2fs_has_feature_inline_data(fs->super);
        if (fastlink) {
                /* Fast symlinks, target stored in inode */
                strcpy((char *)&inode.i_block, target);
+       } else if (inlinelink) {
+               /* Try inserting an inline data symlink */
+               inode.i_flags |= EXT4_INLINE_DATA_FL;
+               retval = ext2fs_write_new_inode(fs, ino, &inode);
+               if (retval)
+                       goto cleanup;
+               retval = ext2fs_inline_data_set(fs, ino, &inode, block_buf,
+                                               target_len);
+               if (retval) {
+                       inode.i_flags &= ~EXT4_INLINE_DATA_FL;
+                       inlinelink = 0;
+                       goto need_block;
+               }
+               retval = ext2fs_read_inode(fs, ino, &inode);
+               if (retval)
+                       goto cleanup;
        } else {
+need_block:
                /* Slow symlinks, target stored in the first block */
-               memset(block_buf, 0, fs->blocksize);
-               strcpy(block_buf, target);
-               if (fs->super->s_feature_incompat &
-                   EXT3_FEATURE_INCOMPAT_EXTENTS) {
+               ext2fs_iblk_set(fs, &inode, 1);
+               if (ext2fs_has_feature_extents(fs->super)) {
                        /*
                         * The extent bmap is setup after the inode and block
                         * have been written out below.
@@ -104,11 +145,14 @@ errcode_t ext2fs_symlink(ext2_filsys fs, ext2_ino_t parent, ext2_ino_t ino,
         * number is assigned by write_new_inode, which means that the
         * operations using ino must come after it.
         */
-       retval = ext2fs_write_new_inode(fs, ino, &inode);
+       if (inlinelink)
+               retval = ext2fs_write_inode(fs, ino, &inode);
+       else
+               retval = ext2fs_write_new_inode(fs, ino, &inode);
        if (retval)
                goto cleanup;
 
-       if (!fastlink) {
+       if (!fastlink && !inlinelink) {
                retval = ext2fs_bmap2(fs, ino, &inode, NULL, BMAP_SET, 0, NULL,
                                      &blk);
                if (retval)
@@ -120,6 +164,14 @@ errcode_t ext2fs_symlink(ext2_filsys fs, ext2_ino_t parent, ext2_ino_t ino,
        }
 
        /*
+        * Update accounting....
+        */
+       if (!fastlink && !inlinelink)
+               ext2fs_block_alloc_stats2(fs, blk, +1);
+       ext2fs_inode_alloc_stats2(fs, ino, +1, 0);
+       drop_refcount = 1;
+
+       /*
         * Link the symlink into the filesystem hierarchy
         */
        if (name) {
@@ -135,16 +187,26 @@ errcode_t ext2fs_symlink(ext2_filsys fs, ext2_ino_t parent, ext2_ino_t ino,
                if (retval)
                        goto cleanup;
        }
-
-       /*
-        * Update accounting....
-        */
-       if (!fastlink)
-               ext2fs_block_alloc_stats2(fs, blk, +1);
-       ext2fs_inode_alloc_stats2(fs, ino, +1, 0);
+       drop_refcount = 0;
 
 cleanup:
        if (block_buf)
                ext2fs_free_mem(&block_buf);
+       if (drop_refcount) {
+               if (!fastlink && !inlinelink)
+                       ext2fs_block_alloc_stats2(fs, blk, -1);
+               ext2fs_inode_alloc_stats2(fs, ino, -1, 0);
+       }
        return retval;
 }
+
+/*
+ * Test whether an inode is a fast symlink.
+ *
+ * A fast symlink has its symlink data stored in inode->i_block.
+ */
+int ext2fs_is_fast_symlink(struct ext2_inode *inode)
+{
+       return LINUX_S_ISLNK(inode->i_mode) && EXT2_I_SIZE(inode) &&
+              EXT2_I_SIZE(inode) < sizeof(inode->i_block);
+}