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