relayfs: add read() support
PATCH 1/1 relayfs: add read() support
At the kernel summit, there was some discussion of relayfs and the consensus was that it didn't make sense for relayfs to not implement read(). So here's a read implementation...
Andrew, please apply.
Tom
fs/relayfs/inode.c +156 -8
@@ -232,7 +232,7 @@ int relayfs_remove_dir(struct dentry *de* * Increments the channel buffer refcount. */ -int relayfs_open (struct inode *inode,struct file *filp) +static int relayfs_open (struct inode *inode,struct file *filp) {struct rchan_buf *buf =RELAYFS_I (inode)->buf;kref_get (&buf->kref);
@@ -247,7 +247,7 @@ int relayfs_open(struct inode *inode, st* * Calls upon relay_mmap_buf to map the file into user space. */ -int relayfs_mmap (struct file *filp,struct vm_area_struct *vma) +static int relayfs_mmap (struct file *filp,struct vm_area_struct *vma) {struct inode *inode = filp->f_dentry->d_inode;return relay_mmap_buf (RELAYFS_I (inode)->buf, vma);
@@ -260,7 +260,7 @@ int relayfs_mmap(struct file *filp, stru* * Poll implemention. */ -unsigned int relayfs_poll (struct file *filp,poll_table *wait) +static unsigned int relayfs_poll (struct file *filp,poll_table *wait) {unsigned int mask =0 ;struct inode *inode = filp->f_dentry->d_inode;
@@ -286,7 +286,7 @@ unsigned int relayfs_poll(struct file *f* Decrements the channel refcount, as the filesystem is * no longer using it. */ -int relayfs_release (struct inode *inode,struct file *filp) +static int relayfs_release (struct inode *inode,struct file *filp) {struct rchan_buf *buf =RELAYFS_I (inode)->buf;kref_put (&buf->kref, relay_remove_buf);
@@ -295,6 +295,157 @@ int relayfs_release(struct inode *inode, }/** +* relayfs_read_start - find the first available byte to read +* +* If the read_pos is in the middle of padding, return the +* position of the first actually available byte, otherwise +* return the original value. +*/ +static inline unsigned int relayfs_read_start (unsigned int read_pos, +unsigned int avail, +unsigned int start_subbuf, +struct rchan_buf *buf) +{ +unsigned int read_subbuf, adj_read_subbuf; +unsigned int padding, padding_start, padding_end; +unsigned int subbuf_size = buf->chan->subbuf_size; +unsigned int n_subbufs = buf->chan->n_subbufs; + + read_subbuf = read_pos / subbuf_size; + adj_read_subbuf = (read_subbuf + start_subbuf) % n_subbufs; + +if ((read_subbuf +1 ) * subbuf_size <= avail) { + padding = buf->padding[adj_read_subbuf]; + padding_start = (read_subbuf +1 ) * subbuf_size - padding; + padding_end = (read_subbuf +1 ) * subbuf_size; +if (read_pos >= padding_start && read_pos < padding_end) { + read_subbuf = (read_subbuf +1 ) % n_subbufs; + read_pos = read_subbuf * subbuf_size; + } + } + +return read_pos; +} + +/** +* relayfs_read_end - return the end of available bytes to read +* +* If the read_pos is in the middle of a full sub-buffer, return +* the padding-adjusted end of that sub-buffer, otherwise return +* the position after the last byte written to the buffer. At +* most, 1 sub-buffer can be read at a time. +* +*/ +static inline unsigned int relayfs_read_end (unsigned int read_pos, +unsigned int avail, +unsigned int start_subbuf, +struct rchan_buf *buf) +{ +unsigned int padding, read_endpos, buf_offset; +unsigned int read_subbuf, adj_read_subbuf; +unsigned int subbuf_size = buf->chan->subbuf_size; +unsigned int n_subbufs = buf->chan->n_subbufs; + + buf_offset = buf->offset > subbuf_size ? subbuf_size : buf->offset; + read_subbuf = read_pos / subbuf_size; + adj_read_subbuf = (read_subbuf + start_subbuf) % n_subbufs; + +if ((read_subbuf +1 ) * subbuf_size <= avail) { + padding = buf->padding[adj_read_subbuf]; + read_endpos = (read_subbuf +1 ) * subbuf_size - padding; + }else + read_endpos = read_subbuf * subbuf_size + buf_offset; + +return read_endpos; +} + +/** +* relayfs_read_avail - return total available along with buffer start +* +* Because buffers are circular, the 'beginning' of the buffer +* depends on where the buffer was last written. If the writer +* has cycled around the buffer, the beginning is defined to be +* the beginning of the sub-buffer following the last sub-buffer +* written to, otherwise it's the beginning of sub-buffer 0. +* +*/ +static inline unsigned int relayfs_read_avail (struct rchan_buf *buf, +unsigned int *start_subbuf) +{ +unsigned int avail, complete_subbufs, cur_subbuf, buf_offset; +unsigned int subbuf_size = buf->chan->subbuf_size; +unsigned int n_subbufs = buf->chan->n_subbufs; + + buf_offset = buf->offset > subbuf_size ? subbuf_size : buf->offset; + +if (buf->subbufs_produced >= n_subbufs) { + complete_subbufs = n_subbufs -1 ; + cur_subbuf = (buf->data - buf->start) / subbuf_size; + *start_subbuf = (cur_subbuf +1 ) % n_subbufs; + }else { + complete_subbufs = buf->subbufs_produced; + *start_subbuf =0 ; + } + + avail = complete_subbufs * subbuf_size + buf_offset; + +return avail; +} + +/** +* relayfs_read - read file op for relayfs files +* @filp: the file +* @buffer: the userspace buffer +* @count: number of bytes to read +* @ppos: position to read from +* +* Reads count bytes or the number of bytes available in the +* current sub-buffer being read, whichever is smaller. +* +* NOTE: The results of reading a relayfs file which is currently +* being written to are undefined. This is because the buffer is +* circular and an active writer in the kernel could be +* overwriting the data currently being read. Therefore read() +* is mainly useful for reading the contents of a buffer after +* logging has completed. +*/ +static ssize_t relayfs_read (struct file *filp, +char __user *buffer, +size_t count, +loff_t *ppos) +{ +struct inode *inode = filp->f_dentry->d_inode; +struct rchan_buf *buf =RELAYFS_I (inode)->buf; +unsigned int read_start, read_end, avail, start_subbuf; +unsigned int buf_size = buf->chan->subbuf_size * buf->chan->n_subbufs; +void *from; + + avail =relayfs_read_avail (buf, &start_subbuf); +if (*ppos >= avail) +return 0 ; + + read_start =relayfs_read_start (*ppos, avail, start_subbuf, buf); +if (read_start ==0 && *ppos) +return 0 ; + + read_end =relayfs_read_end (read_start, avail, start_subbuf, buf); +if (read_end == read_start) +return 0 ; + + from = buf->start + start_subbuf * buf->chan->subbuf_size + read_start; +if (from >= buf->start + buf_size) + from -= buf_size; + + count =min (count, read_end - read_start); +if (copy_to_user (buffer, from, count)) +return -EFAULT ; + + *ppos = read_start + count; + +return count; +} + +/** * relayfs alloc_inode() implementation */ static struct inode *relayfs_alloc_inode (struct super_block *sb)
@@ -329,6 +480,7 @@ struct file_operations relayfs_file_oper .open = relayfs_open, .poll = relayfs_poll, .mmap = relayfs_mmap, + .read = relayfs_read, .release = relayfs_release, };
@@ -404,10 +556,6 @@ static void __exit exit_relayfs_fs(void) module_init(init_relayfs_fs) module_exit(exit_relayfs_fs) -EXPORT_SYMBOL_GPL(relayfs_open); -EXPORT_SYMBOL_GPL(relayfs_poll); -EXPORT_SYMBOL_GPL(relayfs_mmap); -EXPORT_SYMBOL_GPL(relayfs_release); EXPORT_SYMBOL_GPL(relayfs_file_operations); EXPORT_SYMBOL_GPL(relayfs_create_dir); EXPORT_SYMBOL_GPL(relayfs_remove_dir);
Quoted 28 lines · Tom Zanussi
+static
ssize_trelayfs_read(struct file *filp,+ char __user *buffer, + size_t count, + loff_t *ppos)+{
+ struct inode *inode =filp->f_dentry->d_inode;
+ structrchan_buf*buf = RELAYFS_I(inode)->buf;
+ unsigned intread_start,read_end, avail,start_subbuf;
+ unsigned intbuf_size=buf->chan->subbuf_size*buf->chan->n_subbufs;
+ void *from;
+
+ avail =relayfs_read_avail(buf, &start_subbuf);
+ if (*ppos >= avail)
+ return 0;
+
+read_start=relayfs_read_start(*ppos, avail, start_subbuf, buf);
+ if (read_start== 0 && *ppos)
+ return 0;
+
+read_end=relayfs_read_end(read_start, avail, start_subbuf, buf);
+ if (read_end==read_start)
+ return 0;
+
+ from =buf->start+start_subbuf*buf->chan->subbuf_size+read_start;
+ if (from >=buf->start+buf_size)
+ from -=buf_size;
+
Quoted 8 lines
+ count =
min(count, read_end - read_start);
+ if (copy_to_user(buffer, from, count))
+ return-EFAULT;
+
+ *ppos =read_start+ count;
+
+ return count;
+}
The use of `unsigned int' in here will cause >4G reads to fail on 64-bit platforms. I think you want size_t throughout.
Quoted 3 lines · Andrew Morton
The use of `unsigned int' in here will cause >4G reads to fail on 64-bit platforms. I think you want
size_tthroughout.
OK, here's a patch that does that. It also switches to size_t for the buffer-related sizes elsewhere in relayfs.
This applies on top of the previous patches.
Tom
fs/relayfs/buffers.c +1 -1
@@ -137,7 +137,7 @@ struct rchan_buf *relay_create_buf(strucif (!buf)return NULL ; - buf->padding =kmalloc (chan->n_subbufs *sizeof (unsigned *),GFP_KERNEL ); + buf->padding =kmalloc (chan->n_subbufs *sizeof (size_t *),GFP_KERNEL );if (!buf->padding) goto free_buf;
fs/relayfs/inode.c +25 -25
75 lines not on this page · raw message
fs/relayfs/relay.c +8 -8
@@ -37,7 +37,7 @@ int relay_buf_empty(struct rchan_buf *bu*/ int relay_buf_full (struct rchan_buf *buf) { -unsigned int ready = buf->subbufs_produced - buf->subbufs_consumed; +size_t ready = buf->subbufs_produced - buf->subbufs_consumed;return (ready >= buf->chan->n_subbufs) ?1 :0 ; }
@@ -56,7 +56,7 @@ int relay_buf_full(struct rchan_buf *bufstatic int subbuf_start_default_callback (struct rchan_buf *buf,void *subbuf,void *prev_subbuf, -unsigned int prev_padding) +size_t prev_padding) {return 1 ; }
@@ -107,7 +107,7 @@ static void wakeup_readers(void *private*/ static inline void __relay_reset (struct rchan_buf *buf,unsigned int init) { -unsigned int i; +size_t i;if (init) {init_waitqueue_head (&buf->read_wait);
@@ -230,8 +230,8 @@ static inline void setup_callbacks(struc*/ struct rchan *relay_open (const char *base_filename,struct dentry *parent, -unsigned int subbuf_size, -unsigned int n_subbufs, +size_t subbuf_size, +size_t n_subbufs,struct rchan_callbacks *cb) {unsigned int i;
@@ -292,10 +292,10 @@ free_chan:* Performs sub-buffer-switch tasks such as invoking callbacks, * updating padding counts, waking up readers, etc. */ -unsigned int relay_switch_subbuf (struct rchan_buf *buf,unsigned int length) +size_t relay_switch_subbuf (struct rchan_buf *buf,size_t length) {void *old, *new; -unsigned int old_subbuf, new_subbuf; +size_t old_subbuf, new_subbuf;if (unlikely (length > buf->chan->subbuf_size)) goto toobig;
@@ -348,7 +348,7 @@ toobig:*/ void relay_subbufs_consumed (struct rchan *chan,unsigned int cpu, -unsigned int subbufs_consumed) +size_t subbufs_consumed) {struct rchan_buf *buf;
include/linux/relayfs_fs.h +18 -18
@@ -31,9 +31,9 @@ struct rchan_buf {void *start;/* start of channel buffer */ void *data;/* start of current sub-buffer */ -unsigned int offset;/* current offset into sub-buffer */ -unsigned int subbufs_produced;/* count of sub-buffers produced */ -unsigned int subbufs_consumed;/* count of sub-buffers consumed */ +size_t offset;/* current offset into sub-buffer */ +size_t subbufs_produced;/* count of sub-buffers produced */ +size_t subbufs_consumed;/* count of sub-buffers consumed */ struct rchan *chan;/* associated channel */ wait_queue_head_t read_wait;/* reader wait queue */ struct work_struct wake_readers;/* reader wake-up work struct */
@@ -42,8 +42,8 @@ struct rchan_bufstruct page **page_array;/* array of current buffer pages */ unsigned int page_count;/* number of current buffer pages */ unsigned int finalized;/* buffer has been finalized */ -unsigned *padding;/* padding counts per sub-buffer */ -unsigned int prev_padding;/* temporary variable */ +size_t *padding;/* padding counts per sub-buffer */ +size_t prev_padding;/* temporary variable */ }____cacheline_aligned ;/*
@@ -52,9 +52,9 @@ struct rchan_bufstruct rchan {u32 version;/* the version of this struct */ -unsigned int subbuf_size;/* sub-buffer size */ -unsigned int n_subbufs;/* number of sub-buffers per buffer */ -unsigned int alloc_size;/* total buffer size allocated */ +size_t subbuf_size;/* sub-buffer size */ +size_t n_subbufs;/* number of sub-buffers per buffer */ +size_t alloc_size;/* total buffer size allocated */ struct rchan_callbacks *cb;/* client callbacks */ struct kref kref;/* channel refcount */ void *private_data;/* for user-defined data */
@@ -100,7 +100,7 @@ struct rchan_callbacksint (*subbuf_start) (struct rchan_buf *buf,void *subbuf,void *prev_subbuf, -unsigned int prev_padding); +size_t prev_padding);/* * buf_mapped - relayfs buffer mmap notification
@@ -129,19 +129,19 @@ struct rchan_callbacksstruct rchan *relay_open (const char *base_filename,struct dentry *parent, -unsigned int subbuf_size, -unsigned int n_subbufs, +size_t subbuf_size, +size_t n_subbufs,struct rchan_callbacks *cb);extern void relay_close (struct rchan *chan);extern void relay_flush (struct rchan *chan);extern void relay_subbufs_consumed (struct rchan *chan,unsigned int cpu, -unsigned int consumed); +size_t consumed);extern void relay_reset (struct rchan *chan);extern int relay_buf_full (struct rchan_buf *buf); -extern unsigned int relay_switch_subbuf (struct rchan_buf *buf, -unsigned int length); +extern size_t relay_switch_subbuf (struct rchan_buf *buf, +size_t length);extern struct dentry *relayfs_create_dir (const char *name,struct dentry *parent);extern int relayfs_remove_dir (struct dentry *dentry);
@@ -161,7 +161,7 @@ extern int relayfs_remove_dir(struct den*/ static inline void relay_write (struct rchan *chan,const void *data, -unsigned int length) +size_t length) {unsigned long flags;struct rchan_buf *buf;
@@ -189,7 +189,7 @@ static inline void relay_write(struct rc*/ static inline void __relay_write (struct rchan *chan,const void *data, -unsigned int length) +size_t length) {struct rchan_buf *buf;
@@ -212,7 +212,7 @@ static inline void __relay_write(struct* Does not protect the buffer at all - caller must provide * appropriate synchronization. */ -static inline void *relay_reserve (struct rchan *chan,unsigned length) +static inline void *relay_reserve (struct rchan *chan,size_t length) {void *reserved;struct rchan_buf *buf = chan->buf[smp_processor_id ()];
@@ -237,7 +237,7 @@ static inline void *relay_reserve(struct* a sub-buffer in the subbuf_start() callback. */ static inline void subbuf_start_reserve (struct rchan_buf *buf, -unsigned int length) +size_t length) {BUG_ON (length >= buf->chan->subbuf_size -1 ); buf->offset = length;
Quoted 3 lines · Tom Zanussi
At the kernel summit, there was some discussion of relayfs and the consensus was that it didn't make sense for relayfs to not implement
read(). So here's a read implementation...
It needs a few fixes to actually compile without errors. This works for me, just tested with the block tracing stuff, works a charm!
fs/relayfs/inode.c renamed from opt/kernel/linux-2.6.13-rc4-mm1/fs/relayfs/inode.c +7 -5
@@ -33,6 +33,8 @@ static struct backing_dev_info relayfs_ .capabilities = BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_WRITEBACK, }; +struct file_operations relayfs_file_operations; +static struct inode *relayfs_get_inode (struct super_block *sb,int mode,struct rchan *chan) {
@@ -370,7 +372,7 @@ static inline unsigned int relayfs_read_* */ static inline unsigned int relayfs_read_avail (struct rchan_buf *buf, -unsigned int *start_subbuf) +size_t *start_subbuf) {unsigned int avail, complete_subbufs, cur_subbuf, buf_offset;unsigned int subbuf_size = buf->chan->subbuf_size;
@@ -378,12 +380,12 @@ static inline unsigned int relayfs_read_ buf_offset = buf->offset > subbuf_size ? subbuf_size : buf->offset; -if (buf->subbufs_produced >= n_subbufs) { +if (atomic_read (&buf->subbufs_produced) >= n_subbufs) { complete_subbufs = n_subbufs -1 ; cur_subbuf = (buf->data - buf->start) / subbuf_size; *start_subbuf = (cur_subbuf +1 ) % n_subbufs; }else { - complete_subbufs = buf->subbufs_produced; + complete_subbufs =atomic_read (&buf->subbufs_produced); *start_subbuf =0 ; }
@@ -416,8 +418,8 @@ static ssize_t relayfs_read(struct file {struct inode *inode = filp->f_dentry->d_inode;struct rchan_buf *buf =RELAYFS_I (inode)->buf; -unsigned int read_start, read_end, avail, start_subbuf; -unsigned int buf_size = buf->chan->subbuf_size * buf->chan->n_subbufs; +size_t read_start, read_end, avail, start_subbuf; +size_t buf_size = buf->chan->subbuf_size * buf->chan->n_subbufs;void *from; avail =relayfs_read_avail (buf, &start_subbuf);
include/linux/relayfs_fs.h renamed from opt/kernel/linux-2.6.13-rc4-mm1/include/linux/relayfs_fs.h +0 -10
@@ -253,15 +253,5 @@ static inline void *relay_reserve(struct return reserved; } -/* -* exported relayfs file operations, fs/relayfs/inode.c -*/ - -extern struct file_operations relayfs_file_operations; -extern int relayfs_open (struct inode *inode,struct file *filp); -extern unsigned int relayfs_poll (struct file *filp,poll_table *wait); -extern int relayfs_mmap (struct file *filp,struct vm_area_struct *vma); -extern int relayfs_release (struct inode *inode,struct file *filp); -#endif /* _LINUX_RELAYFS_FS_H */
Signature · 1 line
Jens Axboe
Quoted 7 lines · Jens Axboe
Quoted 3 lines · Tom Zanussi
At the kernel summit, there was some discussion of relayfs and the consensus was that it didn't make sense for relayfs to not implement
read(). So here's a read implementation...It needs a few fixes to actually compile without errors. This works for me, just tested with the block tracing stuff, works a charm!
Great, glad to hear it! I should have noted in the posting, though, that you should first apply the 'API cleanup' patch, in which case you shouldn't get the compile errors.
Tom
Quoted 6 lines · Tom Zanussi
Quoted 5 lines · Jens Axboe
Quoted 3 lines · Tom Zanussi
At the kernel summit, there was some discussion of relayfs and the consensus was that it didn't make sense for relayfs to not implement
read(). So here's a read implementation...
Quoted 6 lines
Quoted 2 lines
It needs a few fixes to actually compile without errors. This works for me, just tested with the block tracing stuff, works a charm!
Great, glad to hear it! I should have noted in the posting, though, that you should first apply the 'API cleanup' patch, in which case you shouldn't get the compile errors.
Ah, I see. The API is also much cleaner than what I looked at a few months ago (even before the cleanup).
Andrew, can you merge it pretty please?
Signature · 1 line
Jens Axboe
Quoted 11 lines · Jens Axboe
Quoted 9 lines · Tom Zanussi
Quoted 7 lines · Jens Axboe
Quoted 3 lines · Tom Zanussi
At the kernel summit, there was some discussion of relayfs and the consensus was that it didn't make sense for relayfs to not implement
read(). So here's a read implementation...It needs a few fixes to actually compile without errors. This works for me, just tested with the block tracing stuff, works a charm!
Quoted 8 lines
Quoted 3 lines
Great, glad to hear it! I should have noted in the posting, though, that you should first apply the 'API cleanup' patch, in which case you shouldn't get the compile errors.
Ah, I see. The API is also much cleaner than what I looked at a few months ago (even before the cleanup).
Andrew, can you merge it pretty please?
yup, we'll add relayfs to 2.6.14.