inode.c 168 KB

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