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 */ |
Al Viro | 4cdfe84 | 2008-08-24 07:45:33 -0400 | [diff] [blame] | 111 | pos = m->index; |
| 112 | p = m->op->start(m, &pos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | while (1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Al Viro | 4cdfe84 | 2008-08-24 07:45:33 -0400 | [diff] [blame] | 122 | if (unlikely(!m->count)) { |
| 123 | p = m->op->next(m, p, &pos); |
| 124 | m->index = pos; |
| 125 | continue; |
| 126 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | if (m->count < m->size) |
| 128 | goto Fill; |
| 129 | m->op->stop(m, p); |
| 130 | kfree(m->buf); |
| 131 | m->buf = kmalloc(m->size <<= 1, GFP_KERNEL); |
| 132 | if (!m->buf) |
| 133 | goto Enomem; |
| 134 | m->count = 0; |
| 135 | m->version = 0; |
Al Viro | 4cdfe84 | 2008-08-24 07:45:33 -0400 | [diff] [blame] | 136 | pos = m->index; |
| 137 | p = m->op->start(m, &pos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | } |
| 139 | m->op->stop(m, p); |
| 140 | m->count = 0; |
| 141 | goto Done; |
| 142 | Fill: |
| 143 | /* they want more? let's try to get some more */ |
| 144 | while (m->count < size) { |
| 145 | size_t offs = m->count; |
| 146 | loff_t next = pos; |
| 147 | p = m->op->next(m, p, &next); |
| 148 | if (!p || IS_ERR(p)) { |
| 149 | err = PTR_ERR(p); |
| 150 | break; |
| 151 | } |
| 152 | err = m->op->show(m, p); |
Al Viro | 521b5d0 | 2008-03-28 00:46:41 -0400 | [diff] [blame] | 153 | if (m->count == m->size || err) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | m->count = offs; |
Al Viro | 521b5d0 | 2008-03-28 00:46:41 -0400 | [diff] [blame] | 155 | if (likely(err <= 0)) |
| 156 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | } |
| 158 | pos = next; |
| 159 | } |
| 160 | m->op->stop(m, p); |
| 161 | n = min(m->count, size); |
| 162 | err = copy_to_user(buf, m->buf, n); |
| 163 | if (err) |
| 164 | goto Efault; |
| 165 | copied += n; |
| 166 | m->count -= n; |
| 167 | if (m->count) |
| 168 | m->from = n; |
| 169 | else |
| 170 | pos++; |
| 171 | m->index = pos; |
| 172 | Done: |
| 173 | if (!copied) |
| 174 | copied = err; |
| 175 | else |
| 176 | *ppos += copied; |
| 177 | file->f_version = m->version; |
Ingo Molnar | 0ac1759 | 2006-03-23 03:00:37 -0800 | [diff] [blame] | 178 | mutex_unlock(&m->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | return copied; |
| 180 | Enomem: |
| 181 | err = -ENOMEM; |
| 182 | goto Done; |
| 183 | Efault: |
| 184 | err = -EFAULT; |
| 185 | goto Done; |
| 186 | } |
| 187 | EXPORT_SYMBOL(seq_read); |
| 188 | |
| 189 | static int traverse(struct seq_file *m, loff_t offset) |
| 190 | { |
Alexey Dobriyan | cb510b8 | 2007-07-15 23:40:39 -0700 | [diff] [blame] | 191 | loff_t pos = 0, index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | int error = 0; |
| 193 | void *p; |
| 194 | |
| 195 | m->version = 0; |
Alexey Dobriyan | cb510b8 | 2007-07-15 23:40:39 -0700 | [diff] [blame] | 196 | index = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | m->count = m->from = 0; |
Alexey Dobriyan | cb510b8 | 2007-07-15 23:40:39 -0700 | [diff] [blame] | 198 | if (!offset) { |
| 199 | m->index = index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | return 0; |
Alexey Dobriyan | cb510b8 | 2007-07-15 23:40:39 -0700 | [diff] [blame] | 201 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | if (!m->buf) { |
| 203 | m->buf = kmalloc(m->size = PAGE_SIZE, GFP_KERNEL); |
| 204 | if (!m->buf) |
| 205 | return -ENOMEM; |
| 206 | } |
Alexey Dobriyan | cb510b8 | 2007-07-15 23:40:39 -0700 | [diff] [blame] | 207 | p = m->op->start(m, &index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | while (p) { |
| 209 | error = PTR_ERR(p); |
| 210 | if (IS_ERR(p)) |
| 211 | break; |
| 212 | error = m->op->show(m, p); |
Al Viro | 521b5d0 | 2008-03-28 00:46:41 -0400 | [diff] [blame] | 213 | if (error < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | break; |
Al Viro | 521b5d0 | 2008-03-28 00:46:41 -0400 | [diff] [blame] | 215 | if (unlikely(error)) { |
| 216 | error = 0; |
| 217 | m->count = 0; |
| 218 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | if (m->count == m->size) |
| 220 | goto Eoverflow; |
| 221 | if (pos + m->count > offset) { |
| 222 | m->from = offset - pos; |
| 223 | m->count -= m->from; |
Alexey Dobriyan | cb510b8 | 2007-07-15 23:40:39 -0700 | [diff] [blame] | 224 | m->index = index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | break; |
| 226 | } |
| 227 | pos += m->count; |
| 228 | m->count = 0; |
| 229 | if (pos == offset) { |
Alexey Dobriyan | cb510b8 | 2007-07-15 23:40:39 -0700 | [diff] [blame] | 230 | index++; |
| 231 | m->index = index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | break; |
| 233 | } |
Alexey Dobriyan | cb510b8 | 2007-07-15 23:40:39 -0700 | [diff] [blame] | 234 | p = m->op->next(m, p, &index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | } |
| 236 | m->op->stop(m, p); |
| 237 | return error; |
| 238 | |
| 239 | Eoverflow: |
| 240 | m->op->stop(m, p); |
| 241 | kfree(m->buf); |
| 242 | m->buf = kmalloc(m->size <<= 1, GFP_KERNEL); |
| 243 | return !m->buf ? -ENOMEM : -EAGAIN; |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * seq_lseek - ->llseek() method for sequential files. |
Martin Waitz | 67be2dd | 2005-05-01 08:59:26 -0700 | [diff] [blame] | 248 | * @file: the file in question |
| 249 | * @offset: new position |
| 250 | * @origin: 0 for absolute, 1 for relative position |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | * |
| 252 | * Ready-made ->f_op->llseek() |
| 253 | */ |
| 254 | loff_t seq_lseek(struct file *file, loff_t offset, int origin) |
| 255 | { |
| 256 | struct seq_file *m = (struct seq_file *)file->private_data; |
David Sterba | 16abef0 | 2008-04-22 15:09:22 +0200 | [diff] [blame] | 257 | loff_t retval = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | |
Ingo Molnar | 0ac1759 | 2006-03-23 03:00:37 -0800 | [diff] [blame] | 259 | mutex_lock(&m->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | m->version = file->f_version; |
| 261 | switch (origin) { |
| 262 | case 1: |
| 263 | offset += file->f_pos; |
| 264 | case 0: |
| 265 | if (offset < 0) |
| 266 | break; |
| 267 | retval = offset; |
| 268 | if (offset != file->f_pos) { |
| 269 | while ((retval=traverse(m, offset)) == -EAGAIN) |
| 270 | ; |
| 271 | if (retval) { |
| 272 | /* with extreme prejudice... */ |
| 273 | file->f_pos = 0; |
| 274 | m->version = 0; |
| 275 | m->index = 0; |
| 276 | m->count = 0; |
| 277 | } else { |
| 278 | retval = file->f_pos = offset; |
| 279 | } |
| 280 | } |
| 281 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | file->f_version = m->version; |
Alexey Dobriyan | 00c5746 | 2007-07-15 23:40:22 -0700 | [diff] [blame] | 283 | mutex_unlock(&m->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | return retval; |
| 285 | } |
| 286 | EXPORT_SYMBOL(seq_lseek); |
| 287 | |
| 288 | /** |
| 289 | * seq_release - free the structures associated with sequential file. |
| 290 | * @file: file in question |
Josef "Jeff" Sipek | 0f7fc9e | 2006-12-08 02:36:35 -0800 | [diff] [blame] | 291 | * @inode: file->f_path.dentry->d_inode |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | * |
| 293 | * Frees the structures associated with sequential file; can be used |
| 294 | * as ->f_op->release() if you don't have private data to destroy. |
| 295 | */ |
| 296 | int seq_release(struct inode *inode, struct file *file) |
| 297 | { |
| 298 | struct seq_file *m = (struct seq_file *)file->private_data; |
| 299 | kfree(m->buf); |
| 300 | kfree(m); |
| 301 | return 0; |
| 302 | } |
| 303 | EXPORT_SYMBOL(seq_release); |
| 304 | |
| 305 | /** |
| 306 | * seq_escape - print string into buffer, escaping some characters |
| 307 | * @m: target buffer |
| 308 | * @s: string |
| 309 | * @esc: set of characters that need escaping |
| 310 | * |
| 311 | * Puts string into buffer, replacing each occurrence of character from |
| 312 | * @esc with usual octal escape. Returns 0 in case of success, -1 - in |
| 313 | * case of overflow. |
| 314 | */ |
| 315 | int seq_escape(struct seq_file *m, const char *s, const char *esc) |
| 316 | { |
| 317 | char *end = m->buf + m->size; |
| 318 | char *p; |
| 319 | char c; |
| 320 | |
| 321 | for (p = m->buf + m->count; (c = *s) != '\0' && p < end; s++) { |
| 322 | if (!strchr(esc, c)) { |
| 323 | *p++ = c; |
| 324 | continue; |
| 325 | } |
| 326 | if (p + 3 < end) { |
| 327 | *p++ = '\\'; |
| 328 | *p++ = '0' + ((c & 0300) >> 6); |
| 329 | *p++ = '0' + ((c & 070) >> 3); |
| 330 | *p++ = '0' + (c & 07); |
| 331 | continue; |
| 332 | } |
| 333 | m->count = m->size; |
| 334 | return -1; |
| 335 | } |
| 336 | m->count = p - m->buf; |
| 337 | return 0; |
| 338 | } |
| 339 | EXPORT_SYMBOL(seq_escape); |
| 340 | |
| 341 | int seq_printf(struct seq_file *m, const char *f, ...) |
| 342 | { |
| 343 | va_list args; |
| 344 | int len; |
| 345 | |
| 346 | if (m->count < m->size) { |
| 347 | va_start(args, f); |
| 348 | len = vsnprintf(m->buf + m->count, m->size - m->count, f, args); |
| 349 | va_end(args); |
| 350 | if (m->count + len < m->size) { |
| 351 | m->count += len; |
| 352 | return 0; |
| 353 | } |
| 354 | } |
| 355 | m->count = m->size; |
| 356 | return -1; |
| 357 | } |
| 358 | EXPORT_SYMBOL(seq_printf); |
| 359 | |
Török Edwin | 74e2f33 | 2008-11-22 13:28:48 +0200 | [diff] [blame] | 360 | /** |
Török Edwin | 958086d | 2008-11-23 23:24:53 +0200 | [diff] [blame] | 361 | * mangle_path - mangle and copy path to buffer beginning |
| 362 | * @s: buffer start |
| 363 | * @p: beginning of path in above buffer |
| 364 | * @esc: set of characters that need escaping |
Török Edwin | 74e2f33 | 2008-11-22 13:28:48 +0200 | [diff] [blame] | 365 | * |
| 366 | * Copy the path from @p to @s, replacing each occurrence of character from |
| 367 | * @esc with usual octal escape. |
| 368 | * Returns pointer past last written character in @s, or NULL in case of |
| 369 | * failure. |
| 370 | */ |
| 371 | char *mangle_path(char *s, char *p, char *esc) |
Ram Pai | 6092d04 | 2008-03-27 13:06:20 +0100 | [diff] [blame] | 372 | { |
| 373 | while (s <= p) { |
| 374 | char c = *p++; |
| 375 | if (!c) { |
| 376 | return s; |
| 377 | } else if (!strchr(esc, c)) { |
| 378 | *s++ = c; |
| 379 | } else if (s + 4 > p) { |
| 380 | break; |
| 381 | } else { |
| 382 | *s++ = '\\'; |
| 383 | *s++ = '0' + ((c & 0300) >> 6); |
| 384 | *s++ = '0' + ((c & 070) >> 3); |
| 385 | *s++ = '0' + (c & 07); |
| 386 | } |
| 387 | } |
| 388 | return NULL; |
| 389 | } |
Ingo Molnar | 604094f | 2008-11-28 18:03:22 +0100 | [diff] [blame] | 390 | EXPORT_SYMBOL(mangle_path); |
Ram Pai | 6092d04 | 2008-03-27 13:06:20 +0100 | [diff] [blame] | 391 | |
| 392 | /* |
| 393 | * return the absolute path of 'dentry' residing in mount 'mnt'. |
| 394 | */ |
Jan Blunck | c32c2f6 | 2008-02-14 19:38:43 -0800 | [diff] [blame] | 395 | int seq_path(struct seq_file *m, struct path *path, char *esc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | { |
| 397 | if (m->count < m->size) { |
| 398 | char *s = m->buf + m->count; |
Jan Blunck | cf28b48 | 2008-02-14 19:38:44 -0800 | [diff] [blame] | 399 | char *p = d_path(path, s, m->size - m->count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | if (!IS_ERR(p)) { |
Ram Pai | 6092d04 | 2008-03-27 13:06:20 +0100 | [diff] [blame] | 401 | s = mangle_path(s, p, esc); |
| 402 | if (s) { |
| 403 | p = m->buf + m->count; |
| 404 | m->count = s - m->buf; |
| 405 | return s - p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | } |
| 407 | } |
| 408 | } |
| 409 | m->count = m->size; |
| 410 | return -1; |
| 411 | } |
| 412 | EXPORT_SYMBOL(seq_path); |
| 413 | |
Ram Pai | 6092d04 | 2008-03-27 13:06:20 +0100 | [diff] [blame] | 414 | /* |
Miklos Szeredi | 9d1bc601 | 2008-03-27 13:06:21 +0100 | [diff] [blame] | 415 | * Same as seq_path, but relative to supplied root. |
| 416 | * |
| 417 | * root may be changed, see __d_path(). |
| 418 | */ |
| 419 | int seq_path_root(struct seq_file *m, struct path *path, struct path *root, |
| 420 | char *esc) |
| 421 | { |
| 422 | int err = -ENAMETOOLONG; |
| 423 | if (m->count < m->size) { |
| 424 | char *s = m->buf + m->count; |
| 425 | char *p; |
| 426 | |
| 427 | spin_lock(&dcache_lock); |
| 428 | p = __d_path(path, root, s, m->size - m->count); |
| 429 | spin_unlock(&dcache_lock); |
| 430 | err = PTR_ERR(p); |
| 431 | if (!IS_ERR(p)) { |
| 432 | s = mangle_path(s, p, esc); |
| 433 | if (s) { |
| 434 | p = m->buf + m->count; |
| 435 | m->count = s - m->buf; |
| 436 | return 0; |
| 437 | } |
| 438 | } |
| 439 | } |
| 440 | m->count = m->size; |
| 441 | return err; |
| 442 | } |
| 443 | |
| 444 | /* |
Ram Pai | 6092d04 | 2008-03-27 13:06:20 +0100 | [diff] [blame] | 445 | * returns the path of the 'dentry' from the root of its filesystem. |
| 446 | */ |
| 447 | int seq_dentry(struct seq_file *m, struct dentry *dentry, char *esc) |
| 448 | { |
| 449 | if (m->count < m->size) { |
| 450 | char *s = m->buf + m->count; |
| 451 | char *p = dentry_path(dentry, s, m->size - m->count); |
| 452 | if (!IS_ERR(p)) { |
| 453 | s = mangle_path(s, p, esc); |
| 454 | if (s) { |
| 455 | p = m->buf + m->count; |
| 456 | m->count = s - m->buf; |
| 457 | return s - p; |
| 458 | } |
| 459 | } |
| 460 | } |
| 461 | m->count = m->size; |
| 462 | return -1; |
| 463 | } |
| 464 | |
Rusty Russell | cb78a0c | 2008-12-30 09:05:14 +1030 | [diff] [blame^] | 465 | int seq_bitmap(struct seq_file *m, const unsigned long *bits, |
| 466 | unsigned int nr_bits) |
Alexey Dobriyan | 50ac2d6 | 2008-08-12 15:09:02 -0700 | [diff] [blame] | 467 | { |
Lai Jiangshan | 85dd030 | 2008-10-18 20:28:18 -0700 | [diff] [blame] | 468 | if (m->count < m->size) { |
| 469 | int len = bitmap_scnprintf(m->buf + m->count, |
| 470 | m->size - m->count, bits, nr_bits); |
| 471 | if (m->count + len < m->size) { |
| 472 | m->count += len; |
| 473 | return 0; |
| 474 | } |
Alexey Dobriyan | 50ac2d6 | 2008-08-12 15:09:02 -0700 | [diff] [blame] | 475 | } |
| 476 | m->count = m->size; |
| 477 | return -1; |
| 478 | } |
Lai Jiangshan | 85dd030 | 2008-10-18 20:28:18 -0700 | [diff] [blame] | 479 | EXPORT_SYMBOL(seq_bitmap); |
Alexey Dobriyan | 50ac2d6 | 2008-08-12 15:09:02 -0700 | [diff] [blame] | 480 | |
Lai Jiangshan | 3eda201 | 2008-10-18 20:28:19 -0700 | [diff] [blame] | 481 | int seq_bitmap_list(struct seq_file *m, unsigned long *bits, |
| 482 | unsigned int nr_bits) |
| 483 | { |
| 484 | if (m->count < m->size) { |
| 485 | int len = bitmap_scnlistprintf(m->buf + m->count, |
| 486 | m->size - m->count, bits, nr_bits); |
| 487 | if (m->count + len < m->size) { |
| 488 | m->count += len; |
| 489 | return 0; |
| 490 | } |
| 491 | } |
| 492 | m->count = m->size; |
| 493 | return -1; |
| 494 | } |
| 495 | EXPORT_SYMBOL(seq_bitmap_list); |
| 496 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | static void *single_start(struct seq_file *p, loff_t *pos) |
| 498 | { |
| 499 | return NULL + (*pos == 0); |
| 500 | } |
| 501 | |
| 502 | static void *single_next(struct seq_file *p, void *v, loff_t *pos) |
| 503 | { |
| 504 | ++*pos; |
| 505 | return NULL; |
| 506 | } |
| 507 | |
| 508 | static void single_stop(struct seq_file *p, void *v) |
| 509 | { |
| 510 | } |
| 511 | |
| 512 | int single_open(struct file *file, int (*show)(struct seq_file *, void *), |
| 513 | void *data) |
| 514 | { |
| 515 | struct seq_operations *op = kmalloc(sizeof(*op), GFP_KERNEL); |
| 516 | int res = -ENOMEM; |
| 517 | |
| 518 | if (op) { |
| 519 | op->start = single_start; |
| 520 | op->next = single_next; |
| 521 | op->stop = single_stop; |
| 522 | op->show = show; |
| 523 | res = seq_open(file, op); |
| 524 | if (!res) |
| 525 | ((struct seq_file *)file->private_data)->private = data; |
| 526 | else |
| 527 | kfree(op); |
| 528 | } |
| 529 | return res; |
| 530 | } |
| 531 | EXPORT_SYMBOL(single_open); |
| 532 | |
| 533 | int single_release(struct inode *inode, struct file *file) |
| 534 | { |
Helge Deller | 15ad7cd | 2006-12-06 20:40:36 -0800 | [diff] [blame] | 535 | const struct seq_operations *op = ((struct seq_file *)file->private_data)->op; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | int res = seq_release(inode, file); |
| 537 | kfree(op); |
| 538 | return res; |
| 539 | } |
| 540 | EXPORT_SYMBOL(single_release); |
| 541 | |
| 542 | int seq_release_private(struct inode *inode, struct file *file) |
| 543 | { |
| 544 | struct seq_file *seq = file->private_data; |
| 545 | |
| 546 | kfree(seq->private); |
| 547 | seq->private = NULL; |
| 548 | return seq_release(inode, file); |
| 549 | } |
| 550 | EXPORT_SYMBOL(seq_release_private); |
| 551 | |
Pavel Emelyanov | 3969903 | 2007-10-10 02:28:42 -0700 | [diff] [blame] | 552 | void *__seq_open_private(struct file *f, const struct seq_operations *ops, |
| 553 | int psize) |
| 554 | { |
| 555 | int rc; |
| 556 | void *private; |
| 557 | struct seq_file *seq; |
| 558 | |
| 559 | private = kzalloc(psize, GFP_KERNEL); |
| 560 | if (private == NULL) |
| 561 | goto out; |
| 562 | |
| 563 | rc = seq_open(f, ops); |
| 564 | if (rc < 0) |
| 565 | goto out_free; |
| 566 | |
| 567 | seq = f->private_data; |
| 568 | seq->private = private; |
| 569 | return private; |
| 570 | |
| 571 | out_free: |
| 572 | kfree(private); |
| 573 | out: |
| 574 | return NULL; |
| 575 | } |
| 576 | EXPORT_SYMBOL(__seq_open_private); |
| 577 | |
| 578 | int seq_open_private(struct file *filp, const struct seq_operations *ops, |
| 579 | int psize) |
| 580 | { |
| 581 | return __seq_open_private(filp, ops, psize) ? 0 : -ENOMEM; |
| 582 | } |
| 583 | EXPORT_SYMBOL(seq_open_private); |
| 584 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | int seq_putc(struct seq_file *m, char c) |
| 586 | { |
| 587 | if (m->count < m->size) { |
| 588 | m->buf[m->count++] = c; |
| 589 | return 0; |
| 590 | } |
| 591 | return -1; |
| 592 | } |
| 593 | EXPORT_SYMBOL(seq_putc); |
| 594 | |
| 595 | int seq_puts(struct seq_file *m, const char *s) |
| 596 | { |
| 597 | int len = strlen(s); |
| 598 | if (m->count + len < m->size) { |
| 599 | memcpy(m->buf + m->count, s, len); |
| 600 | m->count += len; |
| 601 | return 0; |
| 602 | } |
| 603 | m->count = m->size; |
| 604 | return -1; |
| 605 | } |
| 606 | EXPORT_SYMBOL(seq_puts); |
Pavel Emelianov | bcf67e1 | 2007-07-10 17:22:26 -0700 | [diff] [blame] | 607 | |
| 608 | struct list_head *seq_list_start(struct list_head *head, loff_t pos) |
| 609 | { |
| 610 | struct list_head *lh; |
| 611 | |
| 612 | list_for_each(lh, head) |
| 613 | if (pos-- == 0) |
| 614 | return lh; |
| 615 | |
| 616 | return NULL; |
| 617 | } |
| 618 | |
| 619 | EXPORT_SYMBOL(seq_list_start); |
| 620 | |
| 621 | struct list_head *seq_list_start_head(struct list_head *head, loff_t pos) |
| 622 | { |
| 623 | if (!pos) |
| 624 | return head; |
| 625 | |
| 626 | return seq_list_start(head, pos - 1); |
| 627 | } |
| 628 | |
| 629 | EXPORT_SYMBOL(seq_list_start_head); |
| 630 | |
| 631 | struct list_head *seq_list_next(void *v, struct list_head *head, loff_t *ppos) |
| 632 | { |
| 633 | struct list_head *lh; |
| 634 | |
| 635 | lh = ((struct list_head *)v)->next; |
| 636 | ++*ppos; |
| 637 | return lh == head ? NULL : lh; |
| 638 | } |
| 639 | |
| 640 | EXPORT_SYMBOL(seq_list_next); |