]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - drivers/staging/exfat/exfat_super.c
Merge tag 'pinctrl-v5.5-3' of git://git.kernel.org/pub/scm/linux/kernel/git/linusw...
[mirror_ubuntu-jammy-kernel.git] / drivers / staging / exfat / exfat_super.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd.
4 */
5
6 #include <linux/module.h>
7 #include <linux/init.h>
8 #include <linux/time.h>
9 #include <linux/slab.h>
10 #include <linux/mm.h>
11 #include <linux/seq_file.h>
12 #include <linux/pagemap.h>
13 #include <linux/mpage.h>
14 #include <linux/buffer_head.h>
15 #include <linux/exportfs.h>
16 #include <linux/mount.h>
17 #include <linux/vfs.h>
18 #include <linux/aio.h>
19 #include <linux/iversion.h>
20 #include <linux/parser.h>
21 #include <linux/uio.h>
22 #include <linux/writeback.h>
23 #include <linux/log2.h>
24 #include <linux/hash.h>
25 #include <linux/backing-dev.h>
26 #include <linux/sched.h>
27 #include <linux/fs_struct.h>
28 #include <linux/namei.h>
29 #include <linux/random.h>
30 #include <linux/string.h>
31 #include <linux/nls.h>
32 #include <linux/mutex.h>
33 #include <linux/swap.h>
34
35 #define EXFAT_VERSION "1.3.0"
36
37 #include "exfat.h"
38
39 static struct kmem_cache *exfat_inode_cachep;
40
41 static int exfat_default_codepage = CONFIG_EXFAT_DEFAULT_CODEPAGE;
42 static char exfat_default_iocharset[] = CONFIG_EXFAT_DEFAULT_IOCHARSET;
43
44 #define INC_IVERSION(x) (inode_inc_iversion(x))
45 #define GET_IVERSION(x) (inode_peek_iversion_raw(x))
46 #define SET_IVERSION(x, y) (inode_set_iversion(x, y))
47
48 static struct inode *exfat_iget(struct super_block *sb, loff_t i_pos);
49 static int exfat_sync_inode(struct inode *inode);
50 static struct inode *exfat_build_inode(struct super_block *sb,
51 struct file_id_t *fid, loff_t i_pos);
52 static int exfat_write_inode(struct inode *inode,
53 struct writeback_control *wbc);
54 static void exfat_write_super(struct super_block *sb);
55
56 #define UNIX_SECS_1980 315532800L
57 #define UNIX_SECS_2108 4354819200L
58
59 /* Convert a FAT time/date pair to a UNIX date (seconds since 1 1 70). */
60 static void exfat_time_fat2unix(struct timespec64 *ts, struct date_time_t *tp)
61 {
62 ts->tv_sec = mktime64(tp->Year + 1980, tp->Month + 1, tp->Day,
63 tp->Hour, tp->Minute, tp->Second);
64
65 ts->tv_nsec = tp->MilliSecond * NSEC_PER_MSEC;
66 }
67
68 /* Convert linear UNIX date to a FAT time/date pair. */
69 static void exfat_time_unix2fat(struct timespec64 *ts, struct date_time_t *tp)
70 {
71 time64_t second = ts->tv_sec;
72 struct tm tm;
73
74 time64_to_tm(second, 0, &tm);
75
76 if (second < UNIX_SECS_1980) {
77 tp->MilliSecond = 0;
78 tp->Second = 0;
79 tp->Minute = 0;
80 tp->Hour = 0;
81 tp->Day = 1;
82 tp->Month = 1;
83 tp->Year = 0;
84 return;
85 }
86
87 if (second >= UNIX_SECS_2108) {
88 tp->MilliSecond = 999;
89 tp->Second = 59;
90 tp->Minute = 59;
91 tp->Hour = 23;
92 tp->Day = 31;
93 tp->Month = 12;
94 tp->Year = 127;
95 return;
96 }
97
98 tp->MilliSecond = ts->tv_nsec / NSEC_PER_MSEC;
99 tp->Second = tm.tm_sec;
100 tp->Minute = tm.tm_min;
101 tp->Hour = tm.tm_hour;
102 tp->Day = tm.tm_mday;
103 tp->Month = tm.tm_mon + 1;
104 tp->Year = tm.tm_year + 1900 - 1980;
105 }
106
107 struct timestamp_t *tm_current(struct timestamp_t *tp)
108 {
109 time64_t second = ktime_get_real_seconds();
110 struct tm tm;
111
112 time64_to_tm(second, 0, &tm);
113
114 if (second < UNIX_SECS_1980) {
115 tp->sec = 0;
116 tp->min = 0;
117 tp->hour = 0;
118 tp->day = 1;
119 tp->mon = 1;
120 tp->year = 0;
121 return tp;
122 }
123
124 if (second >= UNIX_SECS_2108) {
125 tp->sec = 59;
126 tp->min = 59;
127 tp->hour = 23;
128 tp->day = 31;
129 tp->mon = 12;
130 tp->year = 127;
131 return tp;
132 }
133
134 tp->sec = tm.tm_sec;
135 tp->min = tm.tm_min;
136 tp->hour = tm.tm_hour;
137 tp->day = tm.tm_mday;
138 tp->mon = tm.tm_mon + 1;
139 tp->year = tm.tm_year + 1900 - 1980;
140
141 return tp;
142 }
143
144 static void __lock_super(struct super_block *sb)
145 {
146 struct exfat_sb_info *sbi = EXFAT_SB(sb);
147
148 mutex_lock(&sbi->s_lock);
149 }
150
151 static void __unlock_super(struct super_block *sb)
152 {
153 struct exfat_sb_info *sbi = EXFAT_SB(sb);
154
155 mutex_unlock(&sbi->s_lock);
156 }
157
158 static int __is_sb_dirty(struct super_block *sb)
159 {
160 struct exfat_sb_info *sbi = EXFAT_SB(sb);
161
162 return sbi->s_dirt;
163 }
164
165 static void __set_sb_clean(struct super_block *sb)
166 {
167 struct exfat_sb_info *sbi = EXFAT_SB(sb);
168
169 sbi->s_dirt = 0;
170 }
171
172 static int __exfat_revalidate(struct dentry *dentry)
173 {
174 return 0;
175 }
176
177 static int exfat_revalidate(struct dentry *dentry, unsigned int flags)
178 {
179 if (flags & LOOKUP_RCU)
180 return -ECHILD;
181
182 if (dentry->d_inode)
183 return 1;
184 return __exfat_revalidate(dentry);
185 }
186
187 static int exfat_revalidate_ci(struct dentry *dentry, unsigned int flags)
188 {
189 if (flags & LOOKUP_RCU)
190 return -ECHILD;
191
192 if (dentry->d_inode)
193 return 1;
194
195 if (!flags)
196 return 0;
197
198 if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET))
199 return 0;
200
201 return __exfat_revalidate(dentry);
202 }
203
204 static unsigned int __exfat_striptail_len(unsigned int len, const char *name)
205 {
206 while (len && name[len - 1] == '.')
207 len--;
208 return len;
209 }
210
211 static unsigned int exfat_striptail_len(const struct qstr *qstr)
212 {
213 return __exfat_striptail_len(qstr->len, qstr->name);
214 }
215
216 static int exfat_d_hash(const struct dentry *dentry, struct qstr *qstr)
217 {
218 qstr->hash = full_name_hash(dentry, qstr->name,
219 exfat_striptail_len(qstr));
220 return 0;
221 }
222
223 static int exfat_d_hashi(const struct dentry *dentry, struct qstr *qstr)
224 {
225 struct super_block *sb = dentry->d_sb;
226 const unsigned char *name;
227 unsigned int len;
228 unsigned long hash;
229
230 name = qstr->name;
231 len = exfat_striptail_len(qstr);
232
233 hash = init_name_hash(dentry);
234 while (len--)
235 hash = partial_name_hash(nls_upper(sb, *name++), hash);
236 qstr->hash = end_name_hash(hash);
237
238 return 0;
239 }
240
241 static int exfat_cmpi(const struct dentry *dentry, unsigned int len,
242 const char *str, const struct qstr *name)
243 {
244 struct nls_table *t = EXFAT_SB(dentry->d_sb)->nls_io;
245 unsigned int alen, blen;
246
247 alen = exfat_striptail_len(name);
248 blen = __exfat_striptail_len(len, str);
249 if (alen == blen) {
250 if (!t) {
251 if (strncasecmp(name->name, str, alen) == 0)
252 return 0;
253 } else {
254 if (nls_strnicmp(t, name->name, str, alen) == 0)
255 return 0;
256 }
257 }
258 return 1;
259 }
260
261 static int exfat_cmp(const struct dentry *dentry, unsigned int len,
262 const char *str, const struct qstr *name)
263 {
264 unsigned int alen, blen;
265
266 alen = exfat_striptail_len(name);
267 blen = __exfat_striptail_len(len, str);
268 if (alen == blen) {
269 if (strncmp(name->name, str, alen) == 0)
270 return 0;
271 }
272 return 1;
273 }
274
275 static const struct dentry_operations exfat_ci_dentry_ops = {
276 .d_revalidate = exfat_revalidate_ci,
277 .d_hash = exfat_d_hashi,
278 .d_compare = exfat_cmpi,
279 };
280
281 static const struct dentry_operations exfat_dentry_ops = {
282 .d_revalidate = exfat_revalidate,
283 .d_hash = exfat_d_hash,
284 .d_compare = exfat_cmp,
285 };
286
287 static DEFINE_MUTEX(z_mutex);
288
289 static inline void fs_sync(struct super_block *sb, bool do_sync)
290 {
291 if (do_sync)
292 exfat_bdev_sync(sb);
293 }
294
295 /*
296 * If ->i_mode can't hold S_IWUGO (i.e. ATTR_RO), we use ->i_attrs to
297 * save ATTR_RO instead of ->i_mode.
298 *
299 * If it's directory and !sbi->options.rodir, ATTR_RO isn't read-only
300 * bit, it's just used as flag for app.
301 */
302 static inline int exfat_mode_can_hold_ro(struct inode *inode)
303 {
304 struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb);
305
306 if (S_ISDIR(inode->i_mode))
307 return 0;
308
309 if ((~sbi->options.fs_fmask) & 0222)
310 return 1;
311 return 0;
312 }
313
314 /* Convert attribute bits and a mask to the UNIX mode. */
315 static inline mode_t exfat_make_mode(struct exfat_sb_info *sbi, u32 attr,
316 mode_t mode)
317 {
318 if ((attr & ATTR_READONLY) && !(attr & ATTR_SUBDIR))
319 mode &= ~0222;
320
321 if (attr & ATTR_SUBDIR)
322 return (mode & ~sbi->options.fs_dmask) | S_IFDIR;
323 else if (attr & ATTR_SYMLINK)
324 return (mode & ~sbi->options.fs_dmask) | S_IFLNK;
325 else
326 return (mode & ~sbi->options.fs_fmask) | S_IFREG;
327 }
328
329 /* Return the FAT attribute byte for this inode */
330 static inline u32 exfat_make_attr(struct inode *inode)
331 {
332 if (exfat_mode_can_hold_ro(inode) && !(inode->i_mode & 0222))
333 return (EXFAT_I(inode)->fid.attr) | ATTR_READONLY;
334 else
335 return EXFAT_I(inode)->fid.attr;
336 }
337
338 static inline void exfat_save_attr(struct inode *inode, u32 attr)
339 {
340 if (exfat_mode_can_hold_ro(inode))
341 EXFAT_I(inode)->fid.attr = attr & ATTR_RWMASK;
342 else
343 EXFAT_I(inode)->fid.attr = attr & (ATTR_RWMASK | ATTR_READONLY);
344 }
345
346 static int ffsMountVol(struct super_block *sb)
347 {
348 int i, ret;
349 struct pbr_sector_t *p_pbr;
350 struct buffer_head *tmp_bh = NULL;
351 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
352 struct bd_info_t *p_bd = &(EXFAT_SB(sb)->bd_info);
353
354 pr_info("[EXFAT] trying to mount...\n");
355
356 mutex_lock(&z_mutex);
357
358 exfat_buf_init(sb);
359
360 mutex_init(&p_fs->v_mutex);
361 p_fs->dev_ejected = 0;
362
363 /* open the block device */
364 exfat_bdev_open(sb);
365
366 if (p_bd->sector_size < sb->s_blocksize) {
367 printk(KERN_INFO "EXFAT: mount failed - sector size %d less than blocksize %ld\n",
368 p_bd->sector_size, sb->s_blocksize);
369 ret = -EINVAL;
370 goto out;
371 }
372 if (p_bd->sector_size > sb->s_blocksize)
373 sb_set_blocksize(sb, p_bd->sector_size);
374
375 /* read Sector 0 */
376 if (sector_read(sb, 0, &tmp_bh, 1) != 0) {
377 ret = -EIO;
378 goto out;
379 }
380
381 p_fs->PBR_sector = 0;
382
383 p_pbr = (struct pbr_sector_t *)tmp_bh->b_data;
384
385 /* check the validity of PBR */
386 if (GET16_A(p_pbr->signature) != PBR_SIGNATURE) {
387 brelse(tmp_bh);
388 exfat_bdev_close(sb);
389 ret = -EFSCORRUPTED;
390 goto out;
391 }
392
393 /* fill fs_struct */
394 for (i = 0; i < 53; i++)
395 if (p_pbr->bpb[i])
396 break;
397
398 if (i < 53) {
399 /* Not sure how we'd get here, but complain if it does */
400 ret = -EINVAL;
401 pr_info("EXFAT: Attempted to mount VFAT filesystem\n");
402 goto out;
403 } else {
404 ret = exfat_mount(sb, p_pbr);
405 }
406
407 brelse(tmp_bh);
408
409 if (ret) {
410 exfat_bdev_close(sb);
411 goto out;
412 }
413
414 ret = load_alloc_bitmap(sb);
415 if (ret) {
416 exfat_bdev_close(sb);
417 goto out;
418 }
419 ret = load_upcase_table(sb);
420 if (ret) {
421 free_alloc_bitmap(sb);
422 exfat_bdev_close(sb);
423 goto out;
424 }
425
426 if (p_fs->dev_ejected) {
427 free_upcase_table(sb);
428 free_alloc_bitmap(sb);
429 exfat_bdev_close(sb);
430 ret = -EIO;
431 goto out;
432 }
433
434 pr_info("[EXFAT] mounted successfully\n");
435
436 out:
437 mutex_unlock(&z_mutex);
438
439 return ret;
440 }
441
442 static int ffsUmountVol(struct super_block *sb)
443 {
444 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
445 int err = 0;
446
447 pr_info("[EXFAT] trying to unmount...\n");
448
449 mutex_lock(&z_mutex);
450
451 /* acquire the lock for file system critical section */
452 mutex_lock(&p_fs->v_mutex);
453
454 fs_sync(sb, true);
455 fs_set_vol_flags(sb, VOL_CLEAN);
456
457 free_upcase_table(sb);
458 free_alloc_bitmap(sb);
459
460 exfat_fat_release_all(sb);
461 exfat_buf_release_all(sb);
462
463 /* close the block device */
464 exfat_bdev_close(sb);
465
466 if (p_fs->dev_ejected) {
467 pr_info("[EXFAT] unmounted with media errors. Device is already ejected.\n");
468 err = -EIO;
469 }
470
471 exfat_buf_shutdown(sb);
472
473 /* release the lock for file system critical section */
474 mutex_unlock(&p_fs->v_mutex);
475 mutex_unlock(&z_mutex);
476
477 pr_info("[EXFAT] unmounted successfully\n");
478
479 return err;
480 }
481
482 static int ffsGetVolInfo(struct super_block *sb, struct vol_info_t *info)
483 {
484 int err = 0;
485 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
486
487 /* check the validity of pointer parameters */
488 if (!info)
489 return -EINVAL;
490
491 /* acquire the lock for file system critical section */
492 mutex_lock(&p_fs->v_mutex);
493
494 if (p_fs->used_clusters == UINT_MAX)
495 p_fs->used_clusters = p_fs->fs_func->count_used_clusters(sb);
496
497 info->FatType = p_fs->vol_type;
498 info->ClusterSize = p_fs->cluster_size;
499 info->NumClusters = p_fs->num_clusters - 2; /* clu 0 & 1 */
500 info->UsedClusters = p_fs->used_clusters;
501 info->FreeClusters = info->NumClusters - info->UsedClusters;
502
503 if (p_fs->dev_ejected)
504 err = -EIO;
505
506 /* release the lock for file system critical section */
507 mutex_unlock(&p_fs->v_mutex);
508
509 return err;
510 }
511
512 static int ffsSyncVol(struct super_block *sb, bool do_sync)
513 {
514 int err = 0;
515 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
516
517 /* acquire the lock for file system critical section */
518 mutex_lock(&p_fs->v_mutex);
519
520 /* synchronize the file system */
521 fs_sync(sb, do_sync);
522 fs_set_vol_flags(sb, VOL_CLEAN);
523
524 if (p_fs->dev_ejected)
525 err = -EIO;
526
527 /* release the lock for file system critical section */
528 mutex_unlock(&p_fs->v_mutex);
529
530 return err;
531 }
532
533 /*----------------------------------------------------------------------*/
534 /* File Operation Functions */
535 /*----------------------------------------------------------------------*/
536
537 static int ffsLookupFile(struct inode *inode, char *path, struct file_id_t *fid)
538 {
539 int ret, dentry, num_entries;
540 struct chain_t dir;
541 struct uni_name_t uni_name;
542 struct dos_name_t dos_name;
543 struct dentry_t *ep, *ep2;
544 struct entry_set_cache_t *es = NULL;
545 struct super_block *sb = inode->i_sb;
546 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
547
548 pr_debug("%s entered\n", __func__);
549
550 /* check the validity of pointer parameters */
551 if (!fid || !path || (*path == '\0'))
552 return -EINVAL;
553
554 /* acquire the lock for file system critical section */
555 mutex_lock(&p_fs->v_mutex);
556
557 /* check the validity of directory name in the given pathname */
558 ret = resolve_path(inode, path, &dir, &uni_name);
559 if (ret)
560 goto out;
561
562 ret = get_num_entries_and_dos_name(sb, &dir, &uni_name, &num_entries,
563 &dos_name);
564 if (ret)
565 goto out;
566
567 /* search the file name for directories */
568 dentry = p_fs->fs_func->find_dir_entry(sb, &dir, &uni_name, num_entries,
569 &dos_name, TYPE_ALL);
570 if (dentry < -1) {
571 ret = -ENOENT;
572 goto out;
573 }
574
575 fid->dir.dir = dir.dir;
576 fid->dir.size = dir.size;
577 fid->dir.flags = dir.flags;
578 fid->entry = dentry;
579
580 if (dentry == -1) {
581 fid->type = TYPE_DIR;
582 fid->rwoffset = 0;
583 fid->hint_last_off = -1;
584
585 fid->attr = ATTR_SUBDIR;
586 fid->flags = 0x01;
587 fid->size = 0;
588 fid->start_clu = p_fs->root_dir;
589 } else {
590 es = get_entry_set_in_dir(sb, &dir, dentry,
591 ES_2_ENTRIES, &ep);
592 if (!es) {
593 ret = -ENOENT;
594 goto out;
595 }
596 ep2 = ep + 1;
597
598 fid->type = p_fs->fs_func->get_entry_type(ep);
599 fid->rwoffset = 0;
600 fid->hint_last_off = -1;
601 fid->attr = p_fs->fs_func->get_entry_attr(ep);
602
603 fid->size = p_fs->fs_func->get_entry_size(ep2);
604 if ((fid->type == TYPE_FILE) && (fid->size == 0)) {
605 fid->flags = (p_fs->vol_type == EXFAT) ? 0x03 : 0x01;
606 fid->start_clu = CLUSTER_32(~0);
607 } else {
608 fid->flags = p_fs->fs_func->get_entry_flag(ep2);
609 fid->start_clu = p_fs->fs_func->get_entry_clu0(ep2);
610 }
611
612 release_entry_set(es);
613 }
614
615 if (p_fs->dev_ejected)
616 ret = -EIO;
617 out:
618 /* release the lock for file system critical section */
619 mutex_unlock(&p_fs->v_mutex);
620
621 return ret;
622 }
623
624 static int ffsCreateFile(struct inode *inode, char *path, u8 mode,
625 struct file_id_t *fid)
626 {
627 struct chain_t dir;
628 struct uni_name_t uni_name;
629 struct super_block *sb = inode->i_sb;
630 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
631 int ret = 0;
632
633 /* check the validity of pointer parameters */
634 if (!fid || !path || (*path == '\0'))
635 return -EINVAL;
636
637 /* acquire the lock for file system critical section */
638 mutex_lock(&p_fs->v_mutex);
639
640 /* check the validity of directory name in the given pathname */
641 ret = resolve_path(inode, path, &dir, &uni_name);
642 if (ret)
643 goto out;
644
645 fs_set_vol_flags(sb, VOL_DIRTY);
646
647 /* create a new file */
648 ret = create_file(inode, &dir, &uni_name, mode, fid);
649
650 #ifndef CONFIG_EXFAT_DELAYED_SYNC
651 fs_sync(sb, true);
652 fs_set_vol_flags(sb, VOL_CLEAN);
653 #endif
654
655 if (p_fs->dev_ejected)
656 ret = -EIO;
657
658 out:
659 /* release the lock for file system critical section */
660 mutex_unlock(&p_fs->v_mutex);
661
662 return ret;
663 }
664
665 static int ffsReadFile(struct inode *inode, struct file_id_t *fid, void *buffer,
666 u64 count, u64 *rcount)
667 {
668 s32 offset, sec_offset, clu_offset;
669 u32 clu;
670 int ret = 0;
671 sector_t LogSector;
672 u64 oneblkread, read_bytes;
673 struct buffer_head *tmp_bh = NULL;
674 struct super_block *sb = inode->i_sb;
675 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
676 struct bd_info_t *p_bd = &(EXFAT_SB(sb)->bd_info);
677
678 /* check the validity of the given file id */
679 if (!fid)
680 return -EINVAL;
681
682 /* check the validity of pointer parameters */
683 if (!buffer)
684 return -EINVAL;
685
686 /* acquire the lock for file system critical section */
687 mutex_lock(&p_fs->v_mutex);
688
689 /* check if the given file ID is opened */
690 if (fid->type != TYPE_FILE) {
691 ret = -EPERM;
692 goto out;
693 }
694
695 if (fid->rwoffset > fid->size)
696 fid->rwoffset = fid->size;
697
698 if (count > (fid->size - fid->rwoffset))
699 count = fid->size - fid->rwoffset;
700
701 if (count == 0) {
702 if (rcount)
703 *rcount = 0;
704 ret = 0;
705 goto out;
706 }
707
708 read_bytes = 0;
709
710 while (count > 0) {
711 clu_offset = (s32)(fid->rwoffset >> p_fs->cluster_size_bits);
712 clu = fid->start_clu;
713
714 if (fid->flags == 0x03) {
715 clu += clu_offset;
716 } else {
717 /* hint information */
718 if ((clu_offset > 0) && (fid->hint_last_off > 0) &&
719 (clu_offset >= fid->hint_last_off)) {
720 clu_offset -= fid->hint_last_off;
721 clu = fid->hint_last_clu;
722 }
723
724 while (clu_offset > 0) {
725 /* clu = exfat_fat_read(sb, clu); */
726 if (exfat_fat_read(sb, clu, &clu) == -1) {
727 ret = -EIO;
728 goto out;
729 }
730
731 clu_offset--;
732 }
733 }
734
735 /* hint information */
736 fid->hint_last_off = (s32)(fid->rwoffset >>
737 p_fs->cluster_size_bits);
738 fid->hint_last_clu = clu;
739
740 /* byte offset in cluster */
741 offset = (s32)(fid->rwoffset & (p_fs->cluster_size - 1));
742
743 /* sector offset in cluster */
744 sec_offset = offset >> p_bd->sector_size_bits;
745
746 /* byte offset in sector */
747 offset &= p_bd->sector_size_mask;
748
749 LogSector = START_SECTOR(clu) + sec_offset;
750
751 oneblkread = (u64)(p_bd->sector_size - offset);
752 if (oneblkread > count)
753 oneblkread = count;
754
755 if ((offset == 0) && (oneblkread == p_bd->sector_size)) {
756 if (sector_read(sb, LogSector, &tmp_bh, 1) !=
757 0)
758 goto err_out;
759 memcpy((char *)buffer + read_bytes,
760 (char *)tmp_bh->b_data, (s32)oneblkread);
761 } else {
762 if (sector_read(sb, LogSector, &tmp_bh, 1) !=
763 0)
764 goto err_out;
765 memcpy((char *)buffer + read_bytes,
766 (char *)tmp_bh->b_data + offset,
767 (s32)oneblkread);
768 }
769 count -= oneblkread;
770 read_bytes += oneblkread;
771 fid->rwoffset += oneblkread;
772 }
773 brelse(tmp_bh);
774
775 /* How did this ever work and not leak a brlse()?? */
776 err_out:
777 /* set the size of read bytes */
778 if (rcount)
779 *rcount = read_bytes;
780
781 if (p_fs->dev_ejected)
782 ret = -EIO;
783
784 out:
785 /* release the lock for file system critical section */
786 mutex_unlock(&p_fs->v_mutex);
787
788 return ret;
789 }
790
791 static int ffsWriteFile(struct inode *inode, struct file_id_t *fid,
792 void *buffer, u64 count, u64 *wcount)
793 {
794 bool modified = false;
795 s32 offset, sec_offset, clu_offset;
796 s32 num_clusters, num_alloc, num_alloced = (s32)~0;
797 int ret = 0;
798 u32 clu, last_clu;
799 sector_t LogSector;
800 u64 oneblkwrite, write_bytes;
801 struct chain_t new_clu;
802 struct timestamp_t tm;
803 struct dentry_t *ep, *ep2;
804 struct entry_set_cache_t *es = NULL;
805 struct buffer_head *tmp_bh = NULL;
806 struct super_block *sb = inode->i_sb;
807 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
808 struct bd_info_t *p_bd = &(EXFAT_SB(sb)->bd_info);
809
810 /* check the validity of the given file id */
811 if (!fid)
812 return -EINVAL;
813
814 /* check the validity of pointer parameters */
815 if (!buffer)
816 return -EINVAL;
817
818 /* acquire the lock for file system critical section */
819 mutex_lock(&p_fs->v_mutex);
820
821 /* check if the given file ID is opened */
822 if (fid->type != TYPE_FILE) {
823 ret = -EPERM;
824 goto out;
825 }
826
827 if (fid->rwoffset > fid->size)
828 fid->rwoffset = fid->size;
829
830 if (count == 0) {
831 if (wcount)
832 *wcount = 0;
833 ret = 0;
834 goto out;
835 }
836
837 fs_set_vol_flags(sb, VOL_DIRTY);
838
839 if (fid->size == 0)
840 num_clusters = 0;
841 else
842 num_clusters = (s32)((fid->size - 1) >>
843 p_fs->cluster_size_bits) + 1;
844
845 write_bytes = 0;
846
847 while (count > 0) {
848 clu_offset = (s32)(fid->rwoffset >> p_fs->cluster_size_bits);
849 clu = fid->start_clu;
850 last_clu = fid->start_clu;
851
852 if (fid->flags == 0x03) {
853 if ((clu_offset > 0) && (clu != CLUSTER_32(~0))) {
854 last_clu += clu_offset - 1;
855
856 if (clu_offset == num_clusters)
857 clu = CLUSTER_32(~0);
858 else
859 clu += clu_offset;
860 }
861 } else {
862 /* hint information */
863 if ((clu_offset > 0) && (fid->hint_last_off > 0) &&
864 (clu_offset >= fid->hint_last_off)) {
865 clu_offset -= fid->hint_last_off;
866 clu = fid->hint_last_clu;
867 }
868
869 while ((clu_offset > 0) && (clu != CLUSTER_32(~0))) {
870 last_clu = clu;
871 /* clu = exfat_fat_read(sb, clu); */
872 if (exfat_fat_read(sb, clu, &clu) == -1) {
873 ret = -EIO;
874 goto out;
875 }
876 clu_offset--;
877 }
878 }
879
880 if (clu == CLUSTER_32(~0)) {
881 num_alloc = (s32)((count - 1) >>
882 p_fs->cluster_size_bits) + 1;
883 new_clu.dir = (last_clu == CLUSTER_32(~0)) ?
884 CLUSTER_32(~0) : last_clu + 1;
885 new_clu.size = 0;
886 new_clu.flags = fid->flags;
887
888 /* (1) allocate a chain of clusters */
889 num_alloced = p_fs->fs_func->alloc_cluster(sb,
890 num_alloc,
891 &new_clu);
892 if (num_alloced == 0)
893 break;
894 if (num_alloced < 0) {
895 ret = num_alloced;
896 goto out;
897 }
898
899 /* (2) append to the FAT chain */
900 if (last_clu == CLUSTER_32(~0)) {
901 if (new_clu.flags == 0x01)
902 fid->flags = 0x01;
903 fid->start_clu = new_clu.dir;
904 modified = true;
905 } else {
906 if (new_clu.flags != fid->flags) {
907 exfat_chain_cont_cluster(sb,
908 fid->start_clu,
909 num_clusters);
910 fid->flags = 0x01;
911 modified = true;
912 }
913 if (new_clu.flags == 0x01)
914 exfat_fat_write(sb, last_clu, new_clu.dir);
915 }
916
917 num_clusters += num_alloced;
918 clu = new_clu.dir;
919 }
920
921 /* hint information */
922 fid->hint_last_off = (s32)(fid->rwoffset >>
923 p_fs->cluster_size_bits);
924 fid->hint_last_clu = clu;
925
926 /* byte offset in cluster */
927 offset = (s32)(fid->rwoffset & (p_fs->cluster_size - 1));
928
929 /* sector offset in cluster */
930 sec_offset = offset >> p_bd->sector_size_bits;
931
932 /* byte offset in sector */
933 offset &= p_bd->sector_size_mask;
934
935 LogSector = START_SECTOR(clu) + sec_offset;
936
937 oneblkwrite = (u64)(p_bd->sector_size - offset);
938 if (oneblkwrite > count)
939 oneblkwrite = count;
940
941 if ((offset == 0) && (oneblkwrite == p_bd->sector_size)) {
942 if (sector_read(sb, LogSector, &tmp_bh, 0) !=
943 0)
944 goto err_out;
945 memcpy((char *)tmp_bh->b_data,
946 (char *)buffer + write_bytes, (s32)oneblkwrite);
947 if (sector_write(sb, LogSector, tmp_bh, 0) !=
948 0) {
949 brelse(tmp_bh);
950 goto err_out;
951 }
952 } else {
953 if ((offset > 0) ||
954 ((fid->rwoffset + oneblkwrite) < fid->size)) {
955 if (sector_read(sb, LogSector, &tmp_bh, 1) !=
956 0)
957 goto err_out;
958 } else {
959 if (sector_read(sb, LogSector, &tmp_bh, 0) !=
960 0)
961 goto err_out;
962 }
963
964 memcpy((char *)tmp_bh->b_data + offset,
965 (char *)buffer + write_bytes, (s32)oneblkwrite);
966 if (sector_write(sb, LogSector, tmp_bh, 0) !=
967 0) {
968 brelse(tmp_bh);
969 goto err_out;
970 }
971 }
972
973 count -= oneblkwrite;
974 write_bytes += oneblkwrite;
975 fid->rwoffset += oneblkwrite;
976
977 fid->attr |= ATTR_ARCHIVE;
978
979 if (fid->size < fid->rwoffset) {
980 fid->size = fid->rwoffset;
981 modified = true;
982 }
983 }
984
985 brelse(tmp_bh);
986
987 /* (3) update the direcoty entry */
988 es = get_entry_set_in_dir(sb, &fid->dir, fid->entry,
989 ES_ALL_ENTRIES, &ep);
990 if (!es)
991 goto err_out;
992 ep2 = ep + 1;
993
994 p_fs->fs_func->set_entry_time(ep, tm_current(&tm), TM_MODIFY);
995 p_fs->fs_func->set_entry_attr(ep, fid->attr);
996
997 if (modified) {
998 if (p_fs->fs_func->get_entry_flag(ep2) != fid->flags)
999 p_fs->fs_func->set_entry_flag(ep2, fid->flags);
1000
1001 if (p_fs->fs_func->get_entry_size(ep2) != fid->size)
1002 p_fs->fs_func->set_entry_size(ep2, fid->size);
1003
1004 if (p_fs->fs_func->get_entry_clu0(ep2) != fid->start_clu)
1005 p_fs->fs_func->set_entry_clu0(ep2, fid->start_clu);
1006 }
1007
1008 update_dir_checksum_with_entry_set(sb, es);
1009 release_entry_set(es);
1010
1011 #ifndef CONFIG_EXFAT_DELAYED_SYNC
1012 fs_sync(sb, true);
1013 fs_set_vol_flags(sb, VOL_CLEAN);
1014 #endif
1015
1016 err_out:
1017 /* set the size of written bytes */
1018 if (wcount)
1019 *wcount = write_bytes;
1020
1021 if (num_alloced == 0)
1022 ret = -ENOSPC;
1023
1024 else if (p_fs->dev_ejected)
1025 ret = -EIO;
1026
1027 out:
1028 /* release the lock for file system critical section */
1029 mutex_unlock(&p_fs->v_mutex);
1030
1031 return ret;
1032 }
1033
1034 static int ffsTruncateFile(struct inode *inode, u64 old_size, u64 new_size)
1035 {
1036 s32 num_clusters;
1037 u32 last_clu = CLUSTER_32(0);
1038 int ret = 0;
1039 struct chain_t clu;
1040 struct timestamp_t tm;
1041 struct dentry_t *ep, *ep2;
1042 struct super_block *sb = inode->i_sb;
1043 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
1044 struct file_id_t *fid = &(EXFAT_I(inode)->fid);
1045 struct entry_set_cache_t *es = NULL;
1046
1047 pr_debug("%s entered (inode %p size %llu)\n", __func__, inode,
1048 new_size);
1049
1050 /* acquire the lock for file system critical section */
1051 mutex_lock(&p_fs->v_mutex);
1052
1053 /* check if the given file ID is opened */
1054 if (fid->type != TYPE_FILE) {
1055 ret = -EPERM;
1056 goto out;
1057 }
1058
1059 if (fid->size != old_size) {
1060 pr_err("[EXFAT] truncate : can't skip it because of size-mismatch(old:%lld->fid:%lld).\n",
1061 old_size, fid->size);
1062 }
1063
1064 if (old_size <= new_size) {
1065 ret = 0;
1066 goto out;
1067 }
1068
1069 fs_set_vol_flags(sb, VOL_DIRTY);
1070
1071 clu.dir = fid->start_clu;
1072 clu.size = (s32)((old_size - 1) >> p_fs->cluster_size_bits) + 1;
1073 clu.flags = fid->flags;
1074
1075 if (new_size > 0) {
1076 num_clusters = (s32)((new_size - 1) >>
1077 p_fs->cluster_size_bits) + 1;
1078
1079 if (clu.flags == 0x03) {
1080 clu.dir += num_clusters;
1081 } else {
1082 while (num_clusters > 0) {
1083 last_clu = clu.dir;
1084 if (exfat_fat_read(sb, clu.dir, &clu.dir) == -1) {
1085 ret = -EIO;
1086 goto out;
1087 }
1088 num_clusters--;
1089 }
1090 }
1091
1092 clu.size -= num_clusters;
1093 }
1094
1095 fid->size = new_size;
1096 fid->attr |= ATTR_ARCHIVE;
1097 if (new_size == 0) {
1098 fid->flags = (p_fs->vol_type == EXFAT) ? 0x03 : 0x01;
1099 fid->start_clu = CLUSTER_32(~0);
1100 }
1101
1102 /* (1) update the directory entry */
1103 es = get_entry_set_in_dir(sb, &fid->dir, fid->entry,
1104 ES_ALL_ENTRIES, &ep);
1105 if (!es) {
1106 ret = -ENOENT;
1107 goto out;
1108 }
1109 ep2 = ep + 1;
1110
1111 p_fs->fs_func->set_entry_time(ep, tm_current(&tm), TM_MODIFY);
1112 p_fs->fs_func->set_entry_attr(ep, fid->attr);
1113
1114 p_fs->fs_func->set_entry_size(ep2, new_size);
1115 if (new_size == 0) {
1116 p_fs->fs_func->set_entry_flag(ep2, 0x01);
1117 p_fs->fs_func->set_entry_clu0(ep2, CLUSTER_32(0));
1118 }
1119
1120 update_dir_checksum_with_entry_set(sb, es);
1121 release_entry_set(es);
1122
1123 /* (2) cut off from the FAT chain */
1124 if (last_clu != CLUSTER_32(0)) {
1125 if (fid->flags == 0x01)
1126 exfat_fat_write(sb, last_clu, CLUSTER_32(~0));
1127 }
1128
1129 /* (3) free the clusters */
1130 p_fs->fs_func->free_cluster(sb, &clu, 0);
1131
1132 /* hint information */
1133 fid->hint_last_off = -1;
1134 if (fid->rwoffset > fid->size)
1135 fid->rwoffset = fid->size;
1136
1137 #ifndef CONFIG_EXFAT_DELAYED_SYNC
1138 fs_sync(sb, true);
1139 fs_set_vol_flags(sb, VOL_CLEAN);
1140 #endif
1141
1142 if (p_fs->dev_ejected)
1143 ret = -EIO;
1144
1145 out:
1146 pr_debug("%s exited (%d)\n", __func__, ret);
1147 /* release the lock for file system critical section */
1148 mutex_unlock(&p_fs->v_mutex);
1149
1150 return ret;
1151 }
1152
1153 static void update_parent_info(struct file_id_t *fid,
1154 struct inode *parent_inode)
1155 {
1156 struct fs_info_t *p_fs = &(EXFAT_SB(parent_inode->i_sb)->fs_info);
1157 struct file_id_t *parent_fid = &(EXFAT_I(parent_inode)->fid);
1158
1159 if (unlikely((parent_fid->flags != fid->dir.flags) ||
1160 (parent_fid->size !=
1161 (fid->dir.size << p_fs->cluster_size_bits)) ||
1162 (parent_fid->start_clu != fid->dir.dir))) {
1163 fid->dir.dir = parent_fid->start_clu;
1164 fid->dir.flags = parent_fid->flags;
1165 fid->dir.size = ((parent_fid->size + (p_fs->cluster_size - 1))
1166 >> p_fs->cluster_size_bits);
1167 }
1168 }
1169
1170 static int ffsMoveFile(struct inode *old_parent_inode, struct file_id_t *fid,
1171 struct inode *new_parent_inode, struct dentry *new_dentry)
1172 {
1173 s32 ret;
1174 s32 dentry;
1175 struct chain_t olddir, newdir;
1176 struct chain_t *p_dir = NULL;
1177 struct uni_name_t uni_name;
1178 struct dentry_t *ep;
1179 struct super_block *sb = old_parent_inode->i_sb;
1180 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
1181 u8 *new_path = (u8 *)new_dentry->d_name.name;
1182 struct inode *new_inode = new_dentry->d_inode;
1183 int num_entries;
1184 struct file_id_t *new_fid = NULL;
1185 s32 new_entry = 0;
1186
1187 /* check the validity of the given file id */
1188 if (!fid)
1189 return -EINVAL;
1190
1191 /* check the validity of pointer parameters */
1192 if (!new_path || (*new_path == '\0'))
1193 return -EINVAL;
1194
1195 /* acquire the lock for file system critical section */
1196 mutex_lock(&p_fs->v_mutex);
1197
1198 update_parent_info(fid, old_parent_inode);
1199
1200 olddir.dir = fid->dir.dir;
1201 olddir.size = fid->dir.size;
1202 olddir.flags = fid->dir.flags;
1203
1204 dentry = fid->entry;
1205
1206 /* check if the old file is "." or ".." */
1207 if (p_fs->vol_type != EXFAT) {
1208 if ((olddir.dir != p_fs->root_dir) && (dentry < 2)) {
1209 ret = -EPERM;
1210 goto out2;
1211 }
1212 }
1213
1214 ep = get_entry_in_dir(sb, &olddir, dentry, NULL);
1215 if (!ep) {
1216 ret = -ENOENT;
1217 goto out2;
1218 }
1219
1220 if (p_fs->fs_func->get_entry_attr(ep) & ATTR_READONLY) {
1221 ret = -EPERM;
1222 goto out2;
1223 }
1224
1225 /* check whether new dir is existing directory and empty */
1226 if (new_inode) {
1227 u32 entry_type;
1228
1229 ret = -ENOENT;
1230 new_fid = &EXFAT_I(new_inode)->fid;
1231
1232 update_parent_info(new_fid, new_parent_inode);
1233
1234 p_dir = &new_fid->dir;
1235 new_entry = new_fid->entry;
1236 ep = get_entry_in_dir(sb, p_dir, new_entry, NULL);
1237 if (!ep)
1238 goto out;
1239
1240 entry_type = p_fs->fs_func->get_entry_type(ep);
1241
1242 if (entry_type == TYPE_DIR) {
1243 struct chain_t new_clu;
1244
1245 new_clu.dir = new_fid->start_clu;
1246 new_clu.size = (s32)((new_fid->size - 1) >>
1247 p_fs->cluster_size_bits) + 1;
1248 new_clu.flags = new_fid->flags;
1249
1250 if (!is_dir_empty(sb, &new_clu)) {
1251 ret = -EEXIST;
1252 goto out;
1253 }
1254 }
1255 }
1256
1257 /* check the validity of directory name in the given new pathname */
1258 ret = resolve_path(new_parent_inode, new_path, &newdir, &uni_name);
1259 if (ret)
1260 goto out2;
1261
1262 fs_set_vol_flags(sb, VOL_DIRTY);
1263
1264 if (olddir.dir == newdir.dir)
1265 ret = exfat_rename_file(new_parent_inode, &olddir, dentry,
1266 &uni_name, fid);
1267 else
1268 ret = move_file(new_parent_inode, &olddir, dentry, &newdir,
1269 &uni_name, fid);
1270
1271 if ((ret == 0) && new_inode) {
1272 /* delete entries of new_dir */
1273 ep = get_entry_in_dir(sb, p_dir, new_entry, NULL);
1274 if (!ep)
1275 goto out;
1276
1277 num_entries = p_fs->fs_func->count_ext_entries(sb, p_dir,
1278 new_entry, ep);
1279 if (num_entries < 0)
1280 goto out;
1281 p_fs->fs_func->delete_dir_entry(sb, p_dir, new_entry, 0,
1282 num_entries + 1);
1283 }
1284 out:
1285 #ifndef CONFIG_EXFAT_DELAYED_SYNC
1286 fs_sync(sb, true);
1287 fs_set_vol_flags(sb, VOL_CLEAN);
1288 #endif
1289
1290 if (p_fs->dev_ejected)
1291 ret = -EIO;
1292 out2:
1293 /* release the lock for file system critical section */
1294 mutex_unlock(&p_fs->v_mutex);
1295
1296 return ret;
1297 }
1298
1299 static int ffsRemoveFile(struct inode *inode, struct file_id_t *fid)
1300 {
1301 s32 dentry;
1302 int ret = 0;
1303 struct chain_t dir, clu_to_free;
1304 struct dentry_t *ep;
1305 struct super_block *sb = inode->i_sb;
1306 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
1307
1308 /* check the validity of the given file id */
1309 if (!fid)
1310 return -EINVAL;
1311
1312 /* acquire the lock for file system critical section */
1313 mutex_lock(&p_fs->v_mutex);
1314
1315 dir.dir = fid->dir.dir;
1316 dir.size = fid->dir.size;
1317 dir.flags = fid->dir.flags;
1318
1319 dentry = fid->entry;
1320
1321 ep = get_entry_in_dir(sb, &dir, dentry, NULL);
1322 if (!ep) {
1323 ret = -ENOENT;
1324 goto out;
1325 }
1326
1327 if (p_fs->fs_func->get_entry_attr(ep) & ATTR_READONLY) {
1328 ret = -EPERM;
1329 goto out;
1330 }
1331 fs_set_vol_flags(sb, VOL_DIRTY);
1332
1333 /* (1) update the directory entry */
1334 remove_file(inode, &dir, dentry);
1335
1336 clu_to_free.dir = fid->start_clu;
1337 clu_to_free.size = (s32)((fid->size - 1) >> p_fs->cluster_size_bits) + 1;
1338 clu_to_free.flags = fid->flags;
1339
1340 /* (2) free the clusters */
1341 p_fs->fs_func->free_cluster(sb, &clu_to_free, 0);
1342
1343 fid->size = 0;
1344 fid->start_clu = CLUSTER_32(~0);
1345 fid->flags = (p_fs->vol_type == EXFAT) ? 0x03 : 0x01;
1346
1347 #ifndef CONFIG_EXFAT_DELAYED_SYNC
1348 fs_sync(sb, true);
1349 fs_set_vol_flags(sb, VOL_CLEAN);
1350 #endif
1351
1352 if (p_fs->dev_ejected)
1353 ret = -EIO;
1354 out:
1355 /* release the lock for file system critical section */
1356 mutex_unlock(&p_fs->v_mutex);
1357
1358 return ret;
1359 }
1360
1361 #if 0
1362 /* Not currently wired up */
1363 static int ffsSetAttr(struct inode *inode, u32 attr)
1364 {
1365 u32 type;
1366 int ret = 0;
1367 sector_t sector = 0;
1368 struct dentry_t *ep;
1369 struct super_block *sb = inode->i_sb;
1370 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
1371 struct file_id_t *fid = &(EXFAT_I(inode)->fid);
1372 u8 is_dir = (fid->type == TYPE_DIR) ? 1 : 0;
1373 struct entry_set_cache_t *es = NULL;
1374
1375 if (fid->attr == attr) {
1376 if (p_fs->dev_ejected)
1377 return -EIO;
1378 return 0;
1379 }
1380
1381 if (is_dir) {
1382 if ((fid->dir.dir == p_fs->root_dir) &&
1383 (fid->entry == -1)) {
1384 if (p_fs->dev_ejected)
1385 return -EIO;
1386 return 0;
1387 }
1388 }
1389
1390 /* acquire the lock for file system critical section */
1391 mutex_lock(&p_fs->v_mutex);
1392
1393 /* get the directory entry of given file */
1394 es = get_entry_set_in_dir(sb, &fid->dir, fid->entry,
1395 ES_ALL_ENTRIES, &ep);
1396 if (!es) {
1397 ret = -ENOENT;
1398 goto out;
1399 }
1400
1401 type = p_fs->fs_func->get_entry_type(ep);
1402
1403 if (((type == TYPE_FILE) && (attr & ATTR_SUBDIR)) ||
1404 ((type == TYPE_DIR) && (!(attr & ATTR_SUBDIR)))) {
1405 if (p_fs->dev_ejected)
1406 ret = -EIO;
1407 else
1408 ret = -EINVAL;
1409
1410 release_entry_set(es);
1411 goto out;
1412 }
1413
1414 fs_set_vol_flags(sb, VOL_DIRTY);
1415
1416 /* set the file attribute */
1417 fid->attr = attr;
1418 p_fs->fs_func->set_entry_attr(ep, attr);
1419
1420 update_dir_checksum_with_entry_set(sb, es);
1421 release_entry_set(es);
1422
1423 #ifndef CONFIG_EXFAT_DELAYED_SYNC
1424 fs_sync(sb, true);
1425 fs_set_vol_flags(sb, VOL_CLEAN);
1426 #endif
1427
1428 if (p_fs->dev_ejected)
1429 ret = -EIO;
1430 out:
1431 /* release the lock for file system critical section */
1432 mutex_unlock(&p_fs->v_mutex);
1433
1434 return ret;
1435 }
1436 #endif
1437
1438 static int ffsReadStat(struct inode *inode, struct dir_entry_t *info)
1439 {
1440 s32 count;
1441 int ret = 0;
1442 struct chain_t dir;
1443 struct uni_name_t uni_name;
1444 struct timestamp_t tm;
1445 struct dentry_t *ep, *ep2;
1446 struct super_block *sb = inode->i_sb;
1447 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
1448 struct file_id_t *fid = &(EXFAT_I(inode)->fid);
1449 struct entry_set_cache_t *es = NULL;
1450 u8 is_dir = (fid->type == TYPE_DIR) ? 1 : 0;
1451
1452 pr_debug("%s entered\n", __func__);
1453
1454 /* acquire the lock for file system critical section */
1455 mutex_lock(&p_fs->v_mutex);
1456
1457 if (is_dir) {
1458 if ((fid->dir.dir == p_fs->root_dir) &&
1459 (fid->entry == -1)) {
1460 info->Attr = ATTR_SUBDIR;
1461 memset((char *)&info->CreateTimestamp, 0,
1462 sizeof(struct date_time_t));
1463 memset((char *)&info->ModifyTimestamp, 0,
1464 sizeof(struct date_time_t));
1465 memset((char *)&info->AccessTimestamp, 0,
1466 sizeof(struct date_time_t));
1467 strcpy(info->ShortName, ".");
1468 strcpy(info->Name, ".");
1469
1470 dir.dir = p_fs->root_dir;
1471 dir.flags = 0x01;
1472
1473 if (p_fs->root_dir == CLUSTER_32(0)) {
1474 /* FAT16 root_dir */
1475 info->Size = p_fs->dentries_in_root <<
1476 DENTRY_SIZE_BITS;
1477 } else {
1478 info->Size = count_num_clusters(sb, &dir) <<
1479 p_fs->cluster_size_bits;
1480 }
1481
1482 count = count_dos_name_entries(sb, &dir, TYPE_DIR);
1483 if (count < 0) {
1484 ret = count; /* propogate error upward */
1485 goto out;
1486 }
1487 info->NumSubdirs = count;
1488
1489 if (p_fs->dev_ejected)
1490 ret = -EIO;
1491 goto out;
1492 }
1493 }
1494
1495 /* get the directory entry of given file or directory */
1496 es = get_entry_set_in_dir(sb, &fid->dir, fid->entry,
1497 ES_2_ENTRIES, &ep);
1498 if (!es) {
1499 ret = -ENOENT;
1500 goto out;
1501 }
1502 ep2 = ep + 1;
1503
1504 /* set FILE_INFO structure using the acquired struct dentry_t */
1505 info->Attr = p_fs->fs_func->get_entry_attr(ep);
1506
1507 p_fs->fs_func->get_entry_time(ep, &tm, TM_CREATE);
1508 info->CreateTimestamp.Year = tm.year;
1509 info->CreateTimestamp.Month = tm.mon;
1510 info->CreateTimestamp.Day = tm.day;
1511 info->CreateTimestamp.Hour = tm.hour;
1512 info->CreateTimestamp.Minute = tm.min;
1513 info->CreateTimestamp.Second = tm.sec;
1514 info->CreateTimestamp.MilliSecond = 0;
1515
1516 p_fs->fs_func->get_entry_time(ep, &tm, TM_MODIFY);
1517 info->ModifyTimestamp.Year = tm.year;
1518 info->ModifyTimestamp.Month = tm.mon;
1519 info->ModifyTimestamp.Day = tm.day;
1520 info->ModifyTimestamp.Hour = tm.hour;
1521 info->ModifyTimestamp.Minute = tm.min;
1522 info->ModifyTimestamp.Second = tm.sec;
1523 info->ModifyTimestamp.MilliSecond = 0;
1524
1525 memset((char *)&info->AccessTimestamp, 0, sizeof(struct date_time_t));
1526
1527 *uni_name.name = 0x0;
1528 /* XXX this is very bad for exfat cuz name is already included in es.
1529 * API should be revised
1530 */
1531 p_fs->fs_func->get_uni_name_from_ext_entry(sb, &fid->dir, fid->entry,
1532 uni_name.name);
1533 nls_uniname_to_cstring(sb, info->Name, &uni_name);
1534
1535 info->NumSubdirs = 2;
1536
1537 info->Size = p_fs->fs_func->get_entry_size(ep2);
1538
1539 release_entry_set(es);
1540
1541 if (is_dir) {
1542 dir.dir = fid->start_clu;
1543 dir.flags = 0x01;
1544
1545 if (info->Size == 0)
1546 info->Size = (u64)count_num_clusters(sb, &dir) <<
1547 p_fs->cluster_size_bits;
1548
1549 count = count_dos_name_entries(sb, &dir, TYPE_DIR);
1550 if (count < 0) {
1551 ret = count; /* propogate error upward */
1552 goto out;
1553 }
1554 info->NumSubdirs += count;
1555 }
1556
1557 if (p_fs->dev_ejected)
1558 ret = -EIO;
1559
1560 out:
1561 /* release the lock for file system critical section */
1562 mutex_unlock(&p_fs->v_mutex);
1563
1564 pr_debug("%s exited successfully\n", __func__);
1565 return ret;
1566 }
1567
1568 static int ffsWriteStat(struct inode *inode, struct dir_entry_t *info)
1569 {
1570 int ret = 0;
1571 struct timestamp_t tm;
1572 struct dentry_t *ep, *ep2;
1573 struct entry_set_cache_t *es = NULL;
1574 struct super_block *sb = inode->i_sb;
1575 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
1576 struct file_id_t *fid = &(EXFAT_I(inode)->fid);
1577 u8 is_dir = (fid->type == TYPE_DIR) ? 1 : 0;
1578
1579 pr_debug("%s entered (inode %p info %p\n", __func__, inode, info);
1580
1581 /* acquire the lock for file system critical section */
1582 mutex_lock(&p_fs->v_mutex);
1583
1584 if (is_dir) {
1585 if ((fid->dir.dir == p_fs->root_dir) &&
1586 (fid->entry == -1)) {
1587 if (p_fs->dev_ejected)
1588 ret = -EIO;
1589 ret = 0;
1590 goto out;
1591 }
1592 }
1593
1594 fs_set_vol_flags(sb, VOL_DIRTY);
1595
1596 /* get the directory entry of given file or directory */
1597 es = get_entry_set_in_dir(sb, &fid->dir, fid->entry,
1598 ES_ALL_ENTRIES, &ep);
1599 if (!es) {
1600 ret = -ENOENT;
1601 goto out;
1602 }
1603 ep2 = ep + 1;
1604
1605 p_fs->fs_func->set_entry_attr(ep, info->Attr);
1606
1607 /* set FILE_INFO structure using the acquired struct dentry_t */
1608 tm.sec = info->CreateTimestamp.Second;
1609 tm.min = info->CreateTimestamp.Minute;
1610 tm.hour = info->CreateTimestamp.Hour;
1611 tm.day = info->CreateTimestamp.Day;
1612 tm.mon = info->CreateTimestamp.Month;
1613 tm.year = info->CreateTimestamp.Year;
1614 p_fs->fs_func->set_entry_time(ep, &tm, TM_CREATE);
1615
1616 tm.sec = info->ModifyTimestamp.Second;
1617 tm.min = info->ModifyTimestamp.Minute;
1618 tm.hour = info->ModifyTimestamp.Hour;
1619 tm.day = info->ModifyTimestamp.Day;
1620 tm.mon = info->ModifyTimestamp.Month;
1621 tm.year = info->ModifyTimestamp.Year;
1622 p_fs->fs_func->set_entry_time(ep, &tm, TM_MODIFY);
1623
1624 p_fs->fs_func->set_entry_size(ep2, info->Size);
1625
1626 update_dir_checksum_with_entry_set(sb, es);
1627 release_entry_set(es);
1628
1629 if (p_fs->dev_ejected)
1630 ret = -EIO;
1631
1632 out:
1633 /* release the lock for file system critical section */
1634 mutex_unlock(&p_fs->v_mutex);
1635
1636 pr_debug("%s exited (%d)\n", __func__, ret);
1637
1638 return ret;
1639 }
1640
1641 static int ffsMapCluster(struct inode *inode, s32 clu_offset, u32 *clu)
1642 {
1643 s32 num_clusters, num_alloced;
1644 bool modified = false;
1645 u32 last_clu;
1646 int ret = 0;
1647 struct chain_t new_clu;
1648 struct dentry_t *ep;
1649 struct entry_set_cache_t *es = NULL;
1650 struct super_block *sb = inode->i_sb;
1651 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
1652 struct file_id_t *fid = &(EXFAT_I(inode)->fid);
1653
1654 /* check the validity of pointer parameters */
1655 if (!clu)
1656 return -EINVAL;
1657
1658 /* acquire the lock for file system critical section */
1659 mutex_lock(&p_fs->v_mutex);
1660
1661 fid->rwoffset = (s64)(clu_offset) << p_fs->cluster_size_bits;
1662
1663 if (EXFAT_I(inode)->mmu_private == 0)
1664 num_clusters = 0;
1665 else
1666 num_clusters = (s32)((EXFAT_I(inode)->mmu_private - 1) >>
1667 p_fs->cluster_size_bits) + 1;
1668
1669 *clu = last_clu = fid->start_clu;
1670
1671 if (fid->flags == 0x03) {
1672 if ((clu_offset > 0) && (*clu != CLUSTER_32(~0))) {
1673 last_clu += clu_offset - 1;
1674
1675 if (clu_offset == num_clusters)
1676 *clu = CLUSTER_32(~0);
1677 else
1678 *clu += clu_offset;
1679 }
1680 } else {
1681 /* hint information */
1682 if ((clu_offset > 0) && (fid->hint_last_off > 0) &&
1683 (clu_offset >= fid->hint_last_off)) {
1684 clu_offset -= fid->hint_last_off;
1685 *clu = fid->hint_last_clu;
1686 }
1687
1688 while ((clu_offset > 0) && (*clu != CLUSTER_32(~0))) {
1689 last_clu = *clu;
1690 if (exfat_fat_read(sb, *clu, clu) == -1) {
1691 ret = -EIO;
1692 goto out;
1693 }
1694 clu_offset--;
1695 }
1696 }
1697
1698 if (*clu == CLUSTER_32(~0)) {
1699 fs_set_vol_flags(sb, VOL_DIRTY);
1700
1701 new_clu.dir = (last_clu == CLUSTER_32(~0)) ? CLUSTER_32(~0) :
1702 last_clu + 1;
1703 new_clu.size = 0;
1704 new_clu.flags = fid->flags;
1705
1706 /* (1) allocate a cluster */
1707 num_alloced = p_fs->fs_func->alloc_cluster(sb, 1, &new_clu);
1708 if (num_alloced < 0) {
1709 ret = -EIO;
1710 goto out;
1711 } else if (num_alloced == 0) {
1712 ret = -ENOSPC;
1713 goto out;
1714 }
1715
1716 /* (2) append to the FAT chain */
1717 if (last_clu == CLUSTER_32(~0)) {
1718 if (new_clu.flags == 0x01)
1719 fid->flags = 0x01;
1720 fid->start_clu = new_clu.dir;
1721 modified = true;
1722 } else {
1723 if (new_clu.flags != fid->flags) {
1724 exfat_chain_cont_cluster(sb, fid->start_clu,
1725 num_clusters);
1726 fid->flags = 0x01;
1727 modified = true;
1728 }
1729 if (new_clu.flags == 0x01)
1730 exfat_fat_write(sb, last_clu, new_clu.dir);
1731 }
1732
1733 num_clusters += num_alloced;
1734 *clu = new_clu.dir;
1735
1736 es = get_entry_set_in_dir(sb, &fid->dir, fid->entry,
1737 ES_ALL_ENTRIES, &ep);
1738 if (!es) {
1739 ret = -ENOENT;
1740 goto out;
1741 }
1742 /* get stream entry */
1743 ep++;
1744
1745 /* (3) update directory entry */
1746 if (modified) {
1747 if (p_fs->fs_func->get_entry_flag(ep) != fid->flags)
1748 p_fs->fs_func->set_entry_flag(ep, fid->flags);
1749
1750 if (p_fs->fs_func->get_entry_clu0(ep) != fid->start_clu)
1751 p_fs->fs_func->set_entry_clu0(ep,
1752 fid->start_clu);
1753
1754 }
1755
1756 update_dir_checksum_with_entry_set(sb, es);
1757 release_entry_set(es);
1758
1759 /* add number of new blocks to inode */
1760 inode->i_blocks += num_alloced << (p_fs->cluster_size_bits - 9);
1761 }
1762
1763 /* hint information */
1764 fid->hint_last_off = (s32)(fid->rwoffset >> p_fs->cluster_size_bits);
1765 fid->hint_last_clu = *clu;
1766
1767 if (p_fs->dev_ejected)
1768 ret = -EIO;
1769
1770 out:
1771 /* release the lock for file system critical section */
1772 mutex_unlock(&p_fs->v_mutex);
1773
1774 return ret;
1775 }
1776
1777 /*----------------------------------------------------------------------*/
1778 /* Directory Operation Functions */
1779 /*----------------------------------------------------------------------*/
1780
1781 static int ffsCreateDir(struct inode *inode, char *path, struct file_id_t *fid)
1782 {
1783 int ret = 0;
1784 struct chain_t dir;
1785 struct uni_name_t uni_name;
1786 struct super_block *sb = inode->i_sb;
1787 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
1788
1789 pr_debug("%s entered\n", __func__);
1790
1791 /* check the validity of pointer parameters */
1792 if (!fid || !path || (*path == '\0'))
1793 return -EINVAL;
1794
1795 /* acquire the lock for file system critical section */
1796 mutex_lock(&p_fs->v_mutex);
1797
1798 /* check the validity of directory name in the given old pathname */
1799 ret = resolve_path(inode, path, &dir, &uni_name);
1800 if (ret)
1801 goto out;
1802
1803 fs_set_vol_flags(sb, VOL_DIRTY);
1804
1805 ret = create_dir(inode, &dir, &uni_name, fid);
1806
1807 #ifndef CONFIG_EXFAT_DELAYED_SYNC
1808 fs_sync(sb, true);
1809 fs_set_vol_flags(sb, VOL_CLEAN);
1810 #endif
1811
1812 if (p_fs->dev_ejected)
1813 ret = -EIO;
1814 out:
1815 /* release the lock for file system critical section */
1816 mutex_unlock(&p_fs->v_mutex);
1817
1818 return ret;
1819 }
1820
1821 static int ffsReadDir(struct inode *inode, struct dir_entry_t *dir_entry)
1822 {
1823 int i, dentry, clu_offset;
1824 int ret = 0;
1825 s32 dentries_per_clu, dentries_per_clu_bits = 0;
1826 u32 type;
1827 sector_t sector;
1828 struct chain_t dir, clu;
1829 struct uni_name_t uni_name;
1830 struct timestamp_t tm;
1831 struct dentry_t *ep;
1832 struct super_block *sb = inode->i_sb;
1833 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
1834 struct fs_func *fs_func = p_fs->fs_func;
1835 struct file_id_t *fid = &(EXFAT_I(inode)->fid);
1836
1837 /* check the validity of pointer parameters */
1838 if (!dir_entry)
1839 return -EINVAL;
1840
1841 /* check if the given file ID is opened */
1842 if (fid->type != TYPE_DIR)
1843 return -ENOTDIR;
1844
1845 /* acquire the lock for file system critical section */
1846 mutex_lock(&p_fs->v_mutex);
1847
1848 if (fid->entry == -1) {
1849 dir.dir = p_fs->root_dir;
1850 dir.flags = 0x01;
1851 } else {
1852 dir.dir = fid->start_clu;
1853 dir.size = (s32)(fid->size >> p_fs->cluster_size_bits);
1854 dir.flags = fid->flags;
1855 }
1856
1857 dentry = (s32)fid->rwoffset;
1858
1859 if (dir.dir == CLUSTER_32(0)) {
1860 /* FAT16 root_dir */
1861 dentries_per_clu = p_fs->dentries_in_root;
1862
1863 if (dentry == dentries_per_clu) {
1864 clu.dir = CLUSTER_32(~0);
1865 } else {
1866 clu.dir = dir.dir;
1867 clu.size = dir.size;
1868 clu.flags = dir.flags;
1869 }
1870 } else {
1871 dentries_per_clu = p_fs->dentries_per_clu;
1872 dentries_per_clu_bits = ilog2(dentries_per_clu);
1873
1874 clu_offset = dentry >> dentries_per_clu_bits;
1875 clu.dir = dir.dir;
1876 clu.size = dir.size;
1877 clu.flags = dir.flags;
1878
1879 if (clu.flags == 0x03) {
1880 clu.dir += clu_offset;
1881 clu.size -= clu_offset;
1882 } else {
1883 /* hint_information */
1884 if ((clu_offset > 0) && (fid->hint_last_off > 0) &&
1885 (clu_offset >= fid->hint_last_off)) {
1886 clu_offset -= fid->hint_last_off;
1887 clu.dir = fid->hint_last_clu;
1888 }
1889
1890 while (clu_offset > 0) {
1891 /* clu.dir = exfat_fat_read(sb, clu.dir); */
1892 if (exfat_fat_read(sb, clu.dir, &clu.dir) == -1) {
1893 ret = -EIO;
1894 goto out;
1895 }
1896 clu_offset--;
1897 }
1898 }
1899 }
1900
1901 while (clu.dir != CLUSTER_32(~0)) {
1902 if (p_fs->dev_ejected)
1903 break;
1904
1905 if (dir.dir == CLUSTER_32(0)) /* FAT16 root_dir */
1906 i = dentry % dentries_per_clu;
1907 else
1908 i = dentry & (dentries_per_clu - 1);
1909
1910 for ( ; i < dentries_per_clu; i++, dentry++) {
1911 ep = get_entry_in_dir(sb, &clu, i, &sector);
1912 if (!ep) {
1913 ret = -ENOENT;
1914 goto out;
1915 }
1916 type = fs_func->get_entry_type(ep);
1917
1918 if (type == TYPE_UNUSED)
1919 break;
1920
1921 if ((type != TYPE_FILE) && (type != TYPE_DIR))
1922 continue;
1923
1924 exfat_buf_lock(sb, sector);
1925 dir_entry->Attr = fs_func->get_entry_attr(ep);
1926
1927 fs_func->get_entry_time(ep, &tm, TM_CREATE);
1928 dir_entry->CreateTimestamp.Year = tm.year;
1929 dir_entry->CreateTimestamp.Month = tm.mon;
1930 dir_entry->CreateTimestamp.Day = tm.day;
1931 dir_entry->CreateTimestamp.Hour = tm.hour;
1932 dir_entry->CreateTimestamp.Minute = tm.min;
1933 dir_entry->CreateTimestamp.Second = tm.sec;
1934 dir_entry->CreateTimestamp.MilliSecond = 0;
1935
1936 fs_func->get_entry_time(ep, &tm, TM_MODIFY);
1937 dir_entry->ModifyTimestamp.Year = tm.year;
1938 dir_entry->ModifyTimestamp.Month = tm.mon;
1939 dir_entry->ModifyTimestamp.Day = tm.day;
1940 dir_entry->ModifyTimestamp.Hour = tm.hour;
1941 dir_entry->ModifyTimestamp.Minute = tm.min;
1942 dir_entry->ModifyTimestamp.Second = tm.sec;
1943 dir_entry->ModifyTimestamp.MilliSecond = 0;
1944
1945 memset((char *)&dir_entry->AccessTimestamp, 0,
1946 sizeof(struct date_time_t));
1947
1948 *uni_name.name = 0x0;
1949 fs_func->get_uni_name_from_ext_entry(sb, &dir, dentry,
1950 uni_name.name);
1951 nls_uniname_to_cstring(sb, dir_entry->Name, &uni_name);
1952 exfat_buf_unlock(sb, sector);
1953
1954 ep = get_entry_in_dir(sb, &clu, i + 1, NULL);
1955 if (!ep) {
1956 ret = -ENOENT;
1957 goto out;
1958 }
1959
1960 dir_entry->Size = fs_func->get_entry_size(ep);
1961
1962 /* hint information */
1963 if (dir.dir == CLUSTER_32(0)) { /* FAT16 root_dir */
1964 } else {
1965 fid->hint_last_off = dentry >>
1966 dentries_per_clu_bits;
1967 fid->hint_last_clu = clu.dir;
1968 }
1969
1970 fid->rwoffset = (s64)(++dentry);
1971
1972 if (p_fs->dev_ejected)
1973 ret = -EIO;
1974 goto out;
1975 }
1976
1977 if (dir.dir == CLUSTER_32(0))
1978 break; /* FAT16 root_dir */
1979
1980 if (clu.flags == 0x03) {
1981 if ((--clu.size) > 0)
1982 clu.dir++;
1983 else
1984 clu.dir = CLUSTER_32(~0);
1985 } else {
1986 /* clu.dir = exfat_fat_read(sb, clu.dir); */
1987 if (exfat_fat_read(sb, clu.dir, &clu.dir) == -1) {
1988 ret = -EIO;
1989 goto out;
1990 }
1991 }
1992 }
1993
1994 *dir_entry->Name = '\0';
1995
1996 fid->rwoffset = (s64)(++dentry);
1997
1998 if (p_fs->dev_ejected)
1999 ret = -EIO;
2000
2001 out:
2002 /* release the lock for file system critical section */
2003 mutex_unlock(&p_fs->v_mutex);
2004
2005 return ret;
2006 }
2007
2008 static int ffsRemoveDir(struct inode *inode, struct file_id_t *fid)
2009 {
2010 s32 dentry;
2011 int ret = 0;
2012 struct chain_t dir, clu_to_free;
2013 struct super_block *sb = inode->i_sb;
2014 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
2015
2016 /* check the validity of the given file id */
2017 if (!fid)
2018 return -EINVAL;
2019
2020 dir.dir = fid->dir.dir;
2021 dir.size = fid->dir.size;
2022 dir.flags = fid->dir.flags;
2023
2024 dentry = fid->entry;
2025
2026 /* check if the file is "." or ".." */
2027 if (p_fs->vol_type != EXFAT) {
2028 if ((dir.dir != p_fs->root_dir) && (dentry < 2))
2029 return -EPERM;
2030 }
2031
2032 /* acquire the lock for file system critical section */
2033 mutex_lock(&p_fs->v_mutex);
2034
2035 clu_to_free.dir = fid->start_clu;
2036 clu_to_free.size = (s32)((fid->size - 1) >> p_fs->cluster_size_bits) + 1;
2037 clu_to_free.flags = fid->flags;
2038
2039 if (!is_dir_empty(sb, &clu_to_free)) {
2040 ret = -ENOTEMPTY;
2041 goto out;
2042 }
2043
2044 fs_set_vol_flags(sb, VOL_DIRTY);
2045
2046 /* (1) update the directory entry */
2047 remove_file(inode, &dir, dentry);
2048
2049 /* (2) free the clusters */
2050 p_fs->fs_func->free_cluster(sb, &clu_to_free, 1);
2051
2052 fid->size = 0;
2053 fid->start_clu = CLUSTER_32(~0);
2054 fid->flags = (p_fs->vol_type == EXFAT) ? 0x03 : 0x01;
2055
2056 #ifndef CONFIG_EXFAT_DELAYED_SYNC
2057 fs_sync(sb, true);
2058 fs_set_vol_flags(sb, VOL_CLEAN);
2059 #endif
2060
2061 if (p_fs->dev_ejected)
2062 ret = -EIO;
2063
2064 out:
2065 /* release the lock for file system critical section */
2066 mutex_unlock(&p_fs->v_mutex);
2067
2068 return ret;
2069 }
2070
2071 /*======================================================================*/
2072 /* Directory Entry Operations */
2073 /*======================================================================*/
2074
2075 static int exfat_readdir(struct file *filp, struct dir_context *ctx)
2076 {
2077 struct inode *inode = file_inode(filp);
2078 struct super_block *sb = inode->i_sb;
2079 struct exfat_sb_info *sbi = EXFAT_SB(sb);
2080 struct fs_info_t *p_fs = &sbi->fs_info;
2081 struct bd_info_t *p_bd = &(EXFAT_SB(sb)->bd_info);
2082 struct dir_entry_t de;
2083 unsigned long inum;
2084 loff_t cpos;
2085 int err = 0;
2086
2087 __lock_super(sb);
2088
2089 cpos = ctx->pos;
2090 /* Fake . and .. for the root directory. */
2091 if ((p_fs->vol_type == EXFAT) || (inode->i_ino == EXFAT_ROOT_INO)) {
2092 while (cpos < 2) {
2093 if (inode->i_ino == EXFAT_ROOT_INO)
2094 inum = EXFAT_ROOT_INO;
2095 else if (cpos == 0)
2096 inum = inode->i_ino;
2097 else /* (cpos == 1) */
2098 inum = parent_ino(filp->f_path.dentry);
2099
2100 if (!dir_emit_dots(filp, ctx))
2101 goto out;
2102 cpos++;
2103 ctx->pos++;
2104 }
2105 if (cpos == 2)
2106 cpos = 0;
2107 }
2108 if (cpos & (DENTRY_SIZE - 1)) {
2109 err = -ENOENT;
2110 goto out;
2111 }
2112
2113 get_new:
2114 EXFAT_I(inode)->fid.size = i_size_read(inode);
2115 EXFAT_I(inode)->fid.rwoffset = cpos >> DENTRY_SIZE_BITS;
2116
2117 err = ffsReadDir(inode, &de);
2118 if (err) {
2119 /* at least we tried to read a sector
2120 * move cpos to next sector position (should be aligned)
2121 */
2122 if (err == -EIO) {
2123 cpos += 1 << p_bd->sector_size_bits;
2124 cpos &= ~((1 << p_bd->sector_size_bits) - 1);
2125 }
2126
2127 goto end_of_dir;
2128 }
2129
2130 cpos = EXFAT_I(inode)->fid.rwoffset << DENTRY_SIZE_BITS;
2131
2132 if (!de.Name[0])
2133 goto end_of_dir;
2134
2135 if (!memcmp(de.ShortName, DOS_CUR_DIR_NAME, DOS_NAME_LENGTH)) {
2136 inum = inode->i_ino;
2137 } else if (!memcmp(de.ShortName, DOS_PAR_DIR_NAME, DOS_NAME_LENGTH)) {
2138 inum = parent_ino(filp->f_path.dentry);
2139 } else {
2140 loff_t i_pos = ((loff_t)EXFAT_I(inode)->fid.start_clu << 32) |
2141 ((EXFAT_I(inode)->fid.rwoffset - 1) & 0xffffffff);
2142 struct inode *tmp = exfat_iget(sb, i_pos);
2143
2144 if (tmp) {
2145 inum = tmp->i_ino;
2146 iput(tmp);
2147 } else {
2148 inum = iunique(sb, EXFAT_ROOT_INO);
2149 }
2150 }
2151
2152 if (!dir_emit(ctx, de.Name, strlen(de.Name), inum,
2153 (de.Attr & ATTR_SUBDIR) ? DT_DIR : DT_REG))
2154 goto out;
2155
2156 ctx->pos = cpos;
2157 goto get_new;
2158
2159 end_of_dir:
2160 ctx->pos = cpos;
2161 out:
2162 __unlock_super(sb);
2163 return err;
2164 }
2165
2166 static int exfat_ioctl_volume_id(struct inode *dir)
2167 {
2168 struct super_block *sb = dir->i_sb;
2169 struct exfat_sb_info *sbi = EXFAT_SB(sb);
2170 struct fs_info_t *p_fs = &sbi->fs_info;
2171
2172 return p_fs->vol_id;
2173 }
2174
2175 static long exfat_generic_ioctl(struct file *filp, unsigned int cmd,
2176 unsigned long arg)
2177 {
2178 struct inode *inode = filp->f_path.dentry->d_inode;
2179 #ifdef CONFIG_EXFAT_KERNEL_DEBUG
2180 unsigned int flags;
2181 #endif /* CONFIG_EXFAT_KERNEL_DEBUG */
2182
2183 switch (cmd) {
2184 case EXFAT_IOCTL_GET_VOLUME_ID:
2185 return exfat_ioctl_volume_id(inode);
2186 #ifdef CONFIG_EXFAT_KERNEL_DEBUG
2187 case EXFAT_IOC_GET_DEBUGFLAGS: {
2188 struct super_block *sb = inode->i_sb;
2189 struct exfat_sb_info *sbi = EXFAT_SB(sb);
2190
2191 flags = sbi->debug_flags;
2192 return put_user(flags, (int __user *)arg);
2193 }
2194 case EXFAT_IOC_SET_DEBUGFLAGS: {
2195 struct super_block *sb = inode->i_sb;
2196 struct exfat_sb_info *sbi = EXFAT_SB(sb);
2197
2198 if (!capable(CAP_SYS_ADMIN))
2199 return -EPERM;
2200
2201 if (get_user(flags, (int __user *)arg))
2202 return -EFAULT;
2203
2204 __lock_super(sb);
2205 sbi->debug_flags = flags;
2206 __unlock_super(sb);
2207
2208 return 0;
2209 }
2210 #endif /* CONFIG_EXFAT_KERNEL_DEBUG */
2211 default:
2212 return -ENOTTY; /* Inappropriate ioctl for device */
2213 }
2214 }
2215
2216 static const struct file_operations exfat_dir_operations = {
2217 .llseek = generic_file_llseek,
2218 .read = generic_read_dir,
2219 .iterate = exfat_readdir,
2220 .unlocked_ioctl = exfat_generic_ioctl,
2221 .fsync = generic_file_fsync,
2222 };
2223
2224 static int exfat_create(struct inode *dir, struct dentry *dentry, umode_t mode,
2225 bool excl)
2226 {
2227 struct super_block *sb = dir->i_sb;
2228 struct timespec64 curtime;
2229 struct inode *inode;
2230 struct file_id_t fid;
2231 loff_t i_pos;
2232 int err;
2233
2234 __lock_super(sb);
2235
2236 pr_debug("%s entered\n", __func__);
2237
2238 err = ffsCreateFile(dir, (u8 *)dentry->d_name.name, FM_REGULAR, &fid);
2239 if (err)
2240 goto out;
2241
2242 INC_IVERSION(dir);
2243 curtime = current_time(dir);
2244 dir->i_ctime = curtime;
2245 dir->i_mtime = curtime;
2246 dir->i_atime = curtime;
2247 if (IS_DIRSYNC(dir))
2248 (void)exfat_sync_inode(dir);
2249 else
2250 mark_inode_dirty(dir);
2251
2252 i_pos = ((loff_t)fid.dir.dir << 32) | (fid.entry & 0xffffffff);
2253
2254 inode = exfat_build_inode(sb, &fid, i_pos);
2255 if (IS_ERR(inode)) {
2256 err = PTR_ERR(inode);
2257 goto out;
2258 }
2259 INC_IVERSION(inode);
2260 curtime = current_time(inode);
2261 inode->i_mtime = curtime;
2262 inode->i_atime = curtime;
2263 inode->i_ctime = curtime;
2264 /*
2265 * timestamp is already written, so mark_inode_dirty() is unnecessary.
2266 */
2267
2268 dentry->d_time = GET_IVERSION(dentry->d_parent->d_inode);
2269 d_instantiate(dentry, inode);
2270
2271 out:
2272 __unlock_super(sb);
2273 pr_debug("%s exited\n", __func__);
2274 return err;
2275 }
2276
2277 static int exfat_find(struct inode *dir, struct qstr *qname,
2278 struct file_id_t *fid)
2279 {
2280 int err;
2281
2282 if (qname->len == 0)
2283 return -ENOENT;
2284
2285 err = ffsLookupFile(dir, (u8 *)qname->name, fid);
2286 if (err)
2287 return -ENOENT;
2288
2289 return 0;
2290 }
2291
2292 static int exfat_d_anon_disconn(struct dentry *dentry)
2293 {
2294 return IS_ROOT(dentry) && (dentry->d_flags & DCACHE_DISCONNECTED);
2295 }
2296
2297 static struct dentry *exfat_lookup(struct inode *dir, struct dentry *dentry,
2298 unsigned int flags)
2299 {
2300 struct super_block *sb = dir->i_sb;
2301 struct inode *inode;
2302 struct dentry *alias;
2303 int err;
2304 struct file_id_t fid;
2305 loff_t i_pos;
2306 u64 ret;
2307 mode_t i_mode;
2308
2309 __lock_super(sb);
2310 pr_debug("%s entered\n", __func__);
2311 err = exfat_find(dir, &dentry->d_name, &fid);
2312 if (err) {
2313 if (err == -ENOENT) {
2314 inode = NULL;
2315 goto out;
2316 }
2317 goto error;
2318 }
2319
2320 i_pos = ((loff_t)fid.dir.dir << 32) | (fid.entry & 0xffffffff);
2321 inode = exfat_build_inode(sb, &fid, i_pos);
2322 if (IS_ERR(inode)) {
2323 err = PTR_ERR(inode);
2324 goto error;
2325 }
2326
2327 i_mode = inode->i_mode;
2328 if (S_ISLNK(i_mode) && !EXFAT_I(inode)->target) {
2329 EXFAT_I(inode)->target = kmalloc(i_size_read(inode) + 1,
2330 GFP_KERNEL);
2331 if (!EXFAT_I(inode)->target) {
2332 err = -ENOMEM;
2333 goto error;
2334 }
2335 ffsReadFile(dir, &fid, EXFAT_I(inode)->target,
2336 i_size_read(inode), &ret);
2337 *(EXFAT_I(inode)->target + i_size_read(inode)) = '\0';
2338 }
2339
2340 alias = d_find_alias(inode);
2341 if (alias && !exfat_d_anon_disconn(alias)) {
2342 BUG_ON(d_unhashed(alias));
2343 if (!S_ISDIR(i_mode))
2344 d_move(alias, dentry);
2345 iput(inode);
2346 __unlock_super(sb);
2347 pr_debug("%s exited 1\n", __func__);
2348 return alias;
2349 }
2350 dput(alias);
2351 out:
2352 __unlock_super(sb);
2353 dentry->d_time = GET_IVERSION(dentry->d_parent->d_inode);
2354 dentry = d_splice_alias(inode, dentry);
2355 if (dentry)
2356 dentry->d_time = GET_IVERSION(dentry->d_parent->d_inode);
2357 pr_debug("%s exited 2\n", __func__);
2358 return dentry;
2359
2360 error:
2361 __unlock_super(sb);
2362 pr_debug("%s exited 3\n", __func__);
2363 return ERR_PTR(err);
2364 }
2365
2366 static inline unsigned long exfat_hash(loff_t i_pos)
2367 {
2368 return hash_32(i_pos, EXFAT_HASH_BITS);
2369 }
2370
2371 static void exfat_attach(struct inode *inode, loff_t i_pos)
2372 {
2373 struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb);
2374 struct hlist_head *head = sbi->inode_hashtable + exfat_hash(i_pos);
2375
2376 spin_lock(&sbi->inode_hash_lock);
2377 EXFAT_I(inode)->i_pos = i_pos;
2378 hlist_add_head(&EXFAT_I(inode)->i_hash_fat, head);
2379 spin_unlock(&sbi->inode_hash_lock);
2380 }
2381
2382 static void exfat_detach(struct inode *inode)
2383 {
2384 struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb);
2385
2386 spin_lock(&sbi->inode_hash_lock);
2387 hlist_del_init(&EXFAT_I(inode)->i_hash_fat);
2388 EXFAT_I(inode)->i_pos = 0;
2389 spin_unlock(&sbi->inode_hash_lock);
2390 }
2391
2392 static int exfat_unlink(struct inode *dir, struct dentry *dentry)
2393 {
2394 struct inode *inode = dentry->d_inode;
2395 struct super_block *sb = dir->i_sb;
2396 struct timespec64 curtime;
2397 int err;
2398
2399 __lock_super(sb);
2400
2401 pr_debug("%s entered\n", __func__);
2402
2403 EXFAT_I(inode)->fid.size = i_size_read(inode);
2404
2405 err = ffsRemoveFile(dir, &(EXFAT_I(inode)->fid));
2406 if (err)
2407 goto out;
2408
2409 INC_IVERSION(dir);
2410 curtime = current_time(dir);
2411 dir->i_mtime = curtime;
2412 dir->i_atime = curtime;
2413 if (IS_DIRSYNC(dir))
2414 (void)exfat_sync_inode(dir);
2415 else
2416 mark_inode_dirty(dir);
2417
2418 clear_nlink(inode);
2419 curtime = current_time(inode);
2420 inode->i_mtime = curtime;
2421 inode->i_atime = curtime;
2422 exfat_detach(inode);
2423 remove_inode_hash(inode);
2424
2425 out:
2426 __unlock_super(sb);
2427 pr_debug("%s exited\n", __func__);
2428 return err;
2429 }
2430
2431 static int exfat_symlink(struct inode *dir, struct dentry *dentry,
2432 const char *target)
2433 {
2434 struct super_block *sb = dir->i_sb;
2435 struct timespec64 curtime;
2436 struct inode *inode;
2437 struct file_id_t fid;
2438 loff_t i_pos;
2439 int err;
2440 u64 len = (u64)strlen(target);
2441 u64 ret;
2442
2443 __lock_super(sb);
2444
2445 pr_debug("%s entered\n", __func__);
2446
2447 err = ffsCreateFile(dir, (u8 *)dentry->d_name.name, FM_SYMLINK, &fid);
2448 if (err)
2449 goto out;
2450
2451
2452 err = ffsWriteFile(dir, &fid, (char *)target, len, &ret);
2453
2454 if (err) {
2455 ffsRemoveFile(dir, &fid);
2456 goto out;
2457 }
2458
2459 INC_IVERSION(dir);
2460 curtime = current_time(dir);
2461 dir->i_ctime = curtime;
2462 dir->i_mtime = curtime;
2463 dir->i_atime = curtime;
2464 if (IS_DIRSYNC(dir))
2465 (void)exfat_sync_inode(dir);
2466 else
2467 mark_inode_dirty(dir);
2468
2469 i_pos = ((loff_t)fid.dir.dir << 32) | (fid.entry & 0xffffffff);
2470
2471 inode = exfat_build_inode(sb, &fid, i_pos);
2472 if (IS_ERR(inode)) {
2473 err = PTR_ERR(inode);
2474 goto out;
2475 }
2476 INC_IVERSION(inode);
2477 curtime = current_time(inode);
2478 inode->i_mtime = curtime;
2479 inode->i_atime = curtime;
2480 inode->i_ctime = curtime;
2481 /* timestamp is already written, so mark_inode_dirty() is unneeded. */
2482
2483 EXFAT_I(inode)->target = kmemdup(target, len + 1, GFP_KERNEL);
2484 if (!EXFAT_I(inode)->target) {
2485 err = -ENOMEM;
2486 goto out;
2487 }
2488
2489 dentry->d_time = GET_IVERSION(dentry->d_parent->d_inode);
2490 d_instantiate(dentry, inode);
2491
2492 out:
2493 __unlock_super(sb);
2494 pr_debug("%s exited\n", __func__);
2495 return err;
2496 }
2497
2498 static int exfat_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
2499 {
2500 struct super_block *sb = dir->i_sb;
2501 struct timespec64 curtime;
2502 struct inode *inode;
2503 struct file_id_t fid;
2504 loff_t i_pos;
2505 int err;
2506
2507 __lock_super(sb);
2508
2509 pr_debug("%s entered\n", __func__);
2510
2511 err = ffsCreateDir(dir, (u8 *)dentry->d_name.name, &fid);
2512 if (err)
2513 goto out;
2514
2515 INC_IVERSION(dir);
2516 curtime = current_time(dir);
2517 dir->i_ctime = curtime;
2518 dir->i_mtime = curtime;
2519 dir->i_atime = curtime;
2520 if (IS_DIRSYNC(dir))
2521 (void)exfat_sync_inode(dir);
2522 else
2523 mark_inode_dirty(dir);
2524 inc_nlink(dir);
2525
2526 i_pos = ((loff_t)fid.dir.dir << 32) | (fid.entry & 0xffffffff);
2527
2528 inode = exfat_build_inode(sb, &fid, i_pos);
2529 if (IS_ERR(inode)) {
2530 err = PTR_ERR(inode);
2531 goto out;
2532 }
2533 INC_IVERSION(inode);
2534 curtime = current_time(inode);
2535 inode->i_mtime = curtime;
2536 inode->i_atime = curtime;
2537 inode->i_ctime = curtime;
2538 /* timestamp is already written, so mark_inode_dirty() is unneeded. */
2539
2540 dentry->d_time = GET_IVERSION(dentry->d_parent->d_inode);
2541 d_instantiate(dentry, inode);
2542
2543 out:
2544 __unlock_super(sb);
2545 pr_debug("%s exited\n", __func__);
2546 return err;
2547 }
2548
2549 static int exfat_rmdir(struct inode *dir, struct dentry *dentry)
2550 {
2551 struct inode *inode = dentry->d_inode;
2552 struct super_block *sb = dir->i_sb;
2553 struct timespec64 curtime;
2554 int err;
2555
2556 __lock_super(sb);
2557
2558 pr_debug("%s entered\n", __func__);
2559
2560 EXFAT_I(inode)->fid.size = i_size_read(inode);
2561
2562 err = ffsRemoveDir(dir, &(EXFAT_I(inode)->fid));
2563 if (err)
2564 goto out;
2565
2566 INC_IVERSION(dir);
2567 curtime = current_time(dir);
2568 dir->i_mtime = curtime;
2569 dir->i_atime = curtime;
2570 if (IS_DIRSYNC(dir))
2571 (void)exfat_sync_inode(dir);
2572 else
2573 mark_inode_dirty(dir);
2574 drop_nlink(dir);
2575
2576 clear_nlink(inode);
2577 curtime = current_time(inode);
2578 inode->i_mtime = curtime;
2579 inode->i_atime = curtime;
2580 exfat_detach(inode);
2581 remove_inode_hash(inode);
2582
2583 out:
2584 __unlock_super(sb);
2585 pr_debug("%s exited\n", __func__);
2586 return err;
2587 }
2588
2589 static int exfat_rename(struct inode *old_dir, struct dentry *old_dentry,
2590 struct inode *new_dir, struct dentry *new_dentry,
2591 unsigned int flags)
2592 {
2593 struct inode *old_inode, *new_inode;
2594 struct super_block *sb = old_dir->i_sb;
2595 struct timespec64 curtime;
2596 loff_t i_pos;
2597 int err;
2598
2599 if (flags)
2600 return -EINVAL;
2601
2602 __lock_super(sb);
2603
2604 pr_debug("%s entered\n", __func__);
2605
2606 old_inode = old_dentry->d_inode;
2607 new_inode = new_dentry->d_inode;
2608
2609 EXFAT_I(old_inode)->fid.size = i_size_read(old_inode);
2610
2611 err = ffsMoveFile(old_dir, &(EXFAT_I(old_inode)->fid), new_dir,
2612 new_dentry);
2613 if (err)
2614 goto out;
2615
2616 INC_IVERSION(new_dir);
2617 curtime = current_time(new_dir);
2618 new_dir->i_ctime = curtime;
2619 new_dir->i_mtime = curtime;
2620 new_dir->i_atime = curtime;
2621
2622 if (IS_DIRSYNC(new_dir))
2623 (void)exfat_sync_inode(new_dir);
2624 else
2625 mark_inode_dirty(new_dir);
2626
2627 i_pos = ((loff_t)EXFAT_I(old_inode)->fid.dir.dir << 32) |
2628 (EXFAT_I(old_inode)->fid.entry & 0xffffffff);
2629
2630 exfat_detach(old_inode);
2631 exfat_attach(old_inode, i_pos);
2632 if (IS_DIRSYNC(new_dir))
2633 (void)exfat_sync_inode(old_inode);
2634 else
2635 mark_inode_dirty(old_inode);
2636
2637 if ((S_ISDIR(old_inode->i_mode)) && (old_dir != new_dir)) {
2638 drop_nlink(old_dir);
2639 if (!new_inode)
2640 inc_nlink(new_dir);
2641 }
2642 INC_IVERSION(old_dir);
2643 curtime = current_time(old_dir);
2644 old_dir->i_ctime = curtime;
2645 old_dir->i_mtime = curtime;
2646 if (IS_DIRSYNC(old_dir))
2647 (void)exfat_sync_inode(old_dir);
2648 else
2649 mark_inode_dirty(old_dir);
2650
2651 if (new_inode) {
2652 exfat_detach(new_inode);
2653 drop_nlink(new_inode);
2654 if (S_ISDIR(new_inode->i_mode))
2655 drop_nlink(new_inode);
2656 new_inode->i_ctime = current_time(new_inode);
2657 }
2658
2659 out:
2660 __unlock_super(sb);
2661 pr_debug("%s exited\n", __func__);
2662 return err;
2663 }
2664
2665 static int exfat_cont_expand(struct inode *inode, loff_t size)
2666 {
2667 struct address_space *mapping = inode->i_mapping;
2668 loff_t start = i_size_read(inode), count = size - i_size_read(inode);
2669 struct timespec64 curtime;
2670 int err, err2;
2671
2672 err = generic_cont_expand_simple(inode, size);
2673 if (err != 0)
2674 return err;
2675
2676 curtime = current_time(inode);
2677 inode->i_ctime = curtime;
2678 inode->i_mtime = curtime;
2679 mark_inode_dirty(inode);
2680
2681 if (IS_SYNC(inode)) {
2682 err = filemap_fdatawrite_range(mapping, start,
2683 start + count - 1);
2684 err2 = sync_mapping_buffers(mapping);
2685 err = (err) ? (err) : (err2);
2686 err2 = write_inode_now(inode, 1);
2687 err = (err) ? (err) : (err2);
2688 if (!err)
2689 err = filemap_fdatawait_range(mapping, start,
2690 start + count - 1);
2691 }
2692 return err;
2693 }
2694
2695 static int exfat_allow_set_time(struct exfat_sb_info *sbi, struct inode *inode)
2696 {
2697 mode_t allow_utime = sbi->options.allow_utime;
2698
2699 if (!uid_eq(current_fsuid(), inode->i_uid)) {
2700 if (in_group_p(inode->i_gid))
2701 allow_utime >>= 3;
2702 if (allow_utime & MAY_WRITE)
2703 return 1;
2704 }
2705
2706 /* use a default check */
2707 return 0;
2708 }
2709
2710 static int exfat_sanitize_mode(const struct exfat_sb_info *sbi,
2711 struct inode *inode, umode_t *mode_ptr)
2712 {
2713 mode_t i_mode, mask, perm;
2714
2715 i_mode = inode->i_mode;
2716
2717 if (S_ISREG(i_mode) || S_ISLNK(i_mode))
2718 mask = sbi->options.fs_fmask;
2719 else
2720 mask = sbi->options.fs_dmask;
2721
2722 perm = *mode_ptr & ~(S_IFMT | mask);
2723
2724 /* Of the r and x bits, all (subject to umask) must be present.*/
2725 if ((perm & 0555) != (i_mode & 0555))
2726 return -EPERM;
2727
2728 if (exfat_mode_can_hold_ro(inode)) {
2729 /*
2730 * Of the w bits, either all (subject to umask) or none must be
2731 * present.
2732 */
2733 if ((perm & 0222) && ((perm & 0222) != (0222 & ~mask)))
2734 return -EPERM;
2735 } else {
2736 /*
2737 * If exfat_mode_can_hold_ro(inode) is false, can't change w
2738 * bits.
2739 */
2740 if ((perm & 0222) != (0222 & ~mask))
2741 return -EPERM;
2742 }
2743
2744 *mode_ptr &= S_IFMT | perm;
2745
2746 return 0;
2747 }
2748
2749 static void exfat_truncate(struct inode *inode, loff_t old_size)
2750 {
2751 struct super_block *sb = inode->i_sb;
2752 struct exfat_sb_info *sbi = EXFAT_SB(sb);
2753 struct fs_info_t *p_fs = &sbi->fs_info;
2754 struct timespec64 curtime;
2755 int err;
2756
2757 __lock_super(sb);
2758
2759 /*
2760 * This protects against truncating a file bigger than it was then
2761 * trying to write into the hole.
2762 */
2763 if (EXFAT_I(inode)->mmu_private > i_size_read(inode))
2764 EXFAT_I(inode)->mmu_private = i_size_read(inode);
2765
2766 if (EXFAT_I(inode)->fid.start_clu == 0)
2767 goto out;
2768
2769 err = ffsTruncateFile(inode, old_size, i_size_read(inode));
2770 if (err)
2771 goto out;
2772
2773 curtime = current_time(inode);
2774 inode->i_ctime = curtime;
2775 inode->i_mtime = curtime;
2776 if (IS_DIRSYNC(inode))
2777 (void)exfat_sync_inode(inode);
2778 else
2779 mark_inode_dirty(inode);
2780
2781 inode->i_blocks = ((i_size_read(inode) + (p_fs->cluster_size - 1)) &
2782 ~((loff_t)p_fs->cluster_size - 1)) >> 9;
2783 out:
2784 __unlock_super(sb);
2785 }
2786
2787 static int exfat_setattr(struct dentry *dentry, struct iattr *attr)
2788 {
2789 struct exfat_sb_info *sbi = EXFAT_SB(dentry->d_sb);
2790 struct inode *inode = dentry->d_inode;
2791 unsigned int ia_valid;
2792 int error;
2793 loff_t old_size;
2794
2795 pr_debug("%s entered\n", __func__);
2796
2797 if ((attr->ia_valid & ATTR_SIZE) &&
2798 attr->ia_size > i_size_read(inode)) {
2799 error = exfat_cont_expand(inode, attr->ia_size);
2800 if (error || attr->ia_valid == ATTR_SIZE)
2801 return error;
2802 attr->ia_valid &= ~ATTR_SIZE;
2803 }
2804
2805 ia_valid = attr->ia_valid;
2806
2807 if ((ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)) &&
2808 exfat_allow_set_time(sbi, inode)) {
2809 attr->ia_valid &= ~(ATTR_MTIME_SET |
2810 ATTR_ATIME_SET |
2811 ATTR_TIMES_SET);
2812 }
2813
2814 error = setattr_prepare(dentry, attr);
2815 attr->ia_valid = ia_valid;
2816 if (error)
2817 return error;
2818
2819 if (((attr->ia_valid & ATTR_UID) &&
2820 (!uid_eq(attr->ia_uid, sbi->options.fs_uid))) ||
2821 ((attr->ia_valid & ATTR_GID) &&
2822 (!gid_eq(attr->ia_gid, sbi->options.fs_gid))) ||
2823 ((attr->ia_valid & ATTR_MODE) &&
2824 (attr->ia_mode & ~(S_IFREG | S_IFLNK | S_IFDIR | 0777)))) {
2825 return -EPERM;
2826 }
2827
2828 /*
2829 * We don't return -EPERM here. Yes, strange, but this is too
2830 * old behavior.
2831 */
2832 if (attr->ia_valid & ATTR_MODE) {
2833 if (exfat_sanitize_mode(sbi, inode, &attr->ia_mode) < 0)
2834 attr->ia_valid &= ~ATTR_MODE;
2835 }
2836
2837 EXFAT_I(inode)->fid.size = i_size_read(inode);
2838
2839 if (attr->ia_valid & ATTR_SIZE) {
2840 old_size = i_size_read(inode);
2841 down_write(&EXFAT_I(inode)->truncate_lock);
2842 truncate_setsize(inode, attr->ia_size);
2843 exfat_truncate(inode, old_size);
2844 up_write(&EXFAT_I(inode)->truncate_lock);
2845 }
2846 setattr_copy(inode, attr);
2847 mark_inode_dirty(inode);
2848
2849 pr_debug("%s exited\n", __func__);
2850 return error;
2851 }
2852
2853 static int exfat_getattr(const struct path *path, struct kstat *stat,
2854 u32 request_mask, unsigned int flags)
2855 {
2856 struct inode *inode = path->dentry->d_inode;
2857
2858 pr_debug("%s entered\n", __func__);
2859
2860 generic_fillattr(inode, stat);
2861 stat->blksize = EXFAT_SB(inode->i_sb)->fs_info.cluster_size;
2862
2863 pr_debug("%s exited\n", __func__);
2864 return 0;
2865 }
2866
2867 static const struct inode_operations exfat_dir_inode_operations = {
2868 .create = exfat_create,
2869 .lookup = exfat_lookup,
2870 .unlink = exfat_unlink,
2871 .symlink = exfat_symlink,
2872 .mkdir = exfat_mkdir,
2873 .rmdir = exfat_rmdir,
2874 .rename = exfat_rename,
2875 .setattr = exfat_setattr,
2876 .getattr = exfat_getattr,
2877 };
2878
2879 /*======================================================================*/
2880 /* File Operations */
2881 /*======================================================================*/
2882 static const char *exfat_get_link(struct dentry *dentry, struct inode *inode,
2883 struct delayed_call *done)
2884 {
2885 struct exfat_inode_info *ei = EXFAT_I(inode);
2886
2887 if (ei->target) {
2888 char *cookie = ei->target;
2889
2890 if (cookie)
2891 return (char *)(ei->target);
2892 }
2893 return NULL;
2894 }
2895
2896 static const struct inode_operations exfat_symlink_inode_operations = {
2897 .get_link = exfat_get_link,
2898 };
2899
2900 static int exfat_file_release(struct inode *inode, struct file *filp)
2901 {
2902 struct super_block *sb = inode->i_sb;
2903
2904 EXFAT_I(inode)->fid.size = i_size_read(inode);
2905 ffsSyncVol(sb, false);
2906 return 0;
2907 }
2908
2909 static const struct file_operations exfat_file_operations = {
2910 .llseek = generic_file_llseek,
2911 .read_iter = generic_file_read_iter,
2912 .write_iter = generic_file_write_iter,
2913 .mmap = generic_file_mmap,
2914 .release = exfat_file_release,
2915 .unlocked_ioctl = exfat_generic_ioctl,
2916 .fsync = generic_file_fsync,
2917 .splice_read = generic_file_splice_read,
2918 };
2919
2920 static const struct inode_operations exfat_file_inode_operations = {
2921 .setattr = exfat_setattr,
2922 .getattr = exfat_getattr,
2923 };
2924
2925 /*======================================================================*/
2926 /* Address Space Operations */
2927 /*======================================================================*/
2928
2929 static int exfat_bmap(struct inode *inode, sector_t sector, sector_t *phys,
2930 unsigned long *mapped_blocks, int *create)
2931 {
2932 struct super_block *sb = inode->i_sb;
2933 struct exfat_sb_info *sbi = EXFAT_SB(sb);
2934 struct fs_info_t *p_fs = &sbi->fs_info;
2935 const unsigned long blocksize = sb->s_blocksize;
2936 const unsigned char blocksize_bits = sb->s_blocksize_bits;
2937 sector_t last_block;
2938 int err, clu_offset, sec_offset;
2939 unsigned int cluster;
2940
2941 *phys = 0;
2942 *mapped_blocks = 0;
2943
2944 last_block = (i_size_read(inode) + (blocksize - 1)) >> blocksize_bits;
2945 if (sector >= last_block) {
2946 if (*create == 0)
2947 return 0;
2948 } else {
2949 *create = 0;
2950 }
2951
2952 /* cluster offset */
2953 clu_offset = sector >> p_fs->sectors_per_clu_bits;
2954
2955 /* sector offset in cluster */
2956 sec_offset = sector & (p_fs->sectors_per_clu - 1);
2957
2958 EXFAT_I(inode)->fid.size = i_size_read(inode);
2959
2960 err = ffsMapCluster(inode, clu_offset, &cluster);
2961
2962 if (!err && (cluster != CLUSTER_32(~0))) {
2963 *phys = START_SECTOR(cluster) + sec_offset;
2964 *mapped_blocks = p_fs->sectors_per_clu - sec_offset;
2965 }
2966
2967 return 0;
2968 }
2969
2970 static int exfat_get_block(struct inode *inode, sector_t iblock,
2971 struct buffer_head *bh_result, int create)
2972 {
2973 struct super_block *sb = inode->i_sb;
2974 unsigned long max_blocks = bh_result->b_size >> inode->i_blkbits;
2975 int err;
2976 unsigned long mapped_blocks;
2977 sector_t phys;
2978
2979 __lock_super(sb);
2980
2981 err = exfat_bmap(inode, iblock, &phys, &mapped_blocks, &create);
2982 if (err) {
2983 __unlock_super(sb);
2984 return err;
2985 }
2986
2987 if (phys) {
2988 max_blocks = min(mapped_blocks, max_blocks);
2989 if (create) {
2990 EXFAT_I(inode)->mmu_private += max_blocks <<
2991 sb->s_blocksize_bits;
2992 set_buffer_new(bh_result);
2993 }
2994 map_bh(bh_result, sb, phys);
2995 }
2996
2997 bh_result->b_size = max_blocks << sb->s_blocksize_bits;
2998 __unlock_super(sb);
2999
3000 return 0;
3001 }
3002
3003 static int exfat_readpage(struct file *file, struct page *page)
3004 {
3005 return mpage_readpage(page, exfat_get_block);
3006 }
3007
3008 static int exfat_readpages(struct file *file, struct address_space *mapping,
3009 struct list_head *pages, unsigned int nr_pages)
3010 {
3011 return mpage_readpages(mapping, pages, nr_pages, exfat_get_block);
3012 }
3013
3014 static int exfat_writepage(struct page *page, struct writeback_control *wbc)
3015 {
3016 return block_write_full_page(page, exfat_get_block, wbc);
3017 }
3018
3019 static int exfat_writepages(struct address_space *mapping,
3020 struct writeback_control *wbc)
3021 {
3022 return mpage_writepages(mapping, wbc, exfat_get_block);
3023 }
3024
3025 static void exfat_write_failed(struct address_space *mapping, loff_t to)
3026 {
3027 struct inode *inode = mapping->host;
3028
3029 if (to > i_size_read(inode)) {
3030 truncate_pagecache(inode, i_size_read(inode));
3031 EXFAT_I(inode)->fid.size = i_size_read(inode);
3032 exfat_truncate(inode, i_size_read(inode));
3033 }
3034 }
3035
3036 static int exfat_write_begin(struct file *file, struct address_space *mapping,
3037 loff_t pos, unsigned int len, unsigned int flags,
3038 struct page **pagep, void **fsdata)
3039 {
3040 int ret;
3041
3042 *pagep = NULL;
3043 ret = cont_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
3044 exfat_get_block,
3045 &EXFAT_I(mapping->host)->mmu_private);
3046
3047 if (ret < 0)
3048 exfat_write_failed(mapping, pos + len);
3049 return ret;
3050 }
3051
3052 static int exfat_write_end(struct file *file, struct address_space *mapping,
3053 loff_t pos, unsigned int len, unsigned int copied,
3054 struct page *pagep, void *fsdata)
3055 {
3056 struct inode *inode = mapping->host;
3057 struct file_id_t *fid = &(EXFAT_I(inode)->fid);
3058 struct timespec64 curtime;
3059 int err;
3060
3061 err = generic_write_end(file, mapping, pos, len, copied, pagep, fsdata);
3062
3063 if (err < len)
3064 exfat_write_failed(mapping, pos + len);
3065
3066 if (!(err < 0) && !(fid->attr & ATTR_ARCHIVE)) {
3067 curtime = current_time(inode);
3068 inode->i_mtime = curtime;
3069 inode->i_ctime = curtime;
3070 fid->attr |= ATTR_ARCHIVE;
3071 mark_inode_dirty(inode);
3072 }
3073 return err;
3074 }
3075
3076 static ssize_t exfat_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
3077 {
3078 struct inode *inode = iocb->ki_filp->f_mapping->host;
3079 struct address_space *mapping = iocb->ki_filp->f_mapping;
3080 ssize_t ret;
3081 int rw;
3082
3083 rw = iov_iter_rw(iter);
3084
3085 if (rw == WRITE) {
3086 if (EXFAT_I(inode)->mmu_private < iov_iter_count(iter))
3087 return 0;
3088 }
3089 ret = blockdev_direct_IO(iocb, inode, iter, exfat_get_block);
3090
3091 if ((ret < 0) && (rw & WRITE))
3092 exfat_write_failed(mapping, iov_iter_count(iter));
3093 return ret;
3094 }
3095
3096 static sector_t _exfat_bmap(struct address_space *mapping, sector_t block)
3097 {
3098 sector_t blocknr;
3099
3100 /* exfat_get_cluster() assumes the requested blocknr isn't truncated. */
3101 down_read(&EXFAT_I(mapping->host)->truncate_lock);
3102 blocknr = generic_block_bmap(mapping, block, exfat_get_block);
3103 up_read(&EXFAT_I(mapping->host)->truncate_lock);
3104
3105 return blocknr;
3106 }
3107
3108 static const struct address_space_operations exfat_aops = {
3109 .readpage = exfat_readpage,
3110 .readpages = exfat_readpages,
3111 .writepage = exfat_writepage,
3112 .writepages = exfat_writepages,
3113 .write_begin = exfat_write_begin,
3114 .write_end = exfat_write_end,
3115 .direct_IO = exfat_direct_IO,
3116 .bmap = _exfat_bmap
3117 };
3118
3119 /*======================================================================*/
3120 /* Super Operations */
3121 /*======================================================================*/
3122
3123 static struct inode *exfat_iget(struct super_block *sb, loff_t i_pos)
3124 {
3125 struct exfat_sb_info *sbi = EXFAT_SB(sb);
3126 struct exfat_inode_info *info;
3127 struct hlist_head *head = sbi->inode_hashtable + exfat_hash(i_pos);
3128 struct inode *inode = NULL;
3129
3130 spin_lock(&sbi->inode_hash_lock);
3131 hlist_for_each_entry(info, head, i_hash_fat) {
3132 BUG_ON(info->vfs_inode.i_sb != sb);
3133
3134 if (i_pos != info->i_pos)
3135 continue;
3136 inode = igrab(&info->vfs_inode);
3137 if (inode)
3138 break;
3139 }
3140 spin_unlock(&sbi->inode_hash_lock);
3141 return inode;
3142 }
3143
3144 /* doesn't deal with root inode */
3145 static int exfat_fill_inode(struct inode *inode, struct file_id_t *fid)
3146 {
3147 struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb);
3148 struct fs_info_t *p_fs = &sbi->fs_info;
3149 struct dir_entry_t info;
3150
3151 memcpy(&(EXFAT_I(inode)->fid), fid, sizeof(struct file_id_t));
3152
3153 ffsReadStat(inode, &info);
3154
3155 EXFAT_I(inode)->i_pos = 0;
3156 EXFAT_I(inode)->target = NULL;
3157 inode->i_uid = sbi->options.fs_uid;
3158 inode->i_gid = sbi->options.fs_gid;
3159 INC_IVERSION(inode);
3160 inode->i_generation = prandom_u32();
3161
3162 if (info.Attr & ATTR_SUBDIR) { /* directory */
3163 inode->i_generation &= ~1;
3164 inode->i_mode = exfat_make_mode(sbi, info.Attr, 0777);
3165 inode->i_op = &exfat_dir_inode_operations;
3166 inode->i_fop = &exfat_dir_operations;
3167
3168 i_size_write(inode, info.Size);
3169 EXFAT_I(inode)->mmu_private = i_size_read(inode);
3170 set_nlink(inode, info.NumSubdirs);
3171 } else if (info.Attr & ATTR_SYMLINK) { /* symbolic link */
3172 inode->i_generation |= 1;
3173 inode->i_mode = exfat_make_mode(sbi, info.Attr, 0777);
3174 inode->i_op = &exfat_symlink_inode_operations;
3175
3176 i_size_write(inode, info.Size);
3177 EXFAT_I(inode)->mmu_private = i_size_read(inode);
3178 } else { /* regular file */
3179 inode->i_generation |= 1;
3180 inode->i_mode = exfat_make_mode(sbi, info.Attr, 0777);
3181 inode->i_op = &exfat_file_inode_operations;
3182 inode->i_fop = &exfat_file_operations;
3183 inode->i_mapping->a_ops = &exfat_aops;
3184 inode->i_mapping->nrpages = 0;
3185
3186 i_size_write(inode, info.Size);
3187 EXFAT_I(inode)->mmu_private = i_size_read(inode);
3188 }
3189 exfat_save_attr(inode, info.Attr);
3190
3191 inode->i_blocks = ((i_size_read(inode) + (p_fs->cluster_size - 1))
3192 & ~((loff_t)p_fs->cluster_size - 1)) >> 9;
3193
3194 exfat_time_fat2unix(&inode->i_mtime, &info.ModifyTimestamp);
3195 exfat_time_fat2unix(&inode->i_ctime, &info.CreateTimestamp);
3196 exfat_time_fat2unix(&inode->i_atime, &info.AccessTimestamp);
3197
3198 return 0;
3199 }
3200
3201 static struct inode *exfat_build_inode(struct super_block *sb,
3202 struct file_id_t *fid, loff_t i_pos)
3203 {
3204 struct inode *inode;
3205 int err;
3206
3207 inode = exfat_iget(sb, i_pos);
3208 if (inode)
3209 goto out;
3210 inode = new_inode(sb);
3211 if (!inode) {
3212 inode = ERR_PTR(-ENOMEM);
3213 goto out;
3214 }
3215 inode->i_ino = iunique(sb, EXFAT_ROOT_INO);
3216 SET_IVERSION(inode, 1);
3217 err = exfat_fill_inode(inode, fid);
3218 if (err) {
3219 iput(inode);
3220 inode = ERR_PTR(err);
3221 goto out;
3222 }
3223 exfat_attach(inode, i_pos);
3224 insert_inode_hash(inode);
3225 out:
3226 return inode;
3227 }
3228
3229 static int exfat_sync_inode(struct inode *inode)
3230 {
3231 return exfat_write_inode(inode, NULL);
3232 }
3233
3234 static struct inode *exfat_alloc_inode(struct super_block *sb)
3235 {
3236 struct exfat_inode_info *ei;
3237
3238 ei = kmem_cache_alloc(exfat_inode_cachep, GFP_NOFS);
3239 if (!ei)
3240 return NULL;
3241
3242 init_rwsem(&ei->truncate_lock);
3243
3244 return &ei->vfs_inode;
3245 }
3246
3247 static void exfat_destroy_inode(struct inode *inode)
3248 {
3249 kfree(EXFAT_I(inode)->target);
3250 EXFAT_I(inode)->target = NULL;
3251
3252 kmem_cache_free(exfat_inode_cachep, EXFAT_I(inode));
3253 }
3254
3255 static int exfat_write_inode(struct inode *inode, struct writeback_control *wbc)
3256 {
3257 struct dir_entry_t info;
3258
3259 if (inode->i_ino == EXFAT_ROOT_INO)
3260 return 0;
3261
3262 info.Attr = exfat_make_attr(inode);
3263 info.Size = i_size_read(inode);
3264
3265 exfat_time_unix2fat(&inode->i_mtime, &info.ModifyTimestamp);
3266 exfat_time_unix2fat(&inode->i_ctime, &info.CreateTimestamp);
3267 exfat_time_unix2fat(&inode->i_atime, &info.AccessTimestamp);
3268
3269 ffsWriteStat(inode, &info);
3270
3271 return 0;
3272 }
3273
3274 static void exfat_evict_inode(struct inode *inode)
3275 {
3276 truncate_inode_pages(&inode->i_data, 0);
3277
3278 if (!inode->i_nlink)
3279 i_size_write(inode, 0);
3280 invalidate_inode_buffers(inode);
3281 clear_inode(inode);
3282 exfat_detach(inode);
3283
3284 remove_inode_hash(inode);
3285 }
3286
3287 static void exfat_free_super(struct exfat_sb_info *sbi)
3288 {
3289 if (sbi->nls_disk)
3290 unload_nls(sbi->nls_disk);
3291 if (sbi->nls_io)
3292 unload_nls(sbi->nls_io);
3293 if (sbi->options.iocharset != exfat_default_iocharset)
3294 kfree(sbi->options.iocharset);
3295 /* mutex_init is in exfat_fill_super function. only for 3.7+ */
3296 mutex_destroy(&sbi->s_lock);
3297 kvfree(sbi);
3298 }
3299
3300 static void exfat_put_super(struct super_block *sb)
3301 {
3302 struct exfat_sb_info *sbi = EXFAT_SB(sb);
3303
3304 if (__is_sb_dirty(sb))
3305 exfat_write_super(sb);
3306
3307 ffsUmountVol(sb);
3308
3309 sb->s_fs_info = NULL;
3310 exfat_free_super(sbi);
3311 }
3312
3313 static void exfat_write_super(struct super_block *sb)
3314 {
3315 __lock_super(sb);
3316
3317 __set_sb_clean(sb);
3318
3319 if (!sb_rdonly(sb))
3320 ffsSyncVol(sb, true);
3321
3322 __unlock_super(sb);
3323 }
3324
3325 static int exfat_sync_fs(struct super_block *sb, int wait)
3326 {
3327 int err = 0;
3328
3329 if (__is_sb_dirty(sb)) {
3330 __lock_super(sb);
3331 __set_sb_clean(sb);
3332 err = ffsSyncVol(sb, true);
3333 __unlock_super(sb);
3334 }
3335
3336 return err;
3337 }
3338
3339 static int exfat_statfs(struct dentry *dentry, struct kstatfs *buf)
3340 {
3341 struct super_block *sb = dentry->d_sb;
3342 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
3343 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
3344 struct vol_info_t info;
3345
3346 if (p_fs->used_clusters == UINT_MAX) {
3347 if (ffsGetVolInfo(sb, &info) == -EIO)
3348 return -EIO;
3349
3350 } else {
3351 info.FatType = p_fs->vol_type;
3352 info.ClusterSize = p_fs->cluster_size;
3353 info.NumClusters = p_fs->num_clusters - 2;
3354 info.UsedClusters = p_fs->used_clusters;
3355 info.FreeClusters = info.NumClusters - info.UsedClusters;
3356
3357 if (p_fs->dev_ejected)
3358 pr_info("[EXFAT] statfs on device that is ejected\n");
3359 }
3360
3361 buf->f_type = sb->s_magic;
3362 buf->f_bsize = info.ClusterSize;
3363 buf->f_blocks = info.NumClusters;
3364 buf->f_bfree = info.FreeClusters;
3365 buf->f_bavail = info.FreeClusters;
3366 buf->f_fsid.val[0] = (u32)id;
3367 buf->f_fsid.val[1] = (u32)(id >> 32);
3368 buf->f_namelen = 260;
3369
3370 return 0;
3371 }
3372
3373 static int exfat_remount(struct super_block *sb, int *flags, char *data)
3374 {
3375 *flags |= SB_NODIRATIME;
3376 return 0;
3377 }
3378
3379 static int exfat_show_options(struct seq_file *m, struct dentry *root)
3380 {
3381 struct exfat_sb_info *sbi = EXFAT_SB(root->d_sb);
3382 struct exfat_mount_options *opts = &sbi->options;
3383
3384 if (__kuid_val(opts->fs_uid))
3385 seq_printf(m, ",uid=%u", __kuid_val(opts->fs_uid));
3386 if (__kgid_val(opts->fs_gid))
3387 seq_printf(m, ",gid=%u", __kgid_val(opts->fs_gid));
3388 seq_printf(m, ",fmask=%04o", opts->fs_fmask);
3389 seq_printf(m, ",dmask=%04o", opts->fs_dmask);
3390 if (opts->allow_utime)
3391 seq_printf(m, ",allow_utime=%04o", opts->allow_utime);
3392 if (sbi->nls_disk)
3393 seq_printf(m, ",codepage=%s", sbi->nls_disk->charset);
3394 if (sbi->nls_io)
3395 seq_printf(m, ",iocharset=%s", sbi->nls_io->charset);
3396 seq_printf(m, ",namecase=%u", opts->casesensitive);
3397 if (opts->errors == EXFAT_ERRORS_CONT)
3398 seq_puts(m, ",errors=continue");
3399 else if (opts->errors == EXFAT_ERRORS_PANIC)
3400 seq_puts(m, ",errors=panic");
3401 else
3402 seq_puts(m, ",errors=remount-ro");
3403 #ifdef CONFIG_EXFAT_DISCARD
3404 if (opts->discard)
3405 seq_puts(m, ",discard");
3406 #endif
3407 return 0;
3408 }
3409
3410 static const struct super_operations exfat_sops = {
3411 .alloc_inode = exfat_alloc_inode,
3412 .destroy_inode = exfat_destroy_inode,
3413 .write_inode = exfat_write_inode,
3414 .evict_inode = exfat_evict_inode,
3415 .put_super = exfat_put_super,
3416 .sync_fs = exfat_sync_fs,
3417 .statfs = exfat_statfs,
3418 .remount_fs = exfat_remount,
3419 .show_options = exfat_show_options,
3420 };
3421
3422 /*======================================================================*/
3423 /* Export Operations */
3424 /*======================================================================*/
3425
3426 static struct inode *exfat_nfs_get_inode(struct super_block *sb, u64 ino,
3427 u32 generation)
3428 {
3429 struct inode *inode = NULL;
3430
3431 if (ino < EXFAT_ROOT_INO)
3432 return inode;
3433 inode = ilookup(sb, ino);
3434
3435 if (inode && generation && (inode->i_generation != generation)) {
3436 iput(inode);
3437 inode = NULL;
3438 }
3439
3440 return inode;
3441 }
3442
3443 static struct dentry *exfat_fh_to_dentry(struct super_block *sb,
3444 struct fid *fid, int fh_len,
3445 int fh_type)
3446 {
3447 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
3448 exfat_nfs_get_inode);
3449 }
3450
3451 static struct dentry *exfat_fh_to_parent(struct super_block *sb,
3452 struct fid *fid, int fh_len,
3453 int fh_type)
3454 {
3455 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
3456 exfat_nfs_get_inode);
3457 }
3458
3459 static const struct export_operations exfat_export_ops = {
3460 .fh_to_dentry = exfat_fh_to_dentry,
3461 .fh_to_parent = exfat_fh_to_parent,
3462 };
3463
3464 /*======================================================================*/
3465 /* Super Block Read Operations */
3466 /*======================================================================*/
3467
3468 enum {
3469 Opt_uid,
3470 Opt_gid,
3471 Opt_umask,
3472 Opt_dmask,
3473 Opt_fmask,
3474 Opt_allow_utime,
3475 Opt_codepage,
3476 Opt_charset,
3477 Opt_namecase,
3478 Opt_debug,
3479 Opt_err_cont,
3480 Opt_err_panic,
3481 Opt_err_ro,
3482 Opt_utf8_hack,
3483 Opt_err,
3484 #ifdef CONFIG_EXFAT_DISCARD
3485 Opt_discard,
3486 #endif /* EXFAT_CONFIG_DISCARD */
3487 };
3488
3489 static const match_table_t exfat_tokens = {
3490 {Opt_uid, "uid=%u"},
3491 {Opt_gid, "gid=%u"},
3492 {Opt_umask, "umask=%o"},
3493 {Opt_dmask, "dmask=%o"},
3494 {Opt_fmask, "fmask=%o"},
3495 {Opt_allow_utime, "allow_utime=%o"},
3496 {Opt_codepage, "codepage=%u"},
3497 {Opt_charset, "iocharset=%s"},
3498 {Opt_namecase, "namecase=%u"},
3499 {Opt_debug, "debug"},
3500 {Opt_err_cont, "errors=continue"},
3501 {Opt_err_panic, "errors=panic"},
3502 {Opt_err_ro, "errors=remount-ro"},
3503 {Opt_utf8_hack, "utf8"},
3504 #ifdef CONFIG_EXFAT_DISCARD
3505 {Opt_discard, "discard"},
3506 #endif /* CONFIG_EXFAT_DISCARD */
3507 {Opt_err, NULL}
3508 };
3509
3510 static int parse_options(char *options, int silent, int *debug,
3511 struct exfat_mount_options *opts)
3512 {
3513 char *p;
3514 substring_t args[MAX_OPT_ARGS];
3515 int option;
3516 char *iocharset;
3517
3518 opts->fs_uid = current_uid();
3519 opts->fs_gid = current_gid();
3520 opts->fs_fmask = current->fs->umask;
3521 opts->fs_dmask = current->fs->umask;
3522 opts->allow_utime = U16_MAX;
3523 opts->codepage = exfat_default_codepage;
3524 opts->iocharset = exfat_default_iocharset;
3525 opts->casesensitive = 0;
3526 opts->errors = EXFAT_ERRORS_RO;
3527 #ifdef CONFIG_EXFAT_DISCARD
3528 opts->discard = 0;
3529 #endif
3530 *debug = 0;
3531
3532 if (!options)
3533 goto out;
3534
3535 while ((p = strsep(&options, ","))) {
3536 int token;
3537
3538 if (!*p)
3539 continue;
3540
3541 token = match_token(p, exfat_tokens, args);
3542 switch (token) {
3543 case Opt_uid:
3544 if (match_int(&args[0], &option))
3545 return 0;
3546 opts->fs_uid = KUIDT_INIT(option);
3547 break;
3548 case Opt_gid:
3549 if (match_int(&args[0], &option))
3550 return 0;
3551 opts->fs_gid = KGIDT_INIT(option);
3552 break;
3553 case Opt_umask:
3554 case Opt_dmask:
3555 case Opt_fmask:
3556 if (match_octal(&args[0], &option))
3557 return 0;
3558 if (token != Opt_dmask)
3559 opts->fs_fmask = option;
3560 if (token != Opt_fmask)
3561 opts->fs_dmask = option;
3562 break;
3563 case Opt_allow_utime:
3564 if (match_octal(&args[0], &option))
3565 return 0;
3566 opts->allow_utime = option & 0022;
3567 break;
3568 case Opt_codepage:
3569 if (match_int(&args[0], &option))
3570 return 0;
3571 opts->codepage = option;
3572 break;
3573 case Opt_charset:
3574 if (opts->iocharset != exfat_default_iocharset)
3575 kfree(opts->iocharset);
3576 iocharset = match_strdup(&args[0]);
3577 if (!iocharset)
3578 return -ENOMEM;
3579 opts->iocharset = iocharset;
3580 break;
3581 case Opt_namecase:
3582 if (match_int(&args[0], &option))
3583 return 0;
3584 opts->casesensitive = option;
3585 break;
3586 case Opt_err_cont:
3587 opts->errors = EXFAT_ERRORS_CONT;
3588 break;
3589 case Opt_err_panic:
3590 opts->errors = EXFAT_ERRORS_PANIC;
3591 break;
3592 case Opt_err_ro:
3593 opts->errors = EXFAT_ERRORS_RO;
3594 break;
3595 case Opt_debug:
3596 *debug = 1;
3597 break;
3598 #ifdef CONFIG_EXFAT_DISCARD
3599 case Opt_discard:
3600 opts->discard = 1;
3601 break;
3602 #endif /* CONFIG_EXFAT_DISCARD */
3603 case Opt_utf8_hack:
3604 break;
3605 default:
3606 if (!silent)
3607 pr_err("[EXFAT] Unrecognized mount option %s or missing value\n",
3608 p);
3609 return -EINVAL;
3610 }
3611 }
3612
3613 out:
3614 if (opts->allow_utime == U16_MAX)
3615 opts->allow_utime = ~opts->fs_dmask & 0022;
3616
3617 return 0;
3618 }
3619
3620 static void exfat_hash_init(struct super_block *sb)
3621 {
3622 struct exfat_sb_info *sbi = EXFAT_SB(sb);
3623 int i;
3624
3625 spin_lock_init(&sbi->inode_hash_lock);
3626 for (i = 0; i < EXFAT_HASH_SIZE; i++)
3627 INIT_HLIST_HEAD(&sbi->inode_hashtable[i]);
3628 }
3629
3630 static int exfat_read_root(struct inode *inode)
3631 {
3632 struct super_block *sb = inode->i_sb;
3633 struct exfat_sb_info *sbi = EXFAT_SB(sb);
3634 struct fs_info_t *p_fs = &sbi->fs_info;
3635 struct timespec64 curtime;
3636 struct dir_entry_t info;
3637
3638 EXFAT_I(inode)->fid.dir.dir = p_fs->root_dir;
3639 EXFAT_I(inode)->fid.dir.flags = 0x01;
3640 EXFAT_I(inode)->fid.entry = -1;
3641 EXFAT_I(inode)->fid.start_clu = p_fs->root_dir;
3642 EXFAT_I(inode)->fid.flags = 0x01;
3643 EXFAT_I(inode)->fid.type = TYPE_DIR;
3644 EXFAT_I(inode)->fid.rwoffset = 0;
3645 EXFAT_I(inode)->fid.hint_last_off = -1;
3646
3647 EXFAT_I(inode)->target = NULL;
3648
3649 ffsReadStat(inode, &info);
3650
3651 inode->i_uid = sbi->options.fs_uid;
3652 inode->i_gid = sbi->options.fs_gid;
3653 INC_IVERSION(inode);
3654 inode->i_generation = 0;
3655 inode->i_mode = exfat_make_mode(sbi, ATTR_SUBDIR, 0777);
3656 inode->i_op = &exfat_dir_inode_operations;
3657 inode->i_fop = &exfat_dir_operations;
3658
3659 i_size_write(inode, info.Size);
3660 inode->i_blocks = ((i_size_read(inode) + (p_fs->cluster_size - 1))
3661 & ~((loff_t)p_fs->cluster_size - 1)) >> 9;
3662 EXFAT_I(inode)->i_pos = ((loff_t)p_fs->root_dir << 32) | 0xffffffff;
3663 EXFAT_I(inode)->mmu_private = i_size_read(inode);
3664
3665 exfat_save_attr(inode, ATTR_SUBDIR);
3666 curtime = current_time(inode);
3667 inode->i_mtime = curtime;
3668 inode->i_atime = curtime;
3669 inode->i_ctime = curtime;
3670 set_nlink(inode, info.NumSubdirs + 2);
3671
3672 return 0;
3673 }
3674
3675 static void setup_dops(struct super_block *sb)
3676 {
3677 if (EXFAT_SB(sb)->options.casesensitive == 0)
3678 sb->s_d_op = &exfat_ci_dentry_ops;
3679 else
3680 sb->s_d_op = &exfat_dentry_ops;
3681 }
3682
3683 static int exfat_fill_super(struct super_block *sb, void *data, int silent)
3684 {
3685 struct inode *root_inode = NULL;
3686 struct exfat_sb_info *sbi;
3687 int debug, ret;
3688 long error;
3689
3690 /*
3691 * GFP_KERNEL is ok here, because while we do hold the
3692 * supeblock lock, memory pressure can't call back into
3693 * the filesystem, since we're only just about to mount
3694 * it and have no inodes etc active!
3695 */
3696 sbi = kvzalloc(sizeof(*sbi), GFP_KERNEL);
3697 if (!sbi)
3698 return -ENOMEM;
3699 mutex_init(&sbi->s_lock);
3700 sb->s_fs_info = sbi;
3701 sb->s_flags |= SB_NODIRATIME;
3702 sb->s_magic = EXFAT_SUPER_MAGIC;
3703 sb->s_op = &exfat_sops;
3704 sb->s_export_op = &exfat_export_ops;
3705
3706 error = parse_options(data, silent, &debug, &sbi->options);
3707 if (error)
3708 goto out_fail;
3709
3710 setup_dops(sb);
3711
3712 error = -EIO;
3713 sb_min_blocksize(sb, 512);
3714 sb->s_maxbytes = 0x7fffffffffffffffLL; /* maximum file size */
3715
3716 ret = ffsMountVol(sb);
3717 if (ret) {
3718 if (!silent)
3719 pr_err("[EXFAT] ffsMountVol failed\n");
3720
3721 goto out_fail;
3722 }
3723
3724 /* set up enough so that it can read an inode */
3725 exfat_hash_init(sb);
3726
3727 /*
3728 * The low byte of FAT's first entry must have same value with
3729 * media-field. But in real world, too many devices is
3730 * writing wrong value. So, removed that validity check.
3731 *
3732 * if (FAT_FIRST_ENT(sb, media) != first)
3733 */
3734
3735 sbi->nls_io = load_nls(sbi->options.iocharset);
3736
3737 error = -ENOMEM;
3738 root_inode = new_inode(sb);
3739 if (!root_inode)
3740 goto out_fail2;
3741 root_inode->i_ino = EXFAT_ROOT_INO;
3742 SET_IVERSION(root_inode, 1);
3743
3744 error = exfat_read_root(root_inode);
3745 if (error < 0)
3746 goto out_fail2;
3747 error = -ENOMEM;
3748 exfat_attach(root_inode, EXFAT_I(root_inode)->i_pos);
3749 insert_inode_hash(root_inode);
3750 sb->s_root = d_make_root(root_inode);
3751 if (!sb->s_root) {
3752 pr_err("[EXFAT] Getting the root inode failed\n");
3753 goto out_fail2;
3754 }
3755
3756 return 0;
3757
3758 out_fail2:
3759 ffsUmountVol(sb);
3760 out_fail:
3761 if (root_inode)
3762 iput(root_inode);
3763 sb->s_fs_info = NULL;
3764 exfat_free_super(sbi);
3765 return error;
3766 }
3767
3768 static struct dentry *exfat_fs_mount(struct file_system_type *fs_type,
3769 int flags, const char *dev_name,
3770 void *data)
3771 {
3772 return mount_bdev(fs_type, flags, dev_name, data, exfat_fill_super);
3773 }
3774
3775 static void init_once(void *foo)
3776 {
3777 struct exfat_inode_info *ei = (struct exfat_inode_info *)foo;
3778
3779 INIT_HLIST_NODE(&ei->i_hash_fat);
3780 inode_init_once(&ei->vfs_inode);
3781 }
3782
3783 static int __init exfat_init_inodecache(void)
3784 {
3785 exfat_inode_cachep = kmem_cache_create("exfat_inode_cache",
3786 sizeof(struct exfat_inode_info),
3787 0,
3788 (SLAB_RECLAIM_ACCOUNT |
3789 SLAB_MEM_SPREAD),
3790 init_once);
3791 if (!exfat_inode_cachep)
3792 return -ENOMEM;
3793 return 0;
3794 }
3795
3796 static void __exit exfat_destroy_inodecache(void)
3797 {
3798 /*
3799 * Make sure all delayed rcu free inodes are flushed before we
3800 * destroy cache.
3801 */
3802 rcu_barrier();
3803 kmem_cache_destroy(exfat_inode_cachep);
3804 }
3805
3806 #ifdef CONFIG_EXFAT_KERNEL_DEBUG
3807 static void exfat_debug_kill_sb(struct super_block *sb)
3808 {
3809 struct exfat_sb_info *sbi = EXFAT_SB(sb);
3810 struct block_device *bdev = sb->s_bdev;
3811 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
3812
3813 long flags;
3814
3815 if (sbi) {
3816 flags = sbi->debug_flags;
3817
3818 if (flags & EXFAT_DEBUGFLAGS_INVALID_UMOUNT) {
3819 /*
3820 * invalidate_bdev drops all device cache include
3821 * dirty. We use this to simulate device removal.
3822 */
3823 mutex_lock(&p_fs->v_mutex);
3824 exfat_fat_release_all(sb);
3825 exfat_buf_release_all(sb);
3826 mutex_unlock(&p_fs->v_mutex);
3827
3828 invalidate_bdev(bdev);
3829 }
3830 }
3831
3832 kill_block_super(sb);
3833 }
3834 #endif /* CONFIG_EXFAT_KERNEL_DEBUG */
3835
3836 static struct file_system_type exfat_fs_type = {
3837 .owner = THIS_MODULE,
3838 .name = "exfat",
3839 .mount = exfat_fs_mount,
3840 #ifdef CONFIG_EXFAT_KERNEL_DEBUG
3841 .kill_sb = exfat_debug_kill_sb,
3842 #else
3843 .kill_sb = kill_block_super,
3844 #endif /* CONFIG_EXFAT_KERNEL_DEBUG */
3845 .fs_flags = FS_REQUIRES_DEV,
3846 };
3847
3848 static int __init init_exfat(void)
3849 {
3850 int err;
3851
3852 BUILD_BUG_ON(sizeof(struct dentry_t) != DENTRY_SIZE);
3853 BUILD_BUG_ON(sizeof(struct dos_dentry_t) != DENTRY_SIZE);
3854 BUILD_BUG_ON(sizeof(struct ext_dentry_t) != DENTRY_SIZE);
3855 BUILD_BUG_ON(sizeof(struct file_dentry_t) != DENTRY_SIZE);
3856 BUILD_BUG_ON(sizeof(struct strm_dentry_t) != DENTRY_SIZE);
3857 BUILD_BUG_ON(sizeof(struct name_dentry_t) != DENTRY_SIZE);
3858 BUILD_BUG_ON(sizeof(struct bmap_dentry_t) != DENTRY_SIZE);
3859 BUILD_BUG_ON(sizeof(struct case_dentry_t) != DENTRY_SIZE);
3860 BUILD_BUG_ON(sizeof(struct volm_dentry_t) != DENTRY_SIZE);
3861
3862 pr_info("exFAT: Version %s\n", EXFAT_VERSION);
3863
3864 err = exfat_init_inodecache();
3865 if (err)
3866 return err;
3867
3868 err = register_filesystem(&exfat_fs_type);
3869 if (err)
3870 return err;
3871
3872 return 0;
3873 }
3874
3875 static void __exit exit_exfat(void)
3876 {
3877 exfat_destroy_inodecache();
3878 unregister_filesystem(&exfat_fs_type);
3879 }
3880
3881 module_init(init_exfat);
3882 module_exit(exit_exfat);
3883
3884 MODULE_LICENSE("GPL");
3885 MODULE_DESCRIPTION("exFAT Filesystem Driver");
3886 MODULE_ALIAS_FS("exfat");