2 * sufile.c - NILFS segment usage file.
4 * Copyright (C) 2006-2008 Nippon Telegraph and Telephone Corporation.
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.
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.
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
20 * Written by Koji Sato <koji@osrg.net>.
21 * Rivised by Ryusuke Konishi <ryusuke@osrg.net>.
24 #include <linux/kernel.h>
26 #include <linux/string.h>
27 #include <linux/buffer_head.h>
28 #include <linux/errno.h>
29 #include <linux/nilfs2_fs.h>
34 static inline unsigned long
35 nilfs_sufile_segment_usages_per_block(const struct inode *sufile)
37 return NILFS_MDT(sufile)->mi_entries_per_block;
41 nilfs_sufile_get_blkoff(const struct inode *sufile, __u64 segnum)
43 __u64 t = segnum + NILFS_MDT(sufile)->mi_first_entry_offset;
44 do_div(t, nilfs_sufile_segment_usages_per_block(sufile));
45 return (unsigned long)t;
49 nilfs_sufile_get_offset(const struct inode *sufile, __u64 segnum)
51 __u64 t = segnum + NILFS_MDT(sufile)->mi_first_entry_offset;
52 return do_div(t, nilfs_sufile_segment_usages_per_block(sufile));
56 nilfs_sufile_segment_usages_in_block(const struct inode *sufile, __u64 curr,
59 return min_t(unsigned long,
60 nilfs_sufile_segment_usages_per_block(sufile) -
61 nilfs_sufile_get_offset(sufile, curr),
65 static inline struct nilfs_sufile_header *
66 nilfs_sufile_block_get_header(const struct inode *sufile,
67 struct buffer_head *bh,
70 return kaddr + bh_offset(bh);
73 static struct nilfs_segment_usage *
74 nilfs_sufile_block_get_segment_usage(const struct inode *sufile, __u64 segnum,
75 struct buffer_head *bh, void *kaddr)
77 return kaddr + bh_offset(bh) +
78 nilfs_sufile_get_offset(sufile, segnum) *
79 NILFS_MDT(sufile)->mi_entry_size;
82 static inline int nilfs_sufile_get_header_block(struct inode *sufile,
83 struct buffer_head **bhp)
85 return nilfs_mdt_get_block(sufile, 0, 0, NULL, bhp);
89 nilfs_sufile_get_segment_usage_block(struct inode *sufile, __u64 segnum,
90 int create, struct buffer_head **bhp)
92 return nilfs_mdt_get_block(sufile,
93 nilfs_sufile_get_blkoff(sufile, segnum),
97 static void nilfs_sufile_mod_counter(struct buffer_head *header_bh,
98 u64 ncleanadd, u64 ndirtyadd)
100 struct nilfs_sufile_header *header;
103 kaddr = kmap_atomic(header_bh->b_page, KM_USER0);
104 header = kaddr + bh_offset(header_bh);
105 le64_add_cpu(&header->sh_ncleansegs, ncleanadd);
106 le64_add_cpu(&header->sh_ndirtysegs, ndirtyadd);
107 kunmap_atomic(kaddr, KM_USER0);
109 nilfs_mdt_mark_buffer_dirty(header_bh);
113 * nilfs_sufile_updatev - modify multiple segment usages at a time
114 * @sufile: inode of segment usage file
115 * @segnumv: array of segment numbers
116 * @nsegs: size of @segnumv array
117 * @create: creation flag
118 * @ndone: place to store number of modified segments on @segnumv
119 * @dofunc: primitive operation for the update
121 * Description: nilfs_sufile_updatev() repeatedly calls @dofunc
122 * against the given array of segments. The @dofunc is called with
123 * buffers of a header block and the sufile block in which the target
124 * segment usage entry is contained. If @ndone is given, the number
125 * of successfully modified segments from the head is stored in the
126 * place @ndone points to.
128 * Return Value: On success, zero is returned. On error, one of the
129 * following negative error codes is returned.
133 * %-ENOMEM - Insufficient amount of memory available.
135 * %-ENOENT - Given segment usage is in hole block (may be returned if
138 * %-EINVAL - Invalid segment usage number
140 int nilfs_sufile_updatev(struct inode *sufile, __u64 *segnumv, size_t nsegs,
141 int create, size_t *ndone,
142 void (*dofunc)(struct inode *, __u64,
143 struct buffer_head *,
144 struct buffer_head *))
146 struct buffer_head *header_bh, *bh;
147 unsigned long blkoff, prev_blkoff;
149 size_t nerr = 0, n = 0;
152 if (unlikely(nsegs == 0))
155 down_write(&NILFS_MDT(sufile)->mi_sem);
156 for (seg = segnumv; seg < segnumv + nsegs; seg++) {
157 if (unlikely(*seg >= nilfs_sufile_get_nsegments(sufile))) {
159 "%s: invalid segment number: %llu\n", __func__,
160 (unsigned long long)*seg);
169 ret = nilfs_sufile_get_header_block(sufile, &header_bh);
174 blkoff = nilfs_sufile_get_blkoff(sufile, *seg);
175 ret = nilfs_mdt_get_block(sufile, blkoff, create, NULL, &bh);
180 dofunc(sufile, *seg, header_bh, bh);
182 if (++seg >= segnumv + nsegs)
184 prev_blkoff = blkoff;
185 blkoff = nilfs_sufile_get_blkoff(sufile, *seg);
186 if (blkoff == prev_blkoff)
189 /* get different block */
191 ret = nilfs_mdt_get_block(sufile, blkoff, create, NULL, &bh);
192 if (unlikely(ret < 0))
201 up_write(&NILFS_MDT(sufile)->mi_sem);
208 int nilfs_sufile_update(struct inode *sufile, __u64 segnum, int create,
209 void (*dofunc)(struct inode *, __u64,
210 struct buffer_head *,
211 struct buffer_head *))
213 struct buffer_head *header_bh, *bh;
216 if (unlikely(segnum >= nilfs_sufile_get_nsegments(sufile))) {
217 printk(KERN_WARNING "%s: invalid segment number: %llu\n",
218 __func__, (unsigned long long)segnum);
221 down_write(&NILFS_MDT(sufile)->mi_sem);
223 ret = nilfs_sufile_get_header_block(sufile, &header_bh);
227 ret = nilfs_sufile_get_segment_usage_block(sufile, segnum, create, &bh);
229 dofunc(sufile, segnum, header_bh, bh);
235 up_write(&NILFS_MDT(sufile)->mi_sem);
240 * nilfs_sufile_alloc - allocate a segment
241 * @sufile: inode of segment usage file
242 * @segnump: pointer to segment number
244 * Description: nilfs_sufile_alloc() allocates a clean segment.
246 * Return Value: On success, 0 is returned and the segment number of the
247 * allocated segment is stored in the place pointed by @segnump. On error, one
248 * of the following negative error codes is returned.
252 * %-ENOMEM - Insufficient amount of memory available.
254 * %-ENOSPC - No clean segment left.
256 int nilfs_sufile_alloc(struct inode *sufile, __u64 *segnump)
258 struct buffer_head *header_bh, *su_bh;
259 struct nilfs_sufile_header *header;
260 struct nilfs_segment_usage *su;
261 size_t susz = NILFS_MDT(sufile)->mi_entry_size;
262 __u64 segnum, maxsegnum, last_alloc;
264 unsigned long nsegments, ncleansegs, nsus;
267 down_write(&NILFS_MDT(sufile)->mi_sem);
269 ret = nilfs_sufile_get_header_block(sufile, &header_bh);
272 kaddr = kmap_atomic(header_bh->b_page, KM_USER0);
273 header = nilfs_sufile_block_get_header(sufile, header_bh, kaddr);
274 ncleansegs = le64_to_cpu(header->sh_ncleansegs);
275 last_alloc = le64_to_cpu(header->sh_last_alloc);
276 kunmap_atomic(kaddr, KM_USER0);
278 nsegments = nilfs_sufile_get_nsegments(sufile);
279 segnum = last_alloc + 1;
280 maxsegnum = nsegments - 1;
281 for (i = 0; i < nsegments; i += nsus) {
282 if (segnum >= nsegments) {
285 maxsegnum = last_alloc;
287 ret = nilfs_sufile_get_segment_usage_block(sufile, segnum, 1,
291 kaddr = kmap_atomic(su_bh->b_page, KM_USER0);
292 su = nilfs_sufile_block_get_segment_usage(
293 sufile, segnum, su_bh, kaddr);
295 nsus = nilfs_sufile_segment_usages_in_block(
296 sufile, segnum, maxsegnum);
297 for (j = 0; j < nsus; j++, su = (void *)su + susz, segnum++) {
298 if (!nilfs_segment_usage_clean(su))
300 /* found a clean segment */
301 nilfs_segment_usage_set_dirty(su);
302 kunmap_atomic(kaddr, KM_USER0);
304 kaddr = kmap_atomic(header_bh->b_page, KM_USER0);
305 header = nilfs_sufile_block_get_header(
306 sufile, header_bh, kaddr);
307 le64_add_cpu(&header->sh_ncleansegs, -1);
308 le64_add_cpu(&header->sh_ndirtysegs, 1);
309 header->sh_last_alloc = cpu_to_le64(segnum);
310 kunmap_atomic(kaddr, KM_USER0);
312 nilfs_mdt_mark_buffer_dirty(header_bh);
313 nilfs_mdt_mark_buffer_dirty(su_bh);
314 nilfs_mdt_mark_dirty(sufile);
320 kunmap_atomic(kaddr, KM_USER0);
324 /* no segments left */
331 up_write(&NILFS_MDT(sufile)->mi_sem);
335 void nilfs_sufile_do_cancel_free(struct inode *sufile, __u64 segnum,
336 struct buffer_head *header_bh,
337 struct buffer_head *su_bh)
339 struct nilfs_segment_usage *su;
342 kaddr = kmap_atomic(su_bh->b_page, KM_USER0);
343 su = nilfs_sufile_block_get_segment_usage(sufile, segnum, su_bh, kaddr);
344 if (unlikely(!nilfs_segment_usage_clean(su))) {
345 printk(KERN_WARNING "%s: segment %llu must be clean\n",
346 __func__, (unsigned long long)segnum);
347 kunmap_atomic(kaddr, KM_USER0);
350 nilfs_segment_usage_set_dirty(su);
351 kunmap_atomic(kaddr, KM_USER0);
353 nilfs_sufile_mod_counter(header_bh, -1, 1);
354 nilfs_mdt_mark_buffer_dirty(su_bh);
355 nilfs_mdt_mark_dirty(sufile);
358 void nilfs_sufile_do_scrap(struct inode *sufile, __u64 segnum,
359 struct buffer_head *header_bh,
360 struct buffer_head *su_bh)
362 struct nilfs_segment_usage *su;
366 kaddr = kmap_atomic(su_bh->b_page, KM_USER0);
367 su = nilfs_sufile_block_get_segment_usage(sufile, segnum, su_bh, kaddr);
368 if (su->su_flags == cpu_to_le32(1UL << NILFS_SEGMENT_USAGE_DIRTY) &&
369 su->su_nblocks == cpu_to_le32(0)) {
370 kunmap_atomic(kaddr, KM_USER0);
373 clean = nilfs_segment_usage_clean(su);
374 dirty = nilfs_segment_usage_dirty(su);
376 /* make the segment garbage */
377 su->su_lastmod = cpu_to_le64(0);
378 su->su_nblocks = cpu_to_le32(0);
379 su->su_flags = cpu_to_le32(1UL << NILFS_SEGMENT_USAGE_DIRTY);
380 kunmap_atomic(kaddr, KM_USER0);
382 nilfs_sufile_mod_counter(header_bh, clean ? (u64)-1 : 0, dirty ? 0 : 1);
383 nilfs_mdt_mark_buffer_dirty(su_bh);
384 nilfs_mdt_mark_dirty(sufile);
387 void nilfs_sufile_do_free(struct inode *sufile, __u64 segnum,
388 struct buffer_head *header_bh,
389 struct buffer_head *su_bh)
391 struct nilfs_segment_usage *su;
395 kaddr = kmap_atomic(su_bh->b_page, KM_USER0);
396 su = nilfs_sufile_block_get_segment_usage(sufile, segnum, su_bh, kaddr);
397 if (nilfs_segment_usage_clean(su)) {
398 printk(KERN_WARNING "%s: segment %llu is already clean\n",
399 __func__, (unsigned long long)segnum);
400 kunmap_atomic(kaddr, KM_USER0);
403 WARN_ON(nilfs_segment_usage_error(su));
404 WARN_ON(!nilfs_segment_usage_dirty(su));
406 sudirty = nilfs_segment_usage_dirty(su);
407 nilfs_segment_usage_set_clean(su);
408 kunmap_atomic(kaddr, KM_USER0);
409 nilfs_mdt_mark_buffer_dirty(su_bh);
411 nilfs_sufile_mod_counter(header_bh, 1, sudirty ? (u64)-1 : 0);
412 nilfs_mdt_mark_dirty(sufile);
416 * nilfs_sufile_get_segment_usage - get a segment usage
417 * @sufile: inode of segment usage file
418 * @segnum: segment number
419 * @sup: pointer to segment usage
420 * @bhp: pointer to buffer head
422 * Description: nilfs_sufile_get_segment_usage() acquires the segment usage
423 * specified by @segnum.
425 * Return Value: On success, 0 is returned, and the segment usage and the
426 * buffer head of the buffer on which the segment usage is located are stored
427 * in the place pointed by @sup and @bhp, respectively. On error, one of the
428 * following negative error codes is returned.
432 * %-ENOMEM - Insufficient amount of memory available.
434 * %-EINVAL - Invalid segment usage number.
436 int nilfs_sufile_get_segment_usage(struct inode *sufile, __u64 segnum,
437 struct nilfs_segment_usage **sup,
438 struct buffer_head **bhp)
440 struct buffer_head *bh;
441 struct nilfs_segment_usage *su;
445 /* segnum is 0 origin */
446 if (segnum >= nilfs_sufile_get_nsegments(sufile))
448 down_write(&NILFS_MDT(sufile)->mi_sem);
449 ret = nilfs_sufile_get_segment_usage_block(sufile, segnum, 1, &bh);
452 kaddr = kmap(bh->b_page);
453 su = nilfs_sufile_block_get_segment_usage(sufile, segnum, bh, kaddr);
454 if (nilfs_segment_usage_error(su)) {
466 up_write(&NILFS_MDT(sufile)->mi_sem);
471 * nilfs_sufile_put_segment_usage - put a segment usage
472 * @sufile: inode of segment usage file
473 * @segnum: segment number
476 * Description: nilfs_sufile_put_segment_usage() releases the segment usage
477 * specified by @segnum. @bh must be the buffer head which have been returned
478 * by a previous call to nilfs_sufile_get_segment_usage() with @segnum.
480 void nilfs_sufile_put_segment_usage(struct inode *sufile, __u64 segnum,
481 struct buffer_head *bh)
488 * nilfs_sufile_get_stat - get segment usage statistics
489 * @sufile: inode of segment usage file
490 * @stat: pointer to a structure of segment usage statistics
492 * Description: nilfs_sufile_get_stat() returns information about segment
495 * Return Value: On success, 0 is returned, and segment usage information is
496 * stored in the place pointed by @stat. On error, one of the following
497 * negative error codes is returned.
501 * %-ENOMEM - Insufficient amount of memory available.
503 int nilfs_sufile_get_stat(struct inode *sufile, struct nilfs_sustat *sustat)
505 struct buffer_head *header_bh;
506 struct nilfs_sufile_header *header;
507 struct the_nilfs *nilfs = NILFS_MDT(sufile)->mi_nilfs;
511 down_read(&NILFS_MDT(sufile)->mi_sem);
513 ret = nilfs_sufile_get_header_block(sufile, &header_bh);
517 kaddr = kmap_atomic(header_bh->b_page, KM_USER0);
518 header = nilfs_sufile_block_get_header(sufile, header_bh, kaddr);
519 sustat->ss_nsegs = nilfs_sufile_get_nsegments(sufile);
520 sustat->ss_ncleansegs = le64_to_cpu(header->sh_ncleansegs);
521 sustat->ss_ndirtysegs = le64_to_cpu(header->sh_ndirtysegs);
522 sustat->ss_ctime = nilfs->ns_ctime;
523 sustat->ss_nongc_ctime = nilfs->ns_nongc_ctime;
524 spin_lock(&nilfs->ns_last_segment_lock);
525 sustat->ss_prot_seq = nilfs->ns_prot_seq;
526 spin_unlock(&nilfs->ns_last_segment_lock);
527 kunmap_atomic(kaddr, KM_USER0);
531 up_read(&NILFS_MDT(sufile)->mi_sem);
536 * nilfs_sufile_get_ncleansegs - get the number of clean segments
537 * @sufile: inode of segment usage file
538 * @nsegsp: pointer to the number of clean segments
540 * Description: nilfs_sufile_get_ncleansegs() acquires the number of clean
543 * Return Value: On success, 0 is returned and the number of clean segments is
544 * stored in the place pointed by @nsegsp. On error, one of the following
545 * negative error codes is returned.
549 * %-ENOMEM - Insufficient amount of memory available.
551 int nilfs_sufile_get_ncleansegs(struct inode *sufile, unsigned long *nsegsp)
553 struct nilfs_sustat sustat;
556 ret = nilfs_sufile_get_stat(sufile, &sustat);
558 *nsegsp = sustat.ss_ncleansegs;
562 void nilfs_sufile_do_set_error(struct inode *sufile, __u64 segnum,
563 struct buffer_head *header_bh,
564 struct buffer_head *su_bh)
566 struct nilfs_segment_usage *su;
570 kaddr = kmap_atomic(su_bh->b_page, KM_USER0);
571 su = nilfs_sufile_block_get_segment_usage(sufile, segnum, su_bh, kaddr);
572 if (nilfs_segment_usage_error(su)) {
573 kunmap_atomic(kaddr, KM_USER0);
576 suclean = nilfs_segment_usage_clean(su);
577 nilfs_segment_usage_set_error(su);
578 kunmap_atomic(kaddr, KM_USER0);
581 nilfs_sufile_mod_counter(header_bh, -1, 0);
582 nilfs_mdt_mark_buffer_dirty(su_bh);
583 nilfs_mdt_mark_dirty(sufile);
587 * nilfs_sufile_get_suinfo -
588 * @sufile: inode of segment usage file
589 * @segnum: segment number to start looking
590 * @buf: array of suinfo
591 * @sisz: byte size of suinfo
592 * @nsi: size of suinfo array
596 * Return Value: On success, 0 is returned and .... On error, one of the
597 * following negative error codes is returned.
601 * %-ENOMEM - Insufficient amount of memory available.
603 ssize_t nilfs_sufile_get_suinfo(struct inode *sufile, __u64 segnum, void *buf,
604 unsigned sisz, size_t nsi)
606 struct buffer_head *su_bh;
607 struct nilfs_segment_usage *su;
608 struct nilfs_suinfo *si = buf;
609 size_t susz = NILFS_MDT(sufile)->mi_entry_size;
610 struct the_nilfs *nilfs = NILFS_MDT(sufile)->mi_nilfs;
612 unsigned long nsegs, segusages_per_block;
616 down_read(&NILFS_MDT(sufile)->mi_sem);
618 segusages_per_block = nilfs_sufile_segment_usages_per_block(sufile);
619 nsegs = min_t(unsigned long,
620 nilfs_sufile_get_nsegments(sufile) - segnum,
622 for (i = 0; i < nsegs; i += n, segnum += n) {
623 n = min_t(unsigned long,
624 segusages_per_block -
625 nilfs_sufile_get_offset(sufile, segnum),
627 ret = nilfs_sufile_get_segment_usage_block(sufile, segnum, 0,
633 memset(si, 0, sisz * n);
634 si = (void *)si + sisz * n;
638 kaddr = kmap_atomic(su_bh->b_page, KM_USER0);
639 su = nilfs_sufile_block_get_segment_usage(
640 sufile, segnum, su_bh, kaddr);
642 j++, su = (void *)su + susz, si = (void *)si + sisz) {
643 si->sui_lastmod = le64_to_cpu(su->su_lastmod);
644 si->sui_nblocks = le32_to_cpu(su->su_nblocks);
645 si->sui_flags = le32_to_cpu(su->su_flags) &
646 ~(1UL << NILFS_SEGMENT_USAGE_ACTIVE);
647 if (nilfs_segment_is_active(nilfs, segnum + j))
649 (1UL << NILFS_SEGMENT_USAGE_ACTIVE);
651 kunmap_atomic(kaddr, KM_USER0);
657 up_read(&NILFS_MDT(sufile)->mi_sem);