ioctl.c 17.0 KB
Newer Older
K
Koji Sato 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * ioctl.c - NILFS ioctl operations.
 *
 * Copyright (C) 2007, 2008 Nippon Telegraph and Telephone Corporation.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Written by Koji Sato <koji@osrg.net>.
 */

#include <linux/fs.h>
#include <linux/wait.h>
25
#include <linux/slab.h>
K
Koji Sato 已提交
26 27
#include <linux/capability.h>	/* capable() */
#include <linux/uaccess.h>	/* copy_from_user(), copy_to_user() */
28
#include <linux/vmalloc.h>
29
#include <linux/mount.h>	/* mnt_want_write(), mnt_drop_write() */
K
Koji Sato 已提交
30 31 32 33 34 35 36 37 38 39 40 41
#include <linux/nilfs2_fs.h>
#include "nilfs.h"
#include "segment.h"
#include "bmap.h"
#include "cpfile.h"
#include "sufile.h"
#include "dat.h"


static int nilfs_ioctl_wrap_copy(struct the_nilfs *nilfs,
				 struct nilfs_argv *argv, int dir,
				 ssize_t (*dofunc)(struct the_nilfs *,
42
						   __u64 *, int,
K
Koji Sato 已提交
43 44 45
						   void *, size_t, size_t))
{
	void *buf;
46
	void __user *base = (void __user *)(unsigned long)argv->v_base;
47
	size_t maxmembs, total, n;
K
Koji Sato 已提交
48 49
	ssize_t nr;
	int ret, i;
50
	__u64 pos, ppos;
K
Koji Sato 已提交
51 52 53 54

	if (argv->v_nmembs == 0)
		return 0;

55 56 57 58 59
	if (argv->v_size > PAGE_SIZE)
		return -EINVAL;

	buf = (void *)__get_free_pages(GFP_NOFS, 0);
	if (unlikely(!buf))
K
Koji Sato 已提交
60
		return -ENOMEM;
61
	maxmembs = PAGE_SIZE / argv->v_size;
K
Koji Sato 已提交
62 63 64

	ret = 0;
	total = 0;
65
	pos = argv->v_index;
K
Koji Sato 已提交
66 67 68 69
	for (i = 0; i < argv->v_nmembs; i += n) {
		n = (argv->v_nmembs - i < maxmembs) ?
			argv->v_nmembs - i : maxmembs;
		if ((dir & _IOC_WRITE) &&
70 71
		    copy_from_user(buf, base + argv->v_size * i,
				   argv->v_size * n)) {
K
Koji Sato 已提交
72 73 74
			ret = -EFAULT;
			break;
		}
75
		ppos = pos;
76
		nr = dofunc(nilfs, &pos, argv->v_flags, buf, argv->v_size,
77
			       n);
K
Koji Sato 已提交
78 79 80 81 82
		if (nr < 0) {
			ret = nr;
			break;
		}
		if ((dir & _IOC_READ) &&
83 84
		    copy_to_user(base + argv->v_size * i, buf,
				 argv->v_size * nr)) {
K
Koji Sato 已提交
85 86 87 88
			ret = -EFAULT;
			break;
		}
		total += nr;
89 90 91 92
		if ((size_t)nr < n)
			break;
		if (pos == ppos)
			pos += n;
K
Koji Sato 已提交
93 94 95
	}
	argv->v_nmembs = total;

96
	free_pages((unsigned long)buf, 0);
K
Koji Sato 已提交
97 98 99 100 101 102
	return ret;
}

