aboutsummaryrefslogtreecommitdiffstats
path: root/Documentation/filesystems
diff options
context:
space:
mode:
Diffstat (limited to 'Documentation/filesystems')
-rw-r--r--Documentation/filesystems/00-INDEX6
-rw-r--r--Documentation/filesystems/nfsroot.txt270
-rw-r--r--Documentation/filesystems/rpc-cache.txt202
-rw-r--r--Documentation/filesystems/seq_file.txt283
4 files changed, 761 insertions, 0 deletions
diff --git a/Documentation/filesystems/00-INDEX b/Documentation/filesystems/00-INDEX
index e68021c08fbd..52cd611277a3 100644
--- a/Documentation/filesystems/00-INDEX
+++ b/Documentation/filesystems/00-INDEX
@@ -66,6 +66,8 @@ mandatory-locking.txt
66 - info on the Linux implementation of Sys V mandatory file locking. 66 - info on the Linux implementation of Sys V mandatory file locking.
67ncpfs.txt 67ncpfs.txt
68 - info on Novell Netware(tm) filesystem using NCP protocol. 68 - info on Novell Netware(tm) filesystem using NCP protocol.
69nfsroot.txt
70 - short guide on setting up a diskless box with NFS root filesystem.
69ntfs.txt 71ntfs.txt
70 - info and mount options for the NTFS filesystem (Windows NT). 72 - info and mount options for the NTFS filesystem (Windows NT).
71ocfs2.txt 73ocfs2.txt
@@ -82,6 +84,10 @@ relay.txt
82 - info on relay, for efficient streaming from kernel to user space. 84 - info on relay, for efficient streaming from kernel to user space.
83romfs.txt 85romfs.txt
84 - description of the ROMFS filesystem. 86 - description of the ROMFS filesystem.
87rpc-cache.txt
88 - introduction to the caching mechanisms in the sunrpc layer.
89seq_file.txt
90 - how to use the seq_file API
85sharedsubtree.txt 91sharedsubtree.txt
86 - a description of shared subtrees for namespaces. 92 - a description of shared subtrees for namespaces.
87smbfs.txt 93smbfs.txt
diff --git a/Documentation/filesystems/nfsroot.txt b/Documentation/filesystems/nfsroot.txt
new file mode 100644
index 000000000000..31b329172343
--- /dev/null
+++ b/Documentation/filesystems/nfsroot.txt
@@ -0,0 +1,270 @@
1Mounting the root filesystem via NFS (nfsroot)
2===============================================
3
4Written 1996 by Gero Kuhlmann <gero@gkminix.han.de>
5Updated 1997 by Martin Mares <mj@atrey.karlin.mff.cuni.cz>
6Updated 2006 by Nico Schottelius <nico-kernel-nfsroot@schottelius.org>
7Updated 2006 by Horms <horms@verge.net.au>
8
9
10
11In order to use a diskless system, such as an X-terminal or printer server
12for example, it is necessary for the root filesystem to be present on a
13non-disk device. This may be an initramfs (see Documentation/filesystems/
14ramfs-rootfs-initramfs.txt), a ramdisk (see Documentation/initrd.txt) or a
15filesystem mounted via NFS. The following text describes on how to use NFS
16for the root filesystem. For the rest of this text 'client' means the
17diskless system, and 'server' means the NFS server.
18
19
20
21
221.) Enabling nfsroot capabilities
23 -----------------------------
24
25In order to use nfsroot, NFS client support needs to be selected as
26built-in during configuration. Once this has been selected, the nfsroot
27option will become available, which should also be selected.
28
29In the networking options, kernel level autoconfiguration can be selected,
30along with the types of autoconfiguration to support. Selecting all of
31DHCP, BOOTP and RARP is safe.
32
33
34
35
362.) Kernel command line
37 -------------------
38
39When the kernel has been loaded by a boot loader (see below) it needs to be
40told what root fs device to use. And in the case of nfsroot, where to find
41both the server and the name of the directory on the server to mount as root.
42This can be established using the following kernel command line parameters:
43
44
45root=/dev/nfs
46
47 This is necessary to enable the pseudo-NFS-device. Note that it's not a
48 real device but just a synonym to tell the kernel to use NFS instead of
49 a real device.
50
51
52nfsroot=[<server-ip>:]<root-dir>[,<nfs-options>]
53
54 If the `nfsroot' parameter is NOT given on the command line,
55 the default "/tftpboot/%s" will be used.
56
57 <server-ip> Specifies the IP address of the NFS server.
58 The default address is determined by the `ip' parameter
59 (see below). This parameter allows the use of different
60 servers for IP autoconfiguration and NFS.
61
62 <root-dir> Name of the directory on the server to mount as root.
63 If there is a "%s" token in the string, it will be
64 replaced by the ASCII-representation of the client's
65 IP address.
66
67 <nfs-options> Standard NFS options. All options are separated by commas.
68 The following defaults are used:
69 port = as given by server portmap daemon
70 rsize = 4096
71 wsize = 4096
72 timeo = 7
73 retrans = 3
74 acregmin = 3
75 acregmax = 60
76 acdirmin = 30
77 acdirmax = 60
78 flags = hard, nointr, noposix, cto, ac
79
80
81ip=<client-ip>:<server-ip>:<gw-ip>:<netmask>:<hostname>:<device>:<autoconf>
82
83 This parameter tells the kernel how to configure IP addresses of devices
84 and also how to set up the IP routing table. It was originally called
85 `nfsaddrs', but now the boot-time IP configuration works independently of
86 NFS, so it was renamed to `ip' and the old name remained as an alias for
87 compatibility reasons.
88
89 If this parameter is missing from the kernel command line, all fields are
90 assumed to be empty, and the defaults mentioned below apply. In general
91 this means that the kernel tries to configure everything using
92 autoconfiguration.
93
94 The <autoconf> parameter can appear alone as the value to the `ip'
95 parameter (without all the ':' characters before). If the value is
96 "ip=off" or "ip=none", no autoconfiguration will take place, otherwise
97 autoconfiguration will take place. The most common way to use this
98 is "ip=dhcp".
99
100 <client-ip> IP address of the client.
101
102 Default: Determined using autoconfiguration.
103
104 <server-ip> IP address of the NFS server. If RARP is used to determine
105 the client address and this parameter is NOT empty only
106 replies from the specified server are accepted.
107
108 Only required for for NFS root. That is autoconfiguration
109 will not be triggered if it is missing and NFS root is not
110 in operation.
111
112 Default: Determined using autoconfiguration.
113 The address of the autoconfiguration server is used.
114
115 <gw-ip> IP address of a gateway if the server is on a different subnet.
116
117 Default: Determined using autoconfiguration.
118
119 <netmask> Netmask for local network interface. If unspecified
120 the netmask is derived from the client IP address assuming
121 classful addressing.
122
123 Default: Determined using autoconfiguration.
124
125 <hostname> Name of the client. May be supplied by autoconfiguration,
126 but its absence will not trigger autoconfiguration.
127
128 Default: Client IP address is used in ASCII notation.
129
130 <device> Name of network device to use.
131
132 Default: If the host only has one device, it is used.
133 Otherwise the device is determined using
134 autoconfiguration. This is done by sending
135 autoconfiguration requests out of all devices,
136 and using the device that received the first reply.
137
138 <autoconf> Method to use for autoconfiguration. In the case of options
139 which specify multiple autoconfiguration protocols,
140 requests are sent using all protocols, and the first one
141 to reply is used.
142
143 Only autoconfiguration protocols that have been compiled
144 into the kernel will be used, regardless of the value of
145 this option.
146
147 off or none: don't use autoconfiguration
148 (do static IP assignment instead)
149 on or any: use any protocol available in the kernel
150 (default)
151 dhcp: use DHCP
152 bootp: use BOOTP
153 rarp: use RARP
154 both: use both BOOTP and RARP but not DHCP
155 (old option kept for backwards compatibility)
156
157 Default: any
158
159
160
161
1623.) Boot Loader
163 ----------
164
165To get the kernel into memory different approaches can be used.
166They depend on various facilities being available:
167
168
1693.1) Booting from a floppy using syslinux
170
171 When building kernels, an easy way to create a boot floppy that uses
172 syslinux is to use the zdisk or bzdisk make targets which use
173 and bzimage images respectively. Both targets accept the
174 FDARGS parameter which can be used to set the kernel command line.
175
176 e.g.
177 make bzdisk FDARGS="root=/dev/nfs"
178
179 Note that the user running this command will need to have
180 access to the floppy drive device, /dev/fd0
181
182 For more information on syslinux, including how to create bootdisks
183 for prebuilt kernels, see http://syslinux.zytor.com/
184
185 N.B: Previously it was possible to write a kernel directly to
186 a floppy using dd, configure the boot device using rdev, and
187 boot using the resulting floppy. Linux no longer supports this
188 method of booting.
189
1903.2) Booting from a cdrom using isolinux
191
192 When building kernels, an easy way to create a bootable cdrom that
193 uses isolinux is to use the isoimage target which uses a bzimage
194 image. Like zdisk and bzdisk, this target accepts the FDARGS
195 parameter which can be used to set the kernel command line.
196
197 e.g.
198 make isoimage FDARGS="root=/dev/nfs"
199
200 The resulting iso image will be arch/<ARCH>/boot/image.iso
201 This can be written to a cdrom using a variety of tools including
202 cdrecord.
203
204 e.g.
205 cdrecord dev=ATAPI:1,0,0 arch/i386/boot/image.iso
206
207 For more information on isolinux, including how to create bootdisks
208 for prebuilt kernels, see http://syslinux.zytor.com/
209
2103.2) Using LILO
211 When using LILO all the necessary command line parameters may be
212 specified using the 'append=' directive in the LILO configuration
213 file.
214
215 However, to use the 'root=' directive you also need to create
216 a dummy root device, which may be removed after LILO is run.
217
218 mknod /dev/boot255 c 0 255
219
220 For information on configuring LILO, please refer to its documentation.
221
2223.3) Using GRUB
223 When using GRUB, kernel parameter are simply appended after the kernel
224 specification: kernel <kernel> <parameters>
225
2263.4) Using loadlin
227 loadlin may be used to boot Linux from a DOS command prompt without
228 requiring a local hard disk to mount as root. This has not been
229 thoroughly tested by the authors of this document, but in general
230 it should be possible configure the kernel command line similarly
231 to the configuration of LILO.
232
233 Please refer to the loadlin documentation for further information.
234
2353.5) Using a boot ROM
236 This is probably the most elegant way of booting a diskless client.
237 With a boot ROM the kernel is loaded using the TFTP protocol. The
238 authors of this document are not aware of any no commercial boot
239 ROMs that support booting Linux over the network. However, there
240 are two free implementations of a boot ROM, netboot-nfs and
241 etherboot, both of which are available on sunsite.unc.edu, and both
242 of which contain everything you need to boot a diskless Linux client.
243
2443.6) Using pxelinux
245 Pxelinux may be used to boot linux using the PXE boot loader
246 which is present on many modern network cards.
247
248 When using pxelinux, the kernel image is specified using
249 "kernel <relative-path-below /tftpboot>". The nfsroot parameters
250 are passed to the kernel by adding them to the "append" line.
251 It is common to use serial console in conjunction with pxeliunx,
252 see Documentation/serial-console.txt for more information.
253
254 For more information on isolinux, including how to create bootdisks
255 for prebuilt kernels, see http://syslinux.zytor.com/
256
257
258
259
2604.) Credits
261 -------
262
263 The nfsroot code in the kernel and the RARP support have been written
264 by Gero Kuhlmann <gero@gkminix.han.de>.
265
266 The rest of the IP layer autoconfiguration code has been written
267 by Martin Mares <mj@atrey.karlin.mff.cuni.cz>.
268
269 In order to write the initial version of nfsroot I would like to thank
270 Jens-Uwe Mager <jum@anubis.han.de> for his help.
diff --git a/Documentation/filesystems/rpc-cache.txt b/Documentation/filesystems/rpc-cache.txt
new file mode 100644
index 000000000000..8a382bea6808
--- /dev/null
+++ b/Documentation/filesystems/rpc-cache.txt
@@ -0,0 +1,202 @@
1 This document gives a brief introduction to the caching
2mechanisms in the sunrpc layer that is used, in particular,
3for NFS authentication.
4
5CACHES
6======
7The caching replaces the old exports table and allows for
8a wide variety of values to be caches.
9
10There are a number of caches that are similar in structure though
11quite possibly very different in content and use. There is a corpus
12of common code for managing these caches.
13
14Examples of caches that are likely to be needed are:
15 - mapping from IP address to client name
16 - mapping from client name and filesystem to export options
17 - mapping from UID to list of GIDs, to work around NFS's limitation
18 of 16 gids.
19 - mappings between local UID/GID and remote UID/GID for sites that
20 do not have uniform uid assignment
21 - mapping from network identify to public key for crypto authentication.
22
23The common code handles such things as:
24 - general cache lookup with correct locking
25 - supporting 'NEGATIVE' as well as positive entries
26 - allowing an EXPIRED time on cache items, and removing
27 items after they expire, and are no longer in-use.
28 - making requests to user-space to fill in cache entries
29 - allowing user-space to directly set entries in the cache
30 - delaying RPC requests that depend on as-yet incomplete
31 cache entries, and replaying those requests when the cache entry
32 is complete.
33 - clean out old entries as they expire.
34
35Creating a Cache
36----------------
37
381/ A cache needs a datum to store. This is in the form of a
39 structure definition that must contain a
40 struct cache_head
41 as an element, usually the first.
42 It will also contain a key and some content.
43 Each cache element is reference counted and contains
44 expiry and update times for use in cache management.
452/ A cache needs a "cache_detail" structure that
46 describes the cache. This stores the hash table, some
47 parameters for cache management, and some operations detailing how
48 to work with particular cache items.
49 The operations requires are:
50 struct cache_head *alloc(void)
51 This simply allocates appropriate memory and returns
52 a pointer to the cache_detail embedded within the
53 structure
54 void cache_put(struct kref *)
55 This is called when the last reference to an item is
56 dropped. The pointer passed is to the 'ref' field
57 in the cache_head. cache_put should release any
58 references create by 'cache_init' and, if CACHE_VALID
59 is set, any references created by cache_update.
60 It should then release the memory allocated by
61 'alloc'.
62 int match(struct cache_head *orig, struct cache_head *new)
63 test if the keys in the two structures match. Return
64 1 if they do, 0 if they don't.
65 void init(struct cache_head *orig, struct cache_head *new)
66 Set the 'key' fields in 'new' from 'orig'. This may
67 include taking references to shared objects.
68 void update(struct cache_head *orig, struct cache_head *new)
69 Set the 'content' fileds in 'new' from 'orig'.
70 int cache_show(struct seq_file *m, struct cache_detail *cd,
71 struct cache_head *h)
72 Optional. Used to provide a /proc file that lists the
73 contents of a cache. This should show one item,
74 usually on just one line.
75 int cache_request(struct cache_detail *cd, struct cache_head *h,
76 char **bpp, int *blen)
77 Format a request to be send to user-space for an item
78 to be instantiated. *bpp is a buffer of size *blen.
79 bpp should be moved forward over the encoded message,
80 and *blen should be reduced to show how much free
81 space remains. Return 0 on success or <0 if not
82 enough room or other problem.
83 int cache_parse(struct cache_detail *cd, char *buf, int len)
84 A message from user space has arrived to fill out a
85 cache entry. It is in 'buf' of length 'len'.
86 cache_parse should parse this, find the item in the
87 cache with sunrpc_cache_lookup, and update the item
88 with sunrpc_cache_update.
89
90
913/ A cache needs to be registered using cache_register(). This
92 includes it on a list of caches that will be regularly
93 cleaned to discard old data.
94
95Using a cache
96-------------
97
98To find a value in a cache, call sunrpc_cache_lookup passing a pointer
99to the cache_head in a sample item with the 'key' fields filled in.
100This will be passed to ->match to identify the target entry. If no
101entry is found, a new entry will be create, added to the cache, and
102marked as not containing valid data.
103
104The item returned is typically passed to cache_check which will check
105if the data is valid, and may initiate an up-call to get fresh data.
106cache_check will return -ENOENT in the entry is negative or if an up
107call is needed but not possible, -EAGAIN if an upcall is pending,
108or 0 if the data is valid;
109
110cache_check can be passed a "struct cache_req *". This structure is
111typically embedded in the actual request and can be used to create a
112deferred copy of the request (struct cache_deferred_req). This is
113done when the found cache item is not uptodate, but the is reason to
114believe that userspace might provide information soon. When the cache
115item does become valid, the deferred copy of the request will be
116revisited (->revisit). It is expected that this method will
117reschedule the request for processing.
118
119The value returned by sunrpc_cache_lookup can also be passed to
120sunrpc_cache_update to set the content for the item. A second item is
121passed which should hold the content. If the item found by _lookup
122has valid data, then it is discarded and a new item is created. This
123saves any user of an item from worrying about content changing while
124it is being inspected. If the item found by _lookup does not contain
125valid data, then the content is copied across and CACHE_VALID is set.
126
127Populating a cache
128------------------
129
130Each cache has a name, and when the cache is registered, a directory
131with that name is created in /proc/net/rpc
132
133This directory contains a file called 'channel' which is a channel
134for communicating between kernel and user for populating the cache.
135This directory may later contain other files of interacting
136with the cache.
137
138The 'channel' works a bit like a datagram socket. Each 'write' is
139passed as a whole to the cache for parsing and interpretation.
140Each cache can treat the write requests differently, but it is
141expected that a message written will contain:
142 - a key
143 - an expiry time
144 - a content.
145with the intention that an item in the cache with the give key
146should be create or updated to have the given content, and the
147expiry time should be set on that item.
148
149Reading from a channel is a bit more interesting. When a cache
150lookup fails, or when it succeeds but finds an entry that may soon
151expire, a request is lodged for that cache item to be updated by
152user-space. These requests appear in the channel file.
153
154Successive reads will return successive requests.
155If there are no more requests to return, read will return EOF, but a
156select or poll for read will block waiting for another request to be
157added.
158
159Thus a user-space helper is likely to:
160 open the channel.
161 select for readable
162 read a request
163 write a response
164 loop.
165
166If it dies and needs to be restarted, any requests that have not been
167answered will still appear in the file and will be read by the new
168instance of the helper.
169
170Each cache should define a "cache_parse" method which takes a message
171written from user-space and processes it. It should return an error
172(which propagates back to the write syscall) or 0.
173
174Each cache should also define a "cache_request" method which
175takes a cache item and encodes a request into the buffer
176provided.
177
178Note: If a cache has no active readers on the channel, and has had not
179active readers for more than 60 seconds, further requests will not be
180added to the channel but instead all lookups that do not find a valid
181entry will fail. This is partly for backward compatibility: The
182previous nfs exports table was deemed to be authoritative and a
183failed lookup meant a definite 'no'.
184
185request/response format
186-----------------------
187
188While each cache is free to use it's own format for requests
189and responses over channel, the following is recommended as
190appropriate and support routines are available to help:
191Each request or response record should be printable ASCII
192with precisely one newline character which should be at the end.
193Fields within the record should be separated by spaces, normally one.
194If spaces, newlines, or nul characters are needed in a field they
195much be quoted. two mechanisms are available:
1961/ If a field begins '\x' then it must contain an even number of
197 hex digits, and pairs of these digits provide the bytes in the
198 field.
1992/ otherwise a \ in the field must be followed by 3 octal digits
200 which give the code for a byte. Other characters are treated
201 as them selves. At the very least, space, newline, nul, and
202 '\' must be quoted in this way.
diff --git a/Documentation/filesystems/seq_file.txt b/Documentation/filesystems/seq_file.txt
new file mode 100644
index 000000000000..7fb8e6dc62bf
--- /dev/null
+++ b/Documentation/filesystems/seq_file.txt
@@ -0,0 +1,283 @@
1The seq_file interface
2
3 Copyright 2003 Jonathan Corbet <corbet@lwn.net>
4 This file is originally from the LWN.net Driver Porting series at
5 http://lwn.net/Articles/driver-porting/
6
7
8There are numerous ways for a device driver (or other kernel component) to
9provide information to the user or system administrator. One useful
10technique is the creation of virtual files, in debugfs, /proc or elsewhere.
11Virtual files can provide human-readable output that is easy to get at
12without any special utility programs; they can also make life easier for
13script writers. It is not surprising that the use of virtual files has
14grown over the years.
15
16Creating those files correctly has always been a bit of a challenge,
17however. It is not that hard to make a virtual file which returns a
18string. But life gets trickier if the output is long - anything greater
19than an application is likely to read in a single operation. Handling
20multiple reads (and seeks) requires careful attention to the reader's
21position within the virtual file - that position is, likely as not, in the
22middle of a line of output. The kernel has traditionally had a number of
23implementations that got this wrong.
24
25The 2.6 kernel contains a set of functions (implemented by Alexander Viro)
26which are designed to make it easy for virtual file creators to get it
27right.
28
29The seq_file interface is available via <linux/seq_file.h>. There are
30three aspects to seq_file:
31
32 * An iterator interface which lets a virtual file implementation
33 step through the objects it is presenting.
34
35 * Some utility functions for formatting objects for output without
36 needing to worry about things like output buffers.
37
38 * A set of canned file_operations which implement most operations on
39 the virtual file.
40
41We'll look at the seq_file interface via an extremely simple example: a
42loadable module which creates a file called /proc/sequence. The file, when
43read, simply produces a set of increasing integer values, one per line. The
44sequence will continue until the user loses patience and finds something
45better to do. The file is seekable, in that one can do something like the
46following:
47
48 dd if=/proc/sequence of=out1 count=1
49 dd if=/proc/sequence skip=1 out=out2 count=1
50
51Then concatenate the output files out1 and out2 and get the right
52result. Yes, it is a thoroughly useless module, but the point is to show
53how the mechanism works without getting lost in other details. (Those
54wanting to see the full source for this module can find it at
55http://lwn.net/Articles/22359/).
56
57
58The iterator interface
59
60Modules implementing a virtual file with seq_file must implement a simple
61iterator object that allows stepping through the data of interest.
62Iterators must be able to move to a specific position - like the file they
63implement - but the interpretation of that position is up to the iterator
64itself. A seq_file implementation that is formatting firewall rules, for
65example, could interpret position N as the Nth rule in the chain.
66Positioning can thus be done in whatever way makes the most sense for the
67generator of the data, which need not be aware of how a position translates
68to an offset in the virtual file. The one obvious exception is that a
69position of zero should indicate the beginning of the file.
70
71The /proc/sequence iterator just uses the count of the next number it
72will output as its position.
73
74Four functions must be implemented to make the iterator work. The first,
75called start() takes a position as an argument and returns an iterator
76which will start reading at that position. For our simple sequence example,
77the start() function looks like:
78
79 static void *ct_seq_start(struct seq_file *s, loff_t *pos)
80 {
81 loff_t *spos = kmalloc(sizeof(loff_t), GFP_KERNEL);
82 if (! spos)
83 return NULL;
84 *spos = *pos;
85 return spos;
86 }
87
88The entire data structure for this iterator is a single loff_t value
89holding the current position. There is no upper bound for the sequence
90iterator, but that will not be the case for most other seq_file
91implementations; in most cases the start() function should check for a
92"past end of file" condition and return NULL if need be.
93
94For more complicated applications, the private field of the seq_file
95structure can be used. There is also a special value which can be returned
96by the start() function called SEQ_START_TOKEN; it can be used if you wish
97to instruct your show() function (described below) to print a header at the
98top of the output. SEQ_START_TOKEN should only be used if the offset is
99zero, however.
100
101The next function to implement is called, amazingly, next(); its job is to
102move the iterator forward to the next position in the sequence. The
103example module can simply increment the position by one; more useful
104modules will do what is needed to step through some data structure. The
105next() function returns a new iterator, or NULL if the sequence is
106complete. Here's the example version:
107
108 static void *ct_seq_next(struct seq_file *s, void *v, loff_t *pos)
109 {
110 loff_t *spos = v;
111 *pos = ++*spos;
112 return spos;
113 }
114
115The stop() function is called when iteration is complete; its job, of
116course, is to clean up. If dynamic memory is allocated for the iterator,
117stop() is the place to free it.
118
119 static void ct_seq_stop(struct seq_file *s, void *v)
120 {
121 kfree(v);
122 }
123
124Finally, the show() function should format the object currently pointed to
125by the iterator for output. It should return zero, or an error code if
126something goes wrong. The example module's show() function is:
127
128 static int ct_seq_show(struct seq_file *s, void *v)
129 {
130 loff_t *spos = v;
131 seq_printf(s, "%lld\n", (long long)*spos);
132 return 0;
133 }
134
135We will look at seq_printf() in a moment. But first, the definition of the
136seq_file iterator is finished by creating a seq_operations structure with
137the four functions we have just defined:
138
139 static const struct seq_operations ct_seq_ops = {
140 .start = ct_seq_start,
141 .next = ct_seq_next,
142 .stop = ct_seq_stop,
143 .show = ct_seq_show
144 };
145
146This structure will be needed to tie our iterator to the /proc file in
147a little bit.
148
149It's worth noting that the iterator value returned by start() and
150manipulated by the other functions is considered to be completely opaque by
151the seq_file code. It can thus be anything that is useful in stepping
152through the data to be output. Counters can be useful, but it could also be
153a direct pointer into an array or linked list. Anything goes, as long as
154the programmer is aware that things can happen between calls to the
155iterator function. However, the seq_file code (by design) will not sleep
156between the calls to start() and stop(), so holding a lock during that time
157is a reasonable thing to do. The seq_file code will also avoid taking any
158other locks while the iterator is active.
159
160
161Formatted output
162
163The seq_file code manages positioning within the output created by the
164iterator and getting it into the user's buffer. But, for that to work, that
165output must be passed to the seq_file code. Some utility functions have
166been defined which make this task easy.
167
168Most code will simply use seq_printf(), which works pretty much like
169printk(), but which requires the seq_file pointer as an argument. It is
170common to ignore the return value from seq_printf(), but a function
171producing complicated output may want to check that value and quit if
172something non-zero is returned; an error return means that the seq_file
173buffer has been filled and further output will be discarded.
174
175For straight character output, the following functions may be used:
176
177 int seq_putc(struct seq_file *m, char c);
178 int seq_puts(struct seq_file *m, const char *s);
179 int seq_escape(struct seq_file *m, const char *s, const char *esc);
180
181The first two output a single character and a string, just like one would
182expect. seq_escape() is like seq_puts(), except that any character in s
183which is in the string esc will be represented in octal form in the output.
184
185There is also a function for printing filenames:
186
187 int seq_path(struct seq_file *m, struct path *path, char *esc);
188
189Here, path indicates the file of interest, and esc is a set of characters
190which should be escaped in the output.
191
192
193Making it all work
194
195So far, we have a nice set of functions which can produce output within the
196seq_file system, but we have not yet turned them into a file that a user
197can see. Creating a file within the kernel requires, of course, the
198creation of a set of file_operations which implement the operations on that
199file. The seq_file interface provides a set of canned operations which do
200most of the work. The virtual file author still must implement the open()
201method, however, to hook everything up. The open function is often a single
202line, as in the example module:
203
204 static int ct_open(struct inode *inode, struct file *file)
205 {
206 return seq_open(file, &ct_seq_ops);
207 }
208
209Here, the call to seq_open() takes the seq_operations structure we created
210before, and gets set up to iterate through the virtual file.
211
212On a successful open, seq_open() stores the struct seq_file pointer in
213file->private_data. If you have an application where the same iterator can
214be used for more than one file, you can store an arbitrary pointer in the
215private field of the seq_file structure; that value can then be retrieved
216by the iterator functions.
217
218The other operations of interest - read(), llseek(), and release() - are
219all implemented by the seq_file code itself. So a virtual file's
220file_operations structure will look like:
221
222 static const struct file_operations ct_file_ops = {
223 .owner = THIS_MODULE,
224 .open = ct_open,
225 .read = seq_read,
226 .llseek = seq_lseek,
227 .release = seq_release
228 };
229
230There is also a seq_release_private() which passes the contents of the
231seq_file private field to kfree() before releasing the structure.
232
233The final step is the creation of the /proc file itself. In the example
234code, that is done in the initialization code in the usual way:
235
236 static int ct_init(void)
237 {
238 struct proc_dir_entry *entry;
239
240 entry = create_proc_entry("sequence", 0, NULL);
241 if (entry)
242 entry->proc_fops = &ct_file_ops;
243 return 0;
244 }
245
246 module_init(ct_init);
247
248And that is pretty much it.
249
250
251seq_list
252
253If your file will be iterating through a linked list, you may find these
254routines useful:
255
256 struct list_head *seq_list_start(struct list_head *head,
257 loff_t pos);
258 struct list_head *seq_list_start_head(struct list_head *head,
259 loff_t pos);
260 struct list_head *seq_list_next(void *v, struct list_head *head,
261 loff_t *ppos);
262
263These helpers will interpret pos as a position within the list and iterate
264accordingly. Your start() and next() functions need only invoke the
265seq_list_* helpers with a pointer to the appropriate list_head structure.
266
267
268The extra-simple version
269
270For extremely simple virtual files, there is an even easier interface. A
271module can define only the show() function, which should create all the
272output that the virtual file will contain. The file's open() method then
273calls:
274
275 int single_open(struct file *file,
276 int (*show)(struct seq_file *m, void *p),
277 void *data);
278
279When output time comes, the show() function will be called once. The data
280value given to single_open() can be found in the private field of the
281seq_file structure. When using single_open(), the programmer should use
282single_release() instead of seq_release() in the file_operations structure
283to avoid a memory leak.