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