Skip to content

Commit ce7a7ee

Browse files
neilbrownJonathan Corbet
authored andcommitted
doc: seq_file: clarify role of *pos in ->next()
There are behavioural requirements on the seq_file next() function in terms of how it updates *pos at end-of-file, and these are now enforced by a warning. I was recently attempting to justify the reason this was needed, and couldn't remember the details, and didn't find them in the documentation. So I re-read the code until I understood it again, and updated the documentation to match. I also enhanced the text about SEQ_START_TOKEN as it seemed potentially misleading. Cc: Vasily Averin <vvs@virtuozzo.com> Signed-off-by: NeilBrown <neilb@suse.de> Link: https://lore.kernel.org/r/87eemqiazh.fsf@notabene.neil.brown.name Signed-off-by: Jonathan Corbet <corbet@lwn.net>
1 parent e0bc9cf commit ce7a7ee

1 file changed

Lines changed: 19 additions & 1 deletion

File tree

Documentation/filesystems/seq_file.rst

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,9 @@ also a special value which can be returned by the start() function
129129
called SEQ_START_TOKEN; it can be used if you wish to instruct your
130130
show() function (described below) to print a header at the top of the
131131
output. SEQ_START_TOKEN should only be used if the offset is zero,
132-
however.
132+
however. SEQ_START_TOKEN has no special meaning to the core seq_file
133+
code. It is provided as a convenience for a start() funciton to
134+
communicate with the next() and show() functions.
133135

134136
The next function to implement is called, amazingly, next(); its job is to
135137
move the iterator forward to the next position in the sequence. The
@@ -145,6 +147,22 @@ complete. Here's the example version::
145147
return spos;
146148
}
147149

150+
The next() function should set ``*pos`` to a value that start() can use
151+
to find the new location in the sequence. When the iterator is being
152+
stored in the private data area, rather than being reinitialized on each
153+
start(), it might seem sufficient to simply set ``*pos`` to any non-zero
154+
value (zero always tells start() to restart the sequence). This is not
155+
sufficient due to historical problems.
156+
157+
Historically, many next() functions have *not* updated ``*pos`` at
158+
end-of-file. If the value is then used by start() to initialise the
159+
iterator, this can result in corner cases where the last entry in the
160+
sequence is reported twice in the file. In order to discourage this bug
161+
from being resurrected, the core seq_file code now produces a warning if
162+
a next() function does not change the value of ``*pos``. Consequently a
163+
next() function *must* change the value of ``*pos``, and of course must
164+
set it to a non-zero value.
165+
148166
The stop() function closes a session; its job, of course, is to clean
149167
up. If dynamic memory is allocated for the iterator, stop() is the
150168
place to free it; if a lock was taken by start(), stop() must release

0 commit comments

Comments
 (0)