inode.c 167 KB

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