]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - fs/nilfs2/ioctl.c
nilfs2: use sb instance instead of nilfs_sb_info struct
[mirror_ubuntu-jammy-kernel.git] / fs / nilfs2 / ioctl.c
CommitLineData
7942b919
KS
1/*
2 * ioctl.c - NILFS ioctl operations.
3 *
4 * Copyright (C) 2007, 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 */
22
23#include <linux/fs.h>
24#include <linux/wait.h>
5a0e3ad6 25#include <linux/slab.h>
7942b919
KS
26#include <linux/capability.h> /* capable() */
27#include <linux/uaccess.h> /* copy_from_user(), copy_to_user() */
4f6b8288 28#include <linux/vmalloc.h>
828b1c50 29#include <linux/compat.h> /* compat_ptr() */
7512487e 30#include <linux/mount.h> /* mnt_want_write(), mnt_drop_write() */
ae191838 31#include <linux/buffer_head.h>
7942b919
KS
32#include <linux/nilfs2_fs.h>
33#include "nilfs.h"
34#include "segment.h"
35#include "bmap.h"
36#include "cpfile.h"
37#include "sufile.h"
38#include "dat.h"
39
40
7942b919
KS
41static int nilfs_ioctl_wrap_copy(struct the_nilfs *nilfs,
42 struct nilfs_argv *argv, int dir,
43 ssize_t (*dofunc)(struct the_nilfs *,
b028fcfc 44 __u64 *, int,
7942b919
KS
45 void *, size_t, size_t))
46{
47 void *buf;
dc498d09 48 void __user *base = (void __user *)(unsigned long)argv->v_base;
3358b4aa 49 size_t maxmembs, total, n;
7942b919
KS
50 ssize_t nr;
51 int ret, i;
b028fcfc 52 __u64 pos, ppos;
7942b919
KS
53
54 if (argv->v_nmembs == 0)
55 return 0;
56
3358b4aa
RK
57 if (argv->v_size > PAGE_SIZE)
58 return -EINVAL;
59
60 buf = (void *)__get_free_pages(GFP_NOFS, 0);
61 if (unlikely(!buf))
7942b919 62 return -ENOMEM;
3358b4aa 63 maxmembs = PAGE_SIZE / argv->v_size;
7942b919
KS
64
65 ret = 0;
66 total = 0;
b028fcfc 67 pos = argv->v_index;
7942b919
KS
68 for (i = 0; i < argv->v_nmembs; i += n) {
69 n = (argv->v_nmembs - i < maxmembs) ?
70 argv->v_nmembs - i : maxmembs;
71 if ((dir & _IOC_WRITE) &&
dc498d09
RK
72 copy_from_user(buf, base + argv->v_size * i,
73 argv->v_size * n)) {
7942b919
KS
74 ret = -EFAULT;
75 break;
76 }
b028fcfc 77 ppos = pos;
8acfbf09 78 nr = dofunc(nilfs, &pos, argv->v_flags, buf, argv->v_size,
b028fcfc 79 n);
7942b919
KS
80 if (nr < 0) {
81 ret = nr;
82 break;
83 }
84 if ((dir & _IOC_READ) &&
dc498d09
RK
85 copy_to_user(base + argv->v_size * i, buf,
86 argv->v_size * nr)) {
7942b919
KS
87 ret = -EFAULT;
88 break;
89 }
90 total += nr;
b028fcfc
RK
91 if ((size_t)nr < n)
92 break;
93 if (pos == ppos)
94 pos += n;
7942b919
KS
95 }
96 argv->v_nmembs = total;
97
3358b4aa 98 free_pages((unsigned long)buf, 0);
7942b919
KS
99 return ret;
100}
101
cde98f0f
RK
102static int nilfs_ioctl_getflags(struct inode *inode, void __user *argp)
103{
104 unsigned int flags = NILFS_I(inode)->i_flags & FS_FL_USER_VISIBLE;
105
106 return put_user(flags, (int __user *)argp);
107}
108
109static int nilfs_ioctl_setflags(struct inode *inode, struct file *filp,
110 void __user *argp)
111{
112 struct nilfs_transaction_info ti;
113 unsigned int flags, oldflags;
114 int ret;
115
116 if (!is_owner_or_cap(inode))
117 return -EACCES;
118
119 if (get_user(flags, (int __user *)argp))
120 return -EFAULT;
121
122 ret = mnt_want_write(filp->f_path.mnt);
123 if (ret)
124 return ret;
125
126 flags = nilfs_mask_flags(inode->i_mode, flags);
127
128 mutex_lock(&inode->i_mutex);
129
130 oldflags = NILFS_I(inode)->i_flags;
131
132 /*
133 * The IMMUTABLE and APPEND_ONLY flags can only be changed by the
134 * relevant capability.
135 */
136 ret = -EPERM;
137 if (((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) &&
138 !capable(CAP_LINUX_IMMUTABLE))
139 goto out;
140
141 ret = nilfs_transaction_begin(inode->i_sb, &ti, 0);
142 if (ret)
143 goto out;
144
145 NILFS_I(inode)->i_flags = (oldflags & ~FS_FL_USER_MODIFIABLE) |
146 (flags & FS_FL_USER_MODIFIABLE);
147
148 nilfs_set_inode_flags(inode);
149 inode->i_ctime = CURRENT_TIME;
150 if (IS_SYNC(inode))
151 nilfs_set_transaction_flag(NILFS_TI_SYNC);
152
153 nilfs_mark_inode_dirty(inode);
154 ret = nilfs_transaction_commit(inode->i_sb);
155out:
156 mutex_unlock(&inode->i_mutex);
157 mnt_drop_write(filp->f_path.mnt);
158 return ret;
159}
160
161static int nilfs_ioctl_getversion(struct inode *inode, void __user *argp)
162{
163 return put_user(inode->i_generation, (int __user *)argp);
164}
165
7942b919
KS
166static int nilfs_ioctl_change_cpmode(struct inode *inode, struct file *filp,
167 unsigned int cmd, void __user *argp)
168{
c1ea985c
RK
169 struct the_nilfs *nilfs = NILFS_SB(inode->i_sb)->s_nilfs;
170 struct inode *cpfile = nilfs->ns_cpfile;
7942b919
KS
171 struct nilfs_transaction_info ti;
172 struct nilfs_cpmode cpmode;
173 int ret;
174
175 if (!capable(CAP_SYS_ADMIN))
176 return -EPERM;
7512487e
RK
177
178 ret = mnt_want_write(filp->f_path.mnt);
179 if (ret)
180 return ret;
181
182 ret = -EFAULT;
7942b919 183 if (copy_from_user(&cpmode, argp, sizeof(cpmode)))
7512487e 184 goto out;
7942b919 185
348fe8da 186 down_read(&inode->i_sb->s_umount);
7512487e 187
7942b919
KS
188 nilfs_transaction_begin(inode->i_sb, &ti, 0);
189 ret = nilfs_cpfile_change_cpmode(
190 cpfile, cpmode.cm_cno, cpmode.cm_mode);
7512487e 191 if (unlikely(ret < 0))
47420c79 192 nilfs_transaction_abort(inode->i_sb);
7512487e
RK
193 else
194 nilfs_transaction_commit(inode->i_sb); /* never fails */
195
348fe8da 196 up_read(&inode->i_sb->s_umount);
7512487e
RK
197out:
198 mnt_drop_write(filp->f_path.mnt);
7942b919
KS
199 return ret;
200}
201
202static int
203nilfs_ioctl_delete_checkpoint(struct inode *inode, struct file *filp,
204 unsigned int cmd, void __user *argp)
205{
206 struct inode *cpfile = NILFS_SB(inode->i_sb)->s_nilfs->ns_cpfile;
207 struct nilfs_transaction_info ti;
208 __u64 cno;
209 int ret;
210
211 if (!capable(CAP_SYS_ADMIN))
212 return -EPERM;
7512487e
RK
213
214 ret = mnt_want_write(filp->f_path.mnt);
215 if (ret)
216 return ret;
217
218 ret = -EFAULT;
7942b919 219 if (copy_from_user(&cno, argp, sizeof(cno)))
7512487e 220 goto out;
7942b919
KS
221
222 nilfs_transaction_begin(inode->i_sb, &ti, 0);
223 ret = nilfs_cpfile_delete_checkpoint(cpfile, cno);
7512487e 224 if (unlikely(ret < 0))
47420c79 225 nilfs_transaction_abort(inode->i_sb);
7512487e
RK
226 else
227 nilfs_transaction_commit(inode->i_sb); /* never fails */
228out:
229 mnt_drop_write(filp->f_path.mnt);
7942b919
KS
230 return ret;
231}
232
233static ssize_t
b028fcfc 234nilfs_ioctl_do_get_cpinfo(struct the_nilfs *nilfs, __u64 *posp, int flags,
7942b919
KS
235 void *buf, size_t size, size_t nmembs)
236{
7942b919
KS
237 int ret;
238
47420c79 239 down_read(&nilfs->ns_segctor_sem);
47eb6b9c 240 ret = nilfs_cpfile_get_cpinfo(nilfs->ns_cpfile, posp, flags, buf,
003ff182 241 size, nmembs);
47420c79 242 up_read(&nilfs->ns_segctor_sem);
7942b919
KS
243 return ret;
244}
245
246static int nilfs_ioctl_get_cpstat(struct inode *inode, struct file *filp,
247 unsigned int cmd, void __user *argp)
248{
47420c79 249 struct the_nilfs *nilfs = NILFS_SB(inode->i_sb)->s_nilfs;
7942b919 250 struct nilfs_cpstat cpstat;
7942b919
KS
251 int ret;
252
47420c79
RK
253 down_read(&nilfs->ns_segctor_sem);
254 ret = nilfs_cpfile_get_stat(nilfs->ns_cpfile, &cpstat);
255 up_read(&nilfs->ns_segctor_sem);
7942b919
KS
256 if (ret < 0)
257 return ret;
258
259 if (copy_to_user(argp, &cpstat, sizeof(cpstat)))
260 ret = -EFAULT;
261 return ret;
262}
263
264static ssize_t
b028fcfc 265nilfs_ioctl_do_get_suinfo(struct the_nilfs *nilfs, __u64 *posp, int flags,
7942b919
KS
266 void *buf, size_t size, size_t nmembs)
267{
7942b919
KS
268 int ret;
269
47420c79 270 down_read(&nilfs->ns_segctor_sem);
003ff182
RK
271 ret = nilfs_sufile_get_suinfo(nilfs->ns_sufile, *posp, buf, size,
272 nmembs);
47420c79 273 up_read(&nilfs->ns_segctor_sem);
7942b919
KS
274 return ret;
275}
276
277static int nilfs_ioctl_get_sustat(struct inode *inode, struct file *filp,
278 unsigned int cmd, void __user *argp)
279{
47420c79 280 struct the_nilfs *nilfs = NILFS_SB(inode->i_sb)->s_nilfs;
7942b919 281 struct nilfs_sustat sustat;
7942b919
KS
282 int ret;
283
47420c79
RK
284 down_read(&nilfs->ns_segctor_sem);
285 ret = nilfs_sufile_get_stat(nilfs->ns_sufile, &sustat);
286 up_read(&nilfs->ns_segctor_sem);
7942b919
KS
287 if (ret < 0)
288 return ret;
289
290 if (copy_to_user(argp, &sustat, sizeof(sustat)))
291 ret = -EFAULT;
292 return ret;
293}
294
295static ssize_t
b028fcfc 296nilfs_ioctl_do_get_vinfo(struct the_nilfs *nilfs, __u64 *posp, int flags,
7942b919
KS
297 void *buf, size_t size, size_t nmembs)
298{
7942b919
KS
299 int ret;
300
47420c79 301 down_read(&nilfs->ns_segctor_sem);
365e215c 302 ret = nilfs_dat_get_vinfo(nilfs->ns_dat, buf, size, nmembs);
47420c79 303 up_read(&nilfs->ns_segctor_sem);
7942b919
KS
304 return ret;
305}
306
307static ssize_t
b028fcfc 308nilfs_ioctl_do_get_bdescs(struct the_nilfs *nilfs, __u64 *posp, int flags,
7942b919
KS
309 void *buf, size_t size, size_t nmembs)
310{
365e215c 311 struct nilfs_bmap *bmap = NILFS_I(nilfs->ns_dat)->i_bmap;
7942b919
KS
312 struct nilfs_bdesc *bdescs = buf;
313 int ret, i;
314
47eb6b9c 315 down_read(&nilfs->ns_segctor_sem);
7942b919
KS
316 for (i = 0; i < nmembs; i++) {
317 ret = nilfs_bmap_lookup_at_level(bmap,
318 bdescs[i].bd_offset,
319 bdescs[i].bd_level + 1,
320 &bdescs[i].bd_blocknr);
321 if (ret < 0) {
47eb6b9c
RK
322 if (ret != -ENOENT) {
323 up_read(&nilfs->ns_segctor_sem);
7942b919 324 return ret;
47eb6b9c 325 }
7942b919
KS
326 bdescs[i].bd_blocknr = 0;
327 }
328 }
47eb6b9c 329 up_read(&nilfs->ns_segctor_sem);
7942b919
KS
330 return nmembs;
331}
332
333static int nilfs_ioctl_get_bdescs(struct inode *inode, struct file *filp,
334 unsigned int cmd, void __user *argp)
335{
336 struct the_nilfs *nilfs = NILFS_SB(inode->i_sb)->s_nilfs;
337 struct nilfs_argv argv;
7942b919
KS
338 int ret;
339
340 if (copy_from_user(&argv, argp, sizeof(argv)))
341 return -EFAULT;
342
83aca8f4
RK
343 if (argv.v_size != sizeof(struct nilfs_bdesc))
344 return -EINVAL;
345
7942b919
KS
346 ret = nilfs_ioctl_wrap_copy(nilfs, &argv, _IOC_DIR(cmd),
347 nilfs_ioctl_do_get_bdescs);
47420c79
RK
348 if (ret < 0)
349 return ret;
7942b919
KS
350
351 if (copy_to_user(argp, &argv, sizeof(argv)))
352 ret = -EFAULT;
353 return ret;
354}
355
356static int nilfs_ioctl_move_inode_block(struct inode *inode,
357 struct nilfs_vdesc *vdesc,
358 struct list_head *buffers)
359{
360 struct buffer_head *bh;
361 int ret;
362
363 if (vdesc->vd_flags == 0)
364 ret = nilfs_gccache_submit_read_data(
365 inode, vdesc->vd_offset, vdesc->vd_blocknr,
366 vdesc->vd_vblocknr, &bh);
367 else
368 ret = nilfs_gccache_submit_read_node(
369 inode, vdesc->vd_blocknr, vdesc->vd_vblocknr, &bh);
370
371 if (unlikely(ret < 0)) {
372 if (ret == -ENOENT)
373 printk(KERN_CRIT
374 "%s: invalid virtual block address (%s): "
375 "ino=%llu, cno=%llu, offset=%llu, "
376 "blocknr=%llu, vblocknr=%llu\n",
377 __func__, vdesc->vd_flags ? "node" : "data",
378 (unsigned long long)vdesc->vd_ino,
379 (unsigned long long)vdesc->vd_cno,
380 (unsigned long long)vdesc->vd_offset,
381 (unsigned long long)vdesc->vd_blocknr,
382 (unsigned long long)vdesc->vd_vblocknr);
383 return ret;
384 }
5399dd1f
RK
385 if (unlikely(!list_empty(&bh->b_assoc_buffers))) {
386 printk(KERN_CRIT "%s: conflicting %s buffer: ino=%llu, "
387 "cno=%llu, offset=%llu, blocknr=%llu, vblocknr=%llu\n",
388 __func__, vdesc->vd_flags ? "node" : "data",
389 (unsigned long long)vdesc->vd_ino,
390 (unsigned long long)vdesc->vd_cno,
391 (unsigned long long)vdesc->vd_offset,
392 (unsigned long long)vdesc->vd_blocknr,
393 (unsigned long long)vdesc->vd_vblocknr);
394 brelse(bh);
395 return -EEXIST;
396 }
7942b919
KS
397 list_add_tail(&bh->b_assoc_buffers, buffers);
398 return 0;
399}
400
263d90ce 401static int nilfs_ioctl_move_blocks(struct super_block *sb,
4f6b8288 402 struct nilfs_argv *argv, void *buf)
7942b919 403{
4f6b8288 404 size_t nmembs = argv->v_nmembs;
947b10ae 405 struct the_nilfs *nilfs = NILFS_SB(sb)->s_nilfs;
7942b919
KS
406 struct inode *inode;
407 struct nilfs_vdesc *vdesc;
408 struct buffer_head *bh, *n;
409 LIST_HEAD(buffers);
410 ino_t ino;
411 __u64 cno;
412 int i, ret;
413
414 for (i = 0, vdesc = buf; i < nmembs; ) {
415 ino = vdesc->vd_ino;
416 cno = vdesc->vd_cno;
263d90ce 417 inode = nilfs_iget_for_gc(sb, ino, cno);
103cfcf5
DC
418 if (IS_ERR(inode)) {
419 ret = PTR_ERR(inode);
7942b919
KS
420 goto failed;
421 }
947b10ae
RK
422 if (list_empty(&NILFS_I(inode)->i_dirty)) {
423 /*
424 * Add the inode to GC inode list. Garbage Collection
425 * is serialized and no two processes manipulate the
426 * list simultaneously.
427 */
428 igrab(inode);
429 list_add(&NILFS_I(inode)->i_dirty,
430 &nilfs->ns_gc_inodes);
431 }
432
7942b919
KS
433 do {
434 ret = nilfs_ioctl_move_inode_block(inode, vdesc,
435 &buffers);
263d90ce
RK
436 if (unlikely(ret < 0)) {
437 iput(inode);
7942b919 438 goto failed;
263d90ce 439 }
7942b919
KS
440 vdesc++;
441 } while (++i < nmembs &&
442 vdesc->vd_ino == ino && vdesc->vd_cno == cno);
263d90ce
RK
443
444 iput(inode); /* The inode still remains in GC inode list */
7942b919
KS
445 }
446
447 list_for_each_entry_safe(bh, n, &buffers, b_assoc_buffers) {
448 ret = nilfs_gccache_wait_and_mark_dirty(bh);
449 if (unlikely(ret < 0)) {
5399dd1f 450 WARN_ON(ret == -EEXIST);
7942b919
KS
451 goto failed;
452 }
453 list_del_init(&bh->b_assoc_buffers);
7942b919
KS
454 brelse(bh);
455 }
456 return nmembs;
457
458 failed:
459 list_for_each_entry_safe(bh, n, &buffers, b_assoc_buffers) {
460 list_del_init(&bh->b_assoc_buffers);
7942b919
KS
461 brelse(bh);
462 }
463 return ret;
464}
465
4f6b8288
RK
466static int nilfs_ioctl_delete_checkpoints(struct the_nilfs *nilfs,
467 struct nilfs_argv *argv, void *buf)
7942b919 468{
4f6b8288 469 size_t nmembs = argv->v_nmembs;
7942b919
KS
470 struct inode *cpfile = nilfs->ns_cpfile;
471 struct nilfs_period *periods = buf;
472 int ret, i;
473
474 for (i = 0; i < nmembs; i++) {
475 ret = nilfs_cpfile_delete_checkpoints(
476 cpfile, periods[i].p_start, periods[i].p_end);
477 if (ret < 0)
478 return ret;
479 }
480 return nmembs;
481}
482
4f6b8288
RK
483static int nilfs_ioctl_free_vblocknrs(struct the_nilfs *nilfs,
484 struct nilfs_argv *argv, void *buf)
7942b919 485{
4f6b8288
RK
486 size_t nmembs = argv->v_nmembs;
487 int ret;
7942b919 488
365e215c 489 ret = nilfs_dat_freev(nilfs->ns_dat, buf, nmembs);
7942b919
KS
490
491 return (ret < 0) ? ret : nmembs;
492}
493
4f6b8288
RK
494static int nilfs_ioctl_mark_blocks_dirty(struct the_nilfs *nilfs,
495 struct nilfs_argv *argv, void *buf)
7942b919 496{
4f6b8288 497 size_t nmembs = argv->v_nmembs;
365e215c 498 struct nilfs_bmap *bmap = NILFS_I(nilfs->ns_dat)->i_bmap;
7942b919
KS
499 struct nilfs_bdesc *bdescs = buf;
500 int ret, i;
501
502 for (i = 0; i < nmembs; i++) {
503 /* XXX: use macro or inline func to check liveness */
504 ret = nilfs_bmap_lookup_at_level(bmap,
505 bdescs[i].bd_offset,
506 bdescs[i].bd_level + 1,
507 &bdescs[i].bd_blocknr);
508 if (ret < 0) {
509 if (ret != -ENOENT)
510 return ret;
511 bdescs[i].bd_blocknr = 0;
512 }
513 if (bdescs[i].bd_blocknr != bdescs[i].bd_oblocknr)
514 /* skip dead block */
515 continue;
516 if (bdescs[i].bd_level == 0) {
365e215c 517 ret = nilfs_mdt_mark_block_dirty(nilfs->ns_dat,
7942b919
KS
518 bdescs[i].bd_offset);
519 if (ret < 0) {
1f5abe7e 520 WARN_ON(ret == -ENOENT);
7942b919
KS
521 return ret;
522 }
523 } else {
524 ret = nilfs_bmap_mark(bmap, bdescs[i].bd_offset,
525 bdescs[i].bd_level);
526 if (ret < 0) {
1f5abe7e 527 WARN_ON(ret == -ENOENT);
7942b919
KS
528 return ret;
529 }
530 }
531 }
532 return nmembs;
533}
534
7942b919 535int nilfs_ioctl_prepare_clean_segments(struct the_nilfs *nilfs,
4f6b8288 536 struct nilfs_argv *argv, void **kbufs)
7942b919 537{
1f5abe7e 538 const char *msg;
4f6b8288 539 int ret;
7942b919 540
4f6b8288 541 ret = nilfs_ioctl_delete_checkpoints(nilfs, &argv[1], kbufs[1]);
1f5abe7e
RK
542 if (ret < 0) {
543 /*
544 * can safely abort because checkpoints can be removed
545 * independently.
546 */
547 msg = "cannot delete checkpoints";
548 goto failed;
549 }
4f6b8288 550 ret = nilfs_ioctl_free_vblocknrs(nilfs, &argv[2], kbufs[2]);
1f5abe7e
RK
551 if (ret < 0) {
552 /*
553 * can safely abort because DAT file is updated atomically
554 * using a copy-on-write technique.
555 */
556 msg = "cannot delete virtual blocks from DAT file";
557 goto failed;
558 }
4f6b8288 559 ret = nilfs_ioctl_mark_blocks_dirty(nilfs, &argv[3], kbufs[3]);
1f5abe7e
RK
560 if (ret < 0) {
561 /*
562 * can safely abort because the operation is nondestructive.
563 */
564 msg = "cannot mark copying blocks dirty";
565 goto failed;
566 }
7942b919
KS
567 return 0;
568
1f5abe7e 569 failed:
1f5abe7e
RK
570 printk(KERN_ERR "NILFS: GC failed during preparation: %s: err=%d\n",
571 msg, ret);
7942b919
KS
572 return ret;
573}
574
575static int nilfs_ioctl_clean_segments(struct inode *inode, struct file *filp,
576 unsigned int cmd, void __user *argp)
577{
4f6b8288 578 struct nilfs_argv argv[5];
33e189bd 579 static const size_t argsz[5] = {
4f6b8288
RK
580 sizeof(struct nilfs_vdesc),
581 sizeof(struct nilfs_period),
582 sizeof(__u64),
583 sizeof(struct nilfs_bdesc),
584 sizeof(__u64),
585 };
586 void __user *base;
587 void *kbufs[5];
588 struct the_nilfs *nilfs;
589 size_t len, nsegs;
590 int n, ret;
591
7942b919
KS
592 if (!capable(CAP_SYS_ADMIN))
593 return -EPERM;
4f6b8288 594
7512487e
RK
595 ret = mnt_want_write(filp->f_path.mnt);
596 if (ret)
597 return ret;
598
599 ret = -EFAULT;
4f6b8288 600 if (copy_from_user(argv, argp, sizeof(argv)))
7512487e 601 goto out;
4f6b8288 602
7512487e 603 ret = -EINVAL;
4f6b8288
RK
604 nsegs = argv[4].v_nmembs;
605 if (argv[4].v_size != argsz[4])
7512487e
RK
606 goto out;
607
4f6b8288
RK
608 /*
609 * argv[4] points to segment numbers this ioctl cleans. We
610 * use kmalloc() for its buffer because memory used for the
611 * segment numbers is enough small.
612 */
613 kbufs[4] = memdup_user((void __user *)(unsigned long)argv[4].v_base,
614 nsegs * sizeof(__u64));
7512487e
RK
615 if (IS_ERR(kbufs[4])) {
616 ret = PTR_ERR(kbufs[4]);
617 goto out;
618 }
4f6b8288
RK
619 nilfs = NILFS_SB(inode->i_sb)->s_nilfs;
620
621 for (n = 0; n < 4; n++) {
622 ret = -EINVAL;
623 if (argv[n].v_size != argsz[n])
624 goto out_free;
625
626 if (argv[n].v_nmembs > nsegs * nilfs->ns_blocks_per_segment)
627 goto out_free;
628
629 len = argv[n].v_size * argv[n].v_nmembs;
630 base = (void __user *)(unsigned long)argv[n].v_base;
631 if (len == 0) {
632 kbufs[n] = NULL;
633 continue;
634 }
635
636 kbufs[n] = vmalloc(len);
637 if (!kbufs[n]) {
638 ret = -ENOMEM;
639 goto out_free;
640 }
641 if (copy_from_user(kbufs[n], base, len)) {
642 ret = -EFAULT;
643 vfree(kbufs[n]);
644 goto out_free;
645 }
646 }
647
1cf58fa8 648 /*
263d90ce 649 * nilfs_ioctl_move_blocks() will call nilfs_iget_for_gc(),
1cf58fa8
JS
650 * which will operates an inode list without blocking.
651 * To protect the list from concurrent operations,
652 * nilfs_ioctl_move_blocks should be atomic operation.
653 */
654 if (test_and_set_bit(THE_NILFS_GC_RUNNING, &nilfs->ns_flags)) {
655 ret = -EBUSY;
656 goto out_free;
657 }
658
5beb6e0b
RK
659 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
660
263d90ce 661 ret = nilfs_ioctl_move_blocks(inode->i_sb, &argv[0], kbufs[0]);
1cf58fa8
JS
662 if (ret < 0)
663 printk(KERN_ERR "NILFS: GC failed during preparation: "
664 "cannot read source blocks: err=%d\n", ret);
665 else
666 ret = nilfs_clean_segments(inode->i_sb, argv, kbufs);
667
263d90ce 668 nilfs_remove_all_gcinodes(nilfs);
1cf58fa8 669 clear_nilfs_gc_running(nilfs);
4f6b8288 670
7512487e 671out_free:
d5046853 672 while (--n >= 0)
4f6b8288
RK
673 vfree(kbufs[n]);
674 kfree(kbufs[4]);
7512487e
RK
675out:
676 mnt_drop_write(filp->f_path.mnt);
4f6b8288 677 return ret;
7942b919
KS
678}
679
680static int nilfs_ioctl_sync(struct inode *inode, struct file *filp,
681 unsigned int cmd, void __user *argp)
682{
683 __u64 cno;
684 int ret;
0d561f12 685 struct the_nilfs *nilfs;
7942b919
KS
686
687 ret = nilfs_construct_segment(inode->i_sb);
688 if (ret < 0)
689 return ret;
690
691 if (argp != NULL) {
0d561f12
JS
692 nilfs = NILFS_SB(inode->i_sb)->s_nilfs;
693 down_read(&nilfs->ns_segctor_sem);
694 cno = nilfs->ns_cno - 1;
695 up_read(&nilfs->ns_segctor_sem);
7942b919
KS
696 if (copy_to_user(argp, &cno, sizeof(cno)))
697 return -EFAULT;
698 }
699 return 0;
700}
701
47eb6b9c
RK
702static int nilfs_ioctl_get_info(struct inode *inode, struct file *filp,
703 unsigned int cmd, void __user *argp,
83aca8f4 704 size_t membsz,
47eb6b9c
RK
705 ssize_t (*dofunc)(struct the_nilfs *,
706 __u64 *, int,
707 void *, size_t, size_t))
708
709{
710 struct the_nilfs *nilfs = NILFS_SB(inode->i_sb)->s_nilfs;
711 struct nilfs_argv argv;
712 int ret;
713
714 if (copy_from_user(&argv, argp, sizeof(argv)))
715 return -EFAULT;
716
003ff182 717 if (argv.v_size < membsz)
83aca8f4
RK
718 return -EINVAL;
719
47eb6b9c
RK
720 ret = nilfs_ioctl_wrap_copy(nilfs, &argv, _IOC_DIR(cmd), dofunc);
721 if (ret < 0)
722 return ret;
723
724 if (copy_to_user(argp, &argv, sizeof(argv)))
725 ret = -EFAULT;
726 return ret;
727}
728
7a946193 729long nilfs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
7942b919 730{
7a946193 731 struct inode *inode = filp->f_dentry->d_inode;
75323400 732 void __user *argp = (void __user *)arg;
7942b919
KS
733
734 switch (cmd) {
cde98f0f
RK
735 case FS_IOC_GETFLAGS:
736 return nilfs_ioctl_getflags(inode, argp);
737 case FS_IOC_SETFLAGS:
738 return nilfs_ioctl_setflags(inode, filp, argp);
739 case FS_IOC_GETVERSION:
740 return nilfs_ioctl_getversion(inode, argp);
7942b919
KS
741 case NILFS_IOCTL_CHANGE_CPMODE:
742 return nilfs_ioctl_change_cpmode(inode, filp, cmd, argp);
743 case NILFS_IOCTL_DELETE_CHECKPOINT:
744 return nilfs_ioctl_delete_checkpoint(inode, filp, cmd, argp);
745 case NILFS_IOCTL_GET_CPINFO:
47eb6b9c 746 return nilfs_ioctl_get_info(inode, filp, cmd, argp,
83aca8f4 747 sizeof(struct nilfs_cpinfo),
47eb6b9c 748 nilfs_ioctl_do_get_cpinfo);
7942b919
KS
749 case NILFS_IOCTL_GET_CPSTAT:
750 return nilfs_ioctl_get_cpstat(inode, filp, cmd, argp);
751 case NILFS_IOCTL_GET_SUINFO:
47eb6b9c 752 return nilfs_ioctl_get_info(inode, filp, cmd, argp,
83aca8f4 753 sizeof(struct nilfs_suinfo),
47eb6b9c 754 nilfs_ioctl_do_get_suinfo);
7942b919
KS
755 case NILFS_IOCTL_GET_SUSTAT:
756 return nilfs_ioctl_get_sustat(inode, filp, cmd, argp);
757 case NILFS_IOCTL_GET_VINFO:
47eb6b9c 758 return nilfs_ioctl_get_info(inode, filp, cmd, argp,
83aca8f4 759 sizeof(struct nilfs_vinfo),
47eb6b9c 760 nilfs_ioctl_do_get_vinfo);
7942b919
KS
761 case NILFS_IOCTL_GET_BDESCS:
762 return nilfs_ioctl_get_bdescs(inode, filp, cmd, argp);
763 case NILFS_IOCTL_CLEAN_SEGMENTS:
764 return nilfs_ioctl_clean_segments(inode, filp, cmd, argp);
7942b919
KS
765 case NILFS_IOCTL_SYNC:
766 return nilfs_ioctl_sync(inode, filp, cmd, argp);
767 default:
768 return -ENOTTY;
769 }
770}
828b1c50
RK
771
772#ifdef CONFIG_COMPAT
773long nilfs_compat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
774{
775 switch (cmd) {
776 case FS_IOC32_GETFLAGS:
777 cmd = FS_IOC_GETFLAGS;
778 break;
779 case FS_IOC32_SETFLAGS:
780 cmd = FS_IOC_SETFLAGS;
781 break;
782 case FS_IOC32_GETVERSION:
783 cmd = FS_IOC_GETVERSION;
784 break;
785 default:
786 return -ENOIOCTLCMD;
787 }
788 return nilfs_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
789}
790#endif