mirror of
https://github.com/AetherDroid/android_kernel_samsung_on5xelte.git
synced 2025-09-08 01:08:03 -04:00
Fixed MTP to work with TWRP
This commit is contained in:
commit
f6dfaef42e
50820 changed files with 20846062 additions and 0 deletions
7
fs/kernfs/Kconfig
Normal file
7
fs/kernfs/Kconfig
Normal file
|
@ -0,0 +1,7 @@
|
|||
#
|
||||
# KERNFS should be selected by its users
|
||||
#
|
||||
|
||||
config KERNFS
|
||||
bool
|
||||
default n
|
5
fs/kernfs/Makefile
Normal file
5
fs/kernfs/Makefile
Normal file
|
@ -0,0 +1,5 @@
|
|||
#
|
||||
# Makefile for the kernfs pseudo filesystem
|
||||
#
|
||||
|
||||
obj-y := mount.o inode.o dir.o file.o symlink.o
|
1421
fs/kernfs/dir.c
Normal file
1421
fs/kernfs/dir.c
Normal file
File diff suppressed because it is too large
Load diff
952
fs/kernfs/file.c
Normal file
952
fs/kernfs/file.c
Normal file
|
@ -0,0 +1,952 @@
|
|||
/*
|
||||
* fs/kernfs/file.c - kernfs file implementation
|
||||
*
|
||||
* Copyright (c) 2001-3 Patrick Mochel
|
||||
* Copyright (c) 2007 SUSE Linux Products GmbH
|
||||
* Copyright (c) 2007, 2013 Tejun Heo <tj@kernel.org>
|
||||
*
|
||||
* This file is released under the GPLv2.
|
||||
*/
|
||||
|
||||
#include <linux/fs.h>
|
||||
#include <linux/seq_file.h>
|
||||
#include <linux/slab.h>
|
||||
#include <linux/poll.h>
|
||||
#include <linux/pagemap.h>
|
||||
#include <linux/sched.h>
|
||||
#include <linux/fsnotify.h>
|
||||
|
||||
#include "kernfs-internal.h"
|
||||
|
||||
/*
|
||||
* There's one kernfs_open_file for each open file and one kernfs_open_node
|
||||
* for each kernfs_node with one or more open files.
|
||||
*
|
||||
* kernfs_node->attr.open points to kernfs_open_node. attr.open is
|
||||
* protected by kernfs_open_node_lock.
|
||||
*
|
||||
* filp->private_data points to seq_file whose ->private points to
|
||||
* kernfs_open_file. kernfs_open_files are chained at
|
||||
* kernfs_open_node->files, which is protected by kernfs_open_file_mutex.
|
||||
*/
|
||||
static DEFINE_SPINLOCK(kernfs_open_node_lock);
|
||||
static DEFINE_MUTEX(kernfs_open_file_mutex);
|
||||
|
||||
struct kernfs_open_node {
|
||||
atomic_t refcnt;
|
||||
atomic_t event;
|
||||
wait_queue_head_t poll;
|
||||
struct list_head files; /* goes through kernfs_open_file.list */
|
||||
};
|
||||
|
||||
/*
|
||||
* kernfs_notify() may be called from any context and bounces notifications
|
||||
* through a work item. To minimize space overhead in kernfs_node, the
|
||||
* pending queue is implemented as a singly linked list of kernfs_nodes.
|
||||
* The list is terminated with the self pointer so that whether a
|
||||
* kernfs_node is on the list or not can be determined by testing the next
|
||||
* pointer for NULL.
|
||||
*/
|
||||
#define KERNFS_NOTIFY_EOL ((void *)&kernfs_notify_list)
|
||||
|
||||
static DEFINE_SPINLOCK(kernfs_notify_lock);
|
||||
static struct kernfs_node *kernfs_notify_list = KERNFS_NOTIFY_EOL;
|
||||
|
||||
static struct kernfs_open_file *kernfs_of(struct file *file)
|
||||
{
|
||||
return ((struct seq_file *)file->private_data)->private;
|
||||
}
|
||||
|
||||
/*
|
||||
* Determine the kernfs_ops for the given kernfs_node. This function must
|
||||
* be called while holding an active reference.
|
||||
*/
|
||||
static const struct kernfs_ops *kernfs_ops(struct kernfs_node *kn)
|
||||
{
|
||||
if (kn->flags & KERNFS_LOCKDEP)
|
||||
lockdep_assert_held(kn);
|
||||
return kn->attr.ops;
|
||||
}
|
||||
|
||||
/*
|
||||
* As kernfs_seq_stop() is also called after kernfs_seq_start() or
|
||||
* kernfs_seq_next() failure, it needs to distinguish whether it's stopping
|
||||
* a seq_file iteration which is fully initialized with an active reference
|
||||
* or an aborted kernfs_seq_start() due to get_active failure. The
|
||||
* position pointer is the only context for each seq_file iteration and
|
||||
* thus the stop condition should be encoded in it. As the return value is
|
||||
* directly visible to userland, ERR_PTR(-ENODEV) is the only acceptable
|
||||
* choice to indicate get_active failure.
|
||||
*
|
||||
* Unfortunately, this is complicated due to the optional custom seq_file
|
||||
* operations which may return ERR_PTR(-ENODEV) too. kernfs_seq_stop()
|
||||
* can't distinguish whether ERR_PTR(-ENODEV) is from get_active failure or
|
||||
* custom seq_file operations and thus can't decide whether put_active
|
||||
* should be performed or not only on ERR_PTR(-ENODEV).
|
||||
*
|
||||
* This is worked around by factoring out the custom seq_stop() and
|
||||
* put_active part into kernfs_seq_stop_active(), skipping it from
|
||||
* kernfs_seq_stop() if ERR_PTR(-ENODEV) while invoking it directly after
|
||||
* custom seq_file operations fail with ERR_PTR(-ENODEV) - this ensures
|
||||
* that kernfs_seq_stop_active() is skipped only after get_active failure.
|
||||
*/
|
||||
static void kernfs_seq_stop_active(struct seq_file *sf, void *v)
|
||||
{
|
||||
struct kernfs_open_file *of = sf->private;
|
||||
const struct kernfs_ops *ops = kernfs_ops(of->kn);
|
||||
|
||||
if (ops->seq_stop)
|
||||
ops->seq_stop(sf, v);
|
||||
kernfs_put_active(of->kn);
|
||||
}
|
||||
|
||||
static void *kernfs_seq_start(struct seq_file *sf, loff_t *ppos)
|
||||
{
|
||||
struct kernfs_open_file *of = sf->private;
|
||||
const struct kernfs_ops *ops;
|
||||
|
||||
/*
|
||||
* @of->mutex nests outside active ref and is just to ensure that
|
||||
* the ops aren't called concurrently for the same open file.
|
||||
*/
|
||||
mutex_lock(&of->mutex);
|
||||
if (!kernfs_get_active(of->kn))
|
||||
return ERR_PTR(-ENODEV);
|
||||
|
||||
ops = kernfs_ops(of->kn);
|
||||
if (ops->seq_start) {
|
||||
void *next = ops->seq_start(sf, ppos);
|
||||
/* see the comment above kernfs_seq_stop_active() */
|
||||
if (next == ERR_PTR(-ENODEV))
|
||||
kernfs_seq_stop_active(sf, next);
|
||||
return next;
|
||||
} else {
|
||||
/*
|
||||
* The same behavior and code as single_open(). Returns
|
||||
* !NULL if pos is at the beginning; otherwise, NULL.
|
||||
*/
|
||||
return NULL + !*ppos;
|
||||
}
|
||||
}
|
||||
|
||||
static void *kernfs_seq_next(struct seq_file *sf, void *v, loff_t *ppos)
|
||||
{
|
||||
struct kernfs_open_file *of = sf->private;
|
||||
const struct kernfs_ops *ops = kernfs_ops(of->kn);
|
||||
|
||||
if (ops->seq_next) {
|
||||
void *next = ops->seq_next(sf, v, ppos);
|
||||
/* see the comment above kernfs_seq_stop_active() */
|
||||
if (next == ERR_PTR(-ENODEV))
|
||||
kernfs_seq_stop_active(sf, next);
|
||||
return next;
|
||||
} else {
|
||||
/*
|
||||
* The same behavior and code as single_open(), always
|
||||
* terminate after the initial read.
|
||||
*/
|
||||
++*ppos;
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
static void kernfs_seq_stop(struct seq_file *sf, void *v)
|
||||
{
|
||||
struct kernfs_open_file *of = sf->private;
|
||||
|
||||
if (v != ERR_PTR(-ENODEV))
|
||||
kernfs_seq_stop_active(sf, v);
|
||||
mutex_unlock(&of->mutex);
|
||||
}
|
||||
|
||||
static int kernfs_seq_show(struct seq_file *sf, void *v)
|
||||
{
|
||||
struct kernfs_open_file *of = sf->private;
|
||||
|
||||
of->event = atomic_read(&of->kn->attr.open->event);
|
||||
|
||||
return of->kn->attr.ops->seq_show(sf, v);
|
||||
}
|
||||
|
||||
static const struct seq_operations kernfs_seq_ops = {
|
||||
.start = kernfs_seq_start,
|
||||
.next = kernfs_seq_next,
|
||||
.stop = kernfs_seq_stop,
|
||||
.show = kernfs_seq_show,
|
||||
};
|
||||
|
||||
/*
|
||||
* As reading a bin file can have side-effects, the exact offset and bytes
|
||||
* specified in read(2) call should be passed to the read callback making
|
||||
* it difficult to use seq_file. Implement simplistic custom buffering for
|
||||
* bin files.
|
||||
*/
|
||||
static ssize_t kernfs_file_direct_read(struct kernfs_open_file *of,
|
||||
char __user *user_buf, size_t count,
|
||||
loff_t *ppos)
|
||||
{
|
||||
ssize_t len = min_t(size_t, count, PAGE_SIZE);
|
||||
const struct kernfs_ops *ops;
|
||||
char *buf;
|
||||
|
||||
buf = kmalloc(len, GFP_KERNEL);
|
||||
if (!buf)
|
||||
return -ENOMEM;
|
||||
|
||||
/*
|
||||
* @of->mutex nests outside active ref and is just to ensure that
|
||||
* the ops aren't called concurrently for the same open file.
|
||||
*/
|
||||
mutex_lock(&of->mutex);
|
||||
if (!kernfs_get_active(of->kn)) {
|
||||
len = -ENODEV;
|
||||
mutex_unlock(&of->mutex);
|
||||
goto out_free;
|
||||
}
|
||||
|
||||
ops = kernfs_ops(of->kn);
|
||||
if (ops->read)
|
||||
len = ops->read(of, buf, len, *ppos);
|
||||
else
|
||||
len = -EINVAL;
|
||||
|
||||
kernfs_put_active(of->kn);
|
||||
mutex_unlock(&of->mutex);
|
||||
|
||||
if (len < 0)
|
||||
goto out_free;
|
||||
|
||||
if (copy_to_user(user_buf, buf, len)) {
|
||||
len = -EFAULT;
|
||||
goto out_free;
|
||||
}
|
||||
|
||||
*ppos += len;
|
||||
|
||||
out_free:
|
||||
kfree(buf);
|
||||
return len;
|
||||
}
|
||||
|
||||
/**
|
||||
* kernfs_fop_read - kernfs vfs read callback
|
||||
* @file: file pointer
|
||||
* @user_buf: data to write
|
||||
* @count: number of bytes
|
||||
* @ppos: starting offset
|
||||
*/
|
||||
static ssize_t kernfs_fop_read(struct file *file, char __user *user_buf,
|
||||
size_t count, loff_t *ppos)
|
||||
{
|
||||
struct kernfs_open_file *of = kernfs_of(file);
|
||||
|
||||
if (of->kn->flags & KERNFS_HAS_SEQ_SHOW)
|
||||
return seq_read(file, user_buf, count, ppos);
|
||||
else
|
||||
return kernfs_file_direct_read(of, user_buf, count, ppos);
|
||||
}
|
||||
|
||||
/**
|
||||
* kernfs_fop_write - kernfs vfs write callback
|
||||
* @file: file pointer
|
||||
* @user_buf: data to write
|
||||
* @count: number of bytes
|
||||
* @ppos: starting offset
|
||||
*
|
||||
* Copy data in from userland and pass it to the matching kernfs write
|
||||
* operation.
|
||||
*
|
||||
* There is no easy way for us to know if userspace is only doing a partial
|
||||
* write, so we don't support them. We expect the entire buffer to come on
|
||||
* the first write. Hint: if you're writing a value, first read the file,
|
||||
* modify only the the value you're changing, then write entire buffer
|
||||
* back.
|
||||
*/
|
||||
static ssize_t kernfs_fop_write(struct file *file, const char __user *user_buf,
|
||||
size_t count, loff_t *ppos)
|
||||
{
|
||||
struct kernfs_open_file *of = kernfs_of(file);
|
||||
const struct kernfs_ops *ops;
|
||||
size_t len;
|
||||
char *buf;
|
||||
|
||||
if (of->atomic_write_len) {
|
||||
len = count;
|
||||
if (len > of->atomic_write_len)
|
||||
return -E2BIG;
|
||||
} else {
|
||||
len = min_t(size_t, count, PAGE_SIZE);
|
||||
}
|
||||
|
||||
buf = kmalloc(len + 1, GFP_KERNEL);
|
||||
if (!buf)
|
||||
return -ENOMEM;
|
||||
|
||||
if (copy_from_user(buf, user_buf, len)) {
|
||||
len = -EFAULT;
|
||||
goto out_free;
|
||||
}
|
||||
buf[len] = '\0'; /* guarantee string termination */
|
||||
|
||||
/*
|
||||
* @of->mutex nests outside active ref and is just to ensure that
|
||||
* the ops aren't called concurrently for the same open file.
|
||||
*/
|
||||
mutex_lock(&of->mutex);
|
||||
if (!kernfs_get_active(of->kn)) {
|
||||
mutex_unlock(&of->mutex);
|
||||
len = -ENODEV;
|
||||
goto out_free;
|
||||
}
|
||||
|
||||
ops = kernfs_ops(of->kn);
|
||||
if (ops->write)
|
||||
len = ops->write(of, buf, len, *ppos);
|
||||
else
|
||||
len = -EINVAL;
|
||||
|
||||
kernfs_put_active(of->kn);
|
||||
mutex_unlock(&of->mutex);
|
||||
|
||||
if (len > 0)
|
||||
*ppos += len;
|
||||
out_free:
|
||||
kfree(buf);
|
||||
return len;
|
||||
}
|
||||
|
||||
static void kernfs_vma_open(struct vm_area_struct *vma)
|
||||
{
|
||||
struct file *file = vma->vm_file;
|
||||
struct kernfs_open_file *of = kernfs_of(file);
|
||||
|
||||
if (!of->vm_ops)
|
||||
return;
|
||||
|
||||
if (!kernfs_get_active(of->kn))
|
||||
return;
|
||||
|
||||
if (of->vm_ops->open)
|
||||
of->vm_ops->open(vma);
|
||||
|
||||
kernfs_put_active(of->kn);
|
||||
}
|
||||
|
||||
static int kernfs_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
|
||||
{
|
||||
struct file *file = vma->vm_file;
|
||||
struct kernfs_open_file *of = kernfs_of(file);
|
||||
int ret;
|
||||
|
||||
if (!of->vm_ops)
|
||||
return VM_FAULT_SIGBUS;
|
||||
|
||||
if (!kernfs_get_active(of->kn))
|
||||
return VM_FAULT_SIGBUS;
|
||||
|
||||
ret = VM_FAULT_SIGBUS;
|
||||
if (of->vm_ops->fault)
|
||||
ret = of->vm_ops->fault(vma, vmf);
|
||||
|
||||
kernfs_put_active(of->kn);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int kernfs_vma_page_mkwrite(struct vm_area_struct *vma,
|
||||
struct vm_fault *vmf)
|
||||
{
|
||||
struct file *file = vma->vm_file;
|
||||
struct kernfs_open_file *of = kernfs_of(file);
|
||||
int ret;
|
||||
|
||||
if (!of->vm_ops)
|
||||
return VM_FAULT_SIGBUS;
|
||||
|
||||
if (!kernfs_get_active(of->kn))
|
||||
return VM_FAULT_SIGBUS;
|
||||
|
||||
ret = 0;
|
||||
if (of->vm_ops->page_mkwrite)
|
||||
ret = of->vm_ops->page_mkwrite(vma, vmf);
|
||||
else
|
||||
file_update_time(file);
|
||||
|
||||
kernfs_put_active(of->kn);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int kernfs_vma_access(struct vm_area_struct *vma, unsigned long addr,
|
||||
void *buf, int len, int write)
|
||||
{
|
||||
struct file *file = vma->vm_file;
|
||||
struct kernfs_open_file *of = kernfs_of(file);
|
||||
int ret;
|
||||
|
||||
if (!of->vm_ops)
|
||||
return -EINVAL;
|
||||
|
||||
if (!kernfs_get_active(of->kn))
|
||||
return -EINVAL;
|
||||
|
||||
ret = -EINVAL;
|
||||
if (of->vm_ops->access)
|
||||
ret = of->vm_ops->access(vma, addr, buf, len, write);
|
||||
|
||||
kernfs_put_active(of->kn);
|
||||
return ret;
|
||||
}
|
||||
|
||||
#ifdef CONFIG_NUMA
|
||||
static int kernfs_vma_set_policy(struct vm_area_struct *vma,
|
||||
struct mempolicy *new)
|
||||
{
|
||||
struct file *file = vma->vm_file;
|
||||
struct kernfs_open_file *of = kernfs_of(file);
|
||||
int ret;
|
||||
|
||||
if (!of->vm_ops)
|
||||
return 0;
|
||||
|
||||
if (!kernfs_get_active(of->kn))
|
||||
return -EINVAL;
|
||||
|
||||
ret = 0;
|
||||
if (of->vm_ops->set_policy)
|
||||
ret = of->vm_ops->set_policy(vma, new);
|
||||
|
||||
kernfs_put_active(of->kn);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static struct mempolicy *kernfs_vma_get_policy(struct vm_area_struct *vma,
|
||||
unsigned long addr)
|
||||
{
|
||||
struct file *file = vma->vm_file;
|
||||
struct kernfs_open_file *of = kernfs_of(file);
|
||||
struct mempolicy *pol;
|
||||
|
||||
if (!of->vm_ops)
|
||||
return vma->vm_policy;
|
||||
|
||||
if (!kernfs_get_active(of->kn))
|
||||
return vma->vm_policy;
|
||||
|
||||
pol = vma->vm_policy;
|
||||
if (of->vm_ops->get_policy)
|
||||
pol = of->vm_ops->get_policy(vma, addr);
|
||||
|
||||
kernfs_put_active(of->kn);
|
||||
return pol;
|
||||
}
|
||||
|
||||
static int kernfs_vma_migrate(struct vm_area_struct *vma,
|
||||
const nodemask_t *from, const nodemask_t *to,
|
||||
unsigned long flags)
|
||||
{
|
||||
struct file *file = vma->vm_file;
|
||||
struct kernfs_open_file *of = kernfs_of(file);
|
||||
int ret;
|
||||
|
||||
if (!of->vm_ops)
|
||||
return 0;
|
||||
|
||||
if (!kernfs_get_active(of->kn))
|
||||
return 0;
|
||||
|
||||
ret = 0;
|
||||
if (of->vm_ops->migrate)
|
||||
ret = of->vm_ops->migrate(vma, from, to, flags);
|
||||
|
||||
kernfs_put_active(of->kn);
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
|
||||
static const struct vm_operations_struct kernfs_vm_ops = {
|
||||
.open = kernfs_vma_open,
|
||||
.fault = kernfs_vma_fault,
|
||||
.page_mkwrite = kernfs_vma_page_mkwrite,
|
||||
.access = kernfs_vma_access,
|
||||
#ifdef CONFIG_NUMA
|
||||
.set_policy = kernfs_vma_set_policy,
|
||||
.get_policy = kernfs_vma_get_policy,
|
||||
.migrate = kernfs_vma_migrate,
|
||||
#endif
|
||||
};
|
||||
|
||||
static int kernfs_fop_mmap(struct file *file, struct vm_area_struct *vma)
|
||||
{
|
||||
struct kernfs_open_file *of = kernfs_of(file);
|
||||
const struct kernfs_ops *ops;
|
||||
int rc;
|
||||
|
||||
/*
|
||||
* mmap path and of->mutex are prone to triggering spurious lockdep
|
||||
* warnings and we don't want to add spurious locking dependency
|
||||
* between the two. Check whether mmap is actually implemented
|
||||
* without grabbing @of->mutex by testing HAS_MMAP flag. See the
|
||||
* comment in kernfs_file_open() for more details.
|
||||
*/
|
||||
if (!(of->kn->flags & KERNFS_HAS_MMAP))
|
||||
return -ENODEV;
|
||||
|
||||
mutex_lock(&of->mutex);
|
||||
|
||||
rc = -ENODEV;
|
||||
if (!kernfs_get_active(of->kn))
|
||||
goto out_unlock;
|
||||
|
||||
ops = kernfs_ops(of->kn);
|
||||
rc = ops->mmap(of, vma);
|
||||
if (rc)
|
||||
goto out_put;
|
||||
|
||||
/*
|
||||
* PowerPC's pci_mmap of legacy_mem uses shmem_zero_setup()
|
||||
* to satisfy versions of X which crash if the mmap fails: that
|
||||
* substitutes a new vm_file, and we don't then want bin_vm_ops.
|
||||
*/
|
||||
if (vma->vm_file != file)
|
||||
goto out_put;
|
||||
|
||||
rc = -EINVAL;
|
||||
if (of->mmapped && of->vm_ops != vma->vm_ops)
|
||||
goto out_put;
|
||||
|
||||
/*
|
||||
* It is not possible to successfully wrap close.
|
||||
* So error if someone is trying to use close.
|
||||
*/
|
||||
rc = -EINVAL;
|
||||
if (vma->vm_ops && vma->vm_ops->close)
|
||||
goto out_put;
|
||||
|
||||
rc = 0;
|
||||
of->mmapped = 1;
|
||||
of->vm_ops = vma->vm_ops;
|
||||
vma->vm_ops = &kernfs_vm_ops;
|
||||
out_put:
|
||||
kernfs_put_active(of->kn);
|
||||
out_unlock:
|
||||
mutex_unlock(&of->mutex);
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
/**
|
||||
* kernfs_get_open_node - get or create kernfs_open_node
|
||||
* @kn: target kernfs_node
|
||||
* @of: kernfs_open_file for this instance of open
|
||||
*
|
||||
* If @kn->attr.open exists, increment its reference count; otherwise,
|
||||
* create one. @of is chained to the files list.
|
||||
*
|
||||
* LOCKING:
|
||||
* Kernel thread context (may sleep).
|
||||
*
|
||||
* RETURNS:
|
||||
* 0 on success, -errno on failure.
|
||||
*/
|
||||
static int kernfs_get_open_node(struct kernfs_node *kn,
|
||||
struct kernfs_open_file *of)
|
||||
{
|
||||
struct kernfs_open_node *on, *new_on = NULL;
|
||||
|
||||
retry:
|
||||
mutex_lock(&kernfs_open_file_mutex);
|
||||
spin_lock_irq(&kernfs_open_node_lock);
|
||||
|
||||
if (!kn->attr.open && new_on) {
|
||||
kn->attr.open = new_on;
|
||||
new_on = NULL;
|
||||
}
|
||||
|
||||
on = kn->attr.open;
|
||||
if (on) {
|
||||
atomic_inc(&on->refcnt);
|
||||
list_add_tail(&of->list, &on->files);
|
||||
}
|
||||
|
||||
spin_unlock_irq(&kernfs_open_node_lock);
|
||||
mutex_unlock(&kernfs_open_file_mutex);
|
||||
|
||||
if (on) {
|
||||
kfree(new_on);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* not there, initialize a new one and retry */
|
||||
new_on = kmalloc(sizeof(*new_on), GFP_KERNEL);
|
||||
if (!new_on)
|
||||
return -ENOMEM;
|
||||
|
||||
atomic_set(&new_on->refcnt, 0);
|
||||
atomic_set(&new_on->event, 1);
|
||||
init_waitqueue_head(&new_on->poll);
|
||||
INIT_LIST_HEAD(&new_on->files);
|
||||
goto retry;
|
||||
}
|
||||
|
||||
/**
|
||||
* kernfs_put_open_node - put kernfs_open_node
|
||||
* @kn: target kernfs_nodet
|
||||
* @of: associated kernfs_open_file
|
||||
*
|
||||
* Put @kn->attr.open and unlink @of from the files list. If
|
||||
* reference count reaches zero, disassociate and free it.
|
||||
*
|
||||
* LOCKING:
|
||||
* None.
|
||||
*/
|
||||
static void kernfs_put_open_node(struct kernfs_node *kn,
|
||||
struct kernfs_open_file *of)
|
||||
{
|
||||
struct kernfs_open_node *on = kn->attr.open;
|
||||
unsigned long flags;
|
||||
|
||||
mutex_lock(&kernfs_open_file_mutex);
|
||||
spin_lock_irqsave(&kernfs_open_node_lock, flags);
|
||||
|
||||
if (of)
|
||||
list_del(&of->list);
|
||||
|
||||
if (atomic_dec_and_test(&on->refcnt))
|
||||
kn->attr.open = NULL;
|
||||
else
|
||||
on = NULL;
|
||||
|
||||
spin_unlock_irqrestore(&kernfs_open_node_lock, flags);
|
||||
mutex_unlock(&kernfs_open_file_mutex);
|
||||
|
||||
kfree(on);
|
||||
}
|
||||
|
||||
static int kernfs_fop_open(struct inode *inode, struct file *file)
|
||||
{
|
||||
struct kernfs_node *kn = file->f_path.dentry->d_fsdata;
|
||||
struct kernfs_root *root = kernfs_root(kn);
|
||||
const struct kernfs_ops *ops;
|
||||
struct kernfs_open_file *of;
|
||||
bool has_read, has_write, has_mmap;
|
||||
int error = -EACCES;
|
||||
|
||||
if (!kernfs_get_active(kn))
|
||||
return -ENODEV;
|
||||
|
||||
ops = kernfs_ops(kn);
|
||||
|
||||
has_read = ops->seq_show || ops->read || ops->mmap;
|
||||
has_write = ops->write || ops->mmap;
|
||||
has_mmap = ops->mmap;
|
||||
|
||||
/* see the flag definition for details */
|
||||
if (root->flags & KERNFS_ROOT_EXTRA_OPEN_PERM_CHECK) {
|
||||
if ((file->f_mode & FMODE_WRITE) &&
|
||||
(!(inode->i_mode & S_IWUGO) || !has_write))
|
||||
goto err_out;
|
||||
|
||||
if ((file->f_mode & FMODE_READ) &&
|
||||
(!(inode->i_mode & S_IRUGO) || !has_read))
|
||||
goto err_out;
|
||||
}
|
||||
|
||||
/* allocate a kernfs_open_file for the file */
|
||||
error = -ENOMEM;
|
||||
of = kzalloc(sizeof(struct kernfs_open_file), GFP_KERNEL);
|
||||
if (!of)
|
||||
goto err_out;
|
||||
|
||||
/*
|
||||
* The following is done to give a different lockdep key to
|
||||
* @of->mutex for files which implement mmap. This is a rather
|
||||
* crude way to avoid false positive lockdep warning around
|
||||
* mm->mmap_sem - mmap nests @of->mutex under mm->mmap_sem and
|
||||
* reading /sys/block/sda/trace/act_mask grabs sr_mutex, under
|
||||
* which mm->mmap_sem nests, while holding @of->mutex. As each
|
||||
* open file has a separate mutex, it's okay as long as those don't
|
||||
* happen on the same file. At this point, we can't easily give
|
||||
* each file a separate locking class. Let's differentiate on
|
||||
* whether the file has mmap or not for now.
|
||||
*
|
||||
* Both paths of the branch look the same. They're supposed to
|
||||
* look that way and give @of->mutex different static lockdep keys.
|
||||
*/
|
||||
if (has_mmap)
|
||||
mutex_init(&of->mutex);
|
||||
else
|
||||
mutex_init(&of->mutex);
|
||||
|
||||
of->kn = kn;
|
||||
of->file = file;
|
||||
|
||||
/*
|
||||
* Write path needs to atomic_write_len outside active reference.
|
||||
* Cache it in open_file. See kernfs_fop_write() for details.
|
||||
*/
|
||||
of->atomic_write_len = ops->atomic_write_len;
|
||||
|
||||
/*
|
||||
* Always instantiate seq_file even if read access doesn't use
|
||||
* seq_file or is not requested. This unifies private data access
|
||||
* and readable regular files are the vast majority anyway.
|
||||
*/
|
||||
if (ops->seq_show)
|
||||
error = seq_open(file, &kernfs_seq_ops);
|
||||
else
|
||||
error = seq_open(file, NULL);
|
||||
if (error)
|
||||
goto err_free;
|
||||
|
||||
((struct seq_file *)file->private_data)->private = of;
|
||||
|
||||
/* seq_file clears PWRITE unconditionally, restore it if WRITE */
|
||||
if (file->f_mode & FMODE_WRITE)
|
||||
file->f_mode |= FMODE_PWRITE;
|
||||
|
||||
/* make sure we have open node struct */
|
||||
error = kernfs_get_open_node(kn, of);
|
||||
if (error)
|
||||
goto err_close;
|
||||
|
||||
/* open succeeded, put active references */
|
||||
kernfs_put_active(kn);
|
||||
return 0;
|
||||
|
||||
err_close:
|
||||
seq_release(inode, file);
|
||||
err_free:
|
||||
kfree(of);
|
||||
err_out:
|
||||
kernfs_put_active(kn);
|
||||
return error;
|
||||
}
|
||||
|
||||
static int kernfs_fop_release(struct inode *inode, struct file *filp)
|
||||
{
|
||||
struct kernfs_node *kn = filp->f_path.dentry->d_fsdata;
|
||||
struct kernfs_open_file *of = kernfs_of(filp);
|
||||
|
||||
kernfs_put_open_node(kn, of);
|
||||
seq_release(inode, filp);
|
||||
kfree(of);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void kernfs_unmap_bin_file(struct kernfs_node *kn)
|
||||
{
|
||||
struct kernfs_open_node *on;
|
||||
struct kernfs_open_file *of;
|
||||
|
||||
if (!(kn->flags & KERNFS_HAS_MMAP))
|
||||
return;
|
||||
|
||||
spin_lock_irq(&kernfs_open_node_lock);
|
||||
on = kn->attr.open;
|
||||
if (on)
|
||||
atomic_inc(&on->refcnt);
|
||||
spin_unlock_irq(&kernfs_open_node_lock);
|
||||
if (!on)
|
||||
return;
|
||||
|
||||
mutex_lock(&kernfs_open_file_mutex);
|
||||
list_for_each_entry(of, &on->files, list) {
|
||||
struct inode *inode = file_inode(of->file);
|
||||
unmap_mapping_range(inode->i_mapping, 0, 0, 1);
|
||||
}
|
||||
mutex_unlock(&kernfs_open_file_mutex);
|
||||
|
||||
kernfs_put_open_node(kn, NULL);
|
||||
}
|
||||
|
||||
/*
|
||||
* Kernfs attribute files are pollable. The idea is that you read
|
||||
* the content and then you use 'poll' or 'select' to wait for
|
||||
* the content to change. When the content changes (assuming the
|
||||
* manager for the kobject supports notification), poll will
|
||||
* return POLLERR|POLLPRI, and select will return the fd whether
|
||||
* it is waiting for read, write, or exceptions.
|
||||
* Once poll/select indicates that the value has changed, you
|
||||
* need to close and re-open the file, or seek to 0 and read again.
|
||||
* Reminder: this only works for attributes which actively support
|
||||
* it, and it is not possible to test an attribute from userspace
|
||||
* to see if it supports poll (Neither 'poll' nor 'select' return
|
||||
* an appropriate error code). When in doubt, set a suitable timeout value.
|
||||
*/
|
||||
static unsigned int kernfs_fop_poll(struct file *filp, poll_table *wait)
|
||||
{
|
||||
struct kernfs_open_file *of = kernfs_of(filp);
|
||||
struct kernfs_node *kn = filp->f_path.dentry->d_fsdata;
|
||||
struct kernfs_open_node *on = kn->attr.open;
|
||||
|
||||
/* need parent for the kobj, grab both */
|
||||
if (!kernfs_get_active(kn))
|
||||
goto trigger;
|
||||
|
||||
poll_wait(filp, &on->poll, wait);
|
||||
|
||||
kernfs_put_active(kn);
|
||||
|
||||
if (of->event != atomic_read(&on->event))
|
||||
goto trigger;
|
||||
|
||||
return DEFAULT_POLLMASK;
|
||||
|
||||
trigger:
|
||||
return DEFAULT_POLLMASK|POLLERR|POLLPRI;
|
||||
}
|
||||
|
||||
static void kernfs_notify_workfn(struct work_struct *work)
|
||||
{
|
||||
struct kernfs_node *kn;
|
||||
struct kernfs_open_node *on;
|
||||
struct kernfs_super_info *info;
|
||||
repeat:
|
||||
/* pop one off the notify_list */
|
||||
spin_lock_irq(&kernfs_notify_lock);
|
||||
kn = kernfs_notify_list;
|
||||
if (kn == KERNFS_NOTIFY_EOL) {
|
||||
spin_unlock_irq(&kernfs_notify_lock);
|
||||
return;
|
||||
}
|
||||
kernfs_notify_list = kn->attr.notify_next;
|
||||
kn->attr.notify_next = NULL;
|
||||
spin_unlock_irq(&kernfs_notify_lock);
|
||||
|
||||
/* kick poll */
|
||||
spin_lock_irq(&kernfs_open_node_lock);
|
||||
|
||||
on = kn->attr.open;
|
||||
if (on) {
|
||||
atomic_inc(&on->event);
|
||||
wake_up_interruptible(&on->poll);
|
||||
}
|
||||
|
||||
spin_unlock_irq(&kernfs_open_node_lock);
|
||||
|
||||
/* kick fsnotify */
|
||||
mutex_lock(&kernfs_mutex);
|
||||
|
||||
list_for_each_entry(info, &kernfs_root(kn)->supers, node) {
|
||||
struct inode *inode;
|
||||
struct dentry *dentry;
|
||||
|
||||
inode = ilookup(info->sb, kn->ino);
|
||||
if (!inode)
|
||||
continue;
|
||||
|
||||
dentry = d_find_any_alias(inode);
|
||||
if (dentry) {
|
||||
fsnotify_parent(NULL, dentry, FS_MODIFY);
|
||||
fsnotify(inode, FS_MODIFY, inode, FSNOTIFY_EVENT_INODE,
|
||||
NULL, 0);
|
||||
dput(dentry);
|
||||
}
|
||||
|
||||
iput(inode);
|
||||
}
|
||||
|
||||
mutex_unlock(&kernfs_mutex);
|
||||
kernfs_put(kn);
|
||||
goto repeat;
|
||||
}
|
||||
|
||||
/**
|
||||
* kernfs_notify - notify a kernfs file
|
||||
* @kn: file to notify
|
||||
*
|
||||
* Notify @kn such that poll(2) on @kn wakes up. Maybe be called from any
|
||||
* context.
|
||||
*/
|
||||
void kernfs_notify(struct kernfs_node *kn)
|
||||
{
|
||||
static DECLARE_WORK(kernfs_notify_work, kernfs_notify_workfn);
|
||||
unsigned long flags;
|
||||
|
||||
if (WARN_ON(kernfs_type(kn) != KERNFS_FILE))
|
||||
return;
|
||||
|
||||
spin_lock_irqsave(&kernfs_notify_lock, flags);
|
||||
if (!kn->attr.notify_next) {
|
||||
kernfs_get(kn);
|
||||
kn->attr.notify_next = kernfs_notify_list;
|
||||
kernfs_notify_list = kn;
|
||||
schedule_work(&kernfs_notify_work);
|
||||
}
|
||||
spin_unlock_irqrestore(&kernfs_notify_lock, flags);
|
||||
}
|
||||
EXPORT_SYMBOL_GPL(kernfs_notify);
|
||||
|
||||
const struct file_operations kernfs_file_fops = {
|
||||
.read = kernfs_fop_read,
|
||||
.write = kernfs_fop_write,
|
||||
.llseek = generic_file_llseek,
|
||||
.mmap = kernfs_fop_mmap,
|
||||
.open = kernfs_fop_open,
|
||||
.release = kernfs_fop_release,
|
||||
.poll = kernfs_fop_poll,
|
||||
};
|
||||
|
||||
/**
|
||||
* __kernfs_create_file - kernfs internal function to create a file
|
||||
* @parent: directory to create the file in
|
||||
* @name: name of the file
|
||||
* @mode: mode of the file
|
||||
* @size: size of the file
|
||||
* @ops: kernfs operations for the file
|
||||
* @priv: private data for the file
|
||||
* @ns: optional namespace tag of the file
|
||||
* @name_is_static: don't copy file name
|
||||
* @key: lockdep key for the file's active_ref, %NULL to disable lockdep
|
||||
*
|
||||
* Returns the created node on success, ERR_PTR() value on error.
|
||||
*/
|
||||
struct kernfs_node *__kernfs_create_file(struct kernfs_node *parent,
|
||||
const char *name,
|
||||
umode_t mode, loff_t size,
|
||||
const struct kernfs_ops *ops,
|
||||
void *priv, const void *ns,
|
||||
bool name_is_static,
|
||||
struct lock_class_key *key)
|
||||
{
|
||||
struct kernfs_node *kn;
|
||||
unsigned flags;
|
||||
int rc;
|
||||
|
||||
flags = KERNFS_FILE;
|
||||
if (name_is_static)
|
||||
flags |= KERNFS_STATIC_NAME;
|
||||
|
||||
kn = kernfs_new_node(parent, name, (mode & S_IALLUGO) | S_IFREG, flags);
|
||||
if (!kn)
|
||||
return ERR_PTR(-ENOMEM);
|
||||
|
||||
kn->attr.ops = ops;
|
||||
kn->attr.size = size;
|
||||
kn->ns = ns;
|
||||
kn->priv = priv;
|
||||
|
||||
#ifdef CONFIG_DEBUG_LOCK_ALLOC
|
||||
if (key) {
|
||||
lockdep_init_map(&kn->dep_map, "s_active", key, 0);
|
||||
kn->flags |= KERNFS_LOCKDEP;
|
||||
}
|
||||
#endif
|
||||
|
||||
/*
|
||||
* kn->attr.ops is accesible only while holding active ref. We
|
||||
* need to know whether some ops are implemented outside active
|
||||
* ref. Cache their existence in flags.
|
||||
*/
|
||||
if (ops->seq_show)
|
||||
kn->flags |= KERNFS_HAS_SEQ_SHOW;
|
||||
if (ops->mmap)
|
||||
kn->flags |= KERNFS_HAS_MMAP;
|
||||
|
||||
rc = kernfs_add_one(kn);
|
||||
if (rc) {
|
||||
kernfs_put(kn);
|
||||
return ERR_PTR(rc);
|
||||
}
|
||||
return kn;
|
||||
}
|
383
fs/kernfs/inode.c
Normal file
383
fs/kernfs/inode.c
Normal file
|
@ -0,0 +1,383 @@
|
|||
/*
|
||||
* fs/kernfs/inode.c - kernfs inode implementation
|
||||
*
|
||||
* Copyright (c) 2001-3 Patrick Mochel
|
||||
* Copyright (c) 2007 SUSE Linux Products GmbH
|
||||
* Copyright (c) 2007, 2013 Tejun Heo <tj@kernel.org>
|
||||
*
|
||||
* This file is released under the GPLv2.
|
||||
*/
|
||||
|
||||
#include <linux/pagemap.h>
|
||||
#include <linux/backing-dev.h>
|
||||
#include <linux/capability.h>
|
||||
#include <linux/errno.h>
|
||||
#include <linux/slab.h>
|
||||
#include <linux/xattr.h>
|
||||
#include <linux/security.h>
|
||||
|
||||
#include "kernfs-internal.h"
|
||||
|
||||
static const struct address_space_operations kernfs_aops = {
|
||||
.readpage = simple_readpage,
|
||||
.write_begin = simple_write_begin,
|
||||
.write_end = simple_write_end,
|
||||
};
|
||||
|
||||
static struct backing_dev_info kernfs_bdi = {
|
||||
.name = "kernfs",
|
||||
.ra_pages = 0, /* No readahead */
|
||||
.capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
|
||||
};
|
||||
|
||||
static const struct inode_operations kernfs_iops = {
|
||||
.permission = kernfs_iop_permission,
|
||||
.setattr = kernfs_iop_setattr,
|
||||
.getattr = kernfs_iop_getattr,
|
||||
.setxattr = kernfs_iop_setxattr,
|
||||
.removexattr = kernfs_iop_removexattr,
|
||||
.getxattr = kernfs_iop_getxattr,
|
||||
.listxattr = kernfs_iop_listxattr,
|
||||
};
|
||||
|
||||
void __init kernfs_inode_init(void)
|
||||
{
|
||||
if (bdi_init(&kernfs_bdi))
|
||||
panic("failed to init kernfs_bdi");
|
||||
}
|
||||
|
||||
static struct kernfs_iattrs *kernfs_iattrs(struct kernfs_node *kn)
|
||||
{
|
||||
static DEFINE_MUTEX(iattr_mutex);
|
||||
struct kernfs_iattrs *ret;
|
||||
struct iattr *iattrs;
|
||||
|
||||
mutex_lock(&iattr_mutex);
|
||||
|
||||
if (kn->iattr)
|
||||
goto out_unlock;
|
||||
|
||||
kn->iattr = kzalloc(sizeof(struct kernfs_iattrs), GFP_KERNEL);
|
||||
if (!kn->iattr)
|
||||
goto out_unlock;
|
||||
iattrs = &kn->iattr->ia_iattr;
|
||||
|
||||
/* assign default attributes */
|
||||
iattrs->ia_mode = kn->mode;
|
||||
iattrs->ia_uid = GLOBAL_ROOT_UID;
|
||||
iattrs->ia_gid = GLOBAL_ROOT_GID;
|
||||
iattrs->ia_atime = iattrs->ia_mtime = iattrs->ia_ctime = CURRENT_TIME;
|
||||
|
||||
simple_xattrs_init(&kn->iattr->xattrs);
|
||||
out_unlock:
|
||||
ret = kn->iattr;
|
||||
mutex_unlock(&iattr_mutex);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int __kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr)
|
||||
{
|
||||
struct kernfs_iattrs *attrs;
|
||||
struct iattr *iattrs;
|
||||
unsigned int ia_valid = iattr->ia_valid;
|
||||
|
||||
attrs = kernfs_iattrs(kn);
|
||||
if (!attrs)
|
||||
return -ENOMEM;
|
||||
|
||||
iattrs = &attrs->ia_iattr;
|
||||
|
||||
if (ia_valid & ATTR_UID)
|
||||
iattrs->ia_uid = iattr->ia_uid;
|
||||
if (ia_valid & ATTR_GID)
|
||||
iattrs->ia_gid = iattr->ia_gid;
|
||||
if (ia_valid & ATTR_ATIME)
|
||||
iattrs->ia_atime = iattr->ia_atime;
|
||||
if (ia_valid & ATTR_MTIME)
|
||||
iattrs->ia_mtime = iattr->ia_mtime;
|
||||
if (ia_valid & ATTR_CTIME)
|
||||
iattrs->ia_ctime = iattr->ia_ctime;
|
||||
if (ia_valid & ATTR_MODE) {
|
||||
umode_t mode = iattr->ia_mode;
|
||||
iattrs->ia_mode = kn->mode = mode;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* kernfs_setattr - set iattr on a node
|
||||
* @kn: target node
|
||||
* @iattr: iattr to set
|
||||
*
|
||||
* Returns 0 on success, -errno on failure.
|
||||
*/
|
||||
int kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr)
|
||||
{
|
||||
int ret;
|
||||
|
||||
mutex_lock(&kernfs_mutex);
|
||||
ret = __kernfs_setattr(kn, iattr);
|
||||
mutex_unlock(&kernfs_mutex);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int kernfs_iop_setattr(struct dentry *dentry, struct iattr *iattr)
|
||||
{
|
||||
struct inode *inode = dentry->d_inode;
|
||||
struct kernfs_node *kn = dentry->d_fsdata;
|
||||
int error;
|
||||
|
||||
if (!kn)
|
||||
return -EINVAL;
|
||||
|
||||
mutex_lock(&kernfs_mutex);
|
||||
error = inode_change_ok(inode, iattr);
|
||||
if (error)
|
||||
goto out;
|
||||
|
||||
error = __kernfs_setattr(kn, iattr);
|
||||
if (error)
|
||||
goto out;
|
||||
|
||||
/* this ignores size changes */
|
||||
setattr_copy(inode, iattr);
|
||||
|
||||
out:
|
||||
mutex_unlock(&kernfs_mutex);
|
||||
return error;
|
||||
}
|
||||
|
||||
static int kernfs_node_setsecdata(struct kernfs_node *kn, void **secdata,
|
||||
u32 *secdata_len)
|
||||
{
|
||||
struct kernfs_iattrs *attrs;
|
||||
void *old_secdata;
|
||||
size_t old_secdata_len;
|
||||
|
||||
attrs = kernfs_iattrs(kn);
|
||||
if (!attrs)
|
||||
return -ENOMEM;
|
||||
|
||||
old_secdata = attrs->ia_secdata;
|
||||
old_secdata_len = attrs->ia_secdata_len;
|
||||
|
||||
attrs->ia_secdata = *secdata;
|
||||
attrs->ia_secdata_len = *secdata_len;
|
||||
|
||||
*secdata = old_secdata;
|
||||
*secdata_len = old_secdata_len;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int kernfs_iop_setxattr(struct dentry *dentry, const char *name,
|
||||
const void *value, size_t size, int flags)
|
||||
{
|
||||
struct kernfs_node *kn = dentry->d_fsdata;
|
||||
struct kernfs_iattrs *attrs;
|
||||
void *secdata;
|
||||
int error;
|
||||
u32 secdata_len = 0;
|
||||
|
||||
attrs = kernfs_iattrs(kn);
|
||||
if (!attrs)
|
||||
return -ENOMEM;
|
||||
|
||||
if (!strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN)) {
|
||||
const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
|
||||
error = security_inode_setsecurity(dentry->d_inode, suffix,
|
||||
value, size, flags);
|
||||
if (error)
|
||||
return error;
|
||||
error = security_inode_getsecctx(dentry->d_inode,
|
||||
&secdata, &secdata_len);
|
||||
if (error)
|
||||
return error;
|
||||
|
||||
mutex_lock(&kernfs_mutex);
|
||||
error = kernfs_node_setsecdata(kn, &secdata, &secdata_len);
|
||||
mutex_unlock(&kernfs_mutex);
|
||||
|
||||
if (secdata)
|
||||
security_release_secctx(secdata, secdata_len);
|
||||
return error;
|
||||
} else if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN)) {
|
||||
return simple_xattr_set(&attrs->xattrs, name, value, size,
|
||||
flags);
|
||||
}
|
||||
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
int kernfs_iop_removexattr(struct dentry *dentry, const char *name)
|
||||
{
|
||||
struct kernfs_node *kn = dentry->d_fsdata;
|
||||
struct kernfs_iattrs *attrs;
|
||||
|
||||
attrs = kernfs_iattrs(kn);
|
||||
if (!attrs)
|
||||
return -ENOMEM;
|
||||
|
||||
return simple_xattr_remove(&attrs->xattrs, name);
|
||||
}
|
||||
|
||||
ssize_t kernfs_iop_getxattr(struct dentry *dentry, const char *name, void *buf,
|
||||
size_t size)
|
||||
{
|
||||
struct kernfs_node *kn = dentry->d_fsdata;
|
||||
struct kernfs_iattrs *attrs;
|
||||
|
||||
attrs = kernfs_iattrs(kn);
|
||||
if (!attrs)
|
||||
return -ENOMEM;
|
||||
|
||||
return simple_xattr_get(&attrs->xattrs, name, buf, size);
|
||||
}
|
||||
|
||||
ssize_t kernfs_iop_listxattr(struct dentry *dentry, char *buf, size_t size)
|
||||
{
|
||||
struct kernfs_node *kn = dentry->d_fsdata;
|
||||
struct kernfs_iattrs *attrs;
|
||||
|
||||
attrs = kernfs_iattrs(kn);
|
||||
if (!attrs)
|
||||
return -ENOMEM;
|
||||
|
||||
return simple_xattr_list(&attrs->xattrs, buf, size);
|
||||
}
|
||||
|
||||
static inline void set_default_inode_attr(struct inode *inode, umode_t mode)
|
||||
{
|
||||
inode->i_mode = mode;
|
||||
inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
|
||||
}
|
||||
|
||||
static inline void set_inode_attr(struct inode *inode, struct iattr *iattr)
|
||||
{
|
||||
inode->i_uid = iattr->ia_uid;
|
||||
inode->i_gid = iattr->ia_gid;
|
||||
inode->i_atime = iattr->ia_atime;
|
||||
inode->i_mtime = iattr->ia_mtime;
|
||||
inode->i_ctime = iattr->ia_ctime;
|
||||
}
|
||||
|
||||
static void kernfs_refresh_inode(struct kernfs_node *kn, struct inode *inode)
|
||||
{
|
||||
struct kernfs_iattrs *attrs = kn->iattr;
|
||||
|
||||
inode->i_mode = kn->mode;
|
||||
if (attrs) {
|
||||
/*
|
||||
* kernfs_node has non-default attributes get them from
|
||||
* persistent copy in kernfs_node.
|
||||
*/
|
||||
set_inode_attr(inode, &attrs->ia_iattr);
|
||||
security_inode_notifysecctx(inode, attrs->ia_secdata,
|
||||
attrs->ia_secdata_len);
|
||||
}
|
||||
|
||||
if (kernfs_type(kn) == KERNFS_DIR)
|
||||
set_nlink(inode, kn->dir.subdirs + 2);
|
||||
}
|
||||
|
||||
int kernfs_iop_getattr(struct vfsmount *mnt, struct dentry *dentry,
|
||||
struct kstat *stat)
|
||||
{
|
||||
struct kernfs_node *kn = dentry->d_fsdata;
|
||||
struct inode *inode = dentry->d_inode;
|
||||
|
||||
mutex_lock(&kernfs_mutex);
|
||||
kernfs_refresh_inode(kn, inode);
|
||||
mutex_unlock(&kernfs_mutex);
|
||||
|
||||
generic_fillattr(inode, stat);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void kernfs_init_inode(struct kernfs_node *kn, struct inode *inode)
|
||||
{
|
||||
kernfs_get(kn);
|
||||
inode->i_private = kn;
|
||||
inode->i_mapping->a_ops = &kernfs_aops;
|
||||
inode->i_mapping->backing_dev_info = &kernfs_bdi;
|
||||
inode->i_op = &kernfs_iops;
|
||||
|
||||
set_default_inode_attr(inode, kn->mode);
|
||||
kernfs_refresh_inode(kn, inode);
|
||||
|
||||
/* initialize inode according to type */
|
||||
switch (kernfs_type(kn)) {
|
||||
case KERNFS_DIR:
|
||||
inode->i_op = &kernfs_dir_iops;
|
||||
inode->i_fop = &kernfs_dir_fops;
|
||||
break;
|
||||
case KERNFS_FILE:
|
||||
inode->i_size = kn->attr.size;
|
||||
inode->i_fop = &kernfs_file_fops;
|
||||
break;
|
||||
case KERNFS_LINK:
|
||||
inode->i_op = &kernfs_symlink_iops;
|
||||
break;
|
||||
default:
|
||||
BUG();
|
||||
}
|
||||
|
||||
unlock_new_inode(inode);
|
||||
}
|
||||
|
||||
/**
|
||||
* kernfs_get_inode - get inode for kernfs_node
|
||||
* @sb: super block
|
||||
* @kn: kernfs_node to allocate inode for
|
||||
*
|
||||
* Get inode for @kn. If such inode doesn't exist, a new inode is
|
||||
* allocated and basics are initialized. New inode is returned
|
||||
* locked.
|
||||
*
|
||||
* LOCKING:
|
||||
* Kernel thread context (may sleep).
|
||||
*
|
||||
* RETURNS:
|
||||
* Pointer to allocated inode on success, NULL on failure.
|
||||
*/
|
||||
struct inode *kernfs_get_inode(struct super_block *sb, struct kernfs_node *kn)
|
||||
{
|
||||
struct inode *inode;
|
||||
|
||||
inode = iget_locked(sb, kn->ino);
|
||||
if (inode && (inode->i_state & I_NEW))
|
||||
kernfs_init_inode(kn, inode);
|
||||
|
||||
return inode;
|
||||
}
|
||||
|
||||
/*
|
||||
* The kernfs_node serves as both an inode and a directory entry for
|
||||
* kernfs. To prevent the kernfs inode numbers from being freed
|
||||
* prematurely we take a reference to kernfs_node from the kernfs inode. A
|
||||
* super_operations.evict_inode() implementation is needed to drop that
|
||||
* reference upon inode destruction.
|
||||
*/
|
||||
void kernfs_evict_inode(struct inode *inode)
|
||||
{
|
||||
struct kernfs_node *kn = inode->i_private;
|
||||
|
||||
truncate_inode_pages_final(&inode->i_data);
|
||||
clear_inode(inode);
|
||||
kernfs_put(kn);
|
||||
}
|
||||
|
||||
int kernfs_iop_permission(struct inode *inode, int mask)
|
||||
{
|
||||
struct kernfs_node *kn;
|
||||
|
||||
if (mask & MAY_NOT_BLOCK)
|
||||
return -ECHILD;
|
||||
|
||||
kn = inode->i_private;
|
||||
|
||||
mutex_lock(&kernfs_mutex);
|
||||
kernfs_refresh_inode(kn, inode);
|
||||
mutex_unlock(&kernfs_mutex);
|
||||
|
||||
return generic_permission(inode, mask);
|
||||
}
|
120
fs/kernfs/kernfs-internal.h
Normal file
120
fs/kernfs/kernfs-internal.h
Normal file
|
@ -0,0 +1,120 @@
|
|||
/*
|
||||
* fs/kernfs/kernfs-internal.h - kernfs internal header file
|
||||
*
|
||||
* Copyright (c) 2001-3 Patrick Mochel
|
||||
* Copyright (c) 2007 SUSE Linux Products GmbH
|
||||
* Copyright (c) 2007, 2013 Tejun Heo <teheo@suse.de>
|
||||
*
|
||||
* This file is released under the GPLv2.
|
||||
*/
|
||||
|
||||
#ifndef __KERNFS_INTERNAL_H
|
||||
#define __KERNFS_INTERNAL_H
|
||||
|
||||
#include <linux/lockdep.h>
|
||||
#include <linux/fs.h>
|
||||
#include <linux/mutex.h>
|
||||
#include <linux/xattr.h>
|
||||
|
||||
#include <linux/kernfs.h>
|
||||
|
||||
struct kernfs_iattrs {
|
||||
struct iattr ia_iattr;
|
||||
void *ia_secdata;
|
||||
u32 ia_secdata_len;
|
||||
|
||||
struct simple_xattrs xattrs;
|
||||
};
|
||||
|
||||
/* +1 to avoid triggering overflow warning when negating it */
|
||||
#define KN_DEACTIVATED_BIAS (INT_MIN + 1)
|
||||
|
||||
/* KERNFS_TYPE_MASK and types are defined in include/linux/kernfs.h */
|
||||
|
||||
/**
|
||||
* kernfs_root - find out the kernfs_root a kernfs_node belongs to
|
||||
* @kn: kernfs_node of interest
|
||||
*
|
||||
* Return the kernfs_root @kn belongs to.
|
||||
*/
|
||||
static inline struct kernfs_root *kernfs_root(struct kernfs_node *kn)
|
||||
{
|
||||
/* if parent exists, it's always a dir; otherwise, @sd is a dir */
|
||||
if (kn->parent)
|
||||
kn = kn->parent;
|
||||
return kn->dir.root;
|
||||
}
|
||||
|
||||
/*
|
||||
* mount.c
|
||||
*/
|
||||
struct kernfs_super_info {
|
||||
struct super_block *sb;
|
||||
|
||||
/*
|
||||
* The root associated with this super_block. Each super_block is
|
||||
* identified by the root and ns it's associated with.
|
||||
*/
|
||||
struct kernfs_root *root;
|
||||
|
||||
/*
|
||||
* Each sb is associated with one namespace tag, currently the
|
||||
* network namespace of the task which mounted this kernfs
|
||||
* instance. If multiple tags become necessary, make the following
|
||||
* an array and compare kernfs_node tag against every entry.
|
||||
*/
|
||||
const void *ns;
|
||||
|
||||
/* anchored at kernfs_root->supers, protected by kernfs_mutex */
|
||||
struct list_head node;
|
||||
};
|
||||
#define kernfs_info(SB) ((struct kernfs_super_info *)(SB->s_fs_info))
|
||||
|
||||
extern const struct super_operations kernfs_sops;
|
||||
extern struct kmem_cache *kernfs_node_cache;
|
||||
|
||||
/*
|
||||
* inode.c
|
||||
*/
|
||||
struct inode *kernfs_get_inode(struct super_block *sb, struct kernfs_node *kn);
|
||||
void kernfs_evict_inode(struct inode *inode);
|
||||
int kernfs_iop_permission(struct inode *inode, int mask);
|
||||
int kernfs_iop_setattr(struct dentry *dentry, struct iattr *iattr);
|
||||
int kernfs_iop_getattr(struct vfsmount *mnt, struct dentry *dentry,
|
||||
struct kstat *stat);
|
||||
int kernfs_iop_setxattr(struct dentry *dentry, const char *name, const void *value,
|
||||
size_t size, int flags);
|
||||
int kernfs_iop_removexattr(struct dentry *dentry, const char *name);
|
||||
ssize_t kernfs_iop_getxattr(struct dentry *dentry, const char *name, void *buf,
|
||||
size_t size);
|
||||
ssize_t kernfs_iop_listxattr(struct dentry *dentry, char *buf, size_t size);
|
||||
void kernfs_inode_init(void);
|
||||
|
||||
/*
|
||||
* dir.c
|
||||
*/
|
||||
extern struct mutex kernfs_mutex;
|
||||
extern const struct dentry_operations kernfs_dops;
|
||||
extern const struct file_operations kernfs_dir_fops;
|
||||
extern const struct inode_operations kernfs_dir_iops;
|
||||
|
||||
struct kernfs_node *kernfs_get_active(struct kernfs_node *kn);
|
||||
void kernfs_put_active(struct kernfs_node *kn);
|
||||
int kernfs_add_one(struct kernfs_node *kn);
|
||||
struct kernfs_node *kernfs_new_node(struct kernfs_node *parent,
|
||||
const char *name, umode_t mode,
|
||||
unsigned flags);
|
||||
|
||||
/*
|
||||
* file.c
|
||||
*/
|
||||
extern const struct file_operations kernfs_file_fops;
|
||||
|
||||
void kernfs_unmap_bin_file(struct kernfs_node *kn);
|
||||
|
||||
/*
|
||||
* symlink.c
|
||||
*/
|
||||
extern const struct inode_operations kernfs_symlink_iops;
|
||||
|
||||
#endif /* __KERNFS_INTERNAL_H */
|
250
fs/kernfs/mount.c
Normal file
250
fs/kernfs/mount.c
Normal file
|
@ -0,0 +1,250 @@
|
|||
/*
|
||||
* fs/kernfs/mount.c - kernfs mount implementation
|
||||
*
|
||||
* Copyright (c) 2001-3 Patrick Mochel
|
||||
* Copyright (c) 2007 SUSE Linux Products GmbH
|
||||
* Copyright (c) 2007, 2013 Tejun Heo <tj@kernel.org>
|
||||
*
|
||||
* This file is released under the GPLv2.
|
||||
*/
|
||||
|
||||
#include <linux/fs.h>
|
||||
#include <linux/mount.h>
|
||||
#include <linux/init.h>
|
||||
#include <linux/magic.h>
|
||||
#include <linux/slab.h>
|
||||
#include <linux/pagemap.h>
|
||||
|
||||
#include "kernfs-internal.h"
|
||||
|
||||
struct kmem_cache *kernfs_node_cache;
|
||||
|
||||
static int kernfs_sop_remount_fs(struct super_block *sb, int *flags, char *data)
|
||||
{
|
||||
struct kernfs_root *root = kernfs_info(sb)->root;
|
||||
struct kernfs_syscall_ops *scops = root->syscall_ops;
|
||||
|
||||
if (scops && scops->remount_fs)
|
||||
return scops->remount_fs(root, flags, data);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int kernfs_sop_show_options(struct seq_file *sf, struct dentry *dentry)
|
||||
{
|
||||
struct kernfs_root *root = kernfs_root(dentry->d_fsdata);
|
||||
struct kernfs_syscall_ops *scops = root->syscall_ops;
|
||||
|
||||
if (scops && scops->show_options)
|
||||
return scops->show_options(sf, root);
|
||||
return 0;
|
||||
}
|
||||
|
||||
const struct super_operations kernfs_sops = {
|
||||
.statfs = simple_statfs,
|
||||
.drop_inode = generic_delete_inode,
|
||||
.evict_inode = kernfs_evict_inode,
|
||||
|
||||
.remount_fs = kernfs_sop_remount_fs,
|
||||
.show_options = kernfs_sop_show_options,
|
||||
};
|
||||
|
||||
/**
|
||||
* kernfs_root_from_sb - determine kernfs_root associated with a super_block
|
||||
* @sb: the super_block in question
|
||||
*
|
||||
* Return the kernfs_root associated with @sb. If @sb is not a kernfs one,
|
||||
* %NULL is returned.
|
||||
*/
|
||||
struct kernfs_root *kernfs_root_from_sb(struct super_block *sb)
|
||||
{
|
||||
if (sb->s_op == &kernfs_sops)
|
||||
return kernfs_info(sb)->root;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static int kernfs_fill_super(struct super_block *sb, unsigned long magic)
|
||||
{
|
||||
struct kernfs_super_info *info = kernfs_info(sb);
|
||||
struct inode *inode;
|
||||
struct dentry *root;
|
||||
|
||||
info->sb = sb;
|
||||
sb->s_blocksize = PAGE_CACHE_SIZE;
|
||||
sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
|
||||
sb->s_magic = magic;
|
||||
sb->s_op = &kernfs_sops;
|
||||
sb->s_time_gran = 1;
|
||||
|
||||
/* get root inode, initialize and unlock it */
|
||||
mutex_lock(&kernfs_mutex);
|
||||
inode = kernfs_get_inode(sb, info->root->kn);
|
||||
mutex_unlock(&kernfs_mutex);
|
||||
if (!inode) {
|
||||
pr_debug("kernfs: could not get root inode\n");
|
||||
return -ENOMEM;
|
||||
}
|
||||
|
||||
/* instantiate and link root dentry */
|
||||
root = d_make_root(inode);
|
||||
if (!root) {
|
||||
pr_debug("%s: could not get root dentry!\n", __func__);
|
||||
return -ENOMEM;
|
||||
}
|
||||
kernfs_get(info->root->kn);
|
||||
root->d_fsdata = info->root->kn;
|
||||
sb->s_root = root;
|
||||
sb->s_d_op = &kernfs_dops;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int kernfs_test_super(struct super_block *sb, void *data)
|
||||
{
|
||||
struct kernfs_super_info *sb_info = kernfs_info(sb);
|
||||
struct kernfs_super_info *info = data;
|
||||
|
||||
return sb_info->root == info->root && sb_info->ns == info->ns;
|
||||
}
|
||||
|
||||
static int kernfs_set_super(struct super_block *sb, void *data)
|
||||
{
|
||||
int error;
|
||||
error = set_anon_super(sb, data);
|
||||
if (!error)
|
||||
sb->s_fs_info = data;
|
||||
return error;
|
||||
}
|
||||
|
||||
/**
|
||||
* kernfs_super_ns - determine the namespace tag of a kernfs super_block
|
||||
* @sb: super_block of interest
|
||||
*
|
||||
* Return the namespace tag associated with kernfs super_block @sb.
|
||||
*/
|
||||
const void *kernfs_super_ns(struct super_block *sb)
|
||||
{
|
||||
struct kernfs_super_info *info = kernfs_info(sb);
|
||||
|
||||
return info->ns;
|
||||
}
|
||||
|
||||
/**
|
||||
* kernfs_mount_ns - kernfs mount helper
|
||||
* @fs_type: file_system_type of the fs being mounted
|
||||
* @flags: mount flags specified for the mount
|
||||
* @root: kernfs_root of the hierarchy being mounted
|
||||
* @magic: file system specific magic number
|
||||
* @new_sb_created: tell the caller if we allocated a new superblock
|
||||
* @ns: optional namespace tag of the mount
|
||||
*
|
||||
* This is to be called from each kernfs user's file_system_type->mount()
|
||||
* implementation, which should pass through the specified @fs_type and
|
||||
* @flags, and specify the hierarchy and namespace tag to mount via @root
|
||||
* and @ns, respectively.
|
||||
*
|
||||
* The return value can be passed to the vfs layer verbatim.
|
||||
*/
|
||||
struct dentry *kernfs_mount_ns(struct file_system_type *fs_type, int flags,
|
||||
struct kernfs_root *root, unsigned long magic,
|
||||
bool *new_sb_created, const void *ns)
|
||||
{
|
||||
struct super_block *sb;
|
||||
struct kernfs_super_info *info;
|
||||
int error;
|
||||
|
||||
info = kzalloc(sizeof(*info), GFP_KERNEL);
|
||||
if (!info)
|
||||
return ERR_PTR(-ENOMEM);
|
||||
|
||||
info->root = root;
|
||||
info->ns = ns;
|
||||
|
||||
sb = sget(fs_type, kernfs_test_super, kernfs_set_super, flags, info);
|
||||
if (IS_ERR(sb) || sb->s_fs_info != info)
|
||||
kfree(info);
|
||||
if (IS_ERR(sb))
|
||||
return ERR_CAST(sb);
|
||||
|
||||
if (new_sb_created)
|
||||
*new_sb_created = !sb->s_root;
|
||||
|
||||
if (!sb->s_root) {
|
||||
struct kernfs_super_info *info = kernfs_info(sb);
|
||||
|
||||
error = kernfs_fill_super(sb, magic);
|
||||
if (error) {
|
||||
deactivate_locked_super(sb);
|
||||
return ERR_PTR(error);
|
||||
}
|
||||
sb->s_flags |= MS_ACTIVE;
|
||||
|
||||
mutex_lock(&kernfs_mutex);
|
||||
list_add(&info->node, &root->supers);
|
||||
mutex_unlock(&kernfs_mutex);
|
||||
}
|
||||
|
||||
return dget(sb->s_root);
|
||||
}
|
||||
|
||||
/**
|
||||
* kernfs_kill_sb - kill_sb for kernfs
|
||||
* @sb: super_block being killed
|
||||
*
|
||||
* This can be used directly for file_system_type->kill_sb(). If a kernfs
|
||||
* user needs extra cleanup, it can implement its own kill_sb() and call
|
||||
* this function at the end.
|
||||
*/
|
||||
void kernfs_kill_sb(struct super_block *sb)
|
||||
{
|
||||
struct kernfs_super_info *info = kernfs_info(sb);
|
||||
struct kernfs_node *root_kn = sb->s_root->d_fsdata;
|
||||
|
||||
mutex_lock(&kernfs_mutex);
|
||||
list_del(&info->node);
|
||||
mutex_unlock(&kernfs_mutex);
|
||||
|
||||
/*
|
||||
* Remove the superblock from fs_supers/s_instances
|
||||
* so we can't find it, before freeing kernfs_super_info.
|
||||
*/
|
||||
kill_anon_super(sb);
|
||||
kfree(info);
|
||||
kernfs_put(root_kn);
|
||||
}
|
||||
|
||||
/**
|
||||
* kernfs_pin_sb: try to pin the superblock associated with a kernfs_root
|
||||
* @kernfs_root: the kernfs_root in question
|
||||
* @ns: the namespace tag
|
||||
*
|
||||
* Pin the superblock so the superblock won't be destroyed in subsequent
|
||||
* operations. This can be used to block ->kill_sb() which may be useful
|
||||
* for kernfs users which dynamically manage superblocks.
|
||||
*
|
||||
* Returns NULL if there's no superblock associated to this kernfs_root, or
|
||||
* -EINVAL if the superblock is being freed.
|
||||
*/
|
||||
struct super_block *kernfs_pin_sb(struct kernfs_root *root, const void *ns)
|
||||
{
|
||||
struct kernfs_super_info *info;
|
||||
struct super_block *sb = NULL;
|
||||
|
||||
mutex_lock(&kernfs_mutex);
|
||||
list_for_each_entry(info, &root->supers, node) {
|
||||
if (info->ns == ns) {
|
||||
sb = info->sb;
|
||||
if (!atomic_inc_not_zero(&info->sb->s_active))
|
||||
sb = ERR_PTR(-EINVAL);
|
||||
break;
|
||||
}
|
||||
}
|
||||
mutex_unlock(&kernfs_mutex);
|
||||
return sb;
|
||||
}
|
||||
|
||||
void __init kernfs_init(void)
|
||||
{
|
||||
kernfs_node_cache = kmem_cache_create("kernfs_node_cache",
|
||||
sizeof(struct kernfs_node),
|
||||
0, SLAB_PANIC, NULL);
|
||||
kernfs_inode_init();
|
||||
}
|
147
fs/kernfs/symlink.c
Normal file
147
fs/kernfs/symlink.c
Normal file
|
@ -0,0 +1,147 @@
|
|||
/*
|
||||
* fs/kernfs/symlink.c - kernfs symlink implementation
|
||||
*
|
||||
* Copyright (c) 2001-3 Patrick Mochel
|
||||
* Copyright (c) 2007 SUSE Linux Products GmbH
|
||||
* Copyright (c) 2007, 2013 Tejun Heo <tj@kernel.org>
|
||||
*
|
||||
* This file is released under the GPLv2.
|
||||
*/
|
||||
|
||||
#include <linux/fs.h>
|
||||
#include <linux/gfp.h>
|
||||
#include <linux/namei.h>
|
||||
|
||||
#include "kernfs-internal.h"
|
||||
|
||||
/**
|
||||
* kernfs_create_link - create a symlink
|
||||
* @parent: directory to create the symlink in
|
||||
* @name: name of the symlink
|
||||
* @target: target node for the symlink to point to
|
||||
*
|
||||
* Returns the created node on success, ERR_PTR() value on error.
|
||||
*/
|
||||
struct kernfs_node *kernfs_create_link(struct kernfs_node *parent,
|
||||
const char *name,
|
||||
struct kernfs_node *target)
|
||||
{
|
||||
struct kernfs_node *kn;
|
||||
int error;
|
||||
|
||||
kn = kernfs_new_node(parent, name, S_IFLNK|S_IRWXUGO, KERNFS_LINK);
|
||||
if (!kn)
|
||||
return ERR_PTR(-ENOMEM);
|
||||
|
||||
if (kernfs_ns_enabled(parent))
|
||||
kn->ns = target->ns;
|
||||
kn->symlink.target_kn = target;
|
||||
kernfs_get(target); /* ref owned by symlink */
|
||||
|
||||
error = kernfs_add_one(kn);
|
||||
if (!error)
|
||||
return kn;
|
||||
|
||||
kernfs_put(kn);
|
||||
return ERR_PTR(error);
|
||||
}
|
||||
|
||||
static int kernfs_get_target_path(struct kernfs_node *parent,
|
||||
struct kernfs_node *target, char *path)
|
||||
{
|
||||
struct kernfs_node *base, *kn;
|
||||
char *s = path;
|
||||
int len = 0;
|
||||
|
||||
/* go up to the root, stop at the base */
|
||||
base = parent;
|
||||
while (base->parent) {
|
||||
kn = target->parent;
|
||||
while (kn->parent && base != kn)
|
||||
kn = kn->parent;
|
||||
|
||||
if (base == kn)
|
||||
break;
|
||||
|
||||
strcpy(s, "../");
|
||||
s += 3;
|
||||
base = base->parent;
|
||||
}
|
||||
|
||||
/* determine end of target string for reverse fillup */
|
||||
kn = target;
|
||||
while (kn->parent && kn != base) {
|
||||
len += strlen(kn->name) + 1;
|
||||
kn = kn->parent;
|
||||
}
|
||||
|
||||
/* check limits */
|
||||
if (len < 2)
|
||||
return -EINVAL;
|
||||
len--;
|
||||
if ((s - path) + len > PATH_MAX)
|
||||
return -ENAMETOOLONG;
|
||||
|
||||
/* reverse fillup of target string from target to base */
|
||||
kn = target;
|
||||
while (kn->parent && kn != base) {
|
||||
int slen = strlen(kn->name);
|
||||
|
||||
len -= slen;
|
||||
strncpy(s + len, kn->name, slen);
|
||||
if (len)
|
||||
s[--len] = '/';
|
||||
|
||||
kn = kn->parent;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int kernfs_getlink(struct dentry *dentry, char *path)
|
||||
{
|
||||
struct kernfs_node *kn = dentry->d_fsdata;
|
||||
struct kernfs_node *parent = kn->parent;
|
||||
struct kernfs_node *target = kn->symlink.target_kn;
|
||||
int error;
|
||||
|
||||
mutex_lock(&kernfs_mutex);
|
||||
error = kernfs_get_target_path(parent, target, path);
|
||||
mutex_unlock(&kernfs_mutex);
|
||||
|
||||
return error;
|
||||
}
|
||||
|
||||
static void *kernfs_iop_follow_link(struct dentry *dentry, struct nameidata *nd)
|
||||
{
|
||||
int error = -ENOMEM;
|
||||
unsigned long page = get_zeroed_page(GFP_KERNEL);
|
||||
if (page) {
|
||||
error = kernfs_getlink(dentry, (char *) page);
|
||||
if (error < 0)
|
||||
free_page((unsigned long)page);
|
||||
}
|
||||
nd_set_link(nd, error ? ERR_PTR(error) : (char *)page);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void kernfs_iop_put_link(struct dentry *dentry, struct nameidata *nd,
|
||||
void *cookie)
|
||||
{
|
||||
char *page = nd_get_link(nd);
|
||||
if (!IS_ERR(page))
|
||||
free_page((unsigned long)page);
|
||||
}
|
||||
|
||||
const struct inode_operations kernfs_symlink_iops = {
|
||||
.setxattr = kernfs_iop_setxattr,
|
||||
.removexattr = kernfs_iop_removexattr,
|
||||
.getxattr = kernfs_iop_getxattr,
|
||||
.listxattr = kernfs_iop_listxattr,
|
||||
.readlink = generic_readlink,
|
||||
.follow_link = kernfs_iop_follow_link,
|
||||
.put_link = kernfs_iop_put_link,
|
||||
.setattr = kernfs_iop_setattr,
|
||||
.getattr = kernfs_iop_getattr,
|
||||
.permission = kernfs_iop_permission,
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue