This repository was archived by the owner on Apr 21, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathgim_file.c
More file actions
148 lines (121 loc) · 3.41 KB
/
gim_file.c
File metadata and controls
148 lines (121 loc) · 3.41 KB
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
/*
* Copyright (c) 2014-2017 Advanced Micro Devices, Inc. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE
*/
#include <linux/fs.h>
#include <asm/segment.h>
#include <linux/buffer_head.h>
#include <linux/version.h>
struct file *file_open(const char *path, int flags, int rights)
{
struct file *filp = NULL;
mm_segment_t oldfs;
int err = 0;
oldfs = get_fs();
set_fs(get_ds());
filp = filp_open(path, flags, rights);
set_fs(oldfs);
if (IS_ERR(filp)) {
err = PTR_ERR(filp);
return NULL;
}
return filp;
}
void file_close(struct file *file)
{
filp_close(file, NULL);
}
unsigned long long file_size(struct file *file)
{
mm_segment_t oldfs;
struct kstat ks;
oldfs = get_fs();
set_fs(get_ds());
#if !defined(XEN_DUNDEE) && (KERNEL_VERSION(3, 9, 0) > LINUX_VERSION_CODE)
/* 3.4.9 */
vfs_getattr(file->f_vfsmnt, file->f_dentry, &ks);
#elif LINUX_VERSION_CODE < KERNEL_VERSION(4, 11, 0)
/* 3.14.0 + */
vfs_getattr(&file->f_path, &ks);
#else
/* 4.11.0 + */
vfs_getattr(&file->f_path, &ks, STATX_TYPE | STATX_MODE,
AT_STATX_SYNC_AS_STAT);
#endif
set_fs(oldfs);
return ks.size;
}
int file_truncate(struct file *file, unsigned long long size)
{
int ret = 0;
struct iattr newattrs;
mm_segment_t oldfs;
newattrs.ia_valid = ATTR_SIZE | ATTR_FILE;
newattrs.ia_file = file;
newattrs.ia_size = size;
oldfs = get_fs();
set_fs(get_ds());
#if KERNEL_VERSION(3, 13, 0) > LINUX_VERSION_CODE
/* 3.4.9 */
ret = notify_change(file->f_path.dentry, &newattrs);
#else
/* to do */
/* 3.14.0 +
* ret = notify_change(file->f_path.dentry, &newattrs, NULL);
*/
#endif
set_fs(oldfs);
return ret;
}
int file_read(struct file *file, unsigned long long offset, unsigned char *data,
unsigned int size)
{
#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 14, 0)
mm_segment_t oldfs;
int ret;
oldfs = get_fs();
set_fs(get_ds());
ret = vfs_read(file, data, size, &offset);
set_fs(oldfs);
return ret;
#else
return kernel_read(file, data, size, &offset);
#endif
}
int file_write(struct file *file, unsigned long long offset,
unsigned char *data, unsigned int size)
{
#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 14, 0)
mm_segment_t oldfs;
int ret;
oldfs = get_fs();
set_fs(get_ds());
ret = vfs_write(file, data, size, &offset);
set_fs(oldfs);
return ret;
#else
return kernel_write(file, data, size, &offset);
#endif
}
int file_sync(struct file *file)
{
vfs_fsync(file, 0);
return 0;
}