17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 556f10d37Spetede * Common Development and Distribution License (the "License"). 656f10d37Spetede * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate /* 22*e7cbe64fSgw25295 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 277c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate /* 307c478bd9Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988 317c478bd9Sstevel@tonic-gate * The Regents of the University of California 327c478bd9Sstevel@tonic-gate * All Rights Reserved 337c478bd9Sstevel@tonic-gate * 347c478bd9Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from 357c478bd9Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its 367c478bd9Sstevel@tonic-gate * contributors. 377c478bd9Sstevel@tonic-gate */ 387c478bd9Sstevel@tonic-gate 397c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 407c478bd9Sstevel@tonic-gate 417c478bd9Sstevel@tonic-gate /* 427c478bd9Sstevel@tonic-gate * Swap administrative interface 437c478bd9Sstevel@tonic-gate * Used to add/delete/list swap devices. 447c478bd9Sstevel@tonic-gate */ 457c478bd9Sstevel@tonic-gate 467c478bd9Sstevel@tonic-gate #include <sys/types.h> 477c478bd9Sstevel@tonic-gate #include <sys/dumpadm.h> 487c478bd9Sstevel@tonic-gate #include <string.h> 497c478bd9Sstevel@tonic-gate #include <stdio.h> 507c478bd9Sstevel@tonic-gate #include <stdlib.h> 517c478bd9Sstevel@tonic-gate #include <unistd.h> 527c478bd9Sstevel@tonic-gate #include <errno.h> 537c478bd9Sstevel@tonic-gate #include <sys/param.h> 547c478bd9Sstevel@tonic-gate #include <dirent.h> 557c478bd9Sstevel@tonic-gate #include <sys/swap.h> 567c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h> 577c478bd9Sstevel@tonic-gate #include <sys/mkdev.h> 587c478bd9Sstevel@tonic-gate #include <sys/stat.h> 597c478bd9Sstevel@tonic-gate #include <sys/statvfs.h> 607c478bd9Sstevel@tonic-gate #include <sys/uadmin.h> 617c478bd9Sstevel@tonic-gate #include <vm/anon.h> 627c478bd9Sstevel@tonic-gate #include <fcntl.h> 637c478bd9Sstevel@tonic-gate #include <locale.h> 647c478bd9Sstevel@tonic-gate #include <libintl.h> 653e1bd7a2Ssjelinek #include <libdiskmgt.h> 66*e7cbe64fSgw25295 #include <sys/fs/zfs.h> 677c478bd9Sstevel@tonic-gate 687c478bd9Sstevel@tonic-gate #define LFLAG 0x01 /* swap -l (list swap devices) */ 697c478bd9Sstevel@tonic-gate #define DFLAG 0x02 /* swap -d (delete swap device) */ 707c478bd9Sstevel@tonic-gate #define AFLAG 0x04 /* swap -a (add swap device) */ 717c478bd9Sstevel@tonic-gate #define SFLAG 0x08 /* swap -s (swap info summary) */ 727c478bd9Sstevel@tonic-gate #define P1FLAG 0x10 /* swap -1 (swapadd pass1; do not modify dump device) */ 737c478bd9Sstevel@tonic-gate #define P2FLAG 0x20 /* swap -2 (swapadd pass2; do not modify dump device) */ 7456f10d37Spetede #define HFLAG 0x40 /* swap -h (size in human readable format) */ 7556f10d37Spetede #define KFLAG 0x80 /* swap -k (size in kilobytes) */ 7656f10d37Spetede 7756f10d37Spetede #define NUMBER_WIDTH 64 7856f10d37Spetede typedef char numbuf_t[NUMBER_WIDTH]; 797c478bd9Sstevel@tonic-gate 807c478bd9Sstevel@tonic-gate static char *prognamep; 817c478bd9Sstevel@tonic-gate 827c478bd9Sstevel@tonic-gate static int add(char *, off_t, off_t, int); 837c478bd9Sstevel@tonic-gate static int delete(char *, off_t); 847c478bd9Sstevel@tonic-gate static void usage(void); 8556f10d37Spetede static int doswap(int flag); 867c478bd9Sstevel@tonic-gate static int valid(char *, off_t, off_t); 8756f10d37Spetede static int list(int flag); 8856f10d37Spetede static char *number_to_scaled_string(numbuf_t buf, unsigned long long number, 8956f10d37Spetede unsigned long long unit_from, unsigned long long scale); 9056f10d37Spetede 917c478bd9Sstevel@tonic-gate 927c478bd9Sstevel@tonic-gate int 937c478bd9Sstevel@tonic-gate main(int argc, char **argv) 947c478bd9Sstevel@tonic-gate { 957c478bd9Sstevel@tonic-gate int c, flag = 0; 967c478bd9Sstevel@tonic-gate int ret; 973e1bd7a2Ssjelinek int error = 0; 987c478bd9Sstevel@tonic-gate off_t s_offset = 0; 997c478bd9Sstevel@tonic-gate off_t length = 0; 1007c478bd9Sstevel@tonic-gate char *pathname; 1013e1bd7a2Ssjelinek char *msg; 1027c478bd9Sstevel@tonic-gate 1037c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 1047c478bd9Sstevel@tonic-gate 1057c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) 1067c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" 1077c478bd9Sstevel@tonic-gate #endif 1087c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 1097c478bd9Sstevel@tonic-gate 1107c478bd9Sstevel@tonic-gate prognamep = argv[0]; 1117c478bd9Sstevel@tonic-gate if (argc < 2) { 1127c478bd9Sstevel@tonic-gate usage(); 1137c478bd9Sstevel@tonic-gate exit(1); 1147c478bd9Sstevel@tonic-gate } 1157c478bd9Sstevel@tonic-gate 11656f10d37Spetede while ((c = getopt(argc, argv, "khlsd:a:12")) != EOF) { 1177c478bd9Sstevel@tonic-gate char *char_p; 1187c478bd9Sstevel@tonic-gate switch (c) { 1197c478bd9Sstevel@tonic-gate case 'l': /* list all the swap devices */ 1207c478bd9Sstevel@tonic-gate flag |= LFLAG; 1217c478bd9Sstevel@tonic-gate break; 1227c478bd9Sstevel@tonic-gate case 's': 1237c478bd9Sstevel@tonic-gate flag |= SFLAG; 1247c478bd9Sstevel@tonic-gate break; 1257c478bd9Sstevel@tonic-gate case 'd': 1267c478bd9Sstevel@tonic-gate /* 1277c478bd9Sstevel@tonic-gate * The argument for starting offset is optional. 1287c478bd9Sstevel@tonic-gate * If no argument is specified, the entire swap file 1297c478bd9Sstevel@tonic-gate * is added although this will fail if a non-zero 1307c478bd9Sstevel@tonic-gate * starting offset was specified when added. 1317c478bd9Sstevel@tonic-gate */ 1327c478bd9Sstevel@tonic-gate if ((argc - optind) > 1 || flag != 0) { 1337c478bd9Sstevel@tonic-gate usage(); 1347c478bd9Sstevel@tonic-gate exit(1); 1357c478bd9Sstevel@tonic-gate } 1367c478bd9Sstevel@tonic-gate flag |= DFLAG; 1377c478bd9Sstevel@tonic-gate pathname = optarg; 1387c478bd9Sstevel@tonic-gate if (optind < argc) { 1397c478bd9Sstevel@tonic-gate errno = 0; 1407c478bd9Sstevel@tonic-gate s_offset = strtol(argv[optind++], &char_p, 10); 1417c478bd9Sstevel@tonic-gate if (errno != 0 || *char_p != '\0') { 1427c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 1437c478bd9Sstevel@tonic-gate gettext("error in [low block]\n")); 1447c478bd9Sstevel@tonic-gate exit(1); 1457c478bd9Sstevel@tonic-gate } 1467c478bd9Sstevel@tonic-gate } 1477c478bd9Sstevel@tonic-gate ret = delete(pathname, s_offset); 1487c478bd9Sstevel@tonic-gate break; 1497c478bd9Sstevel@tonic-gate 1507c478bd9Sstevel@tonic-gate case 'a': 1517c478bd9Sstevel@tonic-gate /* 1527c478bd9Sstevel@tonic-gate * The arguments for starting offset and number of 1537c478bd9Sstevel@tonic-gate * blocks are optional. If only the starting offset 1547c478bd9Sstevel@tonic-gate * is specified, all the blocks to the end of the swap 1557c478bd9Sstevel@tonic-gate * file will be added. If no starting offset is 1567c478bd9Sstevel@tonic-gate * specified, the entire swap file is assumed. 1577c478bd9Sstevel@tonic-gate */ 1587c478bd9Sstevel@tonic-gate if ((argc - optind) > 2 || 1597c478bd9Sstevel@tonic-gate (flag & ~(P1FLAG | P2FLAG)) != 0) { 1607c478bd9Sstevel@tonic-gate usage(); 1617c478bd9Sstevel@tonic-gate exit(1); 1627c478bd9Sstevel@tonic-gate } 1637c478bd9Sstevel@tonic-gate if (*optarg != '/') { 1647c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 1657c478bd9Sstevel@tonic-gate gettext("%s: path must be absolute\n"), 1667c478bd9Sstevel@tonic-gate prognamep); 1677c478bd9Sstevel@tonic-gate exit(1); 1687c478bd9Sstevel@tonic-gate } 1697c478bd9Sstevel@tonic-gate flag |= AFLAG; 1707c478bd9Sstevel@tonic-gate pathname = optarg; 1717c478bd9Sstevel@tonic-gate if (optind < argc) { 1727c478bd9Sstevel@tonic-gate errno = 0; 1737c478bd9Sstevel@tonic-gate s_offset = strtol(argv[optind++], &char_p, 10); 1747c478bd9Sstevel@tonic-gate if (errno != 0 || *char_p != '\0') { 1757c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 1767c478bd9Sstevel@tonic-gate gettext("error in [low block]\n")); 1777c478bd9Sstevel@tonic-gate exit(1); 1787c478bd9Sstevel@tonic-gate } 1797c478bd9Sstevel@tonic-gate } 1807c478bd9Sstevel@tonic-gate if (optind < argc) { 1817c478bd9Sstevel@tonic-gate errno = 0; 1827c478bd9Sstevel@tonic-gate length = strtol(argv[optind++], &char_p, 10); 1837c478bd9Sstevel@tonic-gate if (errno != 0 || *char_p != '\0') { 1847c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 1857c478bd9Sstevel@tonic-gate gettext("error in [nbr of blocks]\n")); 1867c478bd9Sstevel@tonic-gate exit(1); 1877c478bd9Sstevel@tonic-gate } 1887c478bd9Sstevel@tonic-gate } 1897c478bd9Sstevel@tonic-gate break; 19056f10d37Spetede case 'h': 19156f10d37Spetede flag |= HFLAG; 19256f10d37Spetede break; 19356f10d37Spetede 19456f10d37Spetede case 'k': 19556f10d37Spetede flag |= KFLAG; 19656f10d37Spetede break; 1977c478bd9Sstevel@tonic-gate 1987c478bd9Sstevel@tonic-gate case '1': 1997c478bd9Sstevel@tonic-gate flag |= P1FLAG; 2007c478bd9Sstevel@tonic-gate break; 2017c478bd9Sstevel@tonic-gate 2027c478bd9Sstevel@tonic-gate case '2': 2037c478bd9Sstevel@tonic-gate flag |= P2FLAG; 2047c478bd9Sstevel@tonic-gate break; 2057c478bd9Sstevel@tonic-gate 2067c478bd9Sstevel@tonic-gate case '?': 2077c478bd9Sstevel@tonic-gate usage(); 2087c478bd9Sstevel@tonic-gate exit(1); 2097c478bd9Sstevel@tonic-gate } 2107c478bd9Sstevel@tonic-gate } 21156f10d37Spetede 21256f10d37Spetede if (flag & SFLAG) { 21356f10d37Spetede if (flag & ~SFLAG & ~HFLAG) { 21456f10d37Spetede /* 21556f10d37Spetede * The only option that can be used with -s is -h. 21656f10d37Spetede */ 21756f10d37Spetede usage(); 21856f10d37Spetede exit(1); 21956f10d37Spetede } 22056f10d37Spetede 22156f10d37Spetede ret = doswap(flag); 22256f10d37Spetede 22356f10d37Spetede } 22456f10d37Spetede 22556f10d37Spetede if (flag & LFLAG) { 22656f10d37Spetede if (flag & ~KFLAG & ~HFLAG & ~LFLAG) { 22756f10d37Spetede usage(); 22856f10d37Spetede exit(1); 22956f10d37Spetede } 23056f10d37Spetede ret = list(flag); 23156f10d37Spetede } 23256f10d37Spetede 2333e1bd7a2Ssjelinek /* 2343e1bd7a2Ssjelinek * do the add here. Check for in use prior to add. 2353e1bd7a2Ssjelinek * The values for length and offset are set above. 2363e1bd7a2Ssjelinek */ 2373e1bd7a2Ssjelinek if (flag & AFLAG) { 2383e1bd7a2Ssjelinek /* 2393e1bd7a2Ssjelinek * If device is in use for a swap device, print message 2403e1bd7a2Ssjelinek * and exit. 2413e1bd7a2Ssjelinek */ 2423e1bd7a2Ssjelinek if (dm_inuse(pathname, &msg, DM_WHO_SWAP, &error) || 2433e1bd7a2Ssjelinek error) { 2443e1bd7a2Ssjelinek if (error != 0) { 2453e1bd7a2Ssjelinek (void) fprintf(stderr, gettext("Error occurred" 2463e1bd7a2Ssjelinek " with device in use checking: %s\n"), 2473e1bd7a2Ssjelinek strerror(error)); 2483e1bd7a2Ssjelinek } else { 2493e1bd7a2Ssjelinek (void) fprintf(stderr, "%s", msg); 2503e1bd7a2Ssjelinek free(msg); 2513e1bd7a2Ssjelinek exit(1); 2523e1bd7a2Ssjelinek } 2533e1bd7a2Ssjelinek } 2543e1bd7a2Ssjelinek if ((ret = valid(pathname, 2553e1bd7a2Ssjelinek s_offset * 512, length * 512)) == 0) { 2563e1bd7a2Ssjelinek ret = add(pathname, s_offset, length, flag); 2573e1bd7a2Ssjelinek } 2583e1bd7a2Ssjelinek } 25956f10d37Spetede if (!(flag & ~HFLAG & ~KFLAG)) { 26056f10d37Spetede /* only -h and/or -k flag, or no flag */ 2617c478bd9Sstevel@tonic-gate usage(); 2627c478bd9Sstevel@tonic-gate exit(1); 2637c478bd9Sstevel@tonic-gate } 2647c478bd9Sstevel@tonic-gate return (ret); 2657c478bd9Sstevel@tonic-gate } 2667c478bd9Sstevel@tonic-gate 2677c478bd9Sstevel@tonic-gate 2687c478bd9Sstevel@tonic-gate static void 2697c478bd9Sstevel@tonic-gate usage(void) 2707c478bd9Sstevel@tonic-gate { 2717c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("Usage:\t%s -l\n"), prognamep); 27256f10d37Spetede (void) fprintf(stderr, gettext("\tsub option :\n")); 27356f10d37Spetede (void) fprintf(stderr, gettext("\t\t-h : displays size in human " 27456f10d37Spetede "readable format\n")); 27556f10d37Spetede (void) fprintf(stderr, gettext("\t\t-k : displays size in KB\n")); 2767c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "\t%s -s\n", prognamep); 27756f10d37Spetede (void) fprintf(stderr, gettext("\tsub option :\n")); 27856f10d37Spetede (void) fprintf(stderr, gettext("\t\t-h : displays size in human " 27956f10d37Spetede "readable format rather than KB\n")); 2807c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("\t%s -d <file name> [low block]\n"), 2817c478bd9Sstevel@tonic-gate prognamep); 2827c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("\t%s -a <file name> [low block]" 2837c478bd9Sstevel@tonic-gate " [nbr of blocks]\n"), prognamep); 2847c478bd9Sstevel@tonic-gate } 2857c478bd9Sstevel@tonic-gate 2867c478bd9Sstevel@tonic-gate /* 2877c478bd9Sstevel@tonic-gate * Implement: 2887c478bd9Sstevel@tonic-gate * #define ctok(x) ((ctob(x))>>10) 2897c478bd9Sstevel@tonic-gate * in a machine independent way. (Both assume a click > 1k) 2907c478bd9Sstevel@tonic-gate */ 2917c478bd9Sstevel@tonic-gate static size_t 2927c478bd9Sstevel@tonic-gate ctok(pgcnt_t clicks) 2937c478bd9Sstevel@tonic-gate { 2947c478bd9Sstevel@tonic-gate static int factor = -1; 2957c478bd9Sstevel@tonic-gate 2967c478bd9Sstevel@tonic-gate if (factor == -1) 2977c478bd9Sstevel@tonic-gate factor = (int)(sysconf(_SC_PAGESIZE) >> 10); 2987c478bd9Sstevel@tonic-gate return ((size_t)(clicks * factor)); 2997c478bd9Sstevel@tonic-gate } 3007c478bd9Sstevel@tonic-gate 3017c478bd9Sstevel@tonic-gate 3027c478bd9Sstevel@tonic-gate static int 30356f10d37Spetede doswap(int flag) 3047c478bd9Sstevel@tonic-gate { 3057c478bd9Sstevel@tonic-gate struct anoninfo ai; 3067c478bd9Sstevel@tonic-gate pgcnt_t allocated, reserved, available; 30756f10d37Spetede numbuf_t numbuf; 30856f10d37Spetede unsigned long long scale = 1024L; 3097c478bd9Sstevel@tonic-gate 3107c478bd9Sstevel@tonic-gate /* 3117c478bd9Sstevel@tonic-gate * max = total amount of swap space including physical memory 3127c478bd9Sstevel@tonic-gate * ai.ani_max = MAX(anoninfo.ani_resv, anoninfo.ani_max) + 3137c478bd9Sstevel@tonic-gate * availrmem - swapfs_minfree; 3147c478bd9Sstevel@tonic-gate * ai.ani_free = amount of unallocated anonymous memory 3157c478bd9Sstevel@tonic-gate * (ie. = resverved_unallocated + unreserved) 3167c478bd9Sstevel@tonic-gate * ai.ani_free = anoninfo.ani_free + (availrmem - swapfs_minfree); 3177c478bd9Sstevel@tonic-gate * ai.ani_resv = total amount of reserved anonymous memory 3187c478bd9Sstevel@tonic-gate * ai.ani_resv = anoninfo.ani_resv; 3197c478bd9Sstevel@tonic-gate * 3207c478bd9Sstevel@tonic-gate * allocated = anon memory not free 3217c478bd9Sstevel@tonic-gate * reserved = anon memory reserved but not allocated 3227c478bd9Sstevel@tonic-gate * available = anon memory not reserved 3237c478bd9Sstevel@tonic-gate */ 3247c478bd9Sstevel@tonic-gate if (swapctl(SC_AINFO, &ai) == -1) { 3257c478bd9Sstevel@tonic-gate perror(prognamep); 3267c478bd9Sstevel@tonic-gate return (2); 3277c478bd9Sstevel@tonic-gate } 3287c478bd9Sstevel@tonic-gate 3297c478bd9Sstevel@tonic-gate allocated = ai.ani_max - ai.ani_free; 3307c478bd9Sstevel@tonic-gate reserved = ai.ani_resv - allocated; 3317c478bd9Sstevel@tonic-gate available = ai.ani_max - ai.ani_resv; 3327c478bd9Sstevel@tonic-gate 3337c478bd9Sstevel@tonic-gate /* 3347c478bd9Sstevel@tonic-gate * TRANSLATION_NOTE 3357c478bd9Sstevel@tonic-gate * Translations (if any) of these keywords should match with 3367c478bd9Sstevel@tonic-gate * translations (if any) of the swap.1M man page keywords for 3377c478bd9Sstevel@tonic-gate * -s option: "allocated", "reserved", "used", "available" 3387c478bd9Sstevel@tonic-gate */ 33956f10d37Spetede 34056f10d37Spetede if (flag & HFLAG) { 34156f10d37Spetede int factor = (int)(sysconf(_SC_PAGESIZE)); 34256f10d37Spetede (void) printf(gettext("total: %s allocated + "), 34356f10d37Spetede number_to_scaled_string(numbuf, allocated, 34456f10d37Spetede factor, scale)); 34556f10d37Spetede (void) printf(gettext("%s reserved = "), 3469ab3bc54Spetede number_to_scaled_string(numbuf, reserved, 34756f10d37Spetede factor, scale)); 34856f10d37Spetede (void) printf(gettext("%s used, "), 34956f10d37Spetede number_to_scaled_string(numbuf, 35056f10d37Spetede allocated + reserved, factor, scale)); 35156f10d37Spetede (void) printf(gettext("%s available\n"), 35256f10d37Spetede number_to_scaled_string(numbuf, available, 35356f10d37Spetede factor, scale)); 35456f10d37Spetede } else { 35556f10d37Spetede (void) printf(gettext("total: %luk bytes allocated + %luk" 35656f10d37Spetede " reserved = %luk used, %luk available\n"), 35756f10d37Spetede ctok(allocated), ctok(reserved), 35856f10d37Spetede ctok(reserved) + ctok(allocated), 3597c478bd9Sstevel@tonic-gate ctok(available)); 36056f10d37Spetede } 3617c478bd9Sstevel@tonic-gate 3627c478bd9Sstevel@tonic-gate return (0); 3637c478bd9Sstevel@tonic-gate } 3647c478bd9Sstevel@tonic-gate 3657c478bd9Sstevel@tonic-gate static int 36656f10d37Spetede list(int flag) 3677c478bd9Sstevel@tonic-gate { 3687c478bd9Sstevel@tonic-gate struct swaptable *st; 3697c478bd9Sstevel@tonic-gate struct swapent *swapent; 3707c478bd9Sstevel@tonic-gate int i; 3717c478bd9Sstevel@tonic-gate struct stat64 statbuf; 3727c478bd9Sstevel@tonic-gate char *path; 3737c478bd9Sstevel@tonic-gate char fullpath[MAXPATHLEN+1]; 3747c478bd9Sstevel@tonic-gate int num; 37556f10d37Spetede numbuf_t numbuf; 37656f10d37Spetede unsigned long long scale = 1024L; 3777c478bd9Sstevel@tonic-gate 3787c478bd9Sstevel@tonic-gate if ((num = swapctl(SC_GETNSWP, NULL)) == -1) { 3797c478bd9Sstevel@tonic-gate perror(prognamep); 3807c478bd9Sstevel@tonic-gate return (2); 3817c478bd9Sstevel@tonic-gate } 3827c478bd9Sstevel@tonic-gate if (num == 0) { 3837c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("No swap devices configured\n")); 3847c478bd9Sstevel@tonic-gate return (1); 3857c478bd9Sstevel@tonic-gate } 3867c478bd9Sstevel@tonic-gate 3877c478bd9Sstevel@tonic-gate if ((st = malloc(num * sizeof (swapent_t) + sizeof (int))) 3887c478bd9Sstevel@tonic-gate == NULL) { 3897c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 3907c478bd9Sstevel@tonic-gate gettext("Malloc failed. Please try later.\n")); 3917c478bd9Sstevel@tonic-gate perror(prognamep); 3927c478bd9Sstevel@tonic-gate return (2); 3937c478bd9Sstevel@tonic-gate } 3947c478bd9Sstevel@tonic-gate if ((path = malloc(num * MAXPATHLEN)) == NULL) { 3957c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 3967c478bd9Sstevel@tonic-gate gettext("Malloc failed. Please try later.\n")); 3977c478bd9Sstevel@tonic-gate perror(prognamep); 3987c478bd9Sstevel@tonic-gate return (2); 3997c478bd9Sstevel@tonic-gate } 4007c478bd9Sstevel@tonic-gate swapent = st->swt_ent; 4017c478bd9Sstevel@tonic-gate for (i = 0; i < num; i++, swapent++) { 4027c478bd9Sstevel@tonic-gate swapent->ste_path = path; 4037c478bd9Sstevel@tonic-gate path += MAXPATHLEN; 4047c478bd9Sstevel@tonic-gate } 4057c478bd9Sstevel@tonic-gate 4067c478bd9Sstevel@tonic-gate st->swt_n = num; 4077c478bd9Sstevel@tonic-gate if ((num = swapctl(SC_LIST, st)) == -1) { 4087c478bd9Sstevel@tonic-gate perror(prognamep); 4097c478bd9Sstevel@tonic-gate return (2); 4107c478bd9Sstevel@tonic-gate } 4117c478bd9Sstevel@tonic-gate 4127c478bd9Sstevel@tonic-gate /* 4137c478bd9Sstevel@tonic-gate * TRANSLATION_NOTE 4147c478bd9Sstevel@tonic-gate * Following translations for "swap -l" should account for for 4157c478bd9Sstevel@tonic-gate * alignment of header and output. 4167c478bd9Sstevel@tonic-gate * The first translation is for the header. If the alignment 4177c478bd9Sstevel@tonic-gate * of the header changes, change the next 5 formats as needed 4187c478bd9Sstevel@tonic-gate * to make alignment of output agree with alignment of the header. 4197c478bd9Sstevel@tonic-gate * The next four translations are four cases for printing the 4207c478bd9Sstevel@tonic-gate * 1st & 2nd fields. 4217c478bd9Sstevel@tonic-gate * The next translation is for printing the 3rd, 4th & 5th fields. 4227c478bd9Sstevel@tonic-gate * 4237c478bd9Sstevel@tonic-gate * Translations (if any) of the following keywords should match the 4247c478bd9Sstevel@tonic-gate * translations (if any) of the swap.1M man page keywords for 4257c478bd9Sstevel@tonic-gate * -l option: "swapfile", "dev", "swaplo", "blocks", "free" 4267c478bd9Sstevel@tonic-gate */ 4277c478bd9Sstevel@tonic-gate (void) printf( 4287c478bd9Sstevel@tonic-gate gettext("swapfile dev swaplo blocks free\n")); 4297c478bd9Sstevel@tonic-gate 4307c478bd9Sstevel@tonic-gate swapent = st->swt_ent; 4317c478bd9Sstevel@tonic-gate for (i = 0; i < num; i++, swapent++) { 4327c478bd9Sstevel@tonic-gate if (*swapent->ste_path != '/') 4337c478bd9Sstevel@tonic-gate (void) snprintf(fullpath, sizeof (fullpath), 4347c478bd9Sstevel@tonic-gate "/dev/%s", swapent->ste_path); 4357c478bd9Sstevel@tonic-gate else 4367c478bd9Sstevel@tonic-gate (void) snprintf(fullpath, sizeof (fullpath), 4377c478bd9Sstevel@tonic-gate "%s", swapent->ste_path); 4387c478bd9Sstevel@tonic-gate if (stat64(fullpath, &statbuf) < 0) 4397c478bd9Sstevel@tonic-gate if (*swapent->ste_path != '/') 4407c478bd9Sstevel@tonic-gate (void) printf(gettext("%-20s - "), 4417c478bd9Sstevel@tonic-gate swapent->ste_path); 4427c478bd9Sstevel@tonic-gate else 4437c478bd9Sstevel@tonic-gate (void) printf(gettext("%-20s ?,? "), 4447c478bd9Sstevel@tonic-gate fullpath); 4457c478bd9Sstevel@tonic-gate else { 4464bc0a2efScasper if (S_ISBLK(statbuf.st_mode) || 4474bc0a2efScasper S_ISCHR(statbuf.st_mode)) { 4487c478bd9Sstevel@tonic-gate (void) printf(gettext("%-19s %2lu,%-2lu"), 4497c478bd9Sstevel@tonic-gate fullpath, 4507c478bd9Sstevel@tonic-gate major(statbuf.st_rdev), 4517c478bd9Sstevel@tonic-gate minor(statbuf.st_rdev)); 4524bc0a2efScasper } else { 4537c478bd9Sstevel@tonic-gate (void) printf(gettext("%-20s - "), fullpath); 4547c478bd9Sstevel@tonic-gate } 4554bc0a2efScasper } 4567c478bd9Sstevel@tonic-gate { 4577c478bd9Sstevel@tonic-gate int diskblks_per_page = 4587c478bd9Sstevel@tonic-gate (int)(sysconf(_SC_PAGESIZE) >> DEV_BSHIFT); 45956f10d37Spetede if (flag & HFLAG) { 46056f10d37Spetede (void) printf(gettext(" %8s"), 46156f10d37Spetede number_to_scaled_string(numbuf, 46256f10d37Spetede swapent->ste_start, DEV_BSIZE, 46356f10d37Spetede scale)); 46456f10d37Spetede (void) printf(gettext(" %8s"), 46556f10d37Spetede number_to_scaled_string(numbuf, 46656f10d37Spetede swapent->ste_pages * 46756f10d37Spetede diskblks_per_page, 46856f10d37Spetede DEV_BSIZE, scale)); 46956f10d37Spetede (void) printf(gettext(" %8s"), 47056f10d37Spetede number_to_scaled_string(numbuf, 47156f10d37Spetede swapent->ste_free * 47256f10d37Spetede diskblks_per_page, 47356f10d37Spetede DEV_BSIZE, scale)); 47456f10d37Spetede } else if (flag & KFLAG) { 47556f10d37Spetede (void) printf(gettext(" %7luK %7luK %7luK"), 47656f10d37Spetede swapent->ste_start * DEV_BSIZE / 1024, 47756f10d37Spetede swapent->ste_pages * diskblks_per_page * 47856f10d37Spetede DEV_BSIZE / 1024, 47956f10d37Spetede swapent->ste_free * diskblks_per_page * 48056f10d37Spetede DEV_BSIZE / 1024); 48156f10d37Spetede } else { 48256f10d37Spetede (void) printf(gettext(" %8lu %8lu %8lu"), 48356f10d37Spetede swapent->ste_start, 4847c478bd9Sstevel@tonic-gate swapent->ste_pages * diskblks_per_page, 4857c478bd9Sstevel@tonic-gate swapent->ste_free * diskblks_per_page); 4867c478bd9Sstevel@tonic-gate } 48756f10d37Spetede } 4887c478bd9Sstevel@tonic-gate if (swapent->ste_flags & ST_INDEL) 4897c478bd9Sstevel@tonic-gate (void) printf(" INDEL\n"); 4907c478bd9Sstevel@tonic-gate else 4917c478bd9Sstevel@tonic-gate (void) printf("\n"); 4927c478bd9Sstevel@tonic-gate } 4937c478bd9Sstevel@tonic-gate return (0); 4947c478bd9Sstevel@tonic-gate } 4957c478bd9Sstevel@tonic-gate 49656f10d37Spetede /* Copied from du.c */ 49756f10d37Spetede static char * 49856f10d37Spetede number_to_scaled_string( 49956f10d37Spetede numbuf_t buf, /* put the result here */ 50056f10d37Spetede unsigned long long number, /* convert this number */ 50156f10d37Spetede unsigned long long unit_from, /* number of byes per input unit */ 50256f10d37Spetede unsigned long long scale) /* 1024 (-h) or 1000 (-H) */ 50356f10d37Spetede { 50456f10d37Spetede unsigned long long save = 0; 50556f10d37Spetede char *M = "KMGTPE"; /* Measurement: kilo, mega, giga, tera, peta, exa */ 50656f10d37Spetede char *uom = M; /* unit of measurement, initially 'K' (=M[0]) */ 50756f10d37Spetede 50856f10d37Spetede if ((long long)number == (long long) -1) { 50956f10d37Spetede (void) strcpy(buf, "-1"); 51056f10d37Spetede return (buf); 51156f10d37Spetede } 51256f10d37Spetede 51356f10d37Spetede /* 51456f10d37Spetede * Convert number from unit_from to given scale (1024 or 1000) 51556f10d37Spetede * This means multiply number with unit_from and divide by scale. 51656f10d37Spetede * if number is large enough, we first divide and then multiply 51756f10d37Spetede * to avoid an overflow (large enough here means 100 (rather arbitrary 51856f10d37Spetede * value) times scale in order to reduce rounding errors) 51956f10d37Spetede * otherwise, we first multiply and then divide to avoid an underflow. 52056f10d37Spetede */ 52156f10d37Spetede if (number >= 100L * scale) { 52256f10d37Spetede number = number / scale; 52356f10d37Spetede number = number * unit_from; 52456f10d37Spetede } else { 52556f10d37Spetede number = number * unit_from; 52656f10d37Spetede number = number / scale; 52756f10d37Spetede } 52856f10d37Spetede 52956f10d37Spetede /* 53056f10d37Spetede * Now we have number as a count of scale units. 53156f10d37Spetede * Stop scaling when we reached exa bytes, then something is 5329ab3bc54Spetede * probably wrong with our number. 53356f10d37Spetede */ 53456f10d37Spetede while ((number >= scale) && (*uom != 'E')) { 53556f10d37Spetede uom++; /* Next unit of measurement */ 53656f10d37Spetede save = number; 53756f10d37Spetede number = (number + (scale / 2)) / scale; 53856f10d37Spetede } 53956f10d37Spetede 54056f10d37Spetede /* Check if we should output a decimal place after the point */ 54156f10d37Spetede if (save && ((save / scale) < 10)) { 54256f10d37Spetede /* sprintf() will round for us */ 54356f10d37Spetede float fnum = (float)save / scale; 54456f10d37Spetede (void) sprintf(buf, "%.1f%c", fnum, *uom); 54556f10d37Spetede } else { 54656f10d37Spetede (void) sprintf(buf, "%llu%c", number, *uom); 54756f10d37Spetede } 54856f10d37Spetede return (buf); 54956f10d37Spetede } 55056f10d37Spetede 55156f10d37Spetede 55256f10d37Spetede 55356f10d37Spetede 5547c478bd9Sstevel@tonic-gate static void 5557c478bd9Sstevel@tonic-gate dumpadm_err(const char *warning) 5567c478bd9Sstevel@tonic-gate { 5577c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s (%s):\n", warning, strerror(errno)); 5587c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 5597c478bd9Sstevel@tonic-gate "run dumpadm(1M) to verify dump configuration\n")); 5607c478bd9Sstevel@tonic-gate } 5617c478bd9Sstevel@tonic-gate 5627c478bd9Sstevel@tonic-gate static int 5637c478bd9Sstevel@tonic-gate delete(char *path, off_t offset) 5647c478bd9Sstevel@tonic-gate { 5657c478bd9Sstevel@tonic-gate swapres_t swr; 5667c478bd9Sstevel@tonic-gate int fd; 5677c478bd9Sstevel@tonic-gate 5687c478bd9Sstevel@tonic-gate swr.sr_name = path; 5697c478bd9Sstevel@tonic-gate swr.sr_start = offset; 5707c478bd9Sstevel@tonic-gate 5717c478bd9Sstevel@tonic-gate if (swapctl(SC_REMOVE, &swr) < 0) { 5727c478bd9Sstevel@tonic-gate switch (errno) { 5737c478bd9Sstevel@tonic-gate case (ENOSYS): 5747c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 5757c478bd9Sstevel@tonic-gate "%s: Invalid operation for this filesystem type\n"), 5767c478bd9Sstevel@tonic-gate path); 5777c478bd9Sstevel@tonic-gate break; 5787c478bd9Sstevel@tonic-gate default: 5797c478bd9Sstevel@tonic-gate perror(path); 5807c478bd9Sstevel@tonic-gate break; 5817c478bd9Sstevel@tonic-gate } 5827c478bd9Sstevel@tonic-gate return (2); 5837c478bd9Sstevel@tonic-gate } 5847c478bd9Sstevel@tonic-gate 5857c478bd9Sstevel@tonic-gate /* 5867c478bd9Sstevel@tonic-gate * If our swap -d succeeded, open up /dev/dump and ask what the dump 5877c478bd9Sstevel@tonic-gate * device is set to. If this returns ENODEV, we just deleted the 5887c478bd9Sstevel@tonic-gate * dump device, so try to change the dump device to another swap 5897c478bd9Sstevel@tonic-gate * device. We do this by firing up /usr/sbin/dumpadm -ud swap. 5907c478bd9Sstevel@tonic-gate */ 5917c478bd9Sstevel@tonic-gate if ((fd = open("/dev/dump", O_RDONLY)) >= 0) { 5927c478bd9Sstevel@tonic-gate char dumpdev[MAXPATHLEN]; 5937c478bd9Sstevel@tonic-gate 5947c478bd9Sstevel@tonic-gate if (ioctl(fd, DIOCGETDEV, dumpdev) == -1) { 5957c478bd9Sstevel@tonic-gate if (errno == ENODEV) { 5967c478bd9Sstevel@tonic-gate (void) printf(gettext("%s was dump device --\n" 5977c478bd9Sstevel@tonic-gate "invoking dumpadm(1M) -d swap to " 5987c478bd9Sstevel@tonic-gate "select new dump device\n"), path); 5997c478bd9Sstevel@tonic-gate /* 6007c478bd9Sstevel@tonic-gate * Close /dev/dump prior to executing dumpadm 6017c478bd9Sstevel@tonic-gate * since /dev/dump mandates exclusive open. 6027c478bd9Sstevel@tonic-gate */ 6037c478bd9Sstevel@tonic-gate (void) close(fd); 6047c478bd9Sstevel@tonic-gate 6057c478bd9Sstevel@tonic-gate if (system("/usr/sbin/dumpadm -ud swap") == -1) 6067c478bd9Sstevel@tonic-gate dumpadm_err(gettext( 6077c478bd9Sstevel@tonic-gate "Warning: failed to execute dumpadm -d swap")); 6087c478bd9Sstevel@tonic-gate } else 6097c478bd9Sstevel@tonic-gate dumpadm_err(gettext( 6107c478bd9Sstevel@tonic-gate "Warning: failed to check dump device")); 6117c478bd9Sstevel@tonic-gate } 6127c478bd9Sstevel@tonic-gate (void) close(fd); 6137c478bd9Sstevel@tonic-gate } else 6147c478bd9Sstevel@tonic-gate dumpadm_err(gettext("Warning: failed to open /dev/dump")); 6157c478bd9Sstevel@tonic-gate 6167c478bd9Sstevel@tonic-gate return (0); 6177c478bd9Sstevel@tonic-gate } 6187c478bd9Sstevel@tonic-gate 6197c478bd9Sstevel@tonic-gate /* 6207c478bd9Sstevel@tonic-gate * swapres_t structure units are in 512-blocks 6217c478bd9Sstevel@tonic-gate */ 6227c478bd9Sstevel@tonic-gate static int 6237c478bd9Sstevel@tonic-gate add(char *path, off_t offset, off_t cnt, int flags) 6247c478bd9Sstevel@tonic-gate { 6257c478bd9Sstevel@tonic-gate swapres_t swr; 6267c478bd9Sstevel@tonic-gate 6277c478bd9Sstevel@tonic-gate int fd, have_dumpdev = 1; 6287c478bd9Sstevel@tonic-gate struct statvfs fsb; 6297c478bd9Sstevel@tonic-gate 6307c478bd9Sstevel@tonic-gate /* 6317c478bd9Sstevel@tonic-gate * Before adding swap, we first check to see if we have a dump 6327c478bd9Sstevel@tonic-gate * device configured. If we don't (errno == ENODEV), and if 6337c478bd9Sstevel@tonic-gate * our SC_ADD is successful, then run /usr/sbin/dumpadm -ud swap 6347c478bd9Sstevel@tonic-gate * to attempt to reconfigure the dump device to the new swap. 6357c478bd9Sstevel@tonic-gate */ 6367c478bd9Sstevel@tonic-gate if ((fd = open("/dev/dump", O_RDONLY)) >= 0) { 6377c478bd9Sstevel@tonic-gate char dumpdev[MAXPATHLEN]; 6387c478bd9Sstevel@tonic-gate 6397c478bd9Sstevel@tonic-gate if (ioctl(fd, DIOCGETDEV, dumpdev) == -1) { 6407c478bd9Sstevel@tonic-gate if (errno == ENODEV) 6417c478bd9Sstevel@tonic-gate have_dumpdev = 0; 6427c478bd9Sstevel@tonic-gate else 6437c478bd9Sstevel@tonic-gate dumpadm_err(gettext( 6447c478bd9Sstevel@tonic-gate "Warning: failed to check dump device")); 6457c478bd9Sstevel@tonic-gate } 6467c478bd9Sstevel@tonic-gate 6477c478bd9Sstevel@tonic-gate (void) close(fd); 6487c478bd9Sstevel@tonic-gate 649*e7cbe64fSgw25295 /* 650*e7cbe64fSgw25295 * zvols cannot act as both a swap device and dump device. 651*e7cbe64fSgw25295 */ 652*e7cbe64fSgw25295 if (strncmp(dumpdev, ZVOL_FULL_DEV_DIR, 653*e7cbe64fSgw25295 strlen(ZVOL_FULL_DEV_DIR)) == 0) { 654*e7cbe64fSgw25295 if (strcmp(dumpdev, path) == 0) { 655*e7cbe64fSgw25295 (void) fprintf(stderr, gettext("%s: zvol " 656*e7cbe64fSgw25295 "cannot be used as a swap device and a " 657*e7cbe64fSgw25295 "dump device\n"), path); 658*e7cbe64fSgw25295 return (2); 659*e7cbe64fSgw25295 } 660*e7cbe64fSgw25295 } 661*e7cbe64fSgw25295 6627c478bd9Sstevel@tonic-gate } else if (!(flags & P1FLAG)) 6637c478bd9Sstevel@tonic-gate dumpadm_err(gettext("Warning: failed to open /dev/dump")); 6647c478bd9Sstevel@tonic-gate 6657c478bd9Sstevel@tonic-gate swr.sr_name = path; 6667c478bd9Sstevel@tonic-gate swr.sr_start = offset; 6677c478bd9Sstevel@tonic-gate swr.sr_length = cnt; 6687c478bd9Sstevel@tonic-gate 6697c478bd9Sstevel@tonic-gate if (swapctl(SC_ADD, &swr) < 0) { 6707c478bd9Sstevel@tonic-gate switch (errno) { 6717c478bd9Sstevel@tonic-gate case (ENOSYS): 6727c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 6737c478bd9Sstevel@tonic-gate "%s: Invalid operation for this filesystem type\n"), 6747c478bd9Sstevel@tonic-gate path); 6757c478bd9Sstevel@tonic-gate break; 6767c478bd9Sstevel@tonic-gate case (EEXIST): 6777c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 6787c478bd9Sstevel@tonic-gate "%s: Overlapping swap files are not allowed\n"), 6797c478bd9Sstevel@tonic-gate path); 6807c478bd9Sstevel@tonic-gate break; 6817c478bd9Sstevel@tonic-gate default: 6827c478bd9Sstevel@tonic-gate perror(path); 6837c478bd9Sstevel@tonic-gate break; 6847c478bd9Sstevel@tonic-gate } 6857c478bd9Sstevel@tonic-gate return (2); 6867c478bd9Sstevel@tonic-gate } 6877c478bd9Sstevel@tonic-gate 6887c478bd9Sstevel@tonic-gate /* 6897c478bd9Sstevel@tonic-gate * If the swapctl worked and we don't have a dump device, and /etc 6907c478bd9Sstevel@tonic-gate * is part of a writeable filesystem, then run dumpadm -ud swap. 6917c478bd9Sstevel@tonic-gate * If /etc (presumably part of /) is still mounted read-only, then 6927c478bd9Sstevel@tonic-gate * dumpadm will fail to write its config file, so there's no point 6937c478bd9Sstevel@tonic-gate * running it now. This also avoids spurious messages during boot 6947c478bd9Sstevel@tonic-gate * when the first swapadd takes place, at which point / is still ro. 6957c478bd9Sstevel@tonic-gate * Similarly, if swapadd invoked us with -1 or -2 (but root is 6967c478bd9Sstevel@tonic-gate * writeable), we don't want to modify the dump device because 6977c478bd9Sstevel@tonic-gate * /etc/init.d/savecore has yet to execute; if we run dumpadm now 6987c478bd9Sstevel@tonic-gate * we would lose the user's previous setting. 6997c478bd9Sstevel@tonic-gate */ 7007c478bd9Sstevel@tonic-gate if (!have_dumpdev && !(flags & (P1FLAG | P2FLAG)) && 7017c478bd9Sstevel@tonic-gate statvfs("/etc", &fsb) == 0 && !(fsb.f_flag & ST_RDONLY)) { 7027c478bd9Sstevel@tonic-gate 7037c478bd9Sstevel@tonic-gate (void) printf( 7047c478bd9Sstevel@tonic-gate gettext("operating system crash dump was previously " 7057c478bd9Sstevel@tonic-gate "disabled --\ninvoking dumpadm(1M) -d swap to select " 7067c478bd9Sstevel@tonic-gate "new dump device\n")); 7077c478bd9Sstevel@tonic-gate 7087c478bd9Sstevel@tonic-gate if (system("/usr/sbin/dumpadm -ud swap") == -1) 7097c478bd9Sstevel@tonic-gate dumpadm_err(gettext( 7107c478bd9Sstevel@tonic-gate "Warning: failed to execute dumpadm -d swap")); 7117c478bd9Sstevel@tonic-gate } 7127c478bd9Sstevel@tonic-gate 7137c478bd9Sstevel@tonic-gate return (0); 7147c478bd9Sstevel@tonic-gate } 7157c478bd9Sstevel@tonic-gate 7167c478bd9Sstevel@tonic-gate static int 7177c478bd9Sstevel@tonic-gate valid(char *pathname, off_t offset, off_t length) 7187c478bd9Sstevel@tonic-gate { 7197c478bd9Sstevel@tonic-gate struct stat64 f; 7207c478bd9Sstevel@tonic-gate struct statvfs64 fs; 7217c478bd9Sstevel@tonic-gate off_t need; 7227c478bd9Sstevel@tonic-gate 7237c478bd9Sstevel@tonic-gate if (stat64(pathname, &f) < 0 || statvfs64(pathname, &fs) < 0) { 7247c478bd9Sstevel@tonic-gate (void) perror(pathname); 7257c478bd9Sstevel@tonic-gate return (errno); 7267c478bd9Sstevel@tonic-gate } 7277c478bd9Sstevel@tonic-gate 7287c478bd9Sstevel@tonic-gate if (!((S_ISREG(f.st_mode) && (f.st_mode & S_ISVTX) == S_ISVTX) || 7297c478bd9Sstevel@tonic-gate S_ISBLK(f.st_mode))) { 7307c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 7317c478bd9Sstevel@tonic-gate gettext("\"%s\" is not valid for swapping.\n" 7327c478bd9Sstevel@tonic-gate "It must be a block device or a regular file with the\n" 7337c478bd9Sstevel@tonic-gate "\"save user text on execution\" bit set.\n"), 7347c478bd9Sstevel@tonic-gate pathname); 7357c478bd9Sstevel@tonic-gate return (EINVAL); 7367c478bd9Sstevel@tonic-gate } 7377c478bd9Sstevel@tonic-gate 7387c478bd9Sstevel@tonic-gate if (S_ISREG(f.st_mode)) { 7397c478bd9Sstevel@tonic-gate if (length == 0) 7407c478bd9Sstevel@tonic-gate length = (off_t)f.st_size; 7417c478bd9Sstevel@tonic-gate 7427c478bd9Sstevel@tonic-gate /* 7437c478bd9Sstevel@tonic-gate * "f.st_blocks < 8" because the first eight 7447c478bd9Sstevel@tonic-gate * 512-byte sectors are always skipped 7457c478bd9Sstevel@tonic-gate */ 7467c478bd9Sstevel@tonic-gate 7477c478bd9Sstevel@tonic-gate if (f.st_size < (length - offset) || f.st_size == 0 || 7487c478bd9Sstevel@tonic-gate f.st_size > MAXOFF_T || f.st_blocks < 8 || length < 0) { 7497c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s: size is invalid\n"), 7507c478bd9Sstevel@tonic-gate pathname); 7517c478bd9Sstevel@tonic-gate return (EINVAL); 7527c478bd9Sstevel@tonic-gate } 7537c478bd9Sstevel@tonic-gate 7547c478bd9Sstevel@tonic-gate if (offset < 0) { 7557c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 7567c478bd9Sstevel@tonic-gate gettext("%s: low block is invalid\n"), 7577c478bd9Sstevel@tonic-gate pathname); 7587c478bd9Sstevel@tonic-gate return (EINVAL); 7597c478bd9Sstevel@tonic-gate } 7607c478bd9Sstevel@tonic-gate 7617c478bd9Sstevel@tonic-gate need = roundup(length, fs.f_bsize) / DEV_BSIZE; 7627c478bd9Sstevel@tonic-gate 7637c478bd9Sstevel@tonic-gate /* 7647c478bd9Sstevel@tonic-gate * "need > f.st_blocks" to account for indirect blocks 7657c478bd9Sstevel@tonic-gate * Note: 7667c478bd9Sstevel@tonic-gate * This can be fooled by a file large enough to 7677c478bd9Sstevel@tonic-gate * contain indirect blocks that also contains holes. 7687c478bd9Sstevel@tonic-gate * However, we don't know (and don't want to know) 7697c478bd9Sstevel@tonic-gate * about the underlying storage implementation. 7707c478bd9Sstevel@tonic-gate * But, if it doesn't have at least this many blocks, 7717c478bd9Sstevel@tonic-gate * there must be a hole. 7727c478bd9Sstevel@tonic-gate */ 7737c478bd9Sstevel@tonic-gate 7747c478bd9Sstevel@tonic-gate if (need > f.st_blocks) { 7757c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 7767c478bd9Sstevel@tonic-gate "\"%s\" may contain holes - can't swap on it.\n"), 7777c478bd9Sstevel@tonic-gate pathname); 7787c478bd9Sstevel@tonic-gate return (EINVAL); 7797c478bd9Sstevel@tonic-gate } 7807c478bd9Sstevel@tonic-gate } 7817c478bd9Sstevel@tonic-gate /* 7827c478bd9Sstevel@tonic-gate * else, we cannot get st_size for S_ISBLK device and 7837c478bd9Sstevel@tonic-gate * no meaningful checking can be done. 7847c478bd9Sstevel@tonic-gate */ 7857c478bd9Sstevel@tonic-gate 7867c478bd9Sstevel@tonic-gate return (0); 7877c478bd9Sstevel@tonic-gate } 788