153a41d3eSMauro Carvalho Chehab.. SPDX-License-Identifier: GPL-2.0
253a41d3eSMauro Carvalho Chehab
353a41d3eSMauro Carvalho Chehab======================
453a41d3eSMauro Carvalho ChehabThe seq_file Interface
553a41d3eSMauro Carvalho Chehab======================
653a41d3eSMauro Carvalho Chehab
753a41d3eSMauro Carvalho Chehab	Copyright 2003 Jonathan Corbet <corbet@lwn.net>
853a41d3eSMauro Carvalho Chehab
953a41d3eSMauro Carvalho Chehab	This file is originally from the LWN.net Driver Porting series at
1093431e06SAlexander A. Klimov	https://lwn.net/Articles/driver-porting/
1153a41d3eSMauro Carvalho Chehab
1253a41d3eSMauro Carvalho Chehab
1353a41d3eSMauro Carvalho ChehabThere are numerous ways for a device driver (or other kernel component) to
1453a41d3eSMauro Carvalho Chehabprovide information to the user or system administrator.  One useful
1553a41d3eSMauro Carvalho Chehabtechnique is the creation of virtual files, in debugfs, /proc or elsewhere.
1653a41d3eSMauro Carvalho ChehabVirtual files can provide human-readable output that is easy to get at
1753a41d3eSMauro Carvalho Chehabwithout any special utility programs; they can also make life easier for
1853a41d3eSMauro Carvalho Chehabscript writers. It is not surprising that the use of virtual files has
1953a41d3eSMauro Carvalho Chehabgrown over the years.
2053a41d3eSMauro Carvalho Chehab
2153a41d3eSMauro Carvalho ChehabCreating those files correctly has always been a bit of a challenge,
2253a41d3eSMauro Carvalho Chehabhowever. It is not that hard to make a virtual file which returns a
2353a41d3eSMauro Carvalho Chehabstring. But life gets trickier if the output is long - anything greater
2453a41d3eSMauro Carvalho Chehabthan an application is likely to read in a single operation.  Handling
2553a41d3eSMauro Carvalho Chehabmultiple reads (and seeks) requires careful attention to the reader's
2653a41d3eSMauro Carvalho Chehabposition within the virtual file - that position is, likely as not, in the
2753a41d3eSMauro Carvalho Chehabmiddle of a line of output. The kernel has traditionally had a number of
2853a41d3eSMauro Carvalho Chehabimplementations that got this wrong.
2953a41d3eSMauro Carvalho Chehab
3053a41d3eSMauro Carvalho ChehabThe 2.6 kernel contains a set of functions (implemented by Alexander Viro)
3153a41d3eSMauro Carvalho Chehabwhich are designed to make it easy for virtual file creators to get it
3253a41d3eSMauro Carvalho Chehabright.
3353a41d3eSMauro Carvalho Chehab
3453a41d3eSMauro Carvalho ChehabThe seq_file interface is available via <linux/seq_file.h>. There are
3553a41d3eSMauro Carvalho Chehabthree aspects to seq_file:
3653a41d3eSMauro Carvalho Chehab
3753a41d3eSMauro Carvalho Chehab     * An iterator interface which lets a virtual file implementation
3853a41d3eSMauro Carvalho Chehab       step through the objects it is presenting.
3953a41d3eSMauro Carvalho Chehab
4053a41d3eSMauro Carvalho Chehab     * Some utility functions for formatting objects for output without
4153a41d3eSMauro Carvalho Chehab       needing to worry about things like output buffers.
4253a41d3eSMauro Carvalho Chehab
4353a41d3eSMauro Carvalho Chehab     * A set of canned file_operations which implement most operations on
4453a41d3eSMauro Carvalho Chehab       the virtual file.
4553a41d3eSMauro Carvalho Chehab
4653a41d3eSMauro Carvalho ChehabWe'll look at the seq_file interface via an extremely simple example: a
4753a41d3eSMauro Carvalho Chehabloadable module which creates a file called /proc/sequence. The file, when
4853a41d3eSMauro Carvalho Chehabread, simply produces a set of increasing integer values, one per line. The
4953a41d3eSMauro Carvalho Chehabsequence will continue until the user loses patience and finds something
5053a41d3eSMauro Carvalho Chehabbetter to do. The file is seekable, in that one can do something like the
5153a41d3eSMauro Carvalho Chehabfollowing::
5253a41d3eSMauro Carvalho Chehab
5353a41d3eSMauro Carvalho Chehab    dd if=/proc/sequence of=out1 count=1
5453a41d3eSMauro Carvalho Chehab    dd if=/proc/sequence skip=1 of=out2 count=1
5553a41d3eSMauro Carvalho Chehab
5653a41d3eSMauro Carvalho ChehabThen concatenate the output files out1 and out2 and get the right
5753a41d3eSMauro Carvalho Chehabresult. Yes, it is a thoroughly useless module, but the point is to show
5853a41d3eSMauro Carvalho Chehabhow the mechanism works without getting lost in other details.  (Those
5953a41d3eSMauro Carvalho Chehabwanting to see the full source for this module can find it at
6093431e06SAlexander A. Klimovhttps://lwn.net/Articles/22359/).
6153a41d3eSMauro Carvalho Chehab
6253a41d3eSMauro Carvalho ChehabDeprecated create_proc_entry
6353a41d3eSMauro Carvalho Chehab============================
6453a41d3eSMauro Carvalho Chehab
6553a41d3eSMauro Carvalho ChehabNote that the above article uses create_proc_entry which was removed in
6653a41d3eSMauro Carvalho Chehabkernel 3.10. Current versions require the following update::
6753a41d3eSMauro Carvalho Chehab
6853a41d3eSMauro Carvalho Chehab    -	entry = create_proc_entry("sequence", 0, NULL);
6953a41d3eSMauro Carvalho Chehab    -	if (entry)
7053a41d3eSMauro Carvalho Chehab    -		entry->proc_fops = &ct_file_ops;
7153a41d3eSMauro Carvalho Chehab    +	entry = proc_create("sequence", 0, NULL, &ct_file_ops);
7253a41d3eSMauro Carvalho Chehab
7353a41d3eSMauro Carvalho ChehabThe iterator interface
7453a41d3eSMauro Carvalho Chehab======================
7553a41d3eSMauro Carvalho Chehab
7653a41d3eSMauro Carvalho ChehabModules implementing a virtual file with seq_file must implement an
7753a41d3eSMauro Carvalho Chehabiterator object that allows stepping through the data of interest
7853a41d3eSMauro Carvalho Chehabduring a "session" (roughly one read() system call).  If the iterator
7953a41d3eSMauro Carvalho Chehabis able to move to a specific position - like the file they implement,
8053a41d3eSMauro Carvalho Chehabthough with freedom to map the position number to a sequence location
8153a41d3eSMauro Carvalho Chehabin whatever way is convenient - the iterator need only exist
8253a41d3eSMauro Carvalho Chehabtransiently during a session.  If the iterator cannot easily find a
8353a41d3eSMauro Carvalho Chehabnumerical position but works well with a first/next interface, the
8453a41d3eSMauro Carvalho Chehabiterator can be stored in the private data area and continue from one
8553a41d3eSMauro Carvalho Chehabsession to the next.
8653a41d3eSMauro Carvalho Chehab
8753a41d3eSMauro Carvalho ChehabA seq_file implementation that is formatting firewall rules from a
8853a41d3eSMauro Carvalho Chehabtable, for example, could provide a simple iterator that interprets
8953a41d3eSMauro Carvalho Chehabposition N as the Nth rule in the chain.  A seq_file implementation
9053a41d3eSMauro Carvalho Chehabthat presents the content of a, potentially volatile, linked list
9153a41d3eSMauro Carvalho Chehabmight record a pointer into that list, providing that can be done
9253a41d3eSMauro Carvalho Chehabwithout risk of the current location being removed.
9353a41d3eSMauro Carvalho Chehab
9453a41d3eSMauro Carvalho ChehabPositioning can thus be done in whatever way makes the most sense for
9553a41d3eSMauro Carvalho Chehabthe generator of the data, which need not be aware of how a position
9653a41d3eSMauro Carvalho Chehabtranslates to an offset in the virtual file. The one obvious exception
9753a41d3eSMauro Carvalho Chehabis that a position of zero should indicate the beginning of the file.
9853a41d3eSMauro Carvalho Chehab
9953a41d3eSMauro Carvalho ChehabThe /proc/sequence iterator just uses the count of the next number it
10053a41d3eSMauro Carvalho Chehabwill output as its position.
10153a41d3eSMauro Carvalho Chehab
10253a41d3eSMauro Carvalho ChehabFour functions must be implemented to make the iterator work. The
10353a41d3eSMauro Carvalho Chehabfirst, called start(), starts a session and takes a position as an
10453a41d3eSMauro Carvalho Chehabargument, returning an iterator which will start reading at that
10553a41d3eSMauro Carvalho Chehabposition.  The pos passed to start() will always be either zero, or
10653a41d3eSMauro Carvalho Chehabthe most recent pos used in the previous session.
10753a41d3eSMauro Carvalho Chehab
10853a41d3eSMauro Carvalho ChehabFor our simple sequence example,
10953a41d3eSMauro Carvalho Chehabthe start() function looks like::
11053a41d3eSMauro Carvalho Chehab
11153a41d3eSMauro Carvalho Chehab	static void *ct_seq_start(struct seq_file *s, loff_t *pos)
11253a41d3eSMauro Carvalho Chehab	{
11353a41d3eSMauro Carvalho Chehab	        loff_t *spos = kmalloc(sizeof(loff_t), GFP_KERNEL);
11453a41d3eSMauro Carvalho Chehab	        if (! spos)
11553a41d3eSMauro Carvalho Chehab	                return NULL;
11653a41d3eSMauro Carvalho Chehab	        *spos = *pos;
11753a41d3eSMauro Carvalho Chehab	        return spos;
11853a41d3eSMauro Carvalho Chehab	}
11953a41d3eSMauro Carvalho Chehab
12053a41d3eSMauro Carvalho ChehabThe entire data structure for this iterator is a single loff_t value
12153a41d3eSMauro Carvalho Chehabholding the current position. There is no upper bound for the sequence
12253a41d3eSMauro Carvalho Chehabiterator, but that will not be the case for most other seq_file
12353a41d3eSMauro Carvalho Chehabimplementations; in most cases the start() function should check for a
12453a41d3eSMauro Carvalho Chehab"past end of file" condition and return NULL if need be.
12553a41d3eSMauro Carvalho Chehab
12653a41d3eSMauro Carvalho ChehabFor more complicated applications, the private field of the seq_file
12753a41d3eSMauro Carvalho Chehabstructure can be used to hold state from session to session.  There is
12853a41d3eSMauro Carvalho Chehabalso a special value which can be returned by the start() function
12953a41d3eSMauro Carvalho Chehabcalled SEQ_START_TOKEN; it can be used if you wish to instruct your
13053a41d3eSMauro Carvalho Chehabshow() function (described below) to print a header at the top of the
13153a41d3eSMauro Carvalho Chehaboutput. SEQ_START_TOKEN should only be used if the offset is zero,
132ce7a7eedSNeilBrownhowever.  SEQ_START_TOKEN has no special meaning to the core seq_file
133*d56b699dSBjorn Helgaascode.  It is provided as a convenience for a start() function to
134ce7a7eedSNeilBrowncommunicate with the next() and show() functions.
13553a41d3eSMauro Carvalho Chehab
13653a41d3eSMauro Carvalho ChehabThe next function to implement is called, amazingly, next(); its job is to
13753a41d3eSMauro Carvalho Chehabmove the iterator forward to the next position in the sequence.  The
13853a41d3eSMauro Carvalho Chehabexample module can simply increment the position by one; more useful
13953a41d3eSMauro Carvalho Chehabmodules will do what is needed to step through some data structure. The
14053a41d3eSMauro Carvalho Chehabnext() function returns a new iterator, or NULL if the sequence is
14153a41d3eSMauro Carvalho Chehabcomplete. Here's the example version::
14253a41d3eSMauro Carvalho Chehab
14353a41d3eSMauro Carvalho Chehab	static void *ct_seq_next(struct seq_file *s, void *v, loff_t *pos)
14453a41d3eSMauro Carvalho Chehab	{
14553a41d3eSMauro Carvalho Chehab	        loff_t *spos = v;
14653a41d3eSMauro Carvalho Chehab	        *pos = ++*spos;
14753a41d3eSMauro Carvalho Chehab	        return spos;
14853a41d3eSMauro Carvalho Chehab	}
14953a41d3eSMauro Carvalho Chehab
150ce7a7eedSNeilBrownThe next() function should set ``*pos`` to a value that start() can use
151ce7a7eedSNeilBrownto find the new location in the sequence.  When the iterator is being
152ce7a7eedSNeilBrownstored in the private data area, rather than being reinitialized on each
153ce7a7eedSNeilBrownstart(), it might seem sufficient to simply set ``*pos`` to any non-zero
154ce7a7eedSNeilBrownvalue (zero always tells start() to restart the sequence).  This is not
155ce7a7eedSNeilBrownsufficient due to historical problems.
156ce7a7eedSNeilBrown
157ce7a7eedSNeilBrownHistorically, many next() functions have *not* updated ``*pos`` at
158ce7a7eedSNeilBrownend-of-file.  If the value is then used by start() to initialise the
159ce7a7eedSNeilBrowniterator, this can result in corner cases where the last entry in the
160ce7a7eedSNeilBrownsequence is reported twice in the file.  In order to discourage this bug
161ce7a7eedSNeilBrownfrom being resurrected, the core seq_file code now produces a warning if
162ce7a7eedSNeilBrowna next() function does not change the value of ``*pos``.  Consequently a
163ce7a7eedSNeilBrownnext() function *must* change the value of ``*pos``, and of course must
164ce7a7eedSNeilBrownset it to a non-zero value.
165ce7a7eedSNeilBrown
16653a41d3eSMauro Carvalho ChehabThe stop() function closes a session; its job, of course, is to clean
16753a41d3eSMauro Carvalho Chehabup. If dynamic memory is allocated for the iterator, stop() is the
16853a41d3eSMauro Carvalho Chehabplace to free it; if a lock was taken by start(), stop() must release
16953a41d3eSMauro Carvalho Chehabthat lock.  The value that ``*pos`` was set to by the last next() call
17053a41d3eSMauro Carvalho Chehabbefore stop() is remembered, and used for the first start() call of
17153a41d3eSMauro Carvalho Chehabthe next session unless lseek() has been called on the file; in that
17253a41d3eSMauro Carvalho Chehabcase next start() will be asked to start at position zero::
17353a41d3eSMauro Carvalho Chehab
17453a41d3eSMauro Carvalho Chehab	static void ct_seq_stop(struct seq_file *s, void *v)
17553a41d3eSMauro Carvalho Chehab	{
17653a41d3eSMauro Carvalho Chehab	        kfree(v);
17753a41d3eSMauro Carvalho Chehab	}
17853a41d3eSMauro Carvalho Chehab
17953a41d3eSMauro Carvalho ChehabFinally, the show() function should format the object currently pointed to
18053a41d3eSMauro Carvalho Chehabby the iterator for output.  The example module's show() function is::
18153a41d3eSMauro Carvalho Chehab
18253a41d3eSMauro Carvalho Chehab	static int ct_seq_show(struct seq_file *s, void *v)
18353a41d3eSMauro Carvalho Chehab	{
18453a41d3eSMauro Carvalho Chehab	        loff_t *spos = v;
18553a41d3eSMauro Carvalho Chehab	        seq_printf(s, "%lld\n", (long long)*spos);
18653a41d3eSMauro Carvalho Chehab	        return 0;
18753a41d3eSMauro Carvalho Chehab	}
18853a41d3eSMauro Carvalho Chehab
18953a41d3eSMauro Carvalho ChehabIf all is well, the show() function should return zero.  A negative error
19053a41d3eSMauro Carvalho Chehabcode in the usual manner indicates that something went wrong; it will be
19153a41d3eSMauro Carvalho Chehabpassed back to user space.  This function can also return SEQ_SKIP, which
19253a41d3eSMauro Carvalho Chehabcauses the current item to be skipped; if the show() function has already
19353a41d3eSMauro Carvalho Chehabgenerated output before returning SEQ_SKIP, that output will be dropped.
19453a41d3eSMauro Carvalho Chehab
19553a41d3eSMauro Carvalho ChehabWe will look at seq_printf() in a moment. But first, the definition of the
19653a41d3eSMauro Carvalho Chehabseq_file iterator is finished by creating a seq_operations structure with
19753a41d3eSMauro Carvalho Chehabthe four functions we have just defined::
19853a41d3eSMauro Carvalho Chehab
19953a41d3eSMauro Carvalho Chehab	static const struct seq_operations ct_seq_ops = {
20053a41d3eSMauro Carvalho Chehab	        .start = ct_seq_start,
20153a41d3eSMauro Carvalho Chehab	        .next  = ct_seq_next,
20253a41d3eSMauro Carvalho Chehab	        .stop  = ct_seq_stop,
20353a41d3eSMauro Carvalho Chehab	        .show  = ct_seq_show
20453a41d3eSMauro Carvalho Chehab	};
20553a41d3eSMauro Carvalho Chehab
20653a41d3eSMauro Carvalho ChehabThis structure will be needed to tie our iterator to the /proc file in
20753a41d3eSMauro Carvalho Chehaba little bit.
20853a41d3eSMauro Carvalho Chehab
20953a41d3eSMauro Carvalho ChehabIt's worth noting that the iterator value returned by start() and
21053a41d3eSMauro Carvalho Chehabmanipulated by the other functions is considered to be completely opaque by
21153a41d3eSMauro Carvalho Chehabthe seq_file code. It can thus be anything that is useful in stepping
21253a41d3eSMauro Carvalho Chehabthrough the data to be output. Counters can be useful, but it could also be
21353a41d3eSMauro Carvalho Chehaba direct pointer into an array or linked list. Anything goes, as long as
21453a41d3eSMauro Carvalho Chehabthe programmer is aware that things can happen between calls to the
21553a41d3eSMauro Carvalho Chehabiterator function. However, the seq_file code (by design) will not sleep
21653a41d3eSMauro Carvalho Chehabbetween the calls to start() and stop(), so holding a lock during that time
21753a41d3eSMauro Carvalho Chehabis a reasonable thing to do. The seq_file code will also avoid taking any
21853a41d3eSMauro Carvalho Chehabother locks while the iterator is active.
21953a41d3eSMauro Carvalho Chehab
220*d56b699dSBjorn HelgaasThe iterator value returned by start() or next() is guaranteed to be
221b3656d82SNeilBrownpassed to a subsequent next() or stop() call.  This allows resources
222b3656d82SNeilBrownsuch as locks that were taken to be reliably released.  There is *no*
223b3656d82SNeilBrownguarantee that the iterator will be passed to show(), though in practice
224b3656d82SNeilBrownit often will be.
225b3656d82SNeilBrown
22653a41d3eSMauro Carvalho Chehab
22753a41d3eSMauro Carvalho ChehabFormatted output
22853a41d3eSMauro Carvalho Chehab================
22953a41d3eSMauro Carvalho Chehab
23053a41d3eSMauro Carvalho ChehabThe seq_file code manages positioning within the output created by the
23153a41d3eSMauro Carvalho Chehabiterator and getting it into the user's buffer. But, for that to work, that
23253a41d3eSMauro Carvalho Chehaboutput must be passed to the seq_file code. Some utility functions have
23353a41d3eSMauro Carvalho Chehabbeen defined which make this task easy.
23453a41d3eSMauro Carvalho Chehab
23553a41d3eSMauro Carvalho ChehabMost code will simply use seq_printf(), which works pretty much like
23653a41d3eSMauro Carvalho Chehabprintk(), but which requires the seq_file pointer as an argument.
23753a41d3eSMauro Carvalho Chehab
23853a41d3eSMauro Carvalho ChehabFor straight character output, the following functions may be used::
23953a41d3eSMauro Carvalho Chehab
24053a41d3eSMauro Carvalho Chehab	seq_putc(struct seq_file *m, char c);
24153a41d3eSMauro Carvalho Chehab	seq_puts(struct seq_file *m, const char *s);
24253a41d3eSMauro Carvalho Chehab	seq_escape(struct seq_file *m, const char *s, const char *esc);
24353a41d3eSMauro Carvalho Chehab
24453a41d3eSMauro Carvalho ChehabThe first two output a single character and a string, just like one would
24553a41d3eSMauro Carvalho Chehabexpect. seq_escape() is like seq_puts(), except that any character in s
24653a41d3eSMauro Carvalho Chehabwhich is in the string esc will be represented in octal form in the output.
24753a41d3eSMauro Carvalho Chehab
24853a41d3eSMauro Carvalho ChehabThere are also a pair of functions for printing filenames::
24953a41d3eSMauro Carvalho Chehab
25053a41d3eSMauro Carvalho Chehab	int seq_path(struct seq_file *m, const struct path *path,
25153a41d3eSMauro Carvalho Chehab		     const char *esc);
25253a41d3eSMauro Carvalho Chehab	int seq_path_root(struct seq_file *m, const struct path *path,
25353a41d3eSMauro Carvalho Chehab			  const struct path *root, const char *esc)
25453a41d3eSMauro Carvalho Chehab
25553a41d3eSMauro Carvalho ChehabHere, path indicates the file of interest, and esc is a set of characters
25653a41d3eSMauro Carvalho Chehabwhich should be escaped in the output.  A call to seq_path() will output
25753a41d3eSMauro Carvalho Chehabthe path relative to the current process's filesystem root.  If a different
25853a41d3eSMauro Carvalho Chehabroot is desired, it can be used with seq_path_root().  If it turns out that
25953a41d3eSMauro Carvalho Chehabpath cannot be reached from root, seq_path_root() returns SEQ_SKIP.
26053a41d3eSMauro Carvalho Chehab
26153a41d3eSMauro Carvalho ChehabA function producing complicated output may want to check::
26253a41d3eSMauro Carvalho Chehab
26353a41d3eSMauro Carvalho Chehab	bool seq_has_overflowed(struct seq_file *m);
26453a41d3eSMauro Carvalho Chehab
26553a41d3eSMauro Carvalho Chehaband avoid further seq_<output> calls if true is returned.
26653a41d3eSMauro Carvalho Chehab
26753a41d3eSMauro Carvalho ChehabA true return from seq_has_overflowed means that the seq_file buffer will
26853a41d3eSMauro Carvalho Chehabbe discarded and the seq_show function will attempt to allocate a larger
26953a41d3eSMauro Carvalho Chehabbuffer and retry printing.
27053a41d3eSMauro Carvalho Chehab
27153a41d3eSMauro Carvalho Chehab
27253a41d3eSMauro Carvalho ChehabMaking it all work
27353a41d3eSMauro Carvalho Chehab==================
27453a41d3eSMauro Carvalho Chehab
27553a41d3eSMauro Carvalho ChehabSo far, we have a nice set of functions which can produce output within the
27653a41d3eSMauro Carvalho Chehabseq_file system, but we have not yet turned them into a file that a user
27753a41d3eSMauro Carvalho Chehabcan see. Creating a file within the kernel requires, of course, the
27853a41d3eSMauro Carvalho Chehabcreation of a set of file_operations which implement the operations on that
27953a41d3eSMauro Carvalho Chehabfile. The seq_file interface provides a set of canned operations which do
28053a41d3eSMauro Carvalho Chehabmost of the work. The virtual file author still must implement the open()
28153a41d3eSMauro Carvalho Chehabmethod, however, to hook everything up. The open function is often a single
28253a41d3eSMauro Carvalho Chehabline, as in the example module::
28353a41d3eSMauro Carvalho Chehab
28453a41d3eSMauro Carvalho Chehab	static int ct_open(struct inode *inode, struct file *file)
28553a41d3eSMauro Carvalho Chehab	{
28653a41d3eSMauro Carvalho Chehab		return seq_open(file, &ct_seq_ops);
28753a41d3eSMauro Carvalho Chehab	}
28853a41d3eSMauro Carvalho Chehab
28953a41d3eSMauro Carvalho ChehabHere, the call to seq_open() takes the seq_operations structure we created
29053a41d3eSMauro Carvalho Chehabbefore, and gets set up to iterate through the virtual file.
29153a41d3eSMauro Carvalho Chehab
29253a41d3eSMauro Carvalho ChehabOn a successful open, seq_open() stores the struct seq_file pointer in
29353a41d3eSMauro Carvalho Chehabfile->private_data. If you have an application where the same iterator can
29453a41d3eSMauro Carvalho Chehabbe used for more than one file, you can store an arbitrary pointer in the
29553a41d3eSMauro Carvalho Chehabprivate field of the seq_file structure; that value can then be retrieved
29653a41d3eSMauro Carvalho Chehabby the iterator functions.
29753a41d3eSMauro Carvalho Chehab
29853a41d3eSMauro Carvalho ChehabThere is also a wrapper function to seq_open() called seq_open_private(). It
29953a41d3eSMauro Carvalho Chehabkmallocs a zero filled block of memory and stores a pointer to it in the
30053a41d3eSMauro Carvalho Chehabprivate field of the seq_file structure, returning 0 on success. The
30153a41d3eSMauro Carvalho Chehabblock size is specified in a third parameter to the function, e.g.::
30253a41d3eSMauro Carvalho Chehab
30353a41d3eSMauro Carvalho Chehab	static int ct_open(struct inode *inode, struct file *file)
30453a41d3eSMauro Carvalho Chehab	{
30553a41d3eSMauro Carvalho Chehab		return seq_open_private(file, &ct_seq_ops,
30653a41d3eSMauro Carvalho Chehab					sizeof(struct mystruct));
30753a41d3eSMauro Carvalho Chehab	}
30853a41d3eSMauro Carvalho Chehab
30953a41d3eSMauro Carvalho ChehabThere is also a variant function, __seq_open_private(), which is functionally
31053a41d3eSMauro Carvalho Chehabidentical except that, if successful, it returns the pointer to the allocated
31153a41d3eSMauro Carvalho Chehabmemory block, allowing further initialisation e.g.::
31253a41d3eSMauro Carvalho Chehab
31353a41d3eSMauro Carvalho Chehab	static int ct_open(struct inode *inode, struct file *file)
31453a41d3eSMauro Carvalho Chehab	{
31553a41d3eSMauro Carvalho Chehab		struct mystruct *p =
31653a41d3eSMauro Carvalho Chehab			__seq_open_private(file, &ct_seq_ops, sizeof(*p));
31753a41d3eSMauro Carvalho Chehab
31853a41d3eSMauro Carvalho Chehab		if (!p)
31953a41d3eSMauro Carvalho Chehab			return -ENOMEM;
32053a41d3eSMauro Carvalho Chehab
32153a41d3eSMauro Carvalho Chehab		p->foo = bar; /* initialize my stuff */
32253a41d3eSMauro Carvalho Chehab			...
32353a41d3eSMauro Carvalho Chehab		p->baz = true;
32453a41d3eSMauro Carvalho Chehab
32553a41d3eSMauro Carvalho Chehab		return 0;
32653a41d3eSMauro Carvalho Chehab	}
32753a41d3eSMauro Carvalho Chehab
32853a41d3eSMauro Carvalho ChehabA corresponding close function, seq_release_private() is available which
32953a41d3eSMauro Carvalho Chehabfrees the memory allocated in the corresponding open.
33053a41d3eSMauro Carvalho Chehab
33153a41d3eSMauro Carvalho ChehabThe other operations of interest - read(), llseek(), and release() - are
33253a41d3eSMauro Carvalho Chehaball implemented by the seq_file code itself. So a virtual file's
33353a41d3eSMauro Carvalho Chehabfile_operations structure will look like::
33453a41d3eSMauro Carvalho Chehab
33553a41d3eSMauro Carvalho Chehab	static const struct file_operations ct_file_ops = {
33653a41d3eSMauro Carvalho Chehab	        .owner   = THIS_MODULE,
33753a41d3eSMauro Carvalho Chehab	        .open    = ct_open,
33853a41d3eSMauro Carvalho Chehab	        .read    = seq_read,
33953a41d3eSMauro Carvalho Chehab	        .llseek  = seq_lseek,
34053a41d3eSMauro Carvalho Chehab	        .release = seq_release
34153a41d3eSMauro Carvalho Chehab	};
34253a41d3eSMauro Carvalho Chehab
34353a41d3eSMauro Carvalho ChehabThere is also a seq_release_private() which passes the contents of the
34453a41d3eSMauro Carvalho Chehabseq_file private field to kfree() before releasing the structure.
34553a41d3eSMauro Carvalho Chehab
34653a41d3eSMauro Carvalho ChehabThe final step is the creation of the /proc file itself. In the example
34753a41d3eSMauro Carvalho Chehabcode, that is done in the initialization code in the usual way::
34853a41d3eSMauro Carvalho Chehab
34953a41d3eSMauro Carvalho Chehab	static int ct_init(void)
35053a41d3eSMauro Carvalho Chehab	{
35153a41d3eSMauro Carvalho Chehab	        struct proc_dir_entry *entry;
35253a41d3eSMauro Carvalho Chehab
35353a41d3eSMauro Carvalho Chehab	        proc_create("sequence", 0, NULL, &ct_file_ops);
35453a41d3eSMauro Carvalho Chehab	        return 0;
35553a41d3eSMauro Carvalho Chehab	}
35653a41d3eSMauro Carvalho Chehab
35753a41d3eSMauro Carvalho Chehab	module_init(ct_init);
35853a41d3eSMauro Carvalho Chehab
35953a41d3eSMauro Carvalho ChehabAnd that is pretty much it.
36053a41d3eSMauro Carvalho Chehab
36153a41d3eSMauro Carvalho Chehab
36253a41d3eSMauro Carvalho Chehabseq_list
36353a41d3eSMauro Carvalho Chehab========
36453a41d3eSMauro Carvalho Chehab
36553a41d3eSMauro Carvalho ChehabIf your file will be iterating through a linked list, you may find these
36653a41d3eSMauro Carvalho Chehabroutines useful::
36753a41d3eSMauro Carvalho Chehab
36853a41d3eSMauro Carvalho Chehab	struct list_head *seq_list_start(struct list_head *head,
36953a41d3eSMauro Carvalho Chehab	       		 		 loff_t pos);
37053a41d3eSMauro Carvalho Chehab	struct list_head *seq_list_start_head(struct list_head *head,
37153a41d3eSMauro Carvalho Chehab			 		      loff_t pos);
37253a41d3eSMauro Carvalho Chehab	struct list_head *seq_list_next(void *v, struct list_head *head,
37353a41d3eSMauro Carvalho Chehab					loff_t *ppos);
37453a41d3eSMauro Carvalho Chehab
37553a41d3eSMauro Carvalho ChehabThese helpers will interpret pos as a position within the list and iterate
37653a41d3eSMauro Carvalho Chehabaccordingly.  Your start() and next() functions need only invoke the
37753a41d3eSMauro Carvalho Chehab``seq_list_*`` helpers with a pointer to the appropriate list_head structure.
37853a41d3eSMauro Carvalho Chehab
37953a41d3eSMauro Carvalho Chehab
38053a41d3eSMauro Carvalho ChehabThe extra-simple version
38153a41d3eSMauro Carvalho Chehab========================
38253a41d3eSMauro Carvalho Chehab
38353a41d3eSMauro Carvalho ChehabFor extremely simple virtual files, there is an even easier interface.  A
38453a41d3eSMauro Carvalho Chehabmodule can define only the show() function, which should create all the
38553a41d3eSMauro Carvalho Chehaboutput that the virtual file will contain. The file's open() method then
38653a41d3eSMauro Carvalho Chehabcalls::
38753a41d3eSMauro Carvalho Chehab
38853a41d3eSMauro Carvalho Chehab	int single_open(struct file *file,
38953a41d3eSMauro Carvalho Chehab	                int (*show)(struct seq_file *m, void *p),
39053a41d3eSMauro Carvalho Chehab	                void *data);
39153a41d3eSMauro Carvalho Chehab
39253a41d3eSMauro Carvalho ChehabWhen output time comes, the show() function will be called once. The data
39353a41d3eSMauro Carvalho Chehabvalue given to single_open() can be found in the private field of the
39453a41d3eSMauro Carvalho Chehabseq_file structure. When using single_open(), the programmer should use
39553a41d3eSMauro Carvalho Chehabsingle_release() instead of seq_release() in the file_operations structure
39653a41d3eSMauro Carvalho Chehabto avoid a memory leak.
397