- if (sz + *pos > hello->len)
- sz = hello->len - *pos;
- r = copy_to_user(buf, hello->buffer + *pos, sz);
- up(&hello_mtx);
+ len = CIRC_CNT(hello->head, hello->tail, MAXLEN);
+ if (sz > len)
+ sz = len;
+ /* First, copy at most until the limit of the buffer */
+ fsz = min(sz, CIRC_CNT_TO_END(hello->head, hello->tail, MAXLEN));
+ r = copy_to_user(buf, hello->buf + hello->tail, fsz);
+ ssz = sz - fsz;
+ if (!r && !ssz)
+ r = copy_to_user(buf + fsz, hello->buf, ssz);
+ if (!r)
+ hello->tail = (hello->tail + sz) & ~MAXLEN;
+ mutex_unlock(&hello_mtx);