OSDN Git Service

Merge remote-tracking branch 'origin/upstream-f2fs-stable-linux-4.4.y' into android-4.4
[sagit-ice-cold/kernel_xiaomi_msm8998.git] / fs / f2fs / namei.c
index 6bb1adb..05193a1 100644 (file)
@@ -37,7 +37,7 @@ static struct inode *f2fs_new_inode(struct inode *dir, umode_t mode)
                return ERR_PTR(-ENOMEM);
 
        f2fs_lock_op(sbi);
-       if (!alloc_nid(sbi, &ino)) {
+       if (!f2fs_alloc_nid(sbi, &ino)) {
                f2fs_unlock_op(sbi);
                err = -ENOSPC;
                goto fail;
@@ -54,6 +54,9 @@ static struct inode *f2fs_new_inode(struct inode *dir, umode_t mode)
                        F2FS_I(inode)->i_crtime = current_time(inode);
        inode->i_generation = sbi->s_next_generation++;
 
+       if (S_ISDIR(inode->i_mode))
+               F2FS_I(inode)->i_current_depth = 1;
+
        err = insert_inode_locked(inode);
        if (err) {
                err = -EINVAL;
@@ -61,7 +64,7 @@ static struct inode *f2fs_new_inode(struct inode *dir, umode_t mode)
        }
 
        if (f2fs_sb_has_project_quota(sbi->sb) &&
-               (F2FS_I(dir)->i_flags & FS_PROJINHERIT_FL))
+               (F2FS_I(dir)->i_flags & F2FS_PROJINHERIT_FL))
                F2FS_I(inode)->i_projid = F2FS_I(dir)->i_projid;
        else
                F2FS_I(inode)->i_projid = make_kprojid(&init_user_ns,
@@ -78,7 +81,8 @@ static struct inode *f2fs_new_inode(struct inode *dir, umode_t mode)
        set_inode_flag(inode, FI_NEW_INODE);
 
        /* If the directory encrypted, then we should encrypt the inode. */
-       if (f2fs_encrypted_inode(dir) && f2fs_may_encrypt(inode))
+       if ((f2fs_encrypted_inode(dir) || DUMMY_ENCRYPTION_ENABLED(sbi)) &&
+                               f2fs_may_encrypt(inode))
                f2fs_set_encrypted_inode(inode);
 
        if (f2fs_sb_has_extra_attr(sbi->sb)) {
@@ -97,7 +101,7 @@ static struct inode *f2fs_new_inode(struct inode *dir, umode_t mode)
        if (f2fs_sb_has_flexible_inline_xattr(sbi->sb)) {
                f2fs_bug_on(sbi, !f2fs_has_extra_attr(inode));
                if (f2fs_has_inline_xattr(inode))
-                       xattr_size = sbi->inline_xattr_size;
+                       xattr_size = F2FS_OPTION(sbi).inline_xattr_size;
                /* Otherwise, will be 0 */
        } else if (f2fs_has_inline_xattr(inode) ||
                                f2fs_has_inline_dentry(inode)) {
@@ -115,9 +119,9 @@ static struct inode *f2fs_new_inode(struct inode *dir, umode_t mode)
                f2fs_mask_flags(mode, F2FS_I(dir)->i_flags & F2FS_FL_INHERITED);
 
        if (S_ISDIR(inode->i_mode))
-               F2FS_I(inode)->i_flags |= FS_INDEX_FL;
+               F2FS_I(inode)->i_flags |= F2FS_INDEX_FL;
 
-       if (F2FS_I(inode)->i_flags & FS_PROJINHERIT_FL)
+       if (F2FS_I(inode)->i_flags & F2FS_PROJINHERIT_FL)
                set_inode_flag(inode, FI_PROJ_INHERIT);
 
        trace_f2fs_new_inode(inode, 0);
@@ -142,7 +146,7 @@ fail_drop:
        return ERR_PTR(err);
 }
 
-static int is_multimedia_file(const unsigned char *s, const char *sub)
+static int is_extension_exist(const unsigned char *s, const char *sub)
 {
        size_t slen = strlen(s);
        size_t sublen = strlen(sub);
@@ -168,19 +172,94 @@ static int is_multimedia_file(const unsigned char *s, const char *sub)
 /*
  * Set multimedia files as cold files for hot/cold data separation
  */
-static inline void set_cold_files(struct f2fs_sb_info *sbi, struct inode *inode,
+static inline void set_file_temperature(struct f2fs_sb_info *sbi, struct inode *inode,
                const unsigned char *name)
 {
-       int i;
-       __u8 (*extlist)[8] = sbi->raw_super->extension_list;
+       __u8 (*extlist)[F2FS_EXTENSION_LEN] = sbi->raw_super->extension_list;
+       int i, cold_count, hot_count;
+
+       down_read(&sbi->sb_lock);
+
+       cold_count = le32_to_cpu(sbi->raw_super->extension_count);
+       hot_count = sbi->raw_super->hot_ext_count;
 
-       int count = le32_to_cpu(sbi->raw_super->extension_count);
-       for (i = 0; i < count; i++) {
-               if (is_multimedia_file(name, extlist[i])) {
+       for (i = 0; i < cold_count + hot_count; i++) {
+               if (!is_extension_exist(name, extlist[i]))
+                       continue;
+               if (i < cold_count)
                        file_set_cold(inode);
-                       break;
-               }
+               else
+                       file_set_hot(inode);
+               break;
+       }
+
+       up_read(&sbi->sb_lock);
+}
+
+int f2fs_update_extension_list(struct f2fs_sb_info *sbi, const char *name,
+                                                       bool hot, bool set)
+{
+       __u8 (*extlist)[F2FS_EXTENSION_LEN] = sbi->raw_super->extension_list;
+       int cold_count = le32_to_cpu(sbi->raw_super->extension_count);
+       int hot_count = sbi->raw_super->hot_ext_count;
+       int total_count = cold_count + hot_count;
+       int start, count;
+       int i;
+
+       if (set) {
+               if (total_count == F2FS_MAX_EXTENSION)
+                       return -EINVAL;
+       } else {
+               if (!hot && !cold_count)
+                       return -EINVAL;
+               if (hot && !hot_count)
+                       return -EINVAL;
+       }
+
+       if (hot) {
+               start = cold_count;
+               count = total_count;
+       } else {
+               start = 0;
+               count = cold_count;
        }
+
+       for (i = start; i < count; i++) {
+               if (strcmp(name, extlist[i]))
+                       continue;
+
+               if (set)
+                       return -EINVAL;
+
+               memcpy(extlist[i], extlist[i + 1],
+                               F2FS_EXTENSION_LEN * (total_count - i - 1));
+               memset(extlist[total_count - 1], 0, F2FS_EXTENSION_LEN);
+               if (hot)
+                       sbi->raw_super->hot_ext_count = hot_count - 1;
+               else
+                       sbi->raw_super->extension_count =
+                                               cpu_to_le32(cold_count - 1);
+               return 0;
+       }
+
+       if (!set)
+               return -EINVAL;
+
+       if (hot) {
+               memcpy(extlist[count], name, strlen(name));
+               sbi->raw_super->hot_ext_count = hot_count + 1;
+       } else {
+               char buf[F2FS_MAX_EXTENSION][F2FS_EXTENSION_LEN];
+
+               memcpy(buf, &extlist[cold_count],
+                               F2FS_EXTENSION_LEN * hot_count);
+               memset(extlist[cold_count], 0, F2FS_EXTENSION_LEN);
+               memcpy(extlist[cold_count], name, strlen(name));
+               memcpy(&extlist[cold_count + 1], buf,
+                               F2FS_EXTENSION_LEN * hot_count);
+               sbi->raw_super->extension_count = cpu_to_le32(cold_count + 1);
+       }
+       return 0;
 }
 
 static int f2fs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
@@ -203,7 +282,7 @@ static int f2fs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
                return PTR_ERR(inode);
 
        if (!test_opt(sbi, DISABLE_EXT_IDENTIFY))
-               set_cold_files(sbi, inode, dentry->d_name.name);
+               set_file_temperature(sbi, inode, dentry->d_name.name);
 
        inode->i_op = &f2fs_file_inode_operations;
        inode->i_fop = &f2fs_file_operations;
@@ -216,10 +295,9 @@ static int f2fs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
                goto out;
        f2fs_unlock_op(sbi);
 
-       alloc_nid_done(sbi, ino);
+       f2fs_alloc_nid_done(sbi, ino);
 
-       d_instantiate(dentry, inode);
-       unlock_new_inode(inode);
+       d_instantiate_new(dentry, inode);
 
        if (IS_DIRSYNC(dir))
                f2fs_sync_fs(sbi->sb, 1);
@@ -227,7 +305,7 @@ static int f2fs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
        f2fs_balance_fs(sbi, true);
        return 0;
 out:
-       handle_failed_inode(inode);
+       f2fs_handle_failed_inode(inode);
        return err;
 }
 
@@ -322,7 +400,7 @@ static int __recover_dot_dentries(struct inode *dir, nid_t pino)
                err = PTR_ERR(page);
                goto out;
        } else {
-               err = __f2fs_add_link(dir, &dot, NULL, dir->i_ino, S_IFDIR);
+               err = f2fs_do_add_link(dir, &dot, NULL, dir->i_ino, S_IFDIR);
                if (err)
                        goto out;
        }
@@ -333,7 +411,7 @@ static int __recover_dot_dentries(struct inode *dir, nid_t pino)
        else if (IS_ERR(page))
                err = PTR_ERR(page);
        else
-               err = __f2fs_add_link(dir, &dotdot, NULL, pino, S_IFDIR);
+               err = f2fs_do_add_link(dir, &dotdot, NULL, pino, S_IFDIR);
 out:
        if (!err)
                clear_inode_flag(dir, FI_INLINE_DOTS);
@@ -445,7 +523,7 @@ static int f2fs_unlink(struct inode *dir, struct dentry *dentry)
        f2fs_balance_fs(sbi, true);
 
        f2fs_lock_op(sbi);
-       err = acquire_orphan_inode(sbi);
+       err = f2fs_acquire_orphan_inode(sbi);
        if (err) {
                f2fs_unlock_op(sbi);
                f2fs_put_page(page, 0);
@@ -478,27 +556,16 @@ static int f2fs_symlink(struct inode *dir, struct dentry *dentry,
        struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
        struct inode *inode;
        size_t len = strlen(symname);
-       struct fscrypt_str disk_link = FSTR_INIT((char *)symname, len + 1);
-       struct fscrypt_symlink_data *sd = NULL;
+       struct fscrypt_str disk_link;
        int err;
 
        if (unlikely(f2fs_cp_error(sbi)))
                return -EIO;
 
-       if (f2fs_encrypted_inode(dir)) {
-               err = fscrypt_get_encryption_info(dir);
-               if (err)
-                       return err;
-
-               if (!fscrypt_has_encryption_key(dir))
-                       return -ENOKEY;
-
-               disk_link.len = (fscrypt_fname_encrypted_size(dir, len) +
-                               sizeof(struct fscrypt_symlink_data));
-       }
-
-       if (disk_link.len > dir->i_sb->s_blocksize)
-               return -ENAMETOOLONG;
+       err = fscrypt_prepare_symlink(dir, symname, len, dir->i_sb->s_blocksize,
+                                     &disk_link);
+       if (err)
+               return err;
 
        err = dquot_initialize(dir);
        if (err)
@@ -508,7 +575,7 @@ static int f2fs_symlink(struct inode *dir, struct dentry *dentry,
        if (IS_ERR(inode))
                return PTR_ERR(inode);
 
-       if (f2fs_encrypted_inode(inode))
+       if (IS_ENCRYPTED(inode))
                inode->i_op = &f2fs_encrypted_symlink_inode_operations;
        else
                inode->i_op = &f2fs_symlink_inode_operations;
@@ -518,44 +585,18 @@ static int f2fs_symlink(struct inode *dir, struct dentry *dentry,
        f2fs_lock_op(sbi);
        err = f2fs_add_link(dentry, inode);
        if (err)
-               goto out;
+               goto out_f2fs_handle_failed_inode;
        f2fs_unlock_op(sbi);
-       alloc_nid_done(sbi, inode->i_ino);
-
-       if (f2fs_encrypted_inode(inode)) {
-               struct qstr istr = QSTR_INIT(symname, len);
-               struct fscrypt_str ostr;
-
-               sd = f2fs_kzalloc(sbi, disk_link.len, GFP_NOFS);
-               if (!sd) {
-                       err = -ENOMEM;
-                       goto err_out;
-               }
-
-               err = fscrypt_get_encryption_info(inode);
-               if (err)
-                       goto err_out;
+       f2fs_alloc_nid_done(sbi, inode->i_ino);
 
-               if (!fscrypt_has_encryption_key(inode)) {
-                       err = -ENOKEY;
-                       goto err_out;
-               }
-
-               ostr.name = sd->encrypted_path;
-               ostr.len = disk_link.len;
-               err = fscrypt_fname_usr_to_disk(inode, &istr, &ostr);
-               if (err)
-                       goto err_out;
-
-               sd->len = cpu_to_le16(ostr.len);
-               disk_link.name = (char *)sd;
-       }
+       err = fscrypt_encrypt_symlink(inode, symname, len, &disk_link);
+       if (err)
+               goto err_out;
 
        err = page_symlink(inode, disk_link.name, disk_link.len);
 
 err_out:
-       d_instantiate(dentry, inode);
-       unlock_new_inode(inode);
+       d_instantiate_new(dentry, inode);
 
        /*
         * Let's flush symlink data in order to avoid broken symlink as much as
@@ -576,12 +617,14 @@ err_out:
                f2fs_unlink(dir, dentry);
        }
 
-       kfree(sd);
-
        f2fs_balance_fs(sbi, true);
-       return err;
-out:
-       handle_failed_inode(inode);
+       goto out_free_encrypted_link;
+
+out_f2fs_handle_failed_inode:
+       f2fs_handle_failed_inode(inode);
+out_free_encrypted_link:
+       if (disk_link.name != (unsigned char *)symname)
+               kfree(disk_link.name);
        return err;
 }
 
@@ -614,10 +657,9 @@ static int f2fs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
                goto out_fail;
        f2fs_unlock_op(sbi);
 
-       alloc_nid_done(sbi, inode->i_ino);
+       f2fs_alloc_nid_done(sbi, inode->i_ino);
 
-       d_instantiate(dentry, inode);
-       unlock_new_inode(inode);
+       d_instantiate_new(dentry, inode);
 
        if (IS_DIRSYNC(dir))
                f2fs_sync_fs(sbi->sb, 1);
@@ -627,7 +669,7 @@ static int f2fs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
 
 out_fail:
        clear_inode_flag(inode, FI_INC_LINK);
-       handle_failed_inode(inode);
+       f2fs_handle_failed_inode(inode);
        return err;
 }
 
@@ -666,10 +708,9 @@ static int f2fs_mknod(struct inode *dir, struct dentry *dentry,
                goto out;
        f2fs_unlock_op(sbi);
 
-       alloc_nid_done(sbi, inode->i_ino);
+       f2fs_alloc_nid_done(sbi, inode->i_ino);
 
-       d_instantiate(dentry, inode);
-       unlock_new_inode(inode);
+       d_instantiate_new(dentry, inode);
 
        if (IS_DIRSYNC(dir))
                f2fs_sync_fs(sbi->sb, 1);
@@ -677,7 +718,7 @@ static int f2fs_mknod(struct inode *dir, struct dentry *dentry,
        f2fs_balance_fs(sbi, true);
        return 0;
 out:
-       handle_failed_inode(inode);
+       f2fs_handle_failed_inode(inode);
        return err;
 }
 
@@ -706,7 +747,7 @@ static int __f2fs_tmpfile(struct inode *dir, struct dentry *dentry,
        }
 
        f2fs_lock_op(sbi);
-       err = acquire_orphan_inode(sbi);
+       err = f2fs_acquire_orphan_inode(sbi);
        if (err)
                goto out;
 
@@ -718,8 +759,8 @@ static int __f2fs_tmpfile(struct inode *dir, struct dentry *dentry,
         * add this non-linked tmpfile to orphan list, in this way we could
         * remove all unused data of tmpfile after abnormal power-off.
         */
-       add_orphan_inode(inode);
-       alloc_nid_done(sbi, inode->i_ino);
+       f2fs_add_orphan_inode(inode);
+       f2fs_alloc_nid_done(sbi, inode->i_ino);
 
        if (whiteout) {
                f2fs_i_links_write(inode, false);
@@ -735,18 +776,20 @@ static int __f2fs_tmpfile(struct inode *dir, struct dentry *dentry,
        return 0;
 
 release_out:
-       release_orphan_inode(sbi);
+       f2fs_release_orphan_inode(sbi);
 out:
-       handle_failed_inode(inode);
+       f2fs_handle_failed_inode(inode);
        return err;
 }
 
 static int f2fs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
 {
-       if (unlikely(f2fs_cp_error(F2FS_I_SB(dir))))
+       struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
+
+       if (unlikely(f2fs_cp_error(sbi)))
                return -EIO;
 
-       if (f2fs_encrypted_inode(dir)) {
+       if (f2fs_encrypted_inode(dir) || DUMMY_ENCRYPTION_ENABLED(sbi)) {
                int err = fscrypt_get_encryption_info(dir);
                if (err)
                        return err;
@@ -842,7 +885,7 @@ static int f2fs_rename(struct inode *old_dir, struct dentry *old_dentry,
 
                f2fs_lock_op(sbi);
 
-               err = acquire_orphan_inode(sbi);
+               err = f2fs_acquire_orphan_inode(sbi);
                if (err)
                        goto put_out_dir;
 
@@ -856,9 +899,9 @@ static int f2fs_rename(struct inode *old_dir, struct dentry *old_dentry,
                up_write(&F2FS_I(new_inode)->i_sem);
 
                if (!new_inode->i_nlink)
-                       add_orphan_inode(new_inode);
+                       f2fs_add_orphan_inode(new_inode);
                else
-                       release_orphan_inode(sbi);
+                       f2fs_release_orphan_inode(sbi);
        } else {
                f2fs_balance_fs(sbi, true);
 
@@ -926,7 +969,12 @@ static int f2fs_rename(struct inode *old_dir, struct dentry *old_dentry,
                        f2fs_put_page(old_dir_page, 0);
                f2fs_i_links_write(old_dir, false);
        }
-       add_ino_entry(sbi, new_dir->i_ino, TRANS_DIR_INO);
+       if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT) {
+               f2fs_add_ino_entry(sbi, new_dir->i_ino, TRANS_DIR_INO);
+               if (S_ISDIR(old_inode->i_mode))
+                       f2fs_add_ino_entry(sbi, old_inode->i_ino,
+                                                       TRANS_DIR_INO);
+       }
 
        f2fs_unlock_op(sbi);
 
@@ -1076,8 +1124,10 @@ static int f2fs_cross_rename(struct inode *old_dir, struct dentry *old_dentry,
        }
        f2fs_mark_inode_dirty_sync(new_dir, false);
 
-       add_ino_entry(sbi, old_dir->i_ino, TRANS_DIR_INO);
-       add_ino_entry(sbi, new_dir->i_ino, TRANS_DIR_INO);
+       if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT) {
+               f2fs_add_ino_entry(sbi, old_dir->i_ino, TRANS_DIR_INO);
+               f2fs_add_ino_entry(sbi, new_dir->i_ino, TRANS_DIR_INO);
+       }
 
        f2fs_unlock_op(sbi);
 
@@ -1127,65 +1177,21 @@ static int f2fs_rename2(struct inode *old_dir, struct dentry *old_dentry,
 
 static const char *f2fs_encrypted_follow_link(struct dentry *dentry, void **cookie)
 {
-       struct page *cpage = NULL;
-       char *caddr, *paddr = NULL;
-       struct fscrypt_str cstr = FSTR_INIT(NULL, 0);
-       struct fscrypt_str pstr = FSTR_INIT(NULL, 0);
-       struct fscrypt_symlink_data *sd;
        struct inode *inode = d_inode(dentry);
-       u32 max_size = inode->i_sb->s_blocksize;
-       int res;
-
-       res = fscrypt_get_encryption_info(inode);
-       if (res)
-               return ERR_PTR(res);
-
-       cpage = read_mapping_page(inode->i_mapping, 0, NULL);
-       if (IS_ERR(cpage))
-               return ERR_CAST(cpage);
-       caddr = page_address(cpage);
-
-       /* Symlink is encrypted */
-       sd = (struct fscrypt_symlink_data *)caddr;
-       cstr.name = sd->encrypted_path;
-       cstr.len = le16_to_cpu(sd->len);
-
-       /* this is broken symlink case */
-       if (unlikely(cstr.len == 0)) {
-               res = -ENOENT;
-               goto errout;
-       }
-
-       if ((cstr.len + sizeof(struct fscrypt_symlink_data) - 1) > max_size) {
-               /* Symlink data on the disk is corrupted */
-               res = -EIO;
-               goto errout;
-       }
-       res = fscrypt_fname_alloc_buffer(inode, cstr.len, &pstr);
-       if (res)
-               goto errout;
-
-       res = fscrypt_fname_disk_to_usr(inode, 0, 0, &cstr, &pstr);
-       if (res)
-               goto errout;
-
-       /* this is broken symlink case */
-       if (unlikely(pstr.name[0] == 0)) {
-               res = -ENOENT;
-               goto errout;
-       }
+       struct page *page;
+       void *target;
 
-       paddr = pstr.name;
+       if (!dentry)
+               return ERR_PTR(-ECHILD);
 
-       /* Null-terminate the name */
-       paddr[pstr.len] = '\0';
+       page = read_mapping_page(inode->i_mapping, 0, NULL);
+       if (IS_ERR(page))
+               return ERR_CAST(page);
 
-       put_page(cpage);
-       return *cookie = paddr;
-errout:
-       fscrypt_fname_free_buffer(&pstr);
-       put_page(cpage);
-       return ERR_PTR(res);
+       target = fscrypt_get_symlink(inode, page_address(page),
+                                    inode->i_sb->s_blocksize);
+       put_page(page);
+       return *cookie = target;
 }
 
 const struct inode_operations f2fs_encrypted_symlink_inode_operations = {