inode.c 178 KB

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