static int nilfs_ioctl_change_cpmode(struct inode *inode, struct file *filp,
				     unsigned int cmd, void __user *argp)
{
103 104
	struct the_nilfs *nilfs = NILFS_SB(inode->i_sb)->s_nilfs;
	struct inode *cpfile = nilfs->ns_cpfile;
K
Koji Sato 已提交
105 106 107 108 109 110
	struct nilfs_transaction_info ti;
	struct nilfs_cpmode cpmode;
	int ret;

	if (!capable(CAP_SYS_ADMIN))
		return -EPERM;
111 112 113 114 115 116

	ret = mnt_want_write(filp->f_path.mnt);
	if (ret)
		return ret;

	ret = -EFAULT;
K
Koji Sato 已提交
117
	if (copy_from_user(&cpmode, argp, sizeof(cpmode)))
118
		goto out;
K
Koji Sato 已提交
119

120
	mutex_lock(&nilfs->ns_mount_mutex);
121

K
Koji Sato 已提交
122 123 124
	nilfs_transaction_begin(inode->i_sb, &ti, 0);
	ret = nilfs_cpfile_change_cpmode(
		cpfile, cpmode.cm_cno, cpmode.cm_mode);
125
	if (unlikely(ret < 0))
126
		nilfs_transaction_abort(inode->i_sb);
127 128 129
	else
		nilfs_transaction_commit(inode->i_sb); /* never fails */

130
	mutex_unlock(&nilfs->ns_mount_mutex);
131 132
out:
	mnt_drop_write(filp->f_path.mnt);
K
Koji Sato 已提交
133 134 135 136 137 138 139 140 141 142 143 144 145 146
	return ret;
}

static int
nilfs_ioctl_delete_checkpoint(struct inode *inode, struct file *filp,
			      unsigned int cmd, void __user *argp)
{
	struct inode *cpfile = NILFS_SB(inode->i_sb)->s_nilfs->ns_cpfile;
	struct nilfs_transaction_info ti;
	__u64 cno;
	int ret;

	if (!capable(CAP_SYS_ADMIN))
		return -EPERM;
147 148 149 150 151 152

	ret = mnt_want_write(filp->f_path.mnt);
	if (ret)
		return ret;

	ret = -EFAULT;
K
Koji Sato 已提交
153
	if (copy_from_user(&cno, argp, sizeof(cno)))
154
		goto out;
K
Koji Sato 已提交
155 156 157

	nilfs_transaction_begin(inode->i_sb, &ti, 0);
	ret = nilfs_cpfile_delete_checkpoint(cpfile, cno);
158
	if (unlikely(ret < 0))
159
		nilfs_transaction_abort(inode->i_sb);
160 161 162 163
	else
		nilfs_transaction_commit(inode->i_sb); /* never fails */
out:
	mnt_drop_write(filp->f_path.mnt);
K
Koji Sato 已提交
164 165 166 167
	return ret;
}

static ssize_t
168
nilfs_ioctl_do_get_cpinfo(struct the_nilfs *nilfs, __u64 *posp, int flags,
K
Koji Sato 已提交
169 170 171 172
			  void *buf, size_t size, size_t nmembs)
{
	int ret;

173
	down_read(&nilfs->ns_segctor_sem);
174
	ret = nilfs_cpfile_get_cpinfo(nilfs->ns_cpfile, posp, flags, buf,
175
				      size, nmembs);
176
	up_read(&nilfs->ns_segctor_sem);
K
Koji Sato 已提交
177 178 179 180 181 182
	return ret;
}

static int nilfs_ioctl_get_cpstat(struct inode *inode, struct file *filp,
				  unsigned int cmd, void __user *argp)
{
183
	struct the_nilfs *nilfs = NILFS_SB(inode->i_sb)->s_nilfs;
K
Koji Sato 已提交
184 185 186
	struct nilfs_cpstat cpstat;
	int ret;

187 188 189
	down_read(&nilfs->ns_segctor_sem);
	ret = nilfs_cpfile_get_stat(nilfs->ns_cpfile, &cpstat);
	up_read(&nilfs->ns_segctor_sem);
K
Koji Sato 已提交
190 191 192 193 194 195 196 197 198
	if (ret < 0)
		return ret;

	if (copy_to_user(argp, &cpstat, sizeof(cpstat)))
		ret = -EFAULT;
	return ret;
}

static ssize_t
199
nilfs_ioctl_do_get_suinfo(struct the_nilfs *nilfs, __u64 *posp, int flags,
K
Koji Sato 已提交
200 201 202 203
			  void *buf, size_t size, size_t nmembs)
{
	int ret;

204
	down_read(&nilfs->ns_segctor_sem);
205 206
	ret = nilfs_sufile_get_suinfo(nilfs->ns_sufile, *posp, buf, size,
				      nmembs);
207
	up_read(&nilfs->ns_segctor_sem);
K
Koji Sato 已提交
208 209 210 211 212 213
	return ret;
}

