* An optimized version of ttyoutq_read() which can be used in pseudo * TTY drivers to directly copy data from the outq to userspace, instead * of buffering it. * * We can only copy data directly if we need to read the entire block * back to the user, because we temporarily remove the block from the * queue. Otherwise we need to copy it to a temporary buffer first, to * make sure data remain
| 201 | * make sure data remains in the correct order. |
| 202 | */ |
| 203 | int |
| 204 | ttyoutq_read_uio(struct ttyoutq *to, struct tty *tp, struct uio *uio) |
| 205 | { |
| 206 | |
| 207 | while (uio->uio_resid > 0) { |
| 208 | int error; |
| 209 | struct ttyoutq_block *tob; |
| 210 | size_t cbegin, cend, clen; |
| 211 | |
| 212 | /* See if there still is data. */ |
| 213 | if (to->to_begin == to->to_end) |
| 214 | return (0); |
| 215 | tob = to->to_firstblock; |
| 216 | if (tob == NULL) |
| 217 | return (0); |
| 218 | |
| 219 | /* |
| 220 | * The end address should be the lowest of these three: |
| 221 | * - The write pointer |
| 222 | * - The blocksize - we can't read beyond the block |
| 223 | * - The end address if we could perform the full read |
| 224 | */ |
| 225 | cbegin = to->to_begin; |
| 226 | cend = MIN(MIN(to->to_end, to->to_begin + uio->uio_resid), |
| 227 | TTYOUTQ_DATASIZE); |
| 228 | clen = cend - cbegin; |
| 229 | |
| 230 | /* |
| 231 | * We can prevent buffering in some cases: |
| 232 | * - We need to read the block until the end. |
| 233 | * - We don't need to read the block until the end, but |
| 234 | * there is no data beyond it, which allows us to move |
| 235 | * the write pointer to a new block. |
| 236 | */ |
| 237 | if (cend == TTYOUTQ_DATASIZE || cend == to->to_end) { |
| 238 | /* |
| 239 | * Fast path: zero copy. Remove the first block, |
| 240 | * so we can unlock the TTY temporarily. |
| 241 | */ |
| 242 | TTYOUTQ_REMOVE_HEAD(to); |
| 243 | to->to_begin = 0; |
| 244 | if (to->to_end <= TTYOUTQ_DATASIZE) |
| 245 | to->to_end = 0; |
| 246 | else |
| 247 | to->to_end -= TTYOUTQ_DATASIZE; |
| 248 | |
| 249 | /* Temporary unlock and copy the data to userspace. */ |
| 250 | tty_unlock(tp); |
| 251 | error = uiomove(tob->tob_data + cbegin, clen, uio); |
| 252 | tty_lock(tp); |
| 253 | |
| 254 | /* Block can now be readded to the list. */ |
| 255 | TTYOUTQ_RECYCLE(to, tob); |
| 256 | } else { |
| 257 | char ob[TTYOUTQ_DATASIZE - 1]; |
| 258 | |
| 259 | /* |
| 260 | * Slow path: store data in a temporary buffer. |
no test coverage detected