inode.c 135 KB

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