Btrfs: online shrinking fixes

While shrinking the FS, the allocation functions need to make sure
they don't try to allocate bytes past the end of the FS.

nodatacow needed an extra check to force cows when the existing extents are
past the end of the FS.

Signed-off-by: Chris Mason <chris.mason@oracle.com>
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index 2cb2dd3..2817570 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -148,6 +148,7 @@
 	u64 bytenr;
 	u64 cow_end;
 	u64 loops = 0;
+	u64 total_fs_bytes;
 	struct btrfs_root *root = BTRFS_I(inode)->root;
 	struct extent_buffer *leaf;
 	int found_type;
@@ -157,6 +158,7 @@
 	int err;
 	struct btrfs_key found_key;
 
+	total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
 	path = btrfs_alloc_path();
 	BUG_ON(!path);
 again:
@@ -189,8 +191,10 @@
 	found_type = btrfs_file_extent_type(leaf, item);
 	extent_start = found_key.offset;
 	if (found_type == BTRFS_FILE_EXTENT_REG) {
-		extent_end = extent_start +
-		       btrfs_file_extent_num_bytes(leaf, item);
+		u64 extent_num_bytes;
+
+		extent_num_bytes = btrfs_file_extent_num_bytes(leaf, item);
+		extent_end = extent_start + extent_num_bytes;
 		err = 0;
 
 		if (loops && start != extent_start)
@@ -204,6 +208,13 @@
 		if (bytenr == 0)
 			goto not_found;
 
+		/*
+		 * we may be called by the resizer, make sure we're inside
+		 * the limits of the FS
+		 */
+		if (bytenr + extent_num_bytes > total_fs_bytes)
+			goto not_found;
+
 		if (btrfs_count_snapshots_in_path(root, path, bytenr) != 1) {
 			goto not_found;
 		}