endoffset_fsb = XFS_B_TO_FSBT(mp, offset + len);
 
        /* We can only free complete realtime extents. */
-       if (XFS_IS_REALTIME_INODE(ip)) {
-               xfs_extlen_t    extsz = xfs_get_extsz_hint(ip);
-
-               if ((startoffset_fsb | endoffset_fsb) & (extsz - 1))
-                       return -EINVAL;
+       if (XFS_IS_REALTIME_INODE(ip) && mp->m_sb.sb_rextsize > 1) {
+               startoffset_fsb = roundup_64(startoffset_fsb,
+                                            mp->m_sb.sb_rextsize);
+               endoffset_fsb = rounddown_64(endoffset_fsb,
+                                            mp->m_sb.sb_rextsize);
        }
 
        /*
 
        trace_xfs_insert_file_space(ip);
 
-       /* We can only insert complete realtime extents. */
-       if (XFS_IS_REALTIME_INODE(ip)) {
-               xfs_extlen_t    extsz = xfs_get_extsz_hint(ip);
-
-               if ((stop_fsb | shift_fsb) & (extsz - 1))
-                       return -EINVAL;
-       }
-
        error = xfs_bmap_can_insert_extents(ip, stop_fsb, shift_fsb);
        if (error)
                return error;
 
 
 static const struct vm_operations_struct xfs_file_vm_ops;
 
+/*
+ * Decide if the given file range is aligned to the size of the fundamental
+ * allocation unit for the file.
+ */
+static bool
+xfs_is_falloc_aligned(
+       struct xfs_inode        *ip,
+       loff_t                  pos,
+       long long int           len)
+{
+       struct xfs_mount        *mp = ip->i_mount;
+       uint64_t                mask;
+
+       if (XFS_IS_REALTIME_INODE(ip)) {
+               if (!is_power_of_2(mp->m_sb.sb_rextsize)) {
+                       u64     rextbytes;
+                       u32     mod;
+
+                       rextbytes = XFS_FSB_TO_B(mp, mp->m_sb.sb_rextsize);
+                       div_u64_rem(pos, rextbytes, &mod);
+                       if (mod)
+                               return false;
+                       div_u64_rem(len, rextbytes, &mod);
+                       return mod == 0;
+               }
+               mask = XFS_FSB_TO_B(mp, mp->m_sb.sb_rextsize) - 1;
+       } else {
+               mask = mp->m_sb.sb_blocksize - 1;
+       }
+
+       return !((pos | len) & mask);
+}
+
 int
 xfs_update_prealloc_flags(
        struct xfs_inode        *ip,
                if (error)
                        goto out_unlock;
        } else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
-               unsigned int blksize_mask = i_blocksize(inode) - 1;
-
-               if (offset & blksize_mask || len & blksize_mask) {
+               if (!xfs_is_falloc_aligned(ip, offset, len)) {
                        error = -EINVAL;
                        goto out_unlock;
                }
                if (error)
                        goto out_unlock;
        } else if (mode & FALLOC_FL_INSERT_RANGE) {
-               unsigned int    blksize_mask = i_blocksize(inode) - 1;
                loff_t          isize = i_size_read(inode);
 
-               if (offset & blksize_mask || len & blksize_mask) {
+               if (!xfs_is_falloc_aligned(ip, offset, len)) {
                        error = -EINVAL;
                        goto out_unlock;
                }
 
 #define xfs_sort(a,n,s,fn)     sort(a,n,s,fn,NULL)
 #define xfs_stack_trace()      dump_stack()
 
+static inline uint64_t rounddown_64(uint64_t x, uint32_t y)
+{
+       do_div(x, y);
+       return x * y;
+}
+
 static inline uint64_t roundup_64(uint64_t x, uint32_t y)
 {
        x += y - 1;