Skip to contentSkip to conversation

undelete still opened file

DISCUSSION Lars Täuber 3 participants 8 messages Oct 17, 2008 – Oct 20, 2008

lore
Share & tools
lore
#0 lore raw

Hi there!

If this should be the wrong mailing list please excuse me but I didn't find any better suiting. I just subscribed after unsuccessfully searching on the net for an answer to my question.

Is it possible to undelete an accidentally deleted file that is still opened by a process? As far as I know the file is not really deleted as long there is a file descriptor connected to the file. I thought about something in connection with /proc/####/fd/# .
Is there any chance to get the file back to normal?

- Linux 2.6.[12]x
- ext3 or nfs (should be independent of fs, wouldn't it?)

Thanks and best regards.
Lars

PS: Sorry for my english.

#1 lore raw
Quoted 9 lines · Lars Täuber

Hi there!

If this should be the wrong mailing list please excuse me but I didn't find any better suiting. I just subscribed after unsuccessfully searching on the net for an answer to my question.

Is it possible to undelete an accidentally deleted file that is still opened by a process? As far as I know the file is not really deleted as long there is a file descriptor connected to the file. I thought about something in connection with /proc/####/fd/# .
Is there any chance to get the file back to normal?

To some extent yes......

Quoted 3 lines

- Linux 2.6.[12]x
- ext3 or nfs (should be independent of fs, wouldn't it?)

It is tightly coupled with the filesystem. So depending on your filesystem you may or may not get it.

LOGFS (or any log structured file system) : They generally have snapshots or some kind of file versioning. So you can retreive it from there.

ext2 : After deletion ext2 doesn't zero the block pointers. So it is possible to get it from there using debugfs.

ext3 : Search google for "Carlo wood + ext3grep"

ext4 : I don't know of any method ...

You can try the /proc/****/fd/** approach . It has worked for me in past......but that was for a running binary. I am not sure about shellscript kind of thing. There are some other tools too to recover , unfortunately I don't remember the name but google should be good enough.

Thanks -
Manish

Quoted 6 lines

Thanks and best regards.
Lars

Quoted 6 lines

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

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

#2 lore raw

Hi Manish,

Quoted 4 lines · Manish Katiyar
Quoted 2 lines

I thought about something in connection with /proc/####/fd/# . Is there any chance to get the file back to normal?

To some extent yes......

yes I know I could do this:

cat /proc/####/fd/32 > /dir/to/new/file.ext

but then all changes made by the process afterwards are not in the copy.

Quoted 2 lines

You can try the /proc/****/fd/** approach . It has worked for me in past......but that was for a running binary.

Of course there is a process still running and works with a file discriptor to the deleted file.

Onother question:
Is the space of the file on the underlying block device already marked as free? Or does this happen after all processes have closed all file descriptors pointing to the file?

I really want to undo the deletion. (get a link/name connected to the root inode of the file again) Is there a way to do this?

Thanks
Lars

#3 lore raw
Quoted 3 lines · Lars Täuber

Is the space of the file on the underlying block device already marked as free? Or does this happen after all processes have closed all file descriptors pointing to the file?

No, the space on the file is not yet marked as free. *However* for ext3 and ext4, the inode has been placed on the orphaned inode list, so that if the system crashes, part of the journal recovery process will at that point free the blocks.

Quoted 2 lines

I really want to undo the deletion. (get a link/name connected to the root inode of the file again) Is there a way to do this?

Not currently using ext3/ext4, no. There would have to be an entirely new system call or other userspace interface for something like this.

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

#4 lore raw

Hi Theodore,

Quoted 9 lines · Theodore Tso
Quoted 3 lines · Lars Täuber

Is the space of the file on the underlying block device already marked as free? Or does this happen after all processes have closed all file descriptors pointing to the file?

No, the space on the file is not yet marked as free. *However* for ext3 and ext4, the inode has been placed on the orphaned inode list, so that if the system crashes, part of the journal recovery process will at that point free the blocks.

not that it is a way I would go, but theoretically: I could provoke a crash and mount the ext3 filesystem as ext2 and restore the file?

Quoted 5 lines
Quoted 2 lines

I really want to undo the deletion. (get a link/name connected to the root inode of the file again) Is there a way to do this?

Not currently using ext3/ext4, no. There would have to be an entirely new system call or other userspace interface for something like this.

Is this planned?

But a solution should be independent of the real filesystem. Because the file is still somewhere in the ram and represented in linux vfs, isn't it? It only needs to be copied/recreated on to a (different) filesystem somehow.

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

#5 lore raw
Quoted 9 lines · Lars Täuber

Hi Theodore,

Quoted 5 lines · Theodore Tso
Quoted 3 lines · Lars Täuber

Is the space of the file on the underlying block device already marked as free? Or does this happen after all processes have closed all file descriptors pointing to the file?

Quoted 6 lines
Quoted 4 lines

No, the space on the file is not yet marked as free. *However* for ext3 and ext4, the inode has been placed on the orphaned inode list, so that if the system crashes, part of the journal recovery process will at that point free the blocks.

not that it is a way I would go, but theoretically: I could provoke a crash and mount the ext3 filesystem as ext2 and restore the file?

Errr.... If I read the code correctly, once deleted the inode is added to the orphan list and on recovery this orphan list is traversed and iput will be called on each of the inodes. So even a crash will not restore your inode on the contrary *it will make sure that the inode is deleted*.

Quoted 5 lines
Quoted 5 lines
Quoted 3 lines

I really want to undo the deletion. (get a link/name connected to the root inode of the file again) Is there a way to do this?

Not currently using ext3/ext4, no.

Isn't it possible to some extent using ext3grep for ext3 ????

There would have to be an entirely

Quoted 5 lines
Quoted 1 line

new system call or other userspace interface for something like this.

Is this planned?

But a solution should be independent of the real filesystem.

No as I said...depending on the nature of filesystem it may be write in place or it might write to a new block every time like logfs. So it is very much filesystem dependant.

Thanks -
Manish

Quoted 9 lines

Because the file is still somewhere in the ram and represented in linux vfs, isn't it?

It only needs to be copied/recreated on to a (different) filesystem somehow.

Regards
Lars

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

#6 lore raw
In reply to Lars Täuber
Quoted 4 lines · Lars Täuber

not that it is a way I would go, but theoretically: I could provoke a crash and mount the ext3 filesystem as ext2 and restore the file?

Unless you then manually modify the orphan list to remove the inode from the orphan list, the next time you mount the filesystem as ext3 the blocks will be released and the filesystem will be corrupted. Also, if you provoke a crash, the filesystem will be marked as "needs recovery" and ext2 will refuse to mount the filesystem.

The much simpler way would be to kill -STOP the process that owns the file, copy it via "cat /proc/$pid/fd/$fd > rescue-file". and then kill -9 the process if you don't want it to make any further changes.

Quoted 4 lines
Quoted 2 lines

Not currently using ext3/ext4, no. There would have to be an entirely new system call or other userspace interface for something like this.

Is this planned?

No, this is not planned. "rm is forever".

Quoted 4 lines

But a solution should be independent of the real filesystem. Because the file is still somewhere in the ram and represented in linux vfs, isn't it? It only needs to be copied/recreated on to a (different) filesystem somehow.

No, it's not necessarily in RAM. It's on disk, in the filesystem, but marked in such a way that if the system crashes or when the file descriptor is closed, the inode and its contents are marked as freed for allocation.

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

#7 lore raw
Quoted 3 lines · Theodore Tso
Quoted 1 line

Is this planned?

No, this is not planned. "rm is forever".

Ok, Thanks anyway.
Lars