• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /** quotaio.c
2  *
3  * Generic IO operations on quotafiles
4  * Jan Kara <jack@suse.cz> - sponsored by SuSE CR
5  * Aditya Kali <adityakali@google.com> - Ported to e2fsprogs
6  * Hyojun Kim <hyojun@google.com> - Ported to f2fs-tools
7  */
8 
9 #include "config.h"
10 #include <stdio.h>
11 #include <errno.h>
12 #include <string.h>
13 #include <unistd.h>
14 #include <stdlib.h>
15 #include <time.h>
16 #include <sys/types.h>
17 #include <sys/stat.h>
18 #include <sys/file.h>
19 #include <assert.h>
20 
21 #include "common.h"
22 #include "quotaio.h"
23 
24 static const char * const extensions[MAXQUOTAS] = {
25 	[USRQUOTA] = "user",
26 	[GRPQUOTA] = "group",
27 	[PRJQUOTA] = "project",
28 };
29 
30 /* Header in all newer quotafiles */
31 struct disk_dqheader {
32 	__le32 dqh_magic;
33 	__le32 dqh_version;
34 } __attribute__ ((packed));
35 
36 int cur_qtype = -1;
37 u32 qf_last_blkofs[MAXQUOTAS] = {0, 0, 0};
38 enum qf_szchk_type_t qf_szchk_type[MAXQUOTAS] =
39 {
40 	QF_SZCHK_NONE, QF_SZCHK_NONE, QF_SZCHK_NONE
41 };
42 u64 qf_maxsize[MAXQUOTAS];
43 
44 /**
45  * Convert type of quota to written representation
46  */
quota_type2name(enum quota_type qtype)47 const char *quota_type2name(enum quota_type qtype)
48 {
49 	if (qtype >= MAXQUOTAS)
50 		return "unknown";
51 	return extensions[qtype];
52 }
53 
54 /*
55  * Set grace time if needed
56  */
update_grace_times(struct dquot * q)57 void update_grace_times(struct dquot *q)
58 {
59 	time_t now;
60 
61 	time(&now);
62 	if (q->dq_dqb.dqb_bsoftlimit && toqb(q->dq_dqb.dqb_curspace) >
63 			q->dq_dqb.dqb_bsoftlimit) {
64 		if (!q->dq_dqb.dqb_btime)
65 			q->dq_dqb.dqb_btime =
66 				now + q->dq_h->qh_info.dqi_bgrace;
67 	} else {
68 		q->dq_dqb.dqb_btime = 0;
69 	}
70 
71 	if (q->dq_dqb.dqb_isoftlimit && q->dq_dqb.dqb_curinodes >
72 			q->dq_dqb.dqb_isoftlimit) {
73 		if (!q->dq_dqb.dqb_itime)
74 				q->dq_dqb.dqb_itime =
75 					now + q->dq_h->qh_info.dqi_igrace;
76 	} else {
77 		q->dq_dqb.dqb_itime = 0;
78 	}
79 }
80 
81 /* Functions to read/write quota file. */
quota_write_nomount(struct quota_file * qf,long offset,void * buf,unsigned int size)82 static unsigned int quota_write_nomount(struct quota_file *qf,
83 					long offset,
84 					void *buf, unsigned int size)
85 {
86 	unsigned int written;
87 
88 	written = f2fs_write(qf->sbi, qf->ino, buf, size, offset);
89 	if (qf->filesize < offset + written)
90 		qf->filesize = offset + written;
91 	if (written != size)
92 		return -EIO;
93 	return written;
94 }
95 
quota_read_nomount(struct quota_file * qf,long offset,void * buf,unsigned int size)96 static unsigned int quota_read_nomount(struct quota_file *qf, long offset,
97 		void *buf, unsigned int size)
98 {
99 	return f2fs_read(qf->sbi, qf->ino, buf, size, offset);
100 }
101 
102 /*
103  * Detect quota format and initialize quota IO
104  */
quota_file_open(struct f2fs_sb_info * sbi,struct quota_handle * h,enum quota_type qtype,int flags)105 errcode_t quota_file_open(struct f2fs_sb_info *sbi, struct quota_handle *h,
106 			  enum quota_type qtype, int flags)
107 {
108 	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
109 	struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
110 	quota_ctx_t qctx = fsck->qctx;
111 	f2fs_ino_t qf_ino;
112 	errcode_t err = 0;
113 	int allocated_handle = 0;
114 
115 	if (qtype >= MAXQUOTAS)
116 		return EINVAL;
117 
118 	qf_ino = sb->qf_ino[qtype];
119 
120 	if (!h) {
121 		if (qctx->quota_file[qtype]) {
122 			h = qctx->quota_file[qtype];
123 			(void) quota_file_close(sbi, h, 0);
124 		}
125 		err = quota_get_mem(sizeof(struct quota_handle), &h);
126 		if (err) {
127 			log_err("Unable to allocate quota handle");
128 			return err;
129 		}
130 		allocated_handle = 1;
131 	}
132 
133 	h->qh_qf.sbi = sbi;
134 	h->qh_qf.ino = qf_ino;
135 	h->write = quota_write_nomount;
136 	h->read = quota_read_nomount;
137 	h->qh_file_flags = flags;
138 	h->qh_io_flags = 0;
139 	h->qh_type = qtype;
140 	h->qh_fmt = QFMT_VFS_V1;
141 	memset(&h->qh_info, 0, sizeof(h->qh_info));
142 	h->qh_ops = &quotafile_ops_2;
143 
144 	if (h->qh_ops->check_file &&
145 	    (h->qh_ops->check_file(h, qtype) == 0)) {
146 		log_err("qh_ops->check_file failed");
147 		err = EIO;
148 		goto errout;
149 	}
150 
151 	if (h->qh_ops->init_io && (h->qh_ops->init_io(h, qtype) < 0)) {
152 		log_err("qh_ops->init_io failed");
153 		err = EIO;
154 		goto errout;
155 	}
156 	if (allocated_handle)
157 		qctx->quota_file[qtype] = h;
158 errout:
159 	return err;
160 }
161 
162 /*
163  * Create new quotafile of specified format on given filesystem
164  */
quota_file_create(struct f2fs_sb_info * sbi,struct quota_handle * h,enum quota_type qtype)165 errcode_t quota_file_create(struct f2fs_sb_info *sbi, struct quota_handle *h,
166 		enum quota_type qtype)
167 {
168 	struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
169 	f2fs_ino_t qf_inum = sb->qf_ino[qtype];
170 	errcode_t err = 0;
171 
172 	memset(&h->qh_qf, 0, sizeof(h->qh_qf));
173 	h->qh_qf.sbi = sbi;
174 	h->qh_qf.ino = qf_inum;
175 	h->write = quota_write_nomount;
176 	h->read = quota_read_nomount;
177 
178 	log_debug("Creating quota ino=%u, type=%d", qf_inum, qtype);
179 	h->qh_io_flags = 0;
180 	h->qh_type = qtype;
181 	h->qh_fmt = QFMT_VFS_V1;
182 	memset(&h->qh_info, 0, sizeof(h->qh_info));
183 	h->qh_ops = &quotafile_ops_2;
184 
185 	if (h->qh_ops->new_io && (h->qh_ops->new_io(h) < 0)) {
186 		log_err("qh_ops->new_io failed");
187 		err = EIO;
188 	}
189 
190 	return err;
191 }
192 
193 /*
194  * Close quotafile and release handle
195  */
quota_file_close(struct f2fs_sb_info * sbi,struct quota_handle * h,int update_filesize)196 errcode_t quota_file_close(struct f2fs_sb_info *sbi, struct quota_handle *h,
197 		int update_filesize)
198 {
199 	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
200 	quota_ctx_t qctx = fsck->qctx;
201 
202         if (h->qh_io_flags & IOFL_INFODIRTY) {
203 		if (h->qh_ops->write_info && h->qh_ops->write_info(h) < 0)
204 			return EIO;
205 		h->qh_io_flags &= ~IOFL_INFODIRTY;
206 	}
207 	if (h->qh_ops->end_io && h->qh_ops->end_io(h) < 0)
208 		return EIO;
209 	if (update_filesize) {
210 		f2fs_filesize_update(sbi, h->qh_qf.ino, h->qh_qf.filesize);
211 	}
212 	if (qctx->quota_file[h->qh_type] == h)
213 		quota_free_mem(&qctx->quota_file[h->qh_type]);
214 	return 0;
215 }
216 
217 /*
218  * Create empty quota structure
219  */
get_empty_dquot(void)220 struct dquot *get_empty_dquot(void)
221 {
222 	struct dquot *dquot;
223 
224 	if (quota_get_memzero(sizeof(struct dquot), &dquot)) {
225 		log_err("Failed to allocate dquot");
226 		return NULL;
227 	}
228 
229 	dquot->dq_id = -1;
230 	return dquot;
231 }
232