* Help on automatic population of directories
@ 2004-03-05 16:55 Shailabh
2004-03-06 20:51 ` Jan Hudec
2004-03-08 6:14 ` Maneesh Soni
0 siblings, 2 replies; 6+ messages in thread
From: Shailabh @ 2004-03-05 16:55 UTC (permalink / raw)
To: linux-fsdevel
Hi,
I'm writing a ram-based filesystem. I want the filesystem to
automatically create a few files inside every directory that a user creates.
I'm trying to do this within the mkdir dir_inode op of the fs
using code which looks like this:
>>>>>>>>>>>>>>>>>>
/* rcfs_mknod and rcfs_get_inode are replicas of corresponding code from
fs/ramfs/inode.c */
static int
rcfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
{
struct inode * inode = rcfs_get_inode(dir->i_sb, mode, dev);
int error = -ENOSPC, i=0;
if (inode) {
if (dir->i_mode & S_ISGID) {
inode->i_gid = dir->i_gid;
if (S_ISDIR(mode))
inode->i_mode |= S_ISGID;
}
d_instantiate(dentry, inode);
dget(dentry); /* Extra count - pin the dentry in core */
error = 0;
}
return error;
}
static int rcfs_mkdir(struct inode * dir, struct dentry * dentry, int mode)
{
int retval = rcfs_mknod(dir, dentry, mode | S_IFDIR, 0);
/* rcfs_mknod is a complete replica of
fs/ramfs/inode.c:ramfs_mknod */
if (!retval)
dir->i_nlink++;
/* Do something here to create some "magic" files in this
directory before returning to the user */
return retval;
}
<<<<<<<<<<<<<<<<<<<<<<<<<<
Q1: is there sufficient info in the dir/dentry variables for a file to
be created within rcfs_mkdir (at the comment point) ? If so, what would
be the best way to do this ?
Q2: How can one find the vfsmnt given the dentry of a directory that has
just been created ?
I was thinking of using d_path() to lookup the complete path of the
just-created directory and then calling a modified version of sys_mknod
on the resulting string. Probably not the best way of doing this but it
was simpler to understand :-) But I'm having trouble determining the
vfsmnt from the dentry returned from rcfs_mknod - I tried using
current->namespace->root and also current->fs->rootmnt with no success -
dpath returns blank or garbage as the resulting string and not
"/rcfs/hello/" which is what I expect when directory hello is being created.
I'm new to filesystem development and still not completely clear about
the relationships between mount points, dentries etc....Any pointers on
useful functions would be much appreciated !
Thanks,
Shailabh
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Help on automatic population of directories
2004-03-05 16:55 Help on automatic population of directories Shailabh
@ 2004-03-06 20:51 ` Jan Hudec
2004-03-08 20:32 ` Shailabh Nagar
2004-03-08 6:14 ` Maneesh Soni
1 sibling, 1 reply; 6+ messages in thread
From: Jan Hudec @ 2004-03-06 20:51 UTC (permalink / raw)
To: Shailabh; +Cc: linux-fsdevel
[-- Attachment #1: Type: text/plain, Size: 4060 bytes --]
On Fri, Mar 05, 2004 at 11:55:09 -0500, Shailabh wrote:
> Hi,
>
> I'm writing a ram-based filesystem. I want the filesystem to
> automatically create a few files inside every directory that a user creates.
Why do you want to do it?
One reason to ask that is: The user did not create the files. Should he
really need to delete them manualy?
> I'm trying to do this within the mkdir dir_inode op of the fs
> using code which looks like this:
>
> >>>>>>>>>>>>>>>>>>
> /* rcfs_mknod and rcfs_get_inode are replicas of corresponding code from
> fs/ramfs/inode.c */
>
> static int
> rcfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
> {
> struct inode * inode = rcfs_get_inode(dir->i_sb, mode, dev);
> int error = -ENOSPC, i=0;
>
> if (inode) {
> if (dir->i_mode & S_ISGID) {
> inode->i_gid = dir->i_gid;
> if (S_ISDIR(mode))
> inode->i_mode |= S_ISGID;
> }
> d_instantiate(dentry, inode);
> dget(dentry); /* Extra count - pin the dentry in core */
> error = 0;
> }
> return error;
> }
>
> static int rcfs_mkdir(struct inode * dir, struct dentry * dentry, int mode)
> {
> int retval = rcfs_mknod(dir, dentry, mode | S_IFDIR, 0);
> /* rcfs_mknod is a complete replica of
> fs/ramfs/inode.c:ramfs_mknod */
>
> if (!retval)
> dir->i_nlink++;
IIRC empty directory has a link-count 2. Thus you should fix it for
dentry->inode too.
>
> /* Do something here to create some "magic" files in this
> directory before returning to the user */
>
> return retval;
> }
>
> <<<<<<<<<<<<<<<<<<<<<<<<<<
>
> Q1: is there sufficient info in the dir/dentry variables for a file to
> be created within rcfs_mkdir (at the comment point) ? If so, what would
> be the best way to do this ?
You must be _very_ careful to avoid deadlock, but it should be possible
to do almost anything here. (Generaly, locking i_sem must be ordered by
address, but an inode that you didn't publish yet can be locked safely.)
So you have two basic choices:
1) Call ->create method on the newly created inode (or ->mknod if you
are making a special node).
2) Set some additional info on the new inode and pick the tab later in
lookup. ... and readdir.
> Q2: How can one find the vfsmnt given the dentry of a directory that has
> just been created ?
Don't do it unless you must. You shouldn't need it here.
> I was thinking of using d_path() to lookup the complete path of the
> just-created directory and then calling a modified version of sys_mknod
> on the resulting string. Probably not the best way of doing this but it
> was simpler to understand :-)
Oh, HORRORS! That's a very bad way to do it -- remember, that you
already hold the parent directory. So you can simply call it's methods.
... actualy, you should call the do_ or vfs_ subroutines for the
syscalls, which are the main workhorses.
> But I'm having trouble determining the
> vfsmnt from the dentry returned from rcfs_mknod - I tried using
> current->namespace->root and also current->fs->rootmnt with no success -
> dpath returns blank or garbage as the resulting string and not
> "/rcfs/hello/" which is what I expect when directory hello is being created.
You see the garbage when you directly printk it, or when you call
a sys_* function on it? In the second case, mind the copy_(to|from)_user!
> I'm new to filesystem development and still not completely clear about
> the relationships between mount points, dentries etc....Any pointers on
> useful functions would be much appreciated !
It's rather bad unfortunately. There are few files in Documentation
describing locking scheme and very basic semantics of the methods (it's
VERY good to have these handy when programming). Other documentation
tends to be outdated. The source is quite easy to understand, however.
(Hope you heared of http://lxr.linux.no/source)
-------------------------------------------------------------------------------
Jan 'Bulb' Hudec <bulb@ucw.cz>
[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 189 bytes --]
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Help on automatic population of directories
2004-03-05 16:55 Help on automatic population of directories Shailabh
2004-03-06 20:51 ` Jan Hudec
@ 2004-03-08 6:14 ` Maneesh Soni
2004-03-08 20:38 ` Shailabh Nagar
1 sibling, 1 reply; 6+ messages in thread
From: Maneesh Soni @ 2004-03-08 6:14 UTC (permalink / raw)
To: Shailabh; +Cc: linux-fsdevel
On Fri, Mar 05, 2004 at 04:57:49PM +0000, Shailabh wrote:
> Hi,
>
> I'm writing a ram-based filesystem. I want the filesystem to
> automatically create a few files inside every directory that a user creates.
>
>
> I'm trying to do this within the mkdir dir_inode op of the fs
> using code which looks like this:
>
> >>>>>>>>>>>>>>>>>>
> /* rcfs_mknod and rcfs_get_inode are replicas of corresponding code from
> fs/ramfs/inode.c */
>
> static int
> rcfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
> {
> struct inode * inode = rcfs_get_inode(dir->i_sb, mode, dev);
> int error = -ENOSPC, i=0;
>
> if (inode) {
> if (dir->i_mode & S_ISGID) {
> inode->i_gid = dir->i_gid;
> if (S_ISDIR(mode))
> inode->i_mode |= S_ISGID;
> }
> d_instantiate(dentry, inode);
> dget(dentry); /* Extra count - pin the dentry in core */
> error = 0;
> }
> return error;
> }
>
> static int rcfs_mkdir(struct inode * dir, struct dentry * dentry, int mode)
> {
> int retval = rcfs_mknod(dir, dentry, mode | S_IFDIR, 0);
> /* rcfs_mknod is a complete replica of
> fs/ramfs/inode.c:ramfs_mknod */
>
> if (!retval)
> dir->i_nlink++;
>
> /* Do something here to create some "magic" files in this
> directory before returning to the user */
>
> return retval;
> }
>
> <<<<<<<<<<<<<<<<<<<<<<<<<<
>
> Q1: is there sufficient info in the dir/dentry variables for a file to
> be created within rcfs_mkdir (at the comment point) ? If so, what would
> be the best way to do this ?
>
For creating new files, dentry and inode for the new files are needed. So
that can be done as follows where dentry is the dentry for the directory
under which you want to create new files.
down(&dentry->d_inode->i_sem);
new_dentry = rcfs_get_dentry(dentry->d_inode, "New_file");
if (!IS_ERR(new_dentry)) {
retval = rcfs_mknod(dentry->d_inode, new_dentry, mode, 0);
}
up(&dentry->d_inode->i_sem);
Now for rcfs_get_dentry(), have a look at sysfs_get_dentry(). This should
allocate new dentry if it is not already in dcache. Also you have to
define ->lookup() dir_inode operation as simple_lookup().
>
> Q2: How can one find the vfsmnt given the dentry of a directory that has
> just been created ?
>
> I was thinking of using d_path() to lookup the complete path of the
> just-created directory and then calling a modified version of sys_mknod
> on the resulting string. Probably not the best way of doing this but it
> was simpler to understand :-) But I'm having trouble determining the
> vfsmnt from the dentry returned from rcfs_mknod - I tried using
> current->namespace->root and also current->fs->rootmnt with no success -
> dpath returns blank or garbage as the resulting string and not
> "/rcfs/hello/" which is what I expect when directory hello is being created.
>
Why do you need this?.. It seems you want to know the absolute path for the
new created dentry.
Maneesh
--
Maneesh Soni
Linux Technology Center,
IBM Software Lab, Bangalore, India
email: maneesh@in.ibm.com
Phone: 91-80-25044999 Fax: 91-80-25268553
T/L : 9243696
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Help on automatic population of directories
2004-03-06 20:51 ` Jan Hudec
@ 2004-03-08 20:32 ` Shailabh Nagar
2004-03-09 8:22 ` Jan Hudec
0 siblings, 1 reply; 6+ messages in thread
From: Shailabh Nagar @ 2004-03-08 20:32 UTC (permalink / raw)
To: Jan Hudec; +Cc: linux-fsdevel
Jan Hudec wrote:
>On Fri, Mar 05, 2004 at 11:55:09 -0500, Shailabh wrote:
>
>
>>Hi,
>>
>>I'm writing a ram-based filesystem. I want the filesystem to
>>automatically create a few files inside every directory that a user creates.
>>
>>
>
>Why do you want to do it?
>
>One reason to ask that is: The user did not create the files. Should he
>really need to delete them manualy?
>
>
No, I'll need autodelete functionality as well.
>>I'm new to filesystem development and still not completely clear about
>>the relationships between mount points, dentries etc....Any pointers on
>>useful functions would be much appreciated !
>>
>>
>
>It's rather bad unfortunately. There are few files in Documentation
>describing locking scheme and very basic semantics of the methods (it's
>VERY good to have these handy when programming). Other documentation
>tends to be outdated. The source is quite easy to understand, however.
>(Hope you heared of http://lxr.linux.no/source)
>
>
Thanks for the tips. Yes, I've been looking through the sources too.
-- Shailabh
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Help on automatic population of directories
2004-03-08 6:14 ` Maneesh Soni
@ 2004-03-08 20:38 ` Shailabh Nagar
0 siblings, 0 replies; 6+ messages in thread
From: Shailabh Nagar @ 2004-03-08 20:38 UTC (permalink / raw)
To: maneesh; +Cc: linux-fsdevel
Maneesh Soni wrote:
>For creating new files, dentry and inode for the new files are needed. So
>that can be done as follows where dentry is the dentry for the directory
>under which you want to create new files.
>
> down(&dentry->d_inode->i_sem);
> new_dentry = rcfs_get_dentry(dentry->d_inode, "New_file");
> if (!IS_ERR(new_dentry)) {
> retval = rcfs_mknod(dentry->d_inode, new_dentry, mode, 0);
> }
> up(&dentry->d_inode->i_sem);
>
>Now for rcfs_get_dentry(), have a look at sysfs_get_dentry(). This should
>allocate new dentry if it is not already in dcache. Also you have to
>define ->lookup() dir_inode operation as simple_lookup().
>
>
>
Thanks for that simple code snippet which works well (with the mode
value set properly).
This is exactly what I was looking for !
Now onto autodelete.....
>>Q2: How can one find the vfsmnt given the dentry of a directory that has
>>just been created ?
>>
>>I was thinking of using d_path() to lookup the complete path of the
>>just-created directory and then calling a modified version of sys_mknod
>>on the resulting string. Probably not the best way of doing this but it
>>was simpler to understand :-) But I'm having trouble determining the
>>vfsmnt from the dentry returned from rcfs_mknod - I tried using
>>current->namespace->root and also current->fs->rootmnt with no success -
>>dpath returns blank or garbage as the resulting string and not
>>"/rcfs/hello/" which is what I expect when directory hello is being created.
>>
>>
>>
>
>Why do you need this?.. It seems you want to know the absolute path for the
>new created dentry.
>
>
Yes, I was thinking of getting the absolute path and then using a
modified sys_mknod (without the copy/from user
for the pathname) to create the entry. Obviously pretty inefficient as
Jan Hudec pointed out :-(
-- Shailabh
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Help on automatic population of directories
2004-03-08 20:32 ` Shailabh Nagar
@ 2004-03-09 8:22 ` Jan Hudec
0 siblings, 0 replies; 6+ messages in thread
From: Jan Hudec @ 2004-03-09 8:22 UTC (permalink / raw)
To: Shailabh Nagar; +Cc: linux-fsdevel
[-- Attachment #1: Type: text/plain, Size: 1045 bytes --]
On Mon, Mar 08, 2004 at 15:32:11 -0500, Shailabh Nagar wrote:
> Jan Hudec wrote:
>
> >On Fri, Mar 05, 2004 at 11:55:09 -0500, Shailabh wrote:
> >
> >
> >>Hi,
> >>
> >>I'm writing a ram-based filesystem. I want the filesystem to
> >>automatically create a few files inside every directory that a user
> >>creates.
> >>
> >>
> >
> >Why do you want to do it?
> >
> >One reason to ask that is: The user did not create the files. Should he
> >really need to delete them manualy?
> >
> >
> No, I'll need autodelete functionality as well.
Well, actualy looking in the source, it seems it's up to the filesystem
to detect whether the directory is empty. So after you check, that there
are no files but the magic ones (link count should help), you simply
delete by calling vfs_unlink on the magic files and then proceed with
removal (which, for ram-based fs, just means to put the dentry, IIRC).
-------------------------------------------------------------------------------
Jan 'Bulb' Hudec <bulb@ucw.cz>
[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 189 bytes --]
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2004-03-09 8:22 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-03-05 16:55 Help on automatic population of directories Shailabh
2004-03-06 20:51 ` Jan Hudec
2004-03-08 20:32 ` Shailabh Nagar
2004-03-09 8:22 ` Jan Hudec
2004-03-08 6:14 ` Maneesh Soni
2004-03-08 20:38 ` Shailabh Nagar
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).