Whamcloud - gitweb
3c871b3f2fd658160b3da1db43deef4d38dfdc69
[tools/e2fsprogs.git] / lib / support / quotaio.h
1 /** quotaio.h
2  *
3  * Interface to the quota library.
4  *
5  * The quota library provides interface for creating and updating the quota
6  * files and the ext4 superblock fields. It supports the new VFS_V1 quota
7  * format. The quota library also provides support for keeping track of quotas
8  * in memory.
9  * The typical way to use the quota library is as follows:
10  * {
11  *      quota_ctx_t qctx;
12  *
13  *      quota_init_context(&qctx, fs, QUOTA_ALL_BIT);
14  *      {
15  *              quota_compute_usage(qctx, QUOTA_ALL_BIT);
16  *              AND/OR
17  *              quota_data_add/quota_data_sub/quota_data_inodes();
18  *      }
19  *      quota_write_inode(qctx, USRQUOTA);
20  *      quota_write_inode(qctx, GRPQUOTA);
21  *      quota_release_context(&qctx);
22  * }
23  *
24  * This initial version does not support reading the quota files. This support
25  * will be added in near future.
26  *
27  * Aditya Kali <adityakali@google.com>
28  * Header of IO operations for quota utilities
29  *
30  * Jan Kara <jack@suse.cz>
31  */
32
33 #ifndef GUARD_QUOTAIO_H
34 #define GUARD_QUOTAIO_H
35
36 #include <limits.h>
37 #include <sys/types.h>
38 #include <sys/stat.h>
39
40 #include "ext2fs/ext2_fs.h"
41 #include "ext2fs/ext2fs.h"
42 #include "dqblk_v2.h"
43
44 typedef int64_t qsize_t;        /* Type in which we store size limitations */
45
46 enum quota_type {
47         USRQUOTA = 0,
48         GRPQUOTA = 1,
49         MAXQUOTAS = 2,
50 };
51
52 #if MAXQUOTAS > 32
53 #error "cannot have more than 32 quota types to fit in qtype_bits"
54 #endif
55
56 #define QUOTA_USR_BIT (1 << USRQUOTA)
57 #define QUOTA_GRP_BIT (1 << GRPQUOTA)
58 #define QUOTA_ALL_BIT (QUOTA_USR_BIT | QUOTA_GRP_BIT)
59
60 typedef struct quota_ctx *quota_ctx_t;
61 struct dict_t;
62
63 struct quota_ctx {
64         ext2_filsys     fs;
65         struct dict_t   *quota_dict[MAXQUOTAS];
66         struct quota_handle *quota_file[MAXQUOTAS];
67 };
68
69 /*
70  * Definitions of magics and versions of current quota files
71  */
72 #define INITQMAGICS {\
73         0xd9c01f11,     /* USRQUOTA */\
74         0xd9c01927      /* GRPQUOTA */\
75 }
76
77 /* Size of blocks in which are counted size limits in generic utility parts */
78 #define QUOTABLOCK_BITS 10
79 #define QUOTABLOCK_SIZE (1 << QUOTABLOCK_BITS)
80 #define toqb(x) (((x) + QUOTABLOCK_SIZE - 1) >> QUOTABLOCK_BITS)
81
82 /* Quota format type IDs */
83 #define QFMT_VFS_OLD 1
84 #define QFMT_VFS_V0 2
85 #define QFMT_VFS_V1 4
86
87 /*
88  * The following constants define the default amount of time given a user
89  * before the soft limits are treated as hard limits (usually resulting
90  * in an allocation failure). The timer is started when the user crosses
91  * their soft limit, it is reset when they go below their soft limit.
92  */
93 #define MAX_IQ_TIME  604800     /* (7*24*60*60) 1 week */
94 #define MAX_DQ_TIME  604800     /* (7*24*60*60) 1 week */
95
96 #define IOFL_INFODIRTY  0x01    /* Did info change? */
97
98 struct quotafile_ops;
99
100 /* Generic information about quotafile */
101 struct util_dqinfo {
102         time_t dqi_bgrace;      /* Block grace time for given quotafile */
103         time_t dqi_igrace;      /* Inode grace time for given quotafile */
104         union {
105                 struct v2_mem_dqinfo v2_mdqi;
106         } u;                    /* Format specific info about quotafile */
107 };
108
109 struct quota_file {
110         ext2_filsys fs;
111         ext2_ino_t ino;
112         ext2_file_t e2_file;
113 };
114
115 /* Structure for one opened quota file */
116 struct quota_handle {
117         enum quota_type qh_type;        /* Type of quotafile */
118         int qh_fmt;             /* Quotafile format */
119         int qh_file_flags;
120         int qh_io_flags;        /* IO flags for file */
121         struct quota_file qh_qf;
122         unsigned int (*e2fs_read)(struct quota_file *qf, ext2_loff_t offset,
123                          void *buf, unsigned int size);
124         unsigned int (*e2fs_write)(struct quota_file *qf, ext2_loff_t offset,
125                           void *buf, unsigned int size);
126         struct quotafile_ops *qh_ops;   /* Operations on quotafile */
127         struct util_dqinfo qh_info;     /* Generic quotafile info */
128 };
129
130 /* Utility quota block */
131 struct util_dqblk {
132         qsize_t dqb_ihardlimit;
133         qsize_t dqb_isoftlimit;
134         qsize_t dqb_curinodes;
135         qsize_t dqb_bhardlimit;
136         qsize_t dqb_bsoftlimit;
137         qsize_t dqb_curspace;
138         time_t dqb_btime;
139         time_t dqb_itime;
140         union {
141                 struct v2_mem_dqblk v2_mdqb;
142         } u;                    /* Format specific dquot information */
143 };
144
145 /* Structure for one loaded quota */
146 struct dquot {
147         struct dquot *dq_next;  /* Pointer to next dquot in the list */
148         qid_t dq_id;            /* ID dquot belongs to */
149         int dq_flags;           /* Some flags for utils */
150         struct quota_handle *dq_h;      /* Handle of quotafile for this dquot */
151         struct util_dqblk dq_dqb;       /* Parsed data of dquot */
152 };
153
154 #define DQF_SEEN        0x0001
155
156 /* Structure of quotafile operations */
157 struct quotafile_ops {
158         /* Check whether quotafile is in our format */
159         int (*check_file) (struct quota_handle *h, int type, int fmt);
160         /* Open quotafile */
161         int (*init_io) (struct quota_handle *h);
162         /* Create new quotafile */
163         int (*new_io) (struct quota_handle *h);
164         /* Write all changes and close quotafile */
165         int (*end_io) (struct quota_handle *h);
166         /* Write info about quotafile */
167         int (*write_info) (struct quota_handle *h);
168         /* Read dquot into memory */
169         struct dquot *(*read_dquot) (struct quota_handle *h, qid_t id);
170         /* Write given dquot to disk */
171         int (*commit_dquot) (struct dquot *dquot);
172         /* Scan quotafile and call callback on every structure */
173         int (*scan_dquots) (struct quota_handle *h,
174                             int (*process_dquot) (struct dquot *dquot,
175                                                   void *data),
176                             void *data);
177         /* Function to print format specific file information */
178         int (*report) (struct quota_handle *h, int verbose);
179 };
180
181 /* This might go into a special header file but that sounds a bit silly... */
182 extern struct quotafile_ops quotafile_ops_meta;
183
184 /* Open existing quotafile of given type (and verify its format) on given
185  * filesystem. */
186 errcode_t quota_file_open(quota_ctx_t qctx, struct quota_handle *h,
187                           ext2_ino_t qf_ino, enum quota_type type,
188                           int fmt, int flags);
189
190
191 /* Create new quotafile of specified format on given filesystem */
192 errcode_t quota_file_create(struct quota_handle *h, ext2_filsys fs,
193                             enum quota_type qtype, int fmt);
194
195 /* Close quotafile */
196 errcode_t quota_file_close(quota_ctx_t qctx, struct quota_handle *h);
197
198 /* Get empty quota structure */
199 struct dquot *get_empty_dquot(void);
200
201 errcode_t quota_inode_truncate(ext2_filsys fs, ext2_ino_t ino);
202
203 const char *quota_type2name(enum quota_type qtype);
204 ext2_ino_t quota_type2inum(enum quota_type qtype, struct ext2_super_block *);
205
206 void update_grace_times(struct dquot *q);
207
208 /* size for the buffer returned by quota_get_qf_name(); must be greater
209    than maxlen of extensions[] and fmtnames[] (plus 2) found in quotaio.c */
210 #define QUOTA_NAME_LEN 16
211
212 const char *quota_get_qf_name(enum quota_type, int fmt, char *buf);
213
214 /* In mkquota.c */
215 errcode_t quota_init_context(quota_ctx_t *qctx, ext2_filsys fs,
216                              unsigned int qtype_bits);
217 void quota_data_inodes(quota_ctx_t qctx, struct ext2_inode *inode, ext2_ino_t ino,
218                 int adjust);
219 void quota_data_add(quota_ctx_t qctx, struct ext2_inode *inode, ext2_ino_t ino,
220                 qsize_t space);
221 void quota_data_sub(quota_ctx_t qctx, struct ext2_inode *inode, ext2_ino_t ino,
222                 qsize_t space);
223 errcode_t quota_write_inode(quota_ctx_t qctx, enum quota_type qtype);
224 errcode_t quota_update_limits(quota_ctx_t qctx, ext2_ino_t qf_ino,
225                               enum quota_type type);
226 errcode_t quota_compute_usage(quota_ctx_t qctx);
227 void quota_release_context(quota_ctx_t *qctx);
228 errcode_t quota_remove_inode(ext2_filsys fs, enum quota_type qtype);
229 int quota_file_exists(ext2_filsys fs, enum quota_type qtype);
230 void quota_set_sb_inum(ext2_filsys fs, ext2_ino_t ino, enum quota_type qtype);
231 errcode_t quota_compare_and_update(quota_ctx_t qctx, enum quota_type qtype,
232                                    int *usage_inconsistent);
233 int parse_quota_opts(const char *opts, int (*func)(), void *data);
234
235 /*
236  * Return pointer to reserved inode field in superblock for given quota type.
237  *
238  * This allows the caller to get or set the quota inode by type without the
239  * need for the quota array to be contiguous in the superbock.
240  */
241 static inline ext2_ino_t *quota_sb_inump(struct ext2_super_block *sb,
242                                          enum quota_type qtype)
243 {
244         switch (qtype) {
245         case USRQUOTA:
246                 return &sb->s_usr_quota_inum;
247         case GRPQUOTA:
248                 return &sb->s_grp_quota_inum;
249         default:
250                 return NULL;
251         }
252
253         return NULL;
254 }
255
256 #endif /* GUARD_QUOTAIO_H */