]>
git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - io/mmap.c
2 * Copyright (c) 2004-2005 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 static cmdinfo_t mmap_cmd
;
27 static cmdinfo_t mread_cmd
;
28 static cmdinfo_t msync_cmd
;
29 static cmdinfo_t munmap_cmd
;
30 static cmdinfo_t mwrite_cmd
;
31 static cmdinfo_t mremap_cmd
;
33 mmap_region_t
*maptable
;
35 mmap_region_t
*mapping
;
43 unsigned char buffer
[8] = { 0 };
56 for (i
= 0, p
= pflags
; p
->prot
!= PROT_NONE
; i
++, p
++)
57 buffer
[i
] = (map
->prot
& p
->prot
) ? p
->mode
: '-';
58 printf("%c%03d%c 0x%lx - 0x%lx %s %14s (%lld : %ld)\n",
59 braces
? '[' : ' ', index
, braces
? ']' : ' ',
60 (unsigned long)map
->addr
,
61 (unsigned long)((char *)map
->addr
+ map
->length
),
62 buffer
, map
->name
? map
->name
: "???",
63 (long long)map
->offset
, (long)map
->length
);
75 if (offset
< mapping
->offset
) {
76 printf(_("offset (%lld) is before start of mapping (%lld)\n"),
77 (long long)offset
, (long long)mapping
->offset
);
80 relative
= offset
- mapping
->offset
;
81 if (relative
> mapping
->length
) {
82 printf(_("offset (%lld) is beyond end of mapping (%lld)\n"),
83 (long long)relative
, (long long)mapping
->offset
);
86 if ((relative
+ length
) > (mapping
->offset
+ mapping
->length
)) {
87 printf(_("range (%lld:%lld) is beyond mapping (%lld:%ld)\n"),
88 (long long)offset
, (long long)relative
,
89 (long long)mapping
->offset
, (long)mapping
->length
);
92 if (pagealign
&& (long)((char *)mapping
->addr
+ relative
) % pagesize
) {
93 printf(_("offset address (%p) is not page aligned\n"),
94 (char *)mapping
->addr
+ relative
);
98 return (char *)mapping
->addr
+ relative
;
106 for (i
= 0; i
< mapcount
; i
++)
107 print_mapping(&maptable
[i
], i
, &maptable
[i
] == mapping
);
120 if (i
< 0 || i
>= mapcount
) {
121 printf("value %d is out of range (0-%d)\n", i
, mapcount
);
123 mapping
= &maptable
[i
];
134 " maps a range within the current file into memory\n"
137 " 'mmap -rw 0 1m' - maps one megabyte from the start of the current file\n"
139 " Memory maps a range of a file for subsequent use by other xfs_io commands.\n"
140 " With no arguments, mmap shows the current mappings. The current mapping\n"
141 " can be set by using the single argument form (mapping number or address).\n"
142 " If two arguments are specified (a range), a new mapping is created and the\n"
143 " following options are available:\n"
144 " -r -- map with PROT_READ protection\n"
145 " -w -- map with PROT_WRITE protection\n"
146 " -x -- map with PROT_EXEC protection\n"
147 " If no protection mode is specified, all are used by default.\n"
160 size_t blocksize
, sectsize
;
166 fprintf(stderr
, file
?
167 _("no mapped regions, try 'help mmap'\n") :
168 _("no files are open, try 'help open'\n"));
170 } else if (argc
== 2) {
172 return mapset_f(argc
, argv
);
173 fprintf(stderr
, file
?
174 _("no mapped regions, try 'help mmap'\n") :
175 _("no files are open, try 'help open'\n"));
178 fprintf(stderr
, _("no files are open, try 'help open'\n"));
182 while ((c
= getopt(argc
, argv
, "rwx")) != EOF
) {
194 return command_usage(&mmap_cmd
);
198 prot
= PROT_READ
| PROT_WRITE
| PROT_EXEC
;
200 if (optind
!= argc
- 2)
201 return command_usage(&mmap_cmd
);
203 init_cvtnum(&blocksize
, §size
);
204 offset
= cvtnum(blocksize
, sectsize
, argv
[optind
]);
206 printf(_("non-numeric offset argument -- %s\n"), argv
[optind
]);
210 length
= cvtnum(blocksize
, sectsize
, argv
[optind
]);
212 printf(_("non-numeric length argument -- %s\n"), argv
[optind
]);
216 filename
= strdup(file
->name
);
222 address
= mmap(NULL
, length
, prot
, MAP_SHARED
, file
->fd
, offset
);
223 if (address
== MAP_FAILED
) {
229 /* Extend the control array of mmap'd regions */
230 maptable
= (mmap_region_t
*)realloc(maptable
, /* growing */
231 ++mapcount
* sizeof(mmap_region_t
));
235 munmap(address
, length
);
240 /* Finally, make this the new active mapping */
241 mapping
= &maptable
[mapcount
- 1];
242 mapping
->addr
= address
;
243 mapping
->length
= length
;
244 mapping
->offset
= offset
;
245 mapping
->name
= filename
;
246 mapping
->prot
= prot
;
255 " flushes a range of bytes in the current memory mapping\n"
257 " Writes all modified copies of pages over the specified range (or entire\n"
258 " mapping if no range specified) to their backing storage locations. Also,\n"
259 " optionally invalidates so that subsequent references to the pages will be\n"
260 " obtained from their backing storage locations (instead of cached copies).\n"
261 " -a -- perform asynchronous writes (MS_ASYNC)\n"
262 " -i -- invalidate mapped pages (MS_INVALIDATE)\n"
263 " -s -- perform synchronous writes (MS_SYNC)\n"
276 size_t blocksize
, sectsize
;
278 while ((c
= getopt(argc
, argv
, "ais")) != EOF
) {
284 flags
|= MS_INVALIDATE
;
290 return command_usage(&msync_cmd
);
294 if (optind
== argc
) {
295 offset
= mapping
->offset
;
296 length
= mapping
->length
;
297 } else if (optind
== argc
- 2) {
298 init_cvtnum(&blocksize
, §size
);
299 offset
= cvtnum(blocksize
, sectsize
, argv
[optind
]);
301 printf(_("non-numeric offset argument -- %s\n"),
306 length
= cvtnum(blocksize
, sectsize
, argv
[optind
]);
308 printf(_("non-numeric length argument -- %s\n"),
313 return command_usage(&msync_cmd
);
316 start
= check_mapping_range(mapping
, offset
, length
, 1);
320 if (msync(start
, length
, flags
) < 0)
331 " reads a range of bytes in the current memory mapping\n"
334 " 'mread -v 512 20' - dumps 20 bytes read from 512 bytes into the mapping\n"
336 " Accesses a range of the current memory mapping, optionally dumping it to\n"
337 " the standard output stream (with -v option) for subsequent inspection.\n"
338 " -f -- verbose mode, dump bytes with offsets relative to start of file.\n"
339 " -r -- reverse order; start accessing from the end of range, moving backward\n"
340 " -v -- verbose mode, dump bytes with offsets relative to start of mapping.\n"
341 " The accesses are performed sequentially from the start offset by default.\n"
343 " References to whole pages following the end of the backing file results\n"
344 " in delivery of the SIGBUS signal. SIGBUS signals may also be delivered\n"
345 " on various filesystem conditions, including quota exceeded errors, and\n"
346 " for physical device errors (such as unreadable disk blocks). No attempt\n"
347 " has been made to catch signals at this stage...\n"
356 off64_t offset
, tmp
, dumpoffset
, printoffset
;
358 size_t dumplen
, cnt
= 0;
361 int dump
= 0, rflag
= 0, c
;
362 size_t blocksize
, sectsize
;
364 while ((c
= getopt(argc
, argv
, "frv")) != EOF
) {
367 dump
= 2; /* file offset dump */
370 rflag
= 1; /* read in reverse */
373 dump
= 1; /* mapping offset dump */
376 return command_usage(&mread_cmd
);
380 if (optind
== argc
) {
381 offset
= mapping
->offset
;
382 length
= mapping
->length
;
383 } else if (optind
== argc
- 2) {
384 init_cvtnum(&blocksize
, §size
);
385 offset
= cvtnum(blocksize
, sectsize
, argv
[optind
]);
387 printf(_("non-numeric offset argument -- %s\n"),
392 length
= cvtnum(blocksize
, sectsize
, argv
[optind
]);
394 printf(_("non-numeric length argument -- %s\n"),
399 return command_usage(&mread_cmd
);
402 start
= check_mapping_range(mapping
, offset
, length
, 0);
405 dumpoffset
= offset
- mapping
->offset
;
407 printoffset
= offset
;
409 printoffset
= dumpoffset
;
411 if (alloc_buffer(pagesize
, 0, 0) < 0)
415 dumplen
= length
% pagesize
;
420 for (tmp
= length
- 1, c
= 0; tmp
>= 0; tmp
--, c
= 1) {
421 *bp
= *(((char *)mapping
->addr
) + dumpoffset
+ tmp
);
423 if (c
&& cnt
== dumplen
) {
425 dump_buffer(printoffset
, dumplen
);
426 printoffset
+= dumplen
;
436 for (tmp
= 0, c
= 0; tmp
< length
; tmp
++, c
= 1) {
437 *bp
= *(((char *)mapping
->addr
) + dumpoffset
+ tmp
);
439 if (c
&& cnt
== dumplen
) {
441 dump_buffer(printoffset
+ tmp
-
442 (dumplen
- 1), dumplen
);
462 if (munmap(mapping
->addr
, mapping
->length
) < 0) {
468 /* Shuffle the mapping table entries down over the removed entry */
469 offset
= mapping
- &maptable
[0];
470 length
= mapcount
* sizeof(mmap_region_t
);
471 length
-= (offset
+ 1) * sizeof(mmap_region_t
);
473 memmove(mapping
, mapping
+ 1, length
);
475 /* Resize the memory allocated for the table, possibly freeing */
477 maptable
= (mmap_region_t
*)realloc(maptable
, /* shrinking */
478 mapcount
* sizeof(mmap_region_t
));
479 if (offset
== mapcount
)
481 mapping
= maptable
+ offset
;
484 mapping
= maptable
= NULL
;
495 " dirties a range of bytes in the current memory mapping\n"
498 " 'mwrite 512 20 - writes 20 bytes at 512 bytes into the current mapping.\n"
500 " Stores a byte into memory for a range within a mapping.\n"
501 " The default stored value is 'X', repeated to fill the range specified.\n"
502 " -S -- use an alternate seed character\n"
503 " -r -- reverse order; start storing from the end of range, moving backward\n"
504 " The stores are performed sequentially from the start offset by default.\n"
520 size_t blocksize
, sectsize
;
522 while ((c
= getopt(argc
, argv
, "rS:")) != EOF
) {
528 seed
= (int)strtol(optarg
, &sp
, 0);
529 if (!sp
|| sp
== optarg
) {
530 printf(_("non-numeric seed -- %s\n"), optarg
);
535 return command_usage(&mwrite_cmd
);
539 if (optind
== argc
) {
540 offset
= mapping
->offset
;
541 length
= mapping
->length
;
542 } else if (optind
== argc
- 2) {
543 init_cvtnum(&blocksize
, §size
);
544 offset
= cvtnum(blocksize
, sectsize
, argv
[optind
]);
546 printf(_("non-numeric offset argument -- %s\n"),
551 length
= cvtnum(blocksize
, sectsize
, argv
[optind
]);
553 printf(_("non-numeric length argument -- %s\n"),
558 return command_usage(&mwrite_cmd
);
561 start
= check_mapping_range(mapping
, offset
, length
, 0);
565 offset
-= mapping
->offset
;
567 for (tmp
= offset
+ length
-1; tmp
>= offset
; tmp
--)
568 ((char *)mapping
->addr
)[tmp
] = seed
;
570 for (tmp
= offset
; tmp
< offset
+ length
; tmp
++)
571 ((char *)mapping
->addr
)[tmp
] = seed
;
582 " resizes the current memory mapping\n"
585 " 'mremap 8192' - resizes the current mapping to 8192 bytes.\n"
587 " Resizes the mappping, growing or shrinking from the current size.\n"
588 " The default stored value is 'X', repeated to fill the range specified.\n"
589 " -f -- use the MREMAP_FIXED flag\n"
590 " -m -- use the MREMAP_MAYMOVE flag\n"
603 size_t blocksize
, sectsize
;
605 while ((c
= getopt(argc
, argv
, "fm")) != EOF
) {
608 flags
= MREMAP_FIXED
|MREMAP_MAYMOVE
;
611 flags
= MREMAP_MAYMOVE
;
614 return command_usage(&mremap_cmd
);
618 init_cvtnum(&blocksize
, §size
);
619 new_length
= cvtnum(blocksize
, sectsize
, argv
[optind
]);
620 if (new_length
< 0) {
621 printf(_("non-numeric offset argument -- %s\n"),
626 new_addr
= mremap(mapping
->addr
, mapping
->length
, new_length
, flags
);
627 if (new_addr
== MAP_FAILED
)
630 mapping
->addr
= new_addr
;
631 mapping
->length
= new_length
;
640 mmap_cmd
.name
= "mmap";
641 mmap_cmd
.altname
= "mm";
642 mmap_cmd
.cfunc
= mmap_f
;
644 mmap_cmd
.argmax
= -1;
645 mmap_cmd
.flags
= CMD_NOMAP_OK
| CMD_NOFILE_OK
| CMD_FOREIGN_OK
;
646 mmap_cmd
.args
= _("[N] | [-rwx] [off len]");
648 _("mmap a range in the current file, show mappings");
649 mmap_cmd
.help
= mmap_help
;
651 mread_cmd
.name
= "mread";
652 mread_cmd
.altname
= "mr";
653 mread_cmd
.cfunc
= mread_f
;
654 mread_cmd
.argmin
= 0;
655 mread_cmd
.argmax
= -1;
656 mread_cmd
.flags
= CMD_NOFILE_OK
| CMD_FOREIGN_OK
;
657 mread_cmd
.args
= _("[-r] [off len]");
659 _("reads data from a region in the current memory mapping");
660 mread_cmd
.help
= mread_help
;
662 msync_cmd
.name
= "msync";
663 msync_cmd
.altname
= "ms";
664 msync_cmd
.cfunc
= msync_f
;
665 msync_cmd
.argmin
= 0;
666 msync_cmd
.argmax
= -1;
667 msync_cmd
.flags
= CMD_NOFILE_OK
| CMD_FOREIGN_OK
;
668 msync_cmd
.args
= _("[-ais] [off len]");
669 msync_cmd
.oneline
= _("flush a region in the current memory mapping");
670 msync_cmd
.help
= msync_help
;
672 munmap_cmd
.name
= "munmap";
673 munmap_cmd
.altname
= "mu";
674 munmap_cmd
.cfunc
= munmap_f
;
675 munmap_cmd
.argmin
= 0;
676 munmap_cmd
.argmax
= 0;
677 munmap_cmd
.flags
= CMD_NOFILE_OK
| CMD_FOREIGN_OK
;
678 munmap_cmd
.oneline
= _("unmaps the current memory mapping");
680 mwrite_cmd
.name
= "mwrite";
681 mwrite_cmd
.altname
= "mw";
682 mwrite_cmd
.cfunc
= mwrite_f
;
683 mwrite_cmd
.argmin
= 0;
684 mwrite_cmd
.argmax
= -1;
685 mwrite_cmd
.flags
= CMD_NOFILE_OK
| CMD_FOREIGN_OK
;
686 mwrite_cmd
.args
= _("[-r] [-S seed] [off len]");
688 _("writes data into a region in the current memory mapping");
689 mwrite_cmd
.help
= mwrite_help
;
691 mremap_cmd
.name
= "mremap";
692 mremap_cmd
.altname
= "mrm";
693 mremap_cmd
.cfunc
= mremap_f
;
694 mremap_cmd
.argmin
= 1;
695 mremap_cmd
.argmax
= 2;
696 mremap_cmd
.flags
= CMD_NOFILE_OK
| CMD_FOREIGN_OK
;
697 mremap_cmd
.args
= _("[-m|-f] newsize");
699 _("alters the size of the current memory mapping");
700 mremap_cmd
.help
= mremap_help
;
702 add_command(&mmap_cmd
);
703 add_command(&mread_cmd
);
704 add_command(&msync_cmd
);
705 add_command(&munmap_cmd
);
706 add_command(&mwrite_cmd
);
707 add_command(&mremap_cmd
);