Home | History | Annotate | Line # | Download | only in libquota
quota_open.c revision 1.5
      1 /*	$NetBSD: quota_open.c,v 1.5 2012/01/25 17:43:37 dholland Exp $	*/
      2 /*-
      3  * Copyright (c) 2011 The NetBSD Foundation, Inc.
      4  * All rights reserved.
      5  *
      6  * This code is derived from software contributed to The NetBSD Foundation
      7  * by David A. Holland.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     19  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     20  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     21  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     28  * POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 #include <sys/cdefs.h>
     32 __RCSID("$NetBSD: quota_open.c,v 1.5 2012/01/25 17:43:37 dholland Exp $");
     33 
     34 #include <sys/types.h>
     35 #include <sys/statvfs.h>
     36 #include <stdlib.h>
     37 #include <string.h>
     38 #include <unistd.h>
     39 #include <errno.h>
     40 
     41 #include <quota.h>
     42 #include "quotapvt.h"
     43 
     44 struct quotahandle *
     45 quota_open(const char *path)
     46 {
     47 
     48 	struct statvfs stv;
     49 	struct quotahandle *qh;
     50 	int mode;
     51 	int serrno;
     52 
     53 	/*
     54 	 * Probe order:
     55 	 *
     56 	 *    1. Check for NFS. NFS quota ops don't go to the kernel
     57 	 *    at all but instead do RPCs to the NFS server's
     58 	 *    rpc.rquotad, so it doesn't matter what the kernel
     59 	 *    thinks.
     60 	 *
     61 	 *    2. Check if quotas are enabled in the mount flags. If
     62 	 *    so, we can do quotactl calls.
     63 	 *
     64 	 *    3. Check if the volume is listed in fstab as one of
     65 	 *    the filesystem types supported by quota_oldfiles.c,
     66 	 *    and with the proper mount options to enable quotas.
     67 	 */
     68 
     69 	if (statvfs(path, &stv) < 0) {
     70 		return NULL;
     71 	}
     72 
     73 	__quota_oldfiles_load_fstab();
     74 
     75 	if (!strcmp(stv.f_fstypename, "nfs")) {
     76 		mode = QUOTA_MODE_NFS;
     77 	} else if ((stv.f_flag & ST_QUOTA) != 0) {
     78 		mode = QUOTA_MODE_PROPLIB;
     79 	} else if (__quota_oldfiles_infstab(stv.f_mntonname)) {
     80 		mode = QUOTA_MODE_OLDFILES;
     81 	} else {
     82 		errno = EOPNOTSUPP;
     83 		return NULL;
     84 	}
     85 
     86 	qh = malloc(sizeof(*qh));
     87 	if (qh == NULL) {
     88 		return NULL;
     89 	}
     90 
     91 	/*
     92 	 * Get the mount point from statvfs; this way the passed-in
     93 	 * path can be any path on the volume.
     94 	 */
     95 
     96 	qh->qh_mountpoint = strdup(stv.f_mntonname);
     97 	if (qh->qh_mountpoint == NULL) {
     98 		serrno = errno;
     99 		free(qh);
    100 		errno = serrno;
    101 		return NULL;
    102 	}
    103 
    104 	qh->qh_mountdevice = strdup(stv.f_mntfromname);
    105 	if (qh->qh_mountdevice == NULL) {
    106 		serrno = errno;
    107 		free(qh->qh_mountpoint);
    108 		free(qh);
    109 		errno = serrno;
    110 		return NULL;
    111 	}
    112 
    113 	qh->qh_mode = mode;
    114 
    115 	qh->qh_oldfilesopen = 0;
    116 	qh->qh_userfile = -1;
    117 	qh->qh_groupfile = -1;
    118 
    119 	return qh;
    120 }
    121 
    122 const char *
    123 quota_getmountpoint(struct quotahandle *qh)
    124 {
    125 	return qh->qh_mountpoint;
    126 }
    127 
    128 const char *
    129 quota_getmountdevice(struct quotahandle *qh)
    130 {
    131 	return qh->qh_mountdevice;
    132 }
    133 
    134 void
    135 quota_close(struct quotahandle *qh)
    136 {
    137 	if (qh->qh_userfile >= 0) {
    138 		close(qh->qh_userfile);
    139 	}
    140 	if (qh->qh_groupfile >= 0) {
    141 		close(qh->qh_groupfile);
    142 	}
    143 	free(qh->qh_mountdevice);
    144 	free(qh->qh_mountpoint);
    145 	free(qh);
    146 }
    147