static int nilfs_ioctl_get_sustat(struct inode *inode, struct file *filp,
				  unsigned int cmd, void __user *argp)
{
214
	struct the_nilfs *nilfs = NILFS_SB(inode->i_sb)->s_nilfs;
K
Koji Sato 已提交
215 216 217
	struct nilfs_sustat sustat;
	int ret;

218 219 220
	down_read(&nilfs->ns_segctor_sem);
	ret = nilfs_sufile_get_stat(nilfs->ns_sufile, &sustat);
	up_read(&nilfs->ns_segctor_sem);
K
Koji Sato 已提交
221 222 223 224 225 226 227 228 229
	if (ret < 0)
		return ret;

	if (copy_to_user(argp, &sustat, sizeof(sustat)))
		ret = -EFAULT;
	return ret;
}

static ssize_t
230
nilfs_ioctl_do_get_vinfo(struct the_nilfs *nilfs, __u64 *posp, int flags,
K
Koji Sato 已提交
231 232 233 234
			 void *buf, size_t size, size_t nmembs)
{
	int ret;

235
	down_read(&nilfs->ns_segctor_sem);
236
	ret = nilfs_dat_get_vinfo(nilfs_dat_inode(nilfs), buf, size, nmembs);
237
	up_read(&nilfs->ns_segctor_sem);
K
Koji Sato 已提交
238 239 240 241
	return ret;
}

static ssize_t
242
nilfs_ioctl_do_get_bdescs(struct the_nilfs *nilfs, __u64 *posp, int flags,
K
Koji Sato 已提交
243 244 245 246 247 248 249
			  void *buf, size_t size, size_t nmembs)
{
	struct inode *dat = nilfs_dat_inode(nilfs);
	struct nilfs_bmap *bmap = NILFS_I(dat)->i_bmap;
	struct nilfs_bdesc *bdescs = buf;
	int ret, i;

250
	down_read(&nilfs->ns_segctor_sem);
K
Koji Sato 已提交
251 252 253 254 255 256
	for (i = 0; i < nmembs; i++) {
		ret = nilfs_bmap_lookup_at_level(bmap,
						 bdescs[i].bd_offset,
						 bdescs[i].bd_level + 1,
						 &bdescs[i].bd_blocknr);
		if (ret < 0) {
257 258
			if (ret != -ENOENT) {
				up_read(&nilfs->ns_segctor_sem);
K
Koji Sato 已提交
259
				return ret;
260
			}
K
Koji Sato 已提交
261 262 263
			bdescs[i].bd_blocknr = 0;
		}
	}
264
	up_read(&nilfs->ns_segctor_sem);
K
Koji Sato 已提交
265 266 267 268 269 270 271 272 273 274 275 276 277
	return nmembs;
}

static int nilfs_ioctl_get_bdescs(struct inode *inode, struct file *filp,
				  unsigned int cmd, void __user *argp)
{
	struct the_nilfs *nilfs = NILFS_SB(inode->i_sb)->s_nilfs;
	struct nilfs_argv argv;
	int ret;

	if (copy_from_user(&argv, argp, sizeof(argv)))
		return -EFAULT;

278 279 280
	if (argv.v_size != sizeof(struct nilfs_bdesc))
		return -EINVAL;

K
Koji Sato 已提交
281 282
	ret = nilfs_ioctl_wrap_copy(nilfs, &argv, _IOC_DIR(cmd),
				    nilfs_ioctl_do_get_bdescs);
283 284
	if (ret < 0)
		return ret;
K
Koji Sato 已提交
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319

	if (copy_to_user(argp, &argv, sizeof(argv)))
		ret = -EFAULT;
	return ret;
}

static int nilfs_ioctl_move_inode_block(struct inode *inode,
					struct nilfs_vdesc *vdesc,
					struct list_head *buffers)
{
	struct buffer_head *bh;
	int ret;

