blob: bf3f8f05c89b3dbb10849111e7a0815b2a3786e5 [file] [log] [blame]
Ryusuke Konishiae980432018-09-04 15:46:30 -07001// SPDX-License-Identifier: GPL-2.0+
Koji Sato6c98cd42009-04-06 19:01:32 -07002/*
3 * sufile.c - NILFS segment usage file.
4 *
5 * Copyright (C) 2006-2008 Nippon Telegraph and Telephone Corporation.
6 *
Ryusuke Konishi4b420ab2016-05-23 16:23:09 -07007 * Written by Koji Sato.
8 * Revised by Ryusuke Konishi.
Koji Sato6c98cd42009-04-06 19:01:32 -07009 */
10
11#include <linux/kernel.h>
12#include <linux/fs.h>
13#include <linux/string.h>
14#include <linux/buffer_head.h>
15#include <linux/errno.h>
Koji Sato6c98cd42009-04-06 19:01:32 -070016#include "mdt.h"
17#include "sufile.h"
18
Hitoshi Mitake83eec5e2015-11-06 16:32:05 -080019#include <trace/events/nilfs2.h>
20
Vyacheslav Dubeykof5974c82012-07-30 14:42:10 -070021/**
22 * struct nilfs_sufile_info - on-memory private data of sufile
23 * @mi: on-memory private data of metadata file
24 * @ncleansegs: number of clean segments
25 * @allocmin: lower limit of allocatable segment range
26 * @allocmax: upper limit of allocatable segment range
27 */
Ryusuke Konishiaa474a22009-11-13 03:41:55 +090028struct nilfs_sufile_info {
29 struct nilfs_mdt_info mi;
Ryusuke Konishi619205d2011-05-05 01:23:57 +090030 unsigned long ncleansegs;/* number of clean segments */
31 __u64 allocmin; /* lower limit of allocatable segment range */
32 __u64 allocmax; /* upper limit of allocatable segment range */
Ryusuke Konishiaa474a22009-11-13 03:41:55 +090033};
34
35static inline struct nilfs_sufile_info *NILFS_SUI(struct inode *sufile)
36{
37 return (struct nilfs_sufile_info *)NILFS_MDT(sufile);
38}
39
Koji Sato6c98cd42009-04-06 19:01:32 -070040static inline unsigned long
41nilfs_sufile_segment_usages_per_block(const struct inode *sufile)
42{
43 return NILFS_MDT(sufile)->mi_entries_per_block;
44}
45
46static unsigned long
47nilfs_sufile_get_blkoff(const struct inode *sufile, __u64 segnum)
48{
49 __u64 t = segnum + NILFS_MDT(sufile)->mi_first_entry_offset;
Ryusuke Konishi4ad364c2016-05-23 16:23:25 -070050
Koji Sato6c98cd42009-04-06 19:01:32 -070051 do_div(t, nilfs_sufile_segment_usages_per_block(sufile));
52 return (unsigned long)t;
53}
54
55static unsigned long
56nilfs_sufile_get_offset(const struct inode *sufile, __u64 segnum)
57{
58 __u64 t = segnum + NILFS_MDT(sufile)->mi_first_entry_offset;
Ryusuke Konishi4ad364c2016-05-23 16:23:25 -070059
Koji Sato6c98cd42009-04-06 19:01:32 -070060 return do_div(t, nilfs_sufile_segment_usages_per_block(sufile));
61}
62
63static unsigned long
64nilfs_sufile_segment_usages_in_block(const struct inode *sufile, __u64 curr,
65 __u64 max)
66{
67 return min_t(unsigned long,
68 nilfs_sufile_segment_usages_per_block(sufile) -
69 nilfs_sufile_get_offset(sufile, curr),
70 max - curr + 1);
71}
72
Koji Sato6c98cd42009-04-06 19:01:32 -070073static struct nilfs_segment_usage *
74nilfs_sufile_block_get_segment_usage(const struct inode *sufile, __u64 segnum,
75 struct buffer_head *bh, void *kaddr)
76{
77 return kaddr + bh_offset(bh) +
78 nilfs_sufile_get_offset(sufile, segnum) *
79 NILFS_MDT(sufile)->mi_entry_size;
80}
81
82static inline int nilfs_sufile_get_header_block(struct inode *sufile,
83 struct buffer_head **bhp)
84{
85 return nilfs_mdt_get_block(sufile, 0, 0, NULL, bhp);
86}
87
88static inline int
89nilfs_sufile_get_segment_usage_block(struct inode *sufile, __u64 segnum,
90 int create, struct buffer_head **bhp)
91{
92 return nilfs_mdt_get_block(sufile,
93 nilfs_sufile_get_blkoff(sufile, segnum),
94 create, NULL, bhp);
95}
96
Ryusuke Konishi78eb64c2011-05-05 01:23:58 +090097static int nilfs_sufile_delete_segment_usage_block(struct inode *sufile,
98 __u64 segnum)
99{
100 return nilfs_mdt_delete_block(sufile,
101 nilfs_sufile_get_blkoff(sufile, segnum));
102}
103
Ryusuke Konishia7030182009-04-05 18:24:11 +0900104static void nilfs_sufile_mod_counter(struct buffer_head *header_bh,
105 u64 ncleanadd, u64 ndirtyadd)
106{
107 struct nilfs_sufile_header *header;
108 void *kaddr;
109
Cong Wang7b9c0972011-11-25 23:14:33 +0800110 kaddr = kmap_atomic(header_bh->b_page);
Ryusuke Konishia7030182009-04-05 18:24:11 +0900111 header = kaddr + bh_offset(header_bh);
112 le64_add_cpu(&header->sh_ncleansegs, ncleanadd);
113 le64_add_cpu(&header->sh_ndirtysegs, ndirtyadd);
Cong Wang7b9c0972011-11-25 23:14:33 +0800114 kunmap_atomic(kaddr);
Ryusuke Konishia7030182009-04-05 18:24:11 +0900115
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900116 mark_buffer_dirty(header_bh);
Ryusuke Konishia7030182009-04-05 18:24:11 +0900117}
118
Ryusuke Konishidda54f42009-05-16 21:49:10 +0900119/**
Ryusuke Konishief7d4752009-11-13 08:45:32 +0900120 * nilfs_sufile_get_ncleansegs - return the number of clean segments
121 * @sufile: inode of segment usage file
122 */
123unsigned long nilfs_sufile_get_ncleansegs(struct inode *sufile)
124{
125 return NILFS_SUI(sufile)->ncleansegs;
126}
127
128/**
Ryusuke Konishidda54f42009-05-16 21:49:10 +0900129 * nilfs_sufile_updatev - modify multiple segment usages at a time
130 * @sufile: inode of segment usage file
131 * @segnumv: array of segment numbers
132 * @nsegs: size of @segnumv array
133 * @create: creation flag
134 * @ndone: place to store number of modified segments on @segnumv
135 * @dofunc: primitive operation for the update
136 *
137 * Description: nilfs_sufile_updatev() repeatedly calls @dofunc
138 * against the given array of segments. The @dofunc is called with
139 * buffers of a header block and the sufile block in which the target
140 * segment usage entry is contained. If @ndone is given, the number
141 * of successfully modified segments from the head is stored in the
142 * place @ndone points to.
143 *
144 * Return Value: On success, zero is returned. On error, one of the
145 * following negative error codes is returned.
146 *
147 * %-EIO - I/O error.
148 *
149 * %-ENOMEM - Insufficient amount of memory available.
150 *
151 * %-ENOENT - Given segment usage is in hole block (may be returned if
152 * @create is zero)
153 *
154 * %-EINVAL - Invalid segment usage number
155 */
156int nilfs_sufile_updatev(struct inode *sufile, __u64 *segnumv, size_t nsegs,
157 int create, size_t *ndone,
158 void (*dofunc)(struct inode *, __u64,
159 struct buffer_head *,
160 struct buffer_head *))
161{
162 struct buffer_head *header_bh, *bh;
163 unsigned long blkoff, prev_blkoff;
164 __u64 *seg;
165 size_t nerr = 0, n = 0;
166 int ret = 0;
167
168 if (unlikely(nsegs == 0))
169 goto out;
170
171 down_write(&NILFS_MDT(sufile)->mi_sem);
172 for (seg = segnumv; seg < segnumv + nsegs; seg++) {
173 if (unlikely(*seg >= nilfs_sufile_get_nsegments(sufile))) {
Ryusuke Konishifeee8802016-08-02 14:05:10 -0700174 nilfs_msg(sufile->i_sb, KERN_WARNING,
175 "%s: invalid segment number: %llu",
176 __func__, (unsigned long long)*seg);
Ryusuke Konishidda54f42009-05-16 21:49:10 +0900177 nerr++;
178 }
179 }
180 if (nerr > 0) {
181 ret = -EINVAL;
182 goto out_sem;
183 }
184
185 ret = nilfs_sufile_get_header_block(sufile, &header_bh);
186 if (ret < 0)
187 goto out_sem;
188
189 seg = segnumv;
190 blkoff = nilfs_sufile_get_blkoff(sufile, *seg);
191 ret = nilfs_mdt_get_block(sufile, blkoff, create, NULL, &bh);
192 if (ret < 0)
193 goto out_header;
194
195 for (;;) {
196 dofunc(sufile, *seg, header_bh, bh);
197
198 if (++seg >= segnumv + nsegs)
199 break;
200 prev_blkoff = blkoff;
201 blkoff = nilfs_sufile_get_blkoff(sufile, *seg);
202 if (blkoff == prev_blkoff)
203 continue;
204
205 /* get different block */
206 brelse(bh);
207 ret = nilfs_mdt_get_block(sufile, blkoff, create, NULL, &bh);
208 if (unlikely(ret < 0))
209 goto out_header;
210 }
211 brelse(bh);
212
213 out_header:
214 n = seg - segnumv;
215 brelse(header_bh);
216 out_sem:
217 up_write(&NILFS_MDT(sufile)->mi_sem);
218 out:
219 if (ndone)
220 *ndone = n;
221 return ret;
222}
223
Ryusuke Konishia7030182009-04-05 18:24:11 +0900224int nilfs_sufile_update(struct inode *sufile, __u64 segnum, int create,
225 void (*dofunc)(struct inode *, __u64,
226 struct buffer_head *,
227 struct buffer_head *))
228{
229 struct buffer_head *header_bh, *bh;
230 int ret;
231
232 if (unlikely(segnum >= nilfs_sufile_get_nsegments(sufile))) {
Ryusuke Konishifeee8802016-08-02 14:05:10 -0700233 nilfs_msg(sufile->i_sb, KERN_WARNING,
234 "%s: invalid segment number: %llu",
235 __func__, (unsigned long long)segnum);
Ryusuke Konishia7030182009-04-05 18:24:11 +0900236 return -EINVAL;
237 }
238 down_write(&NILFS_MDT(sufile)->mi_sem);
239
240 ret = nilfs_sufile_get_header_block(sufile, &header_bh);
241 if (ret < 0)
242 goto out_sem;
243
244 ret = nilfs_sufile_get_segment_usage_block(sufile, segnum, create, &bh);
245 if (!ret) {
246 dofunc(sufile, segnum, header_bh, bh);
247 brelse(bh);
248 }
249 brelse(header_bh);
250
251 out_sem:
252 up_write(&NILFS_MDT(sufile)->mi_sem);
253 return ret;
254}
255
Koji Sato6c98cd42009-04-06 19:01:32 -0700256/**
Ryusuke Konishi619205d2011-05-05 01:23:57 +0900257 * nilfs_sufile_set_alloc_range - limit range of segment to be allocated
258 * @sufile: inode of segment usage file
259 * @start: minimum segment number of allocatable region (inclusive)
260 * @end: maximum segment number of allocatable region (inclusive)
261 *
262 * Return Value: On success, 0 is returned. On error, one of the
263 * following negative error codes is returned.
264 *
265 * %-ERANGE - invalid segment region
266 */
267int nilfs_sufile_set_alloc_range(struct inode *sufile, __u64 start, __u64 end)
268{
269 struct nilfs_sufile_info *sui = NILFS_SUI(sufile);
270 __u64 nsegs;
271 int ret = -ERANGE;
272
273 down_write(&NILFS_MDT(sufile)->mi_sem);
274 nsegs = nilfs_sufile_get_nsegments(sufile);
275
276 if (start <= end && end < nsegs) {
277 sui->allocmin = start;
278 sui->allocmax = end;
279 ret = 0;
280 }
281 up_write(&NILFS_MDT(sufile)->mi_sem);
282 return ret;
283}
284
285/**
Koji Sato6c98cd42009-04-06 19:01:32 -0700286 * nilfs_sufile_alloc - allocate a segment
287 * @sufile: inode of segment usage file
288 * @segnump: pointer to segment number
289 *
290 * Description: nilfs_sufile_alloc() allocates a clean segment.
291 *
292 * Return Value: On success, 0 is returned and the segment number of the
293 * allocated segment is stored in the place pointed by @segnump. On error, one
294 * of the following negative error codes is returned.
295 *
296 * %-EIO - I/O error.
297 *
298 * %-ENOMEM - Insufficient amount of memory available.
299 *
300 * %-ENOSPC - No clean segment left.
301 */
302int nilfs_sufile_alloc(struct inode *sufile, __u64 *segnump)
303{
304 struct buffer_head *header_bh, *su_bh;
Koji Sato6c98cd42009-04-06 19:01:32 -0700305 struct nilfs_sufile_header *header;
306 struct nilfs_segment_usage *su;
Ryusuke Konishi619205d2011-05-05 01:23:57 +0900307 struct nilfs_sufile_info *sui = NILFS_SUI(sufile);
Koji Sato6c98cd42009-04-06 19:01:32 -0700308 size_t susz = NILFS_MDT(sufile)->mi_entry_size;
309 __u64 segnum, maxsegnum, last_alloc;
310 void *kaddr;
Ryusuke Konishi09ef29e2015-11-06 16:32:14 -0800311 unsigned long nsegments, nsus, cnt;
Ryusuke Konishi619205d2011-05-05 01:23:57 +0900312 int ret, j;
Koji Sato6c98cd42009-04-06 19:01:32 -0700313
314 down_write(&NILFS_MDT(sufile)->mi_sem);
315
Koji Sato6c98cd42009-04-06 19:01:32 -0700316 ret = nilfs_sufile_get_header_block(sufile, &header_bh);
317 if (ret < 0)
318 goto out_sem;
Cong Wang7b9c0972011-11-25 23:14:33 +0800319 kaddr = kmap_atomic(header_bh->b_page);
Ryusuke Konishi7b16c8a2009-11-13 03:10:21 +0900320 header = kaddr + bh_offset(header_bh);
Koji Sato6c98cd42009-04-06 19:01:32 -0700321 last_alloc = le64_to_cpu(header->sh_last_alloc);
Cong Wang7b9c0972011-11-25 23:14:33 +0800322 kunmap_atomic(kaddr);
Koji Sato6c98cd42009-04-06 19:01:32 -0700323
324 nsegments = nilfs_sufile_get_nsegments(sufile);
Ryusuke Konishi619205d2011-05-05 01:23:57 +0900325 maxsegnum = sui->allocmax;
Koji Sato6c98cd42009-04-06 19:01:32 -0700326 segnum = last_alloc + 1;
Ryusuke Konishi619205d2011-05-05 01:23:57 +0900327 if (segnum < sui->allocmin || segnum > sui->allocmax)
328 segnum = sui->allocmin;
329
330 for (cnt = 0; cnt < nsegments; cnt += nsus) {
331 if (segnum > maxsegnum) {
332 if (cnt < sui->allocmax - sui->allocmin + 1) {
333 /*
334 * wrap around in the limited region.
335 * if allocation started from
336 * sui->allocmin, this never happens.
337 */
338 segnum = sui->allocmin;
339 maxsegnum = last_alloc;
340 } else if (segnum > sui->allocmin &&
341 sui->allocmax + 1 < nsegments) {
342 segnum = sui->allocmax + 1;
343 maxsegnum = nsegments - 1;
344 } else if (sui->allocmin > 0) {
345 segnum = 0;
346 maxsegnum = sui->allocmin - 1;
347 } else {
348 break; /* never happens */
349 }
Koji Sato6c98cd42009-04-06 19:01:32 -0700350 }
Hitoshi Mitake83eec5e2015-11-06 16:32:05 -0800351 trace_nilfs2_segment_usage_check(sufile, segnum, cnt);
Koji Sato6c98cd42009-04-06 19:01:32 -0700352 ret = nilfs_sufile_get_segment_usage_block(sufile, segnum, 1,
353 &su_bh);
354 if (ret < 0)
355 goto out_header;
Cong Wang7b9c0972011-11-25 23:14:33 +0800356 kaddr = kmap_atomic(su_bh->b_page);
Koji Sato6c98cd42009-04-06 19:01:32 -0700357 su = nilfs_sufile_block_get_segment_usage(
358 sufile, segnum, su_bh, kaddr);
359
360 nsus = nilfs_sufile_segment_usages_in_block(
361 sufile, segnum, maxsegnum);
362 for (j = 0; j < nsus; j++, su = (void *)su + susz, segnum++) {
363 if (!nilfs_segment_usage_clean(su))
364 continue;
365 /* found a clean segment */
Koji Sato6c98cd42009-04-06 19:01:32 -0700366 nilfs_segment_usage_set_dirty(su);
Cong Wang7b9c0972011-11-25 23:14:33 +0800367 kunmap_atomic(kaddr);
Koji Sato6c98cd42009-04-06 19:01:32 -0700368
Cong Wang7b9c0972011-11-25 23:14:33 +0800369 kaddr = kmap_atomic(header_bh->b_page);
Ryusuke Konishi7b16c8a2009-11-13 03:10:21 +0900370 header = kaddr + bh_offset(header_bh);
Koji Sato6c98cd42009-04-06 19:01:32 -0700371 le64_add_cpu(&header->sh_ncleansegs, -1);
372 le64_add_cpu(&header->sh_ndirtysegs, 1);
373 header->sh_last_alloc = cpu_to_le64(segnum);
Cong Wang7b9c0972011-11-25 23:14:33 +0800374 kunmap_atomic(kaddr);
Koji Sato6c98cd42009-04-06 19:01:32 -0700375
Ryusuke Konishi619205d2011-05-05 01:23:57 +0900376 sui->ncleansegs--;
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900377 mark_buffer_dirty(header_bh);
378 mark_buffer_dirty(su_bh);
Koji Sato6c98cd42009-04-06 19:01:32 -0700379 nilfs_mdt_mark_dirty(sufile);
380 brelse(su_bh);
381 *segnump = segnum;
Hitoshi Mitake83eec5e2015-11-06 16:32:05 -0800382
383 trace_nilfs2_segment_usage_allocated(sufile, segnum);
384
Koji Sato6c98cd42009-04-06 19:01:32 -0700385 goto out_header;
386 }
387
Cong Wang7b9c0972011-11-25 23:14:33 +0800388 kunmap_atomic(kaddr);
Koji Sato6c98cd42009-04-06 19:01:32 -0700389 brelse(su_bh);
390 }
391
392 /* no segments left */
393 ret = -ENOSPC;
394
395 out_header:
396 brelse(header_bh);
397
398 out_sem:
399 up_write(&NILFS_MDT(sufile)->mi_sem);
400 return ret;
401}
402
Ryusuke Konishia7030182009-04-05 18:24:11 +0900403void nilfs_sufile_do_cancel_free(struct inode *sufile, __u64 segnum,
404 struct buffer_head *header_bh,
405 struct buffer_head *su_bh)
Koji Sato6c98cd42009-04-06 19:01:32 -0700406{
Koji Sato6c98cd42009-04-06 19:01:32 -0700407 struct nilfs_segment_usage *su;
408 void *kaddr;
Koji Sato6c98cd42009-04-06 19:01:32 -0700409
Cong Wang7b9c0972011-11-25 23:14:33 +0800410 kaddr = kmap_atomic(su_bh->b_page);
Ryusuke Konishia7030182009-04-05 18:24:11 +0900411 su = nilfs_sufile_block_get_segment_usage(sufile, segnum, su_bh, kaddr);
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -0700412 if (unlikely(!nilfs_segment_usage_clean(su))) {
Ryusuke Konishifeee8802016-08-02 14:05:10 -0700413 nilfs_msg(sufile->i_sb, KERN_WARNING,
414 "%s: segment %llu must be clean", __func__,
415 (unsigned long long)segnum);
Cong Wang7b9c0972011-11-25 23:14:33 +0800416 kunmap_atomic(kaddr);
Ryusuke Konishia7030182009-04-05 18:24:11 +0900417 return;
Koji Sato6c98cd42009-04-06 19:01:32 -0700418 }
419 nilfs_segment_usage_set_dirty(su);
Cong Wang7b9c0972011-11-25 23:14:33 +0800420 kunmap_atomic(kaddr);
Koji Sato6c98cd42009-04-06 19:01:32 -0700421
Ryusuke Konishia7030182009-04-05 18:24:11 +0900422 nilfs_sufile_mod_counter(header_bh, -1, 1);
Ryusuke Konishiaa474a22009-11-13 03:41:55 +0900423 NILFS_SUI(sufile)->ncleansegs--;
424
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900425 mark_buffer_dirty(su_bh);
Koji Sato6c98cd42009-04-06 19:01:32 -0700426 nilfs_mdt_mark_dirty(sufile);
Koji Sato6c98cd42009-04-06 19:01:32 -0700427}
428
Ryusuke Konishic85399c2009-04-05 18:30:58 +0900429void nilfs_sufile_do_scrap(struct inode *sufile, __u64 segnum,
430 struct buffer_head *header_bh,
431 struct buffer_head *su_bh)
432{
433 struct nilfs_segment_usage *su;
434 void *kaddr;
435 int clean, dirty;
436
Cong Wang7b9c0972011-11-25 23:14:33 +0800437 kaddr = kmap_atomic(su_bh->b_page);
Ryusuke Konishic85399c2009-04-05 18:30:58 +0900438 su = nilfs_sufile_block_get_segment_usage(sufile, segnum, su_bh, kaddr);
Ryusuke Konishi4ce5c342016-08-02 14:05:28 -0700439 if (su->su_flags == cpu_to_le32(BIT(NILFS_SEGMENT_USAGE_DIRTY)) &&
Ryusuke Konishic85399c2009-04-05 18:30:58 +0900440 su->su_nblocks == cpu_to_le32(0)) {
Cong Wang7b9c0972011-11-25 23:14:33 +0800441 kunmap_atomic(kaddr);
Ryusuke Konishic85399c2009-04-05 18:30:58 +0900442 return;
443 }
444 clean = nilfs_segment_usage_clean(su);
445 dirty = nilfs_segment_usage_dirty(su);
446
447 /* make the segment garbage */
448 su->su_lastmod = cpu_to_le64(0);
449 su->su_nblocks = cpu_to_le32(0);
Ryusuke Konishi4ce5c342016-08-02 14:05:28 -0700450 su->su_flags = cpu_to_le32(BIT(NILFS_SEGMENT_USAGE_DIRTY));
Cong Wang7b9c0972011-11-25 23:14:33 +0800451 kunmap_atomic(kaddr);
Ryusuke Konishic85399c2009-04-05 18:30:58 +0900452
453 nilfs_sufile_mod_counter(header_bh, clean ? (u64)-1 : 0, dirty ? 0 : 1);
Ryusuke Konishiaa474a22009-11-13 03:41:55 +0900454 NILFS_SUI(sufile)->ncleansegs -= clean;
455
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900456 mark_buffer_dirty(su_bh);
Ryusuke Konishic85399c2009-04-05 18:30:58 +0900457 nilfs_mdt_mark_dirty(sufile);
458}
459
Ryusuke Konishia7030182009-04-05 18:24:11 +0900460void nilfs_sufile_do_free(struct inode *sufile, __u64 segnum,
461 struct buffer_head *header_bh,
462 struct buffer_head *su_bh)
Koji Sato6c98cd42009-04-06 19:01:32 -0700463{
Koji Sato6c98cd42009-04-06 19:01:32 -0700464 struct nilfs_segment_usage *su;
465 void *kaddr;
Ryusuke Konishia7030182009-04-05 18:24:11 +0900466 int sudirty;
Koji Sato6c98cd42009-04-06 19:01:32 -0700467
Cong Wang7b9c0972011-11-25 23:14:33 +0800468 kaddr = kmap_atomic(su_bh->b_page);
Ryusuke Konishia7030182009-04-05 18:24:11 +0900469 su = nilfs_sufile_block_get_segment_usage(sufile, segnum, su_bh, kaddr);
470 if (nilfs_segment_usage_clean(su)) {
Ryusuke Konishifeee8802016-08-02 14:05:10 -0700471 nilfs_msg(sufile->i_sb, KERN_WARNING,
472 "%s: segment %llu is already clean",
473 __func__, (unsigned long long)segnum);
Cong Wang7b9c0972011-11-25 23:14:33 +0800474 kunmap_atomic(kaddr);
Ryusuke Konishia7030182009-04-05 18:24:11 +0900475 return;
Koji Sato6c98cd42009-04-06 19:01:32 -0700476 }
Ryusuke Konishia7030182009-04-05 18:24:11 +0900477 WARN_ON(nilfs_segment_usage_error(su));
478 WARN_ON(!nilfs_segment_usage_dirty(su));
479
480 sudirty = nilfs_segment_usage_dirty(su);
481 nilfs_segment_usage_set_clean(su);
Cong Wang7b9c0972011-11-25 23:14:33 +0800482 kunmap_atomic(kaddr);
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900483 mark_buffer_dirty(su_bh);
Ryusuke Konishia7030182009-04-05 18:24:11 +0900484
485 nilfs_sufile_mod_counter(header_bh, 1, sudirty ? (u64)-1 : 0);
Ryusuke Konishiaa474a22009-11-13 03:41:55 +0900486 NILFS_SUI(sufile)->ncleansegs++;
487
Koji Sato6c98cd42009-04-06 19:01:32 -0700488 nilfs_mdt_mark_dirty(sufile);
Hitoshi Mitake83eec5e2015-11-06 16:32:05 -0800489
490 trace_nilfs2_segment_usage_freed(sufile, segnum);
Koji Sato6c98cd42009-04-06 19:01:32 -0700491}
492
493/**
Ryusuke Konishi61a189e2009-11-18 17:25:12 +0900494 * nilfs_sufile_mark_dirty - mark the buffer having a segment usage dirty
495 * @sufile: inode of segment usage file
496 * @segnum: segment number
497 */
498int nilfs_sufile_mark_dirty(struct inode *sufile, __u64 segnum)
499{
500 struct buffer_head *bh;
501 int ret;
502
503 ret = nilfs_sufile_get_segment_usage_block(sufile, segnum, 0, &bh);
504 if (!ret) {
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900505 mark_buffer_dirty(bh);
Ryusuke Konishi61a189e2009-11-18 17:25:12 +0900506 nilfs_mdt_mark_dirty(sufile);
507 brelse(bh);
508 }
509 return ret;
510}
511
512/**
Ryusuke Konishi071ec542009-11-18 18:23:34 +0900513 * nilfs_sufile_set_segment_usage - set usage of a segment
514 * @sufile: inode of segment usage file
515 * @segnum: segment number
516 * @nblocks: number of live blocks in the segment
517 * @modtime: modification time (option)
518 */
519int nilfs_sufile_set_segment_usage(struct inode *sufile, __u64 segnum,
Arnd Bergmannfb04b912018-02-06 15:39:21 -0800520 unsigned long nblocks, time64_t modtime)
Ryusuke Konishi071ec542009-11-18 18:23:34 +0900521{
522 struct buffer_head *bh;
523 struct nilfs_segment_usage *su;
524 void *kaddr;
525 int ret;
526
527 down_write(&NILFS_MDT(sufile)->mi_sem);
528 ret = nilfs_sufile_get_segment_usage_block(sufile, segnum, 0, &bh);
529 if (ret < 0)
530 goto out_sem;
531
Cong Wang7b9c0972011-11-25 23:14:33 +0800532 kaddr = kmap_atomic(bh->b_page);
Ryusuke Konishi071ec542009-11-18 18:23:34 +0900533 su = nilfs_sufile_block_get_segment_usage(sufile, segnum, bh, kaddr);
534 WARN_ON(nilfs_segment_usage_error(su));
535 if (modtime)
536 su->su_lastmod = cpu_to_le64(modtime);
537 su->su_nblocks = cpu_to_le32(nblocks);
Cong Wang7b9c0972011-11-25 23:14:33 +0800538 kunmap_atomic(kaddr);
Ryusuke Konishi071ec542009-11-18 18:23:34 +0900539
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900540 mark_buffer_dirty(bh);
Ryusuke Konishi071ec542009-11-18 18:23:34 +0900541 nilfs_mdt_mark_dirty(sufile);
542 brelse(bh);
543
544 out_sem:
545 up_write(&NILFS_MDT(sufile)->mi_sem);
546 return ret;
547}
548
549/**
Koji Sato6c98cd42009-04-06 19:01:32 -0700550 * nilfs_sufile_get_stat - get segment usage statistics
551 * @sufile: inode of segment usage file
552 * @stat: pointer to a structure of segment usage statistics
553 *
554 * Description: nilfs_sufile_get_stat() returns information about segment
555 * usage.
556 *
557 * Return Value: On success, 0 is returned, and segment usage information is
558 * stored in the place pointed by @stat. On error, one of the following
559 * negative error codes is returned.
560 *
561 * %-EIO - I/O error.
562 *
563 * %-ENOMEM - Insufficient amount of memory available.
564 */
565int nilfs_sufile_get_stat(struct inode *sufile, struct nilfs_sustat *sustat)
566{
567 struct buffer_head *header_bh;
568 struct nilfs_sufile_header *header;
Ryusuke Konishi0ef28f92011-05-05 12:56:51 +0900569 struct the_nilfs *nilfs = sufile->i_sb->s_fs_info;
Koji Sato6c98cd42009-04-06 19:01:32 -0700570 void *kaddr;
571 int ret;
572
573 down_read(&NILFS_MDT(sufile)->mi_sem);
574
575 ret = nilfs_sufile_get_header_block(sufile, &header_bh);
576 if (ret < 0)
577 goto out_sem;
578
Cong Wang7b9c0972011-11-25 23:14:33 +0800579 kaddr = kmap_atomic(header_bh->b_page);
Ryusuke Konishi7b16c8a2009-11-13 03:10:21 +0900580 header = kaddr + bh_offset(header_bh);
Koji Sato6c98cd42009-04-06 19:01:32 -0700581 sustat->ss_nsegs = nilfs_sufile_get_nsegments(sufile);
582 sustat->ss_ncleansegs = le64_to_cpu(header->sh_ncleansegs);
583 sustat->ss_ndirtysegs = le64_to_cpu(header->sh_ndirtysegs);
Ryusuke Konishi2c2e52f2009-04-06 19:01:54 -0700584 sustat->ss_ctime = nilfs->ns_ctime;
585 sustat->ss_nongc_ctime = nilfs->ns_nongc_ctime;
586 spin_lock(&nilfs->ns_last_segment_lock);
587 sustat->ss_prot_seq = nilfs->ns_prot_seq;
588 spin_unlock(&nilfs->ns_last_segment_lock);
Cong Wang7b9c0972011-11-25 23:14:33 +0800589 kunmap_atomic(kaddr);
Koji Sato6c98cd42009-04-06 19:01:32 -0700590 brelse(header_bh);
591
592 out_sem:
593 up_read(&NILFS_MDT(sufile)->mi_sem);
594 return ret;
595}
596
Ryusuke Konishia7030182009-04-05 18:24:11 +0900597void nilfs_sufile_do_set_error(struct inode *sufile, __u64 segnum,
598 struct buffer_head *header_bh,
599 struct buffer_head *su_bh)
Koji Sato6c98cd42009-04-06 19:01:32 -0700600{
Koji Sato6c98cd42009-04-06 19:01:32 -0700601 struct nilfs_segment_usage *su;
Koji Sato6c98cd42009-04-06 19:01:32 -0700602 void *kaddr;
Ryusuke Konishia7030182009-04-05 18:24:11 +0900603 int suclean;
Koji Sato6c98cd42009-04-06 19:01:32 -0700604
Cong Wang7b9c0972011-11-25 23:14:33 +0800605 kaddr = kmap_atomic(su_bh->b_page);
Koji Sato6c98cd42009-04-06 19:01:32 -0700606 su = nilfs_sufile_block_get_segment_usage(sufile, segnum, su_bh, kaddr);
607 if (nilfs_segment_usage_error(su)) {
Cong Wang7b9c0972011-11-25 23:14:33 +0800608 kunmap_atomic(kaddr);
Ryusuke Konishia7030182009-04-05 18:24:11 +0900609 return;
Koji Sato6c98cd42009-04-06 19:01:32 -0700610 }
Ryusuke Konishi88072fa2009-04-05 15:03:16 +0900611 suclean = nilfs_segment_usage_clean(su);
Koji Sato6c98cd42009-04-06 19:01:32 -0700612 nilfs_segment_usage_set_error(su);
Cong Wang7b9c0972011-11-25 23:14:33 +0800613 kunmap_atomic(kaddr);
Koji Sato6c98cd42009-04-06 19:01:32 -0700614
Ryusuke Konishiaa474a22009-11-13 03:41:55 +0900615 if (suclean) {
Ryusuke Konishia7030182009-04-05 18:24:11 +0900616 nilfs_sufile_mod_counter(header_bh, -1, 0);
Ryusuke Konishiaa474a22009-11-13 03:41:55 +0900617 NILFS_SUI(sufile)->ncleansegs--;
618 }
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900619 mark_buffer_dirty(su_bh);
Koji Sato6c98cd42009-04-06 19:01:32 -0700620 nilfs_mdt_mark_dirty(sufile);
Koji Sato6c98cd42009-04-06 19:01:32 -0700621}
622
623/**
Ryusuke Konishi4d685f92017-11-17 15:29:43 -0800624 * nilfs_sufile_truncate_range - truncate range of segment array
625 * @sufile: inode of segment usage file
626 * @start: start segment number (inclusive)
627 * @end: end segment number (inclusive)
628 *
629 * Return Value: On success, 0 is returned. On error, one of the
630 * following negative error codes is returned.
631 *
632 * %-EIO - I/O error.
633 *
634 * %-ENOMEM - Insufficient amount of memory available.
635 *
636 * %-EINVAL - Invalid number of segments specified
637 *
638 * %-EBUSY - Dirty or active segments are present in the range
639 */
Ryusuke Konishi78eb64c2011-05-05 01:23:58 +0900640static int nilfs_sufile_truncate_range(struct inode *sufile,
641 __u64 start, __u64 end)
642{
643 struct the_nilfs *nilfs = sufile->i_sb->s_fs_info;
644 struct buffer_head *header_bh;
645 struct buffer_head *su_bh;
646 struct nilfs_segment_usage *su, *su2;
647 size_t susz = NILFS_MDT(sufile)->mi_entry_size;
648 unsigned long segusages_per_block;
649 unsigned long nsegs, ncleaned;
650 __u64 segnum;
651 void *kaddr;
652 ssize_t n, nc;
653 int ret;
654 int j;
655
656 nsegs = nilfs_sufile_get_nsegments(sufile);
657
658 ret = -EINVAL;
659 if (start > end || start >= nsegs)
660 goto out;
661
662 ret = nilfs_sufile_get_header_block(sufile, &header_bh);
663 if (ret < 0)
664 goto out;
665
666 segusages_per_block = nilfs_sufile_segment_usages_per_block(sufile);
667 ncleaned = 0;
668
669 for (segnum = start; segnum <= end; segnum += n) {
670 n = min_t(unsigned long,
671 segusages_per_block -
672 nilfs_sufile_get_offset(sufile, segnum),
673 end - segnum + 1);
674 ret = nilfs_sufile_get_segment_usage_block(sufile, segnum, 0,
675 &su_bh);
676 if (ret < 0) {
677 if (ret != -ENOENT)
678 goto out_header;
679 /* hole */
680 continue;
681 }
Cong Wang7b9c0972011-11-25 23:14:33 +0800682 kaddr = kmap_atomic(su_bh->b_page);
Ryusuke Konishi78eb64c2011-05-05 01:23:58 +0900683 su = nilfs_sufile_block_get_segment_usage(
684 sufile, segnum, su_bh, kaddr);
685 su2 = su;
686 for (j = 0; j < n; j++, su = (void *)su + susz) {
687 if ((le32_to_cpu(su->su_flags) &
Ryusuke Konishi4ce5c342016-08-02 14:05:28 -0700688 ~BIT(NILFS_SEGMENT_USAGE_ERROR)) ||
Ryusuke Konishi78eb64c2011-05-05 01:23:58 +0900689 nilfs_segment_is_active(nilfs, segnum + j)) {
690 ret = -EBUSY;
Cong Wang7b9c0972011-11-25 23:14:33 +0800691 kunmap_atomic(kaddr);
Ryusuke Konishi78eb64c2011-05-05 01:23:58 +0900692 brelse(su_bh);
693 goto out_header;
694 }
695 }
696 nc = 0;
697 for (su = su2, j = 0; j < n; j++, su = (void *)su + susz) {
698 if (nilfs_segment_usage_error(su)) {
699 nilfs_segment_usage_set_clean(su);
700 nc++;
701 }
702 }
Cong Wang7b9c0972011-11-25 23:14:33 +0800703 kunmap_atomic(kaddr);
Ryusuke Konishi78eb64c2011-05-05 01:23:58 +0900704 if (nc > 0) {
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900705 mark_buffer_dirty(su_bh);
Ryusuke Konishi78eb64c2011-05-05 01:23:58 +0900706 ncleaned += nc;
707 }
708 brelse(su_bh);
709
710 if (n == segusages_per_block) {
711 /* make hole */
712 nilfs_sufile_delete_segment_usage_block(sufile, segnum);
713 }
714 }
715 ret = 0;
716
717out_header:
718 if (ncleaned > 0) {
719 NILFS_SUI(sufile)->ncleansegs += ncleaned;
720 nilfs_sufile_mod_counter(header_bh, ncleaned, 0);
721 nilfs_mdt_mark_dirty(sufile);
722 }
723 brelse(header_bh);
724out:
725 return ret;
726}
727
728/**
Ryusuke Konishi4e33f9e2011-05-05 01:23:58 +0900729 * nilfs_sufile_resize - resize segment array
730 * @sufile: inode of segment usage file
731 * @newnsegs: new number of segments
732 *
733 * Return Value: On success, 0 is returned. On error, one of the
734 * following negative error codes is returned.
735 *
736 * %-EIO - I/O error.
737 *
738 * %-ENOMEM - Insufficient amount of memory available.
739 *
740 * %-ENOSPC - Enough free space is not left for shrinking
741 *
742 * %-EBUSY - Dirty or active segments exist in the region to be truncated
743 */
744int nilfs_sufile_resize(struct inode *sufile, __u64 newnsegs)
745{
746 struct the_nilfs *nilfs = sufile->i_sb->s_fs_info;
747 struct buffer_head *header_bh;
748 struct nilfs_sufile_header *header;
749 struct nilfs_sufile_info *sui = NILFS_SUI(sufile);
750 void *kaddr;
751 unsigned long nsegs, nrsvsegs;
752 int ret = 0;
753
754 down_write(&NILFS_MDT(sufile)->mi_sem);
755
756 nsegs = nilfs_sufile_get_nsegments(sufile);
757 if (nsegs == newnsegs)
758 goto out;
759
760 ret = -ENOSPC;
761 nrsvsegs = nilfs_nrsvsegs(nilfs, newnsegs);
762 if (newnsegs < nsegs && nsegs - newnsegs + nrsvsegs > sui->ncleansegs)
763 goto out;
764
765 ret = nilfs_sufile_get_header_block(sufile, &header_bh);
766 if (ret < 0)
767 goto out;
768
769 if (newnsegs > nsegs) {
770 sui->ncleansegs += newnsegs - nsegs;
771 } else /* newnsegs < nsegs */ {
772 ret = nilfs_sufile_truncate_range(sufile, newnsegs, nsegs - 1);
773 if (ret < 0)
774 goto out_header;
775
776 sui->ncleansegs -= nsegs - newnsegs;
777 }
778
Cong Wang7b9c0972011-11-25 23:14:33 +0800779 kaddr = kmap_atomic(header_bh->b_page);
Ryusuke Konishi4e33f9e2011-05-05 01:23:58 +0900780 header = kaddr + bh_offset(header_bh);
781 header->sh_ncleansegs = cpu_to_le64(sui->ncleansegs);
Cong Wang7b9c0972011-11-25 23:14:33 +0800782 kunmap_atomic(kaddr);
Ryusuke Konishi4e33f9e2011-05-05 01:23:58 +0900783
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900784 mark_buffer_dirty(header_bh);
Ryusuke Konishi4e33f9e2011-05-05 01:23:58 +0900785 nilfs_mdt_mark_dirty(sufile);
786 nilfs_set_nsegments(nilfs, newnsegs);
787
788out_header:
789 brelse(header_bh);
790out:
791 up_write(&NILFS_MDT(sufile)->mi_sem);
792 return ret;
793}
794
795/**
Koji Sato6c98cd42009-04-06 19:01:32 -0700796 * nilfs_sufile_get_suinfo -
797 * @sufile: inode of segment usage file
798 * @segnum: segment number to start looking
Ryusuke Konishi003ff182009-05-12 03:58:47 +0900799 * @buf: array of suinfo
800 * @sisz: byte size of suinfo
Koji Sato6c98cd42009-04-06 19:01:32 -0700801 * @nsi: size of suinfo array
802 *
803 * Description:
804 *
805 * Return Value: On success, 0 is returned and .... On error, one of the
806 * following negative error codes is returned.
807 *
808 * %-EIO - I/O error.
809 *
810 * %-ENOMEM - Insufficient amount of memory available.
811 */
Ryusuke Konishi003ff182009-05-12 03:58:47 +0900812ssize_t nilfs_sufile_get_suinfo(struct inode *sufile, __u64 segnum, void *buf,
Ryusuke Konishif19e78d2016-05-23 16:23:03 -0700813 unsigned int sisz, size_t nsi)
Koji Sato6c98cd42009-04-06 19:01:32 -0700814{
815 struct buffer_head *su_bh;
816 struct nilfs_segment_usage *su;
Ryusuke Konishi003ff182009-05-12 03:58:47 +0900817 struct nilfs_suinfo *si = buf;
Koji Sato6c98cd42009-04-06 19:01:32 -0700818 size_t susz = NILFS_MDT(sufile)->mi_entry_size;
Ryusuke Konishi0ef28f92011-05-05 12:56:51 +0900819 struct the_nilfs *nilfs = sufile->i_sb->s_fs_info;
Koji Sato6c98cd42009-04-06 19:01:32 -0700820 void *kaddr;
821 unsigned long nsegs, segusages_per_block;
822 ssize_t n;
823 int ret, i, j;
824
825 down_read(&NILFS_MDT(sufile)->mi_sem);
826
827 segusages_per_block = nilfs_sufile_segment_usages_per_block(sufile);
828 nsegs = min_t(unsigned long,
829 nilfs_sufile_get_nsegments(sufile) - segnum,
830 nsi);
831 for (i = 0; i < nsegs; i += n, segnum += n) {
832 n = min_t(unsigned long,
833 segusages_per_block -
834 nilfs_sufile_get_offset(sufile, segnum),
835 nsegs - i);
836 ret = nilfs_sufile_get_segment_usage_block(sufile, segnum, 0,
837 &su_bh);
838 if (ret < 0) {
839 if (ret != -ENOENT)
840 goto out;
841 /* hole */
Ryusuke Konishi003ff182009-05-12 03:58:47 +0900842 memset(si, 0, sisz * n);
843 si = (void *)si + sisz * n;
Koji Sato6c98cd42009-04-06 19:01:32 -0700844 continue;
845 }
846
Cong Wang7b9c0972011-11-25 23:14:33 +0800847 kaddr = kmap_atomic(su_bh->b_page);
Koji Sato6c98cd42009-04-06 19:01:32 -0700848 su = nilfs_sufile_block_get_segment_usage(
849 sufile, segnum, su_bh, kaddr);
Ryusuke Konishi003ff182009-05-12 03:58:47 +0900850 for (j = 0; j < n;
851 j++, su = (void *)su + susz, si = (void *)si + sisz) {
852 si->sui_lastmod = le64_to_cpu(su->su_lastmod);
853 si->sui_nblocks = le32_to_cpu(su->su_nblocks);
854 si->sui_flags = le32_to_cpu(su->su_flags) &
Ryusuke Konishi4ce5c342016-08-02 14:05:28 -0700855 ~BIT(NILFS_SEGMENT_USAGE_ACTIVE);
Ryusuke Konishi3efb55b2009-03-30 00:50:19 +0900856 if (nilfs_segment_is_active(nilfs, segnum + j))
Ryusuke Konishi003ff182009-05-12 03:58:47 +0900857 si->sui_flags |=
Ryusuke Konishi4ce5c342016-08-02 14:05:28 -0700858 BIT(NILFS_SEGMENT_USAGE_ACTIVE);
Koji Sato6c98cd42009-04-06 19:01:32 -0700859 }
Cong Wang7b9c0972011-11-25 23:14:33 +0800860 kunmap_atomic(kaddr);
Koji Sato6c98cd42009-04-06 19:01:32 -0700861 brelse(su_bh);
862 }
863 ret = nsegs;
864
865 out:
866 up_read(&NILFS_MDT(sufile)->mi_sem);
867 return ret;
868}
Ryusuke Konishi79739562009-11-12 23:56:43 +0900869
870/**
Andreas Rohner00e9ffc2014-04-03 14:50:27 -0700871 * nilfs_sufile_set_suinfo - sets segment usage info
872 * @sufile: inode of segment usage file
873 * @buf: array of suinfo_update
874 * @supsz: byte size of suinfo_update
875 * @nsup: size of suinfo_update array
876 *
877 * Description: Takes an array of nilfs_suinfo_update structs and updates
878 * segment usage accordingly. Only the fields indicated by the sup_flags
879 * are updated.
880 *
881 * Return Value: On success, 0 is returned. On error, one of the
882 * following negative error codes is returned.
883 *
884 * %-EIO - I/O error.
885 *
886 * %-ENOMEM - Insufficient amount of memory available.
887 *
888 * %-EINVAL - Invalid values in input (segment number, flags or nblocks)
889 */
890ssize_t nilfs_sufile_set_suinfo(struct inode *sufile, void *buf,
Ryusuke Konishif19e78d2016-05-23 16:23:03 -0700891 unsigned int supsz, size_t nsup)
Andreas Rohner00e9ffc2014-04-03 14:50:27 -0700892{
893 struct the_nilfs *nilfs = sufile->i_sb->s_fs_info;
894 struct buffer_head *header_bh, *bh;
895 struct nilfs_suinfo_update *sup, *supend = buf + supsz * nsup;
896 struct nilfs_segment_usage *su;
897 void *kaddr;
898 unsigned long blkoff, prev_blkoff;
899 int cleansi, cleansu, dirtysi, dirtysu;
900 long ncleaned = 0, ndirtied = 0;
901 int ret = 0;
902
903 if (unlikely(nsup == 0))
904 return ret;
905
906 for (sup = buf; sup < supend; sup = (void *)sup + supsz) {
907 if (sup->sup_segnum >= nilfs->ns_nsegments
908 || (sup->sup_flags &
909 (~0UL << __NR_NILFS_SUINFO_UPDATE_FIELDS))
910 || (nilfs_suinfo_update_nblocks(sup) &&
911 sup->sup_sui.sui_nblocks >
912 nilfs->ns_blocks_per_segment))
913 return -EINVAL;
914 }
915
916 down_write(&NILFS_MDT(sufile)->mi_sem);
917
918 ret = nilfs_sufile_get_header_block(sufile, &header_bh);
919 if (ret < 0)
920 goto out_sem;
921
922 sup = buf;
923 blkoff = nilfs_sufile_get_blkoff(sufile, sup->sup_segnum);
924 ret = nilfs_mdt_get_block(sufile, blkoff, 1, NULL, &bh);
925 if (ret < 0)
926 goto out_header;
927
928 for (;;) {
929 kaddr = kmap_atomic(bh->b_page);
930 su = nilfs_sufile_block_get_segment_usage(
931 sufile, sup->sup_segnum, bh, kaddr);
932
933 if (nilfs_suinfo_update_lastmod(sup))
934 su->su_lastmod = cpu_to_le64(sup->sup_sui.sui_lastmod);
935
936 if (nilfs_suinfo_update_nblocks(sup))
937 su->su_nblocks = cpu_to_le32(sup->sup_sui.sui_nblocks);
938
939 if (nilfs_suinfo_update_flags(sup)) {
940 /*
941 * Active flag is a virtual flag projected by running
942 * nilfs kernel code - drop it not to write it to
943 * disk.
944 */
945 sup->sup_sui.sui_flags &=
Ryusuke Konishi4ce5c342016-08-02 14:05:28 -0700946 ~BIT(NILFS_SEGMENT_USAGE_ACTIVE);
Andreas Rohner00e9ffc2014-04-03 14:50:27 -0700947
948 cleansi = nilfs_suinfo_clean(&sup->sup_sui);
949 cleansu = nilfs_segment_usage_clean(su);
950 dirtysi = nilfs_suinfo_dirty(&sup->sup_sui);
951 dirtysu = nilfs_segment_usage_dirty(su);
952
953 if (cleansi && !cleansu)
954 ++ncleaned;
955 else if (!cleansi && cleansu)
956 --ncleaned;
957
958 if (dirtysi && !dirtysu)
959 ++ndirtied;
960 else if (!dirtysi && dirtysu)
961 --ndirtied;
962
963 su->su_flags = cpu_to_le32(sup->sup_sui.sui_flags);
964 }
965
966 kunmap_atomic(kaddr);
967
968 sup = (void *)sup + supsz;
969 if (sup >= supend)
970 break;
971
972 prev_blkoff = blkoff;
973 blkoff = nilfs_sufile_get_blkoff(sufile, sup->sup_segnum);
974 if (blkoff == prev_blkoff)
975 continue;
976
977 /* get different block */
978 mark_buffer_dirty(bh);
979 put_bh(bh);
980 ret = nilfs_mdt_get_block(sufile, blkoff, 1, NULL, &bh);
981 if (unlikely(ret < 0))
982 goto out_mark;
983 }
984 mark_buffer_dirty(bh);
985 put_bh(bh);
986
987 out_mark:
988 if (ncleaned || ndirtied) {
989 nilfs_sufile_mod_counter(header_bh, (u64)ncleaned,
990 (u64)ndirtied);
991 NILFS_SUI(sufile)->ncleansegs += ncleaned;
992 }
993 nilfs_mdt_mark_dirty(sufile);
994 out_header:
995 put_bh(header_bh);
996 out_sem:
997 up_write(&NILFS_MDT(sufile)->mi_sem);
998 return ret;
999}
1000
1001/**
Andreas Rohner82e11e82014-04-03 14:50:29 -07001002 * nilfs_sufile_trim_fs() - trim ioctl handle function
1003 * @sufile: inode of segment usage file
1004 * @range: fstrim_range structure
1005 *
1006 * start: First Byte to trim
1007 * len: number of Bytes to trim from start
1008 * minlen: minimum extent length in Bytes
1009 *
1010 * Decription: nilfs_sufile_trim_fs goes through all segments containing bytes
1011 * from start to start+len. start is rounded up to the next block boundary
1012 * and start+len is rounded down. For each clean segment blkdev_issue_discard
1013 * function is invoked.
1014 *
1015 * Return Value: On success, 0 is returned or negative error code, otherwise.
1016 */
1017int nilfs_sufile_trim_fs(struct inode *sufile, struct fstrim_range *range)
1018{
1019 struct the_nilfs *nilfs = sufile->i_sb->s_fs_info;
1020 struct buffer_head *su_bh;
1021 struct nilfs_segment_usage *su;
1022 void *kaddr;
1023 size_t n, i, susz = NILFS_MDT(sufile)->mi_entry_size;
1024 sector_t seg_start, seg_end, start_block, end_block;
1025 sector_t start = 0, nblocks = 0;
1026 u64 segnum, segnum_end, minlen, len, max_blocks, ndiscarded = 0;
1027 int ret = 0;
1028 unsigned int sects_per_block;
1029
1030 sects_per_block = (1 << nilfs->ns_blocksize_bits) /
1031 bdev_logical_block_size(nilfs->ns_bdev);
1032 len = range->len >> nilfs->ns_blocksize_bits;
1033 minlen = range->minlen >> nilfs->ns_blocksize_bits;
1034 max_blocks = ((u64)nilfs->ns_nsegments * nilfs->ns_blocks_per_segment);
1035
1036 if (!len || range->start >= max_blocks << nilfs->ns_blocksize_bits)
1037 return -EINVAL;
1038
1039 start_block = (range->start + nilfs->ns_blocksize - 1) >>
1040 nilfs->ns_blocksize_bits;
1041
1042 /*
1043 * range->len can be very large (actually, it is set to
1044 * ULLONG_MAX by default) - truncate upper end of the range
1045 * carefully so as not to overflow.
1046 */
1047 if (max_blocks - start_block < len)
1048 end_block = max_blocks - 1;
1049 else
1050 end_block = start_block + len - 1;
1051
1052 segnum = nilfs_get_segnum_of_block(nilfs, start_block);
1053 segnum_end = nilfs_get_segnum_of_block(nilfs, end_block);
1054
1055 down_read(&NILFS_MDT(sufile)->mi_sem);
1056
1057 while (segnum <= segnum_end) {
1058 n = nilfs_sufile_segment_usages_in_block(sufile, segnum,
1059 segnum_end);
1060
1061 ret = nilfs_sufile_get_segment_usage_block(sufile, segnum, 0,
1062 &su_bh);
1063 if (ret < 0) {
1064 if (ret != -ENOENT)
1065 goto out_sem;
1066 /* hole */
1067 segnum += n;
1068 continue;
1069 }
1070
1071 kaddr = kmap_atomic(su_bh->b_page);
1072 su = nilfs_sufile_block_get_segment_usage(sufile, segnum,
1073 su_bh, kaddr);
1074 for (i = 0; i < n; ++i, ++segnum, su = (void *)su + susz) {
1075 if (!nilfs_segment_usage_clean(su))
1076 continue;
1077
1078 nilfs_get_segment_range(nilfs, segnum, &seg_start,
1079 &seg_end);
1080
1081 if (!nblocks) {
1082 /* start new extent */
1083 start = seg_start;
1084 nblocks = seg_end - seg_start + 1;
1085 continue;
1086 }
1087
1088 if (start + nblocks == seg_start) {
1089 /* add to previous extent */
1090 nblocks += seg_end - seg_start + 1;
1091 continue;
1092 }
1093
1094 /* discard previous extent */
1095 if (start < start_block) {
1096 nblocks -= start_block - start;
1097 start = start_block;
1098 }
1099
1100 if (nblocks >= minlen) {
1101 kunmap_atomic(kaddr);
1102
1103 ret = blkdev_issue_discard(nilfs->ns_bdev,
1104 start * sects_per_block,
1105 nblocks * sects_per_block,
1106 GFP_NOFS, 0);
1107 if (ret < 0) {
1108 put_bh(su_bh);
1109 goto out_sem;
1110 }
1111
1112 ndiscarded += nblocks;
1113 kaddr = kmap_atomic(su_bh->b_page);
1114 su = nilfs_sufile_block_get_segment_usage(
1115 sufile, segnum, su_bh, kaddr);
1116 }
1117
1118 /* start new extent */
1119 start = seg_start;
1120 nblocks = seg_end - seg_start + 1;
1121 }
1122 kunmap_atomic(kaddr);
1123 put_bh(su_bh);
1124 }
1125
1126
1127 if (nblocks) {
1128 /* discard last extent */
1129 if (start < start_block) {
1130 nblocks -= start_block - start;
1131 start = start_block;
1132 }
1133 if (start + nblocks > end_block + 1)
1134 nblocks = end_block - start + 1;
1135
1136 if (nblocks >= minlen) {
1137 ret = blkdev_issue_discard(nilfs->ns_bdev,
1138 start * sects_per_block,
1139 nblocks * sects_per_block,
1140 GFP_NOFS, 0);
1141 if (!ret)
1142 ndiscarded += nblocks;
1143 }
1144 }
1145
1146out_sem:
1147 up_read(&NILFS_MDT(sufile)->mi_sem);
1148
1149 range->len = ndiscarded << nilfs->ns_blocksize_bits;
1150 return ret;
1151}
1152
1153/**
Ryusuke Konishif1e89c82010-09-05 12:20:59 +09001154 * nilfs_sufile_read - read or get sufile inode
1155 * @sb: super block instance
1156 * @susize: size of a segment usage entry
Ryusuke Konishi8707df32009-11-13 01:36:56 +09001157 * @raw_inode: on-disk sufile inode
Ryusuke Konishif1e89c82010-09-05 12:20:59 +09001158 * @inodep: buffer to store the inode
Ryusuke Konishi8707df32009-11-13 01:36:56 +09001159 */
Ryusuke Konishif1e89c82010-09-05 12:20:59 +09001160int nilfs_sufile_read(struct super_block *sb, size_t susize,
1161 struct nilfs_inode *raw_inode, struct inode **inodep)
Ryusuke Konishi8707df32009-11-13 01:36:56 +09001162{
Ryusuke Konishif1e89c82010-09-05 12:20:59 +09001163 struct inode *sufile;
1164 struct nilfs_sufile_info *sui;
Ryusuke Konishiaa474a22009-11-13 03:41:55 +09001165 struct buffer_head *header_bh;
1166 struct nilfs_sufile_header *header;
1167 void *kaddr;
Ryusuke Konishif1e89c82010-09-05 12:20:59 +09001168 int err;
Ryusuke Konishiaa474a22009-11-13 03:41:55 +09001169
Ryusuke Konishi0ec060d2014-04-03 14:50:31 -07001170 if (susize > sb->s_blocksize) {
Ryusuke Konishifeee8802016-08-02 14:05:10 -07001171 nilfs_msg(sb, KERN_ERR,
1172 "too large segment usage size: %zu bytes", susize);
Ryusuke Konishi0ec060d2014-04-03 14:50:31 -07001173 return -EINVAL;
1174 } else if (susize < NILFS_MIN_SEGMENT_USAGE_SIZE) {
Ryusuke Konishifeee8802016-08-02 14:05:10 -07001175 nilfs_msg(sb, KERN_ERR,
1176 "too small segment usage size: %zu bytes", susize);
Ryusuke Konishi0ec060d2014-04-03 14:50:31 -07001177 return -EINVAL;
1178 }
1179
Ryusuke Konishif1e89c82010-09-05 12:20:59 +09001180 sufile = nilfs_iget_locked(sb, NULL, NILFS_SUFILE_INO);
1181 if (unlikely(!sufile))
1182 return -ENOMEM;
1183 if (!(sufile->i_state & I_NEW))
1184 goto out;
Ryusuke Konishiaa474a22009-11-13 03:41:55 +09001185
Ryusuke Konishif1e89c82010-09-05 12:20:59 +09001186 err = nilfs_mdt_init(sufile, NILFS_MDT_GFP, sizeof(*sui));
1187 if (err)
1188 goto failed;
Ryusuke Konishi8707df32009-11-13 01:36:56 +09001189
Ryusuke Konishif1e89c82010-09-05 12:20:59 +09001190 nilfs_mdt_set_entry_size(sufile, susize,
1191 sizeof(struct nilfs_sufile_header));
Ryusuke Konishi79739562009-11-12 23:56:43 +09001192
Ryusuke Konishif1e89c82010-09-05 12:20:59 +09001193 err = nilfs_read_inode_common(sufile, raw_inode);
1194 if (err)
1195 goto failed;
1196
1197 err = nilfs_sufile_get_header_block(sufile, &header_bh);
1198 if (err)
1199 goto failed;
1200
1201 sui = NILFS_SUI(sufile);
Cong Wang7b9c0972011-11-25 23:14:33 +08001202 kaddr = kmap_atomic(header_bh->b_page);
Ryusuke Konishif1e89c82010-09-05 12:20:59 +09001203 header = kaddr + bh_offset(header_bh);
1204 sui->ncleansegs = le64_to_cpu(header->sh_ncleansegs);
Cong Wang7b9c0972011-11-25 23:14:33 +08001205 kunmap_atomic(kaddr);
Ryusuke Konishif1e89c82010-09-05 12:20:59 +09001206 brelse(header_bh);
1207
Ryusuke Konishi619205d2011-05-05 01:23:57 +09001208 sui->allocmax = nilfs_sufile_get_nsegments(sufile) - 1;
1209 sui->allocmin = 0;
1210
Ryusuke Konishif1e89c82010-09-05 12:20:59 +09001211 unlock_new_inode(sufile);
1212 out:
1213 *inodep = sufile;
1214 return 0;
1215 failed:
1216 iget_failed(sufile);
1217 return err;
Ryusuke Konishi79739562009-11-12 23:56:43 +09001218}