diff options
author | 2023-02-21 18:24:12 -0800 | |
---|---|---|
committer | 2023-02-21 18:24:12 -0800 | |
commit | 5b7c4cabbb65f5c469464da6c5f614cbd7f730f2 (patch) | |
tree | cc5c2d0a898769fd59549594fedb3ee6f84e59a0 /fs/hostfs | |
download | linux-5b7c4cabbb65f5c469464da6c5f614cbd7f730f2.tar.gz linux-5b7c4cabbb65f5c469464da6c5f614cbd7f730f2.zip |
Merge tag 'net-next-6.3' of git://git.kernel.org/pub/scm/linux/kernel/git/netdev/net-nextgrafted
Pull networking updates from Jakub Kicinski:
"Core:
- Add dedicated kmem_cache for typical/small skb->head, avoid having
to access struct page at kfree time, and improve memory use.
- Introduce sysctl to set default RPS configuration for new netdevs.
- Define Netlink protocol specification format which can be used to
describe messages used by each family and auto-generate parsers.
Add tools for generating kernel data structures and uAPI headers.
- Expose all net/core sysctls inside netns.
- Remove 4s sleep in netpoll if carrier is instantly detected on
boot.
- Add configurable limit of MDB entries per port, and port-vlan.
- Continue populating drop reasons throughout the stack.
- Retire a handful of legacy Qdiscs and classifiers.
Protocols:
- Support IPv4 big TCP (TSO frames larger than 64kB).
- Add IP_LOCAL_PORT_RANGE socket option, to control local port range
on socket by socket basis.
- Track and report in procfs number of MPTCP sockets used.
- Support mixing IPv4 and IPv6 flows in the in-kernel MPTCP path
manager.
- IPv6: don't check net.ipv6.route.max_size and rely on garbage
collection to free memory (similarly to IPv4).
- Support Penultimate Segment Pop (PSP) flavor in SRv6 (RFC8986).
- ICMP: add per-rate limit counters.
- Add support for user scanning requests in ieee802154.
- Remove static WEP support.
- Support minimal Wi-Fi 7 Extremely High Throughput (EHT) rate
reporting.
- WiFi 7 EHT channel puncturing support (client & AP).
BPF:
- Add a rbtree data structure following the "next-gen data structure"
precedent set by recently added linked list, that is, by using
kfunc + kptr instead of adding a new BPF map type.
- Expose XDP hints via kfuncs with initial support for RX hash and
timestamp metadata.
- Add BPF_F_NO_TUNNEL_KEY extension to bpf_skb_set_tunnel_key to
better support decap on GRE tunnel devices not operating in collect
metadata.
- Improve x86 JIT's codegen for PROBE_MEM runtime error checks.
- Remove the need for trace_printk_lock for bpf_trace_printk and
bpf_trace_vprintk helpers.
- Extend libbpf's bpf_tracing.h support for tracing arguments of
kprobes/uprobes and syscall as a special case.
- Significantly reduce the search time for module symbols by
livepatch and BPF.
- Enable cpumasks to be used as kptrs, which is useful for tracing
programs tracking which tasks end up running on which CPUs in
different time intervals.
- Add support for BPF trampoline on s390x and riscv64.
- Add capability to export the XDP features supported by the NIC.
- Add __bpf_kfunc tag for marking kernel functions as kfuncs.
- Add cgroup.memory=nobpf kernel parameter option to disable BPF
memory accounting for container environments.
Netfilter:
- Remove the CLUSTERIP target. It has been marked as obsolete for
years, and we still have WARN splats wrt races of the out-of-band
/proc interface installed by this target.
- Add 'destroy' commands to nf_tables. They are identical to the
existing 'delete' commands, but do not return an error if the
referenced object (set, chain, rule...) did not exist.
Driver API:
- Improve cpumask_local_spread() locality to help NICs set the right
IRQ affinity on AMD platforms.
- Separate C22 and C45 MDIO bus transactions more clearly.
- Introduce new DCB table to control DSCP rewrite on egress.
- Support configuration of Physical Layer Collision Avoidance (PLCA)
Reconciliation Sublayer (RS) (802.3cg-2019). Modern version of
shared medium Ethernet.
- Support for MAC Merge layer (IEEE 802.3-2018 clause 99). Allowing
preemption of low priority frames by high priority frames.
- Add support for controlling MACSec offload using netlink SET.
- Rework devlink instance refcounts to allow registration and
de-registration under the instance lock. Split the code into
multiple files, drop some of the unnecessarily granular locks and
factor out common parts of netlink operation handling.
- Add TX frame aggregation parameters (for USB drivers).
- Add a new attr TCA_EXT_WARN_MSG to report TC (offload) warning
messages with notifications for debug.
- Allow offloading of UDP NEW connections via act_ct.
- Add support for per action HW stats in TC.
- Support hardware miss to TC action (continue processing in SW from
a specific point in the action chain).
- Warn if old Wireless Extension user space interface is used with
modern cfg80211/mac80211 drivers. Do not support Wireless
Extensions for Wi-Fi 7 devices at all. Everyone should switch to
using nl80211 interface instead.
- Improve the CAN bit timing configuration. Use extack to return
error messages directly to user space, update the SJW handling,
including the definition of a new default value that will benefit
CAN-FD controllers, by increasing their oscillator tolerance.
New hardware / drivers:
- Ethernet:
- nVidia BlueField-3 support (control traffic driver)
- Ethernet support for imx93 SoCs
- Motorcomm yt8531 gigabit Ethernet PHY
- onsemi NCN26000 10BASE-T1S PHY (with support for PLCA)
- Microchip LAN8841 PHY (incl. cable diagnostics and PTP)
- Amlogic gxl MDIO mux
- WiFi:
- RealTek RTL8188EU (rtl8xxxu)
- Qualcomm Wi-Fi 7 devices (ath12k)
- CAN:
- Renesas R-Car V4H
Drivers:
- Bluetooth:
- Set Per Platform Antenna Gain (PPAG) for Intel controllers.
- Ethernet NICs:
- Intel (1G, igc):
- support TSN / Qbv / packet scheduling features of i226 model
- Intel (100G, ice):
- use GNSS subsystem instead of TTY
- multi-buffer XDP support
- extend support for GPIO pins to E823 devices
- nVidia/Mellanox:
- update the shared buffer configuration on PFC commands
- implement PTP adjphase function for HW offset control
- TC support for Geneve and GRE with VF tunnel offload
- more efficient crypto key management method
- multi-port eswitch support
- Netronome/Corigine:
- add DCB IEEE support
- support IPsec offloading for NFP3800
- Freescale/NXP (enetc):
- support XDP_REDIRECT for XDP non-linear buffers
- improve reconfig, avoid link flap and waiting for idle
- support MAC Merge layer
- Other NICs:
- sfc/ef100: add basic devlink support for ef100
- ionic: rx_push mode operation (writing descriptors via MMIO)
- bnxt: use the auxiliary bus abstraction for RDMA
- r8169: disable ASPM and reset bus in case of tx timeout
- cpsw: support QSGMII mode for J721e CPSW9G
- cpts: support pulse-per-second output
- ngbe: add an mdio bus driver
- usbnet: optimize usbnet_bh() by avoiding unnecessary queuing
- r8152: handle devices with FW with NCM support
- amd-xgbe: support 10Mbps, 2.5GbE speeds and rx-adaptation
- virtio-net: support multi buffer XDP
- virtio/vsock: replace virtio_vsock_pkt with sk_buff
- tsnep: XDP support
- Ethernet high-speed switches:
- nVidia/Mellanox (mlxsw):
- add support for latency TLV (in FW control messages)
- Microchip (sparx5):
- separate explicit and implicit traffic forwarding rules, make
the implicit rules always active
- add support for egress DSCP rewrite
- IS0 VCAP support (Ingress Classification)
- IS2 VCAP filters (protos, L3 addrs, L4 ports, flags, ToS
etc.)
- ES2 VCAP support (Egress Access Control)
- support for Per-Stream Filtering and Policing (802.1Q,
8.6.5.1)
- Ethernet embedded switches:
- Marvell (mv88e6xxx):
- add MAB (port auth) offload support
- enable PTP receive for mv88e6390
- NXP (ocelot):
- support MAC Merge layer
- support for the the vsc7512 internal copper phys
- Microchip:
- lan9303: convert to PHYLINK
- lan966x: support TC flower filter statistics
- lan937x: PTP support for KSZ9563/KSZ8563 and LAN937x
- lan937x: support Credit Based Shaper configuration
- ksz9477: support Energy Efficient Ethernet
- other:
- qca8k: convert to regmap read/write API, use bulk operations
- rswitch: Improve TX timestamp accuracy
- Intel WiFi (iwlwifi):
- EHT (Wi-Fi 7) rate reporting
- STEP equalizer support: transfer some STEP (connection to radio
on platforms with integrated wifi) related parameters from the
BIOS to the firmware.
- Qualcomm 802.11ax WiFi (ath11k):
- IPQ5018 support
- Fine Timing Measurement (FTM) responder role support
- channel 177 support
- MediaTek WiFi (mt76):
- per-PHY LED support
- mt7996: EHT (Wi-Fi 7) support
- Wireless Ethernet Dispatch (WED) reset support
- switch to using page pool allocator
- RealTek WiFi (rtw89):
- support new version of Bluetooth co-existance
- Mobile:
- rmnet: support TX aggregation"
* tag 'net-next-6.3' of git://git.kernel.org/pub/scm/linux/kernel/git/netdev/net-next: (1872 commits)
page_pool: add a comment explaining the fragment counter usage
net: ethtool: fix __ethtool_dev_mm_supported() implementation
ethtool: pse-pd: Fix double word in comments
xsk: add linux/vmalloc.h to xsk.c
sefltests: netdevsim: wait for devlink instance after netns removal
selftest: fib_tests: Always cleanup before exit
net/mlx5e: Align IPsec ASO result memory to be as required by hardware
net/mlx5e: TC, Set CT miss to the specific ct action instance
net/mlx5e: Rename CHAIN_TO_REG to MAPPED_OBJ_TO_REG
net/mlx5: Refactor tc miss handling to a single function
net/mlx5: Kconfig: Make tc offload depend on tc skb extension
net/sched: flower: Support hardware miss to tc action
net/sched: flower: Move filter handle initialization earlier
net/sched: cls_api: Support hardware miss to tc action
net/sched: Rename user cookie and act cookie
sfc: fix builds without CONFIG_RTC_LIB
sfc: clean up some inconsistent indentings
net/mlx4_en: Introduce flexible array to silence overflow warning
net: lan966x: Fix possible deadlock inside PTP
net/ulp: Remove redundant ->clone() test in inet_clone_ulp().
...
Diffstat (limited to 'fs/hostfs')
-rw-r--r-- | fs/hostfs/Makefile | 11 | ||||
-rw-r--r-- | fs/hostfs/hostfs.h | 103 | ||||
-rw-r--r-- | fs/hostfs/hostfs_kern.c | 1014 | ||||
-rw-r--r-- | fs/hostfs/hostfs_user.c | 410 |
4 files changed, 1538 insertions, 0 deletions
diff --git a/fs/hostfs/Makefile b/fs/hostfs/Makefile new file mode 100644 index 000000000..d5beaffad --- /dev/null +++ b/fs/hostfs/Makefile @@ -0,0 +1,11 @@ +# +# Copyright (C) 2000 Jeff Dike (jdike@karaya.com) +# Licensed under the GPL +# + +hostfs-objs := hostfs_kern.o hostfs_user.o + +obj-y := +obj-$(CONFIG_HOSTFS) += hostfs.o + +include arch/um/scripts/Makefile.rules diff --git a/fs/hostfs/hostfs.h b/fs/hostfs/hostfs.h new file mode 100644 index 000000000..69cb796f6 --- /dev/null +++ b/fs/hostfs/hostfs.h @@ -0,0 +1,103 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __UM_FS_HOSTFS +#define __UM_FS_HOSTFS + +#include <os.h> + +/* + * These are exactly the same definitions as in fs.h, but the names are + * changed so that this file can be included in both kernel and user files. + */ + +#define HOSTFS_ATTR_MODE 1 +#define HOSTFS_ATTR_UID 2 +#define HOSTFS_ATTR_GID 4 +#define HOSTFS_ATTR_SIZE 8 +#define HOSTFS_ATTR_ATIME 16 +#define HOSTFS_ATTR_MTIME 32 +#define HOSTFS_ATTR_CTIME 64 +#define HOSTFS_ATTR_ATIME_SET 128 +#define HOSTFS_ATTR_MTIME_SET 256 + +/* This one is unused by hostfs. */ +#define HOSTFS_ATTR_FORCE 512 /* Not a change, but a change it */ +#define HOSTFS_ATTR_ATTR_FLAG 1024 + +/* + * If you are very careful, you'll notice that these two are missing: + * + * #define ATTR_KILL_SUID 2048 + * #define ATTR_KILL_SGID 4096 + * + * and this is because they were added in 2.5 development. + * Actually, they are not needed by most ->setattr() methods - they are set by + * callers of notify_change() to notify that the setuid/setgid bits must be + * dropped. + * notify_change() will delete those flags, make sure attr->ia_valid & ATTR_MODE + * is on, and remove the appropriate bits from attr->ia_mode (attr is a + * "struct iattr *"). -BlaisorBlade + */ +struct hostfs_timespec { + long long tv_sec; + long long tv_nsec; +}; + +struct hostfs_iattr { + unsigned int ia_valid; + unsigned short ia_mode; + uid_t ia_uid; + gid_t ia_gid; + loff_t ia_size; + struct hostfs_timespec ia_atime; + struct hostfs_timespec ia_mtime; + struct hostfs_timespec ia_ctime; +}; + +struct hostfs_stat { + unsigned long long ino; + unsigned int mode; + unsigned int nlink; + unsigned int uid; + unsigned int gid; + unsigned long long size; + struct hostfs_timespec atime, mtime, ctime; + unsigned int blksize; + unsigned long long blocks; + unsigned int maj; + unsigned int min; +}; + +extern int stat_file(const char *path, struct hostfs_stat *p, int fd); +extern int access_file(char *path, int r, int w, int x); +extern int open_file(char *path, int r, int w, int append); +extern void *open_dir(char *path, int *err_out); +extern void seek_dir(void *stream, unsigned long long pos); +extern char *read_dir(void *stream, unsigned long long *pos_out, + unsigned long long *ino_out, int *len_out, + unsigned int *type_out); +extern void close_file(void *stream); +extern int replace_file(int oldfd, int fd); +extern void close_dir(void *stream); +extern int read_file(int fd, unsigned long long *offset, char *buf, int len); +extern int write_file(int fd, unsigned long long *offset, const char *buf, + int len); +extern int lseek_file(int fd, long long offset, int whence); +extern int fsync_file(int fd, int datasync); +extern int file_create(char *name, int mode); +extern int set_attr(const char *file, struct hostfs_iattr *attrs, int fd); +extern int make_symlink(const char *from, const char *to); +extern int unlink_file(const char *file); +extern int do_mkdir(const char *file, int mode); +extern int hostfs_do_rmdir(const char *file); +extern int do_mknod(const char *file, int mode, unsigned int major, + unsigned int minor); +extern int link_file(const char *to, const char *from); +extern int hostfs_do_readlink(char *file, char *buf, int size); +extern int rename_file(char *from, char *to); +extern int rename2_file(char *from, char *to, unsigned int flags); +extern int do_statfs(char *root, long *bsize_out, long long *blocks_out, + long long *bfree_out, long long *bavail_out, + long long *files_out, long long *ffree_out, + void *fsid_out, int fsid_size, long *namelen_out); + +#endif diff --git a/fs/hostfs/hostfs_kern.c b/fs/hostfs/hostfs_kern.c new file mode 100644 index 000000000..c18bb50c3 --- /dev/null +++ b/fs/hostfs/hostfs_kern.c @@ -0,0 +1,1014 @@ +/* + * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com) + * Licensed under the GPL + * + * Ported the filesystem routines to 2.5. + * 2003-02-10 Petr Baudis <pasky@ucw.cz> + */ + +#include <linux/fs.h> +#include <linux/magic.h> +#include <linux/module.h> +#include <linux/mm.h> +#include <linux/pagemap.h> +#include <linux/statfs.h> +#include <linux/slab.h> +#include <linux/seq_file.h> +#include <linux/writeback.h> +#include <linux/mount.h> +#include <linux/namei.h> +#include "hostfs.h" +#include <init.h> +#include <kern.h> + +struct hostfs_inode_info { + int fd; + fmode_t mode; + struct inode vfs_inode; + struct mutex open_mutex; +}; + +static inline struct hostfs_inode_info *HOSTFS_I(struct inode *inode) +{ + return list_entry(inode, struct hostfs_inode_info, vfs_inode); +} + +#define FILE_HOSTFS_I(file) HOSTFS_I(file_inode(file)) + +static struct kmem_cache *hostfs_inode_cache; + +/* Changed in hostfs_args before the kernel starts running */ +static char *root_ino = ""; +static int append = 0; + +static const struct inode_operations hostfs_iops; +static const struct inode_operations hostfs_dir_iops; +static const struct inode_operations hostfs_link_iops; + +#ifndef MODULE +static int __init hostfs_args(char *options, int *add) +{ + char *ptr; + + ptr = strchr(options, ','); + if (ptr != NULL) + *ptr++ = '\0'; + if (*options != '\0') + root_ino = options; + + options = ptr; + while (options) { + ptr = strchr(options, ','); + if (ptr != NULL) + *ptr++ = '\0'; + if (*options != '\0') { + if (!strcmp(options, "append")) + append = 1; + else printf("hostfs_args - unsupported option - %s\n", + options); + } + options = ptr; + } + return 0; +} + +__uml_setup("hostfs=", hostfs_args, +"hostfs=<root dir>,<flags>,...\n" +" This is used to set hostfs parameters. The root directory argument\n" +" is used to confine all hostfs mounts to within the specified directory\n" +" tree on the host. If this isn't specified, then a user inside UML can\n" +" mount anything on the host that's accessible to the user that's running\n" +" it.\n" +" The only flag currently supported is 'append', which specifies that all\n" +" files opened by hostfs will be opened in append mode.\n\n" +); +#endif + +static char *__dentry_name(struct dentry *dentry, char *name) +{ + char *p = dentry_path_raw(dentry, name, PATH_MAX); + char *root; + size_t len; + + root = dentry->d_sb->s_fs_info; + len = strlen(root); + if (IS_ERR(p)) { + __putname(name); + return NULL; + } + + /* + * This function relies on the fact that dentry_path_raw() will place + * the path name at the end of the provided buffer. + */ + BUG_ON(p + strlen(p) + 1 != name + PATH_MAX); + + strscpy(name, root, PATH_MAX); + if (len > p - name) { + __putname(name); + return NULL; + } + + if (p > name + len) + strcpy(name + len, p); + + return name; +} + +static char *dentry_name(struct dentry *dentry) +{ + char *name = __getname(); + if (!name) + return NULL; + + return __dentry_name(dentry, name); +} + +static char *inode_name(struct inode *ino) +{ + struct dentry *dentry; + char *name; + + dentry = d_find_alias(ino); + if (!dentry) + return NULL; + + name = dentry_name(dentry); + + dput(dentry); + + return name; +} + +static char *follow_link(char *link) +{ + char *name, *resolved, *end; + int n; + + name = kmalloc(PATH_MAX, GFP_KERNEL); + if (!name) { + n = -ENOMEM; + goto out_free; + } + + n = hostfs_do_readlink(link, name, PATH_MAX); + if (n < 0) + goto out_free; + else if (n == PATH_MAX) { + n = -E2BIG; + goto out_free; + } + + if (*name == '/') + return name; + + end = strrchr(link, '/'); + if (end == NULL) + return name; + + *(end + 1) = '\0'; + + resolved = kasprintf(GFP_KERNEL, "%s%s", link, name); + if (resolved == NULL) { + n = -ENOMEM; + goto out_free; + } + + kfree(name); + return resolved; + + out_free: + kfree(name); + return ERR_PTR(n); +} + +static struct inode *hostfs_iget(struct super_block *sb) +{ + struct inode *inode = new_inode(sb); + if (!inode) + return ERR_PTR(-ENOMEM); + return inode; +} + +static int hostfs_statfs(struct dentry *dentry, struct kstatfs *sf) +{ + /* + * do_statfs uses struct statfs64 internally, but the linux kernel + * struct statfs still has 32-bit versions for most of these fields, + * so we convert them here + */ + int err; + long long f_blocks; + long long f_bfree; + long long f_bavail; + long long f_files; + long long f_ffree; + + err = do_statfs(dentry->d_sb->s_fs_info, + &sf->f_bsize, &f_blocks, &f_bfree, &f_bavail, &f_files, + &f_ffree, &sf->f_fsid, sizeof(sf->f_fsid), + &sf->f_namelen); + if (err) + return err; + sf->f_blocks = f_blocks; + sf->f_bfree = f_bfree; + sf->f_bavail = f_bavail; + sf->f_files = f_files; + sf->f_ffree = f_ffree; + sf->f_type = HOSTFS_SUPER_MAGIC; + return 0; +} + +static struct inode *hostfs_alloc_inode(struct super_block *sb) +{ + struct hostfs_inode_info *hi; + + hi = alloc_inode_sb(sb, hostfs_inode_cache, GFP_KERNEL_ACCOUNT); + if (hi == NULL) + return NULL; + hi->fd = -1; + hi->mode = 0; + inode_init_once(&hi->vfs_inode); + mutex_init(&hi->open_mutex); + return &hi->vfs_inode; +} + +static void hostfs_evict_inode(struct inode *inode) +{ + truncate_inode_pages_final(&inode->i_data); + clear_inode(inode); + if (HOSTFS_I(inode)->fd != -1) { + close_file(&HOSTFS_I(inode)->fd); + HOSTFS_I(inode)->fd = -1; + } +} + +static void hostfs_free_inode(struct inode *inode) +{ + kmem_cache_free(hostfs_inode_cache, HOSTFS_I(inode)); +} + +static int hostfs_show_options(struct seq_file *seq, struct dentry *root) +{ + const char *root_path = root->d_sb->s_fs_info; + size_t offset = strlen(root_ino) + 1; + + if (strlen(root_path) > offset) + seq_show_option(seq, root_path + offset, NULL); + + if (append) + seq_puts(seq, ",append"); + + return 0; +} + +static const struct super_operations hostfs_sbops = { + .alloc_inode = hostfs_alloc_inode, + .free_inode = hostfs_free_inode, + .evict_inode = hostfs_evict_inode, + .statfs = hostfs_statfs, + .show_options = hostfs_show_options, +}; + +static int hostfs_readdir(struct file *file, struct dir_context *ctx) +{ + void *dir; + char *name; + unsigned long long next, ino; + int error, len; + unsigned int type; + + name = dentry_name(file->f_path.dentry); + if (name == NULL) + return -ENOMEM; + dir = open_dir(name, &error); + __putname(name); + if (dir == NULL) + return -error; + next = ctx->pos; + seek_dir(dir, next); + while ((name = read_dir(dir, &next, &ino, &len, &type)) != NULL) { + if (!dir_emit(ctx, name, len, ino, type)) + break; + ctx->pos = next; + } + close_dir(dir); + return 0; +} + +static int hostfs_open(struct inode *ino, struct file *file) +{ + char *name; + fmode_t mode; + int err; + int r, w, fd; + + mode = file->f_mode & (FMODE_READ | FMODE_WRITE); + if ((mode & HOSTFS_I(ino)->mode) == mode) + return 0; + + mode |= HOSTFS_I(ino)->mode; + +retry: + r = w = 0; + + if (mode & FMODE_READ) + r = 1; + if (mode & FMODE_WRITE) + r = w = 1; + + name = dentry_name(file_dentry(file)); + if (name == NULL) + return -ENOMEM; + + fd = open_file(name, r, w, append); + __putname(name); + if (fd < 0) + return fd; + + mutex_lock(&HOSTFS_I(ino)->open_mutex); + /* somebody else had handled it first? */ + if ((mode & HOSTFS_I(ino)->mode) == mode) { + mutex_unlock(&HOSTFS_I(ino)->open_mutex); + close_file(&fd); + return 0; + } + if ((mode | HOSTFS_I(ino)->mode) != mode) { + mode |= HOSTFS_I(ino)->mode; + mutex_unlock(&HOSTFS_I(ino)->open_mutex); + close_file(&fd); + goto retry; + } + if (HOSTFS_I(ino)->fd == -1) { + HOSTFS_I(ino)->fd = fd; + } else { + err = replace_file(fd, HOSTFS_I(ino)->fd); + close_file(&fd); + if (err < 0) { + mutex_unlock(&HOSTFS_I(ino)->open_mutex); + return err; + } + } + HOSTFS_I(ino)->mode = mode; + mutex_unlock(&HOSTFS_I(ino)->open_mutex); + + return 0; +} + +static int hostfs_file_release(struct inode *inode, struct file *file) +{ + filemap_write_and_wait(inode->i_mapping); + + return 0; +} + +static int hostfs_fsync(struct file *file, loff_t start, loff_t end, + int datasync) +{ + struct inode *inode = file->f_mapping->host; + int ret; + + ret = file_write_and_wait_range(file, start, end); + if (ret) + return ret; + + inode_lock(inode); + ret = fsync_file(HOSTFS_I(inode)->fd, datasync); + inode_unlock(inode); + + return ret; +} + +static const struct file_operations hostfs_file_fops = { + .llseek = generic_file_llseek, + .splice_read = generic_file_splice_read, + .splice_write = iter_file_splice_write, + .read_iter = generic_file_read_iter, + .write_iter = generic_file_write_iter, + .mmap = generic_file_mmap, + .open = hostfs_open, + .release = hostfs_file_release, + .fsync = hostfs_fsync, +}; + +static const struct file_operations hostfs_dir_fops = { + .llseek = generic_file_llseek, + .iterate_shared = hostfs_readdir, + .read = generic_read_dir, + .open = hostfs_open, + .fsync = hostfs_fsync, +}; + +static int hostfs_writepage(struct page *page, struct writeback_control *wbc) +{ + struct address_space *mapping = page->mapping; + struct inode *inode = mapping->host; + char *buffer; + loff_t base = page_offset(page); + int count = PAGE_SIZE; + int end_index = inode->i_size >> PAGE_SHIFT; + int err; + + if (page->index >= end_index) + count = inode->i_size & (PAGE_SIZE-1); + + buffer = kmap(page); + + err = write_file(HOSTFS_I(inode)->fd, &base, buffer, count); + if (err != count) { + if (err >= 0) + err = -EIO; + mapping_set_error(mapping, err); + goto out; + } + + if (base > inode->i_size) + inode->i_size = base; + + err = 0; + + out: + kunmap(page); + + unlock_page(page); + return err; +} + +static int hostfs_read_folio(struct file *file, struct folio *folio) +{ + struct page *page = &folio->page; + char *buffer; + loff_t start = page_offset(page); + int bytes_read, ret = 0; + + buffer = kmap(page); + bytes_read = read_file(FILE_HOSTFS_I(file)->fd, &start, buffer, + PAGE_SIZE); + if (bytes_read < 0) { + ClearPageUptodate(page); + SetPageError(page); + ret = bytes_read; + goto out; + } + + memset(buffer + bytes_read, 0, PAGE_SIZE - bytes_read); + + ClearPageError(page); + SetPageUptodate(page); + + out: + flush_dcache_page(page); + kunmap(page); + unlock_page(page); + return ret; +} + +static int hostfs_write_begin(struct file *file, struct address_space *mapping, + loff_t pos, unsigned len, + struct page **pagep, void **fsdata) +{ + pgoff_t index = pos >> PAGE_SHIFT; + + *pagep = grab_cache_page_write_begin(mapping, index); + if (!*pagep) + return -ENOMEM; + return 0; +} + +static int hostfs_write_end(struct file *file, struct address_space *mapping, + loff_t pos, unsigned len, unsigned copied, + struct page *page, void *fsdata) +{ + struct inode *inode = mapping->host; + void *buffer; + unsigned from = pos & (PAGE_SIZE - 1); + int err; + + buffer = kmap(page); + err = write_file(FILE_HOSTFS_I(file)->fd, &pos, buffer + from, copied); + kunmap(page); + + if (!PageUptodate(page) && err == PAGE_SIZE) + SetPageUptodate(page); + + /* + * If err > 0, write_file has added err to pos, so we are comparing + * i_size against the last byte written. + */ + if (err > 0 && (pos > inode->i_size)) + inode->i_size = pos; + unlock_page(page); + put_page(page); + + return err; +} + +static const struct address_space_operations hostfs_aops = { + .writepage = hostfs_writepage, + .read_folio = hostfs_read_folio, + .dirty_folio = filemap_dirty_folio, + .write_begin = hostfs_write_begin, + .write_end = hostfs_write_end, +}; + +static int read_name(struct inode *ino, char *name) +{ + dev_t rdev; + struct hostfs_stat st; + int err = stat_file(name, &st, -1); + if (err) + return err; + + /* Reencode maj and min with the kernel encoding.*/ + rdev = MKDEV(st.maj, st.min); + + switch (st.mode & S_IFMT) { + case S_IFLNK: + ino->i_op = &hostfs_link_iops; + break; + case S_IFDIR: + ino->i_op = &hostfs_dir_iops; + ino->i_fop = &hostfs_dir_fops; + break; + case S_IFCHR: + case S_IFBLK: + case S_IFIFO: + case S_IFSOCK: + init_special_inode(ino, st.mode & S_IFMT, rdev); + ino->i_op = &hostfs_iops; + break; + case S_IFREG: + ino->i_op = &hostfs_iops; + ino->i_fop = &hostfs_file_fops; + ino->i_mapping->a_ops = &hostfs_aops; + break; + default: + return -EIO; + } + + ino->i_ino = st.ino; + ino->i_mode = st.mode; + set_nlink(ino, st.nlink); + i_uid_write(ino, st.uid); + i_gid_write(ino, st.gid); + ino->i_atime = (struct timespec64){ st.atime.tv_sec, st.atime.tv_nsec }; + ino->i_mtime = (struct timespec64){ st.mtime.tv_sec, st.mtime.tv_nsec }; + ino->i_ctime = (struct timespec64){ st.ctime.tv_sec, st.ctime.tv_nsec }; + ino->i_size = st.size; + ino->i_blocks = st.blocks; + return 0; +} + +static int hostfs_create(struct mnt_idmap *idmap, struct inode *dir, + struct dentry *dentry, umode_t mode, bool excl) +{ + struct inode *inode; + char *name; + int error, fd; + + inode = hostfs_iget(dir->i_sb); + if (IS_ERR(inode)) { + error = PTR_ERR(inode); + goto out; + } + + error = -ENOMEM; + name = dentry_name(dentry); + if (name == NULL) + goto out_put; + + fd = file_create(name, mode & 0777); + if (fd < 0) + error = fd; + else + error = read_name(inode, name); + + __putname(name); + if (error) + goto out_put; + + HOSTFS_I(inode)->fd = fd; + HOSTFS_I(inode)->mode = FMODE_READ | FMODE_WRITE; + d_instantiate(dentry, inode); + return 0; + + out_put: + iput(inode); + out: + return error; +} + +static struct dentry *hostfs_lookup(struct inode *ino, struct dentry *dentry, + unsigned int flags) +{ + struct inode *inode; + char *name; + int err; + + inode = hostfs_iget(ino->i_sb); + if (IS_ERR(inode)) + goto out; + + err = -ENOMEM; + name = dentry_name(dentry); + if (name) { + err = read_name(inode, name); + __putname(name); + } + if (err) { + iput(inode); + inode = (err == -ENOENT) ? NULL : ERR_PTR(err); + } + out: + return d_splice_alias(inode, dentry); +} + +static int hostfs_link(struct dentry *to, struct inode *ino, + struct dentry *from) +{ + char *from_name, *to_name; + int err; + + if ((from_name = dentry_name(from)) == NULL) + return -ENOMEM; + to_name = dentry_name(to); + if (to_name == NULL) { + __putname(from_name); + return -ENOMEM; + } + err = link_file(to_name, from_name); + __putname(from_name); + __putname(to_name); + return err; +} + +static int hostfs_unlink(struct inode *ino, struct dentry *dentry) +{ + char *file; + int err; + + if (append) + return -EPERM; + + if ((file = dentry_name(dentry)) == NULL) + return -ENOMEM; + + err = unlink_file(file); + __putname(file); + return err; +} + +static int hostfs_symlink(struct mnt_idmap *idmap, struct inode *ino, + struct dentry *dentry, const char *to) +{ + char *file; + int err; + + if ((file = dentry_name(dentry)) == NULL) + return -ENOMEM; + err = make_symlink(file, to); + __putname(file); + return err; +} + +static int hostfs_mkdir(struct mnt_idmap *idmap, struct inode *ino, + struct dentry *dentry, umode_t mode) +{ + char *file; + int err; + + if ((file = dentry_name(dentry)) == NULL) + return -ENOMEM; + err = do_mkdir(file, mode); + __putname(file); + return err; +} + +static int hostfs_rmdir(struct inode *ino, struct dentry *dentry) +{ + char *file; + int err; + + if ((file = dentry_name(dentry)) == NULL) + return -ENOMEM; + err = hostfs_do_rmdir(file); + __putname(file); + return err; +} + +static int hostfs_mknod(struct mnt_idmap *idmap, struct inode *dir, + struct dentry *dentry, umode_t mode, dev_t dev) +{ + struct inode *inode; + char *name; + int err; + + inode = hostfs_iget(dir->i_sb); + if (IS_ERR(inode)) { + err = PTR_ERR(inode); + goto out; + } + + err = -ENOMEM; + name = dentry_name(dentry); + if (name == NULL) + goto out_put; + + err = do_mknod(name, mode, MAJOR(dev), MINOR(dev)); + if (err) + goto out_free; + + err = read_name(inode, name); + __putname(name); + if (err) + goto out_put; + + d_instantiate(dentry, inode); + return 0; + + out_free: + __putname(name); + out_put: + iput(inode); + out: + return err; +} + +static int hostfs_rename2(struct mnt_idmap *idmap, + struct inode *old_dir, struct dentry *old_dentry, + struct inode *new_dir, struct dentry *new_dentry, + unsigned int flags) +{ + char *old_name, *new_name; + int err; + + if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE)) + return -EINVAL; + + old_name = dentry_name(old_dentry); + if (old_name == NULL) + return -ENOMEM; + new_name = dentry_name(new_dentry); + if (new_name == NULL) { + __putname(old_name); + return -ENOMEM; + } + if (!flags) + err = rename_file(old_name, new_name); + else + err = rename2_file(old_name, new_name, flags); + + __putname(old_name); + __putname(new_name); + return err; +} + +static int hostfs_permission(struct mnt_idmap *idmap, + struct inode *ino, int desired) +{ + char *name; + int r = 0, w = 0, x = 0, err; + + if (desired & MAY_NOT_BLOCK) + return -ECHILD; + + if (desired & MAY_READ) r = 1; + if (desired & MAY_WRITE) w = 1; + if (desired & MAY_EXEC) x = 1; + name = inode_name(ino); + if (name == NULL) + return -ENOMEM; + + if (S_ISCHR(ino->i_mode) || S_ISBLK(ino->i_mode) || + S_ISFIFO(ino->i_mode) || S_ISSOCK(ino->i_mode)) + err = 0; + else + err = access_file(name, r, w, x); + __putname(name); + if (!err) + err = generic_permission(&nop_mnt_idmap, ino, desired); + return err; +} + +static int hostfs_setattr(struct mnt_idmap *idmap, + struct dentry *dentry, struct iattr *attr) +{ + struct inode *inode = d_inode(dentry); + struct hostfs_iattr attrs; + char *name; + int err; + + int fd = HOSTFS_I(inode)->fd; + + err = setattr_prepare(&nop_mnt_idmap, dentry, attr); + if (err) + return err; + + if (append) + attr->ia_valid &= ~ATTR_SIZE; + + attrs.ia_valid = 0; + if (attr->ia_valid & ATTR_MODE) { + attrs.ia_valid |= HOSTFS_ATTR_MODE; + attrs.ia_mode = attr->ia_mode; + } + if (attr->ia_valid & ATTR_UID) { + attrs.ia_valid |= HOSTFS_ATTR_UID; + attrs.ia_uid = from_kuid(&init_user_ns, attr->ia_uid); + } + if (attr->ia_valid & ATTR_GID) { + attrs.ia_valid |= HOSTFS_ATTR_GID; + attrs.ia_gid = from_kgid(&init_user_ns, attr->ia_gid); + } + if (attr->ia_valid & ATTR_SIZE) { + attrs.ia_valid |= HOSTFS_ATTR_SIZE; + attrs.ia_size = attr->ia_size; + } + if (attr->ia_valid & ATTR_ATIME) { + attrs.ia_valid |= HOSTFS_ATTR_ATIME; + attrs.ia_atime = (struct hostfs_timespec) + { attr->ia_atime.tv_sec, attr->ia_atime.tv_nsec }; + } + if (attr->ia_valid & ATTR_MTIME) { + attrs.ia_valid |= HOSTFS_ATTR_MTIME; + attrs.ia_mtime = (struct hostfs_timespec) + { attr->ia_mtime.tv_sec, attr->ia_mtime.tv_nsec }; + } + if (attr->ia_valid & ATTR_CTIME) { + attrs.ia_valid |= HOSTFS_ATTR_CTIME; + attrs.ia_ctime = (struct hostfs_timespec) + { attr->ia_ctime.tv_sec, attr->ia_ctime.tv_nsec }; + } + if (attr->ia_valid & ATTR_ATIME_SET) { + attrs.ia_valid |= HOSTFS_ATTR_ATIME_SET; + } + if (attr->ia_valid & ATTR_MTIME_SET) { + attrs.ia_valid |= HOSTFS_ATTR_MTIME_SET; + } + name = dentry_name(dentry); + if (name == NULL) + return -ENOMEM; + err = set_attr(name, &attrs, fd); + __putname(name); + if (err) + return err; + + if ((attr->ia_valid & ATTR_SIZE) && + attr->ia_size != i_size_read(inode)) + truncate_setsize(inode, attr->ia_size); + + setattr_copy(&nop_mnt_idmap, inode, attr); + mark_inode_dirty(inode); + return 0; +} + +static const struct inode_operations hostfs_iops = { + .permission = hostfs_permission, + .setattr = hostfs_setattr, +}; + +static const struct inode_operations hostfs_dir_iops = { + .create = hostfs_create, + .lookup = hostfs_lookup, + .link = hostfs_link, + .unlink = hostfs_unlink, + .symlink = hostfs_symlink, + .mkdir = hostfs_mkdir, + .rmdir = hostfs_rmdir, + .mknod = hostfs_mknod, + .rename = hostfs_rename2, + .permission = hostfs_permission, + .setattr = hostfs_setattr, +}; + +static const char *hostfs_get_link(struct dentry *dentry, + struct inode *inode, + struct delayed_call *done) +{ + char *link; + if (!dentry) + return ERR_PTR(-ECHILD); + link = kmalloc(PATH_MAX, GFP_KERNEL); + if (link) { + char *path = dentry_name(dentry); + int err = -ENOMEM; + if (path) { + err = hostfs_do_readlink(path, link, PATH_MAX); + if (err == PATH_MAX) + err = -E2BIG; + __putname(path); + } + if (err < 0) { + kfree(link); + return ERR_PTR(err); + } + } else { + return ERR_PTR(-ENOMEM); + } + + set_delayed_call(done, kfree_link, link); + return link; +} + +static const struct inode_operations hostfs_link_iops = { + .get_link = hostfs_get_link, +}; + +static int hostfs_fill_sb_common(struct super_block *sb, void *d, int silent) +{ + struct inode *root_inode; + char *host_root_path, *req_root = d; + int err; + + sb->s_blocksize = 1024; + sb->s_blocksize_bits = 10; + sb->s_magic = HOSTFS_SUPER_MAGIC; + sb->s_op = &hostfs_sbops; + sb->s_d_op = &simple_dentry_operations; + sb->s_maxbytes = MAX_LFS_FILESIZE; + err = super_setup_bdi(sb); + if (err) + goto out; + + /* NULL is printed as '(null)' by printf(): avoid that. */ + if (req_root == NULL) + req_root = ""; + + err = -ENOMEM; + sb->s_fs_info = host_root_path = + kasprintf(GFP_KERNEL, "%s/%s", root_ino, req_root); + if (host_root_path == NULL) + goto out; + + root_inode = new_inode(sb); + if (!root_inode) + goto out; + + err = read_name(root_inode, host_root_path); + if (err) + goto out_put; + + if (S_ISLNK(root_inode->i_mode)) { + char *name = follow_link(host_root_path); + if (IS_ERR(name)) { + err = PTR_ERR(name); + goto out_put; + } + err = read_name(root_inode, name); + kfree(name); + if (err) + goto out_put; + } + + err = -ENOMEM; + sb->s_root = d_make_root(root_inode); + if (sb->s_root == NULL) + goto out; + + return 0; + +out_put: + iput(root_inode); +out: + return err; +} + +static struct dentry *hostfs_read_sb(struct file_system_type *type, + int flags, const char *dev_name, + void *data) +{ + return mount_nodev(type, flags, data, hostfs_fill_sb_common); +} + +static void hostfs_kill_sb(struct super_block *s) +{ + kill_anon_super(s); + kfree(s->s_fs_info); +} + +static struct file_system_type hostfs_type = { + .owner = THIS_MODULE, + .name = "hostfs", + .mount = hostfs_read_sb, + .kill_sb = hostfs_kill_sb, + .fs_flags = 0, +}; +MODULE_ALIAS_FS("hostfs"); + +static int __init init_hostfs(void) +{ + hostfs_inode_cache = KMEM_CACHE(hostfs_inode_info, 0); + if (!hostfs_inode_cache) + return -ENOMEM; + return register_filesystem(&hostfs_type); +} + +static void __exit exit_hostfs(void) +{ + unregister_filesystem(&hostfs_type); + kmem_cache_destroy(hostfs_inode_cache); +} + +module_init(init_hostfs) +module_exit(exit_hostfs) +MODULE_LICENSE("GPL"); diff --git a/fs/hostfs/hostfs_user.c b/fs/hostfs/hostfs_user.c new file mode 100644 index 000000000..5ecc47061 --- /dev/null +++ b/fs/hostfs/hostfs_user.c @@ -0,0 +1,410 @@ +/* + * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com) + * Licensed under the GPL + */ + +#include <stdio.h> +#include <stddef.h> +#include <unistd.h> +#include <dirent.h> +#include <errno.h> +#include <fcntl.h> +#include <string.h> +#include <sys/stat.h> +#include <sys/time.h> +#include <sys/types.h> +#include <sys/vfs.h> +#include <sys/syscall.h> +#include "hostfs.h" +#include <utime.h> + +static void stat64_to_hostfs(const struct stat64 *buf, struct hostfs_stat *p) +{ + p->ino = buf->st_ino; + p->mode = buf->st_mode; + p->nlink = buf->st_nlink; + p->uid = buf->st_uid; + p->gid = buf->st_gid; + p->size = buf->st_size; + p->atime.tv_sec = buf->st_atime; + p->atime.tv_nsec = 0; + p->ctime.tv_sec = buf->st_ctime; + p->ctime.tv_nsec = 0; + p->mtime.tv_sec = buf->st_mtime; + p->mtime.tv_nsec = 0; + p->blksize = buf->st_blksize; + p->blocks = buf->st_blocks; + p->maj = os_major(buf->st_rdev); + p->min = os_minor(buf->st_rdev); +} + +int stat_file(const char *path, struct hostfs_stat *p, int fd) +{ + struct stat64 buf; + + if (fd >= 0) { + if (fstat64(fd, &buf) < 0) + return -errno; + } else if (lstat64(path, &buf) < 0) { + return -errno; + } + stat64_to_hostfs(&buf, p); + return 0; +} + +int access_file(char *path, int r, int w, int x) +{ + int mode = 0; + + if (r) + mode = R_OK; + if (w) + mode |= W_OK; + if (x) + mode |= X_OK; + if (access(path, mode) != 0) + return -errno; + else return 0; +} + +int open_file(char *path, int r, int w, int append) +{ + int mode = 0, fd; + + if (r && !w) + mode = O_RDONLY; + else if (!r && w) + mode = O_WRONLY; + else if (r && w) + mode = O_RDWR; + else panic("Impossible mode in open_file"); + + if (append) + mode |= O_APPEND; + fd = open64(path, mode); + if (fd < 0) + return -errno; + else return fd; +} + +void *open_dir(char *path, int *err_out) +{ + DIR *dir; + + dir = opendir(path); + *err_out = errno; + + return dir; +} + +void seek_dir(void *stream, unsigned long long pos) +{ + DIR *dir = stream; + + seekdir(dir, pos); +} + +char *read_dir(void *stream, unsigned long long *pos_out, + unsigned long long *ino_out, int *len_out, + unsigned int *type_out) +{ + DIR *dir = stream; + struct dirent *ent; + + ent = readdir(dir); + if (ent == NULL) + return NULL; + *len_out = strlen(ent->d_name); + *ino_out = ent->d_ino; + *type_out = ent->d_type; + *pos_out = ent->d_off; + return ent->d_name; +} + +int read_file(int fd, unsigned long long *offset, char *buf, int len) +{ + int n; + + n = pread64(fd, buf, len, *offset); + if (n < 0) + return -errno; + *offset += n; + return n; +} + +int write_file(int fd, unsigned long long *offset, const char *buf, int len) +{ + int n; + + n = pwrite64(fd, buf, len, *offset); + if (n < 0) + return -errno; + *offset += n; + return n; +} + +int lseek_file(int fd, long long offset, int whence) +{ + int ret; + + ret = lseek64(fd, offset, whence); + if (ret < 0) + return -errno; + return 0; +} + +int fsync_file(int fd, int datasync) +{ + int ret; + if (datasync) + ret = fdatasync(fd); + else + ret = fsync(fd); + + if (ret < 0) + return -errno; + return 0; +} + +int replace_file(int oldfd, int fd) +{ + return dup2(oldfd, fd); +} + +void close_file(void *stream) +{ + close(*((int *) stream)); +} + +void close_dir(void *stream) +{ + closedir(stream); +} + +int file_create(char *name, int mode) +{ + int fd; + + fd = open64(name, O_CREAT | O_RDWR, mode); + if (fd < 0) + return -errno; + return fd; +} + +int set_attr(const char *file, struct hostfs_iattr *attrs, int fd) +{ + struct hostfs_stat st; + struct timeval times[2]; + int err, ma; + + if (attrs->ia_valid & HOSTFS_ATTR_MODE) { + if (fd >= 0) { + if (fchmod(fd, attrs->ia_mode) != 0) + return -errno; + } else if (chmod(file, attrs->ia_mode) != 0) { + return -errno; + } + } + if (attrs->ia_valid & HOSTFS_ATTR_UID) { + if (fd >= 0) { + if (fchown(fd, attrs->ia_uid, -1)) + return -errno; + } else if (chown(file, attrs->ia_uid, -1)) { + return -errno; + } + } + if (attrs->ia_valid & HOSTFS_ATTR_GID) { + if (fd >= 0) { + if (fchown(fd, -1, attrs->ia_gid)) + return -errno; + } else if (chown(file, -1, attrs->ia_gid)) { + return -errno; + } + } + if (attrs->ia_valid & HOSTFS_ATTR_SIZE) { + if (fd >= 0) { + if (ftruncate(fd, attrs->ia_size)) + return -errno; + } else if (truncate(file, attrs->ia_size)) { + return -errno; + } + } + + /* + * Update accessed and/or modified time, in two parts: first set + * times according to the changes to perform, and then call futimes() + * or utimes() to apply them. + */ + ma = (HOSTFS_ATTR_ATIME_SET | HOSTFS_ATTR_MTIME_SET); + if (attrs->ia_valid & ma) { + err = stat_file(file, &st, fd); + if (err != 0) + return err; + + times[0].tv_sec = st.atime.tv_sec; + times[0].tv_usec = st.atime.tv_nsec / 1000; + times[1].tv_sec = st.mtime.tv_sec; + times[1].tv_usec = st.mtime.tv_nsec / 1000; + + if (attrs->ia_valid & HOSTFS_ATTR_ATIME_SET) { + times[0].tv_sec = attrs->ia_atime.tv_sec; + times[0].tv_usec = attrs->ia_atime.tv_nsec / 1000; + } + if (attrs->ia_valid & HOSTFS_ATTR_MTIME_SET) { + times[1].tv_sec = attrs->ia_mtime.tv_sec; + times[1].tv_usec = attrs->ia_mtime.tv_nsec / 1000; + } + + if (fd >= 0) { + if (futimes(fd, times) != 0) + return -errno; + } else if (utimes(file, times) != 0) { + return -errno; + } + } + + /* Note: ctime is not handled */ + if (attrs->ia_valid & (HOSTFS_ATTR_ATIME | HOSTFS_ATTR_MTIME)) { + err = stat_file(file, &st, fd); + attrs->ia_atime = st.atime; + attrs->ia_mtime = st.mtime; + if (err != 0) + return err; + } + return 0; +} + +int make_symlink(const char *from, const char *to) +{ + int err; + + err = symlink(to, from); + if (err) + return -errno; + return 0; +} + +int unlink_file(const char *file) +{ + int err; + + err = unlink(file); + if (err) + return -errno; + return 0; +} + +int do_mkdir(const char *file, int mode) +{ + int err; + + err = mkdir(file, mode); + if (err) + return -errno; + return 0; +} + +int hostfs_do_rmdir(const char *file) +{ + int err; + + err = rmdir(file); + if (err) + return -errno; + return 0; +} + +int do_mknod(const char *file, int mode, unsigned int major, unsigned int minor) +{ + int err; + + err = mknod(file, mode, os_makedev(major, minor)); + if (err) + return -errno; + return 0; +} + +int link_file(const char *to, const char *from) +{ + int err; + + err = link(to, from); + if (err) + return -errno; + return 0; +} + +int hostfs_do_readlink(char *file, char *buf, int size) +{ + int n; + + n = readlink(file, buf, size); + if (n < 0) + return -errno; + if (n < size) + buf[n] = '\0'; + return n; +} + +int rename_file(char *from, char *to) +{ + int err; + + err = rename(from, to); + if (err < 0) + return -errno; + return 0; +} + +int rename2_file(char *from, char *to, unsigned int flags) +{ + int err; + +#ifndef SYS_renameat2 +# ifdef __x86_64__ +# define SYS_renameat2 316 +# endif +# ifdef __i386__ +# define SYS_renameat2 353 +# endif +#endif + +#ifdef SYS_renameat2 + err = syscall(SYS_renameat2, AT_FDCWD, from, AT_FDCWD, to, flags); + if (err < 0) { + if (errno != ENOSYS) + return -errno; + else + return -EINVAL; + } + return 0; +#else + return -EINVAL; +#endif +} + +int do_statfs(char *root, long *bsize_out, long long *blocks_out, + long long *bfree_out, long long *bavail_out, + long long *files_out, long long *ffree_out, + void *fsid_out, int fsid_size, long *namelen_out) +{ + struct statfs64 buf; + int err; + + err = statfs64(root, &buf); + if (err < 0) + return -errno; + + *bsize_out = buf.f_bsize; + *blocks_out = buf.f_blocks; + *bfree_out = buf.f_bfree; + *bavail_out = buf.f_bavail; + *files_out = buf.f_files; + *ffree_out = buf.f_ffree; + memcpy(fsid_out, &buf.f_fsid, + sizeof(buf.f_fsid) > fsid_size ? fsid_size : + sizeof(buf.f_fsid)); + *namelen_out = buf.f_namelen; + + return 0; +} |