inode.c 159 KB

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