mirror of
https://github.com/AuxXxilium/linux_dsm_epyc7002.git
synced 2024-12-14 05:46:50 +07:00
a528d35e8b
Add a system call to make extended file information available, including file creation and some attribute flags where available through the underlying filesystem. The getattr inode operation is altered to take two additional arguments: a u32 request_mask and an unsigned int flags that indicate the synchronisation mode. This change is propagated to the vfs_getattr*() function. Functions like vfs_stat() are now inline wrappers around new functions vfs_statx() and vfs_statx_fd() to reduce stack usage. ======== OVERVIEW ======== The idea was initially proposed as a set of xattrs that could be retrieved with getxattr(), but the general preference proved to be for a new syscall with an extended stat structure. A number of requests were gathered for features to be included. The following have been included: (1) Make the fields a consistent size on all arches and make them large. (2) Spare space, request flags and information flags are provided for future expansion. (3) Better support for the y2038 problem [Arnd Bergmann] (tv_sec is an __s64). (4) Creation time: The SMB protocol carries the creation time, which could be exported by Samba, which will in turn help CIFS make use of FS-Cache as that can be used for coherency data (stx_btime). This is also specified in NFSv4 as a recommended attribute and could be exported by NFSD [Steve French]. (5) Lightweight stat: Ask for just those details of interest, and allow a netfs (such as NFS) to approximate anything not of interest, possibly without going to the server [Trond Myklebust, Ulrich Drepper, Andreas Dilger] (AT_STATX_DONT_SYNC). (6) Heavyweight stat: Force a netfs to go to the server, even if it thinks its cached attributes are up to date [Trond Myklebust] (AT_STATX_FORCE_SYNC). And the following have been left out for future extension: (7) Data version number: Could be used by userspace NFS servers [Aneesh Kumar]. Can also be used to modify fill_post_wcc() in NFSD which retrieves i_version directly, but has just called vfs_getattr(). It could get it from the kstat struct if it used vfs_xgetattr() instead. (There's disagreement on the exact semantics of a single field, since not all filesystems do this the same way). (8) BSD stat compatibility: Including more fields from the BSD stat such as creation time (st_btime) and inode generation number (st_gen) [Jeremy Allison, Bernd Schubert]. (9) Inode generation number: Useful for FUSE and userspace NFS servers [Bernd Schubert]. (This was asked for but later deemed unnecessary with the open-by-handle capability available and caused disagreement as to whether it's a security hole or not). (10) Extra coherency data may be useful in making backups [Andreas Dilger]. (No particular data were offered, but things like last backup timestamp, the data version number and the DOS archive bit would come into this category). (11) Allow the filesystem to indicate what it can/cannot provide: A filesystem can now say it doesn't support a standard stat feature if that isn't available, so if, for instance, inode numbers or UIDs don't exist or are fabricated locally... (This requires a separate system call - I have an fsinfo() call idea for this). (12) Store a 16-byte volume ID in the superblock that can be returned in struct xstat [Steve French]. (Deferred to fsinfo). (13) Include granularity fields in the time data to indicate the granularity of each of the times (NFSv4 time_delta) [Steve French]. (Deferred to fsinfo). (14) FS_IOC_GETFLAGS value. These could be translated to BSD's st_flags. Note that the Linux IOC flags are a mess and filesystems such as Ext4 define flags that aren't in linux/fs.h, so translation in the kernel may be a necessity (or, possibly, we provide the filesystem type too). (Some attributes are made available in stx_attributes, but the general feeling was that the IOC flags were to ext[234]-specific and shouldn't be exposed through statx this way). (15) Mask of features available on file (eg: ACLs, seclabel) [Brad Boyer, Michael Kerrisk]. (Deferred, probably to fsinfo. Finding out if there's an ACL or seclabal might require extra filesystem operations). (16) Femtosecond-resolution timestamps [Dave Chinner]. (A __reserved field has been left in the statx_timestamp struct for this - if there proves to be a need). (17) A set multiple attributes syscall to go with this. =============== NEW SYSTEM CALL =============== The new system call is: int ret = statx(int dfd, const char *filename, unsigned int flags, unsigned int mask, struct statx *buffer); The dfd, filename and flags parameters indicate the file to query, in a similar way to fstatat(). There is no equivalent of lstat() as that can be emulated with statx() by passing AT_SYMLINK_NOFOLLOW in flags. There is also no equivalent of fstat() as that can be emulated by passing a NULL filename to statx() with the fd of interest in dfd. Whether or not statx() synchronises the attributes with the backing store can be controlled by OR'ing a value into the flags argument (this typically only affects network filesystems): (1) AT_STATX_SYNC_AS_STAT tells statx() to behave as stat() does in this respect. (2) AT_STATX_FORCE_SYNC will require a network filesystem to synchronise its attributes with the server - which might require data writeback to occur to get the timestamps correct. (3) AT_STATX_DONT_SYNC will suppress synchronisation with the server in a network filesystem. The resulting values should be considered approximate. mask is a bitmask indicating the fields in struct statx that are of interest to the caller. The user should set this to STATX_BASIC_STATS to get the basic set returned by stat(). It should be noted that asking for more information may entail extra I/O operations. buffer points to the destination for the data. This must be 256 bytes in size. ====================== MAIN ATTRIBUTES RECORD ====================== The following structures are defined in which to return the main attribute set: struct statx_timestamp { __s64 tv_sec; __s32 tv_nsec; __s32 __reserved; }; struct statx { __u32 stx_mask; __u32 stx_blksize; __u64 stx_attributes; __u32 stx_nlink; __u32 stx_uid; __u32 stx_gid; __u16 stx_mode; __u16 __spare0[1]; __u64 stx_ino; __u64 stx_size; __u64 stx_blocks; __u64 __spare1[1]; struct statx_timestamp stx_atime; struct statx_timestamp stx_btime; struct statx_timestamp stx_ctime; struct statx_timestamp stx_mtime; __u32 stx_rdev_major; __u32 stx_rdev_minor; __u32 stx_dev_major; __u32 stx_dev_minor; __u64 __spare2[14]; }; The defined bits in request_mask and stx_mask are: STATX_TYPE Want/got stx_mode & S_IFMT STATX_MODE Want/got stx_mode & ~S_IFMT STATX_NLINK Want/got stx_nlink STATX_UID Want/got stx_uid STATX_GID Want/got stx_gid STATX_ATIME Want/got stx_atime{,_ns} STATX_MTIME Want/got stx_mtime{,_ns} STATX_CTIME Want/got stx_ctime{,_ns} STATX_INO Want/got stx_ino STATX_SIZE Want/got stx_size STATX_BLOCKS Want/got stx_blocks STATX_BASIC_STATS [The stuff in the normal stat struct] STATX_BTIME Want/got stx_btime{,_ns} STATX_ALL [All currently available stuff] stx_btime is the file creation time, stx_mask is a bitmask indicating the data provided and __spares*[] are where as-yet undefined fields can be placed. Time fields are structures with separate seconds and nanoseconds fields plus a reserved field in case we want to add even finer resolution. Note that times will be negative if before 1970; in such a case, the nanosecond fields will also be negative if not zero. The bits defined in the stx_attributes field convey information about a file, how it is accessed, where it is and what it does. The following attributes map to FS_*_FL flags and are the same numerical value: STATX_ATTR_COMPRESSED File is compressed by the fs STATX_ATTR_IMMUTABLE File is marked immutable STATX_ATTR_APPEND File is append-only STATX_ATTR_NODUMP File is not to be dumped STATX_ATTR_ENCRYPTED File requires key to decrypt in fs Within the kernel, the supported flags are listed by: KSTAT_ATTR_FS_IOC_FLAGS [Are any other IOC flags of sufficient general interest to be exposed through this interface?] New flags include: STATX_ATTR_AUTOMOUNT Object is an automount trigger These are for the use of GUI tools that might want to mark files specially, depending on what they are. Fields in struct statx come in a number of classes: (0) stx_dev_*, stx_blksize. These are local system information and are always available. (1) stx_mode, stx_nlinks, stx_uid, stx_gid, stx_[amc]time, stx_ino, stx_size, stx_blocks. These will be returned whether the caller asks for them or not. The corresponding bits in stx_mask will be set to indicate whether they actually have valid values. If the caller didn't ask for them, then they may be approximated. For example, NFS won't waste any time updating them from the server, unless as a byproduct of updating something requested. If the values don't actually exist for the underlying object (such as UID or GID on a DOS file), then the bit won't be set in the stx_mask, even if the caller asked for the value. In such a case, the returned value will be a fabrication. Note that there are instances where the type might not be valid, for instance Windows reparse points. (2) stx_rdev_*. This will be set only if stx_mode indicates we're looking at a blockdev or a chardev, otherwise will be 0. (3) stx_btime. Similar to (1), except this will be set to 0 if it doesn't exist. ======= TESTING ======= The following test program can be used to test the statx system call: samples/statx/test-statx.c Just compile and run, passing it paths to the files you want to examine. The file is built automatically if CONFIG_SAMPLES is enabled. Here's some example output. Firstly, an NFS directory that crosses to another FSID. Note that the AUTOMOUNT attribute is set because transiting this directory will cause d_automount to be invoked by the VFS. [root@andromeda ~]# /tmp/test-statx -A /warthog/data statx(/warthog/data) = 0 results=7ff Size: 4096 Blocks: 8 IO Block: 1048576 directory Device: 00:26 Inode: 1703937 Links: 125 Access: (3777/drwxrwxrwx) Uid: 0 Gid: 4041 Access: 2016-11-24 09:02:12.219699527+0000 Modify: 2016-11-17 10:44:36.225653653+0000 Change: 2016-11-17 10:44:36.225653653+0000 Attributes: 0000000000001000 (-------- -------- -------- -------- -------- -------- ---m---- --------) Secondly, the result of automounting on that directory. [root@andromeda ~]# /tmp/test-statx /warthog/data statx(/warthog/data) = 0 results=7ff Size: 4096 Blocks: 8 IO Block: 1048576 directory Device: 00:27 Inode: 2 Links: 125 Access: (3777/drwxrwxrwx) Uid: 0 Gid: 4041 Access: 2016-11-24 09:02:12.219699527+0000 Modify: 2016-11-17 10:44:36.225653653+0000 Change: 2016-11-17 10:44:36.225653653+0000 Signed-off-by: David Howells <dhowells@redhat.com> Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
650 lines
14 KiB
C
650 lines
14 KiB
C
/*
|
|
* proc/fs/generic.c --- generic routines for the proc-fs
|
|
*
|
|
* This file contains generic proc-fs routines for handling
|
|
* directories and files.
|
|
*
|
|
* Copyright (C) 1991, 1992 Linus Torvalds.
|
|
* Copyright (C) 1997 Theodore Ts'o
|
|
*/
|
|
|
|
#include <linux/errno.h>
|
|
#include <linux/time.h>
|
|
#include <linux/proc_fs.h>
|
|
#include <linux/stat.h>
|
|
#include <linux/mm.h>
|
|
#include <linux/module.h>
|
|
#include <linux/slab.h>
|
|
#include <linux/printk.h>
|
|
#include <linux/mount.h>
|
|
#include <linux/init.h>
|
|
#include <linux/idr.h>
|
|
#include <linux/bitops.h>
|
|
#include <linux/spinlock.h>
|
|
#include <linux/completion.h>
|
|
#include <linux/uaccess.h>
|
|
|
|
#include "internal.h"
|
|
|
|
static DEFINE_RWLOCK(proc_subdir_lock);
|
|
|
|
static int proc_match(unsigned int len, const char *name, struct proc_dir_entry *de)
|
|
{
|
|
if (len < de->namelen)
|
|
return -1;
|
|
if (len > de->namelen)
|
|
return 1;
|
|
|
|
return memcmp(name, de->name, len);
|
|
}
|
|
|
|
static struct proc_dir_entry *pde_subdir_first(struct proc_dir_entry *dir)
|
|
{
|
|
return rb_entry_safe(rb_first(&dir->subdir), struct proc_dir_entry,
|
|
subdir_node);
|
|
}
|
|
|
|
static struct proc_dir_entry *pde_subdir_next(struct proc_dir_entry *dir)
|
|
{
|
|
return rb_entry_safe(rb_next(&dir->subdir_node), struct proc_dir_entry,
|
|
subdir_node);
|
|
}
|
|
|
|
static struct proc_dir_entry *pde_subdir_find(struct proc_dir_entry *dir,
|
|
const char *name,
|
|
unsigned int len)
|
|
{
|
|
struct rb_node *node = dir->subdir.rb_node;
|
|
|
|
while (node) {
|
|
struct proc_dir_entry *de = rb_entry(node,
|
|
struct proc_dir_entry,
|
|
subdir_node);
|
|
int result = proc_match(len, name, de);
|
|
|
|
if (result < 0)
|
|
node = node->rb_left;
|
|
else if (result > 0)
|
|
node = node->rb_right;
|
|
else
|
|
return de;
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
static bool pde_subdir_insert(struct proc_dir_entry *dir,
|
|
struct proc_dir_entry *de)
|
|
{
|
|
struct rb_root *root = &dir->subdir;
|
|
struct rb_node **new = &root->rb_node, *parent = NULL;
|
|
|
|
/* Figure out where to put new node */
|
|
while (*new) {
|
|
struct proc_dir_entry *this = rb_entry(*new,
|
|
struct proc_dir_entry,
|
|
subdir_node);
|
|
int result = proc_match(de->namelen, de->name, this);
|
|
|
|
parent = *new;
|
|
if (result < 0)
|
|
new = &(*new)->rb_left;
|
|
else if (result > 0)
|
|
new = &(*new)->rb_right;
|
|
else
|
|
return false;
|
|
}
|
|
|
|
/* Add new node and rebalance tree. */
|
|
rb_link_node(&de->subdir_node, parent, new);
|
|
rb_insert_color(&de->subdir_node, root);
|
|
return true;
|
|
}
|
|
|
|
static int proc_notify_change(struct dentry *dentry, struct iattr *iattr)
|
|
{
|
|
struct inode *inode = d_inode(dentry);
|
|
struct proc_dir_entry *de = PDE(inode);
|
|
int error;
|
|
|
|
error = setattr_prepare(dentry, iattr);
|
|
if (error)
|
|
return error;
|
|
|
|
setattr_copy(inode, iattr);
|
|
mark_inode_dirty(inode);
|
|
|
|
proc_set_user(de, inode->i_uid, inode->i_gid);
|
|
de->mode = inode->i_mode;
|
|
return 0;
|
|
}
|
|
|
|
static int proc_getattr(const struct path *path, struct kstat *stat,
|
|
u32 request_mask, unsigned int query_flags)
|
|
{
|
|
struct inode *inode = d_inode(path->dentry);
|
|
struct proc_dir_entry *de = PDE(inode);
|
|
if (de && de->nlink)
|
|
set_nlink(inode, de->nlink);
|
|
|
|
generic_fillattr(inode, stat);
|
|
return 0;
|
|
}
|
|
|
|
static const struct inode_operations proc_file_inode_operations = {
|
|
.setattr = proc_notify_change,
|
|
};
|
|
|
|
/*
|
|
* This function parses a name such as "tty/driver/serial", and
|
|
* returns the struct proc_dir_entry for "/proc/tty/driver", and
|
|
* returns "serial" in residual.
|
|
*/
|
|
static int __xlate_proc_name(const char *name, struct proc_dir_entry **ret,
|
|
const char **residual)
|
|
{
|
|
const char *cp = name, *next;
|
|
struct proc_dir_entry *de;
|
|
unsigned int len;
|
|
|
|
de = *ret;
|
|
if (!de)
|
|
de = &proc_root;
|
|
|
|
while (1) {
|
|
next = strchr(cp, '/');
|
|
if (!next)
|
|
break;
|
|
|
|
len = next - cp;
|
|
de = pde_subdir_find(de, cp, len);
|
|
if (!de) {
|
|
WARN(1, "name '%s'\n", name);
|
|
return -ENOENT;
|
|
}
|
|
cp += len + 1;
|
|
}
|
|
*residual = cp;
|
|
*ret = de;
|
|
return 0;
|
|
}
|
|
|
|
static int xlate_proc_name(const char *name, struct proc_dir_entry **ret,
|
|
const char **residual)
|
|
{
|
|
int rv;
|
|
|
|
read_lock(&proc_subdir_lock);
|
|
rv = __xlate_proc_name(name, ret, residual);
|
|
read_unlock(&proc_subdir_lock);
|
|
return rv;
|
|
}
|
|
|
|
static DEFINE_IDA(proc_inum_ida);
|
|
static DEFINE_SPINLOCK(proc_inum_lock); /* protects the above */
|
|
|
|
#define PROC_DYNAMIC_FIRST 0xF0000000U
|
|
|
|
/*
|
|
* Return an inode number between PROC_DYNAMIC_FIRST and
|
|
* 0xffffffff, or zero on failure.
|
|
*/
|
|
int proc_alloc_inum(unsigned int *inum)
|
|
{
|
|
unsigned int i;
|
|
int error;
|
|
|
|
retry:
|
|
if (!ida_pre_get(&proc_inum_ida, GFP_KERNEL))
|
|
return -ENOMEM;
|
|
|
|
spin_lock_irq(&proc_inum_lock);
|
|
error = ida_get_new(&proc_inum_ida, &i);
|
|
spin_unlock_irq(&proc_inum_lock);
|
|
if (error == -EAGAIN)
|
|
goto retry;
|
|
else if (error)
|
|
return error;
|
|
|
|
if (i > UINT_MAX - PROC_DYNAMIC_FIRST) {
|
|
spin_lock_irq(&proc_inum_lock);
|
|
ida_remove(&proc_inum_ida, i);
|
|
spin_unlock_irq(&proc_inum_lock);
|
|
return -ENOSPC;
|
|
}
|
|
*inum = PROC_DYNAMIC_FIRST + i;
|
|
return 0;
|
|
}
|
|
|
|
void proc_free_inum(unsigned int inum)
|
|
{
|
|
unsigned long flags;
|
|
spin_lock_irqsave(&proc_inum_lock, flags);
|
|
ida_remove(&proc_inum_ida, inum - PROC_DYNAMIC_FIRST);
|
|
spin_unlock_irqrestore(&proc_inum_lock, flags);
|
|
}
|
|
|
|
/*
|
|
* Don't create negative dentries here, return -ENOENT by hand
|
|
* instead.
|
|
*/
|
|
struct dentry *proc_lookup_de(struct proc_dir_entry *de, struct inode *dir,
|
|
struct dentry *dentry)
|
|
{
|
|
struct inode *inode;
|
|
|
|
read_lock(&proc_subdir_lock);
|
|
de = pde_subdir_find(de, dentry->d_name.name, dentry->d_name.len);
|
|
if (de) {
|
|
pde_get(de);
|
|
read_unlock(&proc_subdir_lock);
|
|
inode = proc_get_inode(dir->i_sb, de);
|
|
if (!inode)
|
|
return ERR_PTR(-ENOMEM);
|
|
d_set_d_op(dentry, &simple_dentry_operations);
|
|
d_add(dentry, inode);
|
|
return NULL;
|
|
}
|
|
read_unlock(&proc_subdir_lock);
|
|
return ERR_PTR(-ENOENT);
|
|
}
|
|
|
|
struct dentry *proc_lookup(struct inode *dir, struct dentry *dentry,
|
|
unsigned int flags)
|
|
{
|
|
return proc_lookup_de(PDE(dir), dir, dentry);
|
|
}
|
|
|
|
/*
|
|
* This returns non-zero if at EOF, so that the /proc
|
|
* root directory can use this and check if it should
|
|
* continue with the <pid> entries..
|
|
*
|
|
* Note that the VFS-layer doesn't care about the return
|
|
* value of the readdir() call, as long as it's non-negative
|
|
* for success..
|
|
*/
|
|
int proc_readdir_de(struct proc_dir_entry *de, struct file *file,
|
|
struct dir_context *ctx)
|
|
{
|
|
int i;
|
|
|
|
if (!dir_emit_dots(file, ctx))
|
|
return 0;
|
|
|
|
read_lock(&proc_subdir_lock);
|
|
de = pde_subdir_first(de);
|
|
i = ctx->pos - 2;
|
|
for (;;) {
|
|
if (!de) {
|
|
read_unlock(&proc_subdir_lock);
|
|
return 0;
|
|
}
|
|
if (!i)
|
|
break;
|
|
de = pde_subdir_next(de);
|
|
i--;
|
|
}
|
|
|
|
do {
|
|
struct proc_dir_entry *next;
|
|
pde_get(de);
|
|
read_unlock(&proc_subdir_lock);
|
|
if (!dir_emit(ctx, de->name, de->namelen,
|
|
de->low_ino, de->mode >> 12)) {
|
|
pde_put(de);
|
|
return 0;
|
|
}
|
|
read_lock(&proc_subdir_lock);
|
|
ctx->pos++;
|
|
next = pde_subdir_next(de);
|
|
pde_put(de);
|
|
de = next;
|
|
} while (de);
|
|
read_unlock(&proc_subdir_lock);
|
|
return 1;
|
|
}
|
|
|
|
int proc_readdir(struct file *file, struct dir_context *ctx)
|
|
{
|
|
struct inode *inode = file_inode(file);
|
|
|
|
return proc_readdir_de(PDE(inode), file, ctx);
|
|
}
|
|
|
|
/*
|
|
* These are the generic /proc directory operations. They
|
|
* use the in-memory "struct proc_dir_entry" tree to parse
|
|
* the /proc directory.
|
|
*/
|
|
static const struct file_operations proc_dir_operations = {
|
|
.llseek = generic_file_llseek,
|
|
.read = generic_read_dir,
|
|
.iterate_shared = proc_readdir,
|
|
};
|
|
|
|
/*
|
|
* proc directories can do almost nothing..
|
|
*/
|
|
static const struct inode_operations proc_dir_inode_operations = {
|
|
.lookup = proc_lookup,
|
|
.getattr = proc_getattr,
|
|
.setattr = proc_notify_change,
|
|
};
|
|
|
|
static int proc_register(struct proc_dir_entry * dir, struct proc_dir_entry * dp)
|
|
{
|
|
int ret;
|
|
|
|
ret = proc_alloc_inum(&dp->low_ino);
|
|
if (ret)
|
|
return ret;
|
|
|
|
write_lock(&proc_subdir_lock);
|
|
dp->parent = dir;
|
|
if (pde_subdir_insert(dir, dp) == false) {
|
|
WARN(1, "proc_dir_entry '%s/%s' already registered\n",
|
|
dir->name, dp->name);
|
|
write_unlock(&proc_subdir_lock);
|
|
proc_free_inum(dp->low_ino);
|
|
return -EEXIST;
|
|
}
|
|
write_unlock(&proc_subdir_lock);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static struct proc_dir_entry *__proc_create(struct proc_dir_entry **parent,
|
|
const char *name,
|
|
umode_t mode,
|
|
nlink_t nlink)
|
|
{
|
|
struct proc_dir_entry *ent = NULL;
|
|
const char *fn;
|
|
struct qstr qstr;
|
|
|
|
if (xlate_proc_name(name, parent, &fn) != 0)
|
|
goto out;
|
|
qstr.name = fn;
|
|
qstr.len = strlen(fn);
|
|
if (qstr.len == 0 || qstr.len >= 256) {
|
|
WARN(1, "name len %u\n", qstr.len);
|
|
return NULL;
|
|
}
|
|
if (*parent == &proc_root && name_to_int(&qstr) != ~0U) {
|
|
WARN(1, "create '/proc/%s' by hand\n", qstr.name);
|
|
return NULL;
|
|
}
|
|
if (is_empty_pde(*parent)) {
|
|
WARN(1, "attempt to add to permanently empty directory");
|
|
return NULL;
|
|
}
|
|
|
|
ent = kzalloc(sizeof(struct proc_dir_entry) + qstr.len + 1, GFP_KERNEL);
|
|
if (!ent)
|
|
goto out;
|
|
|
|
memcpy(ent->name, fn, qstr.len + 1);
|
|
ent->namelen = qstr.len;
|
|
ent->mode = mode;
|
|
ent->nlink = nlink;
|
|
ent->subdir = RB_ROOT;
|
|
atomic_set(&ent->count, 1);
|
|
spin_lock_init(&ent->pde_unload_lock);
|
|
INIT_LIST_HEAD(&ent->pde_openers);
|
|
proc_set_user(ent, (*parent)->uid, (*parent)->gid);
|
|
|
|
out:
|
|
return ent;
|
|
}
|
|
|
|
struct proc_dir_entry *proc_symlink(const char *name,
|
|
struct proc_dir_entry *parent, const char *dest)
|
|
{
|
|
struct proc_dir_entry *ent;
|
|
|
|
ent = __proc_create(&parent, name,
|
|
(S_IFLNK | S_IRUGO | S_IWUGO | S_IXUGO),1);
|
|
|
|
if (ent) {
|
|
ent->data = kmalloc((ent->size=strlen(dest))+1, GFP_KERNEL);
|
|
if (ent->data) {
|
|
strcpy((char*)ent->data,dest);
|
|
ent->proc_iops = &proc_link_inode_operations;
|
|
if (proc_register(parent, ent) < 0) {
|
|
kfree(ent->data);
|
|
kfree(ent);
|
|
ent = NULL;
|
|
}
|
|
} else {
|
|
kfree(ent);
|
|
ent = NULL;
|
|
}
|
|
}
|
|
return ent;
|
|
}
|
|
EXPORT_SYMBOL(proc_symlink);
|
|
|
|
struct proc_dir_entry *proc_mkdir_data(const char *name, umode_t mode,
|
|
struct proc_dir_entry *parent, void *data)
|
|
{
|
|
struct proc_dir_entry *ent;
|
|
|
|
if (mode == 0)
|
|
mode = S_IRUGO | S_IXUGO;
|
|
|
|
ent = __proc_create(&parent, name, S_IFDIR | mode, 2);
|
|
if (ent) {
|
|
ent->data = data;
|
|
ent->proc_fops = &proc_dir_operations;
|
|
ent->proc_iops = &proc_dir_inode_operations;
|
|
parent->nlink++;
|
|
if (proc_register(parent, ent) < 0) {
|
|
kfree(ent);
|
|
parent->nlink--;
|
|
ent = NULL;
|
|
}
|
|
}
|
|
return ent;
|
|
}
|
|
EXPORT_SYMBOL_GPL(proc_mkdir_data);
|
|
|
|
struct proc_dir_entry *proc_mkdir_mode(const char *name, umode_t mode,
|
|
struct proc_dir_entry *parent)
|
|
{
|
|
return proc_mkdir_data(name, mode, parent, NULL);
|
|
}
|
|
EXPORT_SYMBOL(proc_mkdir_mode);
|
|
|
|
struct proc_dir_entry *proc_mkdir(const char *name,
|
|
struct proc_dir_entry *parent)
|
|
{
|
|
return proc_mkdir_data(name, 0, parent, NULL);
|
|
}
|
|
EXPORT_SYMBOL(proc_mkdir);
|
|
|
|
struct proc_dir_entry *proc_create_mount_point(const char *name)
|
|
{
|
|
umode_t mode = S_IFDIR | S_IRUGO | S_IXUGO;
|
|
struct proc_dir_entry *ent, *parent = NULL;
|
|
|
|
ent = __proc_create(&parent, name, mode, 2);
|
|
if (ent) {
|
|
ent->data = NULL;
|
|
ent->proc_fops = NULL;
|
|
ent->proc_iops = NULL;
|
|
if (proc_register(parent, ent) < 0) {
|
|
kfree(ent);
|
|
parent->nlink--;
|
|
ent = NULL;
|
|
}
|
|
}
|
|
return ent;
|
|
}
|
|
EXPORT_SYMBOL(proc_create_mount_point);
|
|
|
|
struct proc_dir_entry *proc_create_data(const char *name, umode_t mode,
|
|
struct proc_dir_entry *parent,
|
|
const struct file_operations *proc_fops,
|
|
void *data)
|
|
{
|
|
struct proc_dir_entry *pde;
|
|
if ((mode & S_IFMT) == 0)
|
|
mode |= S_IFREG;
|
|
|
|
if (!S_ISREG(mode)) {
|
|
WARN_ON(1); /* use proc_mkdir() */
|
|
return NULL;
|
|
}
|
|
|
|
BUG_ON(proc_fops == NULL);
|
|
|
|
if ((mode & S_IALLUGO) == 0)
|
|
mode |= S_IRUGO;
|
|
pde = __proc_create(&parent, name, mode, 1);
|
|
if (!pde)
|
|
goto out;
|
|
pde->proc_fops = proc_fops;
|
|
pde->data = data;
|
|
pde->proc_iops = &proc_file_inode_operations;
|
|
if (proc_register(parent, pde) < 0)
|
|
goto out_free;
|
|
return pde;
|
|
out_free:
|
|
kfree(pde);
|
|
out:
|
|
return NULL;
|
|
}
|
|
EXPORT_SYMBOL(proc_create_data);
|
|
|
|
void proc_set_size(struct proc_dir_entry *de, loff_t size)
|
|
{
|
|
de->size = size;
|
|
}
|
|
EXPORT_SYMBOL(proc_set_size);
|
|
|
|
void proc_set_user(struct proc_dir_entry *de, kuid_t uid, kgid_t gid)
|
|
{
|
|
de->uid = uid;
|
|
de->gid = gid;
|
|
}
|
|
EXPORT_SYMBOL(proc_set_user);
|
|
|
|
static void free_proc_entry(struct proc_dir_entry *de)
|
|
{
|
|
proc_free_inum(de->low_ino);
|
|
|
|
if (S_ISLNK(de->mode))
|
|
kfree(de->data);
|
|
kfree(de);
|
|
}
|
|
|
|
void pde_put(struct proc_dir_entry *pde)
|
|
{
|
|
if (atomic_dec_and_test(&pde->count))
|
|
free_proc_entry(pde);
|
|
}
|
|
|
|
/*
|
|
* Remove a /proc entry and free it if it's not currently in use.
|
|
*/
|
|
void remove_proc_entry(const char *name, struct proc_dir_entry *parent)
|
|
{
|
|
struct proc_dir_entry *de = NULL;
|
|
const char *fn = name;
|
|
unsigned int len;
|
|
|
|
write_lock(&proc_subdir_lock);
|
|
if (__xlate_proc_name(name, &parent, &fn) != 0) {
|
|
write_unlock(&proc_subdir_lock);
|
|
return;
|
|
}
|
|
len = strlen(fn);
|
|
|
|
de = pde_subdir_find(parent, fn, len);
|
|
if (de)
|
|
rb_erase(&de->subdir_node, &parent->subdir);
|
|
write_unlock(&proc_subdir_lock);
|
|
if (!de) {
|
|
WARN(1, "name '%s'\n", name);
|
|
return;
|
|
}
|
|
|
|
proc_entry_rundown(de);
|
|
|
|
if (S_ISDIR(de->mode))
|
|
parent->nlink--;
|
|
de->nlink = 0;
|
|
WARN(pde_subdir_first(de),
|
|
"%s: removing non-empty directory '%s/%s', leaking at least '%s'\n",
|
|
__func__, de->parent->name, de->name, pde_subdir_first(de)->name);
|
|
pde_put(de);
|
|
}
|
|
EXPORT_SYMBOL(remove_proc_entry);
|
|
|
|
int remove_proc_subtree(const char *name, struct proc_dir_entry *parent)
|
|
{
|
|
struct proc_dir_entry *root = NULL, *de, *next;
|
|
const char *fn = name;
|
|
unsigned int len;
|
|
|
|
write_lock(&proc_subdir_lock);
|
|
if (__xlate_proc_name(name, &parent, &fn) != 0) {
|
|
write_unlock(&proc_subdir_lock);
|
|
return -ENOENT;
|
|
}
|
|
len = strlen(fn);
|
|
|
|
root = pde_subdir_find(parent, fn, len);
|
|
if (!root) {
|
|
write_unlock(&proc_subdir_lock);
|
|
return -ENOENT;
|
|
}
|
|
rb_erase(&root->subdir_node, &parent->subdir);
|
|
|
|
de = root;
|
|
while (1) {
|
|
next = pde_subdir_first(de);
|
|
if (next) {
|
|
rb_erase(&next->subdir_node, &de->subdir);
|
|
de = next;
|
|
continue;
|
|
}
|
|
write_unlock(&proc_subdir_lock);
|
|
|
|
proc_entry_rundown(de);
|
|
next = de->parent;
|
|
if (S_ISDIR(de->mode))
|
|
next->nlink--;
|
|
de->nlink = 0;
|
|
if (de == root)
|
|
break;
|
|
pde_put(de);
|
|
|
|
write_lock(&proc_subdir_lock);
|
|
de = next;
|
|
}
|
|
pde_put(root);
|
|
return 0;
|
|
}
|
|
EXPORT_SYMBOL(remove_proc_subtree);
|
|
|
|
void *proc_get_parent_data(const struct inode *inode)
|
|
{
|
|
struct proc_dir_entry *de = PDE(inode);
|
|
return de->parent->data;
|
|
}
|
|
EXPORT_SYMBOL_GPL(proc_get_parent_data);
|
|
|
|
void proc_remove(struct proc_dir_entry *de)
|
|
{
|
|
if (de)
|
|
remove_proc_subtree(de->name, de->parent);
|
|
}
|
|
EXPORT_SYMBOL(proc_remove);
|
|
|
|
void *PDE_DATA(const struct inode *inode)
|
|
{
|
|
return __PDE_DATA(inode);
|
|
}
|
|
EXPORT_SYMBOL(PDE_DATA);
|