Linux Audio

Check our new training course

Embedded Linux training

Mar 10-20, 2025, special US time zones
Register
Loading...
v5.4
   1// SPDX-License-Identifier: GPL-2.0+
   2/*
   3 * sufile.c - NILFS segment usage file.
   4 *
   5 * Copyright (C) 2006-2008 Nippon Telegraph and Telephone Corporation.
   6 *
   7 * Written by Koji Sato.
   8 * Revised by Ryusuke Konishi.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
   9 */
  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>
 
  16#include "mdt.h"
  17#include "sufile.h"
  18
  19#include <trace/events/nilfs2.h>
  20
  21/**
  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 */
  28struct nilfs_sufile_info {
  29	struct nilfs_mdt_info mi;
  30	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 */
  33};
  34
  35static inline struct nilfs_sufile_info *NILFS_SUI(struct inode *sufile)
  36{
  37	return (struct nilfs_sufile_info *)NILFS_MDT(sufile);
  38}
  39
  40static 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;
  50
  51	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;
  59
  60	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
  73static 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
  97static 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
 104static 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
 110	kaddr = kmap_atomic(header_bh->b_page);
 111	header = kaddr + bh_offset(header_bh);
 112	le64_add_cpu(&header->sh_ncleansegs, ncleanadd);
 113	le64_add_cpu(&header->sh_ndirtysegs, ndirtyadd);
 114	kunmap_atomic(kaddr);
 115
 116	mark_buffer_dirty(header_bh);
 117}
 118
 119/**
 120 * 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/**
 129 * 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))) {
 174			nilfs_msg(sufile->i_sb, KERN_WARNING,
 175				  "%s: invalid segment number: %llu",
 176				  __func__, (unsigned long long)*seg);
 177			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
 224int 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))) {
 233		nilfs_msg(sufile->i_sb, KERN_WARNING,
 234			  "%s: invalid segment number: %llu",
 235			  __func__, (unsigned long long)segnum);
 236		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
 256/**
 257 * 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/**
 286 * 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;
 305	struct nilfs_sufile_header *header;
 306	struct nilfs_segment_usage *su;
 307	struct nilfs_sufile_info *sui = NILFS_SUI(sufile);
 308	size_t susz = NILFS_MDT(sufile)->mi_entry_size;
 309	__u64 segnum, maxsegnum, last_alloc;
 310	void *kaddr;
 311	unsigned long nsegments, nsus, cnt;
 312	int ret, j;
 313
 314	down_write(&NILFS_MDT(sufile)->mi_sem);
 315
 316	ret = nilfs_sufile_get_header_block(sufile, &header_bh);
 317	if (ret < 0)
 318		goto out_sem;
 319	kaddr = kmap_atomic(header_bh->b_page);
 320	header = kaddr + bh_offset(header_bh);
 
 321	last_alloc = le64_to_cpu(header->sh_last_alloc);
 322	kunmap_atomic(kaddr);
 323
 324	nsegments = nilfs_sufile_get_nsegments(sufile);
 325	maxsegnum = sui->allocmax;
 326	segnum = last_alloc + 1;
 327	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			}
 350		}
 351		trace_nilfs2_segment_usage_check(sufile, segnum, cnt);
 352		ret = nilfs_sufile_get_segment_usage_block(sufile, segnum, 1,
 353							   &su_bh);
 354		if (ret < 0)
 355			goto out_header;
 356		kaddr = kmap_atomic(su_bh->b_page);
 357		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 */
 366			nilfs_segment_usage_set_dirty(su);
 367			kunmap_atomic(kaddr);
 368
 369			kaddr = kmap_atomic(header_bh->b_page);
 370			header = kaddr + bh_offset(header_bh);
 371			le64_add_cpu(&header->sh_ncleansegs, -1);
 372			le64_add_cpu(&header->sh_ndirtysegs, 1);
 373			header->sh_last_alloc = cpu_to_le64(segnum);
 374			kunmap_atomic(kaddr);
 375
 376			sui->ncleansegs--;
 377			mark_buffer_dirty(header_bh);
 378			mark_buffer_dirty(su_bh);
 379			nilfs_mdt_mark_dirty(sufile);
 380			brelse(su_bh);
 381			*segnump = segnum;
 382
 383			trace_nilfs2_segment_usage_allocated(sufile, segnum);
 384
 385			goto out_header;
 386		}
 387
 388		kunmap_atomic(kaddr);
 389		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
 403void nilfs_sufile_do_cancel_free(struct inode *sufile, __u64 segnum,
 404				 struct buffer_head *header_bh,
 405				 struct buffer_head *su_bh)
 406{
 407	struct nilfs_segment_usage *su;
 408	void *kaddr;
 409
 410	kaddr = kmap_atomic(su_bh->b_page);
 411	su = nilfs_sufile_block_get_segment_usage(sufile, segnum, su_bh, kaddr);
 412	if (unlikely(!nilfs_segment_usage_clean(su))) {
 413		nilfs_msg(sufile->i_sb, KERN_WARNING,
 414			  "%s: segment %llu must be clean", __func__,
 415			  (unsigned long long)segnum);
 416		kunmap_atomic(kaddr);
 417		return;
 418	}
 419	nilfs_segment_usage_set_dirty(su);
 420	kunmap_atomic(kaddr);
 421
 422	nilfs_sufile_mod_counter(header_bh, -1, 1);
 423	NILFS_SUI(sufile)->ncleansegs--;
 424
 425	mark_buffer_dirty(su_bh);
 426	nilfs_mdt_mark_dirty(sufile);
 427}
 428
 429void 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
 437	kaddr = kmap_atomic(su_bh->b_page);
 438	su = nilfs_sufile_block_get_segment_usage(sufile, segnum, su_bh, kaddr);
 439	if (su->su_flags == cpu_to_le32(BIT(NILFS_SEGMENT_USAGE_DIRTY)) &&
 440	    su->su_nblocks == cpu_to_le32(0)) {
 441		kunmap_atomic(kaddr);
 442		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);
 450	su->su_flags = cpu_to_le32(BIT(NILFS_SEGMENT_USAGE_DIRTY));
 451	kunmap_atomic(kaddr);
 452
 453	nilfs_sufile_mod_counter(header_bh, clean ? (u64)-1 : 0, dirty ? 0 : 1);
 454	NILFS_SUI(sufile)->ncleansegs -= clean;
 455
 456	mark_buffer_dirty(su_bh);
 457	nilfs_mdt_mark_dirty(sufile);
 458}
 459
 460void nilfs_sufile_do_free(struct inode *sufile, __u64 segnum,
 461			  struct buffer_head *header_bh,
 462			  struct buffer_head *su_bh)
 463{
 464	struct nilfs_segment_usage *su;
 465	void *kaddr;
 466	int sudirty;
 467
 468	kaddr = kmap_atomic(su_bh->b_page);
 469	su = nilfs_sufile_block_get_segment_usage(sufile, segnum, su_bh, kaddr);
 470	if (nilfs_segment_usage_clean(su)) {
 471		nilfs_msg(sufile->i_sb, KERN_WARNING,
 472			  "%s: segment %llu is already clean",
 473			  __func__, (unsigned long long)segnum);
 474		kunmap_atomic(kaddr);
 475		return;
 476	}
 477	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);
 482	kunmap_atomic(kaddr);
 483	mark_buffer_dirty(su_bh);
 484
 485	nilfs_sufile_mod_counter(header_bh, 1, sudirty ? (u64)-1 : 0);
 486	NILFS_SUI(sufile)->ncleansegs++;
 487
 488	nilfs_mdt_mark_dirty(sufile);
 489
 490	trace_nilfs2_segment_usage_freed(sufile, segnum);
 491}
 492
 493/**
 494 * 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) {
 505		mark_buffer_dirty(bh);
 506		nilfs_mdt_mark_dirty(sufile);
 507		brelse(bh);
 508	}
 509	return ret;
 510}
 511
 512/**
 513 * 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,
 520				   unsigned long nblocks, time64_t modtime)
 521{
 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
 532	kaddr = kmap_atomic(bh->b_page);
 533	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);
 538	kunmap_atomic(kaddr);
 539
 540	mark_buffer_dirty(bh);
 541	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/**
 550 * 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;
 569	struct the_nilfs *nilfs = sufile->i_sb->s_fs_info;
 570	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
 579	kaddr = kmap_atomic(header_bh->b_page);
 580	header = kaddr + bh_offset(header_bh);
 581	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);
 584	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);
 589	kunmap_atomic(kaddr);
 590	brelse(header_bh);
 591
 592 out_sem:
 593	up_read(&NILFS_MDT(sufile)->mi_sem);
 594	return ret;
 595}
 596
 597void nilfs_sufile_do_set_error(struct inode *sufile, __u64 segnum,
 598			       struct buffer_head *header_bh,
 599			       struct buffer_head *su_bh)
 600{
 601	struct nilfs_segment_usage *su;
 602	void *kaddr;
 603	int suclean;
 604
 605	kaddr = kmap_atomic(su_bh->b_page);
 606	su = nilfs_sufile_block_get_segment_usage(sufile, segnum, su_bh, kaddr);
 607	if (nilfs_segment_usage_error(su)) {
 608		kunmap_atomic(kaddr);
 609		return;
 610	}
 611	suclean = nilfs_segment_usage_clean(su);
 612	nilfs_segment_usage_set_error(su);
 613	kunmap_atomic(kaddr);
 614
 615	if (suclean) {
 616		nilfs_sufile_mod_counter(header_bh, -1, 0);
 617		NILFS_SUI(sufile)->ncleansegs--;
 618	}
 619	mark_buffer_dirty(su_bh);
 620	nilfs_mdt_mark_dirty(sufile);
 621}
 622
 623/**
 624 * 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 */
 640static 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		}
 682		kaddr = kmap_atomic(su_bh->b_page);
 683		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) &
 688			     ~BIT(NILFS_SEGMENT_USAGE_ERROR)) ||
 689			    nilfs_segment_is_active(nilfs, segnum + j)) {
 690				ret = -EBUSY;
 691				kunmap_atomic(kaddr);
 692				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		}
 703		kunmap_atomic(kaddr);
 704		if (nc > 0) {
 705			mark_buffer_dirty(su_bh);
 706			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/**
 729 * 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
 779	kaddr = kmap_atomic(header_bh->b_page);
 780	header = kaddr + bh_offset(header_bh);
 781	header->sh_ncleansegs = cpu_to_le64(sui->ncleansegs);
 782	kunmap_atomic(kaddr);
 783
 784	mark_buffer_dirty(header_bh);
 785	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/**
 796 * nilfs_sufile_get_suinfo -
 797 * @sufile: inode of segment usage file
 798 * @segnum: segment number to start looking
 799 * @buf: array of suinfo
 800 * @sisz: byte size of suinfo
 801 * @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 */
 812ssize_t nilfs_sufile_get_suinfo(struct inode *sufile, __u64 segnum, void *buf,
 813				unsigned int sisz, size_t nsi)
 814{
 815	struct buffer_head *su_bh;
 816	struct nilfs_segment_usage *su;
 817	struct nilfs_suinfo *si = buf;
 818	size_t susz = NILFS_MDT(sufile)->mi_entry_size;
 819	struct the_nilfs *nilfs = sufile->i_sb->s_fs_info;
 820	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 */
 842			memset(si, 0, sisz * n);
 843			si = (void *)si + sisz * n;
 844			continue;
 845		}
 846
 847		kaddr = kmap_atomic(su_bh->b_page);
 848		su = nilfs_sufile_block_get_segment_usage(
 849			sufile, segnum, su_bh, kaddr);
 850		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) &
 855				~BIT(NILFS_SEGMENT_USAGE_ACTIVE);
 856			if (nilfs_segment_is_active(nilfs, segnum + j))
 857				si->sui_flags |=
 858					BIT(NILFS_SEGMENT_USAGE_ACTIVE);
 859		}
 860		kunmap_atomic(kaddr);
 861		brelse(su_bh);
 862	}
 863	ret = nsegs;
 864
 865 out:
 866	up_read(&NILFS_MDT(sufile)->mi_sem);
 867	return ret;
 868}
 869
 870/**
 871 * 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,
 891				unsigned int supsz, size_t nsup)
 892{
 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 &=
 946					~BIT(NILFS_SEGMENT_USAGE_ACTIVE);
 947
 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/**
1002 * 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/**
1154 * nilfs_sufile_read - read or get sufile inode
1155 * @sb: super block instance
1156 * @susize: size of a segment usage entry
1157 * @raw_inode: on-disk sufile inode
1158 * @inodep: buffer to store the inode
1159 */
1160int nilfs_sufile_read(struct super_block *sb, size_t susize,
1161		      struct nilfs_inode *raw_inode, struct inode **inodep)
1162{
1163	struct inode *sufile;
1164	struct nilfs_sufile_info *sui;
1165	struct buffer_head *header_bh;
1166	struct nilfs_sufile_header *header;
1167	void *kaddr;
1168	int err;
1169
1170	if (susize > sb->s_blocksize) {
1171		nilfs_msg(sb, KERN_ERR,
1172			  "too large segment usage size: %zu bytes", susize);
 
1173		return -EINVAL;
1174	} else if (susize < NILFS_MIN_SEGMENT_USAGE_SIZE) {
1175		nilfs_msg(sb, KERN_ERR,
1176			  "too small segment usage size: %zu bytes", susize);
 
1177		return -EINVAL;
1178	}
1179
1180	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;
1185
1186	err = nilfs_mdt_init(sufile, NILFS_MDT_GFP, sizeof(*sui));
1187	if (err)
1188		goto failed;
1189
1190	nilfs_mdt_set_entry_size(sufile, susize,
1191				 sizeof(struct nilfs_sufile_header));
1192
1193	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);
1202	kaddr = kmap_atomic(header_bh->b_page);
1203	header = kaddr + bh_offset(header_bh);
1204	sui->ncleansegs = le64_to_cpu(header->sh_ncleansegs);
1205	kunmap_atomic(kaddr);
1206	brelse(header_bh);
1207
1208	sui->allocmax = nilfs_sufile_get_nsegments(sufile) - 1;
1209	sui->allocmin = 0;
1210
1211	unlock_new_inode(sufile);
1212 out:
1213	*inodep = sufile;
1214	return 0;
1215 failed:
1216	iget_failed(sufile);
1217	return err;
1218}
v3.15
 
   1/*
   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>.
  21 * Revised by Ryusuke Konishi <ryusuke@osrg.net>.
  22 */
  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
 
 
  33/**
  34 * struct nilfs_sufile_info - on-memory private data of sufile
  35 * @mi: on-memory private data of metadata file
  36 * @ncleansegs: number of clean segments
  37 * @allocmin: lower limit of allocatable segment range
  38 * @allocmax: upper limit of allocatable segment range
  39 */
  40struct nilfs_sufile_info {
  41	struct nilfs_mdt_info mi;
  42	unsigned long ncleansegs;/* number of clean segments */
  43	__u64 allocmin;		/* lower limit of allocatable segment range */
  44	__u64 allocmax;		/* upper limit of allocatable segment range */
  45};
  46
  47static inline struct nilfs_sufile_info *NILFS_SUI(struct inode *sufile)
  48{
  49	return (struct nilfs_sufile_info *)NILFS_MDT(sufile);
  50}
  51
  52static inline unsigned long
  53nilfs_sufile_segment_usages_per_block(const struct inode *sufile)
  54{
  55	return NILFS_MDT(sufile)->mi_entries_per_block;
  56}
  57
  58static unsigned long
  59nilfs_sufile_get_blkoff(const struct inode *sufile, __u64 segnum)
  60{
  61	__u64 t = segnum + NILFS_MDT(sufile)->mi_first_entry_offset;
 
  62	do_div(t, nilfs_sufile_segment_usages_per_block(sufile));
  63	return (unsigned long)t;
  64}
  65
  66static unsigned long
  67nilfs_sufile_get_offset(const struct inode *sufile, __u64 segnum)
  68{
  69	__u64 t = segnum + NILFS_MDT(sufile)->mi_first_entry_offset;
 
  70	return do_div(t, nilfs_sufile_segment_usages_per_block(sufile));
  71}
  72
  73static unsigned long
  74nilfs_sufile_segment_usages_in_block(const struct inode *sufile, __u64 curr,
  75				     __u64 max)
  76{
  77	return min_t(unsigned long,
  78		     nilfs_sufile_segment_usages_per_block(sufile) -
  79		     nilfs_sufile_get_offset(sufile, curr),
  80		     max - curr + 1);
  81}
  82
  83static struct nilfs_segment_usage *
  84nilfs_sufile_block_get_segment_usage(const struct inode *sufile, __u64 segnum,
  85				     struct buffer_head *bh, void *kaddr)
  86{
  87	return kaddr + bh_offset(bh) +
  88		nilfs_sufile_get_offset(sufile, segnum) *
  89		NILFS_MDT(sufile)->mi_entry_size;
  90}
  91
  92static inline int nilfs_sufile_get_header_block(struct inode *sufile,
  93						struct buffer_head **bhp)
  94{
  95	return nilfs_mdt_get_block(sufile, 0, 0, NULL, bhp);
  96}
  97
  98static inline int
  99nilfs_sufile_get_segment_usage_block(struct inode *sufile, __u64 segnum,
 100				     int create, struct buffer_head **bhp)
 101{
 102	return nilfs_mdt_get_block(sufile,
 103				   nilfs_sufile_get_blkoff(sufile, segnum),
 104				   create, NULL, bhp);
 105}
 106
 107static int nilfs_sufile_delete_segment_usage_block(struct inode *sufile,
 108						   __u64 segnum)
 109{
 110	return nilfs_mdt_delete_block(sufile,
 111				      nilfs_sufile_get_blkoff(sufile, segnum));
 112}
 113
 114static void nilfs_sufile_mod_counter(struct buffer_head *header_bh,
 115				     u64 ncleanadd, u64 ndirtyadd)
 116{
 117	struct nilfs_sufile_header *header;
 118	void *kaddr;
 119
 120	kaddr = kmap_atomic(header_bh->b_page);
 121	header = kaddr + bh_offset(header_bh);
 122	le64_add_cpu(&header->sh_ncleansegs, ncleanadd);
 123	le64_add_cpu(&header->sh_ndirtysegs, ndirtyadd);
 124	kunmap_atomic(kaddr);
 125
 126	mark_buffer_dirty(header_bh);
 127}
 128
 129/**
 130 * nilfs_sufile_get_ncleansegs - return the number of clean segments
 131 * @sufile: inode of segment usage file
 132 */
 133unsigned long nilfs_sufile_get_ncleansegs(struct inode *sufile)
 134{
 135	return NILFS_SUI(sufile)->ncleansegs;
 136}
 137
 138/**
 139 * nilfs_sufile_updatev - modify multiple segment usages at a time
 140 * @sufile: inode of segment usage file
 141 * @segnumv: array of segment numbers
 142 * @nsegs: size of @segnumv array
 143 * @create: creation flag
 144 * @ndone: place to store number of modified segments on @segnumv
 145 * @dofunc: primitive operation for the update
 146 *
 147 * Description: nilfs_sufile_updatev() repeatedly calls @dofunc
 148 * against the given array of segments.  The @dofunc is called with
 149 * buffers of a header block and the sufile block in which the target
 150 * segment usage entry is contained.  If @ndone is given, the number
 151 * of successfully modified segments from the head is stored in the
 152 * place @ndone points to.
 153 *
 154 * Return Value: On success, zero is returned.  On error, one of the
 155 * following negative error codes is returned.
 156 *
 157 * %-EIO - I/O error.
 158 *
 159 * %-ENOMEM - Insufficient amount of memory available.
 160 *
 161 * %-ENOENT - Given segment usage is in hole block (may be returned if
 162 *            @create is zero)
 163 *
 164 * %-EINVAL - Invalid segment usage number
 165 */
 166int nilfs_sufile_updatev(struct inode *sufile, __u64 *segnumv, size_t nsegs,
 167			 int create, size_t *ndone,
 168			 void (*dofunc)(struct inode *, __u64,
 169					struct buffer_head *,
 170					struct buffer_head *))
 171{
 172	struct buffer_head *header_bh, *bh;
 173	unsigned long blkoff, prev_blkoff;
 174	__u64 *seg;
 175	size_t nerr = 0, n = 0;
 176	int ret = 0;
 177
 178	if (unlikely(nsegs == 0))
 179		goto out;
 180
 181	down_write(&NILFS_MDT(sufile)->mi_sem);
 182	for (seg = segnumv; seg < segnumv + nsegs; seg++) {
 183		if (unlikely(*seg >= nilfs_sufile_get_nsegments(sufile))) {
 184			printk(KERN_WARNING
 185			       "%s: invalid segment number: %llu\n", __func__,
 186			       (unsigned long long)*seg);
 187			nerr++;
 188		}
 189	}
 190	if (nerr > 0) {
 191		ret = -EINVAL;
 192		goto out_sem;
 193	}
 194
 195	ret = nilfs_sufile_get_header_block(sufile, &header_bh);
 196	if (ret < 0)
 197		goto out_sem;
 198
 199	seg = segnumv;
 200	blkoff = nilfs_sufile_get_blkoff(sufile, *seg);
 201	ret = nilfs_mdt_get_block(sufile, blkoff, create, NULL, &bh);
 202	if (ret < 0)
 203		goto out_header;
 204
 205	for (;;) {
 206		dofunc(sufile, *seg, header_bh, bh);
 207
 208		if (++seg >= segnumv + nsegs)
 209			break;
 210		prev_blkoff = blkoff;
 211		blkoff = nilfs_sufile_get_blkoff(sufile, *seg);
 212		if (blkoff == prev_blkoff)
 213			continue;
 214
 215		/* get different block */
 216		brelse(bh);
 217		ret = nilfs_mdt_get_block(sufile, blkoff, create, NULL, &bh);
 218		if (unlikely(ret < 0))
 219			goto out_header;
 220	}
 221	brelse(bh);
 222
 223 out_header:
 224	n = seg - segnumv;
 225	brelse(header_bh);
 226 out_sem:
 227	up_write(&NILFS_MDT(sufile)->mi_sem);
 228 out:
 229	if (ndone)
 230		*ndone = n;
 231	return ret;
 232}
 233
 234int nilfs_sufile_update(struct inode *sufile, __u64 segnum, int create,
 235			void (*dofunc)(struct inode *, __u64,
 236				       struct buffer_head *,
 237				       struct buffer_head *))
 238{
 239	struct buffer_head *header_bh, *bh;
 240	int ret;
 241
 242	if (unlikely(segnum >= nilfs_sufile_get_nsegments(sufile))) {
 243		printk(KERN_WARNING "%s: invalid segment number: %llu\n",
 244		       __func__, (unsigned long long)segnum);
 
 245		return -EINVAL;
 246	}
 247	down_write(&NILFS_MDT(sufile)->mi_sem);
 248
 249	ret = nilfs_sufile_get_header_block(sufile, &header_bh);
 250	if (ret < 0)
 251		goto out_sem;
 252
 253	ret = nilfs_sufile_get_segment_usage_block(sufile, segnum, create, &bh);
 254	if (!ret) {
 255		dofunc(sufile, segnum, header_bh, bh);
 256		brelse(bh);
 257	}
 258	brelse(header_bh);
 259
 260 out_sem:
 261	up_write(&NILFS_MDT(sufile)->mi_sem);
 262	return ret;
 263}
 264
 265/**
 266 * nilfs_sufile_set_alloc_range - limit range of segment to be allocated
 267 * @sufile: inode of segment usage file
 268 * @start: minimum segment number of allocatable region (inclusive)
 269 * @end: maximum segment number of allocatable region (inclusive)
 270 *
 271 * Return Value: On success, 0 is returned.  On error, one of the
 272 * following negative error codes is returned.
 273 *
 274 * %-ERANGE - invalid segment region
 275 */
 276int nilfs_sufile_set_alloc_range(struct inode *sufile, __u64 start, __u64 end)
 277{
 278	struct nilfs_sufile_info *sui = NILFS_SUI(sufile);
 279	__u64 nsegs;
 280	int ret = -ERANGE;
 281
 282	down_write(&NILFS_MDT(sufile)->mi_sem);
 283	nsegs = nilfs_sufile_get_nsegments(sufile);
 284
 285	if (start <= end && end < nsegs) {
 286		sui->allocmin = start;
 287		sui->allocmax = end;
 288		ret = 0;
 289	}
 290	up_write(&NILFS_MDT(sufile)->mi_sem);
 291	return ret;
 292}
 293
 294/**
 295 * nilfs_sufile_alloc - allocate a segment
 296 * @sufile: inode of segment usage file
 297 * @segnump: pointer to segment number
 298 *
 299 * Description: nilfs_sufile_alloc() allocates a clean segment.
 300 *
 301 * Return Value: On success, 0 is returned and the segment number of the
 302 * allocated segment is stored in the place pointed by @segnump. On error, one
 303 * of the following negative error codes is returned.
 304 *
 305 * %-EIO - I/O error.
 306 *
 307 * %-ENOMEM - Insufficient amount of memory available.
 308 *
 309 * %-ENOSPC - No clean segment left.
 310 */
 311int nilfs_sufile_alloc(struct inode *sufile, __u64 *segnump)
 312{
 313	struct buffer_head *header_bh, *su_bh;
 314	struct nilfs_sufile_header *header;
 315	struct nilfs_segment_usage *su;
 316	struct nilfs_sufile_info *sui = NILFS_SUI(sufile);
 317	size_t susz = NILFS_MDT(sufile)->mi_entry_size;
 318	__u64 segnum, maxsegnum, last_alloc;
 319	void *kaddr;
 320	unsigned long nsegments, ncleansegs, nsus, cnt;
 321	int ret, j;
 322
 323	down_write(&NILFS_MDT(sufile)->mi_sem);
 324
 325	ret = nilfs_sufile_get_header_block(sufile, &header_bh);
 326	if (ret < 0)
 327		goto out_sem;
 328	kaddr = kmap_atomic(header_bh->b_page);
 329	header = kaddr + bh_offset(header_bh);
 330	ncleansegs = le64_to_cpu(header->sh_ncleansegs);
 331	last_alloc = le64_to_cpu(header->sh_last_alloc);
 332	kunmap_atomic(kaddr);
 333
 334	nsegments = nilfs_sufile_get_nsegments(sufile);
 335	maxsegnum = sui->allocmax;
 336	segnum = last_alloc + 1;
 337	if (segnum < sui->allocmin || segnum > sui->allocmax)
 338		segnum = sui->allocmin;
 339
 340	for (cnt = 0; cnt < nsegments; cnt += nsus) {
 341		if (segnum > maxsegnum) {
 342			if (cnt < sui->allocmax - sui->allocmin + 1) {
 343				/*
 344				 * wrap around in the limited region.
 345				 * if allocation started from
 346				 * sui->allocmin, this never happens.
 347				 */
 348				segnum = sui->allocmin;
 349				maxsegnum = last_alloc;
 350			} else if (segnum > sui->allocmin &&
 351				   sui->allocmax + 1 < nsegments) {
 352				segnum = sui->allocmax + 1;
 353				maxsegnum = nsegments - 1;
 354			} else if (sui->allocmin > 0)  {
 355				segnum = 0;
 356				maxsegnum = sui->allocmin - 1;
 357			} else {
 358				break; /* never happens */
 359			}
 360		}
 
 361		ret = nilfs_sufile_get_segment_usage_block(sufile, segnum, 1,
 362							   &su_bh);
 363		if (ret < 0)
 364			goto out_header;
 365		kaddr = kmap_atomic(su_bh->b_page);
 366		su = nilfs_sufile_block_get_segment_usage(
 367			sufile, segnum, su_bh, kaddr);
 368
 369		nsus = nilfs_sufile_segment_usages_in_block(
 370			sufile, segnum, maxsegnum);
 371		for (j = 0; j < nsus; j++, su = (void *)su + susz, segnum++) {
 372			if (!nilfs_segment_usage_clean(su))
 373				continue;
 374			/* found a clean segment */
 375			nilfs_segment_usage_set_dirty(su);
 376			kunmap_atomic(kaddr);
 377
 378			kaddr = kmap_atomic(header_bh->b_page);
 379			header = kaddr + bh_offset(header_bh);
 380			le64_add_cpu(&header->sh_ncleansegs, -1);
 381			le64_add_cpu(&header->sh_ndirtysegs, 1);
 382			header->sh_last_alloc = cpu_to_le64(segnum);
 383			kunmap_atomic(kaddr);
 384
 385			sui->ncleansegs--;
 386			mark_buffer_dirty(header_bh);
 387			mark_buffer_dirty(su_bh);
 388			nilfs_mdt_mark_dirty(sufile);
 389			brelse(su_bh);
 390			*segnump = segnum;
 
 
 
 391			goto out_header;
 392		}
 393
 394		kunmap_atomic(kaddr);
 395		brelse(su_bh);
 396	}
 397
 398	/* no segments left */
 399	ret = -ENOSPC;
 400
 401 out_header:
 402	brelse(header_bh);
 403
 404 out_sem:
 405	up_write(&NILFS_MDT(sufile)->mi_sem);
 406	return ret;
 407}
 408
 409void nilfs_sufile_do_cancel_free(struct inode *sufile, __u64 segnum,
 410				 struct buffer_head *header_bh,
 411				 struct buffer_head *su_bh)
 412{
 413	struct nilfs_segment_usage *su;
 414	void *kaddr;
 415
 416	kaddr = kmap_atomic(su_bh->b_page);
 417	su = nilfs_sufile_block_get_segment_usage(sufile, segnum, su_bh, kaddr);
 418	if (unlikely(!nilfs_segment_usage_clean(su))) {
 419		printk(KERN_WARNING "%s: segment %llu must be clean\n",
 420		       __func__, (unsigned long long)segnum);
 
 421		kunmap_atomic(kaddr);
 422		return;
 423	}
 424	nilfs_segment_usage_set_dirty(su);
 425	kunmap_atomic(kaddr);
 426
 427	nilfs_sufile_mod_counter(header_bh, -1, 1);
 428	NILFS_SUI(sufile)->ncleansegs--;
 429
 430	mark_buffer_dirty(su_bh);
 431	nilfs_mdt_mark_dirty(sufile);
 432}
 433
 434void nilfs_sufile_do_scrap(struct inode *sufile, __u64 segnum,
 435			   struct buffer_head *header_bh,
 436			   struct buffer_head *su_bh)
 437{
 438	struct nilfs_segment_usage *su;
 439	void *kaddr;
 440	int clean, dirty;
 441
 442	kaddr = kmap_atomic(su_bh->b_page);
 443	su = nilfs_sufile_block_get_segment_usage(sufile, segnum, su_bh, kaddr);
 444	if (su->su_flags == cpu_to_le32(1UL << NILFS_SEGMENT_USAGE_DIRTY) &&
 445	    su->su_nblocks == cpu_to_le32(0)) {
 446		kunmap_atomic(kaddr);
 447		return;
 448	}
 449	clean = nilfs_segment_usage_clean(su);
 450	dirty = nilfs_segment_usage_dirty(su);
 451
 452	/* make the segment garbage */
 453	su->su_lastmod = cpu_to_le64(0);
 454	su->su_nblocks = cpu_to_le32(0);
 455	su->su_flags = cpu_to_le32(1UL << NILFS_SEGMENT_USAGE_DIRTY);
 456	kunmap_atomic(kaddr);
 457
 458	nilfs_sufile_mod_counter(header_bh, clean ? (u64)-1 : 0, dirty ? 0 : 1);
 459	NILFS_SUI(sufile)->ncleansegs -= clean;
 460
 461	mark_buffer_dirty(su_bh);
 462	nilfs_mdt_mark_dirty(sufile);
 463}
 464
 465void nilfs_sufile_do_free(struct inode *sufile, __u64 segnum,
 466			  struct buffer_head *header_bh,
 467			  struct buffer_head *su_bh)
 468{
 469	struct nilfs_segment_usage *su;
 470	void *kaddr;
 471	int sudirty;
 472
 473	kaddr = kmap_atomic(su_bh->b_page);
 474	su = nilfs_sufile_block_get_segment_usage(sufile, segnum, su_bh, kaddr);
 475	if (nilfs_segment_usage_clean(su)) {
 476		printk(KERN_WARNING "%s: segment %llu is already clean\n",
 477		       __func__, (unsigned long long)segnum);
 
 478		kunmap_atomic(kaddr);
 479		return;
 480	}
 481	WARN_ON(nilfs_segment_usage_error(su));
 482	WARN_ON(!nilfs_segment_usage_dirty(su));
 483
 484	sudirty = nilfs_segment_usage_dirty(su);
 485	nilfs_segment_usage_set_clean(su);
 486	kunmap_atomic(kaddr);
 487	mark_buffer_dirty(su_bh);
 488
 489	nilfs_sufile_mod_counter(header_bh, 1, sudirty ? (u64)-1 : 0);
 490	NILFS_SUI(sufile)->ncleansegs++;
 491
 492	nilfs_mdt_mark_dirty(sufile);
 
 
 493}
 494
 495/**
 496 * nilfs_sufile_mark_dirty - mark the buffer having a segment usage dirty
 497 * @sufile: inode of segment usage file
 498 * @segnum: segment number
 499 */
 500int nilfs_sufile_mark_dirty(struct inode *sufile, __u64 segnum)
 501{
 502	struct buffer_head *bh;
 503	int ret;
 504
 505	ret = nilfs_sufile_get_segment_usage_block(sufile, segnum, 0, &bh);
 506	if (!ret) {
 507		mark_buffer_dirty(bh);
 508		nilfs_mdt_mark_dirty(sufile);
 509		brelse(bh);
 510	}
 511	return ret;
 512}
 513
 514/**
 515 * nilfs_sufile_set_segment_usage - set usage of a segment
 516 * @sufile: inode of segment usage file
 517 * @segnum: segment number
 518 * @nblocks: number of live blocks in the segment
 519 * @modtime: modification time (option)
 520 */
 521int nilfs_sufile_set_segment_usage(struct inode *sufile, __u64 segnum,
 522				   unsigned long nblocks, time_t modtime)
 523{
 524	struct buffer_head *bh;
 525	struct nilfs_segment_usage *su;
 526	void *kaddr;
 527	int ret;
 528
 529	down_write(&NILFS_MDT(sufile)->mi_sem);
 530	ret = nilfs_sufile_get_segment_usage_block(sufile, segnum, 0, &bh);
 531	if (ret < 0)
 532		goto out_sem;
 533
 534	kaddr = kmap_atomic(bh->b_page);
 535	su = nilfs_sufile_block_get_segment_usage(sufile, segnum, bh, kaddr);
 536	WARN_ON(nilfs_segment_usage_error(su));
 537	if (modtime)
 538		su->su_lastmod = cpu_to_le64(modtime);
 539	su->su_nblocks = cpu_to_le32(nblocks);
 540	kunmap_atomic(kaddr);
 541
 542	mark_buffer_dirty(bh);
 543	nilfs_mdt_mark_dirty(sufile);
 544	brelse(bh);
 545
 546 out_sem:
 547	up_write(&NILFS_MDT(sufile)->mi_sem);
 548	return ret;
 549}
 550
 551/**
 552 * nilfs_sufile_get_stat - get segment usage statistics
 553 * @sufile: inode of segment usage file
 554 * @stat: pointer to a structure of segment usage statistics
 555 *
 556 * Description: nilfs_sufile_get_stat() returns information about segment
 557 * usage.
 558 *
 559 * Return Value: On success, 0 is returned, and segment usage information is
 560 * stored in the place pointed by @stat. On error, one of the following
 561 * negative error codes is returned.
 562 *
 563 * %-EIO - I/O error.
 564 *
 565 * %-ENOMEM - Insufficient amount of memory available.
 566 */
 567int nilfs_sufile_get_stat(struct inode *sufile, struct nilfs_sustat *sustat)
 568{
 569	struct buffer_head *header_bh;
 570	struct nilfs_sufile_header *header;
 571	struct the_nilfs *nilfs = sufile->i_sb->s_fs_info;
 572	void *kaddr;
 573	int ret;
 574
 575	down_read(&NILFS_MDT(sufile)->mi_sem);
 576
 577	ret = nilfs_sufile_get_header_block(sufile, &header_bh);
 578	if (ret < 0)
 579		goto out_sem;
 580
 581	kaddr = kmap_atomic(header_bh->b_page);
 582	header = kaddr + bh_offset(header_bh);
 583	sustat->ss_nsegs = nilfs_sufile_get_nsegments(sufile);
 584	sustat->ss_ncleansegs = le64_to_cpu(header->sh_ncleansegs);
 585	sustat->ss_ndirtysegs = le64_to_cpu(header->sh_ndirtysegs);
 586	sustat->ss_ctime = nilfs->ns_ctime;
 587	sustat->ss_nongc_ctime = nilfs->ns_nongc_ctime;
 588	spin_lock(&nilfs->ns_last_segment_lock);
 589	sustat->ss_prot_seq = nilfs->ns_prot_seq;
 590	spin_unlock(&nilfs->ns_last_segment_lock);
 591	kunmap_atomic(kaddr);
 592	brelse(header_bh);
 593
 594 out_sem:
 595	up_read(&NILFS_MDT(sufile)->mi_sem);
 596	return ret;
 597}
 598
 599void nilfs_sufile_do_set_error(struct inode *sufile, __u64 segnum,
 600			       struct buffer_head *header_bh,
 601			       struct buffer_head *su_bh)
 602{
 603	struct nilfs_segment_usage *su;
 604	void *kaddr;
 605	int suclean;
 606
 607	kaddr = kmap_atomic(su_bh->b_page);
 608	su = nilfs_sufile_block_get_segment_usage(sufile, segnum, su_bh, kaddr);
 609	if (nilfs_segment_usage_error(su)) {
 610		kunmap_atomic(kaddr);
 611		return;
 612	}
 613	suclean = nilfs_segment_usage_clean(su);
 614	nilfs_segment_usage_set_error(su);
 615	kunmap_atomic(kaddr);
 616
 617	if (suclean) {
 618		nilfs_sufile_mod_counter(header_bh, -1, 0);
 619		NILFS_SUI(sufile)->ncleansegs--;
 620	}
 621	mark_buffer_dirty(su_bh);
 622	nilfs_mdt_mark_dirty(sufile);
 623}
 624
 625/**
 626  * nilfs_sufile_truncate_range - truncate range of segment array
 627  * @sufile: inode of segment usage file
 628  * @start: start segment number (inclusive)
 629  * @end: end segment number (inclusive)
 630  *
 631  * Return Value: On success, 0 is returned.  On error, one of the
 632  * following negative error codes is returned.
 633  *
 634  * %-EIO - I/O error.
 635  *
 636  * %-ENOMEM - Insufficient amount of memory available.
 637  *
 638  * %-EINVAL - Invalid number of segments specified
 639  *
 640  * %-EBUSY - Dirty or active segments are present in the range
 641  */
 642static int nilfs_sufile_truncate_range(struct inode *sufile,
 643				       __u64 start, __u64 end)
 644{
 645	struct the_nilfs *nilfs = sufile->i_sb->s_fs_info;
 646	struct buffer_head *header_bh;
 647	struct buffer_head *su_bh;
 648	struct nilfs_segment_usage *su, *su2;
 649	size_t susz = NILFS_MDT(sufile)->mi_entry_size;
 650	unsigned long segusages_per_block;
 651	unsigned long nsegs, ncleaned;
 652	__u64 segnum;
 653	void *kaddr;
 654	ssize_t n, nc;
 655	int ret;
 656	int j;
 657
 658	nsegs = nilfs_sufile_get_nsegments(sufile);
 659
 660	ret = -EINVAL;
 661	if (start > end || start >= nsegs)
 662		goto out;
 663
 664	ret = nilfs_sufile_get_header_block(sufile, &header_bh);
 665	if (ret < 0)
 666		goto out;
 667
 668	segusages_per_block = nilfs_sufile_segment_usages_per_block(sufile);
 669	ncleaned = 0;
 670
 671	for (segnum = start; segnum <= end; segnum += n) {
 672		n = min_t(unsigned long,
 673			  segusages_per_block -
 674				  nilfs_sufile_get_offset(sufile, segnum),
 675			  end - segnum + 1);
 676		ret = nilfs_sufile_get_segment_usage_block(sufile, segnum, 0,
 677							   &su_bh);
 678		if (ret < 0) {
 679			if (ret != -ENOENT)
 680				goto out_header;
 681			/* hole */
 682			continue;
 683		}
 684		kaddr = kmap_atomic(su_bh->b_page);
 685		su = nilfs_sufile_block_get_segment_usage(
 686			sufile, segnum, su_bh, kaddr);
 687		su2 = su;
 688		for (j = 0; j < n; j++, su = (void *)su + susz) {
 689			if ((le32_to_cpu(su->su_flags) &
 690			     ~(1UL << NILFS_SEGMENT_USAGE_ERROR)) ||
 691			    nilfs_segment_is_active(nilfs, segnum + j)) {
 692				ret = -EBUSY;
 693				kunmap_atomic(kaddr);
 694				brelse(su_bh);
 695				goto out_header;
 696			}
 697		}
 698		nc = 0;
 699		for (su = su2, j = 0; j < n; j++, su = (void *)su + susz) {
 700			if (nilfs_segment_usage_error(su)) {
 701				nilfs_segment_usage_set_clean(su);
 702				nc++;
 703			}
 704		}
 705		kunmap_atomic(kaddr);
 706		if (nc > 0) {
 707			mark_buffer_dirty(su_bh);
 708			ncleaned += nc;
 709		}
 710		brelse(su_bh);
 711
 712		if (n == segusages_per_block) {
 713			/* make hole */
 714			nilfs_sufile_delete_segment_usage_block(sufile, segnum);
 715		}
 716	}
 717	ret = 0;
 718
 719out_header:
 720	if (ncleaned > 0) {
 721		NILFS_SUI(sufile)->ncleansegs += ncleaned;
 722		nilfs_sufile_mod_counter(header_bh, ncleaned, 0);
 723		nilfs_mdt_mark_dirty(sufile);
 724	}
 725	brelse(header_bh);
 726out:
 727	return ret;
 728}
 729
 730/**
 731 * nilfs_sufile_resize - resize segment array
 732 * @sufile: inode of segment usage file
 733 * @newnsegs: new number of segments
 734 *
 735 * Return Value: On success, 0 is returned.  On error, one of the
 736 * following negative error codes is returned.
 737 *
 738 * %-EIO - I/O error.
 739 *
 740 * %-ENOMEM - Insufficient amount of memory available.
 741 *
 742 * %-ENOSPC - Enough free space is not left for shrinking
 743 *
 744 * %-EBUSY - Dirty or active segments exist in the region to be truncated
 745 */
 746int nilfs_sufile_resize(struct inode *sufile, __u64 newnsegs)
 747{
 748	struct the_nilfs *nilfs = sufile->i_sb->s_fs_info;
 749	struct buffer_head *header_bh;
 750	struct nilfs_sufile_header *header;
 751	struct nilfs_sufile_info *sui = NILFS_SUI(sufile);
 752	void *kaddr;
 753	unsigned long nsegs, nrsvsegs;
 754	int ret = 0;
 755
 756	down_write(&NILFS_MDT(sufile)->mi_sem);
 757
 758	nsegs = nilfs_sufile_get_nsegments(sufile);
 759	if (nsegs == newnsegs)
 760		goto out;
 761
 762	ret = -ENOSPC;
 763	nrsvsegs = nilfs_nrsvsegs(nilfs, newnsegs);
 764	if (newnsegs < nsegs && nsegs - newnsegs + nrsvsegs > sui->ncleansegs)
 765		goto out;
 766
 767	ret = nilfs_sufile_get_header_block(sufile, &header_bh);
 768	if (ret < 0)
 769		goto out;
 770
 771	if (newnsegs > nsegs) {
 772		sui->ncleansegs += newnsegs - nsegs;
 773	} else /* newnsegs < nsegs */ {
 774		ret = nilfs_sufile_truncate_range(sufile, newnsegs, nsegs - 1);
 775		if (ret < 0)
 776			goto out_header;
 777
 778		sui->ncleansegs -= nsegs - newnsegs;
 779	}
 780
 781	kaddr = kmap_atomic(header_bh->b_page);
 782	header = kaddr + bh_offset(header_bh);
 783	header->sh_ncleansegs = cpu_to_le64(sui->ncleansegs);
 784	kunmap_atomic(kaddr);
 785
 786	mark_buffer_dirty(header_bh);
 787	nilfs_mdt_mark_dirty(sufile);
 788	nilfs_set_nsegments(nilfs, newnsegs);
 789
 790out_header:
 791	brelse(header_bh);
 792out:
 793	up_write(&NILFS_MDT(sufile)->mi_sem);
 794	return ret;
 795}
 796
 797/**
 798 * nilfs_sufile_get_suinfo -
 799 * @sufile: inode of segment usage file
 800 * @segnum: segment number to start looking
 801 * @buf: array of suinfo
 802 * @sisz: byte size of suinfo
 803 * @nsi: size of suinfo array
 804 *
 805 * Description:
 806 *
 807 * Return Value: On success, 0 is returned and .... On error, one of the
 808 * following negative error codes is returned.
 809 *
 810 * %-EIO - I/O error.
 811 *
 812 * %-ENOMEM - Insufficient amount of memory available.
 813 */
 814ssize_t nilfs_sufile_get_suinfo(struct inode *sufile, __u64 segnum, void *buf,
 815				unsigned sisz, size_t nsi)
 816{
 817	struct buffer_head *su_bh;
 818	struct nilfs_segment_usage *su;
 819	struct nilfs_suinfo *si = buf;
 820	size_t susz = NILFS_MDT(sufile)->mi_entry_size;
 821	struct the_nilfs *nilfs = sufile->i_sb->s_fs_info;
 822	void *kaddr;
 823	unsigned long nsegs, segusages_per_block;
 824	ssize_t n;
 825	int ret, i, j;
 826
 827	down_read(&NILFS_MDT(sufile)->mi_sem);
 828
 829	segusages_per_block = nilfs_sufile_segment_usages_per_block(sufile);
 830	nsegs = min_t(unsigned long,
 831		      nilfs_sufile_get_nsegments(sufile) - segnum,
 832		      nsi);
 833	for (i = 0; i < nsegs; i += n, segnum += n) {
 834		n = min_t(unsigned long,
 835			  segusages_per_block -
 836				  nilfs_sufile_get_offset(sufile, segnum),
 837			  nsegs - i);
 838		ret = nilfs_sufile_get_segment_usage_block(sufile, segnum, 0,
 839							   &su_bh);
 840		if (ret < 0) {
 841			if (ret != -ENOENT)
 842				goto out;
 843			/* hole */
 844			memset(si, 0, sisz * n);
 845			si = (void *)si + sisz * n;
 846			continue;
 847		}
 848
 849		kaddr = kmap_atomic(su_bh->b_page);
 850		su = nilfs_sufile_block_get_segment_usage(
 851			sufile, segnum, su_bh, kaddr);
 852		for (j = 0; j < n;
 853		     j++, su = (void *)su + susz, si = (void *)si + sisz) {
 854			si->sui_lastmod = le64_to_cpu(su->su_lastmod);
 855			si->sui_nblocks = le32_to_cpu(su->su_nblocks);
 856			si->sui_flags = le32_to_cpu(su->su_flags) &
 857				~(1UL << NILFS_SEGMENT_USAGE_ACTIVE);
 858			if (nilfs_segment_is_active(nilfs, segnum + j))
 859				si->sui_flags |=
 860					(1UL << NILFS_SEGMENT_USAGE_ACTIVE);
 861		}
 862		kunmap_atomic(kaddr);
 863		brelse(su_bh);
 864	}
 865	ret = nsegs;
 866
 867 out:
 868	up_read(&NILFS_MDT(sufile)->mi_sem);
 869	return ret;
 870}
 871
 872/**
 873 * nilfs_sufile_set_suinfo - sets segment usage info
 874 * @sufile: inode of segment usage file
 875 * @buf: array of suinfo_update
 876 * @supsz: byte size of suinfo_update
 877 * @nsup: size of suinfo_update array
 878 *
 879 * Description: Takes an array of nilfs_suinfo_update structs and updates
 880 * segment usage accordingly. Only the fields indicated by the sup_flags
 881 * are updated.
 882 *
 883 * Return Value: On success, 0 is returned. On error, one of the
 884 * following negative error codes is returned.
 885 *
 886 * %-EIO - I/O error.
 887 *
 888 * %-ENOMEM - Insufficient amount of memory available.
 889 *
 890 * %-EINVAL - Invalid values in input (segment number, flags or nblocks)
 891 */
 892ssize_t nilfs_sufile_set_suinfo(struct inode *sufile, void *buf,
 893				unsigned supsz, size_t nsup)
 894{
 895	struct the_nilfs *nilfs = sufile->i_sb->s_fs_info;
 896	struct buffer_head *header_bh, *bh;
 897	struct nilfs_suinfo_update *sup, *supend = buf + supsz * nsup;
 898	struct nilfs_segment_usage *su;
 899	void *kaddr;
 900	unsigned long blkoff, prev_blkoff;
 901	int cleansi, cleansu, dirtysi, dirtysu;
 902	long ncleaned = 0, ndirtied = 0;
 903	int ret = 0;
 904
 905	if (unlikely(nsup == 0))
 906		return ret;
 907
 908	for (sup = buf; sup < supend; sup = (void *)sup + supsz) {
 909		if (sup->sup_segnum >= nilfs->ns_nsegments
 910			|| (sup->sup_flags &
 911				(~0UL << __NR_NILFS_SUINFO_UPDATE_FIELDS))
 912			|| (nilfs_suinfo_update_nblocks(sup) &&
 913				sup->sup_sui.sui_nblocks >
 914				nilfs->ns_blocks_per_segment))
 915			return -EINVAL;
 916	}
 917
 918	down_write(&NILFS_MDT(sufile)->mi_sem);
 919
 920	ret = nilfs_sufile_get_header_block(sufile, &header_bh);
 921	if (ret < 0)
 922		goto out_sem;
 923
 924	sup = buf;
 925	blkoff = nilfs_sufile_get_blkoff(sufile, sup->sup_segnum);
 926	ret = nilfs_mdt_get_block(sufile, blkoff, 1, NULL, &bh);
 927	if (ret < 0)
 928		goto out_header;
 929
 930	for (;;) {
 931		kaddr = kmap_atomic(bh->b_page);
 932		su = nilfs_sufile_block_get_segment_usage(
 933			sufile, sup->sup_segnum, bh, kaddr);
 934
 935		if (nilfs_suinfo_update_lastmod(sup))
 936			su->su_lastmod = cpu_to_le64(sup->sup_sui.sui_lastmod);
 937
 938		if (nilfs_suinfo_update_nblocks(sup))
 939			su->su_nblocks = cpu_to_le32(sup->sup_sui.sui_nblocks);
 940
 941		if (nilfs_suinfo_update_flags(sup)) {
 942			/*
 943			 * Active flag is a virtual flag projected by running
 944			 * nilfs kernel code - drop it not to write it to
 945			 * disk.
 946			 */
 947			sup->sup_sui.sui_flags &=
 948					~(1UL << NILFS_SEGMENT_USAGE_ACTIVE);
 949
 950			cleansi = nilfs_suinfo_clean(&sup->sup_sui);
 951			cleansu = nilfs_segment_usage_clean(su);
 952			dirtysi = nilfs_suinfo_dirty(&sup->sup_sui);
 953			dirtysu = nilfs_segment_usage_dirty(su);
 954
 955			if (cleansi && !cleansu)
 956				++ncleaned;
 957			else if (!cleansi && cleansu)
 958				--ncleaned;
 959
 960			if (dirtysi && !dirtysu)
 961				++ndirtied;
 962			else if (!dirtysi && dirtysu)
 963				--ndirtied;
 964
 965			su->su_flags = cpu_to_le32(sup->sup_sui.sui_flags);
 966		}
 967
 968		kunmap_atomic(kaddr);
 969
 970		sup = (void *)sup + supsz;
 971		if (sup >= supend)
 972			break;
 973
 974		prev_blkoff = blkoff;
 975		blkoff = nilfs_sufile_get_blkoff(sufile, sup->sup_segnum);
 976		if (blkoff == prev_blkoff)
 977			continue;
 978
 979		/* get different block */
 980		mark_buffer_dirty(bh);
 981		put_bh(bh);
 982		ret = nilfs_mdt_get_block(sufile, blkoff, 1, NULL, &bh);
 983		if (unlikely(ret < 0))
 984			goto out_mark;
 985	}
 986	mark_buffer_dirty(bh);
 987	put_bh(bh);
 988
 989 out_mark:
 990	if (ncleaned || ndirtied) {
 991		nilfs_sufile_mod_counter(header_bh, (u64)ncleaned,
 992				(u64)ndirtied);
 993		NILFS_SUI(sufile)->ncleansegs += ncleaned;
 994	}
 995	nilfs_mdt_mark_dirty(sufile);
 996 out_header:
 997	put_bh(header_bh);
 998 out_sem:
 999	up_write(&NILFS_MDT(sufile)->mi_sem);
1000	return ret;
1001}
1002
1003/**
1004 * nilfs_sufile_trim_fs() - trim ioctl handle function
1005 * @sufile: inode of segment usage file
1006 * @range: fstrim_range structure
1007 *
1008 * start:	First Byte to trim
1009 * len:		number of Bytes to trim from start
1010 * minlen:	minimum extent length in Bytes
1011 *
1012 * Decription: nilfs_sufile_trim_fs goes through all segments containing bytes
1013 * from start to start+len. start is rounded up to the next block boundary
1014 * and start+len is rounded down. For each clean segment blkdev_issue_discard
1015 * function is invoked.
1016 *
1017 * Return Value: On success, 0 is returned or negative error code, otherwise.
1018 */
1019int nilfs_sufile_trim_fs(struct inode *sufile, struct fstrim_range *range)
1020{
1021	struct the_nilfs *nilfs = sufile->i_sb->s_fs_info;
1022	struct buffer_head *su_bh;
1023	struct nilfs_segment_usage *su;
1024	void *kaddr;
1025	size_t n, i, susz = NILFS_MDT(sufile)->mi_entry_size;
1026	sector_t seg_start, seg_end, start_block, end_block;
1027	sector_t start = 0, nblocks = 0;
1028	u64 segnum, segnum_end, minlen, len, max_blocks, ndiscarded = 0;
1029	int ret = 0;
1030	unsigned int sects_per_block;
1031
1032	sects_per_block = (1 << nilfs->ns_blocksize_bits) /
1033			bdev_logical_block_size(nilfs->ns_bdev);
1034	len = range->len >> nilfs->ns_blocksize_bits;
1035	minlen = range->minlen >> nilfs->ns_blocksize_bits;
1036	max_blocks = ((u64)nilfs->ns_nsegments * nilfs->ns_blocks_per_segment);
1037
1038	if (!len || range->start >= max_blocks << nilfs->ns_blocksize_bits)
1039		return -EINVAL;
1040
1041	start_block = (range->start + nilfs->ns_blocksize - 1) >>
1042			nilfs->ns_blocksize_bits;
1043
1044	/*
1045	 * range->len can be very large (actually, it is set to
1046	 * ULLONG_MAX by default) - truncate upper end of the range
1047	 * carefully so as not to overflow.
1048	 */
1049	if (max_blocks - start_block < len)
1050		end_block = max_blocks - 1;
1051	else
1052		end_block = start_block + len - 1;
1053
1054	segnum = nilfs_get_segnum_of_block(nilfs, start_block);
1055	segnum_end = nilfs_get_segnum_of_block(nilfs, end_block);
1056
1057	down_read(&NILFS_MDT(sufile)->mi_sem);
1058
1059	while (segnum <= segnum_end) {
1060		n = nilfs_sufile_segment_usages_in_block(sufile, segnum,
1061				segnum_end);
1062
1063		ret = nilfs_sufile_get_segment_usage_block(sufile, segnum, 0,
1064							   &su_bh);
1065		if (ret < 0) {
1066			if (ret != -ENOENT)
1067				goto out_sem;
1068			/* hole */
1069			segnum += n;
1070			continue;
1071		}
1072
1073		kaddr = kmap_atomic(su_bh->b_page);
1074		su = nilfs_sufile_block_get_segment_usage(sufile, segnum,
1075				su_bh, kaddr);
1076		for (i = 0; i < n; ++i, ++segnum, su = (void *)su + susz) {
1077			if (!nilfs_segment_usage_clean(su))
1078				continue;
1079
1080			nilfs_get_segment_range(nilfs, segnum, &seg_start,
1081						&seg_end);
1082
1083			if (!nblocks) {
1084				/* start new extent */
1085				start = seg_start;
1086				nblocks = seg_end - seg_start + 1;
1087				continue;
1088			}
1089
1090			if (start + nblocks == seg_start) {
1091				/* add to previous extent */
1092				nblocks += seg_end - seg_start + 1;
1093				continue;
1094			}
1095
1096			/* discard previous extent */
1097			if (start < start_block) {
1098				nblocks -= start_block - start;
1099				start = start_block;
1100			}
1101
1102			if (nblocks >= minlen) {
1103				kunmap_atomic(kaddr);
1104
1105				ret = blkdev_issue_discard(nilfs->ns_bdev,
1106						start * sects_per_block,
1107						nblocks * sects_per_block,
1108						GFP_NOFS, 0);
1109				if (ret < 0) {
1110					put_bh(su_bh);
1111					goto out_sem;
1112				}
1113
1114				ndiscarded += nblocks;
1115				kaddr = kmap_atomic(su_bh->b_page);
1116				su = nilfs_sufile_block_get_segment_usage(
1117					sufile, segnum, su_bh, kaddr);
1118			}
1119
1120			/* start new extent */
1121			start = seg_start;
1122			nblocks = seg_end - seg_start + 1;
1123		}
1124		kunmap_atomic(kaddr);
1125		put_bh(su_bh);
1126	}
1127
1128
1129	if (nblocks) {
1130		/* discard last extent */
1131		if (start < start_block) {
1132			nblocks -= start_block - start;
1133			start = start_block;
1134		}
1135		if (start + nblocks > end_block + 1)
1136			nblocks = end_block - start + 1;
1137
1138		if (nblocks >= minlen) {
1139			ret = blkdev_issue_discard(nilfs->ns_bdev,
1140					start * sects_per_block,
1141					nblocks * sects_per_block,
1142					GFP_NOFS, 0);
1143			if (!ret)
1144				ndiscarded += nblocks;
1145		}
1146	}
1147
1148out_sem:
1149	up_read(&NILFS_MDT(sufile)->mi_sem);
1150
1151	range->len = ndiscarded << nilfs->ns_blocksize_bits;
1152	return ret;
1153}
1154
1155/**
1156 * nilfs_sufile_read - read or get sufile inode
1157 * @sb: super block instance
1158 * @susize: size of a segment usage entry
1159 * @raw_inode: on-disk sufile inode
1160 * @inodep: buffer to store the inode
1161 */
1162int nilfs_sufile_read(struct super_block *sb, size_t susize,
1163		      struct nilfs_inode *raw_inode, struct inode **inodep)
1164{
1165	struct inode *sufile;
1166	struct nilfs_sufile_info *sui;
1167	struct buffer_head *header_bh;
1168	struct nilfs_sufile_header *header;
1169	void *kaddr;
1170	int err;
1171
1172	if (susize > sb->s_blocksize) {
1173		printk(KERN_ERR
1174		       "NILFS: too large segment usage size: %zu bytes.\n",
1175		       susize);
1176		return -EINVAL;
1177	} else if (susize < NILFS_MIN_SEGMENT_USAGE_SIZE) {
1178		printk(KERN_ERR
1179		       "NILFS: too small segment usage size: %zu bytes.\n",
1180		       susize);
1181		return -EINVAL;
1182	}
1183
1184	sufile = nilfs_iget_locked(sb, NULL, NILFS_SUFILE_INO);
1185	if (unlikely(!sufile))
1186		return -ENOMEM;
1187	if (!(sufile->i_state & I_NEW))
1188		goto out;
1189
1190	err = nilfs_mdt_init(sufile, NILFS_MDT_GFP, sizeof(*sui));
1191	if (err)
1192		goto failed;
1193
1194	nilfs_mdt_set_entry_size(sufile, susize,
1195				 sizeof(struct nilfs_sufile_header));
1196
1197	err = nilfs_read_inode_common(sufile, raw_inode);
1198	if (err)
1199		goto failed;
1200
1201	err = nilfs_sufile_get_header_block(sufile, &header_bh);
1202	if (err)
1203		goto failed;
1204
1205	sui = NILFS_SUI(sufile);
1206	kaddr = kmap_atomic(header_bh->b_page);
1207	header = kaddr + bh_offset(header_bh);
1208	sui->ncleansegs = le64_to_cpu(header->sh_ncleansegs);
1209	kunmap_atomic(kaddr);
1210	brelse(header_bh);
1211
1212	sui->allocmax = nilfs_sufile_get_nsegments(sufile) - 1;
1213	sui->allocmin = 0;
1214
1215	unlock_new_inode(sufile);
1216 out:
1217	*inodep = sufile;
1218	return 0;
1219 failed:
1220	iget_failed(sufile);
1221	return err;
1222}