]> mj.ucw.cz Git - libucw.git/blob - ucw/fb-param.c
Merge branch 'v3.12.4'
[libucw.git] / ucw / fb-param.c
1 /*
2  *      UCW Library -- FastIO on files with run-time parametrization
3  *
4  *      (c) 2007 Pavel Charvat <pchar@ucw.cz>
5  *      (c) 2007 Martin Mares <mj@ucw.cz>
6  *
7  *      This software may be freely distributed and used according to the terms
8  *      of the GNU Lesser General Public License.
9  */
10
11 #include "ucw/lib.h"
12 #include "ucw/conf.h"
13 #include "ucw/lfs.h"
14 #include "ucw/fastbuf.h"
15
16 #include <fcntl.h>
17 #include <stdio.h>
18
19 struct fb_params fbpar_def = {
20   .buffer_size = 65536,
21   .read_ahead = 1,
22   .write_back = 1,
23 };
24
25 static char *
26 fbpar_cf_commit(struct fb_params *p UNUSED)
27 {
28 #ifndef CONFIG_UCW_THREADS
29   if (p->type == FB_DIRECT)
30     return "Direct I/O is supported only with CONFIG_UCW_THREADS";
31 #endif
32   return NULL;
33 }
34
35 struct cf_section fbpar_cf = {
36 # define F(x) PTR_TO(struct fb_params, x)
37   CF_TYPE(struct fb_params),
38   CF_COMMIT(fbpar_cf_commit),
39   CF_ITEMS {
40     CF_LOOKUP("Type", (int *)F(type), ((const char * const []){"std", "direct", "mmap", NULL})),
41     CF_UNS("BufSize", F(buffer_size)),
42     CF_UNS("KeepBackBuf", F(keep_back_buf)),
43     CF_UNS("ReadAhead", F(read_ahead)),
44     CF_UNS("WriteBack", F(write_back)),
45     CF_END
46   }
47 # undef F
48 };
49
50 static struct cf_section fbpar_global_cf = {
51   CF_ITEMS {
52     CF_SECTION("Defaults", &fbpar_def, &fbpar_cf),
53     CF_END
54   }
55 };
56
57 static void CONSTRUCTOR
58 fbpar_global_init(void)
59 {
60   cf_declare_section("FBParam", &fbpar_global_cf, 0);
61 }
62
63 static struct fastbuf *
64 bopen_fd_internal(int fd, struct fb_params *params, uns mode, const char *name)
65 {
66   char buf[32];
67   if (!name)
68     {
69       sprintf(buf, "fd%d", fd);
70       name = buf;
71     }
72   struct fastbuf *fb;
73   switch (params->type)
74     {
75 #ifdef CONFIG_UCW_THREADS
76       case FB_DIRECT:
77         fb = fbdir_open_fd_internal(fd, name, params->asio,
78             params->buffer_size ? : fbpar_def.buffer_size,
79             params->read_ahead ? : fbpar_def.read_ahead,
80             params->write_back ? : fbpar_def.write_back);
81         if (!~mode && !fbdir_cheat && ((int)(mode = fcntl(fd, F_GETFL)) < 0 || fcntl(fd, F_SETFL, mode | O_DIRECT)) < 0)
82           msg(L_WARN, "Cannot set O_DIRECT on fd %d: %m", fd);
83         return fb;
84 #endif
85       case FB_STD:
86         fb = bfdopen_internal(fd, name,
87             params->buffer_size ? : fbpar_def.buffer_size);
88         if (params->keep_back_buf)
89           bconfig(fb, BCONFIG_KEEP_BACK_BUF, 1);
90         return fb;
91       case FB_MMAP:
92         if (!~mode && (int)(mode = fcntl(fd, F_GETFL)) < 0)
93           die("Cannot get flags of fd %d: %m", fd);
94         return bfmmopen_internal(fd, name, mode);
95       default:
96         ASSERT(0);
97     }
98 }
99
100 static struct fastbuf *
101 bopen_file_internal(const char *name, int mode, struct fb_params *params, int try)
102 {
103   if (!params)
104     params = &fbpar_def;
105 #ifdef CONFIG_UCW_THREADS
106   if (params->type == FB_DIRECT && !fbdir_cheat)
107     mode |= O_DIRECT;
108 #endif
109   if (params->type == FB_MMAP && (mode & O_ACCMODE) == O_WRONLY)
110     mode = (mode & ~O_ACCMODE) | O_RDWR;
111   int fd = ucw_open(name, mode, 0666);
112   if (fd < 0)
113     if (try)
114       return NULL;
115     else
116       die("Unable to %s file %s: %m", (mode & O_CREAT) ? "create" : "open", name);
117   struct fastbuf *fb = bopen_fd_internal(fd, params, mode, name);
118   ASSERT(fb);
119   if (mode & O_APPEND)
120     bseek(fb, 0, SEEK_END);
121   return fb;
122 }
123
124 struct fastbuf *
125 bopen_file(const char *name, int mode, struct fb_params *params)
126 {
127   return bopen_file_internal(name, mode, params, 0);
128 }
129
130 struct fastbuf *
131 bopen_file_try(const char *name, int mode, struct fb_params *params)
132 {
133   return bopen_file_internal(name, mode, params, 1);
134 }
135
136 struct fastbuf *
137 bopen_fd_name(int fd, struct fb_params *params, const char *name)
138 {
139   return bopen_fd_internal(fd, params ? : &fbpar_def, ~0U, name);
140 }
141
142 /* Function for use by individual file back-ends */
143
144 void
145 bclose_file_helper(struct fastbuf *f, int fd, int is_temp_file)
146 {
147   switch (is_temp_file)
148     {
149     case 1:
150       if (unlink(f->name) < 0)
151         msg(L_ERROR, "unlink(%s): %m", f->name);
152     case 0:
153       if (close(fd))
154         die("close(%s): %m", f->name);
155     }
156 }
157
158 /* Compatibility wrappers */
159
160 struct fastbuf *
161 bopen_try(const char *name, uns mode, uns buflen)
162 {
163   return bopen_file_try(name, mode, &(struct fb_params){ .type = FB_STD, .buffer_size = buflen });
164 }
165
166 struct fastbuf *
167 bopen(const char *name, uns mode, uns buflen)
168 {
169   return bopen_file(name, mode, &(struct fb_params){ .type = FB_STD, .buffer_size = buflen });
170 }
171
172 struct fastbuf *
173 bfdopen(int fd, uns buflen)
174 {
175   return bopen_fd(fd, &(struct fb_params){ .type = FB_STD, .buffer_size = buflen });
176 }
177
178 struct fastbuf *
179 bfdopen_shared(int fd, uns buflen)
180 {
181   struct fastbuf *f = bfdopen(fd, buflen);
182   bconfig(f, BCONFIG_IS_TEMP_FILE, 2);
183   return f;
184 }