fs: Add sdfat
[GitHub/LineageOS/android_kernel_samsung_universal7580.git] / fs / sdfat / sdfat.c
1 /*
2 * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
16 */
17
18 /************************************************************************/
19 /* */
20 /* PROJECT : exFAT & FAT12/16/32 File System */
21 /* FILE : core.c */
22 /* PURPOSE : sdFAT glue layer for supporting VFS */
23 /* */
24 /*----------------------------------------------------------------------*/
25 /* NOTES */
26 /* */
27 /* */
28 /************************************************************************/
29
30 #include <linux/version.h>
31 #include <linux/module.h>
32 #include <linux/init.h>
33 #include <linux/time.h>
34 #include <linux/slab.h>
35 #include <linux/seq_file.h>
36 #include <linux/pagemap.h>
37 #include <linux/mpage.h>
38 #include <linux/buffer_head.h>
39 #include <linux/exportfs.h>
40 #include <linux/mount.h>
41 #include <linux/vfs.h>
42 #include <linux/parser.h>
43 #include <linux/uio.h>
44 #include <linux/writeback.h>
45 #include <linux/log2.h>
46 #include <linux/hash.h>
47 #include <linux/backing-dev.h>
48 #include <linux/sched.h>
49 #include <linux/fs_struct.h>
50 #include <linux/namei.h>
51 #include <linux/bio.h>
52 #include <linux/blkdev.h>
53 #include <linux/swap.h> /* for mark_page_accessed() */
54 #include <linux/vmalloc.h>
55 #include <asm/current.h>
56 #include <asm/unaligned.h>
57 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 10, 0)
58 #include <linux/aio.h>
59 #endif
60
61 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 0, 0)
62 #error SDFAT only supports linux kernel version 3.0 or higher
63 #endif
64
65 #include "sdfat.h"
66 #include "version.h"
67
68 /* skip iterating emit_dots when dir is empty */
69 #define ITER_POS_FILLED_DOTS (2)
70
71 /* type index declare at sdfat.h */
72 const char *FS_TYPE_STR[] = {
73 "auto",
74 "exfat",
75 "vfat"
76 };
77
78 static struct kset *sdfat_kset;
79 static struct kmem_cache *sdfat_inode_cachep;
80
81 static int sdfat_default_codepage = CONFIG_SDFAT_DEFAULT_CODEPAGE;
82 static char sdfat_default_iocharset[] = CONFIG_SDFAT_DEFAULT_IOCHARSET;
83 static const char sdfat_iocharset_with_utf8[] = "iso8859-1";
84
85 #ifdef CONFIG_SDFAT_TRACE_SB_LOCK
86 static unsigned long __lock_jiffies;
87 #endif
88
89 static void sdfat_truncate(struct inode *inode, loff_t old_size);
90 static int sdfat_get_block(struct inode *inode, sector_t iblock,
91 struct buffer_head *bh_result, int create);
92
93 static struct inode *sdfat_iget(struct super_block *sb, loff_t i_pos);
94 static struct inode *sdfat_build_inode(struct super_block *sb, const FILE_ID_T *fid, loff_t i_pos);
95 static void sdfat_detach(struct inode *inode);
96 static void sdfat_attach(struct inode *inode, loff_t i_pos);
97 static inline unsigned long sdfat_hash(loff_t i_pos);
98 static int __sdfat_write_inode(struct inode *inode, int sync);
99 static int sdfat_sync_inode(struct inode *inode);
100 static int sdfat_write_inode(struct inode *inode, struct writeback_control *wbc);
101 static void sdfat_write_super(struct super_block *sb);
102 static void sdfat_write_failed(struct address_space *mapping, loff_t to);
103
104 static void sdfat_init_namebuf(DENTRY_NAMEBUF_T *nb);
105 static int sdfat_alloc_namebuf(DENTRY_NAMEBUF_T *nb);
106 static void sdfat_free_namebuf(DENTRY_NAMEBUF_T *nb);
107
108 /*************************************************************************
109 * INNER FUNCTIONS FOR FUNCTIONS WHICH HAS KERNEL VERSION DEPENDENCY
110 *************************************************************************/
111 static void __sdfat_writepage_end_io(struct bio *bio, int err);
112 static inline void __lock_super(struct super_block *sb);
113 static inline void __unlock_super(struct super_block *sb);
114 static int __sdfat_create(struct inode *dir, struct dentry *dentry);
115 static int __sdfat_revalidate(struct dentry *dentry);
116 static int __sdfat_revalidate_ci(struct dentry *dentry, unsigned int flags);
117 static int __sdfat_file_fsync(struct file *filp, loff_t start, loff_t end, int datasync);
118 static struct dentry *__sdfat_lookup(struct inode *dir, struct dentry *dentry);
119 static int __sdfat_mkdir(struct inode *dir, struct dentry *dentry);
120 static int __sdfat_rename(struct inode *old_dir, struct dentry *old_dentry,
121 struct inode *new_dir, struct dentry *new_dentry);
122 static int __sdfat_show_options(struct seq_file *m, struct super_block *sb);
123 static inline ssize_t __sdfat_blkdev_direct_IO(int rw, struct kiocb *iocb,
124 struct inode *inode, void *iov_u, loff_t offset,
125 unsigned long nr_segs);
126 static inline ssize_t __sdfat_direct_IO(int rw, struct kiocb *iocb,
127 struct inode *inode, void *iov_u, loff_t offset,
128 loff_t count, unsigned long nr_segs);
129 static int __sdfat_d_hash(const struct dentry *dentry, struct qstr *qstr);
130 static int __sdfat_d_hashi(const struct dentry *dentry, struct qstr *qstr);
131 static int __sdfat_cmp(const struct dentry *dentry, unsigned int len,
132 const char *str, const struct qstr *name);
133 static int __sdfat_cmpi(const struct dentry *dentry, unsigned int len,
134 const char *str, const struct qstr *name);
135
136 /*************************************************************************
137 * FUNCTIONS WHICH HAS KERNEL VERSION DEPENDENCY
138 *************************************************************************/
139 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 9, 0)
140 static int sdfat_rename(struct inode *old_dir, struct dentry *old_dentry,
141 struct inode *new_dir, struct dentry *new_dentry,
142 unsigned int flags)
143 {
144 /*
145 * The VFS already checks for existence, so for local filesystems
146 * the RENAME_NOREPLACE implementation is equivalent to plain rename.
147 * Don't support any other flags
148 */
149 if (flags & ~RENAME_NOREPLACE)
150 return -EINVAL;
151 return __sdfat_rename(old_dir, old_dentry, new_dir, new_dentry);
152 }
153 #else /* LINUX_VERSION_CODE < KERNEL_VERSION(4, 9, 0) */
154 static int sdfat_rename(struct inode *old_dir, struct dentry *old_dentry,
155 struct inode *new_dir, struct dentry *new_dentry)
156 {
157 return __sdfat_rename(old_dir, old_dentry, new_dir, new_dentry);
158 }
159
160 static int setattr_prepare(struct dentry *dentry, struct iattr *attr)
161 {
162 struct inode *inode = dentry->d_inode;
163
164 return inode_change_ok(inode, attr);
165 }
166 #endif
167
168
169 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 8, 0)
170 static inline void __sdfat_submit_bio_write(struct bio *bio)
171 {
172 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
173 submit_bio(bio);
174 }
175
176 static inline unsigned int __sdfat_full_name_hash(const struct dentry *dentry, const char *name, unsigned int len)
177 {
178 return full_name_hash(dentry, name, len);
179 }
180
181 static inline unsigned long __sdfat_init_name_hash(const struct dentry *dentry)
182 {
183 return init_name_hash(dentry);
184 }
185 #else /* LINUX_VERSION_CODE < KERNEL_VERSION(4, 8, 0) */
186 static inline void __sdfat_submit_bio_write(struct bio *bio)
187 {
188 submit_bio(WRITE, bio);
189 }
190
191 static inline unsigned int __sdfat_full_name_hash(const struct dentry *unused, const char *name, unsigned int len)
192 {
193 return full_name_hash(name, len);
194 }
195
196 static inline unsigned long __sdfat_init_name_hash(const struct dentry *unused)
197 {
198 return init_name_hash();
199 }
200 #endif
201
202 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 4, 21)
203 /* EMPTY */
204 #else /* LINUX_VERSION_CODE < KERNEL_VERSION(4, 4, 21) */
205 static inline void inode_lock(struct inode *inode)
206 {
207 mutex_lock(&inode->i_mutex);
208 }
209
210 static inline void inode_unlock(struct inode *inode)
211 {
212 mutex_unlock(&inode->i_mutex);
213 }
214 #endif
215
216 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 3, 0)
217 static void sdfat_writepage_end_io(struct bio *bio)
218 {
219 __sdfat_writepage_end_io(bio, bio->bi_error);
220 }
221 #else /* LINUX_VERSION_CODE < KERNEL_VERSION(4, 3, 0) */
222 static void sdfat_writepage_end_io(struct bio *bio, int err)
223 {
224 if (test_bit(BIO_UPTODATE, &bio->bi_flags))
225 err = 0;
226 __sdfat_writepage_end_io(bio, err);
227 }
228 #endif
229
230
231 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 16, 0)
232 static inline int sdfat_remount_syncfs(struct super_block *sb)
233 {
234 sync_filesystem(sb);
235 return 0;
236 }
237 #else /* LINUX_VERSION_CODE < KERNEL_VERSION(3, 16, 0) */
238 static inline int sdfat_remount_syncfs(struct super_block *sb)
239 {
240 /*
241 * We don`t need to call sync_filesystem(sb),
242 * Because VFS calls it.
243 */
244 return 0;
245 }
246 #endif
247
248
249 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 14, 0)
250 static inline sector_t __sdfat_bio_sector(struct bio *bio)
251 {
252 return bio->bi_iter.bi_sector;
253 }
254
255 static inline void __sdfat_set_bio_iterate(struct bio *bio, sector_t sector,
256 unsigned int size, unsigned int idx, unsigned int done)
257 {
258 struct bvec_iter *iter = &(bio->bi_iter);
259
260 iter->bi_sector = sector;
261 iter->bi_size = size;
262 iter->bi_idx = idx;
263 iter->bi_bvec_done = done;
264 }
265
266 static void __sdfat_truncate_pagecache(struct inode *inode,
267 loff_t to, loff_t newsize)
268 {
269 truncate_pagecache(inode, newsize);
270 }
271
272 static int sdfat_d_hash(const struct dentry *dentry, struct qstr *qstr)
273 {
274 return __sdfat_d_hash(dentry, qstr);
275 }
276
277 static int sdfat_d_hashi(const struct dentry *dentry, struct qstr *qstr)
278 {
279 return __sdfat_d_hashi(dentry, qstr);
280 }
281
282 //instead of sdfat_readdir
283 static int sdfat_iterate(struct file *filp, struct dir_context *ctx)
284 {
285 struct inode *inode = filp->f_path.dentry->d_inode;
286 struct super_block *sb = inode->i_sb;
287 struct sdfat_sb_info *sbi = SDFAT_SB(sb);
288 FS_INFO_T *fsi = &(sbi->fsi);
289 DIR_ENTRY_T de;
290 DENTRY_NAMEBUF_T *nb = &(de.NameBuf);
291 unsigned long inum;
292 loff_t cpos;
293 int err = 0, fake_offset = 0;
294
295 sdfat_init_namebuf(nb);
296 __lock_super(sb);
297
298 cpos = ctx->pos;
299 if ((fsi->vol_type == EXFAT) || (inode->i_ino == SDFAT_ROOT_INO)) {
300 if (!dir_emit_dots(filp, ctx))
301 goto out;
302 if (ctx->pos == ITER_POS_FILLED_DOTS) {
303 cpos = 0;
304 fake_offset = 1;
305 }
306 }
307 if (cpos & (DENTRY_SIZE - 1)) {
308 err = -ENOENT;
309 goto out;
310 }
311
312 /* name buffer should be allocated before use */
313 err = sdfat_alloc_namebuf(nb);
314 if (err)
315 goto out;
316 get_new:
317 SDFAT_I(inode)->fid.size = i_size_read(inode);
318 SDFAT_I(inode)->fid.rwoffset = cpos >> DENTRY_SIZE_BITS;
319
320 if (cpos >= SDFAT_I(inode)->fid.size)
321 goto end_of_dir;
322
323 err = fsapi_readdir(inode, &de);
324 if (err) {
325 // at least we tried to read a sector
326 // move cpos to next sector position (should be aligned)
327 if (err == -EIO) {
328 cpos += 1 << (sb->s_blocksize_bits);
329 cpos &= ~((u32)sb->s_blocksize-1);
330 }
331
332 err = -EIO;
333 goto end_of_dir;
334 }
335
336 cpos = SDFAT_I(inode)->fid.rwoffset << DENTRY_SIZE_BITS;
337
338 if (!nb->lfn[0])
339 goto end_of_dir;
340
341 if (!memcmp(nb->sfn, DOS_CUR_DIR_NAME, DOS_NAME_LENGTH)) {
342 inum = inode->i_ino;
343 } else if (!memcmp(nb->sfn, DOS_PAR_DIR_NAME, DOS_NAME_LENGTH)) {
344 inum = parent_ino(filp->f_path.dentry);
345 } else {
346 loff_t i_pos = ((loff_t) SDFAT_I(inode)->fid.start_clu << 32) |
347 ((SDFAT_I(inode)->fid.rwoffset-1) & 0xffffffff);
348 struct inode *tmp = sdfat_iget(sb, i_pos);
349
350 if (tmp) {
351 inum = tmp->i_ino;
352 iput(tmp);
353 } else {
354 inum = iunique(sb, SDFAT_ROOT_INO);
355 }
356 }
357
358 /* Before calling dir_emit(), sb_lock should be released.
359 * Because page fault can occur in dir_emit() when the size of buffer given
360 * from user is larger than one page size
361 */
362 __unlock_super(sb);
363 if (!dir_emit(ctx, nb->lfn, strlen(nb->lfn), inum,
364 (de.Attr & ATTR_SUBDIR) ? DT_DIR : DT_REG))
365 goto out_unlocked;
366 __lock_super(sb);
367
368 ctx->pos = cpos;
369 goto get_new;
370
371 end_of_dir:
372 if (!cpos && fake_offset)
373 cpos = ITER_POS_FILLED_DOTS;
374 ctx->pos = cpos;
375 out:
376 __unlock_super(sb);
377 out_unlocked:
378 /*
379 * To improve performance, free namebuf after unlock sb_lock.
380 * If namebuf is not allocated, this function do nothing
381 */
382 sdfat_free_namebuf(nb);
383 return err;
384 }
385 #else /* LINUX_VERSION_CODE < KERNEL_VERSION(3, 14, 0) */
386 static inline sector_t __sdfat_bio_sector(struct bio *bio)
387 {
388 return bio->bi_sector;
389 }
390
391 static inline void __sdfat_set_bio_iterate(struct bio *bio, sector_t sector,
392 unsigned int size, unsigned int idx, unsigned int done)
393 {
394 bio->bi_sector = sector;
395 bio->bi_idx = idx;
396 bio->bi_size = size; //PAGE_SIZE;
397 }
398
399 static void __sdfat_truncate_pagecache(struct inode *inode,
400 loff_t to, loff_t newsize)
401 {
402 truncate_pagecache(inode, to, newsize);
403 }
404
405 static int sdfat_d_hash(const struct dentry *dentry,
406 const struct inode *inode, struct qstr *qstr)
407 {
408 return __sdfat_d_hash(dentry, qstr);
409 }
410
411 static int sdfat_d_hashi(const struct dentry *dentry,
412 const struct inode *inode, struct qstr *qstr)
413 {
414 return __sdfat_d_hashi(dentry, qstr);
415 }
416
417 static int sdfat_readdir(struct file *filp, void *dirent, filldir_t filldir)
418 {
419 struct inode *inode = filp->f_path.dentry->d_inode;
420 struct super_block *sb = inode->i_sb;
421 struct sdfat_sb_info *sbi = SDFAT_SB(sb);
422 FS_INFO_T *fsi = &(sbi->fsi);
423 DIR_ENTRY_T de;
424 DENTRY_NAMEBUF_T *nb = &(de.NameBuf);
425 unsigned long inum;
426 loff_t cpos;
427 int err = 0, fake_offset = 0;
428
429 sdfat_init_namebuf(nb);
430 __lock_super(sb);
431
432 cpos = filp->f_pos;
433 /* Fake . and .. for the root directory. */
434 if ((fsi->vol_type == EXFAT) || (inode->i_ino == SDFAT_ROOT_INO)) {
435 while (cpos < ITER_POS_FILLED_DOTS) {
436 if (inode->i_ino == SDFAT_ROOT_INO)
437 inum = SDFAT_ROOT_INO;
438 else if (cpos == 0)
439 inum = inode->i_ino;
440 else /* (cpos == 1) */
441 inum = parent_ino(filp->f_path.dentry);
442
443 if (filldir(dirent, "..", cpos+1, cpos, inum, DT_DIR) < 0)
444 goto out;
445 cpos++;
446 filp->f_pos++;
447 }
448 if (cpos == ITER_POS_FILLED_DOTS) {
449 cpos = 0;
450 fake_offset = 1;
451 }
452 }
453 if (cpos & (DENTRY_SIZE - 1)) {
454 err = -ENOENT;
455 goto out;
456 }
457
458 /* name buffer should be allocated before use */
459 err = sdfat_alloc_namebuf(nb);
460 if (err)
461 goto out;
462 get_new:
463 SDFAT_I(inode)->fid.size = i_size_read(inode);
464 SDFAT_I(inode)->fid.rwoffset = cpos >> DENTRY_SIZE_BITS;
465
466 if (cpos >= SDFAT_I(inode)->fid.size)
467 goto end_of_dir;
468
469 err = fsapi_readdir(inode, &de);
470 if (err) {
471 // at least we tried to read a sector
472 // move cpos to next sector position (should be aligned)
473 if (err == -EIO) {
474 cpos += 1 << (sb->s_blocksize_bits);
475 cpos &= ~((u32)sb->s_blocksize-1);
476 }
477
478 err = -EIO;
479 goto end_of_dir;
480 }
481
482 cpos = SDFAT_I(inode)->fid.rwoffset << DENTRY_SIZE_BITS;
483
484 if (!nb->lfn[0])
485 goto end_of_dir;
486
487 if (!memcmp(nb->sfn, DOS_CUR_DIR_NAME, DOS_NAME_LENGTH)) {
488 inum = inode->i_ino;
489 } else if (!memcmp(nb->sfn, DOS_PAR_DIR_NAME, DOS_NAME_LENGTH)) {
490 inum = parent_ino(filp->f_path.dentry);
491 } else {
492 loff_t i_pos = ((loff_t) SDFAT_I(inode)->fid.start_clu << 32) |
493 ((SDFAT_I(inode)->fid.rwoffset-1) & 0xffffffff);
494 struct inode *tmp = sdfat_iget(sb, i_pos);
495
496 if (tmp) {
497 inum = tmp->i_ino;
498 iput(tmp);
499 } else {
500 inum = iunique(sb, SDFAT_ROOT_INO);
501 }
502 }
503
504 /* Before calling dir_emit(), sb_lock should be released.
505 * Because page fault can occur in dir_emit() when the size of buffer given
506 * from user is larger than one page size
507 */
508 __unlock_super(sb);
509 if (filldir(dirent, nb->lfn, strlen(nb->lfn), cpos, inum,
510 (de.Attr & ATTR_SUBDIR) ? DT_DIR : DT_REG) < 0)
511 goto out_unlocked;
512 __lock_super(sb);
513
514 filp->f_pos = cpos;
515 goto get_new;
516
517 end_of_dir:
518 if (!cpos && fake_offset)
519 cpos = ITER_POS_FILLED_DOTS;
520 filp->f_pos = cpos;
521 out:
522 __unlock_super(sb);
523 out_unlocked:
524 /*
525 * To improve performance, free namebuf after unlock sb_lock.
526 * If namebuf is not allocated, this function do nothing
527 */
528 sdfat_free_namebuf(nb);
529 return err;
530 }
531 #endif
532
533
534 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 9, 0)
535 /* EMPTY */
536 #else /* LINUX_VERSION_CODE < KERNEL_VERSION(3, 9, 0) */
537 static inline struct inode *file_inode(const struct file *f)
538 {
539 return f->f_dentry->d_inode;
540 }
541 #endif
542
543
544 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 7, 0)
545 static inline int __is_sb_dirty(struct super_block *sb)
546 {
547 return SDFAT_SB(sb)->s_dirt;
548 }
549
550 static inline void __set_sb_clean(struct super_block *sb)
551 {
552 SDFAT_SB(sb)->s_dirt = 0;
553 }
554
555 /* Workqueue wrapper for sdfat_write_super () */
556 static void __write_super_delayed(struct work_struct *work)
557 {
558 struct sdfat_sb_info *sbi;
559 struct super_block *sb;
560
561 sbi = container_of(work, struct sdfat_sb_info, write_super_work.work);
562 sb = sbi->host_sb;
563
564 /* XXX: Is this needed? */
565 if (!sb || !down_read_trylock(&sb->s_umount)) {
566 DMSG("%s: skip delayed work(write_super).\n", __func__);
567 return;
568 }
569
570 DMSG("%s: do delayed_work(write_super).\n", __func__);
571
572 spin_lock(&sbi->work_lock);
573 sbi->write_super_queued = 0;
574 spin_unlock(&sbi->work_lock);
575
576 sdfat_write_super(sb);
577
578 up_read(&sb->s_umount);
579 }
580
581 static void setup_sdfat_sync_super_wq(struct super_block *sb)
582 {
583 struct sdfat_sb_info *sbi = SDFAT_SB(sb);
584
585 mutex_init(&sbi->s_lock);
586 spin_lock_init(&sbi->work_lock);
587 INIT_DELAYED_WORK(&sbi->write_super_work, __write_super_delayed);
588 sbi->host_sb = sb;
589 }
590
591 static inline bool __cancel_delayed_work_sync(struct sdfat_sb_info *sbi)
592 {
593 return cancel_delayed_work_sync(&sbi->write_super_work);
594 }
595
596 static inline void lock_super(struct super_block *sb)
597 {
598 struct sdfat_sb_info *sbi = SDFAT_SB(sb);
599
600 mutex_lock(&sbi->s_lock);
601 }
602
603 static inline void unlock_super(struct super_block *sb)
604 {
605 struct sdfat_sb_info *sbi = SDFAT_SB(sb);
606
607 mutex_unlock(&sbi->s_lock);
608 }
609
610 static int sdfat_revalidate(struct dentry *dentry, unsigned int flags)
611 {
612 if (flags & LOOKUP_RCU)
613 return -ECHILD;
614
615 return __sdfat_revalidate(dentry);
616 }
617
618 static int sdfat_revalidate_ci(struct dentry *dentry, unsigned int flags)
619 {
620 if (flags & LOOKUP_RCU)
621 return -ECHILD;
622
623 return __sdfat_revalidate_ci(dentry, flags);
624 }
625
626 static struct inode *sdfat_iget(struct super_block *sb, loff_t i_pos)
627 {
628 struct sdfat_sb_info *sbi = SDFAT_SB(sb);
629 struct sdfat_inode_info *info;
630 struct hlist_head *head = sbi->inode_hashtable + sdfat_hash(i_pos);
631 struct inode *inode = NULL;
632
633 spin_lock(&sbi->inode_hash_lock);
634 hlist_for_each_entry(info, head, i_hash_fat) {
635 BUG_ON(info->vfs_inode.i_sb != sb);
636
637 if (i_pos != info->i_pos)
638 continue;
639 inode = igrab(&info->vfs_inode);
640 if (inode)
641 break;
642 }
643 spin_unlock(&sbi->inode_hash_lock);
644 return inode;
645 }
646 #else /* LINUX_VERSION_CODE < KERNEL_VERSION(3, 7, 0) */
647 static inline int __is_sb_dirty(struct super_block *sb)
648 {
649 return sb->s_dirt;
650 }
651
652 static inline void __set_sb_clean(struct super_block *sb)
653 {
654 sb->s_dirt = 0;
655 }
656
657 static void setup_sdfat_sync_super_wq(struct super_block *sb)
658 {
659 struct sdfat_sb_info *sbi = SDFAT_SB(sb);
660
661 sbi->host_sb = sb;
662 }
663
664 static inline bool __cancel_delayed_work_sync(struct sdfat_sb_info *sbi)
665 {
666 /* DO NOTHING */
667 return 0;
668 }
669
670 static inline void clear_inode(struct inode *inode)
671 {
672 end_writeback(inode);
673 }
674
675 static int sdfat_revalidate(struct dentry *dentry, struct nameidata *nd)
676 {
677 if (nd && nd->flags & LOOKUP_RCU)
678 return -ECHILD;
679
680 return __sdfat_revalidate(dentry);
681 }
682
683 static int sdfat_revalidate_ci(struct dentry *dentry, struct nameidata *nd)
684 {
685 if (nd && nd->flags & LOOKUP_RCU)
686 return -ECHILD;
687
688 return __sdfat_revalidate_ci(dentry, nd ? nd->flags : 0);
689
690 }
691
692 static struct inode *sdfat_iget(struct super_block *sb, loff_t i_pos)
693 {
694 struct sdfat_sb_info *sbi = SDFAT_SB(sb);
695 struct sdfat_inode_info *info;
696 struct hlist_node *node;
697 struct hlist_head *head = sbi->inode_hashtable + sdfat_hash(i_pos);
698 struct inode *inode = NULL;
699
700 spin_lock(&sbi->inode_hash_lock);
701 hlist_for_each_entry(info, node, head, i_hash_fat) {
702 BUG_ON(info->vfs_inode.i_sb != sb);
703
704 if (i_pos != info->i_pos)
705 continue;
706 inode = igrab(&info->vfs_inode);
707 if (inode)
708 break;
709 }
710 spin_unlock(&sbi->inode_hash_lock);
711 return inode;
712 }
713 #endif
714
715
716 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0)
717 static struct dentry *sdfat_lookup(struct inode *dir, struct dentry *dentry,
718 unsigned int flags)
719 {
720 return __sdfat_lookup(dir, dentry);
721 }
722 #else /* LINUX_VERSION_CODE < KERNEL_VERSION(3, 6, 0) */
723 static struct dentry *sdfat_lookup(struct inode *dir, struct dentry *dentry,
724 struct nameidata *nd)
725 {
726 return __sdfat_lookup(dir, dentry);
727 }
728 #endif
729
730
731 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 5, 0)
732 /* NOTHING NOW */
733 #else /* LINUX_VERSION_CODE < KERNEL_VERSION(3, 5, 0) */
734 #define GLOBAL_ROOT_UID (0)
735 #define GLOBAL_ROOT_GID (0)
736
737 static inline bool uid_eq(uid_t left, uid_t right)
738 {
739 return left == right;
740 }
741
742 static inline bool gid_eq(gid_t left, gid_t right)
743 {
744 return left == right;
745 }
746
747 static inline uid_t from_kuid_munged(struct user_namespace *to, uid_t kuid)
748 {
749 return kuid;
750 }
751
752 static inline gid_t from_kgid_munged(struct user_namespace *to, gid_t kgid)
753 {
754 return kgid;
755 }
756
757 static inline uid_t make_kuid(struct user_namespace *from, uid_t uid)
758 {
759 return uid;
760 }
761
762 static inline gid_t make_kgid(struct user_namespace *from, gid_t gid)
763 {
764 return gid;
765 }
766 #endif
767
768
769 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 4, 0)
770 static struct dentry *__d_make_root(struct inode *root_inode)
771 {
772 return d_make_root(root_inode);
773 }
774
775 static void __sdfat_do_truncate(struct inode *inode, loff_t old, loff_t new)
776 {
777 down_write(&SDFAT_I(inode)->truncate_lock);
778 truncate_setsize(inode, new);
779 sdfat_truncate(inode, old);
780 up_write(&SDFAT_I(inode)->truncate_lock);
781 }
782
783 static sector_t sdfat_aop_bmap(struct address_space *mapping, sector_t block)
784 {
785 sector_t blocknr;
786
787 /* sdfat_get_cluster() assumes the requested blocknr isn't truncated. */
788 down_read(&SDFAT_I(mapping->host)->truncate_lock);
789 blocknr = generic_block_bmap(mapping, block, sdfat_get_block);
790 up_read(&SDFAT_I(mapping->host)->truncate_lock);
791 return blocknr;
792 }
793
794 static int sdfat_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
795 {
796 return __sdfat_mkdir(dir, dentry);
797 }
798
799 static int sdfat_show_options(struct seq_file *m, struct dentry *root)
800 {
801 return __sdfat_show_options(m, root->d_sb);
802 }
803 #else /* LINUX_VERSION_CODE < KERNEL_VERSION(3, 4, 0) */
804 static inline void set_nlink(struct inode *inode, unsigned int nlink)
805 {
806 inode->i_nlink = nlink;
807 }
808
809 static struct dentry *__d_make_root(struct inode *root_inode)
810 {
811 return d_alloc_root(root_inode);
812 }
813
814 static void __sdfat_do_truncate(struct inode *inode, loff_t old, loff_t new)
815 {
816 truncate_setsize(inode, new);
817 sdfat_truncate(inode, old);
818 }
819
820 static sector_t sdfat_aop_bmap(struct address_space *mapping, sector_t block)
821 {
822 sector_t blocknr;
823
824 /* sdfat_get_cluster() assumes the requested blocknr isn't truncated. */
825 down_read(&mapping->host->i_alloc_sem);
826 blocknr = generic_block_bmap(mapping, block, sdfat_get_block);
827 up_read(&mapping->host->i_alloc_sem);
828 return blocknr;
829 }
830
831 static int sdfat_mkdir(struct inode *dir, struct dentry *dentry, int mode)
832 {
833 return __sdfat_mkdir(dir, dentry);
834 }
835
836 static int sdfat_show_options(struct seq_file *m, struct vfsmount *mnt)
837 {
838 return __sdfat_show_options(m, mnt->mnt_sb);
839 }
840 #endif
841
842
843 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 1, 0)
844 #define __sdfat_generic_file_fsync(filp, start, end, datasync) \
845 generic_file_fsync(filp, start, end, datasync)
846
847 static int sdfat_file_fsync(struct file *filp, loff_t start, loff_t end, int datasync)
848 {
849 return __sdfat_file_fsync(filp, start, end, datasync);
850 }
851 #else /* LINUX_VERSION_CODE < KERNEL_VERSION(3, 1, 0) */
852 #define __sdfat_generic_file_fsync(filp, start, end, datasync) \
853 generic_file_fsync(filp, datasync)
854 static int sdfat_file_fsync(struct file *filp, int datasync)
855 {
856 return __sdfat_file_fsync(filp, 0, 0, datasync);
857 }
858 #endif
859
860 /*************************************************************************
861 * MORE FUNCTIONS WHICH HAS KERNEL VERSION DEPENDENCY
862 *************************************************************************/
863 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 8, 0)
864 static int sdfat_cmp(const struct dentry *dentry,
865 unsigned int len, const char *str, const struct qstr *name)
866 {
867 return __sdfat_cmp(dentry, len, str, name);
868 }
869
870 static int sdfat_cmpi(const struct dentry *dentry,
871 unsigned int len, const char *str, const struct qstr *name)
872 {
873 return __sdfat_cmpi(dentry, len, str, name);
874 }
875 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(3, 14, 0)
876 static int sdfat_cmp(const struct dentry *parent, const struct dentry *dentry,
877 unsigned int len, const char *str, const struct qstr *name)
878 {
879 return __sdfat_cmp(dentry, len, str, name);
880 }
881
882 static int sdfat_cmpi(const struct dentry *parent, const struct dentry *dentry,
883 unsigned int len, const char *str, const struct qstr *name)
884 {
885 return __sdfat_cmpi(dentry, len, str, name);
886 }
887 #else
888 static int sdfat_cmp(const struct dentry *parent, const struct inode *pinode,
889 const struct dentry *dentry, const struct inode *inode,
890 unsigned int len, const char *str, const struct qstr *name)
891 {
892 return __sdfat_cmp(dentry, len, str, name);
893 }
894
895 static int sdfat_cmpi(const struct dentry *parent, const struct inode *pinode,
896 const struct dentry *dentry, const struct inode *inode,
897 unsigned int len, const char *str, const struct qstr *name)
898 {
899 return __sdfat_cmpi(dentry, len, str, name);
900 }
901 #endif
902
903 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 5, 0)
904 static const char *sdfat_follow_link(struct dentry *dentry, struct inode *inode, struct delayed_call *done)
905 {
906 struct sdfat_inode_info *ei = SDFAT_I(inode);
907
908 return (char *)(ei->target);
909 }
910 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0)
911 static const char *sdfat_follow_link(struct dentry *dentry, void **cookie)
912 {
913 struct sdfat_inode_info *ei = SDFAT_I(dentry->d_inode);
914
915 return *cookie = (char *)(ei->target);
916 }
917 #else
918 static void *sdfat_follow_link(struct dentry *dentry, struct nameidata *nd)
919 {
920 struct sdfat_inode_info *ei = SDFAT_I(dentry->d_inode);
921
922 nd_set_link(nd, (char *)(ei->target));
923 return NULL;
924 }
925 #endif
926
927
928 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 7, 0)
929 static ssize_t sdfat_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
930 {
931 struct file *file = iocb->ki_filp;
932 struct address_space *mapping = file->f_mapping;
933 struct inode *inode = mapping->host;
934 size_t count = iov_iter_count(iter);
935 int rw = iov_iter_rw(iter);
936 loff_t offset = iocb->ki_pos;
937
938 return __sdfat_direct_IO(rw, iocb, inode,
939 (void *)iter, offset, count, 0 /* UNUSED */);
940 }
941 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(4, 1, 0)
942 static ssize_t sdfat_direct_IO(struct kiocb *iocb,
943 struct iov_iter *iter,
944 loff_t offset)
945 {
946 struct file *file = iocb->ki_filp;
947 struct address_space *mapping = file->f_mapping;
948 struct inode *inode = mapping->host;
949 size_t count = iov_iter_count(iter);
950 int rw = iov_iter_rw(iter);
951
952 return __sdfat_direct_IO(rw, iocb, inode,
953 (void *)iter, offset, count, 0 /* UNUSED */);
954 }
955 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(3, 16, 0)
956 static ssize_t sdfat_direct_IO(int rw, struct kiocb *iocb,
957 struct iov_iter *iter,
958 loff_t offset)
959 {
960 struct file *file = iocb->ki_filp;
961 struct address_space *mapping = file->f_mapping;
962 struct inode *inode = mapping->host;
963 size_t count = iov_iter_count(iter);
964
965 return __sdfat_direct_IO(rw, iocb, inode,
966 (void *)iter, offset, count, 0 /* UNUSED */);
967 }
968 #else
969 static ssize_t sdfat_direct_IO(int rw, struct kiocb *iocb,
970 const struct iovec *iov, loff_t offset, unsigned long nr_segs)
971 {
972 struct file *file = iocb->ki_filp;
973 struct address_space *mapping = file->f_mapping;
974 struct inode *inode = mapping->host;
975 size_t count = iov_length(iov, nr_segs);
976
977 return __sdfat_direct_IO(rw, iocb, inode,
978 (void *)iov, offset, count, nr_segs);
979 }
980 #endif
981
982
983 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 7, 0)
984 static inline ssize_t __sdfat_blkdev_direct_IO(int unused, struct kiocb *iocb,
985 struct inode *inode, void *iov_u, loff_t unused_1,
986 unsigned long nr_segs)
987 {
988 struct iov_iter *iter = (struct iov_iter *)iov_u;
989
990 return blockdev_direct_IO(iocb, inode, iter, sdfat_get_block);
991 }
992 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(4, 1, 0)
993 static inline ssize_t __sdfat_blkdev_direct_IO(int unused, struct kiocb *iocb,
994 struct inode *inode, void *iov_u, loff_t offset,
995 unsigned long nr_segs)
996 {
997 struct iov_iter *iter = (struct iov_iter *)iov_u;
998
999 return blockdev_direct_IO(iocb, inode, iter, offset, sdfat_get_block);
1000 }
1001 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(3, 16, 0)
1002 static inline ssize_t __sdfat_blkdev_direct_IO(int rw, struct kiocb *iocb,
1003 struct inode *inode, void *iov_u, loff_t offset,
1004 unsigned long nr_segs)
1005 {
1006 struct iov_iter *iter = (struct iov_iter *)iov_u;
1007
1008 return blockdev_direct_IO(rw, iocb, inode, iter,
1009 offset, sdfat_get_block);
1010 }
1011 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(3, 4, 0)
1012 static inline ssize_t __sdfat_blkdev_direct_IO(int rw, struct kiocb *iocb,
1013 struct inode *inode, void *iov_u, loff_t offset,
1014 unsigned long nr_segs)
1015 {
1016 const struct iovec *iov = (const struct iovec *)iov_u;
1017
1018 return blockdev_direct_IO(rw, iocb, inode, iov,
1019 offset, nr_segs, sdfat_get_block);
1020 }
1021 #else
1022 static inline ssize_t __sdfat_blkdev_direct_IO(int rw, struct kiocb *iocb,
1023 struct inode *inode, void *iov_u, loff_t offset,
1024 unsigned long nr_segs)
1025 {
1026 const struct iovec *iov = (const struct iovec *)iov_u;
1027
1028 return blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
1029 offset, nr_segs, sdfat_get_block, NULL);
1030 }
1031 #endif
1032
1033
1034 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0)
1035 static int sdfat_create(struct inode *dir, struct dentry *dentry, umode_t mode,
1036 bool excl)
1037 {
1038 return __sdfat_create(dir, dentry);
1039 }
1040 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(3, 4, 0)
1041 static int sdfat_create(struct inode *dir, struct dentry *dentry, umode_t mode,
1042 struct nameidata *nd)
1043 {
1044 return __sdfat_create(dir, dentry);
1045 }
1046 #else
1047 static int sdfat_create(struct inode *dir, struct dentry *dentry, int mode,
1048 struct nameidata *nd)
1049 {
1050 return __sdfat_create(dir, dentry);
1051 }
1052 #endif
1053
1054
1055 /*************************************************************************
1056 * WRAP FUNCTIONS FOR DEBUGGING
1057 *************************************************************************/
1058 #ifdef CONFIG_SDFAT_TRACE_SB_LOCK
1059 static inline void __lock_super(struct super_block *sb)
1060 {
1061 lock_super(sb);
1062 __lock_jiffies = jiffies;
1063 }
1064
1065 static inline void __unlock_super(struct super_block *sb)
1066 {
1067 int time = ((jiffies - __lock_jiffies) * 1000 / HZ);
1068 /* FIXME : error message should be modified */
1069 if (time > 10)
1070 EMSG("lock_super in %s (%d ms)\n", __func__, time);
1071
1072 unlock_super(sb);
1073 }
1074 #else /* CONFIG_SDFAT_TRACE_SB_LOCK */
1075 static inline void __lock_super(struct super_block *sb)
1076 {
1077 lock_super(sb);
1078 }
1079
1080 static inline void __unlock_super(struct super_block *sb)
1081 {
1082 unlock_super(sb);
1083 }
1084 #endif /* CONFIG_SDFAT_TRACE_SB_LOCK */
1085
1086 /*************************************************************************
1087 * NORMAL FUNCTIONS
1088 *************************************************************************/
1089 static inline loff_t sdfat_make_i_pos(FILE_ID_T *fid)
1090 {
1091 return ((loff_t) fid->dir.dir << 32) | (fid->entry & 0xffffffff);
1092 }
1093
1094 /*======================================================================*/
1095 /* Directory Entry Name Buffer Operations */
1096 /*======================================================================*/
1097 static void sdfat_init_namebuf(DENTRY_NAMEBUF_T *nb)
1098 {
1099 nb->lfn = NULL;
1100 nb->sfn = NULL;
1101 nb->lfnbuf_len = 0;
1102 nb->sfnbuf_len = 0;
1103 }
1104
1105 static int sdfat_alloc_namebuf(DENTRY_NAMEBUF_T *nb)
1106 {
1107 nb->lfn = __getname();
1108 if (!nb->lfn)
1109 return -ENOMEM;
1110 nb->sfn = nb->lfn + MAX_VFSNAME_BUF_SIZE;
1111 nb->lfnbuf_len = MAX_VFSNAME_BUF_SIZE;
1112 nb->sfnbuf_len = MAX_VFSNAME_BUF_SIZE;
1113 return 0;
1114 }
1115
1116 static void sdfat_free_namebuf(DENTRY_NAMEBUF_T *nb)
1117 {
1118 if (!nb->lfn)
1119 return;
1120
1121 __putname(nb->lfn);
1122 sdfat_init_namebuf(nb);
1123 }
1124
1125 /*======================================================================*/
1126 /* Directory Entry Operations */
1127 /*======================================================================*/
1128 #define SDFAT_DSTATE_LOCKED (void *)(0xCAFE2016)
1129 #define SDFAT_DSTATE_UNLOCKED (void *)(0x00000000)
1130
1131 static inline void __lock_d_revalidate(struct dentry *dentry)
1132 {
1133 spin_lock(&dentry->d_lock);
1134 dentry->d_fsdata = SDFAT_DSTATE_LOCKED;
1135 spin_unlock(&dentry->d_lock);
1136 }
1137
1138 static inline void __unlock_d_revalidate(struct dentry *dentry)
1139 {
1140 spin_lock(&dentry->d_lock);
1141 dentry->d_fsdata = SDFAT_DSTATE_UNLOCKED;
1142 spin_unlock(&dentry->d_lock);
1143 }
1144
1145 /* __check_dstate_locked requires dentry->d_lock */
1146 static inline int __check_dstate_locked(struct dentry *dentry)
1147 {
1148 if (dentry->d_fsdata == SDFAT_DSTATE_LOCKED)
1149 return 1;
1150
1151 return 0;
1152 }
1153
1154 /*
1155 * If new entry was created in the parent, it could create the 8.3
1156 * alias (the shortname of logname). So, the parent may have the
1157 * negative-dentry which matches the created 8.3 alias.
1158 *
1159 * If it happened, the negative dentry isn't actually negative
1160 * anymore. So, drop it.
1161 */
1162 static int __sdfat_revalidate_common(struct dentry *dentry)
1163 {
1164 int ret = 1;
1165
1166 spin_lock(&dentry->d_lock);
1167 if ((!dentry->d_inode) && (!__check_dstate_locked(dentry) &&
1168 (dentry->d_time != dentry->d_parent->d_inode->i_version))) {
1169 ret = 0;
1170 }
1171 spin_unlock(&dentry->d_lock);
1172 return ret;
1173 }
1174
1175 static int __sdfat_revalidate(struct dentry *dentry)
1176 {
1177 /* This is not negative dentry. Always valid. */
1178 if (dentry->d_inode)
1179 return 1;
1180 return __sdfat_revalidate_common(dentry);
1181 }
1182
1183 static int __sdfat_revalidate_ci(struct dentry *dentry, unsigned int flags)
1184 {
1185 /*
1186 * This is not negative dentry. Always valid.
1187 *
1188 * Note, rename() to existing directory entry will have ->d_inode,
1189 * and will use existing name which isn't specified name by user.
1190 *
1191 * We may be able to drop this positive dentry here. But dropping
1192 * positive dentry isn't good idea. So it's unsupported like
1193 * rename("filename", "FILENAME") for now.
1194 */
1195 if (dentry->d_inode)
1196 return 1;
1197 #if 0 /* Blocked below code for lookup_one_len() called by stackable FS */
1198 /*
1199 * This may be nfsd (or something), anyway, we can't see the
1200 * intent of this. So, since this can be for creation, drop it.
1201 */
1202 if (!flags)
1203 return 0;
1204 #endif
1205 /*
1206 * Drop the negative dentry, in order to make sure to use the
1207 * case sensitive name which is specified by user if this is
1208 * for creation.
1209 */
1210 if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET))
1211 return 0;
1212 return __sdfat_revalidate_common(dentry);
1213 }
1214
1215
1216 /* returns the length of a struct qstr, ignoring trailing dots */
1217 static unsigned int __sdfat_striptail_len(unsigned int len, const char *name)
1218 {
1219 while (len && name[len - 1] == '.')
1220 len--;
1221 return len;
1222 }
1223
1224 static unsigned int sdfat_striptail_len(const struct qstr *qstr)
1225 {
1226 return __sdfat_striptail_len(qstr->len, qstr->name);
1227 }
1228
1229 /*
1230 * Compute the hash for the sdfat name corresponding to the dentry.
1231 * Note: if the name is invalid, we leave the hash code unchanged so
1232 * that the existing dentry can be used. The sdfat fs routines will
1233 * return ENOENT or EINVAL as appropriate.
1234 */
1235 static int __sdfat_d_hash(const struct dentry *dentry, struct qstr *qstr)
1236 {
1237 unsigned int len = sdfat_striptail_len(qstr);
1238
1239 qstr->hash = __sdfat_full_name_hash(dentry, qstr->name, len);
1240 return 0;
1241 }
1242
1243 /*
1244 * Compute the hash for the sdfat name corresponding to the dentry.
1245 * Note: if the name is invalid, we leave the hash code unchanged so
1246 * that the existing dentry can be used. The sdfat fs routines will
1247 * return ENOENT or EINVAL as appropriate.
1248 */
1249 static int __sdfat_d_hashi(const struct dentry *dentry, struct qstr *qstr)
1250 {
1251 struct nls_table *t = SDFAT_SB(dentry->d_sb)->nls_io;
1252 const unsigned char *name;
1253 unsigned int len;
1254 unsigned long hash;
1255
1256 name = qstr->name;
1257 len = sdfat_striptail_len(qstr);
1258
1259 hash = __sdfat_init_name_hash(dentry);
1260 while (len--)
1261 hash = partial_name_hash(nls_tolower(t, *name++), hash);
1262 qstr->hash = end_name_hash(hash);
1263
1264 return 0;
1265 }
1266
1267 /*
1268 * Case sensitive compare of two sdfat names.
1269 */
1270 static int __sdfat_cmp(const struct dentry *dentry, unsigned int len,
1271 const char *str, const struct qstr *name)
1272 {
1273 unsigned int alen, blen;
1274
1275 /* A filename cannot end in '.' or we treat it like it has none */
1276 alen = sdfat_striptail_len(name);
1277 blen = __sdfat_striptail_len(len, str);
1278 if (alen == blen) {
1279 if (strncmp(name->name, str, alen) == 0)
1280 return 0;
1281 }
1282 return 1;
1283 }
1284
1285 /*
1286 * Case insensitive compare of two sdfat names.
1287 */
1288 static int __sdfat_cmpi(const struct dentry *dentry, unsigned int len,
1289 const char *str, const struct qstr *name)
1290 {
1291 struct nls_table *t = SDFAT_SB(dentry->d_sb)->nls_io;
1292 unsigned int alen, blen;
1293
1294 /* A filename cannot end in '.' or we treat it like it has none */
1295 alen = sdfat_striptail_len(name);
1296 blen = __sdfat_striptail_len(len, str);
1297 if (alen == blen) {
1298 if (nls_strnicmp(t, name->name, str, alen) == 0)
1299 return 0;
1300 }
1301 return 1;
1302 }
1303
1304 static const struct dentry_operations sdfat_dentry_ops = {
1305 .d_revalidate = sdfat_revalidate,
1306 .d_hash = sdfat_d_hash,
1307 .d_compare = sdfat_cmp,
1308 };
1309
1310 static const struct dentry_operations sdfat_ci_dentry_ops = {
1311 .d_revalidate = sdfat_revalidate_ci,
1312 .d_hash = sdfat_d_hashi,
1313 .d_compare = sdfat_cmpi,
1314 };
1315
1316 #ifdef CONFIG_SDFAT_DFR
1317 /*----------------------------------------------------------------------*/
1318 /* Defragmentation related */
1319 /*----------------------------------------------------------------------*/
1320 /**
1321 * @fn defrag_cleanup_reqs
1322 * @brief clean-up defrag info depending on error flag
1323 * @return void
1324 * @param sb super block
1325 * @param error error flag
1326 */
1327 static void defrag_cleanup_reqs(INOUT struct super_block *sb, IN int error)
1328 {
1329 struct sdfat_sb_info *sbi = SDFAT_SB(sb);
1330 struct defrag_info *sb_dfr = &(sbi->dfr_info);
1331 struct defrag_info *ino_dfr = NULL, *tmp = NULL;
1332 /* sdfat patch 0.96 : sbi->dfr_info crash problem */
1333 __lock_super(sb);
1334
1335 /* Clean-up ino_dfr */
1336 if (!error) {
1337 list_for_each_entry_safe(ino_dfr, tmp, &sb_dfr->entry, entry) {
1338 struct inode *inode = &(container_of(ino_dfr, struct sdfat_inode_info, dfr_info)->vfs_inode);
1339
1340 mutex_lock(&ino_dfr->lock);
1341
1342 atomic_set(&ino_dfr->stat, DFR_INO_STAT_IDLE);
1343
1344 list_del(&ino_dfr->entry);
1345
1346 ino_dfr->chunks = NULL;
1347 ino_dfr->nr_chunks = 0;
1348 INIT_LIST_HEAD(&ino_dfr->entry);
1349
1350 BUG_ON(!mutex_is_locked(&ino_dfr->lock));
1351 mutex_unlock(&ino_dfr->lock);
1352
1353 iput(inode);
1354 }
1355 }
1356
1357 /* Clean-up sb_dfr */
1358 sb_dfr->chunks = NULL;
1359 sb_dfr->nr_chunks = 0;
1360 INIT_LIST_HEAD(&sb_dfr->entry);
1361
1362 /* Clear dfr_new_clus page */
1363 memset(sbi->dfr_new_clus, 0, PAGE_SIZE);
1364 sbi->dfr_new_idx = 1;
1365 memset(sbi->dfr_page_wb, 0, PAGE_SIZE);
1366
1367 sbi->dfr_hint_clus = sbi->dfr_hint_idx = sbi->dfr_reserved_clus = 0;
1368
1369 __unlock_super(sb);
1370 }
1371
1372 /**
1373 * @fn defrag_validate_pages
1374 * @brief validate and mark dirty for victiim pages
1375 * @return 0 on success, -errno otherwise
1376 * @param inode inode
1377 * @param chunk given chunk
1378 * @remark protected by inode_lock and super_lock
1379 */
1380 static int
1381 defrag_validate_pages(
1382 IN struct inode *inode,
1383 IN struct defrag_chunk_info *chunk)
1384 {
1385 struct super_block *sb = inode->i_sb;
1386 struct sdfat_sb_info *sbi = SDFAT_SB(sb);
1387 struct page *page = NULL;
1388 unsigned int i_size = 0, page_off = 0, page_nr = 0;
1389 int buf_i = 0, i = 0, err = 0;
1390
1391 i_size = i_size_read(inode);
1392 page_off = chunk->f_clus * PAGES_PER_CLUS(sb);
1393 page_nr = (i_size / PAGE_SIZE) + ((i_size % PAGE_SIZE) ? 1 : 0);
1394 if ((i_size <= 0) || (page_nr <= 0)) {
1395 dfr_err("inode %p, i_size %d, page_nr %d", inode, i_size, page_nr);
1396 return -EINVAL;
1397 }
1398
1399 /* Get victim pages
1400 * and check its dirty/writeback/mapped state
1401 */
1402 for (i = 0;
1403 i < min((int)(page_nr - page_off), (int)(chunk->nr_clus * PAGES_PER_CLUS(sb)));
1404 i++) {
1405 page = find_get_page(inode->i_mapping, page_off + i);
1406 if (page)
1407 if (!trylock_page(page)) {
1408 put_page(page);
1409 page = NULL;
1410 }
1411
1412 if (!page) {
1413 dfr_debug("get/lock_page() failed, index %d", i);
1414 err = -EINVAL;
1415 goto error;
1416 }
1417
1418 sbi->dfr_pagep[buf_i++] = page;
1419 if (PageError(page) || !PageUptodate(page) || PageDirty(page) ||
1420 PageWriteback(page) || page_mapped(page)) {
1421 dfr_debug("page %p, err %d, uptodate %d, "
1422 "dirty %d, wb %d, mapped %d",
1423 page, PageError(page), PageUptodate(page),
1424 PageDirty(page), PageWriteback(page),
1425 page_mapped(page));
1426 err = -EINVAL;
1427 goto error;
1428 }
1429
1430 set_bit((page->index & (PAGES_PER_CLUS(sb) - 1)),
1431 (volatile unsigned long *)&(sbi->dfr_page_wb[chunk->new_idx + i / PAGES_PER_CLUS(sb)]));
1432
1433 page = NULL;
1434 }
1435
1436 /**
1437 * All pages in the chunks are valid.
1438 */
1439 i_size -= (chunk->f_clus * (sbi->fsi.cluster_size));
1440 BUG_ON(((i_size / PAGE_SIZE) + ((i_size % PAGE_SIZE) ? 1 : 0)) != (page_nr - page_off));
1441
1442 for (i = 0; i < buf_i; i++) {
1443 struct buffer_head *bh = NULL, *head = NULL;
1444 int bh_idx = 0;
1445
1446 page = sbi->dfr_pagep[i];
1447 BUG_ON(!page);
1448
1449 /* Mark dirty in page */
1450 set_page_dirty(page);
1451 mark_page_accessed(page);
1452
1453 /* Attach empty BHs */
1454 if (!page_has_buffers(page))
1455 create_empty_buffers(page, 1 << inode->i_blkbits, 0);
1456
1457 /* Mark dirty in BHs */
1458 bh = head = page_buffers(page);
1459 BUG_ON(!bh && !i_size);
1460 do {
1461 if ((bh_idx >= 1) && (bh_idx >= (i_size >> inode->i_blkbits))) {
1462 clear_buffer_dirty(bh);
1463 } else {
1464 if (PageUptodate(page))
1465 if (!buffer_uptodate(bh))
1466 set_buffer_uptodate(bh);
1467
1468 /* Set this bh as delay */
1469 set_buffer_new(bh);
1470 set_buffer_delay(bh);
1471
1472 mark_buffer_dirty(bh);
1473 }
1474
1475 bh_idx++;
1476 bh = bh->b_this_page;
1477 } while (bh != head);
1478
1479 /* Mark this page accessed */
1480 mark_page_accessed(page);
1481
1482 i_size -= PAGE_SIZE;
1483 }
1484
1485 error:
1486 /* Unlock and put refs for pages */
1487 for (i = 0; i < buf_i; i++) {
1488 BUG_ON(!sbi->dfr_pagep[i]);
1489 unlock_page(sbi->dfr_pagep[i]);
1490 put_page(sbi->dfr_pagep[i]);
1491 }
1492 memset(sbi->dfr_pagep, 0, sizeof(PAGE_SIZE));
1493
1494 return err;
1495 }
1496
1497
1498 /**
1499 * @fn defrag_validate_reqs
1500 * @brief validate defrag requests
1501 * @return negative if all requests not valid, 0 otherwise
1502 * @param sb super block
1503 * @param chunks given chunks
1504 */
1505 static int
1506 defrag_validate_reqs(
1507 IN struct super_block *sb,
1508 INOUT struct defrag_chunk_info *chunks)
1509 {
1510 struct sdfat_sb_info *sbi = SDFAT_SB(sb);
1511 struct defrag_info *sb_dfr = &(sbi->dfr_info);
1512 int i = 0, err = 0, err_cnt = 0;
1513
1514 /* Validate all reqs */
1515 for (i = REQ_HEADER_IDX + 1; i < sb_dfr->nr_chunks; i++) {
1516 struct defrag_chunk_info *chunk = NULL;
1517 struct inode *inode = NULL;
1518 struct defrag_info *ino_dfr = NULL;
1519
1520 chunk = &chunks[i];
1521
1522 /* Check inode */
1523 __lock_super(sb);
1524 inode = sdfat_iget(sb, chunk->i_pos);
1525 if (!inode) {
1526 dfr_debug("inode not found, i_pos %08llx", chunk->i_pos);
1527 chunk->stat = DFR_CHUNK_STAT_ERR;
1528 err_cnt++;
1529 __unlock_super(sb);
1530 continue;
1531 }
1532 __unlock_super(sb);
1533
1534 dfr_debug("req[%d] inode %p, i_pos %08llx, f_clus %d, "
1535 "d_clus %08x, nr %d, prev %08x, next %08x",
1536 i, inode, chunk->i_pos, chunk->f_clus, chunk->d_clus,
1537 chunk->nr_clus, chunk->prev_clus, chunk->next_clus);
1538 /**
1539 * Lock ordering: inode_lock -> lock_super
1540 */
1541 inode_lock(inode);
1542 __lock_super(sb);
1543
1544 /* Check if enough buffers exist for chunk->new_idx */
1545 if ((sbi->dfr_new_idx + chunk->nr_clus) >= (PAGE_SIZE / sizeof(int))) {
1546 dfr_err("dfr_new_idx %d, chunk->nr_clus %d",
1547 sbi->dfr_new_idx, chunk->nr_clus);
1548 err = -ENOSPC;
1549 goto unlock;
1550 }
1551
1552 /* Reserve clusters for defrag with DA */
1553 err = fsapi_dfr_reserve_clus(sb, chunk->nr_clus);
1554 if (err)
1555 goto unlock;
1556
1557 /* Check clusters */
1558 err = fsapi_dfr_validate_clus(inode, chunk, 0);
1559 if (err) {
1560 fsapi_dfr_reserve_clus(sb, 0 - chunk->nr_clus);
1561 dfr_debug("Cluster validation: err %d", err);
1562 goto unlock;
1563 }
1564
1565 /* Check pages */
1566 err = defrag_validate_pages(inode, chunk);
1567 if (err) {
1568 fsapi_dfr_reserve_clus(sb, 0 - chunk->nr_clus);
1569 dfr_debug("Page validation: err %d", err);
1570 goto unlock;
1571 }
1572
1573 /* Mark IGNORE flag to victim AU */
1574 if (sbi->options.improved_allocation & SDFAT_ALLOC_SMART)
1575 fsapi_dfr_mark_ignore(sb, chunk->d_clus);
1576
1577 ino_dfr = &(SDFAT_I(inode)->dfr_info);
1578 mutex_lock(&ino_dfr->lock);
1579
1580 /* Update chunk info */
1581 chunk->stat = DFR_CHUNK_STAT_REQ;
1582 chunk->new_idx = sbi->dfr_new_idx;
1583
1584 /* Update ino_dfr info */
1585 if (list_empty(&(ino_dfr->entry))) {
1586 list_add_tail(&ino_dfr->entry, &sb_dfr->entry);
1587 ino_dfr->chunks = chunk;
1588 igrab(inode);
1589 }
1590 ino_dfr->nr_chunks++;
1591
1592 atomic_set(&ino_dfr->stat, DFR_INO_STAT_REQ);
1593
1594 BUG_ON(!mutex_is_locked(&ino_dfr->lock));
1595 mutex_unlock(&ino_dfr->lock);
1596
1597 /* Reserved buffers for chunk->new_idx */
1598 sbi->dfr_new_idx += chunk->nr_clus;
1599
1600 unlock:
1601 if (err) {
1602 chunk->stat = DFR_CHUNK_STAT_ERR;
1603 err_cnt++;
1604 }
1605 iput(inode);
1606 __unlock_super(sb);
1607 inode_unlock(inode);
1608 }
1609
1610 /* Return error if all chunks are invalid */
1611 if (err_cnt == sb_dfr->nr_chunks - 1) {
1612 dfr_debug("%s failed (err_cnt %d)", __func__, err_cnt);
1613 return -ENXIO;
1614 }
1615
1616 return 0;
1617 }
1618
1619
1620 /**
1621 * @fn defrag_check_fs_busy
1622 * @brief check if this module busy
1623 * @return 0 when idle, 1 otherwise
1624 * @param sb super block
1625 * @param reserved_clus # of reserved clusters
1626 * @param queued_pages # of queued pages
1627 */
1628 static int
1629 defrag_check_fs_busy(
1630 IN struct super_block *sb,
1631 OUT int *reserved_clus,
1632 OUT int *queued_pages)
1633 {
1634 FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
1635 int err = 0;
1636
1637 *reserved_clus = *queued_pages = 0;
1638
1639 __lock_super(sb);
1640 *reserved_clus = fsi->reserved_clusters;
1641 *queued_pages = atomic_read(&SDFAT_SB(sb)->stat_n_pages_queued);
1642
1643 if (*reserved_clus || *queued_pages)
1644 err = 1;
1645 __unlock_super(sb);
1646
1647 return err;
1648 }
1649
1650
1651 /**
1652 * @fn sdfat_ioctl_defrag_req
1653 * @brief ioctl to send defrag requests
1654 * @return 0 on success, -errno otherwise
1655 * @param inode inode
1656 * @param uarg given requests
1657 */
1658 static int
1659 sdfat_ioctl_defrag_req(
1660 IN struct inode *inode,
1661 INOUT unsigned int *uarg)
1662 {
1663 struct super_block *sb = inode->i_sb;
1664 struct sdfat_sb_info *sbi = SDFAT_SB(sb);
1665 struct defrag_info *sb_dfr = &(sbi->dfr_info);
1666 struct defrag_chunk_header head;
1667 struct defrag_chunk_info *chunks = NULL;
1668 unsigned int len = 0;
1669 int err = 0;
1670 unsigned long timeout = 0;
1671
1672 /* Check overlapped defrag */
1673 if (atomic_cmpxchg(&sb_dfr->stat, DFR_SB_STAT_IDLE, DFR_SB_STAT_REQ)) {
1674 dfr_debug("sb_dfr->stat %d", atomic_read(&sb_dfr->stat));
1675 return -EBUSY;
1676 }
1677
1678 /* Check if defrag required */
1679 __lock_super(sb);
1680 if (!fsapi_dfr_check_dfr_required(sb, NULL, NULL, NULL)) {
1681 dfr_debug("Not enough space left for defrag (err %d)", -ENOSPC);
1682 atomic_set(&sb_dfr->stat, DFR_SB_STAT_IDLE);
1683 __unlock_super(sb);
1684 return -ENOSPC;
1685 }
1686 __unlock_super(sb);
1687
1688 /* Copy args */
1689 memset(&head, 0, sizeof(struct defrag_chunk_header));
1690 err = copy_from_user(&head, uarg, sizeof(struct defrag_chunk_info));
1691 ERR_HANDLE(err);
1692
1693 /* If FS busy, cancel defrag */
1694 if (!(head.mode == DFR_MODE_TEST)) {
1695 int reserved_clus = 0, queued_pages = 0;
1696
1697 err = defrag_check_fs_busy(sb, &reserved_clus, &queued_pages);
1698 if (err) {
1699 dfr_debug("FS busy, cancel defrag (reserved_clus %d, queued_pages %d)",
1700 reserved_clus, queued_pages);
1701 err = -EBUSY;
1702 goto error;
1703 }
1704 }
1705
1706 /* Total length is saved in the chunk header's nr_chunks field */
1707 len = head.nr_chunks;
1708 ERR_HANDLE2(!len, err, -EINVAL);
1709
1710 dfr_debug("IOC_DFR_REQ started (mode %d, nr_req %d)", head.mode, len - 1);
1711 if (get_order(len * sizeof(struct defrag_chunk_info)) > MAX_ORDER) {
1712 dfr_debug("len %u, sizeof(struct defrag_chunk_info) %lu, MAX_ORDER %d",
1713 len, sizeof(struct defrag_chunk_info), MAX_ORDER);
1714 err = -EINVAL;
1715 goto error;
1716 }
1717 chunks = alloc_pages_exact(len * sizeof(struct defrag_chunk_info),
1718 GFP_KERNEL | __GFP_ZERO);
1719 ERR_HANDLE2(!chunks, err, -ENOMEM)
1720
1721 err = copy_from_user(chunks, uarg, len * sizeof(struct defrag_chunk_info));
1722 ERR_HANDLE(err);
1723
1724 /* Initialize sb_dfr */
1725 sb_dfr->chunks = chunks;
1726 sb_dfr->nr_chunks = len;
1727
1728 /* Validate reqs & mark defrag/dirty */
1729 err = defrag_validate_reqs(sb, sb_dfr->chunks);
1730 ERR_HANDLE(err);
1731
1732 atomic_set(&sb_dfr->stat, DFR_SB_STAT_VALID);
1733
1734 /* Wait for defrag completion */
1735 if (head.mode == DFR_MODE_ONESHOT)
1736 timeout = 0;
1737 else if (head.mode & DFR_MODE_BACKGROUND)
1738 timeout = DFR_DEFAULT_TIMEOUT;
1739 else
1740 timeout = DFR_MIN_TIMEOUT;
1741
1742 dfr_debug("Wait for completion (timeout %ld)", timeout);
1743 init_completion(&sbi->dfr_complete);
1744 timeout = wait_for_completion_timeout(&sbi->dfr_complete, timeout);
1745
1746 if (!timeout) {
1747 /* Force defrag_updat_fat() after timeout. */
1748 dfr_debug("Force sync(), mode %d, left-timeout %ld", head.mode, timeout);
1749
1750 down_read(&sb->s_umount);
1751
1752 sync_inodes_sb(sb);
1753
1754 __lock_super(sb);
1755 fsapi_dfr_update_fat_next(sb);
1756
1757 fsapi_sync_fs(sb, 1);
1758
1759 #ifdef CONFIG_SDFAT_DFR_DEBUG
1760 /* SPO test */
1761 fsapi_dfr_spo_test(sb, DFR_SPO_FAT_NEXT, __func__);
1762 #endif
1763
1764 fsapi_dfr_update_fat_prev(sb, 1);
1765 fsapi_sync_fs(sb, 1);
1766
1767 __unlock_super(sb);
1768
1769 up_read(&sb->s_umount);
1770 }
1771
1772 #ifdef CONFIG_SDFAT_DFR_DEBUG
1773 /* SPO test */
1774 fsapi_dfr_spo_test(sb, DFR_SPO_NORMAL, __func__);
1775 #endif
1776
1777 __lock_super(sb);
1778 /* Send DISCARD to clean-ed AUs */
1779 fsapi_dfr_check_discard(sb);
1780
1781 #ifdef CONFIG_SDFAT_DFR_DEBUG
1782 /* SPO test */
1783 fsapi_dfr_spo_test(sb, DFR_SPO_DISCARD, __func__);
1784 #endif
1785
1786 /* Unmark IGNORE flag to all victim AUs */
1787 fsapi_dfr_unmark_ignore_all(sb);
1788 __unlock_super(sb);
1789
1790 err = copy_to_user(uarg, sb_dfr->chunks, sizeof(struct defrag_chunk_info) * len);
1791 ERR_HANDLE(err);
1792
1793 error:
1794 /* Clean-up sb_dfr & ino_dfr */
1795 defrag_cleanup_reqs(sb, err);
1796
1797 if (chunks)
1798 free_pages_exact(chunks, len * sizeof(struct defrag_chunk_info));
1799
1800 /* Set sb_dfr's state as IDLE */
1801 atomic_set(&sb_dfr->stat, DFR_SB_STAT_IDLE);
1802
1803 dfr_debug("IOC_DFR_REQ done (err %d)", err);
1804 return err;
1805 }
1806
1807 /**
1808 * @fn sdfat_ioctl_defrag_trav
1809 * @brief ioctl to traverse given directory for defrag
1810 * @return 0 on success, -errno otherwise
1811 * @param inode inode
1812 * @param uarg output buffer
1813 */
1814 static int
1815 sdfat_ioctl_defrag_trav(
1816 IN struct inode *inode,
1817 INOUT unsigned int *uarg)
1818 {
1819 struct super_block *sb = inode->i_sb;
1820 struct sdfat_sb_info *sbi = SDFAT_SB(sb);
1821 struct defrag_info *sb_dfr = &(sbi->dfr_info);
1822 struct defrag_trav_arg *args = (struct defrag_trav_arg *) sbi->dfr_pagep;
1823 struct defrag_trav_header *header = (struct defrag_trav_header *) args;
1824 int err = 0;
1825
1826 /* Check overlapped defrag */
1827 if (atomic_cmpxchg(&sb_dfr->stat, DFR_SB_STAT_IDLE, DFR_SB_STAT_REQ)) {
1828 dfr_debug("sb_dfr->stat %d", atomic_read(&sb_dfr->stat));
1829 return -EBUSY;
1830 }
1831
1832 /* Check if defrag required */
1833 __lock_super(sb);
1834 if (!fsapi_dfr_check_dfr_required(sb, NULL, NULL, NULL)) {
1835 dfr_debug("Not enough space left for defrag (err %d)", -ENOSPC);
1836 atomic_set(&sb_dfr->stat, DFR_SB_STAT_IDLE);
1837 __unlock_super(sb);
1838 return -ENOSPC;
1839 }
1840 __unlock_super(sb);
1841
1842 /* Copy args */
1843 err = copy_from_user(args, uarg, PAGE_SIZE);
1844 ERR_HANDLE(err);
1845
1846 /**
1847 * Check args.
1848 * ROOT directory has i_pos = 0 and start_clus = 0 .
1849 */
1850 if (!(header->type & DFR_TRAV_TYPE_HEADER)) {
1851 err = -EINVAL;
1852 dfr_debug("type %d, i_pos %08llx, start_clus %08x",
1853 header->type, header->i_pos, header->start_clus);
1854 goto error;
1855 }
1856
1857 /* If FS busy, cancel defrag */
1858 if (!(header->type & DFR_TRAV_TYPE_TEST)) {
1859 unsigned int reserved_clus = 0, queued_pages = 0;
1860
1861 err = defrag_check_fs_busy(sb, &reserved_clus, &queued_pages);
1862 if (err) {
1863 dfr_debug("FS busy, cancel defrag (reserved_clus %d, queued_pages %d)",
1864 reserved_clus, queued_pages);
1865 err = -EBUSY;
1866 goto error;
1867 }
1868 }
1869
1870 /* Scan given directory and gather info */
1871 inode_lock(inode);
1872 __lock_super(sb);
1873 err = fsapi_dfr_scan_dir(sb, (void *)args);
1874 __unlock_super(sb);
1875 inode_unlock(inode);
1876 ERR_HANDLE(err);
1877
1878 /* Copy the result to user */
1879 err = copy_to_user(uarg, args, PAGE_SIZE);
1880 ERR_HANDLE(err);
1881
1882 error:
1883 memset(sbi->dfr_pagep, 0, PAGE_SIZE);
1884
1885 atomic_set(&sb_dfr->stat, DFR_SB_STAT_IDLE);
1886 return err;
1887 }
1888
1889 /**
1890 * @fn sdfat_ioctl_defrag_info
1891 * @brief ioctl to get HW param info
1892 * @return 0 on success, -errno otherwise
1893 * @param sb super block
1894 * @param uarg output buffer
1895 */
1896 static int
1897 sdfat_ioctl_defrag_info(
1898 IN struct super_block *sb,
1899 OUT unsigned int *uarg)
1900 {
1901 struct defrag_info_arg info_arg;
1902 int err = 0;
1903
1904 memset(&info_arg, 0, sizeof(struct defrag_info_arg));
1905
1906 __lock_super(sb);
1907 err = fsapi_dfr_get_info(sb, &info_arg);
1908 __unlock_super(sb);
1909 ERR_HANDLE(err);
1910 dfr_debug("IOC_DFR_INFO: sec_per_au %d, hidden_sectors %d",
1911 info_arg.sec_per_au, info_arg.hidden_sectors);
1912
1913 err = copy_to_user(uarg, &info_arg, sizeof(struct defrag_info_arg));
1914 error:
1915 return err;
1916 }
1917
1918 #endif /* CONFIG_SDFAT_DFR */
1919
1920 static inline int __do_dfr_map_cluster(struct inode *inode, u32 clu_offset, unsigned int *clus_ptr)
1921 {
1922 #ifdef CONFIG_SDFAT_DFR
1923 return fsapi_dfr_map_clus(inode, clu_offset, clus_ptr);
1924 #else
1925 return 0;
1926 #endif
1927 }
1928
1929 static inline int __check_dfr_on(struct inode *inode, loff_t start, loff_t end, const char *fname)
1930 {
1931 #ifdef CONFIG_SDFAT_DFR
1932 struct defrag_info *ino_dfr = &(SDFAT_I(inode)->dfr_info);
1933
1934 if ((atomic_read(&ino_dfr->stat) == DFR_INO_STAT_REQ) &&
1935 fsapi_dfr_check_dfr_on(inode, start, end, 0, fname))
1936 return 1;
1937 #endif
1938 return 0;
1939 }
1940
1941 static inline int __cancel_dfr_work(struct inode *inode, loff_t start, loff_t end, const char *fname)
1942 {
1943 #ifdef CONFIG_SDFAT_DFR
1944 struct defrag_info *ino_dfr = &(SDFAT_I(inode)->dfr_info);
1945 /* Cancel DEFRAG */
1946 if (atomic_read(&ino_dfr->stat) == DFR_INO_STAT_REQ)
1947 fsapi_dfr_check_dfr_on(inode, start, end, 1, fname);
1948 #endif
1949 return 0;
1950 }
1951
1952 static inline int __dfr_writepage_end_io(struct page *page)
1953 {
1954 #ifdef CONFIG_SDFAT_DFR
1955 struct defrag_info *ino_dfr = &(SDFAT_I(page->mapping->host)->dfr_info);
1956
1957 if (atomic_read(&ino_dfr->stat) == DFR_INO_STAT_REQ)
1958 fsapi_dfr_writepage_endio(page);
1959 #endif
1960 return 0;
1961 }
1962
1963 static inline void __init_dfr_info(struct inode *inode)
1964 {
1965 #ifdef CONFIG_SDFAT_DFR
1966 memset(&(SDFAT_I(inode)->dfr_info), 0, sizeof(struct defrag_info));
1967 INIT_LIST_HEAD(&(SDFAT_I(inode)->dfr_info.entry));
1968 mutex_init(&(SDFAT_I(inode)->dfr_info.lock));
1969 #endif
1970 }
1971
1972 static inline int __alloc_dfr_mem_if_required(struct super_block *sb)
1973 {
1974 #ifdef CONFIG_SDFAT_DFR
1975 struct sdfat_sb_info *sbi = SDFAT_SB(sb);
1976
1977 if (!sbi->options.defrag)
1978 return 0;
1979
1980 memset(&sbi->dfr_info, 0, sizeof(struct defrag_info));
1981 INIT_LIST_HEAD(&(sbi->dfr_info.entry));
1982 mutex_init(&(sbi->dfr_info.lock));
1983
1984 sbi->dfr_new_clus = kzalloc(PAGE_SIZE, GFP_KERNEL);
1985 if (!sbi->dfr_new_clus) {
1986 dfr_debug("error %d", -ENOMEM);
1987 return -ENOMEM;
1988 }
1989 sbi->dfr_new_idx = 1;
1990
1991 sbi->dfr_page_wb = kzalloc(PAGE_SIZE, GFP_KERNEL);
1992 if (!sbi->dfr_page_wb) {
1993 dfr_debug("error %d", -ENOMEM);
1994 return -ENOMEM;
1995 }
1996
1997 sbi->dfr_pagep = alloc_pages_exact(sizeof(struct page *) *
1998 PAGES_PER_AU(sb), GFP_KERNEL | __GFP_ZERO);
1999 if (!sbi->dfr_pagep) {
2000 dfr_debug("error %d", -ENOMEM);
2001 return -ENOMEM;
2002 }
2003 #endif
2004 return 0;
2005 }
2006
2007 static void __free_dfr_mem_if_required(struct super_block *sb)
2008 {
2009 #ifdef CONFIG_SDFAT_DFR
2010 struct sdfat_sb_info *sbi = SDFAT_SB(sb);
2011
2012 if (sbi->dfr_pagep) {
2013 free_pages_exact(sbi->dfr_pagep, sizeof(struct page *) * PAGES_PER_AU(sb));
2014 sbi->dfr_pagep = NULL;
2015 }
2016
2017 /* thanks for kfree */
2018 kfree(sbi->dfr_page_wb);
2019 sbi->dfr_page_wb = NULL;
2020
2021 kfree(sbi->dfr_new_clus);
2022 sbi->dfr_new_clus = NULL;
2023 #endif
2024 }
2025
2026
2027 static int sdfat_file_mmap(struct file *file, struct vm_area_struct *vm_struct)
2028 {
2029 __cancel_dfr_work(file->f_mapping->host,
2030 (loff_t)vm_struct->vm_start,
2031 (loff_t)(vm_struct->vm_end - 1),
2032 __func__);
2033
2034 return generic_file_mmap(file, vm_struct);
2035 }
2036
2037 static int sdfat_ioctl_volume_id(struct inode *dir)
2038 {
2039 struct sdfat_sb_info *sbi = SDFAT_SB(dir->i_sb);
2040 FS_INFO_T *fsi = &(sbi->fsi);
2041
2042 return fsi->vol_id;
2043 }
2044
2045 static int sdfat_dfr_ioctl(struct inode *inode, struct file *filp,
2046 unsigned int cmd, unsigned long arg)
2047 {
2048 #ifdef CONFIG_SDFAT_DFR
2049 switch (cmd) {
2050 case SDFAT_IOCTL_DFR_INFO: {
2051 struct super_block *sb = inode->i_sb;
2052 FS_INFO_T *fsi = &SDFAT_SB(sb)->fsi;
2053 unsigned int __user *uarg = (unsigned int __user *) arg;
2054
2055 __lock_super(sb);
2056 /* Check FS type (FAT32 only) */
2057 if (fsi->vol_type != FAT32) {
2058 dfr_err("Defrag not supported, vol_type %d", fsi->vol_type);
2059 __unlock_super(sb);
2060 return -EPERM;
2061
2062 }
2063
2064 /* Check if SB's defrag option enabled */
2065 if (!(SDFAT_SB(sb)->options.defrag)) {
2066 dfr_err("Defrag not supported, sbi->options.defrag %d", SDFAT_SB(sb)->options.defrag);
2067 __unlock_super(sb);
2068 return -EPERM;
2069 }
2070
2071 /* Only IOCTL on mount-point allowed */
2072 if (filp->f_path.mnt->mnt_root != filp->f_path.dentry) {
2073 dfr_err("IOC_DFR_INFO only allowed on ROOT, root %p, dentry %p",
2074 filp->f_path.mnt->mnt_root, filp->f_path.dentry);
2075 __unlock_super(sb);
2076 return -EPERM;
2077 }
2078 __unlock_super(sb);
2079
2080 return sdfat_ioctl_defrag_info(sb, uarg);
2081 }
2082 case SDFAT_IOCTL_DFR_TRAV: {
2083 struct super_block *sb = inode->i_sb;
2084 FS_INFO_T *fsi = &SDFAT_SB(sb)->fsi;
2085 unsigned int __user *uarg = (unsigned int __user *) arg;
2086
2087 __lock_super(sb);
2088 /* Check FS type (FAT32 only) */
2089 if (fsi->vol_type != FAT32) {
2090 dfr_err("Defrag not supported, vol_type %d", fsi->vol_type);
2091 __unlock_super(sb);
2092 return -EPERM;
2093
2094 }
2095
2096 /* Check if SB's defrag option enabled */
2097 if (!(SDFAT_SB(sb)->options.defrag)) {
2098 dfr_err("Defrag not supported, sbi->options.defrag %d", SDFAT_SB(sb)->options.defrag);
2099 __unlock_super(sb);
2100 return -EPERM;
2101 }
2102 __unlock_super(sb);
2103
2104 return sdfat_ioctl_defrag_trav(inode, uarg);
2105 }
2106 case SDFAT_IOCTL_DFR_REQ: {
2107 struct super_block *sb = inode->i_sb;
2108 FS_INFO_T *fsi = &SDFAT_SB(sb)->fsi;
2109 unsigned int __user *uarg = (unsigned int __user *) arg;
2110
2111 __lock_super(sb);
2112
2113 /* Check if FS_ERROR occurred */
2114 if (sb->s_flags & MS_RDONLY) {
2115 dfr_err("RDONLY partition (err %d)", -EPERM);
2116 __unlock_super(sb);
2117 return -EPERM;
2118 }
2119
2120 /* Check FS type (FAT32 only) */
2121 if (fsi->vol_type != FAT32) {
2122 dfr_err("Defrag not supported, vol_type %d", fsi->vol_type);
2123 __unlock_super(sb);
2124 return -EINVAL;
2125
2126 }
2127
2128 /* Check if SB's defrag option enabled */
2129 if (!(SDFAT_SB(sb)->options.defrag)) {
2130 dfr_err("Defrag not supported, sbi->options.defrag %d", SDFAT_SB(sb)->options.defrag);
2131 __unlock_super(sb);
2132 return -EPERM;
2133 }
2134
2135 /* Only IOCTL on mount-point allowed */
2136 if (filp->f_path.mnt->mnt_root != filp->f_path.dentry) {
2137 dfr_err("IOC_DFR_INFO only allowed on ROOT, root %p, dentry %p",
2138 filp->f_path.mnt->mnt_root, filp->f_path.dentry);
2139 __unlock_super(sb);
2140 return -EINVAL;
2141 }
2142 __unlock_super(sb);
2143
2144 return sdfat_ioctl_defrag_req(inode, uarg);
2145 }
2146 #ifdef CONFIG_SDFAT_DFR_DEBUG
2147 case SDFAT_IOCTL_DFR_SPO_FLAG: {
2148 struct sdfat_sb_info *sbi = SDFAT_SB(inode->i_sb);
2149 int ret = 0;
2150
2151 ret = get_user(sbi->dfr_spo_flag, (int __user *)arg);
2152 dfr_debug("dfr_spo_flag %d", sbi->dfr_spo_flag);
2153
2154 return ret;
2155 }
2156 #endif /* CONFIG_SDFAT_DFR_DEBUG */
2157 }
2158 #endif /* CONFIG_SDFAT_DFR */
2159
2160 /* Inappropriate ioctl for device */
2161 return -ENOTTY;
2162 }
2163
2164 static int sdfat_dbg_ioctl(struct inode *inode, struct file *filp,
2165 unsigned int cmd, unsigned long arg)
2166 {
2167 #ifdef CONFIG_SDFAT_DBG_IOCTL
2168 struct super_block *sb = inode->i_sb;
2169 struct sdfat_sb_info *sbi = SDFAT_SB(sb);
2170 unsigned int flags;
2171
2172 switch (cmd) {
2173 case SDFAT_IOC_GET_DEBUGFLAGS:
2174 flags = sbi->debug_flags;
2175 return put_user(flags, (int __user *)arg);
2176 case SDFAT_IOC_SET_DEBUGFLAGS:
2177 flags = 0;
2178 if (!capable(CAP_SYS_ADMIN))
2179 return -EPERM;
2180
2181 if (get_user(flags, (int __user *) arg))
2182 return -EFAULT;
2183
2184 __lock_super(sb);
2185 sbi->debug_flags = flags;
2186 __unlock_super(sb);
2187 return 0;
2188 case SDFAT_IOCTL_PANIC:
2189 panic("ioctl panic for test");
2190
2191 /* COULD NOT REACH HEAR */
2192 return 0;
2193 }
2194 #endif /* CONFIG_SDFAT_DBG_IOCTL */
2195 return -ENOTTY;
2196 }
2197
2198 static long sdfat_generic_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
2199 {
2200 struct inode *inode = file_inode(filp);
2201 int err;
2202
2203 if (cmd == SDFAT_IOCTL_GET_VOLUME_ID)
2204 return sdfat_ioctl_volume_id(inode);
2205
2206 err = sdfat_dfr_ioctl(inode, filp, cmd, arg);
2207 if (err != -ENOTTY)
2208 return err;
2209
2210 /* -ENOTTY if inappropriate ioctl for device */
2211 return sdfat_dbg_ioctl(inode, filp, cmd, arg);
2212 }
2213
2214
2215 static void __sdfat_writepage_end_io(struct bio *bio, int err)
2216 {
2217 struct page *page = bio->bi_io_vec->bv_page;
2218 struct super_block *sb = page->mapping->host->i_sb;
2219
2220 ASSERT(bio->bi_vcnt == 1); /* Single page endio */
2221 ASSERT(bio_data_dir(bio)); /* Write */
2222
2223 if (err) {
2224 SetPageError(page);
2225 mapping_set_error(page->mapping, err);
2226 }
2227
2228 __dfr_writepage_end_io(page);
2229
2230 #ifdef CONFIG_SDFAT_TRACE_IO
2231 {
2232 //struct sdfat_sb_info *sbi = SDFAT_SB(bio->bi_bdev->bd_super);
2233 struct sdfat_sb_info *sbi = SDFAT_SB(sb);
2234
2235 sbi->stat_n_pages_written++;
2236 if (page->mapping->host == sb->s_bdev->bd_inode)
2237 sbi->stat_n_bdev_pages_written++;
2238
2239 /* 4 MB = 1024 pages => 0.4 sec (approx.)
2240 * 32 KB = 64 pages => 0.025 sec
2241 * Min. average latency b/w msgs. ~= 0.025 sec
2242 */
2243 if ((sbi->stat_n_pages_written & 63) == 0) {
2244 DMSG("STAT:%u, %u, %u, %u (Sector #: %u)\n",
2245 sbi->stat_n_pages_added, sbi->stat_n_pages_written,
2246 sbi->stat_n_bdev_pages_witten,
2247 sbi->stat_n_pages_confused,
2248 (unsigned int)__sdfat_bio_sector(bio));
2249 }
2250 }
2251 #endif
2252 end_page_writeback(page);
2253 bio_put(bio);
2254
2255 // Update trace info.
2256 atomic_dec(&SDFAT_SB(sb)->stat_n_pages_queued);
2257 }
2258
2259
2260 static int __support_write_inode_sync(struct super_block *sb)
2261 {
2262 #ifdef CONFIG_SDFAT_SUPPORT_DIR_SYNC
2263 #ifdef CONFIG_SDFAT_DELAYED_META_DIRTY
2264 struct sdfat_sb_info *sbi = SDFAT_SB(sb);
2265
2266 if (sbi->fsi.vol_type != EXFAT)
2267 return 0;
2268 #endif
2269 return 1;
2270 #endif
2271 return 0;
2272 }
2273
2274
2275 static int __sdfat_file_fsync(struct file *filp, loff_t start, loff_t end, int datasync)
2276 {
2277 struct inode *inode = filp->f_mapping->host;
2278 struct super_block *sb = inode->i_sb;
2279 int res, err = 0;
2280
2281 res = __sdfat_generic_file_fsync(filp, start, end, datasync);
2282
2283 if (!__support_write_inode_sync(sb))
2284 err = fsapi_sync_fs(sb, 1);
2285
2286 return res ? res : err;
2287 }
2288
2289
2290 static const struct file_operations sdfat_dir_operations = {
2291 .llseek = generic_file_llseek,
2292 .read = generic_read_dir,
2293 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 14, 0)
2294 .iterate = sdfat_iterate,
2295 #else /* LINUX_VERSION_CODE < KERNEL_VERSION(3, 14, 0) */
2296 .readdir = sdfat_readdir,
2297 #endif
2298 .fsync = sdfat_file_fsync,
2299 .unlocked_ioctl = sdfat_generic_ioctl,
2300 };
2301
2302 static int __sdfat_create(struct inode *dir, struct dentry *dentry)
2303 {
2304 struct super_block *sb = dir->i_sb;
2305 struct inode *inode;
2306 struct timespec ts;
2307 FILE_ID_T fid;
2308 loff_t i_pos;
2309 int err;
2310
2311 __lock_super(sb);
2312
2313 TMSG("%s entered\n", __func__);
2314
2315 ts = CURRENT_TIME_SEC;
2316
2317 err = fsapi_create(dir, (u8 *) dentry->d_name.name, FM_REGULAR, &fid);
2318 if (err)
2319 goto out;
2320
2321 __lock_d_revalidate(dentry);
2322
2323 dir->i_version++;
2324 dir->i_ctime = dir->i_mtime = dir->i_atime = ts;
2325 if (IS_DIRSYNC(dir))
2326 (void) sdfat_sync_inode(dir);
2327 else
2328 mark_inode_dirty(dir);
2329
2330 i_pos = sdfat_make_i_pos(&fid);
2331 inode = sdfat_build_inode(sb, &fid, i_pos);
2332 if (IS_ERR(inode)) {
2333 err = PTR_ERR(inode);
2334 goto out;
2335 }
2336 inode->i_version++;
2337 inode->i_mtime = inode->i_atime = inode->i_ctime = ts;
2338 /* timestamp is already written, so mark_inode_dirty() is unneeded. */
2339
2340 d_instantiate(dentry, inode);
2341 out:
2342 __unlock_d_revalidate(dentry);
2343 __unlock_super(sb);
2344 TMSG("%s exited with err(%d)\n", __func__, err);
2345 if (!err)
2346 sdfat_statistics_set_create(fid.flags);
2347 return err;
2348 }
2349
2350
2351 static int sdfat_find(struct inode *dir, struct qstr *qname, FILE_ID_T *fid)
2352 {
2353 int err;
2354
2355 if (qname->len == 0)
2356 return -ENOENT;
2357
2358 err = fsapi_lookup(dir, (u8 *) qname->name, fid);
2359 if (err)
2360 return -ENOENT;
2361
2362 return 0;
2363 }
2364
2365 static int sdfat_d_anon_disconn(struct dentry *dentry)
2366 {
2367 return IS_ROOT(dentry) && (dentry->d_flags & DCACHE_DISCONNECTED);
2368 }
2369
2370 static struct dentry *__sdfat_lookup(struct inode *dir, struct dentry *dentry)
2371 {
2372 struct super_block *sb = dir->i_sb;
2373 struct inode *inode;
2374 struct dentry *alias;
2375 int err;
2376 FILE_ID_T fid;
2377 loff_t i_pos;
2378 u64 ret;
2379 mode_t i_mode;
2380
2381 __lock_super(sb);
2382 TMSG("%s entered\n", __func__);
2383 err = sdfat_find(dir, &dentry->d_name, &fid);
2384 if (err) {
2385 if (err == -ENOENT) {
2386 inode = NULL;
2387 goto out;
2388 }
2389 goto error;
2390 }
2391
2392 i_pos = sdfat_make_i_pos(&fid);
2393 inode = sdfat_build_inode(sb, &fid, i_pos);
2394 if (IS_ERR(inode)) {
2395 err = PTR_ERR(inode);
2396 goto error;
2397 }
2398
2399 i_mode = inode->i_mode;
2400 if (S_ISLNK(i_mode) && !SDFAT_I(inode)->target) {
2401 SDFAT_I(inode)->target = kmalloc((i_size_read(inode)+1), GFP_KERNEL);
2402 if (!SDFAT_I(inode)->target) {
2403 err = -ENOMEM;
2404 goto error;
2405 }
2406 fsapi_read_link(dir, &fid, SDFAT_I(inode)->target, i_size_read(inode), &ret);
2407 *(SDFAT_I(inode)->target + i_size_read(inode)) = '\0';
2408 }
2409
2410 alias = d_find_alias(inode);
2411
2412 /*
2413 * Checking "alias->d_parent == dentry->d_parent" to make sure
2414 * FS is not corrupted (especially double linked dir).
2415 */
2416 if (alias && alias->d_parent == dentry->d_parent &&
2417 !sdfat_d_anon_disconn(alias)) {
2418
2419 /*
2420 * Unhashed alias is able to exist because of revalidate()
2421 * called by lookup_fast. You can easily make this status
2422 * by calling create and lookup concurrently
2423 * In such case, we reuse an alias instead of new dentry
2424 */
2425 if (d_unhashed(alias)) {
2426 BUG_ON(alias->d_name.hash_len != dentry->d_name.hash_len);
2427 sdfat_msg(sb, KERN_INFO, "rehashed a dentry(%p) "
2428 "in read lookup", alias);
2429 d_drop(dentry);
2430 d_rehash(alias);
2431 } else if (!S_ISDIR(i_mode)) {
2432 /*
2433 * This inode has non anonymous-DCACHE_DISCONNECTED
2434 * dentry. This means, the user did ->lookup() by an
2435 * another name (longname vs 8.3 alias of it) in past.
2436 *
2437 * Switch to new one for reason of locality if possible.
2438 */
2439 d_move(alias, dentry);
2440 }
2441 iput(inode);
2442 __unlock_super(sb);
2443 TMSG("%s exited\n", __func__);
2444 return alias;
2445 }
2446 dput(alias);
2447 out:
2448 /* initialize d_time even though it is positive dentry */
2449 dentry->d_time = dir->i_version;
2450 __unlock_super(sb);
2451
2452 dentry = d_splice_alias(inode, dentry);
2453
2454 TMSG("%s exited\n", __func__);
2455 return dentry;
2456 error:
2457 __unlock_super(sb);
2458 TMSG("%s exited with err(%d)\n", __func__, err);
2459 return ERR_PTR(err);
2460 }
2461
2462
2463 static int sdfat_unlink(struct inode *dir, struct dentry *dentry)
2464 {
2465 struct inode *inode = dentry->d_inode;
2466 struct super_block *sb = dir->i_sb;
2467 struct timespec ts;
2468 int err;
2469
2470 __lock_super(sb);
2471
2472 TMSG("%s entered\n", __func__);
2473
2474 ts = CURRENT_TIME_SEC;
2475
2476 SDFAT_I(inode)->fid.size = i_size_read(inode);
2477
2478 __cancel_dfr_work(inode, 0, SDFAT_I(inode)->fid.size, __func__);
2479
2480 err = fsapi_unlink(dir, &(SDFAT_I(inode)->fid));
2481 if (err)
2482 goto out;
2483
2484 __lock_d_revalidate(dentry);
2485
2486 dir->i_version++;
2487 dir->i_mtime = dir->i_atime = ts;
2488 if (IS_DIRSYNC(dir))
2489 (void) sdfat_sync_inode(dir);
2490 else
2491 mark_inode_dirty(dir);
2492
2493 clear_nlink(inode);
2494 inode->i_mtime = inode->i_atime = ts;
2495 sdfat_detach(inode);
2496 dentry->d_time = dir->i_version;
2497 out:
2498 __unlock_d_revalidate(dentry);
2499 __unlock_super(sb);
2500 TMSG("%s exited with err(%d)\n", __func__, err);
2501 return err;
2502 }
2503
2504 static int sdfat_symlink(struct inode *dir, struct dentry *dentry, const char *target)
2505 {
2506 struct super_block *sb = dir->i_sb;
2507 struct inode *inode;
2508 struct timespec ts;
2509 FILE_ID_T fid;
2510 loff_t i_pos;
2511 int err;
2512 u64 len = (u64) strlen(target);
2513 u64 ret;
2514
2515 /* symlink option check */
2516 if (!SDFAT_SB(sb)->options.symlink)
2517 return -ENOTSUPP;
2518
2519 __lock_super(sb);
2520
2521 TMSG("%s entered\n", __func__);
2522
2523 ts = CURRENT_TIME_SEC;
2524
2525 err = fsapi_create(dir, (u8 *) dentry->d_name.name, FM_SYMLINK, &fid);
2526 if (err)
2527 goto out;
2528
2529 err = fsapi_write_link(dir, &fid, (char *) target, len, &ret);
2530
2531 if (err) {
2532 fsapi_remove(dir, &fid);
2533 goto out;
2534 }
2535
2536 __lock_d_revalidate(dentry);
2537
2538 dir->i_version++;
2539 dir->i_ctime = dir->i_mtime = dir->i_atime = ts;
2540 if (IS_DIRSYNC(dir))
2541 (void) sdfat_sync_inode(dir);
2542 else
2543 mark_inode_dirty(dir);
2544
2545 i_pos = sdfat_make_i_pos(&fid);
2546 inode = sdfat_build_inode(sb, &fid, i_pos);
2547 if (IS_ERR(inode)) {
2548 err = PTR_ERR(inode);
2549 goto out;
2550 }
2551 inode->i_version++;
2552 inode->i_mtime = inode->i_atime = inode->i_ctime = ts;
2553 /* timestamp is already written, so mark_inode_dirty() is unneeded. */
2554
2555 SDFAT_I(inode)->target = kmalloc((len+1), GFP_KERNEL);
2556 if (!SDFAT_I(inode)->target) {
2557 err = -ENOMEM;
2558 goto out;
2559 }
2560 memcpy(SDFAT_I(inode)->target, target, len+1);
2561
2562 d_instantiate(dentry, inode);
2563 out:
2564 __unlock_d_revalidate(dentry);
2565 __unlock_super(sb);
2566 TMSG("%s exited with err(%d)\n", __func__, err);
2567 return err;
2568 }
2569
2570
2571 static int __sdfat_mkdir(struct inode *dir, struct dentry *dentry)
2572 {
2573 struct super_block *sb = dir->i_sb;
2574 struct inode *inode;
2575 struct timespec ts;
2576 FILE_ID_T fid;
2577 loff_t i_pos;
2578 int err;
2579
2580 __lock_super(sb);
2581
2582 TMSG("%s entered\n", __func__);
2583
2584 ts = CURRENT_TIME_SEC;
2585
2586 err = fsapi_mkdir(dir, (u8 *) dentry->d_name.name, &fid);
2587 if (err)
2588 goto out;
2589
2590 __lock_d_revalidate(dentry);
2591
2592 dir->i_version++;
2593 dir->i_ctime = dir->i_mtime = dir->i_atime = ts;
2594 if (IS_DIRSYNC(dir))
2595 (void) sdfat_sync_inode(dir);
2596 else
2597 mark_inode_dirty(dir);
2598 inc_nlink(dir);
2599
2600 i_pos = sdfat_make_i_pos(&fid);
2601 inode = sdfat_build_inode(sb, &fid, i_pos);
2602 if (IS_ERR(inode)) {
2603 err = PTR_ERR(inode);
2604 goto out;
2605 }
2606 inode->i_version++;
2607 inode->i_mtime = inode->i_atime = inode->i_ctime = ts;
2608 /* timestamp is already written, so mark_inode_dirty() is unneeded. */
2609
2610 d_instantiate(dentry, inode);
2611
2612 out:
2613 __unlock_d_revalidate(dentry);
2614 __unlock_super(sb);
2615 TMSG("%s exited with err(%d)\n", __func__, err);
2616 if (!err)
2617 sdfat_statistics_set_mkdir(fid.flags);
2618 return err;
2619 }
2620
2621
2622 static int sdfat_rmdir(struct inode *dir, struct dentry *dentry)
2623 {
2624 struct inode *inode = dentry->d_inode;
2625 struct super_block *sb = dir->i_sb;
2626 struct timespec ts;
2627 int err;
2628
2629 __lock_super(sb);
2630
2631 TMSG("%s entered\n", __func__);
2632
2633 ts = CURRENT_TIME_SEC;
2634
2635 SDFAT_I(inode)->fid.size = i_size_read(inode);
2636
2637 err = fsapi_rmdir(dir, &(SDFAT_I(inode)->fid));
2638 if (err)
2639 goto out;
2640
2641 __lock_d_revalidate(dentry);
2642
2643 dir->i_version++;
2644 dir->i_mtime = dir->i_atime = ts;
2645 if (IS_DIRSYNC(dir))
2646 (void) sdfat_sync_inode(dir);
2647 else
2648 mark_inode_dirty(dir);
2649 drop_nlink(dir);
2650
2651 clear_nlink(inode);
2652 inode->i_mtime = inode->i_atime = ts;
2653 sdfat_detach(inode);
2654 dentry->d_time = dir->i_version;
2655 out:
2656 __unlock_d_revalidate(dentry);
2657 __unlock_super(sb);
2658 TMSG("%s exited with err(%d)\n", __func__, err);
2659 return err;
2660 }
2661
2662 static int __sdfat_rename(struct inode *old_dir, struct dentry *old_dentry,
2663 struct inode *new_dir, struct dentry *new_dentry)
2664 {
2665 struct inode *old_inode, *new_inode;
2666 struct super_block *sb = old_dir->i_sb;
2667 struct timespec ts;
2668 loff_t i_pos;
2669 int err;
2670
2671 __lock_super(sb);
2672
2673 TMSG("%s entered\n", __func__);
2674
2675 old_inode = old_dentry->d_inode;
2676 new_inode = new_dentry->d_inode;
2677
2678 ts = CURRENT_TIME_SEC;
2679
2680 SDFAT_I(old_inode)->fid.size = i_size_read(old_inode);
2681
2682 __cancel_dfr_work(old_inode, 0, 1, __func__);
2683
2684 err = fsapi_rename(old_dir, &(SDFAT_I(old_inode)->fid), new_dir, new_dentry);
2685 if (err)
2686 goto out;
2687
2688 __lock_d_revalidate(old_dentry);
2689 __lock_d_revalidate(new_dentry);
2690
2691 new_dir->i_version++;
2692 new_dir->i_ctime = new_dir->i_mtime = new_dir->i_atime = ts;
2693 if (IS_DIRSYNC(new_dir))
2694 (void) sdfat_sync_inode(new_dir);
2695 else
2696 mark_inode_dirty(new_dir);
2697
2698 i_pos = sdfat_make_i_pos(&(SDFAT_I(old_inode)->fid));
2699 sdfat_detach(old_inode);
2700 sdfat_attach(old_inode, i_pos);
2701 if (IS_DIRSYNC(new_dir))
2702 (void) sdfat_sync_inode(old_inode);
2703 else
2704 mark_inode_dirty(old_inode);
2705
2706 if ((S_ISDIR(old_inode->i_mode)) && (old_dir != new_dir)) {
2707 drop_nlink(old_dir);
2708 if (!new_inode)
2709 inc_nlink(new_dir);
2710 }
2711
2712 old_dir->i_version++;
2713 old_dir->i_ctime = old_dir->i_mtime = ts;
2714 if (IS_DIRSYNC(old_dir))
2715 (void) sdfat_sync_inode(old_dir);
2716 else
2717 mark_inode_dirty(old_dir);
2718
2719 if (new_inode) {
2720 sdfat_detach(new_inode);
2721
2722 /* skip drop_nlink if new_inode already has been dropped */
2723 if (new_inode->i_nlink) {
2724 drop_nlink(new_inode);
2725 if (S_ISDIR(new_inode->i_mode))
2726 drop_nlink(new_inode);
2727 } else {
2728 EMSG("%s : abnormal access to an inode dropped\n",
2729 __func__);
2730 WARN_ON(new_inode->i_nlink == 0);
2731 }
2732 new_inode->i_ctime = ts;
2733 #if 0
2734 (void) sdfat_sync_inode(new_inode);
2735 #endif
2736 }
2737
2738 out:
2739 __unlock_d_revalidate(old_dentry);
2740 __unlock_d_revalidate(new_dentry);
2741 __unlock_super(sb);
2742 TMSG("%s exited with err(%d)\n", __func__, err);
2743 return err;
2744 }
2745
2746 static int sdfat_cont_expand(struct inode *inode, loff_t size)
2747 {
2748 struct address_space *mapping = inode->i_mapping;
2749 loff_t start = i_size_read(inode), count = size - i_size_read(inode);
2750 int err, err2;
2751
2752 err = generic_cont_expand_simple(inode, size);
2753 if (err)
2754 return err;
2755
2756 inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC;
2757 mark_inode_dirty(inode);
2758
2759 if (!IS_SYNC(inode))
2760 return 0;
2761
2762 err = filemap_fdatawrite_range(mapping, start, start + count - 1);
2763 err2 = sync_mapping_buffers(mapping);
2764 err = (err)?(err):(err2);
2765 err2 = write_inode_now(inode, 1);
2766 err = (err)?(err):(err2);
2767 if (err)
2768 return err;
2769
2770 return filemap_fdatawait_range(mapping, start, start + count - 1);
2771 }
2772
2773 static int sdfat_allow_set_time(struct sdfat_sb_info *sbi, struct inode *inode)
2774 {
2775 mode_t allow_utime = sbi->options.allow_utime;
2776
2777 if (!uid_eq(current_fsuid(), inode->i_uid)) {
2778 if (in_group_p(inode->i_gid))
2779 allow_utime >>= 3;
2780 if (allow_utime & MAY_WRITE)
2781 return 1;
2782 }
2783
2784 /* use a default check */
2785 return 0;
2786 }
2787
2788 static int sdfat_sanitize_mode(const struct sdfat_sb_info *sbi,
2789 struct inode *inode, umode_t *mode_ptr)
2790 {
2791 mode_t i_mode, mask, perm;
2792
2793 i_mode = inode->i_mode;
2794
2795 if (S_ISREG(i_mode) || S_ISLNK(i_mode))
2796 mask = sbi->options.fs_fmask;
2797 else
2798 mask = sbi->options.fs_dmask;
2799
2800 perm = *mode_ptr & ~(S_IFMT | mask);
2801
2802 /* Of the r and x bits, all (subject to umask) must be present.*/
2803 if ((perm & (S_IRUGO | S_IXUGO)) != (i_mode & (S_IRUGO | S_IXUGO)))
2804 return -EPERM;
2805
2806 if (sdfat_mode_can_hold_ro(inode)) {
2807 /* Of the w bits, either all (subject to umask) or none must be present. */
2808 if ((perm & S_IWUGO) && ((perm & S_IWUGO) != (S_IWUGO & ~mask)))
2809 return -EPERM;
2810 } else {
2811 /* If sdfat_mode_can_hold_ro(inode) is false, can't change w bits. */
2812 if ((perm & S_IWUGO) != (S_IWUGO & ~mask))
2813 return -EPERM;
2814 }
2815
2816 *mode_ptr &= S_IFMT | perm;
2817
2818 return 0;
2819 }
2820
2821 static int sdfat_setattr(struct dentry *dentry, struct iattr *attr)
2822 {
2823
2824 struct sdfat_sb_info *sbi = SDFAT_SB(dentry->d_sb);
2825 struct inode *inode = dentry->d_inode;
2826 unsigned int ia_valid;
2827 int error;
2828 loff_t old_size;
2829
2830 TMSG("%s entered\n", __func__);
2831
2832 if ((attr->ia_valid & ATTR_SIZE)
2833 && (attr->ia_size > i_size_read(inode))) {
2834 error = sdfat_cont_expand(inode, attr->ia_size);
2835 if (error || attr->ia_valid == ATTR_SIZE)
2836 return error;
2837 attr->ia_valid &= ~ATTR_SIZE;
2838 }
2839
2840 /* Check for setting the inode time. */
2841 ia_valid = attr->ia_valid;
2842 if ((ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET))
2843 && sdfat_allow_set_time(sbi, inode)) {
2844 attr->ia_valid &= ~(ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET);
2845 }
2846
2847 error = setattr_prepare(dentry, attr);
2848 attr->ia_valid = ia_valid;
2849 if (error)
2850 return error;
2851
2852 if (((attr->ia_valid & ATTR_UID) &&
2853 (!uid_eq(attr->ia_uid, sbi->options.fs_uid))) ||
2854 ((attr->ia_valid & ATTR_GID) &&
2855 (!gid_eq(attr->ia_gid, sbi->options.fs_gid))) ||
2856 ((attr->ia_valid & ATTR_MODE) &&
2857 (attr->ia_mode & ~(S_IFREG | S_IFLNK | S_IFDIR | S_IRWXUGO)))) {
2858 return -EPERM;
2859 }
2860
2861 /*
2862 * We don't return -EPERM here. Yes, strange, but this is too
2863 * old behavior.
2864 */
2865 if (attr->ia_valid & ATTR_MODE) {
2866 if (sdfat_sanitize_mode(sbi, inode, &attr->ia_mode) < 0)
2867 attr->ia_valid &= ~ATTR_MODE;
2868 }
2869
2870 SDFAT_I(inode)->fid.size = i_size_read(inode);
2871
2872 /* patch 1.2.0 : fixed the problem of size mismatch. */
2873 if (attr->ia_valid & ATTR_SIZE) {
2874 old_size = i_size_read(inode);
2875
2876 /* TO CHECK evicting directory works correctly */
2877 MMSG("%s: inode(%p) truncate size (%llu->%llu)\n", __func__,
2878 inode, (u64)old_size, (u64)attr->ia_size);
2879 __sdfat_do_truncate(inode, old_size, attr->ia_size);
2880 }
2881 setattr_copy(inode, attr);
2882 mark_inode_dirty(inode);
2883
2884
2885 TMSG("%s exited with err(%d)\n", __func__, error);
2886 return error;
2887 }
2888
2889 static int sdfat_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
2890 {
2891 struct inode *inode = dentry->d_inode;
2892
2893 TMSG("%s entered\n", __func__);
2894
2895 generic_fillattr(inode, stat);
2896 stat->blksize = SDFAT_SB(inode->i_sb)->fsi.cluster_size;
2897
2898 TMSG("%s exited\n", __func__);
2899 return 0;
2900 }
2901
2902 static const struct inode_operations sdfat_dir_inode_operations = {
2903 .create = sdfat_create,
2904 .lookup = sdfat_lookup,
2905 .unlink = sdfat_unlink,
2906 .symlink = sdfat_symlink,
2907 .mkdir = sdfat_mkdir,
2908 .rmdir = sdfat_rmdir,
2909 .rename = sdfat_rename,
2910 .setattr = sdfat_setattr,
2911 .getattr = sdfat_getattr,
2912 #ifdef CONFIG_SDFAT_VIRTUAL_XATTR
2913 .listxattr = sdfat_listxattr,
2914 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 9, 0)
2915 .setxattr = sdfat_setxattr,
2916 .getxattr = sdfat_getxattr,
2917 .removexattr = sdfat_removexattr,
2918 #endif
2919 #endif
2920 };
2921
2922 /*======================================================================*/
2923 /* File Operations */
2924 /*======================================================================*/
2925 static const struct inode_operations sdfat_symlink_inode_operations = {
2926 .readlink = generic_readlink,
2927 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 5, 0)
2928 .get_link = sdfat_follow_link,
2929 #else /* LINUX_VERSION_CODE < KERNEL_VERSION(4, 5, 0) */
2930 .follow_link = sdfat_follow_link,
2931 #endif
2932 #ifdef CONFIG_SDFAT_VIRTUAL_XATTR
2933 .listxattr = sdfat_listxattr,
2934 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 9, 0)
2935 .setxattr = sdfat_setxattr,
2936 .getxattr = sdfat_getxattr,
2937 .removexattr = sdfat_removexattr,
2938 #endif
2939 #endif
2940 };
2941
2942 static int sdfat_file_release(struct inode *inode, struct file *filp)
2943 {
2944 struct super_block *sb = inode->i_sb;
2945
2946 /* Moved below code from sdfat_write_inode
2947 * TO FIX size-mismatch problem.
2948 */
2949 /* FIXME : Added bug_on to confirm that there is no size mismatch */
2950 sdfat_debug_bug_on(SDFAT_I(inode)->fid.size != i_size_read(inode));
2951 SDFAT_I(inode)->fid.size = i_size_read(inode);
2952 fsapi_sync_fs(sb, 0);
2953 return 0;
2954 }
2955
2956 static const struct file_operations sdfat_file_operations = {
2957 .llseek = generic_file_llseek,
2958 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 1, 0)
2959 .read_iter = generic_file_read_iter,
2960 .write_iter = generic_file_write_iter,
2961 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(3, 16, 0)
2962 .read = new_sync_read,
2963 .write = new_sync_write,
2964 .read_iter = generic_file_read_iter,
2965 .write_iter = generic_file_write_iter,
2966 #else /* LINUX_VERSION_CODE < KERNEL_VERSION(3, 16, 0) */
2967 .read = do_sync_read,
2968 .write = do_sync_write,
2969 .aio_read = generic_file_aio_read,
2970 .aio_write = generic_file_aio_write,
2971 #endif
2972 .mmap = sdfat_file_mmap,
2973 .release = sdfat_file_release,
2974 .unlocked_ioctl = sdfat_generic_ioctl,
2975 .fsync = sdfat_file_fsync,
2976 .splice_read = generic_file_splice_read,
2977 };
2978
2979 static const struct address_space_operations sdfat_da_aops;
2980 static const struct address_space_operations sdfat_aops;
2981
2982 static void sdfat_truncate(struct inode *inode, loff_t old_size)
2983 {
2984 struct super_block *sb = inode->i_sb;
2985 struct sdfat_sb_info *sbi = SDFAT_SB(sb);
2986 FS_INFO_T *fsi = &(sbi->fsi);
2987 unsigned int blocksize = 1 << inode->i_blkbits;
2988 loff_t aligned_size;
2989 int err;
2990
2991 __lock_super(sb);
2992
2993 if (SDFAT_I(inode)->fid.start_clu == 0) {
2994 /* Stange statement:
2995 * Empty start_clu != ~0 (not allocated)
2996 */
2997 sdfat_fs_error(sb, "tried to truncate zeroed cluster.");
2998 goto out;
2999 }
3000
3001 sdfat_debug_check_clusters(inode);
3002
3003 __cancel_dfr_work(inode, (loff_t)i_size_read(inode), (loff_t)old_size, __func__);
3004
3005 err = fsapi_truncate(inode, old_size, i_size_read(inode));
3006 if (err)
3007 goto out;
3008
3009 inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC;
3010 if (IS_DIRSYNC(inode))
3011 (void) sdfat_sync_inode(inode);
3012 else
3013 mark_inode_dirty(inode);
3014
3015 // FIXME: 확인 요망
3016 // inode->i_blocks = ((SDFAT_I(inode)->i_size_ondisk + (fsi->cluster_size - 1))
3017 inode->i_blocks = ((i_size_read(inode) + (fsi->cluster_size - 1)) &
3018 ~((loff_t)fsi->cluster_size - 1)) >> inode->i_blkbits;
3019 out:
3020 /*
3021 * This protects against truncating a file bigger than it was then
3022 * trying to write into the hole.
3023 *
3024 * comment by sh.hong:
3025 * This seems to mean 'intra page/block' truncate and writing.
3026 * I couldn't find a reason to change the values prior to fsapi_truncate
3027 * Therefore, I switched the order of operations
3028 * so that it's possible to utilize i_size_ondisk in fsapi_truncate
3029 */
3030
3031 aligned_size = i_size_read(inode);
3032 if (aligned_size & (blocksize - 1)) {
3033 aligned_size |= (blocksize - 1);
3034 aligned_size++;
3035 }
3036
3037 if (SDFAT_I(inode)->i_size_ondisk > i_size_read(inode))
3038 SDFAT_I(inode)->i_size_ondisk = aligned_size;
3039
3040 sdfat_debug_check_clusters(inode);
3041
3042 if (SDFAT_I(inode)->i_size_aligned > i_size_read(inode))
3043 SDFAT_I(inode)->i_size_aligned = aligned_size;
3044
3045 /* After truncation :
3046 * 1) Delayed allocation is OFF
3047 * i_size = i_size_ondisk <= i_size_aligned
3048 * (useless size var.)
3049 * (block-aligned)
3050 * 2) Delayed allocation is ON
3051 * i_size = i_size_ondisk = i_size_aligned
3052 * (will be block-aligned after write)
3053 * or
3054 * i_size_ondisk < i_size <= i_size_aligned (block_aligned)
3055 * (will be block-aligned after write)
3056 */
3057
3058 __unlock_super(sb);
3059 }
3060
3061 static const struct inode_operations sdfat_file_inode_operations = {
3062 .setattr = sdfat_setattr,
3063 .getattr = sdfat_getattr,
3064 #ifdef CONFIG_SDFAT_VIRTUAL_XATTR
3065 .listxattr = sdfat_listxattr,
3066 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 9, 0)
3067 .setxattr = sdfat_setxattr,
3068 .getxattr = sdfat_getxattr,
3069 .removexattr = sdfat_removexattr,
3070 #endif
3071 #endif
3072 };
3073
3074 /*======================================================================*/
3075 /* Address Space Operations */
3076 /*======================================================================*/
3077 /* 2-level option flag */
3078 #define BMAP_NOT_CREATE 0
3079 #define BMAP_ADD_BLOCK 1
3080 #define BMAP_ADD_CLUSTER 2
3081 #define BLOCK_ADDED(bmap_ops) (bmap_ops)
3082 static int sdfat_bmap(struct inode *inode, sector_t sector, sector_t *phys,
3083 unsigned long *mapped_blocks, int *create)
3084 {
3085 struct super_block *sb = inode->i_sb;
3086 struct sdfat_sb_info *sbi = SDFAT_SB(sb);
3087 FS_INFO_T *fsi = &(sbi->fsi);
3088 const unsigned long blocksize = sb->s_blocksize;
3089 const unsigned char blocksize_bits = sb->s_blocksize_bits;
3090 sector_t last_block;
3091 unsigned int cluster, clu_offset, sec_offset;
3092 int err = 0;
3093
3094 *phys = 0;
3095 *mapped_blocks = 0;
3096
3097 /* core code should handle EIO */
3098 #if 0
3099 if (fsi->prev_eio && BLOCK_ADDED(*create))
3100 return -EIO;
3101 #endif
3102
3103 if (((fsi->vol_type == FAT12) || (fsi->vol_type == FAT16)) &&
3104 (inode->i_ino == SDFAT_ROOT_INO)) {
3105 if (sector < (fsi->dentries_in_root >>
3106 (sb->s_blocksize_bits-DENTRY_SIZE_BITS))) {
3107 *phys = sector + fsi->root_start_sector;
3108 *mapped_blocks = 1;
3109 }
3110 return 0;
3111 }
3112
3113 last_block = (i_size_read(inode) + (blocksize - 1)) >> blocksize_bits;
3114 if ((sector >= last_block) && (*create == BMAP_NOT_CREATE))
3115 return 0;
3116
3117 /* Is this block already allocated? */
3118 clu_offset = sector >> fsi->sect_per_clus_bits; /* cluster offset */
3119
3120 SDFAT_I(inode)->fid.size = i_size_read(inode);
3121
3122
3123 if (unlikely(__check_dfr_on(inode,
3124 (loff_t)((loff_t)clu_offset << fsi->cluster_size_bits),
3125 (loff_t)((loff_t)(clu_offset + 1) << fsi->cluster_size_bits),
3126 __func__))) {
3127 err = __do_dfr_map_cluster(inode, clu_offset, &cluster);
3128 } else {
3129 if (*create & BMAP_ADD_CLUSTER)
3130 err = fsapi_map_clus(inode, clu_offset, &cluster, 1);
3131 else
3132 err = fsapi_map_clus(inode, clu_offset, &cluster, ALLOC_NOWHERE);
3133 }
3134
3135 if (err) {
3136 if (err != -ENOSPC)
3137 return -EIO;
3138 return err;
3139 }
3140
3141 /* FOR BIGDATA */
3142 sdfat_statistics_set_rw(SDFAT_I(inode)->fid.flags,
3143 clu_offset, *create & BMAP_ADD_CLUSTER);
3144
3145 if (!IS_CLUS_EOF(cluster)) {
3146 /* sector offset in cluster */
3147 sec_offset = sector & (fsi->sect_per_clus - 1);
3148
3149 *phys = CLUS_TO_SECT(fsi, cluster) + sec_offset;
3150 *mapped_blocks = fsi->sect_per_clus - sec_offset;
3151 }
3152 #if 0
3153 else {
3154 /* Debug purpose (new clu needed) */
3155 ASSERT((*create & BMAP_ADD_CLUSTER) == 0);
3156 ASSERT(sector >= last_block);
3157 }
3158 #endif
3159
3160 if (sector < last_block)
3161 *create = BMAP_NOT_CREATE;
3162 #if 0
3163 else if (sector >= last_block)
3164 *create = non-zero;
3165
3166 if (iblock <= last mapped-block)
3167 *phys != 0
3168 *create = BMAP_NOT_CREATE
3169 else if (iblock <= last cluster)
3170 *phys != 0
3171 *create = non-zero
3172 #endif
3173 return 0;
3174 }
3175
3176 static int sdfat_da_prep_block(struct inode *inode, sector_t iblock,
3177 struct buffer_head *bh_result, int create)
3178 {
3179 struct super_block *sb = inode->i_sb;
3180 struct sdfat_sb_info *sbi = SDFAT_SB(sb);
3181 FS_INFO_T *fsi = &(sbi->fsi);
3182 unsigned long max_blocks = bh_result->b_size >> inode->i_blkbits;
3183 unsigned long mapped_blocks;
3184 sector_t phys;
3185 loff_t pos;
3186 int sec_offset;
3187 int bmap_create = create ? BMAP_ADD_BLOCK : BMAP_NOT_CREATE;
3188 int err = 0;
3189
3190 __lock_super(sb);
3191
3192 /* FAT32 only */
3193 ASSERT(fsi->vol_type == FAT32);
3194
3195 err = sdfat_bmap(inode, iblock, &phys, &mapped_blocks, &bmap_create);
3196 if (err) {
3197 if (err != -ENOSPC)
3198 sdfat_fs_error_ratelimit(sb, "%s: failed to bmap "
3199 "(iblock:%u, err:%d)", __func__,
3200 (u32)iblock, err);
3201 goto unlock_ret;
3202 }
3203
3204 sec_offset = iblock & (fsi->sect_per_clus - 1);
3205
3206 if (phys) {
3207 /* the block in in the mapped cluster boundary */
3208 max_blocks = min(mapped_blocks, max_blocks);
3209 map_bh(bh_result, sb, phys);
3210
3211 BUG_ON(BLOCK_ADDED(bmap_create) && (sec_offset == 0));
3212
3213 } else if (create == 1) {
3214 /* Not exist: new cluster needed */
3215 BUG_ON(!BLOCK_ADDED(bmap_create));
3216
3217 // Reserved Cluster (only if iblock is the first sector in a clu)
3218 if (sec_offset == 0) {
3219 err = fsapi_reserve_clus(inode);
3220 if (err) {
3221 if (err != -ENOSPC)
3222 sdfat_fs_error_ratelimit(sb,
3223 "%s: failed to bmap "
3224 "(iblock:%u, err:%d)", __func__,
3225 (u32)iblock, err);
3226
3227 goto unlock_ret;
3228 }
3229 }
3230
3231 // Delayed mapping
3232 map_bh(bh_result, sb, ~((sector_t) 0xffff));
3233 set_buffer_new(bh_result);
3234 set_buffer_delay(bh_result);
3235
3236 } else {
3237 /* get_block on non-existing addr. with create==0 */
3238 /*
3239 * CHECKME:
3240 * i_size_aligned 보다 작으면 delay 매핑을 일단
3241 * 켜줘야되는 게 아닌가?
3242 * - 0-fill 을 항상 하기에, FAT 에서는 문제 없음.
3243 * 중간에 영역이 꽉 찼으면, 디스크에 내려가지 않고는
3244 * invalidate 될 일이 없음
3245 */
3246 goto unlock_ret;
3247 }
3248
3249
3250 /* Newly added blocks */
3251 if (BLOCK_ADDED(bmap_create)) {
3252 set_buffer_new(bh_result);
3253
3254 SDFAT_I(inode)->i_size_aligned += max_blocks << sb->s_blocksize_bits;
3255 if (phys) {
3256 /* i_size_ondisk changes if a block added in the existing cluster */
3257 #define num_clusters(value) ((value) ? (s32)((value - 1) >> fsi->cluster_size_bits) + 1 : 0)
3258
3259 /* FOR GRACEFUL ERROR HANDLING */
3260 if (num_clusters(SDFAT_I(inode)->i_size_aligned) !=
3261 num_clusters(SDFAT_I(inode)->i_size_ondisk)) {
3262 EMSG("%s: inode(%p) invalid size (create(%d) "
3263 "bmap_create(%d) phys(%lld) aligned(%lld) "
3264 "on_disk(%lld) iblock(%u) sec_off(%d))\n",
3265 __func__, inode, create, bmap_create, (s64)phys,
3266 (s64)SDFAT_I(inode)->i_size_aligned,
3267 (s64)SDFAT_I(inode)->i_size_ondisk,
3268 (u32)iblock,
3269 (s32)sec_offset);
3270 sdfat_debug_bug_on(1);
3271 }
3272 SDFAT_I(inode)->i_size_ondisk = SDFAT_I(inode)->i_size_aligned;
3273 }
3274
3275 pos = (iblock + 1) << sb->s_blocksize_bits;
3276 /* Debug purpose - defensive coding */
3277 ASSERT(SDFAT_I(inode)->i_size_aligned == pos);
3278 if (SDFAT_I(inode)->i_size_aligned < pos)
3279 SDFAT_I(inode)->i_size_aligned = pos;
3280 /* Debug end */
3281
3282 #ifdef CONFIG_SDFAT_TRACE_IO
3283 /* New page added (ASSERTION: 8 blocks per page) */
3284 if ((sec_offset & 7) == 0)
3285 sbi->stat_n_pages_added++;
3286 #endif
3287 }
3288
3289 /* FOR GRACEFUL ERROR HANDLING */
3290 if (i_size_read(inode) > SDFAT_I(inode)->i_size_aligned) {
3291 sdfat_fs_error_ratelimit(sb, "%s: invalid size (inode(%p), "
3292 "size(%llu) > aligned(%llu)\n", __func__, inode,
3293 i_size_read(inode), SDFAT_I(inode)->i_size_aligned);
3294 sdfat_debug_bug_on(1);
3295 }
3296
3297 bh_result->b_size = max_blocks << sb->s_blocksize_bits;
3298
3299 unlock_ret:
3300 __unlock_super(sb);
3301 return err;
3302 }
3303
3304 static int sdfat_get_block(struct inode *inode, sector_t iblock,
3305 struct buffer_head *bh_result, int create)
3306 {
3307 struct super_block *sb = inode->i_sb;
3308 unsigned long max_blocks = bh_result->b_size >> inode->i_blkbits;
3309 int err = 0;
3310 unsigned long mapped_blocks;
3311 sector_t phys;
3312 loff_t pos;
3313 int bmap_create = create ? BMAP_ADD_CLUSTER : BMAP_NOT_CREATE;
3314
3315 __lock_super(sb);
3316 err = sdfat_bmap(inode, iblock, &phys, &mapped_blocks, &bmap_create);
3317 if (err) {
3318 if (err != -ENOSPC)
3319 sdfat_fs_error_ratelimit(sb, "%s: failed to bmap "
3320 "(inode:%p iblock:%u, err:%d)",
3321 __func__, inode, (u32)iblock, err);
3322 goto unlock_ret;
3323 }
3324
3325 if (phys) {
3326 max_blocks = min(mapped_blocks, max_blocks);
3327
3328 /* Treat newly added block / cluster */
3329 if (BLOCK_ADDED(bmap_create) || buffer_delay(bh_result)) {
3330
3331 /* Update i_size_ondisk */
3332 pos = (iblock + 1) << sb->s_blocksize_bits;
3333 if (SDFAT_I(inode)->i_size_ondisk < pos) {
3334 /* Debug purpose */
3335 if ((pos - SDFAT_I(inode)->i_size_ondisk) > bh_result->b_size) {
3336 /* This never happens without DA */
3337 MMSG("Jumping get_block\n");
3338 }
3339
3340 SDFAT_I(inode)->i_size_ondisk = pos;
3341 sdfat_debug_check_clusters(inode);
3342 }
3343
3344 if (BLOCK_ADDED(bmap_create)) {
3345 /* Old way (w/o DA)
3346 * create == 1 only if iblock > i_size
3347 * (in block unit)
3348 */
3349
3350 /* 20130723 CHECK
3351 * Truncate와 동시에 발생할 경우,
3352 * i_size < (i_block 위치) 면서 buffer_delay()가
3353 * 켜져있을 수 있다.
3354 *
3355 * 기존에 할당된 영역을 다시 쓸 뿐이므로 큰 문제
3356 * 없지만, 그 경우, 미리 i_size_aligned 가 확장된
3357 * 영역이어야 한다.
3358 */
3359
3360 /* FOR GRACEFUL ERROR HANDLING */
3361 if (buffer_delay(bh_result) &&
3362 (pos > SDFAT_I(inode)->i_size_aligned)) {
3363 sdfat_fs_error(sb, "requested for bmap "
3364 "out of range(pos:(%llu)>i_size_aligned(%llu)\n",
3365 pos, SDFAT_I(inode)->i_size_aligned);
3366 sdfat_debug_bug_on(1);
3367 err = -EIO;
3368 goto unlock_ret;
3369 }
3370 set_buffer_new(bh_result);
3371
3372 /*
3373 * adjust i_size_aligned if i_size_ondisk is
3374 * bigger than it. (i.e. non-DA)
3375 */
3376 if (SDFAT_I(inode)->i_size_ondisk >
3377 SDFAT_I(inode)->i_size_aligned) {
3378 SDFAT_I(inode)->i_size_aligned =
3379 SDFAT_I(inode)->i_size_ondisk;
3380 }
3381 }
3382
3383 if (buffer_delay(bh_result))
3384 clear_buffer_delay(bh_result);
3385
3386 #if 0
3387 /* Debug purpose */
3388 if (SDFAT_I(inode)->i_size_ondisk >
3389 SDFAT_I(inode)->i_size_aligned) {
3390 /* Only after truncate
3391 * and the two size variables should indicate
3392 * same i_block
3393 */
3394 unsigned int blocksize = 1 << inode->i_blkbits;
3395 BUG_ON(SDFAT_I(inode)->i_size_ondisk -
3396 SDFAT_I(inode)->i_size_aligned >= blocksize);
3397 }
3398 #endif
3399 }
3400 map_bh(bh_result, sb, phys);
3401 }
3402
3403 bh_result->b_size = max_blocks << sb->s_blocksize_bits;
3404 unlock_ret:
3405 __unlock_super(sb);
3406 return err;
3407 }
3408
3409 static int sdfat_readpage(struct file *file, struct page *page)
3410 {
3411 int ret;
3412
3413 ret = mpage_readpage(page, sdfat_get_block);
3414 return ret;
3415 }
3416
3417 static int sdfat_readpages(struct file *file, struct address_space *mapping,
3418 struct list_head *pages, unsigned int nr_pages)
3419 {
3420 int ret;
3421
3422 ret = mpage_readpages(mapping, pages, nr_pages, sdfat_get_block);
3423 return ret;
3424 }
3425
3426 static inline void sdfat_submit_fullpage_bio(struct block_device *bdev,
3427 sector_t sector, unsigned int length, struct page *page)
3428 {
3429 /* Single page bio submit */
3430 struct bio *bio;
3431
3432 BUG_ON((length > PAGE_SIZE) || (length == 0));
3433
3434 /*
3435 * If __GFP_WAIT is set, then bio_alloc will always be able to allocate
3436 * a bio. This is due to the mempool guarantees. To make this work, callers
3437 * must never allocate more than 1 bio at a time from this pool.
3438 *
3439 * #define GFP_NOIO (__GFP_WAIT)
3440 */
3441 bio = bio_alloc(GFP_NOIO, 1);
3442
3443 bio->bi_bdev = bdev;
3444 bio->bi_vcnt = 1;
3445 bio->bi_io_vec[0].bv_page = page; /* Inline vec */
3446 bio->bi_io_vec[0].bv_len = length; /* PAGE_SIZE */
3447 bio->bi_io_vec[0].bv_offset = 0;
3448 __sdfat_set_bio_iterate(bio, sector, length, 0, 0);
3449
3450 bio->bi_end_io = sdfat_writepage_end_io;
3451 __sdfat_submit_bio_write(bio);
3452 }
3453
3454 static int sdfat_writepage(struct page *page, struct writeback_control *wbc)
3455 {
3456 struct inode * const inode = page->mapping->host;
3457 struct super_block *sb = inode->i_sb;
3458 loff_t i_size = i_size_read(inode);
3459 const pgoff_t end_index = i_size >> PAGE_SHIFT;
3460 const unsigned int blocks_per_page = PAGE_SIZE >> inode->i_blkbits;
3461 FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
3462 struct buffer_head *bh, *head;
3463 sector_t block, block_0, last_phys;
3464 int ret;
3465 unsigned int nr_blocks_towrite = blocks_per_page;
3466
3467 /* Don't distinguish 0-filled/clean block.
3468 * Just write back the whole page
3469 */
3470 if (fsi->cluster_size < PAGE_SIZE)
3471 goto confused;
3472
3473 if (!PageUptodate(page)) {
3474 MMSG("%s: Not up-to-date page -> block_write_full_page\n",
3475 __func__);
3476 goto confused;
3477 }
3478
3479 if (page->index >= end_index) {
3480 /* last page or outside i_size */
3481 unsigned int offset = i_size & (PAGE_SIZE-1);
3482
3483 /* If a truncation is in progress */
3484 if (page->index > end_index || !offset)
3485 goto confused;
3486
3487 /* 0-fill after i_size */
3488 zero_user_segment(page, offset, PAGE_SIZE);
3489 }
3490
3491 if (!page_has_buffers(page)) {
3492 MMSG("WP: No buffers -> block_write_full_page\n");
3493 goto confused;
3494 }
3495
3496 block = (sector_t)page->index << (PAGE_SHIFT - inode->i_blkbits);
3497 block_0 = block; /* first block */
3498 head = page_buffers(page);
3499 bh = head;
3500
3501 last_phys = 0;
3502 do {
3503 BUG_ON(buffer_locked(bh));
3504
3505 if (!buffer_dirty(bh) || !buffer_uptodate(bh)) {
3506 if (nr_blocks_towrite == blocks_per_page)
3507 nr_blocks_towrite = (unsigned int) (block - block_0);
3508
3509 BUG_ON(nr_blocks_towrite >= blocks_per_page);
3510
3511 // !uptodate but dirty??
3512 if (buffer_dirty(bh))
3513 goto confused;
3514
3515 // Nothing to writeback in this block
3516 bh = bh->b_this_page;
3517 block++;
3518 continue;
3519 }
3520
3521 if (nr_blocks_towrite != blocks_per_page)
3522 // Dirty -> Non-dirty -> Dirty again case
3523 goto confused;
3524
3525 /* Map if needed */
3526 if (!buffer_mapped(bh) || buffer_delay(bh)) {
3527 BUG_ON(bh->b_size != (1 << (inode->i_blkbits)));
3528 ret = sdfat_get_block(inode, block, bh, 1);
3529 if (ret)
3530 goto confused;
3531
3532 if (buffer_new(bh)) {
3533 clear_buffer_new(bh);
3534 unmap_underlying_metadata(bh->b_bdev, bh->b_blocknr);
3535 }
3536 }
3537
3538 /* continuity check */
3539 if (((last_phys + 1) != bh->b_blocknr) && (last_phys != 0)) {
3540 DMSG("Non-contiguous block mapping in single page");
3541 goto confused;
3542 }
3543
3544 last_phys = bh->b_blocknr;
3545 bh = bh->b_this_page;
3546 block++;
3547 } while (bh != head);
3548
3549 if (nr_blocks_towrite == 0) {
3550 DMSG("Page dirty but no dirty bh? alloc_208\n");
3551 goto confused;
3552 }
3553
3554
3555 /* Write-back */
3556 do {
3557 clear_buffer_dirty(bh);
3558 bh = bh->b_this_page;
3559 } while (bh != head);
3560
3561 BUG_ON(PageWriteback(page));
3562 set_page_writeback(page);
3563
3564 /**
3565 * Turn off MAPPED flag in victim's bh if defrag on.
3566 * Another write_begin can starts after get_block for defrag victims called.
3567 * In this case, write_begin calls get_block and get original block number
3568 * and previous defrag will be canceled.
3569 */
3570 if (unlikely(__check_dfr_on(inode,
3571 (loff_t)(page->index << PAGE_SHIFT),
3572 (loff_t)((page->index + 1) << PAGE_SHIFT),
3573 __func__))) {
3574 do {
3575 clear_buffer_mapped(bh);
3576 bh = bh->b_this_page;
3577 } while (bh != head);
3578 }
3579
3580 // Trace # of pages queued (Approx.)
3581 atomic_inc(&SDFAT_SB(sb)->stat_n_pages_queued);
3582
3583 sdfat_submit_fullpage_bio(head->b_bdev,
3584 head->b_blocknr << (inode->i_blkbits - sb->s_blocksize_bits),
3585 nr_blocks_towrite << inode->i_blkbits,
3586 page);
3587
3588 unlock_page(page);
3589
3590 return 0;
3591
3592 confused:
3593 #ifdef CONFIG_SDFAT_TRACE_IO
3594 SDFAT_SB(sb)->stat_n_pages_confused++;
3595 #endif
3596 ret = block_write_full_page(page, sdfat_get_block, wbc);
3597 return ret;
3598 }
3599
3600 static int sdfat_da_writepages(struct address_space *mapping,
3601 struct writeback_control *wbc)
3602 {
3603 MMSG("%s(inode:%p) with nr_to_write = 0x%08lx "
3604 "(ku %d, bg %d, tag %d, rc %d )\n",
3605 __func__, mapping->host, wbc->nr_to_write,
3606 wbc->for_kupdate, wbc->for_background, wbc->tagged_writepages,
3607 wbc->for_reclaim);
3608
3609 ASSERT(mapping->a_ops == &sdfat_da_aops);
3610
3611 #ifdef CONFIG_SDFAT_ALIGNED_MPAGE_WRITE
3612 if (SDFAT_SB(mapping->host->i_sb)->options.adj_req)
3613 return sdfat_mpage_writepages(mapping, wbc, sdfat_get_block);
3614 #endif
3615 return generic_writepages(mapping, wbc);
3616 }
3617
3618 static int sdfat_writepages(struct address_space *mapping,
3619 struct writeback_control *wbc)
3620 {
3621 MMSG("%s(inode:%p) with nr_to_write = 0x%08lx "
3622 "(ku %d, bg %d, tag %d, rc %d )\n",
3623 __func__, mapping->host, wbc->nr_to_write,
3624 wbc->for_kupdate, wbc->for_background, wbc->tagged_writepages,
3625 wbc->for_reclaim);
3626
3627 ASSERT(mapping->a_ops == &sdfat_aops);
3628
3629 #ifdef CONFIG_SDFAT_ALIGNED_MPAGE_WRITE
3630 if (SDFAT_SB(mapping->host->i_sb)->options.adj_req)
3631 return sdfat_mpage_writepages(mapping, wbc, sdfat_get_block);
3632 #endif
3633 return mpage_writepages(mapping, wbc, sdfat_get_block);
3634 }
3635
3636 static void sdfat_write_failed(struct address_space *mapping, loff_t to)
3637 {
3638 struct inode *inode = mapping->host;
3639
3640 if (to > i_size_read(inode)) {
3641 __sdfat_truncate_pagecache(inode, to, i_size_read(inode));
3642 sdfat_truncate(inode, SDFAT_I(inode)->i_size_aligned);
3643 }
3644 }
3645
3646 static int __sdfat_write_begin(struct file *file, struct address_space *mapping,
3647 loff_t pos, unsigned int len,
3648 unsigned int flags, struct page **pagep,
3649 void **fsdata, get_block_t *get_block,
3650 loff_t *bytes, const char *fname)
3651 {
3652 int ret;
3653
3654 __cancel_dfr_work(mapping->host, pos, (loff_t)(pos + len), fname);
3655
3656 *pagep = NULL;
3657 ret = cont_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
3658 get_block, bytes);
3659
3660 if (ret < 0)
3661 sdfat_write_failed(mapping, pos+len);
3662
3663 return ret;
3664 }
3665
3666
3667 static int sdfat_da_write_begin(struct file *file, struct address_space *mapping,
3668 loff_t pos, unsigned int len, unsigned int flags,
3669 struct page **pagep, void **fsdata)
3670 {
3671 return __sdfat_write_begin(file, mapping, pos, len, flags,
3672 pagep, fsdata, sdfat_da_prep_block,
3673 &SDFAT_I(mapping->host)->i_size_aligned,
3674 __func__);
3675 }
3676
3677
3678 static int sdfat_write_begin(struct file *file, struct address_space *mapping,
3679 loff_t pos, unsigned int len, unsigned int flags,
3680 struct page **pagep, void **fsdata)
3681 {
3682 return __sdfat_write_begin(file, mapping, pos, len, flags,
3683 pagep, fsdata, sdfat_get_block,
3684 &SDFAT_I(mapping->host)->i_size_ondisk,
3685 __func__);
3686 }
3687
3688 static int sdfat_write_end(struct file *file, struct address_space *mapping,
3689 loff_t pos, unsigned int len, unsigned int copied,
3690 struct page *pagep, void *fsdata)
3691 {
3692 struct inode *inode = mapping->host;
3693 FILE_ID_T *fid = &(SDFAT_I(inode)->fid);
3694 int err;
3695
3696 err = generic_write_end(file, mapping, pos, len, copied, pagep, fsdata);
3697
3698 /* FOR GRACEFUL ERROR HANDLING */
3699 if (SDFAT_I(inode)->i_size_aligned < i_size_read(inode)) {
3700 sdfat_fs_error(inode->i_sb, "invalid size(size(%llu) "
3701 "> aligned(%llu)\n", i_size_read(inode),
3702 SDFAT_I(inode)->i_size_aligned);
3703 sdfat_debug_bug_on(1);
3704 }
3705
3706 if (err < len)
3707 sdfat_write_failed(mapping, pos+len);
3708
3709 if (!(err < 0) && !(fid->attr & ATTR_ARCHIVE)) {
3710 inode->i_mtime = inode->i_ctime = CURRENT_TIME_SEC;
3711 fid->attr |= ATTR_ARCHIVE;
3712 mark_inode_dirty(inode);
3713 }
3714
3715 return err;
3716 }
3717
3718 static inline ssize_t __sdfat_direct_IO(int rw, struct kiocb *iocb,
3719 struct inode *inode, void *iov_u, loff_t offset,
3720 loff_t count, unsigned long nr_segs)
3721 {
3722 struct address_space *mapping = inode->i_mapping;
3723 loff_t size = offset + count;
3724 ssize_t ret;
3725
3726 if (rw == WRITE) {
3727 /*
3728 * FIXME: blockdev_direct_IO() doesn't use ->write_begin(),
3729 * so we need to update the ->i_size_aligned to block boundary.
3730 *
3731 * But we must fill the remaining area or hole by nul for
3732 * updating ->i_size_aligned
3733 *
3734 * Return 0, and fallback to normal buffered write.
3735 */
3736 if (SDFAT_I(inode)->i_size_aligned < size)
3737 return 0;
3738 }
3739
3740 /*
3741 * sdFAT need to use the DIO_LOCKING for avoiding the race
3742 * condition of sdfat_get_block() and ->truncate().
3743 */
3744 ret = __sdfat_blkdev_direct_IO(rw, iocb, inode, iov_u, offset, nr_segs);
3745 if (ret < 0 && (rw & WRITE))
3746 sdfat_write_failed(mapping, size);
3747
3748 return ret;
3749 }
3750
3751 static const struct address_space_operations sdfat_aops = {
3752 .readpage = sdfat_readpage,
3753 .readpages = sdfat_readpages,
3754 .writepage = sdfat_writepage,
3755 .writepages = sdfat_writepages,
3756 .write_begin = sdfat_write_begin,
3757 .write_end = sdfat_write_end,
3758 .direct_IO = sdfat_direct_IO,
3759 .bmap = sdfat_aop_bmap
3760 };
3761
3762 static const struct address_space_operations sdfat_da_aops = {
3763 .readpage = sdfat_readpage,
3764 .readpages = sdfat_readpages,
3765 .writepage = sdfat_writepage,
3766 .writepages = sdfat_da_writepages,
3767 .write_begin = sdfat_da_write_begin,
3768 .write_end = sdfat_write_end,
3769 .direct_IO = sdfat_direct_IO,
3770 .bmap = sdfat_aop_bmap
3771 };
3772
3773 /*======================================================================*/
3774 /* Super Operations */
3775 /*======================================================================*/
3776
3777 static inline unsigned long sdfat_hash(loff_t i_pos)
3778 {
3779 return hash_32(i_pos, SDFAT_HASH_BITS);
3780 }
3781
3782 static void sdfat_attach(struct inode *inode, loff_t i_pos)
3783 {
3784 struct sdfat_sb_info *sbi = SDFAT_SB(inode->i_sb);
3785 struct hlist_head *head = sbi->inode_hashtable + sdfat_hash(i_pos);
3786
3787 spin_lock(&sbi->inode_hash_lock);
3788 SDFAT_I(inode)->i_pos = i_pos;
3789 hlist_add_head(&SDFAT_I(inode)->i_hash_fat, head);
3790 spin_unlock(&sbi->inode_hash_lock);
3791 }
3792
3793 static void sdfat_detach(struct inode *inode)
3794 {
3795 struct sdfat_sb_info *sbi = SDFAT_SB(inode->i_sb);
3796
3797 spin_lock(&sbi->inode_hash_lock);
3798 hlist_del_init(&SDFAT_I(inode)->i_hash_fat);
3799 SDFAT_I(inode)->i_pos = 0;
3800 spin_unlock(&sbi->inode_hash_lock);
3801 }
3802
3803
3804 /* doesn't deal with root inode */
3805 static int sdfat_fill_inode(struct inode *inode, const FILE_ID_T *fid)
3806 {
3807 struct sdfat_sb_info *sbi = SDFAT_SB(inode->i_sb);
3808 FS_INFO_T *fsi = &(sbi->fsi);
3809 DIR_ENTRY_T info;
3810 u64 size = fid->size;
3811
3812 memcpy(&(SDFAT_I(inode)->fid), fid, sizeof(FILE_ID_T));
3813
3814 SDFAT_I(inode)->i_pos = 0;
3815 SDFAT_I(inode)->target = NULL;
3816 inode->i_uid = sbi->options.fs_uid;
3817 inode->i_gid = sbi->options.fs_gid;
3818 inode->i_version++;
3819 inode->i_generation = get_seconds();
3820
3821 if (fsapi_read_inode(inode, &info) < 0) {
3822 MMSG("%s: failed to read stat!\n", __func__);
3823 return -EIO;
3824 }
3825
3826 if (info.Attr & ATTR_SUBDIR) { /* directory */
3827 inode->i_generation &= ~1;
3828 inode->i_mode = sdfat_make_mode(sbi, info.Attr, S_IRWXUGO);
3829 inode->i_op = &sdfat_dir_inode_operations;
3830 inode->i_fop = &sdfat_dir_operations;
3831
3832 set_nlink(inode, info.NumSubdirs);
3833 } else if (info.Attr & ATTR_SYMLINK) { /* symbolic link */
3834 inode->i_op = &sdfat_symlink_inode_operations;
3835 inode->i_generation |= 1;
3836 inode->i_mode = sdfat_make_mode(sbi, info.Attr, S_IRWXUGO);
3837 } else { /* regular file */
3838 inode->i_generation |= 1;
3839 inode->i_mode = sdfat_make_mode(sbi, info.Attr, S_IRWXUGO);
3840 inode->i_op = &sdfat_file_inode_operations;
3841 inode->i_fop = &sdfat_file_operations;
3842
3843 if (sbi->options.improved_allocation & SDFAT_ALLOC_DELAY)
3844 inode->i_mapping->a_ops = &sdfat_da_aops;
3845 else
3846 inode->i_mapping->a_ops = &sdfat_aops;
3847
3848 inode->i_mapping->nrpages = 0;
3849
3850 }
3851
3852 /*
3853 * Use fid->size instead of info.Size
3854 * because info.Size means the value saved on disk
3855 */
3856 i_size_write(inode, size);
3857
3858 /* ondisk and aligned size should be aligned with block size */
3859 if (size & (inode->i_sb->s_blocksize - 1)) {
3860 size |= (inode->i_sb->s_blocksize - 1);
3861 size++;
3862 }
3863
3864 SDFAT_I(inode)->i_size_aligned = size;
3865 SDFAT_I(inode)->i_size_ondisk = size;
3866 sdfat_debug_check_clusters(inode);
3867
3868 sdfat_save_attr(inode, info.Attr);
3869
3870 inode->i_blocks = ((i_size_read(inode) + (fsi->cluster_size - 1))
3871 & ~((loff_t)fsi->cluster_size - 1)) >> inode->i_blkbits;
3872
3873 sdfat_time_fat2unix(sbi, &inode->i_mtime, &info.ModifyTimestamp);
3874 sdfat_time_fat2unix(sbi, &inode->i_ctime, &info.CreateTimestamp);
3875 sdfat_time_fat2unix(sbi, &inode->i_atime, &info.AccessTimestamp);
3876
3877 __init_dfr_info(inode);
3878
3879 return 0;
3880 }
3881
3882 static struct inode *sdfat_build_inode(struct super_block *sb,
3883 const FILE_ID_T *fid, loff_t i_pos) {
3884 struct inode *inode;
3885 int err;
3886
3887 inode = sdfat_iget(sb, i_pos);
3888 if (inode)
3889 goto out;
3890 inode = new_inode(sb);
3891 if (!inode) {
3892 inode = ERR_PTR(-ENOMEM);
3893 goto out;
3894 }
3895 inode->i_ino = iunique(sb, SDFAT_ROOT_INO);
3896 inode->i_version = 1;
3897 err = sdfat_fill_inode(inode, fid);
3898 if (err) {
3899 iput(inode);
3900 inode = ERR_PTR(err);
3901 goto out;
3902 }
3903 sdfat_attach(inode, i_pos);
3904 insert_inode_hash(inode);
3905 out:
3906 return inode;
3907 }
3908
3909 static struct inode *sdfat_alloc_inode(struct super_block *sb)
3910 {
3911 struct sdfat_inode_info *ei;
3912
3913 ei = kmem_cache_alloc(sdfat_inode_cachep, GFP_NOFS);
3914 if (!ei)
3915 return NULL;
3916 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 4, 0)
3917 init_rwsem(&ei->truncate_lock);
3918 #endif
3919 return &ei->vfs_inode;
3920 }
3921
3922 static void sdfat_destroy_inode(struct inode *inode)
3923 {
3924 if (SDFAT_I(inode)->target) {
3925 kfree(SDFAT_I(inode)->target);
3926 SDFAT_I(inode)->target = NULL;
3927 }
3928
3929 kmem_cache_free(sdfat_inode_cachep, SDFAT_I(inode));
3930 }
3931
3932 static int __sdfat_write_inode(struct inode *inode, int sync)
3933 {
3934 struct super_block *sb = inode->i_sb;
3935 struct sdfat_sb_info *sbi = SDFAT_SB(sb);
3936 DIR_ENTRY_T info;
3937
3938 if (inode->i_ino == SDFAT_ROOT_INO)
3939 return 0;
3940
3941 info.Attr = sdfat_make_attr(inode);
3942 info.Size = i_size_read(inode);
3943
3944 sdfat_time_unix2fat(sbi, &inode->i_mtime, &info.ModifyTimestamp);
3945 sdfat_time_unix2fat(sbi, &inode->i_ctime, &info.CreateTimestamp);
3946 sdfat_time_unix2fat(sbi, &inode->i_atime, &info.AccessTimestamp);
3947
3948 if (!__support_write_inode_sync(sb))
3949 sync = 0;
3950
3951 /* FIXME : Do we need handling error? */
3952 return fsapi_write_inode(inode, &info, sync);
3953 }
3954
3955 static int sdfat_sync_inode(struct inode *inode)
3956 {
3957 return __sdfat_write_inode(inode, 1);
3958 }
3959
3960 static int sdfat_write_inode(struct inode *inode, struct writeback_control *wbc)
3961 {
3962 return __sdfat_write_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
3963 }
3964
3965 static void sdfat_evict_inode(struct inode *inode)
3966 {
3967 truncate_inode_pages(&inode->i_data, 0);
3968
3969 if (!inode->i_nlink) {
3970 loff_t old_size = i_size_read(inode);
3971
3972 i_size_write(inode, 0);
3973
3974 SDFAT_I(inode)->fid.size = old_size;
3975
3976 __cancel_dfr_work(inode, 0, (loff_t)old_size, __func__);
3977
3978 /* TO CHECK evicting directory works correctly */
3979 MMSG("%s: inode(%p) evict %s (size(%llu) to zero)\n",
3980 __func__, inode,
3981 S_ISDIR(inode->i_mode) ? "directory" : "file",
3982 (u64)old_size);
3983 fsapi_truncate(inode, old_size, 0);
3984 }
3985
3986 invalidate_inode_buffers(inode);
3987 clear_inode(inode);
3988 fsapi_invalidate_extent(inode);
3989 sdfat_detach(inode);
3990
3991 /* after end of this function, caller will remove inode hash */
3992 /* remove_inode_hash(inode); */
3993 }
3994
3995
3996
3997 static void sdfat_put_super(struct super_block *sb)
3998 {
3999 struct sdfat_sb_info *sbi = SDFAT_SB(sb);
4000 int err;
4001
4002 sdfat_log_msg(sb, KERN_INFO, "trying to unmount...");
4003
4004 __cancel_delayed_work_sync(sbi);
4005
4006 if (__is_sb_dirty(sb))
4007 sdfat_write_super(sb);
4008
4009 __free_dfr_mem_if_required(sb);
4010 err = fsapi_umount(sb);
4011
4012 if (sbi->nls_disk) {
4013 unload_nls(sbi->nls_disk);
4014 sbi->nls_disk = NULL;
4015 sbi->options.codepage = sdfat_default_codepage;
4016 }
4017 if (sbi->nls_io) {
4018 unload_nls(sbi->nls_io);
4019 sbi->nls_io = NULL;
4020 }
4021 if (sbi->options.iocharset != sdfat_default_iocharset) {
4022 kfree(sbi->options.iocharset);
4023 sbi->options.iocharset = sdfat_default_iocharset;
4024 }
4025
4026 sb->s_fs_info = NULL;
4027
4028 kobject_del(&sbi->sb_kobj);
4029 kobject_put(&sbi->sb_kobj);
4030 if (!sbi->use_vmalloc)
4031 kfree(sbi);
4032 else
4033 vfree(sbi);
4034
4035 sdfat_log_msg(sb, KERN_INFO, "unmounted successfully! %s",
4036 err ? "(with previous I/O errors)" : "");
4037 }
4038
4039 static inline void __flush_delayed_meta(struct super_block *sb, s32 sync)
4040 {
4041 #ifdef CONFIG_SDFAT_DELAYED_META_DIRTY
4042 fsapi_cache_flush(sb, sync);
4043 #else
4044 /* DO NOTHING */
4045 #endif
4046 }
4047
4048 static void sdfat_write_super(struct super_block *sb)
4049 {
4050 int time = 0;
4051
4052 __lock_super(sb);
4053
4054 __set_sb_clean(sb);
4055
4056 #ifdef CONFIG_SDFAT_DFR
4057 if (atomic_read(&(SDFAT_SB(sb)->dfr_info.stat)) == DFR_SB_STAT_VALID)
4058 fsapi_dfr_update_fat_next(sb);
4059 #endif
4060
4061 /* flush delayed FAT/DIR dirty */
4062 __flush_delayed_meta(sb, 0);
4063
4064 if (!(sb->s_flags & MS_RDONLY))
4065 fsapi_sync_fs(sb, 0);
4066
4067 __unlock_super(sb);
4068
4069 time = jiffies;
4070
4071 /* Issuing bdev requests is needed
4072 * to guarantee DIR updates in time
4073 * whether w/ or w/o delayed DIR dirty feature.
4074 * (otherwise DIR updates could be delayed for 5 + 5 secs at max.)
4075 */
4076 sync_blockdev(sb->s_bdev);
4077
4078 #if (defined(CONFIG_SDFAT_DFR) && defined(CONFIG_SDFAT_DFR_DEBUG))
4079 /* SPO test */
4080 fsapi_dfr_spo_test(sb, DFR_SPO_FAT_NEXT, __func__);
4081 #endif
4082 MMSG("BD: sdfat_write_super (bdev_sync for %ld ms)\n",
4083 (jiffies - time) * 1000 / HZ);
4084 }
4085
4086
4087 static void __dfr_update_fat_next(struct super_block *sb)
4088 {
4089 #ifdef CONFIG_SDFAT_DFR
4090 struct sdfat_sb_info *sbi = SDFAT_SB(sb);
4091
4092 if (sbi->options.defrag &&
4093 (atomic_read(&sbi->dfr_info.stat) == DFR_SB_STAT_VALID)) {
4094 fsapi_dfr_update_fat_next(sb);
4095 }
4096 #endif
4097 }
4098
4099 static void __dfr_update_fat_prev(struct super_block *sb, int wait)
4100 {
4101 #ifdef CONFIG_SDFAT_DFR
4102 struct sdfat_sb_info *sbi = SDFAT_SB(sb);
4103 struct defrag_info *sb_dfr = &sbi->dfr_info;
4104 /* static time available? */
4105 static int time; /* initialized by zero */
4106 int uevent = 0, total = 0, clean = 0, full = 0;
4107 int spent = jiffies - time;
4108
4109 if (!(sbi->options.defrag && wait))
4110 return;
4111
4112 __lock_super(sb);
4113 /* Update FAT for defrag */
4114 if (atomic_read(&(sbi->dfr_info.stat)) == DFR_SB_STAT_VALID) {
4115
4116 fsapi_dfr_update_fat_prev(sb, 0);
4117
4118 /* flush delayed FAT/DIR dirty */
4119 __flush_delayed_meta(sb, 0);
4120
4121 /* Complete defrag req */
4122 fsapi_sync_fs(sb, 1);
4123 atomic_set(&sb_dfr->stat, DFR_SB_STAT_REQ);
4124 complete_all(&sbi->dfr_complete);
4125 } else if (((spent < 0) || (spent > DFR_DEFAULT_TIMEOUT)) &&
4126 (atomic_read(&(sbi->dfr_info.stat)) == DFR_SB_STAT_IDLE)) {
4127 uevent = fsapi_dfr_check_dfr_required(sb, &total, &clean, &full);
4128 time = jiffies;
4129 }
4130 __unlock_super(sb);
4131
4132 if (uevent) {
4133 kobject_uevent(&SDFAT_SB(sb)->sb_kobj, KOBJ_CHANGE);
4134 dfr_debug("uevent for defrag_daemon, total_au %d, "
4135 "clean_au %d, full_au %d", total, clean, full);
4136 }
4137 #endif
4138 }
4139
4140 static int sdfat_sync_fs(struct super_block *sb, int wait)
4141 {
4142 int err = 0;
4143
4144 /* If there are some dirty buffers in the bdev inode */
4145 if (__is_sb_dirty(sb)) {
4146 __lock_super(sb);
4147 __set_sb_clean(sb);
4148
4149 __dfr_update_fat_next(sb);
4150
4151 err = fsapi_sync_fs(sb, 1);
4152
4153 #if (defined(CONFIG_SDFAT_DFR) && defined(CONFIG_SDFAT_DFR_DEBUG))
4154 /* SPO test */
4155 fsapi_dfr_spo_test(sb, DFR_SPO_FAT_NEXT, __func__);
4156 #endif
4157
4158 __unlock_super(sb);
4159 }
4160
4161 __dfr_update_fat_prev(sb, wait);
4162
4163 return err;
4164 }
4165
4166 static int sdfat_statfs(struct dentry *dentry, struct kstatfs *buf)
4167 {
4168 /*
4169 * patch 1.2.2 :
4170 * fixed the slow-call problem because of volume-lock contention.
4171 */
4172 struct super_block *sb = dentry->d_sb;
4173 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
4174 FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
4175 VOL_INFO_T info;
4176
4177 /* fsapi_statfs will try to get a volume lock if needed */
4178 if (fsapi_statfs(sb, &info))
4179 return -EIO;
4180
4181 if (fsi->prev_eio)
4182 sdfat_msg(sb, KERN_INFO, "called statfs with previous"
4183 " I/O error(0x%02X).", fsi->prev_eio);
4184
4185 buf->f_type = sb->s_magic;
4186 buf->f_bsize = info.ClusterSize;
4187 buf->f_blocks = info.NumClusters;
4188 buf->f_bfree = info.FreeClusters;
4189 buf->f_bavail = info.FreeClusters;
4190 buf->f_fsid.val[0] = (u32)id;
4191 buf->f_fsid.val[1] = (u32)(id >> 32);
4192 buf->f_namelen = 260;
4193
4194 return 0;
4195 }
4196
4197 static int sdfat_remount(struct super_block *sb, int *flags, char *data)
4198 {
4199 unsigned long prev_sb_flags;
4200 char *orig_data = kstrdup(data, GFP_KERNEL);
4201 struct sdfat_sb_info *sbi = SDFAT_SB(sb);
4202 FS_INFO_T *fsi = &(sbi->fsi);
4203
4204 *flags |= MS_NODIRATIME;
4205
4206 prev_sb_flags = sb->s_flags;
4207
4208 sdfat_remount_syncfs(sb);
4209
4210 fsapi_set_vol_flags(sb, VOL_CLEAN, 1);
4211
4212 sdfat_log_msg(sb, KERN_INFO, "re-mounted(%s->%s), eio=0x%x, Opts: %s",
4213 (prev_sb_flags & MS_RDONLY) ? "ro" : "rw",
4214 (*flags & MS_RDONLY) ? "ro" : "rw",
4215 fsi->prev_eio, orig_data);
4216 kfree(orig_data);
4217 return 0;
4218 }
4219
4220 static int __sdfat_show_options(struct seq_file *m, struct super_block *sb)
4221 {
4222 struct sdfat_sb_info *sbi = SDFAT_SB(sb);
4223 struct sdfat_mount_options *opts = &sbi->options;
4224 FS_INFO_T *fsi = &(sbi->fsi);
4225
4226 /* Show partition info */
4227 seq_printf(m, ",fs=%s", sdfat_get_vol_type_str(fsi->vol_type));
4228 if (fsi->prev_eio)
4229 seq_printf(m, ",eio=0x%x", fsi->prev_eio);
4230 if (!uid_eq(opts->fs_uid, GLOBAL_ROOT_UID))
4231 seq_printf(m, ",uid=%u",
4232 from_kuid_munged(&init_user_ns, opts->fs_uid));
4233 if (!gid_eq(opts->fs_gid, GLOBAL_ROOT_GID))
4234 seq_printf(m, ",gid=%u",
4235 from_kgid_munged(&init_user_ns, opts->fs_gid));
4236 seq_printf(m, ",fmask=%04o", opts->fs_fmask);
4237 seq_printf(m, ",dmask=%04o", opts->fs_dmask);
4238 if (opts->allow_utime)
4239 seq_printf(m, ",allow_utime=%04o", opts->allow_utime);
4240 if (sbi->nls_disk)
4241 seq_printf(m, ",codepage=%s", sbi->nls_disk->charset);
4242 if (sbi->nls_io)
4243 seq_printf(m, ",iocharset=%s", sbi->nls_io->charset);
4244 if (opts->utf8)
4245 seq_puts(m, ",utf8");
4246 if (sbi->fsi.vol_type != EXFAT)
4247 seq_puts(m, ",shortname=winnt");
4248 seq_printf(m, ",namecase=%u", opts->casesensitive);
4249 if (opts->tz_utc)
4250 seq_puts(m, ",tz=UTC");
4251 if (opts->improved_allocation & SDFAT_ALLOC_DELAY)
4252 seq_puts(m, ",delay");
4253 if (opts->improved_allocation & SDFAT_ALLOC_SMART)
4254 seq_printf(m, ",smart,ausize=%u", opts->amap_opt.sect_per_au);
4255 if (opts->defrag)
4256 seq_puts(m, ",defrag");
4257 if (opts->adj_hidsect)
4258 seq_puts(m, ",adj_hid");
4259 if (opts->adj_req)
4260 seq_puts(m, ",adj_req");
4261 seq_printf(m, ",symlink=%u", opts->symlink);
4262 seq_printf(m, ",bps=%ld", sb->s_blocksize);
4263 if (opts->errors == SDFAT_ERRORS_CONT)
4264 seq_puts(m, ",errors=continue");
4265 else if (opts->errors == SDFAT_ERRORS_PANIC)
4266 seq_puts(m, ",errors=panic");
4267 else
4268 seq_puts(m, ",errors=remount-ro");
4269 if (opts->discard)
4270 seq_puts(m, ",discard");
4271
4272 return 0;
4273 }
4274
4275 static const struct super_operations sdfat_sops = {
4276 .alloc_inode = sdfat_alloc_inode,
4277 .destroy_inode = sdfat_destroy_inode,
4278 .write_inode = sdfat_write_inode,
4279 .evict_inode = sdfat_evict_inode,
4280 .put_super = sdfat_put_super,
4281 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 7, 0)
4282 .write_super = sdfat_write_super,
4283 #endif
4284 .sync_fs = sdfat_sync_fs,
4285 .statfs = sdfat_statfs,
4286 .remount_fs = sdfat_remount,
4287 .show_options = sdfat_show_options,
4288 };
4289
4290 /*======================================================================*/
4291 /* SYSFS Operations */
4292 /*======================================================================*/
4293 #define SDFAT_ATTR(name, mode, show, store) \
4294 static struct sdfat_attr sdfat_attr_##name = __ATTR(name, mode, show, store)
4295
4296 struct sdfat_attr {
4297 struct attribute attr;
4298 ssize_t (*show)(struct sdfat_sb_info *, char *);
4299 ssize_t (*store)(struct sdfat_sb_info *, const char *, size_t);
4300 };
4301
4302 static ssize_t sdfat_attr_show(struct kobject *kobj, struct attribute *attr, char *buf)
4303 {
4304 struct sdfat_sb_info *sbi = container_of(kobj, struct sdfat_sb_info, sb_kobj);
4305 struct sdfat_attr *a = container_of(attr, struct sdfat_attr, attr);
4306
4307 return a->show ? a->show(sbi, buf) : 0;
4308 }
4309
4310 static ssize_t sdfat_attr_store(struct kobject *kobj, struct attribute *attr,
4311 const char *buf, size_t len)
4312 {
4313 struct sdfat_sb_info *sbi = container_of(kobj, struct sdfat_sb_info, sb_kobj);
4314 struct sdfat_attr *a = container_of(attr, struct sdfat_attr, attr);
4315
4316 return a->store ? a->store(sbi, buf, len) : len;
4317 }
4318
4319 static const struct sysfs_ops sdfat_attr_ops = {
4320 .show = sdfat_attr_show,
4321 .store = sdfat_attr_store,
4322 };
4323
4324
4325 static ssize_t type_show(struct sdfat_sb_info *sbi, char *buf)
4326 {
4327 FS_INFO_T *fsi = &(sbi->fsi);
4328
4329 return snprintf(buf, PAGE_SIZE, "%s\n", sdfat_get_vol_type_str(fsi->vol_type));
4330 }
4331 SDFAT_ATTR(type, 0444, type_show, NULL);
4332
4333 static ssize_t eio_show(struct sdfat_sb_info *sbi, char *buf)
4334 {
4335 FS_INFO_T *fsi = &(sbi->fsi);
4336
4337 return snprintf(buf, PAGE_SIZE, "0x%x\n", fsi->prev_eio);
4338 }
4339 SDFAT_ATTR(eio, 0444, eio_show, NULL);
4340
4341 static ssize_t fratio_show(struct sdfat_sb_info *sbi, char *buf)
4342 {
4343 unsigned int n_total_au = 0;
4344 unsigned int n_clean_au = 0;
4345 unsigned int n_full_au = 0;
4346 unsigned int n_dirty_au = 0;
4347 unsigned int fr = 0;
4348
4349 n_total_au = fsapi_get_au_stat(sbi->host_sb, VOL_AU_STAT_TOTAL);
4350 n_clean_au = fsapi_get_au_stat(sbi->host_sb, VOL_AU_STAT_CLEAN);
4351 n_full_au = fsapi_get_au_stat(sbi->host_sb, VOL_AU_STAT_FULL);
4352 n_dirty_au = n_total_au - (n_full_au + n_clean_au);
4353
4354 if (!n_dirty_au)
4355 fr = 0;
4356 else if (!n_clean_au)
4357 fr = 100;
4358 else
4359 fr = (n_dirty_au * 100) / (n_clean_au + n_dirty_au);
4360
4361 return snprintf(buf, PAGE_SIZE, "%u\n", fr);
4362 }
4363 SDFAT_ATTR(fratio, 0444, fratio_show, NULL);
4364
4365 static ssize_t totalau_show(struct sdfat_sb_info *sbi, char *buf)
4366 {
4367 unsigned int n_au = 0;
4368
4369 n_au = fsapi_get_au_stat(sbi->host_sb, VOL_AU_STAT_TOTAL);
4370 return snprintf(buf, PAGE_SIZE, "%u\n", n_au);
4371 }
4372 SDFAT_ATTR(totalau, 0444, totalau_show, NULL);
4373
4374 static ssize_t cleanau_show(struct sdfat_sb_info *sbi, char *buf)
4375 {
4376 unsigned int n_clean_au = 0;
4377
4378 n_clean_au = fsapi_get_au_stat(sbi->host_sb, VOL_AU_STAT_CLEAN);
4379 return snprintf(buf, PAGE_SIZE, "%u\n", n_clean_au);
4380 }
4381 SDFAT_ATTR(cleanau, 0444, cleanau_show, NULL);
4382
4383 static ssize_t fullau_show(struct sdfat_sb_info *sbi, char *buf)
4384 {
4385 unsigned int n_full_au = 0;
4386
4387 n_full_au = fsapi_get_au_stat(sbi->host_sb, VOL_AU_STAT_FULL);
4388 return snprintf(buf, PAGE_SIZE, "%u\n", n_full_au);
4389 }
4390 SDFAT_ATTR(fullau, 0444, fullau_show, NULL);
4391
4392 static struct attribute *sdfat_attrs[] = {
4393 &sdfat_attr_type.attr,
4394 &sdfat_attr_eio.attr,
4395 &sdfat_attr_fratio.attr,
4396 &sdfat_attr_totalau.attr,
4397 &sdfat_attr_cleanau.attr,
4398 &sdfat_attr_fullau.attr,
4399 NULL,
4400 };
4401
4402 static struct kobj_type sdfat_ktype = {
4403 .default_attrs = sdfat_attrs,
4404 .sysfs_ops = &sdfat_attr_ops,
4405 };
4406
4407 static ssize_t version_show(struct kobject *kobj,
4408 struct kobj_attribute *attr, char *buff)
4409 {
4410 return snprintf(buff, PAGE_SIZE, "FS Version %s\n", SDFAT_VERSION);
4411 }
4412
4413 static struct kobj_attribute version_attr = __ATTR_RO(version);
4414
4415 static struct attribute *attributes[] = {
4416 &version_attr.attr,
4417 NULL,
4418 };
4419
4420 static struct attribute_group attr_group = {
4421 .attrs = attributes,
4422 };
4423
4424 /*======================================================================*/
4425 /* Super Block Read Operations */
4426 /*======================================================================*/
4427
4428 enum {
4429 Opt_uid,
4430 Opt_gid,
4431 Opt_umask,
4432 Opt_dmask,
4433 Opt_fmask,
4434 Opt_allow_utime,
4435 Opt_codepage,
4436 Opt_charset,
4437 Opt_utf8,
4438 Opt_namecase,
4439 Opt_tz_utc,
4440 Opt_adj_hidsect,
4441 Opt_delay,
4442 Opt_smart,
4443 Opt_ausize,
4444 Opt_packing,
4445 Opt_defrag,
4446 Opt_symlink,
4447 Opt_debug,
4448 Opt_err_cont,
4449 Opt_err_panic,
4450 Opt_err_ro,
4451 Opt_err,
4452 Opt_discard,
4453 Opt_fs,
4454 Opt_adj_req,
4455 };
4456
4457 static const match_table_t sdfat_tokens = {
4458 {Opt_uid, "uid=%u"},
4459 {Opt_gid, "gid=%u"},
4460 {Opt_umask, "umask=%o"},
4461 {Opt_dmask, "dmask=%o"},
4462 {Opt_fmask, "fmask=%o"},
4463 {Opt_allow_utime, "allow_utime=%o"},
4464 {Opt_codepage, "codepage=%u"},
4465 {Opt_charset, "iocharset=%s"},
4466 {Opt_utf8, "utf8"},
4467 {Opt_namecase, "namecase=%u"},
4468 {Opt_tz_utc, "tz=UTC"},
4469 {Opt_adj_hidsect, "adj_hid"},
4470 {Opt_delay, "delay"},
4471 {Opt_smart, "smart"},
4472 {Opt_ausize, "ausize=%u"},
4473 {Opt_packing, "packing=%u"},
4474 {Opt_defrag, "defrag"},
4475 {Opt_symlink, "symlink=%u"},
4476 {Opt_debug, "debug"},
4477 {Opt_err_cont, "errors=continue"},
4478 {Opt_err_panic, "errors=panic"},
4479 {Opt_err_ro, "errors=remount-ro"},
4480 {Opt_discard, "discard"},
4481 {Opt_fs, "fs=%s"},
4482 {Opt_adj_req, "adj_req"},
4483 {Opt_err, NULL}
4484 };
4485
4486 static int parse_options(struct super_block *sb, char *options, int silent,
4487 int *debug, struct sdfat_mount_options *opts)
4488 {
4489 char *p;
4490 substring_t args[MAX_OPT_ARGS];
4491 int option, i;
4492 char *tmpstr;
4493
4494 opts->fs_uid = current_uid();
4495 opts->fs_gid = current_gid();
4496 opts->fs_fmask = opts->fs_dmask = current->fs->umask;
4497 opts->allow_utime = (unsigned short) -1;
4498 opts->codepage = sdfat_default_codepage;
4499 opts->iocharset = sdfat_default_iocharset;
4500 opts->casesensitive = 0;
4501 opts->utf8 = 0;
4502 opts->adj_hidsect = 0;
4503 opts->tz_utc = 0;
4504 opts->improved_allocation = 0;
4505 opts->amap_opt.pack_ratio = 0; // Default packing
4506 opts->amap_opt.sect_per_au = 0;
4507 opts->amap_opt.misaligned_sect = 0;
4508 opts->symlink = 0;
4509 opts->errors = SDFAT_ERRORS_RO;
4510 opts->discard = 0;
4511 *debug = 0;
4512
4513 if (!options)
4514 goto out;
4515
4516 while ((p = strsep(&options, ",")) != NULL) {
4517 int token;
4518
4519 if (!*p)
4520 continue;
4521 token = match_token(p, sdfat_tokens, args);
4522 switch (token) {
4523 case Opt_uid:
4524 if (match_int(&args[0], &option))
4525 return 0;
4526 opts->fs_uid = make_kuid(current_user_ns(), option);
4527 break;
4528 case Opt_gid:
4529 if (match_int(&args[0], &option))
4530 return 0;
4531 opts->fs_gid = make_kgid(current_user_ns(), option);
4532 break;
4533 case Opt_umask:
4534 case Opt_dmask:
4535 case Opt_fmask:
4536 if (match_octal(&args[0], &option))
4537 return 0;
4538 if (token != Opt_dmask)
4539 opts->fs_fmask = option;
4540 if (token != Opt_fmask)
4541 opts->fs_dmask = option;
4542 break;
4543 case Opt_allow_utime:
4544 if (match_octal(&args[0], &option))
4545 return 0;
4546 opts->allow_utime = option & (S_IWGRP | S_IWOTH);
4547 break;
4548 case Opt_codepage:
4549 if (match_int(&args[0], &option))
4550 return 0;
4551 opts->codepage = option;
4552 break;
4553 case Opt_charset:
4554 if (opts->iocharset != sdfat_default_iocharset)
4555 kfree(opts->iocharset);
4556 tmpstr = match_strdup(&args[0]);
4557 if (!tmpstr)
4558 return -ENOMEM;
4559 opts->iocharset = tmpstr;
4560 break;
4561 case Opt_namecase:
4562 if (match_int(&args[0], &option))
4563 return 0;
4564 opts->casesensitive = (option > 0) ? 1:0;
4565 break;
4566 case Opt_utf8:
4567 opts->utf8 = 1;
4568 break;
4569 case Opt_adj_hidsect:
4570 opts->adj_hidsect = 1;
4571 break;
4572 case Opt_tz_utc:
4573 opts->tz_utc = 1;
4574 break;
4575 case Opt_symlink:
4576 if (match_int(&args[0], &option))
4577 return 0;
4578 opts->symlink = option > 0 ? 1 : 0;
4579 break;
4580 case Opt_delay:
4581 opts->improved_allocation |= SDFAT_ALLOC_DELAY;
4582 break;
4583 case Opt_smart:
4584 opts->improved_allocation |= SDFAT_ALLOC_SMART;
4585 break;
4586 case Opt_ausize:
4587 if (match_int(&args[0], &option))
4588 return -EINVAL;
4589 if (!is_power_of_2(option))
4590 return -EINVAL;
4591 opts->amap_opt.sect_per_au = option;
4592 IMSG("set AU size by option : %u sectors\n", option);
4593 break;
4594 case Opt_packing:
4595 if (match_int(&args[0], &option))
4596 return 0;
4597 opts->amap_opt.pack_ratio = option;
4598 break;
4599 case Opt_defrag:
4600 #ifdef CONFIG_SDFAT_DFR
4601 opts->defrag = 1;
4602 #else
4603 IMSG("defragmentation config is not enabled. ignore\n");
4604 #endif
4605 break;
4606 case Opt_err_cont:
4607 opts->errors = SDFAT_ERRORS_CONT;
4608 break;
4609 case Opt_err_panic:
4610 opts->errors = SDFAT_ERRORS_PANIC;
4611 break;
4612 case Opt_err_ro:
4613 opts->errors = SDFAT_ERRORS_RO;
4614 break;
4615 case Opt_debug:
4616 *debug = 1;
4617 break;
4618 case Opt_discard:
4619 opts->discard = 1;
4620 break;
4621 case Opt_fs:
4622 tmpstr = match_strdup(&args[0]);
4623 if (!tmpstr)
4624 return -ENOMEM;
4625 for (i = 0; i < FS_TYPE_MAX; i++) {
4626 if (!strcmp(tmpstr, FS_TYPE_STR[i])) {
4627 opts->fs_type = (unsigned char)i;
4628 sdfat_log_msg(sb, KERN_ERR,
4629 "set fs-type by option : %s",
4630 FS_TYPE_STR[i]);
4631 break;
4632 }
4633 }
4634 kfree(tmpstr);
4635 if (i == FS_TYPE_MAX) {
4636 sdfat_log_msg(sb, KERN_ERR,
4637 "invalid fs-type, "
4638 "only allow auto, exfat, vfat");
4639 return -EINVAL;
4640 }
4641 break;
4642 case Opt_adj_req:
4643 #ifdef CONFIG_SDFAT_ALIGNED_MPAGE_WRITE
4644 opts->adj_req = 1;
4645 #else
4646 IMSG("adjust request config is not enabled. ignore\n");
4647 #endif
4648 break;
4649 default:
4650 if (!silent) {
4651 sdfat_msg(sb, KERN_ERR,
4652 "unrecognized mount option \"%s\" "
4653 "or missing value", p);
4654 }
4655 return -EINVAL;
4656 }
4657 }
4658
4659 out:
4660 if (opts->allow_utime == (unsigned short) -1)
4661 opts->allow_utime = ~opts->fs_dmask & (S_IWGRP | S_IWOTH);
4662
4663 if (opts->utf8 && strcmp(opts->iocharset, sdfat_iocharset_with_utf8)) {
4664 sdfat_msg(sb, KERN_WARNING,
4665 "utf8 enabled, \"iocharset=%s\" is recommended",
4666 sdfat_iocharset_with_utf8);
4667 }
4668
4669 if (opts->discard) {
4670 struct request_queue *q = bdev_get_queue(sb->s_bdev);
4671
4672 if (!blk_queue_discard(q))
4673 sdfat_msg(sb, KERN_WARNING,
4674 "mounting with \"discard\" option, but "
4675 "the device does not support discard");
4676 opts->discard = 0;
4677 }
4678
4679 return 0;
4680 }
4681
4682 static void sdfat_hash_init(struct super_block *sb)
4683 {
4684 struct sdfat_sb_info *sbi = SDFAT_SB(sb);
4685 int i;
4686
4687 spin_lock_init(&sbi->inode_hash_lock);
4688 for (i = 0; i < SDFAT_HASH_SIZE; i++)
4689 INIT_HLIST_HEAD(&sbi->inode_hashtable[i]);
4690 }
4691
4692 static int sdfat_read_root(struct inode *inode)
4693 {
4694 struct super_block *sb = inode->i_sb;
4695 struct sdfat_sb_info *sbi = SDFAT_SB(sb);
4696 struct timespec ts;
4697 FS_INFO_T *fsi = &(sbi->fsi);
4698 DIR_ENTRY_T info;
4699
4700 ts = CURRENT_TIME_SEC;
4701
4702 SDFAT_I(inode)->fid.dir.dir = fsi->root_dir;
4703 SDFAT_I(inode)->fid.dir.flags = 0x01;
4704 SDFAT_I(inode)->fid.entry = -1;
4705 SDFAT_I(inode)->fid.start_clu = fsi->root_dir;
4706 SDFAT_I(inode)->fid.flags = 0x01;
4707 SDFAT_I(inode)->fid.type = TYPE_DIR;
4708 SDFAT_I(inode)->fid.version = 0;
4709 SDFAT_I(inode)->fid.rwoffset = 0;
4710 SDFAT_I(inode)->fid.hint_bmap.off = -1;
4711 SDFAT_I(inode)->fid.hint_stat.eidx = 0;
4712 SDFAT_I(inode)->fid.hint_stat.clu = fsi->root_dir;
4713 SDFAT_I(inode)->fid.hint_femp.eidx = -1;
4714
4715 SDFAT_I(inode)->target = NULL;
4716
4717 if (fsapi_read_inode(inode, &info) < 0)
4718 return -EIO;
4719
4720 inode->i_uid = sbi->options.fs_uid;
4721 inode->i_gid = sbi->options.fs_gid;
4722 inode->i_version++;
4723 inode->i_generation = 0;
4724 inode->i_mode = sdfat_make_mode(sbi, ATTR_SUBDIR, S_IRWXUGO);
4725 inode->i_op = &sdfat_dir_inode_operations;
4726 inode->i_fop = &sdfat_dir_operations;
4727
4728 i_size_write(inode, info.Size);
4729 SDFAT_I(inode)->fid.size = info.Size;
4730 inode->i_blocks = ((i_size_read(inode) + (fsi->cluster_size - 1))
4731 & ~((loff_t)fsi->cluster_size - 1)) >> inode->i_blkbits;
4732 SDFAT_I(inode)->i_pos = ((loff_t) fsi->root_dir << 32) | 0xffffffff;
4733 SDFAT_I(inode)->i_size_aligned = i_size_read(inode);
4734 SDFAT_I(inode)->i_size_ondisk = i_size_read(inode);
4735
4736 sdfat_save_attr(inode, ATTR_SUBDIR);
4737 inode->i_mtime = inode->i_atime = inode->i_ctime = ts;
4738 set_nlink(inode, info.NumSubdirs + 2);
4739 return 0;
4740 }
4741
4742
4743
4744 static void setup_dops(struct super_block *sb)
4745 {
4746 if (SDFAT_SB(sb)->options.casesensitive == 0)
4747 sb->s_d_op = &sdfat_ci_dentry_ops;
4748 else
4749 sb->s_d_op = &sdfat_dentry_ops;
4750 }
4751
4752 static int sdfat_fill_super(struct super_block *sb, void *data, int silent)
4753 {
4754 struct inode *root_inode = NULL;
4755 struct sdfat_sb_info *sbi;
4756 int debug;
4757 int err;
4758 char buf[50];
4759 struct block_device *bdev = sb->s_bdev;
4760 dev_t bd_dev = bdev ? bdev->bd_dev : 0;
4761
4762 sdfat_log_msg(sb, KERN_INFO, "trying to mount...");
4763
4764 /*
4765 * GFP_KERNEL is ok here, because while we do hold the
4766 * supeblock lock, memory pressure can't call back into
4767 * the filesystem, since we're only just about to mount
4768 * it and have no inodes etc active!
4769 */
4770 sbi = kzalloc(sizeof(struct sdfat_sb_info), GFP_KERNEL);
4771 if (!sbi) {
4772 sdfat_log_msg(sb, KERN_INFO,
4773 "trying to alloc sbi with vzalloc()");
4774 sbi = vzalloc(sizeof(struct sdfat_sb_info));
4775 if (!sbi) {
4776 sdfat_log_msg(sb, KERN_ERR, "failed to mount! (ENOMEM)");
4777 return -ENOMEM;
4778 }
4779 sbi->use_vmalloc = 1;
4780 }
4781
4782 mutex_init(&sbi->s_vlock);
4783 sb->s_fs_info = sbi;
4784 sb->s_flags |= MS_NODIRATIME;
4785 sb->s_magic = SDFAT_SUPER_MAGIC;
4786 sb->s_op = &sdfat_sops;
4787 ratelimit_state_init(&sbi->ratelimit, DEFAULT_RATELIMIT_INTERVAL,
4788 DEFAULT_RATELIMIT_BURST);
4789 err = parse_options(sb, data, silent, &debug, &sbi->options);
4790 if (err) {
4791 sdfat_log_msg(sb, KERN_ERR, "failed to parse options");
4792 goto failed_mount;
4793 }
4794
4795 setup_sdfat_xattr_handler(sb);
4796 setup_sdfat_sync_super_wq(sb);
4797 setup_dops(sb);
4798
4799 err = fsapi_mount(sb);
4800 if (err) {
4801 sdfat_log_msg(sb, KERN_ERR, "failed to recognize fat type");
4802 goto failed_mount;
4803 }
4804
4805 /* set up enough so that it can read an inode */
4806 sdfat_hash_init(sb);
4807
4808 /*
4809 * The low byte of FAT's first entry must have same value with
4810 * media-field. But in real world, too many devices is
4811 * writing wrong value. So, removed that validity check.
4812 *
4813 * if (FAT_FIRST_ENT(sb, media) != first)
4814 */
4815
4816 err = -EINVAL;
4817 sprintf(buf, "cp%d", sbi->options.codepage);
4818 sbi->nls_disk = load_nls(buf);
4819 if (!sbi->nls_disk) {
4820 sdfat_log_msg(sb, KERN_ERR, "codepage %s not found", buf);
4821 goto failed_mount2;
4822 }
4823
4824 sbi->nls_io = load_nls(sbi->options.iocharset);
4825 if (!sbi->nls_io) {
4826 sdfat_log_msg(sb, KERN_ERR, "IO charset %s not found",
4827 sbi->options.iocharset);
4828 goto failed_mount2;
4829 }
4830
4831 err = __alloc_dfr_mem_if_required(sb);
4832 if (err) {
4833 sdfat_log_msg(sb, KERN_ERR, "failed to initialize a memory for "
4834 "defragmentation");
4835 goto failed_mount3;
4836 }
4837
4838 err = -ENOMEM;
4839 root_inode = new_inode(sb);
4840 if (!root_inode) {
4841 sdfat_log_msg(sb, KERN_ERR, "failed to allocate root inode.");
4842 goto failed_mount3;
4843 }
4844
4845 root_inode->i_ino = SDFAT_ROOT_INO;
4846 root_inode->i_version = 1;
4847
4848 err = sdfat_read_root(root_inode);
4849 if (err) {
4850 sdfat_log_msg(sb, KERN_ERR, "failed to initialize root inode.");
4851 goto failed_mount3;
4852 }
4853
4854 sdfat_attach(root_inode, SDFAT_I(root_inode)->i_pos);
4855 insert_inode_hash(root_inode);
4856
4857 err = -ENOMEM;
4858 sb->s_root = __d_make_root(root_inode);
4859 if (!sb->s_root) {
4860 sdfat_msg(sb, KERN_ERR, "failed to get the root dentry");
4861 goto failed_mount3;
4862 }
4863
4864 /*
4865 * Initialize filesystem attributes (for sysfs)
4866 * ex: /sys/fs/sdfat/mmcblk1[179:17]
4867 */
4868 sbi->sb_kobj.kset = sdfat_kset;
4869 err = kobject_init_and_add(&sbi->sb_kobj, &sdfat_ktype, NULL,
4870 "%s[%d:%d]", sb->s_id, MAJOR(bd_dev), MINOR(bd_dev));
4871 if (err) {
4872 sdfat_msg(sb, KERN_ERR, "Unable to create sdfat attributes for"
4873 " %s[%d:%d](%d)", sb->s_id,
4874 MAJOR(bd_dev), MINOR(bd_dev), err);
4875 goto failed_mount3;
4876 }
4877
4878 sdfat_log_msg(sb, KERN_INFO, "mounted successfully!");
4879 /* FOR BIGDATA */
4880 sdfat_statistics_set_mnt(&sbi->fsi);
4881 sdfat_statistics_set_vol_size(sb);
4882 return 0;
4883
4884 failed_mount3:
4885 __free_dfr_mem_if_required(sb);
4886 failed_mount2:
4887 fsapi_umount(sb);
4888 failed_mount:
4889 sdfat_log_msg(sb, KERN_INFO, "failed to mount! (%d)", err);
4890
4891 if (root_inode)
4892 iput(root_inode);
4893 sb->s_root = NULL;
4894
4895 if (sbi->nls_io)
4896 unload_nls(sbi->nls_io);
4897 if (sbi->nls_disk)
4898 unload_nls(sbi->nls_disk);
4899 if (sbi->options.iocharset != sdfat_default_iocharset)
4900 kfree(sbi->options.iocharset);
4901 sb->s_fs_info = NULL;
4902 if (!sbi->use_vmalloc)
4903 kfree(sbi);
4904 else
4905 vfree(sbi);
4906 return err;
4907 }
4908
4909 static struct dentry *sdfat_fs_mount(struct file_system_type *fs_type,
4910 int flags, const char *dev_name, void *data) {
4911 return mount_bdev(fs_type, flags, dev_name, data, sdfat_fill_super);
4912 }
4913
4914 static void init_once(void *foo)
4915 {
4916 struct sdfat_inode_info *ei = (struct sdfat_inode_info *)foo;
4917
4918 INIT_HLIST_NODE(&ei->i_hash_fat);
4919 inode_init_once(&ei->vfs_inode);
4920 }
4921
4922 static int __init sdfat_init_inodecache(void)
4923 {
4924 sdfat_inode_cachep = kmem_cache_create("sdfat_inode_cache",
4925 sizeof(struct sdfat_inode_info),
4926 0, (SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD),
4927 init_once);
4928 if (!sdfat_inode_cachep)
4929 return -ENOMEM;
4930 return 0;
4931 }
4932
4933 static void sdfat_destroy_inodecache(void)
4934 {
4935 kmem_cache_destroy(sdfat_inode_cachep);
4936 }
4937
4938 #ifdef CONFIG_SDFAT_DBG_IOCTL
4939 static void sdfat_debug_kill_sb(struct super_block *sb)
4940 {
4941 struct sdfat_sb_info *sbi = SDFAT_SB(sb);
4942 struct block_device *bdev = sb->s_bdev;
4943
4944 long flags;
4945
4946 if (sbi) {
4947 flags = sbi->debug_flags;
4948
4949 if (flags & SDFAT_DEBUGFLAGS_INVALID_UMOUNT) {
4950 /* invalidate_bdev drops all device cache include dirty.
4951 * we use this to simulate device removal
4952 */
4953 fsapi_cache_release(sb);
4954 invalidate_bdev(bdev);
4955 }
4956 }
4957
4958 kill_block_super(sb);
4959 }
4960 #endif /* CONFIG_SDFAT_DBG_IOCTL */
4961
4962 static struct file_system_type sdfat_fs_type = {
4963 .owner = THIS_MODULE,
4964 .name = "sdfat",
4965 .mount = sdfat_fs_mount,
4966 #ifdef CONFIG_SDFAT_DBG_IOCTL
4967 .kill_sb = sdfat_debug_kill_sb,
4968 #else
4969 .kill_sb = kill_block_super,
4970 #endif /* CONFIG_SDFAT_DBG_IOCTL */
4971 .fs_flags = FS_REQUIRES_DEV,
4972 };
4973
4974 static int __init init_sdfat_fs(void)
4975 {
4976 int err;
4977
4978 sdfat_log_version();
4979 err = fsapi_init();
4980 if (err)
4981 goto error;
4982
4983 sdfat_kset = kset_create_and_add("sdfat", NULL, fs_kobj);
4984 if (!sdfat_kset) {
4985 pr_err("[SDFAT] failed to create fs_kobj\n");
4986 err = -ENOMEM;
4987 goto error;
4988 }
4989
4990 err = sysfs_create_group(&sdfat_kset->kobj, &attr_group);
4991 if (err) {
4992 pr_err("[SDFAT] failed to create sdfat version attributes\n");
4993 goto error;
4994 }
4995
4996 err = sdfat_statistics_init(sdfat_kset);
4997 if (err)
4998 goto error;
4999
5000 err = sdfat_init_inodecache();
5001 if (err) {
5002 pr_err("[SDFAT] failed to initialize inode cache\n");
5003 goto error;
5004 }
5005
5006 err = register_filesystem(&sdfat_fs_type);
5007 if (err) {
5008 pr_err("[SDFAT] failed to register filesystem\n");
5009 goto error;
5010 }
5011
5012 return 0;
5013 error:
5014 sdfat_statistics_uninit();
5015
5016 if (sdfat_kset) {
5017 sysfs_remove_group(&sdfat_kset->kobj, &attr_group);
5018 kset_unregister(sdfat_kset);
5019 sdfat_kset = NULL;
5020 }
5021
5022 sdfat_destroy_inodecache();
5023 fsapi_shutdown();
5024
5025 pr_err("[SDFAT] failed to initialize FS driver(err:%d)\n", err);
5026 return err;
5027 }
5028
5029 static void __exit exit_sdfat_fs(void)
5030 {
5031 sdfat_statistics_uninit();
5032
5033 if (sdfat_kset) {
5034 sysfs_remove_group(&sdfat_kset->kobj, &attr_group);
5035 kset_unregister(sdfat_kset);
5036 sdfat_kset = NULL;
5037 }
5038
5039 sdfat_destroy_inodecache();
5040 unregister_filesystem(&sdfat_fs_type);
5041
5042 fsapi_shutdown();
5043 }
5044
5045 module_init(init_sdfat_fs);
5046 module_exit(exit_sdfat_fs);
5047
5048 MODULE_LICENSE("GPL");
5049 MODULE_DESCRIPTION("FAT/exFAT filesystem support");
5050 MODULE_AUTHOR("Samsung Electronics Co., Ltd.");
5051