	if (vdesc->vd_flags == 0)
		ret = nilfs_gccache_submit_read_data(
			inode, vdesc->vd_offset, vdesc->vd_blocknr,
			vdesc->vd_vblocknr, &bh);
	else
		ret = nilfs_gccache_submit_read_node(
			inode, vdesc->vd_blocknr, vdesc->vd_vblocknr, &bh);

	if (unlikely(ret < 0)) {
		if (ret == -ENOENT)
			printk(KERN_CRIT
			       "%s: invalid virtual block address (%s): "
			       "ino=%llu, cno=%llu, offset=%llu, "
			       "blocknr=%llu, vblocknr=%llu\n",
			       __func__, vdesc->vd_flags ? "node" : "data",
			       (unsigned long long)vdesc->vd_ino,
			       (unsigned long long)vdesc->vd_cno,
			       (unsigned long long)vdesc->vd_offset,
			       (unsigned long long)vdesc->vd_blocknr,
			       (unsigned long long)vdesc->vd_vblocknr);
		return ret;
	}
320 321 322 323 324 325 326 327 328 329 330 331
	if (unlikely(!list_empty(&bh->b_assoc_buffers))) {
		printk(KERN_CRIT "%s: conflicting %s buffer: ino=%llu, "
		       "cno=%llu, offset=%llu, blocknr=%llu, vblocknr=%llu\n",
		       __func__, vdesc->vd_flags ? "node" : "data",
		       (unsigned long long)vdesc->vd_ino,
		       (unsigned long long)vdesc->vd_cno,
		       (unsigned long long)vdesc->vd_offset,
		       (unsigned long long)vdesc->vd_blocknr,
		       (unsigned long long)vdesc->vd_vblocknr);
		brelse(bh);
		return -EEXIST;
	}
K
Koji Sato 已提交
332 333 334 335
	list_add_tail(&bh->b_assoc_buffers, buffers);
	return 0;
}

336
static int nilfs_ioctl_move_blocks(struct super_block *sb,
337
				   struct nilfs_argv *argv, void *buf)
K
Koji Sato 已提交
338
{
339
	size_t nmembs = argv->v_nmembs;
K
Koji Sato 已提交
340 341 342 343 344 345 346 347 348 349 350
	struct inode *inode;
	struct nilfs_vdesc *vdesc;
	struct buffer_head *bh, *n;
	LIST_HEAD(buffers);
	ino_t ino;
	__u64 cno;
	int i, ret;

	for (i = 0, vdesc = buf; i < nmembs; ) {
		ino = vdesc->vd_ino;
		cno = vdesc->vd_cno;
351
		inode = nilfs_iget_for_gc(sb, ino, cno);
K
Koji Sato 已提交
352 353 354 355 356 357 358
		if (unlikely(inode == NULL)) {
			ret = -ENOMEM;
			goto failed;
		}
		do {
			ret = nilfs_ioctl_move_inode_block(inode, vdesc,
							   &buffers);
359 360
			if (unlikely(ret < 0)) {
				iput(inode);
K
Koji Sato 已提交
361
				goto failed;
362
			}
K
Koji Sato 已提交
363 364 365
			vdesc++;
		} while (++i < nmembs &&
			 vdesc->vd_ino == ino && vdesc->vd_cno == cno);
366 367

		iput(inode); /* The inode still remains in GC inode list */
K
Koji Sato 已提交
368 369 370 371 372
	}

	list_for_each_entry_safe(bh, n, &buffers, b_assoc_buffers) {
		ret = nilfs_gccache_wait_and_mark_dirty(bh);
		if (unlikely(ret < 0)) {
373
			WARN_ON(ret == -EEXIST);
K
Koji Sato 已提交
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
			goto failed;
		}
		list_del_init(&bh->b_assoc_buffers);
		brelse(bh);
	}
	return nmembs;

 failed:
	list_for_each_entry_safe(bh, n, &buffers, b_assoc_buffers) {
		list_del_init(&bh->b_assoc_buffers);
		brelse(bh);
	}
	return ret;
}

389 390
static int nilfs_ioctl_delete_checkpoints(struct the_nilfs *nilfs,
					  struct nilfs_argv *argv, void *buf)
