Changes in 4.19.313
batman-adv: Avoid infinite loop trying to resize local TT
Bluetooth: Fix memory leak in hci_req_sync_complete()
nouveau: fix function cast warning
geneve: fix header validation in geneve[6]_xmit_skb
ipv6: fib: hide unused 'pn' variable
ipv4/route: avoid unused-but-set-variable warning
ipv6: fix race condition between ipv6_get_ifaddr and ipv6_del_addr
net/mlx5: Properly link new fs rules into the tree
tracing: hide unused ftrace_event_id_fops
vhost: Add smp_rmb() in vhost_vq_avail_empty()
selftests: timers: Fix abs() warning in posix_timers test
x86/apic: Force native_apic_mem_read() to use the MOV instruction
btrfs: record delayed inode root in transaction
selftests/ftrace: Limit length in subsystem-enable tests
kprobes: Fix possible use-after-free issue on kprobe registration
Revert "tracing/trigger: Fix to return error if failed to alloc snapshot"
netfilter: nf_tables: __nft_expr_type_get() selects specific family type
netfilter: nf_tables: Fix potential data-race in __nft_expr_type_get()
tun: limit printing rate when illegal packet received by tun dev
RDMA/mlx5: Fix port number for counter query in multi-port configuration
drm: nv04: Fix out of bounds access
comedi: vmk80xx: fix incomplete endpoint checking
serial/pmac_zilog: Remove flawed mitigation for rx irq flood
USB: serial: option: add Fibocom FM135-GL variants
USB: serial: option: add support for Fibocom FM650/FG650
USB: serial: option: add Lonsung U8300/U9300 product
USB: serial: option: support Quectel EM060K sub-models
USB: serial: option: add Rolling RW101-GL and RW135-GL support
USB: serial: option: add Telit FN920C04 rmnet compositions
Revert "usb: cdc-wdm: close race between read and workqueue"
usb: dwc2: host: Fix dereference issue in DDMA completion flow.
speakup: Avoid crash on very long word
fs: sysfs: Fix reference leak in sysfs_break_active_protection()
nouveau: fix instmem race condition around ptr stores
nilfs2: fix OOB in nilfs_set_de_type
tracing: Remove hist trigger synth_var_refs
tracing: Use var_refs[] for hist trigger reference checking
arm64: dts: rockchip: fix alphabetical ordering RK3399 puma
arm64: dts: rockchip: enable internal pull-up on PCIE_WAKE# for RK3399 Puma
arm64: dts: mediatek: mt7622: fix IR nodename
arm64: dts: mediatek: mt7622: fix ethernet controller "compatible"
arm64: dts: mediatek: mt7622: drop "reset-names" from thermal block
ARC: [plat-hsdk]: Remove misplaced interrupt-cells property
vxlan: drop packets from invalid src-address
mlxsw: core: Unregister EMAD trap using FORWARD action
NFC: trf7970a: disable all regulators on removal
net: usb: ax88179_178a: stop lying about skb->truesize
net: gtp: Fix Use-After-Free in gtp_dellink
ipvs: Fix checksumming on GSO of SCTP packets
net: openvswitch: ovs_ct_exit to be done under ovs_lock
net: openvswitch: Fix Use-After-Free in ovs_ct_exit
i40e: Do not use WQ_MEM_RECLAIM flag for workqueue
serial: core: Provide port lock wrappers
serial: mxs-auart: add spinlock around changing cts state
drm/amdgpu: restrict bo mapping within gpu address limits
amdgpu: validate offset_in_bo of drm_amdgpu_gem_va
drm/amdgpu: validate the parameters of bo mapping operations more clearly
Revert "crypto: api - Disallow identical driver names"
tracing: Show size of requested perf buffer
tracing: Increase PERF_MAX_TRACE_SIZE to handle Sentinel1 and docker together
Bluetooth: Fix type of len in {l2cap,sco}_sock_getsockopt_old()
btrfs: fix information leak in btrfs_ioctl_logical_to_ino()
arm64: dts: rockchip: enable internal pull-up for Q7_THRM# on RK3399 Puma
irqchip/gic-v3-its: Prevent double free on error
net: b44: set pause params only when interface is up
stackdepot: respect __GFP_NOLOCKDEP allocation flag
mtd: diskonchip: work around ubsan link failure
tcp: Clean up kernel listener's reqsk in inet_twsk_purge()
tcp: Fix NEW_SYN_RECV handling in inet_twsk_purge()
dmaengine: owl: fix register access functions
idma64: Don't try to serve interrupts when device is powered off
i2c: smbus: fix NULL function pointer dereference
HID: i2c-hid: remove I2C_HID_READ_PENDING flag to prevent lock-up
Revert "loop: Remove sector_t truncation checks"
Revert "y2038: rusage: use __kernel_old_timeval"
udp: preserve the connected status if only UDP cmsg
serial: core: fix kernel-doc for uart_port_unlock_irqrestore()
Linux 4.19.313
Change-Id: I6558068fa522b4cd853251716389c0d30a47522f
Signed-off-by: Greg Kroah-Hartman <gregkh@google.com>
617 lines
15 KiB
C
617 lines
15 KiB
C
// SPDX-License-Identifier: GPL-2.0
|
|
/*
|
|
* fs/sysfs/file.c - sysfs regular (text) file implementation
|
|
*
|
|
* Copyright (c) 2001-3 Patrick Mochel
|
|
* Copyright (c) 2007 SUSE Linux Products GmbH
|
|
* Copyright (c) 2007 Tejun Heo <teheo@suse.de>
|
|
*
|
|
* Please see Documentation/filesystems/sysfs.txt for more information.
|
|
*/
|
|
|
|
#include <linux/module.h>
|
|
#include <linux/kobject.h>
|
|
#include <linux/slab.h>
|
|
#include <linux/list.h>
|
|
#include <linux/mutex.h>
|
|
#include <linux/seq_file.h>
|
|
#ifndef __GENKSYMS__
|
|
#include <linux/mm.h>
|
|
#endif
|
|
|
|
#include "sysfs.h"
|
|
#include "../kernfs/kernfs-internal.h"
|
|
|
|
/*
|
|
* Determine ktype->sysfs_ops for the given kernfs_node. This function
|
|
* must be called while holding an active reference.
|
|
*/
|
|
static const struct sysfs_ops *sysfs_file_ops(struct kernfs_node *kn)
|
|
{
|
|
struct kobject *kobj = kn->parent->priv;
|
|
|
|
if (kn->flags & KERNFS_LOCKDEP)
|
|
lockdep_assert_held(kn);
|
|
return kobj->ktype ? kobj->ktype->sysfs_ops : NULL;
|
|
}
|
|
|
|
/*
|
|
* Reads on sysfs are handled through seq_file, which takes care of hairy
|
|
* details like buffering and seeking. The following function pipes
|
|
* sysfs_ops->show() result through seq_file.
|
|
*/
|
|
static int sysfs_kf_seq_show(struct seq_file *sf, void *v)
|
|
{
|
|
struct kernfs_open_file *of = sf->private;
|
|
struct kobject *kobj = of->kn->parent->priv;
|
|
const struct sysfs_ops *ops = sysfs_file_ops(of->kn);
|
|
ssize_t count;
|
|
char *buf;
|
|
|
|
/* acquire buffer and ensure that it's >= PAGE_SIZE and clear */
|
|
count = seq_get_buf(sf, &buf);
|
|
if (count < PAGE_SIZE) {
|
|
seq_commit(sf, -1);
|
|
return 0;
|
|
}
|
|
memset(buf, 0, PAGE_SIZE);
|
|
|
|
/*
|
|
* Invoke show(). Control may reach here via seq file lseek even
|
|
* if @ops->show() isn't implemented.
|
|
*/
|
|
if (ops->show) {
|
|
count = ops->show(kobj, of->kn->priv, buf);
|
|
if (count < 0)
|
|
return count;
|
|
}
|
|
|
|
/*
|
|
* The code works fine with PAGE_SIZE return but it's likely to
|
|
* indicate truncated result or overflow in normal use cases.
|
|
*/
|
|
if (count >= (ssize_t)PAGE_SIZE) {
|
|
printk("fill_read_buffer: %pS returned bad count\n",
|
|
ops->show);
|
|
/* Try to struggle along */
|
|
count = PAGE_SIZE - 1;
|
|
}
|
|
seq_commit(sf, count);
|
|
return 0;
|
|
}
|
|
|
|
static ssize_t sysfs_kf_bin_read(struct kernfs_open_file *of, char *buf,
|
|
size_t count, loff_t pos)
|
|
{
|
|
struct bin_attribute *battr = of->kn->priv;
|
|
struct kobject *kobj = of->kn->parent->priv;
|
|
loff_t size = file_inode(of->file)->i_size;
|
|
|
|
if (!count)
|
|
return 0;
|
|
|
|
if (size) {
|
|
if (pos >= size)
|
|
return 0;
|
|
if (pos + count > size)
|
|
count = size - pos;
|
|
}
|
|
|
|
if (!battr->read)
|
|
return -EIO;
|
|
|
|
return battr->read(of->file, kobj, battr, buf, pos, count);
|
|
}
|
|
|
|
/* kernfs read callback for regular sysfs files with pre-alloc */
|
|
static ssize_t sysfs_kf_read(struct kernfs_open_file *of, char *buf,
|
|
size_t count, loff_t pos)
|
|
{
|
|
const struct sysfs_ops *ops = sysfs_file_ops(of->kn);
|
|
struct kobject *kobj = of->kn->parent->priv;
|
|
ssize_t len;
|
|
|
|
/*
|
|
* If buf != of->prealloc_buf, we don't know how
|
|
* large it is, so cannot safely pass it to ->show
|
|
*/
|
|
if (WARN_ON_ONCE(buf != of->prealloc_buf))
|
|
return 0;
|
|
len = ops->show(kobj, of->kn->priv, buf);
|
|
if (len < 0)
|
|
return len;
|
|
if (pos) {
|
|
if (len <= pos)
|
|
return 0;
|
|
len -= pos;
|
|
memmove(buf, buf + pos, len);
|
|
}
|
|
return min_t(ssize_t, count, len);
|
|
}
|
|
|
|
/* kernfs write callback for regular sysfs files */
|
|
static ssize_t sysfs_kf_write(struct kernfs_open_file *of, char *buf,
|
|
size_t count, loff_t pos)
|
|
{
|
|
const struct sysfs_ops *ops = sysfs_file_ops(of->kn);
|
|
struct kobject *kobj = of->kn->parent->priv;
|
|
|
|
if (!count)
|
|
return 0;
|
|
|
|
return ops->store(kobj, of->kn->priv, buf, count);
|
|
}
|
|
|
|
/* kernfs write callback for bin sysfs files */
|
|
static ssize_t sysfs_kf_bin_write(struct kernfs_open_file *of, char *buf,
|
|
size_t count, loff_t pos)
|
|
{
|
|
struct bin_attribute *battr = of->kn->priv;
|
|
struct kobject *kobj = of->kn->parent->priv;
|
|
loff_t size = file_inode(of->file)->i_size;
|
|
|
|
if (size) {
|
|
if (size <= pos)
|
|
return -EFBIG;
|
|
count = min_t(ssize_t, count, size - pos);
|
|
}
|
|
if (!count)
|
|
return 0;
|
|
|
|
if (!battr->write)
|
|
return -EIO;
|
|
|
|
return battr->write(of->file, kobj, battr, buf, pos, count);
|
|
}
|
|
|
|
static int sysfs_kf_bin_mmap(struct kernfs_open_file *of,
|
|
struct vm_area_struct *vma)
|
|
{
|
|
struct bin_attribute *battr = of->kn->priv;
|
|
struct kobject *kobj = of->kn->parent->priv;
|
|
|
|
return battr->mmap(of->file, kobj, battr, vma);
|
|
}
|
|
|
|
void sysfs_notify(struct kobject *kobj, const char *dir, const char *attr)
|
|
{
|
|
struct kernfs_node *kn = kobj->sd, *tmp;
|
|
|
|
if (kn && dir)
|
|
kn = kernfs_find_and_get(kn, dir);
|
|
else
|
|
kernfs_get(kn);
|
|
|
|
if (kn && attr) {
|
|
tmp = kernfs_find_and_get(kn, attr);
|
|
kernfs_put(kn);
|
|
kn = tmp;
|
|
}
|
|
|
|
if (kn) {
|
|
kernfs_notify(kn);
|
|
kernfs_put(kn);
|
|
}
|
|
}
|
|
EXPORT_SYMBOL_GPL(sysfs_notify);
|
|
|
|
static const struct kernfs_ops sysfs_file_kfops_empty = {
|
|
};
|
|
|
|
static const struct kernfs_ops sysfs_file_kfops_ro = {
|
|
.seq_show = sysfs_kf_seq_show,
|
|
};
|
|
|
|
static const struct kernfs_ops sysfs_file_kfops_wo = {
|
|
.write = sysfs_kf_write,
|
|
};
|
|
|
|
static const struct kernfs_ops sysfs_file_kfops_rw = {
|
|
.seq_show = sysfs_kf_seq_show,
|
|
.write = sysfs_kf_write,
|
|
};
|
|
|
|
static const struct kernfs_ops sysfs_prealloc_kfops_ro = {
|
|
.read = sysfs_kf_read,
|
|
.prealloc = true,
|
|
};
|
|
|
|
static const struct kernfs_ops sysfs_prealloc_kfops_wo = {
|
|
.write = sysfs_kf_write,
|
|
.prealloc = true,
|
|
};
|
|
|
|
static const struct kernfs_ops sysfs_prealloc_kfops_rw = {
|
|
.read = sysfs_kf_read,
|
|
.write = sysfs_kf_write,
|
|
.prealloc = true,
|
|
};
|
|
|
|
static const struct kernfs_ops sysfs_bin_kfops_ro = {
|
|
.read = sysfs_kf_bin_read,
|
|
};
|
|
|
|
static const struct kernfs_ops sysfs_bin_kfops_wo = {
|
|
.write = sysfs_kf_bin_write,
|
|
};
|
|
|
|
static const struct kernfs_ops sysfs_bin_kfops_rw = {
|
|
.read = sysfs_kf_bin_read,
|
|
.write = sysfs_kf_bin_write,
|
|
};
|
|
|
|
static const struct kernfs_ops sysfs_bin_kfops_mmap = {
|
|
.read = sysfs_kf_bin_read,
|
|
.write = sysfs_kf_bin_write,
|
|
.mmap = sysfs_kf_bin_mmap,
|
|
};
|
|
|
|
int sysfs_add_file_mode_ns(struct kernfs_node *parent,
|
|
const struct attribute *attr, bool is_bin,
|
|
umode_t mode, kuid_t uid, kgid_t gid, const void *ns)
|
|
{
|
|
struct lock_class_key *key = NULL;
|
|
const struct kernfs_ops *ops;
|
|
struct kernfs_node *kn;
|
|
loff_t size;
|
|
|
|
if (!is_bin) {
|
|
struct kobject *kobj = parent->priv;
|
|
const struct sysfs_ops *sysfs_ops = kobj->ktype->sysfs_ops;
|
|
|
|
/* every kobject with an attribute needs a ktype assigned */
|
|
if (WARN(!sysfs_ops, KERN_ERR
|
|
"missing sysfs attribute operations for kobject: %s\n",
|
|
kobject_name(kobj)))
|
|
return -EINVAL;
|
|
|
|
if (sysfs_ops->show && sysfs_ops->store) {
|
|
if (mode & SYSFS_PREALLOC)
|
|
ops = &sysfs_prealloc_kfops_rw;
|
|
else
|
|
ops = &sysfs_file_kfops_rw;
|
|
} else if (sysfs_ops->show) {
|
|
if (mode & SYSFS_PREALLOC)
|
|
ops = &sysfs_prealloc_kfops_ro;
|
|
else
|
|
ops = &sysfs_file_kfops_ro;
|
|
} else if (sysfs_ops->store) {
|
|
if (mode & SYSFS_PREALLOC)
|
|
ops = &sysfs_prealloc_kfops_wo;
|
|
else
|
|
ops = &sysfs_file_kfops_wo;
|
|
} else
|
|
ops = &sysfs_file_kfops_empty;
|
|
|
|
size = PAGE_SIZE;
|
|
} else {
|
|
struct bin_attribute *battr = (void *)attr;
|
|
|
|
if (battr->mmap)
|
|
ops = &sysfs_bin_kfops_mmap;
|
|
else if (battr->read && battr->write)
|
|
ops = &sysfs_bin_kfops_rw;
|
|
else if (battr->read)
|
|
ops = &sysfs_bin_kfops_ro;
|
|
else if (battr->write)
|
|
ops = &sysfs_bin_kfops_wo;
|
|
else
|
|
ops = &sysfs_file_kfops_empty;
|
|
|
|
size = battr->size;
|
|
}
|
|
|
|
#ifdef CONFIG_DEBUG_LOCK_ALLOC
|
|
if (!attr->ignore_lockdep)
|
|
key = attr->key ?: (struct lock_class_key *)&attr->skey;
|
|
#endif
|
|
|
|
kn = __kernfs_create_file(parent, attr->name, mode & 0777, uid, gid,
|
|
size, ops, (void *)attr, ns, key);
|
|
if (IS_ERR(kn)) {
|
|
if (PTR_ERR(kn) == -EEXIST)
|
|
sysfs_warn_dup(parent, attr->name);
|
|
return PTR_ERR(kn);
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
/**
|
|
* sysfs_create_file_ns - create an attribute file for an object with custom ns
|
|
* @kobj: object we're creating for
|
|
* @attr: attribute descriptor
|
|
* @ns: namespace the new file should belong to
|
|
*/
|
|
int sysfs_create_file_ns(struct kobject *kobj, const struct attribute *attr,
|
|
const void *ns)
|
|
{
|
|
kuid_t uid;
|
|
kgid_t gid;
|
|
|
|
BUG_ON(!kobj || !kobj->sd || !attr);
|
|
|
|
kobject_get_ownership(kobj, &uid, &gid);
|
|
return sysfs_add_file_mode_ns(kobj->sd, attr, false, attr->mode,
|
|
uid, gid, ns);
|
|
|
|
}
|
|
EXPORT_SYMBOL_GPL(sysfs_create_file_ns);
|
|
|
|
int sysfs_create_files(struct kobject *kobj, const struct attribute **ptr)
|
|
{
|
|
int err = 0;
|
|
int i;
|
|
|
|
for (i = 0; ptr[i] && !err; i++)
|
|
err = sysfs_create_file(kobj, ptr[i]);
|
|
if (err)
|
|
while (--i >= 0)
|
|
sysfs_remove_file(kobj, ptr[i]);
|
|
return err;
|
|
}
|
|
EXPORT_SYMBOL_GPL(sysfs_create_files);
|
|
|
|
/**
|
|
* sysfs_add_file_to_group - add an attribute file to a pre-existing group.
|
|
* @kobj: object we're acting for.
|
|
* @attr: attribute descriptor.
|
|
* @group: group name.
|
|
*/
|
|
int sysfs_add_file_to_group(struct kobject *kobj,
|
|
const struct attribute *attr, const char *group)
|
|
{
|
|
struct kernfs_node *parent;
|
|
kuid_t uid;
|
|
kgid_t gid;
|
|
int error;
|
|
|
|
if (group) {
|
|
parent = kernfs_find_and_get(kobj->sd, group);
|
|
} else {
|
|
parent = kobj->sd;
|
|
kernfs_get(parent);
|
|
}
|
|
|
|
if (!parent)
|
|
return -ENOENT;
|
|
|
|
kobject_get_ownership(kobj, &uid, &gid);
|
|
error = sysfs_add_file_mode_ns(parent, attr, false,
|
|
attr->mode, uid, gid, NULL);
|
|
kernfs_put(parent);
|
|
|
|
return error;
|
|
}
|
|
EXPORT_SYMBOL_GPL(sysfs_add_file_to_group);
|
|
|
|
/**
|
|
* sysfs_chmod_file - update the modified mode value on an object attribute.
|
|
* @kobj: object we're acting for.
|
|
* @attr: attribute descriptor.
|
|
* @mode: file permissions.
|
|
*
|
|
*/
|
|
int sysfs_chmod_file(struct kobject *kobj, const struct attribute *attr,
|
|
umode_t mode)
|
|
{
|
|
struct kernfs_node *kn;
|
|
struct iattr newattrs;
|
|
int rc;
|
|
|
|
kn = kernfs_find_and_get(kobj->sd, attr->name);
|
|
if (!kn)
|
|
return -ENOENT;
|
|
|
|
newattrs.ia_mode = (mode & S_IALLUGO) | (kn->mode & ~S_IALLUGO);
|
|
newattrs.ia_valid = ATTR_MODE;
|
|
|
|
rc = kernfs_setattr(kn, &newattrs);
|
|
|
|
kernfs_put(kn);
|
|
return rc;
|
|
}
|
|
EXPORT_SYMBOL_GPL(sysfs_chmod_file);
|
|
|
|
/**
|
|
* sysfs_break_active_protection - break "active" protection
|
|
* @kobj: The kernel object @attr is associated with.
|
|
* @attr: The attribute to break the "active" protection for.
|
|
*
|
|
* With sysfs, just like kernfs, deletion of an attribute is postponed until
|
|
* all active .show() and .store() callbacks have finished unless this function
|
|
* is called. Hence this function is useful in methods that implement self
|
|
* deletion.
|
|
*/
|
|
struct kernfs_node *sysfs_break_active_protection(struct kobject *kobj,
|
|
const struct attribute *attr)
|
|
{
|
|
struct kernfs_node *kn;
|
|
|
|
kobject_get(kobj);
|
|
kn = kernfs_find_and_get(kobj->sd, attr->name);
|
|
if (kn)
|
|
kernfs_break_active_protection(kn);
|
|
else
|
|
kobject_put(kobj);
|
|
return kn;
|
|
}
|
|
EXPORT_SYMBOL_GPL(sysfs_break_active_protection);
|
|
|
|
/**
|
|
* sysfs_unbreak_active_protection - restore "active" protection
|
|
* @kn: Pointer returned by sysfs_break_active_protection().
|
|
*
|
|
* Undo the effects of sysfs_break_active_protection(). Since this function
|
|
* calls kernfs_put() on the kernfs node that corresponds to the 'attr'
|
|
* argument passed to sysfs_break_active_protection() that attribute may have
|
|
* been removed between the sysfs_break_active_protection() and
|
|
* sysfs_unbreak_active_protection() calls, it is not safe to access @kn after
|
|
* this function has returned.
|
|
*/
|
|
void sysfs_unbreak_active_protection(struct kernfs_node *kn)
|
|
{
|
|
struct kobject *kobj = kn->parent->priv;
|
|
|
|
kernfs_unbreak_active_protection(kn);
|
|
kernfs_put(kn);
|
|
kobject_put(kobj);
|
|
}
|
|
EXPORT_SYMBOL_GPL(sysfs_unbreak_active_protection);
|
|
|
|
/**
|
|
* sysfs_remove_file_ns - remove an object attribute with a custom ns tag
|
|
* @kobj: object we're acting for
|
|
* @attr: attribute descriptor
|
|
* @ns: namespace tag of the file to remove
|
|
*
|
|
* Hash the attribute name and namespace tag and kill the victim.
|
|
*/
|
|
void sysfs_remove_file_ns(struct kobject *kobj, const struct attribute *attr,
|
|
const void *ns)
|
|
{
|
|
struct kernfs_node *parent = kobj->sd;
|
|
|
|
kernfs_remove_by_name_ns(parent, attr->name, ns);
|
|
}
|
|
EXPORT_SYMBOL_GPL(sysfs_remove_file_ns);
|
|
|
|
/**
|
|
* sysfs_remove_file_self - remove an object attribute from its own method
|
|
* @kobj: object we're acting for
|
|
* @attr: attribute descriptor
|
|
*
|
|
* See kernfs_remove_self() for details.
|
|
*/
|
|
bool sysfs_remove_file_self(struct kobject *kobj, const struct attribute *attr)
|
|
{
|
|
struct kernfs_node *parent = kobj->sd;
|
|
struct kernfs_node *kn;
|
|
bool ret;
|
|
|
|
kn = kernfs_find_and_get(parent, attr->name);
|
|
if (WARN_ON_ONCE(!kn))
|
|
return false;
|
|
|
|
ret = kernfs_remove_self(kn);
|
|
|
|
kernfs_put(kn);
|
|
return ret;
|
|
}
|
|
|
|
void sysfs_remove_files(struct kobject *kobj, const struct attribute **ptr)
|
|
{
|
|
int i;
|
|
for (i = 0; ptr[i]; i++)
|
|
sysfs_remove_file(kobj, ptr[i]);
|
|
}
|
|
EXPORT_SYMBOL_GPL(sysfs_remove_files);
|
|
|
|
/**
|
|
* sysfs_remove_file_from_group - remove an attribute file from a group.
|
|
* @kobj: object we're acting for.
|
|
* @attr: attribute descriptor.
|
|
* @group: group name.
|
|
*/
|
|
void sysfs_remove_file_from_group(struct kobject *kobj,
|
|
const struct attribute *attr, const char *group)
|
|
{
|
|
struct kernfs_node *parent;
|
|
|
|
if (group) {
|
|
parent = kernfs_find_and_get(kobj->sd, group);
|
|
} else {
|
|
parent = kobj->sd;
|
|
kernfs_get(parent);
|
|
}
|
|
|
|
if (parent) {
|
|
kernfs_remove_by_name(parent, attr->name);
|
|
kernfs_put(parent);
|
|
}
|
|
}
|
|
EXPORT_SYMBOL_GPL(sysfs_remove_file_from_group);
|
|
|
|
/**
|
|
* sysfs_create_bin_file - create binary file for object.
|
|
* @kobj: object.
|
|
* @attr: attribute descriptor.
|
|
*/
|
|
int sysfs_create_bin_file(struct kobject *kobj,
|
|
const struct bin_attribute *attr)
|
|
{
|
|
kuid_t uid;
|
|
kgid_t gid;
|
|
|
|
BUG_ON(!kobj || !kobj->sd || !attr);
|
|
|
|
kobject_get_ownership(kobj, &uid, &gid);
|
|
return sysfs_add_file_mode_ns(kobj->sd, &attr->attr, true,
|
|
attr->attr.mode, uid, gid, NULL);
|
|
}
|
|
EXPORT_SYMBOL_GPL(sysfs_create_bin_file);
|
|
|
|
/**
|
|
* sysfs_remove_bin_file - remove binary file for object.
|
|
* @kobj: object.
|
|
* @attr: attribute descriptor.
|
|
*/
|
|
void sysfs_remove_bin_file(struct kobject *kobj,
|
|
const struct bin_attribute *attr)
|
|
{
|
|
kernfs_remove_by_name(kobj->sd, attr->attr.name);
|
|
}
|
|
EXPORT_SYMBOL_GPL(sysfs_remove_bin_file);
|
|
|
|
/**
|
|
* sysfs_emit - scnprintf equivalent, aware of PAGE_SIZE buffer.
|
|
* @buf: start of PAGE_SIZE buffer.
|
|
* @fmt: format
|
|
* @...: optional arguments to @format
|
|
*
|
|
*
|
|
* Returns number of characters written to @buf.
|
|
*/
|
|
int sysfs_emit(char *buf, const char *fmt, ...)
|
|
{
|
|
va_list args;
|
|
int len;
|
|
|
|
if (WARN(!buf, "invalid sysfs_emit: buf:%p\n", buf))
|
|
return 0;
|
|
|
|
va_start(args, fmt);
|
|
len = vscnprintf(buf, PAGE_SIZE, fmt, args);
|
|
va_end(args);
|
|
|
|
return len;
|
|
}
|
|
EXPORT_SYMBOL_GPL(sysfs_emit);
|
|
|
|
/**
|
|
* sysfs_emit_at - scnprintf equivalent, aware of PAGE_SIZE buffer.
|
|
* @buf: start of PAGE_SIZE buffer.
|
|
* @at: offset in @buf to start write in bytes
|
|
* @at must be >= 0 && < PAGE_SIZE
|
|
* @fmt: format
|
|
* @...: optional arguments to @fmt
|
|
*
|
|
*
|
|
* Returns number of characters written starting at &@buf[@at].
|
|
*/
|
|
int sysfs_emit_at(char *buf, int at, const char *fmt, ...)
|
|
{
|
|
va_list args;
|
|
int len;
|
|
|
|
if (WARN(!buf || at < 0 || at >= PAGE_SIZE,
|
|
"invalid sysfs_emit_at: buf:%p at:%d\n", buf, at))
|
|
return 0;
|
|
|
|
va_start(args, fmt);
|
|
len = vscnprintf(buf + at, PAGE_SIZE - at, fmt, args);
|
|
va_end(args);
|
|
|
|
return len;
|
|
}
|
|
EXPORT_SYMBOL_GPL(sysfs_emit_at);
|