2 * ioctl.c - NILFS ioctl operations.
4 * Copyright (C) 2007, 2008 Nippon Telegraph and Telephone Corporation.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * Written by Koji Sato <koji@osrg.net>.
24 #include <linux/wait.h>
25 #include <linux/smp_lock.h> /* lock_kernel(), unlock_kernel() */
26 #include <linux/slab.h>
27 #include <linux/capability.h> /* capable() */
28 #include <linux/uaccess.h> /* copy_from_user(), copy_to_user() */
29 #include <linux/vmalloc.h>
30 #include <linux/mount.h> /* mnt_want_write(), mnt_drop_write() */
31 #include <linux/nilfs2_fs.h>
40 static int nilfs_ioctl_wrap_copy(struct the_nilfs
*nilfs
,
41 struct nilfs_argv
*argv
, int dir
,
42 ssize_t (*dofunc
)(struct the_nilfs
*,
44 void *, size_t, size_t))
47 void __user
*base
= (void __user
*)(unsigned long)argv
->v_base
;
48 size_t maxmembs
, total
, n
;
53 if (argv
->v_nmembs
== 0)
56 if (argv
->v_size
> PAGE_SIZE
)
59 buf
= (void *)__get_free_pages(GFP_NOFS
, 0);
62 maxmembs
= PAGE_SIZE
/ argv
->v_size
;
67 for (i
= 0; i
< argv
->v_nmembs
; i
+= n
) {
68 n
= (argv
->v_nmembs
- i
< maxmembs
) ?
69 argv
->v_nmembs
- i
: maxmembs
;
70 if ((dir
& _IOC_WRITE
) &&
71 copy_from_user(buf
, base
+ argv
->v_size
* i
,
77 nr
= dofunc(nilfs
, &pos
, argv
->v_flags
, buf
, argv
->v_size
,
83 if ((dir
& _IOC_READ
) &&
84 copy_to_user(base
+ argv
->v_size
* i
, buf
,
95 argv
->v_nmembs
= total
;
97 free_pages((unsigned long)buf
, 0);
101 static int nilfs_ioctl_change_cpmode(struct inode
*inode
, struct file
*filp
,
102 unsigned int cmd
, void __user
*argp
)
104 struct the_nilfs
*nilfs
= NILFS_SB(inode
->i_sb
)->s_nilfs
;
105 struct inode
*cpfile
= nilfs
->ns_cpfile
;
106 struct nilfs_transaction_info ti
;
107 struct nilfs_cpmode cpmode
;
110 if (!capable(CAP_SYS_ADMIN
))
113 ret
= mnt_want_write(filp
->f_path
.mnt
);
118 if (copy_from_user(&cpmode
, argp
, sizeof(cpmode
)))
121 mutex_lock(&nilfs
->ns_mount_mutex
);
123 nilfs_transaction_begin(inode
->i_sb
, &ti
, 0);
124 ret
= nilfs_cpfile_change_cpmode(
125 cpfile
, cpmode
.cm_cno
, cpmode
.cm_mode
);
126 if (unlikely(ret
< 0))
127 nilfs_transaction_abort(inode
->i_sb
);
129 nilfs_transaction_commit(inode
->i_sb
); /* never fails */
131 mutex_unlock(&nilfs
->ns_mount_mutex
);
133 mnt_drop_write(filp
->f_path
.mnt
);
138 nilfs_ioctl_delete_checkpoint(struct inode
*inode
, struct file
*filp
,
139 unsigned int cmd
, void __user
*argp
)
141 struct inode
*cpfile
= NILFS_SB(inode
->i_sb
)->s_nilfs
->ns_cpfile
;
142 struct nilfs_transaction_info ti
;
146 if (!capable(CAP_SYS_ADMIN
))
149 ret
= mnt_want_write(filp
->f_path
.mnt
);
154 if (copy_from_user(&cno
, argp
, sizeof(cno
)))
157 nilfs_transaction_begin(inode
->i_sb
, &ti
, 0);
158 ret
= nilfs_cpfile_delete_checkpoint(cpfile
, cno
);
159 if (unlikely(ret
< 0))
160 nilfs_transaction_abort(inode
->i_sb
);
162 nilfs_transaction_commit(inode
->i_sb
); /* never fails */
164 mnt_drop_write(filp
->f_path
.mnt
);
169 nilfs_ioctl_do_get_cpinfo(struct the_nilfs
*nilfs
, __u64
*posp
, int flags
,
170 void *buf
, size_t size
, size_t nmembs
)
174 down_read(&nilfs
->ns_segctor_sem
);
175 ret
= nilfs_cpfile_get_cpinfo(nilfs
->ns_cpfile
, posp
, flags
, buf
,
177 up_read(&nilfs
->ns_segctor_sem
);
181 static int nilfs_ioctl_get_cpstat(struct inode
*inode
, struct file
*filp
,
182 unsigned int cmd
, void __user
*argp
)
184 struct the_nilfs
*nilfs
= NILFS_SB(inode
->i_sb
)->s_nilfs
;
185 struct nilfs_cpstat cpstat
;
188 down_read(&nilfs
->ns_segctor_sem
);
189 ret
= nilfs_cpfile_get_stat(nilfs
->ns_cpfile
, &cpstat
);
190 up_read(&nilfs
->ns_segctor_sem
);
194 if (copy_to_user(argp
, &cpstat
, sizeof(cpstat
)))
200 nilfs_ioctl_do_get_suinfo(struct the_nilfs
*nilfs
, __u64
*posp
, int flags
,
201 void *buf
, size_t size
, size_t nmembs
)
205 down_read(&nilfs
->ns_segctor_sem
);
206 ret
= nilfs_sufile_get_suinfo(nilfs
->ns_sufile
, *posp
, buf
, size
,
208 up_read(&nilfs
->ns_segctor_sem
);
212 static int nilfs_ioctl_get_sustat(struct inode
*inode
, struct file
*filp
,
213 unsigned int cmd
, void __user
*argp
)
215 struct the_nilfs
*nilfs
= NILFS_SB(inode
->i_sb
)->s_nilfs
;
216 struct nilfs_sustat sustat
;
219 down_read(&nilfs
->ns_segctor_sem
);
220 ret
= nilfs_sufile_get_stat(nilfs
->ns_sufile
, &sustat
);
221 up_read(&nilfs
->ns_segctor_sem
);
225 if (copy_to_user(argp
, &sustat
, sizeof(sustat
)))
231 nilfs_ioctl_do_get_vinfo(struct the_nilfs
*nilfs
, __u64
*posp
, int flags
,
232 void *buf
, size_t size
, size_t nmembs
)
236 down_read(&nilfs
->ns_segctor_sem
);
237 ret
= nilfs_dat_get_vinfo(nilfs_dat_inode(nilfs
), buf
, size
, nmembs
);
238 up_read(&nilfs
->ns_segctor_sem
);
243 nilfs_ioctl_do_get_bdescs(struct the_nilfs
*nilfs
, __u64
*posp
, int flags
,
244 void *buf
, size_t size
, size_t nmembs
)
246 struct inode
*dat
= nilfs_dat_inode(nilfs
);
247 struct nilfs_bmap
*bmap
= NILFS_I(dat
)->i_bmap
;
248 struct nilfs_bdesc
*bdescs
= buf
;
251 down_read(&nilfs
->ns_segctor_sem
);
252 for (i
= 0; i
< nmembs
; i
++) {
253 ret
= nilfs_bmap_lookup_at_level(bmap
,
255 bdescs
[i
].bd_level
+ 1,
256 &bdescs
[i
].bd_blocknr
);
258 if (ret
!= -ENOENT
) {
259 up_read(&nilfs
->ns_segctor_sem
);
262 bdescs
[i
].bd_blocknr
= 0;
265 up_read(&nilfs
->ns_segctor_sem
);
269 static int nilfs_ioctl_get_bdescs(struct inode
*inode
, struct file
*filp
,
270 unsigned int cmd
, void __user
*argp
)
272 struct the_nilfs
*nilfs
= NILFS_SB(inode
->i_sb
)->s_nilfs
;
273 struct nilfs_argv argv
;
276 if (copy_from_user(&argv
, argp
, sizeof(argv
)))
279 if (argv
.v_size
!= sizeof(struct nilfs_bdesc
))
282 ret
= nilfs_ioctl_wrap_copy(nilfs
, &argv
, _IOC_DIR(cmd
),
283 nilfs_ioctl_do_get_bdescs
);
287 if (copy_to_user(argp
, &argv
, sizeof(argv
)))
292 static int nilfs_ioctl_move_inode_block(struct inode
*inode
,
293 struct nilfs_vdesc
*vdesc
,
294 struct list_head
*buffers
)
296 struct buffer_head
*bh
;
299 if (vdesc
->vd_flags
== 0)
300 ret
= nilfs_gccache_submit_read_data(
301 inode
, vdesc
->vd_offset
, vdesc
->vd_blocknr
,
302 vdesc
->vd_vblocknr
, &bh
);
304 ret
= nilfs_gccache_submit_read_node(
305 inode
, vdesc
->vd_blocknr
, vdesc
->vd_vblocknr
, &bh
);
307 if (unlikely(ret
< 0)) {
310 "%s: invalid virtual block address (%s): "
311 "ino=%llu, cno=%llu, offset=%llu, "
312 "blocknr=%llu, vblocknr=%llu\n",
313 __func__
, vdesc
->vd_flags
? "node" : "data",
314 (unsigned long long)vdesc
->vd_ino
,
315 (unsigned long long)vdesc
->vd_cno
,
316 (unsigned long long)vdesc
->vd_offset
,
317 (unsigned long long)vdesc
->vd_blocknr
,
318 (unsigned long long)vdesc
->vd_vblocknr
);
321 if (unlikely(!list_empty(&bh
->b_assoc_buffers
))) {
322 printk(KERN_CRIT
"%s: conflicting %s buffer: ino=%llu, "
323 "cno=%llu, offset=%llu, blocknr=%llu, vblocknr=%llu\n",
324 __func__
, vdesc
->vd_flags
? "node" : "data",
325 (unsigned long long)vdesc
->vd_ino
,
326 (unsigned long long)vdesc
->vd_cno
,
327 (unsigned long long)vdesc
->vd_offset
,
328 (unsigned long long)vdesc
->vd_blocknr
,
329 (unsigned long long)vdesc
->vd_vblocknr
);
333 list_add_tail(&bh
->b_assoc_buffers
, buffers
);
337 static int nilfs_ioctl_move_blocks(struct the_nilfs
*nilfs
,
338 struct nilfs_argv
*argv
, void *buf
)
340 size_t nmembs
= argv
->v_nmembs
;
342 struct nilfs_vdesc
*vdesc
;
343 struct buffer_head
*bh
, *n
;
349 for (i
= 0, vdesc
= buf
; i
< nmembs
; ) {
352 inode
= nilfs_gc_iget(nilfs
, ino
, cno
);
353 if (unlikely(inode
== NULL
)) {
358 ret
= nilfs_ioctl_move_inode_block(inode
, vdesc
,
360 if (unlikely(ret
< 0))
363 } while (++i
< nmembs
&&
364 vdesc
->vd_ino
== ino
&& vdesc
->vd_cno
== cno
);
367 list_for_each_entry_safe(bh
, n
, &buffers
, b_assoc_buffers
) {
368 ret
= nilfs_gccache_wait_and_mark_dirty(bh
);
369 if (unlikely(ret
< 0)) {
370 WARN_ON(ret
== -EEXIST
);
373 list_del_init(&bh
->b_assoc_buffers
);
379 list_for_each_entry_safe(bh
, n
, &buffers
, b_assoc_buffers
) {
380 list_del_init(&bh
->b_assoc_buffers
);
386 static int nilfs_ioctl_delete_checkpoints(struct the_nilfs
*nilfs
,
387 struct nilfs_argv
*argv
, void *buf
)
389 size_t nmembs
= argv
->v_nmembs
;
390 struct inode
*cpfile
= nilfs
->ns_cpfile
;
391 struct nilfs_period
*periods
= buf
;
394 for (i
= 0; i
< nmembs
; i
++) {
395 ret
= nilfs_cpfile_delete_checkpoints(
396 cpfile
, periods
[i
].p_start
, periods
[i
].p_end
);
403 static int nilfs_ioctl_free_vblocknrs(struct the_nilfs
*nilfs
,
404 struct nilfs_argv
*argv
, void *buf
)
406 size_t nmembs
= argv
->v_nmembs
;
409 ret
= nilfs_dat_freev(nilfs_dat_inode(nilfs
), buf
, nmembs
);
411 return (ret
< 0) ? ret
: nmembs
;
414 static int nilfs_ioctl_mark_blocks_dirty(struct the_nilfs
*nilfs
,
415 struct nilfs_argv
*argv
, void *buf
)
417 size_t nmembs
= argv
->v_nmembs
;
418 struct inode
*dat
= nilfs_dat_inode(nilfs
);
419 struct nilfs_bmap
*bmap
= NILFS_I(dat
)->i_bmap
;
420 struct nilfs_bdesc
*bdescs
= buf
;
423 for (i
= 0; i
< nmembs
; i
++) {
424 /* XXX: use macro or inline func to check liveness */
425 ret
= nilfs_bmap_lookup_at_level(bmap
,
427 bdescs
[i
].bd_level
+ 1,
428 &bdescs
[i
].bd_blocknr
);
432 bdescs
[i
].bd_blocknr
= 0;
434 if (bdescs
[i
].bd_blocknr
!= bdescs
[i
].bd_oblocknr
)
435 /* skip dead block */
437 if (bdescs
[i
].bd_level
== 0) {
438 ret
= nilfs_mdt_mark_block_dirty(dat
,
439 bdescs
[i
].bd_offset
);
441 WARN_ON(ret
== -ENOENT
);
445 ret
= nilfs_bmap_mark(bmap
, bdescs
[i
].bd_offset
,
448 WARN_ON(ret
== -ENOENT
);
456 int nilfs_ioctl_prepare_clean_segments(struct the_nilfs
*nilfs
,
457 struct nilfs_argv
*argv
, void **kbufs
)
462 ret
= nilfs_ioctl_delete_checkpoints(nilfs
, &argv
[1], kbufs
[1]);
465 * can safely abort because checkpoints can be removed
468 msg
= "cannot delete checkpoints";
471 ret
= nilfs_ioctl_free_vblocknrs(nilfs
, &argv
[2], kbufs
[2]);
474 * can safely abort because DAT file is updated atomically
475 * using a copy-on-write technique.
477 msg
= "cannot delete virtual blocks from DAT file";
480 ret
= nilfs_ioctl_mark_blocks_dirty(nilfs
, &argv
[3], kbufs
[3]);
483 * can safely abort because the operation is nondestructive.
485 msg
= "cannot mark copying blocks dirty";
491 printk(KERN_ERR
"NILFS: GC failed during preparation: %s: err=%d\n",
496 static int nilfs_ioctl_clean_segments(struct inode
*inode
, struct file
*filp
,
497 unsigned int cmd
, void __user
*argp
)
499 struct nilfs_argv argv
[5];
500 static const size_t argsz
[5] = {
501 sizeof(struct nilfs_vdesc
),
502 sizeof(struct nilfs_period
),
504 sizeof(struct nilfs_bdesc
),
509 struct the_nilfs
*nilfs
;
513 if (!capable(CAP_SYS_ADMIN
))
516 ret
= mnt_want_write(filp
->f_path
.mnt
);
521 if (copy_from_user(argv
, argp
, sizeof(argv
)))
525 nsegs
= argv
[4].v_nmembs
;
526 if (argv
[4].v_size
!= argsz
[4])
530 * argv[4] points to segment numbers this ioctl cleans. We
531 * use kmalloc() for its buffer because memory used for the
532 * segment numbers is enough small.
534 kbufs
[4] = memdup_user((void __user
*)(unsigned long)argv
[4].v_base
,
535 nsegs
* sizeof(__u64
));
536 if (IS_ERR(kbufs
[4])) {
537 ret
= PTR_ERR(kbufs
[4]);
540 nilfs
= NILFS_SB(inode
->i_sb
)->s_nilfs
;
542 for (n
= 0; n
< 4; n
++) {
544 if (argv
[n
].v_size
!= argsz
[n
])
547 if (argv
[n
].v_nmembs
> nsegs
* nilfs
->ns_blocks_per_segment
)
550 len
= argv
[n
].v_size
* argv
[n
].v_nmembs
;
551 base
= (void __user
*)(unsigned long)argv
[n
].v_base
;
557 kbufs
[n
] = vmalloc(len
);
562 if (copy_from_user(kbufs
[n
], base
, len
)) {
570 * nilfs_ioctl_move_blocks() will call nilfs_gc_iget(),
571 * which will operates an inode list without blocking.
572 * To protect the list from concurrent operations,
573 * nilfs_ioctl_move_blocks should be atomic operation.
575 if (test_and_set_bit(THE_NILFS_GC_RUNNING
, &nilfs
->ns_flags
)) {
580 ret
= nilfs_ioctl_move_blocks(nilfs
, &argv
[0], kbufs
[0]);
582 printk(KERN_ERR
"NILFS: GC failed during preparation: "
583 "cannot read source blocks: err=%d\n", ret
);
585 ret
= nilfs_clean_segments(inode
->i_sb
, argv
, kbufs
);
588 nilfs_remove_all_gcinode(nilfs
);
589 clear_nilfs_gc_running(nilfs
);
596 mnt_drop_write(filp
->f_path
.mnt
);
600 static int nilfs_ioctl_sync(struct inode
*inode
, struct file
*filp
,
601 unsigned int cmd
, void __user
*argp
)
605 struct the_nilfs
*nilfs
;
607 ret
= nilfs_construct_segment(inode
->i_sb
);
612 nilfs
= NILFS_SB(inode
->i_sb
)->s_nilfs
;
613 down_read(&nilfs
->ns_segctor_sem
);
614 cno
= nilfs
->ns_cno
- 1;
615 up_read(&nilfs
->ns_segctor_sem
);
616 if (copy_to_user(argp
, &cno
, sizeof(cno
)))
622 static int nilfs_ioctl_get_info(struct inode
*inode
, struct file
*filp
,
623 unsigned int cmd
, void __user
*argp
,
625 ssize_t (*dofunc
)(struct the_nilfs
*,
627 void *, size_t, size_t))
630 struct the_nilfs
*nilfs
= NILFS_SB(inode
->i_sb
)->s_nilfs
;
631 struct nilfs_argv argv
;
634 if (copy_from_user(&argv
, argp
, sizeof(argv
)))
637 if (argv
.v_size
< membsz
)
640 ret
= nilfs_ioctl_wrap_copy(nilfs
, &argv
, _IOC_DIR(cmd
), dofunc
);
644 if (copy_to_user(argp
, &argv
, sizeof(argv
)))
649 long nilfs_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
651 struct inode
*inode
= filp
->f_dentry
->d_inode
;
652 void __user
*argp
= (void __user
*)arg
;
655 case NILFS_IOCTL_CHANGE_CPMODE
:
656 return nilfs_ioctl_change_cpmode(inode
, filp
, cmd
, argp
);
657 case NILFS_IOCTL_DELETE_CHECKPOINT
:
658 return nilfs_ioctl_delete_checkpoint(inode
, filp
, cmd
, argp
);
659 case NILFS_IOCTL_GET_CPINFO
:
660 return nilfs_ioctl_get_info(inode
, filp
, cmd
, argp
,
661 sizeof(struct nilfs_cpinfo
),
662 nilfs_ioctl_do_get_cpinfo
);
663 case NILFS_IOCTL_GET_CPSTAT
:
664 return nilfs_ioctl_get_cpstat(inode
, filp
, cmd
, argp
);
665 case NILFS_IOCTL_GET_SUINFO
:
666 return nilfs_ioctl_get_info(inode
, filp
, cmd
, argp
,
667 sizeof(struct nilfs_suinfo
),
668 nilfs_ioctl_do_get_suinfo
);
669 case NILFS_IOCTL_GET_SUSTAT
:
670 return nilfs_ioctl_get_sustat(inode
, filp
, cmd
, argp
);
671 case NILFS_IOCTL_GET_VINFO
:
672 return nilfs_ioctl_get_info(inode
, filp
, cmd
, argp
,
673 sizeof(struct nilfs_vinfo
),
674 nilfs_ioctl_do_get_vinfo
);
675 case NILFS_IOCTL_GET_BDESCS
:
676 return nilfs_ioctl_get_bdescs(inode
, filp
, cmd
, argp
);
677 case NILFS_IOCTL_CLEAN_SEGMENTS
:
678 return nilfs_ioctl_clean_segments(inode
, filp
, cmd
, argp
);
679 case NILFS_IOCTL_SYNC
:
680 return nilfs_ioctl_sync(inode
, filp
, cmd
, argp
);