inode.c 157 KB

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