]>
git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - io/open.c
2 * Copyright (c) 2003 Silicon Graphics, Inc. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
28 * For further information regarding this notice, see:
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
33 #include <xfs/libxfs.h>
38 static cmdinfo_t open_cmd
;
39 static cmdinfo_t stat_cmd
;
40 static cmdinfo_t setfl_cmd
;
41 static cmdinfo_t statfs_cmd
;
42 static cmdinfo_t extsize_cmd
;
43 static int stat_f(int, char **);
48 xfs_fsop_geom_t
*geom
,
60 oflags
= (rflag
? O_RDONLY
: O_RDWR
);
72 fd
= open(path
, oflags
, 0644);
77 if (!platform_test_xfs_fd(fd
)) {
78 fprintf(stderr
, _("%s: specified file "
79 "[\"%s\"] is not on an XFS filesystem\n"),
85 if (xfsctl(path
, fd
, XFS_IOC_FSGEOMETRY
, geom
) < 0) {
86 perror("XFS_IOC_FSGEOMETRY");
91 if (!readonly
&& xflag
) { /* read/write and realtime */
94 if (xfsctl(path
, fd
, XFS_IOC_FSGETXATTR
, &attr
) < 0) {
95 perror("XFS_IOC_FSGETXATTR");
99 if (!(attr
.fsx_xflags
& XFS_XFLAG_REALTIME
)) {
100 attr
.fsx_xflags
|= XFS_XFLAG_REALTIME
;
101 if (xfsctl(path
, fd
, XFS_IOC_FSSETXATTR
, &attr
) < 0) {
102 perror("XFS_IOC_FSSETXATTR");
114 printf("%s %s\n", open_cmd
.name
, open_cmd
.oneline
);
123 " opens a new file in the requested mode, after closing the current file\n"
126 " 'open -d /tmp/data' - opens data file read-write for direct IO\n"
128 " Opens a file for subsequent use by all of the other xfs_io commands.\n"
129 " With no arguments, open uses the stat command to show the current file.\n"
130 " -a -- open with the O_APPEND flag (append-only mode)\n"
131 " -c -- open with O_CREAT (create the file if it doesn't exist)\n"
132 " -d -- open with O_DIRECT (non-buffered IO, note alignment constraints)\n"
133 " -r -- open with O_RDONLY, the default is O_RDWR\n"
134 " -s -- open with O_SYNC\n"
135 " -t -- open with O_TRUNC (truncate the file to zero length if it exists)\n"
136 " -x -- mark the file as a realtime XFS file immediately after opening it\n"
137 " Note1: read/write direct IO requests must be blocksize aligned.\n"
138 " Note2: the bmap for non-regular files can be obtained provided the file\n"
139 " was opened appropriately (in particular, must be opened read-only).\n"
156 xfs_fsop_geom_t geometry
;
161 return stat_f(argc
, argv
);
163 while ((c
= getopt(argc
, argv
, "acdrstx")) != EOF
) {
191 if (optind
!= argc
- 1)
194 fd
= openfile(argv
[optind
], &geometry
,
195 aflag
, cflag
, dflag
, rflag
, sflag
, tflag
, xflag
);
199 filename
= strdup(argv
[optind
]);
207 * All OK, proceed to make this the new global open file
230 if (fstat64(fdesc
, &st
) < 0) {
238 filetype(mode_t mode
)
240 switch (mode
& S_IFMT
) {
244 return _("directory");
246 return _("char device");
248 return _("block device");
250 return _("regular file");
252 return _("symbolic link");
266 char fullname
[PATH_MAX
+ 1];
268 printf(_("fd.path = \"%s\"\n"),
269 realpath(fname
, fullname
) ? fullname
: fname
);
270 printf(_("fd.flags = %s,%s,%s%s%s\n"),
271 osync
? _("sync") : _("non-sync"),
272 directio
? _("direct") : _("non-direct"),
273 readonly
? _("read-only") : _("read-write"),
274 realtime
? _(",real-time") : "",
275 append
? _(",append-only") : "");
276 if (fstat64(fdesc
, &st
) < 0) {
279 printf(_("stat.ino = %lld\n"), (long long)st
.st_ino
);
280 printf(_("stat.type = %s\n"), filetype(st
.st_mode
));
281 printf(_("stat.size = %lld\n"), (long long)st
.st_size
);
282 printf(_("stat.blocks = %lld\n"), (long long)st
.st_blocks
);
283 if (argc
== 2 && !strcmp(argv
[1], "-v")) {
284 printf(_("stat.atime = %s"), ctime(&st
.st_atime
));
285 printf(_("stat.mtime = %s"), ctime(&st
.st_mtime
));
286 printf(_("stat.ctime = %s"), ctime(&st
.st_ctime
));
289 if ((xfsctl(fname
, fdesc
, XFS_IOC_FSGETXATTR
, &fsx
)) < 0) {
290 perror("xfsctl(XFS_IOC_FSGETXATTR)");
292 printf(_("xattr.xflags = 0x%x\n"), fsx
.fsx_xflags
);
293 printf(_("xattr.extsize = %u\n"), fsx
.fsx_extsize
);
294 printf(_("xattr.nextents = %u\n"), fsx
.fsx_nextents
);
306 flags
= fcntl(fdesc
, F_GETFL
, 0);
308 perror("fcntl(F_GETFL)");
312 while ((c
= getopt(argc
, argv
, "ad")) != EOF
) {
315 if (flags
& O_APPEND
)
321 if (flags
& O_DIRECT
)
327 printf(_("invalid setfl argument -- '%c'\n"), c
);
332 if (fcntl(fdesc
, F_SETFL
, flags
) < 0)
333 perror("fcntl(F_SETFL)");
346 extsize
= (long)cvtnum(fgeom
.blocksize
, fgeom
.sectsize
, argv
[1]);
348 printf(_("non-numeric extsize argument -- %s\n"), argv
[1]);
351 if ((xfsctl(fname
, fdesc
, XFS_IOC_FSGETXATTR
, &fsx
)) < 0) {
352 perror("xfsctl(XFS_IOC_FSGETXATTR)");
355 fsx
.fsx_extsize
= extsize
;
356 if ((xfsctl(fname
, fdesc
, XFS_IOC_FSSETXATTR
, &fsx
)) < 0) {
357 perror("xfsctl(XFS_IOC_FSSETXATTR)");
369 struct xfs_fsop_geom_v1 fsgeo
;
371 char fullname
[PATH_MAX
+ 1];
373 printf(_("fd.path = \"%s\"\n"),
374 realpath(fname
, fullname
) ? fullname
: fname
);
375 if (platform_fstatfs(fdesc
, &st
) < 0) {
378 printf(_("statfs.f_bsize = %lld\n"), (long long) st
.f_bsize
);
379 printf(_("statfs.f_blocks = %lld\n"), (long long) st
.f_blocks
);
380 #if !defined(__sgi__)
381 printf(_("statfs.f_bavail = %lld\n"), (long long) st
.f_bavail
);
384 if ((xfsctl(fname
, fdesc
, XFS_IOC_FSGEOMETRY_V1
, &fsgeo
)) < 0) {
385 perror("xfsctl(XFS_IOC_FSGEOMETRY_V1)");
387 printf(_("geom.bsize = %u\n"), fsgeo
.blocksize
);
388 printf(_("geom.agcount = %u\n"), fsgeo
.agcount
);
389 printf(_("geom.agblocks = %u\n"), fsgeo
.agblocks
);
390 printf(_("geom.datablocks = %llu\n"),
391 (unsigned long long) fsgeo
.datablocks
);
392 printf(_("geom.rtblocks = %llu\n"),
393 (unsigned long long) fsgeo
.rtblocks
);
394 printf(_("geom.rtextents = %llu\n"),
395 (unsigned long long) fsgeo
.rtextents
);
396 printf(_("geom.rtextsize = %u\n"), fsgeo
.rtextsize
);
397 printf(_("geom.sunit = %u\n"), fsgeo
.sunit
);
398 printf(_("geom.swidth = %u\n"), fsgeo
.swidth
);
406 open_cmd
.name
= _("open");
407 open_cmd
.altname
= _("o");
408 open_cmd
.cfunc
= open_f
;
410 open_cmd
.argmax
= -1;
411 open_cmd
.args
= _("[-acdrstx] [path]");
413 _("close the current file, open file specified by path");
414 open_cmd
.help
= open_help
;
416 stat_cmd
.name
= _("stat");
417 stat_cmd
.cfunc
= stat_f
;
420 stat_cmd
.args
= _("[-v]");
422 _("statistics on the currently open file");
424 setfl_cmd
.name
= _("setfl");
425 setfl_cmd
.cfunc
= setfl_f
;
426 setfl_cmd
.args
= _("[-adx]");
428 _("set/clear append/direct flags on the open file");
430 statfs_cmd
.name
= _("statfs");
431 statfs_cmd
.cfunc
= statfs_f
;
433 _("statistics on the filesystem of the currently open file");
435 extsize_cmd
.name
= _("extsize");
436 extsize_cmd
.cfunc
= extsize_f
;
437 extsize_cmd
.argmin
= 1;
438 extsize_cmd
.argmax
= 1;
439 extsize_cmd
.oneline
=
440 _("set prefered extent size (in bytes) for the open file");
442 add_command(&open_cmd
);
443 add_command(&stat_cmd
);
444 add_command(&setfl_cmd
);
445 add_command(&statfs_cmd
);
446 add_command(&extsize_cmd
);