1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886
|
/*
* Berkeley Lab Checkpoint/Restart (BLCR) for Linux is Copyright (c)
* 2007, The Regents of the University of California, through Lawrence
* Berkeley National Laboratory (subject to receipt of any required
* approvals from the U.S. Dept. of Energy). All rights reserved.
*
* Portions may be copyrighted by others, as may be noted in specific
* copyright notices within specific files.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* $Id: cr_pipes.c,v 1.225.8.9 2013/01/04 04:21:24 phargrov Exp $
*/
#include "cr_module.h"
#include "cr_context.h"
#include <linux/pipe_fs_i.h>
#if HAVE_PIPE_INODE_INFO_BASE
#define CR_ALLOC_PIPEBUF(_sz) kmalloc((_sz),GFP_KERNEL)
#define CR_FREE_PIPEBUF(_buf) kfree(_buf)
#else
#define CR_ALLOC_PIPEBUF(_sz) vmalloc(_sz)
#define CR_FREE_PIPEBUF(_buf) vfree(_buf)
#if HAVE_PIPE_INODE_INFO_BUFFERS
static __inline__ unsigned int cr_pipe_buffers(struct pipe_inode_info *p) {
return p->buffers;
}
#else
static __inline__ unsigned int cr_pipe_buffers(struct pipe_inode_info *p) {
return PIPE_BUFFERS;
}
#endif
#endif
#if HAVE_INODE_SEM
#define cr_pipe_lock(_i) down(PIPE_SEM(*(_i)))
#define cr_pipe_lock_interruptible(_i) down_interruptible(PIPE_SEM((*_i)))
#define cr_pipe_unlock(_i) up(PIPE_SEM(*(_i)))
#elif HAVE_INODE_MUTEX
#define cr_pipe_lock(_i) mutex_lock(&(_i)->i_mutex)
#define cr_pipe_lock_interruptible(_i) mutex_lock_interruptible(&(_i)->i_mutex)
#define cr_pipe_unlock(_i) mutex_unlock(&(_i)->i_mutex)
#else
#error "Unknown pipe lock type"
#endif
/*
* f_flags - same as in user space
* f_mode - funny kernel thing (can reconstruct from f_flags)
* 00 - no permissions needed
* 01 - read permission needed
* 10 - write permission needed
* 11 - read/write permissions needed
*/
static int
cr_posix_flags_to_f_mode(int posix_flags)
{
return ((posix_flags+1) & O_ACCMODE);
}
/*
* This is more or less the same thing as open(), with the major exception
* that we DO NOT BLOCK. (Normal open() does block in certain cases.)
* We take the approach of first openning read-write. Then a second
* open is done with the real flags. The presence of the first open
* ensures the second will not block for lack of readers or writers.
*/
static int
cr_open_named_fifo(cr_errbuf_t *eb, int fd, struct path *path, int flags)
{
int retval = 0;
struct file *filp, *rw_filp;
/* First we open R/W unconditionally.
*
* Note, since dentry_open() assumes caller has checked permissions, we
* know this "extra" RDWR open will work regardless of true access rights.
* THIS IS ALSO WHY WE MUST CLOSE rw_filp WHEN DONE WITH IT.
*/
path_get(path);
rw_filp = cr_dentry_open(path, O_RDWR);
retval = PTR_ERR(rw_filp);
if (IS_ERR(rw_filp)) goto out;
/* Now we open again w/ the desired permissions.
* Note we don't short-cut on (flags==O_RDWR) because we want
* the permission check that occurs in cr_filp_reopen().
*/
filp = cr_filp_reopen(rw_filp, flags);
filp_close(rw_filp, current->files);
retval = PTR_ERR(filp);
if (IS_ERR(filp)) goto out;
/* Install it */
retval = cr_fd_claim(fd);
if (retval < 0) {
CR_ERR_EB(eb, "File descriptor %d in use.", fd);
goto out;
}
fd_install(fd, filp);
out:
return retval;
}
/* Set up a new file pointer to an existing fifo */
static int
cr_couple_named_fifo(cr_rstrt_req_t *req, struct cr_file_info *file_info,
struct cr_fifo *cf_fifo, struct file *first_filp,
const char *name)
{
cr_errbuf_t *eb = req->errbuf;
struct dentry *dentry = first_filp->f_dentry;
struct vfsmount *mnt = first_filp->f_vfsmnt;
struct dentry *new_dentry = NULL;
int retval = -EINVAL;
/*
* Note that the following logic is certain to work if either ALL or NONE
* of the original hard links existed at checkpoint time, and again either
* ALL or NONE exist at restart time.
* If only SOME links exist at either point in time, then this logic may
* still work, depending on ordering. However, even case in which this logic
* fails to restart, it does so with an EEXIST or ENOENT, not a kernel Oops!
*/
if (file_info->unlinked || vmad_dentry_unlinked(dentry)) {
/* We currently can't link to an unlinked file (cr_link() limitation). Also,
* we don't currently bother to create a new link that we immediately unlink,
* since only /proc/<pid>/fd will show the difference.
*/
new_dentry = dget(dentry);
} else if (!cr_find_object(req->map, cf_fifo->fifo_dentry, (void **) &new_dentry)) {
/* We are first to use this name, so ensure the link exists (and is correct).
* The link ensures that any post-restart open()s of this name will get
* the correct FIFO.
*/
CR_PATH_DECL(path);
CR_PATH_GET_FILE(path, first_filp);
new_dentry = cr_link(eb, path, name);
path_put(path);
if (IS_ERR(new_dentry)) {
retval = PTR_ERR(new_dentry);
CR_ERR_REQ(req, "cr_couple_named_fifo: unable to link named fifo: %s", name);
goto out;
}
cr_insert_object(req->map, cf_fifo->fifo_dentry, new_dentry, GFP_KERNEL);
} else {
/* This name is already created, so just open it again */
dget(new_dentry);
}
/* Now open it */
if (!new_dentry->d_inode) {
CR_ERR_REQ(req, "cr_couple_named_fifo: NULL new_dentry->d_inode");
retval = -EIO;
goto out_dput;
}
{
struct path path;
path.dentry = new_dentry;
path.mnt = mnt;
retval = cr_open_named_fifo(eb, file_info->fd, &path, cf_fifo->fifo_flags);
}
out_dput:
dput(new_dentry);
out:
return retval;
}
/* Set up a new file pointer to an existing pipe */
static int
cr_couple_unnamed_pipe(cr_rstrt_req_t *req, struct cr_file_info *file_info,
struct cr_fifo *cf_fifo, struct file *first_filp)
{
int retval;
struct file *filp;
CR_KTRACE_LOW_LVL("Opening FIFO ID %p", cf_fifo->fifo_id);
/* i_writecount is not incremented in do_pipe(), but is decremented below close().
* The value is never checked, so there is not a problem with letting that field
* just become negative when one end of a pipe is closed. However, if we call
* cr_filp_reopen() asking for write permissions, there *is* a check of i_writecount
* that count fail (returning -ETXTBUSY of all things). So, we zero it here and
* let its value assume a bogus positive value rather than a bogus negative one.
*/
atomic_set(&first_filp->f_dentry->d_inode->i_writecount, 0);
filp = cr_filp_reopen(first_filp, cf_fifo->fifo_flags);
retval = PTR_ERR(filp);
if (IS_ERR(filp)) {
goto out;
}
/* install the fd */
retval = cr_fd_claim(file_info->fd);
if (retval) {
/* this should never happen. */
CR_ERR_REQ(req, "File descriptor %d in use.", file_info->fd);
goto out;
}
fd_install(file_info->fd, filp);
out:
return retval;
}
static int
cr_creat_named_fifo(cr_errbuf_t *eb, struct cr_file_info *file_info,
const struct cr_fifo *cf_fifo, const char *name)
{
int retval;
struct path path;
/* make a new FIFO, or find the existing one */
retval = cr_mknod(eb, &path, name, cf_fifo->fifo_perms, file_info->unlinked ? file_info->fd+1 : 0);
if (retval) {
CR_ERR_EB(eb, "Failed to create named fifo %s. err=%d.", name, retval);
goto out;
}
/* open it */
retval = cr_open_named_fifo(eb, file_info->fd, &path, cf_fifo->fifo_flags);
if (retval < 0) {
CR_ERR_EB(eb, "Couldn't open %s. err=%d.", name, retval);
goto out_put;
}
retval = 0;
out_put:
path_put(&path);
out:
return retval;
}
static int
cr_creat_unnamed_pipe(cr_errbuf_t *eb, struct cr_file_info *file_info,
const struct cr_fifo *cf_fifo)
{
int retval;
int pipe_fds[2];
int tmp_fd;
int keep;
retval = cr_do_pipe(pipe_fds);
if (retval < 0) {
CR_ERR_EB(eb, "do_pipe() failed (%d).", retval);
goto out;
}
/* now have two pipe objects, close one of them. */
switch (cr_posix_flags_to_f_mode(cf_fifo->fifo_flags)) {
case FMODE_READ:
keep = 0;
break;
case FMODE_WRITE:
keep = 1;
break;
default:
CR_ERR_EB(eb, "Bad access mode for pipe.");
goto out;
}
sys_close(pipe_fds[!keep]);
tmp_fd = pipe_fds[keep];
/* put the fd into the right place */
if (tmp_fd != file_info->fd) {
retval = sys_dup2(tmp_fd, file_info->fd);
(void)sys_close(tmp_fd); /* we ignore any error from close here. */
if (retval < 0) {
CR_ERR_EB(eb, "sys_dup2(%d,%d) failed.", tmp_fd, file_info->fd);
goto out;
}
}
out:
return retval;
}
static int
cr_restore_pipe_buf(cr_errbuf_t *eb, struct file *cf_filp, struct inode *p_inode, int buf_len, int do_not_restore_flag)
{
struct pipe_inode_info *pipe;
int retval;
void *buf;
if (!buf_len) {
CR_KTRACE_LOW_LVL("Skipping empty pipe buffer %p", p_inode);
retval = 0;
goto out;
}
buf = (void *)CR_ALLOC_PIPEBUF(buf_len);
if (!buf) {
CR_ERR_EB(eb, "Unable to allocate memory for pipe buffer!");
retval = -ENOMEM;
goto out;
}
// XXX: need eb arg for kread
retval = cr_kread(eb, cf_filp, buf, buf_len);
if (retval != buf_len) {
CR_ERR_EB(eb, "pipe fifo: read buf returned %d", retval);
goto out_free;
}
if (do_not_restore_flag) {
CR_KTRACE_LOW_LVL("Unrestored pipe buffer %p length = %d", p_inode, buf_len);
retval = 0;
goto out_free;
}
if (cr_pipe_lock_interruptible(p_inode)) {
retval = -ERESTARTSYS;
goto out_free;
}
pipe = p_inode->i_pipe;
#if HAVE_PIPE_INODE_INFO_BASE
if (PIPE_LEN(*p_inode)) {
/* someone put data in here already -- bail */
retval = -EBUSY;
goto out_up;
}
if ((buf_len) > PIPE_SIZE) {
/* should never happen */
retval = -ENOSPC;
goto out_up;
}
if (buf_len) {
memcpy((void *)PIPE_BASE(*p_inode), buf, buf_len);
PIPE_LEN(*p_inode) = buf_len;
CR_KTRACE_LOW_LVL("Pipe buffer %p length = %d inode = %lu", p_inode, buf_len, p_inode->i_ino);
retval = 0;
}
#else
{ struct pipe_inode_info *pipe = p_inode->i_pipe;
char * p;
int nrbufs;
if (pipe->nrbufs != 0) {
/* someone put data in here already -- bail */
retval = -EBUSY;
goto out_up;
}
if (buf_len > cr_pipe_buffers(pipe)*PAGE_SIZE) {
/* cr_restore_open_fifo should have done this already. */
retval = -ENOSPC;
goto out_up;
/* Do this here instead of calling pipe_fcntl?
* pipe_set_size(pipe, nr_pages); */
}
nrbufs = 0;
p = buf;
while (buf_len) {
size_t count = buf_len;
struct pipe_buffer *buf = pipe->bufs + nrbufs;
struct page *page = alloc_page(GFP_HIGHUSER);
if (count > PAGE_SIZE)
count = PAGE_SIZE;
memcpy(kmap(page), p, count);
kunmap(page);
buf->page = page;
buf->offset = 0;
buf->len = count;
buf->ops = &anon_pipe_buf_ops;
++nrbufs;
buf_len -= count;
p += count;
}
pipe->curbuf = 0;
pipe->nrbufs = nrbufs;
}
#endif
out_up:
cr_pipe_unlock(p_inode);
out_free:
CR_FREE_PIPEBUF(buf);
out:
return retval;
}
/*
* pipe was instantiated already, so we connect our file descriptor
* to the pipe inode.
*/
static int
cr_couple_fifo(cr_rstrt_req_t *req, struct cr_file_info *file_info,
struct cr_fifo *cf_fifo, const char *name, int do_not_restore_flag)
{
int retval;
struct file *first_filp = NULL;
retval = -EINVAL;
if (do_not_restore_flag) {
/* The only reason we don't return right here is validation of name */
} else if (!cr_find_object(req->map, cf_fifo->fifo_id, (void **) &first_filp)) {
CR_ERR_REQ(req, "cr_couple_fifo - unable to find previously restored fifo (id=%p)", cf_fifo->fifo_id);
goto out;
}
CR_KTRACE_LOW_LVL("%s: phase 2: connecting second end.", name);
/* no obvious way to distinguish named pipes from unnamed besides
* filename. anything with a '/' at the beginning is a named fifo,
* and anything with a 'p' is a pipe. */
retval = 0;
switch (name[0]) {
case '/': /* named fifo */
if (!do_not_restore_flag) {
retval = cr_couple_named_fifo(req, file_info, cf_fifo, first_filp, name);
if (retval < 0) {
CR_ERR_REQ(req, "cr_couple_fifo: unable to open named fifo: %s", name);
}
}
break;
case 'p': /* unnamed pipe */
if (!do_not_restore_flag) {
retval = cr_couple_unnamed_pipe(req, file_info, cf_fifo, first_filp);
if (retval < 0) {
CR_ERR_REQ(req, "cr_couple_fifo: unable to open unnamed pipe: %s", name);
}
}
break;
default: /* garbage */
retval = -EINVAL;
CR_ERR_REQ(req, "bogus pipe filename in context file: %s", name);
goto out;
}
out:
return retval;
}
/* Returns a flip that caller must fput() */
static struct file *
cr_make_new_pipe(cr_rstrt_req_t *req, struct cr_file_info *file_info,
struct cr_fifo *cf_fifo, const char *name, int do_not_restore_flag)
{
cr_errbuf_t *eb = req->errbuf;
struct file *p_filp = NULL;
int err = 0;
CR_KTRACE_LOW_LVL("%s: Phase 1: Making new pipe.", name);
switch (name[0]) {
case '/': /* named fifo */
if (do_not_restore_flag) goto out;
err = cr_creat_named_fifo(eb, file_info, cf_fifo, name);
break;
case 'p': /* unnamed pipe */
if (do_not_restore_flag) goto out;
err = cr_creat_unnamed_pipe(eb, file_info, cf_fifo);
break;
default: /* garbage */
err = -EINVAL;
CR_ERR_REQ(req, "Bogus pipe filename in context file: %s", name);
}
if (err < 0) goto out;
/* Might be the new pipe fd, or might be the fd protected by do-not-restore */
p_filp = fget(file_info->fd);
if (!p_filp) {
goto out;
}
/* now do the easy stuff (I'm not sure we should touch these -PHH) */
p_filp->f_pos = cf_fifo->fifo_pos;
p_filp->f_version = cf_fifo->fifo_version;
/* p_filp->f_fop and f_flags initialized correctly by do_pipe/mknod */
/* Map fifo_id (the pre-checkpoint inode) to the new file* for subsequent open(s) */
cr_insert_object(req->map, cf_fifo->fifo_id, p_filp, GFP_KERNEL);
/* And one to distinguish multiple hard links */
cr_insert_object(req->map, cf_fifo->fifo_dentry, p_filp->f_dentry, GFP_KERNEL);
out:
return (err < 0) ? ERR_PTR(err) : p_filp;
}
#if HAVE_PIPE_FCNTL
static long cr_pipe_size_init(cr_errbuf_t *eb, struct file *pipe_filp,
long new_size)
{
static long filp_size;
/* check the current size */
filp_size = pipe_fcntl(pipe_filp, F_GETPIPE_SZ, 0);
if (filp_size != new_size) {
/* resize the thing, this can shrink the pipe, as well as grow it */
filp_size = pipe_fcntl(pipe_filp, F_SETPIPE_SZ, new_size);
if (filp_size < 0) {
CR_ERR_EB(eb, "pipe_fcntl: failed with err=%ld", filp_size);
} else if (filp_size < new_size) {
CR_ERR_EB(eb, "Unable to set pipe size");
filp_size = -ENOSPC;
}
}
return filp_size;
}
#endif
extern int
cr_restore_open_fifo(cr_rstrt_proc_req_t *proc_req,
struct cr_file_info *file_info, int do_not_restore_flag)
{
cr_errbuf_t *eb = proc_req->req->errbuf;
int retval = -ENOSYS;
cr_rstrt_req_t *req = proc_req->req;
struct file *cf_filp = proc_req->file;
struct cr_fifo cf_fifo;
const char *name;
CR_KTRACE_FUNC_ENTRY();
/* read fifo struct */
retval = cr_kread(eb, cf_filp, &cf_fifo, sizeof(cf_fifo));
if (retval != sizeof(cf_fifo)) {
CR_ERR_REQ(req, "pipe fifo: read failed");
goto out;
}
CR_KTRACE_LOW_LVL(" Open fifo: id == %p.", cf_fifo.fifo_id);
if (cf_fifo.cr_type != cr_fifo_obj) {
CR_ERR_REQ(req, "cr_restore_open_fifo. Garbage found in context file. Wrong object type.");
retval = -EINVAL;
goto out;
}
/* Force do_not_restore_flag OFF for "internal" pipes and ON for "external" ones */
if (cf_fifo.fifo_internal) {
do_not_restore_flag = 0;
if (fcheck(file_info->fd) != NULL) {
sys_close(file_info->fd);
}
} else if (!do_not_restore_flag) {
/* Deal with a dangling pipe by dup()ing dpipe_{in,out} */
struct file *filp;
retval = -EBADF;
switch (cf_fifo.fifo_flags & O_ACCMODE) {
case O_RDONLY:
filp = req->dpipe_in;
if (!filp) {
CR_ERR_REQ(req, "No alternate input file when restoring an external pipe");
goto out;
}
if (!(filp->f_mode & FMODE_READ)) {
CR_ERR_REQ(req, "Alternate input file non-readable when restoring an external pipe");
goto out;
}
break;
case O_WRONLY:
filp = req->dpipe_out;
if (!filp) {
CR_ERR_REQ(req, "No alternate output file when restoring an external pipe");
goto out;
}
if (!(filp->f_mode & FMODE_WRITE)) {
CR_ERR_REQ(req, "Alternate output file non-writable when restoring an external pipe");
goto out;
}
break;
default:
CR_ERR_REQ(req, "Invalid access mode %d while restoring external pipe",
(cf_fifo.fifo_flags & O_ACCMODE));
retval = -EINVAL;
goto out;
}
CRI_ASSERT(filp != NULL);
get_file(filp);
retval = cr_fd_claim(file_info->fd);
if (retval < 0) {
CR_ERR_REQ(req, "File descriptor %d in use.", file_info->fd);
goto out;
}
fd_install(file_info->fd, filp);
do_not_restore_flag = 1;
}
/* read file name */
name = cr_getname(eb, req->relocate, cf_filp, 0);
if (IS_ERR(name)) {
CR_ERR_REQ(req, "pipe_name - Bad pathname read!");
retval = PTR_ERR(name);
goto out;
}
/* now, actually rebuild the thing. */
if (cf_fifo.fifo_len == ((unsigned int)-1)) {
retval = cr_couple_fifo(req, file_info, &cf_fifo, name,
do_not_restore_flag);
if (retval < 0) {
goto out_free;
}
/*
* the pipe buffer should have been restored when we first built the pipe
*/
} else {
struct file *p_filp;
p_filp = cr_make_new_pipe(req, file_info, &cf_fifo, name,
do_not_restore_flag);
retval = PTR_ERR(p_filp);
if (IS_ERR(p_filp) || !p_filp) {
goto out_free;
}
#if HAVE_PIPE_FCNTL
/* resize the pipe buffers while it's still easy. */
if (cf_fifo.pipe_sz < cf_fifo.fifo_len) {
/* should never ever happen, abort */
CR_ERR_REQ(req, "bad pipe info: (pipe_sz < fifo_len)!");
goto out_free;
}
retval = cr_pipe_size_init(eb, p_filp, cf_fifo.pipe_sz);
if ((retval < cf_fifo.pipe_sz) || (retval < 0)) {
CR_ERR_REQ(req, "failed to resize pipe buffers");
goto out_free;
}
#endif
/* now read the pipe buffer data, and optionally restore it */
retval = cr_restore_pipe_buf(eb, cf_filp, p_filp->f_dentry->d_inode,
cf_fifo.fifo_len, do_not_restore_flag);
fput(p_filp);
if (retval < 0) {
CR_ERR_REQ(req, "could not restore pipe buffer");
goto out_free;
}
}
out_free:
__putname(name);
out:
return retval;
}
/* Deterimine if a given FIFO is "internal", defined has having
* at least one reader and one writer in the checkpoint set.
*
* XXX: This is almost certainly the worst possible way to do this.
* Alas, I don't have a better way in mind that can be implemented
* w/o need to move file saves/restores to the end (after memory of
* all processes).
*/
static int
cr_fifo_is_internal(cr_chkpt_req_t *req, struct inode *inode) {
cr_chkpt_proc_req_t *proc_req;
int have_reader = 0;
int have_writer = 0;
unsigned long retval = 0; /* For size match w/ void* in object map */
char *map_key = 1 + (char *)inode;
read_lock(&req->lock);
/* Look for cached value first. Note "creative" use of key. */
if (cr_find_object(req->map, map_key, (void **) &retval)) {
goto out_cached;
}
list_for_each_entry(proc_req, &req->procs, list) {
int max_fds, fd;
cr_fdtable_t *fdt;
struct task_struct *task = NULL;
cr_task_t *cr_task;
/* Find a non-dead task in proc_req, if any.
* XXX: Race against exits? What lock should we hold here?
*/
list_for_each_entry(cr_task, &proc_req->tasks, proc_req_list) {
if (!cri_task_dead(cr_task->task)) {
task = cr_task->task;
break;
}
}
if (!task) continue;
spin_lock(&task->files->file_lock);
rcu_read_lock();
fdt = cr_fdtable(task->files);
max_fds = fdt->max_fds; /* Never shrinks, right? */
rcu_read_unlock();
for (fd = 0; !retval && (fd < max_fds); ++fd) {
struct file *filp = fcheck_files(task->files, fd);
if (!filp) continue;
get_file(filp);
if (filp->f_dentry->d_inode == inode) {
if (filp->f_mode & FMODE_WRITE) have_writer = 1;
if (filp->f_mode & FMODE_READ) have_reader = 1;
}
fput(filp);
retval = (have_reader && have_writer);
}
spin_unlock(&task->files->file_lock);
if (retval) break;
}
cr_insert_object(req->map, map_key, (void *)retval, GFP_ATOMIC /* we hold req->lock */);
out_cached:
read_unlock(&req->lock);
CR_KTRACE_FUNC_EXIT("Return '%sternal'", retval ? "in" : "ex");
return (int)retval;
}
extern int
cr_save_open_fifo(cr_chkpt_proc_req_t *proc_req, struct file *filp)
{
cr_errbuf_t *eb = proc_req->req->errbuf;
struct file *cf_filp = proc_req->file;
struct cr_fifo cf_fifo;
struct inode *inode;
void *buf = NULL;
int retval;
retval = -EINVAL;
if (!filp)
goto out;
inode = filp->f_dentry->d_inode;
cf_fifo.cr_type = cr_fifo_obj;
cf_fifo.fifo_id = inode;
cf_fifo.fifo_dentry = filp->f_dentry;
cf_fifo.fifo_len = -1;
cf_fifo.pipe_sz = -1;
cf_fifo.fifo_internal = cr_fifo_is_internal(proc_req->req, inode);
/* Note: cf_fifo.fifo_internal check makes us skip the pipebuf save for external pipes
* since we currently don't even try to read the data back in for this case.
*/
if (!cr_insert_object(proc_req->req->map, inode, inode, GFP_KERNEL) && cf_fifo.fifo_internal) {
/* We are first to save: suck the data out of the pipe while holding the pipe semaphore */
retval = -ERESTARTSYS;
if (cr_pipe_lock_interruptible(inode)) {
goto out;
}
#if HAVE_PIPE_INODE_INFO_BASE
{
cf_fifo.fifo_len = PIPE_LEN(*inode);
buf = CR_ALLOC_PIPEBUF(cf_fifo.fifo_len);
if (!buf) {
retval = -ENOMEM;
cr_pipe_unlock(inode);
goto out;
}
memcpy(buf, (void*)PIPE_BASE(*inode)+PIPE_START(*inode), cf_fifo.fifo_len);
}
#else
{
struct pipe_inode_info *pipe = inode->i_pipe;
int i, curbuf;
char *p;
buf = CR_ALLOC_PIPEBUF(cr_pipe_buffers(pipe)*PAGE_SIZE);
if (!buf) {
retval = -ENOMEM;
cr_pipe_unlock(inode);
goto out;
}
p = buf;
cf_fifo.fifo_len = 0;
curbuf = pipe->curbuf;
for (i = 0; i < pipe->nrbufs; ++i) {
struct pipe_buffer *pbuf = pipe->bufs + curbuf;
const struct pipe_buf_operations *ops = pbuf->ops;
char *addr;
#if HAVE_PIPE_BUF_OPERATIONS_PIN
int error = ops->pin(pipe, pbuf);
if (error) {
retval = error;
cr_pipe_unlock(inode);
goto out_free;
}
addr = ops->map(pipe, pbuf, 0);
memcpy(p, addr + pbuf->offset, pbuf->len);
ops->unmap(pipe, pbuf, addr);
#elif HAVE_2_ARG_PIPE_OPS_UNMAP
addr = ops->map(filp, pipe, pbuf);
memcpy(p, addr + pbuf->offset, pbuf->len);
ops->unmap(pipe, pbuf);
#elif HAVE_3_ARG_PIPE_OPS_UNMAP
addr = ops->map(pipe, pbuf, 0);
memcpy(p, addr + pbuf->offset, pbuf->len);
ops->unmap(pipe, pbuf, addr);
#else
#error "Unknown pipe buf operations"
#endif
p += pbuf->len;
cf_fifo.fifo_len += pbuf->len;
curbuf = (curbuf + 1) & (cr_pipe_buffers(pipe)-1);
}
}
#endif
cr_pipe_unlock(inode);
} else {
/* We've saved this fifo previously */
/* cf_fifo.fifo_len == -1 will identify this case at restart time. */
}
/* pipe perms - for FIFOs, need to know what to pass to mknod() */
cf_fifo.fifo_perms = inode->i_mode;
/* posix flags (for open) */
cf_fifo.fifo_pos = filp->f_pos;
cf_fifo.fifo_flags = filp->f_flags;
cf_fifo.fifo_version = filp->f_version;
/* write out the pipe size, for fcntl(F_SETPIPE_SZ). Do this without
* holding the mutex, since pipe_fcntl tries to acquire it. */
#if HAVE_PIPE_FCNTL
cf_fifo.pipe_sz = pipe_fcntl(filp, F_GETPIPE_SZ, 0);
#else
cf_fifo.pipe_sz = cf_fifo.fifo_len;
#endif
/* write out fifo structure */
retval = cr_kwrite(eb, cf_filp, &cf_fifo, sizeof(cf_fifo));
if (retval != sizeof(cf_fifo)) {
CR_ERR_PROC_REQ(proc_req, "pipe fifo: write failed");
goto out_free;
}
/* write the filename out */
retval = cr_save_filename(eb, cf_filp, filp, NULL, 0);
if (retval < 0) {
CR_ERR_PROC_REQ(proc_req, "Error saving pipe filename. (err=%d)", retval);
goto out_free;
}
/* write out pipe data last (unless saved previously) */
if (cf_fifo.fifo_len != ((unsigned int)-1)) {
retval = cr_kwrite(eb, cf_filp, buf, cf_fifo.fifo_len);
if (retval != cf_fifo.fifo_len) {
CR_ERR_PROC_REQ(proc_req, "pipe fifo: write buf failed");
goto out_free;
}
}
retval = 0;
out_free:
CR_FREE_PIPEBUF(buf); /* NULL ok */
out:
return retval;
}
|