K
Koji Sato 已提交
391
{
392
	size_t nmembs = argv->v_nmembs;
K
Koji Sato 已提交
393 394 395 396 397 398 399 400 401 402 403 404 405
	struct inode *cpfile = nilfs->ns_cpfile;
	struct nilfs_period *periods = buf;
	int ret, i;

	for (i = 0; i < nmembs; i++) {
		ret = nilfs_cpfile_delete_checkpoints(
			cpfile, periods[i].p_start, periods[i].p_end);
		if (ret < 0)
			return ret;
	}
	return nmembs;
}

406 407
static int nilfs_ioctl_free_vblocknrs(struct the_nilfs *nilfs,
				      struct nilfs_argv *argv, void *buf)
K
Koji Sato 已提交
408
{
409 410
	size_t nmembs = argv->v_nmembs;
	int ret;
K
Koji Sato 已提交
411

412
	ret = nilfs_dat_freev(nilfs_dat_inode(nilfs), buf, nmembs);
K
Koji Sato 已提交
413 414 415 416

	return (ret < 0) ? ret : nmembs;
}

417 418
static int nilfs_ioctl_mark_blocks_dirty(struct the_nilfs *nilfs,
					 struct nilfs_argv *argv, void *buf)
K
Koji Sato 已提交
419
{
420
	size_t nmembs = argv->v_nmembs;
K
Koji Sato 已提交
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
	struct inode *dat = nilfs_dat_inode(nilfs);
	struct nilfs_bmap *bmap = NILFS_I(dat)->i_bmap;
	struct nilfs_bdesc *bdescs = buf;
	int ret, i;

	for (i = 0; i < nmembs; i++) {
		/* XXX: use macro or inline func to check liveness */
		ret = nilfs_bmap_lookup_at_level(bmap,
						 bdescs[i].bd_offset,
						 bdescs[i].bd_level + 1,
						 &bdescs[i].bd_blocknr);
		if (ret < 0) {
			if (ret != -ENOENT)
				return ret;
			bdescs[i].bd_blocknr = 0;
		}
		if (bdescs[i].bd_blocknr != bdescs[i].bd_oblocknr)
			/* skip dead block */
			continue;
		if (bdescs[i].bd_level == 0) {
			ret = nilfs_mdt_mark_block_dirty(dat,
							 bdescs[i].bd_offset);
			if (ret < 0) {
444
				WARN_ON(ret == -ENOENT);
K
Koji Sato 已提交
445 446 447 448 449 450
				return ret;
			}
		} else {
			ret = nilfs_bmap_mark(bmap, bdescs[i].bd_offset,
					      bdescs[i].bd_level);
			if (ret < 0) {
451
				WARN_ON(ret == -ENOENT);
K
Koji Sato 已提交
452 453 454 455 456 457 458 459
				return ret;
			}
		}
	}
	return nmembs;
}

int nilfs_ioctl_prepare_clean_segments(struct the_nilfs *nilfs,
460
				       struct nilfs_argv *argv, void **kbufs)
K
Koji Sato 已提交
461
{
462
	const char *msg;
463
	int ret;
K
Koji Sato 已提交
464

465
	ret = nilfs_ioctl_delete_checkpoints(nilfs, &argv[1], kbufs[1]);
466 467 468 469 470 471 472 473
	if (ret < 0) {
		/*
		 * can safely abort because checkpoints can be removed
		 * independently.
		 */
		msg = "cannot delete checkpoints";
		goto failed;
	}
474
	ret = nilfs_ioctl_free_vblocknrs(nilfs, &argv[2], kbufs[2]);
475 476 477 478 479 480 481 482
	if (ret < 0) {
		/*
		 * can safely abort because DAT file is updated atomically
		 * using a copy-on-write technique.
		 */
		msg = "cannot delete virtual blocks from DAT file";
		goto failed;
	}
483
	ret = nilfs_ioctl_mark_blocks_dirty(nilfs, &argv[3], kbufs[3]);
484 485 486 487 488 489 490
	if (ret < 0) {
		/*
		 * can safely abort because the operation is nondestructive.
		 */
		msg = "cannot mark copying blocks dirty";
		goto failed;
	}
K
Koji Sato 已提交
491 492
	return 0;

493 494 495
 failed:
	printk(KERN_ERR "NILFS: GC failed during preparation: %s: err=%d\n",
	       msg, ret);
K
Koji Sato 已提交
496 497 498 499 500 501
	return ret;
}

