aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/char/tty_io.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/char/tty_io.c')
-rw-r--r--drivers/char/tty_io.c1380
1 files changed, 403 insertions, 977 deletions
diff --git a/drivers/char/tty_io.c b/drivers/char/tty_io.c
index e4dce8709541..7053d6333692 100644
--- a/drivers/char/tty_io.c
+++ b/drivers/char/tty_io.c
@@ -49,7 +49,7 @@
49 * implement CONFIG_VT and generalize console device interface. 49 * implement CONFIG_VT and generalize console device interface.
50 * -- Marko Kohtala <Marko.Kohtala@hut.fi>, March 97 50 * -- Marko Kohtala <Marko.Kohtala@hut.fi>, March 97
51 * 51 *
52 * Rewrote init_dev and release_dev to eliminate races. 52 * Rewrote tty_init_dev and tty_release_dev to eliminate races.
53 * -- Bill Hawes <whawes@star.net>, June 97 53 * -- Bill Hawes <whawes@star.net>, June 97
54 * 54 *
55 * Added devfs support. 55 * Added devfs support.
@@ -136,13 +136,6 @@ LIST_HEAD(tty_drivers); /* linked list of tty drivers */
136DEFINE_MUTEX(tty_mutex); 136DEFINE_MUTEX(tty_mutex);
137EXPORT_SYMBOL(tty_mutex); 137EXPORT_SYMBOL(tty_mutex);
138 138
139#ifdef CONFIG_UNIX98_PTYS
140extern struct tty_driver *ptm_driver; /* Unix98 pty masters; for /dev/ptmx */
141static int ptmx_open(struct inode *, struct file *);
142#endif
143
144static void initialize_tty_struct(struct tty_struct *tty);
145
146static ssize_t tty_read(struct file *, char __user *, size_t, loff_t *); 139static ssize_t tty_read(struct file *, char __user *, size_t, loff_t *);
147static ssize_t tty_write(struct file *, const char __user *, size_t, loff_t *); 140static ssize_t tty_write(struct file *, const char __user *, size_t, loff_t *);
148ssize_t redirected_tty_write(struct file *, const char __user *, 141ssize_t redirected_tty_write(struct file *, const char __user *,
@@ -171,13 +164,11 @@ static void proc_set_tty(struct task_struct *tsk, struct tty_struct *tty);
171 * Locking: none 164 * Locking: none
172 */ 165 */
173 166
174static struct tty_struct *alloc_tty_struct(void) 167struct tty_struct *alloc_tty_struct(void)
175{ 168{
176 return kzalloc(sizeof(struct tty_struct), GFP_KERNEL); 169 return kzalloc(sizeof(struct tty_struct), GFP_KERNEL);
177} 170}
178 171
179static void tty_buffer_free_all(struct tty_struct *);
180
181/** 172/**
182 * free_tty_struct - free a disused tty 173 * free_tty_struct - free a disused tty
183 * @tty: tty struct to free 174 * @tty: tty struct to free
@@ -187,7 +178,7 @@ static void tty_buffer_free_all(struct tty_struct *);
187 * Locking: none. Must be called after tty is definitely unused 178 * Locking: none. Must be called after tty is definitely unused
188 */ 179 */
189 180
190static inline void free_tty_struct(struct tty_struct *tty) 181void free_tty_struct(struct tty_struct *tty)
191{ 182{
192 kfree(tty->write_buf); 183 kfree(tty->write_buf);
193 tty_buffer_free_all(tty); 184 tty_buffer_free_all(tty);
@@ -263,398 +254,6 @@ static int check_tty_count(struct tty_struct *tty, const char *routine)
263 return 0; 254 return 0;
264} 255}
265 256
266/*
267 * Tty buffer allocation management
268 */
269
270/**
271 * tty_buffer_free_all - free buffers used by a tty
272 * @tty: tty to free from
273 *
274 * Remove all the buffers pending on a tty whether queued with data
275 * or in the free ring. Must be called when the tty is no longer in use
276 *
277 * Locking: none
278 */
279
280static void tty_buffer_free_all(struct tty_struct *tty)
281{
282 struct tty_buffer *thead;
283 while ((thead = tty->buf.head) != NULL) {
284 tty->buf.head = thead->next;
285 kfree(thead);
286 }
287 while ((thead = tty->buf.free) != NULL) {
288 tty->buf.free = thead->next;
289 kfree(thead);
290 }
291 tty->buf.tail = NULL;
292 tty->buf.memory_used = 0;
293}
294
295/**
296 * tty_buffer_init - prepare a tty buffer structure
297 * @tty: tty to initialise
298 *
299 * Set up the initial state of the buffer management for a tty device.
300 * Must be called before the other tty buffer functions are used.
301 *
302 * Locking: none
303 */
304
305static void tty_buffer_init(struct tty_struct *tty)
306{
307 spin_lock_init(&tty->buf.lock);
308 tty->buf.head = NULL;
309 tty->buf.tail = NULL;
310 tty->buf.free = NULL;
311 tty->buf.memory_used = 0;
312}
313
314/**
315 * tty_buffer_alloc - allocate a tty buffer
316 * @tty: tty device
317 * @size: desired size (characters)
318 *
319 * Allocate a new tty buffer to hold the desired number of characters.
320 * Return NULL if out of memory or the allocation would exceed the
321 * per device queue
322 *
323 * Locking: Caller must hold tty->buf.lock
324 */
325
326static struct tty_buffer *tty_buffer_alloc(struct tty_struct *tty, size_t size)
327{
328 struct tty_buffer *p;
329
330 if (tty->buf.memory_used + size > 65536)
331 return NULL;
332 p = kmalloc(sizeof(struct tty_buffer) + 2 * size, GFP_ATOMIC);
333 if (p == NULL)
334 return NULL;
335 p->used = 0;
336 p->size = size;
337 p->next = NULL;
338 p->commit = 0;
339 p->read = 0;
340 p->char_buf_ptr = (char *)(p->data);
341 p->flag_buf_ptr = (unsigned char *)p->char_buf_ptr + size;
342 tty->buf.memory_used += size;
343 return p;
344}
345
346/**
347 * tty_buffer_free - free a tty buffer
348 * @tty: tty owning the buffer
349 * @b: the buffer to free
350 *
351 * Free a tty buffer, or add it to the free list according to our
352 * internal strategy
353 *
354 * Locking: Caller must hold tty->buf.lock
355 */
356
357static void tty_buffer_free(struct tty_struct *tty, struct tty_buffer *b)
358{
359 /* Dumb strategy for now - should keep some stats */
360 tty->buf.memory_used -= b->size;
361 WARN_ON(tty->buf.memory_used < 0);
362
363 if (b->size >= 512)
364 kfree(b);
365 else {
366 b->next = tty->buf.free;
367 tty->buf.free = b;
368 }
369}
370
371/**
372 * __tty_buffer_flush - flush full tty buffers
373 * @tty: tty to flush
374 *
375 * flush all the buffers containing receive data. Caller must
376 * hold the buffer lock and must have ensured no parallel flush to
377 * ldisc is running.
378 *
379 * Locking: Caller must hold tty->buf.lock
380 */
381
382static void __tty_buffer_flush(struct tty_struct *tty)
383{
384 struct tty_buffer *thead;
385
386 while ((thead = tty->buf.head) != NULL) {
387 tty->buf.head = thead->next;
388 tty_buffer_free(tty, thead);
389 }
390 tty->buf.tail = NULL;
391}
392
393/**
394 * tty_buffer_flush - flush full tty buffers
395 * @tty: tty to flush
396 *
397 * flush all the buffers containing receive data. If the buffer is
398 * being processed by flush_to_ldisc then we defer the processing
399 * to that function
400 *
401 * Locking: none
402 */
403
404static void tty_buffer_flush(struct tty_struct *tty)
405{
406 unsigned long flags;
407 spin_lock_irqsave(&tty->buf.lock, flags);
408
409 /* If the data is being pushed to the tty layer then we can't
410 process it here. Instead set a flag and the flush_to_ldisc
411 path will process the flush request before it exits */
412 if (test_bit(TTY_FLUSHING, &tty->flags)) {
413 set_bit(TTY_FLUSHPENDING, &tty->flags);
414 spin_unlock_irqrestore(&tty->buf.lock, flags);
415 wait_event(tty->read_wait,
416 test_bit(TTY_FLUSHPENDING, &tty->flags) == 0);
417 return;
418 } else
419 __tty_buffer_flush(tty);
420 spin_unlock_irqrestore(&tty->buf.lock, flags);
421}
422
423/**
424 * tty_buffer_find - find a free tty buffer
425 * @tty: tty owning the buffer
426 * @size: characters wanted
427 *
428 * Locate an existing suitable tty buffer or if we are lacking one then
429 * allocate a new one. We round our buffers off in 256 character chunks
430 * to get better allocation behaviour.
431 *
432 * Locking: Caller must hold tty->buf.lock
433 */
434
435static struct tty_buffer *tty_buffer_find(struct tty_struct *tty, size_t size)
436{
437 struct tty_buffer **tbh = &tty->buf.free;
438 while ((*tbh) != NULL) {
439 struct tty_buffer *t = *tbh;
440 if (t->size >= size) {
441 *tbh = t->next;
442 t->next = NULL;
443 t->used = 0;
444 t->commit = 0;
445 t->read = 0;
446 tty->buf.memory_used += t->size;
447 return t;
448 }
449 tbh = &((*tbh)->next);
450 }
451 /* Round the buffer size out */
452 size = (size + 0xFF) & ~0xFF;
453 return tty_buffer_alloc(tty, size);
454 /* Should possibly check if this fails for the largest buffer we
455 have queued and recycle that ? */
456}
457
458/**
459 * tty_buffer_request_room - grow tty buffer if needed
460 * @tty: tty structure
461 * @size: size desired
462 *
463 * Make at least size bytes of linear space available for the tty
464 * buffer. If we fail return the size we managed to find.
465 *
466 * Locking: Takes tty->buf.lock
467 */
468int tty_buffer_request_room(struct tty_struct *tty, size_t size)
469{
470 struct tty_buffer *b, *n;
471 int left;
472 unsigned long flags;
473
474 spin_lock_irqsave(&tty->buf.lock, flags);
475
476 /* OPTIMISATION: We could keep a per tty "zero" sized buffer to
477 remove this conditional if its worth it. This would be invisible
478 to the callers */
479 if ((b = tty->buf.tail) != NULL)
480 left = b->size - b->used;
481 else
482 left = 0;
483
484 if (left < size) {
485 /* This is the slow path - looking for new buffers to use */
486 if ((n = tty_buffer_find(tty, size)) != NULL) {
487 if (b != NULL) {
488 b->next = n;
489 b->commit = b->used;
490 } else
491 tty->buf.head = n;
492 tty->buf.tail = n;
493 } else
494 size = left;
495 }
496
497 spin_unlock_irqrestore(&tty->buf.lock, flags);
498 return size;
499}
500EXPORT_SYMBOL_GPL(tty_buffer_request_room);
501
502/**
503 * tty_insert_flip_string - Add characters to the tty buffer
504 * @tty: tty structure
505 * @chars: characters
506 * @size: size
507 *
508 * Queue a series of bytes to the tty buffering. All the characters
509 * passed are marked as without error. Returns the number added.
510 *
511 * Locking: Called functions may take tty->buf.lock
512 */
513
514int tty_insert_flip_string(struct tty_struct *tty, const unsigned char *chars,
515 size_t size)
516{
517 int copied = 0;
518 do {
519 int space = tty_buffer_request_room(tty, size - copied);
520 struct tty_buffer *tb = tty->buf.tail;
521 /* If there is no space then tb may be NULL */
522 if (unlikely(space == 0))
523 break;
524 memcpy(tb->char_buf_ptr + tb->used, chars, space);
525 memset(tb->flag_buf_ptr + tb->used, TTY_NORMAL, space);
526 tb->used += space;
527 copied += space;
528 chars += space;
529 /* There is a small chance that we need to split the data over
530 several buffers. If this is the case we must loop */
531 } while (unlikely(size > copied));
532 return copied;
533}
534EXPORT_SYMBOL(tty_insert_flip_string);
535
536/**
537 * tty_insert_flip_string_flags - Add characters to the tty buffer
538 * @tty: tty structure
539 * @chars: characters
540 * @flags: flag bytes
541 * @size: size
542 *
543 * Queue a series of bytes to the tty buffering. For each character
544 * the flags array indicates the status of the character. Returns the
545 * number added.
546 *
547 * Locking: Called functions may take tty->buf.lock
548 */
549
550int tty_insert_flip_string_flags(struct tty_struct *tty,
551 const unsigned char *chars, const char *flags, size_t size)
552{
553 int copied = 0;
554 do {
555 int space = tty_buffer_request_room(tty, size - copied);
556 struct tty_buffer *tb = tty->buf.tail;
557 /* If there is no space then tb may be NULL */
558 if (unlikely(space == 0))
559 break;
560 memcpy(tb->char_buf_ptr + tb->used, chars, space);
561 memcpy(tb->flag_buf_ptr + tb->used, flags, space);
562 tb->used += space;
563 copied += space;
564 chars += space;
565 flags += space;
566 /* There is a small chance that we need to split the data over
567 several buffers. If this is the case we must loop */
568 } while (unlikely(size > copied));
569 return copied;
570}
571EXPORT_SYMBOL(tty_insert_flip_string_flags);
572
573/**
574 * tty_schedule_flip - push characters to ldisc
575 * @tty: tty to push from
576 *
577 * Takes any pending buffers and transfers their ownership to the
578 * ldisc side of the queue. It then schedules those characters for
579 * processing by the line discipline.
580 *
581 * Locking: Takes tty->buf.lock
582 */
583
584void tty_schedule_flip(struct tty_struct *tty)
585{
586 unsigned long flags;
587 spin_lock_irqsave(&tty->buf.lock, flags);
588 if (tty->buf.tail != NULL)
589 tty->buf.tail->commit = tty->buf.tail->used;
590 spin_unlock_irqrestore(&tty->buf.lock, flags);
591 schedule_delayed_work(&tty->buf.work, 1);
592}
593EXPORT_SYMBOL(tty_schedule_flip);
594
595/**
596 * tty_prepare_flip_string - make room for characters
597 * @tty: tty
598 * @chars: return pointer for character write area
599 * @size: desired size
600 *
601 * Prepare a block of space in the buffer for data. Returns the length
602 * available and buffer pointer to the space which is now allocated and
603 * accounted for as ready for normal characters. This is used for drivers
604 * that need their own block copy routines into the buffer. There is no
605 * guarantee the buffer is a DMA target!
606 *
607 * Locking: May call functions taking tty->buf.lock
608 */
609
610int tty_prepare_flip_string(struct tty_struct *tty, unsigned char **chars,
611 size_t size)
612{
613 int space = tty_buffer_request_room(tty, size);
614 if (likely(space)) {
615 struct tty_buffer *tb = tty->buf.tail;
616 *chars = tb->char_buf_ptr + tb->used;
617 memset(tb->flag_buf_ptr + tb->used, TTY_NORMAL, space);
618 tb->used += space;
619 }
620 return space;
621}
622
623EXPORT_SYMBOL_GPL(tty_prepare_flip_string);
624
625/**
626 * tty_prepare_flip_string_flags - make room for characters
627 * @tty: tty
628 * @chars: return pointer for character write area
629 * @flags: return pointer for status flag write area
630 * @size: desired size
631 *
632 * Prepare a block of space in the buffer for data. Returns the length
633 * available and buffer pointer to the space which is now allocated and
634 * accounted for as ready for characters. This is used for drivers
635 * that need their own block copy routines into the buffer. There is no
636 * guarantee the buffer is a DMA target!
637 *
638 * Locking: May call functions taking tty->buf.lock
639 */
640
641int tty_prepare_flip_string_flags(struct tty_struct *tty,
642 unsigned char **chars, char **flags, size_t size)
643{
644 int space = tty_buffer_request_room(tty, size);
645 if (likely(space)) {
646 struct tty_buffer *tb = tty->buf.tail;
647 *chars = tb->char_buf_ptr + tb->used;
648 *flags = tb->flag_buf_ptr + tb->used;
649 tb->used += space;
650 }
651 return space;
652}
653
654EXPORT_SYMBOL_GPL(tty_prepare_flip_string_flags);
655
656
657
658/** 257/**
659 * get_tty_driver - find device of a tty 258 * get_tty_driver - find device of a tty
660 * @dev_t: device identifier 259 * @dev_t: device identifier
@@ -675,7 +274,7 @@ static struct tty_driver *get_tty_driver(dev_t device, int *index)
675 if (device < base || device >= base + p->num) 274 if (device < base || device >= base + p->num)
676 continue; 275 continue;
677 *index = device - base; 276 *index = device - base;
678 return p; 277 return tty_driver_kref_get(p);
679 } 278 }
680 return NULL; 279 return NULL;
681} 280}
@@ -719,7 +318,7 @@ struct tty_driver *tty_find_polling_driver(char *name, int *line)
719 318
720 if (tty_line >= 0 && tty_line <= p->num && p->ops && 319 if (tty_line >= 0 && tty_line <= p->num && p->ops &&
721 p->ops->poll_init && !p->ops->poll_init(p, tty_line, str)) { 320 p->ops->poll_init && !p->ops->poll_init(p, tty_line, str)) {
722 res = p; 321 res = tty_driver_kref_get(p);
723 *line = tty_line; 322 *line = tty_line;
724 break; 323 break;
725 } 324 }
@@ -819,20 +418,6 @@ static const struct file_operations tty_fops = {
819 .fasync = tty_fasync, 418 .fasync = tty_fasync,
820}; 419};
821 420
822#ifdef CONFIG_UNIX98_PTYS
823static const struct file_operations ptmx_fops = {
824 .llseek = no_llseek,
825 .read = tty_read,
826 .write = tty_write,
827 .poll = tty_poll,
828 .unlocked_ioctl = tty_ioctl,
829 .compat_ioctl = tty_compat_ioctl,
830 .open = ptmx_open,
831 .release = tty_release,
832 .fasync = tty_fasync,
833};
834#endif
835
836static const struct file_operations console_fops = { 421static const struct file_operations console_fops = {
837 .llseek = no_llseek, 422 .llseek = no_llseek,
838 .read = tty_read, 423 .read = tty_read,
@@ -953,6 +538,7 @@ static void do_tty_hangup(struct work_struct *work)
953 struct tty_ldisc *ld; 538 struct tty_ldisc *ld;
954 int closecount = 0, n; 539 int closecount = 0, n;
955 unsigned long flags; 540 unsigned long flags;
541 int refs = 0;
956 542
957 if (!tty) 543 if (!tty)
958 return; 544 return;
@@ -1019,8 +605,12 @@ static void do_tty_hangup(struct work_struct *work)
1019 if (tty->session) { 605 if (tty->session) {
1020 do_each_pid_task(tty->session, PIDTYPE_SID, p) { 606 do_each_pid_task(tty->session, PIDTYPE_SID, p) {
1021 spin_lock_irq(&p->sighand->siglock); 607 spin_lock_irq(&p->sighand->siglock);
1022 if (p->signal->tty == tty) 608 if (p->signal->tty == tty) {
1023 p->signal->tty = NULL; 609 p->signal->tty = NULL;
610 /* We defer the dereferences outside fo
611 the tasklist lock */
612 refs++;
613 }
1024 if (!p->signal->leader) { 614 if (!p->signal->leader) {
1025 spin_unlock_irq(&p->sighand->siglock); 615 spin_unlock_irq(&p->sighand->siglock);
1026 continue; 616 continue;
@@ -1046,6 +636,10 @@ static void do_tty_hangup(struct work_struct *work)
1046 tty->ctrl_status = 0; 636 tty->ctrl_status = 0;
1047 spin_unlock_irqrestore(&tty->ctrl_lock, flags); 637 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
1048 638
639 /* Account for the p->signal references we killed */
640 while (refs--)
641 tty_kref_put(tty);
642
1049 /* 643 /*
1050 * If one of the devices matches a console pointer, we 644 * If one of the devices matches a console pointer, we
1051 * cannot just call hangup() because that will cause 645 * cannot just call hangup() because that will cause
@@ -1115,6 +709,23 @@ void tty_vhangup(struct tty_struct *tty)
1115EXPORT_SYMBOL(tty_vhangup); 709EXPORT_SYMBOL(tty_vhangup);
1116 710
1117/** 711/**
712 * tty_vhangup_self - process vhangup for own ctty
713 *
714 * Perform a vhangup on the current controlling tty
715 */
716
717void tty_vhangup_self(void)
718{
719 struct tty_struct *tty;
720
721 tty = get_current_tty();
722 if (tty) {
723 tty_vhangup(tty);
724 tty_kref_put(tty);
725 }
726}
727
728/**
1118 * tty_hung_up_p - was tty hung up 729 * tty_hung_up_p - was tty hung up
1119 * @filp: file pointer of tty 730 * @filp: file pointer of tty
1120 * 731 *
@@ -1167,16 +778,14 @@ void disassociate_ctty(int on_exit)
1167 struct pid *tty_pgrp = NULL; 778 struct pid *tty_pgrp = NULL;
1168 779
1169 780
1170 mutex_lock(&tty_mutex);
1171 tty = get_current_tty(); 781 tty = get_current_tty();
1172 if (tty) { 782 if (tty) {
1173 tty_pgrp = get_pid(tty->pgrp); 783 tty_pgrp = get_pid(tty->pgrp);
1174 lock_kernel(); 784 lock_kernel();
1175 mutex_unlock(&tty_mutex);
1176 /* XXX: here we race, there is nothing protecting tty */
1177 if (on_exit && tty->driver->type != TTY_DRIVER_TYPE_PTY) 785 if (on_exit && tty->driver->type != TTY_DRIVER_TYPE_PTY)
1178 tty_vhangup(tty); 786 tty_vhangup(tty);
1179 unlock_kernel(); 787 unlock_kernel();
788 tty_kref_put(tty);
1180 } else if (on_exit) { 789 } else if (on_exit) {
1181 struct pid *old_pgrp; 790 struct pid *old_pgrp;
1182 spin_lock_irq(&current->sighand->siglock); 791 spin_lock_irq(&current->sighand->siglock);
@@ -1188,7 +797,6 @@ void disassociate_ctty(int on_exit)
1188 kill_pgrp(old_pgrp, SIGCONT, on_exit); 797 kill_pgrp(old_pgrp, SIGCONT, on_exit);
1189 put_pid(old_pgrp); 798 put_pid(old_pgrp);
1190 } 799 }
1191 mutex_unlock(&tty_mutex);
1192 return; 800 return;
1193 } 801 }
1194 if (tty_pgrp) { 802 if (tty_pgrp) {
@@ -1203,8 +811,6 @@ void disassociate_ctty(int on_exit)
1203 current->signal->tty_old_pgrp = NULL; 811 current->signal->tty_old_pgrp = NULL;
1204 spin_unlock_irq(&current->sighand->siglock); 812 spin_unlock_irq(&current->sighand->siglock);
1205 813
1206 mutex_lock(&tty_mutex);
1207 /* It is possible that do_tty_hangup has free'd this tty */
1208 tty = get_current_tty(); 814 tty = get_current_tty();
1209 if (tty) { 815 if (tty) {
1210 unsigned long flags; 816 unsigned long flags;
@@ -1214,13 +820,13 @@ void disassociate_ctty(int on_exit)
1214 tty->session = NULL; 820 tty->session = NULL;
1215 tty->pgrp = NULL; 821 tty->pgrp = NULL;
1216 spin_unlock_irqrestore(&tty->ctrl_lock, flags); 822 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
823 tty_kref_put(tty);
1217 } else { 824 } else {
1218#ifdef TTY_DEBUG_HANGUP 825#ifdef TTY_DEBUG_HANGUP
1219 printk(KERN_DEBUG "error attempted to write to tty [0x%p]" 826 printk(KERN_DEBUG "error attempted to write to tty [0x%p]"
1220 " = NULL", tty); 827 " = NULL", tty);
1221#endif 828#endif
1222 } 829 }
1223 mutex_unlock(&tty_mutex);
1224 830
1225 /* Now clear signal->tty under the lock */ 831 /* Now clear signal->tty under the lock */
1226 read_lock(&tasklist_lock); 832 read_lock(&tasklist_lock);
@@ -1420,19 +1026,19 @@ static inline ssize_t do_tty_write(
1420 1026
1421 /* write_buf/write_cnt is protected by the atomic_write_lock mutex */ 1027 /* write_buf/write_cnt is protected by the atomic_write_lock mutex */
1422 if (tty->write_cnt < chunk) { 1028 if (tty->write_cnt < chunk) {
1423 unsigned char *buf; 1029 unsigned char *buf_chunk;
1424 1030
1425 if (chunk < 1024) 1031 if (chunk < 1024)
1426 chunk = 1024; 1032 chunk = 1024;
1427 1033
1428 buf = kmalloc(chunk, GFP_KERNEL); 1034 buf_chunk = kmalloc(chunk, GFP_KERNEL);
1429 if (!buf) { 1035 if (!buf_chunk) {
1430 ret = -ENOMEM; 1036 ret = -ENOMEM;
1431 goto out; 1037 goto out;
1432 } 1038 }
1433 kfree(tty->write_buf); 1039 kfree(tty->write_buf);
1434 tty->write_cnt = chunk; 1040 tty->write_cnt = chunk;
1435 tty->write_buf = buf; 1041 tty->write_buf = buf_chunk;
1436 } 1042 }
1437 1043
1438 /* Do the write .. */ 1044 /* Do the write .. */
@@ -1466,6 +1072,31 @@ out:
1466 return ret; 1072 return ret;
1467} 1073}
1468 1074
1075/**
1076 * tty_write_message - write a message to a certain tty, not just the console.
1077 * @tty: the destination tty_struct
1078 * @msg: the message to write
1079 *
1080 * This is used for messages that need to be redirected to a specific tty.
1081 * We don't put it into the syslog queue right now maybe in the future if
1082 * really needed.
1083 *
1084 * We must still hold the BKL and test the CLOSING flag for the moment.
1085 */
1086
1087void tty_write_message(struct tty_struct *tty, char *msg)
1088{
1089 lock_kernel();
1090 if (tty) {
1091 mutex_lock(&tty->atomic_write_lock);
1092 if (tty->ops->write && !test_bit(TTY_CLOSING, &tty->flags))
1093 tty->ops->write(tty, msg, strlen(msg));
1094 tty_write_unlock(tty);
1095 }
1096 unlock_kernel();
1097 return;
1098}
1099
1469 1100
1470/** 1101/**
1471 * tty_write - write method for tty device file 1102 * tty_write - write method for tty device file
@@ -1533,42 +1164,6 @@ ssize_t redirected_tty_write(struct file *file, const char __user *buf,
1533 return tty_write(file, buf, count, ppos); 1164 return tty_write(file, buf, count, ppos);
1534} 1165}
1535 1166
1536void tty_port_init(struct tty_port *port)
1537{
1538 memset(port, 0, sizeof(*port));
1539 init_waitqueue_head(&port->open_wait);
1540 init_waitqueue_head(&port->close_wait);
1541 mutex_init(&port->mutex);
1542 port->close_delay = (50 * HZ) / 100;
1543 port->closing_wait = (3000 * HZ) / 100;
1544}
1545EXPORT_SYMBOL(tty_port_init);
1546
1547int tty_port_alloc_xmit_buf(struct tty_port *port)
1548{
1549 /* We may sleep in get_zeroed_page() */
1550 mutex_lock(&port->mutex);
1551 if (port->xmit_buf == NULL)
1552 port->xmit_buf = (unsigned char *)get_zeroed_page(GFP_KERNEL);
1553 mutex_unlock(&port->mutex);
1554 if (port->xmit_buf == NULL)
1555 return -ENOMEM;
1556 return 0;
1557}
1558EXPORT_SYMBOL(tty_port_alloc_xmit_buf);
1559
1560void tty_port_free_xmit_buf(struct tty_port *port)
1561{
1562 mutex_lock(&port->mutex);
1563 if (port->xmit_buf != NULL) {
1564 free_page((unsigned long)port->xmit_buf);
1565 port->xmit_buf = NULL;
1566 }
1567 mutex_unlock(&port->mutex);
1568}
1569EXPORT_SYMBOL(tty_port_free_xmit_buf);
1570
1571
1572static char ptychar[] = "pqrstuvwxyzabcde"; 1167static char ptychar[] = "pqrstuvwxyzabcde";
1573 1168
1574/** 1169/**
@@ -1592,7 +1187,7 @@ static void pty_line_name(struct tty_driver *driver, int index, char *p)
1592} 1187}
1593 1188
1594/** 1189/**
1595 * pty_line_name - generate name for a tty 1190 * tty_line_name - generate name for a tty
1596 * @driver: the tty driver in use 1191 * @driver: the tty driver in use
1597 * @index: the minor number 1192 * @index: the minor number
1598 * @p: output buffer of at least 7 bytes 1193 * @p: output buffer of at least 7 bytes
@@ -1608,10 +1203,148 @@ static void tty_line_name(struct tty_driver *driver, int index, char *p)
1608} 1203}
1609 1204
1610/** 1205/**
1611 * init_dev - initialise a tty device 1206 * tty_driver_lookup_tty() - find an existing tty, if any
1207 * @driver: the driver for the tty
1208 * @idx: the minor number
1209 *
1210 * Return the tty, if found or ERR_PTR() otherwise.
1211 *
1212 * Locking: tty_mutex must be held. If tty is found, the mutex must
1213 * be held until the 'fast-open' is also done. Will change once we
1214 * have refcounting in the driver and per driver locking
1215 */
1216struct tty_struct *tty_driver_lookup_tty(struct tty_driver *driver,
1217 struct inode *inode, int idx)
1218{
1219 struct tty_struct *tty;
1220
1221 if (driver->ops->lookup)
1222 return driver->ops->lookup(driver, inode, idx);
1223
1224 tty = driver->ttys[idx];
1225 return tty;
1226}
1227
1228/**
1229 * tty_init_termios - helper for termios setup
1230 * @tty: the tty to set up
1231 *
1232 * Initialise the termios structures for this tty. Thus runs under
1233 * the tty_mutex currently so we can be relaxed about ordering.
1234 */
1235
1236int tty_init_termios(struct tty_struct *tty)
1237{
1238 struct ktermios *tp;
1239 int idx = tty->index;
1240
1241 tp = tty->driver->termios[idx];
1242 if (tp == NULL) {
1243 tp = kzalloc(sizeof(struct ktermios[2]), GFP_KERNEL);
1244 if (tp == NULL)
1245 return -ENOMEM;
1246 memcpy(tp, &tty->driver->init_termios,
1247 sizeof(struct ktermios));
1248 tty->driver->termios[idx] = tp;
1249 }
1250 tty->termios = tp;
1251 tty->termios_locked = tp + 1;
1252
1253 /* Compatibility until drivers always set this */
1254 tty->termios->c_ispeed = tty_termios_input_baud_rate(tty->termios);
1255 tty->termios->c_ospeed = tty_termios_baud_rate(tty->termios);
1256 return 0;
1257}
1258
1259/**
1260 * tty_driver_install_tty() - install a tty entry in the driver
1261 * @driver: the driver for the tty
1262 * @tty: the tty
1263 *
1264 * Install a tty object into the driver tables. The tty->index field
1265 * will be set by the time this is called. This method is responsible
1266 * for ensuring any need additional structures are allocated and
1267 * configured.
1268 *
1269 * Locking: tty_mutex for now
1270 */
1271static int tty_driver_install_tty(struct tty_driver *driver,
1272 struct tty_struct *tty)
1273{
1274 int idx = tty->index;
1275
1276 if (driver->ops->install)
1277 return driver->ops->install(driver, tty);
1278
1279 if (tty_init_termios(tty) == 0) {
1280 tty_driver_kref_get(driver);
1281 tty->count++;
1282 driver->ttys[idx] = tty;
1283 return 0;
1284 }
1285 return -ENOMEM;
1286}
1287
1288/**
1289 * tty_driver_remove_tty() - remove a tty from the driver tables
1290 * @driver: the driver for the tty
1291 * @idx: the minor number
1292 *
1293 * Remvoe a tty object from the driver tables. The tty->index field
1294 * will be set by the time this is called.
1295 *
1296 * Locking: tty_mutex for now
1297 */
1298static void tty_driver_remove_tty(struct tty_driver *driver,
1299 struct tty_struct *tty)
1300{
1301 if (driver->ops->remove)
1302 driver->ops->remove(driver, tty);
1303 else
1304 driver->ttys[tty->index] = NULL;
1305}
1306
1307/*
1308 * tty_reopen() - fast re-open of an open tty
1309 * @tty - the tty to open
1310 *
1311 * Return 0 on success, -errno on error.
1312 *
1313 * Locking: tty_mutex must be held from the time the tty was found
1314 * till this open completes.
1315 */
1316static int tty_reopen(struct tty_struct *tty)
1317{
1318 struct tty_driver *driver = tty->driver;
1319
1320 if (test_bit(TTY_CLOSING, &tty->flags))
1321 return -EIO;
1322
1323 if (driver->type == TTY_DRIVER_TYPE_PTY &&
1324 driver->subtype == PTY_TYPE_MASTER) {
1325 /*
1326 * special case for PTY masters: only one open permitted,
1327 * and the slave side open count is incremented as well.
1328 */
1329 if (tty->count)
1330 return -EIO;
1331
1332 tty->link->count++;
1333 }
1334 tty->count++;
1335 tty->driver = driver; /* N.B. why do this every time?? */
1336
1337 WARN_ON(!test_bit(TTY_LDISC, &tty->flags));
1338
1339 return 0;
1340}
1341
1342/**
1343 * tty_init_dev - initialise a tty device
1612 * @driver: tty driver we are opening a device on 1344 * @driver: tty driver we are opening a device on
1613 * @idx: device index 1345 * @idx: device index
1614 * @tty: returned tty structure 1346 * @ret_tty: returned tty structure
1347 * @first_ok: ok to open a new device (used by ptmx)
1615 * 1348 *
1616 * Prepare a tty device. This may not be a "new" clean device but 1349 * Prepare a tty device. This may not be a "new" clean device but
1617 * could also be an active device. The pty drivers require special 1350 * could also be an active device. The pty drivers require special
@@ -1631,37 +1364,16 @@ static void tty_line_name(struct tty_driver *driver, int index, char *p)
1631 * relaxed for the (most common) case of reopening a tty. 1364 * relaxed for the (most common) case of reopening a tty.
1632 */ 1365 */
1633 1366
1634static int init_dev(struct tty_driver *driver, int idx, 1367struct tty_struct *tty_init_dev(struct tty_driver *driver, int idx,
1635 struct tty_struct **ret_tty) 1368 int first_ok)
1636{ 1369{
1637 struct tty_struct *tty, *o_tty; 1370 struct tty_struct *tty;
1638 struct ktermios *tp, **tp_loc, *o_tp, **o_tp_loc; 1371 int retval;
1639 struct ktermios *ltp, **ltp_loc, *o_ltp, **o_ltp_loc;
1640 int retval = 0;
1641 1372
1642 /* check whether we're reopening an existing tty */ 1373 /* Check if pty master is being opened multiple times */
1643 if (driver->flags & TTY_DRIVER_DEVPTS_MEM) { 1374 if (driver->subtype == PTY_TYPE_MASTER &&
1644 tty = devpts_get_tty(idx); 1375 (driver->flags & TTY_DRIVER_DEVPTS_MEM) && !first_ok)
1645 /* 1376 return ERR_PTR(-EIO);
1646 * If we don't have a tty here on a slave open, it's because
1647 * the master already started the close process and there's
1648 * no relation between devpts file and tty anymore.
1649 */
1650 if (!tty && driver->subtype == PTY_TYPE_SLAVE) {
1651 retval = -EIO;
1652 goto end_init;
1653 }
1654 /*
1655 * It's safe from now on because init_dev() is called with
1656 * tty_mutex held and release_dev() won't change tty->count
1657 * or tty->flags without having to grab tty_mutex
1658 */
1659 if (tty && driver->subtype == PTY_TYPE_MASTER)
1660 tty = tty->link;
1661 } else {
1662 tty = driver->ttys[idx];
1663 }
1664 if (tty) goto fast_track;
1665 1377
1666 /* 1378 /*
1667 * First time open is complex, especially for PTY devices. 1379 * First time open is complex, especially for PTY devices.
@@ -1671,189 +1383,69 @@ static int init_dev(struct tty_driver *driver, int idx,
1671 * and locked termios may be retained.) 1383 * and locked termios may be retained.)
1672 */ 1384 */
1673 1385
1674 if (!try_module_get(driver->owner)) { 1386 if (!try_module_get(driver->owner))
1675 retval = -ENODEV; 1387 return ERR_PTR(-ENODEV);
1676 goto end_init;
1677 }
1678
1679 o_tty = NULL;
1680 tp = o_tp = NULL;
1681 ltp = o_ltp = NULL;
1682 1388
1683 tty = alloc_tty_struct(); 1389 tty = alloc_tty_struct();
1684 if (!tty) 1390 if (!tty)
1685 goto fail_no_mem; 1391 goto fail_no_mem;
1686 initialize_tty_struct(tty); 1392 initialize_tty_struct(tty, driver, idx);
1687 tty->driver = driver;
1688 tty->ops = driver->ops;
1689 tty->index = idx;
1690 tty_line_name(driver, idx, tty->name);
1691
1692 if (driver->flags & TTY_DRIVER_DEVPTS_MEM) {
1693 tp_loc = &tty->termios;
1694 ltp_loc = &tty->termios_locked;
1695 } else {
1696 tp_loc = &driver->termios[idx];
1697 ltp_loc = &driver->termios_locked[idx];
1698 }
1699
1700 if (!*tp_loc) {
1701 tp = kmalloc(sizeof(struct ktermios), GFP_KERNEL);
1702 if (!tp)
1703 goto free_mem_out;
1704 *tp = driver->init_termios;
1705 }
1706
1707 if (!*ltp_loc) {
1708 ltp = kzalloc(sizeof(struct ktermios), GFP_KERNEL);
1709 if (!ltp)
1710 goto free_mem_out;
1711 }
1712
1713 if (driver->type == TTY_DRIVER_TYPE_PTY) {
1714 o_tty = alloc_tty_struct();
1715 if (!o_tty)
1716 goto free_mem_out;
1717 initialize_tty_struct(o_tty);
1718 o_tty->driver = driver->other;
1719 o_tty->ops = driver->ops;
1720 o_tty->index = idx;
1721 tty_line_name(driver->other, idx, o_tty->name);
1722 1393
1723 if (driver->flags & TTY_DRIVER_DEVPTS_MEM) { 1394 retval = tty_driver_install_tty(driver, tty);
1724 o_tp_loc = &o_tty->termios; 1395 if (retval < 0) {
1725 o_ltp_loc = &o_tty->termios_locked; 1396 free_tty_struct(tty);
1726 } else { 1397 module_put(driver->owner);
1727 o_tp_loc = &driver->other->termios[idx]; 1398 return ERR_PTR(retval);
1728 o_ltp_loc = &driver->other->termios_locked[idx];
1729 }
1730
1731 if (!*o_tp_loc) {
1732 o_tp = kmalloc(sizeof(struct ktermios), GFP_KERNEL);
1733 if (!o_tp)
1734 goto free_mem_out;
1735 *o_tp = driver->other->init_termios;
1736 }
1737
1738 if (!*o_ltp_loc) {
1739 o_ltp = kzalloc(sizeof(struct ktermios), GFP_KERNEL);
1740 if (!o_ltp)
1741 goto free_mem_out;
1742 }
1743
1744 /*
1745 * Everything allocated ... set up the o_tty structure.
1746 */
1747 if (!(driver->other->flags & TTY_DRIVER_DEVPTS_MEM))
1748 driver->other->ttys[idx] = o_tty;
1749 if (!*o_tp_loc)
1750 *o_tp_loc = o_tp;
1751 if (!*o_ltp_loc)
1752 *o_ltp_loc = o_ltp;
1753 o_tty->termios = *o_tp_loc;
1754 o_tty->termios_locked = *o_ltp_loc;
1755 driver->other->refcount++;
1756 if (driver->subtype == PTY_TYPE_MASTER)
1757 o_tty->count++;
1758
1759 /* Establish the links in both directions */
1760 tty->link = o_tty;
1761 o_tty->link = tty;
1762 } 1399 }
1763 1400
1764 /* 1401 /*
1765 * All structures have been allocated, so now we install them.
1766 * Failures after this point use release_tty to clean up, so
1767 * there's no need to null out the local pointers.
1768 */
1769 if (!(driver->flags & TTY_DRIVER_DEVPTS_MEM))
1770 driver->ttys[idx] = tty;
1771
1772 if (!*tp_loc)
1773 *tp_loc = tp;
1774 if (!*ltp_loc)
1775 *ltp_loc = ltp;
1776 tty->termios = *tp_loc;
1777 tty->termios_locked = *ltp_loc;
1778 /* Compatibility until drivers always set this */
1779 tty->termios->c_ispeed = tty_termios_input_baud_rate(tty->termios);
1780 tty->termios->c_ospeed = tty_termios_baud_rate(tty->termios);
1781 driver->refcount++;
1782 tty->count++;
1783
1784 /*
1785 * Structures all installed ... call the ldisc open routines. 1402 * Structures all installed ... call the ldisc open routines.
1786 * If we fail here just call release_tty to clean up. No need 1403 * If we fail here just call release_tty to clean up. No need
1787 * to decrement the use counts, as release_tty doesn't care. 1404 * to decrement the use counts, as release_tty doesn't care.
1788 */ 1405 */
1789 1406
1790 retval = tty_ldisc_setup(tty, o_tty); 1407 retval = tty_ldisc_setup(tty, tty->link);
1791
1792 if (retval) 1408 if (retval)
1793 goto release_mem_out; 1409 goto release_mem_out;
1794 goto success; 1410 return tty;
1795
1796 /*
1797 * This fast open can be used if the tty is already open.
1798 * No memory is allocated, and the only failures are from
1799 * attempting to open a closing tty or attempting multiple
1800 * opens on a pty master.
1801 */
1802fast_track:
1803 if (test_bit(TTY_CLOSING, &tty->flags)) {
1804 retval = -EIO;
1805 goto end_init;
1806 }
1807 if (driver->type == TTY_DRIVER_TYPE_PTY &&
1808 driver->subtype == PTY_TYPE_MASTER) {
1809 /*
1810 * special case for PTY masters: only one open permitted,
1811 * and the slave side open count is incremented as well.
1812 */
1813 if (tty->count) {
1814 retval = -EIO;
1815 goto end_init;
1816 }
1817 tty->link->count++;
1818 }
1819 tty->count++;
1820 tty->driver = driver; /* N.B. why do this every time?? */
1821
1822 /* FIXME */
1823 if (!test_bit(TTY_LDISC, &tty->flags))
1824 printk(KERN_ERR "init_dev but no ldisc\n");
1825success:
1826 *ret_tty = tty;
1827
1828 /* All paths come through here to release the mutex */
1829end_init:
1830 return retval;
1831
1832 /* Release locally allocated memory ... nothing placed in slots */
1833free_mem_out:
1834 kfree(o_tp);
1835 if (o_tty)
1836 free_tty_struct(o_tty);
1837 kfree(ltp);
1838 kfree(tp);
1839 free_tty_struct(tty);
1840 1411
1841fail_no_mem: 1412fail_no_mem:
1842 module_put(driver->owner); 1413 module_put(driver->owner);
1843 retval = -ENOMEM; 1414 return ERR_PTR(-ENOMEM);
1844 goto end_init;
1845 1415
1846 /* call the tty release_tty routine to clean out this slot */ 1416 /* call the tty release_tty routine to clean out this slot */
1847release_mem_out: 1417release_mem_out:
1848 if (printk_ratelimit()) 1418 if (printk_ratelimit())
1849 printk(KERN_INFO "init_dev: ldisc open failed, " 1419 printk(KERN_INFO "tty_init_dev: ldisc open failed, "
1850 "clearing slot %d\n", idx); 1420 "clearing slot %d\n", idx);
1851 release_tty(tty, idx); 1421 release_tty(tty, idx);
1852 goto end_init; 1422 return ERR_PTR(retval);
1853} 1423}
1854 1424
1425void tty_free_termios(struct tty_struct *tty)
1426{
1427 struct ktermios *tp;
1428 int idx = tty->index;
1429 /* Kill this flag and push into drivers for locking etc */
1430 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) {
1431 /* FIXME: Locking on ->termios array */
1432 tp = tty->termios;
1433 tty->driver->termios[idx] = NULL;
1434 kfree(tp);
1435 }
1436}
1437EXPORT_SYMBOL(tty_free_termios);
1438
1439void tty_shutdown(struct tty_struct *tty)
1440{
1441 tty_driver_remove_tty(tty->driver, tty);
1442 tty_free_termios(tty);
1443}
1444EXPORT_SYMBOL(tty_shutdown);
1445
1855/** 1446/**
1856 * release_one_tty - release tty structure memory 1447 * release_one_tty - release tty structure memory
1448 * @kref: kref of tty we are obliterating
1857 * 1449 *
1858 * Releases memory associated with a tty structure, and clears out the 1450 * Releases memory associated with a tty structure, and clears out the
1859 * driver table slots. This function is called when a device is no longer 1451 * driver table slots. This function is called when a device is no longer
@@ -1863,31 +1455,19 @@ release_mem_out:
1863 * tty_mutex - sometimes only 1455 * tty_mutex - sometimes only
1864 * takes the file list lock internally when working on the list 1456 * takes the file list lock internally when working on the list
1865 * of ttys that the driver keeps. 1457 * of ttys that the driver keeps.
1866 * FIXME: should we require tty_mutex is held here ??
1867 */ 1458 */
1868static void release_one_tty(struct tty_struct *tty, int idx) 1459static void release_one_tty(struct kref *kref)
1869{ 1460{
1870 int devpts = tty->driver->flags & TTY_DRIVER_DEVPTS_MEM; 1461 struct tty_struct *tty = container_of(kref, struct tty_struct, kref);
1871 struct ktermios *tp; 1462 struct tty_driver *driver = tty->driver;
1872
1873 if (!devpts)
1874 tty->driver->ttys[idx] = NULL;
1875
1876 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) {
1877 tp = tty->termios;
1878 if (!devpts)
1879 tty->driver->termios[idx] = NULL;
1880 kfree(tp);
1881
1882 tp = tty->termios_locked;
1883 if (!devpts)
1884 tty->driver->termios_locked[idx] = NULL;
1885 kfree(tp);
1886 }
1887
1888 1463
1464 if (tty->ops->shutdown)
1465 tty->ops->shutdown(tty);
1466 else
1467 tty_shutdown(tty);
1889 tty->magic = 0; 1468 tty->magic = 0;
1890 tty->driver->refcount--; 1469 tty_driver_kref_put(driver);
1470 module_put(driver->owner);
1891 1471
1892 file_list_lock(); 1472 file_list_lock();
1893 list_del_init(&tty->tty_files); 1473 list_del_init(&tty->tty_files);
@@ -1897,6 +1477,21 @@ static void release_one_tty(struct tty_struct *tty, int idx)
1897} 1477}
1898 1478
1899/** 1479/**
1480 * tty_kref_put - release a tty kref
1481 * @tty: tty device
1482 *
1483 * Release a reference to a tty device and if need be let the kref
1484 * layer destruct the object for us
1485 */
1486
1487void tty_kref_put(struct tty_struct *tty)
1488{
1489 if (tty)
1490 kref_put(&tty->kref, release_one_tty);
1491}
1492EXPORT_SYMBOL(tty_kref_put);
1493
1494/**
1900 * release_tty - release tty structure memory 1495 * release_tty - release tty structure memory
1901 * 1496 *
1902 * Release both @tty and a possible linked partner (think pty pair), 1497 * Release both @tty and a possible linked partner (think pty pair),
@@ -1907,15 +1502,16 @@ static void release_one_tty(struct tty_struct *tty, int idx)
1907 * takes the file list lock internally when working on the list 1502 * takes the file list lock internally when working on the list
1908 * of ttys that the driver keeps. 1503 * of ttys that the driver keeps.
1909 * FIXME: should we require tty_mutex is held here ?? 1504 * FIXME: should we require tty_mutex is held here ??
1505 *
1910 */ 1506 */
1911static void release_tty(struct tty_struct *tty, int idx) 1507static void release_tty(struct tty_struct *tty, int idx)
1912{ 1508{
1913 struct tty_driver *driver = tty->driver; 1509 /* This should always be true but check for the moment */
1510 WARN_ON(tty->index != idx);
1914 1511
1915 if (tty->link) 1512 if (tty->link)
1916 release_one_tty(tty->link, idx); 1513 tty_kref_put(tty->link);
1917 release_one_tty(tty, idx); 1514 tty_kref_put(tty);
1918 module_put(driver->owner);
1919} 1515}
1920 1516
1921/* 1517/*
@@ -1926,20 +1522,21 @@ static void release_tty(struct tty_struct *tty, int idx)
1926 * WSH 09/09/97: rewritten to avoid some nasty race conditions that could 1522 * WSH 09/09/97: rewritten to avoid some nasty race conditions that could
1927 * lead to double frees or releasing memory still in use. 1523 * lead to double frees or releasing memory still in use.
1928 */ 1524 */
1929static void release_dev(struct file *filp) 1525void tty_release_dev(struct file *filp)
1930{ 1526{
1931 struct tty_struct *tty, *o_tty; 1527 struct tty_struct *tty, *o_tty;
1932 int pty_master, tty_closing, o_tty_closing, do_sleep; 1528 int pty_master, tty_closing, o_tty_closing, do_sleep;
1933 int devpts; 1529 int devpts;
1934 int idx; 1530 int idx;
1935 char buf[64]; 1531 char buf[64];
1532 struct inode *inode;
1936 1533
1534 inode = filp->f_path.dentry->d_inode;
1937 tty = (struct tty_struct *)filp->private_data; 1535 tty = (struct tty_struct *)filp->private_data;
1938 if (tty_paranoia_check(tty, filp->f_path.dentry->d_inode, 1536 if (tty_paranoia_check(tty, inode, "tty_release_dev"))
1939 "release_dev"))
1940 return; 1537 return;
1941 1538
1942 check_tty_count(tty, "release_dev"); 1539 check_tty_count(tty, "tty_release_dev");
1943 1540
1944 tty_fasync(-1, filp, 0); 1541 tty_fasync(-1, filp, 0);
1945 1542
@@ -1951,33 +1548,27 @@ static void release_dev(struct file *filp)
1951 1548
1952#ifdef TTY_PARANOIA_CHECK 1549#ifdef TTY_PARANOIA_CHECK
1953 if (idx < 0 || idx >= tty->driver->num) { 1550 if (idx < 0 || idx >= tty->driver->num) {
1954 printk(KERN_DEBUG "release_dev: bad idx when trying to " 1551 printk(KERN_DEBUG "tty_release_dev: bad idx when trying to "
1955 "free (%s)\n", tty->name); 1552 "free (%s)\n", tty->name);
1956 return; 1553 return;
1957 } 1554 }
1958 if (!(tty->driver->flags & TTY_DRIVER_DEVPTS_MEM)) { 1555 if (!devpts) {
1959 if (tty != tty->driver->ttys[idx]) { 1556 if (tty != tty->driver->ttys[idx]) {
1960 printk(KERN_DEBUG "release_dev: driver.table[%d] not tty " 1557 printk(KERN_DEBUG "tty_release_dev: driver.table[%d] not tty "
1961 "for (%s)\n", idx, tty->name); 1558 "for (%s)\n", idx, tty->name);
1962 return; 1559 return;
1963 } 1560 }
1964 if (tty->termios != tty->driver->termios[idx]) { 1561 if (tty->termios != tty->driver->termios[idx]) {
1965 printk(KERN_DEBUG "release_dev: driver.termios[%d] not termios " 1562 printk(KERN_DEBUG "tty_release_dev: driver.termios[%d] not termios "
1966 "for (%s)\n", 1563 "for (%s)\n",
1967 idx, tty->name); 1564 idx, tty->name);
1968 return; 1565 return;
1969 } 1566 }
1970 if (tty->termios_locked != tty->driver->termios_locked[idx]) {
1971 printk(KERN_DEBUG "release_dev: driver.termios_locked[%d] not "
1972 "termios_locked for (%s)\n",
1973 idx, tty->name);
1974 return;
1975 }
1976 } 1567 }
1977#endif 1568#endif
1978 1569
1979#ifdef TTY_DEBUG_HANGUP 1570#ifdef TTY_DEBUG_HANGUP
1980 printk(KERN_DEBUG "release_dev of %s (tty count=%d)...", 1571 printk(KERN_DEBUG "tty_release_dev of %s (tty count=%d)...",
1981 tty_name(tty, buf), tty->count); 1572 tty_name(tty, buf), tty->count);
1982#endif 1573#endif
1983 1574
@@ -1985,26 +1576,19 @@ static void release_dev(struct file *filp)
1985 if (tty->driver->other && 1576 if (tty->driver->other &&
1986 !(tty->driver->flags & TTY_DRIVER_DEVPTS_MEM)) { 1577 !(tty->driver->flags & TTY_DRIVER_DEVPTS_MEM)) {
1987 if (o_tty != tty->driver->other->ttys[idx]) { 1578 if (o_tty != tty->driver->other->ttys[idx]) {
1988 printk(KERN_DEBUG "release_dev: other->table[%d] " 1579 printk(KERN_DEBUG "tty_release_dev: other->table[%d] "
1989 "not o_tty for (%s)\n", 1580 "not o_tty for (%s)\n",
1990 idx, tty->name); 1581 idx, tty->name);
1991 return; 1582 return;
1992 } 1583 }
1993 if (o_tty->termios != tty->driver->other->termios[idx]) { 1584 if (o_tty->termios != tty->driver->other->termios[idx]) {
1994 printk(KERN_DEBUG "release_dev: other->termios[%d] " 1585 printk(KERN_DEBUG "tty_release_dev: other->termios[%d] "
1995 "not o_termios for (%s)\n", 1586 "not o_termios for (%s)\n",
1996 idx, tty->name); 1587 idx, tty->name);
1997 return; 1588 return;
1998 } 1589 }
1999 if (o_tty->termios_locked !=
2000 tty->driver->other->termios_locked[idx]) {
2001 printk(KERN_DEBUG "release_dev: other->termios_locked["
2002 "%d] not o_termios_locked for (%s)\n",
2003 idx, tty->name);
2004 return;
2005 }
2006 if (o_tty->link != tty) { 1590 if (o_tty->link != tty) {
2007 printk(KERN_DEBUG "release_dev: bad pty pointers\n"); 1591 printk(KERN_DEBUG "tty_release_dev: bad pty pointers\n");
2008 return; 1592 return;
2009 } 1593 }
2010 } 1594 }
@@ -2062,7 +1646,7 @@ static void release_dev(struct file *filp)
2062 if (!do_sleep) 1646 if (!do_sleep)
2063 break; 1647 break;
2064 1648
2065 printk(KERN_WARNING "release_dev: %s: read/write wait queue " 1649 printk(KERN_WARNING "tty_release_dev: %s: read/write wait queue "
2066 "active!\n", tty_name(tty, buf)); 1650 "active!\n", tty_name(tty, buf));
2067 mutex_unlock(&tty_mutex); 1651 mutex_unlock(&tty_mutex);
2068 schedule(); 1652 schedule();
@@ -2075,14 +1659,14 @@ static void release_dev(struct file *filp)
2075 */ 1659 */
2076 if (pty_master) { 1660 if (pty_master) {
2077 if (--o_tty->count < 0) { 1661 if (--o_tty->count < 0) {
2078 printk(KERN_WARNING "release_dev: bad pty slave count " 1662 printk(KERN_WARNING "tty_release_dev: bad pty slave count "
2079 "(%d) for %s\n", 1663 "(%d) for %s\n",
2080 o_tty->count, tty_name(o_tty, buf)); 1664 o_tty->count, tty_name(o_tty, buf));
2081 o_tty->count = 0; 1665 o_tty->count = 0;
2082 } 1666 }
2083 } 1667 }
2084 if (--tty->count < 0) { 1668 if (--tty->count < 0) {
2085 printk(KERN_WARNING "release_dev: bad tty->count (%d) for %s\n", 1669 printk(KERN_WARNING "tty_release_dev: bad tty->count (%d) for %s\n",
2086 tty->count, tty_name(tty, buf)); 1670 tty->count, tty_name(tty, buf));
2087 tty->count = 0; 1671 tty->count = 0;
2088 } 1672 }
@@ -2145,11 +1729,11 @@ static void release_dev(struct file *filp)
2145 1729
2146 /* Make this pty number available for reallocation */ 1730 /* Make this pty number available for reallocation */
2147 if (devpts) 1731 if (devpts)
2148 devpts_kill_index(idx); 1732 devpts_kill_index(inode, idx);
2149} 1733}
2150 1734
2151/** 1735/**
2152 * tty_open - open a tty device 1736 * __tty_open - open a tty device
2153 * @inode: inode of device file 1737 * @inode: inode of device file
2154 * @filp: file pointer to tty 1738 * @filp: file pointer to tty
2155 * 1739 *
@@ -2164,14 +1748,14 @@ static void release_dev(struct file *filp)
2164 * The termios state of a pty is reset on first open so that 1748 * The termios state of a pty is reset on first open so that
2165 * settings don't persist across reuse. 1749 * settings don't persist across reuse.
2166 * 1750 *
2167 * Locking: tty_mutex protects tty, get_tty_driver and init_dev work. 1751 * Locking: tty_mutex protects tty, get_tty_driver and tty_init_dev work.
2168 * tty->count should protect the rest. 1752 * tty->count should protect the rest.
2169 * ->siglock protects ->signal/->sighand 1753 * ->siglock protects ->signal/->sighand
2170 */ 1754 */
2171 1755
2172static int __tty_open(struct inode *inode, struct file *filp) 1756static int __tty_open(struct inode *inode, struct file *filp)
2173{ 1757{
2174 struct tty_struct *tty; 1758 struct tty_struct *tty = NULL;
2175 int noctty, retval; 1759 int noctty, retval;
2176 struct tty_driver *driver; 1760 struct tty_driver *driver;
2177 int index; 1761 int index;
@@ -2193,23 +1777,25 @@ retry_open:
2193 mutex_unlock(&tty_mutex); 1777 mutex_unlock(&tty_mutex);
2194 return -ENXIO; 1778 return -ENXIO;
2195 } 1779 }
2196 driver = tty->driver; 1780 driver = tty_driver_kref_get(tty->driver);
2197 index = tty->index; 1781 index = tty->index;
2198 filp->f_flags |= O_NONBLOCK; /* Don't let /dev/tty block */ 1782 filp->f_flags |= O_NONBLOCK; /* Don't let /dev/tty block */
2199 /* noctty = 1; */ 1783 /* noctty = 1; */
1784 /* FIXME: Should we take a driver reference ? */
1785 tty_kref_put(tty);
2200 goto got_driver; 1786 goto got_driver;
2201 } 1787 }
2202#ifdef CONFIG_VT 1788#ifdef CONFIG_VT
2203 if (device == MKDEV(TTY_MAJOR, 0)) { 1789 if (device == MKDEV(TTY_MAJOR, 0)) {
2204 extern struct tty_driver *console_driver; 1790 extern struct tty_driver *console_driver;
2205 driver = console_driver; 1791 driver = tty_driver_kref_get(console_driver);
2206 index = fg_console; 1792 index = fg_console;
2207 noctty = 1; 1793 noctty = 1;
2208 goto got_driver; 1794 goto got_driver;
2209 } 1795 }
2210#endif 1796#endif
2211 if (device == MKDEV(TTYAUX_MAJOR, 1)) { 1797 if (device == MKDEV(TTYAUX_MAJOR, 1)) {
2212 driver = console_device(&index); 1798 driver = tty_driver_kref_get(console_device(&index));
2213 if (driver) { 1799 if (driver) {
2214 /* Don't let /dev/console block */ 1800 /* Don't let /dev/console block */
2215 filp->f_flags |= O_NONBLOCK; 1801 filp->f_flags |= O_NONBLOCK;
@@ -2226,10 +1812,25 @@ retry_open:
2226 return -ENODEV; 1812 return -ENODEV;
2227 } 1813 }
2228got_driver: 1814got_driver:
2229 retval = init_dev(driver, index, &tty); 1815 if (!tty) {
1816 /* check whether we're reopening an existing tty */
1817 tty = tty_driver_lookup_tty(driver, inode, index);
1818
1819 if (IS_ERR(tty))
1820 return PTR_ERR(tty);
1821 }
1822
1823 if (tty) {
1824 retval = tty_reopen(tty);
1825 if (retval)
1826 tty = ERR_PTR(retval);
1827 } else
1828 tty = tty_init_dev(driver, index, 0);
1829
2230 mutex_unlock(&tty_mutex); 1830 mutex_unlock(&tty_mutex);
2231 if (retval) 1831 tty_driver_kref_put(driver);
2232 return retval; 1832 if (IS_ERR(tty))
1833 return PTR_ERR(tty);
2233 1834
2234 filp->private_data = tty; 1835 filp->private_data = tty;
2235 file_move(filp, &tty->tty_files); 1836 file_move(filp, &tty->tty_files);
@@ -2257,7 +1858,7 @@ got_driver:
2257 printk(KERN_DEBUG "error %d in opening %s...", retval, 1858 printk(KERN_DEBUG "error %d in opening %s...", retval,
2258 tty->name); 1859 tty->name);
2259#endif 1860#endif
2260 release_dev(filp); 1861 tty_release_dev(filp);
2261 if (retval != -ERESTARTSYS) 1862 if (retval != -ERESTARTSYS)
2262 return retval; 1863 return retval;
2263 if (signal_pending(current)) 1864 if (signal_pending(current))
@@ -2296,69 +1897,6 @@ static int tty_open(struct inode *inode, struct file *filp)
2296 1897
2297 1898
2298 1899
2299#ifdef CONFIG_UNIX98_PTYS
2300/**
2301 * ptmx_open - open a unix 98 pty master
2302 * @inode: inode of device file
2303 * @filp: file pointer to tty
2304 *
2305 * Allocate a unix98 pty master device from the ptmx driver.
2306 *
2307 * Locking: tty_mutex protects theinit_dev work. tty->count should
2308 * protect the rest.
2309 * allocated_ptys_lock handles the list of free pty numbers
2310 */
2311
2312static int __ptmx_open(struct inode *inode, struct file *filp)
2313{
2314 struct tty_struct *tty;
2315 int retval;
2316 int index;
2317
2318 nonseekable_open(inode, filp);
2319
2320 /* find a device that is not in use. */
2321 index = devpts_new_index();
2322 if (index < 0)
2323 return index;
2324
2325 mutex_lock(&tty_mutex);
2326 retval = init_dev(ptm_driver, index, &tty);
2327 mutex_unlock(&tty_mutex);
2328
2329 if (retval)
2330 goto out;
2331
2332 set_bit(TTY_PTY_LOCK, &tty->flags); /* LOCK THE SLAVE */
2333 filp->private_data = tty;
2334 file_move(filp, &tty->tty_files);
2335
2336 retval = devpts_pty_new(tty->link);
2337 if (retval)
2338 goto out1;
2339
2340 check_tty_count(tty, "ptmx_open");
2341 retval = ptm_driver->ops->open(tty, filp);
2342 if (!retval)
2343 return 0;
2344out1:
2345 release_dev(filp);
2346 return retval;
2347out:
2348 devpts_kill_index(index);
2349 return retval;
2350}
2351
2352static int ptmx_open(struct inode *inode, struct file *filp)
2353{
2354 int ret;
2355
2356 lock_kernel();
2357 ret = __ptmx_open(inode, filp);
2358 unlock_kernel();
2359 return ret;
2360}
2361#endif
2362 1900
2363/** 1901/**
2364 * tty_release - vfs callback for close 1902 * tty_release - vfs callback for close
@@ -2369,13 +1907,13 @@ static int ptmx_open(struct inode *inode, struct file *filp)
2369 * this tty. There may however be several such references. 1907 * this tty. There may however be several such references.
2370 * 1908 *
2371 * Locking: 1909 * Locking:
2372 * Takes bkl. See release_dev 1910 * Takes bkl. See tty_release_dev
2373 */ 1911 */
2374 1912
2375static int tty_release(struct inode *inode, struct file *filp) 1913static int tty_release(struct inode *inode, struct file *filp)
2376{ 1914{
2377 lock_kernel(); 1915 lock_kernel();
2378 release_dev(filp); 1916 tty_release_dev(filp);
2379 unlock_kernel(); 1917 unlock_kernel();
2380 return 0; 1918 return 0;
2381} 1919}
@@ -2524,7 +2062,7 @@ int tty_do_resize(struct tty_struct *tty, struct tty_struct *real_tty,
2524 2062
2525 /* For a PTY we need to lock the tty side */ 2063 /* For a PTY we need to lock the tty side */
2526 mutex_lock(&real_tty->termios_mutex); 2064 mutex_lock(&real_tty->termios_mutex);
2527 if (!memcmp(ws, &tty->winsize, sizeof(*ws))) 2065 if (!memcmp(ws, &real_tty->winsize, sizeof(*ws)))
2528 goto done; 2066 goto done;
2529 /* Get the PID values and reference them so we can 2067 /* Get the PID values and reference them so we can
2530 avoid holding the tty ctrl lock while sending signals */ 2068 avoid holding the tty ctrl lock while sending signals */
@@ -2996,7 +2534,7 @@ long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2996 case TIOCSTI: 2534 case TIOCSTI:
2997 return tiocsti(tty, p); 2535 return tiocsti(tty, p);
2998 case TIOCGWINSZ: 2536 case TIOCGWINSZ:
2999 return tiocgwinsz(tty, p); 2537 return tiocgwinsz(real_tty, p);
3000 case TIOCSWINSZ: 2538 case TIOCSWINSZ:
3001 return tiocswinsz(tty, real_tty, p); 2539 return tiocswinsz(tty, real_tty, p);
3002 case TIOCCONS: 2540 case TIOCCONS:
@@ -3026,10 +2564,6 @@ long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3026 return put_user(tty->ldisc.ops->num, (int __user *)p); 2564 return put_user(tty->ldisc.ops->num, (int __user *)p);
3027 case TIOCSETD: 2565 case TIOCSETD:
3028 return tiocsetd(tty, p); 2566 return tiocsetd(tty, p);
3029#ifdef CONFIG_VT
3030 case TIOCLINUX:
3031 return tioclinux(tty, arg);
3032#endif
3033 /* 2567 /*
3034 * Break handling 2568 * Break handling
3035 */ 2569 */
@@ -3220,113 +2754,6 @@ void do_SAK(struct tty_struct *tty)
3220EXPORT_SYMBOL(do_SAK); 2754EXPORT_SYMBOL(do_SAK);
3221 2755
3222/** 2756/**
3223 * flush_to_ldisc
3224 * @work: tty structure passed from work queue.
3225 *
3226 * This routine is called out of the software interrupt to flush data
3227 * from the buffer chain to the line discipline.
3228 *
3229 * Locking: holds tty->buf.lock to guard buffer list. Drops the lock
3230 * while invoking the line discipline receive_buf method. The
3231 * receive_buf method is single threaded for each tty instance.
3232 */
3233
3234static void flush_to_ldisc(struct work_struct *work)
3235{
3236 struct tty_struct *tty =
3237 container_of(work, struct tty_struct, buf.work.work);
3238 unsigned long flags;
3239 struct tty_ldisc *disc;
3240 struct tty_buffer *tbuf, *head;
3241 char *char_buf;
3242 unsigned char *flag_buf;
3243
3244 disc = tty_ldisc_ref(tty);
3245 if (disc == NULL) /* !TTY_LDISC */
3246 return;
3247
3248 spin_lock_irqsave(&tty->buf.lock, flags);
3249 /* So we know a flush is running */
3250 set_bit(TTY_FLUSHING, &tty->flags);
3251 head = tty->buf.head;
3252 if (head != NULL) {
3253 tty->buf.head = NULL;
3254 for (;;) {
3255 int count = head->commit - head->read;
3256 if (!count) {
3257 if (head->next == NULL)
3258 break;
3259 tbuf = head;
3260 head = head->next;
3261 tty_buffer_free(tty, tbuf);
3262 continue;
3263 }
3264 /* Ldisc or user is trying to flush the buffers
3265 we are feeding to the ldisc, stop feeding the
3266 line discipline as we want to empty the queue */
3267 if (test_bit(TTY_FLUSHPENDING, &tty->flags))
3268 break;
3269 if (!tty->receive_room) {
3270 schedule_delayed_work(&tty->buf.work, 1);
3271 break;
3272 }
3273 if (count > tty->receive_room)
3274 count = tty->receive_room;
3275 char_buf = head->char_buf_ptr + head->read;
3276 flag_buf = head->flag_buf_ptr + head->read;
3277 head->read += count;
3278 spin_unlock_irqrestore(&tty->buf.lock, flags);
3279 disc->ops->receive_buf(tty, char_buf,
3280 flag_buf, count);
3281 spin_lock_irqsave(&tty->buf.lock, flags);
3282 }
3283 /* Restore the queue head */
3284 tty->buf.head = head;
3285 }
3286 /* We may have a deferred request to flush the input buffer,
3287 if so pull the chain under the lock and empty the queue */
3288 if (test_bit(TTY_FLUSHPENDING, &tty->flags)) {
3289 __tty_buffer_flush(tty);
3290 clear_bit(TTY_FLUSHPENDING, &tty->flags);
3291 wake_up(&tty->read_wait);
3292 }
3293 clear_bit(TTY_FLUSHING, &tty->flags);
3294 spin_unlock_irqrestore(&tty->buf.lock, flags);
3295
3296 tty_ldisc_deref(disc);
3297}
3298
3299/**
3300 * tty_flip_buffer_push - terminal
3301 * @tty: tty to push
3302 *
3303 * Queue a push of the terminal flip buffers to the line discipline. This
3304 * function must not be called from IRQ context if tty->low_latency is set.
3305 *
3306 * In the event of the queue being busy for flipping the work will be
3307 * held off and retried later.
3308 *
3309 * Locking: tty buffer lock. Driver locks in low latency mode.
3310 */
3311
3312void tty_flip_buffer_push(struct tty_struct *tty)
3313{
3314 unsigned long flags;
3315 spin_lock_irqsave(&tty->buf.lock, flags);
3316 if (tty->buf.tail != NULL)
3317 tty->buf.tail->commit = tty->buf.tail->used;
3318 spin_unlock_irqrestore(&tty->buf.lock, flags);
3319
3320 if (tty->low_latency)
3321 flush_to_ldisc(&tty->buf.work.work);
3322 else
3323 schedule_delayed_work(&tty->buf.work, 1);
3324}
3325
3326EXPORT_SYMBOL(tty_flip_buffer_push);
3327
3328
3329/**
3330 * initialize_tty_struct 2757 * initialize_tty_struct
3331 * @tty: tty to initialize 2758 * @tty: tty to initialize
3332 * 2759 *
@@ -3336,9 +2763,11 @@ EXPORT_SYMBOL(tty_flip_buffer_push);
3336 * Locking: none - tty in question must not be exposed at this point 2763 * Locking: none - tty in question must not be exposed at this point
3337 */ 2764 */
3338 2765
3339static void initialize_tty_struct(struct tty_struct *tty) 2766void initialize_tty_struct(struct tty_struct *tty,
2767 struct tty_driver *driver, int idx)
3340{ 2768{
3341 memset(tty, 0, sizeof(struct tty_struct)); 2769 memset(tty, 0, sizeof(struct tty_struct));
2770 kref_init(&tty->kref);
3342 tty->magic = TTY_MAGIC; 2771 tty->magic = TTY_MAGIC;
3343 tty_ldisc_init(tty); 2772 tty_ldisc_init(tty);
3344 tty->session = NULL; 2773 tty->session = NULL;
@@ -3346,7 +2775,6 @@ static void initialize_tty_struct(struct tty_struct *tty)
3346 tty->overrun_time = jiffies; 2775 tty->overrun_time = jiffies;
3347 tty->buf.head = tty->buf.tail = NULL; 2776 tty->buf.head = tty->buf.tail = NULL;
3348 tty_buffer_init(tty); 2777 tty_buffer_init(tty);
3349 INIT_DELAYED_WORK(&tty->buf.work, flush_to_ldisc);
3350 mutex_init(&tty->termios_mutex); 2778 mutex_init(&tty->termios_mutex);
3351 init_waitqueue_head(&tty->write_wait); 2779 init_waitqueue_head(&tty->write_wait);
3352 init_waitqueue_head(&tty->read_wait); 2780 init_waitqueue_head(&tty->read_wait);
@@ -3357,6 +2785,11 @@ static void initialize_tty_struct(struct tty_struct *tty)
3357 spin_lock_init(&tty->ctrl_lock); 2785 spin_lock_init(&tty->ctrl_lock);
3358 INIT_LIST_HEAD(&tty->tty_files); 2786 INIT_LIST_HEAD(&tty->tty_files);
3359 INIT_WORK(&tty->SAK_work, do_SAK_work); 2787 INIT_WORK(&tty->SAK_work, do_SAK_work);
2788
2789 tty->driver = driver;
2790 tty->ops = driver->ops;
2791 tty->index = idx;
2792 tty_line_name(driver, idx, tty->name);
3360} 2793}
3361 2794
3362/** 2795/**
@@ -3377,10 +2810,9 @@ int tty_put_char(struct tty_struct *tty, unsigned char ch)
3377 return tty->ops->put_char(tty, ch); 2810 return tty->ops->put_char(tty, ch);
3378 return tty->ops->write(tty, &ch, 1); 2811 return tty->ops->write(tty, &ch, 1);
3379} 2812}
3380
3381EXPORT_SYMBOL_GPL(tty_put_char); 2813EXPORT_SYMBOL_GPL(tty_put_char);
3382 2814
3383static struct class *tty_class; 2815struct class *tty_class;
3384 2816
3385/** 2817/**
3386 * tty_register_device - register a tty device 2818 * tty_register_device - register a tty device
@@ -3420,6 +2852,7 @@ struct device *tty_register_device(struct tty_driver *driver, unsigned index,
3420 2852
3421 return device_create_drvdata(tty_class, device, dev, NULL, name); 2853 return device_create_drvdata(tty_class, device, dev, NULL, name);
3422} 2854}
2855EXPORT_SYMBOL(tty_register_device);
3423 2856
3424/** 2857/**
3425 * tty_unregister_device - unregister a tty device 2858 * tty_unregister_device - unregister a tty device
@@ -3437,8 +2870,6 @@ void tty_unregister_device(struct tty_driver *driver, unsigned index)
3437 device_destroy(tty_class, 2870 device_destroy(tty_class,
3438 MKDEV(driver->major, driver->minor_start) + index); 2871 MKDEV(driver->major, driver->minor_start) + index);
3439} 2872}
3440
3441EXPORT_SYMBOL(tty_register_device);
3442EXPORT_SYMBOL(tty_unregister_device); 2873EXPORT_SYMBOL(tty_unregister_device);
3443 2874
3444struct tty_driver *alloc_tty_driver(int lines) 2875struct tty_driver *alloc_tty_driver(int lines)
@@ -3447,27 +2878,65 @@ struct tty_driver *alloc_tty_driver(int lines)
3447 2878
3448 driver = kzalloc(sizeof(struct tty_driver), GFP_KERNEL); 2879 driver = kzalloc(sizeof(struct tty_driver), GFP_KERNEL);
3449 if (driver) { 2880 if (driver) {
2881 kref_init(&driver->kref);
3450 driver->magic = TTY_DRIVER_MAGIC; 2882 driver->magic = TTY_DRIVER_MAGIC;
3451 driver->num = lines; 2883 driver->num = lines;
3452 /* later we'll move allocation of tables here */ 2884 /* later we'll move allocation of tables here */
3453 } 2885 }
3454 return driver; 2886 return driver;
3455} 2887}
2888EXPORT_SYMBOL(alloc_tty_driver);
3456 2889
3457void put_tty_driver(struct tty_driver *driver) 2890static void destruct_tty_driver(struct kref *kref)
3458{ 2891{
2892 struct tty_driver *driver = container_of(kref, struct tty_driver, kref);
2893 int i;
2894 struct ktermios *tp;
2895 void *p;
2896
2897 if (driver->flags & TTY_DRIVER_INSTALLED) {
2898 /*
2899 * Free the termios and termios_locked structures because
2900 * we don't want to get memory leaks when modular tty
2901 * drivers are removed from the kernel.
2902 */
2903 for (i = 0; i < driver->num; i++) {
2904 tp = driver->termios[i];
2905 if (tp) {
2906 driver->termios[i] = NULL;
2907 kfree(tp);
2908 }
2909 if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV))
2910 tty_unregister_device(driver, i);
2911 }
2912 p = driver->ttys;
2913 proc_tty_unregister_driver(driver);
2914 driver->ttys = NULL;
2915 driver->termios = NULL;
2916 kfree(p);
2917 cdev_del(&driver->cdev);
2918 }
3459 kfree(driver); 2919 kfree(driver);
3460} 2920}
3461 2921
2922void tty_driver_kref_put(struct tty_driver *driver)
2923{
2924 kref_put(&driver->kref, destruct_tty_driver);
2925}
2926EXPORT_SYMBOL(tty_driver_kref_put);
2927
3462void tty_set_operations(struct tty_driver *driver, 2928void tty_set_operations(struct tty_driver *driver,
3463 const struct tty_operations *op) 2929 const struct tty_operations *op)
3464{ 2930{
3465 driver->ops = op; 2931 driver->ops = op;
3466}; 2932};
2933EXPORT_SYMBOL(tty_set_operations);
3467 2934
3468EXPORT_SYMBOL(alloc_tty_driver); 2935void put_tty_driver(struct tty_driver *d)
2936{
2937 tty_driver_kref_put(d);
2938}
3469EXPORT_SYMBOL(put_tty_driver); 2939EXPORT_SYMBOL(put_tty_driver);
3470EXPORT_SYMBOL(tty_set_operations);
3471 2940
3472/* 2941/*
3473 * Called by a tty driver to register itself. 2942 * Called by a tty driver to register itself.
@@ -3479,11 +2948,8 @@ int tty_register_driver(struct tty_driver *driver)
3479 dev_t dev; 2948 dev_t dev;
3480 void **p = NULL; 2949 void **p = NULL;
3481 2950
3482 if (driver->flags & TTY_DRIVER_INSTALLED)
3483 return 0;
3484
3485 if (!(driver->flags & TTY_DRIVER_DEVPTS_MEM) && driver->num) { 2951 if (!(driver->flags & TTY_DRIVER_DEVPTS_MEM) && driver->num) {
3486 p = kzalloc(driver->num * 3 * sizeof(void *), GFP_KERNEL); 2952 p = kzalloc(driver->num * 2 * sizeof(void *), GFP_KERNEL);
3487 if (!p) 2953 if (!p)
3488 return -ENOMEM; 2954 return -ENOMEM;
3489 } 2955 }
@@ -3507,12 +2973,9 @@ int tty_register_driver(struct tty_driver *driver)
3507 if (p) { 2973 if (p) {
3508 driver->ttys = (struct tty_struct **)p; 2974 driver->ttys = (struct tty_struct **)p;
3509 driver->termios = (struct ktermios **)(p + driver->num); 2975 driver->termios = (struct ktermios **)(p + driver->num);
3510 driver->termios_locked = (struct ktermios **)
3511 (p + driver->num * 2);
3512 } else { 2976 } else {
3513 driver->ttys = NULL; 2977 driver->ttys = NULL;
3514 driver->termios = NULL; 2978 driver->termios = NULL;
3515 driver->termios_locked = NULL;
3516 } 2979 }
3517 2980
3518 cdev_init(&driver->cdev, &tty_fops); 2981 cdev_init(&driver->cdev, &tty_fops);
@@ -3521,7 +2984,7 @@ int tty_register_driver(struct tty_driver *driver)
3521 if (error) { 2984 if (error) {
3522 unregister_chrdev_region(dev, driver->num); 2985 unregister_chrdev_region(dev, driver->num);
3523 driver->ttys = NULL; 2986 driver->ttys = NULL;
3524 driver->termios = driver->termios_locked = NULL; 2987 driver->termios = NULL;
3525 kfree(p); 2988 kfree(p);
3526 return error; 2989 return error;
3527 } 2990 }
@@ -3535,6 +2998,7 @@ int tty_register_driver(struct tty_driver *driver)
3535 tty_register_device(driver, i, NULL); 2998 tty_register_device(driver, i, NULL);
3536 } 2999 }
3537 proc_tty_register_driver(driver); 3000 proc_tty_register_driver(driver);
3001 driver->flags |= TTY_DRIVER_INSTALLED;
3538 return 0; 3002 return 0;
3539} 3003}
3540 3004
@@ -3545,46 +3009,19 @@ EXPORT_SYMBOL(tty_register_driver);
3545 */ 3009 */
3546int tty_unregister_driver(struct tty_driver *driver) 3010int tty_unregister_driver(struct tty_driver *driver)
3547{ 3011{
3548 int i; 3012#if 0
3549 struct ktermios *tp; 3013 /* FIXME */
3550 void *p;
3551
3552 if (driver->refcount) 3014 if (driver->refcount)
3553 return -EBUSY; 3015 return -EBUSY;
3554 3016#endif
3555 unregister_chrdev_region(MKDEV(driver->major, driver->minor_start), 3017 unregister_chrdev_region(MKDEV(driver->major, driver->minor_start),
3556 driver->num); 3018 driver->num);
3557 mutex_lock(&tty_mutex); 3019 mutex_lock(&tty_mutex);
3558 list_del(&driver->tty_drivers); 3020 list_del(&driver->tty_drivers);
3559 mutex_unlock(&tty_mutex); 3021 mutex_unlock(&tty_mutex);
3560
3561 /*
3562 * Free the termios and termios_locked structures because
3563 * we don't want to get memory leaks when modular tty
3564 * drivers are removed from the kernel.
3565 */
3566 for (i = 0; i < driver->num; i++) {
3567 tp = driver->termios[i];
3568 if (tp) {
3569 driver->termios[i] = NULL;
3570 kfree(tp);
3571 }
3572 tp = driver->termios_locked[i];
3573 if (tp) {
3574 driver->termios_locked[i] = NULL;
3575 kfree(tp);
3576 }
3577 if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV))
3578 tty_unregister_device(driver, i);
3579 }
3580 p = driver->ttys;
3581 proc_tty_unregister_driver(driver);
3582 driver->ttys = NULL;
3583 driver->termios = driver->termios_locked = NULL;
3584 kfree(p);
3585 cdev_del(&driver->cdev);
3586 return 0; 3022 return 0;
3587} 3023}
3024
3588EXPORT_SYMBOL(tty_unregister_driver); 3025EXPORT_SYMBOL(tty_unregister_driver);
3589 3026
3590dev_t tty_devnum(struct tty_struct *tty) 3027dev_t tty_devnum(struct tty_struct *tty)
@@ -3595,9 +3032,12 @@ EXPORT_SYMBOL(tty_devnum);
3595 3032
3596void proc_clear_tty(struct task_struct *p) 3033void proc_clear_tty(struct task_struct *p)
3597{ 3034{
3035 struct tty_struct *tty;
3598 spin_lock_irq(&p->sighand->siglock); 3036 spin_lock_irq(&p->sighand->siglock);
3037 tty = p->signal->tty;
3599 p->signal->tty = NULL; 3038 p->signal->tty = NULL;
3600 spin_unlock_irq(&p->sighand->siglock); 3039 spin_unlock_irq(&p->sighand->siglock);
3040 tty_kref_put(tty);
3601} 3041}
3602 3042
3603/* Called under the sighand lock */ 3043/* Called under the sighand lock */
@@ -3613,9 +3053,13 @@ static void __proc_set_tty(struct task_struct *tsk, struct tty_struct *tty)
3613 tty->pgrp = get_pid(task_pgrp(tsk)); 3053 tty->pgrp = get_pid(task_pgrp(tsk));
3614 spin_unlock_irqrestore(&tty->ctrl_lock, flags); 3054 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
3615 tty->session = get_pid(task_session(tsk)); 3055 tty->session = get_pid(task_session(tsk));
3056 if (tsk->signal->tty) {
3057 printk(KERN_DEBUG "tty not NULL!!\n");
3058 tty_kref_put(tsk->signal->tty);
3059 }
3616 } 3060 }
3617 put_pid(tsk->signal->tty_old_pgrp); 3061 put_pid(tsk->signal->tty_old_pgrp);
3618 tsk->signal->tty = tty; 3062 tsk->signal->tty = tty_kref_get(tty);
3619 tsk->signal->tty_old_pgrp = NULL; 3063 tsk->signal->tty_old_pgrp = NULL;
3620} 3064}
3621 3065
@@ -3629,18 +3073,20 @@ static void proc_set_tty(struct task_struct *tsk, struct tty_struct *tty)
3629struct tty_struct *get_current_tty(void) 3073struct tty_struct *get_current_tty(void)
3630{ 3074{
3631 struct tty_struct *tty; 3075 struct tty_struct *tty;
3632 WARN_ON_ONCE(!mutex_is_locked(&tty_mutex)); 3076 unsigned long flags;
3633 tty = current->signal->tty; 3077
3634 /* 3078 spin_lock_irqsave(&current->sighand->siglock, flags);
3635 * session->tty can be changed/cleared from under us, make sure we 3079 tty = tty_kref_get(current->signal->tty);
3636 * issue the load. The obtained pointer, when not NULL, is valid as 3080 spin_unlock_irqrestore(&current->sighand->siglock, flags);
3637 * long as we hold tty_mutex.
3638 */
3639 barrier();
3640 return tty; 3081 return tty;
3641} 3082}
3642EXPORT_SYMBOL_GPL(get_current_tty); 3083EXPORT_SYMBOL_GPL(get_current_tty);
3643 3084
3085void tty_default_fops(struct file_operations *fops)
3086{
3087 *fops = tty_fops;
3088}
3089
3644/* 3090/*
3645 * Initialize the console device. This is called *early*, so 3091 * Initialize the console device. This is called *early*, so
3646 * we can't necessarily depend on lots of kernel help here. 3092 * we can't necessarily depend on lots of kernel help here.
@@ -3678,12 +3124,6 @@ postcore_initcall(tty_class_init);
3678/* 3/2004 jmc: why do these devices exist? */ 3124/* 3/2004 jmc: why do these devices exist? */
3679 3125
3680static struct cdev tty_cdev, console_cdev; 3126static struct cdev tty_cdev, console_cdev;
3681#ifdef CONFIG_UNIX98_PTYS
3682static struct cdev ptmx_cdev;
3683#endif
3684#ifdef CONFIG_VT
3685static struct cdev vc0_cdev;
3686#endif
3687 3127
3688/* 3128/*
3689 * Ok, now we can initialize the rest of the tty devices and can count 3129 * Ok, now we can initialize the rest of the tty devices and can count
@@ -3695,32 +3135,18 @@ static int __init tty_init(void)
3695 if (cdev_add(&tty_cdev, MKDEV(TTYAUX_MAJOR, 0), 1) || 3135 if (cdev_add(&tty_cdev, MKDEV(TTYAUX_MAJOR, 0), 1) ||
3696 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 0), 1, "/dev/tty") < 0) 3136 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 0), 1, "/dev/tty") < 0)
3697 panic("Couldn't register /dev/tty driver\n"); 3137 panic("Couldn't register /dev/tty driver\n");
3698 device_create_drvdata(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 0), NULL, 3138 device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 0), NULL,
3699 "tty"); 3139 "tty");
3700 3140
3701 cdev_init(&console_cdev, &console_fops); 3141 cdev_init(&console_cdev, &console_fops);
3702 if (cdev_add(&console_cdev, MKDEV(TTYAUX_MAJOR, 1), 1) || 3142 if (cdev_add(&console_cdev, MKDEV(TTYAUX_MAJOR, 1), 1) ||
3703 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 1), 1, "/dev/console") < 0) 3143 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 1), 1, "/dev/console") < 0)
3704 panic("Couldn't register /dev/console driver\n"); 3144 panic("Couldn't register /dev/console driver\n");
3705 device_create_drvdata(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 1), NULL, 3145 device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 1), NULL,
3706 "console"); 3146 "console");
3707 3147
3708#ifdef CONFIG_UNIX98_PTYS
3709 cdev_init(&ptmx_cdev, &ptmx_fops);
3710 if (cdev_add(&ptmx_cdev, MKDEV(TTYAUX_MAJOR, 2), 1) ||
3711 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 2), 1, "/dev/ptmx") < 0)
3712 panic("Couldn't register /dev/ptmx driver\n");
3713 device_create_drvdata(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 2), NULL, "ptmx");
3714#endif
3715
3716#ifdef CONFIG_VT 3148#ifdef CONFIG_VT
3717 cdev_init(&vc0_cdev, &console_fops); 3149 vty_init(&console_fops);
3718 if (cdev_add(&vc0_cdev, MKDEV(TTY_MAJOR, 0), 1) ||
3719 register_chrdev_region(MKDEV(TTY_MAJOR, 0), 1, "/dev/vc/0") < 0)
3720 panic("Couldn't register /dev/tty0 driver\n");
3721 device_create_drvdata(tty_class, NULL, MKDEV(TTY_MAJOR, 0), NULL, "tty0");
3722
3723 vty_init();
3724#endif 3150#endif
3725 return 0; 3151 return 0;
3726} 3152}