inode.c 168 KB

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