~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/fs/seq_file.c

Version: ~ [ linux-6.12-rc7 ] ~ [ linux-6.11.7 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.60 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.116 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.171 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.229 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.285 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.323 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.12 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

Diff markup

Differences between /fs/seq_file.c (Version linux-6.12-rc7) and /fs/seq_file.c (Version linux-5.12.19)


  1 // SPDX-License-Identifier: GPL-2.0                 1 // SPDX-License-Identifier: GPL-2.0
  2 /*                                                  2 /*
  3  * linux/fs/seq_file.c                              3  * linux/fs/seq_file.c
  4  *                                                  4  *
  5  * helper functions for making synthetic files      5  * helper functions for making synthetic files from sequences of records.
  6  * initial implementation -- AV, Oct 2001.          6  * initial implementation -- AV, Oct 2001.
  7  */                                                 7  */
  8                                                     8 
  9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt         9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 10                                                    10 
 11 #include <linux/cache.h>                           11 #include <linux/cache.h>
 12 #include <linux/fs.h>                              12 #include <linux/fs.h>
 13 #include <linux/export.h>                          13 #include <linux/export.h>
 14 #include <linux/seq_file.h>                        14 #include <linux/seq_file.h>
 15 #include <linux/vmalloc.h>                         15 #include <linux/vmalloc.h>
 16 #include <linux/slab.h>                            16 #include <linux/slab.h>
 17 #include <linux/cred.h>                            17 #include <linux/cred.h>
 18 #include <linux/mm.h>                              18 #include <linux/mm.h>
 19 #include <linux/printk.h>                          19 #include <linux/printk.h>
 20 #include <linux/string_helpers.h>                  20 #include <linux/string_helpers.h>
 21 #include <linux/uio.h>                             21 #include <linux/uio.h>
 22                                                    22 
 23 #include <linux/uaccess.h>                         23 #include <linux/uaccess.h>
 24 #include <asm/page.h>                              24 #include <asm/page.h>
 25                                                    25 
 26 static struct kmem_cache *seq_file_cache __ro_     26 static struct kmem_cache *seq_file_cache __ro_after_init;
 27                                                    27 
 28 static void seq_set_overflow(struct seq_file *     28 static void seq_set_overflow(struct seq_file *m)
 29 {                                                  29 {
 30         m->count = m->size;                        30         m->count = m->size;
 31 }                                                  31 }
 32                                                    32 
 33 static void *seq_buf_alloc(unsigned long size)     33 static void *seq_buf_alloc(unsigned long size)
 34 {                                                  34 {
 35         if (unlikely(size > MAX_RW_COUNT))         35         if (unlikely(size > MAX_RW_COUNT))
 36                 return NULL;                       36                 return NULL;
 37                                                    37 
 38         return kvmalloc(size, GFP_KERNEL_ACCOU     38         return kvmalloc(size, GFP_KERNEL_ACCOUNT);
 39 }                                                  39 }
 40                                                    40 
 41 /**                                                41 /**
 42  *      seq_open -      initialize sequential      42  *      seq_open -      initialize sequential file
 43  *      @file: file we initialize                  43  *      @file: file we initialize
 44  *      @op: method table describing the seque     44  *      @op: method table describing the sequence
 45  *                                                 45  *
 46  *      seq_open() sets @file, associating it      46  *      seq_open() sets @file, associating it with a sequence described
 47  *      by @op.  @op->start() sets the iterato     47  *      by @op.  @op->start() sets the iterator up and returns the first
 48  *      element of sequence. @op->stop() shuts     48  *      element of sequence. @op->stop() shuts it down.  @op->next()
 49  *      returns the next element of sequence.      49  *      returns the next element of sequence.  @op->show() prints element
 50  *      into the buffer.  In case of error ->s     50  *      into the buffer.  In case of error ->start() and ->next() return
 51  *      ERR_PTR(error).  In the end of sequenc     51  *      ERR_PTR(error).  In the end of sequence they return %NULL. ->show()
 52  *      returns 0 in case of success and negat     52  *      returns 0 in case of success and negative number in case of error.
 53  *      Returning SEQ_SKIP means "discard this     53  *      Returning SEQ_SKIP means "discard this element and move on".
 54  *      Note: seq_open() will allocate a struc     54  *      Note: seq_open() will allocate a struct seq_file and store its
 55  *      pointer in @file->private_data. This p     55  *      pointer in @file->private_data. This pointer should not be modified.
 56  */                                                56  */
 57 int seq_open(struct file *file, const struct s     57 int seq_open(struct file *file, const struct seq_operations *op)
 58 {                                                  58 {
 59         struct seq_file *p;                        59         struct seq_file *p;
 60                                                    60 
 61         WARN_ON(file->private_data);               61         WARN_ON(file->private_data);
 62                                                    62 
 63         p = kmem_cache_zalloc(seq_file_cache,      63         p = kmem_cache_zalloc(seq_file_cache, GFP_KERNEL);
 64         if (!p)                                    64         if (!p)
 65                 return -ENOMEM;                    65                 return -ENOMEM;
 66                                                    66 
 67         file->private_data = p;                    67         file->private_data = p;
 68                                                    68 
 69         mutex_init(&p->lock);                      69         mutex_init(&p->lock);
 70         p->op = op;                                70         p->op = op;
 71                                                    71 
 72         // No refcounting: the lifetime of 'p'     72         // No refcounting: the lifetime of 'p' is constrained
 73         // to the lifetime of the file.            73         // to the lifetime of the file.
 74         p->file = file;                            74         p->file = file;
 75                                                    75 
 76         /*                                         76         /*
 77          * seq_files support lseek() and pread     77          * seq_files support lseek() and pread().  They do not implement
 78          * write() at all, but we clear FMODE_     78          * write() at all, but we clear FMODE_PWRITE here for historical
 79          * reasons.                                79          * reasons.
 80          *                                         80          *
 81          * If a client of seq_files a) impleme     81          * If a client of seq_files a) implements file.write() and b) wishes to
 82          * support pwrite() then that client w     82          * support pwrite() then that client will need to implement its own
 83          * file.open() which calls seq_open()      83          * file.open() which calls seq_open() and then sets FMODE_PWRITE.
 84          */                                        84          */
 85         file->f_mode &= ~FMODE_PWRITE;             85         file->f_mode &= ~FMODE_PWRITE;
 86         return 0;                                  86         return 0;
 87 }                                                  87 }
 88 EXPORT_SYMBOL(seq_open);                           88 EXPORT_SYMBOL(seq_open);
 89                                                    89 
 90 static int traverse(struct seq_file *m, loff_t     90 static int traverse(struct seq_file *m, loff_t offset)
 91 {                                                  91 {
 92         loff_t pos = 0;                            92         loff_t pos = 0;
 93         int error = 0;                             93         int error = 0;
 94         void *p;                                   94         void *p;
 95                                                    95 
 96         m->index = 0;                              96         m->index = 0;
 97         m->count = m->from = 0;                    97         m->count = m->from = 0;
 98         if (!offset)                               98         if (!offset)
 99                 return 0;                          99                 return 0;
100                                                   100 
101         if (!m->buf) {                            101         if (!m->buf) {
102                 m->buf = seq_buf_alloc(m->size    102                 m->buf = seq_buf_alloc(m->size = PAGE_SIZE);
103                 if (!m->buf)                      103                 if (!m->buf)
104                         return -ENOMEM;           104                         return -ENOMEM;
105         }                                         105         }
106         p = m->op->start(m, &m->index);           106         p = m->op->start(m, &m->index);
107         while (p) {                               107         while (p) {
108                 error = PTR_ERR(p);               108                 error = PTR_ERR(p);
109                 if (IS_ERR(p))                    109                 if (IS_ERR(p))
110                         break;                    110                         break;
111                 error = m->op->show(m, p);        111                 error = m->op->show(m, p);
112                 if (error < 0)                    112                 if (error < 0)
113                         break;                    113                         break;
114                 if (unlikely(error)) {            114                 if (unlikely(error)) {
115                         error = 0;                115                         error = 0;
116                         m->count = 0;             116                         m->count = 0;
117                 }                                 117                 }
118                 if (seq_has_overflowed(m))        118                 if (seq_has_overflowed(m))
119                         goto Eoverflow;           119                         goto Eoverflow;
120                 p = m->op->next(m, p, &m->inde    120                 p = m->op->next(m, p, &m->index);
121                 if (pos + m->count > offset) {    121                 if (pos + m->count > offset) {
122                         m->from = offset - pos    122                         m->from = offset - pos;
123                         m->count -= m->from;      123                         m->count -= m->from;
124                         break;                    124                         break;
125                 }                                 125                 }
126                 pos += m->count;                  126                 pos += m->count;
127                 m->count = 0;                     127                 m->count = 0;
128                 if (pos == offset)                128                 if (pos == offset)
129                         break;                    129                         break;
130         }                                         130         }
131         m->op->stop(m, p);                        131         m->op->stop(m, p);
132         return error;                             132         return error;
133                                                   133 
134 Eoverflow:                                        134 Eoverflow:
135         m->op->stop(m, p);                        135         m->op->stop(m, p);
136         kvfree(m->buf);                           136         kvfree(m->buf);
137         m->count = 0;                             137         m->count = 0;
138         m->buf = seq_buf_alloc(m->size <<= 1);    138         m->buf = seq_buf_alloc(m->size <<= 1);
139         return !m->buf ? -ENOMEM : -EAGAIN;       139         return !m->buf ? -ENOMEM : -EAGAIN;
140 }                                                 140 }
141                                                   141 
142 /**                                               142 /**
143  *      seq_read -      ->read() method for se    143  *      seq_read -      ->read() method for sequential files.
144  *      @file: the file to read from              144  *      @file: the file to read from
145  *      @buf: the buffer to read to               145  *      @buf: the buffer to read to
146  *      @size: the maximum number of bytes to     146  *      @size: the maximum number of bytes to read
147  *      @ppos: the current position in the fil    147  *      @ppos: the current position in the file
148  *                                                148  *
149  *      Ready-made ->f_op->read()                 149  *      Ready-made ->f_op->read()
150  */                                               150  */
151 ssize_t seq_read(struct file *file, char __use    151 ssize_t seq_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
152 {                                                 152 {
153         struct iovec iov = { .iov_base = buf,     153         struct iovec iov = { .iov_base = buf, .iov_len = size};
154         struct kiocb kiocb;                       154         struct kiocb kiocb;
155         struct iov_iter iter;                     155         struct iov_iter iter;
156         ssize_t ret;                              156         ssize_t ret;
157                                                   157 
158         init_sync_kiocb(&kiocb, file);            158         init_sync_kiocb(&kiocb, file);
159         iov_iter_init(&iter, ITER_DEST, &iov,  !! 159         iov_iter_init(&iter, READ, &iov, 1, size);
160                                                   160 
161         kiocb.ki_pos = *ppos;                     161         kiocb.ki_pos = *ppos;
162         ret = seq_read_iter(&kiocb, &iter);       162         ret = seq_read_iter(&kiocb, &iter);
163         *ppos = kiocb.ki_pos;                     163         *ppos = kiocb.ki_pos;
164         return ret;                               164         return ret;
165 }                                                 165 }
166 EXPORT_SYMBOL(seq_read);                          166 EXPORT_SYMBOL(seq_read);
167                                                   167 
168 /*                                                168 /*
169  * Ready-made ->f_op->read_iter()                 169  * Ready-made ->f_op->read_iter()
170  */                                               170  */
171 ssize_t seq_read_iter(struct kiocb *iocb, stru    171 ssize_t seq_read_iter(struct kiocb *iocb, struct iov_iter *iter)
172 {                                                 172 {
173         struct seq_file *m = iocb->ki_filp->pr    173         struct seq_file *m = iocb->ki_filp->private_data;
174         size_t copied = 0;                        174         size_t copied = 0;
175         size_t n;                                 175         size_t n;
176         void *p;                                  176         void *p;
177         int err = 0;                              177         int err = 0;
178                                                   178 
179         if (!iov_iter_count(iter))                179         if (!iov_iter_count(iter))
180                 return 0;                         180                 return 0;
181                                                   181 
182         mutex_lock(&m->lock);                     182         mutex_lock(&m->lock);
183                                                   183 
184         /*                                        184         /*
185          * if request is to read from zero off    185          * if request is to read from zero offset, reset iterator to first
186          * record as it might have been alread    186          * record as it might have been already advanced by previous requests
187          */                                       187          */
188         if (iocb->ki_pos == 0) {                  188         if (iocb->ki_pos == 0) {
189                 m->index = 0;                     189                 m->index = 0;
190                 m->count = 0;                     190                 m->count = 0;
191         }                                         191         }
192                                                   192 
193         /* Don't assume ki_pos is where we lef    193         /* Don't assume ki_pos is where we left it */
194         if (unlikely(iocb->ki_pos != m->read_p    194         if (unlikely(iocb->ki_pos != m->read_pos)) {
195                 while ((err = traverse(m, iocb    195                 while ((err = traverse(m, iocb->ki_pos)) == -EAGAIN)
196                         ;                         196                         ;
197                 if (err) {                        197                 if (err) {
198                         /* With prejudice... *    198                         /* With prejudice... */
199                         m->read_pos = 0;          199                         m->read_pos = 0;
200                         m->index = 0;             200                         m->index = 0;
201                         m->count = 0;             201                         m->count = 0;
202                         goto Done;                202                         goto Done;
203                 } else {                          203                 } else {
204                         m->read_pos = iocb->ki    204                         m->read_pos = iocb->ki_pos;
205                 }                                 205                 }
206         }                                         206         }
207                                                   207 
208         /* grab buffer if we didn't have one *    208         /* grab buffer if we didn't have one */
209         if (!m->buf) {                            209         if (!m->buf) {
210                 m->buf = seq_buf_alloc(m->size    210                 m->buf = seq_buf_alloc(m->size = PAGE_SIZE);
211                 if (!m->buf)                      211                 if (!m->buf)
212                         goto Enomem;              212                         goto Enomem;
213         }                                         213         }
214         // something left in the buffer - copy    214         // something left in the buffer - copy it out first
215         if (m->count) {                           215         if (m->count) {
216                 n = copy_to_iter(m->buf + m->f    216                 n = copy_to_iter(m->buf + m->from, m->count, iter);
217                 m->count -= n;                    217                 m->count -= n;
218                 m->from += n;                     218                 m->from += n;
219                 copied += n;                      219                 copied += n;
220                 if (m->count)   // hadn't mana    220                 if (m->count)   // hadn't managed to copy everything
221                         goto Done;                221                         goto Done;
222         }                                         222         }
223         // get a non-empty record in the buffe    223         // get a non-empty record in the buffer
224         m->from = 0;                              224         m->from = 0;
225         p = m->op->start(m, &m->index);           225         p = m->op->start(m, &m->index);
226         while (1) {                               226         while (1) {
227                 err = PTR_ERR(p);                 227                 err = PTR_ERR(p);
228                 if (!p || IS_ERR(p))    // EOF    228                 if (!p || IS_ERR(p))    // EOF or an error
229                         break;                    229                         break;
230                 err = m->op->show(m, p);          230                 err = m->op->show(m, p);
231                 if (err < 0)            // har    231                 if (err < 0)            // hard error
232                         break;                    232                         break;
233                 if (unlikely(err))      // ->s    233                 if (unlikely(err))      // ->show() says "skip it"
234                         m->count = 0;             234                         m->count = 0;
235                 if (unlikely(!m->count)) { //     235                 if (unlikely(!m->count)) { // empty record
236                         p = m->op->next(m, p,     236                         p = m->op->next(m, p, &m->index);
237                         continue;                 237                         continue;
238                 }                                 238                 }
239                 if (!seq_has_overflowed(m)) //    239                 if (!seq_has_overflowed(m)) // got it
240                         goto Fill;                240                         goto Fill;
241                 // need a bigger buffer           241                 // need a bigger buffer
242                 m->op->stop(m, p);                242                 m->op->stop(m, p);
243                 kvfree(m->buf);                   243                 kvfree(m->buf);
244                 m->count = 0;                     244                 m->count = 0;
245                 m->buf = seq_buf_alloc(m->size    245                 m->buf = seq_buf_alloc(m->size <<= 1);
246                 if (!m->buf)                      246                 if (!m->buf)
247                         goto Enomem;              247                         goto Enomem;
248                 p = m->op->start(m, &m->index)    248                 p = m->op->start(m, &m->index);
249         }                                         249         }
250         // EOF or an error                        250         // EOF or an error
251         m->op->stop(m, p);                        251         m->op->stop(m, p);
252         m->count = 0;                             252         m->count = 0;
253         goto Done;                                253         goto Done;
254 Fill:                                             254 Fill:
255         // one non-empty record is in the buff    255         // one non-empty record is in the buffer; if they want more,
256         // try to fit more in, but in any case    256         // try to fit more in, but in any case we need to advance
257         // the iterator once for every record     257         // the iterator once for every record shown.
258         while (1) {                               258         while (1) {
259                 size_t offs = m->count;           259                 size_t offs = m->count;
260                 loff_t pos = m->index;            260                 loff_t pos = m->index;
261                                                   261 
262                 p = m->op->next(m, p, &m->inde    262                 p = m->op->next(m, p, &m->index);
263                 if (pos == m->index) {            263                 if (pos == m->index) {
264                         pr_info_ratelimited("b    264                         pr_info_ratelimited("buggy .next function %ps did not update position index\n",
265                                             m-    265                                             m->op->next);
266                         m->index++;               266                         m->index++;
267                 }                                 267                 }
268                 if (!p || IS_ERR(p))    // no     268                 if (!p || IS_ERR(p))    // no next record for us
269                         break;                    269                         break;
270                 if (m->count >= iov_iter_count    270                 if (m->count >= iov_iter_count(iter))
271                         break;                    271                         break;
272                 err = m->op->show(m, p);          272                 err = m->op->show(m, p);
273                 if (err > 0) {          // ->s    273                 if (err > 0) {          // ->show() says "skip it"
274                         m->count = offs;          274                         m->count = offs;
275                 } else if (err || seq_has_over    275                 } else if (err || seq_has_overflowed(m)) {
276                         m->count = offs;          276                         m->count = offs;
277                         break;                    277                         break;
278                 }                                 278                 }
279         }                                         279         }
280         m->op->stop(m, p);                        280         m->op->stop(m, p);
281         n = copy_to_iter(m->buf, m->count, ite    281         n = copy_to_iter(m->buf, m->count, iter);
282         copied += n;                              282         copied += n;
283         m->count -= n;                            283         m->count -= n;
284         m->from = n;                              284         m->from = n;
285 Done:                                             285 Done:
286         if (unlikely(!copied)) {                  286         if (unlikely(!copied)) {
287                 copied = m->count ? -EFAULT :     287                 copied = m->count ? -EFAULT : err;
288         } else {                                  288         } else {
289                 iocb->ki_pos += copied;           289                 iocb->ki_pos += copied;
290                 m->read_pos += copied;            290                 m->read_pos += copied;
291         }                                         291         }
292         mutex_unlock(&m->lock);                   292         mutex_unlock(&m->lock);
293         return copied;                            293         return copied;
294 Enomem:                                           294 Enomem:
295         err = -ENOMEM;                            295         err = -ENOMEM;
296         goto Done;                                296         goto Done;
297 }                                                 297 }
298 EXPORT_SYMBOL(seq_read_iter);                     298 EXPORT_SYMBOL(seq_read_iter);
299                                                   299 
300 /**                                               300 /**
301  *      seq_lseek -     ->llseek() method for     301  *      seq_lseek -     ->llseek() method for sequential files.
302  *      @file: the file in question               302  *      @file: the file in question
303  *      @offset: new position                     303  *      @offset: new position
304  *      @whence: 0 for absolute, 1 for relativ    304  *      @whence: 0 for absolute, 1 for relative position
305  *                                                305  *
306  *      Ready-made ->f_op->llseek()               306  *      Ready-made ->f_op->llseek()
307  */                                               307  */
308 loff_t seq_lseek(struct file *file, loff_t off    308 loff_t seq_lseek(struct file *file, loff_t offset, int whence)
309 {                                                 309 {
310         struct seq_file *m = file->private_dat    310         struct seq_file *m = file->private_data;
311         loff_t retval = -EINVAL;                  311         loff_t retval = -EINVAL;
312                                                   312 
313         mutex_lock(&m->lock);                     313         mutex_lock(&m->lock);
314         switch (whence) {                         314         switch (whence) {
315         case SEEK_CUR:                            315         case SEEK_CUR:
316                 offset += file->f_pos;            316                 offset += file->f_pos;
317                 fallthrough;                      317                 fallthrough;
318         case SEEK_SET:                            318         case SEEK_SET:
319                 if (offset < 0)                   319                 if (offset < 0)
320                         break;                    320                         break;
321                 retval = offset;                  321                 retval = offset;
322                 if (offset != m->read_pos) {      322                 if (offset != m->read_pos) {
323                         while ((retval = trave    323                         while ((retval = traverse(m, offset)) == -EAGAIN)
324                                 ;                 324                                 ;
325                         if (retval) {             325                         if (retval) {
326                                 /* with extrem    326                                 /* with extreme prejudice... */
327                                 file->f_pos =     327                                 file->f_pos = 0;
328                                 m->read_pos =     328                                 m->read_pos = 0;
329                                 m->index = 0;     329                                 m->index = 0;
330                                 m->count = 0;     330                                 m->count = 0;
331                         } else {                  331                         } else {
332                                 m->read_pos =     332                                 m->read_pos = offset;
333                                 retval = file-    333                                 retval = file->f_pos = offset;
334                         }                         334                         }
335                 } else {                          335                 } else {
336                         file->f_pos = offset;     336                         file->f_pos = offset;
337                 }                                 337                 }
338         }                                         338         }
339         mutex_unlock(&m->lock);                   339         mutex_unlock(&m->lock);
340         return retval;                            340         return retval;
341 }                                                 341 }
342 EXPORT_SYMBOL(seq_lseek);                         342 EXPORT_SYMBOL(seq_lseek);
343                                                   343 
344 /**                                               344 /**
345  *      seq_release -   free the structures as    345  *      seq_release -   free the structures associated with sequential file.
346  *      @file: file in question                   346  *      @file: file in question
347  *      @inode: its inode                         347  *      @inode: its inode
348  *                                                348  *
349  *      Frees the structures associated with s    349  *      Frees the structures associated with sequential file; can be used
350  *      as ->f_op->release() if you don't have    350  *      as ->f_op->release() if you don't have private data to destroy.
351  */                                               351  */
352 int seq_release(struct inode *inode, struct fi    352 int seq_release(struct inode *inode, struct file *file)
353 {                                                 353 {
354         struct seq_file *m = file->private_dat    354         struct seq_file *m = file->private_data;
355         kvfree(m->buf);                           355         kvfree(m->buf);
356         kmem_cache_free(seq_file_cache, m);       356         kmem_cache_free(seq_file_cache, m);
357         return 0;                                 357         return 0;
358 }                                                 358 }
359 EXPORT_SYMBOL(seq_release);                       359 EXPORT_SYMBOL(seq_release);
360                                                   360 
361 /**                                               361 /**
362  * seq_escape_mem - print data into buffer, es !! 362  *      seq_escape -    print string into buffer, escaping some characters
363  * @m: target buffer                           !! 363  *      @m:     target buffer
364  * @src: source buffer                         !! 364  *      @s:     string
365  * @len: size of source buffer                 !! 365  *      @esc:   set of characters that need escaping
366  * @flags: flags to pass to string_escape_mem( !! 366  *
367  * @esc: set of characters that need escaping  !! 367  *      Puts string into buffer, replacing each occurrence of character from
368  *                                             !! 368  *      @esc with usual octal escape.
369  * Puts data into buffer, replacing each occur !! 369  *      Use seq_has_overflowed() to check for errors.
370  * given class (defined by @flags and @esc) wi << 
371  *                                             << 
372  * Use seq_has_overflowed() to check for error << 
373  */                                               370  */
374 void seq_escape_mem(struct seq_file *m, const  !! 371 void seq_escape(struct seq_file *m, const char *s, const char *esc)
375                     unsigned int flags, const  << 
376 {                                                 372 {
377         char *buf;                                373         char *buf;
378         size_t size = seq_get_buf(m, &buf);       374         size_t size = seq_get_buf(m, &buf);
379         int ret;                                  375         int ret;
380                                                   376 
381         ret = string_escape_mem(src, len, buf, !! 377         ret = string_escape_str(s, buf, size, ESCAPE_OCTAL, esc);
382         seq_commit(m, ret < size ? ret : -1);     378         seq_commit(m, ret < size ? ret : -1);
383 }                                                 379 }
384 EXPORT_SYMBOL(seq_escape_mem);                 !! 380 EXPORT_SYMBOL(seq_escape);
                                                   >> 381 
                                                   >> 382 void seq_escape_mem_ascii(struct seq_file *m, const char *src, size_t isz)
                                                   >> 383 {
                                                   >> 384         char *buf;
                                                   >> 385         size_t size = seq_get_buf(m, &buf);
                                                   >> 386         int ret;
                                                   >> 387 
                                                   >> 388         ret = string_escape_mem_ascii(src, isz, buf, size);
                                                   >> 389         seq_commit(m, ret < size ? ret : -1);
                                                   >> 390 }
                                                   >> 391 EXPORT_SYMBOL(seq_escape_mem_ascii);
385                                                   392 
386 void seq_vprintf(struct seq_file *m, const cha    393 void seq_vprintf(struct seq_file *m, const char *f, va_list args)
387 {                                                 394 {
388         int len;                                  395         int len;
389                                                   396 
390         if (m->count < m->size) {                 397         if (m->count < m->size) {
391                 len = vsnprintf(m->buf + m->co    398                 len = vsnprintf(m->buf + m->count, m->size - m->count, f, args);
392                 if (m->count + len < m->size)     399                 if (m->count + len < m->size) {
393                         m->count += len;          400                         m->count += len;
394                         return;                   401                         return;
395                 }                                 402                 }
396         }                                         403         }
397         seq_set_overflow(m);                      404         seq_set_overflow(m);
398 }                                                 405 }
399 EXPORT_SYMBOL(seq_vprintf);                       406 EXPORT_SYMBOL(seq_vprintf);
400                                                   407 
401 void seq_printf(struct seq_file *m, const char    408 void seq_printf(struct seq_file *m, const char *f, ...)
402 {                                                 409 {
403         va_list args;                             410         va_list args;
404                                                   411 
405         va_start(args, f);                        412         va_start(args, f);
406         seq_vprintf(m, f, args);                  413         seq_vprintf(m, f, args);
407         va_end(args);                             414         va_end(args);
408 }                                                 415 }
409 EXPORT_SYMBOL(seq_printf);                        416 EXPORT_SYMBOL(seq_printf);
410                                                   417 
411 #ifdef CONFIG_BINARY_PRINTF                    << 
412 void seq_bprintf(struct seq_file *m, const cha << 
413 {                                              << 
414         int len;                               << 
415                                                << 
416         if (m->count < m->size) {              << 
417                 len = bstr_printf(m->buf + m-> << 
418                                   binary);     << 
419                 if (m->count + len < m->size)  << 
420                         m->count += len;       << 
421                         return;                << 
422                 }                              << 
423         }                                      << 
424         seq_set_overflow(m);                   << 
425 }                                              << 
426 EXPORT_SYMBOL(seq_bprintf);                    << 
427 #endif /* CONFIG_BINARY_PRINTF */              << 
428                                                << 
429 /**                                               418 /**
430  *      mangle_path -   mangle and copy path t    419  *      mangle_path -   mangle and copy path to buffer beginning
431  *      @s: buffer start                          420  *      @s: buffer start
432  *      @p: beginning of path in above buffer     421  *      @p: beginning of path in above buffer
433  *      @esc: set of characters that need esca    422  *      @esc: set of characters that need escaping
434  *                                                423  *
435  *      Copy the path from @p to @s, replacing    424  *      Copy the path from @p to @s, replacing each occurrence of character from
436  *      @esc with usual octal escape.             425  *      @esc with usual octal escape.
437  *      Returns pointer past last written char    426  *      Returns pointer past last written character in @s, or NULL in case of
438  *      failure.                                  427  *      failure.
439  */                                               428  */
440 char *mangle_path(char *s, const char *p, cons    429 char *mangle_path(char *s, const char *p, const char *esc)
441 {                                                 430 {
442         while (s <= p) {                          431         while (s <= p) {
443                 char c = *p++;                    432                 char c = *p++;
444                 if (!c) {                         433                 if (!c) {
445                         return s;                 434                         return s;
446                 } else if (!strchr(esc, c)) {     435                 } else if (!strchr(esc, c)) {
447                         *s++ = c;                 436                         *s++ = c;
448                 } else if (s + 4 > p) {           437                 } else if (s + 4 > p) {
449                         break;                    438                         break;
450                 } else {                          439                 } else {
451                         *s++ = '\\';              440                         *s++ = '\\';
452                         *s++ = '' + ((c & 0300    441                         *s++ = '' + ((c & 0300) >> 6);
453                         *s++ = '' + ((c & 070)    442                         *s++ = '' + ((c & 070) >> 3);
454                         *s++ = '' + (c & 07);     443                         *s++ = '' + (c & 07);
455                 }                                 444                 }
456         }                                         445         }
457         return NULL;                              446         return NULL;
458 }                                                 447 }
459 EXPORT_SYMBOL(mangle_path);                       448 EXPORT_SYMBOL(mangle_path);
460                                                   449 
461 /**                                               450 /**
462  * seq_path - seq_file interface to print a pa    451  * seq_path - seq_file interface to print a pathname
463  * @m: the seq_file handle                        452  * @m: the seq_file handle
464  * @path: the struct path to print                453  * @path: the struct path to print
465  * @esc: set of characters to escape in the ou    454  * @esc: set of characters to escape in the output
466  *                                                455  *
467  * return the absolute path of 'path', as repr    456  * return the absolute path of 'path', as represented by the
468  * dentry / mnt pair in the path parameter.       457  * dentry / mnt pair in the path parameter.
469  */                                               458  */
470 int seq_path(struct seq_file *m, const struct     459 int seq_path(struct seq_file *m, const struct path *path, const char *esc)
471 {                                                 460 {
472         char *buf;                                461         char *buf;
473         size_t size = seq_get_buf(m, &buf);       462         size_t size = seq_get_buf(m, &buf);
474         int res = -1;                             463         int res = -1;
475                                                   464 
476         if (size) {                               465         if (size) {
477                 char *p = d_path(path, buf, si    466                 char *p = d_path(path, buf, size);
478                 if (!IS_ERR(p)) {                 467                 if (!IS_ERR(p)) {
479                         char *end = mangle_pat    468                         char *end = mangle_path(buf, p, esc);
480                         if (end)                  469                         if (end)
481                                 res = end - bu    470                                 res = end - buf;
482                 }                                 471                 }
483         }                                         472         }
484         seq_commit(m, res);                       473         seq_commit(m, res);
485                                                   474 
486         return res;                               475         return res;
487 }                                                 476 }
488 EXPORT_SYMBOL(seq_path);                          477 EXPORT_SYMBOL(seq_path);
489                                                   478 
490 /**                                               479 /**
491  * seq_file_path - seq_file interface to print    480  * seq_file_path - seq_file interface to print a pathname of a file
492  * @m: the seq_file handle                        481  * @m: the seq_file handle
493  * @file: the struct file to print                482  * @file: the struct file to print
494  * @esc: set of characters to escape in the ou    483  * @esc: set of characters to escape in the output
495  *                                                484  *
496  * return the absolute path to the file.          485  * return the absolute path to the file.
497  */                                               486  */
498 int seq_file_path(struct seq_file *m, struct f    487 int seq_file_path(struct seq_file *m, struct file *file, const char *esc)
499 {                                                 488 {
500         return seq_path(m, &file->f_path, esc)    489         return seq_path(m, &file->f_path, esc);
501 }                                                 490 }
502 EXPORT_SYMBOL(seq_file_path);                     491 EXPORT_SYMBOL(seq_file_path);
503                                                   492 
504 /*                                                493 /*
505  * Same as seq_path, but relative to supplied     494  * Same as seq_path, but relative to supplied root.
506  */                                               495  */
507 int seq_path_root(struct seq_file *m, const st    496 int seq_path_root(struct seq_file *m, const struct path *path,
508                   const struct path *root, con    497                   const struct path *root, const char *esc)
509 {                                                 498 {
510         char *buf;                                499         char *buf;
511         size_t size = seq_get_buf(m, &buf);       500         size_t size = seq_get_buf(m, &buf);
512         int res = -ENAMETOOLONG;                  501         int res = -ENAMETOOLONG;
513                                                   502 
514         if (size) {                               503         if (size) {
515                 char *p;                          504                 char *p;
516                                                   505 
517                 p = __d_path(path, root, buf,     506                 p = __d_path(path, root, buf, size);
518                 if (!p)                           507                 if (!p)
519                         return SEQ_SKIP;          508                         return SEQ_SKIP;
520                 res = PTR_ERR(p);                 509                 res = PTR_ERR(p);
521                 if (!IS_ERR(p)) {                 510                 if (!IS_ERR(p)) {
522                         char *end = mangle_pat    511                         char *end = mangle_path(buf, p, esc);
523                         if (end)                  512                         if (end)
524                                 res = end - bu    513                                 res = end - buf;
525                         else                      514                         else
526                                 res = -ENAMETO    515                                 res = -ENAMETOOLONG;
527                 }                                 516                 }
528         }                                         517         }
529         seq_commit(m, res);                       518         seq_commit(m, res);
530                                                   519 
531         return res < 0 && res != -ENAMETOOLONG    520         return res < 0 && res != -ENAMETOOLONG ? res : 0;
532 }                                                 521 }
533                                                   522 
534 /*                                                523 /*
535  * returns the path of the 'dentry' from the r    524  * returns the path of the 'dentry' from the root of its filesystem.
536  */                                               525  */
537 int seq_dentry(struct seq_file *m, struct dent    526 int seq_dentry(struct seq_file *m, struct dentry *dentry, const char *esc)
538 {                                                 527 {
539         char *buf;                                528         char *buf;
540         size_t size = seq_get_buf(m, &buf);       529         size_t size = seq_get_buf(m, &buf);
541         int res = -1;                             530         int res = -1;
542                                                   531 
543         if (size) {                               532         if (size) {
544                 char *p = dentry_path(dentry,     533                 char *p = dentry_path(dentry, buf, size);
545                 if (!IS_ERR(p)) {                 534                 if (!IS_ERR(p)) {
546                         char *end = mangle_pat    535                         char *end = mangle_path(buf, p, esc);
547                         if (end)                  536                         if (end)
548                                 res = end - bu    537                                 res = end - buf;
549                 }                                 538                 }
550         }                                         539         }
551         seq_commit(m, res);                       540         seq_commit(m, res);
552                                                   541 
553         return res;                               542         return res;
554 }                                                 543 }
555 EXPORT_SYMBOL(seq_dentry);                        544 EXPORT_SYMBOL(seq_dentry);
556                                                   545 
557 void *single_start(struct seq_file *p, loff_t  !! 546 static void *single_start(struct seq_file *p, loff_t *pos)
558 {                                                 547 {
559         return *pos ? NULL : SEQ_START_TOKEN;  !! 548         return NULL + (*pos == 0);
560 }                                                 549 }
561                                                   550 
562 static void *single_next(struct seq_file *p, v    551 static void *single_next(struct seq_file *p, void *v, loff_t *pos)
563 {                                                 552 {
564         ++*pos;                                   553         ++*pos;
565         return NULL;                              554         return NULL;
566 }                                                 555 }
567                                                   556 
568 static void single_stop(struct seq_file *p, vo    557 static void single_stop(struct seq_file *p, void *v)
569 {                                                 558 {
570 }                                                 559 }
571                                                   560 
572 int single_open(struct file *file, int (*show)    561 int single_open(struct file *file, int (*show)(struct seq_file *, void *),
573                 void *data)                       562                 void *data)
574 {                                                 563 {
575         struct seq_operations *op = kmalloc(si    564         struct seq_operations *op = kmalloc(sizeof(*op), GFP_KERNEL_ACCOUNT);
576         int res = -ENOMEM;                        565         int res = -ENOMEM;
577                                                   566 
578         if (op) {                                 567         if (op) {
579                 op->start = single_start;         568                 op->start = single_start;
580                 op->next = single_next;           569                 op->next = single_next;
581                 op->stop = single_stop;           570                 op->stop = single_stop;
582                 op->show = show;                  571                 op->show = show;
583                 res = seq_open(file, op);         572                 res = seq_open(file, op);
584                 if (!res)                         573                 if (!res)
585                         ((struct seq_file *)fi    574                         ((struct seq_file *)file->private_data)->private = data;
586                 else                              575                 else
587                         kfree(op);                576                         kfree(op);
588         }                                         577         }
589         return res;                               578         return res;
590 }                                                 579 }
591 EXPORT_SYMBOL(single_open);                       580 EXPORT_SYMBOL(single_open);
592                                                   581 
593 int single_open_size(struct file *file, int (*    582 int single_open_size(struct file *file, int (*show)(struct seq_file *, void *),
594                 void *data, size_t size)          583                 void *data, size_t size)
595 {                                                 584 {
596         char *buf = seq_buf_alloc(size);          585         char *buf = seq_buf_alloc(size);
597         int ret;                                  586         int ret;
598         if (!buf)                                 587         if (!buf)
599                 return -ENOMEM;                   588                 return -ENOMEM;
600         ret = single_open(file, show, data);      589         ret = single_open(file, show, data);
601         if (ret) {                                590         if (ret) {
602                 kvfree(buf);                      591                 kvfree(buf);
603                 return ret;                       592                 return ret;
604         }                                         593         }
605         ((struct seq_file *)file->private_data    594         ((struct seq_file *)file->private_data)->buf = buf;
606         ((struct seq_file *)file->private_data    595         ((struct seq_file *)file->private_data)->size = size;
607         return 0;                                 596         return 0;
608 }                                                 597 }
609 EXPORT_SYMBOL(single_open_size);                  598 EXPORT_SYMBOL(single_open_size);
610                                                   599 
611 int single_release(struct inode *inode, struct    600 int single_release(struct inode *inode, struct file *file)
612 {                                                 601 {
613         const struct seq_operations *op = ((st    602         const struct seq_operations *op = ((struct seq_file *)file->private_data)->op;
614         int res = seq_release(inode, file);       603         int res = seq_release(inode, file);
615         kfree(op);                                604         kfree(op);
616         return res;                               605         return res;
617 }                                                 606 }
618 EXPORT_SYMBOL(single_release);                    607 EXPORT_SYMBOL(single_release);
619                                                   608 
620 int seq_release_private(struct inode *inode, s    609 int seq_release_private(struct inode *inode, struct file *file)
621 {                                                 610 {
622         struct seq_file *seq = file->private_d    611         struct seq_file *seq = file->private_data;
623                                                   612 
624         kfree(seq->private);                      613         kfree(seq->private);
625         seq->private = NULL;                      614         seq->private = NULL;
626         return seq_release(inode, file);          615         return seq_release(inode, file);
627 }                                                 616 }
628 EXPORT_SYMBOL(seq_release_private);               617 EXPORT_SYMBOL(seq_release_private);
629                                                   618 
630 void *__seq_open_private(struct file *f, const    619 void *__seq_open_private(struct file *f, const struct seq_operations *ops,
631                 int psize)                        620                 int psize)
632 {                                                 621 {
633         int rc;                                   622         int rc;
634         void *private;                            623         void *private;
635         struct seq_file *seq;                     624         struct seq_file *seq;
636                                                   625 
637         private = kzalloc(psize, GFP_KERNEL_AC    626         private = kzalloc(psize, GFP_KERNEL_ACCOUNT);
638         if (private == NULL)                      627         if (private == NULL)
639                 goto out;                         628                 goto out;
640                                                   629 
641         rc = seq_open(f, ops);                    630         rc = seq_open(f, ops);
642         if (rc < 0)                               631         if (rc < 0)
643                 goto out_free;                    632                 goto out_free;
644                                                   633 
645         seq = f->private_data;                    634         seq = f->private_data;
646         seq->private = private;                   635         seq->private = private;
647         return private;                           636         return private;
648                                                   637 
649 out_free:                                         638 out_free:
650         kfree(private);                           639         kfree(private);
651 out:                                              640 out:
652         return NULL;                              641         return NULL;
653 }                                                 642 }
654 EXPORT_SYMBOL(__seq_open_private);                643 EXPORT_SYMBOL(__seq_open_private);
655                                                   644 
656 int seq_open_private(struct file *filp, const     645 int seq_open_private(struct file *filp, const struct seq_operations *ops,
657                 int psize)                        646                 int psize)
658 {                                                 647 {
659         return __seq_open_private(filp, ops, p    648         return __seq_open_private(filp, ops, psize) ? 0 : -ENOMEM;
660 }                                                 649 }
661 EXPORT_SYMBOL(seq_open_private);                  650 EXPORT_SYMBOL(seq_open_private);
662                                                   651 
663 void seq_putc(struct seq_file *m, char c)         652 void seq_putc(struct seq_file *m, char c)
664 {                                                 653 {
665         if (m->count >= m->size)                  654         if (m->count >= m->size)
666                 return;                           655                 return;
667                                                   656 
668         m->buf[m->count++] = c;                   657         m->buf[m->count++] = c;
669 }                                                 658 }
670 EXPORT_SYMBOL(seq_putc);                          659 EXPORT_SYMBOL(seq_putc);
671                                                   660 
672 void __seq_puts(struct seq_file *m, const char !! 661 void seq_puts(struct seq_file *m, const char *s)
673 {                                                 662 {
674         seq_write(m, s, strlen(s));            !! 663         int len = strlen(s);
                                                   >> 664 
                                                   >> 665         if (m->count + len >= m->size) {
                                                   >> 666                 seq_set_overflow(m);
                                                   >> 667                 return;
                                                   >> 668         }
                                                   >> 669         memcpy(m->buf + m->count, s, len);
                                                   >> 670         m->count += len;
675 }                                                 671 }
676 EXPORT_SYMBOL(__seq_puts);                     !! 672 EXPORT_SYMBOL(seq_puts);
677                                                   673 
678 /**                                               674 /**
679  * seq_put_decimal_ull_width - A helper routin    675  * seq_put_decimal_ull_width - A helper routine for putting decimal numbers
680  *                             without rich fo    676  *                             without rich format of printf().
681  * only 'unsigned long long' is supported.        677  * only 'unsigned long long' is supported.
682  * @m: seq_file identifying the buffer to whic    678  * @m: seq_file identifying the buffer to which data should be written
683  * @delimiter: a string which is printed befor    679  * @delimiter: a string which is printed before the number
684  * @num: the number                               680  * @num: the number
685  * @width: a minimum field width                  681  * @width: a minimum field width
686  *                                                682  *
687  * This routine will put strlen(delimiter) + n    683  * This routine will put strlen(delimiter) + number into seq_filed.
688  * This routine is very quick when you show lo    684  * This routine is very quick when you show lots of numbers.
689  * In usual cases, it will be better to use se    685  * In usual cases, it will be better to use seq_printf(). It's easier to read.
690  */                                               686  */
691 void seq_put_decimal_ull_width(struct seq_file    687 void seq_put_decimal_ull_width(struct seq_file *m, const char *delimiter,
692                          unsigned long long nu    688                          unsigned long long num, unsigned int width)
693 {                                                 689 {
694         int len;                                  690         int len;
695                                                   691 
696         if (m->count + 2 >= m->size) /* we'll     692         if (m->count + 2 >= m->size) /* we'll write 2 bytes at least */
697                 goto overflow;                    693                 goto overflow;
698                                                   694 
699         if (delimiter && delimiter[0]) {          695         if (delimiter && delimiter[0]) {
700                 if (delimiter[1] == 0)            696                 if (delimiter[1] == 0)
701                         seq_putc(m, delimiter[    697                         seq_putc(m, delimiter[0]);
702                 else                              698                 else
703                         seq_puts(m, delimiter)    699                         seq_puts(m, delimiter);
704         }                                         700         }
705                                                   701 
706         if (!width)                               702         if (!width)
707                 width = 1;                        703                 width = 1;
708                                                   704 
709         if (m->count + width >= m->size)          705         if (m->count + width >= m->size)
710                 goto overflow;                    706                 goto overflow;
711                                                   707 
712         len = num_to_str(m->buf + m->count, m-    708         len = num_to_str(m->buf + m->count, m->size - m->count, num, width);
713         if (!len)                                 709         if (!len)
714                 goto overflow;                    710                 goto overflow;
715                                                   711 
716         m->count += len;                          712         m->count += len;
717         return;                                   713         return;
718                                                   714 
719 overflow:                                         715 overflow:
720         seq_set_overflow(m);                      716         seq_set_overflow(m);
721 }                                                 717 }
722                                                   718 
723 void seq_put_decimal_ull(struct seq_file *m, c    719 void seq_put_decimal_ull(struct seq_file *m, const char *delimiter,
724                          unsigned long long nu    720                          unsigned long long num)
725 {                                                 721 {
726         return seq_put_decimal_ull_width(m, de    722         return seq_put_decimal_ull_width(m, delimiter, num, 0);
727 }                                                 723 }
728 EXPORT_SYMBOL(seq_put_decimal_ull);               724 EXPORT_SYMBOL(seq_put_decimal_ull);
729                                                   725 
730 /**                                               726 /**
731  * seq_put_hex_ll - put a number in hexadecima    727  * seq_put_hex_ll - put a number in hexadecimal notation
732  * @m: seq_file identifying the buffer to whic    728  * @m: seq_file identifying the buffer to which data should be written
733  * @delimiter: a string which is printed befor    729  * @delimiter: a string which is printed before the number
734  * @v: the number                                 730  * @v: the number
735  * @width: a minimum field width                  731  * @width: a minimum field width
736  *                                                732  *
737  * seq_put_hex_ll(m, "", v, 8) is equal to seq    733  * seq_put_hex_ll(m, "", v, 8) is equal to seq_printf(m, "%08llx", v)
738  *                                                734  *
739  * This routine is very quick when you show lo    735  * This routine is very quick when you show lots of numbers.
740  * In usual cases, it will be better to use se    736  * In usual cases, it will be better to use seq_printf(). It's easier to read.
741  */                                               737  */
742 void seq_put_hex_ll(struct seq_file *m, const     738 void seq_put_hex_ll(struct seq_file *m, const char *delimiter,
743                                 unsigned long     739                                 unsigned long long v, unsigned int width)
744 {                                                 740 {
745         unsigned int len;                         741         unsigned int len;
746         int i;                                    742         int i;
747                                                   743 
748         if (delimiter && delimiter[0]) {          744         if (delimiter && delimiter[0]) {
749                 if (delimiter[1] == 0)            745                 if (delimiter[1] == 0)
750                         seq_putc(m, delimiter[    746                         seq_putc(m, delimiter[0]);
751                 else                              747                 else
752                         seq_puts(m, delimiter)    748                         seq_puts(m, delimiter);
753         }                                         749         }
754                                                   750 
755         /* If x is 0, the result of __builtin_    751         /* If x is 0, the result of __builtin_clzll is undefined */
756         if (v == 0)                               752         if (v == 0)
757                 len = 1;                          753                 len = 1;
758         else                                      754         else
759                 len = (sizeof(v) * 8 - __built    755                 len = (sizeof(v) * 8 - __builtin_clzll(v) + 3) / 4;
760                                                   756 
761         if (len < width)                          757         if (len < width)
762                 len = width;                      758                 len = width;
763                                                   759 
764         if (m->count + len > m->size) {           760         if (m->count + len > m->size) {
765                 seq_set_overflow(m);              761                 seq_set_overflow(m);
766                 return;                           762                 return;
767         }                                         763         }
768                                                   764 
769         for (i = len - 1; i >= 0; i--) {          765         for (i = len - 1; i >= 0; i--) {
770                 m->buf[m->count + i] = hex_asc    766                 m->buf[m->count + i] = hex_asc[0xf & v];
771                 v = v >> 4;                       767                 v = v >> 4;
772         }                                         768         }
773         m->count += len;                          769         m->count += len;
774 }                                                 770 }
775                                                   771 
776 void seq_put_decimal_ll(struct seq_file *m, co    772 void seq_put_decimal_ll(struct seq_file *m, const char *delimiter, long long num)
777 {                                                 773 {
778         int len;                                  774         int len;
779                                                   775 
780         if (m->count + 3 >= m->size) /* we'll     776         if (m->count + 3 >= m->size) /* we'll write 2 bytes at least */
781                 goto overflow;                    777                 goto overflow;
782                                                   778 
783         if (delimiter && delimiter[0]) {          779         if (delimiter && delimiter[0]) {
784                 if (delimiter[1] == 0)            780                 if (delimiter[1] == 0)
785                         seq_putc(m, delimiter[    781                         seq_putc(m, delimiter[0]);
786                 else                              782                 else
787                         seq_puts(m, delimiter)    783                         seq_puts(m, delimiter);
788         }                                         784         }
789                                                   785 
790         if (m->count + 2 >= m->size)              786         if (m->count + 2 >= m->size)
791                 goto overflow;                    787                 goto overflow;
792                                                   788 
793         if (num < 0) {                            789         if (num < 0) {
794                 m->buf[m->count++] = '-';         790                 m->buf[m->count++] = '-';
795                 num = -num;                       791                 num = -num;
796         }                                         792         }
797                                                   793 
798         if (num < 10) {                           794         if (num < 10) {
799                 m->buf[m->count++] = num + '';    795                 m->buf[m->count++] = num + '';
800                 return;                           796                 return;
801         }                                         797         }
802                                                   798 
803         len = num_to_str(m->buf + m->count, m-    799         len = num_to_str(m->buf + m->count, m->size - m->count, num, 0);
804         if (!len)                                 800         if (!len)
805                 goto overflow;                    801                 goto overflow;
806                                                   802 
807         m->count += len;                          803         m->count += len;
808         return;                                   804         return;
809                                                   805 
810 overflow:                                         806 overflow:
811         seq_set_overflow(m);                      807         seq_set_overflow(m);
812 }                                                 808 }
813 EXPORT_SYMBOL(seq_put_decimal_ll);                809 EXPORT_SYMBOL(seq_put_decimal_ll);
814                                                   810 
815 /**                                               811 /**
816  * seq_write - write arbitrary data to buffer     812  * seq_write - write arbitrary data to buffer
817  * @seq: seq_file identifying the buffer to wh    813  * @seq: seq_file identifying the buffer to which data should be written
818  * @data: data address                            814  * @data: data address
819  * @len: number of bytes                          815  * @len: number of bytes
820  *                                                816  *
821  * Return 0 on success, non-zero otherwise.       817  * Return 0 on success, non-zero otherwise.
822  */                                               818  */
823 int seq_write(struct seq_file *seq, const void    819 int seq_write(struct seq_file *seq, const void *data, size_t len)
824 {                                                 820 {
825         if (seq->count + len < seq->size) {       821         if (seq->count + len < seq->size) {
826                 memcpy(seq->buf + seq->count,     822                 memcpy(seq->buf + seq->count, data, len);
827                 seq->count += len;                823                 seq->count += len;
828                 return 0;                         824                 return 0;
829         }                                         825         }
830         seq_set_overflow(seq);                    826         seq_set_overflow(seq);
831         return -1;                                827         return -1;
832 }                                                 828 }
833 EXPORT_SYMBOL(seq_write);                         829 EXPORT_SYMBOL(seq_write);
834                                                   830 
835 /**                                               831 /**
836  * seq_pad - write padding spaces to buffer       832  * seq_pad - write padding spaces to buffer
837  * @m: seq_file identifying the buffer to whic    833  * @m: seq_file identifying the buffer to which data should be written
838  * @c: the byte to append after padding if non    834  * @c: the byte to append after padding if non-zero
839  */                                               835  */
840 void seq_pad(struct seq_file *m, char c)          836 void seq_pad(struct seq_file *m, char c)
841 {                                                 837 {
842         int size = m->pad_until - m->count;       838         int size = m->pad_until - m->count;
843         if (size > 0) {                           839         if (size > 0) {
844                 if (size + m->count > m->size)    840                 if (size + m->count > m->size) {
845                         seq_set_overflow(m);      841                         seq_set_overflow(m);
846                         return;                   842                         return;
847                 }                                 843                 }
848                 memset(m->buf + m->count, ' ',    844                 memset(m->buf + m->count, ' ', size);
849                 m->count += size;                 845                 m->count += size;
850         }                                         846         }
851         if (c)                                    847         if (c)
852                 seq_putc(m, c);                   848                 seq_putc(m, c);
853 }                                                 849 }
854 EXPORT_SYMBOL(seq_pad);                           850 EXPORT_SYMBOL(seq_pad);
855                                                   851 
856 /* A complete analogue of print_hex_dump() */     852 /* A complete analogue of print_hex_dump() */
857 void seq_hex_dump(struct seq_file *m, const ch    853 void seq_hex_dump(struct seq_file *m, const char *prefix_str, int prefix_type,
858                   int rowsize, int groupsize,     854                   int rowsize, int groupsize, const void *buf, size_t len,
859                   bool ascii)                     855                   bool ascii)
860 {                                                 856 {
861         const u8 *ptr = buf;                      857         const u8 *ptr = buf;
862         int i, linelen, remaining = len;          858         int i, linelen, remaining = len;
863         char *buffer;                             859         char *buffer;
864         size_t size;                              860         size_t size;
865         int ret;                                  861         int ret;
866                                                   862 
867         if (rowsize != 16 && rowsize != 32)       863         if (rowsize != 16 && rowsize != 32)
868                 rowsize = 16;                     864                 rowsize = 16;
869                                                   865 
870         for (i = 0; i < len && !seq_has_overfl    866         for (i = 0; i < len && !seq_has_overflowed(m); i += rowsize) {
871                 linelen = min(remaining, rowsi    867                 linelen = min(remaining, rowsize);
872                 remaining -= rowsize;             868                 remaining -= rowsize;
873                                                   869 
874                 switch (prefix_type) {            870                 switch (prefix_type) {
875                 case DUMP_PREFIX_ADDRESS:         871                 case DUMP_PREFIX_ADDRESS:
876                         seq_printf(m, "%s%p: "    872                         seq_printf(m, "%s%p: ", prefix_str, ptr + i);
877                         break;                    873                         break;
878                 case DUMP_PREFIX_OFFSET:          874                 case DUMP_PREFIX_OFFSET:
879                         seq_printf(m, "%s%.8x:    875                         seq_printf(m, "%s%.8x: ", prefix_str, i);
880                         break;                    876                         break;
881                 default:                          877                 default:
882                         seq_printf(m, "%s", pr    878                         seq_printf(m, "%s", prefix_str);
883                         break;                    879                         break;
884                 }                                 880                 }
885                                                   881 
886                 size = seq_get_buf(m, &buffer)    882                 size = seq_get_buf(m, &buffer);
887                 ret = hex_dump_to_buffer(ptr +    883                 ret = hex_dump_to_buffer(ptr + i, linelen, rowsize, groupsize,
888                                          buffe    884                                          buffer, size, ascii);
889                 seq_commit(m, ret < size ? ret    885                 seq_commit(m, ret < size ? ret : -1);
890                                                   886 
891                 seq_putc(m, '\n');                887                 seq_putc(m, '\n');
892         }                                         888         }
893 }                                                 889 }
894 EXPORT_SYMBOL(seq_hex_dump);                      890 EXPORT_SYMBOL(seq_hex_dump);
895                                                   891 
896 struct list_head *seq_list_start(struct list_h    892 struct list_head *seq_list_start(struct list_head *head, loff_t pos)
897 {                                                 893 {
898         struct list_head *lh;                     894         struct list_head *lh;
899                                                   895 
900         list_for_each(lh, head)                   896         list_for_each(lh, head)
901                 if (pos-- == 0)                   897                 if (pos-- == 0)
902                         return lh;                898                         return lh;
903                                                   899 
904         return NULL;                              900         return NULL;
905 }                                                 901 }
906 EXPORT_SYMBOL(seq_list_start);                    902 EXPORT_SYMBOL(seq_list_start);
907                                                   903 
908 struct list_head *seq_list_start_head(struct l    904 struct list_head *seq_list_start_head(struct list_head *head, loff_t pos)
909 {                                                 905 {
910         if (!pos)                                 906         if (!pos)
911                 return head;                      907                 return head;
912                                                   908 
913         return seq_list_start(head, pos - 1);     909         return seq_list_start(head, pos - 1);
914 }                                                 910 }
915 EXPORT_SYMBOL(seq_list_start_head);               911 EXPORT_SYMBOL(seq_list_start_head);
916                                                   912 
917 struct list_head *seq_list_next(void *v, struc    913 struct list_head *seq_list_next(void *v, struct list_head *head, loff_t *ppos)
918 {                                                 914 {
919         struct list_head *lh;                     915         struct list_head *lh;
920                                                   916 
921         lh = ((struct list_head *)v)->next;       917         lh = ((struct list_head *)v)->next;
922         ++*ppos;                                  918         ++*ppos;
923         return lh == head ? NULL : lh;            919         return lh == head ? NULL : lh;
924 }                                                 920 }
925 EXPORT_SYMBOL(seq_list_next);                     921 EXPORT_SYMBOL(seq_list_next);
926                                                << 
927 struct list_head *seq_list_start_rcu(struct li << 
928 {                                              << 
929         struct list_head *lh;                  << 
930                                                << 
931         list_for_each_rcu(lh, head)            << 
932                 if (pos-- == 0)                << 
933                         return lh;             << 
934                                                << 
935         return NULL;                           << 
936 }                                              << 
937 EXPORT_SYMBOL(seq_list_start_rcu);             << 
938                                                << 
939 struct list_head *seq_list_start_head_rcu(stru << 
940 {                                              << 
941         if (!pos)                              << 
942                 return head;                   << 
943                                                << 
944         return seq_list_start_rcu(head, pos -  << 
945 }                                              << 
946 EXPORT_SYMBOL(seq_list_start_head_rcu);        << 
947                                                << 
948 struct list_head *seq_list_next_rcu(void *v, s << 
949                                     loff_t *pp << 
950 {                                              << 
951         struct list_head *lh;                  << 
952                                                << 
953         lh = list_next_rcu((struct list_head * << 
954         ++*ppos;                               << 
955         return lh == head ? NULL : lh;         << 
956 }                                              << 
957 EXPORT_SYMBOL(seq_list_next_rcu);              << 
958                                                   922 
959 /**                                               923 /**
960  * seq_hlist_start - start an iteration of a h    924  * seq_hlist_start - start an iteration of a hlist
961  * @head: the head of the hlist                   925  * @head: the head of the hlist
962  * @pos:  the start position of the sequence      926  * @pos:  the start position of the sequence
963  *                                                927  *
964  * Called at seq_file->op->start().               928  * Called at seq_file->op->start().
965  */                                               929  */
966 struct hlist_node *seq_hlist_start(struct hlis    930 struct hlist_node *seq_hlist_start(struct hlist_head *head, loff_t pos)
967 {                                                 931 {
968         struct hlist_node *node;                  932         struct hlist_node *node;
969                                                   933 
970         hlist_for_each(node, head)                934         hlist_for_each(node, head)
971                 if (pos-- == 0)                   935                 if (pos-- == 0)
972                         return node;              936                         return node;
973         return NULL;                              937         return NULL;
974 }                                                 938 }
975 EXPORT_SYMBOL(seq_hlist_start);                   939 EXPORT_SYMBOL(seq_hlist_start);
976                                                   940 
977 /**                                               941 /**
978  * seq_hlist_start_head - start an iteration o    942  * seq_hlist_start_head - start an iteration of a hlist
979  * @head: the head of the hlist                   943  * @head: the head of the hlist
980  * @pos:  the start position of the sequence      944  * @pos:  the start position of the sequence
981  *                                                945  *
982  * Called at seq_file->op->start(). Call this     946  * Called at seq_file->op->start(). Call this function if you want to
983  * print a header at the top of the output.       947  * print a header at the top of the output.
984  */                                               948  */
985 struct hlist_node *seq_hlist_start_head(struct    949 struct hlist_node *seq_hlist_start_head(struct hlist_head *head, loff_t pos)
986 {                                                 950 {
987         if (!pos)                                 951         if (!pos)
988                 return SEQ_START_TOKEN;           952                 return SEQ_START_TOKEN;
989                                                   953 
990         return seq_hlist_start(head, pos - 1);    954         return seq_hlist_start(head, pos - 1);
991 }                                                 955 }
992 EXPORT_SYMBOL(seq_hlist_start_head);              956 EXPORT_SYMBOL(seq_hlist_start_head);
993                                                   957 
994 /**                                               958 /**
995  * seq_hlist_next - move to the next position     959  * seq_hlist_next - move to the next position of the hlist
996  * @v:    the current iterator                    960  * @v:    the current iterator
997  * @head: the head of the hlist                   961  * @head: the head of the hlist
998  * @ppos: the current position                    962  * @ppos: the current position
999  *                                                963  *
1000  * Called at seq_file->op->next().               964  * Called at seq_file->op->next().
1001  */                                              965  */
1002 struct hlist_node *seq_hlist_next(void *v, st    966 struct hlist_node *seq_hlist_next(void *v, struct hlist_head *head,
1003                                   loff_t *ppo    967                                   loff_t *ppos)
1004 {                                                968 {
1005         struct hlist_node *node = v;             969         struct hlist_node *node = v;
1006                                                  970 
1007         ++*ppos;                                 971         ++*ppos;
1008         if (v == SEQ_START_TOKEN)                972         if (v == SEQ_START_TOKEN)
1009                 return head->first;              973                 return head->first;
1010         else                                     974         else
1011                 return node->next;               975                 return node->next;
1012 }                                                976 }
1013 EXPORT_SYMBOL(seq_hlist_next);                   977 EXPORT_SYMBOL(seq_hlist_next);
1014                                                  978 
1015 /**                                              979 /**
1016  * seq_hlist_start_rcu - start an iteration o    980  * seq_hlist_start_rcu - start an iteration of a hlist protected by RCU
1017  * @head: the head of the hlist                  981  * @head: the head of the hlist
1018  * @pos:  the start position of the sequence     982  * @pos:  the start position of the sequence
1019  *                                               983  *
1020  * Called at seq_file->op->start().              984  * Called at seq_file->op->start().
1021  *                                               985  *
1022  * This list-traversal primitive may safely r    986  * This list-traversal primitive may safely run concurrently with
1023  * the _rcu list-mutation primitives such as     987  * the _rcu list-mutation primitives such as hlist_add_head_rcu()
1024  * as long as the traversal is guarded by rcu    988  * as long as the traversal is guarded by rcu_read_lock().
1025  */                                              989  */
1026 struct hlist_node *seq_hlist_start_rcu(struct    990 struct hlist_node *seq_hlist_start_rcu(struct hlist_head *head,
1027                                        loff_t    991                                        loff_t pos)
1028 {                                                992 {
1029         struct hlist_node *node;                 993         struct hlist_node *node;
1030                                                  994 
1031         __hlist_for_each_rcu(node, head)         995         __hlist_for_each_rcu(node, head)
1032                 if (pos-- == 0)                  996                 if (pos-- == 0)
1033                         return node;             997                         return node;
1034         return NULL;                             998         return NULL;
1035 }                                                999 }
1036 EXPORT_SYMBOL(seq_hlist_start_rcu);              1000 EXPORT_SYMBOL(seq_hlist_start_rcu);
1037                                                  1001 
1038 /**                                              1002 /**
1039  * seq_hlist_start_head_rcu - start an iterat    1003  * seq_hlist_start_head_rcu - start an iteration of a hlist protected by RCU
1040  * @head: the head of the hlist                  1004  * @head: the head of the hlist
1041  * @pos:  the start position of the sequence     1005  * @pos:  the start position of the sequence
1042  *                                               1006  *
1043  * Called at seq_file->op->start(). Call this    1007  * Called at seq_file->op->start(). Call this function if you want to
1044  * print a header at the top of the output.      1008  * print a header at the top of the output.
1045  *                                               1009  *
1046  * This list-traversal primitive may safely r    1010  * This list-traversal primitive may safely run concurrently with
1047  * the _rcu list-mutation primitives such as     1011  * the _rcu list-mutation primitives such as hlist_add_head_rcu()
1048  * as long as the traversal is guarded by rcu    1012  * as long as the traversal is guarded by rcu_read_lock().
1049  */                                              1013  */
1050 struct hlist_node *seq_hlist_start_head_rcu(s    1014 struct hlist_node *seq_hlist_start_head_rcu(struct hlist_head *head,
1051                                             l    1015                                             loff_t pos)
1052 {                                                1016 {
1053         if (!pos)                                1017         if (!pos)
1054                 return SEQ_START_TOKEN;          1018                 return SEQ_START_TOKEN;
1055                                                  1019 
1056         return seq_hlist_start_rcu(head, pos     1020         return seq_hlist_start_rcu(head, pos - 1);
1057 }                                                1021 }
1058 EXPORT_SYMBOL(seq_hlist_start_head_rcu);         1022 EXPORT_SYMBOL(seq_hlist_start_head_rcu);
1059                                                  1023 
1060 /**                                              1024 /**
1061  * seq_hlist_next_rcu - move to the next posi    1025  * seq_hlist_next_rcu - move to the next position of the hlist protected by RCU
1062  * @v:    the current iterator                   1026  * @v:    the current iterator
1063  * @head: the head of the hlist                  1027  * @head: the head of the hlist
1064  * @ppos: the current position                   1028  * @ppos: the current position
1065  *                                               1029  *
1066  * Called at seq_file->op->next().               1030  * Called at seq_file->op->next().
1067  *                                               1031  *
1068  * This list-traversal primitive may safely r    1032  * This list-traversal primitive may safely run concurrently with
1069  * the _rcu list-mutation primitives such as     1033  * the _rcu list-mutation primitives such as hlist_add_head_rcu()
1070  * as long as the traversal is guarded by rcu    1034  * as long as the traversal is guarded by rcu_read_lock().
1071  */                                              1035  */
1072 struct hlist_node *seq_hlist_next_rcu(void *v    1036 struct hlist_node *seq_hlist_next_rcu(void *v,
1073                                       struct     1037                                       struct hlist_head *head,
1074                                       loff_t     1038                                       loff_t *ppos)
1075 {                                                1039 {
1076         struct hlist_node *node = v;             1040         struct hlist_node *node = v;
1077                                                  1041 
1078         ++*ppos;                                 1042         ++*ppos;
1079         if (v == SEQ_START_TOKEN)                1043         if (v == SEQ_START_TOKEN)
1080                 return rcu_dereference(head->    1044                 return rcu_dereference(head->first);
1081         else                                     1045         else
1082                 return rcu_dereference(node->    1046                 return rcu_dereference(node->next);
1083 }                                                1047 }
1084 EXPORT_SYMBOL(seq_hlist_next_rcu);               1048 EXPORT_SYMBOL(seq_hlist_next_rcu);
1085                                                  1049 
1086 /**                                              1050 /**
1087  * seq_hlist_start_percpu - start an iteratio    1051  * seq_hlist_start_percpu - start an iteration of a percpu hlist array
1088  * @head: pointer to percpu array of struct h    1052  * @head: pointer to percpu array of struct hlist_heads
1089  * @cpu:  pointer to cpu "cursor"                1053  * @cpu:  pointer to cpu "cursor"
1090  * @pos:  start position of sequence             1054  * @pos:  start position of sequence
1091  *                                               1055  *
1092  * Called at seq_file->op->start().              1056  * Called at seq_file->op->start().
1093  */                                              1057  */
1094 struct hlist_node *                              1058 struct hlist_node *
1095 seq_hlist_start_percpu(struct hlist_head __pe    1059 seq_hlist_start_percpu(struct hlist_head __percpu *head, int *cpu, loff_t pos)
1096 {                                                1060 {
1097         struct hlist_node *node;                 1061         struct hlist_node *node;
1098                                                  1062 
1099         for_each_possible_cpu(*cpu) {            1063         for_each_possible_cpu(*cpu) {
1100                 hlist_for_each(node, per_cpu_    1064                 hlist_for_each(node, per_cpu_ptr(head, *cpu)) {
1101                         if (pos-- == 0)          1065                         if (pos-- == 0)
1102                                 return node;     1066                                 return node;
1103                 }                                1067                 }
1104         }                                        1068         }
1105         return NULL;                             1069         return NULL;
1106 }                                                1070 }
1107 EXPORT_SYMBOL(seq_hlist_start_percpu);           1071 EXPORT_SYMBOL(seq_hlist_start_percpu);
1108                                                  1072 
1109 /**                                              1073 /**
1110  * seq_hlist_next_percpu - move to the next p    1074  * seq_hlist_next_percpu - move to the next position of the percpu hlist array
1111  * @v:    pointer to current hlist_node          1075  * @v:    pointer to current hlist_node
1112  * @head: pointer to percpu array of struct h    1076  * @head: pointer to percpu array of struct hlist_heads
1113  * @cpu:  pointer to cpu "cursor"                1077  * @cpu:  pointer to cpu "cursor"
1114  * @pos:  start position of sequence             1078  * @pos:  start position of sequence
1115  *                                               1079  *
1116  * Called at seq_file->op->next().               1080  * Called at seq_file->op->next().
1117  */                                              1081  */
1118 struct hlist_node *                              1082 struct hlist_node *
1119 seq_hlist_next_percpu(void *v, struct hlist_h    1083 seq_hlist_next_percpu(void *v, struct hlist_head __percpu *head,
1120                         int *cpu, loff_t *pos    1084                         int *cpu, loff_t *pos)
1121 {                                                1085 {
1122         struct hlist_node *node = v;             1086         struct hlist_node *node = v;
1123                                                  1087 
1124         ++*pos;                                  1088         ++*pos;
1125                                                  1089 
1126         if (node->next)                          1090         if (node->next)
1127                 return node->next;               1091                 return node->next;
1128                                                  1092 
1129         for (*cpu = cpumask_next(*cpu, cpu_po    1093         for (*cpu = cpumask_next(*cpu, cpu_possible_mask); *cpu < nr_cpu_ids;
1130              *cpu = cpumask_next(*cpu, cpu_po    1094              *cpu = cpumask_next(*cpu, cpu_possible_mask)) {
1131                 struct hlist_head *bucket = p    1095                 struct hlist_head *bucket = per_cpu_ptr(head, *cpu);
1132                                                  1096 
1133                 if (!hlist_empty(bucket))        1097                 if (!hlist_empty(bucket))
1134                         return bucket->first;    1098                         return bucket->first;
1135         }                                        1099         }
1136         return NULL;                             1100         return NULL;
1137 }                                                1101 }
1138 EXPORT_SYMBOL(seq_hlist_next_percpu);            1102 EXPORT_SYMBOL(seq_hlist_next_percpu);
1139                                                  1103 
1140 void __init seq_file_init(void)                  1104 void __init seq_file_init(void)
1141 {                                                1105 {
1142         seq_file_cache = KMEM_CACHE(seq_file,    1106         seq_file_cache = KMEM_CACHE(seq_file, SLAB_ACCOUNT|SLAB_PANIC);
1143 }                                                1107 }
1144                                                  1108 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php