static int nilfs_ioctl_clean_segments(struct inode *inode, struct file *filp,
				      unsigned int cmd, void __user *argp)
{
502
	struct nilfs_argv argv[5];
503
	static const size_t argsz[5] = {
504 505 506 507 508 509 510 511 512 513 514 515
		sizeof(struct nilfs_vdesc),
		sizeof(struct nilfs_period),
		sizeof(__u64),
		sizeof(struct nilfs_bdesc),
		sizeof(__u64),
	};
	void __user *base;
	void *kbufs[5];
	struct the_nilfs *nilfs;
	size_t len, nsegs;
	int n, ret;

K
Koji Sato 已提交
516 517
	if (!capable(CAP_SYS_ADMIN))
		return -EPERM;
518

519 520 521 522 523
	ret = mnt_want_write(filp->f_path.mnt);
	if (ret)
		return ret;

	ret = -EFAULT;
524
	if (copy_from_user(argv, argp, sizeof(argv)))
525
		goto out;
526

527
	ret = -EINVAL;
528 529
	nsegs = argv[4].v_nmembs;
	if (argv[4].v_size != argsz[4])
530 531
		goto out;

532 533 534 535 536 537 538
	/*
	 * argv[4] points to segment numbers this ioctl cleans.  We
	 * use kmalloc() for its buffer because memory used for the
	 * segment numbers is enough small.
	 */
	kbufs[4] = memdup_user((void __user *)(unsigned long)argv[4].v_base,
			       nsegs * sizeof(__u64));
539 540 541 542
	if (IS_ERR(kbufs[4])) {
		ret = PTR_ERR(kbufs[4]);
		goto out;
	}
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571
	nilfs = NILFS_SB(inode->i_sb)->s_nilfs;

	for (n = 0; n < 4; n++) {
		ret = -EINVAL;
		if (argv[n].v_size != argsz[n])
			goto out_free;

		if (argv[n].v_nmembs > nsegs * nilfs->ns_blocks_per_segment)
			goto out_free;

		len = argv[n].v_size * argv[n].v_nmembs;
		base = (void __user *)(unsigned long)argv[n].v_base;
		if (len == 0) {
			kbufs[n] = NULL;
			continue;
		}

		kbufs[n] = vmalloc(len);
		if (!kbufs[n]) {
			ret = -ENOMEM;
			goto out_free;
		}
		if (copy_from_user(kbufs[n], base, len)) {
			ret = -EFAULT;
			vfree(kbufs[n]);
			goto out_free;
		}
	}

572
	/*
573
	 * nilfs_ioctl_move_blocks() will call nilfs_iget_for_gc(),
574 575 576 577 578 579 580 581 582
	 * which will operates an inode list without blocking.
	 * To protect the list from concurrent operations,
	 * nilfs_ioctl_move_blocks should be atomic operation.
	 */
	if (test_and_set_bit(THE_NILFS_GC_RUNNING, &nilfs->ns_flags)) {
		ret = -EBUSY;
		goto out_free;
	}

583
	ret = nilfs_ioctl_move_blocks(inode->i_sb, &argv[0], kbufs[0]);
584 585 586 587 588 589
	if (ret < 0)
		printk(KERN_ERR "NILFS: GC failed during preparation: "
			"cannot read source blocks: err=%d\n", ret);
	else
		ret = nilfs_clean_segments(inode->i_sb, argv, kbufs);

590
	nilfs_remove_all_gcinodes(nilfs);
591
	clear_nilfs_gc_running(nilfs);
592

593
out_free:
594
	while (--n >= 0)
595 596
		vfree(kbufs[n]);
	kfree(kbufs[4]);
597 598
out:
	mnt_drop_write(filp->f_path.mnt);
599
	return ret;
K
Koji Sato 已提交
600 601 602 603 604 605 606
}

