~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/fs/ext4/ioctl.c

Version: ~ [ linux-5.4-rc7 ] ~ [ linux-5.3.11 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.84 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.154 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.201 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.201 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.77 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: GPL-2.0
  2 /*
  3  * linux/fs/ext4/ioctl.c
  4  *
  5  * Copyright (C) 1993, 1994, 1995
  6  * Remy Card (card@masi.ibp.fr)
  7  * Laboratoire MASI - Institut Blaise Pascal
  8  * Universite Pierre et Marie Curie (Paris VI)
  9  */
 10 
 11 #include <linux/fs.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 <linux/quotaops.h>
 18 #include <linux/random.h>
 19 #include <linux/uuid.h>
 20 #include <linux/uaccess.h>
 21 #include <linux/delay.h>
 22 #include <linux/iversion.h>
 23 #include "ext4_jbd2.h"
 24 #include "ext4.h"
 25 #include <linux/fsmap.h>
 26 #include "fsmap.h"
 27 #include <trace/events/ext4.h>
 28 
 29 /**
 30  * Swap memory between @a and @b for @len bytes.
 31  *
 32  * @a:          pointer to first memory area
 33  * @b:          pointer to second memory area
 34  * @len:        number of bytes to swap
 35  *
 36  */
 37 static void memswap(void *a, void *b, size_t len)
 38 {
 39         unsigned char *ap, *bp;
 40 
 41         ap = (unsigned char *)a;
 42         bp = (unsigned char *)b;
 43         while (len-- > 0) {
 44                 swap(*ap, *bp);
 45                 ap++;
 46                 bp++;
 47         }
 48 }
 49 
 50 /**
 51  * Swap i_data and associated attributes between @inode1 and @inode2.
 52  * This function is used for the primary swap between inode1 and inode2
 53  * and also to revert this primary swap in case of errors.
 54  *
 55  * Therefore you have to make sure, that calling this method twice
 56  * will revert all changes.
 57  *
 58  * @inode1:     pointer to first inode
 59  * @inode2:     pointer to second inode
 60  */
 61 static void swap_inode_data(struct inode *inode1, struct inode *inode2)
 62 {
 63         loff_t isize;
 64         struct ext4_inode_info *ei1;
 65         struct ext4_inode_info *ei2;
 66 
 67         ei1 = EXT4_I(inode1);
 68         ei2 = EXT4_I(inode2);
 69 
 70         swap(inode1->i_flags, inode2->i_flags);
 71         swap(inode1->i_version, inode2->i_version);
 72         swap(inode1->i_blocks, inode2->i_blocks);
 73         swap(inode1->i_bytes, inode2->i_bytes);
 74         swap(inode1->i_atime, inode2->i_atime);
 75         swap(inode1->i_mtime, inode2->i_mtime);
 76 
 77         memswap(ei1->i_data, ei2->i_data, sizeof(ei1->i_data));
 78         swap(ei1->i_flags, ei2->i_flags);
 79         swap(ei1->i_disksize, ei2->i_disksize);
 80         ext4_es_remove_extent(inode1, 0, EXT_MAX_BLOCKS);
 81         ext4_es_remove_extent(inode2, 0, EXT_MAX_BLOCKS);
 82 
 83         isize = i_size_read(inode1);
 84         i_size_write(inode1, i_size_read(inode2));
 85         i_size_write(inode2, isize);
 86 }
 87 
 88 /**
 89  * Swap the information from the given @inode and the inode
 90  * EXT4_BOOT_LOADER_INO. It will basically swap i_data and all other
 91  * important fields of the inodes.
 92  *
 93  * @sb:         the super block of the filesystem
 94  * @inode:      the inode to swap with EXT4_BOOT_LOADER_INO
 95  *
 96  */
 97 static long swap_inode_boot_loader(struct super_block *sb,
 98                                 struct inode *inode)
 99 {
100         handle_t *handle;
101         int err;
102         struct inode *inode_bl;
103         struct ext4_inode_info *ei_bl;
104 
105         if (inode->i_nlink != 1 || !S_ISREG(inode->i_mode))
106                 return -EINVAL;
107 
108         if (!inode_owner_or_capable(inode) || !capable(CAP_SYS_ADMIN))
109                 return -EPERM;
110 
111         inode_bl = ext4_iget(sb, EXT4_BOOT_LOADER_INO);
112         if (IS_ERR(inode_bl))
113                 return PTR_ERR(inode_bl);
114         ei_bl = EXT4_I(inode_bl);
115 
116         filemap_flush(inode->i_mapping);
117         filemap_flush(inode_bl->i_mapping);
118 
119         /* Protect orig inodes against a truncate and make sure,
120          * that only 1 swap_inode_boot_loader is running. */
121         lock_two_nondirectories(inode, inode_bl);
122 
123         truncate_inode_pages(&inode->i_data, 0);
124         truncate_inode_pages(&inode_bl->i_data, 0);
125 
126         /* Wait for all existing dio workers */
127         ext4_inode_block_unlocked_dio(inode);
128         ext4_inode_block_unlocked_dio(inode_bl);
129         inode_dio_wait(inode);
130         inode_dio_wait(inode_bl);
131 
132         handle = ext4_journal_start(inode_bl, EXT4_HT_MOVE_EXTENTS, 2);
133         if (IS_ERR(handle)) {
134                 err = -EINVAL;
135                 goto journal_err_out;
136         }
137 
138         /* Protect extent tree against block allocations via delalloc */
139         ext4_double_down_write_data_sem(inode, inode_bl);
140 
141         if (inode_bl->i_nlink == 0) {
142                 /* this inode has never been used as a BOOT_LOADER */
143                 set_nlink(inode_bl, 1);
144                 i_uid_write(inode_bl, 0);
145                 i_gid_write(inode_bl, 0);
146                 inode_bl->i_flags = 0;
147                 ei_bl->i_flags = 0;
148                 inode_set_iversion(inode_bl, 1);
149                 i_size_write(inode_bl, 0);
150                 inode_bl->i_mode = S_IFREG;
151                 if (ext4_has_feature_extents(sb)) {
152                         ext4_set_inode_flag(inode_bl, EXT4_INODE_EXTENTS);
153                         ext4_ext_tree_init(handle, inode_bl);
154                 } else
155                         memset(ei_bl->i_data, 0, sizeof(ei_bl->i_data));
156         }
157 
158         swap_inode_data(inode, inode_bl);
159 
160         inode->i_ctime = inode_bl->i_ctime = current_time(inode);
161 
162         inode->i_generation = prandom_u32();
163         inode_bl->i_generation = prandom_u32();
164 
165         ext4_discard_preallocations(inode);
166 
167         err = ext4_mark_inode_dirty(handle, inode);
168         if (err < 0) {
169                 ext4_warning(inode->i_sb,
170                         "couldn't mark inode #%lu dirty (err %d)",
171                         inode->i_ino, err);
172                 /* Revert all changes: */
173                 swap_inode_data(inode, inode_bl);
174         } else {
175                 err = ext4_mark_inode_dirty(handle, inode_bl);
176                 if (err < 0) {
177                         ext4_warning(inode_bl->i_sb,
178                                 "couldn't mark inode #%lu dirty (err %d)",
179                                 inode_bl->i_ino, err);
180                         /* Revert all changes: */
181                         swap_inode_data(inode, inode_bl);
182                         ext4_mark_inode_dirty(handle, inode);
183                 }
184         }
185         ext4_journal_stop(handle);
186         ext4_double_up_write_data_sem(inode, inode_bl);
187 
188 journal_err_out:
189         ext4_inode_resume_unlocked_dio(inode);
190         ext4_inode_resume_unlocked_dio(inode_bl);
191         unlock_two_nondirectories(inode, inode_bl);
192         iput(inode_bl);
193         return err;
194 }
195 
196 #ifdef CONFIG_EXT4_FS_ENCRYPTION
197 static int uuid_is_zero(__u8 u[16])
198 {
199         int     i;
200 
201         for (i = 0; i < 16; i++)
202                 if (u[i])
203                         return 0;
204         return 1;
205 }
206 #endif
207 
208 static int ext4_ioctl_setflags(struct inode *inode,
209                                unsigned int flags)
210 {
211         struct ext4_inode_info *ei = EXT4_I(inode);
212         handle_t *handle = NULL;
213         int err = -EPERM, migrate = 0;
214         struct ext4_iloc iloc;
215         unsigned int oldflags, mask, i;
216         unsigned int jflag;
217 
218         /* Is it quota file? Do not allow user to mess with it */
219         if (ext4_is_quota_file(inode))
220                 goto flags_out;
221 
222         oldflags = ei->i_flags;
223 
224         /* The JOURNAL_DATA flag is modifiable only by root */
225         jflag = flags & EXT4_JOURNAL_DATA_FL;
226 
227         /*
228          * The IMMUTABLE and APPEND_ONLY flags can only be changed by
229          * the relevant capability.
230          *
231          * This test looks nicer. Thanks to Pauline Middelink
232          */
233         if ((flags ^ oldflags) & (EXT4_APPEND_FL | EXT4_IMMUTABLE_FL)) {
234                 if (!capable(CAP_LINUX_IMMUTABLE))
235                         goto flags_out;
236         }
237 
238         /*
239          * The JOURNAL_DATA flag can only be changed by
240          * the relevant capability.
241          */
242         if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) {
243                 if (!capable(CAP_SYS_RESOURCE))
244                         goto flags_out;
245         }
246         if ((flags ^ oldflags) & EXT4_EXTENTS_FL)
247                 migrate = 1;
248 
249         if (flags & EXT4_EOFBLOCKS_FL) {
250                 /* we don't support adding EOFBLOCKS flag */
251                 if (!(oldflags & EXT4_EOFBLOCKS_FL)) {
252                         err = -EOPNOTSUPP;
253                         goto flags_out;
254                 }
255         } else if (oldflags & EXT4_EOFBLOCKS_FL) {
256                 err = ext4_truncate(inode);
257                 if (err)
258                         goto flags_out;
259         }
260 
261         handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
262         if (IS_ERR(handle)) {
263                 err = PTR_ERR(handle);
264                 goto flags_out;
265         }
266         if (IS_SYNC(inode))
267                 ext4_handle_sync(handle);
268         err = ext4_reserve_inode_write(handle, inode, &iloc);
269         if (err)
270                 goto flags_err;
271 
272         for (i = 0, mask = 1; i < 32; i++, mask <<= 1) {
273                 if (!(mask & EXT4_FL_USER_MODIFIABLE))
274                         continue;
275                 /* These flags get special treatment later */
276                 if (mask == EXT4_JOURNAL_DATA_FL || mask == EXT4_EXTENTS_FL)
277                         continue;
278                 if (mask & flags)
279                         ext4_set_inode_flag(inode, i);
280                 else
281                         ext4_clear_inode_flag(inode, i);
282         }
283 
284         ext4_set_inode_flags(inode);
285         inode->i_ctime = current_time(inode);
286 
287         err = ext4_mark_iloc_dirty(handle, inode, &iloc);
288 flags_err:
289         ext4_journal_stop(handle);
290         if (err)
291                 goto flags_out;
292 
293         if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) {
294                 /*
295                  * Changes to the journaling mode can cause unsafe changes to
296                  * S_DAX if we are using the DAX mount option.
297                  */
298                 if (test_opt(inode->i_sb, DAX)) {
299                         err = -EBUSY;
300                         goto flags_out;
301                 }
302 
303                 err = ext4_change_inode_journal_flag(inode, jflag);
304                 if (err)
305                         goto flags_out;
306         }
307         if (migrate) {
308                 if (flags & EXT4_EXTENTS_FL)
309                         err = ext4_ext_migrate(inode);
310                 else
311                         err = ext4_ind_migrate(inode);
312         }
313 
314 flags_out:
315         return err;
316 }
317 
318 #ifdef CONFIG_QUOTA
319 static int ext4_ioctl_setproject(struct file *filp, __u32 projid)
320 {
321         struct inode *inode = file_inode(filp);
322         struct super_block *sb = inode->i_sb;
323         struct ext4_inode_info *ei = EXT4_I(inode);
324         int err, rc;
325         handle_t *handle;
326         kprojid_t kprojid;
327         struct ext4_iloc iloc;
328         struct ext4_inode *raw_inode;
329         struct dquot *transfer_to[MAXQUOTAS] = { };
330 
331         if (!ext4_has_feature_project(sb)) {
332                 if (projid != EXT4_DEF_PROJID)
333                         return -EOPNOTSUPP;
334                 else
335                         return 0;
336         }
337 
338         if (EXT4_INODE_SIZE(sb) <= EXT4_GOOD_OLD_INODE_SIZE)
339                 return -EOPNOTSUPP;
340 
341         kprojid = make_kprojid(&init_user_ns, (projid_t)projid);
342 
343         if (projid_eq(kprojid, EXT4_I(inode)->i_projid))
344                 return 0;
345 
346         err = mnt_want_write_file(filp);
347         if (err)
348                 return err;
349 
350         err = -EPERM;
351         inode_lock(inode);
352         /* Is it quota file? Do not allow user to mess with it */
353         if (ext4_is_quota_file(inode))
354                 goto out_unlock;
355 
356         err = ext4_get_inode_loc(inode, &iloc);
357         if (err)
358                 goto out_unlock;
359 
360         raw_inode = ext4_raw_inode(&iloc);
361         if (!EXT4_FITS_IN_INODE(raw_inode, ei, i_projid)) {
362                 err = ext4_expand_extra_isize(inode,
363                                               EXT4_SB(sb)->s_want_extra_isize,
364                                               &iloc);
365                 if (err)
366                         goto out_unlock;
367         } else {
368                 brelse(iloc.bh);
369         }
370 
371         dquot_initialize(inode);
372 
373         handle = ext4_journal_start(inode, EXT4_HT_QUOTA,
374                 EXT4_QUOTA_INIT_BLOCKS(sb) +
375                 EXT4_QUOTA_DEL_BLOCKS(sb) + 3);
376         if (IS_ERR(handle)) {
377                 err = PTR_ERR(handle);
378                 goto out_unlock;
379         }
380 
381         err = ext4_reserve_inode_write(handle, inode, &iloc);
382         if (err)
383                 goto out_stop;
384 
385         transfer_to[PRJQUOTA] = dqget(sb, make_kqid_projid(kprojid));
386         if (!IS_ERR(transfer_to[PRJQUOTA])) {
387 
388                 /* __dquot_transfer() calls back ext4_get_inode_usage() which
389                  * counts xattr inode references.
390                  */
391                 down_read(&EXT4_I(inode)->xattr_sem);
392                 err = __dquot_transfer(inode, transfer_to);
393                 up_read(&EXT4_I(inode)->xattr_sem);
394                 dqput(transfer_to[PRJQUOTA]);
395                 if (err)
396                         goto out_dirty;
397         }
398 
399         EXT4_I(inode)->i_projid = kprojid;
400         inode->i_ctime = current_time(inode);
401 out_dirty:
402         rc = ext4_mark_iloc_dirty(handle, inode, &iloc);
403         if (!err)
404                 err = rc;
405 out_stop:
406         ext4_journal_stop(handle);
407 out_unlock:
408         inode_unlock(inode);
409         mnt_drop_write_file(filp);
410         return err;
411 }
412 #else
413 static int ext4_ioctl_setproject(struct file *filp, __u32 projid)
414 {
415         if (projid != EXT4_DEF_PROJID)
416                 return -EOPNOTSUPP;
417         return 0;
418 }
419 #endif
420 
421 /* Transfer internal flags to xflags */
422 static inline __u32 ext4_iflags_to_xflags(unsigned long iflags)
423 {
424         __u32 xflags = 0;
425 
426         if (iflags & EXT4_SYNC_FL)
427                 xflags |= FS_XFLAG_SYNC;
428         if (iflags & EXT4_IMMUTABLE_FL)
429                 xflags |= FS_XFLAG_IMMUTABLE;
430         if (iflags & EXT4_APPEND_FL)
431                 xflags |= FS_XFLAG_APPEND;
432         if (iflags & EXT4_NODUMP_FL)
433                 xflags |= FS_XFLAG_NODUMP;
434         if (iflags & EXT4_NOATIME_FL)
435                 xflags |= FS_XFLAG_NOATIME;
436         if (iflags & EXT4_PROJINHERIT_FL)
437                 xflags |= FS_XFLAG_PROJINHERIT;
438         return xflags;
439 }
440 
441 #define EXT4_SUPPORTED_FS_XFLAGS (FS_XFLAG_SYNC | FS_XFLAG_IMMUTABLE | \
442                                   FS_XFLAG_APPEND | FS_XFLAG_NODUMP | \
443                                   FS_XFLAG_NOATIME | FS_XFLAG_PROJINHERIT)
444 
445 /* Transfer xflags flags to internal */
446 static inline unsigned long ext4_xflags_to_iflags(__u32 xflags)
447 {
448         unsigned long iflags = 0;
449 
450         if (xflags & FS_XFLAG_SYNC)
451                 iflags |= EXT4_SYNC_FL;
452         if (xflags & FS_XFLAG_IMMUTABLE)
453                 iflags |= EXT4_IMMUTABLE_FL;
454         if (xflags & FS_XFLAG_APPEND)
455                 iflags |= EXT4_APPEND_FL;
456         if (xflags & FS_XFLAG_NODUMP)
457                 iflags |= EXT4_NODUMP_FL;
458         if (xflags & FS_XFLAG_NOATIME)
459                 iflags |= EXT4_NOATIME_FL;
460         if (xflags & FS_XFLAG_PROJINHERIT)
461                 iflags |= EXT4_PROJINHERIT_FL;
462 
463         return iflags;
464 }
465 
466 static int ext4_shutdown(struct super_block *sb, unsigned long arg)
467 {
468         struct ext4_sb_info *sbi = EXT4_SB(sb);
469         __u32 flags;
470 
471         if (!capable(CAP_SYS_ADMIN))
472                 return -EPERM;
473 
474         if (get_user(flags, (__u32 __user *)arg))
475                 return -EFAULT;
476 
477         if (flags > EXT4_GOING_FLAGS_NOLOGFLUSH)
478                 return -EINVAL;
479 
480         if (ext4_forced_shutdown(sbi))
481                 return 0;
482 
483         ext4_msg(sb, KERN_ALERT, "shut down requested (%d)", flags);
484 
485         switch (flags) {
486         case EXT4_GOING_FLAGS_DEFAULT:
487                 freeze_bdev(sb->s_bdev);
488                 set_bit(EXT4_FLAGS_SHUTDOWN, &sbi->s_ext4_flags);
489                 thaw_bdev(sb->s_bdev, sb);
490                 break;
491         case EXT4_GOING_FLAGS_LOGFLUSH:
492                 set_bit(EXT4_FLAGS_SHUTDOWN, &sbi->s_ext4_flags);
493                 if (sbi->s_journal && !is_journal_aborted(sbi->s_journal)) {
494                         (void) ext4_force_commit(sb);
495                         jbd2_journal_abort(sbi->s_journal, -ESHUTDOWN);
496                 }
497                 break;
498         case EXT4_GOING_FLAGS_NOLOGFLUSH:
499                 set_bit(EXT4_FLAGS_SHUTDOWN, &sbi->s_ext4_flags);
500                 if (sbi->s_journal && !is_journal_aborted(sbi->s_journal))
501                         jbd2_journal_abort(sbi->s_journal, -ESHUTDOWN);
502                 break;
503         default:
504                 return -EINVAL;
505         }
506         clear_opt(sb, DISCARD);
507         return 0;
508 }
509 
510 struct getfsmap_info {
511         struct super_block      *gi_sb;
512         struct fsmap_head __user *gi_data;
513         unsigned int            gi_idx;
514         __u32                   gi_last_flags;
515 };
516 
517 static int ext4_getfsmap_format(struct ext4_fsmap *xfm, void *priv)
518 {
519         struct getfsmap_info *info = priv;
520         struct fsmap fm;
521 
522         trace_ext4_getfsmap_mapping(info->gi_sb, xfm);
523 
524         info->gi_last_flags = xfm->fmr_flags;
525         ext4_fsmap_from_internal(info->gi_sb, &fm, xfm);
526         if (copy_to_user(&info->gi_data->fmh_recs[info->gi_idx++], &fm,
527                         sizeof(struct fsmap)))
528                 return -EFAULT;
529 
530         return 0;
531 }
532 
533 static int ext4_ioc_getfsmap(struct super_block *sb,
534                              struct fsmap_head __user *arg)
535 {
536         struct getfsmap_info info = {0};
537         struct ext4_fsmap_head xhead = {0};
538         struct fsmap_head head;
539         bool aborted = false;
540         int error;
541 
542         if (copy_from_user(&head, arg, sizeof(struct fsmap_head)))
543                 return -EFAULT;
544         if (memchr_inv(head.fmh_reserved, 0, sizeof(head.fmh_reserved)) ||
545             memchr_inv(head.fmh_keys[0].fmr_reserved, 0,
546                        sizeof(head.fmh_keys[0].fmr_reserved)) ||
547             memchr_inv(head.fmh_keys[1].fmr_reserved, 0,
548                        sizeof(head.fmh_keys[1].fmr_reserved)))
549                 return -EINVAL;
550         /*
551          * ext4 doesn't report file extents at all, so the only valid
552          * file offsets are the magic ones (all zeroes or all ones).
553          */
554         if (head.fmh_keys[0].fmr_offset ||
555             (head.fmh_keys[1].fmr_offset != 0 &&
556              head.fmh_keys[1].fmr_offset != -1ULL))
557                 return -EINVAL;
558 
559         xhead.fmh_iflags = head.fmh_iflags;
560         xhead.fmh_count = head.fmh_count;
561         ext4_fsmap_to_internal(sb, &xhead.fmh_keys[0], &head.fmh_keys[0]);
562         ext4_fsmap_to_internal(sb, &xhead.fmh_keys[1], &head.fmh_keys[1]);
563 
564         trace_ext4_getfsmap_low_key(sb, &xhead.fmh_keys[0]);
565         trace_ext4_getfsmap_high_key(sb, &xhead.fmh_keys[1]);
566 
567         info.gi_sb = sb;
568         info.gi_data = arg;
569         error = ext4_getfsmap(sb, &xhead, ext4_getfsmap_format, &info);
570         if (error == EXT4_QUERY_RANGE_ABORT) {
571                 error = 0;
572                 aborted = true;
573         } else if (error)
574                 return error;
575 
576         /* If we didn't abort, set the "last" flag in the last fmx */
577         if (!aborted && info.gi_idx) {
578                 info.gi_last_flags |= FMR_OF_LAST;
579                 if (copy_to_user(&info.gi_data->fmh_recs[info.gi_idx - 1].fmr_flags,
580                                  &info.gi_last_flags,
581                                  sizeof(info.gi_last_flags)))
582                         return -EFAULT;
583         }
584 
585         /* copy back header */
586         head.fmh_entries = xhead.fmh_entries;
587         head.fmh_oflags = xhead.fmh_oflags;
588         if (copy_to_user(arg, &head, sizeof(struct fsmap_head)))
589                 return -EFAULT;
590 
591         return 0;
592 }
593 
594 static long ext4_ioctl_group_add(struct file *file,
595                                  struct ext4_new_group_data *input)
596 {
597         struct super_block *sb = file_inode(file)->i_sb;
598         int err, err2=0;
599 
600         err = ext4_resize_begin(sb);
601         if (err)
602                 return err;
603 
604         if (ext4_has_feature_bigalloc(sb)) {
605                 ext4_msg(sb, KERN_ERR,
606                          "Online resizing not supported with bigalloc");
607                 err = -EOPNOTSUPP;
608                 goto group_add_out;
609         }
610 
611         err = mnt_want_write_file(file);
612         if (err)
613                 goto group_add_out;
614 
615         err = ext4_group_add(sb, input);
616         if (EXT4_SB(sb)->s_journal) {
617                 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
618                 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
619                 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
620         }
621         if (err == 0)
622                 err = err2;
623         mnt_drop_write_file(file);
624         if (!err && ext4_has_group_desc_csum(sb) &&
625             test_opt(sb, INIT_INODE_TABLE))
626                 err = ext4_register_li_request(sb, input->group);
627 group_add_out:
628         ext4_resize_end(sb);
629         return err;
630 }
631 
632 long ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
633 {
634         struct inode *inode = file_inode(filp);
635         struct super_block *sb = inode->i_sb;
636         struct ext4_inode_info *ei = EXT4_I(inode);
637         unsigned int flags;
638 
639         ext4_debug("cmd = %u, arg = %lu\n", cmd, arg);
640 
641         switch (cmd) {
642         case FS_IOC_GETFSMAP:
643                 return ext4_ioc_getfsmap(sb, (void __user *)arg);
644         case EXT4_IOC_GETFLAGS:
645                 flags = ei->i_flags & EXT4_FL_USER_VISIBLE;
646                 return put_user(flags, (int __user *) arg);
647         case EXT4_IOC_SETFLAGS: {
648                 int err;
649 
650                 if (!inode_owner_or_capable(inode))
651                         return -EACCES;
652 
653                 if (get_user(flags, (int __user *) arg))
654                         return -EFAULT;
655 
656                 if (flags & ~EXT4_FL_USER_VISIBLE)
657                         return -EOPNOTSUPP;
658                 /*
659                  * chattr(1) grabs flags via GETFLAGS, modifies the result and
660                  * passes that to SETFLAGS. So we cannot easily make SETFLAGS
661                  * more restrictive than just silently masking off visible but
662                  * not settable flags as we always did.
663                  */
664                 flags &= EXT4_FL_USER_MODIFIABLE;
665                 if (ext4_mask_flags(inode->i_mode, flags) != flags)
666                         return -EOPNOTSUPP;
667 
668                 err = mnt_want_write_file(filp);
669                 if (err)
670                         return err;
671 
672                 inode_lock(inode);
673                 err = ext4_ioctl_setflags(inode, flags);
674                 inode_unlock(inode);
675                 mnt_drop_write_file(filp);
676                 return err;
677         }
678         case EXT4_IOC_GETVERSION:
679         case EXT4_IOC_GETVERSION_OLD:
680                 return put_user(inode->i_generation, (int __user *) arg);
681         case EXT4_IOC_SETVERSION:
682         case EXT4_IOC_SETVERSION_OLD: {
683                 handle_t *handle;
684                 struct ext4_iloc iloc;
685                 __u32 generation;
686                 int err;
687 
688                 if (!inode_owner_or_capable(inode))
689                         return -EPERM;
690 
691                 if (ext4_has_metadata_csum(inode->i_sb)) {
692                         ext4_warning(sb, "Setting inode version is not "
693                                      "supported with metadata_csum enabled.");
694                         return -ENOTTY;
695                 }
696 
697                 err = mnt_want_write_file(filp);
698                 if (err)
699                         return err;
700                 if (get_user(generation, (int __user *) arg)) {
701                         err = -EFAULT;
702                         goto setversion_out;
703                 }
704 
705                 inode_lock(inode);
706                 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
707                 if (IS_ERR(handle)) {
708                         err = PTR_ERR(handle);
709                         goto unlock_out;
710                 }
711                 err = ext4_reserve_inode_write(handle, inode, &iloc);
712                 if (err == 0) {
713                         inode->i_ctime = current_time(inode);
714                         inode->i_generation = generation;
715                         err = ext4_mark_iloc_dirty(handle, inode, &iloc);
716                 }
717                 ext4_journal_stop(handle);
718 
719 unlock_out:
720                 inode_unlock(inode);
721 setversion_out:
722                 mnt_drop_write_file(filp);
723                 return err;
724         }
725         case EXT4_IOC_GROUP_EXTEND: {
726                 ext4_fsblk_t n_blocks_count;
727                 int err, err2=0;
728 
729                 err = ext4_resize_begin(sb);
730                 if (err)
731                         return err;
732 
733                 if (get_user(n_blocks_count, (__u32 __user *)arg)) {
734                         err = -EFAULT;
735                         goto group_extend_out;
736                 }
737 
738                 if (ext4_has_feature_bigalloc(sb)) {
739                         ext4_msg(sb, KERN_ERR,
740                                  "Online resizing not supported with bigalloc");
741                         err = -EOPNOTSUPP;
742                         goto group_extend_out;
743                 }
744 
745                 err = mnt_want_write_file(filp);
746                 if (err)
747                         goto group_extend_out;
748 
749                 err = ext4_group_extend(sb, EXT4_SB(sb)->s_es, n_blocks_count);
750                 if (EXT4_SB(sb)->s_journal) {
751                         jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
752                         err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
753                         jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
754                 }
755                 if (err == 0)
756                         err = err2;
757                 mnt_drop_write_file(filp);
758 group_extend_out:
759                 ext4_resize_end(sb);
760                 return err;
761         }
762 
763         case EXT4_IOC_MOVE_EXT: {
764                 struct move_extent me;
765                 struct fd donor;
766                 int err;
767 
768                 if (!(filp->f_mode & FMODE_READ) ||
769                     !(filp->f_mode & FMODE_WRITE))
770                         return -EBADF;
771 
772                 if (copy_from_user(&me,
773                         (struct move_extent __user *)arg, sizeof(me)))
774                         return -EFAULT;
775                 me.moved_len = 0;
776 
777                 donor = fdget(me.donor_fd);
778                 if (!donor.file)
779                         return -EBADF;
780 
781                 if (!(donor.file->f_mode & FMODE_WRITE)) {
782                         err = -EBADF;
783                         goto mext_out;
784                 }
785 
786                 if (ext4_has_feature_bigalloc(sb)) {
787                         ext4_msg(sb, KERN_ERR,
788                                  "Online defrag not supported with bigalloc");
789                         err = -EOPNOTSUPP;
790                         goto mext_out;
791                 } else if (IS_DAX(inode)) {
792                         ext4_msg(sb, KERN_ERR,
793                                  "Online defrag not supported with DAX");
794                         err = -EOPNOTSUPP;
795                         goto mext_out;
796                 }
797 
798                 err = mnt_want_write_file(filp);
799                 if (err)
800                         goto mext_out;
801 
802                 err = ext4_move_extents(filp, donor.file, me.orig_start,
803                                         me.donor_start, me.len, &me.moved_len);
804                 mnt_drop_write_file(filp);
805 
806                 if (copy_to_user((struct move_extent __user *)arg,
807                                  &me, sizeof(me)))
808                         err = -EFAULT;
809 mext_out:
810                 fdput(donor);
811                 return err;
812         }
813 
814         case EXT4_IOC_GROUP_ADD: {
815                 struct ext4_new_group_data input;
816 
817                 if (copy_from_user(&input, (struct ext4_new_group_input __user *)arg,
818                                 sizeof(input)))
819                         return -EFAULT;
820 
821                 return ext4_ioctl_group_add(filp, &input);
822         }
823 
824         case EXT4_IOC_MIGRATE:
825         {
826                 int err;
827                 if (!inode_owner_or_capable(inode))
828                         return -EACCES;
829 
830                 err = mnt_want_write_file(filp);
831                 if (err)
832                         return err;
833                 /*
834                  * inode_mutex prevent write and truncate on the file.
835                  * Read still goes through. We take i_data_sem in
836                  * ext4_ext_swap_inode_data before we switch the
837                  * inode format to prevent read.
838                  */
839                 inode_lock((inode));
840                 err = ext4_ext_migrate(inode);
841                 inode_unlock((inode));
842                 mnt_drop_write_file(filp);
843                 return err;
844         }
845 
846         case EXT4_IOC_ALLOC_DA_BLKS:
847         {
848                 int err;
849                 if (!inode_owner_or_capable(inode))
850                         return -EACCES;
851 
852                 err = mnt_want_write_file(filp);
853                 if (err)
854                         return err;
855                 err = ext4_alloc_da_blocks(inode);
856                 mnt_drop_write_file(filp);
857                 return err;
858         }
859 
860         case EXT4_IOC_SWAP_BOOT:
861         {
862                 int err;
863                 if (!(filp->f_mode & FMODE_WRITE))
864                         return -EBADF;
865                 err = mnt_want_write_file(filp);
866                 if (err)
867                         return err;
868                 err = swap_inode_boot_loader(sb, inode);
869                 mnt_drop_write_file(filp);
870                 return err;
871         }
872 
873         case EXT4_IOC_RESIZE_FS: {
874                 ext4_fsblk_t n_blocks_count;
875                 int err = 0, err2 = 0;
876                 ext4_group_t o_group = EXT4_SB(sb)->s_groups_count;
877 
878                 if (copy_from_user(&n_blocks_count, (__u64 __user *)arg,
879                                    sizeof(__u64))) {
880                         return -EFAULT;
881                 }
882 
883                 err = ext4_resize_begin(sb);
884                 if (err)
885                         return err;
886 
887                 err = mnt_want_write_file(filp);
888                 if (err)
889                         goto resizefs_out;
890 
891                 err = ext4_resize_fs(sb, n_blocks_count);
892                 if (EXT4_SB(sb)->s_journal) {
893                         jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
894                         err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
895                         jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
896                 }
897                 if (err == 0)
898                         err = err2;
899                 mnt_drop_write_file(filp);
900                 if (!err && (o_group > EXT4_SB(sb)->s_groups_count) &&
901                     ext4_has_group_desc_csum(sb) &&
902                     test_opt(sb, INIT_INODE_TABLE))
903                         err = ext4_register_li_request(sb, o_group);
904 
905 resizefs_out:
906                 ext4_resize_end(sb);
907                 return err;
908         }
909 
910         case FITRIM:
911         {
912                 struct request_queue *q = bdev_get_queue(sb->s_bdev);
913                 struct fstrim_range range;
914                 int ret = 0;
915 
916                 if (!capable(CAP_SYS_ADMIN))
917                         return -EPERM;
918 
919                 if (!blk_queue_discard(q))
920                         return -EOPNOTSUPP;
921 
922                 if (copy_from_user(&range, (struct fstrim_range __user *)arg,
923                     sizeof(range)))
924                         return -EFAULT;
925 
926                 range.minlen = max((unsigned int)range.minlen,
927                                    q->limits.discard_granularity);
928                 ret = ext4_trim_fs(sb, &range);
929                 if (ret < 0)
930                         return ret;
931 
932                 if (copy_to_user((struct fstrim_range __user *)arg, &range,
933                     sizeof(range)))
934                         return -EFAULT;
935 
936                 return 0;
937         }
938         case EXT4_IOC_PRECACHE_EXTENTS:
939                 return ext4_ext_precache(inode);
940 
941         case EXT4_IOC_SET_ENCRYPTION_POLICY:
942                 if (!ext4_has_feature_encrypt(sb))
943                         return -EOPNOTSUPP;
944                 return fscrypt_ioctl_set_policy(filp, (const void __user *)arg);
945 
946         case EXT4_IOC_GET_ENCRYPTION_PWSALT: {
947 #ifdef CONFIG_EXT4_FS_ENCRYPTION
948                 int err, err2;
949                 struct ext4_sb_info *sbi = EXT4_SB(sb);
950                 handle_t *handle;
951 
952                 if (!ext4_has_feature_encrypt(sb))
953                         return -EOPNOTSUPP;
954                 if (uuid_is_zero(sbi->s_es->s_encrypt_pw_salt)) {
955                         err = mnt_want_write_file(filp);
956                         if (err)
957                                 return err;
958                         handle = ext4_journal_start_sb(sb, EXT4_HT_MISC, 1);
959                         if (IS_ERR(handle)) {
960                                 err = PTR_ERR(handle);
961                                 goto pwsalt_err_exit;
962                         }
963                         err = ext4_journal_get_write_access(handle, sbi->s_sbh);
964                         if (err)
965                                 goto pwsalt_err_journal;
966                         generate_random_uuid(sbi->s_es->s_encrypt_pw_salt);
967                         err = ext4_handle_dirty_metadata(handle, NULL,
968                                                          sbi->s_sbh);
969                 pwsalt_err_journal:
970                         err2 = ext4_journal_stop(handle);
971                         if (err2 && !err)
972                                 err = err2;
973                 pwsalt_err_exit:
974                         mnt_drop_write_file(filp);
975                         if (err)
976                                 return err;
977                 }
978                 if (copy_to_user((void __user *) arg,
979                                  sbi->s_es->s_encrypt_pw_salt, 16))
980                         return -EFAULT;
981                 return 0;
982 #else
983                 return -EOPNOTSUPP;
984 #endif
985         }
986         case EXT4_IOC_GET_ENCRYPTION_POLICY:
987                 return fscrypt_ioctl_get_policy(filp, (void __user *)arg);
988 
989         case EXT4_IOC_FSGETXATTR:
990         {
991                 struct fsxattr fa;
992 
993                 memset(&fa, 0, sizeof(struct fsxattr));
994                 fa.fsx_xflags = ext4_iflags_to_xflags(ei->i_flags & EXT4_FL_USER_VISIBLE);
995 
996                 if (ext4_has_feature_project(inode->i_sb)) {
997                         fa.fsx_projid = (__u32)from_kprojid(&init_user_ns,
998                                 EXT4_I(inode)->i_projid);
999                 }
1000 
1001                 if (copy_to_user((struct fsxattr __user *)arg,
1002                                  &fa, sizeof(fa)))
1003                         return -EFAULT;
1004                 return 0;
1005         }
1006         case EXT4_IOC_FSSETXATTR:
1007         {
1008                 struct fsxattr fa;
1009                 int err;
1010 
1011                 if (copy_from_user(&fa, (struct fsxattr __user *)arg,
1012                                    sizeof(fa)))
1013                         return -EFAULT;
1014 
1015                 /* Make sure caller has proper permission */
1016                 if (!inode_owner_or_capable(inode))
1017                         return -EACCES;
1018 
1019                 if (fa.fsx_xflags & ~EXT4_SUPPORTED_FS_XFLAGS)
1020                         return -EOPNOTSUPP;
1021 
1022                 flags = ext4_xflags_to_iflags(fa.fsx_xflags);
1023                 if (ext4_mask_flags(inode->i_mode, flags) != flags)
1024                         return -EOPNOTSUPP;
1025 
1026                 err = mnt_want_write_file(filp);
1027                 if (err)
1028                         return err;
1029 
1030                 inode_lock(inode);
1031                 flags = (ei->i_flags & ~EXT4_FL_XFLAG_VISIBLE) |
1032                          (flags & EXT4_FL_XFLAG_VISIBLE);
1033                 err = ext4_ioctl_setflags(inode, flags);
1034                 inode_unlock(inode);
1035                 mnt_drop_write_file(filp);
1036                 if (err)
1037                         return err;
1038 
1039                 err = ext4_ioctl_setproject(filp, fa.fsx_projid);
1040                 if (err)
1041                         return err;
1042 
1043                 return 0;
1044         }
1045         case EXT4_IOC_SHUTDOWN:
1046                 return ext4_shutdown(sb, arg);
1047         default:
1048                 return -ENOTTY;
1049         }
1050 }
1051 
1052 #ifdef CONFIG_COMPAT
1053 long ext4_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1054 {
1055         /* These are just misnamed, they actually get/put from/to user an int */
1056         switch (cmd) {
1057         case EXT4_IOC32_GETFLAGS:
1058                 cmd = EXT4_IOC_GETFLAGS;
1059                 break;
1060         case EXT4_IOC32_SETFLAGS:
1061                 cmd = EXT4_IOC_SETFLAGS;
1062                 break;
1063         case EXT4_IOC32_GETVERSION:
1064                 cmd = EXT4_IOC_GETVERSION;
1065                 break;
1066         case EXT4_IOC32_SETVERSION:
1067                 cmd = EXT4_IOC_SETVERSION;
1068                 break;
1069         case EXT4_IOC32_GROUP_EXTEND:
1070                 cmd = EXT4_IOC_GROUP_EXTEND;
1071                 break;
1072         case EXT4_IOC32_GETVERSION_OLD:
1073                 cmd = EXT4_IOC_GETVERSION_OLD;
1074                 break;
1075         case EXT4_IOC32_SETVERSION_OLD:
1076                 cmd = EXT4_IOC_SETVERSION_OLD;
1077                 break;
1078         case EXT4_IOC32_GETRSVSZ:
1079                 cmd = EXT4_IOC_GETRSVSZ;
1080                 break;
1081         case EXT4_IOC32_SETRSVSZ:
1082                 cmd = EXT4_IOC_SETRSVSZ;
1083                 break;
1084         case EXT4_IOC32_GROUP_ADD: {
1085                 struct compat_ext4_new_group_input __user *uinput;
1086                 struct ext4_new_group_data input;
1087                 int err;
1088 
1089                 uinput = compat_ptr(arg);
1090                 err = get_user(input.group, &uinput->group);
1091                 err |= get_user(input.block_bitmap, &uinput->block_bitmap);
1092                 err |= get_user(input.inode_bitmap, &uinput->inode_bitmap);
1093                 err |= get_user(input.inode_table, &uinput->inode_table);
1094                 err |= get_user(input.blocks_count, &uinput->blocks_count);
1095                 err |= get_user(input.reserved_blocks,
1096                                 &uinput->reserved_blocks);
1097                 if (err)
1098                         return -EFAULT;
1099                 return ext4_ioctl_group_add(file, &input);
1100         }
1101         case EXT4_IOC_MOVE_EXT:
1102         case EXT4_IOC_RESIZE_FS:
1103         case EXT4_IOC_PRECACHE_EXTENTS:
1104         case EXT4_IOC_SET_ENCRYPTION_POLICY:
1105         case EXT4_IOC_GET_ENCRYPTION_PWSALT:
1106         case EXT4_IOC_GET_ENCRYPTION_POLICY:
1107         case EXT4_IOC_SHUTDOWN:
1108         case FS_IOC_GETFSMAP:
1109                 break;
1110         default:
1111                 return -ENOIOCTLCMD;
1112         }
1113         return ext4_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
1114 }
1115 #endif
1116 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp