Debian Codegraph @

tmpfs/tmpfs.c

raw
1 /* Main program and global state for tmpfs.
2 Copyright (C) 2000,01,02 Free Software Foundation, Inc.
3
4 This file is part of the GNU Hurd.
5
6 The GNU Hurd is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 The GNU Hurd is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with the GNU Hurd; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20 #include <argp.h>
21 #include <argz.h>
22 #include <string.h>
23 #include <inttypes.h>
24 #include <error.h>
25 #include <pthread.h>
26
27 #include "tmpfs.h"
28 #include <limits.h>
29 #include <version.h>
30 #include <fcntl.h>
31 #include <hurd.h>
32 #include <hurd/paths.h>
33
34 char *diskfs_server_name = "tmpfs";
35 char *diskfs_server_version = HURD_VERSION;
36 char *diskfs_extra_version = "GNU Hurd";
37 char *diskfs_disk_name = "none";
38
39 /* We ain't got to show you no stinkin' sync'ing. */
40 int diskfs_default_sync_interval = 0;
41
42 /* We must supply some claimed limits, though we don't impose any new ones. */
43 int diskfs_link_max = INT_MAX;
44 int diskfs_name_max = 255; /* dirent d_namlen limit */
45 int diskfs_maxsymlinks = 8;
46
47 /* Yeah, baby, we do it all! */
48 int diskfs_shortcut_symlink = 1;
49 int diskfs_shortcut_chrdev = 1;
50 int diskfs_shortcut_blkdev = 1;
51 int diskfs_shortcut_fifo = 1;
52 int diskfs_shortcut_ifsock = 1;
53
54 struct node *diskfs_root_node;
55 mach_port_t default_pager;
56
57 off_t tmpfs_page_limit, tmpfs_space_used;
58 mode_t tmpfs_root_mode = -1;
59
60 error_t
61 diskfs_set_statfs (struct statfs *st)
62 {
63 fsblkcnt_t pages;
64
65 st->f_type = FSTYPE_MEMFS;
66 st->f_fsid = getpid ();
67
68 st->f_bsize = vm_page_size;
69 st->f_blocks = tmpfs_page_limit;
70
71 st->f_files = __atomic_load_n (&num_files, __ATOMIC_RELAXED);
72 pages = round_page (get_used ()) / vm_page_size;
73
74 st->f_bfree = pages < tmpfs_page_limit ? tmpfs_page_limit - pages : 0;
75 st->f_bavail = st->f_bfree;
76 st->f_ffree = st->f_bavail / sizeof (struct disknode); /* Well, sort of. */
77
78 return 0;
79 }
80
81
82 error_t
83 diskfs_set_hypermetadata (int wait, int clean)
84 {
85 /* All the state always just lives in core, so we have nothing to do. */
86 return 0;
87 }
88
89 void
90 diskfs_sync_everything (int wait)
91 {
92 }
93
94 error_t
95 diskfs_reload_global_state (void)
96 {
97 return 0;
98 }
99
100 int diskfs_synchronous = 0;
101
102 #define OPT_SIZE 600 /* --size */
103
104 static const struct argp_option options[] =
105 {
106 {"mode", 'm', "MODE", 0, "Permissions (octal) for root directory"},
107 {"size", OPT_SIZE, "MAX-BYTES", 0, "Maximum size"},
108 {NULL,}
109 };
110
111 struct option_values
112 {
113 off_t size;
114 mode_t mode;
115 };
116
117 /* Parse the size string ARG, and set *NEWSIZE with the resulting size. */
118 static error_t
119 parse_opt_size (const char *arg, struct argp_state *state, off_t *newsize)
120 {
121 char *end = NULL;
122 intmax_t size = strtoimax (arg, &end, 0);
123 if (end == NULL || end == arg)
124 {
125 argp_error (state, "argument must be a number");
126 return EINVAL;
127 }
128 if (size < 0)
129 {
130 argp_error (state, "negative size not meaningful");
131 return EINVAL;
132 }
133 switch (*end)
134 {
135 case 'g':
136 case 'G':
137 size <<= 10;
138 case 'm':
139 case 'M':
140 size <<= 10;
141 case 'k':
142 case 'K':
143 size <<= 10;
144 break;
145 case '%':
146 {
147 /* Set as a percentage of the machine's physical memory. */
148 struct vm_statistics vmstats;
149 error_t err = vm_statistics (mach_task_self (), &vmstats);
150 if (err)
151 {
152 argp_error (state, "cannot find total physical memory: %s",
153 strerror (err));
154 return err;
155 }
156 size = round_page ((((vmstats.free_count
157 + vmstats.active_count
158 + vmstats.inactive_count
159 + vmstats.wire_count)
160 * vm_page_size)
161 * size + 99) / 100);
162 break;
163 }
164 }
165 size = (off_t) size;
166 if (size < 0)
167 {
168 argp_error (state, "size too large");
169 return EINVAL;
170 }
171
172 *newsize = size;
173
174 return 0;
175 }
176
177 /* Parse a command line option. */
178 static error_t
179 parse_opt (int key, char *arg, struct argp_state *state)
180 {
181 /* We save our parsed values in this structure, hung off STATE->hook.
182 Only after parsing all options successfully will we use these values. */
183 struct option_values *values = state->hook;
184
185 switch (key)
186 {
187 case ARGP_KEY_INIT:
188 state->child_inputs[0] = state->input;
189 values = malloc (sizeof *values);
190 if (values == 0)
191 return ENOMEM;
192 state->hook = values;
193 values->size = -1;
194 values->mode = -1;
195 break;
196 case ARGP_KEY_FINI:
197 free (values);
198 state->hook = 0;
199 break;
200
201 case 'm': /* --mode=OCTAL */
202 {
203 char *end = NULL;
204 mode_t mode = strtoul (arg, &end, 8);
205 if (end == NULL || end == arg)
206 {
207 argp_error (state, "argument must be an octal number");
208 return EINVAL;
209 }
210 if (mode & S_IFMT)
211 {
212 argp_error (state, "invalid bits in mode");
213 return EINVAL;
214 }
215 values->mode = mode;
216 }
217 break;
218
219 case OPT_SIZE: /* --size=MAX-BYTES */
220 {
221 error_t err = parse_opt_size (arg, state, &values->size);
222 if (err)
223 return err;
224 }
225 break;
226
227 case ARGP_KEY_NO_ARGS:
228 if (values->size < 0)
229 {
230 argp_error (state, "must supply maximum size");
231 return EINVAL;
232 }
233 break;
234
235 case ARGP_KEY_ARGS:
236 if (state->argv[state->next + 1] != 0)
237 {
238 argp_error (state, "too many arguments");
239 return EINVAL;
240 }
241 else if (values->size >= 0)
242 {
243 if (strcmp (state->argv[state->next], "tmpfs") != 0)
244 {
245 argp_error (state, "size specified with --size and argument is not \"tmpfs\"");
246 return EINVAL;
247 }
248 }
249 else
250 {
251 error_t err = parse_opt_size (state->argv[state->next], state,
252 &values->size);
253 if (err)
254 return err;
255 }
256 break;
257
258 case ARGP_KEY_SUCCESS:
259 /* All options parse successfully, so implement ours if possible. */
260 tmpfs_page_limit = values->size / vm_page_size;
261 tmpfs_root_mode = values->mode;
262 break;
263
264 default:
265 return ARGP_ERR_UNKNOWN;
266 }
267 return 0;
268 }
269
270 /* Override the standard diskfs routine so we can add our own output. */
271 error_t
272 diskfs_append_args (char **argz, size_t *argz_len)
273 {
274 error_t err;
275
276 /* Get the standard things. */
277 err = diskfs_append_std_options (argz, argz_len);
278
279 if (!err)
280 {
281 off_t lim = tmpfs_page_limit * vm_page_size;
282 char buf[100], sfx;
283 #define S(n, c) if ((lim & ((1 << n) - 1)) == 0) sfx = c, lim >>= n
284 S (30, 'G'); else S (20, 'M'); else S (10, 'K'); else sfx = '\0';
285 #undef S
286 snprintf (buf, sizeof buf, "%" PRIi64 "%c", lim, sfx);
287 err = argz_add (argz, argz_len, buf);
288 }
289
290 return err;
291 }
292
293 /* Handling of operations for the ports in diskfs_port_bucket, calling
294 * demuxer for each incoming message */
295 static void *
296 diskfs_thread_function (void *demuxer)
297 {
298 static int thread_timeout = 1000 * 60 * 2; /* two minutes */
299 error_t err;
300
301 pthread_setname_np (pthread_self (), "diskfs");
302
303 do
304 {
305 ports_manage_port_operations_multithread (diskfs_port_bucket,
306 (ports_demuxer_type) demuxer,
307 thread_timeout,
308 0,
309 0);
310 err = diskfs_shutdown (0);
311 }
312 while (err);
313
314 exit (0);
315 /* NOTREACHED */
316 return NULL;
317 }
318
319
320 /* Add our startup arguments to the standard diskfs set. */
321 static const struct argp_child startup_children[] =
322 {{&diskfs_startup_argp}, {0}};
323 static struct argp startup_argp = {options, parse_opt, "MAX-BYTES", "\
324 \v\
325 MAX-BYTES may be followed by k or K for kilobytes,\n\
326 m or M for megabytes, g or G for gigabytes.",
327 startup_children};
328
329 /* Similarly at runtime. */
330 static const struct argp_child runtime_children[] =
331 {{&diskfs_std_runtime_argp}, {0}};
332 static struct argp runtime_argp = {options, parse_opt, 0, 0, runtime_children};
333
334 struct argp *diskfs_runtime_argp = (struct argp *)&runtime_argp;
335
336
337
338 int
339 main (int argc, char **argv)
340 {
341 error_t err;
342 mach_port_t bootstrap, realnode, host_priv;
343 pthread_t pthread_id;
344 struct stat st;
345
346 err = argp_parse (&startup_argp, argc, argv, ARGP_IN_ORDER, NULL, NULL);
347 assert_perror_backtrace (err);
348
349 task_get_bootstrap_port (mach_task_self (), &bootstrap);
350 if (bootstrap == MACH_PORT_NULL)
351 error (2, 0, "Must be started as a translator");
352
353 /* Get our port to the default pager. Without that,
354 we have no place to put file contents. */
355 err = get_privileged_ports (&host_priv, NULL);
356 if (err == EPERM)
357 {
358 default_pager = file_name_lookup (_SERVERS_DEFPAGER, O_EXEC, 0);
359 if (default_pager == MACH_PORT_NULL)
360 error (0, errno, _SERVERS_DEFPAGER);
361 }
362 else if (err)
363 error (0, err, "Cannot get host privileged port");
364 else
365 {
366 err = vm_set_default_memory_manager (host_priv, &default_pager);
367 if (err)
368 error (0, err, "Cannot get default pager port");
369 else
370 if (default_pager == MACH_PORT_NULL)
371 {
372 error (0, 0, "No default pager (memory manager) is running");
373 /* Try to auto-start it. */
374 err = system ("/hurd/mach-defpager");
375 if (err)
376 error (0, err, "Could not start it");
377 else
378 {
379 fprintf (stderr, "Started it\n");
380 err = vm_set_default_memory_manager (host_priv, &default_pager);
381 if (err)
382 error(0, err, "Cannot get default pager port");
383 }
384 }
385 mach_port_deallocate (mach_task_self (), host_priv);
386 }
387 if (default_pager == MACH_PORT_NULL)
388 error (0, 0, "files cannot have contents with no default pager port");
389
390 /* Initialize the diskfs library. Must come before any other diskfs call. */
391 err = diskfs_init_diskfs ();
392 if (err)
393 error (4, err, "init");
394
395 err = diskfs_alloc_node (0, S_IFDIR, &diskfs_root_node);
396 if (err)
397 error (4, err, "cannot create root directory");
398
399 /* Like diskfs_spawn_first_thread. But do it manually, without timeout */
400 err = pthread_create (&pthread_id, NULL, diskfs_thread_function,
401 diskfs_demuxer);
402 if (!err)
403 pthread_detach (pthread_id);
404 else
405 {
406 errno = err;
407 perror ("pthread_create");
408 }
409
410 /* Now that we are all set up to handle requests, and diskfs_root_node is
411 set properly, it is safe to export our fsys control port to the
412 outside world. */
413 realnode = diskfs_startup_diskfs (bootstrap, 0);
414 diskfs_root_node->dn_stat.st_mode = S_IFDIR;
415
416 /* Propagate permissions, owner, etc. from underlying node to
417 the root directory of the new (empty) filesystem. */
418 err = io_stat (realnode, &st);
419 if (err)
420 {
421 error (0, err, "cannot stat underlying node");
422 if (tmpfs_root_mode == -1)
423 diskfs_root_node->dn_stat.st_mode |= 0777 | S_ISVTX;
424 else
425 diskfs_root_node->dn_stat.st_mode |= tmpfs_root_mode;
426 diskfs_root_node->dn_set_ctime = 1;
427 diskfs_root_node->dn_set_mtime = 1;
428 diskfs_root_node->dn_set_atime = 1;
429 }
430 else
431 {
432 if (tmpfs_root_mode == -1)
433 {
434 diskfs_root_node->dn_stat.st_mode |= st.st_mode &~ S_IFMT;
435 if (S_ISREG (st.st_mode) && (st.st_mode & 0111) == 0)
436 /* There are no execute bits set, as by default on a plain file.
437 For the virtual directory, set execute bits where read bits are
438 set on the underlying plain file. */
439 diskfs_root_node->dn_stat.st_mode |= (st.st_mode & 0444) >> 2;
440 }
441 else
442 diskfs_root_node->dn_stat.st_mode |= tmpfs_root_mode;
443 diskfs_root_node->dn_stat.st_uid = st.st_uid;
444 diskfs_root_node->dn_stat.st_author = st.st_author;
445 diskfs_root_node->dn_stat.st_gid = st.st_gid;
446 diskfs_root_node->dn_stat.st_atim = st.st_atim;
447 diskfs_root_node->dn_stat.st_mtim = st.st_mtim;
448 diskfs_root_node->dn_stat.st_ctim = st.st_ctim;
449 diskfs_root_node->dn_stat.st_flags = st.st_flags;
450 }
451 diskfs_root_node->dn_stat.st_mode &= ~S_ITRANS;
452 diskfs_root_node->dn_stat.st_mode |= S_IROOT;
453 diskfs_root_node->dn_stat.st_nlink = 2;
454
455 /* We must keep the REALNODE send right to remain the active
456 translator for the underlying node. */
457
458 pthread_mutex_unlock (&diskfs_root_node->lock);
459
460 /* and so we die, leaving others to do the real work. */
461 pthread_exit (NULL);
462 /* NOTREACHED */
463 return 0;
464 }