inode.c 152 KB

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