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)
152 down_read(&nilfs->ns_segctor_sem);
153 ret = nilfs_cpfile_get_cpinfo(nilfs->ns_cpfile, posp, flags, buf,
155 up_read(&nilfs->ns_segctor_sem);
159 static int nilfs_ioctl_get_cpstat(struct inode *inode, struct file *filp,
160 unsigned int cmd, void __user *argp)
162 struct the_nilfs *nilfs = NILFS_SB(inode->i_sb)->s_nilfs;
163 struct nilfs_cpstat cpstat;
166 down_read(&nilfs->ns_segctor_sem);
167 ret = nilfs_cpfile_get_stat(nilfs->ns_cpfile, &cpstat);
168 up_read(&nilfs->ns_segctor_sem);
172 if (copy_to_user(argp, &cpstat, sizeof(cpstat)))
178 nilfs_ioctl_do_get_suinfo(struct the_nilfs *nilfs, __u64 *posp, int flags,
179 void *buf, size_t size, size_t nmembs)
183 down_read(&nilfs->ns_segctor_sem);
184 ret = nilfs_sufile_get_suinfo(nilfs->ns_sufile, *posp, buf, nmembs);
185 up_read(&nilfs->ns_segctor_sem);
189 static int nilfs_ioctl_get_sustat(struct inode *inode, struct file *filp,
190 unsigned int cmd, void __user *argp)
192 struct the_nilfs *nilfs = NILFS_SB(inode->i_sb)->s_nilfs;
193 struct nilfs_sustat sustat;
196 down_read(&nilfs->ns_segctor_sem);
197 ret = nilfs_sufile_get_stat(nilfs->ns_sufile, &sustat);
198 up_read(&nilfs->ns_segctor_sem);
202 if (copy_to_user(argp, &sustat, sizeof(sustat)))
208 nilfs_ioctl_do_get_vinfo(struct the_nilfs *nilfs, __u64 *posp, int flags,
209 void *buf, size_t size, size_t nmembs)
213 down_read(&nilfs->ns_segctor_sem);
214 ret = nilfs_dat_get_vinfo(nilfs_dat_inode(nilfs), buf, nmembs);
215 up_read(&nilfs->ns_segctor_sem);
220 nilfs_ioctl_do_get_bdescs(struct the_nilfs *nilfs, __u64 *posp, int flags,
221 void *buf, size_t size, size_t nmembs)
223 struct inode *dat = nilfs_dat_inode(nilfs);
224 struct nilfs_bmap *bmap = NILFS_I(dat)->i_bmap;
225 struct nilfs_bdesc *bdescs = buf;
228 down_read(&nilfs->ns_segctor_sem);
229 for (i = 0; i < nmembs; i++) {
230 ret = nilfs_bmap_lookup_at_level(bmap,
232 bdescs[i].bd_level + 1,
233 &bdescs[i].bd_blocknr);
235 if (ret != -ENOENT) {
236 up_read(&nilfs->ns_segctor_sem);
239 bdescs[i].bd_blocknr = 0;
242 up_read(&nilfs->ns_segctor_sem);
246 static int nilfs_ioctl_get_bdescs(struct inode *inode, struct file *filp,
247 unsigned int cmd, void __user *argp)
249 struct the_nilfs *nilfs = NILFS_SB(inode->i_sb)->s_nilfs;
250 struct nilfs_argv argv;
253 if (copy_from_user(&argv, argp, sizeof(argv)))
256 ret = nilfs_ioctl_wrap_copy(nilfs, &argv, _IOC_DIR(cmd),
257 nilfs_ioctl_do_get_bdescs);
261 if (copy_to_user(argp, &argv, sizeof(argv)))
266 static int nilfs_ioctl_move_inode_block(struct inode *inode,
267 struct nilfs_vdesc *vdesc,
268 struct list_head *buffers)
270 struct buffer_head *bh;
273 if (vdesc->vd_flags == 0)
274 ret = nilfs_gccache_submit_read_data(
275 inode, vdesc->vd_offset, vdesc->vd_blocknr,
276 vdesc->vd_vblocknr, &bh);
278 ret = nilfs_gccache_submit_read_node(
279 inode, vdesc->vd_blocknr, vdesc->vd_vblocknr, &bh);
281 if (unlikely(ret < 0)) {
284 "%s: invalid virtual block address (%s): "
285 "ino=%llu, cno=%llu, offset=%llu, "
286 "blocknr=%llu, vblocknr=%llu\n",
287 __func__, vdesc->vd_flags ? "node" : "data",
288 (unsigned long long)vdesc->vd_ino,
289 (unsigned long long)vdesc->vd_cno,
290 (unsigned long long)vdesc->vd_offset,
291 (unsigned long long)vdesc->vd_blocknr,
292 (unsigned long long)vdesc->vd_vblocknr);
295 bh->b_private = vdesc;
296 list_add_tail(&bh->b_assoc_buffers, buffers);
301 nilfs_ioctl_do_move_blocks(struct the_nilfs *nilfs, __u64 *posp, int flags,
302 void *buf, size_t size, size_t nmembs)
305 struct nilfs_vdesc *vdesc;
306 struct buffer_head *bh, *n;
312 for (i = 0, vdesc = buf; i < nmembs; ) {
315 inode = nilfs_gc_iget(nilfs, ino, cno);
316 if (unlikely(inode == NULL)) {
321 ret = nilfs_ioctl_move_inode_block(inode, vdesc,
323 if (unlikely(ret < 0))
326 } while (++i < nmembs &&
327 vdesc->vd_ino == ino && vdesc->vd_cno == cno);
330 list_for_each_entry_safe(bh, n, &buffers, b_assoc_buffers) {
331 ret = nilfs_gccache_wait_and_mark_dirty(bh);
332 if (unlikely(ret < 0)) {
333 if (ret == -EEXIST) {
334 vdesc = bh->b_private;
336 "%s: conflicting %s buffer: "
337 "ino=%llu, cno=%llu, offset=%llu, "
338 "blocknr=%llu, vblocknr=%llu\n",
340 vdesc->vd_flags ? "node" : "data",
341 (unsigned long long)vdesc->vd_ino,
342 (unsigned long long)vdesc->vd_cno,
343 (unsigned long long)vdesc->vd_offset,
344 (unsigned long long)vdesc->vd_blocknr,
345 (unsigned long long)vdesc->vd_vblocknr);
349 list_del_init(&bh->b_assoc_buffers);
350 bh->b_private = NULL;
356 list_for_each_entry_safe(bh, n, &buffers, b_assoc_buffers) {
357 list_del_init(&bh->b_assoc_buffers);
358 bh->b_private = NULL;
364 static inline int nilfs_ioctl_move_blocks(struct the_nilfs *nilfs,
365 struct nilfs_argv *argv,
368 return nilfs_ioctl_wrap_copy(nilfs, argv, dir,
369 nilfs_ioctl_do_move_blocks);
373 nilfs_ioctl_do_delete_checkpoints(struct the_nilfs *nilfs, __u64 *posp,
374 int flags, void *buf, size_t size,
377 struct inode *cpfile = nilfs->ns_cpfile;
378 struct nilfs_period *periods = buf;
381 for (i = 0; i < nmembs; i++) {
382 ret = nilfs_cpfile_delete_checkpoints(
383 cpfile, periods[i].p_start, periods[i].p_end);
390 static inline int nilfs_ioctl_delete_checkpoints(struct the_nilfs *nilfs,
391 struct nilfs_argv *argv,
394 return nilfs_ioctl_wrap_copy(nilfs, argv, dir,
395 nilfs_ioctl_do_delete_checkpoints);
399 nilfs_ioctl_do_free_vblocknrs(struct the_nilfs *nilfs, __u64 *posp, int flags,
400 void *buf, size_t size, size_t nmembs)
402 int ret = nilfs_dat_freev(nilfs_dat_inode(nilfs), buf, nmembs);
404 return (ret < 0) ? ret : nmembs;
407 static inline int nilfs_ioctl_free_vblocknrs(struct the_nilfs *nilfs,
408 struct nilfs_argv *argv,
411 return nilfs_ioctl_wrap_copy(nilfs, argv, dir,
412 nilfs_ioctl_do_free_vblocknrs);
416 nilfs_ioctl_do_mark_blocks_dirty(struct the_nilfs *nilfs, __u64 *posp,
417 int flags, void *buf, size_t size,
420 struct inode *dat = nilfs_dat_inode(nilfs);
421 struct nilfs_bmap *bmap = NILFS_I(dat)->i_bmap;
422 struct nilfs_bdesc *bdescs = buf;
425 for (i = 0; i < nmembs; i++) {
426 /* XXX: use macro or inline func to check liveness */
427 ret = nilfs_bmap_lookup_at_level(bmap,
429 bdescs[i].bd_level + 1,
430 &bdescs[i].bd_blocknr);
434 bdescs[i].bd_blocknr = 0;
436 if (bdescs[i].bd_blocknr != bdescs[i].bd_oblocknr)
437 /* skip dead block */
439 if (bdescs[i].bd_level == 0) {
440 ret = nilfs_mdt_mark_block_dirty(dat,
441 bdescs[i].bd_offset);
443 WARN_ON(ret == -ENOENT);
447 ret = nilfs_bmap_mark(bmap, bdescs[i].bd_offset,
450 WARN_ON(ret == -ENOENT);
458 static inline int nilfs_ioctl_mark_blocks_dirty(struct the_nilfs *nilfs,
459 struct nilfs_argv *argv,
462 return nilfs_ioctl_wrap_copy(nilfs, argv, dir,
463 nilfs_ioctl_do_mark_blocks_dirty);
467 nilfs_ioctl_do_free_segments(struct the_nilfs *nilfs, __u64 *posp, int flags,
468 void *buf, size_t size, size_t nmembs)
470 struct nilfs_sb_info *sbi = nilfs->ns_writer;
473 if (unlikely(!sbi)) {
474 /* never happens because called for a writable mount */
478 ret = nilfs_segctor_add_segments_to_be_freed(
479 NILFS_SC(sbi), buf, nmembs);
481 return (ret < 0) ? ret : nmembs;
484 static inline int nilfs_ioctl_free_segments(struct the_nilfs *nilfs,
485 struct nilfs_argv *argv,
488 return nilfs_ioctl_wrap_copy(nilfs, argv, dir,
489 nilfs_ioctl_do_free_segments);
492 int nilfs_ioctl_prepare_clean_segments(struct the_nilfs *nilfs,
495 struct nilfs_argv argv[5];
499 if (copy_from_user(argv, argp, sizeof(argv)))
503 ret = nilfs_ioctl_move_blocks(nilfs, &argv[0], dir);
505 msg = "cannot read source blocks";
508 ret = nilfs_ioctl_delete_checkpoints(nilfs, &argv[1], dir);
511 * can safely abort because checkpoints can be removed
514 msg = "cannot delete checkpoints";
517 ret = nilfs_ioctl_free_vblocknrs(nilfs, &argv[2], dir);
520 * can safely abort because DAT file is updated atomically
521 * using a copy-on-write technique.
523 msg = "cannot delete virtual blocks from DAT file";
526 ret = nilfs_ioctl_mark_blocks_dirty(nilfs, &argv[3], dir);
529 * can safely abort because the operation is nondestructive.
531 msg = "cannot mark copying blocks dirty";
534 ret = nilfs_ioctl_free_segments(nilfs, &argv[4], dir);
537 * can safely abort because this operation is atomic.
539 msg = "cannot set segments to be freed";
545 nilfs_remove_all_gcinode(nilfs);
546 printk(KERN_ERR "NILFS: GC failed during preparation: %s: err=%d\n",
551 static int nilfs_ioctl_clean_segments(struct inode *inode, struct file *filp,
552 unsigned int cmd, void __user *argp)
554 if (!capable(CAP_SYS_ADMIN))
556 return nilfs_clean_segments(inode->i_sb, argp);
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,
579 ssize_t (*dofunc)(struct the_nilfs *,
581 void *, size_t, size_t))
584 struct the_nilfs *nilfs = NILFS_SB(inode->i_sb)->s_nilfs;
585 struct nilfs_argv argv;
588 if (copy_from_user(&argv, argp, sizeof(argv)))
591 ret = nilfs_ioctl_wrap_copy(nilfs, &argv, _IOC_DIR(cmd), dofunc);
595 if (copy_to_user(argp, &argv, sizeof(argv)))
600 long nilfs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
602 struct inode *inode = filp->f_dentry->d_inode;
603 void __user *argp = (void * __user *)arg;
606 case NILFS_IOCTL_CHANGE_CPMODE:
607 return nilfs_ioctl_change_cpmode(inode, filp, cmd, argp);
608 case NILFS_IOCTL_DELETE_CHECKPOINT:
609 return nilfs_ioctl_delete_checkpoint(inode, filp, cmd, argp);
610 case NILFS_IOCTL_GET_CPINFO:
611 return nilfs_ioctl_get_info(inode, filp, cmd, argp,
612 nilfs_ioctl_do_get_cpinfo);
613 case NILFS_IOCTL_GET_CPSTAT:
614 return nilfs_ioctl_get_cpstat(inode, filp, cmd, argp);
615 case NILFS_IOCTL_GET_SUINFO:
616 return nilfs_ioctl_get_info(inode, filp, cmd, argp,
617 nilfs_ioctl_do_get_suinfo);
618 case NILFS_IOCTL_GET_SUSTAT:
619 return nilfs_ioctl_get_sustat(inode, filp, cmd, argp);
620 case NILFS_IOCTL_GET_VINFO:
621 return nilfs_ioctl_get_info(inode, filp, cmd, argp,
622 nilfs_ioctl_do_get_vinfo);
623 case NILFS_IOCTL_GET_BDESCS:
624 return nilfs_ioctl_get_bdescs(inode, filp, cmd, argp);
625 case NILFS_IOCTL_CLEAN_SEGMENTS:
626 return nilfs_ioctl_clean_segments(inode, filp, cmd, argp);
627 case NILFS_IOCTL_SYNC:
628 return nilfs_ioctl_sync(inode, filp, cmd, argp);