ext2: remove nobh support
[linux-2.6-microblaze.git] / fs / remap_range.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/slab.h>
3 #include <linux/stat.h>
4 #include <linux/sched/xacct.h>
5 #include <linux/fcntl.h>
6 #include <linux/file.h>
7 #include <linux/uio.h>
8 #include <linux/fsnotify.h>
9 #include <linux/security.h>
10 #include <linux/export.h>
11 #include <linux/syscalls.h>
12 #include <linux/pagemap.h>
13 #include <linux/splice.h>
14 #include <linux/compat.h>
15 #include <linux/mount.h>
16 #include <linux/fs.h>
17 #include "internal.h"
18
19 #include <linux/uaccess.h>
20 #include <asm/unistd.h>
21
22 /*
23  * Performs necessary checks before doing a clone.
24  *
25  * Can adjust amount of bytes to clone via @req_count argument.
26  * Returns appropriate error code that caller should return or
27  * zero in case the clone should be allowed.
28  */
29 static int generic_remap_checks(struct file *file_in, loff_t pos_in,
30                                 struct file *file_out, loff_t pos_out,
31                                 loff_t *req_count, unsigned int remap_flags)
32 {
33         struct inode *inode_in = file_in->f_mapping->host;
34         struct inode *inode_out = file_out->f_mapping->host;
35         uint64_t count = *req_count;
36         uint64_t bcount;
37         loff_t size_in, size_out;
38         loff_t bs = inode_out->i_sb->s_blocksize;
39         int ret;
40
41         /* The start of both ranges must be aligned to an fs block. */
42         if (!IS_ALIGNED(pos_in, bs) || !IS_ALIGNED(pos_out, bs))
43                 return -EINVAL;
44
45         /* Ensure offsets don't wrap. */
46         if (pos_in + count < pos_in || pos_out + count < pos_out)
47                 return -EINVAL;
48
49         size_in = i_size_read(inode_in);
50         size_out = i_size_read(inode_out);
51
52         /* Dedupe requires both ranges to be within EOF. */
53         if ((remap_flags & REMAP_FILE_DEDUP) &&
54             (pos_in >= size_in || pos_in + count > size_in ||
55              pos_out >= size_out || pos_out + count > size_out))
56                 return -EINVAL;
57
58         /* Ensure the infile range is within the infile. */
59         if (pos_in >= size_in)
60                 return -EINVAL;
61         count = min(count, size_in - (uint64_t)pos_in);
62
63         ret = generic_write_check_limits(file_out, pos_out, &count);
64         if (ret)
65                 return ret;
66
67         /*
68          * If the user wanted us to link to the infile's EOF, round up to the
69          * next block boundary for this check.
70          *
71          * Otherwise, make sure the count is also block-aligned, having
72          * already confirmed the starting offsets' block alignment.
73          */
74         if (pos_in + count == size_in) {
75                 bcount = ALIGN(size_in, bs) - pos_in;
76         } else {
77                 if (!IS_ALIGNED(count, bs))
78                         count = ALIGN_DOWN(count, bs);
79                 bcount = count;
80         }
81
82         /* Don't allow overlapped cloning within the same file. */
83         if (inode_in == inode_out &&
84             pos_out + bcount > pos_in &&
85             pos_out < pos_in + bcount)
86                 return -EINVAL;
87
88         /*
89          * We shortened the request but the caller can't deal with that, so
90          * bounce the request back to userspace.
91          */
92         if (*req_count != count && !(remap_flags & REMAP_FILE_CAN_SHORTEN))
93                 return -EINVAL;
94
95         *req_count = count;
96         return 0;
97 }
98
99 static int remap_verify_area(struct file *file, loff_t pos, loff_t len,
100                              bool write)
101 {
102         if (unlikely(pos < 0 || len < 0))
103                 return -EINVAL;
104
105         if (unlikely((loff_t) (pos + len) < 0))
106                 return -EINVAL;
107
108         return security_file_permission(file, write ? MAY_WRITE : MAY_READ);
109 }
110
111 /*
112  * Ensure that we don't remap a partial EOF block in the middle of something
113  * else.  Assume that the offsets have already been checked for block
114  * alignment.
115  *
116  * For clone we only link a partial EOF block above or at the destination file's
117  * EOF.  For deduplication we accept a partial EOF block only if it ends at the
118  * destination file's EOF (can not link it into the middle of a file).
119  *
120  * Shorten the request if possible.
121  */
122 static int generic_remap_check_len(struct inode *inode_in,
123                                    struct inode *inode_out,
124                                    loff_t pos_out,
125                                    loff_t *len,
126                                    unsigned int remap_flags)
127 {
128         u64 blkmask = i_blocksize(inode_in) - 1;
129         loff_t new_len = *len;
130
131         if ((*len & blkmask) == 0)
132                 return 0;
133
134         if (pos_out + *len < i_size_read(inode_out))
135                 new_len &= ~blkmask;
136
137         if (new_len == *len)
138                 return 0;
139
140         if (remap_flags & REMAP_FILE_CAN_SHORTEN) {
141                 *len = new_len;
142                 return 0;
143         }
144
145         return (remap_flags & REMAP_FILE_DEDUP) ? -EBADE : -EINVAL;
146 }
147
148 /* Read a page's worth of file data into the page cache. */
149 static struct folio *vfs_dedupe_get_folio(struct file *file, loff_t pos)
150 {
151         return read_mapping_folio(file->f_mapping, pos >> PAGE_SHIFT, file);
152 }
153
154 /*
155  * Lock two folios, ensuring that we lock in offset order if the folios
156  * are from the same file.
157  */
158 static void vfs_lock_two_folios(struct folio *folio1, struct folio *folio2)
159 {
160         /* Always lock in order of increasing index. */
161         if (folio1->index > folio2->index)
162                 swap(folio1, folio2);
163
164         folio_lock(folio1);
165         if (folio1 != folio2)
166                 folio_lock(folio2);
167 }
168
169 /* Unlock two folios, being careful not to unlock the same folio twice. */
170 static void vfs_unlock_two_folios(struct folio *folio1, struct folio *folio2)
171 {
172         folio_unlock(folio1);
173         if (folio1 != folio2)
174                 folio_unlock(folio2);
175 }
176
177 /*
178  * Compare extents of two files to see if they are the same.
179  * Caller must have locked both inodes to prevent write races.
180  */
181 static int vfs_dedupe_file_range_compare(struct file *src, loff_t srcoff,
182                                          struct file *dest, loff_t dstoff,
183                                          loff_t len, bool *is_same)
184 {
185         bool same = true;
186         int error = -EINVAL;
187
188         while (len) {
189                 struct folio *src_folio, *dst_folio;
190                 void *src_addr, *dst_addr;
191                 loff_t cmp_len = min(PAGE_SIZE - offset_in_page(srcoff),
192                                      PAGE_SIZE - offset_in_page(dstoff));
193
194                 cmp_len = min(cmp_len, len);
195                 if (cmp_len <= 0)
196                         goto out_error;
197
198                 src_folio = vfs_dedupe_get_folio(src, srcoff);
199                 if (IS_ERR(src_folio)) {
200                         error = PTR_ERR(src_folio);
201                         goto out_error;
202                 }
203                 dst_folio = vfs_dedupe_get_folio(dest, dstoff);
204                 if (IS_ERR(dst_folio)) {
205                         error = PTR_ERR(dst_folio);
206                         folio_put(src_folio);
207                         goto out_error;
208                 }
209
210                 vfs_lock_two_folios(src_folio, dst_folio);
211
212                 /*
213                  * Now that we've locked both folios, make sure they're still
214                  * mapped to the file data we're interested in.  If not,
215                  * someone is invalidating pages on us and we lose.
216                  */
217                 if (!folio_test_uptodate(src_folio) || !folio_test_uptodate(dst_folio) ||
218                     src_folio->mapping != src->f_mapping ||
219                     dst_folio->mapping != dest->f_mapping) {
220                         same = false;
221                         goto unlock;
222                 }
223
224                 src_addr = kmap_local_folio(src_folio,
225                                         offset_in_folio(src_folio, srcoff));
226                 dst_addr = kmap_local_folio(dst_folio,
227                                         offset_in_folio(dst_folio, dstoff));
228
229                 flush_dcache_folio(src_folio);
230                 flush_dcache_folio(dst_folio);
231
232                 if (memcmp(src_addr, dst_addr, cmp_len))
233                         same = false;
234
235                 kunmap_local(dst_addr);
236                 kunmap_local(src_addr);
237 unlock:
238                 vfs_unlock_two_folios(src_folio, dst_folio);
239                 folio_put(dst_folio);
240                 folio_put(src_folio);
241
242                 if (!same)
243                         break;
244
245                 srcoff += cmp_len;
246                 dstoff += cmp_len;
247                 len -= cmp_len;
248         }
249
250         *is_same = same;
251         return 0;
252
253 out_error:
254         return error;
255 }
256
257 /*
258  * Check that the two inodes are eligible for cloning, the ranges make
259  * sense, and then flush all dirty data.  Caller must ensure that the
260  * inodes have been locked against any other modifications.
261  *
262  * If there's an error, then the usual negative error code is returned.
263  * Otherwise returns 0 with *len set to the request length.
264  */
265 int generic_remap_file_range_prep(struct file *file_in, loff_t pos_in,
266                                   struct file *file_out, loff_t pos_out,
267                                   loff_t *len, unsigned int remap_flags)
268 {
269         struct inode *inode_in = file_inode(file_in);
270         struct inode *inode_out = file_inode(file_out);
271         bool same_inode = (inode_in == inode_out);
272         int ret;
273
274         /* Don't touch certain kinds of inodes */
275         if (IS_IMMUTABLE(inode_out))
276                 return -EPERM;
277
278         if (IS_SWAPFILE(inode_in) || IS_SWAPFILE(inode_out))
279                 return -ETXTBSY;
280
281         /* Don't reflink dirs, pipes, sockets... */
282         if (S_ISDIR(inode_in->i_mode) || S_ISDIR(inode_out->i_mode))
283                 return -EISDIR;
284         if (!S_ISREG(inode_in->i_mode) || !S_ISREG(inode_out->i_mode))
285                 return -EINVAL;
286
287         /* Zero length dedupe exits immediately; reflink goes to EOF. */
288         if (*len == 0) {
289                 loff_t isize = i_size_read(inode_in);
290
291                 if ((remap_flags & REMAP_FILE_DEDUP) || pos_in == isize)
292                         return 0;
293                 if (pos_in > isize)
294                         return -EINVAL;
295                 *len = isize - pos_in;
296                 if (*len == 0)
297                         return 0;
298         }
299
300         /* Check that we don't violate system file offset limits. */
301         ret = generic_remap_checks(file_in, pos_in, file_out, pos_out, len,
302                         remap_flags);
303         if (ret)
304                 return ret;
305
306         /* Wait for the completion of any pending IOs on both files */
307         inode_dio_wait(inode_in);
308         if (!same_inode)
309                 inode_dio_wait(inode_out);
310
311         ret = filemap_write_and_wait_range(inode_in->i_mapping,
312                         pos_in, pos_in + *len - 1);
313         if (ret)
314                 return ret;
315
316         ret = filemap_write_and_wait_range(inode_out->i_mapping,
317                         pos_out, pos_out + *len - 1);
318         if (ret)
319                 return ret;
320
321         /*
322          * Check that the extents are the same.
323          */
324         if (remap_flags & REMAP_FILE_DEDUP) {
325                 bool            is_same = false;
326
327                 ret = vfs_dedupe_file_range_compare(file_in, pos_in,
328                                 file_out, pos_out, *len, &is_same);
329                 if (ret)
330                         return ret;
331                 if (!is_same)
332                         return -EBADE;
333         }
334
335         ret = generic_remap_check_len(inode_in, inode_out, pos_out, len,
336                         remap_flags);
337         if (ret)
338                 return ret;
339
340         /* If can't alter the file contents, we're done. */
341         if (!(remap_flags & REMAP_FILE_DEDUP))
342                 ret = file_modified(file_out);
343
344         return ret;
345 }
346 EXPORT_SYMBOL(generic_remap_file_range_prep);
347
348 loff_t do_clone_file_range(struct file *file_in, loff_t pos_in,
349                            struct file *file_out, loff_t pos_out,
350                            loff_t len, unsigned int remap_flags)
351 {
352         loff_t ret;
353
354         WARN_ON_ONCE(remap_flags & REMAP_FILE_DEDUP);
355
356         if (file_inode(file_in)->i_sb != file_inode(file_out)->i_sb)
357                 return -EXDEV;
358
359         ret = generic_file_rw_checks(file_in, file_out);
360         if (ret < 0)
361                 return ret;
362
363         if (!file_in->f_op->remap_file_range)
364                 return -EOPNOTSUPP;
365
366         ret = remap_verify_area(file_in, pos_in, len, false);
367         if (ret)
368                 return ret;
369
370         ret = remap_verify_area(file_out, pos_out, len, true);
371         if (ret)
372                 return ret;
373
374         ret = file_in->f_op->remap_file_range(file_in, pos_in,
375                         file_out, pos_out, len, remap_flags);
376         if (ret < 0)
377                 return ret;
378
379         fsnotify_access(file_in);
380         fsnotify_modify(file_out);
381         return ret;
382 }
383 EXPORT_SYMBOL(do_clone_file_range);
384
385 loff_t vfs_clone_file_range(struct file *file_in, loff_t pos_in,
386                             struct file *file_out, loff_t pos_out,
387                             loff_t len, unsigned int remap_flags)
388 {
389         loff_t ret;
390
391         file_start_write(file_out);
392         ret = do_clone_file_range(file_in, pos_in, file_out, pos_out, len,
393                                   remap_flags);
394         file_end_write(file_out);
395
396         return ret;
397 }
398 EXPORT_SYMBOL(vfs_clone_file_range);
399
400 /* Check whether we are allowed to dedupe the destination file */
401 static bool allow_file_dedupe(struct file *file)
402 {
403         struct user_namespace *mnt_userns = file_mnt_user_ns(file);
404         struct inode *inode = file_inode(file);
405
406         if (capable(CAP_SYS_ADMIN))
407                 return true;
408         if (file->f_mode & FMODE_WRITE)
409                 return true;
410         if (uid_eq(current_fsuid(), i_uid_into_mnt(mnt_userns, inode)))
411                 return true;
412         if (!inode_permission(mnt_userns, inode, MAY_WRITE))
413                 return true;
414         return false;
415 }
416
417 loff_t vfs_dedupe_file_range_one(struct file *src_file, loff_t src_pos,
418                                  struct file *dst_file, loff_t dst_pos,
419                                  loff_t len, unsigned int remap_flags)
420 {
421         loff_t ret;
422
423         WARN_ON_ONCE(remap_flags & ~(REMAP_FILE_DEDUP |
424                                      REMAP_FILE_CAN_SHORTEN));
425
426         ret = mnt_want_write_file(dst_file);
427         if (ret)
428                 return ret;
429
430         /*
431          * This is redundant if called from vfs_dedupe_file_range(), but other
432          * callers need it and it's not performance sesitive...
433          */
434         ret = remap_verify_area(src_file, src_pos, len, false);
435         if (ret)
436                 goto out_drop_write;
437
438         ret = remap_verify_area(dst_file, dst_pos, len, true);
439         if (ret)
440                 goto out_drop_write;
441
442         ret = -EPERM;
443         if (!allow_file_dedupe(dst_file))
444                 goto out_drop_write;
445
446         ret = -EXDEV;
447         if (file_inode(src_file)->i_sb != file_inode(dst_file)->i_sb)
448                 goto out_drop_write;
449
450         ret = -EISDIR;
451         if (S_ISDIR(file_inode(dst_file)->i_mode))
452                 goto out_drop_write;
453
454         ret = -EINVAL;
455         if (!dst_file->f_op->remap_file_range)
456                 goto out_drop_write;
457
458         if (len == 0) {
459                 ret = 0;
460                 goto out_drop_write;
461         }
462
463         ret = dst_file->f_op->remap_file_range(src_file, src_pos, dst_file,
464                         dst_pos, len, remap_flags | REMAP_FILE_DEDUP);
465 out_drop_write:
466         mnt_drop_write_file(dst_file);
467
468         return ret;
469 }
470 EXPORT_SYMBOL(vfs_dedupe_file_range_one);
471
472 int vfs_dedupe_file_range(struct file *file, struct file_dedupe_range *same)
473 {
474         struct file_dedupe_range_info *info;
475         struct inode *src = file_inode(file);
476         u64 off;
477         u64 len;
478         int i;
479         int ret;
480         u16 count = same->dest_count;
481         loff_t deduped;
482
483         if (!(file->f_mode & FMODE_READ))
484                 return -EINVAL;
485
486         if (same->reserved1 || same->reserved2)
487                 return -EINVAL;
488
489         off = same->src_offset;
490         len = same->src_length;
491
492         if (S_ISDIR(src->i_mode))
493                 return -EISDIR;
494
495         if (!S_ISREG(src->i_mode))
496                 return -EINVAL;
497
498         if (!file->f_op->remap_file_range)
499                 return -EOPNOTSUPP;
500
501         ret = remap_verify_area(file, off, len, false);
502         if (ret < 0)
503                 return ret;
504         ret = 0;
505
506         if (off + len > i_size_read(src))
507                 return -EINVAL;
508
509         /* Arbitrary 1G limit on a single dedupe request, can be raised. */
510         len = min_t(u64, len, 1 << 30);
511
512         /* pre-format output fields to sane values */
513         for (i = 0; i < count; i++) {
514                 same->info[i].bytes_deduped = 0ULL;
515                 same->info[i].status = FILE_DEDUPE_RANGE_SAME;
516         }
517
518         for (i = 0, info = same->info; i < count; i++, info++) {
519                 struct fd dst_fd = fdget(info->dest_fd);
520                 struct file *dst_file = dst_fd.file;
521
522                 if (!dst_file) {
523                         info->status = -EBADF;
524                         goto next_loop;
525                 }
526
527                 if (info->reserved) {
528                         info->status = -EINVAL;
529                         goto next_fdput;
530                 }
531
532                 deduped = vfs_dedupe_file_range_one(file, off, dst_file,
533                                                     info->dest_offset, len,
534                                                     REMAP_FILE_CAN_SHORTEN);
535                 if (deduped == -EBADE)
536                         info->status = FILE_DEDUPE_RANGE_DIFFERS;
537                 else if (deduped < 0)
538                         info->status = deduped;
539                 else
540                         info->bytes_deduped = len;
541
542 next_fdput:
543                 fdput(dst_fd);
544 next_loop:
545                 if (fatal_signal_pending(current))
546                         break;
547         }
548         return ret;
549 }
550 EXPORT_SYMBOL(vfs_dedupe_file_range);