1*59bfda1fSAndroid Build Coastguard Worker /** quotaio.h
2*59bfda1fSAndroid Build Coastguard Worker *
3*59bfda1fSAndroid Build Coastguard Worker * Interface to the quota library.
4*59bfda1fSAndroid Build Coastguard Worker *
5*59bfda1fSAndroid Build Coastguard Worker * The quota library provides interface for creating and updating the quota
6*59bfda1fSAndroid Build Coastguard Worker * files and the ext4 superblock fields. It supports the new VFS_V1 quota
7*59bfda1fSAndroid Build Coastguard Worker * format. The quota library also provides support for keeping track of quotas
8*59bfda1fSAndroid Build Coastguard Worker * in memory.
9*59bfda1fSAndroid Build Coastguard Worker *
10*59bfda1fSAndroid Build Coastguard Worker * Aditya Kali <[email protected]>
11*59bfda1fSAndroid Build Coastguard Worker * Header of IO operations for quota utilities
12*59bfda1fSAndroid Build Coastguard Worker *
13*59bfda1fSAndroid Build Coastguard Worker * Jan Kara <[email protected]>
14*59bfda1fSAndroid Build Coastguard Worker *
15*59bfda1fSAndroid Build Coastguard Worker * Hyojun Kim <[email protected]> - Ported to f2fs-tools
16*59bfda1fSAndroid Build Coastguard Worker */
17*59bfda1fSAndroid Build Coastguard Worker
18*59bfda1fSAndroid Build Coastguard Worker #ifndef GUARD_QUOTAIO_H
19*59bfda1fSAndroid Build Coastguard Worker #define GUARD_QUOTAIO_H
20*59bfda1fSAndroid Build Coastguard Worker
21*59bfda1fSAndroid Build Coastguard Worker #include <limits.h>
22*59bfda1fSAndroid Build Coastguard Worker #include <sys/types.h>
23*59bfda1fSAndroid Build Coastguard Worker #include <sys/stat.h>
24*59bfda1fSAndroid Build Coastguard Worker
25*59bfda1fSAndroid Build Coastguard Worker #include "dict.h"
26*59bfda1fSAndroid Build Coastguard Worker #include "f2fs_fs.h"
27*59bfda1fSAndroid Build Coastguard Worker #include "f2fs.h"
28*59bfda1fSAndroid Build Coastguard Worker #include "node.h"
29*59bfda1fSAndroid Build Coastguard Worker #include "fsck.h"
30*59bfda1fSAndroid Build Coastguard Worker
31*59bfda1fSAndroid Build Coastguard Worker #include "dqblk_v2.h"
32*59bfda1fSAndroid Build Coastguard Worker
33*59bfda1fSAndroid Build Coastguard Worker typedef int64_t qsize_t; /* Type in which we store size limitations */
34*59bfda1fSAndroid Build Coastguard Worker typedef int32_t f2fs_ino_t;
35*59bfda1fSAndroid Build Coastguard Worker typedef int errcode_t;
36*59bfda1fSAndroid Build Coastguard Worker
37*59bfda1fSAndroid Build Coastguard Worker enum quota_type {
38*59bfda1fSAndroid Build Coastguard Worker USRQUOTA = 0,
39*59bfda1fSAndroid Build Coastguard Worker GRPQUOTA = 1,
40*59bfda1fSAndroid Build Coastguard Worker PRJQUOTA = 2,
41*59bfda1fSAndroid Build Coastguard Worker MAXQUOTAS = 3,
42*59bfda1fSAndroid Build Coastguard Worker };
43*59bfda1fSAndroid Build Coastguard Worker
44*59bfda1fSAndroid Build Coastguard Worker #if MAXQUOTAS > 32
45*59bfda1fSAndroid Build Coastguard Worker #error "cannot have more than 32 quota types to fit in qtype_bits"
46*59bfda1fSAndroid Build Coastguard Worker #endif
47*59bfda1fSAndroid Build Coastguard Worker
48*59bfda1fSAndroid Build Coastguard Worker enum qf_szchk_type_t {
49*59bfda1fSAndroid Build Coastguard Worker QF_SZCHK_NONE,
50*59bfda1fSAndroid Build Coastguard Worker QF_SZCHK_ERR,
51*59bfda1fSAndroid Build Coastguard Worker QF_SZCHK_INLINE,
52*59bfda1fSAndroid Build Coastguard Worker QF_SZCHK_REGFILE,
53*59bfda1fSAndroid Build Coastguard Worker };
54*59bfda1fSAndroid Build Coastguard Worker
55*59bfda1fSAndroid Build Coastguard Worker extern int cur_qtype;
56*59bfda1fSAndroid Build Coastguard Worker extern u32 qf_last_blkofs[];
57*59bfda1fSAndroid Build Coastguard Worker extern enum qf_szchk_type_t qf_szchk_type[];
58*59bfda1fSAndroid Build Coastguard Worker extern u64 qf_maxsize[];
59*59bfda1fSAndroid Build Coastguard Worker
60*59bfda1fSAndroid Build Coastguard Worker #define QUOTA_USR_BIT (1 << USRQUOTA)
61*59bfda1fSAndroid Build Coastguard Worker #define QUOTA_GRP_BIT (1 << GRPQUOTA)
62*59bfda1fSAndroid Build Coastguard Worker #define QUOTA_PRJ_BIT (1 << PRJQUOTA)
63*59bfda1fSAndroid Build Coastguard Worker #define QUOTA_ALL_BIT (QUOTA_USR_BIT | QUOTA_GRP_BIT | QUOTA_PRJ_BIT)
64*59bfda1fSAndroid Build Coastguard Worker
65*59bfda1fSAndroid Build Coastguard Worker typedef struct quota_ctx *quota_ctx_t;
66*59bfda1fSAndroid Build Coastguard Worker
67*59bfda1fSAndroid Build Coastguard Worker struct quota_ctx {
68*59bfda1fSAndroid Build Coastguard Worker struct f2fs_sb_info *sbi;
69*59bfda1fSAndroid Build Coastguard Worker struct dict_t *quota_dict[MAXQUOTAS];
70*59bfda1fSAndroid Build Coastguard Worker struct quota_handle *quota_file[MAXQUOTAS];
71*59bfda1fSAndroid Build Coastguard Worker struct dict_t linked_inode_dict;
72*59bfda1fSAndroid Build Coastguard Worker };
73*59bfda1fSAndroid Build Coastguard Worker
74*59bfda1fSAndroid Build Coastguard Worker /*
75*59bfda1fSAndroid Build Coastguard Worker * Definitions of magics and versions of current quota files
76*59bfda1fSAndroid Build Coastguard Worker */
77*59bfda1fSAndroid Build Coastguard Worker #define INITQMAGICS {\
78*59bfda1fSAndroid Build Coastguard Worker 0xd9c01f11, /* USRQUOTA */\
79*59bfda1fSAndroid Build Coastguard Worker 0xd9c01927, /* GRPQUOTA */\
80*59bfda1fSAndroid Build Coastguard Worker 0xd9c03f14 /* PRJQUOTA */\
81*59bfda1fSAndroid Build Coastguard Worker }
82*59bfda1fSAndroid Build Coastguard Worker
83*59bfda1fSAndroid Build Coastguard Worker /* Size of blocks in which are counted size limits in generic utility parts */
84*59bfda1fSAndroid Build Coastguard Worker #define QUOTABLOCK_BITS 10
85*59bfda1fSAndroid Build Coastguard Worker #define QUOTABLOCK_SIZE (1 << QUOTABLOCK_BITS)
86*59bfda1fSAndroid Build Coastguard Worker #define toqb(x) (((x) + QUOTABLOCK_SIZE - 1) >> QUOTABLOCK_BITS)
87*59bfda1fSAndroid Build Coastguard Worker
88*59bfda1fSAndroid Build Coastguard Worker /* Quota format type IDs */
89*59bfda1fSAndroid Build Coastguard Worker #define QFMT_VFS_OLD 1
90*59bfda1fSAndroid Build Coastguard Worker #define QFMT_VFS_V0 2
91*59bfda1fSAndroid Build Coastguard Worker #define QFMT_VFS_V1 4
92*59bfda1fSAndroid Build Coastguard Worker
93*59bfda1fSAndroid Build Coastguard Worker /*
94*59bfda1fSAndroid Build Coastguard Worker * The following constants define the default amount of time given a user
95*59bfda1fSAndroid Build Coastguard Worker * before the soft limits are treated as hard limits (usually resulting
96*59bfda1fSAndroid Build Coastguard Worker * in an allocation failure). The timer is started when the user crosses
97*59bfda1fSAndroid Build Coastguard Worker * their soft limit, it is reset when they go below their soft limit.
98*59bfda1fSAndroid Build Coastguard Worker */
99*59bfda1fSAndroid Build Coastguard Worker #define MAX_IQ_TIME 604800 /* (7*24*60*60) 1 week */
100*59bfda1fSAndroid Build Coastguard Worker #define MAX_DQ_TIME 604800 /* (7*24*60*60) 1 week */
101*59bfda1fSAndroid Build Coastguard Worker
102*59bfda1fSAndroid Build Coastguard Worker #define IOFL_INFODIRTY 0x01 /* Did info change? */
103*59bfda1fSAndroid Build Coastguard Worker
104*59bfda1fSAndroid Build Coastguard Worker struct quotafile_ops;
105*59bfda1fSAndroid Build Coastguard Worker
106*59bfda1fSAndroid Build Coastguard Worker /* Generic information about quotafile */
107*59bfda1fSAndroid Build Coastguard Worker struct util_dqinfo {
108*59bfda1fSAndroid Build Coastguard Worker time_t dqi_bgrace; /* Block grace time for given quotafile */
109*59bfda1fSAndroid Build Coastguard Worker time_t dqi_igrace; /* Inode grace time for given quotafile */
110*59bfda1fSAndroid Build Coastguard Worker union {
111*59bfda1fSAndroid Build Coastguard Worker struct v2_mem_dqinfo v2_mdqi;
112*59bfda1fSAndroid Build Coastguard Worker } u; /* Format specific info about quotafile */
113*59bfda1fSAndroid Build Coastguard Worker };
114*59bfda1fSAndroid Build Coastguard Worker
115*59bfda1fSAndroid Build Coastguard Worker struct quota_file {
116*59bfda1fSAndroid Build Coastguard Worker struct f2fs_sb_info *sbi;
117*59bfda1fSAndroid Build Coastguard Worker f2fs_ino_t ino;
118*59bfda1fSAndroid Build Coastguard Worker int64_t filesize;
119*59bfda1fSAndroid Build Coastguard Worker };
120*59bfda1fSAndroid Build Coastguard Worker
121*59bfda1fSAndroid Build Coastguard Worker /* Structure for one opened quota file */
122*59bfda1fSAndroid Build Coastguard Worker struct quota_handle {
123*59bfda1fSAndroid Build Coastguard Worker enum quota_type qh_type; /* Type of quotafile */
124*59bfda1fSAndroid Build Coastguard Worker int qh_fmt; /* Quotafile format */
125*59bfda1fSAndroid Build Coastguard Worker int qh_file_flags;
126*59bfda1fSAndroid Build Coastguard Worker int qh_io_flags; /* IO flags for file */
127*59bfda1fSAndroid Build Coastguard Worker struct quota_file qh_qf;
128*59bfda1fSAndroid Build Coastguard Worker unsigned int (*read)(struct quota_file *qf, long offset,
129*59bfda1fSAndroid Build Coastguard Worker void *buf, unsigned int size);
130*59bfda1fSAndroid Build Coastguard Worker unsigned int (*write)(struct quota_file *qf, long offset,
131*59bfda1fSAndroid Build Coastguard Worker void *buf, unsigned int size);
132*59bfda1fSAndroid Build Coastguard Worker struct quotafile_ops *qh_ops; /* Operations on quotafile */
133*59bfda1fSAndroid Build Coastguard Worker struct util_dqinfo qh_info; /* Generic quotafile info */
134*59bfda1fSAndroid Build Coastguard Worker };
135*59bfda1fSAndroid Build Coastguard Worker
136*59bfda1fSAndroid Build Coastguard Worker /* Utility quota block */
137*59bfda1fSAndroid Build Coastguard Worker struct util_dqblk {
138*59bfda1fSAndroid Build Coastguard Worker qsize_t dqb_ihardlimit;
139*59bfda1fSAndroid Build Coastguard Worker qsize_t dqb_isoftlimit;
140*59bfda1fSAndroid Build Coastguard Worker qsize_t dqb_curinodes;
141*59bfda1fSAndroid Build Coastguard Worker qsize_t dqb_bhardlimit;
142*59bfda1fSAndroid Build Coastguard Worker qsize_t dqb_bsoftlimit;
143*59bfda1fSAndroid Build Coastguard Worker qsize_t dqb_curspace;
144*59bfda1fSAndroid Build Coastguard Worker time_t dqb_btime;
145*59bfda1fSAndroid Build Coastguard Worker time_t dqb_itime;
146*59bfda1fSAndroid Build Coastguard Worker union {
147*59bfda1fSAndroid Build Coastguard Worker struct v2_mem_dqblk v2_mdqb;
148*59bfda1fSAndroid Build Coastguard Worker } u; /* Format specific dquot information */
149*59bfda1fSAndroid Build Coastguard Worker };
150*59bfda1fSAndroid Build Coastguard Worker
151*59bfda1fSAndroid Build Coastguard Worker /* Structure for one loaded quota */
152*59bfda1fSAndroid Build Coastguard Worker struct dquot {
153*59bfda1fSAndroid Build Coastguard Worker struct dquot *dq_next; /* Pointer to next dquot in the list */
154*59bfda1fSAndroid Build Coastguard Worker qid_t dq_id; /* ID dquot belongs to */
155*59bfda1fSAndroid Build Coastguard Worker int dq_flags; /* Some flags for utils */
156*59bfda1fSAndroid Build Coastguard Worker struct quota_handle *dq_h; /* Handle of quotafile for this dquot */
157*59bfda1fSAndroid Build Coastguard Worker struct util_dqblk dq_dqb; /* Parsed data of dquot */
158*59bfda1fSAndroid Build Coastguard Worker };
159*59bfda1fSAndroid Build Coastguard Worker
160*59bfda1fSAndroid Build Coastguard Worker #define DQF_SEEN 0x0001
161*59bfda1fSAndroid Build Coastguard Worker
162*59bfda1fSAndroid Build Coastguard Worker /* Structure of quotafile operations */
163*59bfda1fSAndroid Build Coastguard Worker struct quotafile_ops {
164*59bfda1fSAndroid Build Coastguard Worker /* Check whether quotafile is in our format */
165*59bfda1fSAndroid Build Coastguard Worker int (*check_file) (struct quota_handle *h, int type);
166*59bfda1fSAndroid Build Coastguard Worker /* Open quotafile */
167*59bfda1fSAndroid Build Coastguard Worker int (*init_io) (struct quota_handle *h, enum quota_type qtype);
168*59bfda1fSAndroid Build Coastguard Worker /* Create new quotafile */
169*59bfda1fSAndroid Build Coastguard Worker int (*new_io) (struct quota_handle *h);
170*59bfda1fSAndroid Build Coastguard Worker /* Write all changes and close quotafile */
171*59bfda1fSAndroid Build Coastguard Worker int (*end_io) (struct quota_handle *h);
172*59bfda1fSAndroid Build Coastguard Worker /* Write info about quotafile */
173*59bfda1fSAndroid Build Coastguard Worker int (*write_info) (struct quota_handle *h);
174*59bfda1fSAndroid Build Coastguard Worker /* Read dquot into memory */
175*59bfda1fSAndroid Build Coastguard Worker struct dquot *(*read_dquot) (struct quota_handle *h, qid_t id);
176*59bfda1fSAndroid Build Coastguard Worker /* Write given dquot to disk */
177*59bfda1fSAndroid Build Coastguard Worker int (*commit_dquot) (struct dquot *dquot);
178*59bfda1fSAndroid Build Coastguard Worker /* Scan quotafile and call callback on every structure */
179*59bfda1fSAndroid Build Coastguard Worker int (*scan_dquots) (struct quota_handle *h,
180*59bfda1fSAndroid Build Coastguard Worker int (*process_dquot) (struct dquot *dquot,
181*59bfda1fSAndroid Build Coastguard Worker void *data),
182*59bfda1fSAndroid Build Coastguard Worker void *data);
183*59bfda1fSAndroid Build Coastguard Worker /* Function to print format specific file information */
184*59bfda1fSAndroid Build Coastguard Worker int (*report) (struct quota_handle *h, int verbose);
185*59bfda1fSAndroid Build Coastguard Worker };
186*59bfda1fSAndroid Build Coastguard Worker
187*59bfda1fSAndroid Build Coastguard Worker #ifdef __CHECKER__
188*59bfda1fSAndroid Build Coastguard Worker # ifndef __bitwise
189*59bfda1fSAndroid Build Coastguard Worker # define __bitwise __attribute__((bitwise))
190*59bfda1fSAndroid Build Coastguard Worker # endif
191*59bfda1fSAndroid Build Coastguard Worker #define __force __attribute__((force))
192*59bfda1fSAndroid Build Coastguard Worker #else
193*59bfda1fSAndroid Build Coastguard Worker # ifndef __bitwise
194*59bfda1fSAndroid Build Coastguard Worker # define __bitwise
195*59bfda1fSAndroid Build Coastguard Worker # endif
196*59bfda1fSAndroid Build Coastguard Worker #define __force
197*59bfda1fSAndroid Build Coastguard Worker #endif
198*59bfda1fSAndroid Build Coastguard Worker
199*59bfda1fSAndroid Build Coastguard Worker /* Open existing quotafile of given type (and verify its format) on given
200*59bfda1fSAndroid Build Coastguard Worker * filesystem. */
201*59bfda1fSAndroid Build Coastguard Worker errcode_t quota_file_open(struct f2fs_sb_info *sbi, struct quota_handle *h,
202*59bfda1fSAndroid Build Coastguard Worker enum quota_type qtype, int flags);
203*59bfda1fSAndroid Build Coastguard Worker
204*59bfda1fSAndroid Build Coastguard Worker /* Create new quotafile of specified format on given filesystem */
205*59bfda1fSAndroid Build Coastguard Worker errcode_t quota_file_create(struct f2fs_sb_info *sbi, struct quota_handle *h,
206*59bfda1fSAndroid Build Coastguard Worker enum quota_type qtype);
207*59bfda1fSAndroid Build Coastguard Worker
208*59bfda1fSAndroid Build Coastguard Worker /* Close quotafile */
209*59bfda1fSAndroid Build Coastguard Worker errcode_t quota_file_close(struct f2fs_sb_info *sbi, struct quota_handle *h,
210*59bfda1fSAndroid Build Coastguard Worker int update_filesize);
211*59bfda1fSAndroid Build Coastguard Worker
212*59bfda1fSAndroid Build Coastguard Worker /* Get empty quota structure */
213*59bfda1fSAndroid Build Coastguard Worker struct dquot *get_empty_dquot(void);
214*59bfda1fSAndroid Build Coastguard Worker const char *quota_type2name(enum quota_type qtype);
215*59bfda1fSAndroid Build Coastguard Worker void update_grace_times(struct dquot *q);
216*59bfda1fSAndroid Build Coastguard Worker
217*59bfda1fSAndroid Build Coastguard Worker /* In mkquota.c */
218*59bfda1fSAndroid Build Coastguard Worker errcode_t quota_init_context(struct f2fs_sb_info *sbi);
219*59bfda1fSAndroid Build Coastguard Worker void quota_data_inodes(quota_ctx_t qctx, struct f2fs_inode *inode, int adjust);
220*59bfda1fSAndroid Build Coastguard Worker void quota_data_add(quota_ctx_t qctx, struct f2fs_inode *inode, qsize_t space);
221*59bfda1fSAndroid Build Coastguard Worker void quota_data_sub(quota_ctx_t qctx, struct f2fs_inode *inode, qsize_t space);
222*59bfda1fSAndroid Build Coastguard Worker errcode_t quota_write_inode(struct f2fs_sb_info *sbi, enum quota_type qtype);
223*59bfda1fSAndroid Build Coastguard Worker void quota_add_inode_usage(quota_ctx_t qctx, f2fs_ino_t ino,
224*59bfda1fSAndroid Build Coastguard Worker struct f2fs_inode* inode);
225*59bfda1fSAndroid Build Coastguard Worker void quota_release_context(quota_ctx_t *qctx);
226*59bfda1fSAndroid Build Coastguard Worker errcode_t quota_compare_and_update(struct f2fs_sb_info *sbi,
227*59bfda1fSAndroid Build Coastguard Worker enum quota_type qtype, int *usage_inconsistent,
228*59bfda1fSAndroid Build Coastguard Worker int preserve_limits);
229*59bfda1fSAndroid Build Coastguard Worker
quota_get_mem(unsigned long size,void * ptr)230*59bfda1fSAndroid Build Coastguard Worker static inline errcode_t quota_get_mem(unsigned long size, void *ptr)
231*59bfda1fSAndroid Build Coastguard Worker {
232*59bfda1fSAndroid Build Coastguard Worker void *pp;
233*59bfda1fSAndroid Build Coastguard Worker
234*59bfda1fSAndroid Build Coastguard Worker pp = malloc(size);
235*59bfda1fSAndroid Build Coastguard Worker if (!pp)
236*59bfda1fSAndroid Build Coastguard Worker return -1;
237*59bfda1fSAndroid Build Coastguard Worker memcpy(ptr, &pp, sizeof (pp));
238*59bfda1fSAndroid Build Coastguard Worker return 0;
239*59bfda1fSAndroid Build Coastguard Worker }
240*59bfda1fSAndroid Build Coastguard Worker
quota_get_memzero(unsigned long size,void * ptr)241*59bfda1fSAndroid Build Coastguard Worker static inline errcode_t quota_get_memzero(unsigned long size, void *ptr)
242*59bfda1fSAndroid Build Coastguard Worker {
243*59bfda1fSAndroid Build Coastguard Worker void *pp;
244*59bfda1fSAndroid Build Coastguard Worker
245*59bfda1fSAndroid Build Coastguard Worker pp = malloc(size);
246*59bfda1fSAndroid Build Coastguard Worker if (!pp)
247*59bfda1fSAndroid Build Coastguard Worker return -1;
248*59bfda1fSAndroid Build Coastguard Worker memset(pp, 0, size);
249*59bfda1fSAndroid Build Coastguard Worker memcpy(ptr, &pp, sizeof(pp));
250*59bfda1fSAndroid Build Coastguard Worker return 0;
251*59bfda1fSAndroid Build Coastguard Worker }
252*59bfda1fSAndroid Build Coastguard Worker
quota_free_mem(void * ptr)253*59bfda1fSAndroid Build Coastguard Worker static inline errcode_t quota_free_mem(void *ptr)
254*59bfda1fSAndroid Build Coastguard Worker {
255*59bfda1fSAndroid Build Coastguard Worker void *p;
256*59bfda1fSAndroid Build Coastguard Worker
257*59bfda1fSAndroid Build Coastguard Worker memcpy(&p, ptr, sizeof(p));
258*59bfda1fSAndroid Build Coastguard Worker free(p);
259*59bfda1fSAndroid Build Coastguard Worker p = 0;
260*59bfda1fSAndroid Build Coastguard Worker memcpy(ptr, &p, sizeof(p));
261*59bfda1fSAndroid Build Coastguard Worker return 0;
262*59bfda1fSAndroid Build Coastguard Worker }
263*59bfda1fSAndroid Build Coastguard Worker
264*59bfda1fSAndroid Build Coastguard Worker #endif /* GUARD_QUOTAIO_H */
265