diff mbox

[v2,1/2] iomap: provide more useful errors for invalid swap files

Message ID 20180516173251.GA29231@vader (mailing list archive)
State New, archived
Headers show

Commit Message

Omar Sandoval May 16, 2018, 5:32 p.m. UTC
On Wed, May 16, 2018 at 10:25:31AM -0700, Christoph Hellwig wrote:
> On Wed, May 16, 2018 at 09:45:50AM -0700, Omar Sandoval wrote:
> > +	if ((iomap->type != IOMAP_MAPPED && iomap->type != IOMAP_UNWRITTEN) ||
> > +	    iomap->addr == IOMAP_NULL_ADDR) {
> > +		pr_err("swapon: file has unallocated extents\n");
> > +		return -EINVAL;
> > +	}
> 
> The two are really different cases - IOMAP_NULL_ADDR shouldn't really
> happen for any of the above.  Although we might have to move the
> inline check before the type check above for the message to make sense.
> 
> I have a patch in the local queue that makes inline a type instead of
> a flag, btw as it really isn't a flag.

So something like this, moving the inline check and removing the hole
check since that doesn't make sense for mapped or unwritten? Then the
inline flag check can be converted to a type check.

Comments

Christoph Hellwig May 16, 2018, 5:46 p.m. UTC | #1
On Wed, May 16, 2018 at 10:32:51AM -0700, Omar Sandoval wrote:
> On Wed, May 16, 2018 at 10:25:31AM -0700, Christoph Hellwig wrote:
> > On Wed, May 16, 2018 at 09:45:50AM -0700, Omar Sandoval wrote:
> > > +	if ((iomap->type != IOMAP_MAPPED && iomap->type != IOMAP_UNWRITTEN) ||
> > > +	    iomap->addr == IOMAP_NULL_ADDR) {
> > > +		pr_err("swapon: file has unallocated extents\n");
> > > +		return -EINVAL;
> > > +	}
> > 
> > The two are really different cases - IOMAP_NULL_ADDR shouldn't really
> > happen for any of the above.  Although we might have to move the
> > inline check before the type check above for the message to make sense.
> > 
> > I have a patch in the local queue that makes inline a type instead of
> > a flag, btw as it really isn't a flag.
> 
> So something like this, moving the inline check and removing the hole
> check since that doesn't make sense for mapped or unwritten? Then the
> inline flag check can be converted to a type check.

Yes, this looks great!
Darrick J. Wong May 16, 2018, 6:11 p.m. UTC | #2
On Wed, May 16, 2018 at 10:46:45AM -0700, Christoph Hellwig wrote:
> On Wed, May 16, 2018 at 10:32:51AM -0700, Omar Sandoval wrote:
> > On Wed, May 16, 2018 at 10:25:31AM -0700, Christoph Hellwig wrote:
> > > On Wed, May 16, 2018 at 09:45:50AM -0700, Omar Sandoval wrote:
> > > > +	if ((iomap->type != IOMAP_MAPPED && iomap->type != IOMAP_UNWRITTEN) ||
> > > > +	    iomap->addr == IOMAP_NULL_ADDR) {
> > > > +		pr_err("swapon: file has unallocated extents\n");
> > > > +		return -EINVAL;
> > > > +	}
> > > 
> > > The two are really different cases - IOMAP_NULL_ADDR shouldn't really
> > > happen for any of the above.  Although we might have to move the
> > > inline check before the type check above for the message to make sense.
> > > 
> > > I have a patch in the local queue that makes inline a type instead of
> > > a flag, btw as it really isn't a flag.

That's what I thought -- we only see NULL_ADDR for holes and delalloc
extents, and we already check for both of those.  But I didn't see
anything in iomap.h expressly saying that, so I decided to be defensive
and check it anyway.

IOWs it would be helpful to have a little more documentation about which
flags go together, particularly for things like IOMAP_F_BOUNDARY that
don't have any meaning in xfs.

> > So something like this, moving the inline check and removing the hole
> > check since that doesn't make sense for mapped or unwritten? Then the
> > inline flag check can be converted to a type check.
> 
> Yes, this looks great!

Yeah, the v3 patch looks ok.

--D

> --
> To unsubscribe from this list: send the line "unsubscribe linux-xfs" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
Christoph Hellwig May 16, 2018, 6:22 p.m. UTC | #3
On Wed, May 16, 2018 at 11:11:42AM -0700, Darrick J. Wong wrote:
> That's what I thought -- we only see NULL_ADDR for holes and delalloc
> extents, and we already check for both of those.  But I didn't see
> anything in iomap.h expressly saying that, so I decided to be defensive
> and check it anyway.
> 
> IOWs it would be helpful to have a little more documentation about which
> flags go together,

patches welcome :)

> particularly for things like IOMAP_F_BOUNDARY that
> don't have any meaning in xfs.

Yikes, how did that even end in the tree?  That whole boundary
thing already made little sene on buffer heads, and even less
so in the generic iomap code.  I think we just need to allocate
a few flags for fs specific uses and move it into gfs2.
diff mbox

Patch

diff --git a/fs/iomap.c b/fs/iomap.c
index d193390a1c20..89517442e296 100644
--- a/fs/iomap.c
+++ b/fs/iomap.c
@@ -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;
 }
 
 /*