diff options
Diffstat (limited to 'net')
-rw-r--r-- | net/9p/trans_fd.c | 112 | ||||
-rw-r--r-- | net/socket.c | 108 |
2 files changed, 82 insertions, 138 deletions
diff --git a/net/9p/trans_fd.c b/net/9p/trans_fd.c index 4dd873e3a1bb..be1cb909d8c0 100644 --- a/net/9p/trans_fd.c +++ b/net/9p/trans_fd.c | |||
@@ -42,6 +42,8 @@ | |||
42 | #include <net/9p/client.h> | 42 | #include <net/9p/client.h> |
43 | #include <net/9p/transport.h> | 43 | #include <net/9p/transport.h> |
44 | 44 | ||
45 | #include <linux/syscalls.h> /* killme */ | ||
46 | |||
45 | #define P9_PORT 564 | 47 | #define P9_PORT 564 |
46 | #define MAX_SOCK_BUF (64*1024) | 48 | #define MAX_SOCK_BUF (64*1024) |
47 | #define MAXPOLLWADDR 2 | 49 | #define MAXPOLLWADDR 2 |
@@ -788,24 +790,41 @@ static int p9_fd_open(struct p9_client *client, int rfd, int wfd) | |||
788 | 790 | ||
789 | static int p9_socket_open(struct p9_client *client, struct socket *csocket) | 791 | static int p9_socket_open(struct p9_client *client, struct socket *csocket) |
790 | { | 792 | { |
791 | int fd, ret; | 793 | struct p9_trans_fd *p; |
794 | int ret, fd; | ||
795 | |||
796 | p = kmalloc(sizeof(struct p9_trans_fd), GFP_KERNEL); | ||
797 | if (!p) | ||
798 | return -ENOMEM; | ||
792 | 799 | ||
793 | csocket->sk->sk_allocation = GFP_NOIO; | 800 | csocket->sk->sk_allocation = GFP_NOIO; |
794 | fd = sock_map_fd(csocket, 0); | 801 | fd = sock_map_fd(csocket, 0); |
795 | if (fd < 0) { | 802 | if (fd < 0) { |
796 | P9_EPRINTK(KERN_ERR, "p9_socket_open: failed to map fd\n"); | 803 | P9_EPRINTK(KERN_ERR, "p9_socket_open: failed to map fd\n"); |
804 | sock_release(csocket); | ||
805 | kfree(p); | ||
797 | return fd; | 806 | return fd; |
798 | } | 807 | } |
799 | 808 | ||
800 | ret = p9_fd_open(client, fd, fd); | 809 | get_file(csocket->file); |
801 | if (ret < 0) { | 810 | get_file(csocket->file); |
802 | P9_EPRINTK(KERN_ERR, "p9_socket_open: failed to open fd\n"); | 811 | p->wr = p->rd = csocket->file; |
812 | client->trans = p; | ||
813 | client->status = Connected; | ||
814 | |||
815 | sys_close(fd); /* still racy */ | ||
816 | |||
817 | p->rd->f_flags |= O_NONBLOCK; | ||
818 | |||
819 | p->conn = p9_conn_create(client); | ||
820 | if (IS_ERR(p->conn)) { | ||
821 | ret = PTR_ERR(p->conn); | ||
822 | p->conn = NULL; | ||
823 | kfree(p); | ||
824 | sockfd_put(csocket); | ||
803 | sockfd_put(csocket); | 825 | sockfd_put(csocket); |
804 | return ret; | 826 | return ret; |
805 | } | 827 | } |
806 | |||
807 | ((struct p9_trans_fd *)client->trans)->rd->f_flags |= O_NONBLOCK; | ||
808 | |||
809 | return 0; | 828 | return 0; |
810 | } | 829 | } |
811 | 830 | ||
@@ -883,7 +902,6 @@ p9_fd_create_tcp(struct p9_client *client, const char *addr, char *args) | |||
883 | struct socket *csocket; | 902 | struct socket *csocket; |
884 | struct sockaddr_in sin_server; | 903 | struct sockaddr_in sin_server; |
885 | struct p9_fd_opts opts; | 904 | struct p9_fd_opts opts; |
886 | struct p9_trans_fd *p = NULL; /* this gets allocated in p9_fd_open */ | ||
887 | 905 | ||
888 | err = parse_opts(args, &opts); | 906 | err = parse_opts(args, &opts); |
889 | if (err < 0) | 907 | if (err < 0) |
@@ -897,12 +915,11 @@ p9_fd_create_tcp(struct p9_client *client, const char *addr, char *args) | |||
897 | sin_server.sin_family = AF_INET; | 915 | sin_server.sin_family = AF_INET; |
898 | sin_server.sin_addr.s_addr = in_aton(addr); | 916 | sin_server.sin_addr.s_addr = in_aton(addr); |
899 | sin_server.sin_port = htons(opts.port); | 917 | sin_server.sin_port = htons(opts.port); |
900 | sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &csocket); | 918 | err = sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &csocket); |
901 | 919 | ||
902 | if (!csocket) { | 920 | if (err) { |
903 | P9_EPRINTK(KERN_ERR, "p9_trans_tcp: problem creating socket\n"); | 921 | P9_EPRINTK(KERN_ERR, "p9_trans_tcp: problem creating socket\n"); |
904 | err = -EIO; | 922 | return err; |
905 | goto error; | ||
906 | } | 923 | } |
907 | 924 | ||
908 | err = csocket->ops->connect(csocket, | 925 | err = csocket->ops->connect(csocket, |
@@ -912,30 +929,11 @@ p9_fd_create_tcp(struct p9_client *client, const char *addr, char *args) | |||
912 | P9_EPRINTK(KERN_ERR, | 929 | P9_EPRINTK(KERN_ERR, |
913 | "p9_trans_tcp: problem connecting socket to %s\n", | 930 | "p9_trans_tcp: problem connecting socket to %s\n", |
914 | addr); | 931 | addr); |
915 | goto error; | ||
916 | } | ||
917 | |||
918 | err = p9_socket_open(client, csocket); | ||
919 | if (err < 0) | ||
920 | goto error; | ||
921 | |||
922 | p = (struct p9_trans_fd *) client->trans; | ||
923 | p->conn = p9_conn_create(client); | ||
924 | if (IS_ERR(p->conn)) { | ||
925 | err = PTR_ERR(p->conn); | ||
926 | p->conn = NULL; | ||
927 | goto error; | ||
928 | } | ||
929 | |||
930 | return 0; | ||
931 | |||
932 | error: | ||
933 | if (csocket) | ||
934 | sock_release(csocket); | 932 | sock_release(csocket); |
933 | return err; | ||
934 | } | ||
935 | 935 | ||
936 | kfree(p); | 936 | return p9_socket_open(client, csocket); |
937 | |||
938 | return err; | ||
939 | } | 937 | } |
940 | 938 | ||
941 | static int | 939 | static int |
@@ -944,49 +942,33 @@ p9_fd_create_unix(struct p9_client *client, const char *addr, char *args) | |||
944 | int err; | 942 | int err; |
945 | struct socket *csocket; | 943 | struct socket *csocket; |
946 | struct sockaddr_un sun_server; | 944 | struct sockaddr_un sun_server; |
947 | struct p9_trans_fd *p = NULL; /* this gets allocated in p9_fd_open */ | ||
948 | 945 | ||
949 | csocket = NULL; | 946 | csocket = NULL; |
950 | 947 | ||
951 | if (strlen(addr) > UNIX_PATH_MAX) { | 948 | if (strlen(addr) > UNIX_PATH_MAX) { |
952 | P9_EPRINTK(KERN_ERR, "p9_trans_unix: address too long: %s\n", | 949 | P9_EPRINTK(KERN_ERR, "p9_trans_unix: address too long: %s\n", |
953 | addr); | 950 | addr); |
954 | err = -ENAMETOOLONG; | 951 | return -ENAMETOOLONG; |
955 | goto error; | ||
956 | } | 952 | } |
957 | 953 | ||
958 | sun_server.sun_family = PF_UNIX; | 954 | sun_server.sun_family = PF_UNIX; |
959 | strcpy(sun_server.sun_path, addr); | 955 | strcpy(sun_server.sun_path, addr); |
960 | sock_create_kern(PF_UNIX, SOCK_STREAM, 0, &csocket); | 956 | err = sock_create_kern(PF_UNIX, SOCK_STREAM, 0, &csocket); |
957 | if (err < 0) { | ||
958 | P9_EPRINTK(KERN_ERR, "p9_trans_unix: problem creating socket\n"); | ||
959 | return err; | ||
960 | } | ||
961 | err = csocket->ops->connect(csocket, (struct sockaddr *)&sun_server, | 961 | err = csocket->ops->connect(csocket, (struct sockaddr *)&sun_server, |
962 | sizeof(struct sockaddr_un) - 1, 0); | 962 | sizeof(struct sockaddr_un) - 1, 0); |
963 | if (err < 0) { | 963 | if (err < 0) { |
964 | P9_EPRINTK(KERN_ERR, | 964 | P9_EPRINTK(KERN_ERR, |
965 | "p9_trans_unix: problem connecting socket: %s: %d\n", | 965 | "p9_trans_unix: problem connecting socket: %s: %d\n", |
966 | addr, err); | 966 | addr, err); |
967 | goto error; | ||
968 | } | ||
969 | |||
970 | err = p9_socket_open(client, csocket); | ||
971 | if (err < 0) | ||
972 | goto error; | ||
973 | |||
974 | p = (struct p9_trans_fd *) client->trans; | ||
975 | p->conn = p9_conn_create(client); | ||
976 | if (IS_ERR(p->conn)) { | ||
977 | err = PTR_ERR(p->conn); | ||
978 | p->conn = NULL; | ||
979 | goto error; | ||
980 | } | ||
981 | |||
982 | return 0; | ||
983 | |||
984 | error: | ||
985 | if (csocket) | ||
986 | sock_release(csocket); | 967 | sock_release(csocket); |
968 | return err; | ||
969 | } | ||
987 | 970 | ||
988 | kfree(p); | 971 | return p9_socket_open(client, csocket); |
989 | return err; | ||
990 | } | 972 | } |
991 | 973 | ||
992 | static int | 974 | static int |
@@ -994,7 +976,7 @@ p9_fd_create(struct p9_client *client, const char *addr, char *args) | |||
994 | { | 976 | { |
995 | int err; | 977 | int err; |
996 | struct p9_fd_opts opts; | 978 | struct p9_fd_opts opts; |
997 | struct p9_trans_fd *p = NULL; /* this get allocated in p9_fd_open */ | 979 | struct p9_trans_fd *p; |
998 | 980 | ||
999 | parse_opts(args, &opts); | 981 | parse_opts(args, &opts); |
1000 | 982 | ||
@@ -1005,21 +987,19 @@ p9_fd_create(struct p9_client *client, const char *addr, char *args) | |||
1005 | 987 | ||
1006 | err = p9_fd_open(client, opts.rfd, opts.wfd); | 988 | err = p9_fd_open(client, opts.rfd, opts.wfd); |
1007 | if (err < 0) | 989 | if (err < 0) |
1008 | goto error; | 990 | return err; |
1009 | 991 | ||
1010 | p = (struct p9_trans_fd *) client->trans; | 992 | p = (struct p9_trans_fd *) client->trans; |
1011 | p->conn = p9_conn_create(client); | 993 | p->conn = p9_conn_create(client); |
1012 | if (IS_ERR(p->conn)) { | 994 | if (IS_ERR(p->conn)) { |
1013 | err = PTR_ERR(p->conn); | 995 | err = PTR_ERR(p->conn); |
1014 | p->conn = NULL; | 996 | p->conn = NULL; |
1015 | goto error; | 997 | fput(p->rd); |
998 | fput(p->wr); | ||
999 | return err; | ||
1016 | } | 1000 | } |
1017 | 1001 | ||
1018 | return 0; | 1002 | return 0; |
1019 | |||
1020 | error: | ||
1021 | kfree(p); | ||
1022 | return err; | ||
1023 | } | 1003 | } |
1024 | 1004 | ||
1025 | static struct p9_trans_module p9_tcp_trans = { | 1005 | static struct p9_trans_module p9_tcp_trans = { |
diff --git a/net/socket.c b/net/socket.c index b94c3dd71015..dbfdfa96d29b 100644 --- a/net/socket.c +++ b/net/socket.c | |||
@@ -355,68 +355,61 @@ static const struct dentry_operations sockfs_dentry_operations = { | |||
355 | * but we take care of internal coherence yet. | 355 | * but we take care of internal coherence yet. |
356 | */ | 356 | */ |
357 | 357 | ||
358 | static int sock_alloc_fd(struct file **filep, int flags) | 358 | static int sock_alloc_file(struct socket *sock, struct file **f, int flags) |
359 | { | 359 | { |
360 | struct qstr name = { .name = "" }; | ||
361 | struct path path; | ||
362 | struct file *file; | ||
360 | int fd; | 363 | int fd; |
361 | 364 | ||
362 | fd = get_unused_fd_flags(flags); | 365 | fd = get_unused_fd_flags(flags); |
363 | if (likely(fd >= 0)) { | 366 | if (unlikely(fd < 0)) |
364 | struct file *file = get_empty_filp(); | 367 | return fd; |
365 | |||
366 | *filep = file; | ||
367 | if (unlikely(!file)) { | ||
368 | put_unused_fd(fd); | ||
369 | return -ENFILE; | ||
370 | } | ||
371 | } else | ||
372 | *filep = NULL; | ||
373 | return fd; | ||
374 | } | ||
375 | |||
376 | static int sock_attach_fd(struct socket *sock, struct file *file, int flags) | ||
377 | { | ||
378 | struct dentry *dentry; | ||
379 | struct qstr name = { .name = "" }; | ||
380 | 368 | ||
381 | dentry = d_alloc(sock_mnt->mnt_sb->s_root, &name); | 369 | path.dentry = d_alloc(sock_mnt->mnt_sb->s_root, &name); |
382 | if (unlikely(!dentry)) | 370 | if (unlikely(!path.dentry)) { |
371 | put_unused_fd(fd); | ||
383 | return -ENOMEM; | 372 | return -ENOMEM; |
373 | } | ||
374 | path.mnt = mntget(sock_mnt); | ||
384 | 375 | ||
385 | dentry->d_op = &sockfs_dentry_operations; | 376 | path.dentry->d_op = &sockfs_dentry_operations; |
386 | /* | 377 | /* |
387 | * We dont want to push this dentry into global dentry hash table. | 378 | * We dont want to push this dentry into global dentry hash table. |
388 | * We pretend dentry is already hashed, by unsetting DCACHE_UNHASHED | 379 | * We pretend dentry is already hashed, by unsetting DCACHE_UNHASHED |
389 | * This permits a working /proc/$pid/fd/XXX on sockets | 380 | * This permits a working /proc/$pid/fd/XXX on sockets |
390 | */ | 381 | */ |
391 | dentry->d_flags &= ~DCACHE_UNHASHED; | 382 | path.dentry->d_flags &= ~DCACHE_UNHASHED; |
392 | d_instantiate(dentry, SOCK_INODE(sock)); | 383 | d_instantiate(path.dentry, SOCK_INODE(sock)); |
384 | SOCK_INODE(sock)->i_fop = &socket_file_ops; | ||
393 | 385 | ||
394 | sock->file = file; | 386 | file = alloc_file(&path, FMODE_READ | FMODE_WRITE, |
395 | init_file(file, sock_mnt, dentry, FMODE_READ | FMODE_WRITE, | ||
396 | &socket_file_ops); | 387 | &socket_file_ops); |
397 | SOCK_INODE(sock)->i_fop = &socket_file_ops; | 388 | if (unlikely(!file)) { |
389 | /* drop dentry, keep inode */ | ||
390 | atomic_inc(&path.dentry->d_inode->i_count); | ||
391 | path_put(&path); | ||
392 | put_unused_fd(fd); | ||
393 | return -ENFILE; | ||
394 | } | ||
395 | |||
396 | sock->file = file; | ||
398 | file->f_flags = O_RDWR | (flags & O_NONBLOCK); | 397 | file->f_flags = O_RDWR | (flags & O_NONBLOCK); |
399 | file->f_pos = 0; | 398 | file->f_pos = 0; |
400 | file->private_data = sock; | 399 | file->private_data = sock; |
401 | 400 | ||
402 | return 0; | 401 | *f = file; |
402 | return fd; | ||
403 | } | 403 | } |
404 | 404 | ||
405 | int sock_map_fd(struct socket *sock, int flags) | 405 | int sock_map_fd(struct socket *sock, int flags) |
406 | { | 406 | { |
407 | struct file *newfile; | 407 | struct file *newfile; |
408 | int fd = sock_alloc_fd(&newfile, flags); | 408 | int fd = sock_alloc_file(sock, &newfile, flags); |
409 | |||
410 | if (likely(fd >= 0)) { | ||
411 | int err = sock_attach_fd(sock, newfile, flags); | ||
412 | 409 | ||
413 | if (unlikely(err < 0)) { | 410 | if (likely(fd >= 0)) |
414 | put_filp(newfile); | ||
415 | put_unused_fd(fd); | ||
416 | return err; | ||
417 | } | ||
418 | fd_install(fd, newfile); | 411 | fd_install(fd, newfile); |
419 | } | 412 | |
420 | return fd; | 413 | return fd; |
421 | } | 414 | } |
422 | 415 | ||
@@ -1390,29 +1383,19 @@ SYSCALL_DEFINE4(socketpair, int, family, int, type, int, protocol, | |||
1390 | if (err < 0) | 1383 | if (err < 0) |
1391 | goto out_release_both; | 1384 | goto out_release_both; |
1392 | 1385 | ||
1393 | fd1 = sock_alloc_fd(&newfile1, flags & O_CLOEXEC); | 1386 | fd1 = sock_alloc_file(sock1, &newfile1, flags); |
1394 | if (unlikely(fd1 < 0)) { | 1387 | if (unlikely(fd1 < 0)) { |
1395 | err = fd1; | 1388 | err = fd1; |
1396 | goto out_release_both; | 1389 | goto out_release_both; |
1397 | } | 1390 | } |
1398 | 1391 | ||
1399 | fd2 = sock_alloc_fd(&newfile2, flags & O_CLOEXEC); | 1392 | fd2 = sock_alloc_file(sock2, &newfile2, flags); |
1400 | if (unlikely(fd2 < 0)) { | 1393 | if (unlikely(fd2 < 0)) { |
1401 | err = fd2; | 1394 | err = fd2; |
1402 | put_filp(newfile1); | ||
1403 | put_unused_fd(fd1); | ||
1404 | goto out_release_both; | ||
1405 | } | ||
1406 | |||
1407 | err = sock_attach_fd(sock1, newfile1, flags & O_NONBLOCK); | ||
1408 | if (unlikely(err < 0)) { | ||
1409 | goto out_fd2; | ||
1410 | } | ||
1411 | |||
1412 | err = sock_attach_fd(sock2, newfile2, flags & O_NONBLOCK); | ||
1413 | if (unlikely(err < 0)) { | ||
1414 | fput(newfile1); | 1395 | fput(newfile1); |
1415 | goto out_fd1; | 1396 | put_unused_fd(fd1); |
1397 | sock_release(sock2); | ||
1398 | goto out; | ||
1416 | } | 1399 | } |
1417 | 1400 | ||
1418 | audit_fd_pair(fd1, fd2); | 1401 | audit_fd_pair(fd1, fd2); |
@@ -1438,16 +1421,6 @@ out_release_1: | |||
1438 | sock_release(sock1); | 1421 | sock_release(sock1); |
1439 | out: | 1422 | out: |
1440 | return err; | 1423 | return err; |
1441 | |||
1442 | out_fd2: | ||
1443 | put_filp(newfile1); | ||
1444 | sock_release(sock1); | ||
1445 | out_fd1: | ||
1446 | put_filp(newfile2); | ||
1447 | sock_release(sock2); | ||
1448 | put_unused_fd(fd1); | ||
1449 | put_unused_fd(fd2); | ||
1450 | goto out; | ||
1451 | } | 1424 | } |
1452 | 1425 | ||
1453 | /* | 1426 | /* |
@@ -1551,17 +1524,13 @@ SYSCALL_DEFINE4(accept4, int, fd, struct sockaddr __user *, upeer_sockaddr, | |||
1551 | */ | 1524 | */ |
1552 | __module_get(newsock->ops->owner); | 1525 | __module_get(newsock->ops->owner); |
1553 | 1526 | ||
1554 | newfd = sock_alloc_fd(&newfile, flags & O_CLOEXEC); | 1527 | newfd = sock_alloc_file(newsock, &newfile, flags); |
1555 | if (unlikely(newfd < 0)) { | 1528 | if (unlikely(newfd < 0)) { |
1556 | err = newfd; | 1529 | err = newfd; |
1557 | sock_release(newsock); | 1530 | sock_release(newsock); |
1558 | goto out_put; | 1531 | goto out_put; |
1559 | } | 1532 | } |
1560 | 1533 | ||
1561 | err = sock_attach_fd(newsock, newfile, flags & O_NONBLOCK); | ||
1562 | if (err < 0) | ||
1563 | goto out_fd_simple; | ||
1564 | |||
1565 | err = security_socket_accept(sock, newsock); | 1534 | err = security_socket_accept(sock, newsock); |
1566 | if (err) | 1535 | if (err) |
1567 | goto out_fd; | 1536 | goto out_fd; |
@@ -1591,11 +1560,6 @@ out_put: | |||
1591 | fput_light(sock->file, fput_needed); | 1560 | fput_light(sock->file, fput_needed); |
1592 | out: | 1561 | out: |
1593 | return err; | 1562 | return err; |
1594 | out_fd_simple: | ||
1595 | sock_release(newsock); | ||
1596 | put_filp(newfile); | ||
1597 | put_unused_fd(newfd); | ||
1598 | goto out_put; | ||
1599 | out_fd: | 1563 | out_fd: |
1600 | fput(newfile); | 1564 | fput(newfile); |
1601 | put_unused_fd(newfd); | 1565 | put_unused_fd(newfd); |