inode.c 151 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266
  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. struct ext4_inode_info *ei = EXT4_I(inode);
  1459. ext4_msg(sb, KERN_CRIT, "Total free blocks count %lld",
  1460. EXT4_C2B(EXT4_SB(inode->i_sb),
  1461. ext4_count_free_clusters(sb)));
  1462. ext4_msg(sb, KERN_CRIT, "Free/Dirty block details");
  1463. ext4_msg(sb, KERN_CRIT, "free_blocks=%lld",
  1464. (long long) EXT4_C2B(EXT4_SB(sb),
  1465. percpu_counter_sum(&sbi->s_freeclusters_counter)));
  1466. ext4_msg(sb, KERN_CRIT, "dirty_blocks=%lld",
  1467. (long long) EXT4_C2B(EXT4_SB(sb),
  1468. percpu_counter_sum(&sbi->s_dirtyclusters_counter)));
  1469. ext4_msg(sb, KERN_CRIT, "Block reservation details");
  1470. ext4_msg(sb, KERN_CRIT, "i_reserved_data_blocks=%u",
  1471. ei->i_reserved_data_blocks);
  1472. ext4_msg(sb, KERN_CRIT, "i_reserved_meta_blocks=%u",
  1473. ei->i_reserved_meta_blocks);
  1474. ext4_msg(sb, KERN_CRIT, "i_allocated_meta_blocks=%u",
  1475. ei->i_allocated_meta_blocks);
  1476. return;
  1477. }
  1478. /*
  1479. * mpage_da_map_and_submit - go through given space, map them
  1480. * if necessary, and then submit them for I/O
  1481. *
  1482. * @mpd - bh describing space
  1483. *
  1484. * The function skips space we know is already mapped to disk blocks.
  1485. *
  1486. */
  1487. static void mpage_da_map_and_submit(struct mpage_da_data *mpd)
  1488. {
  1489. int err, blks, get_blocks_flags;
  1490. struct ext4_map_blocks map, *mapp = NULL;
  1491. sector_t next = mpd->b_blocknr;
  1492. unsigned max_blocks = mpd->b_size >> mpd->inode->i_blkbits;
  1493. loff_t disksize = EXT4_I(mpd->inode)->i_disksize;
  1494. handle_t *handle = NULL;
  1495. /*
  1496. * If the blocks are mapped already, or we couldn't accumulate
  1497. * any blocks, then proceed immediately to the submission stage.
  1498. */
  1499. if ((mpd->b_size == 0) ||
  1500. ((mpd->b_state & (1 << BH_Mapped)) &&
  1501. !(mpd->b_state & (1 << BH_Delay)) &&
  1502. !(mpd->b_state & (1 << BH_Unwritten))))
  1503. goto submit_io;
  1504. handle = ext4_journal_current_handle();
  1505. BUG_ON(!handle);
  1506. /*
  1507. * Call ext4_map_blocks() to allocate any delayed allocation
  1508. * blocks, or to convert an uninitialized extent to be
  1509. * initialized (in the case where we have written into
  1510. * one or more preallocated blocks).
  1511. *
  1512. * We pass in the magic EXT4_GET_BLOCKS_DELALLOC_RESERVE to
  1513. * indicate that we are on the delayed allocation path. This
  1514. * affects functions in many different parts of the allocation
  1515. * call path. This flag exists primarily because we don't
  1516. * want to change *many* call functions, so ext4_map_blocks()
  1517. * will set the EXT4_STATE_DELALLOC_RESERVED flag once the
  1518. * inode's allocation semaphore is taken.
  1519. *
  1520. * If the blocks in questions were delalloc blocks, set
  1521. * EXT4_GET_BLOCKS_DELALLOC_RESERVE so the delalloc accounting
  1522. * variables are updated after the blocks have been allocated.
  1523. */
  1524. map.m_lblk = next;
  1525. map.m_len = max_blocks;
  1526. get_blocks_flags = EXT4_GET_BLOCKS_CREATE;
  1527. if (ext4_should_dioread_nolock(mpd->inode))
  1528. get_blocks_flags |= EXT4_GET_BLOCKS_IO_CREATE_EXT;
  1529. if (mpd->b_state & (1 << BH_Delay))
  1530. get_blocks_flags |= EXT4_GET_BLOCKS_DELALLOC_RESERVE;
  1531. blks = ext4_map_blocks(handle, mpd->inode, &map, get_blocks_flags);
  1532. if (blks < 0) {
  1533. struct super_block *sb = mpd->inode->i_sb;
  1534. err = blks;
  1535. /*
  1536. * If get block returns EAGAIN or ENOSPC and there
  1537. * appears to be free blocks we will just let
  1538. * mpage_da_submit_io() unlock all of the pages.
  1539. */
  1540. if (err == -EAGAIN)
  1541. goto submit_io;
  1542. if (err == -ENOSPC && ext4_count_free_clusters(sb)) {
  1543. mpd->retval = err;
  1544. goto submit_io;
  1545. }
  1546. /*
  1547. * get block failure will cause us to loop in
  1548. * writepages, because a_ops->writepage won't be able
  1549. * to make progress. The page will be redirtied by
  1550. * writepage and writepages will again try to write
  1551. * the same.
  1552. */
  1553. if (!(EXT4_SB(sb)->s_mount_flags & EXT4_MF_FS_ABORTED)) {
  1554. ext4_msg(sb, KERN_CRIT,
  1555. "delayed block allocation failed for inode %lu "
  1556. "at logical offset %llu with max blocks %zd "
  1557. "with error %d", mpd->inode->i_ino,
  1558. (unsigned long long) next,
  1559. mpd->b_size >> mpd->inode->i_blkbits, err);
  1560. ext4_msg(sb, KERN_CRIT,
  1561. "This should not happen!! Data will be lost");
  1562. if (err == -ENOSPC)
  1563. ext4_print_free_blocks(mpd->inode);
  1564. }
  1565. /* invalidate all the pages */
  1566. ext4_da_block_invalidatepages(mpd);
  1567. /* Mark this page range as having been completed */
  1568. mpd->io_done = 1;
  1569. return;
  1570. }
  1571. BUG_ON(blks == 0);
  1572. mapp = &map;
  1573. if (map.m_flags & EXT4_MAP_NEW) {
  1574. struct block_device *bdev = mpd->inode->i_sb->s_bdev;
  1575. int i;
  1576. for (i = 0; i < map.m_len; i++)
  1577. unmap_underlying_metadata(bdev, map.m_pblk + i);
  1578. }
  1579. /*
  1580. * Update on-disk size along with block allocation.
  1581. */
  1582. disksize = ((loff_t) next + blks) << mpd->inode->i_blkbits;
  1583. if (disksize > i_size_read(mpd->inode))
  1584. disksize = i_size_read(mpd->inode);
  1585. if (disksize > EXT4_I(mpd->inode)->i_disksize) {
  1586. ext4_update_i_disksize(mpd->inode, disksize);
  1587. err = ext4_mark_inode_dirty(handle, mpd->inode);
  1588. if (err)
  1589. ext4_error(mpd->inode->i_sb,
  1590. "Failed to mark inode %lu dirty",
  1591. mpd->inode->i_ino);
  1592. }
  1593. submit_io:
  1594. mpage_da_submit_io(mpd, mapp);
  1595. mpd->io_done = 1;
  1596. }
  1597. #define BH_FLAGS ((1 << BH_Uptodate) | (1 << BH_Mapped) | \
  1598. (1 << BH_Delay) | (1 << BH_Unwritten))
  1599. /*
  1600. * mpage_add_bh_to_extent - try to add one more block to extent of blocks
  1601. *
  1602. * @mpd->lbh - extent of blocks
  1603. * @logical - logical number of the block in the file
  1604. * @b_state - b_state of the buffer head added
  1605. *
  1606. * the function is used to collect contig. blocks in same state
  1607. */
  1608. static void mpage_add_bh_to_extent(struct mpage_da_data *mpd, sector_t logical,
  1609. unsigned long b_state)
  1610. {
  1611. sector_t next;
  1612. int blkbits = mpd->inode->i_blkbits;
  1613. int nrblocks = mpd->b_size >> blkbits;
  1614. /*
  1615. * XXX Don't go larger than mballoc is willing to allocate
  1616. * This is a stopgap solution. We eventually need to fold
  1617. * mpage_da_submit_io() into this function and then call
  1618. * ext4_map_blocks() multiple times in a loop
  1619. */
  1620. if (nrblocks >= (8*1024*1024 >> blkbits))
  1621. goto flush_it;
  1622. /* check if the reserved journal credits might overflow */
  1623. if (!ext4_test_inode_flag(mpd->inode, EXT4_INODE_EXTENTS)) {
  1624. if (nrblocks >= EXT4_MAX_TRANS_DATA) {
  1625. /*
  1626. * With non-extent format we are limited by the journal
  1627. * credit available. Total credit needed to insert
  1628. * nrblocks contiguous blocks is dependent on the
  1629. * nrblocks. So limit nrblocks.
  1630. */
  1631. goto flush_it;
  1632. }
  1633. }
  1634. /*
  1635. * First block in the extent
  1636. */
  1637. if (mpd->b_size == 0) {
  1638. mpd->b_blocknr = logical;
  1639. mpd->b_size = 1 << blkbits;
  1640. mpd->b_state = b_state & BH_FLAGS;
  1641. return;
  1642. }
  1643. next = mpd->b_blocknr + nrblocks;
  1644. /*
  1645. * Can we merge the block to our big extent?
  1646. */
  1647. if (logical == next && (b_state & BH_FLAGS) == mpd->b_state) {
  1648. mpd->b_size += 1 << blkbits;
  1649. return;
  1650. }
  1651. flush_it:
  1652. /*
  1653. * We couldn't merge the block to our extent, so we
  1654. * need to flush current extent and start new one
  1655. */
  1656. mpage_da_map_and_submit(mpd);
  1657. return;
  1658. }
  1659. static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh)
  1660. {
  1661. return (buffer_delay(bh) || buffer_unwritten(bh)) && buffer_dirty(bh);
  1662. }
  1663. /*
  1664. * This function is grabs code from the very beginning of
  1665. * ext4_map_blocks, but assumes that the caller is from delayed write
  1666. * time. This function looks up the requested blocks and sets the
  1667. * buffer delay bit under the protection of i_data_sem.
  1668. */
  1669. static int ext4_da_map_blocks(struct inode *inode, sector_t iblock,
  1670. struct ext4_map_blocks *map,
  1671. struct buffer_head *bh)
  1672. {
  1673. struct extent_status es;
  1674. int retval;
  1675. sector_t invalid_block = ~((sector_t) 0xffff);
  1676. #ifdef ES_AGGRESSIVE_TEST
  1677. struct ext4_map_blocks orig_map;
  1678. memcpy(&orig_map, map, sizeof(*map));
  1679. #endif
  1680. if (invalid_block < ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es))
  1681. invalid_block = ~0;
  1682. map->m_flags = 0;
  1683. ext_debug("ext4_da_map_blocks(): inode %lu, max_blocks %u,"
  1684. "logical block %lu\n", inode->i_ino, map->m_len,
  1685. (unsigned long) map->m_lblk);
  1686. /* Lookup extent status tree firstly */
  1687. if (ext4_es_lookup_extent(inode, iblock, &es)) {
  1688. if (ext4_es_is_hole(&es)) {
  1689. retval = 0;
  1690. down_read((&EXT4_I(inode)->i_data_sem));
  1691. goto add_delayed;
  1692. }
  1693. /*
  1694. * Delayed extent could be allocated by fallocate.
  1695. * So we need to check it.
  1696. */
  1697. if (ext4_es_is_delayed(&es) && !ext4_es_is_unwritten(&es)) {
  1698. map_bh(bh, inode->i_sb, invalid_block);
  1699. set_buffer_new(bh);
  1700. set_buffer_delay(bh);
  1701. return 0;
  1702. }
  1703. map->m_pblk = ext4_es_pblock(&es) + iblock - es.es_lblk;
  1704. retval = es.es_len - (iblock - es.es_lblk);
  1705. if (retval > map->m_len)
  1706. retval = map->m_len;
  1707. map->m_len = retval;
  1708. if (ext4_es_is_written(&es))
  1709. map->m_flags |= EXT4_MAP_MAPPED;
  1710. else if (ext4_es_is_unwritten(&es))
  1711. map->m_flags |= EXT4_MAP_UNWRITTEN;
  1712. else
  1713. BUG_ON(1);
  1714. #ifdef ES_AGGRESSIVE_TEST
  1715. ext4_map_blocks_es_recheck(NULL, inode, map, &orig_map, 0);
  1716. #endif
  1717. return retval;
  1718. }
  1719. /*
  1720. * Try to see if we can get the block without requesting a new
  1721. * file system block.
  1722. */
  1723. down_read((&EXT4_I(inode)->i_data_sem));
  1724. if (ext4_has_inline_data(inode)) {
  1725. /*
  1726. * We will soon create blocks for this page, and let
  1727. * us pretend as if the blocks aren't allocated yet.
  1728. * In case of clusters, we have to handle the work
  1729. * of mapping from cluster so that the reserved space
  1730. * is calculated properly.
  1731. */
  1732. if ((EXT4_SB(inode->i_sb)->s_cluster_ratio > 1) &&
  1733. ext4_find_delalloc_cluster(inode, map->m_lblk))
  1734. map->m_flags |= EXT4_MAP_FROM_CLUSTER;
  1735. retval = 0;
  1736. } else if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
  1737. retval = ext4_ext_map_blocks(NULL, inode, map,
  1738. EXT4_GET_BLOCKS_NO_PUT_HOLE);
  1739. else
  1740. retval = ext4_ind_map_blocks(NULL, inode, map,
  1741. EXT4_GET_BLOCKS_NO_PUT_HOLE);
  1742. add_delayed:
  1743. if (retval == 0) {
  1744. int ret;
  1745. /*
  1746. * XXX: __block_prepare_write() unmaps passed block,
  1747. * is it OK?
  1748. */
  1749. /*
  1750. * If the block was allocated from previously allocated cluster,
  1751. * then we don't need to reserve it again. However we still need
  1752. * to reserve metadata for every block we're going to write.
  1753. */
  1754. if (!(map->m_flags & EXT4_MAP_FROM_CLUSTER)) {
  1755. ret = ext4_da_reserve_space(inode, iblock);
  1756. if (ret) {
  1757. /* not enough space to reserve */
  1758. retval = ret;
  1759. goto out_unlock;
  1760. }
  1761. } else {
  1762. ret = ext4_da_reserve_metadata(inode, iblock);
  1763. if (ret) {
  1764. /* not enough space to reserve */
  1765. retval = ret;
  1766. goto out_unlock;
  1767. }
  1768. }
  1769. ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len,
  1770. ~0, EXTENT_STATUS_DELAYED);
  1771. if (ret) {
  1772. retval = ret;
  1773. goto out_unlock;
  1774. }
  1775. /* Clear EXT4_MAP_FROM_CLUSTER flag since its purpose is served
  1776. * and it should not appear on the bh->b_state.
  1777. */
  1778. map->m_flags &= ~EXT4_MAP_FROM_CLUSTER;
  1779. map_bh(bh, inode->i_sb, invalid_block);
  1780. set_buffer_new(bh);
  1781. set_buffer_delay(bh);
  1782. } else if (retval > 0) {
  1783. int ret;
  1784. unsigned long long status;
  1785. #ifdef ES_AGGRESSIVE_TEST
  1786. if (retval != map->m_len) {
  1787. printk("ES len assertation failed for inode: %lu "
  1788. "retval %d != map->m_len %d "
  1789. "in %s (lookup)\n", inode->i_ino, retval,
  1790. map->m_len, __func__);
  1791. }
  1792. #endif
  1793. status = map->m_flags & EXT4_MAP_UNWRITTEN ?
  1794. EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN;
  1795. ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len,
  1796. map->m_pblk, status);
  1797. if (ret != 0)
  1798. retval = ret;
  1799. }
  1800. out_unlock:
  1801. up_read((&EXT4_I(inode)->i_data_sem));
  1802. return retval;
  1803. }
  1804. /*
  1805. * This is a special get_blocks_t callback which is used by
  1806. * ext4_da_write_begin(). It will either return mapped block or
  1807. * reserve space for a single block.
  1808. *
  1809. * For delayed buffer_head we have BH_Mapped, BH_New, BH_Delay set.
  1810. * We also have b_blocknr = -1 and b_bdev initialized properly
  1811. *
  1812. * For unwritten buffer_head we have BH_Mapped, BH_New, BH_Unwritten set.
  1813. * We also have b_blocknr = physicalblock mapping unwritten extent and b_bdev
  1814. * initialized properly.
  1815. */
  1816. int ext4_da_get_block_prep(struct inode *inode, sector_t iblock,
  1817. struct buffer_head *bh, int create)
  1818. {
  1819. struct ext4_map_blocks map;
  1820. int ret = 0;
  1821. BUG_ON(create == 0);
  1822. BUG_ON(bh->b_size != inode->i_sb->s_blocksize);
  1823. map.m_lblk = iblock;
  1824. map.m_len = 1;
  1825. /*
  1826. * first, we need to know whether the block is allocated already
  1827. * preallocated blocks are unmapped but should treated
  1828. * the same as allocated blocks.
  1829. */
  1830. ret = ext4_da_map_blocks(inode, iblock, &map, bh);
  1831. if (ret <= 0)
  1832. return ret;
  1833. map_bh(bh, inode->i_sb, map.m_pblk);
  1834. bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | map.m_flags;
  1835. if (buffer_unwritten(bh)) {
  1836. /* A delayed write to unwritten bh should be marked
  1837. * new and mapped. Mapped ensures that we don't do
  1838. * get_block multiple times when we write to the same
  1839. * offset and new ensures that we do proper zero out
  1840. * for partial write.
  1841. */
  1842. set_buffer_new(bh);
  1843. set_buffer_mapped(bh);
  1844. }
  1845. return 0;
  1846. }
  1847. static int bget_one(handle_t *handle, struct buffer_head *bh)
  1848. {
  1849. get_bh(bh);
  1850. return 0;
  1851. }
  1852. static int bput_one(handle_t *handle, struct buffer_head *bh)
  1853. {
  1854. put_bh(bh);
  1855. return 0;
  1856. }
  1857. static int __ext4_journalled_writepage(struct page *page,
  1858. unsigned int len)
  1859. {
  1860. struct address_space *mapping = page->mapping;
  1861. struct inode *inode = mapping->host;
  1862. struct buffer_head *page_bufs = NULL;
  1863. handle_t *handle = NULL;
  1864. int ret = 0, err = 0;
  1865. int inline_data = ext4_has_inline_data(inode);
  1866. struct buffer_head *inode_bh = NULL;
  1867. ClearPageChecked(page);
  1868. if (inline_data) {
  1869. BUG_ON(page->index != 0);
  1870. BUG_ON(len > ext4_get_max_inline_size(inode));
  1871. inode_bh = ext4_journalled_write_inline_data(inode, len, page);
  1872. if (inode_bh == NULL)
  1873. goto out;
  1874. } else {
  1875. page_bufs = page_buffers(page);
  1876. if (!page_bufs) {
  1877. BUG();
  1878. goto out;
  1879. }
  1880. ext4_walk_page_buffers(handle, page_bufs, 0, len,
  1881. NULL, bget_one);
  1882. }
  1883. /* As soon as we unlock the page, it can go away, but we have
  1884. * references to buffers so we are safe */
  1885. unlock_page(page);
  1886. handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE,
  1887. ext4_writepage_trans_blocks(inode));
  1888. if (IS_ERR(handle)) {
  1889. ret = PTR_ERR(handle);
  1890. goto out;
  1891. }
  1892. BUG_ON(!ext4_handle_valid(handle));
  1893. if (inline_data) {
  1894. ret = ext4_journal_get_write_access(handle, inode_bh);
  1895. err = ext4_handle_dirty_metadata(handle, inode, inode_bh);
  1896. } else {
  1897. ret = ext4_walk_page_buffers(handle, page_bufs, 0, len, NULL,
  1898. do_journal_get_write_access);
  1899. err = ext4_walk_page_buffers(handle, page_bufs, 0, len, NULL,
  1900. write_end_fn);
  1901. }
  1902. if (ret == 0)
  1903. ret = err;
  1904. EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid;
  1905. err = ext4_journal_stop(handle);
  1906. if (!ret)
  1907. ret = err;
  1908. if (!ext4_has_inline_data(inode))
  1909. ext4_walk_page_buffers(handle, page_bufs, 0, len,
  1910. NULL, bput_one);
  1911. ext4_set_inode_state(inode, EXT4_STATE_JDATA);
  1912. out:
  1913. brelse(inode_bh);
  1914. return ret;
  1915. }
  1916. /*
  1917. * Note that we don't need to start a transaction unless we're journaling data
  1918. * because we should have holes filled from ext4_page_mkwrite(). We even don't
  1919. * need to file the inode to the transaction's list in ordered mode because if
  1920. * we are writing back data added by write(), the inode is already there and if
  1921. * we are writing back data modified via mmap(), no one guarantees in which
  1922. * transaction the data will hit the disk. In case we are journaling data, we
  1923. * cannot start transaction directly because transaction start ranks above page
  1924. * lock so we have to do some magic.
  1925. *
  1926. * This function can get called via...
  1927. * - ext4_da_writepages after taking page lock (have journal handle)
  1928. * - journal_submit_inode_data_buffers (no journal handle)
  1929. * - shrink_page_list via the kswapd/direct reclaim (no journal handle)
  1930. * - grab_page_cache when doing write_begin (have journal handle)
  1931. *
  1932. * We don't do any block allocation in this function. If we have page with
  1933. * multiple blocks we need to write those buffer_heads that are mapped. This
  1934. * is important for mmaped based write. So if we do with blocksize 1K
  1935. * truncate(f, 1024);
  1936. * a = mmap(f, 0, 4096);
  1937. * a[0] = 'a';
  1938. * truncate(f, 4096);
  1939. * we have in the page first buffer_head mapped via page_mkwrite call back
  1940. * but other buffer_heads would be unmapped but dirty (dirty done via the
  1941. * do_wp_page). So writepage should write the first block. If we modify
  1942. * the mmap area beyond 1024 we will again get a page_fault and the
  1943. * page_mkwrite callback will do the block allocation and mark the
  1944. * buffer_heads mapped.
  1945. *
  1946. * We redirty the page if we have any buffer_heads that is either delay or
  1947. * unwritten in the page.
  1948. *
  1949. * We can get recursively called as show below.
  1950. *
  1951. * ext4_writepage() -> kmalloc() -> __alloc_pages() -> page_launder() ->
  1952. * ext4_writepage()
  1953. *
  1954. * But since we don't do any block allocation we should not deadlock.
  1955. * Page also have the dirty flag cleared so we don't get recurive page_lock.
  1956. */
  1957. static int ext4_writepage(struct page *page,
  1958. struct writeback_control *wbc)
  1959. {
  1960. int ret = 0;
  1961. loff_t size;
  1962. unsigned int len;
  1963. struct buffer_head *page_bufs = NULL;
  1964. struct inode *inode = page->mapping->host;
  1965. struct ext4_io_submit io_submit;
  1966. trace_ext4_writepage(page);
  1967. size = i_size_read(inode);
  1968. if (page->index == size >> PAGE_CACHE_SHIFT)
  1969. len = size & ~PAGE_CACHE_MASK;
  1970. else
  1971. len = PAGE_CACHE_SIZE;
  1972. page_bufs = page_buffers(page);
  1973. /*
  1974. * We cannot do block allocation or other extent handling in this
  1975. * function. If there are buffers needing that, we have to redirty
  1976. * the page. But we may reach here when we do a journal commit via
  1977. * journal_submit_inode_data_buffers() and in that case we must write
  1978. * allocated buffers to achieve data=ordered mode guarantees.
  1979. */
  1980. if (ext4_walk_page_buffers(NULL, page_bufs, 0, len, NULL,
  1981. ext4_bh_delay_or_unwritten)) {
  1982. redirty_page_for_writepage(wbc, page);
  1983. if (current->flags & PF_MEMALLOC) {
  1984. /*
  1985. * For memory cleaning there's no point in writing only
  1986. * some buffers. So just bail out. Warn if we came here
  1987. * from direct reclaim.
  1988. */
  1989. WARN_ON_ONCE((current->flags & (PF_MEMALLOC|PF_KSWAPD))
  1990. == PF_MEMALLOC);
  1991. unlock_page(page);
  1992. return 0;
  1993. }
  1994. }
  1995. if (PageChecked(page) && ext4_should_journal_data(inode))
  1996. /*
  1997. * It's mmapped pagecache. Add buffers and journal it. There
  1998. * doesn't seem much point in redirtying the page here.
  1999. */
  2000. return __ext4_journalled_writepage(page, len);
  2001. memset(&io_submit, 0, sizeof(io_submit));
  2002. ret = ext4_bio_write_page(&io_submit, page, len, wbc);
  2003. ext4_io_submit(&io_submit);
  2004. return ret;
  2005. }
  2006. /*
  2007. * This is called via ext4_da_writepages() to
  2008. * calculate the total number of credits to reserve to fit
  2009. * a single extent allocation into a single transaction,
  2010. * ext4_da_writpeages() will loop calling this before
  2011. * the block allocation.
  2012. */
  2013. static int ext4_da_writepages_trans_blocks(struct inode *inode)
  2014. {
  2015. int max_blocks = EXT4_I(inode)->i_reserved_data_blocks;
  2016. /*
  2017. * With non-extent format the journal credit needed to
  2018. * insert nrblocks contiguous block is dependent on
  2019. * number of contiguous block. So we will limit
  2020. * number of contiguous block to a sane value
  2021. */
  2022. if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) &&
  2023. (max_blocks > EXT4_MAX_TRANS_DATA))
  2024. max_blocks = EXT4_MAX_TRANS_DATA;
  2025. return ext4_chunk_trans_blocks(inode, max_blocks);
  2026. }
  2027. /*
  2028. * write_cache_pages_da - walk the list of dirty pages of the given
  2029. * address space and accumulate pages that need writing, and call
  2030. * mpage_da_map_and_submit to map a single contiguous memory region
  2031. * and then write them.
  2032. */
  2033. static int write_cache_pages_da(handle_t *handle,
  2034. struct address_space *mapping,
  2035. struct writeback_control *wbc,
  2036. struct mpage_da_data *mpd,
  2037. pgoff_t *done_index)
  2038. {
  2039. struct buffer_head *bh, *head;
  2040. struct inode *inode = mapping->host;
  2041. struct pagevec pvec;
  2042. unsigned int nr_pages;
  2043. sector_t logical;
  2044. pgoff_t index, end;
  2045. long nr_to_write = wbc->nr_to_write;
  2046. int i, tag, ret = 0;
  2047. memset(mpd, 0, sizeof(struct mpage_da_data));
  2048. mpd->wbc = wbc;
  2049. mpd->inode = inode;
  2050. pagevec_init(&pvec, 0);
  2051. index = wbc->range_start >> PAGE_CACHE_SHIFT;
  2052. end = wbc->range_end >> PAGE_CACHE_SHIFT;
  2053. if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
  2054. tag = PAGECACHE_TAG_TOWRITE;
  2055. else
  2056. tag = PAGECACHE_TAG_DIRTY;
  2057. *done_index = index;
  2058. while (index <= end) {
  2059. nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
  2060. min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
  2061. if (nr_pages == 0)
  2062. return 0;
  2063. for (i = 0; i < nr_pages; i++) {
  2064. struct page *page = pvec.pages[i];
  2065. /*
  2066. * At this point, the page may be truncated or
  2067. * invalidated (changing page->mapping to NULL), or
  2068. * even swizzled back from swapper_space to tmpfs file
  2069. * mapping. However, page->index will not change
  2070. * because we have a reference on the page.
  2071. */
  2072. if (page->index > end)
  2073. goto out;
  2074. *done_index = page->index + 1;
  2075. /*
  2076. * If we can't merge this page, and we have
  2077. * accumulated an contiguous region, write it
  2078. */
  2079. if ((mpd->next_page != page->index) &&
  2080. (mpd->next_page != mpd->first_page)) {
  2081. mpage_da_map_and_submit(mpd);
  2082. goto ret_extent_tail;
  2083. }
  2084. lock_page(page);
  2085. /*
  2086. * If the page is no longer dirty, or its
  2087. * mapping no longer corresponds to inode we
  2088. * are writing (which means it has been
  2089. * truncated or invalidated), or the page is
  2090. * already under writeback and we are not
  2091. * doing a data integrity writeback, skip the page
  2092. */
  2093. if (!PageDirty(page) ||
  2094. (PageWriteback(page) &&
  2095. (wbc->sync_mode == WB_SYNC_NONE)) ||
  2096. unlikely(page->mapping != mapping)) {
  2097. unlock_page(page);
  2098. continue;
  2099. }
  2100. wait_on_page_writeback(page);
  2101. BUG_ON(PageWriteback(page));
  2102. /*
  2103. * If we have inline data and arrive here, it means that
  2104. * we will soon create the block for the 1st page, so
  2105. * we'd better clear the inline data here.
  2106. */
  2107. if (ext4_has_inline_data(inode)) {
  2108. BUG_ON(ext4_test_inode_state(inode,
  2109. EXT4_STATE_MAY_INLINE_DATA));
  2110. ext4_destroy_inline_data(handle, inode);
  2111. }
  2112. if (mpd->next_page != page->index)
  2113. mpd->first_page = page->index;
  2114. mpd->next_page = page->index + 1;
  2115. logical = (sector_t) page->index <<
  2116. (PAGE_CACHE_SHIFT - inode->i_blkbits);
  2117. /* Add all dirty buffers to mpd */
  2118. head = page_buffers(page);
  2119. bh = head;
  2120. do {
  2121. BUG_ON(buffer_locked(bh));
  2122. /*
  2123. * We need to try to allocate unmapped blocks
  2124. * in the same page. Otherwise we won't make
  2125. * progress with the page in ext4_writepage
  2126. */
  2127. if (ext4_bh_delay_or_unwritten(NULL, bh)) {
  2128. mpage_add_bh_to_extent(mpd, logical,
  2129. bh->b_state);
  2130. if (mpd->io_done)
  2131. goto ret_extent_tail;
  2132. } else if (buffer_dirty(bh) &&
  2133. buffer_mapped(bh)) {
  2134. /*
  2135. * mapped dirty buffer. We need to
  2136. * update the b_state because we look
  2137. * at b_state in mpage_da_map_blocks.
  2138. * We don't update b_size because if we
  2139. * find an unmapped buffer_head later
  2140. * we need to use the b_state flag of
  2141. * that buffer_head.
  2142. */
  2143. if (mpd->b_size == 0)
  2144. mpd->b_state =
  2145. bh->b_state & BH_FLAGS;
  2146. }
  2147. logical++;
  2148. } while ((bh = bh->b_this_page) != head);
  2149. if (nr_to_write > 0) {
  2150. nr_to_write--;
  2151. if (nr_to_write == 0 &&
  2152. wbc->sync_mode == WB_SYNC_NONE)
  2153. /*
  2154. * We stop writing back only if we are
  2155. * not doing integrity sync. In case of
  2156. * integrity sync we have to keep going
  2157. * because someone may be concurrently
  2158. * dirtying pages, and we might have
  2159. * synced a lot of newly appeared dirty
  2160. * pages, but have not synced all of the
  2161. * old dirty pages.
  2162. */
  2163. goto out;
  2164. }
  2165. }
  2166. pagevec_release(&pvec);
  2167. cond_resched();
  2168. }
  2169. return 0;
  2170. ret_extent_tail:
  2171. ret = MPAGE_DA_EXTENT_TAIL;
  2172. out:
  2173. pagevec_release(&pvec);
  2174. cond_resched();
  2175. return ret;
  2176. }
  2177. static int ext4_da_writepages(struct address_space *mapping,
  2178. struct writeback_control *wbc)
  2179. {
  2180. pgoff_t index;
  2181. int range_whole = 0;
  2182. handle_t *handle = NULL;
  2183. struct mpage_da_data mpd;
  2184. struct inode *inode = mapping->host;
  2185. int pages_written = 0;
  2186. unsigned int max_pages;
  2187. int range_cyclic, cycled = 1, io_done = 0;
  2188. int needed_blocks, ret = 0;
  2189. long desired_nr_to_write, nr_to_writebump = 0;
  2190. loff_t range_start = wbc->range_start;
  2191. struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb);
  2192. pgoff_t done_index = 0;
  2193. pgoff_t end;
  2194. struct blk_plug plug;
  2195. trace_ext4_da_writepages(inode, wbc);
  2196. /*
  2197. * No pages to write? This is mainly a kludge to avoid starting
  2198. * a transaction for special inodes like journal inode on last iput()
  2199. * because that could violate lock ordering on umount
  2200. */
  2201. if (!mapping->nrpages || !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
  2202. return 0;
  2203. /*
  2204. * If the filesystem has aborted, it is read-only, so return
  2205. * right away instead of dumping stack traces later on that
  2206. * will obscure the real source of the problem. We test
  2207. * EXT4_MF_FS_ABORTED instead of sb->s_flag's MS_RDONLY because
  2208. * the latter could be true if the filesystem is mounted
  2209. * read-only, and in that case, ext4_da_writepages should
  2210. * *never* be called, so if that ever happens, we would want
  2211. * the stack trace.
  2212. */
  2213. if (unlikely(sbi->s_mount_flags & EXT4_MF_FS_ABORTED))
  2214. return -EROFS;
  2215. if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
  2216. range_whole = 1;
  2217. range_cyclic = wbc->range_cyclic;
  2218. if (wbc->range_cyclic) {
  2219. index = mapping->writeback_index;
  2220. if (index)
  2221. cycled = 0;
  2222. wbc->range_start = index << PAGE_CACHE_SHIFT;
  2223. wbc->range_end = LLONG_MAX;
  2224. wbc->range_cyclic = 0;
  2225. end = -1;
  2226. } else {
  2227. index = wbc->range_start >> PAGE_CACHE_SHIFT;
  2228. end = wbc->range_end >> PAGE_CACHE_SHIFT;
  2229. }
  2230. /*
  2231. * This works around two forms of stupidity. The first is in
  2232. * the writeback code, which caps the maximum number of pages
  2233. * written to be 1024 pages. This is wrong on multiple
  2234. * levels; different architectues have a different page size,
  2235. * which changes the maximum amount of data which gets
  2236. * written. Secondly, 4 megabytes is way too small. XFS
  2237. * forces this value to be 16 megabytes by multiplying
  2238. * nr_to_write parameter by four, and then relies on its
  2239. * allocator to allocate larger extents to make them
  2240. * contiguous. Unfortunately this brings us to the second
  2241. * stupidity, which is that ext4's mballoc code only allocates
  2242. * at most 2048 blocks. So we force contiguous writes up to
  2243. * the number of dirty blocks in the inode, or
  2244. * sbi->max_writeback_mb_bump whichever is smaller.
  2245. */
  2246. max_pages = sbi->s_max_writeback_mb_bump << (20 - PAGE_CACHE_SHIFT);
  2247. if (!range_cyclic && range_whole) {
  2248. if (wbc->nr_to_write == LONG_MAX)
  2249. desired_nr_to_write = wbc->nr_to_write;
  2250. else
  2251. desired_nr_to_write = wbc->nr_to_write * 8;
  2252. } else
  2253. desired_nr_to_write = ext4_num_dirty_pages(inode, index,
  2254. max_pages);
  2255. if (desired_nr_to_write > max_pages)
  2256. desired_nr_to_write = max_pages;
  2257. if (wbc->nr_to_write < desired_nr_to_write) {
  2258. nr_to_writebump = desired_nr_to_write - wbc->nr_to_write;
  2259. wbc->nr_to_write = desired_nr_to_write;
  2260. }
  2261. retry:
  2262. if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
  2263. tag_pages_for_writeback(mapping, index, end);
  2264. blk_start_plug(&plug);
  2265. while (!ret && wbc->nr_to_write > 0) {
  2266. /*
  2267. * we insert one extent at a time. So we need
  2268. * credit needed for single extent allocation.
  2269. * journalled mode is currently not supported
  2270. * by delalloc
  2271. */
  2272. BUG_ON(ext4_should_journal_data(inode));
  2273. needed_blocks = ext4_da_writepages_trans_blocks(inode);
  2274. /* start a new transaction*/
  2275. handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE,
  2276. needed_blocks);
  2277. if (IS_ERR(handle)) {
  2278. ret = PTR_ERR(handle);
  2279. ext4_msg(inode->i_sb, KERN_CRIT, "%s: jbd2_start: "
  2280. "%ld pages, ino %lu; err %d", __func__,
  2281. wbc->nr_to_write, inode->i_ino, ret);
  2282. blk_finish_plug(&plug);
  2283. goto out_writepages;
  2284. }
  2285. /*
  2286. * Now call write_cache_pages_da() to find the next
  2287. * contiguous region of logical blocks that need
  2288. * blocks to be allocated by ext4 and submit them.
  2289. */
  2290. ret = write_cache_pages_da(handle, mapping,
  2291. wbc, &mpd, &done_index);
  2292. /*
  2293. * If we have a contiguous extent of pages and we
  2294. * haven't done the I/O yet, map the blocks and submit
  2295. * them for I/O.
  2296. */
  2297. if (!mpd.io_done && mpd.next_page != mpd.first_page) {
  2298. mpage_da_map_and_submit(&mpd);
  2299. ret = MPAGE_DA_EXTENT_TAIL;
  2300. }
  2301. trace_ext4_da_write_pages(inode, &mpd);
  2302. wbc->nr_to_write -= mpd.pages_written;
  2303. ext4_journal_stop(handle);
  2304. if ((mpd.retval == -ENOSPC) && sbi->s_journal) {
  2305. /* commit the transaction which would
  2306. * free blocks released in the transaction
  2307. * and try again
  2308. */
  2309. jbd2_journal_force_commit_nested(sbi->s_journal);
  2310. ret = 0;
  2311. } else if (ret == MPAGE_DA_EXTENT_TAIL) {
  2312. /*
  2313. * Got one extent now try with rest of the pages.
  2314. * If mpd.retval is set -EIO, journal is aborted.
  2315. * So we don't need to write any more.
  2316. */
  2317. pages_written += mpd.pages_written;
  2318. ret = mpd.retval;
  2319. io_done = 1;
  2320. } else if (wbc->nr_to_write)
  2321. /*
  2322. * There is no more writeout needed
  2323. * or we requested for a noblocking writeout
  2324. * and we found the device congested
  2325. */
  2326. break;
  2327. }
  2328. blk_finish_plug(&plug);
  2329. if (!io_done && !cycled) {
  2330. cycled = 1;
  2331. index = 0;
  2332. wbc->range_start = index << PAGE_CACHE_SHIFT;
  2333. wbc->range_end = mapping->writeback_index - 1;
  2334. goto retry;
  2335. }
  2336. /* Update index */
  2337. wbc->range_cyclic = range_cyclic;
  2338. if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
  2339. /*
  2340. * set the writeback_index so that range_cyclic
  2341. * mode will write it back later
  2342. */
  2343. mapping->writeback_index = done_index;
  2344. out_writepages:
  2345. wbc->nr_to_write -= nr_to_writebump;
  2346. wbc->range_start = range_start;
  2347. trace_ext4_da_writepages_result(inode, wbc, ret, pages_written);
  2348. return ret;
  2349. }
  2350. static int ext4_nonda_switch(struct super_block *sb)
  2351. {
  2352. s64 free_blocks, dirty_blocks;
  2353. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2354. /*
  2355. * switch to non delalloc mode if we are running low
  2356. * on free block. The free block accounting via percpu
  2357. * counters can get slightly wrong with percpu_counter_batch getting
  2358. * accumulated on each CPU without updating global counters
  2359. * Delalloc need an accurate free block accounting. So switch
  2360. * to non delalloc when we are near to error range.
  2361. */
  2362. free_blocks = EXT4_C2B(sbi,
  2363. percpu_counter_read_positive(&sbi->s_freeclusters_counter));
  2364. dirty_blocks = percpu_counter_read_positive(&sbi->s_dirtyclusters_counter);
  2365. /*
  2366. * Start pushing delalloc when 1/2 of free blocks are dirty.
  2367. */
  2368. if (dirty_blocks && (free_blocks < 2 * dirty_blocks))
  2369. try_to_writeback_inodes_sb(sb, WB_REASON_FS_FREE_SPACE);
  2370. if (2 * free_blocks < 3 * dirty_blocks ||
  2371. free_blocks < (dirty_blocks + EXT4_FREECLUSTERS_WATERMARK)) {
  2372. /*
  2373. * free block count is less than 150% of dirty blocks
  2374. * or free blocks is less than watermark
  2375. */
  2376. return 1;
  2377. }
  2378. return 0;
  2379. }
  2380. static int ext4_da_write_begin(struct file *file, struct address_space *mapping,
  2381. loff_t pos, unsigned len, unsigned flags,
  2382. struct page **pagep, void **fsdata)
  2383. {
  2384. int ret, retries = 0;
  2385. struct page *page;
  2386. pgoff_t index;
  2387. struct inode *inode = mapping->host;
  2388. handle_t *handle;
  2389. index = pos >> PAGE_CACHE_SHIFT;
  2390. if (ext4_nonda_switch(inode->i_sb)) {
  2391. *fsdata = (void *)FALL_BACK_TO_NONDELALLOC;
  2392. return ext4_write_begin(file, mapping, pos,
  2393. len, flags, pagep, fsdata);
  2394. }
  2395. *fsdata = (void *)0;
  2396. trace_ext4_da_write_begin(inode, pos, len, flags);
  2397. if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
  2398. ret = ext4_da_write_inline_data_begin(mapping, inode,
  2399. pos, len, flags,
  2400. pagep, fsdata);
  2401. if (ret < 0)
  2402. return ret;
  2403. if (ret == 1)
  2404. return 0;
  2405. }
  2406. /*
  2407. * grab_cache_page_write_begin() can take a long time if the
  2408. * system is thrashing due to memory pressure, or if the page
  2409. * is being written back. So grab it first before we start
  2410. * the transaction handle. This also allows us to allocate
  2411. * the page (if needed) without using GFP_NOFS.
  2412. */
  2413. retry_grab:
  2414. page = grab_cache_page_write_begin(mapping, index, flags);
  2415. if (!page)
  2416. return -ENOMEM;
  2417. unlock_page(page);
  2418. /*
  2419. * With delayed allocation, we don't log the i_disksize update
  2420. * if there is delayed block allocation. But we still need
  2421. * to journalling the i_disksize update if writes to the end
  2422. * of file which has an already mapped buffer.
  2423. */
  2424. retry_journal:
  2425. handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, 1);
  2426. if (IS_ERR(handle)) {
  2427. page_cache_release(page);
  2428. return PTR_ERR(handle);
  2429. }
  2430. lock_page(page);
  2431. if (page->mapping != mapping) {
  2432. /* The page got truncated from under us */
  2433. unlock_page(page);
  2434. page_cache_release(page);
  2435. ext4_journal_stop(handle);
  2436. goto retry_grab;
  2437. }
  2438. /* In case writeback began while the page was unlocked */
  2439. wait_on_page_writeback(page);
  2440. ret = __block_write_begin(page, pos, len, ext4_da_get_block_prep);
  2441. if (ret < 0) {
  2442. unlock_page(page);
  2443. ext4_journal_stop(handle);
  2444. /*
  2445. * block_write_begin may have instantiated a few blocks
  2446. * outside i_size. Trim these off again. Don't need
  2447. * i_size_read because we hold i_mutex.
  2448. */
  2449. if (pos + len > inode->i_size)
  2450. ext4_truncate_failed_write(inode);
  2451. if (ret == -ENOSPC &&
  2452. ext4_should_retry_alloc(inode->i_sb, &retries))
  2453. goto retry_journal;
  2454. page_cache_release(page);
  2455. return ret;
  2456. }
  2457. *pagep = page;
  2458. return ret;
  2459. }
  2460. /*
  2461. * Check if we should update i_disksize
  2462. * when write to the end of file but not require block allocation
  2463. */
  2464. static int ext4_da_should_update_i_disksize(struct page *page,
  2465. unsigned long offset)
  2466. {
  2467. struct buffer_head *bh;
  2468. struct inode *inode = page->mapping->host;
  2469. unsigned int idx;
  2470. int i;
  2471. bh = page_buffers(page);
  2472. idx = offset >> inode->i_blkbits;
  2473. for (i = 0; i < idx; i++)
  2474. bh = bh->b_this_page;
  2475. if (!buffer_mapped(bh) || (buffer_delay(bh)) || buffer_unwritten(bh))
  2476. return 0;
  2477. return 1;
  2478. }
  2479. static int ext4_da_write_end(struct file *file,
  2480. struct address_space *mapping,
  2481. loff_t pos, unsigned len, unsigned copied,
  2482. struct page *page, void *fsdata)
  2483. {
  2484. struct inode *inode = mapping->host;
  2485. int ret = 0, ret2;
  2486. handle_t *handle = ext4_journal_current_handle();
  2487. loff_t new_i_size;
  2488. unsigned long start, end;
  2489. int write_mode = (int)(unsigned long)fsdata;
  2490. if (write_mode == FALL_BACK_TO_NONDELALLOC)
  2491. return ext4_write_end(file, mapping, pos,
  2492. len, copied, page, fsdata);
  2493. trace_ext4_da_write_end(inode, pos, len, copied);
  2494. start = pos & (PAGE_CACHE_SIZE - 1);
  2495. end = start + copied - 1;
  2496. /*
  2497. * generic_write_end() will run mark_inode_dirty() if i_size
  2498. * changes. So let's piggyback the i_disksize mark_inode_dirty
  2499. * into that.
  2500. */
  2501. new_i_size = pos + copied;
  2502. if (copied && new_i_size > EXT4_I(inode)->i_disksize) {
  2503. if (ext4_has_inline_data(inode) ||
  2504. ext4_da_should_update_i_disksize(page, end)) {
  2505. down_write(&EXT4_I(inode)->i_data_sem);
  2506. if (new_i_size > EXT4_I(inode)->i_disksize)
  2507. EXT4_I(inode)->i_disksize = new_i_size;
  2508. up_write(&EXT4_I(inode)->i_data_sem);
  2509. /* We need to mark inode dirty even if
  2510. * new_i_size is less that inode->i_size
  2511. * bu greater than i_disksize.(hint delalloc)
  2512. */
  2513. ext4_mark_inode_dirty(handle, inode);
  2514. }
  2515. }
  2516. if (write_mode != CONVERT_INLINE_DATA &&
  2517. ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA) &&
  2518. ext4_has_inline_data(inode))
  2519. ret2 = ext4_da_write_inline_data_end(inode, pos, len, copied,
  2520. page);
  2521. else
  2522. ret2 = generic_write_end(file, mapping, pos, len, copied,
  2523. page, fsdata);
  2524. copied = ret2;
  2525. if (ret2 < 0)
  2526. ret = ret2;
  2527. ret2 = ext4_journal_stop(handle);
  2528. if (!ret)
  2529. ret = ret2;
  2530. return ret ? ret : copied;
  2531. }
  2532. static void ext4_da_invalidatepage(struct page *page, unsigned long offset)
  2533. {
  2534. /*
  2535. * Drop reserved blocks
  2536. */
  2537. BUG_ON(!PageLocked(page));
  2538. if (!page_has_buffers(page))
  2539. goto out;
  2540. ext4_da_page_release_reservation(page, offset);
  2541. out:
  2542. ext4_invalidatepage(page, offset);
  2543. return;
  2544. }
  2545. /*
  2546. * Force all delayed allocation blocks to be allocated for a given inode.
  2547. */
  2548. int ext4_alloc_da_blocks(struct inode *inode)
  2549. {
  2550. trace_ext4_alloc_da_blocks(inode);
  2551. if (!EXT4_I(inode)->i_reserved_data_blocks &&
  2552. !EXT4_I(inode)->i_reserved_meta_blocks)
  2553. return 0;
  2554. /*
  2555. * We do something simple for now. The filemap_flush() will
  2556. * also start triggering a write of the data blocks, which is
  2557. * not strictly speaking necessary (and for users of
  2558. * laptop_mode, not even desirable). However, to do otherwise
  2559. * would require replicating code paths in:
  2560. *
  2561. * ext4_da_writepages() ->
  2562. * write_cache_pages() ---> (via passed in callback function)
  2563. * __mpage_da_writepage() -->
  2564. * mpage_add_bh_to_extent()
  2565. * mpage_da_map_blocks()
  2566. *
  2567. * The problem is that write_cache_pages(), located in
  2568. * mm/page-writeback.c, marks pages clean in preparation for
  2569. * doing I/O, which is not desirable if we're not planning on
  2570. * doing I/O at all.
  2571. *
  2572. * We could call write_cache_pages(), and then redirty all of
  2573. * the pages by calling redirty_page_for_writepage() but that
  2574. * would be ugly in the extreme. So instead we would need to
  2575. * replicate parts of the code in the above functions,
  2576. * simplifying them because we wouldn't actually intend to
  2577. * write out the pages, but rather only collect contiguous
  2578. * logical block extents, call the multi-block allocator, and
  2579. * then update the buffer heads with the block allocations.
  2580. *
  2581. * For now, though, we'll cheat by calling filemap_flush(),
  2582. * which will map the blocks, and start the I/O, but not
  2583. * actually wait for the I/O to complete.
  2584. */
  2585. return filemap_flush(inode->i_mapping);
  2586. }
  2587. /*
  2588. * bmap() is special. It gets used by applications such as lilo and by
  2589. * the swapper to find the on-disk block of a specific piece of data.
  2590. *
  2591. * Naturally, this is dangerous if the block concerned is still in the
  2592. * journal. If somebody makes a swapfile on an ext4 data-journaling
  2593. * filesystem and enables swap, then they may get a nasty shock when the
  2594. * data getting swapped to that swapfile suddenly gets overwritten by
  2595. * the original zero's written out previously to the journal and
  2596. * awaiting writeback in the kernel's buffer cache.
  2597. *
  2598. * So, if we see any bmap calls here on a modified, data-journaled file,
  2599. * take extra steps to flush any blocks which might be in the cache.
  2600. */
  2601. static sector_t ext4_bmap(struct address_space *mapping, sector_t block)
  2602. {
  2603. struct inode *inode = mapping->host;
  2604. journal_t *journal;
  2605. int err;
  2606. /*
  2607. * We can get here for an inline file via the FIBMAP ioctl
  2608. */
  2609. if (ext4_has_inline_data(inode))
  2610. return 0;
  2611. if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) &&
  2612. test_opt(inode->i_sb, DELALLOC)) {
  2613. /*
  2614. * With delalloc we want to sync the file
  2615. * so that we can make sure we allocate
  2616. * blocks for file
  2617. */
  2618. filemap_write_and_wait(mapping);
  2619. }
  2620. if (EXT4_JOURNAL(inode) &&
  2621. ext4_test_inode_state(inode, EXT4_STATE_JDATA)) {
  2622. /*
  2623. * This is a REALLY heavyweight approach, but the use of
  2624. * bmap on dirty files is expected to be extremely rare:
  2625. * only if we run lilo or swapon on a freshly made file
  2626. * do we expect this to happen.
  2627. *
  2628. * (bmap requires CAP_SYS_RAWIO so this does not
  2629. * represent an unprivileged user DOS attack --- we'd be
  2630. * in trouble if mortal users could trigger this path at
  2631. * will.)
  2632. *
  2633. * NB. EXT4_STATE_JDATA is not set on files other than
  2634. * regular files. If somebody wants to bmap a directory
  2635. * or symlink and gets confused because the buffer
  2636. * hasn't yet been flushed to disk, they deserve
  2637. * everything they get.
  2638. */
  2639. ext4_clear_inode_state(inode, EXT4_STATE_JDATA);
  2640. journal = EXT4_JOURNAL(inode);
  2641. jbd2_journal_lock_updates(journal);
  2642. err = jbd2_journal_flush(journal);
  2643. jbd2_journal_unlock_updates(journal);
  2644. if (err)
  2645. return 0;
  2646. }
  2647. return generic_block_bmap(mapping, block, ext4_get_block);
  2648. }
  2649. static int ext4_readpage(struct file *file, struct page *page)
  2650. {
  2651. int ret = -EAGAIN;
  2652. struct inode *inode = page->mapping->host;
  2653. trace_ext4_readpage(page);
  2654. if (ext4_has_inline_data(inode))
  2655. ret = ext4_readpage_inline(inode, page);
  2656. if (ret == -EAGAIN)
  2657. return mpage_readpage(page, ext4_get_block);
  2658. return ret;
  2659. }
  2660. static int
  2661. ext4_readpages(struct file *file, struct address_space *mapping,
  2662. struct list_head *pages, unsigned nr_pages)
  2663. {
  2664. struct inode *inode = mapping->host;
  2665. /* If the file has inline data, no need to do readpages. */
  2666. if (ext4_has_inline_data(inode))
  2667. return 0;
  2668. return mpage_readpages(mapping, pages, nr_pages, ext4_get_block);
  2669. }
  2670. static void ext4_invalidatepage(struct page *page, unsigned long offset)
  2671. {
  2672. trace_ext4_invalidatepage(page, offset);
  2673. /* No journalling happens on data buffers when this function is used */
  2674. WARN_ON(page_has_buffers(page) && buffer_jbd(page_buffers(page)));
  2675. block_invalidatepage(page, offset);
  2676. }
  2677. static int __ext4_journalled_invalidatepage(struct page *page,
  2678. unsigned long offset)
  2679. {
  2680. journal_t *journal = EXT4_JOURNAL(page->mapping->host);
  2681. trace_ext4_journalled_invalidatepage(page, offset);
  2682. /*
  2683. * If it's a full truncate we just forget about the pending dirtying
  2684. */
  2685. if (offset == 0)
  2686. ClearPageChecked(page);
  2687. return jbd2_journal_invalidatepage(journal, page, offset);
  2688. }
  2689. /* Wrapper for aops... */
  2690. static void ext4_journalled_invalidatepage(struct page *page,
  2691. unsigned long offset)
  2692. {
  2693. WARN_ON(__ext4_journalled_invalidatepage(page, offset) < 0);
  2694. }
  2695. static int ext4_releasepage(struct page *page, gfp_t wait)
  2696. {
  2697. journal_t *journal = EXT4_JOURNAL(page->mapping->host);
  2698. trace_ext4_releasepage(page);
  2699. /* Page has dirty journalled data -> cannot release */
  2700. if (PageChecked(page))
  2701. return 0;
  2702. if (journal)
  2703. return jbd2_journal_try_to_free_buffers(journal, page, wait);
  2704. else
  2705. return try_to_free_buffers(page);
  2706. }
  2707. /*
  2708. * ext4_get_block used when preparing for a DIO write or buffer write.
  2709. * We allocate an uinitialized extent if blocks haven't been allocated.
  2710. * The extent will be converted to initialized after the IO is complete.
  2711. */
  2712. int ext4_get_block_write(struct inode *inode, sector_t iblock,
  2713. struct buffer_head *bh_result, int create)
  2714. {
  2715. ext4_debug("ext4_get_block_write: inode %lu, create flag %d\n",
  2716. inode->i_ino, create);
  2717. return _ext4_get_block(inode, iblock, bh_result,
  2718. EXT4_GET_BLOCKS_IO_CREATE_EXT);
  2719. }
  2720. static int ext4_get_block_write_nolock(struct inode *inode, sector_t iblock,
  2721. struct buffer_head *bh_result, int create)
  2722. {
  2723. ext4_debug("ext4_get_block_write_nolock: inode %lu, create flag %d\n",
  2724. inode->i_ino, create);
  2725. return _ext4_get_block(inode, iblock, bh_result,
  2726. EXT4_GET_BLOCKS_NO_LOCK);
  2727. }
  2728. static void ext4_end_io_dio(struct kiocb *iocb, loff_t offset,
  2729. ssize_t size, void *private, int ret,
  2730. bool is_async)
  2731. {
  2732. struct inode *inode = file_inode(iocb->ki_filp);
  2733. ext4_io_end_t *io_end = iocb->private;
  2734. /* if not async direct IO or dio with 0 bytes write, just return */
  2735. if (!io_end || !size)
  2736. goto out;
  2737. ext_debug("ext4_end_io_dio(): io_end 0x%p "
  2738. "for inode %lu, iocb 0x%p, offset %llu, size %zd\n",
  2739. iocb->private, io_end->inode->i_ino, iocb, offset,
  2740. size);
  2741. iocb->private = NULL;
  2742. /* if not aio dio with unwritten extents, just free io and return */
  2743. if (!(io_end->flag & EXT4_IO_END_UNWRITTEN)) {
  2744. ext4_free_io_end(io_end);
  2745. out:
  2746. inode_dio_done(inode);
  2747. if (is_async)
  2748. aio_complete(iocb, ret, 0);
  2749. return;
  2750. }
  2751. io_end->offset = offset;
  2752. io_end->size = size;
  2753. if (is_async) {
  2754. io_end->iocb = iocb;
  2755. io_end->result = ret;
  2756. }
  2757. ext4_add_complete_io(io_end);
  2758. }
  2759. /*
  2760. * For ext4 extent files, ext4 will do direct-io write to holes,
  2761. * preallocated extents, and those write extend the file, no need to
  2762. * fall back to buffered IO.
  2763. *
  2764. * For holes, we fallocate those blocks, mark them as uninitialized
  2765. * If those blocks were preallocated, we mark sure they are split, but
  2766. * still keep the range to write as uninitialized.
  2767. *
  2768. * The unwritten extents will be converted to written when DIO is completed.
  2769. * For async direct IO, since the IO may still pending when return, we
  2770. * set up an end_io call back function, which will do the conversion
  2771. * when async direct IO completed.
  2772. *
  2773. * If the O_DIRECT write will extend the file then add this inode to the
  2774. * orphan list. So recovery will truncate it back to the original size
  2775. * if the machine crashes during the write.
  2776. *
  2777. */
  2778. static ssize_t ext4_ext_direct_IO(int rw, struct kiocb *iocb,
  2779. const struct iovec *iov, loff_t offset,
  2780. unsigned long nr_segs)
  2781. {
  2782. struct file *file = iocb->ki_filp;
  2783. struct inode *inode = file->f_mapping->host;
  2784. ssize_t ret;
  2785. size_t count = iov_length(iov, nr_segs);
  2786. int overwrite = 0;
  2787. get_block_t *get_block_func = NULL;
  2788. int dio_flags = 0;
  2789. loff_t final_size = offset + count;
  2790. /* Use the old path for reads and writes beyond i_size. */
  2791. if (rw != WRITE || final_size > inode->i_size)
  2792. return ext4_ind_direct_IO(rw, iocb, iov, offset, nr_segs);
  2793. BUG_ON(iocb->private == NULL);
  2794. /* If we do a overwrite dio, i_mutex locking can be released */
  2795. overwrite = *((int *)iocb->private);
  2796. if (overwrite) {
  2797. atomic_inc(&inode->i_dio_count);
  2798. down_read(&EXT4_I(inode)->i_data_sem);
  2799. mutex_unlock(&inode->i_mutex);
  2800. }
  2801. /*
  2802. * We could direct write to holes and fallocate.
  2803. *
  2804. * Allocated blocks to fill the hole are marked as
  2805. * uninitialized to prevent parallel buffered read to expose
  2806. * the stale data before DIO complete the data IO.
  2807. *
  2808. * As to previously fallocated extents, ext4 get_block will
  2809. * just simply mark the buffer mapped but still keep the
  2810. * extents uninitialized.
  2811. *
  2812. * For non AIO case, we will convert those unwritten extents
  2813. * to written after return back from blockdev_direct_IO.
  2814. *
  2815. * For async DIO, the conversion needs to be deferred when the
  2816. * IO is completed. The ext4 end_io callback function will be
  2817. * called to take care of the conversion work. Here for async
  2818. * case, we allocate an io_end structure to hook to the iocb.
  2819. */
  2820. iocb->private = NULL;
  2821. ext4_inode_aio_set(inode, NULL);
  2822. if (!is_sync_kiocb(iocb)) {
  2823. ext4_io_end_t *io_end = ext4_init_io_end(inode, GFP_NOFS);
  2824. if (!io_end) {
  2825. ret = -ENOMEM;
  2826. goto retake_lock;
  2827. }
  2828. io_end->flag |= EXT4_IO_END_DIRECT;
  2829. iocb->private = io_end;
  2830. /*
  2831. * we save the io structure for current async direct
  2832. * IO, so that later ext4_map_blocks() could flag the
  2833. * io structure whether there is a unwritten extents
  2834. * needs to be converted when IO is completed.
  2835. */
  2836. ext4_inode_aio_set(inode, io_end);
  2837. }
  2838. if (overwrite) {
  2839. get_block_func = ext4_get_block_write_nolock;
  2840. } else {
  2841. get_block_func = ext4_get_block_write;
  2842. dio_flags = DIO_LOCKING;
  2843. }
  2844. ret = __blockdev_direct_IO(rw, iocb, inode,
  2845. inode->i_sb->s_bdev, iov,
  2846. offset, nr_segs,
  2847. get_block_func,
  2848. ext4_end_io_dio,
  2849. NULL,
  2850. dio_flags);
  2851. if (iocb->private)
  2852. ext4_inode_aio_set(inode, NULL);
  2853. /*
  2854. * The io_end structure takes a reference to the inode, that
  2855. * structure needs to be destroyed and the reference to the
  2856. * inode need to be dropped, when IO is complete, even with 0
  2857. * byte write, or failed.
  2858. *
  2859. * In the successful AIO DIO case, the io_end structure will
  2860. * be destroyed and the reference to the inode will be dropped
  2861. * after the end_io call back function is called.
  2862. *
  2863. * In the case there is 0 byte write, or error case, since VFS
  2864. * direct IO won't invoke the end_io call back function, we
  2865. * need to free the end_io structure here.
  2866. */
  2867. if (ret != -EIOCBQUEUED && ret <= 0 && iocb->private) {
  2868. ext4_free_io_end(iocb->private);
  2869. iocb->private = NULL;
  2870. } else if (ret > 0 && !overwrite && ext4_test_inode_state(inode,
  2871. EXT4_STATE_DIO_UNWRITTEN)) {
  2872. int err;
  2873. /*
  2874. * for non AIO case, since the IO is already
  2875. * completed, we could do the conversion right here
  2876. */
  2877. err = ext4_convert_unwritten_extents(inode,
  2878. offset, ret);
  2879. if (err < 0)
  2880. ret = err;
  2881. ext4_clear_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN);
  2882. }
  2883. retake_lock:
  2884. /* take i_mutex locking again if we do a ovewrite dio */
  2885. if (overwrite) {
  2886. inode_dio_done(inode);
  2887. up_read(&EXT4_I(inode)->i_data_sem);
  2888. mutex_lock(&inode->i_mutex);
  2889. }
  2890. return ret;
  2891. }
  2892. static ssize_t ext4_direct_IO(int rw, struct kiocb *iocb,
  2893. const struct iovec *iov, loff_t offset,
  2894. unsigned long nr_segs)
  2895. {
  2896. struct file *file = iocb->ki_filp;
  2897. struct inode *inode = file->f_mapping->host;
  2898. ssize_t ret;
  2899. /*
  2900. * If we are doing data journalling we don't support O_DIRECT
  2901. */
  2902. if (ext4_should_journal_data(inode))
  2903. return 0;
  2904. /* Let buffer I/O handle the inline data case. */
  2905. if (ext4_has_inline_data(inode))
  2906. return 0;
  2907. trace_ext4_direct_IO_enter(inode, offset, iov_length(iov, nr_segs), rw);
  2908. if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
  2909. ret = ext4_ext_direct_IO(rw, iocb, iov, offset, nr_segs);
  2910. else
  2911. ret = ext4_ind_direct_IO(rw, iocb, iov, offset, nr_segs);
  2912. trace_ext4_direct_IO_exit(inode, offset,
  2913. iov_length(iov, nr_segs), rw, ret);
  2914. return ret;
  2915. }
  2916. /*
  2917. * Pages can be marked dirty completely asynchronously from ext4's journalling
  2918. * activity. By filemap_sync_pte(), try_to_unmap_one(), etc. We cannot do
  2919. * much here because ->set_page_dirty is called under VFS locks. The page is
  2920. * not necessarily locked.
  2921. *
  2922. * We cannot just dirty the page and leave attached buffers clean, because the
  2923. * buffers' dirty state is "definitive". We cannot just set the buffers dirty
  2924. * or jbddirty because all the journalling code will explode.
  2925. *
  2926. * So what we do is to mark the page "pending dirty" and next time writepage
  2927. * is called, propagate that into the buffers appropriately.
  2928. */
  2929. static int ext4_journalled_set_page_dirty(struct page *page)
  2930. {
  2931. SetPageChecked(page);
  2932. return __set_page_dirty_nobuffers(page);
  2933. }
  2934. static const struct address_space_operations ext4_aops = {
  2935. .readpage = ext4_readpage,
  2936. .readpages = ext4_readpages,
  2937. .writepage = ext4_writepage,
  2938. .write_begin = ext4_write_begin,
  2939. .write_end = ext4_write_end,
  2940. .bmap = ext4_bmap,
  2941. .invalidatepage = ext4_invalidatepage,
  2942. .releasepage = ext4_releasepage,
  2943. .direct_IO = ext4_direct_IO,
  2944. .migratepage = buffer_migrate_page,
  2945. .is_partially_uptodate = block_is_partially_uptodate,
  2946. .error_remove_page = generic_error_remove_page,
  2947. };
  2948. static const struct address_space_operations ext4_journalled_aops = {
  2949. .readpage = ext4_readpage,
  2950. .readpages = ext4_readpages,
  2951. .writepage = ext4_writepage,
  2952. .write_begin = ext4_write_begin,
  2953. .write_end = ext4_journalled_write_end,
  2954. .set_page_dirty = ext4_journalled_set_page_dirty,
  2955. .bmap = ext4_bmap,
  2956. .invalidatepage = ext4_journalled_invalidatepage,
  2957. .releasepage = ext4_releasepage,
  2958. .direct_IO = ext4_direct_IO,
  2959. .is_partially_uptodate = block_is_partially_uptodate,
  2960. .error_remove_page = generic_error_remove_page,
  2961. };
  2962. static const struct address_space_operations ext4_da_aops = {
  2963. .readpage = ext4_readpage,
  2964. .readpages = ext4_readpages,
  2965. .writepage = ext4_writepage,
  2966. .writepages = ext4_da_writepages,
  2967. .write_begin = ext4_da_write_begin,
  2968. .write_end = ext4_da_write_end,
  2969. .bmap = ext4_bmap,
  2970. .invalidatepage = ext4_da_invalidatepage,
  2971. .releasepage = ext4_releasepage,
  2972. .direct_IO = ext4_direct_IO,
  2973. .migratepage = buffer_migrate_page,
  2974. .is_partially_uptodate = block_is_partially_uptodate,
  2975. .error_remove_page = generic_error_remove_page,
  2976. };
  2977. void ext4_set_aops(struct inode *inode)
  2978. {
  2979. switch (ext4_inode_journal_mode(inode)) {
  2980. case EXT4_INODE_ORDERED_DATA_MODE:
  2981. ext4_set_inode_state(inode, EXT4_STATE_ORDERED_MODE);
  2982. break;
  2983. case EXT4_INODE_WRITEBACK_DATA_MODE:
  2984. ext4_clear_inode_state(inode, EXT4_STATE_ORDERED_MODE);
  2985. break;
  2986. case EXT4_INODE_JOURNAL_DATA_MODE:
  2987. inode->i_mapping->a_ops = &ext4_journalled_aops;
  2988. return;
  2989. default:
  2990. BUG();
  2991. }
  2992. if (test_opt(inode->i_sb, DELALLOC))
  2993. inode->i_mapping->a_ops = &ext4_da_aops;
  2994. else
  2995. inode->i_mapping->a_ops = &ext4_aops;
  2996. }
  2997. /*
  2998. * ext4_discard_partial_page_buffers()
  2999. * Wrapper function for ext4_discard_partial_page_buffers_no_lock.
  3000. * This function finds and locks the page containing the offset
  3001. * "from" and passes it to ext4_discard_partial_page_buffers_no_lock.
  3002. * Calling functions that already have the page locked should call
  3003. * ext4_discard_partial_page_buffers_no_lock directly.
  3004. */
  3005. int ext4_discard_partial_page_buffers(handle_t *handle,
  3006. struct address_space *mapping, loff_t from,
  3007. loff_t length, int flags)
  3008. {
  3009. struct inode *inode = mapping->host;
  3010. struct page *page;
  3011. int err = 0;
  3012. page = find_or_create_page(mapping, from >> PAGE_CACHE_SHIFT,
  3013. mapping_gfp_mask(mapping) & ~__GFP_FS);
  3014. if (!page)
  3015. return -ENOMEM;
  3016. err = ext4_discard_partial_page_buffers_no_lock(handle, inode, page,
  3017. from, length, flags);
  3018. unlock_page(page);
  3019. page_cache_release(page);
  3020. return err;
  3021. }
  3022. /*
  3023. * ext4_discard_partial_page_buffers_no_lock()
  3024. * Zeros a page range of length 'length' starting from offset 'from'.
  3025. * Buffer heads that correspond to the block aligned regions of the
  3026. * zeroed range will be unmapped. Unblock aligned regions
  3027. * will have the corresponding buffer head mapped if needed so that
  3028. * that region of the page can be updated with the partial zero out.
  3029. *
  3030. * This function assumes that the page has already been locked. The
  3031. * The range to be discarded must be contained with in the given page.
  3032. * If the specified range exceeds the end of the page it will be shortened
  3033. * to the end of the page that corresponds to 'from'. This function is
  3034. * appropriate for updating a page and it buffer heads to be unmapped and
  3035. * zeroed for blocks that have been either released, or are going to be
  3036. * released.
  3037. *
  3038. * handle: The journal handle
  3039. * inode: The files inode
  3040. * page: A locked page that contains the offset "from"
  3041. * from: The starting byte offset (from the beginning of the file)
  3042. * to begin discarding
  3043. * len: The length of bytes to discard
  3044. * flags: Optional flags that may be used:
  3045. *
  3046. * EXT4_DISCARD_PARTIAL_PG_ZERO_UNMAPPED
  3047. * Only zero the regions of the page whose buffer heads
  3048. * have already been unmapped. This flag is appropriate
  3049. * for updating the contents of a page whose blocks may
  3050. * have already been released, and we only want to zero
  3051. * out the regions that correspond to those released blocks.
  3052. *
  3053. * Returns zero on success or negative on failure.
  3054. */
  3055. static int ext4_discard_partial_page_buffers_no_lock(handle_t *handle,
  3056. struct inode *inode, struct page *page, loff_t from,
  3057. loff_t length, int flags)
  3058. {
  3059. ext4_fsblk_t index = from >> PAGE_CACHE_SHIFT;
  3060. unsigned int offset = from & (PAGE_CACHE_SIZE-1);
  3061. unsigned int blocksize, max, pos;
  3062. ext4_lblk_t iblock;
  3063. struct buffer_head *bh;
  3064. int err = 0;
  3065. blocksize = inode->i_sb->s_blocksize;
  3066. max = PAGE_CACHE_SIZE - offset;
  3067. if (index != page->index)
  3068. return -EINVAL;
  3069. /*
  3070. * correct length if it does not fall between
  3071. * 'from' and the end of the page
  3072. */
  3073. if (length > max || length < 0)
  3074. length = max;
  3075. iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
  3076. if (!page_has_buffers(page))
  3077. create_empty_buffers(page, blocksize, 0);
  3078. /* Find the buffer that contains "offset" */
  3079. bh = page_buffers(page);
  3080. pos = blocksize;
  3081. while (offset >= pos) {
  3082. bh = bh->b_this_page;
  3083. iblock++;
  3084. pos += blocksize;
  3085. }
  3086. pos = offset;
  3087. while (pos < offset + length) {
  3088. unsigned int end_of_block, range_to_discard;
  3089. err = 0;
  3090. /* The length of space left to zero and unmap */
  3091. range_to_discard = offset + length - pos;
  3092. /* The length of space until the end of the block */
  3093. end_of_block = blocksize - (pos & (blocksize-1));
  3094. /*
  3095. * Do not unmap or zero past end of block
  3096. * for this buffer head
  3097. */
  3098. if (range_to_discard > end_of_block)
  3099. range_to_discard = end_of_block;
  3100. /*
  3101. * Skip this buffer head if we are only zeroing unampped
  3102. * regions of the page
  3103. */
  3104. if (flags & EXT4_DISCARD_PARTIAL_PG_ZERO_UNMAPPED &&
  3105. buffer_mapped(bh))
  3106. goto next;
  3107. /* If the range is block aligned, unmap */
  3108. if (range_to_discard == blocksize) {
  3109. clear_buffer_dirty(bh);
  3110. bh->b_bdev = NULL;
  3111. clear_buffer_mapped(bh);
  3112. clear_buffer_req(bh);
  3113. clear_buffer_new(bh);
  3114. clear_buffer_delay(bh);
  3115. clear_buffer_unwritten(bh);
  3116. clear_buffer_uptodate(bh);
  3117. zero_user(page, pos, range_to_discard);
  3118. BUFFER_TRACE(bh, "Buffer discarded");
  3119. goto next;
  3120. }
  3121. /*
  3122. * If this block is not completely contained in the range
  3123. * to be discarded, then it is not going to be released. Because
  3124. * we need to keep this block, we need to make sure this part
  3125. * of the page is uptodate before we modify it by writeing
  3126. * partial zeros on it.
  3127. */
  3128. if (!buffer_mapped(bh)) {
  3129. /*
  3130. * Buffer head must be mapped before we can read
  3131. * from the block
  3132. */
  3133. BUFFER_TRACE(bh, "unmapped");
  3134. ext4_get_block(inode, iblock, bh, 0);
  3135. /* unmapped? It's a hole - nothing to do */
  3136. if (!buffer_mapped(bh)) {
  3137. BUFFER_TRACE(bh, "still unmapped");
  3138. goto next;
  3139. }
  3140. }
  3141. /* Ok, it's mapped. Make sure it's up-to-date */
  3142. if (PageUptodate(page))
  3143. set_buffer_uptodate(bh);
  3144. if (!buffer_uptodate(bh)) {
  3145. err = -EIO;
  3146. ll_rw_block(READ, 1, &bh);
  3147. wait_on_buffer(bh);
  3148. /* Uhhuh. Read error. Complain and punt.*/
  3149. if (!buffer_uptodate(bh))
  3150. goto next;
  3151. }
  3152. if (ext4_should_journal_data(inode)) {
  3153. BUFFER_TRACE(bh, "get write access");
  3154. err = ext4_journal_get_write_access(handle, bh);
  3155. if (err)
  3156. goto next;
  3157. }
  3158. zero_user(page, pos, range_to_discard);
  3159. err = 0;
  3160. if (ext4_should_journal_data(inode)) {
  3161. err = ext4_handle_dirty_metadata(handle, inode, bh);
  3162. } else
  3163. mark_buffer_dirty(bh);
  3164. BUFFER_TRACE(bh, "Partial buffer zeroed");
  3165. next:
  3166. bh = bh->b_this_page;
  3167. iblock++;
  3168. pos += range_to_discard;
  3169. }
  3170. return err;
  3171. }
  3172. int ext4_can_truncate(struct inode *inode)
  3173. {
  3174. if (S_ISREG(inode->i_mode))
  3175. return 1;
  3176. if (S_ISDIR(inode->i_mode))
  3177. return 1;
  3178. if (S_ISLNK(inode->i_mode))
  3179. return !ext4_inode_is_fast_symlink(inode);
  3180. return 0;
  3181. }
  3182. /*
  3183. * ext4_punch_hole: punches a hole in a file by releaseing the blocks
  3184. * associated with the given offset and length
  3185. *
  3186. * @inode: File inode
  3187. * @offset: The offset where the hole will begin
  3188. * @len: The length of the hole
  3189. *
  3190. * Returns: 0 on success or negative on failure
  3191. */
  3192. int ext4_punch_hole(struct file *file, loff_t offset, loff_t length)
  3193. {
  3194. struct inode *inode = file_inode(file);
  3195. struct super_block *sb = inode->i_sb;
  3196. ext4_lblk_t first_block, stop_block;
  3197. struct address_space *mapping = inode->i_mapping;
  3198. loff_t first_page, last_page, page_len;
  3199. loff_t first_page_offset, last_page_offset;
  3200. handle_t *handle;
  3201. unsigned int credits;
  3202. int ret = 0;
  3203. if (!S_ISREG(inode->i_mode))
  3204. return -EOPNOTSUPP;
  3205. if (EXT4_SB(sb)->s_cluster_ratio > 1) {
  3206. /* TODO: Add support for bigalloc file systems */
  3207. return -EOPNOTSUPP;
  3208. }
  3209. trace_ext4_punch_hole(inode, offset, length);
  3210. /*
  3211. * Write out all dirty pages to avoid race conditions
  3212. * Then release them.
  3213. */
  3214. if (mapping->nrpages && mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) {
  3215. ret = filemap_write_and_wait_range(mapping, offset,
  3216. offset + length - 1);
  3217. if (ret)
  3218. return ret;
  3219. }
  3220. mutex_lock(&inode->i_mutex);
  3221. /* It's not possible punch hole on append only file */
  3222. if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) {
  3223. ret = -EPERM;
  3224. goto out_mutex;
  3225. }
  3226. if (IS_SWAPFILE(inode)) {
  3227. ret = -ETXTBSY;
  3228. goto out_mutex;
  3229. }
  3230. /* No need to punch hole beyond i_size */
  3231. if (offset >= inode->i_size)
  3232. goto out_mutex;
  3233. /*
  3234. * If the hole extends beyond i_size, set the hole
  3235. * to end after the page that contains i_size
  3236. */
  3237. if (offset + length > inode->i_size) {
  3238. length = inode->i_size +
  3239. PAGE_CACHE_SIZE - (inode->i_size & (PAGE_CACHE_SIZE - 1)) -
  3240. offset;
  3241. }
  3242. first_page = (offset + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  3243. last_page = (offset + length) >> PAGE_CACHE_SHIFT;
  3244. first_page_offset = first_page << PAGE_CACHE_SHIFT;
  3245. last_page_offset = last_page << PAGE_CACHE_SHIFT;
  3246. /* Now release the pages */
  3247. if (last_page_offset > first_page_offset) {
  3248. truncate_pagecache_range(inode, first_page_offset,
  3249. last_page_offset - 1);
  3250. }
  3251. /* Wait all existing dio workers, newcomers will block on i_mutex */
  3252. ext4_inode_block_unlocked_dio(inode);
  3253. ret = ext4_flush_unwritten_io(inode);
  3254. if (ret)
  3255. goto out_dio;
  3256. inode_dio_wait(inode);
  3257. if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
  3258. credits = ext4_writepage_trans_blocks(inode);
  3259. else
  3260. credits = ext4_blocks_for_truncate(inode);
  3261. handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits);
  3262. if (IS_ERR(handle)) {
  3263. ret = PTR_ERR(handle);
  3264. ext4_std_error(sb, ret);
  3265. goto out_dio;
  3266. }
  3267. /*
  3268. * Now we need to zero out the non-page-aligned data in the
  3269. * pages at the start and tail of the hole, and unmap the
  3270. * buffer heads for the block aligned regions of the page that
  3271. * were completely zeroed.
  3272. */
  3273. if (first_page > last_page) {
  3274. /*
  3275. * If the file space being truncated is contained
  3276. * within a page just zero out and unmap the middle of
  3277. * that page
  3278. */
  3279. ret = ext4_discard_partial_page_buffers(handle,
  3280. mapping, offset, length, 0);
  3281. if (ret)
  3282. goto out_stop;
  3283. } else {
  3284. /*
  3285. * zero out and unmap the partial page that contains
  3286. * the start of the hole
  3287. */
  3288. page_len = first_page_offset - offset;
  3289. if (page_len > 0) {
  3290. ret = ext4_discard_partial_page_buffers(handle, mapping,
  3291. offset, page_len, 0);
  3292. if (ret)
  3293. goto out_stop;
  3294. }
  3295. /*
  3296. * zero out and unmap the partial page that contains
  3297. * the end of the hole
  3298. */
  3299. page_len = offset + length - last_page_offset;
  3300. if (page_len > 0) {
  3301. ret = ext4_discard_partial_page_buffers(handle, mapping,
  3302. last_page_offset, page_len, 0);
  3303. if (ret)
  3304. goto out_stop;
  3305. }
  3306. }
  3307. /*
  3308. * If i_size is contained in the last page, we need to
  3309. * unmap and zero the partial page after i_size
  3310. */
  3311. if (inode->i_size >> PAGE_CACHE_SHIFT == last_page &&
  3312. inode->i_size % PAGE_CACHE_SIZE != 0) {
  3313. page_len = PAGE_CACHE_SIZE -
  3314. (inode->i_size & (PAGE_CACHE_SIZE - 1));
  3315. if (page_len > 0) {
  3316. ret = ext4_discard_partial_page_buffers(handle,
  3317. mapping, inode->i_size, page_len, 0);
  3318. if (ret)
  3319. goto out_stop;
  3320. }
  3321. }
  3322. first_block = (offset + sb->s_blocksize - 1) >>
  3323. EXT4_BLOCK_SIZE_BITS(sb);
  3324. stop_block = (offset + length) >> EXT4_BLOCK_SIZE_BITS(sb);
  3325. /* If there are no blocks to remove, return now */
  3326. if (first_block >= stop_block)
  3327. goto out_stop;
  3328. down_write(&EXT4_I(inode)->i_data_sem);
  3329. ext4_discard_preallocations(inode);
  3330. ret = ext4_es_remove_extent(inode, first_block,
  3331. stop_block - first_block);
  3332. if (ret) {
  3333. up_write(&EXT4_I(inode)->i_data_sem);
  3334. goto out_stop;
  3335. }
  3336. if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
  3337. ret = ext4_ext_remove_space(inode, first_block,
  3338. stop_block - 1);
  3339. else
  3340. ret = ext4_free_hole_blocks(handle, inode, first_block,
  3341. stop_block);
  3342. ext4_discard_preallocations(inode);
  3343. up_write(&EXT4_I(inode)->i_data_sem);
  3344. if (IS_SYNC(inode))
  3345. ext4_handle_sync(handle);
  3346. inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
  3347. ext4_mark_inode_dirty(handle, inode);
  3348. out_stop:
  3349. ext4_journal_stop(handle);
  3350. out_dio:
  3351. ext4_inode_resume_unlocked_dio(inode);
  3352. out_mutex:
  3353. mutex_unlock(&inode->i_mutex);
  3354. return ret;
  3355. }
  3356. /*
  3357. * ext4_truncate()
  3358. *
  3359. * We block out ext4_get_block() block instantiations across the entire
  3360. * transaction, and VFS/VM ensures that ext4_truncate() cannot run
  3361. * simultaneously on behalf of the same inode.
  3362. *
  3363. * As we work through the truncate and commit bits of it to the journal there
  3364. * is one core, guiding principle: the file's tree must always be consistent on
  3365. * disk. We must be able to restart the truncate after a crash.
  3366. *
  3367. * The file's tree may be transiently inconsistent in memory (although it
  3368. * probably isn't), but whenever we close off and commit a journal transaction,
  3369. * the contents of (the filesystem + the journal) must be consistent and
  3370. * restartable. It's pretty simple, really: bottom up, right to left (although
  3371. * left-to-right works OK too).
  3372. *
  3373. * Note that at recovery time, journal replay occurs *before* the restart of
  3374. * truncate against the orphan inode list.
  3375. *
  3376. * The committed inode has the new, desired i_size (which is the same as
  3377. * i_disksize in this case). After a crash, ext4_orphan_cleanup() will see
  3378. * that this inode's truncate did not complete and it will again call
  3379. * ext4_truncate() to have another go. So there will be instantiated blocks
  3380. * to the right of the truncation point in a crashed ext4 filesystem. But
  3381. * that's fine - as long as they are linked from the inode, the post-crash
  3382. * ext4_truncate() run will find them and release them.
  3383. */
  3384. void ext4_truncate(struct inode *inode)
  3385. {
  3386. struct ext4_inode_info *ei = EXT4_I(inode);
  3387. unsigned int credits;
  3388. handle_t *handle;
  3389. struct address_space *mapping = inode->i_mapping;
  3390. loff_t page_len;
  3391. /*
  3392. * There is a possibility that we're either freeing the inode
  3393. * or it completely new indode. In those cases we might not
  3394. * have i_mutex locked because it's not necessary.
  3395. */
  3396. if (!(inode->i_state & (I_NEW|I_FREEING)))
  3397. WARN_ON(!mutex_is_locked(&inode->i_mutex));
  3398. trace_ext4_truncate_enter(inode);
  3399. if (!ext4_can_truncate(inode))
  3400. return;
  3401. ext4_clear_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
  3402. if (inode->i_size == 0 && !test_opt(inode->i_sb, NO_AUTO_DA_ALLOC))
  3403. ext4_set_inode_state(inode, EXT4_STATE_DA_ALLOC_CLOSE);
  3404. if (ext4_has_inline_data(inode)) {
  3405. int has_inline = 1;
  3406. ext4_inline_data_truncate(inode, &has_inline);
  3407. if (has_inline)
  3408. return;
  3409. }
  3410. /*
  3411. * finish any pending end_io work so we won't run the risk of
  3412. * converting any truncated blocks to initialized later
  3413. */
  3414. ext4_flush_unwritten_io(inode);
  3415. if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
  3416. credits = ext4_writepage_trans_blocks(inode);
  3417. else
  3418. credits = ext4_blocks_for_truncate(inode);
  3419. handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits);
  3420. if (IS_ERR(handle)) {
  3421. ext4_std_error(inode->i_sb, PTR_ERR(handle));
  3422. return;
  3423. }
  3424. if (inode->i_size % PAGE_CACHE_SIZE != 0) {
  3425. page_len = PAGE_CACHE_SIZE -
  3426. (inode->i_size & (PAGE_CACHE_SIZE - 1));
  3427. if (ext4_discard_partial_page_buffers(handle,
  3428. mapping, inode->i_size, page_len, 0))
  3429. goto out_stop;
  3430. }
  3431. /*
  3432. * We add the inode to the orphan list, so that if this
  3433. * truncate spans multiple transactions, and we crash, we will
  3434. * resume the truncate when the filesystem recovers. It also
  3435. * marks the inode dirty, to catch the new size.
  3436. *
  3437. * Implication: the file must always be in a sane, consistent
  3438. * truncatable state while each transaction commits.
  3439. */
  3440. if (ext4_orphan_add(handle, inode))
  3441. goto out_stop;
  3442. down_write(&EXT4_I(inode)->i_data_sem);
  3443. ext4_discard_preallocations(inode);
  3444. if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
  3445. ext4_ext_truncate(handle, inode);
  3446. else
  3447. ext4_ind_truncate(handle, inode);
  3448. up_write(&ei->i_data_sem);
  3449. if (IS_SYNC(inode))
  3450. ext4_handle_sync(handle);
  3451. out_stop:
  3452. /*
  3453. * If this was a simple ftruncate() and the file will remain alive,
  3454. * then we need to clear up the orphan record which we created above.
  3455. * However, if this was a real unlink then we were called by
  3456. * ext4_delete_inode(), and we allow that function to clean up the
  3457. * orphan info for us.
  3458. */
  3459. if (inode->i_nlink)
  3460. ext4_orphan_del(handle, inode);
  3461. inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
  3462. ext4_mark_inode_dirty(handle, inode);
  3463. ext4_journal_stop(handle);
  3464. trace_ext4_truncate_exit(inode);
  3465. }
  3466. /*
  3467. * ext4_get_inode_loc returns with an extra refcount against the inode's
  3468. * underlying buffer_head on success. If 'in_mem' is true, we have all
  3469. * data in memory that is needed to recreate the on-disk version of this
  3470. * inode.
  3471. */
  3472. static int __ext4_get_inode_loc(struct inode *inode,
  3473. struct ext4_iloc *iloc, int in_mem)
  3474. {
  3475. struct ext4_group_desc *gdp;
  3476. struct buffer_head *bh;
  3477. struct super_block *sb = inode->i_sb;
  3478. ext4_fsblk_t block;
  3479. int inodes_per_block, inode_offset;
  3480. iloc->bh = NULL;
  3481. if (!ext4_valid_inum(sb, inode->i_ino))
  3482. return -EIO;
  3483. iloc->block_group = (inode->i_ino - 1) / EXT4_INODES_PER_GROUP(sb);
  3484. gdp = ext4_get_group_desc(sb, iloc->block_group, NULL);
  3485. if (!gdp)
  3486. return -EIO;
  3487. /*
  3488. * Figure out the offset within the block group inode table
  3489. */
  3490. inodes_per_block = EXT4_SB(sb)->s_inodes_per_block;
  3491. inode_offset = ((inode->i_ino - 1) %
  3492. EXT4_INODES_PER_GROUP(sb));
  3493. block = ext4_inode_table(sb, gdp) + (inode_offset / inodes_per_block);
  3494. iloc->offset = (inode_offset % inodes_per_block) * EXT4_INODE_SIZE(sb);
  3495. bh = sb_getblk(sb, block);
  3496. if (unlikely(!bh))
  3497. return -ENOMEM;
  3498. if (!buffer_uptodate(bh)) {
  3499. lock_buffer(bh);
  3500. /*
  3501. * If the buffer has the write error flag, we have failed
  3502. * to write out another inode in the same block. In this
  3503. * case, we don't have to read the block because we may
  3504. * read the old inode data successfully.
  3505. */
  3506. if (buffer_write_io_error(bh) && !buffer_uptodate(bh))
  3507. set_buffer_uptodate(bh);
  3508. if (buffer_uptodate(bh)) {
  3509. /* someone brought it uptodate while we waited */
  3510. unlock_buffer(bh);
  3511. goto has_buffer;
  3512. }
  3513. /*
  3514. * If we have all information of the inode in memory and this
  3515. * is the only valid inode in the block, we need not read the
  3516. * block.
  3517. */
  3518. if (in_mem) {
  3519. struct buffer_head *bitmap_bh;
  3520. int i, start;
  3521. start = inode_offset & ~(inodes_per_block - 1);
  3522. /* Is the inode bitmap in cache? */
  3523. bitmap_bh = sb_getblk(sb, ext4_inode_bitmap(sb, gdp));
  3524. if (unlikely(!bitmap_bh))
  3525. goto make_io;
  3526. /*
  3527. * If the inode bitmap isn't in cache then the
  3528. * optimisation may end up performing two reads instead
  3529. * of one, so skip it.
  3530. */
  3531. if (!buffer_uptodate(bitmap_bh)) {
  3532. brelse(bitmap_bh);
  3533. goto make_io;
  3534. }
  3535. for (i = start; i < start + inodes_per_block; i++) {
  3536. if (i == inode_offset)
  3537. continue;
  3538. if (ext4_test_bit(i, bitmap_bh->b_data))
  3539. break;
  3540. }
  3541. brelse(bitmap_bh);
  3542. if (i == start + inodes_per_block) {
  3543. /* all other inodes are free, so skip I/O */
  3544. memset(bh->b_data, 0, bh->b_size);
  3545. set_buffer_uptodate(bh);
  3546. unlock_buffer(bh);
  3547. goto has_buffer;
  3548. }
  3549. }
  3550. make_io:
  3551. /*
  3552. * If we need to do any I/O, try to pre-readahead extra
  3553. * blocks from the inode table.
  3554. */
  3555. if (EXT4_SB(sb)->s_inode_readahead_blks) {
  3556. ext4_fsblk_t b, end, table;
  3557. unsigned num;
  3558. table = ext4_inode_table(sb, gdp);
  3559. /* s_inode_readahead_blks is always a power of 2 */
  3560. b = block & ~(EXT4_SB(sb)->s_inode_readahead_blks-1);
  3561. if (table > b)
  3562. b = table;
  3563. end = b + EXT4_SB(sb)->s_inode_readahead_blks;
  3564. num = EXT4_INODES_PER_GROUP(sb);
  3565. if (ext4_has_group_desc_csum(sb))
  3566. num -= ext4_itable_unused_count(sb, gdp);
  3567. table += num / inodes_per_block;
  3568. if (end > table)
  3569. end = table;
  3570. while (b <= end)
  3571. sb_breadahead(sb, b++);
  3572. }
  3573. /*
  3574. * There are other valid inodes in the buffer, this inode
  3575. * has in-inode xattrs, or we don't have this inode in memory.
  3576. * Read the block from disk.
  3577. */
  3578. trace_ext4_load_inode(inode);
  3579. get_bh(bh);
  3580. bh->b_end_io = end_buffer_read_sync;
  3581. submit_bh(READ | REQ_META | REQ_PRIO, bh);
  3582. wait_on_buffer(bh);
  3583. if (!buffer_uptodate(bh)) {
  3584. EXT4_ERROR_INODE_BLOCK(inode, block,
  3585. "unable to read itable block");
  3586. brelse(bh);
  3587. return -EIO;
  3588. }
  3589. }
  3590. has_buffer:
  3591. iloc->bh = bh;
  3592. return 0;
  3593. }
  3594. int ext4_get_inode_loc(struct inode *inode, struct ext4_iloc *iloc)
  3595. {
  3596. /* We have all inode data except xattrs in memory here. */
  3597. return __ext4_get_inode_loc(inode, iloc,
  3598. !ext4_test_inode_state(inode, EXT4_STATE_XATTR));
  3599. }
  3600. void ext4_set_inode_flags(struct inode *inode)
  3601. {
  3602. unsigned int flags = EXT4_I(inode)->i_flags;
  3603. inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
  3604. if (flags & EXT4_SYNC_FL)
  3605. inode->i_flags |= S_SYNC;
  3606. if (flags & EXT4_APPEND_FL)
  3607. inode->i_flags |= S_APPEND;
  3608. if (flags & EXT4_IMMUTABLE_FL)
  3609. inode->i_flags |= S_IMMUTABLE;
  3610. if (flags & EXT4_NOATIME_FL)
  3611. inode->i_flags |= S_NOATIME;
  3612. if (flags & EXT4_DIRSYNC_FL)
  3613. inode->i_flags |= S_DIRSYNC;
  3614. }
  3615. /* Propagate flags from i_flags to EXT4_I(inode)->i_flags */
  3616. void ext4_get_inode_flags(struct ext4_inode_info *ei)
  3617. {
  3618. unsigned int vfs_fl;
  3619. unsigned long old_fl, new_fl;
  3620. do {
  3621. vfs_fl = ei->vfs_inode.i_flags;
  3622. old_fl = ei->i_flags;
  3623. new_fl = old_fl & ~(EXT4_SYNC_FL|EXT4_APPEND_FL|
  3624. EXT4_IMMUTABLE_FL|EXT4_NOATIME_FL|
  3625. EXT4_DIRSYNC_FL);
  3626. if (vfs_fl & S_SYNC)
  3627. new_fl |= EXT4_SYNC_FL;
  3628. if (vfs_fl & S_APPEND)
  3629. new_fl |= EXT4_APPEND_FL;
  3630. if (vfs_fl & S_IMMUTABLE)
  3631. new_fl |= EXT4_IMMUTABLE_FL;
  3632. if (vfs_fl & S_NOATIME)
  3633. new_fl |= EXT4_NOATIME_FL;
  3634. if (vfs_fl & S_DIRSYNC)
  3635. new_fl |= EXT4_DIRSYNC_FL;
  3636. } while (cmpxchg(&ei->i_flags, old_fl, new_fl) != old_fl);
  3637. }
  3638. static blkcnt_t ext4_inode_blocks(struct ext4_inode *raw_inode,
  3639. struct ext4_inode_info *ei)
  3640. {
  3641. blkcnt_t i_blocks ;
  3642. struct inode *inode = &(ei->vfs_inode);
  3643. struct super_block *sb = inode->i_sb;
  3644. if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
  3645. EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) {
  3646. /* we are using combined 48 bit field */
  3647. i_blocks = ((u64)le16_to_cpu(raw_inode->i_blocks_high)) << 32 |
  3648. le32_to_cpu(raw_inode->i_blocks_lo);
  3649. if (ext4_test_inode_flag(inode, EXT4_INODE_HUGE_FILE)) {
  3650. /* i_blocks represent file system block size */
  3651. return i_blocks << (inode->i_blkbits - 9);
  3652. } else {
  3653. return i_blocks;
  3654. }
  3655. } else {
  3656. return le32_to_cpu(raw_inode->i_blocks_lo);
  3657. }
  3658. }
  3659. static inline void ext4_iget_extra_inode(struct inode *inode,
  3660. struct ext4_inode *raw_inode,
  3661. struct ext4_inode_info *ei)
  3662. {
  3663. __le32 *magic = (void *)raw_inode +
  3664. EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize;
  3665. if (*magic == cpu_to_le32(EXT4_XATTR_MAGIC)) {
  3666. ext4_set_inode_state(inode, EXT4_STATE_XATTR);
  3667. ext4_find_inline_data_nolock(inode);
  3668. } else
  3669. EXT4_I(inode)->i_inline_off = 0;
  3670. }
  3671. struct inode *ext4_iget(struct super_block *sb, unsigned long ino)
  3672. {
  3673. struct ext4_iloc iloc;
  3674. struct ext4_inode *raw_inode;
  3675. struct ext4_inode_info *ei;
  3676. struct inode *inode;
  3677. journal_t *journal = EXT4_SB(sb)->s_journal;
  3678. long ret;
  3679. int block;
  3680. uid_t i_uid;
  3681. gid_t i_gid;
  3682. inode = iget_locked(sb, ino);
  3683. if (!inode)
  3684. return ERR_PTR(-ENOMEM);
  3685. if (!(inode->i_state & I_NEW))
  3686. return inode;
  3687. ei = EXT4_I(inode);
  3688. iloc.bh = NULL;
  3689. ret = __ext4_get_inode_loc(inode, &iloc, 0);
  3690. if (ret < 0)
  3691. goto bad_inode;
  3692. raw_inode = ext4_raw_inode(&iloc);
  3693. if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
  3694. ei->i_extra_isize = le16_to_cpu(raw_inode->i_extra_isize);
  3695. if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize >
  3696. EXT4_INODE_SIZE(inode->i_sb)) {
  3697. EXT4_ERROR_INODE(inode, "bad extra_isize (%u != %u)",
  3698. EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize,
  3699. EXT4_INODE_SIZE(inode->i_sb));
  3700. ret = -EIO;
  3701. goto bad_inode;
  3702. }
  3703. } else
  3704. ei->i_extra_isize = 0;
  3705. /* Precompute checksum seed for inode metadata */
  3706. if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
  3707. EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) {
  3708. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  3709. __u32 csum;
  3710. __le32 inum = cpu_to_le32(inode->i_ino);
  3711. __le32 gen = raw_inode->i_generation;
  3712. csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&inum,
  3713. sizeof(inum));
  3714. ei->i_csum_seed = ext4_chksum(sbi, csum, (__u8 *)&gen,
  3715. sizeof(gen));
  3716. }
  3717. if (!ext4_inode_csum_verify(inode, raw_inode, ei)) {
  3718. EXT4_ERROR_INODE(inode, "checksum invalid");
  3719. ret = -EIO;
  3720. goto bad_inode;
  3721. }
  3722. inode->i_mode = le16_to_cpu(raw_inode->i_mode);
  3723. i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low);
  3724. i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low);
  3725. if (!(test_opt(inode->i_sb, NO_UID32))) {
  3726. i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16;
  3727. i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16;
  3728. }
  3729. i_uid_write(inode, i_uid);
  3730. i_gid_write(inode, i_gid);
  3731. set_nlink(inode, le16_to_cpu(raw_inode->i_links_count));
  3732. ext4_clear_state_flags(ei); /* Only relevant on 32-bit archs */
  3733. ei->i_inline_off = 0;
  3734. ei->i_dir_start_lookup = 0;
  3735. ei->i_dtime = le32_to_cpu(raw_inode->i_dtime);
  3736. /* We now have enough fields to check if the inode was active or not.
  3737. * This is needed because nfsd might try to access dead inodes
  3738. * the test is that same one that e2fsck uses
  3739. * NeilBrown 1999oct15
  3740. */
  3741. if (inode->i_nlink == 0) {
  3742. if (inode->i_mode == 0 ||
  3743. !(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ORPHAN_FS)) {
  3744. /* this inode is deleted */
  3745. ret = -ESTALE;
  3746. goto bad_inode;
  3747. }
  3748. /* The only unlinked inodes we let through here have
  3749. * valid i_mode and are being read by the orphan
  3750. * recovery code: that's fine, we're about to complete
  3751. * the process of deleting those. */
  3752. }
  3753. ei->i_flags = le32_to_cpu(raw_inode->i_flags);
  3754. inode->i_blocks = ext4_inode_blocks(raw_inode, ei);
  3755. ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl_lo);
  3756. if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT))
  3757. ei->i_file_acl |=
  3758. ((__u64)le16_to_cpu(raw_inode->i_file_acl_high)) << 32;
  3759. inode->i_size = ext4_isize(raw_inode);
  3760. ei->i_disksize = inode->i_size;
  3761. #ifdef CONFIG_QUOTA
  3762. ei->i_reserved_quota = 0;
  3763. #endif
  3764. inode->i_generation = le32_to_cpu(raw_inode->i_generation);
  3765. ei->i_block_group = iloc.block_group;
  3766. ei->i_last_alloc_group = ~0;
  3767. /*
  3768. * NOTE! The in-memory inode i_data array is in little-endian order
  3769. * even on big-endian machines: we do NOT byteswap the block numbers!
  3770. */
  3771. for (block = 0; block < EXT4_N_BLOCKS; block++)
  3772. ei->i_data[block] = raw_inode->i_block[block];
  3773. INIT_LIST_HEAD(&ei->i_orphan);
  3774. /*
  3775. * Set transaction id's of transactions that have to be committed
  3776. * to finish f[data]sync. We set them to currently running transaction
  3777. * as we cannot be sure that the inode or some of its metadata isn't
  3778. * part of the transaction - the inode could have been reclaimed and
  3779. * now it is reread from disk.
  3780. */
  3781. if (journal) {
  3782. transaction_t *transaction;
  3783. tid_t tid;
  3784. read_lock(&journal->j_state_lock);
  3785. if (journal->j_running_transaction)
  3786. transaction = journal->j_running_transaction;
  3787. else
  3788. transaction = journal->j_committing_transaction;
  3789. if (transaction)
  3790. tid = transaction->t_tid;
  3791. else
  3792. tid = journal->j_commit_sequence;
  3793. read_unlock(&journal->j_state_lock);
  3794. ei->i_sync_tid = tid;
  3795. ei->i_datasync_tid = tid;
  3796. }
  3797. if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
  3798. if (ei->i_extra_isize == 0) {
  3799. /* The extra space is currently unused. Use it. */
  3800. ei->i_extra_isize = sizeof(struct ext4_inode) -
  3801. EXT4_GOOD_OLD_INODE_SIZE;
  3802. } else {
  3803. ext4_iget_extra_inode(inode, raw_inode, ei);
  3804. }
  3805. }
  3806. EXT4_INODE_GET_XTIME(i_ctime, inode, raw_inode);
  3807. EXT4_INODE_GET_XTIME(i_mtime, inode, raw_inode);
  3808. EXT4_INODE_GET_XTIME(i_atime, inode, raw_inode);
  3809. EXT4_EINODE_GET_XTIME(i_crtime, ei, raw_inode);
  3810. inode->i_version = le32_to_cpu(raw_inode->i_disk_version);
  3811. if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
  3812. if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi))
  3813. inode->i_version |=
  3814. (__u64)(le32_to_cpu(raw_inode->i_version_hi)) << 32;
  3815. }
  3816. ret = 0;
  3817. if (ei->i_file_acl &&
  3818. !ext4_data_block_valid(EXT4_SB(sb), ei->i_file_acl, 1)) {
  3819. EXT4_ERROR_INODE(inode, "bad extended attribute block %llu",
  3820. ei->i_file_acl);
  3821. ret = -EIO;
  3822. goto bad_inode;
  3823. } else if (!ext4_has_inline_data(inode)) {
  3824. if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
  3825. if ((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
  3826. (S_ISLNK(inode->i_mode) &&
  3827. !ext4_inode_is_fast_symlink(inode))))
  3828. /* Validate extent which is part of inode */
  3829. ret = ext4_ext_check_inode(inode);
  3830. } else if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
  3831. (S_ISLNK(inode->i_mode) &&
  3832. !ext4_inode_is_fast_symlink(inode))) {
  3833. /* Validate block references which are part of inode */
  3834. ret = ext4_ind_check_inode(inode);
  3835. }
  3836. }
  3837. if (ret)
  3838. goto bad_inode;
  3839. if (S_ISREG(inode->i_mode)) {
  3840. inode->i_op = &ext4_file_inode_operations;
  3841. inode->i_fop = &ext4_file_operations;
  3842. ext4_set_aops(inode);
  3843. } else if (S_ISDIR(inode->i_mode)) {
  3844. inode->i_op = &ext4_dir_inode_operations;
  3845. inode->i_fop = &ext4_dir_operations;
  3846. } else if (S_ISLNK(inode->i_mode)) {
  3847. if (ext4_inode_is_fast_symlink(inode)) {
  3848. inode->i_op = &ext4_fast_symlink_inode_operations;
  3849. nd_terminate_link(ei->i_data, inode->i_size,
  3850. sizeof(ei->i_data) - 1);
  3851. } else {
  3852. inode->i_op = &ext4_symlink_inode_operations;
  3853. ext4_set_aops(inode);
  3854. }
  3855. } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
  3856. S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
  3857. inode->i_op = &ext4_special_inode_operations;
  3858. if (raw_inode->i_block[0])
  3859. init_special_inode(inode, inode->i_mode,
  3860. old_decode_dev(le32_to_cpu(raw_inode->i_block[0])));
  3861. else
  3862. init_special_inode(inode, inode->i_mode,
  3863. new_decode_dev(le32_to_cpu(raw_inode->i_block[1])));
  3864. } else {
  3865. ret = -EIO;
  3866. EXT4_ERROR_INODE(inode, "bogus i_mode (%o)", inode->i_mode);
  3867. goto bad_inode;
  3868. }
  3869. brelse(iloc.bh);
  3870. ext4_set_inode_flags(inode);
  3871. unlock_new_inode(inode);
  3872. return inode;
  3873. bad_inode:
  3874. brelse(iloc.bh);
  3875. iget_failed(inode);
  3876. return ERR_PTR(ret);
  3877. }
  3878. static int ext4_inode_blocks_set(handle_t *handle,
  3879. struct ext4_inode *raw_inode,
  3880. struct ext4_inode_info *ei)
  3881. {
  3882. struct inode *inode = &(ei->vfs_inode);
  3883. u64 i_blocks = inode->i_blocks;
  3884. struct super_block *sb = inode->i_sb;
  3885. if (i_blocks <= ~0U) {
  3886. /*
  3887. * i_blocks can be represented in a 32 bit variable
  3888. * as multiple of 512 bytes
  3889. */
  3890. raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
  3891. raw_inode->i_blocks_high = 0;
  3892. ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE);
  3893. return 0;
  3894. }
  3895. if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_HUGE_FILE))
  3896. return -EFBIG;
  3897. if (i_blocks <= 0xffffffffffffULL) {
  3898. /*
  3899. * i_blocks can be represented in a 48 bit variable
  3900. * as multiple of 512 bytes
  3901. */
  3902. raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
  3903. raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32);
  3904. ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE);
  3905. } else {
  3906. ext4_set_inode_flag(inode, EXT4_INODE_HUGE_FILE);
  3907. /* i_block is stored in file system block size */
  3908. i_blocks = i_blocks >> (inode->i_blkbits - 9);
  3909. raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
  3910. raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32);
  3911. }
  3912. return 0;
  3913. }
  3914. /*
  3915. * Post the struct inode info into an on-disk inode location in the
  3916. * buffer-cache. This gobbles the caller's reference to the
  3917. * buffer_head in the inode location struct.
  3918. *
  3919. * The caller must have write access to iloc->bh.
  3920. */
  3921. static int ext4_do_update_inode(handle_t *handle,
  3922. struct inode *inode,
  3923. struct ext4_iloc *iloc)
  3924. {
  3925. struct ext4_inode *raw_inode = ext4_raw_inode(iloc);
  3926. struct ext4_inode_info *ei = EXT4_I(inode);
  3927. struct buffer_head *bh = iloc->bh;
  3928. int err = 0, rc, block;
  3929. int need_datasync = 0;
  3930. uid_t i_uid;
  3931. gid_t i_gid;
  3932. /* For fields not not tracking in the in-memory inode,
  3933. * initialise them to zero for new inodes. */
  3934. if (ext4_test_inode_state(inode, EXT4_STATE_NEW))
  3935. memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
  3936. ext4_get_inode_flags(ei);
  3937. raw_inode->i_mode = cpu_to_le16(inode->i_mode);
  3938. i_uid = i_uid_read(inode);
  3939. i_gid = i_gid_read(inode);
  3940. if (!(test_opt(inode->i_sb, NO_UID32))) {
  3941. raw_inode->i_uid_low = cpu_to_le16(low_16_bits(i_uid));
  3942. raw_inode->i_gid_low = cpu_to_le16(low_16_bits(i_gid));
  3943. /*
  3944. * Fix up interoperability with old kernels. Otherwise, old inodes get
  3945. * re-used with the upper 16 bits of the uid/gid intact
  3946. */
  3947. if (!ei->i_dtime) {
  3948. raw_inode->i_uid_high =
  3949. cpu_to_le16(high_16_bits(i_uid));
  3950. raw_inode->i_gid_high =
  3951. cpu_to_le16(high_16_bits(i_gid));
  3952. } else {
  3953. raw_inode->i_uid_high = 0;
  3954. raw_inode->i_gid_high = 0;
  3955. }
  3956. } else {
  3957. raw_inode->i_uid_low = cpu_to_le16(fs_high2lowuid(i_uid));
  3958. raw_inode->i_gid_low = cpu_to_le16(fs_high2lowgid(i_gid));
  3959. raw_inode->i_uid_high = 0;
  3960. raw_inode->i_gid_high = 0;
  3961. }
  3962. raw_inode->i_links_count = cpu_to_le16(inode->i_nlink);
  3963. EXT4_INODE_SET_XTIME(i_ctime, inode, raw_inode);
  3964. EXT4_INODE_SET_XTIME(i_mtime, inode, raw_inode);
  3965. EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode);
  3966. EXT4_EINODE_SET_XTIME(i_crtime, ei, raw_inode);
  3967. if (ext4_inode_blocks_set(handle, raw_inode, ei))
  3968. goto out_brelse;
  3969. raw_inode->i_dtime = cpu_to_le32(ei->i_dtime);
  3970. raw_inode->i_flags = cpu_to_le32(ei->i_flags & 0xFFFFFFFF);
  3971. if (EXT4_SB(inode->i_sb)->s_es->s_creator_os !=
  3972. cpu_to_le32(EXT4_OS_HURD))
  3973. raw_inode->i_file_acl_high =
  3974. cpu_to_le16(ei->i_file_acl >> 32);
  3975. raw_inode->i_file_acl_lo = cpu_to_le32(ei->i_file_acl);
  3976. if (ei->i_disksize != ext4_isize(raw_inode)) {
  3977. ext4_isize_set(raw_inode, ei->i_disksize);
  3978. need_datasync = 1;
  3979. }
  3980. if (ei->i_disksize > 0x7fffffffULL) {
  3981. struct super_block *sb = inode->i_sb;
  3982. if (!EXT4_HAS_RO_COMPAT_FEATURE(sb,
  3983. EXT4_FEATURE_RO_COMPAT_LARGE_FILE) ||
  3984. EXT4_SB(sb)->s_es->s_rev_level ==
  3985. cpu_to_le32(EXT4_GOOD_OLD_REV)) {
  3986. /* If this is the first large file
  3987. * created, add a flag to the superblock.
  3988. */
  3989. err = ext4_journal_get_write_access(handle,
  3990. EXT4_SB(sb)->s_sbh);
  3991. if (err)
  3992. goto out_brelse;
  3993. ext4_update_dynamic_rev(sb);
  3994. EXT4_SET_RO_COMPAT_FEATURE(sb,
  3995. EXT4_FEATURE_RO_COMPAT_LARGE_FILE);
  3996. ext4_handle_sync(handle);
  3997. err = ext4_handle_dirty_super(handle, sb);
  3998. }
  3999. }
  4000. raw_inode->i_generation = cpu_to_le32(inode->i_generation);
  4001. if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
  4002. if (old_valid_dev(inode->i_rdev)) {
  4003. raw_inode->i_block[0] =
  4004. cpu_to_le32(old_encode_dev(inode->i_rdev));
  4005. raw_inode->i_block[1] = 0;
  4006. } else {
  4007. raw_inode->i_block[0] = 0;
  4008. raw_inode->i_block[1] =
  4009. cpu_to_le32(new_encode_dev(inode->i_rdev));
  4010. raw_inode->i_block[2] = 0;
  4011. }
  4012. } else if (!ext4_has_inline_data(inode)) {
  4013. for (block = 0; block < EXT4_N_BLOCKS; block++)
  4014. raw_inode->i_block[block] = ei->i_data[block];
  4015. }
  4016. raw_inode->i_disk_version = cpu_to_le32(inode->i_version);
  4017. if (ei->i_extra_isize) {
  4018. if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi))
  4019. raw_inode->i_version_hi =
  4020. cpu_to_le32(inode->i_version >> 32);
  4021. raw_inode->i_extra_isize = cpu_to_le16(ei->i_extra_isize);
  4022. }
  4023. ext4_inode_csum_set(inode, raw_inode, ei);
  4024. BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
  4025. rc = ext4_handle_dirty_metadata(handle, NULL, bh);
  4026. if (!err)
  4027. err = rc;
  4028. ext4_clear_inode_state(inode, EXT4_STATE_NEW);
  4029. ext4_update_inode_fsync_trans(handle, inode, need_datasync);
  4030. out_brelse:
  4031. brelse(bh);
  4032. ext4_std_error(inode->i_sb, err);
  4033. return err;
  4034. }
  4035. /*
  4036. * ext4_write_inode()
  4037. *
  4038. * We are called from a few places:
  4039. *
  4040. * - Within generic_file_write() for O_SYNC files.
  4041. * Here, there will be no transaction running. We wait for any running
  4042. * transaction to commit.
  4043. *
  4044. * - Within sys_sync(), kupdate and such.
  4045. * We wait on commit, if tol to.
  4046. *
  4047. * - Within prune_icache() (PF_MEMALLOC == true)
  4048. * Here we simply return. We can't afford to block kswapd on the
  4049. * journal commit.
  4050. *
  4051. * In all cases it is actually safe for us to return without doing anything,
  4052. * because the inode has been copied into a raw inode buffer in
  4053. * ext4_mark_inode_dirty(). This is a correctness thing for O_SYNC and for
  4054. * knfsd.
  4055. *
  4056. * Note that we are absolutely dependent upon all inode dirtiers doing the
  4057. * right thing: they *must* call mark_inode_dirty() after dirtying info in
  4058. * which we are interested.
  4059. *
  4060. * It would be a bug for them to not do this. The code:
  4061. *
  4062. * mark_inode_dirty(inode)
  4063. * stuff();
  4064. * inode->i_size = expr;
  4065. *
  4066. * is in error because a kswapd-driven write_inode() could occur while
  4067. * `stuff()' is running, and the new i_size will be lost. Plus the inode
  4068. * will no longer be on the superblock's dirty inode list.
  4069. */
  4070. int ext4_write_inode(struct inode *inode, struct writeback_control *wbc)
  4071. {
  4072. int err;
  4073. if (current->flags & PF_MEMALLOC)
  4074. return 0;
  4075. if (EXT4_SB(inode->i_sb)->s_journal) {
  4076. if (ext4_journal_current_handle()) {
  4077. jbd_debug(1, "called recursively, non-PF_MEMALLOC!\n");
  4078. dump_stack();
  4079. return -EIO;
  4080. }
  4081. if (wbc->sync_mode != WB_SYNC_ALL)
  4082. return 0;
  4083. err = ext4_force_commit(inode->i_sb);
  4084. } else {
  4085. struct ext4_iloc iloc;
  4086. err = __ext4_get_inode_loc(inode, &iloc, 0);
  4087. if (err)
  4088. return err;
  4089. if (wbc->sync_mode == WB_SYNC_ALL)
  4090. sync_dirty_buffer(iloc.bh);
  4091. if (buffer_req(iloc.bh) && !buffer_uptodate(iloc.bh)) {
  4092. EXT4_ERROR_INODE_BLOCK(inode, iloc.bh->b_blocknr,
  4093. "IO error syncing inode");
  4094. err = -EIO;
  4095. }
  4096. brelse(iloc.bh);
  4097. }
  4098. return err;
  4099. }
  4100. /*
  4101. * In data=journal mode ext4_journalled_invalidatepage() may fail to invalidate
  4102. * buffers that are attached to a page stradding i_size and are undergoing
  4103. * commit. In that case we have to wait for commit to finish and try again.
  4104. */
  4105. static void ext4_wait_for_tail_page_commit(struct inode *inode)
  4106. {
  4107. struct page *page;
  4108. unsigned offset;
  4109. journal_t *journal = EXT4_SB(inode->i_sb)->s_journal;
  4110. tid_t commit_tid = 0;
  4111. int ret;
  4112. offset = inode->i_size & (PAGE_CACHE_SIZE - 1);
  4113. /*
  4114. * All buffers in the last page remain valid? Then there's nothing to
  4115. * do. We do the check mainly to optimize the common PAGE_CACHE_SIZE ==
  4116. * blocksize case
  4117. */
  4118. if (offset > PAGE_CACHE_SIZE - (1 << inode->i_blkbits))
  4119. return;
  4120. while (1) {
  4121. page = find_lock_page(inode->i_mapping,
  4122. inode->i_size >> PAGE_CACHE_SHIFT);
  4123. if (!page)
  4124. return;
  4125. ret = __ext4_journalled_invalidatepage(page, offset);
  4126. unlock_page(page);
  4127. page_cache_release(page);
  4128. if (ret != -EBUSY)
  4129. return;
  4130. commit_tid = 0;
  4131. read_lock(&journal->j_state_lock);
  4132. if (journal->j_committing_transaction)
  4133. commit_tid = journal->j_committing_transaction->t_tid;
  4134. read_unlock(&journal->j_state_lock);
  4135. if (commit_tid)
  4136. jbd2_log_wait_commit(journal, commit_tid);
  4137. }
  4138. }
  4139. /*
  4140. * ext4_setattr()
  4141. *
  4142. * Called from notify_change.
  4143. *
  4144. * We want to trap VFS attempts to truncate the file as soon as
  4145. * possible. In particular, we want to make sure that when the VFS
  4146. * shrinks i_size, we put the inode on the orphan list and modify
  4147. * i_disksize immediately, so that during the subsequent flushing of
  4148. * dirty pages and freeing of disk blocks, we can guarantee that any
  4149. * commit will leave the blocks being flushed in an unused state on
  4150. * disk. (On recovery, the inode will get truncated and the blocks will
  4151. * be freed, so we have a strong guarantee that no future commit will
  4152. * leave these blocks visible to the user.)
  4153. *
  4154. * Another thing we have to assure is that if we are in ordered mode
  4155. * and inode is still attached to the committing transaction, we must
  4156. * we start writeout of all the dirty pages which are being truncated.
  4157. * This way we are sure that all the data written in the previous
  4158. * transaction are already on disk (truncate waits for pages under
  4159. * writeback).
  4160. *
  4161. * Called with inode->i_mutex down.
  4162. */
  4163. int ext4_setattr(struct dentry *dentry, struct iattr *attr)
  4164. {
  4165. struct inode *inode = dentry->d_inode;
  4166. int error, rc = 0;
  4167. int orphan = 0;
  4168. const unsigned int ia_valid = attr->ia_valid;
  4169. error = inode_change_ok(inode, attr);
  4170. if (error)
  4171. return error;
  4172. if (is_quota_modification(inode, attr))
  4173. dquot_initialize(inode);
  4174. if ((ia_valid & ATTR_UID && !uid_eq(attr->ia_uid, inode->i_uid)) ||
  4175. (ia_valid & ATTR_GID && !gid_eq(attr->ia_gid, inode->i_gid))) {
  4176. handle_t *handle;
  4177. /* (user+group)*(old+new) structure, inode write (sb,
  4178. * inode block, ? - but truncate inode update has it) */
  4179. handle = ext4_journal_start(inode, EXT4_HT_QUOTA,
  4180. (EXT4_MAXQUOTAS_INIT_BLOCKS(inode->i_sb) +
  4181. EXT4_MAXQUOTAS_DEL_BLOCKS(inode->i_sb)) + 3);
  4182. if (IS_ERR(handle)) {
  4183. error = PTR_ERR(handle);
  4184. goto err_out;
  4185. }
  4186. error = dquot_transfer(inode, attr);
  4187. if (error) {
  4188. ext4_journal_stop(handle);
  4189. return error;
  4190. }
  4191. /* Update corresponding info in inode so that everything is in
  4192. * one transaction */
  4193. if (attr->ia_valid & ATTR_UID)
  4194. inode->i_uid = attr->ia_uid;
  4195. if (attr->ia_valid & ATTR_GID)
  4196. inode->i_gid = attr->ia_gid;
  4197. error = ext4_mark_inode_dirty(handle, inode);
  4198. ext4_journal_stop(handle);
  4199. }
  4200. if (attr->ia_valid & ATTR_SIZE) {
  4201. if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
  4202. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  4203. if (attr->ia_size > sbi->s_bitmap_maxbytes)
  4204. return -EFBIG;
  4205. }
  4206. }
  4207. if (S_ISREG(inode->i_mode) &&
  4208. attr->ia_valid & ATTR_SIZE &&
  4209. (attr->ia_size < inode->i_size)) {
  4210. handle_t *handle;
  4211. handle = ext4_journal_start(inode, EXT4_HT_INODE, 3);
  4212. if (IS_ERR(handle)) {
  4213. error = PTR_ERR(handle);
  4214. goto err_out;
  4215. }
  4216. if (ext4_handle_valid(handle)) {
  4217. error = ext4_orphan_add(handle, inode);
  4218. orphan = 1;
  4219. }
  4220. EXT4_I(inode)->i_disksize = attr->ia_size;
  4221. rc = ext4_mark_inode_dirty(handle, inode);
  4222. if (!error)
  4223. error = rc;
  4224. ext4_journal_stop(handle);
  4225. if (ext4_should_order_data(inode)) {
  4226. error = ext4_begin_ordered_truncate(inode,
  4227. attr->ia_size);
  4228. if (error) {
  4229. /* Do as much error cleanup as possible */
  4230. handle = ext4_journal_start(inode,
  4231. EXT4_HT_INODE, 3);
  4232. if (IS_ERR(handle)) {
  4233. ext4_orphan_del(NULL, inode);
  4234. goto err_out;
  4235. }
  4236. ext4_orphan_del(handle, inode);
  4237. orphan = 0;
  4238. ext4_journal_stop(handle);
  4239. goto err_out;
  4240. }
  4241. }
  4242. }
  4243. if (attr->ia_valid & ATTR_SIZE) {
  4244. if (attr->ia_size != inode->i_size) {
  4245. loff_t oldsize = inode->i_size;
  4246. i_size_write(inode, attr->ia_size);
  4247. /*
  4248. * Blocks are going to be removed from the inode. Wait
  4249. * for dio in flight. Temporarily disable
  4250. * dioread_nolock to prevent livelock.
  4251. */
  4252. if (orphan) {
  4253. if (!ext4_should_journal_data(inode)) {
  4254. ext4_inode_block_unlocked_dio(inode);
  4255. inode_dio_wait(inode);
  4256. ext4_inode_resume_unlocked_dio(inode);
  4257. } else
  4258. ext4_wait_for_tail_page_commit(inode);
  4259. }
  4260. /*
  4261. * Truncate pagecache after we've waited for commit
  4262. * in data=journal mode to make pages freeable.
  4263. */
  4264. truncate_pagecache(inode, oldsize, inode->i_size);
  4265. }
  4266. ext4_truncate(inode);
  4267. }
  4268. if (!rc) {
  4269. setattr_copy(inode, attr);
  4270. mark_inode_dirty(inode);
  4271. }
  4272. /*
  4273. * If the call to ext4_truncate failed to get a transaction handle at
  4274. * all, we need to clean up the in-core orphan list manually.
  4275. */
  4276. if (orphan && inode->i_nlink)
  4277. ext4_orphan_del(NULL, inode);
  4278. if (!rc && (ia_valid & ATTR_MODE))
  4279. rc = ext4_acl_chmod(inode);
  4280. err_out:
  4281. ext4_std_error(inode->i_sb, error);
  4282. if (!error)
  4283. error = rc;
  4284. return error;
  4285. }
  4286. int ext4_getattr(struct vfsmount *mnt, struct dentry *dentry,
  4287. struct kstat *stat)
  4288. {
  4289. struct inode *inode;
  4290. unsigned long delalloc_blocks;
  4291. inode = dentry->d_inode;
  4292. generic_fillattr(inode, stat);
  4293. /*
  4294. * We can't update i_blocks if the block allocation is delayed
  4295. * otherwise in the case of system crash before the real block
  4296. * allocation is done, we will have i_blocks inconsistent with
  4297. * on-disk file blocks.
  4298. * We always keep i_blocks updated together with real
  4299. * allocation. But to not confuse with user, stat
  4300. * will return the blocks that include the delayed allocation
  4301. * blocks for this file.
  4302. */
  4303. delalloc_blocks = EXT4_C2B(EXT4_SB(inode->i_sb),
  4304. EXT4_I(inode)->i_reserved_data_blocks);
  4305. stat->blocks += (delalloc_blocks << inode->i_sb->s_blocksize_bits)>>9;
  4306. return 0;
  4307. }
  4308. static int ext4_index_trans_blocks(struct inode *inode, int nrblocks, int chunk)
  4309. {
  4310. if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
  4311. return ext4_ind_trans_blocks(inode, nrblocks, chunk);
  4312. return ext4_ext_index_trans_blocks(inode, nrblocks, chunk);
  4313. }
  4314. /*
  4315. * Account for index blocks, block groups bitmaps and block group
  4316. * descriptor blocks if modify datablocks and index blocks
  4317. * worse case, the indexs blocks spread over different block groups
  4318. *
  4319. * If datablocks are discontiguous, they are possible to spread over
  4320. * different block groups too. If they are contiguous, with flexbg,
  4321. * they could still across block group boundary.
  4322. *
  4323. * Also account for superblock, inode, quota and xattr blocks
  4324. */
  4325. static int ext4_meta_trans_blocks(struct inode *inode, int nrblocks, int chunk)
  4326. {
  4327. ext4_group_t groups, ngroups = ext4_get_groups_count(inode->i_sb);
  4328. int gdpblocks;
  4329. int idxblocks;
  4330. int ret = 0;
  4331. /*
  4332. * How many index blocks need to touch to modify nrblocks?
  4333. * The "Chunk" flag indicating whether the nrblocks is
  4334. * physically contiguous on disk
  4335. *
  4336. * For Direct IO and fallocate, they calls get_block to allocate
  4337. * one single extent at a time, so they could set the "Chunk" flag
  4338. */
  4339. idxblocks = ext4_index_trans_blocks(inode, nrblocks, chunk);
  4340. ret = idxblocks;
  4341. /*
  4342. * Now let's see how many group bitmaps and group descriptors need
  4343. * to account
  4344. */
  4345. groups = idxblocks;
  4346. if (chunk)
  4347. groups += 1;
  4348. else
  4349. groups += nrblocks;
  4350. gdpblocks = groups;
  4351. if (groups > ngroups)
  4352. groups = ngroups;
  4353. if (groups > EXT4_SB(inode->i_sb)->s_gdb_count)
  4354. gdpblocks = EXT4_SB(inode->i_sb)->s_gdb_count;
  4355. /* bitmaps and block group descriptor blocks */
  4356. ret += groups + gdpblocks;
  4357. /* Blocks for super block, inode, quota and xattr blocks */
  4358. ret += EXT4_META_TRANS_BLOCKS(inode->i_sb);
  4359. return ret;
  4360. }
  4361. /*
  4362. * Calculate the total number of credits to reserve to fit
  4363. * the modification of a single pages into a single transaction,
  4364. * which may include multiple chunks of block allocations.
  4365. *
  4366. * This could be called via ext4_write_begin()
  4367. *
  4368. * We need to consider the worse case, when
  4369. * one new block per extent.
  4370. */
  4371. int ext4_writepage_trans_blocks(struct inode *inode)
  4372. {
  4373. int bpp = ext4_journal_blocks_per_page(inode);
  4374. int ret;
  4375. ret = ext4_meta_trans_blocks(inode, bpp, 0);
  4376. /* Account for data blocks for journalled mode */
  4377. if (ext4_should_journal_data(inode))
  4378. ret += bpp;
  4379. return ret;
  4380. }
  4381. /*
  4382. * Calculate the journal credits for a chunk of data modification.
  4383. *
  4384. * This is called from DIO, fallocate or whoever calling
  4385. * ext4_map_blocks() to map/allocate a chunk of contiguous disk blocks.
  4386. *
  4387. * journal buffers for data blocks are not included here, as DIO
  4388. * and fallocate do no need to journal data buffers.
  4389. */
  4390. int ext4_chunk_trans_blocks(struct inode *inode, int nrblocks)
  4391. {
  4392. return ext4_meta_trans_blocks(inode, nrblocks, 1);
  4393. }
  4394. /*
  4395. * The caller must have previously called ext4_reserve_inode_write().
  4396. * Give this, we know that the caller already has write access to iloc->bh.
  4397. */
  4398. int ext4_mark_iloc_dirty(handle_t *handle,
  4399. struct inode *inode, struct ext4_iloc *iloc)
  4400. {
  4401. int err = 0;
  4402. if (IS_I_VERSION(inode))
  4403. inode_inc_iversion(inode);
  4404. /* the do_update_inode consumes one bh->b_count */
  4405. get_bh(iloc->bh);
  4406. /* ext4_do_update_inode() does jbd2_journal_dirty_metadata */
  4407. err = ext4_do_update_inode(handle, inode, iloc);
  4408. put_bh(iloc->bh);
  4409. return err;
  4410. }
  4411. /*
  4412. * On success, We end up with an outstanding reference count against
  4413. * iloc->bh. This _must_ be cleaned up later.
  4414. */
  4415. int
  4416. ext4_reserve_inode_write(handle_t *handle, struct inode *inode,
  4417. struct ext4_iloc *iloc)
  4418. {
  4419. int err;
  4420. err = ext4_get_inode_loc(inode, iloc);
  4421. if (!err) {
  4422. BUFFER_TRACE(iloc->bh, "get_write_access");
  4423. err = ext4_journal_get_write_access(handle, iloc->bh);
  4424. if (err) {
  4425. brelse(iloc->bh);
  4426. iloc->bh = NULL;
  4427. }
  4428. }
  4429. ext4_std_error(inode->i_sb, err);
  4430. return err;
  4431. }
  4432. /*
  4433. * Expand an inode by new_extra_isize bytes.
  4434. * Returns 0 on success or negative error number on failure.
  4435. */
  4436. static int ext4_expand_extra_isize(struct inode *inode,
  4437. unsigned int new_extra_isize,
  4438. struct ext4_iloc iloc,
  4439. handle_t *handle)
  4440. {
  4441. struct ext4_inode *raw_inode;
  4442. struct ext4_xattr_ibody_header *header;
  4443. if (EXT4_I(inode)->i_extra_isize >= new_extra_isize)
  4444. return 0;
  4445. raw_inode = ext4_raw_inode(&iloc);
  4446. header = IHDR(inode, raw_inode);
  4447. /* No extended attributes present */
  4448. if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR) ||
  4449. header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)) {
  4450. memset((void *)raw_inode + EXT4_GOOD_OLD_INODE_SIZE, 0,
  4451. new_extra_isize);
  4452. EXT4_I(inode)->i_extra_isize = new_extra_isize;
  4453. return 0;
  4454. }
  4455. /* try to expand with EAs present */
  4456. return ext4_expand_extra_isize_ea(inode, new_extra_isize,
  4457. raw_inode, handle);
  4458. }
  4459. /*
  4460. * What we do here is to mark the in-core inode as clean with respect to inode
  4461. * dirtiness (it may still be data-dirty).
  4462. * This means that the in-core inode may be reaped by prune_icache
  4463. * without having to perform any I/O. This is a very good thing,
  4464. * because *any* task may call prune_icache - even ones which
  4465. * have a transaction open against a different journal.
  4466. *
  4467. * Is this cheating? Not really. Sure, we haven't written the
  4468. * inode out, but prune_icache isn't a user-visible syncing function.
  4469. * Whenever the user wants stuff synced (sys_sync, sys_msync, sys_fsync)
  4470. * we start and wait on commits.
  4471. */
  4472. int ext4_mark_inode_dirty(handle_t *handle, struct inode *inode)
  4473. {
  4474. struct ext4_iloc iloc;
  4475. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  4476. static unsigned int mnt_count;
  4477. int err, ret;
  4478. might_sleep();
  4479. trace_ext4_mark_inode_dirty(inode, _RET_IP_);
  4480. err = ext4_reserve_inode_write(handle, inode, &iloc);
  4481. if (ext4_handle_valid(handle) &&
  4482. EXT4_I(inode)->i_extra_isize < sbi->s_want_extra_isize &&
  4483. !ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND)) {
  4484. /*
  4485. * We need extra buffer credits since we may write into EA block
  4486. * with this same handle. If journal_extend fails, then it will
  4487. * only result in a minor loss of functionality for that inode.
  4488. * If this is felt to be critical, then e2fsck should be run to
  4489. * force a large enough s_min_extra_isize.
  4490. */
  4491. if ((jbd2_journal_extend(handle,
  4492. EXT4_DATA_TRANS_BLOCKS(inode->i_sb))) == 0) {
  4493. ret = ext4_expand_extra_isize(inode,
  4494. sbi->s_want_extra_isize,
  4495. iloc, handle);
  4496. if (ret) {
  4497. ext4_set_inode_state(inode,
  4498. EXT4_STATE_NO_EXPAND);
  4499. if (mnt_count !=
  4500. le16_to_cpu(sbi->s_es->s_mnt_count)) {
  4501. ext4_warning(inode->i_sb,
  4502. "Unable to expand inode %lu. Delete"
  4503. " some EAs or run e2fsck.",
  4504. inode->i_ino);
  4505. mnt_count =
  4506. le16_to_cpu(sbi->s_es->s_mnt_count);
  4507. }
  4508. }
  4509. }
  4510. }
  4511. if (!err)
  4512. err = ext4_mark_iloc_dirty(handle, inode, &iloc);
  4513. return err;
  4514. }
  4515. /*
  4516. * ext4_dirty_inode() is called from __mark_inode_dirty()
  4517. *
  4518. * We're really interested in the case where a file is being extended.
  4519. * i_size has been changed by generic_commit_write() and we thus need
  4520. * to include the updated inode in the current transaction.
  4521. *
  4522. * Also, dquot_alloc_block() will always dirty the inode when blocks
  4523. * are allocated to the file.
  4524. *
  4525. * If the inode is marked synchronous, we don't honour that here - doing
  4526. * so would cause a commit on atime updates, which we don't bother doing.
  4527. * We handle synchronous inodes at the highest possible level.
  4528. */
  4529. void ext4_dirty_inode(struct inode *inode, int flags)
  4530. {
  4531. handle_t *handle;
  4532. handle = ext4_journal_start(inode, EXT4_HT_INODE, 2);
  4533. if (IS_ERR(handle))
  4534. goto out;
  4535. ext4_mark_inode_dirty(handle, inode);
  4536. ext4_journal_stop(handle);
  4537. out:
  4538. return;
  4539. }
  4540. #if 0
  4541. /*
  4542. * Bind an inode's backing buffer_head into this transaction, to prevent
  4543. * it from being flushed to disk early. Unlike
  4544. * ext4_reserve_inode_write, this leaves behind no bh reference and
  4545. * returns no iloc structure, so the caller needs to repeat the iloc
  4546. * lookup to mark the inode dirty later.
  4547. */
  4548. static int ext4_pin_inode(handle_t *handle, struct inode *inode)
  4549. {
  4550. struct ext4_iloc iloc;
  4551. int err = 0;
  4552. if (handle) {
  4553. err = ext4_get_inode_loc(inode, &iloc);
  4554. if (!err) {
  4555. BUFFER_TRACE(iloc.bh, "get_write_access");
  4556. err = jbd2_journal_get_write_access(handle, iloc.bh);
  4557. if (!err)
  4558. err = ext4_handle_dirty_metadata(handle,
  4559. NULL,
  4560. iloc.bh);
  4561. brelse(iloc.bh);
  4562. }
  4563. }
  4564. ext4_std_error(inode->i_sb, err);
  4565. return err;
  4566. }
  4567. #endif
  4568. int ext4_change_inode_journal_flag(struct inode *inode, int val)
  4569. {
  4570. journal_t *journal;
  4571. handle_t *handle;
  4572. int err;
  4573. /*
  4574. * We have to be very careful here: changing a data block's
  4575. * journaling status dynamically is dangerous. If we write a
  4576. * data block to the journal, change the status and then delete
  4577. * that block, we risk forgetting to revoke the old log record
  4578. * from the journal and so a subsequent replay can corrupt data.
  4579. * So, first we make sure that the journal is empty and that
  4580. * nobody is changing anything.
  4581. */
  4582. journal = EXT4_JOURNAL(inode);
  4583. if (!journal)
  4584. return 0;
  4585. if (is_journal_aborted(journal))
  4586. return -EROFS;
  4587. /* We have to allocate physical blocks for delalloc blocks
  4588. * before flushing journal. otherwise delalloc blocks can not
  4589. * be allocated any more. even more truncate on delalloc blocks
  4590. * could trigger BUG by flushing delalloc blocks in journal.
  4591. * There is no delalloc block in non-journal data mode.
  4592. */
  4593. if (val && test_opt(inode->i_sb, DELALLOC)) {
  4594. err = ext4_alloc_da_blocks(inode);
  4595. if (err < 0)
  4596. return err;
  4597. }
  4598. /* Wait for all existing dio workers */
  4599. ext4_inode_block_unlocked_dio(inode);
  4600. inode_dio_wait(inode);
  4601. jbd2_journal_lock_updates(journal);
  4602. /*
  4603. * OK, there are no updates running now, and all cached data is
  4604. * synced to disk. We are now in a completely consistent state
  4605. * which doesn't have anything in the journal, and we know that
  4606. * no filesystem updates are running, so it is safe to modify
  4607. * the inode's in-core data-journaling state flag now.
  4608. */
  4609. if (val)
  4610. ext4_set_inode_flag(inode, EXT4_INODE_JOURNAL_DATA);
  4611. else {
  4612. jbd2_journal_flush(journal);
  4613. ext4_clear_inode_flag(inode, EXT4_INODE_JOURNAL_DATA);
  4614. }
  4615. ext4_set_aops(inode);
  4616. jbd2_journal_unlock_updates(journal);
  4617. ext4_inode_resume_unlocked_dio(inode);
  4618. /* Finally we can mark the inode as dirty. */
  4619. handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
  4620. if (IS_ERR(handle))
  4621. return PTR_ERR(handle);
  4622. err = ext4_mark_inode_dirty(handle, inode);
  4623. ext4_handle_sync(handle);
  4624. ext4_journal_stop(handle);
  4625. ext4_std_error(inode->i_sb, err);
  4626. return err;
  4627. }
  4628. static int ext4_bh_unmapped(handle_t *handle, struct buffer_head *bh)
  4629. {
  4630. return !buffer_mapped(bh);
  4631. }
  4632. int ext4_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
  4633. {
  4634. struct page *page = vmf->page;
  4635. loff_t size;
  4636. unsigned long len;
  4637. int ret;
  4638. struct file *file = vma->vm_file;
  4639. struct inode *inode = file_inode(file);
  4640. struct address_space *mapping = inode->i_mapping;
  4641. handle_t *handle;
  4642. get_block_t *get_block;
  4643. int retries = 0;
  4644. sb_start_pagefault(inode->i_sb);
  4645. file_update_time(vma->vm_file);
  4646. /* Delalloc case is easy... */
  4647. if (test_opt(inode->i_sb, DELALLOC) &&
  4648. !ext4_should_journal_data(inode) &&
  4649. !ext4_nonda_switch(inode->i_sb)) {
  4650. do {
  4651. ret = __block_page_mkwrite(vma, vmf,
  4652. ext4_da_get_block_prep);
  4653. } while (ret == -ENOSPC &&
  4654. ext4_should_retry_alloc(inode->i_sb, &retries));
  4655. goto out_ret;
  4656. }
  4657. lock_page(page);
  4658. size = i_size_read(inode);
  4659. /* Page got truncated from under us? */
  4660. if (page->mapping != mapping || page_offset(page) > size) {
  4661. unlock_page(page);
  4662. ret = VM_FAULT_NOPAGE;
  4663. goto out;
  4664. }
  4665. if (page->index == size >> PAGE_CACHE_SHIFT)
  4666. len = size & ~PAGE_CACHE_MASK;
  4667. else
  4668. len = PAGE_CACHE_SIZE;
  4669. /*
  4670. * Return if we have all the buffers mapped. This avoids the need to do
  4671. * journal_start/journal_stop which can block and take a long time
  4672. */
  4673. if (page_has_buffers(page)) {
  4674. if (!ext4_walk_page_buffers(NULL, page_buffers(page),
  4675. 0, len, NULL,
  4676. ext4_bh_unmapped)) {
  4677. /* Wait so that we don't change page under IO */
  4678. wait_for_stable_page(page);
  4679. ret = VM_FAULT_LOCKED;
  4680. goto out;
  4681. }
  4682. }
  4683. unlock_page(page);
  4684. /* OK, we need to fill the hole... */
  4685. if (ext4_should_dioread_nolock(inode))
  4686. get_block = ext4_get_block_write;
  4687. else
  4688. get_block = ext4_get_block;
  4689. retry_alloc:
  4690. handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE,
  4691. ext4_writepage_trans_blocks(inode));
  4692. if (IS_ERR(handle)) {
  4693. ret = VM_FAULT_SIGBUS;
  4694. goto out;
  4695. }
  4696. ret = __block_page_mkwrite(vma, vmf, get_block);
  4697. if (!ret && ext4_should_journal_data(inode)) {
  4698. if (ext4_walk_page_buffers(handle, page_buffers(page), 0,
  4699. PAGE_CACHE_SIZE, NULL, do_journal_get_write_access)) {
  4700. unlock_page(page);
  4701. ret = VM_FAULT_SIGBUS;
  4702. ext4_journal_stop(handle);
  4703. goto out;
  4704. }
  4705. ext4_set_inode_state(inode, EXT4_STATE_JDATA);
  4706. }
  4707. ext4_journal_stop(handle);
  4708. if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
  4709. goto retry_alloc;
  4710. out_ret:
  4711. ret = block_page_mkwrite_return(ret);
  4712. out:
  4713. sb_end_pagefault(inode->i_sb);
  4714. return ret;
  4715. }