lfs.c

来自「lustre 1.6.5 source code」· C语言 代码 · 共 1,844 行 · 第 1/5 页

C
1,844
字号
                        return CMD_HELP;                }        }        if (check_type)                check_type--;        else    /* do quotacheck for both user & group quota by default */                check_type = 0x02;        if (argc == optind)                return CMD_HELP;        mnt = argv[optind];        memset(&qctl, 0, sizeof(qctl));        qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;        qctl.qc_id = QFMT_LDISKFS;        qctl.qc_type = check_type;        rc = llapi_quotactl(mnt, &qctl);        if (rc) {                fprintf(stderr, "quota off failed: %s\n", strerror(errno));                return rc;        }        rc = llapi_quotacheck(mnt, check_type);        if (rc) {                fprintf(stderr, "quotacheck failed: %s\n", strerror(errno));                return rc;        }        rc = llapi_poll_quotacheck(mnt, &qchk);        if (rc) {                if (*obd_type)                        fprintf(stderr, "%s %s ", obd_type,                                obd_uuid2str(&qchk.obd_uuid));                fprintf(stderr, "quota check failed: %s\n", strerror(errno));                return rc;        }        memset(&qctl, 0, sizeof(qctl));        qctl.qc_cmd = LUSTRE_Q_QUOTAON;        qctl.qc_id = QFMT_LDISKFS;        qctl.qc_type = check_type;        rc = llapi_quotactl(mnt, &qctl);        if (rc) {                if (*obd_type)                        fprintf(stderr, "%s %s ", (char *)qctl.obd_type,                                obd_uuid2str(&qctl.obd_uuid));                fprintf(stderr, "%s turn on quota failed: %s\n",                        argv[0], strerror(errno));                return rc;        }        return 0;}static int lfs_quotaon(int argc, char **argv){        int c;        char *mnt;        struct if_quotactl qctl;        char *obd_type = (char *)qctl.obd_type;        int rc;        memset(&qctl, 0, sizeof(qctl));        qctl.qc_cmd = LUSTRE_Q_QUOTAON;        qctl.qc_id = QFMT_LDISKFS;        optind = 0;        while ((c = getopt(argc, argv, "ugf")) != -1) {                switch (c) {                case 'u':                        qctl.qc_type |= 0x01;                        break;                case 'g':                        qctl.qc_type |= 0x02;                        break;                case 'f':                        qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;                        break;                default:                        fprintf(stderr, "error: %s: option '-%c' "                                        "unrecognized\n", argv[0], c);                        return CMD_HELP;                }        }        if (qctl.qc_type)                qctl.qc_type--;        else /* by default, enable quota for both user & group */                qctl.qc_type = 0x02;        if (argc == optind)                return CMD_HELP;        mnt = argv[optind];        rc = llapi_quotactl(mnt, &qctl);        if (rc) {                if (*obd_type)                        fprintf(stderr, "%s %s ", obd_type,                                obd_uuid2str(&qctl.obd_uuid));                fprintf(stderr, "%s failed: %s\n", argv[0], strerror(errno));                return rc;        }        return 0;}static int lfs_quotaoff(int argc, char **argv){        int c;        char *mnt;        struct if_quotactl qctl;        char *obd_type = (char *)qctl.obd_type;        int rc;        memset(&qctl, 0, sizeof(qctl));        qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;        optind = 0;        while ((c = getopt(argc, argv, "ug")) != -1) {                switch (c) {                case 'u':                        qctl.qc_type |= 0x01;                        break;                case 'g':                        qctl.qc_type |= 0x02;                        break;                default:                        fprintf(stderr, "error: %s: option '-%c' "                                        "unrecognized\n", argv[0], c);                        return CMD_HELP;                }        }        if (qctl.qc_type)                qctl.qc_type--;        else /* by default, disable quota for both user & group */                qctl.qc_type = 0x02;        if (argc == optind)                return CMD_HELP;        mnt = argv[optind];        rc = llapi_quotactl(mnt, &qctl);        if (rc == -1 && errno == ESRCH) {                fprintf(stderr, "\n%s quotas are not enabled.\n",                         qctl.qc_type == 0x00 ? "user" : "group");                return 0;        }        if (rc) {                if (*obd_type)                        fprintf(stderr, "%s %s ", obd_type,                                obd_uuid2str(&qctl.obd_uuid));                fprintf(stderr, "quotaoff failed: %s\n", strerror(errno));                return rc;        }        return 0;}static int lfs_quotainv(int argc, char **argv){        int c;        char *mnt;        struct if_quotactl qctl;        int rc;        memset(&qctl, 0, sizeof(qctl));        qctl.qc_cmd = LUSTRE_Q_INVALIDATE;        optind = 0;        while ((c = getopt(argc, argv, "ug")) != -1) {                switch (c) {                case 'u':                        qctl.qc_type |= 0x01;                        break;                case 'g':                        qctl.qc_type |= 0x02;                        break;                default:                        fprintf(stderr, "error: %s: option '-%c' "                                        "unrecognized\n", argv[0], c);                        return CMD_HELP;                }        }        if (qctl.qc_type)                qctl.qc_type--;        else /* by default, invalidate quota for both user & group */                qctl.qc_type = 0x02;        if (argc == optind)                return CMD_HELP;        mnt = argv[optind];        rc = llapi_quotactl(mnt, &qctl);        if (rc) {                fprintf(stderr, "quotainv failed: %s\n", strerror(errno));                return rc;        }        return 0;}static int name2id(unsigned int *id, char *name, int type){        if (type == USRQUOTA) {                struct passwd *entry;                if (!(entry = getpwnam(name))) {                        if (!errno)                                errno = ENOENT;                        return -1;                }                *id = entry->pw_uid;        } else {                struct group *entry;                if (!(entry = getgrnam(name))) {                        if (!errno)                                errno = ENOENT;                        return -1;                }                *id = entry->gr_gid;        }        return 0;}static int id2name(char **name, unsigned int id, int type){        if (type == USRQUOTA) {                struct passwd *entry;                if (!(entry = getpwuid(id))) {                        if (!errno)                                errno = ENOENT;                        return -1;                }                *name = entry->pw_name;        } else {                struct group *entry;                if (!(entry = getgrgid(id))) {                        if (!errno)                                errno = ENOENT;                        return -1;                }                *name = entry->gr_name;        }        return 0;}#define ARG2INT(nr, str, msg)                                           \do {                                                                    \        char *endp;                                                     \        nr = strtol(str, &endp, 0);                                     \        if (*endp) {                                                    \                fprintf(stderr, "error: bad %s: %s\n", msg, str);       \                return CMD_HELP;                                        \        }                                                               \} while (0)#define ADD_OVERFLOW(a,b) ((a + b) < a) ? (a = ULONG_MAX) : (a = a + b)/* Convert format time string "XXwXXdXXhXXmXXs" into seconds value * returns the value or ULONG_MAX on integer overflow or incorrect format * Notes: *        1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w) *        2. specifiers may be encountered multiple times (2s3s is 5 seconds) *        3. empty integer value is interpreted as 0 */static unsigned long str2sec(const char* timestr) {        const char spec[] = "smhdw";        const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};        unsigned long val = 0;        char *tail;        if (strpbrk(timestr, spec) == NULL) {                /* no specifiers inside the time string,                   should treat it as an integer value */                val = strtoul(timestr, &tail, 10);                return *tail ? ULONG_MAX : val;        }        /* format string is XXwXXdXXhXXmXXs */        while (*timestr) {                unsigned long v;                int ind;                char* ptr;                v = strtoul(timestr, &tail, 10);                if (v == ULONG_MAX || *tail == '\0')                        /* value too large (ULONG_MAX or more)                            or missing specifier */                        goto error;                ptr = strchr(spec, *tail);                if (ptr == NULL)                        /* unknown specifier */                        goto error;                ind = ptr - spec;                /* check if product will overflow the type */                if (!(v < ULONG_MAX / mult[ind]))                        goto error;                ADD_OVERFLOW(val, mult[ind] * v);                if (val == ULONG_MAX)                        goto error;                timestr = tail + 1;        }        return val;error:        return ULONG_MAX;}#define ARG2ULL(nr, str, msg)                                           \do {                                                                    \        char *endp;                                                     \        nr = strtoull(str, &endp, 0);                                   \        if (*endp) {                                                    \                fprintf(stderr, "error: bad %s: %s\n", msg, str);       \                return CMD_HELP;                                        \        }                                                               \} while (0)int lfs_setquota(int argc, char **argv){        int c;        char *mnt;        struct if_quotactl qctl;        char *obd_type = (char *)qctl.obd_type;        int rc;        memset(&qctl, 0, sizeof(qctl));        qctl.qc_cmd = LUSTRE_Q_SETQUOTA;        optind = 0;        while ((c = getopt(argc, argv, "ugt")) != -1) {                switch (c) {                case 'u':                        qctl.qc_type |= 0x01;                        break;                case 'g':                        qctl.qc_type |= 0x02;                        break;                case 't':                        qctl.qc_cmd = LUSTRE_Q_SETINFO;                        break;                default:                        fprintf(stderr, "error: %s: option '-%c' "                                        "unrecognized\n", argv[0], c);                        return CMD_HELP;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?