]> git.karo-electronics.de Git - karo-tx-linux.git/blobdiff - fs/sysfs/file.c
arm: imx6: defconfig: update tx6 defconfigs
[karo-tx-linux.git] / fs / sysfs / file.c
index 02797a134cf8dd50800287f6f9f1efdcef239a72..c3795978b40452772f3dae70194676533bcf9832 100644 (file)
@@ -76,7 +76,8 @@ static const struct sysfs_ops *sysfs_file_ops(struct sysfs_dirent *sd)
 {
        struct kobject *kobj = sd->s_parent->s_dir.kobj;
 
-       lockdep_assert_held(sd);
+       if (!sysfs_ignore_lockdep(sd))
+               lockdep_assert_held(sd);
        return kobj->ktype ? kobj->ktype->sysfs_ops : NULL;
 }
 
@@ -155,7 +156,7 @@ static ssize_t sysfs_bin_read(struct file *file, char __user *userbuf,
        struct sysfs_open_file *of = sysfs_of(file);
        struct bin_attribute *battr = of->sd->s_bin_attr.bin_attr;
        struct kobject *kobj = of->sd->s_parent->s_dir.kobj;
-       int size = file_inode(file)->i_size;
+       loff_t size = file_inode(file)->i_size;
        int count = min_t(size_t, bytes, PAGE_SIZE);
        loff_t offs = *off;
        char *buf;
@@ -274,11 +275,10 @@ static ssize_t sysfs_write_file(struct file *file, const char __user *user_buf,
 {
        struct sysfs_open_file *of = sysfs_of(file);
        ssize_t len = min_t(size_t, count, PAGE_SIZE);
+       loff_t size = file_inode(file)->i_size;
        char *buf;
 
-       if (sysfs_is_bin(of->sd)) {
-               loff_t size = file_inode(file)->i_size;
-
+       if (sysfs_is_bin(of->sd) && size) {
                if (size <= *ppos)
                        return 0;
                len = min_t(ssize_t, len, size - *ppos);
@@ -610,38 +610,40 @@ static int sysfs_open_file(struct inode *inode, struct file *file)
        struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
        struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
        struct sysfs_open_file *of;
-       const struct sysfs_ops *ops;
+       bool has_read, has_write;
        int error = -EACCES;
 
        /* need attr_sd for attr and ops, its parent for kobj */
        if (!sysfs_get_active(attr_sd))
                return -ENODEV;
 
-       /* every kobject with an attribute needs a ktype assigned */
-       ops = sysfs_file_ops(attr_sd);
-       if (WARN(!ops, KERN_ERR
-                "missing sysfs attribute operations for kobject: %s\n",
-                kobject_name(kobj)))
-               goto err_out;
+       if (sysfs_is_bin(attr_sd)) {
+               struct bin_attribute *battr = attr_sd->s_bin_attr.bin_attr;
 
-       /* File needs write support.
-        * The inode's perms must say it's ok,
-        * and we must have a store method.
-        */
-       if (file->f_mode & FMODE_WRITE) {
-               if (!(inode->i_mode & S_IWUGO) || !ops->store)
-                       goto err_out;
-       }
+               has_read = battr->read || battr->mmap;
+               has_write = battr->write || battr->mmap;
+       } else {
+               const struct sysfs_ops *ops = sysfs_file_ops(attr_sd);
 
-       /* File needs read support.
-        * The inode's perms must say it's ok, and we there
-        * must be a show method for it.
-        */
-       if (file->f_mode & FMODE_READ) {
-               if (!(inode->i_mode & S_IRUGO) || !ops->show)
+               /* every kobject with an attribute needs a ktype assigned */
+               if (WARN(!ops, KERN_ERR
+                        "missing sysfs attribute operations for kobject: %s\n",
+                        kobject_name(kobj)))
                        goto err_out;
+
+               has_read = ops->show;
+               has_write = ops->store;
        }
 
+       /* check perms and supported operations */
+       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 sysfs_open_file for the file */
        error = -ENOMEM;
        of = kzalloc(sizeof(struct sysfs_open_file), GFP_KERNEL);
@@ -653,11 +655,14 @@ static int sysfs_open_file(struct inode *inode, struct file *file)
        of->file = file;
 
        /*
-        * Always instantiate seq_file even if read access is not
-        * implemented or requested.  This unifies private data access and
-        * most files are readable anyway.
+        * 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.
         */
-       error = single_open(file, sysfs_seq_show, of);
+       if (sysfs_is_bin(attr_sd))
+               error = single_open(file, NULL, of);
+       else
+               error = single_open(file, sysfs_seq_show, of);
        if (error)
                goto err_free;
 
@@ -807,6 +812,9 @@ const struct file_operations sysfs_bin_operations = {
        .write          = sysfs_write_file,
        .llseek         = generic_file_llseek,
        .mmap           = sysfs_bin_mmap,
+       .open           = sysfs_open_file,
+       .release        = sysfs_release,
+       .poll           = sysfs_poll,
 };
 
 int sysfs_add_file_mode_ns(struct sysfs_dirent *dir_sd,
@@ -979,6 +987,32 @@ void sysfs_remove_file_from_group(struct kobject *kobj,
 }
 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)
+{
+       BUG_ON(!kobj || !kobj->sd || !attr);
+
+       return sysfs_add_file(kobj->sd, &attr->attr, SYSFS_KOBJ_BIN_ATTR);
+}
+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)
+{
+       sysfs_hash_and_remove(kobj->sd, attr->attr.name, NULL);
+}
+EXPORT_SYMBOL_GPL(sysfs_remove_bin_file);
+
 struct sysfs_schedule_callback_struct {
        struct list_head        workq_list;
        struct kobject          *kobj;