···8282 - info on relay, for efficient streaming from kernel to user space.8383romfs.txt8484 - description of the ROMFS filesystem.8585+seq_file.txt8686+ - how to use the seq_file API8587sharedsubtree.txt8688 - a description of shared subtrees for namespaces.8789smbfs.txt
+283
Documentation/filesystems/seq_file.txt
···11+The seq_file interface22+33+ Copyright 2003 Jonathan Corbet <corbet@lwn.net>44+ This file is originally from the LWN.net Driver Porting series at55+ http://lwn.net/Articles/driver-porting/66+77+88+There are numerous ways for a device driver (or other kernel component) to99+provide information to the user or system administrator. One useful1010+technique is the creation of virtual files, in debugfs, /proc or elsewhere.1111+Virtual files can provide human-readable output that is easy to get at1212+without any special utility programs; they can also make life easier for1313+script writers. It is not surprising that the use of virtual files has1414+grown over the years.1515+1616+Creating those files correctly has always been a bit of a challenge,1717+however. It is not that hard to make a virtual file which returns a1818+string. But life gets trickier if the output is long - anything greater1919+than an application is likely to read in a single operation. Handling2020+multiple reads (and seeks) requires careful attention to the reader's2121+position within the virtual file - that position is, likely as not, in the2222+middle of a line of output. The kernel has traditionally had a number of2323+implementations that got this wrong.2424+2525+The 2.6 kernel contains a set of functions (implemented by Alexander Viro)2626+which are designed to make it easy for virtual file creators to get it2727+right.2828+2929+The seq_file interface is available via <linux/seq_file.h>. There are3030+three aspects to seq_file:3131+3232+ * An iterator interface which lets a virtual file implementation3333+ step through the objects it is presenting.3434+3535+ * Some utility functions for formatting objects for output without3636+ needing to worry about things like output buffers.3737+3838+ * A set of canned file_operations which implement most operations on3939+ the virtual file.4040+4141+We'll look at the seq_file interface via an extremely simple example: a4242+loadable module which creates a file called /proc/sequence. The file, when4343+read, simply produces a set of increasing integer values, one per line. The4444+sequence will continue until the user loses patience and finds something4545+better to do. The file is seekable, in that one can do something like the4646+following:4747+4848+ dd if=/proc/sequence of=out1 count=14949+ dd if=/proc/sequence skip=1 out=out2 count=15050+5151+Then concatenate the output files out1 and out2 and get the right5252+result. Yes, it is a thoroughly useless module, but the point is to show5353+how the mechanism works without getting lost in other details. (Those5454+wanting to see the full source for this module can find it at5555+http://lwn.net/Articles/22359/).5656+5757+5858+The iterator interface5959+6060+Modules implementing a virtual file with seq_file must implement a simple6161+iterator object that allows stepping through the data of interest.6262+Iterators must be able to move to a specific position - like the file they6363+implement - but the interpretation of that position is up to the iterator6464+itself. A seq_file implementation that is formatting firewall rules, for6565+example, could interpret position N as the Nth rule in the chain.6666+Positioning can thus be done in whatever way makes the most sense for the6767+generator of the data, which need not be aware of how a position translates6868+to an offset in the virtual file. The one obvious exception is that a6969+position of zero should indicate the beginning of the file.7070+7171+The /proc/sequence iterator just uses the count of the next number it7272+will output as its position.7373+7474+Four functions must be implemented to make the iterator work. The first,7575+called start() takes a position as an argument and returns an iterator7676+which will start reading at that position. For our simple sequence example,7777+the start() function looks like:7878+7979+ static void *ct_seq_start(struct seq_file *s, loff_t *pos)8080+ {8181+ loff_t *spos = kmalloc(sizeof(loff_t), GFP_KERNEL);8282+ if (! spos)8383+ return NULL;8484+ *spos = *pos;8585+ return spos;8686+ }8787+8888+The entire data structure for this iterator is a single loff_t value8989+holding the current position. There is no upper bound for the sequence9090+iterator, but that will not be the case for most other seq_file9191+implementations; in most cases the start() function should check for a9292+"past end of file" condition and return NULL if need be.9393+9494+For more complicated applications, the private field of the seq_file9595+structure can be used. There is also a special value whch can be returned9696+by the start() function called SEQ_START_TOKEN; it can be used if you wish9797+to instruct your show() function (described below) to print a header at the9898+top of the output. SEQ_START_TOKEN should only be used if the offset is9999+zero, however.100100+101101+The next function to implement is called, amazingly, next(); its job is to102102+move the iterator forward to the next position in the sequence. The103103+example module can simply increment the position by one; more useful104104+modules will do what is needed to step through some data structure. The105105+next() function returns a new iterator, or NULL if the sequence is106106+complete. Here's the example version:107107+108108+ static void *ct_seq_next(struct seq_file *s, void *v, loff_t *pos)109109+ {110110+ loff_t *spos = (loff_t *) v;111111+ *pos = ++(*spos);112112+ return spos;113113+ }114114+115115+The stop() function is called when iteration is complete; its job, of116116+course, is to clean up. If dynamic memory is allocated for the iterator,117117+stop() is the place to free it.118118+119119+ static void ct_seq_stop(struct seq_file *s, void *v)120120+ {121121+ kfree(v);122122+ }123123+124124+Finally, the show() function should format the object currently pointed to125125+by the iterator for output. It should return zero, or an error code if126126+something goes wrong. The example module's show() function is:127127+128128+ static int ct_seq_show(struct seq_file *s, void *v)129129+ {130130+ loff_t *spos = (loff_t *) v;131131+ seq_printf(s, "%Ld\n", *spos);132132+ return 0;133133+ }134134+135135+We will look at seq_printf() in a moment. But first, the definition of the136136+seq_file iterator is finished by creating a seq_operations structure with137137+the four functions we have just defined:138138+139139+ static struct seq_operations ct_seq_ops = {140140+ .start = ct_seq_start,141141+ .next = ct_seq_next,142142+ .stop = ct_seq_stop,143143+ .show = ct_seq_show144144+ };145145+146146+This structure will be needed to tie our iterator to the /proc file in147147+a little bit.148148+149149+It's worth noting that the interator value returned by start() and150150+manipulated by the other functions is considered to be completely opaque by151151+the seq_file code. It can thus be anything that is useful in stepping152152+through the data to be output. Counters can be useful, but it could also be153153+a direct pointer into an array or linked list. Anything goes, as long as154154+the programmer is aware that things can happen between calls to the155155+iterator function. However, the seq_file code (by design) will not sleep156156+between the calls to start() and stop(), so holding a lock during that time157157+is a reasonable thing to do. The seq_file code will also avoid taking any158158+other locks while the iterator is active.159159+160160+161161+Formatted output162162+163163+The seq_file code manages positioning within the output created by the164164+iterator and getting it into the user's buffer. But, for that to work, that165165+output must be passed to the seq_file code. Some utility functions have166166+been defined which make this task easy.167167+168168+Most code will simply use seq_printf(), which works pretty much like169169+printk(), but which requires the seq_file pointer as an argument. It is170170+common to ignore the return value from seq_printf(), but a function171171+producing complicated output may want to check that value and quit if172172+something non-zero is returned; an error return means that the seq_file173173+buffer has been filled and further output will be discarded.174174+175175+For straight character output, the following functions may be used:176176+177177+ int seq_putc(struct seq_file *m, char c);178178+ int seq_puts(struct seq_file *m, const char *s);179179+ int seq_escape(struct seq_file *m, const char *s, const char *esc);180180+181181+The first two output a single character and a string, just like one would182182+expect. seq_escape() is like seq_puts(), except that any character in s183183+which is in the string esc will be represented in octal form in the output.184184+185185+There is also a function for printing filenames:186186+187187+ int seq_path(struct seq_file *m, struct path *path, char *esc);188188+189189+Here, path indicates the file of interest, and esc is a set of characters190190+which should be escaped in the output.191191+192192+193193+Making it all work194194+195195+So far, we have a nice set of functions which can produce output within the196196+seq_file system, but we have not yet turned them into a file that a user197197+can see. Creating a file within the kernel requires, of course, the198198+creation of a set of file_operations which implement the operations on that199199+file. The seq_file interface provides a set of canned operations which do200200+most of the work. The virtual file author still must implement the open()201201+method, however, to hook everything up. The open function is often a single202202+line, as in the example module:203203+204204+ static int ct_open(struct inode *inode, struct file *file)205205+ {206206+ return seq_open(file, &ct_seq_ops);207207+ };208208+209209+Here, the call to seq_open() takes the seq_operations structure we created210210+before, and gets set up to iterate through the virtual file.211211+212212+On a successful open, seq_open() stores the struct seq_file pointer in213213+file->private_data. If you have an application where the same iterator can214214+be used for more than one file, you can store an arbitrary pointer in the215215+private field of the seq_file structure; that value can then be retrieved216216+by the iterator functions.217217+218218+The other operations of interest - read(), llseek(), and release() - are219219+all implemented by the seq_file code itself. So a virtual file's220220+file_operations structure will look like:221221+222222+ static struct file_operations ct_file_ops = {223223+ .owner = THIS_MODULE,224224+ .open = ct_open,225225+ .read = seq_read,226226+ .llseek = seq_lseek,227227+ .release = seq_release228228+ };229229+230230+There is also a seq_release_private() which passes the contents of the231231+seq_file private field to kfree() before releasing the structure.232232+233233+The final step is the creation of the /proc file itself. In the example234234+code, that is done in the initialization code in the usual way:235235+236236+ static int ct_init(void)237237+ {238238+ struct proc_dir_entry *entry;239239+240240+ entry = create_proc_entry("sequence", 0, NULL);241241+ if (entry)242242+ entry->proc_fops = &ct_file_ops;243243+ return 0;244244+ }245245+246246+ module_init(ct_init);247247+248248+And that is pretty much it.249249+250250+251251+seq_list252252+253253+If your file will be iterating through a linked list, you may find these254254+routines useful:255255+256256+ struct list_head *seq_list_start(struct list_head *head,257257+ loff_t pos);258258+ struct list_head *seq_list_start_head(struct list_head *head,259259+ loff_t pos);260260+ struct list_head *seq_list_next(void *v, struct list_head *head,261261+ loff_t *ppos);262262+263263+These helpers will interpret pos as a position within the list and iterate264264+accordingly. Your start() and next() functions need only invoke the265265+seq_list_* helpers with a pointer to the appropriate list_head structure. 266266+267267+268268+The extra-simple version269269+270270+For extremely simple virtual files, there is an even easier interface. A271271+module can define only the show() function, which should create all the272272+output that the virtual file will contain. The file's open() method then273273+calls:274274+275275+ int single_open(struct file *file,276276+ int (*show)(struct seq_file *m, void *p),277277+ void *data);278278+279279+When output time comes, the show() function will be called once. The data280280+value given to single_open() can be found in the private field of the281281+seq_file structure. When using single_open(), the programmer should use282282+single_release() instead of seq_release() in the file_operations structure283283+to avoid a memory leak.