inode.c 150 KB

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