blob: 6ea5f872e2de041cb5ba028f2b6f2ff731ed90e4 [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>
Koji Sato7942b912009-04-06 19:01:41 -070029#include <linux/nilfs2_fs.h>
30#include "nilfs.h"
31#include "segment.h"
32#include "bmap.h"
33#include "cpfile.h"
34#include "sufile.h"
35#include "dat.h"
36
37
Koji Sato7942b912009-04-06 19:01:41 -070038static int nilfs_ioctl_wrap_copy(struct the_nilfs *nilfs,
39 struct nilfs_argv *argv, int dir,
40 ssize_t (*dofunc)(struct the_nilfs *,
Ryusuke Konishib028fcf2009-04-06 19:01:47 -070041 __u64 *, int,
Koji Sato7942b912009-04-06 19:01:41 -070042 void *, size_t, size_t))
43{
44 void *buf;
Ryusuke Konishidc498d02009-04-06 19:01:52 -070045 void __user *base = (void __user *)(unsigned long)argv->v_base;
Ryusuke Konishi3358b4a2009-04-06 19:01:43 -070046 size_t maxmembs, total, n;
Koji Sato7942b912009-04-06 19:01:41 -070047 ssize_t nr;
48 int ret, i;
Ryusuke Konishib028fcf2009-04-06 19:01:47 -070049 __u64 pos, ppos;
Koji Sato7942b912009-04-06 19:01:41 -070050
51 if (argv->v_nmembs == 0)
52 return 0;
53
Ryusuke Konishi3358b4a2009-04-06 19:01:43 -070054 if (argv->v_size > PAGE_SIZE)
55 return -EINVAL;
56
57 buf = (void *)__get_free_pages(GFP_NOFS, 0);
58 if (unlikely(!buf))
Koji Sato7942b912009-04-06 19:01:41 -070059 return -ENOMEM;
Ryusuke Konishi3358b4a2009-04-06 19:01:43 -070060 maxmembs = PAGE_SIZE / argv->v_size;
Koji Sato7942b912009-04-06 19:01:41 -070061
62 ret = 0;
63 total = 0;
Ryusuke Konishib028fcf2009-04-06 19:01:47 -070064 pos = argv->v_index;
Koji Sato7942b912009-04-06 19:01:41 -070065 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) &&
Ryusuke Konishidc498d02009-04-06 19:01:52 -070069 copy_from_user(buf, base + argv->v_size * i,
70 argv->v_size * n)) {
Koji Sato7942b912009-04-06 19:01:41 -070071 ret = -EFAULT;
72 break;
73 }
Ryusuke Konishib028fcf2009-04-06 19:01:47 -070074 ppos = pos;
Pekka Enberg8acfbf02009-04-06 19:01:49 -070075 nr = dofunc(nilfs, &pos, argv->v_flags, buf, argv->v_size,
Ryusuke Konishib028fcf2009-04-06 19:01:47 -070076 n);
Koji Sato7942b912009-04-06 19:01:41 -070077 if (nr < 0) {
78 ret = nr;
79 break;
80 }
81 if ((dir & _IOC_READ) &&
Ryusuke Konishidc498d02009-04-06 19:01:52 -070082 copy_to_user(base + argv->v_size * i, buf,
83 argv->v_size * nr)) {
Koji Sato7942b912009-04-06 19:01:41 -070084 ret = -EFAULT;
85 break;
86 }
87 total += nr;
Ryusuke Konishib028fcf2009-04-06 19:01:47 -070088 if ((size_t)nr < n)
89 break;
90 if (pos == ppos)
91 pos += n;
Koji Sato7942b912009-04-06 19:01:41 -070092 }
93 argv->v_nmembs = total;
94
Ryusuke Konishi3358b4a2009-04-06 19:01:43 -070095 free_pages((unsigned long)buf, 0);
Koji Sato7942b912009-04-06 19:01:41 -070096 return ret;
97}
98
99static int nilfs_ioctl_change_cpmode(struct inode *inode, struct file *filp,
100 unsigned int cmd, void __user *argp)
101{
102 struct inode *cpfile = NILFS_SB(inode->i_sb)->s_nilfs->ns_cpfile;
103 struct nilfs_transaction_info ti;
104 struct nilfs_cpmode cpmode;
105 int ret;
106
107 if (!capable(CAP_SYS_ADMIN))
108 return -EPERM;
109 if (copy_from_user(&cpmode, argp, sizeof(cpmode)))
110 return -EFAULT;
111
112 nilfs_transaction_begin(inode->i_sb, &ti, 0);
113 ret = nilfs_cpfile_change_cpmode(
114 cpfile, cpmode.cm_cno, cpmode.cm_mode);
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700115 if (unlikely(ret < 0)) {
116 nilfs_transaction_abort(inode->i_sb);
117 return ret;
118 }
119 nilfs_transaction_commit(inode->i_sb); /* never fails */
Koji Sato7942b912009-04-06 19:01:41 -0700120 return ret;
121}
122
123static int
124nilfs_ioctl_delete_checkpoint(struct inode *inode, struct file *filp,
125 unsigned int cmd, void __user *argp)
126{
127 struct inode *cpfile = NILFS_SB(inode->i_sb)->s_nilfs->ns_cpfile;
128 struct nilfs_transaction_info ti;
129 __u64 cno;
130 int ret;
131
132 if (!capable(CAP_SYS_ADMIN))
133 return -EPERM;
134 if (copy_from_user(&cno, argp, sizeof(cno)))
135 return -EFAULT;
136
137 nilfs_transaction_begin(inode->i_sb, &ti, 0);
138 ret = nilfs_cpfile_delete_checkpoint(cpfile, cno);
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700139 if (unlikely(ret < 0)) {
140 nilfs_transaction_abort(inode->i_sb);
141 return ret;
142 }
143 nilfs_transaction_commit(inode->i_sb); /* never fails */
Koji Sato7942b912009-04-06 19:01:41 -0700144 return ret;
145}
146
147static ssize_t
Ryusuke Konishib028fcf2009-04-06 19:01:47 -0700148nilfs_ioctl_do_get_cpinfo(struct the_nilfs *nilfs, __u64 *posp, int flags,
Koji Sato7942b912009-04-06 19:01:41 -0700149 void *buf, size_t size, size_t nmembs)
150{
Koji Sato7942b912009-04-06 19:01:41 -0700151 int ret;
152
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700153 down_read(&nilfs->ns_segctor_sem);
Ryusuke Konishi47eb6b92009-04-30 02:21:00 +0900154 ret = nilfs_cpfile_get_cpinfo(nilfs->ns_cpfile, posp, flags, buf,
Ryusuke Konishi003ff182009-05-12 03:58:47 +0900155 size, nmembs);
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700156 up_read(&nilfs->ns_segctor_sem);
Koji Sato7942b912009-04-06 19:01:41 -0700157 return ret;
158}
159
160static int nilfs_ioctl_get_cpstat(struct inode *inode, struct file *filp,
161 unsigned int cmd, void __user *argp)
162{
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700163 struct the_nilfs *nilfs = NILFS_SB(inode->i_sb)->s_nilfs;
Koji Sato7942b912009-04-06 19:01:41 -0700164 struct nilfs_cpstat cpstat;
Koji Sato7942b912009-04-06 19:01:41 -0700165 int ret;
166
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700167 down_read(&nilfs->ns_segctor_sem);
168 ret = nilfs_cpfile_get_stat(nilfs->ns_cpfile, &cpstat);
169 up_read(&nilfs->ns_segctor_sem);
Koji Sato7942b912009-04-06 19:01:41 -0700170 if (ret < 0)
171 return ret;
172
173 if (copy_to_user(argp, &cpstat, sizeof(cpstat)))
174 ret = -EFAULT;
175 return ret;
176}
177
178static ssize_t
Ryusuke Konishib028fcf2009-04-06 19:01:47 -0700179nilfs_ioctl_do_get_suinfo(struct the_nilfs *nilfs, __u64 *posp, int flags,
Koji Sato7942b912009-04-06 19:01:41 -0700180 void *buf, size_t size, size_t nmembs)
181{
Koji Sato7942b912009-04-06 19:01:41 -0700182 int ret;
183
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700184 down_read(&nilfs->ns_segctor_sem);
Ryusuke Konishi003ff182009-05-12 03:58:47 +0900185 ret = nilfs_sufile_get_suinfo(nilfs->ns_sufile, *posp, buf, size,
186 nmembs);
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700187 up_read(&nilfs->ns_segctor_sem);
Koji Sato7942b912009-04-06 19:01:41 -0700188 return ret;
189}
190
191static int nilfs_ioctl_get_sustat(struct inode *inode, struct file *filp,
192 unsigned int cmd, void __user *argp)
193{
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700194 struct the_nilfs *nilfs = NILFS_SB(inode->i_sb)->s_nilfs;
Koji Sato7942b912009-04-06 19:01:41 -0700195 struct nilfs_sustat sustat;
Koji Sato7942b912009-04-06 19:01:41 -0700196 int ret;
197
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700198 down_read(&nilfs->ns_segctor_sem);
199 ret = nilfs_sufile_get_stat(nilfs->ns_sufile, &sustat);
200 up_read(&nilfs->ns_segctor_sem);
Koji Sato7942b912009-04-06 19:01:41 -0700201 if (ret < 0)
202 return ret;
203
204 if (copy_to_user(argp, &sustat, sizeof(sustat)))
205 ret = -EFAULT;
206 return ret;
207}
208
209static ssize_t
Ryusuke Konishib028fcf2009-04-06 19:01:47 -0700210nilfs_ioctl_do_get_vinfo(struct the_nilfs *nilfs, __u64 *posp, int flags,
Koji Sato7942b912009-04-06 19:01:41 -0700211 void *buf, size_t size, size_t nmembs)
212{
Koji Sato7942b912009-04-06 19:01:41 -0700213 int ret;
214
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700215 down_read(&nilfs->ns_segctor_sem);
Ryusuke Konishi003ff182009-05-12 03:58:47 +0900216 ret = nilfs_dat_get_vinfo(nilfs_dat_inode(nilfs), buf, size, nmembs);
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700217 up_read(&nilfs->ns_segctor_sem);
Koji Sato7942b912009-04-06 19:01:41 -0700218 return ret;
219}
220
221static ssize_t
Ryusuke Konishib028fcf2009-04-06 19:01:47 -0700222nilfs_ioctl_do_get_bdescs(struct the_nilfs *nilfs, __u64 *posp, int flags,
Koji Sato7942b912009-04-06 19:01:41 -0700223 void *buf, size_t size, size_t nmembs)
224{
225 struct inode *dat = nilfs_dat_inode(nilfs);
226 struct nilfs_bmap *bmap = NILFS_I(dat)->i_bmap;
227 struct nilfs_bdesc *bdescs = buf;
228 int ret, i;
229
Ryusuke Konishi47eb6b92009-04-30 02:21:00 +0900230 down_read(&nilfs->ns_segctor_sem);
Koji Sato7942b912009-04-06 19:01:41 -0700231 for (i = 0; i < nmembs; i++) {
232 ret = nilfs_bmap_lookup_at_level(bmap,
233 bdescs[i].bd_offset,
234 bdescs[i].bd_level + 1,
235 &bdescs[i].bd_blocknr);
236 if (ret < 0) {
Ryusuke Konishi47eb6b92009-04-30 02:21:00 +0900237 if (ret != -ENOENT) {
238 up_read(&nilfs->ns_segctor_sem);
Koji Sato7942b912009-04-06 19:01:41 -0700239 return ret;
Ryusuke Konishi47eb6b92009-04-30 02:21:00 +0900240 }
Koji Sato7942b912009-04-06 19:01:41 -0700241 bdescs[i].bd_blocknr = 0;
242 }
243 }
Ryusuke Konishi47eb6b92009-04-30 02:21:00 +0900244 up_read(&nilfs->ns_segctor_sem);
Koji Sato7942b912009-04-06 19:01:41 -0700245 return nmembs;
246}
247
248static int nilfs_ioctl_get_bdescs(struct inode *inode, struct file *filp,
249 unsigned int cmd, void __user *argp)
250{
251 struct the_nilfs *nilfs = NILFS_SB(inode->i_sb)->s_nilfs;
252 struct nilfs_argv argv;
Koji Sato7942b912009-04-06 19:01:41 -0700253 int ret;
254
255 if (copy_from_user(&argv, argp, sizeof(argv)))
256 return -EFAULT;
257
Ryusuke Konishi83aca8f2009-05-11 23:24:47 +0900258 if (argv.v_size != sizeof(struct nilfs_bdesc))
259 return -EINVAL;
260
Koji Sato7942b912009-04-06 19:01:41 -0700261 ret = nilfs_ioctl_wrap_copy(nilfs, &argv, _IOC_DIR(cmd),
262 nilfs_ioctl_do_get_bdescs);
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700263 if (ret < 0)
264 return ret;
Koji Sato7942b912009-04-06 19:01:41 -0700265
266 if (copy_to_user(argp, &argv, sizeof(argv)))
267 ret = -EFAULT;
268 return ret;
269}
270
271static int nilfs_ioctl_move_inode_block(struct inode *inode,
272 struct nilfs_vdesc *vdesc,
273 struct list_head *buffers)
274{
275 struct buffer_head *bh;
276 int ret;
277
278 if (vdesc->vd_flags == 0)
279 ret = nilfs_gccache_submit_read_data(
280 inode, vdesc->vd_offset, vdesc->vd_blocknr,
281 vdesc->vd_vblocknr, &bh);
282 else
283 ret = nilfs_gccache_submit_read_node(
284 inode, vdesc->vd_blocknr, vdesc->vd_vblocknr, &bh);
285
286 if (unlikely(ret < 0)) {
287 if (ret == -ENOENT)
288 printk(KERN_CRIT
289 "%s: invalid virtual block address (%s): "
290 "ino=%llu, cno=%llu, offset=%llu, "
291 "blocknr=%llu, vblocknr=%llu\n",
292 __func__, vdesc->vd_flags ? "node" : "data",
293 (unsigned long long)vdesc->vd_ino,
294 (unsigned long long)vdesc->vd_cno,
295 (unsigned long long)vdesc->vd_offset,
296 (unsigned long long)vdesc->vd_blocknr,
297 (unsigned long long)vdesc->vd_vblocknr);
298 return ret;
299 }
300 bh->b_private = vdesc;
301 list_add_tail(&bh->b_assoc_buffers, buffers);
302 return 0;
303}
304
Ryusuke Konishi4f6b8282009-05-10 22:41:43 +0900305static int nilfs_ioctl_move_blocks(struct the_nilfs *nilfs,
306 struct nilfs_argv *argv, void *buf)
Koji Sato7942b912009-04-06 19:01:41 -0700307{
Ryusuke Konishi4f6b8282009-05-10 22:41:43 +0900308 size_t nmembs = argv->v_nmembs;
Koji Sato7942b912009-04-06 19:01:41 -0700309 struct inode *inode;
310 struct nilfs_vdesc *vdesc;
311 struct buffer_head *bh, *n;
312 LIST_HEAD(buffers);
313 ino_t ino;
314 __u64 cno;
315 int i, ret;
316
317 for (i = 0, vdesc = buf; i < nmembs; ) {
318 ino = vdesc->vd_ino;
319 cno = vdesc->vd_cno;
320 inode = nilfs_gc_iget(nilfs, ino, cno);
321 if (unlikely(inode == NULL)) {
322 ret = -ENOMEM;
323 goto failed;
324 }
325 do {
326 ret = nilfs_ioctl_move_inode_block(inode, vdesc,
327 &buffers);
328 if (unlikely(ret < 0))
329 goto failed;
330 vdesc++;
331 } while (++i < nmembs &&
332 vdesc->vd_ino == ino && vdesc->vd_cno == cno);
333 }
334
335 list_for_each_entry_safe(bh, n, &buffers, b_assoc_buffers) {
336 ret = nilfs_gccache_wait_and_mark_dirty(bh);
337 if (unlikely(ret < 0)) {
338 if (ret == -EEXIST) {
339 vdesc = bh->b_private;
340 printk(KERN_CRIT
341 "%s: conflicting %s buffer: "
342 "ino=%llu, cno=%llu, offset=%llu, "
343 "blocknr=%llu, vblocknr=%llu\n",
344 __func__,
345 vdesc->vd_flags ? "node" : "data",
346 (unsigned long long)vdesc->vd_ino,
347 (unsigned long long)vdesc->vd_cno,
348 (unsigned long long)vdesc->vd_offset,
349 (unsigned long long)vdesc->vd_blocknr,
350 (unsigned long long)vdesc->vd_vblocknr);
351 }
352 goto failed;
353 }
354 list_del_init(&bh->b_assoc_buffers);
355 bh->b_private = NULL;
356 brelse(bh);
357 }
358 return nmembs;
359
360 failed:
361 list_for_each_entry_safe(bh, n, &buffers, b_assoc_buffers) {
362 list_del_init(&bh->b_assoc_buffers);
363 bh->b_private = NULL;
364 brelse(bh);
365 }
366 return ret;
367}
368
Ryusuke Konishi4f6b8282009-05-10 22:41:43 +0900369static int nilfs_ioctl_delete_checkpoints(struct the_nilfs *nilfs,
370 struct nilfs_argv *argv, void *buf)
Koji Sato7942b912009-04-06 19:01:41 -0700371{
Ryusuke Konishi4f6b8282009-05-10 22:41:43 +0900372 size_t nmembs = argv->v_nmembs;
Koji Sato7942b912009-04-06 19:01:41 -0700373 struct inode *cpfile = nilfs->ns_cpfile;
374 struct nilfs_period *periods = buf;
375 int ret, i;
376
377 for (i = 0; i < nmembs; i++) {
378 ret = nilfs_cpfile_delete_checkpoints(
379 cpfile, periods[i].p_start, periods[i].p_end);
380 if (ret < 0)
381 return ret;
382 }
383 return nmembs;
384}
385
Ryusuke Konishi4f6b8282009-05-10 22:41:43 +0900386static int nilfs_ioctl_free_vblocknrs(struct the_nilfs *nilfs,
387 struct nilfs_argv *argv, void *buf)
Koji Sato7942b912009-04-06 19:01:41 -0700388{
Ryusuke Konishi4f6b8282009-05-10 22:41:43 +0900389 size_t nmembs = argv->v_nmembs;
390 int ret;
Koji Sato7942b912009-04-06 19:01:41 -0700391
Ryusuke Konishi4f6b8282009-05-10 22:41:43 +0900392 ret = nilfs_dat_freev(nilfs_dat_inode(nilfs), buf, nmembs);
Koji Sato7942b912009-04-06 19:01:41 -0700393
394 return (ret < 0) ? ret : nmembs;
395}
396
Ryusuke Konishi4f6b8282009-05-10 22:41:43 +0900397static int nilfs_ioctl_mark_blocks_dirty(struct the_nilfs *nilfs,
398 struct nilfs_argv *argv, void *buf)
Koji Sato7942b912009-04-06 19:01:41 -0700399{
Ryusuke Konishi4f6b8282009-05-10 22:41:43 +0900400 size_t nmembs = argv->v_nmembs;
Koji Sato7942b912009-04-06 19:01:41 -0700401 struct inode *dat = nilfs_dat_inode(nilfs);
402 struct nilfs_bmap *bmap = NILFS_I(dat)->i_bmap;
403 struct nilfs_bdesc *bdescs = buf;
404 int ret, i;
405
406 for (i = 0; i < nmembs; i++) {
407 /* XXX: use macro or inline func to check liveness */
408 ret = nilfs_bmap_lookup_at_level(bmap,
409 bdescs[i].bd_offset,
410 bdescs[i].bd_level + 1,
411 &bdescs[i].bd_blocknr);
412 if (ret < 0) {
413 if (ret != -ENOENT)
414 return ret;
415 bdescs[i].bd_blocknr = 0;
416 }
417 if (bdescs[i].bd_blocknr != bdescs[i].bd_oblocknr)
418 /* skip dead block */
419 continue;
420 if (bdescs[i].bd_level == 0) {
421 ret = nilfs_mdt_mark_block_dirty(dat,
422 bdescs[i].bd_offset);
423 if (ret < 0) {
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -0700424 WARN_ON(ret == -ENOENT);
Koji Sato7942b912009-04-06 19:01:41 -0700425 return ret;
426 }
427 } else {
428 ret = nilfs_bmap_mark(bmap, bdescs[i].bd_offset,
429 bdescs[i].bd_level);
430 if (ret < 0) {
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -0700431 WARN_ON(ret == -ENOENT);
Koji Sato7942b912009-04-06 19:01:41 -0700432 return ret;
433 }
434 }
435 }
436 return nmembs;
437}
438
Koji Sato7942b912009-04-06 19:01:41 -0700439int nilfs_ioctl_prepare_clean_segments(struct the_nilfs *nilfs,
Ryusuke Konishi4f6b8282009-05-10 22:41:43 +0900440 struct nilfs_argv *argv, void **kbufs)
Koji Sato7942b912009-04-06 19:01:41 -0700441{
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -0700442 const char *msg;
Ryusuke Konishi4f6b8282009-05-10 22:41:43 +0900443 int ret;
Koji Sato7942b912009-04-06 19:01:41 -0700444
Ryusuke Konishi4f6b8282009-05-10 22:41:43 +0900445 ret = nilfs_ioctl_move_blocks(nilfs, &argv[0], kbufs[0]);
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -0700446 if (ret < 0) {
447 msg = "cannot read source blocks";
448 goto failed;
449 }
Ryusuke Konishi4f6b8282009-05-10 22:41:43 +0900450
451 ret = nilfs_ioctl_delete_checkpoints(nilfs, &argv[1], kbufs[1]);
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -0700452 if (ret < 0) {
453 /*
454 * can safely abort because checkpoints can be removed
455 * independently.
456 */
457 msg = "cannot delete checkpoints";
458 goto failed;
459 }
Ryusuke Konishi4f6b8282009-05-10 22:41:43 +0900460 ret = nilfs_ioctl_free_vblocknrs(nilfs, &argv[2], kbufs[2]);
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -0700461 if (ret < 0) {
462 /*
463 * can safely abort because DAT file is updated atomically
464 * using a copy-on-write technique.
465 */
466 msg = "cannot delete virtual blocks from DAT file";
467 goto failed;
468 }
Ryusuke Konishi4f6b8282009-05-10 22:41:43 +0900469 ret = nilfs_ioctl_mark_blocks_dirty(nilfs, &argv[3], kbufs[3]);
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -0700470 if (ret < 0) {
471 /*
472 * can safely abort because the operation is nondestructive.
473 */
474 msg = "cannot mark copying blocks dirty";
475 goto failed;
476 }
Koji Sato7942b912009-04-06 19:01:41 -0700477 return 0;
478
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -0700479 failed:
Koji Sato7942b912009-04-06 19:01:41 -0700480 nilfs_remove_all_gcinode(nilfs);
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -0700481 printk(KERN_ERR "NILFS: GC failed during preparation: %s: err=%d\n",
482 msg, ret);
Koji Sato7942b912009-04-06 19:01:41 -0700483 return ret;
484}
485
486static int nilfs_ioctl_clean_segments(struct inode *inode, struct file *filp,
487 unsigned int cmd, void __user *argp)
488{
Ryusuke Konishi4f6b8282009-05-10 22:41:43 +0900489 struct nilfs_argv argv[5];
490 const static size_t argsz[5] = {
491 sizeof(struct nilfs_vdesc),
492 sizeof(struct nilfs_period),
493 sizeof(__u64),
494 sizeof(struct nilfs_bdesc),
495 sizeof(__u64),
496 };
497 void __user *base;
498 void *kbufs[5];
499 struct the_nilfs *nilfs;
500 size_t len, nsegs;
501 int n, ret;
502
Koji Sato7942b912009-04-06 19:01:41 -0700503 if (!capable(CAP_SYS_ADMIN))
504 return -EPERM;
Ryusuke Konishi4f6b8282009-05-10 22:41:43 +0900505
506 if (copy_from_user(argv, argp, sizeof(argv)))
507 return -EFAULT;
508
509 nsegs = argv[4].v_nmembs;
510 if (argv[4].v_size != argsz[4])
511 return -EINVAL;
512 /*
513 * argv[4] points to segment numbers this ioctl cleans. We
514 * use kmalloc() for its buffer because memory used for the
515 * segment numbers is enough small.
516 */
517 kbufs[4] = memdup_user((void __user *)(unsigned long)argv[4].v_base,
518 nsegs * sizeof(__u64));
519 if (IS_ERR(kbufs[4]))
520 return PTR_ERR(kbufs[4]);
521
522 nilfs = NILFS_SB(inode->i_sb)->s_nilfs;
523
524 for (n = 0; n < 4; n++) {
525 ret = -EINVAL;
526 if (argv[n].v_size != argsz[n])
527 goto out_free;
528
529 if (argv[n].v_nmembs > nsegs * nilfs->ns_blocks_per_segment)
530 goto out_free;
531
532 len = argv[n].v_size * argv[n].v_nmembs;
533 base = (void __user *)(unsigned long)argv[n].v_base;
534 if (len == 0) {
535 kbufs[n] = NULL;
536 continue;
537 }
538
539 kbufs[n] = vmalloc(len);
540 if (!kbufs[n]) {
541 ret = -ENOMEM;
542 goto out_free;
543 }
544 if (copy_from_user(kbufs[n], base, len)) {
545 ret = -EFAULT;
546 vfree(kbufs[n]);
547 goto out_free;
548 }
549 }
550
551 ret = nilfs_clean_segments(inode->i_sb, argv, kbufs);
552
553 out_free:
Ryusuke Konishid50468532009-05-22 20:36:21 +0900554 while (--n >= 0)
Ryusuke Konishi4f6b8282009-05-10 22:41:43 +0900555 vfree(kbufs[n]);
556 kfree(kbufs[4]);
557 return ret;
Koji Sato7942b912009-04-06 19:01:41 -0700558}
559
560static int nilfs_ioctl_sync(struct inode *inode, struct file *filp,
561 unsigned int cmd, void __user *argp)
562{
563 __u64 cno;
564 int ret;
565
566 ret = nilfs_construct_segment(inode->i_sb);
567 if (ret < 0)
568 return ret;
569
570 if (argp != NULL) {
571 cno = NILFS_SB(inode->i_sb)->s_nilfs->ns_cno - 1;
572 if (copy_to_user(argp, &cno, sizeof(cno)))
573 return -EFAULT;
574 }
575 return 0;
576}
577
Ryusuke Konishi47eb6b92009-04-30 02:21:00 +0900578static int nilfs_ioctl_get_info(struct inode *inode, struct file *filp,
579 unsigned int cmd, void __user *argp,
Ryusuke Konishi83aca8f2009-05-11 23:24:47 +0900580 size_t membsz,
Ryusuke Konishi47eb6b92009-04-30 02:21:00 +0900581 ssize_t (*dofunc)(struct the_nilfs *,
582 __u64 *, int,
583 void *, size_t, size_t))
584
585{
586 struct the_nilfs *nilfs = NILFS_SB(inode->i_sb)->s_nilfs;
587 struct nilfs_argv argv;
588 int ret;
589
590 if (copy_from_user(&argv, argp, sizeof(argv)))
591 return -EFAULT;
592
Ryusuke Konishi003ff182009-05-12 03:58:47 +0900593 if (argv.v_size < membsz)
Ryusuke Konishi83aca8f2009-05-11 23:24:47 +0900594 return -EINVAL;
595
Ryusuke Konishi47eb6b92009-04-30 02:21:00 +0900596 ret = nilfs_ioctl_wrap_copy(nilfs, &argv, _IOC_DIR(cmd), dofunc);
597 if (ret < 0)
598 return ret;
599
600 if (copy_to_user(argp, &argv, sizeof(argv)))
601 ret = -EFAULT;
602 return ret;
603}
604
Ryusuke Konishi7a946192009-04-06 19:01:53 -0700605long nilfs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
Koji Sato7942b912009-04-06 19:01:41 -0700606{
Ryusuke Konishi7a946192009-04-06 19:01:53 -0700607 struct inode *inode = filp->f_dentry->d_inode;
Koji Sato7942b912009-04-06 19:01:41 -0700608 void __user *argp = (void * __user *)arg;
609
610 switch (cmd) {
611 case NILFS_IOCTL_CHANGE_CPMODE:
612 return nilfs_ioctl_change_cpmode(inode, filp, cmd, argp);
613 case NILFS_IOCTL_DELETE_CHECKPOINT:
614 return nilfs_ioctl_delete_checkpoint(inode, filp, cmd, argp);
615 case NILFS_IOCTL_GET_CPINFO:
Ryusuke Konishi47eb6b92009-04-30 02:21:00 +0900616 return nilfs_ioctl_get_info(inode, filp, cmd, argp,
Ryusuke Konishi83aca8f2009-05-11 23:24:47 +0900617 sizeof(struct nilfs_cpinfo),
Ryusuke Konishi47eb6b92009-04-30 02:21:00 +0900618 nilfs_ioctl_do_get_cpinfo);
Koji Sato7942b912009-04-06 19:01:41 -0700619 case NILFS_IOCTL_GET_CPSTAT:
620 return nilfs_ioctl_get_cpstat(inode, filp, cmd, argp);
621 case NILFS_IOCTL_GET_SUINFO:
Ryusuke Konishi47eb6b92009-04-30 02:21:00 +0900622 return nilfs_ioctl_get_info(inode, filp, cmd, argp,
Ryusuke Konishi83aca8f2009-05-11 23:24:47 +0900623 sizeof(struct nilfs_suinfo),
Ryusuke Konishi47eb6b92009-04-30 02:21:00 +0900624 nilfs_ioctl_do_get_suinfo);
Koji Sato7942b912009-04-06 19:01:41 -0700625 case NILFS_IOCTL_GET_SUSTAT:
626 return nilfs_ioctl_get_sustat(inode, filp, cmd, argp);
627 case NILFS_IOCTL_GET_VINFO:
Ryusuke Konishi47eb6b92009-04-30 02:21:00 +0900628 return nilfs_ioctl_get_info(inode, filp, cmd, argp,
Ryusuke Konishi83aca8f2009-05-11 23:24:47 +0900629 sizeof(struct nilfs_vinfo),
Ryusuke Konishi47eb6b92009-04-30 02:21:00 +0900630 nilfs_ioctl_do_get_vinfo);
Koji Sato7942b912009-04-06 19:01:41 -0700631 case NILFS_IOCTL_GET_BDESCS:
632 return nilfs_ioctl_get_bdescs(inode, filp, cmd, argp);
633 case NILFS_IOCTL_CLEAN_SEGMENTS:
634 return nilfs_ioctl_clean_segments(inode, filp, cmd, argp);
Koji Sato7942b912009-04-06 19:01:41 -0700635 case NILFS_IOCTL_SYNC:
636 return nilfs_ioctl_sync(inode, filp, cmd, argp);
637 default:
638 return -ENOTTY;
639 }
640}