* to the correct offsets in the file
                 */
                if (state && (state->end + 1 != start ||
-                   !state->state & EXTENT_WRITEBACK)) {
+                   !(state->state & EXTENT_WRITEBACK))) {
                        state = NULL;
                }
                spin_unlock_irqrestore(&tree->lock, flags);
                 * to the correct offsets in the file
                 */
                if (state && (state->end + 1 != start ||
-                   !state->state & EXTENT_WRITEBACK)) {
+                   !(state->state & EXTENT_LOCKED))) {
                        state = NULL;
                }
                spin_unlock_irqrestore(&tree->lock, flags);
 
        if (btrfs_test_opt(root, NODATASUM) ||
            btrfs_test_flag(inode, NODATASUM))
                return 0;
-
-       if (state->start == start) {
+       if (state && state->start == start) {
                private = state->private;
                ret = 0;
        } else {