LCOV - code coverage report
Current view: top level - tty - n_tty.c (source / functions) Coverage Total Hit
Test: TTY Combined Coverage Lines: 44.9 % 1286 578
Test Date: 2025-08-26 15:45:50 Functions: 65.3 % 75 49

            Line data    Source code
       1              : // SPDX-License-Identifier: GPL-1.0+
       2              : /*
       3              :  * n_tty.c --- implements the N_TTY line discipline.
       4              :  *
       5              :  * This code used to be in tty_io.c, but things are getting hairy
       6              :  * enough that it made sense to split things off.  (The N_TTY
       7              :  * processing has changed so much that it's hardly recognizable,
       8              :  * anyway...)
       9              :  *
      10              :  * Note that the open routine for N_TTY is guaranteed never to return
      11              :  * an error.  This is because Linux will fall back to setting a line
      12              :  * to N_TTY if it can not switch to any other line discipline.
      13              :  *
      14              :  * Written by Theodore Ts'o, Copyright 1994.
      15              :  *
      16              :  * This file also contains code originally written by Linus Torvalds,
      17              :  * Copyright 1991, 1992, 1993, and by Julian Cowley, Copyright 1994.
      18              :  *
      19              :  * Reduced memory usage for older ARM systems  - Russell King.
      20              :  *
      21              :  * 2000/01/20   Fixed SMP locking on put_tty_queue using bits of
      22              :  *              the patch by Andrew J. Kroll <[email protected]>
      23              :  *              who actually finally proved there really was a race.
      24              :  *
      25              :  * 2002/03/18   Implemented n_tty_wakeup to send SIGIO POLL_OUTs to
      26              :  *              waiting writing processes-Sapan Bhatia <[email protected]>.
      27              :  *              Also fixed a bug in BLOCKING mode where n_tty_write returns
      28              :  *              EAGAIN
      29              :  */
      30              : 
      31              : #include <linux/bitmap.h>
      32              : #include <linux/bitops.h>
      33              : #include <linux/ctype.h>
      34              : #include <linux/errno.h>
      35              : #include <linux/export.h>
      36              : #include <linux/fcntl.h>
      37              : #include <linux/file.h>
      38              : #include <linux/jiffies.h>
      39              : #include <linux/math.h>
      40              : #include <linux/poll.h>
      41              : #include <linux/ratelimit.h>
      42              : #include <linux/sched.h>
      43              : #include <linux/signal.h>
      44              : #include <linux/slab.h>
      45              : #include <linux/string.h>
      46              : #include <linux/tty.h>
      47              : #include <linux/types.h>
      48              : #include <linux/uaccess.h>
      49              : #include <linux/vmalloc.h>
      50              : 
      51              : #include "tty.h"
      52              : 
      53              : /*
      54              :  * Until this number of characters is queued in the xmit buffer, select will
      55              :  * return "we have room for writes".
      56              :  */
      57              : #define WAKEUP_CHARS 256
      58              : 
      59              : #define N_TTY_BUF_SIZE 4096
      60              : 
      61              : /*
      62              :  * This defines the low- and high-watermarks for throttling and
      63              :  * unthrottling the TTY driver.  These watermarks are used for
      64              :  * controlling the space in the read buffer.
      65              :  */
      66              : #define TTY_THRESHOLD_THROTTLE          128 /* now based on remaining room */
      67              : #define TTY_THRESHOLD_UNTHROTTLE        128
      68              : 
      69              : /*
      70              :  * Special byte codes used in the echo buffer to represent operations
      71              :  * or special handling of characters.  Bytes in the echo buffer that
      72              :  * are not part of such special blocks are treated as normal character
      73              :  * codes.
      74              :  */
      75              : #define ECHO_OP_START 0xff
      76              : #define ECHO_OP_MOVE_BACK_COL 0x80
      77              : #define ECHO_OP_SET_CANON_COL 0x81
      78              : #define ECHO_OP_ERASE_TAB 0x82
      79              : 
      80              : #define ECHO_COMMIT_WATERMARK   256
      81              : #define ECHO_BLOCK              256
      82              : #define ECHO_DISCARD_WATERMARK  N_TTY_BUF_SIZE - (ECHO_BLOCK + 32)
      83              : 
      84              : struct n_tty_data {
      85              :         /* producer-published */
      86              :         size_t read_head;
      87              :         size_t commit_head;
      88              :         size_t canon_head;
      89              :         size_t echo_head;
      90              :         size_t echo_commit;
      91              :         size_t echo_mark;
      92              :         DECLARE_BITMAP(char_map, 256);
      93              : 
      94              :         /* private to n_tty_receive_overrun (single-threaded) */
      95              :         unsigned long overrun_time;
      96              :         unsigned int num_overrun;
      97              : 
      98              :         /* non-atomic */
      99              :         bool no_room;
     100              : 
     101              :         /* must hold exclusive termios_rwsem to reset these */
     102              :         unsigned char lnext:1, erasing:1, raw:1, real_raw:1, icanon:1;
     103              :         unsigned char push:1;
     104              : 
     105              :         /* shared by producer and consumer */
     106              :         u8 read_buf[N_TTY_BUF_SIZE];
     107              :         DECLARE_BITMAP(read_flags, N_TTY_BUF_SIZE);
     108              :         u8 echo_buf[N_TTY_BUF_SIZE];
     109              : 
     110              :         /* consumer-published */
     111              :         size_t read_tail;
     112              :         size_t line_start;
     113              : 
     114              :         /* # of chars looked ahead (to find software flow control chars) */
     115              :         size_t lookahead_count;
     116              : 
     117              :         /* protected by output lock */
     118              :         unsigned int column;
     119              :         unsigned int canon_column;
     120              :         size_t echo_tail;
     121              : 
     122              :         struct mutex atomic_read_lock;
     123              :         struct mutex output_lock;
     124              : };
     125              : 
     126              : #define MASK(x) ((x) & (N_TTY_BUF_SIZE - 1))
     127              : 
     128           33 : static inline size_t read_cnt(struct n_tty_data *ldata)
     129              : {
     130           33 :         return ldata->read_head - ldata->read_tail;
     131              : }
     132              : 
     133            0 : static inline u8 read_buf(struct n_tty_data *ldata, size_t i)
     134              : {
     135            0 :         return ldata->read_buf[MASK(i)];
     136              : }
     137              : 
     138           74 : static inline u8 *read_buf_addr(struct n_tty_data *ldata, size_t i)
     139              : {
     140           74 :         return &ldata->read_buf[MASK(i)];
     141              : }
     142              : 
     143           14 : static inline u8 echo_buf(struct n_tty_data *ldata, size_t i)
     144              : {
     145           14 :         smp_rmb(); /* Matches smp_wmb() in add_echo_byte(). */
     146           14 :         return ldata->echo_buf[MASK(i)];
     147              : }
     148              : 
     149           14 : static inline u8 *echo_buf_addr(struct n_tty_data *ldata, size_t i)
     150              : {
     151           14 :         return &ldata->echo_buf[MASK(i)];
     152              : }
     153              : 
     154              : /* If we are not echoing the data, perhaps this is a secret so erase it */
     155           25 : static void zero_buffer(const struct tty_struct *tty, u8 *buffer, size_t size)
     156              : {
     157           25 :         if (L_ICANON(tty) && !L_ECHO(tty))
     158            0 :                 memset(buffer, 0, size);
     159           25 : }
     160              : 
     161            0 : static void tty_copy(const struct tty_struct *tty, void *to, size_t tail,
     162              :                      size_t n)
     163              : {
     164            0 :         struct n_tty_data *ldata = tty->disc_data;
     165            0 :         size_t size = N_TTY_BUF_SIZE - tail;
     166            0 :         void *from = read_buf_addr(ldata, tail);
     167              : 
     168            0 :         if (n > size) {
     169            0 :                 tty_audit_add_data(tty, from, size);
     170            0 :                 memcpy(to, from, size);
     171            0 :                 zero_buffer(tty, from, size);
     172            0 :                 to += size;
     173            0 :                 n -= size;
     174            0 :                 from = ldata->read_buf;
     175            0 :         }
     176              : 
     177            0 :         tty_audit_add_data(tty, from, n);
     178            0 :         memcpy(to, from, n);
     179            0 :         zero_buffer(tty, from, n);
     180            0 : }
     181              : 
     182              : /**
     183              :  * n_tty_kick_worker - start input worker (if required)
     184              :  * @tty: terminal
     185              :  *
     186              :  * Re-schedules the flip buffer work if it may have stopped.
     187              :  *
     188              :  * Locking:
     189              :  *  * Caller holds exclusive %termios_rwsem, or
     190              :  *  * n_tty_read()/consumer path:
     191              :  *      holds non-exclusive %termios_rwsem
     192              :  */
     193           36 : static void n_tty_kick_worker(const struct tty_struct *tty)
     194              : {
     195           36 :         struct n_tty_data *ldata = tty->disc_data;
     196              : 
     197              :         /* Did the input worker stop? Restart it */
     198           36 :         if (unlikely(READ_ONCE(ldata->no_room))) {
     199            0 :                 WRITE_ONCE(ldata->no_room, 0);
     200              : 
     201            0 :                 WARN_RATELIMIT(tty->port->itty == NULL,
     202              :                                 "scheduling with invalid itty\n");
     203              :                 /* see if ldisc has been killed - if so, this means that
     204              :                  * even though the ldisc has been halted and ->buf.work
     205              :                  * cancelled, ->buf.work is about to be rescheduled
     206              :                  */
     207            0 :                 WARN_RATELIMIT(test_bit(TTY_LDISC_HALTED, &tty->flags),
     208              :                                "scheduling buffer work for halted ldisc\n");
     209            0 :                 tty_buffer_restart_work(tty->port);
     210            0 :         }
     211           36 : }
     212              : 
     213           12 : static ssize_t chars_in_buffer(const struct tty_struct *tty)
     214              : {
     215           12 :         const struct n_tty_data *ldata = tty->disc_data;
     216           12 :         size_t head = ldata->icanon ? ldata->canon_head : ldata->commit_head;
     217              : 
     218           24 :         return head - ldata->read_tail;
     219           12 : }
     220              : 
     221              : /**
     222              :  * n_tty_write_wakeup   -       asynchronous I/O notifier
     223              :  * @tty: tty device
     224              :  *
     225              :  * Required for the ptys, serial driver etc. since processes that attach
     226              :  * themselves to the master and rely on ASYNC IO must be woken up.
     227              :  */
     228            0 : static void n_tty_write_wakeup(struct tty_struct *tty)
     229              : {
     230            0 :         clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
     231            0 :         kill_fasync(&tty->fasync, SIGIO, POLL_OUT);
     232            0 : }
     233              : 
     234            1 : static void n_tty_check_throttle(struct tty_struct *tty)
     235              : {
     236            1 :         struct n_tty_data *ldata = tty->disc_data;
     237              : 
     238              :         /*
     239              :          * Check the remaining room for the input canonicalization
     240              :          * mode.  We don't want to throttle the driver if we're in
     241              :          * canonical mode and don't have a newline yet!
     242              :          */
     243            1 :         if (ldata->icanon && ldata->canon_head == ldata->read_tail)
     244            0 :                 return;
     245              : 
     246            1 :         do {
     247            1 :                 tty_set_flow_change(tty, TTY_THROTTLE_SAFE);
     248            1 :                 if (N_TTY_BUF_SIZE - read_cnt(ldata) >= TTY_THRESHOLD_THROTTLE)
     249            1 :                         break;
     250            0 :         } while (!tty_throttle_safe(tty));
     251              : 
     252            1 :         __tty_set_flow_change(tty, 0);
     253            1 : }
     254              : 
     255           12 : static void n_tty_check_unthrottle(struct tty_struct *tty)
     256              : {
     257           12 :         if (tty->driver->type == TTY_DRIVER_TYPE_PTY) {
     258           12 :                 if (chars_in_buffer(tty) > TTY_THRESHOLD_UNTHROTTLE)
     259            0 :                         return;
     260           12 :                 n_tty_kick_worker(tty);
     261           12 :                 tty_wakeup(tty->link);
     262           12 :                 return;
     263              :         }
     264              : 
     265              :         /* If there is enough space in the read buffer now, let the
     266              :          * low-level driver know. We use chars_in_buffer() to
     267              :          * check the buffer, as it now knows about canonical mode.
     268              :          * Otherwise, if the driver is throttled and the line is
     269              :          * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode,
     270              :          * we won't get any more characters.
     271              :          */
     272              : 
     273            0 :         do {
     274            0 :                 tty_set_flow_change(tty, TTY_UNTHROTTLE_SAFE);
     275            0 :                 if (chars_in_buffer(tty) > TTY_THRESHOLD_UNTHROTTLE)
     276            0 :                         break;
     277              : 
     278            0 :                 n_tty_kick_worker(tty);
     279            0 :         } while (!tty_unthrottle_safe(tty));
     280              : 
     281            0 :         __tty_set_flow_change(tty, 0);
     282           12 : }
     283              : 
     284              : /**
     285              :  * put_tty_queue                -       add character to tty
     286              :  * @c: character
     287              :  * @ldata: n_tty data
     288              :  *
     289              :  * Add a character to the tty read_buf queue.
     290              :  *
     291              :  * Locking:
     292              :  *  * n_tty_receive_buf()/producer path:
     293              :  *      caller holds non-exclusive %termios_rwsem
     294              :  */
     295            9 : static inline void put_tty_queue(u8 c, struct n_tty_data *ldata)
     296              : {
     297            9 :         *read_buf_addr(ldata, ldata->read_head) = c;
     298            9 :         ldata->read_head++;
     299            9 : }
     300              : 
     301              : /**
     302              :  * reset_buffer_flags   -       reset buffer state
     303              :  * @ldata: line disc data to reset
     304              :  *
     305              :  * Reset the read buffer counters and clear the flags. Called from
     306              :  * n_tty_open() and n_tty_flush_buffer().
     307              :  *
     308              :  * Locking:
     309              :  *  * caller holds exclusive %termios_rwsem, or
     310              :  *  * (locking is not required)
     311              :  */
     312           12 : static void reset_buffer_flags(struct n_tty_data *ldata)
     313              : {
     314           12 :         ldata->read_head = ldata->canon_head = ldata->read_tail = 0;
     315           12 :         ldata->commit_head = 0;
     316           12 :         ldata->line_start = 0;
     317              : 
     318           12 :         ldata->erasing = 0;
     319           12 :         bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE);
     320           12 :         ldata->push = 0;
     321              : 
     322           12 :         ldata->lookahead_count = 0;
     323           12 : }
     324              : 
     325            0 : static void n_tty_packet_mode_flush(struct tty_struct *tty)
     326              : {
     327            0 :         unsigned long flags;
     328              : 
     329            0 :         if (tty->link->ctrl.packet) {
     330            0 :                 spin_lock_irqsave(&tty->ctrl.lock, flags);
     331            0 :                 tty->ctrl.pktstatus |= TIOCPKT_FLUSHREAD;
     332            0 :                 spin_unlock_irqrestore(&tty->ctrl.lock, flags);
     333            0 :                 wake_up_interruptible(&tty->link->read_wait);
     334            0 :         }
     335            0 : }
     336              : 
     337              : /**
     338              :  * n_tty_flush_buffer   -       clean input queue
     339              :  * @tty: terminal device
     340              :  *
     341              :  * Flush the input buffer. Called when the tty layer wants the buffer flushed
     342              :  * (eg at hangup) or when the %N_TTY line discipline internally has to clean
     343              :  * the pending queue (for example some signals).
     344              :  *
     345              :  * Holds %termios_rwsem to exclude producer/consumer while buffer indices are
     346              :  * reset.
     347              :  *
     348              :  * Locking: %ctrl.lock, exclusive %termios_rwsem
     349              :  */
     350           12 : static void n_tty_flush_buffer(struct tty_struct *tty)
     351              : {
     352           12 :         down_write(&tty->termios_rwsem);
     353           12 :         reset_buffer_flags(tty->disc_data);
     354           12 :         n_tty_kick_worker(tty);
     355              : 
     356           12 :         if (tty->link)
     357            0 :                 n_tty_packet_mode_flush(tty);
     358           12 :         up_write(&tty->termios_rwsem);
     359           12 : }
     360              : 
     361              : /**
     362              :  * is_utf8_continuation -       utf8 multibyte check
     363              :  * @c: byte to check
     364              :  *
     365              :  * Returns: true if the utf8 character @c is a multibyte continuation
     366              :  * character. We use this to correctly compute the on-screen size of the
     367              :  * character when printing.
     368              :  */
     369         1058 : static inline int is_utf8_continuation(u8 c)
     370              : {
     371         1058 :         return (c & 0xc0) == 0x80;
     372              : }
     373              : 
     374              : /**
     375              :  * is_continuation      -       multibyte check
     376              :  * @c: byte to check
     377              :  * @tty: terminal device
     378              :  *
     379              :  * Returns: true if the utf8 character @c is a multibyte continuation character
     380              :  * and the terminal is in unicode mode.
     381              :  */
     382         1203 : static inline int is_continuation(u8 c, const struct tty_struct *tty)
     383              : {
     384         1203 :         return I_IUTF8(tty) && is_utf8_continuation(c);
     385              : }
     386              : 
     387              : /**
     388              :  * do_output_char       -       output one character
     389              :  * @c: character (or partial unicode symbol)
     390              :  * @tty: terminal device
     391              :  * @space: space available in tty driver write buffer
     392              :  *
     393              :  * This is a helper function that handles one output character (including
     394              :  * special characters like TAB, CR, LF, etc.), doing OPOST processing and
     395              :  * putting the results in the tty driver's write buffer.
     396              :  *
     397              :  * Note that Linux currently ignores TABDLY, CRDLY, VTDLY, FFDLY and NLDLY.
     398              :  * They simply aren't relevant in the world today. If you ever need them, add
     399              :  * them here.
     400              :  *
     401              :  * Returns: the number of bytes of buffer space used or -1 if no space left.
     402              :  *
     403              :  * Locking: should be called under the %output_lock to protect the column state
     404              :  * and space left in the buffer.
     405              :  */
     406           35 : static int do_output_char(u8 c, struct tty_struct *tty, int space)
     407              : {
     408           35 :         struct n_tty_data *ldata = tty->disc_data;
     409           35 :         int     spaces;
     410              : 
     411           35 :         if (!space)
     412            0 :                 return -1;
     413              : 
     414           35 :         switch (c) {
     415              :         case '\n':
     416           28 :                 if (O_ONLRET(tty))
     417            0 :                         ldata->column = 0;
     418           28 :                 if (O_ONLCR(tty)) {
     419           28 :                         if (space < 2)
     420            0 :                                 return -1;
     421           28 :                         ldata->canon_column = ldata->column = 0;
     422           28 :                         tty->ops->write(tty, "\r\n", 2);
     423           28 :                         return 2;
     424              :                 }
     425            0 :                 ldata->canon_column = ldata->column;
     426            0 :                 break;
     427              :         case '\r':
     428            0 :                 if (O_ONOCR(tty) && ldata->column == 0)
     429            0 :                         return 0;
     430            0 :                 if (O_OCRNL(tty)) {
     431            0 :                         c = '\n';
     432            0 :                         if (O_ONLRET(tty))
     433            0 :                                 ldata->canon_column = ldata->column = 0;
     434            0 :                         break;
     435              :                 }
     436            0 :                 ldata->canon_column = ldata->column = 0;
     437            0 :                 break;
     438              :         case '\t':
     439            0 :                 spaces = 8 - (ldata->column & 7);
     440            0 :                 if (O_TABDLY(tty) == XTABS) {
     441            0 :                         if (space < spaces)
     442            0 :                                 return -1;
     443            0 :                         ldata->column += spaces;
     444            0 :                         tty->ops->write(tty, "        ", spaces);
     445            0 :                         return spaces;
     446              :                 }
     447            0 :                 ldata->column += spaces;
     448            0 :                 break;
     449              :         case '\b':
     450            0 :                 if (ldata->column > 0)
     451            0 :                         ldata->column--;
     452            0 :                 break;
     453              :         default:
     454            7 :                 if (!iscntrl(c)) {
     455            7 :                         if (O_OLCUC(tty))
     456            0 :                                 c = toupper(c);
     457            7 :                         if (!is_continuation(c, tty))
     458            7 :                                 ldata->column++;
     459            7 :                 }
     460            7 :                 break;
     461              :         }
     462              : 
     463            7 :         tty_put_char(tty, c);
     464            7 :         return 1;
     465           35 : }
     466              : 
     467              : /**
     468              :  * process_output       -       output post processor
     469              :  * @c: character (or partial unicode symbol)
     470              :  * @tty: terminal device
     471              :  *
     472              :  * Output one character with OPOST processing.
     473              :  *
     474              :  * Returns: -1 when the output device is full and the character must be
     475              :  * retried.
     476              :  *
     477              :  * Locking: %output_lock to protect column state and space left (also, this is
     478              :  *called from n_tty_write() under the tty layer write lock).
     479              :  */
     480           27 : static int process_output(u8 c, struct tty_struct *tty)
     481              : {
     482           27 :         struct n_tty_data *ldata = tty->disc_data;
     483              : 
     484           27 :         guard(mutex)(&ldata->output_lock);
     485              : 
     486           27 :         if (do_output_char(c, tty, tty_write_room(tty)) < 0)
     487            0 :                 return -1;
     488              : 
     489           27 :         return 0;
     490           27 : }
     491              : 
     492              : /**
     493              :  * process_output_block -       block post processor
     494              :  * @tty: terminal device
     495              :  * @buf: character buffer
     496              :  * @nr: number of bytes to output
     497              :  *
     498              :  * Output a block of characters with OPOST processing.
     499              :  *
     500              :  * This path is used to speed up block console writes, among other things when
     501              :  * processing blocks of output data. It handles only the simple cases normally
     502              :  * found and helps to generate blocks of symbols for the console driver and
     503              :  * thus improve performance.
     504              :  *
     505              :  * Returns: the number of characters output.
     506              :  *
     507              :  * Locking: %output_lock to protect column state and space left (also, this is
     508              :  * called from n_tty_write() under the tty layer write lock).
     509              :  */
     510           31 : static ssize_t process_output_block(struct tty_struct *tty,
     511              :                                     const u8 *buf, unsigned int nr)
     512              : {
     513           31 :         struct n_tty_data *ldata = tty->disc_data;
     514           31 :         unsigned int space, i;
     515           31 :         const u8 *cp;
     516              : 
     517           31 :         guard(mutex)(&ldata->output_lock);
     518              : 
     519           31 :         space = tty_write_room(tty);
     520           31 :         if (space == 0)
     521            0 :                 return 0;
     522              : 
     523           31 :         if (nr > space)
     524            0 :                 nr = space;
     525              : 
     526         1227 :         for (i = 0, cp = buf; i < nr; i++, cp++) {
     527         1223 :                 u8 c = *cp;
     528              : 
     529         1223 :                 switch (c) {
     530              :                 case '\n':
     531           27 :                         if (O_ONLRET(tty))
     532            0 :                                 ldata->column = 0;
     533           27 :                         if (O_ONLCR(tty))
     534           27 :                                 goto do_write;
     535            0 :                         ldata->canon_column = ldata->column;
     536            0 :                         break;
     537              :                 case '\r':
     538            0 :                         if (O_ONOCR(tty) && ldata->column == 0)
     539            0 :                                 goto do_write;
     540            0 :                         if (O_OCRNL(tty))
     541            0 :                                 goto do_write;
     542            0 :                         ldata->canon_column = ldata->column = 0;
     543            0 :                         break;
     544              :                 case '\t':
     545            0 :                         goto do_write;
     546              :                 case '\b':
     547            0 :                         if (ldata->column > 0)
     548            0 :                                 ldata->column--;
     549            0 :                         break;
     550              :                 default:
     551         1196 :                         if (!iscntrl(c)) {
     552         1196 :                                 if (O_OLCUC(tty))
     553            0 :                                         goto do_write;
     554         1196 :                                 if (!is_continuation(c, tty))
     555         1196 :                                         ldata->column++;
     556         1196 :                         }
     557         1196 :                         break;
     558              :                 }
     559         1227 :         }
     560              : do_write:
     561           31 :         return tty->ops->write(tty, buf, i);
     562           31 : }
     563              : 
     564            3 : static int n_tty_process_echo_ops(struct tty_struct *tty, size_t *tail,
     565              :                                   int space)
     566              : {
     567            3 :         struct n_tty_data *ldata = tty->disc_data;
     568            3 :         u8 op;
     569              : 
     570              :         /*
     571              :          * Since add_echo_byte() is called without holding output_lock, we
     572              :          * might see only portion of multi-byte operation.
     573              :          */
     574            3 :         if (MASK(ldata->echo_commit) == MASK(*tail + 1))
     575            0 :                 return -ENODATA;
     576              : 
     577              :         /*
     578              :          * If the buffer byte is the start of a multi-byte operation, get the
     579              :          * next byte, which is either the op code or a control character value.
     580              :          */
     581            3 :         op = echo_buf(ldata, *tail + 1);
     582              : 
     583            3 :         switch (op) {
     584              :         case ECHO_OP_ERASE_TAB: {
     585            0 :                 unsigned int num_chars, num_bs;
     586              : 
     587            0 :                 if (MASK(ldata->echo_commit) == MASK(*tail + 2))
     588            0 :                         return -ENODATA;
     589              : 
     590            0 :                 num_chars = echo_buf(ldata, *tail + 2);
     591              : 
     592              :                 /*
     593              :                  * Determine how many columns to go back in order to erase the
     594              :                  * tab. This depends on the number of columns used by other
     595              :                  * characters within the tab area. If this (modulo 8) count is
     596              :                  * from the start of input rather than from a previous tab, we
     597              :                  * offset by canon column. Otherwise, tab spacing is normal.
     598              :                  */
     599            0 :                 if (!(num_chars & 0x80))
     600            0 :                         num_chars += ldata->canon_column;
     601            0 :                 num_bs = 8 - (num_chars & 7);
     602              : 
     603            0 :                 if (num_bs > space)
     604            0 :                         return -ENOSPC;
     605              : 
     606            0 :                 space -= num_bs;
     607            0 :                 while (num_bs--) {
     608            0 :                         tty_put_char(tty, '\b');
     609            0 :                         if (ldata->column > 0)
     610            0 :                                 ldata->column--;
     611              :                 }
     612            0 :                 *tail += 3;
     613            0 :                 break;
     614            0 :         }
     615              :         case ECHO_OP_SET_CANON_COL:
     616            2 :                 ldata->canon_column = ldata->column;
     617            2 :                 *tail += 2;
     618            2 :                 break;
     619              : 
     620              :         case ECHO_OP_MOVE_BACK_COL:
     621            0 :                 if (ldata->column > 0)
     622            0 :                         ldata->column--;
     623            0 :                 *tail += 2;
     624            0 :                 break;
     625              : 
     626              :         case ECHO_OP_START:
     627              :                 /* This is an escaped echo op start code */
     628            0 :                 if (!space)
     629            0 :                         return -ENOSPC;
     630              : 
     631            0 :                 tty_put_char(tty, ECHO_OP_START);
     632            0 :                 ldata->column++;
     633            0 :                 space--;
     634            0 :                 *tail += 2;
     635            0 :                 break;
     636              : 
     637              :         default:
     638              :                 /*
     639              :                  * If the op is not a special byte code, it is a ctrl char
     640              :                  * tagged to be echoed as "^X" (where X is the letter
     641              :                  * representing the control char). Note that we must ensure
     642              :                  * there is enough space for the whole ctrl pair.
     643              :                  */
     644            1 :                 if (space < 2)
     645            0 :                         return -ENOSPC;
     646              : 
     647            1 :                 tty_put_char(tty, '^');
     648            1 :                 tty_put_char(tty, op ^ 0100);
     649            1 :                 ldata->column += 2;
     650            1 :                 space -= 2;
     651            1 :                 *tail += 2;
     652            1 :                 break;
     653              :         }
     654              : 
     655            3 :         return space;
     656            3 : }
     657              : 
     658              : /**
     659              :  * __process_echoes     -       write pending echo characters
     660              :  * @tty: terminal device
     661              :  *
     662              :  * Write previously buffered echo (and other ldisc-generated) characters to the
     663              :  * tty.
     664              :  *
     665              :  * Characters generated by the ldisc (including echoes) need to be buffered
     666              :  * because the driver's write buffer can fill during heavy program output.
     667              :  * Echoing straight to the driver will often fail under these conditions,
     668              :  * causing lost characters and resulting mismatches of ldisc state information.
     669              :  *
     670              :  * Since the ldisc state must represent the characters actually sent to the
     671              :  * driver at the time of the write, operations like certain changes in column
     672              :  * state are also saved in the buffer and executed here.
     673              :  *
     674              :  * A circular fifo buffer is used so that the most recent characters are
     675              :  * prioritized. Also, when control characters are echoed with a prefixed "^",
     676              :  * the pair is treated atomically and thus not separated.
     677              :  *
     678              :  * Locking: callers must hold %output_lock.
     679              :  */
     680            1 : static size_t __process_echoes(struct tty_struct *tty)
     681              : {
     682            1 :         struct n_tty_data *ldata = tty->disc_data;
     683            1 :         unsigned int space, old_space;
     684            1 :         size_t tail;
     685            1 :         u8 c;
     686              : 
     687            1 :         old_space = space = tty_write_room(tty);
     688              : 
     689            1 :         tail = ldata->echo_tail;
     690           12 :         while (MASK(ldata->echo_commit) != MASK(tail)) {
     691           11 :                 c = echo_buf(ldata, tail);
     692           11 :                 if (c == ECHO_OP_START) {
     693            3 :                         int ret = n_tty_process_echo_ops(tty, &tail, space);
     694            3 :                         if (ret == -ENODATA)
     695            0 :                                 goto not_yet_stored;
     696            3 :                         if (ret < 0)
     697            0 :                                 break;
     698            3 :                         space = ret;
     699            3 :                 } else {
     700            8 :                         if (O_OPOST(tty)) {
     701            8 :                                 int retval = do_output_char(c, tty, space);
     702            8 :                                 if (retval < 0)
     703            0 :                                         break;
     704            8 :                                 space -= retval;
     705            8 :                         } else {
     706            0 :                                 if (!space)
     707            0 :                                         break;
     708            0 :                                 tty_put_char(tty, c);
     709            0 :                                 space -= 1;
     710              :                         }
     711            8 :                         tail += 1;
     712              :                 }
     713              :         }
     714              : 
     715              :         /* If the echo buffer is nearly full (so that the possibility exists
     716              :          * of echo overrun before the next commit), then discard enough
     717              :          * data at the tail to prevent a subsequent overrun */
     718            1 :         while (ldata->echo_commit > tail &&
     719            0 :                ldata->echo_commit - tail >= ECHO_DISCARD_WATERMARK) {
     720            0 :                 if (echo_buf(ldata, tail) == ECHO_OP_START) {
     721            0 :                         if (echo_buf(ldata, tail + 1) == ECHO_OP_ERASE_TAB)
     722            0 :                                 tail += 3;
     723              :                         else
     724            0 :                                 tail += 2;
     725            0 :                 } else
     726            0 :                         tail++;
     727              :         }
     728              : 
     729              :  not_yet_stored:
     730            1 :         ldata->echo_tail = tail;
     731            1 :         return old_space - space;
     732            1 : }
     733              : 
     734            9 : static void commit_echoes(struct tty_struct *tty)
     735              : {
     736            9 :         struct n_tty_data *ldata = tty->disc_data;
     737            9 :         size_t nr, old, echoed;
     738            9 :         size_t head;
     739              : 
     740            9 :         mutex_lock(&ldata->output_lock);
     741            9 :         head = ldata->echo_head;
     742            9 :         ldata->echo_mark = head;
     743            9 :         old = ldata->echo_commit - ldata->echo_tail;
     744              : 
     745              :         /* Process committed echoes if the accumulated # of bytes
     746              :          * is over the threshold (and try again each time another
     747              :          * block is accumulated) */
     748            9 :         nr = head - ldata->echo_tail;
     749            9 :         if (nr < ECHO_COMMIT_WATERMARK ||
     750            0 :             (nr % ECHO_BLOCK > old % ECHO_BLOCK)) {
     751            9 :                 mutex_unlock(&ldata->output_lock);
     752            9 :                 return;
     753              :         }
     754              : 
     755            0 :         ldata->echo_commit = head;
     756            0 :         echoed = __process_echoes(tty);
     757            0 :         mutex_unlock(&ldata->output_lock);
     758              : 
     759            0 :         if (echoed && tty->ops->flush_chars)
     760            0 :                 tty->ops->flush_chars(tty);
     761            9 : }
     762              : 
     763           44 : static void process_echoes(struct tty_struct *tty)
     764              : {
     765           44 :         struct n_tty_data *ldata = tty->disc_data;
     766           44 :         size_t echoed;
     767              : 
     768           44 :         if (ldata->echo_mark == ldata->echo_tail)
     769           44 :                 return;
     770              : 
     771            0 :         mutex_lock(&ldata->output_lock);
     772            0 :         ldata->echo_commit = ldata->echo_mark;
     773            0 :         echoed = __process_echoes(tty);
     774            0 :         mutex_unlock(&ldata->output_lock);
     775              : 
     776            0 :         if (echoed && tty->ops->flush_chars)
     777            0 :                 tty->ops->flush_chars(tty);
     778           44 : }
     779              : 
     780              : /* NB: echo_mark and echo_head should be equivalent here */
     781            1 : static void flush_echoes(struct tty_struct *tty)
     782              : {
     783            1 :         struct n_tty_data *ldata = tty->disc_data;
     784              : 
     785            1 :         if ((!L_ECHO(tty) && !L_ECHONL(tty)) ||
     786            1 :             ldata->echo_commit == ldata->echo_head)
     787            0 :                 return;
     788              : 
     789            1 :         mutex_lock(&ldata->output_lock);
     790            1 :         ldata->echo_commit = ldata->echo_head;
     791            1 :         __process_echoes(tty);
     792            1 :         mutex_unlock(&ldata->output_lock);
     793            1 : }
     794              : 
     795              : /**
     796              :  * add_echo_byte        -       add a byte to the echo buffer
     797              :  * @c: unicode byte to echo
     798              :  * @ldata: n_tty data
     799              :  *
     800              :  * Add a character or operation byte to the echo buffer.
     801              :  */
     802           14 : static inline void add_echo_byte(u8 c, struct n_tty_data *ldata)
     803              : {
     804           14 :         *echo_buf_addr(ldata, ldata->echo_head) = c;
     805           14 :         smp_wmb(); /* Matches smp_rmb() in echo_buf(). */
     806           14 :         ldata->echo_head++;
     807           14 : }
     808              : 
     809              : /**
     810              :  * echo_move_back_col   -       add operation to move back a column
     811              :  * @ldata: n_tty data
     812              :  *
     813              :  * Add an operation to the echo buffer to move back one column.
     814              :  */
     815            0 : static void echo_move_back_col(struct n_tty_data *ldata)
     816              : {
     817            0 :         add_echo_byte(ECHO_OP_START, ldata);
     818            0 :         add_echo_byte(ECHO_OP_MOVE_BACK_COL, ldata);
     819            0 : }
     820              : 
     821              : /**
     822              :  * echo_set_canon_col   -       add operation to set the canon column
     823              :  * @ldata: n_tty data
     824              :  *
     825              :  * Add an operation to the echo buffer to set the canon column to the current
     826              :  * column.
     827              :  */
     828            2 : static void echo_set_canon_col(struct n_tty_data *ldata)
     829              : {
     830            2 :         add_echo_byte(ECHO_OP_START, ldata);
     831            2 :         add_echo_byte(ECHO_OP_SET_CANON_COL, ldata);
     832            2 : }
     833              : 
     834              : /**
     835              :  * echo_erase_tab       -       add operation to erase a tab
     836              :  * @num_chars: number of character columns already used
     837              :  * @after_tab: true if num_chars starts after a previous tab
     838              :  * @ldata: n_tty data
     839              :  *
     840              :  * Add an operation to the echo buffer to erase a tab.
     841              :  *
     842              :  * Called by the eraser function, which knows how many character columns have
     843              :  * been used since either a previous tab or the start of input. This
     844              :  * information will be used later, along with canon column (if applicable), to
     845              :  * go back the correct number of columns.
     846              :  */
     847            0 : static void echo_erase_tab(unsigned int num_chars, int after_tab,
     848              :                            struct n_tty_data *ldata)
     849              : {
     850            0 :         add_echo_byte(ECHO_OP_START, ldata);
     851            0 :         add_echo_byte(ECHO_OP_ERASE_TAB, ldata);
     852              : 
     853              :         /* We only need to know this modulo 8 (tab spacing) */
     854            0 :         num_chars &= 7;
     855              : 
     856              :         /* Set the high bit as a flag if num_chars is after a previous tab */
     857            0 :         if (after_tab)
     858            0 :                 num_chars |= 0x80;
     859              : 
     860            0 :         add_echo_byte(num_chars, ldata);
     861            0 : }
     862              : 
     863              : /**
     864              :  * echo_char_raw        -       echo a character raw
     865              :  * @c: unicode byte to echo
     866              :  * @ldata: line disc data
     867              :  *
     868              :  * Echo user input back onto the screen. This must be called only when
     869              :  * L_ECHO(tty) is true. Called from the &tty_driver.receive_buf() path.
     870              :  *
     871              :  * This variant does not treat control characters specially.
     872              :  */
     873            1 : static void echo_char_raw(u8 c, struct n_tty_data *ldata)
     874              : {
     875            1 :         if (c == ECHO_OP_START) {
     876            0 :                 add_echo_byte(ECHO_OP_START, ldata);
     877            0 :                 add_echo_byte(ECHO_OP_START, ldata);
     878            0 :         } else {
     879            1 :                 add_echo_byte(c, ldata);
     880              :         }
     881            1 : }
     882              : 
     883              : /**
     884              :  * echo_char            -       echo a character
     885              :  * @c: unicode byte to echo
     886              :  * @tty: terminal device
     887              :  *
     888              :  * Echo user input back onto the screen. This must be called only when
     889              :  * L_ECHO(tty) is true. Called from the &tty_driver.receive_buf() path.
     890              :  *
     891              :  * This variant tags control characters to be echoed as "^X" (where X is the
     892              :  * letter representing the control char).
     893              :  */
     894            8 : static void echo_char(u8 c, const struct tty_struct *tty)
     895              : {
     896            8 :         struct n_tty_data *ldata = tty->disc_data;
     897              : 
     898            8 :         if (c == ECHO_OP_START) {
     899            0 :                 add_echo_byte(ECHO_OP_START, ldata);
     900            0 :                 add_echo_byte(ECHO_OP_START, ldata);
     901            0 :         } else {
     902            8 :                 if (L_ECHOCTL(tty) && iscntrl(c) && c != '\t')
     903            1 :                         add_echo_byte(ECHO_OP_START, ldata);
     904            8 :                 add_echo_byte(c, ldata);
     905              :         }
     906            8 : }
     907              : 
     908              : /**
     909              :  * finish_erasing       -       complete erase
     910              :  * @ldata: n_tty data
     911              :  */
     912            8 : static inline void finish_erasing(struct n_tty_data *ldata)
     913              : {
     914            8 :         if (ldata->erasing) {
     915            0 :                 echo_char_raw('/', ldata);
     916            0 :                 ldata->erasing = 0;
     917            0 :         }
     918            8 : }
     919              : 
     920              : /**
     921              :  * eraser               -       handle erase function
     922              :  * @c: character input
     923              :  * @tty: terminal device
     924              :  *
     925              :  * Perform erase and necessary output when an erase character is present in the
     926              :  * stream from the driver layer. Handles the complexities of UTF-8 multibyte
     927              :  * symbols.
     928              :  *
     929              :  * Locking: n_tty_receive_buf()/producer path:
     930              :  *      caller holds non-exclusive %termios_rwsem
     931              :  */
     932            0 : static void eraser(u8 c, const struct tty_struct *tty)
     933              : {
     934            0 :         struct n_tty_data *ldata = tty->disc_data;
     935            0 :         enum { ERASE, WERASE, KILL } kill_type;
     936            0 :         size_t head;
     937            0 :         size_t cnt;
     938            0 :         int seen_alnums;
     939              : 
     940            0 :         if (ldata->read_head == ldata->canon_head) {
     941              :                 /* process_output('\a', tty); */ /* what do you think? */
     942            0 :                 return;
     943              :         }
     944            0 :         if (c == ERASE_CHAR(tty))
     945            0 :                 kill_type = ERASE;
     946            0 :         else if (c == WERASE_CHAR(tty))
     947            0 :                 kill_type = WERASE;
     948              :         else {
     949            0 :                 if (!L_ECHO(tty)) {
     950            0 :                         ldata->read_head = ldata->canon_head;
     951            0 :                         return;
     952              :                 }
     953            0 :                 if (!L_ECHOK(tty) || !L_ECHOKE(tty) || !L_ECHOE(tty)) {
     954            0 :                         ldata->read_head = ldata->canon_head;
     955            0 :                         finish_erasing(ldata);
     956            0 :                         echo_char(KILL_CHAR(tty), tty);
     957              :                         /* Add a newline if ECHOK is on and ECHOKE is off. */
     958            0 :                         if (L_ECHOK(tty))
     959            0 :                                 echo_char_raw('\n', ldata);
     960            0 :                         return;
     961              :                 }
     962            0 :                 kill_type = KILL;
     963              :         }
     964              : 
     965            0 :         seen_alnums = 0;
     966            0 :         while (MASK(ldata->read_head) != MASK(ldata->canon_head)) {
     967            0 :                 head = ldata->read_head;
     968              : 
     969              :                 /* erase a single possibly multibyte character */
     970            0 :                 do {
     971            0 :                         head--;
     972            0 :                         c = read_buf(ldata, head);
     973            0 :                 } while (is_continuation(c, tty) &&
     974            0 :                          MASK(head) != MASK(ldata->canon_head));
     975              : 
     976              :                 /* do not partially erase */
     977            0 :                 if (is_continuation(c, tty))
     978            0 :                         break;
     979              : 
     980            0 :                 if (kill_type == WERASE) {
     981              :                         /* Equivalent to BSD's ALTWERASE. */
     982            0 :                         if (isalnum(c) || c == '_')
     983            0 :                                 seen_alnums++;
     984            0 :                         else if (seen_alnums)
     985            0 :                                 break;
     986            0 :                 }
     987            0 :                 cnt = ldata->read_head - head;
     988            0 :                 ldata->read_head = head;
     989            0 :                 if (L_ECHO(tty)) {
     990            0 :                         if (L_ECHOPRT(tty)) {
     991            0 :                                 if (!ldata->erasing) {
     992            0 :                                         echo_char_raw('\\', ldata);
     993            0 :                                         ldata->erasing = 1;
     994            0 :                                 }
     995              :                                 /* if cnt > 1, output a multi-byte character */
     996            0 :                                 echo_char(c, tty);
     997            0 :                                 while (--cnt > 0) {
     998            0 :                                         head++;
     999            0 :                                         echo_char_raw(read_buf(ldata, head), ldata);
    1000            0 :                                         echo_move_back_col(ldata);
    1001              :                                 }
    1002            0 :                         } else if (kill_type == ERASE && !L_ECHOE(tty)) {
    1003            0 :                                 echo_char(ERASE_CHAR(tty), tty);
    1004            0 :                         } else if (c == '\t') {
    1005            0 :                                 unsigned int num_chars = 0;
    1006            0 :                                 int after_tab = 0;
    1007            0 :                                 size_t tail = ldata->read_head;
    1008              : 
    1009              :                                 /*
    1010              :                                  * Count the columns used for characters
    1011              :                                  * since the start of input or after a
    1012              :                                  * previous tab.
    1013              :                                  * This info is used to go back the correct
    1014              :                                  * number of columns.
    1015              :                                  */
    1016            0 :                                 while (MASK(tail) != MASK(ldata->canon_head)) {
    1017            0 :                                         tail--;
    1018            0 :                                         c = read_buf(ldata, tail);
    1019            0 :                                         if (c == '\t') {
    1020            0 :                                                 after_tab = 1;
    1021            0 :                                                 break;
    1022            0 :                                         } else if (iscntrl(c)) {
    1023            0 :                                                 if (L_ECHOCTL(tty))
    1024            0 :                                                         num_chars += 2;
    1025            0 :                                         } else if (!is_continuation(c, tty)) {
    1026            0 :                                                 num_chars++;
    1027            0 :                                         }
    1028              :                                 }
    1029            0 :                                 echo_erase_tab(num_chars, after_tab, ldata);
    1030            0 :                         } else {
    1031            0 :                                 if (iscntrl(c) && L_ECHOCTL(tty)) {
    1032            0 :                                         echo_char_raw('\b', ldata);
    1033            0 :                                         echo_char_raw(' ', ldata);
    1034            0 :                                         echo_char_raw('\b', ldata);
    1035            0 :                                 }
    1036            0 :                                 if (!iscntrl(c) || L_ECHOCTL(tty)) {
    1037            0 :                                         echo_char_raw('\b', ldata);
    1038            0 :                                         echo_char_raw(' ', ldata);
    1039            0 :                                         echo_char_raw('\b', ldata);
    1040            0 :                                 }
    1041              :                         }
    1042            0 :                 }
    1043            0 :                 if (kill_type == ERASE)
    1044            0 :                         break;
    1045              :         }
    1046            0 :         if (ldata->read_head == ldata->canon_head && L_ECHO(tty))
    1047            0 :                 finish_erasing(ldata);
    1048            0 : }
    1049              : 
    1050              : 
    1051            0 : static void __isig(int sig, struct tty_struct *tty)
    1052              : {
    1053            0 :         struct pid *tty_pgrp = tty_get_pgrp(tty);
    1054            0 :         if (tty_pgrp) {
    1055            0 :                 kill_pgrp(tty_pgrp, sig, 1);
    1056            0 :                 put_pid(tty_pgrp);
    1057            0 :         }
    1058            0 : }
    1059              : 
    1060              : /**
    1061              :  * isig                 -       handle the ISIG optio
    1062              :  * @sig: signal
    1063              :  * @tty: terminal
    1064              :  *
    1065              :  * Called when a signal is being sent due to terminal input. Called from the
    1066              :  * &tty_driver.receive_buf() path, so serialized.
    1067              :  *
    1068              :  * Performs input and output flush if !NOFLSH. In this context, the echo
    1069              :  * buffer is 'output'. The signal is processed first to alert any current
    1070              :  * readers or writers to discontinue and exit their i/o loops.
    1071              :  *
    1072              :  * Locking: %ctrl.lock
    1073              :  */
    1074            0 : static void isig(int sig, struct tty_struct *tty)
    1075              : {
    1076            0 :         struct n_tty_data *ldata = tty->disc_data;
    1077              : 
    1078            0 :         if (L_NOFLSH(tty)) {
    1079              :                 /* signal only */
    1080            0 :                 __isig(sig, tty);
    1081              : 
    1082            0 :         } else { /* signal and flush */
    1083            0 :                 up_read(&tty->termios_rwsem);
    1084            0 :                 down_write(&tty->termios_rwsem);
    1085              : 
    1086            0 :                 __isig(sig, tty);
    1087              : 
    1088              :                 /* clear echo buffer */
    1089            0 :                 mutex_lock(&ldata->output_lock);
    1090            0 :                 ldata->echo_head = ldata->echo_tail = 0;
    1091            0 :                 ldata->echo_mark = ldata->echo_commit = 0;
    1092            0 :                 mutex_unlock(&ldata->output_lock);
    1093              : 
    1094              :                 /* clear output buffer */
    1095            0 :                 tty_driver_flush_buffer(tty);
    1096              : 
    1097              :                 /* clear input buffer */
    1098            0 :                 reset_buffer_flags(tty->disc_data);
    1099              : 
    1100              :                 /* notify pty master of flush */
    1101            0 :                 if (tty->link)
    1102            0 :                         n_tty_packet_mode_flush(tty);
    1103              : 
    1104            0 :                 up_write(&tty->termios_rwsem);
    1105            0 :                 down_read(&tty->termios_rwsem);
    1106              :         }
    1107            0 : }
    1108              : 
    1109              : /**
    1110              :  * n_tty_receive_break  -       handle break
    1111              :  * @tty: terminal
    1112              :  *
    1113              :  * An RS232 break event has been hit in the incoming bitstream. This can cause
    1114              :  * a variety of events depending upon the termios settings.
    1115              :  *
    1116              :  * Locking: n_tty_receive_buf()/producer path:
    1117              :  *      caller holds non-exclusive termios_rwsem
    1118              :  *
    1119              :  * Note: may get exclusive %termios_rwsem if flushing input buffer
    1120              :  */
    1121            0 : static void n_tty_receive_break(struct tty_struct *tty)
    1122              : {
    1123            0 :         struct n_tty_data *ldata = tty->disc_data;
    1124              : 
    1125            0 :         if (I_IGNBRK(tty))
    1126            0 :                 return;
    1127            0 :         if (I_BRKINT(tty)) {
    1128            0 :                 isig(SIGINT, tty);
    1129            0 :                 return;
    1130              :         }
    1131            0 :         if (I_PARMRK(tty)) {
    1132            0 :                 put_tty_queue('\377', ldata);
    1133            0 :                 put_tty_queue('\0', ldata);
    1134            0 :         }
    1135            0 :         put_tty_queue('\0', ldata);
    1136            0 : }
    1137              : 
    1138              : /**
    1139              :  * n_tty_receive_overrun        -       handle overrun reporting
    1140              :  * @tty: terminal
    1141              :  *
    1142              :  * Data arrived faster than we could process it. While the tty driver has
    1143              :  * flagged this the bits that were missed are gone forever.
    1144              :  *
    1145              :  * Called from the receive_buf path so single threaded. Does not need locking
    1146              :  * as num_overrun and overrun_time are function private.
    1147              :  */
    1148            0 : static void n_tty_receive_overrun(const struct tty_struct *tty)
    1149              : {
    1150            0 :         struct n_tty_data *ldata = tty->disc_data;
    1151              : 
    1152            0 :         ldata->num_overrun++;
    1153            0 :         if (time_is_before_jiffies(ldata->overrun_time + HZ)) {
    1154            0 :                 tty_warn(tty, "%u input overrun(s)\n", ldata->num_overrun);
    1155            0 :                 ldata->overrun_time = jiffies;
    1156            0 :                 ldata->num_overrun = 0;
    1157            0 :         }
    1158            0 : }
    1159              : 
    1160              : /**
    1161              :  * n_tty_receive_parity_error   -       error notifier
    1162              :  * @tty: terminal device
    1163              :  * @c: character
    1164              :  *
    1165              :  * Process a parity error and queue the right data to indicate the error case
    1166              :  * if necessary.
    1167              :  *
    1168              :  * Locking: n_tty_receive_buf()/producer path:
    1169              :  *      caller holds non-exclusive %termios_rwsem
    1170              :  */
    1171            0 : static void n_tty_receive_parity_error(const struct tty_struct *tty,
    1172              :                                        u8 c)
    1173              : {
    1174            0 :         struct n_tty_data *ldata = tty->disc_data;
    1175              : 
    1176            0 :         if (I_INPCK(tty)) {
    1177            0 :                 if (I_IGNPAR(tty))
    1178            0 :                         return;
    1179            0 :                 if (I_PARMRK(tty)) {
    1180            0 :                         put_tty_queue('\377', ldata);
    1181            0 :                         put_tty_queue('\0', ldata);
    1182            0 :                         put_tty_queue(c, ldata);
    1183            0 :                 } else
    1184            0 :                         put_tty_queue('\0', ldata);
    1185            0 :         } else
    1186            0 :                 put_tty_queue(c, ldata);
    1187            0 : }
    1188              : 
    1189              : static void
    1190            0 : n_tty_receive_signal_char(struct tty_struct *tty, int signal, u8 c)
    1191              : {
    1192            0 :         isig(signal, tty);
    1193            0 :         if (I_IXON(tty))
    1194            0 :                 start_tty(tty);
    1195            0 :         if (L_ECHO(tty)) {
    1196            0 :                 echo_char(c, tty);
    1197            0 :                 commit_echoes(tty);
    1198            0 :         } else
    1199            0 :                 process_echoes(tty);
    1200            0 : }
    1201              : 
    1202            1 : static bool n_tty_is_char_flow_ctrl(struct tty_struct *tty, u8 c)
    1203              : {
    1204            1 :         return c == START_CHAR(tty) || c == STOP_CHAR(tty);
    1205              : }
    1206              : 
    1207              : /**
    1208              :  * n_tty_receive_char_flow_ctrl - receive flow control chars
    1209              :  * @tty: terminal device
    1210              :  * @c: character
    1211              :  * @lookahead_done: lookahead has processed this character already
    1212              :  *
    1213              :  * Receive and process flow control character actions.
    1214              :  *
    1215              :  * In case lookahead for flow control chars already handled the character in
    1216              :  * advance to the normal receive, the actions are skipped during normal
    1217              :  * receive.
    1218              :  *
    1219              :  * Returns true if @c is consumed as flow-control character, the character
    1220              :  * must not be treated as normal character.
    1221              :  */
    1222            1 : static bool n_tty_receive_char_flow_ctrl(struct tty_struct *tty, u8 c,
    1223              :                                          bool lookahead_done)
    1224              : {
    1225            1 :         if (!n_tty_is_char_flow_ctrl(tty, c))
    1226            1 :                 return false;
    1227              : 
    1228            0 :         if (lookahead_done)
    1229            0 :                 return true;
    1230              : 
    1231            0 :         if (c == START_CHAR(tty)) {
    1232            0 :                 start_tty(tty);
    1233            0 :                 process_echoes(tty);
    1234            0 :                 return true;
    1235              :         }
    1236              : 
    1237              :         /* STOP_CHAR */
    1238            0 :         stop_tty(tty);
    1239            0 :         return true;
    1240            1 : }
    1241              : 
    1242            1 : static void n_tty_receive_handle_newline(struct tty_struct *tty, u8 c)
    1243              : {
    1244            1 :         struct n_tty_data *ldata = tty->disc_data;
    1245              : 
    1246            1 :         set_bit(MASK(ldata->read_head), ldata->read_flags);
    1247            1 :         put_tty_queue(c, ldata);
    1248            1 :         smp_store_release(&ldata->canon_head, ldata->read_head);
    1249            1 :         kill_fasync(&tty->fasync, SIGIO, POLL_IN);
    1250            1 :         wake_up_interruptible_poll(&tty->read_wait, EPOLLIN | EPOLLRDNORM);
    1251            1 : }
    1252              : 
    1253            1 : static bool n_tty_receive_char_canon(struct tty_struct *tty, u8 c)
    1254              : {
    1255            1 :         struct n_tty_data *ldata = tty->disc_data;
    1256              : 
    1257            1 :         if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) ||
    1258            1 :             (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) {
    1259            0 :                 eraser(c, tty);
    1260            0 :                 commit_echoes(tty);
    1261              : 
    1262            0 :                 return true;
    1263              :         }
    1264              : 
    1265            1 :         if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) {
    1266            0 :                 ldata->lnext = 1;
    1267            0 :                 if (L_ECHO(tty)) {
    1268            0 :                         finish_erasing(ldata);
    1269            0 :                         if (L_ECHOCTL(tty)) {
    1270            0 :                                 echo_char_raw('^', ldata);
    1271            0 :                                 echo_char_raw('\b', ldata);
    1272            0 :                                 commit_echoes(tty);
    1273            0 :                         }
    1274            0 :                 }
    1275              : 
    1276            0 :                 return true;
    1277              :         }
    1278              : 
    1279            1 :         if (c == REPRINT_CHAR(tty) && L_ECHO(tty) && L_IEXTEN(tty)) {
    1280            0 :                 size_t tail = ldata->canon_head;
    1281              : 
    1282            0 :                 finish_erasing(ldata);
    1283            0 :                 echo_char(c, tty);
    1284            0 :                 echo_char_raw('\n', ldata);
    1285            0 :                 while (MASK(tail) != MASK(ldata->read_head)) {
    1286            0 :                         echo_char(read_buf(ldata, tail), tty);
    1287            0 :                         tail++;
    1288              :                 }
    1289            0 :                 commit_echoes(tty);
    1290              : 
    1291            0 :                 return true;
    1292            0 :         }
    1293              : 
    1294            1 :         if (c == '\n') {
    1295            1 :                 if (L_ECHO(tty) || L_ECHONL(tty)) {
    1296            1 :                         echo_char_raw('\n', ldata);
    1297            1 :                         commit_echoes(tty);
    1298            1 :                 }
    1299            1 :                 n_tty_receive_handle_newline(tty, c);
    1300              : 
    1301            1 :                 return true;
    1302              :         }
    1303              : 
    1304            0 :         if (c == EOF_CHAR(tty)) {
    1305            0 :                 c = __DISABLED_CHAR;
    1306            0 :                 n_tty_receive_handle_newline(tty, c);
    1307              : 
    1308            0 :                 return true;
    1309              :         }
    1310              : 
    1311            0 :         if ((c == EOL_CHAR(tty)) ||
    1312            0 :             (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) {
    1313              :                 /*
    1314              :                  * XXX are EOL_CHAR and EOL2_CHAR echoed?!?
    1315              :                  */
    1316            0 :                 if (L_ECHO(tty)) {
    1317              :                         /* Record the column of first canon char. */
    1318            0 :                         if (ldata->canon_head == ldata->read_head)
    1319            0 :                                 echo_set_canon_col(ldata);
    1320            0 :                         echo_char(c, tty);
    1321            0 :                         commit_echoes(tty);
    1322            0 :                 }
    1323              :                 /*
    1324              :                  * XXX does PARMRK doubling happen for
    1325              :                  * EOL_CHAR and EOL2_CHAR?
    1326              :                  */
    1327            0 :                 if (c == '\377' && I_PARMRK(tty))
    1328            0 :                         put_tty_queue(c, ldata);
    1329              : 
    1330            0 :                 n_tty_receive_handle_newline(tty, c);
    1331              : 
    1332            0 :                 return true;
    1333              :         }
    1334              : 
    1335            0 :         return false;
    1336            1 : }
    1337              : 
    1338            1 : static void n_tty_receive_char_special(struct tty_struct *tty, u8 c,
    1339              :                                        bool lookahead_done)
    1340              : {
    1341            1 :         struct n_tty_data *ldata = tty->disc_data;
    1342              : 
    1343            1 :         if (I_IXON(tty) && n_tty_receive_char_flow_ctrl(tty, c, lookahead_done))
    1344            0 :                 return;
    1345              : 
    1346            1 :         if (L_ISIG(tty)) {
    1347            1 :                 if (c == INTR_CHAR(tty)) {
    1348            0 :                         n_tty_receive_signal_char(tty, SIGINT, c);
    1349            0 :                         return;
    1350            1 :                 } else if (c == QUIT_CHAR(tty)) {
    1351            0 :                         n_tty_receive_signal_char(tty, SIGQUIT, c);
    1352            0 :                         return;
    1353            1 :                 } else if (c == SUSP_CHAR(tty)) {
    1354            0 :                         n_tty_receive_signal_char(tty, SIGTSTP, c);
    1355            0 :                         return;
    1356              :                 }
    1357            1 :         }
    1358              : 
    1359            1 :         if (tty->flow.stopped && !tty->flow.tco_stopped && I_IXON(tty) && I_IXANY(tty)) {
    1360            0 :                 start_tty(tty);
    1361            0 :                 process_echoes(tty);
    1362            0 :         }
    1363              : 
    1364            1 :         if (c == '\r') {
    1365            0 :                 if (I_IGNCR(tty))
    1366            0 :                         return;
    1367            0 :                 if (I_ICRNL(tty))
    1368            0 :                         c = '\n';
    1369            1 :         } else if (c == '\n' && I_INLCR(tty))
    1370            0 :                 c = '\r';
    1371              : 
    1372            1 :         if (ldata->icanon && n_tty_receive_char_canon(tty, c))
    1373            1 :                 return;
    1374              : 
    1375            0 :         if (L_ECHO(tty)) {
    1376            0 :                 finish_erasing(ldata);
    1377            0 :                 if (c == '\n')
    1378            0 :                         echo_char_raw('\n', ldata);
    1379              :                 else {
    1380              :                         /* Record the column of first canon char. */
    1381            0 :                         if (ldata->canon_head == ldata->read_head)
    1382            0 :                                 echo_set_canon_col(ldata);
    1383            0 :                         echo_char(c, tty);
    1384              :                 }
    1385            0 :                 commit_echoes(tty);
    1386            0 :         }
    1387              : 
    1388              :         /* PARMRK doubling check */
    1389            0 :         if (c == '\377' && I_PARMRK(tty))
    1390            0 :                 put_tty_queue(c, ldata);
    1391              : 
    1392            0 :         put_tty_queue(c, ldata);
    1393            1 : }
    1394              : 
    1395              : /**
    1396              :  * n_tty_receive_char   -       perform processing
    1397              :  * @tty: terminal device
    1398              :  * @c: character
    1399              :  *
    1400              :  * Process an individual character of input received from the driver.  This is
    1401              :  * serialized with respect to itself by the rules for the driver above.
    1402              :  *
    1403              :  * Locking: n_tty_receive_buf()/producer path:
    1404              :  *      caller holds non-exclusive %termios_rwsem
    1405              :  *      publishes canon_head if canonical mode is active
    1406              :  */
    1407            8 : static void n_tty_receive_char(struct tty_struct *tty, u8 c)
    1408              : {
    1409            8 :         struct n_tty_data *ldata = tty->disc_data;
    1410              : 
    1411            8 :         if (tty->flow.stopped && !tty->flow.tco_stopped && I_IXON(tty) && I_IXANY(tty)) {
    1412            0 :                 start_tty(tty);
    1413            0 :                 process_echoes(tty);
    1414            0 :         }
    1415            8 :         if (L_ECHO(tty)) {
    1416            8 :                 finish_erasing(ldata);
    1417              :                 /* Record the column of first canon char. */
    1418            8 :                 if (ldata->canon_head == ldata->read_head)
    1419            2 :                         echo_set_canon_col(ldata);
    1420            8 :                 echo_char(c, tty);
    1421            8 :                 commit_echoes(tty);
    1422            8 :         }
    1423              :         /* PARMRK doubling check */
    1424            8 :         if (c == '\377' && I_PARMRK(tty))
    1425            0 :                 put_tty_queue(c, ldata);
    1426            8 :         put_tty_queue(c, ldata);
    1427            8 : }
    1428              : 
    1429            0 : static void n_tty_receive_char_closing(struct tty_struct *tty, u8 c,
    1430              :                                        bool lookahead_done)
    1431              : {
    1432            0 :         if (I_ISTRIP(tty))
    1433            0 :                 c &= 0x7f;
    1434            0 :         if (I_IUCLC(tty) && L_IEXTEN(tty))
    1435            0 :                 c = tolower(c);
    1436              : 
    1437            0 :         if (I_IXON(tty)) {
    1438            0 :                 if (!n_tty_receive_char_flow_ctrl(tty, c, lookahead_done) &&
    1439            0 :                     tty->flow.stopped && !tty->flow.tco_stopped && I_IXANY(tty) &&
    1440            0 :                     c != INTR_CHAR(tty) && c != QUIT_CHAR(tty) &&
    1441            0 :                     c != SUSP_CHAR(tty)) {
    1442            0 :                         start_tty(tty);
    1443            0 :                         process_echoes(tty);
    1444            0 :                 }
    1445            0 :         }
    1446            0 : }
    1447              : 
    1448              : static void
    1449            0 : n_tty_receive_char_flagged(struct tty_struct *tty, u8 c, u8 flag)
    1450              : {
    1451            0 :         switch (flag) {
    1452              :         case TTY_BREAK:
    1453            0 :                 n_tty_receive_break(tty);
    1454            0 :                 break;
    1455              :         case TTY_PARITY:
    1456              :         case TTY_FRAME:
    1457            0 :                 n_tty_receive_parity_error(tty, c);
    1458            0 :                 break;
    1459              :         case TTY_OVERRUN:
    1460            0 :                 n_tty_receive_overrun(tty);
    1461            0 :                 break;
    1462              :         default:
    1463            0 :                 tty_err(tty, "unknown flag %u\n", flag);
    1464            0 :                 break;
    1465              :         }
    1466            0 : }
    1467              : 
    1468              : static void
    1469            0 : n_tty_receive_char_lnext(struct tty_struct *tty, u8 c, u8 flag)
    1470              : {
    1471            0 :         struct n_tty_data *ldata = tty->disc_data;
    1472              : 
    1473            0 :         ldata->lnext = 0;
    1474            0 :         if (likely(flag == TTY_NORMAL)) {
    1475            0 :                 if (I_ISTRIP(tty))
    1476            0 :                         c &= 0x7f;
    1477            0 :                 if (I_IUCLC(tty) && L_IEXTEN(tty))
    1478            0 :                         c = tolower(c);
    1479            0 :                 n_tty_receive_char(tty, c);
    1480            0 :         } else
    1481            0 :                 n_tty_receive_char_flagged(tty, c, flag);
    1482            0 : }
    1483              : 
    1484              : /* Caller must ensure count > 0 */
    1485            0 : static void n_tty_lookahead_flow_ctrl(struct tty_struct *tty, const u8 *cp,
    1486              :                                       const u8 *fp, size_t count)
    1487              : {
    1488            0 :         struct n_tty_data *ldata = tty->disc_data;
    1489            0 :         u8 flag = TTY_NORMAL;
    1490              : 
    1491            0 :         ldata->lookahead_count += count;
    1492              : 
    1493            0 :         if (!I_IXON(tty))
    1494            0 :                 return;
    1495              : 
    1496            0 :         while (count--) {
    1497            0 :                 if (fp)
    1498            0 :                         flag = *fp++;
    1499            0 :                 if (likely(flag == TTY_NORMAL))
    1500            0 :                         n_tty_receive_char_flow_ctrl(tty, *cp, false);
    1501            0 :                 cp++;
    1502              :         }
    1503            0 : }
    1504              : 
    1505              : static void
    1506           20 : n_tty_receive_buf_real_raw(const struct tty_struct *tty, const u8 *cp,
    1507              :                            size_t count)
    1508              : {
    1509           20 :         struct n_tty_data *ldata = tty->disc_data;
    1510              : 
    1511              :         /* handle buffer wrap-around by a loop */
    1512           60 :         for (unsigned int i = 0; i < 2; i++) {
    1513           40 :                 size_t head = MASK(ldata->read_head);
    1514           40 :                 size_t n = min(count, N_TTY_BUF_SIZE - head);
    1515              : 
    1516           40 :                 memcpy(read_buf_addr(ldata, head), cp, n);
    1517              : 
    1518           40 :                 ldata->read_head += n;
    1519           40 :                 cp += n;
    1520           40 :                 count -= n;
    1521           40 :         }
    1522           20 : }
    1523              : 
    1524              : static void
    1525            0 : n_tty_receive_buf_raw(struct tty_struct *tty, const u8 *cp, const u8 *fp,
    1526              :                       size_t count)
    1527              : {
    1528            0 :         struct n_tty_data *ldata = tty->disc_data;
    1529            0 :         u8 flag = TTY_NORMAL;
    1530              : 
    1531            0 :         while (count--) {
    1532            0 :                 if (fp)
    1533            0 :                         flag = *fp++;
    1534            0 :                 if (likely(flag == TTY_NORMAL))
    1535            0 :                         put_tty_queue(*cp++, ldata);
    1536              :                 else
    1537            0 :                         n_tty_receive_char_flagged(tty, *cp++, flag);
    1538              :         }
    1539            0 : }
    1540              : 
    1541              : static void
    1542            0 : n_tty_receive_buf_closing(struct tty_struct *tty, const u8 *cp, const u8 *fp,
    1543              :                           size_t count, bool lookahead_done)
    1544              : {
    1545            0 :         u8 flag = TTY_NORMAL;
    1546              : 
    1547            0 :         while (count--) {
    1548            0 :                 if (fp)
    1549            0 :                         flag = *fp++;
    1550            0 :                 if (likely(flag == TTY_NORMAL))
    1551            0 :                         n_tty_receive_char_closing(tty, *cp++, lookahead_done);
    1552              :         }
    1553            0 : }
    1554              : 
    1555            1 : static void n_tty_receive_buf_standard(struct tty_struct *tty, const u8 *cp,
    1556              :                                        const u8 *fp, size_t count,
    1557              :                                        bool lookahead_done)
    1558              : {
    1559            1 :         struct n_tty_data *ldata = tty->disc_data;
    1560            1 :         u8 flag = TTY_NORMAL;
    1561              : 
    1562           10 :         while (count--) {
    1563            9 :                 u8 c = *cp++;
    1564              : 
    1565            9 :                 if (fp)
    1566            0 :                         flag = *fp++;
    1567              : 
    1568            9 :                 if (ldata->lnext) {
    1569            0 :                         n_tty_receive_char_lnext(tty, c, flag);
    1570            0 :                         continue;
    1571              :                 }
    1572              : 
    1573            9 :                 if (unlikely(flag != TTY_NORMAL)) {
    1574            0 :                         n_tty_receive_char_flagged(tty, c, flag);
    1575            0 :                         continue;
    1576              :                 }
    1577              : 
    1578            9 :                 if (I_ISTRIP(tty))
    1579            0 :                         c &= 0x7f;
    1580            9 :                 if (I_IUCLC(tty) && L_IEXTEN(tty))
    1581            0 :                         c = tolower(c);
    1582            9 :                 if (L_EXTPROC(tty)) {
    1583            0 :                         put_tty_queue(c, ldata);
    1584            0 :                         continue;
    1585              :                 }
    1586              : 
    1587            9 :                 if (test_bit(c, ldata->char_map))
    1588            1 :                         n_tty_receive_char_special(tty, c, lookahead_done);
    1589              :                 else
    1590            8 :                         n_tty_receive_char(tty, c);
    1591            9 :         }
    1592            1 : }
    1593              : 
    1594           21 : static void __receive_buf(struct tty_struct *tty, const u8 *cp, const u8 *fp,
    1595              :                           size_t count)
    1596              : {
    1597           21 :         struct n_tty_data *ldata = tty->disc_data;
    1598           21 :         bool preops = I_ISTRIP(tty) || (I_IUCLC(tty) && L_IEXTEN(tty));
    1599           21 :         size_t la_count = min(ldata->lookahead_count, count);
    1600              : 
    1601           21 :         if (ldata->real_raw)
    1602           20 :                 n_tty_receive_buf_real_raw(tty, cp, count);
    1603            1 :         else if (ldata->raw || (L_EXTPROC(tty) && !preops))
    1604            0 :                 n_tty_receive_buf_raw(tty, cp, fp, count);
    1605            1 :         else if (tty->closing && !L_EXTPROC(tty)) {
    1606            0 :                 if (la_count > 0) {
    1607            0 :                         n_tty_receive_buf_closing(tty, cp, fp, la_count, true);
    1608            0 :                         cp += la_count;
    1609            0 :                         if (fp)
    1610            0 :                                 fp += la_count;
    1611            0 :                         count -= la_count;
    1612            0 :                 }
    1613            0 :                 if (count > 0)
    1614            0 :                         n_tty_receive_buf_closing(tty, cp, fp, count, false);
    1615            0 :         } else {
    1616            1 :                 if (la_count > 0) {
    1617            0 :                         n_tty_receive_buf_standard(tty, cp, fp, la_count, true);
    1618            0 :                         cp += la_count;
    1619            0 :                         if (fp)
    1620            0 :                                 fp += la_count;
    1621            0 :                         count -= la_count;
    1622            0 :                 }
    1623            1 :                 if (count > 0)
    1624            1 :                         n_tty_receive_buf_standard(tty, cp, fp, count, false);
    1625              : 
    1626            1 :                 flush_echoes(tty);
    1627            1 :                 if (tty->ops->flush_chars)
    1628            0 :                         tty->ops->flush_chars(tty);
    1629              :         }
    1630              : 
    1631           21 :         ldata->lookahead_count -= la_count;
    1632              : 
    1633           21 :         if (ldata->icanon && !L_EXTPROC(tty))
    1634            1 :                 return;
    1635              : 
    1636              :         /* publish read_head to consumer */
    1637           20 :         smp_store_release(&ldata->commit_head, ldata->read_head);
    1638              : 
    1639           20 :         if (read_cnt(ldata)) {
    1640           20 :                 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
    1641           20 :                 wake_up_interruptible_poll(&tty->read_wait, EPOLLIN | EPOLLRDNORM);
    1642           20 :         }
    1643           21 : }
    1644              : 
    1645              : /**
    1646              :  * n_tty_receive_buf_common     -       process input
    1647              :  * @tty: device to receive input
    1648              :  * @cp: input chars
    1649              :  * @fp: flags for each char (if %NULL, all chars are %TTY_NORMAL)
    1650              :  * @count: number of input chars in @cp
    1651              :  * @flow: enable flow control
    1652              :  *
    1653              :  * Called by the terminal driver when a block of characters has been received.
    1654              :  * This function must be called from soft contexts not from interrupt context.
    1655              :  * The driver is responsible for making calls one at a time and in order (or
    1656              :  * using flush_to_ldisc()).
    1657              :  *
    1658              :  * Returns: the # of input chars from @cp which were processed.
    1659              :  *
    1660              :  * In canonical mode, the maximum line length is 4096 chars (including the line
    1661              :  * termination char); lines longer than 4096 chars are truncated. After 4095
    1662              :  * chars, input data is still processed but not stored. Overflow processing
    1663              :  * ensures the tty can always receive more input until at least one line can be
    1664              :  * read.
    1665              :  *
    1666              :  * In non-canonical mode, the read buffer will only accept 4095 chars; this
    1667              :  * provides the necessary space for a newline char if the input mode is
    1668              :  * switched to canonical.
    1669              :  *
    1670              :  * Note it is possible for the read buffer to _contain_ 4096 chars in
    1671              :  * non-canonical mode: the read buffer could already contain the maximum canon
    1672              :  * line of 4096 chars when the mode is switched to non-canonical.
    1673              :  *
    1674              :  * Locking: n_tty_receive_buf()/producer path:
    1675              :  *      claims non-exclusive %termios_rwsem
    1676              :  *      publishes commit_head or canon_head
    1677              :  */
    1678              : static size_t
    1679           21 : n_tty_receive_buf_common(struct tty_struct *tty, const u8 *cp, const u8 *fp,
    1680              :                          size_t count, bool flow)
    1681              : {
    1682           21 :         struct n_tty_data *ldata = tty->disc_data;
    1683           21 :         size_t n, rcvd = 0;
    1684           21 :         int room, overflow;
    1685              : 
    1686           21 :         down_read(&tty->termios_rwsem);
    1687              : 
    1688           21 :         do {
    1689              :                 /*
    1690              :                  * When PARMRK is set, each input char may take up to 3 chars
    1691              :                  * in the read buf; reduce the buffer space avail by 3x
    1692              :                  *
    1693              :                  * If we are doing input canonicalization, and there are no
    1694              :                  * pending newlines, let characters through without limit, so
    1695              :                  * that erase characters will be handled.  Other excess
    1696              :                  * characters will be beeped.
    1697              :                  *
    1698              :                  * paired with store in *_copy_from_read_buf() -- guarantees
    1699              :                  * the consumer has loaded the data in read_buf up to the new
    1700              :                  * read_tail (so this producer will not overwrite unread data)
    1701              :                  */
    1702           42 :                 size_t tail = smp_load_acquire(&ldata->read_tail);
    1703              : 
    1704           42 :                 room = N_TTY_BUF_SIZE - (ldata->read_head - tail);
    1705           42 :                 if (I_PARMRK(tty))
    1706            0 :                         room = DIV_ROUND_UP(room, 3);
    1707           42 :                 room--;
    1708           42 :                 if (room <= 0) {
    1709            0 :                         overflow = ldata->icanon && ldata->canon_head == tail;
    1710            0 :                         if (overflow && room < 0)
    1711            0 :                                 ldata->read_head--;
    1712            0 :                         room = overflow;
    1713            0 :                         WRITE_ONCE(ldata->no_room, flow && !room);
    1714            0 :                 } else
    1715           42 :                         overflow = 0;
    1716              : 
    1717           42 :                 n = min_t(size_t, count, room);
    1718           42 :                 if (!n)
    1719           21 :                         break;
    1720              : 
    1721              :                 /* ignore parity errors if handling overflow */
    1722           21 :                 if (!overflow || !fp || *fp != TTY_PARITY)
    1723           21 :                         __receive_buf(tty, cp, fp, n);
    1724              : 
    1725           21 :                 cp += n;
    1726           21 :                 if (fp)
    1727            0 :                         fp += n;
    1728           21 :                 count -= n;
    1729           21 :                 rcvd += n;
    1730           42 :         } while (!test_bit(TTY_LDISC_CHANGING, &tty->flags));
    1731              : 
    1732           21 :         tty->receive_room = room;
    1733              : 
    1734              :         /* Unthrottle if handling overflow on pty */
    1735           21 :         if (tty->driver->type == TTY_DRIVER_TYPE_PTY) {
    1736           20 :                 if (overflow) {
    1737            0 :                         tty_set_flow_change(tty, TTY_UNTHROTTLE_SAFE);
    1738            0 :                         tty_unthrottle_safe(tty);
    1739            0 :                         __tty_set_flow_change(tty, 0);
    1740            0 :                 }
    1741           20 :         } else
    1742            1 :                 n_tty_check_throttle(tty);
    1743              : 
    1744           21 :         if (unlikely(ldata->no_room)) {
    1745              :                 /*
    1746              :                  * Barrier here is to ensure to read the latest read_tail in
    1747              :                  * chars_in_buffer() and to make sure that read_tail is not loaded
    1748              :                  * before ldata->no_room is set.
    1749              :                  */
    1750            0 :                 smp_mb();
    1751            0 :                 if (!chars_in_buffer(tty))
    1752            0 :                         n_tty_kick_worker(tty);
    1753            0 :         }
    1754              : 
    1755           21 :         up_read(&tty->termios_rwsem);
    1756              : 
    1757           42 :         return rcvd;
    1758           21 : }
    1759              : 
    1760            0 : static void n_tty_receive_buf(struct tty_struct *tty, const u8 *cp,
    1761              :                               const u8 *fp, size_t count)
    1762              : {
    1763            0 :         n_tty_receive_buf_common(tty, cp, fp, count, false);
    1764            0 : }
    1765              : 
    1766           21 : static size_t n_tty_receive_buf2(struct tty_struct *tty, const u8 *cp,
    1767              :                                  const u8 *fp, size_t count)
    1768              : {
    1769           21 :         return n_tty_receive_buf_common(tty, cp, fp, count, true);
    1770              : }
    1771              : 
    1772              : /**
    1773              :  * n_tty_set_termios    -       termios data changed
    1774              :  * @tty: terminal
    1775              :  * @old: previous data
    1776              :  *
    1777              :  * Called by the tty layer when the user changes termios flags so that the line
    1778              :  * discipline can plan ahead. This function cannot sleep and is protected from
    1779              :  * re-entry by the tty layer. The user is guaranteed that this function will
    1780              :  * not be re-entered or in progress when the ldisc is closed.
    1781              :  *
    1782              :  * Locking: Caller holds @tty->termios_rwsem
    1783              :  */
    1784           12 : static void n_tty_set_termios(struct tty_struct *tty, const struct ktermios *old)
    1785              : {
    1786           12 :         struct n_tty_data *ldata = tty->disc_data;
    1787              : 
    1788           12 :         if (!old || (old->c_lflag ^ tty->termios.c_lflag) & (ICANON | EXTPROC)) {
    1789           12 :                 bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE);
    1790           12 :                 ldata->line_start = ldata->read_tail;
    1791           12 :                 if (!L_ICANON(tty) || !read_cnt(ldata)) {
    1792           12 :                         ldata->canon_head = ldata->read_tail;
    1793           12 :                         ldata->push = 0;
    1794           12 :                 } else {
    1795            0 :                         set_bit(MASK(ldata->read_head - 1), ldata->read_flags);
    1796            0 :                         ldata->canon_head = ldata->read_head;
    1797            0 :                         ldata->push = 1;
    1798              :                 }
    1799           12 :                 ldata->commit_head = ldata->read_head;
    1800           12 :                 ldata->erasing = 0;
    1801           12 :                 ldata->lnext = 0;
    1802           12 :         }
    1803              : 
    1804           12 :         ldata->icanon = (L_ICANON(tty) != 0);
    1805              : 
    1806           12 :         if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) ||
    1807           12 :             I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) ||
    1808            0 :             I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) ||
    1809            0 :             I_PARMRK(tty)) {
    1810           12 :                 bitmap_zero(ldata->char_map, 256);
    1811              : 
    1812           12 :                 if (I_IGNCR(tty) || I_ICRNL(tty))
    1813           12 :                         set_bit('\r', ldata->char_map);
    1814           12 :                 if (I_INLCR(tty))
    1815            0 :                         set_bit('\n', ldata->char_map);
    1816              : 
    1817           12 :                 if (L_ICANON(tty)) {
    1818           12 :                         set_bit(ERASE_CHAR(tty), ldata->char_map);
    1819           12 :                         set_bit(KILL_CHAR(tty), ldata->char_map);
    1820           12 :                         set_bit(EOF_CHAR(tty), ldata->char_map);
    1821           12 :                         set_bit('\n', ldata->char_map);
    1822           12 :                         set_bit(EOL_CHAR(tty), ldata->char_map);
    1823           12 :                         if (L_IEXTEN(tty)) {
    1824           12 :                                 set_bit(WERASE_CHAR(tty), ldata->char_map);
    1825           12 :                                 set_bit(LNEXT_CHAR(tty), ldata->char_map);
    1826           12 :                                 set_bit(EOL2_CHAR(tty), ldata->char_map);
    1827           12 :                                 if (L_ECHO(tty))
    1828           12 :                                         set_bit(REPRINT_CHAR(tty),
    1829           12 :                                                 ldata->char_map);
    1830           12 :                         }
    1831           12 :                 }
    1832           12 :                 if (I_IXON(tty)) {
    1833           12 :                         set_bit(START_CHAR(tty), ldata->char_map);
    1834           12 :                         set_bit(STOP_CHAR(tty), ldata->char_map);
    1835           12 :                 }
    1836           12 :                 if (L_ISIG(tty)) {
    1837           12 :                         set_bit(INTR_CHAR(tty), ldata->char_map);
    1838           12 :                         set_bit(QUIT_CHAR(tty), ldata->char_map);
    1839           12 :                         set_bit(SUSP_CHAR(tty), ldata->char_map);
    1840           12 :                 }
    1841           12 :                 clear_bit(__DISABLED_CHAR, ldata->char_map);
    1842           12 :                 ldata->raw = 0;
    1843           12 :                 ldata->real_raw = 0;
    1844           12 :         } else {
    1845            0 :                 ldata->raw = 1;
    1846            0 :                 if ((I_IGNBRK(tty) || (!I_BRKINT(tty) && !I_PARMRK(tty))) &&
    1847            0 :                     (I_IGNPAR(tty) || !I_INPCK(tty)) &&
    1848            0 :                     (tty->driver->flags & TTY_DRIVER_REAL_RAW))
    1849            0 :                         ldata->real_raw = 1;
    1850              :                 else
    1851            0 :                         ldata->real_raw = 0;
    1852              :         }
    1853              :         /*
    1854              :          * Fix tty hang when I_IXON(tty) is cleared, but the tty
    1855              :          * been stopped by STOP_CHAR(tty) before it.
    1856              :          */
    1857           12 :         if (!I_IXON(tty) && old && (old->c_iflag & IXON) && !tty->flow.tco_stopped) {
    1858            0 :                 start_tty(tty);
    1859            0 :                 process_echoes(tty);
    1860            0 :         }
    1861              : 
    1862              :         /* The termios change make the tty ready for I/O */
    1863           12 :         wake_up_interruptible(&tty->write_wait);
    1864           12 :         wake_up_interruptible(&tty->read_wait);
    1865           12 : }
    1866              : 
    1867              : /**
    1868              :  * n_tty_close          -       close the ldisc for this tty
    1869              :  * @tty: device
    1870              :  *
    1871              :  * Called from the terminal layer when this line discipline is being shut down,
    1872              :  * either because of a close or becsuse of a discipline change. The function
    1873              :  * will not be called while other ldisc methods are in progress.
    1874              :  */
    1875            6 : static void n_tty_close(struct tty_struct *tty)
    1876              : {
    1877            6 :         struct n_tty_data *ldata = tty->disc_data;
    1878              : 
    1879            6 :         if (tty->link)
    1880            0 :                 n_tty_packet_mode_flush(tty);
    1881              : 
    1882            6 :         down_write(&tty->termios_rwsem);
    1883            6 :         vfree(ldata);
    1884            6 :         tty->disc_data = NULL;
    1885            6 :         up_write(&tty->termios_rwsem);
    1886            6 : }
    1887              : 
    1888              : /**
    1889              :  * n_tty_open           -       open an ldisc
    1890              :  * @tty: terminal to open
    1891              :  *
    1892              :  * Called when this line discipline is being attached to the terminal device.
    1893              :  * Can sleep. Called serialized so that no other events will occur in parallel.
    1894              :  * No further open will occur until a close.
    1895              :  */
    1896           12 : static int n_tty_open(struct tty_struct *tty)
    1897              : {
    1898           12 :         struct n_tty_data *ldata;
    1899              : 
    1900              :         /* Currently a malloc failure here can panic */
    1901           12 :         ldata = vzalloc(sizeof(*ldata));
    1902           12 :         if (!ldata)
    1903            0 :                 return -ENOMEM;
    1904              : 
    1905           12 :         ldata->overrun_time = jiffies;
    1906           12 :         mutex_init(&ldata->atomic_read_lock);
    1907           12 :         mutex_init(&ldata->output_lock);
    1908              : 
    1909           12 :         tty->disc_data = ldata;
    1910           12 :         tty->closing = 0;
    1911              :         /* indicate buffer work may resume */
    1912           12 :         clear_bit(TTY_LDISC_HALTED, &tty->flags);
    1913           12 :         n_tty_set_termios(tty, NULL);
    1914           12 :         tty_unthrottle(tty);
    1915           12 :         return 0;
    1916           12 : }
    1917              : 
    1918          172 : static inline int input_available_p(const struct tty_struct *tty, int poll)
    1919              : {
    1920          172 :         const struct n_tty_data *ldata = tty->disc_data;
    1921          172 :         int amt = poll && !TIME_CHAR(tty) && MIN_CHAR(tty) ? MIN_CHAR(tty) : 1;
    1922              : 
    1923          172 :         if (ldata->icanon && !L_EXTPROC(tty))
    1924            0 :                 return ldata->canon_head != ldata->read_tail;
    1925              :         else
    1926          172 :                 return ldata->commit_head - ldata->read_tail >= amt;
    1927          172 : }
    1928              : 
    1929              : /**
    1930              :  * copy_from_read_buf   -       copy read data directly
    1931              :  * @tty: terminal device
    1932              :  * @kbp: data
    1933              :  * @nr: size of data
    1934              :  *
    1935              :  * Helper function to speed up n_tty_read(). It is only called when %ICANON is
    1936              :  * off; it copies characters straight from the tty queue.
    1937              :  *
    1938              :  * Returns: true if it successfully copied data, but there is still more data
    1939              :  * to be had.
    1940              :  *
    1941              :  * Locking:
    1942              :  *  * called under the @ldata->atomic_read_lock sem
    1943              :  *  * n_tty_read()/consumer path:
    1944              :  *              caller holds non-exclusive %termios_rwsem;
    1945              :  *              read_tail published
    1946              :  */
    1947           25 : static bool copy_from_read_buf(const struct tty_struct *tty, u8 **kbp,
    1948              :                                size_t *nr)
    1949              : 
    1950              : {
    1951           25 :         struct n_tty_data *ldata = tty->disc_data;
    1952           25 :         size_t n;
    1953           25 :         bool is_eof;
    1954           25 :         size_t head = smp_load_acquire(&ldata->commit_head);
    1955           25 :         size_t tail = MASK(ldata->read_tail);
    1956              : 
    1957           25 :         n = min3(head - ldata->read_tail, N_TTY_BUF_SIZE - tail, *nr);
    1958           25 :         if (!n)
    1959            0 :                 return false;
    1960              : 
    1961           25 :         u8 *from = read_buf_addr(ldata, tail);
    1962           25 :         memcpy(*kbp, from, n);
    1963           25 :         is_eof = n == 1 && *from == EOF_CHAR(tty);
    1964           25 :         tty_audit_add_data(tty, from, n);
    1965           25 :         zero_buffer(tty, from, n);
    1966           25 :         smp_store_release(&ldata->read_tail, ldata->read_tail + n);
    1967              : 
    1968              :         /* Turn single EOF into zero-length read */
    1969           25 :         if (L_EXTPROC(tty) && ldata->icanon && is_eof &&
    1970            0 :             head == ldata->read_tail)
    1971            0 :                 return false;
    1972              : 
    1973           25 :         *kbp += n;
    1974           25 :         *nr -= n;
    1975              : 
    1976              :         /* If we have more to copy, let the caller know */
    1977           25 :         return head != ldata->read_tail;
    1978           25 : }
    1979              : 
    1980              : /**
    1981              :  * canon_copy_from_read_buf     -       copy read data in canonical mode
    1982              :  * @tty: terminal device
    1983              :  * @kbp: data
    1984              :  * @nr: size of data
    1985              :  *
    1986              :  * Helper function for n_tty_read(). It is only called when %ICANON is on; it
    1987              :  * copies one line of input up to and including the line-delimiting character
    1988              :  * into the result buffer.
    1989              :  *
    1990              :  * Note: When termios is changed from non-canonical to canonical mode and the
    1991              :  * read buffer contains data, n_tty_set_termios() simulates an EOF push (as if
    1992              :  * C-d were input) _without_ the %DISABLED_CHAR in the buffer. This causes data
    1993              :  * already processed as input to be immediately available as input although a
    1994              :  * newline has not been received.
    1995              :  *
    1996              :  * Locking:
    1997              :  *  * called under the %atomic_read_lock mutex
    1998              :  *  * n_tty_read()/consumer path:
    1999              :  *      caller holds non-exclusive %termios_rwsem;
    2000              :  *      read_tail published
    2001              :  */
    2002            0 : static bool canon_copy_from_read_buf(const struct tty_struct *tty, u8 **kbp,
    2003              :                                      size_t *nr)
    2004              : {
    2005            0 :         struct n_tty_data *ldata = tty->disc_data;
    2006            0 :         size_t n, size, more, c;
    2007            0 :         size_t eol;
    2008            0 :         size_t tail, canon_head;
    2009            0 :         int found = 0;
    2010              : 
    2011              :         /* N.B. avoid overrun if nr == 0 */
    2012            0 :         if (!*nr)
    2013            0 :                 return false;
    2014              : 
    2015            0 :         canon_head = smp_load_acquire(&ldata->canon_head);
    2016            0 :         n = min(*nr, canon_head - ldata->read_tail);
    2017              : 
    2018            0 :         tail = MASK(ldata->read_tail);
    2019            0 :         size = min_t(size_t, tail + n, N_TTY_BUF_SIZE);
    2020              : 
    2021            0 :         eol = find_next_bit(ldata->read_flags, size, tail);
    2022            0 :         more = n - (size - tail);
    2023            0 :         if (eol == N_TTY_BUF_SIZE && more) {
    2024              :                 /* scan wrapped without finding set bit */
    2025            0 :                 eol = find_first_bit(ldata->read_flags, more);
    2026            0 :                 found = eol != more;
    2027            0 :         } else
    2028            0 :                 found = eol != size;
    2029              : 
    2030            0 :         n = eol - tail;
    2031            0 :         if (n > N_TTY_BUF_SIZE)
    2032            0 :                 n += N_TTY_BUF_SIZE;
    2033            0 :         c = n + found;
    2034              : 
    2035            0 :         if (!found || read_buf(ldata, eol) != __DISABLED_CHAR)
    2036            0 :                 n = c;
    2037              : 
    2038            0 :         tty_copy(tty, *kbp, tail, n);
    2039            0 :         *kbp += n;
    2040            0 :         *nr -= n;
    2041              : 
    2042            0 :         if (found)
    2043            0 :                 clear_bit(eol, ldata->read_flags);
    2044            0 :         smp_store_release(&ldata->read_tail, ldata->read_tail + c);
    2045              : 
    2046            0 :         if (found) {
    2047            0 :                 if (!ldata->push)
    2048            0 :                         ldata->line_start = ldata->read_tail;
    2049              :                 else
    2050            0 :                         ldata->push = 0;
    2051            0 :                 tty_audit_push();
    2052            0 :                 return false;
    2053              :         }
    2054              : 
    2055              :         /* No EOL found - do a continuation retry if there is more data */
    2056            0 :         return ldata->read_tail != canon_head;
    2057            0 : }
    2058              : 
    2059              : /*
    2060              :  * If we finished a read at the exact location of an
    2061              :  * EOF (special EOL character that's a __DISABLED_CHAR)
    2062              :  * in the stream, silently eat the EOF.
    2063              :  */
    2064            0 : static void canon_skip_eof(struct n_tty_data *ldata)
    2065              : {
    2066            0 :         size_t tail, canon_head;
    2067              : 
    2068            0 :         canon_head = smp_load_acquire(&ldata->canon_head);
    2069            0 :         tail = ldata->read_tail;
    2070              : 
    2071              :         // No data?
    2072            0 :         if (tail == canon_head)
    2073            0 :                 return;
    2074              : 
    2075              :         // See if the tail position is EOF in the circular buffer
    2076            0 :         tail &= (N_TTY_BUF_SIZE - 1);
    2077            0 :         if (!test_bit(tail, ldata->read_flags))
    2078            0 :                 return;
    2079            0 :         if (read_buf(ldata, tail) != __DISABLED_CHAR)
    2080            0 :                 return;
    2081              : 
    2082              :         // Clear the EOL bit, skip the EOF char.
    2083            0 :         clear_bit(tail, ldata->read_flags);
    2084            0 :         smp_store_release(&ldata->read_tail, ldata->read_tail + 1);
    2085            0 : }
    2086              : 
    2087              : /**
    2088              :  * job_control          -       check job control
    2089              :  * @tty: tty
    2090              :  * @file: file handle
    2091              :  *
    2092              :  * Perform job control management checks on this @file/@tty descriptor and if
    2093              :  * appropriate send any needed signals and return a negative error code if
    2094              :  * action should be taken.
    2095              :  *
    2096              :  * Locking:
    2097              :  *  * redirected write test is safe
    2098              :  *  * current->signal->tty check is safe
    2099              :  *  * ctrl.lock to safely reference @tty->ctrl.pgrp
    2100              :  */
    2101           12 : static int job_control(struct tty_struct *tty, struct file *file)
    2102              : {
    2103              :         /* Job control check -- must be done at start and after
    2104              :            every sleep (POSIX.1 7.1.1.4). */
    2105              :         /* NOTE: not yet done after every sleep pending a thorough
    2106              :            check of the logic of this change. -- jlc */
    2107              :         /* don't stop on /dev/console */
    2108           12 :         if (file->f_op->write_iter == redirected_tty_write)
    2109            0 :                 return 0;
    2110              : 
    2111           12 :         return __tty_check_change(tty, SIGTTIN);
    2112           12 : }
    2113              : 
    2114              : /*
    2115              :  * We still hold the atomic_read_lock and the termios_rwsem, and can just
    2116              :  * continue to copy data.
    2117              :  */
    2118           13 : static ssize_t n_tty_continue_cookie(struct tty_struct *tty, u8 *kbuf,
    2119              :                                    size_t nr, void **cookie)
    2120              : {
    2121           13 :         struct n_tty_data *ldata = tty->disc_data;
    2122           13 :         u8 *kb = kbuf;
    2123              : 
    2124           13 :         if (ldata->icanon && !L_EXTPROC(tty)) {
    2125              :                 /*
    2126              :                  * If we have filled the user buffer, see if we should skip an
    2127              :                  * EOF character before releasing the lock and returning done.
    2128              :                  */
    2129            0 :                 if (!nr)
    2130            0 :                         canon_skip_eof(ldata);
    2131            0 :                 else if (canon_copy_from_read_buf(tty, &kb, &nr))
    2132            0 :                         return kb - kbuf;
    2133            0 :         } else {
    2134           13 :                 if (copy_from_read_buf(tty, &kb, &nr))
    2135           10 :                         return kb - kbuf;
    2136              :         }
    2137              : 
    2138              :         /* No more data - release locks and stop retries */
    2139            3 :         n_tty_kick_worker(tty);
    2140            3 :         n_tty_check_unthrottle(tty);
    2141            3 :         up_read(&tty->termios_rwsem);
    2142            3 :         mutex_unlock(&ldata->atomic_read_lock);
    2143            3 :         *cookie = NULL;
    2144              : 
    2145            3 :         return kb - kbuf;
    2146           13 : }
    2147              : 
    2148            0 : static int n_tty_wait_for_input(struct tty_struct *tty, struct file *file,
    2149              :                                 struct wait_queue_entry *wait, long *timeout)
    2150              : {
    2151            0 :         if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
    2152            0 :                 return -EIO;
    2153            0 :         if (tty_hung_up_p(file))
    2154            0 :                 return 0;
    2155              :         /*
    2156              :          * Abort readers for ttys which never actually get hung up.
    2157              :          * See __tty_hangup().
    2158              :          */
    2159            0 :         if (test_bit(TTY_HUPPING, &tty->flags))
    2160            0 :                 return 0;
    2161            0 :         if (!*timeout)
    2162            0 :                 return 0;
    2163            0 :         if (tty_io_nonblock(tty, file))
    2164            0 :                 return -EAGAIN;
    2165            0 :         if (signal_pending(current))
    2166            0 :                 return -ERESTARTSYS;
    2167              : 
    2168            0 :         up_read(&tty->termios_rwsem);
    2169            0 :         *timeout = wait_woken(wait, TASK_INTERRUPTIBLE, *timeout);
    2170            0 :         down_read(&tty->termios_rwsem);
    2171              : 
    2172            0 :         return 1;
    2173            0 : }
    2174              : 
    2175              : /**
    2176              :  * n_tty_read           -       read function for tty
    2177              :  * @tty: tty device
    2178              :  * @file: file object
    2179              :  * @kbuf: kernelspace buffer pointer
    2180              :  * @nr: size of I/O
    2181              :  * @cookie: if non-%NULL, this is a continuation read
    2182              :  * @offset: where to continue reading from (unused in n_tty)
    2183              :  *
    2184              :  * Perform reads for the line discipline. We are guaranteed that the line
    2185              :  * discipline will not be closed under us but we may get multiple parallel
    2186              :  * readers and must handle this ourselves. We may also get a hangup. Always
    2187              :  * called in user context, may sleep.
    2188              :  *
    2189              :  * This code must be sure never to sleep through a hangup.
    2190              :  *
    2191              :  * Locking: n_tty_read()/consumer path:
    2192              :  *      claims non-exclusive termios_rwsem;
    2193              :  *      publishes read_tail
    2194              :  */
    2195           25 : static ssize_t n_tty_read(struct tty_struct *tty, struct file *file, u8 *kbuf,
    2196              :                           size_t nr, void **cookie, unsigned long offset)
    2197              : {
    2198           25 :         struct n_tty_data *ldata = tty->disc_data;
    2199           25 :         u8 *kb = kbuf;
    2200           25 :         DEFINE_WAIT_FUNC(wait, woken_wake_function);
    2201           25 :         int minimum, time;
    2202           25 :         ssize_t retval;
    2203           25 :         long timeout;
    2204           25 :         bool packet;
    2205           25 :         size_t old_tail;
    2206              : 
    2207              :         /* Is this a continuation of a read started earlier? */
    2208           25 :         if (*cookie)
    2209           13 :                 return n_tty_continue_cookie(tty, kbuf, nr, cookie);
    2210              : 
    2211           12 :         retval = job_control(tty, file);
    2212           12 :         if (retval < 0)
    2213            0 :                 return retval;
    2214              : 
    2215              :         /*
    2216              :          *      Internal serialization of reads.
    2217              :          */
    2218           12 :         if (file->f_flags & O_NONBLOCK) {
    2219           12 :                 if (!mutex_trylock(&ldata->atomic_read_lock))
    2220            0 :                         return -EAGAIN;
    2221           12 :         } else {
    2222            0 :                 if (mutex_lock_interruptible(&ldata->atomic_read_lock))
    2223            0 :                         return -ERESTARTSYS;
    2224              :         }
    2225              : 
    2226           12 :         down_read(&tty->termios_rwsem);
    2227              : 
    2228           12 :         minimum = time = 0;
    2229           12 :         timeout = MAX_SCHEDULE_TIMEOUT;
    2230           12 :         if (!ldata->icanon) {
    2231           12 :                 minimum = MIN_CHAR(tty);
    2232           12 :                 if (minimum) {
    2233           12 :                         time = (HZ / 10) * TIME_CHAR(tty);
    2234           12 :                 } else {
    2235            0 :                         timeout = (HZ / 10) * TIME_CHAR(tty);
    2236            0 :                         minimum = 1;
    2237              :                 }
    2238           12 :         }
    2239              : 
    2240           12 :         packet = tty->ctrl.packet;
    2241           12 :         old_tail = ldata->read_tail;
    2242              : 
    2243           12 :         add_wait_queue(&tty->read_wait, &wait);
    2244           12 :         while (nr) {
    2245              :                 /* First test for status change. */
    2246           12 :                 if (packet && tty->link->ctrl.pktstatus) {
    2247            0 :                         u8 cs;
    2248            0 :                         if (kb != kbuf)
    2249            0 :                                 break;
    2250            0 :                         spin_lock_irq(&tty->link->ctrl.lock);
    2251            0 :                         cs = tty->link->ctrl.pktstatus;
    2252            0 :                         tty->link->ctrl.pktstatus = 0;
    2253            0 :                         spin_unlock_irq(&tty->link->ctrl.lock);
    2254            0 :                         *kb++ = cs;
    2255            0 :                         nr--;
    2256            0 :                         break;
    2257            0 :                 }
    2258              : 
    2259           12 :                 if (!input_available_p(tty, 0)) {
    2260            0 :                         up_read(&tty->termios_rwsem);
    2261            0 :                         tty_buffer_flush_work(tty->port);
    2262            0 :                         down_read(&tty->termios_rwsem);
    2263            0 :                         if (!input_available_p(tty, 0)) {
    2264            0 :                                 int ret = n_tty_wait_for_input(tty, file, &wait,
    2265              :                                                                &timeout);
    2266            0 :                                 if (ret <= 0) {
    2267            0 :                                         retval = ret;
    2268            0 :                                         break;
    2269              :                                 }
    2270            0 :                                 continue;
    2271            0 :                         }
    2272            0 :                 }
    2273              : 
    2274           12 :                 if (ldata->icanon && !L_EXTPROC(tty)) {
    2275            0 :                         if (canon_copy_from_read_buf(tty, &kb, &nr))
    2276            0 :                                 goto more_to_be_read;
    2277            0 :                 } else {
    2278              :                         /* Deal with packet mode. */
    2279           12 :                         if (packet && kb == kbuf) {
    2280            0 :                                 *kb++ = TIOCPKT_DATA;
    2281            0 :                                 nr--;
    2282            0 :                         }
    2283              : 
    2284           12 :                         if (copy_from_read_buf(tty, &kb, &nr) && kb - kbuf >= minimum)
    2285            3 :                                 goto more_to_be_read;
    2286              :                 }
    2287              : 
    2288            9 :                 n_tty_check_unthrottle(tty);
    2289              : 
    2290            9 :                 if (kb - kbuf >= minimum)
    2291            9 :                         break;
    2292            0 :                 if (time)
    2293            0 :                         timeout = time;
    2294              :         }
    2295            9 :         if (old_tail != ldata->read_tail) {
    2296              :                 /*
    2297              :                  * Make sure no_room is not read in n_tty_kick_worker()
    2298              :                  * before setting ldata->read_tail in copy_from_read_buf().
    2299              :                  */
    2300            9 :                 smp_mb();
    2301            9 :                 n_tty_kick_worker(tty);
    2302            9 :         }
    2303            9 :         up_read(&tty->termios_rwsem);
    2304              : 
    2305            9 :         remove_wait_queue(&tty->read_wait, &wait);
    2306            9 :         mutex_unlock(&ldata->atomic_read_lock);
    2307              : 
    2308            9 :         if (kb - kbuf)
    2309            9 :                 retval = kb - kbuf;
    2310              : 
    2311            9 :         return retval;
    2312              : more_to_be_read:
    2313              :         /*
    2314              :          * There is more to be had and we have nothing more to wait for, so
    2315              :          * let's mark us for retries.
    2316              :          *
    2317              :          * NOTE! We return here with both the termios_sem and atomic_read_lock
    2318              :          * still held, the retries will release them when done.
    2319              :          */
    2320            3 :         remove_wait_queue(&tty->read_wait, &wait);
    2321            3 :         *cookie = cookie;
    2322              : 
    2323            3 :         return kb - kbuf;
    2324           25 : }
    2325              : 
    2326              : /**
    2327              :  * n_tty_write          -       write function for tty
    2328              :  * @tty: tty device
    2329              :  * @file: file object
    2330              :  * @buf: userspace buffer pointer
    2331              :  * @nr: size of I/O
    2332              :  *
    2333              :  * Write function of the terminal device. This is serialized with respect to
    2334              :  * other write callers but not to termios changes, reads and other such events.
    2335              :  * Since the receive code will echo characters, thus calling driver write
    2336              :  * methods, the %output_lock is used in the output processing functions called
    2337              :  * here as well as in the echo processing function to protect the column state
    2338              :  * and space left in the buffer.
    2339              :  *
    2340              :  * This code must be sure never to sleep through a hangup.
    2341              :  *
    2342              :  * Locking: output_lock to protect column state and space left
    2343              :  *       (note that the process_output*() functions take this lock themselves)
    2344              :  */
    2345              : 
    2346           44 : static ssize_t n_tty_write(struct tty_struct *tty, struct file *file,
    2347              :                            const u8 *buf, size_t nr)
    2348              : {
    2349           44 :         const u8 *b = buf;
    2350           44 :         DEFINE_WAIT_FUNC(wait, woken_wake_function);
    2351           44 :         ssize_t num, retval = 0;
    2352              : 
    2353              :         /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */
    2354           44 :         if (L_TOSTOP(tty) && file->f_op->write_iter != redirected_tty_write) {
    2355            0 :                 retval = tty_check_change(tty);
    2356            0 :                 if (retval)
    2357            0 :                         return retval;
    2358            0 :         }
    2359              : 
    2360           44 :         down_read(&tty->termios_rwsem);
    2361              : 
    2362              :         /* Write out any echoed characters that are still pending */
    2363           44 :         process_echoes(tty);
    2364              : 
    2365           44 :         add_wait_queue(&tty->write_wait, &wait);
    2366           90 :         while (1) {
    2367           45 :                 if (signal_pending(current)) {
    2368            0 :                         retval = -ERESTARTSYS;
    2369            0 :                         break;
    2370              :                 }
    2371           45 :                 if (tty_hung_up_p(file) || (tty->link && !tty->link->count)) {
    2372            2 :                         retval = -EIO;
    2373            2 :                         break;
    2374              :                 }
    2375           45 :                 if (O_OPOST(tty)) {
    2376           58 :                         while (nr > 0) {
    2377           31 :                                 num = process_output_block(tty, b, nr);
    2378           31 :                                 if (num < 0) {
    2379            0 :                                         if (num == -EAGAIN)
    2380            0 :                                                 break;
    2381            0 :                                         retval = num;
    2382            0 :                                         goto break_out;
    2383              :                                 }
    2384           31 :                                 b += num;
    2385           31 :                                 nr -= num;
    2386           31 :                                 if (nr == 0)
    2387            4 :                                         break;
    2388           27 :                                 if (process_output(*b, tty) < 0)
    2389            0 :                                         break;
    2390           27 :                                 b++; nr--;
    2391              :                         }
    2392           31 :                         if (tty->ops->flush_chars)
    2393            0 :                                 tty->ops->flush_chars(tty);
    2394           31 :                 } else {
    2395           14 :                         struct n_tty_data *ldata = tty->disc_data;
    2396              : 
    2397           28 :                         while (nr > 0) {
    2398           14 :                                 mutex_lock(&ldata->output_lock);
    2399           14 :                                 num = tty->ops->write(tty, b, nr);
    2400           14 :                                 mutex_unlock(&ldata->output_lock);
    2401           14 :                                 if (num < 0) {
    2402            0 :                                         retval = num;
    2403            0 :                                         goto break_out;
    2404              :                                 }
    2405           14 :                                 if (!num)
    2406            0 :                                         break;
    2407           14 :                                 b += num;
    2408           14 :                                 nr -= num;
    2409              :                         }
    2410           14 :                 }
    2411           46 :                 if (!nr)
    2412           45 :                         break;
    2413            1 :                 if (tty_io_nonblock(tty, file)) {
    2414            0 :                         retval = -EAGAIN;
    2415            0 :                         break;
    2416              :                 }
    2417            1 :                 up_read(&tty->termios_rwsem);
    2418              : 
    2419            1 :                 wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
    2420              : 
    2421            1 :                 down_read(&tty->termios_rwsem);
    2422              :         }
    2423              : break_out:
    2424           45 :         remove_wait_queue(&tty->write_wait, &wait);
    2425           45 :         if (nr && tty->fasync)
    2426            0 :                 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
    2427           45 :         up_read(&tty->termios_rwsem);
    2428           45 :         return (b - buf) ? b - buf : retval;
    2429           46 : }
    2430              : 
    2431              : /**
    2432              :  * n_tty_poll           -       poll method for N_TTY
    2433              :  * @tty: terminal device
    2434              :  * @file: file accessing it
    2435              :  * @wait: poll table
    2436              :  *
    2437              :  * Called when the line discipline is asked to poll() for data or for special
    2438              :  * events. This code is not serialized with respect to other events save
    2439              :  * open/close.
    2440              :  *
    2441              :  * This code must be sure never to sleep through a hangup.
    2442              :  *
    2443              :  * Locking: called without the kernel lock held -- fine.
    2444              :  */
    2445           86 : static __poll_t n_tty_poll(struct tty_struct *tty, struct file *file,
    2446              :                                                         poll_table *wait)
    2447              : {
    2448           86 :         __poll_t mask = 0;
    2449              : 
    2450           86 :         poll_wait(file, &tty->read_wait, wait);
    2451           86 :         poll_wait(file, &tty->write_wait, wait);
    2452           86 :         if (input_available_p(tty, 1))
    2453           12 :                 mask |= EPOLLIN | EPOLLRDNORM;
    2454              :         else {
    2455           74 :                 tty_buffer_flush_work(tty->port);
    2456           74 :                 if (input_available_p(tty, 1))
    2457            0 :                         mask |= EPOLLIN | EPOLLRDNORM;
    2458              :         }
    2459           86 :         if (tty->ctrl.packet && tty->link->ctrl.pktstatus)
    2460            0 :                 mask |= EPOLLPRI | EPOLLIN | EPOLLRDNORM;
    2461           86 :         if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
    2462            0 :                 mask |= EPOLLHUP;
    2463           86 :         if (tty_hung_up_p(file))
    2464            0 :                 mask |= EPOLLHUP;
    2465           86 :         if (tty->ops->write && !tty_is_writelocked(tty) &&
    2466           86 :                         tty_chars_in_buffer(tty) < WAKEUP_CHARS &&
    2467           85 :                         tty_write_room(tty) > 0)
    2468           85 :                 mask |= EPOLLOUT | EPOLLWRNORM;
    2469          172 :         return mask;
    2470           86 : }
    2471              : 
    2472            0 : static unsigned long inq_canon(struct n_tty_data *ldata)
    2473              : {
    2474            0 :         size_t nr, head, tail;
    2475              : 
    2476            0 :         if (ldata->canon_head == ldata->read_tail)
    2477            0 :                 return 0;
    2478            0 :         head = ldata->canon_head;
    2479            0 :         tail = ldata->read_tail;
    2480            0 :         nr = head - tail;
    2481              :         /* Skip EOF-chars.. */
    2482            0 :         while (MASK(head) != MASK(tail)) {
    2483            0 :                 if (test_bit(MASK(tail), ldata->read_flags) &&
    2484            0 :                     read_buf(ldata, tail) == __DISABLED_CHAR)
    2485            0 :                         nr--;
    2486            0 :                 tail++;
    2487              :         }
    2488            0 :         return nr;
    2489            0 : }
    2490              : 
    2491           18 : static int n_tty_ioctl(struct tty_struct *tty, unsigned int cmd,
    2492              :                        unsigned long arg)
    2493              : {
    2494           18 :         struct n_tty_data *ldata = tty->disc_data;
    2495           18 :         unsigned int num;
    2496              : 
    2497           18 :         switch (cmd) {
    2498              :         case TIOCOUTQ:
    2499            0 :                 return put_user(tty_chars_in_buffer(tty), (int __user *) arg);
    2500              :         case TIOCINQ:
    2501            0 :                 down_write(&tty->termios_rwsem);
    2502            0 :                 if (L_ICANON(tty) && !L_EXTPROC(tty))
    2503            0 :                         num = inq_canon(ldata);
    2504              :                 else
    2505            0 :                         num = read_cnt(ldata);
    2506            0 :                 up_write(&tty->termios_rwsem);
    2507            0 :                 return put_user(num, (unsigned int __user *) arg);
    2508              :         default:
    2509           18 :                 return n_tty_ioctl_helper(tty, cmd, arg);
    2510              :         }
    2511           18 : }
    2512              : 
    2513              : static struct tty_ldisc_ops n_tty_ops = {
    2514              :         .owner           = THIS_MODULE,
    2515              :         .num             = N_TTY,
    2516              :         .name            = "n_tty",
    2517              :         .open            = n_tty_open,
    2518              :         .close           = n_tty_close,
    2519              :         .flush_buffer    = n_tty_flush_buffer,
    2520              :         .read            = n_tty_read,
    2521              :         .write           = n_tty_write,
    2522              :         .ioctl           = n_tty_ioctl,
    2523              :         .set_termios     = n_tty_set_termios,
    2524              :         .poll            = n_tty_poll,
    2525              :         .receive_buf     = n_tty_receive_buf,
    2526              :         .write_wakeup    = n_tty_write_wakeup,
    2527              :         .receive_buf2    = n_tty_receive_buf2,
    2528              :         .lookahead_buf   = n_tty_lookahead_flow_ctrl,
    2529              : };
    2530              : 
    2531              : /**
    2532              :  *      n_tty_inherit_ops       -       inherit N_TTY methods
    2533              :  *      @ops: struct tty_ldisc_ops where to save N_TTY methods
    2534              :  *
    2535              :  *      Enables a 'subclass' line discipline to 'inherit' N_TTY methods.
    2536              :  */
    2537              : 
    2538            0 : void n_tty_inherit_ops(struct tty_ldisc_ops *ops)
    2539              : {
    2540            0 :         *ops = n_tty_ops;
    2541            0 :         ops->owner = NULL;
    2542            0 : }
    2543              : EXPORT_SYMBOL_GPL(n_tty_inherit_ops);
    2544              : 
    2545            0 : void __init n_tty_init(void)
    2546              : {
    2547            0 :         tty_register_ldisc(&n_tty_ops);
    2548            0 : }
        

Generated by: LCOV version 2.0-1