aboutsummaryrefslogtreecommitdiffstats
path: root/fs
diff options
context:
space:
mode:
authorKoji Sato <sato.koji@lab.ntt.co.jp>2009-04-06 22:01:41 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2009-04-07 11:31:16 -0400
commit7942b919f7321f95a777d396ff7894a7a83dc9b0 (patch)
tree60c63349788f8776c3ecd9ac1e02edc2c7f8769e /fs
parenta3d93f709e893187d301aa5458b2248db9f22bd1 (diff)
nilfs2: ioctl operations
This adds userland interface implemented with ioctl. Signed-off-by: Koji Sato <sato.koji@lab.ntt.co.jp> Signed-off-by: Ryusuke Konishi <konishi.ryusuke@lab.ntt.co.jp> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'fs')
-rw-r--r--fs/nilfs2/ioctl.c941
1 files changed, 941 insertions, 0 deletions
diff --git a/fs/nilfs2/ioctl.c b/fs/nilfs2/ioctl.c
new file mode 100644
index 000000000000..35ba60ea9617
--- /dev/null
+++ b/fs/nilfs2/ioctl.c
@@ -0,0 +1,941 @@
1/*
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() */
28#include <linux/nilfs2_fs.h>
29#include "nilfs.h"
30#include "segment.h"
31#include "bmap.h"
32#include "cpfile.h"
33#include "sufile.h"
34#include "dat.h"
35
36
37#define KMALLOC_SIZE_MIN 4096 /* 4KB */
38#define KMALLOC_SIZE_MAX 131072 /* 128 KB */
39
40static int nilfs_ioctl_wrap_copy(struct the_nilfs *nilfs,
41 struct nilfs_argv *argv, int dir,
42 ssize_t (*dofunc)(struct the_nilfs *,
43 int, int,
44 void *, size_t, size_t))
45{
46 void *buf;
47 size_t ksize, maxmembs, total, n;
48 ssize_t nr;
49 int ret, i;
50
51 if (argv->v_nmembs == 0)
52 return 0;
53
54 for (ksize = KMALLOC_SIZE_MAX; ksize >= KMALLOC_SIZE_MIN; ksize /= 2) {
55 buf = kmalloc(ksize, GFP_NOFS);
56 if (buf != NULL)
57 break;
58 }
59 if (ksize < KMALLOC_SIZE_MIN)
60 return -ENOMEM;
61 maxmembs = ksize / argv->v_size;
62
63 ret = 0;
64 total = 0;
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,
70 (void __user *)argv->v_base + argv->v_size * i,
71 argv->v_size * n)) {
72 ret = -EFAULT;
73 break;
74 }
75 nr = (*dofunc)(nilfs, argv->v_index + i, argv->v_flags, buf,
76 argv->v_size, n);
77 if (nr < 0) {
78 ret = nr;
79 break;
80 }
81 if ((dir & _IOC_READ) &&
82 copy_to_user(
83 (void __user *)argv->v_base + argv->v_size * i,
84 buf, argv->v_size * nr)) {
85 ret = -EFAULT;
86 break;
87 }
88 total += nr;
89 }
90 argv->v_nmembs = total;
91
92 kfree(buf);
93 return ret;
94}
95
96static int nilfs_ioctl_change_cpmode(struct inode *inode, struct file *filp,
97 unsigned int cmd, void __user *argp)
98{
99 struct inode *cpfile = NILFS_SB(inode->i_sb)->s_nilfs->ns_cpfile;
100 struct nilfs_transaction_info ti;
101 struct nilfs_cpmode cpmode;
102 int ret;
103
104 if (!capable(CAP_SYS_ADMIN))
105 return -EPERM;
106 if (copy_from_user(&cpmode, argp, sizeof(cpmode)))
107 return -EFAULT;
108
109 nilfs_transaction_begin(inode->i_sb, &ti, 0);
110 ret = nilfs_cpfile_change_cpmode(
111 cpfile, cpmode.cm_cno, cpmode.cm_mode);
112 nilfs_transaction_end(inode->i_sb, !ret);
113 return ret;
114}
115
116static int
117nilfs_ioctl_delete_checkpoint(struct inode *inode, struct file *filp,
118 unsigned int cmd, void __user *argp)
119{
120 struct inode *cpfile = NILFS_SB(inode->i_sb)->s_nilfs->ns_cpfile;
121 struct nilfs_transaction_info ti;
122 __u64 cno;
123 int ret;
124
125 if (!capable(CAP_SYS_ADMIN))
126 return -EPERM;
127 if (copy_from_user(&cno, argp, sizeof(cno)))
128 return -EFAULT;
129
130 nilfs_transaction_begin(inode->i_sb, &ti, 0);
131 ret = nilfs_cpfile_delete_checkpoint(cpfile, cno);
132 nilfs_transaction_end(inode->i_sb, !ret);
133 return ret;
134}
135
136static ssize_t
137nilfs_ioctl_do_get_cpinfo(struct the_nilfs *nilfs, int index, int flags,
138 void *buf, size_t size, size_t nmembs)
139{
140 return nilfs_cpfile_get_cpinfo(nilfs->ns_cpfile, index, flags, buf,
141 nmembs);
142}
143
144static int nilfs_ioctl_get_cpinfo(struct inode *inode, struct file *filp,
145 unsigned int cmd, void __user *argp)
146{
147 struct the_nilfs *nilfs = NILFS_SB(inode->i_sb)->s_nilfs;
148 struct nilfs_argv argv;
149 struct nilfs_transaction_info ti;
150 int ret;
151
152 if (copy_from_user(&argv, argp, sizeof(argv)))
153 return -EFAULT;
154
155 nilfs_transaction_begin(inode->i_sb, &ti, 0);
156 ret = nilfs_ioctl_wrap_copy(nilfs, &argv, _IOC_DIR(cmd),
157 nilfs_ioctl_do_get_cpinfo);
158 nilfs_transaction_end(inode->i_sb, 0);
159
160 if (copy_to_user(argp, &argv, sizeof(argv)))
161 ret = -EFAULT;
162 return ret;
163}
164
165static int nilfs_ioctl_get_cpstat(struct inode *inode, struct file *filp,
166 unsigned int cmd, void __user *argp)
167{
168 struct inode *cpfile = NILFS_SB(inode->i_sb)->s_nilfs->ns_cpfile;
169 struct nilfs_cpstat cpstat;
170 struct nilfs_transaction_info ti;
171 int ret;
172
173 nilfs_transaction_begin(inode->i_sb, &ti, 0);
174 ret = nilfs_cpfile_get_stat(cpfile, &cpstat);
175 nilfs_transaction_end(inode->i_sb, 0);
176 if (ret < 0)
177 return ret;
178
179 if (copy_to_user(argp, &cpstat, sizeof(cpstat)))
180 ret = -EFAULT;
181 return ret;
182}
183
184static ssize_t
185nilfs_ioctl_do_get_suinfo(struct the_nilfs *nilfs, int index, int flags,
186 void *buf, size_t size, size_t nmembs)
187{
188 return nilfs_sufile_get_suinfo(nilfs->ns_sufile, index, buf, nmembs);
189}
190
191static int nilfs_ioctl_get_suinfo(struct inode *inode, struct file *filp,
192 unsigned int cmd, void __user *argp)
193{
194 struct the_nilfs *nilfs = NILFS_SB(inode->i_sb)->s_nilfs;
195 struct nilfs_argv argv;
196 struct nilfs_transaction_info ti;
197 int ret;
198
199 if (copy_from_user(&argv, argp, sizeof(argv)))
200 return -EFAULT;
201
202 nilfs_transaction_begin(inode->i_sb, &ti, 0);
203 ret = nilfs_ioctl_wrap_copy(nilfs, &argv, _IOC_DIR(cmd),
204 nilfs_ioctl_do_get_suinfo);
205 nilfs_transaction_end(inode->i_sb, 0);
206
207 if (copy_to_user(argp, &argv, sizeof(argv)))
208 ret = -EFAULT;
209 return ret;
210}
211
212static int nilfs_ioctl_get_sustat(struct inode *inode, struct file *filp,
213 unsigned int cmd, void __user *argp)
214{
215 struct inode *sufile = NILFS_SB(inode->i_sb)->s_nilfs->ns_sufile;
216 struct nilfs_sustat sustat;
217 struct nilfs_transaction_info ti;
218 int ret;
219
220 nilfs_transaction_begin(inode->i_sb, &ti, 0);
221 ret = nilfs_sufile_get_stat(sufile, &sustat);
222 nilfs_transaction_end(inode->i_sb, 0);
223 if (ret < 0)
224 return ret;
225
226 if (copy_to_user(argp, &sustat, sizeof(sustat)))
227 ret = -EFAULT;
228 return ret;
229}
230
231static ssize_t
232nilfs_ioctl_do_get_vinfo(struct the_nilfs *nilfs, int index, int flags,
233 void *buf, size_t size, size_t nmembs)
234{
235 return nilfs_dat_get_vinfo(nilfs_dat_inode(nilfs), buf, nmembs);
236}
237
238static int nilfs_ioctl_get_vinfo(struct inode *inode, struct file *filp,
239 unsigned int cmd, void __user *argp)
240{
241 struct the_nilfs *nilfs = NILFS_SB(inode->i_sb)->s_nilfs;
242 struct nilfs_argv argv;
243 struct nilfs_transaction_info ti;
244 int ret;
245
246 if (copy_from_user(&argv, argp, sizeof(argv)))
247 return -EFAULT;
248
249 nilfs_transaction_begin(inode->i_sb, &ti, 0);
250 ret = nilfs_ioctl_wrap_copy(nilfs, &argv, _IOC_DIR(cmd),
251 nilfs_ioctl_do_get_vinfo);
252 nilfs_transaction_end(inode->i_sb, 0);
253
254 if (copy_to_user(argp, &argv, sizeof(argv)))
255 ret = -EFAULT;
256 return ret;
257}
258
259static ssize_t
260nilfs_ioctl_do_get_bdescs(struct the_nilfs *nilfs, int index, int flags,
261 void *buf, size_t size, size_t nmembs)
262{
263 struct inode *dat = nilfs_dat_inode(nilfs);
264 struct nilfs_bmap *bmap = NILFS_I(dat)->i_bmap;
265 struct nilfs_bdesc *bdescs = buf;
266 int ret, i;
267
268 for (i = 0; i < nmembs; i++) {
269 ret = nilfs_bmap_lookup_at_level(bmap,
270 bdescs[i].bd_offset,
271 bdescs[i].bd_level + 1,
272 &bdescs[i].bd_blocknr);
273 if (ret < 0) {
274 if (ret != -ENOENT)
275 return ret;
276 bdescs[i].bd_blocknr = 0;
277 }
278 }
279 return nmembs;
280}
281
282static int nilfs_ioctl_get_bdescs(struct inode *inode, struct file *filp,
283 unsigned int cmd, void __user *argp)
284{
285 struct the_nilfs *nilfs = NILFS_SB(inode->i_sb)->s_nilfs;
286 struct nilfs_argv argv;
287 struct nilfs_transaction_info ti;
288 int ret;
289
290 if (copy_from_user(&argv, argp, sizeof(argv)))
291 return -EFAULT;
292
293 nilfs_transaction_begin(inode->i_sb, &ti, 0);
294 ret = nilfs_ioctl_wrap_copy(nilfs, &argv, _IOC_DIR(cmd),
295 nilfs_ioctl_do_get_bdescs);
296 nilfs_transaction_end(inode->i_sb, 0);
297
298 if (copy_to_user(argp, &argv, sizeof(argv)))
299 ret = -EFAULT;
300 return ret;
301}
302
303static int nilfs_ioctl_move_inode_block(struct inode *inode,
304 struct nilfs_vdesc *vdesc,
305 struct list_head *buffers)
306{
307 struct buffer_head *bh;
308 int ret;
309
310 if (vdesc->vd_flags == 0)
311 ret = nilfs_gccache_submit_read_data(
312 inode, vdesc->vd_offset, vdesc->vd_blocknr,
313 vdesc->vd_vblocknr, &bh);
314 else
315 ret = nilfs_gccache_submit_read_node(
316 inode, vdesc->vd_blocknr, vdesc->vd_vblocknr, &bh);
317
318 if (unlikely(ret < 0)) {
319 if (ret == -ENOENT)
320 printk(KERN_CRIT
321 "%s: invalid virtual block address (%s): "
322 "ino=%llu, cno=%llu, offset=%llu, "
323 "blocknr=%llu, vblocknr=%llu\n",
324 __func__, vdesc->vd_flags ? "node" : "data",
325 (unsigned long long)vdesc->vd_ino,
326 (unsigned long long)vdesc->vd_cno,
327 (unsigned long long)vdesc->vd_offset,
328 (unsigned long long)vdesc->vd_blocknr,
329 (unsigned long long)vdesc->vd_vblocknr);
330 return ret;
331 }
332 bh->b_private = vdesc;
333 list_add_tail(&bh->b_assoc_buffers, buffers);
334 return 0;
335}
336
337static ssize_t
338nilfs_ioctl_do_move_blocks(struct the_nilfs *nilfs, int index, int flags,
339 void *buf, size_t size, size_t nmembs)
340{
341 struct inode *inode;
342 struct nilfs_vdesc *vdesc;
343 struct buffer_head *bh, *n;
344 LIST_HEAD(buffers);
345 ino_t ino;
346 __u64 cno;
347 int i, ret;
348
349 for (i = 0, vdesc = buf; i < nmembs; ) {
350 ino = vdesc->vd_ino;
351 cno = vdesc->vd_cno;
352 inode = nilfs_gc_iget(nilfs, ino, cno);
353 if (unlikely(inode == NULL)) {
354 ret = -ENOMEM;
355 goto failed;
356 }
357 do {
358 ret = nilfs_ioctl_move_inode_block(inode, vdesc,
359 &buffers);
360 if (unlikely(ret < 0))
361 goto failed;
362 vdesc++;
363 } while (++i < nmembs &&
364 vdesc->vd_ino == ino && vdesc->vd_cno == cno);
365 }
366
367 list_for_each_entry_safe(bh, n, &buffers, b_assoc_buffers) {
368 ret = nilfs_gccache_wait_and_mark_dirty(bh);
369 if (unlikely(ret < 0)) {
370 if (ret == -EEXIST) {
371 vdesc = bh->b_private;
372 printk(KERN_CRIT
373 "%s: conflicting %s buffer: "
374 "ino=%llu, cno=%llu, offset=%llu, "
375 "blocknr=%llu, vblocknr=%llu\n",
376 __func__,
377 vdesc->vd_flags ? "node" : "data",
378 (unsigned long long)vdesc->vd_ino,
379 (unsigned long long)vdesc->vd_cno,
380 (unsigned long long)vdesc->vd_offset,
381 (unsigned long long)vdesc->vd_blocknr,
382 (unsigned long long)vdesc->vd_vblocknr);
383 }
384 goto failed;
385 }
386 list_del_init(&bh->b_assoc_buffers);
387 bh->b_private = NULL;
388 brelse(bh);
389 }
390 return nmembs;
391
392 failed:
393 list_for_each_entry_safe(bh, n, &buffers, b_assoc_buffers) {
394 list_del_init(&bh->b_assoc_buffers);
395 bh->b_private = NULL;
396 brelse(bh);
397 }
398 return ret;
399}
400
401static inline int nilfs_ioctl_move_blocks(struct the_nilfs *nilfs,
402 struct nilfs_argv *argv,
403 int dir)
404{
405 return nilfs_ioctl_wrap_copy(nilfs, argv, dir,
406 nilfs_ioctl_do_move_blocks);
407}
408
409static ssize_t
410nilfs_ioctl_do_delete_checkpoints(struct the_nilfs *nilfs, int index,
411 int flags, void *buf, size_t size,
412 size_t nmembs)
413{
414 struct inode *cpfile = nilfs->ns_cpfile;
415 struct nilfs_period *periods = buf;
416 int ret, i;
417
418 for (i = 0; i < nmembs; i++) {
419 ret = nilfs_cpfile_delete_checkpoints(
420 cpfile, periods[i].p_start, periods[i].p_end);
421 if (ret < 0)
422 return ret;
423 }
424 return nmembs;
425}
426
427static inline int nilfs_ioctl_delete_checkpoints(struct the_nilfs *nilfs,
428 struct nilfs_argv *argv,
429 int dir)
430{
431 return nilfs_ioctl_wrap_copy(nilfs, argv, dir,
432 nilfs_ioctl_do_delete_checkpoints);
433}
434
435static ssize_t
436nilfs_ioctl_do_free_vblocknrs(struct the_nilfs *nilfs, int index, int flags,
437 void *buf, size_t size, size_t nmembs)
438{
439 int ret = nilfs_dat_freev(nilfs_dat_inode(nilfs), buf, nmembs);
440
441 return (ret < 0) ? ret : nmembs;
442}
443
444static inline int nilfs_ioctl_free_vblocknrs(struct the_nilfs *nilfs,
445 struct nilfs_argv *argv,
446 int dir)
447{
448 return nilfs_ioctl_wrap_copy(nilfs, argv, dir,
449 nilfs_ioctl_do_free_vblocknrs);
450}
451
452static ssize_t
453nilfs_ioctl_do_mark_blocks_dirty(struct the_nilfs *nilfs, int index, int flags,
454 void *buf, size_t size, size_t nmembs)
455{
456 struct inode *dat = nilfs_dat_inode(nilfs);
457 struct nilfs_bmap *bmap = NILFS_I(dat)->i_bmap;
458 struct nilfs_bdesc *bdescs = buf;
459 int ret, i;
460
461 for (i = 0; i < nmembs; i++) {
462 /* XXX: use macro or inline func to check liveness */
463 ret = nilfs_bmap_lookup_at_level(bmap,
464 bdescs[i].bd_offset,
465 bdescs[i].bd_level + 1,
466 &bdescs[i].bd_blocknr);
467 if (ret < 0) {
468 if (ret != -ENOENT)
469 return ret;
470 bdescs[i].bd_blocknr = 0;
471 }
472 if (bdescs[i].bd_blocknr != bdescs[i].bd_oblocknr)
473 /* skip dead block */
474 continue;
475 if (bdescs[i].bd_level == 0) {
476 ret = nilfs_mdt_mark_block_dirty(dat,
477 bdescs[i].bd_offset);
478 if (ret < 0) {
479 BUG_ON(ret == -ENOENT);
480 return ret;
481 }
482 } else {
483 ret = nilfs_bmap_mark(bmap, bdescs[i].bd_offset,
484 bdescs[i].bd_level);
485 if (ret < 0) {
486 BUG_ON(ret == -ENOENT);
487 return ret;
488 }
489 }
490 }
491 return nmembs;
492}
493
494static inline int nilfs_ioctl_mark_blocks_dirty(struct the_nilfs *nilfs,
495 struct nilfs_argv *argv,
496 int dir)
497{
498 return nilfs_ioctl_wrap_copy(nilfs, argv, dir,
499 nilfs_ioctl_do_mark_blocks_dirty);
500}
501
502static ssize_t
503nilfs_ioctl_do_free_segments(struct the_nilfs *nilfs, int index, int flags,
504 void *buf, size_t size, size_t nmembs)
505{
506 struct nilfs_sb_info *sbi = nilfs_get_writer(nilfs);
507 int ret;
508
509 BUG_ON(!sbi);
510 ret = nilfs_segctor_add_segments_to_be_freed(
511 NILFS_SC(sbi), buf, nmembs);
512 nilfs_put_writer(nilfs);
513
514 return (ret < 0) ? ret : nmembs;
515}
516
517static inline int nilfs_ioctl_free_segments(struct the_nilfs *nilfs,
518 struct nilfs_argv *argv,
519 int dir)
520{
521 return nilfs_ioctl_wrap_copy(nilfs, argv, dir,
522 nilfs_ioctl_do_free_segments);
523}
524
525int nilfs_ioctl_prepare_clean_segments(struct the_nilfs *nilfs,
526 void __user *argp)
527{
528 struct nilfs_argv argv[5];
529 int dir, ret;
530
531 if (copy_from_user(argv, argp, sizeof(argv)))
532 return -EFAULT;
533
534 dir = _IOC_WRITE;
535 ret = nilfs_ioctl_move_blocks(nilfs, &argv[0], dir);
536 if (ret < 0)
537 goto out_move_blks;
538 ret = nilfs_ioctl_delete_checkpoints(nilfs, &argv[1], dir);
539 if (ret < 0)
540 goto out_del_cps;
541 ret = nilfs_ioctl_free_vblocknrs(nilfs, &argv[2], dir);
542 if (ret < 0)
543 goto out_free_vbns;
544 ret = nilfs_ioctl_mark_blocks_dirty(nilfs, &argv[3], dir);
545 if (ret < 0)
546 goto out_free_vbns;
547 ret = nilfs_ioctl_free_segments(nilfs, &argv[4], dir);
548 if (ret < 0)
549 goto out_free_segs;
550
551 return 0;
552
553 out_free_segs:
554 BUG(); /* XXX: not implemented yet */
555 out_free_vbns:
556 BUG();/* XXX: not implemented yet */
557 out_del_cps:
558 BUG();/* XXX: not implemented yet */
559 out_move_blks:
560 nilfs_remove_all_gcinode(nilfs);
561 return ret;
562}
563
564static int nilfs_ioctl_clean_segments(struct inode *inode, struct file *filp,
565 unsigned int cmd, void __user *argp)
566{
567 int ret;
568
569 if (!capable(CAP_SYS_ADMIN))
570 return -EPERM;
571
572 ret = nilfs_clean_segments(inode->i_sb, argp);
573 clear_nilfs_cond_nongc_write(NILFS_SB(inode->i_sb)->s_nilfs);
574 return ret;
575}
576
577static int nilfs_ioctl_test_cond(struct the_nilfs *nilfs, int cond)
578{
579 return (cond & NILFS_TIMEDWAIT_SEG_WRITE) &&
580 nilfs_cond_nongc_write(nilfs);
581}
582
583static void nilfs_ioctl_clear_cond(struct the_nilfs *nilfs, int cond)
584{
585 if (cond & NILFS_TIMEDWAIT_SEG_WRITE)
586 clear_nilfs_cond_nongc_write(nilfs);
587}
588
589static int nilfs_ioctl_timedwait(struct inode *inode, struct file *filp,
590 unsigned int cmd, void __user *argp)
591{
592 struct the_nilfs *nilfs = NILFS_SB(inode->i_sb)->s_nilfs;
593 struct nilfs_wait_cond wc;
594 long ret;
595
596 if (!capable(CAP_SYS_ADMIN))
597 return -EPERM;
598 if (copy_from_user(&wc, argp, sizeof(wc)))
599 return -EFAULT;
600
601 unlock_kernel();
602 ret = wc.wc_flags ?
603 wait_event_interruptible_timeout(
604 nilfs->ns_cleanerd_wq,
605 nilfs_ioctl_test_cond(nilfs, wc.wc_cond),
606 timespec_to_jiffies(&wc.wc_timeout)) :
607 wait_event_interruptible(
608 nilfs->ns_cleanerd_wq,
609 nilfs_ioctl_test_cond(nilfs, wc.wc_cond));
610 lock_kernel();
611 nilfs_ioctl_clear_cond(nilfs, wc.wc_cond);
612
613 if (ret > 0) {
614 jiffies_to_timespec(ret, &wc.wc_timeout);
615 if (copy_to_user(argp, &wc, sizeof(wc)))
616 return -EFAULT;
617 return 0;
618 }
619 if (ret != 0)
620 return -EINTR;
621
622 return wc.wc_flags ? -ETIME : 0;
623}
624
625static int nilfs_ioctl_sync(struct inode *inode, struct file *filp,
626 unsigned int cmd, void __user *argp)
627{
628 __u64 cno;
629 int ret;
630
631 ret = nilfs_construct_segment(inode->i_sb);
632 if (ret < 0)
633 return ret;
634
635 if (argp != NULL) {
636 cno = NILFS_SB(inode->i_sb)->s_nilfs->ns_cno - 1;
637 if (copy_to_user(argp, &cno, sizeof(cno)))
638 return -EFAULT;
639 }
640 return 0;
641}
642
643int nilfs_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
644 unsigned long arg)
645{
646 void __user *argp = (void * __user *)arg;
647
648 switch (cmd) {
649 case NILFS_IOCTL_CHANGE_CPMODE:
650 return nilfs_ioctl_change_cpmode(inode, filp, cmd, argp);
651 case NILFS_IOCTL_DELETE_CHECKPOINT:
652 return nilfs_ioctl_delete_checkpoint(inode, filp, cmd, argp);
653 case NILFS_IOCTL_GET_CPINFO:
654 return nilfs_ioctl_get_cpinfo(inode, filp, cmd, argp);
655 case NILFS_IOCTL_GET_CPSTAT:
656 return nilfs_ioctl_get_cpstat(inode, filp, cmd, argp);
657 case NILFS_IOCTL_GET_SUINFO:
658 return nilfs_ioctl_get_suinfo(inode, filp, cmd, argp);
659 case NILFS_IOCTL_GET_SUSTAT:
660 return nilfs_ioctl_get_sustat(inode, filp, cmd, argp);
661 case NILFS_IOCTL_GET_VINFO:
662 /* XXX: rename to ??? */
663 return nilfs_ioctl_get_vinfo(inode, filp, cmd, argp);
664 case NILFS_IOCTL_GET_BDESCS:
665 return nilfs_ioctl_get_bdescs(inode, filp, cmd, argp);
666 case NILFS_IOCTL_CLEAN_SEGMENTS:
667 return nilfs_ioctl_clean_segments(inode, filp, cmd, argp);
668 case NILFS_IOCTL_TIMEDWAIT:
669 return nilfs_ioctl_timedwait(inode, filp, cmd, argp);
670 case NILFS_IOCTL_SYNC:
671 return nilfs_ioctl_sync(inode, filp, cmd, argp);
672 default:
673 return -ENOTTY;
674 }
675}
676
677/* compat_ioctl */
678#ifdef CONFIG_COMPAT
679#include <linux/compat.h>
680
681static int nilfs_compat_locked_ioctl(struct inode *inode, struct file *filp,
682 unsigned int cmd, unsigned long arg)
683{
684 int ret;
685
686 lock_kernel();
687 ret = nilfs_ioctl(inode, filp, cmd, arg);
688 unlock_kernel();
689 return ret;
690}
691
692static int
693nilfs_compat_ioctl_uargv32_to_uargv(struct nilfs_argv32 __user *uargv32,
694 struct nilfs_argv __user *uargv)
695{
696 compat_uptr_t base;
697 compat_size_t nmembs, size;
698 compat_int_t index, flags;
699
700 if (get_user(base, &uargv32->v_base) ||
701 put_user(compat_ptr(base), &uargv->v_base) ||
702 get_user(nmembs, &uargv32->v_nmembs) ||
703 put_user(nmembs, &uargv->v_nmembs) ||
704 get_user(size, &uargv32->v_size) ||
705 put_user(size, &uargv->v_size) ||
706 get_user(index, &uargv32->v_index) ||
707 put_user(index, &uargv->v_index) ||
708 get_user(flags, &uargv32->v_flags) ||
709 put_user(flags, &uargv->v_flags))
710 return -EFAULT;
711 return 0;
712}
713
714static int
715nilfs_compat_ioctl_uargv_to_uargv32(struct nilfs_argv __user *uargv,
716 struct nilfs_argv32 __user *uargv32)
717{
718 size_t nmembs;
719
720 if (get_user(nmembs, &uargv->v_nmembs) ||
721 put_user(nmembs, &uargv32->v_nmembs))
722 return -EFAULT;
723 return 0;
724}
725
726static int
727nilfs_compat_ioctl_get_by_argv(struct inode *inode, struct file *filp,
728 unsigned int cmd, unsigned long arg)
729{
730 struct nilfs_argv __user *uargv;
731 struct nilfs_argv32 __user *uargv32;
732 int ret;
733
734 uargv = compat_alloc_user_space(sizeof(struct nilfs_argv));
735 uargv32 = compat_ptr(arg);
736 ret = nilfs_compat_ioctl_uargv32_to_uargv(uargv32, uargv);
737 if (ret < 0)
738 return ret;
739
740 ret = nilfs_compat_locked_ioctl(inode, filp, cmd, (unsigned long)uargv);
741 if (ret < 0)
742 return ret;
743
744 return nilfs_compat_ioctl_uargv_to_uargv32(uargv, uargv32);
745}
746
747static int
748nilfs_compat_ioctl_change_cpmode(struct inode *inode, struct file *filp,
749 unsigned int cmd, unsigned long arg)
750{
751 struct nilfs_cpmode __user *ucpmode;
752 struct nilfs_cpmode32 __user *ucpmode32;
753 int mode;
754
755 ucpmode = compat_alloc_user_space(sizeof(struct nilfs_cpmode));
756 ucpmode32 = compat_ptr(arg);
757 if (copy_in_user(&ucpmode->cm_cno, &ucpmode32->cm_cno,
758 sizeof(__u64)) ||
759 get_user(mode, &ucpmode32->cm_mode) ||
760 put_user(mode, &ucpmode->cm_mode))
761 return -EFAULT;
762
763 return nilfs_compat_locked_ioctl(
764 inode, filp, cmd, (unsigned long)ucpmode);
765}
766
767
768static inline int
769nilfs_compat_ioctl_delete_checkpoint(struct inode *inode, struct file *filp,
770 unsigned int cmd, unsigned long arg)
771{
772 return nilfs_compat_locked_ioctl(inode, filp, cmd, arg);
773}
774
775static inline int
776nilfs_compat_ioctl_get_cpinfo(struct inode *inode, struct file *filp,
777 unsigned int cmd, unsigned long arg)
778{
779 return nilfs_compat_ioctl_get_by_argv(inode, filp, cmd, arg);
780}
781
782static inline int
783nilfs_compat_ioctl_get_cpstat(struct inode *inode, struct file *filp,
784 unsigned int cmd, unsigned long arg)
785{
786 return nilfs_compat_locked_ioctl(inode, filp, cmd, arg);
787}
788
789static inline int
790nilfs_compat_ioctl_get_suinfo(struct inode *inode, struct file *filp,
791 unsigned int cmd, unsigned long arg)
792{
793 return nilfs_compat_ioctl_get_by_argv(inode, filp, cmd, arg);
794}
795
796static int
797nilfs_compat_ioctl_get_sustat(struct inode *inode, struct file *filp,
798 unsigned int cmd, unsigned long arg)
799{
800 struct nilfs_sustat __user *usustat;
801 struct nilfs_sustat32 __user *usustat32;
802 time_t ctime, nongc_ctime;
803 int ret;
804
805 usustat = compat_alloc_user_space(sizeof(struct nilfs_sustat));
806 ret = nilfs_compat_locked_ioctl(inode, filp, cmd,
807 (unsigned long)usustat);
808 if (ret < 0)
809 return ret;
810
811 usustat32 = compat_ptr(arg);
812 if (copy_in_user(&usustat32->ss_nsegs, &usustat->ss_nsegs,
813 sizeof(__u64)) ||
814 copy_in_user(&usustat32->ss_ncleansegs, &usustat->ss_ncleansegs,
815 sizeof(__u64)) ||
816 copy_in_user(&usustat32->ss_ndirtysegs, &usustat->ss_ndirtysegs,
817 sizeof(__u64)) ||
818 get_user(ctime, &usustat->ss_ctime) ||
819 put_user(ctime, &usustat32->ss_ctime) ||
820 get_user(nongc_ctime, &usustat->ss_nongc_ctime) ||
821 put_user(nongc_ctime, &usustat32->ss_nongc_ctime))
822 return -EFAULT;
823 return 0;
824}
825
826static inline int
827nilfs_compat_ioctl_get_vinfo(struct inode *inode, struct file *filp,
828 unsigned int cmd, unsigned long arg)
829{
830 return nilfs_compat_ioctl_get_by_argv(inode, filp, cmd, arg);
831}
832
833static inline int
834nilfs_compat_ioctl_get_bdescs(struct inode *inode, struct file *filp,
835 unsigned int cmd, unsigned long arg)
836{
837 return nilfs_compat_ioctl_get_by_argv(inode, filp, cmd, arg);
838}
839
840static int
841nilfs_compat_ioctl_clean_segments(struct inode *inode, struct file *filp,
842 unsigned int cmd, unsigned long arg)
843{
844 struct nilfs_argv __user *uargv;
845 struct nilfs_argv32 __user *uargv32;
846 int i, ret;
847
848 uargv = compat_alloc_user_space(sizeof(struct nilfs_argv) * 5);
849 uargv32 = compat_ptr(arg);
850 for (i = 0; i < 5; i++) {
851 ret = nilfs_compat_ioctl_uargv32_to_uargv(&uargv32[i],
852 &uargv[i]);
853 if (ret < 0)
854 return ret;
855 }
856 return nilfs_compat_locked_ioctl(
857 inode, filp, cmd, (unsigned long)uargv);
858}
859
860static int
861nilfs_compat_ioctl_timedwait(struct inode *inode, struct file *filp,
862 unsigned int cmd, unsigned long arg)
863{
864 struct nilfs_wait_cond __user *uwcond;
865 struct nilfs_wait_cond32 __user *uwcond32;
866 struct timespec ts;
867 int cond, flags, ret;
868
869 uwcond = compat_alloc_user_space(sizeof(struct nilfs_wait_cond));
870 uwcond32 = compat_ptr(arg);
871 if (get_user(cond, &uwcond32->wc_cond) ||
872 put_user(cond, &uwcond->wc_cond) ||
873 get_user(flags, &uwcond32->wc_flags) ||
874 put_user(flags, &uwcond->wc_flags) ||
875 get_user(ts.tv_sec, &uwcond32->wc_timeout.tv_sec) ||
876 get_user(ts.tv_nsec, &uwcond32->wc_timeout.tv_nsec) ||
877 put_user(ts.tv_sec, &uwcond->wc_timeout.tv_sec) ||
878 put_user(ts.tv_nsec, &uwcond->wc_timeout.tv_nsec))
879 return -EFAULT;
880
881 ret = nilfs_compat_locked_ioctl(inode, filp, cmd,
882 (unsigned long)uwcond);
883 if (ret < 0)
884 return ret;
885
886 if (get_user(ts.tv_sec, &uwcond->wc_timeout.tv_sec) ||
887 get_user(ts.tv_nsec, &uwcond->wc_timeout.tv_nsec) ||
888 put_user(ts.tv_sec, &uwcond32->wc_timeout.tv_sec) ||
889 put_user(ts.tv_nsec, &uwcond32->wc_timeout.tv_nsec))
890 return -EFAULT;
891
892 return 0;
893}
894
895static int nilfs_compat_ioctl_sync(struct inode *inode, struct file *filp,
896 unsigned int cmd, unsigned long arg)
897{
898 return nilfs_compat_locked_ioctl(inode, filp, cmd, arg);
899}
900
901long nilfs_compat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
902{
903 struct inode *inode = filp->f_dentry->d_inode;
904
905 switch (cmd) {
906 case NILFS_IOCTL32_CHANGE_CPMODE:
907 return nilfs_compat_ioctl_change_cpmode(
908 inode, filp, NILFS_IOCTL_CHANGE_CPMODE, arg);
909 case NILFS_IOCTL_DELETE_CHECKPOINT:
910 return nilfs_compat_ioctl_delete_checkpoint(
911 inode, filp, cmd, arg);
912 case NILFS_IOCTL32_GET_CPINFO:
913 return nilfs_compat_ioctl_get_cpinfo(
914 inode, filp, NILFS_IOCTL_GET_CPINFO, arg);
915 case NILFS_IOCTL_GET_CPSTAT:
916 return nilfs_compat_ioctl_get_cpstat(inode, filp, cmd, arg);
917 case NILFS_IOCTL32_GET_SUINFO:
918 return nilfs_compat_ioctl_get_suinfo(
919 inode, filp, NILFS_IOCTL_GET_SUINFO, arg);
920 case NILFS_IOCTL32_GET_SUSTAT:
921 return nilfs_compat_ioctl_get_sustat(
922 inode, filp, NILFS_IOCTL_GET_SUSTAT, arg);
923 case NILFS_IOCTL32_GET_VINFO:
924 return nilfs_compat_ioctl_get_vinfo(
925 inode, filp, NILFS_IOCTL_GET_VINFO, arg);
926 case NILFS_IOCTL32_GET_BDESCS:
927 return nilfs_compat_ioctl_get_bdescs(
928 inode, filp, NILFS_IOCTL_GET_BDESCS, arg);
929 case NILFS_IOCTL32_CLEAN_SEGMENTS:
930 return nilfs_compat_ioctl_clean_segments(
931 inode, filp, NILFS_IOCTL_CLEAN_SEGMENTS, arg);
932 case NILFS_IOCTL32_TIMEDWAIT:
933 return nilfs_compat_ioctl_timedwait(
934 inode, filp, NILFS_IOCTL_TIMEDWAIT, arg);
935 case NILFS_IOCTL_SYNC:
936 return nilfs_compat_ioctl_sync(inode, filp, cmd, arg);
937 default:
938 return -ENOIOCTLCMD;
939 }
940}
941#endif