xref: /linux/fs/nilfs2/ioctl.c (revision f4cdf7ca9a1fdcca413157df19753f388a5a224e)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * NILFS ioctl operations.
4  *
5  * Copyright (C) 2007, 2008 Nippon Telegraph and Telephone Corporation.
6  *
7  * Written by Koji Sato.
8  */
9 
10 #include <linux/fs.h>
11 #include <linux/wait.h>
12 #include <linux/slab.h>
13 #include <linux/capability.h>	/* capable() */
14 #include <linux/uaccess.h>	/* copy_from_user(), copy_to_user() */
15 #include <linux/vmalloc.h>
16 #include <linux/compat.h>	/* compat_ptr() */
17 #include <linux/mount.h>	/* mnt_want_write_file(), mnt_drop_write_file() */
18 #include <linux/buffer_head.h>
19 #include <linux/fileattr.h>
20 #include <linux/string.h>
21 #include "nilfs.h"
22 #include "segment.h"
23 #include "bmap.h"
24 #include "cpfile.h"
25 #include "sufile.h"
26 #include "dat.h"
27 
28 /**
29  * nilfs_ioctl_wrap_copy - wrapping function of get/set metadata info
30  * @nilfs: nilfs object
31  * @argv: vector of arguments from userspace
32  * @dir: set of direction flags
33  * @dofunc: concrete function of get/set metadata info
34  *
35  * Description: nilfs_ioctl_wrap_copy() gets/sets metadata info by means of
36  * calling dofunc() function on the basis of @argv argument.  If successful,
37  * the requested metadata information is copied to userspace memory.
38  *
39  * Return: 0 on success, or one of the following negative error codes on
40  * failure:
41  * * %-EFAULT	- Failure during execution of requested operation.
42  * * %-EINVAL	- Invalid arguments from userspace.
43  * * %-ENOMEM	- Insufficient memory available.
44  */
45 static int nilfs_ioctl_wrap_copy(struct the_nilfs *nilfs,
46 				 struct nilfs_argv *argv, int dir,
47 				 ssize_t (*dofunc)(struct the_nilfs *,
48 						   __u64 *, int,
49 						   void *, size_t, size_t))
50 {
51 	void *buf;
52 	void __user *base = u64_to_user_ptr(argv->v_base);
53 	size_t maxmembs, total, n;
54 	ssize_t nr;
55 	int ret, i;
56 	__u64 pos, ppos;
57 
58 	if (argv->v_nmembs == 0)
59 		return 0;
60 
61 	if ((size_t)argv->v_size > PAGE_SIZE)
62 		return -EINVAL;
63 
64 	/*
65 	 * Reject pairs of a start item position (argv->v_index) and a
66 	 * total count (argv->v_nmembs) which leads position 'pos' to
67 	 * overflow by the increment at the end of the loop.
68 	 */
69 	if (argv->v_index > ~(__u64)0 - argv->v_nmembs)
70 		return -EINVAL;
71 
72 	buf = kzalloc(PAGE_SIZE, GFP_NOFS);
73 	if (unlikely(!buf))
74 		return -ENOMEM;
75 	maxmembs = PAGE_SIZE / argv->v_size;
76 
77 	ret = 0;
78 	total = 0;
79 	pos = argv->v_index;
80 	for (i = 0; i < argv->v_nmembs; i += n) {
81 		n = (argv->v_nmembs - i < maxmembs) ?
82 			argv->v_nmembs - i : maxmembs;
83 		if ((dir & _IOC_WRITE) &&
84 		    copy_from_user(buf, base + argv->v_size * i,
85 				   argv->v_size * n)) {
86 			ret = -EFAULT;
87 			break;
88 		}
89 		ppos = pos;
90 		nr = dofunc(nilfs, &pos, argv->v_flags, buf, argv->v_size,
91 			       n);
92 		if (nr < 0) {
93 			ret = nr;
94 			break;
95 		}
96 		if ((dir & _IOC_READ) &&
97 		    copy_to_user(base + argv->v_size * i, buf,
98 				 argv->v_size * nr)) {
99 			ret = -EFAULT;
100 			break;
101 		}
102 		total += nr;
103 		if ((size_t)nr < n)
104 			break;
105 		if (pos == ppos)
106 			pos += n;
107 	}
108 	argv->v_nmembs = total;
109 
110 	kfree(buf);
111 	return ret;
112 }
113 
114 /**
115  * nilfs_fileattr_get - retrieve miscellaneous file attributes
116  * @dentry: the object to retrieve from
117  * @fa:     fileattr pointer
118  *
119  * Return: always 0 as success.
120  */
121 int nilfs_fileattr_get(struct dentry *dentry, struct file_kattr *fa)
122 {
123 	struct inode *inode = d_inode(dentry);
124 
125 	fileattr_fill_flags(fa, NILFS_I(inode)->i_flags & FS_FL_USER_VISIBLE);
126 
127 	return 0;
128 }
129 
130 /**
131  * nilfs_fileattr_set - change miscellaneous file attributes
132  * @idmap:  idmap of the mount
133  * @dentry: the object to change
134  * @fa:     fileattr pointer
135  *
136  * Return: 0 on success, or a negative error code on failure.
137  */
138 int nilfs_fileattr_set(struct mnt_idmap *idmap,
139 		       struct dentry *dentry, struct file_kattr *fa)
140 {
141 	struct inode *inode = d_inode(dentry);
142 	struct nilfs_transaction_info ti;
143 	unsigned int flags, oldflags;
144 	int ret;
145 
146 	if (fileattr_has_fsx(fa))
147 		return -EOPNOTSUPP;
148 
149 	flags = nilfs_mask_flags(inode->i_mode, fa->flags);
150 
151 	ret = nilfs_transaction_begin(inode->i_sb, &ti, 0);
152 	if (ret)
153 		return ret;
154 
155 	oldflags = NILFS_I(inode)->i_flags & ~FS_FL_USER_MODIFIABLE;
156 	NILFS_I(inode)->i_flags = oldflags | (flags & FS_FL_USER_MODIFIABLE);
157 
158 	nilfs_set_inode_flags(inode);
159 	inode_set_ctime_current(inode);
160 	if (IS_SYNC(inode))
161 		nilfs_set_transaction_flag(NILFS_TI_SYNC);
162 
163 	nilfs_mark_inode_dirty(inode);
164 	return nilfs_transaction_commit(inode->i_sb);
165 }
166 
167 /**
168  * nilfs_ioctl_getversion - get info about a file's version (generation number)
169  * @inode: inode object
170  * @argp:  userspace memory where the generation number of @inode is stored
171  *
172  * Return: 0 on success, or %-EFAULT on error.
173  */
174 static int nilfs_ioctl_getversion(struct inode *inode, void __user *argp)
175 {
176 	return put_user(inode->i_generation, (int __user *)argp);
177 }
178 
179 /**
180  * nilfs_ioctl_change_cpmode - change checkpoint mode (checkpoint/snapshot)
181  * @inode: inode object
182  * @filp: file object
183  * @cmd: ioctl's request code
184  * @argp: pointer on argument from userspace
185  *
186  * Description: nilfs_ioctl_change_cpmode() function changes mode of
187  * given checkpoint between checkpoint and snapshot state. This ioctl
188  * is used in chcp and mkcp utilities.
189  *
190  * Return: 0 on success, or one of the following negative error codes on
191  * failure:
192  * %-EFAULT	- Failure during checkpoint mode changing.
193  * %-EPERM	- Operation not permitted.
194  */
195 static int nilfs_ioctl_change_cpmode(struct inode *inode, struct file *filp,
196 				     unsigned int cmd, void __user *argp)
197 {
198 	struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
199 	struct nilfs_transaction_info ti;
200 	struct nilfs_cpmode cpmode;
201 	int ret;
202 
203 	if (!capable(CAP_SYS_ADMIN))
204 		return -EPERM;
205 
206 	ret = mnt_want_write_file(filp);
207 	if (ret)
208 		return ret;
209 
210 	ret = -EFAULT;
211 	if (copy_from_user(&cpmode, argp, sizeof(cpmode)))
212 		goto out;
213 
214 	mutex_lock(&nilfs->ns_snapshot_mount_mutex);
215 
216 	nilfs_transaction_begin(inode->i_sb, &ti, 0);
217 	ret = nilfs_cpfile_change_cpmode(
218 		nilfs->ns_cpfile, cpmode.cm_cno, cpmode.cm_mode);
219 	if (unlikely(ret < 0))
220 		nilfs_transaction_abort(inode->i_sb);
221 	else
222 		nilfs_transaction_commit(inode->i_sb); /* never fails */
223 
224 	mutex_unlock(&nilfs->ns_snapshot_mount_mutex);
225 out:
226 	mnt_drop_write_file(filp);
227 	return ret;
228 }
229 
230 /**
231  * nilfs_ioctl_delete_checkpoint - remove checkpoint
232  * @inode: inode object
233  * @filp: file object
234  * @cmd: ioctl's request code
235  * @argp: pointer on argument from userspace
236  *
237  * Description: nilfs_ioctl_delete_checkpoint() function removes
238  * checkpoint from NILFS2 file system. This ioctl is used in rmcp
239  * utility.
240  *
241  * Return: 0 on success, or one of the following negative error codes on
242  * failure:
243  * %-EFAULT	- Failure during checkpoint removing.
244  * %-EPERM	- Operation not permitted.
245  */
246 static int
247 nilfs_ioctl_delete_checkpoint(struct inode *inode, struct file *filp,
248 			      unsigned int cmd, void __user *argp)
249 {
250 	struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
251 	struct nilfs_transaction_info ti;
252 	__u64 cno;
253 	int ret;
254 
255 	if (!capable(CAP_SYS_ADMIN))
256 		return -EPERM;
257 
258 	ret = mnt_want_write_file(filp);
259 	if (ret)
260 		return ret;
261 
262 	ret = -EFAULT;
263 	if (copy_from_user(&cno, argp, sizeof(cno)))
264 		goto out;
265 
266 	nilfs_transaction_begin(inode->i_sb, &ti, 0);
267 	ret = nilfs_cpfile_delete_checkpoint(nilfs->ns_cpfile, cno);
268 	if (unlikely(ret < 0))
269 		nilfs_transaction_abort(inode->i_sb);
270 	else
271 		nilfs_transaction_commit(inode->i_sb); /* never fails */
272 out:
273 	mnt_drop_write_file(filp);
274 	return ret;
275 }
276 
277 /**
278  * nilfs_ioctl_do_get_cpinfo - callback method getting info about checkpoints
279  * @nilfs: nilfs object
280  * @posp: pointer on array of checkpoint's numbers
281  * @flags: checkpoint mode (checkpoint or snapshot)
282  * @buf: buffer for storing checkponts' info
283  * @size: size in bytes of one checkpoint info item in array
284  * @nmembs: number of checkpoints in array (numbers and infos)
285  *
286  * Description: nilfs_ioctl_do_get_cpinfo() function returns info about
287  * requested checkpoints. The NILFS_IOCTL_GET_CPINFO ioctl is used in
288  * lscp utility and by nilfs_cleanerd daemon.
289  *
290  * Return: Count of nilfs_cpinfo structures in output buffer.
291  */
292 static ssize_t
293 nilfs_ioctl_do_get_cpinfo(struct the_nilfs *nilfs, __u64 *posp, int flags,
294 			  void *buf, size_t size, size_t nmembs)
295 {
296 	int ret;
297 
298 	down_read(&nilfs->ns_segctor_sem);
299 	ret = nilfs_cpfile_get_cpinfo(nilfs->ns_cpfile, posp, flags, buf,
300 				      size, nmembs);
301 	up_read(&nilfs->ns_segctor_sem);
302 	return ret;
303 }
304 
305 /**
306  * nilfs_ioctl_get_cpstat - get checkpoints statistics
307  * @inode: inode object
308  * @filp: file object
309  * @cmd: ioctl's request code
310  * @argp: pointer on argument from userspace
311  *
312  * Description: nilfs_ioctl_get_cpstat() returns information about checkpoints.
313  * The NILFS_IOCTL_GET_CPSTAT ioctl is used by lscp, rmcp utilities
314  * and by nilfs_cleanerd daemon.  The checkpoint statistics are copied to
315  * the userspace memory pointed to by @argp.
316  *
317  * Return: 0 on success, or one of the following negative error codes on
318  * failure:
319  * * %-EFAULT	- Failure during getting checkpoints statistics.
320  * * %-EIO	- I/O error.
321  * * %-ENOMEM	- Insufficient memory available.
322  */
323 static int nilfs_ioctl_get_cpstat(struct inode *inode, struct file *filp,
324 				  unsigned int cmd, void __user *argp)
325 {
326 	struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
327 	struct nilfs_cpstat cpstat;
328 	int ret;
329 
330 	down_read(&nilfs->ns_segctor_sem);
331 	ret = nilfs_cpfile_get_stat(nilfs->ns_cpfile, &cpstat);
332 	up_read(&nilfs->ns_segctor_sem);
333 	if (ret < 0)
334 		return ret;
335 
336 	if (copy_to_user(argp, &cpstat, sizeof(cpstat)))
337 		ret = -EFAULT;
338 	return ret;
339 }
340 
341 /**
342  * nilfs_ioctl_do_get_suinfo - callback method getting segment usage info
343  * @nilfs: nilfs object
344  * @posp: pointer on array of segment numbers
345  * @flags: *not used*
346  * @buf: buffer for storing suinfo array
347  * @size: size in bytes of one suinfo item in array
348  * @nmembs: count of segment numbers and suinfos in array
349  *
350  * Description: nilfs_ioctl_do_get_suinfo() function returns segment usage
351  * info about requested segments. The NILFS_IOCTL_GET_SUINFO ioctl is used
352  * in lssu, nilfs_resize utilities and by nilfs_cleanerd daemon.
353  *
354  * Return: Count of nilfs_suinfo structures in output buffer on success,
355  * or a negative error code on failure.
356  */
357 static ssize_t
358 nilfs_ioctl_do_get_suinfo(struct the_nilfs *nilfs, __u64 *posp, int flags,
359 			  void *buf, size_t size, size_t nmembs)
360 {
361 	int ret;
362 
363 	down_read(&nilfs->ns_segctor_sem);
364 	ret = nilfs_sufile_get_suinfo(nilfs->ns_sufile, *posp, buf, size,
365 				      nmembs);
366 	up_read(&nilfs->ns_segctor_sem);
367 	return ret;
368 }
369 
370 /**
371  * nilfs_ioctl_get_sustat - get segment usage statistics
372  * @inode: inode object
373  * @filp: file object
374  * @cmd: ioctl's request code
375  * @argp: pointer on argument from userspace
376  *
377  * Description: nilfs_ioctl_get_sustat() returns segment usage statistics.
378  * The NILFS_IOCTL_GET_SUSTAT ioctl is used in lssu, nilfs_resize utilities
379  * and by nilfs_cleanerd daemon.  The requested segment usage information is
380  * copied to the userspace memory pointed to by @argp.
381  *
382  * Return: 0 on success, or one of the following negative error codes on
383  * failure:
384  * * %-EFAULT	- Failure during getting segment usage statistics.
385  * * %-EIO	- I/O error.
386  * * %-ENOMEM	- Insufficient memory available.
387  */
388 static int nilfs_ioctl_get_sustat(struct inode *inode, struct file *filp,
389 				  unsigned int cmd, void __user *argp)
390 {
391 	struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
392 	struct nilfs_sustat sustat;
393 	int ret;
394 
395 	down_read(&nilfs->ns_segctor_sem);
396 	ret = nilfs_sufile_get_stat(nilfs->ns_sufile, &sustat);
397 	up_read(&nilfs->ns_segctor_sem);
398 	if (ret < 0)
399 		return ret;
400 
401 	if (copy_to_user(argp, &sustat, sizeof(sustat)))
402 		ret = -EFAULT;
403 	return ret;
404 }
405 
406 /**
407  * nilfs_ioctl_do_get_vinfo - callback method getting virtual blocks info
408  * @nilfs: nilfs object
409  * @posp: *not used*
410  * @flags: *not used*
411  * @buf: buffer for storing array of nilfs_vinfo structures
412  * @size: size in bytes of one vinfo item in array
413  * @nmembs: count of vinfos in array
414  *
415  * Description: nilfs_ioctl_do_get_vinfo() function returns information
416  * on virtual block addresses. The NILFS_IOCTL_GET_VINFO ioctl is used
417  * by nilfs_cleanerd daemon.
418  *
419  * Return: Count of nilfs_vinfo structures in output buffer on success, or
420  * a negative error code on failure.
421  */
422 static ssize_t
423 nilfs_ioctl_do_get_vinfo(struct the_nilfs *nilfs, __u64 *posp, int flags,
424 			 void *buf, size_t size, size_t nmembs)
425 {
426 	int ret;
427 
428 	down_read(&nilfs->ns_segctor_sem);
429 	ret = nilfs_dat_get_vinfo(nilfs->ns_dat, buf, size, nmembs);
430 	up_read(&nilfs->ns_segctor_sem);
431 	return ret;
432 }
433 
434 /**
435  * nilfs_ioctl_do_get_bdescs - callback method getting disk block descriptors
436  * @nilfs: nilfs object
437  * @posp: *not used*
438  * @flags: *not used*
439  * @buf: buffer for storing array of nilfs_bdesc structures
440  * @size: size in bytes of one bdesc item in array
441  * @nmembs: count of bdescs in array
442  *
443  * Description: nilfs_ioctl_do_get_bdescs() function returns information
444  * about descriptors of disk block numbers. The NILFS_IOCTL_GET_BDESCS ioctl
445  * is used by nilfs_cleanerd daemon.
446  *
447  * Return: Count of nilfs_bdescs structures in output buffer on success, or
448  * a negative error code on failure.
449  */
450 static ssize_t
451 nilfs_ioctl_do_get_bdescs(struct the_nilfs *nilfs, __u64 *posp, int flags,
452 			  void *buf, size_t size, size_t nmembs)
453 {
454 	struct nilfs_bmap *bmap = NILFS_I(nilfs->ns_dat)->i_bmap;
455 	struct nilfs_bdesc *bdescs = buf;
456 	int ret, i;
457 
458 	down_read(&nilfs->ns_segctor_sem);
459 	for (i = 0; i < nmembs; i++) {
460 		ret = nilfs_bmap_lookup_at_level(bmap,
461 						 bdescs[i].bd_offset,
462 						 bdescs[i].bd_level + 1,
463 						 &bdescs[i].bd_blocknr);
464 		if (ret < 0) {
465 			if (ret != -ENOENT) {
466 				up_read(&nilfs->ns_segctor_sem);
467 				return ret;
468 			}
469 			bdescs[i].bd_blocknr = 0;
470 		}
471 	}
472 	up_read(&nilfs->ns_segctor_sem);
473 	return nmembs;
474 }
475 
476 /**
477  * nilfs_ioctl_get_bdescs - get disk block descriptors
478  * @inode: inode object
479  * @filp: file object
480  * @cmd: ioctl's request code
481  * @argp: pointer on argument from userspace
482  *
483  * Description: nilfs_ioctl_do_get_bdescs() function returns information
484  * about descriptors of disk block numbers. The NILFS_IOCTL_GET_BDESCS ioctl
485  * is used by nilfs_cleanerd daemon.  If successful, disk block descriptors
486  * are copied to userspace pointer @argp.
487  *
488  * Return: 0 on success, or one of the following negative error codes on
489  * failure:
490  * * %-EFAULT	- Failure during getting disk block descriptors.
491  * * %-EINVAL	- Invalid arguments from userspace.
492  * * %-EIO	- I/O error.
493  * * %-ENOMEM	- Insufficient memory available.
494  */
495 static int nilfs_ioctl_get_bdescs(struct inode *inode, struct file *filp,
496 				  unsigned int cmd, void __user *argp)
497 {
498 	struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
499 	struct nilfs_argv argv;
500 	int ret;
501 
502 	if (copy_from_user(&argv, argp, sizeof(argv)))
503 		return -EFAULT;
504 
505 	if (argv.v_size != sizeof(struct nilfs_bdesc))
506 		return -EINVAL;
507 
508 	ret = nilfs_ioctl_wrap_copy(nilfs, &argv, _IOC_DIR(cmd),
509 				    nilfs_ioctl_do_get_bdescs);
510 	if (ret < 0)
511 		return ret;
512 
513 	if (copy_to_user(argp, &argv, sizeof(argv)))
514 		ret = -EFAULT;
515 	return ret;
516 }
517 
518 /**
519  * nilfs_ioctl_move_inode_block - prepare data/node block for moving by GC
520  * @inode: inode object
521  * @vdesc: descriptor of virtual block number
522  * @buffers: list of moving buffers
523  *
524  * Description: nilfs_ioctl_move_inode_block() function registers data/node
525  * buffer in the GC pagecache and submit read request.
526  *
527  * Return: 0 on success, or one of the following negative error codes on
528  * failure:
529  * * %-EEXIST	- Block conflict detected.
530  * * %-EINVAL	- Invalid virtual block descriptor.
531  * * %-EIO	- I/O error.
532  * * %-ENOENT	- Requested block doesn't exist.
533  * * %-ENOMEM	- Insufficient memory available.
534  */
535 static int nilfs_ioctl_move_inode_block(struct inode *inode,
536 					struct nilfs_vdesc *vdesc,
537 					struct list_head *buffers)
538 {
539 	struct buffer_head *bh;
540 	__u64 limit_blkidx = (__u64)inode->i_sb->s_maxbytes >> inode->i_blkbits;
541 	int ret;
542 
543 	/*
544 	 * vblocknr 0 is reserved as an invalid pointer.  Also, limit_blkidx
545 	 * ensures that the page index converted from vd_vblocknr never
546 	 * overflows the page cache limit and respects the architecture's bmap
547 	 * key width.
548 	 */
549 	if (unlikely(vdesc->vd_vblocknr == 0 ||
550 			vdesc->vd_vblocknr >= limit_blkidx))
551 		return -EINVAL;
552 
553 	if (vdesc->vd_flags == 0) {
554 		if (unlikely(vdesc->vd_offset >= limit_blkidx))
555 			return -EINVAL;
556 
557 		ret = nilfs_gccache_submit_read_data(
558 			inode, vdesc->vd_offset, vdesc->vd_blocknr,
559 			vdesc->vd_vblocknr, &bh);
560 	} else {
561 		ret = nilfs_gccache_submit_read_node(
562 			inode, vdesc->vd_blocknr, vdesc->vd_vblocknr, &bh);
563 	}
564 
565 	if (unlikely(ret < 0)) {
566 		if (ret == -ENOENT)
567 			nilfs_crit(inode->i_sb,
568 				   "%s: invalid virtual block address (%s): ino=%llu, cno=%llu, offset=%llu, blocknr=%llu, vblocknr=%llu",
569 				   __func__, vdesc->vd_flags ? "node" : "data",
570 				   (unsigned long long)vdesc->vd_ino,
571 				   (unsigned long long)vdesc->vd_cno,
572 				   (unsigned long long)vdesc->vd_offset,
573 				   (unsigned long long)vdesc->vd_blocknr,
574 				   (unsigned long long)vdesc->vd_vblocknr);
575 		return ret;
576 	}
577 	if (unlikely(!list_empty(&bh->b_assoc_buffers))) {
578 		nilfs_crit(inode->i_sb,
579 			   "%s: conflicting %s buffer: ino=%llu, cno=%llu, offset=%llu, blocknr=%llu, vblocknr=%llu",
580 			   __func__, vdesc->vd_flags ? "node" : "data",
581 			   (unsigned long long)vdesc->vd_ino,
582 			   (unsigned long long)vdesc->vd_cno,
583 			   (unsigned long long)vdesc->vd_offset,
584 			   (unsigned long long)vdesc->vd_blocknr,
585 			   (unsigned long long)vdesc->vd_vblocknr);
586 		brelse(bh);
587 		return -EEXIST;
588 	}
589 	list_add_tail(&bh->b_assoc_buffers, buffers);
590 	return 0;
591 }
592 
593 /**
594  * nilfs_ioctl_move_blocks - move valid inode's blocks during garbage collection
595  * @sb: superblock object
596  * @argv: vector of arguments from userspace
597  * @buf: array of nilfs_vdesc structures
598  *
599  * Description: nilfs_ioctl_move_blocks() function reads valid data/node
600  * blocks that garbage collector specified with the array of nilfs_vdesc
601  * structures and stores them into page caches of GC inodes.
602  *
603  * Return: Number of processed nilfs_vdesc structures on success, or
604  * a negative error code on failure.
605  */
606 static int nilfs_ioctl_move_blocks(struct super_block *sb,
607 				   struct nilfs_argv *argv, void *buf)
608 {
609 	size_t nmembs = argv->v_nmembs;
610 	struct the_nilfs *nilfs = sb->s_fs_info;
611 	struct inode *inode;
612 	struct nilfs_vdesc *vdesc;
613 	struct buffer_head *bh, *n;
614 	LIST_HEAD(buffers);
615 	u64 ino;
616 	__u64 cno;
617 	int i, ret;
618 
619 	for (i = 0, vdesc = buf; i < nmembs; ) {
620 		ino = vdesc->vd_ino;
621 		cno = vdesc->vd_cno;
622 		inode = nilfs_iget_for_gc(sb, ino, cno);
623 		if (IS_ERR(inode)) {
624 			ret = PTR_ERR(inode);
625 			goto failed;
626 		}
627 		if (list_empty(&NILFS_I(inode)->i_dirty)) {
628 			/*
629 			 * Add the inode to GC inode list. Garbage Collection
630 			 * is serialized and no two processes manipulate the
631 			 * list simultaneously.
632 			 */
633 			igrab(inode);
634 			list_add(&NILFS_I(inode)->i_dirty,
635 				 &nilfs->ns_gc_inodes);
636 		}
637 
638 		do {
639 			ret = nilfs_ioctl_move_inode_block(inode, vdesc,
640 							   &buffers);
641 			if (unlikely(ret < 0)) {
642 				iput(inode);
643 				goto failed;
644 			}
645 			vdesc++;
646 		} while (++i < nmembs &&
647 			 vdesc->vd_ino == ino && vdesc->vd_cno == cno);
648 
649 		iput(inode); /* The inode still remains in GC inode list */
650 	}
651 
652 	list_for_each_entry_safe(bh, n, &buffers, b_assoc_buffers) {
653 		ret = nilfs_gccache_wait_and_mark_dirty(bh);
654 		if (unlikely(ret < 0)) {
655 			WARN_ON(ret == -EEXIST);
656 			goto failed;
657 		}
658 		list_del_init(&bh->b_assoc_buffers);
659 		brelse(bh);
660 	}
661 	return nmembs;
662 
663  failed:
664 	list_for_each_entry_safe(bh, n, &buffers, b_assoc_buffers) {
665 		list_del_init(&bh->b_assoc_buffers);
666 		brelse(bh);
667 	}
668 	return ret;
669 }
670 
671 /**
672  * nilfs_ioctl_delete_checkpoints - delete checkpoints
673  * @nilfs: nilfs object
674  * @argv: vector of arguments from userspace
675  * @buf: array of periods of checkpoints numbers
676  *
677  * Description: nilfs_ioctl_delete_checkpoints() function deletes checkpoints
678  * in the period from p_start to p_end, excluding p_end itself. The checkpoints
679  * which have been already deleted are ignored.
680  *
681  * Return: Number of processed nilfs_period structures on success, or one of
682  * the following negative error codes on failure:
683  * * %-EINVAL	- invalid checkpoints.
684  * * %-EIO	- I/O error.
685  * * %-ENOMEM	- Insufficient memory available.
686  */
687 static int nilfs_ioctl_delete_checkpoints(struct the_nilfs *nilfs,
688 					  struct nilfs_argv *argv, void *buf)
689 {
690 	size_t nmembs = argv->v_nmembs;
691 	struct inode *cpfile = nilfs->ns_cpfile;
692 	struct nilfs_period *periods = buf;
693 	int ret, i;
694 
695 	for (i = 0; i < nmembs; i++) {
696 		ret = nilfs_cpfile_delete_checkpoints(
697 			cpfile, periods[i].p_start, periods[i].p_end);
698 		if (ret < 0)
699 			return ret;
700 	}
701 	return nmembs;
702 }
703 
704 /**
705  * nilfs_ioctl_free_vblocknrs - free virtual block numbers
706  * @nilfs: nilfs object
707  * @argv: vector of arguments from userspace
708  * @buf: array of virtual block numbers
709  *
710  * Description: nilfs_ioctl_free_vblocknrs() function frees
711  * the virtual block numbers specified by @buf and @argv->v_nmembs.
712  *
713  * Return: Number of processed virtual block numbers on success, or one of the
714  * following negative error codes on failure:
715  * * %-EIO	- I/O error.
716  * * %-ENOENT	- Unallocated virtual block number.
717  * * %-ENOMEM	- Insufficient memory available.
718  */
719 static int nilfs_ioctl_free_vblocknrs(struct the_nilfs *nilfs,
720 				      struct nilfs_argv *argv, void *buf)
721 {
722 	size_t nmembs = argv->v_nmembs;
723 	int ret;
724 
725 	ret = nilfs_dat_freev(nilfs->ns_dat, buf, nmembs);
726 
727 	return (ret < 0) ? ret : nmembs;
728 }
729 
730 /**
731  * nilfs_ioctl_mark_blocks_dirty - mark blocks dirty
732  * @nilfs: nilfs object
733  * @argv: vector of arguments from userspace
734  * @buf: array of block descriptors
735  *
736  * Description: nilfs_ioctl_mark_blocks_dirty() function marks
737  * metadata file or data blocks as dirty.
738  *
739  * Return: Number of processed block descriptors on success, or one of the
740  * following negative error codes on failure:
741  * * %-EIO	- I/O error.
742  * * %-ENOENT	- Non-existent block (hole block).
743  * * %-ENOMEM	- Insufficient memory available.
744  */
745 static int nilfs_ioctl_mark_blocks_dirty(struct the_nilfs *nilfs,
746 					 struct nilfs_argv *argv, void *buf)
747 {
748 	size_t nmembs = argv->v_nmembs;
749 	struct nilfs_bmap *bmap = NILFS_I(nilfs->ns_dat)->i_bmap;
750 	struct nilfs_bdesc *bdescs = buf;
751 	struct buffer_head *bh;
752 	int ret, i;
753 
754 	for (i = 0; i < nmembs; i++) {
755 		/*
756 		 * bd_oblocknr must never be 0 as block 0
757 		 * is never a valid GC target block
758 		 */
759 		if (unlikely(!bdescs[i].bd_oblocknr))
760 			return -EINVAL;
761 		/* XXX: use macro or inline func to check liveness */
762 		ret = nilfs_bmap_lookup_at_level(bmap,
763 						 bdescs[i].bd_offset,
764 						 bdescs[i].bd_level + 1,
765 						 &bdescs[i].bd_blocknr);
766 		if (ret < 0) {
767 			if (ret != -ENOENT)
768 				return ret;
769 			bdescs[i].bd_blocknr = 0;
770 		}
771 		if (bdescs[i].bd_blocknr != bdescs[i].bd_oblocknr)
772 			/* skip dead block */
773 			continue;
774 		if (bdescs[i].bd_level == 0) {
775 			ret = nilfs_mdt_get_block(nilfs->ns_dat,
776 						  bdescs[i].bd_offset,
777 						  false, NULL, &bh);
778 			if (unlikely(ret)) {
779 				WARN_ON(ret == -ENOENT);
780 				return ret;
781 			}
782 			mark_buffer_dirty(bh);
783 			nilfs_mdt_mark_dirty(nilfs->ns_dat);
784 			put_bh(bh);
785 		} else {
786 			ret = nilfs_bmap_mark(bmap, bdescs[i].bd_offset,
787 					      bdescs[i].bd_level);
788 			if (ret < 0) {
789 				WARN_ON(ret == -ENOENT);
790 				return ret;
791 			}
792 		}
793 	}
794 	return nmembs;
795 }
796 
797 int nilfs_ioctl_prepare_clean_segments(struct the_nilfs *nilfs,
798 				       struct nilfs_argv *argv, void **kbufs)
799 {
800 	const char *msg;
801 	int ret;
802 
803 	ret = nilfs_ioctl_delete_checkpoints(nilfs, &argv[1], kbufs[1]);
804 	if (ret < 0) {
805 		/*
806 		 * can safely abort because checkpoints can be removed
807 		 * independently.
808 		 */
809 		msg = "cannot delete checkpoints";
810 		goto failed;
811 	}
812 	ret = nilfs_ioctl_free_vblocknrs(nilfs, &argv[2], kbufs[2]);
813 	if (ret < 0) {
814 		/*
815 		 * can safely abort because DAT file is updated atomically
816 		 * using a copy-on-write technique.
817 		 */
818 		msg = "cannot delete virtual blocks from DAT file";
819 		goto failed;
820 	}
821 	ret = nilfs_ioctl_mark_blocks_dirty(nilfs, &argv[3], kbufs[3]);
822 	if (ret < 0) {
823 		/*
824 		 * can safely abort because the operation is nondestructive.
825 		 */
826 		msg = "cannot mark copying blocks dirty";
827 		goto failed;
828 	}
829 	return 0;
830 
831  failed:
832 	nilfs_err(nilfs->ns_sb, "error %d preparing GC: %s", ret, msg);
833 	return ret;
834 }
835 
836 /**
837  * nilfs_ioctl_clean_segments - clean segments
838  * @inode: inode object
839  * @filp: file object
840  * @cmd: ioctl's request code
841  * @argp: pointer on argument from userspace
842  *
843  * Description: nilfs_ioctl_clean_segments() function makes garbage
844  * collection operation in the environment of requested parameters
845  * from userspace. The NILFS_IOCTL_CLEAN_SEGMENTS ioctl is used by
846  * nilfs_cleanerd daemon.
847  *
848  * Return: 0 on success, or a negative error code on failure.
849  */
850 static int nilfs_ioctl_clean_segments(struct inode *inode, struct file *filp,
851 				      unsigned int cmd, void __user *argp)
852 {
853 	struct nilfs_argv argv[5];
854 	static const size_t argsz[5] = {
855 		sizeof(struct nilfs_vdesc),
856 		sizeof(struct nilfs_period),
857 		sizeof(__u64),
858 		sizeof(struct nilfs_bdesc),
859 		sizeof(__u64),
860 	};
861 	void *kbufs[5];
862 	struct the_nilfs *nilfs;
863 	size_t len, nsegs;
864 	int n, ret;
865 
866 	if (!capable(CAP_SYS_ADMIN))
867 		return -EPERM;
868 
869 	ret = mnt_want_write_file(filp);
870 	if (ret)
871 		return ret;
872 
873 	ret = -EFAULT;
874 	if (copy_from_user(argv, argp, sizeof(argv)))
875 		goto out;
876 
877 	ret = -EINVAL;
878 	nsegs = argv[4].v_nmembs;
879 	if (argv[4].v_size != argsz[4])
880 		goto out;
881 
882 	/*
883 	 * argv[4] points to segment numbers this ioctl cleans.  We
884 	 * use kmalloc() for its buffer because the memory used for the
885 	 * segment numbers is small enough.
886 	 */
887 	kbufs[4] = memdup_array_user(u64_to_user_ptr(argv[4].v_base),
888 				     nsegs, sizeof(__u64));
889 	if (IS_ERR(kbufs[4])) {
890 		ret = PTR_ERR(kbufs[4]);
891 		goto out;
892 	}
893 	nilfs = inode->i_sb->s_fs_info;
894 
895 	for (n = 0; n < 4; n++) {
896 		ret = -EINVAL;
897 		if (argv[n].v_size != argsz[n])
898 			goto out_free;
899 
900 		if (argv[n].v_nmembs > nsegs * nilfs->ns_blocks_per_segment)
901 			goto out_free;
902 
903 		if (argv[n].v_nmembs >= UINT_MAX / argv[n].v_size)
904 			goto out_free;
905 
906 		len = argv[n].v_size * argv[n].v_nmembs;
907 		if (len == 0) {
908 			kbufs[n] = NULL;
909 			continue;
910 		}
911 
912 		kbufs[n] = vmemdup_user(u64_to_user_ptr(argv[n].v_base), len);
913 		if (IS_ERR(kbufs[n])) {
914 			ret = PTR_ERR(kbufs[n]);
915 			goto out_free;
916 		}
917 	}
918 
919 	/*
920 	 * nilfs_ioctl_move_blocks() will call nilfs_iget_for_gc(),
921 	 * which will operates an inode list without blocking.
922 	 * To protect the list from concurrent operations,
923 	 * nilfs_ioctl_move_blocks should be atomic operation.
924 	 */
925 	if (test_and_set_bit(THE_NILFS_GC_RUNNING, &nilfs->ns_flags)) {
926 		ret = -EBUSY;
927 		goto out_free;
928 	}
929 
930 	ret = nilfs_ioctl_move_blocks(inode->i_sb, &argv[0], kbufs[0]);
931 	if (ret < 0) {
932 		nilfs_err(inode->i_sb,
933 			  "error %d preparing GC: cannot read source blocks",
934 			  ret);
935 	} else {
936 		if (nilfs_sb_need_update(nilfs))
937 			set_nilfs_discontinued(nilfs);
938 		ret = nilfs_clean_segments(inode->i_sb, argv, kbufs);
939 	}
940 
941 	nilfs_remove_all_gcinodes(nilfs);
942 	clear_nilfs_gc_running(nilfs);
943 
944 out_free:
945 	while (--n >= 0)
946 		kvfree(kbufs[n]);
947 	kfree(kbufs[4]);
948 out:
949 	mnt_drop_write_file(filp);
950 	return ret;
951 }
952 
953 /**
954  * nilfs_ioctl_sync - make a checkpoint
955  * @inode: inode object
956  * @filp: file object
957  * @cmd: ioctl's request code
958  * @argp: pointer on argument from userspace
959  *
960  * Description: nilfs_ioctl_sync() function constructs a logical segment
961  * for checkpointing.  This function guarantees that all modified data
962  * and metadata are written out to the device when it successfully
963  * returned.
964  *
965  * Return: 0 on success, or one of the following negative error codes on
966  * failure:
967  * * %-EFAULT		- Failure during execution of requested operation.
968  * * %-EIO		- I/O error.
969  * * %-ENOMEM		- Insufficient memory available.
970  * * %-ENOSPC		- No space left on device (only in a panic state).
971  * * %-ERESTARTSYS	- Interrupted.
972  * * %-EROFS		- Read only filesystem.
973  */
974 static int nilfs_ioctl_sync(struct inode *inode, struct file *filp,
975 			    unsigned int cmd, void __user *argp)
976 {
977 	__u64 cno;
978 	int ret;
979 	struct the_nilfs *nilfs;
980 
981 	ret = nilfs_construct_segment(inode->i_sb);
982 	if (ret < 0)
983 		return ret;
984 
985 	nilfs = inode->i_sb->s_fs_info;
986 	ret = nilfs_flush_device(nilfs);
987 	if (ret < 0)
988 		return ret;
989 
990 	if (argp != NULL) {
991 		down_read(&nilfs->ns_segctor_sem);
992 		cno = nilfs->ns_cno - 1;
993 		up_read(&nilfs->ns_segctor_sem);
994 		if (copy_to_user(argp, &cno, sizeof(cno)))
995 			return -EFAULT;
996 	}
997 	return 0;
998 }
999 
1000 /**
1001  * nilfs_ioctl_resize - resize NILFS2 volume
1002  * @inode: inode object
1003  * @filp: file object
1004  * @argp: pointer on argument from userspace
1005  *
1006  * Return: 0 on success, or a negative error code on failure.
1007  */
1008 static int nilfs_ioctl_resize(struct inode *inode, struct file *filp,
1009 			      void __user *argp)
1010 {
1011 	__u64 newsize;
1012 	int ret = -EPERM;
1013 
1014 	if (!capable(CAP_SYS_ADMIN))
1015 		goto out;
1016 
1017 	ret = mnt_want_write_file(filp);
1018 	if (ret)
1019 		goto out;
1020 
1021 	ret = -EFAULT;
1022 	if (copy_from_user(&newsize, argp, sizeof(newsize)))
1023 		goto out_drop_write;
1024 
1025 	ret = nilfs_resize_fs(inode->i_sb, newsize);
1026 
1027 out_drop_write:
1028 	mnt_drop_write_file(filp);
1029 out:
1030 	return ret;
1031 }
1032 
1033 /**
1034  * nilfs_ioctl_trim_fs() - trim ioctl handle function
1035  * @inode: inode object
1036  * @argp: pointer on argument from userspace
1037  *
1038  * Description: nilfs_ioctl_trim_fs is the FITRIM ioctl handle function. It
1039  * checks the arguments from userspace and calls nilfs_sufile_trim_fs, which
1040  * performs the actual trim operation.
1041  *
1042  * Return: 0 on success, or a negative error code on failure.
1043  */
1044 static int nilfs_ioctl_trim_fs(struct inode *inode, void __user *argp)
1045 {
1046 	struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
1047 	struct fstrim_range range;
1048 	int ret;
1049 
1050 	if (!capable(CAP_SYS_ADMIN))
1051 		return -EPERM;
1052 
1053 	if (!bdev_max_discard_sectors(nilfs->ns_bdev))
1054 		return -EOPNOTSUPP;
1055 
1056 	if (copy_from_user(&range, argp, sizeof(range)))
1057 		return -EFAULT;
1058 
1059 	range.minlen = max_t(u64, range.minlen,
1060 			     bdev_discard_granularity(nilfs->ns_bdev));
1061 
1062 	down_read(&nilfs->ns_segctor_sem);
1063 	ret = nilfs_sufile_trim_fs(nilfs->ns_sufile, &range);
1064 	up_read(&nilfs->ns_segctor_sem);
1065 
1066 	if (ret < 0)
1067 		return ret;
1068 
1069 	if (copy_to_user(argp, &range, sizeof(range)))
1070 		return -EFAULT;
1071 
1072 	return 0;
1073 }
1074 
1075 /**
1076  * nilfs_ioctl_set_alloc_range - limit range of segments to be allocated
1077  * @inode: inode object
1078  * @argp: pointer on argument from userspace
1079  *
1080  * Description: nilfs_ioctl_set_alloc_range() function defines lower limit
1081  * of segments in bytes and upper limit of segments in bytes.
1082  * The NILFS_IOCTL_SET_ALLOC_RANGE is used by nilfs_resize utility.
1083  *
1084  * Return: 0 on success, or a negative error code on failure.
1085  */
1086 static int nilfs_ioctl_set_alloc_range(struct inode *inode, void __user *argp)
1087 {
1088 	struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
1089 	__u64 range[2];
1090 	__u64 minseg, maxseg;
1091 	unsigned long segbytes;
1092 	int ret = -EPERM;
1093 
1094 	if (!capable(CAP_SYS_ADMIN))
1095 		goto out;
1096 
1097 	ret = -EFAULT;
1098 	if (copy_from_user(range, argp, sizeof(__u64[2])))
1099 		goto out;
1100 
1101 	ret = -ERANGE;
1102 	if (range[1] > bdev_nr_bytes(inode->i_sb->s_bdev))
1103 		goto out;
1104 
1105 	segbytes = nilfs->ns_blocks_per_segment * nilfs->ns_blocksize;
1106 
1107 	minseg = range[0] + segbytes - 1;
1108 	minseg = div64_ul(minseg, segbytes);
1109 
1110 	if (range[1] < 4096)
1111 		goto out;
1112 
1113 	maxseg = NILFS_SB2_OFFSET_BYTES(range[1]);
1114 	if (maxseg < segbytes)
1115 		goto out;
1116 
1117 	maxseg = div64_ul(maxseg, segbytes);
1118 	maxseg--;
1119 
1120 	ret = nilfs_sufile_set_alloc_range(nilfs->ns_sufile, minseg, maxseg);
1121 out:
1122 	return ret;
1123 }
1124 
1125 /**
1126  * nilfs_ioctl_get_info - wrapping function of get metadata info
1127  * @inode: inode object
1128  * @filp: file object
1129  * @cmd: ioctl's request code
1130  * @argp: pointer on argument from userspace
1131  * @membsz: size of an item in bytes
1132  * @dofunc: concrete function of getting metadata info
1133  *
1134  * Description: nilfs_ioctl_get_info() gets metadata info by means of
1135  * calling dofunc() function.  The requested metadata information is copied
1136  * to userspace memory @argp.
1137  *
1138  * Return: 0 on success, or one of the following negative error codes on
1139  * failure:
1140  * * %-EFAULT	- Failure during execution of requested operation.
1141  * * %-EINVAL	- Invalid arguments from userspace.
1142  * * %-EIO	- I/O error.
1143  * * %-ENOMEM	- Insufficient memory available.
1144  */
1145 static int nilfs_ioctl_get_info(struct inode *inode, struct file *filp,
1146 				unsigned int cmd, void __user *argp,
1147 				size_t membsz,
1148 				ssize_t (*dofunc)(struct the_nilfs *,
1149 						  __u64 *, int,
1150 						  void *, size_t, size_t))
1151 
1152 {
1153 	struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
1154 	struct nilfs_argv argv;
1155 	int ret;
1156 
1157 	if (copy_from_user(&argv, argp, sizeof(argv)))
1158 		return -EFAULT;
1159 
1160 	if (argv.v_size < membsz)
1161 		return -EINVAL;
1162 
1163 	ret = nilfs_ioctl_wrap_copy(nilfs, &argv, _IOC_DIR(cmd), dofunc);
1164 	if (ret < 0)
1165 		return ret;
1166 
1167 	if (copy_to_user(argp, &argv, sizeof(argv)))
1168 		ret = -EFAULT;
1169 	return ret;
1170 }
1171 
1172 /**
1173  * nilfs_ioctl_set_suinfo - set segment usage info
1174  * @inode: inode object
1175  * @filp: file object
1176  * @cmd: ioctl's request code
1177  * @argp: pointer on argument from userspace
1178  *
1179  * Description: Expects an array of nilfs_suinfo_update structures
1180  * encapsulated in nilfs_argv and updates the segment usage info
1181  * according to the flags in nilfs_suinfo_update.
1182  *
1183  * Return: 0 on success, or one of the following negative error codes on
1184  * failure:
1185  * * %-EEXIST	- Block conflict detected.
1186  * * %-EFAULT	- Error copying input data.
1187  * * %-EINVAL	- Invalid values in input (segment number, flags or nblocks).
1188  * * %-EIO	- I/O error.
1189  * * %-ENOMEM	- Insufficient memory available.
1190  * * %-EPERM	- Not enough permissions.
1191  */
1192 static int nilfs_ioctl_set_suinfo(struct inode *inode, struct file *filp,
1193 				unsigned int cmd, void __user *argp)
1194 {
1195 	struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
1196 	struct nilfs_transaction_info ti;
1197 	struct nilfs_argv argv;
1198 	size_t len;
1199 	void *kbuf;
1200 	int ret;
1201 
1202 	if (!capable(CAP_SYS_ADMIN))
1203 		return -EPERM;
1204 
1205 	ret = mnt_want_write_file(filp);
1206 	if (ret)
1207 		return ret;
1208 
1209 	ret = -EFAULT;
1210 	if (copy_from_user(&argv, argp, sizeof(argv)))
1211 		goto out;
1212 
1213 	ret = -EINVAL;
1214 	if (argv.v_size < sizeof(struct nilfs_suinfo_update))
1215 		goto out;
1216 
1217 	if (argv.v_nmembs > nilfs->ns_nsegments)
1218 		goto out;
1219 
1220 	if (argv.v_nmembs >= UINT_MAX / argv.v_size)
1221 		goto out;
1222 
1223 	len = argv.v_size * argv.v_nmembs;
1224 	if (!len) {
1225 		ret = 0;
1226 		goto out;
1227 	}
1228 
1229 	kbuf = vmemdup_user(u64_to_user_ptr(argv.v_base), len);
1230 	if (IS_ERR(kbuf)) {
1231 		ret = PTR_ERR(kbuf);
1232 		goto out;
1233 	}
1234 
1235 	nilfs_transaction_begin(inode->i_sb, &ti, 0);
1236 	ret = nilfs_sufile_set_suinfo(nilfs->ns_sufile, kbuf, argv.v_size,
1237 			argv.v_nmembs);
1238 	if (unlikely(ret < 0))
1239 		nilfs_transaction_abort(inode->i_sb);
1240 	else
1241 		nilfs_transaction_commit(inode->i_sb); /* never fails */
1242 
1243 	kvfree(kbuf);
1244 out:
1245 	mnt_drop_write_file(filp);
1246 	return ret;
1247 }
1248 
1249 /**
1250  * nilfs_ioctl_get_fslabel - get the volume name of the file system
1251  * @sb:   super block instance
1252  * @argp: pointer to userspace memory where the volume name should be stored
1253  *
1254  * Return: 0 on success, %-EFAULT if copying to userspace memory fails.
1255  */
1256 static int nilfs_ioctl_get_fslabel(struct super_block *sb, void __user *argp)
1257 {
1258 	struct the_nilfs *nilfs = sb->s_fs_info;
1259 	char label[NILFS_MAX_VOLUME_NAME + 1];
1260 
1261 	BUILD_BUG_ON(NILFS_MAX_VOLUME_NAME >= FSLABEL_MAX);
1262 
1263 	down_read(&nilfs->ns_sem);
1264 	memtostr_pad(label, nilfs->ns_sbp[0]->s_volume_name);
1265 	up_read(&nilfs->ns_sem);
1266 
1267 	if (copy_to_user(argp, label, sizeof(label)))
1268 		return -EFAULT;
1269 	return 0;
1270 }
1271 
1272 /**
1273  * nilfs_ioctl_set_fslabel - set the volume name of the file system
1274  * @sb:   super block instance
1275  * @filp: file object
1276  * @argp: pointer to userspace memory that contains the volume name
1277  *
1278  * Return: 0 on success, or one of the following negative error codes on
1279  * failure:
1280  * * %-EFAULT	- Error copying input data.
1281  * * %-EINVAL	- Label length exceeds record size in superblock.
1282  * * %-EIO	- I/O error.
1283  * * %-EPERM	- Operation not permitted (insufficient permissions).
1284  * * %-EROFS	- Read only file system.
1285  */
1286 static int nilfs_ioctl_set_fslabel(struct super_block *sb, struct file *filp,
1287 				   void __user *argp)
1288 {
1289 	char label[NILFS_MAX_VOLUME_NAME + 1];
1290 	struct the_nilfs *nilfs = sb->s_fs_info;
1291 	struct nilfs_super_block **sbp;
1292 	size_t len;
1293 	int ret;
1294 
1295 	if (!capable(CAP_SYS_ADMIN))
1296 		return -EPERM;
1297 
1298 	ret = mnt_want_write_file(filp);
1299 	if (ret)
1300 		return ret;
1301 
1302 	if (copy_from_user(label, argp, NILFS_MAX_VOLUME_NAME + 1)) {
1303 		ret = -EFAULT;
1304 		goto out_drop_write;
1305 	}
1306 
1307 	len = strnlen(label, NILFS_MAX_VOLUME_NAME + 1);
1308 	if (len > NILFS_MAX_VOLUME_NAME) {
1309 		nilfs_err(sb, "unable to set label with more than %zu bytes",
1310 			  NILFS_MAX_VOLUME_NAME);
1311 		ret = -EINVAL;
1312 		goto out_drop_write;
1313 	}
1314 
1315 	down_write(&nilfs->ns_sem);
1316 	sbp = nilfs_prepare_super(sb, false);
1317 	if (unlikely(!sbp)) {
1318 		ret = -EIO;
1319 		goto out_unlock;
1320 	}
1321 
1322 	strtomem_pad(sbp[0]->s_volume_name, label, 0);
1323 	if (sbp[1])
1324 		strtomem_pad(sbp[1]->s_volume_name, label, 0);
1325 
1326 	ret = nilfs_commit_super(sb, NILFS_SB_COMMIT_ALL);
1327 
1328 out_unlock:
1329 	up_write(&nilfs->ns_sem);
1330 out_drop_write:
1331 	mnt_drop_write_file(filp);
1332 	return ret;
1333 }
1334 
1335 long nilfs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1336 {
1337 	struct inode *inode = file_inode(filp);
1338 	void __user *argp = (void __user *)arg;
1339 
1340 	switch (cmd) {
1341 	case FS_IOC_GETVERSION:
1342 		return nilfs_ioctl_getversion(inode, argp);
1343 	case NILFS_IOCTL_CHANGE_CPMODE:
1344 		return nilfs_ioctl_change_cpmode(inode, filp, cmd, argp);
1345 	case NILFS_IOCTL_DELETE_CHECKPOINT:
1346 		return nilfs_ioctl_delete_checkpoint(inode, filp, cmd, argp);
1347 	case NILFS_IOCTL_GET_CPINFO:
1348 		return nilfs_ioctl_get_info(inode, filp, cmd, argp,
1349 					    sizeof(struct nilfs_cpinfo),
1350 					    nilfs_ioctl_do_get_cpinfo);
1351 	case NILFS_IOCTL_GET_CPSTAT:
1352 		return nilfs_ioctl_get_cpstat(inode, filp, cmd, argp);
1353 	case NILFS_IOCTL_GET_SUINFO:
1354 		return nilfs_ioctl_get_info(inode, filp, cmd, argp,
1355 					    sizeof(struct nilfs_suinfo),
1356 					    nilfs_ioctl_do_get_suinfo);
1357 	case NILFS_IOCTL_SET_SUINFO:
1358 		return nilfs_ioctl_set_suinfo(inode, filp, cmd, argp);
1359 	case NILFS_IOCTL_GET_SUSTAT:
1360 		return nilfs_ioctl_get_sustat(inode, filp, cmd, argp);
1361 	case NILFS_IOCTL_GET_VINFO:
1362 		return nilfs_ioctl_get_info(inode, filp, cmd, argp,
1363 					    sizeof(struct nilfs_vinfo),
1364 					    nilfs_ioctl_do_get_vinfo);
1365 	case NILFS_IOCTL_GET_BDESCS:
1366 		return nilfs_ioctl_get_bdescs(inode, filp, cmd, argp);
1367 	case NILFS_IOCTL_CLEAN_SEGMENTS:
1368 		return nilfs_ioctl_clean_segments(inode, filp, cmd, argp);
1369 	case NILFS_IOCTL_SYNC:
1370 		return nilfs_ioctl_sync(inode, filp, cmd, argp);
1371 	case NILFS_IOCTL_RESIZE:
1372 		return nilfs_ioctl_resize(inode, filp, argp);
1373 	case NILFS_IOCTL_SET_ALLOC_RANGE:
1374 		return nilfs_ioctl_set_alloc_range(inode, argp);
1375 	case FITRIM:
1376 		return nilfs_ioctl_trim_fs(inode, argp);
1377 	case FS_IOC_GETFSLABEL:
1378 		return nilfs_ioctl_get_fslabel(inode->i_sb, argp);
1379 	case FS_IOC_SETFSLABEL:
1380 		return nilfs_ioctl_set_fslabel(inode->i_sb, filp, argp);
1381 	default:
1382 		return -ENOTTY;
1383 	}
1384 }
1385 
1386 #ifdef CONFIG_COMPAT
1387 long nilfs_compat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1388 {
1389 	switch (cmd) {
1390 	case FS_IOC32_GETVERSION:
1391 		cmd = FS_IOC_GETVERSION;
1392 		break;
1393 	case NILFS_IOCTL_CHANGE_CPMODE:
1394 	case NILFS_IOCTL_DELETE_CHECKPOINT:
1395 	case NILFS_IOCTL_GET_CPINFO:
1396 	case NILFS_IOCTL_GET_CPSTAT:
1397 	case NILFS_IOCTL_GET_SUINFO:
1398 	case NILFS_IOCTL_SET_SUINFO:
1399 	case NILFS_IOCTL_GET_SUSTAT:
1400 	case NILFS_IOCTL_GET_VINFO:
1401 	case NILFS_IOCTL_GET_BDESCS:
1402 	case NILFS_IOCTL_CLEAN_SEGMENTS:
1403 	case NILFS_IOCTL_SYNC:
1404 	case NILFS_IOCTL_RESIZE:
1405 	case NILFS_IOCTL_SET_ALLOC_RANGE:
1406 	case FITRIM:
1407 	case FS_IOC_GETFSLABEL:
1408 	case FS_IOC_SETFSLABEL:
1409 		break;
1410 	default:
1411 		return -ENOIOCTLCMD;
1412 	}
1413 	return nilfs_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
1414 }
1415 #endif
1416