inode.c 171 KB

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