]> Git Repo - linux.git/blob - fs/ext4/ioctl.c
ext4: calculate and verify checksums of directory leaf blocks
[linux.git] / fs / ext4 / ioctl.c
1 /*
2  * linux/fs/ext4/ioctl.c
3  *
4  * Copyright (C) 1993, 1994, 1995
5  * Remy Card ([email protected])
6  * Laboratoire MASI - Institut Blaise Pascal
7  * Universite Pierre et Marie Curie (Paris VI)
8  */
9
10 #include <linux/fs.h>
11 #include <linux/jbd2.h>
12 #include <linux/capability.h>
13 #include <linux/time.h>
14 #include <linux/compat.h>
15 #include <linux/mount.h>
16 #include <linux/file.h>
17 #include <asm/uaccess.h>
18 #include "ext4_jbd2.h"
19 #include "ext4.h"
20
21 #define MAX_32_NUM ((((unsigned long long) 1) << 32) - 1)
22
23 long ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
24 {
25         struct inode *inode = filp->f_dentry->d_inode;
26         struct super_block *sb = inode->i_sb;
27         struct ext4_inode_info *ei = EXT4_I(inode);
28         unsigned int flags;
29
30         ext4_debug("cmd = %u, arg = %lu\n", cmd, arg);
31
32         switch (cmd) {
33         case EXT4_IOC_GETFLAGS:
34                 ext4_get_inode_flags(ei);
35                 flags = ei->i_flags & EXT4_FL_USER_VISIBLE;
36                 return put_user(flags, (int __user *) arg);
37         case EXT4_IOC_SETFLAGS: {
38                 handle_t *handle = NULL;
39                 int err, migrate = 0;
40                 struct ext4_iloc iloc;
41                 unsigned int oldflags;
42                 unsigned int jflag;
43
44                 if (!inode_owner_or_capable(inode))
45                         return -EACCES;
46
47                 if (get_user(flags, (int __user *) arg))
48                         return -EFAULT;
49
50                 err = mnt_want_write_file(filp);
51                 if (err)
52                         return err;
53
54                 flags = ext4_mask_flags(inode->i_mode, flags);
55
56                 err = -EPERM;
57                 mutex_lock(&inode->i_mutex);
58                 /* Is it quota file? Do not allow user to mess with it */
59                 if (IS_NOQUOTA(inode))
60                         goto flags_out;
61
62                 oldflags = ei->i_flags;
63
64                 /* The JOURNAL_DATA flag is modifiable only by root */
65                 jflag = flags & EXT4_JOURNAL_DATA_FL;
66
67                 /*
68                  * The IMMUTABLE and APPEND_ONLY flags can only be changed by
69                  * the relevant capability.
70                  *
71                  * This test looks nicer. Thanks to Pauline Middelink
72                  */
73                 if ((flags ^ oldflags) & (EXT4_APPEND_FL | EXT4_IMMUTABLE_FL)) {
74                         if (!capable(CAP_LINUX_IMMUTABLE))
75                                 goto flags_out;
76                 }
77
78                 /*
79                  * The JOURNAL_DATA flag can only be changed by
80                  * the relevant capability.
81                  */
82                 if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) {
83                         if (!capable(CAP_SYS_RESOURCE))
84                                 goto flags_out;
85                 }
86                 if (oldflags & EXT4_EXTENTS_FL) {
87                         /* We don't support clearning extent flags */
88                         if (!(flags & EXT4_EXTENTS_FL)) {
89                                 err = -EOPNOTSUPP;
90                                 goto flags_out;
91                         }
92                 } else if (flags & EXT4_EXTENTS_FL) {
93                         /* migrate the file */
94                         migrate = 1;
95                         flags &= ~EXT4_EXTENTS_FL;
96                 }
97
98                 if (flags & EXT4_EOFBLOCKS_FL) {
99                         /* we don't support adding EOFBLOCKS flag */
100                         if (!(oldflags & EXT4_EOFBLOCKS_FL)) {
101                                 err = -EOPNOTSUPP;
102                                 goto flags_out;
103                         }
104                 } else if (oldflags & EXT4_EOFBLOCKS_FL)
105                         ext4_truncate(inode);
106
107                 handle = ext4_journal_start(inode, 1);
108                 if (IS_ERR(handle)) {
109                         err = PTR_ERR(handle);
110                         goto flags_out;
111                 }
112                 if (IS_SYNC(inode))
113                         ext4_handle_sync(handle);
114                 err = ext4_reserve_inode_write(handle, inode, &iloc);
115                 if (err)
116                         goto flags_err;
117
118                 flags = flags & EXT4_FL_USER_MODIFIABLE;
119                 flags |= oldflags & ~EXT4_FL_USER_MODIFIABLE;
120                 ei->i_flags = flags;
121
122                 ext4_set_inode_flags(inode);
123                 inode->i_ctime = ext4_current_time(inode);
124
125                 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
126 flags_err:
127                 ext4_journal_stop(handle);
128                 if (err)
129                         goto flags_out;
130
131                 if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL))
132                         err = ext4_change_inode_journal_flag(inode, jflag);
133                 if (err)
134                         goto flags_out;
135                 if (migrate)
136                         err = ext4_ext_migrate(inode);
137 flags_out:
138                 mutex_unlock(&inode->i_mutex);
139                 mnt_drop_write_file(filp);
140                 return err;
141         }
142         case EXT4_IOC_GETVERSION:
143         case EXT4_IOC_GETVERSION_OLD:
144                 return put_user(inode->i_generation, (int __user *) arg);
145         case EXT4_IOC_SETVERSION:
146         case EXT4_IOC_SETVERSION_OLD: {
147                 handle_t *handle;
148                 struct ext4_iloc iloc;
149                 __u32 generation;
150                 int err;
151
152                 if (!inode_owner_or_capable(inode))
153                         return -EPERM;
154
155                 if (EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
156                                 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) {
157                         ext4_warning(sb, "Setting inode version is not "
158                                      "supported with metadata_csum enabled.");
159                         return -ENOTTY;
160                 }
161
162                 err = mnt_want_write_file(filp);
163                 if (err)
164                         return err;
165                 if (get_user(generation, (int __user *) arg)) {
166                         err = -EFAULT;
167                         goto setversion_out;
168                 }
169
170                 mutex_lock(&inode->i_mutex);
171                 handle = ext4_journal_start(inode, 1);
172                 if (IS_ERR(handle)) {
173                         err = PTR_ERR(handle);
174                         goto unlock_out;
175                 }
176                 err = ext4_reserve_inode_write(handle, inode, &iloc);
177                 if (err == 0) {
178                         inode->i_ctime = ext4_current_time(inode);
179                         inode->i_generation = generation;
180                         err = ext4_mark_iloc_dirty(handle, inode, &iloc);
181                 }
182                 ext4_journal_stop(handle);
183
184 unlock_out:
185                 mutex_unlock(&inode->i_mutex);
186 setversion_out:
187                 mnt_drop_write_file(filp);
188                 return err;
189         }
190         case EXT4_IOC_GROUP_EXTEND: {
191                 ext4_fsblk_t n_blocks_count;
192                 int err, err2=0;
193
194                 err = ext4_resize_begin(sb);
195                 if (err)
196                         return err;
197
198                 if (get_user(n_blocks_count, (__u32 __user *)arg)) {
199                         err = -EFAULT;
200                         goto group_extend_out;
201                 }
202
203                 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
204                                EXT4_FEATURE_RO_COMPAT_BIGALLOC)) {
205                         ext4_msg(sb, KERN_ERR,
206                                  "Online resizing not supported with bigalloc");
207                         err = -EOPNOTSUPP;
208                         goto group_extend_out;
209                 }
210
211                 err = mnt_want_write_file(filp);
212                 if (err)
213                         goto group_extend_out;
214
215                 err = ext4_group_extend(sb, EXT4_SB(sb)->s_es, n_blocks_count);
216                 if (EXT4_SB(sb)->s_journal) {
217                         jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
218                         err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
219                         jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
220                 }
221                 if (err == 0)
222                         err = err2;
223                 mnt_drop_write_file(filp);
224 group_extend_out:
225                 ext4_resize_end(sb);
226                 return err;
227         }
228
229         case EXT4_IOC_MOVE_EXT: {
230                 struct move_extent me;
231                 struct file *donor_filp;
232                 int err;
233
234                 if (!(filp->f_mode & FMODE_READ) ||
235                     !(filp->f_mode & FMODE_WRITE))
236                         return -EBADF;
237
238                 if (copy_from_user(&me,
239                         (struct move_extent __user *)arg, sizeof(me)))
240                         return -EFAULT;
241                 me.moved_len = 0;
242
243                 donor_filp = fget(me.donor_fd);
244                 if (!donor_filp)
245                         return -EBADF;
246
247                 if (!(donor_filp->f_mode & FMODE_WRITE)) {
248                         err = -EBADF;
249                         goto mext_out;
250                 }
251
252                 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
253                                EXT4_FEATURE_RO_COMPAT_BIGALLOC)) {
254                         ext4_msg(sb, KERN_ERR,
255                                  "Online defrag not supported with bigalloc");
256                         return -EOPNOTSUPP;
257                 }
258
259                 err = mnt_want_write_file(filp);
260                 if (err)
261                         goto mext_out;
262
263                 err = ext4_move_extents(filp, donor_filp, me.orig_start,
264                                         me.donor_start, me.len, &me.moved_len);
265                 mnt_drop_write_file(filp);
266                 mnt_drop_write(filp->f_path.mnt);
267
268                 if (copy_to_user((struct move_extent __user *)arg,
269                                  &me, sizeof(me)))
270                         err = -EFAULT;
271 mext_out:
272                 fput(donor_filp);
273                 return err;
274         }
275
276         case EXT4_IOC_GROUP_ADD: {
277                 struct ext4_new_group_data input;
278                 int err, err2=0;
279
280                 err = ext4_resize_begin(sb);
281                 if (err)
282                         return err;
283
284                 if (copy_from_user(&input, (struct ext4_new_group_input __user *)arg,
285                                 sizeof(input))) {
286                         err = -EFAULT;
287                         goto group_add_out;
288                 }
289
290                 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
291                                EXT4_FEATURE_RO_COMPAT_BIGALLOC)) {
292                         ext4_msg(sb, KERN_ERR,
293                                  "Online resizing not supported with bigalloc");
294                         err = -EOPNOTSUPP;
295                         goto group_add_out;
296                 }
297
298                 err = mnt_want_write_file(filp);
299                 if (err)
300                         goto group_add_out;
301
302                 err = ext4_group_add(sb, &input);
303                 if (EXT4_SB(sb)->s_journal) {
304                         jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
305                         err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
306                         jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
307                 }
308                 if (err == 0)
309                         err = err2;
310                 mnt_drop_write_file(filp);
311 group_add_out:
312                 ext4_resize_end(sb);
313                 return err;
314         }
315
316         case EXT4_IOC_MIGRATE:
317         {
318                 int err;
319                 if (!inode_owner_or_capable(inode))
320                         return -EACCES;
321
322                 err = mnt_want_write_file(filp);
323                 if (err)
324                         return err;
325                 /*
326                  * inode_mutex prevent write and truncate on the file.
327                  * Read still goes through. We take i_data_sem in
328                  * ext4_ext_swap_inode_data before we switch the
329                  * inode format to prevent read.
330                  */
331                 mutex_lock(&(inode->i_mutex));
332                 err = ext4_ext_migrate(inode);
333                 mutex_unlock(&(inode->i_mutex));
334                 mnt_drop_write_file(filp);
335                 return err;
336         }
337
338         case EXT4_IOC_ALLOC_DA_BLKS:
339         {
340                 int err;
341                 if (!inode_owner_or_capable(inode))
342                         return -EACCES;
343
344                 err = mnt_want_write_file(filp);
345                 if (err)
346                         return err;
347                 err = ext4_alloc_da_blocks(inode);
348                 mnt_drop_write_file(filp);
349                 return err;
350         }
351
352         case EXT4_IOC_RESIZE_FS: {
353                 ext4_fsblk_t n_blocks_count;
354                 struct super_block *sb = inode->i_sb;
355                 int err = 0, err2 = 0;
356
357                 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
358                                EXT4_FEATURE_RO_COMPAT_BIGALLOC)) {
359                         ext4_msg(sb, KERN_ERR,
360                                  "Online resizing not (yet) supported with bigalloc");
361                         return -EOPNOTSUPP;
362                 }
363
364                 if (EXT4_HAS_INCOMPAT_FEATURE(sb,
365                                EXT4_FEATURE_INCOMPAT_META_BG)) {
366                         ext4_msg(sb, KERN_ERR,
367                                  "Online resizing not (yet) supported with meta_bg");
368                         return -EOPNOTSUPP;
369                 }
370
371                 if (copy_from_user(&n_blocks_count, (__u64 __user *)arg,
372                                    sizeof(__u64))) {
373                         return -EFAULT;
374                 }
375
376                 if (n_blocks_count > MAX_32_NUM &&
377                     !EXT4_HAS_INCOMPAT_FEATURE(sb,
378                                                EXT4_FEATURE_INCOMPAT_64BIT)) {
379                         ext4_msg(sb, KERN_ERR,
380                                  "File system only supports 32-bit block numbers");
381                         return -EOPNOTSUPP;
382                 }
383
384                 err = ext4_resize_begin(sb);
385                 if (err)
386                         return err;
387
388                 err = mnt_want_write(filp->f_path.mnt);
389                 if (err)
390                         goto resizefs_out;
391
392                 err = ext4_resize_fs(sb, n_blocks_count);
393                 if (EXT4_SB(sb)->s_journal) {
394                         jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
395                         err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
396                         jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
397                 }
398                 if (err == 0)
399                         err = err2;
400                 mnt_drop_write(filp->f_path.mnt);
401 resizefs_out:
402                 ext4_resize_end(sb);
403                 return err;
404         }
405
406         case FITRIM:
407         {
408                 struct request_queue *q = bdev_get_queue(sb->s_bdev);
409                 struct fstrim_range range;
410                 int ret = 0;
411
412                 if (!capable(CAP_SYS_ADMIN))
413                         return -EPERM;
414
415                 if (!blk_queue_discard(q))
416                         return -EOPNOTSUPP;
417
418                 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
419                                EXT4_FEATURE_RO_COMPAT_BIGALLOC)) {
420                         ext4_msg(sb, KERN_ERR,
421                                  "FITRIM not supported with bigalloc");
422                         return -EOPNOTSUPP;
423                 }
424
425                 if (copy_from_user(&range, (struct fstrim_range __user *)arg,
426                     sizeof(range)))
427                         return -EFAULT;
428
429                 range.minlen = max((unsigned int)range.minlen,
430                                    q->limits.discard_granularity);
431                 ret = ext4_trim_fs(sb, &range);
432                 if (ret < 0)
433                         return ret;
434
435                 if (copy_to_user((struct fstrim_range __user *)arg, &range,
436                     sizeof(range)))
437                         return -EFAULT;
438
439                 return 0;
440         }
441
442         default:
443                 return -ENOTTY;
444         }
445 }
446
447 #ifdef CONFIG_COMPAT
448 long ext4_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
449 {
450         /* These are just misnamed, they actually get/put from/to user an int */
451         switch (cmd) {
452         case EXT4_IOC32_GETFLAGS:
453                 cmd = EXT4_IOC_GETFLAGS;
454                 break;
455         case EXT4_IOC32_SETFLAGS:
456                 cmd = EXT4_IOC_SETFLAGS;
457                 break;
458         case EXT4_IOC32_GETVERSION:
459                 cmd = EXT4_IOC_GETVERSION;
460                 break;
461         case EXT4_IOC32_SETVERSION:
462                 cmd = EXT4_IOC_SETVERSION;
463                 break;
464         case EXT4_IOC32_GROUP_EXTEND:
465                 cmd = EXT4_IOC_GROUP_EXTEND;
466                 break;
467         case EXT4_IOC32_GETVERSION_OLD:
468                 cmd = EXT4_IOC_GETVERSION_OLD;
469                 break;
470         case EXT4_IOC32_SETVERSION_OLD:
471                 cmd = EXT4_IOC_SETVERSION_OLD;
472                 break;
473         case EXT4_IOC32_GETRSVSZ:
474                 cmd = EXT4_IOC_GETRSVSZ;
475                 break;
476         case EXT4_IOC32_SETRSVSZ:
477                 cmd = EXT4_IOC_SETRSVSZ;
478                 break;
479         case EXT4_IOC32_GROUP_ADD: {
480                 struct compat_ext4_new_group_input __user *uinput;
481                 struct ext4_new_group_input input;
482                 mm_segment_t old_fs;
483                 int err;
484
485                 uinput = compat_ptr(arg);
486                 err = get_user(input.group, &uinput->group);
487                 err |= get_user(input.block_bitmap, &uinput->block_bitmap);
488                 err |= get_user(input.inode_bitmap, &uinput->inode_bitmap);
489                 err |= get_user(input.inode_table, &uinput->inode_table);
490                 err |= get_user(input.blocks_count, &uinput->blocks_count);
491                 err |= get_user(input.reserved_blocks,
492                                 &uinput->reserved_blocks);
493                 if (err)
494                         return -EFAULT;
495                 old_fs = get_fs();
496                 set_fs(KERNEL_DS);
497                 err = ext4_ioctl(file, EXT4_IOC_GROUP_ADD,
498                                  (unsigned long) &input);
499                 set_fs(old_fs);
500                 return err;
501         }
502         case EXT4_IOC_MOVE_EXT:
503         case FITRIM:
504         case EXT4_IOC_RESIZE_FS:
505                 break;
506         default:
507                 return -ENOIOCTLCMD;
508         }
509         return ext4_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
510 }
511 #endif
This page took 0.068933 seconds and 4 git commands to generate.