V4L/DVB (13571): v4l: Adding Digital Video Timings APIs
[GitHub/LineageOS/android_kernel_samsung_universal7580.git] / fs / sdfat / sdfat.h
CommitLineData
3c2a0909
S
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, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17 * MA 02110-1301, USA.
18 */
19
20#ifndef _SDFAT_H
21#define _SDFAT_H
22
23#include <linux/buffer_head.h>
24#include <linux/string.h>
25#include <linux/types.h>
26#include <linux/nls.h>
27#include <linux/fs.h>
28#include <linux/mutex.h>
29#include <linux/ratelimit.h>
30#include <linux/version.h>
31#include <linux/kobject.h>
32#include "api.h"
33
34#ifdef CONFIG_SDFAT_DFR
35#include "dfr.h"
36#endif
37
38/*
39 * sdfat error flags
40 */
41#define SDFAT_ERRORS_CONT 1 /* ignore error and continue */
42#define SDFAT_ERRORS_PANIC 2 /* panic on error */
43#define SDFAT_ERRORS_RO 3 /* remount r/o on error */
44
45/*
46 * sdfat allocator flags
47 */
48#define SDFAT_ALLOC_DELAY 1 /* Delayed allocation */
49#define SDFAT_ALLOC_SMART 2 /* Smart allocation */
50
51/*
52 * sdfat allocator destination for smart allocation
53 */
54#define ALLOC_NOWHERE 0
55#define ALLOC_COLD 1
56#define ALLOC_HOT 16
57#define ALLOC_COLD_ALIGNED 1
58#define ALLOC_COLD_PACKING 2
59#define ALLOC_COLD_SEQ 4
60
61/*
62 * sdfat nls lossy flag
63 */
64#define NLS_NAME_NO_LOSSY (0x00) /* no lossy */
65#define NLS_NAME_LOSSY (0x01) /* just detected incorrect filename(s) */
66#define NLS_NAME_OVERLEN (0x02) /* the length is over than its limit */
67
68/*
69 * sdfat common MACRO
70 */
71#define CLUSTER_16(x) ((u16)((x) & 0xFFFFU))
72#define CLUSTER_32(x) ((u32)((x) & 0xFFFFFFFFU))
73#define CLUS_EOF CLUSTER_32(~0)
74#define CLUS_FREE (0)
75#define CLUS_BASE (2)
76#define IS_CLUS_EOF(x) (x == CLUS_EOF)
77#define IS_CLUS_FREE(x) (x == CLUS_FREE)
78#define IS_LAST_SECT_IN_CLUS(fsi, sec) \
79 ( (((sec) - (fsi)->data_start_sector + 1) \
80 & ((1 << (fsi)->sect_per_clus_bits) -1)) == 0 )
81
82#define CLUS_TO_SECT(fsi, x) \
83 ( (((x) - CLUS_BASE) << (fsi)->sect_per_clus_bits) + (fsi)->data_start_sector )
84
85#define SECT_TO_CLUS(fsi, sec) \
86 ((((sec) - (fsi)->data_start_sector) >> (fsi)->sect_per_clus_bits) + CLUS_BASE)
87
88/* variables defined at sdfat.c */
89extern const char* FS_TYPE_STR[];
90
91enum {
92 FS_TYPE_AUTO,
93 FS_TYPE_EXFAT,
94 FS_TYPE_VFAT,
95 FS_TYPE_MAX
96};
97
98/*
99 * sdfat mount in-memory data
100 */
101struct sdfat_mount_options {
102#if LINUX_VERSION_CODE < KERNEL_VERSION(3,5,0)
103 uid_t fs_uid;
104 gid_t fs_gid;
105#else
106 kuid_t fs_uid;
107 kgid_t fs_gid;
108#endif
109 unsigned short fs_fmask;
110 unsigned short fs_dmask;
111 unsigned short allow_utime; /* permission for setting the [am]time */
112 unsigned short codepage; /* codepage for shortname conversions */
113 char *iocharset; /* charset for filename input/display */
114 struct {
115 unsigned int pack_ratio;
116 unsigned int sect_per_au;
117 unsigned int misaligned_sect;
118 } amap_opt; /* AMAP-related options (see amap.c) */
119
120 unsigned char utf8;
121 unsigned char casesensitive;
122 unsigned char adj_hidsect;
123 unsigned char tz_utc;
124 unsigned char improved_allocation;
125 unsigned char defrag;
126 unsigned char symlink; /* support symlink operation */
127 unsigned char errors; /* on error: continue, panic, remount-ro */
128 unsigned char discard; /* flag on if -o dicard specified and device support discard() */
129 unsigned char fs_type; /* fs_type that user specified */
130 unsigned short adj_req; /* support aligned mpage write */
131};
132
133#define SDFAT_HASH_BITS 8
134#define SDFAT_HASH_SIZE (1UL << SDFAT_HASH_BITS)
135
136/*
137 * SDFAT file system superblock in-memory data
138 */
139struct sdfat_sb_info {
140 FS_INFO_T fsi; /* private filesystem info */
141
142 struct mutex s_vlock; /* volume lock */
143 int use_vmalloc;
144
145#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,00)
146 int s_dirt;
147 struct mutex s_lock; /* superblock lock */
148 int write_super_queued; /* Write_super work is pending? */
149 struct delayed_work write_super_work; /* Work_queue data structrue for write_super() */
150 spinlock_t work_lock; /* Lock for WQ */
151#endif
152 struct super_block *host_sb; /* sb pointer */
153 struct sdfat_mount_options options;
154 struct nls_table *nls_disk; /* Codepage used on disk */
155 struct nls_table *nls_io; /* Charset used for input and display */
156 struct ratelimit_state ratelimit;
157
158 spinlock_t inode_hash_lock;
159 struct hlist_head inode_hashtable[SDFAT_HASH_SIZE];
160 struct kobject sb_kobj;
161#ifdef CONFIG_SDFAT_DBG_IOCTL
162 long debug_flags;
163#endif /* CONFIG_SDFAT_DBG_IOCTL */
164
165#ifdef CONFIG_SDFAT_DFR
166 struct defrag_info dfr_info;
167 struct completion dfr_complete;
168 unsigned int *dfr_new_clus;
169 int dfr_new_idx;
170 unsigned int *dfr_page_wb;
171 void **dfr_pagep;
172 unsigned int dfr_hint_clus;
173 unsigned int dfr_hint_idx;
174 int dfr_reserved_clus;
175
176#ifdef CONFIG_SDFAT_DFR_DEBUG
177 int dfr_spo_flag;
178#endif /* CONFIG_SDFAT_DFR_DEBUG */
179
180#endif /* CONFIG_SDFAT_DFR */
181
182#ifdef CONFIG_SDFAT_TRACE_IO
183 /* Statistics for allocator */
184 unsigned int stat_n_pages_written; /* # of written pages in total */
185 unsigned int stat_n_pages_added; /* # of added blocks in total */
186 unsigned int stat_n_bdev_pages_written; /* # of written pages owned by bdev inode */
187 unsigned int stat_n_pages_confused;
188#endif
189 atomic_t stat_n_pages_queued; /* # of pages in the request queue (approx.) */
190};
191
192/*
193 * SDFAT file system inode in-memory data
194 */
195struct sdfat_inode_info {
196 FILE_ID_T fid;
197 char *target;
198 /* NOTE: i_size_ondisk is 64bits, so must hold ->i_mutex to access */
199 loff_t i_size_ondisk; /* physically allocated size */
200 loff_t i_size_aligned; /* block-aligned i_size (used in cont_write_begin) */
201 loff_t i_pos; /* on-disk position of directory entry or 0 */
202 struct hlist_node i_hash_fat; /* hash by i_location */
203#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,00)
204 struct rw_semaphore truncate_lock; /* protect bmap against truncate */
205#endif
206#ifdef CONFIG_SDFAT_DFR
207 struct defrag_info dfr_info;
208#endif
209 struct inode vfs_inode;
210};
211
212/*
213 * FIXME : needs on-disk-slot in-memory data
214 */
215
216/* static inline functons */
217static inline const char *sdfat_get_vol_type_str(unsigned int type)
218{
219 if (type == EXFAT)
220 return "exfat";
221 else if (type == FAT32)
222 return "vfat:32";
223 else if (type == FAT16)
224 return "vfat:16";
225 else if (type == FAT12)
226 return "vfat:12";
227
228 return "unknown";
229}
230
231static inline struct sdfat_sb_info *SDFAT_SB(struct super_block *sb)
232{
233 return (struct sdfat_sb_info*)sb->s_fs_info;
234}
235
236static inline struct sdfat_inode_info *SDFAT_I(struct inode *inode)
237{
238 return container_of(inode, struct sdfat_inode_info, vfs_inode);
239}
240
241/*
242 * If ->i_mode can't hold S_IWUGO (i.e. ATTR_RO), we use ->i_attrs to
243 * save ATTR_RO instead of ->i_mode.
244 *
245 * If it's directory and !sbi->options.rodir, ATTR_RO isn't read-only
246 * bit, it's just used as flag for app.
247 */
248static inline int sdfat_mode_can_hold_ro(struct inode *inode)
249{
250 struct sdfat_sb_info *sbi = SDFAT_SB(inode->i_sb);
251
252 if (S_ISDIR(inode->i_mode))
253 return 0;
254
255 if ((~sbi->options.fs_fmask) & S_IWUGO)
256 return 1;
257 return 0;
258}
259
260/*
261 * FIXME : needs to check symlink option.
262 */
263/* Convert attribute bits and a mask to the UNIX mode. */
264static inline mode_t sdfat_make_mode(struct sdfat_sb_info *sbi,
265 u32 attr, mode_t mode)
266{
267 if ((attr & ATTR_READONLY) && !(attr & ATTR_SUBDIR))
268 mode &= ~S_IWUGO;
269
270 if (attr & ATTR_SUBDIR)
271 return (mode & ~sbi->options.fs_dmask) | S_IFDIR;
272 else if (attr & ATTR_SYMLINK)
273 return (mode & ~sbi->options.fs_dmask) | S_IFLNK;
274 else
275 return (mode & ~sbi->options.fs_fmask) | S_IFREG;
276}
277
278/* Return the FAT attribute byte for this inode */
279static inline u32 sdfat_make_attr(struct inode *inode)
280{
281 u32 attrs = SDFAT_I(inode)->fid.attr;
282 if (S_ISDIR(inode->i_mode))
283 attrs |= ATTR_SUBDIR;
284 if (sdfat_mode_can_hold_ro(inode) && !(inode->i_mode & S_IWUGO))
285 attrs |= ATTR_READONLY;
286 return attrs;
287}
288
289static inline void sdfat_save_attr(struct inode *inode, u32 attr)
290{
291 if (sdfat_mode_can_hold_ro(inode))
292 SDFAT_I(inode)->fid.attr = attr & ATTR_RWMASK;
293 else
294 SDFAT_I(inode)->fid.attr = attr & (ATTR_RWMASK | ATTR_READONLY);
295}
296
297/* sdfat/nls.c */
298/* NLS management function */
299s32 nls_cmp_sfn(struct super_block *sb, u8 *a, u8 *b);
300s32 nls_cmp_uniname(struct super_block *sb, u16 *a, u16 *b);
301s32 nls_uni16s_to_sfn(struct super_block *sb, UNI_NAME_T *p_uniname, DOS_NAME_T *p_dosname, s32 *p_lossy);
302s32 nls_sfn_to_uni16s(struct super_block *sb, DOS_NAME_T *p_dosname, UNI_NAME_T *p_uniname);
303s32 nls_uni16s_to_vfsname(struct super_block *sb, UNI_NAME_T *uniname, u8 *p_cstring, s32 len);
304s32 nls_vfsname_to_uni16s(struct super_block *sb, const u8 *p_cstring, const s32 len, UNI_NAME_T *uniname, s32 *p_lossy);
305
306/* sdfat/mpage.c */
307#ifdef CONFIG_SDFAT_ALIGNED_MPAGE_WRITE
308int sdfat_mpage_writepages(struct address_space *mapping,
309 struct writeback_control *wbc, get_block_t *get_block);
310#endif
311
312/* sdfat/xattr.c */
313#ifdef CONFIG_SDFAT_VIRTUAL_XATTR
314extern int sdfat_setxattr(struct dentry*dentry, const char *name, const void *value, size_t size, int flags);
315extern ssize_t sdfat_getxattr(struct dentry *dentry, const char *name, void *value, size_t size);
316extern ssize_t sdfat_listxattr(struct dentry *dentry, char *list, size_t size);
317extern int sdfat_removexattr(struct dentry *dentry, const char *name);
318#endif
319
320/* sdfat/misc.c */
321extern void
322__sdfat_fs_error(struct super_block *sb, int report, const char *fmt, ...)
323 __attribute__ ((format (printf, 3, 4))) __cold;
324#define sdfat_fs_error(sb, fmt, args...) \
325 __sdfat_fs_error(sb, 1, fmt , ## args)
326#define sdfat_fs_error_ratelimit(sb, fmt, args...) \
327 __sdfat_fs_error(sb, __ratelimit(&SDFAT_SB(sb)->ratelimit), fmt, ## args)
328extern void
329__sdfat_msg(struct super_block *sb, const char *lv, int st, const char *fmt, ...)
330 __attribute__ ((format (printf, 4, 5))) __cold;
331#define sdfat_msg(sb, lv, fmt, args...) \
332 __sdfat_msg(sb, lv, 0, fmt , ## args)
333#define sdfat_log_msg(sb, lv, fmt, args...) \
334 __sdfat_msg(sb, lv, 1, fmt , ## args)
335extern void sdfat_log_version(void);
336extern void sdfat_time_fat2unix(struct sdfat_sb_info *sbi, struct timespec *ts,
337 DATE_TIME_T *tp);
338extern void sdfat_time_unix2fat(struct sdfat_sb_info *sbi, struct timespec *ts,
339 DATE_TIME_T *tp);
340extern TIMESTAMP_T *tm_now(struct sdfat_sb_info *sbi, TIMESTAMP_T *tm);
341
342#ifdef CONFIG_SDFAT_DEBUG
343
344#ifdef CONFIG_SDFAT_DBG_CAREFUL
345void sdfat_debug_check_clusters(struct inode *inode);
346#else
347#define sdfat_debug_check_clusters(inode)
348#endif /* CONFIG_SDFAT_DBG_CAREFUL */
349
350#ifdef CONFIG_SDFAT_DBG_BUGON
351#define sdfat_debug_bug_on(expr) BUG_ON(expr)
352#else
353#define sdfat_debug_bug_on(expr)
354#endif
355
356#ifdef CONFIG_SDFAT_DBG_WARNON
357#define sdfat_debug_warn_on(expr) WARN_ON(expr)
358#else
359#define sdfat_debug_warn_on(expr)
360#endif
361
362#else /* CONFIG_SDFAT_DEBUG */
363
364#define sdfat_debug_check_clusters(inode)
365#define sdfat_debug_bug_on(expr)
366
367#endif /* CONFIG_SDFAT_DEBUG */
368
369#ifdef CONFIG_SDFAT_TRACE_ELAPSED_TIME
370u32 sdfat_time_current_usec(struct timeval* tv);
371extern struct timeval __t1;
372extern struct timeval __t2;
373
374#define TIME_GET(tv) sdfat_time_current_usec(tv)
375#define TIME_START(s) do {sdfat_time_current_usec(s); } while (0)
376#define TIME_END(e) do {sdfat_time_current_usec(e); } while (0)
377#define TIME_ELAPSED(s, e) ((u32)(((e)->tv_sec - (s)->tv_sec) * 1000000 + \
378 ((e)->tv_usec - (s)->tv_usec)))
379#define PRINT_TIME(n) \
380 do { \
381 printk("[SDFAT] Elapsed time %d = %d (usec)\n", \
382 n, (__t2 - __t1)); \
383 } while(0)
384#else /* CONFIG_SDFAT_TRACE_ELAPSED_TIME */
385#define TIME_GET(tv) (0)
386#define TIME_START(s)
387#define TIME_END(e)
388#define TIME_ELAPSED(s, e) (0)
389#define PRINT_TIME(n)
390#endif /* CONFIG_SDFAT_TRACE_ELAPSED_TIME */
391
392#define SDFAT_MSG_LV_NONE (0x00000000)
393#define SDFAT_MSG_LV_ERR (0x00000001)
394#define SDFAT_MSG_LV_INFO (0x00000002)
395#define SDFAT_MSG_LV_DBG (0x00000003)
396#define SDFAT_MSG_LV_MORE (0x00000004)
397#define SDFAT_MSG_LV_TRACE (0x00000005)
398#define SDFAT_MSG_LV_ALL (0x00000006)
399
400#define SDFAT_MSG_LEVEL SDFAT_MSG_LV_INFO
401
402#define SDFAT_TAG_NAME "SDFAT"
403#define __S(x) #x
404#define _S(x) __S(x)
405
406extern void __sdfat_dmsg(int level, const char *fmt, ...)
407 __attribute__ ((format (printf, 2, 3))) __cold;
408
409#define SDFAT_EMSG_T(level, ...) __sdfat_dmsg(level, KERN_ERR "[" SDFAT_TAG_NAME "] [" _S(__FILE__) "(" _S(__LINE__) ")] " __VA_ARGS__)
410#define SDFAT_DMSG_T(level, ...) __sdfat_dmsg(level, KERN_INFO "[" SDFAT_TAG_NAME "] " __VA_ARGS__)
411
412#define SDFAT_EMSG(...) SDFAT_EMSG_T(SDFAT_MSG_LV_ERR, __VA_ARGS__)
413#define SDFAT_IMSG(...) SDFAT_DMSG_T(SDFAT_MSG_LV_INFO, __VA_ARGS__)
414#define SDFAT_DMSG(...) SDFAT_DMSG_T(SDFAT_MSG_LV_DBG, __VA_ARGS__)
415#define SDFAT_MMSG(...) SDFAT_DMSG_T(SDFAT_MSG_LV_MORE, __VA_ARGS__)
416#define SDFAT_TMSG(...) SDFAT_DMSG_T(SDFAT_MSG_LV_TRACE, __VA_ARGS__)
417
418#define EMSG(...)
419#define IMSG(...)
420#define DMSG(...)
421#define MMSG(...)
422#define TMSG(...)
423
424#define EMSG_VAR(exp)
425#define IMSG_VAR(exp)
426#define DMSG_VAR(exp)
427#define MMSG_VAR(exp)
428#define TMSG_VAR(exp)
429
430#ifdef CONFIG_SDFAT_DBG_MSG
431
432
433#if (SDFAT_MSG_LEVEL >= SDFAT_MSG_LV_ERR)
434#undef EMSG
435#undef EMSG_VAR
436#define EMSG(...) SDFAT_EMSG(__VA_ARGS__)
437#define EMSG_VAR(exp) exp
438#endif
439
440#if (SDFAT_MSG_LEVEL >= SDFAT_MSG_LV_INFO)
441#undef IMSG
442#undef IMSG_VAR
443#define IMSG(...) SDFAT_IMSG(__VA_ARGS__)
444#define IMSG_VAR(exp) exp
445#endif
446
447#if (SDFAT_MSG_LEVEL >= SDFAT_MSG_LV_DBG)
448#undef DMSG
449#undef DMSG_VAR
450#define DMSG(...) SDFAT_DMSG(__VA_ARGS__)
451#define DMSG_VAR(exp) exp
452#endif
453
454#if (SDFAT_MSG_LEVEL >= SDFAT_MSG_LV_MORE)
455#undef MMSG
456#undef MMSG_VAR
457#define MMSG(...) SDFAT_MMSG(__VA_ARGS__)
458#define MMSG_VAR(exp) exp
459#endif
460
461/* should replace with trace function */
462#if (SDFAT_MSG_LEVEL >= SDFAT_MSG_LV_TRACE)
463#undef TMSG
464#undef TMSG_VAR
465#define TMSG(...) SDFAT_TMSG(__VA_ARGS__)
466#define TMSG_VAR(exp) exp
467#endif
468
469#endif /* CONFIG_SDFAT_DBG_MSG */
470
471
472#define ASSERT(expr) \
473 if (!(expr)) { \
474 printk(KERN_ERR "Assertion failed! %s\n", #expr); \
475 BUG_ON(1); \
476 }
477
478#endif /* !_SDFAT_H */
479