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