Merge tag 'ext4_for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tytso...
[platform/kernel/linux-rpi.git] / fs / ext4 / ialloc.c
index 2400a82..b215c56 100644 (file)
@@ -862,6 +862,53 @@ out:
        return err;
 }
 
+static int ext4_xattr_credits_for_new_inode(struct inode *dir, mode_t mode,
+                                           bool encrypt)
+{
+       struct super_block *sb = dir->i_sb;
+       int nblocks = 0;
+#ifdef CONFIG_EXT4_FS_POSIX_ACL
+       struct posix_acl *p = get_acl(dir, ACL_TYPE_DEFAULT);
+
+       if (IS_ERR(p))
+               return PTR_ERR(p);
+       if (p) {
+               int acl_size = p->a_count * sizeof(ext4_acl_entry);
+
+               nblocks += (S_ISDIR(mode) ? 2 : 1) *
+                       __ext4_xattr_set_credits(sb, NULL /* inode */,
+                                                NULL /* block_bh */, acl_size,
+                                                true /* is_create */);
+               posix_acl_release(p);
+       }
+#endif
+
+#ifdef CONFIG_SECURITY
+       {
+               int num_security_xattrs = 1;
+
+#ifdef CONFIG_INTEGRITY
+               num_security_xattrs++;
+#endif
+               /*
+                * We assume that security xattrs are never more than 1k.
+                * In practice they are under 128 bytes.
+                */
+               nblocks += num_security_xattrs *
+                       __ext4_xattr_set_credits(sb, NULL /* inode */,
+                                                NULL /* block_bh */, 1024,
+                                                true /* is_create */);
+       }
+#endif
+       if (encrypt)
+               nblocks += __ext4_xattr_set_credits(sb,
+                                                   NULL /* inode */,
+                                                   NULL /* block_bh */,
+                                                   FSCRYPT_SET_CONTEXT_MAX_SIZE,
+                                                   true /* is_create */);
+       return nblocks;
+}
+
 /*
  * There are two policies for allocating an inode.  If the new inode is
  * a directory, then a forward search is made for a block group with both
@@ -892,7 +939,7 @@ struct inode *__ext4_new_inode(handle_t *handle, struct inode *dir,
        ext4_group_t i;
        ext4_group_t flex_group;
        struct ext4_group_info *grp = NULL;
-       int encrypt = 0;
+       bool encrypt = false;
 
        /* Cannot create files in a deleted directory */
        if (!dir || !dir->i_nlink)
@@ -904,59 +951,6 @@ struct inode *__ext4_new_inode(handle_t *handle, struct inode *dir,
        if (unlikely(ext4_forced_shutdown(sbi)))
                return ERR_PTR(-EIO);
 
-       if ((IS_ENCRYPTED(dir) || DUMMY_ENCRYPTION_ENABLED(sbi)) &&
-           (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)) &&
-           !(i_flags & EXT4_EA_INODE_FL)) {
-               err = fscrypt_get_encryption_info(dir);
-               if (err)
-                       return ERR_PTR(err);
-               if (!fscrypt_has_encryption_key(dir))
-                       return ERR_PTR(-ENOKEY);
-               encrypt = 1;
-       }
-
-       if (!handle && sbi->s_journal && !(i_flags & EXT4_EA_INODE_FL)) {
-#ifdef CONFIG_EXT4_FS_POSIX_ACL
-               struct posix_acl *p = get_acl(dir, ACL_TYPE_DEFAULT);
-
-               if (IS_ERR(p))
-                       return ERR_CAST(p);
-               if (p) {
-                       int acl_size = p->a_count * sizeof(ext4_acl_entry);
-
-                       nblocks += (S_ISDIR(mode) ? 2 : 1) *
-                               __ext4_xattr_set_credits(sb, NULL /* inode */,
-                                       NULL /* block_bh */, acl_size,
-                                       true /* is_create */);
-                       posix_acl_release(p);
-               }
-#endif
-
-#ifdef CONFIG_SECURITY
-               {
-                       int num_security_xattrs = 1;
-
-#ifdef CONFIG_INTEGRITY
-                       num_security_xattrs++;
-#endif
-                       /*
-                        * We assume that security xattrs are never
-                        * more than 1k.  In practice they are under
-                        * 128 bytes.
-                        */
-                       nblocks += num_security_xattrs *
-                               __ext4_xattr_set_credits(sb, NULL /* inode */,
-                                       NULL /* block_bh */, 1024,
-                                       true /* is_create */);
-               }
-#endif
-               if (encrypt)
-                       nblocks += __ext4_xattr_set_credits(sb,
-                                       NULL /* inode */, NULL /* block_bh */,
-                                       FSCRYPT_SET_CONTEXT_MAX_SIZE,
-                                       true /* is_create */);
-       }
-
        ngroups = ext4_get_groups_count(sb);
        trace_ext4_request_inode(dir, mode);
        inode = new_inode(sb);
@@ -986,10 +980,25 @@ struct inode *__ext4_new_inode(handle_t *handle, struct inode *dir,
        else
                ei->i_projid = make_kprojid(&init_user_ns, EXT4_DEF_PROJID);
 
+       if (!(i_flags & EXT4_EA_INODE_FL)) {
+               err = fscrypt_prepare_new_inode(dir, inode, &encrypt);
+               if (err)
+                       goto out;
+       }
+
        err = dquot_initialize(inode);
        if (err)
                goto out;
 
+       if (!handle && sbi->s_journal && !(i_flags & EXT4_EA_INODE_FL)) {
+               ret2 = ext4_xattr_credits_for_new_inode(dir, mode, encrypt);
+               if (ret2 < 0) {
+                       err = ret2;
+                       goto out;
+               }
+               nblocks += ret2;
+       }
+
        if (!goal)
                goal = sbi->s_inode_goal;
 
@@ -1295,7 +1304,7 @@ got:
         * prevent its deduplication.
         */
        if (encrypt) {
-               err = fscrypt_inherit_context(dir, inode, handle, true);
+               err = fscrypt_set_context(inode, handle);
                if (err)
                        goto fail_free_drop;
        }