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/capability.h> /* capable() */
27 #include <linux/uaccess.h> /* copy_from_user(), copy_to_user() */
28 #include <linux/nilfs2_fs.h>
37 static int nilfs_ioctl_wrap_copy(struct the_nilfs *nilfs,
38 struct nilfs_argv *argv, int dir,
39 ssize_t (*dofunc)(struct the_nilfs *,
41 void *, size_t, size_t))
44 void __user *base = (void __user *)(unsigned long)argv->v_base;
45 size_t maxmembs, total, n;
50 if (argv->v_nmembs == 0)
53 if (argv->v_size > PAGE_SIZE)
56 buf = (void *)__get_free_pages(GFP_NOFS, 0);
59 maxmembs = PAGE_SIZE / argv->v_size;
64 for (i = 0; i < argv->v_nmembs; i += n) {
65 n = (argv->v_nmembs - i < maxmembs) ?
66 argv->v_nmembs - i : maxmembs;
67 if ((dir & _IOC_WRITE) &&
68 copy_from_user(buf, base + argv->v_size * i,
74 nr = dofunc(nilfs, &pos, argv->v_flags, buf, argv->v_size,
80 if ((dir & _IOC_READ) &&
81 copy_to_user(base + argv->v_size * i, buf,
92 argv->v_nmembs = total;
94 free_pages((unsigned long)buf, 0);
98 static int nilfs_ioctl_change_cpmode(struct inode *inode, struct file *filp,
99 unsigned int cmd, void __user *argp)
101 struct inode *cpfile = NILFS_SB(inode->i_sb)->s_nilfs->ns_cpfile;
102 struct nilfs_transaction_info ti;
103 struct nilfs_cpmode cpmode;
106 if (!capable(CAP_SYS_ADMIN))
108 if (copy_from_user(&cpmode, argp, sizeof(cpmode)))
111 nilfs_transaction_begin(inode->i_sb, &ti, 0);
112 ret = nilfs_cpfile_change_cpmode(
113 cpfile, cpmode.cm_cno, cpmode.cm_mode);
114 if (unlikely(ret < 0)) {
115 nilfs_transaction_abort(inode->i_sb);
118 nilfs_transaction_commit(inode->i_sb); /* never fails */
123 nilfs_ioctl_delete_checkpoint(struct inode *inode, struct file *filp,
124 unsigned int cmd, void __user *argp)
126 struct inode *cpfile = NILFS_SB(inode->i_sb)->s_nilfs->ns_cpfile;
127 struct nilfs_transaction_info ti;
131 if (!capable(CAP_SYS_ADMIN))
133 if (copy_from_user(&cno, argp, sizeof(cno)))
136 nilfs_transaction_begin(inode->i_sb, &ti, 0);
137 ret = nilfs_cpfile_delete_checkpoint(cpfile, cno);
138 if (unlikely(ret < 0)) {
139 nilfs_transaction_abort(inode->i_sb);
142 nilfs_transaction_commit(inode->i_sb); /* never fails */
147 nilfs_ioctl_do_get_cpinfo(struct the_nilfs *nilfs, __u64 *posp, int flags,
148 void *buf, size_t size, size_t nmembs)
150 return nilfs_cpfile_get_cpinfo(nilfs->ns_cpfile, posp, flags, buf,
154 static int nilfs_ioctl_get_cpinfo(struct inode *inode, struct file *filp,
155 unsigned int cmd, void __user *argp)
157 struct the_nilfs *nilfs = NILFS_SB(inode->i_sb)->s_nilfs;
158 struct nilfs_argv argv;
161 if (copy_from_user(&argv, argp, sizeof(argv)))
164 down_read(&nilfs->ns_segctor_sem);
165 ret = nilfs_ioctl_wrap_copy(nilfs, &argv, _IOC_DIR(cmd),
166 nilfs_ioctl_do_get_cpinfo);
167 up_read(&nilfs->ns_segctor_sem);
171 if (copy_to_user(argp, &argv, sizeof(argv)))
176 static int nilfs_ioctl_get_cpstat(struct inode *inode, struct file *filp,
177 unsigned int cmd, void __user *argp)
179 struct the_nilfs *nilfs = NILFS_SB(inode->i_sb)->s_nilfs;
180 struct nilfs_cpstat cpstat;
183 down_read(&nilfs->ns_segctor_sem);
184 ret = nilfs_cpfile_get_stat(nilfs->ns_cpfile, &cpstat);
185 up_read(&nilfs->ns_segctor_sem);
189 if (copy_to_user(argp, &cpstat, sizeof(cpstat)))
195 nilfs_ioctl_do_get_suinfo(struct the_nilfs *nilfs, __u64 *posp, int flags,
196 void *buf, size_t size, size_t nmembs)
198 return nilfs_sufile_get_suinfo(nilfs->ns_sufile, *posp, buf, nmembs);
201 static int nilfs_ioctl_get_suinfo(struct inode *inode, struct file *filp,
202 unsigned int cmd, void __user *argp)
204 struct the_nilfs *nilfs = NILFS_SB(inode->i_sb)->s_nilfs;
205 struct nilfs_argv argv;
208 if (copy_from_user(&argv, argp, sizeof(argv)))
211 down_read(&nilfs->ns_segctor_sem);
212 ret = nilfs_ioctl_wrap_copy(nilfs, &argv, _IOC_DIR(cmd),
213 nilfs_ioctl_do_get_suinfo);
214 up_read(&nilfs->ns_segctor_sem);
218 if (copy_to_user(argp, &argv, sizeof(argv)))
223 static int nilfs_ioctl_get_sustat(struct inode *inode, struct file *filp,
224 unsigned int cmd, void __user *argp)
226 struct the_nilfs *nilfs = NILFS_SB(inode->i_sb)->s_nilfs;
227 struct nilfs_sustat sustat;
230 down_read(&nilfs->ns_segctor_sem);
231 ret = nilfs_sufile_get_stat(nilfs->ns_sufile, &sustat);
232 up_read(&nilfs->ns_segctor_sem);
236 if (copy_to_user(argp, &sustat, sizeof(sustat)))
242 nilfs_ioctl_do_get_vinfo(struct the_nilfs *nilfs, __u64 *posp, int flags,
243 void *buf, size_t size, size_t nmembs)
245 return nilfs_dat_get_vinfo(nilfs_dat_inode(nilfs), buf, nmembs);
248 static int nilfs_ioctl_get_vinfo(struct inode *inode, struct file *filp,
249 unsigned int cmd, void __user *argp)
251 struct the_nilfs *nilfs = NILFS_SB(inode->i_sb)->s_nilfs;
252 struct nilfs_argv argv;
255 if (copy_from_user(&argv, argp, sizeof(argv)))
258 down_read(&nilfs->ns_segctor_sem);
259 ret = nilfs_ioctl_wrap_copy(nilfs, &argv, _IOC_DIR(cmd),
260 nilfs_ioctl_do_get_vinfo);
261 up_read(&nilfs->ns_segctor_sem);
265 if (copy_to_user(argp, &argv, sizeof(argv)))
271 nilfs_ioctl_do_get_bdescs(struct the_nilfs *nilfs, __u64 *posp, int flags,
272 void *buf, size_t size, size_t nmembs)
274 struct inode *dat = nilfs_dat_inode(nilfs);
275 struct nilfs_bmap *bmap = NILFS_I(dat)->i_bmap;
276 struct nilfs_bdesc *bdescs = buf;
279 for (i = 0; i < nmembs; i++) {
280 ret = nilfs_bmap_lookup_at_level(bmap,
282 bdescs[i].bd_level + 1,
283 &bdescs[i].bd_blocknr);
287 bdescs[i].bd_blocknr = 0;
293 static int nilfs_ioctl_get_bdescs(struct inode *inode, struct file *filp,
294 unsigned int cmd, void __user *argp)
296 struct the_nilfs *nilfs = NILFS_SB(inode->i_sb)->s_nilfs;
297 struct nilfs_argv argv;
300 if (copy_from_user(&argv, argp, sizeof(argv)))
303 down_read(&nilfs->ns_segctor_sem);
304 ret = nilfs_ioctl_wrap_copy(nilfs, &argv, _IOC_DIR(cmd),
305 nilfs_ioctl_do_get_bdescs);
306 up_read(&nilfs->ns_segctor_sem);
310 if (copy_to_user(argp, &argv, sizeof(argv)))
315 static int nilfs_ioctl_move_inode_block(struct inode *inode,
316 struct nilfs_vdesc *vdesc,
317 struct list_head *buffers)
319 struct buffer_head *bh;
322 if (vdesc->vd_flags == 0)
323 ret = nilfs_gccache_submit_read_data(
324 inode, vdesc->vd_offset, vdesc->vd_blocknr,
325 vdesc->vd_vblocknr, &bh);
327 ret = nilfs_gccache_submit_read_node(
328 inode, vdesc->vd_blocknr, vdesc->vd_vblocknr, &bh);
330 if (unlikely(ret < 0)) {
333 "%s: invalid virtual block address (%s): "
334 "ino=%llu, cno=%llu, offset=%llu, "
335 "blocknr=%llu, vblocknr=%llu\n",
336 __func__, vdesc->vd_flags ? "node" : "data",
337 (unsigned long long)vdesc->vd_ino,
338 (unsigned long long)vdesc->vd_cno,
339 (unsigned long long)vdesc->vd_offset,
340 (unsigned long long)vdesc->vd_blocknr,
341 (unsigned long long)vdesc->vd_vblocknr);
344 bh->b_private = vdesc;
345 list_add_tail(&bh->b_assoc_buffers, buffers);
350 nilfs_ioctl_do_move_blocks(struct the_nilfs *nilfs, __u64 *posp, int flags,
351 void *buf, size_t size, size_t nmembs)
354 struct nilfs_vdesc *vdesc;
355 struct buffer_head *bh, *n;
361 for (i = 0, vdesc = buf; i < nmembs; ) {
364 inode = nilfs_gc_iget(nilfs, ino, cno);
365 if (unlikely(inode == NULL)) {
370 ret = nilfs_ioctl_move_inode_block(inode, vdesc,
372 if (unlikely(ret < 0))
375 } while (++i < nmembs &&
376 vdesc->vd_ino == ino && vdesc->vd_cno == cno);
379 list_for_each_entry_safe(bh, n, &buffers, b_assoc_buffers) {
380 ret = nilfs_gccache_wait_and_mark_dirty(bh);
381 if (unlikely(ret < 0)) {
382 if (ret == -EEXIST) {
383 vdesc = bh->b_private;
385 "%s: conflicting %s buffer: "
386 "ino=%llu, cno=%llu, offset=%llu, "
387 "blocknr=%llu, vblocknr=%llu\n",
389 vdesc->vd_flags ? "node" : "data",
390 (unsigned long long)vdesc->vd_ino,
391 (unsigned long long)vdesc->vd_cno,
392 (unsigned long long)vdesc->vd_offset,
393 (unsigned long long)vdesc->vd_blocknr,
394 (unsigned long long)vdesc->vd_vblocknr);
398 list_del_init(&bh->b_assoc_buffers);
399 bh->b_private = NULL;
405 list_for_each_entry_safe(bh, n, &buffers, b_assoc_buffers) {
406 list_del_init(&bh->b_assoc_buffers);
407 bh->b_private = NULL;
413 static inline int nilfs_ioctl_move_blocks(struct the_nilfs *nilfs,
414 struct nilfs_argv *argv,
417 return nilfs_ioctl_wrap_copy(nilfs, argv, dir,
418 nilfs_ioctl_do_move_blocks);
422 nilfs_ioctl_do_delete_checkpoints(struct the_nilfs *nilfs, __u64 *posp,
423 int flags, void *buf, size_t size,
426 struct inode *cpfile = nilfs->ns_cpfile;
427 struct nilfs_period *periods = buf;
430 for (i = 0; i < nmembs; i++) {
431 ret = nilfs_cpfile_delete_checkpoints(
432 cpfile, periods[i].p_start, periods[i].p_end);
439 static inline int nilfs_ioctl_delete_checkpoints(struct the_nilfs *nilfs,
440 struct nilfs_argv *argv,
443 return nilfs_ioctl_wrap_copy(nilfs, argv, dir,
444 nilfs_ioctl_do_delete_checkpoints);
448 nilfs_ioctl_do_free_vblocknrs(struct the_nilfs *nilfs, __u64 *posp, int flags,
449 void *buf, size_t size, size_t nmembs)
451 int ret = nilfs_dat_freev(nilfs_dat_inode(nilfs), buf, nmembs);
453 return (ret < 0) ? ret : nmembs;
456 static inline int nilfs_ioctl_free_vblocknrs(struct the_nilfs *nilfs,
457 struct nilfs_argv *argv,
460 return nilfs_ioctl_wrap_copy(nilfs, argv, dir,
461 nilfs_ioctl_do_free_vblocknrs);
465 nilfs_ioctl_do_mark_blocks_dirty(struct the_nilfs *nilfs, __u64 *posp,
466 int flags, void *buf, size_t size,
469 struct inode *dat = nilfs_dat_inode(nilfs);
470 struct nilfs_bmap *bmap = NILFS_I(dat)->i_bmap;
471 struct nilfs_bdesc *bdescs = buf;
474 for (i = 0; i < nmembs; i++) {
475 /* XXX: use macro or inline func to check liveness */
476 ret = nilfs_bmap_lookup_at_level(bmap,
478 bdescs[i].bd_level + 1,
479 &bdescs[i].bd_blocknr);
483 bdescs[i].bd_blocknr = 0;
485 if (bdescs[i].bd_blocknr != bdescs[i].bd_oblocknr)
486 /* skip dead block */
488 if (bdescs[i].bd_level == 0) {
489 ret = nilfs_mdt_mark_block_dirty(dat,
490 bdescs[i].bd_offset);
492 WARN_ON(ret == -ENOENT);
496 ret = nilfs_bmap_mark(bmap, bdescs[i].bd_offset,
499 WARN_ON(ret == -ENOENT);
507 static inline int nilfs_ioctl_mark_blocks_dirty(struct the_nilfs *nilfs,
508 struct nilfs_argv *argv,
511 return nilfs_ioctl_wrap_copy(nilfs, argv, dir,
512 nilfs_ioctl_do_mark_blocks_dirty);
516 nilfs_ioctl_do_free_segments(struct the_nilfs *nilfs, __u64 *posp, int flags,
517 void *buf, size_t size, size_t nmembs)
519 struct nilfs_sb_info *sbi = nilfs_get_writer(nilfs);
524 ret = nilfs_segctor_add_segments_to_be_freed(
525 NILFS_SC(sbi), buf, nmembs);
526 nilfs_put_writer(nilfs);
528 return (ret < 0) ? ret : nmembs;
531 static inline int nilfs_ioctl_free_segments(struct the_nilfs *nilfs,
532 struct nilfs_argv *argv,
535 return nilfs_ioctl_wrap_copy(nilfs, argv, dir,
536 nilfs_ioctl_do_free_segments);
539 int nilfs_ioctl_prepare_clean_segments(struct the_nilfs *nilfs,
542 struct nilfs_argv argv[5];
546 if (copy_from_user(argv, argp, sizeof(argv)))
550 ret = nilfs_ioctl_move_blocks(nilfs, &argv[0], dir);
552 msg = "cannot read source blocks";
555 ret = nilfs_ioctl_delete_checkpoints(nilfs, &argv[1], dir);
558 * can safely abort because checkpoints can be removed
561 msg = "cannot delete checkpoints";
564 ret = nilfs_ioctl_free_vblocknrs(nilfs, &argv[2], dir);
567 * can safely abort because DAT file is updated atomically
568 * using a copy-on-write technique.
570 msg = "cannot delete virtual blocks from DAT file";
573 ret = nilfs_ioctl_mark_blocks_dirty(nilfs, &argv[3], dir);
576 * can safely abort because the operation is nondestructive.
578 msg = "cannot mark copying blocks dirty";
581 ret = nilfs_ioctl_free_segments(nilfs, &argv[4], dir);
584 * can safely abort because this operation is atomic.
586 msg = "cannot set segments to be freed";
592 nilfs_remove_all_gcinode(nilfs);
593 printk(KERN_ERR "NILFS: GC failed during preparation: %s: err=%d\n",
598 static int nilfs_ioctl_clean_segments(struct inode *inode, struct file *filp,
599 unsigned int cmd, void __user *argp)
601 if (!capable(CAP_SYS_ADMIN))
603 return nilfs_clean_segments(inode->i_sb, argp);
606 static int nilfs_ioctl_sync(struct inode *inode, struct file *filp,
607 unsigned int cmd, void __user *argp)
612 ret = nilfs_construct_segment(inode->i_sb);
617 cno = NILFS_SB(inode->i_sb)->s_nilfs->ns_cno - 1;
618 if (copy_to_user(argp, &cno, sizeof(cno)))
624 long nilfs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
626 struct inode *inode = filp->f_dentry->d_inode;
627 void __user *argp = (void * __user *)arg;
630 case NILFS_IOCTL_CHANGE_CPMODE:
631 return nilfs_ioctl_change_cpmode(inode, filp, cmd, argp);
632 case NILFS_IOCTL_DELETE_CHECKPOINT:
633 return nilfs_ioctl_delete_checkpoint(inode, filp, cmd, argp);
634 case NILFS_IOCTL_GET_CPINFO:
635 return nilfs_ioctl_get_cpinfo(inode, filp, cmd, argp);
636 case NILFS_IOCTL_GET_CPSTAT:
637 return nilfs_ioctl_get_cpstat(inode, filp, cmd, argp);
638 case NILFS_IOCTL_GET_SUINFO:
639 return nilfs_ioctl_get_suinfo(inode, filp, cmd, argp);
640 case NILFS_IOCTL_GET_SUSTAT:
641 return nilfs_ioctl_get_sustat(inode, filp, cmd, argp);
642 case NILFS_IOCTL_GET_VINFO:
643 /* XXX: rename to ??? */
644 return nilfs_ioctl_get_vinfo(inode, filp, cmd, argp);
645 case NILFS_IOCTL_GET_BDESCS:
646 return nilfs_ioctl_get_bdescs(inode, filp, cmd, argp);
647 case NILFS_IOCTL_CLEAN_SEGMENTS:
648 return nilfs_ioctl_clean_segments(inode, filp, cmd, argp);
649 case NILFS_IOCTL_SYNC:
650 return nilfs_ioctl_sync(inode, filp, cmd, argp);