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