linux/drivers/staging/dst/export.c
<<
>>
Prefs
   1/*
   2 * 2007+ Copyright (c) Evgeniy Polyakov <zbr@ioremap.net>
   3 * All rights reserved.
   4 *
   5 * This program is free software; you can redistribute it and/or modify
   6 * it under the terms of the GNU General Public License as published by
   7 * the Free Software Foundation; either version 2 of the License, or
   8 * (at your option) any later version.
   9 *
  10 * This program is distributed in the hope that it will be useful,
  11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13 * GNU General Public License for more details.
  14 */
  15
  16#include <linux/blkdev.h>
  17#include <linux/bio.h>
  18#include <linux/dst.h>
  19#include <linux/in.h>
  20#include <linux/in6.h>
  21#include <linux/poll.h>
  22#include <linux/slab.h>
  23#include <linux/socket.h>
  24
  25#include <net/sock.h>
  26
  27/*
  28 * Export bioset is used for server block IO requests.
  29 */
  30static struct bio_set *dst_bio_set;
  31
  32int __init dst_export_init(void)
  33{
  34        int err = -ENOMEM;
  35
  36        dst_bio_set = bioset_create(32, sizeof(struct dst_export_priv));
  37        if (!dst_bio_set)
  38                goto err_out_exit;
  39
  40        return 0;
  41
  42err_out_exit:
  43        return err;
  44}
  45
  46void dst_export_exit(void)
  47{
  48        bioset_free(dst_bio_set);
  49}
  50
  51/*
  52 * When client connects and autonegotiates with the server node,
  53 * its permissions are checked in a security attributes and sent
  54 * back.
  55 */
  56static unsigned int dst_check_permissions(struct dst_state *main, struct dst_state *st)
  57{
  58        struct dst_node *n = main->node;
  59        struct dst_secure *sentry;
  60        struct dst_secure_user *s;
  61        struct saddr *sa = &st->ctl.addr;
  62        unsigned int perm = 0;
  63
  64        mutex_lock(&n->security_lock);
  65        list_for_each_entry(sentry, &n->security_list, sec_entry) {
  66                s = &sentry->sec;
  67
  68                if (s->addr.sa_family != sa->sa_family)
  69                        continue;
  70
  71                if (s->addr.sa_data_len != sa->sa_data_len)
  72                        continue;
  73
  74                /*
  75                 * This '2' below is a port field. This may be very wrong to do
  76                 * in atalk for example though. If there will be any need to extent
  77                 * protocol to something else, I can create per-family helpers and
  78                 * use them instead of this memcmp.
  79                 */
  80                if (memcmp(s->addr.sa_data + 2, sa->sa_data + 2,
  81                                        sa->sa_data_len - 2))
  82                        continue;
  83
  84                perm = s->permissions;
  85        }
  86        mutex_unlock(&n->security_lock);
  87
  88        return perm;
  89}
  90
  91/*
  92 * Accept new client: allocate appropriate network state and check permissions.
  93 */
  94static struct dst_state *dst_accept_client(struct dst_state *st)
  95{
  96        unsigned int revents = 0;
  97        unsigned int err_mask = POLLERR | POLLHUP | POLLRDHUP;
  98        unsigned int mask = err_mask | POLLIN;
  99        struct dst_node *n = st->node;
 100        int err = 0;
 101        struct socket *sock = NULL;
 102        struct dst_state *new;
 103
 104        while (!err && !sock) {
 105                revents = dst_state_poll(st);
 106
 107                if (!(revents & mask)) {
 108                        DEFINE_WAIT(wait);
 109
 110                        for (;;) {
 111                                prepare_to_wait(&st->thread_wait,
 112                                                &wait, TASK_INTERRUPTIBLE);
 113                                if (!n->trans_scan_timeout || st->need_exit)
 114                                        break;
 115
 116                                revents = dst_state_poll(st);
 117
 118                                if (revents & mask)
 119                                        break;
 120
 121                                if (signal_pending(current))
 122                                        break;
 123
 124                                /*
 125                                 * Magic HZ? Polling check above is not safe in
 126                                 * all cases (like socket reset in BH context),
 127                                 * so it is simpler just to postpone it to the
 128                                 * process context instead of implementing special
 129                                 * locking there.
 130                                 */
 131                                schedule_timeout(HZ);
 132                        }
 133                        finish_wait(&st->thread_wait, &wait);
 134                }
 135
 136                err = -ECONNRESET;
 137                dst_state_lock(st);
 138
 139                dprintk("%s: st: %p, revents: %x [err: %d, in: %d].\n",
 140                        __func__, st, revents, revents & err_mask,
 141                        revents & POLLIN);
 142
 143                if (revents & err_mask) {
 144                        dprintk("%s: revents: %x, socket: %p, err: %d.\n",
 145                                        __func__, revents, st->socket, err);
 146                        err = -ECONNRESET;
 147                }
 148
 149                if (!n->trans_scan_timeout || st->need_exit)
 150                        err = -ENODEV;
 151
 152                if (st->socket && (revents & POLLIN))
 153                        err = kernel_accept(st->socket, &sock, 0);
 154
 155                dst_state_unlock(st);
 156        }
 157
 158        if (err)
 159                goto err_out_exit;
 160
 161        new = dst_state_alloc(st->node);
 162        if (IS_ERR(new)) {
 163                err = -ENOMEM;
 164                goto err_out_release;
 165        }
 166        new->socket = sock;
 167
 168        new->ctl.addr.sa_data_len = sizeof(struct sockaddr);
 169        err = kernel_getpeername(sock, (struct sockaddr *)&new->ctl.addr,
 170                        (int *)&new->ctl.addr.sa_data_len);
 171        if (err)
 172                goto err_out_put;
 173
 174        new->permissions = dst_check_permissions(st, new);
 175        if (new->permissions == 0) {
 176                err = -EPERM;
 177                dst_dump_addr(sock, (struct sockaddr *)&new->ctl.addr,
 178                                "Client is not allowed to connect");
 179                goto err_out_put;
 180        }
 181
 182        err = dst_poll_init(new);
 183        if (err)
 184                goto err_out_put;
 185
 186        dst_dump_addr(sock, (struct sockaddr *)&new->ctl.addr,
 187                        "Connected client");
 188
 189        return new;
 190
 191err_out_put:
 192        dst_state_put(new);
 193err_out_release:
 194        sock_release(sock);
 195err_out_exit:
 196        return ERR_PTR(err);
 197}
 198
 199/*
 200 * Each server's block request sometime finishes.
 201 * Usually it happens in hard irq context of the appropriate controller,
 202 * so to play good with all cases we just queue BIO into the queue
 203 * and wake up processing thread, which gets completed request and
 204 * send (encrypting if needed) it back to the client (if it was a read
 205 * request), or sends back reply that writing succesfully completed.
 206 */
 207static int dst_export_process_request_queue(struct dst_state *st)
 208{
 209        unsigned long flags;
 210        struct dst_export_priv *p = NULL;
 211        struct bio *bio;
 212        int err = 0;
 213
 214        while (!list_empty(&st->request_list)) {
 215                spin_lock_irqsave(&st->request_lock, flags);
 216                if (!list_empty(&st->request_list)) {
 217                        p = list_first_entry(&st->request_list,
 218                                struct dst_export_priv, request_entry);
 219                        list_del(&p->request_entry);
 220                }
 221                spin_unlock_irqrestore(&st->request_lock, flags);
 222
 223                if (!p)
 224                        break;
 225
 226                bio = p->bio;
 227
 228                if (dst_need_crypto(st->node) && (bio_data_dir(bio) == READ))
 229                        err = dst_export_crypto(st->node, bio);
 230                else
 231                        err = dst_export_send_bio(bio);
 232
 233                if (err)
 234                        break;
 235        }
 236
 237        return err;
 238}
 239
 240/*
 241 * Cleanup export state.
 242 * It has to wait until all requests are finished,
 243 * and then free them all.
 244 */
 245static void dst_state_cleanup_export(struct dst_state *st)
 246{
 247        struct dst_export_priv *p;
 248        unsigned long flags;
 249
 250        /*
 251         * This loop waits for all pending bios to be completed and freed.
 252         */
 253        while (atomic_read(&st->refcnt) > 1) {
 254                dprintk("%s: st: %p, refcnt: %d, list_empty: %d.\n",
 255                                __func__, st, atomic_read(&st->refcnt),
 256                                list_empty(&st->request_list));
 257                wait_event_timeout(st->thread_wait,
 258                                (atomic_read(&st->refcnt) == 1) ||
 259                                !list_empty(&st->request_list),
 260                                HZ/2);
 261
 262                while (!list_empty(&st->request_list)) {
 263                        p = NULL;
 264                        spin_lock_irqsave(&st->request_lock, flags);
 265                        if (!list_empty(&st->request_list)) {
 266                                p = list_first_entry(&st->request_list,
 267                                        struct dst_export_priv, request_entry);
 268                                list_del(&p->request_entry);
 269                        }
 270                        spin_unlock_irqrestore(&st->request_lock, flags);
 271
 272                        if (p)
 273                                bio_put(p->bio);
 274
 275                        dprintk("%s: st: %p, refcnt: %d, list_empty: %d, p: %p.\n",
 276                                __func__, st, atomic_read(&st->refcnt),
 277                                list_empty(&st->request_list), p);
 278                }
 279        }
 280
 281        dst_state_put(st);
 282}
 283
 284/*
 285 * Client accepting thread.
 286 * Not only accepts new connection, but also schedules receiving thread
 287 * and performs request completion described above.
 288 */
 289static int dst_accept(void *init_data, void *schedule_data)
 290{
 291        struct dst_state *main_st = schedule_data;
 292        struct dst_node *n = init_data;
 293        struct dst_state *st;
 294        int err;
 295
 296        while (n->trans_scan_timeout && !main_st->need_exit) {
 297                dprintk("%s: main_st: %p, n: %p.\n", __func__, main_st, n);
 298                st = dst_accept_client(main_st);
 299                if (IS_ERR(st))
 300                        continue;
 301
 302                err = dst_state_schedule_receiver(st);
 303                if (!err) {
 304                        while (n->trans_scan_timeout) {
 305                                err = wait_event_interruptible_timeout(st->thread_wait,
 306                                                !list_empty(&st->request_list) ||
 307                                                !n->trans_scan_timeout ||
 308                                                st->need_exit,
 309                                        HZ);
 310
 311                                if (!n->trans_scan_timeout || st->need_exit)
 312                                        break;
 313
 314                                if (list_empty(&st->request_list))
 315                                        continue;
 316
 317                                err = dst_export_process_request_queue(st);
 318                                if (err)
 319                                        break;
 320                        }
 321
 322                        st->need_exit = 1;
 323                        wake_up(&st->thread_wait);
 324                }
 325
 326                dst_state_cleanup_export(st);
 327        }
 328
 329        dprintk("%s: freeing listening socket st: %p.\n", __func__, main_st);
 330
 331        dst_state_lock(main_st);
 332        dst_poll_exit(main_st);
 333        dst_state_socket_release(main_st);
 334        dst_state_unlock(main_st);
 335        dst_state_put(main_st);
 336        dprintk("%s: freed listening socket st: %p.\n", __func__, main_st);
 337
 338        return 0;
 339}
 340
 341int dst_start_export(struct dst_node *n)
 342{
 343        if (list_empty(&n->security_list)) {
 344                printk(KERN_ERR "You are trying to export node '%s' without security attributes.\n"
 345                                "No clients will be allowed to connect. Exiting.\n", n->name);
 346                return -EINVAL;
 347        }
 348        return dst_node_trans_init(n, sizeof(struct dst_export_priv));
 349}
 350
 351/*
 352 * Initialize listening state and schedule accepting thread.
 353 */
 354int dst_node_init_listened(struct dst_node *n, struct dst_export_ctl *le)
 355{
 356        struct dst_state *st;
 357        int err = -ENOMEM;
 358        struct dst_network_ctl *ctl = &le->ctl;
 359
 360        memcpy(&n->info->net, ctl, sizeof(struct dst_network_ctl));
 361
 362        st = dst_state_alloc(n);
 363        if (IS_ERR(st)) {
 364                err = PTR_ERR(st);
 365                goto err_out_exit;
 366        }
 367        memcpy(&st->ctl, ctl, sizeof(struct dst_network_ctl));
 368
 369        err = dst_state_socket_create(st);
 370        if (err)
 371                goto err_out_put;
 372
 373        st->socket->sk->sk_reuse = 1;
 374
 375        err = kernel_bind(st->socket, (struct sockaddr *)&ctl->addr,
 376                        ctl->addr.sa_data_len);
 377        if (err)
 378                goto err_out_socket_release;
 379
 380        err = kernel_listen(st->socket, 1024);
 381        if (err)
 382                goto err_out_socket_release;
 383        n->state = st;
 384
 385        err = dst_poll_init(st);
 386        if (err)
 387                goto err_out_socket_release;
 388
 389        dst_state_get(st);
 390
 391        err = thread_pool_schedule(n->pool, dst_thread_setup,
 392                        dst_accept, st, MAX_SCHEDULE_TIMEOUT);
 393        if (err)
 394                goto err_out_poll_exit;
 395
 396        return 0;
 397
 398err_out_poll_exit:
 399        dst_poll_exit(st);
 400err_out_socket_release:
 401        dst_state_socket_release(st);
 402err_out_put:
 403        dst_state_put(st);
 404err_out_exit:
 405        n->state = NULL;
 406        return err;
 407}
 408
 409/*
 410 * Free bio and related private data.
 411 * Also drop a reference counter for appropriate state,
 412 * which waits when there are no more block IOs in-flight.
 413 */
 414static void dst_bio_destructor(struct bio *bio)
 415{
 416        struct bio_vec *bv;
 417        struct dst_export_priv *priv = bio->bi_private;
 418        int i;
 419
 420        bio_for_each_segment(bv, bio, i) {
 421                if (!bv->bv_page)
 422                        break;
 423
 424                __free_page(bv->bv_page);
 425        }
 426
 427        if (priv)
 428                dst_state_put(priv->state);
 429        bio_free(bio, dst_bio_set);
 430}
 431
 432/*
 433 * Block IO completion. Queue request to be sent back to
 434 * the client (or just confirmation).
 435 */
 436static void dst_bio_end_io(struct bio *bio, int err)
 437{
 438        struct dst_export_priv *p = bio->bi_private;
 439        struct dst_state *st = p->state;
 440        unsigned long flags;
 441
 442        spin_lock_irqsave(&st->request_lock, flags);
 443        list_add_tail(&p->request_entry, &st->request_list);
 444        spin_unlock_irqrestore(&st->request_lock, flags);
 445
 446        wake_up(&st->thread_wait);
 447}
 448
 449/*
 450 * Allocate read request for the server.
 451 */
 452static int dst_export_read_request(struct bio *bio, unsigned int total_size)
 453{
 454        unsigned int size;
 455        struct page *page;
 456        int err;
 457
 458        while (total_size) {
 459                err = -ENOMEM;
 460                page = alloc_page(GFP_KERNEL);
 461                if (!page)
 462                        goto err_out_exit;
 463
 464                size = min_t(unsigned int, PAGE_SIZE, total_size);
 465
 466                err = bio_add_page(bio, page, size, 0);
 467                dprintk("%s: bio: %llu/%u, size: %u, err: %d.\n",
 468                                __func__, (u64)bio->bi_sector, bio->bi_size,
 469                                size, err);
 470                if (err <= 0)
 471                        goto err_out_free_page;
 472
 473                total_size -= size;
 474        }
 475
 476        return 0;
 477
 478err_out_free_page:
 479        __free_page(page);
 480err_out_exit:
 481        return err;
 482}
 483
 484/*
 485 * Allocate write request for the server.
 486 * Should not only get pages, but also read data from the network.
 487 */
 488static int dst_export_write_request(struct dst_state *st,
 489                struct bio *bio, unsigned int total_size)
 490{
 491        unsigned int size;
 492        struct page *page;
 493        void *data;
 494        int err;
 495
 496        while (total_size) {
 497                err = -ENOMEM;
 498                page = alloc_page(GFP_KERNEL);
 499                if (!page)
 500                        goto err_out_exit;
 501
 502                data = kmap(page);
 503                if (!data)
 504                        goto err_out_free_page;
 505
 506                size = min_t(unsigned int, PAGE_SIZE, total_size);
 507
 508                err = dst_data_recv(st, data, size);
 509                if (err)
 510                        goto err_out_unmap_page;
 511
 512                err = bio_add_page(bio, page, size, 0);
 513                if (err <= 0)
 514                        goto err_out_unmap_page;
 515
 516                kunmap(page);
 517
 518                total_size -= size;
 519        }
 520
 521        return 0;
 522
 523err_out_unmap_page:
 524        kunmap(page);
 525err_out_free_page:
 526        __free_page(page);
 527err_out_exit:
 528        return err;
 529}
 530
 531/*
 532 * Groovy, we've gotten an IO request from the client.
 533 * Allocate BIO from the bioset, private data from the mempool
 534 * and lots of pages for IO.
 535 */
 536int dst_process_io(struct dst_state *st)
 537{
 538        struct dst_node *n = st->node;
 539        struct dst_cmd *cmd = st->data;
 540        struct bio *bio;
 541        struct dst_export_priv *priv;
 542        int err = -ENOMEM;
 543
 544        if (unlikely(!n->bdev)) {
 545                err = -EINVAL;
 546                goto err_out_exit;
 547        }
 548
 549        bio = bio_alloc_bioset(GFP_KERNEL,
 550                        PAGE_ALIGN(cmd->size) >> PAGE_SHIFT,
 551                        dst_bio_set);
 552        if (!bio)
 553                goto err_out_exit;
 554
 555        priv = (struct dst_export_priv *)(((void *)bio) - sizeof (struct dst_export_priv));
 556
 557        priv->state = dst_state_get(st);
 558        priv->bio = bio;
 559
 560        bio->bi_private = priv;
 561        bio->bi_end_io = dst_bio_end_io;
 562        bio->bi_destructor = dst_bio_destructor;
 563        bio->bi_bdev = n->bdev;
 564
 565        /*
 566         * Server side is only interested in two low bits:
 567         * uptodate (set by itself actually) and rw block
 568         */
 569        bio->bi_flags |= cmd->flags & 3;
 570
 571        bio->bi_rw = cmd->rw;
 572        bio->bi_size = 0;
 573        bio->bi_sector = cmd->sector;
 574
 575        dst_bio_to_cmd(bio, &priv->cmd, DST_IO_RESPONSE, cmd->id);
 576
 577        priv->cmd.flags = 0;
 578        priv->cmd.size = cmd->size;
 579
 580        if (bio_data_dir(bio) == WRITE) {
 581                err = dst_recv_cdata(st, priv->cmd.hash);
 582                if (err)
 583                        goto err_out_free;
 584
 585                err = dst_export_write_request(st, bio, cmd->size);
 586                if (err)
 587                        goto err_out_free;
 588
 589                if (dst_need_crypto(n))
 590                        return dst_export_crypto(n, bio);
 591        } else {
 592                err = dst_export_read_request(bio, cmd->size);
 593                if (err)
 594                        goto err_out_free;
 595        }
 596
 597        dprintk("%s: bio: %llu/%u, rw: %lu, dir: %lu, flags: %lx, phys: %d.\n",
 598                        __func__, (u64)bio->bi_sector, bio->bi_size,
 599                        bio->bi_rw, bio_data_dir(bio),
 600                        bio->bi_flags, bio->bi_phys_segments);
 601
 602        generic_make_request(bio);
 603
 604        return 0;
 605
 606err_out_free:
 607        bio_put(bio);
 608err_out_exit:
 609        return err;
 610}
 611
 612/*
 613 * Ok, block IO is ready, let's send it back to the client...
 614 */
 615int dst_export_send_bio(struct bio *bio)
 616{
 617        struct dst_export_priv *p = bio->bi_private;
 618        struct dst_state *st = p->state;
 619        struct dst_cmd *cmd = &p->cmd;
 620        int err;
 621
 622        dprintk("%s: id: %llu, bio: %llu/%u, csize: %u, flags: %lu, rw: %lu.\n",
 623                        __func__, cmd->id, (u64)bio->bi_sector, bio->bi_size,
 624                        cmd->csize, bio->bi_flags, bio->bi_rw);
 625
 626        dst_convert_cmd(cmd);
 627
 628        dst_state_lock(st);
 629        if (!st->socket) {
 630                err = -ECONNRESET;
 631                goto err_out_unlock;
 632        }
 633
 634        if (bio_data_dir(bio) == WRITE) {
 635                /* ... or just confirmation that writing has completed. */
 636                cmd->size = cmd->csize = 0;
 637                err = dst_data_send_header(st->socket, cmd,
 638                                sizeof(struct dst_cmd), 0);
 639                if (err)
 640                        goto err_out_unlock;
 641        } else {
 642                err = dst_send_bio(st, cmd, bio);
 643                if (err)
 644                        goto err_out_unlock;
 645        }
 646
 647        dst_state_unlock(st);
 648
 649        bio_put(bio);
 650        return 0;
 651
 652err_out_unlock:
 653        dst_state_unlock(st);
 654
 655        bio_put(bio);
 656        return err;
 657}
 658