Debian Codegraph @

tmpfs/node.c

raw
1 /* Node state and file contents 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 "tmpfs.h"
21 #include <stddef.h>
22 #include <stdlib.h>
23 #include <fcntl.h>
24 #include <mach/mach4.h>
25 #include <hurd/hurd_types.h>
26 #include <hurd/store.h>
27 #include "default_pager_U.h"
28 #include "libdiskfs/fs_S.h"
29
30 unsigned int num_files;
31 static unsigned int gen;
32
33 /* all_nodes is a list of all nodes.
34
35 Access to all_nodes and all_nodes_nr_items is protected by
36 all_nodes_lock.
37
38 Every node in all_nodes carries a light reference. When we are
39 asked to give up that light reference, we reacquire our lock
40 momentarily to check whether someone else reacquired a
41 reference. */
42 struct node *all_nodes;
43 static size_t all_nodes_nr_items;
44 pthread_rwlock_t all_nodes_lock = PTHREAD_RWLOCK_INITIALIZER;
45
46 error_t
47 diskfs_alloc_node (struct node *dp, mode_t mode, struct node **npp)
48 {
49 struct disknode *dn;
50
51 dn = calloc (1, sizeof *dn);
52 if (dn == 0)
53 return ENOSPC;
54
55 if (round_page (get_used () + sizeof *dn) / vm_page_size
56 > tmpfs_page_limit)
57 {
58 pthread_rwlock_unlock (&all_nodes_lock);
59 free (dn);
60 return ENOSPC;
61 }
62 dn->gen = gen++;
63 __atomic_add_fetch (&num_files, 1, __ATOMIC_RELAXED);
64 adjust_used (sizeof *dn);
65
66 dn->type = IFTODT (mode & S_IFMT);
67 return diskfs_cached_lookup ((ino_t) (uintptr_t) dn, npp);
68 }
69
70 void
71 diskfs_free_node (struct node *np, mode_t mode)
72 {
73 switch (np->dn->type)
74 {
75 case DT_REG:
76 if (np->dn->u.reg.memobj != MACH_PORT_NULL) {
77 /* XXX GNU Mach will terminate the object, and thus existing mappings
78 * will get SIGBUS. */
79 vm_deallocate (mach_task_self (), np->dn->u.reg.memref, 4096);
80 mach_port_deallocate (mach_task_self (), np->dn->u.reg.memobj);
81 }
82 break;
83 case DT_DIR:
84 assert_backtrace (np->dn->u.dir.entries == 0);
85 break;
86 case DT_LNK:
87 free (np->dn->u.lnk);
88 break;
89 }
90
91 pthread_rwlock_wrlock (&all_nodes_lock);
92 *np->dn->hprevp = np->dn->hnext;
93 if (np->dn->hnext != 0)
94 np->dn->hnext->dn->hprevp = np->dn->hprevp;
95 all_nodes_nr_items -= 1;
96 pthread_rwlock_unlock (&all_nodes_lock);
97
98 free (np->dn);
99 np->dn = 0;
100
101 __atomic_sub_fetch (&num_files, 1, __ATOMIC_RELAXED);
102 adjust_used (-sizeof *np->dn);
103 }
104
105 void
106 diskfs_node_norefs (struct node *np)
107 {
108 if (np->dn != 0)
109 {
110 /* We don't bother to do this in diskfs_write_disknode, since it only
111 ever matters here. The node state goes back into the `struct
112 disknode' while it has no associated diskfs node. */
113
114 np->dn->size = np->dn_stat.st_size;
115 np->dn->mode = np->dn_stat.st_mode;
116 np->dn->nlink = np->dn_stat.st_nlink;
117 np->dn->uid = np->dn_stat.st_uid;
118 np->dn->author = np->dn_stat.st_author;
119 np->dn->gid = np->dn_stat.st_gid;
120 np->dn->atime = np->dn_stat.st_atim;
121 np->dn->mtime = np->dn_stat.st_mtim;
122 np->dn->ctime = np->dn_stat.st_ctim;
123 np->dn->flags = np->dn_stat.st_flags;
124
125 switch (np->dn->type)
126 {
127 case DT_REG:
128 assert_backtrace (np->allocsize % vm_page_size == 0);
129 np->dn->u.reg.allocpages = np->allocsize / vm_page_size;
130 break;
131 case DT_CHR:
132 case DT_BLK:
133 np->dn->u.chr = np->dn_stat.st_rdev;
134 break;
135 }
136 }
137
138 free (np);
139 }
140
141 static void
142 recompute_blocks (struct node *np)
143 {
144 struct disknode *const dn = np->dn;
145 struct stat *const st = &np->dn_stat;
146
147 st->st_blocks = sizeof *dn + dn->translen;
148 switch (dn->type)
149 {
150 case DT_REG:
151 np->allocsize = dn->u.reg.allocpages * vm_page_size;
152 st->st_blocks += np->allocsize;
153 break;
154 case DT_LNK:
155 st->st_blocks += st->st_size + 1;
156 break;
157 case DT_CHR:
158 case DT_BLK:
159 st->st_rdev = dn->u.chr;
160 break;
161 case DT_DIR:
162 st->st_blocks += dn->size;
163 break;
164 }
165 st->st_blocks = (st->st_blocks + 511) / 512;
166 }
167
168 /* Fetch inode INUM, set *NPP to the node structure;
169 gain one user reference and lock the node. */
170 error_t
171 diskfs_cached_lookup (ino_t inum, struct node **npp)
172 {
173 struct disknode *dn = (void *) (uintptr_t) inum;
174 struct node *np;
175
176 assert_backtrace (npp);
177
178 pthread_rwlock_rdlock (&all_nodes_lock);
179 if (dn->hprevp != 0) /* There is already a node. */
180 goto gotit;
181 else
182 /* Create the new node. */
183 {
184 struct stat *st;
185 pthread_rwlock_unlock (&all_nodes_lock);
186
187 np = diskfs_make_node (dn);
188 np->cache_id = (ino_t) (uintptr_t) dn;
189
190 pthread_rwlock_wrlock (&all_nodes_lock);
191 if (dn->hprevp != NULL)
192 {
193 /* We lost a race. */
194 diskfs_nrele (np);
195 goto gotit;
196 }
197
198 dn->hnext = all_nodes;
199 if (dn->hnext)
200 dn->hnext->dn->hprevp = &dn->hnext;
201 dn->hprevp = &all_nodes;
202 all_nodes = np;
203 all_nodes_nr_items += 1;
204 diskfs_nref_light (np);
205 pthread_rwlock_unlock (&all_nodes_lock);
206
207 st = &np->dn_stat;
208 memset (st, 0, sizeof *st);
209 st->st_fstype = FSTYPE_MEMFS;
210 st->st_fsid = getpid ();
211 st->st_blksize = vm_page_size;
212
213 st->st_ino = (ino_t) (uintptr_t) dn;
214 st->st_gen = dn->gen;
215
216 st->st_size = dn->size;
217 st->st_mode = dn->mode;
218 st->st_nlink = dn->nlink;
219 st->st_uid = dn->uid;
220 st->st_author = dn->author;
221 st->st_gid = dn->gid;
222 st->st_atim = dn->atime;
223 st->st_mtim = dn->mtime;
224 st->st_ctim = dn->ctime;
225 st->st_flags = dn->flags;
226
227 st->st_rdev = 0;
228 np->allocsize = 0;
229 recompute_blocks (np);
230 }
231
232 pthread_mutex_lock (&np->lock);
233 *npp = np;
234 return 0;
235
236 gotit:
237 np = *dn->hprevp;
238 assert_backtrace (np->dn == dn);
239 assert_backtrace (*dn->hprevp == np);
240 diskfs_nref (np);
241 pthread_rwlock_unlock (&all_nodes_lock);
242 pthread_mutex_lock (&np->lock);
243 *npp = np;
244 return 0;
245 }
246
247 error_t
248 diskfs_node_iterate (error_t (*fun) (struct node *))
249 {
250 error_t err = 0;
251 size_t num_nodes;
252 struct node *node, **node_list, **p;
253
254 pthread_rwlock_rdlock (&all_nodes_lock);
255
256 /* We must copy everything from the hash table into another data structure
257 to avoid running into any problems with the hash-table being modified
258 during processing (normally we delegate access to hash-table with
259 all_nodes_lock, but we can't hold this while locking the
260 individual node locks). */
261
262 num_nodes = all_nodes_nr_items;
263
264 p = node_list = alloca (num_nodes * sizeof (struct node *));
265 for (node = all_nodes; node != 0; node = node->dn->hnext)
266 {
267 *p++ = node;
268
269 /* We acquire a hard reference for node, but without using
270 diskfs_nref. We do this so that diskfs_new_hardrefs will not
271 get called. */
272 refcounts_ref (&node->refcounts, NULL);
273 }
274
275 pthread_rwlock_unlock (&all_nodes_lock);
276
277 p = node_list;
278 while (num_nodes-- > 0)
279 {
280 node = *p++;
281 if (!err)
282 {
283 pthread_mutex_lock (&node->lock);
284 err = (*fun) (node);
285 pthread_mutex_unlock (&node->lock);
286 }
287 diskfs_nrele (node);
288 }
289
290 return err;
291 }
292
293 /* The user must define this function. Node NP has some light
294 references, but has just lost its last hard references. Take steps
295 so that if any light references can be freed, they are. NP is locked
296 as is the pager refcount lock. This function will be called after
297 diskfs_lost_hardrefs. */
298 void
299 diskfs_try_dropping_softrefs (struct node *np)
300 {
301 pthread_rwlock_wrlock (&all_nodes_lock);
302 if (np->cache_id != 0)
303 {
304 /* Check if someone reacquired a reference. */
305 struct references result;
306 refcounts_references (&np->refcounts, &result);
307
308 if (result.hard > 0)
309 {
310 /* A reference was reacquired. It's fine, we didn't touch
311 anything yet. */
312 pthread_rwlock_unlock (&all_nodes_lock);
313 return;
314 }
315
316 /* Just let go of the weak reference. The node will be removed
317 from all_nodes in diskfs_free_node. */
318 np->cache_id = 0;
319 diskfs_nrele_light (np);
320 }
321 pthread_rwlock_unlock (&all_nodes_lock);
322 }
323
324 /* The user must define this funcction. Node NP has some light
325 references but has just lost its last hard reference. NP is locked. */
326 void
327 diskfs_lost_hardrefs (struct node *np)
328 {
329 }
330
331 /* The user must define this function. Node NP has just acquired
332 a hard reference where it had none previously. It is thus now
333 OK again to have light references without real users. NP is
334 locked. */
335 void
336 diskfs_new_hardrefs (struct node *np)
337 {
338 }
339
340
341
342 error_t
343 diskfs_get_translator (struct node *np, char **namep, mach_msg_type_number_t *namelen)
344 {
345 *namelen = np->dn->translen;
346 if (*namelen == 0)
347 return 0;
348 *namep = malloc (*namelen);
349 if (*namep == 0)
350 return ENOMEM;
351 memcpy (*namep, np->dn->trans, *namelen);
352 return 0;
353 }
354
355 error_t
356 diskfs_set_translator (struct node *np,
357 const char *name, mach_msg_type_number_t namelen,
358 struct protid *cred)
359 {
360 char *new;
361 if (namelen == 0)
362 {
363 free (np->dn->trans);
364 new = 0;
365 np->dn_stat.st_mode &= ~S_IPTRANS;
366 }
367 else
368 {
369 new = realloc (np->dn->trans, namelen);
370 if (new == 0)
371 return ENOSPC;
372 memcpy (new, name, namelen);
373 np->dn_stat.st_mode |= S_IPTRANS;
374 }
375 adjust_used (namelen - np->dn->translen);
376 np->dn->trans = new;
377 np->dn->translen = namelen;
378 recompute_blocks (np);
379 return 0;
380 }
381
382 static error_t
383 create_symlink_hook (struct node *np, const char *target)
384 {
385 assert_backtrace (np->dn->u.lnk == 0);
386 np->dn_stat.st_size = strlen (target);
387 if (np->dn_stat.st_size > 0)
388 {
389 const size_t size = np->dn_stat.st_size + 1;
390 np->dn->u.lnk = malloc (size);
391 if (np->dn->u.lnk == 0)
392 return ENOSPC;
393 memcpy (np->dn->u.lnk, target, size);
394 np->dn->type = DT_LNK;
395 adjust_used (size);
396 recompute_blocks (np);
397 }
398 return 0;
399 }
400 error_t (*diskfs_create_symlink_hook)(struct node *np, const char *target)
401 = create_symlink_hook;
402
403 static error_t
404 read_symlink_hook (struct node *np, char *target)
405 {
406 memcpy (target, np->dn->u.lnk, np->dn_stat.st_size + 1);
407 return 0;
408 }
409 error_t (*diskfs_read_symlink_hook)(struct node *np, char *target)
410 = read_symlink_hook;
411
412 void
413 diskfs_write_disknode (struct node *np, int wait)
414 {
415 }
416
417 void
418 diskfs_file_update (struct node *np, int wait)
419 {
420 diskfs_node_update (np, wait);
421 }
422
423 error_t
424 diskfs_node_reload (struct node *node)
425 {
426 return 0;
427 }
428
429
430 /* The user must define this function. Truncate locked node NP to be SIZE
431 bytes long. (If NP is already less than or equal to SIZE bytes
432 long, do nothing.) If this is a symlink (and diskfs_shortcut_symlink
433 is set) then this should clear the symlink, even if
434 diskfs_create_symlink_hook stores the link target elsewhere. */
435 error_t
436 diskfs_truncate (struct node *np, off_t size)
437 {
438 if (np->dn->type == DT_LNK)
439 {
440 free (np->dn->u.lnk);
441 adjust_used (size - np->dn_stat.st_size);
442 np->dn->u.lnk = 0;
443 np->dn_stat.st_size = size;
444 return 0;
445 }
446
447 if (np->allocsize <= size)
448 return 0;
449
450 assert_backtrace (np->dn->type == DT_REG);
451
452 if (default_pager == MACH_PORT_NULL)
453 return EIO;
454
455 np->dn_stat.st_size = size;
456
457 off_t set_size = size;
458 size = round_page (size);
459
460 if (np->dn->u.reg.memobj != MACH_PORT_NULL)
461 {
462 error_t err = default_pager_object_set_size (np->dn->u.reg.memobj, set_size);
463 if (err == MIG_BAD_ID)
464 /* This is an old default pager. We have no way to truncate the
465 memory object. Note that the behavior here will be wrong in
466 two ways: user accesses past the end won't fault; and, more
467 importantly, later growing the file won't zero the contents
468 past the size we just supposedly truncated to. For proper
469 behavior, use a new default pager. */
470 return 0;
471 if (err)
472 return err;
473 }
474 /* Otherwise it never had any real contents. */
475
476 adjust_used (size - np->allocsize);
477 np->dn_stat.st_blocks += (size - np->allocsize) / 512;
478 np->allocsize = size;
479
480 return 0;
481 }
482
483 /* The user must define this function. Grow the disk allocated to locked node
484 NP to be at least SIZE bytes, and set NP->allocsize to the actual
485 allocated size. (If the allocated size is already SIZE bytes, do
486 nothing.) CRED identifies the user responsible for the call. */
487 error_t
488 diskfs_grow (struct node *np, off_t size, struct protid *cred)
489 {
490 assert_backtrace (np->dn->type == DT_REG);
491
492 if (np->allocsize >= size)
493 return 0;
494
495 off_t set_size = size;
496 size = round_page (size);
497 if (round_page (get_used () + size - np->allocsize)
498 / vm_page_size > tmpfs_page_limit)
499 return ENOSPC;
500
501 if (default_pager == MACH_PORT_NULL)
502 return EIO;
503
504 if (np->dn->u.reg.memobj != MACH_PORT_NULL)
505 {
506 /* Increase the limit the memory object will allow to be accessed. */
507 error_t err = default_pager_object_set_size (np->dn->u.reg.memobj, set_size);
508 if (err == MIG_BAD_ID) /* Old default pager, never limited it. */
509 err = 0;
510 if (err)
511 return err;
512 }
513
514 adjust_used (size - np->allocsize);
515 np->dn_stat.st_blocks += (size - np->allocsize) / 512;
516 np->allocsize = size;
517 return 0;
518 }
519
520 mach_port_t
521 diskfs_get_filemap (struct node *np, vm_prot_t prot)
522 {
523 error_t err;
524 mach_port_t right;
525
526 if (np->dn->type != DT_REG)
527 {
528 errno = EOPNOTSUPP; /* ? */
529 return MACH_PORT_NULL;
530 }
531
532 if (default_pager == MACH_PORT_NULL)
533 {
534 errno = EIO;
535 return MACH_PORT_NULL;
536 }
537
538 /* We don't bother to create the memory object until the first time we
539 need it (i.e. first mapping or i/o). This way we might have a clue
540 what size it's going to be beforehand, so we can tell the default
541 pager how big to make its bitmaps. This is just an optimization for
542 the default pager; the memory object can be expanded at any time just
543 by accessing more of it. (It also optimizes the case of empty files
544 so we might never make a memory object at all.) */
545 if (np->dn->u.reg.memobj == MACH_PORT_NULL)
546 {
547 error_t err = default_pager_object_create (default_pager,
548 &np->dn->u.reg.memobj,
549 np->allocsize);
550 if (err)
551 {
552 errno = err;
553 return MACH_PORT_NULL;
554 }
555 assert_backtrace (np->dn->u.reg.memobj != MACH_PORT_NULL);
556
557 /* XXX we need to keep a reference to the object, or GNU Mach
558 will terminate it when we release the map. */
559 np->dn->u.reg.memref = 0;
560 vm_map (mach_task_self (), &np->dn->u.reg.memref, 4096, 0, 1,
561 np->dn->u.reg.memobj, 0, 0, VM_PROT_NONE, VM_PROT_NONE,
562 VM_INHERIT_NONE);
563 assert_perror_backtrace (err);
564 }
565
566 if (prot & VM_PROT_WRITE)
567 right = np->dn->u.reg.memobj;
568 else
569 {
570 vm_offset_t offset = 0;
571 vm_offset_t start = 0;
572 vm_size_t len = ~0;
573 err = memory_object_create_proxy (mach_task_self (),
574 VM_PROT_READ | VM_PROT_EXECUTE,
575 &np->dn->u.reg.memobj,
576 MACH_MSG_TYPE_COPY_SEND, 1,
577 &offset, 1, &start, 1, &len, 1,
578 &right);
579 if (err)
580 {
581 errno = err;
582 return MACH_PORT_NULL;
583 }
584 }
585
586 /* Add a reference for each call, the caller will deallocate it. */
587 err = mach_port_mod_refs (mach_task_self (), np->dn->u.reg.memobj,
588 MACH_PORT_RIGHT_SEND, +1);
589 assert_perror_backtrace (err);
590
591 return right;
592 }
593
594 /* The user must define this function. Return a `struct pager *' suitable
595 for use as an argument to diskfs_register_memory_fault_area that
596 refers to the pager returned by diskfs_get_filemap for node NP.
597 NP is locked. */
598 struct pager *
599 diskfs_get_filemap_pager_struct (struct node *np)
600 {
601 return 0;
602 }
603
604 /* We have no pager of our own, so there is no need to worry about
605 users of it, or to shut it down. */
606 int
607 diskfs_pager_users (void)
608 {
609 return 0;
610 }
611 void
612 diskfs_shutdown_pager (void)
613 {
614 }
615
616 /* The purpose of this is to decide that it's ok to make the fs read-only.
617 Turning a temporary filesystem read-only seem pretty useless. */
618 vm_prot_t
619 diskfs_max_user_pager_prot (void)
620 {
621 return VM_PROT_READ; /* Probable lie that lets us go read-only. */
622 }
623
624 kern_return_t
625 diskfs_S_file_get_storage_info (struct protid *cred,
626 mach_port_t **ports,
627 mach_msg_type_name_t *ports_type,
628 mach_msg_type_number_t *num_ports,
629 int **ints, mach_msg_type_number_t *num_ints,
630 off_t **offsets,
631 mach_msg_type_number_t *num_offsets,
632 data_t *data, mach_msg_type_number_t *data_len)
633 {
634 mach_port_t memobj = diskfs_get_filemap (cred->po->np, VM_PROT_ALL);
635 if (memobj == MACH_PORT_NULL)
636 return errno;
637
638 assert_backtrace (*num_ports >= 1); /* mig always gives us some */
639 *num_ports = 1;
640 *ports_type = MACH_MSG_TYPE_MOVE_SEND;
641 (*ports)[0]
642 = (cred->po->openstat & O_RDWR) == O_RDWR ? memobj : MACH_PORT_NULL;
643
644 assert_backtrace (*num_offsets >= 2); /* mig always gives us some */
645 *num_offsets = 2;
646 (*offsets)[0] = 0;
647 (*offsets)[1] = cred->po->np->dn_stat.st_size;
648
649 assert_backtrace (*num_ints >= 6); /* mig always gives us some */
650 *num_ints = 6;
651 (*ints)[0] = STORAGE_MEMORY;
652 (*ints)[1] = (cred->po->openstat & O_WRITE) ? 0 : STORE_READONLY;
653 (*ints)[2] = 1; /* block size */
654 (*ints)[3] = 1; /* 1 run in offsets list */
655 (*ints)[4] = 0; /* name len */
656 (*ints)[5] = 0; /* misc len */
657
658 *data_len = 0;
659
660 return 0;
661 }