procfs: fix numbering in /proc/locks
Subsystems (2)
PATCH 1/1 procfs: fix numbering in /proc/locks
The lock number in /proc/locks (first field) is implemented by a counter (private field of struct seq_file) which is incremented at each call of locks_show(). Currently it is reset each time locks_start() is called, that is each time we call the read() syscall on /proc/locks. Because of that, the numbering erratically restarts at 1 several times when reading a long /proc/locks file.
We want the counter to be initialized at opening time and then never reset until we close the file. Fortunately, seq_open() memzeros the seq_file structure, so we can just drop the reset in locks_start() and move the increment the counter before actually printing the line so the numbering still starts at 1.
---
fs/locks.c +1 -2
@@ -2161,19 +2161,18 @@ static int locks_show(struct seq_file *f, void *v) fl = list_entry(v, struct file_lock, fl_link); + f->private++;lock_get_status (f, fl, (long )f->private,"" );list_for_each_entry (bfl, &fl->fl_block, fl_block)lock_get_status (f, bfl, (long )f->private," ->" ); - f->private++;return 0 ; }static void *locks_start (struct seq_file *f,loff_t *pos) {lock_kernel (); - f->private = (void *)1 ;return seq_list_start (&file_lock_list, *pos); }
Quoted 12 lines · Jerome Marchand
The lock number in
/proc/locks(first field) is implemented by a counter (private field of structseq_file) which is incremented at each call oflocks_show(). Currently it is reset each timelocks_start()is called, that is each time we call theread()syscall on/proc/locks. Because of that, the numbering erratically restarts at 1 several times when reading a long/proc/locksfile.
We want the counter to be initialized at opening time and then never reset until we close the file. Fortunately,seq_open()memzeros theseq_filestructure, so we can just drop the reset inlocks_start()and move the increment the counter before actually printing the line so the numbering still starts at 1.
IMHO the implementation is wrong. If you want the proper sequence number while file is open you should increase on in the ->next callback of the seq_ops, not in show.
Quoted 28 lines
Signed-off-by Jerome Marchand <[email protected]>---
fs/locks.c +1
-2@@ -2161,19 +2161,18 @@ static int locks_show(struct seq_file *f, void *v) fl = list_entry(v, struct file_lock, fl_link); + f->private++;lock_get_status (f, fl, (long )f->private,"" );list_for_each_entry (bfl, &fl->fl_block, fl_block)lock_get_status (f, bfl, (long )f->private," ->" ); - f->private++;return 0 ; }static void *locks_start (struct seq_file *f,loff_t *pos) {lock_kernel (); - f->private = (void *)1 ;return seq_list_start (&file_lock_list, *pos); }
Quoted 14 lines · Pavel Emelyanov
Quoted 12 lines · Jerome Marchand
The lock number in
/proc/locks(first field) is implemented by a counter (private field of structseq_file) which is incremented at each call oflocks_show(). Currently it is reset each timelocks_start()is called, that is each time we call theread()syscall on/proc/locks. Because of that, the numbering erratically restarts at 1 several times when reading a long/proc/locksfile.
We want the counter to be initialized at opening time and then never reset until we close the file. Fortunately,seq_open()memzeros theseq_filestructure, so we can just drop the reset inlocks_start()and move the increment the counter before actually printing the line so the numbering still starts at 1.
Quoted 3 lines
IMHO the implementation is wrong. If you want the proper sequence number while file is open you should increase on in the ->next callback of the
seq_ops, not in show.
Good point. My implementation is definitely wrong. But I'm afraid that moving the increment in locks_next() won't help either. It will fail when a program do something more than just read the file sequentially (use of lseek() for instance). We need a better way to keep track of the current position in the list.
Thanks,
Jerome
Quoted 5 lines
Good point. My implementation is definitely wrong. But I'm afraid that moving the increment in
locks_next()won't help either. It will fail when a program do something more than just read the file sequentially (use oflseek()for instance). We need a better way to keep track of the current position in the list.
The seq files core implementation knows about the lseek and calls the seq_ops callbacks properly.
Quoted 4 lines
Thanks,
Jerome
Quoted 9 lines · Pavel Emelyanov
Quoted 5 lines
Good point. My implementation is definitely wrong. But I'm afraid that moving the increment in
locks_next()won't help either. It will fail when a program do something more than just read the file sequentially (use oflseek()for instance). We need a better way to keep track of the current position in the list.The seq files core implementation knows about the lseek and calls the
seq_opscallbacks properly.
Yes, but if read a few lines and then lseek() back. I'm afraid it will call a few more locks_next() function and thus increase the counter again.
Quoted 7 lines · Jerome Marchand
Quoted 6 lines · Pavel Emelyanov
Quoted 5 lines
Good point. My implementation is definitely wrong. But I'm afraid that moving the increment in
locks_next()won't help either. It will fail when a program do something more than just read the file sequentially (use oflseek()for instance). We need a better way to keep track of the current position in the list.
Quoted 6 lines
Quoted 3 lines
The seq files core implementation knows about the lseek and calls the
seq_opscallbacks properly.Yes, but if read a few lines and then
lseek()back. I'm afraid it will call a few morelocks_next()function and thus increase the counter again.
No. If you lseek back it calls the locks_start which should reset the counter, and then will call locks_next.
Can you try out my proposal and check whether it really works as expected?
Thanks,
Pavel
Quoted 12 lines · Pavel Emelyanov
Quoted 11 lines · Jerome Marchand
Quoted 9 lines · Pavel Emelyanov
Quoted 5 lines
Good point. My implementation is definitely wrong. But I'm afraid that moving the increment in
locks_next()won't help either. It will fail when a program do something more than just read the file sequentially (use oflseek()for instance). We need a better way to keep track of the current position in the list.The seq files core implementation knows about the lseek and calls the
seq_opscallbacks properly.
Quoted 8 lines
Quoted 2 lines
Yes, but if read a few lines and then
lseek()back. I'm afraid it will call a few morelocks_next()function and thus increase the counter again.No. If you lseek back it calls the
locks_startwhich should reset the counter, and then will calllocks_next.Can you try out my proposal and check whether it really works as expected?
OK. I didn't understand you meant to keep the reset in locks_start(). I'll try that.
Jerome