Initial commit to Gerrit
[profile/ivi/quota.git] / quotaio_generic.c
1 /*
2  *      Implementation of communication with kernel generic interface
3  *
4  *      Jan Kara <jack@suse.cz> - sponsored by SuSE CR
5  */
6
7 #include "config.h"
8
9 #include <errno.h>
10 #include <string.h>
11 #include <sys/types.h>
12
13 #include "pot.h"
14 #include "common.h"
15 #include "quotaio.h"
16 #include "quota.h"
17 #include "quotasys.h"
18
19 /* Convert kernel quotablock format to utility one */
20 static inline void generic_kern2utildqblk(struct util_dqblk *u, struct if_dqblk *k)
21 {
22         u->dqb_ihardlimit = k->dqb_ihardlimit;
23         u->dqb_isoftlimit = k->dqb_isoftlimit;
24         u->dqb_bhardlimit = k->dqb_bhardlimit;
25         u->dqb_bsoftlimit = k->dqb_bsoftlimit;
26         u->dqb_curinodes = k->dqb_curinodes;
27         u->dqb_curspace = k->dqb_curspace;
28         u->dqb_itime = k->dqb_itime;
29         u->dqb_btime = k->dqb_btime;
30 }
31
32 /* Convert utility quotablock format to kernel one */
33 static inline void generic_util2kerndqblk(struct if_dqblk *k, struct util_dqblk *u)
34 {
35         k->dqb_ihardlimit = u->dqb_ihardlimit;
36         k->dqb_isoftlimit = u->dqb_isoftlimit;
37         k->dqb_bhardlimit = u->dqb_bhardlimit;
38         k->dqb_bsoftlimit = u->dqb_bsoftlimit;
39         k->dqb_curinodes = u->dqb_curinodes;
40         k->dqb_curspace = u->dqb_curspace;
41         k->dqb_itime = u->dqb_itime;
42         k->dqb_btime = u->dqb_btime;
43 }
44
45 /* Get info from kernel to handle */
46 int vfs_get_info(struct quota_handle *h)
47 {
48         struct if_dqinfo kinfo;
49
50         if (quotactl(QCMD(Q_GETINFO, h->qh_type), h->qh_quotadev, 0, (void *)&kinfo) < 0) {
51                 errstr(_("Cannot get info for %s quota file from kernel on %s: %s\n"), type2name(h->qh_type), h->qh_quotadev, strerror(errno));
52                 return -1;
53         }
54         h->qh_info.dqi_bgrace = kinfo.dqi_bgrace;
55         h->qh_info.dqi_igrace = kinfo.dqi_igrace;
56         return 0;
57 }
58
59 /* Set info in kernel from handle */
60 int vfs_set_info(struct quota_handle *h, int flags)
61 {
62         struct if_dqinfo kinfo;
63
64         kinfo.dqi_bgrace = h->qh_info.dqi_bgrace;
65         kinfo.dqi_igrace = h->qh_info.dqi_igrace;
66         kinfo.dqi_valid = flags;
67
68         if (quotactl(QCMD(Q_SETINFO, h->qh_type), h->qh_quotadev, 0, (void *)&kinfo) < 0) {
69                 errstr(_("Cannot set info for %s quota file from kernel on %s: %s\n"), type2name(h->qh_type), h->qh_quotadev, strerror(errno));
70                 return -1;
71         }
72         return 0;
73 }
74
75 /* Get dquot from kernel */
76 int vfs_get_dquot(struct dquot *dquot)
77 {
78         struct if_dqblk kdqblk;
79
80         if (quotactl(QCMD(Q_GETQUOTA, dquot->dq_h->qh_type), dquot->dq_h->qh_quotadev, dquot->dq_id, (void *)&kdqblk) < 0) {
81                 errstr(_("Cannot get quota for %s %d from kernel on %s: %s\n"), type2name(dquot->dq_h->qh_type), dquot->dq_id, dquot->dq_h->qh_quotadev, strerror(errno));
82                 return -1;
83         }
84         generic_kern2utildqblk(&dquot->dq_dqb, &kdqblk);
85         return 0;
86 }
87
88 /* Set dquot in kernel */
89 int vfs_set_dquot(struct dquot *dquot, int flags)
90 {
91         struct if_dqblk kdqblk;
92
93         generic_util2kerndqblk(&kdqblk, &dquot->dq_dqb);
94         kdqblk.dqb_valid = flags;
95         if (quotactl(QCMD(Q_SETQUOTA, dquot->dq_h->qh_type), dquot->dq_h->qh_quotadev, dquot->dq_id, (void *)&kdqblk) < 0) {
96                 errstr(_("Cannot set quota for %s %d from kernel on %s: %s\n"), type2name(dquot->dq_h->qh_type), dquot->dq_id, dquot->dq_h->qh_quotadev, strerror(errno));
97                 return -1;
98         }
99         return 0;
100 }