#include <linux/magic.h>
 #include <linux/kobject.h>
 #include <linux/sched.h>
+#include <linux/cred.h>
 #include <linux/vmalloc.h>
 #include <linux/bio.h>
 #include <linux/blkdev.h>
 #define F2FS_CLEAR_FEATURE(sb, mask)                                   \
        (F2FS_SB(sb)->raw_super->feature &= ~cpu_to_le32(mask))
 
+/*
+ * Default values for user and/or group using reserved blocks
+ */
+#define        F2FS_DEF_RESUID         0
+#define        F2FS_DEF_RESGID         0
+
 /*
  * For checkpoint manager
  */
        block_t reserved_blocks;                /* configurable reserved blocks */
        block_t current_reserved_blocks;        /* current reserved blocks */
        block_t root_reserved_blocks;           /* root reserved blocks */
+       kuid_t s_resuid;                        /* reserved blocks for uid */
+       kgid_t s_resgid;                        /* reserved blocks for gid */
 
        unsigned int nquota_files;              /* # of quota sysfile */
 
        return ofs == XATTR_NODE_OFFSET;
 }
 
+static inline bool __allow_reserved_blocks(struct f2fs_sb_info *sbi)
+{
+       if (!test_opt(sbi, RESERVE_ROOT))
+               return false;
+       if (capable(CAP_SYS_RESOURCE))
+               return true;
+       if (uid_eq(sbi->s_resuid, current_fsuid()))
+               return true;
+       if (!gid_eq(sbi->s_resgid, GLOBAL_ROOT_GID) &&
+                                       in_group_p(sbi->s_resgid))
+               return true;
+       return false;
+}
+
 static inline void f2fs_i_blocks_write(struct inode *, block_t, bool, bool);
 static inline int inc_valid_block_count(struct f2fs_sb_info *sbi,
                                 struct inode *inode, blkcnt_t *count)
        avail_user_block_count = sbi->user_block_count -
                                        sbi->current_reserved_blocks;
 
-       if (!(test_opt(sbi, RESERVE_ROOT) && capable(CAP_SYS_RESOURCE)))
+       if (!__allow_reserved_blocks(sbi))
                avail_user_block_count -= sbi->root_reserved_blocks;
 
        if (unlikely(sbi->total_valid_block_count > avail_user_block_count)) {
        valid_block_count = sbi->total_valid_block_count +
                                        sbi->current_reserved_blocks + 1;
 
-       if (!(test_opt(sbi, RESERVE_ROOT) && capable(CAP_SYS_RESOURCE)))
+       if (!__allow_reserved_blocks(sbi))
                valid_block_count += sbi->root_reserved_blocks;
 
        if (unlikely(valid_block_count > sbi->user_block_count)) {
 
        Opt_noinline_data,
        Opt_data_flush,
        Opt_reserve_root,
+       Opt_resgid,
+       Opt_resuid,
        Opt_mode,
        Opt_io_size_bits,
        Opt_fault_injection,
        {Opt_noinline_data, "noinline_data"},
        {Opt_data_flush, "data_flush"},
        {Opt_reserve_root, "reserve_root=%u"},
+       {Opt_resgid, "resgid=%u"},
+       {Opt_resuid, "resuid=%u"},
        {Opt_mode, "mode=%s"},
        {Opt_io_size_bits, "io_bits=%u"},
        {Opt_fault_injection, "fault_injection=%u"},
                        "Reduce reserved blocks for root = %u",
                                sbi->root_reserved_blocks);
        }
+       if (!test_opt(sbi, RESERVE_ROOT) &&
+               (!uid_eq(sbi->s_resuid,
+                               make_kuid(&init_user_ns, F2FS_DEF_RESUID)) ||
+               !gid_eq(sbi->s_resgid,
+                               make_kgid(&init_user_ns, F2FS_DEF_RESGID))))
+               f2fs_msg(sbi->sb, KERN_INFO,
+                       "Ignore s_resuid=%u, s_resgid=%u w/o reserve_root",
+                               from_kuid_munged(&init_user_ns, sbi->s_resuid),
+                               from_kgid_munged(&init_user_ns, sbi->s_resgid));
 }
 
 static void init_once(void *foo)
        substring_t args[MAX_OPT_ARGS];
        char *p, *name;
        int arg = 0;
+       kuid_t uid;
+       kgid_t gid;
 #ifdef CONFIG_QUOTA
        int ret;
 #endif
                                set_opt(sbi, RESERVE_ROOT);
                        }
                        break;
+               case Opt_resuid:
+                       if (args->from && match_int(args, &arg))
+                               return -EINVAL;
+                       uid = make_kuid(current_user_ns(), arg);
+                       if (!uid_valid(uid)) {
+                               f2fs_msg(sb, KERN_ERR,
+                                       "Invalid uid value %d", arg);
+                               return -EINVAL;
+                       }
+                       sbi->s_resuid = uid;
+                       break;
+               case Opt_resgid:
+                       if (args->from && match_int(args, &arg))
+                               return -EINVAL;
+                       gid = make_kgid(current_user_ns(), arg);
+                       if (!gid_valid(gid)) {
+                               f2fs_msg(sb, KERN_ERR,
+                                       "Invalid gid value %d", arg);
+                               return -EINVAL;
+                       }
+                       sbi->s_resgid = gid;
+                       break;
                case Opt_mode:
                        name = match_strdup(&args[0]);
 
                seq_puts(seq, "lfs");
        seq_printf(seq, ",active_logs=%u", sbi->active_logs);
        if (test_opt(sbi, RESERVE_ROOT))
-               seq_printf(seq, ",reserve_root=%u",
-                               sbi->root_reserved_blocks);
+               seq_printf(seq, ",reserve_root=%u,resuid=%u,resgid=%u",
+                               sbi->root_reserved_blocks,
+                               from_kuid_munged(&init_user_ns, sbi->s_resuid),
+                               from_kgid_munged(&init_user_ns, sbi->s_resgid));
        if (F2FS_IO_SIZE_BITS(sbi))
                seq_printf(seq, ",io_size=%uKB", F2FS_IO_SIZE_KB(sbi));
 #ifdef CONFIG_F2FS_FAULT_INJECTION
        sb->s_fs_info = sbi;
        sbi->raw_super = raw_super;
 
+       sbi->s_resuid = make_kuid(&init_user_ns, F2FS_DEF_RESUID);
+       sbi->s_resgid = make_kgid(&init_user_ns, F2FS_DEF_RESGID);
+
        /* precompute checksum seed for metadata */
        if (f2fs_sb_has_inode_chksum(sb))
                sbi->s_chksum_seed = f2fs_chksum(sbi, ~0, raw_super->uuid,