Skip to content

Commit 948098e

Browse files
committed
Step.30 direct_IO method of address_space
1 parent 118888e commit 948098e

25 files changed

+12031
-0
lines changed

030_direct_io/Makefile

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
ME2FS_SOURCE = me2fs_main.c me2fs_super.c me2fs_block.c me2fs_inode.c \
2+
me2fs_dir.c me2fs_namei.c me2fs_file.c me2fs_ialloc.c \
3+
me2fs_symlink.c me2fs_sysfs.c me2fs_ioctl.c
4+
5+
obj-m += me2fs.o
6+
me2fs-objs := $(ME2FS_SOURCE:.c=.o)
7+
8+
all:
9+
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
10+
11+
insmod:
12+
sudo insmod me2fs.ko
13+
14+
rmmod:
15+
sudo rmmod me2fs.ko
16+
17+
mount:
18+
sudo mount -t me2fs -o loop ../ext2.img ../mnt
19+
20+
mount2:
21+
sudo insmod me2fs.ko
22+
sudo mount -t me2fs -o loop ../ext2.img ../mnt
23+
24+
remount:
25+
sudo mount -o remount$(OPT) ../mnt
26+
27+
umount:
28+
sudo umount ../mnt
29+
30+
umount2:
31+
sudo umount ../mnt
32+
sudo rmmod me2fs.ko
33+
34+
clean:
35+
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

030_direct_io/direct_io.c

+186
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
/********************************************************************************
2+
File : direct_io.c
3+
Description : example of direct io
4+
5+
*********************************************************************************/
6+
#define _GNU_SOURCE
7+
#include <stdio.h>
8+
#include <errno.h>
9+
#include <string.h>
10+
#include <stdlib.h>
11+
12+
#include <sys/types.h>
13+
#include <sys/stat.h>
14+
#include <unistd.h>
15+
#include <fcntl.h>
16+
17+
/*
18+
==================================================================================
19+
20+
Prototype Statement
21+
22+
==================================================================================
23+
*/
24+
25+
/*
26+
==================================================================================
27+
28+
DEFINES
29+
30+
==================================================================================
31+
*/
32+
33+
/*
34+
==================================================================================
35+
36+
Management
37+
38+
==================================================================================
39+
*/
40+
static char write_strings[ ] = "direct io example";
41+
42+
/*
43+
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
44+
45+
< Open Functions >
46+
47+
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
48+
*/
49+
/*
50+
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
51+
Function :main
52+
Input :int argc
53+
< number of arguments >
54+
char *argv[ ]
55+
< arguments >
56+
Output :void
57+
Return :int
58+
< result >
59+
60+
Description :direct io
61+
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
62+
*/
63+
int main( int argc, char *argv[ ] )
64+
{
65+
int err;
66+
67+
char *pathname;
68+
int fd;
69+
long align;
70+
char *buf = NULL;
71+
struct stat stat_buf;
72+
size_t block_size;
73+
74+
if( argc < 2 )
75+
{
76+
fprintf( stderr, "./%s pathname\n", argv[ 0 ] );
77+
return( -1 );
78+
}
79+
80+
/* ------------------------------------------------------------------------ */
81+
/* open a file with O_DIRECT flag */
82+
/* ------------------------------------------------------------------------ */
83+
pathname = argv[ 1 ];
84+
85+
fd = open( pathname, O_CREAT | O_RDWR | O_DIRECT, 0664 );
86+
87+
if( fd < 0 )
88+
{
89+
perror( "open : " );
90+
return( fd );
91+
}
92+
93+
/* ------------------------------------------------------------------------ */
94+
/* get block size of the filesystem */
95+
/* ------------------------------------------------------------------------ */
96+
err = fstat( fd, &stat_buf );
97+
98+
if( err < 0 )
99+
{
100+
perror( "fstat : " );
101+
goto err_close;
102+
}
103+
104+
block_size = ( size_t )stat_buf.st_blksize;
105+
106+
/* ------------------------------------------------------------------------ */
107+
/* get alignment restriction */
108+
/* ------------------------------------------------------------------------ */
109+
align = fpathconf( fd, _PC_REC_XFER_ALIGN );
110+
111+
if( align < 0 )
112+
{
113+
fprintf( stderr, "error : fpathconf : %ld\n", align );
114+
goto err_close;
115+
}
116+
117+
/* ------------------------------------------------------------------------ */
118+
/* allocate aligned memory */
119+
/* ------------------------------------------------------------------------ */
120+
err = posix_memalign( ( void** )&buf, ( size_t )align, block_size );
121+
122+
if( err < 0 )
123+
{
124+
perror( "posix_memalign : " );
125+
goto err_close;
126+
}
127+
128+
/* ------------------------------------------------------------------------ */
129+
/* write via direct io */
130+
/* ------------------------------------------------------------------------ */
131+
memset( buf, '\0', block_size );
132+
memcpy( buf, write_strings, sizeof( write_strings ) );
133+
134+
err = write( fd, buf, block_size );
135+
136+
if( err < 0 )
137+
{
138+
perror( "write : " );
139+
}
140+
141+
/* ------------------------------------------------------------------------ */
142+
/* free memory */
143+
/* ------------------------------------------------------------------------ */
144+
err_write:
145+
free( buf );
146+
147+
/* ------------------------------------------------------------------------ */
148+
/* close a file */
149+
/* ------------------------------------------------------------------------ */
150+
err_close:
151+
if( ( err = close( fd ) ) < 0 )
152+
{
153+
perror( "close : " );
154+
}
155+
156+
return( err );
157+
}
158+
159+
/*
160+
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
161+
Function :void
162+
Input :void
163+
Output :void
164+
Return :void
165+
166+
Description :void
167+
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
168+
*/
169+
170+
/*
171+
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
172+
173+
< Local Functions >
174+
175+
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
176+
*/
177+
/*
178+
==================================================================================
179+
Function :void
180+
Input :void
181+
Output :void
182+
Return :void
183+
184+
Description :void
185+
==================================================================================
186+
*/

0 commit comments

Comments
 (0)