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