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