]>
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
;
19 static cmdinfo_t mremap_cmd
;
21 mmap_region_t
*maptable
;
23 mmap_region_t
*mapping
;
31 char buffer
[8] = { 0 };
44 for (i
= 0, p
= pflags
; p
->prot
!= PROT_NONE
; i
++, p
++)
45 buffer
[i
] = (map
->prot
& p
->prot
) ? p
->mode
: '-';
48 if ((map
->flags
& (MAP_SYNC
| MAP_SHARED_VALIDATE
)) ==
49 (MAP_SYNC
| MAP_SHARED_VALIDATE
))
50 sprintf(&buffer
[i
], " S");
53 printf("%c%03d%c 0x%lx - 0x%lx %s %14s (%lld : %ld)\n",
54 braces
? '[' : ' ', index
, braces
? ']' : ' ',
55 (unsigned long)map
->addr
,
56 (unsigned long)((char *)map
->addr
+ map
->length
),
57 buffer
, map
->name
? map
->name
: "???",
58 (long long)map
->offset
, (long)map
->length
);
70 if (offset
< mapping
->offset
) {
71 printf(_("offset (%lld) is before start of mapping (%lld)\n"),
72 (long long)offset
, (long long)mapping
->offset
);
75 relative
= offset
- mapping
->offset
;
76 if (relative
> mapping
->length
) {
77 printf(_("offset (%lld) is beyond end of mapping (%lld)\n"),
78 (long long)relative
, (long long)mapping
->offset
);
81 if ((relative
+ length
) > (mapping
->offset
+ mapping
->length
)) {
82 printf(_("range (%lld:%lld) is beyond mapping (%lld:%ld)\n"),
83 (long long)offset
, (long long)relative
,
84 (long long)mapping
->offset
, (long)mapping
->length
);
87 if (pagealign
&& (long)((char *)mapping
->addr
+ relative
) % pagesize
) {
88 printf(_("offset address (%p) is not page aligned\n"),
89 (char *)mapping
->addr
+ relative
);
93 return (char *)mapping
->addr
+ relative
;
101 for (i
= 0; i
< mapcount
; i
++)
102 print_mapping(&maptable
[i
], i
, &maptable
[i
] == mapping
);
115 if (i
< 0 || i
>= mapcount
) {
116 printf("value %d is out of range (0-%d)\n", i
, mapcount
);
119 mapping
= &maptable
[i
];
130 " maps a range within the current file into memory\n"
133 " 'mmap -rw 0 1m' - maps one megabyte from the start of the current file\n"
135 " Memory maps a range of a file for subsequent use by other xfs_io commands.\n"
136 " With no arguments, mmap shows the current mappings. The current mapping\n"
137 " can be set by using the single argument form (mapping number or address).\n"
138 " If two arguments are specified (a range), a new mapping is created and the\n"
139 " following options are available:\n"
140 " -r -- map with PROT_READ protection\n"
141 " -w -- map with PROT_WRITE protection\n"
142 " -x -- map with PROT_EXEC protection\n"
144 " -S -- map with MAP_SYNC and MAP_SHARED_VALIDATE flags\n"
146 " -s <size> -- first do mmap(size)/munmap(size), try to reserve some free space\n"
147 " If no protection mode is specified, all are used by default.\n"
158 ssize_t length
= 0, length2
= 0;
159 void *address
= NULL
;
161 size_t blocksize
, sectsize
;
162 int c
, prot
= 0, flags
= MAP_SHARED
;
167 fprintf(stderr
, file
?
168 _("no mapped regions, try 'help mmap'\n") :
169 _("no files are open, try 'help open'\n"));
172 } else if (argc
== 2) {
174 return mapset_f(argc
, argv
);
175 fprintf(stderr
, file
?
176 _("no mapped regions, try 'help mmap'\n") :
177 _("no files are open, try 'help open'\n"));
181 fprintf(stderr
, _("no files are open, try 'help open'\n"));
186 init_cvtnum(&blocksize
, §size
);
188 while ((c
= getopt(argc
, argv
, "rwxSs:")) != EOF
) {
201 flags
= MAP_SYNC
| MAP_SHARED_VALIDATE
;
204 printf("MAP_SYNC not supported\n");
206 return command_usage(&mmap_cmd
);
209 length2
= cvtnum(blocksize
, sectsize
, optarg
);
213 return command_usage(&mmap_cmd
);
217 prot
= PROT_READ
| PROT_WRITE
| PROT_EXEC
;
219 if (optind
!= argc
- 2) {
221 return command_usage(&mmap_cmd
);
224 offset
= cvtnum(blocksize
, sectsize
, argv
[optind
]);
226 printf(_("non-numeric offset argument -- %s\n"), argv
[optind
]);
231 length
= cvtnum(blocksize
, sectsize
, argv
[optind
]);
233 printf(_("non-numeric length argument -- %s\n"), argv
[optind
]);
238 filename
= strdup(file
->name
);
246 * mmap and munmap memory area of length2 region is helpful to
247 * make a region of extendible free memory. It's generally used
248 * for later mremap operation(no MREMAP_MAYMOVE flag). But there
249 * isn't guarantee that the memory after length (up to length2)
252 if (length2
> length
) {
253 address
= mmap(NULL
, length2
, prot
,
254 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
255 munmap(address
, length2
);
257 address
= mmap(address
, length
, prot
, flags
, file
->fd
, offset
);
258 if (address
== MAP_FAILED
) {
265 /* Extend the control array of mmap'd regions */
266 maptable
= (mmap_region_t
*)realloc(maptable
, /* growing */
267 ++mapcount
* sizeof(mmap_region_t
));
271 munmap(address
, length
);
277 /* Finally, make this the new active mapping */
278 mapping
= &maptable
[mapcount
- 1];
279 mapping
->addr
= address
;
280 mapping
->length
= length
;
281 mapping
->offset
= offset
;
282 mapping
->name
= filename
;
283 mapping
->prot
= prot
;
284 mapping
->flags
= flags
;
293 " flushes a range of bytes in the current memory mapping\n"
295 " Writes all modified copies of pages over the specified range (or entire\n"
296 " mapping if no range specified) to their backing storage locations. Also,\n"
297 " optionally invalidates so that subsequent references to the pages will be\n"
298 " obtained from their backing storage locations (instead of cached copies).\n"
299 " -a -- perform asynchronous writes (MS_ASYNC)\n"
300 " -i -- invalidate mapped pages (MS_INVALIDATE)\n"
301 " -s -- perform synchronous writes (MS_SYNC)\n"
314 size_t blocksize
, sectsize
;
316 while ((c
= getopt(argc
, argv
, "ais")) != EOF
) {
322 flags
|= MS_INVALIDATE
;
329 return command_usage(&msync_cmd
);
333 if (optind
== argc
) {
334 offset
= mapping
->offset
;
335 length
= mapping
->length
;
336 } else if (optind
== argc
- 2) {
337 init_cvtnum(&blocksize
, §size
);
338 offset
= cvtnum(blocksize
, sectsize
, argv
[optind
]);
340 printf(_("non-numeric offset argument -- %s\n"),
346 length
= cvtnum(blocksize
, sectsize
, argv
[optind
]);
348 printf(_("non-numeric length argument -- %s\n"),
355 return command_usage(&msync_cmd
);
358 start
= check_mapping_range(mapping
, offset
, length
, 1);
364 if (msync(start
, length
, flags
) < 0) {
378 " reads a range of bytes in the current memory mapping\n"
381 " 'mread -v 512 20' - dumps 20 bytes read from 512 bytes into the mapping\n"
383 " Accesses a range of the current memory mapping, optionally dumping it to\n"
384 " the standard output stream (with -v option) for subsequent inspection.\n"
385 " -f -- verbose mode, dump bytes with offsets relative to start of file.\n"
386 " -r -- reverse order; start accessing from the end of range, moving backward\n"
387 " -v -- verbose mode, dump bytes with offsets relative to start of mapping.\n"
388 " The accesses are performed sequentially from the start offset by default.\n"
390 " References to whole pages following the end of the backing file results\n"
391 " in delivery of the SIGBUS signal. SIGBUS signals may also be delivered\n"
392 " on various filesystem conditions, including quota exceeded errors, and\n"
393 " for physical device errors (such as unreadable disk blocks). No attempt\n"
394 " has been made to catch signals at this stage...\n"
403 off_t offset
, tmp
, dumpoffset
, printoffset
;
405 size_t dumplen
, cnt
= 0;
408 int dump
= 0, rflag
= 0, c
;
409 size_t blocksize
, sectsize
;
411 while ((c
= getopt(argc
, argv
, "frv")) != EOF
) {
414 dump
= 2; /* file offset dump */
417 rflag
= 1; /* read in reverse */
420 dump
= 1; /* mapping offset dump */
424 return command_usage(&mread_cmd
);
428 if (optind
== argc
) {
429 offset
= mapping
->offset
;
430 length
= mapping
->length
;
431 } else if (optind
== argc
- 2) {
432 init_cvtnum(&blocksize
, §size
);
433 offset
= cvtnum(blocksize
, sectsize
, argv
[optind
]);
435 printf(_("non-numeric offset argument -- %s\n"),
441 length
= cvtnum(blocksize
, sectsize
, argv
[optind
]);
443 printf(_("non-numeric length argument -- %s\n"),
449 return command_usage(&mread_cmd
);
452 start
= check_mapping_range(mapping
, offset
, length
, 0);
457 dumpoffset
= offset
- mapping
->offset
;
459 printoffset
= offset
;
461 printoffset
= dumpoffset
;
463 if (alloc_buffer(pagesize
, 0, 0) < 0) {
467 bp
= (char *)io_buffer
;
469 dumplen
= length
% pagesize
;
474 for (tmp
= length
- 1; tmp
>= 0; tmp
--) {
475 bp
[cnt
++] = ((char *)mapping
->addr
)[dumpoffset
+ tmp
];
476 if (cnt
== dumplen
) {
478 dump_buffer(printoffset
, dumplen
);
479 printoffset
+= dumplen
;
486 for (tmp
= 0; tmp
< length
; tmp
++) {
487 bp
[cnt
++] = ((char *)mapping
->addr
)[dumpoffset
+ tmp
];
488 if (cnt
== dumplen
) {
490 dump_buffer(printoffset
+ tmp
-
491 (dumplen
- 1), dumplen
);
508 if (munmap(mapping
->addr
, mapping
->length
) < 0) {
515 /* Shuffle the mapping table entries down over the removed entry */
516 offset
= mapping
- &maptable
[0];
517 length
= mapcount
* sizeof(mmap_region_t
);
518 length
-= (offset
+ 1) * sizeof(mmap_region_t
);
520 memmove(mapping
, mapping
+ 1, length
);
522 /* Resize the memory allocated for the table, possibly freeing */
524 maptable
= (mmap_region_t
*)realloc(maptable
, /* shrinking */
525 mapcount
* sizeof(mmap_region_t
));
526 if (offset
== mapcount
)
528 mapping
= maptable
+ offset
;
531 mapping
= maptable
= NULL
;
542 " dirties a range of bytes in the current memory mapping\n"
545 " 'mwrite 512 20 - writes 20 bytes at 512 bytes into the current mapping.\n"
547 " Stores a byte into memory for a range within a mapping.\n"
548 " The default stored value is 'X', repeated to fill the range specified.\n"
549 " -S -- use an alternate seed character\n"
550 " -r -- reverse order; start storing from the end of range, moving backward\n"
551 " The stores are performed sequentially from the start offset by default.\n"
567 size_t blocksize
, sectsize
;
569 while ((c
= getopt(argc
, argv
, "rS:")) != EOF
) {
575 seed
= (int)strtol(optarg
, &sp
, 0);
576 if (!sp
|| sp
== optarg
) {
577 printf(_("non-numeric seed -- %s\n"), optarg
);
583 return command_usage(&mwrite_cmd
);
587 if (optind
== argc
) {
588 offset
= mapping
->offset
;
589 length
= mapping
->length
;
590 } else if (optind
== argc
- 2) {
591 init_cvtnum(&blocksize
, §size
);
592 offset
= cvtnum(blocksize
, sectsize
, argv
[optind
]);
594 printf(_("non-numeric offset argument -- %s\n"),
600 length
= cvtnum(blocksize
, sectsize
, argv
[optind
]);
602 printf(_("non-numeric length argument -- %s\n"),
609 return command_usage(&mwrite_cmd
);
612 start
= check_mapping_range(mapping
, offset
, length
, 0);
618 offset
-= mapping
->offset
;
620 for (tmp
= offset
+ length
-1; tmp
>= offset
; tmp
--)
621 ((char *)mapping
->addr
)[tmp
] = seed
;
623 for (tmp
= offset
; tmp
< offset
+ length
; tmp
++)
624 ((char *)mapping
->addr
)[tmp
] = seed
;
635 " resizes the current memory mapping\n"
638 " 'mremap 8192' - resizes the current mapping to 8192 bytes.\n"
640 " Resizes the mapping, growing or shrinking from the current size.\n"
641 " The default stored value is 'X', repeated to fill the range specified.\n"
642 " -f <new_address> -- use MREMAP_FIXED flag to mremap on new_address\n"
643 " -m -- use the MREMAP_MAYMOVE flag\n"
653 void *new_addr
= NULL
;
656 size_t blocksize
, sectsize
;
658 init_cvtnum(&blocksize
, §size
);
660 while ((c
= getopt(argc
, argv
, "f:m")) != EOF
) {
663 flags
= MREMAP_FIXED
|MREMAP_MAYMOVE
;
664 new_addr
= (void *)(unsigned long)cvtnum(blocksize
,
668 flags
= MREMAP_MAYMOVE
;
672 return command_usage(&mremap_cmd
);
676 if (optind
!= argc
- 1) {
678 return command_usage(&mremap_cmd
);
681 new_length
= cvtnum(blocksize
, sectsize
, argv
[optind
]);
682 if (new_length
< 0) {
683 printf(_("non-numeric offset argument -- %s\n"),
690 new_addr
= mremap(mapping
->addr
, mapping
->length
,
693 new_addr
= mremap(mapping
->addr
, mapping
->length
,
694 new_length
, flags
, new_addr
);
695 if (new_addr
== MAP_FAILED
) {
701 mapping
->addr
= new_addr
;
702 mapping
->length
= new_length
;
709 mmap_cmd
.name
= "mmap";
710 mmap_cmd
.altname
= "mm";
711 mmap_cmd
.cfunc
= mmap_f
;
713 mmap_cmd
.argmax
= -1;
714 mmap_cmd
.flags
= CMD_NOMAP_OK
| CMD_NOFILE_OK
|
715 CMD_FOREIGN_OK
| CMD_FLAG_ONESHOT
;
716 mmap_cmd
.args
= _("[N] | [-rwxS] [-s size] [off len]");
718 _("mmap a range in the current file, show mappings");
719 mmap_cmd
.help
= mmap_help
;
721 mread_cmd
.name
= "mread";
722 mread_cmd
.altname
= "mr";
723 mread_cmd
.cfunc
= mread_f
;
724 mread_cmd
.argmin
= 0;
725 mread_cmd
.argmax
= -1;
726 mread_cmd
.flags
= CMD_NOFILE_OK
| CMD_FOREIGN_OK
;
727 mread_cmd
.args
= _("[-r] [off len]");
729 _("reads data from a region in the current memory mapping");
730 mread_cmd
.help
= mread_help
;
732 msync_cmd
.name
= "msync";
733 msync_cmd
.altname
= "ms";
734 msync_cmd
.cfunc
= msync_f
;
735 msync_cmd
.argmin
= 0;
736 msync_cmd
.argmax
= -1;
737 msync_cmd
.flags
= CMD_NOFILE_OK
| CMD_FOREIGN_OK
;
738 msync_cmd
.args
= _("[-ais] [off len]");
739 msync_cmd
.oneline
= _("flush a region in the current memory mapping");
740 msync_cmd
.help
= msync_help
;
742 munmap_cmd
.name
= "munmap";
743 munmap_cmd
.altname
= "mu";
744 munmap_cmd
.cfunc
= munmap_f
;
745 munmap_cmd
.argmin
= 0;
746 munmap_cmd
.argmax
= 0;
747 munmap_cmd
.flags
= CMD_NOFILE_OK
| CMD_FOREIGN_OK
;
748 munmap_cmd
.oneline
= _("unmaps the current memory mapping");
750 mwrite_cmd
.name
= "mwrite";
751 mwrite_cmd
.altname
= "mw";
752 mwrite_cmd
.cfunc
= mwrite_f
;
753 mwrite_cmd
.argmin
= 0;
754 mwrite_cmd
.argmax
= -1;
755 mwrite_cmd
.flags
= CMD_NOFILE_OK
| CMD_FOREIGN_OK
;
756 mwrite_cmd
.args
= _("[-r] [-S seed] [off len]");
758 _("writes data into a region in the current memory mapping");
759 mwrite_cmd
.help
= mwrite_help
;
761 mremap_cmd
.name
= "mremap";
762 mremap_cmd
.altname
= "mrm";
763 mremap_cmd
.cfunc
= mremap_f
;
764 mremap_cmd
.argmin
= 1;
765 mremap_cmd
.argmax
= 3;
766 mremap_cmd
.flags
= CMD_NOFILE_OK
| CMD_FOREIGN_OK
;
767 mremap_cmd
.args
= _("[-m|-f <new_address>] newsize");
769 _("alters the size of the current memory mapping");
770 mremap_cmd
.help
= mremap_help
;
772 add_command(&mmap_cmd
);
773 add_command(&mread_cmd
);
774 add_command(&msync_cmd
);
775 add_command(&munmap_cmd
);
776 add_command(&mwrite_cmd
);
777 add_command(&mremap_cmd
);