Commit | Line | Data |
---|---|---|
4342306f KK |
1 | // SPDX-License-Identifier: GPL-2.0 |
2 | /* | |
3 | * | |
4 | * Copyright (C) 2019-2021 Paragon Software GmbH, All rights reserved. | |
5 | * | |
e8b8e97f KA |
6 | * Regular file handling primitives for NTFS-based filesystems. |
7 | * | |
4342306f | 8 | */ |
e8b8e97f | 9 | |
4342306f | 10 | #include <linux/backing-dev.h> |
ccdf7741 | 11 | #include <linux/blkdev.h> |
4342306f KK |
12 | #include <linux/buffer_head.h> |
13 | #include <linux/compat.h> | |
14 | #include <linux/falloc.h> | |
15 | #include <linux/fiemap.h> | |
4342306f KK |
16 | |
17 | #include "debug.h" | |
18 | #include "ntfs.h" | |
19 | #include "ntfs_fs.h" | |
20 | ||
21 | static int ntfs_ioctl_fitrim(struct ntfs_sb_info *sbi, unsigned long arg) | |
22 | { | |
23 | struct fstrim_range __user *user_range; | |
24 | struct fstrim_range range; | |
4342306f KK |
25 | int err; |
26 | ||
27 | if (!capable(CAP_SYS_ADMIN)) | |
28 | return -EPERM; | |
29 | ||
70200574 | 30 | if (!bdev_max_discard_sectors(sbi->sb->s_bdev)) |
4342306f KK |
31 | return -EOPNOTSUPP; |
32 | ||
33 | user_range = (struct fstrim_range __user *)arg; | |
34 | if (copy_from_user(&range, user_range, sizeof(range))) | |
35 | return -EFAULT; | |
36 | ||
7b47ef52 CH |
37 | range.minlen = max_t(u32, range.minlen, |
38 | bdev_discard_granularity(sbi->sb->s_bdev)); | |
4342306f KK |
39 | |
40 | err = ntfs_trim_fs(sbi, &range); | |
41 | if (err < 0) | |
42 | return err; | |
43 | ||
44 | if (copy_to_user(user_range, &range, sizeof(range))) | |
45 | return -EFAULT; | |
46 | ||
47 | return 0; | |
48 | } | |
49 | ||
50 | static long ntfs_ioctl(struct file *filp, u32 cmd, unsigned long arg) | |
51 | { | |
52 | struct inode *inode = file_inode(filp); | |
53 | struct ntfs_sb_info *sbi = inode->i_sb->s_fs_info; | |
4342306f KK |
54 | |
55 | switch (cmd) { | |
4342306f KK |
56 | case FITRIM: |
57 | return ntfs_ioctl_fitrim(sbi, arg); | |
58 | } | |
e8b8e97f | 59 | return -ENOTTY; /* Inappropriate ioctl for device. */ |
4342306f KK |
60 | } |
61 | ||
62 | #ifdef CONFIG_COMPAT | |
63 | static long ntfs_compat_ioctl(struct file *filp, u32 cmd, unsigned long arg) | |
64 | ||
65 | { | |
66 | return ntfs_ioctl(filp, cmd, (unsigned long)compat_ptr(arg)); | |
67 | } | |
68 | #endif | |
69 | ||
70 | /* | |
e8b8e97f | 71 | * ntfs_getattr - inode_operations::getattr |
4342306f KK |
72 | */ |
73 | int ntfs_getattr(struct user_namespace *mnt_userns, const struct path *path, | |
74 | struct kstat *stat, u32 request_mask, u32 flags) | |
75 | { | |
76 | struct inode *inode = d_inode(path->dentry); | |
77 | struct ntfs_inode *ni = ntfs_i(inode); | |
78 | ||
79 | if (is_compressed(ni)) | |
80 | stat->attributes |= STATX_ATTR_COMPRESSED; | |
81 | ||
82 | if (is_encrypted(ni)) | |
83 | stat->attributes |= STATX_ATTR_ENCRYPTED; | |
84 | ||
85 | stat->attributes_mask |= STATX_ATTR_COMPRESSED | STATX_ATTR_ENCRYPTED; | |
86 | ||
87 | generic_fillattr(mnt_userns, inode, stat); | |
88 | ||
89 | stat->result_mask |= STATX_BTIME; | |
90 | stat->btime = ni->i_crtime; | |
91 | stat->blksize = ni->mi.sbi->cluster_size; /* 512, 1K, ..., 2M */ | |
92 | ||
93 | return 0; | |
94 | } | |
95 | ||
96 | static int ntfs_extend_initialized_size(struct file *file, | |
97 | struct ntfs_inode *ni, | |
98 | const loff_t valid, | |
99 | const loff_t new_valid) | |
100 | { | |
101 | struct inode *inode = &ni->vfs_inode; | |
102 | struct address_space *mapping = inode->i_mapping; | |
103 | struct ntfs_sb_info *sbi = inode->i_sb->s_fs_info; | |
104 | loff_t pos = valid; | |
105 | int err; | |
106 | ||
107 | if (is_resident(ni)) { | |
108 | ni->i_valid = new_valid; | |
109 | return 0; | |
110 | } | |
111 | ||
112 | WARN_ON(is_compressed(ni)); | |
113 | WARN_ON(valid >= new_valid); | |
114 | ||
115 | for (;;) { | |
116 | u32 zerofrom, len; | |
117 | struct page *page; | |
4342306f KK |
118 | u8 bits; |
119 | CLST vcn, lcn, clen; | |
120 | ||
121 | if (is_sparsed(ni)) { | |
122 | bits = sbi->cluster_bits; | |
123 | vcn = pos >> bits; | |
124 | ||
125 | err = attr_data_get_block(ni, vcn, 0, &lcn, &clen, | |
126 | NULL); | |
127 | if (err) | |
128 | goto out; | |
129 | ||
130 | if (lcn == SPARSE_LCN) { | |
2f56a3f8 KK |
131 | pos = ((loff_t)clen + vcn) << bits; |
132 | ni->i_valid = pos; | |
133 | goto next; | |
4342306f KK |
134 | } |
135 | } | |
136 | ||
137 | zerofrom = pos & (PAGE_SIZE - 1); | |
138 | len = PAGE_SIZE - zerofrom; | |
139 | ||
140 | if (pos + len > new_valid) | |
141 | len = new_valid - pos; | |
142 | ||
652118b8 | 143 | err = ntfs_write_begin(file, mapping, pos, len, &page, NULL); |
4342306f KK |
144 | if (err) |
145 | goto out; | |
146 | ||
147 | zero_user_segment(page, zerofrom, PAGE_SIZE); | |
148 | ||
e8b8e97f | 149 | /* This function in any case puts page. */ |
652118b8 | 150 | err = ntfs_write_end(file, mapping, pos, len, len, page, NULL); |
4342306f KK |
151 | if (err < 0) |
152 | goto out; | |
153 | pos += len; | |
154 | ||
155 | next: | |
156 | if (pos >= new_valid) | |
157 | break; | |
158 | ||
159 | balance_dirty_pages_ratelimited(mapping); | |
160 | cond_resched(); | |
161 | } | |
162 | ||
163 | return 0; | |
164 | ||
165 | out: | |
166 | ni->i_valid = valid; | |
167 | ntfs_inode_warn(inode, "failed to extend initialized size to %llx.", | |
168 | new_valid); | |
169 | return err; | |
170 | } | |
171 | ||
172 | /* | |
e8b8e97f | 173 | * ntfs_zero_range - Helper function for punch_hole. |
d3624466 KK |
174 | * |
175 | * It zeroes a range [vbo, vbo_to). | |
4342306f KK |
176 | */ |
177 | static int ntfs_zero_range(struct inode *inode, u64 vbo, u64 vbo_to) | |
178 | { | |
179 | int err = 0; | |
180 | struct address_space *mapping = inode->i_mapping; | |
181 | u32 blocksize = 1 << inode->i_blkbits; | |
182 | pgoff_t idx = vbo >> PAGE_SHIFT; | |
183 | u32 z_start = vbo & (PAGE_SIZE - 1); | |
184 | pgoff_t idx_end = (vbo_to + PAGE_SIZE - 1) >> PAGE_SHIFT; | |
185 | loff_t page_off; | |
186 | struct buffer_head *head, *bh; | |
187 | u32 bh_next, bh_off, z_end; | |
188 | sector_t iblock; | |
189 | struct page *page; | |
190 | ||
191 | for (; idx < idx_end; idx += 1, z_start = 0) { | |
192 | page_off = (loff_t)idx << PAGE_SHIFT; | |
193 | z_end = (page_off + PAGE_SIZE) > vbo_to ? (vbo_to - page_off) | |
194 | : PAGE_SIZE; | |
195 | iblock = page_off >> inode->i_blkbits; | |
196 | ||
197 | page = find_or_create_page(mapping, idx, | |
198 | mapping_gfp_constraint(mapping, | |
199 | ~__GFP_FS)); | |
200 | if (!page) | |
201 | return -ENOMEM; | |
202 | ||
203 | if (!page_has_buffers(page)) | |
204 | create_empty_buffers(page, blocksize, 0); | |
205 | ||
206 | bh = head = page_buffers(page); | |
207 | bh_off = 0; | |
208 | do { | |
209 | bh_next = bh_off + blocksize; | |
210 | ||
211 | if (bh_next <= z_start || bh_off >= z_end) | |
212 | continue; | |
213 | ||
214 | if (!buffer_mapped(bh)) { | |
215 | ntfs_get_block(inode, iblock, bh, 0); | |
d3624466 | 216 | /* Unmapped? It's a hole - nothing to do. */ |
4342306f KK |
217 | if (!buffer_mapped(bh)) |
218 | continue; | |
219 | } | |
220 | ||
d3624466 | 221 | /* Ok, it's mapped. Make sure it's up-to-date. */ |
4342306f KK |
222 | if (PageUptodate(page)) |
223 | set_buffer_uptodate(bh); | |
224 | ||
225 | if (!buffer_uptodate(bh)) { | |
226 | lock_buffer(bh); | |
227 | bh->b_end_io = end_buffer_read_sync; | |
228 | get_bh(bh); | |
1420c4a5 | 229 | submit_bh(REQ_OP_READ, bh); |
4342306f KK |
230 | |
231 | wait_on_buffer(bh); | |
232 | if (!buffer_uptodate(bh)) { | |
233 | unlock_page(page); | |
234 | put_page(page); | |
235 | err = -EIO; | |
236 | goto out; | |
237 | } | |
238 | } | |
239 | ||
240 | mark_buffer_dirty(bh); | |
241 | ||
242 | } while (bh_off = bh_next, iblock += 1, | |
243 | head != (bh = bh->b_this_page)); | |
244 | ||
245 | zero_user_segment(page, z_start, z_end); | |
246 | ||
247 | unlock_page(page); | |
248 | put_page(page); | |
249 | cond_resched(); | |
250 | } | |
251 | out: | |
252 | mark_inode_dirty(inode); | |
253 | return err; | |
254 | } | |
255 | ||
256 | /* | |
d3624466 | 257 | * ntfs_sparse_cluster - Helper function to zero a new allocated clusters. |
4342306f | 258 | * |
4342306f KK |
259 | * NOTE: 512 <= cluster size <= 2M |
260 | */ | |
261 | void ntfs_sparse_cluster(struct inode *inode, struct page *page0, CLST vcn, | |
262 | CLST len) | |
263 | { | |
264 | struct address_space *mapping = inode->i_mapping; | |
265 | struct ntfs_sb_info *sbi = inode->i_sb->s_fs_info; | |
2f56a3f8 KK |
266 | u8 cluster_bits = sbi->cluster_bits; |
267 | u64 vbo = (u64)vcn << cluster_bits; | |
268 | u64 bytes = (u64)len << cluster_bits; | |
4342306f KK |
269 | u32 blocksize = 1 << inode->i_blkbits; |
270 | pgoff_t idx0 = page0 ? page0->index : -1; | |
271 | loff_t vbo_clst = vbo & sbi->cluster_mask_inv; | |
272 | loff_t end = ntfs_up_cluster(sbi, vbo + bytes); | |
273 | pgoff_t idx = vbo_clst >> PAGE_SHIFT; | |
274 | u32 from = vbo_clst & (PAGE_SIZE - 1); | |
275 | pgoff_t idx_end = (end + PAGE_SIZE - 1) >> PAGE_SHIFT; | |
276 | loff_t page_off; | |
277 | u32 to; | |
278 | bool partial; | |
279 | struct page *page; | |
280 | ||
281 | for (; idx < idx_end; idx += 1, from = 0) { | |
282 | page = idx == idx0 ? page0 : grab_cache_page(mapping, idx); | |
283 | ||
284 | if (!page) | |
285 | continue; | |
286 | ||
287 | page_off = (loff_t)idx << PAGE_SHIFT; | |
288 | to = (page_off + PAGE_SIZE) > end ? (end - page_off) | |
289 | : PAGE_SIZE; | |
290 | partial = false; | |
291 | ||
292 | if ((from || PAGE_SIZE != to) && | |
293 | likely(!page_has_buffers(page))) { | |
294 | create_empty_buffers(page, blocksize, 0); | |
295 | } | |
296 | ||
297 | if (page_has_buffers(page)) { | |
298 | struct buffer_head *head, *bh; | |
299 | u32 bh_off = 0; | |
300 | ||
301 | bh = head = page_buffers(page); | |
302 | do { | |
303 | u32 bh_next = bh_off + blocksize; | |
304 | ||
305 | if (from <= bh_off && bh_next <= to) { | |
306 | set_buffer_uptodate(bh); | |
307 | mark_buffer_dirty(bh); | |
308 | } else if (!buffer_uptodate(bh)) { | |
309 | partial = true; | |
310 | } | |
311 | bh_off = bh_next; | |
312 | } while (head != (bh = bh->b_this_page)); | |
313 | } | |
314 | ||
315 | zero_user_segment(page, from, to); | |
316 | ||
2f56a3f8 KK |
317 | if (!partial) |
318 | SetPageUptodate(page); | |
319 | flush_dcache_page(page); | |
320 | set_page_dirty(page); | |
4342306f KK |
321 | |
322 | if (idx != idx0) { | |
323 | unlock_page(page); | |
324 | put_page(page); | |
325 | } | |
326 | cond_resched(); | |
327 | } | |
4342306f KK |
328 | } |
329 | ||
330 | /* | |
e8b8e97f | 331 | * ntfs_file_mmap - file_operations::mmap |
4342306f KK |
332 | */ |
333 | static int ntfs_file_mmap(struct file *file, struct vm_area_struct *vma) | |
334 | { | |
335 | struct address_space *mapping = file->f_mapping; | |
336 | struct inode *inode = mapping->host; | |
337 | struct ntfs_inode *ni = ntfs_i(inode); | |
338 | u64 from = ((u64)vma->vm_pgoff << PAGE_SHIFT); | |
339 | bool rw = vma->vm_flags & VM_WRITE; | |
340 | int err; | |
341 | ||
342 | if (is_encrypted(ni)) { | |
343 | ntfs_inode_warn(inode, "mmap encrypted not supported"); | |
344 | return -EOPNOTSUPP; | |
345 | } | |
346 | ||
347 | if (is_dedup(ni)) { | |
348 | ntfs_inode_warn(inode, "mmap deduplicated not supported"); | |
349 | return -EOPNOTSUPP; | |
350 | } | |
351 | ||
352 | if (is_compressed(ni) && rw) { | |
353 | ntfs_inode_warn(inode, "mmap(write) compressed not supported"); | |
354 | return -EOPNOTSUPP; | |
355 | } | |
356 | ||
357 | if (rw) { | |
358 | u64 to = min_t(loff_t, i_size_read(inode), | |
359 | from + vma->vm_end - vma->vm_start); | |
360 | ||
361 | if (is_sparsed(ni)) { | |
e8b8e97f | 362 | /* Allocate clusters for rw map. */ |
4342306f KK |
363 | struct ntfs_sb_info *sbi = inode->i_sb->s_fs_info; |
364 | CLST lcn, len; | |
365 | CLST vcn = from >> sbi->cluster_bits; | |
366 | CLST end = bytes_to_cluster(sbi, to); | |
367 | bool new; | |
368 | ||
369 | for (; vcn < end; vcn += len) { | |
370 | err = attr_data_get_block(ni, vcn, 1, &lcn, | |
371 | &len, &new); | |
372 | if (err) | |
373 | goto out; | |
374 | ||
375 | if (!new) | |
376 | continue; | |
377 | ntfs_sparse_cluster(inode, NULL, vcn, 1); | |
378 | } | |
379 | } | |
380 | ||
381 | if (ni->i_valid < to) { | |
382 | if (!inode_trylock(inode)) { | |
383 | err = -EAGAIN; | |
384 | goto out; | |
385 | } | |
386 | err = ntfs_extend_initialized_size(file, ni, | |
387 | ni->i_valid, to); | |
388 | inode_unlock(inode); | |
389 | if (err) | |
390 | goto out; | |
391 | } | |
392 | } | |
393 | ||
394 | err = generic_file_mmap(file, vma); | |
395 | out: | |
396 | return err; | |
397 | } | |
398 | ||
399 | static int ntfs_extend(struct inode *inode, loff_t pos, size_t count, | |
400 | struct file *file) | |
401 | { | |
402 | struct ntfs_inode *ni = ntfs_i(inode); | |
403 | struct address_space *mapping = inode->i_mapping; | |
404 | loff_t end = pos + count; | |
405 | bool extend_init = file && pos > ni->i_valid; | |
406 | int err; | |
407 | ||
408 | if (end <= inode->i_size && !extend_init) | |
409 | return 0; | |
410 | ||
e8b8e97f | 411 | /* Mark rw ntfs as dirty. It will be cleared at umount. */ |
4342306f KK |
412 | ntfs_set_state(ni->mi.sbi, NTFS_DIRTY_DIRTY); |
413 | ||
414 | if (end > inode->i_size) { | |
415 | err = ntfs_set_size(inode, end); | |
416 | if (err) | |
417 | goto out; | |
418 | inode->i_size = end; | |
419 | } | |
420 | ||
421 | if (extend_init && !is_compressed(ni)) { | |
422 | err = ntfs_extend_initialized_size(file, ni, ni->i_valid, pos); | |
423 | if (err) | |
424 | goto out; | |
425 | } else { | |
426 | err = 0; | |
427 | } | |
428 | ||
429 | inode->i_ctime = inode->i_mtime = current_time(inode); | |
430 | mark_inode_dirty(inode); | |
431 | ||
432 | if (IS_SYNC(inode)) { | |
433 | int err2; | |
434 | ||
435 | err = filemap_fdatawrite_range(mapping, pos, end - 1); | |
436 | err2 = sync_mapping_buffers(mapping); | |
437 | if (!err) | |
438 | err = err2; | |
439 | err2 = write_inode_now(inode, 1); | |
440 | if (!err) | |
441 | err = err2; | |
442 | if (!err) | |
443 | err = filemap_fdatawait_range(mapping, pos, end - 1); | |
444 | } | |
445 | ||
446 | out: | |
447 | return err; | |
448 | } | |
449 | ||
450 | static int ntfs_truncate(struct inode *inode, loff_t new_size) | |
451 | { | |
452 | struct super_block *sb = inode->i_sb; | |
453 | struct ntfs_inode *ni = ntfs_i(inode); | |
454 | int err, dirty = 0; | |
455 | u64 new_valid; | |
456 | ||
457 | if (!S_ISREG(inode->i_mode)) | |
458 | return 0; | |
459 | ||
460 | if (is_compressed(ni)) { | |
461 | if (ni->i_valid > new_size) | |
462 | ni->i_valid = new_size; | |
463 | } else { | |
464 | err = block_truncate_page(inode->i_mapping, new_size, | |
465 | ntfs_get_block); | |
466 | if (err) | |
467 | return err; | |
468 | } | |
469 | ||
470 | new_valid = ntfs_up_block(sb, min_t(u64, ni->i_valid, new_size)); | |
471 | ||
472 | ni_lock(ni); | |
473 | ||
474 | truncate_setsize(inode, new_size); | |
475 | ||
476 | down_write(&ni->file.run_lock); | |
477 | err = attr_set_size(ni, ATTR_DATA, NULL, 0, &ni->file.run, new_size, | |
e95113ed | 478 | &new_valid, ni->mi.sbi->options->prealloc, NULL); |
4342306f KK |
479 | up_write(&ni->file.run_lock); |
480 | ||
481 | if (new_valid < ni->i_valid) | |
482 | ni->i_valid = new_valid; | |
483 | ||
484 | ni_unlock(ni); | |
485 | ||
486 | ni->std_fa |= FILE_ATTRIBUTE_ARCHIVE; | |
487 | inode->i_ctime = inode->i_mtime = current_time(inode); | |
488 | if (!IS_DIRSYNC(inode)) { | |
489 | dirty = 1; | |
490 | } else { | |
491 | err = ntfs_sync_inode(inode); | |
492 | if (err) | |
493 | return err; | |
494 | } | |
495 | ||
496 | if (dirty) | |
497 | mark_inode_dirty(inode); | |
498 | ||
499 | /*ntfs_flush_inodes(inode->i_sb, inode, NULL);*/ | |
500 | ||
501 | return 0; | |
502 | } | |
503 | ||
504 | /* | |
e8b8e97f KA |
505 | * ntfs_fallocate |
506 | * | |
4342306f KK |
507 | * Preallocate space for a file. This implements ntfs's fallocate file |
508 | * operation, which gets called from sys_fallocate system call. User | |
509 | * space requests 'len' bytes at 'vbo'. If FALLOC_FL_KEEP_SIZE is set | |
510 | * we just allocate clusters without zeroing them out. Otherwise we | |
511 | * allocate and zero out clusters via an expanding truncate. | |
512 | */ | |
513 | static long ntfs_fallocate(struct file *file, int mode, loff_t vbo, loff_t len) | |
514 | { | |
515 | struct inode *inode = file->f_mapping->host; | |
e4d2f4fd | 516 | struct address_space *mapping = inode->i_mapping; |
4342306f KK |
517 | struct super_block *sb = inode->i_sb; |
518 | struct ntfs_sb_info *sbi = sb->s_fs_info; | |
519 | struct ntfs_inode *ni = ntfs_i(inode); | |
520 | loff_t end = vbo + len; | |
521 | loff_t vbo_down = round_down(vbo, PAGE_SIZE); | |
e4d2f4fd KK |
522 | bool is_supported_holes = is_sparsed(ni) || is_compressed(ni); |
523 | loff_t i_size, new_size; | |
524 | bool map_locked; | |
4342306f KK |
525 | int err; |
526 | ||
e8b8e97f | 527 | /* No support for dir. */ |
4342306f KK |
528 | if (!S_ISREG(inode->i_mode)) |
529 | return -EOPNOTSUPP; | |
530 | ||
e4d2f4fd KK |
531 | /* |
532 | * vfs_fallocate checks all possible combinations of mode. | |
533 | * Do additional checks here before ntfs_set_state(dirty). | |
534 | */ | |
535 | if (mode & FALLOC_FL_PUNCH_HOLE) { | |
536 | if (!is_supported_holes) | |
537 | return -EOPNOTSUPP; | |
538 | } else if (mode & FALLOC_FL_COLLAPSE_RANGE) { | |
539 | } else if (mode & FALLOC_FL_INSERT_RANGE) { | |
540 | if (!is_supported_holes) | |
541 | return -EOPNOTSUPP; | |
542 | } else if (mode & | |
543 | ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE | | |
544 | FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)) { | |
4342306f KK |
545 | ntfs_inode_warn(inode, "fallocate(0x%x) is not supported", |
546 | mode); | |
547 | return -EOPNOTSUPP; | |
548 | } | |
549 | ||
550 | ntfs_set_state(sbi, NTFS_DIRTY_DIRTY); | |
551 | ||
552 | inode_lock(inode); | |
553 | i_size = inode->i_size; | |
e4d2f4fd KK |
554 | new_size = max(end, i_size); |
555 | map_locked = false; | |
4342306f KK |
556 | |
557 | if (WARN_ON(ni->ni_flags & NI_FLAG_COMPRESSED_MASK)) { | |
e8b8e97f | 558 | /* Should never be here, see ntfs_file_open. */ |
4342306f KK |
559 | err = -EOPNOTSUPP; |
560 | goto out; | |
561 | } | |
562 | ||
e4d2f4fd KK |
563 | if (mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_COLLAPSE_RANGE | |
564 | FALLOC_FL_INSERT_RANGE)) { | |
565 | inode_dio_wait(inode); | |
566 | filemap_invalidate_lock(mapping); | |
567 | map_locked = true; | |
568 | } | |
569 | ||
4342306f KK |
570 | if (mode & FALLOC_FL_PUNCH_HOLE) { |
571 | u32 frame_size; | |
572 | loff_t mask, vbo_a, end_a, tmp; | |
573 | ||
2f56a3f8 | 574 | err = filemap_write_and_wait_range(mapping, vbo, LLONG_MAX); |
4342306f KK |
575 | if (err) |
576 | goto out; | |
577 | ||
4342306f KK |
578 | truncate_pagecache(inode, vbo_down); |
579 | ||
4342306f KK |
580 | ni_lock(ni); |
581 | err = attr_punch_hole(ni, vbo, len, &frame_size); | |
582 | ni_unlock(ni); | |
583 | if (err != E_NTFS_NOTALIGNED) | |
584 | goto out; | |
585 | ||
d3624466 | 586 | /* Process not aligned punch. */ |
4342306f KK |
587 | mask = frame_size - 1; |
588 | vbo_a = (vbo + mask) & ~mask; | |
589 | end_a = end & ~mask; | |
590 | ||
591 | tmp = min(vbo_a, end); | |
592 | if (tmp > vbo) { | |
593 | err = ntfs_zero_range(inode, vbo, tmp); | |
594 | if (err) | |
595 | goto out; | |
596 | } | |
597 | ||
598 | if (vbo < end_a && end_a < end) { | |
599 | err = ntfs_zero_range(inode, end_a, end); | |
600 | if (err) | |
601 | goto out; | |
602 | } | |
603 | ||
604 | /* Aligned punch_hole */ | |
605 | if (end_a > vbo_a) { | |
606 | ni_lock(ni); | |
607 | err = attr_punch_hole(ni, vbo_a, end_a - vbo_a, NULL); | |
608 | ni_unlock(ni); | |
609 | } | |
610 | } else if (mode & FALLOC_FL_COLLAPSE_RANGE) { | |
4342306f KK |
611 | /* |
612 | * Write tail of the last page before removed range since | |
613 | * it will get removed from the page cache below. | |
614 | */ | |
e4d2f4fd | 615 | err = filemap_write_and_wait_range(mapping, vbo_down, vbo); |
4342306f KK |
616 | if (err) |
617 | goto out; | |
618 | ||
619 | /* | |
620 | * Write data that will be shifted to preserve them | |
e8b8e97f | 621 | * when discarding page cache below. |
4342306f | 622 | */ |
e4d2f4fd | 623 | err = filemap_write_and_wait_range(mapping, end, LLONG_MAX); |
4342306f KK |
624 | if (err) |
625 | goto out; | |
626 | ||
4342306f KK |
627 | truncate_pagecache(inode, vbo_down); |
628 | ||
629 | ni_lock(ni); | |
630 | err = attr_collapse_range(ni, vbo, len); | |
631 | ni_unlock(ni); | |
e4d2f4fd KK |
632 | } else if (mode & FALLOC_FL_INSERT_RANGE) { |
633 | /* Check new size. */ | |
634 | err = inode_newsize_ok(inode, new_size); | |
635 | if (err) | |
636 | goto out; | |
637 | ||
638 | /* Write out all dirty pages. */ | |
639 | err = filemap_write_and_wait_range(mapping, vbo_down, | |
640 | LLONG_MAX); | |
641 | if (err) | |
642 | goto out; | |
643 | truncate_pagecache(inode, vbo_down); | |
11434697 | 644 | |
e4d2f4fd KK |
645 | ni_lock(ni); |
646 | err = attr_insert_range(ni, vbo, len); | |
647 | ni_unlock(ni); | |
4342306f | 648 | } else { |
e4d2f4fd | 649 | /* Check new size. */ |
b3e04872 KK |
650 | |
651 | /* generic/213: expected -ENOSPC instead of -EFBIG. */ | |
652 | if (!is_supported_holes) { | |
653 | loff_t to_alloc = new_size - inode_get_bytes(inode); | |
654 | ||
655 | if (to_alloc > 0 && | |
656 | (to_alloc >> sbi->cluster_bits) > | |
657 | wnd_zeroes(&sbi->used.bitmap)) { | |
658 | err = -ENOSPC; | |
659 | goto out; | |
660 | } | |
661 | } | |
11434697 KK |
662 | |
663 | err = inode_newsize_ok(inode, new_size); | |
664 | if (err) | |
665 | goto out; | |
666 | ||
e4d2f4fd KK |
667 | /* |
668 | * Allocate clusters, do not change 'valid' size. | |
669 | */ | |
11434697 | 670 | err = ntfs_set_size(inode, new_size); |
4342306f KK |
671 | if (err) |
672 | goto out; | |
673 | ||
e4d2f4fd | 674 | if (is_supported_holes) { |
2f56a3f8 | 675 | CLST vcn_v = bytes_to_cluster(sbi, ni->i_valid); |
4342306f KK |
676 | CLST vcn = vbo >> sbi->cluster_bits; |
677 | CLST cend = bytes_to_cluster(sbi, end); | |
678 | CLST lcn, clen; | |
679 | bool new; | |
680 | ||
681 | /* | |
e8b8e97f KA |
682 | * Allocate but do not zero new clusters. (see below comments) |
683 | * This breaks security: One can read unused on-disk areas. | |
684 | * Zeroing these clusters may be too long. | |
685 | * Maybe we should check here for root rights? | |
4342306f KK |
686 | */ |
687 | for (; vcn < cend; vcn += clen) { | |
688 | err = attr_data_get_block(ni, vcn, cend - vcn, | |
689 | &lcn, &clen, &new); | |
690 | if (err) | |
691 | goto out; | |
692 | if (!new || vcn >= vcn_v) | |
693 | continue; | |
694 | ||
695 | /* | |
e8b8e97f KA |
696 | * Unwritten area. |
697 | * NTFS is not able to store several unwritten areas. | |
698 | * Activate 'ntfs_sparse_cluster' to zero new allocated clusters. | |
4342306f KK |
699 | * |
700 | * Dangerous in case: | |
701 | * 1G of sparsed clusters + 1 cluster of data => | |
702 | * valid_size == 1G + 1 cluster | |
703 | * fallocate(1G) will zero 1G and this can be very long | |
e8b8e97f | 704 | * xfstest 016/086 will fail without 'ntfs_sparse_cluster'. |
4342306f KK |
705 | */ |
706 | ntfs_sparse_cluster(inode, NULL, vcn, | |
707 | min(vcn_v - vcn, clen)); | |
708 | } | |
709 | } | |
710 | ||
711 | if (mode & FALLOC_FL_KEEP_SIZE) { | |
712 | ni_lock(ni); | |
e8b8e97f | 713 | /* True - Keep preallocated. */ |
4342306f KK |
714 | err = attr_set_size(ni, ATTR_DATA, NULL, 0, |
715 | &ni->file.run, i_size, &ni->i_valid, | |
716 | true, NULL); | |
717 | ni_unlock(ni); | |
718 | } | |
719 | } | |
720 | ||
721 | out: | |
e4d2f4fd KK |
722 | if (map_locked) |
723 | filemap_invalidate_unlock(mapping); | |
4342306f KK |
724 | |
725 | if (!err) { | |
726 | inode->i_ctime = inode->i_mtime = current_time(inode); | |
727 | mark_inode_dirty(inode); | |
728 | } | |
729 | ||
730 | inode_unlock(inode); | |
731 | return err; | |
732 | } | |
733 | ||
734 | /* | |
e8b8e97f | 735 | * ntfs3_setattr - inode_operations::setattr |
4342306f KK |
736 | */ |
737 | int ntfs3_setattr(struct user_namespace *mnt_userns, struct dentry *dentry, | |
738 | struct iattr *attr) | |
739 | { | |
740 | struct super_block *sb = dentry->d_sb; | |
741 | struct ntfs_sb_info *sbi = sb->s_fs_info; | |
742 | struct inode *inode = d_inode(dentry); | |
743 | struct ntfs_inode *ni = ntfs_i(inode); | |
744 | u32 ia_valid = attr->ia_valid; | |
745 | umode_t mode = inode->i_mode; | |
746 | int err; | |
747 | ||
28a941ff | 748 | if (sbi->options->noacsrules) { |
e8b8e97f | 749 | /* "No access rules" - Force any changes of time etc. */ |
4342306f | 750 | attr->ia_valid |= ATTR_FORCE; |
e8b8e97f | 751 | /* and disable for editing some attributes. */ |
4342306f KK |
752 | attr->ia_valid &= ~(ATTR_UID | ATTR_GID | ATTR_MODE); |
753 | ia_valid = attr->ia_valid; | |
754 | } | |
755 | ||
756 | err = setattr_prepare(mnt_userns, dentry, attr); | |
757 | if (err) | |
758 | goto out; | |
759 | ||
760 | if (ia_valid & ATTR_SIZE) { | |
761 | loff_t oldsize = inode->i_size; | |
762 | ||
763 | if (WARN_ON(ni->ni_flags & NI_FLAG_COMPRESSED_MASK)) { | |
e8b8e97f | 764 | /* Should never be here, see ntfs_file_open(). */ |
4342306f KK |
765 | err = -EOPNOTSUPP; |
766 | goto out; | |
767 | } | |
768 | inode_dio_wait(inode); | |
769 | ||
3880f2b8 | 770 | if (attr->ia_size <= oldsize) |
4342306f KK |
771 | err = ntfs_truncate(inode, attr->ia_size); |
772 | else if (attr->ia_size > oldsize) | |
773 | err = ntfs_extend(inode, attr->ia_size, 0, NULL); | |
774 | ||
775 | if (err) | |
776 | goto out; | |
777 | ||
778 | ni->ni_flags |= NI_FLAG_UPDATE_PARENT; | |
779 | } | |
780 | ||
781 | setattr_copy(mnt_userns, inode, attr); | |
782 | ||
783 | if (mode != inode->i_mode) { | |
784 | err = ntfs_acl_chmod(mnt_userns, inode); | |
785 | if (err) | |
786 | goto out; | |
787 | ||
e8b8e97f | 788 | /* Linux 'w' -> Windows 'ro'. */ |
4342306f KK |
789 | if (0222 & inode->i_mode) |
790 | ni->std_fa &= ~FILE_ATTRIBUTE_READONLY; | |
791 | else | |
792 | ni->std_fa |= FILE_ATTRIBUTE_READONLY; | |
793 | } | |
794 | ||
795 | if (ia_valid & (ATTR_UID | ATTR_GID | ATTR_MODE)) | |
796 | ntfs_save_wsl_perm(inode); | |
797 | mark_inode_dirty(inode); | |
798 | out: | |
799 | return err; | |
800 | } | |
801 | ||
802 | static ssize_t ntfs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter) | |
803 | { | |
4342306f KK |
804 | struct file *file = iocb->ki_filp; |
805 | struct inode *inode = file->f_mapping->host; | |
806 | struct ntfs_inode *ni = ntfs_i(inode); | |
807 | ||
808 | if (is_encrypted(ni)) { | |
809 | ntfs_inode_warn(inode, "encrypted i/o not supported"); | |
810 | return -EOPNOTSUPP; | |
811 | } | |
812 | ||
813 | if (is_compressed(ni) && (iocb->ki_flags & IOCB_DIRECT)) { | |
814 | ntfs_inode_warn(inode, "direct i/o + compressed not supported"); | |
815 | return -EOPNOTSUPP; | |
816 | } | |
817 | ||
818 | #ifndef CONFIG_NTFS3_LZX_XPRESS | |
819 | if (ni->ni_flags & NI_FLAG_COMPRESSED_MASK) { | |
820 | ntfs_inode_warn( | |
821 | inode, | |
822 | "activate CONFIG_NTFS3_LZX_XPRESS to read external compressed files"); | |
823 | return -EOPNOTSUPP; | |
824 | } | |
825 | #endif | |
826 | ||
827 | if (is_dedup(ni)) { | |
828 | ntfs_inode_warn(inode, "read deduplicated not supported"); | |
829 | return -EOPNOTSUPP; | |
830 | } | |
831 | ||
dd854e4b | 832 | return generic_file_read_iter(iocb, iter); |
4342306f KK |
833 | } |
834 | ||
e8b8e97f KA |
835 | /* |
836 | * ntfs_get_frame_pages | |
837 | * | |
838 | * Return: Array of locked pages. | |
839 | */ | |
4342306f KK |
840 | static int ntfs_get_frame_pages(struct address_space *mapping, pgoff_t index, |
841 | struct page **pages, u32 pages_per_frame, | |
842 | bool *frame_uptodate) | |
843 | { | |
844 | gfp_t gfp_mask = mapping_gfp_mask(mapping); | |
845 | u32 npages; | |
846 | ||
847 | *frame_uptodate = true; | |
848 | ||
849 | for (npages = 0; npages < pages_per_frame; npages++, index++) { | |
850 | struct page *page; | |
851 | ||
852 | page = find_or_create_page(mapping, index, gfp_mask); | |
853 | if (!page) { | |
854 | while (npages--) { | |
855 | page = pages[npages]; | |
856 | unlock_page(page); | |
857 | put_page(page); | |
858 | } | |
859 | ||
860 | return -ENOMEM; | |
861 | } | |
862 | ||
863 | if (!PageUptodate(page)) | |
864 | *frame_uptodate = false; | |
865 | ||
866 | pages[npages] = page; | |
867 | } | |
868 | ||
869 | return 0; | |
870 | } | |
871 | ||
e8b8e97f KA |
872 | /* |
873 | * ntfs_compress_write - Helper for ntfs_file_write_iter() (compressed files). | |
874 | */ | |
4342306f KK |
875 | static ssize_t ntfs_compress_write(struct kiocb *iocb, struct iov_iter *from) |
876 | { | |
877 | int err; | |
878 | struct file *file = iocb->ki_filp; | |
879 | size_t count = iov_iter_count(from); | |
880 | loff_t pos = iocb->ki_pos; | |
881 | struct inode *inode = file_inode(file); | |
882 | loff_t i_size = inode->i_size; | |
883 | struct address_space *mapping = inode->i_mapping; | |
884 | struct ntfs_inode *ni = ntfs_i(inode); | |
885 | u64 valid = ni->i_valid; | |
886 | struct ntfs_sb_info *sbi = ni->mi.sbi; | |
887 | struct page *page, **pages = NULL; | |
888 | size_t written = 0; | |
889 | u8 frame_bits = NTFS_LZNT_CUNIT + sbi->cluster_bits; | |
890 | u32 frame_size = 1u << frame_bits; | |
891 | u32 pages_per_frame = frame_size >> PAGE_SHIFT; | |
892 | u32 ip, off; | |
893 | CLST frame; | |
894 | u64 frame_vbo; | |
895 | pgoff_t index; | |
896 | bool frame_uptodate; | |
897 | ||
898 | if (frame_size < PAGE_SIZE) { | |
899 | /* | |
900 | * frame_size == 8K if cluster 512 | |
901 | * frame_size == 64K if cluster 4096 | |
902 | */ | |
903 | ntfs_inode_warn(inode, "page size is bigger than frame size"); | |
904 | return -EOPNOTSUPP; | |
905 | } | |
906 | ||
345482bc | 907 | pages = kmalloc_array(pages_per_frame, sizeof(struct page *), GFP_NOFS); |
4342306f KK |
908 | if (!pages) |
909 | return -ENOMEM; | |
910 | ||
911 | current->backing_dev_info = inode_to_bdi(inode); | |
912 | err = file_remove_privs(file); | |
913 | if (err) | |
914 | goto out; | |
915 | ||
916 | err = file_update_time(file); | |
917 | if (err) | |
918 | goto out; | |
919 | ||
e8b8e97f | 920 | /* Zero range [valid : pos). */ |
4342306f KK |
921 | while (valid < pos) { |
922 | CLST lcn, clen; | |
923 | ||
924 | frame = valid >> frame_bits; | |
925 | frame_vbo = valid & ~(frame_size - 1); | |
926 | off = valid & (frame_size - 1); | |
927 | ||
928 | err = attr_data_get_block(ni, frame << NTFS_LZNT_CUNIT, 0, &lcn, | |
929 | &clen, NULL); | |
930 | if (err) | |
931 | goto out; | |
932 | ||
933 | if (lcn == SPARSE_LCN) { | |
934 | ni->i_valid = valid = | |
935 | frame_vbo + ((u64)clen << sbi->cluster_bits); | |
936 | continue; | |
937 | } | |
938 | ||
e8b8e97f | 939 | /* Load full frame. */ |
4342306f KK |
940 | err = ntfs_get_frame_pages(mapping, frame_vbo >> PAGE_SHIFT, |
941 | pages, pages_per_frame, | |
942 | &frame_uptodate); | |
943 | if (err) | |
944 | goto out; | |
945 | ||
946 | if (!frame_uptodate && off) { | |
947 | err = ni_read_frame(ni, frame_vbo, pages, | |
948 | pages_per_frame); | |
949 | if (err) { | |
950 | for (ip = 0; ip < pages_per_frame; ip++) { | |
951 | page = pages[ip]; | |
952 | unlock_page(page); | |
953 | put_page(page); | |
954 | } | |
955 | goto out; | |
956 | } | |
957 | } | |
958 | ||
959 | ip = off >> PAGE_SHIFT; | |
960 | off = offset_in_page(valid); | |
961 | for (; ip < pages_per_frame; ip++, off = 0) { | |
962 | page = pages[ip]; | |
963 | zero_user_segment(page, off, PAGE_SIZE); | |
964 | flush_dcache_page(page); | |
965 | SetPageUptodate(page); | |
966 | } | |
967 | ||
968 | ni_lock(ni); | |
969 | err = ni_write_frame(ni, pages, pages_per_frame); | |
970 | ni_unlock(ni); | |
971 | ||
972 | for (ip = 0; ip < pages_per_frame; ip++) { | |
973 | page = pages[ip]; | |
974 | SetPageUptodate(page); | |
975 | unlock_page(page); | |
976 | put_page(page); | |
977 | } | |
978 | ||
979 | if (err) | |
980 | goto out; | |
981 | ||
982 | ni->i_valid = valid = frame_vbo + frame_size; | |
983 | } | |
984 | ||
e8b8e97f | 985 | /* Copy user data [pos : pos + count). */ |
4342306f KK |
986 | while (count) { |
987 | size_t copied, bytes; | |
988 | ||
989 | off = pos & (frame_size - 1); | |
990 | bytes = frame_size - off; | |
991 | if (bytes > count) | |
992 | bytes = count; | |
993 | ||
4342306f KK |
994 | frame_vbo = pos & ~(frame_size - 1); |
995 | index = frame_vbo >> PAGE_SHIFT; | |
996 | ||
a6294593 | 997 | if (unlikely(fault_in_iov_iter_readable(from, bytes))) { |
4342306f KK |
998 | err = -EFAULT; |
999 | goto out; | |
1000 | } | |
1001 | ||
e8b8e97f | 1002 | /* Load full frame. */ |
4342306f KK |
1003 | err = ntfs_get_frame_pages(mapping, index, pages, |
1004 | pages_per_frame, &frame_uptodate); | |
1005 | if (err) | |
1006 | goto out; | |
1007 | ||
1008 | if (!frame_uptodate) { | |
1009 | loff_t to = pos + bytes; | |
1010 | ||
1011 | if (off || (to < i_size && (to & (frame_size - 1)))) { | |
1012 | err = ni_read_frame(ni, frame_vbo, pages, | |
1013 | pages_per_frame); | |
1014 | if (err) { | |
1015 | for (ip = 0; ip < pages_per_frame; | |
1016 | ip++) { | |
1017 | page = pages[ip]; | |
1018 | unlock_page(page); | |
1019 | put_page(page); | |
1020 | } | |
1021 | goto out; | |
1022 | } | |
1023 | } | |
1024 | } | |
1025 | ||
1026 | WARN_ON(!bytes); | |
1027 | copied = 0; | |
1028 | ip = off >> PAGE_SHIFT; | |
1029 | off = offset_in_page(pos); | |
1030 | ||
e8b8e97f | 1031 | /* Copy user data to pages. */ |
4342306f KK |
1032 | for (;;) { |
1033 | size_t cp, tail = PAGE_SIZE - off; | |
1034 | ||
1035 | page = pages[ip]; | |
1036 | cp = copy_page_from_iter_atomic(page, off, | |
1037 | min(tail, bytes), from); | |
1038 | flush_dcache_page(page); | |
1039 | ||
1040 | copied += cp; | |
1041 | bytes -= cp; | |
1042 | if (!bytes || !cp) | |
1043 | break; | |
1044 | ||
1045 | if (cp < tail) { | |
1046 | off += cp; | |
1047 | } else { | |
1048 | ip++; | |
1049 | off = 0; | |
1050 | } | |
1051 | } | |
1052 | ||
1053 | ni_lock(ni); | |
1054 | err = ni_write_frame(ni, pages, pages_per_frame); | |
1055 | ni_unlock(ni); | |
1056 | ||
1057 | for (ip = 0; ip < pages_per_frame; ip++) { | |
1058 | page = pages[ip]; | |
1059 | ClearPageDirty(page); | |
1060 | SetPageUptodate(page); | |
1061 | unlock_page(page); | |
1062 | put_page(page); | |
1063 | } | |
1064 | ||
1065 | if (err) | |
1066 | goto out; | |
1067 | ||
1068 | /* | |
1069 | * We can loop for a long time in here. Be nice and allow | |
1070 | * us to schedule out to avoid softlocking if preempt | |
1071 | * is disabled. | |
1072 | */ | |
1073 | cond_resched(); | |
1074 | ||
1075 | pos += copied; | |
1076 | written += copied; | |
1077 | ||
1078 | count = iov_iter_count(from); | |
1079 | } | |
1080 | ||
1081 | out: | |
195c52bd | 1082 | kfree(pages); |
4342306f KK |
1083 | |
1084 | current->backing_dev_info = NULL; | |
1085 | ||
1086 | if (err < 0) | |
1087 | return err; | |
1088 | ||
1089 | iocb->ki_pos += written; | |
1090 | if (iocb->ki_pos > ni->i_valid) | |
1091 | ni->i_valid = iocb->ki_pos; | |
1092 | ||
1093 | return written; | |
1094 | } | |
1095 | ||
1096 | /* | |
e8b8e97f | 1097 | * ntfs_file_write_iter - file_operations::write_iter |
4342306f KK |
1098 | */ |
1099 | static ssize_t ntfs_file_write_iter(struct kiocb *iocb, struct iov_iter *from) | |
1100 | { | |
1101 | struct file *file = iocb->ki_filp; | |
1102 | struct address_space *mapping = file->f_mapping; | |
1103 | struct inode *inode = mapping->host; | |
1104 | ssize_t ret; | |
1105 | struct ntfs_inode *ni = ntfs_i(inode); | |
1106 | ||
1107 | if (is_encrypted(ni)) { | |
1108 | ntfs_inode_warn(inode, "encrypted i/o not supported"); | |
1109 | return -EOPNOTSUPP; | |
1110 | } | |
1111 | ||
1112 | if (is_compressed(ni) && (iocb->ki_flags & IOCB_DIRECT)) { | |
1113 | ntfs_inode_warn(inode, "direct i/o + compressed not supported"); | |
1114 | return -EOPNOTSUPP; | |
1115 | } | |
1116 | ||
1117 | if (is_dedup(ni)) { | |
1118 | ntfs_inode_warn(inode, "write into deduplicated not supported"); | |
1119 | return -EOPNOTSUPP; | |
1120 | } | |
1121 | ||
1122 | if (!inode_trylock(inode)) { | |
1123 | if (iocb->ki_flags & IOCB_NOWAIT) | |
1124 | return -EAGAIN; | |
1125 | inode_lock(inode); | |
1126 | } | |
1127 | ||
1128 | ret = generic_write_checks(iocb, from); | |
1129 | if (ret <= 0) | |
1130 | goto out; | |
1131 | ||
1132 | if (WARN_ON(ni->ni_flags & NI_FLAG_COMPRESSED_MASK)) { | |
d3624466 | 1133 | /* Should never be here, see ntfs_file_open(). */ |
4342306f KK |
1134 | ret = -EOPNOTSUPP; |
1135 | goto out; | |
1136 | } | |
1137 | ||
1138 | ret = ntfs_extend(inode, iocb->ki_pos, ret, file); | |
1139 | if (ret) | |
1140 | goto out; | |
1141 | ||
1142 | ret = is_compressed(ni) ? ntfs_compress_write(iocb, from) | |
1143 | : __generic_file_write_iter(iocb, from); | |
1144 | ||
1145 | out: | |
1146 | inode_unlock(inode); | |
1147 | ||
1148 | if (ret > 0) | |
1149 | ret = generic_write_sync(iocb, ret); | |
1150 | ||
1151 | return ret; | |
1152 | } | |
1153 | ||
1154 | /* | |
e8b8e97f | 1155 | * ntfs_file_open - file_operations::open |
4342306f KK |
1156 | */ |
1157 | int ntfs_file_open(struct inode *inode, struct file *file) | |
1158 | { | |
1159 | struct ntfs_inode *ni = ntfs_i(inode); | |
1160 | ||
1161 | if (unlikely((is_compressed(ni) || is_encrypted(ni)) && | |
1162 | (file->f_flags & O_DIRECT))) { | |
1163 | return -EOPNOTSUPP; | |
1164 | } | |
1165 | ||
e8b8e97f | 1166 | /* Decompress "external compressed" file if opened for rw. */ |
4342306f KK |
1167 | if ((ni->ni_flags & NI_FLAG_COMPRESSED_MASK) && |
1168 | (file->f_flags & (O_WRONLY | O_RDWR | O_TRUNC))) { | |
1169 | #ifdef CONFIG_NTFS3_LZX_XPRESS | |
1170 | int err = ni_decompress_file(ni); | |
1171 | ||
1172 | if (err) | |
1173 | return err; | |
1174 | #else | |
1175 | ntfs_inode_warn( | |
1176 | inode, | |
1177 | "activate CONFIG_NTFS3_LZX_XPRESS to write external compressed files"); | |
1178 | return -EOPNOTSUPP; | |
1179 | #endif | |
1180 | } | |
1181 | ||
1182 | return generic_file_open(inode, file); | |
1183 | } | |
1184 | ||
1185 | /* | |
e8b8e97f | 1186 | * ntfs_file_release - file_operations::release |
4342306f KK |
1187 | */ |
1188 | static int ntfs_file_release(struct inode *inode, struct file *file) | |
1189 | { | |
1190 | struct ntfs_inode *ni = ntfs_i(inode); | |
1191 | struct ntfs_sb_info *sbi = ni->mi.sbi; | |
1192 | int err = 0; | |
1193 | ||
e8b8e97f | 1194 | /* If we are last writer on the inode, drop the block reservation. */ |
564c97bd | 1195 | if (sbi->options->prealloc && ((file->f_mode & FMODE_WRITE) && |
4342306f KK |
1196 | atomic_read(&inode->i_writecount) == 1)) { |
1197 | ni_lock(ni); | |
1198 | down_write(&ni->file.run_lock); | |
1199 | ||
1200 | err = attr_set_size(ni, ATTR_DATA, NULL, 0, &ni->file.run, | |
1201 | inode->i_size, &ni->i_valid, false, NULL); | |
1202 | ||
1203 | up_write(&ni->file.run_lock); | |
1204 | ni_unlock(ni); | |
1205 | } | |
1206 | return err; | |
1207 | } | |
1208 | ||
e8b8e97f KA |
1209 | /* |
1210 | * ntfs_fiemap - file_operations::fiemap | |
1211 | */ | |
4342306f KK |
1212 | int ntfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
1213 | __u64 start, __u64 len) | |
1214 | { | |
1215 | int err; | |
1216 | struct ntfs_inode *ni = ntfs_i(inode); | |
1217 | ||
d4e8e135 KA |
1218 | err = fiemap_prep(inode, fieinfo, start, &len, ~FIEMAP_FLAG_XATTR); |
1219 | if (err) | |
1220 | return err; | |
4342306f KK |
1221 | |
1222 | ni_lock(ni); | |
1223 | ||
1224 | err = ni_fiemap(ni, fieinfo, start, len); | |
1225 | ||
1226 | ni_unlock(ni); | |
1227 | ||
1228 | return err; | |
1229 | } | |
1230 | ||
1231 | // clang-format off | |
1232 | const struct inode_operations ntfs_file_inode_operations = { | |
1233 | .getattr = ntfs_getattr, | |
1234 | .setattr = ntfs3_setattr, | |
1235 | .listxattr = ntfs_listxattr, | |
1236 | .permission = ntfs_permission, | |
1237 | .get_acl = ntfs_get_acl, | |
1238 | .set_acl = ntfs_set_acl, | |
1239 | .fiemap = ntfs_fiemap, | |
1240 | }; | |
1241 | ||
1242 | const struct file_operations ntfs_file_operations = { | |
1243 | .llseek = generic_file_llseek, | |
1244 | .read_iter = ntfs_file_read_iter, | |
1245 | .write_iter = ntfs_file_write_iter, | |
1246 | .unlocked_ioctl = ntfs_ioctl, | |
1247 | #ifdef CONFIG_COMPAT | |
1248 | .compat_ioctl = ntfs_compat_ioctl, | |
1249 | #endif | |
1250 | .splice_read = generic_file_splice_read, | |
1251 | .mmap = ntfs_file_mmap, | |
1252 | .open = ntfs_file_open, | |
1253 | .fsync = generic_file_fsync, | |
1254 | .splice_write = iter_file_splice_write, | |
1255 | .fallocate = ntfs_fallocate, | |
1256 | .release = ntfs_file_release, | |
1257 | }; | |
1258 | // clang-format on |