1
2
3
4
5
6#include <linux/init.h>
7#include <linux/buffer_head.h>
8#include <linux/mpage.h>
9#include <linux/bio.h>
10#include <linux/blkdev.h>
11#include <linux/time.h>
12#include <linux/writeback.h>
13#include <linux/uio.h>
14#include <linux/random.h>
15#include <linux/iversion.h>
16
17#include "exfat_raw.h"
18#include "exfat_fs.h"
19
20static int __exfat_write_inode(struct inode *inode, int sync)
21{
22 unsigned long long on_disk_size;
23 struct exfat_dentry *ep, *ep2;
24 struct exfat_entry_set_cache *es = NULL;
25 struct super_block *sb = inode->i_sb;
26 struct exfat_sb_info *sbi = EXFAT_SB(sb);
27 struct exfat_inode_info *ei = EXFAT_I(inode);
28 bool is_dir = (ei->type == TYPE_DIR) ? true : false;
29
30 if (inode->i_ino == EXFAT_ROOT_INO)
31 return 0;
32
33
34
35
36 if (ei->dir.dir == DIR_DELETED)
37 return 0;
38
39 if (is_dir && ei->dir.dir == sbi->root_dir && ei->entry == -1)
40 return 0;
41
42 exfat_set_volume_dirty(sb);
43
44
45 es = exfat_get_dentry_set(sb, &(ei->dir), ei->entry, ES_ALL_ENTRIES);
46 if (!es)
47 return -EIO;
48 ep = exfat_get_dentry_cached(es, 0);
49 ep2 = exfat_get_dentry_cached(es, 1);
50
51 ep->dentry.file.attr = cpu_to_le16(exfat_make_attr(inode));
52
53
54 exfat_set_entry_time(sbi, &ei->i_crtime,
55 &ep->dentry.file.create_tz,
56 &ep->dentry.file.create_time,
57 &ep->dentry.file.create_date,
58 &ep->dentry.file.create_time_cs);
59 exfat_set_entry_time(sbi, &inode->i_mtime,
60 &ep->dentry.file.modify_tz,
61 &ep->dentry.file.modify_time,
62 &ep->dentry.file.modify_date,
63 &ep->dentry.file.modify_time_cs);
64 exfat_set_entry_time(sbi, &inode->i_atime,
65 &ep->dentry.file.access_tz,
66 &ep->dentry.file.access_time,
67 &ep->dentry.file.access_date,
68 NULL);
69
70
71 on_disk_size = i_size_read(inode);
72
73 if (ei->start_clu == EXFAT_EOF_CLUSTER)
74 on_disk_size = 0;
75
76 ep2->dentry.stream.valid_size = cpu_to_le64(on_disk_size);
77 ep2->dentry.stream.size = ep2->dentry.stream.valid_size;
78
79 exfat_update_dir_chksum_with_entry_set(es);
80 return exfat_free_dentry_set(es, sync);
81}
82
83int exfat_write_inode(struct inode *inode, struct writeback_control *wbc)
84{
85 int ret;
86
87 mutex_lock(&EXFAT_SB(inode->i_sb)->s_lock);
88 ret = __exfat_write_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
89 mutex_unlock(&EXFAT_SB(inode->i_sb)->s_lock);
90
91 return ret;
92}
93
94void exfat_sync_inode(struct inode *inode)
95{
96 lockdep_assert_held(&EXFAT_SB(inode->i_sb)->s_lock);
97 __exfat_write_inode(inode, 1);
98}
99
100
101
102
103
104
105static int exfat_map_cluster(struct inode *inode, unsigned int clu_offset,
106 unsigned int *clu, int create)
107{
108 int ret, modified = false;
109 unsigned int last_clu;
110 struct exfat_chain new_clu;
111 struct super_block *sb = inode->i_sb;
112 struct exfat_sb_info *sbi = EXFAT_SB(sb);
113 struct exfat_inode_info *ei = EXFAT_I(inode);
114 unsigned int local_clu_offset = clu_offset;
115 unsigned int num_to_be_allocated = 0, num_clusters = 0;
116
117 if (EXFAT_I(inode)->i_size_ondisk > 0)
118 num_clusters =
119 EXFAT_B_TO_CLU_ROUND_UP(EXFAT_I(inode)->i_size_ondisk,
120 sbi);
121
122 if (clu_offset >= num_clusters)
123 num_to_be_allocated = clu_offset - num_clusters + 1;
124
125 if (!create && (num_to_be_allocated > 0)) {
126 *clu = EXFAT_EOF_CLUSTER;
127 return 0;
128 }
129
130 *clu = last_clu = ei->start_clu;
131
132 if (ei->flags == ALLOC_NO_FAT_CHAIN) {
133 if (clu_offset > 0 && *clu != EXFAT_EOF_CLUSTER) {
134 last_clu += clu_offset - 1;
135
136 if (clu_offset == num_clusters)
137 *clu = EXFAT_EOF_CLUSTER;
138 else
139 *clu += clu_offset;
140 }
141 } else if (ei->type == TYPE_FILE) {
142 unsigned int fclus = 0;
143 int err = exfat_get_cluster(inode, clu_offset,
144 &fclus, clu, &last_clu, 1);
145 if (err)
146 return -EIO;
147
148 clu_offset -= fclus;
149 } else {
150
151 if (clu_offset > 0 && ei->hint_bmap.off != EXFAT_EOF_CLUSTER &&
152 ei->hint_bmap.off > 0 && clu_offset >= ei->hint_bmap.off) {
153 clu_offset -= ei->hint_bmap.off;
154
155 WARN_ON(ei->hint_bmap.clu < 2);
156 *clu = ei->hint_bmap.clu;
157 }
158
159 while (clu_offset > 0 && *clu != EXFAT_EOF_CLUSTER) {
160 last_clu = *clu;
161 if (exfat_get_next_cluster(sb, clu))
162 return -EIO;
163 clu_offset--;
164 }
165 }
166
167 if (*clu == EXFAT_EOF_CLUSTER) {
168 exfat_set_volume_dirty(sb);
169
170 new_clu.dir = (last_clu == EXFAT_EOF_CLUSTER) ?
171 EXFAT_EOF_CLUSTER : last_clu + 1;
172 new_clu.size = 0;
173 new_clu.flags = ei->flags;
174
175
176 if (num_to_be_allocated < 1) {
177
178 exfat_fs_error(sb, "broken FAT chain.");
179 return -EIO;
180 }
181
182 ret = exfat_alloc_cluster(inode, num_to_be_allocated, &new_clu);
183 if (ret)
184 return ret;
185
186 if (new_clu.dir == EXFAT_EOF_CLUSTER ||
187 new_clu.dir == EXFAT_FREE_CLUSTER) {
188 exfat_fs_error(sb,
189 "bogus cluster new allocated (last_clu : %u, new_clu : %u)",
190 last_clu, new_clu.dir);
191 return -EIO;
192 }
193
194
195 if (last_clu == EXFAT_EOF_CLUSTER) {
196 if (new_clu.flags == ALLOC_FAT_CHAIN)
197 ei->flags = ALLOC_FAT_CHAIN;
198 ei->start_clu = new_clu.dir;
199 modified = true;
200 } else {
201 if (new_clu.flags != ei->flags) {
202
203
204
205
206 exfat_chain_cont_cluster(sb, ei->start_clu,
207 num_clusters);
208 ei->flags = ALLOC_FAT_CHAIN;
209 modified = true;
210 }
211 if (new_clu.flags == ALLOC_FAT_CHAIN)
212 if (exfat_ent_set(sb, last_clu, new_clu.dir))
213 return -EIO;
214 }
215
216 num_clusters += num_to_be_allocated;
217 *clu = new_clu.dir;
218
219 if (ei->dir.dir != DIR_DELETED && modified) {
220 struct exfat_dentry *ep;
221 struct exfat_entry_set_cache *es;
222 int err;
223
224 es = exfat_get_dentry_set(sb, &(ei->dir), ei->entry,
225 ES_ALL_ENTRIES);
226 if (!es)
227 return -EIO;
228
229 ep = exfat_get_dentry_cached(es, 1);
230
231
232 ep->dentry.stream.flags = ei->flags;
233 ep->dentry.stream.start_clu =
234 cpu_to_le32(ei->start_clu);
235 ep->dentry.stream.valid_size =
236 cpu_to_le64(i_size_read(inode));
237 ep->dentry.stream.size =
238 ep->dentry.stream.valid_size;
239
240 exfat_update_dir_chksum_with_entry_set(es);
241 err = exfat_free_dentry_set(es, inode_needs_sync(inode));
242 if (err)
243 return err;
244 }
245
246 inode->i_blocks +=
247 num_to_be_allocated << sbi->sect_per_clus_bits;
248
249
250
251
252
253
254
255
256 if (ei->flags == ALLOC_NO_FAT_CHAIN) {
257 *clu += num_to_be_allocated - 1;
258 } else {
259 while (num_to_be_allocated > 1) {
260 if (exfat_get_next_cluster(sb, clu))
261 return -EIO;
262 num_to_be_allocated--;
263 }
264 }
265
266 }
267
268
269 ei->hint_bmap.off = local_clu_offset;
270 ei->hint_bmap.clu = *clu;
271
272 return 0;
273}
274
275static int exfat_map_new_buffer(struct exfat_inode_info *ei,
276 struct buffer_head *bh, loff_t pos)
277{
278 if (buffer_delay(bh) && pos > ei->i_size_aligned)
279 return -EIO;
280 set_buffer_new(bh);
281
282
283
284
285 if (ei->i_size_ondisk > ei->i_size_aligned)
286 ei->i_size_aligned = ei->i_size_ondisk;
287 return 0;
288}
289
290static int exfat_get_block(struct inode *inode, sector_t iblock,
291 struct buffer_head *bh_result, int create)
292{
293 struct exfat_inode_info *ei = EXFAT_I(inode);
294 struct super_block *sb = inode->i_sb;
295 struct exfat_sb_info *sbi = EXFAT_SB(sb);
296 unsigned long max_blocks = bh_result->b_size >> inode->i_blkbits;
297 int err = 0;
298 unsigned long mapped_blocks = 0;
299 unsigned int cluster, sec_offset;
300 sector_t last_block;
301 sector_t phys = 0;
302 loff_t pos;
303
304 mutex_lock(&sbi->s_lock);
305 last_block = EXFAT_B_TO_BLK_ROUND_UP(i_size_read(inode), sb);
306 if (iblock >= last_block && !create)
307 goto done;
308
309
310 err = exfat_map_cluster(inode, iblock >> sbi->sect_per_clus_bits,
311 &cluster, create);
312 if (err) {
313 if (err != -ENOSPC)
314 exfat_fs_error_ratelimit(sb,
315 "failed to bmap (inode : %p iblock : %llu, err : %d)",
316 inode, (unsigned long long)iblock, err);
317 goto unlock_ret;
318 }
319
320 if (cluster == EXFAT_EOF_CLUSTER)
321 goto done;
322
323
324 sec_offset = iblock & (sbi->sect_per_clus - 1);
325
326 phys = exfat_cluster_to_sector(sbi, cluster) + sec_offset;
327 mapped_blocks = sbi->sect_per_clus - sec_offset;
328 max_blocks = min(mapped_blocks, max_blocks);
329
330
331 if (iblock < last_block)
332 create = 0;
333
334 if (create || buffer_delay(bh_result)) {
335 pos = EXFAT_BLK_TO_B((iblock + 1), sb);
336 if (ei->i_size_ondisk < pos)
337 ei->i_size_ondisk = pos;
338 }
339
340 if (create) {
341 err = exfat_map_new_buffer(ei, bh_result, pos);
342 if (err) {
343 exfat_fs_error(sb,
344 "requested for bmap out of range(pos : (%llu) > i_size_aligned(%llu)\n",
345 pos, ei->i_size_aligned);
346 goto unlock_ret;
347 }
348 }
349
350 if (buffer_delay(bh_result))
351 clear_buffer_delay(bh_result);
352 map_bh(bh_result, sb, phys);
353done:
354 bh_result->b_size = EXFAT_BLK_TO_B(max_blocks, sb);
355unlock_ret:
356 mutex_unlock(&sbi->s_lock);
357 return err;
358}
359
360static int exfat_readpage(struct file *file, struct page *page)
361{
362 return mpage_readpage(page, exfat_get_block);
363}
364
365static void exfat_readahead(struct readahead_control *rac)
366{
367 mpage_readahead(rac, exfat_get_block);
368}
369
370static int exfat_writepage(struct page *page, struct writeback_control *wbc)
371{
372 return block_write_full_page(page, exfat_get_block, wbc);
373}
374
375static int exfat_writepages(struct address_space *mapping,
376 struct writeback_control *wbc)
377{
378 return mpage_writepages(mapping, wbc, exfat_get_block);
379}
380
381static void exfat_write_failed(struct address_space *mapping, loff_t to)
382{
383 struct inode *inode = mapping->host;
384
385 if (to > i_size_read(inode)) {
386 truncate_pagecache(inode, i_size_read(inode));
387 exfat_truncate(inode, EXFAT_I(inode)->i_size_aligned);
388 }
389}
390
391static int exfat_write_begin(struct file *file, struct address_space *mapping,
392 loff_t pos, unsigned int len, unsigned int flags,
393 struct page **pagep, void **fsdata)
394{
395 int ret;
396
397 *pagep = NULL;
398 ret = cont_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
399 exfat_get_block,
400 &EXFAT_I(mapping->host)->i_size_ondisk);
401
402 if (ret < 0)
403 exfat_write_failed(mapping, pos+len);
404
405 return ret;
406}
407
408static int exfat_write_end(struct file *file, struct address_space *mapping,
409 loff_t pos, unsigned int len, unsigned int copied,
410 struct page *pagep, void *fsdata)
411{
412 struct inode *inode = mapping->host;
413 struct exfat_inode_info *ei = EXFAT_I(inode);
414 int err;
415
416 err = generic_write_end(file, mapping, pos, len, copied, pagep, fsdata);
417
418 if (EXFAT_I(inode)->i_size_aligned < i_size_read(inode)) {
419 exfat_fs_error(inode->i_sb,
420 "invalid size(size(%llu) > aligned(%llu)\n",
421 i_size_read(inode), EXFAT_I(inode)->i_size_aligned);
422 return -EIO;
423 }
424
425 if (err < len)
426 exfat_write_failed(mapping, pos+len);
427
428 if (!(err < 0) && !(ei->attr & ATTR_ARCHIVE)) {
429 inode->i_mtime = inode->i_ctime = current_time(inode);
430 ei->attr |= ATTR_ARCHIVE;
431 mark_inode_dirty(inode);
432 }
433
434 return err;
435}
436
437static ssize_t exfat_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
438{
439 struct address_space *mapping = iocb->ki_filp->f_mapping;
440 struct inode *inode = mapping->host;
441 loff_t size = iocb->ki_pos + iov_iter_count(iter);
442 int rw = iov_iter_rw(iter);
443 ssize_t ret;
444
445 if (rw == WRITE) {
446
447
448
449
450
451
452
453
454
455 if (EXFAT_I(inode)->i_size_aligned < size)
456 return 0;
457 }
458
459
460
461
462
463 ret = blockdev_direct_IO(iocb, inode, iter, exfat_get_block);
464 if (ret < 0 && (rw & WRITE))
465 exfat_write_failed(mapping, size);
466 return ret;
467}
468
469static sector_t exfat_aop_bmap(struct address_space *mapping, sector_t block)
470{
471 sector_t blocknr;
472
473
474 down_read(&EXFAT_I(mapping->host)->truncate_lock);
475 blocknr = generic_block_bmap(mapping, block, exfat_get_block);
476 up_read(&EXFAT_I(mapping->host)->truncate_lock);
477 return blocknr;
478}
479
480
481
482
483
484
485
486
487int exfat_block_truncate_page(struct inode *inode, loff_t from)
488{
489 return block_truncate_page(inode->i_mapping, from, exfat_get_block);
490}
491
492static const struct address_space_operations exfat_aops = {
493 .readpage = exfat_readpage,
494 .readahead = exfat_readahead,
495 .writepage = exfat_writepage,
496 .writepages = exfat_writepages,
497 .write_begin = exfat_write_begin,
498 .write_end = exfat_write_end,
499 .direct_IO = exfat_direct_IO,
500 .bmap = exfat_aop_bmap
501};
502
503static inline unsigned long exfat_hash(loff_t i_pos)
504{
505 return hash_32(i_pos, EXFAT_HASH_BITS);
506}
507
508void exfat_hash_inode(struct inode *inode, loff_t i_pos)
509{
510 struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb);
511 struct hlist_head *head = sbi->inode_hashtable + exfat_hash(i_pos);
512
513 spin_lock(&sbi->inode_hash_lock);
514 EXFAT_I(inode)->i_pos = i_pos;
515 hlist_add_head(&EXFAT_I(inode)->i_hash_fat, head);
516 spin_unlock(&sbi->inode_hash_lock);
517}
518
519void exfat_unhash_inode(struct inode *inode)
520{
521 struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb);
522
523 spin_lock(&sbi->inode_hash_lock);
524 hlist_del_init(&EXFAT_I(inode)->i_hash_fat);
525 EXFAT_I(inode)->i_pos = 0;
526 spin_unlock(&sbi->inode_hash_lock);
527}
528
529struct inode *exfat_iget(struct super_block *sb, loff_t i_pos)
530{
531 struct exfat_sb_info *sbi = EXFAT_SB(sb);
532 struct exfat_inode_info *info;
533 struct hlist_head *head = sbi->inode_hashtable + exfat_hash(i_pos);
534 struct inode *inode = NULL;
535
536 spin_lock(&sbi->inode_hash_lock);
537 hlist_for_each_entry(info, head, i_hash_fat) {
538 WARN_ON(info->vfs_inode.i_sb != sb);
539
540 if (i_pos != info->i_pos)
541 continue;
542 inode = igrab(&info->vfs_inode);
543 if (inode)
544 break;
545 }
546 spin_unlock(&sbi->inode_hash_lock);
547 return inode;
548}
549
550
551static int exfat_fill_inode(struct inode *inode, struct exfat_dir_entry *info)
552{
553 struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb);
554 struct exfat_inode_info *ei = EXFAT_I(inode);
555 loff_t size = info->size;
556
557 ei->dir = info->dir;
558 ei->entry = info->entry;
559 ei->attr = info->attr;
560 ei->start_clu = info->start_clu;
561 ei->flags = info->flags;
562 ei->type = info->type;
563
564 ei->version = 0;
565 ei->hint_stat.eidx = 0;
566 ei->hint_stat.clu = info->start_clu;
567 ei->hint_femp.eidx = EXFAT_HINT_NONE;
568 ei->hint_bmap.off = EXFAT_EOF_CLUSTER;
569 ei->i_pos = 0;
570
571 inode->i_uid = sbi->options.fs_uid;
572 inode->i_gid = sbi->options.fs_gid;
573 inode_inc_iversion(inode);
574 inode->i_generation = prandom_u32();
575
576 if (info->attr & ATTR_SUBDIR) {
577 inode->i_generation &= ~1;
578 inode->i_mode = exfat_make_mode(sbi, info->attr, 0777);
579 inode->i_op = &exfat_dir_inode_operations;
580 inode->i_fop = &exfat_dir_operations;
581 set_nlink(inode, info->num_subdirs);
582 } else {
583 inode->i_generation |= 1;
584 inode->i_mode = exfat_make_mode(sbi, info->attr, 0777);
585 inode->i_op = &exfat_file_inode_operations;
586 inode->i_fop = &exfat_file_operations;
587 inode->i_mapping->a_ops = &exfat_aops;
588 inode->i_mapping->nrpages = 0;
589 }
590
591 i_size_write(inode, size);
592
593
594 if (size & (inode->i_sb->s_blocksize - 1)) {
595 size |= (inode->i_sb->s_blocksize - 1);
596 size++;
597 }
598
599 ei->i_size_aligned = size;
600 ei->i_size_ondisk = size;
601
602 exfat_save_attr(inode, info->attr);
603
604 inode->i_blocks = ((i_size_read(inode) + (sbi->cluster_size - 1)) &
605 ~(sbi->cluster_size - 1)) >> inode->i_blkbits;
606 inode->i_mtime = info->mtime;
607 inode->i_ctime = info->mtime;
608 ei->i_crtime = info->crtime;
609 inode->i_atime = info->atime;
610
611 return 0;
612}
613
614struct inode *exfat_build_inode(struct super_block *sb,
615 struct exfat_dir_entry *info, loff_t i_pos)
616{
617 struct inode *inode;
618 int err;
619
620 inode = exfat_iget(sb, i_pos);
621 if (inode)
622 goto out;
623 inode = new_inode(sb);
624 if (!inode) {
625 inode = ERR_PTR(-ENOMEM);
626 goto out;
627 }
628 inode->i_ino = iunique(sb, EXFAT_ROOT_INO);
629 inode_set_iversion(inode, 1);
630 err = exfat_fill_inode(inode, info);
631 if (err) {
632 iput(inode);
633 inode = ERR_PTR(err);
634 goto out;
635 }
636 exfat_hash_inode(inode, i_pos);
637 insert_inode_hash(inode);
638out:
639 return inode;
640}
641
642void exfat_evict_inode(struct inode *inode)
643{
644 truncate_inode_pages(&inode->i_data, 0);
645
646 if (!inode->i_nlink) {
647 i_size_write(inode, 0);
648 mutex_lock(&EXFAT_SB(inode->i_sb)->s_lock);
649 __exfat_truncate(inode, 0);
650 mutex_unlock(&EXFAT_SB(inode->i_sb)->s_lock);
651 }
652
653 invalidate_inode_buffers(inode);
654 clear_inode(inode);
655 exfat_cache_inval_inode(inode);
656 exfat_unhash_inode(inode);
657}
658