• 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 };
35 
36 static_assert(sizeof(struct disk_dqheader) == 8, "");
37 
38 int cur_qtype = -1;
39 u32 qf_last_blkofs[MAXQUOTAS] = {0, 0, 0};
40 enum qf_szchk_type_t qf_szchk_type[MAXQUOTAS] =
41 {
42 	QF_SZCHK_NONE, QF_SZCHK_NONE, QF_SZCHK_NONE
43 };
44 u64 qf_maxsize[MAXQUOTAS];
45 
46 /**
47  * Convert type of quota to written representation
48  */
quota_type2name(enum quota_type qtype)49 const char *quota_type2name(enum quota_type qtype)
50 {
51 	if (qtype >= MAXQUOTAS)
52 		return "unknown";
53 	return extensions[qtype];
54 }
55 
56 /*
57  * Set grace time if needed
58  */
update_grace_times(struct dquot * q)59 void update_grace_times(struct dquot *q)
60 {
61 	time_t now;
62 
63 	time(&now);
64 	if (q->dq_dqb.dqb_bsoftlimit && toqb(q->dq_dqb.dqb_curspace) >
65 			q->dq_dqb.dqb_bsoftlimit) {
66 		if (!q->dq_dqb.dqb_btime)
67 			q->dq_dqb.dqb_btime =
68 				now + q->dq_h->qh_info.dqi_bgrace;
69 	} else {
70 		q->dq_dqb.dqb_btime = 0;
71 	}
72 
73 	if (q->dq_dqb.dqb_isoftlimit && q->dq_dqb.dqb_curinodes >
74 			q->dq_dqb.dqb_isoftlimit) {
75 		if (!q->dq_dqb.dqb_itime)
76 				q->dq_dqb.dqb_itime =
77 					now + q->dq_h->qh_info.dqi_igrace;
78 	} else {
79 		q->dq_dqb.dqb_itime = 0;
80 	}
81 }
82 
83 /* Functions to read/write quota file. */
quota_write_nomount(struct quota_file * qf,long offset,void * buf,unsigned int size)84 static unsigned int quota_write_nomount(struct quota_file *qf,
85 					long offset,
86 					void *buf, unsigned int size)
87 {
88 	unsigned int written;
89 
90 	written = f2fs_write(qf->sbi, qf->ino, buf, size, offset);
91 	if (qf->filesize < offset + written)
92 		qf->filesize = offset + written;
93 	if (written != size)
94 		return -EIO;
95 	return written;
96 }
97 
quota_read_nomount(struct quota_file * qf,long offset,void * buf,unsigned int size)98 static unsigned int quota_read_nomount(struct quota_file *qf, long offset,
99 		void *buf, unsigned int size)
100 {
101 	return f2fs_read(qf->sbi, qf->ino, buf, size, offset);
102 }
103 
104 /*
105  * Detect quota format and initialize quota IO
106  */
quota_file_open(struct f2fs_sb_info * sbi,struct quota_handle * h,enum quota_type qtype,int flags)107 errcode_t quota_file_open(struct f2fs_sb_info *sbi, struct quota_handle *h,
108 			  enum quota_type qtype, int flags)
109 {
110 	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
111 	struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
112 	quota_ctx_t qctx = fsck->qctx;
113 	f2fs_ino_t qf_ino;
114 	errcode_t err = 0;
115 	int allocated_handle = 0;
116 
117 	if (qtype >= MAXQUOTAS)
118 		return EINVAL;
119 
120 	qf_ino = sb->qf_ino[qtype];
121 
122 	if (!h) {
123 		if (qctx->quota_file[qtype]) {
124 			h = qctx->quota_file[qtype];
125 			(void) quota_file_close(sbi, h, 0);
126 		}
127 		err = quota_get_mem(sizeof(struct quota_handle), &h);
128 		if (err) {
129 			log_err("Unable to allocate quota handle");
130 			return err;
131 		}
132 		allocated_handle = 1;
133 	}
134 
135 	h->qh_qf.sbi = sbi;
136 	h->qh_qf.ino = qf_ino;
137 	h->write = quota_write_nomount;
138 	h->read = quota_read_nomount;
139 	h->qh_file_flags = flags;
140 	h->qh_io_flags = 0;
141 	h->qh_type = qtype;
142 	h->qh_fmt = QFMT_VFS_V1;
143 	memset(&h->qh_info, 0, sizeof(h->qh_info));
144 	h->qh_ops = &quotafile_ops_2;
145 
146 	if (h->qh_ops->check_file &&
147 	    (h->qh_ops->check_file(h, qtype) == 0)) {
148 		log_err("qh_ops->check_file failed");
149 		err = EIO;
150 		goto errout;
151 	}
152 
153 	if (h->qh_ops->init_io && (h->qh_ops->init_io(h, qtype) < 0)) {
154 		log_err("qh_ops->init_io failed");
155 		err = EIO;
156 		goto errout;
157 	}
158 	if (allocated_handle)
159 		qctx->quota_file[qtype] = h;
160 errout:
161 	return err;
162 }
163 
164 /*
165  * Create new quotafile of specified format on given filesystem
166  */
quota_file_create(struct f2fs_sb_info * sbi,struct quota_handle * h,enum quota_type qtype)167 errcode_t quota_file_create(struct f2fs_sb_info *sbi, struct quota_handle *h,
168 		enum quota_type qtype)
169 {
170 	struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
171 	f2fs_ino_t qf_inum = sb->qf_ino[qtype];
172 	errcode_t err = 0;
173 
174 	memset(&h->qh_qf, 0, sizeof(h->qh_qf));
175 	h->qh_qf.sbi = sbi;
176 	h->qh_qf.ino = qf_inum;
177 	h->write = quota_write_nomount;
178 	h->read = quota_read_nomount;
179 
180 	log_debug("Creating quota ino=%u, type=%d", qf_inum, qtype);
181 	h->qh_io_flags = 0;
182 	h->qh_type = qtype;
183 	h->qh_fmt = QFMT_VFS_V1;
184 	memset(&h->qh_info, 0, sizeof(h->qh_info));
185 	h->qh_ops = &quotafile_ops_2;
186 
187 	if (h->qh_ops->new_io && (h->qh_ops->new_io(h) < 0)) {
188 		log_err("qh_ops->new_io failed");
189 		err = EIO;
190 	}
191 
192 	return err;
193 }
194 
195 /*
196  * Close quotafile and release handle
197  */
quota_file_close(struct f2fs_sb_info * sbi,struct quota_handle * h,int update_filesize)198 errcode_t quota_file_close(struct f2fs_sb_info *sbi, struct quota_handle *h,
199 		int update_filesize)
200 {
201 	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
202 	quota_ctx_t qctx = fsck->qctx;
203 
204         if (h->qh_io_flags & IOFL_INFODIRTY) {
205 		if (h->qh_ops->write_info && h->qh_ops->write_info(h) < 0)
206 			return EIO;
207 		h->qh_io_flags &= ~IOFL_INFODIRTY;
208 	}
209 	if (h->qh_ops->end_io && h->qh_ops->end_io(h) < 0)
210 		return EIO;
211 	if (update_filesize) {
212 		f2fs_filesize_update(sbi, h->qh_qf.ino, h->qh_qf.filesize);
213 	}
214 	if (qctx->quota_file[h->qh_type] == h)
215 		quota_free_mem(&qctx->quota_file[h->qh_type]);
216 	return 0;
217 }
218 
219 /*
220  * Create empty quota structure
221  */
get_empty_dquot(void)222 struct dquot *get_empty_dquot(void)
223 {
224 	struct dquot *dquot;
225 
226 	if (quota_get_memzero(sizeof(struct dquot), &dquot)) {
227 		log_err("Failed to allocate dquot");
228 		return NULL;
229 	}
230 
231 	dquot->dq_id = -1;
232 	return dquot;
233 }
234