1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025
#include <linux/string_choices.h>
#include <linux/writeback.h>
#include <linux/iomap.h>
#include "attrib.h"
#include "attrlist.h"
#include "lcnalloc.h"
#include "debug.h"
#include "mft.h"
#include "ntfs.h"
#include "iomap.h"
__le16 AT_UNNAMED[] = { cpu_to_le16('\0') };
#define NTFS_ATTR_READALL_MAX_SIZE (64 * 1024)
int ntfs_map_runlist_nolock(struct ntfs_inode *ni, s64 vcn, struct ntfs_attr_search_ctx *ctx)
{
s64 end_vcn;
unsigned long flags;
struct ntfs_inode *base_ni;
struct mft_record *m;
struct attr_record *a;
struct runlist_element *rl;
struct folio *put_this_folio = NULL;
int err = 0;
bool ctx_is_temporary = false, ctx_needs_reset = false;
struct ntfs_attr_search_ctx old_ctx = { NULL, };
size_t new_rl_count;
ntfs_debug("Mapping runlist part containing vcn 0x%llx.",
(unsigned long long)vcn);
if (!NInoAttr(ni))
base_ni = ni;
else
base_ni = ni->ext.base_ntfs_ino;
if (unlikely(NVolMftBootstrap(ni->vol) &&
base_ni == NTFS_I(ni->vol->mft_ino))) {
ntfs_error(ni->vol->sb,
"$MFT needs its own extent records to describe itself; cannot mount.");
return -EIO;
}
if (!ctx) {
ctx_is_temporary = ctx_needs_reset = true;
m = map_mft_record(base_ni);
if (IS_ERR(m))
return PTR_ERR(m);
ctx = ntfs_attr_get_search_ctx(base_ni, m);
if (unlikely(!ctx)) {
err = -ENOMEM;
goto err_out;
}
} else {
s64 allocated_size_vcn;
WARN_ON(IS_ERR(ctx->mrec));
a = ctx->attr;
if (!a->non_resident) {
err = -EIO;
goto err_out;
}
end_vcn = le64_to_cpu(a->data.non_resident.highest_vcn);
read_lock_irqsave(&ni->size_lock, flags);
allocated_size_vcn =
ntfs_bytes_to_cluster(ni->vol, ni->allocated_size);
read_unlock_irqrestore(&ni->size_lock, flags);
if (!a->data.non_resident.lowest_vcn && end_vcn <= 0)
end_vcn = allocated_size_vcn - 1;
if (vcn >= allocated_size_vcn || (a->type == ni->type &&
a->name_length == ni->name_len &&
!memcmp((u8 *)a + le16_to_cpu(a->name_offset),
ni->name, ni->name_len) &&
le64_to_cpu(a->data.non_resident.lowest_vcn)
<= vcn && end_vcn >= vcn))
ctx_needs_reset = false;
else {
old_ctx = *ctx;
if (old_ctx.base_ntfs_ino && old_ctx.ntfs_ino !=
old_ctx.base_ntfs_ino) {
put_this_folio = old_ctx.ntfs_ino->folio;
folio_get(put_this_folio);
}
ntfs_attr_reinit_search_ctx(ctx);
ctx_needs_reset = true;
}
}
if (ctx_needs_reset) {
err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
CASE_SENSITIVE, vcn, NULL, 0, ctx);
if (unlikely(err)) {
if (err == -ENOENT)
err = -EIO;
goto err_out;
}
if (unlikely(!ctx->attr->non_resident)) {
err = -EIO;
goto err_out;
}
}
a = ctx->attr;
end_vcn = le64_to_cpu(a->data.non_resident.highest_vcn) + 1;
if (unlikely(vcn && vcn >= end_vcn)) {
err = -ENOENT;
goto err_out;
}
rl = ntfs_mapping_pairs_decompress(ni->vol, a, &ni->runlist, &new_rl_count);
if (IS_ERR(rl))
err = PTR_ERR(rl);
else {
ni->runlist.rl = rl;
ni->runlist.count = new_rl_count;
}
err_out:
if (ctx_is_temporary) {
if (likely(ctx))
ntfs_attr_put_search_ctx(ctx);
unmap_mft_record(base_ni);
} else if (ctx_needs_reset) {
if (NInoAttrList(base_ni)) {
if (ctx->ntfs_ino != old_ctx.ntfs_ino) {
if (ctx->base_ntfs_ino && ctx->ntfs_ino !=
ctx->base_ntfs_ino) {
unmap_extent_mft_record(ctx->ntfs_ino);
ctx->mrec = ctx->base_mrec;
WARN_ON(!ctx->mrec);
}
if (old_ctx.base_ntfs_ino &&
old_ctx.ntfs_ino != old_ctx.base_ntfs_ino) {
retry_map:
ctx->mrec = map_mft_record(old_ctx.ntfs_ino);
if (IS_ERR(ctx->mrec)) {
if (PTR_ERR(ctx->mrec) == -ENOMEM) {
schedule();
goto retry_map;
} else
old_ctx.ntfs_ino =
old_ctx.base_ntfs_ino;
}
}
}
if (ctx->mrec != old_ctx.mrec) {
if (!IS_ERR(ctx->mrec))
old_ctx.attr = (struct attr_record *)(
(u8 *)ctx->mrec +
((u8 *)old_ctx.attr -
(u8 *)old_ctx.mrec));
old_ctx.mrec = ctx->mrec;
}
}
*ctx = old_ctx;
if (put_this_folio)
folio_put(put_this_folio);
}
return err;
}
int ntfs_map_runlist(struct ntfs_inode *ni, s64 vcn)
{
int err = 0;
down_write(&ni->runlist.lock);
if (likely(ntfs_rl_vcn_to_lcn(ni->runlist.rl, vcn) <=
LCN_RL_NOT_MAPPED))
err = ntfs_map_runlist_nolock(ni, vcn, NULL);
up_write(&ni->runlist.lock);
return err;
}
struct runlist_element *ntfs_attr_vcn_to_rl(struct ntfs_inode *ni, s64 vcn, s64 *lcn)
{
struct runlist_element *rl = ni->runlist.rl;
int err = 0;
bool is_retry = false;
if (!rl) {
err = ntfs_attr_map_whole_runlist(ni);
if (err)
return ERR_PTR(-ENOENT);
rl = ni->runlist.rl;
}
remap_rl:
while (rl->length && rl[1].vcn <= vcn)
rl++;
*lcn = ntfs_rl_vcn_to_lcn(rl, vcn);
if (*lcn <= LCN_RL_NOT_MAPPED && is_retry == false) {
is_retry = true;
err = ntfs_map_runlist_nolock(ni, vcn, NULL);
if (!err) {
rl = ni->runlist.rl;
goto remap_rl;
}
}
if (*lcn <= LCN_RL_NOT_MAPPED) {
unsigned long flags;
s64 allocated_size;
read_lock_irqsave(&ni->size_lock, flags);
allocated_size = ni->allocated_size;
read_unlock_irqrestore(&ni->size_lock, flags);
if ((s64)ntfs_cluster_to_bytes(ni->vol, vcn) < allocated_size)
return ERR_PTR(err == -ENOMEM ? -ENOMEM : -EIO);
}
return rl;
}
s64 ntfs_attr_vcn_to_lcn_nolock(struct ntfs_inode *ni, const s64 vcn,
const bool write_locked)
{
s64 lcn;
unsigned long flags;
bool is_retry = false;
ntfs_debug("Entering for i_ino 0x%llx, vcn 0x%llx, %s_locked.",
ni->mft_no, (unsigned long long)vcn,
write_locked ? "write" : "read");
if (!ni->runlist.rl) {
read_lock_irqsave(&ni->size_lock, flags);
if (!ni->allocated_size) {
read_unlock_irqrestore(&ni->size_lock, flags);
return LCN_ENOENT;
}
read_unlock_irqrestore(&ni->size_lock, flags);
}
retry_remap:
lcn = ntfs_rl_vcn_to_lcn(ni->runlist.rl, vcn);
if (likely(lcn >= LCN_HOLE)) {
ntfs_debug("Done, lcn 0x%llx.", (long long)lcn);
return lcn;
}
if (lcn != LCN_RL_NOT_MAPPED) {
if (lcn != LCN_ENOENT)
lcn = LCN_EIO;
} else if (!is_retry) {
int err;
if (!write_locked) {
up_read(&ni->runlist.lock);
down_write(&ni->runlist.lock);
if (unlikely(ntfs_rl_vcn_to_lcn(ni->runlist.rl, vcn) !=
LCN_RL_NOT_MAPPED)) {
up_write(&ni->runlist.lock);
down_read(&ni->runlist.lock);
goto retry_remap;
}
}
err = ntfs_map_runlist_nolock(ni, vcn, NULL);
if (!write_locked) {
up_write(&ni->runlist.lock);
down_read(&ni->runlist.lock);
}
if (likely(!err)) {
is_retry = true;
goto retry_remap;
}
if (err == -ENOENT)
lcn = LCN_ENOENT;
else if (err == -ENOMEM)
lcn = LCN_ENOMEM;
else
lcn = LCN_EIO;
}
if (lcn != LCN_ENOENT)
ntfs_error(ni->vol->sb, "Failed with error code %lli.",
(long long)lcn);
return lcn;
}
struct runlist_element *__ntfs_attr_find_vcn_nolock(struct runlist *runlist, const s64 vcn)
{
size_t lower_idx, upper_idx, idx;
struct runlist_element *run;
int rh = runlist->rl_hint;
if (runlist->count <= 1)
return ERR_PTR(-ENOENT);
if (runlist->count - 1 > rh && runlist->rl[rh].vcn <= vcn) {
if (vcn < runlist->rl[rh].vcn + runlist->rl[rh].length)
return &runlist->rl[rh];
if (runlist->count - 2 == rh)
return ERR_PTR(-ENOENT);
lower_idx = rh + 1;
} else {
run = &runlist->rl[0];
if (vcn < run->vcn)
return ERR_PTR(-ENOENT);
else if (vcn < run->vcn + run->length) {
runlist->rl_hint = 0;
return run;
}
lower_idx = 1;
}
run = &runlist->rl[runlist->count - 2];
if (vcn >= run->vcn && vcn < run->vcn + run->length) {
runlist->rl_hint = runlist->count - 2;
return run;
}
if (vcn >= run->vcn + run->length)
return ERR_PTR(-ENOENT);
upper_idx = runlist->count - 2;
while (lower_idx <= upper_idx) {
idx = (lower_idx + upper_idx) >> 1;
run = &runlist->rl[idx];
if (vcn < run->vcn)
upper_idx = idx - 1;
else if (vcn >= run->vcn + run->length)
lower_idx = idx + 1;
else {
runlist->rl_hint = idx;
return run;
}
}
return ERR_PTR(-ENOENT);
}
struct runlist_element *ntfs_attr_find_vcn_nolock(struct ntfs_inode *ni, const s64 vcn,
struct ntfs_attr_search_ctx *ctx)
{
unsigned long flags;
struct runlist_element *rl;
int err = 0;
bool is_retry = false;
ntfs_debug("Entering for i_ino 0x%llx, vcn 0x%llx, with%s ctx.",
ni->mft_no, (unsigned long long)vcn, ctx ? "" : "out");
if (!ni->runlist.rl) {
read_lock_irqsave(&ni->size_lock, flags);
if (!ni->allocated_size) {
read_unlock_irqrestore(&ni->size_lock, flags);
return ERR_PTR(-ENOENT);
}
read_unlock_irqrestore(&ni->size_lock, flags);
}
retry_remap:
rl = ni->runlist.rl;
if (likely(rl && vcn >= rl[0].vcn)) {
rl = __ntfs_attr_find_vcn_nolock(&ni->runlist, vcn);
if (IS_ERR(rl))
err = PTR_ERR(rl);
else if (rl->lcn >= LCN_HOLE)
return rl;
else if (rl->lcn <= LCN_ENOENT)
err = -EIO;
}
if (!err && !is_retry) {
if (ctx && IS_ERR(ctx->mrec))
err = PTR_ERR(ctx->mrec);
else {
err = ntfs_map_runlist_nolock(ni, vcn, ctx);
if (likely(!err)) {
is_retry = true;
goto retry_remap;
}
}
if (err == -EINVAL)
err = -EIO;
} else if (!err)
err = -EIO;
if (err != -ENOENT)
ntfs_error(ni->vol->sb, "Failed with error code %i.", err);
return ERR_PTR(err);
}
static u32 ntfs_resident_attr_min_value_length(const __le32 type)
{
switch (type) {
case AT_STANDARD_INFORMATION:
return offsetof(struct standard_information, ver) +
sizeof(((struct standard_information *)0)->ver.v1.reserved12);
case AT_FILE_NAME:
return offsetof(struct file_name_attr, file_name) +
sizeof(__le16) * 1;
case AT_VOLUME_INFORMATION:
return sizeof(struct volume_information);
case AT_INDEX_ROOT:
return sizeof(struct index_root);
case AT_EA_INFORMATION:
return sizeof(struct ea_information);
default:
return 0;
}
}
static bool ntfs_attr_type_is_resident_only(const __le32 type)
{
switch (type) {
case AT_STANDARD_INFORMATION:
case AT_FILE_NAME:
case AT_OBJECT_ID:
case AT_VOLUME_NAME:
case AT_VOLUME_INFORMATION:
case AT_INDEX_ROOT:
case AT_EA_INFORMATION:
return true;
default:
return false;
}
}
static bool ntfs_file_name_attr_value_is_valid(const u8 *value, const u32 value_length)
{
const struct file_name_attr *fn;
u32 file_name_size;
fn = (const struct file_name_attr *)value;
file_name_size = fn->file_name_length * sizeof(__le16);
return file_name_size <=
value_length - offsetof(struct file_name_attr, file_name);
}
static bool ntfs_volume_name_attr_value_is_valid(const u32 value_length)
{
if (value_length & 1)
return false;
return value_length <= NTFS_MAX_LABEL_LEN * sizeof(__le16);
}
static bool ntfs_index_root_attr_value_is_valid(const u8 *value, const u32 value_length)
{
const struct index_root *ir;
u32 index_size;
u32 entries_offset;
u32 index_length;
u32 allocated_size;
ir = (const struct index_root *)value;
index_size = value_length - offsetof(struct index_root, index);
entries_offset = le32_to_cpu(ir->index.entries_offset);
index_length = le32_to_cpu(ir->index.index_length);
allocated_size = le32_to_cpu(ir->index.allocated_size);
if ((entries_offset | index_length | allocated_size) & 7 ||
entries_offset < sizeof(struct index_header) ||
entries_offset > index_length ||
index_length > allocated_size ||
allocated_size > index_size ||
index_length - entries_offset < sizeof(struct index_entry_header))
return false;
return true;
}
struct ntfs_resident_attr_value {
const u8 *data;
u32 len;
};
static bool ntfs_resident_attr_value_get(const struct attr_record *a,
struct ntfs_resident_attr_value *value)
{
u32 attr_len;
u32 min_len;
u16 value_offset;
attr_len = le32_to_cpu(a->length);
min_len = offsetof(struct attr_record, data.resident.reserved) +
sizeof(a->data.resident.reserved);
if (attr_len < min_len)
return false;
value->len = le32_to_cpu(a->data.resident.value_length);
value_offset = le16_to_cpu(a->data.resident.value_offset);
if (value_offset < min_len || value->len > attr_len ||
value_offset > attr_len - value->len)
return false;
value->data = (const u8 *)a + value_offset;
return true;
}
static bool ntfs_non_resident_attr_value_is_valid(const struct attr_record *a)
{
u32 attr_len;
u32 min_len;
u16 mp_offset;
u16 name_offset;
u32 name_end;
attr_len = le32_to_cpu(a->length);
min_len = offsetof(struct attr_record, data.non_resident.initialized_size) +
sizeof(a->data.non_resident.initialized_size);
if ((a->flags & (ATTR_IS_SPARSE | ATTR_COMPRESSION_MASK)) &&
!a->data.non_resident.lowest_vcn)
min_len += sizeof(a->data.non_resident.compressed_size);
if (attr_len < min_len)
return false;
mp_offset = le16_to_cpu(a->data.non_resident.mapping_pairs_offset);
if (mp_offset < min_len || mp_offset > attr_len)
return false;
if (a->name_length) {
name_offset = le16_to_cpu(a->name_offset);
if (name_offset < min_len || name_offset >= attr_len)
return false;
name_end = name_offset + a->name_length * sizeof(__le16);
if (name_end > attr_len || name_end > mp_offset)
return false;
}
if (!(a->flags & (ATTR_IS_SPARSE | ATTR_COMPRESSION_MASK)) &&
attr_len - mp_offset <
sizeof(a->data.non_resident.compressed_size))
return false;
return true;
}
static bool ntfs_attr_value_is_valid(struct ntfs_volume *vol,
const struct attr_record *a,
const u64 mft_no)
{
struct ntfs_resident_attr_value value;
u32 min_len;
if (a->non_resident) {
if (ntfs_attr_type_is_resident_only(a->type))
goto corrupt;
if (!ntfs_non_resident_attr_value_is_valid(a))
goto corrupt;
return true;
}
if (!ntfs_resident_attr_value_get(a, &value))
goto corrupt;
min_len = ntfs_resident_attr_min_value_length(a->type);
if (min_len && value.len < min_len)
goto corrupt;
switch (a->type) {
case AT_FILE_NAME:
if (!ntfs_file_name_attr_value_is_valid(value.data, value.len))
goto corrupt;
break;
case AT_VOLUME_NAME:
if (!ntfs_volume_name_attr_value_is_valid(value.len))
goto corrupt;
break;
case AT_INDEX_ROOT:
if (!ntfs_index_root_attr_value_is_valid(value.data, value.len))
goto corrupt;
break;
}
return true;
corrupt:
ntfs_error(vol->sb,
"Corrupt %#x attribute in MFT record %llu\n",
le32_to_cpu(a->type), mft_no);
return false;
}
static int ntfs_attr_find(const __le32 type, const __le16 *name,
const u32 name_len, const u32 ic,
const u8 *val, const u32 val_len, struct ntfs_attr_search_ctx *ctx)
{
struct attr_record *a;
struct ntfs_volume *vol = ctx->ntfs_ino->vol;
__le16 *upcase = vol->upcase;
u32 upcase_len = vol->upcase_len;
unsigned int space;
u16 name_offset;
u32 attr_len;
u32 name_size;
if (ctx->is_first) {
a = ctx->attr;
ctx->is_first = false;
} else
a = (struct attr_record *)((u8 *)ctx->attr +
le32_to_cpu(ctx->attr->length));
for (;; a = (struct attr_record *)((u8 *)a + le32_to_cpu(a->length))) {
if ((u8 *)a < (u8 *)ctx->mrec || (u8 *)a > (u8 *)ctx->mrec +
le32_to_cpu(ctx->mrec->bytes_allocated))
break;
space = le32_to_cpu(ctx->mrec->bytes_in_use) - ((u8 *)a - (u8 *)ctx->mrec);
if ((space < offsetof(struct attr_record, data.resident.reserved) + 1 ||
space < le32_to_cpu(a->length)) && (space < 4 || a->type != AT_END))
break;
ctx->attr = a;
if (((type != AT_UNUSED) && (le32_to_cpu(a->type) > le32_to_cpu(type))) ||
a->type == AT_END)
return -ENOENT;
if (unlikely(!a->length))
break;
if (a->name_length) {
name_offset = le16_to_cpu(a->name_offset);
attr_len = le32_to_cpu(a->length);
name_size = a->name_length * sizeof(__le16);
if (name_offset > attr_len ||
attr_len - name_offset < name_size) {
ntfs_error(vol->sb,
"Corrupt attribute name in MFT record %llu\n",
ctx->ntfs_ino->mft_no);
break;
}
}
if (type == AT_UNUSED) {
if (!ntfs_attr_value_is_valid(vol, a, ctx->ntfs_ino->mft_no))
break;
return 0;
}
if (a->type != type)
continue;
if (!name || name == AT_UNNAMED) {
if (a->name_length)
return -ENOENT;
} else {
if (!ntfs_are_names_equal(name, name_len,
(__le16 *)((u8 *)a + le16_to_cpu(a->name_offset)),
a->name_length, ic, upcase, upcase_len)) {
register int rc;
rc = ntfs_collate_names(name, name_len,
(__le16 *)((u8 *)a + le16_to_cpu(a->name_offset)),
a->name_length, true, IGNORE_CASE,
upcase, upcase_len);
if (rc == -1)
return -ENOENT;
if (rc)
continue;
rc = ntfs_collate_names(name, name_len,
(__le16 *)((u8 *)a + le16_to_cpu(a->name_offset)),
a->name_length, true, CASE_SENSITIVE,
upcase, upcase_len);
if (rc == -1)
return -ENOENT;
if (rc)
continue;
}
}
if (!ntfs_attr_value_is_valid(vol, a, ctx->ntfs_ino->mft_no))
break;
if (!val || a->non_resident)
return 0;
else {
u32 value_length = le32_to_cpu(a->data.resident.value_length);
int rc;
rc = memcmp(val, (u8 *)a + le16_to_cpu(
a->data.resident.value_offset),
min_t(u32, val_len, value_length));
if (!rc) {
if (val_len == value_length)
return 0;
if (val_len < value_length)
return -ENOENT;
} else if (rc < 0)
return -ENOENT;
}
}
ntfs_error(vol->sb, "mft %#llx, type %#x is corrupt. Run chkdsk.",
(long long)ctx->ntfs_ino->mft_no, le32_to_cpu(type));
NVolSetErrors(vol);
return -EIO;
}
void ntfs_attr_name_free(unsigned char **name)
{
if (*name) {
kfree(*name);
*name = NULL;
}
}
char *ntfs_attr_name_get(const struct ntfs_volume *vol, const __le16 *uname,
const int uname_len)
{
unsigned char *name = NULL;
int name_len;
name_len = ntfs_ucstonls(vol, uname, uname_len, &name, 0);
if (name_len < 0) {
ntfs_error(vol->sb, "ntfs_ucstonls error");
ntfs_attr_name_free(&name);
return NULL;
} else if (name_len > 0)
return name;
ntfs_attr_name_free(&name);
return NULL;
}
bool ntfs_attr_list_entry_is_valid(const struct attr_list_entry *ale,
const u8 *al_end)
{
const u8 *al = (const u8 *)ale;
u16 ale_len;
if (al + offsetof(struct attr_list_entry, name) > al_end)
return false;
ale_len = le16_to_cpu(ale->length);
if (ale_len & 7)
return false;
if (ale->name_offset != sizeof(struct attr_list_entry))
return false;
if ((u32)ale->name_offset +
(u32)ale->name_length * sizeof(__le16) > ale_len ||
al + ale_len > al_end)
return false;
if (ale->type == AT_UNUSED)
return false;
if (MSEQNO_LE(ale->mft_reference) == 0)
return false;
return true;
}
bool ntfs_attr_list_is_valid(const u8 *al_start, s64 size)
{
const u8 *al = al_start;
const u8 *al_end = al_start + size;
while (al < al_end) {
const struct attr_list_entry *ale =
(const struct attr_list_entry *)al;
if (!ntfs_attr_list_entry_is_valid(ale, al_end))
return false;
al += le16_to_cpu(ale->length);
}
return al == al_end;
}
int load_attribute_list(struct ntfs_inode *base_ni, u8 *al_start, const s64 size)
{
struct inode *attr_vi = NULL;
if (!al_start || size <= 0)
return -EINVAL;
attr_vi = ntfs_attr_iget(VFS_I(base_ni), AT_ATTRIBUTE_LIST, AT_UNNAMED, 0);
if (IS_ERR(attr_vi)) {
ntfs_error(base_ni->vol->sb,
"Failed to open an inode for Attribute list, mft = %llu",
base_ni->mft_no);
return PTR_ERR(attr_vi);
}
if (ntfs_inode_attr_pread(attr_vi, 0, size, al_start) != size) {
iput(attr_vi);
ntfs_error(base_ni->vol->sb,
"Failed to read attribute list, mft = %llu",
base_ni->mft_no);
return -EIO;
}
iput(attr_vi);
if (!ntfs_attr_list_is_valid(al_start, size)) {
ntfs_error(base_ni->vol->sb, "Corrupt attribute list, mft = %llu",
base_ni->mft_no);
return -EIO;
}
return 0;
}
static int ntfs_external_attr_find(const __le32 type,
const __le16 *name, const u32 name_len,
const u32 ic, const s64 lowest_vcn,
const u8 *val, const u32 val_len, struct ntfs_attr_search_ctx *ctx)
{
struct ntfs_inode *base_ni = ctx->base_ntfs_ino, *ni = ctx->ntfs_ino;
struct ntfs_volume *vol;
struct attr_list_entry *al_entry, *next_al_entry;
u8 *al_start, *al_end;
struct attr_record *a;
__le16 *al_name;
u32 al_name_len;
u32 attr_len, mft_free_len;
bool is_first_search = false;
int err = 0;
static const char *es = " Unmount and run chkdsk.";
ntfs_debug("Entering for inode 0x%llx, type 0x%x.", ni->mft_no, type);
if (!base_ni) {
base_ni = ctx->base_ntfs_ino = ctx->ntfs_ino;
ctx->base_mrec = ctx->mrec;
ctx->mapped_base_mrec = ctx->mapped_mrec;
}
if (ni == base_ni)
ctx->base_attr = ctx->attr;
if (type == AT_END)
goto not_found;
vol = base_ni->vol;
al_start = base_ni->attr_list;
al_end = al_start + base_ni->attr_list_size;
if (!ctx->al_entry) {
ctx->al_entry = (struct attr_list_entry *)al_start;
is_first_search = true;
}
if (ctx->is_first) {
al_entry = ctx->al_entry;
ctx->is_first = false;
if ((type == AT_UNUSED) && is_first_search &&
le32_to_cpu(al_entry->type) >
le32_to_cpu(AT_ATTRIBUTE_LIST))
goto find_attr_list_attr;
} else {
if (((al_end - (u8 *)ctx->al_entry) <
(long)offsetof(struct attr_list_entry, name)) ||
(le16_to_cpu(ctx->al_entry->length) & 7) ||
(le16_to_cpu(ctx->al_entry->length) < offsetof(struct attr_list_entry, name)))
goto corrupt;
al_entry = (struct attr_list_entry *)((u8 *)ctx->al_entry +
le16_to_cpu(ctx->al_entry->length));
if ((u8 *)al_entry == al_end)
goto not_found;
if ((al_end - (u8 *)al_entry) <
(long)offsetof(struct attr_list_entry, name))
goto corrupt;
if ((type == AT_UNUSED) && le32_to_cpu(ctx->al_entry->type) <
le32_to_cpu(AT_ATTRIBUTE_LIST) &&
le32_to_cpu(al_entry->type) >
le32_to_cpu(AT_ATTRIBUTE_LIST)) {
find_attr_list_attr:
if (name || name_len || val || val_len || lowest_vcn)
return -EINVAL;
if (ctx->ntfs_ino != base_ni)
unmap_mft_record(ctx->ntfs_ino);
ctx->ntfs_ino = base_ni;
ctx->mapped_mrec = ctx->mapped_base_mrec;
ctx->mrec = ctx->base_mrec;
ctx->is_first = true;
ctx->attr = (struct attr_record *)((u8 *)ctx->mrec +
le16_to_cpu(ctx->mrec->attrs_offset));
err = ntfs_attr_find(AT_ATTRIBUTE_LIST, NULL, 0,
IGNORE_CASE, NULL, 0, ctx);
ctx->al_entry = al_entry;
ctx->is_first = true;
if (!err)
return 0;
if (err != -ENOENT)
return err;
ntfs_error(ctx->ntfs_ino->vol->sb, "Attribute list wasn't found");
return -EIO;
}
}
for (;; al_entry = next_al_entry) {
if ((u8 *)al_entry < base_ni->attr_list ||
(u8 *)al_entry > al_end)
break;
ctx->al_entry = al_entry;
if ((u8 *)al_entry == al_end)
goto not_found;
if ((((u8 *)al_entry + offsetof(struct attr_list_entry, name)) > al_end) ||
((u8 *)al_entry + le16_to_cpu(al_entry->length) > al_end) ||
(le16_to_cpu(al_entry->length) & 7) ||
(le16_to_cpu(al_entry->length) <
offsetof(struct attr_list_entry, name_length)) ||
(al_entry->name_length && ((u8 *)al_entry + al_entry->name_offset +
al_entry->name_length * sizeof(__le16)) > al_end))
break;
next_al_entry = (struct attr_list_entry *)((u8 *)al_entry +
le16_to_cpu(al_entry->length));
if (type != AT_UNUSED) {
if (le32_to_cpu(al_entry->type) > le32_to_cpu(type))
goto not_found;
if (type != al_entry->type)
continue;
}
al_name_len = al_entry->name_length;
al_name = (__le16 *)((u8 *)al_entry + al_entry->name_offset);
if (type == AT_UNUSED)
goto is_enumeration;
if (!name || name == AT_UNNAMED) {
if (al_name_len)
goto not_found;
} else if (!ntfs_are_names_equal(al_name, al_name_len, name,
name_len, ic, vol->upcase, vol->upcase_len)) {
register int rc;
rc = ntfs_collate_names(name, name_len, al_name,
al_name_len, true, IGNORE_CASE,
vol->upcase, vol->upcase_len);
if (rc == -1)
goto not_found;
if (rc)
continue;
rc = ntfs_collate_names(name, name_len, al_name,
al_name_len, true, CASE_SENSITIVE,
vol->upcase, vol->upcase_len);
if (rc == -1)
goto not_found;
if (rc)
continue;
}
if (lowest_vcn && (u8 *)next_al_entry >= al_start &&
ntfs_attr_list_entry_is_valid(next_al_entry,
al_end) &&
le64_to_cpu(next_al_entry->lowest_vcn) <=
lowest_vcn &&
next_al_entry->type == al_entry->type &&
next_al_entry->name_length == al_name_len &&
ntfs_are_names_equal((__le16 *)((u8 *)
next_al_entry +
next_al_entry->name_offset),
next_al_entry->name_length,
al_name, al_name_len, CASE_SENSITIVE,
vol->upcase, vol->upcase_len))
continue;
is_enumeration:
if (MREF_LE(al_entry->mft_reference) == ni->mft_no) {
if (MSEQNO_LE(al_entry->mft_reference) != ni->seq_no) {
ntfs_error(vol->sb,
"Found stale mft reference in attribute list of base inode 0x%llx.%s",
base_ni->mft_no, es);
err = -EIO;
break;
}
} else {
if (ni != base_ni)
unmap_extent_mft_record(ni);
if (MREF_LE(al_entry->mft_reference) ==
base_ni->mft_no) {
ni = ctx->ntfs_ino = base_ni;
ctx->mrec = ctx->base_mrec;
ctx->mapped_mrec = ctx->mapped_base_mrec;
} else {
ctx->mrec = map_extent_mft_record(base_ni,
le64_to_cpu(
al_entry->mft_reference), &ni);
if (IS_ERR(ctx->mrec)) {
ntfs_error(vol->sb,
"Failed to map extent mft record 0x%lx of base inode 0x%llx.%s",
MREF_LE(al_entry->mft_reference),
base_ni->mft_no, es);
err = PTR_ERR(ctx->mrec);
if (err == -ENOENT)
err = -EIO;
ni = NULL;
break;
}
ctx->ntfs_ino = ni;
ctx->mapped_mrec = true;
}
}
a = ctx->attr = (struct attr_record *)((u8 *)ctx->mrec +
le16_to_cpu(ctx->mrec->attrs_offset));
do_next_attr_loop:
if ((u8 *)a < (u8 *)ctx->mrec ||
(u8 *)a >= (u8 *)ctx->mrec + le32_to_cpu(ctx->mrec->bytes_allocated) ||
(u8 *)a >= (u8 *)ctx->mrec + le32_to_cpu(ctx->mrec->bytes_in_use))
break;
mft_free_len = le32_to_cpu(ctx->mrec->bytes_in_use) -
((u8 *)a - (u8 *)ctx->mrec);
if (mft_free_len >= sizeof(a->type) && a->type == AT_END)
continue;
attr_len = le32_to_cpu(a->length);
if (!attr_len ||
attr_len < offsetof(struct attr_record, data.resident.reserved) +
sizeof(a->data.resident.reserved) ||
attr_len > mft_free_len)
break;
if (al_entry->instance != a->instance)
goto do_next_attr;
if (al_entry->type != a->type)
break;
if (a->name_length && ((le16_to_cpu(a->name_offset) +
a->name_length * sizeof(__le16)) > attr_len))
break;
if (!ntfs_are_names_equal((__le16 *)((u8 *)a +
le16_to_cpu(a->name_offset)), a->name_length,
al_name, al_name_len, CASE_SENSITIVE,
vol->upcase, vol->upcase_len))
break;
ctx->attr = a;
if (!ntfs_attr_value_is_valid(vol, a, ctx->ntfs_ino->mft_no))
break;
if ((type == AT_UNUSED) || !val)
goto attr_found;
if (!a->non_resident) {
u32 value_length = le32_to_cpu(a->data.resident.value_length);
u16 value_offset = le16_to_cpu(a->data.resident.value_offset);
if (value_length == val_len &&
!memcmp((u8 *)a + value_offset, val, val_len)) {
attr_found:
ntfs_debug("Done, found.");
return 0;
}
}
do_next_attr:
a = (struct attr_record *)((u8 *)a + attr_len);
goto do_next_attr_loop;
}
corrupt:
if (ni != base_ni) {
if (ni)
unmap_extent_mft_record(ni);
ctx->ntfs_ino = base_ni;
ctx->mrec = ctx->base_mrec;
ctx->attr = ctx->base_attr;
ctx->mapped_mrec = ctx->mapped_base_mrec;
}
if (!err) {
u64 mft_no = ctx->al_entry ? MREF_LE(ctx->al_entry->mft_reference) : 0;
u32 type = ctx->al_entry ? le32_to_cpu(ctx->al_entry->type) : 0;
ntfs_error(vol->sb,
"Base inode 0x%llx contains corrupt attribute, mft %#llx, type %#x. %s",
(long long)base_ni->mft_no, (long long)mft_no, type,
"Unmount and run chkdsk.");
err = -EIO;
}
if (err != -ENOMEM)
NVolSetErrors(vol);
return err;
not_found:
if (type == AT_UNUSED || type == AT_END) {
ntfs_attr_reinit_search_ctx(ctx);
return ntfs_attr_find(AT_END, name, name_len, ic, val, val_len,
ctx);
}
if (ni != base_ni)
unmap_extent_mft_record(ni);
ctx->mrec = ctx->base_mrec;
ctx->attr = (struct attr_record *)((u8 *)ctx->mrec +
le16_to_cpu(ctx->mrec->attrs_offset));
ctx->is_first = true;
ctx->ntfs_ino = base_ni;
ctx->base_ntfs_ino = NULL;
ctx->base_mrec = NULL;
ctx->base_attr = NULL;
ctx->mapped_mrec = ctx->mapped_base_mrec;
do {
err = ntfs_attr_find(type, name, name_len, ic, val, val_len,
ctx);
} while (!err);
ntfs_debug("Done, not found.");
return err;
}
int ntfs_attr_lookup(const __le32 type, const __le16 *name,
const u32 name_len, const u32 ic,
const s64 lowest_vcn, const u8 *val, const u32 val_len,
struct ntfs_attr_search_ctx *ctx)
{
struct ntfs_inode *base_ni;
ntfs_debug("Entering.");
if (ctx->base_ntfs_ino)
base_ni = ctx->base_ntfs_ino;
else
base_ni = ctx->ntfs_ino;
if (!base_ni || !NInoAttrList(base_ni) || type == AT_ATTRIBUTE_LIST)
return ntfs_attr_find(type, name, name_len, ic, val, val_len,
ctx);
return ntfs_external_attr_find(type, name, name_len, ic, lowest_vcn,
val, val_len, ctx);
}
static bool ntfs_attr_init_search_ctx(struct ntfs_attr_search_ctx *ctx,
struct ntfs_inode *ni, struct mft_record *mrec)
{
if (!mrec) {
mrec = map_mft_record(ni);
if (IS_ERR(mrec))
return false;
ctx->mapped_mrec = true;
} else {
ctx->mapped_mrec = false;
}
ctx->mrec = mrec;
ctx->attr = (struct attr_record *)((u8 *)mrec + le16_to_cpu(mrec->attrs_offset));
ctx->is_first = true;
ctx->ntfs_ino = ni;
ctx->al_entry = NULL;
ctx->base_ntfs_ino = NULL;
ctx->base_mrec = NULL;
ctx->base_attr = NULL;
ctx->mapped_base_mrec = false;
return true;
}
void ntfs_attr_reinit_search_ctx(struct ntfs_attr_search_ctx *ctx)
{
bool mapped_mrec;
if (likely(!ctx->base_ntfs_ino)) {
ctx->is_first = true;
ctx->attr = (struct attr_record *)((u8 *)ctx->mrec +
le16_to_cpu(ctx->mrec->attrs_offset));
ctx->al_entry = NULL;
return;
}
if (ctx->ntfs_ino != ctx->base_ntfs_ino && ctx->ntfs_ino)
unmap_extent_mft_record(ctx->ntfs_ino);
mapped_mrec = ctx->mapped_base_mrec;
ntfs_attr_init_search_ctx(ctx, ctx->base_ntfs_ino, ctx->base_mrec);
ctx->mapped_mrec = mapped_mrec;
}
struct ntfs_attr_search_ctx *ntfs_attr_get_search_ctx(struct ntfs_inode *ni,
struct mft_record *mrec)
{
struct ntfs_attr_search_ctx *ctx;
bool init;
ctx = kmem_cache_alloc(ntfs_attr_ctx_cache, GFP_NOFS);
if (ctx) {
init = ntfs_attr_init_search_ctx(ctx, ni, mrec);
if (init == false) {
kmem_cache_free(ntfs_attr_ctx_cache, ctx);
ctx = NULL;
}
}
return ctx;
}
void ntfs_attr_put_search_ctx(struct ntfs_attr_search_ctx *ctx)
{
if (ctx->mapped_mrec)
unmap_mft_record(ctx->ntfs_ino);
if (ctx->mapped_base_mrec && ctx->base_ntfs_ino &&
ctx->ntfs_ino != ctx->base_ntfs_ino)
unmap_extent_mft_record(ctx->base_ntfs_ino);
kmem_cache_free(ntfs_attr_ctx_cache, ctx);
}
static struct attr_def *ntfs_attr_find_in_attrdef(const struct ntfs_volume *vol,
const __le32 type)
{
struct attr_def *ad;
WARN_ON(!type);
for (ad = vol->attrdef; (u8 *)ad - (u8 *)vol->attrdef <=
vol->attrdef_size - (s32)sizeof(*ad) && ad->type; ++ad) {
if (likely(le32_to_cpu(ad->type) < le32_to_cpu(type)))
continue;
if (likely(ad->type == type))
return ad;
break;
}
ntfs_debug("Attribute type 0x%x not found in $AttrDef.",
le32_to_cpu(type));
return NULL;
}
int ntfs_attr_size_bounds_check(const struct ntfs_volume *vol, const __le32 type,
const s64 size)
{
struct attr_def *ad;
if (size < 0)
return -EINVAL;
if (unlikely(type == AT_ATTRIBUTE_LIST && size > NTFS_MAX_ATTR_LIST_SIZE))
return -ERANGE;
ad = ntfs_attr_find_in_attrdef(vol, type);
if (unlikely(!ad))
return -ENOENT;
if (((le64_to_cpu(ad->min_size) > 0) &&
size < le64_to_cpu(ad->min_size)) ||
((le64_to_cpu(ad->max_size) > 0) && size >
le64_to_cpu(ad->max_size)))
return -ERANGE;
return 0;
}
static int ntfs_attr_can_be_non_resident(const struct ntfs_volume *vol,
const __le32 type)
{
struct attr_def *ad;
ad = ntfs_attr_find_in_attrdef(vol, type);
if (unlikely(!ad))
return -ENOENT;
if (ad->flags & ATTR_DEF_RESIDENT)
return -EPERM;
return 0;
}
int ntfs_attr_can_be_resident(const struct ntfs_volume *vol, const __le32 type)
{
if (type == AT_INDEX_ALLOCATION)
return -EPERM;
return 0;
}
int ntfs_attr_record_resize(struct mft_record *m, struct attr_record *a, u32 new_size)
{
u32 old_size, alloc_size, attr_size;
old_size = le32_to_cpu(m->bytes_in_use);
alloc_size = le32_to_cpu(m->bytes_allocated);
attr_size = le32_to_cpu(a->length);
ntfs_debug("Sizes: old=%u alloc=%u attr=%u new=%u\n",
(unsigned int)old_size, (unsigned int)alloc_size,
(unsigned int)attr_size, (unsigned int)new_size);
if (new_size & 7)
new_size = (new_size + 7) & ~7;
if (new_size != attr_size) {
u32 new_muse = le32_to_cpu(m->bytes_in_use) -
attr_size + new_size;
if (new_muse > le32_to_cpu(m->bytes_allocated))
return -ENOSPC;
if (a->type == AT_INDEX_ROOT && new_size > attr_size &&
new_muse + 120 > alloc_size && old_size + 120 <= alloc_size) {
ntfs_debug("Too big struct index_root (%u > %u)\n",
new_muse, alloc_size);
return -ENOSPC;
}
memmove((u8 *)a + new_size, (u8 *)a + le32_to_cpu(a->length),
le32_to_cpu(m->bytes_in_use) - ((u8 *)a -
(u8 *)m) - attr_size);
m->bytes_in_use = cpu_to_le32(new_muse);
if (new_size >= offsetof(struct attr_record, length) + sizeof(a->length))
a->length = cpu_to_le32(new_size);
}
return 0;
}
int ntfs_resident_attr_value_resize(struct mft_record *m, struct attr_record *a,
const u32 new_size)
{
u32 old_size;
if (ntfs_attr_record_resize(m, a,
le16_to_cpu(a->data.resident.value_offset) + new_size))
return -ENOSPC;
old_size = le32_to_cpu(a->data.resident.value_length);
if (new_size > old_size)
memset((u8 *)a + le16_to_cpu(a->data.resident.value_offset) +
old_size, 0, new_size - old_size);
a->data.resident.value_length = cpu_to_le32(new_size);
return 0;
}
int ntfs_attr_make_non_resident(struct ntfs_inode *ni, const u32 data_size)
{
s64 new_size;
struct inode *vi = VFS_I(ni);
struct ntfs_volume *vol = ni->vol;
struct ntfs_inode *base_ni;
struct mft_record *m;
struct attr_record *a;
struct ntfs_attr_search_ctx *ctx;
struct folio *folio;
struct runlist_element *rl;
unsigned long flags;
int mp_size, mp_ofs, name_ofs, arec_size, err, err2;
u32 attr_size;
u8 old_res_attr_flags;
if (NInoNonResident(ni)) {
ntfs_warning(vol->sb,
"Trying to make non-resident attribute non-resident. Aborting...\n");
return -EINVAL;
}
err = ntfs_attr_can_be_non_resident(vol, ni->type);
if (unlikely(err)) {
if (err == -EPERM)
ntfs_debug("Attribute is not allowed to be non-resident.");
else
ntfs_debug("Attribute not defined on the NTFS volume!");
return err;
}
if (NInoEncrypted(ni))
return -EIO;
if (!NInoAttr(ni))
base_ni = ni;
else
base_ni = ni->ext.base_ntfs_ino;
m = map_mft_record(base_ni);
if (IS_ERR(m)) {
err = PTR_ERR(m);
m = NULL;
ctx = NULL;
goto err_out;
}
ctx = ntfs_attr_get_search_ctx(base_ni, m);
if (unlikely(!ctx)) {
err = -ENOMEM;
goto err_out;
}
err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
CASE_SENSITIVE, 0, NULL, 0, ctx);
if (unlikely(err)) {
if (err == -ENOENT)
err = -EIO;
goto err_out;
}
m = ctx->mrec;
a = ctx->attr;
new_size = (data_size + vol->cluster_size - 1) &
~(vol->cluster_size - 1);
if (new_size > 0) {
if ((a->flags & ATTR_COMPRESSION_MASK) == ATTR_IS_COMPRESSED) {
new_size =
((new_size - 1) |
((1L << (STANDARD_COMPRESSION_UNIT +
vol->cluster_size_bits)) - 1)) + 1;
}
folio = __filemap_get_folio(vi->i_mapping, 0,
FGP_CREAT | FGP_LOCK,
mapping_gfp_mask(vi->i_mapping));
if (IS_ERR(folio)) {
err = -ENOMEM;
goto err_out;
}
rl = ntfs_cluster_alloc(vol, 0,
ntfs_bytes_to_cluster(vol, new_size),
-1, DATA_ZONE, true, false, false);
if (IS_ERR(rl)) {
err = PTR_ERR(rl);
ntfs_debug("Failed to allocate cluster%s, error code %i.",
str_plural(ntfs_bytes_to_cluster(vol, new_size)),
err);
goto folio_err_out;
}
} else {
rl = NULL;
folio = NULL;
}
down_write(&ni->runlist.lock);
mp_size = ntfs_get_size_for_mapping_pairs(vol, rl, 0, -1, -1);
if (unlikely(mp_size < 0)) {
err = mp_size;
ntfs_debug("Failed to get size for mapping pairs array, error code %i.\n", err);
goto rl_err_out;
}
if (NInoNonResident(ni) || a->non_resident) {
err = -EIO;
goto rl_err_out;
}
if (NInoSparse(ni) || NInoCompressed(ni))
name_ofs = (offsetof(struct attr_record,
data.non_resident.compressed_size) +
sizeof(a->data.non_resident.compressed_size) +
7) & ~7;
else
name_ofs = (offsetof(struct attr_record,
data.non_resident.compressed_size) + 7) & ~7;
mp_ofs = (name_ofs + a->name_length * sizeof(__le16) + 7) & ~7;
arec_size = (mp_ofs + mp_size + 7) & ~7;
attr_size = le32_to_cpu(a->data.resident.value_length);
WARN_ON(attr_size != data_size);
if (folio && !folio_test_uptodate(folio)) {
folio_fill_tail(folio, 0, (u8 *)a +
le16_to_cpu(a->data.resident.value_offset),
attr_size);
folio_mark_uptodate(folio);
}
old_res_attr_flags = a->data.resident.flags;
err = ntfs_attr_record_resize(m, a, arec_size);
if (unlikely(err))
goto rl_err_out;
a->non_resident = 1;
if (a->name_length)
memmove((u8 *)a + name_ofs, (u8 *)a + le16_to_cpu(a->name_offset),
a->name_length * sizeof(__le16));
a->name_offset = cpu_to_le16(name_ofs);
a->data.non_resident.lowest_vcn = 0;
a->data.non_resident.highest_vcn =
cpu_to_le64(ntfs_bytes_to_cluster(vol, new_size - 1));
a->data.non_resident.mapping_pairs_offset = cpu_to_le16(mp_ofs);
memset(&a->data.non_resident.reserved, 0,
sizeof(a->data.non_resident.reserved));
a->data.non_resident.allocated_size = cpu_to_le64(new_size);
a->data.non_resident.data_size =
a->data.non_resident.initialized_size =
cpu_to_le64(attr_size);
if (NInoSparse(ni) || NInoCompressed(ni)) {
a->data.non_resident.compression_unit = 0;
if (NInoCompressed(ni) || vol->major_ver < 3)
a->data.non_resident.compression_unit = 4;
a->data.non_resident.compressed_size =
a->data.non_resident.allocated_size;
} else
a->data.non_resident.compression_unit = 0;
err = ntfs_mapping_pairs_build(vol, (u8 *)a + mp_ofs,
arec_size - mp_ofs, rl, 0, -1, NULL, NULL, NULL);
if (unlikely(err)) {
ntfs_error(vol->sb, "Failed to build mapping pairs, error code %i.",
err);
goto undo_err_out;
}
ni->runlist.rl = rl;
if (rl) {
for (ni->runlist.count = 1; rl->length != 0; rl++)
ni->runlist.count++;
} else
ni->runlist.count = 0;
write_lock_irqsave(&ni->size_lock, flags);
ni->allocated_size = new_size;
if ((NInoSparse(ni) && !NInoWofCompressed(ni)) || NInoCompressed(ni)) {
ni->itype.compressed.size = ni->allocated_size;
if (a->data.non_resident.compression_unit) {
ni->itype.compressed.block_size = 1U <<
(a->data.non_resident.compression_unit +
vol->cluster_size_bits);
ni->itype.compressed.block_size_bits =
ffs(ni->itype.compressed.block_size) -
1;
ni->itype.compressed.block_clusters = 1U <<
a->data.non_resident.compression_unit;
} else {
ni->itype.compressed.block_size = 0;
ni->itype.compressed.block_size_bits = 0;
ni->itype.compressed.block_clusters = 0;
}
vi->i_blocks = ni->itype.compressed.size >> 9;
} else
vi->i_blocks = ni->allocated_size >> 9;
write_unlock_irqrestore(&ni->size_lock, flags);
NInoSetNonResident(ni);
NInoSetFullyMapped(ni);
mark_mft_record_dirty(ctx->ntfs_ino);
ntfs_attr_put_search_ctx(ctx);
unmap_mft_record(base_ni);
up_write(&ni->runlist.lock);
if (folio) {
iomap_dirty_folio(vi->i_mapping, folio);
folio_unlock(folio);
folio_put(folio);
}
ntfs_debug("Done.");
return 0;
undo_err_out:
a->non_resident = 0;
name_ofs = (offsetof(struct attr_record, data.resident.reserved) +
sizeof(a->data.resident.reserved) + 7) & ~7;
if (a->name_length)
memmove((u8 *)a + name_ofs, (u8 *)a + le16_to_cpu(a->name_offset),
a->name_length * sizeof(__le16));
mp_ofs = (name_ofs + a->name_length * sizeof(__le16) + 7) & ~7;
a->name_offset = cpu_to_le16(name_ofs);
arec_size = (mp_ofs + attr_size + 7) & ~7;
err2 = ntfs_attr_record_resize(m, a, arec_size);
if (unlikely(err2)) {
arec_size = le32_to_cpu(a->length);
if ((mp_ofs + attr_size) > arec_size) {
err2 = attr_size;
attr_size = arec_size - mp_ofs;
ntfs_error(vol->sb,
"Failed to undo partial resident to non-resident attribute conversion. Truncating inode 0x%llx, attribute type 0x%x from %i bytes to %i bytes to maintain metadata consistency. THIS MEANS YOU ARE LOSING %i BYTES DATA FROM THIS %s.",
ni->mft_no,
(unsigned int)le32_to_cpu(ni->type),
err2, attr_size, err2 - attr_size,
((ni->type == AT_DATA) &&
!ni->name_len) ? "FILE" : "ATTRIBUTE");
write_lock_irqsave(&ni->size_lock, flags);
ni->initialized_size = attr_size;
i_size_write(vi, attr_size);
write_unlock_irqrestore(&ni->size_lock, flags);
}
}
a->data.resident.value_length = cpu_to_le32(attr_size);
a->data.resident.value_offset = cpu_to_le16(mp_ofs);
a->data.resident.flags = old_res_attr_flags;
memset(&a->data.resident.reserved, 0,
sizeof(a->data.resident.reserved));
if (folio)
memcpy_from_folio((u8 *)a + mp_ofs, folio, 0, attr_size);
write_lock_irqsave(&ni->size_lock, flags);
ni->allocated_size = arec_size - mp_ofs;
write_unlock_irqrestore(&ni->size_lock, flags);
mark_mft_record_dirty(ctx->ntfs_ino);
rl_err_out:
up_write(&ni->runlist.lock);
if (rl) {
if (ntfs_cluster_free_from_rl(vol, rl) < 0) {
ntfs_error(vol->sb,
"Failed to release allocated cluster(s) in error code path. Run chkdsk to recover the lost cluster(s).");
NVolSetErrors(vol);
}
kvfree(rl);
folio_err_out:
folio_unlock(folio);
folio_put(folio);
}
err_out:
if (ctx)
ntfs_attr_put_search_ctx(ctx);
if (m)
unmap_mft_record(base_ni);
ni->runlist.rl = NULL;
if (err == -EINVAL)
err = -EIO;
return err;
}
int ntfs_attr_set(struct ntfs_inode *ni, s64 ofs, s64 cnt, const u8 val)
{
struct address_space *mapping = VFS_I(ni)->i_mapping;
struct folio *folio;
pgoff_t index;
u8 *addr;
unsigned long offset;
size_t attr_len;
int ret = 0;
index = ofs >> PAGE_SHIFT;
while (cnt) {
folio = read_mapping_folio(mapping, index, NULL);
if (IS_ERR(folio)) {
ret = PTR_ERR(folio);
ntfs_error(VFS_I(ni)->i_sb, "Failed to read a page %lu for attr %#x: %ld",
index, ni->type, PTR_ERR(folio));
break;
}
offset = offset_in_folio(folio, ofs);
attr_len = min_t(size_t, (size_t)cnt, folio_size(folio) - offset);
folio_lock(folio);
addr = kmap_local_folio(folio, offset);
memset(addr, val, attr_len);
kunmap_local(addr);
folio_mark_dirty(folio);
folio_unlock(folio);
folio_put(folio);
ofs += attr_len;
cnt -= attr_len;
index++;
cond_resched();
}
return ret;
}
int ntfs_attr_set_initialized_size(struct ntfs_inode *ni, loff_t new_size)
{
struct ntfs_attr_search_ctx *ctx;
struct ntfs_inode *base_ni = ntfs_base_inode(ni);
int err = 0;
if (!NInoNonResident(ni))
return -EINVAL;
ctx = ntfs_attr_get_search_ctx(base_ni, NULL);
if (!ctx)
return -ENOMEM;
err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
CASE_SENSITIVE, 0, NULL, 0, ctx);
if (err)
goto out_ctx;
ctx->attr->data.non_resident.initialized_size = cpu_to_le64(new_size);
ni->initialized_size = new_size;
mark_mft_record_dirty(ctx->ntfs_ino);
out_ctx:
ntfs_attr_put_search_ctx(ctx);
return err;
}
static int ntfs_make_room_for_attr(struct mft_record *m, u8 *pos, u32 size)
{
u32 biu;
ntfs_debug("Entering for pos 0x%x, size %u.\n",
(int)(pos - (u8 *)m), (unsigned int) size);
size = (size + 7) & ~7;
if (!m || !pos || pos < (u8 *)m) {
pr_err("%s: pos=%p m=%p\n", __func__, pos, m);
return -EINVAL;
}
if (pos - (u8 *)m > (int)le32_to_cpu(m->bytes_in_use) - 8)
return -EINVAL;
if (!size)
return 0;
biu = le32_to_cpu(m->bytes_in_use);
if (biu + size > le32_to_cpu(m->bytes_allocated) ||
pos + size > (u8 *)m + le32_to_cpu(m->bytes_allocated)) {
ntfs_debug("No enough space in the MFT record\n");
return -ENOSPC;
}
memmove(pos + size, pos, biu - (pos - (u8 *)m));
m->bytes_in_use = cpu_to_le32(biu + size);
return 0;
}
int ntfs_resident_attr_record_add(struct ntfs_inode *ni, __le32 type,
__le16 *name, u8 name_len, u8 *val, u32 size,
__le16 flags)
{
struct ntfs_attr_search_ctx *ctx;
u32 length;
struct attr_record *a;
struct mft_record *m;
int err, offset;
struct ntfs_inode *base_ni;
u32 ic;
if (!ni || (!name && name_len))
return -EINVAL;
ntfs_debug("Entering for inode 0x%llx, attr 0x%x, flags 0x%x.\n",
(long long) ni->mft_no, (unsigned int) le32_to_cpu(type),
(unsigned int) le16_to_cpu(flags));
err = ntfs_attr_can_be_resident(ni->vol, type);
if (err) {
if (err == -EPERM)
ntfs_debug("Attribute can't be resident.\n");
else
ntfs_debug("ntfs_attr_can_be_resident failed.\n");
return err;
}
ctx = ntfs_attr_get_search_ctx(ni, NULL);
if (!ctx) {
ntfs_error(ni->vol->sb, "%s: Failed to get search context",
__func__);
return -ENOMEM;
}
ic = type == AT_DATA && name_len ? IGNORE_CASE : CASE_SENSITIVE;
err = ntfs_attr_find(type, name, name_len, ic, val, size, ctx);
if (!err) {
err = -EEXIST;
ntfs_debug("Attribute already present.\n");
goto put_err_out;
}
if (err != -ENOENT) {
err = -EIO;
goto put_err_out;
}
a = ctx->attr;
m = ctx->mrec;
length = offsetof(struct attr_record, data.resident.reserved) +
sizeof(a->data.resident.reserved) +
((name_len * sizeof(__le16) + 7) & ~7) +
((size + 7) & ~7);
err = ntfs_make_room_for_attr(ctx->mrec, (u8 *) ctx->attr, length);
if (err) {
ntfs_debug("Failed to make room for attribute.\n");
goto put_err_out;
}
offset = ((u8 *)a - (u8 *)m);
a->type = type;
a->length = cpu_to_le32(length);
a->non_resident = 0;
a->name_length = name_len;
a->name_offset =
name_len ? cpu_to_le16((offsetof(struct attr_record, data.resident.reserved) +
sizeof(a->data.resident.reserved))) : cpu_to_le16(0);
a->flags = flags;
a->instance = m->next_attr_instance;
a->data.resident.value_length = cpu_to_le32(size);
a->data.resident.value_offset = cpu_to_le16(length - ((size + 7) & ~7));
if (val)
memcpy((u8 *)a + le16_to_cpu(a->data.resident.value_offset), val, size);
else
memset((u8 *)a + le16_to_cpu(a->data.resident.value_offset), 0, size);
if (type == AT_FILE_NAME)
a->data.resident.flags = RESIDENT_ATTR_IS_INDEXED;
else
a->data.resident.flags = 0;
if (name_len)
memcpy((u8 *)a + le16_to_cpu(a->name_offset),
name, sizeof(__le16) * name_len);
m->next_attr_instance =
cpu_to_le16((le16_to_cpu(m->next_attr_instance) + 1) & 0xffff);
if (ni->nr_extents == -1)
base_ni = ni->ext.base_ntfs_ino;
else
base_ni = ni;
if (type != AT_ATTRIBUTE_LIST && NInoAttrList(base_ni)) {
err = ntfs_attrlist_entry_add(ni, a);
if (err) {
ntfs_attr_record_resize(m, a, 0);
mark_mft_record_dirty(ctx->ntfs_ino);
ntfs_debug("Failed add attribute entry to ATTRIBUTE_LIST.\n");
goto put_err_out;
}
}
mark_mft_record_dirty(ni);
ntfs_attr_put_search_ctx(ctx);
return offset;
put_err_out:
ntfs_attr_put_search_ctx(ctx);
return err;
}
static int ntfs_non_resident_attr_record_add(struct ntfs_inode *ni, __le32 type,
__le16 *name, u8 name_len, s64 lowest_vcn, int dataruns_size,
__le16 flags)
{
struct ntfs_attr_search_ctx *ctx;
u32 length;
struct attr_record *a;
struct mft_record *m;
struct ntfs_inode *base_ni;
int err, offset;
u32 ic;
if (!ni || dataruns_size <= 0 || (!name && name_len))
return -EINVAL;
ntfs_debug("Entering for inode 0x%llx, attr 0x%x, lowest_vcn %lld, dataruns_size %d, flags 0x%x.\n",
(long long) ni->mft_no, (unsigned int) le32_to_cpu(type),
(long long) lowest_vcn, dataruns_size,
(unsigned int) le16_to_cpu(flags));
err = ntfs_attr_can_be_non_resident(ni->vol, type);
if (err) {
if (err == -EPERM)
pr_err("Attribute can't be non resident\n");
else
pr_err("ntfs_attr_can_be_non_resident failed\n");
return err;
}
ctx = ntfs_attr_get_search_ctx(ni, NULL);
if (!ctx) {
pr_err("%s: Failed to get search context\n", __func__);
return -ENOMEM;
}
ic = type == AT_DATA && name_len ? IGNORE_CASE : CASE_SENSITIVE;
err = ntfs_attr_find(type, name, name_len, ic, NULL, 0, ctx);
if (!err) {
err = -EEXIST;
pr_err("Attribute 0x%x already present\n", type);
goto put_err_out;
}
if (err != -ENOENT) {
pr_err("ntfs_attr_find failed\n");
err = -EIO;
goto put_err_out;
}
a = ctx->attr;
m = ctx->mrec;
dataruns_size = (dataruns_size + 7) & ~7;
length = offsetof(struct attr_record, data.non_resident.compressed_size) +
((sizeof(__le16) * name_len + 7) & ~7) + dataruns_size +
((flags & (ATTR_IS_COMPRESSED | ATTR_IS_SPARSE)) ?
sizeof(a->data.non_resident.compressed_size) : 0);
err = ntfs_make_room_for_attr(ctx->mrec, (u8 *) ctx->attr, length);
if (err) {
pr_err("Failed to make room for attribute\n");
goto put_err_out;
}
a->type = type;
a->length = cpu_to_le32(length);
a->non_resident = 1;
a->name_length = name_len;
a->name_offset = cpu_to_le16(offsetof(struct attr_record,
data.non_resident.compressed_size) +
((flags & (ATTR_IS_COMPRESSED | ATTR_IS_SPARSE)) ?
sizeof(a->data.non_resident.compressed_size) : 0));
a->flags = flags;
a->instance = m->next_attr_instance;
a->data.non_resident.lowest_vcn = cpu_to_le64(lowest_vcn);
a->data.non_resident.mapping_pairs_offset = cpu_to_le16(length - dataruns_size);
a->data.non_resident.compression_unit =
(flags & ATTR_IS_COMPRESSED) ? STANDARD_COMPRESSION_UNIT : 0;
if (!lowest_vcn) {
a->data.non_resident.highest_vcn = cpu_to_le64(-1);
a->data.non_resident.allocated_size = 0;
a->data.non_resident.data_size = 0;
a->data.non_resident.initialized_size = 0;
*((u8 *)a + le16_to_cpu(a->data.non_resident.mapping_pairs_offset)) = 0;
}
if (name_len)
memcpy((u8 *)a + le16_to_cpu(a->name_offset),
name, sizeof(__le16) * name_len);
m->next_attr_instance =
cpu_to_le16((le16_to_cpu(m->next_attr_instance) + 1) & 0xffff);
if (ni->nr_extents == -1)
base_ni = ni->ext.base_ntfs_ino;
else
base_ni = ni;
if (type != AT_ATTRIBUTE_LIST && NInoAttrList(base_ni)) {
err = ntfs_attrlist_entry_add(ni, a);
if (err) {
pr_err("Failed add attr entry to attrlist\n");
ntfs_attr_record_resize(m, a, 0);
goto put_err_out;
}
}
mark_mft_record_dirty(ni);
ntfs_attr_reinit_search_ctx(ctx);
err = ntfs_attr_lookup(type, name, name_len, ic,
lowest_vcn, NULL, 0, ctx);
if (err) {
pr_err("%s: attribute lookup failed\n", __func__);
ntfs_attr_put_search_ctx(ctx);
return err;
}
offset = (u8 *)ctx->attr - (u8 *)ctx->mrec;
ntfs_attr_put_search_ctx(ctx);
return offset;
put_err_out:
ntfs_attr_put_search_ctx(ctx);
return err;
}
int ntfs_attr_record_rm(struct ntfs_attr_search_ctx *ctx)
{
struct ntfs_inode *base_ni, *ni;
__le32 type;
int err;
if (!ctx || !ctx->ntfs_ino || !ctx->mrec || !ctx->attr)
return -EINVAL;
ntfs_debug("Entering for inode 0x%llx, attr 0x%x.\n",
(long long) ctx->ntfs_ino->mft_no,
(unsigned int) le32_to_cpu(ctx->attr->type));
type = ctx->attr->type;
ni = ctx->ntfs_ino;
if (ctx->base_ntfs_ino)
base_ni = ctx->base_ntfs_ino;
else
base_ni = ctx->ntfs_ino;
if (ntfs_attr_record_resize(ctx->mrec, ctx->attr, 0)) {
ntfs_debug("Couldn't remove attribute record. Bug or damaged MFT record.\n");
return -EIO;
}
mark_mft_record_dirty(ni);
if (NInoAttrList(base_ni) && type != AT_ATTRIBUTE_LIST) {
err = ntfs_attrlist_entry_rm(ctx);
if (err) {
ntfs_debug("Couldn't delete record from $ATTRIBUTE_LIST.\n");
return err;
}
}
if (type == AT_ATTRIBUTE_LIST) {
if (NInoAttrList(base_ni) && base_ni->attr_list)
kvfree(base_ni->attr_list);
base_ni->attr_list = NULL;
NInoClearAttrList(base_ni);
}
if (le32_to_cpu(ctx->mrec->bytes_in_use) -
le16_to_cpu(ctx->mrec->attrs_offset) == 8) {
if (ntfs_mft_record_free(ni->vol, ni)) {
ntfs_debug("Couldn't free MFT record.\n");
return -EIO;
}
if (ni == base_ni)
return 0;
ntfs_inode_close(ni);
ctx->ntfs_ino = ni = NULL;
}
if (type == AT_ATTRIBUTE_LIST || !NInoAttrList(base_ni))
return 0;
if (!ntfs_attrlist_need(base_ni)) {
struct ntfs_attr na;
struct inode *attr_vi;
ntfs_attr_reinit_search_ctx(ctx);
if (ntfs_attr_lookup(AT_ATTRIBUTE_LIST, NULL, 0, CASE_SENSITIVE,
0, NULL, 0, ctx)) {
ntfs_debug("Couldn't find attribute list. Succeed anyway.\n");
return 0;
}
if (ctx->attr->non_resident) {
struct runlist_element *al_rl;
size_t new_rl_count;
al_rl = ntfs_mapping_pairs_decompress(base_ni->vol,
ctx->attr, NULL, &new_rl_count);
if (IS_ERR(al_rl)) {
ntfs_debug("Couldn't decompress attribute list runlist. Succeed anyway.\n");
return 0;
}
if (ntfs_cluster_free_from_rl(base_ni->vol, al_rl))
ntfs_debug("Leaking clusters! Run chkdsk. Couldn't free clusters from attribute list runlist.\n");
kvfree(al_rl);
}
if (ntfs_attr_record_rm(ctx)) {
ntfs_debug("Couldn't remove attribute list. Succeed anyway.\n");
return 0;
}
na.mft_no = VFS_I(base_ni)->i_ino;
na.type = AT_ATTRIBUTE_LIST;
na.name = NULL;
na.name_len = 0;
attr_vi = ilookup5(VFS_I(base_ni)->i_sb, VFS_I(base_ni)->i_ino,
ntfs_test_inode, &na);
if (attr_vi) {
clear_nlink(attr_vi);
iput(attr_vi);
}
}
return 0;
}
int ntfs_attr_add(struct ntfs_inode *ni, __le32 type,
__le16 *name, u8 name_len, u8 *val, s64 size)
{
struct super_block *sb;
u32 attr_rec_size;
int err, i, offset;
bool is_resident;
bool can_be_non_resident = false;
struct ntfs_inode *attr_ni;
struct inode *attr_vi;
struct mft_record *ni_mrec;
if (!ni || size < 0 || type == AT_ATTRIBUTE_LIST)
return -EINVAL;
ntfs_debug("Entering for inode 0x%llx, attr %x, size %lld.\n",
(long long) ni->mft_no, type, size);
if (ni->nr_extents == -1)
ni = ni->ext.base_ntfs_ino;
err = ntfs_attr_size_bounds_check(ni->vol, type, size);
if (err) {
if (err == -ENOENT)
err = -EIO;
return err;
}
sb = ni->vol->sb;
err = ntfs_attr_can_be_non_resident(ni->vol, type);
if (err) {
if (err != -EPERM) {
ntfs_error(sb, "ntfs_attr_can_be_non_resident failed");
goto err_out;
}
if (!val) {
ntfs_error(sb,
"val is mandatory for always resident attributes");
return -EINVAL;
}
if (size > ni->vol->mft_record_size) {
ntfs_error(sb, "Attribute is too big");
return -ERANGE;
}
} else
can_be_non_resident = true;
err = ntfs_attr_can_be_resident(ni->vol, type);
if (!err) {
is_resident = true;
} else {
if (err != -EPERM) {
ntfs_error(sb, "ntfs_attr_can_be_resident failed");
goto err_out;
}
is_resident = false;
}
if (is_resident)
attr_rec_size = offsetof(struct attr_record, data.resident.reserved) +
1 +
((name_len * sizeof(__le16) + 7) & ~7) +
((size + 7) & ~7);
else
attr_rec_size = offsetof(struct attr_record, data.non_resident.compressed_size) +
((name_len * sizeof(__le16) + 7) & ~7) + 8;
retry:
ni_mrec = map_mft_record(ni);
if (IS_ERR(ni_mrec)) {
err = -EIO;
goto err_out;
}
if (le32_to_cpu(ni_mrec->bytes_allocated) -
le32_to_cpu(ni_mrec->bytes_in_use) >= attr_rec_size) {
attr_ni = ni;
unmap_mft_record(ni);
goto add_attr_record;
}
unmap_mft_record(ni);
err = ntfs_inode_attach_all_extents(ni);
if (err) {
ntfs_error(sb, "Failed to attach all extents to inode");
goto err_out;
}
for (i = 0; i < ni->nr_extents; i++) {
attr_ni = ni->ext.extent_ntfs_inos[i];
ni_mrec = map_mft_record(attr_ni);
if (IS_ERR(ni_mrec)) {
err = -EIO;
goto err_out;
}
if (le32_to_cpu(ni_mrec->bytes_allocated) -
le32_to_cpu(ni_mrec->bytes_in_use) >=
attr_rec_size) {
unmap_mft_record(attr_ni);
goto add_attr_record;
}
unmap_mft_record(attr_ni);
}
if (!NInoAttrList(ni)) {
err = ntfs_inode_add_attrlist(ni);
if (err) {
ntfs_error(sb, "Failed to add attribute list");
goto err_out;
}
goto retry;
}
attr_ni = NULL;
err = ntfs_mft_record_alloc(ni->vol, 0, &attr_ni, ni, NULL, -1);
if (err) {
ntfs_error(sb, "Failed to allocate extent record");
goto err_out;
}
unmap_mft_record(attr_ni);
add_attr_record:
if (is_resident) {
offset = ntfs_resident_attr_record_add(attr_ni, type, name,
name_len, val, size, 0);
if (offset < 0) {
if (offset == -ENOSPC && can_be_non_resident)
goto add_non_resident;
err = offset;
ntfs_error(sb, "Failed to add resident attribute");
goto free_err_out;
}
return 0;
}
add_non_resident:
offset = ntfs_non_resident_attr_record_add(attr_ni, type, name,
name_len, 0, 8, 0);
if (offset < 0) {
err = offset;
ntfs_error(sb, "Failed to add non resident attribute");
goto free_err_out;
}
if (!size)
return 0;
attr_vi = ntfs_attr_iget(VFS_I(ni), type, name, name_len);
if (IS_ERR(attr_vi)) {
err = PTR_ERR(attr_vi);
ntfs_error(sb, "Failed to open just added attribute");
goto rm_attr_err_out;
}
attr_ni = NTFS_I(attr_vi);
if (ntfs_attr_truncate(attr_ni, size) ||
(val && (ntfs_inode_attr_pwrite(attr_vi, 0, size, val, false) != size))) {
err = -EIO;
ntfs_error(sb, "Failed to initialize just added attribute");
if (ntfs_attr_rm(attr_ni))
ntfs_error(sb, "Failed to remove just added attribute");
iput(attr_vi);
goto err_out;
}
iput(attr_vi);
return 0;
rm_attr_err_out:
ni_mrec = map_mft_record(attr_ni);
if (!IS_ERR(ni_mrec)) {
if (ntfs_attr_record_resize(ni_mrec,
(struct attr_record *)((u8 *)ni_mrec + offset), 0))
ntfs_error(sb, "Failed to remove just added attribute #2");
unmap_mft_record(attr_ni);
} else
pr_err("EIO when try to remove new added attr\n");
free_err_out:
ni_mrec = map_mft_record(attr_ni);
if (!IS_ERR(ni_mrec)) {
int attr_size;
attr_size = le32_to_cpu(ni_mrec->bytes_in_use) -
le16_to_cpu(ni_mrec->attrs_offset);
unmap_mft_record(attr_ni);
if (attr_size == 8) {
if (ntfs_mft_record_free(attr_ni->vol, attr_ni))
ntfs_error(sb, "Failed to free MFT record");
if (attr_ni->nr_extents < 0)
ntfs_inode_close(attr_ni);
}
} else
pr_err("EIO when testing mft record is free-able\n");
err_out:
return err;
}
static void __ntfs_attr_init(struct ntfs_inode *ni,
const __le32 type, __le16 *name, const u32 name_len)
{
ni->runlist.rl = NULL;
ni->type = type;
ni->name = name;
if (name)
ni->name_len = name_len;
else
ni->name_len = 0;
}
static void ntfs_attr_init(struct ntfs_inode *ni, const bool non_resident,
const bool compressed, const bool encrypted, const bool sparse,
const s64 allocated_size, const s64 data_size,
const s64 initialized_size, const s64 compressed_size,
const u8 compression_unit)
{
if (non_resident)
NInoSetNonResident(ni);
if (compressed) {
NInoSetCompressed(ni);
ni->flags |= FILE_ATTR_COMPRESSED;
}
if (encrypted) {
NInoSetEncrypted(ni);
ni->flags |= FILE_ATTR_ENCRYPTED;
}
if (sparse) {
NInoSetSparse(ni);
ni->flags |= FILE_ATTR_SPARSE_FILE;
}
ni->allocated_size = allocated_size;
ni->data_size = data_size;
ni->initialized_size = initialized_size;
if (compressed || sparse) {
struct ntfs_volume *vol = ni->vol;
ni->itype.compressed.size = compressed_size;
ni->itype.compressed.block_clusters = 1 << compression_unit;
ni->itype.compressed.block_size = 1 << (compression_unit +
vol->cluster_size_bits);
ni->itype.compressed.block_size_bits = ffs(
ni->itype.compressed.block_size) - 1;
}
}
int ntfs_attr_open(struct ntfs_inode *ni, const __le32 type,
__le16 *name, u32 name_len)
{
struct ntfs_attr_search_ctx *ctx;
__le16 *newname = NULL;
struct attr_record *a;
bool cs;
struct ntfs_inode *base_ni;
u32 ic;
int err;
if (!ni || !ni->vol)
return -EINVAL;
ntfs_debug("Entering for inode %lld, attr 0x%x.\n",
ni->mft_no, type);
base_ni = ntfs_base_inode(ni);
if (name && name != AT_UNNAMED && name != I30) {
name = ntfs_ucsndup(name, name_len);
if (!name) {
err = -ENOMEM;
goto err_out;
}
newname = name;
}
ctx = ntfs_attr_get_search_ctx(base_ni, NULL);
if (!ctx) {
err = -ENOMEM;
pr_err("%s: Failed to get search context\n", __func__);
goto err_out;
}
ic = type == AT_DATA && name_len ? IGNORE_CASE : CASE_SENSITIVE;
err = ntfs_attr_lookup(type, name, name_len, ic, 0, NULL, 0, ctx);
if (err)
goto put_err_out;
a = ctx->attr;
if (!name) {
if (a->name_length) {
name = ntfs_ucsndup((__le16 *)((u8 *)a + le16_to_cpu(a->name_offset)),
a->name_length);
if (!name)
goto put_err_out;
newname = name;
name_len = a->name_length;
} else {
name = AT_UNNAMED;
name_len = 0;
}
}
__ntfs_attr_init(ni, type, name, name_len);
if (type == AT_ATTRIBUTE_LIST)
a->flags = 0;
if ((type == AT_DATA) &&
(a->non_resident ? !a->data.non_resident.initialized_size :
!a->data.resident.value_length)) {
a->flags &= ~ATTR_COMPRESSION_MASK;
if (NInoCompressed(ni)
&& (ni->vol->major_ver >= 3)
&& NVolCompression(ni->vol)
&& (ni->vol->cluster_size <= MAX_COMPRESSION_CLUSTER_SIZE))
a->flags |= ATTR_IS_COMPRESSED;
}
cs = a->flags & (ATTR_IS_COMPRESSED | ATTR_IS_SPARSE);
if (ni->type == AT_DATA && ni->name == AT_UNNAMED &&
((!(a->flags & ATTR_IS_COMPRESSED) != !NInoCompressed(ni)) ||
(!(a->flags & ATTR_IS_SPARSE) != !NInoSparse(ni)) ||
(!(a->flags & ATTR_IS_ENCRYPTED) != !NInoEncrypted(ni)))) {
err = -EIO;
pr_err("Inode %lld has corrupt attribute flags (0x%x <> 0x%x)\n",
(unsigned long long)ni->mft_no,
a->flags, ni->flags);
goto put_err_out;
}
if (a->non_resident) {
if (((a->flags & ATTR_COMPRESSION_MASK) || a->data.non_resident.compression_unit) &&
(ni->vol->major_ver < 3)) {
err = -EIO;
pr_err("Compressed inode %lld not allowed on NTFS %d.%d\n",
(unsigned long long)ni->mft_no,
ni->vol->major_ver,
ni->vol->major_ver);
goto put_err_out;
}
if ((a->flags & ATTR_IS_COMPRESSED) && !a->data.non_resident.compression_unit) {
err = -EIO;
pr_err("Compressed inode %lld attr 0x%x has no compression unit\n",
(unsigned long long)ni->mft_no, type);
goto put_err_out;
}
if ((a->flags & ATTR_COMPRESSION_MASK) &&
(a->data.non_resident.compression_unit != STANDARD_COMPRESSION_UNIT)) {
err = -EIO;
pr_err("Compressed inode %lld attr 0x%lx has an unsupported compression unit %d\n",
(unsigned long long)ni->mft_no,
(long)le32_to_cpu(type),
(int)a->data.non_resident.compression_unit);
goto put_err_out;
}
ntfs_attr_init(ni, true, a->flags & ATTR_IS_COMPRESSED,
a->flags & ATTR_IS_ENCRYPTED,
a->flags & ATTR_IS_SPARSE,
le64_to_cpu(a->data.non_resident.allocated_size),
le64_to_cpu(a->data.non_resident.data_size),
le64_to_cpu(a->data.non_resident.initialized_size),
cs ? le64_to_cpu(a->data.non_resident.compressed_size) : 0,
cs ? a->data.non_resident.compression_unit : 0);
} else {
s64 l = le32_to_cpu(a->data.resident.value_length);
ntfs_attr_init(ni, false, a->flags & ATTR_IS_COMPRESSED,
a->flags & ATTR_IS_ENCRYPTED,
a->flags & ATTR_IS_SPARSE, (l + 7) & ~7, l, l,
cs ? (l + 7) & ~7 : 0, 0);
}
ntfs_attr_put_search_ctx(ctx);
out:
ntfs_debug("\n");
return err;
put_err_out:
ntfs_attr_put_search_ctx(ctx);
err_out:
kfree(newname);
goto out;
}
void ntfs_attr_close(struct ntfs_inode *ni)
{
if (NInoNonResident(ni) && ni->runlist.rl)
kvfree(ni->runlist.rl);
if (ni->name != AT_UNNAMED && ni->name != I30)
kfree(ni->name);
}
int ntfs_attr_map_whole_runlist(struct ntfs_inode *ni)
{
s64 next_vcn, last_vcn, highest_vcn;
struct ntfs_attr_search_ctx *ctx;
struct ntfs_volume *vol = ni->vol;
struct super_block *sb = vol->sb;
struct attr_record *a;
int err;
struct ntfs_inode *base_ni;
int not_mapped;
size_t new_rl_count;
ntfs_debug("Entering for inode 0x%llx, attr 0x%x.\n",
(unsigned long long)ni->mft_no, ni->type);
if (NInoFullyMapped(ni) && ni->runlist.rl)
return 0;
if (NInoAttr(ni))
base_ni = ni->ext.base_ntfs_ino;
else
base_ni = ni;
ctx = ntfs_attr_get_search_ctx(base_ni, NULL);
if (!ctx) {
ntfs_error(sb, "%s: Failed to get search context", __func__);
return -ENOMEM;
}
next_vcn = last_vcn = highest_vcn = 0;
a = NULL;
while (1) {
struct runlist_element *rl;
not_mapped = 0;
if (ntfs_rl_vcn_to_lcn(ni->runlist.rl, next_vcn) == LCN_RL_NOT_MAPPED)
not_mapped = 1;
err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
CASE_SENSITIVE, next_vcn,
NULL, 0, ctx);
if (err)
break;
a = ctx->attr;
if (not_mapped) {
rl = ntfs_mapping_pairs_decompress(ni->vol, a, &ni->runlist,
&new_rl_count);
if (IS_ERR(rl)) {
err = PTR_ERR(rl);
goto err_out;
}
ni->runlist.rl = rl;
ni->runlist.count = new_rl_count;
}
if (!next_vcn) {
if (a->data.non_resident.lowest_vcn) {
err = -EIO;
ntfs_error(sb,
"First extent of inode %llu attribute has non-zero lowest_vcn",
(unsigned long long)ni->mft_no);
goto err_out;
}
last_vcn = ntfs_bytes_to_cluster(vol,
le64_to_cpu(a->data.non_resident.allocated_size));
}
highest_vcn = le64_to_cpu(a->data.non_resident.highest_vcn);
next_vcn = highest_vcn + 1;
if (next_vcn <= 0) {
err = -ENOENT;
break;
}
if (next_vcn < le64_to_cpu(a->data.non_resident.lowest_vcn)) {
err = -EIO;
ntfs_error(sb, "Inode %llu has corrupt attribute list",
(unsigned long long)ni->mft_no);
goto err_out;
}
}
if (!a) {
ntfs_error(sb, "Couldn't find attribute for runlist mapping");
goto err_out;
}
if (not_mapped && highest_vcn && highest_vcn != last_vcn - 1) {
err = -EIO;
ntfs_error(sb,
"Failed to load full runlist: inode: %llu highest_vcn: 0x%llx last_vcn: 0x%llx",
(unsigned long long)ni->mft_no,
(long long)highest_vcn, (long long)last_vcn);
goto err_out;
}
ntfs_attr_put_search_ctx(ctx);
if (err == -ENOENT) {
NInoSetFullyMapped(ni);
return 0;
}
return err;
err_out:
ntfs_attr_put_search_ctx(ctx);
return err;
}
int ntfs_attr_record_move_to(struct ntfs_attr_search_ctx *ctx, struct ntfs_inode *ni)
{
struct ntfs_attr_search_ctx *nctx;
struct attr_record *a;
int err;
u32 ic;
struct mft_record *ni_mrec;
struct super_block *sb;
if (!ctx || !ctx->attr || !ctx->ntfs_ino || !ni) {
ntfs_debug("Invalid arguments passed.\n");
return -EINVAL;
}
sb = ni->vol->sb;
ntfs_debug("Entering for ctx->attr->type 0x%x, ctx->ntfs_ino->mft_no 0x%llx, ni->mft_no 0x%llx.\n",
(unsigned int) le32_to_cpu(ctx->attr->type),
(long long) ctx->ntfs_ino->mft_no,
(long long) ni->mft_no);
if (ctx->ntfs_ino == ni)
return 0;
if (!ctx->al_entry) {
ntfs_debug("Inode should contain attribute list to use this function.\n");
return -EINVAL;
}
a = ctx->attr;
nctx = ntfs_attr_get_search_ctx(ni, NULL);
if (!nctx) {
ntfs_error(sb, "%s: Failed to get search context", __func__);
return -ENOMEM;
}
ic = a->type == AT_DATA && a->name_length ?
IGNORE_CASE : CASE_SENSITIVE;
err = ntfs_attr_find(a->type,
(__le16 *)((u8 *)a + le16_to_cpu(a->name_offset)),
a->name_length, ic, NULL, 0, nctx);
if (!err) {
ntfs_debug("Attribute of such type, with same name already present in this MFT record.\n");
err = -EEXIST;
goto put_err_out;
}
if (err != -ENOENT) {
ntfs_debug("Attribute lookup failed.\n");
goto put_err_out;
}
ni_mrec = map_mft_record(ni);
if (IS_ERR(ni_mrec)) {
err = -EIO;
goto put_err_out;
}
err = ntfs_make_room_for_attr(ni_mrec, (u8 *) nctx->attr,
le32_to_cpu(a->length));
if (err) {
ntfs_debug("Couldn't make space for attribute.\n");
unmap_mft_record(ni);
goto put_err_out;
}
memcpy(nctx->attr, a, le32_to_cpu(a->length));
nctx->attr->instance = nctx->mrec->next_attr_instance;
nctx->mrec->next_attr_instance =
cpu_to_le16((le16_to_cpu(nctx->mrec->next_attr_instance) + 1) & 0xffff);
ntfs_attr_record_resize(ctx->mrec, a, 0);
mark_mft_record_dirty(ctx->ntfs_ino);
mark_mft_record_dirty(ni);
ctx->al_entry->mft_reference =
MK_LE_MREF(ni->mft_no, le16_to_cpu(ni_mrec->sequence_number));
ctx->al_entry->instance = nctx->attr->instance;
unmap_mft_record(ni);
put_err_out:
ntfs_attr_put_search_ctx(nctx);
return err;
}
int ntfs_attr_record_move_away(struct ntfs_attr_search_ctx *ctx, int extra)
{
struct ntfs_inode *base_ni, *ni = NULL;
struct mft_record *m;
int i, err;
struct super_block *sb;
if (!ctx || !ctx->attr || !ctx->ntfs_ino || extra < 0)
return -EINVAL;
ntfs_debug("Entering for attr 0x%x, inode %llu\n",
(unsigned int) le32_to_cpu(ctx->attr->type),
(unsigned long long)ctx->ntfs_ino->mft_no);
if (ctx->ntfs_ino->nr_extents == -1)
base_ni = ctx->base_ntfs_ino;
else
base_ni = ctx->ntfs_ino;
sb = ctx->ntfs_ino->vol->sb;
if (!NInoAttrList(base_ni)) {
ntfs_error(sb, "Inode %llu has no attrlist",
(unsigned long long)base_ni->mft_no);
return -EINVAL;
}
err = ntfs_inode_attach_all_extents(ctx->ntfs_ino);
if (err) {
ntfs_error(sb, "Couldn't attach extents, inode=%llu",
(unsigned long long)base_ni->mft_no);
return err;
}
mutex_lock(&base_ni->extent_lock);
for (i = 0; i < base_ni->nr_extents; i++) {
ni = base_ni->ext.extent_ntfs_inos[i];
if (ctx->ntfs_ino->mft_no == ni->mft_no)
continue;
m = map_mft_record(ni);
if (IS_ERR(m)) {
ntfs_error(sb, "Can not map mft record for mft_no %lld",
(unsigned long long)ni->mft_no);
mutex_unlock(&base_ni->extent_lock);
return -EIO;
}
if (le32_to_cpu(m->bytes_allocated) -
le32_to_cpu(m->bytes_in_use) < le32_to_cpu(ctx->attr->length) + extra) {
unmap_mft_record(ni);
continue;
}
unmap_mft_record(ni);
if (!ntfs_attr_record_move_to(ctx, ni)) {
mutex_unlock(&base_ni->extent_lock);
return 0;
}
}
mutex_unlock(&base_ni->extent_lock);
ni = NULL;
err = ntfs_mft_record_alloc(base_ni->vol, 0, &ni, base_ni, NULL, -1);
if (err) {
ntfs_error(sb, "Couldn't allocate MFT record, err : %d", err);
return err;
}
unmap_mft_record(ni);
err = ntfs_attr_record_move_to(ctx, ni);
if (err) {
ntfs_error(sb, "Couldn't move attribute to MFT record");
if (ntfs_mft_record_free(base_ni->vol, ni))
ntfs_error(sb, "Couldn't free empty MFT record");
else
ntfs_inode_close(ni);
}
return err;
}
static int ntfs_attr_update_meta(struct attr_record *a, struct ntfs_inode *ni,
struct mft_record *m, struct ntfs_attr_search_ctx *ctx,
struct ntfs_inode *locked_ni, bool defer_attrlist)
{
int sparse, err = 0;
struct ntfs_inode *base_ni;
struct super_block *sb = ni->vol->sb;
ntfs_debug("Entering for inode 0x%llx, attr 0x%x\n",
(unsigned long long)ni->mft_no, ni->type);
if (NInoAttr(ni))
base_ni = ni->ext.base_ntfs_ino;
else
base_ni = ni;
if (a->data.non_resident.lowest_vcn)
goto out;
a->data.non_resident.allocated_size = cpu_to_le64(ni->allocated_size);
sparse = ntfs_rl_sparse(ni->runlist.rl);
if (sparse < 0) {
err = -EIO;
goto out;
}
if (sparse && !(a->flags & (ATTR_IS_SPARSE | ATTR_IS_COMPRESSED))) {
if ((le32_to_cpu(a->length) -
le16_to_cpu(a->data.non_resident.mapping_pairs_offset) == 8) &&
!(le32_to_cpu(m->bytes_allocated) - le32_to_cpu(m->bytes_in_use))) {
if (defer_attrlist)
return -ENOSPC;
if (!NInoAttrList(base_ni)) {
err = ntfs_inode_add_attrlist(base_ni);
if (err)
goto out;
err = -EAGAIN;
goto out;
}
err = ntfs_attr_record_move_away(ctx, 8);
if (err) {
ntfs_error(sb, "Failed to move attribute");
goto out;
}
err = ntfs_attrlist_update_locked(base_ni, locked_ni);
if (err)
goto out;
err = -EAGAIN;
goto out;
}
if (!(le32_to_cpu(a->length) -
le16_to_cpu(a->data.non_resident.mapping_pairs_offset))) {
err = -EIO;
ntfs_error(sb, "Mapping pairs space is 0");
goto out;
}
NInoSetSparse(ni);
ni->flags |= FILE_ATTR_SPARSE_FILE;
a->flags |= ATTR_IS_SPARSE;
a->data.non_resident.compression_unit = 0;
memmove((u8 *)a + le16_to_cpu(a->name_offset) + 8,
(u8 *)a + le16_to_cpu(a->name_offset),
a->name_length * sizeof(__le16));
a->name_offset = cpu_to_le16(le16_to_cpu(a->name_offset) + 8);
a->data.non_resident.mapping_pairs_offset =
cpu_to_le16(le16_to_cpu(a->data.non_resident.mapping_pairs_offset) + 8);
}
if (!sparse && (a->flags & ATTR_IS_SPARSE) &&
!(a->flags & ATTR_IS_COMPRESSED)) {
NInoClearSparse(ni);
ni->flags &= ~FILE_ATTR_SPARSE_FILE;
a->flags &= ~ATTR_IS_SPARSE;
a->data.non_resident.compression_unit = 0;
memmove((u8 *)a + le16_to_cpu(a->name_offset) - 8,
(u8 *)a + le16_to_cpu(a->name_offset),
a->name_length * sizeof(__le16));
if (le16_to_cpu(a->name_offset) >= 8)
a->name_offset = cpu_to_le16(le16_to_cpu(a->name_offset) - 8);
a->data.non_resident.mapping_pairs_offset =
cpu_to_le16(le16_to_cpu(a->data.non_resident.mapping_pairs_offset) - 8);
}
if (NInoFullyMapped(ni) && (sparse || NInoCompressed(ni))) {
s64 new_compr_size;
new_compr_size = ntfs_rl_get_compressed_size(ni->vol, ni->runlist.rl);
if (new_compr_size < 0) {
err = new_compr_size;
goto out;
}
ni->itype.compressed.size = new_compr_size;
a->data.non_resident.compressed_size = cpu_to_le64(new_compr_size);
}
if (NInoSparse(ni) || NInoCompressed(ni))
VFS_I(base_ni)->i_blocks = ni->itype.compressed.size >> 9;
else
VFS_I(base_ni)->i_blocks = ni->allocated_size >> 9;
if (ni->type == AT_DATA && ni->name == AT_UNNAMED)
NInoSetFileNameDirty(ni);
out:
return err;
}
#define NTFS_VCN_DELETE_MARK -2
static int __ntfs_attr_update_mapping_pairs(struct ntfs_inode *ni,
s64 from_vcn,
struct ntfs_inode *locked_ni,
bool defer_attrlist)
{
struct ntfs_attr_search_ctx *ctx;
struct ntfs_inode *base_ni;
struct mft_record *m;
struct attr_record *a;
s64 stop_vcn;
int err = 0, mp_size, cur_max_mp_size, exp_max_mp_size;
bool finished_build;
bool first_updated = false;
struct super_block *sb;
struct runlist_element *start_rl;
unsigned int de_cluster_count = 0;
retry:
if (!ni || !ni->runlist.rl)
return -EINVAL;
ntfs_debug("Entering for inode %llu, attr 0x%x\n",
(unsigned long long)ni->mft_no, ni->type);
sb = ni->vol->sb;
if (!NInoNonResident(ni)) {
ntfs_error(sb, "%s: resident attribute", __func__);
return -EINVAL;
}
if (ni->nr_extents == -1)
base_ni = ni->ext.base_ntfs_ino;
else
base_ni = ni;
ctx = ntfs_attr_get_search_ctx(base_ni, NULL);
if (!ctx) {
ntfs_error(sb, "%s: Failed to get search context", __func__);
return -ENOMEM;
}
stop_vcn = 0;
finished_build = false;
start_rl = ni->runlist.rl;
while (!(err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
CASE_SENSITIVE, from_vcn, NULL,
0, ctx))) {
unsigned int de_cnt = 0;
a = ctx->attr;
m = ctx->mrec;
if (!a->data.non_resident.lowest_vcn)
first_updated = true;
if (from_vcn) {
s64 first_lcn;
stop_vcn = le64_to_cpu(a->data.non_resident.lowest_vcn);
from_vcn = 0;
first_lcn = ntfs_rl_vcn_to_lcn(ni->runlist.rl, stop_vcn);
if (first_lcn == LCN_EINVAL) {
err = -EIO;
ntfs_error(sb, "Bad runlist");
goto put_err_out;
}
if (first_lcn == LCN_ENOENT ||
first_lcn == LCN_RL_NOT_MAPPED)
finished_build = true;
}
if (finished_build) {
ntfs_debug("Mark attr 0x%x for delete in inode 0x%llx.\n",
(unsigned int)le32_to_cpu(a->type), ctx->ntfs_ino->mft_no);
a->data.non_resident.highest_vcn = cpu_to_le64(NTFS_VCN_DELETE_MARK);
mark_mft_record_dirty(ctx->ntfs_ino);
continue;
}
err = ntfs_attr_update_meta(a, ni, m, ctx, locked_ni,
defer_attrlist);
if (err < 0) {
if (err == -EAGAIN) {
ntfs_attr_put_search_ctx(ctx);
goto retry;
}
goto put_err_out;
}
cur_max_mp_size = le32_to_cpu(a->length) -
le16_to_cpu(a->data.non_resident.mapping_pairs_offset);
exp_max_mp_size = le32_to_cpu(m->bytes_allocated) -
le32_to_cpu(m->bytes_in_use) + cur_max_mp_size;
mp_size = ntfs_get_size_for_mapping_pairs(ni->vol, start_rl,
stop_vcn, -1, exp_max_mp_size);
if (mp_size <= 0) {
err = mp_size;
ntfs_error(sb, "%s: get MP size failed", __func__);
goto put_err_out;
}
if (mp_size > exp_max_mp_size) {
if (ni->type == AT_ATTRIBUTE_LIST) {
ntfs_attr_put_search_ctx(ctx);
ctx = NULL;
if (locked_ni == ni || defer_attrlist) {
err = -ENOSPC;
goto put_err_out;
}
err = ntfs_inode_free_space(base_ni, mp_size -
cur_max_mp_size);
if (err)
return err;
err = ntfs_attrlist_update_locked(
base_ni, locked_ni);
if (err)
return err;
goto retry;
}
if (!NInoAttrList(base_ni)) {
if (defer_attrlist) {
err = -ENOSPC;
goto put_err_out;
}
ntfs_attr_put_search_ctx(ctx);
if (ntfs_inode_add_attrlist(base_ni)) {
ntfs_error(sb, "Can not add attrlist");
return -EIO;
}
goto retry;
}
mp_size = exp_max_mp_size;
}
if (((mp_size + 7) & ~7) != cur_max_mp_size) {
if (ntfs_attr_record_resize(m, a,
le16_to_cpu(a->data.non_resident.mapping_pairs_offset) +
mp_size)) {
err = -EIO;
ntfs_error(sb, "Failed to resize attribute");
goto put_err_out;
}
}
if (defer_attrlist &&
(ctx->ntfs_ino->nr_extents == -1 ||
NInoAttrList(ctx->ntfs_ino)) &&
ctx->attr->type != AT_ATTRIBUTE_LIST) {
err = -ENOSPC;
goto put_err_out;
}
a->data.non_resident.lowest_vcn = cpu_to_le64(stop_vcn);
mark_mft_record_dirty(ctx->ntfs_ino);
if ((ctx->ntfs_ino->nr_extents == -1 || NInoAttrList(ctx->ntfs_ino)) &&
ctx->attr->type != AT_ATTRIBUTE_LIST) {
ctx->al_entry->lowest_vcn = cpu_to_le64(stop_vcn);
err = ntfs_attrlist_update_locked(base_ni, locked_ni);
if (err)
goto put_err_out;
}
err = ntfs_mapping_pairs_build(ni->vol,
(u8 *)a + le16_to_cpu(a->data.non_resident.mapping_pairs_offset),
mp_size, start_rl, stop_vcn, -1, &stop_vcn, &start_rl, &de_cnt);
if (!err)
finished_build = true;
if (!finished_build && err != -ENOSPC) {
ntfs_error(sb, "Failed to build mapping pairs");
goto put_err_out;
}
a->data.non_resident.highest_vcn = cpu_to_le64(stop_vcn - 1);
mark_mft_record_dirty(ctx->ntfs_ino);
de_cluster_count += de_cnt;
}
if (err && err != -ENOENT) {
ntfs_error(sb, "%s: Attribute lookup failed", __func__);
goto put_err_out;
}
if (!first_updated) {
ntfs_attr_reinit_search_ctx(ctx);
err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
CASE_SENSITIVE, 0, NULL, 0, ctx);
if (!err) {
a = ctx->attr;
a->data.non_resident.allocated_size = cpu_to_le64(ni->allocated_size);
if (NInoCompressed(ni) || NInoSparse(ni))
a->data.non_resident.compressed_size =
cpu_to_le64(ni->itype.compressed.size);
if (ni->type == AT_DATA && ni->name == AT_UNNAMED)
NInoSetFileNameDirty(ni);
mark_mft_record_dirty(ctx->ntfs_ino);
} else {
ntfs_error(sb, "Failed to update sizes in base extent\n");
goto put_err_out;
}
}
if (finished_build) {
ntfs_attr_reinit_search_ctx(ctx);
ntfs_debug("Deallocate marked extents.\n");
while (!(err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
CASE_SENSITIVE, 0, NULL, 0,
ctx))) {
if (le64_to_cpu(ctx->attr->data.non_resident.highest_vcn) !=
NTFS_VCN_DELETE_MARK)
continue;
err = ntfs_attr_record_rm(ctx);
if (err) {
ntfs_error(sb, "Could not remove unused attr");
goto put_err_out;
}
ntfs_attr_reinit_search_ctx(ctx);
}
if (err && err != -ENOENT) {
ntfs_error(sb, "%s: Attr lookup failed", __func__);
goto put_err_out;
}
ntfs_debug("Deallocate done.\n");
ntfs_attr_put_search_ctx(ctx);
goto out;
}
ntfs_attr_put_search_ctx(ctx);
ctx = NULL;
while (1) {
struct ntfs_inode *ext_ni = NULL;
unsigned int de_cnt = 0;
err = ntfs_mft_record_alloc(ni->vol, 0, &ext_ni, base_ni, NULL,
base_ni->mft_no == FILE_MFT &&
ni->type == AT_DATA &&
ni->name == AT_UNNAMED ? stop_vcn : -1);
if (err) {
ntfs_error(sb, "Failed to allocate extent record");
goto put_err_out;
}
unmap_mft_record(ext_ni);
m = map_mft_record(ext_ni);
if (IS_ERR(m)) {
ntfs_error(sb, "Could not map new MFT record");
if (ntfs_mft_record_free(ni->vol, ext_ni))
ntfs_error(sb, "Could not free MFT record");
ntfs_inode_close(ext_ni);
err = -ENOMEM;
ext_ni = NULL;
goto put_err_out;
}
cur_max_mp_size = le32_to_cpu(m->bytes_allocated) -
le32_to_cpu(m->bytes_in_use) -
(sizeof(struct attr_record) +
((NInoCompressed(ni) || NInoSparse(ni)) ?
sizeof(a->data.non_resident.compressed_size) : 0)) -
((sizeof(__le16) * ni->name_len + 7) & ~7);
mp_size = ntfs_get_size_for_mapping_pairs(ni->vol,
start_rl, stop_vcn, -1, cur_max_mp_size);
if (mp_size <= 0) {
unmap_mft_record(ext_ni);
ntfs_inode_close(ext_ni);
err = mp_size;
ntfs_error(sb, "%s: get mp size failed", __func__);
goto put_err_out;
}
if (mp_size > cur_max_mp_size)
mp_size = cur_max_mp_size;
err = ntfs_non_resident_attr_record_add(ext_ni, ni->type,
ni->name, ni->name_len, stop_vcn, mp_size, 0);
if (err < 0) {
ntfs_error(sb, "Could not add attribute extent");
unmap_mft_record(ext_ni);
if (ntfs_mft_record_free(ni->vol, ext_ni))
ntfs_error(sb, "Could not free MFT record");
ntfs_inode_close(ext_ni);
goto put_err_out;
}
a = (struct attr_record *)((u8 *)m + err);
err = ntfs_mapping_pairs_build(ni->vol, (u8 *)a +
le16_to_cpu(a->data.non_resident.mapping_pairs_offset),
mp_size, start_rl, stop_vcn, -1, &stop_vcn, &start_rl,
&de_cnt);
if (err < 0 && err != -ENOSPC) {
ntfs_error(sb, "Failed to build MP");
unmap_mft_record(ext_ni);
if (ntfs_mft_record_free(ni->vol, ext_ni))
ntfs_error(sb, "Couldn't free MFT record");
goto put_err_out;
}
a->data.non_resident.highest_vcn = cpu_to_le64(stop_vcn - 1);
mark_mft_record_dirty(ext_ni);
unmap_mft_record(ext_ni);
de_cluster_count += de_cnt;
if (!err)
break;
}
out:
if (from_vcn == 0)
ni->i_dealloc_clusters = de_cluster_count;
return 0;
put_err_out:
if (ctx)
ntfs_attr_put_search_ctx(ctx);
return err;
}
int ntfs_attr_update_mapping_pairs_locked(struct ntfs_inode *ni,
s64 from_vcn,
struct ntfs_inode *locked_ni)
{
return __ntfs_attr_update_mapping_pairs(ni, from_vcn, locked_ni,
false);
}
int ntfs_attr_update_mapping_pairs(struct ntfs_inode *ni, s64 from_vcn)
{
return ntfs_attr_update_mapping_pairs_locked(ni, from_vcn, NULL);
}
static int ntfs_attr_make_resident(struct ntfs_inode *ni, struct ntfs_attr_search_ctx *ctx)
{
struct ntfs_volume *vol = ni->vol;
struct super_block *sb = vol->sb;
struct attr_record *a = ctx->attr;
int name_ofs, val_ofs, err;
s64 arec_size;
ntfs_debug("Entering for inode 0x%llx, attr 0x%x.\n",
(unsigned long long)ni->mft_no, ni->type);
if (le64_to_cpu(a->data.non_resident.lowest_vcn)) {
ntfs_debug("Eeek! Should be called for the first extent of the attribute. Aborting...\n");
return -EINVAL;
}
if (!NInoNonResident(ni)) {
ntfs_debug("Eeek! Trying to make resident attribute resident. Aborting...\n");
return -EINVAL;
}
if (ni->type == AT_BITMAP && ni->mft_no == FILE_MFT)
return -EPERM;
err = ntfs_attr_can_be_resident(vol, ni->type);
if (err)
return err;
if (NInoCompressed(ni) || NInoEncrypted(ni)) {
ntfs_debug("Making compressed or encrypted files resident is not implemented yet.\n");
return -EOPNOTSUPP;
}
name_ofs = 24;
val_ofs = (name_ofs + a->name_length * sizeof(__le16) + 7) & ~7;
arec_size = (val_ofs + ni->data_size + 7) & ~7;
if (le32_to_cpu(ctx->mrec->bytes_in_use) - le32_to_cpu(a->length) +
arec_size > le32_to_cpu(ctx->mrec->bytes_allocated)) {
ntfs_debug("Not enough space to make attribute resident\n");
return -ENOSPC;
}
err = ntfs_attr_map_whole_runlist(ni);
if (err)
return err;
if (a->name_length) {
memmove((u8 *)a + name_ofs, (u8 *)a + le16_to_cpu(a->name_offset),
a->name_length * sizeof(__le16));
}
a->name_offset = cpu_to_le16(name_ofs);
if (ntfs_attr_record_resize(ctx->mrec, a, arec_size) < 0) {
ntfs_error(ctx->ntfs_ino->vol->sb, "BUG! Failed to resize attribute record. ");
return -EIO;
}
a->non_resident = 0;
a->flags = 0;
a->data.resident.value_length = cpu_to_le32(ni->data_size);
a->data.resident.value_offset = cpu_to_le16(val_ofs);
if (a->type == AT_FILE_NAME)
a->data.resident.flags = RESIDENT_ATTR_IS_INDEXED;
else
a->data.resident.flags = 0;
a->data.resident.reserved = 0;
err = ntfs_cluster_free(ni, 0, -1, ctx);
if (err) {
ntfs_error(sb, "Eeek! Failed to release allocated clusters");
ntfs_debug("Ignoring error and leaving behind wasted clusters.\n");
}
kvfree(ni->runlist.rl);
ni->runlist.rl = NULL;
ni->runlist.count = 0;
NInoClearNonResident(ni);
NInoClearCompressed(ni);
ni->flags &= ~FILE_ATTR_COMPRESSED;
NInoClearSparse(ni);
ni->flags &= ~FILE_ATTR_SPARSE_FILE;
NInoClearEncrypted(ni);
ni->flags &= ~FILE_ATTR_ENCRYPTED;
ni->initialized_size = ni->data_size;
ni->allocated_size = ni->itype.compressed.size = (ni->data_size + 7) & ~7;
ni->itype.compressed.block_size = 0;
ni->itype.compressed.block_size_bits = ni->itype.compressed.block_clusters = 0;
return 0;
}
static int ntfs_non_resident_attr_shrink(struct ntfs_inode *ni,
const s64 newsize,
struct ntfs_inode *locked_ni,
bool pagecache_truncated)
{
struct ntfs_volume *vol;
struct ntfs_attr_search_ctx *ctx;
s64 first_free_vcn;
s64 nr_freed_clusters;
int err;
struct ntfs_inode *base_ni;
bool runlist_locked = locked_ni == ni;
ntfs_debug("Inode 0x%llx attr 0x%x new size %lld\n",
(unsigned long long)ni->mft_no, ni->type, (long long)newsize);
vol = ni->vol;
if (NInoAttr(ni))
base_ni = ni->ext.base_ntfs_ino;
else
base_ni = ni;
err = ntfs_attr_size_bounds_check(vol, ni->type, newsize);
if (err) {
if (err == -ERANGE)
ntfs_debug("Eeek! Size bounds check failed. Aborting...\n");
else if (err == -ENOENT)
err = -EIO;
return err;
}
if (NInoCompressed(ni))
first_free_vcn = ntfs_bytes_to_cluster(vol,
((newsize - 1) | (ni->itype.compressed.block_size - 1)) + 1);
else
first_free_vcn =
ntfs_bytes_to_cluster(vol, newsize + vol->cluster_size - 1);
if (first_free_vcn < 0)
return -EINVAL;
if (ntfs_bytes_to_cluster(vol, ni->allocated_size) != first_free_vcn) {
if (!runlist_locked)
down_write(&ni->runlist.lock);
err = ntfs_attr_map_whole_runlist(ni);
if (err) {
ntfs_debug("Eeek! ntfs_attr_map_whole_runlist failed.\n");
goto unlock_runlist;
}
ctx = ntfs_attr_get_search_ctx(base_ni, NULL);
if (!ctx) {
ntfs_error(vol->sb, "%s: Failed to get search context", __func__);
err = -ENOMEM;
goto unlock_runlist;
}
nr_freed_clusters = ntfs_cluster_free(ni, first_free_vcn, -1, ctx);
if (nr_freed_clusters < 0) {
ntfs_debug("Eeek! Freeing of clusters failed. Aborting...\n");
ntfs_attr_put_search_ctx(ctx);
err = (int)nr_freed_clusters;
goto unlock_runlist;
}
ntfs_attr_put_search_ctx(ctx);
if (ntfs_rl_truncate_nolock(vol, &ni->runlist, first_free_vcn)) {
kvfree(ni->runlist.rl);
ni->runlist.rl = NULL;
ntfs_error(vol->sb, "Eeek! Run list truncation failed.\n");
err = -EIO;
goto unlock_runlist;
}
ni->allocated_size = ntfs_cluster_to_bytes(vol, first_free_vcn);
if (NInoSparse(ni) || NInoCompressed(ni)) {
if (nr_freed_clusters) {
ni->itype.compressed.size -=
ntfs_cluster_to_bytes(vol, nr_freed_clusters);
VFS_I(base_ni)->i_blocks = ni->itype.compressed.size >> 9;
}
} else
VFS_I(base_ni)->i_blocks = ni->allocated_size >> 9;
err = ntfs_attr_update_mapping_pairs_locked(ni, 0, ni);
if (err) {
ntfs_debug("Eeek! Mapping pairs update failed. Leaving inconstant metadata. Run chkdsk.\n");
goto unlock_runlist;
}
if (!runlist_locked)
up_write(&ni->runlist.lock);
}
ctx = ntfs_attr_get_search_ctx(base_ni, NULL);
if (!ctx) {
ntfs_error(vol->sb, "%s: Failed to get search context", __func__);
return -ENOMEM;
}
err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
CASE_SENSITIVE, 0, NULL, 0, ctx);
if (err) {
if (err == -ENOENT)
err = -EIO;
ntfs_debug("Eeek! Lookup of first attribute extent failed. Leaving inconstant metadata.\n");
goto put_err_out;
}
ni->data_size = newsize;
ctx->attr->data.non_resident.data_size = cpu_to_le64(newsize);
if (newsize < ni->initialized_size) {
ni->initialized_size = newsize;
ctx->attr->data.non_resident.initialized_size = cpu_to_le64(newsize);
}
if (!pagecache_truncated)
truncate_inode_pages(VFS_I(ni)->i_mapping, newsize);
if (ni->type == AT_DATA && ni->name == AT_UNNAMED)
NInoSetFileNameDirty(ni);
if (!newsize && !NInoEncrypted(ni) && !NInoCompressed(ni)) {
if (!runlist_locked)
down_write(&ni->runlist.lock);
err = ntfs_attr_make_resident(ni, ctx);
if (!runlist_locked)
up_write(&ni->runlist.lock);
if (err) {
if (err != -EPERM)
ntfs_error(ni->vol->sb,
"Failed to make attribute resident. Leaving as is...\n");
}
}
mark_mft_record_dirty(ctx->ntfs_ino);
ntfs_attr_put_search_ctx(ctx);
return 0;
put_err_out:
ntfs_attr_put_search_ctx(ctx);
return err;
unlock_runlist:
if (!runlist_locked)
up_write(&ni->runlist.lock);
return err;
}
static int ntfs_non_resident_attr_expand(struct ntfs_inode *ni, const s64 newsize,
const s64 prealloc_size, unsigned int holes,
struct ntfs_inode *locked_ni)
{
s64 lcn_seek_from;
s64 first_free_vcn;
struct ntfs_volume *vol;
struct ntfs_attr_search_ctx *ctx = NULL;
struct runlist_element *rl, *rln;
s64 org_alloc_size, org_compressed_size;
int err, err2;
struct ntfs_inode *base_ni;
struct super_block *sb = ni->vol->sb;
size_t new_rl_count;
unsigned long flags;
bool runlist_locked = locked_ni == ni;
ntfs_debug("Inode 0x%llx, attr 0x%x, new size %lld old size %lld\n",
(unsigned long long)ni->mft_no, ni->type,
(long long)newsize, (long long)ni->data_size);
vol = ni->vol;
if (NInoAttr(ni))
base_ni = ni->ext.base_ntfs_ino;
else
base_ni = ni;
err = ntfs_attr_size_bounds_check(vol, ni->type, newsize);
if (err < 0) {
ntfs_error(sb, "%s: bounds check failed", __func__);
return err;
}
org_alloc_size = ni->allocated_size;
org_compressed_size = ni->itype.compressed.size;
if (prealloc_size)
first_free_vcn =
ntfs_bytes_to_cluster(vol, prealloc_size + vol->cluster_size - 1);
else
first_free_vcn =
ntfs_bytes_to_cluster(vol, newsize + vol->cluster_size - 1);
if (first_free_vcn < 0)
return -EFBIG;
if (ntfs_bytes_to_cluster(vol, ni->allocated_size) < first_free_vcn) {
if (runlist_locked)
lockdep_assert_held_write(&ni->runlist.lock);
else
down_write(&ni->runlist.lock);
err = ntfs_attr_map_whole_runlist(ni);
if (err) {
ntfs_error(sb, "ntfs_attr_map_whole_runlist failed");
goto unlock_runlist;
}
if ((ni->type == AT_DATA && (vol->major_ver >= 3 || !NInoSparseDisabled(ni))) &&
(holes != HOLES_NO)) {
if (NInoCompressed(ni)) {
int last = 0, i = 0;
s64 alloc_size;
u64 more_entries = round_up(first_free_vcn -
ntfs_bytes_to_cluster(vol, ni->allocated_size),
ni->itype.compressed.block_clusters);
do_div(more_entries, ni->itype.compressed.block_clusters);
while (ni->runlist.rl[last].length)
last++;
rl = ntfs_rl_realloc(ni->runlist.rl, last + 1,
last + more_entries + 1);
if (IS_ERR(rl)) {
err = -ENOMEM;
goto unlock_runlist;
}
alloc_size = ni->allocated_size;
while (i++ < more_entries) {
rl[last].vcn = ntfs_bytes_to_cluster(vol,
round_up(alloc_size, vol->cluster_size));
rl[last].length = ni->itype.compressed.block_clusters -
(rl[last].vcn &
(ni->itype.compressed.block_clusters - 1));
rl[last].lcn = LCN_HOLE;
last++;
alloc_size += ni->itype.compressed.block_size;
}
rl[last].vcn = first_free_vcn;
rl[last].lcn = LCN_ENOENT;
rl[last].length = 0;
ni->runlist.rl = rl;
ni->runlist.count += more_entries;
} else {
rl = kmalloc(sizeof(struct runlist_element) * 2, GFP_NOFS);
if (!rl) {
err = -ENOMEM;
goto unlock_runlist;
}
rl[0].vcn = ntfs_bytes_to_cluster(vol, ni->allocated_size);
rl[0].lcn = LCN_HOLE;
rl[0].length = first_free_vcn -
ntfs_bytes_to_cluster(vol, ni->allocated_size);
rl[1].vcn = first_free_vcn;
rl[1].lcn = LCN_ENOENT;
rl[1].length = 0;
}
} else {
lcn_seek_from = -1;
if (ni->runlist.rl->length) {
for (rl = ni->runlist.rl; (rl + 1)->length; rl++)
;
while (rl->lcn < 0 && rl != ni->runlist.rl)
rl--;
if (rl->lcn >= 0)
lcn_seek_from = rl->lcn + rl->length;
}
rl = ntfs_cluster_alloc(vol,
ntfs_bytes_to_cluster(vol, ni->allocated_size),
first_free_vcn -
ntfs_bytes_to_cluster(vol, ni->allocated_size),
lcn_seek_from, DATA_ZONE, false,
ni->type == AT_ATTRIBUTE_LIST, false);
if (IS_ERR(rl)) {
ntfs_debug("Cluster allocation failed (%lld)",
(long long)first_free_vcn -
ntfs_bytes_to_cluster(vol, ni->allocated_size));
err = PTR_ERR(rl);
goto unlock_runlist;
}
if (ni->type == AT_ATTRIBUTE_LIST &&
(rl->vcn != ntfs_bytes_to_cluster(vol,
ni->allocated_size) ||
rl->length != first_free_vcn -
ntfs_bytes_to_cluster(vol, ni->allocated_size) ||
rl[1].length)) {
ntfs_cluster_free_from_rl(vol, rl);
kvfree(rl);
rl = ntfs_cluster_alloc(vol,
ntfs_bytes_to_cluster(vol,
ni->allocated_size),
first_free_vcn -
ntfs_bytes_to_cluster(vol,
ni->allocated_size),
lcn_seek_from, DATA_ZONE, false,
false, false);
if (IS_ERR(rl)) {
err = PTR_ERR(rl);
goto unlock_runlist;
}
}
}
if (!NInoCompressed(ni)) {
rln = ntfs_runlists_merge(&ni->runlist, rl, 0, &new_rl_count);
if (IS_ERR(rln)) {
ntfs_error(sb, "Run list merge failed");
ntfs_cluster_free_from_rl(vol, rl);
kvfree(rl);
err = -EIO;
goto unlock_runlist;
}
ni->runlist.rl = rln;
ni->runlist.count = new_rl_count;
}
ni->allocated_size = ntfs_cluster_to_bytes(vol, first_free_vcn);
if (ni->type == AT_ATTRIBUTE_LIST && !runlist_locked) {
up_write(&ni->runlist.lock);
err = ntfs_attr_update_mapping_pairs_locked(ni, 0,
locked_ni);
if (err) {
ntfs_debug("Mapping pairs update failed");
goto rollback;
}
} else {
err = ntfs_attr_update_mapping_pairs_locked(ni, 0, ni);
if (err) {
ntfs_debug("Mapping pairs update failed");
goto rollback_locked;
}
if (!runlist_locked)
up_write(&ni->runlist.lock);
}
}
ctx = ntfs_attr_get_search_ctx(base_ni, NULL);
if (!ctx) {
err = -ENOMEM;
if (ni->allocated_size == org_alloc_size)
return err;
goto rollback;
}
err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
CASE_SENSITIVE, 0, NULL, 0, ctx);
if (err) {
if (err == -ENOENT)
err = -EIO;
if (ni->allocated_size != org_alloc_size)
goto rollback;
goto put_err_out;
}
ni->data_size = newsize;
ctx->attr->data.non_resident.data_size = cpu_to_le64(newsize);
if (ni->type == AT_DATA && ni->name == AT_UNNAMED)
NInoSetFileNameDirty(ni);
mark_mft_record_dirty(ctx->ntfs_ino);
ntfs_attr_put_search_ctx(ctx);
return 0;
rollback:
if (!runlist_locked)
down_write(&ni->runlist.lock);
rollback_locked:
err2 = ntfs_cluster_free(ni, ntfs_bytes_to_cluster(vol, org_alloc_size),
-1, ctx);
if (err2)
ntfs_debug("Leaking clusters");
err2 = ntfs_rl_truncate_nolock(vol, &ni->runlist,
ntfs_bytes_to_cluster(vol, org_alloc_size));
if (!err2) {
write_lock_irqsave(&ni->size_lock, flags);
ni->allocated_size = org_alloc_size;
write_unlock_irqrestore(&ni->size_lock, flags);
}
if (err2) {
kvfree(ni->runlist.rl);
ni->runlist.rl = NULL;
ntfs_error(sb, "Couldn't truncate runlist. Rollback failed");
} else {
if (__ntfs_attr_update_mapping_pairs(ni, 0, ni, true))
ntfs_error(sb, "Failed to restore old mapping pairs");
if (NInoSparse(ni) || NInoCompressed(ni)) {
ni->itype.compressed.size = org_compressed_size;
VFS_I(base_ni)->i_blocks = ni->itype.compressed.size >> 9;
} else
VFS_I(base_ni)->i_blocks = ni->allocated_size >> 9;
}
if (!runlist_locked)
up_write(&ni->runlist.lock);
if (ctx)
ntfs_attr_put_search_ctx(ctx);
return err;
put_err_out:
if (ctx)
ntfs_attr_put_search_ctx(ctx);
return err;
unlock_runlist:
if (!runlist_locked)
up_write(&ni->runlist.lock);
return err;
}
static int ntfs_resident_attr_resize(struct ntfs_inode *attr_ni, const s64 newsize,
const s64 prealloc_size, unsigned int holes)
{
struct ntfs_attr_search_ctx *ctx;
struct ntfs_volume *vol = attr_ni->vol;
struct super_block *sb = vol->sb;
int err = -EIO;
struct ntfs_inode *base_ni, *ext_ni = NULL;
attr_resize_again:
ntfs_debug("Inode 0x%llx attr 0x%x new size %lld\n",
(unsigned long long)attr_ni->mft_no, attr_ni->type,
(long long)newsize);
if (NInoAttr(attr_ni))
base_ni = attr_ni->ext.base_ntfs_ino;
else
base_ni = attr_ni;
ctx = ntfs_attr_get_search_ctx(base_ni, NULL);
if (!ctx) {
ntfs_error(sb, "%s: Failed to get search context", __func__);
return -ENOMEM;
}
err = ntfs_attr_lookup(attr_ni->type, attr_ni->name, attr_ni->name_len,
CASE_SENSITIVE, 0, NULL, 0, ctx);
if (err) {
ntfs_error(sb, "ntfs_attr_lookup failed");
goto put_err_out;
}
err = ntfs_attr_size_bounds_check(vol, attr_ni->type, newsize);
if (err) {
if (err == -ENOENT)
err = -EIO;
ntfs_debug("%s: bounds check failed", __func__);
goto put_err_out;
}
if (newsize < vol->mft_record_size) {
err = ntfs_resident_attr_value_resize(ctx->mrec, ctx->attr,
newsize);
if (!err) {
attr_ni->data_size = attr_ni->initialized_size = newsize;
attr_ni->allocated_size = (newsize + 7) & ~7;
if (NInoCompressed(attr_ni) || NInoSparse(attr_ni))
attr_ni->itype.compressed.size = attr_ni->allocated_size;
if (attr_ni->type == AT_DATA && attr_ni->name == AT_UNNAMED)
NInoSetFileNameDirty(attr_ni);
goto resize_done;
}
if (err == -ENOSPC && ctx->attr->type == AT_INDEX_ROOT)
goto put_err_out;
}
err = ntfs_attr_make_non_resident(attr_ni,
le32_to_cpu(ctx->attr->data.resident.value_length));
if (!err) {
mark_mft_record_dirty(ctx->ntfs_ino);
ntfs_attr_put_search_ctx(ctx);
return ntfs_non_resident_attr_expand(
attr_ni, newsize, prealloc_size, holes, NULL);
} else if (err != -ENOSPC && err != -EPERM) {
ntfs_error(sb, "Failed to make attribute non-resident");
goto put_err_out;
}
ntfs_attr_reinit_search_ctx(ctx);
while (!(err = ntfs_attr_lookup(AT_UNUSED, NULL, 0, 0, 0, NULL, 0, ctx))) {
struct inode *tvi;
struct attr_record *a;
u32 value_len;
a = ctx->attr;
if (a->non_resident || a->type == AT_ATTRIBUTE_LIST)
continue;
value_len = le32_to_cpu(a->data.resident.value_length);
if (ntfs_attr_can_be_non_resident(vol, a->type))
continue;
if (le32_to_cpu(a->length) <= (sizeof(struct attr_record) - sizeof(s64)) +
((a->name_length * sizeof(__le16) + 7) & ~7) + 8)
continue;
if (a->type == AT_DATA && !value_len)
continue;
if (a->type == AT_DATA)
tvi = ntfs_iget(sb, base_ni->mft_no);
else
tvi = ntfs_attr_iget(VFS_I(base_ni), a->type,
(__le16 *)((u8 *)a + le16_to_cpu(a->name_offset)),
a->name_length);
if (IS_ERR(tvi)) {
ntfs_error(sb, "Couldn't open attribute");
continue;
}
if (ntfs_attr_make_non_resident(NTFS_I(tvi), value_len)) {
iput(tvi);
continue;
}
mark_mft_record_dirty(ctx->ntfs_ino);
iput(tvi);
ntfs_attr_put_search_ctx(ctx);
goto attr_resize_again;
}
if (err != -ENOENT) {
ntfs_error(sb, "%s: Attribute lookup failed 1", __func__);
goto put_err_out;
}
if (attr_ni->type == AT_STANDARD_INFORMATION ||
attr_ni->type == AT_ATTRIBUTE_LIST) {
ntfs_attr_put_search_ctx(ctx);
if (!NInoAttrList(base_ni)) {
err = ntfs_inode_add_attrlist(base_ni);
if (err)
return err;
}
err = ntfs_inode_free_space(base_ni, sizeof(struct attr_record));
if (err) {
err = -ENOSPC;
ntfs_error(sb,
"Couldn't free space in the MFT record to make attribute list non resident");
return err;
}
err = ntfs_attrlist_update(base_ni);
if (err)
return err;
goto attr_resize_again;
}
ntfs_attr_reinit_search_ctx(ctx);
err = ntfs_attr_lookup(attr_ni->type, attr_ni->name, attr_ni->name_len,
CASE_SENSITIVE, 0, NULL, 0, ctx);
if (err) {
ntfs_error(sb, "%s: Attribute lookup failed 2", __func__);
goto put_err_out;
}
if (le32_to_cpu(ctx->mrec->bytes_in_use) ==
le16_to_cpu(ctx->mrec->attrs_offset) + le32_to_cpu(ctx->attr->length) + 8) {
err = -ENOSPC;
ntfs_debug("MFT record is filled with one attribute\n");
goto put_err_out;
}
if (!NInoAttrList(base_ni)) {
ntfs_attr_put_search_ctx(ctx);
err = ntfs_inode_add_attrlist(base_ni);
if (err)
return err;
goto attr_resize_again;
}
err = ntfs_mft_record_alloc(base_ni->vol, 0, &ext_ni, base_ni, NULL, -1);
if (err) {
ntfs_error(sb, "Couldn't allocate MFT record");
goto put_err_out;
}
unmap_mft_record(ext_ni);
err = ntfs_attr_record_move_to(ctx, ext_ni);
if (err) {
ntfs_error(sb, "Couldn't move attribute to new MFT record");
err = -ENOMEM;
goto put_err_out;
}
err = ntfs_attrlist_update(base_ni);
if (err < 0)
goto put_err_out;
ntfs_attr_put_search_ctx(ctx);
goto attr_resize_again;
resize_done:
mark_mft_record_dirty(ctx->ntfs_ino);
ntfs_attr_put_search_ctx(ctx);
return 0;
put_err_out:
ntfs_attr_put_search_ctx(ctx);
return err;
}
int __ntfs_attr_truncate_vfs(struct ntfs_inode *ni, const s64 newsize,
const s64 i_size)
{
int err = 0;
if (newsize < 0 ||
(ni->mft_no == FILE_MFT && ni->type == AT_DATA)) {
ntfs_debug("Invalid arguments passed.\n");
return -EINVAL;
}
ntfs_debug("Entering for inode 0x%llx, attr 0x%x, size %lld\n",
(unsigned long long)ni->mft_no, ni->type, newsize);
if (NInoNonResident(ni)) {
if (newsize > i_size) {
down_write(&ni->runlist.lock);
err = ntfs_non_resident_attr_expand(
ni, newsize, 0,
NVolDisableSparse(ni->vol) ?
HOLES_NO : HOLES_OK, ni);
up_write(&ni->runlist.lock);
} else
err = ntfs_non_resident_attr_shrink(
ni, newsize, NULL, true);
} else
err = ntfs_resident_attr_resize(ni, newsize, 0,
NVolDisableSparse(ni->vol) ?
HOLES_NO : HOLES_OK);
ntfs_debug("Return status %d\n", err);
return err;
}
int ntfs_attr_expand_locked(struct ntfs_inode *ni, const s64 newsize,
const s64 prealloc_size,
struct ntfs_inode *locked_ni)
{
int err = 0;
if (newsize < 0 ||
(ni->mft_no == FILE_MFT && ni->type == AT_DATA)) {
ntfs_debug("Invalid arguments passed.\n");
return -EINVAL;
}
ntfs_debug("Entering for inode 0x%llx, attr 0x%x, size %lld\n",
(unsigned long long)ni->mft_no, ni->type, newsize);
if (ni->data_size == newsize &&
(!prealloc_size || prealloc_size <= ni->allocated_size)) {
ntfs_debug("Size is already ok\n");
return 0;
}
if (NInoEncrypted(ni)) {
pr_err("Failed to truncate encrypted attribute\n");
return -EACCES;
}
if (NInoNonResident(ni)) {
if (newsize > ni->data_size || prealloc_size > ni->allocated_size)
err = ntfs_non_resident_attr_expand(
ni, newsize, prealloc_size,
NVolDisableSparse(ni->vol) ?
HOLES_NO : HOLES_OK, locked_ni);
} else
err = ntfs_resident_attr_resize(ni, newsize, prealloc_size,
NVolDisableSparse(ni->vol) ?
HOLES_NO : HOLES_OK);
if (!err)
i_size_write(VFS_I(ni), newsize);
ntfs_debug("Return status %d\n", err);
return err;
}
int ntfs_attr_expand(struct ntfs_inode *ni, const s64 newsize,
const s64 prealloc_size)
{
return ntfs_attr_expand_locked(ni, newsize, prealloc_size, NULL);
}
int ntfs_attr_truncate_i_locked(struct ntfs_inode *ni, const s64 newsize,
unsigned int holes,
struct ntfs_inode *locked_ni)
{
int err;
if (newsize < 0 ||
(ni->mft_no == FILE_MFT && ni->type == AT_DATA)) {
ntfs_debug("Invalid arguments passed.\n");
return -EINVAL;
}
ntfs_debug("Entering for inode 0x%llx, attr 0x%x, size %lld\n",
(unsigned long long)ni->mft_no, ni->type, newsize);
if (ni->data_size == newsize) {
ntfs_debug("Size is already ok\n");
return 0;
}
if (NInoEncrypted(ni)) {
pr_err("Failed to truncate encrypted attribute\n");
return -EACCES;
}
if (NInoCompressed(ni)) {
pr_err("Failed to truncate compressed attribute\n");
return -EOPNOTSUPP;
}
if (NInoNonResident(ni)) {
if (newsize > ni->data_size)
err = ntfs_non_resident_attr_expand(
ni, newsize, 0, holes, locked_ni);
else
err = ntfs_non_resident_attr_shrink(
ni, newsize, locked_ni, false);
} else
err = ntfs_resident_attr_resize(ni, newsize, 0, holes);
if (!err && NInoAttr(ni))
i_size_write(VFS_I(ni), newsize);
ntfs_debug("Return status %d\n", err);
return err;
}
int ntfs_attr_truncate_i(struct ntfs_inode *ni, const s64 newsize,
unsigned int holes)
{
return ntfs_attr_truncate_i_locked(ni, newsize, holes, NULL);
}
int ntfs_attr_truncate(struct ntfs_inode *ni, const s64 newsize)
{
return ntfs_attr_truncate_i(ni, newsize,
NVolDisableSparse(ni->vol) ?
HOLES_NO : HOLES_OK);
}
int ntfs_attr_map_cluster(struct ntfs_inode *ni, s64 vcn_start, s64 *lcn_start,
s64 *lcn_count, s64 max_clu_count, bool *balloc, bool update_mp,
bool skip_holes)
{
struct ntfs_inode *base_ni = ntfs_base_inode(ni);
struct ntfs_volume *vol = ni->vol;
struct ntfs_attr_search_ctx *ctx;
struct runlist_element *rl, *rlc;
struct runlist_element *old_rl = NULL;
s64 vcn = vcn_start, lcn, clu_count;
s64 lcn_seek_from = -1;
int err = 0;
size_t new_rl_count, old_rl_count;
err = ntfs_attr_map_whole_runlist(ni);
if (err)
return err;
ctx = ntfs_attr_get_search_ctx(base_ni, NULL);
if (!ctx) {
ntfs_error(vol->sb, "%s: Failed to get search context", __func__);
return -ENOMEM;
}
err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
CASE_SENSITIVE, vcn, NULL, 0, ctx);
if (err) {
ntfs_error(vol->sb,
"ntfs_attr_lookup failed, ntfs inode(mft_no : %llu) type : 0x%x, err : %d",
ni->mft_no, ni->type, err);
goto out;
}
rl = ntfs_attr_find_vcn_nolock(ni, vcn, ctx);
if (IS_ERR(rl)) {
ntfs_error(vol->sb, "Failed to find run after mapping runlist.");
err = PTR_ERR(rl);
goto out;
}
lcn = ntfs_rl_vcn_to_lcn(rl, vcn);
clu_count = min(max_clu_count, rl->length - (vcn - rl->vcn));
if (lcn >= LCN_HOLE) {
if (lcn > LCN_DELALLOC ||
(lcn == LCN_HOLE && skip_holes)) {
*lcn_start = lcn;
*lcn_count = clu_count;
*balloc = false;
goto out;
}
} else {
WARN_ON(lcn == LCN_RL_NOT_MAPPED);
if (lcn == LCN_ENOENT)
err = -ENOENT;
else
err = -EIO;
goto out;
}
rlc = rl;
while (rlc->vcn) {
rlc--;
if (rlc->lcn >= 0) {
if (NInoCompressed(ni))
lcn_seek_from = rlc->lcn + rlc->length;
else
lcn_seek_from = rlc->lcn + (vcn - rlc->vcn);
break;
}
}
if (lcn_seek_from == -1) {
rlc = rl;
while (rlc->length) {
rlc++;
if (rlc->lcn >= 0) {
lcn_seek_from = rlc->lcn - (rlc->vcn - vcn);
if (lcn_seek_from < -1)
lcn_seek_from = -1;
break;
}
}
}
rlc = ntfs_cluster_alloc(vol, vcn, clu_count, lcn_seek_from, DATA_ZONE,
false, true, true);
if (IS_ERR(rlc)) {
err = PTR_ERR(rlc);
goto out;
}
WARN_ON(rlc->vcn != vcn);
lcn = rlc->lcn;
clu_count = rlc->length;
old_rl_count = ni->runlist.count;
old_rl = kmemdup(ni->runlist.rl,
old_rl_count * sizeof(*old_rl), GFP_NOFS);
if (!old_rl) {
err = -ENOMEM;
if (ntfs_cluster_free_from_rl(vol, rlc)) {
ntfs_error(vol->sb,
"Failed to free cluster allocation after runlist backup failure.");
NVolSetErrors(vol);
}
kvfree(rlc);
goto out;
}
rl = ntfs_runlists_merge(&ni->runlist, rlc, 0, &new_rl_count);
if (IS_ERR(rl)) {
ntfs_error(vol->sb, "Failed to merge runlists");
err = PTR_ERR(rl);
if (ntfs_cluster_free_from_rl(vol, rlc))
ntfs_error(vol->sb, "Failed to free hot clusters.");
kvfree(rlc);
goto out;
}
ni->runlist.rl = rl;
ni->runlist.count = new_rl_count;
if (!update_mp) {
u64 free = atomic64_read(&vol->free_clusters) * 100;
do_div(free, vol->nr_clusters);
if (free <= 5)
update_mp = true;
}
if (update_mp) {
ntfs_attr_reinit_search_ctx(ctx);
err = ntfs_attr_update_mapping_pairs_locked(ni, 0, ni);
if (err) {
int err2;
err2 = ntfs_cluster_free(ni, vcn, clu_count, ctx);
if (err2 < 0 || err2 != clu_count) {
ntfs_error(vol->sb,
"Failed to free cluster allocation. Leaving inconsistent metadata.\n");
NVolSetErrors(vol);
goto out;
}
kvfree(ni->runlist.rl);
ni->runlist.rl = old_rl;
ni->runlist.count = old_rl_count;
old_rl = NULL;
if (ntfs_attr_update_mapping_pairs_locked(
ni, 0, ni)) {
ntfs_error(vol->sb,
"Failed to restore mapping pairs after allocation rollback.\n");
NVolSetErrors(vol);
}
}
} else {
VFS_I(ni)->i_blocks += clu_count << (vol->cluster_size_bits - 9);
NInoSetRunlistDirty(ni);
mark_mft_record_dirty(base_ni);
}
*lcn_start = lcn;
*lcn_count = clu_count;
*balloc = true;
out:
kvfree(old_rl);
ntfs_attr_put_search_ctx(ctx);
return err;
}
int ntfs_attr_rm(struct ntfs_inode *ni)
{
struct ntfs_attr_search_ctx *ctx;
int err = 0, ret = 0;
struct ntfs_inode *base_ni;
struct super_block *sb = ni->vol->sb;
base_ni = ntfs_base_inode(ni);
ntfs_debug("Entering for inode 0x%llx, attr 0x%x.\n",
(long long) ni->mft_no, ni->type);
if (NInoNonResident(ni)) {
struct ntfs_attr_search_ctx *ctx;
err = ntfs_attr_map_whole_runlist(ni);
if (err)
return err;
ctx = ntfs_attr_get_search_ctx(base_ni, NULL);
if (!ctx) {
ntfs_error(sb, "%s: Failed to get search context", __func__);
return -ENOMEM;
}
ret = ntfs_cluster_free(ni, 0, -1, ctx);
if (ret < 0)
ntfs_error(sb,
"Failed to free cluster allocation. Leaving inconstant metadata.\n");
ntfs_attr_put_search_ctx(ctx);
}
ctx = ntfs_attr_get_search_ctx(base_ni, NULL);
if (!ctx) {
ntfs_error(sb, "%s: Failed to get search context", __func__);
return -ENOMEM;
}
while (!(err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
CASE_SENSITIVE, 0, NULL, 0, ctx))) {
err = ntfs_attr_record_rm(ctx);
if (err) {
ntfs_error(sb,
"Failed to remove attribute extent. Leaving inconstant metadata.\n");
ret = err;
}
ntfs_attr_reinit_search_ctx(ctx);
}
ntfs_attr_put_search_ctx(ctx);
if (err != -ENOENT) {
ntfs_error(sb, "Attribute lookup failed. Probably leaving inconstant metadata.\n");
ret = err;
}
return ret;
}
int ntfs_attr_exist(struct ntfs_inode *ni, const __le32 type, __le16 *name,
u32 name_len)
{
struct ntfs_attr_search_ctx *ctx;
u32 ic;
int ret;
ntfs_debug("Entering\n");
ctx = ntfs_attr_get_search_ctx(ni, NULL);
if (!ctx) {
ntfs_error(ni->vol->sb, "%s: Failed to get search context",
__func__);
return 0;
}
ic = type == AT_DATA && name_len ? IGNORE_CASE : CASE_SENSITIVE;
ret = ntfs_attr_lookup(type, name, name_len, ic,
0, NULL, 0, ctx);
ntfs_attr_put_search_ctx(ctx);
return !ret;
}
int ntfs_attr_remove(struct ntfs_inode *ni, const __le32 type, __le16 *name,
u32 name_len, struct inode **attr_vi)
{
int err;
struct inode *vi;
struct ntfs_inode *attr_ni;
ntfs_debug("Entering\n");
if (attr_vi)
*attr_vi = NULL;
if (!ni)
return -EINVAL;
vi = ntfs_attr_iget(VFS_I(ni), type, name, name_len);
if (IS_ERR(vi)) {
err = PTR_ERR(vi);
ntfs_error(ni->vol->sb, "Failed to open attribute 0x%02x of inode 0x%llx",
type, (unsigned long long)ni->mft_no);
return err;
}
attr_ni = NTFS_I(vi);
err = ntfs_attr_rm(attr_ni);
if (err) {
ntfs_error(ni->vol->sb, "Failed to remove attribute 0x%02x of inode 0x%llx",
type, (unsigned long long)ni->mft_no);
} else {
NInoClearDirty(attr_ni);
clear_nlink(vi);
remove_inode_hash(vi);
}
if (attr_vi)
*attr_vi = vi;
else
iput(vi);
return err;
}
void *ntfs_attr_readall(struct ntfs_inode *ni, const __le32 type,
__le16 *name, u32 name_len, s64 *data_size)
{
struct ntfs_inode *bmp_ni;
struct inode *bmp_vi;
void *data, *ret = NULL;
s64 size;
struct super_block *sb = ni->vol->sb;
ntfs_debug("Entering\n");
bmp_vi = ntfs_attr_iget(VFS_I(ni), type, name, name_len);
if (IS_ERR(bmp_vi)) {
ret = ERR_PTR(PTR_ERR(bmp_vi));
ntfs_debug("ntfs_attr_iget failed");
goto err_exit;
}
bmp_ni = NTFS_I(bmp_vi);
if (bmp_ni->data_size > NTFS_ATTR_READALL_MAX_SIZE &&
(bmp_ni->type != AT_BITMAP ||
bmp_ni->data_size > ((ni->vol->nr_clusters + 7) >> 3))) {
ntfs_error(sb, "Invalid attribute data size");
ret = ERR_PTR(-EIO);
goto out;
}
data = kvmalloc(bmp_ni->data_size, GFP_NOFS);
if (!data) {
ret = ERR_PTR(-ENOMEM);
goto out;
}
size = ntfs_inode_attr_pread(VFS_I(bmp_ni), 0, bmp_ni->data_size,
(u8 *)data);
if (size != bmp_ni->data_size) {
ntfs_error(sb, "ntfs_attr_pread failed");
ret = size < 0 ? ERR_PTR((int)size) : ERR_PTR(-EIO);
kvfree(data);
goto out;
}
ret = data;
if (data_size)
*data_size = size;
out:
iput(bmp_vi);
err_exit:
ntfs_debug("\n");
return ret;
}
int ntfs_non_resident_attr_insert_range(struct ntfs_inode *ni, s64 start_vcn, s64 len)
{
struct ntfs_inode *base_ni = ntfs_base_inode(ni);
struct ntfs_volume *vol = ni->vol;
struct runlist_element *hole_rl, *rl;
struct ntfs_attr_search_ctx *ctx;
int ret;
size_t new_rl_count;
if (ni->type != AT_DATA)
return -EOPNOTSUPP;
if (start_vcn > ntfs_bytes_to_cluster(vol, ni->allocated_size))
return -EINVAL;
hole_rl = kmalloc(sizeof(*hole_rl) * 2, GFP_NOFS);
if (!hole_rl)
return -ENOMEM;
hole_rl[0].vcn = start_vcn;
hole_rl[0].lcn = LCN_HOLE;
hole_rl[0].length = len;
hole_rl[1].vcn = start_vcn + len;
hole_rl[1].lcn = LCN_ENOENT;
hole_rl[1].length = 0;
down_write(&ni->runlist.lock);
ret = ntfs_attr_map_whole_runlist(ni);
if (ret) {
up_write(&ni->runlist.lock);
kfree(hole_rl);
return ret;
}
rl = ntfs_rl_find_vcn_nolock(ni->runlist.rl, start_vcn);
if (!rl) {
up_write(&ni->runlist.lock);
kfree(hole_rl);
return -EIO;
}
rl = ntfs_rl_insert_range(ni->runlist.rl, (int)ni->runlist.count,
hole_rl, 1, &new_rl_count);
if (IS_ERR(rl)) {
up_write(&ni->runlist.lock);
kfree(hole_rl);
return PTR_ERR(rl);
}
ni->runlist.rl = rl;
ni->runlist.count = new_rl_count;
ni->allocated_size += ntfs_cluster_to_bytes(vol, len);
ni->data_size += ntfs_cluster_to_bytes(vol, len);
if (ntfs_cluster_to_bytes(vol, start_vcn) < ni->initialized_size)
ni->initialized_size += ntfs_cluster_to_bytes(vol, len);
ret = ntfs_attr_update_mapping_pairs_locked(ni, 0, ni);
up_write(&ni->runlist.lock);
if (ret)
return ret;
ctx = ntfs_attr_get_search_ctx(base_ni, NULL);
if (!ctx) {
ret = -ENOMEM;
return ret;
}
ret = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
CASE_SENSITIVE, 0, NULL, 0, ctx);
if (ret) {
ntfs_attr_put_search_ctx(ctx);
return ret;
}
ctx->attr->data.non_resident.data_size = cpu_to_le64(ni->data_size);
ctx->attr->data.non_resident.initialized_size = cpu_to_le64(ni->initialized_size);
if (ni->type == AT_DATA && ni->name == AT_UNNAMED)
NInoSetFileNameDirty(ni);
mark_mft_record_dirty(ctx->ntfs_ino);
ntfs_attr_put_search_ctx(ctx);
return ret;
}
int ntfs_non_resident_attr_collapse_range(struct ntfs_inode *ni, s64 start_vcn, s64 len)
{
struct ntfs_inode *base_ni = ntfs_base_inode(ni);
struct ntfs_volume *vol = ni->vol;
struct runlist_element *punch_rl, *rl;
struct ntfs_attr_search_ctx *ctx = NULL;
s64 end_vcn;
int dst_cnt;
int ret;
size_t new_rl_cnt;
if (ni->type != AT_DATA)
return -EOPNOTSUPP;
end_vcn = ntfs_bytes_to_cluster(vol, ni->allocated_size);
if (start_vcn >= end_vcn)
return -EINVAL;
down_write(&ni->runlist.lock);
ret = ntfs_attr_map_whole_runlist(ni);
if (ret) {
up_write(&ni->runlist.lock);
return ret;
}
len = min(len, end_vcn - start_vcn);
for (rl = ni->runlist.rl, dst_cnt = 0; rl && rl->length; rl++)
dst_cnt++;
rl = ntfs_rl_find_vcn_nolock(ni->runlist.rl, start_vcn);
if (!rl) {
up_write(&ni->runlist.lock);
return -EIO;
}
rl = ntfs_rl_collapse_range(ni->runlist.rl, dst_cnt + 1,
start_vcn, len, &punch_rl, &new_rl_cnt);
if (IS_ERR(rl)) {
up_write(&ni->runlist.lock);
return PTR_ERR(rl);
}
ni->runlist.rl = rl;
ni->runlist.count = new_rl_cnt;
ni->allocated_size -= ntfs_cluster_to_bytes(vol, len);
if (ni->data_size > ntfs_cluster_to_bytes(vol, start_vcn)) {
if (ni->data_size > ntfs_cluster_to_bytes(vol, (start_vcn + len)))
ni->data_size -= ntfs_cluster_to_bytes(vol, len);
else
ni->data_size = ntfs_cluster_to_bytes(vol, start_vcn);
}
if (ni->initialized_size > ntfs_cluster_to_bytes(vol, start_vcn)) {
if (ni->initialized_size >
ntfs_cluster_to_bytes(vol, start_vcn + len))
ni->initialized_size -= ntfs_cluster_to_bytes(vol, len);
else
ni->initialized_size = ntfs_cluster_to_bytes(vol, start_vcn);
}
if (ni->allocated_size > 0) {
ret = ntfs_attr_update_mapping_pairs_locked(ni, 0, ni);
if (ret) {
up_write(&ni->runlist.lock);
goto out_rl;
}
}
up_write(&ni->runlist.lock);
ctx = ntfs_attr_get_search_ctx(base_ni, NULL);
if (!ctx) {
ret = -ENOMEM;
goto out_rl;
}
ret = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
CASE_SENSITIVE, 0, NULL, 0, ctx);
if (ret)
goto out_ctx;
ctx->attr->data.non_resident.data_size = cpu_to_le64(ni->data_size);
ctx->attr->data.non_resident.initialized_size = cpu_to_le64(ni->initialized_size);
if (ni->allocated_size == 0)
ntfs_attr_make_resident(ni, ctx);
mark_mft_record_dirty(ctx->ntfs_ino);
ret = ntfs_cluster_free_from_rl(vol, punch_rl);
if (ret)
ntfs_error(vol->sb, "Freeing of clusters failed");
out_ctx:
if (ctx)
ntfs_attr_put_search_ctx(ctx);
out_rl:
kvfree(punch_rl);
mark_mft_record_dirty(base_ni);
return ret;
}
int ntfs_non_resident_attr_punch_hole(struct ntfs_inode *ni, s64 start_vcn, s64 len)
{
struct ntfs_inode *base_ni = ntfs_base_inode(ni);
struct ntfs_volume *vol = ni->vol;
struct runlist_element *punch_rl, *rl;
s64 end_vcn;
int dst_cnt;
int ret;
size_t new_rl_count;
if (ni->type != AT_DATA)
return -EOPNOTSUPP;
end_vcn = ntfs_bytes_to_cluster(vol, ni->allocated_size);
if (start_vcn >= end_vcn)
return -EINVAL;
down_write(&ni->runlist.lock);
ret = ntfs_attr_map_whole_runlist(ni);
if (ret) {
up_write(&ni->runlist.lock);
return ret;
}
len = min(len, end_vcn - start_vcn + 1);
for (rl = ni->runlist.rl, dst_cnt = 0; rl && rl->length; rl++)
dst_cnt++;
rl = ntfs_rl_find_vcn_nolock(ni->runlist.rl, start_vcn);
if (!rl) {
up_write(&ni->runlist.lock);
return -EIO;
}
rl = ntfs_rl_punch_hole(ni->runlist.rl, dst_cnt + 1,
start_vcn, len, &punch_rl, &new_rl_count);
if (IS_ERR(rl)) {
up_write(&ni->runlist.lock);
return PTR_ERR(rl);
}
ni->runlist.rl = rl;
ni->runlist.count = new_rl_count;
ret = ntfs_attr_update_mapping_pairs_locked(ni, 0, ni);
up_write(&ni->runlist.lock);
if (ret) {
kvfree(punch_rl);
return ret;
}
ret = ntfs_cluster_free_from_rl(vol, punch_rl);
if (ret)
ntfs_error(vol->sb, "Freeing of clusters failed");
kvfree(punch_rl);
mark_mft_record_dirty(base_ni);
return ret;
}
int ntfs_attr_fallocate(struct ntfs_inode *ni, loff_t start, loff_t byte_len, bool keep_size)
{
struct ntfs_inode *base_ni = ntfs_base_inode(ni);
struct ntfs_volume *vol = ni->vol;
struct mft_record *mrec;
struct ntfs_attr_search_ctx *ctx;
s64 old_data_size;
s64 vcn_start, vcn_end, vcn_uninit, vcn, try_alloc_cnt;
s64 lcn, alloc_cnt;
s64 rl_lcn, rl_length, rl_vcn;
int err = 0;
struct runlist_element *rl;
bool balloc;
if (ni->type != AT_DATA)
return -EINVAL;
if (NInoNonResident(ni) && !NInoFullyMapped(ni)) {
down_write(&ni->runlist.lock);
err = ntfs_attr_map_whole_runlist(ni);
up_write(&ni->runlist.lock);
if (err)
return err;
}
mutex_lock_nested(&base_ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL);
mrec = map_mft_record(base_ni);
if (IS_ERR(mrec)) {
mutex_unlock(&base_ni->mrec_lock);
return PTR_ERR(mrec);
}
ctx = ntfs_attr_get_search_ctx(base_ni, mrec);
if (!ctx) {
err = -ENOMEM;
goto out_unmap;
}
err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
CASE_SENSITIVE, 0, NULL, 0, ctx);
if (err) {
err = -EIO;
goto out_unmap;
}
old_data_size = ni->data_size;
if (start + byte_len > ni->data_size) {
err = ntfs_attr_truncate(ni, start + byte_len);
if (err)
goto out_unmap;
if (keep_size) {
ntfs_attr_reinit_search_ctx(ctx);
err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
CASE_SENSITIVE, 0, NULL, 0,
ctx);
if (err) {
err = -EIO;
goto out_unmap;
}
ni->data_size = old_data_size;
i_size_write(VFS_I(ni), old_data_size);
if (NInoNonResident(ni))
ctx->attr->data.non_resident.data_size =
cpu_to_le64(old_data_size);
else
ctx->attr->data.resident.value_length =
cpu_to_le32((u32)old_data_size);
mark_mft_record_dirty(base_ni);
}
}
ntfs_attr_put_search_ctx(ctx);
unmap_mft_record(base_ni);
mutex_unlock(&base_ni->mrec_lock);
if (!NInoNonResident(ni))
goto out;
vcn_start = (s64)ntfs_bytes_to_cluster(vol, start);
vcn_end = (s64)ntfs_bytes_to_cluster(vol,
round_up(start + byte_len, vol->cluster_size));
vcn_uninit = (s64)ntfs_bytes_to_cluster(vol,
round_up(ni->initialized_size, vol->cluster_size));
vcn_uninit = min_t(s64, vcn_uninit, vcn_end);
vcn = vcn_start;
while (vcn < vcn_uninit) {
down_read(&ni->runlist.lock);
rl = ntfs_attr_find_vcn_nolock(ni, vcn, NULL);
if (IS_ERR(rl)) {
up_read(&ni->runlist.lock);
err = PTR_ERR(rl);
goto out;
}
rl_lcn = rl->lcn;
rl_length = rl->length;
rl_vcn = rl->vcn;
up_read(&ni->runlist.lock);
if (rl_lcn > 0) {
vcn += rl_length - (vcn - rl_vcn);
} else if (rl_lcn == LCN_DELALLOC || rl_lcn == LCN_HOLE) {
try_alloc_cnt = min(rl_length - (vcn - rl_vcn),
vcn_uninit - vcn);
if (rl_lcn == LCN_DELALLOC) {
vcn += try_alloc_cnt;
continue;
}
while (try_alloc_cnt > 0) {
mutex_lock_nested(&base_ni->mrec_lock,
NTFS_INODE_MUTEX_NORMAL);
down_write(&ni->runlist.lock);
err = ntfs_attr_map_cluster(ni, vcn, &lcn, &alloc_cnt,
try_alloc_cnt, &balloc, false, false);
up_write(&ni->runlist.lock);
mutex_unlock(&base_ni->mrec_lock);
if (err)
goto out;
if (balloc) {
err = ntfs_dio_zero_range(VFS_I(ni),
lcn << vol->cluster_size_bits,
alloc_cnt <<
vol->cluster_size_bits);
if (err)
goto out;
}
if (fatal_signal_pending(current))
goto signal_out;
vcn += alloc_cnt;
try_alloc_cnt -= alloc_cnt;
}
} else {
err = -EIO;
goto out;
}
}
try_alloc_cnt = vcn_end - vcn;
while (try_alloc_cnt > 0) {
mutex_lock_nested(&base_ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL);
down_write(&ni->runlist.lock);
err = ntfs_attr_map_cluster(ni, vcn, &lcn, &alloc_cnt,
try_alloc_cnt, &balloc, false, false);
up_write(&ni->runlist.lock);
mutex_unlock(&base_ni->mrec_lock);
if (err || fatal_signal_pending(current))
goto signal_out;
vcn += alloc_cnt;
try_alloc_cnt -= alloc_cnt;
cond_resched();
}
if (NInoRunlistDirty(ni)) {
mutex_lock_nested(&base_ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL);
down_write(&ni->runlist.lock);
err = ntfs_attr_update_mapping_pairs_locked(ni, 0, ni);
if (err)
ntfs_error(ni->vol->sb, "Updating mapping pairs failed");
else
NInoClearRunlistDirty(ni);
up_write(&ni->runlist.lock);
mutex_unlock(&base_ni->mrec_lock);
}
return err;
out_unmap:
if (ctx)
ntfs_attr_put_search_ctx(ctx);
unmap_mft_record(base_ni);
mutex_unlock(&base_ni->mrec_lock);
out:
return err >= 0 ? 0 : err;
signal_out:
if (!err)
err = -EINTR;
goto out;
}