blob: 313d0a21da480afc05afacfb46bec6f74e34b76a [file] [log] [blame]
Koji Sato7942b912009-04-06 19:01:41 -07001/*
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>
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() */
Ryusuke Konishi4f6b8282009-05-10 22:41:43 +090028#include <linux/vmalloc.h>
Ryusuke Konishi75124872010-01-26 13:59:40 +090029#include <linux/mount.h> /* mnt_want_write(), mnt_drop_write() */
Koji Sato7942b912009-04-06 19:01:41 -070030#include <linux/nilfs2_fs.h>
31#include "nilfs.h"
32#include "segment.h"
33#include "bmap.h"
34#include "cpfile.h"
35#include "sufile.h"
36#include "dat.h"
37
38
Koji Sato7942b912009-04-06 19:01:41 -070039static int nilfs_ioctl_wrap_copy(struct the_nilfs *nilfs,
40 struct nilfs_argv *argv, int dir,
41 ssize_t (*dofunc)(struct the_nilfs *,
Ryusuke Konishib028fcf2009-04-06 19:01:47 -070042 __u64 *, int,
Koji Sato7942b912009-04-06 19:01:41 -070043 void *, size_t, size_t))
44{
45 void *buf;
Ryusuke Konishidc498d02009-04-06 19:01:52 -070046 void __user *base = (void __user *)(unsigned long)argv->v_base;
Ryusuke Konishi3358b4a2009-04-06 19:01:43 -070047 size_t maxmembs, total, n;
Koji Sato7942b912009-04-06 19:01:41 -070048 ssize_t nr;
49 int ret, i;
Ryusuke Konishib028fcf2009-04-06 19:01:47 -070050 __u64 pos, ppos;
Koji Sato7942b912009-04-06 19:01:41 -070051
52 if (argv->v_nmembs == 0)
53 return 0;
54
Ryusuke Konishi3358b4a2009-04-06 19:01:43 -070055 if (argv->v_size > PAGE_SIZE)
56 return -EINVAL;
57
58 buf = (void *)__get_free_pages(GFP_NOFS, 0);
59 if (unlikely(!buf))
Koji Sato7942b912009-04-06 19:01:41 -070060 return -ENOMEM;
Ryusuke Konishi3358b4a2009-04-06 19:01:43 -070061 maxmembs = PAGE_SIZE / argv->v_size;
Koji Sato7942b912009-04-06 19:01:41 -070062
63 ret = 0;
64 total = 0;
Ryusuke Konishib028fcf2009-04-06 19:01:47 -070065 pos = argv->v_index;
Koji Sato7942b912009-04-06 19:01:41 -070066 for (i = 0; i < argv->v_nmembs; i += n) {
67 n = (argv->v_nmembs - i < maxmembs) ?
68 argv->v_nmembs - i : maxmembs;
69 if ((dir & _IOC_WRITE) &&
Ryusuke Konishidc498d02009-04-06 19:01:52 -070070 copy_from_user(buf, base + argv->v_size * i,
71 argv->v_size * n)) {
Koji Sato7942b912009-04-06 19:01:41 -070072 ret = -EFAULT;
73 break;
74 }
Ryusuke Konishib028fcf2009-04-06 19:01:47 -070075 ppos = pos;
Pekka Enberg8acfbf02009-04-06 19:01:49 -070076 nr = dofunc(nilfs, &pos, argv->v_flags, buf, argv->v_size,
Ryusuke Konishib028fcf2009-04-06 19:01:47 -070077 n);
Koji Sato7942b912009-04-06 19:01:41 -070078 if (nr < 0) {
79 ret = nr;
80 break;
81 }
82 if ((dir & _IOC_READ) &&
Ryusuke Konishidc498d02009-04-06 19:01:52 -070083 copy_to_user(base + argv->v_size * i, buf,
84 argv->v_size * nr)) {
Koji Sato7942b912009-04-06 19:01:41 -070085 ret = -EFAULT;
86 break;
87 }
88 total += nr;
Ryusuke Konishib028fcf2009-04-06 19:01:47 -070089 if ((size_t)nr < n)
90 break;
91 if (pos == ppos)
92 pos += n;
Koji Sato7942b912009-04-06 19:01:41 -070093 }
94 argv->v_nmembs = total;
95
Ryusuke Konishi3358b4a2009-04-06 19:01:43 -070096 free_pages((unsigned long)buf, 0);
Koji Sato7942b912009-04-06 19:01:41 -070097 return ret;
98}
99
100static int nilfs_ioctl_change_cpmode(struct inode *inode, struct file *filp,
101 unsigned int cmd, void __user *argp)
102{
Ryusuke Konishic1ea9852009-11-12 00:13:32 +0900103 struct the_nilfs *nilfs = NILFS_SB(inode->i_sb)->s_nilfs;
104 struct inode *cpfile = nilfs->ns_cpfile;
Koji Sato7942b912009-04-06 19:01:41 -0700105 struct nilfs_transaction_info ti;
106 struct nilfs_cpmode cpmode;
107 int ret;
108
109 if (!capable(CAP_SYS_ADMIN))
110 return -EPERM;
Ryusuke Konishi75124872010-01-26 13:59:40 +0900111
112 ret = mnt_want_write(filp->f_path.mnt);
113 if (ret)
114 return ret;
115
116 ret = -EFAULT;
Koji Sato7942b912009-04-06 19:01:41 -0700117 if (copy_from_user(&cpmode, argp, sizeof(cpmode)))
Ryusuke Konishi75124872010-01-26 13:59:40 +0900118 goto out;
Koji Sato7942b912009-04-06 19:01:41 -0700119
Ryusuke Konishic1ea9852009-11-12 00:13:32 +0900120 mutex_lock(&nilfs->ns_mount_mutex);
Ryusuke Konishi75124872010-01-26 13:59:40 +0900121
Koji Sato7942b912009-04-06 19:01:41 -0700122 nilfs_transaction_begin(inode->i_sb, &ti, 0);
123 ret = nilfs_cpfile_change_cpmode(
124 cpfile, cpmode.cm_cno, cpmode.cm_mode);
Ryusuke Konishi75124872010-01-26 13:59:40 +0900125 if (unlikely(ret < 0))
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700126 nilfs_transaction_abort(inode->i_sb);
Ryusuke Konishi75124872010-01-26 13:59:40 +0900127 else
128 nilfs_transaction_commit(inode->i_sb); /* never fails */
129
Ryusuke Konishic1ea9852009-11-12 00:13:32 +0900130 mutex_unlock(&nilfs->ns_mount_mutex);
Ryusuke Konishi75124872010-01-26 13:59:40 +0900131out:
132 mnt_drop_write(filp->f_path.mnt);
Koji Sato7942b912009-04-06 19:01:41 -0700133 return ret;
134}
135
136static int
137nilfs_ioctl_delete_checkpoint(struct inode *inode, struct file *filp,
138 unsigned int cmd, void __user *argp)
139{
140 struct inode *cpfile = NILFS_SB(inode->i_sb)->s_nilfs->ns_cpfile;
141 struct nilfs_transaction_info ti;
142 __u64 cno;
143 int ret;
144
145 if (!capable(CAP_SYS_ADMIN))
146 return -EPERM;
Ryusuke Konishi75124872010-01-26 13:59:40 +0900147
148 ret = mnt_want_write(filp->f_path.mnt);
149 if (ret)
150 return ret;
151
152 ret = -EFAULT;
Koji Sato7942b912009-04-06 19:01:41 -0700153 if (copy_from_user(&cno, argp, sizeof(cno)))
Ryusuke Konishi75124872010-01-26 13:59:40 +0900154 goto out;
Koji Sato7942b912009-04-06 19:01:41 -0700155
156 nilfs_transaction_begin(inode->i_sb, &ti, 0);
157 ret = nilfs_cpfile_delete_checkpoint(cpfile, cno);
Ryusuke Konishi75124872010-01-26 13:59:40 +0900158 if (unlikely(ret < 0))
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700159 nilfs_transaction_abort(inode->i_sb);
Ryusuke Konishi75124872010-01-26 13:59:40 +0900160 else
161 nilfs_transaction_commit(inode->i_sb); /* never fails */
162out:
163 mnt_drop_write(filp->f_path.mnt);
Koji Sato7942b912009-04-06 19:01:41 -0700164 return ret;
165}
166
167static ssize_t
Ryusuke Konishib028fcf2009-04-06 19:01:47 -0700168nilfs_ioctl_do_get_cpinfo(struct the_nilfs *nilfs, __u64 *posp, int flags,
Koji Sato7942b912009-04-06 19:01:41 -0700169 void *buf, size_t size, size_t nmembs)
170{
Koji Sato7942b912009-04-06 19:01:41 -0700171 int ret;
172
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700173 down_read(&nilfs->ns_segctor_sem);
Ryusuke Konishi47eb6b92009-04-30 02:21:00 +0900174 ret = nilfs_cpfile_get_cpinfo(nilfs->ns_cpfile, posp, flags, buf,
Ryusuke Konishi003ff182009-05-12 03:58:47 +0900175 size, nmembs);
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700176 up_read(&nilfs->ns_segctor_sem);
Koji Sato7942b912009-04-06 19:01:41 -0700177 return ret;
178}
179
180static int nilfs_ioctl_get_cpstat(struct inode *inode, struct file *filp,
181 unsigned int cmd, void __user *argp)
182{
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700183 struct the_nilfs *nilfs = NILFS_SB(inode->i_sb)->s_nilfs;
Koji Sato7942b912009-04-06 19:01:41 -0700184 struct nilfs_cpstat cpstat;
Koji Sato7942b912009-04-06 19:01:41 -0700185 int ret;
186
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700187 down_read(&nilfs->ns_segctor_sem);
188 ret = nilfs_cpfile_get_stat(nilfs->ns_cpfile, &cpstat);
189 up_read(&nilfs->ns_segctor_sem);
Koji Sato7942b912009-04-06 19:01:41 -0700190 if (ret < 0)
191 return ret;
192
193 if (copy_to_user(argp, &cpstat, sizeof(cpstat)))
194 ret = -EFAULT;
195 return ret;
196}
197
198static ssize_t
Ryusuke Konishib028fcf2009-04-06 19:01:47 -0700199nilfs_ioctl_do_get_suinfo(struct the_nilfs *nilfs, __u64 *posp, int flags,
Koji Sato7942b912009-04-06 19:01:41 -0700200 void *buf, size_t size, size_t nmembs)
201{
Koji Sato7942b912009-04-06 19:01:41 -0700202 int ret;
203
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700204 down_read(&nilfs->ns_segctor_sem);
Ryusuke Konishi003ff182009-05-12 03:58:47 +0900205 ret = nilfs_sufile_get_suinfo(nilfs->ns_sufile, *posp, buf, size,
206 nmembs);
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700207 up_read(&nilfs->ns_segctor_sem);
Koji Sato7942b912009-04-06 19:01:41 -0700208 return ret;
209}
210
211static int nilfs_ioctl_get_sustat(struct inode *inode, struct file *filp,
212 unsigned int cmd, void __user *argp)
213{
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700214 struct the_nilfs *nilfs = NILFS_SB(inode->i_sb)->s_nilfs;
Koji Sato7942b912009-04-06 19:01:41 -0700215 struct nilfs_sustat sustat;
Koji Sato7942b912009-04-06 19:01:41 -0700216 int ret;
217
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700218 down_read(&nilfs->ns_segctor_sem);
219 ret = nilfs_sufile_get_stat(nilfs->ns_sufile, &sustat);
220 up_read(&nilfs->ns_segctor_sem);
Koji Sato7942b912009-04-06 19:01:41 -0700221 if (ret < 0)
222 return ret;
223
224 if (copy_to_user(argp, &sustat, sizeof(sustat)))
225 ret = -EFAULT;
226 return ret;
227}
228
229static ssize_t
Ryusuke Konishib028fcf2009-04-06 19:01:47 -0700230nilfs_ioctl_do_get_vinfo(struct the_nilfs *nilfs, __u64 *posp, int flags,
Koji Sato7942b912009-04-06 19:01:41 -0700231 void *buf, size_t size, size_t nmembs)
232{
Koji Sato7942b912009-04-06 19:01:41 -0700233 int ret;
234
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700235 down_read(&nilfs->ns_segctor_sem);
Ryusuke Konishi003ff182009-05-12 03:58:47 +0900236 ret = nilfs_dat_get_vinfo(nilfs_dat_inode(nilfs), buf, size, nmembs);
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700237 up_read(&nilfs->ns_segctor_sem);
Koji Sato7942b912009-04-06 19:01:41 -0700238 return ret;
239}
240
241static ssize_t
Ryusuke Konishib028fcf2009-04-06 19:01:47 -0700242nilfs_ioctl_do_get_bdescs(struct the_nilfs *nilfs, __u64 *posp, int flags,
Koji Sato7942b912009-04-06 19:01:41 -0700243 void *buf, size_t size, size_t nmembs)
244{
245 struct inode *dat = nilfs_dat_inode(nilfs);
246 struct nilfs_bmap *bmap = NILFS_I(dat)->i_bmap;
247 struct nilfs_bdesc *bdescs = buf;
248 int ret, i;
249
Ryusuke Konishi47eb6b92009-04-30 02:21:00 +0900250 down_read(&nilfs->ns_segctor_sem);
Koji Sato7942b912009-04-06 19:01:41 -0700251 for (i = 0; i < nmembs; i++) {
252 ret = nilfs_bmap_lookup_at_level(bmap,
253 bdescs[i].bd_offset,
254 bdescs[i].bd_level + 1,
255 &bdescs[i].bd_blocknr);
256 if (ret < 0) {
Ryusuke Konishi47eb6b92009-04-30 02:21:00 +0900257 if (ret != -ENOENT) {
258 up_read(&nilfs->ns_segctor_sem);
Koji Sato7942b912009-04-06 19:01:41 -0700259 return ret;
Ryusuke Konishi47eb6b92009-04-30 02:21:00 +0900260 }
Koji Sato7942b912009-04-06 19:01:41 -0700261 bdescs[i].bd_blocknr = 0;
262 }
263 }
Ryusuke Konishi47eb6b92009-04-30 02:21:00 +0900264 up_read(&nilfs->ns_segctor_sem);
Koji Sato7942b912009-04-06 19:01:41 -0700265 return nmembs;
266}
267
268static int nilfs_ioctl_get_bdescs(struct inode *inode, struct file *filp,
269 unsigned int cmd, void __user *argp)
270{
271 struct the_nilfs *nilfs = NILFS_SB(inode->i_sb)->s_nilfs;
272 struct nilfs_argv argv;
Koji Sato7942b912009-04-06 19:01:41 -0700273 int ret;
274
275 if (copy_from_user(&argv, argp, sizeof(argv)))
276 return -EFAULT;
277
Ryusuke Konishi83aca8f2009-05-11 23:24:47 +0900278 if (argv.v_size != sizeof(struct nilfs_bdesc))
279 return -EINVAL;
280
Koji Sato7942b912009-04-06 19:01:41 -0700281 ret = nilfs_ioctl_wrap_copy(nilfs, &argv, _IOC_DIR(cmd),
282 nilfs_ioctl_do_get_bdescs);
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700283 if (ret < 0)
284 return ret;
Koji Sato7942b912009-04-06 19:01:41 -0700285
286 if (copy_to_user(argp, &argv, sizeof(argv)))
287 ret = -EFAULT;
288 return ret;
289}
290
291static int nilfs_ioctl_move_inode_block(struct inode *inode,
292 struct nilfs_vdesc *vdesc,
293 struct list_head *buffers)
294{
295 struct buffer_head *bh;
296 int ret;
297
298 if (vdesc->vd_flags == 0)
299 ret = nilfs_gccache_submit_read_data(
300 inode, vdesc->vd_offset, vdesc->vd_blocknr,
301 vdesc->vd_vblocknr, &bh);
302 else
303 ret = nilfs_gccache_submit_read_node(
304 inode, vdesc->vd_blocknr, vdesc->vd_vblocknr, &bh);
305
306 if (unlikely(ret < 0)) {
307 if (ret == -ENOENT)
308 printk(KERN_CRIT
309 "%s: invalid virtual block address (%s): "
310 "ino=%llu, cno=%llu, offset=%llu, "
311 "blocknr=%llu, vblocknr=%llu\n",
312 __func__, vdesc->vd_flags ? "node" : "data",
313 (unsigned long long)vdesc->vd_ino,
314 (unsigned long long)vdesc->vd_cno,
315 (unsigned long long)vdesc->vd_offset,
316 (unsigned long long)vdesc->vd_blocknr,
317 (unsigned long long)vdesc->vd_vblocknr);
318 return ret;
319 }
Ryusuke Konishi5399dd12009-11-07 18:45:16 +0900320 if (unlikely(!list_empty(&bh->b_assoc_buffers))) {
321 printk(KERN_CRIT "%s: conflicting %s buffer: ino=%llu, "
322 "cno=%llu, offset=%llu, blocknr=%llu, vblocknr=%llu\n",
323 __func__, vdesc->vd_flags ? "node" : "data",
324 (unsigned long long)vdesc->vd_ino,
325 (unsigned long long)vdesc->vd_cno,
326 (unsigned long long)vdesc->vd_offset,
327 (unsigned long long)vdesc->vd_blocknr,
328 (unsigned long long)vdesc->vd_vblocknr);
329 brelse(bh);
330 return -EEXIST;
331 }
Koji Sato7942b912009-04-06 19:01:41 -0700332 list_add_tail(&bh->b_assoc_buffers, buffers);
333 return 0;
334}
335
Ryusuke Konishi4f6b8282009-05-10 22:41:43 +0900336static int nilfs_ioctl_move_blocks(struct the_nilfs *nilfs,
337 struct nilfs_argv *argv, void *buf)
Koji Sato7942b912009-04-06 19:01:41 -0700338{
Ryusuke Konishi4f6b8282009-05-10 22:41:43 +0900339 size_t nmembs = argv->v_nmembs;
Koji Sato7942b912009-04-06 19:01:41 -0700340 struct inode *inode;
341 struct nilfs_vdesc *vdesc;
342 struct buffer_head *bh, *n;
343 LIST_HEAD(buffers);
344 ino_t ino;
345 __u64 cno;
346 int i, ret;
347
348 for (i = 0, vdesc = buf; i < nmembs; ) {
349 ino = vdesc->vd_ino;
350 cno = vdesc->vd_cno;
351 inode = nilfs_gc_iget(nilfs, ino, cno);
352 if (unlikely(inode == NULL)) {
353 ret = -ENOMEM;
354 goto failed;
355 }
356 do {
357 ret = nilfs_ioctl_move_inode_block(inode, vdesc,
358 &buffers);
359 if (unlikely(ret < 0))
360 goto failed;
361 vdesc++;
362 } while (++i < nmembs &&
363 vdesc->vd_ino == ino && vdesc->vd_cno == cno);
364 }
365
366 list_for_each_entry_safe(bh, n, &buffers, b_assoc_buffers) {
367 ret = nilfs_gccache_wait_and_mark_dirty(bh);
368 if (unlikely(ret < 0)) {
Ryusuke Konishi5399dd12009-11-07 18:45:16 +0900369 WARN_ON(ret == -EEXIST);
Koji Sato7942b912009-04-06 19:01:41 -0700370 goto failed;
371 }
372 list_del_init(&bh->b_assoc_buffers);
Koji Sato7942b912009-04-06 19:01:41 -0700373 brelse(bh);
374 }
375 return nmembs;
376
377 failed:
378 list_for_each_entry_safe(bh, n, &buffers, b_assoc_buffers) {
379 list_del_init(&bh->b_assoc_buffers);
Koji Sato7942b912009-04-06 19:01:41 -0700380 brelse(bh);
381 }
382 return ret;
383}
384
Ryusuke Konishi4f6b8282009-05-10 22:41:43 +0900385static int nilfs_ioctl_delete_checkpoints(struct the_nilfs *nilfs,
386 struct nilfs_argv *argv, void *buf)
Koji Sato7942b912009-04-06 19:01:41 -0700387{
Ryusuke Konishi4f6b8282009-05-10 22:41:43 +0900388 size_t nmembs = argv->v_nmembs;
Koji Sato7942b912009-04-06 19:01:41 -0700389 struct inode *cpfile = nilfs->ns_cpfile;
390 struct nilfs_period *periods = buf;
391 int ret, i;
392
393 for (i = 0; i < nmembs; i++) {
394 ret = nilfs_cpfile_delete_checkpoints(
395 cpfile, periods[i].p_start, periods[i].p_end);
396 if (ret < 0)
397 return ret;
398 }
399 return nmembs;
400}
401
Ryusuke Konishi4f6b8282009-05-10 22:41:43 +0900402static int nilfs_ioctl_free_vblocknrs(struct the_nilfs *nilfs,
403 struct nilfs_argv *argv, void *buf)
Koji Sato7942b912009-04-06 19:01:41 -0700404{
Ryusuke Konishi4f6b8282009-05-10 22:41:43 +0900405 size_t nmembs = argv->v_nmembs;
406 int ret;
Koji Sato7942b912009-04-06 19:01:41 -0700407
Ryusuke Konishi4f6b8282009-05-10 22:41:43 +0900408 ret = nilfs_dat_freev(nilfs_dat_inode(nilfs), buf, nmembs);
Koji Sato7942b912009-04-06 19:01:41 -0700409
410 return (ret < 0) ? ret : nmembs;
411}
412
Ryusuke Konishi4f6b8282009-05-10 22:41:43 +0900413static int nilfs_ioctl_mark_blocks_dirty(struct the_nilfs *nilfs,
414 struct nilfs_argv *argv, void *buf)
Koji Sato7942b912009-04-06 19:01:41 -0700415{
Ryusuke Konishi4f6b8282009-05-10 22:41:43 +0900416 size_t nmembs = argv->v_nmembs;
Koji Sato7942b912009-04-06 19:01:41 -0700417 struct inode *dat = nilfs_dat_inode(nilfs);
418 struct nilfs_bmap *bmap = NILFS_I(dat)->i_bmap;
419 struct nilfs_bdesc *bdescs = buf;
420 int ret, i;
421
422 for (i = 0; i < nmembs; i++) {
423 /* XXX: use macro or inline func to check liveness */
424 ret = nilfs_bmap_lookup_at_level(bmap,
425 bdescs[i].bd_offset,
426 bdescs[i].bd_level + 1,
427 &bdescs[i].bd_blocknr);
428 if (ret < 0) {
429 if (ret != -ENOENT)
430 return ret;
431 bdescs[i].bd_blocknr = 0;
432 }
433 if (bdescs[i].bd_blocknr != bdescs[i].bd_oblocknr)
434 /* skip dead block */
435 continue;
436 if (bdescs[i].bd_level == 0) {
437 ret = nilfs_mdt_mark_block_dirty(dat,
438 bdescs[i].bd_offset);
439 if (ret < 0) {
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -0700440 WARN_ON(ret == -ENOENT);
Koji Sato7942b912009-04-06 19:01:41 -0700441 return ret;
442 }
443 } else {
444 ret = nilfs_bmap_mark(bmap, bdescs[i].bd_offset,
445 bdescs[i].bd_level);
446 if (ret < 0) {
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -0700447 WARN_ON(ret == -ENOENT);
Koji Sato7942b912009-04-06 19:01:41 -0700448 return ret;
449 }
450 }
451 }
452 return nmembs;
453}
454
Koji Sato7942b912009-04-06 19:01:41 -0700455int nilfs_ioctl_prepare_clean_segments(struct the_nilfs *nilfs,
Ryusuke Konishi4f6b8282009-05-10 22:41:43 +0900456 struct nilfs_argv *argv, void **kbufs)
Koji Sato7942b912009-04-06 19:01:41 -0700457{
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -0700458 const char *msg;
Ryusuke Konishi4f6b8282009-05-10 22:41:43 +0900459 int ret;
Koji Sato7942b912009-04-06 19:01:41 -0700460
Ryusuke Konishi4f6b8282009-05-10 22:41:43 +0900461 ret = nilfs_ioctl_delete_checkpoints(nilfs, &argv[1], kbufs[1]);
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -0700462 if (ret < 0) {
463 /*
464 * can safely abort because checkpoints can be removed
465 * independently.
466 */
467 msg = "cannot delete checkpoints";
468 goto failed;
469 }
Ryusuke Konishi4f6b8282009-05-10 22:41:43 +0900470 ret = nilfs_ioctl_free_vblocknrs(nilfs, &argv[2], kbufs[2]);
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -0700471 if (ret < 0) {
472 /*
473 * can safely abort because DAT file is updated atomically
474 * using a copy-on-write technique.
475 */
476 msg = "cannot delete virtual blocks from DAT file";
477 goto failed;
478 }
Ryusuke Konishi4f6b8282009-05-10 22:41:43 +0900479 ret = nilfs_ioctl_mark_blocks_dirty(nilfs, &argv[3], kbufs[3]);
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -0700480 if (ret < 0) {
481 /*
482 * can safely abort because the operation is nondestructive.
483 */
484 msg = "cannot mark copying blocks dirty";
485 goto failed;
486 }
Koji Sato7942b912009-04-06 19:01:41 -0700487 return 0;
488
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -0700489 failed:
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -0700490 printk(KERN_ERR "NILFS: GC failed during preparation: %s: err=%d\n",
491 msg, ret);
Koji Sato7942b912009-04-06 19:01:41 -0700492 return ret;
493}
494
495static int nilfs_ioctl_clean_segments(struct inode *inode, struct file *filp,
496 unsigned int cmd, void __user *argp)
497{
Ryusuke Konishi4f6b8282009-05-10 22:41:43 +0900498 struct nilfs_argv argv[5];
Tobias Klauser33e189b2009-12-23 13:57:47 +0100499 static const size_t argsz[5] = {
Ryusuke Konishi4f6b8282009-05-10 22:41:43 +0900500 sizeof(struct nilfs_vdesc),
501 sizeof(struct nilfs_period),
502 sizeof(__u64),
503 sizeof(struct nilfs_bdesc),
504 sizeof(__u64),
505 };
506 void __user *base;
507 void *kbufs[5];
508 struct the_nilfs *nilfs;
509 size_t len, nsegs;
510 int n, ret;
511
Koji Sato7942b912009-04-06 19:01:41 -0700512 if (!capable(CAP_SYS_ADMIN))
513 return -EPERM;
Ryusuke Konishi4f6b8282009-05-10 22:41:43 +0900514
Ryusuke Konishi75124872010-01-26 13:59:40 +0900515 ret = mnt_want_write(filp->f_path.mnt);
516 if (ret)
517 return ret;
Ryusuke Konishi4f6b8282009-05-10 22:41:43 +0900518
Ryusuke Konishi75124872010-01-26 13:59:40 +0900519 ret = -EFAULT;
520 if (copy_from_user(argv, argp, sizeof(argv)))
521 goto out;
522
523 ret = -EINVAL;
Ryusuke Konishi4f6b8282009-05-10 22:41:43 +0900524 nsegs = argv[4].v_nmembs;
525 if (argv[4].v_size != argsz[4])
Ryusuke Konishi75124872010-01-26 13:59:40 +0900526 goto out;
527
Ryusuke Konishi4f6b8282009-05-10 22:41:43 +0900528 /*
529 * argv[4] points to segment numbers this ioctl cleans. We
530 * use kmalloc() for its buffer because memory used for the
531 * segment numbers is enough small.
532 */
533 kbufs[4] = memdup_user((void __user *)(unsigned long)argv[4].v_base,
534 nsegs * sizeof(__u64));
Ryusuke Konishi75124872010-01-26 13:59:40 +0900535 if (IS_ERR(kbufs[4])) {
536 ret = PTR_ERR(kbufs[4]);
537 goto out;
538 }
Ryusuke Konishi4f6b8282009-05-10 22:41:43 +0900539 nilfs = NILFS_SB(inode->i_sb)->s_nilfs;
540
541 for (n = 0; n < 4; n++) {
542 ret = -EINVAL;
543 if (argv[n].v_size != argsz[n])
544 goto out_free;
545
546 if (argv[n].v_nmembs > nsegs * nilfs->ns_blocks_per_segment)
547 goto out_free;
548
549 len = argv[n].v_size * argv[n].v_nmembs;
550 base = (void __user *)(unsigned long)argv[n].v_base;
551 if (len == 0) {
552 kbufs[n] = NULL;
553 continue;
554 }
555
556 kbufs[n] = vmalloc(len);
557 if (!kbufs[n]) {
558 ret = -ENOMEM;
559 goto out_free;
560 }
561 if (copy_from_user(kbufs[n], base, len)) {
562 ret = -EFAULT;
563 vfree(kbufs[n]);
564 goto out_free;
565 }
566 }
567
Jiro SEKIBA1cf58fa2009-09-03 22:24:17 +0900568 /*
569 * nilfs_ioctl_move_blocks() will call nilfs_gc_iget(),
570 * which will operates an inode list without blocking.
571 * To protect the list from concurrent operations,
572 * nilfs_ioctl_move_blocks should be atomic operation.
573 */
574 if (test_and_set_bit(THE_NILFS_GC_RUNNING, &nilfs->ns_flags)) {
575 ret = -EBUSY;
576 goto out_free;
577 }
578
579 ret = nilfs_ioctl_move_blocks(nilfs, &argv[0], kbufs[0]);
580 if (ret < 0)
581 printk(KERN_ERR "NILFS: GC failed during preparation: "
582 "cannot read source blocks: err=%d\n", ret);
583 else
584 ret = nilfs_clean_segments(inode->i_sb, argv, kbufs);
585
Ryusuke Konishic0832342009-11-08 12:09:24 +0900586 if (ret < 0)
587 nilfs_remove_all_gcinode(nilfs);
Jiro SEKIBA1cf58fa2009-09-03 22:24:17 +0900588 clear_nilfs_gc_running(nilfs);
Ryusuke Konishi4f6b8282009-05-10 22:41:43 +0900589
Ryusuke Konishi75124872010-01-26 13:59:40 +0900590out_free:
Ryusuke Konishid5046852009-05-22 20:36:21 +0900591 while (--n >= 0)
Ryusuke Konishi4f6b8282009-05-10 22:41:43 +0900592 vfree(kbufs[n]);
593 kfree(kbufs[4]);
Ryusuke Konishi75124872010-01-26 13:59:40 +0900594out:
595 mnt_drop_write(filp->f_path.mnt);
Ryusuke Konishi4f6b8282009-05-10 22:41:43 +0900596 return ret;
Koji Sato7942b912009-04-06 19:01:41 -0700597}
598
599static int nilfs_ioctl_sync(struct inode *inode, struct file *filp,
600 unsigned int cmd, void __user *argp)
601{
602 __u64 cno;
603 int ret;
Jiro SEKIBA0d561f12010-02-20 19:47:49 +0900604 struct the_nilfs *nilfs;
Koji Sato7942b912009-04-06 19:01:41 -0700605
606 ret = nilfs_construct_segment(inode->i_sb);
607 if (ret < 0)
608 return ret;
609
610 if (argp != NULL) {
Jiro SEKIBA0d561f12010-02-20 19:47:49 +0900611 nilfs = NILFS_SB(inode->i_sb)->s_nilfs;
612 down_read(&nilfs->ns_segctor_sem);
613 cno = nilfs->ns_cno - 1;
614 up_read(&nilfs->ns_segctor_sem);
Koji Sato7942b912009-04-06 19:01:41 -0700615 if (copy_to_user(argp, &cno, sizeof(cno)))
616 return -EFAULT;
617 }
618 return 0;
619}
620
Ryusuke Konishi47eb6b92009-04-30 02:21:00 +0900621static int nilfs_ioctl_get_info(struct inode *inode, struct file *filp,
622 unsigned int cmd, void __user *argp,
Ryusuke Konishi83aca8f2009-05-11 23:24:47 +0900623 size_t membsz,
Ryusuke Konishi47eb6b92009-04-30 02:21:00 +0900624 ssize_t (*dofunc)(struct the_nilfs *,
625 __u64 *, int,
626 void *, size_t, size_t))
627
628{
629 struct the_nilfs *nilfs = NILFS_SB(inode->i_sb)->s_nilfs;
630 struct nilfs_argv argv;
631 int ret;
632
633 if (copy_from_user(&argv, argp, sizeof(argv)))
634 return -EFAULT;
635
Ryusuke Konishi003ff182009-05-12 03:58:47 +0900636 if (argv.v_size < membsz)
Ryusuke Konishi83aca8f2009-05-11 23:24:47 +0900637 return -EINVAL;
638
Ryusuke Konishi47eb6b92009-04-30 02:21:00 +0900639 ret = nilfs_ioctl_wrap_copy(nilfs, &argv, _IOC_DIR(cmd), dofunc);
640 if (ret < 0)
641 return ret;
642
643 if (copy_to_user(argp, &argv, sizeof(argv)))
644 ret = -EFAULT;
645 return ret;
646}
647
Ryusuke Konishi7a946192009-04-06 19:01:53 -0700648long nilfs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
Koji Sato7942b912009-04-06 19:01:41 -0700649{
Ryusuke Konishi7a946192009-04-06 19:01:53 -0700650 struct inode *inode = filp->f_dentry->d_inode;
Koji Sato7942b912009-04-06 19:01:41 -0700651 void __user *argp = (void * __user *)arg;
652
653 switch (cmd) {
654 case NILFS_IOCTL_CHANGE_CPMODE:
655 return nilfs_ioctl_change_cpmode(inode, filp, cmd, argp);
656 case NILFS_IOCTL_DELETE_CHECKPOINT:
657 return nilfs_ioctl_delete_checkpoint(inode, filp, cmd, argp);
658 case NILFS_IOCTL_GET_CPINFO:
Ryusuke Konishi47eb6b92009-04-30 02:21:00 +0900659 return nilfs_ioctl_get_info(inode, filp, cmd, argp,
Ryusuke Konishi83aca8f2009-05-11 23:24:47 +0900660 sizeof(struct nilfs_cpinfo),
Ryusuke Konishi47eb6b92009-04-30 02:21:00 +0900661 nilfs_ioctl_do_get_cpinfo);
Koji Sato7942b912009-04-06 19:01:41 -0700662 case NILFS_IOCTL_GET_CPSTAT:
663 return nilfs_ioctl_get_cpstat(inode, filp, cmd, argp);
664 case NILFS_IOCTL_GET_SUINFO:
Ryusuke Konishi47eb6b92009-04-30 02:21:00 +0900665 return nilfs_ioctl_get_info(inode, filp, cmd, argp,
Ryusuke Konishi83aca8f2009-05-11 23:24:47 +0900666 sizeof(struct nilfs_suinfo),
Ryusuke Konishi47eb6b92009-04-30 02:21:00 +0900667 nilfs_ioctl_do_get_suinfo);
Koji Sato7942b912009-04-06 19:01:41 -0700668 case NILFS_IOCTL_GET_SUSTAT:
669 return nilfs_ioctl_get_sustat(inode, filp, cmd, argp);
670 case NILFS_IOCTL_GET_VINFO:
Ryusuke Konishi47eb6b92009-04-30 02:21:00 +0900671 return nilfs_ioctl_get_info(inode, filp, cmd, argp,
Ryusuke Konishi83aca8f2009-05-11 23:24:47 +0900672 sizeof(struct nilfs_vinfo),
Ryusuke Konishi47eb6b92009-04-30 02:21:00 +0900673 nilfs_ioctl_do_get_vinfo);
Koji Sato7942b912009-04-06 19:01:41 -0700674 case NILFS_IOCTL_GET_BDESCS:
675 return nilfs_ioctl_get_bdescs(inode, filp, cmd, argp);
676 case NILFS_IOCTL_CLEAN_SEGMENTS:
677 return nilfs_ioctl_clean_segments(inode, filp, cmd, argp);
Koji Sato7942b912009-04-06 19:01:41 -0700678 case NILFS_IOCTL_SYNC:
679 return nilfs_ioctl_sync(inode, filp, cmd, argp);
680 default:
681 return -ENOTTY;
682 }
683}