static int nilfs_ioctl_sync(struct inode *inode, struct file *filp,
			    unsigned int cmd, void __user *argp)
{
	__u64 cno;
	int ret;
607
	struct the_nilfs *nilfs;
K
Koji Sato 已提交
608 609 610 611 612 613

	ret = nilfs_construct_segment(inode->i_sb);
	if (ret < 0)
		return ret;

	if (argp != NULL) {
614 615 616 617
		nilfs = NILFS_SB(inode->i_sb)->s_nilfs;
		down_read(&nilfs->ns_segctor_sem);
		cno = nilfs->ns_cno - 1;
		up_read(&nilfs->ns_segctor_sem);
K
Koji Sato 已提交
618 619 620 621 622 623
		if (copy_to_user(argp, &cno, sizeof(cno)))
			return -EFAULT;
	}
	return 0;
}

624 625
static int nilfs_ioctl_get_info(struct inode *inode, struct file *filp,
				unsigned int cmd, void __user *argp,
626
				size_t membsz,
627 628 629 630 631 632 633 634 635 636 637 638
				ssize_t (*dofunc)(struct the_nilfs *,
						  __u64 *, int,
						  void *, size_t, size_t))

{
	struct the_nilfs *nilfs = NILFS_SB(inode->i_sb)->s_nilfs;
	struct nilfs_argv argv;
	int ret;

	if (copy_from_user(&argv, argp, sizeof(argv)))
		return -EFAULT;

639
	if (argv.v_size < membsz)
640 641
		return -EINVAL;

642 643 644 645 646 647 648 649 650
	ret = nilfs_ioctl_wrap_copy(nilfs, &argv, _IOC_DIR(cmd), dofunc);
	if (ret < 0)
		return ret;

	if (copy_to_user(argp, &argv, sizeof(argv)))
		ret = -EFAULT;
	return ret;
}

R
Ryusuke Konishi 已提交
651
long nilfs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
K
Koji Sato 已提交
652
{
R
Ryusuke Konishi 已提交
653
	struct inode *inode = filp->f_dentry->d_inode;
654
	void __user *argp = (void __user *)arg;
K
Koji Sato 已提交
655 656 657 658 659 660 661

	switch (cmd) {
	case NILFS_IOCTL_CHANGE_CPMODE:
		return nilfs_ioctl_change_cpmode(inode, filp, cmd, argp);
	case NILFS_IOCTL_DELETE_CHECKPOINT:
		return nilfs_ioctl_delete_checkpoint(inode, filp, cmd, argp);
	case NILFS_IOCTL_GET_CPINFO:
662
		return nilfs_ioctl_get_info(inode, filp, cmd, argp,
663
					    sizeof(struct nilfs_cpinfo),
664
					    nilfs_ioctl_do_get_cpinfo);
K
Koji Sato 已提交
665 666 667
	case NILFS_IOCTL_GET_CPSTAT:
		return nilfs_ioctl_get_cpstat(inode, filp, cmd, argp);
	case NILFS_IOCTL_GET_SUINFO:
668
		return nilfs_ioctl_get_info(inode, filp, cmd, argp,
669
					    sizeof(struct nilfs_suinfo),
670
					    nilfs_ioctl_do_get_suinfo);
K
Koji Sato 已提交
671 672 673
	case NILFS_IOCTL_GET_SUSTAT:
		return nilfs_ioctl_get_sustat(inode, filp, cmd, argp);
	case NILFS_IOCTL_GET_VINFO:
674
		return nilfs_ioctl_get_info(inode, filp, cmd, argp,
675
					    sizeof(struct nilfs_vinfo),
676
					    nilfs_ioctl_do_get_vinfo);
K
Koji Sato 已提交
677 678 679 680 681 682 683 684 685 686
	case NILFS_IOCTL_GET_BDESCS:
		return nilfs_ioctl_get_bdescs(inode, filp, cmd, argp);
	case NILFS_IOCTL_CLEAN_SEGMENTS:
		return nilfs_ioctl_clean_segments(inode, filp, cmd, argp);
	case NILFS_IOCTL_SYNC:
		return nilfs_ioctl_sync(inode, filp, cmd, argp);
	default:
		return -ENOTTY;
	}
}