Skip to contentSkip to conversation

iomap: swapfile tweaks

PATCH In review Omar Sandoval 4 participants 8 messages May 16, 2018 – May 17, 2018 VFS

lore
Share & tools
lore
#0 lore raw

v3 cover iomap: swapfile tweaks

From: Omar Sandoval <[email protected]>

Changes from v2:

- Move up inline flag check, which Christoph is going to turn into a type at some point
- Remove null address check, since it doesn't make sense for IOMAP_MAPPED or IOMAP_UNWRITTEN

Omar Sandoval (2):
iomap: provide more useful errors for invalid swap files
iomap: don't allow holes in swapfiles

 fs/iomap.c | 43 +++++++++++++++++++++++--------------------
 1 file changed, 23 insertions(+), 20 deletions(-)
Signature · 1 line
2.17.0
#1 lore raw

PATCH v3 1/2 iomap: provide more useful errors for invalid swap files

From: Omar Sandoval <[email protected]>

Currently, for an invalid swap file, we print the same error message regardless of the reason. This isn't very useful for an admin, who will likely want to know why exactly they can't use their swap file. So, let's add specific error messages for each reason, and also move the bdev check after the flags checks, since the latter are more fundamental.

---
 fs/iomap.c | 40 ++++++++++++++++++++++++----------------
 1 file changed, 24 insertions(+), 16 deletions(-)
