Skip to content

Commit 6a727b4

Browse files
Jiri Slabyrjwysocki
authored andcommitted
FS / libfs: Implement simple_write_to_buffer
It will be used in suspend code and serves as an easy wrap around copy_from_user. Similar to simple_read_from_buffer, it takes care of transfers with proper lengths depending on available and count parameters and advances ppos appropriately. Signed-off-by: Jiri Slaby <[email protected]> Signed-off-by: Rafael J. Wysocki <[email protected]>
1 parent bc6a0cb commit 6a727b4

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

fs/libfs.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,40 @@ ssize_t simple_read_from_buffer(void __user *to, size_t count, loff_t *ppos,
546546
return count;
547547
}
548548

549+
/**
550+
* simple_write_to_buffer - copy data from user space to the buffer
551+
* @to: the buffer to write to
552+
* @available: the size of the buffer
553+
* @ppos: the current position in the buffer
554+
* @from: the user space buffer to read from
555+
* @count: the maximum number of bytes to read
556+
*
557+
* The simple_write_to_buffer() function reads up to @count bytes from the user
558+
* space address starting at @from into the buffer @to at offset @ppos.
559+
*
560+
* On success, the number of bytes written is returned and the offset @ppos is
561+
* advanced by this number, or negative value is returned on error.
562+
**/
563+
ssize_t simple_write_to_buffer(void *to, size_t available, loff_t *ppos,
564+
const void __user *from, size_t count)
565+
{
566+
loff_t pos = *ppos;
567+
size_t res;
568+
569+
if (pos < 0)
570+
return -EINVAL;
571+
if (pos >= available || !count)
572+
return 0;
573+
if (count > available - pos)
574+
count = available - pos;
575+
res = copy_from_user(to + pos, from, count);
576+
if (res == count)
577+
return -EFAULT;
578+
count -= res;
579+
*ppos = pos + count;
580+
return count;
581+
}
582+
549583
/**
550584
* memory_read_from_buffer - copy data from the buffer
551585
* @to: the kernel space buffer to read to
@@ -864,6 +898,7 @@ EXPORT_SYMBOL(simple_statfs);
864898
EXPORT_SYMBOL(simple_sync_file);
865899
EXPORT_SYMBOL(simple_unlink);
866900
EXPORT_SYMBOL(simple_read_from_buffer);
901+
EXPORT_SYMBOL(simple_write_to_buffer);
867902
EXPORT_SYMBOL(memory_read_from_buffer);
868903
EXPORT_SYMBOL(simple_transaction_set);
869904
EXPORT_SYMBOL(simple_transaction_get);

include/linux/fs.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2362,6 +2362,8 @@ extern void simple_release_fs(struct vfsmount **mount, int *count);
23622362

23632363
extern ssize_t simple_read_from_buffer(void __user *to, size_t count,
23642364
loff_t *ppos, const void *from, size_t available);
2365+
extern ssize_t simple_write_to_buffer(void *to, size_t available, loff_t *ppos,
2366+
const void __user *from, size_t count);
23652367

23662368
extern int simple_fsync(struct file *, struct dentry *, int);
23672369

0 commit comments

Comments
 (0)