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
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/vmalloc.h>
29 #include <linux/nilfs2_fs.h>
38 static int nilfs_ioctl_wrap_copy(struct the_nilfs *nilfs,
39 struct nilfs_argv *argv, int dir,
40 ssize_t (*dofunc)(struct the_nilfs *,
42 void *, size_t, size_t))
45 void __user *base = (void __user *)(unsigned long)argv->v_base;
46 size_t maxmembs, total, n;
51 if (argv->v_nmembs == 0)
54 if (argv->v_size > PAGE_SIZE)
57 buf = (void *)__get_free_pages(GFP_NOFS, 0);
60 maxmembs = PAGE_SIZE / argv->v_size;
65 for (i = 0; i < argv->v_nmembs; i += n) {
66 n = (argv->v_nmembs - i < maxmembs) ?
67 argv->v_nmembs - i : maxmembs;
68 if ((dir & _IOC_WRITE) &&
69 copy_from_user(buf, base + argv->v_size * i,
75 nr = dofunc(nilfs, &pos, argv->v_flags, buf, argv->v_size,
81 if ((dir & _IOC_READ) &&
82 copy_to_user(base + argv->v_size * i, buf,
93 argv->v_nmembs = total;
95 free_pages((unsigned long)buf, 0);
99 static int nilfs_ioctl_change_cpmode(struct inode *inode, struct file *filp,
100 unsigned int cmd, void __user *argp)
102 struct inode *cpfile = NILFS_SB(inode->i_sb)->s_nilfs->ns_cpfile;
103 struct nilfs_transaction_info ti;
104 struct nilfs_cpmode cpmode;
107 if (!capable(CAP_SYS_ADMIN))
109 if (copy_from_user(&cpmode, argp, sizeof(cpmode)))
112 nilfs_transaction_begin(inode->i_sb, &ti, 0);
113 ret = nilfs_cpfile_change_cpmode(
114 cpfile, cpmode.cm_cno, cpmode.cm_mode);
115 if (unlikely(ret < 0)) {
116 nilfs_transaction_abort(inode->i_sb);
119 nilfs_transaction_commit(inode->i_sb); /* never fails */
124 nilfs_ioctl_delete_checkpoint(struct inode *inode, struct file *filp,
125 unsigned int cmd, void __user *argp)
127 struct inode *cpfile = NILFS_SB(inode->i_sb)->s_nilfs->ns_cpfile;
128 struct nilfs_transaction_info ti;
132 if (!capable(CAP_SYS_ADMIN))
134 if (copy_from_user(&cno, argp, sizeof(cno)))
137 nilfs_transaction_begin(inode->i_sb, &ti, 0);
138 ret = nilfs_cpfile_delete_checkpoint(cpfile, cno);
139 if (unlikely(ret < 0)) {
140 nilfs_transaction_abort(inode->i_sb);
143 nilfs_transaction_commit(inode->i_sb); /* never fails */
148 nilfs_ioctl_do_get_cpinfo(struct the_nilfs *nilfs, __u64 *posp, int flags,
149 void *buf, size_t size, size_t nmembs)
153 down_read(&nilfs->ns_segctor_sem);
154 ret = nilfs_cpfile_get_cpinfo(nilfs->ns_cpfile, posp, flags, buf,
156 up_read(&nilfs->ns_segctor_sem);
160 static int nilfs_ioctl_get_cpstat(struct inode *inode, struct file *filp,
161 unsigned int cmd, void __user *argp)
163 struct the_nilfs *nilfs = NILFS_SB(inode->i_sb)->s_nilfs;
164 struct nilfs_cpstat cpstat;
167 down_read(&nilfs->ns_segctor_sem);
168 ret = nilfs_cpfile_get_stat(nilfs->ns_cpfile, &cpstat);
169 up_read(&nilfs->ns_segctor_sem);
173 if (copy_to_user(argp, &cpstat, sizeof(cpstat)))
179 nilfs_ioctl_do_get_suinfo(struct the_nilfs *nilfs, __u64 *posp, int flags,
180 void *buf, size_t size, size_t nmembs)
184 down_read(&nilfs->ns_segctor_sem);
185 ret = nilfs_sufile_get_suinfo(nilfs->ns_sufile, *posp, buf, nmembs);
186 up_read(&nilfs->ns_segctor_sem);
190 static int nilfs_ioctl_get_sustat(struct inode *inode, struct file *filp,
191 unsigned int cmd, void __user *argp)
193 struct the_nilfs *nilfs = NILFS_SB(inode->i_sb)->s_nilfs;
194 struct nilfs_sustat sustat;
197 down_read(&nilfs->ns_segctor_sem);
198 ret = nilfs_sufile_get_stat(nilfs->ns_sufile, &sustat);
199 up_read(&nilfs->ns_segctor_sem);
203 if (copy_to_user(argp, &sustat, sizeof(sustat)))
209 nilfs_ioctl_do_get_vinfo(struct the_nilfs *nilfs, __u64 *posp, int flags,
210 void *buf, size_t size, size_t nmembs)
214 down_read(&nilfs->ns_segctor_sem);
215 ret = nilfs_dat_get_vinfo(nilfs_dat_inode(nilfs), buf, nmembs);
216 up_read(&nilfs->ns_segctor_sem);
221 nilfs_ioctl_do_get_bdescs(struct the_nilfs *nilfs, __u64 *posp, int flags,
222 void *buf, size_t size, size_t nmembs)
224 struct inode *dat = nilfs_dat_inode(nilfs);
225 struct nilfs_bmap *bmap = NILFS_I(dat)->i_bmap;
226 struct nilfs_bdesc *bdescs = buf;
229 down_read(&nilfs->ns_segctor_sem);
230 for (i = 0; i < nmembs; i++) {
231 ret = nilfs_bmap_lookup_at_level(bmap,
233 bdescs[i].bd_level + 1,
234 &bdescs[i].bd_blocknr);
236 if (ret != -ENOENT) {
237 up_read(&nilfs->ns_segctor_sem);
240 bdescs[i].bd_blocknr = 0;
243 up_read(&nilfs->ns_segctor_sem);
247 static int nilfs_ioctl_get_bdescs(struct inode *inode, struct file *filp,
248 unsigned int cmd, void __user *argp)
250 struct the_nilfs *nilfs = NILFS_SB(inode->i_sb)->s_nilfs;
251 struct nilfs_argv argv;
254 if (copy_from_user(&argv, argp, sizeof(argv)))
257 if (argv.v_size != sizeof(struct nilfs_bdesc))
260 ret = nilfs_ioctl_wrap_copy(nilfs, &argv, _IOC_DIR(cmd),
261 nilfs_ioctl_do_get_bdescs);
265 if (copy_to_user(argp, &argv, sizeof(argv)))
270 static int nilfs_ioctl_move_inode_block(struct inode *inode,
271 struct nilfs_vdesc *vdesc,
272 struct list_head *buffers)
274 struct buffer_head *bh;
277 if (vdesc->vd_flags == 0)
278 ret = nilfs_gccache_submit_read_data(
279 inode, vdesc->vd_offset, vdesc->vd_blocknr,
280 vdesc->vd_vblocknr, &bh);
282 ret = nilfs_gccache_submit_read_node(
283 inode, vdesc->vd_blocknr, vdesc->vd_vblocknr, &bh);
285 if (unlikely(ret < 0)) {
288 "%s: invalid virtual block address (%s): "
289 "ino=%llu, cno=%llu, offset=%llu, "
290 "blocknr=%llu, vblocknr=%llu\n",
291 __func__, vdesc->vd_flags ? "node" : "data",
292 (unsigned long long)vdesc->vd_ino,
293 (unsigned long long)vdesc->vd_cno,
294 (unsigned long long)vdesc->vd_offset,
295 (unsigned long long)vdesc->vd_blocknr,
296 (unsigned long long)vdesc->vd_vblocknr);
299 bh->b_private = vdesc;
300 list_add_tail(&bh->b_assoc_buffers, buffers);
304 static int nilfs_ioctl_move_blocks(struct the_nilfs *nilfs,
305 struct nilfs_argv *argv, void *buf)
307 size_t nmembs = argv->v_nmembs;
309 struct nilfs_vdesc *vdesc;
310 struct buffer_head *bh, *n;
316 for (i = 0, vdesc = buf; i < nmembs; ) {
319 inode = nilfs_gc_iget(nilfs, ino, cno);
320 if (unlikely(inode == NULL)) {
325 ret = nilfs_ioctl_move_inode_block(inode, vdesc,
327 if (unlikely(ret < 0))
330 } while (++i < nmembs &&
331 vdesc->vd_ino == ino && vdesc->vd_cno == cno);
334 list_for_each_entry_safe(bh, n, &buffers, b_assoc_buffers) {
335 ret = nilfs_gccache_wait_and_mark_dirty(bh);
336 if (unlikely(ret < 0)) {
337 if (ret == -EEXIST) {
338 vdesc = bh->b_private;
340 "%s: conflicting %s buffer: "
341 "ino=%llu, cno=%llu, offset=%llu, "
342 "blocknr=%llu, vblocknr=%llu\n",
344 vdesc->vd_flags ? "node" : "data",
345 (unsigned long long)vdesc->vd_ino,
346 (unsigned long long)vdesc->vd_cno,
347 (unsigned long long)vdesc->vd_offset,
348 (unsigned long long)vdesc->vd_blocknr,
349 (unsigned long long)vdesc->vd_vblocknr);
353 list_del_init(&bh->b_assoc_buffers);
354 bh->b_private = NULL;
360 list_for_each_entry_safe(bh, n, &buffers, b_assoc_buffers) {
361 list_del_init(&bh->b_assoc_buffers);
362 bh->b_private = NULL;
368 static int nilfs_ioctl_delete_checkpoints(struct the_nilfs *nilfs,
369 struct nilfs_argv *argv, void *buf)
371 size_t nmembs = argv->v_nmembs;
372 struct inode *cpfile = nilfs->ns_cpfile;
373 struct nilfs_period *periods = buf;
376 for (i = 0; i < nmembs; i++) {
377 ret = nilfs_cpfile_delete_checkpoints(
378 cpfile, periods[i].p_start, periods[i].p_end);
385 static int nilfs_ioctl_free_vblocknrs(struct the_nilfs *nilfs,
386 struct nilfs_argv *argv, void *buf)
388 size_t nmembs = argv->v_nmembs;
391 ret = nilfs_dat_freev(nilfs_dat_inode(nilfs), buf, nmembs);
393 return (ret < 0) ? ret : nmembs;
396 static int nilfs_ioctl_mark_blocks_dirty(struct the_nilfs *nilfs,
397 struct nilfs_argv *argv, void *buf)
399 size_t nmembs = argv->v_nmembs;
400 struct inode *dat = nilfs_dat_inode(nilfs);
401 struct nilfs_bmap *bmap = NILFS_I(dat)->i_bmap;
402 struct nilfs_bdesc *bdescs = buf;
405 for (i = 0; i < nmembs; i++) {
406 /* XXX: use macro or inline func to check liveness */
407 ret = nilfs_bmap_lookup_at_level(bmap,
409 bdescs[i].bd_level + 1,
410 &bdescs[i].bd_blocknr);
414 bdescs[i].bd_blocknr = 0;
416 if (bdescs[i].bd_blocknr != bdescs[i].bd_oblocknr)
417 /* skip dead block */
419 if (bdescs[i].bd_level == 0) {
420 ret = nilfs_mdt_mark_block_dirty(dat,
421 bdescs[i].bd_offset);
423 WARN_ON(ret == -ENOENT);
427 ret = nilfs_bmap_mark(bmap, bdescs[i].bd_offset,
430 WARN_ON(ret == -ENOENT);
438 int nilfs_ioctl_prepare_clean_segments(struct the_nilfs *nilfs,
439 struct nilfs_argv *argv, void **kbufs)
444 ret = nilfs_ioctl_move_blocks(nilfs, &argv[0], kbufs[0]);
446 msg = "cannot read source blocks";
450 ret = nilfs_ioctl_delete_checkpoints(nilfs, &argv[1], kbufs[1]);
453 * can safely abort because checkpoints can be removed
456 msg = "cannot delete checkpoints";
459 ret = nilfs_ioctl_free_vblocknrs(nilfs, &argv[2], kbufs[2]);
462 * can safely abort because DAT file is updated atomically
463 * using a copy-on-write technique.
465 msg = "cannot delete virtual blocks from DAT file";
468 ret = nilfs_ioctl_mark_blocks_dirty(nilfs, &argv[3], kbufs[3]);
471 * can safely abort because the operation is nondestructive.
473 msg = "cannot mark copying blocks dirty";
479 nilfs_remove_all_gcinode(nilfs);
480 printk(KERN_ERR "NILFS: GC failed during preparation: %s: err=%d\n",
485 static int nilfs_ioctl_clean_segments(struct inode *inode, struct file *filp,
486 unsigned int cmd, void __user *argp)
488 struct nilfs_argv argv[5];
489 const static size_t argsz[5] = {
490 sizeof(struct nilfs_vdesc),
491 sizeof(struct nilfs_period),
493 sizeof(struct nilfs_bdesc),
498 struct the_nilfs *nilfs;
502 if (!capable(CAP_SYS_ADMIN))
505 if (copy_from_user(argv, argp, sizeof(argv)))
508 nsegs = argv[4].v_nmembs;
509 if (argv[4].v_size != argsz[4])
512 * argv[4] points to segment numbers this ioctl cleans. We
513 * use kmalloc() for its buffer because memory used for the
514 * segment numbers is enough small.
516 kbufs[4] = memdup_user((void __user *)(unsigned long)argv[4].v_base,
517 nsegs * sizeof(__u64));
518 if (IS_ERR(kbufs[4]))
519 return PTR_ERR(kbufs[4]);
521 nilfs = NILFS_SB(inode->i_sb)->s_nilfs;
523 for (n = 0; n < 4; n++) {
525 if (argv[n].v_size != argsz[n])
528 if (argv[n].v_nmembs > nsegs * nilfs->ns_blocks_per_segment)
531 len = argv[n].v_size * argv[n].v_nmembs;
532 base = (void __user *)(unsigned long)argv[n].v_base;
538 kbufs[n] = vmalloc(len);
543 if (copy_from_user(kbufs[n], base, len)) {
550 ret = nilfs_clean_segments(inode->i_sb, argv, kbufs);
559 static int nilfs_ioctl_sync(struct inode *inode, struct file *filp,
560 unsigned int cmd, void __user *argp)
565 ret = nilfs_construct_segment(inode->i_sb);
570 cno = NILFS_SB(inode->i_sb)->s_nilfs->ns_cno - 1;
571 if (copy_to_user(argp, &cno, sizeof(cno)))
577 static int nilfs_ioctl_get_info(struct inode *inode, struct file *filp,
578 unsigned int cmd, void __user *argp,
580 ssize_t (*dofunc)(struct the_nilfs *,
582 void *, size_t, size_t))
585 struct the_nilfs *nilfs = NILFS_SB(inode->i_sb)->s_nilfs;
586 struct nilfs_argv argv;
589 if (copy_from_user(&argv, argp, sizeof(argv)))
592 if (argv.v_size != membsz)
595 ret = nilfs_ioctl_wrap_copy(nilfs, &argv, _IOC_DIR(cmd), dofunc);
599 if (copy_to_user(argp, &argv, sizeof(argv)))
604 long nilfs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
606 struct inode *inode = filp->f_dentry->d_inode;
607 void __user *argp = (void * __user *)arg;
610 case NILFS_IOCTL_CHANGE_CPMODE:
611 return nilfs_ioctl_change_cpmode(inode, filp, cmd, argp);
612 case NILFS_IOCTL_DELETE_CHECKPOINT:
613 return nilfs_ioctl_delete_checkpoint(inode, filp, cmd, argp);
614 case NILFS_IOCTL_GET_CPINFO:
615 return nilfs_ioctl_get_info(inode, filp, cmd, argp,
616 sizeof(struct nilfs_cpinfo),
617 nilfs_ioctl_do_get_cpinfo);
618 case NILFS_IOCTL_GET_CPSTAT:
619 return nilfs_ioctl_get_cpstat(inode, filp, cmd, argp);
620 case NILFS_IOCTL_GET_SUINFO:
621 return nilfs_ioctl_get_info(inode, filp, cmd, argp,
622 sizeof(struct nilfs_suinfo),
623 nilfs_ioctl_do_get_suinfo);
624 case NILFS_IOCTL_GET_SUSTAT:
625 return nilfs_ioctl_get_sustat(inode, filp, cmd, argp);
626 case NILFS_IOCTL_GET_VINFO:
627 return nilfs_ioctl_get_info(inode, filp, cmd, argp,
628 sizeof(struct nilfs_vinfo),
629 nilfs_ioctl_do_get_vinfo);
630 case NILFS_IOCTL_GET_BDESCS:
631 return nilfs_ioctl_get_bdescs(inode, filp, cmd, argp);
632 case NILFS_IOCTL_CLEAN_SEGMENTS:
633 return nilfs_ioctl_clean_segments(inode, filp, cmd, argp);
634 case NILFS_IOCTL_SYNC:
635 return nilfs_ioctl_sync(inode, filp, cmd, argp);