9
0
Fork 0

fs: Store mtab entries in list

To make the code a bit easier to read. Also, do not allow
to umount / when something else is mounted.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
This commit is contained in:
Sascha Hauer 2012-02-11 14:29:19 +01:00
parent 80b767748b
commit 8a81eab846
3 changed files with 43 additions and 56 deletions

View File

@ -33,18 +33,15 @@
static int do_mount(struct command *cmdtp, int argc, char *argv[]) static int do_mount(struct command *cmdtp, int argc, char *argv[])
{ {
int ret = 0; int ret = 0;
struct mtab_entry *entry = NULL; struct mtab_entry *entry;
if (argc == 1) { if (argc == 1) {
do { for_each_mtab_entry(entry) {
entry = mtab_next_entry(entry); printf("%s on %s type %s\n",
if (entry) { entry->parent_device ? dev_name(entry->parent_device) : "none",
printf("%s on %s type %s\n", entry->path,
entry->parent_device ? dev_name(entry->parent_device) : "none", entry->dev->name);
entry->path, }
entry->dev->name);
}
} while (entry);
return 0; return 0;
} }

76
fs/fs.c
View File

@ -167,11 +167,12 @@ char *normalise_path(const char *pathname)
} }
EXPORT_SYMBOL(normalise_path); EXPORT_SYMBOL(normalise_path);
static struct mtab_entry *mtab; LIST_HEAD(mtab_list);
static struct mtab_entry *mtab_root;
struct mtab_entry *get_mtab_entry_by_path(const char *_path) struct mtab_entry *get_mtab_entry_by_path(const char *_path)
{ {
struct mtab_entry *match = NULL, *e = mtab; struct mtab_entry *e = NULL;
char *path, *tok; char *path, *tok;
if (*_path != '/') if (*_path != '/')
@ -183,24 +184,15 @@ struct mtab_entry *get_mtab_entry_by_path(const char *_path)
if (tok) if (tok)
*tok = 0; *tok = 0;
while (e) { for_each_mtab_entry(e) {
if (!strcmp(path, e->path)) { if (!strcmp(path, e->path))
match = e; goto found;
break;
}
e = e->next;
} }
e = mtab_root;
found:
free(path); free(path);
return match ? match : mtab; return e;
}
struct mtab_entry *mtab_next_entry(struct mtab_entry *e)
{
if (!e)
return mtab;
return e->next;
} }
const char *fsdev_get_mountpoint(struct fs_device_d *fsdev) const char *fsdev_get_mountpoint(struct fs_device_d *fsdev)
@ -248,7 +240,7 @@ static struct device_d *get_fs_device_by_path(char **path)
e = get_mtab_entry_by_path(*path); e = get_mtab_entry_by_path(*path);
if (!e) if (!e)
return NULL; return NULL;
if (e != mtab) if (e != mtab_root)
*path += strlen(e->path); *path += strlen(e->path);
dev = e->dev; dev = e->dev;
@ -751,11 +743,6 @@ int mount(const char *device, const char *fsname, const char *_path)
debug("mount: %s on %s type %s\n", device, path, fsname); debug("mount: %s on %s type %s\n", device, path, fsname);
if (get_mtab_entry_by_path(path) != mtab) {
errno = -EBUSY;
goto out;
}
if (strchr(path + 1, '/')) { if (strchr(path + 1, '/')) {
printf("mounting allowed on first directory level only\n"); printf("mounting allowed on first directory level only\n");
errno = -EBUSY; errno = -EBUSY;
@ -774,7 +761,7 @@ int mount(const char *device, const char *fsname, const char *_path)
goto out; goto out;
} }
if (mtab) { if (mtab_root) {
if (path_check_prereq(path, S_IFDIR)) if (path_check_prereq(path, S_IFDIR))
goto out; goto out;
} else { } else {
@ -825,16 +812,12 @@ int mount(const char *device, const char *fsname, const char *_path)
safe_strncpy(entry->path, path, PATH_MAX); safe_strncpy(entry->path, path, PATH_MAX);
entry->dev = dev; entry->dev = dev;
entry->parent_device = parent_device; entry->parent_device = parent_device;
entry->next = NULL;
if (!mtab) list_add_tail(&entry->list, &mtab_list);
mtab = entry;
else { if (!mtab_root)
struct mtab_entry *e = mtab; mtab_root = entry;
while (e->next)
e = e->next;
e->next = entry;
}
errno = 0; errno = 0;
free(path); free(path);
@ -854,27 +837,32 @@ EXPORT_SYMBOL(mount);
int umount(const char *pathname) int umount(const char *pathname)
{ {
struct mtab_entry *entry = mtab; struct mtab_entry *entry = NULL, *e;
struct mtab_entry *last = mtab;
char *p = normalise_path(pathname); char *p = normalise_path(pathname);
struct fs_device_d *fsdev; struct fs_device_d *fsdev;
while(entry && strcmp(p, entry->path)) { for_each_mtab_entry(e) {
last = entry; if (!strcmp(p, e->path)) {
entry = entry->next; entry = e;
break;
}
} }
free(p); free(p);
if (e == mtab_root && !list_is_singular(&mtab_list)) {
errno = -EBUSY;
return errno;
}
if (!entry) { if (!entry) {
errno = -EFAULT; errno = -EFAULT;
return errno; return errno;
} }
if (entry == mtab) list_del(&entry->list);
mtab = mtab->next; if (entry == mtab_root)
else mtab_root = NULL;
last->next = entry->next;
unregister_device(entry->dev); unregister_device(entry->dev);
fsdev = entry->dev->type_data; fsdev = entry->dev->type_data;
@ -951,11 +939,11 @@ int stat(const char *filename, struct stat *s)
goto out; goto out;
} }
if (e != mtab && strcmp(f, e->path)) { if (e != mtab_root && strcmp(f, e->path)) {
f += strlen(e->path); f += strlen(e->path);
dev = e->dev; dev = e->dev;
} else } else
dev = mtab->dev; dev = mtab_root->dev;
fsdrv = (struct fs_driver_d *)dev->driver->type_data; fsdrv = (struct fs_driver_d *)dev->driver->type_data;

View File

@ -78,11 +78,14 @@ struct fs_driver_d {
struct mtab_entry { struct mtab_entry {
char path[PATH_MAX]; char path[PATH_MAX];
struct mtab_entry *next;
struct device_d *dev; struct device_d *dev;
struct device_d *parent_device; struct device_d *parent_device;
struct list_head list;
}; };
extern struct list_head mtab_list;
#define for_each_mtab_entry(e) list_for_each_entry(e, &mtab_list, list)
struct fs_device_d { struct fs_device_d {
char *backingstore; /* the device we are associated with */ char *backingstore; /* the device we are associated with */
struct device_d dev; /* our own device */ struct device_d dev; /* our own device */
@ -148,7 +151,6 @@ char *mkmodestr(unsigned long mode, char *str);
* directly in / and of course the root directory itself * directly in / and of course the root directory itself
*/ */
struct mtab_entry *get_mtab_entry_by_path(const char *path); struct mtab_entry *get_mtab_entry_by_path(const char *path);
struct mtab_entry *mtab_next_entry(struct mtab_entry *entry);
const char *fsdev_get_mountpoint(struct fs_device_d *fsdev); const char *fsdev_get_mountpoint(struct fs_device_d *fsdev);
/* /*