]>
git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - io/mmap.c
1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2004-2005 Silicon Graphics, Inc.
14 static cmdinfo_t mmap_cmd
;
15 static cmdinfo_t mread_cmd
;
16 static cmdinfo_t msync_cmd
;
17 static cmdinfo_t munmap_cmd
;
18 static cmdinfo_t mwrite_cmd
;
20 static cmdinfo_t mremap_cmd
;
21 #endif /* HAVE_MREMAP */
23 mmap_region_t
*maptable
;
25 mmap_region_t
*mapping
;
33 char buffer
[8] = { 0 };
46 for (i
= 0, p
= pflags
; p
->prot
!= PROT_NONE
; i
++, p
++)
47 buffer
[i
] = (map
->prot
& p
->prot
) ? p
->mode
: '-';
50 sprintf(&buffer
[i
], " S");
52 printf("%c%03d%c 0x%lx - 0x%lx %s %14s (%lld : %ld)\n",
53 braces
? '[' : ' ', index
, braces
? ']' : ' ',
54 (unsigned long)map
->addr
,
55 (unsigned long)((char *)map
->addr
+ map
->length
),
56 buffer
, map
->name
? map
->name
: "???",
57 (long long)map
->offset
, (long)map
->length
);
69 if (offset
< mapping
->offset
) {
70 printf(_("offset (%lld) is before start of mapping (%lld)\n"),
71 (long long)offset
, (long long)mapping
->offset
);
74 relative
= offset
- mapping
->offset
;
75 if (relative
> mapping
->length
) {
76 printf(_("offset (%lld) is beyond end of mapping (%lld)\n"),
77 (long long)relative
, (long long)mapping
->offset
);
80 if ((relative
+ length
) > (mapping
->offset
+ mapping
->length
)) {
81 printf(_("range (%lld:%lld) is beyond mapping (%lld:%ld)\n"),
82 (long long)offset
, (long long)relative
,
83 (long long)mapping
->offset
, (long)mapping
->length
);
86 if (pagealign
&& (long)((char *)mapping
->addr
+ relative
) % pagesize
) {
87 printf(_("offset address (%p) is not page aligned\n"),
88 (char *)mapping
->addr
+ relative
);
92 return (char *)mapping
->addr
+ relative
;
100 for (i
= 0; i
< mapcount
; i
++)
101 print_mapping(&maptable
[i
], i
, &maptable
[i
] == mapping
);
114 if (i
< 0 || i
>= mapcount
) {
115 printf("value %d is out of range (0-%d)\n", i
, mapcount
);
117 mapping
= &maptable
[i
];
128 " maps a range within the current file into memory\n"
131 " 'mmap -rw 0 1m' - maps one megabyte from the start of the current file\n"
133 " Memory maps a range of a file for subsequent use by other xfs_io commands.\n"
134 " With no arguments, mmap shows the current mappings. The current mapping\n"
135 " can be set by using the single argument form (mapping number or address).\n"
136 " If two arguments are specified (a range), a new mapping is created and the\n"
137 " following options are available:\n"
138 " -r -- map with PROT_READ protection\n"
139 " -w -- map with PROT_WRITE protection\n"
140 " -x -- map with PROT_EXEC protection\n"
141 " -S -- map with MAP_SYNC and MAP_SHARED_VALIDATE flags\n"
142 " -s <size> -- first do mmap(size)/munmap(size), try to reserve some free space\n"
143 " If no protection mode is specified, all are used by default.\n"
153 ssize_t length
= 0, length2
= 0;
154 void *address
= NULL
;
156 size_t blocksize
, sectsize
;
157 int c
, prot
= 0, flags
= MAP_SHARED
;
162 fprintf(stderr
, file
?
163 _("no mapped regions, try 'help mmap'\n") :
164 _("no files are open, try 'help open'\n"));
166 } else if (argc
== 2) {
168 return mapset_f(argc
, argv
);
169 fprintf(stderr
, file
?
170 _("no mapped regions, try 'help mmap'\n") :
171 _("no files are open, try 'help open'\n"));
174 fprintf(stderr
, _("no files are open, try 'help open'\n"));
178 init_cvtnum(&blocksize
, §size
);
180 while ((c
= getopt(argc
, argv
, "rwxSs:")) != EOF
) {
192 flags
= MAP_SYNC
| MAP_SHARED_VALIDATE
;
195 * If MAP_SYNC and MAP_SHARED_VALIDATE aren't defined
196 * in the system headers we will have defined them
200 printf("MAP_SYNC not supported\n");
205 length2
= cvtnum(blocksize
, sectsize
, optarg
);
208 return command_usage(&mmap_cmd
);
212 prot
= PROT_READ
| PROT_WRITE
| PROT_EXEC
;
214 if (optind
!= argc
- 2)
215 return command_usage(&mmap_cmd
);
217 offset
= cvtnum(blocksize
, sectsize
, argv
[optind
]);
219 printf(_("non-numeric offset argument -- %s\n"), argv
[optind
]);
223 length
= cvtnum(blocksize
, sectsize
, argv
[optind
]);
225 printf(_("non-numeric length argument -- %s\n"), argv
[optind
]);
229 filename
= strdup(file
->name
);
236 * mmap and munmap memory area of length2 region is helpful to
237 * make a region of extendible free memory. It's generally used
238 * for later mremap operation(no MREMAP_MAYMOVE flag). But there
239 * isn't guarantee that the memory after length (up to length2)
242 if (length2
> length
) {
243 address
= mmap(NULL
, length2
, prot
,
244 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
245 munmap(address
, length2
);
247 address
= mmap(address
, length
, prot
, flags
, file
->fd
, offset
);
248 if (address
== MAP_FAILED
) {
254 /* Extend the control array of mmap'd regions */
255 maptable
= (mmap_region_t
*)realloc(maptable
, /* growing */
256 ++mapcount
* sizeof(mmap_region_t
));
260 munmap(address
, length
);
265 /* Finally, make this the new active mapping */
266 mapping
= &maptable
[mapcount
- 1];
267 mapping
->addr
= address
;
268 mapping
->length
= length
;
269 mapping
->offset
= offset
;
270 mapping
->name
= filename
;
271 mapping
->prot
= prot
;
272 mapping
->map_sync
= (flags
== (MAP_SYNC
| MAP_SHARED_VALIDATE
));
281 " flushes a range of bytes in the current memory mapping\n"
283 " Writes all modified copies of pages over the specified range (or entire\n"
284 " mapping if no range specified) to their backing storage locations. Also,\n"
285 " optionally invalidates so that subsequent references to the pages will be\n"
286 " obtained from their backing storage locations (instead of cached copies).\n"
287 " -a -- perform asynchronous writes (MS_ASYNC)\n"
288 " -i -- invalidate mapped pages (MS_INVALIDATE)\n"
289 " -s -- perform synchronous writes (MS_SYNC)\n"
302 size_t blocksize
, sectsize
;
304 while ((c
= getopt(argc
, argv
, "ais")) != EOF
) {
310 flags
|= MS_INVALIDATE
;
316 return command_usage(&msync_cmd
);
320 if (optind
== argc
) {
321 offset
= mapping
->offset
;
322 length
= mapping
->length
;
323 } else if (optind
== argc
- 2) {
324 init_cvtnum(&blocksize
, §size
);
325 offset
= cvtnum(blocksize
, sectsize
, argv
[optind
]);
327 printf(_("non-numeric offset argument -- %s\n"),
332 length
= cvtnum(blocksize
, sectsize
, argv
[optind
]);
334 printf(_("non-numeric length argument -- %s\n"),
339 return command_usage(&msync_cmd
);
342 start
= check_mapping_range(mapping
, offset
, length
, 1);
346 if (msync(start
, length
, flags
) < 0)
357 " reads a range of bytes in the current memory mapping\n"
360 " 'mread -v 512 20' - dumps 20 bytes read from 512 bytes into the mapping\n"
362 " Accesses a range of the current memory mapping, optionally dumping it to\n"
363 " the standard output stream (with -v option) for subsequent inspection.\n"
364 " -f -- verbose mode, dump bytes with offsets relative to start of file.\n"
365 " -r -- reverse order; start accessing from the end of range, moving backward\n"
366 " -v -- verbose mode, dump bytes with offsets relative to start of mapping.\n"
367 " The accesses are performed sequentially from the start offset by default.\n"
369 " References to whole pages following the end of the backing file results\n"
370 " in delivery of the SIGBUS signal. SIGBUS signals may also be delivered\n"
371 " on various filesystem conditions, including quota exceeded errors, and\n"
372 " for physical device errors (such as unreadable disk blocks). No attempt\n"
373 " has been made to catch signals at this stage...\n"
382 off64_t offset
, tmp
, dumpoffset
, printoffset
;
384 size_t dumplen
, cnt
= 0;
387 int dump
= 0, rflag
= 0, c
;
388 size_t blocksize
, sectsize
;
390 while ((c
= getopt(argc
, argv
, "frv")) != EOF
) {
393 dump
= 2; /* file offset dump */
396 rflag
= 1; /* read in reverse */
399 dump
= 1; /* mapping offset dump */
402 return command_usage(&mread_cmd
);
406 if (optind
== argc
) {
407 offset
= mapping
->offset
;
408 length
= mapping
->length
;
409 } else if (optind
== argc
- 2) {
410 init_cvtnum(&blocksize
, §size
);
411 offset
= cvtnum(blocksize
, sectsize
, argv
[optind
]);
413 printf(_("non-numeric offset argument -- %s\n"),
418 length
= cvtnum(blocksize
, sectsize
, argv
[optind
]);
420 printf(_("non-numeric length argument -- %s\n"),
425 return command_usage(&mread_cmd
);
428 start
= check_mapping_range(mapping
, offset
, length
, 0);
431 dumpoffset
= offset
- mapping
->offset
;
433 printoffset
= offset
;
435 printoffset
= dumpoffset
;
437 if (alloc_buffer(pagesize
, 0, 0) < 0)
441 dumplen
= length
% pagesize
;
446 for (tmp
= length
- 1, c
= 0; tmp
>= 0; tmp
--, c
= 1) {
447 *bp
= *(((char *)mapping
->addr
) + dumpoffset
+ tmp
);
449 if (c
&& cnt
== dumplen
) {
451 dump_buffer(printoffset
, dumplen
);
452 printoffset
+= dumplen
;
462 for (tmp
= 0, c
= 0; tmp
< length
; tmp
++, c
= 1) {
463 *bp
= *(((char *)mapping
->addr
) + dumpoffset
+ tmp
);
465 if (c
&& cnt
== dumplen
) {
467 dump_buffer(printoffset
+ tmp
-
468 (dumplen
- 1), dumplen
);
488 if (munmap(mapping
->addr
, mapping
->length
) < 0) {
494 /* Shuffle the mapping table entries down over the removed entry */
495 offset
= mapping
- &maptable
[0];
496 length
= mapcount
* sizeof(mmap_region_t
);
497 length
-= (offset
+ 1) * sizeof(mmap_region_t
);
499 memmove(mapping
, mapping
+ 1, length
);
501 /* Resize the memory allocated for the table, possibly freeing */
503 maptable
= (mmap_region_t
*)realloc(maptable
, /* shrinking */
504 mapcount
* sizeof(mmap_region_t
));
505 if (offset
== mapcount
)
507 mapping
= maptable
+ offset
;
510 mapping
= maptable
= NULL
;
521 " dirties a range of bytes in the current memory mapping\n"
524 " 'mwrite 512 20 - writes 20 bytes at 512 bytes into the current mapping.\n"
526 " Stores a byte into memory for a range within a mapping.\n"
527 " The default stored value is 'X', repeated to fill the range specified.\n"
528 " -S -- use an alternate seed character\n"
529 " -r -- reverse order; start storing from the end of range, moving backward\n"
530 " The stores are performed sequentially from the start offset by default.\n"
546 size_t blocksize
, sectsize
;
548 while ((c
= getopt(argc
, argv
, "rS:")) != EOF
) {
554 seed
= (int)strtol(optarg
, &sp
, 0);
555 if (!sp
|| sp
== optarg
) {
556 printf(_("non-numeric seed -- %s\n"), optarg
);
561 return command_usage(&mwrite_cmd
);
565 if (optind
== argc
) {
566 offset
= mapping
->offset
;
567 length
= mapping
->length
;
568 } else if (optind
== argc
- 2) {
569 init_cvtnum(&blocksize
, §size
);
570 offset
= cvtnum(blocksize
, sectsize
, argv
[optind
]);
572 printf(_("non-numeric offset argument -- %s\n"),
577 length
= cvtnum(blocksize
, sectsize
, argv
[optind
]);
579 printf(_("non-numeric length argument -- %s\n"),
584 return command_usage(&mwrite_cmd
);
587 start
= check_mapping_range(mapping
, offset
, length
, 0);
591 offset
-= mapping
->offset
;
593 for (tmp
= offset
+ length
-1; tmp
>= offset
; tmp
--)
594 ((char *)mapping
->addr
)[tmp
] = seed
;
596 for (tmp
= offset
; tmp
< offset
+ length
; tmp
++)
597 ((char *)mapping
->addr
)[tmp
] = seed
;
609 " resizes the current memory mapping\n"
612 " 'mremap 8192' - resizes the current mapping to 8192 bytes.\n"
614 " Resizes the mapping, growing or shrinking from the current size.\n"
615 " The default stored value is 'X', repeated to fill the range specified.\n"
616 " -f <new_address> -- use MREMAP_FIXED flag to mremap on new_address\n"
617 " -m -- use the MREMAP_MAYMOVE flag\n"
627 void *new_addr
= NULL
;
630 size_t blocksize
, sectsize
;
632 init_cvtnum(&blocksize
, §size
);
634 while ((c
= getopt(argc
, argv
, "f:m")) != EOF
) {
637 flags
= MREMAP_FIXED
|MREMAP_MAYMOVE
;
638 new_addr
= (void *)(unsigned long)cvtnum(blocksize
,
642 flags
= MREMAP_MAYMOVE
;
645 return command_usage(&mremap_cmd
);
649 if (optind
!= argc
- 1)
650 return command_usage(&mremap_cmd
);
652 new_length
= cvtnum(blocksize
, sectsize
, argv
[optind
]);
653 if (new_length
< 0) {
654 printf(_("non-numeric offset argument -- %s\n"),
660 new_addr
= mremap(mapping
->addr
, mapping
->length
,
663 new_addr
= mremap(mapping
->addr
, mapping
->length
,
664 new_length
, flags
, new_addr
);
665 if (new_addr
== MAP_FAILED
)
668 mapping
->addr
= new_addr
;
669 mapping
->length
= new_length
;
674 #endif /* HAVE_MREMAP */
679 mmap_cmd
.name
= "mmap";
680 mmap_cmd
.altname
= "mm";
681 mmap_cmd
.cfunc
= mmap_f
;
683 mmap_cmd
.argmax
= -1;
684 mmap_cmd
.flags
= CMD_NOMAP_OK
| CMD_NOFILE_OK
|
685 CMD_FOREIGN_OK
| CMD_FLAG_ONESHOT
;
686 mmap_cmd
.args
= _("[N] | [-rwxS] [-s size] [off len]");
688 _("mmap a range in the current file, show mappings");
689 mmap_cmd
.help
= mmap_help
;
691 mread_cmd
.name
= "mread";
692 mread_cmd
.altname
= "mr";
693 mread_cmd
.cfunc
= mread_f
;
694 mread_cmd
.argmin
= 0;
695 mread_cmd
.argmax
= -1;
696 mread_cmd
.flags
= CMD_NOFILE_OK
| CMD_FOREIGN_OK
;
697 mread_cmd
.args
= _("[-r] [off len]");
699 _("reads data from a region in the current memory mapping");
700 mread_cmd
.help
= mread_help
;
702 msync_cmd
.name
= "msync";
703 msync_cmd
.altname
= "ms";
704 msync_cmd
.cfunc
= msync_f
;
705 msync_cmd
.argmin
= 0;
706 msync_cmd
.argmax
= -1;
707 msync_cmd
.flags
= CMD_NOFILE_OK
| CMD_FOREIGN_OK
;
708 msync_cmd
.args
= _("[-ais] [off len]");
709 msync_cmd
.oneline
= _("flush a region in the current memory mapping");
710 msync_cmd
.help
= msync_help
;
712 munmap_cmd
.name
= "munmap";
713 munmap_cmd
.altname
= "mu";
714 munmap_cmd
.cfunc
= munmap_f
;
715 munmap_cmd
.argmin
= 0;
716 munmap_cmd
.argmax
= 0;
717 munmap_cmd
.flags
= CMD_NOFILE_OK
| CMD_FOREIGN_OK
;
718 munmap_cmd
.oneline
= _("unmaps the current memory mapping");
720 mwrite_cmd
.name
= "mwrite";
721 mwrite_cmd
.altname
= "mw";
722 mwrite_cmd
.cfunc
= mwrite_f
;
723 mwrite_cmd
.argmin
= 0;
724 mwrite_cmd
.argmax
= -1;
725 mwrite_cmd
.flags
= CMD_NOFILE_OK
| CMD_FOREIGN_OK
;
726 mwrite_cmd
.args
= _("[-r] [-S seed] [off len]");
728 _("writes data into a region in the current memory mapping");
729 mwrite_cmd
.help
= mwrite_help
;
732 mremap_cmd
.name
= "mremap";
733 mremap_cmd
.altname
= "mrm";
734 mremap_cmd
.cfunc
= mremap_f
;
735 mremap_cmd
.argmin
= 1;
736 mremap_cmd
.argmax
= 3;
737 mremap_cmd
.flags
= CMD_NOFILE_OK
| CMD_FOREIGN_OK
;
738 mremap_cmd
.args
= _("[-m|-f <new_address>] newsize");
740 _("alters the size of the current memory mapping");
741 mremap_cmd
.help
= mremap_help
;
742 #endif /* HAVE_MREMAP */
744 add_command(&mmap_cmd
);
745 add_command(&mread_cmd
);
746 add_command(&msync_cmd
);
747 add_command(&munmap_cmd
);
748 add_command(&mwrite_cmd
);
750 add_command(&mremap_cmd
);
751 #endif /* HAVE_MREMAP */