inode.c 151 KB

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