Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/seq_file.c |
| 3 | * |
| 4 | * helper functions for making synthetic files from sequences of records. |
| 5 | * initial implementation -- AV, Oct 2001. |
| 6 | */ |
| 7 | |
| 8 | #include <linux/fs.h> |
| 9 | #include <linux/module.h> |
| 10 | #include <linux/seq_file.h> |
| 11 | #include <linux/slab.h> |
| 12 | |
| 13 | #include <asm/uaccess.h> |
| 14 | #include <asm/page.h> |
| 15 | |
| 16 | /** |
| 17 | * seq_open - initialize sequential file |
| 18 | * @file: file we initialize |
| 19 | * @op: method table describing the sequence |
| 20 | * |
| 21 | * seq_open() sets @file, associating it with a sequence described |
| 22 | * by @op. @op->start() sets the iterator up and returns the first |
| 23 | * element of sequence. @op->stop() shuts it down. @op->next() |
| 24 | * returns the next element of sequence. @op->show() prints element |
| 25 | * into the buffer. In case of error ->start() and ->next() return |
| 26 | * ERR_PTR(error). In the end of sequence they return %NULL. ->show() |
| 27 | * returns 0 in case of success and negative number in case of error. |
Al Viro | 521b5d0 | 2008-03-28 00:46:41 -0400 | [diff] [blame] | 28 | * Returning SEQ_SKIP means "discard this element and move on". |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | */ |
Helge Deller | 15ad7cd | 2006-12-06 20:40:36 -0800 | [diff] [blame] | 30 | int seq_open(struct file *file, const struct seq_operations *op) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | { |
Al Viro | 1abe77b | 2005-11-07 17:15:34 -0500 | [diff] [blame] | 32 | struct seq_file *p = file->private_data; |
| 33 | |
| 34 | if (!p) { |
| 35 | p = kmalloc(sizeof(*p), GFP_KERNEL); |
| 36 | if (!p) |
| 37 | return -ENOMEM; |
| 38 | file->private_data = p; |
| 39 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | memset(p, 0, sizeof(*p)); |
Ingo Molnar | 0ac1759 | 2006-03-23 03:00:37 -0800 | [diff] [blame] | 41 | mutex_init(&p->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | p->op = op; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
| 44 | /* |
| 45 | * Wrappers around seq_open(e.g. swaps_open) need to be |
| 46 | * aware of this. If they set f_version themselves, they |
| 47 | * should call seq_open first and then set f_version. |
| 48 | */ |
| 49 | file->f_version = 0; |
| 50 | |
| 51 | /* SEQ files support lseek, but not pread/pwrite */ |
| 52 | file->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE); |
| 53 | return 0; |
| 54 | } |
| 55 | EXPORT_SYMBOL(seq_open); |
| 56 | |
| 57 | /** |
| 58 | * seq_read - ->read() method for sequential files. |
Martin Waitz | 67be2dd | 2005-05-01 08:59:26 -0700 | [diff] [blame] | 59 | * @file: the file to read from |
| 60 | * @buf: the buffer to read to |
| 61 | * @size: the maximum number of bytes to read |
| 62 | * @ppos: the current position in the file |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | * |
| 64 | * Ready-made ->f_op->read() |
| 65 | */ |
| 66 | ssize_t seq_read(struct file *file, char __user *buf, size_t size, loff_t *ppos) |
| 67 | { |
| 68 | struct seq_file *m = (struct seq_file *)file->private_data; |
| 69 | size_t copied = 0; |
| 70 | loff_t pos; |
| 71 | size_t n; |
| 72 | void *p; |
| 73 | int err = 0; |
| 74 | |
Ingo Molnar | 0ac1759 | 2006-03-23 03:00:37 -0800 | [diff] [blame] | 75 | mutex_lock(&m->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | /* |
| 77 | * seq_file->op->..m_start/m_stop/m_next may do special actions |
| 78 | * or optimisations based on the file->f_version, so we want to |
| 79 | * pass the file->f_version to those methods. |
| 80 | * |
| 81 | * seq_file->version is just copy of f_version, and seq_file |
| 82 | * methods can treat it simply as file version. |
| 83 | * It is copied in first and copied out after all operations. |
| 84 | * It is convenient to have it as part of structure to avoid the |
| 85 | * need of passing another argument to all the seq_file methods. |
| 86 | */ |
| 87 | m->version = file->f_version; |
| 88 | /* grab buffer if we didn't have one */ |
| 89 | if (!m->buf) { |
| 90 | m->buf = kmalloc(m->size = PAGE_SIZE, GFP_KERNEL); |
| 91 | if (!m->buf) |
| 92 | goto Enomem; |
| 93 | } |
| 94 | /* if not empty - flush it first */ |
| 95 | if (m->count) { |
| 96 | n = min(m->count, size); |
| 97 | err = copy_to_user(buf, m->buf + m->from, n); |
| 98 | if (err) |
| 99 | goto Efault; |
| 100 | m->count -= n; |
| 101 | m->from += n; |
| 102 | size -= n; |
| 103 | buf += n; |
| 104 | copied += n; |
| 105 | if (!m->count) |
| 106 | m->index++; |
| 107 | if (!size) |
| 108 | goto Done; |
| 109 | } |
| 110 | /* we need at least one record in buffer */ |
| 111 | while (1) { |
| 112 | pos = m->index; |
| 113 | p = m->op->start(m, &pos); |
| 114 | err = PTR_ERR(p); |
| 115 | if (!p || IS_ERR(p)) |
| 116 | break; |
| 117 | err = m->op->show(m, p); |
Al Viro | 521b5d0 | 2008-03-28 00:46:41 -0400 | [diff] [blame] | 118 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | break; |
Al Viro | 521b5d0 | 2008-03-28 00:46:41 -0400 | [diff] [blame] | 120 | if (unlikely(err)) |
| 121 | m->count = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | if (m->count < m->size) |
| 123 | goto Fill; |
| 124 | m->op->stop(m, p); |
| 125 | kfree(m->buf); |
| 126 | m->buf = kmalloc(m->size <<= 1, GFP_KERNEL); |
| 127 | if (!m->buf) |
| 128 | goto Enomem; |
| 129 | m->count = 0; |
| 130 | m->version = 0; |
| 131 | } |
| 132 | m->op->stop(m, p); |
| 133 | m->count = 0; |
| 134 | goto Done; |
| 135 | Fill: |
| 136 | /* they want more? let's try to get some more */ |
| 137 | while (m->count < size) { |
| 138 | size_t offs = m->count; |
| 139 | loff_t next = pos; |
| 140 | p = m->op->next(m, p, &next); |
| 141 | if (!p || IS_ERR(p)) { |
| 142 | err = PTR_ERR(p); |
| 143 | break; |
| 144 | } |
| 145 | err = m->op->show(m, p); |
Al Viro | 521b5d0 | 2008-03-28 00:46:41 -0400 | [diff] [blame] | 146 | if (m->count == m->size || err) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | m->count = offs; |
Al Viro | 521b5d0 | 2008-03-28 00:46:41 -0400 | [diff] [blame] | 148 | if (likely(err <= 0)) |
| 149 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | } |
| 151 | pos = next; |
| 152 | } |
| 153 | m->op->stop(m, p); |
| 154 | n = min(m->count, size); |
| 155 | err = copy_to_user(buf, m->buf, n); |
| 156 | if (err) |
| 157 | goto Efault; |
| 158 | copied += n; |
| 159 | m->count -= n; |
| 160 | if (m->count) |
| 161 | m->from = n; |
| 162 | else |
| 163 | pos++; |
| 164 | m->index = pos; |
| 165 | Done: |
| 166 | if (!copied) |
| 167 | copied = err; |
| 168 | else |
| 169 | *ppos += copied; |
| 170 | file->f_version = m->version; |
Ingo Molnar | 0ac1759 | 2006-03-23 03:00:37 -0800 | [diff] [blame] | 171 | mutex_unlock(&m->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | return copied; |
| 173 | Enomem: |
| 174 | err = -ENOMEM; |
| 175 | goto Done; |
| 176 | Efault: |
| 177 | err = -EFAULT; |
| 178 | goto Done; |
| 179 | } |
| 180 | EXPORT_SYMBOL(seq_read); |
| 181 | |
| 182 | static int traverse(struct seq_file *m, loff_t offset) |
| 183 | { |
Alexey Dobriyan | cb510b8 | 2007-07-15 23:40:39 -0700 | [diff] [blame] | 184 | loff_t pos = 0, index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | int error = 0; |
| 186 | void *p; |
| 187 | |
| 188 | m->version = 0; |
Alexey Dobriyan | cb510b8 | 2007-07-15 23:40:39 -0700 | [diff] [blame] | 189 | index = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | m->count = m->from = 0; |
Alexey Dobriyan | cb510b8 | 2007-07-15 23:40:39 -0700 | [diff] [blame] | 191 | if (!offset) { |
| 192 | m->index = index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | return 0; |
Alexey Dobriyan | cb510b8 | 2007-07-15 23:40:39 -0700 | [diff] [blame] | 194 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | if (!m->buf) { |
| 196 | m->buf = kmalloc(m->size = PAGE_SIZE, GFP_KERNEL); |
| 197 | if (!m->buf) |
| 198 | return -ENOMEM; |
| 199 | } |
Alexey Dobriyan | cb510b8 | 2007-07-15 23:40:39 -0700 | [diff] [blame] | 200 | p = m->op->start(m, &index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | while (p) { |
| 202 | error = PTR_ERR(p); |
| 203 | if (IS_ERR(p)) |
| 204 | break; |
| 205 | error = m->op->show(m, p); |
Al Viro | 521b5d0 | 2008-03-28 00:46:41 -0400 | [diff] [blame] | 206 | if (error < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | break; |
Al Viro | 521b5d0 | 2008-03-28 00:46:41 -0400 | [diff] [blame] | 208 | if (unlikely(error)) { |
| 209 | error = 0; |
| 210 | m->count = 0; |
| 211 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | if (m->count == m->size) |
| 213 | goto Eoverflow; |
| 214 | if (pos + m->count > offset) { |
| 215 | m->from = offset - pos; |
| 216 | m->count -= m->from; |
Alexey Dobriyan | cb510b8 | 2007-07-15 23:40:39 -0700 | [diff] [blame] | 217 | m->index = index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | break; |
| 219 | } |
| 220 | pos += m->count; |
| 221 | m->count = 0; |
| 222 | if (pos == offset) { |
Alexey Dobriyan | cb510b8 | 2007-07-15 23:40:39 -0700 | [diff] [blame] | 223 | index++; |
| 224 | m->index = index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | break; |
| 226 | } |
Alexey Dobriyan | cb510b8 | 2007-07-15 23:40:39 -0700 | [diff] [blame] | 227 | p = m->op->next(m, p, &index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | } |
| 229 | m->op->stop(m, p); |
| 230 | return error; |
| 231 | |
| 232 | Eoverflow: |
| 233 | m->op->stop(m, p); |
| 234 | kfree(m->buf); |
| 235 | m->buf = kmalloc(m->size <<= 1, GFP_KERNEL); |
| 236 | return !m->buf ? -ENOMEM : -EAGAIN; |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * seq_lseek - ->llseek() method for sequential files. |
Martin Waitz | 67be2dd | 2005-05-01 08:59:26 -0700 | [diff] [blame] | 241 | * @file: the file in question |
| 242 | * @offset: new position |
| 243 | * @origin: 0 for absolute, 1 for relative position |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | * |
| 245 | * Ready-made ->f_op->llseek() |
| 246 | */ |
| 247 | loff_t seq_lseek(struct file *file, loff_t offset, int origin) |
| 248 | { |
| 249 | struct seq_file *m = (struct seq_file *)file->private_data; |
David Sterba | 16abef0 | 2008-04-22 15:09:22 +0200 | [diff] [blame] | 250 | loff_t retval = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | |
Ingo Molnar | 0ac1759 | 2006-03-23 03:00:37 -0800 | [diff] [blame] | 252 | mutex_lock(&m->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | m->version = file->f_version; |
| 254 | switch (origin) { |
| 255 | case 1: |
| 256 | offset += file->f_pos; |
| 257 | case 0: |
| 258 | if (offset < 0) |
| 259 | break; |
| 260 | retval = offset; |
| 261 | if (offset != file->f_pos) { |
| 262 | while ((retval=traverse(m, offset)) == -EAGAIN) |
| 263 | ; |
| 264 | if (retval) { |
| 265 | /* with extreme prejudice... */ |
| 266 | file->f_pos = 0; |
| 267 | m->version = 0; |
| 268 | m->index = 0; |
| 269 | m->count = 0; |
| 270 | } else { |
| 271 | retval = file->f_pos = offset; |
| 272 | } |
| 273 | } |
| 274 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | file->f_version = m->version; |
Alexey Dobriyan | 00c5746 | 2007-07-15 23:40:22 -0700 | [diff] [blame] | 276 | mutex_unlock(&m->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | return retval; |
| 278 | } |
| 279 | EXPORT_SYMBOL(seq_lseek); |
| 280 | |
| 281 | /** |
| 282 | * seq_release - free the structures associated with sequential file. |
| 283 | * @file: file in question |
Josef "Jeff" Sipek | 0f7fc9e | 2006-12-08 02:36:35 -0800 | [diff] [blame] | 284 | * @inode: file->f_path.dentry->d_inode |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | * |
| 286 | * Frees the structures associated with sequential file; can be used |
| 287 | * as ->f_op->release() if you don't have private data to destroy. |
| 288 | */ |
| 289 | int seq_release(struct inode *inode, struct file *file) |
| 290 | { |
| 291 | struct seq_file *m = (struct seq_file *)file->private_data; |
| 292 | kfree(m->buf); |
| 293 | kfree(m); |
| 294 | return 0; |
| 295 | } |
| 296 | EXPORT_SYMBOL(seq_release); |
| 297 | |
| 298 | /** |
| 299 | * seq_escape - print string into buffer, escaping some characters |
| 300 | * @m: target buffer |
| 301 | * @s: string |
| 302 | * @esc: set of characters that need escaping |
| 303 | * |
| 304 | * Puts string into buffer, replacing each occurrence of character from |
| 305 | * @esc with usual octal escape. Returns 0 in case of success, -1 - in |
| 306 | * case of overflow. |
| 307 | */ |
| 308 | int seq_escape(struct seq_file *m, const char *s, const char *esc) |
| 309 | { |
| 310 | char *end = m->buf + m->size; |
| 311 | char *p; |
| 312 | char c; |
| 313 | |
| 314 | for (p = m->buf + m->count; (c = *s) != '\0' && p < end; s++) { |
| 315 | if (!strchr(esc, c)) { |
| 316 | *p++ = c; |
| 317 | continue; |
| 318 | } |
| 319 | if (p + 3 < end) { |
| 320 | *p++ = '\\'; |
| 321 | *p++ = '0' + ((c & 0300) >> 6); |
| 322 | *p++ = '0' + ((c & 070) >> 3); |
| 323 | *p++ = '0' + (c & 07); |
| 324 | continue; |
| 325 | } |
| 326 | m->count = m->size; |
| 327 | return -1; |
| 328 | } |
| 329 | m->count = p - m->buf; |
| 330 | return 0; |
| 331 | } |
| 332 | EXPORT_SYMBOL(seq_escape); |
| 333 | |
| 334 | int seq_printf(struct seq_file *m, const char *f, ...) |
| 335 | { |
| 336 | va_list args; |
| 337 | int len; |
| 338 | |
| 339 | if (m->count < m->size) { |
| 340 | va_start(args, f); |
| 341 | len = vsnprintf(m->buf + m->count, m->size - m->count, f, args); |
| 342 | va_end(args); |
| 343 | if (m->count + len < m->size) { |
| 344 | m->count += len; |
| 345 | return 0; |
| 346 | } |
| 347 | } |
| 348 | m->count = m->size; |
| 349 | return -1; |
| 350 | } |
| 351 | EXPORT_SYMBOL(seq_printf); |
| 352 | |
Ram Pai | 6092d04 | 2008-03-27 13:06:20 +0100 | [diff] [blame] | 353 | static char *mangle_path(char *s, char *p, char *esc) |
| 354 | { |
| 355 | while (s <= p) { |
| 356 | char c = *p++; |
| 357 | if (!c) { |
| 358 | return s; |
| 359 | } else if (!strchr(esc, c)) { |
| 360 | *s++ = c; |
| 361 | } else if (s + 4 > p) { |
| 362 | break; |
| 363 | } else { |
| 364 | *s++ = '\\'; |
| 365 | *s++ = '0' + ((c & 0300) >> 6); |
| 366 | *s++ = '0' + ((c & 070) >> 3); |
| 367 | *s++ = '0' + (c & 07); |
| 368 | } |
| 369 | } |
| 370 | return NULL; |
| 371 | } |
| 372 | |
| 373 | /* |
| 374 | * return the absolute path of 'dentry' residing in mount 'mnt'. |
| 375 | */ |
Jan Blunck | c32c2f6 | 2008-02-14 19:38:43 -0800 | [diff] [blame] | 376 | int seq_path(struct seq_file *m, struct path *path, char *esc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | { |
| 378 | if (m->count < m->size) { |
| 379 | char *s = m->buf + m->count; |
Jan Blunck | cf28b48 | 2008-02-14 19:38:44 -0800 | [diff] [blame] | 380 | char *p = d_path(path, s, m->size - m->count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | if (!IS_ERR(p)) { |
Ram Pai | 6092d04 | 2008-03-27 13:06:20 +0100 | [diff] [blame] | 382 | s = mangle_path(s, p, esc); |
| 383 | if (s) { |
| 384 | p = m->buf + m->count; |
| 385 | m->count = s - m->buf; |
| 386 | return s - p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | } |
| 388 | } |
| 389 | } |
| 390 | m->count = m->size; |
| 391 | return -1; |
| 392 | } |
| 393 | EXPORT_SYMBOL(seq_path); |
| 394 | |
Ram Pai | 6092d04 | 2008-03-27 13:06:20 +0100 | [diff] [blame] | 395 | /* |
Miklos Szeredi | 9d1bc601 | 2008-03-27 13:06:21 +0100 | [diff] [blame] | 396 | * Same as seq_path, but relative to supplied root. |
| 397 | * |
| 398 | * root may be changed, see __d_path(). |
| 399 | */ |
| 400 | int seq_path_root(struct seq_file *m, struct path *path, struct path *root, |
| 401 | char *esc) |
| 402 | { |
| 403 | int err = -ENAMETOOLONG; |
| 404 | if (m->count < m->size) { |
| 405 | char *s = m->buf + m->count; |
| 406 | char *p; |
| 407 | |
| 408 | spin_lock(&dcache_lock); |
| 409 | p = __d_path(path, root, s, m->size - m->count); |
| 410 | spin_unlock(&dcache_lock); |
| 411 | err = PTR_ERR(p); |
| 412 | if (!IS_ERR(p)) { |
| 413 | s = mangle_path(s, p, esc); |
| 414 | if (s) { |
| 415 | p = m->buf + m->count; |
| 416 | m->count = s - m->buf; |
| 417 | return 0; |
| 418 | } |
| 419 | } |
| 420 | } |
| 421 | m->count = m->size; |
| 422 | return err; |
| 423 | } |
| 424 | |
| 425 | /* |
Ram Pai | 6092d04 | 2008-03-27 13:06:20 +0100 | [diff] [blame] | 426 | * returns the path of the 'dentry' from the root of its filesystem. |
| 427 | */ |
| 428 | int seq_dentry(struct seq_file *m, struct dentry *dentry, char *esc) |
| 429 | { |
| 430 | if (m->count < m->size) { |
| 431 | char *s = m->buf + m->count; |
| 432 | char *p = dentry_path(dentry, s, m->size - m->count); |
| 433 | if (!IS_ERR(p)) { |
| 434 | s = mangle_path(s, p, esc); |
| 435 | if (s) { |
| 436 | p = m->buf + m->count; |
| 437 | m->count = s - m->buf; |
| 438 | return s - p; |
| 439 | } |
| 440 | } |
| 441 | } |
| 442 | m->count = m->size; |
| 443 | return -1; |
| 444 | } |
| 445 | |
Alexey Dobriyan | 50ac2d6 | 2008-08-12 15:09:02 -0700 | [diff] [blame^] | 446 | int seq_bitmap(struct seq_file *m, unsigned long *bits, unsigned int nr_bits) |
| 447 | { |
| 448 | size_t len = bitmap_scnprintf_len(nr_bits); |
| 449 | |
| 450 | if (m->count + len < m->size) { |
| 451 | bitmap_scnprintf(m->buf + m->count, m->size - m->count, |
| 452 | bits, nr_bits); |
| 453 | m->count += len; |
| 454 | return 0; |
| 455 | } |
| 456 | m->count = m->size; |
| 457 | return -1; |
| 458 | } |
| 459 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | static void *single_start(struct seq_file *p, loff_t *pos) |
| 461 | { |
| 462 | return NULL + (*pos == 0); |
| 463 | } |
| 464 | |
| 465 | static void *single_next(struct seq_file *p, void *v, loff_t *pos) |
| 466 | { |
| 467 | ++*pos; |
| 468 | return NULL; |
| 469 | } |
| 470 | |
| 471 | static void single_stop(struct seq_file *p, void *v) |
| 472 | { |
| 473 | } |
| 474 | |
| 475 | int single_open(struct file *file, int (*show)(struct seq_file *, void *), |
| 476 | void *data) |
| 477 | { |
| 478 | struct seq_operations *op = kmalloc(sizeof(*op), GFP_KERNEL); |
| 479 | int res = -ENOMEM; |
| 480 | |
| 481 | if (op) { |
| 482 | op->start = single_start; |
| 483 | op->next = single_next; |
| 484 | op->stop = single_stop; |
| 485 | op->show = show; |
| 486 | res = seq_open(file, op); |
| 487 | if (!res) |
| 488 | ((struct seq_file *)file->private_data)->private = data; |
| 489 | else |
| 490 | kfree(op); |
| 491 | } |
| 492 | return res; |
| 493 | } |
| 494 | EXPORT_SYMBOL(single_open); |
| 495 | |
| 496 | int single_release(struct inode *inode, struct file *file) |
| 497 | { |
Helge Deller | 15ad7cd | 2006-12-06 20:40:36 -0800 | [diff] [blame] | 498 | const struct seq_operations *op = ((struct seq_file *)file->private_data)->op; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | int res = seq_release(inode, file); |
| 500 | kfree(op); |
| 501 | return res; |
| 502 | } |
| 503 | EXPORT_SYMBOL(single_release); |
| 504 | |
| 505 | int seq_release_private(struct inode *inode, struct file *file) |
| 506 | { |
| 507 | struct seq_file *seq = file->private_data; |
| 508 | |
| 509 | kfree(seq->private); |
| 510 | seq->private = NULL; |
| 511 | return seq_release(inode, file); |
| 512 | } |
| 513 | EXPORT_SYMBOL(seq_release_private); |
| 514 | |
Pavel Emelyanov | 3969903 | 2007-10-10 02:28:42 -0700 | [diff] [blame] | 515 | void *__seq_open_private(struct file *f, const struct seq_operations *ops, |
| 516 | int psize) |
| 517 | { |
| 518 | int rc; |
| 519 | void *private; |
| 520 | struct seq_file *seq; |
| 521 | |
| 522 | private = kzalloc(psize, GFP_KERNEL); |
| 523 | if (private == NULL) |
| 524 | goto out; |
| 525 | |
| 526 | rc = seq_open(f, ops); |
| 527 | if (rc < 0) |
| 528 | goto out_free; |
| 529 | |
| 530 | seq = f->private_data; |
| 531 | seq->private = private; |
| 532 | return private; |
| 533 | |
| 534 | out_free: |
| 535 | kfree(private); |
| 536 | out: |
| 537 | return NULL; |
| 538 | } |
| 539 | EXPORT_SYMBOL(__seq_open_private); |
| 540 | |
| 541 | int seq_open_private(struct file *filp, const struct seq_operations *ops, |
| 542 | int psize) |
| 543 | { |
| 544 | return __seq_open_private(filp, ops, psize) ? 0 : -ENOMEM; |
| 545 | } |
| 546 | EXPORT_SYMBOL(seq_open_private); |
| 547 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | int seq_putc(struct seq_file *m, char c) |
| 549 | { |
| 550 | if (m->count < m->size) { |
| 551 | m->buf[m->count++] = c; |
| 552 | return 0; |
| 553 | } |
| 554 | return -1; |
| 555 | } |
| 556 | EXPORT_SYMBOL(seq_putc); |
| 557 | |
| 558 | int seq_puts(struct seq_file *m, const char *s) |
| 559 | { |
| 560 | int len = strlen(s); |
| 561 | if (m->count + len < m->size) { |
| 562 | memcpy(m->buf + m->count, s, len); |
| 563 | m->count += len; |
| 564 | return 0; |
| 565 | } |
| 566 | m->count = m->size; |
| 567 | return -1; |
| 568 | } |
| 569 | EXPORT_SYMBOL(seq_puts); |
Pavel Emelianov | bcf67e1 | 2007-07-10 17:22:26 -0700 | [diff] [blame] | 570 | |
| 571 | struct list_head *seq_list_start(struct list_head *head, loff_t pos) |
| 572 | { |
| 573 | struct list_head *lh; |
| 574 | |
| 575 | list_for_each(lh, head) |
| 576 | if (pos-- == 0) |
| 577 | return lh; |
| 578 | |
| 579 | return NULL; |
| 580 | } |
| 581 | |
| 582 | EXPORT_SYMBOL(seq_list_start); |
| 583 | |
| 584 | struct list_head *seq_list_start_head(struct list_head *head, loff_t pos) |
| 585 | { |
| 586 | if (!pos) |
| 587 | return head; |
| 588 | |
| 589 | return seq_list_start(head, pos - 1); |
| 590 | } |
| 591 | |
| 592 | EXPORT_SYMBOL(seq_list_start_head); |
| 593 | |
| 594 | struct list_head *seq_list_next(void *v, struct list_head *head, loff_t *ppos) |
| 595 | { |
| 596 | struct list_head *lh; |
| 597 | |
| 598 | lh = ((struct list_head *)v)->next; |
| 599 | ++*ppos; |
| 600 | return lh == head ? NULL : lh; |
| 601 | } |
| 602 | |
| 603 | EXPORT_SYMBOL(seq_list_next); |