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