1*4882a593Smuzhiyun.. SPDX-License-Identifier: GPL-2.0 2*4882a593Smuzhiyun 3*4882a593Smuzhiyun====================== 4*4882a593SmuzhiyunThe seq_file Interface 5*4882a593Smuzhiyun====================== 6*4882a593Smuzhiyun 7*4882a593Smuzhiyun Copyright 2003 Jonathan Corbet <corbet@lwn.net> 8*4882a593Smuzhiyun 9*4882a593Smuzhiyun This file is originally from the LWN.net Driver Porting series at 10*4882a593Smuzhiyun https://lwn.net/Articles/driver-porting/ 11*4882a593Smuzhiyun 12*4882a593Smuzhiyun 13*4882a593SmuzhiyunThere are numerous ways for a device driver (or other kernel component) to 14*4882a593Smuzhiyunprovide information to the user or system administrator. One useful 15*4882a593Smuzhiyuntechnique is the creation of virtual files, in debugfs, /proc or elsewhere. 16*4882a593SmuzhiyunVirtual files can provide human-readable output that is easy to get at 17*4882a593Smuzhiyunwithout any special utility programs; they can also make life easier for 18*4882a593Smuzhiyunscript writers. It is not surprising that the use of virtual files has 19*4882a593Smuzhiyungrown over the years. 20*4882a593Smuzhiyun 21*4882a593SmuzhiyunCreating those files correctly has always been a bit of a challenge, 22*4882a593Smuzhiyunhowever. It is not that hard to make a virtual file which returns a 23*4882a593Smuzhiyunstring. But life gets trickier if the output is long - anything greater 24*4882a593Smuzhiyunthan an application is likely to read in a single operation. Handling 25*4882a593Smuzhiyunmultiple reads (and seeks) requires careful attention to the reader's 26*4882a593Smuzhiyunposition within the virtual file - that position is, likely as not, in the 27*4882a593Smuzhiyunmiddle of a line of output. The kernel has traditionally had a number of 28*4882a593Smuzhiyunimplementations that got this wrong. 29*4882a593Smuzhiyun 30*4882a593SmuzhiyunThe 2.6 kernel contains a set of functions (implemented by Alexander Viro) 31*4882a593Smuzhiyunwhich are designed to make it easy for virtual file creators to get it 32*4882a593Smuzhiyunright. 33*4882a593Smuzhiyun 34*4882a593SmuzhiyunThe seq_file interface is available via <linux/seq_file.h>. There are 35*4882a593Smuzhiyunthree aspects to seq_file: 36*4882a593Smuzhiyun 37*4882a593Smuzhiyun * An iterator interface which lets a virtual file implementation 38*4882a593Smuzhiyun step through the objects it is presenting. 39*4882a593Smuzhiyun 40*4882a593Smuzhiyun * Some utility functions for formatting objects for output without 41*4882a593Smuzhiyun needing to worry about things like output buffers. 42*4882a593Smuzhiyun 43*4882a593Smuzhiyun * A set of canned file_operations which implement most operations on 44*4882a593Smuzhiyun the virtual file. 45*4882a593Smuzhiyun 46*4882a593SmuzhiyunWe'll look at the seq_file interface via an extremely simple example: a 47*4882a593Smuzhiyunloadable module which creates a file called /proc/sequence. The file, when 48*4882a593Smuzhiyunread, simply produces a set of increasing integer values, one per line. The 49*4882a593Smuzhiyunsequence will continue until the user loses patience and finds something 50*4882a593Smuzhiyunbetter to do. The file is seekable, in that one can do something like the 51*4882a593Smuzhiyunfollowing:: 52*4882a593Smuzhiyun 53*4882a593Smuzhiyun dd if=/proc/sequence of=out1 count=1 54*4882a593Smuzhiyun dd if=/proc/sequence skip=1 of=out2 count=1 55*4882a593Smuzhiyun 56*4882a593SmuzhiyunThen concatenate the output files out1 and out2 and get the right 57*4882a593Smuzhiyunresult. Yes, it is a thoroughly useless module, but the point is to show 58*4882a593Smuzhiyunhow the mechanism works without getting lost in other details. (Those 59*4882a593Smuzhiyunwanting to see the full source for this module can find it at 60*4882a593Smuzhiyunhttps://lwn.net/Articles/22359/). 61*4882a593Smuzhiyun 62*4882a593SmuzhiyunDeprecated create_proc_entry 63*4882a593Smuzhiyun============================ 64*4882a593Smuzhiyun 65*4882a593SmuzhiyunNote that the above article uses create_proc_entry which was removed in 66*4882a593Smuzhiyunkernel 3.10. Current versions require the following update:: 67*4882a593Smuzhiyun 68*4882a593Smuzhiyun - entry = create_proc_entry("sequence", 0, NULL); 69*4882a593Smuzhiyun - if (entry) 70*4882a593Smuzhiyun - entry->proc_fops = &ct_file_ops; 71*4882a593Smuzhiyun + entry = proc_create("sequence", 0, NULL, &ct_file_ops); 72*4882a593Smuzhiyun 73*4882a593SmuzhiyunThe iterator interface 74*4882a593Smuzhiyun====================== 75*4882a593Smuzhiyun 76*4882a593SmuzhiyunModules implementing a virtual file with seq_file must implement an 77*4882a593Smuzhiyuniterator object that allows stepping through the data of interest 78*4882a593Smuzhiyunduring a "session" (roughly one read() system call). If the iterator 79*4882a593Smuzhiyunis able to move to a specific position - like the file they implement, 80*4882a593Smuzhiyunthough with freedom to map the position number to a sequence location 81*4882a593Smuzhiyunin whatever way is convenient - the iterator need only exist 82*4882a593Smuzhiyuntransiently during a session. If the iterator cannot easily find a 83*4882a593Smuzhiyunnumerical position but works well with a first/next interface, the 84*4882a593Smuzhiyuniterator can be stored in the private data area and continue from one 85*4882a593Smuzhiyunsession to the next. 86*4882a593Smuzhiyun 87*4882a593SmuzhiyunA seq_file implementation that is formatting firewall rules from a 88*4882a593Smuzhiyuntable, for example, could provide a simple iterator that interprets 89*4882a593Smuzhiyunposition N as the Nth rule in the chain. A seq_file implementation 90*4882a593Smuzhiyunthat presents the content of a, potentially volatile, linked list 91*4882a593Smuzhiyunmight record a pointer into that list, providing that can be done 92*4882a593Smuzhiyunwithout risk of the current location being removed. 93*4882a593Smuzhiyun 94*4882a593SmuzhiyunPositioning can thus be done in whatever way makes the most sense for 95*4882a593Smuzhiyunthe generator of the data, which need not be aware of how a position 96*4882a593Smuzhiyuntranslates to an offset in the virtual file. The one obvious exception 97*4882a593Smuzhiyunis that a position of zero should indicate the beginning of the file. 98*4882a593Smuzhiyun 99*4882a593SmuzhiyunThe /proc/sequence iterator just uses the count of the next number it 100*4882a593Smuzhiyunwill output as its position. 101*4882a593Smuzhiyun 102*4882a593SmuzhiyunFour functions must be implemented to make the iterator work. The 103*4882a593Smuzhiyunfirst, called start(), starts a session and takes a position as an 104*4882a593Smuzhiyunargument, returning an iterator which will start reading at that 105*4882a593Smuzhiyunposition. The pos passed to start() will always be either zero, or 106*4882a593Smuzhiyunthe most recent pos used in the previous session. 107*4882a593Smuzhiyun 108*4882a593SmuzhiyunFor our simple sequence example, 109*4882a593Smuzhiyunthe start() function looks like:: 110*4882a593Smuzhiyun 111*4882a593Smuzhiyun static void *ct_seq_start(struct seq_file *s, loff_t *pos) 112*4882a593Smuzhiyun { 113*4882a593Smuzhiyun loff_t *spos = kmalloc(sizeof(loff_t), GFP_KERNEL); 114*4882a593Smuzhiyun if (! spos) 115*4882a593Smuzhiyun return NULL; 116*4882a593Smuzhiyun *spos = *pos; 117*4882a593Smuzhiyun return spos; 118*4882a593Smuzhiyun } 119*4882a593Smuzhiyun 120*4882a593SmuzhiyunThe entire data structure for this iterator is a single loff_t value 121*4882a593Smuzhiyunholding the current position. There is no upper bound for the sequence 122*4882a593Smuzhiyuniterator, but that will not be the case for most other seq_file 123*4882a593Smuzhiyunimplementations; in most cases the start() function should check for a 124*4882a593Smuzhiyun"past end of file" condition and return NULL if need be. 125*4882a593Smuzhiyun 126*4882a593SmuzhiyunFor more complicated applications, the private field of the seq_file 127*4882a593Smuzhiyunstructure can be used to hold state from session to session. There is 128*4882a593Smuzhiyunalso a special value which can be returned by the start() function 129*4882a593Smuzhiyuncalled SEQ_START_TOKEN; it can be used if you wish to instruct your 130*4882a593Smuzhiyunshow() function (described below) to print a header at the top of the 131*4882a593Smuzhiyunoutput. SEQ_START_TOKEN should only be used if the offset is zero, 132*4882a593Smuzhiyunhowever. SEQ_START_TOKEN has no special meaning to the core seq_file 133*4882a593Smuzhiyuncode. It is provided as a convenience for a start() funciton to 134*4882a593Smuzhiyuncommunicate with the next() and show() functions. 135*4882a593Smuzhiyun 136*4882a593SmuzhiyunThe next function to implement is called, amazingly, next(); its job is to 137*4882a593Smuzhiyunmove the iterator forward to the next position in the sequence. The 138*4882a593Smuzhiyunexample module can simply increment the position by one; more useful 139*4882a593Smuzhiyunmodules will do what is needed to step through some data structure. The 140*4882a593Smuzhiyunnext() function returns a new iterator, or NULL if the sequence is 141*4882a593Smuzhiyuncomplete. Here's the example version:: 142*4882a593Smuzhiyun 143*4882a593Smuzhiyun static void *ct_seq_next(struct seq_file *s, void *v, loff_t *pos) 144*4882a593Smuzhiyun { 145*4882a593Smuzhiyun loff_t *spos = v; 146*4882a593Smuzhiyun *pos = ++*spos; 147*4882a593Smuzhiyun return spos; 148*4882a593Smuzhiyun } 149*4882a593Smuzhiyun 150*4882a593SmuzhiyunThe next() function should set ``*pos`` to a value that start() can use 151*4882a593Smuzhiyunto find the new location in the sequence. When the iterator is being 152*4882a593Smuzhiyunstored in the private data area, rather than being reinitialized on each 153*4882a593Smuzhiyunstart(), it might seem sufficient to simply set ``*pos`` to any non-zero 154*4882a593Smuzhiyunvalue (zero always tells start() to restart the sequence). This is not 155*4882a593Smuzhiyunsufficient due to historical problems. 156*4882a593Smuzhiyun 157*4882a593SmuzhiyunHistorically, many next() functions have *not* updated ``*pos`` at 158*4882a593Smuzhiyunend-of-file. If the value is then used by start() to initialise the 159*4882a593Smuzhiyuniterator, this can result in corner cases where the last entry in the 160*4882a593Smuzhiyunsequence is reported twice in the file. In order to discourage this bug 161*4882a593Smuzhiyunfrom being resurrected, the core seq_file code now produces a warning if 162*4882a593Smuzhiyuna next() function does not change the value of ``*pos``. Consequently a 163*4882a593Smuzhiyunnext() function *must* change the value of ``*pos``, and of course must 164*4882a593Smuzhiyunset it to a non-zero value. 165*4882a593Smuzhiyun 166*4882a593SmuzhiyunThe stop() function closes a session; its job, of course, is to clean 167*4882a593Smuzhiyunup. If dynamic memory is allocated for the iterator, stop() is the 168*4882a593Smuzhiyunplace to free it; if a lock was taken by start(), stop() must release 169*4882a593Smuzhiyunthat lock. The value that ``*pos`` was set to by the last next() call 170*4882a593Smuzhiyunbefore stop() is remembered, and used for the first start() call of 171*4882a593Smuzhiyunthe next session unless lseek() has been called on the file; in that 172*4882a593Smuzhiyuncase next start() will be asked to start at position zero:: 173*4882a593Smuzhiyun 174*4882a593Smuzhiyun static void ct_seq_stop(struct seq_file *s, void *v) 175*4882a593Smuzhiyun { 176*4882a593Smuzhiyun kfree(v); 177*4882a593Smuzhiyun } 178*4882a593Smuzhiyun 179*4882a593SmuzhiyunFinally, the show() function should format the object currently pointed to 180*4882a593Smuzhiyunby the iterator for output. The example module's show() function is:: 181*4882a593Smuzhiyun 182*4882a593Smuzhiyun static int ct_seq_show(struct seq_file *s, void *v) 183*4882a593Smuzhiyun { 184*4882a593Smuzhiyun loff_t *spos = v; 185*4882a593Smuzhiyun seq_printf(s, "%lld\n", (long long)*spos); 186*4882a593Smuzhiyun return 0; 187*4882a593Smuzhiyun } 188*4882a593Smuzhiyun 189*4882a593SmuzhiyunIf all is well, the show() function should return zero. A negative error 190*4882a593Smuzhiyuncode in the usual manner indicates that something went wrong; it will be 191*4882a593Smuzhiyunpassed back to user space. This function can also return SEQ_SKIP, which 192*4882a593Smuzhiyuncauses the current item to be skipped; if the show() function has already 193*4882a593Smuzhiyungenerated output before returning SEQ_SKIP, that output will be dropped. 194*4882a593Smuzhiyun 195*4882a593SmuzhiyunWe will look at seq_printf() in a moment. But first, the definition of the 196*4882a593Smuzhiyunseq_file iterator is finished by creating a seq_operations structure with 197*4882a593Smuzhiyunthe four functions we have just defined:: 198*4882a593Smuzhiyun 199*4882a593Smuzhiyun static const struct seq_operations ct_seq_ops = { 200*4882a593Smuzhiyun .start = ct_seq_start, 201*4882a593Smuzhiyun .next = ct_seq_next, 202*4882a593Smuzhiyun .stop = ct_seq_stop, 203*4882a593Smuzhiyun .show = ct_seq_show 204*4882a593Smuzhiyun }; 205*4882a593Smuzhiyun 206*4882a593SmuzhiyunThis structure will be needed to tie our iterator to the /proc file in 207*4882a593Smuzhiyuna little bit. 208*4882a593Smuzhiyun 209*4882a593SmuzhiyunIt's worth noting that the iterator value returned by start() and 210*4882a593Smuzhiyunmanipulated by the other functions is considered to be completely opaque by 211*4882a593Smuzhiyunthe seq_file code. It can thus be anything that is useful in stepping 212*4882a593Smuzhiyunthrough the data to be output. Counters can be useful, but it could also be 213*4882a593Smuzhiyuna direct pointer into an array or linked list. Anything goes, as long as 214*4882a593Smuzhiyunthe programmer is aware that things can happen between calls to the 215*4882a593Smuzhiyuniterator function. However, the seq_file code (by design) will not sleep 216*4882a593Smuzhiyunbetween the calls to start() and stop(), so holding a lock during that time 217*4882a593Smuzhiyunis a reasonable thing to do. The seq_file code will also avoid taking any 218*4882a593Smuzhiyunother locks while the iterator is active. 219*4882a593Smuzhiyun 220*4882a593SmuzhiyunThe iterater value returned by start() or next() is guaranteed to be 221*4882a593Smuzhiyunpassed to a subsequent next() or stop() call. This allows resources 222*4882a593Smuzhiyunsuch as locks that were taken to be reliably released. There is *no* 223*4882a593Smuzhiyunguarantee that the iterator will be passed to show(), though in practice 224*4882a593Smuzhiyunit often will be. 225*4882a593Smuzhiyun 226*4882a593Smuzhiyun 227*4882a593SmuzhiyunFormatted output 228*4882a593Smuzhiyun================ 229*4882a593Smuzhiyun 230*4882a593SmuzhiyunThe seq_file code manages positioning within the output created by the 231*4882a593Smuzhiyuniterator and getting it into the user's buffer. But, for that to work, that 232*4882a593Smuzhiyunoutput must be passed to the seq_file code. Some utility functions have 233*4882a593Smuzhiyunbeen defined which make this task easy. 234*4882a593Smuzhiyun 235*4882a593SmuzhiyunMost code will simply use seq_printf(), which works pretty much like 236*4882a593Smuzhiyunprintk(), but which requires the seq_file pointer as an argument. 237*4882a593Smuzhiyun 238*4882a593SmuzhiyunFor straight character output, the following functions may be used:: 239*4882a593Smuzhiyun 240*4882a593Smuzhiyun seq_putc(struct seq_file *m, char c); 241*4882a593Smuzhiyun seq_puts(struct seq_file *m, const char *s); 242*4882a593Smuzhiyun seq_escape(struct seq_file *m, const char *s, const char *esc); 243*4882a593Smuzhiyun 244*4882a593SmuzhiyunThe first two output a single character and a string, just like one would 245*4882a593Smuzhiyunexpect. seq_escape() is like seq_puts(), except that any character in s 246*4882a593Smuzhiyunwhich is in the string esc will be represented in octal form in the output. 247*4882a593Smuzhiyun 248*4882a593SmuzhiyunThere are also a pair of functions for printing filenames:: 249*4882a593Smuzhiyun 250*4882a593Smuzhiyun int seq_path(struct seq_file *m, const struct path *path, 251*4882a593Smuzhiyun const char *esc); 252*4882a593Smuzhiyun int seq_path_root(struct seq_file *m, const struct path *path, 253*4882a593Smuzhiyun const struct path *root, const char *esc) 254*4882a593Smuzhiyun 255*4882a593SmuzhiyunHere, path indicates the file of interest, and esc is a set of characters 256*4882a593Smuzhiyunwhich should be escaped in the output. A call to seq_path() will output 257*4882a593Smuzhiyunthe path relative to the current process's filesystem root. If a different 258*4882a593Smuzhiyunroot is desired, it can be used with seq_path_root(). If it turns out that 259*4882a593Smuzhiyunpath cannot be reached from root, seq_path_root() returns SEQ_SKIP. 260*4882a593Smuzhiyun 261*4882a593SmuzhiyunA function producing complicated output may want to check:: 262*4882a593Smuzhiyun 263*4882a593Smuzhiyun bool seq_has_overflowed(struct seq_file *m); 264*4882a593Smuzhiyun 265*4882a593Smuzhiyunand avoid further seq_<output> calls if true is returned. 266*4882a593Smuzhiyun 267*4882a593SmuzhiyunA true return from seq_has_overflowed means that the seq_file buffer will 268*4882a593Smuzhiyunbe discarded and the seq_show function will attempt to allocate a larger 269*4882a593Smuzhiyunbuffer and retry printing. 270*4882a593Smuzhiyun 271*4882a593Smuzhiyun 272*4882a593SmuzhiyunMaking it all work 273*4882a593Smuzhiyun================== 274*4882a593Smuzhiyun 275*4882a593SmuzhiyunSo far, we have a nice set of functions which can produce output within the 276*4882a593Smuzhiyunseq_file system, but we have not yet turned them into a file that a user 277*4882a593Smuzhiyuncan see. Creating a file within the kernel requires, of course, the 278*4882a593Smuzhiyuncreation of a set of file_operations which implement the operations on that 279*4882a593Smuzhiyunfile. The seq_file interface provides a set of canned operations which do 280*4882a593Smuzhiyunmost of the work. The virtual file author still must implement the open() 281*4882a593Smuzhiyunmethod, however, to hook everything up. The open function is often a single 282*4882a593Smuzhiyunline, as in the example module:: 283*4882a593Smuzhiyun 284*4882a593Smuzhiyun static int ct_open(struct inode *inode, struct file *file) 285*4882a593Smuzhiyun { 286*4882a593Smuzhiyun return seq_open(file, &ct_seq_ops); 287*4882a593Smuzhiyun } 288*4882a593Smuzhiyun 289*4882a593SmuzhiyunHere, the call to seq_open() takes the seq_operations structure we created 290*4882a593Smuzhiyunbefore, and gets set up to iterate through the virtual file. 291*4882a593Smuzhiyun 292*4882a593SmuzhiyunOn a successful open, seq_open() stores the struct seq_file pointer in 293*4882a593Smuzhiyunfile->private_data. If you have an application where the same iterator can 294*4882a593Smuzhiyunbe used for more than one file, you can store an arbitrary pointer in the 295*4882a593Smuzhiyunprivate field of the seq_file structure; that value can then be retrieved 296*4882a593Smuzhiyunby the iterator functions. 297*4882a593Smuzhiyun 298*4882a593SmuzhiyunThere is also a wrapper function to seq_open() called seq_open_private(). It 299*4882a593Smuzhiyunkmallocs a zero filled block of memory and stores a pointer to it in the 300*4882a593Smuzhiyunprivate field of the seq_file structure, returning 0 on success. The 301*4882a593Smuzhiyunblock size is specified in a third parameter to the function, e.g.:: 302*4882a593Smuzhiyun 303*4882a593Smuzhiyun static int ct_open(struct inode *inode, struct file *file) 304*4882a593Smuzhiyun { 305*4882a593Smuzhiyun return seq_open_private(file, &ct_seq_ops, 306*4882a593Smuzhiyun sizeof(struct mystruct)); 307*4882a593Smuzhiyun } 308*4882a593Smuzhiyun 309*4882a593SmuzhiyunThere is also a variant function, __seq_open_private(), which is functionally 310*4882a593Smuzhiyunidentical except that, if successful, it returns the pointer to the allocated 311*4882a593Smuzhiyunmemory block, allowing further initialisation e.g.:: 312*4882a593Smuzhiyun 313*4882a593Smuzhiyun static int ct_open(struct inode *inode, struct file *file) 314*4882a593Smuzhiyun { 315*4882a593Smuzhiyun struct mystruct *p = 316*4882a593Smuzhiyun __seq_open_private(file, &ct_seq_ops, sizeof(*p)); 317*4882a593Smuzhiyun 318*4882a593Smuzhiyun if (!p) 319*4882a593Smuzhiyun return -ENOMEM; 320*4882a593Smuzhiyun 321*4882a593Smuzhiyun p->foo = bar; /* initialize my stuff */ 322*4882a593Smuzhiyun ... 323*4882a593Smuzhiyun p->baz = true; 324*4882a593Smuzhiyun 325*4882a593Smuzhiyun return 0; 326*4882a593Smuzhiyun } 327*4882a593Smuzhiyun 328*4882a593SmuzhiyunA corresponding close function, seq_release_private() is available which 329*4882a593Smuzhiyunfrees the memory allocated in the corresponding open. 330*4882a593Smuzhiyun 331*4882a593SmuzhiyunThe other operations of interest - read(), llseek(), and release() - are 332*4882a593Smuzhiyunall implemented by the seq_file code itself. So a virtual file's 333*4882a593Smuzhiyunfile_operations structure will look like:: 334*4882a593Smuzhiyun 335*4882a593Smuzhiyun static const struct file_operations ct_file_ops = { 336*4882a593Smuzhiyun .owner = THIS_MODULE, 337*4882a593Smuzhiyun .open = ct_open, 338*4882a593Smuzhiyun .read = seq_read, 339*4882a593Smuzhiyun .llseek = seq_lseek, 340*4882a593Smuzhiyun .release = seq_release 341*4882a593Smuzhiyun }; 342*4882a593Smuzhiyun 343*4882a593SmuzhiyunThere is also a seq_release_private() which passes the contents of the 344*4882a593Smuzhiyunseq_file private field to kfree() before releasing the structure. 345*4882a593Smuzhiyun 346*4882a593SmuzhiyunThe final step is the creation of the /proc file itself. In the example 347*4882a593Smuzhiyuncode, that is done in the initialization code in the usual way:: 348*4882a593Smuzhiyun 349*4882a593Smuzhiyun static int ct_init(void) 350*4882a593Smuzhiyun { 351*4882a593Smuzhiyun struct proc_dir_entry *entry; 352*4882a593Smuzhiyun 353*4882a593Smuzhiyun proc_create("sequence", 0, NULL, &ct_file_ops); 354*4882a593Smuzhiyun return 0; 355*4882a593Smuzhiyun } 356*4882a593Smuzhiyun 357*4882a593Smuzhiyun module_init(ct_init); 358*4882a593Smuzhiyun 359*4882a593SmuzhiyunAnd that is pretty much it. 360*4882a593Smuzhiyun 361*4882a593Smuzhiyun 362*4882a593Smuzhiyunseq_list 363*4882a593Smuzhiyun======== 364*4882a593Smuzhiyun 365*4882a593SmuzhiyunIf your file will be iterating through a linked list, you may find these 366*4882a593Smuzhiyunroutines useful:: 367*4882a593Smuzhiyun 368*4882a593Smuzhiyun struct list_head *seq_list_start(struct list_head *head, 369*4882a593Smuzhiyun loff_t pos); 370*4882a593Smuzhiyun struct list_head *seq_list_start_head(struct list_head *head, 371*4882a593Smuzhiyun loff_t pos); 372*4882a593Smuzhiyun struct list_head *seq_list_next(void *v, struct list_head *head, 373*4882a593Smuzhiyun loff_t *ppos); 374*4882a593Smuzhiyun 375*4882a593SmuzhiyunThese helpers will interpret pos as a position within the list and iterate 376*4882a593Smuzhiyunaccordingly. Your start() and next() functions need only invoke the 377*4882a593Smuzhiyun``seq_list_*`` helpers with a pointer to the appropriate list_head structure. 378*4882a593Smuzhiyun 379*4882a593Smuzhiyun 380*4882a593SmuzhiyunThe extra-simple version 381*4882a593Smuzhiyun======================== 382*4882a593Smuzhiyun 383*4882a593SmuzhiyunFor extremely simple virtual files, there is an even easier interface. A 384*4882a593Smuzhiyunmodule can define only the show() function, which should create all the 385*4882a593Smuzhiyunoutput that the virtual file will contain. The file's open() method then 386*4882a593Smuzhiyuncalls:: 387*4882a593Smuzhiyun 388*4882a593Smuzhiyun int single_open(struct file *file, 389*4882a593Smuzhiyun int (*show)(struct seq_file *m, void *p), 390*4882a593Smuzhiyun void *data); 391*4882a593Smuzhiyun 392*4882a593SmuzhiyunWhen output time comes, the show() function will be called once. The data 393*4882a593Smuzhiyunvalue given to single_open() can be found in the private field of the 394*4882a593Smuzhiyunseq_file structure. When using single_open(), the programmer should use 395*4882a593Smuzhiyunsingle_release() instead of seq_release() in the file_operations structure 396*4882a593Smuzhiyunto avoid a memory leak. 397