2007-07-05 16:02:19 +00:00
|
|
|
/*
|
|
|
|
* fs.c - posix like file functions
|
|
|
|
*
|
|
|
|
* Copyright (c) 2007 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
|
|
|
|
*
|
|
|
|
* See file CREDITS for list of people who contributed to this
|
|
|
|
* project.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License version 2
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2007-07-05 16:01:35 +00:00
|
|
|
#include <common.h>
|
|
|
|
#include <fs.h>
|
|
|
|
#include <driver.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <malloc.h>
|
|
|
|
#include <linux/stat.h>
|
2012-08-22 10:54:50 +00:00
|
|
|
#include <linux/err.h>
|
2007-07-05 16:01:38 +00:00
|
|
|
#include <fcntl.h>
|
2007-07-05 16:01:44 +00:00
|
|
|
#include <xfuncs.h>
|
2007-07-05 16:01:51 +00:00
|
|
|
#include <init.h>
|
2007-10-04 10:30:32 +00:00
|
|
|
#include <module.h>
|
2011-04-08 08:52:50 +00:00
|
|
|
#include <libbb.h>
|
2012-04-14 22:25:40 +00:00
|
|
|
#include <magicvar.h>
|
|
|
|
#include <environment.h>
|
2012-06-24 11:24:00 +00:00
|
|
|
#include <libgen.h>
|
2007-07-05 16:01:35 +00:00
|
|
|
|
2007-09-27 14:33:35 +00:00
|
|
|
void *read_file(const char *filename, size_t *size)
|
2007-07-05 16:02:09 +00:00
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
struct stat s;
|
|
|
|
void *buf = NULL;
|
|
|
|
|
|
|
|
if (stat(filename, &s))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
buf = xzalloc(s.st_size + 1);
|
|
|
|
|
|
|
|
fd = open(filename, O_RDONLY);
|
|
|
|
if (fd < 0)
|
|
|
|
goto err_out;
|
|
|
|
|
|
|
|
if (read(fd, buf, s.st_size) < s.st_size)
|
|
|
|
goto err_out1;
|
|
|
|
|
|
|
|
close(fd);
|
2007-09-27 14:33:35 +00:00
|
|
|
|
|
|
|
if (size)
|
|
|
|
*size = s.st_size;
|
|
|
|
|
2007-07-05 16:02:09 +00:00
|
|
|
return buf;
|
|
|
|
|
|
|
|
err_out1:
|
|
|
|
close(fd);
|
|
|
|
err_out:
|
|
|
|
free(buf);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2007-10-04 10:30:32 +00:00
|
|
|
EXPORT_SYMBOL(read_file);
|
|
|
|
|
2007-07-05 16:01:35 +00:00
|
|
|
char *mkmodestr(unsigned long mode, char *str)
|
|
|
|
{
|
|
|
|
static const char *l = "xwr";
|
|
|
|
int mask = 1, i;
|
|
|
|
char c;
|
|
|
|
|
|
|
|
switch (mode & S_IFMT) {
|
|
|
|
case S_IFDIR: str[0] = 'd'; break;
|
|
|
|
case S_IFBLK: str[0] = 'b'; break;
|
|
|
|
case S_IFCHR: str[0] = 'c'; break;
|
|
|
|
case S_IFIFO: str[0] = 'f'; break;
|
|
|
|
case S_IFLNK: str[0] = 'l'; break;
|
|
|
|
case S_IFSOCK: str[0] = 's'; break;
|
|
|
|
case S_IFREG: str[0] = '-'; break;
|
|
|
|
default: str[0] = '?';
|
|
|
|
}
|
|
|
|
|
|
|
|
for(i = 0; i < 9; i++) {
|
|
|
|
c = l[i%3];
|
|
|
|
str[9-i] = (mode & mask)?c:'-';
|
|
|
|
mask = mask<<1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(mode & S_ISUID) str[3] = (mode & S_IXUSR)?'s':'S';
|
|
|
|
if(mode & S_ISGID) str[6] = (mode & S_IXGRP)?'s':'S';
|
|
|
|
if(mode & S_ISVTX) str[9] = (mode & S_IXOTH)?'t':'T';
|
|
|
|
str[10] = '\0';
|
|
|
|
return str;
|
|
|
|
}
|
2007-10-07 12:27:24 +00:00
|
|
|
EXPORT_SYMBOL(mkmodestr);
|
2007-07-05 16:01:35 +00:00
|
|
|
|
2007-07-05 16:01:51 +00:00
|
|
|
static char *cwd;
|
|
|
|
|
|
|
|
static int init_cwd(void)
|
|
|
|
{
|
|
|
|
cwd = xzalloc(PATH_MAX);
|
|
|
|
*cwd = '/';
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-07-05 16:01:55 +00:00
|
|
|
postcore_initcall(init_cwd);
|
2007-07-05 16:01:51 +00:00
|
|
|
|
2012-08-22 10:54:50 +00:00
|
|
|
char *normalise_link(const char *pathname, const char *symlink)
|
|
|
|
{
|
|
|
|
const char *buf = symlink;
|
|
|
|
char *path_free, *path;
|
|
|
|
char *absolute_path;
|
|
|
|
int point = 0;
|
|
|
|
int dir = 1;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
if (symlink[0] == '/')
|
|
|
|
return strdup(symlink);
|
|
|
|
|
|
|
|
while (*buf == '.' || *buf == '/') {
|
|
|
|
if (*buf == '.') {
|
|
|
|
point++;
|
|
|
|
} else if (*buf == '/') {
|
|
|
|
point = 0;
|
|
|
|
dir++;
|
|
|
|
}
|
|
|
|
if (point > 2) {
|
|
|
|
buf -= 2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
buf++;
|
|
|
|
}
|
|
|
|
|
|
|
|
path = path_free = strdup(pathname);
|
|
|
|
if (!path)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
while(dir) {
|
|
|
|
path = dirname(path);
|
|
|
|
dir--;
|
|
|
|
}
|
|
|
|
|
|
|
|
len = strlen(buf) + strlen(path) + 1;
|
|
|
|
if (buf[0] != '/')
|
|
|
|
len++;
|
|
|
|
|
|
|
|
absolute_path = calloc(sizeof(char), len);
|
|
|
|
|
|
|
|
if (!absolute_path)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
strcat(absolute_path, path);
|
|
|
|
if (buf[0] != '/')
|
|
|
|
strcat(absolute_path, "/");
|
|
|
|
strcat(absolute_path, buf);
|
|
|
|
|
|
|
|
out:
|
|
|
|
free(path_free);
|
|
|
|
|
|
|
|
return absolute_path;
|
|
|
|
}
|
|
|
|
|
2007-07-05 16:01:51 +00:00
|
|
|
char *normalise_path(const char *pathname)
|
2007-07-05 16:01:38 +00:00
|
|
|
{
|
2007-07-05 16:01:51 +00:00
|
|
|
char *path = xzalloc(strlen(pathname) + strlen(cwd) + 2);
|
|
|
|
char *in, *out, *slashes[32];
|
|
|
|
int sl = 0;
|
|
|
|
|
2007-07-05 16:02:12 +00:00
|
|
|
debug("in: %s\n", pathname);
|
|
|
|
|
2007-07-05 16:01:51 +00:00
|
|
|
if (*pathname != '/')
|
|
|
|
strcpy(path, cwd);
|
|
|
|
strcat(path, "/");
|
|
|
|
strcat(path, pathname);
|
|
|
|
|
|
|
|
slashes[0] = in = out = path;
|
2007-07-05 16:01:38 +00:00
|
|
|
|
2007-07-05 16:01:51 +00:00
|
|
|
while (*in) {
|
2007-07-05 16:01:38 +00:00
|
|
|
if(*in == '/') {
|
2007-07-05 16:01:51 +00:00
|
|
|
slashes[sl++] = out;
|
2007-07-05 16:01:38 +00:00
|
|
|
*out++ = *in++;
|
|
|
|
while(*in == '/')
|
|
|
|
in++;
|
|
|
|
} else {
|
2007-07-05 16:01:51 +00:00
|
|
|
if (*in == '.' && (*(in + 1) == '/' || !*(in + 1))) {
|
|
|
|
sl--;
|
|
|
|
if (sl < 0)
|
|
|
|
sl = 0;
|
|
|
|
out = slashes[sl];
|
|
|
|
in++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (*in == '.' && *(in + 1) == '.') {
|
|
|
|
sl -= 2;
|
|
|
|
if (sl < 0)
|
|
|
|
sl = 0;
|
|
|
|
out = slashes[sl];
|
|
|
|
in += 2;
|
|
|
|
continue;
|
|
|
|
}
|
2007-07-05 16:01:38 +00:00
|
|
|
*out++ = *in++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-05 16:01:51 +00:00
|
|
|
*out-- = 0;
|
|
|
|
|
2007-07-05 16:01:38 +00:00
|
|
|
/*
|
2007-07-05 16:01:51 +00:00
|
|
|
* Remove trailing slash
|
2007-07-05 16:01:38 +00:00
|
|
|
*/
|
2007-07-05 16:01:51 +00:00
|
|
|
if (*out == '/')
|
|
|
|
*out = 0;
|
2007-07-05 16:01:38 +00:00
|
|
|
|
2007-07-05 16:01:51 +00:00
|
|
|
if (!*path) {
|
|
|
|
*path = '/';
|
|
|
|
*(path + 1) = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return path;
|
2007-07-05 16:01:38 +00:00
|
|
|
}
|
2007-10-07 12:27:24 +00:00
|
|
|
EXPORT_SYMBOL(normalise_path);
|
2007-07-05 16:01:35 +00:00
|
|
|
|
2012-02-19 17:10:50 +00:00
|
|
|
LIST_HEAD(fs_device_list);
|
|
|
|
static struct fs_device_d *fs_dev_root;
|
2007-07-05 16:01:35 +00:00
|
|
|
|
2012-02-19 17:10:50 +00:00
|
|
|
static struct fs_device_d *get_fsdevice_by_path(const char *path)
|
2007-07-05 16:01:35 +00:00
|
|
|
{
|
2012-02-19 17:10:50 +00:00
|
|
|
struct fs_device_d *fsdev = NULL;
|
2007-07-05 16:01:37 +00:00
|
|
|
|
2012-02-19 17:10:50 +00:00
|
|
|
for_each_fs_device(fsdev) {
|
|
|
|
int len = strlen(fsdev->path);
|
|
|
|
if (!strncmp(path, fsdev->path, len) &&
|
2012-02-11 14:01:16 +00:00
|
|
|
(path[len] == '/' || path[len] == 0))
|
2012-02-19 17:10:50 +00:00
|
|
|
return fsdev;
|
2007-07-05 16:01:37 +00:00
|
|
|
}
|
2007-07-05 16:01:35 +00:00
|
|
|
|
2012-02-19 17:10:50 +00:00
|
|
|
return fs_dev_root;
|
2007-07-05 16:01:44 +00:00
|
|
|
}
|
|
|
|
|
2012-08-17 07:28:15 +00:00
|
|
|
char *get_mounted_path(const char *path)
|
|
|
|
{
|
|
|
|
struct fs_device_d *fdev;
|
|
|
|
|
|
|
|
fdev = get_fsdevice_by_path(path);
|
|
|
|
|
|
|
|
return fdev->path;
|
|
|
|
}
|
|
|
|
|
2007-11-15 22:36:30 +00:00
|
|
|
static FILE files[MAX_FILES];
|
2007-07-05 16:01:38 +00:00
|
|
|
|
2007-11-15 22:36:30 +00:00
|
|
|
static FILE *get_file(void)
|
2007-07-05 16:01:38 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 3; i < MAX_FILES; i++) {
|
2007-07-05 16:01:38 +00:00
|
|
|
if (!files[i].in_use) {
|
2007-07-05 16:01:38 +00:00
|
|
|
memset(&files[i], 0, sizeof(FILE));
|
2007-07-05 16:01:38 +00:00
|
|
|
files[i].in_use = 1;
|
|
|
|
files[i].no = i;
|
2007-07-05 16:01:38 +00:00
|
|
|
return &files[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2007-11-15 22:36:30 +00:00
|
|
|
static void put_file(FILE *f)
|
2007-07-05 16:01:38 +00:00
|
|
|
{
|
2007-07-05 16:01:38 +00:00
|
|
|
files[f->no].in_use = 0;
|
2007-07-05 16:01:38 +00:00
|
|
|
}
|
|
|
|
|
2010-07-28 05:27:50 +00:00
|
|
|
static int check_fd(int fd)
|
|
|
|
{
|
|
|
|
if (fd < 0 || fd >= MAX_FILES || !files[fd].in_use) {
|
2012-05-13 10:43:58 +00:00
|
|
|
errno = EBADF;
|
|
|
|
return -errno;
|
2010-07-28 05:27:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-02-19 17:22:04 +00:00
|
|
|
#ifdef CONFIG_FS_AUTOMOUNT
|
|
|
|
|
|
|
|
#define AUTOMOUNT_IS_FILE (1 << 0)
|
|
|
|
|
|
|
|
struct automount {
|
|
|
|
char *path;
|
|
|
|
char *cmd;
|
|
|
|
struct list_head list;
|
|
|
|
unsigned int flags;
|
|
|
|
};
|
|
|
|
|
|
|
|
static LIST_HEAD(automount_list);
|
|
|
|
|
|
|
|
void automount_remove(const char *_path)
|
|
|
|
{
|
|
|
|
char *path = normalise_path(_path);
|
|
|
|
struct automount *am;
|
|
|
|
|
|
|
|
list_for_each_entry(am, &automount_list, list) {
|
|
|
|
if (!strcmp(path, am->path))
|
|
|
|
goto found;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
found:
|
|
|
|
list_del(&am->list);
|
|
|
|
free(am->path);
|
|
|
|
free(am->cmd);
|
|
|
|
free(am);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(automount_remove);
|
|
|
|
|
|
|
|
int automount_add(const char *path, const char *cmd)
|
|
|
|
{
|
|
|
|
struct automount *am = xzalloc(sizeof(*am));
|
|
|
|
struct stat s;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
am->path = normalise_path(path);
|
|
|
|
am->cmd = xstrdup(cmd);
|
|
|
|
|
2012-04-14 22:30:46 +00:00
|
|
|
automount_remove(am->path);
|
|
|
|
|
2012-02-19 17:22:04 +00:00
|
|
|
ret = stat(path, &s);
|
|
|
|
if (!ret) {
|
|
|
|
/*
|
|
|
|
* If it exists it must be a directory
|
|
|
|
*/
|
|
|
|
if (!S_ISDIR(s.st_mode))
|
|
|
|
return -ENOTDIR;
|
|
|
|
} else {
|
|
|
|
am->flags |= AUTOMOUNT_IS_FILE;
|
|
|
|
}
|
|
|
|
|
|
|
|
list_add_tail(&am->list, &automount_list);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(automount_add);
|
|
|
|
|
|
|
|
void automount_print(void)
|
|
|
|
{
|
|
|
|
struct automount *am;
|
|
|
|
|
|
|
|
list_for_each_entry(am, &automount_list, list)
|
|
|
|
printf("%-20s %s\n", am->path, am->cmd);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(automount_print);
|
|
|
|
|
|
|
|
static void automount_mount(const char *path, int instat)
|
|
|
|
{
|
|
|
|
struct automount *am;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
list_for_each_entry(am, &automount_list, list) {
|
|
|
|
int len_path = strlen(path);
|
|
|
|
int len_am_path = strlen(am->path);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* stat is a bit special. We do not want to trigger
|
|
|
|
* automount when someone calls stat() on the automount
|
|
|
|
* directory itself.
|
|
|
|
*/
|
|
|
|
if (instat && !(am->flags & AUTOMOUNT_IS_FILE) &&
|
|
|
|
len_path == len_am_path) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (len_path < len_am_path)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (strncmp(path, am->path, len_am_path))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (*(path + len_am_path) != 0 && *(path + len_am_path) != '/')
|
|
|
|
continue;
|
|
|
|
|
2012-04-14 22:25:40 +00:00
|
|
|
setenv("automount_path", am->path);
|
|
|
|
export("automount_path");
|
|
|
|
ret = run_command(am->cmd, 0);
|
|
|
|
setenv("automount_path", NULL);
|
2012-02-19 17:22:04 +00:00
|
|
|
|
|
|
|
if (ret)
|
|
|
|
printf("running automount command '%s' failed\n",
|
|
|
|
am->cmd);
|
|
|
|
else
|
|
|
|
automount_remove(am->path);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2012-04-14 22:25:40 +00:00
|
|
|
|
|
|
|
BAREBOX_MAGICVAR(automount_path, "mountpath passed to automount scripts");
|
|
|
|
|
2012-02-19 17:22:04 +00:00
|
|
|
#else
|
|
|
|
static void automount_mount(const char *path, int instat)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_FS_AUTOMOUNT */
|
|
|
|
|
2012-02-19 17:06:48 +00:00
|
|
|
static struct fs_device_d *get_fs_device_and_root_path(char **path)
|
2007-07-05 16:01:38 +00:00
|
|
|
{
|
2012-02-19 17:10:50 +00:00
|
|
|
struct fs_device_d *fsdev;
|
2007-07-05 16:01:38 +00:00
|
|
|
|
2012-02-19 17:22:04 +00:00
|
|
|
automount_mount(*path, 0);
|
|
|
|
|
2012-02-19 17:10:50 +00:00
|
|
|
fsdev = get_fsdevice_by_path(*path);
|
|
|
|
if (!fsdev)
|
2007-07-05 16:01:38 +00:00
|
|
|
return NULL;
|
2012-02-19 17:10:50 +00:00
|
|
|
if (fsdev != fs_dev_root)
|
|
|
|
*path += strlen(fsdev->path);
|
2007-07-05 16:01:44 +00:00
|
|
|
|
2012-02-19 17:10:50 +00:00
|
|
|
return fsdev;
|
2007-07-05 16:01:44 +00:00
|
|
|
}
|
|
|
|
|
2007-11-15 22:36:30 +00:00
|
|
|
static int dir_is_empty(const char *pathname)
|
2007-07-05 16:01:44 +00:00
|
|
|
{
|
2007-07-05 16:01:52 +00:00
|
|
|
DIR *dir;
|
2007-07-05 16:01:44 +00:00
|
|
|
struct dirent *d;
|
|
|
|
int ret = 1;
|
|
|
|
|
|
|
|
dir = opendir(pathname);
|
|
|
|
if (!dir) {
|
2012-05-13 10:43:58 +00:00
|
|
|
errno = ENOENT;
|
2007-07-05 16:01:44 +00:00
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((d = readdir(dir))) {
|
2007-07-05 16:01:52 +00:00
|
|
|
if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
|
2007-07-05 16:01:44 +00:00
|
|
|
continue;
|
|
|
|
ret = 0;
|
|
|
|
break;
|
2007-07-05 16:01:38 +00:00
|
|
|
}
|
|
|
|
|
2007-07-05 16:01:44 +00:00
|
|
|
closedir(dir);
|
|
|
|
return ret;
|
2007-07-05 16:01:38 +00:00
|
|
|
}
|
|
|
|
|
2007-07-05 16:02:12 +00:00
|
|
|
#define S_UB_IS_EMPTY (1 << 31)
|
|
|
|
#define S_UB_EXISTS (1 << 30)
|
|
|
|
#define S_UB_DOES_NOT_EXIST (1 << 29)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Helper function to check the prerequisites of a path given
|
|
|
|
* to fs functions. Besides the flags above S_IFREG and S_IFDIR
|
|
|
|
* can be passed in.
|
|
|
|
*/
|
2007-07-05 16:01:44 +00:00
|
|
|
static int path_check_prereq(const char *path, unsigned int flags)
|
2007-07-05 16:01:38 +00:00
|
|
|
{
|
2007-07-05 16:01:44 +00:00
|
|
|
struct stat s;
|
|
|
|
unsigned int m;
|
2012-05-13 10:43:58 +00:00
|
|
|
int ret = 0;
|
2011-10-18 09:48:44 +00:00
|
|
|
|
2012-08-22 04:54:19 +00:00
|
|
|
if (lstat(path, &s)) {
|
2012-05-13 10:43:58 +00:00
|
|
|
if (flags & S_UB_DOES_NOT_EXIST)
|
2011-10-18 09:48:44 +00:00
|
|
|
goto out;
|
2012-05-13 10:43:58 +00:00
|
|
|
ret = -ENOENT;
|
2007-07-05 16:01:51 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & S_UB_DOES_NOT_EXIST) {
|
2012-05-13 10:43:58 +00:00
|
|
|
ret = -EEXIST;
|
2007-07-05 16:01:51 +00:00
|
|
|
goto out;
|
2007-07-05 16:01:38 +00:00
|
|
|
}
|
|
|
|
|
2012-05-13 10:43:58 +00:00
|
|
|
if (flags == S_UB_EXISTS)
|
2011-10-18 09:48:44 +00:00
|
|
|
goto out;
|
2007-07-05 16:01:38 +00:00
|
|
|
|
2007-07-05 16:01:44 +00:00
|
|
|
m = s.st_mode;
|
|
|
|
|
|
|
|
if (S_ISDIR(m)) {
|
|
|
|
if (flags & S_IFREG) {
|
2012-05-13 10:43:58 +00:00
|
|
|
ret = -EISDIR;
|
2007-07-05 16:01:51 +00:00
|
|
|
goto out;
|
2007-07-05 16:01:44 +00:00
|
|
|
}
|
|
|
|
if ((flags & S_UB_IS_EMPTY) && !dir_is_empty(path)) {
|
2012-05-13 10:43:58 +00:00
|
|
|
ret = -ENOTEMPTY;
|
2007-07-05 16:01:51 +00:00
|
|
|
goto out;
|
2007-07-05 16:01:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if ((flags & S_IFDIR) && S_ISREG(m)) {
|
2012-05-13 10:43:58 +00:00
|
|
|
ret = -ENOTDIR;
|
2007-07-05 16:01:51 +00:00
|
|
|
goto out;
|
2007-07-05 16:01:44 +00:00
|
|
|
}
|
2011-03-16 08:00:36 +00:00
|
|
|
|
2007-07-05 16:01:51 +00:00
|
|
|
out:
|
2012-05-13 10:43:58 +00:00
|
|
|
return ret;
|
2007-07-05 16:01:51 +00:00
|
|
|
}
|
|
|
|
|
2012-06-24 11:24:00 +00:00
|
|
|
static int parent_check_directory(const char *path)
|
|
|
|
{
|
|
|
|
struct stat s;
|
|
|
|
int ret;
|
|
|
|
char *dir = dirname(xstrdup(path));
|
|
|
|
|
2012-08-22 04:54:19 +00:00
|
|
|
ret = lstat(dir, &s);
|
2012-06-24 11:24:00 +00:00
|
|
|
|
|
|
|
free(dir);
|
|
|
|
|
|
|
|
if (ret)
|
|
|
|
return -ENOENT;
|
|
|
|
|
|
|
|
if (!S_ISDIR(s.st_mode))
|
|
|
|
return -ENOTDIR;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-07-05 16:01:51 +00:00
|
|
|
const char *getcwd(void)
|
|
|
|
{
|
|
|
|
return cwd;
|
|
|
|
}
|
2007-10-04 10:30:32 +00:00
|
|
|
EXPORT_SYMBOL(getcwd);
|
2007-07-05 16:01:51 +00:00
|
|
|
|
|
|
|
int chdir(const char *pathname)
|
|
|
|
{
|
|
|
|
char *p = normalise_path(pathname);
|
2012-05-13 10:43:58 +00:00
|
|
|
int ret;
|
|
|
|
|
2007-07-05 16:01:51 +00:00
|
|
|
|
2012-05-13 10:43:58 +00:00
|
|
|
ret = path_check_prereq(p, S_IFDIR);
|
|
|
|
if (ret)
|
2007-07-05 16:01:51 +00:00
|
|
|
goto out;
|
|
|
|
|
|
|
|
strcpy(cwd, p);
|
|
|
|
|
|
|
|
out:
|
2011-10-18 09:48:45 +00:00
|
|
|
free(p);
|
|
|
|
|
2012-05-13 10:43:58 +00:00
|
|
|
if (ret)
|
|
|
|
errno = -ret;
|
|
|
|
|
|
|
|
return ret;
|
2007-07-05 16:01:44 +00:00
|
|
|
}
|
2007-10-04 10:30:32 +00:00
|
|
|
EXPORT_SYMBOL(chdir);
|
2007-07-05 16:01:44 +00:00
|
|
|
|
|
|
|
int unlink(const char *pathname)
|
|
|
|
{
|
2012-02-19 17:06:48 +00:00
|
|
|
struct fs_device_d *fsdev;
|
2007-07-05 16:01:44 +00:00
|
|
|
struct fs_driver_d *fsdrv;
|
2007-07-05 16:01:51 +00:00
|
|
|
char *p = normalise_path(pathname);
|
2007-07-05 16:01:44 +00:00
|
|
|
char *freep = p;
|
2012-05-13 10:43:58 +00:00
|
|
|
int ret;
|
2007-07-05 16:01:38 +00:00
|
|
|
|
2012-05-13 10:43:58 +00:00
|
|
|
ret = path_check_prereq(pathname, S_IFREG);
|
|
|
|
if (ret) {
|
|
|
|
ret = -EINVAL;
|
2007-07-05 16:01:51 +00:00
|
|
|
goto out;
|
2012-05-13 10:43:58 +00:00
|
|
|
}
|
2007-07-05 16:01:38 +00:00
|
|
|
|
2012-02-19 17:06:48 +00:00
|
|
|
fsdev = get_fs_device_and_root_path(&p);
|
2012-05-13 10:43:58 +00:00
|
|
|
if (!fsdev) {
|
|
|
|
ret = -ENOENT;
|
2007-07-05 16:01:44 +00:00
|
|
|
goto out;
|
2012-05-13 10:43:58 +00:00
|
|
|
}
|
2012-02-19 17:06:48 +00:00
|
|
|
fsdrv = fsdev->driver;
|
2007-07-05 16:01:44 +00:00
|
|
|
|
2008-03-01 20:05:23 +00:00
|
|
|
if (!fsdrv->unlink) {
|
2012-05-13 10:43:58 +00:00
|
|
|
ret = -ENOSYS;
|
2008-03-01 20:05:23 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2012-05-13 10:43:58 +00:00
|
|
|
ret = fsdrv->unlink(&fsdev->dev, p);
|
|
|
|
if (ret)
|
|
|
|
errno = -ret;
|
2007-07-05 16:01:44 +00:00
|
|
|
out:
|
|
|
|
free(freep);
|
2012-05-13 10:43:58 +00:00
|
|
|
if (ret)
|
|
|
|
errno = -ret;
|
|
|
|
return ret;
|
2007-07-05 16:01:38 +00:00
|
|
|
}
|
2007-10-04 10:30:32 +00:00
|
|
|
EXPORT_SYMBOL(unlink);
|
2007-07-05 16:01:38 +00:00
|
|
|
|
2012-08-22 10:54:50 +00:00
|
|
|
static char *realfile(const char *pathname, struct stat *s)
|
|
|
|
{
|
|
|
|
char *path = normalise_path(pathname);
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = lstat(path, s);
|
|
|
|
if (ret)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
if (S_ISLNK(s->st_mode)) {
|
|
|
|
char tmp[PATH_MAX];
|
|
|
|
char *new_path;
|
|
|
|
|
|
|
|
memset(tmp, 0, PATH_MAX);
|
|
|
|
|
|
|
|
ret = readlink(path, tmp, PATH_MAX - 1);
|
|
|
|
if (ret < 0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
new_path = normalise_link(path, tmp);
|
|
|
|
free(path);
|
|
|
|
if (!new_path)
|
|
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
path = new_path;
|
|
|
|
|
|
|
|
ret = lstat(path, s);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ret)
|
|
|
|
return path;
|
|
|
|
|
|
|
|
out:
|
|
|
|
free(path);
|
|
|
|
return ERR_PTR(ret);
|
|
|
|
}
|
|
|
|
|
2007-09-24 15:03:20 +00:00
|
|
|
int open(const char *pathname, int flags, ...)
|
2007-07-05 16:01:38 +00:00
|
|
|
{
|
2012-02-19 17:06:48 +00:00
|
|
|
struct fs_device_d *fsdev;
|
2007-07-05 16:01:38 +00:00
|
|
|
struct fs_driver_d *fsdrv;
|
|
|
|
FILE *f;
|
2012-08-21 10:12:34 +00:00
|
|
|
int exist_err = 0;
|
2007-07-05 16:01:38 +00:00
|
|
|
struct stat s;
|
2012-08-21 10:12:34 +00:00
|
|
|
char *path;
|
|
|
|
char *freep;
|
2012-05-13 10:43:58 +00:00
|
|
|
int ret;
|
2007-07-05 16:01:38 +00:00
|
|
|
|
2012-08-21 10:12:34 +00:00
|
|
|
path = realfile(pathname, &s);
|
|
|
|
|
|
|
|
if (IS_ERR(path)) {
|
|
|
|
exist_err = PTR_ERR(path);
|
|
|
|
path = normalise_path(pathname);
|
|
|
|
}
|
|
|
|
|
|
|
|
freep = path;
|
2007-07-05 16:01:38 +00:00
|
|
|
|
2012-02-27 08:30:11 +00:00
|
|
|
if (!exist_err && S_ISDIR(s.st_mode)) {
|
2012-05-13 10:43:58 +00:00
|
|
|
ret = -EISDIR;
|
2007-07-05 16:01:51 +00:00
|
|
|
goto out1;
|
2007-07-05 16:01:44 +00:00
|
|
|
}
|
2007-07-05 16:01:38 +00:00
|
|
|
|
2012-02-27 08:30:11 +00:00
|
|
|
if (exist_err && !(flags & O_CREAT)) {
|
2012-05-13 10:43:58 +00:00
|
|
|
ret = exist_err;
|
2007-07-05 16:01:51 +00:00
|
|
|
goto out1;
|
2007-07-05 16:01:38 +00:00
|
|
|
}
|
2007-07-05 16:01:38 +00:00
|
|
|
|
2012-06-24 11:24:00 +00:00
|
|
|
if (exist_err) {
|
|
|
|
ret = parent_check_directory(path);
|
|
|
|
if (ret)
|
|
|
|
goto out1;
|
|
|
|
}
|
|
|
|
|
2007-07-05 16:01:44 +00:00
|
|
|
f = get_file();
|
|
|
|
if (!f) {
|
2012-05-13 10:43:58 +00:00
|
|
|
ret = -EMFILE;
|
2007-07-05 16:01:51 +00:00
|
|
|
goto out1;
|
2007-07-05 16:01:44 +00:00
|
|
|
}
|
2007-07-05 16:01:38 +00:00
|
|
|
|
2012-02-19 17:06:48 +00:00
|
|
|
fsdev = get_fs_device_and_root_path(&path);
|
2012-05-13 10:43:58 +00:00
|
|
|
if (!fsdev) {
|
|
|
|
ret = -ENOENT;
|
2007-07-05 16:01:44 +00:00
|
|
|
goto out;
|
2012-05-13 10:43:58 +00:00
|
|
|
}
|
2007-07-05 16:01:38 +00:00
|
|
|
|
2012-02-19 17:06:48 +00:00
|
|
|
fsdrv = fsdev->driver;
|
2007-07-05 16:01:44 +00:00
|
|
|
|
2012-02-19 17:06:48 +00:00
|
|
|
f->dev = &fsdev->dev;
|
2007-07-05 16:01:40 +00:00
|
|
|
f->flags = flags;
|
2007-07-05 16:01:38 +00:00
|
|
|
|
|
|
|
if ((flags & O_ACCMODE) && !fsdrv->write) {
|
2012-05-13 10:43:58 +00:00
|
|
|
ret = -EROFS;
|
2007-07-05 16:01:38 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2012-02-27 08:30:11 +00:00
|
|
|
if (exist_err) {
|
2008-02-15 19:26:45 +00:00
|
|
|
if (NULL != fsdrv->create)
|
2012-05-13 10:43:58 +00:00
|
|
|
ret = fsdrv->create(&fsdev->dev, path,
|
2008-02-15 19:26:45 +00:00
|
|
|
S_IFREG | S_IRWXU | S_IRWXG | S_IRWXO);
|
|
|
|
else
|
2012-05-13 10:43:58 +00:00
|
|
|
ret = -EROFS;
|
|
|
|
if (ret)
|
2007-07-05 16:01:38 +00:00
|
|
|
goto out;
|
|
|
|
}
|
2012-05-13 10:43:58 +00:00
|
|
|
ret = fsdrv->open(&fsdev->dev, f, path);
|
|
|
|
if (ret)
|
2007-07-05 16:01:38 +00:00
|
|
|
goto out;
|
|
|
|
|
2007-07-05 16:01:47 +00:00
|
|
|
|
2007-07-05 16:01:38 +00:00
|
|
|
if (flags & O_TRUNC) {
|
2012-05-13 10:43:58 +00:00
|
|
|
ret = fsdrv->truncate(&fsdev->dev, f, 0);
|
2007-07-05 16:01:48 +00:00
|
|
|
f->size = 0;
|
2012-05-13 10:43:58 +00:00
|
|
|
if (ret)
|
2007-07-05 16:01:38 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2007-07-05 16:01:44 +00:00
|
|
|
if (flags & O_APPEND)
|
|
|
|
f->pos = f->size;
|
|
|
|
|
2007-07-05 16:01:47 +00:00
|
|
|
free(freep);
|
2007-07-05 16:01:38 +00:00
|
|
|
return f->no;
|
|
|
|
|
|
|
|
out:
|
|
|
|
put_file(f);
|
2007-07-05 16:01:51 +00:00
|
|
|
out1:
|
2007-07-05 16:01:47 +00:00
|
|
|
free(freep);
|
2012-05-13 10:43:58 +00:00
|
|
|
if (ret)
|
|
|
|
errno = -ret;
|
|
|
|
return ret;
|
2007-07-05 16:01:38 +00:00
|
|
|
}
|
2007-10-04 10:30:32 +00:00
|
|
|
EXPORT_SYMBOL(open);
|
2007-07-05 16:01:38 +00:00
|
|
|
|
2007-07-05 16:01:44 +00:00
|
|
|
int creat(const char *pathname, mode_t mode)
|
|
|
|
{
|
|
|
|
return open(pathname, O_CREAT | O_WRONLY | O_TRUNC);
|
|
|
|
}
|
2007-10-04 10:30:32 +00:00
|
|
|
EXPORT_SYMBOL(creat);
|
2007-07-05 16:01:44 +00:00
|
|
|
|
2008-06-06 07:25:13 +00:00
|
|
|
int ioctl(int fd, int request, void *buf)
|
|
|
|
{
|
|
|
|
struct device_d *dev;
|
|
|
|
struct fs_driver_d *fsdrv;
|
2012-10-07 22:01:22 +00:00
|
|
|
FILE *f;
|
2012-05-13 10:43:58 +00:00
|
|
|
int ret;
|
2008-06-06 07:25:13 +00:00
|
|
|
|
2010-07-28 05:27:50 +00:00
|
|
|
if (check_fd(fd))
|
2012-05-13 10:43:58 +00:00
|
|
|
return -errno;
|
2010-07-28 05:27:50 +00:00
|
|
|
|
2012-10-07 22:01:22 +00:00
|
|
|
f = &files[fd];
|
2008-06-06 07:25:13 +00:00
|
|
|
dev = f->dev;
|
|
|
|
|
2012-02-19 15:39:53 +00:00
|
|
|
fsdrv = dev_to_fs_driver(dev);
|
2008-06-06 07:25:13 +00:00
|
|
|
|
|
|
|
if (fsdrv->ioctl)
|
2012-05-13 10:43:58 +00:00
|
|
|
ret = fsdrv->ioctl(dev, f, request, buf);
|
2008-06-25 08:23:15 +00:00
|
|
|
else
|
2012-05-13 10:43:58 +00:00
|
|
|
ret = -ENOSYS;
|
|
|
|
if (ret)
|
|
|
|
errno = -ret;
|
|
|
|
return ret;
|
2008-06-06 07:25:13 +00:00
|
|
|
}
|
|
|
|
|
2007-07-05 16:01:38 +00:00
|
|
|
int read(int fd, void *buf, size_t count)
|
|
|
|
{
|
|
|
|
struct device_d *dev;
|
|
|
|
struct fs_driver_d *fsdrv;
|
2012-10-07 22:01:22 +00:00
|
|
|
FILE *f;
|
2012-05-13 10:43:58 +00:00
|
|
|
int ret;
|
2007-07-05 16:01:38 +00:00
|
|
|
|
2010-07-28 05:27:50 +00:00
|
|
|
if (check_fd(fd))
|
2012-05-13 10:43:58 +00:00
|
|
|
return -errno;
|
2010-07-28 05:27:50 +00:00
|
|
|
|
2012-10-07 22:01:22 +00:00
|
|
|
f = &files[fd];
|
2007-07-05 16:01:38 +00:00
|
|
|
dev = f->dev;
|
2007-07-05 16:01:40 +00:00
|
|
|
|
2012-02-19 15:39:53 +00:00
|
|
|
fsdrv = dev_to_fs_driver(dev);
|
2007-07-05 16:02:12 +00:00
|
|
|
|
2012-07-03 08:12:40 +00:00
|
|
|
if (f->size != FILE_SIZE_STREAM && f->pos + count > f->size)
|
2007-07-05 16:01:40 +00:00
|
|
|
count = f->size - f->pos;
|
2011-11-08 12:19:22 +00:00
|
|
|
|
|
|
|
if (!count)
|
|
|
|
return 0;
|
|
|
|
|
2012-05-13 10:43:58 +00:00
|
|
|
ret = fsdrv->read(dev, f, buf, count);
|
2007-07-05 16:01:40 +00:00
|
|
|
|
2012-05-13 10:43:58 +00:00
|
|
|
if (ret > 0)
|
|
|
|
f->pos += ret;
|
|
|
|
if (ret < 0)
|
|
|
|
errno = -ret;
|
|
|
|
return ret;
|
2007-07-05 16:01:38 +00:00
|
|
|
}
|
2007-10-04 10:30:32 +00:00
|
|
|
EXPORT_SYMBOL(read);
|
2007-07-05 16:01:38 +00:00
|
|
|
|
|
|
|
ssize_t write(int fd, const void *buf, size_t count)
|
|
|
|
{
|
2007-07-05 16:01:38 +00:00
|
|
|
struct device_d *dev;
|
|
|
|
struct fs_driver_d *fsdrv;
|
2012-10-07 22:01:22 +00:00
|
|
|
FILE *f;
|
2012-05-13 10:43:58 +00:00
|
|
|
int ret;
|
2007-07-05 16:01:38 +00:00
|
|
|
|
2010-07-28 05:27:50 +00:00
|
|
|
if (check_fd(fd))
|
2012-05-13 10:43:58 +00:00
|
|
|
return -errno;
|
2010-07-28 05:27:50 +00:00
|
|
|
|
2012-10-07 22:01:22 +00:00
|
|
|
f = &files[fd];
|
2007-07-05 16:01:38 +00:00
|
|
|
dev = f->dev;
|
2007-07-05 16:02:12 +00:00
|
|
|
|
2012-02-19 15:39:53 +00:00
|
|
|
fsdrv = dev_to_fs_driver(dev);
|
2012-07-03 08:12:40 +00:00
|
|
|
if (f->size != FILE_SIZE_STREAM && f->pos + count > f->size) {
|
2012-05-13 10:43:58 +00:00
|
|
|
ret = fsdrv->truncate(dev, f, f->pos + count);
|
|
|
|
if (ret) {
|
|
|
|
if (ret != -ENOSPC)
|
|
|
|
goto out;
|
2009-07-30 14:10:20 +00:00
|
|
|
count = f->size - f->pos;
|
|
|
|
if (!count)
|
2012-05-13 10:43:58 +00:00
|
|
|
goto out;
|
2009-07-30 14:10:20 +00:00
|
|
|
} else {
|
|
|
|
f->size = f->pos + count;
|
|
|
|
}
|
2007-07-05 16:01:38 +00:00
|
|
|
}
|
2012-05-13 10:43:58 +00:00
|
|
|
ret = fsdrv->write(dev, f, buf, count);
|
|
|
|
if (ret > 0)
|
|
|
|
f->pos += ret;
|
|
|
|
out:
|
|
|
|
if (ret < 0)
|
|
|
|
errno = -ret;
|
|
|
|
return ret;
|
2007-07-05 16:01:38 +00:00
|
|
|
}
|
2007-10-04 10:30:32 +00:00
|
|
|
EXPORT_SYMBOL(write);
|
2007-07-05 16:01:38 +00:00
|
|
|
|
2011-03-25 10:04:53 +00:00
|
|
|
int flush(int fd)
|
|
|
|
{
|
|
|
|
struct device_d *dev;
|
|
|
|
struct fs_driver_d *fsdrv;
|
2012-10-07 22:01:22 +00:00
|
|
|
FILE *f;
|
2012-05-13 10:43:58 +00:00
|
|
|
int ret;
|
2011-03-25 10:04:53 +00:00
|
|
|
|
|
|
|
if (check_fd(fd))
|
2012-05-13 10:43:58 +00:00
|
|
|
return -errno;
|
2011-03-25 10:04:53 +00:00
|
|
|
|
2012-10-07 22:01:22 +00:00
|
|
|
f = &files[fd];
|
2011-03-25 10:04:53 +00:00
|
|
|
dev = f->dev;
|
|
|
|
|
2012-02-19 15:39:53 +00:00
|
|
|
fsdrv = dev_to_fs_driver(dev);
|
2011-03-25 10:04:53 +00:00
|
|
|
if (fsdrv->flush)
|
2012-05-13 10:43:58 +00:00
|
|
|
ret = fsdrv->flush(dev, f);
|
2011-03-25 10:04:53 +00:00
|
|
|
else
|
2012-05-13 10:43:58 +00:00
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
if (ret)
|
|
|
|
errno = -ret;
|
2011-03-25 10:04:53 +00:00
|
|
|
|
2012-05-13 10:43:58 +00:00
|
|
|
return ret;
|
2011-03-25 10:04:53 +00:00
|
|
|
}
|
|
|
|
|
2011-10-14 11:46:09 +00:00
|
|
|
loff_t lseek(int fildes, loff_t offset, int whence)
|
2007-07-05 16:01:40 +00:00
|
|
|
{
|
2008-06-06 07:25:13 +00:00
|
|
|
struct device_d *dev;
|
|
|
|
struct fs_driver_d *fsdrv;
|
2012-10-07 22:01:22 +00:00
|
|
|
FILE *f;
|
2011-10-19 07:27:47 +00:00
|
|
|
loff_t pos;
|
2012-05-13 10:43:58 +00:00
|
|
|
int ret;
|
2008-06-06 07:25:13 +00:00
|
|
|
|
2010-07-28 05:27:50 +00:00
|
|
|
if (check_fd(fildes))
|
|
|
|
return -1;
|
|
|
|
|
2012-10-07 22:01:22 +00:00
|
|
|
f = &files[fildes];
|
2008-06-06 07:25:13 +00:00
|
|
|
dev = f->dev;
|
2012-02-19 15:39:53 +00:00
|
|
|
fsdrv = dev_to_fs_driver(dev);
|
2008-08-01 06:59:27 +00:00
|
|
|
if (!fsdrv->lseek) {
|
2012-05-13 10:43:58 +00:00
|
|
|
ret = -ENOSYS;
|
|
|
|
goto out;
|
2008-08-01 06:59:27 +00:00
|
|
|
}
|
2008-06-06 07:25:13 +00:00
|
|
|
|
2012-05-13 10:43:58 +00:00
|
|
|
ret = -EINVAL;
|
|
|
|
|
|
|
|
switch (whence) {
|
2007-07-05 16:01:40 +00:00
|
|
|
case SEEK_SET:
|
2012-07-03 08:12:40 +00:00
|
|
|
if (f->size != FILE_SIZE_STREAM && offset > f->size)
|
2007-07-05 16:01:40 +00:00
|
|
|
goto out;
|
2008-06-06 07:25:13 +00:00
|
|
|
pos = offset;
|
2007-07-05 16:01:40 +00:00
|
|
|
break;
|
|
|
|
case SEEK_CUR:
|
2012-07-03 08:12:40 +00:00
|
|
|
if (f->size != FILE_SIZE_STREAM && offset + f->pos > f->size)
|
2007-07-05 16:01:40 +00:00
|
|
|
goto out;
|
2008-06-06 07:25:13 +00:00
|
|
|
pos = f->pos + offset;
|
2007-07-05 16:01:40 +00:00
|
|
|
break;
|
|
|
|
case SEEK_END:
|
|
|
|
if (offset)
|
|
|
|
goto out;
|
2008-06-06 07:25:13 +00:00
|
|
|
pos = f->size;
|
2007-07-05 16:01:40 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2012-05-18 09:14:58 +00:00
|
|
|
return fsdrv->lseek(dev, f, pos);
|
2008-06-06 07:25:13 +00:00
|
|
|
|
2007-07-05 16:01:40 +00:00
|
|
|
out:
|
2012-05-13 10:43:58 +00:00
|
|
|
if (ret)
|
|
|
|
errno = -ret;
|
|
|
|
|
2012-05-18 09:14:58 +00:00
|
|
|
return -1;
|
2007-07-05 16:01:40 +00:00
|
|
|
}
|
2007-10-04 10:30:32 +00:00
|
|
|
EXPORT_SYMBOL(lseek);
|
2007-07-05 16:01:40 +00:00
|
|
|
|
2007-07-05 16:01:55 +00:00
|
|
|
int erase(int fd, size_t count, unsigned long offset)
|
|
|
|
{
|
|
|
|
struct device_d *dev;
|
|
|
|
struct fs_driver_d *fsdrv;
|
2012-10-07 22:01:22 +00:00
|
|
|
FILE *f;
|
2012-05-13 10:43:58 +00:00
|
|
|
int ret;
|
2007-07-05 16:01:55 +00:00
|
|
|
|
2010-07-28 05:27:50 +00:00
|
|
|
if (check_fd(fd))
|
2012-05-13 10:43:58 +00:00
|
|
|
return -errno;
|
2012-10-07 22:01:22 +00:00
|
|
|
f = &files[fd];
|
2012-06-06 16:05:00 +00:00
|
|
|
if (offset >= f->size)
|
|
|
|
return 0;
|
|
|
|
if (count > f->size - offset)
|
|
|
|
count = f->size - offset;
|
2010-07-28 05:27:50 +00:00
|
|
|
|
2007-07-05 16:01:55 +00:00
|
|
|
dev = f->dev;
|
2012-02-19 15:39:53 +00:00
|
|
|
fsdrv = dev_to_fs_driver(dev);
|
2007-07-05 16:02:14 +00:00
|
|
|
if (fsdrv->erase)
|
2012-05-13 10:43:58 +00:00
|
|
|
ret = fsdrv->erase(dev, f, count, offset);
|
2007-07-05 16:02:14 +00:00
|
|
|
else
|
2012-05-13 10:43:58 +00:00
|
|
|
ret = -ENOSYS;
|
|
|
|
|
|
|
|
if (ret)
|
|
|
|
errno = -ret;
|
2007-07-16 08:29:28 +00:00
|
|
|
|
2012-05-13 10:43:58 +00:00
|
|
|
return ret;
|
2007-07-16 08:29:28 +00:00
|
|
|
}
|
2007-10-04 10:30:32 +00:00
|
|
|
EXPORT_SYMBOL(erase);
|
2007-07-16 08:29:28 +00:00
|
|
|
|
|
|
|
int protect(int fd, size_t count, unsigned long offset, int prot)
|
|
|
|
{
|
|
|
|
struct device_d *dev;
|
|
|
|
struct fs_driver_d *fsdrv;
|
2012-10-07 22:01:22 +00:00
|
|
|
FILE *f;
|
2012-05-13 10:43:58 +00:00
|
|
|
int ret;
|
2007-07-16 08:29:28 +00:00
|
|
|
|
2010-07-28 05:27:50 +00:00
|
|
|
if (check_fd(fd))
|
2012-05-13 10:43:58 +00:00
|
|
|
return -errno;
|
2012-10-07 22:01:22 +00:00
|
|
|
f = &files[fd];
|
2012-06-06 16:05:00 +00:00
|
|
|
if (offset >= f->size)
|
|
|
|
return 0;
|
|
|
|
if (count > f->size - offset)
|
|
|
|
count = f->size - offset;
|
2010-07-28 05:27:50 +00:00
|
|
|
|
2007-07-16 08:29:28 +00:00
|
|
|
dev = f->dev;
|
2012-02-19 15:39:53 +00:00
|
|
|
fsdrv = dev_to_fs_driver(dev);
|
2007-07-16 08:29:28 +00:00
|
|
|
if (fsdrv->protect)
|
2012-05-13 10:43:58 +00:00
|
|
|
ret = fsdrv->protect(dev, f, count, offset, prot);
|
2007-07-16 08:29:28 +00:00
|
|
|
else
|
2012-05-13 10:43:58 +00:00
|
|
|
ret = -ENOSYS;
|
|
|
|
|
|
|
|
if (ret)
|
|
|
|
errno = -ret;
|
2007-07-05 16:01:55 +00:00
|
|
|
|
2012-05-13 10:43:58 +00:00
|
|
|
return ret;
|
2007-07-05 16:01:55 +00:00
|
|
|
}
|
2007-10-04 10:30:32 +00:00
|
|
|
EXPORT_SYMBOL(protect);
|
2007-07-05 16:01:55 +00:00
|
|
|
|
2009-10-02 13:23:22 +00:00
|
|
|
int protect_file(const char *file, int prot)
|
|
|
|
{
|
|
|
|
int fd, ret;
|
|
|
|
|
|
|
|
fd = open(file, O_WRONLY);
|
|
|
|
if (fd < 0)
|
|
|
|
return fd;
|
|
|
|
|
|
|
|
ret = protect(fd, ~0, 0, prot);
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-07-15 11:50:04 +00:00
|
|
|
void *memmap(int fd, int flags)
|
|
|
|
{
|
|
|
|
struct device_d *dev;
|
|
|
|
struct fs_driver_d *fsdrv;
|
2012-10-07 22:01:22 +00:00
|
|
|
FILE *f;
|
2012-05-13 10:43:58 +00:00
|
|
|
void *retp = (void *)-1;
|
|
|
|
int ret;
|
2007-07-15 11:50:04 +00:00
|
|
|
|
2010-07-28 05:27:50 +00:00
|
|
|
if (check_fd(fd))
|
2012-05-13 10:43:58 +00:00
|
|
|
return retp;
|
2010-07-28 05:27:50 +00:00
|
|
|
|
2012-10-07 22:01:22 +00:00
|
|
|
f = &files[fd];
|
2007-07-15 11:50:04 +00:00
|
|
|
dev = f->dev;
|
|
|
|
|
2012-02-19 15:39:53 +00:00
|
|
|
fsdrv = dev_to_fs_driver(dev);
|
2007-07-15 11:50:04 +00:00
|
|
|
|
|
|
|
if (fsdrv->memmap)
|
2012-05-13 10:43:58 +00:00
|
|
|
ret = fsdrv->memmap(dev, f, &retp, flags);
|
2007-07-15 11:50:04 +00:00
|
|
|
else
|
2012-05-13 10:43:58 +00:00
|
|
|
ret = -EINVAL;
|
2007-07-15 11:50:04 +00:00
|
|
|
|
2012-05-13 10:43:58 +00:00
|
|
|
if (ret)
|
|
|
|
errno = -ret;
|
|
|
|
|
|
|
|
return retp;
|
2007-07-15 11:50:04 +00:00
|
|
|
}
|
2007-10-04 10:30:32 +00:00
|
|
|
EXPORT_SYMBOL(memmap);
|
2007-07-15 11:50:04 +00:00
|
|
|
|
2007-07-05 16:01:38 +00:00
|
|
|
int close(int fd)
|
|
|
|
{
|
|
|
|
struct device_d *dev;
|
|
|
|
struct fs_driver_d *fsdrv;
|
2012-10-07 22:01:22 +00:00
|
|
|
FILE *f;
|
2012-05-13 10:43:58 +00:00
|
|
|
int ret;
|
2007-07-05 16:01:38 +00:00
|
|
|
|
2010-07-28 05:27:50 +00:00
|
|
|
if (check_fd(fd))
|
2012-05-13 10:43:58 +00:00
|
|
|
return -errno;
|
2010-07-28 05:27:50 +00:00
|
|
|
|
2012-10-07 22:01:22 +00:00
|
|
|
f = &files[fd];
|
2007-07-05 16:01:38 +00:00
|
|
|
dev = f->dev;
|
2007-07-05 16:01:38 +00:00
|
|
|
|
2012-02-19 15:39:53 +00:00
|
|
|
fsdrv = dev_to_fs_driver(dev);
|
2012-05-13 10:43:58 +00:00
|
|
|
ret = fsdrv->close(dev, f);
|
2007-07-05 16:01:38 +00:00
|
|
|
|
|
|
|
put_file(f);
|
2012-05-13 10:43:58 +00:00
|
|
|
|
|
|
|
if (ret)
|
|
|
|
errno = -ret;
|
|
|
|
|
|
|
|
return ret;
|
2007-07-05 16:01:38 +00:00
|
|
|
}
|
2007-10-04 10:30:32 +00:00
|
|
|
EXPORT_SYMBOL(close);
|
2007-07-05 16:01:38 +00:00
|
|
|
|
2012-08-16 16:48:35 +00:00
|
|
|
int readlink(const char *pathname, char *buf, size_t bufsiz)
|
|
|
|
{
|
|
|
|
struct fs_driver_d *fsdrv;
|
|
|
|
struct fs_device_d *fsdev;
|
|
|
|
char *p = normalise_path(pathname);
|
|
|
|
char *freep = p;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = path_check_prereq(pathname, S_IFLNK);
|
|
|
|
if (ret)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
fsdev = get_fs_device_and_root_path(&p);
|
|
|
|
if (!fsdev) {
|
|
|
|
ret = -ENODEV;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
fsdrv = fsdev->driver;
|
|
|
|
|
|
|
|
if (fsdrv->readlink)
|
|
|
|
ret = fsdrv->readlink(&fsdev->dev, p, buf, bufsiz);
|
|
|
|
else
|
|
|
|
ret = -ENOSYS;
|
|
|
|
|
|
|
|
if (ret)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
out:
|
|
|
|
free(freep);
|
|
|
|
|
|
|
|
if (ret)
|
|
|
|
errno = -ret;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(readlink);
|
|
|
|
|
2012-08-21 09:21:33 +00:00
|
|
|
int symlink(const char *pathname, const char *newpath)
|
|
|
|
{
|
|
|
|
struct fs_driver_d *fsdrv;
|
|
|
|
struct fs_device_d *fsdev;
|
|
|
|
char *p;
|
|
|
|
char *freep = normalise_path(pathname);
|
|
|
|
int ret;
|
|
|
|
struct stat s;
|
|
|
|
|
|
|
|
if (!freep)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
if (!stat(freep, &s) && S_ISDIR(s.st_mode)) {
|
|
|
|
ret = -ENOSYS;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(freep);
|
|
|
|
freep = p = normalise_path(newpath);
|
|
|
|
|
|
|
|
if (!p)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
ret = lstat(p, &s);
|
|
|
|
if (!ret) {
|
|
|
|
ret = -EEXIST;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
fsdev = get_fs_device_and_root_path(&p);
|
|
|
|
if (!fsdev) {
|
|
|
|
ret = -ENODEV;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
fsdrv = fsdev->driver;
|
|
|
|
|
|
|
|
if (fsdrv->symlink) {
|
|
|
|
ret = fsdrv->symlink(&fsdev->dev, pathname, p);
|
|
|
|
} else {
|
|
|
|
ret = -EPERM;
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
free(freep);
|
|
|
|
if (ret)
|
|
|
|
errno = -ret;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(symlink);
|
|
|
|
|
2012-02-14 22:07:57 +00:00
|
|
|
static int fs_match(struct device_d *dev, struct driver_d *drv)
|
|
|
|
{
|
|
|
|
return strcmp(dev->name, drv->name) ? -1 : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int fs_probe(struct device_d *dev)
|
|
|
|
{
|
2012-02-19 15:43:39 +00:00
|
|
|
struct fs_device_d *fsdev = dev_to_fs_device(dev);
|
2012-02-19 17:10:50 +00:00
|
|
|
struct fs_driver_d *fsdrv = dev_to_fs_driver(dev);
|
2012-02-14 22:07:57 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = dev->driver->probe(dev);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2012-02-19 17:10:50 +00:00
|
|
|
fsdev->driver = fsdrv;
|
2012-02-14 22:07:57 +00:00
|
|
|
|
2012-02-19 17:10:50 +00:00
|
|
|
list_add_tail(&fsdev->list, &fs_device_list);
|
2012-02-14 22:07:57 +00:00
|
|
|
|
2012-02-19 17:10:50 +00:00
|
|
|
if (!fs_dev_root)
|
|
|
|
fs_dev_root = fsdev;
|
2012-02-14 22:07:57 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void fs_remove(struct device_d *dev)
|
|
|
|
{
|
2012-02-19 15:43:39 +00:00
|
|
|
struct fs_device_d *fsdev = dev_to_fs_device(dev);
|
2012-02-14 22:07:57 +00:00
|
|
|
|
|
|
|
if (fsdev->dev.driver) {
|
|
|
|
dev->driver->remove(dev);
|
2012-02-19 17:10:50 +00:00
|
|
|
list_del(&fsdev->list);
|
2012-02-14 22:07:57 +00:00
|
|
|
}
|
|
|
|
|
2012-02-19 17:10:50 +00:00
|
|
|
free(fsdev->path);
|
2012-02-14 22:07:57 +00:00
|
|
|
|
2012-02-19 17:10:50 +00:00
|
|
|
if (fsdev == fs_dev_root)
|
|
|
|
fs_dev_root = NULL;
|
2012-02-14 22:07:57 +00:00
|
|
|
|
|
|
|
free(fsdev->backingstore);
|
|
|
|
free(fsdev);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct bus_type fs_bus = {
|
|
|
|
.name = "fs",
|
|
|
|
.match = fs_match,
|
|
|
|
.probe = fs_probe,
|
|
|
|
.remove = fs_remove,
|
|
|
|
};
|
2009-06-10 19:15:06 +00:00
|
|
|
|
2012-09-20 05:36:44 +00:00
|
|
|
static int fs_bus_init(void)
|
|
|
|
{
|
|
|
|
return bus_register(&fs_bus);
|
|
|
|
}
|
|
|
|
pure_initcall(fs_bus_init);
|
|
|
|
|
2009-06-10 19:15:06 +00:00
|
|
|
int register_fs_driver(struct fs_driver_d *fsdrv)
|
|
|
|
{
|
2012-02-14 22:07:57 +00:00
|
|
|
fsdrv->drv.bus = &fs_bus;
|
2009-06-10 19:15:06 +00:00
|
|
|
register_driver(&fsdrv->drv);
|
2012-02-14 22:07:57 +00:00
|
|
|
|
2009-06-10 19:15:06 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(register_fs_driver);
|
|
|
|
|
2012-08-12 11:28:27 +00:00
|
|
|
static const char *detect_fs(const char *filename)
|
|
|
|
{
|
|
|
|
enum filetype type = file_name_detect_type(filename);
|
|
|
|
struct driver_d *drv;
|
|
|
|
struct fs_driver_d *fdrv;
|
|
|
|
|
|
|
|
if (type == filetype_unknown)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
for_each_driver(drv) {
|
|
|
|
if (drv->bus != &fs_bus)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
fdrv = drv_to_fs_driver(drv);
|
|
|
|
|
|
|
|
if (type == fdrv->type)
|
|
|
|
return drv->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2007-07-05 16:02:12 +00:00
|
|
|
/*
|
|
|
|
* Mount a device to a directory.
|
|
|
|
* We do this by registering a new device on which the filesystem
|
2012-02-19 15:39:53 +00:00
|
|
|
* driver will match.
|
2007-07-05 16:02:12 +00:00
|
|
|
*/
|
2007-10-19 06:45:57 +00:00
|
|
|
int mount(const char *device, const char *fsname, const char *_path)
|
2007-07-05 16:01:35 +00:00
|
|
|
{
|
2007-07-05 16:01:37 +00:00
|
|
|
struct fs_device_d *fsdev;
|
|
|
|
int ret;
|
2007-10-19 06:45:57 +00:00
|
|
|
char *path = normalise_path(_path);
|
2007-07-05 16:01:35 +00:00
|
|
|
|
2007-07-05 16:02:12 +00:00
|
|
|
debug("mount: %s on %s type %s\n", device, path, fsname);
|
|
|
|
|
2012-02-19 17:10:50 +00:00
|
|
|
if (fs_dev_root) {
|
|
|
|
fsdev = get_fsdevice_by_path(path);
|
|
|
|
if (fsdev != fs_dev_root) {
|
2012-02-15 07:48:36 +00:00
|
|
|
printf("sorry, no nested mounts\n");
|
2012-05-13 10:43:58 +00:00
|
|
|
ret = -EBUSY;
|
2012-02-15 07:48:36 +00:00
|
|
|
goto err_free_path;
|
|
|
|
}
|
2012-05-13 10:43:58 +00:00
|
|
|
ret = path_check_prereq(path, S_IFDIR);
|
|
|
|
if (ret)
|
2012-02-14 22:07:57 +00:00
|
|
|
goto err_free_path;
|
2007-07-05 16:01:38 +00:00
|
|
|
} else {
|
|
|
|
/* no mtab, so we only allow to mount on '/' */
|
|
|
|
if (*path != '/' || *(path + 1)) {
|
2012-05-13 10:43:58 +00:00
|
|
|
ret = -ENOTDIR;
|
2012-02-14 22:07:57 +00:00
|
|
|
goto err_free_path;
|
2007-07-05 16:01:38 +00:00
|
|
|
}
|
|
|
|
}
|
2007-07-05 16:01:35 +00:00
|
|
|
|
2012-08-12 11:28:27 +00:00
|
|
|
if (!fsname)
|
|
|
|
fsname = detect_fs(device);
|
|
|
|
|
|
|
|
if (!fsname)
|
|
|
|
return -ENOENT;
|
|
|
|
|
2009-06-11 13:33:49 +00:00
|
|
|
fsdev = xzalloc(sizeof(struct fs_device_d));
|
2012-02-14 20:14:25 +00:00
|
|
|
fsdev->backingstore = xstrdup(device);
|
2011-04-08 08:52:50 +00:00
|
|
|
safe_strncpy(fsdev->dev.name, fsname, MAX_DRIVER_NAME);
|
2010-12-20 08:16:08 +00:00
|
|
|
fsdev->dev.id = get_free_deviceid(fsdev->dev.name);
|
2012-02-19 17:10:50 +00:00
|
|
|
fsdev->path = xstrdup(path);
|
2012-02-14 22:07:57 +00:00
|
|
|
fsdev->dev.bus = &fs_bus;
|
|
|
|
|
|
|
|
if (!strncmp(device, "/dev/", 5))
|
|
|
|
fsdev->cdev = cdev_by_name(device + 5);
|
2007-07-05 16:01:47 +00:00
|
|
|
|
2012-12-12 13:49:02 +00:00
|
|
|
if (fsdev->cdev) {
|
2012-12-12 13:55:40 +00:00
|
|
|
fsdev->dev.parent = fsdev->cdev->dev;
|
2012-12-12 13:49:02 +00:00
|
|
|
fsdev->parent_device = fsdev->cdev->dev;
|
|
|
|
}
|
|
|
|
|
2012-05-13 10:43:58 +00:00
|
|
|
ret = register_device(&fsdev->dev);
|
|
|
|
if (ret)
|
2012-02-14 22:07:57 +00:00
|
|
|
goto err_register;
|
2007-07-05 16:01:47 +00:00
|
|
|
|
|
|
|
if (!fsdev->dev.driver) {
|
2012-02-14 22:07:57 +00:00
|
|
|
/*
|
|
|
|
* Driver didn't accept the device or no driver for this
|
|
|
|
* device. Bail out
|
|
|
|
*/
|
2012-05-13 10:43:58 +00:00
|
|
|
ret = -EINVAL;
|
2012-02-14 22:07:57 +00:00
|
|
|
goto err_no_driver;
|
2007-07-05 16:01:47 +00:00
|
|
|
}
|
|
|
|
|
2011-04-08 10:28:08 +00:00
|
|
|
return 0;
|
|
|
|
|
2012-02-14 22:07:57 +00:00
|
|
|
err_no_driver:
|
2011-04-08 10:28:08 +00:00
|
|
|
unregister_device(&fsdev->dev);
|
2012-02-14 22:07:57 +00:00
|
|
|
err_register:
|
|
|
|
fs_remove(&fsdev->dev);
|
|
|
|
err_free_path:
|
2007-10-19 06:45:57 +00:00
|
|
|
free(path);
|
2012-02-14 22:07:57 +00:00
|
|
|
|
2012-05-13 10:43:58 +00:00
|
|
|
errno = -ret;
|
|
|
|
|
|
|
|
return ret;
|
2007-07-05 16:01:35 +00:00
|
|
|
}
|
2007-10-04 10:30:32 +00:00
|
|
|
EXPORT_SYMBOL(mount);
|
2007-07-05 16:01:35 +00:00
|
|
|
|
2007-07-05 16:01:38 +00:00
|
|
|
int umount(const char *pathname)
|
2007-07-05 16:01:35 +00:00
|
|
|
{
|
2012-02-19 17:10:50 +00:00
|
|
|
struct fs_device_d *fsdev = NULL, *f;
|
2007-07-05 16:01:51 +00:00
|
|
|
char *p = normalise_path(pathname);
|
2007-07-05 16:01:35 +00:00
|
|
|
|
2012-02-19 17:10:50 +00:00
|
|
|
for_each_fs_device(f) {
|
|
|
|
if (!strcmp(p, f->path)) {
|
|
|
|
fsdev = f;
|
2012-02-11 13:29:19 +00:00
|
|
|
break;
|
|
|
|
}
|
2007-07-05 16:01:37 +00:00
|
|
|
}
|
2007-07-05 16:01:35 +00:00
|
|
|
|
2007-07-05 16:01:38 +00:00
|
|
|
free(p);
|
2007-07-05 16:01:35 +00:00
|
|
|
|
2012-02-19 17:10:50 +00:00
|
|
|
if (f == fs_dev_root && !list_is_singular(&fs_device_list)) {
|
2012-05-13 10:43:58 +00:00
|
|
|
errno = EBUSY;
|
|
|
|
return -EBUSY;
|
2012-02-11 13:29:19 +00:00
|
|
|
}
|
|
|
|
|
2012-02-19 17:10:50 +00:00
|
|
|
if (!fsdev) {
|
2012-05-13 10:43:58 +00:00
|
|
|
errno = EFAULT;
|
|
|
|
return -EFAULT;
|
2007-07-05 16:01:37 +00:00
|
|
|
}
|
2007-07-05 16:01:38 +00:00
|
|
|
|
2012-02-19 17:10:50 +00:00
|
|
|
unregister_device(&fsdev->dev);
|
2007-10-19 06:45:57 +00:00
|
|
|
|
2007-07-05 16:01:37 +00:00
|
|
|
return 0;
|
2007-07-05 16:01:35 +00:00
|
|
|
}
|
2007-10-04 10:30:32 +00:00
|
|
|
EXPORT_SYMBOL(umount);
|
2007-07-05 16:01:35 +00:00
|
|
|
|
2007-07-05 16:01:52 +00:00
|
|
|
DIR *opendir(const char *pathname)
|
2007-07-05 16:01:35 +00:00
|
|
|
{
|
2007-07-05 16:01:52 +00:00
|
|
|
DIR *dir = NULL;
|
2012-02-19 17:06:48 +00:00
|
|
|
struct fs_device_d *fsdev;
|
2007-07-05 16:01:37 +00:00
|
|
|
struct fs_driver_d *fsdrv;
|
2007-07-05 16:01:51 +00:00
|
|
|
char *p = normalise_path(pathname);
|
2007-07-05 16:01:40 +00:00
|
|
|
char *freep = p;
|
2012-05-13 10:43:58 +00:00
|
|
|
int ret;
|
2007-07-05 16:01:35 +00:00
|
|
|
|
2012-05-13 10:43:58 +00:00
|
|
|
ret = path_check_prereq(pathname, S_IFDIR);
|
|
|
|
if (ret)
|
2007-07-05 16:01:48 +00:00
|
|
|
goto out;
|
2007-07-05 16:01:40 +00:00
|
|
|
|
2012-02-19 17:06:48 +00:00
|
|
|
fsdev = get_fs_device_and_root_path(&p);
|
2012-05-13 10:43:58 +00:00
|
|
|
if (!fsdev) {
|
|
|
|
ret = -ENOENT;
|
2007-07-05 16:01:40 +00:00
|
|
|
goto out;
|
2012-05-13 10:43:58 +00:00
|
|
|
}
|
2012-02-19 17:06:48 +00:00
|
|
|
fsdrv = fsdev->driver;
|
2007-07-05 16:01:44 +00:00
|
|
|
|
2007-07-05 16:02:12 +00:00
|
|
|
debug("opendir: fsdrv: %p\n",fsdrv);
|
2007-07-05 16:01:37 +00:00
|
|
|
|
2012-02-19 17:06:48 +00:00
|
|
|
dir = fsdrv->opendir(&fsdev->dev, p);
|
2007-07-05 16:01:37 +00:00
|
|
|
if (dir) {
|
2012-02-19 17:06:48 +00:00
|
|
|
dir->dev = &fsdev->dev;
|
2007-07-05 16:01:37 +00:00
|
|
|
dir->fsdrv = fsdrv;
|
2012-05-13 10:43:58 +00:00
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* FIXME: The fs drivers should return ERR_PTR here so that
|
|
|
|
* we are able to forward the error
|
|
|
|
*/
|
|
|
|
ret = -EINVAL;
|
2007-07-05 16:01:35 +00:00
|
|
|
}
|
|
|
|
|
2007-07-05 16:01:40 +00:00
|
|
|
out:
|
|
|
|
free(freep);
|
2012-05-13 10:43:58 +00:00
|
|
|
|
|
|
|
if (ret)
|
|
|
|
errno = -ret;
|
|
|
|
|
2007-07-05 16:01:37 +00:00
|
|
|
return dir;
|
|
|
|
}
|
2007-10-04 10:30:32 +00:00
|
|
|
EXPORT_SYMBOL(opendir);
|
2007-07-05 16:01:37 +00:00
|
|
|
|
2007-07-05 16:01:52 +00:00
|
|
|
struct dirent *readdir(DIR *dir)
|
2007-07-05 16:01:37 +00:00
|
|
|
{
|
2012-05-13 10:43:58 +00:00
|
|
|
struct dirent *ent;
|
|
|
|
|
2012-05-14 19:44:31 +00:00
|
|
|
if (!dir)
|
|
|
|
return NULL;
|
|
|
|
|
2012-05-13 10:43:58 +00:00
|
|
|
ent = dir->fsdrv->readdir(dir->dev, dir);
|
2007-09-25 12:36:55 +00:00
|
|
|
|
2012-05-13 10:43:58 +00:00
|
|
|
if (!ent)
|
|
|
|
errno = EBADF;
|
|
|
|
|
|
|
|
return ent;
|
2007-07-05 16:01:37 +00:00
|
|
|
}
|
2007-10-04 10:30:32 +00:00
|
|
|
EXPORT_SYMBOL(readdir);
|
2007-07-05 16:01:37 +00:00
|
|
|
|
2007-07-05 16:01:52 +00:00
|
|
|
int closedir(DIR *dir)
|
2007-07-05 16:01:37 +00:00
|
|
|
{
|
2012-05-13 10:43:58 +00:00
|
|
|
int ret;
|
|
|
|
|
2007-09-25 12:36:55 +00:00
|
|
|
if (!dir) {
|
2012-05-13 10:43:58 +00:00
|
|
|
errno = EBADF;
|
|
|
|
return -EBADF;
|
2007-09-25 12:36:55 +00:00
|
|
|
}
|
|
|
|
|
2012-05-13 10:43:58 +00:00
|
|
|
ret = dir->fsdrv->closedir(dir->dev, dir);
|
|
|
|
if (ret)
|
|
|
|
errno = -ret;
|
|
|
|
|
|
|
|
return ret;
|
2007-07-05 16:01:37 +00:00
|
|
|
}
|
2007-10-04 10:30:32 +00:00
|
|
|
EXPORT_SYMBOL(closedir);
|
2007-07-05 16:01:37 +00:00
|
|
|
|
2007-07-05 16:01:38 +00:00
|
|
|
int stat(const char *filename, struct stat *s)
|
2012-08-22 10:54:50 +00:00
|
|
|
{
|
|
|
|
char *f;
|
|
|
|
|
|
|
|
f = realfile(filename, s);
|
|
|
|
if (IS_ERR(f))
|
|
|
|
return PTR_ERR(f);
|
|
|
|
|
|
|
|
free(f);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(stat);
|
|
|
|
|
2012-08-22 04:54:19 +00:00
|
|
|
int lstat(const char *filename, struct stat *s)
|
2007-07-05 16:01:38 +00:00
|
|
|
{
|
|
|
|
struct device_d *dev;
|
|
|
|
struct fs_driver_d *fsdrv;
|
2012-02-19 17:10:50 +00:00
|
|
|
struct fs_device_d *fsdev;
|
2007-07-05 16:01:51 +00:00
|
|
|
char *f = normalise_path(filename);
|
|
|
|
char *freep = f;
|
2012-05-13 10:43:58 +00:00
|
|
|
int ret;
|
2007-07-05 16:01:38 +00:00
|
|
|
|
2012-02-19 17:22:04 +00:00
|
|
|
automount_mount(f, 1);
|
|
|
|
|
2007-07-05 16:01:38 +00:00
|
|
|
memset(s, 0, sizeof(struct stat));
|
|
|
|
|
2012-02-19 17:10:50 +00:00
|
|
|
fsdev = get_fsdevice_by_path(f);
|
|
|
|
if (!fsdev) {
|
2012-05-13 10:43:58 +00:00
|
|
|
ret = -ENOENT;
|
2007-07-05 16:01:38 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2012-02-19 17:10:50 +00:00
|
|
|
if (fsdev != fs_dev_root && strcmp(f, fsdev->path)) {
|
|
|
|
f += strlen(fsdev->path);
|
|
|
|
dev = &fsdev->dev;
|
2007-07-05 16:01:40 +00:00
|
|
|
} else
|
2012-02-19 17:10:50 +00:00
|
|
|
dev = &fs_dev_root->dev;
|
2007-07-05 16:01:38 +00:00
|
|
|
|
2012-02-19 15:39:53 +00:00
|
|
|
fsdrv = dev_to_fs_driver(dev);
|
2007-07-05 16:01:38 +00:00
|
|
|
|
2007-07-05 16:01:38 +00:00
|
|
|
if (*f == 0)
|
|
|
|
f = "/";
|
|
|
|
|
2012-05-13 10:43:58 +00:00
|
|
|
ret = fsdrv->stat(dev, f, s);
|
2007-07-05 16:01:38 +00:00
|
|
|
out:
|
2007-07-05 16:01:51 +00:00
|
|
|
free(freep);
|
2012-05-13 10:43:58 +00:00
|
|
|
|
|
|
|
if (ret)
|
|
|
|
errno = -ret;
|
|
|
|
|
|
|
|
return ret;
|
2007-07-05 16:01:38 +00:00
|
|
|
}
|
2012-08-22 04:54:19 +00:00
|
|
|
EXPORT_SYMBOL(lstat);
|
2007-07-05 16:01:38 +00:00
|
|
|
|
2007-09-24 15:03:20 +00:00
|
|
|
int mkdir (const char *pathname, mode_t mode)
|
2007-07-05 16:01:35 +00:00
|
|
|
{
|
2007-07-05 16:01:37 +00:00
|
|
|
struct fs_driver_d *fsdrv;
|
2012-02-19 17:06:48 +00:00
|
|
|
struct fs_device_d *fsdev;
|
2007-07-05 16:01:51 +00:00
|
|
|
char *p = normalise_path(pathname);
|
2007-07-05 16:01:44 +00:00
|
|
|
char *freep = p;
|
2012-05-13 10:43:58 +00:00
|
|
|
int ret;
|
2007-07-05 16:01:37 +00:00
|
|
|
|
2012-06-24 11:24:00 +00:00
|
|
|
ret = parent_check_directory(p);
|
|
|
|
if (ret)
|
|
|
|
goto out;
|
|
|
|
|
2012-05-13 10:43:58 +00:00
|
|
|
ret = path_check_prereq(pathname, S_UB_DOES_NOT_EXIST);
|
|
|
|
if (ret)
|
2007-07-05 16:01:51 +00:00
|
|
|
goto out;
|
2007-07-05 16:01:44 +00:00
|
|
|
|
2012-02-19 17:06:48 +00:00
|
|
|
fsdev = get_fs_device_and_root_path(&p);
|
2012-05-13 10:43:58 +00:00
|
|
|
if (!fsdev) {
|
|
|
|
ret = -ENOENT;
|
2007-07-05 16:01:44 +00:00
|
|
|
goto out;
|
2007-07-05 16:01:38 +00:00
|
|
|
}
|
2012-05-13 10:43:58 +00:00
|
|
|
fsdrv = fsdev->driver;
|
2007-07-05 16:01:38 +00:00
|
|
|
|
2012-05-13 10:43:58 +00:00
|
|
|
if (fsdrv->mkdir)
|
|
|
|
ret = fsdrv->mkdir(&fsdev->dev, p);
|
|
|
|
else
|
|
|
|
ret = -EROFS;
|
2007-07-05 16:01:44 +00:00
|
|
|
out:
|
|
|
|
free(freep);
|
2012-05-13 10:43:58 +00:00
|
|
|
|
|
|
|
if (ret)
|
|
|
|
errno = -ret;
|
|
|
|
|
|
|
|
return ret;
|
2007-07-05 16:01:44 +00:00
|
|
|
}
|
2007-10-04 10:30:32 +00:00
|
|
|
EXPORT_SYMBOL(mkdir);
|
2007-07-05 16:01:37 +00:00
|
|
|
|
2007-07-05 16:01:44 +00:00
|
|
|
int rmdir (const char *pathname)
|
|
|
|
{
|
|
|
|
struct fs_driver_d *fsdrv;
|
2012-02-19 17:06:48 +00:00
|
|
|
struct fs_device_d *fsdev;
|
2007-07-05 16:01:51 +00:00
|
|
|
char *p = normalise_path(pathname);
|
2007-07-05 16:01:44 +00:00
|
|
|
char *freep = p;
|
2012-05-13 10:43:58 +00:00
|
|
|
int ret;
|
2007-07-05 16:01:44 +00:00
|
|
|
|
2012-09-04 13:42:19 +00:00
|
|
|
ret = path_check_prereq(pathname, S_IFLNK);
|
|
|
|
if (!ret) {
|
|
|
|
ret = -ENOTDIR;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2012-05-13 10:43:58 +00:00
|
|
|
ret = path_check_prereq(pathname, S_IFDIR | S_UB_IS_EMPTY);
|
|
|
|
if (ret)
|
2007-07-05 16:01:51 +00:00
|
|
|
goto out;
|
2007-07-05 16:01:35 +00:00
|
|
|
|
2012-02-19 17:06:48 +00:00
|
|
|
fsdev = get_fs_device_and_root_path(&p);
|
2012-05-13 10:43:58 +00:00
|
|
|
if (!fsdev) {
|
2012-08-12 11:28:27 +00:00
|
|
|
ret = -ENODEV;
|
2007-07-05 16:01:44 +00:00
|
|
|
goto out;
|
2007-07-05 16:01:40 +00:00
|
|
|
}
|
2012-05-13 10:43:58 +00:00
|
|
|
fsdrv = fsdev->driver;
|
2007-07-05 16:01:37 +00:00
|
|
|
|
2012-05-13 10:43:58 +00:00
|
|
|
if (fsdrv->rmdir)
|
|
|
|
ret = fsdrv->rmdir(&fsdev->dev, p);
|
|
|
|
else
|
|
|
|
ret = -EROFS;
|
2007-07-05 16:01:44 +00:00
|
|
|
out:
|
|
|
|
free(freep);
|
2012-05-13 10:43:58 +00:00
|
|
|
|
|
|
|
if (ret)
|
|
|
|
errno = -ret;
|
|
|
|
|
|
|
|
return ret;
|
2007-07-05 16:01:35 +00:00
|
|
|
}
|
2007-10-04 10:30:32 +00:00
|
|
|
EXPORT_SYMBOL(rmdir);
|
2007-07-05 16:01:35 +00:00
|
|
|
|
2007-07-05 16:02:14 +00:00
|
|
|
static void memcpy_sz(void *_dst, const void *_src, ulong count, ulong rwsize)
|
|
|
|
{
|
|
|
|
ulong dst = (ulong)_dst;
|
|
|
|
ulong src = (ulong)_src;
|
|
|
|
|
|
|
|
/* no rwsize specification given. Do whatever memcpy likes best */
|
|
|
|
if (!rwsize) {
|
|
|
|
memcpy(_dst, _src, count);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
rwsize = rwsize >> O_RWSIZE_SHIFT;
|
|
|
|
|
|
|
|
count /= rwsize;
|
|
|
|
|
|
|
|
while (count-- > 0) {
|
|
|
|
switch (rwsize) {
|
|
|
|
case 1:
|
|
|
|
*((u_char *)dst) = *((u_char *)src);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
*((ushort *)dst) = *((ushort *)src);
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
*((ulong *)dst) = *((ulong *)src);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
dst += rwsize;
|
|
|
|
src += rwsize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-14 11:46:09 +00:00
|
|
|
ssize_t mem_read(struct cdev *cdev, void *buf, size_t count, loff_t offset, ulong flags)
|
2007-07-05 16:02:14 +00:00
|
|
|
{
|
|
|
|
ulong size;
|
2009-04-22 21:39:27 +00:00
|
|
|
struct device_d *dev;
|
|
|
|
|
2011-07-29 07:15:38 +00:00
|
|
|
if (!cdev->dev || cdev->dev->num_resources < 1)
|
2009-04-22 21:39:27 +00:00
|
|
|
return -1;
|
|
|
|
dev = cdev->dev;
|
|
|
|
|
2012-09-11 05:31:31 +00:00
|
|
|
size = min((resource_size_t)count,
|
|
|
|
resource_size(&dev->resource[0]) -
|
|
|
|
(resource_size_t)offset);
|
2011-07-19 08:29:36 +00:00
|
|
|
memcpy_sz(buf, dev_get_mem_region(dev, 0) + offset, size, flags & O_RWSIZE_MASK);
|
2007-07-05 16:02:14 +00:00
|
|
|
return size;
|
|
|
|
}
|
2007-10-04 10:30:32 +00:00
|
|
|
EXPORT_SYMBOL(mem_read);
|
2007-07-05 16:02:14 +00:00
|
|
|
|
2011-10-14 11:46:09 +00:00
|
|
|
ssize_t mem_write(struct cdev *cdev, const void *buf, size_t count, loff_t offset, ulong flags)
|
2007-07-05 16:02:14 +00:00
|
|
|
{
|
|
|
|
ulong size;
|
2009-04-22 21:39:27 +00:00
|
|
|
struct device_d *dev;
|
|
|
|
|
2011-07-29 07:15:38 +00:00
|
|
|
if (!cdev->dev || cdev->dev->num_resources < 1)
|
2009-04-22 21:39:27 +00:00
|
|
|
return -1;
|
|
|
|
dev = cdev->dev;
|
|
|
|
|
2012-09-11 05:31:31 +00:00
|
|
|
size = min((resource_size_t)count,
|
|
|
|
resource_size(&dev->resource[0]) -
|
|
|
|
(resource_size_t)offset);
|
2011-07-19 08:29:36 +00:00
|
|
|
memcpy_sz(dev_get_mem_region(dev, 0) + offset, buf, size, flags & O_RWSIZE_MASK);
|
2007-07-05 16:02:14 +00:00
|
|
|
return size;
|
|
|
|
}
|
2007-10-04 10:30:32 +00:00
|
|
|
EXPORT_SYMBOL(mem_write);
|