1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710 |
- /* 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". */
- /*
- * linux/fs/fat/file.c
- *
- * Written 1992,1993 by Werner Almesberger
- *
- * regular file handling primitives for fat-based filesystems
- */
- /*
- * linux/fs/fat/inode.c
- *
- * Written 1992,1993 by Werner Almesberger
- * VFAT extensions by Gordon Chaffee, merged with msdos fs by Henrik Storner
- * Rewritten for the constant inumbers support by Al Viro
- *
- * Fixes:
- *
- * Max Cohan: Fixed invalid FSINFO offset when info_sector is 0
- */
- /*
- * linux/fs/fat/misc.c
- *
- * Written 1992,1993 by Werner Almesberger
- * 22/11/2000 - Fixed fat_date_unix2dos for dates earlier than 01/01/1980
- * and date_dos2unix for date==0 by Igor Zhbanov(bsg@uniyar.ac.ru)
- */
- /*
- * linux/fs/vfat/namei.c
- *
- * Written 1992,1993 by Werner Almesberger
- *
- * Windows95/Windows NT compatible extended MSDOS filesystem
- * by Gordon Chaffee Copyright (C) 1995. Send bug reports for the
- * VFAT filesystem to <chaffee@cs.berkeley.edu>. Specify
- * what file operation caused you trouble and if you can duplicate
- * the problem, send a script that demonstrates it.
- *
- * Short name translation 1999, 2001 by Wolfram Pienkoss <wp@bszh.de>
- *
- * Support Multibyte characters and cleanup by
- * OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>
- */
- /*
- * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd.
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
- #include <linux/version.h>
- #include <linux/module.h>
- #include <linux/init.h>
- #include <linux/time.h>
- #include <linux/slab.h>
- #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,37)
- #include <linux/smp_lock.h>
- #endif
- #include <linux/seq_file.h>
- #include <linux/pagemap.h>
- #include <linux/mpage.h>
- #include <linux/buffer_head.h>
- #include <linux/exportfs.h>
- #include <linux/mount.h>
- #include <linux/vfs.h>
- #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,10,0)
- #include <linux/aio.h>
- #endif
- #include <linux/parser.h>
- #include <linux/uio.h>
- #include <linux/writeback.h>
- #include <linux/log2.h>
- #include <linux/hash.h>
- #include <linux/backing-dev.h>
- #include <linux/sched.h>
- #include <linux/fs_struct.h>
- #include <linux/namei.h>
- #include <linux/vmalloc.h>
- #include <asm/current.h>
- #include <asm/unaligned.h>
- #include "exfat_version.h"
- #include "exfat_config.h"
- #include "exfat_global.h"
- #include "exfat_data.h"
- #include "exfat_oal.h"
- #include "exfat_blkdev.h"
- #include "exfat_cache.h"
- #include "exfat_part.h"
- #include "exfat_nls.h"
- #include "exfat_api.h"
- #include "exfat.h"
- #include "exfat_super.h"
- static struct kmem_cache *exfat_inode_cachep;
- static int exfat_default_codepage = DEFAULT_CODEPAGE;
- static char exfat_default_iocharset[] = DEFAULT_IOCHARSET;
- extern struct timezone sys_tz;
- #define EXFAT_DSTATE_LOCKED (void *)(0xCAFE2016)
- #define EXFAT_DSTATE_UNLOCKED (void *)(0x0)
- static inline void __lock_d_revalidate(struct dentry *dentry)
- {
- spin_lock(&dentry->d_lock);
- dentry->d_fsdata = EXFAT_DSTATE_LOCKED;
- spin_unlock(&dentry->d_lock);
- }
- static inline void __unlock_d_revalidate(struct dentry *dentry)
- {
- spin_lock(&dentry->d_lock);
- dentry->d_fsdata = EXFAT_DSTATE_UNLOCKED;
- spin_unlock(&dentry->d_lock);
- }
- /* __check_dstate_locked requires dentry->d_lock */
- static inline int __check_dstate_locked(struct dentry *dentry)
- {
- if (dentry->d_fsdata == EXFAT_DSTATE_LOCKED)
- return 1;
- return 0;
- }
- #define CHECK_ERR(x) BUG_ON(x)
- #define ELAPSED_TIME 0
- #if (ELAPSED_TIME == 1)
- #include <linux/time.h>
- static UINT32 __t1, __t2;
- static UINT32 get_current_msec(void)
- {
- struct timeval tm;
- do_gettimeofday(&tm);
- return((UINT32)(tm.tv_sec*1000000 + tm.tv_usec));
- }
- #define TIME_START() do {__t1 = get_current_msec();} while (0)
- #define TIME_END() do {__t2 = get_current_msec();} while (0)
- #define PRINT_TIME(n) do {printk("[EXFAT] Elapsed time %d = %d (usec)\n", n, (__t2 - __t1));} while (0)
- #else
- #define TIME_START()
- #define TIME_END()
- #define PRINT_TIME(n)
- #endif
- #define UNIX_SECS_1980 315532800L
- #if BITS_PER_LONG == 64
- #define UNIX_SECS_2108 4354819200L
- #endif
- #define DAYS_DELTA_DECADE (365 * 10 + 2)
- #define NO_LEAP_YEAR_2100 (120)
- #define IS_LEAP_YEAR(y) (!((y) & 0x3) && (y) != NO_LEAP_YEAR_2100)
- #define SECS_PER_MIN (60)
- #define SECS_PER_HOUR (60 * SECS_PER_MIN)
- #define SECS_PER_DAY (24 * SECS_PER_HOUR)
- #define MAKE_LEAP_YEAR(leap_year, year) \
- do { \
- if (unlikely(year > NO_LEAP_YEAR_2100)) \
- leap_year = ((year + 3) / 4) - 1; \
- else \
- leap_year = ((year + 3) / 4); \
- } while(0)
- static time_t accum_days_in_year[] = {
- 0, 0, 31, 59, 90,120,151,181,212,243,273,304,334, 0, 0, 0,
- };
- static void _exfat_truncate(struct inode *inode, loff_t old_size);
- void exfat_time_fat2unix(struct exfat_sb_info *sbi, struct timespec *ts,
- DATE_TIME_T *tp)
- {
- time_t year = tp->Year;
- time_t ld;
- MAKE_LEAP_YEAR(ld, year);
- if (IS_LEAP_YEAR(year) && (tp->Month) > 2)
- ld++;
- ts->tv_sec = tp->Second + tp->Minute * SECS_PER_MIN
- + tp->Hour * SECS_PER_HOUR
- + (year * 365 + ld + accum_days_in_year[(tp->Month)]
- + (tp->Day - 1) + DAYS_DELTA_DECADE) * SECS_PER_DAY;
- if(!sbi->options.tz_utc)
- ts->tv_sec += sys_tz.tz_minuteswest * SECS_PER_MIN;
- ts->tv_nsec = 0;
- }
- void exfat_time_unix2fat(struct exfat_sb_info *sbi, struct timespec *ts,
- DATE_TIME_T *tp)
- {
- time_t second = ts->tv_sec;
- time_t day, month, year;
- time_t ld;
- if (!sbi->options.tz_utc)
- second -= sys_tz.tz_minuteswest * SECS_PER_MIN;
- if (second < UNIX_SECS_1980) {
- tp->Second = 0;
- tp->Minute = 0;
- tp->Hour = 0;
- tp->Day = 1;
- tp->Month = 1;
- tp->Year = 0;
- return;
- }
- #if (BITS_PER_LONG == 64)
- if (second >= UNIX_SECS_2108) {
- tp->Second = 59;
- tp->Minute = 59;
- tp->Hour = 23;
- tp->Day = 31;
- tp->Month = 12;
- tp->Year = 127;
- return;
- }
- #endif
- day = second / SECS_PER_DAY - DAYS_DELTA_DECADE;
- year = day / 365;
- MAKE_LEAP_YEAR(ld, year);
- if (year * 365 + ld > day)
- year--;
- MAKE_LEAP_YEAR(ld, year);
- day -= year * 365 + ld;
- if (IS_LEAP_YEAR(year) && day == accum_days_in_year[3]) {
- month = 2;
- } else {
- if (IS_LEAP_YEAR(year) && day > accum_days_in_year[3])
- day--;
- for (month = 1; month < 12; month++) {
- if (accum_days_in_year[month + 1] > day)
- break;
- }
- }
- day -= accum_days_in_year[month];
- tp->Second = second % SECS_PER_MIN;
- tp->Minute = (second / SECS_PER_MIN) % 60;
- tp->Hour = (second / SECS_PER_HOUR) % 24;
- tp->Day = day + 1;
- tp->Month = month;
- tp->Year = year;
- }
- static struct inode *exfat_iget(struct super_block *sb, loff_t i_pos);
- #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36)
- static int exfat_generic_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg);
- #else
- static long exfat_generic_ioctl(struct file *filp, unsigned int cmd, unsigned long arg);
- #endif
- static int exfat_sync_inode(struct inode *inode);
- static struct inode *exfat_build_inode(struct super_block *sb, FILE_ID_T *fid, loff_t i_pos);
- static void exfat_detach(struct inode *inode);
- static void exfat_attach(struct inode *inode, loff_t i_pos);
- static inline unsigned long exfat_hash(loff_t i_pos);
- static int exfat_write_inode(struct inode *inode, struct writeback_control *wbc);
- static void exfat_write_super(struct super_block *sb);
- #if LINUX_VERSION_CODE < KERNEL_VERSION(3,5,0)
- #define GLOBAL_ROOT_UID (0)
- #define GLOBAL_ROOT_GID (0)
- static inline bool uid_eq(uid_t left, uid_t right)
- {
- return left == right;
- }
- static inline bool gid_eq(gid_t left, gid_t right)
- {
- return left == right;
- }
- static inline uid_t from_kuid_munged(struct user_namespace *to, uid_t kuid)
- {
- return kuid;
- }
- static inline gid_t from_kgid_munged(struct user_namespace *to, gid_t kgid)
- {
- return kgid;
- }
- static inline uid_t make_kuid(struct user_namespace *from, uid_t uid)
- {
- return uid;
- }
- static inline gid_t make_kgid(struct user_namespace *from, gid_t gid)
- {
- return gid;
- }
- #endif
- static void __lock_super(struct super_block *sb)
- {
- #if LINUX_VERSION_CODE < KERNEL_VERSION(3,7,0)
- lock_super(sb);
- #else
- struct exfat_sb_info *sbi = EXFAT_SB(sb);
- mutex_lock(&sbi->s_lock);
- #endif
- }
- static void __unlock_super(struct super_block *sb)
- {
- #if LINUX_VERSION_CODE < KERNEL_VERSION(3,7,0)
- unlock_super(sb);
- #else
- struct exfat_sb_info *sbi = EXFAT_SB(sb);
- mutex_unlock(&sbi->s_lock);
- #endif
- }
- static int __is_sb_dirty(struct super_block *sb)
- {
- #if LINUX_VERSION_CODE < KERNEL_VERSION(3,7,0)
- return sb->s_dirt;
- #else
- struct exfat_sb_info *sbi = EXFAT_SB(sb);
- return sbi->s_dirt;
- #endif
- }
- static void __set_sb_clean(struct super_block *sb)
- {
- #if LINUX_VERSION_CODE < KERNEL_VERSION(3,7,0)
- sb->s_dirt = 0;
- #else
- struct exfat_sb_info *sbi = EXFAT_SB(sb);
- sbi->s_dirt = 0;
- #endif
- }
- static void exfat_msg(struct super_block *sb, const char *level, const char *fmt, ...)
- {
- struct va_format vaf;
- va_list args;
- struct block_device *bdev = sb->s_bdev;
- dev_t bd_dev = bdev ? bdev->bd_dev : 0;
- va_start(args, fmt);
- vaf.fmt = fmt;
- vaf.va = &args;
- printk("%s[EXFAT] (%s[%d:%d]): %pV\n", level,
- sb->s_id, MAJOR(bd_dev), MINOR(bd_dev), &vaf);
- va_end(args);
- }
- static void exfat_mnt_msg(struct super_block *sb, int mount, int prev_err, const char *msg)
- {
- exfat_msg(sb, KERN_INFO, "%s %s",
- msg, prev_err ? "(with previous I/O errors)" : "");
- ST_LOG("[EXFAT] (%s[%d:%d]):%s %s",sb->s_id, MAJOR(sb->s_dev),MINOR(sb->s_dev),
- msg, prev_err ? "(with previous I/O errors)" : "");
- }
- static int __exfat_revalidate(struct dentry *dentry)
- {
- int ret = 1;
- spin_lock(&dentry->d_lock);
- if ((dentry->d_time != dentry->d_parent->d_inode->i_version) &&
- !__check_dstate_locked(dentry)) {
- ret = 0;
- }
- spin_unlock(&dentry->d_lock);
- return ret;
- }
- #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,00)
- static int exfat_revalidate(struct dentry *dentry, unsigned int flags)
- #else
- static int exfat_revalidate(struct dentry *dentry, struct nameidata *nd)
- #endif
- {
- #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,00)
- if (flags & LOOKUP_RCU)
- return -ECHILD;
- #elif LINUX_VERSION_CODE >= KERNEL_VERSION(3,0,00)
- if (nd && nd->flags & LOOKUP_RCU)
- return -ECHILD;
- #endif
- if (dentry->d_inode)
- return 1;
- return __exfat_revalidate(dentry);
- }
- #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,00)
- static int exfat_revalidate_ci(struct dentry *dentry, unsigned int flags)
- #else
- static int exfat_revalidate_ci(struct dentry *dentry, struct nameidata *nd)
- #endif
- {
- #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,00)
- if (flags & LOOKUP_RCU)
- return -ECHILD;
- #elif LINUX_VERSION_CODE >= KERNEL_VERSION(3,0,00)
- unsigned int flags;
- if (nd && nd->flags & LOOKUP_RCU)
- return -ECHILD;
- flags = nd ? nd->flags : 0;
- #else
- flags = nd ? nd->flags : 0;
- #endif
- if (dentry->d_inode)
- return 1;
- #if 0
- if (!flags)
- return 0;
- #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,0,00)
- if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET))
- return 0;
- #else
- if (!(nd->flags & (LOOKUP_CONTINUE | LOOKUP_PARENT))) {
- if (nd->flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET))
- return 0;
- }
- #endif
- #endif
- return __exfat_revalidate(dentry);
- }
- static unsigned int __exfat_striptail_len(unsigned int len, const char *name)
- {
- while (len && name[len - 1] == '.')
- len--;
- return len;
- }
- static unsigned int exfat_striptail_len(const struct qstr *qstr)
- {
- return __exfat_striptail_len(qstr->len, qstr->name);
- }
- #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,14,00)
- static int exfat_d_hash(const struct dentry *dentry, struct qstr *qstr)
- #else
- static int exfat_d_hash(const struct dentry *dentry, const struct inode *inode,
- struct qstr *qstr)
- #endif
- {
- qstr->hash = full_name_hash(qstr->name, exfat_striptail_len(qstr));
- return 0;
- }
- #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,14,00)
- static int exfat_d_hashi(const struct dentry *dentry, struct qstr *qstr)
- #else
- static int exfat_d_hashi(const struct dentry *dentry, const struct inode *inode,
- struct qstr *qstr)
- #endif
- {
- struct nls_table *t = EXFAT_SB(dentry->d_sb)->nls_io;
- const unsigned char *name;
- unsigned int len;
- unsigned long hash;
- name = qstr->name;
- len = exfat_striptail_len(qstr);
- hash = init_name_hash();
- while (len--)
- hash = partial_name_hash(nls_tolower(t, *name++), hash);
- qstr->hash = end_name_hash(hash);
- return 0;
- }
- #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,14,00)
- static int exfat_cmpi(const struct dentry *parent, const struct dentry *dentry,
- unsigned int len, const char *str, const struct qstr *name)
- #else
- static int exfat_cmpi(const struct dentry *parent, const struct inode *pinode,
- const struct dentry *dentry, const struct inode *inode,
- unsigned int len, const char *str, const struct qstr *name)
- #endif
- {
- struct nls_table *t = EXFAT_SB(parent->d_sb)->nls_io;
- unsigned int alen, blen;
- alen = exfat_striptail_len(name);
- blen = __exfat_striptail_len(len, str);
- if (alen == blen) {
- if (nls_strnicmp(t, name->name, str, alen) == 0)
- return 0;
- }
- return 1;
- }
- #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,14,00)
- static int exfat_cmp(const struct dentry *parent, const struct dentry *dentry,
- unsigned int len, const char *str, const struct qstr *name)
- #else
- static int exfat_cmp(const struct dentry *parent, const struct inode *pinode,
- const struct dentry *dentry, const struct inode *inode,
- unsigned int len, const char *str, const struct qstr *name)
- #endif
- {
- unsigned int alen, blen;
- alen = exfat_striptail_len(name);
- blen = __exfat_striptail_len(len, str);
- if (alen == blen) {
- if (strncmp(name->name, str, alen) == 0)
- return 0;
- }
- return 1;
- }
- static const struct dentry_operations exfat_ci_dentry_ops = {
- .d_revalidate = exfat_revalidate_ci,
- .d_hash = exfat_d_hashi,
- .d_compare = exfat_cmpi,
- };
- static const struct dentry_operations exfat_dentry_ops = {
- .d_revalidate = exfat_revalidate,
- .d_hash = exfat_d_hash,
- .d_compare = exfat_cmp,
- };
- #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,14,00)
- static int exfat_readdir(struct file *filp, struct dir_context *ctx)
- {
- struct inode *inode = filp->f_path.dentry->d_inode;
- struct super_block *sb = inode->i_sb;
- struct exfat_sb_info *sbi = EXFAT_SB(sb);
- FS_INFO_T *p_fs = &(sbi->fs_info);
- BD_INFO_T *p_bd = &(EXFAT_SB(sb)->bd_info);
- DIR_ENTRY_T de;
- unsigned long inum;
- loff_t cpos;
- int err = 0;
- __lock_super(sb);
- cpos = ctx->pos;
- if ((p_fs->vol_type == EXFAT) || (inode->i_ino == EXFAT_ROOT_INO)) {
- if (!dir_emit_dots(filp, ctx))
- goto out;
- if (ctx->pos == 2)
- cpos = 0;
- }
- if (cpos & (DENTRY_SIZE - 1)) {
- err = -ENOENT;
- goto out;
- }
- get_new:
- EXFAT_I(inode)->fid.size = i_size_read(inode);
- EXFAT_I(inode)->fid.rwoffset = cpos >> DENTRY_SIZE_BITS;
- err = FsReadDir(inode, &de);
- if (err) {
- if (err == FFS_MEDIAERR) {
- cpos += 1 << p_bd->sector_size_bits;
- cpos &= ~((1 << p_bd->sector_size_bits)-1);
- }
- err = -EIO;
- goto end_of_dir;
- }
- cpos = EXFAT_I(inode)->fid.rwoffset << DENTRY_SIZE_BITS;
- if (!de.Name[0])
- goto end_of_dir;
- if (!memcmp(de.ShortName, DOS_CUR_DIR_NAME, DOS_NAME_LENGTH)) {
- inum = inode->i_ino;
- } else if (!memcmp(de.ShortName, DOS_PAR_DIR_NAME, DOS_NAME_LENGTH)) {
- inum = parent_ino(filp->f_path.dentry);
- } else {
- loff_t i_pos = ((loff_t) EXFAT_I(inode)->fid.start_clu << 32) |
- ((EXFAT_I(inode)->fid.rwoffset-1) & 0xffffffff);
- struct inode *tmp = exfat_iget(sb, i_pos);
- if (tmp) {
- inum = tmp->i_ino;
- iput(tmp);
- } else {
- inum = iunique(sb, EXFAT_ROOT_INO);
- }
- }
- __unlock_super(sb);
- if (!dir_emit(ctx, de.Name, strlen(de.Name), inum,
- (de.Attr & ATTR_SUBDIR) ? DT_DIR : DT_REG))
- goto out_nolock;
- __lock_super(sb);
- ctx->pos = cpos;
- goto get_new;
- end_of_dir:
- ctx->pos = cpos;
- out:
- __unlock_super(sb);
- out_nolock:
- return err;
- }
- #else
- static int exfat_readdir(struct file *filp, void *dirent, filldir_t filldir)
- {
- struct inode *inode = filp->f_path.dentry->d_inode;
- struct super_block *sb = inode->i_sb;
- struct exfat_sb_info *sbi = EXFAT_SB(sb);
- FS_INFO_T *p_fs = &(sbi->fs_info);
- BD_INFO_T *p_bd = &(EXFAT_SB(sb)->bd_info);
- DIR_ENTRY_T de;
- unsigned long inum;
- loff_t cpos;
- int err = 0;
- __lock_super(sb);
- cpos = filp->f_pos;
- if ((p_fs->vol_type == EXFAT) || (inode->i_ino == EXFAT_ROOT_INO)) {
- while (cpos < 2) {
- if (inode->i_ino == EXFAT_ROOT_INO)
- inum = EXFAT_ROOT_INO;
- else if (cpos == 0)
- inum = inode->i_ino;
- else
- inum = parent_ino(filp->f_path.dentry);
- if (filldir(dirent, "..", cpos+1, cpos, inum, DT_DIR) < 0)
- goto out;
- cpos++;
- filp->f_pos++;
- }
- if (cpos == 2) {
- cpos = 0;
- }
- }
- if (cpos & (DENTRY_SIZE - 1)) {
- err = -ENOENT;
- goto out;
- }
- get_new:
- EXFAT_I(inode)->fid.size = i_size_read(inode);
- EXFAT_I(inode)->fid.rwoffset = cpos >> DENTRY_SIZE_BITS;
- err = FsReadDir(inode, &de);
- if (err) {
- if (err == FFS_MEDIAERR) {
- cpos += 1 << p_bd->sector_size_bits;
- cpos &= ~((1 << p_bd->sector_size_bits)-1);
- }
- err = -EIO;
- goto end_of_dir;
- }
- cpos = EXFAT_I(inode)->fid.rwoffset << DENTRY_SIZE_BITS;
- if (!de.Name[0])
- goto end_of_dir;
- if (!memcmp(de.ShortName, DOS_CUR_DIR_NAME, DOS_NAME_LENGTH)) {
- inum = inode->i_ino;
- } else if (!memcmp(de.ShortName, DOS_PAR_DIR_NAME, DOS_NAME_LENGTH)) {
- inum = parent_ino(filp->f_path.dentry);
- } else {
- loff_t i_pos = ((loff_t) EXFAT_I(inode)->fid.start_clu << 32) |
- ((EXFAT_I(inode)->fid.rwoffset-1) & 0xffffffff);
- struct inode *tmp = exfat_iget(sb, i_pos);
- if (tmp) {
- inum = tmp->i_ino;
- iput(tmp);
- } else {
- inum = iunique(sb, EXFAT_ROOT_INO);
- }
- }
- __unlock_super(sb);
- if (filldir(dirent, de.Name, strlen(de.Name), cpos, inum,
- (de.Attr & ATTR_SUBDIR) ? DT_DIR : DT_REG) < 0)
- goto out_nolock;
- __lock_super(sb);
- filp->f_pos = cpos;
- goto get_new;
- end_of_dir:
- filp->f_pos = cpos;
- out:
- __unlock_super(sb);
- out_nolock:
- return err;
- }
- #endif
- static int exfat_ioctl_volume_id(struct inode *dir)
- {
- struct super_block *sb = dir->i_sb;
- struct exfat_sb_info *sbi = EXFAT_SB(sb);
- FS_INFO_T *p_fs = &(sbi->fs_info);
- return p_fs->vol_id;
- }
- #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36)
- static int exfat_generic_ioctl(struct inode *inode, struct file *filp,
- unsigned int cmd, unsigned long arg)
- #else
- static long exfat_generic_ioctl(struct file *filp,
- unsigned int cmd, unsigned long arg)
- #endif
- {
- #if EXFAT_CONFIG_KERNEL_DEBUG
- #if !(LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36))
- struct inode *inode = filp->f_dentry->d_inode;
- #endif
- unsigned int flags;
- #endif
- switch (cmd) {
- case EXFAT_IOCTL_GET_VOLUME_ID:
- return exfat_ioctl_volume_id(inode);
- #if EXFAT_CONFIG_KERNEL_DEBUG
- case EXFAT_IOC_GET_DEBUGFLAGS: {
- struct super_block *sb = inode->i_sb;
- struct exfat_sb_info *sbi = EXFAT_SB(sb);
- flags = sbi->debug_flags;
- return put_user(flags, (int __user *)arg);
- }
- case EXFAT_IOC_SET_DEBUGFLAGS: {
- struct super_block *sb = inode->i_sb;
- struct exfat_sb_info *sbi = EXFAT_SB(sb);
- if (!capable(CAP_SYS_ADMIN))
- return -EPERM;
- if (get_user(flags, (int __user *) arg))
- return -EFAULT;
- __lock_super(sb);
- sbi->debug_flags = flags;
- __unlock_super(sb);
- return 0;
- }
- #endif
- default:
- return -ENOTTY;
- }
- }
- #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,1,0)
- static int exfat_file_fsync(struct file *filp, loff_t start, loff_t end, int datasync)
- #else
- static int exfat_file_fsync(struct file *filp, int datasync)
- #endif
- {
- struct inode *inode = filp->f_mapping->host;
- struct super_block *sb = inode->i_sb;
- int res, err;
- #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,1,0)
- res = generic_file_fsync(filp, start, end, datasync);
- #else
- res = generic_file_fsync(filp, datasync);
- #endif
- err = FsSyncVol(sb, 1);
- return res ? res : err;
- }
- const struct file_operations exfat_dir_operations = {
- .llseek = generic_file_llseek,
- .read = generic_read_dir,
- #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,14,00)
- .iterate = exfat_readdir,
- #else
- .readdir = exfat_readdir,
- #endif
- #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36)
- .ioctl = exfat_generic_ioctl,
- #else
- .unlocked_ioctl = exfat_generic_ioctl,
- #endif
- .fsync = exfat_file_fsync,
- };
- #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,00)
- static int exfat_create(struct inode *dir, struct dentry *dentry, umode_t mode,
- bool excl)
- #elif LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,00)
- static int exfat_create(struct inode *dir, struct dentry *dentry, umode_t mode,
- struct nameidata *nd)
- #else
- static int exfat_create(struct inode *dir, struct dentry *dentry, int mode,
- struct nameidata *nd)
- #endif
- {
- struct super_block *sb = dir->i_sb;
- struct inode *inode;
- struct timespec ts;
- FILE_ID_T fid;
- loff_t i_pos;
- int err;
- __lock_super(sb);
- PRINTK("exfat_create entered\n");
- ts = CURRENT_TIME_SEC;
- err = FsCreateFile(dir, (UINT8 *) dentry->d_name.name, FM_REGULAR, &fid);
- if (err) {
- if (err == FFS_INVALIDPATH)
- err = -EINVAL;
- else if (err == FFS_FILEEXIST)
- err = -EEXIST;
- else if (err == FFS_FULL)
- err = -ENOSPC;
- else if (err == FFS_NAMETOOLONG)
- err = -ENAMETOOLONG;
- else
- err = -EIO;
- goto out;
- }
- __lock_d_revalidate(dentry);
- dir->i_version++;
- dir->i_ctime = dir->i_mtime = dir->i_atime = ts;
- if (IS_DIRSYNC(dir))
- (void) exfat_sync_inode(dir);
- else
- mark_inode_dirty(dir);
- i_pos = ((loff_t) fid.dir.dir << 32) | (fid.entry & 0xffffffff);
- inode = exfat_build_inode(sb, &fid, i_pos);
- if (IS_ERR(inode)) {
- err = PTR_ERR(inode);
- goto out;
- }
- inode->i_version++;
- inode->i_mtime = inode->i_atime = inode->i_ctime = ts;
- d_instantiate(dentry, inode);
- out:
- __unlock_d_revalidate(dentry);
- __unlock_super(sb);
- PRINTK("exfat_create exited\n");
- return err;
- }
- static int exfat_find(struct inode *dir, struct qstr *qname,
- FILE_ID_T *fid)
- {
- int err;
- if (qname->len == 0)
- return -ENOENT;
- err = FsLookupFile(dir, (UINT8 *) qname->name, fid);
- if (err)
- return -ENOENT;
- return 0;
- }
- static int exfat_d_anon_disconn(struct dentry *dentry)
- {
- return IS_ROOT(dentry) && (dentry->d_flags & DCACHE_DISCONNECTED);
- }
- #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,00)
- static struct dentry *exfat_lookup(struct inode *dir, struct dentry *dentry,
- unsigned int flags)
- #else
- static struct dentry *exfat_lookup(struct inode *dir, struct dentry *dentry,
- struct nameidata *nd)
- #endif
- {
- struct super_block *sb = dir->i_sb;
- struct inode *inode;
- struct dentry *alias;
- int err;
- FILE_ID_T fid;
- loff_t i_pos;
- UINT64 ret;
- mode_t i_mode;
- __lock_super(sb);
- PRINTK("exfat_lookup entered\n");
- err = exfat_find(dir, &dentry->d_name, &fid);
- if (err) {
- if (err == -ENOENT) {
- inode = NULL;
- goto out;
- }
- goto error;
- }
- i_pos = ((loff_t) fid.dir.dir << 32) | (fid.entry & 0xffffffff);
- inode = exfat_build_inode(sb, &fid, i_pos);
- if (IS_ERR(inode)) {
- err = PTR_ERR(inode);
- goto error;
- }
- i_mode = inode->i_mode;
- if (S_ISLNK(i_mode)) {
- EXFAT_I(inode)->target = MALLOC(i_size_read(inode)+1);
- if (!EXFAT_I(inode)->target) {
- err = -ENOMEM;
- goto error;
- }
- FsReadFile(dir, &fid, EXFAT_I(inode)->target, i_size_read(inode), &ret);
- *(EXFAT_I(inode)->target + i_size_read(inode)) = '\0';
- }
- alias = d_find_alias(inode);
- /*
- * Checking "alias->d_parent == dentry->d_parent" to make sure
- * FS is not corrupted (especially double linked dir).
- */
- if (alias && alias->d_parent == dentry->d_parent &&
- !exfat_d_anon_disconn(alias)) {
- /*
- * This inode has non anonymous-DCACHE_DISCONNECTED
- * dentry. This means, the user did ->lookup() by an
- * another name (longname vs 8.3 alias of it) in past.
- *
- * Switch to new one for reason of locality if possible.
- */
- CHECK_ERR(d_unhashed(alias));
- if (!S_ISDIR(i_mode))
- d_move(alias, dentry);
- iput(inode);
- __unlock_super(sb);
- PRINTK("exfat_lookup exited 1\n");
- return alias;
- } else {
- dput(alias);
- }
- out:
- dentry->d_time = dir->i_version;
- __unlock_super(sb);
- #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,38)
- dentry->d_op = sb->s_root->d_op;
- #endif
- dentry = d_splice_alias(inode, dentry);
- PRINTK("exfat_lookup exited 2\n");
- return dentry;
- error:
- __unlock_super(sb);
- PRINTK("exfat_lookup exited 3\n");
- return ERR_PTR(err);
- }
- static int exfat_unlink(struct inode *dir, struct dentry *dentry)
- {
- struct inode *inode = dentry->d_inode;
- struct super_block *sb = dir->i_sb;
- struct timespec ts;
- int err;
- __lock_super(sb);
- PRINTK("exfat_unlink entered\n");
- ts = CURRENT_TIME_SEC;
- EXFAT_I(inode)->fid.size = i_size_read(inode);
- if (EXFAT_I(inode)->fid.dir.dir == DIR_DELETED) {
- err = -ENOENT;
- goto out;
- }
- err = FsRemoveEntry(dir, &(EXFAT_I(inode)->fid));
- if (err) {
- if (err == FFS_PERMISSIONERR)
- err = -EPERM;
- else
- err = -EIO;
- goto out;
- }
- __lock_d_revalidate(dentry);
- dir->i_version++;
- dir->i_mtime = dir->i_atime = ts;
- if (IS_DIRSYNC(dir))
- (void) exfat_sync_inode(dir);
- else
- mark_inode_dirty(dir);
- clear_nlink(inode);
- inode->i_mtime = inode->i_atime = ts;
- exfat_detach(inode);
- dentry->d_time = dir->i_version;
- out:
- __unlock_d_revalidate(dentry);
- __unlock_super(sb);
- PRINTK("exfat_unlink exited\n");
- return err;
- }
- static int exfat_symlink(struct inode *dir, struct dentry *dentry, const char *target)
- {
- struct super_block *sb = dir->i_sb;
- struct inode *inode;
- struct timespec ts;
- FILE_ID_T fid;
- loff_t i_pos;
- int err;
- UINT64 len = (UINT64) strlen(target);
- UINT64 ret;
- __lock_super(sb);
- PRINTK("exfat_symlink entered\n");
- ts = CURRENT_TIME_SEC;
- err = FsCreateFile(dir, (UINT8 *) dentry->d_name.name, FM_SYMLINK, &fid);
- if (err) {
- if (err == FFS_INVALIDPATH)
- err = -EINVAL;
- else if (err == FFS_FILEEXIST)
- err = -EEXIST;
- else if (err == FFS_FULL)
- err = -ENOSPC;
- else
- err = -EIO;
- goto out;
- }
- err = FsWriteFile(dir, &fid, (char *) target, len, &ret);
- if (err) {
- FsRemoveFile(dir, &fid);
- if (err == FFS_FULL)
- err = -ENOSPC;
- else
- err = -EIO;
- goto out;
- }
- __lock_d_revalidate(dentry);
- dir->i_version++;
- dir->i_ctime = dir->i_mtime = dir->i_atime = ts;
- if (IS_DIRSYNC(dir))
- (void) exfat_sync_inode(dir);
- else
- mark_inode_dirty(dir);
- i_pos = ((loff_t) fid.dir.dir << 32) | (fid.entry & 0xffffffff);
- inode = exfat_build_inode(sb, &fid, i_pos);
- if (IS_ERR(inode)) {
- err = PTR_ERR(inode);
- goto out;
- }
- inode->i_version++;
- inode->i_mtime = inode->i_atime = inode->i_ctime = ts;
- EXFAT_I(inode)->target = MALLOC(len+1);
- if (!EXFAT_I(inode)->target) {
- err = -ENOMEM;
- goto out;
- }
- MEMCPY(EXFAT_I(inode)->target, target, len+1);
- dentry->d_time = dentry->d_parent->d_inode->i_version;
- d_instantiate(dentry, inode);
- out:
- __unlock_d_revalidate(dentry);
- __unlock_super(sb);
- PRINTK("exfat_symlink exited\n");
- return err;
- }
- #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,00)
- static int exfat_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
- #else
- static int exfat_mkdir(struct inode *dir, struct dentry *dentry, int mode)
- #endif
- {
- struct super_block *sb = dir->i_sb;
- struct inode *inode;
- struct timespec ts;
- FILE_ID_T fid;
- loff_t i_pos;
- int err;
- __lock_super(sb);
- PRINTK("exfat_mkdir entered\n");
- ts = CURRENT_TIME_SEC;
- err = FsCreateDir(dir, (UINT8 *) dentry->d_name.name, &fid);
- if (err) {
- if (err == FFS_INVALIDPATH)
- err = -EINVAL;
- else if (err == FFS_FILEEXIST)
- err = -EEXIST;
- else if (err == FFS_FULL)
- err = -ENOSPC;
- else if (err == FFS_NAMETOOLONG)
- err = -ENAMETOOLONG;
- else
- err = -EIO;
- goto out;
- }
- __lock_d_revalidate(dentry);
- dir->i_version++;
- dir->i_ctime = dir->i_mtime = dir->i_atime = ts;
- if (IS_DIRSYNC(dir))
- (void) exfat_sync_inode(dir);
- else
- mark_inode_dirty(dir);
- inc_nlink(dir);
- i_pos = ((loff_t) fid.dir.dir << 32) | (fid.entry & 0xffffffff);
- inode = exfat_build_inode(sb, &fid, i_pos);
- if (IS_ERR(inode)) {
- err = PTR_ERR(inode);
- goto out;
- }
- inode->i_version++;
- inode->i_mtime = inode->i_atime = inode->i_ctime = ts;
- d_instantiate(dentry, inode);
- out:
- __unlock_d_revalidate(dentry);
- __unlock_super(sb);
- PRINTK("exfat_mkdir exited\n");
- return err;
- }
- static int exfat_rmdir(struct inode *dir, struct dentry *dentry)
- {
- struct inode *inode = dentry->d_inode;
- struct super_block *sb = dir->i_sb;
- struct timespec ts;
- int err;
- __lock_super(sb);
- PRINTK("exfat_rmdir entered\n");
- ts = CURRENT_TIME_SEC;
- EXFAT_I(inode)->fid.size = i_size_read(inode);
- if (EXFAT_I(inode)->fid.dir.dir == DIR_DELETED) {
- err = -ENOENT;
- goto out;
- }
- err = FsRemoveDir(dir, &(EXFAT_I(inode)->fid));
- if (err) {
- if (err == FFS_INVALIDPATH)
- err = -EINVAL;
- else if (err == FFS_FILEEXIST)
- err = -ENOTEMPTY;
- else if (err == FFS_NOTFOUND)
- err = -ENOENT;
- else if (err == FFS_DIRBUSY)
- err = -EBUSY;
- else
- err = -EIO;
- goto out;
- }
- __lock_d_revalidate(dentry);
- dir->i_version++;
- dir->i_mtime = dir->i_atime = ts;
- if (IS_DIRSYNC(dir))
- (void) exfat_sync_inode(dir);
- else
- mark_inode_dirty(dir);
- drop_nlink(dir);
- clear_nlink(inode);
- inode->i_mtime = inode->i_atime = ts;
- exfat_detach(inode);
- dentry->d_time = dir->i_version;
- remove_inode_hash(inode);
- out:
- __unlock_d_revalidate(dentry);
- __unlock_super(sb);
- PRINTK("exfat_rmdir exited\n");
- return err;
- }
- static int exfat_rename(struct inode *old_dir, struct dentry *old_dentry,
- struct inode *new_dir, struct dentry *new_dentry)
- {
- struct inode *old_inode, *new_inode;
- struct super_block *sb = old_dir->i_sb;
- struct timespec ts;
- loff_t i_pos;
- int err;
- __lock_super(sb);
- PRINTK("exfat_rename entered\n");
- old_inode = old_dentry->d_inode;
- new_inode = new_dentry->d_inode;
- ts = CURRENT_TIME_SEC;
- EXFAT_I(old_inode)->fid.size = i_size_read(old_inode);
- if (EXFAT_I(old_inode)->fid.dir.dir == DIR_DELETED) {
- err = -ENOENT;
- goto out;
- }
- err = FsMoveFile(old_dir, &(EXFAT_I(old_inode)->fid), new_dir, new_dentry);
- if (err) {
- if (err == FFS_PERMISSIONERR)
- err = -EPERM;
- else if (err == FFS_INVALIDPATH)
- err = -EINVAL;
- else if (err == FFS_FILEEXIST)
- err = -EEXIST;
- else if (err == FFS_NOTFOUND)
- err = -ENOENT;
- else if (err == FFS_FULL)
- err = -ENOSPC;
- else
- err = -EIO;
- goto out;
- }
- __lock_d_revalidate(old_dentry);
- __lock_d_revalidate(new_dentry);
- new_dir->i_version++;
- new_dir->i_ctime = new_dir->i_mtime = new_dir->i_atime = ts;
- if (IS_DIRSYNC(new_dir))
- (void) exfat_sync_inode(new_dir);
- else
- mark_inode_dirty(new_dir);
- i_pos = ((loff_t) EXFAT_I(old_inode)->fid.dir.dir << 32) |
- (EXFAT_I(old_inode)->fid.entry & 0xffffffff);
- exfat_detach(old_inode);
- exfat_attach(old_inode, i_pos);
- if (IS_DIRSYNC(new_dir))
- (void) exfat_sync_inode(old_inode);
- else
- mark_inode_dirty(old_inode);
- if ((S_ISDIR(old_inode->i_mode)) && (old_dir != new_dir)) {
- drop_nlink(old_dir);
- if (!new_inode) inc_nlink(new_dir);
- }
- old_dir->i_version++;
- old_dir->i_ctime = old_dir->i_mtime = ts;
- if (IS_DIRSYNC(old_dir))
- (void) exfat_sync_inode(old_dir);
- else
- mark_inode_dirty(old_dir);
- if (new_inode) {
- exfat_detach(new_inode);
- if (EXFAT_I(new_inode)->fid.dir.dir != DIR_DELETED) {
- drop_nlink(new_inode);
- if (S_ISDIR(new_inode->i_mode))
- drop_nlink(new_inode);
- }
- new_inode->i_ctime = ts;
- }
- out:
- __unlock_d_revalidate(old_dentry);
- __unlock_d_revalidate(new_dentry);
- __unlock_super(sb);
- PRINTK("exfat_rename exited\n");
- return err;
- }
- static int exfat_cont_expand(struct inode *inode, loff_t size)
- {
- struct address_space *mapping = inode->i_mapping;
- loff_t start = i_size_read(inode), count = size - i_size_read(inode);
- int err, err2;
- if ((err = generic_cont_expand_simple(inode, size)) != 0)
- return err;
- inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC;
- mark_inode_dirty(inode);
- if (IS_SYNC(inode)) {
- err = filemap_fdatawrite_range(mapping, start, start + count - 1);
- err2 = sync_mapping_buffers(mapping);
- err = (err)?(err):(err2);
- err2 = write_inode_now(inode, 1);
- err = (err)?(err):(err2);
- if (!err) {
- err = filemap_fdatawait_range(mapping, start, start + count - 1);
- }
- }
- return err;
- }
- static int exfat_allow_set_time(struct exfat_sb_info *sbi, struct inode *inode)
- {
- mode_t allow_utime = sbi->options.allow_utime;
- if (!uid_eq(current_fsuid(), inode->i_uid)) {
- if (in_group_p(inode->i_gid))
- allow_utime >>= 3;
- if (allow_utime & MAY_WRITE)
- return 1;
- }
- return 0;
- }
- static int exfat_sanitize_mode(const struct exfat_sb_info *sbi,
- struct inode *inode, umode_t *mode_ptr)
- {
- mode_t i_mode, mask, perm;
- i_mode = inode->i_mode;
- if (S_ISREG(i_mode) || S_ISLNK(i_mode))
- mask = sbi->options.fs_fmask;
- else
- mask = sbi->options.fs_dmask;
- perm = *mode_ptr & ~(S_IFMT | mask);
- if ((perm & (S_IRUGO | S_IXUGO)) != (i_mode & (S_IRUGO|S_IXUGO)))
- return -EPERM;
- if (exfat_mode_can_hold_ro(inode)) {
- if ((perm & S_IWUGO) && ((perm & S_IWUGO) != (S_IWUGO & ~mask)))
- return -EPERM;
- } else {
- if ((perm & S_IWUGO) != (S_IWUGO & ~mask))
- return -EPERM;
- }
- *mode_ptr &= S_IFMT | perm;
- return 0;
- }
- static int exfat_setattr(struct dentry *dentry, struct iattr *attr)
- {
- struct exfat_sb_info *sbi = EXFAT_SB(dentry->d_sb);
- struct inode *inode = dentry->d_inode;
- unsigned int ia_valid;
- int error;
- loff_t old_size;
- PRINTK("exfat_setattr entered\n");
- if ((attr->ia_valid & ATTR_SIZE)
- && (attr->ia_size > i_size_read(inode))) {
- error = exfat_cont_expand(inode, attr->ia_size);
- if (error || attr->ia_valid == ATTR_SIZE)
- return error;
- attr->ia_valid &= ~ATTR_SIZE;
- }
- ia_valid = attr->ia_valid;
- if ((ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET))
- && exfat_allow_set_time(sbi, inode)) {
- attr->ia_valid &= ~(ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET);
- }
- error = inode_change_ok(inode, attr);
- attr->ia_valid = ia_valid;
- if (error) {
- return error;
- }
- if (((attr->ia_valid & ATTR_UID) &&
- (!uid_eq(attr->ia_uid, sbi->options.fs_uid))) ||
- ((attr->ia_valid & ATTR_GID) &&
- (!gid_eq(attr->ia_gid, sbi->options.fs_gid))) ||
- ((attr->ia_valid & ATTR_MODE) &&
- (attr->ia_mode & ~(S_IFREG | S_IFLNK | S_IFDIR | S_IRWXUGO)))) {
- return -EPERM;
- }
- if (attr->ia_valid & ATTR_MODE) {
- if (exfat_sanitize_mode(sbi, inode, &attr->ia_mode) < 0)
- attr->ia_valid &= ~ATTR_MODE;
- }
- EXFAT_I(inode)->fid.size = i_size_read(inode);
- #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36)
- if (attr->ia_valid)
- error = inode_setattr(inode, attr);
- #else
- if (attr->ia_valid & ATTR_SIZE) {
- old_size = i_size_read(inode);
- #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,00)
- down_write(&EXFAT_I(inode)->truncate_lock);
- truncate_setsize(inode, attr->ia_size);
- _exfat_truncate(inode, old_size);
- up_write(&EXFAT_I(inode)->truncate_lock);
- #else
- truncate_setsize(inode, attr->ia_size);
- _exfat_truncate(inode, old_size);
- #endif
- }
- setattr_copy(inode, attr);
- mark_inode_dirty(inode);
- #endif
- PRINTK("exfat_setattr exited\n");
- return error;
- }
- static int exfat_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
- {
- struct inode *inode = dentry->d_inode;
- PRINTK("exfat_getattr entered\n");
- generic_fillattr(inode, stat);
- stat->blksize = EXFAT_SB(inode->i_sb)->fs_info.cluster_size;
- PRINTK("exfat_getattr exited\n");
- return 0;
- }
- const struct inode_operations exfat_dir_inode_operations = {
- .create = exfat_create,
- .lookup = exfat_lookup,
- .unlink = exfat_unlink,
- .symlink = exfat_symlink,
- .mkdir = exfat_mkdir,
- .rmdir = exfat_rmdir,
- .rename = exfat_rename,
- .setattr = exfat_setattr,
- .getattr = exfat_getattr,
- #ifdef CONFIG_EXFAT_VIRTUAL_XATTR
- .setxattr = exfat_setxattr,
- .getxattr = exfat_getxattr,
- .listxattr = exfat_listxattr,
- .removexattr = exfat_removexattr,
- #endif
- };
- static void *exfat_follow_link(struct dentry *dentry, struct nameidata *nd)
- {
- struct exfat_inode_info *ei = EXFAT_I(dentry->d_inode);
- nd_set_link(nd, (char *)(ei->target));
- return NULL;
- }
- const struct inode_operations exfat_symlink_inode_operations = {
- .readlink = generic_readlink,
- .follow_link = exfat_follow_link,
- #ifdef CONFIG_EXFAT_VIRTUAL_XATTR
- .setxattr = exfat_setxattr,
- .getxattr = exfat_getxattr,
- .listxattr = exfat_listxattr,
- .removexattr = exfat_removexattr,
- #endif
- };
- static int exfat_file_release(struct inode *inode, struct file *filp)
- {
- struct super_block *sb = inode->i_sb;
- EXFAT_I(inode)->fid.size = i_size_read(inode);
- FsSyncVol(sb, 0);
- return 0;
- }
- const struct file_operations exfat_file_operations = {
- .llseek = generic_file_llseek,
- #if LINUX_VERSION_CODE < KERNEL_VERSION(3,16,0)
- .read = do_sync_read,
- .write = do_sync_write,
- .aio_read = generic_file_aio_read,
- .aio_write = generic_file_aio_write,
- #else
- .read = new_sync_read,
- .write = new_sync_write,
- .read_iter = generic_file_read_iter,
- .write_iter = generic_file_write_iter,
- #endif
- .mmap = generic_file_mmap,
- .release = exfat_file_release,
- #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36)
- .ioctl = exfat_generic_ioctl,
- .fsync = exfat_file_fsync,
- #else
- .unlocked_ioctl = exfat_generic_ioctl,
- .fsync = generic_file_fsync,
- #endif
- .splice_read = generic_file_splice_read,
- };
- static void _exfat_truncate(struct inode *inode, loff_t old_size)
- {
- struct super_block *sb = inode->i_sb;
- struct exfat_sb_info *sbi = EXFAT_SB(sb);
- FS_INFO_T *p_fs = &(sbi->fs_info);
- int err;
- __lock_super(sb);
- if (EXFAT_I(inode)->mmu_private > i_size_read(inode))
- EXFAT_I(inode)->mmu_private = i_size_read(inode);
- if (EXFAT_I(inode)->fid.start_clu == 0) goto out;
- err = FsTruncateFile(inode, old_size, i_size_read(inode));
- if (err) goto out;
- inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC;
- if (IS_DIRSYNC(inode))
- (void) exfat_sync_inode(inode);
- else
- mark_inode_dirty(inode);
- inode->i_blocks = ((i_size_read(inode) + (p_fs->cluster_size - 1))
- & ~((loff_t)p_fs->cluster_size - 1)) >> inode->i_blkbits;
- out:
- __unlock_super(sb);
- }
- #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,36)
- static void exfat_truncate(struct inode *inode)
- {
- _exfat_truncate(inode, i_size_read(inode));
- }
- #endif
- const struct inode_operations exfat_file_inode_operations = {
- #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,36)
- .truncate = exfat_truncate,
- #endif
- .setattr = exfat_setattr,
- .getattr = exfat_getattr,
- #ifdef CONFIG_EXFAT_VIRTUAL_XATTR
- .setxattr = exfat_setxattr,
- .getxattr = exfat_getxattr,
- .listxattr = exfat_listxattr,
- .removexattr = exfat_removexattr,
- #endif
- };
- static int exfat_bmap(struct inode *inode, sector_t sector, sector_t *phys,
- unsigned long *mapped_blocks, int *create)
- {
- struct super_block *sb = inode->i_sb;
- struct exfat_sb_info *sbi = EXFAT_SB(sb);
- FS_INFO_T *p_fs = &(sbi->fs_info);
- BD_INFO_T *p_bd = &(sbi->bd_info);
- const unsigned long blocksize = sb->s_blocksize;
- const unsigned char blocksize_bits = sb->s_blocksize_bits;
- sector_t last_block;
- int err, clu_offset, sec_offset;
- unsigned int cluster;
- *phys = 0;
- *mapped_blocks = 0;
- if ((p_fs->vol_type == FAT12) || (p_fs->vol_type == FAT16)) {
- if (inode->i_ino == EXFAT_ROOT_INO) {
- if (sector < (p_fs->dentries_in_root >> (p_bd->sector_size_bits-DENTRY_SIZE_BITS))) {
- *phys = sector + p_fs->root_start_sector;
- *mapped_blocks = 1;
- }
- return 0;
- }
- }
- last_block = (i_size_read(inode) + (blocksize - 1)) >> blocksize_bits;
- if (sector >= last_block) {
- if (*create == 0) return 0;
- } else {
- *create = 0;
- }
- clu_offset = sector >> p_fs->sectors_per_clu_bits;
- sec_offset = sector & (p_fs->sectors_per_clu - 1);
- EXFAT_I(inode)->fid.size = i_size_read(inode);
- err = FsMapCluster(inode, clu_offset, &cluster);
- if (err) {
- if (err == FFS_FULL)
- return -ENOSPC;
- else
- return -EIO;
- } else if (cluster != CLUSTER_32(~0)) {
- *phys = START_SECTOR(cluster) + sec_offset;
- *mapped_blocks = p_fs->sectors_per_clu - sec_offset;
- }
- return 0;
- }
- static int exfat_get_block(struct inode *inode, sector_t iblock,
- struct buffer_head *bh_result, int create)
- {
- struct super_block *sb = inode->i_sb;
- unsigned long max_blocks = bh_result->b_size >> inode->i_blkbits;
- int err;
- unsigned long mapped_blocks;
- sector_t phys;
- __lock_super(sb);
- err = exfat_bmap(inode, iblock, &phys, &mapped_blocks, &create);
- if (err) {
- __unlock_super(sb);
- return err;
- }
- if (phys) {
- max_blocks = min(mapped_blocks, max_blocks);
- if (create) {
- EXFAT_I(inode)->mmu_private += max_blocks << sb->s_blocksize_bits;
- set_buffer_new(bh_result);
- }
- map_bh(bh_result, sb, phys);
- }
- bh_result->b_size = max_blocks << sb->s_blocksize_bits;
- __unlock_super(sb);
- return 0;
- }
- static int exfat_readpage(struct file *file, struct page *page)
- {
- int ret;
- ret = mpage_readpage(page, exfat_get_block);
- return ret;
- }
- static int exfat_readpages(struct file *file, struct address_space *mapping,
- struct list_head *pages, unsigned nr_pages)
- {
- int ret;
- ret = mpage_readpages(mapping, pages, nr_pages, exfat_get_block);
- return ret;
- }
- static int exfat_writepage(struct page *page, struct writeback_control *wbc)
- {
- int ret;
- ret = block_write_full_page(page, exfat_get_block, wbc);
- return ret;
- }
- static int exfat_writepages(struct address_space *mapping,
- struct writeback_control *wbc)
- {
- int ret;
- ret = mpage_writepages(mapping, wbc, exfat_get_block);
- return ret;
- }
- #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,34)
- static void exfat_write_failed(struct address_space *mapping, loff_t to)
- {
- struct inode *inode = mapping->host;
- if (to > i_size_read(inode)) {
- #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,14,00)
- truncate_pagecache(inode, i_size_read(inode));
- #else
- truncate_pagecache(inode, to, i_size_read(inode));
- #endif
- EXFAT_I(inode)->fid.size = i_size_read(inode);
- _exfat_truncate(inode, i_size_read(inode));
- }
- }
- #endif
- static int exfat_write_begin(struct file *file, struct address_space *mapping,
- loff_t pos, unsigned len, unsigned flags,
- struct page **pagep, void **fsdata)
- {
- int ret;
- *pagep = NULL;
- ret = cont_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
- exfat_get_block,
- &EXFAT_I(mapping->host)->mmu_private);
- #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,34)
- if (ret < 0)
- exfat_write_failed(mapping, pos+len);
- #endif
- return ret;
- }
- static int exfat_write_end(struct file *file, struct address_space *mapping,
- loff_t pos, unsigned len, unsigned copied,
- struct page *pagep, void *fsdata)
- {
- struct inode *inode = mapping->host;
- FILE_ID_T *fid = &(EXFAT_I(inode)->fid);
- int err;
- err = generic_write_end(file, mapping, pos, len, copied, pagep, fsdata);
- #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,34)
- if (err < len)
- exfat_write_failed(mapping, pos+len);
- #endif
- if (!(err < 0) && !(fid->attr & ATTR_ARCHIVE)) {
- inode->i_mtime = inode->i_ctime = CURRENT_TIME_SEC;
- fid->attr |= ATTR_ARCHIVE;
- mark_inode_dirty(inode);
- }
- return err;
- }
- #if LINUX_VERSION_CODE < KERNEL_VERSION(3,16,0)
- static ssize_t exfat_direct_IO(int rw, struct kiocb *iocb,
- const struct iovec *iov,
- loff_t offset, unsigned long nr_segs)
- {
- struct inode *inode = iocb->ki_filp->f_mapping->host;
- #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,34)
- struct address_space *mapping = iocb->ki_filp->f_mapping;
- #endif
- ssize_t ret;
- if (rw == WRITE) {
- if (EXFAT_I(inode)->mmu_private < (offset + iov_length(iov, nr_segs)))
- return 0;
- }
- #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,00)
- ret = blockdev_direct_IO(rw, iocb, inode, iov,
- offset, nr_segs, exfat_get_block);
- #else
- ret = blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
- offset, nr_segs, exfat_get_block, NULL);
- #endif
- #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,34)
- if ((ret < 0) && (rw & WRITE))
- exfat_write_failed(mapping, offset+iov_length(iov, nr_segs));
- #endif
- return ret;
- }
- #else
- static ssize_t exfat_direct_IO(int rw, struct kiocb *iocb,
- struct iov_iter *iter,
- loff_t offset)
- {
- struct file *file = iocb->ki_filp;
- struct address_space *mapping = file->f_mapping;
- struct inode *inode = mapping->host;
- size_t count = iov_iter_count(iter);
- ssize_t ret;
- if (rw == WRITE) {
- /*
- * FIXME: blockdev_direct_IO() doesn't use ->write_begin(),
- * so we need to update the ->mmu_private to block boundary.
- *
- * But we must fill the remaining area or hole by nul for
- * updating ->mmu_private.
- *
- * Return 0, and fallback to normal buffered write.
- */
- loff_t size = offset + count;
- if (EXFAT_I(inode)->mmu_private < size)
- return 0;
- }
- /*
- * FAT need to use the DIO_LOCKING for avoiding the race
- * condition of fat_get_block() and ->truncate().
- */
- ret = blockdev_direct_IO(rw, iocb, inode, iter, offset, exfat_get_block);
- if (ret < 0 && (rw & WRITE))
- exfat_write_failed(mapping, offset + count);
- return ret;
- }
- #endif
- static sector_t _exfat_bmap(struct address_space *mapping, sector_t block)
- {
- sector_t blocknr;
- #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,00)
- down_read(&EXFAT_I(mapping->host)->truncate_lock);
- blocknr = generic_block_bmap(mapping, block, exfat_get_block);
- up_read(&EXFAT_I(mapping->host)->truncate_lock);
- #else
- down_read(&mapping->host->i_alloc_sem);
- blocknr = generic_block_bmap(mapping, block, exfat_get_block);
- up_read(&mapping->host->i_alloc_sem);
- #endif
- return blocknr;
- }
- const struct address_space_operations exfat_aops = {
- .readpage = exfat_readpage,
- .readpages = exfat_readpages,
- .writepage = exfat_writepage,
- .writepages = exfat_writepages,
- #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,39)
- .sync_page = block_sync_page,
- #endif
- .write_begin = exfat_write_begin,
- .write_end = exfat_write_end,
- .direct_IO = exfat_direct_IO,
- .bmap = _exfat_bmap
- };
- static inline unsigned long exfat_hash(loff_t i_pos)
- {
- return hash_32(i_pos, EXFAT_HASH_BITS);
- }
- static struct inode *exfat_iget(struct super_block *sb, loff_t i_pos) {
- struct exfat_sb_info *sbi = EXFAT_SB(sb);
- struct exfat_inode_info *info;
- struct hlist_head *head = sbi->inode_hashtable + exfat_hash(i_pos);
- struct inode *inode = NULL;
- #if LINUX_VERSION_CODE < KERNEL_VERSION(3,9,0)
- struct hlist_node *node;
- spin_lock(&sbi->inode_hash_lock);
- hlist_for_each_entry(info, node, head, i_hash_fat) {
- #else
- spin_lock(&sbi->inode_hash_lock);
- hlist_for_each_entry(info, head, i_hash_fat) {
- #endif
- CHECK_ERR(info->vfs_inode.i_sb != sb);
- if (i_pos != info->i_pos)
- continue;
- inode = igrab(&info->vfs_inode);
- if (inode)
- break;
- }
- spin_unlock(&sbi->inode_hash_lock);
- return inode;
- }
- static void exfat_attach(struct inode *inode, loff_t i_pos)
- {
- struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb);
- struct hlist_head *head = sbi->inode_hashtable + exfat_hash(i_pos);
- spin_lock(&sbi->inode_hash_lock);
- EXFAT_I(inode)->i_pos = i_pos;
- hlist_add_head(&EXFAT_I(inode)->i_hash_fat, head);
- spin_unlock(&sbi->inode_hash_lock);
- }
- static void exfat_detach(struct inode *inode)
- {
- struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb);
- spin_lock(&sbi->inode_hash_lock);
- hlist_del_init(&EXFAT_I(inode)->i_hash_fat);
- EXFAT_I(inode)->i_pos = 0;
- spin_unlock(&sbi->inode_hash_lock);
- }
- static int exfat_fill_inode(struct inode *inode, FILE_ID_T *fid)
- {
- struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb);
- FS_INFO_T *p_fs = &(sbi->fs_info);
- DIR_ENTRY_T info;
- memcpy(&(EXFAT_I(inode)->fid), fid, sizeof(FILE_ID_T));
- FsReadStat(inode, &info);
- EXFAT_I(inode)->i_pos = 0;
- EXFAT_I(inode)->target = NULL;
- inode->i_uid = sbi->options.fs_uid;
- inode->i_gid = sbi->options.fs_gid;
- inode->i_version++;
- inode->i_generation = get_seconds();
- if (info.Attr & ATTR_SUBDIR) {
- inode->i_generation &= ~1;
- inode->i_mode = exfat_make_mode(sbi, info.Attr, S_IRWXUGO);
- inode->i_op = &exfat_dir_inode_operations;
- inode->i_fop = &exfat_dir_operations;
- i_size_write(inode, info.Size);
- EXFAT_I(inode)->mmu_private = i_size_read(inode);
- #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,00)
- set_nlink(inode,info.NumSubdirs);
- #else
- inode->i_nlink = info.NumSubdirs;
- #endif
- } else if (info.Attr & ATTR_SYMLINK) {
- inode->i_generation |= 1;
- inode->i_mode = exfat_make_mode(sbi, info.Attr, S_IRWXUGO);
- inode->i_op = &exfat_symlink_inode_operations;
- i_size_write(inode, info.Size);
- EXFAT_I(inode)->mmu_private = i_size_read(inode);
- } else {
- inode->i_generation |= 1;
- inode->i_mode = exfat_make_mode(sbi, info.Attr, S_IRWXUGO);
- inode->i_op = &exfat_file_inode_operations;
- inode->i_fop = &exfat_file_operations;
- inode->i_mapping->a_ops = &exfat_aops;
- inode->i_mapping->nrpages = 0;
- i_size_write(inode, info.Size);
- EXFAT_I(inode)->mmu_private = i_size_read(inode);
- }
- exfat_save_attr(inode, info.Attr);
- inode->i_blocks = ((i_size_read(inode) + (p_fs->cluster_size - 1))
- & ~((loff_t)p_fs->cluster_size - 1)) >> inode->i_blkbits;
- exfat_time_fat2unix(sbi, &inode->i_mtime, &info.ModifyTimestamp);
- exfat_time_fat2unix(sbi, &inode->i_ctime, &info.CreateTimestamp);
- exfat_time_fat2unix(sbi, &inode->i_atime, &info.AccessTimestamp);
- return 0;
- }
- static struct inode *exfat_build_inode(struct super_block *sb,
- FILE_ID_T *fid, loff_t i_pos) {
- struct inode *inode;
- int err;
- inode = exfat_iget(sb, i_pos);
- if (inode)
- goto out;
- inode = new_inode(sb);
- if (!inode) {
- inode = ERR_PTR(-ENOMEM);
- goto out;
- }
- inode->i_ino = iunique(sb, EXFAT_ROOT_INO);
- inode->i_version = 1;
- err = exfat_fill_inode(inode, fid);
- if (err) {
- iput(inode);
- inode = ERR_PTR(err);
- goto out;
- }
- exfat_attach(inode, i_pos);
- insert_inode_hash(inode);
- out:
- return inode;
- }
- static int exfat_sync_inode(struct inode *inode)
- {
- return exfat_write_inode(inode, NULL);
- }
- static struct inode *exfat_alloc_inode(struct super_block *sb) {
- struct exfat_inode_info *ei;
- ei = kmem_cache_alloc(exfat_inode_cachep, GFP_NOFS);
- if (!ei)
- return NULL;
- #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,00)
- init_rwsem(&ei->truncate_lock);
- #endif
- return &ei->vfs_inode;
- }
- static void exfat_destroy_inode(struct inode *inode)
- {
- FREE(EXFAT_I(inode)->target);
- EXFAT_I(inode)->target = NULL;
- kmem_cache_free(exfat_inode_cachep, EXFAT_I(inode));
- }
- static int exfat_write_inode(struct inode *inode, struct writeback_control *wbc)
- {
- struct super_block *sb = inode->i_sb;
- struct exfat_sb_info *sbi = EXFAT_SB(sb);
- DIR_ENTRY_T info;
- if (inode->i_ino == EXFAT_ROOT_INO)
- return 0;
- info.Attr = exfat_make_attr(inode);
- info.Size = i_size_read(inode);
- exfat_time_unix2fat(sbi, &inode->i_mtime, &info.ModifyTimestamp);
- exfat_time_unix2fat(sbi, &inode->i_ctime, &info.CreateTimestamp);
- exfat_time_unix2fat(sbi, &inode->i_atime, &info.AccessTimestamp);
- FsWriteStat(inode, &info);
- return 0;
- }
- #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36)
- static void exfat_delete_inode(struct inode *inode)
- {
- truncate_inode_pages(&inode->i_data, 0);
- clear_inode(inode);
- }
- static void exfat_clear_inode(struct inode *inode)
- {
- exfat_detach(inode);
- remove_inode_hash(inode);
- }
- #else
- static void exfat_evict_inode(struct inode *inode)
- {
- truncate_inode_pages(&inode->i_data, 0);
- if (!inode->i_nlink) {
- loff_t old_size = i_size_read(inode);
- i_size_write(inode, 0);
- EXFAT_I(inode)->fid.size = old_size;
- FsTruncateFile(inode, old_size, 0);
- }
- invalidate_inode_buffers(inode);
- #if LINUX_VERSION_CODE < KERNEL_VERSION(3,7,00)
- end_writeback(inode);
- #else
- clear_inode(inode);
- #endif
- exfat_detach(inode);
- remove_inode_hash(inode);
- }
- #endif
- static void exfat_put_super(struct super_block *sb)
- {
- struct exfat_sb_info *sbi = EXFAT_SB(sb);
- int err;
- exfat_mnt_msg(sb, 0, 0, "trying to unmount...");
- if (__is_sb_dirty(sb))
- exfat_write_super(sb);
- err = FsUmountVol(sb);
- if (sbi->nls_disk) {
- unload_nls(sbi->nls_disk);
- sbi->nls_disk = NULL;
- sbi->options.codepage = exfat_default_codepage;
- }
- if (sbi->nls_io) {
- unload_nls(sbi->nls_io);
- sbi->nls_io = NULL;
- }
- if (sbi->options.iocharset != exfat_default_iocharset) {
- kfree(sbi->options.iocharset);
- sbi->options.iocharset = exfat_default_iocharset;
- }
- sb->s_fs_info = NULL;
- if (!sbi->use_vmalloc)
- kfree(sbi);
- else
- vfree(sbi);
- exfat_mnt_msg(sb, 0, err, "unmounted successfully!");
- }
- static void exfat_write_super(struct super_block *sb)
- {
- __lock_super(sb);
- __set_sb_clean(sb);
- if (!(sb->s_flags & MS_RDONLY))
- FsSyncVol(sb, 1);
- __unlock_super(sb);
- }
- static int exfat_sync_fs(struct super_block *sb, int wait)
- {
- int err = 0;
- if (__is_sb_dirty(sb)) {
- __lock_super(sb);
- __set_sb_clean(sb);
- err = FsSyncVol(sb, 1);
- __unlock_super(sb);
- }
- return err;
- }
- static int exfat_statfs(struct dentry *dentry, struct kstatfs *buf)
- {
- struct super_block *sb = dentry->d_sb;
- u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
- FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
- VOL_INFO_T info;
- if (p_fs->used_clusters == (UINT32) ~0) {
- if (FFS_MEDIAERR == FsGetVolInfo(sb, &info))
- return -EIO;
- } else {
- info.FatType = p_fs->vol_type;
- info.ClusterSize = p_fs->cluster_size;
- info.NumClusters = p_fs->num_clusters - 2;
- info.UsedClusters = p_fs->used_clusters;
- info.FreeClusters = info.NumClusters - info.UsedClusters;
- if (p_fs->dev_ejected)
- printk("[EXFAT] called statfs with previous I/O error.\n");
- }
- buf->f_type = sb->s_magic;
- buf->f_bsize = info.ClusterSize;
- buf->f_blocks = info.NumClusters;
- buf->f_bfree = info.FreeClusters;
- buf->f_bavail = info.FreeClusters;
- buf->f_fsid.val[0] = (u32)id;
- buf->f_fsid.val[1] = (u32)(id >> 32);
- buf->f_namelen = 260;
- return 0;
- }
- static int exfat_remount(struct super_block *sb, int *flags, char *data)
- {
- char *orig_data = kstrdup(data, GFP_KERNEL);
- *flags |= MS_NODIRATIME;
- #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,16,00)
- sync_filesystem(sb);
- #endif
- exfat_msg(sb, KERN_INFO, "re-mounted. Opts: %s", orig_data);
- ST_LOG("[EXFAT] re-mounted. Opts: %s", orig_data);
- kfree(orig_data);
- return 0;
- }
- #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,00)
- static int exfat_show_options(struct seq_file *m, struct dentry *root)
- {
- struct exfat_sb_info *sbi = EXFAT_SB(root->d_sb);
- #else
- static int exfat_show_options(struct seq_file *m, struct vfsmount *mnt)
- {
- struct exfat_sb_info *sbi = EXFAT_SB(mnt->mnt_sb);
- #endif
- struct exfat_mount_options *opts = &sbi->options;
- FS_INFO_T *p_fs = &(sbi->fs_info);
- if (!uid_eq(opts->fs_uid, GLOBAL_ROOT_UID))
- seq_printf(m, ",uid=%u",
- from_kuid_munged(&init_user_ns, opts->fs_uid));
- if (!gid_eq(opts->fs_gid, GLOBAL_ROOT_GID))
- seq_printf(m, ",gid=%u",
- from_kgid_munged(&init_user_ns, opts->fs_gid));
- seq_printf(m, ",fmask=%04o", opts->fs_fmask);
- seq_printf(m, ",dmask=%04o", opts->fs_dmask);
- if (opts->allow_utime)
- seq_printf(m, ",allow_utime=%04o", opts->allow_utime);
- if (sbi->nls_disk)
- seq_printf(m, ",codepage=%s", sbi->nls_disk->charset);
- if (sbi->nls_io)
- seq_printf(m, ",iocharset=%s", sbi->nls_io->charset);
- seq_printf(m, ",namecase=%u", opts->casesensitive);
- if (opts->tz_utc)
- seq_puts(m, ",tz=UTC");
- if (opts->errors == EXFAT_ERRORS_CONT)
- seq_puts(m, ",errors=continue");
- else if (opts->errors == EXFAT_ERRORS_PANIC)
- seq_puts(m, ",errors=panic");
- else
- seq_puts(m, ",errors=remount-ro");
- #if EXFAT_CONFIG_DISCARD
- if (opts->discard)
- seq_printf(m, ",discard");
- #endif
- if (p_fs->dev_ejected)
- seq_puts(m, ",ejected");
- return 0;
- }
- const struct super_operations exfat_sops = {
- .alloc_inode = exfat_alloc_inode,
- .destroy_inode = exfat_destroy_inode,
- .write_inode = exfat_write_inode,
- #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36)
- .delete_inode = exfat_delete_inode,
- .clear_inode = exfat_clear_inode,
- #else
- .evict_inode = exfat_evict_inode,
- #endif
- .put_super = exfat_put_super,
- #if LINUX_VERSION_CODE < KERNEL_VERSION(3,7,00)
- .write_super = exfat_write_super,
- #endif
- .sync_fs = exfat_sync_fs,
- .statfs = exfat_statfs,
- .remount_fs = exfat_remount,
- .show_options = exfat_show_options,
- };
- enum {
- Opt_uid,
- Opt_gid,
- Opt_umask,
- Opt_dmask,
- Opt_fmask,
- Opt_allow_utime,
- Opt_codepage,
- Opt_charset,
- Opt_namecase,
- Opt_debug,
- Opt_tz_utc,
- Opt_err_cont,
- Opt_err_panic,
- Opt_err_ro,
- Opt_err,
- #if EXFAT_CONFIG_DISCARD
- Opt_discard,
- #endif
- };
- static const match_table_t exfat_tokens = {
- {Opt_uid, "uid=%u"},
- {Opt_gid, "gid=%u"},
- {Opt_umask, "umask=%o"},
- {Opt_dmask, "dmask=%o"},
- {Opt_fmask, "fmask=%o"},
- {Opt_allow_utime, "allow_utime=%o"},
- {Opt_codepage, "codepage=%u"},
- {Opt_charset, "iocharset=%s"},
- {Opt_namecase, "namecase=%u"},
- {Opt_debug, "debug"},
- {Opt_tz_utc, "tz=UTC"},
- {Opt_err_cont, "errors=continue"},
- {Opt_err_panic, "errors=panic"},
- {Opt_err_ro, "errors=remount-ro"},
- #if EXFAT_CONFIG_DISCARD
- {Opt_discard, "discard"},
- #endif
- {Opt_err, NULL}
- };
- static int parse_options(char *options, int silent, int *debug,
- struct exfat_mount_options *opts)
- {
- char *p;
- substring_t args[MAX_OPT_ARGS];
- int option;
- char *iocharset;
- opts->fs_uid = current_uid();
- opts->fs_gid = current_gid();
- opts->fs_fmask = opts->fs_dmask = current->fs->umask;
- opts->allow_utime = (unsigned short) -1;
- opts->codepage = exfat_default_codepage;
- opts->iocharset = exfat_default_iocharset;
- opts->casesensitive = 0;
- opts->tz_utc = 0;
- opts->errors = EXFAT_ERRORS_RO;
- #if EXFAT_CONFIG_DISCARD
- opts->discard = 0;
- #endif
- *debug = 0;
- if (!options)
- goto out;
- while ((p = strsep(&options, ",")) != NULL) {
- int token;
- if (!*p)
- continue;
- token = match_token(p, exfat_tokens, args);
- switch (token) {
- case Opt_uid:
- if (match_int(&args[0], &option))
- return 0;
- opts->fs_uid = make_kuid(current_user_ns(), option);
- break;
- case Opt_gid:
- if (match_int(&args[0], &option))
- return 0;
- opts->fs_gid = make_kgid(current_user_ns(), option);
- break;
- case Opt_umask:
- case Opt_dmask:
- case Opt_fmask:
- if (match_octal(&args[0], &option))
- return 0;
- if (token != Opt_dmask)
- opts->fs_fmask = option;
- if (token != Opt_fmask)
- opts->fs_dmask = option;
- break;
- case Opt_allow_utime:
- if (match_octal(&args[0], &option))
- return 0;
- opts->allow_utime = option & (S_IWGRP | S_IWOTH);
- break;
- case Opt_codepage:
- if (match_int(&args[0], &option))
- return 0;
- opts->codepage = option;
- break;
- case Opt_charset:
- if (opts->iocharset != exfat_default_iocharset)
- kfree(opts->iocharset);
- iocharset = match_strdup(&args[0]);
- if (!iocharset)
- return -ENOMEM;
- opts->iocharset = iocharset;
- break;
- case Opt_namecase:
- if (match_int(&args[0], &option))
- return 0;
- opts->casesensitive = option;
- break;
- case Opt_tz_utc:
- opts->tz_utc = 1;
- break;
- case Opt_err_cont:
- opts->errors = EXFAT_ERRORS_CONT;
- break;
- case Opt_err_panic:
- opts->errors = EXFAT_ERRORS_PANIC;
- break;
- case Opt_err_ro:
- opts->errors = EXFAT_ERRORS_RO;
- break;
- case Opt_debug:
- *debug = 1;
- break;
- #if EXFAT_CONFIG_DISCARD
- case Opt_discard:
- opts->discard = 1;
- break;
- #endif
- default:
- if (!silent) {
- printk(KERN_ERR "[EXFAT] Unrecognized mount option %s or missing value\n", p);
- }
- return -EINVAL;
- }
- }
- out:
- if (opts->allow_utime == (unsigned short) -1)
- opts->allow_utime = ~opts->fs_dmask & (S_IWGRP | S_IWOTH);
- return 0;
- }
- static void exfat_hash_init(struct super_block *sb)
- {
- struct exfat_sb_info *sbi = EXFAT_SB(sb);
- int i;
- spin_lock_init(&sbi->inode_hash_lock);
- for (i = 0; i < EXFAT_HASH_SIZE; i++)
- INIT_HLIST_HEAD(&sbi->inode_hashtable[i]);
- }
- static int exfat_read_root(struct inode *inode)
- {
- struct super_block *sb = inode->i_sb;
- struct exfat_sb_info *sbi = EXFAT_SB(sb);
- struct timespec ts;
- FS_INFO_T *p_fs = &(sbi->fs_info);
- DIR_ENTRY_T info;
- ts = CURRENT_TIME_SEC;
- EXFAT_I(inode)->fid.dir.dir = p_fs->root_dir;
- EXFAT_I(inode)->fid.dir.flags = 0x01;
- EXFAT_I(inode)->fid.entry = -1;
- EXFAT_I(inode)->fid.start_clu = p_fs->root_dir;
- EXFAT_I(inode)->fid.flags = 0x01;
- EXFAT_I(inode)->fid.type = TYPE_DIR;
- EXFAT_I(inode)->fid.rwoffset = 0;
- EXFAT_I(inode)->fid.hint_last_off = -1;
- EXFAT_I(inode)->target = NULL;
- FsReadStat(inode, &info);
- inode->i_uid = sbi->options.fs_uid;
- inode->i_gid = sbi->options.fs_gid;
- inode->i_version++;
- inode->i_generation = 0;
- inode->i_mode = exfat_make_mode(sbi, ATTR_SUBDIR, S_IRWXUGO);
- inode->i_op = &exfat_dir_inode_operations;
- inode->i_fop = &exfat_dir_operations;
- i_size_write(inode, info.Size);
- inode->i_blocks = ((i_size_read(inode) + (p_fs->cluster_size - 1))
- & ~((loff_t)p_fs->cluster_size - 1)) >> inode->i_blkbits;
- EXFAT_I(inode)->i_pos = ((loff_t) p_fs->root_dir << 32) | 0xffffffff;
- EXFAT_I(inode)->mmu_private = i_size_read(inode);
- exfat_save_attr(inode, ATTR_SUBDIR);
- inode->i_mtime = inode->i_atime = inode->i_ctime = ts;
- #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,00)
- set_nlink(inode,info.NumSubdirs + 2);
- #else
- inode->i_nlink = info.NumSubdirs + 2;
- #endif
- return 0;
- }
- static void setup_dops(struct super_block *sb)
- {
- if (EXFAT_SB(sb)->options.casesensitive == 0)
- sb->s_d_op = &exfat_ci_dentry_ops;
- else
- sb->s_d_op = &exfat_dentry_ops;
- }
- static int exfat_fill_super(struct super_block *sb, void *data, int silent)
- {
- struct inode *root_inode = NULL;
- struct exfat_sb_info *sbi;
- int debug, ret;
- long error;
- char buf[50];
- exfat_mnt_msg(sb, 1, 0, "trying to mount...");
- sbi = kzalloc(sizeof(struct exfat_sb_info), GFP_KERNEL);
- if (!sbi) {
- sbi = vzalloc(sizeof(struct exfat_sb_info));
- if (!sbi) {
- exfat_mnt_msg(sb, 1, 0, "failed to mount! (ENOMEM)");
- return -ENOMEM;
- }
- sbi->use_vmalloc = 1;
- }
- #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,0)
- mutex_init(&sbi->s_lock);
- #endif
- sb->s_fs_info = sbi;
- sb->s_flags |= MS_NODIRATIME;
- sb->s_magic = EXFAT_SUPER_MAGIC;
- sb->s_op = &exfat_sops;
- error = parse_options(data, silent, &debug, &sbi->options);
- if (error)
- goto out_fail;
- setup_dops(sb);
- error = -EIO;
- sb_min_blocksize(sb, 512);
- sb->s_maxbytes = 0x7fffffffffffffffLL;
- ret = FsMountVol(sb);
- if (ret) {
- if (!silent)
- printk(KERN_ERR "[EXFAT] FsMountVol failed\n");
- goto out_fail;
- }
- exfat_hash_init(sb);
- error = -EINVAL;
- sprintf(buf, "cp%d", sbi->options.codepage);
- sbi->nls_disk = load_nls(buf);
- if (!sbi->nls_disk) {
- printk(KERN_ERR "[EXFAT] Codepage %s not found\n", buf);
- goto out_fail2;
- }
- sbi->nls_io = load_nls(sbi->options.iocharset);
- if (!sbi->nls_io) {
- printk(KERN_ERR "[EXFAT] IO charset %s not found\n",
- sbi->options.iocharset);
- goto out_fail2;
- }
- error = -ENOMEM;
- root_inode = new_inode(sb);
- if (!root_inode)
- goto out_fail2;
- root_inode->i_ino = EXFAT_ROOT_INO;
- root_inode->i_version = 1;
- error = exfat_read_root(root_inode);
- if (error < 0)
- goto out_fail2;
- error = -ENOMEM;
- exfat_attach(root_inode, EXFAT_I(root_inode)->i_pos);
- insert_inode_hash(root_inode);
- #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,00)
- sb->s_root = d_make_root(root_inode);
- #else
- sb->s_root = d_alloc_root(root_inode);
- #endif
- if (!sb->s_root) {
- printk(KERN_ERR "[EXFAT] Getting the root inode failed\n");
- goto out_fail2;
- }
- exfat_mnt_msg(sb, 1, 0, "mounted successfully!");
- return 0;
- out_fail2:
- FsUmountVol(sb);
- out_fail:
- exfat_mnt_msg(sb, 1, 0, "failed to mount!");
- if (root_inode)
- iput(root_inode);
- if (sbi->nls_io)
- unload_nls(sbi->nls_io);
- if (sbi->nls_disk)
- unload_nls(sbi->nls_disk);
- if (sbi->options.iocharset != exfat_default_iocharset)
- kfree(sbi->options.iocharset);
- sb->s_fs_info = NULL;
- if (!sbi->use_vmalloc)
- kfree(sbi);
- else
- vfree(sbi);
- return error;
- }
- #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,37)
- static int exfat_get_sb(struct file_system_type *fs_type,
- int flags, const char *dev_name,
- void *data, struct vfsmount *mnt)
- {
- return get_sb_bdev(fs_type, flags, dev_name, data, exfat_fill_super, mnt);
- }
- #else
- static struct dentry *exfat_fs_mount(struct file_system_type *fs_type,
- int flags, const char *dev_name,
- void *data) {
- return mount_bdev(fs_type, flags, dev_name, data, exfat_fill_super);
- }
- #endif
- static void init_once(void *foo)
- {
- struct exfat_inode_info *ei = (struct exfat_inode_info *)foo;
- INIT_HLIST_NODE(&ei->i_hash_fat);
- inode_init_once(&ei->vfs_inode);
- }
- static int __init exfat_init_inodecache(void)
- {
- exfat_inode_cachep = kmem_cache_create("exfat_inode_cache",
- sizeof(struct exfat_inode_info),
- 0, (SLAB_RECLAIM_ACCOUNT|
- SLAB_MEM_SPREAD),
- init_once);
- if (exfat_inode_cachep == NULL)
- return -ENOMEM;
- return 0;
- }
- static void __exit exfat_destroy_inodecache(void)
- {
- kmem_cache_destroy(exfat_inode_cachep);
- }
- #if EXFAT_CONFIG_KERNEL_DEBUG
- static void exfat_debug_kill_sb(struct super_block *sb)
- {
- struct exfat_sb_info *sbi = EXFAT_SB(sb);
- struct block_device *bdev = sb->s_bdev;
- long flags;
- if (sbi) {
- flags = sbi->debug_flags;
- if (flags & EXFAT_DEBUGFLAGS_INVALID_UMOUNT) {
- FsReleaseCache(sb);
- invalidate_bdev(bdev);
- }
- }
- kill_block_super(sb);
- }
- #endif
- static struct file_system_type exfat_fs_type = {
- .owner = THIS_MODULE,
- .name = "exfat",
- #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,37)
- .get_sb = exfat_get_sb,
- #else
- .mount = exfat_fs_mount,
- #endif
- #if EXFAT_CONFIG_KERNEL_DEBUG
- .kill_sb = exfat_debug_kill_sb,
- #else
- .kill_sb = kill_block_super,
- #endif
- .fs_flags = FS_REQUIRES_DEV,
- };
- static int __init init_exfat_fs(void)
- {
- int err;
- printk(KERN_INFO "exFAT: FS Version %s\n", EXFAT_VERSION);
- err = exfat_init_inodecache();
- if (err) return err;
- return register_filesystem(&exfat_fs_type);
- }
- static void __exit exit_exfat_fs(void)
- {
- exfat_destroy_inodecache();
- unregister_filesystem(&exfat_fs_type);
- }
- module_init(init_exfat_fs);
- module_exit(exit_exfat_fs);
- MODULE_LICENSE("GPL");
|