inode.c 167 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786
  1. /*
  2. * linux/fs/ext4/inode.c
  3. *
  4. * Copyright (C) 1992, 1993, 1994, 1995
  5. * Remy Card (card@masi.ibp.fr)
  6. * Laboratoire MASI - Institut Blaise Pascal
  7. * Universite Pierre et Marie Curie (Paris VI)
  8. *
  9. * from
  10. *
  11. * linux/fs/minix/inode.c
  12. *
  13. * Copyright (C) 1991, 1992 Linus Torvalds
  14. *
  15. * 64-bit file support on 64-bit platforms by Jakub Jelinek
  16. * (jj@sunsite.ms.mff.cuni.cz)
  17. *
  18. * Assorted race fixes, rewrite of ext4_get_block() by Al Viro, 2000
  19. */
  20. #include <linux/fs.h>
  21. #include <linux/time.h>
  22. #include <linux/highuid.h>
  23. #include <linux/pagemap.h>
  24. #include <linux/dax.h>
  25. #include <linux/quotaops.h>
  26. #include <linux/string.h>
  27. #include <linux/buffer_head.h>
  28. #include <linux/writeback.h>
  29. #include <linux/pagevec.h>
  30. #include <linux/mpage.h>
  31. #include <linux/namei.h>
  32. #include <linux/uio.h>
  33. #include <linux/bio.h>
  34. #include <linux/workqueue.h>
  35. #include <linux/kernel.h>
  36. #include <linux/printk.h>
  37. #include <linux/slab.h>
  38. #include <linux/bitops.h>
  39. #include "ext4_jbd2.h"
  40. #include "xattr.h"
  41. #include "acl.h"
  42. #include "truncate.h"
  43. #include <trace/events/ext4.h>
  44. #define MPAGE_DA_EXTENT_TAIL 0x01
  45. static __u32 ext4_inode_csum(struct inode *inode, struct ext4_inode *raw,
  46. struct ext4_inode_info *ei)
  47. {
  48. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  49. __u16 csum_lo;
  50. __u16 csum_hi = 0;
  51. __u32 csum;
  52. csum_lo = le16_to_cpu(raw->i_checksum_lo);
  53. raw->i_checksum_lo = 0;
  54. if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE &&
  55. EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi)) {
  56. csum_hi = le16_to_cpu(raw->i_checksum_hi);
  57. raw->i_checksum_hi = 0;
  58. }
  59. csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)raw,
  60. EXT4_INODE_SIZE(inode->i_sb));
  61. raw->i_checksum_lo = cpu_to_le16(csum_lo);
  62. if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE &&
  63. EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi))
  64. raw->i_checksum_hi = cpu_to_le16(csum_hi);
  65. return csum;
  66. }
  67. static int ext4_inode_csum_verify(struct inode *inode, struct ext4_inode *raw,
  68. struct ext4_inode_info *ei)
  69. {
  70. __u32 provided, calculated;
  71. if (EXT4_SB(inode->i_sb)->s_es->s_creator_os !=
  72. cpu_to_le32(EXT4_OS_LINUX) ||
  73. !ext4_has_metadata_csum(inode->i_sb))
  74. return 1;
  75. provided = le16_to_cpu(raw->i_checksum_lo);
  76. calculated = ext4_inode_csum(inode, raw, ei);
  77. if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE &&
  78. EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi))
  79. provided |= ((__u32)le16_to_cpu(raw->i_checksum_hi)) << 16;
  80. else
  81. calculated &= 0xFFFF;
  82. return provided == calculated;
  83. }
  84. static void ext4_inode_csum_set(struct inode *inode, struct ext4_inode *raw,
  85. struct ext4_inode_info *ei)
  86. {
  87. __u32 csum;
  88. if (EXT4_SB(inode->i_sb)->s_es->s_creator_os !=
  89. cpu_to_le32(EXT4_OS_LINUX) ||
  90. !ext4_has_metadata_csum(inode->i_sb))
  91. return;
  92. csum = ext4_inode_csum(inode, raw, ei);
  93. raw->i_checksum_lo = cpu_to_le16(csum & 0xFFFF);
  94. if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE &&
  95. EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi))
  96. raw->i_checksum_hi = cpu_to_le16(csum >> 16);
  97. }
  98. static inline int ext4_begin_ordered_truncate(struct inode *inode,
  99. loff_t new_size)
  100. {
  101. trace_ext4_begin_ordered_truncate(inode, new_size);
  102. /*
  103. * If jinode is zero, then we never opened the file for
  104. * writing, so there's no need to call
  105. * jbd2_journal_begin_ordered_truncate() since there's no
  106. * outstanding writes we need to flush.
  107. */
  108. if (!EXT4_I(inode)->jinode)
  109. return 0;
  110. return jbd2_journal_begin_ordered_truncate(EXT4_JOURNAL(inode),
  111. EXT4_I(inode)->jinode,
  112. new_size);
  113. }
  114. static void ext4_invalidatepage(struct page *page, unsigned int offset,
  115. unsigned int length);
  116. static int __ext4_journalled_writepage(struct page *page, unsigned int len);
  117. static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh);
  118. static int ext4_meta_trans_blocks(struct inode *inode, int lblocks,
  119. int pextents);
  120. /*
  121. * Test whether an inode is a fast symlink.
  122. */
  123. int ext4_inode_is_fast_symlink(struct inode *inode)
  124. {
  125. int ea_blocks = EXT4_I(inode)->i_file_acl ?
  126. EXT4_CLUSTER_SIZE(inode->i_sb) >> 9 : 0;
  127. if (ext4_has_inline_data(inode))
  128. return 0;
  129. return (S_ISLNK(inode->i_mode) && inode->i_blocks - ea_blocks == 0);
  130. }
  131. /*
  132. * Restart the transaction associated with *handle. This does a commit,
  133. * so before we call here everything must be consistently dirtied against
  134. * this transaction.
  135. */
  136. int ext4_truncate_restart_trans(handle_t *handle, struct inode *inode,
  137. int nblocks)
  138. {
  139. int ret;
  140. /*
  141. * Drop i_data_sem to avoid deadlock with ext4_map_blocks. At this
  142. * moment, get_block can be called only for blocks inside i_size since
  143. * page cache has been already dropped and writes are blocked by
  144. * i_mutex. So we can safely drop the i_data_sem here.
  145. */
  146. BUG_ON(EXT4_JOURNAL(inode) == NULL);
  147. jbd_debug(2, "restarting handle %p\n", handle);
  148. up_write(&EXT4_I(inode)->i_data_sem);
  149. ret = ext4_journal_restart(handle, nblocks);
  150. down_write(&EXT4_I(inode)->i_data_sem);
  151. ext4_discard_preallocations(inode);
  152. return ret;
  153. }
  154. /*
  155. * Called at the last iput() if i_nlink is zero.
  156. */
  157. void ext4_evict_inode(struct inode *inode)
  158. {
  159. handle_t *handle;
  160. int err;
  161. trace_ext4_evict_inode(inode);
  162. if (inode->i_nlink) {
  163. /*
  164. * When journalling data dirty buffers are tracked only in the
  165. * journal. So although mm thinks everything is clean and
  166. * ready for reaping the inode might still have some pages to
  167. * write in the running transaction or waiting to be
  168. * checkpointed. Thus calling jbd2_journal_invalidatepage()
  169. * (via truncate_inode_pages()) to discard these buffers can
  170. * cause data loss. Also even if we did not discard these
  171. * buffers, we would have no way to find them after the inode
  172. * is reaped and thus user could see stale data if he tries to
  173. * read them before the transaction is checkpointed. So be
  174. * careful and force everything to disk here... We use
  175. * ei->i_datasync_tid to store the newest transaction
  176. * containing inode's data.
  177. *
  178. * Note that directories do not have this problem because they
  179. * don't use page cache.
  180. */
  181. if (ext4_should_journal_data(inode) &&
  182. (S_ISLNK(inode->i_mode) || S_ISREG(inode->i_mode)) &&
  183. inode->i_ino != EXT4_JOURNAL_INO) {
  184. journal_t *journal = EXT4_SB(inode->i_sb)->s_journal;
  185. tid_t commit_tid = EXT4_I(inode)->i_datasync_tid;
  186. jbd2_complete_transaction(journal, commit_tid);
  187. filemap_write_and_wait(&inode->i_data);
  188. }
  189. truncate_inode_pages_final(&inode->i_data);
  190. goto no_delete;
  191. }
  192. if (is_bad_inode(inode))
  193. goto no_delete;
  194. dquot_initialize(inode);
  195. if (ext4_should_order_data(inode))
  196. ext4_begin_ordered_truncate(inode, 0);
  197. truncate_inode_pages_final(&inode->i_data);
  198. /*
  199. * Protect us against freezing - iput() caller didn't have to have any
  200. * protection against it
  201. */
  202. sb_start_intwrite(inode->i_sb);
  203. handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE,
  204. ext4_blocks_for_truncate(inode)+3);
  205. if (IS_ERR(handle)) {
  206. ext4_std_error(inode->i_sb, PTR_ERR(handle));
  207. /*
  208. * If we're going to skip the normal cleanup, we still need to
  209. * make sure that the in-core orphan linked list is properly
  210. * cleaned up.
  211. */
  212. ext4_orphan_del(NULL, inode);
  213. sb_end_intwrite(inode->i_sb);
  214. goto no_delete;
  215. }
  216. if (IS_SYNC(inode))
  217. ext4_handle_sync(handle);
  218. inode->i_size = 0;
  219. err = ext4_mark_inode_dirty(handle, inode);
  220. if (err) {
  221. ext4_warning(inode->i_sb,
  222. "couldn't mark inode dirty (err %d)", err);
  223. goto stop_handle;
  224. }
  225. if (inode->i_blocks)
  226. ext4_truncate(inode);
  227. /*
  228. * ext4_ext_truncate() doesn't reserve any slop when it
  229. * restarts journal transactions; therefore there may not be
  230. * enough credits left in the handle to remove the inode from
  231. * the orphan list and set the dtime field.
  232. */
  233. if (!ext4_handle_has_enough_credits(handle, 3)) {
  234. err = ext4_journal_extend(handle, 3);
  235. if (err > 0)
  236. err = ext4_journal_restart(handle, 3);
  237. if (err != 0) {
  238. ext4_warning(inode->i_sb,
  239. "couldn't extend journal (err %d)", err);
  240. stop_handle:
  241. ext4_journal_stop(handle);
  242. ext4_orphan_del(NULL, inode);
  243. sb_end_intwrite(inode->i_sb);
  244. goto no_delete;
  245. }
  246. }
  247. /*
  248. * Kill off the orphan record which ext4_truncate created.
  249. * AKPM: I think this can be inside the above `if'.
  250. * Note that ext4_orphan_del() has to be able to cope with the
  251. * deletion of a non-existent orphan - this is because we don't
  252. * know if ext4_truncate() actually created an orphan record.
  253. * (Well, we could do this if we need to, but heck - it works)
  254. */
  255. ext4_orphan_del(handle, inode);
  256. EXT4_I(inode)->i_dtime = get_seconds();
  257. /*
  258. * One subtle ordering requirement: if anything has gone wrong
  259. * (transaction abort, IO errors, whatever), then we can still
  260. * do these next steps (the fs will already have been marked as
  261. * having errors), but we can't free the inode if the mark_dirty
  262. * fails.
  263. */
  264. if (ext4_mark_inode_dirty(handle, inode))
  265. /* If that failed, just do the required in-core inode clear. */
  266. ext4_clear_inode(inode);
  267. else
  268. ext4_free_inode(handle, inode);
  269. ext4_journal_stop(handle);
  270. sb_end_intwrite(inode->i_sb);
  271. return;
  272. no_delete:
  273. ext4_clear_inode(inode); /* We must guarantee clearing of inode... */
  274. }
  275. #ifdef CONFIG_QUOTA
  276. qsize_t *ext4_get_reserved_space(struct inode *inode)
  277. {
  278. return &EXT4_I(inode)->i_reserved_quota;
  279. }
  280. #endif
  281. /*
  282. * Called with i_data_sem down, which is important since we can call
  283. * ext4_discard_preallocations() from here.
  284. */
  285. void ext4_da_update_reserve_space(struct inode *inode,
  286. int used, int quota_claim)
  287. {
  288. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  289. struct ext4_inode_info *ei = EXT4_I(inode);
  290. spin_lock(&ei->i_block_reservation_lock);
  291. trace_ext4_da_update_reserve_space(inode, used, quota_claim);
  292. if (unlikely(used > ei->i_reserved_data_blocks)) {
  293. ext4_warning(inode->i_sb, "%s: ino %lu, used %d "
  294. "with only %d reserved data blocks",
  295. __func__, inode->i_ino, used,
  296. ei->i_reserved_data_blocks);
  297. WARN_ON(1);
  298. used = ei->i_reserved_data_blocks;
  299. }
  300. /* Update per-inode reservations */
  301. ei->i_reserved_data_blocks -= used;
  302. percpu_counter_sub(&sbi->s_dirtyclusters_counter, used);
  303. spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
  304. /* Update quota subsystem for data blocks */
  305. if (quota_claim)
  306. dquot_claim_block(inode, EXT4_C2B(sbi, used));
  307. else {
  308. /*
  309. * We did fallocate with an offset that is already delayed
  310. * allocated. So on delayed allocated writeback we should
  311. * not re-claim the quota for fallocated blocks.
  312. */
  313. dquot_release_reservation_block(inode, EXT4_C2B(sbi, used));
  314. }
  315. /*
  316. * If we have done all the pending block allocations and if
  317. * there aren't any writers on the inode, we can discard the
  318. * inode's preallocations.
  319. */
  320. if ((ei->i_reserved_data_blocks == 0) &&
  321. (atomic_read(&inode->i_writecount) == 0))
  322. ext4_discard_preallocations(inode);
  323. }
  324. static int __check_block_validity(struct inode *inode, const char *func,
  325. unsigned int line,
  326. struct ext4_map_blocks *map)
  327. {
  328. if (!ext4_data_block_valid(EXT4_SB(inode->i_sb), map->m_pblk,
  329. map->m_len)) {
  330. ext4_error_inode(inode, func, line, map->m_pblk,
  331. "lblock %lu mapped to illegal pblock "
  332. "(length %d)", (unsigned long) map->m_lblk,
  333. map->m_len);
  334. return -EFSCORRUPTED;
  335. }
  336. return 0;
  337. }
  338. int ext4_issue_zeroout(struct inode *inode, ext4_lblk_t lblk, ext4_fsblk_t pblk,
  339. ext4_lblk_t len)
  340. {
  341. int ret;
  342. if (ext4_encrypted_inode(inode))
  343. return ext4_encrypted_zeroout(inode, lblk, pblk, len);
  344. ret = sb_issue_zeroout(inode->i_sb, pblk, len, GFP_NOFS);
  345. if (ret > 0)
  346. ret = 0;
  347. return ret;
  348. }
  349. #define check_block_validity(inode, map) \
  350. __check_block_validity((inode), __func__, __LINE__, (map))
  351. #ifdef ES_AGGRESSIVE_TEST
  352. static void ext4_map_blocks_es_recheck(handle_t *handle,
  353. struct inode *inode,
  354. struct ext4_map_blocks *es_map,
  355. struct ext4_map_blocks *map,
  356. int flags)
  357. {
  358. int retval;
  359. map->m_flags = 0;
  360. /*
  361. * There is a race window that the result is not the same.
  362. * e.g. xfstests #223 when dioread_nolock enables. The reason
  363. * is that we lookup a block mapping in extent status tree with
  364. * out taking i_data_sem. So at the time the unwritten extent
  365. * could be converted.
  366. */
  367. down_read(&EXT4_I(inode)->i_data_sem);
  368. if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
  369. retval = ext4_ext_map_blocks(handle, inode, map, flags &
  370. EXT4_GET_BLOCKS_KEEP_SIZE);
  371. } else {
  372. retval = ext4_ind_map_blocks(handle, inode, map, flags &
  373. EXT4_GET_BLOCKS_KEEP_SIZE);
  374. }
  375. up_read((&EXT4_I(inode)->i_data_sem));
  376. /*
  377. * We don't check m_len because extent will be collpased in status
  378. * tree. So the m_len might not equal.
  379. */
  380. if (es_map->m_lblk != map->m_lblk ||
  381. es_map->m_flags != map->m_flags ||
  382. es_map->m_pblk != map->m_pblk) {
  383. printk("ES cache assertion failed for inode: %lu "
  384. "es_cached ex [%d/%d/%llu/%x] != "
  385. "found ex [%d/%d/%llu/%x] retval %d flags %x\n",
  386. inode->i_ino, es_map->m_lblk, es_map->m_len,
  387. es_map->m_pblk, es_map->m_flags, map->m_lblk,
  388. map->m_len, map->m_pblk, map->m_flags,
  389. retval, flags);
  390. }
  391. }
  392. #endif /* ES_AGGRESSIVE_TEST */
  393. /*
  394. * The ext4_map_blocks() function tries to look up the requested blocks,
  395. * and returns if the blocks are already mapped.
  396. *
  397. * Otherwise it takes the write lock of the i_data_sem and allocate blocks
  398. * and store the allocated blocks in the result buffer head and mark it
  399. * mapped.
  400. *
  401. * If file type is extents based, it will call ext4_ext_map_blocks(),
  402. * Otherwise, call with ext4_ind_map_blocks() to handle indirect mapping
  403. * based files
  404. *
  405. * On success, it returns the number of blocks being mapped or allocated. if
  406. * create==0 and the blocks are pre-allocated and unwritten, the resulting @map
  407. * is marked as unwritten. If the create == 1, it will mark @map as mapped.
  408. *
  409. * It returns 0 if plain look up failed (blocks have not been allocated), in
  410. * that case, @map is returned as unmapped but we still do fill map->m_len to
  411. * indicate the length of a hole starting at map->m_lblk.
  412. *
  413. * It returns the error in case of allocation failure.
  414. */
  415. int ext4_map_blocks(handle_t *handle, struct inode *inode,
  416. struct ext4_map_blocks *map, int flags)
  417. {
  418. struct extent_status es;
  419. int retval;
  420. int ret = 0;
  421. #ifdef ES_AGGRESSIVE_TEST
  422. struct ext4_map_blocks orig_map;
  423. memcpy(&orig_map, map, sizeof(*map));
  424. #endif
  425. map->m_flags = 0;
  426. ext_debug("ext4_map_blocks(): inode %lu, flag %d, max_blocks %u,"
  427. "logical block %lu\n", inode->i_ino, flags, map->m_len,
  428. (unsigned long) map->m_lblk);
  429. /*
  430. * ext4_map_blocks returns an int, and m_len is an unsigned int
  431. */
  432. if (unlikely(map->m_len > INT_MAX))
  433. map->m_len = INT_MAX;
  434. /* We can handle the block number less than EXT_MAX_BLOCKS */
  435. if (unlikely(map->m_lblk >= EXT_MAX_BLOCKS))
  436. return -EFSCORRUPTED;
  437. /* Lookup extent status tree firstly */
  438. if (ext4_es_lookup_extent(inode, map->m_lblk, &es)) {
  439. if (ext4_es_is_written(&es) || ext4_es_is_unwritten(&es)) {
  440. map->m_pblk = ext4_es_pblock(&es) +
  441. map->m_lblk - es.es_lblk;
  442. map->m_flags |= ext4_es_is_written(&es) ?
  443. EXT4_MAP_MAPPED : EXT4_MAP_UNWRITTEN;
  444. retval = es.es_len - (map->m_lblk - es.es_lblk);
  445. if (retval > map->m_len)
  446. retval = map->m_len;
  447. map->m_len = retval;
  448. } else if (ext4_es_is_delayed(&es) || ext4_es_is_hole(&es)) {
  449. map->m_pblk = 0;
  450. retval = es.es_len - (map->m_lblk - es.es_lblk);
  451. if (retval > map->m_len)
  452. retval = map->m_len;
  453. map->m_len = retval;
  454. retval = 0;
  455. } else {
  456. BUG_ON(1);
  457. }
  458. #ifdef ES_AGGRESSIVE_TEST
  459. ext4_map_blocks_es_recheck(handle, inode, map,
  460. &orig_map, flags);
  461. #endif
  462. goto found;
  463. }
  464. /*
  465. * Try to see if we can get the block without requesting a new
  466. * file system block.
  467. */
  468. down_read(&EXT4_I(inode)->i_data_sem);
  469. if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
  470. retval = ext4_ext_map_blocks(handle, inode, map, flags &
  471. EXT4_GET_BLOCKS_KEEP_SIZE);
  472. } else {
  473. retval = ext4_ind_map_blocks(handle, inode, map, flags &
  474. EXT4_GET_BLOCKS_KEEP_SIZE);
  475. }
  476. if (retval > 0) {
  477. unsigned int status;
  478. if (unlikely(retval != map->m_len)) {
  479. ext4_warning(inode->i_sb,
  480. "ES len assertion failed for inode "
  481. "%lu: retval %d != map->m_len %d",
  482. inode->i_ino, retval, map->m_len);
  483. WARN_ON(1);
  484. }
  485. status = map->m_flags & EXT4_MAP_UNWRITTEN ?
  486. EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN;
  487. if (!(flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) &&
  488. !(status & EXTENT_STATUS_WRITTEN) &&
  489. ext4_find_delalloc_range(inode, map->m_lblk,
  490. map->m_lblk + map->m_len - 1))
  491. status |= EXTENT_STATUS_DELAYED;
  492. ret = ext4_es_insert_extent(inode, map->m_lblk,
  493. map->m_len, map->m_pblk, status);
  494. if (ret < 0)
  495. retval = ret;
  496. }
  497. up_read((&EXT4_I(inode)->i_data_sem));
  498. found:
  499. if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) {
  500. ret = check_block_validity(inode, map);
  501. if (ret != 0)
  502. return ret;
  503. }
  504. /* If it is only a block(s) look up */
  505. if ((flags & EXT4_GET_BLOCKS_CREATE) == 0)
  506. return retval;
  507. /*
  508. * Returns if the blocks have already allocated
  509. *
  510. * Note that if blocks have been preallocated
  511. * ext4_ext_get_block() returns the create = 0
  512. * with buffer head unmapped.
  513. */
  514. if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED)
  515. /*
  516. * If we need to convert extent to unwritten
  517. * we continue and do the actual work in
  518. * ext4_ext_map_blocks()
  519. */
  520. if (!(flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN))
  521. return retval;
  522. /*
  523. * Here we clear m_flags because after allocating an new extent,
  524. * it will be set again.
  525. */
  526. map->m_flags &= ~EXT4_MAP_FLAGS;
  527. /*
  528. * New blocks allocate and/or writing to unwritten extent
  529. * will possibly result in updating i_data, so we take
  530. * the write lock of i_data_sem, and call get_block()
  531. * with create == 1 flag.
  532. */
  533. down_write(&EXT4_I(inode)->i_data_sem);
  534. /*
  535. * We need to check for EXT4 here because migrate
  536. * could have changed the inode type in between
  537. */
  538. if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
  539. retval = ext4_ext_map_blocks(handle, inode, map, flags);
  540. } else {
  541. retval = ext4_ind_map_blocks(handle, inode, map, flags);
  542. if (retval > 0 && map->m_flags & EXT4_MAP_NEW) {
  543. /*
  544. * We allocated new blocks which will result in
  545. * i_data's format changing. Force the migrate
  546. * to fail by clearing migrate flags
  547. */
  548. ext4_clear_inode_state(inode, EXT4_STATE_EXT_MIGRATE);
  549. }
  550. /*
  551. * Update reserved blocks/metadata blocks after successful
  552. * block allocation which had been deferred till now. We don't
  553. * support fallocate for non extent files. So we can update
  554. * reserve space here.
  555. */
  556. if ((retval > 0) &&
  557. (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE))
  558. ext4_da_update_reserve_space(inode, retval, 1);
  559. }
  560. if (retval > 0) {
  561. unsigned int status;
  562. if (unlikely(retval != map->m_len)) {
  563. ext4_warning(inode->i_sb,
  564. "ES len assertion failed for inode "
  565. "%lu: retval %d != map->m_len %d",
  566. inode->i_ino, retval, map->m_len);
  567. WARN_ON(1);
  568. }
  569. /*
  570. * We have to zeroout blocks before inserting them into extent
  571. * status tree. Otherwise someone could look them up there and
  572. * use them before they are really zeroed.
  573. */
  574. if (flags & EXT4_GET_BLOCKS_ZERO &&
  575. map->m_flags & EXT4_MAP_MAPPED &&
  576. map->m_flags & EXT4_MAP_NEW) {
  577. ret = ext4_issue_zeroout(inode, map->m_lblk,
  578. map->m_pblk, map->m_len);
  579. if (ret) {
  580. retval = ret;
  581. goto out_sem;
  582. }
  583. }
  584. /*
  585. * If the extent has been zeroed out, we don't need to update
  586. * extent status tree.
  587. */
  588. if ((flags & EXT4_GET_BLOCKS_PRE_IO) &&
  589. ext4_es_lookup_extent(inode, map->m_lblk, &es)) {
  590. if (ext4_es_is_written(&es))
  591. goto out_sem;
  592. }
  593. status = map->m_flags & EXT4_MAP_UNWRITTEN ?
  594. EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN;
  595. if (!(flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) &&
  596. !(status & EXTENT_STATUS_WRITTEN) &&
  597. ext4_find_delalloc_range(inode, map->m_lblk,
  598. map->m_lblk + map->m_len - 1))
  599. status |= EXTENT_STATUS_DELAYED;
  600. ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len,
  601. map->m_pblk, status);
  602. if (ret < 0) {
  603. retval = ret;
  604. goto out_sem;
  605. }
  606. }
  607. out_sem:
  608. up_write((&EXT4_I(inode)->i_data_sem));
  609. if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) {
  610. ret = check_block_validity(inode, map);
  611. if (ret != 0)
  612. return ret;
  613. /*
  614. * Inodes with freshly allocated blocks where contents will be
  615. * visible after transaction commit must be on transaction's
  616. * ordered data list.
  617. */
  618. if (map->m_flags & EXT4_MAP_NEW &&
  619. !(map->m_flags & EXT4_MAP_UNWRITTEN) &&
  620. !(flags & EXT4_GET_BLOCKS_ZERO) &&
  621. !IS_NOQUOTA(inode) &&
  622. ext4_should_order_data(inode)) {
  623. if (flags & EXT4_GET_BLOCKS_IO_SUBMIT)
  624. ret = ext4_jbd2_inode_add_wait(handle, inode);
  625. else
  626. ret = ext4_jbd2_inode_add_write(handle, inode);
  627. if (ret)
  628. return ret;
  629. }
  630. }
  631. return retval;
  632. }
  633. /*
  634. * Update EXT4_MAP_FLAGS in bh->b_state. For buffer heads attached to pages
  635. * we have to be careful as someone else may be manipulating b_state as well.
  636. */
  637. static void ext4_update_bh_state(struct buffer_head *bh, unsigned long flags)
  638. {
  639. unsigned long old_state;
  640. unsigned long new_state;
  641. flags &= EXT4_MAP_FLAGS;
  642. /* Dummy buffer_head? Set non-atomically. */
  643. if (!bh->b_page) {
  644. bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | flags;
  645. return;
  646. }
  647. /*
  648. * Someone else may be modifying b_state. Be careful! This is ugly but
  649. * once we get rid of using bh as a container for mapping information
  650. * to pass to / from get_block functions, this can go away.
  651. */
  652. do {
  653. old_state = READ_ONCE(bh->b_state);
  654. new_state = (old_state & ~EXT4_MAP_FLAGS) | flags;
  655. } while (unlikely(
  656. cmpxchg(&bh->b_state, old_state, new_state) != old_state));
  657. }
  658. static int _ext4_get_block(struct inode *inode, sector_t iblock,
  659. struct buffer_head *bh, int flags)
  660. {
  661. struct ext4_map_blocks map;
  662. int ret = 0;
  663. if (ext4_has_inline_data(inode))
  664. return -ERANGE;
  665. map.m_lblk = iblock;
  666. map.m_len = bh->b_size >> inode->i_blkbits;
  667. ret = ext4_map_blocks(ext4_journal_current_handle(), inode, &map,
  668. flags);
  669. if (ret > 0) {
  670. map_bh(bh, inode->i_sb, map.m_pblk);
  671. ext4_update_bh_state(bh, map.m_flags);
  672. bh->b_size = inode->i_sb->s_blocksize * map.m_len;
  673. ret = 0;
  674. }
  675. return ret;
  676. }
  677. int ext4_get_block(struct inode *inode, sector_t iblock,
  678. struct buffer_head *bh, int create)
  679. {
  680. return _ext4_get_block(inode, iblock, bh,
  681. create ? EXT4_GET_BLOCKS_CREATE : 0);
  682. }
  683. /*
  684. * Get block function used when preparing for buffered write if we require
  685. * creating an unwritten extent if blocks haven't been allocated. The extent
  686. * will be converted to written after the IO is complete.
  687. */
  688. int ext4_get_block_unwritten(struct inode *inode, sector_t iblock,
  689. struct buffer_head *bh_result, int create)
  690. {
  691. ext4_debug("ext4_get_block_unwritten: inode %lu, create flag %d\n",
  692. inode->i_ino, create);
  693. return _ext4_get_block(inode, iblock, bh_result,
  694. EXT4_GET_BLOCKS_IO_CREATE_EXT);
  695. }
  696. /* Maximum number of blocks we map for direct IO at once. */
  697. #define DIO_MAX_BLOCKS 4096
  698. /*
  699. * Get blocks function for the cases that need to start a transaction -
  700. * generally difference cases of direct IO and DAX IO. It also handles retries
  701. * in case of ENOSPC.
  702. */
  703. static int ext4_get_block_trans(struct inode *inode, sector_t iblock,
  704. struct buffer_head *bh_result, int flags)
  705. {
  706. int dio_credits;
  707. handle_t *handle;
  708. int retries = 0;
  709. int ret;
  710. /* Trim mapping request to maximum we can map at once for DIO */
  711. if (bh_result->b_size >> inode->i_blkbits > DIO_MAX_BLOCKS)
  712. bh_result->b_size = DIO_MAX_BLOCKS << inode->i_blkbits;
  713. dio_credits = ext4_chunk_trans_blocks(inode,
  714. bh_result->b_size >> inode->i_blkbits);
  715. retry:
  716. handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS, dio_credits);
  717. if (IS_ERR(handle))
  718. return PTR_ERR(handle);
  719. ret = _ext4_get_block(inode, iblock, bh_result, flags);
  720. ext4_journal_stop(handle);
  721. if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
  722. goto retry;
  723. return ret;
  724. }
  725. /* Get block function for DIO reads and writes to inodes without extents */
  726. int ext4_dio_get_block(struct inode *inode, sector_t iblock,
  727. struct buffer_head *bh, int create)
  728. {
  729. /* We don't expect handle for direct IO */
  730. WARN_ON_ONCE(ext4_journal_current_handle());
  731. if (!create)
  732. return _ext4_get_block(inode, iblock, bh, 0);
  733. return ext4_get_block_trans(inode, iblock, bh, EXT4_GET_BLOCKS_CREATE);
  734. }
  735. /*
  736. * Get block function for AIO DIO writes when we create unwritten extent if
  737. * blocks are not allocated yet. The extent will be converted to written
  738. * after IO is complete.
  739. */
  740. static int ext4_dio_get_block_unwritten_async(struct inode *inode,
  741. sector_t iblock, struct buffer_head *bh_result, int create)
  742. {
  743. int ret;
  744. /* We don't expect handle for direct IO */
  745. WARN_ON_ONCE(ext4_journal_current_handle());
  746. ret = ext4_get_block_trans(inode, iblock, bh_result,
  747. EXT4_GET_BLOCKS_IO_CREATE_EXT);
  748. /*
  749. * When doing DIO using unwritten extents, we need io_end to convert
  750. * unwritten extents to written on IO completion. We allocate io_end
  751. * once we spot unwritten extent and store it in b_private. Generic
  752. * DIO code keeps b_private set and furthermore passes the value to
  753. * our completion callback in 'private' argument.
  754. */
  755. if (!ret && buffer_unwritten(bh_result)) {
  756. if (!bh_result->b_private) {
  757. ext4_io_end_t *io_end;
  758. io_end = ext4_init_io_end(inode, GFP_KERNEL);
  759. if (!io_end)
  760. return -ENOMEM;
  761. bh_result->b_private = io_end;
  762. ext4_set_io_unwritten_flag(inode, io_end);
  763. }
  764. set_buffer_defer_completion(bh_result);
  765. }
  766. return ret;
  767. }
  768. /*
  769. * Get block function for non-AIO DIO writes when we create unwritten extent if
  770. * blocks are not allocated yet. The extent will be converted to written
  771. * after IO is complete from ext4_ext_direct_IO() function.
  772. */
  773. static int ext4_dio_get_block_unwritten_sync(struct inode *inode,
  774. sector_t iblock, struct buffer_head *bh_result, int create)
  775. {
  776. int ret;
  777. /* We don't expect handle for direct IO */
  778. WARN_ON_ONCE(ext4_journal_current_handle());
  779. ret = ext4_get_block_trans(inode, iblock, bh_result,
  780. EXT4_GET_BLOCKS_IO_CREATE_EXT);
  781. /*
  782. * Mark inode as having pending DIO writes to unwritten extents.
  783. * ext4_ext_direct_IO() checks this flag and converts extents to
  784. * written.
  785. */
  786. if (!ret && buffer_unwritten(bh_result))
  787. ext4_set_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN);
  788. return ret;
  789. }
  790. static int ext4_dio_get_block_overwrite(struct inode *inode, sector_t iblock,
  791. struct buffer_head *bh_result, int create)
  792. {
  793. int ret;
  794. ext4_debug("ext4_dio_get_block_overwrite: inode %lu, create flag %d\n",
  795. inode->i_ino, create);
  796. /* We don't expect handle for direct IO */
  797. WARN_ON_ONCE(ext4_journal_current_handle());
  798. ret = _ext4_get_block(inode, iblock, bh_result, 0);
  799. /*
  800. * Blocks should have been preallocated! ext4_file_write_iter() checks
  801. * that.
  802. */
  803. WARN_ON_ONCE(!buffer_mapped(bh_result) || buffer_unwritten(bh_result));
  804. return ret;
  805. }
  806. /*
  807. * `handle' can be NULL if create is zero
  808. */
  809. struct buffer_head *ext4_getblk(handle_t *handle, struct inode *inode,
  810. ext4_lblk_t block, int map_flags)
  811. {
  812. struct ext4_map_blocks map;
  813. struct buffer_head *bh;
  814. int create = map_flags & EXT4_GET_BLOCKS_CREATE;
  815. int err;
  816. J_ASSERT(handle != NULL || create == 0);
  817. map.m_lblk = block;
  818. map.m_len = 1;
  819. err = ext4_map_blocks(handle, inode, &map, map_flags);
  820. if (err == 0)
  821. return create ? ERR_PTR(-ENOSPC) : NULL;
  822. if (err < 0)
  823. return ERR_PTR(err);
  824. bh = sb_getblk(inode->i_sb, map.m_pblk);
  825. if (unlikely(!bh))
  826. return ERR_PTR(-ENOMEM);
  827. if (map.m_flags & EXT4_MAP_NEW) {
  828. J_ASSERT(create != 0);
  829. J_ASSERT(handle != NULL);
  830. /*
  831. * Now that we do not always journal data, we should
  832. * keep in mind whether this should always journal the
  833. * new buffer as metadata. For now, regular file
  834. * writes use ext4_get_block instead, so it's not a
  835. * problem.
  836. */
  837. lock_buffer(bh);
  838. BUFFER_TRACE(bh, "call get_create_access");
  839. err = ext4_journal_get_create_access(handle, bh);
  840. if (unlikely(err)) {
  841. unlock_buffer(bh);
  842. goto errout;
  843. }
  844. if (!buffer_uptodate(bh)) {
  845. memset(bh->b_data, 0, inode->i_sb->s_blocksize);
  846. set_buffer_uptodate(bh);
  847. }
  848. unlock_buffer(bh);
  849. BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
  850. err = ext4_handle_dirty_metadata(handle, inode, bh);
  851. if (unlikely(err))
  852. goto errout;
  853. } else
  854. BUFFER_TRACE(bh, "not a new buffer");
  855. return bh;
  856. errout:
  857. brelse(bh);
  858. return ERR_PTR(err);
  859. }
  860. struct buffer_head *ext4_bread(handle_t *handle, struct inode *inode,
  861. ext4_lblk_t block, int map_flags)
  862. {
  863. struct buffer_head *bh;
  864. bh = ext4_getblk(handle, inode, block, map_flags);
  865. if (IS_ERR(bh))
  866. return bh;
  867. if (!bh || buffer_uptodate(bh))
  868. return bh;
  869. ll_rw_block(READ | REQ_META | REQ_PRIO, 1, &bh);
  870. wait_on_buffer(bh);
  871. if (buffer_uptodate(bh))
  872. return bh;
  873. put_bh(bh);
  874. return ERR_PTR(-EIO);
  875. }
  876. int ext4_walk_page_buffers(handle_t *handle,
  877. struct buffer_head *head,
  878. unsigned from,
  879. unsigned to,
  880. int *partial,
  881. int (*fn)(handle_t *handle,
  882. struct buffer_head *bh))
  883. {
  884. struct buffer_head *bh;
  885. unsigned block_start, block_end;
  886. unsigned blocksize = head->b_size;
  887. int err, ret = 0;
  888. struct buffer_head *next;
  889. for (bh = head, block_start = 0;
  890. ret == 0 && (bh != head || !block_start);
  891. block_start = block_end, bh = next) {
  892. next = bh->b_this_page;
  893. block_end = block_start + blocksize;
  894. if (block_end <= from || block_start >= to) {
  895. if (partial && !buffer_uptodate(bh))
  896. *partial = 1;
  897. continue;
  898. }
  899. err = (*fn)(handle, bh);
  900. if (!ret)
  901. ret = err;
  902. }
  903. return ret;
  904. }
  905. /*
  906. * To preserve ordering, it is essential that the hole instantiation and
  907. * the data write be encapsulated in a single transaction. We cannot
  908. * close off a transaction and start a new one between the ext4_get_block()
  909. * and the commit_write(). So doing the jbd2_journal_start at the start of
  910. * prepare_write() is the right place.
  911. *
  912. * Also, this function can nest inside ext4_writepage(). In that case, we
  913. * *know* that ext4_writepage() has generated enough buffer credits to do the
  914. * whole page. So we won't block on the journal in that case, which is good,
  915. * because the caller may be PF_MEMALLOC.
  916. *
  917. * By accident, ext4 can be reentered when a transaction is open via
  918. * quota file writes. If we were to commit the transaction while thus
  919. * reentered, there can be a deadlock - we would be holding a quota
  920. * lock, and the commit would never complete if another thread had a
  921. * transaction open and was blocking on the quota lock - a ranking
  922. * violation.
  923. *
  924. * So what we do is to rely on the fact that jbd2_journal_stop/journal_start
  925. * will _not_ run commit under these circumstances because handle->h_ref
  926. * is elevated. We'll still have enough credits for the tiny quotafile
  927. * write.
  928. */
  929. int do_journal_get_write_access(handle_t *handle,
  930. struct buffer_head *bh)
  931. {
  932. int dirty = buffer_dirty(bh);
  933. int ret;
  934. if (!buffer_mapped(bh) || buffer_freed(bh))
  935. return 0;
  936. /*
  937. * __block_write_begin() could have dirtied some buffers. Clean
  938. * the dirty bit as jbd2_journal_get_write_access() could complain
  939. * otherwise about fs integrity issues. Setting of the dirty bit
  940. * by __block_write_begin() isn't a real problem here as we clear
  941. * the bit before releasing a page lock and thus writeback cannot
  942. * ever write the buffer.
  943. */
  944. if (dirty)
  945. clear_buffer_dirty(bh);
  946. BUFFER_TRACE(bh, "get write access");
  947. ret = ext4_journal_get_write_access(handle, bh);
  948. if (!ret && dirty)
  949. ret = ext4_handle_dirty_metadata(handle, NULL, bh);
  950. return ret;
  951. }
  952. #ifdef CONFIG_EXT4_FS_ENCRYPTION
  953. static int ext4_block_write_begin(struct page *page, loff_t pos, unsigned len,
  954. get_block_t *get_block)
  955. {
  956. unsigned from = pos & (PAGE_SIZE - 1);
  957. unsigned to = from + len;
  958. struct inode *inode = page->mapping->host;
  959. unsigned block_start, block_end;
  960. sector_t block;
  961. int err = 0;
  962. unsigned blocksize = inode->i_sb->s_blocksize;
  963. unsigned bbits;
  964. struct buffer_head *bh, *head, *wait[2], **wait_bh = wait;
  965. bool decrypt = false;
  966. BUG_ON(!PageLocked(page));
  967. BUG_ON(from > PAGE_SIZE);
  968. BUG_ON(to > PAGE_SIZE);
  969. BUG_ON(from > to);
  970. if (!page_has_buffers(page))
  971. create_empty_buffers(page, blocksize, 0);
  972. head = page_buffers(page);
  973. bbits = ilog2(blocksize);
  974. block = (sector_t)page->index << (PAGE_SHIFT - bbits);
  975. for (bh = head, block_start = 0; bh != head || !block_start;
  976. block++, block_start = block_end, bh = bh->b_this_page) {
  977. block_end = block_start + blocksize;
  978. if (block_end <= from || block_start >= to) {
  979. if (PageUptodate(page)) {
  980. if (!buffer_uptodate(bh))
  981. set_buffer_uptodate(bh);
  982. }
  983. continue;
  984. }
  985. if (buffer_new(bh))
  986. clear_buffer_new(bh);
  987. if (!buffer_mapped(bh)) {
  988. WARN_ON(bh->b_size != blocksize);
  989. err = get_block(inode, block, bh, 1);
  990. if (err)
  991. break;
  992. if (buffer_new(bh)) {
  993. unmap_underlying_metadata(bh->b_bdev,
  994. bh->b_blocknr);
  995. if (PageUptodate(page)) {
  996. clear_buffer_new(bh);
  997. set_buffer_uptodate(bh);
  998. mark_buffer_dirty(bh);
  999. continue;
  1000. }
  1001. if (block_end > to || block_start < from)
  1002. zero_user_segments(page, to, block_end,
  1003. block_start, from);
  1004. continue;
  1005. }
  1006. }
  1007. if (PageUptodate(page)) {
  1008. if (!buffer_uptodate(bh))
  1009. set_buffer_uptodate(bh);
  1010. continue;
  1011. }
  1012. if (!buffer_uptodate(bh) && !buffer_delay(bh) &&
  1013. !buffer_unwritten(bh) &&
  1014. (block_start < from || block_end > to)) {
  1015. ll_rw_block(READ, 1, &bh);
  1016. *wait_bh++ = bh;
  1017. decrypt = ext4_encrypted_inode(inode) &&
  1018. S_ISREG(inode->i_mode);
  1019. }
  1020. }
  1021. /*
  1022. * If we issued read requests, let them complete.
  1023. */
  1024. while (wait_bh > wait) {
  1025. wait_on_buffer(*--wait_bh);
  1026. if (!buffer_uptodate(*wait_bh))
  1027. err = -EIO;
  1028. }
  1029. if (unlikely(err))
  1030. page_zero_new_buffers(page, from, to);
  1031. else if (decrypt)
  1032. err = ext4_decrypt(page);
  1033. return err;
  1034. }
  1035. #endif
  1036. static int ext4_write_begin(struct file *file, struct address_space *mapping,
  1037. loff_t pos, unsigned len, unsigned flags,
  1038. struct page **pagep, void **fsdata)
  1039. {
  1040. struct inode *inode = mapping->host;
  1041. int ret, needed_blocks;
  1042. handle_t *handle;
  1043. int retries = 0;
  1044. struct page *page;
  1045. pgoff_t index;
  1046. unsigned from, to;
  1047. trace_ext4_write_begin(inode, pos, len, flags);
  1048. /*
  1049. * Reserve one block more for addition to orphan list in case
  1050. * we allocate blocks but write fails for some reason
  1051. */
  1052. needed_blocks = ext4_writepage_trans_blocks(inode) + 1;
  1053. index = pos >> PAGE_SHIFT;
  1054. from = pos & (PAGE_SIZE - 1);
  1055. to = from + len;
  1056. if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
  1057. ret = ext4_try_to_write_inline_data(mapping, inode, pos, len,
  1058. flags, pagep);
  1059. if (ret < 0)
  1060. return ret;
  1061. if (ret == 1)
  1062. return 0;
  1063. }
  1064. /*
  1065. * grab_cache_page_write_begin() can take a long time if the
  1066. * system is thrashing due to memory pressure, or if the page
  1067. * is being written back. So grab it first before we start
  1068. * the transaction handle. This also allows us to allocate
  1069. * the page (if needed) without using GFP_NOFS.
  1070. */
  1071. retry_grab:
  1072. page = grab_cache_page_write_begin(mapping, index, flags);
  1073. if (!page)
  1074. return -ENOMEM;
  1075. unlock_page(page);
  1076. retry_journal:
  1077. handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks);
  1078. if (IS_ERR(handle)) {
  1079. put_page(page);
  1080. return PTR_ERR(handle);
  1081. }
  1082. lock_page(page);
  1083. if (page->mapping != mapping) {
  1084. /* The page got truncated from under us */
  1085. unlock_page(page);
  1086. put_page(page);
  1087. ext4_journal_stop(handle);
  1088. goto retry_grab;
  1089. }
  1090. /* In case writeback began while the page was unlocked */
  1091. wait_for_stable_page(page);
  1092. #ifdef CONFIG_EXT4_FS_ENCRYPTION
  1093. if (ext4_should_dioread_nolock(inode))
  1094. ret = ext4_block_write_begin(page, pos, len,
  1095. ext4_get_block_unwritten);
  1096. else
  1097. ret = ext4_block_write_begin(page, pos, len,
  1098. ext4_get_block);
  1099. #else
  1100. if (ext4_should_dioread_nolock(inode))
  1101. ret = __block_write_begin(page, pos, len,
  1102. ext4_get_block_unwritten);
  1103. else
  1104. ret = __block_write_begin(page, pos, len, ext4_get_block);
  1105. #endif
  1106. if (!ret && ext4_should_journal_data(inode)) {
  1107. ret = ext4_walk_page_buffers(handle, page_buffers(page),
  1108. from, to, NULL,
  1109. do_journal_get_write_access);
  1110. }
  1111. if (ret) {
  1112. unlock_page(page);
  1113. /*
  1114. * __block_write_begin may have instantiated a few blocks
  1115. * outside i_size. Trim these off again. Don't need
  1116. * i_size_read because we hold i_mutex.
  1117. *
  1118. * Add inode to orphan list in case we crash before
  1119. * truncate finishes
  1120. */
  1121. if (pos + len > inode->i_size && ext4_can_truncate(inode))
  1122. ext4_orphan_add(handle, inode);
  1123. ext4_journal_stop(handle);
  1124. if (pos + len > inode->i_size) {
  1125. ext4_truncate_failed_write(inode);
  1126. /*
  1127. * If truncate failed early the inode might
  1128. * still be on the orphan list; we need to
  1129. * make sure the inode is removed from the
  1130. * orphan list in that case.
  1131. */
  1132. if (inode->i_nlink)
  1133. ext4_orphan_del(NULL, inode);
  1134. }
  1135. if (ret == -ENOSPC &&
  1136. ext4_should_retry_alloc(inode->i_sb, &retries))
  1137. goto retry_journal;
  1138. put_page(page);
  1139. return ret;
  1140. }
  1141. *pagep = page;
  1142. return ret;
  1143. }
  1144. /* For write_end() in data=journal mode */
  1145. static int write_end_fn(handle_t *handle, struct buffer_head *bh)
  1146. {
  1147. int ret;
  1148. if (!buffer_mapped(bh) || buffer_freed(bh))
  1149. return 0;
  1150. set_buffer_uptodate(bh);
  1151. ret = ext4_handle_dirty_metadata(handle, NULL, bh);
  1152. clear_buffer_meta(bh);
  1153. clear_buffer_prio(bh);
  1154. return ret;
  1155. }
  1156. /*
  1157. * We need to pick up the new inode size which generic_commit_write gave us
  1158. * `file' can be NULL - eg, when called from page_symlink().
  1159. *
  1160. * ext4 never places buffers on inode->i_mapping->private_list. metadata
  1161. * buffers are managed internally.
  1162. */
  1163. static int ext4_write_end(struct file *file,
  1164. struct address_space *mapping,
  1165. loff_t pos, unsigned len, unsigned copied,
  1166. struct page *page, void *fsdata)
  1167. {
  1168. handle_t *handle = ext4_journal_current_handle();
  1169. struct inode *inode = mapping->host;
  1170. loff_t old_size = inode->i_size;
  1171. int ret = 0, ret2;
  1172. int i_size_changed = 0;
  1173. trace_ext4_write_end(inode, pos, len, copied);
  1174. if (ext4_has_inline_data(inode)) {
  1175. ret = ext4_write_inline_data_end(inode, pos, len,
  1176. copied, page);
  1177. if (ret < 0)
  1178. goto errout;
  1179. copied = ret;
  1180. } else
  1181. copied = block_write_end(file, mapping, pos,
  1182. len, copied, page, fsdata);
  1183. /*
  1184. * it's important to update i_size while still holding page lock:
  1185. * page writeout could otherwise come in and zero beyond i_size.
  1186. */
  1187. i_size_changed = ext4_update_inode_size(inode, pos + copied);
  1188. unlock_page(page);
  1189. put_page(page);
  1190. if (old_size < pos)
  1191. pagecache_isize_extended(inode, old_size, pos);
  1192. /*
  1193. * Don't mark the inode dirty under page lock. First, it unnecessarily
  1194. * makes the holding time of page lock longer. Second, it forces lock
  1195. * ordering of page lock and transaction start for journaling
  1196. * filesystems.
  1197. */
  1198. if (i_size_changed)
  1199. ext4_mark_inode_dirty(handle, inode);
  1200. if (pos + len > inode->i_size && ext4_can_truncate(inode))
  1201. /* if we have allocated more blocks and copied
  1202. * less. We will have blocks allocated outside
  1203. * inode->i_size. So truncate them
  1204. */
  1205. ext4_orphan_add(handle, inode);
  1206. errout:
  1207. ret2 = ext4_journal_stop(handle);
  1208. if (!ret)
  1209. ret = ret2;
  1210. if (pos + len > inode->i_size) {
  1211. ext4_truncate_failed_write(inode);
  1212. /*
  1213. * If truncate failed early the inode might still be
  1214. * on the orphan list; we need to make sure the inode
  1215. * is removed from the orphan list in that case.
  1216. */
  1217. if (inode->i_nlink)
  1218. ext4_orphan_del(NULL, inode);
  1219. }
  1220. return ret ? ret : copied;
  1221. }
  1222. /*
  1223. * This is a private version of page_zero_new_buffers() which doesn't
  1224. * set the buffer to be dirty, since in data=journalled mode we need
  1225. * to call ext4_handle_dirty_metadata() instead.
  1226. */
  1227. static void zero_new_buffers(struct page *page, unsigned from, unsigned to)
  1228. {
  1229. unsigned int block_start = 0, block_end;
  1230. struct buffer_head *head, *bh;
  1231. bh = head = page_buffers(page);
  1232. do {
  1233. block_end = block_start + bh->b_size;
  1234. if (buffer_new(bh)) {
  1235. if (block_end > from && block_start < to) {
  1236. if (!PageUptodate(page)) {
  1237. unsigned start, size;
  1238. start = max(from, block_start);
  1239. size = min(to, block_end) - start;
  1240. zero_user(page, start, size);
  1241. set_buffer_uptodate(bh);
  1242. }
  1243. clear_buffer_new(bh);
  1244. }
  1245. }
  1246. block_start = block_end;
  1247. bh = bh->b_this_page;
  1248. } while (bh != head);
  1249. }
  1250. static int ext4_journalled_write_end(struct file *file,
  1251. struct address_space *mapping,
  1252. loff_t pos, unsigned len, unsigned copied,
  1253. struct page *page, void *fsdata)
  1254. {
  1255. handle_t *handle = ext4_journal_current_handle();
  1256. struct inode *inode = mapping->host;
  1257. loff_t old_size = inode->i_size;
  1258. int ret = 0, ret2;
  1259. int partial = 0;
  1260. unsigned from, to;
  1261. int size_changed = 0;
  1262. trace_ext4_journalled_write_end(inode, pos, len, copied);
  1263. from = pos & (PAGE_SIZE - 1);
  1264. to = from + len;
  1265. BUG_ON(!ext4_handle_valid(handle));
  1266. if (ext4_has_inline_data(inode))
  1267. copied = ext4_write_inline_data_end(inode, pos, len,
  1268. copied, page);
  1269. else {
  1270. if (copied < len) {
  1271. if (!PageUptodate(page))
  1272. copied = 0;
  1273. zero_new_buffers(page, from+copied, to);
  1274. }
  1275. ret = ext4_walk_page_buffers(handle, page_buffers(page), from,
  1276. to, &partial, write_end_fn);
  1277. if (!partial)
  1278. SetPageUptodate(page);
  1279. }
  1280. size_changed = ext4_update_inode_size(inode, pos + copied);
  1281. ext4_set_inode_state(inode, EXT4_STATE_JDATA);
  1282. EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid;
  1283. unlock_page(page);
  1284. put_page(page);
  1285. if (old_size < pos)
  1286. pagecache_isize_extended(inode, old_size, pos);
  1287. if (size_changed) {
  1288. ret2 = ext4_mark_inode_dirty(handle, inode);
  1289. if (!ret)
  1290. ret = ret2;
  1291. }
  1292. if (pos + len > inode->i_size && ext4_can_truncate(inode))
  1293. /* if we have allocated more blocks and copied
  1294. * less. We will have blocks allocated outside
  1295. * inode->i_size. So truncate them
  1296. */
  1297. ext4_orphan_add(handle, inode);
  1298. ret2 = ext4_journal_stop(handle);
  1299. if (!ret)
  1300. ret = ret2;
  1301. if (pos + len > inode->i_size) {
  1302. ext4_truncate_failed_write(inode);
  1303. /*
  1304. * If truncate failed early the inode might still be
  1305. * on the orphan list; we need to make sure the inode
  1306. * is removed from the orphan list in that case.
  1307. */
  1308. if (inode->i_nlink)
  1309. ext4_orphan_del(NULL, inode);
  1310. }
  1311. return ret ? ret : copied;
  1312. }
  1313. /*
  1314. * Reserve space for a single cluster
  1315. */
  1316. static int ext4_da_reserve_space(struct inode *inode)
  1317. {
  1318. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  1319. struct ext4_inode_info *ei = EXT4_I(inode);
  1320. int ret;
  1321. /*
  1322. * We will charge metadata quota at writeout time; this saves
  1323. * us from metadata over-estimation, though we may go over by
  1324. * a small amount in the end. Here we just reserve for data.
  1325. */
  1326. ret = dquot_reserve_block(inode, EXT4_C2B(sbi, 1));
  1327. if (ret)
  1328. return ret;
  1329. spin_lock(&ei->i_block_reservation_lock);
  1330. if (ext4_claim_free_clusters(sbi, 1, 0)) {
  1331. spin_unlock(&ei->i_block_reservation_lock);
  1332. dquot_release_reservation_block(inode, EXT4_C2B(sbi, 1));
  1333. return -ENOSPC;
  1334. }
  1335. ei->i_reserved_data_blocks++;
  1336. trace_ext4_da_reserve_space(inode);
  1337. spin_unlock(&ei->i_block_reservation_lock);
  1338. return 0; /* success */
  1339. }
  1340. static void ext4_da_release_space(struct inode *inode, int to_free)
  1341. {
  1342. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  1343. struct ext4_inode_info *ei = EXT4_I(inode);
  1344. if (!to_free)
  1345. return; /* Nothing to release, exit */
  1346. spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
  1347. trace_ext4_da_release_space(inode, to_free);
  1348. if (unlikely(to_free > ei->i_reserved_data_blocks)) {
  1349. /*
  1350. * if there aren't enough reserved blocks, then the
  1351. * counter is messed up somewhere. Since this
  1352. * function is called from invalidate page, it's
  1353. * harmless to return without any action.
  1354. */
  1355. ext4_warning(inode->i_sb, "ext4_da_release_space: "
  1356. "ino %lu, to_free %d with only %d reserved "
  1357. "data blocks", inode->i_ino, to_free,
  1358. ei->i_reserved_data_blocks);
  1359. WARN_ON(1);
  1360. to_free = ei->i_reserved_data_blocks;
  1361. }
  1362. ei->i_reserved_data_blocks -= to_free;
  1363. /* update fs dirty data blocks counter */
  1364. percpu_counter_sub(&sbi->s_dirtyclusters_counter, to_free);
  1365. spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
  1366. dquot_release_reservation_block(inode, EXT4_C2B(sbi, to_free));
  1367. }
  1368. static void ext4_da_page_release_reservation(struct page *page,
  1369. unsigned int offset,
  1370. unsigned int length)
  1371. {
  1372. int to_release = 0, contiguous_blks = 0;
  1373. struct buffer_head *head, *bh;
  1374. unsigned int curr_off = 0;
  1375. struct inode *inode = page->mapping->host;
  1376. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  1377. unsigned int stop = offset + length;
  1378. int num_clusters;
  1379. ext4_fsblk_t lblk;
  1380. BUG_ON(stop > PAGE_SIZE || stop < length);
  1381. head = page_buffers(page);
  1382. bh = head;
  1383. do {
  1384. unsigned int next_off = curr_off + bh->b_size;
  1385. if (next_off > stop)
  1386. break;
  1387. if ((offset <= curr_off) && (buffer_delay(bh))) {
  1388. to_release++;
  1389. contiguous_blks++;
  1390. clear_buffer_delay(bh);
  1391. } else if (contiguous_blks) {
  1392. lblk = page->index <<
  1393. (PAGE_SHIFT - inode->i_blkbits);
  1394. lblk += (curr_off >> inode->i_blkbits) -
  1395. contiguous_blks;
  1396. ext4_es_remove_extent(inode, lblk, contiguous_blks);
  1397. contiguous_blks = 0;
  1398. }
  1399. curr_off = next_off;
  1400. } while ((bh = bh->b_this_page) != head);
  1401. if (contiguous_blks) {
  1402. lblk = page->index << (PAGE_SHIFT - inode->i_blkbits);
  1403. lblk += (curr_off >> inode->i_blkbits) - contiguous_blks;
  1404. ext4_es_remove_extent(inode, lblk, contiguous_blks);
  1405. }
  1406. /* If we have released all the blocks belonging to a cluster, then we
  1407. * need to release the reserved space for that cluster. */
  1408. num_clusters = EXT4_NUM_B2C(sbi, to_release);
  1409. while (num_clusters > 0) {
  1410. lblk = (page->index << (PAGE_SHIFT - inode->i_blkbits)) +
  1411. ((num_clusters - 1) << sbi->s_cluster_bits);
  1412. if (sbi->s_cluster_ratio == 1 ||
  1413. !ext4_find_delalloc_cluster(inode, lblk))
  1414. ext4_da_release_space(inode, 1);
  1415. num_clusters--;
  1416. }
  1417. }
  1418. /*
  1419. * Delayed allocation stuff
  1420. */
  1421. struct mpage_da_data {
  1422. struct inode *inode;
  1423. struct writeback_control *wbc;
  1424. pgoff_t first_page; /* The first page to write */
  1425. pgoff_t next_page; /* Current page to examine */
  1426. pgoff_t last_page; /* Last page to examine */
  1427. /*
  1428. * Extent to map - this can be after first_page because that can be
  1429. * fully mapped. We somewhat abuse m_flags to store whether the extent
  1430. * is delalloc or unwritten.
  1431. */
  1432. struct ext4_map_blocks map;
  1433. struct ext4_io_submit io_submit; /* IO submission data */
  1434. };
  1435. static void mpage_release_unused_pages(struct mpage_da_data *mpd,
  1436. bool invalidate)
  1437. {
  1438. int nr_pages, i;
  1439. pgoff_t index, end;
  1440. struct pagevec pvec;
  1441. struct inode *inode = mpd->inode;
  1442. struct address_space *mapping = inode->i_mapping;
  1443. /* This is necessary when next_page == 0. */
  1444. if (mpd->first_page >= mpd->next_page)
  1445. return;
  1446. index = mpd->first_page;
  1447. end = mpd->next_page - 1;
  1448. if (invalidate) {
  1449. ext4_lblk_t start, last;
  1450. start = index << (PAGE_SHIFT - inode->i_blkbits);
  1451. last = end << (PAGE_SHIFT - inode->i_blkbits);
  1452. ext4_es_remove_extent(inode, start, last - start + 1);
  1453. }
  1454. pagevec_init(&pvec, 0);
  1455. while (index <= end) {
  1456. nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE);
  1457. if (nr_pages == 0)
  1458. break;
  1459. for (i = 0; i < nr_pages; i++) {
  1460. struct page *page = pvec.pages[i];
  1461. if (page->index > end)
  1462. break;
  1463. BUG_ON(!PageLocked(page));
  1464. BUG_ON(PageWriteback(page));
  1465. if (invalidate) {
  1466. block_invalidatepage(page, 0, PAGE_SIZE);
  1467. ClearPageUptodate(page);
  1468. }
  1469. unlock_page(page);
  1470. }
  1471. index = pvec.pages[nr_pages - 1]->index + 1;
  1472. pagevec_release(&pvec);
  1473. }
  1474. }
  1475. static void ext4_print_free_blocks(struct inode *inode)
  1476. {
  1477. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  1478. struct super_block *sb = inode->i_sb;
  1479. struct ext4_inode_info *ei = EXT4_I(inode);
  1480. ext4_msg(sb, KERN_CRIT, "Total free blocks count %lld",
  1481. EXT4_C2B(EXT4_SB(inode->i_sb),
  1482. ext4_count_free_clusters(sb)));
  1483. ext4_msg(sb, KERN_CRIT, "Free/Dirty block details");
  1484. ext4_msg(sb, KERN_CRIT, "free_blocks=%lld",
  1485. (long long) EXT4_C2B(EXT4_SB(sb),
  1486. percpu_counter_sum(&sbi->s_freeclusters_counter)));
  1487. ext4_msg(sb, KERN_CRIT, "dirty_blocks=%lld",
  1488. (long long) EXT4_C2B(EXT4_SB(sb),
  1489. percpu_counter_sum(&sbi->s_dirtyclusters_counter)));
  1490. ext4_msg(sb, KERN_CRIT, "Block reservation details");
  1491. ext4_msg(sb, KERN_CRIT, "i_reserved_data_blocks=%u",
  1492. ei->i_reserved_data_blocks);
  1493. return;
  1494. }
  1495. static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh)
  1496. {
  1497. return (buffer_delay(bh) || buffer_unwritten(bh)) && buffer_dirty(bh);
  1498. }
  1499. /*
  1500. * This function is grabs code from the very beginning of
  1501. * ext4_map_blocks, but assumes that the caller is from delayed write
  1502. * time. This function looks up the requested blocks and sets the
  1503. * buffer delay bit under the protection of i_data_sem.
  1504. */
  1505. static int ext4_da_map_blocks(struct inode *inode, sector_t iblock,
  1506. struct ext4_map_blocks *map,
  1507. struct buffer_head *bh)
  1508. {
  1509. struct extent_status es;
  1510. int retval;
  1511. sector_t invalid_block = ~((sector_t) 0xffff);
  1512. #ifdef ES_AGGRESSIVE_TEST
  1513. struct ext4_map_blocks orig_map;
  1514. memcpy(&orig_map, map, sizeof(*map));
  1515. #endif
  1516. if (invalid_block < ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es))
  1517. invalid_block = ~0;
  1518. map->m_flags = 0;
  1519. ext_debug("ext4_da_map_blocks(): inode %lu, max_blocks %u,"
  1520. "logical block %lu\n", inode->i_ino, map->m_len,
  1521. (unsigned long) map->m_lblk);
  1522. /* Lookup extent status tree firstly */
  1523. if (ext4_es_lookup_extent(inode, iblock, &es)) {
  1524. if (ext4_es_is_hole(&es)) {
  1525. retval = 0;
  1526. down_read(&EXT4_I(inode)->i_data_sem);
  1527. goto add_delayed;
  1528. }
  1529. /*
  1530. * Delayed extent could be allocated by fallocate.
  1531. * So we need to check it.
  1532. */
  1533. if (ext4_es_is_delayed(&es) && !ext4_es_is_unwritten(&es)) {
  1534. map_bh(bh, inode->i_sb, invalid_block);
  1535. set_buffer_new(bh);
  1536. set_buffer_delay(bh);
  1537. return 0;
  1538. }
  1539. map->m_pblk = ext4_es_pblock(&es) + iblock - es.es_lblk;
  1540. retval = es.es_len - (iblock - es.es_lblk);
  1541. if (retval > map->m_len)
  1542. retval = map->m_len;
  1543. map->m_len = retval;
  1544. if (ext4_es_is_written(&es))
  1545. map->m_flags |= EXT4_MAP_MAPPED;
  1546. else if (ext4_es_is_unwritten(&es))
  1547. map->m_flags |= EXT4_MAP_UNWRITTEN;
  1548. else
  1549. BUG_ON(1);
  1550. #ifdef ES_AGGRESSIVE_TEST
  1551. ext4_map_blocks_es_recheck(NULL, inode, map, &orig_map, 0);
  1552. #endif
  1553. return retval;
  1554. }
  1555. /*
  1556. * Try to see if we can get the block without requesting a new
  1557. * file system block.
  1558. */
  1559. down_read(&EXT4_I(inode)->i_data_sem);
  1560. if (ext4_has_inline_data(inode))
  1561. retval = 0;
  1562. else if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
  1563. retval = ext4_ext_map_blocks(NULL, inode, map, 0);
  1564. else
  1565. retval = ext4_ind_map_blocks(NULL, inode, map, 0);
  1566. add_delayed:
  1567. if (retval == 0) {
  1568. int ret;
  1569. /*
  1570. * XXX: __block_prepare_write() unmaps passed block,
  1571. * is it OK?
  1572. */
  1573. /*
  1574. * If the block was allocated from previously allocated cluster,
  1575. * then we don't need to reserve it again. However we still need
  1576. * to reserve metadata for every block we're going to write.
  1577. */
  1578. if (EXT4_SB(inode->i_sb)->s_cluster_ratio == 1 ||
  1579. !ext4_find_delalloc_cluster(inode, map->m_lblk)) {
  1580. ret = ext4_da_reserve_space(inode);
  1581. if (ret) {
  1582. /* not enough space to reserve */
  1583. retval = ret;
  1584. goto out_unlock;
  1585. }
  1586. }
  1587. ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len,
  1588. ~0, EXTENT_STATUS_DELAYED);
  1589. if (ret) {
  1590. retval = ret;
  1591. goto out_unlock;
  1592. }
  1593. map_bh(bh, inode->i_sb, invalid_block);
  1594. set_buffer_new(bh);
  1595. set_buffer_delay(bh);
  1596. } else if (retval > 0) {
  1597. int ret;
  1598. unsigned int status;
  1599. if (unlikely(retval != map->m_len)) {
  1600. ext4_warning(inode->i_sb,
  1601. "ES len assertion failed for inode "
  1602. "%lu: retval %d != map->m_len %d",
  1603. inode->i_ino, retval, map->m_len);
  1604. WARN_ON(1);
  1605. }
  1606. status = map->m_flags & EXT4_MAP_UNWRITTEN ?
  1607. EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN;
  1608. ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len,
  1609. map->m_pblk, status);
  1610. if (ret != 0)
  1611. retval = ret;
  1612. }
  1613. out_unlock:
  1614. up_read((&EXT4_I(inode)->i_data_sem));
  1615. return retval;
  1616. }
  1617. /*
  1618. * This is a special get_block_t callback which is used by
  1619. * ext4_da_write_begin(). It will either return mapped block or
  1620. * reserve space for a single block.
  1621. *
  1622. * For delayed buffer_head we have BH_Mapped, BH_New, BH_Delay set.
  1623. * We also have b_blocknr = -1 and b_bdev initialized properly
  1624. *
  1625. * For unwritten buffer_head we have BH_Mapped, BH_New, BH_Unwritten set.
  1626. * We also have b_blocknr = physicalblock mapping unwritten extent and b_bdev
  1627. * initialized properly.
  1628. */
  1629. int ext4_da_get_block_prep(struct inode *inode, sector_t iblock,
  1630. struct buffer_head *bh, int create)
  1631. {
  1632. struct ext4_map_blocks map;
  1633. int ret = 0;
  1634. BUG_ON(create == 0);
  1635. BUG_ON(bh->b_size != inode->i_sb->s_blocksize);
  1636. map.m_lblk = iblock;
  1637. map.m_len = 1;
  1638. /*
  1639. * first, we need to know whether the block is allocated already
  1640. * preallocated blocks are unmapped but should treated
  1641. * the same as allocated blocks.
  1642. */
  1643. ret = ext4_da_map_blocks(inode, iblock, &map, bh);
  1644. if (ret <= 0)
  1645. return ret;
  1646. map_bh(bh, inode->i_sb, map.m_pblk);
  1647. ext4_update_bh_state(bh, map.m_flags);
  1648. if (buffer_unwritten(bh)) {
  1649. /* A delayed write to unwritten bh should be marked
  1650. * new and mapped. Mapped ensures that we don't do
  1651. * get_block multiple times when we write to the same
  1652. * offset and new ensures that we do proper zero out
  1653. * for partial write.
  1654. */
  1655. set_buffer_new(bh);
  1656. set_buffer_mapped(bh);
  1657. }
  1658. return 0;
  1659. }
  1660. static int bget_one(handle_t *handle, struct buffer_head *bh)
  1661. {
  1662. get_bh(bh);
  1663. return 0;
  1664. }
  1665. static int bput_one(handle_t *handle, struct buffer_head *bh)
  1666. {
  1667. put_bh(bh);
  1668. return 0;
  1669. }
  1670. static int __ext4_journalled_writepage(struct page *page,
  1671. unsigned int len)
  1672. {
  1673. struct address_space *mapping = page->mapping;
  1674. struct inode *inode = mapping->host;
  1675. struct buffer_head *page_bufs = NULL;
  1676. handle_t *handle = NULL;
  1677. int ret = 0, err = 0;
  1678. int inline_data = ext4_has_inline_data(inode);
  1679. struct buffer_head *inode_bh = NULL;
  1680. ClearPageChecked(page);
  1681. if (inline_data) {
  1682. BUG_ON(page->index != 0);
  1683. BUG_ON(len > ext4_get_max_inline_size(inode));
  1684. inode_bh = ext4_journalled_write_inline_data(inode, len, page);
  1685. if (inode_bh == NULL)
  1686. goto out;
  1687. } else {
  1688. page_bufs = page_buffers(page);
  1689. if (!page_bufs) {
  1690. BUG();
  1691. goto out;
  1692. }
  1693. ext4_walk_page_buffers(handle, page_bufs, 0, len,
  1694. NULL, bget_one);
  1695. }
  1696. /*
  1697. * We need to release the page lock before we start the
  1698. * journal, so grab a reference so the page won't disappear
  1699. * out from under us.
  1700. */
  1701. get_page(page);
  1702. unlock_page(page);
  1703. handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE,
  1704. ext4_writepage_trans_blocks(inode));
  1705. if (IS_ERR(handle)) {
  1706. ret = PTR_ERR(handle);
  1707. put_page(page);
  1708. goto out_no_pagelock;
  1709. }
  1710. BUG_ON(!ext4_handle_valid(handle));
  1711. lock_page(page);
  1712. put_page(page);
  1713. if (page->mapping != mapping) {
  1714. /* The page got truncated from under us */
  1715. ext4_journal_stop(handle);
  1716. ret = 0;
  1717. goto out;
  1718. }
  1719. if (inline_data) {
  1720. BUFFER_TRACE(inode_bh, "get write access");
  1721. ret = ext4_journal_get_write_access(handle, inode_bh);
  1722. err = ext4_handle_dirty_metadata(handle, inode, inode_bh);
  1723. } else {
  1724. ret = ext4_walk_page_buffers(handle, page_bufs, 0, len, NULL,
  1725. do_journal_get_write_access);
  1726. err = ext4_walk_page_buffers(handle, page_bufs, 0, len, NULL,
  1727. write_end_fn);
  1728. }
  1729. if (ret == 0)
  1730. ret = err;
  1731. EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid;
  1732. err = ext4_journal_stop(handle);
  1733. if (!ret)
  1734. ret = err;
  1735. if (!ext4_has_inline_data(inode))
  1736. ext4_walk_page_buffers(NULL, page_bufs, 0, len,
  1737. NULL, bput_one);
  1738. ext4_set_inode_state(inode, EXT4_STATE_JDATA);
  1739. out:
  1740. unlock_page(page);
  1741. out_no_pagelock:
  1742. brelse(inode_bh);
  1743. return ret;
  1744. }
  1745. /*
  1746. * Note that we don't need to start a transaction unless we're journaling data
  1747. * because we should have holes filled from ext4_page_mkwrite(). We even don't
  1748. * need to file the inode to the transaction's list in ordered mode because if
  1749. * we are writing back data added by write(), the inode is already there and if
  1750. * we are writing back data modified via mmap(), no one guarantees in which
  1751. * transaction the data will hit the disk. In case we are journaling data, we
  1752. * cannot start transaction directly because transaction start ranks above page
  1753. * lock so we have to do some magic.
  1754. *
  1755. * This function can get called via...
  1756. * - ext4_writepages after taking page lock (have journal handle)
  1757. * - journal_submit_inode_data_buffers (no journal handle)
  1758. * - shrink_page_list via the kswapd/direct reclaim (no journal handle)
  1759. * - grab_page_cache when doing write_begin (have journal handle)
  1760. *
  1761. * We don't do any block allocation in this function. If we have page with
  1762. * multiple blocks we need to write those buffer_heads that are mapped. This
  1763. * is important for mmaped based write. So if we do with blocksize 1K
  1764. * truncate(f, 1024);
  1765. * a = mmap(f, 0, 4096);
  1766. * a[0] = 'a';
  1767. * truncate(f, 4096);
  1768. * we have in the page first buffer_head mapped via page_mkwrite call back
  1769. * but other buffer_heads would be unmapped but dirty (dirty done via the
  1770. * do_wp_page). So writepage should write the first block. If we modify
  1771. * the mmap area beyond 1024 we will again get a page_fault and the
  1772. * page_mkwrite callback will do the block allocation and mark the
  1773. * buffer_heads mapped.
  1774. *
  1775. * We redirty the page if we have any buffer_heads that is either delay or
  1776. * unwritten in the page.
  1777. *
  1778. * We can get recursively called as show below.
  1779. *
  1780. * ext4_writepage() -> kmalloc() -> __alloc_pages() -> page_launder() ->
  1781. * ext4_writepage()
  1782. *
  1783. * But since we don't do any block allocation we should not deadlock.
  1784. * Page also have the dirty flag cleared so we don't get recurive page_lock.
  1785. */
  1786. static int ext4_writepage(struct page *page,
  1787. struct writeback_control *wbc)
  1788. {
  1789. int ret = 0;
  1790. loff_t size;
  1791. unsigned int len;
  1792. struct buffer_head *page_bufs = NULL;
  1793. struct inode *inode = page->mapping->host;
  1794. struct ext4_io_submit io_submit;
  1795. bool keep_towrite = false;
  1796. trace_ext4_writepage(page);
  1797. size = i_size_read(inode);
  1798. if (page->index == size >> PAGE_SHIFT)
  1799. len = size & ~PAGE_MASK;
  1800. else
  1801. len = PAGE_SIZE;
  1802. page_bufs = page_buffers(page);
  1803. /*
  1804. * We cannot do block allocation or other extent handling in this
  1805. * function. If there are buffers needing that, we have to redirty
  1806. * the page. But we may reach here when we do a journal commit via
  1807. * journal_submit_inode_data_buffers() and in that case we must write
  1808. * allocated buffers to achieve data=ordered mode guarantees.
  1809. *
  1810. * Also, if there is only one buffer per page (the fs block
  1811. * size == the page size), if one buffer needs block
  1812. * allocation or needs to modify the extent tree to clear the
  1813. * unwritten flag, we know that the page can't be written at
  1814. * all, so we might as well refuse the write immediately.
  1815. * Unfortunately if the block size != page size, we can't as
  1816. * easily detect this case using ext4_walk_page_buffers(), but
  1817. * for the extremely common case, this is an optimization that
  1818. * skips a useless round trip through ext4_bio_write_page().
  1819. */
  1820. if (ext4_walk_page_buffers(NULL, page_bufs, 0, len, NULL,
  1821. ext4_bh_delay_or_unwritten)) {
  1822. redirty_page_for_writepage(wbc, page);
  1823. if ((current->flags & PF_MEMALLOC) ||
  1824. (inode->i_sb->s_blocksize == PAGE_SIZE)) {
  1825. /*
  1826. * For memory cleaning there's no point in writing only
  1827. * some buffers. So just bail out. Warn if we came here
  1828. * from direct reclaim.
  1829. */
  1830. WARN_ON_ONCE((current->flags & (PF_MEMALLOC|PF_KSWAPD))
  1831. == PF_MEMALLOC);
  1832. unlock_page(page);
  1833. return 0;
  1834. }
  1835. keep_towrite = true;
  1836. }
  1837. if (PageChecked(page) && ext4_should_journal_data(inode))
  1838. /*
  1839. * It's mmapped pagecache. Add buffers and journal it. There
  1840. * doesn't seem much point in redirtying the page here.
  1841. */
  1842. return __ext4_journalled_writepage(page, len);
  1843. ext4_io_submit_init(&io_submit, wbc);
  1844. io_submit.io_end = ext4_init_io_end(inode, GFP_NOFS);
  1845. if (!io_submit.io_end) {
  1846. redirty_page_for_writepage(wbc, page);
  1847. unlock_page(page);
  1848. return -ENOMEM;
  1849. }
  1850. ret = ext4_bio_write_page(&io_submit, page, len, wbc, keep_towrite);
  1851. ext4_io_submit(&io_submit);
  1852. /* Drop io_end reference we got from init */
  1853. ext4_put_io_end_defer(io_submit.io_end);
  1854. return ret;
  1855. }
  1856. static int mpage_submit_page(struct mpage_da_data *mpd, struct page *page)
  1857. {
  1858. int len;
  1859. loff_t size = i_size_read(mpd->inode);
  1860. int err;
  1861. BUG_ON(page->index != mpd->first_page);
  1862. if (page->index == size >> PAGE_SHIFT)
  1863. len = size & ~PAGE_MASK;
  1864. else
  1865. len = PAGE_SIZE;
  1866. clear_page_dirty_for_io(page);
  1867. err = ext4_bio_write_page(&mpd->io_submit, page, len, mpd->wbc, false);
  1868. if (!err)
  1869. mpd->wbc->nr_to_write--;
  1870. mpd->first_page++;
  1871. return err;
  1872. }
  1873. #define BH_FLAGS ((1 << BH_Unwritten) | (1 << BH_Delay))
  1874. /*
  1875. * mballoc gives us at most this number of blocks...
  1876. * XXX: That seems to be only a limitation of ext4_mb_normalize_request().
  1877. * The rest of mballoc seems to handle chunks up to full group size.
  1878. */
  1879. #define MAX_WRITEPAGES_EXTENT_LEN 2048
  1880. /*
  1881. * mpage_add_bh_to_extent - try to add bh to extent of blocks to map
  1882. *
  1883. * @mpd - extent of blocks
  1884. * @lblk - logical number of the block in the file
  1885. * @bh - buffer head we want to add to the extent
  1886. *
  1887. * The function is used to collect contig. blocks in the same state. If the
  1888. * buffer doesn't require mapping for writeback and we haven't started the
  1889. * extent of buffers to map yet, the function returns 'true' immediately - the
  1890. * caller can write the buffer right away. Otherwise the function returns true
  1891. * if the block has been added to the extent, false if the block couldn't be
  1892. * added.
  1893. */
  1894. static bool mpage_add_bh_to_extent(struct mpage_da_data *mpd, ext4_lblk_t lblk,
  1895. struct buffer_head *bh)
  1896. {
  1897. struct ext4_map_blocks *map = &mpd->map;
  1898. /* Buffer that doesn't need mapping for writeback? */
  1899. if (!buffer_dirty(bh) || !buffer_mapped(bh) ||
  1900. (!buffer_delay(bh) && !buffer_unwritten(bh))) {
  1901. /* So far no extent to map => we write the buffer right away */
  1902. if (map->m_len == 0)
  1903. return true;
  1904. return false;
  1905. }
  1906. /* First block in the extent? */
  1907. if (map->m_len == 0) {
  1908. map->m_lblk = lblk;
  1909. map->m_len = 1;
  1910. map->m_flags = bh->b_state & BH_FLAGS;
  1911. return true;
  1912. }
  1913. /* Don't go larger than mballoc is willing to allocate */
  1914. if (map->m_len >= MAX_WRITEPAGES_EXTENT_LEN)
  1915. return false;
  1916. /* Can we merge the block to our big extent? */
  1917. if (lblk == map->m_lblk + map->m_len &&
  1918. (bh->b_state & BH_FLAGS) == map->m_flags) {
  1919. map->m_len++;
  1920. return true;
  1921. }
  1922. return false;
  1923. }
  1924. /*
  1925. * mpage_process_page_bufs - submit page buffers for IO or add them to extent
  1926. *
  1927. * @mpd - extent of blocks for mapping
  1928. * @head - the first buffer in the page
  1929. * @bh - buffer we should start processing from
  1930. * @lblk - logical number of the block in the file corresponding to @bh
  1931. *
  1932. * Walk through page buffers from @bh upto @head (exclusive) and either submit
  1933. * the page for IO if all buffers in this page were mapped and there's no
  1934. * accumulated extent of buffers to map or add buffers in the page to the
  1935. * extent of buffers to map. The function returns 1 if the caller can continue
  1936. * by processing the next page, 0 if it should stop adding buffers to the
  1937. * extent to map because we cannot extend it anymore. It can also return value
  1938. * < 0 in case of error during IO submission.
  1939. */
  1940. static int mpage_process_page_bufs(struct mpage_da_data *mpd,
  1941. struct buffer_head *head,
  1942. struct buffer_head *bh,
  1943. ext4_lblk_t lblk)
  1944. {
  1945. struct inode *inode = mpd->inode;
  1946. int err;
  1947. ext4_lblk_t blocks = (i_size_read(inode) + (1 << inode->i_blkbits) - 1)
  1948. >> inode->i_blkbits;
  1949. do {
  1950. BUG_ON(buffer_locked(bh));
  1951. if (lblk >= blocks || !mpage_add_bh_to_extent(mpd, lblk, bh)) {
  1952. /* Found extent to map? */
  1953. if (mpd->map.m_len)
  1954. return 0;
  1955. /* Everything mapped so far and we hit EOF */
  1956. break;
  1957. }
  1958. } while (lblk++, (bh = bh->b_this_page) != head);
  1959. /* So far everything mapped? Submit the page for IO. */
  1960. if (mpd->map.m_len == 0) {
  1961. err = mpage_submit_page(mpd, head->b_page);
  1962. if (err < 0)
  1963. return err;
  1964. }
  1965. return lblk < blocks;
  1966. }
  1967. /*
  1968. * mpage_map_buffers - update buffers corresponding to changed extent and
  1969. * submit fully mapped pages for IO
  1970. *
  1971. * @mpd - description of extent to map, on return next extent to map
  1972. *
  1973. * Scan buffers corresponding to changed extent (we expect corresponding pages
  1974. * to be already locked) and update buffer state according to new extent state.
  1975. * We map delalloc buffers to their physical location, clear unwritten bits,
  1976. * and mark buffers as uninit when we perform writes to unwritten extents
  1977. * and do extent conversion after IO is finished. If the last page is not fully
  1978. * mapped, we update @map to the next extent in the last page that needs
  1979. * mapping. Otherwise we submit the page for IO.
  1980. */
  1981. static int mpage_map_and_submit_buffers(struct mpage_da_data *mpd)
  1982. {
  1983. struct pagevec pvec;
  1984. int nr_pages, i;
  1985. struct inode *inode = mpd->inode;
  1986. struct buffer_head *head, *bh;
  1987. int bpp_bits = PAGE_SHIFT - inode->i_blkbits;
  1988. pgoff_t start, end;
  1989. ext4_lblk_t lblk;
  1990. sector_t pblock;
  1991. int err;
  1992. start = mpd->map.m_lblk >> bpp_bits;
  1993. end = (mpd->map.m_lblk + mpd->map.m_len - 1) >> bpp_bits;
  1994. lblk = start << bpp_bits;
  1995. pblock = mpd->map.m_pblk;
  1996. pagevec_init(&pvec, 0);
  1997. while (start <= end) {
  1998. nr_pages = pagevec_lookup(&pvec, inode->i_mapping, start,
  1999. PAGEVEC_SIZE);
  2000. if (nr_pages == 0)
  2001. break;
  2002. for (i = 0; i < nr_pages; i++) {
  2003. struct page *page = pvec.pages[i];
  2004. if (page->index > end)
  2005. break;
  2006. /* Up to 'end' pages must be contiguous */
  2007. BUG_ON(page->index != start);
  2008. bh = head = page_buffers(page);
  2009. do {
  2010. if (lblk < mpd->map.m_lblk)
  2011. continue;
  2012. if (lblk >= mpd->map.m_lblk + mpd->map.m_len) {
  2013. /*
  2014. * Buffer after end of mapped extent.
  2015. * Find next buffer in the page to map.
  2016. */
  2017. mpd->map.m_len = 0;
  2018. mpd->map.m_flags = 0;
  2019. /*
  2020. * FIXME: If dioread_nolock supports
  2021. * blocksize < pagesize, we need to make
  2022. * sure we add size mapped so far to
  2023. * io_end->size as the following call
  2024. * can submit the page for IO.
  2025. */
  2026. err = mpage_process_page_bufs(mpd, head,
  2027. bh, lblk);
  2028. pagevec_release(&pvec);
  2029. if (err > 0)
  2030. err = 0;
  2031. return err;
  2032. }
  2033. if (buffer_delay(bh)) {
  2034. clear_buffer_delay(bh);
  2035. bh->b_blocknr = pblock++;
  2036. }
  2037. clear_buffer_unwritten(bh);
  2038. } while (lblk++, (bh = bh->b_this_page) != head);
  2039. /*
  2040. * FIXME: This is going to break if dioread_nolock
  2041. * supports blocksize < pagesize as we will try to
  2042. * convert potentially unmapped parts of inode.
  2043. */
  2044. mpd->io_submit.io_end->size += PAGE_SIZE;
  2045. /* Page fully mapped - let IO run! */
  2046. err = mpage_submit_page(mpd, page);
  2047. if (err < 0) {
  2048. pagevec_release(&pvec);
  2049. return err;
  2050. }
  2051. start++;
  2052. }
  2053. pagevec_release(&pvec);
  2054. }
  2055. /* Extent fully mapped and matches with page boundary. We are done. */
  2056. mpd->map.m_len = 0;
  2057. mpd->map.m_flags = 0;
  2058. return 0;
  2059. }
  2060. static int mpage_map_one_extent(handle_t *handle, struct mpage_da_data *mpd)
  2061. {
  2062. struct inode *inode = mpd->inode;
  2063. struct ext4_map_blocks *map = &mpd->map;
  2064. int get_blocks_flags;
  2065. int err, dioread_nolock;
  2066. trace_ext4_da_write_pages_extent(inode, map);
  2067. /*
  2068. * Call ext4_map_blocks() to allocate any delayed allocation blocks, or
  2069. * to convert an unwritten extent to be initialized (in the case
  2070. * where we have written into one or more preallocated blocks). It is
  2071. * possible that we're going to need more metadata blocks than
  2072. * previously reserved. However we must not fail because we're in
  2073. * writeback and there is nothing we can do about it so it might result
  2074. * in data loss. So use reserved blocks to allocate metadata if
  2075. * possible.
  2076. *
  2077. * We pass in the magic EXT4_GET_BLOCKS_DELALLOC_RESERVE if
  2078. * the blocks in question are delalloc blocks. This indicates
  2079. * that the blocks and quotas has already been checked when
  2080. * the data was copied into the page cache.
  2081. */
  2082. get_blocks_flags = EXT4_GET_BLOCKS_CREATE |
  2083. EXT4_GET_BLOCKS_METADATA_NOFAIL |
  2084. EXT4_GET_BLOCKS_IO_SUBMIT;
  2085. dioread_nolock = ext4_should_dioread_nolock(inode);
  2086. if (dioread_nolock)
  2087. get_blocks_flags |= EXT4_GET_BLOCKS_IO_CREATE_EXT;
  2088. if (map->m_flags & (1 << BH_Delay))
  2089. get_blocks_flags |= EXT4_GET_BLOCKS_DELALLOC_RESERVE;
  2090. err = ext4_map_blocks(handle, inode, map, get_blocks_flags);
  2091. if (err < 0)
  2092. return err;
  2093. if (dioread_nolock && (map->m_flags & EXT4_MAP_UNWRITTEN)) {
  2094. if (!mpd->io_submit.io_end->handle &&
  2095. ext4_handle_valid(handle)) {
  2096. mpd->io_submit.io_end->handle = handle->h_rsv_handle;
  2097. handle->h_rsv_handle = NULL;
  2098. }
  2099. ext4_set_io_unwritten_flag(inode, mpd->io_submit.io_end);
  2100. }
  2101. BUG_ON(map->m_len == 0);
  2102. if (map->m_flags & EXT4_MAP_NEW) {
  2103. struct block_device *bdev = inode->i_sb->s_bdev;
  2104. int i;
  2105. for (i = 0; i < map->m_len; i++)
  2106. unmap_underlying_metadata(bdev, map->m_pblk + i);
  2107. }
  2108. return 0;
  2109. }
  2110. /*
  2111. * mpage_map_and_submit_extent - map extent starting at mpd->lblk of length
  2112. * mpd->len and submit pages underlying it for IO
  2113. *
  2114. * @handle - handle for journal operations
  2115. * @mpd - extent to map
  2116. * @give_up_on_write - we set this to true iff there is a fatal error and there
  2117. * is no hope of writing the data. The caller should discard
  2118. * dirty pages to avoid infinite loops.
  2119. *
  2120. * The function maps extent starting at mpd->lblk of length mpd->len. If it is
  2121. * delayed, blocks are allocated, if it is unwritten, we may need to convert
  2122. * them to initialized or split the described range from larger unwritten
  2123. * extent. Note that we need not map all the described range since allocation
  2124. * can return less blocks or the range is covered by more unwritten extents. We
  2125. * cannot map more because we are limited by reserved transaction credits. On
  2126. * the other hand we always make sure that the last touched page is fully
  2127. * mapped so that it can be written out (and thus forward progress is
  2128. * guaranteed). After mapping we submit all mapped pages for IO.
  2129. */
  2130. static int mpage_map_and_submit_extent(handle_t *handle,
  2131. struct mpage_da_data *mpd,
  2132. bool *give_up_on_write)
  2133. {
  2134. struct inode *inode = mpd->inode;
  2135. struct ext4_map_blocks *map = &mpd->map;
  2136. int err;
  2137. loff_t disksize;
  2138. int progress = 0;
  2139. mpd->io_submit.io_end->offset =
  2140. ((loff_t)map->m_lblk) << inode->i_blkbits;
  2141. do {
  2142. err = mpage_map_one_extent(handle, mpd);
  2143. if (err < 0) {
  2144. struct super_block *sb = inode->i_sb;
  2145. if (EXT4_SB(sb)->s_mount_flags & EXT4_MF_FS_ABORTED)
  2146. goto invalidate_dirty_pages;
  2147. /*
  2148. * Let the uper layers retry transient errors.
  2149. * In the case of ENOSPC, if ext4_count_free_blocks()
  2150. * is non-zero, a commit should free up blocks.
  2151. */
  2152. if ((err == -ENOMEM) ||
  2153. (err == -ENOSPC && ext4_count_free_clusters(sb))) {
  2154. if (progress)
  2155. goto update_disksize;
  2156. return err;
  2157. }
  2158. ext4_msg(sb, KERN_CRIT,
  2159. "Delayed block allocation failed for "
  2160. "inode %lu at logical offset %llu with"
  2161. " max blocks %u with error %d",
  2162. inode->i_ino,
  2163. (unsigned long long)map->m_lblk,
  2164. (unsigned)map->m_len, -err);
  2165. ext4_msg(sb, KERN_CRIT,
  2166. "This should not happen!! Data will "
  2167. "be lost\n");
  2168. if (err == -ENOSPC)
  2169. ext4_print_free_blocks(inode);
  2170. invalidate_dirty_pages:
  2171. *give_up_on_write = true;
  2172. return err;
  2173. }
  2174. progress = 1;
  2175. /*
  2176. * Update buffer state, submit mapped pages, and get us new
  2177. * extent to map
  2178. */
  2179. err = mpage_map_and_submit_buffers(mpd);
  2180. if (err < 0)
  2181. goto update_disksize;
  2182. } while (map->m_len);
  2183. update_disksize:
  2184. /*
  2185. * Update on-disk size after IO is submitted. Races with
  2186. * truncate are avoided by checking i_size under i_data_sem.
  2187. */
  2188. disksize = ((loff_t)mpd->first_page) << PAGE_SHIFT;
  2189. if (disksize > EXT4_I(inode)->i_disksize) {
  2190. int err2;
  2191. loff_t i_size;
  2192. down_write(&EXT4_I(inode)->i_data_sem);
  2193. i_size = i_size_read(inode);
  2194. if (disksize > i_size)
  2195. disksize = i_size;
  2196. if (disksize > EXT4_I(inode)->i_disksize)
  2197. EXT4_I(inode)->i_disksize = disksize;
  2198. err2 = ext4_mark_inode_dirty(handle, inode);
  2199. up_write(&EXT4_I(inode)->i_data_sem);
  2200. if (err2)
  2201. ext4_error(inode->i_sb,
  2202. "Failed to mark inode %lu dirty",
  2203. inode->i_ino);
  2204. if (!err)
  2205. err = err2;
  2206. }
  2207. return err;
  2208. }
  2209. /*
  2210. * Calculate the total number of credits to reserve for one writepages
  2211. * iteration. This is called from ext4_writepages(). We map an extent of
  2212. * up to MAX_WRITEPAGES_EXTENT_LEN blocks and then we go on and finish mapping
  2213. * the last partial page. So in total we can map MAX_WRITEPAGES_EXTENT_LEN +
  2214. * bpp - 1 blocks in bpp different extents.
  2215. */
  2216. static int ext4_da_writepages_trans_blocks(struct inode *inode)
  2217. {
  2218. int bpp = ext4_journal_blocks_per_page(inode);
  2219. return ext4_meta_trans_blocks(inode,
  2220. MAX_WRITEPAGES_EXTENT_LEN + bpp - 1, bpp);
  2221. }
  2222. /*
  2223. * mpage_prepare_extent_to_map - find & lock contiguous range of dirty pages
  2224. * and underlying extent to map
  2225. *
  2226. * @mpd - where to look for pages
  2227. *
  2228. * Walk dirty pages in the mapping. If they are fully mapped, submit them for
  2229. * IO immediately. When we find a page which isn't mapped we start accumulating
  2230. * extent of buffers underlying these pages that needs mapping (formed by
  2231. * either delayed or unwritten buffers). We also lock the pages containing
  2232. * these buffers. The extent found is returned in @mpd structure (starting at
  2233. * mpd->lblk with length mpd->len blocks).
  2234. *
  2235. * Note that this function can attach bios to one io_end structure which are
  2236. * neither logically nor physically contiguous. Although it may seem as an
  2237. * unnecessary complication, it is actually inevitable in blocksize < pagesize
  2238. * case as we need to track IO to all buffers underlying a page in one io_end.
  2239. */
  2240. static int mpage_prepare_extent_to_map(struct mpage_da_data *mpd)
  2241. {
  2242. struct address_space *mapping = mpd->inode->i_mapping;
  2243. struct pagevec pvec;
  2244. unsigned int nr_pages;
  2245. long left = mpd->wbc->nr_to_write;
  2246. pgoff_t index = mpd->first_page;
  2247. pgoff_t end = mpd->last_page;
  2248. int tag;
  2249. int i, err = 0;
  2250. int blkbits = mpd->inode->i_blkbits;
  2251. ext4_lblk_t lblk;
  2252. struct buffer_head *head;
  2253. if (mpd->wbc->sync_mode == WB_SYNC_ALL || mpd->wbc->tagged_writepages)
  2254. tag = PAGECACHE_TAG_TOWRITE;
  2255. else
  2256. tag = PAGECACHE_TAG_DIRTY;
  2257. pagevec_init(&pvec, 0);
  2258. mpd->map.m_len = 0;
  2259. mpd->next_page = index;
  2260. while (index <= end) {
  2261. nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
  2262. min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
  2263. if (nr_pages == 0)
  2264. goto out;
  2265. for (i = 0; i < nr_pages; i++) {
  2266. struct page *page = pvec.pages[i];
  2267. /*
  2268. * At this point, the page may be truncated or
  2269. * invalidated (changing page->mapping to NULL), or
  2270. * even swizzled back from swapper_space to tmpfs file
  2271. * mapping. However, page->index will not change
  2272. * because we have a reference on the page.
  2273. */
  2274. if (page->index > end)
  2275. goto out;
  2276. /*
  2277. * Accumulated enough dirty pages? This doesn't apply
  2278. * to WB_SYNC_ALL mode. For integrity sync we have to
  2279. * keep going because someone may be concurrently
  2280. * dirtying pages, and we might have synced a lot of
  2281. * newly appeared dirty pages, but have not synced all
  2282. * of the old dirty pages.
  2283. */
  2284. if (mpd->wbc->sync_mode == WB_SYNC_NONE && left <= 0)
  2285. goto out;
  2286. /* If we can't merge this page, we are done. */
  2287. if (mpd->map.m_len > 0 && mpd->next_page != page->index)
  2288. goto out;
  2289. lock_page(page);
  2290. /*
  2291. * If the page is no longer dirty, or its mapping no
  2292. * longer corresponds to inode we are writing (which
  2293. * means it has been truncated or invalidated), or the
  2294. * page is already under writeback and we are not doing
  2295. * a data integrity writeback, skip the page
  2296. */
  2297. if (!PageDirty(page) ||
  2298. (PageWriteback(page) &&
  2299. (mpd->wbc->sync_mode == WB_SYNC_NONE)) ||
  2300. unlikely(page->mapping != mapping)) {
  2301. unlock_page(page);
  2302. continue;
  2303. }
  2304. wait_on_page_writeback(page);
  2305. BUG_ON(PageWriteback(page));
  2306. if (mpd->map.m_len == 0)
  2307. mpd->first_page = page->index;
  2308. mpd->next_page = page->index + 1;
  2309. /* Add all dirty buffers to mpd */
  2310. lblk = ((ext4_lblk_t)page->index) <<
  2311. (PAGE_SHIFT - blkbits);
  2312. head = page_buffers(page);
  2313. err = mpage_process_page_bufs(mpd, head, head, lblk);
  2314. if (err <= 0)
  2315. goto out;
  2316. err = 0;
  2317. left--;
  2318. }
  2319. pagevec_release(&pvec);
  2320. cond_resched();
  2321. }
  2322. return 0;
  2323. out:
  2324. pagevec_release(&pvec);
  2325. return err;
  2326. }
  2327. static int __writepage(struct page *page, struct writeback_control *wbc,
  2328. void *data)
  2329. {
  2330. struct address_space *mapping = data;
  2331. int ret = ext4_writepage(page, wbc);
  2332. mapping_set_error(mapping, ret);
  2333. return ret;
  2334. }
  2335. static int ext4_writepages(struct address_space *mapping,
  2336. struct writeback_control *wbc)
  2337. {
  2338. pgoff_t writeback_index = 0;
  2339. long nr_to_write = wbc->nr_to_write;
  2340. int range_whole = 0;
  2341. int cycled = 1;
  2342. handle_t *handle = NULL;
  2343. struct mpage_da_data mpd;
  2344. struct inode *inode = mapping->host;
  2345. int needed_blocks, rsv_blocks = 0, ret = 0;
  2346. struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb);
  2347. bool done;
  2348. struct blk_plug plug;
  2349. bool give_up_on_write = false;
  2350. percpu_down_read(&sbi->s_journal_flag_rwsem);
  2351. trace_ext4_writepages(inode, wbc);
  2352. if (dax_mapping(mapping)) {
  2353. ret = dax_writeback_mapping_range(mapping, inode->i_sb->s_bdev,
  2354. wbc);
  2355. goto out_writepages;
  2356. }
  2357. /*
  2358. * No pages to write? This is mainly a kludge to avoid starting
  2359. * a transaction for special inodes like journal inode on last iput()
  2360. * because that could violate lock ordering on umount
  2361. */
  2362. if (!mapping->nrpages || !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
  2363. goto out_writepages;
  2364. if (ext4_should_journal_data(inode)) {
  2365. struct blk_plug plug;
  2366. blk_start_plug(&plug);
  2367. ret = write_cache_pages(mapping, wbc, __writepage, mapping);
  2368. blk_finish_plug(&plug);
  2369. goto out_writepages;
  2370. }
  2371. /*
  2372. * If the filesystem has aborted, it is read-only, so return
  2373. * right away instead of dumping stack traces later on that
  2374. * will obscure the real source of the problem. We test
  2375. * EXT4_MF_FS_ABORTED instead of sb->s_flag's MS_RDONLY because
  2376. * the latter could be true if the filesystem is mounted
  2377. * read-only, and in that case, ext4_writepages should
  2378. * *never* be called, so if that ever happens, we would want
  2379. * the stack trace.
  2380. */
  2381. if (unlikely(sbi->s_mount_flags & EXT4_MF_FS_ABORTED)) {
  2382. ret = -EROFS;
  2383. goto out_writepages;
  2384. }
  2385. if (ext4_should_dioread_nolock(inode)) {
  2386. /*
  2387. * We may need to convert up to one extent per block in
  2388. * the page and we may dirty the inode.
  2389. */
  2390. rsv_blocks = 1 + (PAGE_SIZE >> inode->i_blkbits);
  2391. }
  2392. /*
  2393. * If we have inline data and arrive here, it means that
  2394. * we will soon create the block for the 1st page, so
  2395. * we'd better clear the inline data here.
  2396. */
  2397. if (ext4_has_inline_data(inode)) {
  2398. /* Just inode will be modified... */
  2399. handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
  2400. if (IS_ERR(handle)) {
  2401. ret = PTR_ERR(handle);
  2402. goto out_writepages;
  2403. }
  2404. BUG_ON(ext4_test_inode_state(inode,
  2405. EXT4_STATE_MAY_INLINE_DATA));
  2406. ext4_destroy_inline_data(handle, inode);
  2407. ext4_journal_stop(handle);
  2408. }
  2409. if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
  2410. range_whole = 1;
  2411. if (wbc->range_cyclic) {
  2412. writeback_index = mapping->writeback_index;
  2413. if (writeback_index)
  2414. cycled = 0;
  2415. mpd.first_page = writeback_index;
  2416. mpd.last_page = -1;
  2417. } else {
  2418. mpd.first_page = wbc->range_start >> PAGE_SHIFT;
  2419. mpd.last_page = wbc->range_end >> PAGE_SHIFT;
  2420. }
  2421. mpd.inode = inode;
  2422. mpd.wbc = wbc;
  2423. ext4_io_submit_init(&mpd.io_submit, wbc);
  2424. retry:
  2425. if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
  2426. tag_pages_for_writeback(mapping, mpd.first_page, mpd.last_page);
  2427. done = false;
  2428. blk_start_plug(&plug);
  2429. while (!done && mpd.first_page <= mpd.last_page) {
  2430. /* For each extent of pages we use new io_end */
  2431. mpd.io_submit.io_end = ext4_init_io_end(inode, GFP_KERNEL);
  2432. if (!mpd.io_submit.io_end) {
  2433. ret = -ENOMEM;
  2434. break;
  2435. }
  2436. /*
  2437. * We have two constraints: We find one extent to map and we
  2438. * must always write out whole page (makes a difference when
  2439. * blocksize < pagesize) so that we don't block on IO when we
  2440. * try to write out the rest of the page. Journalled mode is
  2441. * not supported by delalloc.
  2442. */
  2443. BUG_ON(ext4_should_journal_data(inode));
  2444. needed_blocks = ext4_da_writepages_trans_blocks(inode);
  2445. /* start a new transaction */
  2446. handle = ext4_journal_start_with_reserve(inode,
  2447. EXT4_HT_WRITE_PAGE, needed_blocks, rsv_blocks);
  2448. if (IS_ERR(handle)) {
  2449. ret = PTR_ERR(handle);
  2450. ext4_msg(inode->i_sb, KERN_CRIT, "%s: jbd2_start: "
  2451. "%ld pages, ino %lu; err %d", __func__,
  2452. wbc->nr_to_write, inode->i_ino, ret);
  2453. /* Release allocated io_end */
  2454. ext4_put_io_end(mpd.io_submit.io_end);
  2455. break;
  2456. }
  2457. trace_ext4_da_write_pages(inode, mpd.first_page, mpd.wbc);
  2458. ret = mpage_prepare_extent_to_map(&mpd);
  2459. if (!ret) {
  2460. if (mpd.map.m_len)
  2461. ret = mpage_map_and_submit_extent(handle, &mpd,
  2462. &give_up_on_write);
  2463. else {
  2464. /*
  2465. * We scanned the whole range (or exhausted
  2466. * nr_to_write), submitted what was mapped and
  2467. * didn't find anything needing mapping. We are
  2468. * done.
  2469. */
  2470. done = true;
  2471. }
  2472. }
  2473. ext4_journal_stop(handle);
  2474. /* Submit prepared bio */
  2475. ext4_io_submit(&mpd.io_submit);
  2476. /* Unlock pages we didn't use */
  2477. mpage_release_unused_pages(&mpd, give_up_on_write);
  2478. /* Drop our io_end reference we got from init */
  2479. ext4_put_io_end(mpd.io_submit.io_end);
  2480. if (ret == -ENOSPC && sbi->s_journal) {
  2481. /*
  2482. * Commit the transaction which would
  2483. * free blocks released in the transaction
  2484. * and try again
  2485. */
  2486. jbd2_journal_force_commit_nested(sbi->s_journal);
  2487. ret = 0;
  2488. continue;
  2489. }
  2490. /* Fatal error - ENOMEM, EIO... */
  2491. if (ret)
  2492. break;
  2493. }
  2494. blk_finish_plug(&plug);
  2495. if (!ret && !cycled && wbc->nr_to_write > 0) {
  2496. cycled = 1;
  2497. mpd.last_page = writeback_index - 1;
  2498. mpd.first_page = 0;
  2499. goto retry;
  2500. }
  2501. /* Update index */
  2502. if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
  2503. /*
  2504. * Set the writeback_index so that range_cyclic
  2505. * mode will write it back later
  2506. */
  2507. mapping->writeback_index = mpd.first_page;
  2508. out_writepages:
  2509. trace_ext4_writepages_result(inode, wbc, ret,
  2510. nr_to_write - wbc->nr_to_write);
  2511. percpu_up_read(&sbi->s_journal_flag_rwsem);
  2512. return ret;
  2513. }
  2514. static int ext4_nonda_switch(struct super_block *sb)
  2515. {
  2516. s64 free_clusters, dirty_clusters;
  2517. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2518. /*
  2519. * switch to non delalloc mode if we are running low
  2520. * on free block. The free block accounting via percpu
  2521. * counters can get slightly wrong with percpu_counter_batch getting
  2522. * accumulated on each CPU without updating global counters
  2523. * Delalloc need an accurate free block accounting. So switch
  2524. * to non delalloc when we are near to error range.
  2525. */
  2526. free_clusters =
  2527. percpu_counter_read_positive(&sbi->s_freeclusters_counter);
  2528. dirty_clusters =
  2529. percpu_counter_read_positive(&sbi->s_dirtyclusters_counter);
  2530. /*
  2531. * Start pushing delalloc when 1/2 of free blocks are dirty.
  2532. */
  2533. if (dirty_clusters && (free_clusters < 2 * dirty_clusters))
  2534. try_to_writeback_inodes_sb(sb, WB_REASON_FS_FREE_SPACE);
  2535. if (2 * free_clusters < 3 * dirty_clusters ||
  2536. free_clusters < (dirty_clusters + EXT4_FREECLUSTERS_WATERMARK)) {
  2537. /*
  2538. * free block count is less than 150% of dirty blocks
  2539. * or free blocks is less than watermark
  2540. */
  2541. return 1;
  2542. }
  2543. return 0;
  2544. }
  2545. /* We always reserve for an inode update; the superblock could be there too */
  2546. static int ext4_da_write_credits(struct inode *inode, loff_t pos, unsigned len)
  2547. {
  2548. if (likely(ext4_has_feature_large_file(inode->i_sb)))
  2549. return 1;
  2550. if (pos + len <= 0x7fffffffULL)
  2551. return 1;
  2552. /* We might need to update the superblock to set LARGE_FILE */
  2553. return 2;
  2554. }
  2555. static int ext4_da_write_begin(struct file *file, struct address_space *mapping,
  2556. loff_t pos, unsigned len, unsigned flags,
  2557. struct page **pagep, void **fsdata)
  2558. {
  2559. int ret, retries = 0;
  2560. struct page *page;
  2561. pgoff_t index;
  2562. struct inode *inode = mapping->host;
  2563. handle_t *handle;
  2564. index = pos >> PAGE_SHIFT;
  2565. if (ext4_nonda_switch(inode->i_sb)) {
  2566. *fsdata = (void *)FALL_BACK_TO_NONDELALLOC;
  2567. return ext4_write_begin(file, mapping, pos,
  2568. len, flags, pagep, fsdata);
  2569. }
  2570. *fsdata = (void *)0;
  2571. trace_ext4_da_write_begin(inode, pos, len, flags);
  2572. if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
  2573. ret = ext4_da_write_inline_data_begin(mapping, inode,
  2574. pos, len, flags,
  2575. pagep, fsdata);
  2576. if (ret < 0)
  2577. return ret;
  2578. if (ret == 1)
  2579. return 0;
  2580. }
  2581. /*
  2582. * grab_cache_page_write_begin() can take a long time if the
  2583. * system is thrashing due to memory pressure, or if the page
  2584. * is being written back. So grab it first before we start
  2585. * the transaction handle. This also allows us to allocate
  2586. * the page (if needed) without using GFP_NOFS.
  2587. */
  2588. retry_grab:
  2589. page = grab_cache_page_write_begin(mapping, index, flags);
  2590. if (!page)
  2591. return -ENOMEM;
  2592. unlock_page(page);
  2593. /*
  2594. * With delayed allocation, we don't log the i_disksize update
  2595. * if there is delayed block allocation. But we still need
  2596. * to journalling the i_disksize update if writes to the end
  2597. * of file which has an already mapped buffer.
  2598. */
  2599. retry_journal:
  2600. handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE,
  2601. ext4_da_write_credits(inode, pos, len));
  2602. if (IS_ERR(handle)) {
  2603. put_page(page);
  2604. return PTR_ERR(handle);
  2605. }
  2606. lock_page(page);
  2607. if (page->mapping != mapping) {
  2608. /* The page got truncated from under us */
  2609. unlock_page(page);
  2610. put_page(page);
  2611. ext4_journal_stop(handle);
  2612. goto retry_grab;
  2613. }
  2614. /* In case writeback began while the page was unlocked */
  2615. wait_for_stable_page(page);
  2616. #ifdef CONFIG_EXT4_FS_ENCRYPTION
  2617. ret = ext4_block_write_begin(page, pos, len,
  2618. ext4_da_get_block_prep);
  2619. #else
  2620. ret = __block_write_begin(page, pos, len, ext4_da_get_block_prep);
  2621. #endif
  2622. if (ret < 0) {
  2623. unlock_page(page);
  2624. ext4_journal_stop(handle);
  2625. /*
  2626. * block_write_begin may have instantiated a few blocks
  2627. * outside i_size. Trim these off again. Don't need
  2628. * i_size_read because we hold i_mutex.
  2629. */
  2630. if (pos + len > inode->i_size)
  2631. ext4_truncate_failed_write(inode);
  2632. if (ret == -ENOSPC &&
  2633. ext4_should_retry_alloc(inode->i_sb, &retries))
  2634. goto retry_journal;
  2635. put_page(page);
  2636. return ret;
  2637. }
  2638. *pagep = page;
  2639. return ret;
  2640. }
  2641. /*
  2642. * Check if we should update i_disksize
  2643. * when write to the end of file but not require block allocation
  2644. */
  2645. static int ext4_da_should_update_i_disksize(struct page *page,
  2646. unsigned long offset)
  2647. {
  2648. struct buffer_head *bh;
  2649. struct inode *inode = page->mapping->host;
  2650. unsigned int idx;
  2651. int i;
  2652. bh = page_buffers(page);
  2653. idx = offset >> inode->i_blkbits;
  2654. for (i = 0; i < idx; i++)
  2655. bh = bh->b_this_page;
  2656. if (!buffer_mapped(bh) || (buffer_delay(bh)) || buffer_unwritten(bh))
  2657. return 0;
  2658. return 1;
  2659. }
  2660. static int ext4_da_write_end(struct file *file,
  2661. struct address_space *mapping,
  2662. loff_t pos, unsigned len, unsigned copied,
  2663. struct page *page, void *fsdata)
  2664. {
  2665. struct inode *inode = mapping->host;
  2666. int ret = 0, ret2;
  2667. handle_t *handle = ext4_journal_current_handle();
  2668. loff_t new_i_size;
  2669. unsigned long start, end;
  2670. int write_mode = (int)(unsigned long)fsdata;
  2671. if (write_mode == FALL_BACK_TO_NONDELALLOC)
  2672. return ext4_write_end(file, mapping, pos,
  2673. len, copied, page, fsdata);
  2674. trace_ext4_da_write_end(inode, pos, len, copied);
  2675. start = pos & (PAGE_SIZE - 1);
  2676. end = start + copied - 1;
  2677. /*
  2678. * generic_write_end() will run mark_inode_dirty() if i_size
  2679. * changes. So let's piggyback the i_disksize mark_inode_dirty
  2680. * into that.
  2681. */
  2682. new_i_size = pos + copied;
  2683. if (copied && new_i_size > EXT4_I(inode)->i_disksize) {
  2684. if (ext4_has_inline_data(inode) ||
  2685. ext4_da_should_update_i_disksize(page, end)) {
  2686. ext4_update_i_disksize(inode, new_i_size);
  2687. /* We need to mark inode dirty even if
  2688. * new_i_size is less that inode->i_size
  2689. * bu greater than i_disksize.(hint delalloc)
  2690. */
  2691. ext4_mark_inode_dirty(handle, inode);
  2692. }
  2693. }
  2694. if (write_mode != CONVERT_INLINE_DATA &&
  2695. ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA) &&
  2696. ext4_has_inline_data(inode))
  2697. ret2 = ext4_da_write_inline_data_end(inode, pos, len, copied,
  2698. page);
  2699. else
  2700. ret2 = generic_write_end(file, mapping, pos, len, copied,
  2701. page, fsdata);
  2702. copied = ret2;
  2703. if (ret2 < 0)
  2704. ret = ret2;
  2705. ret2 = ext4_journal_stop(handle);
  2706. if (!ret)
  2707. ret = ret2;
  2708. return ret ? ret : copied;
  2709. }
  2710. static void ext4_da_invalidatepage(struct page *page, unsigned int offset,
  2711. unsigned int length)
  2712. {
  2713. /*
  2714. * Drop reserved blocks
  2715. */
  2716. BUG_ON(!PageLocked(page));
  2717. if (!page_has_buffers(page))
  2718. goto out;
  2719. ext4_da_page_release_reservation(page, offset, length);
  2720. out:
  2721. ext4_invalidatepage(page, offset, length);
  2722. return;
  2723. }
  2724. /*
  2725. * Force all delayed allocation blocks to be allocated for a given inode.
  2726. */
  2727. int ext4_alloc_da_blocks(struct inode *inode)
  2728. {
  2729. trace_ext4_alloc_da_blocks(inode);
  2730. if (!EXT4_I(inode)->i_reserved_data_blocks)
  2731. return 0;
  2732. /*
  2733. * We do something simple for now. The filemap_flush() will
  2734. * also start triggering a write of the data blocks, which is
  2735. * not strictly speaking necessary (and for users of
  2736. * laptop_mode, not even desirable). However, to do otherwise
  2737. * would require replicating code paths in:
  2738. *
  2739. * ext4_writepages() ->
  2740. * write_cache_pages() ---> (via passed in callback function)
  2741. * __mpage_da_writepage() -->
  2742. * mpage_add_bh_to_extent()
  2743. * mpage_da_map_blocks()
  2744. *
  2745. * The problem is that write_cache_pages(), located in
  2746. * mm/page-writeback.c, marks pages clean in preparation for
  2747. * doing I/O, which is not desirable if we're not planning on
  2748. * doing I/O at all.
  2749. *
  2750. * We could call write_cache_pages(), and then redirty all of
  2751. * the pages by calling redirty_page_for_writepage() but that
  2752. * would be ugly in the extreme. So instead we would need to
  2753. * replicate parts of the code in the above functions,
  2754. * simplifying them because we wouldn't actually intend to
  2755. * write out the pages, but rather only collect contiguous
  2756. * logical block extents, call the multi-block allocator, and
  2757. * then update the buffer heads with the block allocations.
  2758. *
  2759. * For now, though, we'll cheat by calling filemap_flush(),
  2760. * which will map the blocks, and start the I/O, but not
  2761. * actually wait for the I/O to complete.
  2762. */
  2763. return filemap_flush(inode->i_mapping);
  2764. }
  2765. /*
  2766. * bmap() is special. It gets used by applications such as lilo and by
  2767. * the swapper to find the on-disk block of a specific piece of data.
  2768. *
  2769. * Naturally, this is dangerous if the block concerned is still in the
  2770. * journal. If somebody makes a swapfile on an ext4 data-journaling
  2771. * filesystem and enables swap, then they may get a nasty shock when the
  2772. * data getting swapped to that swapfile suddenly gets overwritten by
  2773. * the original zero's written out previously to the journal and
  2774. * awaiting writeback in the kernel's buffer cache.
  2775. *
  2776. * So, if we see any bmap calls here on a modified, data-journaled file,
  2777. * take extra steps to flush any blocks which might be in the cache.
  2778. */
  2779. static sector_t ext4_bmap(struct address_space *mapping, sector_t block)
  2780. {
  2781. struct inode *inode = mapping->host;
  2782. journal_t *journal;
  2783. int err;
  2784. /*
  2785. * We can get here for an inline file via the FIBMAP ioctl
  2786. */
  2787. if (ext4_has_inline_data(inode))
  2788. return 0;
  2789. if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) &&
  2790. test_opt(inode->i_sb, DELALLOC)) {
  2791. /*
  2792. * With delalloc we want to sync the file
  2793. * so that we can make sure we allocate
  2794. * blocks for file
  2795. */
  2796. filemap_write_and_wait(mapping);
  2797. }
  2798. if (EXT4_JOURNAL(inode) &&
  2799. ext4_test_inode_state(inode, EXT4_STATE_JDATA)) {
  2800. /*
  2801. * This is a REALLY heavyweight approach, but the use of
  2802. * bmap on dirty files is expected to be extremely rare:
  2803. * only if we run lilo or swapon on a freshly made file
  2804. * do we expect this to happen.
  2805. *
  2806. * (bmap requires CAP_SYS_RAWIO so this does not
  2807. * represent an unprivileged user DOS attack --- we'd be
  2808. * in trouble if mortal users could trigger this path at
  2809. * will.)
  2810. *
  2811. * NB. EXT4_STATE_JDATA is not set on files other than
  2812. * regular files. If somebody wants to bmap a directory
  2813. * or symlink and gets confused because the buffer
  2814. * hasn't yet been flushed to disk, they deserve
  2815. * everything they get.
  2816. */
  2817. ext4_clear_inode_state(inode, EXT4_STATE_JDATA);
  2818. journal = EXT4_JOURNAL(inode);
  2819. jbd2_journal_lock_updates(journal);
  2820. err = jbd2_journal_flush(journal);
  2821. jbd2_journal_unlock_updates(journal);
  2822. if (err)
  2823. return 0;
  2824. }
  2825. return generic_block_bmap(mapping, block, ext4_get_block);
  2826. }
  2827. static int ext4_readpage(struct file *file, struct page *page)
  2828. {
  2829. int ret = -EAGAIN;
  2830. struct inode *inode = page->mapping->host;
  2831. trace_ext4_readpage(page);
  2832. if (ext4_has_inline_data(inode))
  2833. ret = ext4_readpage_inline(inode, page);
  2834. if (ret == -EAGAIN)
  2835. return ext4_mpage_readpages(page->mapping, NULL, page, 1);
  2836. return ret;
  2837. }
  2838. static int
  2839. ext4_readpages(struct file *file, struct address_space *mapping,
  2840. struct list_head *pages, unsigned nr_pages)
  2841. {
  2842. struct inode *inode = mapping->host;
  2843. /* If the file has inline data, no need to do readpages. */
  2844. if (ext4_has_inline_data(inode))
  2845. return 0;
  2846. return ext4_mpage_readpages(mapping, pages, NULL, nr_pages);
  2847. }
  2848. static void ext4_invalidatepage(struct page *page, unsigned int offset,
  2849. unsigned int length)
  2850. {
  2851. trace_ext4_invalidatepage(page, offset, length);
  2852. /* No journalling happens on data buffers when this function is used */
  2853. WARN_ON(page_has_buffers(page) && buffer_jbd(page_buffers(page)));
  2854. block_invalidatepage(page, offset, length);
  2855. }
  2856. static int __ext4_journalled_invalidatepage(struct page *page,
  2857. unsigned int offset,
  2858. unsigned int length)
  2859. {
  2860. journal_t *journal = EXT4_JOURNAL(page->mapping->host);
  2861. trace_ext4_journalled_invalidatepage(page, offset, length);
  2862. /*
  2863. * If it's a full truncate we just forget about the pending dirtying
  2864. */
  2865. if (offset == 0 && length == PAGE_SIZE)
  2866. ClearPageChecked(page);
  2867. return jbd2_journal_invalidatepage(journal, page, offset, length);
  2868. }
  2869. /* Wrapper for aops... */
  2870. static void ext4_journalled_invalidatepage(struct page *page,
  2871. unsigned int offset,
  2872. unsigned int length)
  2873. {
  2874. WARN_ON(__ext4_journalled_invalidatepage(page, offset, length) < 0);
  2875. }
  2876. static int ext4_releasepage(struct page *page, gfp_t wait)
  2877. {
  2878. journal_t *journal = EXT4_JOURNAL(page->mapping->host);
  2879. trace_ext4_releasepage(page);
  2880. /* Page has dirty journalled data -> cannot release */
  2881. if (PageChecked(page))
  2882. return 0;
  2883. if (journal)
  2884. return jbd2_journal_try_to_free_buffers(journal, page, wait);
  2885. else
  2886. return try_to_free_buffers(page);
  2887. }
  2888. #ifdef CONFIG_FS_DAX
  2889. /*
  2890. * Get block function for DAX IO and mmap faults. It takes care of converting
  2891. * unwritten extents to written ones and initializes new / converted blocks
  2892. * to zeros.
  2893. */
  2894. int ext4_dax_get_block(struct inode *inode, sector_t iblock,
  2895. struct buffer_head *bh_result, int create)
  2896. {
  2897. int ret;
  2898. ext4_debug("inode %lu, create flag %d\n", inode->i_ino, create);
  2899. if (!create)
  2900. return _ext4_get_block(inode, iblock, bh_result, 0);
  2901. ret = ext4_get_block_trans(inode, iblock, bh_result,
  2902. EXT4_GET_BLOCKS_PRE_IO |
  2903. EXT4_GET_BLOCKS_CREATE_ZERO);
  2904. if (ret < 0)
  2905. return ret;
  2906. if (buffer_unwritten(bh_result)) {
  2907. /*
  2908. * We are protected by i_mmap_sem or i_mutex so we know block
  2909. * cannot go away from under us even though we dropped
  2910. * i_data_sem. Convert extent to written and write zeros there.
  2911. */
  2912. ret = ext4_get_block_trans(inode, iblock, bh_result,
  2913. EXT4_GET_BLOCKS_CONVERT |
  2914. EXT4_GET_BLOCKS_CREATE_ZERO);
  2915. if (ret < 0)
  2916. return ret;
  2917. }
  2918. /*
  2919. * At least for now we have to clear BH_New so that DAX code
  2920. * doesn't attempt to zero blocks again in a racy way.
  2921. */
  2922. clear_buffer_new(bh_result);
  2923. return 0;
  2924. }
  2925. #else
  2926. /* Just define empty function, it will never get called. */
  2927. int ext4_dax_get_block(struct inode *inode, sector_t iblock,
  2928. struct buffer_head *bh_result, int create)
  2929. {
  2930. BUG();
  2931. return 0;
  2932. }
  2933. #endif
  2934. static int ext4_end_io_dio(struct kiocb *iocb, loff_t offset,
  2935. ssize_t size, void *private)
  2936. {
  2937. ext4_io_end_t *io_end = private;
  2938. /* if not async direct IO just return */
  2939. if (!io_end)
  2940. return 0;
  2941. ext_debug("ext4_end_io_dio(): io_end 0x%p "
  2942. "for inode %lu, iocb 0x%p, offset %llu, size %zd\n",
  2943. io_end, io_end->inode->i_ino, iocb, offset, size);
  2944. /*
  2945. * Error during AIO DIO. We cannot convert unwritten extents as the
  2946. * data was not written. Just clear the unwritten flag and drop io_end.
  2947. */
  2948. if (size <= 0) {
  2949. ext4_clear_io_unwritten_flag(io_end);
  2950. size = 0;
  2951. }
  2952. io_end->offset = offset;
  2953. io_end->size = size;
  2954. ext4_put_io_end(io_end);
  2955. return 0;
  2956. }
  2957. /*
  2958. * Handling of direct IO writes.
  2959. *
  2960. * For ext4 extent files, ext4 will do direct-io write even to holes,
  2961. * preallocated extents, and those write extend the file, no need to
  2962. * fall back to buffered IO.
  2963. *
  2964. * For holes, we fallocate those blocks, mark them as unwritten
  2965. * If those blocks were preallocated, we mark sure they are split, but
  2966. * still keep the range to write as unwritten.
  2967. *
  2968. * The unwritten extents will be converted to written when DIO is completed.
  2969. * For async direct IO, since the IO may still pending when return, we
  2970. * set up an end_io call back function, which will do the conversion
  2971. * when async direct IO completed.
  2972. *
  2973. * If the O_DIRECT write will extend the file then add this inode to the
  2974. * orphan list. So recovery will truncate it back to the original size
  2975. * if the machine crashes during the write.
  2976. *
  2977. */
  2978. static ssize_t ext4_direct_IO_write(struct kiocb *iocb, struct iov_iter *iter)
  2979. {
  2980. struct file *file = iocb->ki_filp;
  2981. struct inode *inode = file->f_mapping->host;
  2982. struct ext4_inode_info *ei = EXT4_I(inode);
  2983. ssize_t ret;
  2984. loff_t offset = iocb->ki_pos;
  2985. size_t count = iov_iter_count(iter);
  2986. int overwrite = 0;
  2987. get_block_t *get_block_func = NULL;
  2988. int dio_flags = 0;
  2989. loff_t final_size = offset + count;
  2990. int orphan = 0;
  2991. handle_t *handle;
  2992. if (final_size > inode->i_size) {
  2993. /* Credits for sb + inode write */
  2994. handle = ext4_journal_start(inode, EXT4_HT_INODE, 2);
  2995. if (IS_ERR(handle)) {
  2996. ret = PTR_ERR(handle);
  2997. goto out;
  2998. }
  2999. ret = ext4_orphan_add(handle, inode);
  3000. if (ret) {
  3001. ext4_journal_stop(handle);
  3002. goto out;
  3003. }
  3004. orphan = 1;
  3005. ei->i_disksize = inode->i_size;
  3006. ext4_journal_stop(handle);
  3007. }
  3008. BUG_ON(iocb->private == NULL);
  3009. /*
  3010. * Make all waiters for direct IO properly wait also for extent
  3011. * conversion. This also disallows race between truncate() and
  3012. * overwrite DIO as i_dio_count needs to be incremented under i_mutex.
  3013. */
  3014. inode_dio_begin(inode);
  3015. /* If we do a overwrite dio, i_mutex locking can be released */
  3016. overwrite = *((int *)iocb->private);
  3017. if (overwrite)
  3018. inode_unlock(inode);
  3019. /*
  3020. * For extent mapped files we could direct write to holes and fallocate.
  3021. *
  3022. * Allocated blocks to fill the hole are marked as unwritten to prevent
  3023. * parallel buffered read to expose the stale data before DIO complete
  3024. * the data IO.
  3025. *
  3026. * As to previously fallocated extents, ext4 get_block will just simply
  3027. * mark the buffer mapped but still keep the extents unwritten.
  3028. *
  3029. * For non AIO case, we will convert those unwritten extents to written
  3030. * after return back from blockdev_direct_IO. That way we save us from
  3031. * allocating io_end structure and also the overhead of offloading
  3032. * the extent convertion to a workqueue.
  3033. *
  3034. * For async DIO, the conversion needs to be deferred when the
  3035. * IO is completed. The ext4 end_io callback function will be
  3036. * called to take care of the conversion work. Here for async
  3037. * case, we allocate an io_end structure to hook to the iocb.
  3038. */
  3039. iocb->private = NULL;
  3040. if (overwrite)
  3041. get_block_func = ext4_dio_get_block_overwrite;
  3042. else if (IS_DAX(inode)) {
  3043. /*
  3044. * We can avoid zeroing for aligned DAX writes beyond EOF. Other
  3045. * writes need zeroing either because they can race with page
  3046. * faults or because they use partial blocks.
  3047. */
  3048. if (round_down(offset, 1<<inode->i_blkbits) >= inode->i_size &&
  3049. ext4_aligned_io(inode, offset, count))
  3050. get_block_func = ext4_dio_get_block;
  3051. else
  3052. get_block_func = ext4_dax_get_block;
  3053. dio_flags = DIO_LOCKING;
  3054. } else if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS) ||
  3055. round_down(offset, 1 << inode->i_blkbits) >= inode->i_size) {
  3056. get_block_func = ext4_dio_get_block;
  3057. dio_flags = DIO_LOCKING | DIO_SKIP_HOLES;
  3058. } else if (is_sync_kiocb(iocb)) {
  3059. get_block_func = ext4_dio_get_block_unwritten_sync;
  3060. dio_flags = DIO_LOCKING;
  3061. } else {
  3062. get_block_func = ext4_dio_get_block_unwritten_async;
  3063. dio_flags = DIO_LOCKING;
  3064. }
  3065. #ifdef CONFIG_EXT4_FS_ENCRYPTION
  3066. BUG_ON(ext4_encrypted_inode(inode) && S_ISREG(inode->i_mode));
  3067. #endif
  3068. if (IS_DAX(inode)) {
  3069. ret = dax_do_io(iocb, inode, iter, get_block_func,
  3070. ext4_end_io_dio, dio_flags);
  3071. } else
  3072. ret = __blockdev_direct_IO(iocb, inode,
  3073. inode->i_sb->s_bdev, iter,
  3074. get_block_func,
  3075. ext4_end_io_dio, NULL, dio_flags);
  3076. if (ret > 0 && !overwrite && ext4_test_inode_state(inode,
  3077. EXT4_STATE_DIO_UNWRITTEN)) {
  3078. int err;
  3079. /*
  3080. * for non AIO case, since the IO is already
  3081. * completed, we could do the conversion right here
  3082. */
  3083. err = ext4_convert_unwritten_extents(NULL, inode,
  3084. offset, ret);
  3085. if (err < 0)
  3086. ret = err;
  3087. ext4_clear_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN);
  3088. }
  3089. inode_dio_end(inode);
  3090. /* take i_mutex locking again if we do a ovewrite dio */
  3091. if (overwrite)
  3092. inode_lock(inode);
  3093. if (ret < 0 && final_size > inode->i_size)
  3094. ext4_truncate_failed_write(inode);
  3095. /* Handle extending of i_size after direct IO write */
  3096. if (orphan) {
  3097. int err;
  3098. /* Credits for sb + inode write */
  3099. handle = ext4_journal_start(inode, EXT4_HT_INODE, 2);
  3100. if (IS_ERR(handle)) {
  3101. /* This is really bad luck. We've written the data
  3102. * but cannot extend i_size. Bail out and pretend
  3103. * the write failed... */
  3104. ret = PTR_ERR(handle);
  3105. if (inode->i_nlink)
  3106. ext4_orphan_del(NULL, inode);
  3107. goto out;
  3108. }
  3109. if (inode->i_nlink)
  3110. ext4_orphan_del(handle, inode);
  3111. if (ret > 0) {
  3112. loff_t end = offset + ret;
  3113. if (end > inode->i_size) {
  3114. ei->i_disksize = end;
  3115. i_size_write(inode, end);
  3116. /*
  3117. * We're going to return a positive `ret'
  3118. * here due to non-zero-length I/O, so there's
  3119. * no way of reporting error returns from
  3120. * ext4_mark_inode_dirty() to userspace. So
  3121. * ignore it.
  3122. */
  3123. ext4_mark_inode_dirty(handle, inode);
  3124. }
  3125. }
  3126. err = ext4_journal_stop(handle);
  3127. if (ret == 0)
  3128. ret = err;
  3129. }
  3130. out:
  3131. return ret;
  3132. }
  3133. static ssize_t ext4_direct_IO_read(struct kiocb *iocb, struct iov_iter *iter)
  3134. {
  3135. int unlocked = 0;
  3136. struct inode *inode = iocb->ki_filp->f_mapping->host;
  3137. ssize_t ret;
  3138. if (ext4_should_dioread_nolock(inode)) {
  3139. /*
  3140. * Nolock dioread optimization may be dynamically disabled
  3141. * via ext4_inode_block_unlocked_dio(). Check inode's state
  3142. * while holding extra i_dio_count ref.
  3143. */
  3144. inode_dio_begin(inode);
  3145. smp_mb();
  3146. if (unlikely(ext4_test_inode_state(inode,
  3147. EXT4_STATE_DIOREAD_LOCK)))
  3148. inode_dio_end(inode);
  3149. else
  3150. unlocked = 1;
  3151. }
  3152. if (IS_DAX(inode)) {
  3153. ret = dax_do_io(iocb, inode, iter, ext4_dio_get_block,
  3154. NULL, unlocked ? 0 : DIO_LOCKING);
  3155. } else {
  3156. ret = __blockdev_direct_IO(iocb, inode, inode->i_sb->s_bdev,
  3157. iter, ext4_dio_get_block,
  3158. NULL, NULL,
  3159. unlocked ? 0 : DIO_LOCKING);
  3160. }
  3161. if (unlocked)
  3162. inode_dio_end(inode);
  3163. return ret;
  3164. }
  3165. static ssize_t ext4_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
  3166. {
  3167. struct file *file = iocb->ki_filp;
  3168. struct inode *inode = file->f_mapping->host;
  3169. size_t count = iov_iter_count(iter);
  3170. loff_t offset = iocb->ki_pos;
  3171. ssize_t ret;
  3172. #ifdef CONFIG_EXT4_FS_ENCRYPTION
  3173. if (ext4_encrypted_inode(inode) && S_ISREG(inode->i_mode))
  3174. return 0;
  3175. #endif
  3176. /*
  3177. * If we are doing data journalling we don't support O_DIRECT
  3178. */
  3179. if (ext4_should_journal_data(inode))
  3180. return 0;
  3181. /* Let buffer I/O handle the inline data case. */
  3182. if (ext4_has_inline_data(inode))
  3183. return 0;
  3184. trace_ext4_direct_IO_enter(inode, offset, count, iov_iter_rw(iter));
  3185. if (iov_iter_rw(iter) == READ)
  3186. ret = ext4_direct_IO_read(iocb, iter);
  3187. else
  3188. ret = ext4_direct_IO_write(iocb, iter);
  3189. trace_ext4_direct_IO_exit(inode, offset, count, iov_iter_rw(iter), ret);
  3190. return ret;
  3191. }
  3192. /*
  3193. * Pages can be marked dirty completely asynchronously from ext4's journalling
  3194. * activity. By filemap_sync_pte(), try_to_unmap_one(), etc. We cannot do
  3195. * much here because ->set_page_dirty is called under VFS locks. The page is
  3196. * not necessarily locked.
  3197. *
  3198. * We cannot just dirty the page and leave attached buffers clean, because the
  3199. * buffers' dirty state is "definitive". We cannot just set the buffers dirty
  3200. * or jbddirty because all the journalling code will explode.
  3201. *
  3202. * So what we do is to mark the page "pending dirty" and next time writepage
  3203. * is called, propagate that into the buffers appropriately.
  3204. */
  3205. static int ext4_journalled_set_page_dirty(struct page *page)
  3206. {
  3207. SetPageChecked(page);
  3208. return __set_page_dirty_nobuffers(page);
  3209. }
  3210. static const struct address_space_operations ext4_aops = {
  3211. .readpage = ext4_readpage,
  3212. .readpages = ext4_readpages,
  3213. .writepage = ext4_writepage,
  3214. .writepages = ext4_writepages,
  3215. .write_begin = ext4_write_begin,
  3216. .write_end = ext4_write_end,
  3217. .bmap = ext4_bmap,
  3218. .invalidatepage = ext4_invalidatepage,
  3219. .releasepage = ext4_releasepage,
  3220. .direct_IO = ext4_direct_IO,
  3221. .migratepage = buffer_migrate_page,
  3222. .is_partially_uptodate = block_is_partially_uptodate,
  3223. .error_remove_page = generic_error_remove_page,
  3224. };
  3225. static const struct address_space_operations ext4_journalled_aops = {
  3226. .readpage = ext4_readpage,
  3227. .readpages = ext4_readpages,
  3228. .writepage = ext4_writepage,
  3229. .writepages = ext4_writepages,
  3230. .write_begin = ext4_write_begin,
  3231. .write_end = ext4_journalled_write_end,
  3232. .set_page_dirty = ext4_journalled_set_page_dirty,
  3233. .bmap = ext4_bmap,
  3234. .invalidatepage = ext4_journalled_invalidatepage,
  3235. .releasepage = ext4_releasepage,
  3236. .direct_IO = ext4_direct_IO,
  3237. .is_partially_uptodate = block_is_partially_uptodate,
  3238. .error_remove_page = generic_error_remove_page,
  3239. };
  3240. static const struct address_space_operations ext4_da_aops = {
  3241. .readpage = ext4_readpage,
  3242. .readpages = ext4_readpages,
  3243. .writepage = ext4_writepage,
  3244. .writepages = ext4_writepages,
  3245. .write_begin = ext4_da_write_begin,
  3246. .write_end = ext4_da_write_end,
  3247. .bmap = ext4_bmap,
  3248. .invalidatepage = ext4_da_invalidatepage,
  3249. .releasepage = ext4_releasepage,
  3250. .direct_IO = ext4_direct_IO,
  3251. .migratepage = buffer_migrate_page,
  3252. .is_partially_uptodate = block_is_partially_uptodate,
  3253. .error_remove_page = generic_error_remove_page,
  3254. };
  3255. void ext4_set_aops(struct inode *inode)
  3256. {
  3257. switch (ext4_inode_journal_mode(inode)) {
  3258. case EXT4_INODE_ORDERED_DATA_MODE:
  3259. case EXT4_INODE_WRITEBACK_DATA_MODE:
  3260. break;
  3261. case EXT4_INODE_JOURNAL_DATA_MODE:
  3262. inode->i_mapping->a_ops = &ext4_journalled_aops;
  3263. return;
  3264. default:
  3265. BUG();
  3266. }
  3267. if (test_opt(inode->i_sb, DELALLOC))
  3268. inode->i_mapping->a_ops = &ext4_da_aops;
  3269. else
  3270. inode->i_mapping->a_ops = &ext4_aops;
  3271. }
  3272. static int __ext4_block_zero_page_range(handle_t *handle,
  3273. struct address_space *mapping, loff_t from, loff_t length)
  3274. {
  3275. ext4_fsblk_t index = from >> PAGE_SHIFT;
  3276. unsigned offset = from & (PAGE_SIZE-1);
  3277. unsigned blocksize, pos;
  3278. ext4_lblk_t iblock;
  3279. struct inode *inode = mapping->host;
  3280. struct buffer_head *bh;
  3281. struct page *page;
  3282. int err = 0;
  3283. page = find_or_create_page(mapping, from >> PAGE_SHIFT,
  3284. mapping_gfp_constraint(mapping, ~__GFP_FS));
  3285. if (!page)
  3286. return -ENOMEM;
  3287. blocksize = inode->i_sb->s_blocksize;
  3288. iblock = index << (PAGE_SHIFT - inode->i_sb->s_blocksize_bits);
  3289. if (!page_has_buffers(page))
  3290. create_empty_buffers(page, blocksize, 0);
  3291. /* Find the buffer that contains "offset" */
  3292. bh = page_buffers(page);
  3293. pos = blocksize;
  3294. while (offset >= pos) {
  3295. bh = bh->b_this_page;
  3296. iblock++;
  3297. pos += blocksize;
  3298. }
  3299. if (buffer_freed(bh)) {
  3300. BUFFER_TRACE(bh, "freed: skip");
  3301. goto unlock;
  3302. }
  3303. if (!buffer_mapped(bh)) {
  3304. BUFFER_TRACE(bh, "unmapped");
  3305. ext4_get_block(inode, iblock, bh, 0);
  3306. /* unmapped? It's a hole - nothing to do */
  3307. if (!buffer_mapped(bh)) {
  3308. BUFFER_TRACE(bh, "still unmapped");
  3309. goto unlock;
  3310. }
  3311. }
  3312. /* Ok, it's mapped. Make sure it's up-to-date */
  3313. if (PageUptodate(page))
  3314. set_buffer_uptodate(bh);
  3315. if (!buffer_uptodate(bh)) {
  3316. err = -EIO;
  3317. ll_rw_block(READ, 1, &bh);
  3318. wait_on_buffer(bh);
  3319. /* Uhhuh. Read error. Complain and punt. */
  3320. if (!buffer_uptodate(bh))
  3321. goto unlock;
  3322. if (S_ISREG(inode->i_mode) &&
  3323. ext4_encrypted_inode(inode)) {
  3324. /* We expect the key to be set. */
  3325. BUG_ON(!ext4_has_encryption_key(inode));
  3326. BUG_ON(blocksize != PAGE_SIZE);
  3327. WARN_ON_ONCE(ext4_decrypt(page));
  3328. }
  3329. }
  3330. if (ext4_should_journal_data(inode)) {
  3331. BUFFER_TRACE(bh, "get write access");
  3332. err = ext4_journal_get_write_access(handle, bh);
  3333. if (err)
  3334. goto unlock;
  3335. }
  3336. zero_user(page, offset, length);
  3337. BUFFER_TRACE(bh, "zeroed end of block");
  3338. if (ext4_should_journal_data(inode)) {
  3339. err = ext4_handle_dirty_metadata(handle, inode, bh);
  3340. } else {
  3341. err = 0;
  3342. mark_buffer_dirty(bh);
  3343. if (ext4_should_order_data(inode))
  3344. err = ext4_jbd2_inode_add_write(handle, inode);
  3345. }
  3346. unlock:
  3347. unlock_page(page);
  3348. put_page(page);
  3349. return err;
  3350. }
  3351. /*
  3352. * ext4_block_zero_page_range() zeros out a mapping of length 'length'
  3353. * starting from file offset 'from'. The range to be zero'd must
  3354. * be contained with in one block. If the specified range exceeds
  3355. * the end of the block it will be shortened to end of the block
  3356. * that cooresponds to 'from'
  3357. */
  3358. static int ext4_block_zero_page_range(handle_t *handle,
  3359. struct address_space *mapping, loff_t from, loff_t length)
  3360. {
  3361. struct inode *inode = mapping->host;
  3362. unsigned offset = from & (PAGE_SIZE-1);
  3363. unsigned blocksize = inode->i_sb->s_blocksize;
  3364. unsigned max = blocksize - (offset & (blocksize - 1));
  3365. /*
  3366. * correct length if it does not fall between
  3367. * 'from' and the end of the block
  3368. */
  3369. if (length > max || length < 0)
  3370. length = max;
  3371. if (IS_DAX(inode))
  3372. return dax_zero_page_range(inode, from, length, ext4_get_block);
  3373. return __ext4_block_zero_page_range(handle, mapping, from, length);
  3374. }
  3375. /*
  3376. * ext4_block_truncate_page() zeroes out a mapping from file offset `from'
  3377. * up to the end of the block which corresponds to `from'.
  3378. * This required during truncate. We need to physically zero the tail end
  3379. * of that block so it doesn't yield old data if the file is later grown.
  3380. */
  3381. static int ext4_block_truncate_page(handle_t *handle,
  3382. struct address_space *mapping, loff_t from)
  3383. {
  3384. unsigned offset = from & (PAGE_SIZE-1);
  3385. unsigned length;
  3386. unsigned blocksize;
  3387. struct inode *inode = mapping->host;
  3388. blocksize = inode->i_sb->s_blocksize;
  3389. length = blocksize - (offset & (blocksize - 1));
  3390. return ext4_block_zero_page_range(handle, mapping, from, length);
  3391. }
  3392. int ext4_zero_partial_blocks(handle_t *handle, struct inode *inode,
  3393. loff_t lstart, loff_t length)
  3394. {
  3395. struct super_block *sb = inode->i_sb;
  3396. struct address_space *mapping = inode->i_mapping;
  3397. unsigned partial_start, partial_end;
  3398. ext4_fsblk_t start, end;
  3399. loff_t byte_end = (lstart + length - 1);
  3400. int err = 0;
  3401. partial_start = lstart & (sb->s_blocksize - 1);
  3402. partial_end = byte_end & (sb->s_blocksize - 1);
  3403. start = lstart >> sb->s_blocksize_bits;
  3404. end = byte_end >> sb->s_blocksize_bits;
  3405. /* Handle partial zero within the single block */
  3406. if (start == end &&
  3407. (partial_start || (partial_end != sb->s_blocksize - 1))) {
  3408. err = ext4_block_zero_page_range(handle, mapping,
  3409. lstart, length);
  3410. return err;
  3411. }
  3412. /* Handle partial zero out on the start of the range */
  3413. if (partial_start) {
  3414. err = ext4_block_zero_page_range(handle, mapping,
  3415. lstart, sb->s_blocksize);
  3416. if (err)
  3417. return err;
  3418. }
  3419. /* Handle partial zero out on the end of the range */
  3420. if (partial_end != sb->s_blocksize - 1)
  3421. err = ext4_block_zero_page_range(handle, mapping,
  3422. byte_end - partial_end,
  3423. partial_end + 1);
  3424. return err;
  3425. }
  3426. int ext4_can_truncate(struct inode *inode)
  3427. {
  3428. if (S_ISREG(inode->i_mode))
  3429. return 1;
  3430. if (S_ISDIR(inode->i_mode))
  3431. return 1;
  3432. if (S_ISLNK(inode->i_mode))
  3433. return !ext4_inode_is_fast_symlink(inode);
  3434. return 0;
  3435. }
  3436. /*
  3437. * We have to make sure i_disksize gets properly updated before we truncate
  3438. * page cache due to hole punching or zero range. Otherwise i_disksize update
  3439. * can get lost as it may have been postponed to submission of writeback but
  3440. * that will never happen after we truncate page cache.
  3441. */
  3442. int ext4_update_disksize_before_punch(struct inode *inode, loff_t offset,
  3443. loff_t len)
  3444. {
  3445. handle_t *handle;
  3446. loff_t size = i_size_read(inode);
  3447. WARN_ON(!inode_is_locked(inode));
  3448. if (offset > size || offset + len < size)
  3449. return 0;
  3450. if (EXT4_I(inode)->i_disksize >= size)
  3451. return 0;
  3452. handle = ext4_journal_start(inode, EXT4_HT_MISC, 1);
  3453. if (IS_ERR(handle))
  3454. return PTR_ERR(handle);
  3455. ext4_update_i_disksize(inode, size);
  3456. ext4_mark_inode_dirty(handle, inode);
  3457. ext4_journal_stop(handle);
  3458. return 0;
  3459. }
  3460. /*
  3461. * ext4_punch_hole: punches a hole in a file by releaseing the blocks
  3462. * associated with the given offset and length
  3463. *
  3464. * @inode: File inode
  3465. * @offset: The offset where the hole will begin
  3466. * @len: The length of the hole
  3467. *
  3468. * Returns: 0 on success or negative on failure
  3469. */
  3470. int ext4_punch_hole(struct inode *inode, loff_t offset, loff_t length)
  3471. {
  3472. struct super_block *sb = inode->i_sb;
  3473. ext4_lblk_t first_block, stop_block;
  3474. struct address_space *mapping = inode->i_mapping;
  3475. loff_t first_block_offset, last_block_offset;
  3476. handle_t *handle;
  3477. unsigned int credits;
  3478. int ret = 0;
  3479. if (!S_ISREG(inode->i_mode))
  3480. return -EOPNOTSUPP;
  3481. trace_ext4_punch_hole(inode, offset, length, 0);
  3482. /*
  3483. * Write out all dirty pages to avoid race conditions
  3484. * Then release them.
  3485. */
  3486. if (mapping->nrpages && mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) {
  3487. ret = filemap_write_and_wait_range(mapping, offset,
  3488. offset + length - 1);
  3489. if (ret)
  3490. return ret;
  3491. }
  3492. inode_lock(inode);
  3493. /* No need to punch hole beyond i_size */
  3494. if (offset >= inode->i_size)
  3495. goto out_mutex;
  3496. /*
  3497. * If the hole extends beyond i_size, set the hole
  3498. * to end after the page that contains i_size
  3499. */
  3500. if (offset + length > inode->i_size) {
  3501. length = inode->i_size +
  3502. PAGE_SIZE - (inode->i_size & (PAGE_SIZE - 1)) -
  3503. offset;
  3504. }
  3505. if (offset & (sb->s_blocksize - 1) ||
  3506. (offset + length) & (sb->s_blocksize - 1)) {
  3507. /*
  3508. * Attach jinode to inode for jbd2 if we do any zeroing of
  3509. * partial block
  3510. */
  3511. ret = ext4_inode_attach_jinode(inode);
  3512. if (ret < 0)
  3513. goto out_mutex;
  3514. }
  3515. /* Wait all existing dio workers, newcomers will block on i_mutex */
  3516. ext4_inode_block_unlocked_dio(inode);
  3517. inode_dio_wait(inode);
  3518. /*
  3519. * Prevent page faults from reinstantiating pages we have released from
  3520. * page cache.
  3521. */
  3522. down_write(&EXT4_I(inode)->i_mmap_sem);
  3523. first_block_offset = round_up(offset, sb->s_blocksize);
  3524. last_block_offset = round_down((offset + length), sb->s_blocksize) - 1;
  3525. /* Now release the pages and zero block aligned part of pages*/
  3526. if (last_block_offset > first_block_offset) {
  3527. ret = ext4_update_disksize_before_punch(inode, offset, length);
  3528. if (ret)
  3529. goto out_dio;
  3530. truncate_pagecache_range(inode, first_block_offset,
  3531. last_block_offset);
  3532. }
  3533. if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
  3534. credits = ext4_writepage_trans_blocks(inode);
  3535. else
  3536. credits = ext4_blocks_for_truncate(inode);
  3537. handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits);
  3538. if (IS_ERR(handle)) {
  3539. ret = PTR_ERR(handle);
  3540. ext4_std_error(sb, ret);
  3541. goto out_dio;
  3542. }
  3543. ret = ext4_zero_partial_blocks(handle, inode, offset,
  3544. length);
  3545. if (ret)
  3546. goto out_stop;
  3547. first_block = (offset + sb->s_blocksize - 1) >>
  3548. EXT4_BLOCK_SIZE_BITS(sb);
  3549. stop_block = (offset + length) >> EXT4_BLOCK_SIZE_BITS(sb);
  3550. /* If there are no blocks to remove, return now */
  3551. if (first_block >= stop_block)
  3552. goto out_stop;
  3553. down_write(&EXT4_I(inode)->i_data_sem);
  3554. ext4_discard_preallocations(inode);
  3555. ret = ext4_es_remove_extent(inode, first_block,
  3556. stop_block - first_block);
  3557. if (ret) {
  3558. up_write(&EXT4_I(inode)->i_data_sem);
  3559. goto out_stop;
  3560. }
  3561. if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
  3562. ret = ext4_ext_remove_space(inode, first_block,
  3563. stop_block - 1);
  3564. else
  3565. ret = ext4_ind_remove_space(handle, inode, first_block,
  3566. stop_block);
  3567. up_write(&EXT4_I(inode)->i_data_sem);
  3568. if (IS_SYNC(inode))
  3569. ext4_handle_sync(handle);
  3570. inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
  3571. ext4_mark_inode_dirty(handle, inode);
  3572. out_stop:
  3573. ext4_journal_stop(handle);
  3574. out_dio:
  3575. up_write(&EXT4_I(inode)->i_mmap_sem);
  3576. ext4_inode_resume_unlocked_dio(inode);
  3577. out_mutex:
  3578. inode_unlock(inode);
  3579. return ret;
  3580. }
  3581. int ext4_inode_attach_jinode(struct inode *inode)
  3582. {
  3583. struct ext4_inode_info *ei = EXT4_I(inode);
  3584. struct jbd2_inode *jinode;
  3585. if (ei->jinode || !EXT4_SB(inode->i_sb)->s_journal)
  3586. return 0;
  3587. jinode = jbd2_alloc_inode(GFP_KERNEL);
  3588. spin_lock(&inode->i_lock);
  3589. if (!ei->jinode) {
  3590. if (!jinode) {
  3591. spin_unlock(&inode->i_lock);
  3592. return -ENOMEM;
  3593. }
  3594. ei->jinode = jinode;
  3595. jbd2_journal_init_jbd_inode(ei->jinode, inode);
  3596. jinode = NULL;
  3597. }
  3598. spin_unlock(&inode->i_lock);
  3599. if (unlikely(jinode != NULL))
  3600. jbd2_free_inode(jinode);
  3601. return 0;
  3602. }
  3603. /*
  3604. * ext4_truncate()
  3605. *
  3606. * We block out ext4_get_block() block instantiations across the entire
  3607. * transaction, and VFS/VM ensures that ext4_truncate() cannot run
  3608. * simultaneously on behalf of the same inode.
  3609. *
  3610. * As we work through the truncate and commit bits of it to the journal there
  3611. * is one core, guiding principle: the file's tree must always be consistent on
  3612. * disk. We must be able to restart the truncate after a crash.
  3613. *
  3614. * The file's tree may be transiently inconsistent in memory (although it
  3615. * probably isn't), but whenever we close off and commit a journal transaction,
  3616. * the contents of (the filesystem + the journal) must be consistent and
  3617. * restartable. It's pretty simple, really: bottom up, right to left (although
  3618. * left-to-right works OK too).
  3619. *
  3620. * Note that at recovery time, journal replay occurs *before* the restart of
  3621. * truncate against the orphan inode list.
  3622. *
  3623. * The committed inode has the new, desired i_size (which is the same as
  3624. * i_disksize in this case). After a crash, ext4_orphan_cleanup() will see
  3625. * that this inode's truncate did not complete and it will again call
  3626. * ext4_truncate() to have another go. So there will be instantiated blocks
  3627. * to the right of the truncation point in a crashed ext4 filesystem. But
  3628. * that's fine - as long as they are linked from the inode, the post-crash
  3629. * ext4_truncate() run will find them and release them.
  3630. */
  3631. void ext4_truncate(struct inode *inode)
  3632. {
  3633. struct ext4_inode_info *ei = EXT4_I(inode);
  3634. unsigned int credits;
  3635. handle_t *handle;
  3636. struct address_space *mapping = inode->i_mapping;
  3637. /*
  3638. * There is a possibility that we're either freeing the inode
  3639. * or it's a completely new inode. In those cases we might not
  3640. * have i_mutex locked because it's not necessary.
  3641. */
  3642. if (!(inode->i_state & (I_NEW|I_FREEING)))
  3643. WARN_ON(!inode_is_locked(inode));
  3644. trace_ext4_truncate_enter(inode);
  3645. if (!ext4_can_truncate(inode))
  3646. return;
  3647. ext4_clear_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
  3648. if (inode->i_size == 0 && !test_opt(inode->i_sb, NO_AUTO_DA_ALLOC))
  3649. ext4_set_inode_state(inode, EXT4_STATE_DA_ALLOC_CLOSE);
  3650. if (ext4_has_inline_data(inode)) {
  3651. int has_inline = 1;
  3652. ext4_inline_data_truncate(inode, &has_inline);
  3653. if (has_inline)
  3654. return;
  3655. }
  3656. /* If we zero-out tail of the page, we have to create jinode for jbd2 */
  3657. if (inode->i_size & (inode->i_sb->s_blocksize - 1)) {
  3658. if (ext4_inode_attach_jinode(inode) < 0)
  3659. return;
  3660. }
  3661. if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
  3662. credits = ext4_writepage_trans_blocks(inode);
  3663. else
  3664. credits = ext4_blocks_for_truncate(inode);
  3665. handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits);
  3666. if (IS_ERR(handle)) {
  3667. ext4_std_error(inode->i_sb, PTR_ERR(handle));
  3668. return;
  3669. }
  3670. if (inode->i_size & (inode->i_sb->s_blocksize - 1))
  3671. ext4_block_truncate_page(handle, mapping, inode->i_size);
  3672. /*
  3673. * We add the inode to the orphan list, so that if this
  3674. * truncate spans multiple transactions, and we crash, we will
  3675. * resume the truncate when the filesystem recovers. It also
  3676. * marks the inode dirty, to catch the new size.
  3677. *
  3678. * Implication: the file must always be in a sane, consistent
  3679. * truncatable state while each transaction commits.
  3680. */
  3681. if (ext4_orphan_add(handle, inode))
  3682. goto out_stop;
  3683. down_write(&EXT4_I(inode)->i_data_sem);
  3684. ext4_discard_preallocations(inode);
  3685. if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
  3686. ext4_ext_truncate(handle, inode);
  3687. else
  3688. ext4_ind_truncate(handle, inode);
  3689. up_write(&ei->i_data_sem);
  3690. if (IS_SYNC(inode))
  3691. ext4_handle_sync(handle);
  3692. out_stop:
  3693. /*
  3694. * If this was a simple ftruncate() and the file will remain alive,
  3695. * then we need to clear up the orphan record which we created above.
  3696. * However, if this was a real unlink then we were called by
  3697. * ext4_evict_inode(), and we allow that function to clean up the
  3698. * orphan info for us.
  3699. */
  3700. if (inode->i_nlink)
  3701. ext4_orphan_del(handle, inode);
  3702. inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
  3703. ext4_mark_inode_dirty(handle, inode);
  3704. ext4_journal_stop(handle);
  3705. trace_ext4_truncate_exit(inode);
  3706. }
  3707. /*
  3708. * ext4_get_inode_loc returns with an extra refcount against the inode's
  3709. * underlying buffer_head on success. If 'in_mem' is true, we have all
  3710. * data in memory that is needed to recreate the on-disk version of this
  3711. * inode.
  3712. */
  3713. static int __ext4_get_inode_loc(struct inode *inode,
  3714. struct ext4_iloc *iloc, int in_mem)
  3715. {
  3716. struct ext4_group_desc *gdp;
  3717. struct buffer_head *bh;
  3718. struct super_block *sb = inode->i_sb;
  3719. ext4_fsblk_t block;
  3720. int inodes_per_block, inode_offset;
  3721. iloc->bh = NULL;
  3722. if (!ext4_valid_inum(sb, inode->i_ino))
  3723. return -EFSCORRUPTED;
  3724. iloc->block_group = (inode->i_ino - 1) / EXT4_INODES_PER_GROUP(sb);
  3725. gdp = ext4_get_group_desc(sb, iloc->block_group, NULL);
  3726. if (!gdp)
  3727. return -EIO;
  3728. /*
  3729. * Figure out the offset within the block group inode table
  3730. */
  3731. inodes_per_block = EXT4_SB(sb)->s_inodes_per_block;
  3732. inode_offset = ((inode->i_ino - 1) %
  3733. EXT4_INODES_PER_GROUP(sb));
  3734. block = ext4_inode_table(sb, gdp) + (inode_offset / inodes_per_block);
  3735. iloc->offset = (inode_offset % inodes_per_block) * EXT4_INODE_SIZE(sb);
  3736. bh = sb_getblk(sb, block);
  3737. if (unlikely(!bh))
  3738. return -ENOMEM;
  3739. if (!buffer_uptodate(bh)) {
  3740. lock_buffer(bh);
  3741. /*
  3742. * If the buffer has the write error flag, we have failed
  3743. * to write out another inode in the same block. In this
  3744. * case, we don't have to read the block because we may
  3745. * read the old inode data successfully.
  3746. */
  3747. if (buffer_write_io_error(bh) && !buffer_uptodate(bh))
  3748. set_buffer_uptodate(bh);
  3749. if (buffer_uptodate(bh)) {
  3750. /* someone brought it uptodate while we waited */
  3751. unlock_buffer(bh);
  3752. goto has_buffer;
  3753. }
  3754. /*
  3755. * If we have all information of the inode in memory and this
  3756. * is the only valid inode in the block, we need not read the
  3757. * block.
  3758. */
  3759. if (in_mem) {
  3760. struct buffer_head *bitmap_bh;
  3761. int i, start;
  3762. start = inode_offset & ~(inodes_per_block - 1);
  3763. /* Is the inode bitmap in cache? */
  3764. bitmap_bh = sb_getblk(sb, ext4_inode_bitmap(sb, gdp));
  3765. if (unlikely(!bitmap_bh))
  3766. goto make_io;
  3767. /*
  3768. * If the inode bitmap isn't in cache then the
  3769. * optimisation may end up performing two reads instead
  3770. * of one, so skip it.
  3771. */
  3772. if (!buffer_uptodate(bitmap_bh)) {
  3773. brelse(bitmap_bh);
  3774. goto make_io;
  3775. }
  3776. for (i = start; i < start + inodes_per_block; i++) {
  3777. if (i == inode_offset)
  3778. continue;
  3779. if (ext4_test_bit(i, bitmap_bh->b_data))
  3780. break;
  3781. }
  3782. brelse(bitmap_bh);
  3783. if (i == start + inodes_per_block) {
  3784. /* all other inodes are free, so skip I/O */
  3785. memset(bh->b_data, 0, bh->b_size);
  3786. set_buffer_uptodate(bh);
  3787. unlock_buffer(bh);
  3788. goto has_buffer;
  3789. }
  3790. }
  3791. make_io:
  3792. /*
  3793. * If we need to do any I/O, try to pre-readahead extra
  3794. * blocks from the inode table.
  3795. */
  3796. if (EXT4_SB(sb)->s_inode_readahead_blks) {
  3797. ext4_fsblk_t b, end, table;
  3798. unsigned num;
  3799. __u32 ra_blks = EXT4_SB(sb)->s_inode_readahead_blks;
  3800. table = ext4_inode_table(sb, gdp);
  3801. /* s_inode_readahead_blks is always a power of 2 */
  3802. b = block & ~((ext4_fsblk_t) ra_blks - 1);
  3803. if (table > b)
  3804. b = table;
  3805. end = b + ra_blks;
  3806. num = EXT4_INODES_PER_GROUP(sb);
  3807. if (ext4_has_group_desc_csum(sb))
  3808. num -= ext4_itable_unused_count(sb, gdp);
  3809. table += num / inodes_per_block;
  3810. if (end > table)
  3811. end = table;
  3812. while (b <= end)
  3813. sb_breadahead(sb, b++);
  3814. }
  3815. /*
  3816. * There are other valid inodes in the buffer, this inode
  3817. * has in-inode xattrs, or we don't have this inode in memory.
  3818. * Read the block from disk.
  3819. */
  3820. trace_ext4_load_inode(inode);
  3821. get_bh(bh);
  3822. bh->b_end_io = end_buffer_read_sync;
  3823. submit_bh(READ | REQ_META | REQ_PRIO, bh);
  3824. wait_on_buffer(bh);
  3825. if (!buffer_uptodate(bh)) {
  3826. EXT4_ERROR_INODE_BLOCK(inode, block,
  3827. "unable to read itable block");
  3828. brelse(bh);
  3829. return -EIO;
  3830. }
  3831. }
  3832. has_buffer:
  3833. iloc->bh = bh;
  3834. return 0;
  3835. }
  3836. int ext4_get_inode_loc(struct inode *inode, struct ext4_iloc *iloc)
  3837. {
  3838. /* We have all inode data except xattrs in memory here. */
  3839. return __ext4_get_inode_loc(inode, iloc,
  3840. !ext4_test_inode_state(inode, EXT4_STATE_XATTR));
  3841. }
  3842. void ext4_set_inode_flags(struct inode *inode)
  3843. {
  3844. unsigned int flags = EXT4_I(inode)->i_flags;
  3845. unsigned int new_fl = 0;
  3846. if (flags & EXT4_SYNC_FL)
  3847. new_fl |= S_SYNC;
  3848. if (flags & EXT4_APPEND_FL)
  3849. new_fl |= S_APPEND;
  3850. if (flags & EXT4_IMMUTABLE_FL)
  3851. new_fl |= S_IMMUTABLE;
  3852. if (flags & EXT4_NOATIME_FL)
  3853. new_fl |= S_NOATIME;
  3854. if (flags & EXT4_DIRSYNC_FL)
  3855. new_fl |= S_DIRSYNC;
  3856. if (test_opt(inode->i_sb, DAX) && S_ISREG(inode->i_mode))
  3857. new_fl |= S_DAX;
  3858. inode_set_flags(inode, new_fl,
  3859. S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC|S_DAX);
  3860. }
  3861. /* Propagate flags from i_flags to EXT4_I(inode)->i_flags */
  3862. void ext4_get_inode_flags(struct ext4_inode_info *ei)
  3863. {
  3864. unsigned int vfs_fl;
  3865. unsigned long old_fl, new_fl;
  3866. do {
  3867. vfs_fl = ei->vfs_inode.i_flags;
  3868. old_fl = ei->i_flags;
  3869. new_fl = old_fl & ~(EXT4_SYNC_FL|EXT4_APPEND_FL|
  3870. EXT4_IMMUTABLE_FL|EXT4_NOATIME_FL|
  3871. EXT4_DIRSYNC_FL);
  3872. if (vfs_fl & S_SYNC)
  3873. new_fl |= EXT4_SYNC_FL;
  3874. if (vfs_fl & S_APPEND)
  3875. new_fl |= EXT4_APPEND_FL;
  3876. if (vfs_fl & S_IMMUTABLE)
  3877. new_fl |= EXT4_IMMUTABLE_FL;
  3878. if (vfs_fl & S_NOATIME)
  3879. new_fl |= EXT4_NOATIME_FL;
  3880. if (vfs_fl & S_DIRSYNC)
  3881. new_fl |= EXT4_DIRSYNC_FL;
  3882. } while (cmpxchg(&ei->i_flags, old_fl, new_fl) != old_fl);
  3883. }
  3884. static blkcnt_t ext4_inode_blocks(struct ext4_inode *raw_inode,
  3885. struct ext4_inode_info *ei)
  3886. {
  3887. blkcnt_t i_blocks ;
  3888. struct inode *inode = &(ei->vfs_inode);
  3889. struct super_block *sb = inode->i_sb;
  3890. if (ext4_has_feature_huge_file(sb)) {
  3891. /* we are using combined 48 bit field */
  3892. i_blocks = ((u64)le16_to_cpu(raw_inode->i_blocks_high)) << 32 |
  3893. le32_to_cpu(raw_inode->i_blocks_lo);
  3894. if (ext4_test_inode_flag(inode, EXT4_INODE_HUGE_FILE)) {
  3895. /* i_blocks represent file system block size */
  3896. return i_blocks << (inode->i_blkbits - 9);
  3897. } else {
  3898. return i_blocks;
  3899. }
  3900. } else {
  3901. return le32_to_cpu(raw_inode->i_blocks_lo);
  3902. }
  3903. }
  3904. static inline void ext4_iget_extra_inode(struct inode *inode,
  3905. struct ext4_inode *raw_inode,
  3906. struct ext4_inode_info *ei)
  3907. {
  3908. __le32 *magic = (void *)raw_inode +
  3909. EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize;
  3910. if (*magic == cpu_to_le32(EXT4_XATTR_MAGIC)) {
  3911. ext4_set_inode_state(inode, EXT4_STATE_XATTR);
  3912. ext4_find_inline_data_nolock(inode);
  3913. } else
  3914. EXT4_I(inode)->i_inline_off = 0;
  3915. }
  3916. int ext4_get_projid(struct inode *inode, kprojid_t *projid)
  3917. {
  3918. if (!EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb, EXT4_FEATURE_RO_COMPAT_PROJECT))
  3919. return -EOPNOTSUPP;
  3920. *projid = EXT4_I(inode)->i_projid;
  3921. return 0;
  3922. }
  3923. struct inode *ext4_iget(struct super_block *sb, unsigned long ino)
  3924. {
  3925. struct ext4_iloc iloc;
  3926. struct ext4_inode *raw_inode;
  3927. struct ext4_inode_info *ei;
  3928. struct inode *inode;
  3929. journal_t *journal = EXT4_SB(sb)->s_journal;
  3930. long ret;
  3931. int block;
  3932. uid_t i_uid;
  3933. gid_t i_gid;
  3934. projid_t i_projid;
  3935. inode = iget_locked(sb, ino);
  3936. if (!inode)
  3937. return ERR_PTR(-ENOMEM);
  3938. if (!(inode->i_state & I_NEW))
  3939. return inode;
  3940. ei = EXT4_I(inode);
  3941. iloc.bh = NULL;
  3942. ret = __ext4_get_inode_loc(inode, &iloc, 0);
  3943. if (ret < 0)
  3944. goto bad_inode;
  3945. raw_inode = ext4_raw_inode(&iloc);
  3946. if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
  3947. ei->i_extra_isize = le16_to_cpu(raw_inode->i_extra_isize);
  3948. if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize >
  3949. EXT4_INODE_SIZE(inode->i_sb)) {
  3950. EXT4_ERROR_INODE(inode, "bad extra_isize (%u != %u)",
  3951. EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize,
  3952. EXT4_INODE_SIZE(inode->i_sb));
  3953. ret = -EFSCORRUPTED;
  3954. goto bad_inode;
  3955. }
  3956. } else
  3957. ei->i_extra_isize = 0;
  3958. /* Precompute checksum seed for inode metadata */
  3959. if (ext4_has_metadata_csum(sb)) {
  3960. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  3961. __u32 csum;
  3962. __le32 inum = cpu_to_le32(inode->i_ino);
  3963. __le32 gen = raw_inode->i_generation;
  3964. csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&inum,
  3965. sizeof(inum));
  3966. ei->i_csum_seed = ext4_chksum(sbi, csum, (__u8 *)&gen,
  3967. sizeof(gen));
  3968. }
  3969. if (!ext4_inode_csum_verify(inode, raw_inode, ei)) {
  3970. EXT4_ERROR_INODE(inode, "checksum invalid");
  3971. ret = -EFSBADCRC;
  3972. goto bad_inode;
  3973. }
  3974. inode->i_mode = le16_to_cpu(raw_inode->i_mode);
  3975. i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low);
  3976. i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low);
  3977. if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_PROJECT) &&
  3978. EXT4_INODE_SIZE(sb) > EXT4_GOOD_OLD_INODE_SIZE &&
  3979. EXT4_FITS_IN_INODE(raw_inode, ei, i_projid))
  3980. i_projid = (projid_t)le32_to_cpu(raw_inode->i_projid);
  3981. else
  3982. i_projid = EXT4_DEF_PROJID;
  3983. if (!(test_opt(inode->i_sb, NO_UID32))) {
  3984. i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16;
  3985. i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16;
  3986. }
  3987. i_uid_write(inode, i_uid);
  3988. i_gid_write(inode, i_gid);
  3989. ei->i_projid = make_kprojid(&init_user_ns, i_projid);
  3990. set_nlink(inode, le16_to_cpu(raw_inode->i_links_count));
  3991. ext4_clear_state_flags(ei); /* Only relevant on 32-bit archs */
  3992. ei->i_inline_off = 0;
  3993. ei->i_dir_start_lookup = 0;
  3994. ei->i_dtime = le32_to_cpu(raw_inode->i_dtime);
  3995. /* We now have enough fields to check if the inode was active or not.
  3996. * This is needed because nfsd might try to access dead inodes
  3997. * the test is that same one that e2fsck uses
  3998. * NeilBrown 1999oct15
  3999. */
  4000. if (inode->i_nlink == 0) {
  4001. if ((inode->i_mode == 0 ||
  4002. !(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ORPHAN_FS)) &&
  4003. ino != EXT4_BOOT_LOADER_INO) {
  4004. /* this inode is deleted */
  4005. ret = -ESTALE;
  4006. goto bad_inode;
  4007. }
  4008. /* The only unlinked inodes we let through here have
  4009. * valid i_mode and are being read by the orphan
  4010. * recovery code: that's fine, we're about to complete
  4011. * the process of deleting those.
  4012. * OR it is the EXT4_BOOT_LOADER_INO which is
  4013. * not initialized on a new filesystem. */
  4014. }
  4015. ei->i_flags = le32_to_cpu(raw_inode->i_flags);
  4016. inode->i_blocks = ext4_inode_blocks(raw_inode, ei);
  4017. ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl_lo);
  4018. if (ext4_has_feature_64bit(sb))
  4019. ei->i_file_acl |=
  4020. ((__u64)le16_to_cpu(raw_inode->i_file_acl_high)) << 32;
  4021. inode->i_size = ext4_isize(raw_inode);
  4022. ei->i_disksize = inode->i_size;
  4023. #ifdef CONFIG_QUOTA
  4024. ei->i_reserved_quota = 0;
  4025. #endif
  4026. inode->i_generation = le32_to_cpu(raw_inode->i_generation);
  4027. ei->i_block_group = iloc.block_group;
  4028. ei->i_last_alloc_group = ~0;
  4029. /*
  4030. * NOTE! The in-memory inode i_data array is in little-endian order
  4031. * even on big-endian machines: we do NOT byteswap the block numbers!
  4032. */
  4033. for (block = 0; block < EXT4_N_BLOCKS; block++)
  4034. ei->i_data[block] = raw_inode->i_block[block];
  4035. INIT_LIST_HEAD(&ei->i_orphan);
  4036. /*
  4037. * Set transaction id's of transactions that have to be committed
  4038. * to finish f[data]sync. We set them to currently running transaction
  4039. * as we cannot be sure that the inode or some of its metadata isn't
  4040. * part of the transaction - the inode could have been reclaimed and
  4041. * now it is reread from disk.
  4042. */
  4043. if (journal) {
  4044. transaction_t *transaction;
  4045. tid_t tid;
  4046. read_lock(&journal->j_state_lock);
  4047. if (journal->j_running_transaction)
  4048. transaction = journal->j_running_transaction;
  4049. else
  4050. transaction = journal->j_committing_transaction;
  4051. if (transaction)
  4052. tid = transaction->t_tid;
  4053. else
  4054. tid = journal->j_commit_sequence;
  4055. read_unlock(&journal->j_state_lock);
  4056. ei->i_sync_tid = tid;
  4057. ei->i_datasync_tid = tid;
  4058. }
  4059. if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
  4060. if (ei->i_extra_isize == 0) {
  4061. /* The extra space is currently unused. Use it. */
  4062. ei->i_extra_isize = sizeof(struct ext4_inode) -
  4063. EXT4_GOOD_OLD_INODE_SIZE;
  4064. } else {
  4065. ext4_iget_extra_inode(inode, raw_inode, ei);
  4066. }
  4067. }
  4068. EXT4_INODE_GET_XTIME(i_ctime, inode, raw_inode);
  4069. EXT4_INODE_GET_XTIME(i_mtime, inode, raw_inode);
  4070. EXT4_INODE_GET_XTIME(i_atime, inode, raw_inode);
  4071. EXT4_EINODE_GET_XTIME(i_crtime, ei, raw_inode);
  4072. if (likely(!test_opt2(inode->i_sb, HURD_COMPAT))) {
  4073. inode->i_version = le32_to_cpu(raw_inode->i_disk_version);
  4074. if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
  4075. if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi))
  4076. inode->i_version |=
  4077. (__u64)(le32_to_cpu(raw_inode->i_version_hi)) << 32;
  4078. }
  4079. }
  4080. ret = 0;
  4081. if (ei->i_file_acl &&
  4082. !ext4_data_block_valid(EXT4_SB(sb), ei->i_file_acl, 1)) {
  4083. EXT4_ERROR_INODE(inode, "bad extended attribute block %llu",
  4084. ei->i_file_acl);
  4085. ret = -EFSCORRUPTED;
  4086. goto bad_inode;
  4087. } else if (!ext4_has_inline_data(inode)) {
  4088. if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
  4089. if ((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
  4090. (S_ISLNK(inode->i_mode) &&
  4091. !ext4_inode_is_fast_symlink(inode))))
  4092. /* Validate extent which is part of inode */
  4093. ret = ext4_ext_check_inode(inode);
  4094. } else if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
  4095. (S_ISLNK(inode->i_mode) &&
  4096. !ext4_inode_is_fast_symlink(inode))) {
  4097. /* Validate block references which are part of inode */
  4098. ret = ext4_ind_check_inode(inode);
  4099. }
  4100. }
  4101. if (ret)
  4102. goto bad_inode;
  4103. if (S_ISREG(inode->i_mode)) {
  4104. inode->i_op = &ext4_file_inode_operations;
  4105. inode->i_fop = &ext4_file_operations;
  4106. ext4_set_aops(inode);
  4107. } else if (S_ISDIR(inode->i_mode)) {
  4108. inode->i_op = &ext4_dir_inode_operations;
  4109. inode->i_fop = &ext4_dir_operations;
  4110. } else if (S_ISLNK(inode->i_mode)) {
  4111. if (ext4_encrypted_inode(inode)) {
  4112. inode->i_op = &ext4_encrypted_symlink_inode_operations;
  4113. ext4_set_aops(inode);
  4114. } else if (ext4_inode_is_fast_symlink(inode)) {
  4115. inode->i_link = (char *)ei->i_data;
  4116. inode->i_op = &ext4_fast_symlink_inode_operations;
  4117. nd_terminate_link(ei->i_data, inode->i_size,
  4118. sizeof(ei->i_data) - 1);
  4119. } else {
  4120. inode->i_op = &ext4_symlink_inode_operations;
  4121. ext4_set_aops(inode);
  4122. }
  4123. inode_nohighmem(inode);
  4124. } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
  4125. S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
  4126. inode->i_op = &ext4_special_inode_operations;
  4127. if (raw_inode->i_block[0])
  4128. init_special_inode(inode, inode->i_mode,
  4129. old_decode_dev(le32_to_cpu(raw_inode->i_block[0])));
  4130. else
  4131. init_special_inode(inode, inode->i_mode,
  4132. new_decode_dev(le32_to_cpu(raw_inode->i_block[1])));
  4133. } else if (ino == EXT4_BOOT_LOADER_INO) {
  4134. make_bad_inode(inode);
  4135. } else {
  4136. ret = -EFSCORRUPTED;
  4137. EXT4_ERROR_INODE(inode, "bogus i_mode (%o)", inode->i_mode);
  4138. goto bad_inode;
  4139. }
  4140. brelse(iloc.bh);
  4141. ext4_set_inode_flags(inode);
  4142. unlock_new_inode(inode);
  4143. return inode;
  4144. bad_inode:
  4145. brelse(iloc.bh);
  4146. iget_failed(inode);
  4147. return ERR_PTR(ret);
  4148. }
  4149. struct inode *ext4_iget_normal(struct super_block *sb, unsigned long ino)
  4150. {
  4151. if (ino < EXT4_FIRST_INO(sb) && ino != EXT4_ROOT_INO)
  4152. return ERR_PTR(-EFSCORRUPTED);
  4153. return ext4_iget(sb, ino);
  4154. }
  4155. static int ext4_inode_blocks_set(handle_t *handle,
  4156. struct ext4_inode *raw_inode,
  4157. struct ext4_inode_info *ei)
  4158. {
  4159. struct inode *inode = &(ei->vfs_inode);
  4160. u64 i_blocks = inode->i_blocks;
  4161. struct super_block *sb = inode->i_sb;
  4162. if (i_blocks <= ~0U) {
  4163. /*
  4164. * i_blocks can be represented in a 32 bit variable
  4165. * as multiple of 512 bytes
  4166. */
  4167. raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
  4168. raw_inode->i_blocks_high = 0;
  4169. ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE);
  4170. return 0;
  4171. }
  4172. if (!ext4_has_feature_huge_file(sb))
  4173. return -EFBIG;
  4174. if (i_blocks <= 0xffffffffffffULL) {
  4175. /*
  4176. * i_blocks can be represented in a 48 bit variable
  4177. * as multiple of 512 bytes
  4178. */
  4179. raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
  4180. raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32);
  4181. ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE);
  4182. } else {
  4183. ext4_set_inode_flag(inode, EXT4_INODE_HUGE_FILE);
  4184. /* i_block is stored in file system block size */
  4185. i_blocks = i_blocks >> (inode->i_blkbits - 9);
  4186. raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
  4187. raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32);
  4188. }
  4189. return 0;
  4190. }
  4191. struct other_inode {
  4192. unsigned long orig_ino;
  4193. struct ext4_inode *raw_inode;
  4194. };
  4195. static int other_inode_match(struct inode * inode, unsigned long ino,
  4196. void *data)
  4197. {
  4198. struct other_inode *oi = (struct other_inode *) data;
  4199. if ((inode->i_ino != ino) ||
  4200. (inode->i_state & (I_FREEING | I_WILL_FREE | I_NEW |
  4201. I_DIRTY_SYNC | I_DIRTY_DATASYNC)) ||
  4202. ((inode->i_state & I_DIRTY_TIME) == 0))
  4203. return 0;
  4204. spin_lock(&inode->i_lock);
  4205. if (((inode->i_state & (I_FREEING | I_WILL_FREE | I_NEW |
  4206. I_DIRTY_SYNC | I_DIRTY_DATASYNC)) == 0) &&
  4207. (inode->i_state & I_DIRTY_TIME)) {
  4208. struct ext4_inode_info *ei = EXT4_I(inode);
  4209. inode->i_state &= ~(I_DIRTY_TIME | I_DIRTY_TIME_EXPIRED);
  4210. spin_unlock(&inode->i_lock);
  4211. spin_lock(&ei->i_raw_lock);
  4212. EXT4_INODE_SET_XTIME(i_ctime, inode, oi->raw_inode);
  4213. EXT4_INODE_SET_XTIME(i_mtime, inode, oi->raw_inode);
  4214. EXT4_INODE_SET_XTIME(i_atime, inode, oi->raw_inode);
  4215. ext4_inode_csum_set(inode, oi->raw_inode, ei);
  4216. spin_unlock(&ei->i_raw_lock);
  4217. trace_ext4_other_inode_update_time(inode, oi->orig_ino);
  4218. return -1;
  4219. }
  4220. spin_unlock(&inode->i_lock);
  4221. return -1;
  4222. }
  4223. /*
  4224. * Opportunistically update the other time fields for other inodes in
  4225. * the same inode table block.
  4226. */
  4227. static void ext4_update_other_inodes_time(struct super_block *sb,
  4228. unsigned long orig_ino, char *buf)
  4229. {
  4230. struct other_inode oi;
  4231. unsigned long ino;
  4232. int i, inodes_per_block = EXT4_SB(sb)->s_inodes_per_block;
  4233. int inode_size = EXT4_INODE_SIZE(sb);
  4234. oi.orig_ino = orig_ino;
  4235. /*
  4236. * Calculate the first inode in the inode table block. Inode
  4237. * numbers are one-based. That is, the first inode in a block
  4238. * (assuming 4k blocks and 256 byte inodes) is (n*16 + 1).
  4239. */
  4240. ino = ((orig_ino - 1) & ~(inodes_per_block - 1)) + 1;
  4241. for (i = 0; i < inodes_per_block; i++, ino++, buf += inode_size) {
  4242. if (ino == orig_ino)
  4243. continue;
  4244. oi.raw_inode = (struct ext4_inode *) buf;
  4245. (void) find_inode_nowait(sb, ino, other_inode_match, &oi);
  4246. }
  4247. }
  4248. /*
  4249. * Post the struct inode info into an on-disk inode location in the
  4250. * buffer-cache. This gobbles the caller's reference to the
  4251. * buffer_head in the inode location struct.
  4252. *
  4253. * The caller must have write access to iloc->bh.
  4254. */
  4255. static int ext4_do_update_inode(handle_t *handle,
  4256. struct inode *inode,
  4257. struct ext4_iloc *iloc)
  4258. {
  4259. struct ext4_inode *raw_inode = ext4_raw_inode(iloc);
  4260. struct ext4_inode_info *ei = EXT4_I(inode);
  4261. struct buffer_head *bh = iloc->bh;
  4262. struct super_block *sb = inode->i_sb;
  4263. int err = 0, rc, block;
  4264. int need_datasync = 0, set_large_file = 0;
  4265. uid_t i_uid;
  4266. gid_t i_gid;
  4267. projid_t i_projid;
  4268. spin_lock(&ei->i_raw_lock);
  4269. /* For fields not tracked in the in-memory inode,
  4270. * initialise them to zero for new inodes. */
  4271. if (ext4_test_inode_state(inode, EXT4_STATE_NEW))
  4272. memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
  4273. ext4_get_inode_flags(ei);
  4274. raw_inode->i_mode = cpu_to_le16(inode->i_mode);
  4275. i_uid = i_uid_read(inode);
  4276. i_gid = i_gid_read(inode);
  4277. i_projid = from_kprojid(&init_user_ns, ei->i_projid);
  4278. if (!(test_opt(inode->i_sb, NO_UID32))) {
  4279. raw_inode->i_uid_low = cpu_to_le16(low_16_bits(i_uid));
  4280. raw_inode->i_gid_low = cpu_to_le16(low_16_bits(i_gid));
  4281. /*
  4282. * Fix up interoperability with old kernels. Otherwise, old inodes get
  4283. * re-used with the upper 16 bits of the uid/gid intact
  4284. */
  4285. if (!ei->i_dtime) {
  4286. raw_inode->i_uid_high =
  4287. cpu_to_le16(high_16_bits(i_uid));
  4288. raw_inode->i_gid_high =
  4289. cpu_to_le16(high_16_bits(i_gid));
  4290. } else {
  4291. raw_inode->i_uid_high = 0;
  4292. raw_inode->i_gid_high = 0;
  4293. }
  4294. } else {
  4295. raw_inode->i_uid_low = cpu_to_le16(fs_high2lowuid(i_uid));
  4296. raw_inode->i_gid_low = cpu_to_le16(fs_high2lowgid(i_gid));
  4297. raw_inode->i_uid_high = 0;
  4298. raw_inode->i_gid_high = 0;
  4299. }
  4300. raw_inode->i_links_count = cpu_to_le16(inode->i_nlink);
  4301. EXT4_INODE_SET_XTIME(i_ctime, inode, raw_inode);
  4302. EXT4_INODE_SET_XTIME(i_mtime, inode, raw_inode);
  4303. EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode);
  4304. EXT4_EINODE_SET_XTIME(i_crtime, ei, raw_inode);
  4305. err = ext4_inode_blocks_set(handle, raw_inode, ei);
  4306. if (err) {
  4307. spin_unlock(&ei->i_raw_lock);
  4308. goto out_brelse;
  4309. }
  4310. raw_inode->i_dtime = cpu_to_le32(ei->i_dtime);
  4311. raw_inode->i_flags = cpu_to_le32(ei->i_flags & 0xFFFFFFFF);
  4312. if (likely(!test_opt2(inode->i_sb, HURD_COMPAT)))
  4313. raw_inode->i_file_acl_high =
  4314. cpu_to_le16(ei->i_file_acl >> 32);
  4315. raw_inode->i_file_acl_lo = cpu_to_le32(ei->i_file_acl);
  4316. if (ei->i_disksize != ext4_isize(raw_inode)) {
  4317. ext4_isize_set(raw_inode, ei->i_disksize);
  4318. need_datasync = 1;
  4319. }
  4320. if (ei->i_disksize > 0x7fffffffULL) {
  4321. if (!ext4_has_feature_large_file(sb) ||
  4322. EXT4_SB(sb)->s_es->s_rev_level ==
  4323. cpu_to_le32(EXT4_GOOD_OLD_REV))
  4324. set_large_file = 1;
  4325. }
  4326. raw_inode->i_generation = cpu_to_le32(inode->i_generation);
  4327. if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
  4328. if (old_valid_dev(inode->i_rdev)) {
  4329. raw_inode->i_block[0] =
  4330. cpu_to_le32(old_encode_dev(inode->i_rdev));
  4331. raw_inode->i_block[1] = 0;
  4332. } else {
  4333. raw_inode->i_block[0] = 0;
  4334. raw_inode->i_block[1] =
  4335. cpu_to_le32(new_encode_dev(inode->i_rdev));
  4336. raw_inode->i_block[2] = 0;
  4337. }
  4338. } else if (!ext4_has_inline_data(inode)) {
  4339. for (block = 0; block < EXT4_N_BLOCKS; block++)
  4340. raw_inode->i_block[block] = ei->i_data[block];
  4341. }
  4342. if (likely(!test_opt2(inode->i_sb, HURD_COMPAT))) {
  4343. raw_inode->i_disk_version = cpu_to_le32(inode->i_version);
  4344. if (ei->i_extra_isize) {
  4345. if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi))
  4346. raw_inode->i_version_hi =
  4347. cpu_to_le32(inode->i_version >> 32);
  4348. raw_inode->i_extra_isize =
  4349. cpu_to_le16(ei->i_extra_isize);
  4350. }
  4351. }
  4352. BUG_ON(!EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
  4353. EXT4_FEATURE_RO_COMPAT_PROJECT) &&
  4354. i_projid != EXT4_DEF_PROJID);
  4355. if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE &&
  4356. EXT4_FITS_IN_INODE(raw_inode, ei, i_projid))
  4357. raw_inode->i_projid = cpu_to_le32(i_projid);
  4358. ext4_inode_csum_set(inode, raw_inode, ei);
  4359. spin_unlock(&ei->i_raw_lock);
  4360. if (inode->i_sb->s_flags & MS_LAZYTIME)
  4361. ext4_update_other_inodes_time(inode->i_sb, inode->i_ino,
  4362. bh->b_data);
  4363. BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
  4364. rc = ext4_handle_dirty_metadata(handle, NULL, bh);
  4365. if (!err)
  4366. err = rc;
  4367. ext4_clear_inode_state(inode, EXT4_STATE_NEW);
  4368. if (set_large_file) {
  4369. BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get write access");
  4370. err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh);
  4371. if (err)
  4372. goto out_brelse;
  4373. ext4_update_dynamic_rev(sb);
  4374. ext4_set_feature_large_file(sb);
  4375. ext4_handle_sync(handle);
  4376. err = ext4_handle_dirty_super(handle, sb);
  4377. }
  4378. ext4_update_inode_fsync_trans(handle, inode, need_datasync);
  4379. out_brelse:
  4380. brelse(bh);
  4381. ext4_std_error(inode->i_sb, err);
  4382. return err;
  4383. }
  4384. /*
  4385. * ext4_write_inode()
  4386. *
  4387. * We are called from a few places:
  4388. *
  4389. * - Within generic_file_aio_write() -> generic_write_sync() for O_SYNC files.
  4390. * Here, there will be no transaction running. We wait for any running
  4391. * transaction to commit.
  4392. *
  4393. * - Within flush work (sys_sync(), kupdate and such).
  4394. * We wait on commit, if told to.
  4395. *
  4396. * - Within iput_final() -> write_inode_now()
  4397. * We wait on commit, if told to.
  4398. *
  4399. * In all cases it is actually safe for us to return without doing anything,
  4400. * because the inode has been copied into a raw inode buffer in
  4401. * ext4_mark_inode_dirty(). This is a correctness thing for WB_SYNC_ALL
  4402. * writeback.
  4403. *
  4404. * Note that we are absolutely dependent upon all inode dirtiers doing the
  4405. * right thing: they *must* call mark_inode_dirty() after dirtying info in
  4406. * which we are interested.
  4407. *
  4408. * It would be a bug for them to not do this. The code:
  4409. *
  4410. * mark_inode_dirty(inode)
  4411. * stuff();
  4412. * inode->i_size = expr;
  4413. *
  4414. * is in error because write_inode() could occur while `stuff()' is running,
  4415. * and the new i_size will be lost. Plus the inode will no longer be on the
  4416. * superblock's dirty inode list.
  4417. */
  4418. int ext4_write_inode(struct inode *inode, struct writeback_control *wbc)
  4419. {
  4420. int err;
  4421. if (WARN_ON_ONCE(current->flags & PF_MEMALLOC))
  4422. return 0;
  4423. if (EXT4_SB(inode->i_sb)->s_journal) {
  4424. if (ext4_journal_current_handle()) {
  4425. jbd_debug(1, "called recursively, non-PF_MEMALLOC!\n");
  4426. dump_stack();
  4427. return -EIO;
  4428. }
  4429. /*
  4430. * No need to force transaction in WB_SYNC_NONE mode. Also
  4431. * ext4_sync_fs() will force the commit after everything is
  4432. * written.
  4433. */
  4434. if (wbc->sync_mode != WB_SYNC_ALL || wbc->for_sync)
  4435. return 0;
  4436. err = ext4_force_commit(inode->i_sb);
  4437. } else {
  4438. struct ext4_iloc iloc;
  4439. err = __ext4_get_inode_loc(inode, &iloc, 0);
  4440. if (err)
  4441. return err;
  4442. /*
  4443. * sync(2) will flush the whole buffer cache. No need to do
  4444. * it here separately for each inode.
  4445. */
  4446. if (wbc->sync_mode == WB_SYNC_ALL && !wbc->for_sync)
  4447. sync_dirty_buffer(iloc.bh);
  4448. if (buffer_req(iloc.bh) && !buffer_uptodate(iloc.bh)) {
  4449. EXT4_ERROR_INODE_BLOCK(inode, iloc.bh->b_blocknr,
  4450. "IO error syncing inode");
  4451. err = -EIO;
  4452. }
  4453. brelse(iloc.bh);
  4454. }
  4455. return err;
  4456. }
  4457. /*
  4458. * In data=journal mode ext4_journalled_invalidatepage() may fail to invalidate
  4459. * buffers that are attached to a page stradding i_size and are undergoing
  4460. * commit. In that case we have to wait for commit to finish and try again.
  4461. */
  4462. static void ext4_wait_for_tail_page_commit(struct inode *inode)
  4463. {
  4464. struct page *page;
  4465. unsigned offset;
  4466. journal_t *journal = EXT4_SB(inode->i_sb)->s_journal;
  4467. tid_t commit_tid = 0;
  4468. int ret;
  4469. offset = inode->i_size & (PAGE_SIZE - 1);
  4470. /*
  4471. * All buffers in the last page remain valid? Then there's nothing to
  4472. * do. We do the check mainly to optimize the common PAGE_SIZE ==
  4473. * blocksize case
  4474. */
  4475. if (offset > PAGE_SIZE - (1 << inode->i_blkbits))
  4476. return;
  4477. while (1) {
  4478. page = find_lock_page(inode->i_mapping,
  4479. inode->i_size >> PAGE_SHIFT);
  4480. if (!page)
  4481. return;
  4482. ret = __ext4_journalled_invalidatepage(page, offset,
  4483. PAGE_SIZE - offset);
  4484. unlock_page(page);
  4485. put_page(page);
  4486. if (ret != -EBUSY)
  4487. return;
  4488. commit_tid = 0;
  4489. read_lock(&journal->j_state_lock);
  4490. if (journal->j_committing_transaction)
  4491. commit_tid = journal->j_committing_transaction->t_tid;
  4492. read_unlock(&journal->j_state_lock);
  4493. if (commit_tid)
  4494. jbd2_log_wait_commit(journal, commit_tid);
  4495. }
  4496. }
  4497. /*
  4498. * ext4_setattr()
  4499. *
  4500. * Called from notify_change.
  4501. *
  4502. * We want to trap VFS attempts to truncate the file as soon as
  4503. * possible. In particular, we want to make sure that when the VFS
  4504. * shrinks i_size, we put the inode on the orphan list and modify
  4505. * i_disksize immediately, so that during the subsequent flushing of
  4506. * dirty pages and freeing of disk blocks, we can guarantee that any
  4507. * commit will leave the blocks being flushed in an unused state on
  4508. * disk. (On recovery, the inode will get truncated and the blocks will
  4509. * be freed, so we have a strong guarantee that no future commit will
  4510. * leave these blocks visible to the user.)
  4511. *
  4512. * Another thing we have to assure is that if we are in ordered mode
  4513. * and inode is still attached to the committing transaction, we must
  4514. * we start writeout of all the dirty pages which are being truncated.
  4515. * This way we are sure that all the data written in the previous
  4516. * transaction are already on disk (truncate waits for pages under
  4517. * writeback).
  4518. *
  4519. * Called with inode->i_mutex down.
  4520. */
  4521. int ext4_setattr(struct dentry *dentry, struct iattr *attr)
  4522. {
  4523. struct inode *inode = d_inode(dentry);
  4524. int error, rc = 0;
  4525. int orphan = 0;
  4526. const unsigned int ia_valid = attr->ia_valid;
  4527. error = inode_change_ok(inode, attr);
  4528. if (error)
  4529. return error;
  4530. if (is_quota_modification(inode, attr)) {
  4531. error = dquot_initialize(inode);
  4532. if (error)
  4533. return error;
  4534. }
  4535. if ((ia_valid & ATTR_UID && !uid_eq(attr->ia_uid, inode->i_uid)) ||
  4536. (ia_valid & ATTR_GID && !gid_eq(attr->ia_gid, inode->i_gid))) {
  4537. handle_t *handle;
  4538. /* (user+group)*(old+new) structure, inode write (sb,
  4539. * inode block, ? - but truncate inode update has it) */
  4540. handle = ext4_journal_start(inode, EXT4_HT_QUOTA,
  4541. (EXT4_MAXQUOTAS_INIT_BLOCKS(inode->i_sb) +
  4542. EXT4_MAXQUOTAS_DEL_BLOCKS(inode->i_sb)) + 3);
  4543. if (IS_ERR(handle)) {
  4544. error = PTR_ERR(handle);
  4545. goto err_out;
  4546. }
  4547. error = dquot_transfer(inode, attr);
  4548. if (error) {
  4549. ext4_journal_stop(handle);
  4550. return error;
  4551. }
  4552. /* Update corresponding info in inode so that everything is in
  4553. * one transaction */
  4554. if (attr->ia_valid & ATTR_UID)
  4555. inode->i_uid = attr->ia_uid;
  4556. if (attr->ia_valid & ATTR_GID)
  4557. inode->i_gid = attr->ia_gid;
  4558. error = ext4_mark_inode_dirty(handle, inode);
  4559. ext4_journal_stop(handle);
  4560. }
  4561. if (attr->ia_valid & ATTR_SIZE) {
  4562. handle_t *handle;
  4563. loff_t oldsize = inode->i_size;
  4564. int shrink = (attr->ia_size <= inode->i_size);
  4565. if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
  4566. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  4567. if (attr->ia_size > sbi->s_bitmap_maxbytes)
  4568. return -EFBIG;
  4569. }
  4570. if (!S_ISREG(inode->i_mode))
  4571. return -EINVAL;
  4572. if (IS_I_VERSION(inode) && attr->ia_size != inode->i_size)
  4573. inode_inc_iversion(inode);
  4574. if (ext4_should_order_data(inode) &&
  4575. (attr->ia_size < inode->i_size)) {
  4576. error = ext4_begin_ordered_truncate(inode,
  4577. attr->ia_size);
  4578. if (error)
  4579. goto err_out;
  4580. }
  4581. if (attr->ia_size != inode->i_size) {
  4582. handle = ext4_journal_start(inode, EXT4_HT_INODE, 3);
  4583. if (IS_ERR(handle)) {
  4584. error = PTR_ERR(handle);
  4585. goto err_out;
  4586. }
  4587. if (ext4_handle_valid(handle) && shrink) {
  4588. error = ext4_orphan_add(handle, inode);
  4589. orphan = 1;
  4590. }
  4591. /*
  4592. * Update c/mtime on truncate up, ext4_truncate() will
  4593. * update c/mtime in shrink case below
  4594. */
  4595. if (!shrink) {
  4596. inode->i_mtime = ext4_current_time(inode);
  4597. inode->i_ctime = inode->i_mtime;
  4598. }
  4599. down_write(&EXT4_I(inode)->i_data_sem);
  4600. EXT4_I(inode)->i_disksize = attr->ia_size;
  4601. rc = ext4_mark_inode_dirty(handle, inode);
  4602. if (!error)
  4603. error = rc;
  4604. /*
  4605. * We have to update i_size under i_data_sem together
  4606. * with i_disksize to avoid races with writeback code
  4607. * running ext4_wb_update_i_disksize().
  4608. */
  4609. if (!error)
  4610. i_size_write(inode, attr->ia_size);
  4611. up_write(&EXT4_I(inode)->i_data_sem);
  4612. ext4_journal_stop(handle);
  4613. if (error) {
  4614. if (orphan)
  4615. ext4_orphan_del(NULL, inode);
  4616. goto err_out;
  4617. }
  4618. }
  4619. if (!shrink)
  4620. pagecache_isize_extended(inode, oldsize, inode->i_size);
  4621. /*
  4622. * Blocks are going to be removed from the inode. Wait
  4623. * for dio in flight. Temporarily disable
  4624. * dioread_nolock to prevent livelock.
  4625. */
  4626. if (orphan) {
  4627. if (!ext4_should_journal_data(inode)) {
  4628. ext4_inode_block_unlocked_dio(inode);
  4629. inode_dio_wait(inode);
  4630. ext4_inode_resume_unlocked_dio(inode);
  4631. } else
  4632. ext4_wait_for_tail_page_commit(inode);
  4633. }
  4634. down_write(&EXT4_I(inode)->i_mmap_sem);
  4635. /*
  4636. * Truncate pagecache after we've waited for commit
  4637. * in data=journal mode to make pages freeable.
  4638. */
  4639. truncate_pagecache(inode, inode->i_size);
  4640. if (shrink)
  4641. ext4_truncate(inode);
  4642. up_write(&EXT4_I(inode)->i_mmap_sem);
  4643. }
  4644. if (!rc) {
  4645. setattr_copy(inode, attr);
  4646. mark_inode_dirty(inode);
  4647. }
  4648. /*
  4649. * If the call to ext4_truncate failed to get a transaction handle at
  4650. * all, we need to clean up the in-core orphan list manually.
  4651. */
  4652. if (orphan && inode->i_nlink)
  4653. ext4_orphan_del(NULL, inode);
  4654. if (!rc && (ia_valid & ATTR_MODE))
  4655. rc = posix_acl_chmod(inode, inode->i_mode);
  4656. err_out:
  4657. ext4_std_error(inode->i_sb, error);
  4658. if (!error)
  4659. error = rc;
  4660. return error;
  4661. }
  4662. int ext4_getattr(struct vfsmount *mnt, struct dentry *dentry,
  4663. struct kstat *stat)
  4664. {
  4665. struct inode *inode;
  4666. unsigned long long delalloc_blocks;
  4667. inode = d_inode(dentry);
  4668. generic_fillattr(inode, stat);
  4669. /*
  4670. * If there is inline data in the inode, the inode will normally not
  4671. * have data blocks allocated (it may have an external xattr block).
  4672. * Report at least one sector for such files, so tools like tar, rsync,
  4673. * others doen't incorrectly think the file is completely sparse.
  4674. */
  4675. if (unlikely(ext4_has_inline_data(inode)))
  4676. stat->blocks += (stat->size + 511) >> 9;
  4677. /*
  4678. * We can't update i_blocks if the block allocation is delayed
  4679. * otherwise in the case of system crash before the real block
  4680. * allocation is done, we will have i_blocks inconsistent with
  4681. * on-disk file blocks.
  4682. * We always keep i_blocks updated together with real
  4683. * allocation. But to not confuse with user, stat
  4684. * will return the blocks that include the delayed allocation
  4685. * blocks for this file.
  4686. */
  4687. delalloc_blocks = EXT4_C2B(EXT4_SB(inode->i_sb),
  4688. EXT4_I(inode)->i_reserved_data_blocks);
  4689. stat->blocks += delalloc_blocks << (inode->i_sb->s_blocksize_bits - 9);
  4690. return 0;
  4691. }
  4692. static int ext4_index_trans_blocks(struct inode *inode, int lblocks,
  4693. int pextents)
  4694. {
  4695. if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
  4696. return ext4_ind_trans_blocks(inode, lblocks);
  4697. return ext4_ext_index_trans_blocks(inode, pextents);
  4698. }
  4699. /*
  4700. * Account for index blocks, block groups bitmaps and block group
  4701. * descriptor blocks if modify datablocks and index blocks
  4702. * worse case, the indexs blocks spread over different block groups
  4703. *
  4704. * If datablocks are discontiguous, they are possible to spread over
  4705. * different block groups too. If they are contiguous, with flexbg,
  4706. * they could still across block group boundary.
  4707. *
  4708. * Also account for superblock, inode, quota and xattr blocks
  4709. */
  4710. static int ext4_meta_trans_blocks(struct inode *inode, int lblocks,
  4711. int pextents)
  4712. {
  4713. ext4_group_t groups, ngroups = ext4_get_groups_count(inode->i_sb);
  4714. int gdpblocks;
  4715. int idxblocks;
  4716. int ret = 0;
  4717. /*
  4718. * How many index blocks need to touch to map @lblocks logical blocks
  4719. * to @pextents physical extents?
  4720. */
  4721. idxblocks = ext4_index_trans_blocks(inode, lblocks, pextents);
  4722. ret = idxblocks;
  4723. /*
  4724. * Now let's see how many group bitmaps and group descriptors need
  4725. * to account
  4726. */
  4727. groups = idxblocks + pextents;
  4728. gdpblocks = groups;
  4729. if (groups > ngroups)
  4730. groups = ngroups;
  4731. if (groups > EXT4_SB(inode->i_sb)->s_gdb_count)
  4732. gdpblocks = EXT4_SB(inode->i_sb)->s_gdb_count;
  4733. /* bitmaps and block group descriptor blocks */
  4734. ret += groups + gdpblocks;
  4735. /* Blocks for super block, inode, quota and xattr blocks */
  4736. ret += EXT4_META_TRANS_BLOCKS(inode->i_sb);
  4737. return ret;
  4738. }
  4739. /*
  4740. * Calculate the total number of credits to reserve to fit
  4741. * the modification of a single pages into a single transaction,
  4742. * which may include multiple chunks of block allocations.
  4743. *
  4744. * This could be called via ext4_write_begin()
  4745. *
  4746. * We need to consider the worse case, when
  4747. * one new block per extent.
  4748. */
  4749. int ext4_writepage_trans_blocks(struct inode *inode)
  4750. {
  4751. int bpp = ext4_journal_blocks_per_page(inode);
  4752. int ret;
  4753. ret = ext4_meta_trans_blocks(inode, bpp, bpp);
  4754. /* Account for data blocks for journalled mode */
  4755. if (ext4_should_journal_data(inode))
  4756. ret += bpp;
  4757. return ret;
  4758. }
  4759. /*
  4760. * Calculate the journal credits for a chunk of data modification.
  4761. *
  4762. * This is called from DIO, fallocate or whoever calling
  4763. * ext4_map_blocks() to map/allocate a chunk of contiguous disk blocks.
  4764. *
  4765. * journal buffers for data blocks are not included here, as DIO
  4766. * and fallocate do no need to journal data buffers.
  4767. */
  4768. int ext4_chunk_trans_blocks(struct inode *inode, int nrblocks)
  4769. {
  4770. return ext4_meta_trans_blocks(inode, nrblocks, 1);
  4771. }
  4772. /*
  4773. * The caller must have previously called ext4_reserve_inode_write().
  4774. * Give this, we know that the caller already has write access to iloc->bh.
  4775. */
  4776. int ext4_mark_iloc_dirty(handle_t *handle,
  4777. struct inode *inode, struct ext4_iloc *iloc)
  4778. {
  4779. int err = 0;
  4780. if (IS_I_VERSION(inode))
  4781. inode_inc_iversion(inode);
  4782. /* the do_update_inode consumes one bh->b_count */
  4783. get_bh(iloc->bh);
  4784. /* ext4_do_update_inode() does jbd2_journal_dirty_metadata */
  4785. err = ext4_do_update_inode(handle, inode, iloc);
  4786. put_bh(iloc->bh);
  4787. return err;
  4788. }
  4789. /*
  4790. * On success, We end up with an outstanding reference count against
  4791. * iloc->bh. This _must_ be cleaned up later.
  4792. */
  4793. int
  4794. ext4_reserve_inode_write(handle_t *handle, struct inode *inode,
  4795. struct ext4_iloc *iloc)
  4796. {
  4797. int err;
  4798. err = ext4_get_inode_loc(inode, iloc);
  4799. if (!err) {
  4800. BUFFER_TRACE(iloc->bh, "get_write_access");
  4801. err = ext4_journal_get_write_access(handle, iloc->bh);
  4802. if (err) {
  4803. brelse(iloc->bh);
  4804. iloc->bh = NULL;
  4805. }
  4806. }
  4807. ext4_std_error(inode->i_sb, err);
  4808. return err;
  4809. }
  4810. /*
  4811. * Expand an inode by new_extra_isize bytes.
  4812. * Returns 0 on success or negative error number on failure.
  4813. */
  4814. static int ext4_expand_extra_isize(struct inode *inode,
  4815. unsigned int new_extra_isize,
  4816. struct ext4_iloc iloc,
  4817. handle_t *handle)
  4818. {
  4819. struct ext4_inode *raw_inode;
  4820. struct ext4_xattr_ibody_header *header;
  4821. if (EXT4_I(inode)->i_extra_isize >= new_extra_isize)
  4822. return 0;
  4823. raw_inode = ext4_raw_inode(&iloc);
  4824. header = IHDR(inode, raw_inode);
  4825. /* No extended attributes present */
  4826. if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR) ||
  4827. header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)) {
  4828. memset((void *)raw_inode + EXT4_GOOD_OLD_INODE_SIZE, 0,
  4829. new_extra_isize);
  4830. EXT4_I(inode)->i_extra_isize = new_extra_isize;
  4831. return 0;
  4832. }
  4833. /* try to expand with EAs present */
  4834. return ext4_expand_extra_isize_ea(inode, new_extra_isize,
  4835. raw_inode, handle);
  4836. }
  4837. /*
  4838. * What we do here is to mark the in-core inode as clean with respect to inode
  4839. * dirtiness (it may still be data-dirty).
  4840. * This means that the in-core inode may be reaped by prune_icache
  4841. * without having to perform any I/O. This is a very good thing,
  4842. * because *any* task may call prune_icache - even ones which
  4843. * have a transaction open against a different journal.
  4844. *
  4845. * Is this cheating? Not really. Sure, we haven't written the
  4846. * inode out, but prune_icache isn't a user-visible syncing function.
  4847. * Whenever the user wants stuff synced (sys_sync, sys_msync, sys_fsync)
  4848. * we start and wait on commits.
  4849. */
  4850. int ext4_mark_inode_dirty(handle_t *handle, struct inode *inode)
  4851. {
  4852. struct ext4_iloc iloc;
  4853. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  4854. static unsigned int mnt_count;
  4855. int err, ret;
  4856. might_sleep();
  4857. trace_ext4_mark_inode_dirty(inode, _RET_IP_);
  4858. err = ext4_reserve_inode_write(handle, inode, &iloc);
  4859. if (err)
  4860. return err;
  4861. if (ext4_handle_valid(handle) &&
  4862. EXT4_I(inode)->i_extra_isize < sbi->s_want_extra_isize &&
  4863. !ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND)) {
  4864. /*
  4865. * We need extra buffer credits since we may write into EA block
  4866. * with this same handle. If journal_extend fails, then it will
  4867. * only result in a minor loss of functionality for that inode.
  4868. * If this is felt to be critical, then e2fsck should be run to
  4869. * force a large enough s_min_extra_isize.
  4870. */
  4871. if ((jbd2_journal_extend(handle,
  4872. EXT4_DATA_TRANS_BLOCKS(inode->i_sb))) == 0) {
  4873. ret = ext4_expand_extra_isize(inode,
  4874. sbi->s_want_extra_isize,
  4875. iloc, handle);
  4876. if (ret) {
  4877. ext4_set_inode_state(inode,
  4878. EXT4_STATE_NO_EXPAND);
  4879. if (mnt_count !=
  4880. le16_to_cpu(sbi->s_es->s_mnt_count)) {
  4881. ext4_warning(inode->i_sb,
  4882. "Unable to expand inode %lu. Delete"
  4883. " some EAs or run e2fsck.",
  4884. inode->i_ino);
  4885. mnt_count =
  4886. le16_to_cpu(sbi->s_es->s_mnt_count);
  4887. }
  4888. }
  4889. }
  4890. }
  4891. return ext4_mark_iloc_dirty(handle, inode, &iloc);
  4892. }
  4893. /*
  4894. * ext4_dirty_inode() is called from __mark_inode_dirty()
  4895. *
  4896. * We're really interested in the case where a file is being extended.
  4897. * i_size has been changed by generic_commit_write() and we thus need
  4898. * to include the updated inode in the current transaction.
  4899. *
  4900. * Also, dquot_alloc_block() will always dirty the inode when blocks
  4901. * are allocated to the file.
  4902. *
  4903. * If the inode is marked synchronous, we don't honour that here - doing
  4904. * so would cause a commit on atime updates, which we don't bother doing.
  4905. * We handle synchronous inodes at the highest possible level.
  4906. *
  4907. * If only the I_DIRTY_TIME flag is set, we can skip everything. If
  4908. * I_DIRTY_TIME and I_DIRTY_SYNC is set, the only inode fields we need
  4909. * to copy into the on-disk inode structure are the timestamp files.
  4910. */
  4911. void ext4_dirty_inode(struct inode *inode, int flags)
  4912. {
  4913. handle_t *handle;
  4914. if (flags == I_DIRTY_TIME)
  4915. return;
  4916. handle = ext4_journal_start(inode, EXT4_HT_INODE, 2);
  4917. if (IS_ERR(handle))
  4918. goto out;
  4919. ext4_mark_inode_dirty(handle, inode);
  4920. ext4_journal_stop(handle);
  4921. out:
  4922. return;
  4923. }
  4924. #if 0
  4925. /*
  4926. * Bind an inode's backing buffer_head into this transaction, to prevent
  4927. * it from being flushed to disk early. Unlike
  4928. * ext4_reserve_inode_write, this leaves behind no bh reference and
  4929. * returns no iloc structure, so the caller needs to repeat the iloc
  4930. * lookup to mark the inode dirty later.
  4931. */
  4932. static int ext4_pin_inode(handle_t *handle, struct inode *inode)
  4933. {
  4934. struct ext4_iloc iloc;
  4935. int err = 0;
  4936. if (handle) {
  4937. err = ext4_get_inode_loc(inode, &iloc);
  4938. if (!err) {
  4939. BUFFER_TRACE(iloc.bh, "get_write_access");
  4940. err = jbd2_journal_get_write_access(handle, iloc.bh);
  4941. if (!err)
  4942. err = ext4_handle_dirty_metadata(handle,
  4943. NULL,
  4944. iloc.bh);
  4945. brelse(iloc.bh);
  4946. }
  4947. }
  4948. ext4_std_error(inode->i_sb, err);
  4949. return err;
  4950. }
  4951. #endif
  4952. int ext4_change_inode_journal_flag(struct inode *inode, int val)
  4953. {
  4954. journal_t *journal;
  4955. handle_t *handle;
  4956. int err;
  4957. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  4958. /*
  4959. * We have to be very careful here: changing a data block's
  4960. * journaling status dynamically is dangerous. If we write a
  4961. * data block to the journal, change the status and then delete
  4962. * that block, we risk forgetting to revoke the old log record
  4963. * from the journal and so a subsequent replay can corrupt data.
  4964. * So, first we make sure that the journal is empty and that
  4965. * nobody is changing anything.
  4966. */
  4967. journal = EXT4_JOURNAL(inode);
  4968. if (!journal)
  4969. return 0;
  4970. if (is_journal_aborted(journal))
  4971. return -EROFS;
  4972. /* Wait for all existing dio workers */
  4973. ext4_inode_block_unlocked_dio(inode);
  4974. inode_dio_wait(inode);
  4975. /*
  4976. * Before flushing the journal and switching inode's aops, we have
  4977. * to flush all dirty data the inode has. There can be outstanding
  4978. * delayed allocations, there can be unwritten extents created by
  4979. * fallocate or buffered writes in dioread_nolock mode covered by
  4980. * dirty data which can be converted only after flushing the dirty
  4981. * data (and journalled aops don't know how to handle these cases).
  4982. */
  4983. if (val) {
  4984. down_write(&EXT4_I(inode)->i_mmap_sem);
  4985. err = filemap_write_and_wait(inode->i_mapping);
  4986. if (err < 0) {
  4987. up_write(&EXT4_I(inode)->i_mmap_sem);
  4988. ext4_inode_resume_unlocked_dio(inode);
  4989. return err;
  4990. }
  4991. }
  4992. percpu_down_write(&sbi->s_journal_flag_rwsem);
  4993. jbd2_journal_lock_updates(journal);
  4994. /*
  4995. * OK, there are no updates running now, and all cached data is
  4996. * synced to disk. We are now in a completely consistent state
  4997. * which doesn't have anything in the journal, and we know that
  4998. * no filesystem updates are running, so it is safe to modify
  4999. * the inode's in-core data-journaling state flag now.
  5000. */
  5001. if (val)
  5002. ext4_set_inode_flag(inode, EXT4_INODE_JOURNAL_DATA);
  5003. else {
  5004. err = jbd2_journal_flush(journal);
  5005. if (err < 0) {
  5006. jbd2_journal_unlock_updates(journal);
  5007. percpu_up_write(&sbi->s_journal_flag_rwsem);
  5008. ext4_inode_resume_unlocked_dio(inode);
  5009. return err;
  5010. }
  5011. ext4_clear_inode_flag(inode, EXT4_INODE_JOURNAL_DATA);
  5012. }
  5013. ext4_set_aops(inode);
  5014. jbd2_journal_unlock_updates(journal);
  5015. percpu_up_write(&sbi->s_journal_flag_rwsem);
  5016. if (val)
  5017. up_write(&EXT4_I(inode)->i_mmap_sem);
  5018. ext4_inode_resume_unlocked_dio(inode);
  5019. /* Finally we can mark the inode as dirty. */
  5020. handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
  5021. if (IS_ERR(handle))
  5022. return PTR_ERR(handle);
  5023. err = ext4_mark_inode_dirty(handle, inode);
  5024. ext4_handle_sync(handle);
  5025. ext4_journal_stop(handle);
  5026. ext4_std_error(inode->i_sb, err);
  5027. return err;
  5028. }
  5029. static int ext4_bh_unmapped(handle_t *handle, struct buffer_head *bh)
  5030. {
  5031. return !buffer_mapped(bh);
  5032. }
  5033. int ext4_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
  5034. {
  5035. struct page *page = vmf->page;
  5036. loff_t size;
  5037. unsigned long len;
  5038. int ret;
  5039. struct file *file = vma->vm_file;
  5040. struct inode *inode = file_inode(file);
  5041. struct address_space *mapping = inode->i_mapping;
  5042. handle_t *handle;
  5043. get_block_t *get_block;
  5044. int retries = 0;
  5045. sb_start_pagefault(inode->i_sb);
  5046. file_update_time(vma->vm_file);
  5047. down_read(&EXT4_I(inode)->i_mmap_sem);
  5048. /* Delalloc case is easy... */
  5049. if (test_opt(inode->i_sb, DELALLOC) &&
  5050. !ext4_should_journal_data(inode) &&
  5051. !ext4_nonda_switch(inode->i_sb)) {
  5052. do {
  5053. ret = block_page_mkwrite(vma, vmf,
  5054. ext4_da_get_block_prep);
  5055. } while (ret == -ENOSPC &&
  5056. ext4_should_retry_alloc(inode->i_sb, &retries));
  5057. goto out_ret;
  5058. }
  5059. lock_page(page);
  5060. size = i_size_read(inode);
  5061. /* Page got truncated from under us? */
  5062. if (page->mapping != mapping || page_offset(page) > size) {
  5063. unlock_page(page);
  5064. ret = VM_FAULT_NOPAGE;
  5065. goto out;
  5066. }
  5067. if (page->index == size >> PAGE_SHIFT)
  5068. len = size & ~PAGE_MASK;
  5069. else
  5070. len = PAGE_SIZE;
  5071. /*
  5072. * Return if we have all the buffers mapped. This avoids the need to do
  5073. * journal_start/journal_stop which can block and take a long time
  5074. */
  5075. if (page_has_buffers(page)) {
  5076. if (!ext4_walk_page_buffers(NULL, page_buffers(page),
  5077. 0, len, NULL,
  5078. ext4_bh_unmapped)) {
  5079. /* Wait so that we don't change page under IO */
  5080. wait_for_stable_page(page);
  5081. ret = VM_FAULT_LOCKED;
  5082. goto out;
  5083. }
  5084. }
  5085. unlock_page(page);
  5086. /* OK, we need to fill the hole... */
  5087. if (ext4_should_dioread_nolock(inode))
  5088. get_block = ext4_get_block_unwritten;
  5089. else
  5090. get_block = ext4_get_block;
  5091. retry_alloc:
  5092. handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE,
  5093. ext4_writepage_trans_blocks(inode));
  5094. if (IS_ERR(handle)) {
  5095. ret = VM_FAULT_SIGBUS;
  5096. goto out;
  5097. }
  5098. ret = block_page_mkwrite(vma, vmf, get_block);
  5099. if (!ret && ext4_should_journal_data(inode)) {
  5100. if (ext4_walk_page_buffers(handle, page_buffers(page), 0,
  5101. PAGE_SIZE, NULL, do_journal_get_write_access)) {
  5102. unlock_page(page);
  5103. ret = VM_FAULT_SIGBUS;
  5104. ext4_journal_stop(handle);
  5105. goto out;
  5106. }
  5107. ext4_set_inode_state(inode, EXT4_STATE_JDATA);
  5108. }
  5109. ext4_journal_stop(handle);
  5110. if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
  5111. goto retry_alloc;
  5112. out_ret:
  5113. ret = block_page_mkwrite_return(ret);
  5114. out:
  5115. up_read(&EXT4_I(inode)->i_mmap_sem);
  5116. sb_end_pagefault(inode->i_sb);
  5117. return ret;
  5118. }
  5119. int ext4_filemap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  5120. {
  5121. struct inode *inode = file_inode(vma->vm_file);
  5122. int err;
  5123. down_read(&EXT4_I(inode)->i_mmap_sem);
  5124. err = filemap_fault(vma, vmf);
  5125. up_read(&EXT4_I(inode)->i_mmap_sem);
  5126. return err;
  5127. }
  5128. /*
  5129. * Find the first extent at or after @lblk in an inode that is not a hole.
  5130. * Search for @map_len blocks at most. The extent is returned in @result.
  5131. *
  5132. * The function returns 1 if we found an extent. The function returns 0 in
  5133. * case there is no extent at or after @lblk and in that case also sets
  5134. * @result->es_len to 0. In case of error, the error code is returned.
  5135. */
  5136. int ext4_get_next_extent(struct inode *inode, ext4_lblk_t lblk,
  5137. unsigned int map_len, struct extent_status *result)
  5138. {
  5139. struct ext4_map_blocks map;
  5140. struct extent_status es = {};
  5141. int ret;
  5142. map.m_lblk = lblk;
  5143. map.m_len = map_len;
  5144. /*
  5145. * For non-extent based files this loop may iterate several times since
  5146. * we do not determine full hole size.
  5147. */
  5148. while (map.m_len > 0) {
  5149. ret = ext4_map_blocks(NULL, inode, &map, 0);
  5150. if (ret < 0)
  5151. return ret;
  5152. /* There's extent covering m_lblk? Just return it. */
  5153. if (ret > 0) {
  5154. int status;
  5155. ext4_es_store_pblock(result, map.m_pblk);
  5156. result->es_lblk = map.m_lblk;
  5157. result->es_len = map.m_len;
  5158. if (map.m_flags & EXT4_MAP_UNWRITTEN)
  5159. status = EXTENT_STATUS_UNWRITTEN;
  5160. else
  5161. status = EXTENT_STATUS_WRITTEN;
  5162. ext4_es_store_status(result, status);
  5163. return 1;
  5164. }
  5165. ext4_es_find_delayed_extent_range(inode, map.m_lblk,
  5166. map.m_lblk + map.m_len - 1,
  5167. &es);
  5168. /* Is delalloc data before next block in extent tree? */
  5169. if (es.es_len && es.es_lblk < map.m_lblk + map.m_len) {
  5170. ext4_lblk_t offset = 0;
  5171. if (es.es_lblk < lblk)
  5172. offset = lblk - es.es_lblk;
  5173. result->es_lblk = es.es_lblk + offset;
  5174. ext4_es_store_pblock(result,
  5175. ext4_es_pblock(&es) + offset);
  5176. result->es_len = es.es_len - offset;
  5177. ext4_es_store_status(result, ext4_es_status(&es));
  5178. return 1;
  5179. }
  5180. /* There's a hole at m_lblk, advance us after it */
  5181. map.m_lblk += map.m_len;
  5182. map_len -= map.m_len;
  5183. map.m_len = map_len;
  5184. cond_resched();
  5185. }
  5186. result->es_len = 0;
  5187. return 0;
  5188. }