exfat_super.c 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710
  1. /* Some of the source code in this file came from "linux/fs/fat/file.c","linux/fs/fat/inode.c" and "linux/fs/fat/misc.c". */
  2. /*
  3. * linux/fs/fat/file.c
  4. *
  5. * Written 1992,1993 by Werner Almesberger
  6. *
  7. * regular file handling primitives for fat-based filesystems
  8. */
  9. /*
  10. * linux/fs/fat/inode.c
  11. *
  12. * Written 1992,1993 by Werner Almesberger
  13. * VFAT extensions by Gordon Chaffee, merged with msdos fs by Henrik Storner
  14. * Rewritten for the constant inumbers support by Al Viro
  15. *
  16. * Fixes:
  17. *
  18. * Max Cohan: Fixed invalid FSINFO offset when info_sector is 0
  19. */
  20. /*
  21. * linux/fs/fat/misc.c
  22. *
  23. * Written 1992,1993 by Werner Almesberger
  24. * 22/11/2000 - Fixed fat_date_unix2dos for dates earlier than 01/01/1980
  25. * and date_dos2unix for date==0 by Igor Zhbanov(bsg@uniyar.ac.ru)
  26. */
  27. /*
  28. * linux/fs/vfat/namei.c
  29. *
  30. * Written 1992,1993 by Werner Almesberger
  31. *
  32. * Windows95/Windows NT compatible extended MSDOS filesystem
  33. * by Gordon Chaffee Copyright (C) 1995. Send bug reports for the
  34. * VFAT filesystem to <chaffee@cs.berkeley.edu>. Specify
  35. * what file operation caused you trouble and if you can duplicate
  36. * the problem, send a script that demonstrates it.
  37. *
  38. * Short name translation 1999, 2001 by Wolfram Pienkoss <wp@bszh.de>
  39. *
  40. * Support Multibyte characters and cleanup by
  41. * OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>
  42. */
  43. /*
  44. * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd.
  45. *
  46. * This program is free software; you can redistribute it and/or
  47. * modify it under the terms of the GNU General Public License
  48. * as published by the Free Software Foundation; either version 2
  49. * of the License, or (at your option) any later version.
  50. *
  51. * This program is distributed in the hope that it will be useful,
  52. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  53. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  54. * GNU General Public License for more details.
  55. *
  56. * You should have received a copy of the GNU General Public License
  57. * along with this program; if not, write to the Free Software
  58. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  59. */
  60. #include <linux/version.h>
  61. #include <linux/module.h>
  62. #include <linux/init.h>
  63. #include <linux/time.h>
  64. #include <linux/slab.h>
  65. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,37)
  66. #include <linux/smp_lock.h>
  67. #endif
  68. #include <linux/seq_file.h>
  69. #include <linux/pagemap.h>
  70. #include <linux/mpage.h>
  71. #include <linux/buffer_head.h>
  72. #include <linux/exportfs.h>
  73. #include <linux/mount.h>
  74. #include <linux/vfs.h>
  75. #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,10,0)
  76. #include <linux/aio.h>
  77. #endif
  78. #include <linux/parser.h>
  79. #include <linux/uio.h>
  80. #include <linux/writeback.h>
  81. #include <linux/log2.h>
  82. #include <linux/hash.h>
  83. #include <linux/backing-dev.h>
  84. #include <linux/sched.h>
  85. #include <linux/fs_struct.h>
  86. #include <linux/namei.h>
  87. #include <linux/vmalloc.h>
  88. #include <asm/current.h>
  89. #include <asm/unaligned.h>
  90. #include "exfat_version.h"
  91. #include "exfat_config.h"
  92. #include "exfat_global.h"
  93. #include "exfat_data.h"
  94. #include "exfat_oal.h"
  95. #include "exfat_blkdev.h"
  96. #include "exfat_cache.h"
  97. #include "exfat_part.h"
  98. #include "exfat_nls.h"
  99. #include "exfat_api.h"
  100. #include "exfat.h"
  101. #include "exfat_super.h"
  102. static struct kmem_cache *exfat_inode_cachep;
  103. static int exfat_default_codepage = DEFAULT_CODEPAGE;
  104. static char exfat_default_iocharset[] = DEFAULT_IOCHARSET;
  105. extern struct timezone sys_tz;
  106. #define EXFAT_DSTATE_LOCKED (void *)(0xCAFE2016)
  107. #define EXFAT_DSTATE_UNLOCKED (void *)(0x0)
  108. static inline void __lock_d_revalidate(struct dentry *dentry)
  109. {
  110. spin_lock(&dentry->d_lock);
  111. dentry->d_fsdata = EXFAT_DSTATE_LOCKED;
  112. spin_unlock(&dentry->d_lock);
  113. }
  114. static inline void __unlock_d_revalidate(struct dentry *dentry)
  115. {
  116. spin_lock(&dentry->d_lock);
  117. dentry->d_fsdata = EXFAT_DSTATE_UNLOCKED;
  118. spin_unlock(&dentry->d_lock);
  119. }
  120. /* __check_dstate_locked requires dentry->d_lock */
  121. static inline int __check_dstate_locked(struct dentry *dentry)
  122. {
  123. if (dentry->d_fsdata == EXFAT_DSTATE_LOCKED)
  124. return 1;
  125. return 0;
  126. }
  127. #define CHECK_ERR(x) BUG_ON(x)
  128. #define ELAPSED_TIME 0
  129. #if (ELAPSED_TIME == 1)
  130. #include <linux/time.h>
  131. static UINT32 __t1, __t2;
  132. static UINT32 get_current_msec(void)
  133. {
  134. struct timeval tm;
  135. do_gettimeofday(&tm);
  136. return((UINT32)(tm.tv_sec*1000000 + tm.tv_usec));
  137. }
  138. #define TIME_START() do {__t1 = get_current_msec();} while (0)
  139. #define TIME_END() do {__t2 = get_current_msec();} while (0)
  140. #define PRINT_TIME(n) do {printk("[EXFAT] Elapsed time %d = %d (usec)\n", n, (__t2 - __t1));} while (0)
  141. #else
  142. #define TIME_START()
  143. #define TIME_END()
  144. #define PRINT_TIME(n)
  145. #endif
  146. #define UNIX_SECS_1980 315532800L
  147. #if BITS_PER_LONG == 64
  148. #define UNIX_SECS_2108 4354819200L
  149. #endif
  150. #define DAYS_DELTA_DECADE (365 * 10 + 2)
  151. #define NO_LEAP_YEAR_2100 (120)
  152. #define IS_LEAP_YEAR(y) (!((y) & 0x3) && (y) != NO_LEAP_YEAR_2100)
  153. #define SECS_PER_MIN (60)
  154. #define SECS_PER_HOUR (60 * SECS_PER_MIN)
  155. #define SECS_PER_DAY (24 * SECS_PER_HOUR)
  156. #define MAKE_LEAP_YEAR(leap_year, year) \
  157. do { \
  158. if (unlikely(year > NO_LEAP_YEAR_2100)) \
  159. leap_year = ((year + 3) / 4) - 1; \
  160. else \
  161. leap_year = ((year + 3) / 4); \
  162. } while(0)
  163. static time_t accum_days_in_year[] = {
  164. 0, 0, 31, 59, 90,120,151,181,212,243,273,304,334, 0, 0, 0,
  165. };
  166. static void _exfat_truncate(struct inode *inode, loff_t old_size);
  167. void exfat_time_fat2unix(struct exfat_sb_info *sbi, struct timespec *ts,
  168. DATE_TIME_T *tp)
  169. {
  170. time_t year = tp->Year;
  171. time_t ld;
  172. MAKE_LEAP_YEAR(ld, year);
  173. if (IS_LEAP_YEAR(year) && (tp->Month) > 2)
  174. ld++;
  175. ts->tv_sec = tp->Second + tp->Minute * SECS_PER_MIN
  176. + tp->Hour * SECS_PER_HOUR
  177. + (year * 365 + ld + accum_days_in_year[(tp->Month)]
  178. + (tp->Day - 1) + DAYS_DELTA_DECADE) * SECS_PER_DAY;
  179. if(!sbi->options.tz_utc)
  180. ts->tv_sec += sys_tz.tz_minuteswest * SECS_PER_MIN;
  181. ts->tv_nsec = 0;
  182. }
  183. void exfat_time_unix2fat(struct exfat_sb_info *sbi, struct timespec *ts,
  184. DATE_TIME_T *tp)
  185. {
  186. time_t second = ts->tv_sec;
  187. time_t day, month, year;
  188. time_t ld;
  189. if (!sbi->options.tz_utc)
  190. second -= sys_tz.tz_minuteswest * SECS_PER_MIN;
  191. if (second < UNIX_SECS_1980) {
  192. tp->Second = 0;
  193. tp->Minute = 0;
  194. tp->Hour = 0;
  195. tp->Day = 1;
  196. tp->Month = 1;
  197. tp->Year = 0;
  198. return;
  199. }
  200. #if (BITS_PER_LONG == 64)
  201. if (second >= UNIX_SECS_2108) {
  202. tp->Second = 59;
  203. tp->Minute = 59;
  204. tp->Hour = 23;
  205. tp->Day = 31;
  206. tp->Month = 12;
  207. tp->Year = 127;
  208. return;
  209. }
  210. #endif
  211. day = second / SECS_PER_DAY - DAYS_DELTA_DECADE;
  212. year = day / 365;
  213. MAKE_LEAP_YEAR(ld, year);
  214. if (year * 365 + ld > day)
  215. year--;
  216. MAKE_LEAP_YEAR(ld, year);
  217. day -= year * 365 + ld;
  218. if (IS_LEAP_YEAR(year) && day == accum_days_in_year[3]) {
  219. month = 2;
  220. } else {
  221. if (IS_LEAP_YEAR(year) && day > accum_days_in_year[3])
  222. day--;
  223. for (month = 1; month < 12; month++) {
  224. if (accum_days_in_year[month + 1] > day)
  225. break;
  226. }
  227. }
  228. day -= accum_days_in_year[month];
  229. tp->Second = second % SECS_PER_MIN;
  230. tp->Minute = (second / SECS_PER_MIN) % 60;
  231. tp->Hour = (second / SECS_PER_HOUR) % 24;
  232. tp->Day = day + 1;
  233. tp->Month = month;
  234. tp->Year = year;
  235. }
  236. static struct inode *exfat_iget(struct super_block *sb, loff_t i_pos);
  237. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36)
  238. static int exfat_generic_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg);
  239. #else
  240. static long exfat_generic_ioctl(struct file *filp, unsigned int cmd, unsigned long arg);
  241. #endif
  242. static int exfat_sync_inode(struct inode *inode);
  243. static struct inode *exfat_build_inode(struct super_block *sb, FILE_ID_T *fid, loff_t i_pos);
  244. static void exfat_detach(struct inode *inode);
  245. static void exfat_attach(struct inode *inode, loff_t i_pos);
  246. static inline unsigned long exfat_hash(loff_t i_pos);
  247. static int exfat_write_inode(struct inode *inode, struct writeback_control *wbc);
  248. static void exfat_write_super(struct super_block *sb);
  249. #if LINUX_VERSION_CODE < KERNEL_VERSION(3,5,0)
  250. #define GLOBAL_ROOT_UID (0)
  251. #define GLOBAL_ROOT_GID (0)
  252. static inline bool uid_eq(uid_t left, uid_t right)
  253. {
  254. return left == right;
  255. }
  256. static inline bool gid_eq(gid_t left, gid_t right)
  257. {
  258. return left == right;
  259. }
  260. static inline uid_t from_kuid_munged(struct user_namespace *to, uid_t kuid)
  261. {
  262. return kuid;
  263. }
  264. static inline gid_t from_kgid_munged(struct user_namespace *to, gid_t kgid)
  265. {
  266. return kgid;
  267. }
  268. static inline uid_t make_kuid(struct user_namespace *from, uid_t uid)
  269. {
  270. return uid;
  271. }
  272. static inline gid_t make_kgid(struct user_namespace *from, gid_t gid)
  273. {
  274. return gid;
  275. }
  276. #endif
  277. static void __lock_super(struct super_block *sb)
  278. {
  279. #if LINUX_VERSION_CODE < KERNEL_VERSION(3,7,0)
  280. lock_super(sb);
  281. #else
  282. struct exfat_sb_info *sbi = EXFAT_SB(sb);
  283. mutex_lock(&sbi->s_lock);
  284. #endif
  285. }
  286. static void __unlock_super(struct super_block *sb)
  287. {
  288. #if LINUX_VERSION_CODE < KERNEL_VERSION(3,7,0)
  289. unlock_super(sb);
  290. #else
  291. struct exfat_sb_info *sbi = EXFAT_SB(sb);
  292. mutex_unlock(&sbi->s_lock);
  293. #endif
  294. }
  295. static int __is_sb_dirty(struct super_block *sb)
  296. {
  297. #if LINUX_VERSION_CODE < KERNEL_VERSION(3,7,0)
  298. return sb->s_dirt;
  299. #else
  300. struct exfat_sb_info *sbi = EXFAT_SB(sb);
  301. return sbi->s_dirt;
  302. #endif
  303. }
  304. static void __set_sb_clean(struct super_block *sb)
  305. {
  306. #if LINUX_VERSION_CODE < KERNEL_VERSION(3,7,0)
  307. sb->s_dirt = 0;
  308. #else
  309. struct exfat_sb_info *sbi = EXFAT_SB(sb);
  310. sbi->s_dirt = 0;
  311. #endif
  312. }
  313. static void exfat_msg(struct super_block *sb, const char *level, const char *fmt, ...)
  314. {
  315. struct va_format vaf;
  316. va_list args;
  317. struct block_device *bdev = sb->s_bdev;
  318. dev_t bd_dev = bdev ? bdev->bd_dev : 0;
  319. va_start(args, fmt);
  320. vaf.fmt = fmt;
  321. vaf.va = &args;
  322. printk("%s[EXFAT] (%s[%d:%d]): %pV\n", level,
  323. sb->s_id, MAJOR(bd_dev), MINOR(bd_dev), &vaf);
  324. va_end(args);
  325. }
  326. static void exfat_mnt_msg(struct super_block *sb, int mount, int prev_err, const char *msg)
  327. {
  328. exfat_msg(sb, KERN_INFO, "%s %s",
  329. msg, prev_err ? "(with previous I/O errors)" : "");
  330. ST_LOG("[EXFAT] (%s[%d:%d]):%s %s",sb->s_id, MAJOR(sb->s_dev),MINOR(sb->s_dev),
  331. msg, prev_err ? "(with previous I/O errors)" : "");
  332. }
  333. static int __exfat_revalidate(struct dentry *dentry)
  334. {
  335. int ret = 1;
  336. spin_lock(&dentry->d_lock);
  337. if ((dentry->d_time != dentry->d_parent->d_inode->i_version) &&
  338. !__check_dstate_locked(dentry)) {
  339. ret = 0;
  340. }
  341. spin_unlock(&dentry->d_lock);
  342. return ret;
  343. }
  344. #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,00)
  345. static int exfat_revalidate(struct dentry *dentry, unsigned int flags)
  346. #else
  347. static int exfat_revalidate(struct dentry *dentry, struct nameidata *nd)
  348. #endif
  349. {
  350. #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,00)
  351. if (flags & LOOKUP_RCU)
  352. return -ECHILD;
  353. #elif LINUX_VERSION_CODE >= KERNEL_VERSION(3,0,00)
  354. if (nd && nd->flags & LOOKUP_RCU)
  355. return -ECHILD;
  356. #endif
  357. if (dentry->d_inode)
  358. return 1;
  359. return __exfat_revalidate(dentry);
  360. }
  361. #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,00)
  362. static int exfat_revalidate_ci(struct dentry *dentry, unsigned int flags)
  363. #else
  364. static int exfat_revalidate_ci(struct dentry *dentry, struct nameidata *nd)
  365. #endif
  366. {
  367. #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,00)
  368. if (flags & LOOKUP_RCU)
  369. return -ECHILD;
  370. #elif LINUX_VERSION_CODE >= KERNEL_VERSION(3,0,00)
  371. unsigned int flags;
  372. if (nd && nd->flags & LOOKUP_RCU)
  373. return -ECHILD;
  374. flags = nd ? nd->flags : 0;
  375. #else
  376. flags = nd ? nd->flags : 0;
  377. #endif
  378. if (dentry->d_inode)
  379. return 1;
  380. #if 0
  381. if (!flags)
  382. return 0;
  383. #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,0,00)
  384. if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET))
  385. return 0;
  386. #else
  387. if (!(nd->flags & (LOOKUP_CONTINUE | LOOKUP_PARENT))) {
  388. if (nd->flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET))
  389. return 0;
  390. }
  391. #endif
  392. #endif
  393. return __exfat_revalidate(dentry);
  394. }
  395. static unsigned int __exfat_striptail_len(unsigned int len, const char *name)
  396. {
  397. while (len && name[len - 1] == '.')
  398. len--;
  399. return len;
  400. }
  401. static unsigned int exfat_striptail_len(const struct qstr *qstr)
  402. {
  403. return __exfat_striptail_len(qstr->len, qstr->name);
  404. }
  405. #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,14,00)
  406. static int exfat_d_hash(const struct dentry *dentry, struct qstr *qstr)
  407. #else
  408. static int exfat_d_hash(const struct dentry *dentry, const struct inode *inode,
  409. struct qstr *qstr)
  410. #endif
  411. {
  412. qstr->hash = full_name_hash(qstr->name, exfat_striptail_len(qstr));
  413. return 0;
  414. }
  415. #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,14,00)
  416. static int exfat_d_hashi(const struct dentry *dentry, struct qstr *qstr)
  417. #else
  418. static int exfat_d_hashi(const struct dentry *dentry, const struct inode *inode,
  419. struct qstr *qstr)
  420. #endif
  421. {
  422. struct nls_table *t = EXFAT_SB(dentry->d_sb)->nls_io;
  423. const unsigned char *name;
  424. unsigned int len;
  425. unsigned long hash;
  426. name = qstr->name;
  427. len = exfat_striptail_len(qstr);
  428. hash = init_name_hash();
  429. while (len--)
  430. hash = partial_name_hash(nls_tolower(t, *name++), hash);
  431. qstr->hash = end_name_hash(hash);
  432. return 0;
  433. }
  434. #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,14,00)
  435. static int exfat_cmpi(const struct dentry *parent, const struct dentry *dentry,
  436. unsigned int len, const char *str, const struct qstr *name)
  437. #else
  438. static int exfat_cmpi(const struct dentry *parent, const struct inode *pinode,
  439. const struct dentry *dentry, const struct inode *inode,
  440. unsigned int len, const char *str, const struct qstr *name)
  441. #endif
  442. {
  443. struct nls_table *t = EXFAT_SB(parent->d_sb)->nls_io;
  444. unsigned int alen, blen;
  445. alen = exfat_striptail_len(name);
  446. blen = __exfat_striptail_len(len, str);
  447. if (alen == blen) {
  448. if (nls_strnicmp(t, name->name, str, alen) == 0)
  449. return 0;
  450. }
  451. return 1;
  452. }
  453. #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,14,00)
  454. static int exfat_cmp(const struct dentry *parent, const struct dentry *dentry,
  455. unsigned int len, const char *str, const struct qstr *name)
  456. #else
  457. static int exfat_cmp(const struct dentry *parent, const struct inode *pinode,
  458. const struct dentry *dentry, const struct inode *inode,
  459. unsigned int len, const char *str, const struct qstr *name)
  460. #endif
  461. {
  462. unsigned int alen, blen;
  463. alen = exfat_striptail_len(name);
  464. blen = __exfat_striptail_len(len, str);
  465. if (alen == blen) {
  466. if (strncmp(name->name, str, alen) == 0)
  467. return 0;
  468. }
  469. return 1;
  470. }
  471. static const struct dentry_operations exfat_ci_dentry_ops = {
  472. .d_revalidate = exfat_revalidate_ci,
  473. .d_hash = exfat_d_hashi,
  474. .d_compare = exfat_cmpi,
  475. };
  476. static const struct dentry_operations exfat_dentry_ops = {
  477. .d_revalidate = exfat_revalidate,
  478. .d_hash = exfat_d_hash,
  479. .d_compare = exfat_cmp,
  480. };
  481. #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,14,00)
  482. static int exfat_readdir(struct file *filp, struct dir_context *ctx)
  483. {
  484. struct inode *inode = filp->f_path.dentry->d_inode;
  485. struct super_block *sb = inode->i_sb;
  486. struct exfat_sb_info *sbi = EXFAT_SB(sb);
  487. FS_INFO_T *p_fs = &(sbi->fs_info);
  488. BD_INFO_T *p_bd = &(EXFAT_SB(sb)->bd_info);
  489. DIR_ENTRY_T de;
  490. unsigned long inum;
  491. loff_t cpos;
  492. int err = 0;
  493. __lock_super(sb);
  494. cpos = ctx->pos;
  495. if ((p_fs->vol_type == EXFAT) || (inode->i_ino == EXFAT_ROOT_INO)) {
  496. if (!dir_emit_dots(filp, ctx))
  497. goto out;
  498. if (ctx->pos == 2)
  499. cpos = 0;
  500. }
  501. if (cpos & (DENTRY_SIZE - 1)) {
  502. err = -ENOENT;
  503. goto out;
  504. }
  505. get_new:
  506. EXFAT_I(inode)->fid.size = i_size_read(inode);
  507. EXFAT_I(inode)->fid.rwoffset = cpos >> DENTRY_SIZE_BITS;
  508. err = FsReadDir(inode, &de);
  509. if (err) {
  510. if (err == FFS_MEDIAERR) {
  511. cpos += 1 << p_bd->sector_size_bits;
  512. cpos &= ~((1 << p_bd->sector_size_bits)-1);
  513. }
  514. err = -EIO;
  515. goto end_of_dir;
  516. }
  517. cpos = EXFAT_I(inode)->fid.rwoffset << DENTRY_SIZE_BITS;
  518. if (!de.Name[0])
  519. goto end_of_dir;
  520. if (!memcmp(de.ShortName, DOS_CUR_DIR_NAME, DOS_NAME_LENGTH)) {
  521. inum = inode->i_ino;
  522. } else if (!memcmp(de.ShortName, DOS_PAR_DIR_NAME, DOS_NAME_LENGTH)) {
  523. inum = parent_ino(filp->f_path.dentry);
  524. } else {
  525. loff_t i_pos = ((loff_t) EXFAT_I(inode)->fid.start_clu << 32) |
  526. ((EXFAT_I(inode)->fid.rwoffset-1) & 0xffffffff);
  527. struct inode *tmp = exfat_iget(sb, i_pos);
  528. if (tmp) {
  529. inum = tmp->i_ino;
  530. iput(tmp);
  531. } else {
  532. inum = iunique(sb, EXFAT_ROOT_INO);
  533. }
  534. }
  535. __unlock_super(sb);
  536. if (!dir_emit(ctx, de.Name, strlen(de.Name), inum,
  537. (de.Attr & ATTR_SUBDIR) ? DT_DIR : DT_REG))
  538. goto out_nolock;
  539. __lock_super(sb);
  540. ctx->pos = cpos;
  541. goto get_new;
  542. end_of_dir:
  543. ctx->pos = cpos;
  544. out:
  545. __unlock_super(sb);
  546. out_nolock:
  547. return err;
  548. }
  549. #else
  550. static int exfat_readdir(struct file *filp, void *dirent, filldir_t filldir)
  551. {
  552. struct inode *inode = filp->f_path.dentry->d_inode;
  553. struct super_block *sb = inode->i_sb;
  554. struct exfat_sb_info *sbi = EXFAT_SB(sb);
  555. FS_INFO_T *p_fs = &(sbi->fs_info);
  556. BD_INFO_T *p_bd = &(EXFAT_SB(sb)->bd_info);
  557. DIR_ENTRY_T de;
  558. unsigned long inum;
  559. loff_t cpos;
  560. int err = 0;
  561. __lock_super(sb);
  562. cpos = filp->f_pos;
  563. if ((p_fs->vol_type == EXFAT) || (inode->i_ino == EXFAT_ROOT_INO)) {
  564. while (cpos < 2) {
  565. if (inode->i_ino == EXFAT_ROOT_INO)
  566. inum = EXFAT_ROOT_INO;
  567. else if (cpos == 0)
  568. inum = inode->i_ino;
  569. else
  570. inum = parent_ino(filp->f_path.dentry);
  571. if (filldir(dirent, "..", cpos+1, cpos, inum, DT_DIR) < 0)
  572. goto out;
  573. cpos++;
  574. filp->f_pos++;
  575. }
  576. if (cpos == 2) {
  577. cpos = 0;
  578. }
  579. }
  580. if (cpos & (DENTRY_SIZE - 1)) {
  581. err = -ENOENT;
  582. goto out;
  583. }
  584. get_new:
  585. EXFAT_I(inode)->fid.size = i_size_read(inode);
  586. EXFAT_I(inode)->fid.rwoffset = cpos >> DENTRY_SIZE_BITS;
  587. err = FsReadDir(inode, &de);
  588. if (err) {
  589. if (err == FFS_MEDIAERR) {
  590. cpos += 1 << p_bd->sector_size_bits;
  591. cpos &= ~((1 << p_bd->sector_size_bits)-1);
  592. }
  593. err = -EIO;
  594. goto end_of_dir;
  595. }
  596. cpos = EXFAT_I(inode)->fid.rwoffset << DENTRY_SIZE_BITS;
  597. if (!de.Name[0])
  598. goto end_of_dir;
  599. if (!memcmp(de.ShortName, DOS_CUR_DIR_NAME, DOS_NAME_LENGTH)) {
  600. inum = inode->i_ino;
  601. } else if (!memcmp(de.ShortName, DOS_PAR_DIR_NAME, DOS_NAME_LENGTH)) {
  602. inum = parent_ino(filp->f_path.dentry);
  603. } else {
  604. loff_t i_pos = ((loff_t) EXFAT_I(inode)->fid.start_clu << 32) |
  605. ((EXFAT_I(inode)->fid.rwoffset-1) & 0xffffffff);
  606. struct inode *tmp = exfat_iget(sb, i_pos);
  607. if (tmp) {
  608. inum = tmp->i_ino;
  609. iput(tmp);
  610. } else {
  611. inum = iunique(sb, EXFAT_ROOT_INO);
  612. }
  613. }
  614. __unlock_super(sb);
  615. if (filldir(dirent, de.Name, strlen(de.Name), cpos, inum,
  616. (de.Attr & ATTR_SUBDIR) ? DT_DIR : DT_REG) < 0)
  617. goto out_nolock;
  618. __lock_super(sb);
  619. filp->f_pos = cpos;
  620. goto get_new;
  621. end_of_dir:
  622. filp->f_pos = cpos;
  623. out:
  624. __unlock_super(sb);
  625. out_nolock:
  626. return err;
  627. }
  628. #endif
  629. static int exfat_ioctl_volume_id(struct inode *dir)
  630. {
  631. struct super_block *sb = dir->i_sb;
  632. struct exfat_sb_info *sbi = EXFAT_SB(sb);
  633. FS_INFO_T *p_fs = &(sbi->fs_info);
  634. return p_fs->vol_id;
  635. }
  636. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36)
  637. static int exfat_generic_ioctl(struct inode *inode, struct file *filp,
  638. unsigned int cmd, unsigned long arg)
  639. #else
  640. static long exfat_generic_ioctl(struct file *filp,
  641. unsigned int cmd, unsigned long arg)
  642. #endif
  643. {
  644. #if EXFAT_CONFIG_KERNEL_DEBUG
  645. #if !(LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36))
  646. struct inode *inode = filp->f_dentry->d_inode;
  647. #endif
  648. unsigned int flags;
  649. #endif
  650. switch (cmd) {
  651. case EXFAT_IOCTL_GET_VOLUME_ID:
  652. return exfat_ioctl_volume_id(inode);
  653. #if EXFAT_CONFIG_KERNEL_DEBUG
  654. case EXFAT_IOC_GET_DEBUGFLAGS: {
  655. struct super_block *sb = inode->i_sb;
  656. struct exfat_sb_info *sbi = EXFAT_SB(sb);
  657. flags = sbi->debug_flags;
  658. return put_user(flags, (int __user *)arg);
  659. }
  660. case EXFAT_IOC_SET_DEBUGFLAGS: {
  661. struct super_block *sb = inode->i_sb;
  662. struct exfat_sb_info *sbi = EXFAT_SB(sb);
  663. if (!capable(CAP_SYS_ADMIN))
  664. return -EPERM;
  665. if (get_user(flags, (int __user *) arg))
  666. return -EFAULT;
  667. __lock_super(sb);
  668. sbi->debug_flags = flags;
  669. __unlock_super(sb);
  670. return 0;
  671. }
  672. #endif
  673. default:
  674. return -ENOTTY;
  675. }
  676. }
  677. #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,1,0)
  678. static int exfat_file_fsync(struct file *filp, loff_t start, loff_t end, int datasync)
  679. #else
  680. static int exfat_file_fsync(struct file *filp, int datasync)
  681. #endif
  682. {
  683. struct inode *inode = filp->f_mapping->host;
  684. struct super_block *sb = inode->i_sb;
  685. int res, err;
  686. #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,1,0)
  687. res = generic_file_fsync(filp, start, end, datasync);
  688. #else
  689. res = generic_file_fsync(filp, datasync);
  690. #endif
  691. err = FsSyncVol(sb, 1);
  692. return res ? res : err;
  693. }
  694. const struct file_operations exfat_dir_operations = {
  695. .llseek = generic_file_llseek,
  696. .read = generic_read_dir,
  697. #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,14,00)
  698. .iterate = exfat_readdir,
  699. #else
  700. .readdir = exfat_readdir,
  701. #endif
  702. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36)
  703. .ioctl = exfat_generic_ioctl,
  704. #else
  705. .unlocked_ioctl = exfat_generic_ioctl,
  706. #endif
  707. .fsync = exfat_file_fsync,
  708. };
  709. #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,00)
  710. static int exfat_create(struct inode *dir, struct dentry *dentry, umode_t mode,
  711. bool excl)
  712. #elif LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,00)
  713. static int exfat_create(struct inode *dir, struct dentry *dentry, umode_t mode,
  714. struct nameidata *nd)
  715. #else
  716. static int exfat_create(struct inode *dir, struct dentry *dentry, int mode,
  717. struct nameidata *nd)
  718. #endif
  719. {
  720. struct super_block *sb = dir->i_sb;
  721. struct inode *inode;
  722. struct timespec ts;
  723. FILE_ID_T fid;
  724. loff_t i_pos;
  725. int err;
  726. __lock_super(sb);
  727. PRINTK("exfat_create entered\n");
  728. ts = CURRENT_TIME_SEC;
  729. err = FsCreateFile(dir, (UINT8 *) dentry->d_name.name, FM_REGULAR, &fid);
  730. if (err) {
  731. if (err == FFS_INVALIDPATH)
  732. err = -EINVAL;
  733. else if (err == FFS_FILEEXIST)
  734. err = -EEXIST;
  735. else if (err == FFS_FULL)
  736. err = -ENOSPC;
  737. else if (err == FFS_NAMETOOLONG)
  738. err = -ENAMETOOLONG;
  739. else
  740. err = -EIO;
  741. goto out;
  742. }
  743. __lock_d_revalidate(dentry);
  744. dir->i_version++;
  745. dir->i_ctime = dir->i_mtime = dir->i_atime = ts;
  746. if (IS_DIRSYNC(dir))
  747. (void) exfat_sync_inode(dir);
  748. else
  749. mark_inode_dirty(dir);
  750. i_pos = ((loff_t) fid.dir.dir << 32) | (fid.entry & 0xffffffff);
  751. inode = exfat_build_inode(sb, &fid, i_pos);
  752. if (IS_ERR(inode)) {
  753. err = PTR_ERR(inode);
  754. goto out;
  755. }
  756. inode->i_version++;
  757. inode->i_mtime = inode->i_atime = inode->i_ctime = ts;
  758. d_instantiate(dentry, inode);
  759. out:
  760. __unlock_d_revalidate(dentry);
  761. __unlock_super(sb);
  762. PRINTK("exfat_create exited\n");
  763. return err;
  764. }
  765. static int exfat_find(struct inode *dir, struct qstr *qname,
  766. FILE_ID_T *fid)
  767. {
  768. int err;
  769. if (qname->len == 0)
  770. return -ENOENT;
  771. err = FsLookupFile(dir, (UINT8 *) qname->name, fid);
  772. if (err)
  773. return -ENOENT;
  774. return 0;
  775. }
  776. static int exfat_d_anon_disconn(struct dentry *dentry)
  777. {
  778. return IS_ROOT(dentry) && (dentry->d_flags & DCACHE_DISCONNECTED);
  779. }
  780. #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,00)
  781. static struct dentry *exfat_lookup(struct inode *dir, struct dentry *dentry,
  782. unsigned int flags)
  783. #else
  784. static struct dentry *exfat_lookup(struct inode *dir, struct dentry *dentry,
  785. struct nameidata *nd)
  786. #endif
  787. {
  788. struct super_block *sb = dir->i_sb;
  789. struct inode *inode;
  790. struct dentry *alias;
  791. int err;
  792. FILE_ID_T fid;
  793. loff_t i_pos;
  794. UINT64 ret;
  795. mode_t i_mode;
  796. __lock_super(sb);
  797. PRINTK("exfat_lookup entered\n");
  798. err = exfat_find(dir, &dentry->d_name, &fid);
  799. if (err) {
  800. if (err == -ENOENT) {
  801. inode = NULL;
  802. goto out;
  803. }
  804. goto error;
  805. }
  806. i_pos = ((loff_t) fid.dir.dir << 32) | (fid.entry & 0xffffffff);
  807. inode = exfat_build_inode(sb, &fid, i_pos);
  808. if (IS_ERR(inode)) {
  809. err = PTR_ERR(inode);
  810. goto error;
  811. }
  812. i_mode = inode->i_mode;
  813. if (S_ISLNK(i_mode)) {
  814. EXFAT_I(inode)->target = MALLOC(i_size_read(inode)+1);
  815. if (!EXFAT_I(inode)->target) {
  816. err = -ENOMEM;
  817. goto error;
  818. }
  819. FsReadFile(dir, &fid, EXFAT_I(inode)->target, i_size_read(inode), &ret);
  820. *(EXFAT_I(inode)->target + i_size_read(inode)) = '\0';
  821. }
  822. alias = d_find_alias(inode);
  823. /*
  824. * Checking "alias->d_parent == dentry->d_parent" to make sure
  825. * FS is not corrupted (especially double linked dir).
  826. */
  827. if (alias && alias->d_parent == dentry->d_parent &&
  828. !exfat_d_anon_disconn(alias)) {
  829. /*
  830. * This inode has non anonymous-DCACHE_DISCONNECTED
  831. * dentry. This means, the user did ->lookup() by an
  832. * another name (longname vs 8.3 alias of it) in past.
  833. *
  834. * Switch to new one for reason of locality if possible.
  835. */
  836. CHECK_ERR(d_unhashed(alias));
  837. if (!S_ISDIR(i_mode))
  838. d_move(alias, dentry);
  839. iput(inode);
  840. __unlock_super(sb);
  841. PRINTK("exfat_lookup exited 1\n");
  842. return alias;
  843. } else {
  844. dput(alias);
  845. }
  846. out:
  847. dentry->d_time = dir->i_version;
  848. __unlock_super(sb);
  849. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,38)
  850. dentry->d_op = sb->s_root->d_op;
  851. #endif
  852. dentry = d_splice_alias(inode, dentry);
  853. PRINTK("exfat_lookup exited 2\n");
  854. return dentry;
  855. error:
  856. __unlock_super(sb);
  857. PRINTK("exfat_lookup exited 3\n");
  858. return ERR_PTR(err);
  859. }
  860. static int exfat_unlink(struct inode *dir, struct dentry *dentry)
  861. {
  862. struct inode *inode = dentry->d_inode;
  863. struct super_block *sb = dir->i_sb;
  864. struct timespec ts;
  865. int err;
  866. __lock_super(sb);
  867. PRINTK("exfat_unlink entered\n");
  868. ts = CURRENT_TIME_SEC;
  869. EXFAT_I(inode)->fid.size = i_size_read(inode);
  870. if (EXFAT_I(inode)->fid.dir.dir == DIR_DELETED) {
  871. err = -ENOENT;
  872. goto out;
  873. }
  874. err = FsRemoveEntry(dir, &(EXFAT_I(inode)->fid));
  875. if (err) {
  876. if (err == FFS_PERMISSIONERR)
  877. err = -EPERM;
  878. else
  879. err = -EIO;
  880. goto out;
  881. }
  882. __lock_d_revalidate(dentry);
  883. dir->i_version++;
  884. dir->i_mtime = dir->i_atime = ts;
  885. if (IS_DIRSYNC(dir))
  886. (void) exfat_sync_inode(dir);
  887. else
  888. mark_inode_dirty(dir);
  889. clear_nlink(inode);
  890. inode->i_mtime = inode->i_atime = ts;
  891. exfat_detach(inode);
  892. dentry->d_time = dir->i_version;
  893. out:
  894. __unlock_d_revalidate(dentry);
  895. __unlock_super(sb);
  896. PRINTK("exfat_unlink exited\n");
  897. return err;
  898. }
  899. static int exfat_symlink(struct inode *dir, struct dentry *dentry, const char *target)
  900. {
  901. struct super_block *sb = dir->i_sb;
  902. struct inode *inode;
  903. struct timespec ts;
  904. FILE_ID_T fid;
  905. loff_t i_pos;
  906. int err;
  907. UINT64 len = (UINT64) strlen(target);
  908. UINT64 ret;
  909. __lock_super(sb);
  910. PRINTK("exfat_symlink entered\n");
  911. ts = CURRENT_TIME_SEC;
  912. err = FsCreateFile(dir, (UINT8 *) dentry->d_name.name, FM_SYMLINK, &fid);
  913. if (err) {
  914. if (err == FFS_INVALIDPATH)
  915. err = -EINVAL;
  916. else if (err == FFS_FILEEXIST)
  917. err = -EEXIST;
  918. else if (err == FFS_FULL)
  919. err = -ENOSPC;
  920. else
  921. err = -EIO;
  922. goto out;
  923. }
  924. err = FsWriteFile(dir, &fid, (char *) target, len, &ret);
  925. if (err) {
  926. FsRemoveFile(dir, &fid);
  927. if (err == FFS_FULL)
  928. err = -ENOSPC;
  929. else
  930. err = -EIO;
  931. goto out;
  932. }
  933. __lock_d_revalidate(dentry);
  934. dir->i_version++;
  935. dir->i_ctime = dir->i_mtime = dir->i_atime = ts;
  936. if (IS_DIRSYNC(dir))
  937. (void) exfat_sync_inode(dir);
  938. else
  939. mark_inode_dirty(dir);
  940. i_pos = ((loff_t) fid.dir.dir << 32) | (fid.entry & 0xffffffff);
  941. inode = exfat_build_inode(sb, &fid, i_pos);
  942. if (IS_ERR(inode)) {
  943. err = PTR_ERR(inode);
  944. goto out;
  945. }
  946. inode->i_version++;
  947. inode->i_mtime = inode->i_atime = inode->i_ctime = ts;
  948. EXFAT_I(inode)->target = MALLOC(len+1);
  949. if (!EXFAT_I(inode)->target) {
  950. err = -ENOMEM;
  951. goto out;
  952. }
  953. MEMCPY(EXFAT_I(inode)->target, target, len+1);
  954. dentry->d_time = dentry->d_parent->d_inode->i_version;
  955. d_instantiate(dentry, inode);
  956. out:
  957. __unlock_d_revalidate(dentry);
  958. __unlock_super(sb);
  959. PRINTK("exfat_symlink exited\n");
  960. return err;
  961. }
  962. #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,00)
  963. static int exfat_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  964. #else
  965. static int exfat_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  966. #endif
  967. {
  968. struct super_block *sb = dir->i_sb;
  969. struct inode *inode;
  970. struct timespec ts;
  971. FILE_ID_T fid;
  972. loff_t i_pos;
  973. int err;
  974. __lock_super(sb);
  975. PRINTK("exfat_mkdir entered\n");
  976. ts = CURRENT_TIME_SEC;
  977. err = FsCreateDir(dir, (UINT8 *) dentry->d_name.name, &fid);
  978. if (err) {
  979. if (err == FFS_INVALIDPATH)
  980. err = -EINVAL;
  981. else if (err == FFS_FILEEXIST)
  982. err = -EEXIST;
  983. else if (err == FFS_FULL)
  984. err = -ENOSPC;
  985. else if (err == FFS_NAMETOOLONG)
  986. err = -ENAMETOOLONG;
  987. else
  988. err = -EIO;
  989. goto out;
  990. }
  991. __lock_d_revalidate(dentry);
  992. dir->i_version++;
  993. dir->i_ctime = dir->i_mtime = dir->i_atime = ts;
  994. if (IS_DIRSYNC(dir))
  995. (void) exfat_sync_inode(dir);
  996. else
  997. mark_inode_dirty(dir);
  998. inc_nlink(dir);
  999. i_pos = ((loff_t) fid.dir.dir << 32) | (fid.entry & 0xffffffff);
  1000. inode = exfat_build_inode(sb, &fid, i_pos);
  1001. if (IS_ERR(inode)) {
  1002. err = PTR_ERR(inode);
  1003. goto out;
  1004. }
  1005. inode->i_version++;
  1006. inode->i_mtime = inode->i_atime = inode->i_ctime = ts;
  1007. d_instantiate(dentry, inode);
  1008. out:
  1009. __unlock_d_revalidate(dentry);
  1010. __unlock_super(sb);
  1011. PRINTK("exfat_mkdir exited\n");
  1012. return err;
  1013. }
  1014. static int exfat_rmdir(struct inode *dir, struct dentry *dentry)
  1015. {
  1016. struct inode *inode = dentry->d_inode;
  1017. struct super_block *sb = dir->i_sb;
  1018. struct timespec ts;
  1019. int err;
  1020. __lock_super(sb);
  1021. PRINTK("exfat_rmdir entered\n");
  1022. ts = CURRENT_TIME_SEC;
  1023. EXFAT_I(inode)->fid.size = i_size_read(inode);
  1024. if (EXFAT_I(inode)->fid.dir.dir == DIR_DELETED) {
  1025. err = -ENOENT;
  1026. goto out;
  1027. }
  1028. err = FsRemoveDir(dir, &(EXFAT_I(inode)->fid));
  1029. if (err) {
  1030. if (err == FFS_INVALIDPATH)
  1031. err = -EINVAL;
  1032. else if (err == FFS_FILEEXIST)
  1033. err = -ENOTEMPTY;
  1034. else if (err == FFS_NOTFOUND)
  1035. err = -ENOENT;
  1036. else if (err == FFS_DIRBUSY)
  1037. err = -EBUSY;
  1038. else
  1039. err = -EIO;
  1040. goto out;
  1041. }
  1042. __lock_d_revalidate(dentry);
  1043. dir->i_version++;
  1044. dir->i_mtime = dir->i_atime = ts;
  1045. if (IS_DIRSYNC(dir))
  1046. (void) exfat_sync_inode(dir);
  1047. else
  1048. mark_inode_dirty(dir);
  1049. drop_nlink(dir);
  1050. clear_nlink(inode);
  1051. inode->i_mtime = inode->i_atime = ts;
  1052. exfat_detach(inode);
  1053. dentry->d_time = dir->i_version;
  1054. remove_inode_hash(inode);
  1055. out:
  1056. __unlock_d_revalidate(dentry);
  1057. __unlock_super(sb);
  1058. PRINTK("exfat_rmdir exited\n");
  1059. return err;
  1060. }
  1061. static int exfat_rename(struct inode *old_dir, struct dentry *old_dentry,
  1062. struct inode *new_dir, struct dentry *new_dentry)
  1063. {
  1064. struct inode *old_inode, *new_inode;
  1065. struct super_block *sb = old_dir->i_sb;
  1066. struct timespec ts;
  1067. loff_t i_pos;
  1068. int err;
  1069. __lock_super(sb);
  1070. PRINTK("exfat_rename entered\n");
  1071. old_inode = old_dentry->d_inode;
  1072. new_inode = new_dentry->d_inode;
  1073. ts = CURRENT_TIME_SEC;
  1074. EXFAT_I(old_inode)->fid.size = i_size_read(old_inode);
  1075. if (EXFAT_I(old_inode)->fid.dir.dir == DIR_DELETED) {
  1076. err = -ENOENT;
  1077. goto out;
  1078. }
  1079. err = FsMoveFile(old_dir, &(EXFAT_I(old_inode)->fid), new_dir, new_dentry);
  1080. if (err) {
  1081. if (err == FFS_PERMISSIONERR)
  1082. err = -EPERM;
  1083. else if (err == FFS_INVALIDPATH)
  1084. err = -EINVAL;
  1085. else if (err == FFS_FILEEXIST)
  1086. err = -EEXIST;
  1087. else if (err == FFS_NOTFOUND)
  1088. err = -ENOENT;
  1089. else if (err == FFS_FULL)
  1090. err = -ENOSPC;
  1091. else
  1092. err = -EIO;
  1093. goto out;
  1094. }
  1095. __lock_d_revalidate(old_dentry);
  1096. __lock_d_revalidate(new_dentry);
  1097. new_dir->i_version++;
  1098. new_dir->i_ctime = new_dir->i_mtime = new_dir->i_atime = ts;
  1099. if (IS_DIRSYNC(new_dir))
  1100. (void) exfat_sync_inode(new_dir);
  1101. else
  1102. mark_inode_dirty(new_dir);
  1103. i_pos = ((loff_t) EXFAT_I(old_inode)->fid.dir.dir << 32) |
  1104. (EXFAT_I(old_inode)->fid.entry & 0xffffffff);
  1105. exfat_detach(old_inode);
  1106. exfat_attach(old_inode, i_pos);
  1107. if (IS_DIRSYNC(new_dir))
  1108. (void) exfat_sync_inode(old_inode);
  1109. else
  1110. mark_inode_dirty(old_inode);
  1111. if ((S_ISDIR(old_inode->i_mode)) && (old_dir != new_dir)) {
  1112. drop_nlink(old_dir);
  1113. if (!new_inode) inc_nlink(new_dir);
  1114. }
  1115. old_dir->i_version++;
  1116. old_dir->i_ctime = old_dir->i_mtime = ts;
  1117. if (IS_DIRSYNC(old_dir))
  1118. (void) exfat_sync_inode(old_dir);
  1119. else
  1120. mark_inode_dirty(old_dir);
  1121. if (new_inode) {
  1122. exfat_detach(new_inode);
  1123. if (EXFAT_I(new_inode)->fid.dir.dir != DIR_DELETED) {
  1124. drop_nlink(new_inode);
  1125. if (S_ISDIR(new_inode->i_mode))
  1126. drop_nlink(new_inode);
  1127. }
  1128. new_inode->i_ctime = ts;
  1129. }
  1130. out:
  1131. __unlock_d_revalidate(old_dentry);
  1132. __unlock_d_revalidate(new_dentry);
  1133. __unlock_super(sb);
  1134. PRINTK("exfat_rename exited\n");
  1135. return err;
  1136. }
  1137. static int exfat_cont_expand(struct inode *inode, loff_t size)
  1138. {
  1139. struct address_space *mapping = inode->i_mapping;
  1140. loff_t start = i_size_read(inode), count = size - i_size_read(inode);
  1141. int err, err2;
  1142. if ((err = generic_cont_expand_simple(inode, size)) != 0)
  1143. return err;
  1144. inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC;
  1145. mark_inode_dirty(inode);
  1146. if (IS_SYNC(inode)) {
  1147. err = filemap_fdatawrite_range(mapping, start, start + count - 1);
  1148. err2 = sync_mapping_buffers(mapping);
  1149. err = (err)?(err):(err2);
  1150. err2 = write_inode_now(inode, 1);
  1151. err = (err)?(err):(err2);
  1152. if (!err) {
  1153. err = filemap_fdatawait_range(mapping, start, start + count - 1);
  1154. }
  1155. }
  1156. return err;
  1157. }
  1158. static int exfat_allow_set_time(struct exfat_sb_info *sbi, struct inode *inode)
  1159. {
  1160. mode_t allow_utime = sbi->options.allow_utime;
  1161. if (!uid_eq(current_fsuid(), inode->i_uid)) {
  1162. if (in_group_p(inode->i_gid))
  1163. allow_utime >>= 3;
  1164. if (allow_utime & MAY_WRITE)
  1165. return 1;
  1166. }
  1167. return 0;
  1168. }
  1169. static int exfat_sanitize_mode(const struct exfat_sb_info *sbi,
  1170. struct inode *inode, umode_t *mode_ptr)
  1171. {
  1172. mode_t i_mode, mask, perm;
  1173. i_mode = inode->i_mode;
  1174. if (S_ISREG(i_mode) || S_ISLNK(i_mode))
  1175. mask = sbi->options.fs_fmask;
  1176. else
  1177. mask = sbi->options.fs_dmask;
  1178. perm = *mode_ptr & ~(S_IFMT | mask);
  1179. if ((perm & (S_IRUGO | S_IXUGO)) != (i_mode & (S_IRUGO|S_IXUGO)))
  1180. return -EPERM;
  1181. if (exfat_mode_can_hold_ro(inode)) {
  1182. if ((perm & S_IWUGO) && ((perm & S_IWUGO) != (S_IWUGO & ~mask)))
  1183. return -EPERM;
  1184. } else {
  1185. if ((perm & S_IWUGO) != (S_IWUGO & ~mask))
  1186. return -EPERM;
  1187. }
  1188. *mode_ptr &= S_IFMT | perm;
  1189. return 0;
  1190. }
  1191. static int exfat_setattr(struct dentry *dentry, struct iattr *attr)
  1192. {
  1193. struct exfat_sb_info *sbi = EXFAT_SB(dentry->d_sb);
  1194. struct inode *inode = dentry->d_inode;
  1195. unsigned int ia_valid;
  1196. int error;
  1197. loff_t old_size;
  1198. PRINTK("exfat_setattr entered\n");
  1199. if ((attr->ia_valid & ATTR_SIZE)
  1200. && (attr->ia_size > i_size_read(inode))) {
  1201. error = exfat_cont_expand(inode, attr->ia_size);
  1202. if (error || attr->ia_valid == ATTR_SIZE)
  1203. return error;
  1204. attr->ia_valid &= ~ATTR_SIZE;
  1205. }
  1206. ia_valid = attr->ia_valid;
  1207. if ((ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET))
  1208. && exfat_allow_set_time(sbi, inode)) {
  1209. attr->ia_valid &= ~(ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET);
  1210. }
  1211. error = inode_change_ok(inode, attr);
  1212. attr->ia_valid = ia_valid;
  1213. if (error) {
  1214. return error;
  1215. }
  1216. if (((attr->ia_valid & ATTR_UID) &&
  1217. (!uid_eq(attr->ia_uid, sbi->options.fs_uid))) ||
  1218. ((attr->ia_valid & ATTR_GID) &&
  1219. (!gid_eq(attr->ia_gid, sbi->options.fs_gid))) ||
  1220. ((attr->ia_valid & ATTR_MODE) &&
  1221. (attr->ia_mode & ~(S_IFREG | S_IFLNK | S_IFDIR | S_IRWXUGO)))) {
  1222. return -EPERM;
  1223. }
  1224. if (attr->ia_valid & ATTR_MODE) {
  1225. if (exfat_sanitize_mode(sbi, inode, &attr->ia_mode) < 0)
  1226. attr->ia_valid &= ~ATTR_MODE;
  1227. }
  1228. EXFAT_I(inode)->fid.size = i_size_read(inode);
  1229. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36)
  1230. if (attr->ia_valid)
  1231. error = inode_setattr(inode, attr);
  1232. #else
  1233. if (attr->ia_valid & ATTR_SIZE) {
  1234. old_size = i_size_read(inode);
  1235. #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,00)
  1236. down_write(&EXFAT_I(inode)->truncate_lock);
  1237. truncate_setsize(inode, attr->ia_size);
  1238. _exfat_truncate(inode, old_size);
  1239. up_write(&EXFAT_I(inode)->truncate_lock);
  1240. #else
  1241. truncate_setsize(inode, attr->ia_size);
  1242. _exfat_truncate(inode, old_size);
  1243. #endif
  1244. }
  1245. setattr_copy(inode, attr);
  1246. mark_inode_dirty(inode);
  1247. #endif
  1248. PRINTK("exfat_setattr exited\n");
  1249. return error;
  1250. }
  1251. static int exfat_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
  1252. {
  1253. struct inode *inode = dentry->d_inode;
  1254. PRINTK("exfat_getattr entered\n");
  1255. generic_fillattr(inode, stat);
  1256. stat->blksize = EXFAT_SB(inode->i_sb)->fs_info.cluster_size;
  1257. PRINTK("exfat_getattr exited\n");
  1258. return 0;
  1259. }
  1260. const struct inode_operations exfat_dir_inode_operations = {
  1261. .create = exfat_create,
  1262. .lookup = exfat_lookup,
  1263. .unlink = exfat_unlink,
  1264. .symlink = exfat_symlink,
  1265. .mkdir = exfat_mkdir,
  1266. .rmdir = exfat_rmdir,
  1267. .rename = exfat_rename,
  1268. .setattr = exfat_setattr,
  1269. .getattr = exfat_getattr,
  1270. #ifdef CONFIG_EXFAT_VIRTUAL_XATTR
  1271. .setxattr = exfat_setxattr,
  1272. .getxattr = exfat_getxattr,
  1273. .listxattr = exfat_listxattr,
  1274. .removexattr = exfat_removexattr,
  1275. #endif
  1276. };
  1277. static void *exfat_follow_link(struct dentry *dentry, struct nameidata *nd)
  1278. {
  1279. struct exfat_inode_info *ei = EXFAT_I(dentry->d_inode);
  1280. nd_set_link(nd, (char *)(ei->target));
  1281. return NULL;
  1282. }
  1283. const struct inode_operations exfat_symlink_inode_operations = {
  1284. .readlink = generic_readlink,
  1285. .follow_link = exfat_follow_link,
  1286. #ifdef CONFIG_EXFAT_VIRTUAL_XATTR
  1287. .setxattr = exfat_setxattr,
  1288. .getxattr = exfat_getxattr,
  1289. .listxattr = exfat_listxattr,
  1290. .removexattr = exfat_removexattr,
  1291. #endif
  1292. };
  1293. static int exfat_file_release(struct inode *inode, struct file *filp)
  1294. {
  1295. struct super_block *sb = inode->i_sb;
  1296. EXFAT_I(inode)->fid.size = i_size_read(inode);
  1297. FsSyncVol(sb, 0);
  1298. return 0;
  1299. }
  1300. const struct file_operations exfat_file_operations = {
  1301. .llseek = generic_file_llseek,
  1302. #if LINUX_VERSION_CODE < KERNEL_VERSION(3,16,0)
  1303. .read = do_sync_read,
  1304. .write = do_sync_write,
  1305. .aio_read = generic_file_aio_read,
  1306. .aio_write = generic_file_aio_write,
  1307. #else
  1308. .read = new_sync_read,
  1309. .write = new_sync_write,
  1310. .read_iter = generic_file_read_iter,
  1311. .write_iter = generic_file_write_iter,
  1312. #endif
  1313. .mmap = generic_file_mmap,
  1314. .release = exfat_file_release,
  1315. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36)
  1316. .ioctl = exfat_generic_ioctl,
  1317. .fsync = exfat_file_fsync,
  1318. #else
  1319. .unlocked_ioctl = exfat_generic_ioctl,
  1320. .fsync = generic_file_fsync,
  1321. #endif
  1322. .splice_read = generic_file_splice_read,
  1323. };
  1324. static void _exfat_truncate(struct inode *inode, loff_t old_size)
  1325. {
  1326. struct super_block *sb = inode->i_sb;
  1327. struct exfat_sb_info *sbi = EXFAT_SB(sb);
  1328. FS_INFO_T *p_fs = &(sbi->fs_info);
  1329. int err;
  1330. __lock_super(sb);
  1331. if (EXFAT_I(inode)->mmu_private > i_size_read(inode))
  1332. EXFAT_I(inode)->mmu_private = i_size_read(inode);
  1333. if (EXFAT_I(inode)->fid.start_clu == 0) goto out;
  1334. err = FsTruncateFile(inode, old_size, i_size_read(inode));
  1335. if (err) goto out;
  1336. inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC;
  1337. if (IS_DIRSYNC(inode))
  1338. (void) exfat_sync_inode(inode);
  1339. else
  1340. mark_inode_dirty(inode);
  1341. inode->i_blocks = ((i_size_read(inode) + (p_fs->cluster_size - 1))
  1342. & ~((loff_t)p_fs->cluster_size - 1)) >> inode->i_blkbits;
  1343. out:
  1344. __unlock_super(sb);
  1345. }
  1346. #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,36)
  1347. static void exfat_truncate(struct inode *inode)
  1348. {
  1349. _exfat_truncate(inode, i_size_read(inode));
  1350. }
  1351. #endif
  1352. const struct inode_operations exfat_file_inode_operations = {
  1353. #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,36)
  1354. .truncate = exfat_truncate,
  1355. #endif
  1356. .setattr = exfat_setattr,
  1357. .getattr = exfat_getattr,
  1358. #ifdef CONFIG_EXFAT_VIRTUAL_XATTR
  1359. .setxattr = exfat_setxattr,
  1360. .getxattr = exfat_getxattr,
  1361. .listxattr = exfat_listxattr,
  1362. .removexattr = exfat_removexattr,
  1363. #endif
  1364. };
  1365. static int exfat_bmap(struct inode *inode, sector_t sector, sector_t *phys,
  1366. unsigned long *mapped_blocks, int *create)
  1367. {
  1368. struct super_block *sb = inode->i_sb;
  1369. struct exfat_sb_info *sbi = EXFAT_SB(sb);
  1370. FS_INFO_T *p_fs = &(sbi->fs_info);
  1371. BD_INFO_T *p_bd = &(sbi->bd_info);
  1372. const unsigned long blocksize = sb->s_blocksize;
  1373. const unsigned char blocksize_bits = sb->s_blocksize_bits;
  1374. sector_t last_block;
  1375. int err, clu_offset, sec_offset;
  1376. unsigned int cluster;
  1377. *phys = 0;
  1378. *mapped_blocks = 0;
  1379. if ((p_fs->vol_type == FAT12) || (p_fs->vol_type == FAT16)) {
  1380. if (inode->i_ino == EXFAT_ROOT_INO) {
  1381. if (sector < (p_fs->dentries_in_root >> (p_bd->sector_size_bits-DENTRY_SIZE_BITS))) {
  1382. *phys = sector + p_fs->root_start_sector;
  1383. *mapped_blocks = 1;
  1384. }
  1385. return 0;
  1386. }
  1387. }
  1388. last_block = (i_size_read(inode) + (blocksize - 1)) >> blocksize_bits;
  1389. if (sector >= last_block) {
  1390. if (*create == 0) return 0;
  1391. } else {
  1392. *create = 0;
  1393. }
  1394. clu_offset = sector >> p_fs->sectors_per_clu_bits;
  1395. sec_offset = sector & (p_fs->sectors_per_clu - 1);
  1396. EXFAT_I(inode)->fid.size = i_size_read(inode);
  1397. err = FsMapCluster(inode, clu_offset, &cluster);
  1398. if (err) {
  1399. if (err == FFS_FULL)
  1400. return -ENOSPC;
  1401. else
  1402. return -EIO;
  1403. } else if (cluster != CLUSTER_32(~0)) {
  1404. *phys = START_SECTOR(cluster) + sec_offset;
  1405. *mapped_blocks = p_fs->sectors_per_clu - sec_offset;
  1406. }
  1407. return 0;
  1408. }
  1409. static int exfat_get_block(struct inode *inode, sector_t iblock,
  1410. struct buffer_head *bh_result, int create)
  1411. {
  1412. struct super_block *sb = inode->i_sb;
  1413. unsigned long max_blocks = bh_result->b_size >> inode->i_blkbits;
  1414. int err;
  1415. unsigned long mapped_blocks;
  1416. sector_t phys;
  1417. __lock_super(sb);
  1418. err = exfat_bmap(inode, iblock, &phys, &mapped_blocks, &create);
  1419. if (err) {
  1420. __unlock_super(sb);
  1421. return err;
  1422. }
  1423. if (phys) {
  1424. max_blocks = min(mapped_blocks, max_blocks);
  1425. if (create) {
  1426. EXFAT_I(inode)->mmu_private += max_blocks << sb->s_blocksize_bits;
  1427. set_buffer_new(bh_result);
  1428. }
  1429. map_bh(bh_result, sb, phys);
  1430. }
  1431. bh_result->b_size = max_blocks << sb->s_blocksize_bits;
  1432. __unlock_super(sb);
  1433. return 0;
  1434. }
  1435. static int exfat_readpage(struct file *file, struct page *page)
  1436. {
  1437. int ret;
  1438. ret = mpage_readpage(page, exfat_get_block);
  1439. return ret;
  1440. }
  1441. static int exfat_readpages(struct file *file, struct address_space *mapping,
  1442. struct list_head *pages, unsigned nr_pages)
  1443. {
  1444. int ret;
  1445. ret = mpage_readpages(mapping, pages, nr_pages, exfat_get_block);
  1446. return ret;
  1447. }
  1448. static int exfat_writepage(struct page *page, struct writeback_control *wbc)
  1449. {
  1450. int ret;
  1451. ret = block_write_full_page(page, exfat_get_block, wbc);
  1452. return ret;
  1453. }
  1454. static int exfat_writepages(struct address_space *mapping,
  1455. struct writeback_control *wbc)
  1456. {
  1457. int ret;
  1458. ret = mpage_writepages(mapping, wbc, exfat_get_block);
  1459. return ret;
  1460. }
  1461. #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,34)
  1462. static void exfat_write_failed(struct address_space *mapping, loff_t to)
  1463. {
  1464. struct inode *inode = mapping->host;
  1465. if (to > i_size_read(inode)) {
  1466. #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,14,00)
  1467. truncate_pagecache(inode, i_size_read(inode));
  1468. #else
  1469. truncate_pagecache(inode, to, i_size_read(inode));
  1470. #endif
  1471. EXFAT_I(inode)->fid.size = i_size_read(inode);
  1472. _exfat_truncate(inode, i_size_read(inode));
  1473. }
  1474. }
  1475. #endif
  1476. static int exfat_write_begin(struct file *file, struct address_space *mapping,
  1477. loff_t pos, unsigned len, unsigned flags,
  1478. struct page **pagep, void **fsdata)
  1479. {
  1480. int ret;
  1481. *pagep = NULL;
  1482. ret = cont_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
  1483. exfat_get_block,
  1484. &EXFAT_I(mapping->host)->mmu_private);
  1485. #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,34)
  1486. if (ret < 0)
  1487. exfat_write_failed(mapping, pos+len);
  1488. #endif
  1489. return ret;
  1490. }
  1491. static int exfat_write_end(struct file *file, struct address_space *mapping,
  1492. loff_t pos, unsigned len, unsigned copied,
  1493. struct page *pagep, void *fsdata)
  1494. {
  1495. struct inode *inode = mapping->host;
  1496. FILE_ID_T *fid = &(EXFAT_I(inode)->fid);
  1497. int err;
  1498. err = generic_write_end(file, mapping, pos, len, copied, pagep, fsdata);
  1499. #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,34)
  1500. if (err < len)
  1501. exfat_write_failed(mapping, pos+len);
  1502. #endif
  1503. if (!(err < 0) && !(fid->attr & ATTR_ARCHIVE)) {
  1504. inode->i_mtime = inode->i_ctime = CURRENT_TIME_SEC;
  1505. fid->attr |= ATTR_ARCHIVE;
  1506. mark_inode_dirty(inode);
  1507. }
  1508. return err;
  1509. }
  1510. #if LINUX_VERSION_CODE < KERNEL_VERSION(3,16,0)
  1511. static ssize_t exfat_direct_IO(int rw, struct kiocb *iocb,
  1512. const struct iovec *iov,
  1513. loff_t offset, unsigned long nr_segs)
  1514. {
  1515. struct inode *inode = iocb->ki_filp->f_mapping->host;
  1516. #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,34)
  1517. struct address_space *mapping = iocb->ki_filp->f_mapping;
  1518. #endif
  1519. ssize_t ret;
  1520. if (rw == WRITE) {
  1521. if (EXFAT_I(inode)->mmu_private < (offset + iov_length(iov, nr_segs)))
  1522. return 0;
  1523. }
  1524. #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,00)
  1525. ret = blockdev_direct_IO(rw, iocb, inode, iov,
  1526. offset, nr_segs, exfat_get_block);
  1527. #else
  1528. ret = blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
  1529. offset, nr_segs, exfat_get_block, NULL);
  1530. #endif
  1531. #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,34)
  1532. if ((ret < 0) && (rw & WRITE))
  1533. exfat_write_failed(mapping, offset+iov_length(iov, nr_segs));
  1534. #endif
  1535. return ret;
  1536. }
  1537. #else
  1538. static ssize_t exfat_direct_IO(int rw, struct kiocb *iocb,
  1539. struct iov_iter *iter,
  1540. loff_t offset)
  1541. {
  1542. struct file *file = iocb->ki_filp;
  1543. struct address_space *mapping = file->f_mapping;
  1544. struct inode *inode = mapping->host;
  1545. size_t count = iov_iter_count(iter);
  1546. ssize_t ret;
  1547. if (rw == WRITE) {
  1548. /*
  1549. * FIXME: blockdev_direct_IO() doesn't use ->write_begin(),
  1550. * so we need to update the ->mmu_private to block boundary.
  1551. *
  1552. * But we must fill the remaining area or hole by nul for
  1553. * updating ->mmu_private.
  1554. *
  1555. * Return 0, and fallback to normal buffered write.
  1556. */
  1557. loff_t size = offset + count;
  1558. if (EXFAT_I(inode)->mmu_private < size)
  1559. return 0;
  1560. }
  1561. /*
  1562. * FAT need to use the DIO_LOCKING for avoiding the race
  1563. * condition of fat_get_block() and ->truncate().
  1564. */
  1565. ret = blockdev_direct_IO(rw, iocb, inode, iter, offset, exfat_get_block);
  1566. if (ret < 0 && (rw & WRITE))
  1567. exfat_write_failed(mapping, offset + count);
  1568. return ret;
  1569. }
  1570. #endif
  1571. static sector_t _exfat_bmap(struct address_space *mapping, sector_t block)
  1572. {
  1573. sector_t blocknr;
  1574. #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,00)
  1575. down_read(&EXFAT_I(mapping->host)->truncate_lock);
  1576. blocknr = generic_block_bmap(mapping, block, exfat_get_block);
  1577. up_read(&EXFAT_I(mapping->host)->truncate_lock);
  1578. #else
  1579. down_read(&mapping->host->i_alloc_sem);
  1580. blocknr = generic_block_bmap(mapping, block, exfat_get_block);
  1581. up_read(&mapping->host->i_alloc_sem);
  1582. #endif
  1583. return blocknr;
  1584. }
  1585. const struct address_space_operations exfat_aops = {
  1586. .readpage = exfat_readpage,
  1587. .readpages = exfat_readpages,
  1588. .writepage = exfat_writepage,
  1589. .writepages = exfat_writepages,
  1590. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,39)
  1591. .sync_page = block_sync_page,
  1592. #endif
  1593. .write_begin = exfat_write_begin,
  1594. .write_end = exfat_write_end,
  1595. .direct_IO = exfat_direct_IO,
  1596. .bmap = _exfat_bmap
  1597. };
  1598. static inline unsigned long exfat_hash(loff_t i_pos)
  1599. {
  1600. return hash_32(i_pos, EXFAT_HASH_BITS);
  1601. }
  1602. static struct inode *exfat_iget(struct super_block *sb, loff_t i_pos) {
  1603. struct exfat_sb_info *sbi = EXFAT_SB(sb);
  1604. struct exfat_inode_info *info;
  1605. struct hlist_head *head = sbi->inode_hashtable + exfat_hash(i_pos);
  1606. struct inode *inode = NULL;
  1607. #if LINUX_VERSION_CODE < KERNEL_VERSION(3,9,0)
  1608. struct hlist_node *node;
  1609. spin_lock(&sbi->inode_hash_lock);
  1610. hlist_for_each_entry(info, node, head, i_hash_fat) {
  1611. #else
  1612. spin_lock(&sbi->inode_hash_lock);
  1613. hlist_for_each_entry(info, head, i_hash_fat) {
  1614. #endif
  1615. CHECK_ERR(info->vfs_inode.i_sb != sb);
  1616. if (i_pos != info->i_pos)
  1617. continue;
  1618. inode = igrab(&info->vfs_inode);
  1619. if (inode)
  1620. break;
  1621. }
  1622. spin_unlock(&sbi->inode_hash_lock);
  1623. return inode;
  1624. }
  1625. static void exfat_attach(struct inode *inode, loff_t i_pos)
  1626. {
  1627. struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb);
  1628. struct hlist_head *head = sbi->inode_hashtable + exfat_hash(i_pos);
  1629. spin_lock(&sbi->inode_hash_lock);
  1630. EXFAT_I(inode)->i_pos = i_pos;
  1631. hlist_add_head(&EXFAT_I(inode)->i_hash_fat, head);
  1632. spin_unlock(&sbi->inode_hash_lock);
  1633. }
  1634. static void exfat_detach(struct inode *inode)
  1635. {
  1636. struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb);
  1637. spin_lock(&sbi->inode_hash_lock);
  1638. hlist_del_init(&EXFAT_I(inode)->i_hash_fat);
  1639. EXFAT_I(inode)->i_pos = 0;
  1640. spin_unlock(&sbi->inode_hash_lock);
  1641. }
  1642. static int exfat_fill_inode(struct inode *inode, FILE_ID_T *fid)
  1643. {
  1644. struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb);
  1645. FS_INFO_T *p_fs = &(sbi->fs_info);
  1646. DIR_ENTRY_T info;
  1647. memcpy(&(EXFAT_I(inode)->fid), fid, sizeof(FILE_ID_T));
  1648. FsReadStat(inode, &info);
  1649. EXFAT_I(inode)->i_pos = 0;
  1650. EXFAT_I(inode)->target = NULL;
  1651. inode->i_uid = sbi->options.fs_uid;
  1652. inode->i_gid = sbi->options.fs_gid;
  1653. inode->i_version++;
  1654. inode->i_generation = get_seconds();
  1655. if (info.Attr & ATTR_SUBDIR) {
  1656. inode->i_generation &= ~1;
  1657. inode->i_mode = exfat_make_mode(sbi, info.Attr, S_IRWXUGO);
  1658. inode->i_op = &exfat_dir_inode_operations;
  1659. inode->i_fop = &exfat_dir_operations;
  1660. i_size_write(inode, info.Size);
  1661. EXFAT_I(inode)->mmu_private = i_size_read(inode);
  1662. #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,00)
  1663. set_nlink(inode,info.NumSubdirs);
  1664. #else
  1665. inode->i_nlink = info.NumSubdirs;
  1666. #endif
  1667. } else if (info.Attr & ATTR_SYMLINK) {
  1668. inode->i_generation |= 1;
  1669. inode->i_mode = exfat_make_mode(sbi, info.Attr, S_IRWXUGO);
  1670. inode->i_op = &exfat_symlink_inode_operations;
  1671. i_size_write(inode, info.Size);
  1672. EXFAT_I(inode)->mmu_private = i_size_read(inode);
  1673. } else {
  1674. inode->i_generation |= 1;
  1675. inode->i_mode = exfat_make_mode(sbi, info.Attr, S_IRWXUGO);
  1676. inode->i_op = &exfat_file_inode_operations;
  1677. inode->i_fop = &exfat_file_operations;
  1678. inode->i_mapping->a_ops = &exfat_aops;
  1679. inode->i_mapping->nrpages = 0;
  1680. i_size_write(inode, info.Size);
  1681. EXFAT_I(inode)->mmu_private = i_size_read(inode);
  1682. }
  1683. exfat_save_attr(inode, info.Attr);
  1684. inode->i_blocks = ((i_size_read(inode) + (p_fs->cluster_size - 1))
  1685. & ~((loff_t)p_fs->cluster_size - 1)) >> inode->i_blkbits;
  1686. exfat_time_fat2unix(sbi, &inode->i_mtime, &info.ModifyTimestamp);
  1687. exfat_time_fat2unix(sbi, &inode->i_ctime, &info.CreateTimestamp);
  1688. exfat_time_fat2unix(sbi, &inode->i_atime, &info.AccessTimestamp);
  1689. return 0;
  1690. }
  1691. static struct inode *exfat_build_inode(struct super_block *sb,
  1692. FILE_ID_T *fid, loff_t i_pos) {
  1693. struct inode *inode;
  1694. int err;
  1695. inode = exfat_iget(sb, i_pos);
  1696. if (inode)
  1697. goto out;
  1698. inode = new_inode(sb);
  1699. if (!inode) {
  1700. inode = ERR_PTR(-ENOMEM);
  1701. goto out;
  1702. }
  1703. inode->i_ino = iunique(sb, EXFAT_ROOT_INO);
  1704. inode->i_version = 1;
  1705. err = exfat_fill_inode(inode, fid);
  1706. if (err) {
  1707. iput(inode);
  1708. inode = ERR_PTR(err);
  1709. goto out;
  1710. }
  1711. exfat_attach(inode, i_pos);
  1712. insert_inode_hash(inode);
  1713. out:
  1714. return inode;
  1715. }
  1716. static int exfat_sync_inode(struct inode *inode)
  1717. {
  1718. return exfat_write_inode(inode, NULL);
  1719. }
  1720. static struct inode *exfat_alloc_inode(struct super_block *sb) {
  1721. struct exfat_inode_info *ei;
  1722. ei = kmem_cache_alloc(exfat_inode_cachep, GFP_NOFS);
  1723. if (!ei)
  1724. return NULL;
  1725. #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,00)
  1726. init_rwsem(&ei->truncate_lock);
  1727. #endif
  1728. return &ei->vfs_inode;
  1729. }
  1730. static void exfat_destroy_inode(struct inode *inode)
  1731. {
  1732. FREE(EXFAT_I(inode)->target);
  1733. EXFAT_I(inode)->target = NULL;
  1734. kmem_cache_free(exfat_inode_cachep, EXFAT_I(inode));
  1735. }
  1736. static int exfat_write_inode(struct inode *inode, struct writeback_control *wbc)
  1737. {
  1738. struct super_block *sb = inode->i_sb;
  1739. struct exfat_sb_info *sbi = EXFAT_SB(sb);
  1740. DIR_ENTRY_T info;
  1741. if (inode->i_ino == EXFAT_ROOT_INO)
  1742. return 0;
  1743. info.Attr = exfat_make_attr(inode);
  1744. info.Size = i_size_read(inode);
  1745. exfat_time_unix2fat(sbi, &inode->i_mtime, &info.ModifyTimestamp);
  1746. exfat_time_unix2fat(sbi, &inode->i_ctime, &info.CreateTimestamp);
  1747. exfat_time_unix2fat(sbi, &inode->i_atime, &info.AccessTimestamp);
  1748. FsWriteStat(inode, &info);
  1749. return 0;
  1750. }
  1751. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36)
  1752. static void exfat_delete_inode(struct inode *inode)
  1753. {
  1754. truncate_inode_pages(&inode->i_data, 0);
  1755. clear_inode(inode);
  1756. }
  1757. static void exfat_clear_inode(struct inode *inode)
  1758. {
  1759. exfat_detach(inode);
  1760. remove_inode_hash(inode);
  1761. }
  1762. #else
  1763. static void exfat_evict_inode(struct inode *inode)
  1764. {
  1765. truncate_inode_pages(&inode->i_data, 0);
  1766. if (!inode->i_nlink) {
  1767. loff_t old_size = i_size_read(inode);
  1768. i_size_write(inode, 0);
  1769. EXFAT_I(inode)->fid.size = old_size;
  1770. FsTruncateFile(inode, old_size, 0);
  1771. }
  1772. invalidate_inode_buffers(inode);
  1773. #if LINUX_VERSION_CODE < KERNEL_VERSION(3,7,00)
  1774. end_writeback(inode);
  1775. #else
  1776. clear_inode(inode);
  1777. #endif
  1778. exfat_detach(inode);
  1779. remove_inode_hash(inode);
  1780. }
  1781. #endif
  1782. static void exfat_put_super(struct super_block *sb)
  1783. {
  1784. struct exfat_sb_info *sbi = EXFAT_SB(sb);
  1785. int err;
  1786. exfat_mnt_msg(sb, 0, 0, "trying to unmount...");
  1787. if (__is_sb_dirty(sb))
  1788. exfat_write_super(sb);
  1789. err = FsUmountVol(sb);
  1790. if (sbi->nls_disk) {
  1791. unload_nls(sbi->nls_disk);
  1792. sbi->nls_disk = NULL;
  1793. sbi->options.codepage = exfat_default_codepage;
  1794. }
  1795. if (sbi->nls_io) {
  1796. unload_nls(sbi->nls_io);
  1797. sbi->nls_io = NULL;
  1798. }
  1799. if (sbi->options.iocharset != exfat_default_iocharset) {
  1800. kfree(sbi->options.iocharset);
  1801. sbi->options.iocharset = exfat_default_iocharset;
  1802. }
  1803. sb->s_fs_info = NULL;
  1804. if (!sbi->use_vmalloc)
  1805. kfree(sbi);
  1806. else
  1807. vfree(sbi);
  1808. exfat_mnt_msg(sb, 0, err, "unmounted successfully!");
  1809. }
  1810. static void exfat_write_super(struct super_block *sb)
  1811. {
  1812. __lock_super(sb);
  1813. __set_sb_clean(sb);
  1814. if (!(sb->s_flags & MS_RDONLY))
  1815. FsSyncVol(sb, 1);
  1816. __unlock_super(sb);
  1817. }
  1818. static int exfat_sync_fs(struct super_block *sb, int wait)
  1819. {
  1820. int err = 0;
  1821. if (__is_sb_dirty(sb)) {
  1822. __lock_super(sb);
  1823. __set_sb_clean(sb);
  1824. err = FsSyncVol(sb, 1);
  1825. __unlock_super(sb);
  1826. }
  1827. return err;
  1828. }
  1829. static int exfat_statfs(struct dentry *dentry, struct kstatfs *buf)
  1830. {
  1831. struct super_block *sb = dentry->d_sb;
  1832. u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
  1833. FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
  1834. VOL_INFO_T info;
  1835. if (p_fs->used_clusters == (UINT32) ~0) {
  1836. if (FFS_MEDIAERR == FsGetVolInfo(sb, &info))
  1837. return -EIO;
  1838. } else {
  1839. info.FatType = p_fs->vol_type;
  1840. info.ClusterSize = p_fs->cluster_size;
  1841. info.NumClusters = p_fs->num_clusters - 2;
  1842. info.UsedClusters = p_fs->used_clusters;
  1843. info.FreeClusters = info.NumClusters - info.UsedClusters;
  1844. if (p_fs->dev_ejected)
  1845. printk("[EXFAT] called statfs with previous I/O error.\n");
  1846. }
  1847. buf->f_type = sb->s_magic;
  1848. buf->f_bsize = info.ClusterSize;
  1849. buf->f_blocks = info.NumClusters;
  1850. buf->f_bfree = info.FreeClusters;
  1851. buf->f_bavail = info.FreeClusters;
  1852. buf->f_fsid.val[0] = (u32)id;
  1853. buf->f_fsid.val[1] = (u32)(id >> 32);
  1854. buf->f_namelen = 260;
  1855. return 0;
  1856. }
  1857. static int exfat_remount(struct super_block *sb, int *flags, char *data)
  1858. {
  1859. char *orig_data = kstrdup(data, GFP_KERNEL);
  1860. *flags |= MS_NODIRATIME;
  1861. #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,16,00)
  1862. sync_filesystem(sb);
  1863. #endif
  1864. exfat_msg(sb, KERN_INFO, "re-mounted. Opts: %s", orig_data);
  1865. ST_LOG("[EXFAT] re-mounted. Opts: %s", orig_data);
  1866. kfree(orig_data);
  1867. return 0;
  1868. }
  1869. #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,00)
  1870. static int exfat_show_options(struct seq_file *m, struct dentry *root)
  1871. {
  1872. struct exfat_sb_info *sbi = EXFAT_SB(root->d_sb);
  1873. #else
  1874. static int exfat_show_options(struct seq_file *m, struct vfsmount *mnt)
  1875. {
  1876. struct exfat_sb_info *sbi = EXFAT_SB(mnt->mnt_sb);
  1877. #endif
  1878. struct exfat_mount_options *opts = &sbi->options;
  1879. FS_INFO_T *p_fs = &(sbi->fs_info);
  1880. if (!uid_eq(opts->fs_uid, GLOBAL_ROOT_UID))
  1881. seq_printf(m, ",uid=%u",
  1882. from_kuid_munged(&init_user_ns, opts->fs_uid));
  1883. if (!gid_eq(opts->fs_gid, GLOBAL_ROOT_GID))
  1884. seq_printf(m, ",gid=%u",
  1885. from_kgid_munged(&init_user_ns, opts->fs_gid));
  1886. seq_printf(m, ",fmask=%04o", opts->fs_fmask);
  1887. seq_printf(m, ",dmask=%04o", opts->fs_dmask);
  1888. if (opts->allow_utime)
  1889. seq_printf(m, ",allow_utime=%04o", opts->allow_utime);
  1890. if (sbi->nls_disk)
  1891. seq_printf(m, ",codepage=%s", sbi->nls_disk->charset);
  1892. if (sbi->nls_io)
  1893. seq_printf(m, ",iocharset=%s", sbi->nls_io->charset);
  1894. seq_printf(m, ",namecase=%u", opts->casesensitive);
  1895. if (opts->tz_utc)
  1896. seq_puts(m, ",tz=UTC");
  1897. if (opts->errors == EXFAT_ERRORS_CONT)
  1898. seq_puts(m, ",errors=continue");
  1899. else if (opts->errors == EXFAT_ERRORS_PANIC)
  1900. seq_puts(m, ",errors=panic");
  1901. else
  1902. seq_puts(m, ",errors=remount-ro");
  1903. #if EXFAT_CONFIG_DISCARD
  1904. if (opts->discard)
  1905. seq_printf(m, ",discard");
  1906. #endif
  1907. if (p_fs->dev_ejected)
  1908. seq_puts(m, ",ejected");
  1909. return 0;
  1910. }
  1911. const struct super_operations exfat_sops = {
  1912. .alloc_inode = exfat_alloc_inode,
  1913. .destroy_inode = exfat_destroy_inode,
  1914. .write_inode = exfat_write_inode,
  1915. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36)
  1916. .delete_inode = exfat_delete_inode,
  1917. .clear_inode = exfat_clear_inode,
  1918. #else
  1919. .evict_inode = exfat_evict_inode,
  1920. #endif
  1921. .put_super = exfat_put_super,
  1922. #if LINUX_VERSION_CODE < KERNEL_VERSION(3,7,00)
  1923. .write_super = exfat_write_super,
  1924. #endif
  1925. .sync_fs = exfat_sync_fs,
  1926. .statfs = exfat_statfs,
  1927. .remount_fs = exfat_remount,
  1928. .show_options = exfat_show_options,
  1929. };
  1930. enum {
  1931. Opt_uid,
  1932. Opt_gid,
  1933. Opt_umask,
  1934. Opt_dmask,
  1935. Opt_fmask,
  1936. Opt_allow_utime,
  1937. Opt_codepage,
  1938. Opt_charset,
  1939. Opt_namecase,
  1940. Opt_debug,
  1941. Opt_tz_utc,
  1942. Opt_err_cont,
  1943. Opt_err_panic,
  1944. Opt_err_ro,
  1945. Opt_err,
  1946. #if EXFAT_CONFIG_DISCARD
  1947. Opt_discard,
  1948. #endif
  1949. };
  1950. static const match_table_t exfat_tokens = {
  1951. {Opt_uid, "uid=%u"},
  1952. {Opt_gid, "gid=%u"},
  1953. {Opt_umask, "umask=%o"},
  1954. {Opt_dmask, "dmask=%o"},
  1955. {Opt_fmask, "fmask=%o"},
  1956. {Opt_allow_utime, "allow_utime=%o"},
  1957. {Opt_codepage, "codepage=%u"},
  1958. {Opt_charset, "iocharset=%s"},
  1959. {Opt_namecase, "namecase=%u"},
  1960. {Opt_debug, "debug"},
  1961. {Opt_tz_utc, "tz=UTC"},
  1962. {Opt_err_cont, "errors=continue"},
  1963. {Opt_err_panic, "errors=panic"},
  1964. {Opt_err_ro, "errors=remount-ro"},
  1965. #if EXFAT_CONFIG_DISCARD
  1966. {Opt_discard, "discard"},
  1967. #endif
  1968. {Opt_err, NULL}
  1969. };
  1970. static int parse_options(char *options, int silent, int *debug,
  1971. struct exfat_mount_options *opts)
  1972. {
  1973. char *p;
  1974. substring_t args[MAX_OPT_ARGS];
  1975. int option;
  1976. char *iocharset;
  1977. opts->fs_uid = current_uid();
  1978. opts->fs_gid = current_gid();
  1979. opts->fs_fmask = opts->fs_dmask = current->fs->umask;
  1980. opts->allow_utime = (unsigned short) -1;
  1981. opts->codepage = exfat_default_codepage;
  1982. opts->iocharset = exfat_default_iocharset;
  1983. opts->casesensitive = 0;
  1984. opts->tz_utc = 0;
  1985. opts->errors = EXFAT_ERRORS_RO;
  1986. #if EXFAT_CONFIG_DISCARD
  1987. opts->discard = 0;
  1988. #endif
  1989. *debug = 0;
  1990. if (!options)
  1991. goto out;
  1992. while ((p = strsep(&options, ",")) != NULL) {
  1993. int token;
  1994. if (!*p)
  1995. continue;
  1996. token = match_token(p, exfat_tokens, args);
  1997. switch (token) {
  1998. case Opt_uid:
  1999. if (match_int(&args[0], &option))
  2000. return 0;
  2001. opts->fs_uid = make_kuid(current_user_ns(), option);
  2002. break;
  2003. case Opt_gid:
  2004. if (match_int(&args[0], &option))
  2005. return 0;
  2006. opts->fs_gid = make_kgid(current_user_ns(), option);
  2007. break;
  2008. case Opt_umask:
  2009. case Opt_dmask:
  2010. case Opt_fmask:
  2011. if (match_octal(&args[0], &option))
  2012. return 0;
  2013. if (token != Opt_dmask)
  2014. opts->fs_fmask = option;
  2015. if (token != Opt_fmask)
  2016. opts->fs_dmask = option;
  2017. break;
  2018. case Opt_allow_utime:
  2019. if (match_octal(&args[0], &option))
  2020. return 0;
  2021. opts->allow_utime = option & (S_IWGRP | S_IWOTH);
  2022. break;
  2023. case Opt_codepage:
  2024. if (match_int(&args[0], &option))
  2025. return 0;
  2026. opts->codepage = option;
  2027. break;
  2028. case Opt_charset:
  2029. if (opts->iocharset != exfat_default_iocharset)
  2030. kfree(opts->iocharset);
  2031. iocharset = match_strdup(&args[0]);
  2032. if (!iocharset)
  2033. return -ENOMEM;
  2034. opts->iocharset = iocharset;
  2035. break;
  2036. case Opt_namecase:
  2037. if (match_int(&args[0], &option))
  2038. return 0;
  2039. opts->casesensitive = option;
  2040. break;
  2041. case Opt_tz_utc:
  2042. opts->tz_utc = 1;
  2043. break;
  2044. case Opt_err_cont:
  2045. opts->errors = EXFAT_ERRORS_CONT;
  2046. break;
  2047. case Opt_err_panic:
  2048. opts->errors = EXFAT_ERRORS_PANIC;
  2049. break;
  2050. case Opt_err_ro:
  2051. opts->errors = EXFAT_ERRORS_RO;
  2052. break;
  2053. case Opt_debug:
  2054. *debug = 1;
  2055. break;
  2056. #if EXFAT_CONFIG_DISCARD
  2057. case Opt_discard:
  2058. opts->discard = 1;
  2059. break;
  2060. #endif
  2061. default:
  2062. if (!silent) {
  2063. printk(KERN_ERR "[EXFAT] Unrecognized mount option %s or missing value\n", p);
  2064. }
  2065. return -EINVAL;
  2066. }
  2067. }
  2068. out:
  2069. if (opts->allow_utime == (unsigned short) -1)
  2070. opts->allow_utime = ~opts->fs_dmask & (S_IWGRP | S_IWOTH);
  2071. return 0;
  2072. }
  2073. static void exfat_hash_init(struct super_block *sb)
  2074. {
  2075. struct exfat_sb_info *sbi = EXFAT_SB(sb);
  2076. int i;
  2077. spin_lock_init(&sbi->inode_hash_lock);
  2078. for (i = 0; i < EXFAT_HASH_SIZE; i++)
  2079. INIT_HLIST_HEAD(&sbi->inode_hashtable[i]);
  2080. }
  2081. static int exfat_read_root(struct inode *inode)
  2082. {
  2083. struct super_block *sb = inode->i_sb;
  2084. struct exfat_sb_info *sbi = EXFAT_SB(sb);
  2085. struct timespec ts;
  2086. FS_INFO_T *p_fs = &(sbi->fs_info);
  2087. DIR_ENTRY_T info;
  2088. ts = CURRENT_TIME_SEC;
  2089. EXFAT_I(inode)->fid.dir.dir = p_fs->root_dir;
  2090. EXFAT_I(inode)->fid.dir.flags = 0x01;
  2091. EXFAT_I(inode)->fid.entry = -1;
  2092. EXFAT_I(inode)->fid.start_clu = p_fs->root_dir;
  2093. EXFAT_I(inode)->fid.flags = 0x01;
  2094. EXFAT_I(inode)->fid.type = TYPE_DIR;
  2095. EXFAT_I(inode)->fid.rwoffset = 0;
  2096. EXFAT_I(inode)->fid.hint_last_off = -1;
  2097. EXFAT_I(inode)->target = NULL;
  2098. FsReadStat(inode, &info);
  2099. inode->i_uid = sbi->options.fs_uid;
  2100. inode->i_gid = sbi->options.fs_gid;
  2101. inode->i_version++;
  2102. inode->i_generation = 0;
  2103. inode->i_mode = exfat_make_mode(sbi, ATTR_SUBDIR, S_IRWXUGO);
  2104. inode->i_op = &exfat_dir_inode_operations;
  2105. inode->i_fop = &exfat_dir_operations;
  2106. i_size_write(inode, info.Size);
  2107. inode->i_blocks = ((i_size_read(inode) + (p_fs->cluster_size - 1))
  2108. & ~((loff_t)p_fs->cluster_size - 1)) >> inode->i_blkbits;
  2109. EXFAT_I(inode)->i_pos = ((loff_t) p_fs->root_dir << 32) | 0xffffffff;
  2110. EXFAT_I(inode)->mmu_private = i_size_read(inode);
  2111. exfat_save_attr(inode, ATTR_SUBDIR);
  2112. inode->i_mtime = inode->i_atime = inode->i_ctime = ts;
  2113. #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,00)
  2114. set_nlink(inode,info.NumSubdirs + 2);
  2115. #else
  2116. inode->i_nlink = info.NumSubdirs + 2;
  2117. #endif
  2118. return 0;
  2119. }
  2120. static void setup_dops(struct super_block *sb)
  2121. {
  2122. if (EXFAT_SB(sb)->options.casesensitive == 0)
  2123. sb->s_d_op = &exfat_ci_dentry_ops;
  2124. else
  2125. sb->s_d_op = &exfat_dentry_ops;
  2126. }
  2127. static int exfat_fill_super(struct super_block *sb, void *data, int silent)
  2128. {
  2129. struct inode *root_inode = NULL;
  2130. struct exfat_sb_info *sbi;
  2131. int debug, ret;
  2132. long error;
  2133. char buf[50];
  2134. exfat_mnt_msg(sb, 1, 0, "trying to mount...");
  2135. sbi = kzalloc(sizeof(struct exfat_sb_info), GFP_KERNEL);
  2136. if (!sbi) {
  2137. sbi = vzalloc(sizeof(struct exfat_sb_info));
  2138. if (!sbi) {
  2139. exfat_mnt_msg(sb, 1, 0, "failed to mount! (ENOMEM)");
  2140. return -ENOMEM;
  2141. }
  2142. sbi->use_vmalloc = 1;
  2143. }
  2144. #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,0)
  2145. mutex_init(&sbi->s_lock);
  2146. #endif
  2147. sb->s_fs_info = sbi;
  2148. sb->s_flags |= MS_NODIRATIME;
  2149. sb->s_magic = EXFAT_SUPER_MAGIC;
  2150. sb->s_op = &exfat_sops;
  2151. error = parse_options(data, silent, &debug, &sbi->options);
  2152. if (error)
  2153. goto out_fail;
  2154. setup_dops(sb);
  2155. error = -EIO;
  2156. sb_min_blocksize(sb, 512);
  2157. sb->s_maxbytes = 0x7fffffffffffffffLL;
  2158. ret = FsMountVol(sb);
  2159. if (ret) {
  2160. if (!silent)
  2161. printk(KERN_ERR "[EXFAT] FsMountVol failed\n");
  2162. goto out_fail;
  2163. }
  2164. exfat_hash_init(sb);
  2165. error = -EINVAL;
  2166. sprintf(buf, "cp%d", sbi->options.codepage);
  2167. sbi->nls_disk = load_nls(buf);
  2168. if (!sbi->nls_disk) {
  2169. printk(KERN_ERR "[EXFAT] Codepage %s not found\n", buf);
  2170. goto out_fail2;
  2171. }
  2172. sbi->nls_io = load_nls(sbi->options.iocharset);
  2173. if (!sbi->nls_io) {
  2174. printk(KERN_ERR "[EXFAT] IO charset %s not found\n",
  2175. sbi->options.iocharset);
  2176. goto out_fail2;
  2177. }
  2178. error = -ENOMEM;
  2179. root_inode = new_inode(sb);
  2180. if (!root_inode)
  2181. goto out_fail2;
  2182. root_inode->i_ino = EXFAT_ROOT_INO;
  2183. root_inode->i_version = 1;
  2184. error = exfat_read_root(root_inode);
  2185. if (error < 0)
  2186. goto out_fail2;
  2187. error = -ENOMEM;
  2188. exfat_attach(root_inode, EXFAT_I(root_inode)->i_pos);
  2189. insert_inode_hash(root_inode);
  2190. #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,00)
  2191. sb->s_root = d_make_root(root_inode);
  2192. #else
  2193. sb->s_root = d_alloc_root(root_inode);
  2194. #endif
  2195. if (!sb->s_root) {
  2196. printk(KERN_ERR "[EXFAT] Getting the root inode failed\n");
  2197. goto out_fail2;
  2198. }
  2199. exfat_mnt_msg(sb, 1, 0, "mounted successfully!");
  2200. return 0;
  2201. out_fail2:
  2202. FsUmountVol(sb);
  2203. out_fail:
  2204. exfat_mnt_msg(sb, 1, 0, "failed to mount!");
  2205. if (root_inode)
  2206. iput(root_inode);
  2207. if (sbi->nls_io)
  2208. unload_nls(sbi->nls_io);
  2209. if (sbi->nls_disk)
  2210. unload_nls(sbi->nls_disk);
  2211. if (sbi->options.iocharset != exfat_default_iocharset)
  2212. kfree(sbi->options.iocharset);
  2213. sb->s_fs_info = NULL;
  2214. if (!sbi->use_vmalloc)
  2215. kfree(sbi);
  2216. else
  2217. vfree(sbi);
  2218. return error;
  2219. }
  2220. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,37)
  2221. static int exfat_get_sb(struct file_system_type *fs_type,
  2222. int flags, const char *dev_name,
  2223. void *data, struct vfsmount *mnt)
  2224. {
  2225. return get_sb_bdev(fs_type, flags, dev_name, data, exfat_fill_super, mnt);
  2226. }
  2227. #else
  2228. static struct dentry *exfat_fs_mount(struct file_system_type *fs_type,
  2229. int flags, const char *dev_name,
  2230. void *data) {
  2231. return mount_bdev(fs_type, flags, dev_name, data, exfat_fill_super);
  2232. }
  2233. #endif
  2234. static void init_once(void *foo)
  2235. {
  2236. struct exfat_inode_info *ei = (struct exfat_inode_info *)foo;
  2237. INIT_HLIST_NODE(&ei->i_hash_fat);
  2238. inode_init_once(&ei->vfs_inode);
  2239. }
  2240. static int __init exfat_init_inodecache(void)
  2241. {
  2242. exfat_inode_cachep = kmem_cache_create("exfat_inode_cache",
  2243. sizeof(struct exfat_inode_info),
  2244. 0, (SLAB_RECLAIM_ACCOUNT|
  2245. SLAB_MEM_SPREAD),
  2246. init_once);
  2247. if (exfat_inode_cachep == NULL)
  2248. return -ENOMEM;
  2249. return 0;
  2250. }
  2251. static void __exit exfat_destroy_inodecache(void)
  2252. {
  2253. kmem_cache_destroy(exfat_inode_cachep);
  2254. }
  2255. #if EXFAT_CONFIG_KERNEL_DEBUG
  2256. static void exfat_debug_kill_sb(struct super_block *sb)
  2257. {
  2258. struct exfat_sb_info *sbi = EXFAT_SB(sb);
  2259. struct block_device *bdev = sb->s_bdev;
  2260. long flags;
  2261. if (sbi) {
  2262. flags = sbi->debug_flags;
  2263. if (flags & EXFAT_DEBUGFLAGS_INVALID_UMOUNT) {
  2264. FsReleaseCache(sb);
  2265. invalidate_bdev(bdev);
  2266. }
  2267. }
  2268. kill_block_super(sb);
  2269. }
  2270. #endif
  2271. static struct file_system_type exfat_fs_type = {
  2272. .owner = THIS_MODULE,
  2273. .name = "exfat",
  2274. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,37)
  2275. .get_sb = exfat_get_sb,
  2276. #else
  2277. .mount = exfat_fs_mount,
  2278. #endif
  2279. #if EXFAT_CONFIG_KERNEL_DEBUG
  2280. .kill_sb = exfat_debug_kill_sb,
  2281. #else
  2282. .kill_sb = kill_block_super,
  2283. #endif
  2284. .fs_flags = FS_REQUIRES_DEV,
  2285. };
  2286. static int __init init_exfat_fs(void)
  2287. {
  2288. int err;
  2289. printk(KERN_INFO "exFAT: FS Version %s\n", EXFAT_VERSION);
  2290. err = exfat_init_inodecache();
  2291. if (err) return err;
  2292. return register_filesystem(&exfat_fs_type);
  2293. }
  2294. static void __exit exit_exfat_fs(void)
  2295. {
  2296. exfat_destroy_inodecache();
  2297. unregister_filesystem(&exfat_fs_type);
  2298. }
  2299. module_init(init_exfat_fs);
  2300. module_exit(exit_exfat_fs);
  2301. MODULE_LICENSE("GPL");