File: disk.c

package info (click to toggle)
lime-forensics 1.7.5-1~bpo8%2B1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 128 kB
  • sloc: ansic: 371; makefile: 31
file content (113 lines) | stat: -rw-r--r-- 2,557 bytes parent folder | download | duplicates (3)
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
/*
 * LiME - Linux Memory Extractor
 * Copyright (c) 2011-2014 Joe Sylve - 504ENSICS Labs
 *
 *
 * Author:
 * Joe Sylve       - joe.sylve@gmail.com, @jtsylve
 *
 * 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
 */

#include "lime.h"


ssize_t write_vaddr_disk(void *, size_t);
int setup_disk(void);
void cleanup_disk(void);

static void disable_dio(void);

static struct file * f = NULL;
extern char * path;
extern int dio;
static int reopen = 0;

static void disable_dio() {
    DBG("Direct IO may not be supported on this file system. Retrying.");
    dio = 0;
    reopen = 1;
    cleanup_disk();
    setup_disk();
}

int setup_disk() {
    mm_segment_t fs;
    int err;

    fs = get_fs();
    set_fs(KERNEL_DS);

    if (dio && reopen) {
        f = filp_open(path, O_WRONLY | O_CREAT | O_LARGEFILE | O_SYNC | O_DIRECT, 0444);
    } else if (dio) {
        f = filp_open(path, O_WRONLY | O_CREAT | O_LARGEFILE | O_TRUNC | O_SYNC | O_DIRECT, 0444);
    }

    if(!dio || (f == ERR_PTR(-EINVAL))) {
        DBG("Direct IO Disabled");
        f = filp_open(path, O_WRONLY | O_CREAT | O_LARGEFILE | O_TRUNC, 0444);
        dio = 0;
    }

    if (!f || IS_ERR(f)) {
        DBG("Error opening file %ld", PTR_ERR(f));
        set_fs(fs);
        err = (f) ? PTR_ERR(f) : -EIO;
        f = NULL;
        return err;
    }

    set_fs(fs);

    return 0;
}

void cleanup_disk() {
    mm_segment_t fs;

    fs = get_fs();
    set_fs(KERNEL_DS);
    if(f) filp_close(f, NULL);
    set_fs(fs);
}

ssize_t write_vaddr_disk(void * v, size_t is) {
    mm_segment_t fs;

    ssize_t s;
    loff_t pos;

    fs = get_fs();
    set_fs(KERNEL_DS);

    pos = f->f_pos;

    s = vfs_write(f, v, is, &pos);

    if (s == is) {
        f->f_pos = pos;
    }

    set_fs(fs);

    if (s != is && dio) {
        disable_dio();
        f->f_pos = pos;
        return write_vaddr_disk(v, is);
    }

    return s;
}