linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Hugh Dickins <hugh@veritas.com>
To: Willy Tarreau <w@1wt.eu>
Cc: Peter Cordes <peter@cordes.ca>, Bodo Eggert <7eggert@gmx.de>,
	David Newall <davidn@davidnewall.com>,
	Peter Zijlstra <peterz@infradead.org>,
	linux-kernel@vger.kernel.org,
	Christoph Hellwig <hch@infradead.org>,
	linux-mm <linux-mm@kvack.org>
Subject: Re: no way to swapoff a deleted swap file?
Date: Sat, 18 Oct 2008 21:45:14 +0100 (BST)	[thread overview]
Message-ID: <Pine.LNX.4.64.0810182058120.7154@blonde.site> (raw)
In-Reply-To: <20081018051800.GO24654@1wt.eu>

On Sat, 18 Oct 2008, Willy Tarreau wrote:
> (...)
> I have another idea which might be simpler to implement in userspace.
> What happened to you is a typical accident, you did not run on purpose
> on a deleted swap file. So we should at least ensure that such types
> of accidents could not happen easily.
> 
> If swapon did set the immutable bit on a file just after enabling swap
> to it, it would at least prevent accidental removal of that file. Swapoff
> would have to clean that bit, and swapon would have to clean it upon
> startup too (in case of unplanned reboots).
> 
> That way, you could still remove such files on purpose provided you do
> a preliminary "chattr -i" on them, but "rm -rf" would keep them intact.

That's a good idea, thank you Willy:
much more to my taste than previous suggestions.

But I'm still not tempted to build it into the swapon and swapoff,
neither at the command nor at the kernel level.  Let's leave it as
advice to sufferers on how to address the issue if it troubles them.

I did play with immutable on swapfiles back around 2.6.8.  Prior
to that we left i_sem downed on a swapfile to protect it against
truncation (freeing its pages!) while in use - which caused
anyone idly touching it to hang, not very nice.

I experimented with setting immutable in sys_swapon, clearing it
in sys_swapoff, but I see from old mails that I didn't find that
satisfactory: I haven't actually recorded why not, but I think it
was partly a difficulty in getting the locking right, and partly
what happened if the user also made it immutable while swapped on -
oh, yes, and immutable gets written back to the filesystem which is
inconvenient when we crash, as you observe.  So we ended up adding
an additional swapfile flag just at the VFS level.

Hmm, I suppose it would be very easy to make that additional swapfile
flag prohibit unlinking just as immutable does: patch below should do
that.  What do you guys think - should we include this?  It does then
(barring races which I don't propose to worry about) make an unlinked
swapfile impossible, which earlier had seemed a reasonable option.

> It would also prevent accidental modifications, such as "ls .>swapfile"
> instead of "ls ./swapfile".

That we do already protect against with the swapfile flag: we don't
actually protect against writing (that's just a permission thing,
same as when swapping to block device), but we do protect against
truncation, which would otherwise end up with swap corrupting
blocks of other files.

Hugh

--- 2.6.27/fs/namei.c	2008-10-09 23:13:53.000000000 +0100
+++ linux/fs/namei.c	2008-10-18 21:33:01.000000000 +0100
@@ -1407,7 +1407,7 @@ static int may_delete(struct inode *dir,
 	if (IS_APPEND(dir))
 		return -EPERM;
 	if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
-	    IS_IMMUTABLE(victim->d_inode))
+	    IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
 		return -EPERM;
 	if (isdir) {
 		if (!S_ISDIR(victim->d_inode->i_mode))

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org.  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>

  reply	other threads:[~2008-10-18 20:45 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <bnlDw-5vQ-7@gated-at.bofh.it>
     [not found] ` <bnwpg-2EA-17@gated-at.bofh.it>
     [not found]   ` <bnJFK-3bu-7@gated-at.bofh.it>
2008-10-16 23:43     ` Bodo Eggert
2008-10-16 23:43     ` Bodo Eggert
     [not found]     ` <bnR0A-4kq-1@gated-at.bofh.it>
2008-10-17  8:20       ` Bodo Eggert
2008-10-17 12:17         ` Hugh Dickins
2008-10-17 12:36           ` David Newall
2008-10-17 22:42           ` Bodo Eggert
2008-10-18  0:31           ` Peter Cordes
2008-10-18  5:18             ` Willy Tarreau
2008-10-18 20:45               ` Hugh Dickins [this message]
2008-10-18 20:49                 ` Christoph Hellwig
2008-10-18 20:56                   ` Willy Tarreau
2008-11-14  2:37                     ` [PATCH 2.6.28?] don't unlink an active swapfile Hugh Dickins
2008-11-14  4:08                       ` Peter Cordes
2008-11-14 17:34                       ` Christoph Hellwig
2008-11-14 18:02                         ` Hugh Dickins
2008-10-17  8:20       ` no way to swapoff a deleted swap file? Bodo Eggert
     [not found] <20081015202141.GX26067@cordes.ca>
2008-10-16  8:28 ` Peter Zijlstra
2008-10-16 22:38   ` Hugh Dickins
2008-10-17  6:28     ` David Newall

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Pine.LNX.4.64.0810182058120.7154@blonde.site \
    --to=hugh@veritas.com \
    --cc=7eggert@gmx.de \
    --cc=davidn@davidnewall.com \
    --cc=hch@infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=peter@cordes.ca \
    --cc=peterz@infradead.org \
    --cc=w@1wt.eu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox