blob: 7ff8f15207ab44840c9bf3b65480c38ae3e60245 [file] [log] [blame]
Koji Sato6c98cd42009-04-06 19:01:32 -07001/*
2 * sufile.c - NILFS segment usage file.
3 *
4 * Copyright (C) 2006-2008 Nippon Telegraph and Telephone Corporation.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 *
20 * Written by Koji Sato <koji@osrg.net>.
Ryusuke Konishi7a650042010-03-14 03:32:40 +090021 * Revised by Ryusuke Konishi <ryusuke@osrg.net>.
Koji Sato6c98cd42009-04-06 19:01:32 -070022 */
23
24#include <linux/kernel.h>
25#include <linux/fs.h>
26#include <linux/string.h>
27#include <linux/buffer_head.h>
28#include <linux/errno.h>
29#include <linux/nilfs2_fs.h>
30#include "mdt.h"
31#include "sufile.h"
32
Hitoshi Mitake83eec5e2015-11-06 16:32:05 -080033#include <trace/events/nilfs2.h>
34
Vyacheslav Dubeykof5974c82012-07-30 14:42:10 -070035/**
36 * struct nilfs_sufile_info - on-memory private data of sufile
37 * @mi: on-memory private data of metadata file
38 * @ncleansegs: number of clean segments
39 * @allocmin: lower limit of allocatable segment range
40 * @allocmax: upper limit of allocatable segment range
41 */
Ryusuke Konishiaa474a22009-11-13 03:41:55 +090042struct nilfs_sufile_info {
43 struct nilfs_mdt_info mi;
Ryusuke Konishi619205d2011-05-05 01:23:57 +090044 unsigned long ncleansegs;/* number of clean segments */
45 __u64 allocmin; /* lower limit of allocatable segment range */
46 __u64 allocmax; /* upper limit of allocatable segment range */
Ryusuke Konishiaa474a22009-11-13 03:41:55 +090047};
48
49static inline struct nilfs_sufile_info *NILFS_SUI(struct inode *sufile)
50{
51 return (struct nilfs_sufile_info *)NILFS_MDT(sufile);
52}
53
Koji Sato6c98cd42009-04-06 19:01:32 -070054static inline unsigned long
55nilfs_sufile_segment_usages_per_block(const struct inode *sufile)
56{
57 return NILFS_MDT(sufile)->mi_entries_per_block;
58}
59
60static unsigned long
61nilfs_sufile_get_blkoff(const struct inode *sufile, __u64 segnum)
62{
63 __u64 t = segnum + NILFS_MDT(sufile)->mi_first_entry_offset;
64 do_div(t, nilfs_sufile_segment_usages_per_block(sufile));
65 return (unsigned long)t;
66}
67
68static unsigned long
69nilfs_sufile_get_offset(const struct inode *sufile, __u64 segnum)
70{
71 __u64 t = segnum + NILFS_MDT(sufile)->mi_first_entry_offset;
72 return do_div(t, nilfs_sufile_segment_usages_per_block(sufile));
73}
74
75static unsigned long
76nilfs_sufile_segment_usages_in_block(const struct inode *sufile, __u64 curr,
77 __u64 max)
78{
79 return min_t(unsigned long,
80 nilfs_sufile_segment_usages_per_block(sufile) -
81 nilfs_sufile_get_offset(sufile, curr),
82 max - curr + 1);
83}
84
Koji Sato6c98cd42009-04-06 19:01:32 -070085static struct nilfs_segment_usage *
86nilfs_sufile_block_get_segment_usage(const struct inode *sufile, __u64 segnum,
87 struct buffer_head *bh, void *kaddr)
88{
89 return kaddr + bh_offset(bh) +
90 nilfs_sufile_get_offset(sufile, segnum) *
91 NILFS_MDT(sufile)->mi_entry_size;
92}
93
94static inline int nilfs_sufile_get_header_block(struct inode *sufile,
95 struct buffer_head **bhp)
96{
97 return nilfs_mdt_get_block(sufile, 0, 0, NULL, bhp);
98}
99
100static inline int
101nilfs_sufile_get_segment_usage_block(struct inode *sufile, __u64 segnum,
102 int create, struct buffer_head **bhp)
103{
104 return nilfs_mdt_get_block(sufile,
105 nilfs_sufile_get_blkoff(sufile, segnum),
106 create, NULL, bhp);
107}
108
Ryusuke Konishi78eb64c2011-05-05 01:23:58 +0900109static int nilfs_sufile_delete_segment_usage_block(struct inode *sufile,
110 __u64 segnum)
111{
112 return nilfs_mdt_delete_block(sufile,
113 nilfs_sufile_get_blkoff(sufile, segnum));
114}
115
Ryusuke Konishia7030182009-04-05 18:24:11 +0900116static void nilfs_sufile_mod_counter(struct buffer_head *header_bh,
117 u64 ncleanadd, u64 ndirtyadd)
118{
119 struct nilfs_sufile_header *header;
120 void *kaddr;
121
Cong Wang7b9c0972011-11-25 23:14:33 +0800122 kaddr = kmap_atomic(header_bh->b_page);
Ryusuke Konishia7030182009-04-05 18:24:11 +0900123 header = kaddr + bh_offset(header_bh);
124 le64_add_cpu(&header->sh_ncleansegs, ncleanadd);
125 le64_add_cpu(&header->sh_ndirtysegs, ndirtyadd);
Cong Wang7b9c0972011-11-25 23:14:33 +0800126 kunmap_atomic(kaddr);
Ryusuke Konishia7030182009-04-05 18:24:11 +0900127
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900128 mark_buffer_dirty(header_bh);
Ryusuke Konishia7030182009-04-05 18:24:11 +0900129}
130
Ryusuke Konishidda54f42009-05-16 21:49:10 +0900131/**
Ryusuke Konishief7d4752009-11-13 08:45:32 +0900132 * nilfs_sufile_get_ncleansegs - return the number of clean segments
133 * @sufile: inode of segment usage file
134 */
135unsigned long nilfs_sufile_get_ncleansegs(struct inode *sufile)
136{
137 return NILFS_SUI(sufile)->ncleansegs;
138}
139
140/**
Ryusuke Konishidda54f42009-05-16 21:49:10 +0900141 * nilfs_sufile_updatev - modify multiple segment usages at a time
142 * @sufile: inode of segment usage file
143 * @segnumv: array of segment numbers
144 * @nsegs: size of @segnumv array
145 * @create: creation flag
146 * @ndone: place to store number of modified segments on @segnumv
147 * @dofunc: primitive operation for the update
148 *
149 * Description: nilfs_sufile_updatev() repeatedly calls @dofunc
150 * against the given array of segments. The @dofunc is called with
151 * buffers of a header block and the sufile block in which the target
152 * segment usage entry is contained. If @ndone is given, the number
153 * of successfully modified segments from the head is stored in the
154 * place @ndone points to.
155 *
156 * Return Value: On success, zero is returned. On error, one of the
157 * following negative error codes is returned.
158 *
159 * %-EIO - I/O error.
160 *
161 * %-ENOMEM - Insufficient amount of memory available.
162 *
163 * %-ENOENT - Given segment usage is in hole block (may be returned if
164 * @create is zero)
165 *
166 * %-EINVAL - Invalid segment usage number
167 */
168int nilfs_sufile_updatev(struct inode *sufile, __u64 *segnumv, size_t nsegs,
169 int create, size_t *ndone,
170 void (*dofunc)(struct inode *, __u64,
171 struct buffer_head *,
172 struct buffer_head *))
173{
174 struct buffer_head *header_bh, *bh;
175 unsigned long blkoff, prev_blkoff;
176 __u64 *seg;
177 size_t nerr = 0, n = 0;
178 int ret = 0;
179
180 if (unlikely(nsegs == 0))
181 goto out;
182
183 down_write(&NILFS_MDT(sufile)->mi_sem);
184 for (seg = segnumv; seg < segnumv + nsegs; seg++) {
185 if (unlikely(*seg >= nilfs_sufile_get_nsegments(sufile))) {
186 printk(KERN_WARNING
187 "%s: invalid segment number: %llu\n", __func__,
188 (unsigned long long)*seg);
189 nerr++;
190 }
191 }
192 if (nerr > 0) {
193 ret = -EINVAL;
194 goto out_sem;
195 }
196
197 ret = nilfs_sufile_get_header_block(sufile, &header_bh);
198 if (ret < 0)
199 goto out_sem;
200
201 seg = segnumv;
202 blkoff = nilfs_sufile_get_blkoff(sufile, *seg);
203 ret = nilfs_mdt_get_block(sufile, blkoff, create, NULL, &bh);
204 if (ret < 0)
205 goto out_header;
206
207 for (;;) {
208 dofunc(sufile, *seg, header_bh, bh);
209
210 if (++seg >= segnumv + nsegs)
211 break;
212 prev_blkoff = blkoff;
213 blkoff = nilfs_sufile_get_blkoff(sufile, *seg);
214 if (blkoff == prev_blkoff)
215 continue;
216
217 /* get different block */
218 brelse(bh);
219 ret = nilfs_mdt_get_block(sufile, blkoff, create, NULL, &bh);
220 if (unlikely(ret < 0))
221 goto out_header;
222 }
223 brelse(bh);
224
225 out_header:
226 n = seg - segnumv;
227 brelse(header_bh);
228 out_sem:
229 up_write(&NILFS_MDT(sufile)->mi_sem);
230 out:
231 if (ndone)
232 *ndone = n;
233 return ret;
234}
235
Ryusuke Konishia7030182009-04-05 18:24:11 +0900236int nilfs_sufile_update(struct inode *sufile, __u64 segnum, int create,
237 void (*dofunc)(struct inode *, __u64,
238 struct buffer_head *,
239 struct buffer_head *))
240{
241 struct buffer_head *header_bh, *bh;
242 int ret;
243
244 if (unlikely(segnum >= nilfs_sufile_get_nsegments(sufile))) {
245 printk(KERN_WARNING "%s: invalid segment number: %llu\n",
246 __func__, (unsigned long long)segnum);
247 return -EINVAL;
248 }
249 down_write(&NILFS_MDT(sufile)->mi_sem);
250
251 ret = nilfs_sufile_get_header_block(sufile, &header_bh);
252 if (ret < 0)
253 goto out_sem;
254
255 ret = nilfs_sufile_get_segment_usage_block(sufile, segnum, create, &bh);
256 if (!ret) {
257 dofunc(sufile, segnum, header_bh, bh);
258 brelse(bh);
259 }
260 brelse(header_bh);
261
262 out_sem:
263 up_write(&NILFS_MDT(sufile)->mi_sem);
264 return ret;
265}
266
Koji Sato6c98cd42009-04-06 19:01:32 -0700267/**
Ryusuke Konishi619205d2011-05-05 01:23:57 +0900268 * nilfs_sufile_set_alloc_range - limit range of segment to be allocated
269 * @sufile: inode of segment usage file
270 * @start: minimum segment number of allocatable region (inclusive)
271 * @end: maximum segment number of allocatable region (inclusive)
272 *
273 * Return Value: On success, 0 is returned. On error, one of the
274 * following negative error codes is returned.
275 *
276 * %-ERANGE - invalid segment region
277 */
278int nilfs_sufile_set_alloc_range(struct inode *sufile, __u64 start, __u64 end)
279{
280 struct nilfs_sufile_info *sui = NILFS_SUI(sufile);
281 __u64 nsegs;
282 int ret = -ERANGE;
283
284 down_write(&NILFS_MDT(sufile)->mi_sem);
285 nsegs = nilfs_sufile_get_nsegments(sufile);
286
287 if (start <= end && end < nsegs) {
288 sui->allocmin = start;
289 sui->allocmax = end;
290 ret = 0;
291 }
292 up_write(&NILFS_MDT(sufile)->mi_sem);
293 return ret;
294}
295
296/**
Koji Sato6c98cd42009-04-06 19:01:32 -0700297 * nilfs_sufile_alloc - allocate a segment
298 * @sufile: inode of segment usage file
299 * @segnump: pointer to segment number
300 *
301 * Description: nilfs_sufile_alloc() allocates a clean segment.
302 *
303 * Return Value: On success, 0 is returned and the segment number of the
304 * allocated segment is stored in the place pointed by @segnump. On error, one
305 * of the following negative error codes is returned.
306 *
307 * %-EIO - I/O error.
308 *
309 * %-ENOMEM - Insufficient amount of memory available.
310 *
311 * %-ENOSPC - No clean segment left.
312 */
313int nilfs_sufile_alloc(struct inode *sufile, __u64 *segnump)
314{
315 struct buffer_head *header_bh, *su_bh;
Koji Sato6c98cd42009-04-06 19:01:32 -0700316 struct nilfs_sufile_header *header;
317 struct nilfs_segment_usage *su;
Ryusuke Konishi619205d2011-05-05 01:23:57 +0900318 struct nilfs_sufile_info *sui = NILFS_SUI(sufile);
Koji Sato6c98cd42009-04-06 19:01:32 -0700319 size_t susz = NILFS_MDT(sufile)->mi_entry_size;
320 __u64 segnum, maxsegnum, last_alloc;
321 void *kaddr;
Ryusuke Konishi619205d2011-05-05 01:23:57 +0900322 unsigned long nsegments, ncleansegs, nsus, cnt;
323 int ret, j;
Koji Sato6c98cd42009-04-06 19:01:32 -0700324
325 down_write(&NILFS_MDT(sufile)->mi_sem);
326
Koji Sato6c98cd42009-04-06 19:01:32 -0700327 ret = nilfs_sufile_get_header_block(sufile, &header_bh);
328 if (ret < 0)
329 goto out_sem;
Cong Wang7b9c0972011-11-25 23:14:33 +0800330 kaddr = kmap_atomic(header_bh->b_page);
Ryusuke Konishi7b16c8a2009-11-13 03:10:21 +0900331 header = kaddr + bh_offset(header_bh);
Koji Sato6c98cd42009-04-06 19:01:32 -0700332 ncleansegs = le64_to_cpu(header->sh_ncleansegs);
333 last_alloc = le64_to_cpu(header->sh_last_alloc);
Cong Wang7b9c0972011-11-25 23:14:33 +0800334 kunmap_atomic(kaddr);
Koji Sato6c98cd42009-04-06 19:01:32 -0700335
336 nsegments = nilfs_sufile_get_nsegments(sufile);
Ryusuke Konishi619205d2011-05-05 01:23:57 +0900337 maxsegnum = sui->allocmax;
Koji Sato6c98cd42009-04-06 19:01:32 -0700338 segnum = last_alloc + 1;
Ryusuke Konishi619205d2011-05-05 01:23:57 +0900339 if (segnum < sui->allocmin || segnum > sui->allocmax)
340 segnum = sui->allocmin;
341
342 for (cnt = 0; cnt < nsegments; cnt += nsus) {
343 if (segnum > maxsegnum) {
344 if (cnt < sui->allocmax - sui->allocmin + 1) {
345 /*
346 * wrap around in the limited region.
347 * if allocation started from
348 * sui->allocmin, this never happens.
349 */
350 segnum = sui->allocmin;
351 maxsegnum = last_alloc;
352 } else if (segnum > sui->allocmin &&
353 sui->allocmax + 1 < nsegments) {
354 segnum = sui->allocmax + 1;
355 maxsegnum = nsegments - 1;
356 } else if (sui->allocmin > 0) {
357 segnum = 0;
358 maxsegnum = sui->allocmin - 1;
359 } else {
360 break; /* never happens */
361 }
Koji Sato6c98cd42009-04-06 19:01:32 -0700362 }
Hitoshi Mitake83eec5e2015-11-06 16:32:05 -0800363 trace_nilfs2_segment_usage_check(sufile, segnum, cnt);
Koji Sato6c98cd42009-04-06 19:01:32 -0700364 ret = nilfs_sufile_get_segment_usage_block(sufile, segnum, 1,
365 &su_bh);
366 if (ret < 0)
367 goto out_header;
Cong Wang7b9c0972011-11-25 23:14:33 +0800368 kaddr = kmap_atomic(su_bh->b_page);
Koji Sato6c98cd42009-04-06 19:01:32 -0700369 su = nilfs_sufile_block_get_segment_usage(
370 sufile, segnum, su_bh, kaddr);
371
372 nsus = nilfs_sufile_segment_usages_in_block(
373 sufile, segnum, maxsegnum);
374 for (j = 0; j < nsus; j++, su = (void *)su + susz, segnum++) {
375 if (!nilfs_segment_usage_clean(su))
376 continue;
377 /* found a clean segment */
Koji Sato6c98cd42009-04-06 19:01:32 -0700378 nilfs_segment_usage_set_dirty(su);
Cong Wang7b9c0972011-11-25 23:14:33 +0800379 kunmap_atomic(kaddr);
Koji Sato6c98cd42009-04-06 19:01:32 -0700380
Cong Wang7b9c0972011-11-25 23:14:33 +0800381 kaddr = kmap_atomic(header_bh->b_page);
Ryusuke Konishi7b16c8a2009-11-13 03:10:21 +0900382 header = kaddr + bh_offset(header_bh);
Koji Sato6c98cd42009-04-06 19:01:32 -0700383 le64_add_cpu(&header->sh_ncleansegs, -1);
384 le64_add_cpu(&header->sh_ndirtysegs, 1);
385 header->sh_last_alloc = cpu_to_le64(segnum);
Cong Wang7b9c0972011-11-25 23:14:33 +0800386 kunmap_atomic(kaddr);
Koji Sato6c98cd42009-04-06 19:01:32 -0700387
Ryusuke Konishi619205d2011-05-05 01:23:57 +0900388 sui->ncleansegs--;
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900389 mark_buffer_dirty(header_bh);
390 mark_buffer_dirty(su_bh);
Koji Sato6c98cd42009-04-06 19:01:32 -0700391 nilfs_mdt_mark_dirty(sufile);
392 brelse(su_bh);
393 *segnump = segnum;
Hitoshi Mitake83eec5e2015-11-06 16:32:05 -0800394
395 trace_nilfs2_segment_usage_allocated(sufile, segnum);
396
Koji Sato6c98cd42009-04-06 19:01:32 -0700397 goto out_header;
398 }
399
Cong Wang7b9c0972011-11-25 23:14:33 +0800400 kunmap_atomic(kaddr);
Koji Sato6c98cd42009-04-06 19:01:32 -0700401 brelse(su_bh);
402 }
403
404 /* no segments left */
405 ret = -ENOSPC;
406
407 out_header:
408 brelse(header_bh);
409
410 out_sem:
411 up_write(&NILFS_MDT(sufile)->mi_sem);
412 return ret;
413}
414
Ryusuke Konishia7030182009-04-05 18:24:11 +0900415void nilfs_sufile_do_cancel_free(struct inode *sufile, __u64 segnum,
416 struct buffer_head *header_bh,
417 struct buffer_head *su_bh)
Koji Sato6c98cd42009-04-06 19:01:32 -0700418{
Koji Sato6c98cd42009-04-06 19:01:32 -0700419 struct nilfs_segment_usage *su;
420 void *kaddr;
Koji Sato6c98cd42009-04-06 19:01:32 -0700421
Cong Wang7b9c0972011-11-25 23:14:33 +0800422 kaddr = kmap_atomic(su_bh->b_page);
Ryusuke Konishia7030182009-04-05 18:24:11 +0900423 su = nilfs_sufile_block_get_segment_usage(sufile, segnum, su_bh, kaddr);
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -0700424 if (unlikely(!nilfs_segment_usage_clean(su))) {
425 printk(KERN_WARNING "%s: segment %llu must be clean\n",
Koji Sato6c98cd42009-04-06 19:01:32 -0700426 __func__, (unsigned long long)segnum);
Cong Wang7b9c0972011-11-25 23:14:33 +0800427 kunmap_atomic(kaddr);
Ryusuke Konishia7030182009-04-05 18:24:11 +0900428 return;
Koji Sato6c98cd42009-04-06 19:01:32 -0700429 }
430 nilfs_segment_usage_set_dirty(su);
Cong Wang7b9c0972011-11-25 23:14:33 +0800431 kunmap_atomic(kaddr);
Koji Sato6c98cd42009-04-06 19:01:32 -0700432
Ryusuke Konishia7030182009-04-05 18:24:11 +0900433 nilfs_sufile_mod_counter(header_bh, -1, 1);
Ryusuke Konishiaa474a22009-11-13 03:41:55 +0900434 NILFS_SUI(sufile)->ncleansegs--;
435
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900436 mark_buffer_dirty(su_bh);
Koji Sato6c98cd42009-04-06 19:01:32 -0700437 nilfs_mdt_mark_dirty(sufile);
Koji Sato6c98cd42009-04-06 19:01:32 -0700438}
439
Ryusuke Konishic85399c2009-04-05 18:30:58 +0900440void nilfs_sufile_do_scrap(struct inode *sufile, __u64 segnum,
441 struct buffer_head *header_bh,
442 struct buffer_head *su_bh)
443{
444 struct nilfs_segment_usage *su;
445 void *kaddr;
446 int clean, dirty;
447
Cong Wang7b9c0972011-11-25 23:14:33 +0800448 kaddr = kmap_atomic(su_bh->b_page);
Ryusuke Konishic85399c2009-04-05 18:30:58 +0900449 su = nilfs_sufile_block_get_segment_usage(sufile, segnum, su_bh, kaddr);
450 if (su->su_flags == cpu_to_le32(1UL << NILFS_SEGMENT_USAGE_DIRTY) &&
451 su->su_nblocks == cpu_to_le32(0)) {
Cong Wang7b9c0972011-11-25 23:14:33 +0800452 kunmap_atomic(kaddr);
Ryusuke Konishic85399c2009-04-05 18:30:58 +0900453 return;
454 }
455 clean = nilfs_segment_usage_clean(su);
456 dirty = nilfs_segment_usage_dirty(su);
457
458 /* make the segment garbage */
459 su->su_lastmod = cpu_to_le64(0);
460 su->su_nblocks = cpu_to_le32(0);
461 su->su_flags = cpu_to_le32(1UL << NILFS_SEGMENT_USAGE_DIRTY);
Cong Wang7b9c0972011-11-25 23:14:33 +0800462 kunmap_atomic(kaddr);
Ryusuke Konishic85399c2009-04-05 18:30:58 +0900463
464 nilfs_sufile_mod_counter(header_bh, clean ? (u64)-1 : 0, dirty ? 0 : 1);
Ryusuke Konishiaa474a22009-11-13 03:41:55 +0900465 NILFS_SUI(sufile)->ncleansegs -= clean;
466
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900467 mark_buffer_dirty(su_bh);
Ryusuke Konishic85399c2009-04-05 18:30:58 +0900468 nilfs_mdt_mark_dirty(sufile);
469}
470
Ryusuke Konishia7030182009-04-05 18:24:11 +0900471void nilfs_sufile_do_free(struct inode *sufile, __u64 segnum,
472 struct buffer_head *header_bh,
473 struct buffer_head *su_bh)
Koji Sato6c98cd42009-04-06 19:01:32 -0700474{
Koji Sato6c98cd42009-04-06 19:01:32 -0700475 struct nilfs_segment_usage *su;
476 void *kaddr;
Ryusuke Konishia7030182009-04-05 18:24:11 +0900477 int sudirty;
Koji Sato6c98cd42009-04-06 19:01:32 -0700478
Cong Wang7b9c0972011-11-25 23:14:33 +0800479 kaddr = kmap_atomic(su_bh->b_page);
Ryusuke Konishia7030182009-04-05 18:24:11 +0900480 su = nilfs_sufile_block_get_segment_usage(sufile, segnum, su_bh, kaddr);
481 if (nilfs_segment_usage_clean(su)) {
482 printk(KERN_WARNING "%s: segment %llu is already clean\n",
483 __func__, (unsigned long long)segnum);
Cong Wang7b9c0972011-11-25 23:14:33 +0800484 kunmap_atomic(kaddr);
Ryusuke Konishia7030182009-04-05 18:24:11 +0900485 return;
Koji Sato6c98cd42009-04-06 19:01:32 -0700486 }
Ryusuke Konishia7030182009-04-05 18:24:11 +0900487 WARN_ON(nilfs_segment_usage_error(su));
488 WARN_ON(!nilfs_segment_usage_dirty(su));
489
490 sudirty = nilfs_segment_usage_dirty(su);
491 nilfs_segment_usage_set_clean(su);
Cong Wang7b9c0972011-11-25 23:14:33 +0800492 kunmap_atomic(kaddr);
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900493 mark_buffer_dirty(su_bh);
Ryusuke Konishia7030182009-04-05 18:24:11 +0900494
495 nilfs_sufile_mod_counter(header_bh, 1, sudirty ? (u64)-1 : 0);
Ryusuke Konishiaa474a22009-11-13 03:41:55 +0900496 NILFS_SUI(sufile)->ncleansegs++;
497
Koji Sato6c98cd42009-04-06 19:01:32 -0700498 nilfs_mdt_mark_dirty(sufile);
Hitoshi Mitake83eec5e2015-11-06 16:32:05 -0800499
500 trace_nilfs2_segment_usage_freed(sufile, segnum);
Koji Sato6c98cd42009-04-06 19:01:32 -0700501}
502
503/**
Ryusuke Konishi61a189e2009-11-18 17:25:12 +0900504 * nilfs_sufile_mark_dirty - mark the buffer having a segment usage dirty
505 * @sufile: inode of segment usage file
506 * @segnum: segment number
507 */
508int nilfs_sufile_mark_dirty(struct inode *sufile, __u64 segnum)
509{
510 struct buffer_head *bh;
511 int ret;
512
513 ret = nilfs_sufile_get_segment_usage_block(sufile, segnum, 0, &bh);
514 if (!ret) {
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900515 mark_buffer_dirty(bh);
Ryusuke Konishi61a189e2009-11-18 17:25:12 +0900516 nilfs_mdt_mark_dirty(sufile);
517 brelse(bh);
518 }
519 return ret;
520}
521
522/**
Ryusuke Konishi071ec542009-11-18 18:23:34 +0900523 * nilfs_sufile_set_segment_usage - set usage of a segment
524 * @sufile: inode of segment usage file
525 * @segnum: segment number
526 * @nblocks: number of live blocks in the segment
527 * @modtime: modification time (option)
528 */
529int nilfs_sufile_set_segment_usage(struct inode *sufile, __u64 segnum,
530 unsigned long nblocks, time_t modtime)
531{
532 struct buffer_head *bh;
533 struct nilfs_segment_usage *su;
534 void *kaddr;
535 int ret;
536
537 down_write(&NILFS_MDT(sufile)->mi_sem);
538 ret = nilfs_sufile_get_segment_usage_block(sufile, segnum, 0, &bh);
539 if (ret < 0)
540 goto out_sem;
541
Cong Wang7b9c0972011-11-25 23:14:33 +0800542 kaddr = kmap_atomic(bh->b_page);
Ryusuke Konishi071ec542009-11-18 18:23:34 +0900543 su = nilfs_sufile_block_get_segment_usage(sufile, segnum, bh, kaddr);
544 WARN_ON(nilfs_segment_usage_error(su));
545 if (modtime)
546 su->su_lastmod = cpu_to_le64(modtime);
547 su->su_nblocks = cpu_to_le32(nblocks);
Cong Wang7b9c0972011-11-25 23:14:33 +0800548 kunmap_atomic(kaddr);
Ryusuke Konishi071ec542009-11-18 18:23:34 +0900549
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900550 mark_buffer_dirty(bh);
Ryusuke Konishi071ec542009-11-18 18:23:34 +0900551 nilfs_mdt_mark_dirty(sufile);
552 brelse(bh);
553
554 out_sem:
555 up_write(&NILFS_MDT(sufile)->mi_sem);
556 return ret;
557}
558
559/**
Koji Sato6c98cd42009-04-06 19:01:32 -0700560 * nilfs_sufile_get_stat - get segment usage statistics
561 * @sufile: inode of segment usage file
562 * @stat: pointer to a structure of segment usage statistics
563 *
564 * Description: nilfs_sufile_get_stat() returns information about segment
565 * usage.
566 *
567 * Return Value: On success, 0 is returned, and segment usage information is
568 * stored in the place pointed by @stat. On error, one of the following
569 * negative error codes is returned.
570 *
571 * %-EIO - I/O error.
572 *
573 * %-ENOMEM - Insufficient amount of memory available.
574 */
575int nilfs_sufile_get_stat(struct inode *sufile, struct nilfs_sustat *sustat)
576{
577 struct buffer_head *header_bh;
578 struct nilfs_sufile_header *header;
Ryusuke Konishi0ef28f92011-05-05 12:56:51 +0900579 struct the_nilfs *nilfs = sufile->i_sb->s_fs_info;
Koji Sato6c98cd42009-04-06 19:01:32 -0700580 void *kaddr;
581 int ret;
582
583 down_read(&NILFS_MDT(sufile)->mi_sem);
584
585 ret = nilfs_sufile_get_header_block(sufile, &header_bh);
586 if (ret < 0)
587 goto out_sem;
588
Cong Wang7b9c0972011-11-25 23:14:33 +0800589 kaddr = kmap_atomic(header_bh->b_page);
Ryusuke Konishi7b16c8a2009-11-13 03:10:21 +0900590 header = kaddr + bh_offset(header_bh);
Koji Sato6c98cd42009-04-06 19:01:32 -0700591 sustat->ss_nsegs = nilfs_sufile_get_nsegments(sufile);
592 sustat->ss_ncleansegs = le64_to_cpu(header->sh_ncleansegs);
593 sustat->ss_ndirtysegs = le64_to_cpu(header->sh_ndirtysegs);
Ryusuke Konishi2c2e52f2009-04-06 19:01:54 -0700594 sustat->ss_ctime = nilfs->ns_ctime;
595 sustat->ss_nongc_ctime = nilfs->ns_nongc_ctime;
596 spin_lock(&nilfs->ns_last_segment_lock);
597 sustat->ss_prot_seq = nilfs->ns_prot_seq;
598 spin_unlock(&nilfs->ns_last_segment_lock);
Cong Wang7b9c0972011-11-25 23:14:33 +0800599 kunmap_atomic(kaddr);
Koji Sato6c98cd42009-04-06 19:01:32 -0700600 brelse(header_bh);
601
602 out_sem:
603 up_read(&NILFS_MDT(sufile)->mi_sem);
604 return ret;
605}
606
Ryusuke Konishia7030182009-04-05 18:24:11 +0900607void nilfs_sufile_do_set_error(struct inode *sufile, __u64 segnum,
608 struct buffer_head *header_bh,
609 struct buffer_head *su_bh)
Koji Sato6c98cd42009-04-06 19:01:32 -0700610{
Koji Sato6c98cd42009-04-06 19:01:32 -0700611 struct nilfs_segment_usage *su;
Koji Sato6c98cd42009-04-06 19:01:32 -0700612 void *kaddr;
Ryusuke Konishia7030182009-04-05 18:24:11 +0900613 int suclean;
Koji Sato6c98cd42009-04-06 19:01:32 -0700614
Cong Wang7b9c0972011-11-25 23:14:33 +0800615 kaddr = kmap_atomic(su_bh->b_page);
Koji Sato6c98cd42009-04-06 19:01:32 -0700616 su = nilfs_sufile_block_get_segment_usage(sufile, segnum, su_bh, kaddr);
617 if (nilfs_segment_usage_error(su)) {
Cong Wang7b9c0972011-11-25 23:14:33 +0800618 kunmap_atomic(kaddr);
Ryusuke Konishia7030182009-04-05 18:24:11 +0900619 return;
Koji Sato6c98cd42009-04-06 19:01:32 -0700620 }
Ryusuke Konishi88072fa2009-04-05 15:03:16 +0900621 suclean = nilfs_segment_usage_clean(su);
Koji Sato6c98cd42009-04-06 19:01:32 -0700622 nilfs_segment_usage_set_error(su);
Cong Wang7b9c0972011-11-25 23:14:33 +0800623 kunmap_atomic(kaddr);
Koji Sato6c98cd42009-04-06 19:01:32 -0700624
Ryusuke Konishiaa474a22009-11-13 03:41:55 +0900625 if (suclean) {
Ryusuke Konishia7030182009-04-05 18:24:11 +0900626 nilfs_sufile_mod_counter(header_bh, -1, 0);
Ryusuke Konishiaa474a22009-11-13 03:41:55 +0900627 NILFS_SUI(sufile)->ncleansegs--;
628 }
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900629 mark_buffer_dirty(su_bh);
Koji Sato6c98cd42009-04-06 19:01:32 -0700630 nilfs_mdt_mark_dirty(sufile);
Koji Sato6c98cd42009-04-06 19:01:32 -0700631}
632
633/**
Ryusuke Konishi78eb64c2011-05-05 01:23:58 +0900634 * nilfs_sufile_truncate_range - truncate range of segment array
635 * @sufile: inode of segment usage file
636 * @start: start segment number (inclusive)
637 * @end: end segment number (inclusive)
638 *
639 * Return Value: On success, 0 is returned. On error, one of the
640 * following negative error codes is returned.
641 *
642 * %-EIO - I/O error.
643 *
644 * %-ENOMEM - Insufficient amount of memory available.
645 *
646 * %-EINVAL - Invalid number of segments specified
647 *
648 * %-EBUSY - Dirty or active segments are present in the range
649 */
650static int nilfs_sufile_truncate_range(struct inode *sufile,
651 __u64 start, __u64 end)
652{
653 struct the_nilfs *nilfs = sufile->i_sb->s_fs_info;
654 struct buffer_head *header_bh;
655 struct buffer_head *su_bh;
656 struct nilfs_segment_usage *su, *su2;
657 size_t susz = NILFS_MDT(sufile)->mi_entry_size;
658 unsigned long segusages_per_block;
659 unsigned long nsegs, ncleaned;
660 __u64 segnum;
661 void *kaddr;
662 ssize_t n, nc;
663 int ret;
664 int j;
665
666 nsegs = nilfs_sufile_get_nsegments(sufile);
667
668 ret = -EINVAL;
669 if (start > end || start >= nsegs)
670 goto out;
671
672 ret = nilfs_sufile_get_header_block(sufile, &header_bh);
673 if (ret < 0)
674 goto out;
675
676 segusages_per_block = nilfs_sufile_segment_usages_per_block(sufile);
677 ncleaned = 0;
678
679 for (segnum = start; segnum <= end; segnum += n) {
680 n = min_t(unsigned long,
681 segusages_per_block -
682 nilfs_sufile_get_offset(sufile, segnum),
683 end - segnum + 1);
684 ret = nilfs_sufile_get_segment_usage_block(sufile, segnum, 0,
685 &su_bh);
686 if (ret < 0) {
687 if (ret != -ENOENT)
688 goto out_header;
689 /* hole */
690 continue;
691 }
Cong Wang7b9c0972011-11-25 23:14:33 +0800692 kaddr = kmap_atomic(su_bh->b_page);
Ryusuke Konishi78eb64c2011-05-05 01:23:58 +0900693 su = nilfs_sufile_block_get_segment_usage(
694 sufile, segnum, su_bh, kaddr);
695 su2 = su;
696 for (j = 0; j < n; j++, su = (void *)su + susz) {
697 if ((le32_to_cpu(su->su_flags) &
698 ~(1UL << NILFS_SEGMENT_USAGE_ERROR)) ||
699 nilfs_segment_is_active(nilfs, segnum + j)) {
700 ret = -EBUSY;
Cong Wang7b9c0972011-11-25 23:14:33 +0800701 kunmap_atomic(kaddr);
Ryusuke Konishi78eb64c2011-05-05 01:23:58 +0900702 brelse(su_bh);
703 goto out_header;
704 }
705 }
706 nc = 0;
707 for (su = su2, j = 0; j < n; j++, su = (void *)su + susz) {
708 if (nilfs_segment_usage_error(su)) {
709 nilfs_segment_usage_set_clean(su);
710 nc++;
711 }
712 }
Cong Wang7b9c0972011-11-25 23:14:33 +0800713 kunmap_atomic(kaddr);
Ryusuke Konishi78eb64c2011-05-05 01:23:58 +0900714 if (nc > 0) {
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900715 mark_buffer_dirty(su_bh);
Ryusuke Konishi78eb64c2011-05-05 01:23:58 +0900716 ncleaned += nc;
717 }
718 brelse(su_bh);
719
720 if (n == segusages_per_block) {
721 /* make hole */
722 nilfs_sufile_delete_segment_usage_block(sufile, segnum);
723 }
724 }
725 ret = 0;
726
727out_header:
728 if (ncleaned > 0) {
729 NILFS_SUI(sufile)->ncleansegs += ncleaned;
730 nilfs_sufile_mod_counter(header_bh, ncleaned, 0);
731 nilfs_mdt_mark_dirty(sufile);
732 }
733 brelse(header_bh);
734out:
735 return ret;
736}
737
738/**
Ryusuke Konishi4e33f9e2011-05-05 01:23:58 +0900739 * nilfs_sufile_resize - resize segment array
740 * @sufile: inode of segment usage file
741 * @newnsegs: new number of segments
742 *
743 * Return Value: On success, 0 is returned. On error, one of the
744 * following negative error codes is returned.
745 *
746 * %-EIO - I/O error.
747 *
748 * %-ENOMEM - Insufficient amount of memory available.
749 *
750 * %-ENOSPC - Enough free space is not left for shrinking
751 *
752 * %-EBUSY - Dirty or active segments exist in the region to be truncated
753 */
754int nilfs_sufile_resize(struct inode *sufile, __u64 newnsegs)
755{
756 struct the_nilfs *nilfs = sufile->i_sb->s_fs_info;
757 struct buffer_head *header_bh;
758 struct nilfs_sufile_header *header;
759 struct nilfs_sufile_info *sui = NILFS_SUI(sufile);
760 void *kaddr;
761 unsigned long nsegs, nrsvsegs;
762 int ret = 0;
763
764 down_write(&NILFS_MDT(sufile)->mi_sem);
765
766 nsegs = nilfs_sufile_get_nsegments(sufile);
767 if (nsegs == newnsegs)
768 goto out;
769
770 ret = -ENOSPC;
771 nrsvsegs = nilfs_nrsvsegs(nilfs, newnsegs);
772 if (newnsegs < nsegs && nsegs - newnsegs + nrsvsegs > sui->ncleansegs)
773 goto out;
774
775 ret = nilfs_sufile_get_header_block(sufile, &header_bh);
776 if (ret < 0)
777 goto out;
778
779 if (newnsegs > nsegs) {
780 sui->ncleansegs += newnsegs - nsegs;
781 } else /* newnsegs < nsegs */ {
782 ret = nilfs_sufile_truncate_range(sufile, newnsegs, nsegs - 1);
783 if (ret < 0)
784 goto out_header;
785
786 sui->ncleansegs -= nsegs - newnsegs;
787 }
788
Cong Wang7b9c0972011-11-25 23:14:33 +0800789 kaddr = kmap_atomic(header_bh->b_page);
Ryusuke Konishi4e33f9e2011-05-05 01:23:58 +0900790 header = kaddr + bh_offset(header_bh);
791 header->sh_ncleansegs = cpu_to_le64(sui->ncleansegs);
Cong Wang7b9c0972011-11-25 23:14:33 +0800792 kunmap_atomic(kaddr);
Ryusuke Konishi4e33f9e2011-05-05 01:23:58 +0900793
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900794 mark_buffer_dirty(header_bh);
Ryusuke Konishi4e33f9e2011-05-05 01:23:58 +0900795 nilfs_mdt_mark_dirty(sufile);
796 nilfs_set_nsegments(nilfs, newnsegs);
797
798out_header:
799 brelse(header_bh);
800out:
801 up_write(&NILFS_MDT(sufile)->mi_sem);
802 return ret;
803}
804
805/**
Koji Sato6c98cd42009-04-06 19:01:32 -0700806 * nilfs_sufile_get_suinfo -
807 * @sufile: inode of segment usage file
808 * @segnum: segment number to start looking
Ryusuke Konishi003ff182009-05-12 03:58:47 +0900809 * @buf: array of suinfo
810 * @sisz: byte size of suinfo
Koji Sato6c98cd42009-04-06 19:01:32 -0700811 * @nsi: size of suinfo array
812 *
813 * Description:
814 *
815 * Return Value: On success, 0 is returned and .... On error, one of the
816 * following negative error codes is returned.
817 *
818 * %-EIO - I/O error.
819 *
820 * %-ENOMEM - Insufficient amount of memory available.
821 */
Ryusuke Konishi003ff182009-05-12 03:58:47 +0900822ssize_t nilfs_sufile_get_suinfo(struct inode *sufile, __u64 segnum, void *buf,
823 unsigned sisz, size_t nsi)
Koji Sato6c98cd42009-04-06 19:01:32 -0700824{
825 struct buffer_head *su_bh;
826 struct nilfs_segment_usage *su;
Ryusuke Konishi003ff182009-05-12 03:58:47 +0900827 struct nilfs_suinfo *si = buf;
Koji Sato6c98cd42009-04-06 19:01:32 -0700828 size_t susz = NILFS_MDT(sufile)->mi_entry_size;
Ryusuke Konishi0ef28f92011-05-05 12:56:51 +0900829 struct the_nilfs *nilfs = sufile->i_sb->s_fs_info;
Koji Sato6c98cd42009-04-06 19:01:32 -0700830 void *kaddr;
831 unsigned long nsegs, segusages_per_block;
832 ssize_t n;
833 int ret, i, j;
834
835 down_read(&NILFS_MDT(sufile)->mi_sem);
836
837 segusages_per_block = nilfs_sufile_segment_usages_per_block(sufile);
838 nsegs = min_t(unsigned long,
839 nilfs_sufile_get_nsegments(sufile) - segnum,
840 nsi);
841 for (i = 0; i < nsegs; i += n, segnum += n) {
842 n = min_t(unsigned long,
843 segusages_per_block -
844 nilfs_sufile_get_offset(sufile, segnum),
845 nsegs - i);
846 ret = nilfs_sufile_get_segment_usage_block(sufile, segnum, 0,
847 &su_bh);
848 if (ret < 0) {
849 if (ret != -ENOENT)
850 goto out;
851 /* hole */
Ryusuke Konishi003ff182009-05-12 03:58:47 +0900852 memset(si, 0, sisz * n);
853 si = (void *)si + sisz * n;
Koji Sato6c98cd42009-04-06 19:01:32 -0700854 continue;
855 }
856
Cong Wang7b9c0972011-11-25 23:14:33 +0800857 kaddr = kmap_atomic(su_bh->b_page);
Koji Sato6c98cd42009-04-06 19:01:32 -0700858 su = nilfs_sufile_block_get_segment_usage(
859 sufile, segnum, su_bh, kaddr);
Ryusuke Konishi003ff182009-05-12 03:58:47 +0900860 for (j = 0; j < n;
861 j++, su = (void *)su + susz, si = (void *)si + sisz) {
862 si->sui_lastmod = le64_to_cpu(su->su_lastmod);
863 si->sui_nblocks = le32_to_cpu(su->su_nblocks);
864 si->sui_flags = le32_to_cpu(su->su_flags) &
Ryusuke Konishicece5522009-04-06 19:01:58 -0700865 ~(1UL << NILFS_SEGMENT_USAGE_ACTIVE);
Ryusuke Konishi3efb55b2009-03-30 00:50:19 +0900866 if (nilfs_segment_is_active(nilfs, segnum + j))
Ryusuke Konishi003ff182009-05-12 03:58:47 +0900867 si->sui_flags |=
Ryusuke Konishicece5522009-04-06 19:01:58 -0700868 (1UL << NILFS_SEGMENT_USAGE_ACTIVE);
Koji Sato6c98cd42009-04-06 19:01:32 -0700869 }
Cong Wang7b9c0972011-11-25 23:14:33 +0800870 kunmap_atomic(kaddr);
Koji Sato6c98cd42009-04-06 19:01:32 -0700871 brelse(su_bh);
872 }
873 ret = nsegs;
874
875 out:
876 up_read(&NILFS_MDT(sufile)->mi_sem);
877 return ret;
878}
Ryusuke Konishi79739562009-11-12 23:56:43 +0900879
880/**
Andreas Rohner00e9ffc2014-04-03 14:50:27 -0700881 * nilfs_sufile_set_suinfo - sets segment usage info
882 * @sufile: inode of segment usage file
883 * @buf: array of suinfo_update
884 * @supsz: byte size of suinfo_update
885 * @nsup: size of suinfo_update array
886 *
887 * Description: Takes an array of nilfs_suinfo_update structs and updates
888 * segment usage accordingly. Only the fields indicated by the sup_flags
889 * are updated.
890 *
891 * Return Value: On success, 0 is returned. On error, one of the
892 * following negative error codes is returned.
893 *
894 * %-EIO - I/O error.
895 *
896 * %-ENOMEM - Insufficient amount of memory available.
897 *
898 * %-EINVAL - Invalid values in input (segment number, flags or nblocks)
899 */
900ssize_t nilfs_sufile_set_suinfo(struct inode *sufile, void *buf,
901 unsigned supsz, size_t nsup)
902{
903 struct the_nilfs *nilfs = sufile->i_sb->s_fs_info;
904 struct buffer_head *header_bh, *bh;
905 struct nilfs_suinfo_update *sup, *supend = buf + supsz * nsup;
906 struct nilfs_segment_usage *su;
907 void *kaddr;
908 unsigned long blkoff, prev_blkoff;
909 int cleansi, cleansu, dirtysi, dirtysu;
910 long ncleaned = 0, ndirtied = 0;
911 int ret = 0;
912
913 if (unlikely(nsup == 0))
914 return ret;
915
916 for (sup = buf; sup < supend; sup = (void *)sup + supsz) {
917 if (sup->sup_segnum >= nilfs->ns_nsegments
918 || (sup->sup_flags &
919 (~0UL << __NR_NILFS_SUINFO_UPDATE_FIELDS))
920 || (nilfs_suinfo_update_nblocks(sup) &&
921 sup->sup_sui.sui_nblocks >
922 nilfs->ns_blocks_per_segment))
923 return -EINVAL;
924 }
925
926 down_write(&NILFS_MDT(sufile)->mi_sem);
927
928 ret = nilfs_sufile_get_header_block(sufile, &header_bh);
929 if (ret < 0)
930 goto out_sem;
931
932 sup = buf;
933 blkoff = nilfs_sufile_get_blkoff(sufile, sup->sup_segnum);
934 ret = nilfs_mdt_get_block(sufile, blkoff, 1, NULL, &bh);
935 if (ret < 0)
936 goto out_header;
937
938 for (;;) {
939 kaddr = kmap_atomic(bh->b_page);
940 su = nilfs_sufile_block_get_segment_usage(
941 sufile, sup->sup_segnum, bh, kaddr);
942
943 if (nilfs_suinfo_update_lastmod(sup))
944 su->su_lastmod = cpu_to_le64(sup->sup_sui.sui_lastmod);
945
946 if (nilfs_suinfo_update_nblocks(sup))
947 su->su_nblocks = cpu_to_le32(sup->sup_sui.sui_nblocks);
948
949 if (nilfs_suinfo_update_flags(sup)) {
950 /*
951 * Active flag is a virtual flag projected by running
952 * nilfs kernel code - drop it not to write it to
953 * disk.
954 */
955 sup->sup_sui.sui_flags &=
956 ~(1UL << NILFS_SEGMENT_USAGE_ACTIVE);
957
958 cleansi = nilfs_suinfo_clean(&sup->sup_sui);
959 cleansu = nilfs_segment_usage_clean(su);
960 dirtysi = nilfs_suinfo_dirty(&sup->sup_sui);
961 dirtysu = nilfs_segment_usage_dirty(su);
962
963 if (cleansi && !cleansu)
964 ++ncleaned;
965 else if (!cleansi && cleansu)
966 --ncleaned;
967
968 if (dirtysi && !dirtysu)
969 ++ndirtied;
970 else if (!dirtysi && dirtysu)
971 --ndirtied;
972
973 su->su_flags = cpu_to_le32(sup->sup_sui.sui_flags);
974 }
975
976 kunmap_atomic(kaddr);
977
978 sup = (void *)sup + supsz;
979 if (sup >= supend)
980 break;
981
982 prev_blkoff = blkoff;
983 blkoff = nilfs_sufile_get_blkoff(sufile, sup->sup_segnum);
984 if (blkoff == prev_blkoff)
985 continue;
986
987 /* get different block */
988 mark_buffer_dirty(bh);
989 put_bh(bh);
990 ret = nilfs_mdt_get_block(sufile, blkoff, 1, NULL, &bh);
991 if (unlikely(ret < 0))
992 goto out_mark;
993 }
994 mark_buffer_dirty(bh);
995 put_bh(bh);
996
997 out_mark:
998 if (ncleaned || ndirtied) {
999 nilfs_sufile_mod_counter(header_bh, (u64)ncleaned,
1000 (u64)ndirtied);
1001 NILFS_SUI(sufile)->ncleansegs += ncleaned;
1002 }
1003 nilfs_mdt_mark_dirty(sufile);
1004 out_header:
1005 put_bh(header_bh);
1006 out_sem:
1007 up_write(&NILFS_MDT(sufile)->mi_sem);
1008 return ret;
1009}
1010
1011/**
Andreas Rohner82e11e82014-04-03 14:50:29 -07001012 * nilfs_sufile_trim_fs() - trim ioctl handle function
1013 * @sufile: inode of segment usage file
1014 * @range: fstrim_range structure
1015 *
1016 * start: First Byte to trim
1017 * len: number of Bytes to trim from start
1018 * minlen: minimum extent length in Bytes
1019 *
1020 * Decription: nilfs_sufile_trim_fs goes through all segments containing bytes
1021 * from start to start+len. start is rounded up to the next block boundary
1022 * and start+len is rounded down. For each clean segment blkdev_issue_discard
1023 * function is invoked.
1024 *
1025 * Return Value: On success, 0 is returned or negative error code, otherwise.
1026 */
1027int nilfs_sufile_trim_fs(struct inode *sufile, struct fstrim_range *range)
1028{
1029 struct the_nilfs *nilfs = sufile->i_sb->s_fs_info;
1030 struct buffer_head *su_bh;
1031 struct nilfs_segment_usage *su;
1032 void *kaddr;
1033 size_t n, i, susz = NILFS_MDT(sufile)->mi_entry_size;
1034 sector_t seg_start, seg_end, start_block, end_block;
1035 sector_t start = 0, nblocks = 0;
1036 u64 segnum, segnum_end, minlen, len, max_blocks, ndiscarded = 0;
1037 int ret = 0;
1038 unsigned int sects_per_block;
1039
1040 sects_per_block = (1 << nilfs->ns_blocksize_bits) /
1041 bdev_logical_block_size(nilfs->ns_bdev);
1042 len = range->len >> nilfs->ns_blocksize_bits;
1043 minlen = range->minlen >> nilfs->ns_blocksize_bits;
1044 max_blocks = ((u64)nilfs->ns_nsegments * nilfs->ns_blocks_per_segment);
1045
1046 if (!len || range->start >= max_blocks << nilfs->ns_blocksize_bits)
1047 return -EINVAL;
1048
1049 start_block = (range->start + nilfs->ns_blocksize - 1) >>
1050 nilfs->ns_blocksize_bits;
1051
1052 /*
1053 * range->len can be very large (actually, it is set to
1054 * ULLONG_MAX by default) - truncate upper end of the range
1055 * carefully so as not to overflow.
1056 */
1057 if (max_blocks - start_block < len)
1058 end_block = max_blocks - 1;
1059 else
1060 end_block = start_block + len - 1;
1061
1062 segnum = nilfs_get_segnum_of_block(nilfs, start_block);
1063 segnum_end = nilfs_get_segnum_of_block(nilfs, end_block);
1064
1065 down_read(&NILFS_MDT(sufile)->mi_sem);
1066
1067 while (segnum <= segnum_end) {
1068 n = nilfs_sufile_segment_usages_in_block(sufile, segnum,
1069 segnum_end);
1070
1071 ret = nilfs_sufile_get_segment_usage_block(sufile, segnum, 0,
1072 &su_bh);
1073 if (ret < 0) {
1074 if (ret != -ENOENT)
1075 goto out_sem;
1076 /* hole */
1077 segnum += n;
1078 continue;
1079 }
1080
1081 kaddr = kmap_atomic(su_bh->b_page);
1082 su = nilfs_sufile_block_get_segment_usage(sufile, segnum,
1083 su_bh, kaddr);
1084 for (i = 0; i < n; ++i, ++segnum, su = (void *)su + susz) {
1085 if (!nilfs_segment_usage_clean(su))
1086 continue;
1087
1088 nilfs_get_segment_range(nilfs, segnum, &seg_start,
1089 &seg_end);
1090
1091 if (!nblocks) {
1092 /* start new extent */
1093 start = seg_start;
1094 nblocks = seg_end - seg_start + 1;
1095 continue;
1096 }
1097
1098 if (start + nblocks == seg_start) {
1099 /* add to previous extent */
1100 nblocks += seg_end - seg_start + 1;
1101 continue;
1102 }
1103
1104 /* discard previous extent */
1105 if (start < start_block) {
1106 nblocks -= start_block - start;
1107 start = start_block;
1108 }
1109
1110 if (nblocks >= minlen) {
1111 kunmap_atomic(kaddr);
1112
1113 ret = blkdev_issue_discard(nilfs->ns_bdev,
1114 start * sects_per_block,
1115 nblocks * sects_per_block,
1116 GFP_NOFS, 0);
1117 if (ret < 0) {
1118 put_bh(su_bh);
1119 goto out_sem;
1120 }
1121
1122 ndiscarded += nblocks;
1123 kaddr = kmap_atomic(su_bh->b_page);
1124 su = nilfs_sufile_block_get_segment_usage(
1125 sufile, segnum, su_bh, kaddr);
1126 }
1127
1128 /* start new extent */
1129 start = seg_start;
1130 nblocks = seg_end - seg_start + 1;
1131 }
1132 kunmap_atomic(kaddr);
1133 put_bh(su_bh);
1134 }
1135
1136
1137 if (nblocks) {
1138 /* discard last extent */
1139 if (start < start_block) {
1140 nblocks -= start_block - start;
1141 start = start_block;
1142 }
1143 if (start + nblocks > end_block + 1)
1144 nblocks = end_block - start + 1;
1145
1146 if (nblocks >= minlen) {
1147 ret = blkdev_issue_discard(nilfs->ns_bdev,
1148 start * sects_per_block,
1149 nblocks * sects_per_block,
1150 GFP_NOFS, 0);
1151 if (!ret)
1152 ndiscarded += nblocks;
1153 }
1154 }
1155
1156out_sem:
1157 up_read(&NILFS_MDT(sufile)->mi_sem);
1158
1159 range->len = ndiscarded << nilfs->ns_blocksize_bits;
1160 return ret;
1161}
1162
1163/**
Ryusuke Konishif1e89c82010-09-05 12:20:59 +09001164 * nilfs_sufile_read - read or get sufile inode
1165 * @sb: super block instance
1166 * @susize: size of a segment usage entry
Ryusuke Konishi8707df32009-11-13 01:36:56 +09001167 * @raw_inode: on-disk sufile inode
Ryusuke Konishif1e89c82010-09-05 12:20:59 +09001168 * @inodep: buffer to store the inode
Ryusuke Konishi8707df32009-11-13 01:36:56 +09001169 */
Ryusuke Konishif1e89c82010-09-05 12:20:59 +09001170int nilfs_sufile_read(struct super_block *sb, size_t susize,
1171 struct nilfs_inode *raw_inode, struct inode **inodep)
Ryusuke Konishi8707df32009-11-13 01:36:56 +09001172{
Ryusuke Konishif1e89c82010-09-05 12:20:59 +09001173 struct inode *sufile;
1174 struct nilfs_sufile_info *sui;
Ryusuke Konishiaa474a22009-11-13 03:41:55 +09001175 struct buffer_head *header_bh;
1176 struct nilfs_sufile_header *header;
1177 void *kaddr;
Ryusuke Konishif1e89c82010-09-05 12:20:59 +09001178 int err;
Ryusuke Konishiaa474a22009-11-13 03:41:55 +09001179
Ryusuke Konishi0ec060d2014-04-03 14:50:31 -07001180 if (susize > sb->s_blocksize) {
1181 printk(KERN_ERR
1182 "NILFS: too large segment usage size: %zu bytes.\n",
1183 susize);
1184 return -EINVAL;
1185 } else if (susize < NILFS_MIN_SEGMENT_USAGE_SIZE) {
1186 printk(KERN_ERR
1187 "NILFS: too small segment usage size: %zu bytes.\n",
1188 susize);
1189 return -EINVAL;
1190 }
1191
Ryusuke Konishif1e89c82010-09-05 12:20:59 +09001192 sufile = nilfs_iget_locked(sb, NULL, NILFS_SUFILE_INO);
1193 if (unlikely(!sufile))
1194 return -ENOMEM;
1195 if (!(sufile->i_state & I_NEW))
1196 goto out;
Ryusuke Konishiaa474a22009-11-13 03:41:55 +09001197
Ryusuke Konishif1e89c82010-09-05 12:20:59 +09001198 err = nilfs_mdt_init(sufile, NILFS_MDT_GFP, sizeof(*sui));
1199 if (err)
1200 goto failed;
Ryusuke Konishi8707df32009-11-13 01:36:56 +09001201
Ryusuke Konishif1e89c82010-09-05 12:20:59 +09001202 nilfs_mdt_set_entry_size(sufile, susize,
1203 sizeof(struct nilfs_sufile_header));
Ryusuke Konishi79739562009-11-12 23:56:43 +09001204
Ryusuke Konishif1e89c82010-09-05 12:20:59 +09001205 err = nilfs_read_inode_common(sufile, raw_inode);
1206 if (err)
1207 goto failed;
1208
1209 err = nilfs_sufile_get_header_block(sufile, &header_bh);
1210 if (err)
1211 goto failed;
1212
1213 sui = NILFS_SUI(sufile);
Cong Wang7b9c0972011-11-25 23:14:33 +08001214 kaddr = kmap_atomic(header_bh->b_page);
Ryusuke Konishif1e89c82010-09-05 12:20:59 +09001215 header = kaddr + bh_offset(header_bh);
1216 sui->ncleansegs = le64_to_cpu(header->sh_ncleansegs);
Cong Wang7b9c0972011-11-25 23:14:33 +08001217 kunmap_atomic(kaddr);
Ryusuke Konishif1e89c82010-09-05 12:20:59 +09001218 brelse(header_bh);
1219
Ryusuke Konishi619205d2011-05-05 01:23:57 +09001220 sui->allocmax = nilfs_sufile_get_nsegments(sufile) - 1;
1221 sui->allocmin = 0;
1222
Ryusuke Konishif1e89c82010-09-05 12:20:59 +09001223 unlock_new_inode(sufile);
1224 out:
1225 *inodep = sufile;
1226 return 0;
1227 failed:
1228 iget_failed(sufile);
1229 return err;
Ryusuke Konishi79739562009-11-12 23:56:43 +09001230}