Saturday, February 14, 2015

OS -- create file with initial size, fseek, Rman's convert behaviour, Perform Volume Maintenance Tasks in Windows

This post will be about initialized of the files in Linux and Windows operating system..
These kind of theorical topics have been beyond of my scope for a while.
Anyways, I have decided write about creating a file with initial size , when I saw Oracle RMAN 's behaviour yesterday..
It was a Convert Database operation, and I have seen that just after the start of the operation; the files were initialized with definit sizes in the target directory..
Lets say , if we are converting a database with system01.dbf 10gb in size , we ll see a new system01.dbf in the target directory with 10gb size just after the start of our operation..
So rman initializes its target files, and then write to them.
Is it so? Or is it Os that initializes the file immediately? Is there a way to create a file with a initial size?

The following C code clarified my questions;

#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <io.h>
#include <stdio.h>
int main()
{
FILE* file = fopen ("erman", "w+");
fseek (file, 199, SEEK_SET);
fprintf (file, "x");
fclose (file);
return 0;
}

As you see  above, it uses fseek to set the initial size of the created file..
If you compile and run the code; you will see an empty file will be created with 200 bytes in size..
On the other hand; if you open the created file with a proper editor (like vi , or notepad ++) , you will see that the file is not actually empty.
You will see that there are special characters written in the file.. This is because the design.. OS makes those writes, actually formats the blocks in the disks for protecting the consistency in the underlying filesystem..

Also, we have fsutil in windows; which can create empty file with a initial size..

for example; following command can create a file named erman.out with 10000bytes in size.

fsutil file createnew erman.out 10000

Again, when you open this file with a tool like notepad++, you will see that the file is not actually empty..



Okay, lets come back to our topic.
Rman,when converting the database; must have been using some code like the above to set its destination file size.
It creates the file with a definit size ; allocates os Disk space and then formats the file with its own format while making its operations.
Why rman does this? 
The reason can be to guarantee the allocated disk space or it can be performance reasons (allocate once, write many).. Maybe both..

So, why does OS writes(someting like formatting) to a newly created but initally sized empty files?
The answer seems the security.

Imagine, you create an empty file with 1 gb in size and OS gives that file to you without formatting it :) 
You can easily read your allocated blocks to see their current contents.. It is by design .. I mean OS does not actually delete the files when you delete them.. So, you can read the old contents of the blocks that is allocated for your files..

Lets look from the Linux perspective to support this;
When you rm a file in Linux, you delete the filesystem information (you remove the link for inode) ..
Other process might sill have the file open.. 
Even if after all the processes will close the deleted file , you can still recover the data.. This shows that deleted file contents are not actually deleted..

Lets look from the Windows and Sql Server's perspective;

Look at kimberly's blog post;
She talks about the parameter for Sql Server .. The parameter in questiong is Perform Volume Maintenance Tasks.. This parameter works for Sql Server and eliminates the need for initializiation of a newly created file..
it allows file allocation requests to skip zero initialization on creation. As a result, file allocation requests can occur instantly – no matter what the file size.

So , why is it not on by default then?

Kimberly : 
By granting “Perform Volume Maintenance Tasks” to a SQL Server instance, you are giving administrators of the instance the ability to read the encrypted contents of a recently deleted file (ONLY IF the file system decides to use this newly freed space on the creation of a new database – created with instant initialization) with the undocumented DBCC PAGE command.

That 's all for this topic. I hope you will find it useful.

No comments :

Post a Comment