1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2001 Silicon Graphics, Inc.
16 /*---------------------------------------------------------------------------
18 Test program used to test the DMAPI function dm_probe_hole(). The
21 probe_hole [-o offset] [-l length] [-s sid] pathname
23 where pathname is the name of a file, offset is the offset of the start of
24 the proposed punch, and length is the length of the punch. sid is the
25 session ID whose events you you are interested in.
27 ----------------------------------------------------------------------------*/
30 extern char *sys_errlist[];
38 #define METHOD_DMAPI_PROBE 0
39 #define METHOD_DMAPI_PUNCH 1
40 #define METHOD_XFSCTL 2
42 char *methodmap[] = {"DMAPI probe hole",
44 "Punch hole with xfsctl(XFS_IOC_FREESP64)"};
49 fprintf(stderr, "usage:\t%s [-x|-p] [-o offset] [-l length] "
50 "[-s sid] pathname\n", Progname);
63 if ((fd = open(path, O_RDWR|O_CREAT, 0600)) < 0) {
69 flock.l_start = offset;
72 if (xfsctl(path, fd, XFS_IOC_FREESP64, &flock)) {
73 fprintf(stderr, "can't XFS_IOC_FREESP64 %s: %s\n",
74 path, strerror(errno));
89 dm_sessid_t sid = DM_NO_SESSION;
90 char *pathname = NULL;
100 int method = METHOD_DMAPI_PROBE;
102 Progname = strrchr(argv[0], '/');
109 /* Crack and validate the command line options. */
111 while ((opt = getopt(argc, argv, "o:l:s:xp")) != EOF) {
114 offset = strtoll(optarg, &p, 10);
115 if (p && (*p == 'k' || *p == 'K'))
119 length = strtoll(optarg, &p, 10);
120 if (p && (*p == 'k' || *p == 'K'))
127 method = METHOD_XFSCTL;
130 method = METHOD_DMAPI_PUNCH;
136 if (optind + 1 != argc)
138 pathname = argv[optind];
143 printf("Running %s on %s with settings:\n", methodmap[method], pathname);
144 printf(" offset = '%lld', length = '%lld', sid = '%lld'\n",
145 (long long) offset, (unsigned long long) length, (long long) sid);
147 if (method == METHOD_XFSCTL)
148 return xfsctl_punch_hole(pathname, offset, length);
150 if (dm_init_service(&name) == -1) {
151 fprintf(stderr, "Can't initialize the DMAPI\n");
154 if (sid == DM_NO_SESSION)
155 find_test_session(&sid);
157 /* Get the file's handle. */
159 if (dm_path_to_handle(pathname, &hanp, &hlen)) {
160 fprintf(stderr, "can't get handle for file %s\n", pathname);
165 case METHOD_DMAPI_PROBE:
166 if (dm_probe_hole(sid, hanp, hlen, DM_NO_TOKEN, offset, length,
168 fprintf(stderr, "dm_probe_hole failed, %s\n",
172 fprintf(stdout, "roffp is %lld, rlenp is %llu\n",
173 (long long) roffp, (unsigned long long) rlenp);
175 case METHOD_DMAPI_PUNCH:
176 if (dm_punch_hole(sid, hanp, hlen, DM_NO_TOKEN, offset, length)) {
177 fprintf(stderr, "dm_punch_hole failed, %s\n",
183 dm_handle_free(hanp, hlen);