fs/iomap.c +24 -16
index d193390a1c20..89517442e296 100644
@@ -1214,26 +1214,37 @@ static loff_t iomap_swapfile_activate_actor(struct inode *inode, loff_t pos,
 	struct iomap_swapfile_info *isi = data;
 	int error;
 
+	/* No inline data. */
+	if (iomap->flags & IOMAP_F_DATA_INLINE) {
+		pr_err("swapon: file is inline\n");
+		return -EINVAL;
+	}
+
 	/* Skip holes. */
 	if (iomap->type == IOMAP_HOLE)
 		goto out;
 
-	/* Only one bdev per swap file. */
-	if (iomap->bdev != isi->sis->bdev)
-		goto err;
-
 	/* Only real or unwritten extents. */
-	if (iomap->type != IOMAP_MAPPED && iomap->type != IOMAP_UNWRITTEN)
-		goto err;
+	if (iomap->type != IOMAP_MAPPED && iomap->type != IOMAP_UNWRITTEN) {
+		pr_err("swapon: file has unallocated extents\n");
+		return -EINVAL;
+	}
 
-	/* No uncommitted metadata or shared blocks or inline data. */
-	if (iomap->flags & (IOMAP_F_DIRTY | IOMAP_F_SHARED |
-			    IOMAP_F_DATA_INLINE))
-		goto err;
+	/* No uncommitted metadata or shared blocks. */
+	if (iomap->flags & IOMAP_F_DIRTY) {
+		pr_err("swapon: file is not committed\n");
+		return -EINVAL;
+	}
+	if (iomap->flags & IOMAP_F_SHARED) {
+		pr_err("swapon: file has shared extents\n");
+		return -EINVAL;
+	}
 
-	/* No null physical addresses. */
-	if (iomap->addr == IOMAP_NULL_ADDR)
-		goto err;
+	/* Only one bdev per swap file. */
+	if (iomap->bdev != isi->sis->bdev) {
+		pr_err("swapon: file is on multiple devices\n");
+		return -EINVAL;
+	}
 
 	if (isi->iomap.length == 0) {
 		/* No accumulated extent, so just store it. */
@@ -1250,9 +1261,6 @@ static loff_t iomap_swapfile_activate_actor(struct inode *inode, loff_t pos,
 	}
 out:
 	return count;
-err:
-	pr_err("swapon: file cannot be used for swap\n");
-	return -EINVAL;
 }
 
 /*
Signature · 1 line
2.17.0
#4 lore raw

PATCH v3 2/2 iomap: don't allow holes in swapfiles

In reply to Omar Sandoval

From: Omar Sandoval <[email protected]>

generic_swapfile_activate() doesn't allow holes, so we should be consistent here. This is also a bit safer: if the user creates a swapfile with, say, truncate -s $SIZE followed by mkswap, they should really get an error and not much less swap space than they expected. swapon(8) will error out before calling swapon(2) if the file has holes, anyways.

Fixes 9d93388b0afe ("iomap: add a swapfile activation function")
---
 fs/iomap.c | 5 -----
 1 file changed, 5 deletions(-)
fs/iomap.c +0 -5
index 89517442e296..f2456d0d8ddd 100644
@@ -1220,10 +1220,6 @@ static loff_t iomap_swapfile_activate_actor(struct inode *inode, loff_t pos,
 		return -EINVAL;
 	}
 
-	/* Skip holes. */
-	if (iomap->type == IOMAP_HOLE)
-		goto out;
-
 	/* Only real or unwritten extents. */
 	if (iomap->type != IOMAP_MAPPED && iomap->type != IOMAP_UNWRITTEN) {
 		pr_err("swapon: file has unallocated extents\n");
@@ -1259,7 +1255,6 @@ static loff_t iomap_swapfile_activate_actor(struct inode *inode, loff_t pos,
 			return error;
 		memcpy(&isi->iomap, iomap, sizeof(isi->iomap));
 	}
-out:
 	return count;
 }
 
Signature · 1 line
2.17.0
reviewer Reviewed-by on v3 1/2
#3 lore raw

Re: [PATCH v3 1/2] iomap: provide more useful errors for invalid swap files

In reply to Omar Sandoval
Quoted 11 lines · Omar Sandoval

From: Omar Sandoval <[email protected]>

Currently, for an invalid swap file, we print the same error message regardless of the reason. This isn't very useful for an admin, who will likely want to know why exactly they can't use their swap file. So, let's add specific error messages for each reason, and also move the bdev check after the flags checks, since the latter are more fundamental.

Yup, looks good to me. You can add:

Honza

Quoted 72 lines
---
 fs/iomap.c | 40 ++++++++++++++++++++++++----------------
 1 file changed, 24 insertions(+), 16 deletions(-)
fs/iomap.c +24 -16
index d193390a1c20..89517442e296 100644
@@ -1214,26 +1214,37 @@ static loff_t iomap_swapfile_activate_actor(struct inode *inode, loff_t pos,
 	struct iomap_swapfile_info *isi = data;
 	int error;
 
+	/* No inline data. */
+	if (iomap->flags & IOMAP_F_DATA_INLINE) {
+		pr_err("swapon: file is inline\n");
+		return -EINVAL;
+	}
+
 	/* Skip holes. */
 	if (iomap->type == IOMAP_HOLE)
 		goto out;
 
-	/* Only one bdev per swap file. */
-	if (iomap->bdev != isi->sis->bdev)
-		goto err;
-
 	/* Only real or unwritten extents. */
-	if (iomap->type != IOMAP_MAPPED && iomap->type != IOMAP_UNWRITTEN)
-		goto err;
+	if (iomap->type != IOMAP_MAPPED && iomap->type != IOMAP_UNWRITTEN) {
+		pr_err("swapon: file has unallocated extents\n");
+		return -EINVAL;
+	}
 
-	/* No uncommitted metadata or shared blocks or inline data. */
-	if (iomap->flags & (IOMAP_F_DIRTY | IOMAP_F_SHARED |
-			    IOMAP_F_DATA_INLINE))
-		goto err;
+	/* No uncommitted metadata or shared blocks. */
+	if (iomap->flags & IOMAP_F_DIRTY) {
+		pr_err("swapon: file is not committed\n");
+		return -EINVAL;
+	}
+	if (iomap->flags & IOMAP_F_SHARED) {
+		pr_err("swapon: file has shared extents\n");
+		return -EINVAL;
+	}
 
-	/* No null physical addresses. */
-	if (iomap->addr == IOMAP_NULL_ADDR)
-		goto err;
+	/* Only one bdev per swap file. */
+	if (iomap->bdev != isi->sis->bdev) {
+		pr_err("swapon: file is on multiple devices\n");
+		return -EINVAL;
+	}
 
 	if (isi->iomap.length == 0) {
 		/* No accumulated extent, so just store it. */
@@ -1250,9 +1261,6 @@ static loff_t iomap_swapfile_activate_actor(struct inode *inode, loff_t pos,
 	}
 out:
 	return count;
-err:
-	pr_err("swapon: file cannot be used for swap\n");
-	return -EINVAL;
 }
 
 /*
Signature · 1 line
2.17.0
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
reviewer Reviewed-by on v3 2/2
#6 lore raw

Re: [PATCH v3 2/2] iomap: don't allow holes in swapfiles

In reply to Omar Sandoval
Quoted 9 lines · Omar Sandoval

From: Omar Sandoval <[email protected]>

generic_swapfile_activate() doesn't allow holes, so we should be consistent here. This is also a bit safer: if the user creates a swapfile with, say, truncate -s $SIZE followed by mkswap, they should really get an error and not much less swap space than they expected. swapon(8) will error out before calling swapon(2) if the file has holes, anyways.

Quoted 3 lines
Fixes 9d93388b0afe ("iomap: add a swapfile activation function")

Good catch. You can add:

Honza

Quoted 30 lines
---
 fs/iomap.c | 5 -----
 1 file changed, 5 deletions(-)
fs/iomap.c +0 -5
index 89517442e296..f2456d0d8ddd 100644
@@ -1220,10 +1220,6 @@ static loff_t iomap_swapfile_activate_actor(struct inode *inode, loff_t pos,
 		return -EINVAL;
 	}
 
-	/* Skip holes. */
-	if (iomap->type == IOMAP_HOLE)
-		goto out;
-
 	/* Only real or unwritten extents. */
 	if (iomap->type != IOMAP_MAPPED && iomap->type != IOMAP_UNWRITTEN) {
 		pr_err("swapon: file has unallocated extents\n");
@@ -1259,7 +1255,6 @@ static loff_t iomap_swapfile_activate_actor(struct inode *inode, loff_t pos,
 			return error;
 		memcpy(&isi->iomap, iomap, sizeof(isi->iomap));
 	}
-out:
 	return count;
 }
 
Signature · 1 line
2.17.0
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
Reviewed-by on the v3 series
#7 lore raw
In reply to Omar Sandoval
Quoted 9 lines · Omar Sandoval

From: Omar Sandoval <[email protected]>

Changes from v2:

- Move up inline flag check, which Christoph is going to turn into a type at some point
- Remove null address check, since it doesn't make sense for IOMAP_MAPPED or IOMAP_UNWRITTEN

Quoted 5 lines

Omar Sandoval (2):
iomap: provide more useful errors for invalid swap files
iomap: don't allow holes in swapfiles

 fs/iomap.c | 43 +++++++++++++++++++++++--------------------

Tested ok overnight, so:

--D

Quoted 9 lines
 1 file changed, 23 insertions(+), 20 deletions(-)
Signature · 6 lines
2.17.0

--
To unsubscribe from this list: send the line "unsubscribe linux-xfs" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html